Skip to content
Closed
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
11 changes: 11 additions & 0 deletions src/agent/dsh-compaction-disable.ts
Original file line number Diff line number Diff line change
Expand Up @@ -132,6 +132,17 @@ export function _resetDshCompactionGateTelemetryForTest(): void {
delete (globalThis as Record<string, unknown>)[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
Expand Down
30 changes: 23 additions & 7 deletions src/agent/dsh-native.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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";
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -1256,6 +1251,12 @@ interface LlmDispatchScope {
const dispatchScope = new AsyncLocalStorage<LlmDispatchScope>();

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
Expand Down Expand Up @@ -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) {
Expand Down Expand Up @@ -3119,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;
Expand Down
46 changes: 45 additions & 1 deletion tests/dsh-llm-stream-attribution.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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";
Expand All @@ -25,6 +25,7 @@ import {
apply,
forkChildVerdict,
_resetRegisterForTest,
_resetCompactionCoverageNoteForTest,
_settleNativeForTest,
_stateBeforeSendForTest,
_nativeStateForTest,
Expand Down Expand Up @@ -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<number> {
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 });
}
});
});
75 changes: 75 additions & 0 deletions tests/issue6770-compaction-coverage-text.test.ts
Original file line number Diff line number Diff line change
@@ -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();
}
});
Loading