Skip to content
Open
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
18 changes: 10 additions & 8 deletions src/order-persist.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import { stateDir } from "./paths.js";
import { log } from "./logger.js";
import { persistDebounceMs, persistMaxDelayMs } from "./knobs.js";
import { kvStore } from "./persist.js";
import { EXT_VALUE_MAX_BYTES } from "./storage/store.js";

/**
* #3397: session-order skeleton persistence. The resolver's recorded
Expand Down Expand Up @@ -113,15 +114,16 @@ function mergeWithDisk(mine: OrderSkeletonEntry[], disk: OrderSkeletonEntry[]):
* one entry per conversation that churns through the lifetime. Frags can also
* grow with turn count — the byte budget keeps the debounced serialization
* cheap even for a pathological snapshot (freshest entries kept first). */
function normalizeForWrite(entries: OrderSkeletonEntry[]): OrderSkeletonEntry[] {
const capped = entries.length <= MAX_ORDER_ENTRIES
? entries
: [...entries].sort((a, b) => b.lastSeen - a.lastSeen).slice(0, MAX_ORDER_ENTRIES);
function normalizeForWrite(entries: OrderSkeletonEntry[], maxBytes = MAX_SNAPSHOT_BYTES): OrderSkeletonEntry[] {
// The byte budget can bind well before the entry-count cap, so order the
// union by recency FIRST: otherwise a large history evicts the newest
// conversations merely because older records sit earlier in the array.
const capped = [...entries].sort((a, b) => b.lastSeen - a.lastSeen).slice(0, MAX_ORDER_ENTRIES);
const kept: OrderSkeletonEntry[] = [];
let size = JSON.stringify({ version: 1, entries: [] }).length;
for (const e of capped) {
const delta = JSON.stringify(e).length + 1;
if (kept.length > 0 && size + delta > MAX_SNAPSHOT_BYTES) break;
const delta = Buffer.byteLength(JSON.stringify(e), "utf8") + 1;
if (kept.length > 0 && size + delta > maxBytes) break;
kept.push(e);
size += delta;
}
Expand Down Expand Up @@ -173,7 +175,7 @@ function persistUnifiedNow(): void {
}
const mine = orderSnapshot();
const merged = live === null ? mine : mergeWithDisk(mine, live);
const entries = normalizeForWrite(merged);
const entries = normalizeForWrite(merged, EXT_VALUE_MAX_BYTES);
await store.setKvAsync(ORDER_NS, ORDER_KEY, { version: 1, entries });
}).catch((err) => {
kvWarnOnce("persist", err);
Expand Down Expand Up @@ -213,7 +215,7 @@ export async function hydrateOrderUnified(): Promise<void> {
// One-time migration: file rows missing from the store travel up once.
const kvIds = new Set(kvEntries.map((e) => e.id));
if (!hadRow || fileEntries.some((e) => !kvIds.has(e.id))) {
await store.setKvAsync(ORDER_NS, ORDER_KEY, { version: 1, entries: normalizeForWrite(merged) });
await store.setKvAsync(ORDER_NS, ORDER_KEY, { version: 1, entries: normalizeForWrite(merged, EXT_VALUE_MAX_BYTES) });
if (!hadRow && merged.length > 0) log("info", `[session-order] migrated ${merged.length} skeleton(s) from the legacy JSON file into unified storage`);
}
kvReady = true;
Expand Down
3 changes: 2 additions & 1 deletion src/storage/store.ts
Original file line number Diff line number Diff line change
Expand Up @@ -338,7 +338,8 @@ export interface IngestSessionInput {
* namespace by convention like "r3:" / "web:" / "lane:"), hostile by
* accident is what we guard, not hostile by design. */
const EXT_KEY_MAX = 256;
const EXT_VALUE_MAX_BYTES = 4 * 1024 * 1024;
/** Shared ceiling for producers of bounded kv / session_ext snapshots. */
export const EXT_VALUE_MAX_BYTES = 4 * 1024 * 1024;

function validateExtKey(value: string, what: string): void {
if (typeof value !== "string" || value.length === 0 || value.length > EXT_KEY_MAX) {
Expand Down
50 changes: 49 additions & 1 deletion tests/order-persist.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,8 @@ import assert from "node:assert/strict";
import fs from "node:fs";
import os from "node:os";
import path from "node:path";
import { flushOrderSkeletons, scheduleOrderPersist } from "../src/order-persist.ts";
import { flushOrderSkeletons, hydrateOrderSkeletons, scheduleOrderPersist, _resetOrderPersistForTest } from "../src/order-persist.ts";
import { applyOrderSnapshot, orderSnapshot, resetSessionOrderForTest, type OrderSkeletonEntry } from "../src/session-resolve.ts";

// #6058: the session-order snapshot writer had its quiesce/max-delay windows
// hardcoded (500ms / 5s) and never read the persist.* knobs — while the main
Expand Down Expand Up @@ -80,3 +81,50 @@ test("persist.maxDelayMs knob is honored — a small cap beats a huge debounce u
flushOrderSkeletons();
}
});

// #6255: the byte budget used to bind in array order (write order), so with
// fewer than 1,024 entries — where the recency sort was skipped — the OLDEST
// records were kept and the newest conversations were silently dropped. Pin
// both directions: the budget keeps the newest, and sorting a write never
// reorders the resident in-memory records.
test("the order snapshot byte budget keeps newest conversations below the entry-count cap", async () => {
_resetOrderPersistForTest();
resetSessionOrderForTest();
const kinds = Array.from({ length: 4_000 }, (_, i) => i % 2 === 0 ? "user" : "assistant");
const frags = Array.from({ length: 4_000 }, () => "a".repeat(64));
const entries = Array.from({ length: 40 }, (_, i): OrderSkeletonEntry => ({
id: `byte-budget-${i}`, kinds, frags, tagHash: "b".repeat(64), lastSeen: 1_000 + i,
}));
applyOrderSnapshot(entries);
const idsBefore = orderSnapshot().map((e) => e.id);
await flushOrderSkeletons();
const bytes = fs.readFileSync(orderFile());
const saved = JSON.parse(bytes.toString("utf8")) as { entries: OrderSkeletonEntry[] };
assert.ok(bytes.length <= 8 * 1024 * 1024);
assert.ok(saved.entries.length > 0 && saved.entries.length < entries.length, "byte budget, not the 1024-entry cap, truncated the snapshot");
const expected = [...entries].reverse().slice(0, saved.entries.length).map((e) => e.id);
assert.deepEqual(saved.entries.map((e) => e.id), expected);
assert.deepEqual(orderSnapshot().map((e) => e.id), idsBefore, "sorting a write does not reorder resident records");
resetSessionOrderForTest();
hydrateOrderSkeletons();
assert.deepEqual(orderSnapshot().map((e) => e.id), expected, "newest skeletons survive an actual file round trip");
});

test("order snapshot budget preserves all small entries and stable timestamp ties", async () => {
_resetOrderPersistForTest();
resetSessionOrderForTest();
const entries: OrderSkeletonEntry[] = [
{ id: "tie-a", kinds: ["user"], frags: ["a"], tagHash: "", lastSeen: 2 },
{ id: "older", kinds: ["assistant"], frags: ["b"], tagHash: "", lastSeen: 1 },
{ id: "tie-b", kinds: ["user"], frags: ["c"], tagHash: "", lastSeen: 2 },
];
applyOrderSnapshot(entries);
await flushOrderSkeletons();
const saved = JSON.parse(fs.readFileSync(orderFile(), "utf8")) as { entries: OrderSkeletonEntry[] };
assert.equal(saved.entries.length, entries.length);
assert.deepEqual(saved.entries.filter((e) => e.lastSeen === 2).map((e) => e.id), ["tie-a", "tie-b"]);
const savedById = new Map(saved.entries.map((e) => [e.id, e]));
for (const e of entries) {
assert.deepEqual(savedById.get(e.id), e, `entry ${e.id} preserved verbatim`);
}
});
46 changes: 46 additions & 0 deletions tests/order-unified-store.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -85,3 +85,49 @@ test("pre-hydration mutations fall back to the file, which hydration then import
assert.ok(row.entries.some((e) => e.id === "early-bird"), "late import covered the early file write");
assert.ok(orderSnapshot().some((e) => e.id === "early-bird"));
});

// #6255: the KV carrier rejects extension values over 4 MiB, but the producer
// used its 8 MiB legacy-file budget for KV writes — so a large snapshot made
// the one-time migration throw, demoted the process to the legacy file path
// forever, and the byte budget (unsorted) dropped the newest conversations.
// Pin both directions: migration AND steady-state writes stay within the KV
// ceiling and keep the newest skeletons.
test("unified order migration and writes keep newest skeletons within the KV byte budget", async (t) => {
_resetOrderPersistForTest();
resetSessionOrderForTest();
const kinds = Array.from({ length: 4_000 }, (_, i) => i % 2 === 0 ? "user" : "assistant");
const frags = Array.from({ length: 4_000 }, () => "a".repeat(64));
const entries = Array.from({ length: 40 }, (_, i) => ({
id: `unified-byte-budget-${i}`, kinds, frags, tagHash: "b".repeat(64), lastSeen: 1_000 + i,
}));
fs.mkdirSync(path.dirname(legacyFile), { recursive: true });
fs.writeFileSync(legacyFile, JSON.stringify({ version: 1, entries }));
await getStore().setKvAsync("session-order", "v1", { version: 1, entries: [] });
await hydrateOrderUnified();
const row = await kvRow();
assert.ok(Buffer.byteLength(JSON.stringify(row)) <= 4 * 1024 * 1024, "migrated KV row respects the store ceiling");
assert.ok(row.entries.length > 0 && row.entries.length < entries.length);
const expected = [...entries].reverse().slice(0, row.entries.length).map((e) => e.id);
assert.deepEqual(row.entries.map((e) => e.id), expected);
_resetOrderPersistForTest();
resetSessionOrderForTest();
fs.rmSync(legacyFile, { force: true });
await hydrateOrderUnified();
assert.deepEqual(orderSnapshot().map((e) => e.id), expected, "newest chains survive with only the KV carrier remaining");

const { applyOrderSnapshot } = await import("../src/session-resolve.js");
applyOrderSnapshot([{ ...entries[0], id: "newest-after-migration", lastSeen: 2_000 }]);
const store = getStore();
const write = store.setKvAsync.bind(store);
let written!: () => void;
const completed = new Promise<void>((resolve) => { written = resolve; });
t.mock.method(store, "setKvAsync", async (namespace: string, key: string, value: unknown) => {
await write(namespace, key, value);
if (namespace === "session-order" && key === "v1") written();
});
await flushOrderSkeletons();
await completed;
const updated = await kvRow();
assert.equal(updated.entries[0]?.id, "newest-after-migration");
assert.ok(Buffer.byteLength(JSON.stringify(updated)) <= 4 * 1024 * 1024, "steady-state KV write stays under the ceiling");
});
Loading