11import { randomUUID } from "node:crypto" ;
22import { codeModeReplayIdForToolCall } from "./code-mode-bridge.js" ;
33import { awaitCodeModeDeadline } from "./code-mode-deadline.js" ;
4+ import { boundCodeModeResult } from "./code-mode-json.js" ;
45import {
56 createCodeModeNamespaceRuntime ,
67 type CodeModeNamespaceRuntime ,
@@ -10,8 +11,7 @@ import {
1011 codeModeFailureCode ,
1112 codeModeFailureMessage ,
1213 createCodeModeApiFilesForRun ,
13- enforceOutputLimit ,
14- enforceResultLimit ,
14+ boundOutputToLimit ,
1515 enforceSnapshotPayloadLimits ,
1616 prepareSource ,
1717 resolveCodeModeConfig ,
@@ -240,7 +240,7 @@ async function settleCodeModeResult(params: {
240240 let pending = params . pending ?? [ ] ;
241241 const activeRunId = params . activeRunId ?? `cm_${ randomUUID ( ) } ` ;
242242 const output = params . output ;
243- const deliveredOutputCount = params . deliveredOutputCount ?? 0 ;
243+ let deliveredOutputCount = params . deliveredOutputCount ?? 0 ;
244244 // One exec/wait call shares a single wall-clock deadline across its initial
245245 // worker run and this inline settle phase, so auto-draining bridge calls
246246 // cannot stack a second full `timeoutMs` budget on top of the run that
@@ -300,7 +300,6 @@ async function settleCodeModeResult(params: {
300300 enforceSnapshotPayloadLimits ( {
301301 snapshotBytes : result . snapshotBytes ,
302302 config : params . config ,
303- output,
304303 } ) ;
305304 if ( ! params . reservedActiveRunSlot ) {
306305 releaseReservation = reserveActiveRunSlot ( ) ;
@@ -317,6 +316,7 @@ async function settleCodeModeResult(params: {
317316 pending . push (
318317 ...createPendingBridgeStates ( {
319318 pendingRequests : newPendingRequests ,
319+ config : params . config ,
320320 runtime : params . runtime ,
321321 namespaceRuntime : params . namespaceRuntime ,
322322 parentToolCallId : params . parentToolCallId ,
@@ -388,7 +388,9 @@ async function settleCodeModeResult(params: {
388388 ) ,
389389 ) ;
390390 output . push ( ...result . output ) ;
391- enforceOutputLimit ( output , params . config ) ;
391+ if ( boundOutputToLimit ( output , params . config ) ) {
392+ deliveredOutputCount = 0 ;
393+ }
392394 } catch ( error ) {
393395 cancelPendingBridgeStates ( pending ) ;
394396 throw error ;
@@ -409,7 +411,8 @@ async function settleCodeModeResult(params: {
409411 cancelPendingBridgeStates ( pending ) ;
410412 return {
411413 status : "failed" as const ,
412- error : "restart-safe code mode cannot call side-effecting tools." ,
414+ error :
415+ "restart-safe code mode cannot call tool surfaces that are not proven replay-safe; recovery runs must use audited read, grep, or find tools." ,
413416 code : "invalid_input" as const ,
414417 failurePhase : params . bridgeDispatch . started ? ( "bridge" as const ) : ( "input" as const ) ,
415418 bridgeDispatchStarted : params . bridgeDispatch . started ,
@@ -426,7 +429,6 @@ async function settleCodeModeResult(params: {
426429 enforceSnapshotPayloadLimits ( {
427430 snapshotBytes : result . snapshotBytes ,
428431 config : params . config ,
429- output,
430432 } ) ;
431433 // Reserve before launching fresh work; transferred snapshots must
432434 // obey the same process-wide active-run cap as initial suspensions.
@@ -443,6 +445,7 @@ async function settleCodeModeResult(params: {
443445 pending . push (
444446 ...createPendingBridgeStates ( {
445447 pendingRequests : newPendingRequests ,
448+ config : params . config ,
446449 runtime : params . runtime ,
447450 namespaceRuntime : params . namespaceRuntime ,
448451 parentToolCallId : params . parentToolCallId ,
@@ -499,20 +502,21 @@ async function settleCodeModeResult(params: {
499502 // Defensive cleanup covers aborts or terminal failures; successful runs have
500503 // already drained every dispatched call before releasing their snapshot.
501504 cancelPendingBridgeStates ( pending ) ;
502- enforceResultLimit ( {
505+ const bounded = boundCodeModeResult ( {
503506 output,
504- value : result . status === "completed" ? result . value : undefined ,
505- config : params . config ,
507+ ... ( result . status === "completed" ? { value : result . value } : { } ) ,
508+ maxOutputBytes : params . config . maxOutputBytes ,
506509 } ) ;
507510 return {
508511 ...result ,
512+ ...( result . status === "completed" ? { value : bounded . value } : { } ) ,
509513 ...( result . status === "failed"
510514 ? {
511515 failurePhase : params . bridgeDispatch . started ? ( "bridge" as const ) : result . failurePhase ,
512516 bridgeDispatchStarted : params . bridgeDispatch . started ,
513517 }
514518 : { } ) ,
515- output : output . slice ( deliveredOutputCount ) ,
519+ output : bounded . output . slice ( bounded . truncated ? 0 : deliveredOutputCount ) ,
516520 replaySafe : params . replaySafe ,
517521 telemetry : telemetry ( params . runtime ) ,
518522 } ;
@@ -616,7 +620,7 @@ export async function runWait(params: {
616620 ) ,
617621 ) ;
618622 const output = [ ...state . output , ...result . output ] ;
619- enforceOutputLimit ( output , state . config ) ;
623+ const outputTruncated = boundOutputToLimit ( output , state . config ) ;
620624 return await settleCodeModeResult ( {
621625 result,
622626 output,
@@ -629,7 +633,7 @@ export async function runWait(params: {
629633 runtime : state . runtime ,
630634 namespaceRuntime : state . namespaceRuntime ,
631635 bridgeDispatch : { started : true } ,
632- deliveredOutputCount : state . deliveredOutputCount ,
636+ deliveredOutputCount : outputTruncated ? 0 : state . deliveredOutputCount ,
633637 pending,
634638 activeRunId : state . runId ,
635639 reservedActiveRunSlot : true ,
0 commit comments