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
4 changes: 2 additions & 2 deletions kernel/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

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.201",
"version": "0.0.202",
"description": "Framework-agnostic context-compression engine (model-driven, 3-tier LSM). Pure core: no host dependency.",
"license": "MIT",
"author": "ranxianglei",
Expand Down
17 changes: 17 additions & 0 deletions kernel/src/compress.ts
Original file line number Diff line number Diff line change
Expand Up @@ -933,6 +933,20 @@ export function createCore(ports: Ports = {}): CompressionCore {
state.pendingOrphanSweepMinRef === undefined
? foldStartRef
: Math.min(state.pendingOrphanSweepMinRef, foldStartRef);
// #6116: upper bound of the same window. Every message in history at
// apply time carries a ref <= this frontier; the successful call and
// any later same-turn calls only get refs on the next assignRefs pass,
// so they land strictly above it and survive the sweep with their
// failure feedback intact (under-sweep is safe; over-sweep is not).
// Running MAX mirrors the min side's running MIN across folds that
// commit before one consuming turn.
const frontier = highestUsedIndex(state.messageRefs);
if (frontier > 0) {
state.pendingOrphanSweepMaxRef =
state.pendingOrphanSweepMaxRef === undefined
? frontier
: Math.max(state.pendingOrphanSweepMaxRef, frontier);
}
}
// Compress succeeded: clear the growth baseline so the next turn
// re-establishes it at the new (lower) token count. Without this the
Expand Down Expand Up @@ -1434,6 +1448,8 @@ const hideCompressCallsNode: PipelineNode = {
const patch: Partial<CompressionState> = {
hiddenOrphanRefs: hidden.hiddenOrphanRefs,
pendingOrphanSweepMinRef: undefined,
// #6116: the window bound is one-shot with the marker it bounds.
pendingOrphanSweepMaxRef: undefined,
};
if (hidden.newlySweptCallIds.length > 0) {
patch.sweptOrphanCallIds = [
Expand Down Expand Up @@ -2632,6 +2648,7 @@ function cloneState(state: CompressionState): CompressionState {
? [...state.sweptOrphanCallIds]
: undefined,
pendingOrphanSweepMinRef: state.pendingOrphanSweepMinRef,
pendingOrphanSweepMaxRef: state.pendingOrphanSweepMaxRef, // #6116 (sweep window upper bound)
deadRefs: state.deadRefs ? [...state.deadRefs] : undefined,
lastPassIds: state.lastPassIds ? [...state.lastPassIds] : undefined,
// #4797: the one-time ledger-alignment gate must survive the fold clone
Expand Down
24 changes: 18 additions & 6 deletions kernel/src/hide-consumed.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,14 @@ import type { CompressionState, CoreMessage } from "./types.js";
// (billion-context-pi #9: 3,849 identical calls over 5h13m under keep=0), AND a
// quiet turn must never move the cached prefix breakpoint forward just to delete
// a few hundred chars of old failure residue — the old keep-last-two rotation did
// exactly that (hid the oldest failure on EVERY turn, seam cost far exceeding the
// bytes removed). Now: between folds nothing is swept; on a fold the node sweeps
// orphans at/after the fold's earliest start ref onto the persistent
// sweptOrphanCallIds set (they stay hidden forever), leaving orphans before it
// visible. Residue is bounded by fold cadence, not a fixed pair count.
// exactly that (hid the oldest failure on EVERY turn, seam cost far exceeding the
// bytes removed). Now: between folds nothing is swept; on a fold the node sweeps
// orphans at/after the fold's earliest start ref — and at/before the highest ref
// allocated when that fold committed (#6116: the successful call and any later
// same-turn calls get refs above that frontier and keep their failure feedback
// visible) — onto the persistent sweptOrphanCallIds set (they stay hidden
// forever), leaving orphans outside the window visible. Residue is bounded by
// fold cadence, not a fixed pair count.

export interface HideConsumedResult {
messages: CoreMessage[];
Expand Down Expand Up @@ -319,8 +322,15 @@ export function hideConsumedCompressCalls(
// fold pending nothing new is swept, so a quiet turn never breaks the prefix.
// Orphans quoting an ACTIVE block's folded range are that block's summary
// carrier (#1567) and are exempt from the sweep (quotesActiveBlock).
// #6116: the window is bounded above by pendingOrphanSweepMaxRef — the highest
// allocated ref when the pending fold committed. The successful call and any
// same-turn calls after it only get refs above that frontier, so their failure
// feedback must stay visible for the model to react to; sweeping them would be
// over-sweep (under-sweep is safe, over-sweep is not). States written before
// #6116 lack the max bound and keep the pre-fix unbounded behavior.
const sweptSet = new Set(state.sweptOrphanCallIds ?? []);
const foldMinRef = state.pendingOrphanSweepMinRef;
const foldMaxRef = state.pendingOrphanSweepMaxRef;
const newlySweptCallIds: string[] = [];

const keepCallIds = new Set<string>();
Expand All @@ -346,7 +356,9 @@ export function hideConsumedCompressCalls(
hide = true;
} else if (foldMinRef !== undefined && !quotesActiveBlock(messages[i]!.text, activeRangeKeys)) {
const num = refNum(ref);
if (num !== null && num >= foldMinRef) {
// #6116: window is [foldMinRef, foldMaxRef]; a missing max bound
// (pre-#6116 state) degrades to the pre-fix unbounded sweep.
if (num !== null && num >= foldMinRef && (foldMaxRef === undefined || num <= foldMaxRef)) {
hide = true;
newlySweptCallIds.push(callId);
}
Expand Down
1 change: 1 addition & 0 deletions kernel/src/sync.ts
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,7 @@ export function syncBlocks(
hiddenOrphanRefs: state.hiddenOrphanRefs ? [...state.hiddenOrphanRefs] : undefined,
sweptOrphanCallIds: state.sweptOrphanCallIds ? [...state.sweptOrphanCallIds] : undefined,
pendingOrphanSweepMinRef: state.pendingOrphanSweepMinRef,
pendingOrphanSweepMaxRef: state.pendingOrphanSweepMaxRef, // #6116 (sweep window upper bound)
// #3939 ③: the truncation latch must survive the sync node — dropping it
// here would re-expose clipped bytes mid-history after a sync carried the
// rest of the state (#3691 oscillation via the sync path).
Expand Down
13 changes: 13 additions & 0 deletions kernel/src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -363,6 +363,19 @@ export interface CompressionState {
* Carries the position so the sweep is cache-neutral: it only removes records
* the fold already invalidated. Optional: absent until the first post-fix fold. */
pendingOrphanSweepMinRef?: number;
/** UPPER bound of the same pending orphan-sweep window (#6116): the highest
* allocated m-ref at the moment the most recent pending fold committed
* (highestUsedIndex of messageRefs in applyCompression). Every message that
* was already in history then carries a ref <= this value; the successful
* compress call itself and anything issued after it (mixed multi-tool turns)
* only gets a ref on the NEXT assignRefs pass — strictly above it — so it
* stays out of the sweep and its failure feedback remains visible for the
* model to react to. Consumed together with pendingOrphanSweepMinRef (the
* sweep condition becomes min <= refNum <= max). Across several folds that
* commit before one consuming turn it keeps the running MAX, mirroring the
* min side's running MIN. Optional: states written before #6116 lack it, and
* a missing max degrades to the pre-fix unbounded sweep (upgrade compat). */
pendingOrphanSweepMaxRef?: number;
/** Emergency-truncation latch (#3939 ③/#3691): the clipped texts (post-clip
* bytes, keyed by padded ref) of messages clipped by the emergency-truncate
* node. The watermark decision is made ONCE — replaying these clips on every
Expand Down
Loading
Loading