From f828bb07f32e79edd85ba1e2c3e128d2cce3785c Mon Sep 17 00:00:00 2001 From: OMSociety <85862362+OMSociety@users.noreply.github.com> Date: Sun, 15 Nov 2026 04:20:51 +0800 Subject: [PATCH 1/2] feat(dsh): surface compaction overlay coverage on the first routed request (#6770) --- src/agent/dsh-compaction-disable.ts | 11 +++ src/agent/dsh-native.ts | 24 ++++-- ...issue6770-compaction-coverage-text.test.ts | 75 +++++++++++++++++++ 3 files changed, 103 insertions(+), 7 deletions(-) create mode 100644 tests/issue6770-compaction-coverage-text.test.ts diff --git a/src/agent/dsh-compaction-disable.ts b/src/agent/dsh-compaction-disable.ts index 4970e3d159..9836bf598e 100644 --- a/src/agent/dsh-compaction-disable.ts +++ b/src/agent/dsh-compaction-disable.ts @@ -132,6 +132,17 @@ export function _resetDshCompactionGateTelemetryForTest(): void { delete (globalThis as Record)[DSH_COMPACTION_GATE_TELEMETRY_KEY]; } +/** #6770: one-line coverage summary shared by the durable profile notes (#3962) + * and bili-native's first-request log line. Pure over the record; "no entry + * rewritten" is the honest frame because preset configs resolve lazily. */ +export function compactionCoverageText(telemetry: DshCompactionGateTelemetry | null = dshCompactionGateTelemetry()): string { + if (telemetry === null || telemetry.covered.length === 0) { + return "no preset entry rewritten in this process (preset configs resolve lazily — zero at this point does not mean zero later)"; + } + const entries = telemetry.covered.map((c) => `${c.entry}\u00d7${c.rows}`).join(", "); + return `${telemetry.covered.length} preset ${telemetry.covered.length === 1 ? "entry" : "entries"} rewritten (${entries})`; +} + type PresetRow = { name?: unknown; group?: unknown; config?: unknown }; /** Count compaction-basic/command-compact rows the disable walk would mark diff --git a/src/agent/dsh-native.ts b/src/agent/dsh-native.ts index 5d83069fb5..0e8f6fe551 100644 --- a/src/agent/dsh-native.ts +++ b/src/agent/dsh-native.ts @@ -70,7 +70,7 @@ import { installNativeWebSocketIntercept } from "./native-ws-intercept.js"; import { asciiHeaderValue, fetchManifest, fetchProxyVersion, fetchStatus, fetchStatusLatest, forwardTool, reportAttributionHealth, reportRuntimeInfo, waitForProxyVersion, type ManifestTool } from "./shared.js"; import { createForkAdopter, sideShapedBody } from "./fork-adopt.js"; import { clearIncrementalProbe, createPrepareDirectSend, INCREMENTAL_TURN_MARKER_HEADER, INCREMENTAL_TURN_ID_HEADER, newIncrementalTurnId, noteIncrementalRejection, noteIncrementalAck, planIncrementalTurn, probeIncrementalTurns, rememberOriginalBody, sweepStale, wireMessagesLookAnthropic, type IncrementalWireMessage } from "./incremental-client.js"; -import { declareDshCompactionActive, detectCompactionOverlayGap, dshCompactionGateTelemetry } from "./dsh-compaction-disable.js"; +import { compactionCoverageText, declareDshCompactionActive, detectCompactionOverlayGap } from "./dsh-compaction-disable.js"; import { maskUrlForDiagnostics } from "../log-mask.js"; import { mediaBytesOfWireBody } from "./media-bytes.js"; import { conversationRecallStats, learnConversation, recallBoundaryOfMessages, recallConversation, recallHistoryFingerprints, recallLaneHasEntries, recallLaneKey } from "./conversation-recall.js"; @@ -294,12 +294,7 @@ export { DSH_BUILD_CHECK } from "./client-build-note.js"; * the gate rows' fate implied. Best-effort by necessity: preset configs * resolve lazily, so "so far" is the honest frame. */ function gateCoverageSuffix(): string { - const t = dshCompactionGateTelemetry(); - if (t === null || t.covered.length === 0) { - return " Overlay coverage so far: no preset entry rewritten in this process (preset configs resolve lazily — zero at this point does not mean zero later)."; - } - const entries = t.covered.map((c) => `${c.entry}\u00d7${c.rows}`).join(", "); - return ` Overlay coverage so far: ${t.covered.length} preset ${t.covered.length === 1 ? "entry" : "entries"} rewritten (${entries}).`; + return ` Overlay coverage so far: ${compactionCoverageText()}.`; } /** #1590/#1809: origin of the proxy as currently reachable — register.base @@ -1256,6 +1251,12 @@ interface LlmDispatchScope { const dispatchScope = new AsyncLocalStorage(); let noticedScopedAttribution = false; +// #6770: the #3962 coverage readout only ever rode the profile-bundle note, +// which stays silent wherever profileContext.startedBundles cannot be read +// (observed on both a desktop dsh build and the dsh-go runtime), so the +// coverage reading had no reachable exit. Emit it once per process on the +// first routed request instead. +let compactionCoverageNoted = false; /** #4491: extract the host message ids from llm/stream `options.messages`, * all-or-nothing. Mirrors validateMsgIds in src/msgid-pin.ts (the 400-class @@ -1340,6 +1341,15 @@ function attributionOf(ctx: PluginContext): GateAttribution { // driving), and it survives exactly the shapes the ambient reading does // not (agentless compaction/title lanes, disposed scopes, retried // re-sends whose initiator scope went away between attempts). + // #6770: first routed request is the earliest point where the overlay's + // structural rewrites can have happened (preset configs resolve lazily), + // so the coverage reading goes out here — the durable profile note that + // used to carry it never fires when profileContext.startedBundles cannot + // be read, which is the observed state on every host we checked. + if (!compactionCoverageNoted) { + compactionCoverageNoted = true; + persistClientEvent(`bili-native-dsh: compaction overlay coverage — ${compactionCoverageText()} (#6770)`); + } const scoped = dispatchScope.getStore(); if (scoped !== undefined && scoped.sid.length > 0) { if (!noticedScopedAttribution) { diff --git a/tests/issue6770-compaction-coverage-text.test.ts b/tests/issue6770-compaction-coverage-text.test.ts new file mode 100644 index 0000000000..d6f9406c15 --- /dev/null +++ b/tests/issue6770-compaction-coverage-text.test.ts @@ -0,0 +1,75 @@ +import test from "node:test"; +import assert from "node:assert/strict"; +import { + _resetDshCompactionGateTelemetryForTest, + _resetDshCompactionVerdictForTest, + compactionCoverageText, + declareDshCompactionActive, + dshCompactionGateTelemetry, + registerCompactionDisable, +} from "../src/agent/dsh-compaction-disable.ts"; + +// #6770: the #3962 coverage reading needs an exit that does not depend on the +// durable profile-bundle note — that note stays silent wherever +// profileContext.startedBundles cannot be read (the observed state on both a +// desktop dsh build and the dsh-go runtime), so the reading had no reachable +// exit at all. compactionCoverageText() is the one formatter both exits share. + +type Mw = (raw: unknown, next: () => unknown) => unknown; + +function middleware(): Mw { + let mw: Mw | undefined; + registerCompactionDisable({ + on: (_event, listener) => { mw = listener as Mw; }, + reflect: {}, + }); + assert.equal(typeof mw, "function"); + return mw as Mw; +} + +test("#6770: no record yet — the reading states the lazy-resolution caveat instead of reporting zero", () => { + _resetDshCompactionGateTelemetryForTest(); + assert.equal(dshCompactionGateTelemetry(), null); + const text = compactionCoverageText(); + assert.match(text, /^no preset entry rewritten in this process/); + assert.match(text, /resolve lazily/); +}); + +test("#6770: singular and plural follow the covered-entry count", () => { + assert.equal( + compactionCoverageText({ covered: [{ entry: "preset-standard", rows: 2 }], sawFactoryPreset: true }), + "1 preset entry rewritten (preset-standard\u00d72)", + ); + assert.equal( + compactionCoverageText({ + covered: [{ entry: "preset-standard", rows: 2 }, { entry: "preset-go-companion", rows: 1 }], + sawFactoryPreset: true, + }), + "2 preset entries rewritten (preset-standard\u00d72, preset-go-companion\u00d71)", + ); +}); + +test("#6770: an empty covered list uses the empty frame, not a '0 entries rewritten' line", () => { + assert.match(compactionCoverageText({ covered: [], sawFactoryPreset: false }), /^no preset entry rewritten/); +}); + +test("#6770: the argument-less default reads the live record the overlay writes", () => { + try { + _resetDshCompactionVerdictForTest(); + _resetDshCompactionGateTelemetryForTest(); + assert.match(compactionCoverageText(), /^no preset entry rewritten/, "unlatched overlay reads empty"); + declareDshCompactionActive(); + const mw = middleware(); + const cfg = { + plugins: [ + { group: true, config: [{ name: "@deepseek-ai/dsh-compaction-basic" }] }, + { name: "@deepseek-ai/dsh-command-compact" }, + ], + }; + mw.call({ entry: { options: { name: "preset-dsh-go-companion" } } }, null, () => cfg); + assert.equal(compactionCoverageText(), "1 preset entry rewritten (preset-dsh-go-companion\u00d72)"); + } finally { + _resetDshCompactionVerdictForTest(); + _resetDshCompactionGateTelemetryForTest(); + } +}); From 1d304fcda0cc92a767504a6ce3dd5d5ef818e53d Mon Sep 17 00:00:00 2001 From: OMSociety <85862362+OMSociety@users.noreply.github.com> Date: Sun, 15 Nov 2026 16:25:11 +0800 Subject: [PATCH 2/2] test(dsh): pin the #6770 coverage readout wiring (#6770) --- src/agent/dsh-native.ts | 6 ++++ tests/dsh-llm-stream-attribution.test.ts | 46 +++++++++++++++++++++++- 2 files changed, 51 insertions(+), 1 deletion(-) diff --git a/src/agent/dsh-native.ts b/src/agent/dsh-native.ts index 0e8f6fe551..8654a7b2b7 100644 --- a/src/agent/dsh-native.ts +++ b/src/agent/dsh-native.ts @@ -3129,6 +3129,12 @@ export function _resetDshNewerBuildNoteForTest(): void { dshNewerBuildNoted = false; } +/** Test hook (#6770): reset the once-per-process compaction-coverage readout + * flag so a test can pin the emit wiring, not just the formatter. */ +export function _resetCompactionCoverageNoteForTest(): void { + compactionCoverageNoted = false; +} + /** #2924 direction 2: a `threw` attribution (host agent scope disposed * mid-request) gets a bounded settle-retry before the gate decides. The * defaults cap the added latency of a dying turn at ~(attempts-1)×delayMs; diff --git a/tests/dsh-llm-stream-attribution.test.ts b/tests/dsh-llm-stream-attribution.test.ts index 5bb5b7d0bd..c5840939e4 100644 --- a/tests/dsh-llm-stream-attribution.test.ts +++ b/tests/dsh-llm-stream-attribution.test.ts @@ -10,7 +10,7 @@ * loses: retry chains, agentless side-calls, disposed initiator scopes. */ import { strict as assert } from "node:assert"; -import { mkdtempSync, rmSync } from "node:fs"; +import { existsSync, mkdtempSync, readFileSync, rmSync } from "node:fs"; import { tmpdir } from "node:os"; import { join } from "node:path"; import test, { after, before } from "node:test"; @@ -25,6 +25,7 @@ import { apply, forkChildVerdict, _resetRegisterForTest, + _resetCompactionCoverageNoteForTest, _settleNativeForTest, _stateBeforeSendForTest, _nativeStateForTest, @@ -451,3 +452,46 @@ test("#6880: the dispatch purpose is captured into the scope and stamped per fet } }); }); + +// #6770: the durable profile note that used to carry the coverage reading stays +// silent wherever profileContext.startedBundles cannot be read (the observed +// state on every host we checked), so the reading rides the first routed +// request instead. This pins the wiring itself — that it fires, and that it +// fires exactly once per process. +test("#6770: compaction overlay coverage is logged once on the first routed request", { timeout: 15_000 }, async () => { + await withSetup(async (handle) => { + const previousState = process.env.XDG_STATE_HOME; + const state = mkdtempSync(join(tmpdir(), "dsh-coverage-note-")); + process.env.XDG_STATE_HOME = state; + const logFile = join(state, "billion-context", "bili.log"); + const coverageLines = (): number => + existsSync(logFile) ? readFileSync(logFile, "utf8").split("compaction overlay coverage").length - 1 : 0; + const gate = _stateTakeoverGateForTest(); + assert.ok(gate, "takeover gate is installed after apply()"); + const base = async function* (): AsyncIterable { + await gate(MODEL_URL, { bytes: 200 }, { headers: {} }); + yield 1; + }; + try { + _resetCompactionCoverageNoteForTest(); + handle.setInitiator("sess-coverage"); + for await (const _chunk of runWaterfall(handle.events, { sessionId: "sess-coverage" }, base)) { + /* drain */ + } + assert.equal(coverageLines(), 1, "the first routed request carries the coverage readout"); + assert.match( + readFileSync(logFile, "utf8"), + /no preset entry rewritten in this process/, + "an untouched overlay reports the lazy-resolution frame, not a bare zero", + ); + for await (const _chunk of runWaterfall(handle.events, { sessionId: "sess-coverage" }, base)) { + /* drain */ + } + assert.equal(coverageLines(), 1, "a second routed request must not repeat the reading (once per process)"); + } finally { + if (previousState === undefined) delete process.env.XDG_STATE_HOME; + else process.env.XDG_STATE_HOME = previousState; + rmSync(state, { recursive: true, force: true }); + } + }); +});