diff --git a/kernel/package-lock.json b/kernel/package-lock.json index 9ef81b290f..f57461d1fa 100644 --- a/kernel/package-lock.json +++ b/kernel/package-lock.json @@ -1,12 +1,12 @@ { "name": "acp-kernel", - "version": "0.0.127", + "version": "0.0.128", "lockfileVersion": 3, "requires": true, "packages": { "": { "name": "acp-kernel", - "version": "0.0.127", + "version": "0.0.128", "license": "MIT", "devDependencies": { "@types/node": "^22.0.0", diff --git a/kernel/package.json b/kernel/package.json index 3e057e1b3b..f927d49acf 100644 --- a/kernel/package.json +++ b/kernel/package.json @@ -1,6 +1,6 @@ { "name": "acp-kernel", - "version": "0.0.127", + "version": "0.0.128", "description": "Framework-agnostic context-compression engine (model-driven, 3-tier LSM). Pure core: no host dependency.", "license": "MIT", "author": "ranxianglei", diff --git a/kernel/src/recommend.ts b/kernel/src/recommend.ts index e85b67c119..8517176aeb 100644 --- a/kernel/src/recommend.ts +++ b/kernel/src/recommend.ts @@ -36,6 +36,7 @@ import { computeIntegrityWithdrawals } from "./turn-integrity.js"; import { evaluateRefold } from "./refold.js"; import { segmentGroups } from "./segment.js"; import { projectUnit, collectCoverage } from "./unit-projection.js"; +import { BoundaryNotFoundError } from "./boundaries.js"; import type { UnitProjection } from "./unit-projection.js"; // ─── Helpers ────────────────────────────────────────────────────────────────── @@ -566,9 +567,22 @@ export function screenRecommendedUnits( preExistingCoverage: coverage, protectedMessageIds: protectedRefs, }); - } catch { + } catch (err) { // Endpoints that cannot resolve on this array class (dead/consumed) - // must never be advertised. + // must never be advertised. BoundaryNotFoundError is that expected + // case (#3371 screens consumed units through here routinely) and stays + // silent; any OTHER exception is a programming error being swallowed + // into a silent demotion — the unit vanished with no trace at all + // (#3406 item 9). Surface it as a diagnostic (same channel as the + // config-validation warn in processTurn); behavior is unchanged — + // still return false, the unit is never advertised. + if (!(err instanceof BoundaryNotFoundError)) { + console.warn( + `[acp-kernel] screenRecommendedUnits dropped unit ${unit.startRef}..${unit.endRef} after projection error: ${ + err instanceof Error ? err.message : String(err) + }`, + ); + } return false; } if (!projection || projection.fatal !== null) return false; diff --git a/kernel/tests/issue3406-screen-observability.test.ts b/kernel/tests/issue3406-screen-observability.test.ts new file mode 100644 index 0000000000..49481f979f --- /dev/null +++ b/kernel/tests/issue3406-screen-observability.test.ts @@ -0,0 +1,171 @@ +// #3406 item 9: screenRecommendedUnits' catch swallowed EVERY projection +// exception into a silent `return false`. BoundaryNotFoundError is the +// expected dead/consumed-endpoint false (#3371 family) and must stay +// silent, but any OTHER exception is a programming error — before this +// change the unit vanished from the recommendation with no trace at all. +// Behavior is unchanged (still dropped, never advertised); only a +// console.warn diagnostic is added, mirroring the config-validation warn +// in processTurn (compress.ts). These tests pin both halves: the warn +// fires for unexpected errors, stays silent for boundary-not-found, and +// the screening VERDICT (dropped) never changes. + +import { test } from "node:test"; +import assert from "node:assert/strict"; +import { createInitialState } from "../src/state.js"; +import { assignRefs } from "../src/refs.js"; +import { screenRecommendedUnits } from "../src/recommend.js"; +import { defaultConfig } from "../src/config.js"; +import type { CompressibleRange, CoreMessage } from "../src/types.js"; + +function mk(id: string, role: CoreMessage["role"], len = 3000): CoreMessage { + return { id, role, contentType: "text", text: "x".repeat(len) }; +} + +function buildSession(): { + state: ReturnType; + all: CoreMessage[]; +} { + const state = createInitialState(); + const all = [ + mk("U0", "user"), + mk("W1", "assistant"), + mk("U2", "user"), + mk("W3", "assistant"), + ]; + state.messageRefs = assignRefs(all, { + existing: state.messageRefs, + nextIndex: 1, + }).map; + return { state, all }; +} + +/** Capture console.warn lines while fn runs (restored in finally). */ +function captureWarn(fn: () => T): { result: T; warns: string[] } { + const warns: string[] = []; + const real = console.warn; + console.warn = (...args: unknown[]) => { + warns.push( + args.map((a) => (typeof a === "string" ? a : String(a))).join(" "), + ); + }; + try { + return { result: fn(), warns }; + } finally { + console.warn = real; + } +} + +const malformedUnit: CompressibleRange = { + // Refs that cannot parse hit resolveBoundaries' plain "Invalid boundary + // ref(s)" Error — a shape the internal recommender can never produce, so + // inside the screen's catch it represents the programming-error class. + startRef: "zz-invalid", + endRef: "m00002", + count: 2, + tokens: 6000, +}; + +test("#3406: an unexpected projection error warns with unit refs + error summary — verdict unchanged (dropped)", () => { + const { state, all } = buildSession(); + const { result, warns } = captureWarn(() => + screenRecommendedUnits( + [malformedUnit], + all, + state, + defaultConfig(100_000), + new Set(), + ), + ); + assert.equal(result.length, 0, "unit is still dropped (behavior unchanged)"); + assert.equal(warns.length, 1, "exactly one diagnostic line per dropped unit"); + assert.match(warns[0]!, /screenRecommendedUnits dropped unit/); + assert.match( + warns[0]!, + /zz-invalid\.\.m00002/, + "line names the unit endpoints", + ); + assert.match( + warns[0]!, + /Invalid boundary ref/, + "line carries the error summary", + ); +}); + +test("#3406: unknown-endpoint units (BoundaryNotFoundError) drop silently — no warn spam for the expected false", () => { + const { state, all } = buildSession(); + const unknownUnit: CompressibleRange = { + startRef: "m09999", + endRef: "m01000", + count: 2, + tokens: 6000, + }; + const { result, warns } = captureWarn(() => + screenRecommendedUnits( + [unknownUnit], + all, + state, + defaultConfig(100_000), + new Set(), + ), + ); + assert.equal(result.length, 0, "unknown-ref unit is dropped"); + assert.equal( + warns.length, + 0, + "the expected dead-endpoint false stays silent", + ); +}); + +test("#3406: consumed-endpoint units (ref resolves, message not in the screened array) drop silently", () => { + const { state } = buildSession(); + // m00002 exists in the ref map but its message is absent from the array + // being screened (the pruned-view / array-class mismatch the comment + // describes) — BoundaryNotFoundError kind "consumed", expected false. + const screenedView: CoreMessage[] = [mk("U0", "user"), mk("W3", "assistant")]; + const consumedUnit: CompressibleRange = { + startRef: "m00002", + endRef: "m00002", + count: 1, + tokens: 3000, + }; + const { result, warns } = captureWarn(() => + screenRecommendedUnits( + [consumedUnit], + screenedView, + state, + defaultConfig(100_000), + new Set(), + ), + ); + assert.equal(result.length, 0, "consumed-ref unit is dropped"); + assert.equal( + warns.length, + 0, + "no diagnostic for the expected consumed false", + ); +}); + +test("#3406: healthy units still pass screening and emit nothing", () => { + const { state, all } = buildSession(); + const healthy: CompressibleRange = { + startRef: "m00001", + endRef: "m00002", + count: 2, + tokens: 6000, + }; + const { result, warns } = captureWarn(() => + screenRecommendedUnits( + [healthy], + all, + state, + { + ...defaultConfig(100_000), + preserveRecentMessages: 0, + preserveRecentTokens: 0, + }, + new Set(), + ), + ); + assert.equal(result.length, 1, "healthy unit stays advertised"); + assert.equal(warns.length, 0, "no diagnostic on the happy path"); +}); diff --git a/kernel/tests/issue3406-screen-scale.test.ts b/kernel/tests/issue3406-screen-scale.test.ts new file mode 100644 index 0000000000..46a79c6a03 --- /dev/null +++ b/kernel/tests/issue3406-screen-scale.test.ts @@ -0,0 +1,236 @@ +// #3406 item 11: the per-turn recommendation screen is a full-projection +// hot path — screenRecommendedUnits runs projectUnit over EVERY recommended +// unit, and each projection rescans the message array (carrier scan, +// protected-tool filter, media check), so the cost is O(n·m): n messages × +// m units. Nothing pinned that curve: a regression that adds one more +// full-array pass inside projectUnit would silently slow every turn of +// every large session. This test does NOT optimize anything (that is a +// separate issue by design) — it only pins the curve so an +// order-of-magnitude regression cannot land silently. +// +// Thresholds are deliberately loose to stay flake-free on slow CI runners: +// each rung is measured as the MIN of 3 runs (noise only ever adds time), +// and the cap is ~10x the authoring-machine baseline with an absolute +// floor (see #3406: "阈值放宽,只防数量级退化,防 flake"). Baselines recorded +// at authoring time (warm, 1 run): m=10 → ~7.5ms, m=40 → ~24ms, m=120 → +// ~63ms, full chain n=2000 → ~54ms. Measured times are printed to the test +// output on every run for the record. + +import { describe, it } from "node:test"; +import assert from "node:assert/strict"; +import { performance } from "node:perf_hooks"; +import { createInitialState } from "../src/state.js"; +import { assignRefs } from "../src/refs.js"; +import { + buildCompressibleRanges, + computeProtectedRefs, + mergeRangesToThreshold, + screenRecommendedUnits, +} from "../src/recommend.js"; +import { defaultConfig } from "../src/config.js"; +import type { + CompressibleRange, + CompressionBlock, + CompressionState, + CoreMessage, +} from "../src/types.js"; + +/** Loose per-rung caps: ~10x the recorded baseline, floored so a slow-but- + * healthy CI machine (several times slower than the authoring box) never + * flakes. A real O(n·m)→O(n²·m) regression blows through these by 10-100x + * on ANY machine. */ +const CAP_SMALL_MS = 750; +const CAP_LARGE_MS = 1_000; + +const N_MESSAGES = 2_000; +const SPAN = 10; + +/** Deterministic synthetic session: a 10-message turn pattern (user / + * reasoning / tool call / tool result / assistant filler) — the same shape + * family as the #498 scale fixtures — with 40 active tier-1 blocks covering + * the first 800 messages, so screening exercises BOTH the covered + * (directMessageIds empty) and uncovered (advertised) projection paths. */ +function buildSession(): { + state: CompressionState; + messages: CoreMessage[]; + config: ReturnType; + protectedRefs: Set; +} { + const messages: CoreMessage[] = []; + for (let i = 0; i < N_MESSAGES; i++) { + const slot = i % 10; + if (slot === 0 || slot === 1) { + messages.push({ + id: `u${i}`, + role: "user", + contentType: "text", + text: "u".repeat(800), + }); + } else if (slot === 2) { + messages.push({ + id: `r${i}`, + role: "assistant", + contentType: "reasoning", + text: "r".repeat(500), + }); + } else if (slot === 3) { + messages.push({ + id: `c${i}`, + role: "assistant", + contentType: "tool-call", + toolName: "read", + toolCallId: `t${i}`, + text: "{}", + }); + } else if (slot === 4) { + messages.push({ + id: `o${i}`, + role: "tool", + contentType: "tool-result", + toolName: "read", + toolCallId: `t${i}`, + text: "x".repeat(1200), + }); + } else { + messages.push({ + id: `a${i}`, + role: "assistant", + contentType: "text", + text: "a".repeat(700), + }); + } + } + const state = createInitialState(); + state.messageRefs = assignRefs(messages, { + existing: state.messageRefs, + nextIndex: 1, + }).map; + state.lastPassIds = messages.map((m) => m.id); + const block = (id: number, ids: string[]): CompressionBlock => ({ + blockId: `b${id}`, + runId: `r${id}`, + tier: 1, + summary: `Summary ${id}`, + directMessageIds: ids, + effectiveMessageIds: ids, + directBlockIds: [], + compressedTokens: 100, + createdAt: 0, + survivedCount: 1, + generation: "old", + active: true, + }); + for (let b = 0; b < 40; b++) { + const ids = messages.slice(b * 20, (b + 1) * 20).map((m) => m.id); + state.blocks.push(block(b + 1, ids)); + } + const config = defaultConfig(212_992); + const protectedRefs = computeProtectedRefs(messages, state, config); + return { state, messages, config, protectedRefs }; +} + +/** `count` non-overlapping fresh units over the UNCOVERED tail (from raw + * message 801 on), span SPAN each — every unit must survive screening, so + * each runs the projection to completion (the advertised path). */ +function tailUnits(count: number): CompressibleRange[] { + const out: CompressibleRange[] = []; + for (let k = 0; k < count; k++) { + const start = 800 + k * SPAN; + const end = start + SPAN - 1; + out.push({ + startRef: `m${String(start + 1).padStart(5, "0")}`, + endRef: `m${String(end + 1).padStart(5, "0")}`, + count: SPAN, + tokens: SPAN * 200, + }); + } + return out; +} + +/** min of `runs` wall-time measurements (noise only ever adds time). */ +function timedMin(runs: number, fn: () => T): { out: T; ms: number } { + let ms = Infinity; + let out: T | undefined; + for (let i = 0; i < runs; i++) { + const t0 = performance.now(); + out = fn(); + const el = performance.now() - t0; + if (el < ms) ms = el; + } + return { out: out as T, ms }; +} + +describe("#3406 item 11 — full-projection screen scale (n=2000, unit gradient)", () => { + it("hand-built unit gradient: m=10 / 40 / 120 stays within the recorded cap", () => { + const { state, messages, config, protectedRefs } = buildSession(); + + // JIT warm-up, not measured. + screenRecommendedUnits( + tailUnits(10), + messages, + state, + config, + protectedRefs, + ); + + for (const [m, cap] of [ + [10, CAP_SMALL_MS], + [40, CAP_SMALL_MS], + [120, CAP_LARGE_MS], + ] as const) { + const units = tailUnits(m); + const { out, ms } = timedMin(3, () => + screenRecommendedUnits(units, messages, state, config, protectedRefs), + ); + // Functional pin: the timing is only meaningful if the units really + // run the full advertised projection (kept === m), not a vacuous + // early-drop path. + assert.equal( + out.length, + m, + `m=${m}: every fresh tail unit stays advertised`, + ); + assert.ok( + ms < cap, + `screenRecommendedUnits n=${N_MESSAGES} m=${m} took ${ms.toFixed(1)}ms (cap ${cap}ms) — order-of-magnitude regression in the projection hot path`, + ); + console.log( + `#3406 scale record: n=${N_MESSAGES} m=${m} → ${ms.toFixed(1)}ms (${(ms / m).toFixed(2)}ms/unit, cap ${cap}ms)`, + ); + } + }); + + it("real recommend chain at n=2000 (buildCompressibleRanges → merge → screen) within cap", () => { + const { state, messages, config } = buildSession(); + const run = () => { + const protectedRefs = computeProtectedRefs(messages, state, config); + const ranges = buildCompressibleRanges( + messages, + state, + config, + protectedRefs, + ); + const merged = mergeRangesToThreshold( + ranges.compressible, + config.compress.minCompressRange, + ); + return screenRecommendedUnits( + merged, + messages, + state, + config, + protectedRefs, + ); + }; + run(); // warm-up + const { out, ms } = timedMin(3, run); + assert.ok(out.length > 0, "large session still offers ranges"); + assert.ok( + ms < CAP_LARGE_MS, + `full recommend chain n=${N_MESSAGES} took ${ms.toFixed(1)}ms (cap ${CAP_LARGE_MS}ms)`, + ); + console.log( + `#3406 scale record: real chain n=${N_MESSAGES} → ${ms.toFixed(1)}ms, ${out.length} units advertised (cap ${CAP_LARGE_MS}ms)`, + ); + }); +}); diff --git a/package-lock.json b/package-lock.json index 14529301c6..29da925464 100644 --- a/package-lock.json +++ b/package-lock.json @@ -45,7 +45,7 @@ }, "kernel": { "name": "acp-kernel", - "version": "0.0.127", + "version": "0.0.128", "dev": true, "license": "MIT", "devDependencies": {