44 MAX_CONTENT_NODES ,
55 MAX_MODEL_CONTENT_BYTES ,
66 measureModelContent ,
7+ projectResolvedSecretModelContent ,
78} from '@/executor/utils/resolved-secret-content-projection'
89import type { ResolvedSecretTraceRegistry } from '@/executor/utils/resolved-secret-trace-registry'
910
@@ -195,14 +196,16 @@ function compactBlockLogOutputs(logs: unknown, executionId: string | undefined):
195196}
196197
197198/**
198- * The most serialized characters a run result carries to the agent. The agent holds a workflow
199- * tool result inline up to twice its 12K-character output window and stores a larger one behind a
200- * paged read, which costs the agent a round to learn whether the run passed. This leaves room for
201- * the envelope and the secret projection's placeholders .
199+ * The most serialized characters a run result carries to the agent, measured after secret
200+ * projection. The agent holds a workflow tool result inline up to twice its 12K-character output
201+ * window and stores a larger one behind a paged read, which costs the agent a round to learn
202+ * whether the run passed. This leaves room for the envelope .
202203 */
203204const MODEL_RUN_RESULT_MAX_CHARS = 16_000
204- /** How much of an oversized final output stays inline, as serialized JSON, when no secret is active. */
205+ /** The most of an oversized final output that stays inline, as serialized JSON, when no secret is active. */
205206const FINAL_OUTPUT_PREVIEW_CHARS = 4_000
207+ /** A preview shorter than this says too little to be worth more than a pointer. */
208+ const FINAL_OUTPUT_PREVIEW_MIN_CHARS = 200
206209
207210/** `value` serialized the way the agent receives it; undefined for a value JSON cannot encode. */
208211function serialize ( value : unknown ) : string | undefined {
@@ -218,8 +221,12 @@ function serialize(value: unknown): string | undefined {
218221 * active: a kept prefix could otherwise cut through a secret and leave a fragment no whole-literal
219222 * redaction matches.
220223 */
221- function finalOutputPreview ( serialized : string , executionId : string | undefined ) : string {
222- let end = FINAL_OUTPUT_PREVIEW_CHARS
224+ function finalOutputPreview (
225+ serialized : string ,
226+ chars : number ,
227+ executionId : string | undefined
228+ ) : string {
229+ let end = chars
223230 const last = serialized . charCodeAt ( end - 1 )
224231 if ( last >= 0xd800 && last <= 0xdbff ) end -= 1
225232 return `${ serialized . slice ( 0 , end ) } …[final output cut at ${ end } of ${ serialized . length } chars; inspect with logs get ${ executionId ?? '<executionId>' } --trace]`
@@ -229,17 +236,33 @@ function finalOutputPreview(serialized: string, executionId: string | undefined)
229236 * Holds a run result to {@link MODEL_RUN_RESULT_MAX_CHARS} so the agent reads pass/fail, the
230237 * failing block's error, and each block's status inline. Echoed block inputs and outputs are
231238 * replaced with pointers, largest first, until the result fits; the per-block entries keep their
232- * name, id, status, and error. If that is not enough, the final output is cut to a preview, or to a
233- * pointer when `walksContent` (a secret is active). Values a caller named with `select` are kept.
239+ * name, id, status, and error. If that is not enough, the final output is cut to a preview that
240+ * fits what is left, or to a pointer when a secret is active. Values a caller named with `select`
241+ * are kept. A result that still does not fit is returned as far as it was reduced.
242+ *
243+ * While `registry` makes the projection walk the result, its size is measured as projected:
244+ * replacing many short secrets with their longer names can grow it. Raw sizes rank the parts and
245+ * skip the projection while the raw result is plainly over.
234246 */
235247function fitRunResultToWindow (
236248 data : Record < string , unknown > ,
237249 error : string | undefined ,
238250 executionId : string | undefined ,
251+ registry : ResolvedSecretTraceRegistry | undefined ,
239252 walksContent : boolean
240253) : Record < string , unknown > {
241- let total = serialize ( error === undefined ? { output : data } : { output : data , error } ) ?. length
242- if ( total === undefined || total <= MODEL_RUN_RESULT_MAX_CHARS ) return data
254+ const envelope = ( candidate : Record < string , unknown > ) =>
255+ error === undefined ? { output : candidate } : { output : candidate , error }
256+ const fits = ( candidate : Record < string , unknown > , rawChars : number ) : boolean => {
257+ if ( rawChars > MODEL_RUN_RESULT_MAX_CHARS ) return false
258+ if ( ! walksContent ) return true
259+ const projection = projectResolvedSecretModelContent ( envelope ( candidate ) , registry )
260+ const projected = projection . safe ? serialize ( projection . value ) ?. length : undefined
261+ return ( projected ?? rawChars ) <= MODEL_RUN_RESULT_MAX_CHARS
262+ }
263+
264+ let raw = serialize ( envelope ( data ) ) ?. length
265+ if ( raw === undefined || fits ( data , raw ) ) return data
243266
244267 let fitted = data
245268 if ( Array . isArray ( data . logs ) ) {
@@ -255,7 +278,6 @@ function fitRunResultToWindow(
255278 parts . sort ( ( left , right ) => right . size - left . size )
256279 const logs = [ ...data . logs ]
257280 for ( const part of parts ) {
258- if ( total <= MODEL_RUN_RESULT_MAX_CHARS ) break
259281 const entry = logs [ part . index ] as Record < string , unknown >
260282 const pointer =
261283 part . field === 'input'
@@ -267,22 +289,31 @@ function fitRunResultToWindow(
267289 const saved = part . size - JSON . stringify ( pointer ) . length
268290 if ( saved <= 0 ) continue
269291 logs [ part . index ] = { ...entry , [ part . field ] : pointer }
270- total -= saved
292+ raw -= saved
293+ fitted = { ...data , logs }
294+ if ( fits ( fitted , raw ) ) return fitted
271295 }
272- fitted = { ...data , logs }
273296 }
274- if ( total <= MODEL_RUN_RESULT_MAX_CHARS || ! Object . hasOwn ( fitted , 'output' ) ) return fitted
297+ if ( ! Object . hasOwn ( fitted , 'output' ) ) return fitted
275298
299+ // A final output no longer than a preview is what the run was for; it stays whole.
276300 const serialized = serialize ( fitted . output )
277301 if ( serialized === undefined || serialized . length <= FINAL_OUTPUT_PREVIEW_CHARS ) return fitted
278302 const label = sizeBlockOutput ( fitted . output ) ?. label
279- if ( walksContent && ! label ) return fitted
280- return {
281- ...fitted ,
282- output : walksContent
283- ? blockOutputPointer ( label ?? 'output omitted' , executionId )
284- : finalOutputPreview ( serialized , executionId ) ,
303+ if ( ! label ) return fitted
304+ const pointed = { ...fitted , output : blockOutputPointer ( label , executionId ) }
305+ if ( walksContent ) return pointed
306+
307+ const rest = serialize ( envelope ( pointed ) ) ?. length ?? Number . POSITIVE_INFINITY
308+ let chars = Math . min ( FINAL_OUTPUT_PREVIEW_CHARS , MODEL_RUN_RESULT_MAX_CHARS - rest )
309+ while ( chars >= FINAL_OUTPUT_PREVIEW_MIN_CHARS && chars < serialized . length ) {
310+ const previewed = { ...fitted , output : finalOutputPreview ( serialized , chars , executionId ) }
311+ const previewedChars = serialize ( envelope ( previewed ) ) ?. length ?? Number . POSITIVE_INFINITY
312+ if ( previewedChars <= MODEL_RUN_RESULT_MAX_CHARS ) return previewed
313+ // JSON escapes inside the preview grow it once it is embedded as a string.
314+ chars -= previewedChars - MODEL_RUN_RESULT_MAX_CHARS
285315 }
316+ return pointed
286317}
287318
288319/**
@@ -303,7 +334,7 @@ export function boundRunResultForModel(
303334) : Record < string , unknown > {
304335 const walksContent = copilotProjectionWalksContent ( registry )
305336 const capped = walksContent ? boundToProjectionCaps ( data , error , executionId ) : data
306- return fitRunResultToWindow ( capped , error , executionId , walksContent )
337+ return fitRunResultToWindow ( capped , error , executionId , registry , walksContent )
307338}
308339
309340function boundToProjectionCaps (
0 commit comments