Files
orca/src/shared/codex-subagent-transcript.test.ts
T
Brennan Benson 48e31b3fc0 fix(agent-status): show Codex v2 subagents (#11059)
* fix(agent-status): track Codex rollout subagents

* fix(agent-status): resolve cross-day Codex child rollouts and unblock CI gate

Codex files each rollout under its own local start date, so a session that
runs past midnight spawns children into a sibling day directory. Scanning
only the parent's directory left 13% of real subagent spawns (48/371 across
local rollouts) permanently unresolved, which pinned a phantom "working" row
and re-ran readdirSync every poll tick forever. Resolve the child's own day
directory from occurred_at_ms, and time-box a child whose rollout stays
unreadable so a deleted or never-written file can't leak a working row.

Also make the hook HTTP handler return void: the changed-code quality gate
keys findings by span overlap, so this PR's added line inside the pre-existing
async createServer callback resurfaced no-misused-promises as a new finding.

Tests cover cross-day resolution, grace-period retirement, and that the poll
re-arms across successive roster changes (the prior tests passed even when
the poll died after its first change).

* fix(agent-status): keep the Codex subagent poll alive across nested hooks

A nested non-codex CLI inherits its parent's ORCA_PANE_KEY, so its hook
POST reached scheduleCodexSubagentPoll and tore the timer down before the
source guard, silently ending polling while a rollout child was still live.
2026-07-27 23:50:25 -07:00

159 lines
5.8 KiB
TypeScript

import { afterEach, describe, expect, it, vi } from 'vitest'
import { mkdirSync, mkdtempSync, rmSync, writeFileSync } from 'node:fs'
import { tmpdir } from 'node:os'
import { join } from 'node:path'
import {
createCodexSubagentTranscriptState,
hasTrackedCodexTranscriptSubagents,
reconcileCodexSubagentTranscript
} from './codex-subagent-transcript'
import { codexRosterToSnapshots, type CodexSubagentRoster } from './codex-subagent-roster'
const CHILD_ID = '019fa65f-3144-7151-9c02-cff7a28f316f'
function jsonl(records: unknown[]): string {
return `${records.map((record) => JSON.stringify(record)).join('\n')}\n`
}
function activity(kind: string, occurredAtMs = 1234): unknown {
return {
type: 'event_msg',
payload: {
type: 'sub_agent_activity',
occurred_at_ms: occurredAtMs,
agent_thread_id: CHILD_ID,
agent_path: '/root/sidebar_repro',
kind
}
}
}
/** `<root>/YYYY/MM/DD` for a timestamp, matching how Codex buckets rollouts by local start date. */
function dayDirectory(root: string, atMs: number): string {
const at = new Date(atMs)
const pad = (value: number): string => String(value).padStart(2, '0')
return join(root, String(at.getFullYear()), pad(at.getMonth() + 1), pad(at.getDate()))
}
describe('Codex subagent transcript reconciliation', () => {
const dirs: string[] = []
afterEach(() => {
for (const dir of dirs) {
rmSync(dir, { recursive: true, force: true })
}
dirs.length = 0
})
it('adds a child from the parent rollout and removes it after task completion', () => {
const dir = mkdtempSync(join(tmpdir(), 'codex-subagent-transcript-'))
dirs.push(dir)
const parentPath = join(dir, 'rollout-parent.jsonl')
const childPath = join(dir, `rollout-child-${CHILD_ID}.jsonl`)
writeFileSync(parentPath, jsonl([activity('started')]))
writeFileSync(childPath, jsonl([{ type: 'event_msg', payload: { type: 'task_started' } }]))
const state = createCodexSubagentTranscriptState()
const roster: CodexSubagentRoster = new Map()
reconcileCodexSubagentTranscript(state, roster, parentPath)
expect(hasTrackedCodexTranscriptSubagents(state)).toBe(true)
expect(codexRosterToSnapshots(roster)).toEqual([
{
id: CHILD_ID,
description: '/root/sidebar_repro',
state: 'working',
startedAt: 1234,
agentType: undefined,
model: undefined
}
])
writeFileSync(
childPath,
jsonl([
{ type: 'event_msg', payload: { type: 'task_started' } },
{ type: 'event_msg', payload: { type: 'task_complete' } }
])
)
reconcileCodexSubagentTranscript(state, roster, parentPath)
expect(hasTrackedCodexTranscriptSubagents(state)).toBe(false)
expect(codexRosterToSnapshots(roster)).toBeUndefined()
})
it('resolves a child rollout filed under a later session day than the parent', () => {
const root = mkdtempSync(join(tmpdir(), 'codex-subagent-transcript-'))
dirs.push(root)
const childStartedAt = Date.now()
const parentDir = dayDirectory(root, childStartedAt - 24 * 60 * 60 * 1000)
const childDir = dayDirectory(root, childStartedAt)
mkdirSync(parentDir, { recursive: true })
mkdirSync(childDir, { recursive: true })
const parentPath = join(parentDir, 'rollout-parent.jsonl')
const childPath = join(childDir, `rollout-child-${CHILD_ID}.jsonl`)
writeFileSync(parentPath, jsonl([activity('started', childStartedAt)]))
writeFileSync(childPath, jsonl([{ type: 'event_msg', payload: { type: 'task_started' } }]))
const state = createCodexSubagentTranscriptState()
const roster: CodexSubagentRoster = new Map()
reconcileCodexSubagentTranscript(state, roster, parentPath)
writeFileSync(
childPath,
jsonl([
{ type: 'event_msg', payload: { type: 'task_started' } },
{ type: 'event_msg', payload: { type: 'task_complete' } }
])
)
reconcileCodexSubagentTranscript(state, roster, parentPath)
// Why: only a cross-day lookup can observe the completion; the parent-directory scan never finds this file.
expect(roster.size).toBe(0)
expect(hasTrackedCodexTranscriptSubagents(state)).toBe(false)
})
it('retires a child whose rollout never becomes readable', () => {
vi.useFakeTimers()
try {
const dir = mkdtempSync(join(tmpdir(), 'codex-subagent-transcript-'))
dirs.push(dir)
const parentPath = join(dir, 'rollout-parent.jsonl')
writeFileSync(parentPath, jsonl([activity('started')]))
const state = createCodexSubagentTranscriptState()
const roster: CodexSubagentRoster = new Map()
reconcileCodexSubagentTranscript(state, roster, parentPath)
expect(roster.size).toBe(1)
// Why: within the grace window a slow-to-appear rollout must not drop a live child.
vi.advanceTimersByTime(30_000)
reconcileCodexSubagentTranscript(state, roster, parentPath)
expect(roster.size).toBe(1)
vi.advanceTimersByTime(31_000)
reconcileCodexSubagentTranscript(state, roster, parentPath)
expect(roster.size).toBe(0)
expect(hasTrackedCodexTranscriptSubagents(state)).toBe(false)
} finally {
vi.useRealTimers()
}
})
it('removes a child when Codex reports it interrupted', () => {
const dir = mkdtempSync(join(tmpdir(), 'codex-subagent-transcript-'))
dirs.push(dir)
const parentPath = join(dir, 'rollout-parent.jsonl')
writeFileSync(parentPath, jsonl([activity('started')]))
const state = createCodexSubagentTranscriptState()
const roster: CodexSubagentRoster = new Map()
reconcileCodexSubagentTranscript(state, roster, parentPath)
writeFileSync(parentPath, jsonl([activity('started'), activity('interrupted')]))
reconcileCodexSubagentTranscript(state, roster, parentPath)
expect(hasTrackedCodexTranscriptSubagents(state)).toBe(false)
expect(roster.size).toBe(0)
})
})