Skip to content
Closed
28 changes: 26 additions & 2 deletions src/server/dsh-compaction-guard.ts
Original file line number Diff line number Diff line change
Expand Up @@ -265,7 +265,20 @@ export type Refusal = { status: number; body: unknown };
const REFUSAL_MESSAGE =
`dsh native compaction call refused by billion-context: bili owns compression on this lane (#1729, cf. #1772/#1206). ` +
`A landed dsh compaction checkpoint durably shadows the raw conversation history, which destroys the proxy's compression substrate. ` +
`To run dsh native compaction on this lane anyway, opt in explicitly: "allowDshCompaction": true in the bili config or env BILI_ALLOW_DSH_COMPACTION=1 (#2028).`;
`This refusal is terminal for the session (bili_final) — the same call will be refused every time, so stop retrying (#3013: one session burned 5069 refused retries). ` +
`To manage context use bili's compress; to stop dsh's retry pressure disable its auto-compaction (profile recipe #2474); ` +
`to run dsh native compaction anyway opt in explicitly: "allowDshCompaction": true in the bili config file or env BILI_ALLOW_DSH_COMPACTION=1 (#2028).`;

/** #3013/#3022 (A1): the refusal is a DECISION, not a transient error — dsh's
* retry loop is driven by its own raw pressure ledger which nothing on bili's
* side can shrink (#2490), so every retry of the same call fails identically.
* `bili_final: true` gives clients an unambiguous machine-readable terminal
* marker to latch and stop retrying (consumption needs dsh-upstream
* coordination; `retryable:false` alone did not stop the field-reported
* 5038-retry storm). Present at the top level of both protocol shapes and,
* for the openai error envelope (where `retryable` lives), inside `error`
* too. */
const BILI_FINAL_FIELD = "bili_final" as const;

/** Protocol-shaped refusal body (mirrors the #554 side-request guard shape so
* clients render it natively). 422: a policy refusal of this one request — not
Expand All @@ -276,18 +289,29 @@ export function dshCompactionRefusal(protocol: string): Refusal {
if (protocol === "anthropic") {
return {
status: 422,
body: { type: "error", error: { type: "invalid_request_error", message: REFUSAL_MESSAGE } },
body: { type: "error", [BILI_FINAL_FIELD]: true, error: { type: "invalid_request_error", message: REFUSAL_MESSAGE } },
};
}
return {
status: 422,
body: {
[BILI_FINAL_FIELD]: true,
error: {
type: "server_error",
code: "dsh_compaction_refused",
message: REFUSAL_MESSAGE,
retryable: false,
[BILI_FINAL_FIELD]: true,
},
},
};
}

/** #3013/#3022 (A3): cadence for the pipeline-lane storm log. The first
* refusal warns once (once-warn dedup on session.metadata), then refusals are
* silent unless this many more arrive — the field-reported storm re-fired
* every ~328ms for hours with ZERO log lines after the first, leaving the
* user staring at a stuck "生成中" with no clue (#3013). Same cadence caps the
* passthrough lane's per-refusal warn so an agentless storm cannot spam the
* log either. Constant, not config (#2155 precedent). */
export const DSH_REFUSAL_STORM_LOG_EVERY = 100;
29 changes: 23 additions & 6 deletions src/server/handle.ts
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ import { noInjectTool as knobNoInjectTool, rawDumpDir as knobRawDumpDir } from "
import { anthropicBetaContextWindow, BILI_HOP_HEADER, capRegistryWindowByStandard, expandedContextSuffixWindow, launcherContextWindow, launcherMaxOutput, windowSourceLogged } from "./context-window.js";
import { NO_IDENTITY_MESSAGE, safeSessionId } from "./headers.js";
import { demoteGate, hasLeakedBiliToolsOnly, isDshTitleRequest, isServerToolUtilityCall, isSideRequest, resolveSideLane, restoreOutputBudget, sideRequestGuard, stripLeakedBiliTools } from "./side-request.js";
import { DSH_COMPACTION_SHAPE_MSGS, detectPassthroughDshCompactionCall, dshCompactionRefusal, dshPassthroughGuardStats, isDshCompactionCall } from "./dsh-compaction-guard.js";
import { DSH_COMPACTION_SHAPE_MSGS, DSH_REFUSAL_STORM_LOG_EVERY, detectPassthroughDshCompactionCall, dshCompactionRefusal, dshPassthroughGuardStats, isDshCompactionCall } from "./dsh-compaction-guard.js";
import { artifactSeedHit, detectAcpArtifacts } from "./chain-artifacts.js";
import { piSubagentChannelFallback } from "./pi-subagent-channel.js";
import { FORCE_TEXT_PROTOCOL } from "./inject.js";
Expand Down Expand Up @@ -256,7 +256,15 @@ async function handleRequest(
if (protocol !== null && req.method === "POST" && bodyBuffer.length > 0 && opts.allowDshCompaction !== true
&& await detectPassthroughDshCompactionCall(protocol, bodyBuffer, headerValue(req, "content-encoding"), async (enc, buf) => (await decodeRequestBody(enc, buf, MAX_DECODED_REQUEST_BYTES, { signal: decodingSignal, onBytes: (bytes) => admission.observe(bytes) })).body)) {
dshPassthroughGuardStats.refusals += 1;
log("warn", `[passthrough] dsh native compaction call identified on the unattributed passthrough lane (#1117/#2709) — REFUSED, not forwarded: bili owns compression on this lane; a landed dsh checkpoint would durably shadow the raw history (#1729, cf. #1772/#1206). No session exists on this lane, so this log line (not the #2490 ledger) is the record; passthrough refusals so far: ${dshPassthroughGuardStats.refusals}`);
// #3013/#3022 (A3): first refusal warns; then every
// DSH_REFUSAL_STORM_LOG_EVERY-th refusal logs an info so an
// agentless retry storm stays visible without spamming (the
// field-reported storm re-fired every ~328ms — per-refusal warn
// would have written thousands of lines). This log line, not the
// #2490 ledger, is the only record on this lane.
if (dshPassthroughGuardStats.refusals === 1 || dshPassthroughGuardStats.refusals % DSH_REFUSAL_STORM_LOG_EVERY === 0) {
log(dshPassthroughGuardStats.refusals === 1 ? "warn" : "info", `[passthrough] dsh native compaction call identified on the unattributed passthrough lane (#1117/#2709) — REFUSED, not forwarded: bili owns compression on this lane; a landed dsh checkpoint would durably shadow the raw history (#1729, cf. #1772/#1206). No session exists on this lane, so this log line (not the #2490 ledger) is the record; passthrough refusals so far: ${dshPassthroughGuardStats.refusals}` + (dshPassthroughGuardStats.refusals === 1 ? "" : ` — retrying will keep failing (terminal; #3013): disable dsh auto-compaction (#2474) or opt in via allowDshCompaction (#2028)`));
}
const refusal = dshCompactionRefusal(protocol);
if (!res.headersSent && !res.writableEnded && !res.destroyed) {
res.writeHead(refusal.status, { "content-type": "application/json" });
Expand Down Expand Up @@ -807,12 +815,13 @@ async function handleRequest(
// parity with the other three lanes includes the #2601 model
// argument: a chain-broken model switch still migrates instead of
// forking when the plugin header corroborates it.
? (dshPersona && !sideRequestLike
? personaNamespace(
? (dshPersona && !sideRequestLike && personaSystemText !== ""
// Empty system is non-anchoring (kernel anchor semantics,
// same guard as the openai lane): ride the verbatim id.
? personaConversationOrder(
googleIdentity?.value ?? googleSignal,
personaSystemText,
(parsed as GoogleRequestBody).contents,
log,
personaSystemText,
pluginReportedModel(req.headers),
)
: (googleIdentity?.value ?? googleSignal))
Expand Down Expand Up @@ -1631,6 +1640,14 @@ async function handleRequest(
const prevBytes = typeof session.metadata["dshCompactionRefusedBytes"] === "number" ? session.metadata["dshCompactionRefusedBytes"] as number : 0;
session.metadata["dshCompactionRefusedBytes"] = Math.max(prevBytes, inboundBytes);
session.metadata["dshCompactionRefusals"] = (typeof session.metadata["dshCompactionRefusals"] === "number" ? session.metadata["dshCompactionRefusals"] as number : 0) + 1;
// #3013/#3022 (A3): after the once-warn above, refusals were fully
// silent — the field storm burned 5051 refusals with zero further
// log lines while the user saw a stuck "生成中". Every
// DSH_REFUSAL_STORM_LOG_EVERY-th refusal re-surfaces with the
// running count and the actual levers.
if ((session.metadata["dshCompactionRefusals"] as number) % DSH_REFUSAL_STORM_LOG_EVERY === 0) {
log("info", `[${session.id}] dsh native compaction refusal #${session.metadata["dshCompactionRefusals"]} on this session (raw-ledger high-water ${(session.metadata["dshCompactionRefusedBytes"] as number)} bytes) — dsh keeps retrying because its own raw pressure ledger never shrinks (#2490), and every retry of this call will be refused (terminal, #3013). Disable dsh auto-compaction (profile recipe #2474), manage context via bili's compress, or lift the refusal with allowDshCompaction (#2028). If bili's own compression looks idle while this repeats, the nudge channel may be dead — report it (#3004/#3013).`);
}
markDirty(session);
const refusal = dshCompactionRefusal(protocol);
if (!res.headersSent && !res.writableEnded && !res.destroyed) {
Expand Down
13 changes: 8 additions & 5 deletions tests/compress-success-tail.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -135,8 +135,10 @@ test("#2404: a rejected range keeps advertising the still-offered remainder (rec
});

test("#1387: clean success that drains every compressible range emits the stop signal (pi #521)", () => {
// Default preserveRecentMessages: 5 → after folding m00001–m00002 the rest
// Pinned preserveRecentMessages: 5 — after folding m00001–m00002 the rest
// (m00003–m00007) sits in the protected recent zone: nothing actionable left.
// (The kernel default dropped 5 → 3 in #3058; these tests exercise the
// #1387 tail contract, not the preserveRecent default.)
const msgs = [
textMsg("raw_1", "user", "x".repeat(20000)),
textMsg("raw_2", "assistant", "x".repeat(20000)),
Expand All @@ -146,7 +148,7 @@ test("#1387: clean success that drains every compressible range emits the stop s
textMsg("raw_6", "assistant", "x".repeat(5000)),
textMsg("raw_7", "user", "x".repeat(5000)),
];
const ctx = withRefs(makeCtx(msgs));
const ctx = withRefs(makeCtx(msgs, { preserveRecentMessages: 5 }));
ctx.session.stats.lastInputTokens = 80000;
const out = runApply(ctx, { content: [{ startId: "m00001", endId: "m00002", summary: "TAIL-TEST-SUMMARY-PAYLOAD-LONG-ENOUGH-FOR-THE-KERNEL-MIN-LENGTH-CHECK" }] });
assert.ok(out.startsWith("[Compressed"), `expected success, got: ${out.slice(0, 120)}`);
Expand Down Expand Up @@ -213,8 +215,9 @@ test("#2029: estimate-grade baseline must not suppress the stop signal via phant
});

test("#1387: partial failure (some ranges rejected) keeps the old silent tail", () => {
// m00003–m00007 sit entirely in the protected recent zone under the default
// config; pairing that range with a valid one exercises the partial-failure
// m00003–m00007 sit entirely in the protected recent zone (pinned
// preserveRecentMessages: 5, as above — #3058 moved the default to 3);
// pairing that range with a valid one exercises the partial-failure
// gate: no stop verdict while errors are still owed an answer (pi #521).
const msgs = [
textMsg("raw_1", "user", "x".repeat(20000)),
Expand All @@ -225,7 +228,7 @@ test("#1387: partial failure (some ranges rejected) keeps the old silent tail",
textMsg("raw_6", "assistant", "x".repeat(5000)),
textMsg("raw_7", "user", "x".repeat(5000)),
];
const ctx = withRefs(makeCtx(msgs));
const ctx = withRefs(makeCtx(msgs, { preserveRecentMessages: 5 }));
ctx.session.stats.lastInputTokens = 80000;
const out = runApply(ctx, { content: [
{ startId: "m00001", endId: "m00002", summary: "TAIL-TEST-SUMMARY-PAYLOAD-LONG-ENOUGH-FOR-THE-KERNEL-MIN-LENGTH-CHECK" },
Expand Down
4 changes: 2 additions & 2 deletions tests/golden/storage-render/anthropic.json
Original file line number Diff line number Diff line change
Expand Up @@ -80,12 +80,12 @@
"foldAt": 9,
"editedAt": 12,
"summaryFrom": 9,
"stateDigest": "e47a82482ed9d3fc1201562c2c539f5432873acb264862e055c5569396040aac",
"stateDigest": "858ed1c3988c25280181c34a9b30ddf78c86b4237eb5076ab311456a3001c322",
"sectionDigests": {
"state": "285aa622a037321b",
"blockContents": "b3a9c59b22ec1a3a",
"stats": "4271a6483e02458a",
"metadata": "f582165dae264d64"
"metadata": "32e9f789c44286ca"
},
"stateKeyDigests": {
"blocks": "8a3021f39f1f2eb8",
Expand Down
4 changes: 2 additions & 2 deletions tests/golden/storage-render/chat.json
Original file line number Diff line number Diff line change
Expand Up @@ -80,12 +80,12 @@
"foldAt": 9,
"editedAt": 12,
"summaryFrom": 9,
"stateDigest": "3a6d4239e182ebb6fe02a99faf40324a963d5f74f9bb0be0b1752f47bee65497",
"stateDigest": "7488de3b6fbb1f061c1fdc7e83b2b99009a936aef04dc36624e21fc161159903",
"sectionDigests": {
"state": "6e905e4ae1e678b8",
"blockContents": "dd2de296a5bc6160",
"stats": "ed7d3e3f253e0942",
"metadata": "1f78ed1b58e4a77e"
"metadata": "59f40d0ef9c2755e"
},
"stateKeyDigests": {
"blocks": "bdde93b0e100e13e",
Expand Down
4 changes: 2 additions & 2 deletions tests/golden/storage-render/google.json
Original file line number Diff line number Diff line change
Expand Up @@ -80,12 +80,12 @@
"foldAt": 9,
"editedAt": 12,
"summaryFrom": 9,
"stateDigest": "91d1cdd51aed493c3f1c1e4857e4a768cd43be23cc69647ca3f2376f4270e74a",
"stateDigest": "65929f00d8a26805ef859fd9ab9197d605e0531bccb17813c3457019c617ad54",
"sectionDigests": {
"state": "0725cfe81690b748",
"blockContents": "4cc83a808a659f28",
"stats": "096da9a1f61d84d0",
"metadata": "a615198f56928645"
"metadata": "0fb314268fb92034"
},
"stateKeyDigests": {
"blocks": "9adb53303cd4b786",
Expand Down
4 changes: 2 additions & 2 deletions tests/golden/storage-render/responses.json
Original file line number Diff line number Diff line change
Expand Up @@ -80,12 +80,12 @@
"foldAt": 9,
"editedAt": 12,
"summaryFrom": 9,
"stateDigest": "bbb968638e19d82d097871d73240d0dbabdbd3978fb7b6e140960c232e1de1bb",
"stateDigest": "c326a242c21c86ec70b0c148fc8c4951cb31f449e125f97a970a7130444a1ec4",
"sectionDigests": {
"state": "7194736468931151",
"blockContents": "1727e2e1faf79b55",
"stats": "062b6eaaeb325945",
"metadata": "cd0990da20e0f33c"
"metadata": "3b76417727dbe3d0"
},
"stateKeyDigests": {
"blocks": "0458e9c8a7bb1a99",
Expand Down
Loading
Loading