Skip to content

The ultimate harness for coding agents and beyond. All your agents. All your machines. One command center. Start with code, then follow your curiosity and build across disciplines: CAD, circuits, robots, games and music.

Fix Codex transcript replay after rapid Linux rewrite (#1631)

0xken155f05cb29879b
diff --git a/cli/src/core/engines/liveWatcher.fs.spec.ts b/cli/src/core/engines/liveWatcher.fs.spec.ts
new file mode 100644
index 000000000..81d55c8c2
--- /dev/null
+++ b/cli/src/core/engines/liveWatcher.fs.spec.ts
@@ -0,0 +1,79 @@
+import { writeFileSync } from 'node:fs'
+import { appendFile, mkdtemp, rm, writeFile } from 'node:fs/promises'
+import { tmpdir } from 'node:os'
+import { join } from 'node:path'
+import { expect, it, vi } from 'vitest'
+import type { RegisteredSession } from '../../lib/registry.js'
+import { engineLiveRequests } from '../../engines/worker/liveRequests.js'
+import { LIVE_READ, type LivePage } from '../../engines/worker/liveProtocol.js'
+import { Watcher } from '../../watcher/watcher.js'
+import { createLiveTransport } from './liveTransport.js'
+import { createLiveWatcher } from './liveWatcher.js'
+
+const fileSignals = vi.hoisted(() => new Map<string, (path: string) => void>())
+vi.mock('chokidar', () => ({ default: { watch: () => {
+ const watcher = {
+ on(name: string, handler: (path: string) => void) { fileSignals.set(name, handler); return watcher },
+ add() {}, async unwatch() {}, async close() {},
+ }
+ return watcher
+} } }))
+
+const prompt = (text: string) => JSON.stringify({ type: 'event_msg', payload: { type: 'user_message', message: text } }) + '\n'
+const done = JSON.stringify({ type: 'event_msg', payload: { type: 'task_complete', last_agent_message: 'done' } }) + '\n'
+
+it('replays a Codex rewrite hidden by a rapid preceding file change', async () => {
+ const dir = await mkdtemp(join(tmpdir(), 'codex-rewrite-watch-'))
+ const file = join(dir, 'transcript.jsonl')
+ const retained = prompt('retained history') + done
+ await writeFile(file, prompt('an older turn that will be removed') + done + retained)
+ const session = { agentId: 'agent', sessionId: 'session', engine: 'codex', transcriptPath: file,
+ cwd: dir, codexHome: dir, model: null, cliVersion: null, boundAt: 1 } as RegisteredSession
+ const watched = { sessionId: session.sessionId, engine: session.engine, transcriptPath: file }
+ const worker = engineLiveRequests('codex', { fields: () => [] })
+ let rewrote = false
+ let connected = false
+ const transport = createLiveTransport({ call: async (service, method, payload) => {
+ if (!connected) { connected = true; transport.connected(service) }
+ const reply = await worker[method](payload, { local: true, owner: true }) as { answer?: LivePage }
+ // The real E2E rewrites as soon as it receives this turn's completion. Do it before the
+ // first change's 50 ms Chokidar throttle expires, after the worker has read the old file.
+ if (!rewrote && method === LIVE_READ && reply.answer?.frames.some(frame => frame.events.some(event => event.type === 'turn_ended'))) {
+ rewrote = true
+ writeFileSync(file, retained)
+ }
+ return reply
+ } })
+ const frames: LivePage['frames'][number][] = []
+ let liveWatcher: ReturnType<typeof createLiveWatcher> | undefined
+ try {
+ fileSignals.clear()
+ liveWatcher = createLiveWatcher(new Watcher(), {
+ handles: engine => engine === 'codex', transport, bySession: () => session,
+ frame: (_session, frame) => { frames.push(frame) },
+ log: () => {},
+ reattach: async () => {
+ const hold = await liveWatcher!.watcher.hold(session.sessionId, file)
+ try {
+ const prepared = await liveWatcher!.live.prepare(session, { live: false }, () => {})
+ expect(liveWatcher!.live.install(prepared)).toBe(true)
+ } finally { hold?.release() }
+ await liveWatcher!.watcher.addSession(watched)
+ },
+ })
+ liveWatcher.watcher.start()
+ const prepared = await liveWatcher.live.prepare(session, { live: false }, () => {})
+ expect(liveWatcher.live.install(prepared)).toBe(true)
+ await liveWatcher.watcher.addSession(watched)
+ await appendFile(file, prompt('new turn') + done)
+ // Chokidar emits this change, then suppresses the rewrite's event in its 50 ms window.
+ fileSignals.get('change')!(file)
+
+ await vi.waitFor(() => expect(rewrote).toBe(true), { timeout: 3_000 })
+ await vi.waitFor(() => expect(frames.some(frame => frame.replay && frame.events.some(event => event.type === 'turn_ended'))).toBe(true), { timeout: 4_000 })
+ expect(frames.filter(frame => !frame.replay && frame.events.some(event => event.type === 'turn_ended'))).toHaveLength(1)
+ } finally {
+ await liveWatcher?.watcher.stop()
+ await rm(dir, { recursive: true, force: true })
+ }
+})
diff --git a/cli/src/core/engines/liveWatcher.ts b/cli/src/core/engines/liveWatcher.ts
index 22e4083c6..d12b7f0dd 100644
--- a/cli/src/core/engines/liveWatcher.ts
+++ b/cli/src/core/engines/liveWatcher.ts
@@ -5,19 +5,41 @@ import { createLiveSessions, type LiveSessionDeps } from './liveSessions.js'
/** The core watches file names and asks workers to read. Only legacy engines use the local tailer. */
export function createLiveWatcher(local: Watcher, deps: Omit<LiveSessionDeps, 'watch' | 'unwatch'>) {
const paths = new Set<string>()
+ const followUps = new Map<string, ReturnType<typeof setTimeout>>()
let signals: FSWatcher | null = null
+ const change = (path: string): void => {
+ live.changed(path)
+ // Chokidar suppresses a second change to the same file within 50 ms. A Codex turn can finish
+ // and its transcript be shortened before that window closes, after the first read already ran.
+ // Read once more after the window so that rewrite cannot remain invisible until reconciliation.
+ clearTimeout(followUps.get(path))
+ followUps.set(path, setTimeout(() => {
+ followUps.delete(path)
+ live.changed(path)
+ }, 60))
+ }
const live = createLiveSessions({ ...deps,
watch(path) { paths.add(path); signals?.add(path) },
- unwatch(path) { paths.delete(path); void signals?.unwatch(path) },
+ unwatch(path) {
+ paths.delete(path)
+ clearTimeout(followUps.get(path))
+ followUps.delete(path)
+ void signals?.unwatch(path)
+ },
})
const watcher: Pick<Watcher, 'on' | 'start' | 'stop' | 'addSession' | 'removeSession' | 'hold' | 'tails' | 'setTail' | 'pollSession' | 'pollAll'> = {
on: local.on.bind(local),
start() {
local.start()
signals ??= chokidar.watch([...paths], { ignoreInitial: true })
- .on('add', path => live.changed(path)).on('change', path => live.changed(path)).on('unlink', path => live.changed(path))
+ .on('add', path => live.changed(path)).on('change', change).on('unlink', path => live.changed(path))
+ },
+ async stop() {
+ for (const timer of followUps.values()) clearTimeout(timer)
+ followUps.clear()
+ await Promise.all([local.stop(), live.stop(), signals?.close()])
+ signals = null
},
- async stop() { await Promise.all([local.stop(), live.stop(), signals?.close()]); signals = null },
async addSession(session, options) {
if (!deps.handles(session.engine)) return local.addSession(session, options)
await local.removeSession(session.sessionId)