mirror of
https://github.com/stablyai/orca.git
synced 2026-10-20 08:03:28 +00:00
* feat(agents): pi session resume support
* fix(pi): require persisted session files for resume
* test(sleeping-agent): use non-resumable sentinel in malformed-record fixture
The 'drops malformed sleeping agent resume records' test used agent:'pi' as
its example of an unknown/non-resumable agent, expecting the record to be
dropped. This PR added 'pi' to RESUMABLE_TUI_AGENTS, making that fixture
valid and retained, so the toBeUndefined assertion broke. Switch the
malformed-case fixture to a genuinely non-resumable sentinel
('definitely-not-an-agent') so the drop-malformed path is still exercised;
no other assertions changed.
* Add durable resume identity for Pi sessions without fabricating turn sta
Pi's `session_start` hook now carries the session file needed to resume
a sleeping pane, but until now Orca either discarded it or treated it
as a fake status transition. Thread a `providerSessionOnly` envelope
through the hook listener, relay, main-process server, and renderer
store so resume identity (and its session-file-scoped equality/claim
key) can be persisted and replayed without emitting prompt telemetry
or a visible working/done row.
* Add durable resume identity for completed Pi sessions
Pi's agent_end hook marks a turn done, but the underlying TUI session
stays alive and resumable. Previously a `done` status wiped sleeping
records and launch config as if the session ended, so hibernation,
manual worktree sleep, and quit-capture all lost Pi's resume identity.
- Track a "live recovery" record for done-but-still-resumable Pi
sessions, exempting it from the usual done-state cleanup paths in
agent-status.ts and agent-hibernation-planner.ts
- Gate providerSessionOnly rows and sleeping-agent schema records on
actual resumability (getAgentResumeArgv) instead of trusting the
presence of a provider session
- Wait for Pi to persist its session file before advertising resume
metadata, and treat `/reload` as a non-terminal event so it doesn't
clobber visible status
- Extend SSH relay envelopes to carry providerSessionOnly so remote
hosts get the same behavior
* Add explicit periodic/quit mode to sleeping-agent session capture
Split captureAllSleepingAgentSessions into 'periodic' and 'quit' modes
so a background checkpoint can no longer downgrade a confirmed-quit
record or promote a completed Pi session without an authoritative
transcript path. Updates all call sites and tests accordingly.
* Use normalizeAgentStatusPayload for default pi status
Remove unnecessary JSON.stringify wrapper and call the appropriate normalization function directly.
---------
Co-authored-by: Jinjing <6427696+AmethystLiang@users.noreply.github.com>
500 lines
15 KiB
TypeScript
500 lines
15 KiB
TypeScript
import { describe, it, expect } from 'vitest'
|
|
import { parseWorkspaceSession } from './workspace-session-schema'
|
|
|
|
describe('parseWorkspaceSession sleeping agents', () => {
|
|
it('preserves valid sleeping agent resume records', () => {
|
|
const result = parseWorkspaceSession({
|
|
activeRepoId: null,
|
|
activeWorktreeId: null,
|
|
activeTabId: null,
|
|
tabsByWorktree: {},
|
|
terminalLayoutsByTabId: {},
|
|
sleepingAgentSessionsByPaneKey: {
|
|
'tab1:pane-1': {
|
|
paneKey: 'tab1:pane-1',
|
|
tabId: 'tab1',
|
|
worktreeId: 'wt',
|
|
agent: 'codex',
|
|
providerSession: { key: 'session_id', id: 'codex-session' },
|
|
prompt: 'continue',
|
|
state: 'working',
|
|
capturedAt: 10,
|
|
updatedAt: 9,
|
|
terminalTitle: 'Codex',
|
|
lastAssistantMessage: 'done',
|
|
launchConfig: {
|
|
agentArgs: '',
|
|
agentEnv: {}
|
|
},
|
|
origin: 'live'
|
|
}
|
|
}
|
|
})
|
|
expect(result.ok).toBe(true)
|
|
if (result.ok) {
|
|
expect(result.value.sleepingAgentSessionsByPaneKey?.['tab1:pane-1']?.agent).toBe('codex')
|
|
expect(result.value.sleepingAgentSessionsByPaneKey?.['tab1:pane-1']?.origin).toBe('live')
|
|
expect(result.value.sleepingAgentSessionsByPaneKey?.['tab1:pane-1']?.launchConfig).toEqual({
|
|
agentArgs: '',
|
|
agentEnv: {}
|
|
})
|
|
}
|
|
})
|
|
|
|
it('preserves the authoritative Pi session file through hydration', () => {
|
|
const result = parseWorkspaceSession({
|
|
activeRepoId: null,
|
|
activeWorktreeId: null,
|
|
activeTabId: null,
|
|
tabsByWorktree: {},
|
|
terminalLayoutsByTabId: {},
|
|
sleepingAgentSessionsByPaneKey: {
|
|
'tab1:pane-1': {
|
|
paneKey: 'tab1:pane-1',
|
|
tabId: 'tab1',
|
|
worktreeId: 'wt',
|
|
agent: 'pi',
|
|
providerSession: {
|
|
key: 'session_id',
|
|
id: 'pi-session',
|
|
transcriptPath: '/tmp/pi-session.jsonl'
|
|
},
|
|
prompt: '',
|
|
state: 'working',
|
|
capturedAt: 10,
|
|
updatedAt: 10,
|
|
origin: 'live'
|
|
}
|
|
}
|
|
})
|
|
|
|
expect(result.ok).toBe(true)
|
|
if (result.ok) {
|
|
expect(result.value.sleepingAgentSessionsByPaneKey?.['tab1:pane-1']?.providerSession).toEqual(
|
|
{
|
|
key: 'session_id',
|
|
id: 'pi-session',
|
|
transcriptPath: '/tmp/pi-session.jsonl'
|
|
}
|
|
)
|
|
}
|
|
})
|
|
|
|
it('drops Pi sleeping-agent records without an authoritative session file', () => {
|
|
const result = parseWorkspaceSession({
|
|
activeRepoId: null,
|
|
activeWorktreeId: null,
|
|
activeTabId: null,
|
|
tabsByWorktree: {},
|
|
terminalLayoutsByTabId: {},
|
|
sleepingAgentSessionsByPaneKey: {
|
|
'tab1:pane-1': {
|
|
paneKey: 'tab1:pane-1',
|
|
tabId: 'tab1',
|
|
worktreeId: 'wt',
|
|
agent: 'pi',
|
|
providerSession: { key: 'session_id', id: 'pi-session' },
|
|
prompt: '',
|
|
state: 'done',
|
|
capturedAt: 10,
|
|
updatedAt: 10,
|
|
origin: 'live'
|
|
}
|
|
}
|
|
})
|
|
|
|
expect(result.ok).toBe(true)
|
|
if (result.ok) {
|
|
expect(result.value.sleepingAgentSessionsByPaneKey).toBeUndefined()
|
|
}
|
|
})
|
|
|
|
it('drops invalid sleeping agent launch config without dropping the record', () => {
|
|
const result = parseWorkspaceSession({
|
|
activeRepoId: null,
|
|
activeWorktreeId: null,
|
|
activeTabId: null,
|
|
tabsByWorktree: {},
|
|
terminalLayoutsByTabId: {},
|
|
sleepingAgentSessionsByPaneKey: {
|
|
'tab1:pane-1': {
|
|
paneKey: 'tab1:pane-1',
|
|
tabId: 'tab1',
|
|
worktreeId: 'wt',
|
|
agent: 'codex',
|
|
providerSession: { key: 'session_id', id: 'codex-session' },
|
|
prompt: 'continue',
|
|
state: 'working',
|
|
capturedAt: 10,
|
|
updatedAt: 9,
|
|
launchConfig: {
|
|
agentArgs: '--model high',
|
|
agentEnv: { 'BAD=KEY': 'value' }
|
|
}
|
|
}
|
|
}
|
|
})
|
|
|
|
expect(result.ok).toBe(true)
|
|
if (result.ok) {
|
|
const record = result.value.sleepingAgentSessionsByPaneKey?.['tab1:pane-1']
|
|
expect(record?.agent).toBe('codex')
|
|
expect(record?.launchConfig).toBeUndefined()
|
|
}
|
|
})
|
|
|
|
it('drops launch config with prototype-polluting env keys without dropping siblings', () => {
|
|
const sessions = JSON.parse(`{
|
|
"__proto__": {
|
|
"paneKey": "__proto__",
|
|
"worktreeId": "wt",
|
|
"agent": "codex",
|
|
"providerSession": { "key": "session_id", "id": "bad-session" },
|
|
"prompt": "bad",
|
|
"state": "working",
|
|
"capturedAt": 10,
|
|
"updatedAt": 9
|
|
},
|
|
"tab1:pane-1": {
|
|
"paneKey": "tab1:pane-1",
|
|
"tabId": "tab1",
|
|
"worktreeId": "wt",
|
|
"agent": "codex",
|
|
"providerSession": { "key": "session_id", "id": "codex-session" },
|
|
"prompt": "continue",
|
|
"state": "working",
|
|
"capturedAt": 10,
|
|
"updatedAt": 9,
|
|
"launchConfig": {
|
|
"agentArgs": "",
|
|
"agentEnv": { "__proto__": "polluted" }
|
|
}
|
|
}
|
|
}`)
|
|
const result = parseWorkspaceSession({
|
|
activeRepoId: null,
|
|
activeWorktreeId: null,
|
|
activeTabId: null,
|
|
tabsByWorktree: {},
|
|
terminalLayoutsByTabId: {},
|
|
sleepingAgentSessionsByPaneKey: sessions
|
|
})
|
|
|
|
expect(result.ok).toBe(true)
|
|
if (result.ok) {
|
|
expect(
|
|
Object.prototype.hasOwnProperty.call(
|
|
result.value.sleepingAgentSessionsByPaneKey ?? {},
|
|
'__proto__'
|
|
)
|
|
).toBe(false)
|
|
const record = result.value.sleepingAgentSessionsByPaneKey?.['tab1:pane-1']
|
|
expect(record?.agent).toBe('codex')
|
|
expect(record?.launchConfig).toBeUndefined()
|
|
expect(({} as Record<string, unknown>).polluted).toBeUndefined()
|
|
}
|
|
})
|
|
|
|
it('preserves sleeping agent launch env values with whitespace characters', () => {
|
|
const result = parseWorkspaceSession({
|
|
activeRepoId: null,
|
|
activeWorktreeId: null,
|
|
activeTabId: null,
|
|
tabsByWorktree: {},
|
|
terminalLayoutsByTabId: {},
|
|
sleepingAgentSessionsByPaneKey: {
|
|
'tab1:pane-1': {
|
|
paneKey: 'tab1:pane-1',
|
|
tabId: 'tab1',
|
|
worktreeId: 'wt',
|
|
agent: 'codex',
|
|
providerSession: { key: 'session_id', id: 'codex-session' },
|
|
prompt: 'continue',
|
|
state: 'working',
|
|
capturedAt: 10,
|
|
updatedAt: 9,
|
|
launchConfig: {
|
|
agentArgs: '',
|
|
agentEnv: { MULTILINE: 'line1\nline2\tok' }
|
|
}
|
|
}
|
|
}
|
|
})
|
|
|
|
expect(result.ok).toBe(true)
|
|
if (result.ok) {
|
|
expect(
|
|
result.value.sleepingAgentSessionsByPaneKey?.['tab1:pane-1']?.launchConfig?.agentEnv
|
|
).toEqual({ MULTILINE: 'line1\nline2\tok' })
|
|
}
|
|
})
|
|
|
|
it('drops sleeping agent launch config with NUL env values without dropping the record', () => {
|
|
const result = parseWorkspaceSession({
|
|
activeRepoId: null,
|
|
activeWorktreeId: null,
|
|
activeTabId: null,
|
|
tabsByWorktree: {},
|
|
terminalLayoutsByTabId: {},
|
|
sleepingAgentSessionsByPaneKey: {
|
|
'tab1:pane-1': {
|
|
paneKey: 'tab1:pane-1',
|
|
tabId: 'tab1',
|
|
worktreeId: 'wt',
|
|
agent: 'codex',
|
|
providerSession: { key: 'session_id', id: 'codex-session' },
|
|
prompt: 'continue',
|
|
state: 'working',
|
|
capturedAt: 10,
|
|
updatedAt: 9,
|
|
launchConfig: {
|
|
agentArgs: '',
|
|
agentEnv: { BAD_VALUE: 'ok\0bad' }
|
|
}
|
|
}
|
|
}
|
|
})
|
|
|
|
expect(result.ok).toBe(true)
|
|
if (result.ok) {
|
|
const record = result.value.sleepingAgentSessionsByPaneKey?.['tab1:pane-1']
|
|
expect(record?.agent).toBe('codex')
|
|
expect(record?.launchConfig).toBeUndefined()
|
|
}
|
|
})
|
|
|
|
it('preserves sleeping agent record origin across hydration', () => {
|
|
const result = parseWorkspaceSession({
|
|
activeRepoId: null,
|
|
activeWorktreeId: null,
|
|
activeTabId: null,
|
|
tabsByWorktree: {},
|
|
terminalLayoutsByTabId: {},
|
|
sleepingAgentSessionsByPaneKey: {
|
|
'tab1:pane-1': {
|
|
paneKey: 'tab1:pane-1',
|
|
tabId: 'tab1',
|
|
worktreeId: 'wt',
|
|
agent: 'devin',
|
|
providerSession: { key: 'session_id', id: 'devin-session' },
|
|
prompt: 'continue',
|
|
state: 'working',
|
|
capturedAt: 10,
|
|
updatedAt: 9,
|
|
origin: 'quit'
|
|
}
|
|
}
|
|
})
|
|
|
|
expect(result.ok).toBe(true)
|
|
if (result.ok) {
|
|
expect(result.value.sleepingAgentSessionsByPaneKey?.['tab1:pane-1']?.origin).toBe('quit')
|
|
}
|
|
})
|
|
|
|
it('preserves interrupted sleeping agent records across hydration', () => {
|
|
const result = parseWorkspaceSession({
|
|
activeRepoId: null,
|
|
activeWorktreeId: null,
|
|
activeTabId: null,
|
|
tabsByWorktree: {},
|
|
terminalLayoutsByTabId: {},
|
|
sleepingAgentSessionsByPaneKey: {
|
|
'tab1:pane-1': {
|
|
paneKey: 'tab1:pane-1',
|
|
tabId: 'tab1',
|
|
worktreeId: 'wt',
|
|
agent: 'codex',
|
|
providerSession: { key: 'session_id', id: 'codex-session' },
|
|
prompt: 'continue',
|
|
state: 'done',
|
|
capturedAt: 10,
|
|
updatedAt: 9,
|
|
interrupted: true,
|
|
origin: 'worktree-sleep'
|
|
}
|
|
}
|
|
})
|
|
|
|
expect(result.ok).toBe(true)
|
|
if (result.ok) {
|
|
expect(result.value.sleepingAgentSessionsByPaneKey?.['tab1:pane-1']?.interrupted).toBe(true)
|
|
}
|
|
})
|
|
|
|
it('preserves legacy live sleeping agent origins across hydration', () => {
|
|
const result = parseWorkspaceSession({
|
|
activeRepoId: null,
|
|
activeWorktreeId: null,
|
|
activeTabId: null,
|
|
tabsByWorktree: {},
|
|
terminalLayoutsByTabId: {},
|
|
sleepingAgentSessionsByPaneKey: {
|
|
'tab1:pane-1': {
|
|
paneKey: 'tab1:pane-1',
|
|
tabId: 'tab1',
|
|
worktreeId: 'wt',
|
|
agent: 'codex',
|
|
providerSession: { key: 'session_id', id: 'codex-session' },
|
|
prompt: 'continue',
|
|
state: 'working',
|
|
capturedAt: 10,
|
|
updatedAt: 9,
|
|
origin: 'live'
|
|
}
|
|
}
|
|
})
|
|
|
|
expect(result.ok).toBe(true)
|
|
if (result.ok) {
|
|
expect(result.value.sleepingAgentSessionsByPaneKey?.['tab1:pane-1']?.origin).toBe('live')
|
|
}
|
|
})
|
|
|
|
it('drops malformed sleeping agent resume records without failing the whole session', () => {
|
|
const result = parseWorkspaceSession({
|
|
activeRepoId: null,
|
|
activeWorktreeId: null,
|
|
activeTabId: null,
|
|
tabsByWorktree: {},
|
|
terminalLayoutsByTabId: {},
|
|
sleepingAgentSessionsByPaneKey: {
|
|
'tab1:pane-1': {
|
|
paneKey: 'tab1:pane-1',
|
|
worktreeId: 'wt',
|
|
agent: 'definitely-not-an-agent',
|
|
providerSession: { key: 'session_id', id: 'bogus-session' },
|
|
prompt: 'continue',
|
|
state: 'working',
|
|
capturedAt: 10,
|
|
updatedAt: 9
|
|
}
|
|
}
|
|
})
|
|
expect(result.ok).toBe(true)
|
|
if (result.ok) {
|
|
expect(result.value.sleepingAgentSessionsByPaneKey).toBeUndefined()
|
|
}
|
|
})
|
|
|
|
it('preserves valid sleeping agent resume records when sibling records are malformed', () => {
|
|
const result = parseWorkspaceSession({
|
|
activeRepoId: null,
|
|
activeWorktreeId: null,
|
|
activeTabId: null,
|
|
tabsByWorktree: {},
|
|
terminalLayoutsByTabId: {},
|
|
sleepingAgentSessionsByPaneKey: {
|
|
'tab1:pane-1': {
|
|
paneKey: 'tab1:pane-1',
|
|
tabId: 'tab1',
|
|
worktreeId: 'wt',
|
|
agent: 'codex',
|
|
providerSession: { key: 'session_id', id: 'codex-session' },
|
|
prompt: 'continue',
|
|
state: 'working',
|
|
capturedAt: 10,
|
|
updatedAt: 9
|
|
},
|
|
'tab2:pane-1': {
|
|
paneKey: 'tab2:pane-1',
|
|
worktreeId: 'wt',
|
|
agent: 'not-real',
|
|
providerSession: { key: 'session_id', id: 'bad-session' },
|
|
prompt: 'ignore me',
|
|
state: 'working',
|
|
capturedAt: 10,
|
|
updatedAt: 9
|
|
}
|
|
}
|
|
})
|
|
expect(result.ok).toBe(true)
|
|
if (result.ok) {
|
|
expect(result.value.sleepingAgentSessionsByPaneKey?.['tab1:pane-1']?.agent).toBe('codex')
|
|
expect(result.value.sleepingAgentSessionsByPaneKey?.['tab2:pane-1']).toBeUndefined()
|
|
}
|
|
})
|
|
|
|
it('drops sleeping agent records with unsafe provider session ids without dropping valid siblings', () => {
|
|
const result = parseWorkspaceSession({
|
|
activeRepoId: null,
|
|
activeWorktreeId: null,
|
|
activeTabId: null,
|
|
tabsByWorktree: {},
|
|
terminalLayoutsByTabId: {},
|
|
sleepingAgentSessionsByPaneKey: {
|
|
'tab1:pane-1': {
|
|
paneKey: 'tab1:pane-1',
|
|
tabId: 'tab1',
|
|
worktreeId: 'wt',
|
|
agent: 'codex',
|
|
providerSession: { key: 'session_id', id: 'codex-session' },
|
|
prompt: 'continue',
|
|
state: 'working',
|
|
capturedAt: 10,
|
|
updatedAt: 9
|
|
},
|
|
'tab2:pane-1': {
|
|
paneKey: 'tab2:pane-1',
|
|
tabId: 'tab2',
|
|
worktreeId: 'wt',
|
|
agent: 'codex',
|
|
providerSession: { key: 'session_id', id: '--last' },
|
|
prompt: 'ignore me',
|
|
state: 'working',
|
|
capturedAt: 10,
|
|
updatedAt: 9
|
|
}
|
|
}
|
|
})
|
|
expect(result.ok).toBe(true)
|
|
if (result.ok) {
|
|
expect(result.value.sleepingAgentSessionsByPaneKey?.['tab1:pane-1']?.providerSession.id).toBe(
|
|
'codex-session'
|
|
)
|
|
expect(result.value.sleepingAgentSessionsByPaneKey?.['tab2:pane-1']).toBeUndefined()
|
|
}
|
|
})
|
|
|
|
it('drops sleeping agent records whose embedded pane key differs from the map key', () => {
|
|
const result = parseWorkspaceSession({
|
|
activeRepoId: null,
|
|
activeWorktreeId: null,
|
|
activeTabId: null,
|
|
tabsByWorktree: {},
|
|
terminalLayoutsByTabId: {},
|
|
sleepingAgentSessionsByPaneKey: {
|
|
'tab1:pane-1': {
|
|
paneKey: 'tab1:pane-1',
|
|
tabId: 'tab1',
|
|
worktreeId: 'wt',
|
|
agent: 'codex',
|
|
providerSession: { key: 'session_id', id: 'codex-session' },
|
|
prompt: 'continue',
|
|
state: 'working',
|
|
capturedAt: 10,
|
|
updatedAt: 9
|
|
},
|
|
'tab2:pane-1': {
|
|
paneKey: 'other-tab:pane-1',
|
|
tabId: 'tab2',
|
|
worktreeId: 'wt',
|
|
agent: 'codex',
|
|
providerSession: { key: 'session_id', id: 'mismatched-session' },
|
|
prompt: 'ignore me',
|
|
state: 'working',
|
|
capturedAt: 10,
|
|
updatedAt: 9
|
|
}
|
|
}
|
|
})
|
|
expect(result.ok).toBe(true)
|
|
if (result.ok) {
|
|
expect(result.value.sleepingAgentSessionsByPaneKey?.['tab1:pane-1']?.providerSession.id).toBe(
|
|
'codex-session'
|
|
)
|
|
expect(result.value.sleepingAgentSessionsByPaneKey?.['tab2:pane-1']).toBeUndefined()
|
|
}
|
|
})
|
|
})
|