Skip to content

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

test(sessions): pin the canonical file in the copy dedupe

vastsa16950eef168e
diff --git a/packages/agent-runtime/src/native-pi-session.test.ts b/packages/agent-runtime/src/native-pi-session.test.ts
index 13ef82c9c..6bbed1ea9 100644
--- a/packages/agent-runtime/src/native-pi-session.test.ts
+++ b/packages/agent-runtime/src/native-pi-session.test.ts
@@ -63,16 +63,20 @@ describe("NativePiSessionService", () => {
it("collapses copied session files onto one entry per native id (#1359)", async () => {
const f = fixture();
+
+ // Control scan without the copy: the group file is the canonical session.
+ // A byte-identical copy ties on the content-derived recency, so this is
+ // the assertion that fails when the winner follows enumeration order
+ // (#1383).
+ const control = new NativePiSessionService({ agentDir: f.agentDir, sessionRoot: f.sessionRoot });
+ const [original] = await control.list();
+
// 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);
@@ -99,45 +103,10 @@ describe("NativePiSessionService", () => {
it("keeps the open runtime's file when a copy ties on recency (#1383)", async () => {
const f = fixture();
- const model = {
- id: "test-model",
- name: "Test Model",
- api: "openai-completions",
- provider: "test-provider",
- baseUrl: "http://127.0.0.1/unused",
- reasoning: false,
- input: ["text"],
- cost: { input: 0, output: 0, cacheRead: 0, cacheWrite: 0 },
- contextWindow: 16_000,
- maxTokens: 1_000,
- };
- const response: AssistantMessage = {
- role: "assistant",
- content: [{ type: "text", text: "fixture response" }],
- api: "openai-completions",
- provider: "test-provider",
- model: "test-model",
- stopReason: "stop",
- timestamp: Date.now(),
- usage: { input: 1, output: 1, cacheRead: 0, cacheWrite: 0, totalTokens: 2, cost: { input: 0, output: 0, cacheRead: 0, cacheWrite: 0, total: 0 } },
- };
- const fauxRuntime = {
- getModel: () => model,
- hasConfiguredAuth: () => true,
- streamSimple: () => {
- const stream = createAssistantMessageEventStream();
- queueMicrotask(() => {
- stream.push({ type: "start", partial: response });
- stream.push({ type: "done", reason: "stop", message: response });
- stream.end(response);
- });
- return stream;
- },
- };
const service = new NativePiSessionService({
agentDir: f.agentDir,
sessionRoot: f.sessionRoot,
- modelRuntimeFactory: async () => fauxRuntime as any,
+ modelRuntimeFactory: async () => fauxModelRuntime() as any,
});
const [summary] = await service.list();
await service.prompt(summary.id, "desktop prompt", () => undefined);
@@ -237,45 +206,10 @@ describe("NativePiSessionService", () => {
it("continues through AgentSession and appends the faux-model turn to the original file", async () => {
const f = fixture();
- const model = {
- id: "test-model",
- name: "Test Model",
- api: "openai-completions",
- provider: "test-provider",
- baseUrl: "http://127.0.0.1/unused",
- reasoning: false,
- input: ["text"],
- cost: { input: 0, output: 0, cacheRead: 0, cacheWrite: 0 },
- contextWindow: 16_000,
- maxTokens: 1_000,
- };
- const response: AssistantMessage = {
- role: "assistant",
- content: [{ type: "text", text: "fixture response" }],
- api: "openai-completions",
- provider: "test-provider",
- model: "test-model",
- stopReason: "stop",
- timestamp: Date.now(),
- usage: { input: 1, output: 1, cacheRead: 0, cacheWrite: 0, totalTokens: 2, cost: { input: 0, output: 0, cacheRead: 0, cacheWrite: 0, total: 0 } },
- };
- const fauxRuntime = {
- getModel: () => model,
- hasConfiguredAuth: () => true,
- streamSimple: () => {
- const stream = createAssistantMessageEventStream();
- queueMicrotask(() => {
- stream.push({ type: "start", partial: response });
- stream.push({ type: "done", reason: "stop", message: response });
- stream.end(response);
- });
- return stream;
- },
- };
const service = new NativePiSessionService({
agentDir: f.agentDir,
sessionRoot: f.sessionRoot,
- modelRuntimeFactory: async () => fauxRuntime as any,
+ modelRuntimeFactory: async () => fauxModelRuntime() as any,
});
const [summary] = await service.list();
const before = readFileSync(f.file, "utf8");
@@ -346,6 +280,48 @@ function fauxStream(message = response()) {
return stream;
}
+/**
+ * A minimal offline model runtime for the native continuation tests: one
+ * catalog entry, auth always present, one canned assistant turn per call.
+ */
+function fauxModelRuntime() {
+ const model = {
+ id: "test-model",
+ name: "Test Model",
+ api: "openai-completions",
+ provider: "test-provider",
+ baseUrl: "http://127.0.0.1/unused",
+ reasoning: false,
+ input: ["text"],
+ cost: { input: 0, output: 0, cacheRead: 0, cacheWrite: 0 },
+ contextWindow: 16_000,
+ maxTokens: 1_000,
+ };
+ const message: AssistantMessage = {
+ role: "assistant",
+ content: [{ type: "text", text: "fixture response" }],
+ api: "openai-completions",
+ provider: "test-provider",
+ model: "test-model",
+ stopReason: "stop",
+ timestamp: Date.now(),
+ usage: { input: 1, output: 1, cacheRead: 0, cacheWrite: 0, totalTokens: 2, cost: { input: 0, output: 0, cacheRead: 0, cacheWrite: 0, total: 0 } },
+ };
+ return {
+ getModel: () => model,
+ hasConfiguredAuth: () => true,
+ streamSimple: () => {
+ const stream = createAssistantMessageEventStream();
+ queueMicrotask(() => {
+ stream.push({ type: "start", partial: message });
+ stream.push({ type: "done", reason: "stop", message });
+ stream.end(message);
+ });
+ return stream;
+ },
+ };
+}
+
describe("native continuation review regressions", () => {
it("uses the real offline credential/catalog and reports missing exact auth/model", async () => {
const f = await configuredFixture();
diff --git a/packages/agent-runtime/src/native-pi-session.ts b/packages/agent-runtime/src/native-pi-session.ts
index 0b2405587..f0c10807c 100644
--- a/packages/agent-runtime/src/native-pi-session.ts
+++ b/packages/agent-runtime/src/native-pi-session.ts
@@ -564,6 +564,11 @@ export class NativePiSessionService {
// path order, which every platform computes the same way.
const seenNativeIds = new Set<string>();
const deduped: Array<SessionSummary | undefined> = [];
+ // Tie-break inside a recency group: the file an open runtime is already
+ // writing wins, then the smallest record path. Both keys are total orders
+ // over the group, so the winner cannot follow enumeration order. The id
+ // fallback only keeps the comparator total for a summary without a record
+ // (every summary above writes one, so it is not reachable in practice).
const rankTie = (a: SessionSummary, b: SessionSummary): number => {
const aLive = this.runtimes.has(a.id) ? 1 : 0;
const bLive = this.runtimes.has(b.id) ? 1 : 0;