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
29 changes: 29 additions & 0 deletions kernel/src/block-map.ts
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,35 @@ export function activeBlockSpans(state: CompressionState): BlockSpan[] {
return spans;
}

/** #5667 defect B: which ACTIVE blocks' ref-spans sit strictly INSIDE a
* displayed range's numeric span — i.e. the range's literal label bridges
* across an already-folded interior. On surface-replace hosts the covered
* messages leave the array without leaving a segmentation gap, so the segmenter
* joins the two surviving flanks into one range whose label reads as a solid
* mNNNNN–mMMMMM span (billion-context #5508 family). Display-only signal: the
* renderers append it; foldability judgment (KDD#12 single projection) is
* untouched. Returns block ids, oldest-first by span start; empty when nothing
* is spanned. A covered message is never a range member, so a block can only
* intersect a range's span as a strict interior hole — partial overlap is
* impossible and need not be tested. */
export function blocksSpannedByRange(
startRef: string,
endRef: string,
spans: BlockSpan[],
): string[] {
const [loRef, hiRef] = orderedRefPair(startRef, endRef);
const lo = refNum(loRef);
const hi = refNum(hiRef);
if (hi - lo < 1) return [];
const out: string[] = [];
for (const s of spans) {
const bLo = Math.min(refNum(s.startRef), refNum(s.endRef));
const bHi = Math.max(refNum(s.startRef), refNum(s.endRef));
if (bLo > lo && bHi < hi) out.push(s.blockId);
}
return out.sort((a, b) => refNum(a) - refNum(b));
}

/** Format newly created blocks for tool-result display, e.g.
* `blocks: b3=m00044–m00097, b4=m00103–m00123`. Adapters append this to their
* compress result line so the model's block ledger comes from the kernel
Expand Down
8 changes: 7 additions & 1 deletion kernel/src/compress.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1970,7 +1970,13 @@ function applySingleRange(input: SingleRangeInput): SingleRangeOutcome {

for (const consumedId of consumedBlockIds) {
const consumed = blockById(input.state, consumedId);
if (consumed) consumed.active = false;
if (consumed) {
// #5667 A: lineage written ONLY here (consumption). Non-consumptive
// deactivation (orphan GC / host expand in sync.ts) must leave it unset —
// that absence is what keeps the death cause discriminable.
consumed.active = false;
consumed.supersedesBlockId = blockId;
}
}

return { tokens: compressedTokens, warnings };
Expand Down
18 changes: 15 additions & 3 deletions kernel/src/nudge-text.ts
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,7 @@ export function formatRanges(
textPct: number;
dangerous: boolean;
noMergeBefore: boolean;
crossesCoveredBy: string[];
}
const refNum = (ref: string): number => {
const m = ref.match(/\d+/);
Expand All @@ -147,6 +148,7 @@ export function formatRanges(
protectedTools: [],
dangerous: r.dangerous ?? false,
noMergeBefore: r.noMergeBefore ?? false,
crossesCoveredBy: r.crossesCoveredBy ?? [],
});
}
for (const r of protectedRanges) {
Expand All @@ -169,6 +171,7 @@ export function formatRanges(
protectedTools: [...r.tools],
dangerous: false,
noMergeBefore: r.noMergeBefore ?? false,
crossesCoveredBy: [],
});
}
// Order/merge by POSITION, never ref number: refs can be non-monotonic vs array
Expand Down Expand Up @@ -196,6 +199,9 @@ export function formatRanges(
for (const t of e.protectedTools) {
if (!last.protectedTools.includes(t)) last.protectedTools.push(t);
}
for (const b of e.crossesCoveredBy) {
if (!last.crossesCoveredBy.includes(b)) last.crossesCoveredBy.push(b);
}
} else {
merged.push({ ...e });
}
Expand All @@ -211,13 +217,19 @@ export function formatRanges(
e.dangerous && e.compressibleTokens > 0
? " ⚠️ NOT recommended unless you are certain."
: "";
// #5667 B: the label bridges an already-folded interior — say so so the
// model does not re-submit the covered span as new content (#5508 family).
const spanNote =
e.crossesCoveredBy.length > 0
? ` · spans folded ${e.crossesCoveredBy.join(", ")}`
: "";
if (e.protectedTokens > 0 && e.compressibleTokens === 0) {
return ` ${lo}–${hi} ${e.count} msgs ${formatK(e.tokens)} [PROTECTED: ${e.protectedTools.join(", ")} — not compressible]${suffix}`;
return ` ${lo}–${hi} ${e.count} msgs ${formatK(e.tokens)} [PROTECTED: ${e.protectedTools.join(", ")} — not compressible]${spanNote}${suffix}`;
}
if (e.protectedTokens > 0 && e.compressibleTokens > 0) {
return ` ${lo}–${hi} ${e.count} msgs ${formatK(e.tokens)} [${formatK(e.compressibleTokens)} compressible | ${formatK(e.protectedTokens)} protected: ${e.protectedTools.join(", ")}]${userNote(e.userMsgs)}${suffix}`;
return ` ${lo}–${hi} ${e.count} msgs ${formatK(e.tokens)} [${formatK(e.compressibleTokens)} compressible | ${formatK(e.protectedTokens)} protected: ${e.protectedTools.join(", ")}]${userNote(e.userMsgs)}${spanNote}${suffix}`;
}
return ` ${lo}–${hi} ${e.count} msgs ${formatK(e.tokens)} [tool ${e.toolPct}% | text ${e.textPct}%]${userNote(e.userMsgs)}${suffix}`;
return ` ${lo}–${hi} ${e.count} msgs ${formatK(e.tokens)} [tool ${e.toolPct}% | text ${e.textPct}%]${userNote(e.userMsgs)}${spanNote}${suffix}`;
});
return `Compressible ranges (${merged.length}, oldest first):\n${lines.join("\n")}`;
}
Expand Down
12 changes: 12 additions & 0 deletions kernel/src/recommend.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ import { countMessageTokens } from "./tokenize.js";
import { computeIntegrityWithdrawals } from "./turn-integrity.js";
import { evaluateRefold } from "./refold.js";
import { segmentGroups } from "./segment.js";
import { activeBlockSpans, blocksSpannedByRange } from "./block-map.js";
import {
projectUnit,
collectCoverage,
Expand Down Expand Up @@ -461,6 +462,9 @@ export function buildCompressibleRanges(
// split at real array gaps and at user messages once a group has >= 3 messages.
// Splitting at user boundaries keeps each range aligned to roughly one turn.
const compressible: CompressibleRange[] = [];
// #5667 B: active-block spans resolved once so each range can record the
// folded interior its label bridges (display-only, never feeds foldability).
const activeSpansForSpan = activeBlockSpans(state);
for (const group of segmentGroups(compressibleMsgs)) {
const first = group[0]!;
const range: CompressibleRange = {
Expand Down Expand Up @@ -494,6 +498,12 @@ export function buildCompressibleRanges(
}
range.textPct = 100 - range.toolPct;
}
const spanned = blocksSpannedByRange(
range.startRef,
range.endRef,
activeSpansForSpan,
);
if (spanned.length > 0) range.crossesCoveredBy = spanned;
compressible.push(range);
}

Expand Down Expand Up @@ -566,6 +576,8 @@ function mergeBatch(batch: CompressibleRange[]): CompressibleRange {
// position-based merging (formatRanges) still sees it after this batch is
// re-emitted as a single range.
if (first.noMergeBefore === true) merged.noMergeBefore = true;
const spanned = batch.flatMap((r) => r.crossesCoveredBy ?? []);
if (spanned.length > 0) merged.crossesCoveredBy = [...new Set(spanned)];
return merged;
}

Expand Down
22 changes: 18 additions & 4 deletions kernel/src/report.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
import { clampPrefix } from "./truncate.js";
import { clampPrefix, scrubLoneSurrogates } from "./truncate.js";
import { isToolMessage } from "./message-kind.js";
import { BLOCKED_REF, orderedRefPair, refForRaw } from "./refs.js";
import { countMessageTokens } from "./tokenize.js";
import { segmentGroups } from "./segment.js";
import { activeBlockSpans, blocksSpannedByRange } from "./block-map.js";
import { isLiveCheckpointCarrier } from "./state.js";
import { computeProtectedRefs } from "./recommend.js";
import {
Expand Down Expand Up @@ -194,6 +195,12 @@ export interface StatusReportOptions {
originalMessages?: CoreMessage[];
}

export function projectionErrorDetail(err: unknown): string {
const raw = err instanceof Error ? err.message : String(err);
const detail = scrubLoneSurrogates(raw).trim();
return detail.slice(0, 120) || "no error detail";
}

export function buildStatusReport(
state: CompressionState,
messages: CoreMessage[],
Expand Down Expand Up @@ -504,8 +511,8 @@ function renderUncompressedRanges(
protectedMessageIds: protectedRefs,
shared,
});
} catch {
return "\u26a0 not foldable: refs unresolvable";
} catch (err) {
return `\u26a0 not foldable: ${projectionErrorDetail(err)}`;
}
if (proj.fatal?.kind === "zone")
return "\u26a0 not foldable: protected zone only";
Expand All @@ -518,16 +525,23 @@ function renderUncompressedRanges(
}
lines.push(`Sorted by ${sort === "time" ? "time" : "size"}`);
lines.push("");
const activeSpansForSpan = activeBlockSpans(state);
for (const r of merged.slice(0, limit)) {
// Ascending endpoint labels: non-monotonic refs (#1001) can make a
// positional span's labels numerically descending (#2168).
const range =
r.count === 1
? r.startRef
: orderedRefPair(r.startRef, r.endRef).join("–");
const spanned =
r.count > 1
? blocksSpannedByRange(r.startRef, r.endRef, activeSpansForSpan)
: [];
const spanNote =
spanned.length > 0 ? ` · spans folded ${spanned.join(", ")}` : "";
const flag = flagFor ? flagFor(r) : null;
lines.push(
` ${range} (${r.count} msgs, ${formatTokens(r.tokens)}${r.count > 1 ? ` (${Math.round(r.tokens / r.count)}/msg)` : ""}) ${dominantTool(r.toolTokens)}${flag ? ` ${flag}` : ""}`,
` ${range}${spanNote} (${r.count} msgs, ${formatTokens(r.tokens)}${r.count > 1 ? ` (${Math.round(r.tokens / r.count)}/msg)` : ""}) ${dominantTool(r.toolTokens)}${flag ? ` ${flag}` : ""}`,
);
}
if (merged.length > limit) {
Expand Down
15 changes: 15 additions & 0 deletions kernel/src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,12 @@ export interface CompressionBlock {
* Undefined = unstamped traffic or provably-loose coverage; never guessed. */
startSeq?: number;
endSeq?: number;
/** #5667 defect A: the id of the block that CONSUMED this one (nested fold).
* Written only at the single consumption point (compress.ts); non-consumptive
* deactivation (orphan GC / host expand) leaves it unset, which is exactly
* what makes an inactive block's death cause readable. Optional: states
* persisted before this field exist read as "no lineage recorded". */
supersedesBlockId?: string;
}

/** A compression block's current ref span, pre-resolved for display. */
Expand Down Expand Up @@ -708,6 +714,15 @@ export interface CompressibleRange {
* has no index gap and the index check alone would let mergeRangesToThreshold
* bridge back into one doomed span bracketing the dead ref. */
noMergeBefore?: boolean;
/** #5667 defect B (display-only): ids of ACTIVE blocks whose ref-spans sit
* strictly inside this range's numeric span — the range's literal label
* bridges an already-folded interior (surface-replace hosts leave no gap, so
* the segmenter joins the flanks; billion-context #5508 family). Renderers
* append " · spans folded bN" so the model does not re-submit the covered
* interior as new content. Set by buildCompressibleRanges / mergeBatch;
* NEVER consulted by foldability judgment (KDD#12 single projection). Absent
* when nothing is spanned or on hand-built ranges. */
crossesCoveredBy?: string[];
}

export interface ProtectedRange {
Expand Down
Loading
Loading