Skip to content
Open
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
2 changes: 1 addition & 1 deletion docs/configuration.ja.md
Original file line number Diff line number Diff line change
Expand Up @@ -425,7 +425,7 @@
| `BILI_INHERITED_HTTPS_PROXY` | string | unset | — | Launcher channel: spawn 境界をまたいで継承された https_proxy の値が保持されます。 |
| `BILI_INHERITED_ALL_PROXY` | string | unset | — | Launcher channel: spawn 境界をまたいで継承された all_proxy の値が保持されます。 |
| `BILI_INHERITED_NO_PROXY` | string | unset | — | Launcher channel: spawn 境界をまたいで継承された no_proxy の値が保持されます。 |
| `BILI_NATIVE_CLAUDE` | boolean-ish | off | — | Host posture: claude ネイティブプラグインプロセスであることを示します。 |
| `BILI_NATIVE_CLAUDE` | boolean-ish | off | — | Host posture: claude ネイティブプラグインプロセスであることを示します。`=0` はレーンを**passthrough**モード(圧縮完全オフ)にします — レーンは依然として起動しポートをバインドします。レーンの起動を阻止しません(他の `BILI_NATIVE_*` レーンとは異なり、それらの `=0` はレーンを完全にオフにします)。 |
| `BILI_NATIVE_DSH` | boolean-ish | off | — | Host posture: dsh ネイティブプラグインプロセスであることを示します。 |
| `BILI_NATIVE_KIMI` | boolean-ish | off | — | Host posture: kimi ネイティブプラグインプロセスであることを示します。 |
| `BILI_NATIVE_OMP` | boolean-ish | off | — | Host posture: omp ネイティブプラグインプロセスであることを示します。 |
Expand Down
2 changes: 1 addition & 1 deletion docs/configuration.md
Original file line number Diff line number Diff line change
Expand Up @@ -428,7 +428,7 @@ This index is generated from `website/config-reference/*.yaml` — edit the seed
| `BILI_INHERITED_HTTPS_PROXY` | string | unset | — | Launcher channel: inherited https_proxy value preserved across the spawn boundary. |
| `BILI_INHERITED_ALL_PROXY` | string | unset | — | Launcher channel: inherited all_proxy value preserved across the spawn boundary. |
| `BILI_INHERITED_NO_PROXY` | string | unset | — | Launcher channel: inherited no_proxy value preserved across the spawn boundary. |
| `BILI_NATIVE_CLAUDE` | boolean-ish | off | — | Host posture: marks a claude-native plugin process. |
| `BILI_NATIVE_CLAUDE` | boolean-ish | off | — | Host posture: marks a claude-native plugin process. `=0` opts the lane into **passthrough** mode (compression fully off) — the lane STILL starts and binds its port; it does NOT prevent the lane from starting (unlike the other `BILI_NATIVE_*` lanes, where `=0` turns the lane off entirely). |
| `BILI_NATIVE_DSH` | boolean-ish | off | — | Host posture: marks a dsh-native plugin process. |
| `BILI_NATIVE_KIMI` | boolean-ish | off | — | Host posture: marks a kimi-native plugin process. |
| `BILI_NATIVE_OMP` | boolean-ish | off | — | Host posture: marks an omp-native plugin process. |
Expand Down
2 changes: 1 addition & 1 deletion docs/configuration.zh-CN.md
Original file line number Diff line number Diff line change
Expand Up @@ -428,7 +428,7 @@
| `BILI_INHERITED_HTTPS_PROXY` | string | unset | — | 启动器通道:跨派生边界保留的 https_proxy 继承值。 |
| `BILI_INHERITED_ALL_PROXY` | string | unset | — | 启动器通道:跨派生边界保留的 all_proxy 继承值。 |
| `BILI_INHERITED_NO_PROXY` | string | unset | — | 启动器通道:跨派生边界保留的 no_proxy 继承值。 |
| `BILI_NATIVE_CLAUDE` | boolean-ish | off | — | 宿主姿态:标记 claude 原生插件进程。 |
| `BILI_NATIVE_CLAUDE` | boolean-ish | off | — | 宿主姿态:标记 claude 原生插件进程。`=0` 让 lane 以**透传**模式运行(压缩完全关闭)——lane 仍会拉起并绑定端口,**不会**阻止 lane 拉起(与其他 `BILI_NATIVE_*` 车道不同,它们的 `=0` 是整体关闭车道)。 |
| `BILI_NATIVE_DSH` | boolean-ish | off | — | 宿主姿态:标记 dsh 原生插件进程。 |
| `BILI_NATIVE_KIMI` | boolean-ish | off | — | 宿主姿态:标记 kimi 原生插件进程。 |
| `BILI_NATIVE_OMP` | boolean-ish | off | — | 宿主姿态:标记 omp 原生插件进程。 |
Expand Down
3 changes: 2 additions & 1 deletion docs/lifecycle.md
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,8 @@

| 变量 | 一句话用途 |
|------|-----------|
| `BILI_NATIVE_CLAUDE` / `BILI_NATIVE_DSH` / `BILI_NATIVE_KIMI` / `BILI_NATIVE_OMP` / `BILI_NATIVE_OPENCODE` / `BILI_NATIVE_PI` / `BILI_NATIVE_ZCODE` | 对应宿主 native 车道 `=0` 整体退出(bootstrap 各文件入口判断) |
| `BILI_NATIVE_CLAUDE` | claude native 车道 `=0` → **passthrough**(lane 仍拉起、压缩关;不阻止 lane 启动)(claude-native-bootstrap.ts 入口判断) |
| `BILI_NATIVE_DSH` / `BILI_NATIVE_KIMI` / `BILI_NATIVE_OMP` / `BILI_NATIVE_OPENCODE` / `BILI_NATIVE_PI` / `BILI_NATIVE_ZCODE` | 对应宿主 native 车道 `=0` 整体退出(mode: off,lane 不拉起;bootstrap 各文件入口判断) |
| `BILI_NATIVE_ATTACH_POLICY` | native attach 策略(env > 文件 native.attachPolicy) |

### 2.2 内部 spawn 通道(launcher→子进程,非用户面)
Expand Down
3 changes: 3 additions & 0 deletions docs/technical-notes.ja.md
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,9 @@ README では各オプションを簡潔に記述しており、「実際の動
## Claude ネイティブ姿勢 (#964)

Claude Code にはインプロセスの拡張ポイントがないため、`bili plugin install claude` は `~/.claude/settings.json` に管理ブロックを書き込みます(環境変数 `ANTHROPIC_BASE_URL=http://127.0.0.1:<port>/bili/<upstream>`、`DISABLE_AUTO_COMPACT=1`、および `SessionStart` フック)、さらに以前と同じユーザースコープの MCP シェルを追加します。フック(claude の最初のモデルリクエストの前に発火)は、すべてのレーンと同様にポートを解決します(#1660):明示的なピン(`BILI_CLAUDE_NATIVE_PORT` > 設定 `claude.nativePort`)は、その正確なポートで**strict-port** を起動します(占有者は明確に拒否され、#964 が保持されます);それ以外の場合は、自己管理ゾーンに乗ります — レーンのスティッキーレコード、なければベース `18787` — 非 strict で、子プロセスの EADDRINUSE +1 ラダーが競合を解決し、決定したポートがスティッキーとして記録されます。ラダーに対する1つの例外(#1723):レーンのポートの保持者が、**異なるビルド**を実行している同じレーンのインスタンスである場合(アップグレード再起動の重複 — 旧バージョンがまだドレイン中)、子プロセスはそれが解放されるのを待ち(最大5秒)、ドリフトする代わりに*同じ*ポートを再バインドします;決して離れない保持者は待機を使い切り、以前と同様に通常のラダーを取得します。プロキシが起動した後、フックは各セッションで管理対象の `ANTHROPIC_BASE_URL` をライブオリジンに再ピンします(`repinClaudeManagedBaseUrl`)、そのためポートがホップしても次の起動時に自己修復され、焼き込まれた URL が実行中のプロキシからデシンクしたままになることはありません。アップストリームオーバーライド:`BILI_CLAUDE_UPSTREAM`(または既存の `claude.anthropicBaseUrl` 設定)。インストールはもはや `claude.nativePort` を永続化しません。`BILI_NATIVE_CLAUDE=0` でオプトアウトできます — その場合、フックは同じ解決ポートで**passthrough** プロキシを起動します(そのまま転送、圧縮オフ)ため、claude は引き続き動作します。ブロックは純粋な JSON マージ/ストリップです:外部キーは決して触れられず、`bili plugin remove claude` は正確に復元します。ネイティブブロックがインストールされたマシンでも `bili claude` は引き続き動作します — それは静的な URL を自身のプロキシでオーバーライドし、フックは休眠状態のままです。

**無人/サスペンドするデバイス**(スマートフォン、NAS、ディープスリープのラップトップ):レーンの event-loop ウォッチドッグは、**自身の tick 間隔**を観察することで OS レベルのフリーズ(プロセス全体がサスペンド — メインスレッドとワーカーの両方)を検出し、自己終了する代わりにベースラインをリセットします(#7482)。そのため、デバイスがスリープしていた際に生存していたレーンは、ウェイク後も生存しています — 次のセッションはコールドスポンと競走するのではなく、それに取り付けます。ただし、レーンの自己修復は、リスポーンをトリガーするために**セッションの存在**に依然として依存しています:*真の* ウェッジ(真のデッドロック、フリーズではない)の後、セッションのない無人デバイスでは、レーンのポートは次のセッションが開始するまでデッドになります。こうしたデバイスで最大の堅牢性を求める場合、**daemon ポーズ**(`bili start --port N` + ヘルスチェックと再起動を行う外部スーパーバイザー)が依然として最も安全な選択です — それはどのセッションの存在にも依存しません。

## 注入優先順位 — 避ける限りファイルは使用しない (#535)

bili はユーザーデータを所有しません。起動されたクライアントはすべて**実際のホームディレクトリ**で実行されるため、ランタイムの書き込みはユーザーが期待する場所に保存されます。クライアントをプロキシに向ける際、ランチャーは優先順位に基づいて選択します。**まず環境変数**(hermes/dsh/codex 用のプロキシ/CA 環境変数、pi/omp 用の `BILI_PROVIDER_REWRITES` URL マニフェスト。これはロード時にそれらの拡張機能の `registerProvider` によって消費されます)、次に**CLI フラグまたは拡張機能 API**(codex の `-c key=value`、opencode プラグイン)、そして**最後に生成ファイル**です。現在、生成ファイルとして使用されているのは opencode の一時 `opencode.json`(終了時に削除される)と dsh のループバック例外のみです。dsh のフェッチスタックはループバックターゲットに対してプロキシ環境変数を無条件にバイパスするため、ローカルアップストリームは、dsh が設定パスの環境変数やアップストリームループバックのオプトアウト機能を獲得するまで、永続的な `~/.dsh-bili` オーバーレイ書き換えを維持します。旧バージョンによって作成されたオーバーレイディレクトリはそのまま放置され、実際のホームディレクトリにマージされることはありません。
Expand Down
13 changes: 13 additions & 0 deletions docs/technical-notes.md
Original file line number Diff line number Diff line change
Expand Up @@ -164,6 +164,19 @@ restores exactly. `bili claude` still works on a machine with the native
block installed — it overrides the static URL with its own proxy and the
hook stays dormant.

**Unattended / suspending devices** (phones, NAS, deep-sleep laptops): the
lane's event-loop watchdog now detects an OS-level freeze (the whole process
suspended — main thread AND worker) by watching its OWN tick interval, and
resets its baseline instead of self-exiting (#7482). So a lane that was alive
when the device slept is still alive when it wakes — the next session attaches
to it instead of racing a cold spawn. The lane's self-heal still depends on a
session being present to trigger a respawn after a *genuine* wedge (a real
deadlock, not a freeze); on an unattended device with no session, a wedged
lane leaves its port dead until the next session starts. For maximum
robustness on such devices, the **daemon posture** (`bili start --port N` +
an external supervisor that health-checks and restarts) is still the safest
choice — it does not depend on any session being present.

## Injection priority — no files unless unavoidable (#535)

bili never owns user data: every launched client runs on its **real home**, so
Expand Down
2 changes: 2 additions & 0 deletions docs/technical-notes.zh-CN.md
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,8 @@ launcher 环境变量这档覆盖纯代理客户端(无进程内插件):`bili <c

Claude Code 没有进程内扩展点,所以 `bili plugin install claude` 往 `~/.claude/settings.json` 写一个受管块(env `ANTHROPIC_BASE_URL=http://127.0.0.1:<port>/bili/<upstream>`、`DISABLE_AUTO_COMPACT=1`、`SessionStart` hook),外加同样的用户级 MCP shell。hook 在首个模型请求前触发,端口解析与所有 lane 一致(#1660):显式钉死(`BILI_CLAUDE_NATIVE_PORT` > config `claude.nativePort`)→ 该确切端口**严格端口**启动(占用者被大声拒绝,#964 保留);否则骑自管区 —— 该 lane 粘性记录优先,否则基准口 `18787` —— 非严格,子进程 EADDRINUSE +1 阶梯解决碰撞,落定端口粘性记录。阶梯有一个例外(#1723):lane 端口的持有者是**不同构建**的同 lane 实例(升级重启重叠——旧版本还在排水)时,子进程等它释放(最多 5 秒)后复用*同一*端口,而不是漂移;持有者永不退出则等待预算耗尽,照旧走 +1 阶梯。代理就绪后 hook 每会话把受管 `ANTHROPIC_BASE_URL` 重钉到存活 origin(`repinClaudeManagedBaseUrl`),跳口后下次启动自愈,烘进 URL 永不与运行中代理失步。上游覆盖:`BILI_CLAUDE_UPSTREAM`(或既有 `claude.anthropicBaseUrl`)。install 不再持久化 `claude.nativePort`。`BILI_NATIVE_CLAUDE=0` 退出 —— hook 改为在同一解析端口上拉起 **passthrough** 代理(原样转发、关闭压缩)。块是纯 JSON merge/strip:外部键从不触碰,`bili plugin remove claude` 精确还原。装有原生块的机器上 `bili claude` 仍可用 —— 它用自身代理覆盖静态 URL,hook 保持休眠。

**无人值守 / 会休眠的设备**(手机、NAS、深度睡眠的笔记本):lane 的 event-loop watchdog 现在通过观察**自身 tick 间隔**来检测 OS 级冻结(整个进程被挂起——主线程和 worker 全冻),此时重置基线而非自退(#7482)。因此设备休眠时存活的 lane,唤醒后依然存活——下次会话直接 attach,而不是与冷启动竞速。但 lane 的自愈仍依赖**有会话在场**来触发重生:一次*真* wedge(真死锁,不是冻结)之后,无人值守设备上没有会话,lane 的端口就会空置到下次会话启动。在这类设备上追求最大稳健性,**daemon 姿势**(`bili start --port N` + 外部监督者做健康检查与重启)仍是更安全的选择——它不依赖任何会话在场。

## 注入优先级 —— 能不写文件就不写(#535)

bili 永不拥有用户数据:每个被启动的客户端都跑在**真实 home** 上,运行期写入落在用户预期的位置。把客户端指向代理时,启动器按优先级选择——**优先 env 变量**(hermes/dsh/codex 的代理/CA env;pi/omp 的 `BILI_PROVIDER_REWRITES` URL 清单,由扩展加载时经 `registerProvider` 消费),其次 **CLI 参数或扩展 API**(codex `-c key=value`、opencode 插件),最后才是**生成文件**——目前仅剩 opencode 的临时 `opencode.json`(退出即删)和 dsh 的回环例外:dsh 的 fetch 栈对回环目标无条件绕过代理 env,所以本地上游保留持久 `~/.dsh-bili` overlay 改写,直到 dsh 提供 settings-path env 或上游支持回环 opt-out。旧版本创建的 overlay 目录原地保留,绝不合并回真实 home。
Expand Down
24 changes: 18 additions & 6 deletions src/claude-native-bootstrap.ts
Original file line number Diff line number Diff line change
Expand Up @@ -409,11 +409,17 @@ export function isTransientShArgv(argv: string[]): boolean {
* host when the walk finds one. Otherwise: a SessionStart hook's direct
* parent is always the transient `sh -c` wrapper — watching it re-arms the
* 2s self-kill — so fall back to ITS parent instead (in a real session that
* is claude itself, even when an exotic install form went unrecognized);
* when the direct parent is anything else (manual run under an interactive
* shell) or unreadable, keep the legacy direct parent. Exported for tests
* (inject `read` to stub the process table). */
export function chooseWatchdogParentPid(opts: { read?: ProcReader; parentPid?: number } = {}): number {
* is claude itself, even when an exotic install form went unrecognized).
* #7482: when the walk misses AND the direct parent is not a transient
* shell (interactive bash under a session-holder like tmux/herdr/screen,
* or an orphaned tool shell with ppid=1), the direct parent outlives the
* claude session — watching it pins the lane to a long-lived ancestor and
* the anti-orphan never fires (observed: 21 h lane survival on Android
* chroot under herdr-core). Return undefined instead: the lane arms the
* watcher without a parent pid and relies on the MCP child (a child of the
* claude host, registered via POST /__bili/watcher) to detect session end.
* Exported for tests (inject `read` to stub the process table). */
export function chooseWatchdogParentPid(opts: { read?: ProcReader; parentPid?: number } = {}): number | undefined {
const read = opts.read ?? defaultProcReader();
const host = resolveClaudeHostPid({ read });
if (host !== undefined) return host;
Expand All @@ -423,7 +429,7 @@ export function chooseWatchdogParentPid(opts: { read?: ProcReader; parentPid?: n
const grandPid = parentInfo.ppid;
if (grandPid !== null && grandPid > 1) return grandPid;
}
return parentPid;
return undefined;
}

/** #2408: declare fork lineage to the proxy. A `claude --resume <parent>
Expand Down Expand Up @@ -473,6 +479,12 @@ async function run(payloadJson: string): Promise<void> {
// host itself; when the walk cannot find it, chooseWatchdogParentPid
// degrades via the wrapper's parent instead of the wrapper.
const watchPid = chooseWatchdogParentPid();
// #7482: field instrumentation — record which pid the parent watcher
// chose (and the direct parent for comparison). "none" means the walk
// missed and the direct parent is not a transient shell — the lane
// arms the watcher without a parent pid and relies on the MCP child
// to detect session end.
log(`watchdog parent pid=${watchPid ?? "none"} (direct parent=${process.ppid})`);
// #1660: an EXPLICIT pin stays exact + strict; otherwise pass port 0
// so the launcher resolves the zone preference and settles the
// actually-bound port sticky (a pre-resolved port > 0 would skip the
Expand Down
Loading
Loading