mirror of
https://github.com/ranxianglei/billion-context.git
synced 2026-11-26 01:11:05 +08:00
- #498: noteWeakOverflow fires on stream truncation / upstream errors at >=90% window usage; 3 events in 15min learn a shrink-only conservative window and arm the emergency shrink (strong 4xx signals remain the primary teacher). Hooks in loop/core.ts (both adapters' truncation and UpstreamHttpError) and both plugin passthrough pipes (terminal-event detection per wire format). - #499 P1a: prefix-affinity snapshot persisted to ${XDG_STATE_HOME}/billion-context/prefix-affinity.json (atomic write, 5s debounce, flush on shutdown, hydrate on startup) so a proxy restart no longer orphans anonymous sessions. - #499 P1b: cache-warn detects prompt-cache collapse (5 consecutive large requests at ~0% hit after earlier >=50% hits) and logs a diagnostic pointer at #499 causes. Tests: 20 new (weak-overflow, plugin-truncation, cache-warn, affinity-persist). Full suite 1018/1020 (2 known env fails in plugin-agent.test.ts).
63 lines
2.3 KiB
TypeScript
63 lines
2.3 KiB
TypeScript
import { test, beforeEach, afterEach } from "node:test";
|
|
import assert from "node:assert/strict";
|
|
import { warnCacheCollapse } from "../src/cache-warn.ts";
|
|
import { setLogCapture } from "../src/logger.ts";
|
|
import type { Session } from "../src/session.ts";
|
|
|
|
const warnings: string[] = [];
|
|
|
|
beforeEach(() => {
|
|
warnings.length = 0;
|
|
setLogCapture((level, msg) => {
|
|
if (level === "warn") warnings.push(msg);
|
|
});
|
|
});
|
|
|
|
afterEach(() => {
|
|
setLogCapture(null);
|
|
});
|
|
|
|
function makeSession(): Session {
|
|
return {
|
|
id: `cw-${Math.random().toString(36).slice(2, 8)}`,
|
|
metadata: {},
|
|
stats: { lastInputTokens: 0 },
|
|
} as unknown as Session;
|
|
}
|
|
|
|
test("collapse after a healthy cache warns exactly once", () => {
|
|
const session = makeSession();
|
|
warnCacheCollapse(session, 50000, 45000);
|
|
for (let i = 0; i < 10; i++) warnCacheCollapse(session, 50000, 100);
|
|
const hits = warnings.filter((w) => w.includes("prompt-cache collapse"));
|
|
assert.equal(hits.length, 1, "warns once");
|
|
assert.match(hits[0]!, /input=50000, cached=100/);
|
|
});
|
|
|
|
test("no warning without a prior high-hit sample", () => {
|
|
const session = makeSession();
|
|
for (let i = 0; i < 10; i++) warnCacheCollapse(session, 50000, 0);
|
|
assert.equal(warnings.filter((w) => w.includes("prompt-cache collapse")).length, 0);
|
|
});
|
|
|
|
test("a mid-run partial hit resets the low streak", () => {
|
|
const session = makeSession();
|
|
warnCacheCollapse(session, 50000, 45000);
|
|
warnCacheCollapse(session, 50000, 0);
|
|
warnCacheCollapse(session, 50000, 0);
|
|
warnCacheCollapse(session, 50000, 0);
|
|
warnCacheCollapse(session, 50000, 0);
|
|
warnCacheCollapse(session, 50000, 10000, );
|
|
for (let i = 0; i < 4; i++) warnCacheCollapse(session, 50000, 0);
|
|
assert.equal(warnings.filter((w) => w.includes("prompt-cache collapse")).length, 0, "streak broken at 4 < 5");
|
|
warnCacheCollapse(session, 50000, 0);
|
|
assert.equal(warnings.filter((w) => w.includes("prompt-cache collapse")).length, 1, "a full second streak warns");
|
|
});
|
|
|
|
test("small inputs are never counted", () => {
|
|
const session = makeSession();
|
|
warnCacheCollapse(session, 50000, 45000);
|
|
for (let i = 0; i < 10; i++) warnCacheCollapse(session, 500, 0);
|
|
assert.equal(warnings.filter((w) => w.includes("prompt-cache collapse")).length, 0);
|
|
});
|