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
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.180",
"version": "0.0.181",
"description": "Framework-agnostic context-compression engine (model-driven, 3-tier LSM). Pure core: no host dependency.",
"license": "MIT",
"author": "ranxianglei",
Expand Down
93 changes: 91 additions & 2 deletions kernel/src/compress.ts
Original file line number Diff line number Diff line change
Expand Up @@ -168,6 +168,15 @@ type RangeResolution =
* clears the latch, so normal sessions hold a handful at most. Oldest drop. */
const MAX_TRUNCATE_LATCH = 64;

/** #5698: a tier-1 fold answering the fire that offered `pending` tokens must
* reclaim at least this fraction of the offered mass for the T1 channel to
* count as draining. Partial compliance below it (production #5671: nudges
* offering ~450K answered by 3-6K folds) sets the harvest-stall flag so tier
* arbitration can route to mass-qualified distillation instead of starving
* it behind a never-draining T1. Measured against the fire's pendingT1, so
* eager lanes (fold the whole offer) never trip it. */
export const STALL_HARVEST_RATIO = 0.5;

function rangeError(
spec: { startRef: string; endRef: string },
message: string,
Expand Down Expand Up @@ -862,6 +871,25 @@ export function createCore(ports: Ports = {}): CompressionCore {
// tracked — a fresh cadence starts from a clean slate.
state.nudge.absorbedDebt = 0;
state.nudge.absorbCount = 0;
// #5698: T1 harvest-stall accounting (consume-once). The tier-1 fire
// banked its offered pending; this fold answers it. A distill (any new
// block above tier 1) proves the distillation channel delivers — clear
// the stall. A message-shaped fold harvesting < STALL_HARVEST_RATIO of
// the offered pending is partial compliance: the T1 channel is not
// draining while the block pile keeps growing (#5671 treadmill) — set
// the stall so arbitration can route to mass-qualified distillation.
const distilled = state.blocks
.filter((b) => !preFoldBlockIds.has(b.blockId))
.some((b) => (b.tier ?? 1) > 1);
const firePending = state.nudge.lastT1FirePending;
if (distilled) {
state.nudge.t1HarvestStall = false;
state.nudge.lastT1FirePending = undefined;
} else if (firePending !== undefined && firePending > 0) {
state.nudge.t1HarvestStall =
tokensCompressed < STALL_HARVEST_RATIO * firePending;
state.nudge.lastT1FirePending = undefined;
}
// A successful compression is by definition viable progress — restart
// the terminal-floor streak so the escape signal can only fire again
// after N fresh stuck events (#300).
Expand Down Expand Up @@ -1484,6 +1512,12 @@ const nudgeNode: PipelineNode = {
[nudge.tier]: ctx.tokenCount,
};
}
// #5698: bank the offered tier-1 mass as the compliance denominator —
// the first applyCompression after this fire compares its harvest
// against this number to detect a non-draining T1 channel.
if (nudge.tier === 1 && nudge.breakdown) {
stamped.lastT1FirePending = nudge.breakdown.pendingT1;
}
}

return {
Expand Down Expand Up @@ -2081,6 +2115,19 @@ function decideNudge(input: NudgeInput): NudgeDecision {
const t2Pen = tiers[2]?.pending ?? 0;
const t3Pen = tiers[3]?.pending ?? 0;
const maxPending = Math.max(0, t1Eff, t2Pen, t3Pen);
// #5698: the T1 channel is PROVEN non-draining — its last fire was answered
// by a fold harvesting < STALL_HARVEST_RATIO of the offered pending
// (#5671 treadmill: nudges offering ~450K answered by 3-6K folds, forever).
// While set, a mass-qualified distillation tier may fire AHEAD of T1
// (growth band) and T1 leaves the pressure-band candidate set when a
// distill tier can pay the minimum benefit. The stall is an effectiveness
// signal on the T1 channel — consistent with #379's "mass is the need
// signal": it is consulted only when the distill tier's own mass ALREADY
// qualifies. Absent the flag (eager lanes, fresh states, tiers off)
// everything decides byte-identically.
const t1HarvestStall =
config.tiers.enabled && state.nudge.t1HarvestStall === true;
let stallEscalated = false;
// First-sight mass bypass (#194): growthReference seeds to tokenCount when
// no baseline exists, so a session that ARRIVES with a huge ready mass
// (stateless full-history ingest / restored state) shows growthSinceReference
Expand Down Expand Up @@ -2152,7 +2199,14 @@ function decideNudge(input: NudgeInput): NudgeDecision {
// the kernel would atomically reject. emergency vs over-limit only
// changes the reason label/voice; truncate.threshold remains the
// independent last resort when there is genuinely nothing to compress.
const candidates: CompressionTier[] = [1];
// #5698: with the T1 channel proven non-draining, max-pending would just
// re-select the tier that demonstrated it cannot deliver (T1 447K pending,
// 3-6K per nudge) — take T1 out of the running when a distill tier can pay
// the minimum benefit, so pressure routes to the channel that works.
const candidates: CompressionTier[] = [];
if (!(t1HarvestStall && Math.max(t2Pen, t3Pen) >= minPressureBenefit)) {
candidates.push(1);
}
if (config.tiers.enabled) {
candidates.push(2, 3);
}
Expand All @@ -2169,17 +2223,48 @@ function decideNudge(input: NudgeInput): NudgeDecision {
// resets the baselines and the same EMERGENCY nudge re-injects each turn.
if (best !== null && bestPending >= minPressureBenefit) {
injectedTier = best;
stallEscalated = t1HarvestStall && best !== 1;
const label = emergencyOverride ? "EMERGENCY" : "OVER-LIMIT";
injectedReason =
best === 1
? `${label} T1: max effective pending ${bestPending}, usage ${Math.round(usage * 100)}%`
: `${label} T${best} distill: max pending ${bestPending} (T1 effective ${t1Eff}, T2 ${t2Pen}, T3 ${t3Pen}), usage ${Math.round(usage * 100)}%`;
}
} else if (growthReady) {
if (t1Eff >= tierThresholds[1]) {
// #5698 stall escalation: while the T1 channel is proven non-draining,
// a distill tier whose own mass qualifies may fire ahead of T1 — without
// the normal `> t1Eff` dominance requirement (the treadmill state has
// T1 effective 447K vs T2 112K, so dominance can never hold even though
// ONE distill call reclaims 91.6% of the floor). Cadence still applies.
if (t1HarvestStall && t3Pen >= tierThresholds[3] && t3Pen > t2Pen) {
const lastShown = state.nudge.lastShownByTier[3] ?? 0;
const cadenceMet =
lastShown === 0 || tokenCount - lastShown >= growthFloor;
if (cadenceMet) {
injectedTier = 3;
stallEscalated = true;
injectedReason = `T3 condense ready: ${tiers[3]!.targetBlocks.length} tier-2 blocks (${t3Pen} tokens) >= ${tierThresholds[3]}${config.nudge.tierGrowthTokens?.t3 === undefined ? " (1.5x)" : ""} and > T2 ${t2Pen}, usage ${Math.round(usage * 100)}%`;
}
}
if (
injectedTier === null &&
t1HarvestStall &&
t2Pen >= tierThresholds[2]
) {
const lastShown = state.nudge.lastShownByTier[2] ?? 0;
const cadenceMet =
lastShown === 0 || tokenCount - lastShown >= growthFloor;
if (cadenceMet) {
injectedTier = 2;
stallEscalated = true;
injectedReason = `T2 distill ready: ${tiers[2]!.targetBlocks.length} tier-1 blocks (${t2Pen} tokens) >= ${tierThresholds[2]}${config.nudge.tierGrowthTokens?.t2 === undefined ? " (1.5x)" : ""}, usage ${Math.round(usage * 100)}%`;
}
}
if (injectedTier === null && t1Eff >= tierThresholds[1]) {
injectedTier = 1;
injectedReason = `T1 effective ${t1Eff} >= ${tierThresholds[1]}, growth ${growthSinceReference}, usage ${Math.round(usage * 100)}%`;
} else if (
injectedTier === null &&
config.tiers.enabled &&
(t2CountReady || (t2Pen >= tierThresholds[2] && t2Pen > t1Eff))
) {
Expand All @@ -2193,6 +2278,7 @@ function decideNudge(input: NudgeInput): NudgeDecision {
: `T2 distill ready: ${tiers[2]!.targetBlocks.length} tier-1 blocks (${t2Pen} tokens) >= ${tierThresholds[2]}${config.nudge.tierGrowthTokens?.t2 === undefined ? " (1.5x)" : ""} and > T1 effective ${t1Eff}, usage ${Math.round(usage * 100)}%`;
}
} else if (
injectedTier === null &&
config.tiers.enabled &&
(t3CountReady ||
(t3Pen >= tierThresholds[3] && t3Pen > t2Pen && t3Pen > t1Eff))
Expand All @@ -2213,6 +2299,9 @@ function decideNudge(input: NudgeInput): NudgeDecision {
if (shouldInject && firstSightMassReady) {
injectedReason += " [first-sight mass]";
}
if (shouldInject && stallEscalated) {
injectedReason += " [T1 harvest stall]";
}

let reason: string;
if (injectedTier !== null) {
Expand Down
10 changes: 10 additions & 0 deletions kernel/src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -147,6 +147,16 @@ export interface NudgeState {
* by applyCompression alongside the other baselines. */
absorbedDebt?: number;
absorbCount?: number;
/** #5698: pendingT1 (effective) recorded at the last tier-1 nudge fire —
* the denominator for T1 harvest-stall detection. Consumed by the first
* applyCompression after the fire; re-recorded at every tier-1 fire. */
lastT1FirePending?: number;
/** #5698: the last tier-1 fire was answered by a fold harvesting <
* STALL_HARVEST_RATIO of the offered pending — the T1 channel is not
* draining (partial model compliance). While set, tier arbitration lets
* a mass-qualified T2/T3 fire ahead of T1 instead of starving forever.
* Cleared by a compliant harvest or any distillation. */
t1HarvestStall?: boolean;
}

export interface CompressionStats {
Expand Down
Loading
Loading