diff --git a/src/main/agent-hooks/server.test.ts b/src/main/agent-hooks/server.test.ts index 5f63bcd02..b3ff89ab3 100644 --- a/src/main/agent-hooks/server.test.ts +++ b/src/main/agent-hooks/server.test.ts @@ -2219,6 +2219,97 @@ describe('AgentHookServer listener replay', () => { } }) + it('accepts a new local prompt after launch authority retires in a reusable pane', async () => { + const server = new AgentHookServer() + await server.start({ env: 'production' }) + try { + const env = server.buildPtyEnv() + const postHook = (payload: Record): Promise => + fetch(`http://127.0.0.1:${env.ORCA_AGENT_HOOK_PORT}/hook/claude`, { + method: 'POST', + headers: { + 'Content-Type': 'application/json', + 'X-Orca-Agent-Hook-Token': env.ORCA_AGENT_HOOK_TOKEN + }, + body: JSON.stringify(buildBody(payload, { launchToken: 'retired-launch-token' })) + }) + + await postHook({ hook_event_name: 'UserPromptSubmit', prompt: 'first launch' }) + server.retirePaneAuthority(PANE) + await postHook({ hook_event_name: 'Stop', last_assistant_message: 'late prior hook' }) + expect(server.getStatusSnapshot()).toEqual([]) + + await postHook({ hook_event_name: 'UserPromptSubmit', prompt: 'manual restart' }) + + expect(server.getStatusSnapshot()).toEqual([ + expect.objectContaining({ paneKey: PANE, state: 'working', prompt: 'manual restart' }) + ]) + expect( + server.attestCompatibilityAuthority({ + paneKey: PANE, + launchTokenHash: createHash('sha256').update('retired-launch-token').digest('hex'), + connectionId: null, + terminalProvenance: 'current_runtime' + }) + ).toBeNull() + } finally { + server.stop() + } + }) + + it('accepts a live remote prompt but not replay after launch authority retires', () => { + const server = new AgentHookServer() + server.ingestRemote( + { + paneKey: PANE, + hookEventName: 'UserPromptSubmit', + launchToken: 'retired-remote-launch-token', + payload: { state: 'working', prompt: 'first launch', agentType: 'claude' } + }, + 'conn-1' + ) + server.retirePaneAuthority(PANE) + + server.ingestRemote( + { + paneKey: PANE, + hookEventName: 'UserPromptSubmit', + launchToken: 'retired-remote-launch-token', + isReplay: true, + payload: { state: 'working', prompt: 'stale replay', agentType: 'claude' } + }, + 'conn-1' + ) + expect(server.getStatusSnapshot()).toEqual([]) + + server.ingestRemote( + { + paneKey: PANE, + hookEventName: 'UserPromptSubmit', + launchToken: 'retired-remote-launch-token', + payload: { state: 'working', prompt: 'remote manual restart', agentType: 'claude' } + }, + 'conn-1' + ) + + expect(server.getStatusSnapshot()).toEqual([ + expect.objectContaining({ + paneKey: PANE, + state: 'working', + prompt: 'remote manual restart', + connectionId: 'conn-1' + }) + ]) + expect( + server.attestCompatibilityAuthority({ + paneKey: PANE, + launchTokenHash: createHash('sha256').update('retired-remote-launch-token').digest('hex'), + connectionId: 'conn-1', + terminalProvenance: 'current_runtime' + }) + ).toBeNull() + }) + it('hydrates cached statuses as not observed in the current runtime', async () => { const dir = mkdtempSync(join(tmpdir(), 'orca-agent-hooks-')) const firstServer = new AgentHookServer() diff --git a/src/main/agent-hooks/server.ts b/src/main/agent-hooks/server.ts index 1eeac8261..0f99f2cae 100644 --- a/src/main/agent-hooks/server.ts +++ b/src/main/agent-hooks/server.ts @@ -924,19 +924,28 @@ export class AgentHookServer { } } - private shouldSuppressClosedTabStatus(paneKey: string): boolean { + private getAgentStatusDisposition( + paneKey: string, + event?: { hookEventName?: string; isReplay?: boolean } + ): 'accept' | 'restart' | 'suppress' { const ownerPaneKey = this.resolvePaneKeyAlias(paneKey) - if ( + const paneRetired = this.closedAgentStatusPaneKeys.has(paneKey) || this.closedAgentStatusPaneKeys.has(ownerPaneKey) - ) { - return true - } const tabId = parsePaneKey(ownerPaneKey)?.tabId - if (!tabId) { - return false + if (tabId && this.closedAgentStatusTabIds.has(tabId)) { + return 'suppress' } - return this.closedAgentStatusTabIds.has(tabId) + if (!paneRetired) { + return 'accept' + } + // Why: command completion retires launch authority but leaves its shell pane reusable. + if (event?.hookEventName === 'UserPromptSubmit' && event.isReplay !== true) { + this.closedAgentStatusPaneKeys.delete(paneKey) + this.closedAgentStatusPaneKeys.delete(ownerPaneKey) + return 'restart' + } + return 'suppress' } private markPaneClosedForAgentStatus(paneKey: string): void { @@ -1716,7 +1725,7 @@ export class AgentHookServer { return } const tabId = paneKey !== physicalPaneKey ? parsedPaneKey.tabId : reportedTabId - if (this.shouldSuppressClosedTabStatus(paneKey)) { + if (this.getAgentStatusDisposition(paneKey) !== 'accept') { return } const worktreeId = @@ -1836,17 +1845,21 @@ export class AgentHookServer { return } const tabId = paneKey !== physicalPaneKey ? parsedPaneKey.tabId : reportedTabId - if (this.shouldSuppressClosedTabStatus(paneKey)) { + const hookEventName = + typeof envelope.hookEventName === 'string' && envelope.hookEventName.trim().length > 0 + ? envelope.hookEventName.trim() + : undefined + const statusDisposition = this.getAgentStatusDisposition(paneKey, { + hookEventName, + isReplay: envelope.isReplay === true + }) + if (statusDisposition === 'suppress') { return } const worktreeId = envelope.worktreeId !== undefined && envelope.worktreeId.trim().length > 0 ? envelope.worktreeId.trim() : undefined - const hookEventName = - typeof envelope.hookEventName === 'string' && envelope.hookEventName.trim().length > 0 - ? envelope.hookEventName.trim() - : undefined const promptInteractionKey = typeof envelope.promptInteractionKey === 'string' && envelope.promptInteractionKey.trim().length > 0 @@ -1895,7 +1908,7 @@ export class AgentHookServer { }) const event: AgentHookEventPayload = { paneKey, - launchToken: envelope.launchToken, + launchToken: statusDisposition === 'restart' ? undefined : envelope.launchToken, tabId, worktreeId, connectionId: trimmedConnectionId, @@ -1997,9 +2010,19 @@ export class AgentHookServer { trackEmptyPaneKeyHook(body) const aliasedBody = this.normalizeHookBodyPaneKeyAlias(body) const normalized = this.normalizeLocalHookPayload(source, aliasedBody) - if (normalized.event && !this.shouldSuppressClosedTabStatus(normalized.event.paneKey)) { - this.recordCurrentAuthorityObservation(normalized.event) - const enriched = this.applyNormalizedStatus(normalized.event, normalized.onAccepted) + const statusDisposition = normalized.event + ? this.getAgentStatusDisposition(normalized.event.paneKey, { + hookEventName: normalized.event.hookEventName, + isReplay: normalized.event.isReplay + }) + : 'suppress' + if (normalized.event && statusDisposition !== 'suppress') { + const event = + statusDisposition === 'restart' + ? { ...normalized.event, launchToken: undefined } + : normalized.event + this.recordCurrentAuthorityObservation(event) + const enriched = this.applyNormalizedStatus(event, normalized.onAccepted) this.scheduleAssistantMessageRetry(source, aliasedBody, enriched) this.scheduleCodexSubagentPoll(source, aliasedBody, enriched) }