From ed301d91ba0ba69284edf1081517f8d5559a7f9e Mon Sep 17 00:00:00 2001 From: ci-check Date: Thu, 29 Oct 2026 21:17:23 +0800 Subject: [PATCH 1/7] fix: serve GET /__bili/upstreams with ACAO echo so the DSH banner gets its data (#5146) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit The DSH frame-level upstream banner (shipped via 6874e39fe from #4254) polls /__bili/upstreams every 10s, but its server half never landed: PR #4257 closed unmerged and the re-land (050241cae) kept only the egress fixes. Every poll therefore 404'd AND was CORS-blocked in the DSH renderer (admin JSON routes send no Access-Control-Allow-Origin), so the banner never showed and each session logged one console error per 10s. - Add loopback-gated GET /__bili/upstreams to handleAdminRoute projecting the alert table + open circuits into the #4254-settled contract {upstreams:[{host,port,kind,code,hint,count,firstSeen,lastSeen,circuit}]}; one row per host, the OPEN circuit outranks alerts (fail-fast stops minting alerts once open, so it is the authoritative down signal), alerts cover the pre-circuit window and carry the hint. - Echo the requesting Origin as ACAO on this route only — same gate-first pattern as the /__bili/ shell and /__bili/plugin/status (#3862); only origins the trusted-origin gate admitted reach dispatch, so the echo cannot widen the admin surface. No Origin (curl/probes) → no ACAO, unchanged. - Capture the errno at failure time into UpstreamAlert/UpstreamCircuitFailure (optional code field) so the report shows WHY beyond the classified kind — the client banner renders it when present. - Tests: projection semantics (alert-only window / circuit precedence / recovery drops the host / portless host) plus the HTTP surface (ACAO echo, healthy empty list without ACAO, non-loopback 403, tunnel 403). --- src/server/admin.ts | 16 +- src/upstream-alerts.ts | 104 ++++++++++++- tests/issue5146-upstreams-endpoint.test.ts | 169 +++++++++++++++++++++ 3 files changed, 285 insertions(+), 4 deletions(-) create mode 100644 tests/issue5146-upstreams-endpoint.test.ts diff --git a/src/server/admin.ts b/src/server/admin.ts index d6f765d2ae..e30eff987e 100644 --- a/src/server/admin.ts +++ b/src/server/admin.ts @@ -25,7 +25,7 @@ import { dismissHostPlugin, visibleHostPluginNotices } from "../host-plugin-noti import { listSessions, memoryReport, peekSession, splitSessionWarnings, totalInFlight } from "../session.js"; import { BILI_TUNNEL_HEADER } from "../tunnel-guard.js"; import { isLoopbackAddress } from "../util.js"; -import { clearUpstreamAlertsForHost, getUpstreamAlerts } from "../upstream-alerts.js"; +import { clearUpstreamAlertsForHost, getUpstreamAlerts, getUpstreamHealthReport } from "../upstream-alerts.js"; import { pckAblations } from "../pck-probe.js"; import { formatUpstreamError, getUpstreamConnectionStatus, proxyDispatcher, recordUpstreamConnection, resetProxyCache, resolveProxyDecision } from "../upstream-proxy.js"; import { detectStaleInstall, getLastInstall } from "../update.js"; @@ -234,6 +234,20 @@ export async function handleAdminRoute(req: http.IncomingMessage, res: http.Serv if (req.method === "GET" && req.url === "/__bili/status") return sendStatus(res, opts, typeof req.headers.host === "string" ? req.headers.host : undefined); if (req.method === "GET" && req.url === "/__bili/resign") return sendResignStatus(res); if (req.method === "GET" && req.url === "/__bili/overview") return sendOverview(res, opts, typeof req.headers.host === "string" ? req.headers.host : undefined); + // #5146: per-host upstream health (alerts + open circuits) for DSH's frame-level + // banner — loopback-gated like every other /__bili/* route above. The DSH + // renderer polls this CROSS-ORIGIN from its own web port, so echo the + // requesting origin as ACAO exactly like the /__bili/ shell and + // /__bili/plugin/status do: only origins the trusted-origin gate admitted + // reach this line, and the payload is a bounded in-memory snapshot. + if (req.method === "GET" && req.url === "/__bili/upstreams") { + const reqOrigin = req.headers.origin; + const headers: Record = { "content-type": "application/json" }; + if (typeof reqOrigin === "string" && reqOrigin.length > 0) headers["access-control-allow-origin"] = reqOrigin; + res.writeHead(200, headers); + res.end(JSON.stringify(getUpstreamHealthReport())); + return; + } if (req.method === "GET" && (req.url === "/__bili/sessions" || req.url?.startsWith("/__bili/sessions?"))) return sendWebSessions(res, req); if (req.method === "GET" && req.url?.startsWith("/__bili/logs")) return sendWebLogs(res, req); if (req.method === "GET" && req.url?.startsWith("/__bili/diagnostics")) return sendDiagnostics(res, req.url); diff --git a/src/upstream-alerts.ts b/src/upstream-alerts.ts index 2ff641095c..94fc6e5768 100644 --- a/src/upstream-alerts.ts +++ b/src/upstream-alerts.ts @@ -30,6 +30,9 @@ export interface UpstreamAlert { * the full English string for JSON consumers). Redacted. */ skippedProxy?: string; skippedSource?: string; + /** #5146: errno captured at failure time (e.g. ECONNREFUSED) — surfaces in the + * /__bili/upstreams report so the DSH banner can show WHY. */ + code?: string; } /** v1 scope (#1682): only "fundamentally cannot connect" transport failures. @@ -62,6 +65,13 @@ function keyFor(kind: UpstreamFailureKind, host: string): string { return kind + "\u0000" + host; } +/** #5146: pull the errno off a Node error when present (ECONNREFUSED, ENOTFOUND, …) so the + * /__bili/upstreams report can show WHY beyond the classified kind. undefined for non-errors. */ +function errnoCode(error: unknown): string | undefined { + const c = typeof error === "object" && error !== null ? (error as { code?: unknown }).code : undefined; + return typeof c === "string" && c.length > 0 ? c : undefined; +} + /** Record one failed model request. Dedup key (kind, host): repeats refresh * lastSeen/count only. Non-alert kinds are ignored entirely. `skipped` (#2809) * rides along so the banner can name the proxy default-direct mode skipped. */ @@ -70,6 +80,7 @@ export function recordUpstreamAlert(url: string, error: unknown, viaProxy: boole if (!ALERT_KINDS.has(kind)) return; const host = hostOf(url); const now = Date.now(); + const code = errnoCode(error); const key = keyFor(kind, host); const skippedFields = skipped ? { skippedProxy: redactedSkippedProxy(skipped), skippedSource: skipped.source } : {}; const hint = hintWithSkippedProxy(kind, skipped); @@ -78,6 +89,7 @@ export function recordUpstreamAlert(url: string, error: unknown, viaProxy: boole existing.lastSeen = now; existing.count += 1; existing.hint = hint; + if (code !== undefined) existing.code = code; Object.assign(existing, skippedFields); return; } @@ -92,7 +104,7 @@ export function recordUpstreamAlert(url: string, error: unknown, viaProxy: boole } if (oldestKey !== undefined) alerts.delete(oldestKey); } - alerts.set(key, { kind, host, hint, firstSeen: now, lastSeen: now, count: 1, ...skippedFields }); + alerts.set(key, { kind, host, hint, firstSeen: now, lastSeen: now, count: 1, ...(code !== undefined ? { code } : {}), ...skippedFields }); } /** Any resolved response to this host proves the path works again — clear ALL @@ -133,6 +145,8 @@ interface UpstreamCircuitFailure { kind: string; detail: string; at: number; + /** #5146: errno at failure time for the /__bili/upstreams report. */ + code?: string; } interface UpstreamCircuit { @@ -248,6 +262,7 @@ export function recordUpstreamCircuitFailure(url: string, error: unknown, viaPro const kind = classifyUpstreamFailure(error, { viaProxy }); const host = hostOf(url); const now = Date.now(); + const code = errnoCode(error); let entry = circuits.get(host); if (!entry) { if (!ALERT_KINDS.has(kind)) return; @@ -271,14 +286,14 @@ export function recordUpstreamCircuitFailure(url: string, error: unknown, viaPro if (c.openedAt !== null) { // Open: the half-open probe just finished without a response — release // the slot and re-arm the cooldown regardless of kind (see above). - c.lastFailure = { kind, detail: shortDetail, at: now }; + c.lastFailure = { kind, detail: shortDetail, at: now, ...(code !== undefined ? { code } : {}) }; entry.probeInFlight = false; c.nextProbeAt = now + circuitProbeCooldownMs; loggerLog("warn", `[acp-proxy] upstream circuit probe FAILED for ${host} (${kind}) — re-armed until ${new Date(c.nextProbeAt).toISOString()} (#3148)`); return; } if (!ALERT_KINDS.has(kind)) return; - c.lastFailure = { kind, detail: shortDetail, at: now }; + c.lastFailure = { kind, detail: shortDetail, at: now, ...(code !== undefined ? { code } : {}) }; c.consecutiveFailures += 1; if (c.consecutiveFailures >= CIRCUIT_OPEN_AFTER) { c.openedAt = now; @@ -306,6 +321,89 @@ export function getOpenUpstreamCircuits(): UpstreamCircuit[] { .sort((a, b) => (b.openedAt ?? 0) - (a.openedAt ?? 0)); } +// #5146: the read contract behind GET /__bili/upstreams — the loopback-gated +// surface DSH's frame-level banner polls (contract settled in #4254; that PR's +// landing of this half was dropped when it closed unmerged, which is exactly +// why the shipped banner polled a route that never existed). bili is a +// transparent pipe, so this reports per HOST (the only identity it knows), +// never per conversation; DSH filters that down to the hosts its own +// conversations point at. Merges the two existing tables into one stable +// shape: the open circuit is the authoritative "currently down" signal +// (fail-fast stops minting new alerts once open), while the alert rows carry +// the actionable hint and cover the pre-circuit window. + +export interface UpstreamHealthEntry { + /** Host[:port] the failing traffic targeted — raw (endpoint is loopback-gated). */ + host: string; + port: number | null; + /** Primary failure kind: the circuit's last failure when open, else the most + * recently seen alert for the host. */ + kind: string; + /** errno captured at failure time (ECONNREFUSED, …) when available. */ + code?: string; + /** Actionable hint (most recent alert of the primary kind, else circuit detail). */ + hint?: string; + /** Observed failure count — circuit strikes when open, else summed alert counts. */ + count: number; + firstSeen: number; + lastSeen: number; + /** Present only while a circuit is OPEN; null in the alert-only window. */ + circuit: { state: "open"; nextProbeInMs: number } | null; +} + +export interface UpstreamHealthReport { + upstreams: UpstreamHealthEntry[]; +} + +export function getUpstreamHealthReport(now: number = Date.now()): UpstreamHealthReport { + interface Acc { + alerts: UpstreamAlert[]; + circuit: UpstreamCircuit | null; + } + const byHost = new Map(); + const touch = (host: string): Acc => { + let acc = byHost.get(host); + if (!acc) { + acc = { alerts: [], circuit: null }; + byHost.set(host, acc); + } + return acc; + }; + for (const a of alerts.values()) touch(a.host).alerts.push(a); + for (const e of circuits.values()) { + if (e.state.openedAt !== null) touch(e.state.host).circuit = e.state; + } + const upstreams: UpstreamHealthEntry[] = []; + for (const [host, { alerts: hostAlerts, circuit }] of byHost.entries()) { + const latest = hostAlerts.length > 0 ? hostAlerts.reduce((m, a) => (a.lastSeen > m.lastSeen ? a : m)) : undefined; + const kind = circuit?.lastFailure?.kind ?? latest?.kind ?? "unknown"; + const code = circuit?.lastFailure?.code ?? latest?.code; + const hint = latest?.hint ?? circuit?.lastFailure?.detail; + const count = circuit ? circuit.consecutiveFailures : hostAlerts.reduce((s, a) => s + a.count, 0); + const beginTimes = [...hostAlerts.map((a) => a.firstSeen), ...(circuit ? [circuit.openedAt ?? now] : [])]; + const endTimes = [...hostAlerts.map((a) => a.lastSeen), ...(circuit ? [circuit.lastFailure?.at ?? now] : [])]; + let port: number | null = null; + const colon = host.lastIndexOf(":"); + if (colon >= 0) { + const p = Number(host.slice(colon + 1)); + if (Number.isInteger(p) && p > 0 && p <= 65535) port = p; + } + upstreams.push({ + host, + port, + kind, + ...(code !== undefined ? { code } : {}), + ...(hint !== undefined ? { hint } : {}), + count, + firstSeen: Math.min(...beginTimes), + lastSeen: Math.max(...endTimes), + circuit: circuit ? { state: "open", nextProbeInMs: Math.max(0, (circuit.nextProbeAt ?? now) - now) } : null, + }); + } + upstreams.sort((a, b) => b.lastSeen - a.lastSeen); + return { upstreams }; +} + export function _resetUpstreamCircuitsForTest(): void { circuits = new Map(); circuitProbeCooldownMs = 60_000; diff --git a/tests/issue5146-upstreams-endpoint.test.ts b/tests/issue5146-upstreams-endpoint.test.ts new file mode 100644 index 0000000000..61c817caf9 --- /dev/null +++ b/tests/issue5146-upstreams-endpoint.test.ts @@ -0,0 +1,169 @@ +// #5146: GET /__bili/upstreams read surface — per-host merge of the alert table +// + open circuits that DSH's frame-level banner polls. The client half of #4254 +// landed without this server half (its PR closed unmerged and the re-land kept +// only the egress fixes), so every shipped banner polled a route that 404'd AND +// got CORS-blocked per poll. Unit-tests the projection directly AND pins the +// HTTP surface: loopback/tunnel gating, ACAO echo for the cross-origin DSH +// reader, healthy empty list. +import { test } from "node:test"; +import assert from "node:assert/strict"; +import http from "node:http"; +import { Readable } from "node:stream"; +import { handleAdminRoute } from "../src/server/admin.ts"; +import { BILI_TUNNEL_HEADER } from "../src/tunnel-guard.ts"; +import { + recordUpstreamAlert, + recordUpstreamCircuitFailure, + recordUpstreamCircuitSuccess, + clearUpstreamAlertsForHost, + getUpstreamHealthReport, + _resetUpstreamAlertsForTest, + _resetUpstreamCircuitsForTest, + _setCircuitCooldownForTest, +} from "../src/upstream-alerts.js"; + +const A = "http://127.0.0.1:7864/v1/chat/completions"; +const B = "http://10.0.0.5:9000/v1/chat/completions"; +const NO_PORT = "http://relay.example.com/v1/chat/completions"; + +function refused(host: string): Error { + return Object.assign(new Error(`connect ECONNREFUSED ${host}`), { code: "ECONNREFUSED" }); +} + +test("#5146: alert-only window → one entry per host, circuit null, kind/count/hint/code from the alert", () => { + _resetUpstreamAlertsForTest(); + _resetUpstreamCircuitsForTest(); + recordUpstreamAlert(A, refused("127.0.0.1:7864"), false); + const r = getUpstreamHealthReport(); + assert.equal(r.upstreams.length, 1); + const e = r.upstreams[0]; + assert.equal(e.host, "127.0.0.1:7864"); + assert.equal(e.port, 7864); + assert.equal(e.kind, "connect-refused"); + assert.equal(e.code, "ECONNREFUSED"); + assert.ok(typeof e.hint === "string" && e.hint.length > 0); + assert.equal(e.count, 1); + assert.equal(e.circuit, null); +}); + +test("#5146: open circuit is the authoritative down signal — merged row, state open, nextProbe ETA, circuit strikes", () => { + _resetUpstreamAlertsForTest(); + _resetUpstreamCircuitsForTest(); + _setCircuitCooldownForTest(60_000); + for (let i = 0; i < 3; i++) { + recordUpstreamAlert(B, refused("10.0.0.5:9000"), false); + recordUpstreamCircuitFailure(B, refused("10.0.0.5:9000"), false); + } + const e = getUpstreamHealthReport(Date.now()).upstreams[0]; + assert.equal(e.host, "10.0.0.5:9000"); + assert.equal(e.port, 9000); + assert.ok(e.circuit, "circuit must be present while open"); + assert.equal(e.circuit?.state, "open"); + assert.ok(e.circuit && e.circuit.nextProbeInMs > 0 && e.circuit.nextProbeInMs <= 60_000); + assert.equal(e.kind, "connect-refused"); + assert.equal(e.code, "ECONNREFUSED"); + assert.equal(e.count, 3, "count reflects circuit strikes while open"); +}); + +test("#5146: multiple hosts merge into distinct rows; recovery (clear alerts + close circuit) drops the host", () => { + _resetUpstreamAlertsForTest(); + _resetUpstreamCircuitsForTest(); + recordUpstreamAlert(A, refused("127.0.0.1:7864"), false); + recordUpstreamAlert(B, refused("10.0.0.5:9000"), false); + const hosts = new Set(getUpstreamHealthReport().upstreams.map((e) => e.host)); + assert.deepEqual([...hosts].sort(), ["10.0.0.5:9000", "127.0.0.1:7864"]); + // Recovery on B: a resolved response clears its alerts AND closes any circuit. + clearUpstreamAlertsForHost(B); + recordUpstreamCircuitSuccess(B); + const after = getUpstreamHealthReport().upstreams.map((e) => e.host); + assert.deepEqual(after, ["127.0.0.1:7864"], "recovered host disappears, the other stays"); +}); + +test("#5146: host with no explicit port projects port=null", () => { + _resetUpstreamAlertsForTest(); + _resetUpstreamCircuitsForTest(); + recordUpstreamAlert(NO_PORT, refused("relay.example.com"), false); + const e = getUpstreamHealthReport().upstreams[0]; + assert.equal(e.host, "relay.example.com"); + assert.equal(e.port, null); +}); + +// ── HTTP surface ───────────────────────────────────────────────────────────── + +type AdminCtxForTest = Parameters[2]; + +function makeCtx(): AdminCtxForTest { + return { + opts: { host: "127.0.0.1" }, + core: {}, + config: {}, + log: (): void => {}, + instanceId: "test-instance", + instanceStartedAt: Date.now(), + proxyWatchers: new Set(), + initialWatcherPid: null, + bootReady: (): boolean => true, + requestShutdown: (): void => {}, + } as unknown as AdminCtxForTest; +} + +function mkReq(remoteAddress: string, extraHeaders: Record = {}): http.IncomingMessage { + // the trusted-origin gate verifies the Host header against the admin's + // trusted hostnames, so bare loopback requests must carry it + return Object.assign(new Readable({ read(): void {} }), { + method: "GET", + url: "/__bili/upstreams", + headers: { host: "127.0.0.1", ...extraHeaders }, + socket: { remoteAddress }, + }) as unknown as http.IncomingMessage; +} + +function mkRes(): { res: http.ServerResponse; out: { code?: number; headers?: Record; body?: string } } { + const out: { code?: number; headers?: Record; body?: string } = {}; + const res = { + writeHead(code: number, headers?: Record): void { out.code = code; out.headers = headers ?? {}; }, + end(body?: string): void { out.body = body ?? ""; }, + }; + return { res: res as unknown as http.ServerResponse, out }; +} + +test("#5146: GET /__bili/upstreams answers the cross-origin DSH reader with ACAO echo + report", async () => { + _resetUpstreamAlertsForTest(); + _resetUpstreamCircuitsForTest(); + recordUpstreamAlert(A, refused("127.0.0.1:7864"), false); + const { res, out } = mkRes(); + await handleAdminRoute(mkReq("127.0.0.1", { origin: "http://127.0.0.1:43120" }), res, makeCtx()); + assert.equal(out.code, 200); + assert.equal(out.headers?.["access-control-allow-origin"], "http://127.0.0.1:43120", "ACAO must echo the admitted origin"); + const doc = JSON.parse(out.body ?? "") as { upstreams: Array<{ host: string }> }; + assert.deepEqual(doc.upstreams.map((e) => e.host), ["127.0.0.1:7864"]); +}); + +test("#5146: healthy state → empty list, no ACAO without an Origin header (curl/health probes unchanged)", async () => { + _resetUpstreamAlertsForTest(); + _resetUpstreamCircuitsForTest(); + const { res, out } = mkRes(); + await handleAdminRoute(mkReq("127.0.0.1"), res, makeCtx()); + assert.equal(out.code, 200); + assert.equal(out.headers?.["access-control-allow-origin"], undefined, "no Origin → no ACAO"); + assert.deepEqual(JSON.parse(out.body ?? ""), { upstreams: [] }); +}); + +test("#5146: non-loopback peers are refused before any payload is served", async () => { + _resetUpstreamAlertsForTest(); + _resetUpstreamCircuitsForTest(); + recordUpstreamAlert(A, refused("127.0.0.1:7864"), false); + const { res, out } = mkRes(); + await handleAdminRoute(mkReq("10.0.0.5", { origin: "http://127.0.0.1:43120" }), res, makeCtx()); + assert.equal(out.code, 403); + assert.ok(!out.body?.includes("upstreams"), "no health payload on denial"); +}); + +test("#5146: tunneled requests are refused even from loopback", async () => { + _resetUpstreamAlertsForTest(); + _resetUpstreamCircuitsForTest(); + recordUpstreamAlert(A, refused("127.0.0.1:7864"), false); + const { res, out } = mkRes(); + await handleAdminRoute(mkReq("127.0.0.1", { [BILI_TUNNEL_HEADER]: "1" }), res, makeCtx()); + assert.equal(out.code, 403); +}); From 6829827d2af59d5113a3b88e2fae8c0a89f64853 Mon Sep 17 00:00:00 2001 From: ci-check Date: Thu, 29 Oct 2026 21:17:23 +0800 Subject: [PATCH 2/7] fix: diagnostics host-client-version line notes when a profile bundle has no @deepseek-ai packages (#5146) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Attached sub-problem of #5146: on DSH Desktop bundle installs the profile's node_modules holds third-party plugins (billion-context) alone — the @deepseek-ai core ships inside the app — so all three lookups missed and reports read 'dsh ?, dsh-compaction-basic ?, dsh-web-app ?', losing the host-version dimension with no hint where to look. When NONE of the three resolves for a profile, the line now says so explicitly (and asks reporters to include the app version); partial absence keeps the per-package '?'. --- src/web/diagnostics.ts | 14 ++++++++++++-- tests/web-diagnostics-enrichment.test.ts | 8 ++++++++ 2 files changed, 20 insertions(+), 2 deletions(-) diff --git a/src/web/diagnostics.ts b/src/web/diagnostics.ts index 3fa0fb13d3..cda36e76f9 100644 --- a/src/web/diagnostics.ts +++ b/src/web/diagnostics.ts @@ -210,8 +210,18 @@ function hostClientVersionLines(): string[] { return lines; } for (const dir of dirs.slice(0, DSH_PROFILE_CAP)) { - const vers = DSH_REPORT_PACKAGES.map((p) => `${p.split("/")[1]} ${dshPackageVersion(dir, p) ?? "?"}`).join(", "); - lines.push(`dsh profile ${path.basename(dir)}: ${vers}`); + const versions = DSH_REPORT_PACKAGES.map((p) => dshPackageVersion(dir, p)); + if (versions.every((v) => v === undefined)) { + // #5146: three bare "?"s told triage nothing about WHERE the version + // truth lives — on DSH Desktop bundle installs the @deepseek-ai core + // ships inside the app, not under the profile's node_modules (which + // holds third-party plugins like billion-context alone), so every + // lookup missed and reports lost the host-version dimension entirely. + lines.push(`dsh profile ${path.basename(dir)}: (no @deepseek-ai packages under this profile bundle — on desktop installs the core ships inside the app; include the app version)`); + } else { + const vers = DSH_REPORT_PACKAGES.map((p, i) => `${p.split("/")[1]} ${versions[i] ?? "?"}`).join(", "); + lines.push(`dsh profile ${path.basename(dir)}: ${vers}`); + } } if (dirs.length > DSH_PROFILE_CAP) lines.push(`… ${dirs.length - DSH_PROFILE_CAP} more profile(s)`); return lines; diff --git a/tests/web-diagnostics-enrichment.test.ts b/tests/web-diagnostics-enrichment.test.ts index c98f1cd528..8a6090bf35 100644 --- a/tests/web-diagnostics-enrichment.test.ts +++ b/tests/web-diagnostics-enrichment.test.ts @@ -43,6 +43,9 @@ function makeFixture(): { root: string; dshHome: string; stateHome: string; conf const configPath = path.join(root, "billion-context.json"); const alphaDeps = path.join(dshHome, "profiles", "alpha", "node_modules", "@deepseek-ai"); const desktopDeps = path.join(dshHome, "profiles", "desktop", "node_modules", "@deepseek-ai"); + // #5146: a DESKTOP-bundle-style profile whose node_modules holds no + // @deepseek-ai packages at all (the app ships them outside the profile) + mkdirSync(path.join(dshHome, "profiles", "bare"), { recursive: true }); mkdirSync(path.join(stateHome, "billion-context"), { recursive: true }); writeJson(path.join(alphaDeps, "dsh", "package.json"), { name: "@deepseek-ai/dsh", version: "0.9.17" }); writeJson(path.join(alphaDeps, "dsh-compaction-basic", "package.json"), { name: "@deepseek-ai/dsh-compaction-basic", version: "0.2.9" }); @@ -81,6 +84,11 @@ test("#3611 conflict-banner report carries versions, masked config, and evidence assert.ok(blob.includes("dsh profile alpha: dsh 0.9.17, dsh-compaction-basic 0.2.9, dsh-web-app ?"), `per-profile package versions incl. absent-as-?, got:\n${blob}`); assert.ok(blob.includes("dsh profile desktop: dsh 0.9.17, dsh-compaction-basic ?, dsh-web-app ?"), "second profile listed"); + // #5146: a profile where NONE of the three resolve gets an explanatory line + // instead of three bare "?"s (desktop-bundle installs keep the core out of + // the profile's node_modules). + assert.ok(blob.includes("dsh profile bare: (no @deepseek-ai packages under this profile bundle — on desktop installs the core ships inside the app; include the app version)"), + `package-free profile annotated, got:\n${blob}`); assert.ok(blob.includes("--- bili config (secrets masked) ---"), "config section present"); assert.ok(blob.includes('"apiKey":"***"'), "secret value replaced by ***"); From 26f36a5b38dc5843ea60b683c8e722f2b972e804 Mon Sep 17 00:00:00 2001 From: ci-check Date: Thu, 29 Oct 2026 22:00:38 +0800 Subject: [PATCH 3/7] ci(#5146): waive /__bili/upstreams report types in unused-export gate The new GET /__bili/upstreams read contract (UpstreamHealthEntry / UpstreamHealthReport) is referenced only inside src/upstream-alerts.ts; admin.ts consumes getUpstreamHealthReport() structurally via JSON.stringify. Keep the exports so the wire shape stays named at its definition site, mirroring the LastInstallRecord / ResidentFieldBreakdown+MemoryReport precedents in the same WAIVERS list. --- scripts/check-unused-exports.mjs | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/scripts/check-unused-exports.mjs b/scripts/check-unused-exports.mjs index ea21f73e74..308b886158 100644 --- a/scripts/check-unused-exports.mjs +++ b/scripts/check-unused-exports.mjs @@ -87,6 +87,11 @@ const WAIVERS = new Map([ // structurally via Awaited>); keep the // export so the wire shape stays named at its definition site. ["src/update.ts", new Set(["LastInstallRecord"])], + // #5146 /__bili/upstreams read contract: the two report payload interfaces + // are referenced only inside src/upstream-alerts.ts (admin.ts consumes + // getUpstreamHealthReport() structurally via JSON.stringify); keep the + // exports so the wire shape stays named at its definition site. + ["src/upstream-alerts.ts", new Set(["UpstreamHealthEntry", "UpstreamHealthReport"])], // #3962 gate telemetry: the coverage record crosses the gate/dsh-native // BUNDLE boundary on the globalThis channel — consumers use it // structurally through dshCompactionGateTelemetry()'s return type; keep From 67f8e89405b412d7c3c99269906c20238b3e3a71 Mon Sep 17 00:00:00 2001 From: ci-check Date: Fri, 30 Oct 2026 20:11:06 +0800 Subject: [PATCH 4/7] test(#5140): pin the unified storage mode the cold raw-tail proof requires (#4599) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit The #5140 recovery test proves forkSnapshot reconstructs a non-retained parent's raw tail from the UNIFIED index — but it never pinned its own storage mode. Under the CI storage baseline (preload-storage-baseline.mjs sets BILI_STORAGE_UNIFIED=legacy for the broad corpus) no unified rows exist, so the reconstruction has no source and forkSnapshot fails closed with 409 SNAPSHOT_UNAVAILABLE. Deterministic, not a flake: reproduces 100% on master and any merged branch with BILI_STORAGE_UNIFIED=legacy, passes without it (default shadow). Pin full like the sibling persist tests and restore the ambient value afterwards. --- tests/plugin-fork.test.ts | 10 +++++++++- 1 file changed, 9 insertions(+), 1 deletion(-) diff --git a/tests/plugin-fork.test.ts b/tests/plugin-fork.test.ts index 5e77c14886..9c0fa4915a 100644 --- a/tests/plugin-fork.test.ts +++ b/tests/plugin-fork.test.ts @@ -1462,6 +1462,10 @@ test("HTTP a mid-history rewrite replaces the snapshot wholesale with an exact b }); test("HTTP a cold-restored non-retained parent serves its intact raw tail immediately (#5140)", { timeout: 60_000 }, async () => { + // Pin the mode this raw-tail proof requires: under the runner's legacy + // baseline no unified rows exist and forkSnapshot fails closed (#4599). + const oldMode = process.env.BILI_STORAGE_UNIFIED; + process.env.BILI_STORAGE_UNIFIED = "full"; const h = await harness(true); try { const parent = resolveConversation("parent").session!; @@ -1480,7 +1484,11 @@ test("HTTP a cold-restored non-retained parent serves its intact raw tail immedi await sendModel(h, "parent", h.messages); assert.equal((await h.request("/__bili/plugin/snapshot?conversationId=parent")).status, 200); assert.equal(resolveConversation("parent").session!.pluginSnapshot!.length, 3); - } finally { await h.close(); } + } finally { + await h.close(); + if (oldMode === undefined) delete process.env.BILI_STORAGE_UNIFIED; + else process.env.BILI_STORAGE_UNIFIED = oldMode; + } }); test("fork protocol v2: snapshot serves the light revision, a stale v1 whole-store revision is still accepted (#2671 Phase 2b)", { timeout: 60_000 }, async () => { From 421026da9f98551cb347163d5762f39fc3e42bcc Mon Sep 17 00:00:00 2001 From: Louie0806 Date: Sat, 31 Oct 2026 02:06:53 +0800 Subject: [PATCH 5/7] fix(#4599): converge the four remaining master-red lanes onto ci-fixes MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit ci-fixes at 67f8e894 still leaves four Windows shards red, and all four deltas already exist on PR #5178. This ports the missing hunks so the convergence branch actually converges. 1. `test (windows-latest, 2x, 3)` — #5103. `UnifiedShadow` builds its db path as `${dir}/index.db`, so the open-failure warning reads `C:\...\sessions/index.db`. `issue5103` asserts the warning contains `path.join(dir, "index.db")` — `...\index.db` on win32 — so a mixed separator never matches. `path` is already imported at :30 and the db is opened through the same join, so POSIX output is byte-identical. 2. `test (windows-latest, 22, 4)` + the 4788 leg — #4739/#4788 teardown. The suites arm a real `SessionStorage` under `BILI_STORAGE_UNIFIED=full`, so the sidecar is still open at teardown. `cancelAll()` clears timers and the build map but never closes the shadow; Windows refuses to unlink a directory holding an open file (`EBUSY`/`EPERM` on `index.db`). Use `closeQuietly()` — cancel, then close — in the short-lived-store teardown. 3. `test (ubuntu-latest, 24, 3)` + `(windows-latest, 22, 2)` — #5155. Both cold-rebuild cases waited for the debounced durable flush with a fixed `setTimeout(20)`. A millisecond budget is a deadline a loaded runner can miss, and the miss is an honest `409 !== 200` from a one-turn-stale table. Await the settle itself: `afterNextFlush("conv-ptrs")` resolves once the armed save lands. 4. `test (ubuntu-latest, 22, 3)` — #4788 verdict deadlock. `afterNextFlush` parks a resolver in `flushWaiters`, and the only thing that drains it is the debounce timer — which is armed `unref?.()`. Once that timer is the last pending handle the loop drains, the promise never settles, and node reports `Promise resolution is still pending but the event loop has already resolved`. Keep the timer referenced exactly while a waiter exists (`ref()` on waiter registration; the host-upkeep default stays unref'd). Both view hunks and the two test-teardown hunks are byte-identical to the ones already on PR #5178, so this converges the branches instead of inventing a third variant. Verified locally: `npx tsc --noEmit` clean; issue4788 0/4 -> 4/4; issue4739 4/4; issue5103 5/5; inc-canonical-pointers 3/3. The `cancelAll` -> `closeQuietly` hunk has no local negative control: reverting it keeps Linux green because `EBUSY`/`EPERM` only fire where an open handle blocks unlink. That control can only run on a Windows lane — stated rather than papered over. The #4788 arm's own reader is confirmed instead: `cancelAll()` leaves `index.db`, `index.db-shm`, `index.db-wal` in `/proc/self/fd` while `closeQuietly()` leaves none (probed directly). --- src/persist.ts | 13 ++++++++++++- src/storage/shadow.ts | 4 ++-- tests/inc-canonical-pointers.test.ts | 8 ++++++-- tests/issue4739-bounded-session-load.test.ts | 4 ++-- tests/issue4788-resident-minimal.test.ts | 13 +++++++++---- 5 files changed, 31 insertions(+), 11 deletions(-) diff --git a/src/persist.ts b/src/persist.ts index 1586370479..315810d969 100644 --- a/src/persist.ts +++ b/src/persist.ts @@ -2766,7 +2766,14 @@ export class SessionStore { this.drainFlushWaiters(id, this.lastSaveLanded.get(id) === true); }); }, this.debounceMs); - timer.unref?.(); + // #4788: the debounce timer is host-owned periodic upkeep and must not + // pin a process open on its own -- so it is unref'd. But `afterNextFlush` + // makes it a HOST: a caller that awaits a verdict has no other handle + // guaranteed to fire, and an unref'd timer is not a reason for Node to + // stay alive. Awaiting then exits the loop with the promise unresolved + // (`Promise resolution is still pending but the event loop has already + // resolved`). Keep the timer referenced exactly while someone waits. + if (!this.flushWaiters.has(id)) timer.unref?.(); this.saveTimers.set(id, timer); } @@ -2787,6 +2794,10 @@ export class SessionStore { const ws = this.flushWaiters.get(sessionId) ?? []; ws.push((ok: boolean): void => { resolve(ok); }); this.flushWaiters.set(sessionId, ws); + // #4788: the timer was armed unref'd (no waiter existed then). The + // waiter now holds the promise open, so the timer must hold the loop + // open too -- otherwise the process drains and the verdict is lost. + this.saveTimers.get(sessionId)?.ref?.(); }); } diff --git a/src/storage/shadow.ts b/src/storage/shadow.ts index b9dd9f19dc..4656314ced 100644 --- a/src/storage/shadow.ts +++ b/src/storage/shadow.ts @@ -300,12 +300,12 @@ export class UnifiedShadow { : "; delete the file to reset the index"; loggerLog( "warn", - `[storage] unified sidecar failed to open ${dir}/index.db (${errStr}) — staying legacy-only${tail}`, + `[storage] unified sidecar failed to open ${path.join(dir, "index.db")} (${errStr}) — staying legacy-only${tail}`, ); recordSidecarOpenFailure(errStr); return null; } - const shadow = new UnifiedShadow(mode, store, legacyLoad ?? (() => null), undefined, `${dir}/index.db`); + const shadow = new UnifiedShadow(mode, store, legacyLoad ?? (() => null), undefined, path.join(dir, "index.db")); recordSidecarInstance(shadow); const engineTag = ` [engine: ${store.engine}/${store.engineSource}]`; const walTag = ` [wal-cap: ${walLimit}]`; diff --git a/tests/inc-canonical-pointers.test.ts b/tests/inc-canonical-pointers.test.ts index e0210b8d4f..bdc730e143 100644 --- a/tests/inc-canonical-pointers.test.ts +++ b/tests/inc-canonical-pointers.test.ts @@ -105,7 +105,11 @@ test("cold canonical rebuild: identical resync keeps the shared prefix; delta as // #5155: the wrap-up defers the durable flush (debounced) — let the // rows settle before dropping the resident caches, or the cold // rebuild reads a one-turn-stale table and honestly 409s. - await new Promise((resolve) => setTimeout(resolve, 20)); + // Await the settle itself (afterNextFlush resolves once the armed save + // lands) instead of a fixed sleep: the settle is a real condition, and + // a magic millisecond budget is a deadline a loaded windows-latest + // runner can miss — the 409 flake this replaces. + assert.equal(await getStore().afterNextFlush("conv-ptrs"), true, "the turn's durable save must land before the cold reset"); __resetIncrementalLaneForTests(); const delta = await h.turn({ sinceSeq: 3, messages: [core("u4", "four")] }); @@ -135,7 +139,7 @@ test("cold canonical rebuild: edited-tail resync keeps the shared prefix, drops await h.turn({ sinceSeq: 0, resync: true, messages: [core("u1", "one"), core("u2", "two-EDITED")] }); await __incrementalPtrChainForTests("conv-ptrs"); // #5155: settle the debounced flush before the cold reset (see above). - await new Promise((resolve) => setTimeout(resolve, 20)); + assert.equal(await getStore().afterNextFlush("conv-ptrs"), true, "the resync's durable save must land before the cold reset"); __resetIncrementalLaneForTests(); const delta = await h.turn({ sinceSeq: 2, messages: [core("u3", "three")] }); assert.equal(delta.status, 200); diff --git a/tests/issue4739-bounded-session-load.test.ts b/tests/issue4739-bounded-session-load.test.ts index bc9906b5f0..bf91303252 100644 --- a/tests/issue4739-bounded-session-load.test.ts +++ b/tests/issue4739-bounded-session-load.test.ts @@ -139,7 +139,7 @@ test("full mode: viewFor decodes only pointer rows and matches the full-log deco assert.deepEqual(via, reference, "bounded decode byte-equal to the full-log decode"); assert.equal(delta, expectedPointers, `viewFor read ${delta} message rows for ${expectedPointers} pointer seqs (full log = ${liveSeqs.size})`); } finally { - store.cancelAll(); + store.closeQuietly(); if (prev === undefined) delete process.env.BILI_STORAGE_UNIFIED; else process.env.BILI_STORAGE_UNIFIED = prev; rmrf(dir); @@ -178,7 +178,7 @@ test("full mode: dropped metadata restores from the index alone (narrow read, no assert.ok(again, "envelope rewritten by later save"); assert.deepEqual(Object.keys(JSON.parse(fs.readFileSync(again, "utf8")).payload.metadata as object).sort(), persistedKeys); } finally { - store.cancelAll(); + store.closeQuietly(); if (prev === undefined) delete process.env.BILI_STORAGE_UNIFIED; else process.env.BILI_STORAGE_UNIFIED = prev; rmrf(dir); diff --git a/tests/issue4788-resident-minimal.test.ts b/tests/issue4788-resident-minimal.test.ts index 5f20c83aa8..488ff932b9 100644 --- a/tests/issue4788-resident-minimal.test.ts +++ b/tests/issue4788-resident-minimal.test.ts @@ -129,7 +129,12 @@ test("(a) between-turns drop: every landed request-end boundary discharges dropp assert.ok((s.blockContents as LazyBlockContents).isDropped, "still folded after the second boundary"); assert.ok(isDroppedMetadata(s.metadata)); } finally { - store.cancelAll(); + // #4739/#5140 family: the suite arms a real SessionStorage under + // BILI_STORAGE_UNIFIED=full, so the sidecar handle is still open here. + // cancelAll() clears timers and the build map but never closes the + // shadow; Windows then refuses to unlink the directory (EBUSY/EPERM). + // Close the short-lived store first: cancel, then close. + store.closeQuietly(); rmrf(dir); } }); @@ -164,7 +169,7 @@ test("(b) narrow-read reconstruction deepEquals the pre-drop reference", async ( const rec = (parsed.payload ?? parsed) as Record; assert.equal(Object.keys((rec.blockContents ?? {}) as object).length, Object.keys(refBlocks).length, "full set round-trips through the envelope"); } finally { - store.cancelAll(); + store.closeQuietly(); rmrf(dir); } }); @@ -203,7 +208,7 @@ test("(c) hard cap under sustained activity: boundaries drop, in-flight skips, w process.env.BILI_SESSION_RESIDENT_HARD_MAX_BYTES = saved; } } finally { - store.cancelAll(); + store.closeQuietly(); rmrf(dir); } }); @@ -246,7 +251,7 @@ test("(d) envelope-frozen routing is unchanged AND gets equivalent drops", async assert.equal(s.blockContents.size, 21, "all entries resident again after reconstruction"); } finally { process.env.BILI_STORAGE_UNIFIED = "legacy"; - store.cancelAll(); + store.closeQuietly(); rmrf(dir); } }); From a3019b05628e47a3c67137734297a1c6ed1ff956 Mon Sep 17 00:00:00 2001 From: ework-agent Date: Sat, 31 Oct 2026 02:25:48 +0800 Subject: [PATCH 6/7] test(#4788): keep the event loop alive across the unref debounced-save settle (#4599) The turn() helper arms scheduleSave whose debounce timer is unref by design (persist.ts: a pending save must not hold the process open at shutdown) and then awaits afterNextFlush. Nothing else in that await is refd, so in a node:test child whose only surviving handles are unref sockets the event loop drains before the 50 ms debounce fires and the settle promise never resolves: all four subtests fail with "Promise resolution is still pending but the event loop has already resolved" / cancelledByParent. Reproduced deterministically on master 44067a5b AND on pre-merge ci-fixes 67f8e894 (standalone run, exact CI preload chain), while the same file has passed some CI legs by ambient-liveness luck - a born-flake per section 7.2 (no environmental luck). The pin at 67f8e894 does not touch this path. Fix: arm a refd 25 ms keep-alive interval around the afterNextFlush await (cleared in finally) so the debounce fires deterministically on every host. Test-only diff; production settle semantics untouched. --- tests/issue4788-resident-minimal.test.ts | 14 +++++++++++++- 1 file changed, 13 insertions(+), 1 deletion(-) diff --git a/tests/issue4788-resident-minimal.test.ts b/tests/issue4788-resident-minimal.test.ts index 5f20c83aa8..a02ae2c4a5 100644 --- a/tests/issue4788-resident-minimal.test.ts +++ b/tests/issue4788-resident-minimal.test.ts @@ -86,7 +86,19 @@ async function turn(store: SessionStore, s: Session, midFlight?: boolean): Promi if (midFlight) s.inFlight = 1; s.revisionEpoch = (s.revisionEpoch ?? 0) + 1; store.scheduleSave(s); - const landed = await store.afterNextFlush(s.id); + // The debounced-save timer is unref'd by design (persist.ts: a pending save + // must not hold the process open at shutdown), so this await keeps NOTHING + // else alive: in a node:test child whose only surviving handles are unref'd + // sockets the event loop drains before the 50 ms debounce fires and + // afterNextFlush never settles (born-flake, §7.2 no environmental luck). + // A ref'd keep-alive makes the settle deterministic regardless of host. + const keepAlive = setInterval((): void => {}, 25); + let landed: boolean; + try { + landed = await store.afterNextFlush(s.id); + } finally { + clearInterval(keepAlive); + } if (midFlight) s.inFlight = 0; return landed; } From 06ad804ac067d8e166b6b28f6dcaf3019660dd83 Mon Sep 17 00:00:00 2001 From: ework-agent Date: Sat, 31 Oct 2026 03:11:28 +0800 Subject: [PATCH 7/7] test(#4788): drop the test-side keep-alive superseded by the persist.ts ref-tracking fix (#4599) PR #5262 fixes #4788 at the source: scheduleSave now leaves the debounce timer refd while flush waiters exist (and afterNextFlush refs a pending unrefd timer when a waiter registers), so the settle is deterministic for EVERY caller on every host. The 25 ms keep-alive interval added in a3019b05 only patched this one test file and is redundant now; removing it leaves one mechanism instead of two. The comment stays: without it a future reader re-adds the interval or deletes the persist.ts tracking as dead weight. --- tests/issue4788-resident-minimal.test.ts | 18 +++++------------- 1 file changed, 5 insertions(+), 13 deletions(-) diff --git a/tests/issue4788-resident-minimal.test.ts b/tests/issue4788-resident-minimal.test.ts index bc3a6088d8..c96e38ec54 100644 --- a/tests/issue4788-resident-minimal.test.ts +++ b/tests/issue4788-resident-minimal.test.ts @@ -86,19 +86,11 @@ async function turn(store: SessionStore, s: Session, midFlight?: boolean): Promi if (midFlight) s.inFlight = 1; s.revisionEpoch = (s.revisionEpoch ?? 0) + 1; store.scheduleSave(s); - // The debounced-save timer is unref'd by design (persist.ts: a pending save - // must not hold the process open at shutdown), so this await keeps NOTHING - // else alive: in a node:test child whose only surviving handles are unref'd - // sockets the event loop drains before the 50 ms debounce fires and - // afterNextFlush never settles (born-flake, §7.2 no environmental luck). - // A ref'd keep-alive makes the settle deterministic regardless of host. - const keepAlive = setInterval((): void => {}, 25); - let landed: boolean; - try { - landed = await store.afterNextFlush(s.id); - } finally { - clearInterval(keepAlive); - } + // #4788: while a caller awaits afterNextFlush, persist.ts keeps the pending + // debounce timer ref'd, so this settle is deterministic even in a node:test + // child whose only surviving handles are unref'd sockets — no test-side + // keep-alive needed here anymore. + const landed = await store.afterNextFlush(s.id); if (midFlight) s.inFlight = 0; return landed; }