Skip to content

Unify tool output views - #880

Merged
kevincodex1 merged 3 commits into
mainfrom
perf/unified-tool-outcomes
Aug 10, 2026
Merged

Unify tool output views#880
kevincodex1 merged 3 commits into
mainfrom
perf/unified-tool-outcomes

Conversation

@anandh8x

@anandh8x anandh8x commented Aug 8, 2026

Copy link
Copy Markdown
Collaborator

What changed

  • introduce a finalized ToolOutcome that separates model-facing output, human-facing detail, recoverable artifacts, and reduction diagnostics
  • finalize outcomes once at the registry boundary after redaction, command reduction, and output budgeting
  • route agent, TUI, and exec surfaces through the appropriate outcome view while preserving compatibility for direct tool callers and persisted results
  • preserve detailed command evidence for users, including reduced error output, without returning that full output to the model context
  • add regression coverage for view separation, redaction, artifacts, after-hook rebudgeting, reduced errors, and corpus-level token behavior

Why

Tool output previously relied on several loosely related fields and metadata conventions. This made it easy for model context, user-visible detail, and recoverable raw output to drift or accidentally use the wrong representation.

The unified boundary keeps model context compact while retaining useful detailed output for users and exact redacted artifacts for recovery and inspection.

Measured behavior

  • reduction corpus: 513 estimated model tokens before, 495 after
  • retained reduction: approximately 65% before and 67% after
  • manual interactive command: approximately 92 tokens reduced to 41 while retaining an expandable 22-line result

The corpus boundaries differ slightly, so the primary claim is preservation of the existing token reduction with improved human detail and recoverability, rather than a standalone 2-point performance gain.

Validation

  • make fmt-check
  • go vet ./...
  • focused internal/tools, internal/agent, internal/tui, and internal/cli tests
  • go run ./cmd/zero-release build
  • go run ./cmd/zero-release smoke
  • make lint-static
  • make vulncheck
  • git diff HEAD --check
  • manual interactive verification of compact model output and expandable detailed output

The full local suite also exercised all affected packages successfully. Existing environment/baseline failures remain in doctor connectivity, worktree-trust end-to-end, and daemon end-to-end tests; this change does not touch those paths.

Summary by CodeRabbit

  • New Features

    • Improved tool-result presentation with distinct model output, human-readable previews, and recoverable artifacts.
    • Added output diagnostics covering truncation, retained content size, and artifact details.
    • Added bounded, UTF-8-safe previews with references to complete output.
    • Preserved finalized tool-result views across saved and restored sessions.
  • Bug Fixes

    • Preserved useful execution details across CLI, streaming, transcript, and TUI views.
    • Improved error displays with relevant command evidence and fewer misleading success previews.
    • Applied consistent sensitive-content redaction across displayed and stored output.

@coderabbitai

coderabbitai Bot commented Aug 8, 2026

Copy link
Copy Markdown

Review Change Stack

Walkthrough

Tool results now expose finalized model, human, artifact, and diagnostic views. Execution paths finalize outcomes after budgeting and rebudgeting. Agent, CLI, and TUI consumers use the appropriate derived view.

Changes

Tool outcome finalization

Layer / File(s) Summary
Outcome contract and finalization
internal/tools/types.go, internal/tools/tool_outcome.go
Adds structured outcome types and accessors. Finalization records model and human views, artifacts, redaction, truncation, and output diagnostics.
Execution and rebudget finalization
internal/tools/registry.go, internal/tools/output_boundary.go, internal/tools/tool_outcome_test.go
Registry execution and post-hook rebudgeting finalize processed results after output reduction and budget enforcement. Tests cover artifacts, redaction, fallbacks, diagnostics, corpus reduction, and errors.
Agent, CLI, and TUI consumption
internal/agent/..., internal/cli/exec_writer.go, internal/tui/...
Agent transcripts, CLI output, and TUI rendering use model-facing and human-facing accessors. Result conversion preserves structured outcomes. Regression coverage verifies human-readable error output.

Estimated code review effort: 3 (Moderate) | ~30 minutes

Sequence Diagram(s)

sequenceDiagram
  participant ToolRegistry
  participant OutcomeFinalizer
  participant AgentLoop
  participant CLIWriter
  participant TUI
  ToolRegistry->>OutcomeFinalizer: processed output and boundary output
  OutcomeFinalizer-->>ToolRegistry: finalized ToolOutcome
  ToolRegistry->>AgentLoop: finalized tool result
  AgentLoop->>AgentLoop: select ModelOutput
  AgentLoop->>CLIWriter: model and human views
  AgentLoop->>TUI: finalized tool result
  TUI->>TUI: select HumanDisplay or ModelOutput
Loading

Possibly related PRs

  • Gitlawb/zero#838: Modifies tool-result output-budget handling in the same execution and rebudgeting paths.
  • Gitlawb/zero#846: Modifies tool-output reduction used by finalized ToolOutcome views.
  • Gitlawb/zero#873: Modifies tool-result propagation in internal/agent/loop.go.

Suggested reviewers: kevincodex1, gnam...

🚥 Pre-merge checks | ✅ 4 | ❌ 1

❌ Failed checks (1 warning)

Check name Status Explanation Resolution
Docstring Coverage ⚠️ Warning Docstring coverage is 18.18% which is insufficient. The required threshold is 80.00%. Write docstrings for the functions missing them to satisfy the coverage threshold.
✅ Passed checks (4 passed)
Check name Status Explanation
Linked Issues check ✅ Passed Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check ✅ Passed Check skipped because no linked issues were found for this pull request.
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check ✅ Passed The title clearly and concisely describes the main change: separating and unifying tool output views across the system.
✨ Finishing Touches 💡 1
📝 Generate docstrings 💡
  • Create stacked PR
  • Commit on current branch
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Commit unit tests in branch perf/unified-tool-outcomes

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands.

@coderabbitai coderabbitai Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Actionable comments posted: 1

🤖 Prompt for all review comments with AI agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

Inline comments:
In `@internal/tools/types.go`:
- Around line 139-154: Persist the finalized state of ToolOutcome across JSON
serialization by exposing a serialized marker or implementing custom
marshaling/unmarshaling for finalized. Ensure restored outcomes make Finalized()
return true so HumanDisplay(), ModelOutput(), and toolResultDetail() retain
reduced error evidence; add a JSON round-trip regression covering a reduced
error outcome.
🪄 Autofix

Fix all unresolved CodeRabbit comments on this PR:

  • Push a commit to this branch (recommended)
  • Create a new PR with the fixes

ℹ️ Review info
⚙️ Run configuration

Configuration used: Path: .coderabbit.yaml

Review profile: CHILL

Plan: Pro Plus

Run ID: 5ab240c0-d033-48f8-b39f-39a06d4a229c

📥 Commits

Reviewing files that changed from the base of the PR and between 7f39a63 and abc0b90.

📒 Files selected for processing (10)
  • internal/agent/loop.go
  • internal/agent/types.go
  • internal/cli/exec_writer.go
  • internal/tools/output_boundary.go
  • internal/tools/registry.go
  • internal/tools/tool_outcome.go
  • internal/tools/tool_outcome_test.go
  • internal/tools/types.go
  • internal/tui/model.go
  • internal/tui/tool_outcome_test.go

Comment thread internal/tools/types.go
@github-actions

github-actions Bot commented Aug 8, 2026

Copy link
Copy Markdown
Contributor

Zero automated PR review

Verdict: No blockers found

Blockers

  • None found.

Validation

  • [pass] Diff hygiene: git diff --check
  • [pass] Tests: go test ./...
  • [pass] Build: go run ./cmd/zero-release build
  • [pass] Smoke build: go run ./cmd/zero-release smoke

Scope

Head: 22aac8b0a89d
Changed files (10): internal/agent/loop.go, internal/agent/types.go, internal/cli/exec_writer.go, internal/tools/output_boundary.go, internal/tools/registry.go, internal/tools/tool_outcome.go, internal/tools/tool_outcome_test.go, internal/tools/types.go, internal/tui/model.go, internal/tui/tool_outcome_test.go

This deterministic review checks validation status and basic diff hygiene. A human reviewer still owns product judgment and design quality.

@coderabbitai coderabbitai Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Actionable comments posted: 1

🤖 Prompt for all review comments with AI agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

Inline comments:
In `@internal/tui/tool_outcome_test.go`:
- Around line 52-54: Update TestToolResultDetailSurvivesOutcomeJSONRoundTrip to
set TMP and TEMP to t.TempDir() in addition to TMPDIR, ensuring os.TempDir uses
the test-owned directory on Windows for exec_command output spills.
🪄 Autofix

Fix all unresolved CodeRabbit comments on this PR:

  • Push a commit to this branch (recommended)
  • Create a new PR with the fixes

ℹ️ Review info
⚙️ Run configuration

Configuration used: Path: .coderabbit.yaml

Review profile: CHILL

Plan: Pro Plus

Run ID: 887dc319-f869-40f9-81e8-f05b45d5f520

📥 Commits

Reviewing files that changed from the base of the PR and between abc0b90 and b405c0d.

📒 Files selected for processing (2)
  • internal/tools/tool_outcome.go
  • internal/tui/tool_outcome_test.go

Comment thread internal/tui/tool_outcome_test.go
@Gitlawb Gitlawb deleted a comment from coderabbitai Bot Aug 8, 2026

@Vasanthdev2004 Vasanthdev2004 left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Reviewed at 22aac8b0. No blocking findings. Traced the parts where a change like this usually goes wrong rather than reading it top to bottom.

The redaction precondition holds. finalizeToolOutcome documents that boundaryOutput must already be redacted, and the human preview is built from it, so if that were ever false this would put unredacted output in front of a person. Both call sites capture it immediately after scrubResultSecrets: registry.go:145-146 and output_boundary.go:101-102. The ordering is right in both, and it is the thing I would have blocked on.

No silent-empty path. ModelOutput() returns Outcome.ModelView only when finalized and falls back to result.Output otherwise, so a direct Tool.Run caller that never crosses the registry boundary still gets its output. That fallback is what makes the four substitutions in loop.go safe.

Nothing mutates output after the view is frozen. finalizeToolOutcome runs after reduction, budgeting and the ceiling, and the only thing after it in the deferred boundary is CommitFileObservation, which writes Meta keys and nothing else. Worth stating explicitly because the whole design rests on it: once ModelView is set, any later write to result.Output would be invisible to the model, and there is currently no such write.

Hook re-budget does not reset the accounting. RebudgetAfterHook carries previous.Diagnostics.OriginalBytes forward when the outcome was already finalized, so a hook that rewrites output cannot make the original look smaller than it was. Easy thing to get wrong, and it is handled.

Byte-based preview trimming is the right call. boundedHumanOutcomePreview splits head and tail with utf8Prefix/utf8Suffix. Line-based retention is what breaks on minified or single-line files, and this avoids it.

Two things, neither blocking:

human.Preview is cleared when the result is an error and output was not command-reduced. The comment explains it as stopping a success preview from hiding the failure, which is right. But a tool whose Preview IS the error detail loses that detail from the human view while it survives in Output. Intended?

HumanView Display carries json:"humanView,omitempty", and omitempty does nothing for a struct in encoding/json. Harmless, just misleading to the next reader.

One note for your own awareness rather than a review point: observeToolResult now takes ModelOutput(). That feeds the repeated-failure signature in the guardrails, which keys on output text. It is equivalent today because ModelView is assigned from result.Output at finalize, so nothing moves. It stops being equivalent the moment anything sets ModelView to something other than the final Output, and the failure would be silent rather than loud.

I read this rather than ran it, so treat the above as a code review and not a verification that the suite passes.

@gnanam1990 gnanam1990 left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Approving at 22aac8b0, re-confirmed as the live head before posting.

@Vasanthdev2004 already traced the redaction precondition, the ModelOutput() fallback, the frozen-view invariant, the hook re-budget, and the byte-based preview — and I agree with all of it. He was explicit that he read it rather than ran it, so everything below is the empirical and negative-space half.

The redaction chokepoint is real, and load-bearing under mutation

This is the property the whole design rests on, so I didn't want to take it from a code read. Two things I verified that a read can't:

The on-disk artifact is redacted, not just the in-memory views. TestRegistryOutcomeRedactsModelHumanAndArtifactViews plants a real ghp_-shaped token, and — the part that matters — os.ReadFiles the artifact off disk and asserts the secret is absent from the actual spill file, not only from ModelView/Preview. That is the right assertion for "exact redacted artifacts."

Breaking the scrub ordering is caught. I moved the boundaryOutput capture one line earlier, before scrubResultSecrets — the exact ordering bug the comment at registry.go:137-138 warns about. The human view then leaks the token and the test fails (human view leaked secret). So the chokepoint isn't just correct today, it's pinned against regression.

Negative space: the self-managed spill path, which the redaction test doesn't cover

The redaction test drives a registry-budgeted fake tool. The real risk sits one path over — bash and exec_command are self-budgeting, and applySelfManagedOutputBudget reuses an existing meta["spill_path"] (output_boundary.go:60-61, 70-71). If either tool wrote that spill during its own Run(), before the registry scrub, the artifact would point at a raw, unredacted file while the model and human views were clean. That is the leak this refactor could plausibly have introduced.

It doesn't, and here's why I'm confident:

  • bash's registry path (prepareBashOutput, directBudget=false) returns bounded capture and does not call spillBashStreams or set spill_path — it defers, exactly as its comment at bash.go:439-441 claims. The only pre-scrub spill (budgetBashCapture, bash.go:471) is the directBudget=true path, which is direct callers who bypass the registry and its scrub entirely — unchanged by this PR and never model-facing.
  • exec_command is the same shape: truncateExecOutputSpill (the spilling variant) is reachable only from execToolResultWithBudget:431, gated on directBudget. The registry path skips it and flows full output to the boundary.
  • Both exec_command.go and bash.go are untouched by this PR (git diff origin/main --stat on them is empty), so this spill timing is pre-existing. The refactor references the post-scrub spill the boundary already created; it doesn't move the write ahead of the scrub.

So the reuse at applySelfManagedOutputBudget only ever reuses a post-scrub spill on the model-facing path. No leak.

Empirical

  • go build ./... and GOOS=windows go build ./... clean; internal/tools, internal/agent, internal/tui suites all green.
  • The two internal/cli failures the PR calls pre-existing (TestRunDoctorFormatsRedactedProviderDiagnostics, TestRunDoctorConnectivityProbesProvider) fail identically on untouched origin/main, and the PR touches none of the doctor/connectivity files. Claim is accurate.
  • Model context is unchanged by construction: ModelView is assigned from result.Output at tool_outcome.go:86, and ModelOutput() falls back to result.Output when unfinalized, so what reaches the provider is byte-identical to before. Vasanth's point about observeToolResult now keying the failure-signature on ModelOutput() is the one thing to keep an eye on — equivalent only while ModelView == Output, which is true today.
  • Mutation-tested a second guard: gutting the error-preview clearing in finalizeToolOutcome fails TestErrorOutcomeUsesRawCommandEvidenceButDropsUnrelatedPreview, so the "a success preview must not hide the failure" behaviour Vasanth asked about is genuinely pinned.

Platform

Since this reworks the tool-result path on the agent's hot path, I ran the real binary through full feature runs — file writes, nested paths, sandbox refusal, --add-dir grants, control-character output, specialist child processes, exec_command:

macOS Linux Windows
22aac8b0 7/7 7/7 6/7

The single Windows failure is exec_command, and it is not from this PR — it's the #865 PowerShell-under-restricted-token regression (BCrypt.dll init, 0x8007045A) that this branch inherits from main, which fails it identically. Everything that exercises the reworked outcome path — the write and control-character scenarios especially — is green on all three.

Not blocking, already noted by Vasanth

The omitempty on the HumanView struct field is a no-op in encoding/json (harmless), and the human.Preview = "" on a non-reduced error drops detail for a tool whose Preview is the error — his "intended?" is worth a one-line answer in the thread, but the corpus test shows the reduced-error case keeps its evidence, which is the important one. Neither changes my verdict.

Clean, well-tested, and the security-critical seam holds where I tried to break it. Nice work.

@anandh8x
anandh8x requested a review from Vasanthdev2004 August 9, 2026 07:30

@Vasanthdev2004 Vasanthdev2004 left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Approving. Head is still 22aac8b0, the commit I traced in the review above, so that stands rather than needing a fresh pass: same code, all checks green, and zero commits behind main.

For the record, the parts I verified rather than assumed are unchanged: boundaryOutput is captured immediately after scrubResultSecrets at both call sites, so the "must already be redacted" precondition on finalizeToolOutcome genuinely holds; ModelOutput() falls back to result.Output when unfinalized so a direct Tool.Run caller is never handed an empty view; and nothing mutates output after the view is frozen, since CommitFileObservation writes Meta keys only.

My two non-blocking notes are still open, and neither should hold this up:

human.Preview is cleared when the result is an error and output was not command-reduced. Right for a success preview, but a tool whose Preview IS the error detail loses that from the human view while it survives in Output. Worth a look when you are next in there.

HumanView Display carries json:"humanView,omitempty", and omitempty does nothing for a struct in encoding/json. Harmless, just misleading to the next reader.

One thing I flagged for awareness rather than as a finding, repeated here so it does not get lost in the thread: observeToolResult now takes ModelOutput(), which feeds the repeated-failure signature in the guardrails. It is equivalent today because ModelView is assigned from result.Output at finalize. It stops being equivalent the moment anything sets ModelView to something other than the final Output, and that failure would be silent rather than loud.

@Vasanthdev2004

Copy link
Copy Markdown
Collaborator

@kevincodex1 this one is ready whenever you have a minute: approved, MERGEABLE/CLEAN, all nine checks green.

While checking the approved-but-unmerged pile I found the rest of it is not actually waiting on you, so it is worth writing down rather than leaving as "kevin has a backlog".

Six approved PRs are stuck on something neither you nor their authors can clear by deciding anything: #757, #759, #774, #848, #849 and #850. Each is approved, MERGEABLE, and green, but reports BLOCKED with only seven checks instead of nine. The two missing are CodeQL and Analyze (actions), which come from GitHub's default setup rather than a workflow file in the repo, so there is nothing on those branches to re-run. The checks are required, they never reported on those commits, and a required check that never reports blocks the merge indefinitely. #880 has all nine because it was pushed recently; the six oldest date from 19 July to 31 July.

A fresh push on each branch should make the analysis run and let them merge. I can rebase all six onto main and push, which is the natural way to do it and also picks up whatever they have drifted from. Say the word and I will, or tell me if you would rather the authors did it themselves.

Two of those are worth not losing to this: #774 cleans up a daemon child after a startup timeout and #759 moves LSP notifications off the read loop. Both have been sitting since 19 and 20 July on a check that was never going to arrive.

@Vasanthdev2004

Copy link
Copy Markdown
Collaborator

Correcting my comment above: the advice in it was wrong, and I tested it rather than leaving you to find out.

I said a fresh push would make the missing analysis run and let those six merge. It does not. I rebased #848 onto current main and pushed it as a single-PR test before touching the others. All seven checks re-ran green on a brand new commit, and it still reports only seven checks and still says BLOCKED. CodeQL and Analyze (actions) did not appear. So the rebase I offered would have rewritten six contributors' branches for no benefit, and I have not done the other five.

What I can state as verified rather than inferred:

So something in branch protection is refusing these that did not refuse #857 and #876, and I cannot see which rule from outside. My guess would be that CodeQL became a required check at some point after those merged, which would make every fork PR unmergeable from that day on, but that is a guess and I have been wrong about this three times today already, so I would rather you read the actual setting than act on my theory.

The question that would settle it: are CodeQL and Analyze (actions) in the required-status-checks list, and can a fork PR ever satisfy them? If the answer is no, the options are to drop them from required or move CodeQL to a workflow file that fork PRs can run. Either way it is a settings change rather than anything the six authors can do.

Still true from before: #880 is ready whenever you want it, and #774 and #759 have been sitting since 19 and 20 July.

@kevincodex1
kevincodex1 merged commit 160e3be into main Aug 10, 2026
9 checks passed
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.

4 participants