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
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ Versions follow the merge of a `*_release-v*` branch; CI publishes to npm on tag
## [Unreleased]

### Fixed
- **压缩「总是失败、要重试很多次才成功」的活锁(#4521)**:长会话里模型重报已被折叠覆盖的区间、或把 endId 猜到尚未发号的 ref 之上、或整段落在保护区,这些「其实无事可做」的请求一律被判 FAILED——原子批整体拒绝、断路器计数、模型照失败提示再猜再试,形成「失败→重试→再失败」循环(实测会话 01a18a4d 在 20:09 连续 4 次 FAILED 才成功)。现在五处修正:①endId 越界自动钳到最高已发 ref(警告提示,不整批拒);②区间全覆盖既有活动块=幂等 no-op 成功收据(`noop: true`,提示用 bN..bM 改写而非重发原文),不失败、不进断路器;③纯保护区区间同样 no-op(警告明说勿重发);④保护区 fatal 不再中止整批,只降级该区间;⑤新增 armed-but-silent 状态行:tier 级 nudge 已备好但本轮不投横幅时(退避/节拍/模型定时),以临时 user 消息刷新「最高已发 ref + 当前可压区间」,模型不再凭记忆猜号;全新会话的低于阈值计划保持完全静默(#196 域隔离、#3910 基线不破坏)。
- **自动升级换代旋转的「假存活」误判(#5167)**:旧实例接受 `/__bili/shutdown` 后毫秒级就关 listener(口已释放),但存量 keep-alive 连接照常应答——这是排水的本意(#3890 后最坏 (1+10)×10s 才退进程)。而两条旋转等待(launcher `rotateStalePredecessor` 每 200ms `fetchHealthInfo`、dsh `rotatePinnedStaleProxy` 每 500ms `fetchProxyVersion`)都用全局 fetch 的连接池轮询,轮询节拍远小于服务端 5s keepAliveTimeout,永远落在预热过的池化 socket 上 → 把「正在排水」误判为「没退出」,12s 预算耗尽得到假阴性:dsh 宿主 attach-anyway 骑上将被强退的旧实例、launcher 回退 spawn 阶梯,新旧两进程同写一份 machine-global 会话存储(index.db 双写横幅、#5046 SQLITE_BUSY、JSON 状态互踩)。修复:旋转等待的全部请求(含 shutdown POST)改带 `Connection: close` 每轮全新连接,「应答」重新等于「listener 还在收新连接」=真正的口释放信号;同步修正 12s 预算的过时注释(它只需覆盖 shutdown 请求被处理的延迟,不需覆盖进程退出)。`tests/issue5167-drain-aware-rotation.test.ts` 用真实排水 http 服务器(无 fetch mock、单连接 dispatcher 钉住池化拓扑)双向钉住:排水中的 resident 必须判为已释放,真占口不退的 holder 仍走超时回退。
- **shadow 摘要漂移的单实例成因(#4392 / #4986)**:`canonicalize` 曾按 `Object.entries` 插入序参与哈希,而 live 腿(kernel 状态,`byRef` 按发号顺序)与 replay 腿(`loadLegacyView` 按账本 `ord` 重建 `messageRefs`)对同一份等值状态给出不同键序 → 单进程自我报告 `[diverged: state]`,连续 3 次即冻结本进程的信封写入并清掉入库水位。现在任意深度按键排序(数组保序,Map/Set 形态不变),跳过的键(去掉 `storageMode`/`tailGaps`)集中成 `canonical.ts` 导出的 `DIGEST_SKIPPED_KEYS`,供流式摘要路径共用;升级文案不再断言「多个 bili 实例」。storage-corpus manifest 随摘要定义重钉(只有 10 个摘要值变化,文件清单/格式不动);storage-render 金标无需改动——那套驱动在 harness 侧已先 `sortObjectKeys`,排键只改 shadow 腿与 corpus 摘要。`tests/issue4986-digest-drift-ref-order.test.ts`(取自 #4997)钉住 #4986 的 folded-corpus 面。
- 出站不变量判官新增 I3 配对不变量(#4862 出口校验缺口):此前判官只对账入站↔出站连续性(I1/I2),折叠区内完全免检,于是「折叠拆断一对 tool_use/tool_result」的出站体拿到 clean fold verdict 直接放行,直到上游用 400 `tool_use ids were found without tool_result blocks` 说出真相。现在每个 tool-call 必须有同 id 的 tool-result 在其后、每个 tool-result 必须有同 id 的 tool-call 在其前,折叠区不再豁免;归因保持“谁的错”职责(宿主自己发来的断对不计 bili、compress 在途调用豁免、id 重铸不可洗白)。纯检测不拦截,受 diagnostics.invariantJudge 旋钮控制。
Expand Down
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.171",
"version": "0.0.172",
"description": "Framework-agnostic context-compression engine (model-driven, 3-tier LSM). Pure core: no host dependency.",
"license": "MIT",
"author": "ranxianglei",
Expand Down
22 changes: 22 additions & 0 deletions kernel/src/boundaries.ts
Original file line number Diff line number Diff line change
Expand Up @@ -216,6 +216,28 @@ function resolveAnchorIndex(
// hole (pruned or skipped) and must not be read as cross-session.
const highest = highestUsedIndex(state.messageRefs);
if (boundary.numericId > highest) {
// #4521: an endId above the highest allocated ref is the model aiming
// at "the newest tail" without knowing where it ends — the single most
// common compress failure on long sessions (it cannot see refs minted
// after the last banner). START anchors keep the hard refusal (a
// cross-generation start ref is a real targeting error), but the END
// anchor clamps down to the highest visible message: the range still
// folds everything the model meant ("up to now"), and the snapped note
// teaches the correct ceiling for next time.
if (endpoint === "end") {
const neighbor = snapToNeighborVisible(
state,
indexByMessageId,
boundary.numericId,
endpoint,
);
if (neighbor !== null) {
return {
index: neighbor,
snapped: `${label}="${boundary.raw}" is above this session's highest allocated ref (${highest > 0 ? formatPaddedRef(highest) : "none"}) — clamped to it (refs are minted in order and never pre-allocated, so higher numbers do not exist yet). The range folds up to the last visible message.`,
};
}
}
throw new BoundaryNotFoundError(
"unknown",
endpoint,
Expand Down
149 changes: 131 additions & 18 deletions kernel/src/compress.ts
Original file line number Diff line number Diff line change
Expand Up @@ -165,6 +165,10 @@ function numericBlockId(id: string): number {
return parsed ? Number(parsed[1]) : 0;
}

function formatK(n: number): string {
return n >= 1000 ? `${(n / 1000).toFixed(1)}K` : `${n}`;
}

function refGateDiagnostics(
state: CompressionState,
requestedRanges: number,
Expand Down Expand Up @@ -593,6 +597,33 @@ export function createCore(ports: Ports = {}): CompressionCore {
gateMessage += ` ${reversalNotes.join(" ")}`;
}

// #4521: a PURE resubmit (every requested range already consumed,
// no dangling/dead refs, nothing countable left) is the idempotent
// retry of an already-folded bill of work — historically refused with
// "Requested range(s) already compressed", which read as a failure and
// dragged another retry. Return it as the no-op success class instead:
// same informative text, zero errors, host renders a "[Compression
// no-op:" receipt that folds away (#4289 class). Mixed batches (some
// consumed + some live-but-too-small) keep the refusal — there is real
// work left to re-plan.
const pureResubmit =
countedRanges === 0 &&
consumedRanges.length > 0 &&
danglingRefs.length === 0 &&
unknownCount === 0;
if (pureResubmit) {
return {
state,
result: {
blocksCreated: 0,
tokensCompressed: 0,
errors: [],
warnings: [`${gateMessage} Nothing failed and nothing needed redoing — do not resend these ranges.`],
noop: true,
},
};
}

// Rejected batches still carry their side effect: the dead-ref
// tombstones marked above (#2362). The clone differs from input.state
// ONLY in that field before the gate, so returning it is safe.
Expand Down Expand Up @@ -731,7 +762,11 @@ export function createCore(ports: Ports = {}): CompressionCore {
countTokens,
preExistingCoverage,
});
blocksCreated += outcome.refolded ? outcome.refolded.length : 1;
blocksCreated += outcome.noop
? 0
: outcome.refolded
? outcome.refolded.length
: 1;
tokensCompressed += outcome.tokens;
warnings.push(...outcome.warnings);
} catch (error) {
Expand Down Expand Up @@ -829,6 +864,11 @@ export function createCore(ports: Ports = {}): CompressionCore {
errors,
warnings,
...(notes.length > 0 ? { notes } : {}),
// #4521: a batch where every range was a no-op/skip (covered,
// protected, refold-blocked skips) committed nothing and failed
// nothing — surface the no-op class so the host renders the
// success-shaped receipt instead of "FAILED: no blocks created".
...(blocksCreated === 0 && errors.length === 0 ? { noop: true } : {}),
},
};
}
Expand Down Expand Up @@ -875,10 +915,38 @@ export function createCore(ports: Ports = {}): CompressionCore {
}
const state = { ...result.state, lastPassIds: inboundIds, deadRefs };
const ccrEffect = result.effects.ccr as CcrEffect | undefined;
// #4521: between banner deliveries a compression round can stay armed for
// many requests (backoff, tier cadence, model-decided timing). During that
// window the model sees NO current refs and starts guessing beyond the
// highest allocated ref — every guess a failed round-trip. Emit a compact
// boundary line whenever a nudge plan carries offered ranges but its
// banner is not being delivered this turn; hosts append it as an ephemeral
// trailing user message (same carrier as the nudge, same cache safety).
let statusLine: string | undefined;
const nudge = result.effects.nudge;
// Two shapes authorize the refresh line: a real tiered arm whose banner
// is deferred this turn (tier !== null: pressure crossed a threshold but
// cadence/backoff holds it), or any plan after at least one banner was
// already shown (lastNudgeShownTokens > 0: the model HAS seen refs and
// this line only refreshes them). A fresh session's below-threshold plan
// (tier === null, nothing shown yet — e.g. an optimistic local estimate)
// must stay fully silent: same regime promise as the nudge itself
// (#196 regime scoping, #3910 lane baseline).
const bannerShown = (result.state.nudge?.lastNudgeShownTokens ?? 0) > 0 || Object.keys(result.state.nudge?.lastShownByTier ?? {}).length > 0;
if (nudge && !nudge.shouldInject && (nudge.tier !== null || bannerShown) && (nudge.compressibleRanges?.length ?? 0) > 0) {
const highest = highestUsedIndex(state.messageRefs);
const offered = nudge.compressibleRanges!
.slice(0, 3)
.map((r) => `${r.startRef}–${r.endRef} (${formatK(r.tokens)})`)
.join(", ");
const extra = nudge.compressibleRanges!.length > 3 ? `, +${nudge.compressibleRanges!.length - 3} more` : "";
statusLine = `[ACP status: highest allocated ref ${highest > 0 ? indexToRef(highest) : "none"} — refs above it are not minted yet, never guess them; compressible now: ${offered}${extra}; the newest messages stay protected. Use these refs directly; acp_status only if you need the full list.]`;
}
return {
messages: result.messages,
state,
nudge: result.effects.nudge,
statusLine,
terminalEscape: result.effects.terminalEscape,
truncationSkipped: result.effects.truncationSkipped,
contentStore: ccrEffect?.store ?? contentStore,
Expand Down Expand Up @@ -1492,6 +1560,11 @@ interface SingleRangeOutcome {
/** #400: set when the range ended as an in-place refold instead of creating
* a fresh block — the ids of the blocks updated (caller counts these, not 1). */
refolded?: string[];
/** #4521: set when the range resolved legally but folded nothing (all
* content already covered, or entirely inside the protected zone). The
* caller counts zero blocks and records the warning instead of rejecting
* the batch — an idempotent resubmit is success-shaped, not a failure. */
noop?: boolean;
}

function applySingleRange(input: SingleRangeInput): SingleRangeOutcome {
Expand All @@ -1509,7 +1582,26 @@ function applySingleRange(input: SingleRangeInput): SingleRangeOutcome {
protectedMessageIds: input.protectedMessageIds,
warnings,
});
if (projection.fatal) throw projection.fatal.error;
if (projection.fatal) {
// #4521: a range that lies ENTIRELY inside the protected zone is a legal
// no-op, not a failure — the model aimed at the newest tail (exactly the
// messages it just produced) and there is nothing it may fold there.
// Rejecting it sent retry loops back into the same zone (live incident:
// four consecutive zone refusals in session 01a18a4d, 20:09). The
// recommendation side still demotes zone fatals to gaps, so advertised
// ranges never point here (#3236 unchanged).
if (projection.fatal.kind === "zone") {
const highest = highestUsedIndex(input.state.messageRefs);
return {
tokens: 0,
warnings: [
`Range ${input.spec.startRef}..${input.spec.endRef} folded nothing — it lies entirely inside the protected zone (the last ${input.config.preserveRecentMessages} messages and/or the most recent user message), which always stays visible. Nothing failed; do not resend this range. Session highest allocated ref is ${highest > 0 ? indexToRef(highest) : "none"} — target older ranges only.`,
],
noop: true,
};
}
throw projection.fatal.error;
}
const {
filteredIds,
consumedBlockIds,
Expand Down Expand Up @@ -1554,14 +1646,40 @@ function applySingleRange(input: SingleRangeInput): SingleRangeOutcome {
}
const first = consumedBlockIds[0]!;
const last = consumedBlockIds[consumedBlockIds.length - 1]!;
throw new Error(
`Range ${input.spec.startRef}..${input.spec.endRef} contains no new compressible messages — every message in it is already covered by active block(s) ${consumedBlockIds.join(
", ",
)}. Nothing was compressed. To rewrite or merge those blocks, reference them by block ID (${first}..${last}); otherwise run acp_status and compress a range it reports as compressible.`,
);
// #4521: full-log hosts resolve covered spans as "ok" (never classified
// consumed), so an idempotent resubmit of an already-folded range used to
// land HERE and reject the whole batch ("Batch rejected before folding any
// range") — the dominant retry-loop driver on pi/plugin sessions. A
// covered span with nothing new is a no-op success: zero blocks, zero
// errors, one warning that names the covering blocks and how to rewrite
// them if that was the intent.
return {
tokens: 0,
warnings: [
`Range ${input.spec.startRef}..${input.spec.endRef} folded nothing — every message in it is already covered by active block(s) ${consumedBlockIds.join(
", ",
)}. Nothing failed and nothing needed redoing; do not resend this range. To rewrite or merge those blocks, reference them by block ID (${first}..${last}) instead.`,
],
noop: true,
};
}

validateCompressionRange(input, filteredIds, consumedBlockIds.length);
validateCompressionRange(input);

if (filteredIds.length === 0 && consumedBlockIds.length === 0) {
// #4521: everything in the range was dropped by content filters
// (protected tool pairs, media, …) with no zone hit and no consumed
// blocks — same idempotent no-op class as the two above, so the batch is
// not poisoned by it.
const highest = highestUsedIndex(input.state.messageRefs);
return {
tokens: 0,
warnings: [
`Range ${input.spec.startRef}..${input.spec.endRef} folded nothing — all of its content is protected or filtered (protected tool calls/results, media, or the recent-message zone), which always stays visible. Nothing failed; do not resend this range. Session highest allocated ref is ${highest > 0 ? indexToRef(highest) : "none"}.`,
],
noop: true,
};
}

let compressedTokens = 0;
for (const id of filteredIds) {
Expand Down Expand Up @@ -1671,19 +1789,14 @@ function resolveCoverageInterval(
return merged;
}

function validateCompressionRange(
input: SingleRangeInput,
directMessageIds: string[],
consumedBlockCount: number,
): void {
function validateCompressionRange(input: SingleRangeInput): void {
const cfg = input.config.compress;
validateSummaryLength(input.spec, cfg);

if (directMessageIds.length === 0 && consumedBlockCount === 0) {
throw new Error(
"Range contains no compressible messages — all are already covered by active blocks or protected.",
);
}
// #4521: the empty-range case (no direct messages, no consumed blocks)
// moved to the caller as an idempotent NO-OP outcome — see applySingleRange.
// Throwing here rejected whole batches for ranges that were legal but had
// nothing left to fold.
}

function validateSummaryLength(
Expand Down
6 changes: 5 additions & 1 deletion kernel/src/parse-compress-input.ts
Original file line number Diff line number Diff line change
Expand Up @@ -503,7 +503,11 @@ function parseLineEntry(
afterRefs = single.index + single[0].length;
}
if (summary.length === 0)
return { reason: "line entry: missing summary after the refs header line" };
// #4521: name the two most common shapes behind this parse error — a
// stray note line about an already-folded range, and a block rewrite
// written as refs — so the model fixes the entry instead of resending it
// verbatim (each resend was a full failed round-trip).
return { reason: "line entry: missing summary after the refs header line (every entry needs refs THEN its summary text on the same or the next line; a note about an already-folded range does not belong in the batch — drop it; to rewrite an existing block use a bN..bM block-id header entry)" };
const explicitTopic = head.slice(afterRefs).trim();
const range: CompressRangeSpec = {
startRef,
Expand Down
6 changes: 5 additions & 1 deletion kernel/src/protected.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,10 +25,14 @@ export const ALWAYS_PROTECTED_TOOLS = ["compress", "acp_rule"] as const;
* #4307: both classes leave zero blocks committed, so both are dead weight,
* not metadata. Successful and partial receipts start with "[Compressed" and
* stay protected — a partial fold's arguments still cover the ranges that
* DID apply. */
* DID apply. #4521: "[Compression no-op: …]" is the idempotent-resubmit
* class — parse ok, ranges legal, nothing left to fold (already covered /
* protected zone). Zero blocks committed, so it folds away like the other
* zero-applied results instead of lingering as protected residue. */
export const FAILED_COMPRESS_RESULT_PREFIXES = [
"[Compression FAILED",
"[Compression refused",
"[Compression no-op",
] as const;

/** True when toolName denotes a compress call in ANY hosted dialect (#4289):
Expand Down
Loading
Loading