Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
25 commits
Select commit Hold shift + click to select a range
7e40273
feat: partition codex subagent threads by turn-metadata (PR-A of #316)
Aug 28, 2026
8e9c99a
feat: anonymous tail-window reattach + fork lineage for prefix affini…
Aug 28, 2026
40eb72e
fix: partition any non-user codex thread_source by thread-id (PR review)
ranxianglei Aug 28, 2026
3058729
fix: tail-window reattach matches at any offset, not just the stored …
ranxianglei Aug 28, 2026
0bca1b7
feat: align launcher auto-compact budgets with bili window (codex -c,…
Aug 28, 2026
8d27bca
fix: gate native-compaction rebase on semantic terminal state (#321 P…
Aug 28, 2026
c6d1f2c
feat: clamp effective window to codex's model perception for codex cl…
Aug 28, 2026
008bb95
feat: intercept + forge codex native compaction when ACP is healthy (…
Aug 28, 2026
8adeef1
docs: manual claude /bili/ routing + auto-compact alignment (#321 PR-D)
Aug 28, 2026
c507e0c
fix: accept codex_exec/ UA prefix — codex 0.147 exec sends codex_exec…
ranxianglei Aug 29, 2026
45e5fd8
Merge remote-tracking branch 'origin/master' into 2026-08-28_codex-co…
Aug 29, 2026
c959942
test: pin codex_exec/ UA prefix — regression guard for the real-devic…
Aug 29, 2026
2db15cf
Merge remote-tracking branch 'origin/2026-08-28_codex-budget-alignmen…
ranxianglei Aug 29, 2026
546148e
Merge remote-tracking branch 'origin/2026-08-28_semantic-rebase-gate'…
ranxianglei Aug 29, 2026
6cfc65e
Merge remote-tracking branch 'origin/2026-08-28_codex-model-alignment…
ranxianglei Aug 29, 2026
6be6abe
Merge remote-tracking branch 'origin/2026-08-28_codex-compact-interce…
ranxianglei Aug 29, 2026
a5b3bf2
Merge remote-tracking branch 'origin/2026-08-28_codex-turn-metadata' …
ranxianglei Aug 29, 2026
ef9c2cd
Merge remote-tracking branch 'origin/2026-08-28_tail-window-reattach'…
ranxianglei Aug 29, 2026
0074964
fix: unify isCodexClient on CODEX_UA_PREFIXES (codex_exec/ was missed…
ranxianglei Aug 29, 2026
656f90d
fix: codex compact — hand forged summaries off to post-forge turns
Aug 29, 2026
7ed8dab
fix: strip bili compaction item on every /responses/compact forwardin…
Aug 29, 2026
ccd7036
docs: document BILI_CODEX_COMPACT in CONFIGURATION (EN + zh-CN)
Aug 29, 2026
1c6a72c
fix: reconcile the two parallel review-fix passes (hybrid handoff)
ranxianglei Aug 29, 2026
413d3a7
feat: default BILI_CODEX_COMPACT=intercept (opt out with pass)
ranxianglei Aug 29, 2026
e6b822d
fix: auto-fallback to wire tools when codex upstream is local/private
ranxianglei Aug 29, 2026
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
18 changes: 17 additions & 1 deletion CONFIGURATION.md
Original file line number Diff line number Diff line change
Expand Up @@ -356,9 +356,10 @@ Environment variables take precedence over the config file. They are useful for
| `BILLION_CONTEXT_PROXY` | Exported by the launcher; client-side bili plugins/extensions detect it and self-disable their own compression (no double compression). |
| `BILLION_CONTEXT_PLUGIN` | Set `0` to disable plugin mode entirely (wire-level tool injection resumes). |
| `BILI_LAUNCHER_MODEL_WINDOWS` | Internal: the launcher hands the client's own per-model context windows (pi `models.json`, omp `models.yml`, opencode `models.<id>.limit`, codex `model_context_window`) to the spawned proxy as JSON, so the nudge denominator matches the real window for self-hosted models. Only the launcher sets it — no user configuration. |
| `BILI_LAUNCHER_PLUGIN` | Set `0` to disable the launcher's bili MCP server injection for claude/codex (pure wire mode). Native MCP tools are injected by default. See [Launcher Reference](#launcher-reference). |
| `BILI_LAUNCHER_PLUGIN` | Set `0` to disable the launcher's bili MCP server injection for claude/codex (pure wire mode); `1` forces plugin mode. Default: injected — except codex with a local/private upstream (sglang/vllm/ollama cannot parse codex's namespace tool type, so bili auto-falls back to wire tools there). See [Launcher Reference](#launcher-reference). |
| `BILI_LAUNCHER_DIRECT` | Set `1` for direct-URL routing in the launcher (drop MITM/CA trust). See [Launcher Reference](#launcher-reference). |
| `BILI_CLAUDE_UPSTREAM` | claude direct mode: your relay endpoint, when `ANTHROPIC_BASE_URL` already points at a relay the launcher would otherwise bypass. |
| `BILI_CODEX_COMPACT` | Codex native-compaction handling. Default `intercept`: bili intercepts codex's compaction requests and forges a local handoff to the ACP state when the safety gate passes (transform ok + steady-state usage < 90% of the window + at least one active compressed block) — trigger form forges a 2-frame SSE, endpoint form forges `{output}` — and never contacts upstream. Forged ACP summaries are re-injected as a history-borne handoff message (developer-message fallback) so compressed content stays visible after codex truncates its history. Set `pass` to opt out and forward codex's compaction requests upstream (native compaction backstops). On any gate failure the request passes through untouched. |

---

Expand Down Expand Up @@ -449,6 +450,20 @@ Run `codex login` as usual; the OAuth token travels in the `Authorization` heade
"baseUrl": "http://localhost:8787/bili/https://api.anthropic.com"
```

**Claude Code** — set the `ANTHROPIC_BASE_URL` env var to the `/bili/` URL. (claude's undici fetch ignores `HTTPS_PROXY`, so the `/bili/` URL form is the only manual option — cert MITM cannot intercept it.)

```bash
export ANTHROPIC_BASE_URL="http://localhost:8787/bili/https://api.anthropic.com"
```

> **Auto-compact alignment (manual mode only).** The `bili claude` launcher automatically sets `CLAUDE_CODE_AUTO_COMPACT_WINDOW` to bili's effective window for your model, so claude's own auto-compact threshold lines up with bili's compression budget. In manual `/bili/` mode you must do this yourself — otherwise claude may run its own local auto-compact (a "summarize the conversation" turn) on a threshold that doesn't match bili's window. That is usually harmless (same session-id, so bili re-derives state from the truncation) but noisier than needed. Set it to bili's effective window for your model:
>
> ```bash
> export CLAUDE_CODE_AUTO_COMPACT_WINDOW=<bili effective window in tokens>
> ```
>
> claude clamps this value **down** to the window it perceives for the model (never up), so over-setting is safe. You can also set it persistently via claude's settings (`autoCompactWindow`).

**Other API-key clients** (Cursor / Aider / Continue …) — wherever the upstream URL is configured, prepend `http://localhost:8787/bili/`. Nothing else changes.

The `/bili/` prefix doubles as a **self-detection signal**: billion-context client extensions (billion-context-pi / opencode-acp) recognize it in their own baseUrl and self-disable, so you never get double compression.
Expand Down Expand Up @@ -545,6 +560,7 @@ The `/bili/` rewrite modes write a **temp copy** — the real config is never ed
- **omp** — does NOT ship the plugin; the launcher auto-injects `-e dist/agent/omp.js` when the config carries no loadable bili entry (same zero-config ride as pi). Two omp-specific mechanics make the plugin fully native there: omp 17.x mounts extension tools that omit `loadMode` under its `xd://` device URLs (invisible to the model's main turn), so the plugin registers its tools with `loadMode: "essential"` — the model gets the four ACP tools natively; and since omp's fork emits no `before_provider_headers`, the plugin binds the conversation via the launcher identity register (`POST /__bili/plugin/register`, keyed by omp's session id = `prompt_cache_key`/`x-session-id`) — bound sessions run in plugin mode (wire injection suppressed) with the native `/acp` command.
- **opencode** — the temp config appends the thin plugin automatically.
- **claude / codex** — on by default: the launcher injects a single `bili` MCP server (`--mcp-config` for claude, `-c mcp_servers.bili.*` for codex — both ephemeral, nothing written to host config), so the host gets native tools out of the box (verified with claude 2.1.227 / codex 0.147.0). `BILI_LAUNCHER_PLUGIN=0` falls back to plain wire mode — for hosts older than the verified builds that have not been tested against the injection flags.
- **codex + self-hosted upstream auto-fallback** — codex 0.147 ships MCP tools to the model as a `namespace` tool type; self-hosted servers (sglang/vllm/ollama/llama.cpp) do not parse it, leaving the tools silently invisible. When the codex upstream host is loopback/private (`127.0.0.1`, RFC1918, ULA, `.local`, …) and `BILI_LAUNCHER_PLUGIN` is unset, bili automatically uses wire mode instead (flat tools every server understands) and says so on stderr. `BILI_LAUNCHER_PLUGIN=1` forces plugin mode regardless.
- **hermes** — no plugin API; always wire mode.
- **dsh** — the launcher always splices a `--patch <file>` flag into dsh's argv (written to `~/.dsh-bili/.bili-acp.patch.yml`), inserting `dist/agent/dsh-acp.js` into the profile's loader tree: the native `/acp` command, same shape as dsh's own `/compact`. Works on every profile that composes the commands service (web/tui interactive surfaces; the `headless` one-shot driver sends its task straight to the model and parses no commands — `/compact` behaves the same there). Subcommand forms are handled: `dsh web` gets the flag after `web`, `dsh plugin`/`--dump-default-config` take none.

Expand Down
18 changes: 17 additions & 1 deletion CONFIGURATION.zh-CN.md
Original file line number Diff line number Diff line change
Expand Up @@ -356,9 +356,10 @@
| `BILLION_CONTEXT_PROXY` | launcher 会导出它;客户端侧 bili 插件/扩展检测到后自禁用自身压缩(避免双重压缩)。 |
| `BILLION_CONTEXT_PLUGIN` | 设 `0` 彻底关闭插件模式(恢复 wire 层工具注入)。 |
| `BILI_LAUNCHER_MODEL_WINDOWS` | 内部使用:launcher 把客户端自身配置里的逐模型上下文窗口(pi `models.json`、omp `models.yml`、opencode `models.<id>.limit`、codex `model_context_window`)以 JSON 传给自己拉起的代理,让 nudge 分母对自托管模型也用真实窗口。只有 launcher 会设置,无需用户配置。 |
| `BILI_LAUNCHER_PLUGIN` | 设 `0` 关闭 launcher 为 claude/codex 注入 bili MCP 服务器(退回纯 wire 模式)。原生 MCP 工具默认注入。见[启动器参考](#启动器参考)。 |
| `BILI_LAUNCHER_PLUGIN` | 设 `0` 关闭 launcher 为 claude/codex 注入 bili MCP 服务器(退回纯 wire 模式);设 `1` 强制插件模式。默认注入——但 codex 上游为本地/私网地址时自动退回 wire 模式(sglang/vllm/ollama 不解析 codex 的 namespace 工具类型)。见[启动器参考](#启动器参考)。 |
| `BILI_LAUNCHER_DIRECT` | 设 `1` 启用 launcher 直连 URL 路由(放弃 MITM/CA 信任)。见[启动器参考](#启动器参考)。 |
| `BILI_CLAUDE_UPSTREAM` | claude 直连模式:当 `ANTHROPIC_BASE_URL` 已指向某个 relay 时,用它指定你的 relay 端点(否则会被旁路)。 |
| `BILI_CODEX_COMPACT` | codex 原生压缩处理。默认 `intercept`:安全门通过时(transform 成功 + 稳态用量 < 窗口 90% + 至少一个活跃压缩块)拦截 codex 的压缩请求,在本地伪造向 ACP 状态的交接——trigger 形态伪造 2 帧 SSE,endpoint 形态伪造 `{output}`——且不接触上游。伪造的 ACP 摘要经历史承载交接消息注入(缺席时 developer 消息兜底),保证 codex 截断历史后压缩内容仍可见。设为 `pass` 可退出,把 codex 的压缩请求转发给上游(原生压缩兜底)。任一安全门失败则原样透传。 |

---

Expand Down Expand Up @@ -449,6 +450,20 @@ openai_base_url = "http://localhost:8787/bili/https://chatgpt.com/backend-api/co
"baseUrl": "http://localhost:8787/bili/https://api.anthropic.com"
```

**Claude Code** —— 把 `ANTHROPIC_BASE_URL` 环境变量设成 `/bili/` URL。(claude 的 undici fetch 忽略 `HTTPS_PROXY`,所以 `/bili/` URL 形式是唯一的手动方式 —— 证书 MITM 拦不到它。)

```bash
export ANTHROPIC_BASE_URL="http://localhost:8787/bili/https://api.anthropic.com"
```

> **自动压缩对齐(仅手动模式)。** `bili claude` launcher 会自动把 `CLAUDE_CODE_AUTO_COMPACT_WINDOW` 设成 bili 对你模型的有效窗口,让 claude 自己的自动压缩阈值与 bili 的压缩预算对齐。手动 `/bili/` 模式下需要你自己设 —— 否则 claude 可能在与 bili 窗口不一致的阈值上跑它自己的本地自动压缩(一次“总结对话”轮次)。这通常无害(同一 session-id,bili 会从截断中重新推导状态),但比必要的更吵。把它设成 bili 对你模型的有效窗口:
>
> ```bash
> export CLAUDE_CODE_AUTO_COMPACT_WINDOW=<bili 有效窗口 token 数>
> ```
>
> claude 会把这个值**向下**钳制到它自己感知的模型窗口(不会向上),所以设大了是安全的。也可以持久化到 claude 的 settings(`autoCompactWindow`)里。

**其他 API-key 客户端(Cursor / Aider / Continue ……)** —— 只要配置了上游 URL,前面加 `http://localhost:8787/bili/` 就行,其他都不用改。

`/bili/` 前缀还是个**自检测信号**:billion-context 的客户端扩展(billion-context-pi / opencode-acp)能在自己的 baseUrl 里认出它并自禁用,避免双层压缩。
Expand Down Expand Up @@ -545,6 +560,7 @@ Claude Code 的 undici fetch 忽略 `HTTPS_PROXY`,所以证书 MITM 拦不到
- **omp** —— 发行版不自带插件;启动器在配置里没有可加载的 bili 条目时自动注入 `-e dist/agent/omp.js`(与 pi 相同的零配置搭车)。两个 omp 专属机制让插件在那里完全原生:omp 17.x 会把未声明 `loadMode` 的扩展工具挂到 `xd://` 设备 URL 下(模型主回合看不到),插件因此用 `loadMode: "essential"` 注册 —— 模型直接拿到四个 ACP 原生工具;omp 分叉不发 `before_provider_headers`,插件改走启动器身份注册(`POST /__bili/plugin/register`,以 omp 会话 id = `prompt_cache_key`/`x-session-id` 为键)绑定会话 —— 绑定后的会话进入插件模式(抑制 wire 注入)并带有原生 `/acp` 命令。
- **opencode** —— 临时配置自动追加薄插件。
- **claude / codex** —— 默认开启:启动器注入单个 `bili` MCP 服务器(claude 用 `--mcp-config`,codex 用 `-c mcp_servers.bili.*` —— 都是临时生效,不写宿主配置),开箱即原生工具(已在 claude 2.1.227 / codex 0.147.0 验证)。`BILI_LAUNCHER_PLUGIN=0` 退回纯 wire 模式 —— 适用于早于已验证版本、未针对注入参数测试的宿主。
- **codex + 自建上游自动回退** —— codex 0.147 把 MCP 工具以 `namespace` 工具类型发给模型;自建推理服务(sglang/vllm/ollama/llama.cpp)不解析该类型,工具会静默失明。当 codex 上游主机是环回/私网地址(`127.0.0.1`、RFC1918、ULA、`.local` 等)且未设置 `BILI_LAUNCHER_PLUGIN` 时,bili 自动改用 wire 模式(扁平工具,所有服务都认识)并在 stderr 说明。`BILI_LAUNCHER_PLUGIN=1` 可强制插件模式。
- **hermes** —— 无插件 API;永远 wire 模式。
- **dsh** —— 启动器始终在 dsh 的 argv 里拼接 `--patch <file>`(写入 `~/.dsh-bili/.bili-acp.patch.yml`),把 `dist/agent/dsh-acp.js` 插进 profile 的加载树:原生 `/acp` 命令,与 dsh 自带 `/compact` 同一形态。在任何组合了 commands 服务的 profile(web/tui 交互表面)都可用;`headless` 一次性驱动器把任务直接发给模型、不解析命令(原生 `/compact` 在那里同样不可用)。子命令形态已处理:`dsh web` 的 flag 插在 `web` 之后,`dsh plugin`/`--dump-default-config` 不注入。

Expand Down
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
"scripts": {
"build": "tsup",
"registry:snapshot": "node scripts/update-registry-snapshot.mjs",
"codex-models:snapshot": "node scripts/update-codex-models-snapshot.mjs",
"typecheck": "tsc --noEmit --project tsconfig.build.json",
"test": "node --import tsx --test tests/*.test.ts",
"start": "node dist/index.js",
Expand Down
88 changes: 88 additions & 0 deletions scripts/update-codex-models-snapshot.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
#!/usr/bin/env node
/** Refresh src/codex-models-snapshot.json from openai/codex (main branch).

The snapshot is the SLIM form of codex's bundled model table
(codex-rs/models-manager/models.json) — only the fields bili consumes for
budget alignment (#321 PR-E1): slug, contextWindow, maxContextWindow, and the
optional autoCompactTokenLimit / effectiveContextWindowPercent. The full
upstream file is ~424KB of tool/modality metadata bili never reads; the slim
form keeps the bundle small and the contract explicit.

Run manually or before a release:
npm run codex-models:snapshot

Node's global fetch ignores http(s)_proxy env vars, so the script tries the
configured shell proxy first (undici ProxyAgent) and falls back to a direct
connection. If BOTH fail the existing snapshot is left untouched (exit 1).
*/
import { writeFile } from "node:fs/promises";
import path from "node:path";
import { fileURLToPath } from "node:url";

const SOURCE_URL = "https://raw.githubusercontent.com/openai/codex/main/codex-rs/models-manager/models.json";
const OUT_FILE = path.join(path.dirname(fileURLToPath(import.meta.url)), "..", "src", "codex-models-snapshot.json");
const proxyUrl = process.env.https_proxy || process.env.HTTPS_PROXY || process.env.http_proxy || process.env.HTTP_PROXY;

async function attempt(dispatcher) {
const res = await fetch(SOURCE_URL, {
...(dispatcher ? { dispatcher } : {}),
signal: AbortSignal.timeout(20_000),
headers: { Accept: "application/json" },
});
if (!res.ok) throw new Error(`HTTP ${res.status}`);
return res.json();
}

async function fetchFull() {
if (proxyUrl) {
try {
const { ProxyAgent } = await import("undici");
const full = await attempt(new ProxyAgent({ uri: proxyUrl }));
console.log(`fetched openai/codex via proxy ${proxyUrl}`);
return full;
} catch (e) {
console.log(`proxy attempt failed (${e.message}); trying direct`);
}
}
const full = await attempt(undefined);
console.log("fetched openai/codex direct");
return full;
}

let full;
try {
full = await fetchFull();
} catch (e) {
console.error(`could not fetch ${SOURCE_URL}: ${e.message}`);
console.error(`keeping the existing ${path.basename(OUT_FILE)} untouched`);
process.exit(1);
}

const models = Array.isArray(full?.models) ? full.models : null;
if (!models || models.length === 0) {
console.error("unexpected upstream shape: expected { models: [...] }");
console.error(`keeping the existing ${path.basename(OUT_FILE)} untouched`);
process.exit(1);
}

const slim = models.map((m) => {
const e = { slug: m.slug };
for (const [k, key] of [
["context_window", "contextWindow"],
["max_context_window", "maxContextWindow"],
["auto_compact_token_limit", "autoCompactTokenLimit"],
["effective_context_window_percent", "effectiveContextWindowPercent"],
]) {
if (m[k] !== null && m[k] !== undefined) e[key] = m[k];
}
return e;
});

const body = JSON.stringify({
source: `openai/codex main — codex-rs/models-manager/models.json`,
fetchedAt: new Date().toISOString().replace(/\.\d+Z$/, "Z"),
count: slim.length,
models: slim,
}) + "\n";
await writeFile(OUT_FILE, body, "utf8");
console.log(`wrote ${OUT_FILE} (${slim.length} models, ${(body.length / 1024).toFixed(1)} KB)`);
Loading
Loading