Skip to content

A desktop coding agent with free access to frontier models.

TypeScript

fix: a file added and removed in one turn is not listed

ogsamratc4fe390c8c1a
diff --git a/apps/app/src/react-app/domains/session/commands/changes.ts b/apps/app/src/react-app/domains/session/commands/changes.ts
index ebdb23f4..50126f97 100644
--- a/apps/app/src/react-app/domains/session/commands/changes.ts
+++ b/apps/app/src/react-app/domains/session/commands/changes.ts
@@ -130,19 +130,28 @@ export function splitRows(lines: readonly PatchLine[]): SplitRow[] {
/**
* One turn's changes from the engine's per-message diffs (a turn split by a compaction has one list per user
* message, oldest first): a file changed in several parts keeps every part's hunks in order and the summed counts.
+ * Its status is the turn's net one: whether it existed at the turn's start (its first part did not add it) and at
+ * its end (its last part did not delete it). A file that existed at neither (added, then deleted) is left out.
*/
export function mergeTurnChanges(lists: readonly ChangedFile[][]): ChangedFile[] {
- const byPath = new Map<string, ChangedFile>();
+ const byPath = new Map<string, { file: ChangedFile; atStart: boolean; atEnd: boolean }>();
for (const list of lists) {
for (const file of list) {
const seen = byPath.get(file.path);
- if (!seen) { byPath.set(file.path, { ...file }); continue; }
- seen.additions += file.additions;
- seen.deletions += file.deletions;
- seen.patch = [seen.patch, file.patch].filter((patch) => patch.trim()).join("\n");
- // Added earlier in the turn stays added; deleted at the end is deleted.
- seen.status = seen.status === "added" ? (file.status === "deleted" ? "modified" : "added") : file.status === "deleted" ? "deleted" : "modified";
+ if (!seen) {
+ byPath.set(file.path, { file: { ...file }, atStart: file.status !== "added", atEnd: file.status !== "deleted" });
+ continue;
+ }
+ seen.file.additions += file.additions;
+ seen.file.deletions += file.deletions;
+ seen.file.patch = [seen.file.patch, file.patch].filter((patch) => patch.trim()).join("\n");
+ seen.atEnd = file.status !== "deleted";
}
}
- return [...byPath.values()].sort((a, b) => a.path.localeCompare(b.path));
+ const out: ChangedFile[] = [];
+ for (const { file, atStart, atEnd } of byPath.values()) {
+ if (!atStart && !atEnd) continue;
+ out.push({ ...file, status: atStart && atEnd ? "modified" : atEnd ? "added" : "deleted" });
+ }
+ return out.sort((a, b) => a.path.localeCompare(b.path));
}
diff --git a/apps/app/tests/turn-changes.test.ts b/apps/app/tests/turn-changes.test.ts
index 3561a1ae..72a353c4 100644
--- a/apps/app/tests/turn-changes.test.ts
+++ b/apps/app/tests/turn-changes.test.ts
@@ -51,8 +51,14 @@ describe("mergeTurnChanges", () => {
expect(merged.find((f) => f.path === "src/new.ts")?.status).toBe("added");
});
- test("added then deleted in one turn reads as modified; deleted last is deleted", () => {
- expect(mergeTurnChanges([[file("a", "p1", 1, 0, "added")], [file("a", "p2", 0, 1, "deleted")]])[0]?.status).toBe("modified");
+ test("a file added and deleted in one turn is left out, also with a change in between", () => {
+ expect(mergeTurnChanges([[file("a", "p1", 1, 0, "added")], [file("a", "p2", 0, 1, "deleted")]])).toEqual([]);
+ expect(mergeTurnChanges([[file("a", "p1", 1, 0, "added")], [file("a", "p2", 1, 1)], [file("a", "p3", 0, 1, "deleted")]])).toEqual([]);
+ });
+
+ test("deleted and made again reads as modified; deleted last is deleted; added and changed is added", () => {
+ expect(mergeTurnChanges([[file("r", "p1", 0, 2, "deleted")], [file("r", "p2", 3, 0, "added")]])[0]?.status).toBe("modified");
expect(mergeTurnChanges([[file("b", "p1", 1, 1)], [file("b", "p2", 0, 3, "deleted")]])[0]?.status).toBe("deleted");
+ expect(mergeTurnChanges([[file("n", "p1", 2, 0, "added")], [file("n", "p2", 1, 1)]])[0]?.status).toBe("added");
});
});