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 diff --git a/src/persist.ts b/src/persist.ts index 5aa2e6f80f..aca170c9a2 100644 --- a/src/persist.ts +++ b/src/persist.ts @@ -2855,7 +2855,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); } @@ -2876,6 +2883,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/server/admin.ts b/src/server/admin.ts index bec69d8de1..6aaa751b84 100644 --- a/src/server/admin.ts +++ b/src/server/admin.ts @@ -26,7 +26,7 @@ import { PCK_NOTICE_ID, dismissUpgradeNotice, pckNoticeActive } from "../pck-not 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"; @@ -235,6 +235,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/storage/shadow.ts b/src/storage/shadow.ts index db9efa522b..6c0480b4d8 100644 --- a/src/storage/shadow.ts +++ b/src/storage/shadow.ts @@ -329,12 +329,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/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/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/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..c96e38ec54 100644 --- a/tests/issue4788-resident-minimal.test.ts +++ b/tests/issue4788-resident-minimal.test.ts @@ -86,6 +86,10 @@ async function turn(store: SessionStore, s: Session, midFlight?: boolean): Promi if (midFlight) s.inFlight = 1; s.revisionEpoch = (s.revisionEpoch ?? 0) + 1; store.scheduleSave(s); + // #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; @@ -129,7 +133,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 +173,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 +212,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 +255,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); } }); 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); +}); diff --git a/tests/plugin-fork.test.ts b/tests/plugin-fork.test.ts index 96d9dfeb0f..44db0d8a8d 100644 --- a/tests/plugin-fork.test.ts +++ b/tests/plugin-fork.test.ts @@ -1477,6 +1477,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!; @@ -1495,7 +1499,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 () => { 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 ***");