@@ -19,8 +19,8 @@ function presentWorkflowLogs(logs: unknown, select?: string[]): Record<string, u
1919 * resolves against the full logs and replaces them. Otherwise, when `registry` makes the projection
2020 * walk the result, long echoed inputs get a marker that keeps nothing of the raw input. Without an
2121 * active secret the logs cross as they always have: the server handler previews long inputs
22- * (`previewLongInputs`) and the browser-run path leaves them untouched. Outputs are bounded by
23- * { @link boundRunResultForModel}, only when the whole result would pass a projection cap .
22+ * (`previewLongInputs`) and the browser-run path leaves them untouched. Outputs, and any input
23+ * still too long for the agent's window, are bounded by { @link boundRunResultForModel} .
2424 */
2525export function presentWorkflowLogsForModel (
2626 logs : unknown ,
@@ -195,20 +195,122 @@ function compactBlockLogOutputs(logs: unknown, executionId: string | undefined):
195195}
196196
197197/**
198- * Bounds a run's whole model-facing result before secret projection. When `registry` makes the
199- * projection walk the result, one past any of its caps is withheld entirely. So a result that
200- * would pass a cap first has its bulkiest block-log outputs replaced with pointers, down to their
201- * budget, and if it still would, its final output too. The result is measured whole, envelope and
202- * error included, and a result that fits is returned untouched. A block output that run_block or
203- * run_workflow_until_block lifted into `output` is the run's final output here too.
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.
202+ */
203+ const 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+ const FINAL_OUTPUT_PREVIEW_CHARS = 4_000
206+
207+ /** `value` serialized the way the agent receives it; undefined for a value JSON cannot encode. */
208+ function serialize ( value : unknown ) : string | undefined {
209+ try {
210+ return JSON . stringify ( value ) ?? ''
211+ } catch {
212+ return undefined
213+ }
214+ }
215+
216+ /**
217+ * The head of an oversized final output and where the rest is. Only used when no secret is
218+ * active: a kept prefix could otherwise cut through a secret and leave a fragment no whole-literal
219+ * redaction matches.
220+ */
221+ function finalOutputPreview ( serialized : string , executionId : string | undefined ) : string {
222+ let end = FINAL_OUTPUT_PREVIEW_CHARS
223+ const last = serialized . charCodeAt ( end - 1 )
224+ if ( last >= 0xd800 && last <= 0xdbff ) end -= 1
225+ return `${ serialized . slice ( 0 , end ) } …[final output cut at ${ end } of ${ serialized . length } chars; inspect with logs get ${ executionId ?? '<executionId>' } --trace]`
226+ }
227+
228+ /**
229+ * Holds a run result to {@link MODEL_RUN_RESULT_MAX_CHARS} so the agent reads pass/fail, the
230+ * failing block's error, and each block's status inline. Echoed block inputs and outputs are
231+ * 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.
234+ */
235+ function fitRunResultToWindow (
236+ data : Record < string , unknown > ,
237+ error : string | undefined ,
238+ executionId : string | undefined ,
239+ walksContent : boolean
240+ ) : 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
243+
244+ let fitted = data
245+ if ( Array . isArray ( data . logs ) ) {
246+ const parts : Array < { index : number ; field : 'input' | 'output' ; size : number } > = [ ]
247+ for ( const [ index , entry ] of data . logs . entries ( ) ) {
248+ if ( ! isPlainRecord ( entry ) ) continue
249+ for ( const field of [ 'input' , 'output' ] as const ) {
250+ if ( entry [ field ] === undefined ) continue
251+ const size = serialize ( entry [ field ] ) ?. length
252+ if ( size !== undefined ) parts . push ( { index, field, size } )
253+ }
254+ }
255+ parts . sort ( ( left , right ) => right . size - left . size )
256+ const logs = [ ...data . logs ]
257+ for ( const part of parts ) {
258+ if ( total <= MODEL_RUN_RESULT_MAX_CHARS ) break
259+ const entry = logs [ part . index ] as Record < string , unknown >
260+ const pointer =
261+ part . field === 'input'
262+ ? omittedInputMarker ( '' , executionId ?? '<executionId>' )
263+ : blockOutputPointer (
264+ sizeBlockOutput ( entry . output ) ?. label ?? 'output omitted' ,
265+ executionId
266+ )
267+ const saved = part . size - JSON . stringify ( pointer ) . length
268+ if ( saved <= 0 ) continue
269+ logs [ part . index ] = { ...entry , [ part . field ] : pointer }
270+ total -= saved
271+ }
272+ fitted = { ...data , logs }
273+ }
274+ if ( total <= MODEL_RUN_RESULT_MAX_CHARS || ! Object . hasOwn ( fitted , 'output' ) ) return fitted
275+
276+ const serialized = serialize ( fitted . output )
277+ if ( serialized === undefined || serialized . length <= FINAL_OUTPUT_PREVIEW_CHARS ) return fitted
278+ 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 ) ,
285+ }
286+ }
287+
288+ /**
289+ * Bounds a run's whole model-facing result before secret projection, in two steps. First, when
290+ * `registry` makes the projection walk the result, one past any of its caps is withheld entirely.
291+ * So a result that would pass a cap first has its bulkiest block-log outputs replaced with
292+ * pointers, down to their budget, and if it still would, its final output too. The result is
293+ * measured whole, envelope and error included. A block output that run_block or
294+ * run_workflow_until_block lifted into `output` is the run's final output here too. Then every
295+ * result, secret or not, is held to the agent's inline window ({@link fitRunResultToWindow}). A
296+ * result that fits both is returned untouched.
204297 */
205298export function boundRunResultForModel (
206299 data : Record < string , unknown > ,
207300 error : string | undefined ,
208301 executionId : string | undefined ,
209302 registry : ResolvedSecretTraceRegistry | undefined
210303) : Record < string , unknown > {
211- if ( ! copilotProjectionWalksContent ( registry ) ) return data
304+ const walksContent = copilotProjectionWalksContent ( registry )
305+ const capped = walksContent ? boundToProjectionCaps ( data , error , executionId ) : data
306+ return fitRunResultToWindow ( capped , error , executionId , walksContent )
307+ }
308+
309+ function boundToProjectionCaps (
310+ data : Record < string , unknown > ,
311+ error : string | undefined ,
312+ executionId : string | undefined
313+ ) : Record < string , unknown > {
212314 // A result JSON cannot encode is refused whatever its size, so only one past a cap is bounded.
213315 const passesCap = ( candidate : Record < string , unknown > ) : boolean =>
214316 measureModelContent ( error === undefined ? { output : candidate } : { output : candidate , error } )
0 commit comments