From 4116cb900d1ebda8ca813b95870ef7a44df7b357 Mon Sep 17 00:00:00 2001 From: Neil <4138956+nwparker@users.noreply.github.com> Date: Sun, 7 Jun 2026 01:44:53 -0700 Subject: [PATCH] refactor: add terminal status hook ingest path --- src/main/agent-hooks/server.test.ts | 113 ++++++++++++++++++++++++++++ src/main/agent-hooks/server.ts | 62 +++++++++++++++ 2 files changed, 175 insertions(+) diff --git a/src/main/agent-hooks/server.test.ts b/src/main/agent-hooks/server.test.ts index 77463e159..4ae9a06a0 100644 --- a/src/main/agent-hooks/server.test.ts +++ b/src/main/agent-hooks/server.test.ts @@ -6176,3 +6176,116 @@ describe('AgentHookServer ingestRemote', () => { expect(event.payload.prompt.length).toBe(200) }) }) + +describe('AgentHookServer ingestTerminalStatus', () => { + it('forwards runtime terminal status through the normal listener and snapshot path', () => { + vi.useFakeTimers() + vi.setSystemTime(1_000) + try { + const server = new AgentHookServer() + const listener = vi.fn() + server.setListener(listener) + + server.ingestTerminalStatus({ + paneKey: PANE, + tabId: 'tab-1', + worktreeId: 'wt-1', + payload: { + state: 'working', + prompt: 'ship it', + agentType: 'codex' + } + }) + + expect(listener).toHaveBeenCalledTimes(1) + expect(listener).toHaveBeenCalledWith( + expect.objectContaining({ + paneKey: PANE, + tabId: 'tab-1', + worktreeId: 'wt-1', + connectionId: null, + receivedAt: 1_000, + stateStartedAt: 1_000, + payload: { + state: 'working', + prompt: 'ship it', + agentType: 'codex' + } + }) + ) + expect(server.getStatusSnapshot()).toEqual([ + expect.objectContaining({ + paneKey: PANE, + tabId: 'tab-1', + worktreeId: 'wt-1', + connectionId: null, + receivedAt: 1_000, + stateStartedAt: 1_000, + state: 'working', + prompt: 'ship it', + agentType: 'codex' + }) + ]) + expect(trackMock).not.toHaveBeenCalledWith('agent_prompt_sent', expect.anything()) + } finally { + vi.useRealTimers() + } + }) + + it('suppresses exact duplicate runtime terminal status observations', () => { + vi.useFakeTimers() + vi.setSystemTime(1_000) + try { + const server = new AgentHookServer() + const listener = vi.fn() + server.setListener(listener) + const event = { + paneKey: PANE, + tabId: 'tab-1', + worktreeId: 'wt-1', + payload: { + state: 'working' as const, + prompt: 'same turn', + agentType: 'codex' as const + } + } + + server.ingestTerminalStatus(event) + vi.setSystemTime(1_250) + server.ingestTerminalStatus(event) + + expect(listener).toHaveBeenCalledTimes(1) + expect(server.getStatusSnapshot()).toEqual([ + expect.objectContaining({ + paneKey: PANE, + receivedAt: 1_000, + stateStartedAt: 1_000, + state: 'working', + prompt: 'same turn' + }) + ]) + } finally { + vi.useRealTimers() + } + }) + + it('rejects runtime terminal status with mismatched tab identity', () => { + const server = new AgentHookServer() + const listener = vi.fn() + server.setListener(listener) + + server.ingestTerminalStatus({ + paneKey: PANE, + tabId: 'other-tab', + worktreeId: 'wt-1', + payload: { + state: 'working', + prompt: 'bad tab', + agentType: 'codex' + } + }) + + expect(listener).not.toHaveBeenCalled() + expect(server.getStatusSnapshot()).toEqual([]) + }) +}) diff --git a/src/main/agent-hooks/server.ts b/src/main/agent-hooks/server.ts index c36ce287e..fabf4b17e 100644 --- a/src/main/agent-hooks/server.ts +++ b/src/main/agent-hooks/server.ts @@ -42,6 +42,7 @@ import { type AgentStatusIpcPayload, type AgentType, type AgentStatusState, + type ParsedAgentStatusPayload, normalizeAgentStatusPayload } from '../../shared/agent-status-types' import { @@ -239,6 +240,21 @@ function toAgentStatusIpcPayload(entry: EnrichedAgentHookEventPayload): AgentSta } } +function equivalentParsedAgentStatusPayload( + a: ParsedAgentStatusPayload, + b: ParsedAgentStatusPayload +): boolean { + return ( + a.state === b.state && + a.prompt === b.prompt && + a.agentType === b.agentType && + a.toolName === b.toolName && + a.toolInput === b.toolInput && + a.lastAssistantMessage === b.lastAssistantMessage && + a.interrupted === b.interrupted + ) +} + function trackEmptyPaneKeyHook(body: unknown): void { if (typeof body !== 'object' || body === null) { return @@ -893,6 +909,52 @@ export class AgentHookServer { return { ...record, paneKey: stablePaneKey } } + ingestTerminalStatus(event: { + paneKey: string + tabId?: string + worktreeId?: string + payload: ParsedAgentStatusPayload + }): void { + const paneKey = this.resolvePaneKeyAlias(event.paneKey.trim()) + const parsedPaneKey = parsePaneKey(paneKey) + if (paneKey.length === 0) { + track('agent_hook_unattributed', { reason: 'empty_pane_key' }) + return + } + if (paneKey.length > MAX_PANE_KEY_LEN || !parsedPaneKey) { + return + } + const tabId = + event.tabId !== undefined && event.tabId.trim().length > 0 ? event.tabId.trim() : undefined + if (tabId !== undefined && tabId !== parsedPaneKey.tabId) { + return + } + const worktreeId = + event.worktreeId !== undefined && event.worktreeId.trim().length > 0 + ? event.worktreeId.trim() + : undefined + const previous = this.state.lastStatusByPaneKey.get(paneKey) as + | EnrichedAgentHookEventPayload + | undefined + if ( + previous?.connectionId === null && + previous.tabId === tabId && + previous.worktreeId === worktreeId && + equivalentParsedAgentStatusPayload(previous.payload, event.payload) + ) { + return + } + // Why: OSC terminal status is a runtime/model observation, not a hook + // prompt boundary. Keep prompt-sent telemetry tied to native hooks. + this.applyNormalizedStatus({ + paneKey, + tabId, + worktreeId, + connectionId: null, + payload: event.payload + }) + } + /** Ingest a payload that arrived over the relay JSON-RPC channel rather * than the local HTTP server. `connectionId` is the SshChannelMultiplexer * identity Orca holds (the wire envelope carries connectionId: null and