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 README.md
Original file line number Diff line number Diff line change
Expand Up @@ -252,7 +252,7 @@ bili codex # launch codex through the proxy
bili claude # launch claude through the proxy
bili omp # pi-style, file-free (#535): env + extension registerProvider + compaction cancel, real ~/.omp untouched
bili opencode # OpenCode (1.x & 2.x): full guide in the [OpenCode](docs/clients.md#opencode) section below
bili hermes # file-free (#535): hermes proxy env (HTTPS_PROXY + combined CA bundle via SSL_CERT_FILE) — https via CONNECT MITM, http via absolute-form forward proxy; real ~/.hermes untouched
bili hermes # file-free (#535): hermes proxy env (HTTPS_PROXY + HTTP_PROXY per scheme (#5116) + combined CA bundle via SSL_CERT_FILE; loopback direct via NO_PROXY) — https via CONNECT MITM, http via absolute-form forward proxy; real ~/.hermes untouched
bili dsh # deepseek-harness: full native plugin injected via --patch (#941) — real dsh tools, session-bound /acp + /acp-cache (plugin mode); non-loopback upstreams ride proxy envs, loopback keeps the overlay DSH_HOME rewrite (#535); dsh auto-compaction off everywhere — host plane via the bundle patch, web-profile presets at load time (#1772/#2474/#3354)
bili codebuddy # Tencent CodeBuddy Code CLI: CODEBUDDY_BASE_URL /bili/ rewrite (OpenAI chat completions wire), budget aligned via CODEBUDDY_AUTO_COMPACT_WINDOW; real ~/.codebuddy untouched
bili qoder # qoder CLI (`qoder`/`qodercli`): model endpoint is hardcoded https (no /bili/ rewrite possible) — cert-MITM via HTTPS_PROXY + NODE_EXTRA_CA_CERTS, default model hosts whitelisted (#653). Qoder Desktop is a separate BYOK/custom channel that speaks plaintext OpenAI over a `/bili/` prefix — point it at bili as a normal custom provider (not this MITM lane); its official-subscription traffic is encrypted and out of scope (#764)
Expand Down
11 changes: 8 additions & 3 deletions docs/clients.md
Original file line number Diff line number Diff line change
Expand Up @@ -302,9 +302,14 @@ pure-stdlib Python module shipped inside the npm package:
it; parent-pid watchdog tears it down when
hermes exits; concurrent starts arbitrate through the same starting-marker
protocol the launcher uses). Only once the proxy is verified healthy does it
point hermes' httpx stack at it via `HTTPS_PROXY` / `https_proxy` +
`SSL_CERT_FILE` (bili's combined CA bundle — current hermes resolves ambient
trust there; `HERMES_CA_BUNDLE` stays set for older builds) —
point hermes' httpx stack at it via `HTTPS_PROXY` / `https_proxy` (https)
+ `SSL_CERT_FILE` (bili's combined CA bundle — current hermes resolves ambient
trust there; `HERMES_CA_BUNDLE` stays set for older builds). Plain-http
providers additionally ride `HTTP_PROXY` / `http_proxy`, loopback kept direct
in `NO_PROXY` / `no_proxy` (defaulted when the user hasn't set one) — #5116:
HTTPS_PROXY alone left them invisible to bili; the plugin-lane half of that
fix ships in #5117, while the `bili hermes` launcher lane already routes both
schemes —
`~/.hermes/config.yaml` is never touched. Provider https hosts are read from hermes' config and whitelisted
for MITM; everything else blind-tunnels exactly like launcher mode. If no
proxy can be made healthy, the plugin stands down silently and traffic goes
Expand Down
4 changes: 2 additions & 2 deletions docs/configuration.md
Original file line number Diff line number Diff line change
Expand Up @@ -2362,7 +2362,7 @@ How each client is pointed at the proxy (set automatically in the child env):
| codex | `HTTPS_PROXY` + `-c key=value` overrides | `SSL_CERT_FILE` → `combined-ca.pem` |
| claude | `ANTHROPIC_BASE_URL` = `/bili/` URL (Vertex: `ANTHROPIC_VERTEX_BASE_URL`, when `CLAUDE_CODE_USE_VERTEX=1`; + `NO_PROXY`/`no_proxy` loopback merge, #3446) | none needed |
| opencode | `HTTPS_PROXY` + isolated `OPENCODE_CONFIG` | `NODE_EXTRA_CA_CERTS` |
| hermes | `HTTPS_PROXY` (plain-http rides absolute-form forward-proxy requests) | `SSL_CERT_FILE` → `combined-ca.pem` (+ legacy `HERMES_CA_BUNDLE` → `root-ca.pem`) |
| hermes | `HTTPS_PROXY` (https) + `HTTP_PROXY` (plain-http rides absolute-form forward-proxy requests, #5116) + `NO_PROXY` loopback | `SSL_CERT_FILE` → `combined-ca.pem` (+ legacy `HERMES_CA_BUNDLE` → `root-ca.pem`) |
| dsh | `HTTPS_PROXY` (+ `HTTP_PROXY` for plain-http) + `DEEPSEEK_BASE_URL`; **loopback-only** isolated `DSH_HOME` | `SSL_CERT_FILE` → `combined-ca.pem` |

`NODE_EXTRA_CA_CERTS` *appends* to the built-in trust store, so it points at the MITM root alone (`root-ca.pem`). `SSL_CERT_FILE` *replaces* the default CA bundle, so for codex/dsh/hermes it points at `combined-ca.pem` — a bundle containing the MITM root **plus** the OS trust store and the Node public roots. The OS-store merge matters because some launched clients treat this file as their *entire* trust pool (codex's rustls HTTP stack replaces rather than appends, #1807): any direct, non-proxied connection of theirs must validate against this file alone, so the file must be a superset of what the OS itself trusts (Windows cert stores via PowerShell, macOS keychains via `security(1)`; Linux already reads the filesystem bundle; rebuilt at most once per 24 h). Keeping pip/git/curl style TLS (blind-tunnelled, real certificates) working inside the child env is the original reason for the bundle (#152; hermes since #1375, because current hermes resolves ambient trust only through `SSL_CERT_FILE`; OS-store merge since #1807).
Expand All @@ -2387,7 +2387,7 @@ The launcher prefers file-free injection (env vars > CLI flags/extension APIs >

- **pi / omp** — nothing is written (#535): provider baseUrls ride the `BILI_PROVIDER_REWRITES` env manifest consumed by the bili extension at load (`registerProvider`), and auto native compaction is cancelled in-extension (`session_before_compact`; omp distinguishes auto vs manual via the `auto_compaction_start` announcement, #851) — but only on positive evidence the proxy actually carries the conversation (the plugin stamped `x-bili-plugin-conversation` for this session id, or omp's identity register succeeded, or `/__bili/plugin/status?conversationId=` confirms it); non-http(s) provider baseUrls (e.g. pi-claude-bridge's literal `"claude-bridge"`) are never cancelled by default, so their own compaction takeover keeps working (#1382); a provider can be opted in via its entry in the `providers` table — key = the provider id (a non-URL key is inert for routing), field `"compactionOptIn": true` — or `BILI_NON_HTTP_PROVIDERS` (env, comma-separated) — but opt-in only widens the candidate set, so even an opted-in provider is cancelled only on the same positive carriage evidence above (#1392) — manual `/compact` stays user-owned either way. The real `~/.pi` / `~/.omp` homes are untouched.
- **opencode** — a temp `opencode.json` pointed at by `OPENCODE_CONFIG` (removed when the client exits), with `/bili/`-rewritten plaintext baseURLs **plus the thin plugin appended** (`/acp` + `/acp-cache` commands). On OpenCode 1.x the `opencode-acp` entries are stripped from the clone (the host must not load it armed) and the thin plugin imports that same package as a library instead, gated on legacy sessions; the first stripped spec rides along via `BILI_OPENCODE_ACP_SPEC` so the bridge imports the exact copy the host would have loaded (#920). Relative local plugin specs (`./x`, `../x`) are re-anchored to absolute paths in the clone — opencode resolves them against the declaring config file's dir, which the clone no longer is (#826).
- **hermes** — nothing is written (#535): its httpx stack rides `HTTPS_PROXY` (+ `SSL_CERT_FILE` → `combined-ca.pem`; legacy `HERMES_CA_BUNDLE` stays set for older builds, #1375) — https via CONNECT cert-MITM, plain-http via absolute-form forward-proxy requests. If no providers are configured, the launcher prints a warning and hermes runs **unproxied** (compression off).
- **hermes** — nothing is written (#535): its httpx stack resolves proxy envs per scheme (#5116) — https via `HTTPS_PROXY` (CONNECT cert-MITM), plain-http via `HTTP_PROXY` (absolute-form forward-proxy requests); `NO_PROXY`/`no_proxy` keep the loopback legs (local model endpoints, MCP) direct (+ `SSL_CERT_FILE` → `combined-ca.pem`; legacy `HERMES_CA_BUNDLE` stays set for older builds, #1375). If no providers are configured, the launcher prints a warning and hermes runs **unproxied** (compression off).
- **dsh** — split by destination (#535): dsh's fetch stack honors proxy envs except for an unconditional loopback bypass, so **non-loopback** upstreams ride `HTTPS_PROXY` (cert MITM) / `HTTP_PROXY` (absolute-form forward-proxy requests) with `SSL_CERT_FILE` → `combined-ca.pem`; only **loopback** upstreams keep the persistent overlay `DSH_HOME` (`~/.dsh-bili`) with a rewritten `settings.yaml` routing them through `/bili/`. `profiles/`, credentials and sessions are symlinked through; the real `~/.dsh` is never touched. Home-root SQLite databases are the one exception to sharing: each launch gets a private copy in the overlay, merged back as a unit on exit (a file-linked main db would let the two paths grow independent WALs over one inode — #1917). The built-in `deepseek-official` route is captured separately via `$DEEPSEEK_BASE_URL` (dsh resolves `settings llm-deepseek.baseURL` ?? env ?? default, so a user setting wins and the env is the zero-config fallback) — with no custom providers the deepseek route is still proxied out of the box.
- **codex** — a persistent overlay `CODEX_HOME` (`~/.codex-bili`, or `<CODEX_HOME>-bili`) whose other entries (auth, sessions, model settings) stay shared links into the real home. The exception is home-root SQLite databases (`*.db` / `*.sqlite` / `*.sqlite3` — codex keeps its logs/state there): each launch gets a private copy in the overlay, merged back into the real home as ONE unit on exit (newer main-db generation wins; the loser is preserved as `<name>.bili-conflict`). File-linking those dbs across the two homes lets two paths grow independent WALs over one inode — committed writes lost or the database corrupted (#1917). Two generated files: (a) when MCP injection is on, a merged `config.toml` — real contents plus the per-spawn `[mcp_servers.bili]` block (inline `-c` values cannot survive cmd.exe quoting on Windows, #681); (b) since #1802, a generated `.env` (mode 0600) that re-pins THIS launch's `HTTP_PROXY` / `HTTPS_PROXY` / `ALL_PROXY` / `NO_PROXY` / `SSL_CERT_FILE` / `BILLION_CONTEXT_PROXY`: codex's own `load_dotenv()` overwrites the launcher's spawn env from `$CODEX_HOME/.env` **after** start, so a user `.env` pointing at e.g. a socks5h proxy would otherwise silently re-route codex off bili (and codex's custom-CA rustls HTTP stack cannot speak SOCKS at all). Your other `.env` variables are carried over verbatim; pre-existing shared `.env` links are migrated to an owned file; the real home's `.env` and every other real-home file are never touched. Direct-URL mode (`BILI_LAUNCHER_DIRECT=1`) generates no `.env` (there is no injected proxy to protect) and, without MCP, no overlay at all.

Expand Down
16 changes: 16 additions & 0 deletions src/acp-status.ts
Original file line number Diff line number Diff line change
Expand Up @@ -231,6 +231,22 @@ export function handleAcpStatus(args: Record<string, unknown>, ctx: AcpStatusCtx
if (estTotal > 0) billedLine += ` · est-view total ${estTotal} tok · ratio ${(billed / estTotal).toFixed(1)}×`;
extra.push("");
extra.push(billedLine);
// #5021: occupancy anchor. BILLED INPUT is an absolute count with no
// denominator, and the kernel's COMPRESSED BLOCKS header prints a
// LIFETIME "original" total right above it — an agent that does not
// know the route's window reads both as current occupancy and
// concludes the context is nearly full (measured incident: a session
// at 116,902 of a 1,000,000-token window reported "context exhausted"
// to its user and stopped working at 11.7%). One line answers "how
// much is left" without the agent having to guess a domain it cannot
// see: the window only lives in this process's config.
const occupancyLimit = ctx.config.modelContextLimit;
if (occupancyLimit > 0) {
const used = Math.max(0, billed);
const occupancyPct = (used / occupancyLimit) * 100;
const remaining = Math.max(0, occupancyLimit - used);
extra.push(`CONTEXT: ${used} / ${occupancyLimit} tok used (${occupancyPct.toFixed(1)}%) · ${remaining} tok remaining of the input budget`);
}
if (estTotal > 0 && billed / estTotal >= 1.5) {
extra.push("NOTE: the token figures in the report above are local estimates and run well below what upstream actually bills (tokenizer-dependent; CJK-heavy content is the usual cause). Judge context pressure from BILLED INPUT; use the breakdown only to locate what to compress.");
}
Expand Down
15 changes: 11 additions & 4 deletions src/launcher.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6764,10 +6764,14 @@ export async function runLaunch(params: RunLaunchParams, deps: LauncherDeps = {}
// #535 phase 2: file-free — no overlay HERMES_HOME, no config.yaml
// runs on its REAL home — including a user-set HERMES_HOME (discovery
// resolved the same path, so the MITM whitelist matches). Its httpx
// stack resolves ONE proxy env var (HTTPS_PROXY first) for both
// schemes: https upstreams ride CONNECT + cert MITM, plain-http
// upstreams ride absolute-form forward-proxy requests the server
// understands. CA trust (#1375): current hermes resolves the main
// stack resolves proxy envs PER SCHEME (#5116, field-verified on Hermes
// v0.22.5): https upstreams read HTTPS_PROXY (CONNECT + cert MITM),
// plain-http upstreams read HTTP_PROXY (absolute-form forward-proxy
// requests the server understands) — HTTPS_PROXY alone left plain-http
// providers going straight out, invisible to bili. NO_PROXY keeps the
// loopback legs (local model endpoints, MCP) direct; the env was
// stripped above, so the static constant is the whole value. CA trust
// (#1375): current hermes resolves the main
// client via agent/ssl_verify.py — platform store + per-provider
// ssl_ca_cert, ambient trust only through SSL_CERT_FILE (OpenSSL
// replace semantics → the COMBINED bundle keeps blind-tunnelled hosts
Expand All @@ -6777,6 +6781,9 @@ export async function runLaunch(params: RunLaunchParams, deps: LauncherDeps = {}
// carrier is present (anonymous requests are accepted, #286).
env = stripInheritedProxy(process.env);
env.HTTPS_PROXY = origin;
env.HTTP_PROXY = origin;
env.NO_PROXY = LAUNCHER_LOOPBACK_NO_PROXY;
env.no_proxy = LAUNCHER_LOOPBACK_NO_PROXY;
env.HERMES_CA_BUNDLE = ca;
env.SSL_CERT_FILE = resolveCombinedCaPath(process.env);
if (routes.httpRewrites.length === 0 && routes.httpsDomains.length === 0) {
Expand Down
101 changes: 101 additions & 0 deletions tests/issue5021-context-occupancy-anchor.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,101 @@
import assert from "node:assert/strict";
import test from "node:test";

process.env.NODE_ENV = "test";

import { defaultConfig, createCore, createInitialState, assignRefs, emptyRefMap } from "acp-kernel";
import type { CoreMessage } from "acp-kernel";
import type { Session } from "../src/session.ts";
import { handleAcpStatus } from "../src/acp-status.ts";

// #5021: acp_status printed an absolute `BILLED INPUT … tok` with no
// denominator, and the kernel's `COMPRESSED BLOCKS — … original` header reads
// like current occupancy. An agent that cannot see the route's window therefore
// has no way to answer "how much context is left" — one measured incident
// reported "context exhausted" at 116,902 / 1,000,000 tok (11.7%) and stopped
// mid-task. These tests pin the occupancy anchor that removes the guesswork.

interface CtxOverrides {
lastInputTokens?: number;
source?: string;
}

function textMsg(id: string, role: "user" | "assistant", text: string): CoreMessage {
return { id, role, contentType: "text", text };
}

function makeCtx(limit: number, messages: CoreMessage[], o?: CtxOverrides): {
core: ReturnType<typeof createCore>;
config: ReturnType<typeof defaultConfig>;
messages: CoreMessage[];
session: Session;
} {
return {
core: createCore(),
config: defaultConfig(limit),
messages,
session: {
id: "i5021-ctx",
meta: {},
stats: {
requests: 0, tokensSaved: 0, inputTokens: 0, cachedTokens: 0, outputTokens: 0, cacheSamples: 0,
lastInputTokens: o?.lastInputTokens ?? 0,
...(o?.source !== undefined ? { lastInputTokensSource: o.source as never } : {}),
compressCreditTokens: 0, contextTokens: 0, retrieveCalls: 0, retrieveHits: 0, retrieveMisses: 0,
storedBytes: 0, storeBytesSaved: 0, rangeRestores: 0,
},
metadata: {},
state: createInitialState(),
createdAt: Date.now(),
lastSeen: Date.now(),
blockContents: new Map(),
inFlight: 0,
persisted: false,
pendingRetrievals: [],
} as unknown as Session,
};
}

function asciiMsgs(count: number, charsPerMsg: number): CoreMessage[] {
const msgs: CoreMessage[] = [];
for (let i = 1; i <= count; i++) msgs.push(textMsg(`raw_${i}`, i % 2 === 1 ? "user" : "assistant", "x".repeat(charsPerMsg)));
return msgs;
}

function withRefs(ctx: ReturnType<typeof makeCtx>): ReturnType<typeof makeCtx> {
ctx.session.state.messageRefs = assignRefs(ctx.messages, { existing: emptyRefMap(), nextIndex: 0 }).map;
return ctx;
}

// 12 × 4000 ASCII chars → est-view total exactly 12000 tok (chars/4).
const MSGS = () => asciiMsgs(12, 4000);

test("#5021: acp_status prints an occupancy anchor (used / limit / % / remaining) beside BILLED INPUT", () => {
const ctx = withRefs(makeCtx(200000, MSGS(), { lastInputTokens: 30000, source: "usage" }));
const out = handleAcpStatus({}, ctx).text;
assert.ok(out.includes("BILLED INPUT (upstream usage): 30000 tok"), `billed line present (got: ${out.slice(0, 400)})`);
assert.ok(
out.includes("CONTEXT: 30000 / 200000 tok used (15.0%) · 170000 tok remaining"),
`occupancy anchor rendered (got: ${out.slice(0, 600)})`,
);
});

test("#5021: no usage-grade anchor → no CONTEXT line either (never invent occupancy)", () => {
const ctx = withRefs(makeCtx(200000, MSGS(), {}));
const out = handleAcpStatus({}, ctx).text;
assert.ok(!out.includes("BILLED INPUT"), "no billed anchor → no BILLED line");
assert.ok(!out.includes("CONTEXT:"), "no billed anchor → no CONTEXT line");
});

test("#5021: an over-limit reading clamps remaining at 0 instead of printing a negative budget", () => {
const ctx = withRefs(makeCtx(200000, MSGS(), { lastInputTokens: 250000, source: "usage" }));
const out = handleAcpStatus({}, ctx).text;
assert.ok(out.includes("CONTEXT: 250000 / 200000 tok used (125.0%) · 0 tok remaining"), `over-limit anchor (got: ${out.slice(0, 600)})`);
});

test("#5021: a non-positive limit prints no anchor (guarded, not NaN)", () => {
const ctx = withRefs(makeCtx(0, MSGS(), { lastInputTokens: 30000, source: "usage" }));
const out = handleAcpStatus({}, ctx).text;
assert.ok(!out.includes("CONTEXT:"), "limit <= 0 → no CONTEXT line");
assert.ok(!out.includes("NaN"), "no NaN leaks into the report");
});
5 changes: 5 additions & 0 deletions tests/launcher.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -860,6 +860,11 @@ test("runLaunch hermes #535: proxy env routing, no HERMES_HOME overlay, real con
assert.deepEqual(exitCalls, [0]);
assert.ok(childEnv, "client spawned");
assert.match(childEnv!.HTTPS_PROXY ?? "", /^http:\/\/127\.0\.0\.1:\d+$/);
// #5116: per-scheme proxy resolution — plain-http upstreams read HTTP_PROXY (HTTPS_PROXY
// alone left them going straight out), loopback stays direct via NO_PROXY.
assert.equal(childEnv!.HTTP_PROXY, childEnv!.HTTPS_PROXY, "plain-http upstreams ride the same origin");
assert.equal(childEnv!.NO_PROXY, "localhost,127.0.0.1,::1");
assert.equal(childEnv!.no_proxy, "localhost,127.0.0.1,::1");
assert.ok(childEnv!.HERMES_CA_BUNDLE, "CA bundle exported");
assert.ok(
String(childEnv!.SSL_CERT_FILE).endsWith(path.join("billion-context", "ca", "combined-ca.pem")),
Expand Down
Loading