From d6b5f06d5dde1ab3494a3b314719b49b8dc8dbf9 Mon Sep 17 00:00:00 2001 From: ework-agent Date: Thu, 5 Nov 2026 02:15:30 +0800 Subject: [PATCH 1/2] =?UTF-8?q?#5686=20P0-1/P0-2/P1-1/P1-2:=20dsh=20drift?= =?UTF-8?q?=20consolidation=20=E2=80=94=20close=20the=20last=20native-comp?= =?UTF-8?q?action=20leak=20window,=20generalize=20the=20overlay=20alarm,?= =?UTF-8?q?=20fork=20visibility,=20tagChanged=20shorter-tail=20reanchor?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit P0-1 (native-intercept.ts): every degraded DIRECT send (no live proxy / respawn exhausted / gateless host process) now marker-probes the body at the single choke point directSendOrRefuse(); a MARKER-DECISIVE dsh native-compaction summarize call (final user message starts with COMPACTION_INSTRUCTION via isDshCompactionCall) is refused LOCALLY with a protocol-shaped 422 (x-bili-gate: dsh-compaction-refused) instead of being forwarded raw upstream where it would land a checkpoint that durably shadows the session's raw history (#1729 doctrine, #5517/#5656 cluster). Mention-only directive text, google/custom wires, and unreadable/stream bodies keep today's fail-open semantics; routed /bili/ dispatches are excluded (server guards handle them); synchronous origin test avoids adding the 15s readiness hold to every degraded send. P0-2 (dsh-native.ts): detectCompactionOverlayGap now runs for EVERY profile shape, not just @deepseek-ai/dsh-web-app bundles; a failed latch boots an alarm on any form (desktop app-in-bundle layouts used to arm silently). P1-1 (session-resolve.ts + handle.ts): personaConversationOrder gains an optional observer callback; the six production call sites pass a handler that durably logs state-changing outcomes ([session-resolve] reanchor/derived-*), silent on claim/continue — forks no longer vanish. P1-2 (session-resolve.ts): new rung 5 in the tagChanged branch — a SHORTER incoming that keeps the recorded conversation's fragment-verified tail reanchors instead of forking (checkpoint landing + per-turn system-prompt rotation no longer deterministically mints |sub: lanes). Tests: issue5686-compaction-direct-refusal (4 cases incl. origin-alive route regression), issue5686-tagchanged-shorter-tail-reanchor (4 cases incl. observer capture), dsh-native P0-2 non-web alarm case; native-intercept falls-open case rewritten to expect the local refusal. --- src/agent/dsh-native.ts | 26 ++-- src/agent/native-intercept.ts | 120 +++++++++++++++-- src/server/handle.ts | 32 ++++- src/session-resolve.ts | 38 +++++- tests/dsh-native.test.ts | 42 ++++++ ...ssue5686-compaction-direct-refusal.test.ts | 113 ++++++++++++++++ ...6-tagchanged-shorter-tail-reanchor.test.ts | 123 ++++++++++++++++++ tests/native-intercept.test.ts | 11 +- 8 files changed, 477 insertions(+), 28 deletions(-) create mode 100644 tests/issue5686-compaction-direct-refusal.test.ts create mode 100644 tests/issue5686-tagchanged-shorter-tail-reanchor.test.ts diff --git a/src/agent/dsh-native.ts b/src/agent/dsh-native.ts index 08e4345ea0..31e186636a 100644 --- a/src/agent/dsh-native.ts +++ b/src/agent/dsh-native.ts @@ -2684,15 +2684,23 @@ export function apply(ctx: PluginContext): void { } catch { // older dsh builds without the service: diagnostic only } - if (bundles !== undefined && bundles.includes("@deepseek-ai/dsh-web-app")) { - webProfileWarned = true; - // #3806: overlay failed to latch -> escalate the passive note into a boot-time alarm. - const gap = detectCompactionOverlayGap(ctx); - if (gap !== null) { - persistClientEvent(`profile bundles include @deepseek-ai/dsh-web-app and the load-time compaction-disable overlay (#3354) could not latch on this dsh build (${gap}) — dsh native auto-compaction is likely ARMED here. bili's wire-level refusal (#1729) refuses routed compaction calls but CANNOT stop dsh writing its checkpoint locally, so a landed checkpoint will destroy the compression substrate and surface later as a 'compression conflict' banner. Apply the #2474 full-snapshot override of preset-standard in your profile's cordis.patch.yml now (a partial snippet silently drops everything not restated; see CONFIGURATION.md)`); - } else { - persistClientEvent("profile bundles include @deepseek-ai/dsh-web-app — its agent presets run their own compaction-basic in nested rows no patch layer can address by id (#1772/#2360); bili disables those preset rows at load time via a vendored overlay (deshark42/dsh-compaction-basic-disable, MIT, #3354; since #4147 the match is structural, covering custom presets too), so dsh native auto-compaction stays OFF in this profile (ACP compression unaffected). If you still see a dsh compaction checkpoint land on a newer/older dsh build — the overlay rides cordis' INTERNAL 'internal/config' hook and is version-sensitive — fall back to the #2474 full-snapshot override of preset-standard in your profile's cordis.patch.yml (a partial snippet silently drops everything not restated; see CONFIGURATION.md)." + gateCoverageSuffix()); - } + // #5686: the latch check (pure structural typeof probes on the cordis + // host surface — cheap, side-effect-free) runs for EVERY profile + // shape now. The pre-#5686 code only called it inside the + // @deepseek-ai/dsh-web-app branch, so every other form — in + // particular the desktop app-in-bundle layout where NO @deepseek-ai/* + // package sits under the profile and the core ships inside the app — + // booted SILENTLY when the #3354 disable middleware could not latch: + // native auto-compaction armed with zero boot-time signal while bili's + // wire refusals were its only backstop. + const isWeb = bundles !== undefined && bundles.includes("@deepseek-ai/dsh-web-app"); + const gap = detectCompactionOverlayGap(ctx); + webProfileWarned = true; + if (gap !== null) { + // #3806/#5686: overlay failed to latch -> boot-time alarm on ANY profile shape. + persistClientEvent(`profile${isWeb ? " bundles include @deepseek-ai/dsh-web-app and" : bundles === undefined ? " shape could not be introspected (older dsh build) and" : " (non-web-app bundle list) and"} the load-time compaction-disable overlay (#3354) could not latch on this dsh build (${gap}) — dsh native auto-compaction is likely ARMED here. bili's wire-level refusal (#1729) refuses routed compaction calls but CANNOT stop dsh writing its checkpoint locally, so a landed checkpoint will destroy the compression substrate and surface later as a 'compression conflict' banner. Apply the #2474 full-snapshot override of preset-standard in your profile's cordis.patch.yml now (a partial snippet silently drops everything not restated; see CONFIGURATION.md)`); + } else if (isWeb) { + persistClientEvent("profile bundles include @deepseek-ai/dsh-web-app — its agent presets run their own compaction-basic in nested rows no patch layer can address by id (#1772/#2360); bili disables those preset rows at load time via a vendored overlay (deshark42/dsh-compaction-basic-disable, MIT, #3354; since #4147 the match is structural, covering custom presets too), so dsh native auto-compaction stays OFF in this profile (ACP compression unaffected). If you still see a dsh compaction checkpoint land on a newer/older dsh build — the overlay rides cordis' INTERNAL 'internal/config' hook and is version-sensitive — fall back to the #2474 full-snapshot override of preset-standard in your profile's cordis.patch.yml (a partial snippet silently drops everything not restated; see CONFIGURATION.md)." + gateCoverageSuffix()); } else if (bundles !== undefined && !nonWebTraceLogged) { // #3651: the web-profile note above is the ONLY confirmation that // the #3354 overlay took effect, and it is scoped to dsh-web-app diff --git a/src/agent/native-intercept.ts b/src/agent/native-intercept.ts index 93251db334..ca79d33427 100644 --- a/src/agent/native-intercept.ts +++ b/src/agent/native-intercept.ts @@ -14,7 +14,7 @@ import { mediaBytesOfWireBody } from "./media-bytes.js"; import * as pfChannel from "./pf-channel.js"; import { probeLiveness, type LivenessVerdict } from "../liveness.js"; import { BILI_PASSTHROUGH_HEADER, MIN_ASSUMED_WINDOW_TOKENS } from "../util.js"; -import { DSH_COMPACTION_MARKER_SNIFF } from "../server/dsh-compaction-guard.js"; +import { DSH_COMPACTION_MARKER_SNIFF, isDshCompactionCall } from "../server/dsh-compaction-guard.js"; import { APIG_RESIGN_HEADER, APIG_RESIGN_CREDENTIAL_HEADER, APIG_RESIGN_SCHEME, clearSignedRefusal, encodeApigCredential, inboundSignedScheme, recordSignedRefusal, resignEnabled, resignPassthroughEnabled, signedRefusal } from "../apig-resign.js"; /** #2924: takeover-gate verdict. `true` claims the request; `false` keeps the @@ -462,39 +462,72 @@ function outgoingHeadersOf(input: string | URL | Request, init?: RequestInit): R * original must stay sendable (#2496). init.body wins over a Request * input's own body (spec: init overrides), mirroring fetchMethodOf. */ async function dshCompactionMarkerInBody(input: string | URL | Request, init?: RequestInit): Promise { - const marker = DSH_COMPACTION_MARKER_SNIFF; + const text = await readableModelBodyText(input, init); + return text !== undefined && text.includes(DSH_COMPACTION_MARKER_SNIFF); +} + +/** #5686: readable plain-text form of a dispatch body for marker probing. + * Returns the full body text, or undefined when the form cannot be read + * without disturbing the dispatch (ReadableStream/FormData bodies) or + * cannot be decoded (#4912 RangeError on oversized buffers) — callers must + * treat undefined as FAIL OPEN, never as evidence either way. A + * Request-object input with no init.body is probed through a CLONE so the + * original stays sendable (#2496); init.body wins over the Request's own + * body (spec: init overrides), mirroring fetchMethodOf. */ +async function readableModelBodyText(input: string | URL | Request, init?: RequestInit): Promise { const body: unknown = init?.body; if (body === undefined) { - if (input === null || typeof input !== "object" || input instanceof URL) return false; + if (input === null || typeof input !== "object" || input instanceof URL) return undefined; try { const probe = (input as Request).clone(); - return (await probe.text()).includes(marker); + return await probe.text(); } catch { - return false; + return undefined; } } - if (typeof body === "string") return body.includes(marker); - if (body instanceof URLSearchParams) return body.toString().includes(marker); + if (typeof body === "string") return body; + if (body instanceof URLSearchParams) return body.toString(); if (body instanceof Blob) { try { - return (await body.text()).includes(marker); + return await body.text(); } catch { - return false; + return undefined; } } if (body instanceof Uint8Array || body instanceof ArrayBuffer) { const bytes = body instanceof ArrayBuffer ? body : new Uint8Array(body.buffer as ArrayBuffer, body.byteOffset, body.byteLength); try { - return new TextDecoder().decode(bytes).includes(marker); + return new TextDecoder().decode(bytes); } catch { // #4912: undecodable buffer (e.g. >V8-max-string-length, where // decode throws RangeError) — fail OPEN like every other // unreadable form above; the probe must never throw out of the // intercept (it runs on EVERY POST, proxy up or down). - return false; + return undefined; } } - return false; + return undefined; +} + +/** #5686: which protocol wire shape this model URL speaks, for the + * client-side marker-DECISIVE check (isDshCompactionCall needs the wire + * family to know whether to read messages[] or input[]). chat/completions + * and bare /completions speak openai; /responses speaks responses; + * /messages speaks anthropic (openai-compatible gateways included — both + * read messages[]). Google/custom wires are undecidable here and stay out + * of scope exactly like the server-side guard header rules (§PROTOCOL + * COVERAGE). */ +function modelProtocolHintFromUrl(url: string): string | null { + let pathname: string; + try { + pathname = new URL(url).pathname.toLowerCase(); + } catch { + pathname = url.toLowerCase(); + } + if (pathname.includes("/chat/completions") || pathname.endsWith("/completions")) return "openai"; + if (pathname.includes("/responses")) return "responses"; + if (pathname.endsWith("/messages")) return "anthropic"; + return null; } /** #2924: best-effort body size for the gate's oversize arm — strings and @@ -600,6 +633,26 @@ function unattributedOversizeRefusal(url: string): Response { return new Response(JSON.stringify({ error: { type: "server_error", code: "bili_gate_oversize_refusal_unattributed", message, retryable: false } }), { status: 422, headers }); } +/** #5686: local refusal for a MARKER-DECISIVE dsh native-compaction summarize + * call on a path about to degrade to a DIRECT send (no usable proxy right + * now). Forwarding would let the summarization checkpoint land upstream and + * dsh frame it into a checkpoint that DURABLY SHADOWS the session's raw + * history — irreversible compression-substrate loss (#1729 doctrine); " + * either compress, or refuse" leaves refusal as the only legal outcome here + * (#3651). Distinct from the server's refusal (which needs the session + * ledger): this fires where no bili instance answered at all. Protocol- + * shaped like oversizeRefusal so the host treats it as a policy refusal of + * ONE request; dsh logs 'step compaction failed' and continues the turn. */ +function compactionDirectRefusal(url: string): Response { + const message = + "billion-context refused this request locally: it is a dsh native-compaction summarize call (final user message starts with the COMPACTION_INSTRUCTION directive), and no usable bili proxy was available at this moment \u2014 sending it directly to the provider would make the summarization checkpoint land durably and shadow this conversation's raw history, destroying bili's compression base (#1729/#5686). This ONE compaction attempt fails; dsh continues the turn normally. Restore or start the bili proxy (or restart the host) so the next compaction can run under bili."; + const headers = { "content-type": "application/json", "x-bili-gate": "dsh-compaction-refused" }; + if (url.endsWith("/messages")) { + return new Response(JSON.stringify({ type: "error", error: { type: "invalid_request_error", message } }), { status: 422, headers }); + } + return new Response(JSON.stringify({ error: { type: "server_error", code: "bili_gate_dsh_compaction_refused", message, retryable: false } }), { status: 422, headers }); +} + /** #5405: the empty-slot branch has no attribution and no window knowledge. * Small by-design direct sends must still pass; a multi-megabyte model POST * is the one shape that cannot be allowed to ride out silently. Return a @@ -1322,6 +1375,49 @@ export function installNativeFetchIntercept(initialState: NativeInterceptState): logHostEvent("error", "[native-client]", `bili-native: no usable proxy and the request carries a lane-only payload — failing locally instead of sending it to the provider; retry the turn (#3304): ${dispatchUrl}`); throw new TypeError("billion-context: proxy unavailable — the request payload is lane-specific (incremental turn) and cannot be forwarded to the provider directly; retry the turn (#3304)"); } + // #5686: the last leak window of the #1729 doctrine — every + // degraded direct send (no live proxy / respawn exhausted / + // gateless host process) used to carry a dsh native-compaction + // summarize call STRAIGHT to the provider, whose normal reply dsh + // then frames into a checkpoint that DURABLY SHADOWS this session's + // raw history (compression substrate destroyed, identity fork + // follows — the #5517/#5656 report cluster). "Either compress, + // or refuse" (#3651 ruling): with no usable proxy, refusal is the + // only legal outcome for THIS call class. Only a MARKER-DECISIVE + // call refuses (final user message starts with COMPACTION_ + // INSTRUCTION via isDshCompactionCall — quoting the directive + // mid-body, Google/custom wires, and unreadable/stream bodies keep + // today's direct semantics exactly; fail-open per #2496/#4912). + // Routed /bili/ dispatches are excluded — they still reach a bili + // instance whose own guards (#2043 lane) handle the call with full + // session evidence. Synchronous origin test on purpose: awaiting + // readyOrigin here would hold every degraded send up to the 15 s + // readiness timeout, and each caller below already ran that wait. + if ( + state.origin === undefined && + routedBiliModelUrl(dispatchUrl) === undefined && + fetchMethodOf(defaultInput, init) === "POST" && + isModelApiUrl(dispatchUrl) + ) { + const bodyText = prep.outcome === "restored" ? prep.body : await readableModelBodyText(defaultInput, init); + if (bodyText !== undefined && bodyText.includes(DSH_COMPACTION_MARKER_SNIFF)) { + const hint = modelProtocolHintFromUrl(dispatchUrl); + if (hint !== null) { + let parsed: unknown; + try { + parsed = JSON.parse(bodyText); + } catch { + parsed = undefined; + } + if (parsed !== undefined && isDshCompactionCall(hint, parsed)) { + state.onDispatch?.(dispatchUrl, "refused"); + if (degradeLog !== undefined) noteDispatchMode("refused", `bili-native: ${degradeLog} — dsh native compaction summarize call refused locally (#5686)`); + logHostEvent("error", "[native-client]", `bili-native: dsh native compaction summarize call refused LOCALLY (422) instead of a direct send — no usable bili proxy at this moment, and forwarding would let the checkpoint land upstream and durably shadow this session's raw history (#1729/#5686); dsh logs 'step compaction failed' and continues the turn: ${dispatchUrl}`); + return compactionDirectRefusal(dispatchUrl); + } + } + } + } // #4516/#4894: without a live proxy the raw history rides straight // to the provider — refuse LOCALLY what provably cannot fit the // model's own window: the upstream rejects it and every retry diff --git a/src/server/handle.ts b/src/server/handle.ts index f944ca4cac..d13b18621a 100644 --- a/src/server/handle.ts +++ b/src/server/handle.ts @@ -35,7 +35,19 @@ import { emitPreflightError } from "../stream-error.js"; import { agentProviderRecipes } from "../agent-providers.js"; import { affinityToken, anthropicFirstUserFrag, claudeAnthropicPersonaOrderApplies, conversationHeaderSource, claudeSubagentAgentId, claudeSubagentSplit, claudeSubagentLaneTokenOf, clientConversationHeader, codexTurnIdentity, dshPersonaFingerprintApplies, instructionsFingerprintApplies, openaiSystemTextForPersona, preferPromptCacheKeyIdentity, zcodeMemoryConversation } from "../session-id.js"; import { preferKiroConversationIdentity } from "../kiro.js"; -import { personaConversationOrder } from "../session-resolve.js"; +import { personaConversationOrder, type SessionOrderResolution } from "../session-resolve.js"; + +/** #5686: fork/migration visibility on the server side. personaConversation + * used to throw away every outcome but the key, so a production fork + * (derived-*) or migration (reanchor) left NO durable line — the entire + * dsh |sub:-drift cluster had to be reconstructed from user screenshots. + * Log the state-changing outcomes; claim/continue stay silent (they are the + * steady state and would flood the log). derived lanes are the incident + * class → warn level. */ +function onSessionResolveEvent(rawId: string, r: SessionOrderResolution): void { + if (r.action !== "reanchor" && !r.action.startsWith("derived-")) return; + loggerLog(r.action.startsWith("derived-") ? "warn" : "info", `[session-resolve] ${r.action}: ${rawId} -> ${r.key}${r.reason !== undefined ? ` (${r.reason})` : ""}`); +} import { prefixAffinity, describeResumeMiss, type AnonymousAffinity } from "../prefix-affinity.js"; import { buildAnonymousProvenance, type AnonymousProvenance } from "../anon-provenance.js"; import { maybeAdoptForkBlocks, maybeAdoptResume, maybeAdoptDerivedFork } from "../fork-adoption.js"; @@ -1106,8 +1118,11 @@ async function handleRequest( (parsed as GoogleRequestBody).contents, personaSystemText, pluginReportedModel(req.headers), + undefined, + undefined, + onSessionResolveEvent, )) - : (googleIdentity?.value ?? googleSignal)) + : (googleIdentity?.value ?? googleSignal)) : protocol === "anthropic" ? // #970: a subagent's conversation value gets its own // `|sub:` namespace so it lands on its own session @@ -1135,6 +1150,9 @@ async function handleRequest( (parsed as AnthropicRequestBody).messages, personaSystemText, pluginReportedModel(req.headers), + undefined, + undefined, + onSessionResolveEvent, )) : claudeAnthropicPersonaOrderApplies(req.headers) && opts.subagentSplit !== false && !sideRequestLike && personaSystemText !== "" @@ -1165,6 +1183,7 @@ async function handleRequest( pluginReportedModel(req.headers), claudeSubagentLaneTokenOf((parsed as AnthropicRequestBody).messages), anthropicFirstUserFrag((parsed as AnthropicRequestBody).messages), + onSessionResolveEvent, ) : anthropicIdentity?.value ?? anthropicSignal) : protocol === "openai" @@ -1174,6 +1193,9 @@ async function handleRequest( (parsed as OpenAIRequestBody).messages, personaSystemText, pluginReportedModel(req.headers), + undefined, + undefined, + onSessionResolveEvent, ) : openaiIdentity?.value ?? openaiSignal) : codexTurn @@ -1196,6 +1218,9 @@ async function handleRequest( (parsed as ResponsesRequestBody).input, (parsed as ResponsesRequestBody).instructions ?? "", pluginReportedModel(req.headers), + undefined, + undefined, + onSessionResolveEvent, ) : opts.stableSystemAnchor && pluginAgentHeader(req.headers) === undefined // #1085: with anchoring on (plain-proxy mode only — see the @@ -1236,6 +1261,9 @@ async function handleRequest( (parsed as ResponsesRequestBody).input, (parsed as ResponsesRequestBody).instructions ?? "", pluginReportedModel(req.headers), + undefined, + undefined, + onSessionResolveEvent, ) : (responsesIdentity?.value ?? conversationSignalResponses(parsed as ResponsesRequestBody, convHeader))) : (responsesIdentity?.value ?? conversationSignalResponses(parsed as ResponsesRequestBody, convHeader)); diff --git a/src/session-resolve.ts b/src/session-resolve.ts index a9b4e95b29..1d44dab3a8 100644 --- a/src/session-resolve.ts +++ b/src/session-resolve.ts @@ -349,6 +349,23 @@ export function resolveSessionOrder(rawId: string, history: unknown, personaTag? record(rawId, turns, tagHash, effectiveModel); return { key: rawId, action: "reanchor", reason: "persona text changed but the recorded history continues inside the incoming — anchor migrated (#4778)" }; } + // #5686: fifth rung — the composite field failure behind the + // |sub:-drift cluster (#5517/#5656 reports): a native-compaction + // checkpoint LANDS (head cells replaced by one summary message) while + // the host simultaneously rotates its per-turn system prompt (dsh NEXT + // harness-state banners). The incoming is then SHORTER than the + // recorded skeleton with tagChanged true, which defeats every rung + // above (head fragments broken by the summary, no model switch, chain + // depth insufficient, length gate false) and deterministically forks + // the live lane onto |sub: — whose first visible + // message is the checkpoint sentence itself. The fix is the exact + // mirror of the tag-LESS shorter-reanchor below (same two tests, same + // TAIL_WINDOW, no extra floor): a byte-identical fragment-verified + // tail cannot belong to a different conversation (MESSAGE-IDENTITY). + if (kinds.length > 0 && kinds.length < recorded.kinds.length && tailAligns(kinds, recorded.kinds) && fragTailEquals(kinds.length)) { + record(rawId, turns, tagHash, effectiveModel); + return { key: rawId, action: "reanchor", reason: "persona text changed but the shorter incoming keeps the recorded conversation's fragment-verified tail — head rewrite under a new system prompt, anchor kept (#5686)" }; + } // #5095 D1-B: a non-empty discriminator (only the claude-anthropic // headless-subagent call site passes one) widens the key into a // three-segment form so distinct same-type runs never share a @@ -454,6 +471,23 @@ export function applyOrderSnapshot(entries: OrderSkeletonEntry[]): number { * ladder consumed (systemText, model) and keyed by system-hash; the v2 * resolver is content-blind and keys by order+id with fragment guards, * so those parameters are simply gone. */ -export function personaConversationOrder(rawId: string, history: unknown, personaTag?: string, declaredModel?: string, laneToken?: string, laneDiscriminator?: string): string { - return resolveSessionOrder(rawId, history, personaTag ?? "", declaredModel, laneToken, laneDiscriminator).key; +/** #5686 visibility seam (optional): observers of state-changing outcomes. + * Production used to discard everything but the key, so every fork + * (derived-*) and migration (reanchor) ran with zero durable trace — the + * drift cluster had to be reverse-engineered from field screenshots. Claim + * and continue stay silent (steady-state volume); the caller decides what + * to log. The callback is best-effort: a throwing observer must never break + * resolution. */ +export type SessionResolveObserver = (rawId: string, r: SessionOrderResolution) => void; + +export function personaConversationOrder(rawId: string, history: unknown, personaTag?: string, declaredModel?: string, laneToken?: string, laneDiscriminator?: string, onResolve?: SessionResolveObserver): string { + const res = resolveSessionOrder(rawId, history, personaTag ?? "", declaredModel, laneToken, laneDiscriminator); + if (onResolve !== undefined) { + try { + onResolve(rawId, res); + } catch { + // observer hook — diagnostics only + } + } + return res.key; } diff --git a/tests/dsh-native.test.ts b/tests/dsh-native.test.ts index d1c76b43ee..d8a7dab8b0 100644 --- a/tests/dsh-native.test.ts +++ b/tests/dsh-native.test.ts @@ -1278,6 +1278,48 @@ test("#3806 apply(): a degraded web profile alarms loudly instead of the silent } }); +test("#5686 P0-2 apply(): a degraded NON-WEB profile alarms loudly too (no silent arming)", async () => { + const proxy = await startMockProxy([]); + const home = fs.mkdtempSync(path.join(os.tmpdir(), "bili-dsh-5686-")); + const stateHome = fs.mkdtempSync(path.join(os.tmpdir(), "bili-dsh-5686-state-")); + const logFile = path.join(stateHome, "billion-context", "bili.log"); + const clientLines = (): string[] => fs.existsSync(logFile) + ? fs.readFileSync(logFile, "utf8").split("\n").filter((l) => l.includes("[dsh-client]")) + : []; + _setSpawnForTest(async () => undefined); + try { + await withEnv({ DSH_HOME: home, BILLION_CONTEXT_PROXY: proxy.origin, XDG_STATE_HOME: stateHome, BILI_PROVIDER_REWRITES: undefined, BILI_NATIVE_DSH: undefined, BILLION_CONTEXT_PLUGIN: undefined }, async () => { + _resetRegisterForTest(proxy.origin); + _resetWebProfileWarningForTest(); + // headless profile (desktop app-in-bundle shape: no @deepseek-ai/* + // package under the profile) + a host without the internal/config + // hook — pre-#5686 this booted SILENTLY (info trace only). + const bare: Parameters[0] = { + tools: { register: () => undefined }, + commands: { register: () => undefined }, + agents: {}, + profileContext: { startedBundles: ["@deepseek-ai/dsh-base", "@deepseek-ai/dsh-headless", "billion-context"] }, + }; + apply(bare); + const lines = clientLines(); + assert.equal(lines.length, 1); + assert.match(lines[0], /non-web-app bundle list/); + assert.match(lines[0], /could not latch/i); + assert.match(lines[0], /no-config-hook/); + assert.match(lines[0], /ARMED/i); + assert.match(lines[0], /#2474/); + await _stateToolsReadyForTest(); + }); + } finally { + _setSpawnForTest(undefined); + proxy.close(); + rmrf(home); + rmrf(stateHome); + _resetRegisterForTest(undefined); + _resetWebProfileWarningForTest(); + } +}); + test("#3354/#3662 apply(): declares lane ownership instead of registering the overlay", async () => { const proxy = await startMockProxy([]); const home = fs.mkdtempSync(path.join(os.tmpdir(), "bili-dsh-cd-")); diff --git a/tests/issue5686-compaction-direct-refusal.test.ts b/tests/issue5686-compaction-direct-refusal.test.ts new file mode 100644 index 0000000000..fc576106b3 --- /dev/null +++ b/tests/issue5686-compaction-direct-refusal.test.ts @@ -0,0 +1,113 @@ +// #5686: the last leak window of the #1729 doctrine — every DEGRADED direct +// send (no usable bili proxy at that moment) used to carry a dsh +// native-compaction summarize call STRAIGHT to the provider; dsh then frames +// the normal reply into a checkpoint that DURABLY SHADOWS the session's raw +// history (compression substrate destroyed → identity fork follows: the +// #5517/#5656 report cluster). "Either compress, or refuse" (#3651): on a +// no-proxy path the refusal is the only legal outcome for THIS call class. +// +// Change under test (src/agent/native-intercept.ts): inside directSendOrRefuse +// (the single choke point of all six degraded dispatches), a MARKER-DECISIVE +// compaction call (final user message starts with COMPACTION_INSTRUCTION per +// isDshCompactionCall) is refused LOCALLY with a protocol-shaped 422 instead +// of being sent direct. Everything non-decisive keeps today's semantics +// exactly (mid-body mention, unreadable/stream bodies, unknown wires), and a +// LIVE origin never enters this branch (routed /bili/ traffic stays with the +// bili instance's own #2043-lane guard). + +import test from "node:test"; +import assert from "node:assert/strict"; +import { installNativeFetchIntercept, _resetForTest, type NativeInterceptState } from "../src/agent/native-intercept.ts"; + +const DSH_COMPACT_DIRECTIVE = "You are now acting as a compaction engine for this AI coding assistant. Summarize the conversation."; + +/** rc.2-shaped shadowed-region replay: many messages, directive LAST user. */ +function dshCompactionEnvelope(): string { + const region = []; + for (let i = 0; i < 12; i++) { + region.push({ role: i % 2 === 0 ? "user" : "assistant", content: `region turn ${i} ` + "x".repeat(40) }); + } + region.push({ role: "user", content: DSH_COMPACT_DIRECTIVE }); + return JSON.stringify({ model: "deepseek-chat", messages: region }); +} + +/** The directive QUOTED mid-body but NOT as the final user message: a plain + * coding conversation about the directive must keep direct semantics. */ +function mentionOnlyBody(): string { + return JSON.stringify({ + model: "deepseek-chat", + messages: [ + { role: "system", content: DSH_COMPACT_DIRECTIVE }, // quoted in system context — not decisive either + { role: "user", content: "Please review my notes about: You are now acting as a compaction engine for this AI coding assistant…" }, + { role: "assistant", content: "Noted." }, + { role: "user", content: "Now continue editing index.ts" }, + ], + }); +} + +type Recorded = { url: string; init?: RequestInit }; +async function withPatch(state: NativeInterceptState, fn: (fetch: typeof globalThis.fetch) => Promise): Promise<{ calls: Recorded[]; result: T }> { + const saved = globalThis.fetch; + _resetForTest(); + const calls: Recorded[] = []; + const fake = (async (input: string | URL | Request, init?: RequestInit) => { + calls.push({ url: typeof input === "string" ? input : input instanceof URL ? input.href : (input as Request).url, init }); + return new Response("{}", { status: 200 }); + }) as typeof fetch; + globalThis.fetch = fake; + try { + assert.equal(installNativeFetchIntercept(state), true); + const result = await fn(globalThis.fetch); + return { calls, result }; + } finally { + globalThis.fetch = saved; + _resetForTest(); + } +} + +/** No usable proxy right now (origin never resolved) + marker-decisive body. */ +const DEAD_PROXY: NativeInterceptState = { origin: undefined, ready: Promise.resolve(undefined), takeoverGate: () => false }; + +test("#5686: decisive dsh compaction POST with dead proxy refuses LOCALLY (no upstream fetch)", async () => { + const state: NativeInterceptState = { ...DEAD_PROXY, onDispatch: () => {} }; + const { calls, result } = await withPatch(state, async (fetch) => { + const res = await fetch("https://api.deepseek.com/chat/completions", { method: "POST", body: dshCompactionEnvelope() }); + assert.equal(res.status, 422); + assert.equal(res.headers.get("x-bili-gate"), "dsh-compaction-refused"); + const j = (await res.json()) as Record; + assert.equal((j.error as Record).code, "bili_gate_dsh_compaction_refused"); + assert.match(String((j.error as Record).message), /dsh native-compaction summarize call/); + return res; + }); + void result; + assert.equal(calls.length, 0, "the upstream provider must never see this request"); +}); + +test("#5686: anthropic /messages shape gets the /messages protocol envelope", async () => { + const state: NativeInterceptState = { ...DEAD_PROXY }; + await withPatch(state, async (fetch) => { + const res = await fetch("https://api.anthropic.com/v1/messages", { method: "POST", body: JSON.stringify({ max_tokens: 4096, messages: [{ role: "user", content: DSH_COMPACT_DIRECTIVE }] }) }); + assert.equal(res.status, 422); + const j = (await res.json()) as Record; + assert.equal(j.type, "error"); + assert.equal((j.error as Record).type, "invalid_request_error"); + }); +}); + +test("#5686: directive MENTIONED mid-body is not decisive — direct send preserved", async () => { + const state: NativeInterceptState = { ...DEAD_PROXY }; + const { calls } = await withPatch(state, async (fetch) => { + const res = await fetch("https://api.deepseek.com/chat/completions", { method: "POST", body: mentionOnlyBody() }); + assert.equal(res.status, 200); // straight through to (fake) upstream + }); + assert.deepEqual(calls.map((c) => c.url), ["https://api.deepseek.com/chat/completions"]); +}); + +test("#5686: live origin never touches the choke — routed traffic unchanged (#2592 regression)", async () => { + const state: NativeInterceptState = { origin: "http://127.0.0.1:40101", ready: Promise.resolve("http://127.0.0.1:40101"), takeoverGate: () => false }; + const { calls } = await withPatch(state, async (fetch) => { + const res = await fetch("https://api.deepseek.com/chat/completions", { method: "POST", body: dshCompactionEnvelope() }); + assert.equal(res.status, 200); + }); + assert.deepEqual(calls.map((c) => c.url), ["http://127.0.0.1:40101/bili/https://api.deepseek.com/chat/completions"]); +}); diff --git a/tests/issue5686-tagchanged-shorter-tail-reanchor.test.ts b/tests/issue5686-tagchanged-shorter-tail-reanchor.test.ts new file mode 100644 index 0000000000..1503bc4c72 --- /dev/null +++ b/tests/issue5686-tagchanged-shorter-tail-reanchor.test.ts @@ -0,0 +1,123 @@ +// #5686 (P1-2 + P1-1): the A∩B composite drift shape — a dsh native +// compaction checkpoint LANDED under bili's feet (overflow-force-compact + +// RETRY of the same in-flight request: head replaced by the checkpoint, +// tail byte-identical and therefore STRICTLY SHORTER overall), arriving on +// a turn whose system prompt dsh re-stamped differently (tagChanged). Every +// existing migration rung loses that shape: head alignment breaks +// (checkpoint ≠ first turn), byte-exact chains lack depth, and the #4778 +// positional run is length-gated (`kinds.length >= recorded`). Result: the +// WHOLE MAIN CONVERSATION re-keys onto `|sub:` — orphaning fold +// state, and because the forked lane's first visible message IS the +// checkpoint sentence, users see "session drifted, new session titled with +// a checkpoint" (#5517/#5656 cluster). +// +// Fix (src/session-resolve.ts): a FIFTH tag-change rung, exact mirror of the +// tag-invariant "truncated head, tail aligned and fragment-verified" branch +// doctrine — a STRICTLY SHORTER incoming whose tail kinds align AND whose +// last ≤TAIL_WINDOW turn fragments are byte-exact re-anchors the raw id +// instead of forking. P1-1 wires the same event through a new optional +// observer on personaConversationOrder so server-side forks finally leave a +// durable log line (they used to be completely silent). +// +// Scope honesty (documented in the issue): this rung rescues the +// overflow-RETRY shape (verbatim shorter tail). A pre-step compaction that +// EXTENDS the tail before resending still forks — safe direction (no +// substrate loss), tracked as a follow-up generalization to tail-containment. + +import test from "node:test"; +import assert from "node:assert/strict"; +import { createHash } from "node:crypto"; +import { mkdtempSync, rmSync } from "node:fs"; +import { tmpdir } from "node:os"; +import { join } from "node:path"; +import { personaConversationOrder, resetSessionOrderForTest, resolveSessionOrder, type SessionOrderResolution } from "../src/session-resolve.ts"; + +process.env.NODE_ENV = "test"; +process.env.BILI_PERSIST = "0"; +const testRoot = mkdtempSync(join(tmpdir(), "bili-5686-")); +test.after(() => rmSync(testRoot, { recursive: true, force: true })); +for (const key of ["XDG_STATE_HOME", "XDG_DATA_HOME", "XDG_CACHE_HOME", "XDG_CONFIG_HOME"]) process.env[key] = testRoot; + +type M = { role: string; content?: string | null }; +const ID = "sess-5686-anchor"; +const S1 = "You are DeepSeek Harness.\nModel: GLM-A."; +const S2 = "You are DeepSeek Harness NEXT.\nModel: GLM-B.\nBudget day 2."; // per-turn re-stamp shape +const fp16 = (s: string): string => createHash("sha256").update(s, "utf8").digest("hex").slice(0, 16); +const CHECKPOINT = "This is an automatically generated checkpoint condensing an earlier span of the conversation to free up context."; + +/** Sixteen persisted turns' worth (even parity, main-shaped). */ +const TURN_TEXTS = Array.from({ length: 16 }, (_, i) => `turn-${i}-` + "c".repeat(64 + i)); +const seedHistory: M[] = [ + { role: "system", content: S1 }, + ...TURN_TEXTS.map((t, i): M => ({ role: i % 2 === 0 ? "user" : "assistant", content: t })), +]; +/** Verbatim last EIGHT content cells (seed index 9 = 8th turn). */ +const VERBATIM_TAIL = seedHistory.slice(9); + +test("#5686 P1-2: tag-changed STRICTLY-SHORTER verbatim tail re-anchors instead of forking", () => { + resetSessionOrderForTest(); + const claim = resolveSessionOrder(ID, seedHistory, S1); + assert.equal(claim.action, "claim"); + assert.equal(claim.key, ID); + + // Overflow-force-compact + retry: the same in-flight request re-sent + // with the region head replaced by the checkpoint under a re-stamped + // system prompt. Tail = the last eight cells, byte-identical. + const retry: M[] = [ + { role: "system", content: S2 }, + { role: "user", content: CHECKPOINT }, + ...VERBATIM_TAIL, + ]; + const res = resolveSessionOrder(ID, retry, S2); + assert.equal(res.key, ID, "anchor kept on the raw id — no |sub: fork"); + assert.equal(res.action, "reanchor"); +}); + +test("#5686 guardrail: shorter + tail-aligned but NOT fragment-exact still forks (stranger wearing the skeleton)", () => { + resetSessionOrderForTest(); + resolveSessionOrder(ID, seedHistory, S1); + const retry: M[] = [ + { role: "system", content: S2 }, + { role: "user", content: CHECKPOINT }, + ...VERBATIM_TAIL.slice(0, -1), + { role: "assistant", content: "mutated tail cell — different conversation, same skeleton" }, + ]; + const res = resolveSessionOrder(ID, retry, S2); + assert.equal(res.action, "derived-persona"); + assert.equal(res.key, `${ID}|sub:${fp16(S2)}`); +}); + +test("#5686 symmetry regression: tag-INvariant shorter verbatim tail re-anchors (pre-existing branch untouched)", () => { + resetSessionOrderForTest(); + resolveSessionOrder(ID, seedHistory, S1); + const retry: M[] = [ + { role: "system", content: S1 }, + { role: "user", content: CHECKPOINT }, + ...VERBATIM_TAIL, + ]; + const res = resolveSessionOrder(ID, retry, S1); + assert.equal(res.key, ID); + assert.equal(res.action, "reanchor"); +}); + +test("#5686 P1-1: personaConversationOrder delivers every resolution to the observer", () => { + resetSessionOrderForTest(); + const seen: SessionOrderResolution[] = []; + const obs = (_rawId: string, r: SessionOrderResolution): void => { seen.push(r); }; + const k1 = personaConversationOrder(ID, seedHistory, S1, undefined, undefined, undefined, obs); + assert.equal(k1, ID); + assert.deepEqual(seen.map((r) => r.action), ["claim"]); + + // Forking shape: tag changed, shorter, NON-verbatim tail, non-aligned + // kinds run (lucky-skeleton stranger). + const stranger: M[] = [ + { role: "system", content: S2 }, + { role: "user", content: "fresh unrelated question" }, + { role: "assistant", content: "unrelated answer one" }, + { role: "user", content: "unrelated follow-up two" }, + ]; + const k2 = personaConversationOrder(ID, stranger, S2, undefined, undefined, undefined, obs); + assert.equal(k2, `${ID}|sub:${fp16(S2)}`); + assert.deepEqual(seen.map((r) => r.action).slice(-1), ["derived-persona"]); + assert.equal(seen[seen.length - 1].key, `${ID}|sub:${fp16(S2)}`); +}); diff --git a/tests/native-intercept.test.ts b/tests/native-intercept.test.ts index 13dfb5c6eb..7e6c772df1 100644 --- a/tests/native-intercept.test.ts +++ b/tests/native-intercept.test.ts @@ -1821,13 +1821,18 @@ test("#2592/#2709: unattributed raw-URL POST without the marker still sends dire assert.deepEqual(sink, ["https://api.deepseek.com/chat/completions"]); }); -test("#2592/#2709: marker hit but proxy not ready falls open to a direct send", async () => { +test("#2592/#2709/#5686: marker-decisive compaction POST with dead proxy refuses LOCALLY instead of falling open", async () => { + // Pre-#5686 this case fell open to a direct send — exactly the leak + // window that let dsh checkpoints land under bili's feet (#5517 cluster). + // The decisive marker + no usable proxy combination now refuses with a + // protocol-shaped 422; nothing reaches the upstream provider. const state: NativeInterceptState = { origin: undefined, ready: Promise.resolve(undefined), takeoverGate: () => false }; const { sink } = await withPatch(state, async (fetch) => { const res = await fetch("https://api.deepseek.com/chat/completions", { method: "POST", body: dshCompactionEnvelope() }); - assert.equal(res.status, 200); + assert.equal(res.status, 422); + assert.equal(res.headers.get("x-bili-gate"), "dsh-compaction-refused"); }); - assert.deepEqual(sink, ["https://api.deepseek.com/chat/completions"]); + assert.deepEqual(sink, [], "no upstream fetch — local refusal only"); }); test("#2592/#2709: Uint8Array body with the marker diverts; URLSearchParams form-encoding cannot carry the raw marker", async () => { From 79d0d21811c5208ac882627b346aa95dc84a1b53 Mon Sep 17 00:00:00 2001 From: ci-check Date: Thu, 5 Nov 2026 03:02:54 +0800 Subject: [PATCH 2/2] fix: drop unused export on SessionResolveObserver (unused-exports lane red) --- src/session-resolve.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/session-resolve.ts b/src/session-resolve.ts index 1d44dab3a8..189630698a 100644 --- a/src/session-resolve.ts +++ b/src/session-resolve.ts @@ -478,7 +478,7 @@ export function applyOrderSnapshot(entries: OrderSkeletonEntry[]): number { * and continue stay silent (steady-state volume); the caller decides what * to log. The callback is best-effort: a throwing observer must never break * resolution. */ -export type SessionResolveObserver = (rawId: string, r: SessionOrderResolution) => void; +type SessionResolveObserver = (rawId: string, r: SessionOrderResolution) => void; export function personaConversationOrder(rawId: string, history: unknown, personaTag?: string, declaredModel?: string, laneToken?: string, laneDiscriminator?: string, onResolve?: SessionResolveObserver): string { const res = resolveSessionOrder(rawId, history, personaTag ?? "", declaredModel, laneToken, laneDiscriminator);