Skip to content

feat(core): expose structured truncation counts in shell metadata - #65565

Open
angelnereira wants to merge 4 commits into
anomalyco:devfrom
angelnereira:contrib-shell
Open

angelnereira wants to merge 4 commits into
anomalyco:devfrom
angelnereira:contrib-shell

Conversation

@angelnereira

@angelnereira angelnereira commented Nov 22, 2026 •

Copy link
Copy Markdown

Issue for this PR

Closes #65018

Type of change

  • Bug fix
  • New feature
  • Refactor / code improvement
  • Documentation

What does this PR do?

The shell tool already computed truncation sizes and the output file path but discarded them. This exposes outputPath, totalBytes, shownBytes, totalLines, startLine, and endLine as additive optional metadata. No extra I/O, no new dependencies. outputPath was already visible in HEAD, so no new information is disclosed.

How did you verify your code works?

A focused test in packages/core/test/tool-shell.test.ts checks the new metadata fields; it fails before the fix and passes after. File suite: 91 pass, 23 pre-existing skips, 0 fail. bun run check: 36/36.

Screenshots / recordings

Not a UI change.

Checklist

  • I have tested my changes locally
  • I have not included unrelated changes in this PR

Added by the opencode bot (AI-generated).

Public API: adds truncated, totalBytes, shownBytes, totalLines, startLine, endLine to the shell tool's structured output, and outputPath plus the counts to its metadata (backwards compatible)

@github-actions github-actions Bot added the needs:compliance This means the issue will auto-close after 2 hours. label Nov 22, 2026

@opencodejr opencodejr Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Works as described. I ran the shell tool tests on the base (the new test and four adjusted tests fail there) and with this PR (test/tool-shell.test.ts 91 pass / 0 fail, all test/shell* files and the session and automation shell tests pass), and tsc passes for packages/core.

Public API: additive. The shell tool's structured output (tools.opencode.shell(...) in Code Mode, the tool part's output in SDK, event and run --format json streams) gains truncated (now always present, including false), plus totalBytes, shownBytes, totalLines, startLine and endLine, set only when output is truncated. The tool metadata and the background <shell> notification metadata gain outputPath and the same counts. Existing callers keep working; only consumers that compare the whole object exactly would see the extra keys.

Concerns:

  • docs/kaizen/pr-drafts/feat-shell-truncation-metadata.md is a copy of the PR description and shouldn't be committed. Please drop it.
  • The same six-field list is now repeated in four places in shell/result.ts (comment inline).
  • In tool/plugin/shell.ts, the existing ...(result.capture.truncated ? { outputPath: result.info.file } : {}) in the background job's result now duplicates the outputPath that ShellResult.output copies from the capture. One of the two can go.

@@ -0,0 +1,18 @@
# feat(core): expose structured truncation counts in shell metadata

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

This is a draft of the PR description and doesn't belong in the repository. Please remove the file.

Comment thread packages/core/src/shell/result.ts Outdated
info: Shell.Info
// `killed` says why a command was killed when no exit code or signal of its own tells, e.g. the process daemon stopped.
capture: { output: string; truncated: boolean; killed?: string } | undefined
capture:

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

The six truncation fields are now listed four times: the capture type, Output, and the field-by-field spreads in output() and metadata(). A single Truncation type, with the capture's fields spread once, would stop the next field added here from being missed in one of those places.

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

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

Addressed: the six truncation fields now live in a single Truncation type, spread once into Result.capture and Output and forwarded via rest in output() and metadata(). Added refactor(core): share shell truncation fields via one type on contrib-shell; tool-shell test and bun run check still green.

@opencodejr opencodejr Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

The new commit removes the stray PR-draft file, so that point is addressed. No code changed since the last review, so the earlier test and type-check results still apply. Public API impact is unchanged from the last review and stays additive. The duplicated truncation field lists in shell/result.ts and the redundant outputPath spread in tool/plugin/shell.ts are still open.

@github-actions github-actions Bot removed the needs:compliance This means the issue will auto-close after 2 hours. label Nov 22, 2026
@github-actions

Copy link
Copy Markdown
Contributor

Thanks for updating your PR! It now meets our contributing guidelines. 👍

@opencodejr opencodejr Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

The duplicated field lists are fixed: Truncation is now a single type, and the rest-spread in output() is safe because capture holds only those fields.

The rest-spread in metadata() isn't safe, though (comment inline). Its callers pass more than Output's declared fields, so the tool metadata now also gets wallTimeSeconds, and the background <shell> notice metadata gets status and wallTimeSeconds. For a command that moved to the background, the streaming file's outputPath also ends up in the metadata, though truncation fields are meant to appear only when output is truncated. I confirmed this by calling ShellResult.metadata with a tool-shaped object. tool-shell.test.ts (91 pass) and tsc still pass, because the tests use toMatchObject.

Public API: unchanged from the last review apart from this leak. The shell tool's structured output gains truncated, totalBytes, shownBytes, totalLines, startLine and endLine, and its metadata gains outputPath and the counts (backwards compatible). The redundant outputPath spread in tool/plugin/shell.ts is still there.

Comment thread packages/core/src/shell/result.ts Outdated
}

export function metadata(output: Output) {
const { output: _output, truncated, exit, signal, timeout, killed, ...truncation } = output

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

The rest-spread here picks up every field the caller passes, not just the Truncation ones. toolResult passes an Output that also has status, wallTimeSeconds, shellID, pid and (when running in the background) outputPath, and the notification path passes the job's output with status and wallTimeSeconds. All of these now leak into the tool and <shell> notice metadata: ShellResult.metadata({ output: "x", truncated: false, exit: 0, status: "completed", wallTimeSeconds: 1.2, shellID: "sh_1", pid: 42, outputPath: "/tmp/f" }) returns all of them. Please pick the truncation fields explicitly here (e.g. a small truncation(output) helper that lists the Truncation keys) so the metadata stays as narrow as before.

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

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

Fixed both findings: metadata() now uses an explicit truncation(output) helper so only the six Truncation keys are forwarded, and the redundant outputPath spread in tool/plugin/shell.ts is gone. New fail-first test in tool-shell.test.ts rejects wider Output objects; 92 pass / 23 skip / 0 fail, bun run check 36/36.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Thanks, I checked both fixes at b6cfc4beb. With the wider tool output, metadata() now returns only truncated, exit and the truncation keys. The duplicate outputPath spread is gone, and the new test covers the narrow metadata. Nothing further from me on this thread.

@opencodejr opencodejr Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

The earlier points are all addressed. The draft file is gone, the truncation fields share one type, and metadata() now picks only the truncation keys through truncation(), so status, wallTimeSeconds, shellID and pid no longer leak (I checked this by calling it with a tool-shaped object). The redundant outputPath spread in tool/plugin/shell.ts is also removed. tool-shell.test.ts and shell-result.test.ts pass (97 pass), and tsc passes for packages/core. No new concerns.

Public API: additive and backwards compatible. The shell tool's structured output gains truncated (always present), plus totalBytes, shownBytes, totalLines, startLine and endLine when output is truncated. The tool and <shell> notice metadata gain outputPath and the same counts.

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.

feat(core): expose structured truncation counts and output file path in shell tool metadata

1 participant