import { createHash } from "node:crypto"; import * as fs from "node:fs"; import * as path from "node:path"; type JsonRecord = Record; const CONTEXT_KEYS = ["instructions", "messages", "input", "tools", "tool_choice"] as const; const REQUIRED_IDENTITY_KEYS = [ "providerExecutable", "providerConfig", "modelFixture", "workerScript", "userPrompt", "proofDriver", ] as const; const ALLOWED_PROVIDER_INPUTS = new Set([...CONTEXT_KEYS, "conversationState"]); const WORKER_REVIEW_MS = 300_000; export interface UsageTotals { input: number; output: number; cacheRead: number; cacheWrite: number; reasoning: number; totalTokens: number; componentConsistencyTotal: number; } export interface ContextBytes { count: number; total: number; min: number; max: number; perRequest: Array<{ responseId: string; bytes: number }>; } export interface SequenceManifest { firedWakeToken: string; wakeMessageId: string; assistantMessageIds: string[]; settledAssistantMessageId: string; rearmedWakeToken: string; } export interface VariantManifest { sessions: { parent: string; workers: string[] }; providerRequests: string; providerContract: string; observerEvents: string; durableWakes: { fired: string; rearmed: string }; sequenceManifest: string; identityFiles: Record; runtimeArtifacts: { status: string[]; events: string[]; stdout: string[]; stderr: string[]; other: string[] }; artifacts: Array<{ category: string; file: string }>; proofDirectory: string; } export interface MetricsManifest { before: VariantManifest; after: VariantManifest; } export interface VariantMetrics { usage: { parent: UsageTotals; workers: UsageTotals; combined: UsageTotals }; providerTurns: { assistantMessages: number; requests: number }; contextBytes: ContextBytes; reviewSequence: { wakeMessageId: string; assistantMessageIds: string[]; elapsedToWakeMs: number; providerRequests: number; usage: UsageTotals; contextBytes: ContextBytes; }; counts: { agiSleepRequested: Record; agiSleepResult: Record; wakes: Record; agiWorkers: number; agiWorkerViews: Record; shellSleep: number; }; traceBytes: { categories: Record; artifactTotal: number; proofDirectory: number }; } export interface Issue12Metrics { historicalBaseline: { assistantMessages: 74; input: 415181; output: 44854; cacheRead: 2999424; reasoning: 12272; totalTokens: 3459459; }; identityHashes: Record; before: VariantMetrics; after: VariantMetrics; } function record(value: unknown, label: string): JsonRecord { if (value === null || typeof value !== "object" || Array.isArray(value)) throw new Error(`${label} must be a JSON object`); return value as JsonRecord; } function stringField(value: unknown, label: string): string { if (typeof value !== "string" || value.length === 0) throw new Error(`${label} must be a non-empty string`); return value; } function timestamp(value: unknown, label: string): number { const raw = stringField(value, label); const parsed = Date.parse(raw); if (!Number.isFinite(parsed)) throw new Error(`${label} must be an ISO timestamp`); return parsed; } export function readJsonl(file: string): JsonRecord[] { const raw = fs.readFileSync(file, "utf8"); const rows: JsonRecord[] = []; for (const [index, line] of raw.split("\n").entries()) { if (line.trim().length === 0) continue; let parsed: unknown; try { parsed = JSON.parse(line); } catch (error) { throw new Error(`${file}:${index + 1}: malformed JSONL (${(error as Error).message})`); } rows.push(record(parsed, `${file}:${index + 1}`)); } return rows; } function zeroUsage(): UsageTotals { return { input: 0, output: 0, cacheRead: 0, cacheWrite: 0, reasoning: 0, totalTokens: 0, componentConsistencyTotal: 0 }; } function numeric(value: unknown): number { return typeof value === "number" && Number.isFinite(value) ? value : 0; } function addUsage(target: UsageTotals, source: Partial): void { target.input += source.input ?? 0; target.output += source.output ?? 0; target.cacheRead += source.cacheRead ?? 0; target.cacheWrite += source.cacheWrite ?? 0; target.reasoning += source.reasoning ?? 0; target.totalTokens += source.totalTokens ?? 0; target.componentConsistencyTotal += source.componentConsistencyTotal ?? 0; } function messageOf(row: JsonRecord): JsonRecord | undefined { try { return record(row.message, "message"); } catch { return undefined; } } function assistantRows(rows: JsonRecord[]): JsonRecord[] { return rows.filter((row) => messageOf(row)?.role === "assistant"); } function usageOfRows(rows: JsonRecord[]): UsageTotals { const totals = zeroUsage(); for (const row of rows) { const message = messageOf(row); if (message?.role !== "assistant") continue; const usage = record(message.usage ?? {}, "assistant usage"); const input = numeric(usage.input); const output = numeric(usage.output); const cacheRead = numeric(usage.cacheRead); const cacheWrite = numeric(usage.cacheWrite); addUsage(totals, { input, output, cacheRead, cacheWrite, reasoning: numeric(usage.reasoning), totalTokens: numeric(usage.totalTokens), componentConsistencyTotal: input + output + cacheRead + cacheWrite, }); } return totals; } function providerAssistant(row: JsonRecord): boolean { const message = messageOf(row); return message?.role === "assistant" && (typeof message.api === "string" || typeof message.provider === "string" || typeof message.model === "string"); } interface ProviderRequest { responseId: string; body: JsonRecord; inspectedInputs: string[]; } function inspectedInputs(value: unknown, label: string): string[] { if (!Array.isArray(value) || value.length === 0) throw new Error(`${label} must be a non-empty array`); const inputs = value.map((input, index) => stringField(input, `${label}[${index}]`)); if (new Set(inputs).size !== inputs.length) throw new Error(`${label} must not contain duplicates`); for (const input of inputs) { if (!ALLOWED_PROVIDER_INPUTS.has(input)) throw new Error(`${label}: branch-aware or unsupported provider input '${input}'`); } return inputs; } function providerRequests(file: string): ProviderRequest[] { return readJsonl(file).map((row, index) => { const responseId = stringField(row.responseId, `${file}:${index + 1}.responseId`); let bodyValue = row.body; if (typeof bodyValue === "string") { try { bodyValue = JSON.parse(bodyValue) as unknown; } catch (error) { throw new Error(`${file}:${index + 1}.body is malformed JSON (${(error as Error).message})`); } } return { responseId, body: record(bodyValue, `${file}:${index + 1}.body`), inspectedInputs: inspectedInputs(row.inspectedInputs, `${file}:${index + 1}.inspectedInputs`), }; }); } function contextBytes(requests: ProviderRequest[]): ContextBytes { const perRequest = requests.map((request) => { const projection: JsonRecord = {}; for (const key of CONTEXT_KEYS) if (key in request.body) projection[key] = request.body[key]; return { responseId: request.responseId, bytes: Buffer.byteLength(JSON.stringify(projection), "utf8") }; }); const values = perRequest.map((item) => item.bytes); return { count: perRequest.length, total: values.reduce((sum, value) => sum + value, 0), min: values.length === 0 ? 0 : Math.min(...values), max: values.length === 0 ? 0 : Math.max(...values), perRequest, }; } function increment(map: Record, key: string): void { map[key] = (map[key] ?? 0) + 1; } function toolCalls(rows: JsonRecord[]): Array<{ row: JsonRecord; name: string; args: JsonRecord }> { const calls: Array<{ row: JsonRecord; name: string; args: JsonRecord }> = []; for (const row of rows) { const message = messageOf(row); if (message?.role !== "assistant" || !Array.isArray(message.content)) continue; for (const item of message.content) { if (item === null || typeof item !== "object" || Array.isArray(item)) continue; const part = item as JsonRecord; if (part.type !== "toolCall" || typeof part.name !== "string") continue; calls.push({ row, name: part.name, args: record(part.arguments ?? {}, "tool arguments") }); } } return calls; } function shellSegments(command: string): string[] { const segments: string[] = []; let current = ""; let quote: "'" | '"' | undefined; let escaped = false; for (const ch of command) { if (escaped) { current += ch; escaped = false; continue; } if (ch === "\\" && quote !== "'") { current += ch; escaped = true; continue; } if (quote !== undefined) { current += ch; if (ch === quote) quote = undefined; continue; } if (ch === "'" || ch === '"') { quote = ch; current += ch; continue; } if (ch === ";" || ch === "&" || ch === "|" || ch === "\n") { if (current.trim().length > 0) segments.push(current.trim()); current = ""; continue; } current += ch; } if (current.trim().length > 0) segments.push(current.trim()); return segments; } function shellWords(segment: string): string[] { const words: string[] = []; let current = ""; let quote: "'" | '"' | undefined; let escaped = false; const push = (): void => { const cleaned = current.replace(/^[({]+/, "").replace(/[)}]+$/, ""); if (cleaned.length > 0) words.push(cleaned); current = ""; }; for (const ch of segment) { if (escaped) { current += ch; escaped = false; continue; } if (ch === "\\" && quote !== "'") { escaped = true; continue; } if (quote !== undefined) { if (ch === quote) quote = undefined; else current += ch; continue; } if (ch === "'" || ch === '"') { quote = ch; continue; } if (/\s/.test(ch)) { push(); continue; } current += ch; } push(); return words; } function commandBase(word: string): string { return word.split("/").pop() ?? word; } /** Conservative proof-only detector for shell polling, including common wrappers. */ export function shellContainsSleep(command: string, depth = 0): boolean { if (depth > 4) return true; for (const segment of shellSegments(command)) { const words = shellWords(segment); let index = 0; while (index < words.length) { const word = words[index] as string; if (/^[A-Za-z_][A-Za-z0-9_]*=/.test(word)) { index += 1; continue; } const base = commandBase(word); if (["command", "env", "setsid", "stdbuf", "time", "ionice", "nohup", "sudo"].includes(base)) { index += 1; while (index < words.length && (words[index] as string).startsWith("-")) index += 1; continue; } if (base === "timeout" || base === "nice") { index += 1; while (index < words.length && ((words[index] as string).startsWith("-") || /^\d+(?:\.\d+)?[smhd]?$/.test(words[index] as string))) index += 1; continue; } if (base === "sleep") return true; if (["bash", "sh", "zsh", "dash", "ksh"].includes(base)) { for (let arg = index + 1; arg < words.length - 1; arg += 1) { const flag = words[arg] as string; if (/^-[A-Za-z]*c[A-Za-z]*$/.test(flag) || flag === "--command") { if (shellContainsSleep(words[arg + 1] as string, depth + 1)) return true; } } } break; } } return false; } function counts(rows: JsonRecord[]): VariantMetrics["counts"] { const result: VariantMetrics["counts"] = { agiSleepRequested: {}, agiSleepResult: {}, wakes: {}, agiWorkers: 0, agiWorkerViews: {}, shellSleep: 0, }; for (const call of toolCalls(rows)) { if (call.name === "agi_sleep") increment(result.agiSleepRequested, typeof call.args.until === "string" ? call.args.until : "default"); if (call.name === "agi_workers") result.agiWorkers += 1; if (call.name === "agi_worker") increment(result.agiWorkerViews, typeof call.args.view === "string" ? call.args.view : "status"); if (call.name === "bash") { const command = typeof call.args.command === "string" ? call.args.command : typeof call.args.cmd === "string" ? call.args.cmd : ""; if (shellContainsSleep(command)) result.shellSleep += 1; } } for (const row of rows) { if (row.type === "custom_message" && row.customType === "agi-wake") { const details = record(row.details ?? {}, "wake details"); increment(result.wakes, typeof details.reason === "string" ? details.reason : "unknown"); } const message = messageOf(row); if (message?.role !== "toolResult" || message.toolName !== "agi_sleep") continue; const details = record(message.details ?? {}, "sleep result details"); const sleep = record(details.agiSleep ?? {}, "agiSleep details"); increment(result.agiSleepResult, typeof sleep.until === "string" ? sleep.until : "unknown"); } return result; } function sequence(file: string): SequenceManifest { const raw = record(JSON.parse(fs.readFileSync(file, "utf8")) as unknown, file); const assistantMessageIds = Array.isArray(raw.assistantMessageIds) ? raw.assistantMessageIds.map((value, index) => stringField(value, `${file}.assistantMessageIds[${index}]`)) : []; if (assistantMessageIds.length === 0 || new Set(assistantMessageIds).size !== assistantMessageIds.length) { throw new Error(`${file}: assistantMessageIds must be non-empty and unique`); } return { firedWakeToken: stringField(raw.firedWakeToken, `${file}.firedWakeToken`), wakeMessageId: stringField(raw.wakeMessageId, `${file}.wakeMessageId`), assistantMessageIds, settledAssistantMessageId: stringField(raw.settledAssistantMessageId, `${file}.settledAssistantMessageId`), rearmedWakeToken: stringField(raw.rearmedWakeToken, `${file}.rearmedWakeToken`), }; } function uniqueRawMessageIds(rows: JsonRecord[], ids: Set): void { for (const row of rows) { const id = stringField(row.id, "raw session row id"); if (ids.has(id)) throw new Error(`duplicate raw message ID '${id}'`); ids.add(id); } } interface ArchivedWorkerWake { token: string; sessionId: string; armedAt: number; firesAt: number; } function archivedWorkerWake(file: string, expectedToken: string): ArchivedWorkerWake { const wake = record(JSON.parse(fs.readFileSync(file, "utf8")) as unknown, file); if (wake.schemaVersion !== 1 || wake.kind !== "worker_check") throw new Error(`${file}: archived wake must be a schema-1 worker_check`); const token = stringField(wake.token, `${file}.token`); if (token !== expectedToken) throw new Error(`${file}: archived wake token does not match sequence manifest`); const sessionId = stringField(wake.sessionId, `${file}.sessionId`); const armedAt = timestamp(wake.armedAt, `${file}.armedAt`); const firesAt = timestamp(wake.firesAt, `${file}.firesAt`); if (firesAt - armedAt !== WORKER_REVIEW_MS) throw new Error(`${file}: worker_check does not use the fixed five-minute interval`); return { token, sessionId, armedAt, firesAt }; } function exactEventIndex(rows: JsonRecord[], kind: string, field: "token" | "messageId", value: string): number { const matches: number[] = []; for (const [index, row] of rows.entries()) { if (row.kind === kind && row[field] === value) matches.push(index); } if (matches.length !== 1) throw new Error(`observer event ${kind} ${field}=${value} is missing or ambiguous`); return matches[0] as number; } function validateObserverTimes(observerEvents: JsonRecord[]): void { let previous = Number.NEGATIVE_INFINITY; for (const [index, event] of observerEvents.entries()) { const observedAt = timestamp(event.observedAt, `observer event ${index + 1}.observedAt`); if (observedAt < previous) throw new Error("observer event timestamps are out of order"); previous = observedAt; } } function validateSequence( rows: JsonRecord[], observerEvents: JsonRecord[], value: SequenceManifest, firedWake: ArchivedWorkerWake, rearmedWake: ArchivedWorkerWake, ): { rows: JsonRecord[]; elapsedToWakeMs: number } { if (value.firedWakeToken === value.rearmedWakeToken) throw new Error("review sequence reused its fired wake token"); if (value.assistantMessageIds.at(-1) !== value.settledAssistantMessageId) throw new Error("settled assistant must close the ordered review assistant list"); if (firedWake.sessionId !== rearmedWake.sessionId) throw new Error("fired and rearmed worker_check records belong to different sessions"); validateObserverTimes(observerEvents); const byId = new Map(rows.flatMap((row) => typeof row.id === "string" ? [[row.id, row] as const] : [])); const parentSessions = rows.filter((row) => row.type === "session"); if (parentSessions.length !== 1) throw new Error("raw parent session must contain exactly one session record"); const parentSessionId = stringField(parentSessions[0]?.id, "raw parent session id"); if (parentSessionId !== firedWake.sessionId) throw new Error("archived worker_check sessionId does not match the raw parent session"); const wake = byId.get(value.wakeMessageId); if (wake === undefined || wake.type !== "custom_message" || wake.customType !== "agi-wake") throw new Error("worker-review wake message ID is missing or ambiguous"); const wakeDetails = record(wake.details ?? {}, "review wake details"); if (wakeDetails.reason !== "worker_check") throw new Error("sequence wake is not worker_check"); const wakeAt = timestamp(wake.timestamp, "review wake timestamp"); if (wakeAt < firedWake.firesAt) throw new Error("worker-review wake predates the archived five-minute fire time"); const selected = value.assistantMessageIds.map((id) => { const row = byId.get(id); if (row === undefined || !providerAssistant(row)) throw new Error(`review assistant ${id} is missing or is not a provider turn`); let cursor: JsonRecord | undefined = row; const seen = new Set(); let descends = false; while (cursor !== undefined && typeof cursor.parentId === "string" && !seen.has(cursor.parentId)) { if (cursor.parentId === value.wakeMessageId) { descends = true; break; } seen.add(cursor.parentId); cursor = byId.get(cursor.parentId); } if (!descends) throw new Error(`review assistant ${id} does not descend from wake ${value.wakeMessageId}`); return row; }); const indices = [ exactEventIndex(observerEvents, "worker_check_fired", "token", value.firedWakeToken), exactEventIndex(observerEvents, "wake_sent", "messageId", value.wakeMessageId), ...value.assistantMessageIds.map((messageId) => exactEventIndex(observerEvents, "assistant_provider", "messageId", messageId)), exactEventIndex(observerEvents, "agent_settled", "messageId", value.settledAssistantMessageId), exactEventIndex(observerEvents, "worker_check_rearmed", "token", value.rearmedWakeToken), ]; if (indices.some((index, position) => position > 0 && index <= (indices[position - 1] as number))) { throw new Error("review sequence event IDs/tokens are missing, ambiguous, or out of order in observer JSONL"); } const observedAt = indices.map((index) => timestamp(observerEvents[index]?.observedAt, `observer event ${index + 1}.observedAt`)); if ((observedAt[0] as number) < firedWake.firesAt) throw new Error("worker_check_fired observation predates the archived fire time"); if ((observedAt[1] as number) < wakeAt) throw new Error("wake_sent observation predates the raw parent wake"); const settled = selected.at(-1); if (settled === undefined) throw new Error("review sequence has no settling assistant"); const settledAt = timestamp(settled.timestamp, "settled assistant timestamp"); const settlementObservedAt = observedAt.at(-2) as number; const rearmObservedAt = observedAt.at(-1) as number; if (settlementObservedAt < settledAt) throw new Error("agent_settled observation predates the settling assistant"); if (rearmedWake.armedAt < settlementObservedAt) throw new Error("rearmed worker_check predates observed agent settlement"); if (rearmObservedAt < rearmedWake.armedAt) throw new Error("worker_check_rearmed observation predates the archived rearm"); return { rows: selected, elapsedToWakeMs: wakeAt - firedWake.armedAt }; } function directoryBytes(root: string): number { let total = 0; for (const entry of fs.readdirSync(root, { withFileTypes: true })) { const target = path.join(root, entry.name); if (entry.isDirectory()) total += directoryBytes(target); else if (entry.isFile()) total += fs.statSync(target).size; } return total; } function packagedFiles(root: string): string[] { const files: string[] = []; for (const entry of fs.readdirSync(root, { withFileTypes: true })) { const target = path.join(root, entry.name); if (entry.isDirectory()) files.push(...packagedFiles(target)); else if (entry.isFile()) files.push(path.resolve(target)); else throw new Error(`proof directory contains unsupported non-regular entry '${target}'`); } return files.sort(); } interface ArtifactAudit { seenPaths: Set; seenFiles: Set; seenProofRoots: Set; } function requiredArtifactFiles(manifest: VariantManifest): string[] { for (const [category, files] of Object.entries(manifest.runtimeArtifacts)) { if (!Array.isArray(files) || files.length === 0) throw new Error(`runtime artifact category '${category}' must be non-empty`); } return [ manifest.sessions.parent, ...manifest.sessions.workers, manifest.providerRequests, manifest.providerContract, manifest.observerEvents, manifest.durableWakes.fired, manifest.durableWakes.rearmed, manifest.sequenceManifest, ...Object.values(manifest.identityFiles), ...manifest.runtimeArtifacts.status, ...manifest.runtimeArtifacts.events, ...manifest.runtimeArtifacts.stdout, ...manifest.runtimeArtifacts.stderr, ...manifest.runtimeArtifacts.other, ]; } function traceBytes(manifest: VariantManifest, audit: ArtifactAudit): VariantMetrics["traceBytes"] { const categories: Record = {}; let artifactTotal = 0; const proofRoot = fs.realpathSync(manifest.proofDirectory); if (audit.seenProofRoots.has(proofRoot)) throw new Error(`proof directory '${manifest.proofDirectory}' is reused across variants`); audit.seenProofRoots.add(proofRoot); const listed = new Map(); for (const artifact of manifest.artifacts) { stringField(artifact.category, "proof artifact category"); const absolute = path.resolve(artifact.file); if (audit.seenPaths.has(absolute)) throw new Error(`duplicate proof artifact path '${artifact.file}'`); audit.seenPaths.add(absolute); listed.set(absolute, (listed.get(absolute) ?? 0) + 1); const real = fs.realpathSync(absolute); if (real !== proofRoot && !real.startsWith(`${proofRoot}${path.sep}`)) throw new Error(`${artifact.file} is outside the proof directory`); const stat = fs.statSync(real); if (!stat.isFile()) throw new Error(`${artifact.file} is not a proof artifact file`); const identity = `${stat.dev}:${stat.ino}`; if (audit.seenFiles.has(identity)) throw new Error(`overlapping proof artifact '${artifact.file}' resolves to an already counted file`); audit.seenFiles.add(identity); const size = stat.size; categories[artifact.category] = (categories[artifact.category] ?? 0) + size; artifactTotal += size; } const required = requiredArtifactFiles(manifest).map((file) => path.resolve(file)); if (new Set(required).size !== required.length) throw new Error("a manifest file is referenced more than once across proof artifact roles"); for (const file of required) { if (listed.get(file) !== 1) throw new Error(`required proof artifact '${file}' must appear exactly once in artifacts`); } if (listed.size !== required.length) throw new Error("artifacts contains an unclassified file; declare it in runtimeArtifacts.other"); const packaged = packagedFiles(proofRoot); const classified = [...listed.keys()].sort(); if (JSON.stringify(packaged) !== JSON.stringify(classified)) { throw new Error("proof directory contains an unclassified or missing physical file"); } return { categories, artifactTotal, proofDirectory: directoryBytes(manifest.proofDirectory) }; } function providerContract(file: string): string[] { const contract = record(JSON.parse(fs.readFileSync(file, "utf8")) as unknown, file); if (contract.stateMachine !== "content-only") throw new Error(`${file}: provider state machine must be content-only`); return inspectedInputs(contract.inspectedInputs, `${file}.inspectedInputs`); } function validateProviderAudit(requests: ProviderRequest[], contractInputs: string[]): void { const expected = [...contractInputs].sort(); for (const request of requests) { if (JSON.stringify([...request.inspectedInputs].sort()) !== JSON.stringify(expected)) { throw new Error(`provider request ${request.responseId} inspected-input audit does not match the provider contract`); } } } function validateResponseBijection(rows: JsonRecord[], requests: ProviderRequest[], globalResponseIds: Set): void { const assistants = rows.filter(providerAssistant); const assistantIds = assistants.map((row) => stringField(messageOf(row)?.responseId, "assistant provider responseId")); const requestIds = requests.map((request) => request.responseId); if (new Set(assistantIds).size !== assistantIds.length) throw new Error("duplicate assistant provider responseId"); if (new Set(requestIds).size !== requestIds.length) throw new Error("duplicate captured provider responseId"); const sortedAssistants = [...assistantIds].sort(); const sortedRequests = [...requestIds].sort(); if (JSON.stringify(sortedAssistants) !== JSON.stringify(sortedRequests)) { throw new Error("provider request response IDs do not form a bijection with assistant provider response IDs"); } for (const responseId of requestIds) { if (globalResponseIds.has(responseId)) throw new Error(`duplicate provider responseId '${responseId}' across paired variants`); globalResponseIds.add(responseId); } } function fileHash(file: string): string { return createHash("sha256").update(fs.readFileSync(file)).digest("hex"); } function identityHashes(before: VariantManifest, after: VariantManifest): Record { const result: Record = {}; for (const key of REQUIRED_IDENTITY_KEYS) { const beforeFile = before.identityFiles[key]; const afterFile = after.identityFiles[key]; if (beforeFile === undefined || afterFile === undefined) throw new Error(`identity file '${key}' is required for both variants`); const beforeHash = fileHash(beforeFile); const afterHash = fileHash(afterFile); if (beforeHash !== afterHash) throw new Error(`identity hash mismatch for '${key}'`); result[key] = beforeHash; } providerContract(before.providerContract); providerContract(after.providerContract); if (fileHash(before.providerContract) !== fileHash(after.providerContract)) throw new Error("provider contract hash mismatch"); return result; } function variantMetrics( manifest: VariantManifest, audit: ArtifactAudit, globalRawIds: Set, globalResponseIds: Set, ): VariantMetrics { const parentRows = readJsonl(manifest.sessions.parent); const workerRows = manifest.sessions.workers.flatMap(readJsonl); const allRows = [...parentRows, ...workerRows]; uniqueRawMessageIds(allRows, globalRawIds); const requests = providerRequests(manifest.providerRequests); validateProviderAudit(requests, providerContract(manifest.providerContract)); validateResponseBijection(allRows, requests, globalResponseIds); const assistantTurns = allRows.filter(providerAssistant).length; if (assistantTurns !== requests.length) throw new Error(`provider request count ${requests.length} does not match assistant provider turns ${assistantTurns}`); const seq = sequence(manifest.sequenceManifest); const firedWake = archivedWorkerWake(manifest.durableWakes.fired, seq.firedWakeToken); const rearmedWake = archivedWorkerWake(manifest.durableWakes.rearmed, seq.rearmedWakeToken); const validatedSequence = validateSequence(parentRows, readJsonl(manifest.observerEvents), seq, firedWake, rearmedWake); const sequenceRows = validatedSequence.rows; const responseIds = new Set(sequenceRows.map((row) => stringField(messageOf(row)?.responseId, "review assistant responseId"))); const sequenceRequests = requests.filter((request) => responseIds.has(request.responseId)); if (sequenceRequests.length !== sequenceRows.length) throw new Error("review sequence provider requests are missing or ambiguous"); const parentUsage = usageOfRows(parentRows); const workerUsage = usageOfRows(workerRows); const combined = zeroUsage(); addUsage(combined, parentUsage); addUsage(combined, workerUsage); return { usage: { parent: parentUsage, workers: workerUsage, combined }, providerTurns: { assistantMessages: assistantTurns, requests: requests.length }, contextBytes: contextBytes(requests), reviewSequence: { wakeMessageId: seq.wakeMessageId, assistantMessageIds: seq.assistantMessageIds, elapsedToWakeMs: validatedSequence.elapsedToWakeMs, providerRequests: sequenceRequests.length, usage: usageOfRows(sequenceRows), contextBytes: contextBytes(sequenceRequests), }, counts: counts(allRows), traceBytes: traceBytes(manifest, audit), }; } export function extractIssue12Metrics(manifest: MetricsManifest): Issue12Metrics { const audit: ArtifactAudit = { seenPaths: new Set(), seenFiles: new Set(), seenProofRoots: new Set() }; const globalRawIds = new Set(); const globalResponseIds = new Set(); return { historicalBaseline: { assistantMessages: 74, input: 415181, output: 44854, cacheRead: 2999424, reasoning: 12272, totalTokens: 3459459, }, identityHashes: identityHashes(manifest.before, manifest.after), before: variantMetrics(manifest.before, audit, globalRawIds, globalResponseIds), after: variantMetrics(manifest.after, audit, globalRawIds, globalResponseIds), }; } export function readMetricsManifest(file: string): MetricsManifest { return record(JSON.parse(fs.readFileSync(file, "utf8")) as unknown, file) as unknown as MetricsManifest; }