@linzumi/cli 1.0.110 → 1.0.112

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,610 @@
1
+ // Spec: kandan/plans/2026-07-10-compute-nodes-rescript-commander-zero-ts-program.md
2
+ // (M3 cluster 9, pipeline).
3
+ // Relationship: Builds the TS-side PARITY ORACLE for the pipeline cluster
4
+ // of the ReScript commander port, the sibling of
5
+ // build-queue-parity-oracle.mjs (cluster 5) and
6
+ // build-harness-codex-parity-oracle.mjs (cluster 7). The oracle bundles
7
+ // the REAL TS pipeline surface - the pure transition core
8
+ // (src/pipeline/transition.ts + contracts + itemProjection, transitively
9
+ // the item-output rendering slice), the thread event-loop driver
10
+ // (threadEventLoop.ts), the stats reporter + turn-stall detector
11
+ // (statsReporter.ts), and the two recovery watchdog primitives
12
+ // (claudeCodeTurnStallWatchdog.ts / claudeCodeDispatchLivenessWatchdog.ts)
13
+ // - behind the cluster 1/2 one-shot BATCH driver (a JSON array of cases on
14
+ // stdin, a JSON array of results on stdout).
15
+ //
16
+ // Byte parity: transition effects are returned VERBATIM (the TS effect
17
+ // objects' own key order rides JSON.stringify), so the ReScript side's
18
+ // effectToJson must reproduce field presence AND insertion order exactly.
19
+ // Determinism: clocks are injected per case (now()/scripted advance ops),
20
+ // the driver's sweep interval is disabled (0) in loop cases, and watchdog
21
+ // timers ride a scripted timer seam fired in scheduling order - nothing
22
+ // here reads wall time.
23
+ //
24
+ // SECRETS: the oracle only ever sees the conformance suite's synthetic
25
+ // payloads. The output is a local test artifact (.differential/ is
26
+ // gitignored, never published) and stays unminified for debuggability.
27
+ import { mkdir } from 'node:fs/promises';
28
+ import { dirname, join } from 'node:path';
29
+ import { fileURLToPath } from 'node:url';
30
+ import { build } from 'esbuild';
31
+
32
+ const packageRoot = dirname(dirname(fileURLToPath(import.meta.url)));
33
+
34
+ const driverSource = `
35
+ import {
36
+ classifyRejectionRetry,
37
+ classifyStallWindowEvidence,
38
+ createInitialLoopState,
39
+ idleStreamStallDisposition,
40
+ isUpstreamTerminal4xxHttpStatus,
41
+ maxConsecutiveEmptyTurns,
42
+ maxEmptyTurnAutoRetries,
43
+ maxIsolatedRejectionRetries,
44
+ maxReasoningStepsWithoutProgress,
45
+ maxTerminalTurnIds,
46
+ agentNarrationOnlyCompletionReason,
47
+ agentNarrationOnlyEmptyTurnCause,
48
+ autoRetriesBurnedSuffix,
49
+ completedWithoutAssistantOutputReason,
50
+ costCapAutoRetrySuppressedSuffix,
51
+ emptyTurnLoopGuardReason,
52
+ linzumiAgentReplyToolName,
53
+ midStreamResumeIncompleteSuffix,
54
+ midStreamResumeRateLimitSuppressedSuffix,
55
+ midStreamResumeStalledAgainSuffix,
56
+ reasoningLoopBreakerReason,
57
+ stallWindowEvidenceSignalClasses,
58
+ stallWindowRateLimited,
59
+ transition,
60
+ turnMappingFailedMethod,
61
+ turnMappingReadyMethod,
62
+ unsupportedCodexToolCallReasonPrefix,
63
+ upstreamHttpStatusRetryClass,
64
+ upstreamTurnErrorSummary,
65
+ } from './src/pipeline/transition';
66
+ import { pipelineItemKey, outboxEntryKinds } from './src/pipeline/contracts';
67
+ import {
68
+ explicitNotificationItemId,
69
+ notificationTurnId,
70
+ pipelineClientMessageId,
71
+ projectSnapshotItems,
72
+ } from './src/pipeline/itemProjection';
73
+ import {
74
+ createThreadEventLoop,
75
+ defaultBufferedTurnMaxAgeMs,
76
+ defaultBufferedTurnMaxEvents,
77
+ defaultBufferedTurnSweepIntervalMs,
78
+ } from './src/pipeline/threadEventLoop';
79
+ import {
80
+ createPipelineStatsReporter,
81
+ defaultPipelineStatsIntervalMs,
82
+ defaultPipelineTurnStalledThresholdMs,
83
+ openItemTurnStalledThresholdMultiplier,
84
+ } from './src/pipeline/statsReporter';
85
+ import {
86
+ createClaudeCodeTurnStallWatchdog,
87
+ defaultClaudeCodeTurnStallThresholdMs,
88
+ } from './src/claudeCodeTurnStallWatchdog';
89
+ import {
90
+ armClaudeCodeDispatchLivenessWatchdog,
91
+ defaultClaudeCodeDispatchLivenessWindowMs,
92
+ } from './src/claudeCodeDispatchLivenessWatchdog';
93
+
94
+ // Scripted deps table shared by transition + loop runs. sourceSeq
95
+ // mappings, burned-retry counts, resume attempts, and ladder receipts are
96
+ // mutated by ops so terminal-time reads see the scripted values.
97
+ function scriptedDeps(caseInput, tables) {
98
+ return {
99
+ threadKey: caseInput.threadKey ?? 'thread-parity',
100
+ instanceId: caseInput.instanceId ?? 'instance-parity',
101
+ sourceSeqForTurn: (turnId) => tables.turnSeqs.get(turnId),
102
+ autoRetryAttemptsForSeq: (seq) => tables.retryAttempts.get(seq) ?? 0,
103
+ midStreamResumeAttemptAtMsForSeq: (seq) => tables.resumeAttempts.get(seq),
104
+ ladderHistoryForSeq: (seq, fate) => tables.ladders.get(seq + ':' + fate),
105
+ linzumiAgentSession: caseInput.linzumiAgentSession === true,
106
+ };
107
+ }
108
+
109
+ function applyTableOp(tables, op) {
110
+ if (op.op === 'mapTurn') tables.turnSeqs.set(op.turnId, op.seq);
111
+ if (op.op === 'unmapTurn') tables.turnSeqs.delete(op.turnId);
112
+ if (op.op === 'retryAttempts') tables.retryAttempts.set(op.seq, op.attempts);
113
+ if (op.op === 'resumeAttemptAt') tables.resumeAttempts.set(op.seq, op.atMs);
114
+ if (op.op === 'ladder') tables.ladders.set(op.seq + ':' + op.fate, op.ladder);
115
+ }
116
+
117
+ function newTables() {
118
+ return {
119
+ turnSeqs: new Map(),
120
+ retryAttempts: new Map(),
121
+ resumeAttempts: new Map(),
122
+ ladders: new Map(),
123
+ };
124
+ }
125
+
126
+ function runTransitionCase(caseInput) {
127
+ const tables = newTables();
128
+ const state = createInitialLoopState();
129
+ const deps = scriptedDeps(caseInput, tables);
130
+ const steps = [];
131
+ for (const op of caseInput.ops ?? []) {
132
+ if (op.op === 'event') {
133
+ steps.push(transition(state, op.event, deps));
134
+ } else {
135
+ applyTableOp(tables, op);
136
+ }
137
+ }
138
+ return { steps };
139
+ }
140
+
141
+ function runLoopCase(caseInput) {
142
+ const tables = newTables();
143
+ let nowMs = caseInput.startMs ?? 0;
144
+ const trace = [];
145
+ const outbox = {
146
+ enqueue: (entry) => {
147
+ trace.push({
148
+ t: 'enqueue',
149
+ kind: entry.kind,
150
+ data: entry.data,
151
+ coalesceKey: entry.coalesceKey ?? null,
152
+ resolution: entry.resolution,
153
+ });
154
+ return 0;
155
+ },
156
+ flush: async () => {
157
+ if (caseInput.flushFails === true) {
158
+ throw new Error('scripted flush failure');
159
+ }
160
+ trace.push({ t: 'flushed' });
161
+ },
162
+ pause: () => {},
163
+ resume: () => {},
164
+ metrics: () => {
165
+ throw new Error('not used');
166
+ },
167
+ close: async () => {},
168
+ };
169
+ const loop = createThreadEventLoop({
170
+ threadKey: caseInput.threadKey ?? 'thread-parity',
171
+ outbox,
172
+ sourceSeqForTurn: (turnId) => tables.turnSeqs.get(turnId),
173
+ autoRetryAttemptsForSeq: (seq) => tables.retryAttempts.get(seq) ?? 0,
174
+ midStreamResumeAttemptAtMsForSeq: (seq) => tables.resumeAttempts.get(seq),
175
+ ladderHistoryForSeq: (seq, fate) => tables.ladders.get(seq + ':' + fate),
176
+ onTurnTerminal: (turnId, outcome, reason, retryableEmptyTurn, autoResume, fate) => {
177
+ trace.push({
178
+ t: 'terminal',
179
+ turnId,
180
+ outcome,
181
+ reason: reason ?? null,
182
+ retryableEmptyTurn: retryableEmptyTurn === true,
183
+ autoResumeMidStreamStall: autoResume === true,
184
+ fate: fate ?? null,
185
+ });
186
+ },
187
+ requestSnapshot: (turnId) => {
188
+ trace.push({ t: 'requestSnapshot', turnId });
189
+ },
190
+ instanceId: caseInput.instanceId ?? 'instance-parity',
191
+ log: (event, fields) => {
192
+ trace.push({ t: 'log', event, fields });
193
+ },
194
+ linzumiAgentSession: caseInput.linzumiAgentSession === true,
195
+ now: () => nowMs,
196
+ bufferedTurnMaxAgeMs: caseInput.bufferedTurnMaxAgeMs,
197
+ bufferedTurnMaxEvents: caseInput.bufferedTurnMaxEvents,
198
+ // Determinism: the quiet-period sweep timer is disabled; the age cap
199
+ // still applies on every processed event.
200
+ bufferedTurnSweepIntervalMs: 0,
201
+ });
202
+ const metricsJson = () => {
203
+ const metrics = loop.metrics();
204
+ return {
205
+ queueDepth: metrics.queueDepth,
206
+ phase: metrics.phase,
207
+ activeTurns: metrics.activeTurns.map((turn) => ({
208
+ turnId: turn.turnId,
209
+ sourceSeq: turn.sourceSeq,
210
+ startedAtMs: turn.startedAtMs,
211
+ lastActivityAtMs: turn.lastActivityAtMs ?? null,
212
+ lastActivityKind: turn.lastActivityKind ?? null,
213
+ openItems: (turn.openItems ?? []).map((item) => ({
214
+ kind: item.kind,
215
+ startedAtMs: item.startedAtMs,
216
+ })),
217
+ })),
218
+ bufferedTurns: metrics.bufferedTurns.map((turn) => ({
219
+ turnId: turn.turnId,
220
+ bufferedEventCount: turn.bufferedEventCount,
221
+ firstBufferedAtMs: turn.firstBufferedAtMs,
222
+ lastActivityAtMs: turn.lastActivityAtMs ?? null,
223
+ lastActivityKind: turn.lastActivityKind ?? null,
224
+ openItems: (turn.openItems ?? []).map((item) => ({
225
+ kind: item.kind,
226
+ startedAtMs: item.startedAtMs,
227
+ })),
228
+ })),
229
+ };
230
+ };
231
+ const run = async () => {
232
+ for (const op of caseInput.ops ?? []) {
233
+ if (op.op === 'submit') loop.submit(op.event);
234
+ else if (op.op === 'advance') nowMs = op.toMs;
235
+ else if (op.op === 'metrics') trace.push({ t: 'metrics', value: metricsJson() });
236
+ else if (op.op === 'noteActivity') loop.noteTurnActivity(op.turnId, op.kind);
237
+ else if (op.op === 'drainAndClose') {
238
+ await loop.drainAndClose(op.timeoutMs);
239
+ trace.push({ t: 'drained' });
240
+ } else applyTableOp(tables, op);
241
+ }
242
+ };
243
+ return run().then(() => ({ trace }));
244
+ }
245
+
246
+ function runStatsCase(caseInput) {
247
+ let nowMs = caseInput.startMs ?? 0;
248
+ let outboxMetrics = caseInput.outbox;
249
+ let loopMetrics = caseInput.loop;
250
+ let waits = caseInput.waits ?? [];
251
+ const trace = [];
252
+ const reporter = createPipelineStatsReporter({
253
+ threadKey: caseInput.threadKey ?? 'thread-parity',
254
+ outboxMetrics: () => ({
255
+ ...outboxMetrics,
256
+ oldestEntryAgeMs: outboxMetrics.oldestEntryAgeMs ?? undefined,
257
+ pushLatency: outboxMetrics.pushLatency ?? undefined,
258
+ }),
259
+ loopMetrics: () => ({
260
+ queueDepth: loopMetrics.queueDepth,
261
+ phase: loopMetrics.phase,
262
+ activeTurns: loopMetrics.activeTurns.map((turn) => ({
263
+ ...turn,
264
+ lastActivityAtMs: turn.lastActivityAtMs ?? undefined,
265
+ lastActivityKind: turn.lastActivityKind ?? undefined,
266
+ openItems: turn.openItems ?? [],
267
+ })),
268
+ bufferedTurns: loopMetrics.bufferedTurns.map((turn) => ({
269
+ ...turn,
270
+ lastActivityAtMs: turn.lastActivityAtMs ?? undefined,
271
+ lastActivityKind: turn.lastActivityKind ?? undefined,
272
+ openItems: turn.openItems ?? [],
273
+ })),
274
+ }),
275
+ log: (event, fields) => trace.push({ t: 'log', event, fields }),
276
+ intervalMs: 0,
277
+ turnStalledThresholdMs: caseInput.thresholdMs,
278
+ turnWaitStates: () => waits.map((wait) => ({ turnId: wait.turnId, kind: wait.kind })),
279
+ now: () => nowMs,
280
+ });
281
+ for (const op of caseInput.ops ?? []) {
282
+ if (op.op === 'set') {
283
+ if (op.outbox !== undefined) outboxMetrics = op.outbox;
284
+ if (op.loop !== undefined) loopMetrics = op.loop;
285
+ if (op.waits !== undefined) waits = op.waits;
286
+ } else if (op.op === 'sample') {
287
+ nowMs = op.nowMs ?? nowMs;
288
+ reporter.sample();
289
+ } else if (op.op === 'stop') {
290
+ reporter.stop();
291
+ }
292
+ }
293
+ return { trace };
294
+ }
295
+
296
+ // Scripted timer seam shared by the watchdog runs: timers fire in
297
+ // scheduling order once the scripted clock passes their due instant.
298
+ function scriptedTimers(clock, trace, label) {
299
+ let nextId = 1;
300
+ const pending = [];
301
+ return {
302
+ setTimer: (callback, delayMs) => {
303
+ const id = nextId;
304
+ nextId += 1;
305
+ trace.push({ t: label + '.set', id, delayMs, dueAtMs: clock.nowMs + delayMs });
306
+ pending.push({ id, dueAtMs: clock.nowMs + delayMs, callback });
307
+ return id;
308
+ },
309
+ clearTimer: (handle) => {
310
+ const index = pending.findIndex((timer) => timer.id === handle);
311
+ if (index >= 0) {
312
+ trace.push({ t: label + '.clear', id: pending[index].id });
313
+ pending.splice(index, 1);
314
+ }
315
+ },
316
+ fireDue: () => {
317
+ let fired = true;
318
+ while (fired) {
319
+ fired = false;
320
+ const index = pending.findIndex((timer) => timer.dueAtMs <= clock.nowMs);
321
+ if (index >= 0) {
322
+ const timer = pending.splice(index, 1)[0];
323
+ trace.push({ t: label + '.fire', id: timer.id });
324
+ timer.callback();
325
+ fired = true;
326
+ }
327
+ }
328
+ },
329
+ };
330
+ }
331
+
332
+ function runTurnStallWatchdogCase(caseInput) {
333
+ const clock = { nowMs: caseInput.startMs ?? 0 };
334
+ const trace = [];
335
+ const timers = scriptedTimers(clock, trace, 'timer');
336
+ let turnActive = caseInput.turnActive === true;
337
+ let watchdog;
338
+ const outcome = (() => {
339
+ try {
340
+ watchdog = createClaudeCodeTurnStallWatchdog({
341
+ thresholdMs: caseInput.thresholdMs,
342
+ isTurnActive: () => turnActive,
343
+ onStall: (info) => trace.push({ t: 'stall', silentMs: info.silentMs }),
344
+ now: () => clock.nowMs,
345
+ setTimer: timers.setTimer,
346
+ clearTimer: timers.clearTimer,
347
+ });
348
+ return { ok: true };
349
+ } catch (error) {
350
+ return { ok: false, message: error instanceof Error ? error.message : String(error) };
351
+ }
352
+ })();
353
+ if (outcome.ok) {
354
+ for (const op of caseInput.ops ?? []) {
355
+ if (op.op === 'advance') {
356
+ clock.nowMs = op.toMs;
357
+ timers.fireDue();
358
+ } else if (op.op === 'kick') watchdog.kick();
359
+ else if (op.op === 'stop') watchdog.stop();
360
+ else if (op.op === 'setActive') turnActive = op.active === true;
361
+ }
362
+ }
363
+ return { outcome, trace };
364
+ }
365
+
366
+ function runDispatchWatchdogCase(caseInput) {
367
+ const clock = { nowMs: caseInput.startMs ?? 0 };
368
+ const trace = [];
369
+ const timers = scriptedTimers(clock, trace, 'timer');
370
+ let delivered = caseInput.delivered === true;
371
+ let sessionClosed = caseInput.sessionClosed === true;
372
+ let lastActivityMs = caseInput.lastActivityMs ?? 0;
373
+ let pendingBlocking = caseInput.pendingBlocking === true;
374
+ let watchdog;
375
+ const outcome = (() => {
376
+ try {
377
+ watchdog = armClaudeCodeDispatchLivenessWatchdog({
378
+ windowMs: caseInput.windowMs,
379
+ isDelivered: () => delivered,
380
+ isSessionClosed: () => sessionClosed,
381
+ lastSdkActivityMs: () => lastActivityMs,
382
+ hasPendingBlockingRequest: () => pendingBlocking,
383
+ onWedged: (info) => trace.push({ t: 'wedged', waitedMs: info.waitedMs, cause: info.cause }),
384
+ now: () => clock.nowMs,
385
+ setTimer: timers.setTimer,
386
+ clearTimer: timers.clearTimer,
387
+ });
388
+ return { ok: true };
389
+ } catch (error) {
390
+ return { ok: false, message: error instanceof Error ? error.message : String(error) };
391
+ }
392
+ })();
393
+ if (outcome.ok) {
394
+ for (const op of caseInput.ops ?? []) {
395
+ if (op.op === 'advance') {
396
+ clock.nowMs = op.toMs;
397
+ timers.fireDue();
398
+ } else if (op.op === 'setDelivered') delivered = op.value === true;
399
+ else if (op.op === 'setSessionClosed') sessionClosed = op.value === true;
400
+ else if (op.op === 'setActivity') lastActivityMs = op.atMs;
401
+ else if (op.op === 'setPendingBlocking') pendingBlocking = op.value === true;
402
+ else if (op.op === 'stop') watchdog.stop();
403
+ }
404
+ }
405
+ return { outcome, trace };
406
+ }
407
+
408
+ async function runCase(input) {
409
+ switch (input.kind) {
410
+ case 'constants':
411
+ return {
412
+ outboxEntryKinds,
413
+ turnMappingReadyMethod,
414
+ turnMappingFailedMethod,
415
+ maxTerminalTurnIds,
416
+ maxEmptyTurnAutoRetries,
417
+ maxConsecutiveEmptyTurns,
418
+ maxReasoningStepsWithoutProgress,
419
+ maxIsolatedRejectionRetries,
420
+ completedWithoutAssistantOutputReason,
421
+ linzumiAgentReplyToolName,
422
+ agentNarrationOnlyCompletionReason,
423
+ agentNarrationOnlyEmptyTurnCause,
424
+ emptyTurnLoopGuardReason,
425
+ unsupportedCodexToolCallReasonPrefix,
426
+ midStreamResumeRateLimitSuppressedSuffix,
427
+ costCapAutoRetrySuppressedSuffix,
428
+ stallWindowEvidenceSignalClasses,
429
+ defaultBufferedTurnMaxAgeMs,
430
+ defaultBufferedTurnMaxEvents,
431
+ defaultBufferedTurnSweepIntervalMs,
432
+ defaultPipelineStatsIntervalMs,
433
+ defaultPipelineTurnStalledThresholdMs,
434
+ openItemTurnStalledThresholdMultiplier,
435
+ defaultClaudeCodeTurnStallThresholdMs,
436
+ defaultClaudeCodeDispatchLivenessWindowMs,
437
+ };
438
+ case 'reasoningLoopBreakerReason':
439
+ return reasoningLoopBreakerReason(input.steps);
440
+ case 'autoRetriesBurnedSuffix':
441
+ return autoRetriesBurnedSuffix(input.attempts);
442
+ case 'midStreamResumeStalledAgainSuffix':
443
+ return midStreamResumeStalledAgainSuffix(input.attemptAtMs);
444
+ case 'midStreamResumeIncompleteSuffix':
445
+ return midStreamResumeIncompleteSuffix(input.attemptAtMs);
446
+ case 'upstreamHttpStatusRetryClass':
447
+ return upstreamHttpStatusRetryClass(input.status);
448
+ case 'isUpstreamTerminal4xxHttpStatus':
449
+ return isUpstreamTerminal4xxHttpStatus(input.status ?? undefined);
450
+ case 'classifyStallWindowEvidence':
451
+ return classifyStallWindowEvidence({
452
+ lastUpstreamErrorReason: input.turn.lastUpstreamErrorReason ?? undefined,
453
+ lastErrorDiagnosticText: input.turn.lastErrorDiagnosticText ?? undefined,
454
+ lastUpstreamErrorHttpStatus: input.turn.lastUpstreamErrorHttpStatus ?? undefined,
455
+ upstreamRejectedSeen: input.turn.upstreamRejectedSeen === true,
456
+ providerOutageFlaggedSeen: input.turn.providerOutageFlaggedSeen === true,
457
+ toolFatalSeen: input.turn.toolFatalSeen === true,
458
+ errorNotificationCount: input.turn.errorNotificationCount ?? 0,
459
+ unattributedErrorNotificationCount:
460
+ input.turn.unattributedErrorNotificationCount ?? 0,
461
+ modelInteractionSeen: input.turn.modelInteractionSeen === true,
462
+ });
463
+ case 'stallWindowRateLimited':
464
+ return stallWindowRateLimited({
465
+ lastUpstreamErrorReason: input.turn.lastUpstreamErrorReason ?? undefined,
466
+ lastErrorDiagnosticText: input.turn.lastErrorDiagnosticText ?? undefined,
467
+ lastUpstreamErrorHttpStatus: input.turn.lastUpstreamErrorHttpStatus ?? undefined,
468
+ upstreamRejectedSeen: input.turn.upstreamRejectedSeen === true,
469
+ providerOutageFlaggedSeen: input.turn.providerOutageFlaggedSeen === true,
470
+ toolFatalSeen: input.turn.toolFatalSeen === true,
471
+ errorNotificationCount: input.turn.errorNotificationCount ?? 0,
472
+ unattributedErrorNotificationCount:
473
+ input.turn.unattributedErrorNotificationCount ?? 0,
474
+ modelInteractionSeen: input.turn.modelInteractionSeen === true,
475
+ });
476
+ case 'idleStreamStallDisposition': {
477
+ const disposition = idleStreamStallDisposition(
478
+ input.method,
479
+ input.params,
480
+ {
481
+ lastUpstreamErrorReason: input.turn.lastUpstreamErrorReason ?? undefined,
482
+ lastErrorDiagnosticText: input.turn.lastErrorDiagnosticText ?? undefined,
483
+ lastUpstreamErrorHttpStatus: input.turn.lastUpstreamErrorHttpStatus ?? undefined,
484
+ upstreamRejectedSeen: input.turn.upstreamRejectedSeen === true,
485
+ providerOutageFlaggedSeen: input.turn.providerOutageFlaggedSeen === true,
486
+ toolFatalSeen: input.turn.toolFatalSeen === true,
487
+ errorNotificationCount: input.turn.errorNotificationCount ?? 0,
488
+ unattributedErrorNotificationCount:
489
+ input.turn.unattributedErrorNotificationCount ?? 0,
490
+ modelInteractionSeen: input.turn.modelInteractionSeen === true,
491
+ },
492
+ {
493
+ costCap402Seen: input.session.costCap402Seen === true,
494
+ midStreamResumeAttemptAtMs: input.session.midStreamResumeAttemptAtMs ?? undefined,
495
+ }
496
+ );
497
+ return {
498
+ autoRetryPreFirstByteStall: disposition.autoRetryPreFirstByteStall,
499
+ messageStateRetryable: disposition.messageStateRetryable,
500
+ costCapAutoRetrySuppressed: disposition.costCapAutoRetrySuppressed,
501
+ messageStateFate: disposition.messageStateFate ?? null,
502
+ autoResumeMidStreamStall: disposition.autoResumeMidStreamStall,
503
+ midStreamResumeRateLimitSuppressed: disposition.midStreamResumeRateLimitSuppressed,
504
+ midStreamResumeAttemptBurnedAtMs:
505
+ disposition.midStreamResumeAttemptBurnedAtMs ?? null,
506
+ };
507
+ }
508
+ case 'classifyRejectionRetry': {
509
+ const disposition = classifyRejectionRetry({
510
+ providerOutageFlagged: input.providerOutageFlagged === true,
511
+ priorConsecutiveIsolatedRejections: input.priorConsecutiveIsolatedRejections,
512
+ stormDetail: input.stormDetail ?? undefined,
513
+ rejectionDetail: input.rejectionDetail ?? undefined,
514
+ });
515
+ return {
516
+ kind: disposition.kind,
517
+ retryable: disposition.retryable,
518
+ reason: disposition.reason,
519
+ };
520
+ }
521
+ case 'upstreamTurnErrorSummary':
522
+ return upstreamTurnErrorSummary(input.params) ?? null;
523
+ case 'itemKey':
524
+ return pipelineItemKey({
525
+ turnId: input.identity.turnId,
526
+ itemId: input.identity.itemId ?? undefined,
527
+ itemIndex: input.identity.itemIndex,
528
+ streamKind: input.identity.streamKind,
529
+ });
530
+ case 'clientMessageId':
531
+ return pipelineClientMessageId(
532
+ input.instanceId,
533
+ input.turnId,
534
+ input.streamKind,
535
+ input.wireItemKey
536
+ );
537
+ case 'notificationTurnId':
538
+ return notificationTurnId(input.params) ?? null;
539
+ case 'explicitItemId':
540
+ return explicitNotificationItemId(input.params) ?? null;
541
+ case 'projectSnapshotItems':
542
+ return projectSnapshotItems(input.items).map((proj) => ({
543
+ snapshotIndex: proj.snapshotIndex,
544
+ itemId: proj.itemId ?? null,
545
+ streamKind: proj.streamKind,
546
+ body: proj.body,
547
+ structured: proj.structured,
548
+ }));
549
+ case 'transitionRun':
550
+ return runTransitionCase(input);
551
+ case 'loopRun':
552
+ return runLoopCase(input);
553
+ case 'statsRun':
554
+ return runStatsCase(input);
555
+ case 'turnStallWatchdogRun':
556
+ return runTurnStallWatchdogCase(input);
557
+ case 'dispatchWatchdogRun':
558
+ return runDispatchWatchdogCase(input);
559
+ default:
560
+ return { error: 'unknown case kind ' + String(input.kind) };
561
+ }
562
+ }
563
+
564
+ async function runBatch() {
565
+ const chunks = [];
566
+ for await (const chunk of process.stdin) chunks.push(chunk);
567
+ const cases = JSON.parse(Buffer.concat(chunks).toString('utf8'));
568
+ const results = [];
569
+ for (const testCase of cases) {
570
+ results.push(await runCase(testCase));
571
+ }
572
+ process.stdout.write(JSON.stringify(results), () => {
573
+ process.exit(0);
574
+ });
575
+ }
576
+
577
+ runBatch().then(
578
+ () => {},
579
+ (error) => {
580
+ process.stderr.write(String(error && error.stack ? error.stack : error) + '\\n');
581
+ process.exit(1);
582
+ }
583
+ );
584
+ `;
585
+
586
+ const outDir = join(packageRoot, '.differential');
587
+ const outPath = join(outDir, 'pipeline-parity-oracle.mjs');
588
+
589
+ await mkdir(outDir, { recursive: true });
590
+
591
+ await build({
592
+ stdin: {
593
+ contents: driverSource,
594
+ resolveDir: packageRoot,
595
+ sourcefile: 'pipeline-parity-driver.ts',
596
+ loader: 'ts',
597
+ },
598
+ bundle: true,
599
+ platform: 'node',
600
+ target: 'node20',
601
+ format: 'esm',
602
+ outfile: outPath,
603
+ minify: false,
604
+ sourcemap: false,
605
+ legalComments: 'none',
606
+ logLevel: 'silent',
607
+ external: ['ws', 'undici', 'blessed', '@anthropic-ai/claude-agent-sdk'],
608
+ });
609
+
610
+ process.stdout.write(outPath + '\n');