@devries-cameron20 / t3code Mirror · read-only / commits / 1a51984153
Fork 0

The best way to control Claude Code, Codex and more from any computer or phone

Mirrored from https://github.com/Camo67/t3code.git

Synced Nov 21, 06:00 UTC

fix(web): a tool call's diff names its files relative to the workspace (#32697)

Co-authored-by: maria-rcks <maria@kuuro.net> Co-authored-by: Julius Marminge <julius0216@outlook.com>

Entrpi committed Nov 20, 2026 at 07:15 UTC 1a51984153fd99c06d7f49846bac48c840a5a7d0
3 files changed +106 -2
apps/web/src/components/DiffPanel.tsx
+1 -1
@@ -484,7 +484,7 @@ export default function DiffPanel({
484
485 const toolCallPreview =
486 toolCallSelection && toolCallItem?.type === "file_change"
487 - ? toolCallDiff(toolCallItem, messages)
487 + ? toolCallDiff(toolCallItem, activeCwd, messages)
488 : null;
489 const selectedPatch = toolCallSelection
490 ? toolCallPreview?.patch
apps/web/src/lib/toolCallDiff.test.ts
+62
@@ -104,6 +104,68 @@ describe("toolCallDiff", () => {
104 });
105 });
106
107 + it("names files under the workspace relative to it in headers only", () => {
108 + const root = "/Users/me/project";
109 + const paths = (patch: string | undefined) => {
110 + const rendered = getRenderablePatch(patch);
111 + if (rendered?.kind !== "files") throw new Error("Expected a file diff");
112 + return rendered.files.map((file) => resolveFileDiffPath(file));
113 + };
114 + // Claude's Edit: absolute headers; hunk lines that look like headers keep the root.
115 + const claude = `--- ${root}/docs/plan.md\n+++ ${root}/docs/plan.md\n@@ -2 +2 @@\n-- ${root}/x\n++ ${root}/y\n`;
116 + const { patch } = toolCallDiff({ fileName: `${root}/docs/plan.md`, diffStr: claude }, root);
117 + expect(paths(patch)).toEqual(["docs/plan.md"]);
118 + expect(patch).toContain(`\n-- ${root}/x\n++ ${root}/y\n`);
119 + // Synthesized headers, a trailing separator on the root, and a path outside it.
120 + const hunk = "@@ -1 +1 @@\n-x\n+y\n";
121 + expect(
122 + paths(toolCallDiff({ fileName: `${root}/src/a.ts`, diffStr: hunk }, `${root}/`).patch),
123 + ).toEqual(["src/a.ts"]);
124 + expect(paths(toolCallDiff({ fileName: "/elsewhere/b.ts", diffStr: hunk }, root).patch)).toEqual(
125 + ["/elsewhere/b.ts"],
126 + );
127 + // A relative path, and on POSIX a backslash that is part of a name, keep theirs.
128 + for (const [fileName, workspace] of [
129 + ["repo/src/a.ts", "/repo"],
130 + ["/repo/back/slash/a.ts", "/repo/back\\slash"],
131 + ] as const) {
132 + const diffStr = `--- ${fileName}\n+++ ${fileName}\n${hunk}`;
133 + expect(toolCallDiff({ fileName, diffStr }, workspace).patch).toBe(diffStr);
134 + }
135 + const backslash = toolCallDiff(
136 + { fileName: "/repo/back\\slash/a.ts", diffStr: hunk },
137 + "/repo/back\\slash",
138 + ).patch;
139 + expect(paths(backslash)).toEqual(["a.ts"]);
140 + // Each file of a multi-file patch; a path that merely contains the root keeps it.
141 + const twoFiles = [
142 + `diff --git a/${root}/a.ts b/${root}/a.ts`,
143 + `--- a/${root}/a.ts`,
144 + `+++ b/${root}/a.ts`,
145 + hunk.trimEnd(),
146 + `diff --git a/backup${root}/b.ts b/backup${root}/b.ts`,
147 + `--- a/backup${root}/b.ts`,
148 + `+++ b/backup${root}/b.ts`,
149 + hunk,
150 + ].join("\n");
151 + expect(
152 + paths(toolCallDiff({ fileName: `${root}/a.ts`, diffStr: twoFiles }, root).patch),
153 + ).toEqual(["a.ts", `backup${root}/b.ts`]);
154 + // Only where a name starts: a quoted name with a space before the root.
155 + expect(
156 + paths(toolCallDiff({ fileName: `${root}/docs ${root}/a.md`, diffStr: hunk }, root).patch),
157 + ).toEqual([`docs ${root}/a.md`]);
158 + expect(
159 + paths(toolCallDiff({ fileName: `/backup ${root}/a.md`, diffStr: hunk }, root).patch),
160 + ).toEqual([`/backup ${root}/a.md`]);
161 + // Windows paths, which a quoted header escapes, compared without case.
162 + const windows = toolCallDiff(
163 + { fileName: "C:\\repo\\src\\a.ts", diffStr: hunk },
164 + "c:\\repo",
165 + ).patch;
166 + expect(paths(windows)).toEqual(["src\\a.ts"]);
167 + });
168 +
169 it("preserves complete patches and unsupported provider output", () => {
170 for (const operation of ["add", "delete"]) {
171 const diffStr = [
apps/web/src/lib/toolCallDiff.ts
+43 -1
@@ -53,6 +53,44 @@ function toolCallPatch(item: Pick<FileChange, "diffStr" | "fileName" | "changes"
53 return `diff --git ${JSON.stringify(`a/${oldPath}`)} ${JSON.stringify(`b/${newPath}`)}\n--- ${before}\n+++ ${after}\n${patch.trimStart()}`;
54 }
55
56 +// Claude records absolute paths; name files under the workspace relative to it, as other diffs do.
57 +// Only header lines, from the start or a `diff --git` line up to the first hunk, are rewritten.
58 +function relativeHeaderPaths(patch: string | undefined, workspaceRoot: string | undefined) {
59 + const windows = /^(?:[A-Za-z]:[\\/]|\\\\)/.test(workspaceRoot ?? "");
60 + const segments = workspaceRoot?.split(windows ? /[\\/]+/ : /\/+/);
61 + while (segments?.at(-1) === "") segments.pop();
62 + if (!patch || !segments?.some(Boolean)) return patch;
63 + // Windows separates with / or \ (\\ in a quoted header); POSIX only with /, where a \ is part of
64 + // a name and a quoted header doubles it.
65 + const separator = windows ? String.raw`[\\/]+` : "/+";
66 + const root = segments
67 + .map((segment) =>
68 + segment
69 + .split("\\")
70 + .map((text) => text.replace(/[.*+?^${}()|[\]\\]/g, "\\$&"))
71 + .join(String.raw`\\\\?`),
72 + )
73 + .join(separator);
74 + const prefix = new RegExp(String.raw`^("?(?:[ab]/)?)${root}${separator}`, windows ? "i" : "");
75 + const relative = (name: string) => name.replace(prefix, "$1");
76 + let inHeader = true;
77 + return patch
78 + .split("\n")
79 + .map((line) => {
80 + if (line.startsWith("diff --git ")) {
81 + inHeader = true;
82 + // Split the names as Pierre's parser does, so a name containing a space stays whole.
83 + const names =
84 + /^diff --git ("a\/(?:[^"\\]|\\.)*"|a\/.+?) ("b\/(?:[^"\\]|\\.)*"|b\/.+?)$/.exec(line);
85 + return names ? `diff --git ${relative(names[1]!)} ${relative(names[2]!)}` : line;
86 + }
87 + if (line.startsWith("@@")) inHeader = false;
88 + const name = inHeader ? /^(---|\+\+\+)(\s+)(.*)$/.exec(line) : null;
89 + return name ? `${name[1]}${name[2]}${relative(name[3]!)}` : line;
90 + })
91 + .join("\n");
92 +}
93 +
94 function recordedText(value: string | undefined) {
95 const suffix = "\n… output truncated for transport";
96 const truncated = value !== undefined && value.endsWith(suffix);
@@ -61,6 +99,7 @@ function recordedText(value: string | undefined) {
99
100 export function toolCallDiff(
101 item: Pick<FileChange, "diffStr" | "fileName" | "changes" | "oldStr" | "newStr">,
102 + workspaceRoot?: string,
103 resolve: MessageResolver = m,
104 ) {
105 const diff = recordedText(item.diffStr);
@@ -69,7 +108,10 @@ export function toolCallDiff(
108 const before = recordedText(item.oldStr ?? (operation === "delete" ? emptyContent : undefined));
109 const after = recordedText(item.newStr ?? (operation === "add" ? emptyContent : undefined));
110 return {
72 - patch: toolCallPatch({ ...item, ...(diff.text === undefined ? {} : { diffStr: diff.text }) }),
111 + patch: relativeHeaderPaths(
112 + toolCallPatch({ ...item, ...(diff.text === undefined ? {} : { diffStr: diff.text }) }),
113 + workspaceRoot,
114 + ),
115 content: diff.text?.trim()
116 ? []
117 : [