Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 7 additions & 9 deletions src/affinity-persist.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import fs from "node:fs";
import path from "node:path";
import { prefixAffinity, type AffinitySnapshotEntry, MAX_TRACKED_SESSIONS, TTL_MS } from "./prefix-affinity.js";
import { prefixAffinity, type AffinitySnapshotEntry, MAX_TRACKED_SESSIONS } from "./prefix-affinity.js";
import { stateDir } from "./paths.js";
import { log } from "./logger.js";

Expand All @@ -9,7 +9,7 @@ import { log } from "./logger.js";
* pure in-memory (#309), so a proxy restart orphaned every anonymous session:
* the next replay forked a fresh session with zero compression state and
* resent the raw history (#351 — 458K tokens, 0% cache). The chains are
* small (≤256 sessions × ≤128 hashes); persist them to the state dir with a
* small (≤1024 sessions × ≤128 hashes); persist them to the state dir with a
* debounced atomic write and hydrate on boot.
*/

Expand Down Expand Up @@ -70,15 +70,13 @@ function mergeWithDisk(
}

/** The union is monotonic (nothing removes disk-only chains), so without
* re-applying the store's own bounds the file would grow one entry per
* re-applying the store's own bound the file would grow one entry per
* chain that churns through the LRU cap over a process's lifetime. Mirror
* importSnapshot's read-time hygiene on the write path: drop TTL-expired
* chains and keep at most MAX_TRACKED_SESSIONS, most-recently-seen first. */
* the store's semantics on the write path (#1724 permanence: chains never
* expire with time — only the LRU cap applies, most-recently-seen first). */
function normalizeForWrite(entries: AffinitySnapshotEntry[]): AffinitySnapshotEntry[] {
const now = Date.now();
const live = entries.filter((e) => now - e.lastSeen <= TTL_MS);
if (live.length <= MAX_TRACKED_SESSIONS) return live;
return [...live].sort((a, b) => b.lastSeen - a.lastSeen).slice(0, MAX_TRACKED_SESSIONS);
if (entries.length <= MAX_TRACKED_SESSIONS) return entries;
return [...entries].sort((a, b) => b.lastSeen - a.lastSeen).slice(0, MAX_TRACKED_SESSIONS);
}

function writeSnapshot(): void {
Expand Down
43 changes: 15 additions & 28 deletions src/prefix-affinity.ts
Original file line number Diff line number Diff line change
Expand Up @@ -50,22 +50,23 @@ import { createHash } from "node:crypto";
const MIN_CANONICAL_BYTES = 24;

/** Upper bound on tracked chains (LRU-evicted, global — content is the
* only key, so there are no per-credential buckets). Exported so the
/** Upper bound on tracked chains (LRU-evicted, global — content is the
* only key, so there are no per-credential buckets). Chains are PERMANENT
* (#1724): the product promise is month- to year-level single sessions, so
* validity never expires with time — a chain leaves the table only under
* capacity pressure (least-recently-used first). 1024 × ≤128 hashes ≈ 8MB
* worst case; typical chains are far shallower. Still exported so the
* persistence layer (#499 P1a / #1724) can enforce the same bound on the
* on-disk snapshot it writes back. */
export const MAX_TRACKED_SESSIONS = 256;

/** Chains unused for this long stop matching (sessions may outlive tracking).
* Exported so the persistence layer applies the same expiry when writing. */
export const TTL_MS = 7 * 24 * 60 * 60 * 1000;
export const MAX_TRACKED_SESSIONS = 1024;

/** Leading-run length used to attribute a NEW anonymous session's birth to a
* truncated replay of a tracked chain (#1115: lineage attribution ONLY —
* never affects matching). */
const TRUNCATION_LINEAGE_WINDOW = 8;

/** Per tracked chain, store at most this many per-item hashes (the trailing
* ones). Bounds memory (256 chains × 128 × 64B ≈ 2MB) and keeps lineage
* ones). Bounds memory (1024 chains × 128 × 64B ≈ 8MB) and keeps lineage
* lookups (fork-LCP + truncated-run, both ≤ 8 items) comfortably available.
* Chains deeper than this lose their head, so fork-lineage LCP detection
* degrades to "unknown" rather than guessing. */
Expand Down Expand Up @@ -240,7 +241,6 @@ export class PrefixAffinityResolver {
const incItemHashes = perItemHashes(messages);
const storedItemHashes = incItemHashes.slice(-MAX_STORED_ITEMS);
const tracked = this.trackedChains;
this.expire(tracked);

// 1. Full-depth prefix match (the original radix-style resolution).
let best: ChainEntry | undefined;
Expand Down Expand Up @@ -324,15 +324,12 @@ export class PrefixAffinityResolver {
}
}

/** #1486: current tracking entry for a session id (TTL-aware), without
/** #1486: current tracking entry for a session id, without
* touching lastSeen. Lets the identified-session tracker apply the
* append-only discipline (#1075 side requests reuse a session id with
* fewer messages) before overwriting. */
peekChain(sessionId: string): ChainEntry | undefined {
const entry = this.trackedChains.get(sessionId);
if (!entry) return undefined;
if (Date.now() - entry.lastSeen > TTL_MS) return undefined;
return entry;
return this.trackedChains.get(sessionId);
}

/** #1486: fingerprint an incoming message list without resolving it —
Expand Down Expand Up @@ -370,7 +367,6 @@ export class PrefixAffinityResolver {
if (!hasUserMessage(msgs)) return null;
const hashes = chainHashes(msgs);
if (hashes.length < MIN_RESUME_PREFIX) return null;
this.expire(this.trackedChains);
let best: { sessionId: string; sharedDepth: number; lastSeen: number } | null = null;
for (const [id, entry] of this.trackedChains) {
if (id === selfSessionId) continue;
Expand Down Expand Up @@ -405,20 +401,19 @@ export class PrefixAffinityResolver {
}));
}

/** Load chains persisted by a previous process. Entries older than the
* TTL are dropped (they would not match anyway). Returns the count
* actually imported. Defensive: a corrupt/hand-edited file must never
* crash the proxy — malformed entries are skipped. */
/** Load chains persisted by a previous process. Chains are permanent
* (#1724): an entry months old still reattaches its session. Returns
* the count actually imported. Defensive: a corrupt/hand-edited file
* must never crash the proxy — malformed entries are skipped. */
importSnapshot(entries: unknown): number {
if (!Array.isArray(entries)) return 0;
const now = Date.now();
let imported = 0;
for (const raw of entries) {
if (!raw || typeof raw !== "object") continue;
const e = raw as Record<string, unknown>;
if (typeof e.sessionId !== "string" || typeof e.depth !== "number" || typeof e.tailHash !== "string") continue;
if (!Array.isArray(e.itemHashes) || e.itemHashes.some((h) => typeof h !== "string")) continue;
if (typeof e.lastSeen !== "number" || now - e.lastSeen > TTL_MS) continue;
if (typeof e.lastSeen !== "number") continue;
const entry: ChainEntry = {
sessionId: e.sessionId,
depth: e.depth,
Expand All @@ -438,14 +433,6 @@ export class PrefixAffinityResolver {
}
return imported;
}

private expire(tracked: Map<string, ChainEntry>): void {
if (tracked.size === 0) return;
const now = Date.now();
for (const [id, entry] of tracked) {
if (now - entry.lastSeen > TTL_MS) tracked.delete(id);
}
}
}

/** Shared resolver instance (per proxy process). */
Expand Down
30 changes: 24 additions & 6 deletions tests/affinity-persist.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import assert from "node:assert/strict";
import fs from "node:fs";
import os from "node:os";
import path from "node:path";
import { PrefixAffinityResolver, prefixAffinity, MAX_TRACKED_SESSIONS, TTL_MS } from "../src/prefix-affinity.ts";
import { PrefixAffinityResolver, prefixAffinity, MAX_TRACKED_SESSIONS } from "../src/prefix-affinity.ts";
import { flushPrefixAffinity, hydratePrefixAffinity } from "../src/affinity-persist.ts";

let tmp: string;
Expand Down Expand Up @@ -35,10 +35,12 @@ test("exportSnapshot/importSnapshot roundtrip preserves resolution", () => {
assert.equal(replay.sessionId, first.sessionId, "rehydrated chain still resolves to the same session");
});

test("importSnapshot drops malformed and expired entries", () => {
test("importSnapshot drops malformed entries; aged chains stay permanent (#1724)", () => {
const r = new PrefixAffinityResolver();
assert.equal(r.importSnapshot("nope"), 0);
assert.equal(r.importSnapshot([{ sessionId: 1 }, { sessionId: "x", depth: "3" }, null, { sessionId: "ok", depth: 3, tailHash: "t", itemHashes: ["h1", "h2"], lastSeen: Date.now() - 8 * 24 * 60 * 60 * 1000 }]), 0, "malformed + expired are skipped");
assert.equal(r.importSnapshot([{ sessionId: 1 }, { sessionId: "x", depth: "3" }, null]), 0, "malformed are skipped");
// 400 days old — the month/year single-session promise keeps it valid.
assert.equal(r.importSnapshot([{ sessionId: "ok", depth: 3, tailHash: "t", itemHashes: ["h1", "h2"], lastSeen: Date.now() - 400 * 24 * 60 * 60 * 1000 }]), 1, "permanent chains import regardless of age");
});

test("flush writes the snapshot file; hydrate reattaches after a restart", () => {
Expand Down Expand Up @@ -108,7 +110,7 @@ test("merge keeps the deeper disk copy when our in-memory snapshot is stale (#17
assert.equal(parsed.entries[0].depth, mine.depth + 2, "fresher disk copy not rolled back by our stale write");
});

test("writeSnapshot re-applies the store's bounds to the union (TTL + LRU cap) (#1724)", () => {
test("writeSnapshot re-applies the store's bounds to the union (LRU cap; chains are permanent) (#1724)", () => {
const a = prefixAffinity.resolve(messages(7));
assert.ok(a);
prefixAffinity.note(a.sessionId, a.incomingDepth, a.tailHash, a.itemHashes);
Expand All @@ -118,7 +120,7 @@ test("writeSnapshot re-applies the store's bounds to the union (TTL + LRU cap) (
for (let i = 0; i < MAX_TRACKED_SESSIONS + 50; i++) {
pads.push({ sessionId: `pfa-pad-${i}`, depth: 3, tailHash: `t${i}`, itemHashes: ["h"], lastSeen: now - (i + 1) * 1000 });
}
pads.push({ sessionId: "pfa-expired", depth: 3, tailHash: "te", itemHashes: ["h"], lastSeen: now - TTL_MS - 60_000 });
pads.push({ sessionId: "pfa-oldest", depth: 3, tailHash: "ta", itemHashes: ["h"], lastSeen: now - 400 * 24 * 60 * 60 * 1000 });
const file = path.join(tmp, "billion-context", "prefix-affinity.json");
fs.mkdirSync(path.dirname(file), { recursive: true });
fs.writeFileSync(file, JSON.stringify({ version: 1, entries: pads }));
Expand All @@ -127,11 +129,27 @@ test("writeSnapshot re-applies the store's bounds to the union (TTL + LRU cap) (
const parsed = JSON.parse(fs.readFileSync(file, "utf8")) as { entries: { sessionId: string }[] };
const ids = new Set(parsed.entries.map((e) => e.sessionId));
assert.ok(ids.has(a.sessionId), "our own chain survives the capped write");
assert.ok(!ids.has("pfa-expired"), "TTL-expired disk entries are pruned at write time");
assert.ok(!ids.has("pfa-oldest"), "when the cap bites, the least-recently-seen entry is the first to go");
assert.ok(!ids.has(`pfa-pad-${MAX_TRACKED_SESSIONS + 49}`), "oldest disk-only chains are LRU-capped at write time");
assert.equal(parsed.entries.length, MAX_TRACKED_SESSIONS, "file stays bounded at the tracked-chain cap instead of growing per churned chain");
});

test("chains are permanent at write time — age alone never prunes (#1724)", () => {
const now = Date.now();
const file = path.join(tmp, "billion-context", "prefix-affinity.json");
fs.mkdirSync(path.dirname(file), { recursive: true });
fs.writeFileSync(file, JSON.stringify({ version: 1, entries: [
{ sessionId: "pfa-year-old", depth: 5, tailHash: "ty", itemHashes: ["h"], lastSeen: now - 400 * 24 * 60 * 60 * 1000 },
{ sessionId: "pfa-fresh", depth: 2, tailHash: "tf", itemHashes: ["h"], lastSeen: now },
] }));

flushPrefixAffinity();
const parsed = JSON.parse(fs.readFileSync(file, "utf8")) as { entries: { sessionId: string }[] };
const ids = new Set(parsed.entries.map((e) => e.sessionId));
assert.ok(ids.has("pfa-year-old"), "a 400-day-old chain survives the write — permanence means only the LRU cap, never age");
assert.ok(ids.has("pfa-fresh"), "fresh entry untouched");
});

test("a malformed disk entry cannot win a same-depth merge or persist (#1724)", () => {
const a = prefixAffinity.resolve(messages(6));
assert.ok(a);
Expand Down
4 changes: 2 additions & 2 deletions tests/prefix-affinity.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -161,11 +161,11 @@ test("prefix-affinity: mid-conversation system items remain identity data (#1148

test("prefix-affinity: LRU cap bounds tracked sessions", () => {
const r = new PrefixAffinityResolver();
for (let i = 0; i < 262; i++) {
for (let i = 0; i < 1030; i++) {
const a = r.resolve([user(`unique conversation number ${i} with filler content`)]);
r.note(a!.sessionId, a!.incomingDepth, a!.tailHash, a!.itemHashes);
}
assert.equal(r.trackedSessionIds().length, 256);
assert.equal(r.trackedSessionIds().length, 1024);
});

test("prefix-affinity: stableStringify sorts keys recursively", () => {
Expand Down
Loading