@@ -10,6 +10,7 @@ import {
1010 createCodeModeNamespaceRuntime ,
1111 type CodeModeNamespaceRuntime ,
1212} from "./code-mode-namespaces.js" ;
13+ import { registerRepairableCodeModeFailure } from "./code-mode-repair-provenance.js" ;
1314import {
1415 CODE_MODE_WORKER_WATCHDOG_GRACE_MS ,
1516 codeModeFailureCode ,
@@ -31,8 +32,10 @@ import {
3132 cancelPendingBridgeStates ,
3233 cancelPendingBridgeStatesById ,
3334 codeModeWaitingReason ,
35+ createCodeModeBridgeDispatchState ,
3436 createPendingBridgeStates ,
3537 disposeCodeModeRun ,
38+ isCodeModeBridgeRepairEligible ,
3639 pendingBridgeRequestsReplaySafe ,
3740 pendingBridgeStatesForSettlement ,
3841 pendingToolCalls ,
@@ -46,6 +49,7 @@ import {
4649 telemetry ,
4750 waitForPendingBridgeSettlement ,
4851 type PendingBridgeState ,
52+ type CodeModeBridgeDispatchState ,
4953} from "./code-mode-state.js" ;
5054import { normalizeCodeModeWorkerResult , runCodeModeWorker } from "./code-mode-worker.js" ;
5155import type { AgentToolUpdateCallback } from "./runtime/index.js" ;
@@ -74,9 +78,11 @@ export async function runCodeModeExec(params: {
7478 if ( config . enabled === false ) {
7579 throw new ToolInputError ( "code mode is disabled." ) ;
7680 }
77- const runtime = new ToolSearchRuntime ( params . ctx , toToolSearchConfig ( config ) ) ;
81+ const runtime = new ToolSearchRuntime ( params . ctx , toToolSearchConfig ( config ) , {
82+ validateInput : true ,
83+ } ) ;
7884 params . onRuntime ?.( runtime ) ;
79- const bridgeDispatch = { started : false } ;
85+ const bridgeDispatch = createCodeModeBridgeDispatchState ( ) ;
8086 if ( params . signal ?. aborted ) {
8187 return {
8288 status : "failed" as const ,
@@ -241,7 +247,7 @@ async function settleCodeModeResult(params: {
241247 pending ?: PendingBridgeState [ ] ;
242248 activeRunId ?: string ;
243249 reservedActiveRunSlot ?: boolean ;
244- bridgeDispatch : { started : boolean } ;
250+ bridgeDispatch : CodeModeBridgeDispatchState ;
245251 signal ?: AbortSignal ;
246252 onUpdate ?: AgentToolUpdateCallback ;
247253} ) {
@@ -320,11 +326,6 @@ async function settleCodeModeResult(params: {
320326 const newPendingRequests = result . pendingRequests . filter (
321327 ( request ) => ! pendingIds . has ( request . id ) ,
322328 ) ;
323- if ( newPendingRequests . some ( ( request ) => request . method !== "sleep" ) ) {
324- // createPendingBridgeStates starts host calls synchronously. Flip the
325- // evidence first so every later failure is permanently non-retryable.
326- params . bridgeDispatch . started = true ;
327- }
328329 pending . push (
329330 ...createPendingBridgeStates ( {
330331 pendingRequests : newPendingRequests ,
@@ -339,6 +340,7 @@ async function settleCodeModeResult(params: {
339340 ctx : params . ctx ,
340341 signal : params . signal ,
341342 onUpdate : params . onUpdate ,
343+ bridgeDispatch : params . bridgeDispatch ,
342344 } ) ,
343345 ) ;
344346 const ready = await waitForPending (
@@ -375,6 +377,7 @@ async function settleCodeModeResult(params: {
375377 namespaceRuntime : params . namespaceRuntime ,
376378 output,
377379 deliveredOutputCount,
380+ bridgeDispatch : params . bridgeDispatch ,
378381 } ) ;
379382 }
380383 // Deliver the settled frontier only. Unresolved sibling promises remain
@@ -458,9 +461,6 @@ async function settleCodeModeResult(params: {
458461 const newPendingRequests = result . pendingRequests . filter (
459462 ( request ) => ! pendingIds . has ( request . id ) ,
460463 ) ;
461- if ( newPendingRequests . some ( ( request ) => request . method !== "sleep" ) ) {
462- params . bridgeDispatch . started = true ;
463- }
464464 pending . push (
465465 ...createPendingBridgeStates ( {
466466 pendingRequests : newPendingRequests ,
@@ -475,6 +475,7 @@ async function settleCodeModeResult(params: {
475475 ctx : params . ctx ,
476476 signal : params . signal ,
477477 onUpdate : params . onUpdate ,
478+ bridgeDispatch : params . bridgeDispatch ,
478479 } ) ,
479480 ) ;
480481 return storeSnapshotState ( {
@@ -492,6 +493,7 @@ async function settleCodeModeResult(params: {
492493 namespaceRuntime : params . namespaceRuntime ,
493494 output,
494495 deliveredOutputCount,
496+ bridgeDispatch : params . bridgeDispatch ,
495497 } ) ;
496498 } catch ( error ) {
497499 cancelPendingBridgeStates ( pending ) ;
@@ -500,9 +502,6 @@ async function settleCodeModeResult(params: {
500502 releaseReservation ?.( ) ;
501503 }
502504 }
503- if ( result . pendingRequests . some ( ( request ) => request . method !== "sleep" ) ) {
504- params . bridgeDispatch . started = true ;
505- }
506505 return snapshotState ( {
507506 pendingRequests : result . pendingRequests ,
508507 snapshotBytes : result . snapshotBytes ,
@@ -521,6 +520,7 @@ async function settleCodeModeResult(params: {
521520 settlementMode : result . settlementMode ,
522521 signal : params . signal ,
523522 onUpdate : params . onUpdate ,
523+ bridgeDispatch : params . bridgeDispatch ,
524524 } ) ;
525525 }
526526 // Defensive cleanup covers aborts or terminal failures; successful runs have
@@ -531,7 +531,7 @@ async function settleCodeModeResult(params: {
531531 ...( result . status === "completed" ? { value : result . value } : { } ) ,
532532 maxOutputBytes : params . config . maxOutputBytes ,
533533 } ) ;
534- return {
534+ const finalized = {
535535 ...result ,
536536 ...( result . status === "completed" ? { value : bounded . value } : { } ) ,
537537 ...( result . status === "failed"
@@ -544,6 +544,10 @@ async function settleCodeModeResult(params: {
544544 replaySafe : params . replaySafe ,
545545 telemetry : telemetry ( params . runtime ) ,
546546 } ;
547+ if ( finalized . status === "failed" && isCodeModeBridgeRepairEligible ( params . bridgeDispatch ) ) {
548+ registerRepairableCodeModeFailure ( finalized ) ;
549+ }
550+ return finalized ;
547551}
548552
549553export async function runWait ( params : {
@@ -596,7 +600,7 @@ export async function runWait(params: {
596600 error : "code mode execution aborted" ,
597601 code : "aborted" as const ,
598602 failurePhase : "bridge" as const ,
599- bridgeDispatchStarted : true ,
603+ bridgeDispatchStarted : state . bridgeDispatch . started ,
600604 output : takeUndeliveredCodeModeRunOutput ( state ) ,
601605 replaySafe : state . replaySafe ,
602606 telemetry : telemetry ( state . runtime ) ,
@@ -657,7 +661,7 @@ export async function runWait(params: {
657661 runtime : state . runtime ,
658662 catalogProjection : state . catalogProjection ,
659663 namespaceRuntime : state . namespaceRuntime ,
660- bridgeDispatch : { started : true } ,
664+ bridgeDispatch : state . bridgeDispatch ,
661665 deliveredOutputCount : outputTruncated ? 0 : state . deliveredOutputCount ,
662666 pending,
663667 activeRunId : state . runId ,
@@ -676,7 +680,7 @@ export async function runWait(params: {
676680 error : codeModeFailureMessage ( error ) ,
677681 code : codeModeFailureCode ( error ) ,
678682 failurePhase : "bridge" as const ,
679- bridgeDispatchStarted : true ,
683+ bridgeDispatchStarted : state . bridgeDispatch . started ,
680684 output : takeUndeliveredCodeModeRunOutput ( state ) ,
681685 replaySafe : state . replaySafe ,
682686 telemetry : telemetry ( state . runtime ) ,
0 commit comments