Skip to content

Local-first AI coding agent desktop: Electron + Rust host core + pi Agent Harness + user-installable plugins

fix(sessions): collapse copied session files onto one entry per native id

yexisuf90c4e31ef89
diff --git a/packages/agent-runtime/src/native-pi-session.test.ts b/packages/agent-runtime/src/native-pi-session.test.ts
index d9f4b3436..43bc90963 100644
--- a/packages/agent-runtime/src/native-pi-session.test.ts
+++ b/packages/agent-runtime/src/native-pi-session.test.ts
@@ -61,6 +61,24 @@ describe("NativePiSessionService", () => {
expect(readFileSync(f.file)).toEqual(before);
});
+ it("collapses copied session files onto one entry per native id (#1359)", async () => {
+ const f = fixture();
+ // A backup copy of the session under a subdirectory of the scan root:
+ // same header id, different path. It must not become a second session.
+ const backupDir = join(f.sessionRoot, "backup");
+ mkdirSync(backupDir, { recursive: true });
+ writeFileSync(join(backupDir, "fixture-copy.jsonl"), f.text);
+
+ // Control: without the copy, list() names the original file's id.
+ const control = new NativePiSessionService({ agentDir: f.agentDir, sessionRoot: f.sessionRoot });
+ const [original] = await control.list();
+
+ const service = new NativePiSessionService({ agentDir: f.agentDir, sessionRoot: f.sessionRoot });
+ const sessions = await service.list();
+ expect(sessions).toHaveLength(1);
+ expect(sessions[0].id).toBe(original.id);
+ });
+
it("searches native metadata and active-branch message text without rewriting JSONL", async () => {
const f = fixture();
const service = new NativePiSessionService({ agentDir: f.agentDir, sessionRoot: f.sessionRoot });
diff --git a/packages/agent-runtime/src/native-pi-session.ts b/packages/agent-runtime/src/native-pi-session.ts
index b57a5cd44..f6b74d197 100644
--- a/packages/agent-runtime/src/native-pi-session.ts
+++ b/packages/agent-runtime/src/native-pi-session.ts
@@ -547,7 +547,30 @@ export class NativePiSessionService {
}
}),
);
- return summaries
+ // A copied session file (backup, manual copy) under the scan root must
+ // not become a second session: identity follows the header id, not the
+ // path. Keep the newest write per native id and drop the stale copies
+ // from the records map so lookups cannot route to a dead duplicate
+ // (#1359).
+ const seenNativeIds = new Set<string>();
+ const deduped: Array<SessionSummary | undefined> = [];
+ const sortedByRecency = [...summaries].sort((a, b) =>
+ (b?.updatedAt ?? "").localeCompare(a?.updatedAt ?? ""),
+ );
+ const winnerIds = new Set<string>();
+ for (const summary of sortedByRecency) {
+ if (!summary) continue;
+ const record = this.records.get(summary.id);
+ if (!record) continue;
+ if (seenNativeIds.has(record.nativeId)) {
+ this.records.delete(summary.id);
+ continue;
+ }
+ seenNativeIds.add(record.nativeId);
+ winnerIds.add(summary.id);
+ }
+ deduped.push(...summaries.filter((summary) => summary && winnerIds.has(summary.id)));
+ return deduped
.filter((session): session is SessionSummary => Boolean(session))
.sort((a, b) => b.updatedAt.localeCompare(a.updatedAt));
}