Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,13 @@ Versions follow the merge of a `*_release-v*` branch; CI publishes to npm on tag
- **The silent-direct oversized guard (#5405) defaults to 8 MiB and honors `BILI_SILENT_DIRECT_MAX_MB`.** The missing-slot / unattributed arms count RAW body bytes — base64-inflated media included — so an in-window session carrying even one medium image died at 2 MiB with a retryable 422 that could never succeed; the runaway multi-MB prompts the guard exists for are tens of megabytes or more. Default raised 2 → 8 MiB (owner ruling, #5826 release review); installs can lower it back per machine (megabytes, >0; invalid values fall back to the default).

### Fixed

* **私有运行日志改成「按目标窗口 + 硬字节预算」的分代保留,不再悄悄只剩 10 MiB + 一个 `.old`**:`bili.log` 里是打码后的主机名、会话 id、上游错误体——既是排查长时事故的唯一证据,又必须限定体量与暴露面。旧方案「10 MiB 封顶 + 单个 `.old`」两个属性都不保证:繁忙机器上 31.7MB/5 天的日志(#2864)让「30 小时前发生了什么」无从回答,而任何读者也只能看见那两个固定文件名。现在活动文件仍在 10 MiB 封存,但封存成**不可变、名字唯一**的一代:`<log>.archive/<UTC 时间戳>-<pid>-<序号>/bili.log`。占用名字靠**一次原子 `mkdir`**决定(而不是「先探测再 rename」),所以并发封存者不会互相覆盖;时间戳在 POSIX/Windows 都合法,序号破时钟并列,所以按名字即可定序。默认目标覆盖 48 小时。真正兜底的是新增的**总字节预算**(`diagnostics.logBudgetBytes`,默认 256 MiB):两者冲突时**预算赢**——超出即按最旧优先删除整代,活动文件永不删;并且绝不谎称一个它没有的 48 小时窗口。覆盖不足有两种可观测出口:启动时一行 `[warn]` 说明实际覆盖与原因,以及 `GET /__bili/logs` 响应里新增的 `retention` 对象(跨度、字节、`shortfall`、`continuity`)。这些数字只陈述**实测跨度**(最旧保留行 → 现在):分代文件里没有任何谱系证据能证明中间没有缺口,所以 `continuity` 恒为 `unknown`,措辞里也绝不出现「完整/未中断」的说法;时间戳错序(调时钟、并发写者、迁移)同样**不**被当作丢历史的正向证据。读不出来的分代记为 `unreadable` 并降级成「至少 N」的下界,而不是把残集当完整窗口。刚部署的实例无法找回旧方案已经删掉的历史,这是事实而非缺陷,日志会如实说「历史比目标年轻」。首次接管时遗留的 `bili.log.old` 会迁移进分代集合(按它的 mtime 定序),不丢历史。隐私面:所有代文件从第一字节起就是 `0600`(旧代码继承 umask,默认 022 机器上留下 0644),接管的既有文件也会补 chmod。默认读者(Web 日志页、诊断报告、`acp-cache-diff` 用量统计)改用**同一个**分代枚举,所以「日志页看得见、诊断报告看不见」这类分裂不再出现;显式 `--log` 仍按单文件语义处理。新增 `src/log-retention.ts`(纯文件操作,不 import config/knobs,避免 `config → logger → config` 环),策略由启动边界 `src/server.ts` 经 `src/knobs.ts` 解析后注入。

* **分代保留的 CI 断言把「已有事实」当成初值,三处假红掩盖了真问题(无生产改动)**:①`log-mask` 的「文件打码、capture 收原文」用例按位置取 `captured[0]` 当告警,而 `configureLogger` 会先经同一 seam 发一条「新鲜日志覆盖不足」的保留诊断——于是 Ubuntu/Windows 全片报 `capture hook must receive the raw message`;现在显式承认该诊断存在(断言其内容),再按**精确原文成员**断言告警恰收一次且带 `warn` 级别,磁盘打码两项照旧。②`log-retention` 的「并发封存者不共用代目录」在 Windows 上报 `2 !== 4`:原子 `mkdir` 只仲裁**谁可以占用目标目录名**,从不承诺**谁拥有源文件**——四进程抢同一个活动文件名时,一个封存者的 `rename` 可能带走另一个尚未落盘的字节。**这个「4 个声称路径 / 2 个真实代」之间的机制尚未查明**,此处不作任何归因;丢失本身属于仍未关闭的 issue5494,是缺陷而非平台怪癖。原断言要求「归档代数量等于声称数量」,而代码从未做过这个承诺,故改为「无两胜者同名、归档不多于声称数、每代都是声称过的名字」;持久、确定的对照例移进新的 `tests/log-retention-followup.test.ts`:四个**不同来源**、同一命名空间、同一时间戳,逐字节守恒且计数器为 1/2/3/4。③同文件新增两个用例钉住 `pendingRetentionDrains`:被替换流**封存前已写入的那一行**在 `end()` 完成前不得被预算回收,释放后延迟的回收才执行,活动文件上的直接追加与轮转后的新行不受影响;`closeLogger()` 解析前其缓冲尾巴必须已落盘。issue5494 的单封存/无来源语义保留为显式契约断言,不改任何生产代码、不声称共享活动源丢失已修复。

* **分代日志读取保持有界,「下载完整日志」不再暗中只取最后 100000 行**:Web 查询、诊断和用量读者逐行流式遍历,内存随输出/上下文和最长单行增长,不再随 256 MiB 保留预算整体增长。完整原始下载按时间顺序带背压发送所有可读代;预检发现不可读代会在界面与 `x-bili-log-incomplete` 标明,发头后读取失败则中断响应,不把截断当完整。时间窗口允许两次有界遍历但不承诺稳定快照,同一不可读代只计一次;显式 ACP `--log` 仍只读所选文件。看门狗等直接追加现在计入下一次封存的文件大小,活动文件超预算状态按当前字节重新计算,不再沿用打开时快照。预算回收还会等所有被替换流的 `end()` 完成,最后一个排水完成者才执行回收,避免刚封存的代在尾部缓冲落盘前被删除。

- **Hermes native plugin: the documented stock config shape (top-level `base_url` next to `provider:`) now joins the MITM whitelist (#6022).** Hermes' own config examples put the active provider's endpoint in a **top-level** `base_url:` with no `providers:` map and no `custom_providers:` list, but both of the plugin's `~/.hermes/config.yaml` readers scanned only those two map/list shapes — discovery returned `[]`, `_spawn_child()` never set `BILI_MITM_DOMAINS`, and every model request silently blind-tunneled: zero compression, no user-visible error beyond one BLIND TUNNEL WARNING log line per request. `mitm_domains_from_config()` now also reads the top-level `base_url`/`url`/`api` — additive, not fallback-only, since a config may carry both shapes and traffic can legally reach either host; `_provider_base_url()` resolves the same keys for the **active** provider (and unnamed requests — the hook surface often carries no provider kwarg) after its named-entry lookup, so the context-window / max-output header channels (#3261) stop degrading to `None` on this shape. A named non-active provider still gets only its own entry — the active endpoint never leaks into another provider's window resolution. The **launcher** had the same gap: `parseHermesYaml()` collected only `providers:`/`custom_providers:` entries, so `bili hermes` on the stock shape warned "no hermes providers found … traffic will NOT go through the proxy" and blind-tunneled everything; it now captures the indent-0 `provider:`/`base_url:`/`url:`/`api:` lines (the indent guard keeps a `model:`-nested `provider:` from binding) and merges the top-level URL into route discovery under the active provider's name without ever overwriting an existing entry — when both shapes carry different URLs, BOTH hosts stay routable rather than guessing which wins. Quoted endpoint values (`base_url: "https://…"`, as in Hermes' own examples) no longer die silently either: `new URL()` rejects surrounding quotes, so quoted entries were skipped as unparseable; quotes are now stripped at every capture site (unquoted values byte-identical). Behavior change (intended): dual-shape configs whitelist both hosts, and plugin instances spawned before this fix are refused by the attach gate (`mitm-domains-mismatch`) until restarted — the gate exists precisely to avoid riding a whitelist that does not cover the config's hosts. Regressions: `tests/hermes-native.test.ts` new `mitm-top-level` driver scenario (stock-shape whitelist + active/unnamed/other scoping truth table + mixed-shape order/isolation), `tests/launcher.test.ts` top-level shape + quoted-value cases.
- **KeyRing model-local 429s no longer throttle other models on the same credential.** Bili gives an independent origin+credential+model hold only to HTTP 429 responses whose structured KeyRing or Bili marker has an exact request-model match; `keyring_all_limited`, direct OpenCode monthly quotas, malformed bodies, and mismatches remain origin+credential holds. Large passthrough bodies use a top-level model scan, while Gemini/Vertex/v1 and side-lane paths carry their actual wire model. Account/model probe slots are request-owned and canceled sends release only their token; concurrent provisional 429 observations settle independently. Continuation retries reserve bounded classification capacity per origin+credential before egress; saturation skips only the optional refetch, without a global gate or discarding account-wide evidence. Continuation reads retain the upstream idle/abort policy. Side-lane SSE retains its stream mode so render-tag stripping still runs. Regressions: `tests/issue4204-throttle-governor.test.ts`, `tests/issue4743-degenerate-refetch-429.test.ts`, `tests/proxy-noinject-tag-strip.test.ts`, and `tests/upstream-error-log.test.ts`.

Expand Down
4 changes: 4 additions & 0 deletions docs/configuration.md
Original file line number Diff line number Diff line change
Expand Up @@ -255,6 +255,8 @@ This index is generated from `website/config-reference/*.yaml` — edit the seed
| `diagnostics.dump4xxMaxBytes` | number | 2097152 (floor 1024) | BILI_DUMP_4XX_MAX_BYTES | Size cap for a single 4xx dump file. |
| `diagnostics.maxTotalBytes` | number | unset (off) | BILI_DUMP_MAX_TOTAL_BYTES | Total-size cap for debug dump dirs (dumps/, raw/, SSE dump dir); oldest files deleted first when exceeded (floor 1 MiB). Dumps have no rotation by default and can grow tens of GB/day under active capture (#2412). |
| `diagnostics.maxAgeDays` | number | unset (off) | BILI_DUMP_MAX_AGE_DAYS | Age cap in days for debug dump dirs; files older than this are deleted (floor 1 h). Off by default. |
| `diagnostics.logRetentionHours` | number | 48 (floor 1) | BILI_LOG_RETENTION_HOURS | History the PRIVATE run log (`bili.log`) is retained to cover, in hours. The logger seals the active file at 10 MiB into an immutable, uniquely named generation instead of one `bili.log.old`, so every retained generation is enumerable and the window is real rather than accidental. This is a TARGET, not a promise: `diagnostics.logBudgetBytes` is the hard bound and always wins — a shorter window is reported, never silently claimed as complete. Reported coverage is the MEASURED span from the oldest retained line to now; generation files carry no lineage evidence, so `continuity` is always `unknown` and no status claims an unbroken window (out-of-order stamps are not treated as missing history either). Floor 1 h; a non-positive or non-numeric value falls back to the 48 h default, and because a SET env var owns the switch, an illegal `BILI_LOG_RETENTION_HOURS` falls back to that default rather than to this key. |
| `diagnostics.logBudgetBytes` | number | 268435456 (256 MiB, floor 1 MiB) | BILI_LOG_BUDGET_BYTES | Hard total-byte budget across the active log plus every sealed generation; oldest generations are deleted first when it is exceeded — oldest first by sealed stamp (legacy `.old` takes its own mtime), so a migrated legacy artifact is reclaimed in its real chronological place instead of being skipped in favour of newer stamped generations. Bounds disk cost and privacy exposure on a busy machine where the default window would otherwise run to many times this. When the bytes needed for `logRetentionHours` exceed the budget, the BUDGET WINS and the shortfall is surfaced (a start-time `[warn]`, and the `retention` object in `GET /__bili/logs`). The ACTIVE file is never deleted, so a single active file that alone exceeds the budget is reported as over budget rather than truncated. Floor 1 MiB; a non-positive or non-numeric value falls back to the 256 MiB default, and a set `BILI_LOG_BUDGET_BYTES` owns the switch the same way as the hours key. Files are owner-only: 0600 from the first byte, archive directories 0700 (win32 has no POSIX mode bits and the state-dir ACL governs). |
| `diagnostics.renderNone` | boolean | false | ACP_RENDER_NONE | Disable all ACP tag rendering (raw wire study mode). |
| `diagnostics.invariantJudge` | boolean | true | BILI_INVARIANT_JUDGE | Keep the I1/I2 outbound-invariant judge armed as an L1 advisory (#3938): every request's outbound bytes are reconciled against the client's inbound history; violations log at warn, clean verdicts at debug, traffic is never blocked. Set false or BILI_INVARIANT_JUDGE=0 to silence it. |
| `diagnostics.noInjectTool` | boolean | false | ACP_NO_INJECT_TOOL | Stop injecting the acp_compress tool definition into requests. |
Expand Down Expand Up @@ -1923,7 +1925,9 @@ File keys resolve only when the matching env var is unset. Defaults in parenthes
| `BILI_INGEST_BODY_BYTES` | `network.ingestBodyBytes` | 33554432 |
| `BILI_INVARIANT_JUDGE` | `diagnostics.invariantJudge` | true |
| `BILI_KEEP_ALIVE_TIMEOUT_MS` | `network.keepAliveTimeoutMs` | 5000 |
| `BILI_LOG_BUDGET_BYTES` | `diagnostics.logBudgetBytes` | 268435456 (256 MiB, floor 1 MiB) |
| `BILI_LOG_MASK_HOSTS` | `maskHosts` | true |
| `BILI_LOG_RETENTION_HOURS` | `diagnostics.logRetentionHours` | 48 (floor 1) |
| `BILI_MAX_MESSAGES` | `network.maxMessages` | 0 |
| `BILI_MAX_SESSIONS` | `sessions.max` | 256 |
| `BILI_MAX_SHRINK_PER_COMPRESS` | `network.maxShrinkPerCompress` | unset |
Expand Down
4 changes: 4 additions & 0 deletions docs/configuration.zh-CN.md
Original file line number Diff line number Diff line change
Expand Up @@ -255,6 +255,8 @@
| `diagnostics.dump4xxMaxBytes` | number | 2097152 (floor 1024) | BILI_DUMP_4XX_MAX_BYTES | 单个 4xx 转储文件的大小上限。 |
| `diagnostics.maxTotalBytes` | number | unset (off) | BILI_DUMP_MAX_TOTAL_BYTES | 调试转储目录(dumps/、raw/、SSE dump 目录)的总大小上限;超限时按最旧优先删除(下限 1 MiB)。转储默认无任何轮转,持续捕获下每天可增长数十 GB(#2412)。 |
| `diagnostics.maxAgeDays` | number | unset (off) | BILI_DUMP_MAX_AGE_DAYS | 调试转储目录的保留天数上限;超过该天数的文件被删除(下限 1 小时)。默认关闭。 |
| `diagnostics.logRetentionHours` | number | 48(下限 1) | BILI_LOG_RETENTION_HOURS | **私有**运行日志(`bili.log`)目标覆盖的历史小时数。日志器在 10 MiB 处把活动文件封存为**不可变、名字唯一**的一代,取代单个 `bili.log.old`,于是每一代都可枚举、窗口是真实的而不是碰巧的。这是**目标**而非承诺:`diagnostics.logBudgetBytes` 是硬上限且永远优先——更短的窗口会被如实报告,绝不悄悄宣称完整。报告的覆盖是**实测跨度**(最旧保留行 → 现在):分代文件里没有谱系证据,所以 `continuity` 恒为 `unknown`,任何状态都不宣称窗口未中断(时间戳错序同样不被当作历史缺失)。下限 1 小时;非正数或非数值回落到默认 48 小时;按「env 一旦设置即拥有该开关」的约定,非法的 `BILI_LOG_RETENTION_HOURS` 回落到该默认值而不是本键。 |
| `diagnostics.logBudgetBytes` | number | 268435456(256 MiB,下限 1 MiB) | BILI_LOG_BUDGET_BYTES | 活动日志 + 全部封存分代的总字节硬预算;超出时按**最旧优先**整代删除——排序按封存时间戳(遗留 `.old` 用自己的 mtime),所以迁移来的历史按其真实年代被回收,而不会被跳过、先去删更新的分代。它约束繁忙机器上的磁盘占用与隐私暴露面。当 `logRetentionHours` 所需的字节超出预算时**预算赢**,且不足会被上报(启动时一行 `[warn]`,以及 `GET /__bili/logs` 里的 `retention` 对象)。活动文件永不删除,所以单个活动文件本身就超过预算时会被报告为超预算,而不是被截断。下限 1 MiB;非正数或非数值回落到默认 256 MiB,且与小时键同理:设置了的 `BILI_LOG_BUDGET_BYTES` 拥有该开关。日志文件仅属主可读:首字节起 0600,归档目录 0700(win32 无 POSIX 权限位,由状态目录 ACL 决定)。 |
| `diagnostics.renderNone` | boolean | false | ACP_RENDER_NONE | 禁用所有 ACP 标签渲染(原始线路研究模式)。 |
| `diagnostics.invariantJudge` | boolean | true | BILI_INVARIANT_JUDGE | 保持 I1/I2 出站不变量判官以 L1 advisory 形态开启(#3938):每笔请求的出站字节都与客户端入站历史对账,违规以 warn 记录、干净判定以 debug 记录,绝不阻塞流量。设为 false 或 BILI_INVARIANT_JUDGE=0 可静默。 |
| `diagnostics.noInjectTool` | boolean | false | ACP_NO_INJECT_TOOL | 停止向请求注入 acp_compress 工具定义。 |
Expand Down Expand Up @@ -1900,7 +1902,9 @@ ACP 原生 agent(当前为 `pi` 扩展)会在每个进程内向代理上报
| `BILI_INGEST_BODY_BYTES` | `network.ingestBodyBytes` | 33554432 |
| `BILI_INVARIANT_JUDGE` | `diagnostics.invariantJudge` | true |
| `BILI_KEEP_ALIVE_TIMEOUT_MS` | `network.keepAliveTimeoutMs` | 5000 |
| `BILI_LOG_BUDGET_BYTES` | `diagnostics.logBudgetBytes` | 268435456 (256 MiB, floor 1 MiB) |
| `BILI_LOG_MASK_HOSTS` | `maskHosts` | true |
| `BILI_LOG_RETENTION_HOURS` | `diagnostics.logRetentionHours` | 48 (floor 1) |
| `BILI_MAX_MESSAGES` | `network.maxMessages` | 0 |
| `BILI_MAX_SESSIONS` | `sessions.max` | 256 |
| `BILI_MAX_SHRINK_PER_COMPRESS` | `network.maxShrinkPerCompress` | unset |
Expand Down
2 changes: 2 additions & 0 deletions docs/lifecycle.md
Original file line number Diff line number Diff line change
Expand Up @@ -156,6 +156,8 @@
| `BILI_FAKE_BUF_CAP` | fake-completion 缓冲上限,16 MiB |
| `BILI_CONFLICT_SCAN` | thirdparty-scan 冲突扫描开关 |
| `BILI_INVARIANT_JUDGE` | diagnostics.invariantJudge,默认 on——I1/I2 出站不变量法官(L1 顾问:violation 只 warn、流量永不拦;`=0` 解除武装,#3938 decision ②) |
| `BILI_LOG_RETENTION_HOURS` | diagnostics.logRetentionHours,默认 48(下限 1h)——私有 `bili.log` 的目标覆盖窗口:活动文件 10 MiB 封存为不可变、名字唯一的一代;字节预算永远优先,报告的覆盖是**实测跨度**且 `continuity` 恒为 `unknown`(不宣称未中断);非法值回落到默认而非文件层(env 一旦设置即拥有开关) |
| `BILI_LOG_BUDGET_BYTES` | diagnostics.logBudgetBytes,默认 268435456(256 MiB,下限 1 MiB)——活动日志 + 全部封存分代的硬总字节预算,超限按封存时间戳**最旧优先**整代删除;活动文件永不删除(单文件超预算只报告、不截断);与 `BILI_LOG_RETENTION_HOURS` 冲突时预算赢 |

### 2.6 安全 / 运维通道

Expand Down
3 changes: 3 additions & 0 deletions reference/architecture.md
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,9 @@ billion-context/
│ ├── event-loop-watchdog.ts # Worker-thread event-loop wedge detector: self-exit after 5 min of continuous main-loop silence (#4762)
│ ├── paths.ts # XDG paths (config/cache/state) + relocation env vars
│ ├── logger.ts # Tee logger: state-dir file + stderr
│ ├── log-retention.ts # Private-log generations: 48h target under a hard byte budget, 0600, honest coverage
│ ├── log-retention-coverage.ts # Retention coverage reader: measured span from the oldest retained line, continuity always unknown
│ ├── log-file-lines.ts # Streaming line reader over the retained generations (bounded memory, per-file error reporting)
│ ├── log-mask.ts # Log safety: masks credentials/endpoints (#255)
│ ├── version.ts # Own version identity from package.json at runtime
│ ├── doctor.ts # Read-only advisory evaluation against on-disk version (#1577)
Expand Down
Loading
Loading