Files
goose/crates
Michael Neale fb1119cc02 BRAINROT P2: per-field cap inside code_execution result bundles
The code_execution sandbox encourages the model to bundle multiple
sub-tool calls and return one labeled object like
'Result: { foo, bar, baz }'. Without per-field truncation, a single fat
field (full source file, build log, large query result) bloats the
whole bundle even when the other fields are tiny and useful.

Add truncate_execute_output() which intercepts pctx_code_mode's
ExecuteOutput just before output.markdown() is called and:

  - caps stdout and stderr if they exceed the per-field cap,
  - walks the  JSON value tree and truncates any leaf string
    longer than the cap with a marker.

Truncation is UTF-8 safe and recursive (nested objects + arrays). The
top-level P0 cap (16 KB on the whole tool_result text) still applies as
a backstop; this P2 cap (8 KB per field by default) operates earlier so
that one large field can't drown out the small useful ones in the same
bundle.

- Defaults: 8192 bytes per string field.
- Tunable: GOOSE_CODE_EXEC_FIELD_MAX_BYTES (0 to disable).
- Applied to both handle_execute_typescript and handle_execute_bash
  (execute_bash also wraps a bash invocation whose stdout/stderr can be
  large; same treatment).

Three serial unit tests cover (a) leaf-only truncation across object,
array, and nested object, (b) end-to-end ExecuteOutput cap, (c) env
disable.

Signed-off-by: Michael Neale <michael.neale@gmail.com>
2026-05-20 19:10:57 +10:00
..