Skip to content

Commit bcebb79

Browse files
t3dotggclaude
andcommitted
fix: decode quoted paths as UTF-8 and count hidden git files from patch sections
- Git C-style quoting octal-escapes raw UTF-8 bytes; decoding each escape with String.fromCharCode produced mojibake, so attribution lookups missed quoted non-ASCII paths and their sections were never filtered (macroscope, bugbot). stripGitPathQuoting now decodes into a byte buffer and UTF-8 decodes the result. - gitFileCount counted every git-attributed path in the attribution map, overstating hidden files when a path had no section in the patch (e.g. whitespace-only changes elided by ignoreWhitespace) and double-counting renames from the --no-renames map (macroscope, bugbot). It now counts git-attributed sections actually present in the patch, so the banner matches what the toggle can reveal. Co-Authored-By: Claude Fable 5 <[email protected]>
1 parent 0dd887c commit bcebb79

4 files changed

Lines changed: 176 additions & 42 deletions

File tree

‎apps/server/src/checkpointing/CheckpointDiffQuery.test.ts‎

Lines changed: 96 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -304,6 +304,102 @@ describe("CheckpointDiffQuery.layer", () => {
304304
}),
305305
);
306306

307+
it.effect("filters git-attributed sections and reports gitFileCount from patch sections", () =>
308+
Effect.gen(function* () {
309+
const projectId = ProjectId.make("project-attribution");
310+
const threadId = ThreadId.make("thread-attribution");
311+
const toCheckpointRef = checkpointRefForThreadTurn(threadId, 1);
312+
const patch = [
313+
"diff --git a/agent.ts b/agent.ts",
314+
"index 1111111..2222222 100644",
315+
"--- a/agent.ts",
316+
"+++ b/agent.ts",
317+
"@@ -1,1 +1,1 @@",
318+
"-old",
319+
"+new",
320+
"diff --git a/pulled.ts b/pulled.ts",
321+
"index 3333333..4444444 100644",
322+
"--- a/pulled.ts",
323+
"+++ b/pulled.ts",
324+
"@@ -1,1 +1,1 @@",
325+
"-before",
326+
"+after",
327+
"",
328+
].join("\n");
329+
// Attribution also tags a path that has no section in this patch
330+
// (e.g. whitespace-only change elided by ignoreWhitespace) — it must
331+
// not count toward gitFileCount.
332+
const attribution = new Map<string, "agent" | "git">([
333+
["agent.ts", "agent"],
334+
["pulled.ts", "git"],
335+
["whitespace-only.ts", "git"],
336+
]);
337+
338+
const threadCheckpointContext = makeThreadCheckpointContext({
339+
projectId,
340+
threadId,
341+
workspaceRoot: "/tmp/workspace",
342+
worktreePath: null,
343+
checkpointTurnCount: 1,
344+
checkpointRef: toCheckpointRef,
345+
});
346+
347+
const checkpointStore: CheckpointStore.CheckpointStore["Service"] = {
348+
isGitRepository: () => Effect.succeed(true),
349+
captureCheckpoint: () => Effect.void,
350+
hasCheckpointRef: () => Effect.succeed(true),
351+
restoreCheckpoint: () => Effect.succeed(true),
352+
diffCheckpoints: () => Effect.succeed(patch),
353+
deleteCheckpointRefs: () => Effect.void,
354+
attributeCheckpointDiff: () => Effect.succeed(attribution),
355+
};
356+
357+
const layer = CheckpointDiffQuery.layer.pipe(
358+
Layer.provideMerge(Layer.succeed(CheckpointStore.CheckpointStore, checkpointStore)),
359+
Layer.provideMerge(
360+
Layer.succeed(ProjectionSnapshotQuery.ProjectionSnapshotQuery, {
361+
getCommandReadModel: () => Effect.die("unused"),
362+
getSnapshot: () => Effect.die("unused"),
363+
getShellSnapshot: () => Effect.die("unused"),
364+
getArchivedShellSnapshot: () => Effect.die("unused"),
365+
getSnapshotSequence: () => Effect.succeed({ snapshotSequence: 0 }),
366+
getCounts: () => Effect.succeed({ projectCount: 0, threadCount: 0 }),
367+
getActiveProjectByWorkspaceRoot: () => Effect.succeed(Option.none()),
368+
getProjectShellById: () => Effect.succeed(Option.none()),
369+
getFirstActiveThreadIdByProjectId: () => Effect.succeed(Option.none()),
370+
getThreadCheckpointContext: () => Effect.succeed(Option.some(threadCheckpointContext)),
371+
getFullThreadDiffContext: () => Effect.die("unused"),
372+
getThreadShellById: () => Effect.succeed(Option.none()),
373+
getThreadDetailById: () => Effect.succeed(Option.none()),
374+
getThreadDetailSnapshot: () => Effect.succeed(Option.none()),
375+
}),
376+
),
377+
);
378+
379+
const { filtered, revealed } = yield* Effect.gen(function* () {
380+
const query = yield* CheckpointDiffQuery.CheckpointDiffQuery;
381+
const filtered = yield* query.getTurnDiff({
382+
threadId,
383+
fromTurnCount: 0,
384+
toTurnCount: 1,
385+
});
386+
const revealed = yield* query.getTurnDiff({
387+
threadId,
388+
fromTurnCount: 0,
389+
toTurnCount: 1,
390+
includeGitChanges: true,
391+
});
392+
return { filtered, revealed };
393+
}).pipe(Effect.provide(layer));
394+
395+
expect(filtered.diff).toContain("agent.ts");
396+
expect(filtered.diff).not.toContain("pulled.ts");
397+
expect(filtered.gitFileCount).toBe(1);
398+
expect(revealed.diff).toBe(patch);
399+
expect(revealed.gitFileCount).toBe(1);
400+
}),
401+
);
402+
307403
it.effect("does not preflight checkpoint refs before diffing", () =>
308404
Effect.gen(function* () {
309405
const projectId = ProjectId.make("project-no-preflight");

‎apps/server/src/checkpointing/CheckpointDiffQuery.ts‎

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ import {
3131
} from "./Errors.ts";
3232
import type { CheckpointServiceError } from "./Errors.ts";
3333
import { checkpointRefForThreadTurn } from "./Utils.ts";
34-
import { filterUnifiedDiffFiles } from "./Diffs.ts";
34+
import { filterUnifiedDiffFiles, listUnifiedDiffPaths } from "./Diffs.ts";
3535
import * as CheckpointStore from "./CheckpointStore.ts";
3636

3737
/** Service tag for checkpoint diff queries. */
@@ -108,9 +108,13 @@ export const make = Effect.gen(function* () {
108108
if (attribution === null) {
109109
return { diff: input.diff, gitFileCount: undefined };
110110
}
111+
// Count only git-attributed paths with a section in THIS patch. The
112+
// attribution map can include paths absent from the patch (e.g.
113+
// whitespace-only changes elided by ignoreWhitespace) — counting those
114+
// would advertise hidden files the toggle can never reveal.
111115
let gitFileCount = 0;
112-
for (const origin of attribution.values()) {
113-
if (origin === "git") {
116+
for (const path of listUnifiedDiffPaths(input.diff)) {
117+
if (attribution.get(path) === "git") {
114118
gitFileCount += 1;
115119
}
116120
}

‎apps/server/src/checkpointing/Diffs.test.ts‎

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -196,10 +196,9 @@ describe("filterUnifiedDiffFiles fallback paths", () => {
196196
"+new",
197197
"",
198198
].join("\n");
199-
// git octal-escapes UTF-8 bytes; our decoder maps each escaped byte to a
200-
// char code, so the filter key uses the same byte-per-char convention.
201-
const decodedPath = "späce.ts";
202-
expect(filterUnifiedDiffFiles(quotedDiff, (path) => path !== decodedPath).trim()).toBe("");
199+
// git octal-escapes the raw UTF-8 bytes (`ä` = \303\244); the decoder
200+
// must reassemble them into the real character, not per-byte mojibake.
201+
expect(filterUnifiedDiffFiles(quotedDiff, (path) => path !== "späce.ts").trim()).toBe("");
203202
});
204203

205204
it("filters rename-only sections without hunks via rename metadata", () => {

‎apps/server/src/checkpointing/Diffs.ts‎

Lines changed: 70 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -26,35 +26,51 @@ export function parseTurnDiffFilesFromUnifiedDiff(
2626
return files.toSorted((left, right) => left.path.localeCompare(right.path));
2727
}
2828

29+
const SIMPLE_ESCAPES: Record<string, number> = {
30+
"\\": 0x5c,
31+
'"': 0x22,
32+
a: 0x07,
33+
b: 0x08,
34+
f: 0x0c,
35+
n: 0x0a,
36+
r: 0x0d,
37+
t: 0x09,
38+
v: 0x0b,
39+
};
40+
2941
function stripGitPathQuoting(value: string): string {
3042
if (!(value.startsWith('"') && value.endsWith('"') && value.length >= 2)) {
3143
return value;
3244
}
33-
// Git C-style quoting: decode the escapes it emits for unusual paths.
34-
return value.slice(1, -1).replace(/\\([\\"abfnrtv]|[0-7]{1,3})/g, (_, escape: string) => {
35-
switch (escape) {
36-
case "\\":
37-
return "\\";
38-
case '"':
39-
return '"';
40-
case "a":
41-
return "\x07";
42-
case "b":
43-
return "\b";
44-
case "f":
45-
return "\f";
46-
case "n":
47-
return "\n";
48-
case "r":
49-
return "\r";
50-
case "t":
51-
return "\t";
52-
case "v":
53-
return "\v";
54-
default:
55-
return String.fromCharCode(parseInt(escape, 8));
45+
// Git C-style quoting: octal escapes are raw UTF-8 BYTES (e.g. `ä` is
46+
// `\303\244`), so decode into a byte buffer first and UTF-8-decode the
47+
// whole path at the end — per-escape String.fromCharCode would yield
48+
// mojibake that never matches attribution map keys.
49+
const inner = value.slice(1, -1);
50+
const bytes: Array<number> = [];
51+
const encoder = new TextEncoder();
52+
for (let index = 0; index < inner.length; index += 1) {
53+
const char = inner[index]!;
54+
if (char !== "\\" || index === inner.length - 1) {
55+
bytes.push(...encoder.encode(char));
56+
continue;
5657
}
57-
});
58+
const next = inner[index + 1]!;
59+
const simple = SIMPLE_ESCAPES[next];
60+
if (simple !== undefined) {
61+
bytes.push(simple);
62+
index += 1;
63+
continue;
64+
}
65+
const octal = /^[0-7]{1,3}/.exec(inner.slice(index + 1))?.[0];
66+
if (octal) {
67+
bytes.push(parseInt(octal, 8) & 0xff);
68+
index += octal.length;
69+
continue;
70+
}
71+
bytes.push(...encoder.encode(char));
72+
}
73+
return new TextDecoder("utf-8", { fatal: false }).decode(new Uint8Array(bytes));
5874
}
5975

6076
/**
@@ -124,20 +140,11 @@ function resolveDiffSectionPath(section: string): string | null {
124140
return fallbackSource ?? renameTarget ?? headerPath;
125141
}
126142

127-
/**
128-
* Remove file sections from a unified diff by path.
129-
*
130-
* Sections whose path cannot be determined are kept (fail toward showing).
131-
*/
132-
export function filterUnifiedDiffFiles(
133-
diff: string,
134-
shouldKeepPath: (path: string) => boolean,
135-
): string {
143+
function splitUnifiedDiffSections(diff: string): ReadonlyArray<string> {
136144
const normalized = diff.replace(/\r\n/g, "\n");
137145
if (normalized.trim().length === 0) {
138-
return diff;
146+
return [];
139147
}
140-
141148
const sections: Array<string> = [];
142149
let currentStart = -1;
143150
const lines = normalized.split("\n");
@@ -153,6 +160,34 @@ export function filterUnifiedDiffFiles(
153160
}
154161
}
155162
flush(lines.length);
163+
return sections;
164+
}
165+
166+
/**
167+
* List the resolvable post-image paths of a unified diff's file sections.
168+
* Unparseable sections are omitted.
169+
*/
170+
export function listUnifiedDiffPaths(diff: string): ReadonlyArray<string> {
171+
const paths: Array<string> = [];
172+
for (const section of splitUnifiedDiffSections(diff)) {
173+
const path = resolveDiffSectionPath(section);
174+
if (path !== null) {
175+
paths.push(path);
176+
}
177+
}
178+
return paths;
179+
}
180+
181+
/**
182+
* Remove file sections from a unified diff by path.
183+
*
184+
* Sections whose path cannot be determined are kept (fail toward showing).
185+
*/
186+
export function filterUnifiedDiffFiles(
187+
diff: string,
188+
shouldKeepPath: (path: string) => boolean,
189+
): string {
190+
const sections = splitUnifiedDiffSections(diff);
156191
if (sections.length === 0) {
157192
return diff;
158193
}

0 commit comments

Comments
 (0)