Skip to content

fix(agent): bound tool results before the model reads them - #1375

Open
LivXue wants to merge 2 commits into
mainfrom
fix/tool_result_truncation_replay
Open

LivXue wants to merge 2 commits into
mainfrom
fix/tool_result_truncation_replay

Conversation

@LivXue

@LivXue LivXue commented Nov 20, 2026 •

Copy link
Copy Markdown
Member

Summary

A tool result over the cap used to be cut only on the way to the
session log. The model kept the full result for the rest of the turn,
so the next turn's replay diverged at the cut and the provider
re-prefilled the whole turn.

The result is now bounded when it enters the conversation. The
bounded preview is what the session files and replays; the full body
is written to a managed file under the workspace and named in a
trusted hint. The preview stays inside the untrusted fence, keeps the
END marker and any trusted note, and uses opencode's budget: 2000
lines or 50 KiB of UTF-8 bytes, whichever is reached first. exec
keeps the tail of its output; every other tool keeps the head.

The managed file is created owner-only, pruned after seven days, and
the hint names only the reader tools the turn actually has. A write
failure still returns the bounded preview without a path, so a
side-file failure never fails the turn. The save-side character cap
remains as a fallback for a result no ingestion path bounded, and
the legacy toolResultMaxChars setting is still honoured as an extra
cap.

A pre-existing tool-output symlink is refused: the writer checks
the managed directory before and after mkdir, and refuses it unless
it resolves inside the workspace. A refused path falls back to the
bounded preview with no path.

Follows the cache-hit work in #1276 and #1294.

Type

  • Fix
  • Feature
  • Docs
  • CI / tooling
  • Refactor
  • Other

Verification

  • Relevant tests pass locally
  • Relevant lint / type checks pass locally
  • User-facing docs or screenshots are updated when needed

The user-facing docs did not change; the config docstrings describe
the new budgets and the managed file.

uv run --frozen --all-extras pytest -q -x \
  tests/test_tool_output_store.py tests/test_agent_loop_turn_record.py \
  tests/test_context_source_refs.py tests/test_security_trust.py \
  tests/test_context_compacting_defaults.py \
  tests/test_agent_loop_backend_dispatch.py \
  tests/test_agent_loop_run_emit.py tests/test_agent_loop_mid_turn_merge.py \
  tests/test_context_turn_scratch.py tests/test_context_runtime_context.py \
  tests/test_history_trimmer.py tests/test_transcript_mid_turn.py \
  tests/test_agent_loop_reply_room.py tests/test_rpc_session.py \
  tests/test_context_compacting_runtime.py \
  tests/test_context_compaction_pruner.py \
  tests/test_agent_loop_compacting_overflow.py \
  tests/test_agents_research_flow_report_shape.py
831 passed in 23.40s
uv run --frozen ruff check <changed files>          -> all checks passed
uv run --frozen ruff format --check <changed files> -> all files formatted
uv run --frozen ty check <changed core modules>     -> all checks passed
uv run --frozen lint-imports                        -> 10 kept, 0 broken
make check-commits check-large-files check-source-language -> exit 0

The new regression tests cover: the send/replay byte equality across
two turns, the managed file body, line/byte/character budgets, the
exec tail, idempotence, a tool result carrying its own marker, a
write failure, the seven-day prune, reader-tool selection in the
hint, atomic 0600 file creation with chmod disabled, and a symlinked
managed directory. The symlink tests cover an outside target (no
file, no mode change, no prune of an old file) and an inside target
(the refusal is unconditional); both fail at the previous head and
pass after the fix.

Risk

  • Security impact considered
  • Backward compatibility considered
  • Rollback path is clear for risky changes

Under-budget tool results are unchanged. Over-budget results
intentionally show the model a bounded preview plus a path hint
instead of the full body; the full body is in a 7-day managed file
and the model can read it with the file tools the turn has. That is
the trade that keeps this turn's sent bytes equal to the next turn's
replay. The full body now uses disk outside the session JSONL; the
preview remains the durable replay record. Files are created with
O_EXCL and mode 0600, the directory is 0700, and the body is
scrubbed before fencing. A symlinked managed directory cannot
redirect the write, the chmod or the prune outside the workspace. A
concurrent directory swap between the check and the open is not
closed by this change; that needs the dirfd/openat hardening.
Legacy toolResultMaxChars is still honoured as an extra cap, so old
configs load and keep their bound. Rollback is a revert; there is no
data or config migration, and managed files are temporary.

Related Issues

Follows the cache-hit work in #1276 and #1294. No issue is closed by
this PR.

A tool result over the cap was cut only on the way to the session log.
The model kept the full result for the rest of the turn, so the next
turn's replay diverged at the cut and the provider re-prefilled the
whole turn.

Bound the result when it enters the conversation. The preview is what
the session files and replays; the full body goes to a managed file
under the workspace, named by a trusted hint. The limit is opencode's
line/byte budget (2000 lines or 50 KiB, whichever is reached first),
with a tail preview for exec. The cut stays inside the untrusted fence
and keeps the END marker and any trusted note.

The save-side cap remains as a fallback for a result no ingestion path
bounded.
@LivXue
LivXue requested review from 0xKT and gloryfromca November 20, 2026 11:14

@gloryfromca gloryfromca left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Blocking: the managed-output writer can escape the workspace through a pre-existing symlink and change the target directory's permissions.

I reviewed the full diff and the surrounding result-ingestion, trust-fence, session replay, retrieval, and transcript paths. I also checked the commit history and compatibility of the legacy character cap, looked for weakened tests, and covered the applicable AGENTS.md, CONTEXT-MAP.md/runtime architecture constraints.

Verification: uv run pytest tests/test_tool_output_store.py tests/test_context_source_refs.py tests/test_agent_loop_turn_record.py tests/test_security_trust.py tests/test_agent_loop_backend_dispatch.py tests/test_rpc_session.py -x (370 passed). The first attempt did not execute tests because the fresh environment lacked raven_everos; after uv sync --all-packages, the unchanged command passed.

One blocking finding is inline.

Comment thread raven/session/tool_output.py Resolved
A pre-existing tool-output symlink made mkdir, chmod, os.open and
prune_tool_outputs act on its target outside the workspace. Refuse a
symlinked managed directory before mkdir, and re-check after mkdir that
it still resolves under the workspace before chmodding or writing.

A refused path keeps the existing fallback: the model gets the bounded
preview with no path, and prune is not reached. Add tests for an outside
target and an inside target; both fail at the previous head and pass
after the fix.
@LivXue
LivXue requested a review from gloryfromca November 20, 2026 12:02
@LivXue

LivXue commented Nov 20, 2026

Copy link
Copy Markdown
Member Author

@0xKT could you take a look for approval when you have a minute? The B1
symlink finding is fixed in the new push; the thread has the reproduction
and the test evidence.

@gloryfromca gloryfromca left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No blockers; this can merge as far as I am concerned.

The symlink escape reported on the prior revision is fixed and the thread is settled. I reviewed the new commit and rechecked the full diff, result-ingestion and managed-file callers, trust-fence and replay behavior, relevant history and backward compatibility, test changes for weakening, and the applicable AGENTS.md and CONTEXT-MAP.md/runtime architecture constraints. I found no new plain error worth raising.

Verification: uv run pytest tests/test_tool_output_store.py tests/test_context_source_refs.py tests/test_agent_loop_turn_record.py tests/test_security_trust.py tests/test_agent_loop_backend_dispatch.py tests/test_rpc_session.py -x (372 passed).

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants