codeburn/src/parse-worker.ts
Uttam Deb 38f71ab35e
fix(codex): attribute native Codex tool events (#1767)
Codex writes native web_search_call, tool_search_call and image_generation_call items, and newer builds record a completed WebSearch item as type Extension with kind web.search. None of these counted as tool activity, so Codex sessions showed shell calls but no searches. They are now attributed as WebSearch, ToolSearch and ImageGeneration, with stable item identities deduplicated across status updates, mirrors, append checkpoints and fork replay, so a fork keeps its own new activity and never double counts the parent's.

Three maintainer commits sit on top. Codex builds of May 2026 write every search twice in a row, an item_completed WebSearch with its id and then a web_search_call with no id, so an id-less web_search_call directly after a WebSearch item is treated as that item. The depth-1 payload type scan now runs only for lines whose first payload type names a native tool or item_completed, which keeps the cold parse of compacted history from slowing by a fifth. The daily cache version is left alone: native tool items change which activity a turn lands in, never its calls, tokens or cost, and the Codex results file version already re-parses every session once; settled days pick up the new split at the next DAILY_CACHE_VERSION bump, which is reserved for the Node 22.15 floor.

Verified on a real session: usage, tokens and cost identical before and after, one WebSearch where there was none.

Authored by @uttamdeb.
2026-11-02 17:15:31 +01:00

50 lines
2.4 KiB
TypeScript

import { parentPort, workerData, type MessagePort } from 'worker_threads'
import { routeCodexParentReplay, type CodexParentReplayIndex } from './codex-fork-replay.js'
import { restorePricingState, type PricingSnapshot } from './models.js'
import type { ParentReplayReply, ParentReplayRequest, ParseJob } from './parse-workers.js'
import { parseClaudeFileFull } from './parser.js'
import { parseCodexFileFull } from './providers/codex.js'
const port = parentPort
if (!port) throw new Error('parse-worker must be started as a worker thread')
const { pricing, replayPort } = workerData as { pricing: PricingSnapshot; replayPort: MessagePort }
restorePricingState(pricing)
const replayReplies = new Map<number, { resolve: (index: CodexParentReplayIndex | null) => void; reject: (err: Error) => void }>()
let nextReplayRequest = 0
replayPort.on('message', (msg: ParentReplayReply) => {
const reply = replayReplies.get(msg.id)
replayReplies.delete(msg.id)
if (msg.error !== undefined) reply?.reject(new Error(msg.error))
else reply?.resolve(msg.index ?? null)
})
routeCodexParentReplay((...args) => new Promise((resolve, reject) => {
const id = nextReplayRequest++
replayReplies.set(id, { resolve, reject })
replayPort.postMessage({ id, args } satisfies ParentReplayRequest)
}))
// The parsed turns go back as a JSON string rather than as a live object graph:
// structured-cloning a whole corpus of turns costs more than the parallel parse
// saves, while a string is a single copy the parent re-parses at memcpy speed.
// `msgIds` / `keys` is every dedup key this file claimed; the parent uses it to
// prove no earlier file already owned one before installing the result. A Codex
// job also carries back the cache entry it would have written, because the cache
// module's per-directory state belongs to the parent, not to a thread.
port.on('message', (msg: ParseJob) => {
void (async () => {
try {
const seen = new Set<string>()
if (msg.kind === 'codex') {
const parsed = await parseCodexFileFull(msg.source, seen)
port.postMessage({ json: JSON.stringify({ ...parsed, keys: [...seen], path: msg.source.path }) })
return
}
const parsed = await parseClaudeFileFull(msg.filePath, seen)
port.postMessage({ json: parsed === null ? null : JSON.stringify({ ...parsed, msgIds: [...seen], path: msg.filePath }) })
} catch (err) {
port.postMessage({ error: err instanceof Error ? err.message : String(err) })
}
})()
})