Skip to content

Carry a file edit's old and new text to the Diff pane - #450

Merged
abhishekgahlot2 merged 1 commit into
useagenthq:mainfrom
coolestnick:fix/edit-diff-not-shown
Nov 14, 2026
Merged

abhishekgahlot2 merged 1 commit into
useagenthq:mainfrom
coolestnick:fix/edit-diff-not-shown

Conversation

@coolestnick

@coolestnick coolestnick commented Nov 14, 2026 •

Copy link
Copy Markdown
Contributor

Part of #448

On a thread that ran on the native runtime, the Diff pane shows "Patch content not recorded" for every file the agent edited, because the runtime strips an edit's text before the backend sees it. This is the backend half of the fix: when a runtime sends the string an edit replaced and the string it wrote, the backend carries them into the step the Diff pane reads.

By itself it changes nothing a user sees: the pinned runtime (c3a1dd480534) sends neither string. The other half is a small change to the runtime fork, which I cannot release. A tested patch for it is at the end, and the "after" picture below is a real run on a runtime built with that patch.

What changed

  • A completed change to one file carries the item's oldStr / newStr into its step as old_string / new_string, the keys the Diff pane already reads. A change that is running, stopped or failed carries nothing, and neither does one that names several files.
  • The text is kept whole or left out: the two strings together up to 16,384 bytes as sent, never cut, since a piece of a change reads as the whole change.
  • The ledger never holds the text. runtimeActivityProviderEvent takes the activity through the new ledgerRuntimeActivity, which leaves it out. Redaction can lengthen text, so in the ledger it could push a payload over the 32 KiB limit that replaces a payload whole, and nothing reads it there.
  • It is stored once. The step's copy of the activity and the bounded v2 record both leave it out. A failed change keeps diffStr in v2, as today.
  • The runtime's diffStr is not carried, on purpose. The redactor reads text as it was written, and a diff puts a marker in front of every line: a removed line that starts with p matches the -p<password> pattern and is rewritten, and a secret that spans several lines stops matching its value. The two strings are the file's own text and redact like any other tool input.
  • turn-projector.ts is untouched, and the frontend needed no source change.

Shown working

Two real OpenCode runs (OpenCode 2.2.5, google/gemini-3.7-flash through the gateway) with the same prompt: write greet.py, then edit it twice. Both ran on this branch's backend, on the local provider with a runner on my machine, in a sandbox image built from the repo's recipe (deploy/local-sandbox, arm64).

  • Before: the pinned runtime c3a1dd480534. The three file steps record {files, file_path} and the pane shows the note.
  • After: the same fork source plus the patch at the end, installed by the backend from a local manifest. The same three steps record new_string (the write) and old_string / new_string (the two edits), and the pane shows them. The step's copy of the activity and its v2 record hold no text, and none of the run's 42 ledger events does.
Before After
before after

The header's count (+12 -4 above 10 added rows) is the existing diffStat, which counts a trailing newline as a line. Not changed here.

What still shows the note, and why

  • Codex. Its item has only a diff (diffStr), not the two strings. Carrying a diff needs a redactor that understands diff lines first, for the reason above. Separate work. Not run: my stack is set up for OpenCode only.
  • The third engine. Its adapter in the runtime fills neither string (its diffStr is the tool's reply), so it needs a runtime change that fills them from its edit and write input. Not run, for the same reason.
  • An OpenCode tool that takes several edits or a patch. The adapter finds no single pair of strings in its input, only a diff, like Codex.
  • A change over 16 KiB. Left out whole on purpose, by the runtime patch and again by the backend: the rule command output already follows.
  • A step that names several files. The item has one text for several files, and it cannot be split honestly. The pane already declines those.
  • A change that failed or was stopped. Nothing was changed. A failed one keeps its error.
  • A file changed by a shell command. There is no file change item for it. Showing it needs the turn's diff from the workspace, which the runtime takes only when the workspace root is a git repository, and as far as I can tell ours is not.

Order

This can merge first: it does nothing until a runtime sends the text. In the other order, a backend without this change would keep the text, cut at the record's string limit, in the item's bounded v2 record, in the step and in the ledger.

Tests

  • backend/src/engines/runtime-v2-view.test.ts, seven new ones: the two strings reach the step once and the ledger event equals the one a change without text gets; a whole file written carries only the new string; diffStr is never carried in any of four shapes; a running, stopped, failed or several-file change carries nothing and a failed one keeps its error; the limit is whole or nothing and counts bytes; text that grows past the ledger limit when redacted cannot reach the ledger; a registered secret is redacted in the step, across lines too.
  • backend/test/runtime-file-change-text.test.ts drives the real turn projector against the database: the emitted step holds the strings and the stored ledger rows hold neither.
  • frontend/components/session-ui/file-diff-view.test.tsx pins the step shape the pane reads. It passes on main too, since the frontend did not need to change.

Checks

  • bun run typecheck at the root -> ok
  • cd backend && bun run test on my own database -> 3645 pass, 3 skip, 0 fail, then the Slack suite 119 pass, 0 fail
  • cd frontend && bun run test -> 2013 pass, 0 fail
  • cd frontend && bun run lint -> no new warnings
  • cd frontend && bun run build -> ok
  • bun test scripts/agent-docs.test.ts -> 5 pass

The runtime change

Two small parts, both in the fork:

  • The wire (apps/server/src/orchestration-v2/WireProjection.ts): the fork's item 11 (docs/operations/t3-upstream.md), applied to a file change. A finished change carries oldStr / newStr whole when together they are at most 16,384 bytes, and neither when they are longer; never diffStr; a change in any other state carries nothing; a failed one keeps its bounded diffStr as today. The detail read returns the same.
  • The OpenCode adapter (packages/provider-opencode/src/server/toolItems.ts): it calls any tool whose name contains edit, write or patch a file change, so an MCP tool named like one would have its content shown as a file's text under the tool's name. The two strings are now filled only when the input names a file, and exactly as given (a string of blank lines was dropped before, which made such an edit look like a whole file written).
Patch on c3a1dd480534, with tests
diff --git a/apps/server/src/orchestration-v2/WireProjection.test.ts b/apps/server/src/orchestration-v2/WireProjection.test.ts
index dbb1f899..eaf194f6 100644
--- a/apps/server/src/orchestration-v2/WireProjection.test.ts
+++ b/apps/server/src/orchestration-v2/WireProjection.test.ts
@@ -18,6 +18,8 @@ import * as DateTime from "effect/DateTime";
 import * as Schema from "effect/Schema";
 
 import {
+  FILE_CHANGE_TEXT_WIRE_BYTES,
+  boundFileChangeForStorage,
   projectTurnItemForWire,
   projectTurnItemForDetail,
   projectDomainEventForWire,
@@ -375,7 +377,7 @@ describe("orchestration V2 wire projection", () => {
     expect(projectTurnItemForWire(passing)).not.toHaveProperty("outputIndicatesFailure");
   });
 
-  it("omits inline file bodies but preserves file identity and change counts", () => {
+  it("carries a finished file change's old and new text whole, never its diff", () => {
     const item = {
       ...base,
       type: "file_change" as const,
@@ -387,20 +389,108 @@ describe("orchestration V2 wire projection", () => {
       newStr: "new code",
     };
     const projected = projectTurnItemForWire(item);
+    expect(projected).toMatchObject({
+      fileName: "src/main.ts",
+      additions: 3,
+      deletions: 1,
+      oldStr: "old code",
+      newStr: "new code",
+    });
     expect(projected).not.toHaveProperty("diffStr");
-    expect(projected).not.toHaveProperty("oldStr");
-    expect(projected).not.toHaveProperty("newStr");
-    expect(projected).toMatchObject({ fileName: "src/main.ts", additions: 3, deletions: 1 });
     expect(item.diffStr).toBe("+new code");
+
+    // A whole file written has only the string it wrote.
+    const written = projectTurnItemForWire({ ...item, oldStr: undefined });
+    expect(written).toMatchObject({ newStr: "new code" });
+    expect(written).not.toHaveProperty("oldStr");
+    expect(written).not.toHaveProperty("diffStr");
+
+    // The detail read carries the same and no more.
+    expect(projectTurnItemForDetail(item)).toEqual(projected);
+  });
+
+  it("carries no text for a file change that has not finished", () => {
+    const item = {
+      ...base,
+      type: "file_change" as const,
+      fileName: "src/main.ts",
+      diffStr: "+new code",
+      oldStr: "old code",
+      newStr: "new code",
+    };
+    for (const status of [
+      "idle",
+      "pending",
+      "running",
+      "waiting",
+      "cancelled",
+      "interrupted",
+    ] as const) {
+      const projected = projectTurnItemForWire({ ...item, status });
+      expect(projected).toMatchObject({ fileName: "src/main.ts", status });
+      expect(projected).not.toHaveProperty("oldStr");
+      expect(projected).not.toHaveProperty("newStr");
+      expect(projected).not.toHaveProperty("diffStr");
+    }
     const failed = projectTurnItemForWire({
       ...item,
       status: "failed",
       diffStr: "String to replace not found",
     });
     expect(failed).toMatchObject({ diffStr: "String to replace not found" });
+    expect(failed).not.toHaveProperty("oldStr");
     expect(failed).not.toHaveProperty("newStr");
   });
 
+  it("carries a file change's text whole or not at all, counted in bytes", () => {
+    const item = {
+      ...base,
+      type: "file_change" as const,
+      fileName: "src/main.ts",
+    };
+    const half = FILE_CHANGE_TEXT_WIRE_BYTES / 2;
+
+    // Exactly the limit, the two strings together: both whole.
+    const fits = projectTurnItemForWire({
+      ...item,
+      oldStr: "a".repeat(half),
+      newStr: "b".repeat(half),
+    });
+    expect(fits).toMatchObject({ oldStr: "a".repeat(half), newStr: "b".repeat(half) });
+
+    // One byte over, together: neither, and no piece of either.
+    const over = projectTurnItemForWire({
+      ...item,
+      oldStr: "a".repeat(half),
+      newStr: "b".repeat(half + 1),
+    });
+    expect(over).not.toHaveProperty("oldStr");
+    expect(over).not.toHaveProperty("newStr");
+    expect(JSON.stringify(over)).not.toContain("aaaa");
+    expect(JSON.stringify(over)).not.toContain("bbbb");
+
+    // Bytes, not characters: a three-byte character fills the limit sooner.
+    const wide = "\u20ac".repeat(Math.floor(FILE_CHANGE_TEXT_WIRE_BYTES / 3));
+    expect(projectTurnItemForWire({ ...item, newStr: wide })).toMatchObject({ newStr: wide });
+    const wider = projectTurnItemForWire({ ...item, newStr: `${wide}\u20ac` });
+    expect(wider).not.toHaveProperty("newStr");
+
+    // What the store cut is still longer than the limit, so a piece is never
+    // sent. This holds only while the limit stays under the store's own.
+    const whole = "c".repeat(40_000);
+    const stored = boundFileChangeForStorage({ ...item, newStr: whole });
+    const kept = stored.type === "file_change" ? (stored.newStr ?? "") : "";
+    expect(kept).not.toBe(whole);
+    expect(kept.startsWith("cccc")).toBe(true);
+    expect(Buffer.byteLength(kept, "utf8")).toBeGreaterThan(FILE_CHANGE_TEXT_WIRE_BYTES);
+    expect(projectTurnItemForWire(stored)).not.toHaveProperty("newStr");
+
+    // An empty string is no text.
+    const empty = projectTurnItemForWire({ ...item, oldStr: "gone", newStr: "" });
+    expect(empty).toMatchObject({ oldStr: "gone" });
+    expect(empty).not.toHaveProperty("newStr");
+  });
+
   it("retains only result identities and failure metadata in live tool events", () => {
     const output = {
       isError: true,
diff --git a/apps/server/src/orchestration-v2/WireProjection.ts b/apps/server/src/orchestration-v2/WireProjection.ts
index d5bb17cc..b7f9f1a3 100644
--- a/apps/server/src/orchestration-v2/WireProjection.ts
+++ b/apps/server/src/orchestration-v2/WireProjection.ts
@@ -70,6 +70,35 @@ export function wireCommandOutput(output: string): string | undefined {
     : undefined;
 }
 
+/** The longest file change text the wire carries, in UTF-8 bytes. */
+export const FILE_CHANGE_TEXT_WIRE_BYTES = 16_384;
+
+/**
+ * A finished file change's text for the wire: the string it replaced and the
+ * string it wrote, both whole, or neither when together they are longer than
+ * FILE_CHANGE_TEXT_WIRE_BYTES. Never a piece, for the reason wireCommandOutput
+ * gives; text this short was never cut by boundFileChangeForStorage. The diff
+ * stays behind: a host redacts text as it was written, and a diff puts a
+ * marker in front of every line.
+ */
+export function wireFileChangeText(item: {
+  readonly oldStr?: string | undefined;
+  readonly newStr?: string | undefined;
+}): { readonly oldStr?: string; readonly newStr?: string } {
+  const values = [item.oldStr ?? "", item.newStr ?? ""];
+  // UTF-8 needs at least one byte per UTF-16 code unit, so a value too long by
+  // its length alone is never encoded to be counted.
+  const whole =
+    values.every((value) => value.length <= FILE_CHANGE_TEXT_WIRE_BYTES) &&
+    values.reduce((bytes, value) => bytes + Buffer.byteLength(value, "utf8"), 0) <=
+      FILE_CHANGE_TEXT_WIRE_BYTES;
+  if (!whole) return {};
+  return {
+    ...(item.oldStr ? { oldStr: item.oldStr } : {}),
+    ...(item.newStr ? { newStr: item.newStr } : {}),
+  };
+}
+
 function summarizeDynamicValue(value: unknown): unknown {
   let serialized: string;
   try {
@@ -135,8 +164,14 @@ export function projectTurnItemForWire(item: OrchestrationV2TurnItem): Orchestra
     }
     case "file_change": {
       const { diffStr, oldStr: _old, newStr: _new, ...projected } = item;
-      return item.status === "failed" && diffStr?.trim()
-        ? { ...projected, diffStr: truncateDetail(diffStr) }
+      if (item.status === "failed") {
+        return diffStr?.trim() ? { ...projected, diffStr: truncateDetail(diffStr) } : projected;
+      }
+      // What a finished change replaced and wrote rides along whole
+      // (wireFileChangeText). In any other state the change may not have been
+      // made, so its text is nothing a host can show as done.
+      return item.status === "completed"
+        ? { ...projected, ...wireFileChangeText(item) }
         : projected;
     }
     case "subagent":
diff --git a/packages/provider-opencode/src/server/toolItems.test.ts b/packages/provider-opencode/src/server/toolItems.test.ts
new file mode 100644
index 00000000..026c844d
--- /dev/null
+++ b/packages/provider-opencode/src/server/toolItems.test.ts
@@ -0,0 +1,73 @@
+import { assert, describe, it } from "@effect/vitest";
+import { ThreadId, TurnItemId } from "@t3tools/contracts";
+import * as DateTime from "effect/DateTime";
+
+import { openCodeToolTurnItem } from "./toolItems.ts";
+
+const base = {
+  id: TurnItemId.make("tool-1"),
+  threadId: ThreadId.make("thread-1"),
+  runId: null,
+  nodeId: null,
+  providerThreadId: null,
+  providerTurnId: null,
+  nativeItemRef: null,
+  parentItemId: null,
+  ordinal: 1,
+  status: "completed" as const,
+  title: null,
+  startedAt: DateTime.makeUnsafe("2026-08-13T00:00:00.000Z"),
+  completedAt: DateTime.makeUnsafe("2026-08-13T00:00:01.000Z"),
+  updatedAt: DateTime.makeUnsafe("2026-08-13T00:00:01.000Z"),
+};
+
+function item(name: string, input: Record<string, unknown>, metadata?: unknown) {
+  return openCodeToolTurnItem(base, {
+    name,
+    input,
+    output: undefined,
+    completedMetadata: metadata,
+  });
+}
+
+describe("OpenCode file change items", () => {
+  it("carries the strings an edit replaced and wrote, exactly as given", () => {
+    const edit = item(
+      "edit",
+      { filePath: "src/main.ts", oldString: "\n\n", newString: "\nfoo\n" },
+      { diff: "@@ -1,2 +1,2 @@" },
+    );
+    assert.deepStrictEqual(
+      { ...edit },
+      {
+        ...base,
+        type: "file_change",
+        fileName: "src/main.ts",
+        oldStr: "\n\n",
+        newStr: "\nfoo\n",
+        diffStr: "@@ -1,2 +1,2 @@",
+      },
+    );
+  });
+
+  it("carries only the string a whole file write wrote", () => {
+    const write = item("write", { filePath: "notes.md", content: "one\ntwo\n" });
+    assert.strictEqual(write.type, "file_change");
+    assert.strictEqual("newStr" in write ? write.newStr : undefined, "one\ntwo\n");
+    assert.strictEqual("oldStr" in write, false);
+  });
+
+  it("treats an empty string as no text", () => {
+    const removal = item("edit", { filePath: "src/main.ts", oldString: "gone", newString: "" });
+    assert.strictEqual("oldStr" in removal ? removal.oldStr : undefined, "gone");
+    assert.strictEqual("newStr" in removal, false);
+  });
+
+  it("carries no text for a tool that is only named like an edit", () => {
+    const named = item("notion_write_page", { content: "hello", oldText: "was" });
+    assert.deepStrictEqual(
+      { ...named },
+      { ...base, type: "file_change", fileName: "notion_write_page" },
+    );
+  });
+});
diff --git a/packages/provider-opencode/src/server/toolItems.ts b/packages/provider-opencode/src/server/toolItems.ts
index f4551cdd..5fe37819 100644
--- a/packages/provider-opencode/src/server/toolItems.ts
+++ b/packages/provider-opencode/src/server/toolItems.ts
@@ -48,6 +48,16 @@ function recordString(input: unknown, ...keys: ReadonlyArray<string>): string |
   return undefined;
 }
 
+/** A file's text exactly as the tool was given it: blank lines are text too. */
+function recordText(input: unknown, ...keys: ReadonlyArray<string>): string | undefined {
+  if (typeof input !== "object" || input === null) return undefined;
+  for (const key of keys) {
+    const value = (input as Record<string, unknown>)[key];
+    if (typeof value === "string" && value.length > 0) return value;
+  }
+  return undefined;
+}
+
 function recordNumber(input: unknown, ...keys: ReadonlyArray<string>): number | undefined {
   if (typeof input !== "object" || input === null) return undefined;
   for (const key of keys) {
@@ -88,15 +98,20 @@ export function openCodeToolTurnItem(
       };
     }
     case "file_change": {
-      const oldStr = recordString(input, "oldString", "oldText");
-      const newStr = recordString(input, "newString", "content", "newText");
+      const fileName = recordString(input, "filePath", "path", "file");
+      // The strings are a file's text only when the input names the file. A
+      // tool that is just named like an edit (an MCP "write") names none, and
+      // what it was given is not a file's old and new text.
+      const oldStr = fileName === undefined ? undefined : recordText(input, "oldString", "oldText");
+      const newStr =
+        fileName === undefined ? undefined : recordText(input, "newString", "content", "newText");
       const diffStr =
         recordString(tool.completedMetadata, "diff", "patch") ??
         (base.status === "failed" && output?.trim() ? output : undefined);
       return {
         ...base,
         type: "file_change",
-        fileName: recordString(input, "filePath", "path", "file") ?? tool.name,
+        fileName: fileName ?? tool.name,
         ...(oldStr === undefined ? {} : { oldStr }),
         ...(newStr === undefined ? {} : { newStr }),
         ...(diffStr === undefined ? {} : { diffStr }),

Checked in a checkout of the fork source (upstream 9c161462c9 plus the release patch, whose sha256 matches third_party/t3code-fork.lock), in a Linux arm64 container:

  • vp test run src/orchestration-v2/WireProjection.test.ts -> 31 pass (29 before: one test replaced, three added)
  • packages/provider-opencode: the new toolItems.test.ts -> 4 pass, and each fails with its half of the change taken out; adapter.test.ts -> 48 pass
  • vp test run in apps/server -> 6343 pass, 14 fail, 50 skipped. The failures are in eight files (CLI theme, keybindings, the two ACP adapters, migration 026, the browser preview, the terminal manager, usage), and the same eight fail on the source without this patch.
  • vp test run in packages/provider-opencode -> 213 pass, 5 fail, all in OpenCodeServerLedger.test.ts, which fails the same way without this patch.
  • vp fmt --check and vp lint on the four files -> ok, 0 warnings
  • tsc-rs --noEmit in apps/server and in packages/provider-opencode -> 0 errors
  • Built with vp run --filter t3 build and packaged the way deploy/t3/package-runtime.sh does: the same files as the release archive, with one chunk renamed, the one that holds this code.

Three things the text follows that the runtime already does, for whoever takes the patch:

  • OpenCode's adapter marks a tool still open at the end of a completed turn as completed, so such an edit's strings would show as a finished change. The plane already records it as one.
  • The runtime's own thread reader for agents (OrchestratorMcpService) prints a file change's newStr. It is off in our sandboxes (T3_PROVIDER_MCP=off).
  • There is no marker for text left out for its size, unlike outputOmitted for a command.

Autofix with [code]smith
Open in [code]smith · Need help on this PR? Tag @codesmith-bot with what you need.

@abhishekgahlot2
abhishekgahlot2 merged commit f99aa18 into useagenthq:main Nov 14, 2026
21 checks passed
@abhishekgahlot2

Copy link
Copy Markdown
Contributor

Merged, thanks @coolestnick. Mapping the runtime's strings onto the keys the Diff pane already reads, capping them at 16 KiB, storing them once and running them through the same redaction as other tool input is careful groundwork. A small marker when an edit is over the cap would let the pane say why later.

@github-actions github-actions Bot locked and limited conversation to collaborators Nov 14, 2026
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants