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
2 changes: 1 addition & 1 deletion kernel/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "acp-kernel",
"version": "0.0.102",
"version": "0.0.103",
"description": "Framework-agnostic context-compression engine (model-driven, 3-tier LSM). Pure core: no host dependency.",
"license": "MIT",
"author": "ranxianglei",
Expand Down
24 changes: 19 additions & 5 deletions kernel/src/cache-report.ts
Original file line number Diff line number Diff line change
Expand Up @@ -162,9 +162,17 @@ export interface FoldEconomics {
perTurnSavingUnits: number;
/** Breakeven turns n* = max(0,oneTimeCost)/perTurnSaving; null when S ≤ σ. */
breakevenTurns: number | null;
/** paidBack: measured cadence reached the breakeven point.
* null while unobservable (no post-fold request yet / S ≤ σ). */
/** paidBack: lifetime payback verdict — post-fold requests observed so far
* (requestsAfter) reached the breakeven point n*. Same window as
* savedSoFar, so the verdict and the token figures on one row agree
* (billion-context#2044). null while unobservable (S ≤ σ). */
paidBack: boolean | null;
/** cadenceOk: the original short-window verdict — folds until the NEXT
* fold (turnsToNextFold) reached n*. This is an interleave-cadence
* signal (folded too soon after the previous one), NOT a payback
* verdict; null while there is no next fold. Guard-signal candidate for
* billion-context#1280 / #2044. */
cadenceOk: boolean | null;
}

export interface EconomicsSummary {
Expand Down Expand Up @@ -255,7 +263,12 @@ export function computeFoldEconomics(
perTurnSavingUnits > 0
? Math.max(0, oneTimeCostUnits) / perTurnSavingUnits
: null;
// #2044: paidBack uses the FULL post-fold window (requestsAfter), the
// same window savedSoFar uses, so verdict and token figures agree.
// The old short-window verdict survives as cadenceOk (diagnostics only).
const paidBack =
breakevenTurns !== null ? f.requestsAfter >= breakevenTurns : null;
const cadenceOk =
f.turnsToNextFold !== null && breakevenTurns !== null
? f.turnsToNextFold >= breakevenTurns
: null;
Expand All @@ -275,6 +288,7 @@ export function computeFoldEconomics(
perTurnSavingUnits: round1(perTurnSavingUnits),
breakevenTurns,
paidBack,
cadenceOk,
};
}

Expand Down Expand Up @@ -523,7 +537,7 @@ function formatCacheReportFull(report: CacheReport): string {
);
for (const f of report.folds) {
const nstar =
f.breakevenTurns === null ? "n/a" : f.breakevenTurns.toFixed(1);
f.breakevenTurns === null ? "n/a" : f.breakevenTurns.toFixed(2);
const k = f.turnsToNextFold === null ? "—" : String(f.turnsToNextFold);
const verdict =
f.paidBack === null ? "?" : f.paidBack ? "PAID BACK" : "NOT PAID BACK";
Expand Down Expand Up @@ -612,12 +626,12 @@ function formatCacheReportSummary(report: CacheReport): string {
);
if (e.notPaidBackCount > 0)
out.push(
` NOT PAID BACK = measured post-fold cadence never reached n* (end-of-session / back-to-back folds — one-time cost, not data loss)`,
` NOT PAID BACK = post-fold requests so far have not reached n* (early fold or session ended before payback — one-time cost, not data loss)`,
);
if (report.folds.length <= SUMMARY_FOLD_TOPS * 2) {
for (const f of report.folds) {
const nstar =
f.breakevenTurns === null ? "n/a" : f.breakevenTurns.toFixed(1);
f.breakevenTurns === null ? "n/a" : f.breakevenTurns.toFixed(2);
const k = f.turnsToNextFold === null ? "—" : String(f.turnsToNextFold);
const h = f.hPct === null ? "n/a" : `${f.hPct.toFixed(1)}%`;
out.push(
Expand Down
121 changes: 118 additions & 3 deletions kernel/tests/cache-report.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -148,7 +148,7 @@ test("multiple folds in one window: earliest divergence point wins", () => {
assert.equal(r.folds[1].T, 5_000);
});

test("fold without post-fold request is unobserved", () => {
test("fold without post-fold request: zero one-time cost is instantly paid back, cadence unobserved (#2044)", () => {
const samples = [sample(0, 10_000, 9_000)];
const folds: FoldEvent[] = [
{
Expand All @@ -161,11 +161,33 @@ test("fold without post-fold request is unobserved", () => {
const f = r.folds[0];
assert.equal(f.hPct, null);
assert.equal(f.T, 0);
// S=50K σ=0 T=0 → ΔC₁ = −500 → n* = 0 → trivially paid back.
assert.equal(f.paidBack, true);
// No next fold yet → cadence stays unobserved.
assert.equal(f.cadenceOk, null);
assert.equal(r.economics.paidBackCount, 1);
assert.equal(r.economics.unobservedCount, 0);
});

test("fold with S ≤ σ has null verdict (unobservable payback horizon)", () => {
const samples = [sample(0, 70_000, 60_000), sample(1, 22_000, 5_000)];
const folds: FoldEvent[] = [
{
at: T0 + 0.5 * min,
tokensCompressed: 2_000,
summaryTokens: 3_000,
firstFoldStartTokens: 5_000,
},
];
const r = buildCacheReport(samples, folds);
const f = r.folds[0];
assert.equal(f.breakevenTurns, null);
assert.equal(f.paidBack, null);
assert.equal(f.cadenceOk, null);
assert.equal(r.economics.unobservedCount, 1);
});

test("breakeven verdict: measured cadence vs n*", () => {
test("payback verdict uses the full post-fold window (#2044)", () => {
// S=50K σ=2K, X=5K. Post-fold request: input 22667, cached 7000 → missed
// 15667 = compRepay (excess 17667 clamps to it). Defaults w=1,r=0.1,q=4:
// ΔC₁ = 0.9·15667 + 4·2000 − 0.1·50000 ≈ 17100 ; Δs = 48000·0.1 = 4800 → n*≈3.6
Expand All @@ -188,7 +210,83 @@ test("breakeven verdict: measured cadence vs n*", () => {
f.breakevenTurns !== null && f.breakevenTurns > 0 && f.breakevenTurns < 10,
);
assert.equal(f.turnsToNextFold, null);
assert.equal(f.paidBack, null);
// requestsAfter = 1 < n*≈3.6 → NOT PAID BACK under the full window
// (previously unobserved forever — the last fold can now be evaluated).
assert.equal(f.paidBack, false);
assert.equal(f.cadenceOk, null);
assert.equal(r.economics.notPaidBackCount, 1);
});

test("last fold with enough post-fold requests flips to PAID BACK (#2044)", () => {
const samples = [
sample(0, 70_000, 60_000),
sample(1, 22_667, 7_000),
sample(2, 24_000, 22_000),
sample(3, 26_000, 24_000),
sample(4, 28_000, 26_000),
sample(5, 30_000, 28_000),
];
const folds: FoldEvent[] = [
{
at: T0 + 0.5 * min,
tokensCompressed: 50_000,
summaryTokens: 2_000,
firstFoldStartTokens: 5_000,
viewBefore: 70_000,
viewAfter: 20_000,
},
];
const r = buildCacheReport(samples, folds);
const f = r.folds[0];
assert.equal(f.turnsToNextFold, null);
assert.equal(f.requestsAfter, 5);
assert.ok(f.breakevenTurns !== null && f.requestsAfter >= f.breakevenTurns);
assert.equal(f.paidBack, true);
assert.equal(f.cadenceOk, null);
});

test("back-to-back fold: lifetime verdict PAID BACK, cadence NOT ok (#2044 regression)", () => {
// The incident shape from the report: a fold that paid back long ago got
// NOT PAID BACK purely because the NEXT fold arrived in the same batch
// (k small), while savedSoFar on the same row kept growing.
const samples = [
sample(0, 70_000, 60_000),
sample(1, 22_667, 7_000),
sample(2, 24_000, 22_000),
sample(3, 26_000, 24_000),
sample(4, 28_000, 26_000),
sample(5, 30_000, 28_000),
sample(6, 32_000, 30_000),
sample(7, 34_000, 32_000),
sample(8, 36_000, 34_000),
sample(9, 38_000, 36_000),
];
const folds: FoldEvent[] = [
{
at: T0 + 0.5 * min,
tokensCompressed: 50_000,
summaryTokens: 2_000,
firstFoldStartTokens: 5_000,
viewBefore: 70_000,
viewAfter: 20_000,
},
{
// arrives in the same batch, right after one post-fold request of fold #1
at: T0 + 1.5 * min,
tokensCompressed: 10_000,
summaryTokens: 800,
firstFoldStartTokens: 3_000,
},
];
const r = buildCacheReport(samples, folds);
const f1 = r.folds[0];
assert.equal(f1.turnsToNextFold, 1); // short window: the next fold came early
assert.equal(f1.requestsAfter, 9); // full window: kept counting
// Old behavior: 1 < n*≈3.6 → NOT PAID BACK despite savedSoFar ≈ 48K×9.
// New behavior: 9 ≥ 3.6 → PAID BACK; the too-soon signal moves to cadenceOk.
assert.equal(f1.paidBack, true);
assert.equal(f1.cadenceOk, false);
assert.equal(r.economics.paidBackCount >= 1, true);
});

test("economics summary aggregates fold economics", () => {
Expand Down Expand Up @@ -345,8 +443,25 @@ test("computeFoldEconomics matches #359 formulas under default profile", () => {
assert.equal(e.perTurnSavingUnits, 4_800);
assert.ok(Math.abs(e.breakevenTurns! - 18_300 / 4_800) < 1e-9);
assert.equal(e.paidBack, true);
assert.equal(e.cadenceOk, true);
assert.equal(e.savedSoFar, 48_000 * 21);
assert.equal(e.netTokenDelta, 17_000 + 2_000 - 50_000);

// #2044: the incident shape — next fold arrives immediately (k=0) but the
// fold has long since paid back across the full post-fold horizon.
const e2 = computeFoldEconomics({
seq: 1,
at: 1_000_000,
S: 50_000,
sigma: 2_000,
hPct: 33,
T: 17_000,
requestsAfter: 21,
turnsToNextFold: 0,
});
assert.equal(e2.paidBack, true);
assert.equal(e2.cadenceOk, false);
assert.equal(e2.savedSoFar, 48_000 * 21);
});

function round1(n: number): number {
Expand Down
9 changes: 7 additions & 2 deletions tests/cache-ledger.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -139,7 +139,7 @@ test("incremental k spans ALL intermediate samples (parity with batch, #1286)",
for (let i = 0; i < inc.folds.length; i++) {
const a = inc.folds[i]!;
const b = batch.folds[i]!;
for (const key of ["seq", "at", "S", "sigma", "Vprime", "hPct", "T", "requestsAfter", "savedSoFar", "turnsToNextFold", "netTokenDelta", "oneTimeCostUnits", "perTurnSavingUnits", "breakevenTurns", "paidBack"] as const) {
for (const key of ["seq", "at", "S", "sigma", "Vprime", "hPct", "T", "requestsAfter", "savedSoFar", "turnsToNextFold", "netTokenDelta", "oneTimeCostUnits", "perTurnSavingUnits", "breakevenTurns", "paidBack", "cadenceOk"] as const) {
assert.deepEqual(a[key], b[key], `fold ${i} ${key}: incremental ${String(a[key])} !== batch ${String(b[key])}`);
}
}
Expand All @@ -157,10 +157,15 @@ test("incremental k spans ALL intermediate samples (parity with batch, #1286)",
assert.equal(f1.T, 0);
assert.ok(Math.abs(f1.breakevenTurns! - 2.5008976660682225) < 1e-9);
assert.equal(f1.paidBack, true);
assert.equal(f1.cadenceOk, true);
assert.equal(f1.savedSoFar, (12140 - 1000) * 3);
const f2 = inc.folds.find((f) => f.seq === 2)!;
assert.equal(f2.turnsToNextFold, null);
assert.equal(f2.paidBack, null);
// #2044: the last fold is now evaluable on the full post-fold window —
// zero post-fold requests against a positive n* is NOT PAID BACK (yet),
// no longer unobserved; cadenceOk stays null with no next fold.
assert.equal(f2.paidBack, false);
assert.equal(f2.cadenceOk, null);
assert.equal(f2.requestsAfter, 0);
assert.equal(inc.totals.balanced, true);
});
Expand Down
Loading