From b9e67c26dc4247b225567f7ffc0eab1a83d36460 Mon Sep 17 00:00:00 2001 From: awork Date: Sun, 18 Oct 2026 13:10:14 +0800 Subject: [PATCH 1/8] fix: mask non-public upstream hosts in host-side log events (#3651) In-passing fix found while working #3651: the paste-safe sink only existed on the proxy side. Host-side event lines ([dsh-client]/[native-client] persistClientEvent) were written verbatim, so a user pasting bili.log into a public issue leaked real non-public upstream hostnames (opencode.ai, zcode.z.ai) even though the proxy-side lines showed . logHostEvent now applies maskUrlsLoopbackSafe(maskIpsInText(redactSecretsInText(...))) with loopback hosts exempt so attach-target/pinned-origin diagnostics keep identifying bili itself. --- src/agent/host-log.ts | 28 +++++++++++++++++++++++++++- src/log-mask.ts | 4 ++-- tests/dsh-native.test.ts | 14 +++++++++++--- 3 files changed, 40 insertions(+), 6 deletions(-) diff --git a/src/agent/host-log.ts b/src/agent/host-log.ts index 8afed8ddce..bca75cd727 100644 --- a/src/agent/host-log.ts +++ b/src/agent/host-log.ts @@ -18,14 +18,40 @@ // can't strand these writes on a renamed inode. import { appendFileSync, mkdirSync } from "node:fs"; import path from "node:path"; +import { isLoopbackIp, maskIpsInText, maskUrlForLog, redactSecretsInText } from "../log-mask.js"; import { defaultLogFile } from "../paths.js"; import { VERSION } from "../version.js"; +// #3651: URL masking for host-side events with a LOOPBACK EXEMPTION — the +// public/private rule of maskUrlForLog would otherwise collapse bili's own +// local origins (attach targets, pinned proxy URLs) to , and +// those identify bili itself, not an external party (same exemption logic as +// maskIpsInText's loopback carve-out). +function maskUrlsLoopbackSafe(text: string): string { + return text.replace(/https?:\/\/[^\s"'<>]+/gi, (m) => { + try { + const u = new URL(m); + const h = u.hostname.replace(/^\[|\]$/g, "").toLowerCase(); + if (h === "localhost" || isLoopbackIp(h)) return m; + return maskUrlForLog(m); + } catch { + return m; + } + }); +} + export function logHostEvent(level: "info" | "warn" | "error", origin: string, msg: string): void { 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 = maskUrlsLoopbackSafe(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/log-mask.ts b/src/log-mask.ts index d34a619b1b..fba0b75072 100644 --- a/src/log-mask.ts +++ b/src/log-mask.ts @@ -188,8 +188,8 @@ function isIpv4Literal(s: string): boolean { return parts.length === 4 && parts.every((p) => p.length >= 1 && p.length <= 3 && /^\d+$/.test(p) && Number(p) <= 255 && !(p.length > 1 && p[0] === "0")); } -function isLoopbackIp(ip: string): boolean { - return ip === "::1" || ip === "0:0:0:0:0:0:0:1" || ip === "0.0.0.0" || ip.startsWith("127."); +export function isLoopbackIp(ip: string): boolean { + return ip === "::1" || ip === "0:0:0:0" || ip === "0.0.0.0" || ip.startsWith("127."); } /** diff --git a/tests/dsh-native.test.ts b/tests/dsh-native.test.ts index cf0a34e983..831e0e38c7 100644 --- a/tests/dsh-native.test.ts +++ b/tests/dsh-native.test.ts @@ -1860,7 +1860,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 +1950,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 +1992,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(); @@ -2311,9 +2315,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()}`); From ec646b310f7545cf5e0f44296638ea96c657e8e7 Mon Sep 17 00:00:00 2001 From: awork Date: Sun, 18 Oct 2026 13:10:31 +0800 Subject: [PATCH 2/8] fix: agentless-lane attribution refusals stay silent until an escalation floor (#3651) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit The #3581 takeover-broken banner fired on the FIRST none-state refusal, but by-design agentless lanes (dsh's title-gen sidecar et al.) refuse a handful of small one-shot calls per host lifetime and can never recover — field shape from #3651: exactly one refused sidecar call per short-lived worker boot while the main conversation compressed fine, banner lit on every boot, 'restart the host' advice could not help. none episodes now escalate only past ATTR_NONE_ESCALATION_FLOOR (3 cumulative refusals), then keep the doubling cadence; threw keeps its original #3581a contract (first refusal immediate + doubling) since an exception in the attribution lookup is internal error, never legitimate agentless evidence. A genuinely broken takeover refuses every model call, so any real episode crosses the floor within the first user turn. --- README.md | 2 +- README.zh-CN.md | 2 +- src/agent/dsh-native.ts | 30 +++++++++++++++++++++--- tests/dsh-native.test.ts | 49 ++++++++++++++++++++++++++++++++++++++++ 4 files changed, 78 insertions(+), 5 deletions(-) diff --git a/README.md b/README.md index fda4fab72d..5ca5b55322 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 and do not raise the banner on their own (#3651) — it fires only when unattributed refusals accumulate past a small escalation floor (a broken takeover refuses every model call and crosses it within the first user turn) or when the attribution lookup itself throws. **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..d8a97743bc 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)——只有无归属拒绝累计越过一个小的升级阈值(真正失效的接管会拒绝每一次模型调用,首轮对话内即越过),或归属查询本身抛错时,横幅才会出现。 **自 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..212f9e3465 100644 --- a/src/agent/dsh-native.ts +++ b/src/agent/dsh-native.ts @@ -1539,7 +1539,7 @@ export function apply(ctx: PluginContext): void { }; // #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 + // event reports (floor crossing / 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. const reportAttrHealth = (): void => { @@ -1547,17 +1547,33 @@ export function apply(ctx: PluginContext): void { if (base === undefined) return; void reportAttributionHealth(base, { agent: name, state: attrHealth.state, refusals: attrHealth.refusals, since: attrHealth.since }).catch(() => undefined); }; + // #3651: `none` episodes stay silent until their cumulative refusals reach + // ATTR_NONE_ESCALATION_FLOOR. 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 its FIRST refusal 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). A genuinely broken takeover refuses EVERY model call, + // so any real episode crosses the floor within the first user turn. + // `threw` keeps the original #3581a cadence (first refusal immediate, then + // count doubling): an exception inside the host's attribution lookup is an + // internal error, never legitimate agentless evidence (#1158 L2), so it + // never waits for the floor either. const noteAttrRefusal = (state: "none" | "threw"): void => { if (attrHealth.state === "ok") { attrHealth = { state, refusals: 1, since: Date.now() }; attrReportedRefusals = 1; - reportAttrHealth(); + if (state === "threw") reportAttrHealth(); return; } attrHealth.refusals += 1; const changed = attrHealth.state !== state; attrHealth.state = state; - if (changed || attrHealth.refusals >= attrReportedRefusals * 2) { + const doubled = attrHealth.refusals >= attrReportedRefusals * 2; + const escalated = (changed && state === "threw") + || (doubled && (state === "threw" || attrHealth.refusals >= ATTR_NONE_ESCALATION_FLOOR)); + if (escalated) { attrReportedRefusals = attrHealth.refusals; reportAttrHealth(); } @@ -2187,6 +2203,14 @@ export function _setThrewRetryForTest(cfg?: Partial) threwRetry = { ...DEFAULT_THREW_RETRY, ...cfg }; } +// #3651: first-report floor for `none` attribution episodes (see +// noteAttrRefusal). By-design agentless lanes refuse only a handful of small +// one-shot calls per host lifetime and can never recover, so their first +// refusal must not light the #3581 "takeover broken" banner; a genuinely +// broken takeover refuses every model call and crosses this floor within the +// first user turn. `threw` bypasses the floor entirely (internal error). +const ATTR_NONE_ESCALATION_FLOOR = 3; + // #3581: attribution-health counters backing the /__bili status surface. // Module-level like register/webProfileWarned (one live injection per host // process); _resetAttributionHealthForTest isolates test scenarios. diff --git a/tests/dsh-native.test.ts b/tests/dsh-native.test.ts index 831e0e38c7..e7a5f0b1de 100644 --- a/tests/dsh-native.test.ts +++ b/tests/dsh-native.test.ts @@ -2271,6 +2271,55 @@ test("#3578 transition contract: disposed-scope refusal lands on the REAL /__bil } }); +test("#3651 agentless `none` lane: sub-floor refusals stay silent; floor crossing reports once", 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.). One or two such + // refusals per short-lived worker must NOT light the #3581 banner. + ctx.agents.currentInitiator = () => undefined; + const url = "https://api.gate-3651a.test/v1/chat/completions"; + assert.equal(await gate(url), false); + assert.equal(await gate(url), false); + const sub = _attributionHealthForTest(); + assert.deepEqual({ state: sub.state, refusals: sub.refusals }, { state: "none", refusals: 2 }); + assert.equal(attrCalls.length, 0, "sub-floor `none` episode reports nothing"); + assert.equal(await gate(url), false, "floor crossing still refuses direct"); + await waitFor(() => attrCalls.length === 1, "floor-crossing report"); + assert.deepEqual({ state: attrCalls[0]!.state, refusals: attrCalls[0]!.refusals }, { state: "none", refusals: 3 }); + assert.ok(attrCalls[0]!.since > 0, "episode start recorded from the FIRST refusal"); + // further none-refusals keep the doubling cadence (6 next) + for (let i = 0; i < 3; i++) assert.equal(await gate(url), false); + await waitFor(() => attrCalls.length === 2, "doubling report after the floor"); + assert.equal(attrCalls[1]!.refusals, 6); + // 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/); + // attributed traffic heals before any further escalation + ctx.agents.currentInitiator = () => ({ session: { id: "s3651a" } }); + assert.equal(await gate(url), true); + assert.equal(_attributionHealthForTest().refusals, 0, "recovery resets the episode"); + }); + } finally { + 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-")); From 4eb443899374ad78d2652751145187699d588a7a Mon Sep 17 00:00:00 2001 From: awork Date: Sun, 18 Oct 2026 13:25:12 +0800 Subject: [PATCH 3/8] fix(log-mask): restore master's expanded IPv6 loopback literal (review finding on #3653) The paste-safe-sink commit swapped the fully-expanded ::1 literal (0:0:0:0:0:0:0:1) for 0:0:0:0 (fully-expanded ::), silently flipping two free-text edge cases vs master with no stated reason: expanded ::1 in prose would now be masked, expanded :: exempted. Restoring the original literal keeps this PR's log-mask change purely additive (export only). --- src/log-mask.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/log-mask.ts b/src/log-mask.ts index fba0b75072..2a5cef4591 100644 --- a/src/log-mask.ts +++ b/src/log-mask.ts @@ -189,7 +189,7 @@ function isIpv4Literal(s: string): boolean { } export function isLoopbackIp(ip: string): boolean { - return ip === "::1" || ip === "0:0:0:0" || ip === "0.0.0.0" || ip.startsWith("127."); + return ip === "::1" || ip === "0:0:0:0:0:0:0:1" || ip === "0.0.0.0" || ip.startsWith("127."); } /** From a1d0404b62660fa2014a971c8714061f3cd3eac6 Mon Sep 17 00:00:00 2001 From: awork Date: Sun, 18 Oct 2026 13:25:12 +0800 Subject: [PATCH 4/8] =?UTF-8?q?test(dsh-native):=20pin=20the=20none?= =?UTF-8?q?=E2=86=92threw=20immediate-report=20invariant=20(review=20findi?= =?UTF-8?q?ng=20on=20#3653)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit The new floor test covered pure-none episodes only. If a later edit drops the (changed && state === threw) clause, a silent sub-floor none episode flipping to an internal error would stay silent until doubling — exactly the #3581a contract this PR claims to preserve. Extended the #3651 test: sub-floor none x2 → threw must report immediately ({state:threw, refusals:7}). --- tests/dsh-native.test.ts | 11 ++++++++++- 1 file changed, 10 insertions(+), 1 deletion(-) diff --git a/tests/dsh-native.test.ts b/tests/dsh-native.test.ts index e7a5f0b1de..9c56216d53 100644 --- a/tests/dsh-native.test.ts +++ b/tests/dsh-native.test.ts @@ -2271,7 +2271,7 @@ test("#3578 transition contract: disposed-scope refusal lands on the REAL /__bil } }); -test("#3651 agentless `none` lane: sub-floor refusals stay silent; floor crossing reports once", async () => { +test("#3651 agentless `none` lane: sub-floor refusals stay silent; floor crossing reports once; none→threw flip reports immediately", 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-")); @@ -2306,12 +2306,21 @@ test("#3651 agentless `none` lane: sub-floor refusals stay silent; floor crossin // 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/); + // mixed episode: flipping a silent `none` episode to `threw` must + // report IMMEDIATELY — an internal error never waits for the floor + // (#3581a contract holds across state transitions) + _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 === 3, "none→threw transition reports immediately"); + assert.deepEqual({ state: attrCalls[2]!.state, refusals: attrCalls[2]!.refusals }, { state: "threw", refusals: 7 }); // attributed traffic heals before any further escalation ctx.agents.currentInitiator = () => ({ session: { id: "s3651a" } }); assert.equal(await gate(url), true); assert.equal(_attributionHealthForTest().refusals, 0, "recovery resets the episode"); }); } finally { + _setThrewRetryForTest(); proxy.close(); rmrf(stateHome); rmrf(home); From a11b135e3e7aa8af458f6cdb2340f00127f990c1 Mon Sep 17 00:00:00 2001 From: awork Date: Sun, 18 Oct 2026 13:45:19 +0800 Subject: [PATCH 5/8] fix: leave a durable trace when the #3354 overlay registers in a non-web-app profile (#3651) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit The web-profile note was the only confirmation that the load-time compaction-disable overlay took effect, and it is scoped to profiles bundling @deepseek-ai/dsh-web-app — every other profile boots silently even though registerCompactionDisable runs unconditionally. Field triage (#3651) could not tell "overlay registered" from "profile shape unconfirmed" because of this gap. One once-per-process durable client event now records the non-web-app case (with the #2474 fallback pointer and the #2028 allowDshCompaction interplay noted). Closes nothing by itself — third labeled commit of the #3651 PR. --- src/agent/dsh-native.ts | 20 ++++++++++++++++++++ tests/dsh-native.test.ts | 27 +++++++++++++++++++++++---- 2 files changed, 43 insertions(+), 4 deletions(-) diff --git a/src/agent/dsh-native.ts b/src/agent/dsh-native.ts index 212f9e3465..bc855cf166 100644 --- a/src/agent/dsh-native.ts +++ b/src/agent/dsh-native.ts @@ -256,6 +256,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; @@ -2047,6 +2053,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"); } } @@ -2186,6 +2201,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; diff --git a/tests/dsh-native.test.ts b/tests/dsh-native.test.ts index 9c56216d53..ead4cc2bb5 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"; @@ -1084,9 +1084,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,14 +1127,27 @@ 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(); + + // 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 @@ -1152,6 +1170,7 @@ test("#1772 apply(): web-profile compaction caveat warns once in the durable log rmrf(stateHome); _resetRegisterForTest(undefined); _resetWebProfileWarningForTest(); + _resetNonWebTraceForTest(); } }); From 33a62b9b28d93085bfc4cfa079a86f87e4e9e9c2 Mon Sep 17 00:00:00 2001 From: awork Date: Sun, 18 Oct 2026 15:03:19 +0800 Subject: [PATCH 6/8] fix: attribution reporting follows the diagnosable-failure contract (#3651) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Owner contract from the #3651 thread: normal interceptions must stay silent; a report only exists for something broken, diagnosable, and actionable. - agentless 'none' refusals (by-design sidecar/agentless lanes) NEVER call reportAttributionHealth anymore — the interim escalation floor is dropped; they keep their durable per-endpoint masked log line and local counters (recovery still resets them). They can no longer raise the banner. - 'threw' reports now carry diagnostics: the error text (<=300 chars), the masked endpoint (<=200 chars, loopback origins verbatim), first-seen time and count — end to end (plugin -> admin handler -> status store -> wire). - The banner is reworked around that payload: what failed (error line), impact scope (since when, how many requests went direct), concrete remedy (restart the host), and copy-diagnostics / submit-diagnostics buttons (prefilled GitHub issue via the existing /__bili/diagnostics endpoint). - Loopback exemption moved into shared maskUrlForDiagnostics/maskUrlsForDiagnostics (verbatim, not re-serialized: URL.pathname would append '/' to pathless URLs and drift byte-pinned lines); host-log events use it too. --- README.md | 2 +- README.zh-CN.md | 2 +- src/agent/dsh-native.ts | 101 ++++++++++++++++++------------- src/agent/host-log.ts | 22 +------ src/agent/shared.ts | 23 ++++--- src/log-mask.ts | 36 +++++++++++ src/plugin.ts | 29 ++++++--- src/server/attribution-status.ts | 25 ++++++-- src/web/client.ts | 24 +++++--- src/web/i18n.ts | 8 ++- tests/dsh-native.test.ts | 56 +++++++++-------- tests/log-mask.test.ts | 19 ++++++ 12 files changed, 227 insertions(+), 120 deletions(-) diff --git a/README.md b/README.md index 5ca5b55322..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). 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 and do not raise the banner on their own (#3651) — it fires only when unattributed refusals accumulate past a small escalation floor (a broken takeover refuses every model call and crosses it within the first user turn) or when the attribution lookup itself throws. +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 d8a97743bc..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 已安装版本)。若注入代码在运行期损坏(如 agent scope 被销毁),模型流量会静默直连(不压缩),直到宿主重启;自 #3581 起该状态可见:无会话运行时插件上报归属闸门降级,Web UI 显示「接管已失效」横幅,`GET /__bili/status` / `/__bili/overview` 暴露每代理的 `attribution` 状态。**按设计无归属**的后台车道(如 dsh 标题生成 sidecar)本身不携带 initiator,其小体量一次性请求按定义直连、不会单独触发横幅(#3651)——只有无归属拒绝累计越过一个小的升级阈值(真正失效的接管会拒绝每一次模型调用,首轮对话内即越过),或归属查询本身抛错时,横幅才会出现。 +**插件模式**(`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 bc855cf166..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"; @@ -1543,43 +1544,58 @@ 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 (floor crossing / 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); }; - // #3651: `none` episodes stay silent until their cumulative refusals reach - // ATTR_NONE_ESCALATION_FLOOR. 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 its FIRST refusal 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). A genuinely broken takeover refuses EVERY model call, - // so any real episode crosses the floor within the first user turn. - // `threw` keeps the original #3581a cadence (first refusal immediate, then - // count doubling): an exception inside the host's attribution lookup is an - // internal error, never legitimate agentless evidence (#1158 L2), so it - // never waits for the floor either. - 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; - if (state === "threw") reportAttrHealth(); + reportAttrHealth(); return; } attrHealth.refusals += 1; const changed = attrHealth.state !== state; attrHealth.state = state; const doubled = attrHealth.refusals >= attrReportedRefusals * 2; - const escalated = (changed && state === "threw") - || (doubled && (state === "threw" || attrHealth.refusals >= ATTR_NONE_ESCALATION_FLOOR)); - if (escalated) { + if (changed || doubled) { attrReportedRefusals = attrHealth.refusals; reportAttrHealth(); } @@ -1608,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 @@ -1619,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 @@ -1652,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)); @@ -1666,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; @@ -2223,25 +2244,19 @@ export function _setThrewRetryForTest(cfg?: Partial) threwRetry = { ...DEFAULT_THREW_RETRY, ...cfg }; } -// #3651: first-report floor for `none` attribution episodes (see -// noteAttrRefusal). By-design agentless lanes refuse only a handful of small -// one-shot calls per host lifetime and can never recover, so their first -// refusal must not light the #3581 "takeover broken" banner; a genuinely -// broken takeover refuses every model call and crosses this floor within the -// first user turn. `threw` bypasses the floor entirely (internal error). -const ATTR_NONE_ESCALATION_FLOOR = 3; - -// #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 bca75cd727..a0cd325b86 100644 --- a/src/agent/host-log.ts +++ b/src/agent/host-log.ts @@ -18,28 +18,10 @@ // can't strand these writes on a renamed inode. import { appendFileSync, mkdirSync } from "node:fs"; import path from "node:path"; -import { isLoopbackIp, maskIpsInText, maskUrlForLog, redactSecretsInText } from "../log-mask.js"; +import { maskIpsInText, maskUrlsForDiagnostics, redactSecretsInText } from "../log-mask.js"; import { defaultLogFile } from "../paths.js"; import { VERSION } from "../version.js"; -// #3651: URL masking for host-side events with a LOOPBACK EXEMPTION — the -// public/private rule of maskUrlForLog would otherwise collapse bili's own -// local origins (attach targets, pinned proxy URLs) to , and -// those identify bili itself, not an external party (same exemption logic as -// maskIpsInText's loopback carve-out). -function maskUrlsLoopbackSafe(text: string): string { - return text.replace(/https?:\/\/[^\s"'<>]+/gi, (m) => { - try { - const u = new URL(m); - const h = u.hostname.replace(/^\[|\]$/g, "").toLowerCase(); - if (h === "localhost" || isLoopbackIp(h)) return m; - return maskUrlForLog(m); - } catch { - return m; - } - }); -} - export function logHostEvent(level: "info" | "warn" | "error", origin: string, msg: string): void { try { const file = defaultLogFile(); @@ -50,7 +32,7 @@ export function logHostEvent(level: "info" | "warn" | "error", origin: string, m // 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 = maskUrlsLoopbackSafe(maskIpsInText(redactSecretsInText(msg))); + 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 2a5cef4591..d601df9919 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 ead4cc2bb5..24715c8bc2 100644 --- a/tests/dsh-native.test.ts +++ b/tests/dsh-native.test.ts @@ -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" }); @@ -2258,8 +2260,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"); }; @@ -2267,7 +2269,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; @@ -2275,6 +2277,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 { @@ -2290,7 +2297,7 @@ test("#3578 transition contract: disposed-scope refusal lands on the REAL /__bil } }); -test("#3651 agentless `none` lane: sub-floor refusals stay silent; floor crossing reports once; none→threw flip reports immediately", async () => { +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-")); @@ -2305,37 +2312,34 @@ test("#3651 agentless `none` lane: sub-floor refusals stay silent; floor crossin 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.). One or two such - // refusals per short-lived worker must NOT light the #3581 banner. + // 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"; - assert.equal(await gate(url), false); - assert.equal(await gate(url), false); - const sub = _attributionHealthForTest(); - assert.deepEqual({ state: sub.state, refusals: sub.refusals }, { state: "none", refusals: 2 }); - assert.equal(attrCalls.length, 0, "sub-floor `none` episode reports nothing"); - assert.equal(await gate(url), false, "floor crossing still refuses direct"); - await waitFor(() => attrCalls.length === 1, "floor-crossing report"); - assert.deepEqual({ state: attrCalls[0]!.state, refusals: attrCalls[0]!.refusals }, { state: "none", refusals: 3 }); - assert.ok(attrCalls[0]!.since > 0, "episode start recorded from the FIRST refusal"); - // further none-refusals keep the doubling cadence (6 next) - for (let i = 0; i < 3; i++) assert.equal(await gate(url), false); - await waitFor(() => attrCalls.length === 2, "doubling report after the floor"); - assert.equal(attrCalls[1]!.refusals, 6); + 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/); - // mixed episode: flipping a silent `none` episode to `threw` must - // report IMMEDIATELY — an internal error never waits for the floor - // (#3581a contract holds across state transitions) + // 「报告必须可诊断」: 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 === 3, "none→threw transition reports immediately"); - assert.deepEqual({ state: attrCalls[2]!.state, refusals: attrCalls[2]!.refusals }, { state: "threw", refusals: 7 }); - // attributed traffic heals before any further escalation + 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 { 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"); From c024699e95f3c40bc2e7d599a0dfaab5e6249239 Mon Sep 17 00:00:00 2001 From: awork Date: Sun, 18 Oct 2026 15:07:59 +0800 Subject: [PATCH 7/8] fix(log-mask): un-export isLoopbackIp (unused-exports gate) Internal-only after the loopback exemption moved into maskUrlForDiagnostics; no external importer remains. --- src/log-mask.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/log-mask.ts b/src/log-mask.ts index d601df9919..4792109b1f 100644 --- a/src/log-mask.ts +++ b/src/log-mask.ts @@ -224,7 +224,7 @@ function isIpv4Literal(s: string): boolean { return parts.length === 4 && parts.every((p) => p.length >= 1 && p.length <= 3 && /^\d+$/.test(p) && Number(p) <= 255 && !(p.length > 1 && p[0] === "0")); } -export function isLoopbackIp(ip: string): boolean { +function isLoopbackIp(ip: string): boolean { return ip === "::1" || ip === "0:0:0:0:0:0:0:1" || ip === "0.0.0.0" || ip.startsWith("127."); } From 887e481461285a4ccf1cccc4478986d97e32d198 Mon Sep 17 00:00:00 2001 From: awork Date: Tue, 20 Oct 2026 14:53:09 +0800 Subject: [PATCH 8/8] test(dsh-native): adapt #1772 bare-host case to the #3806 overlay-latch gate (#3653) Rebase artifact: master's #3806 made the web-profile note conditional on the #3354 overlay latching (cordis on/reflect present); a hook-less host now gets the ARMED alarm instead. The bare subcase keeps its original purpose (the non-inject direct profileContext bundles path) by supplying minimal no-op hooks so the passive note is what's asserted. --- tests/dsh-native.test.ts | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/tests/dsh-native.test.ts b/tests/dsh-native.test.ts index 24715c8bc2..8ca4018fdb 100644 --- a/tests/dsh-native.test.ts +++ b/tests/dsh-native.test.ts @@ -1152,12 +1152,18 @@ test("#1772 apply(): web-profile compaction caveat warns once in the durable log assert.equal(nonWebTraceLines().length, 1); await _stateToolsReadyForTest(); - // host shape without inject but with a direct profileContext field + // 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);