refactor: add terminal status hook ingest path

This commit is contained in:
Neil
2026-06-07 01:44:53 -07:00
committed by GitHub
parent aaf51dad45
commit 4116cb900d
2 changed files with 175 additions and 0 deletions
+113
View File
@@ -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([])
})
})
+62
View File
@@ -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