From 3be6b6d19bbd44bc51b49b8e3671d44d022e789d Mon Sep 17 00:00:00 2001 From: ework-agent Date: Thu, 3 Sep 2026 23:48:03 +0800 Subject: [PATCH] fix: exclude anonymous pfa sessions from legacy-id migration (#499 prerequisite) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit migrateLegacyIds() re-keys sessions whose stored label differs from their id (#286 cleanup), deleting all but the newest sibling per label. Anonymous prefix-affinity sessions (#309) all carry the shared display label "prefix-affinity" != their pfa- id, so every boot re-keyed them all to the single id "prefix-affinity" and deleted every sibling but the newest — silent loss of saved compression state for anonymous clients on every restart. Exclude pfa- ids from the migration. True legacy derived-hash sessions are still re-keyed as before (regression-tested alongside the guard). Prerequisite for #504 P1a affinity-snapshot persistence: without it, routing restored across restarts lands on session files that no longer exist, so the folded view cannot be rebuilt. --- src/persist.ts | 7 ++++++ tests/proxy-persist.test.ts | 44 +++++++++++++++++++++++++++++++++++++ 2 files changed, 51 insertions(+) diff --git a/src/persist.ts b/src/persist.ts index d1dee94e74..23686899dd 100644 --- a/src/persist.ts +++ b/src/persist.ts @@ -215,6 +215,13 @@ export class SessionStore { const byLabel = new Map(); let unlabeled = 0; for (const [id, envelope] of loaded) { + // #499: anonymous prefix-affinity sessions (#309) carry the shared + // display label "prefix-affinity" ≠ their pfa- id — they are + // CURRENT-format, not legacy derived-hash sessions. Migrating them + // would rekey every pfa session to the single id "prefix-affinity" + // and DELETE all but the newest sibling on every boot (silent data + // loss of saved compression state). + if (id.startsWith("pfa-")) continue; const session = buildSession(envelope.payload); const label = session.meta.label; if (!label) { diff --git a/tests/proxy-persist.test.ts b/tests/proxy-persist.test.ts index 8cb719554a..bd26f51b03 100644 --- a/tests/proxy-persist.test.ts +++ b/tests/proxy-persist.test.ts @@ -383,3 +383,47 @@ await withTempStore("one-time migration re-keys legacy hash-id sessions to their cold.cancelAll(); } }); + +await withTempStore("migration leaves anonymous pfa sessions untouched (#499)", async (store, dir) => { + // Anonymous prefix-affinity sessions (#309) all share the display label + // "prefix-affinity" ≠ their id, which trips the #286 self-termination + // invariant: without the guard, every boot re-keys them all to the single + // id "prefix-affinity" and deletes every sibling but the newest — silent + // loss of saved compression state for anonymous clients. + const mk = (id: string, requests: number) => { + const s = makeSession(id); + s.meta.label = "prefix-affinity"; + s.meta.protocol = "openai"; + s.meta.upstreamOrigin = "https://relay.example/v1"; + s.stats.requests = requests; + return s; + }; + await store.writeNow(mk("pfa-bc1eaaaaaaaaaaaa", 7)); + await store.writeNow(mk("pfa-8588bbbbbbbbbbbb", 3)); + + const fileB = jsonFilesUnder(dir).find((f) => JSON.parse(readFileSync(f, "utf8")).id === "pfa-8588bbbbbbbbbbbb")!; + const envB = JSON.parse(readFileSync(fileB, "utf8")); + envB.savedAt -= 1000; // bc1e is newer → it would win a label collision if migrated + writeFileSync(fileB, JSON.stringify(envB)); + + const legacy = makeSession("legacy-" + "c".repeat(24)); + legacy.meta.label = "conv-legacy-499"; + legacy.meta.protocol = "responses"; + legacy.meta.upstreamOrigin = "https://chatgpt.com"; + legacy.stats.requests = 2; + await store.writeNow(legacy); + + const cold = new SessionStore({ dir, debounceMs: 5, enabled: true }); + try { + await cold.migrateLegacyIds(); + const all = await cold.loadAll(); + assert.ok(all.has("pfa-bc1eaaaaaaaaaaaa"), "newer anonymous session survives"); + assert.ok(all.has("pfa-8588bbbbbbbbbbbb"), "older anonymous sibling survives (no shared-label deletion)"); + assert.equal(all.get("pfa-bc1eaaaaaaaaaaaa")!.stats.requests, 7, "state intact"); + assert.ok(!all.has("legacy-" + "c".repeat(24)), "true legacy session is still re-keyed"); + assert.ok(all.has("conv-legacy-499"), "legacy re-key still works alongside the guard"); + assert.equal(all.size, 3, "exactly the two pfa sessions plus the re-keyed legacy one"); + } finally { + cold.cancelAll(); + } +});