Repository navigation
feat(codemode): show file diffs from a script below the execute block - #65002
Conversation
There was a problem hiding this comment.
Core works as described, but I could not see the new UI rendering. I ran a script through CodeModeTool directly that wrote app.ts twice, created notes.md, deleted old.txt, flushed a writer and copied a file. metadata.files held one merged diff for app.ts, added/deleted entries for the others and a counts-only entry for the copy. bun test test/codemode/ passes, and typecheck:tsc passes in core, tui and session-ui. I couldn't get a simulated execute call to run in opencode-drive, so I have no before/after TUI screenshots. Could you attach a before/after screenshot of the TUI execute block here? The linked artifacts page works too.
Public API: no SDK, HTTP API, plugin, config or CLI change. execute gains an optional metadata.files entry in the same shape edit and patch already use, which is backwards compatible.
Concerns (inline):
- Large files: every Bun write, append and writer flush now reads the whole file twice, and the collector keeps full text in memory until the run ends. The size cap only applies at the end, so a large file being appended to gets slow and memory-heavy.
- TUI "-0 lines": counts-only binary or copied files show as "-0 lines" in the TUI.
Also, packages/tui/src/routes/session/index.tsx has several reformat-only hunks unrelated to the feature. They could be dropped to keep the diff focused.
| ? fileDiff(target.resource, before, after, type === undefined ? "added" : "modified") | ||
| : undefined, | ||
| ) | ||
| // The run's own record of the file reads it whole: the request above may show only an append. |
There was a problem hiding this comment.
Every put now reads the whole file before and after the write, including appends and each writer flush. The collector also keeps both full texts in memory for every file until the run ends. MAX_PATCH_BYTES only applies in summarize, so it doesn't bound this. A script that builds a large log through Bun.file(...).writer() re-reads the growing file on every flush, so reads grow roughly quadratically.
Could the collector read before only the first time it sees a key, and read the final content once when the run ends instead of on every write? Files above a size threshold could keep counts only, or be treated like binary. around in diffs.ts has the same pattern for inner edit and patch calls.
There was a problem hiding this comment.
Fixed in e90c793. The collector doesn't read files anymore: CodeModeDiffs.read is gone, along with the before/after reads in put, remove and around.
- Inner edit and patch: each change comes from the tool result's own
metadata.files. - Inner write: a created file counts its input content; an overwrite is recorded without counts.
Bun.write: reuses the diff the permission request already builds. An append counts the lines it adds.- Delete: recorded as
deleted, without counts.
There's one entry per change, with no merging per file. Only patch text is kept, and MAX_PATCH_BYTES and MAX_FILES are applied as each change arrives. A test in tool-execute.test.ts checks that the only file reads during a script are the ones the existing permission path already made before this PR.
There was a problem hiding this comment.
Yes, this fixes what I raised. In pr/head (e90c793), nothing in diffs.ts reads files anymore, and the before/after reads in put, remove and around are gone. The one read left in put (bun.ts ~L231) is the old one that builds the permission preview for a full overwrite. Appends and writer flushes skip it (options.append), so the quadratic re-read on a growing writer is gone. Memory is now bounded by MAX_PATCH_BYTES as changes arrive.
I ran bun test test/codemode/diffs.test.ts test/tool-execute.test.ts on pr/head in a temporary worktree, which I've since removed: 42 pass. The read-tracking test confirms the only reads are ["notes.txt", "notes.txt"] from the permission path.
Two things the new model introduces that may matter:
MAX_FILESnow caps changes, not files. Every writer flush (bun.ts~L314) and every write in a loop takes one of the 50 slots. A script that streams a log throughBun.file(...).writer()with 50+ flushes, then editssrc/app.ts, would drop the edit from the list. Possible fixes: fold consecutive appends to the same file into one entry (sum the counts, no file reads needed), or count distinct files against the cap.- Inner
writedepends on the tool's output text.changesmatches/^(Created|Wrote) file successfully: (.+)$/againstwrite.ts:64's output to get the status and path. If that string ever changes, write calls stop showing with no error. Havingwritereturnmetadata.files(or at leastexisted/resource) like edit and patch would be sturdier.
I didn't change anything in repo/. The bun.lock modification there was already present and I left it alone.
| part={props.part} | ||
| > | ||
| <Show | ||
| when={file.type !== "delete" && file.patch} |
There was a problem hiding this comment.
Binary writes and copies come through with no patch and additions: 0, deletions: 0. This fallback then renders e.g. # Created copy.txt followed by -0 lines, which reads as if nothing happened. Could it show nothing, or something like "binary", when both counts are 0?
There was a problem hiding this comment.
Fixed in e90c793. A change without counts (a copy, or binary) now shows only its path. Where there are counts, only the non-zero ones show, except that a delete keeps its "-N lines".
There was a problem hiding this comment.
The rendering fix is right. In PatchedFiles (packages/tui/src/routes/session/index.tsx, around line 3776):
- A change with no counts now shows only its path, because the outer
<Show>checks foradditions/deletions. +Nshows only when N > 0.-N linesshows when N > 0, or when the change is a delete.
parseScriptChanges leaves missing counts as undefined instead of setting them to 0. Because of that, a copy, a binary write or a Bun remove (now recorded with no counts) shows just # Created x or # Deleted x. The desktop ScriptChanges fallback in tool-renderer.tsx hides DiffChanges in the same case, so the TUI and desktop agree.
One small thing remains: deleting an empty file that does have counts shows -0 lines. That's accurate, so I'd leave it.
Something else in the same commit: it also re-wraps several unrelated parts of that file (the @opentui/core import, the findLast in the copy command, the Read range and a few JSX <text>s). The result no longer passes the repo's Prettier config (printWidth: 120): prettier --check fails on pr/head's version of the file. Running bunx prettier --write packages/tui/src/routes/session/index.tsx should undo that churn.
I checked this by reading the commit diff and running Prettier on a temporary copy, which I've since deleted. I didn't run the TUI or the typecheck, and I changed nothing in the checkout; bun.lock was already modified before I started.
There was a problem hiding this comment.
The new commit moves a script's changed files outside a folded group, so they stay visible when it is closed. In the TUI the top-level group view shows them; on desktop CurrentContextToolGroup and the timeline-next ContextGroup do. Each place that hides the files inside an execute block also shows them under the group, so nothing goes missing. typecheck:tsc passes in session-ui and tui. I still couldn't run a simulated execute call in opencode-drive, so I haven't seen the rendering. Could you add a screenshot of a grouped script's diffs, open and folded?
Neither earlier point is addressed yet. Core is unchanged, so every Bun write and flush still reads the whole file twice. The TUI fallback still shows "-0 lines" for counts-only files. No new concerns.
Public API: unchanged by this commit. Overall the change still only adds an optional metadata.files to execute, which is backwards compatible.
There was a problem hiding this comment.
The new commit gives a script's changed files their own patch row in the desktop timelines, so it follows the Edits timeline setting. I checked it by reading the projections and running the session-ui tests: src/timeline, src/timeline-next and the new script-files test all pass, as does typecheck:tsc. I haven't seen it rendered, so a screenshot of the desktop timeline with Edits both collapsed and expanded would help.
The earlier points are not addressed yet, because core and the TUI are unchanged. Bun writes and flushes still read the whole file twice, and the TUI shows "-0 lines" for counts-only files. No new concerns.
Public API: unchanged by this commit. Overall the change only adds an optional metadata.files to execute, which is backwards compatible.
There was a problem hiding this comment.
This commit is a lint fix in the session-ui script diff helper and its test, with no behaviour change. The script-files test and typecheck:tsc pass in session-ui. The earlier points are still open, since core and the TUI are untouched. Public API unchanged by this commit.
There was a problem hiding this comment.
The new commit makes the desktop grouped-tools summary count a script once, by leaving out the patch row added after it. The fix looks right. The script-files tests and typecheck:tsc pass in session-ui. I didn't see it rendered. The earlier points are still open, since core and the TUI are unchanged. Public API unchanged by this commit.
One small note on the new test, inline.
| expect(withScriptFileChanges(content)[1]).toBe(withScriptFileChanges(content)[1]) | ||
| }) | ||
|
|
||
| test("a Compact group summary counts the script once: Used 1 Execute", () => { |
There was a problem hiding this comment.
This test doesn't render the group or call CurrentContextToolGroup. It rebuilds the "Used 1 Execute" text by hand from the English strings, so the label part checks the test's own code. expect(summaryCalls(parts)).toHaveLength(1), maybe with isScriptFiles(parts[1]!), covers the same behaviour without the i18n setup.
There was a problem hiding this comment.
Obsolete: script-files.ts and its test were removed when the desktop side was rebuilt on timeline facts.
There was a problem hiding this comment.
You're right. On pr/head, script-files.ts and script-files.test.ts are both gone, and nothing under packages/ still references summaryCalls, isScriptFiles or withScriptFileChanges. My earlier comment no longer applies, so this thread can be resolved. I didn't change anything in the checkout.
There was a problem hiding this comment.
The new commit makes a group that holds only a hidden script's files count them as one patch in its summary. The change looks right, and the script-files tests and typecheck:tsc pass in session-ui. The earlier points are still open: core and the TUI are unchanged, and the label test still builds its string from the English strings. Public API unchanged by this commit.
60dc66e to
9af60c4
Compare
There was a problem hiding this comment.
The desktop rebuild looks sound. Script files are now real file entries that follow the Edits setting in both timelines, and the extra patch part and its special counting are gone, which is a cleaner design. bun test src/timeline src/timeline-next (176 tests, including the new script-labels.test.ts) and typecheck:tsc pass in session-ui. I haven't seen it rendered on desktop.
The label-test point is resolved, since that test was removed. The core read/memory point and the TUI "-0 lines" point are still open, as core and the TUI are unchanged. Public API: unchanged by this commit.
|
Update in e90c793:
|
There was a problem hiding this comment.
The new commit addresses both remaining points. Core no longer reads files to record a script's diffs: it reuses the permission diff, counts appended lines, and uses inner edit/patch results. A new test shows the only reads left are the two the permission diff already made. The TUI no longer prints "-0 lines" for changes without counts. Core tests (72) and session-ui timeline tests (177) pass, and typecheck:tsc passes in core, tui and session-ui.
Two new points, inline: diff recording now runs without full_codemode, and write calls are detected by matching the write tool's output text. Public API: unchanged by this commit; overall it only adds an optional metadata.files to execute, which is backwards compatible.
| }).pipe( | ||
| Effect.mapError((error) => new ToolFailure({ message: toSessionError(error).message, error })), | ||
| ) | ||
| const executed = yield* CodeModeDiffs.around(diffs, tool.name, input, call) |
There was a problem hiding this comment.
This used to be gated on services. Now CodeModeDiffs.around wraps every inner call, so without full_codemode an execute call's inner edit/patch results also fill metadata.files. The script diffs would then show in regular Code Mode too, while the description says the feature is behind full_codemode. If that isn't intended, keep the services ? … : call guard.
| if (tool !== "write") return [] | ||
| const args = Option.getOrUndefined(decodeWrite(input)) | ||
| const match = | ||
| typeof result.output === "string" ? /^(Created|Wrote) file successfully: (.+)$/.exec(result.output) : null |
There was a problem hiding this comment.
This finds a write's file and whether it existed by matching the write tool's output text (Created|Wrote file successfully: …). If that wording changes, script writes silently stop showing, and nothing ties the two together. The write tool could return metadata.files (or resource/existed) the way edit and patch do, so this reads structured data instead.
e90c793 to
8a9e750
Compare
There was a problem hiding this comment.
This push rebases onto the updated timeline-place and adds a blank line. The desktop placement of script changes is adapted to the new placer, and the reactivity test now checks that a finished script rebuilds once. Session-ui timeline tests (191), core codemode tests (72) and typecheck:tsc in session-ui pass. Core and the TUI are unchanged, so the full_codemode gating and write-output matching points are still open. Public API unchanged by this push.
4c0213c to
77c0c86
Compare
8a9e750 to
bf6bb5a
Compare
There was a problem hiding this comment.
This push is a rebase onto the updated timeline-place, and the PR's own changes are unchanged. Session-ui timeline tests (192) and typecheck:tsc pass. The full_codemode gating and write-output matching points are still open. Public API unchanged by this push.
77c0c86 to
12952c8
Compare
bf6bb5a to
3e9505d
Compare
There was a problem hiding this comment.
This push rebases onto dev, and the PR's own commits are unchanged. Against dev it still carries the stacked timeline commits: one already landed as #65025, the other is from #65012. The description still names timeline-place as the base. Session-ui timeline tests (192), core codemode tests (72) and typecheck:tsc in session-ui pass. The full_codemode gating and write-output matching points are still open. Public API unchanged by this push.
User decisions: - Show file diffs from a Code Mode script below the execute block - Merge several edits to the same file into one diff - Cap the stored diff size - Don't show diffs for backgrounded scripts - No images on desktop
… expanding it User decisions: - Show the script's diffs under the execute block without expanding it
User decisions: - Script diffs follow the Edits timeline setting like direct edits
User decisions: - Script diffs follow the Edits timeline setting like direct edits - Everything else matches production - Both timelines use it
User decisions: - Don't read files ourselves to build a script's diffs - One diff card per change for now, no merging per file
User decisions: - Script diffs follow the Edits timeline setting like direct edits
3e9505d to
8d8d8f4
Compare
There was a problem hiding this comment.
This push rebases onto dev now that the stacked timeline PRs have merged. The PR's own commits are unchanged, and the stacked commits are no longer on the branch. Session-ui timeline tests (192), core codemode tests (72) and typecheck:tsc in session-ui pass. The full_codemode gating and write-output matching points are still open. Public API unchanged by this push.
User decisions: - Fix all review findings on script diffs
User decisions: - Fix all review findings on script diffs
There was a problem hiding this comment.
The new commits look sound. A new integration test runs the real write, edit and patch tools from a script and pins their result shape and write wording, so the write-matching concern is covered. Core no longer records no-op overwrites, ACP no longer rebuilds script changes from disk, and the TUI and desktop show counts only where they're known. I ran the core (75), session-ui (196), tui (3) and cli ACP (1) tests, all passing, and typecheck:tsc passes in core, session-ui and tui. The full_codemode gating point is still open. Public API: unchanged; overall the change only adds an optional metadata.files to execute, which is backwards compatible.
User decisions: - Keep a script's changes spaced like direct patches
There was a problem hiding this comment.
This commit spaces a script's diff cards the way a direct patch's files are spaced, and gives a later run of direct edits in a file group its own open/closed state. Groups without a script keep their current behaviour. Both changes look right. Session-ui timeline and file-runs tests (197) and typecheck:tsc pass; I didn't see it rendered. The full_codemode gating point is still open. Public API unchanged by this commit.
Issue for this PR
Closes #
Type of change
What does this PR do?
Targets
devdirectly. It builds on #65012 and #65025, which are both merged.Desktop (rebuilt): a finished foreground
executenow gives one file fact per changed file.placeputs those files under the Edits setting, the same way it does a direct patch's files, in both timelines. The cards use the existing patch renderer; files without a patch show their counts. The pretendpatchpart,script-files.tsand thesummaryCallsspecial case are removed. Core and TUI are unchanged from the earlier version of this PR.Earlier description below (its desktop part is replaced by the paragraph above):
Behind
full_codemode. Files a Code Modeexecutescript changes now show as diffs below the execute block, in the desktop app and the TUI.codemode/diffs.ts) keeps each file's content before its first change and after its last. Inneredit/write/patchcalls are wrapped: the files they name are read before and after the call. Bun'sput(Bun.write,BunFile.write, writer flushes) anddeleterecord too; copies and binary writes are counts-only. At the end, onefileDiffper file goes onmetadata.files(added / deleted / modified).{ file, status, additions, deletions }withoutpatch.PatchedFiles, which Execute reuses below its rows. A file without a patch shows its counts. Mini mode is unchanged.How did you verify your code works?
bun test test/codemode/diffs.test.ts(core): merging, added/deleted/binary, cap, path parsing.typecheck:tscin core, session-ui, tui; prettier and oxlint on touched files.src/app.tstwice,Bun.writesnotes.mdand deletesold.txt. Both the desktop app and the TUI show one merged diff for app.ts, plus Created notes.md and Deleted old.txt.Screenshots / recordings
Desktop and TUI screenshots and videos: https://jamesbox.exe.xyz/artifacts/drive-codemode/#script-diffs
Checklist