diff --git a/README.md b/README.md index fda4fab72d..a29b797b44 100644 --- a/README.md +++ b/README.md @@ -452,7 +452,7 @@ The proxy checks npm on startup and every 3 minutes; a newer version is installe While the running process is behind the on-disk install ("stale"), the web UI shows a banner (running vs installed version, auto-restart state) and `GET /__bili/status` returns `{version, diskVersion, stale, autoRestartOnUpdate, advisory, updatePolicy, inFlight}` for scripting (`advisory` is the active critical-defect entry or `null`, see below; `updatePolicy` reports the #3619 rollout state — policy source, `hold`/`pinned`/`blocked`, and the pending staggered upgrade with its due time). Disable permanently via `"autoUpdate": false` in config or `ACP_AUTO_UPDATE=0`. -In **plugin mode** (`bili opencode` / `bili pi` / `bili dsh` — the proxy runs embedded in a long-lived host), "restart bili" means restarting the **host**: the embedded process cannot exit on its own, so a host left open keeps serving whatever code it started with, silently missing every fix auto-update reports as installed (#1603). Enable `--auto-restart-on-update` to have it self-restart at a safe point, or run `/acp` (OpenCode hosts today — pi/dsh coverage tracked in #2084) — the status panel appends a one-line staleness warning (running vs installed version) whenever the on-disk install is ahead of the live process. Auto-update only ever replaces disk files and (optionally) the proxy process; **injected code inside the host process only takes effect after the host itself restarts** — so if the injection breaks at runtime mid-session (a disposed agent scope, a disabled plugin), model traffic goes direct and uncompressed until then. Since #3581 this state is visible instead of silent: the web UI shows a "takeover broken" banner (agent + refusal count) whenever a plugin reports its attribution gate degraded while no session is live, and `GET /__bili/status` / `/__bili/overview` expose the per-agent `attribution` state (`ok`/`none`/`threw`, refusal count, first-refusal timestamp). +In **plugin mode** (`bili opencode` / `bili pi` / `bili dsh` — the proxy runs embedded in a long-lived host), "restart bili" means restarting the **host**: the embedded process cannot exit on its own, so a host left open keeps serving whatever code it started with, silently missing every fix auto-update reports as installed (#1603). Enable `--auto-restart-on-update` to have it self-restart at a safe point, or run `/acp` (OpenCode hosts today — pi/dsh coverage tracked in #2084) — the status panel appends a one-line staleness warning (running vs installed version) whenever the on-disk install is ahead of the live process. Auto-update only ever replaces disk files and (optionally) the proxy process; **injected code inside the host process only takes effect after the host itself restarts** — so if the injection breaks at runtime mid-session (a disposed agent scope, a disabled plugin), model traffic goes direct and uncompressed until then. Since #3581 this state is visible instead of silent: the web UI shows a "takeover broken" banner (agent + refusal count) whenever a plugin reports its attribution gate degraded while no session is live, and `GET /__bili/status` / `/__bili/overview` expose the per-agent `attribution` state (`ok`/`none`/`threw`, refusal count, first-refusal timestamp). Background lanes that are agentless **by design** (e.g. dsh's title-generation sidecar) never carry an initiator, so their small one-shot calls go direct by definition; under the #3651 contract these normal interceptions are NEVER reported and therefore can never raise the banner. The banner fires only when the attribution lookup itself throws (an internal host error, e.g. a disposed agent scope), and then names what failed: the thrown error text, the masked endpoint, when the episode started, how many requests went uncompressed, the remedy (restart the host process), plus copy/submit-diagnostics buttons that prefill a GitHub issue. **Update visibility is silent by default since v0.1.183 (#1977):** routine/recommended releases no longer render an update line on the `/acp` panel footer or in `acp_status` — only a `critical`-tier release surfaces there (`CRITICAL UPDATE READY/AVAILABLE`). The standing ways to check an installed-but-not-restarted release are the stale cues above: web UI banner, the `/acp` one-line warning, and `GET /__bili/status`. Deliberately no always-on nag; the advisory channel (#1481) remains the only auto-visible critical path. diff --git a/README.zh-CN.md b/README.zh-CN.md index b0a8eeb029..322ffce25a 100644 --- a/README.zh-CN.md +++ b/README.zh-CN.md @@ -337,7 +337,7 @@ bili --no-auto-update # 本次启动禁用自动更新 **升级撒点(#3619):** 发现新版本后,自动更新不再立即应用。每台实例在延迟窗口内随机抽取**一个**时刻(内置默认 **15 分钟 – 48 小时**),到点再升;抽中的时刻落盘持久化,重启不重摇。全量升级因此摊开在整个窗口内,而不是发版后十几分钟内全体升完,给坏发版留出中途拦截的响应窗(#3401/#3600/#3394)。窗口与紧急控制放在一个公开的策略文件里(本仓库的 `auto-update-policy.json`,匿名 GET + ETag 缓存):`hold` 全局冻结自动升级、`blocked` 跳过指定版本、`pinned` 设置升级天花板。按设计 fail-open——文件不可达或格式非法时直接回退本地默认;而且它只能调节奏/跳过/钉住,**永远控制不了装什么**。本机退出方式:手动 `bili update` 绕过 `hold`/撒点(但仍遵守 `blocked`/`pinned`)、`"staggerMinDelayMs": 0` + `"staggerMaxDelayMs": 0`(或 `BILI_UPDATE_STAGGER_MIN_MS`/`BILI_UPDATE_STAGGER_MAX_MS`)恢复旧版立即升级、`ACP_AUTO_UPDATE=0` 完全停用自动更新。完整格式与语义见 [CONFIGURATION.zh-CN.md → `update`](CONFIGURATION.zh-CN.md#update)。 -**插件模式**(`bili opencode` / `bili pi` / `bili dsh` —— 代理内嵌在长驻宿主里运行时),"重启 bili"指的是重启**宿主**:内嵌进程无法自行退出,所以一直开着的宿主会持续运行它启动时的那份代码,默默地错过自动更新报告为"已安装"的每一处修复(#1603)。启用 `--auto-restart-on-update` 可让它在安全点自重启;或运行 `/acp`(当前仅 OpenCode 宿主,pi/dsh 覆盖见 #2084)—— 当磁盘安装领先于运行进程时,状态面板会追加一行过期警告(运行版本 vs 已安装版本)。 +**插件模式**(`bili opencode` / `bili pi` / `bili dsh` —— 代理内嵌在长驻宿主里运行时),"重启 bili"指的是重启**宿主**:内嵌进程无法自行退出,所以一直开着的宿主会持续运行它启动时的那份代码,默默地错过自动更新报告为"已安装"的每一处修复(#1603)。启用 `--auto-restart-on-update` 可让它在安全点自重启;或运行 `/acp`(当前仅 OpenCode 宿主,pi/dsh 覆盖见 #2084)—— 当磁盘安装领先于运行进程时,状态面板会追加一行过期警告(运行版本 vs 已安装版本)。若注入代码在运行期损坏(如 agent scope 被销毁),模型流量会静默直连(不压缩),直到宿主重启;自 #3581 起该状态可见:无会话运行时插件上报归属闸门降级,Web UI 显示「接管已失效」横幅,`GET /__bili/status` / `/__bili/overview` 暴露每代理的 `attribution` 状态。**按设计无归属**的后台车道(如 dsh 标题生成 sidecar)本身不携带 initiator,其小体量一次性请求按定义直连——按 #3651 契约这类正常拦截**永不报告**,因此永远不会触发横幅。横幅只在归属查询本身抛错(宿主内部错误,如 agent scope 被销毁)时出现,并给出可诊断信息:抛出的错误文本、脱敏后的端点、起始时间、已直连的请求数、解决方法(重启宿主进程),以及复制/提交诊断按钮(预填 GitHub issue)。 **自 v0.1.183 起,更新提示默认静默(#1977):**routine/recommended 版本不再在 `/acp` 面板脚注或 `acp_status` 中渲染更新行 —— 只有 `critical` 级别的版本会在那里显示(`CRITICAL UPDATE READY/AVAILABLE`)。检查"已安装未重启"版本的常设途径即上文所列的过期线索:Web UI 横幅、`/acp` 单行警告、以及 `GET /__bili/status`。刻意不设常驻提醒;严重缺陷公告通道(#1481)仍是唯一自动可见的严重路径。 diff --git a/src/agent/dsh-native.ts b/src/agent/dsh-native.ts index c130ee2040..b8e10413be 100644 --- a/src/agent/dsh-native.ts +++ b/src/agent/dsh-native.ts @@ -57,6 +57,7 @@ import { asciiHeaderValue, fetchManifest, fetchProxyVersion, fetchStatus, fetchS 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, type IncrementalWireMessage } from "./incremental-client.js"; import { detectCompactionOverlayGap, registerCompactionDisable } from "./dsh-compaction-disable.js"; +import { maskUrlForDiagnostics } from "../log-mask.js"; export { sideShapedBody } from "./fork-adopt.js"; export const name = "bili-native"; @@ -256,6 +257,12 @@ function trackChain(p: Promise): Promise { // single time even though apply() may run again after context re-arming. let webProfileWarned = false; +// #3651: once-per-process — the non-web-profile counterpart of the above: a +// durable trace that the #3354 load-time overlay IS registered in a profile +// that does not bundle @deepseek-ai/dsh-web-app (the web-profile note never +// fires there, leaving field verification blind). +let nonWebTraceLogged = false; + // #3380: once-per-process — the idempotent-adoption convergence notice must // not repeat per attempt (same discipline as webProfileWarned). let regAdoptLogged = false; @@ -1537,19 +1544,49 @@ export function apply(ctx: PluginContext): void { return url; } }; - // #3581: the refusal evidence above exists ONLY in this host process — the - // proxy learns about the silent-direct degradation exclusively through - // event reports (first refusal, state change, count doubling, give-up - // latch, recovery). Healthy lanes never POST, so the healthy path costs - // nothing; a missed report delays visibility, never touches requests. + // #3581/#3651: the refusal evidence above exists ONLY in this host process + // — the proxy learns about the silent-direct degradation exclusively + // through event reports. Under the #3651 owner contract only DIAGNOSABLE + // failures report: by-design agentless lanes (`none`) are normal + // interceptions and stay silent forever (their per-endpoint durables are + // the record); internal errors (`threw`) report with error text + masked + // endpoint so the panel can name the problem, its impact and the remedy. + // Healthy lanes never POST, so the healthy path costs nothing; a missed + // report delays visibility, never touches requests. const reportAttrHealth = (): void => { const base = register.base; if (base === undefined) return; - void reportAttributionHealth(base, { agent: name, state: attrHealth.state, refusals: attrHealth.refusals, since: attrHealth.since }).catch(() => undefined); + void reportAttributionHealth(base, { + agent: name, + state: attrHealth.state, + refusals: attrHealth.refusals, + since: attrHealth.since, + ...(attrHealth.error !== undefined ? { error: attrHealth.error } : {}), + ...(attrHealth.endpoint !== undefined ? { endpoint: attrHealth.endpoint } : {}), + }).catch(() => undefined); }; - const noteAttrRefusal = (state: "none" | "threw"): void => { + // #3651 (owner contract, #3651 thread): 「正常拦截不报告,报告必须可诊断」. + // A by-design agentless lane (dsh's title-gen sidecar et al.) refuses a + // handful of small one-shot calls per host lifetime and can NEVER recover + // — reporting it lit the #3581 banner ("takeover broken … restart the + // host") on every short-lived worker boot while the main conversation + // compressed fine (field shape: exactly one refused sidecar call per + // worker, thousands of compressed turns elsewhere). So `none` accumulates + // locally (counters + recovery semantics intact) but NEVER reports. + // `threw` — an exception inside the host's attribution lookup, an internal + // error, never legitimate agentless evidence (#1158 L2) — keeps the + // #3581a cadence (first refusal immediate, then count doubling) and now + // carries the thrown message + masked endpoint as diagnostics. + const noteAttrRefusal = (state: "none" | "threw", detail?: { message?: string; endpoint?: string }): void => { + if (state === "none") return; + if (detail?.message !== undefined) attrHealth.error = detail.message; + if (detail?.endpoint !== undefined) attrHealth.endpoint = detail.endpoint; + const diag = { + ...(attrHealth.error !== undefined ? { error: attrHealth.error } : {}), + ...(attrHealth.endpoint !== undefined ? { endpoint: attrHealth.endpoint } : {}), + }; if (attrHealth.state === "ok") { - attrHealth = { state, refusals: 1, since: Date.now() }; + attrHealth = { state, refusals: 1, since: Date.now(), ...diag }; attrReportedRefusals = 1; reportAttrHealth(); return; @@ -1557,7 +1594,8 @@ export function apply(ctx: PluginContext): void { attrHealth.refusals += 1; const changed = attrHealth.state !== state; attrHealth.state = state; - if (changed || attrHealth.refusals >= attrReportedRefusals * 2) { + const doubled = attrHealth.refusals >= attrReportedRefusals * 2; + if (changed || doubled) { attrReportedRefusals = attrHealth.refusals; reportAttrHealth(); } @@ -1586,7 +1624,12 @@ export function apply(ctx: PluginContext): void { const line = `bili-native-dsh: model request sent DIRECT (uncompressed) — takeover gate refused ${key}: ${reason} — refusals so far: ${count}${suffix}`; persistClientEvent(line); } - if (attr.state !== "ok") noteAttrRefusal(attr.state); + if (attr.state !== "ok") { + noteAttrRefusal(attr.state, { + ...(attr.state === "threw" ? { message: attr.message } : {}), + endpoint: maskUrlForDiagnostics(key), + }); + } return false; }; // #2924 direction 2: `threw` is an INTERNAL error in the host's @@ -1597,7 +1640,7 @@ export function apply(ctx: PluginContext): void { // session (the host's own compaction and the #1117 direct fallback must // both ship the full history upstream, which 400s once it exceeds the // window — all recovery paths vanish the instant the gate lets go). - const settleThrewAttribution = async (url: string, body: { bytes: number } | undefined): Promise => { + const settleThrewAttribution = async (message: string, url: string, body: { bytes: number } | undefined): Promise => { // #3581: after giveUpStreak consecutive requests whose settle-retry // exhausted STILL threw, stop re-settling — a disposed ctx stays // disposed for the host's whole lifetime (#3578), so retrying the same @@ -1630,7 +1673,7 @@ export function apply(ctx: PluginContext): void { if (body !== undefined && window !== undefined && Math.ceil(body.bytes / 4) > window) { const floorTokens = Math.ceil(body.bytes / 4); persistClientEvent(`bili-native-dsh: takeover gate refused ${gateKeyOf(url)} LOCALLY (422) — attribution threw and body ≥${floorTokens} tok (floor est ${body.bytes}B/4) exceeds window ${window}; a direct send would 400 upstream (#2924)`); - noteAttrRefusal("threw"); + noteAttrRefusal("threw", { message, endpoint: maskUrlForDiagnostics(gateKeyOf(url)) }); return "refuse-oversized"; } return refuse(url, attributionOf(ctx)); @@ -1644,7 +1687,7 @@ export function apply(ctx: PluginContext): void { return true; } if (attr.state === "threw") { - return settleThrewAttribution(url, body).then((verdict) => { + return settleThrewAttribution(attr.message, url, body).then((verdict) => { if (verdict === true) { threwStreak = 0; threwGiveUpNoticed = false; @@ -2031,6 +2074,15 @@ export function apply(ctx: PluginContext): void { } else { persistClientEvent("profile bundles include @deepseek-ai/dsh-web-app — its agent presets run their own compaction-basic in nested rows no patch layer can address by id (#1772/#2360); bili disables those preset rows at load time via a vendored overlay (deshark42/dsh-compaction-basic-disable, MIT, #3354), so dsh native auto-compaction stays OFF in this profile (ACP compression unaffected). If you still see a dsh compaction checkpoint land on a newer/older dsh build — the overlay rides cordis' INTERNAL 'internal/config' hook and is version-sensitive — fall back to the #2474 full-snapshot override of preset-standard in your profile's cordis.patch.yml (a partial snippet silently drops everything not restated; see CONFIGURATION.md)"); } + } else if (bundles !== undefined && !nonWebTraceLogged) { + // #3651: the web-profile note above is the ONLY confirmation that + // the #3354 overlay took effect, and it is scoped to dsh-web-app + // bundles — every other profile boots silently even though + // registerCompactionDisable ran unconditionally. Leave one durable + // trace so a field log can tell "overlay registered" from "profile + // shape unconfirmed". + nonWebTraceLogged = true; + persistClientEvent("profile bundles do not include @deepseek-ai/dsh-web-app — the #3354 load-time compaction-disable middleware is registered anyway (it rewrites agent-preset compaction-basic/command-compact rows through cordis' version-sensitive internal 'internal/config' hook), but no web-profile disable note applies here; if dsh native auto-compaction still lands in this profile, apply the #2474 full-snapshot override of preset-standard (CONFIGURATION.md) — and remember dsh.allowDshCompaction (#2028), when on, lets such checkpoints through instead of the #1729 wire guard refusing them"); } } @@ -2170,6 +2222,11 @@ export function _resetWebProfileWarningForTest(): void { webProfileWarned = false; } +/** Test hook (#3651): reset the once-per-process non-web-profile trace flag. */ +export function _resetNonWebTraceForTest(): void { + nonWebTraceLogged = 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; @@ -2187,17 +2244,19 @@ export function _setThrewRetryForTest(cfg?: Partial) threwRetry = { ...DEFAULT_THREW_RETRY, ...cfg }; } -// #3581: attribution-health counters backing the /__bili status surface. -// Module-level like register/webProfileWarned (one live injection per host -// process); _resetAttributionHealthForTest isolates test scenarios. -let attrHealth: { state: "ok" | "none" | "threw"; refusals: number; since: number } = { state: "ok", refusals: 0, since: 0 }; +// #3581/#3651: attribution-health counters backing the /__bili status +// surface. Module-level like register/webProfileWarned (one live injection +// per host process); _resetAttributionHealthForTest isolates test scenarios. +// error/endpoint ride along on `threw` reports only (#3651 diagnosable- +// failure contract) — `none` episodes never report at all. +let attrHealth: { state: "ok" | "none" | "threw"; refusals: number; since: number; error?: string; endpoint?: string } = { state: "ok", refusals: 0, since: 0 }; let attrReportedRefusals = 0; let threwStreak = 0; let threwGiveUpNoticed = false; /** Test hook (#3581): snapshot the attribution-health counters and give-up latch. */ -export function _attributionHealthForTest(): { state: "ok" | "none" | "threw"; refusals: number; since: number; threwStreak: number; gaveUp: boolean } { - return { state: attrHealth.state, refusals: attrHealth.refusals, since: attrHealth.since, threwStreak, gaveUp: threwGiveUpNoticed }; +export function _attributionHealthForTest(): { state: "ok" | "none" | "threw"; refusals: number; since: number; error?: string; endpoint?: string; threwStreak: number; gaveUp: boolean } { + return { state: attrHealth.state, refusals: attrHealth.refusals, since: attrHealth.since, ...(attrHealth.error !== undefined ? { error: attrHealth.error } : {}), ...(attrHealth.endpoint !== undefined ? { endpoint: attrHealth.endpoint } : {}), threwStreak, gaveUp: threwGiveUpNoticed }; } /** Test hook (#3581): reset the attribution-health counters between scenarios. */ diff --git a/src/agent/host-log.ts b/src/agent/host-log.ts index 8afed8ddce..a0cd325b86 100644 --- a/src/agent/host-log.ts +++ b/src/agent/host-log.ts @@ -18,6 +18,7 @@ // can't strand these writes on a renamed inode. import { appendFileSync, mkdirSync } from "node:fs"; import path from "node:path"; +import { maskIpsInText, maskUrlsForDiagnostics, redactSecretsInText } from "../log-mask.js"; import { defaultLogFile } from "../paths.js"; import { VERSION } from "../version.js"; @@ -25,7 +26,14 @@ export function logHostEvent(level: "info" | "warn" | "error", origin: string, m try { const file = defaultLogFile(); mkdirSync(path.dirname(file), { recursive: true }); - appendFileSync(file, `${new Date().toISOString()} [${level}] [v=${VERSION}] ${origin ? `${origin} ` : ""}${msg}\n`); + // #3651: these lines share the paste-able bili.log with proxy-side + // lines, so they get the same sink-level safety net the proxy logger + // applies at its choke point (#1718/#255/#897). Before this, client- + // side events wrote real upstream hosts verbatim — the #3651 field log + // leaked provider domains through [dsh-client] lines while every + // proxy-side line masked them. + const safe = maskUrlsForDiagnostics(maskIpsInText(redactSecretsInText(msg))); + appendFileSync(file, `${new Date().toISOString()} [${level}] [v=${VERSION}] ${origin ? `${origin} ` : ""}${safe}\n`); } catch { // best-effort: logging must never break the host } diff --git a/src/agent/shared.ts b/src/agent/shared.ts index beec249d4c..c4b74509d4 100644 --- a/src/agent/shared.ts +++ b/src/agent/shared.ts @@ -332,15 +332,24 @@ type AttributionHealthReport = { refusals: number; /** Epoch ms of the first refusal in the current episode (0 while healthy). */ since: number; + /** #3651: last thrown attribution error text — the report only fires for + * diagnosable failures (`threw`), so this is what makes the banner name + * the actual problem instead of a generic "takeover broken". */ + error?: string; + /** #3651: masked endpoint (origin+pathname) of the last refused request. */ + endpoint?: string; }; -/** Takeover-attribution health report (#3581): when the host plugin's gate - * refuses model traffic (disposed injection after a version swap — #3578), - * the proxy must learn about it or the silent-direct degradation stays - * invisible in /__bili/status and the panel. Sent ONLY on degradation events - * (first refusal, state change, count doubling, give-up latch, recovery) — - * fire-and-forget at call sites; a missed report delays visibility, never - * touches the request path. */ +/** Takeover-attribution health report (#3581, contract reworked by #3651): + * when the host plugin's gate refuses model traffic because its attribution + * lookup THREW (disposed injection after a version swap — #3578), the proxy + * must learn about it or the silent-direct degradation stays invisible in + * /__bili/status and the panel. By-design agentless lanes (`none`) are normal + * interceptions and NEVER report (#3651 owner contract) — so any report on + * this route is either a diagnosable failure or a recovery notice. Sent on + * threw-episode events (first refusal, state change to threw, count doubling, + * give-up latch) plus recovery — fire-and-forget at call sites; a missed + * report delays visibility, never touches the request path. */ export async function reportAttributionHealth(proxyBase: string, health: AttributionHealthReport): Promise { const { ok, status } = await fetchJson(`${proxyBase}/__bili/plugin/attribution`, { method: "POST", diff --git a/src/log-mask.ts b/src/log-mask.ts index d34a619b1b..4792109b1f 100644 --- a/src/log-mask.ts +++ b/src/log-mask.ts @@ -137,6 +137,42 @@ export function maskUrlsForPublish(text: string): string { return text.replace(/https?:\/\/[^\s"'<>]+/gi, (m) => maskUrlForPublish(m)); } +/** Mask a URL for DIAGNOSTIC payloads (#3651): same public/private rule as + * maskUrlForLog, with a LOOPBACK EXEMPTION — localhost / 127.x / ::1 / + * 0.0.0.0 identify bili's own local proxy, never an external party (same + * carve-out logic as maskIpsInText). Used where the masked value must stay + * actionable (attribution-health endpoints, host-side event lines). */ +export function maskUrlForDiagnostics(url: string): string { + try { + const u = new URL(url); + // Node keeps the brackets in hostname for bracketed IPv6 ([::1]). + // Loopback is returned VERBATIM (not re-serialized): URL.pathname + // normalizes pathless URLs by appending "/", which would drift + // byte-pinned local-origin lines for no security benefit — loopback + // identifies bili's own proxy, and secrets are scrubbed separately + // by redactSecretsInText upstream of every call site. + const h = u.hostname.replace(/^\[|\]$/g, "").toLowerCase(); + if (h === "localhost" || isLoopbackIp(h)) return url; + } catch { + // unparseable — fall through to the generic masker + } + return maskUrlForLog(url); +} + +/** Text-level twin of maskUrlForDiagnostics (#3651): masks every embedded + * http(s) URL with the loopback-exempt rule; unparseable fragments pass + * through (the IP scrubber neutralizes bare IP literals separately). */ +export function maskUrlsForDiagnostics(text: string): string { + return text.replace(/https?:\/\/[^\s"'<>]+/gi, (m) => { + try { + void new URL(m); + return maskUrlForDiagnostics(m); + } catch { + return m; + } + }); +} + /** Mask the host of a `host:port` target (e.g. an HTTP CONNECT request * line) for logging. */ export function maskHostPortForLog(target: string): string { diff --git a/src/plugin.ts b/src/plugin.ts index 366554bb5b..404624285c 100644 --- a/src/plugin.ts +++ b/src/plugin.ts @@ -622,13 +622,16 @@ export function handlePluginRuntimeInfo(payload: string, res: import("node:http" res.end(JSON.stringify({ ok: true })); } -/** Accept a takeover-attribution health report (#3581). Body: - * { agent, state: "ok"|"none"|"threw", refusals, since } — sent by a host - * plugin ONLY on degradation events (first refusal, state change, count - * doubling, give-up latch, recovery); healthy lanes never POST here, so the - * healthy path costs zero round-trips. Latest report wins per agent; the - * payload feeds /__bili/status + /__bili/overview (`attribution` field) and - * the web panel's takeover-broken banner. */ +/** Accept a takeover-attribution health report (#3581, contract reworked by + * #3651). Body: { agent, state: "ok"|"none"|"threw", refusals, since, + * [error], [endpoint] } — under the #3651 owner contract only DIAGNOSABLE + * failures (`threw` episodes: first refusal, state change to threw, count + * doubling, give-up latch) plus recovery notices reach this route; by-design + * agentless lanes never report. `error`/`endpoint` are bounded diagnostics + * that make the panel banner name the failure instead of a generic + * "takeover broken". Latest report wins per agent; the payload feeds + * /__bili/status + /__bili/overview (`attribution` field) and the web panel + * banner. */ export function handlePluginAttribution(payload: string, res: import("node:http").ServerResponse): void { let parsed: unknown; try { @@ -638,7 +641,7 @@ export function handlePluginAttribution(payload: string, res: import("node:http" res.end(JSON.stringify({ ok: false, error: "invalid JSON" })); return; } - const body = parsed as { agent?: unknown; state?: unknown; refusals?: unknown; since?: unknown }; + const body = parsed as { agent?: unknown; state?: unknown; refusals?: unknown; since?: unknown; error?: unknown; endpoint?: unknown }; const str = (v: unknown, max: number) => (typeof v === "string" && v.length > 0 && v.length <= max ? v : undefined); const nonNeg = (v: unknown) => (typeof v === "number" && Number.isFinite(v) && v >= 0 ? Math.floor(v) : undefined); const agent = str(body.agent, 64); @@ -647,7 +650,15 @@ export function handlePluginAttribution(payload: string, res: import("node:http" res.end(JSON.stringify({ ok: false, error: "agent and state (ok|none|threw) are required" })); return; } - recordAttributionReport(agent, { state: body.state, refusals: nonNeg(body.refusals) ?? 0, since: nonNeg(body.since) ?? 0 }); + const error = str(body.error, 300); + const endpoint = str(body.endpoint, 200); + recordAttributionReport(agent, { + state: body.state, + refusals: nonNeg(body.refusals) ?? 0, + since: nonNeg(body.since) ?? 0, + ...(error !== undefined ? { error } : {}), + ...(endpoint !== undefined ? { endpoint } : {}), + }); res.writeHead(200, { "content-type": "application/json" }); res.end(JSON.stringify({ ok: true })); } diff --git a/src/server/attribution-status.ts b/src/server/attribution-status.ts index 32b6bcb46e..32f01e2510 100644 --- a/src/server/attribution-status.ts +++ b/src/server/attribution-status.ts @@ -15,6 +15,10 @@ interface AttributionReport { refusals: number; /** Epoch ms of the first refusal in the current episode; 0 while healthy. */ since: number; + /** #3651: last thrown attribution error text (diagnosable-failure contract; `threw` episodes only). */ + error?: string; + /** #3651: masked endpoint (origin+pathname) of the last refused request. */ + endpoint?: string; } const reports = new Map(); @@ -22,15 +26,28 @@ const reports = new Map(); /** Record seam: latest-report-wins per agent. Called from the * /__bili/plugin/attribution endpoint after validation. */ export function recordAttributionReport(agent: string, report: AttributionReport): void { - reports.set(agent, { state: report.state, refusals: report.refusals, since: report.since }); + reports.set(agent, { + state: report.state, + refusals: report.refusals, + since: report.since, + ...(report.error !== undefined ? { error: report.error } : {}), + ...(report.endpoint !== undefined ? { endpoint: report.endpoint } : {}), + }); } /** Wire payload shared by /__bili/status and /__bili/overview (#2152 parity). - * Empty object until the first report arrives — a healthy lane never reports, - * so absence means "no degradation observed", not "no plugins". */ + * Empty object until the first report arrives — under the #3651 contract a + * healthy or by-design-agentless lane never reports, so absence means "no + * diagnosable failure observed", not "no plugins". */ export function attributionStatusWire(): Record { const out: Record = {}; - for (const [agent, r] of reports) out[agent] = { state: r.state, refusals: r.refusals, since: r.since }; + for (const [agent, r] of reports) out[agent] = { + state: r.state, + refusals: r.refusals, + since: r.since, + ...(r.error !== undefined ? { error: r.error } : {}), + ...(r.endpoint !== undefined ? { endpoint: r.endpoint } : {}), + }; return out; } diff --git a/src/web/client.ts b/src/web/client.ts index 3de7709226..5d6f8906fa 100644 --- a/src/web/client.ts +++ b/src/web/client.ts @@ -750,12 +750,14 @@ export const WEB_CLIENT = `(function () { stale.classList.remove("show"); stale.innerHTML = ""; } - // #3581: takeover-broken notice — a plugin host reports its attribution - // gate degradation event-driven (first refusal / state change / give-up - // latch / recovery), and the proxy keeps the latest per agent. Shown - // only when traffic is actually going direct: at least one agent is - // degraded AND no session is live (live > 0 means compression is still - // serving this host, so the bare "0 running" sub-line is not misleading). + // #3581/#3651: takeover-attribution failure notice — under the #3651 + // owner contract only DIAGNOSABLE failures reach this surface (threw + // episodes report error text + masked endpoint; by-design agentless + // lanes never report), so a visible banner always names what failed. + // Shown only when traffic is actually going direct: at least one agent + // is degraded AND no session is live (live > 0 means compression is + // still serving this host, so the bare "0 running" sub-line is not + // misleading). const attEl = $("attribution-banner"); if (attEl) { const att = d.attribution; @@ -767,9 +769,17 @@ export const WEB_CLIENT = `(function () { const show = degraded.length > 0 && ((d.overview || {}).live || 0) === 0; if (show) { const [agent, entry] = degraded[0]; + const sinceText = entry.since > 0 ? new Date(entry.since).toLocaleString() : "?"; attEl.hidden = false; attEl.classList.add("show"); - attEl.innerHTML = "" + escapeHtml(t("sys.attribution_broken", { agent: agent, n: entry.refusals })) + ""; + attEl.innerHTML = "" + escapeHtml(t("sys.attribution_broken", { agent: agent, n: entry.refusals })) + "" + + (entry.error ? '
' + escapeHtml(t("sys.attribution_error", { err: String(entry.error) })) + "
" : "") + + "
" + escapeHtml(t("sys.attribution_impact", { since: sinceText })) + "
" + + '
' + + '" + + '' + t("sys.report_issue") + "" + + "
"; + attachReportButtons("attribution", attEl); } else { attEl.hidden = true; attEl.classList.remove("show"); diff --git a/src/web/i18n.ts b/src/web/i18n.ts index 4bbcb7c5ba..aad6cc658a 100644 --- a/src/web/i18n.ts +++ b/src/web/i18n.ts @@ -109,7 +109,9 @@ export const MESSAGES: Record> = { "sys.stale": "磁盘已有新版本 v{disk} —— 当前运行 v{running},重启宿主后新代码才生效", "sys.stale_auto": "自动重启已启用,空闲时自动切换", "sys.stale_manual": "自动重启未启用(需要 --auto-restart-on-update)", - "sys.attribution_broken": "接管已失效({agent}):模型请求正在直连(不压缩),已拒 {n} 次 — 重启宿主可恢复", + "sys.attribution_broken": "接管归因异常({agent}):模型请求正在直连(不压缩)— 已直连 {n} 个请求", + "sys.attribution_error": "错误:{err}", + "sys.attribution_impact": "影响:自 {since} 起,该宿主的模型请求未经压缩直发上游。解决方法:重启宿主进程恢复接管;若重启后仍出现,点下方按钮提交诊断。", "sys.policy_title": "自动升级受远端更新策略控制:", "sys.policy_hold": "全局冻结中(hold)— 自动升级暂停;手动升级不受限:bili update", "sys.policy_pinned": "车队钉在 v{version} — 不会自动安装更新的版本;手动逃生:npm install -g billion-context@<版本>", @@ -605,7 +607,9 @@ export const MESSAGES: Record> = { "sys.stale": "Newer version v{disk} on disk — running v{running}; restart the host to load the new code", "sys.stale_auto": "Auto-restart enabled — switches automatically when idle", "sys.stale_manual": "Auto-restart not enabled (needs --auto-restart-on-update)", - "sys.attribution_broken": "Takeover broken ({agent}): model requests are going direct (uncompressed), {n} refused — restart the host to restore compression", + "sys.attribution_broken": "Takeover attribution error ({agent}): model requests are going direct (uncompressed) — {n} request(s) sent uncompressed", + "sys.attribution_error": "Error: {err}", + "sys.attribution_impact": "Impact: since {since}, this host's model requests bypass compression entirely. Remedy: restart the host process to restore takeover; if it persists after restart, submit diagnostics below.", "sys.policy_title": "Auto-upgrades under remote update policy:", "sys.policy_hold": "fleet-wide HOLD active — auto-upgrades frozen; manual still works: bili update", "sys.policy_pinned": "fleet pinned at v{version} — nothing newer will be auto-installed; manual escape: npm install -g billion-context@", diff --git a/tests/dsh-native.test.ts b/tests/dsh-native.test.ts index cf0a34e983..8ca4018fdb 100644 --- a/tests/dsh-native.test.ts +++ b/tests/dsh-native.test.ts @@ -5,7 +5,7 @@ import os from "node:os"; import path from "node:path"; import http from "node:http"; import { pathToFileURL } from "node:url"; -import { apply, planNativeDsh, shouldBootstrapNativeDsh, persistClientEvent, _attributionHealthForTest, _resetAttributionHealthForTest, _registerStateForTest, _resetRegisterForTest, _setModelInfoForTest, _setSpawnForTest, _setThrewRetryForTest, _settleNativeForTest, _stateHeadersForTest, _stateRespawnForTest, _stateTakeoverGateForTest, _stateToolsReadyForTest, _stateBeforeSendForTest, _stateOnResponseForTest, _noteRoutedForTest, _resetRoutedForTest, _resetWebProfileWarningForTest, _resetTitlesForTest, type PluginContext } from "../src/agent/dsh-native.ts"; +import { apply, planNativeDsh, shouldBootstrapNativeDsh, persistClientEvent, _attributionHealthForTest, _resetAttributionHealthForTest, _registerStateForTest, _resetRegisterForTest, _setModelInfoForTest, _setSpawnForTest, _setThrewRetryForTest, _settleNativeForTest, _stateHeadersForTest, _stateRespawnForTest, _stateTakeoverGateForTest, _stateToolsReadyForTest, _stateBeforeSendForTest, _stateOnResponseForTest, _noteRoutedForTest, _resetRoutedForTest, _resetWebProfileWarningForTest, _resetNonWebTraceForTest, _resetTitlesForTest, type PluginContext } from "../src/agent/dsh-native.ts"; import { __resetIncrementalClientForTests } from "../src/agent/incremental-client.ts"; import { BILI_COMPACTION_DISABLE_SERVICE } from "../src/agent/dsh-compaction-disable.ts"; import { once } from "node:events"; @@ -426,7 +426,7 @@ type MockTool = { name: string; description?: string; inputSchema: unknown }; type NameCall = { conversationId: string; name: string }; -type MockAttributionCall = { agent: string; state: string; refusals: number; since: number }; +type MockAttributionCall = { agent: string; state: string; refusals: number; since: number; error?: string; endpoint?: string }; function mockBiliHandler(toolCalls: Array<{ conversationId: string; tool: string; args: unknown; nativeCaller?: boolean }>, statusResponder?: (url: string) => unknown | undefined, nameCalls?: NameCall[], nameStatus?: number, turnEnabled?: boolean, attributionCalls?: MockAttributionCall[], holdManifest?: () => Promise | void): (req: http.IncomingMessage, res: http.ServerResponse) => void { const manifestTools: MockTool[] = [ @@ -509,6 +509,8 @@ function mockBiliHandler(toolCalls: Array<{ conversationId: string; tool: string state: typeof parsed.state === "string" ? parsed.state : "", refusals: typeof parsed.refusals === "number" ? parsed.refusals : -1, since: typeof parsed.since === "number" ? parsed.since : -1, + ...(typeof parsed.error === "string" ? { error: parsed.error } : {}), + ...(typeof parsed.endpoint === "string" ? { endpoint: parsed.endpoint } : {}), }); } res.writeHead(200, { "content-type": "application/json" }); @@ -1084,9 +1086,14 @@ test("#1772 apply(): web-profile compaction caveat warns once in the durable log const home = fs.mkdtempSync(path.join(os.tmpdir(), "bili-dsh-webwarn-")); const stateHome = fs.mkdtempSync(path.join(os.tmpdir(), "bili-dsh-webwarn-state-")); const logFile = path.join(stateHome, "billion-context", "bili.log"); - const warnLines = (): string[] => fs.existsSync(logFile) - ? fs.readFileSync(logFile, "utf8").split("\n").filter((l) => l.includes("[dsh-client]") && l.includes("@deepseek-ai/dsh-web-app")) + // #3651: two durable notes now mention dsh-web-app — the web-profile note + // ("stays OFF in this profile") and the non-web-profile trace ("do not + // include …"). Disambiguate on their unique phrases, not the package name. + const clientLines = (phrase: string): string[] => fs.existsSync(logFile) + ? fs.readFileSync(logFile, "utf8").split("\n").filter((l) => l.includes("[dsh-client]") && l.includes(phrase)) : []; + const warnLines = (): string[] => clientLines("stays OFF in this profile"); + const nonWebTraceLines = (): string[] => clientLines("do not include @deepseek-ai/dsh-web-app"); const webBundles = ["@deepseek-ai/dsh-base", "@deepseek-ai/dsh-web-app", "billion-context"]; // #1791: hermetic fallback seam — a deferred probe must never reach a real // bootstrap child process after teardown; undefined keeps it base-less. @@ -1122,22 +1129,41 @@ test("#1772 apply(): web-profile compaction caveat warns once in the durable log assert.equal(warnLines().length, 1); await _stateToolsReadyForTest(); - // non-web profile → no warning + // non-web profile → no web-profile note, but ONE durable trace that + // the #3354 overlay registered anyway (#3651 observability gap) fs.rmSync(logFile, { force: true }); _resetRegisterForTest(proxy.origin); _resetWebProfileWarningForTest(); + _resetNonWebTraceForTest(); const plain = mockCtx(); plain.setProfileContext({ startedBundles: ["@deepseek-ai/dsh-base", "@deepseek-ai/dsh-headless", "billion-context"] }); apply(plain); assert.equal(warnLines().length, 0); + assert.equal(nonWebTraceLines().length, 1); + assert.match(nonWebTraceLines()[0], /#3354/); + assert.match(nonWebTraceLines()[0], /#2474/); await _stateToolsReadyForTest(); - // host shape without inject but with a direct profileContext field + // once-per-process: a later non-web apply adds no further trace + const plainAgain = mockCtx(); + plainAgain.setProfileContext({ startedBundles: ["@deepseek-ai/dsh-base", "@deepseek-ai/dsh-headless", "billion-context"] }); + _resetRegisterForTest(proxy.origin); + apply(plainAgain); + assert.equal(nonWebTraceLines().length, 1); + await _stateToolsReadyForTest(); + + // host shape without inject but with a direct profileContext field. + // #3806: the web-profile note only prints when the overlay can latch + // (cordis on/reflect present) — supply the minimal hooks so this case + // still exercises the non-inject bundles path against the passive note + // (a hook-less host gets the #3806 ARMED alarm instead; see that test). _resetWebProfileWarningForTest(); const bare: Parameters[0] = { tools: { register: () => undefined }, commands: { register: () => undefined }, agents: {}, + on: () => undefined, + reflect: { provide: () => undefined }, profileContext: { startedBundles: ["@deepseek-ai/dsh-web-app"] }, }; _resetRegisterForTest(proxy.origin); @@ -1152,6 +1178,7 @@ test("#1772 apply(): web-profile compaction caveat warns once in the durable log rmrf(stateHome); _resetRegisterForTest(undefined); _resetWebProfileWarningForTest(); + _resetNonWebTraceForTest(); } }); @@ -1860,7 +1887,9 @@ test("#1158 apply() gate refusal logs each endpoint once per process with attrib assert.equal(gate(url), false, "second refusal of the same endpoint"); let lines = refusedLines(); assert.equal(lines.length, 1, `expected one refusal line per endpoint, got: ${lines.join(" | ")}`); - assert.match(lines[0]!, /api\.gate-probe\.test\/v1\/chat\/completions/); + // #3651 paste-safe sink: non-public hosts are masked in the line; the dedup key + // still uses the full URL, which the count assertions below verify. + assert.match(lines[0]!, /https:\/\/\/v1\/chat\/completions/); assert.match(lines[0]!, /no active initiator attribution/); // A query string neither spawns a new line nor leaks into it. assert.equal(gate(`${url}?key=sk-do-not-log`), false); @@ -1948,7 +1977,8 @@ test("#1158 L2 gate three-state: thrown attribution is a distinct state; counts assert.equal(refusedLines().length, 3); // the durable copy carries the same lines into bili.log, [dsh-client]-marked const content = fs.readFileSync(logFile, "utf8"); - assert.match(content, /\[warn\] \[v=[^\]]+\] \[dsh-client\] bili-native-dsh: model request sent DIRECT \(uncompressed\) — takeover gate refused https:\/\/api\.gate-l2\.test\/v1\/chat\/completions: currentInitiator\(\) threw \(agent initiator scope is disposed\) — agent scope disposed\/closing mid-request\? — refusals so far: 5 \(state none→threw\)$/m); + // #3651: the durable line masks non-public upstream hosts (paste-safe sink), loopback stays verbatim. + assert.match(content, /\[warn\] \[v=[^\]]+\] \[dsh-client\] bili-native-dsh: model request sent DIRECT \(uncompressed\) — takeover gate refused https:\/\/\/v1\/chat\/completions: currentInitiator\(\) threw \(agent initiator scope is disposed\) — agent scope disposed\/closing mid-request\? — refusals so far: 5 \(state none→threw\)$/m); assert.equal(errs.filter((e) => e.includes("takeover gate refused")).length, 0, "#2831: must not reach the terminal surface"); } finally { console.error = origErr; @@ -1989,7 +2019,8 @@ test("#2924 gate `threw` attribution: bounded settle-retry recovers and CLAIMS t assert.equal(await gate("https://api.gate-2924a.test/v1/chat/completions"), true, "recovered attribution claims the request"); assert.equal(throws, 3, "initial throw + exactly two settle-retries"); const content = fs.existsSync(logFile) ? fs.readFileSync(logFile, "utf8") : ""; - assert.match(content, /attribution recovered after 2 settle-retries — https:\/\/api\.gate-2924a\.test\/v1\/chat\/completions claimed \(#2924\)/); + // #3651: non-public upstream hosts are masked in the durable line (paste-safe sink). + assert.match(content, /attribution recovered after 2 settle-retries — https:\/\/\/v1\/chat\/completions claimed \(#2924\)/); }); } finally { _setThrewRetryForTest(); @@ -2235,8 +2266,8 @@ test("#3578 transition contract: disposed-scope refusal lands on the REAL /__bil await waitFor(() => ctx.registeredTools.length >= 1, "real-manifest tool registration"); const gate = _stateTakeoverGateForTest(); assert.ok(gate !== undefined, "takeoverGate installed"); - const statusOf = async (): Promise> => - ((await (await fetch(`${origin}/__bili/status`)).json()) as { attribution?: Record }).attribution ?? {}; + const statusOf = async (): Promise> => + ((await (await fetch(`${origin}/__bili/status`)).json()) as { attribution?: Record }).attribution ?? {}; assert.deepEqual(await statusOf(), {}, "no report yet: empty surface"); // #3578 shape: the ALS boundary throws for the host's whole lifetime. ctx.agents.currentInitiator = () => { throw new Error("agent initiator scope is disposed"); }; @@ -2244,7 +2275,7 @@ test("#3578 transition contract: disposed-scope refusal lands on the REAL /__bil // The fire-and-forget report must survive the real endpoint's // validation and land on the status wire. const deadline = Date.now() + 5_000; - let ep: { state: string; refusals: number; since: number } | undefined; + let ep: { state: string; refusals: number; since: number; error?: string; endpoint?: string } | undefined; for (;;) { ep = (await statusOf())["bili-native"]; if (ep !== undefined && ep.state === "threw" && ep.refusals >= 1 && ep.since > 0) break; @@ -2252,6 +2283,11 @@ test("#3578 transition contract: disposed-scope refusal lands on the REAL /__bil await new Promise((r) => setTimeout(r, 25)); } assert.ok(ep.since > 0, "episode start timestamp recorded"); + // #3651 diagnosable-failure contract: the thrown message rides the + // wire, and the endpoint is masked BUT loopback hosts stay + // verbatim (this one points at bili itself, not a third party). + assert.equal(ep.error, "agent initiator scope is disposed"); + assert.match(ep.endpoint ?? "", /^http:\/\/127\.0\.0\.1:\d+\/v1\/messages$/); }); }); } finally { @@ -2267,6 +2303,61 @@ test("#3578 transition contract: disposed-scope refusal lands on the REAL /__bil } }); +test("#3651 owner contract: agentless `none` lanes NEVER report; none→threw flip reports immediately with diagnostics", async () => { + const attrCalls: MockAttributionCall[] = []; + const proxy = await startMockProxy([], undefined, undefined, undefined, false, attrCalls); + const stateHome = fs.mkdtempSync(path.join(os.tmpdir(), "bili-dsh-3651a-state-")); + const home = fs.mkdtempSync(path.join(os.tmpdir(), "bili-dsh-3651a-home-")); + try { + await withEnv({ DSH_HOME: home, BILLION_CONTEXT_PROXY: proxy.origin, XDG_STATE_HOME: stateHome }, async () => { + _resetRegisterForTest(proxy.origin); + _resetAttributionHealthForTest(); + const ctx = mockCtx(); + apply(ctx); + await waitFor(() => ctx.registeredTools.length === 1, "initial attach tool registration"); + const gate = _stateTakeoverGateForTest(); + assert.ok(gate !== undefined, "takeoverGate installed"); + // Field shape (#3651): currentInitiator() returns nothing — a by-design + // agentless sidecar lane (dsh title-gen et al.). Such refusals are + // NORMAL interceptions: they stay direct and never reach the panel + // (「正常拦截不报告」) — no matter how many accumulate. + ctx.agents.currentInitiator = () => undefined; + const url = "https://api.gate-3651a.test/v1/chat/completions"; + for (let i = 0; i < 6; i++) assert.equal(await gate(url), false, "agentless lane still refused direct"); + assert.deepEqual(_attributionHealthForTest(), { state: "ok", refusals: 0, since: 0, threwStreak: 0, gaveUp: false }, "`none` episodes touch no reported state"); + assert.equal(attrCalls.length, 0, "six agentless refusals report nothing — banner cannot light on them"); + // the durable per-endpoint line still fires (diagnostic record), masked + const content = fs.readFileSync(path.join(stateHome, "billion-context", "bili.log"), "utf8"); + assert.match(content, /takeover gate refused https:\/\/\/v1\/chat\/completions: no active initiator attribution/); + // 「报告必须可诊断」: flipping to an internal error (`threw`) must + // report IMMEDIATELY with the thrown message + masked endpoint. + _setThrewRetryForTest({ attempts: 2, delayMs: 1 }); + ctx.agents.currentInitiator = () => { throw new Error("agent initiator scope is disposed"); }; + assert.equal(await gate(url), false, "thrown attribution still refuses direct"); + await waitFor(() => attrCalls.length === 1, "threw flip reports immediately (no cadence wait)"); + assert.equal(attrCalls[0]!.state, "threw"); + assert.equal(attrCalls[0]!.refusals, 1, "fresh episode starts at one"); + assert.ok(attrCalls[0]!.since > 0, "episode start recorded"); + assert.equal(attrCalls[0]!.error, "agent initiator scope is disposed"); + assert.equal(attrCalls[0]!.endpoint, "https:///v1/chat/completions"); + // attributed traffic heals: recovery notice + counters reset + ctx.agents.currentInitiator = () => ({ session: { id: "s3651a" } }); + assert.equal(await gate(url), true); + await waitFor(() => attrCalls.some((c) => c.state === "ok"), "recovery report"); + const okCall = attrCalls.filter((c) => c.state === "ok").pop()!; + assert.equal(okCall.refusals, 0); + assert.equal(_attributionHealthForTest().refusals, 0, "recovery resets the episode"); + }); + } finally { + _setThrewRetryForTest(); + proxy.close(); + rmrf(stateHome); + rmrf(home); + _resetRegisterForTest(undefined); + _resetAttributionHealthForTest(); + } +}); + test("#1158 apply() persists bootstrap failures to bili.log (GUI stderr is invisible)", async () => { const stateHome = fs.mkdtempSync(path.join(os.tmpdir(), "bili-dsh-1158c-state-")); const home = fs.mkdtempSync(path.join(os.tmpdir(), "bili-dsh-1158c-home-")); @@ -2311,9 +2402,13 @@ test("#1158 persistClientEvent: standard line shape into bili.log; broken fs nev try { await withEnv({ XDG_STATE_HOME: stateHome }, async () => { persistClientEvent("boom-marker-xyz"); + // #3651: paste-safe sink — non-public upstream hosts masked (query + // dropped), public API hosts and loopback origins stay verbatim. + persistClientEvent("probe https://relay.internal.example/v1/chat/completions?k=secret http://127.0.0.1:18788/bili/x https://api.deepseek.com/v1/messages"); const logFile = path.join(stateHome, "billion-context", "bili.log"); const content = fs.readFileSync(logFile, "utf8"); assert.match(content, /^\d{4}-\d{2}-\d{2}T[\d:.]+Z \[warn\] \[v=[^\]]+\] \[dsh-client\] boom-marker-xyz$/m); + assert.match(content, /probe https:\/\/\/v1\/chat\/completions http:\/\/127\.0\.0\.1:18788\/bili\/x https:\/\/api\.deepseek\.com\/v1\/messages$/m); }); // Broken target (state root under a regular file): must swallow, never throw. const blocker = path.join(os.tmpdir(), `bili-dsh-1158d-blocker-${Date.now()}`); diff --git a/tests/log-mask.test.ts b/tests/log-mask.test.ts index 518ff8ece0..d8ff08cbfb 100644 --- a/tests/log-mask.test.ts +++ b/tests/log-mask.test.ts @@ -22,7 +22,9 @@ import { maskHostInText, maskHostPortForLog, maskIpsInText, + maskUrlForDiagnostics, maskUrlForLog, + maskUrlsForDiagnostics, maskUrlsInText, redactSecretsInText, setMaskHostsEnabled, @@ -77,6 +79,23 @@ test("maskUrlsInText: masks URLs embedded in arbitrary strings", () => { assert.equal(maskUrlsInText("no urls here"), "no urls here"); }); +test("maskUrlForDiagnostics: loopback exempt (identifies bili's own proxy), non-public still masked (#3651)", () => { + assert.equal(maskUrlForDiagnostics("http://127.0.0.1:18787/v1/messages"), "http://127.0.0.1:18787/v1/messages"); + assert.equal(maskUrlForDiagnostics("http://localhost:9090/acp"), "http://localhost:9090/acp"); + assert.equal(maskUrlForDiagnostics("http://[::1]:9090/acp"), "http://[::1]:9090/acp"); + assert.equal(maskUrlForDiagnostics("https://relay.internal:8443/v1/chat/completions"), "https://:8443/v1/chat/completions"); + assert.equal(maskUrlForDiagnostics("https://api.anthropic.com/v1/messages"), "https://api.anthropic.com/v1/messages"); + assert.equal(maskUrlForDiagnostics("not a url"), ""); +}); + +test("maskUrlsForDiagnostics: text-level twin — loopback URLs kept, others masked, junk passes through (#3651)", () => { + assert.equal( + maskUrlsForDiagnostics("probe https://relay.internal.example/v1/chat/completions http://127.0.0.1:18788/bili/x https://api.deepseek.com/v1/messages"), + "probe https:///v1/chat/completions http://127.0.0.1:18788/bili/x https://api.deepseek.com/v1/messages", + ); + assert.equal(maskUrlsForDiagnostics("junk http:// trailing stays"), "junk http:// trailing stays"); +}); + test("maskHostPortForLog: CONNECT targets", () => { assert.equal(maskHostPortForLog("relay.internal:8443"), ":8443"); assert.equal(maskHostPortForLog("api.anthropic.com:443"), "api.anthropic.com:443");