Repository navigation
feat(core): expose structured truncation counts in shell metadata - #65565
angelnereira wants to merge 4 commits into
Conversation
There was a problem hiding this comment.
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.mdis 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 theoutputPaththatShellResult.outputcopies from the capture. One of the two can go.
| @@ -0,0 +1,18 @@ | |||
| # feat(core): expose structured truncation counts in shell metadata | |||
There was a problem hiding this comment.
This is a draft of the PR description and doesn't belong in the repository. Please remove the file.
| 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: |
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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.
|
Thanks for updating your PR! It now meets our contributing guidelines. 👍 |
There was a problem hiding this comment.
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.
| } | ||
|
|
||
| export function metadata(output: Output) { | ||
| const { output: _output, truncated, exit, signal, timeout, killed, ...truncation } = output |
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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.
Issue for this PR
Closes #65018
Type of change
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, andendLineas additive optional metadata. No extra I/O, no new dependencies.outputPathwas 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.tschecks 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
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)