src/stream.ts rewriteSseStream and src/stream-openai.ts rewriteOpenaiSseStream have
zero production call sites: server.ts imported rewriteSseStream but never called it,
and the live proxy SSE path is runCompressLoop + pickAdapter (src/server.ts), whose
three adapters already pass text through createTagEchoFilter. They survived only
because five test files drove them directly, which is why #460 and #462 both tripped
over them — the cited "warn-only, then emitEvent(ev)" code is unreachable.
Removed: rewriteSseStream and its private machinery (routeEvent, emitToolReplacement,
buildStopReasonRewrite, parseSseEvent, emitEvent, BlockState, NOOP, SseEvent) plus the
now-unused normalizeSseLineEndings import; rewriteOpenaiSseStream and its private
machinery (newState, routeOpenaiEvent, buildOpenaiTail, extractDataLine, StreamState).
Kept: executeAnthropicProxyTool (used by rewriteJsonResponse), applyRanges (preflight,
compress-loop-responses, loop/core, stream-openai), refNum, type RewriteCtx, and both
JSON rewriters — all live.
Tests: deleted the five suites that existed only to feed the dead rewriters
(tests/proxy-anthropic-stream, tests/proxy-sse, tests/sse-fixtures — the latter two
were near-identical copies — and the two "OpenAI SSE rewriter" cases in tests/basic and
tests/proxy-basic) plus the tests/fixtures/*.sse payloads they alone consumed. Flagged
in the PR: fixture-level OpenAI passthrough-integrity coverage was bound to the removed
implementation; the live buffer-to-finish adapters carry their own coverage in
tests/loop-adapters.test.ts.
Verification: typecheck clean; npm test GREEN apart from the two known env failures.
Four gaps remained after #460/#466, all on proxy-mode paths that bypass the compress loop:
- G1: native Responses compaction turns (resetAfterSuccess) still pipeThrough UNSTRIPPED.
That turn is by definition the compression-triggered one, so its context necessarily
carries render tags — highest echo risk of any Responses stream. Now routed through
pipePluginResponsesWithStrip when the upstream answers text/event-stream.
- G2: rewriteResponsesJsonResponse (src/stream-responses.ts) never stripped render tags,
while the anthropic/openai JSON rewriters do. stripResponsesText now runs before the
compress loop, so the synthesized note is never edited and the no-compress early
return is covered. stripItemContent also learned to strip a top-level item text, not
only content[].text.
- G3: non-injected NON-STREAMING JSON (title-gen, ACP_NO_INJECT_TOOL, #353 bypass) still
pipeThrough raw. Now routed through pipePluginJson, whose session became optional so
usage accounting stays off — a tiny title-gen input_tokens would otherwise clobber
lastInputTokens through the last-wins mergeUsageSample and break compression
triggering.
- fast-path hole (#460 family, not recorded anywhere yet): the plugin passthrough pipes
skip the tag filter when a chunk holds no visible tag and nothing is pending. A render
tag whose OPENING straddles a chunk boundary — chunk one ends `...<ac`, chunk two
starts `p tokens="177" type="text">m00155</acp>` — matches no detector on any single
chunk, so the filter was never fed, never went pending, and the whole tag echoed to
the client. endsPartialTag keeps a chunk-ending tag-like tail out of the fast path;
the filter holds it and re-emits on the next push or at flush(), so prose is neither
delayed nor lost.
Verification: typecheck clean; npm test 906/908 (the two failures are the known
network/launcher env cases, identical on master); E2E codex core 4-phase and the
E2E_FORGE=1 native-compact phase both GREEN against the local sglang upstream.
PR #464 shipped the mixed-chunk fix but referenced #462 everywhere (test names, code comment); the bug it fixes is #463. Also fixes the two test call sites that kept the pre-reorder pipePluginChatWithStrip argument order once #466's signature landed.
plugin-mode openai-chat SSE strip (pipePluginChatWithStrip/processOpenai) latched droppedText per-field, so a chunk whose delta carried e.g. content='real answer' plus a pure render-tag echo in reasoning_content was dropped whole, losing the real content. Now the chunk is dropped only when EVERY managed text field strips to empty (keptText guard); the existing non-text payload check (finish_reason/tool_calls) is unchanged.
Requests that skip compress injection (OpenAI title-gen exclusion, ACP_NO_INJECT_TOOL, #353 stop_sequences classifier bypass) piped upstream SSE straight through to the client. A model echoing history-borne ACP render tags into prose leaked them to the client TUI, where they re-enter the conversation via replay and amplify (same mechanism as #457/#459).
Route those non-injected streaming responses through the same tag-strip pipes as plugin mode (#452), with no session passed: usage accounting must stay off here, or a title-gen call's tiny input_tokens would clobber lastInputTokens and break compression triggering for the main conversation.
- pipePluginChatWithStrip: session now optional (signature reordered protocol-before-session); usage sample applied only when a session is present
- pipePluginResponsesWithStrip: session now optional, same guard
- server.ts !useRewriter branch: strip pipe for streaming requests whose upstream response is text/event-stream; everything else stays raw pipeThrough as before
- tests/proxy-noinject-tag-strip.test.ts: anthropic injectTool:false echo split across deltas stripped; tag-free stream byte-identical; openai title-gen stream stripped with [DONE]/finish_reason intact
release v0.1.71
fix: strip plugin-mode render-tag echoes on chat-completions/anthropic passthrough (#206 parity)
release v0.1.70
fix(#451): restore plugin-mode nudge injection + honest diagNudge log
fix(#422): refresh post-compress re-request view so the model sees the fold
0.1.69 (225ceb4) gated all three nudge injection sites by !pluginMode on
the premise that "in plugin mode the agent owns compression". The agent
plugins (pi/omp) are pure protocol clients with NO nudge channel of their
own, so the gate silently disabled proactive compression in plugin mode —
leaving only preflight at the hard limit — while diagNudge kept logging
"INJECT" (the log lied about delivery, same class as #413).
- Drop !pluginMode from the anthropic/openai/responses nudge gates. The nudge
is an ephemeral trailing user message (not persisted, never enters the
agent's re-sent history, prefix-cache-anchor safe), so it belongs in BOTH
modes. Tool injection stays gated by !pluginMode (the agent provides tools).
- diagNudge now takes the actual willInject decision and prints INJECT only
when the nudge truly reaches the upstream payload, else ARMED-SUPPRESSED.
- README two-modes table: split the tools/nudge row (tools = none in plugin
mode, nudge = yes in both modes).
- tests/plugin-nudge-injection.test.ts: regression guard asserting the nudge
is a trailing user message in BOTH proxy and plugin mode when armed
(verified to fail 30!==31 against the old gate).
typecheck / test(901) / build green.
release v0.1.69
fix(#437): make upstream timeout idle-based so long healthy streams aren't truncated
fetchWithTimeout used a single TOTAL timer (UPSTREAM_TIMEOUT_MS = 10min)
that only cleared once the body was fully consumed. A healthy stream that
legitimately runs longer than 10 minutes (e.g. a DeepSeek generation via
bili dsh) was aborted at the 10-minute mark mid-stream: the for-await loop
threw, emitStreamError wrote 'stream error' + finish, and the client saw the
conversation truncated.
Re-arm the timer on every response-body chunk (via an idle-wrapped body), so
the timeout is IDLE-based during streaming: healthy long streams survive,
while a genuinely stuck stream (no chunk for timeoutMs) still trips the abort.
Time-to-first-byte bounding is unchanged. clearTimer/externalSignal semantics
are preserved and callers see an identical Response shape (status/headers/body).
fix(#429): openai chat — write merged terminal chunk once, don't leak finish_reason
Two independent defects in the openai adapter's parseStream, both reachable only
when the upstream merges delta.content and finish_reason into the SAME terminal
chunk (MiniMax-M3, kimi-k3, ...). The client's check is unconditional: any chunk
carrying content after finish_reason is set throws
"OpenAI Chat received content after the finish reason".
Defect 1 (single round, real tool call): the `if (finishReason)` block yielded
the raw chunk as meta but did not `continue`, so control fell through to the text
branch and the SAME bytes were written a second time. Add the `continue`.
Defect 2 (multi round, proxy tool): a round that only called proxy tools is not
terminal (core.ts reRequest) and emits no completion, but its merged chunk still
reached the wire carrying finish_reason, so round 2's text landed after the finish.
Invariant enforced: a verbatim passthrough chunk must never carry finish_reason and
must be written at most once; the sole authoritative completion is the final
round's emitCompletion — except a real-tool-call round (suppressCompletion), where
the verbatim terminal chunk IS the completion and keeps finish_reason, written once.
- stripFinishReasonChunk(): blank finish_reason on a passthrough chunk
- reasoning/text/meta branches: strip when the chunk carries finish_reason
- sawRealToolCall branch: keep verbatim + continue (no double-write)
Tests: tests/openai-finish-reason-dup.test.ts (4 cases: both defects + text-only
and reasoning regression guards). Full suite 896/0, typecheck + build clean.
fix(#377): re-voice mid-stream acp_summary as user messages (user-carrier)
README "Two compression modes" table: add a row for proxy-injected tools +
nudge (plugin mode: none; proxy mode: the 4 context tools + nudge). AGENTS.md
item 6: "suppresses tool injection" -> "suppresses tool + nudge injection".
The nudge (a trailing user message prompting compression) was not gated by
!pluginMode, so in plugin mode the proxy could inject a redundant nudge even
though the agent owns compression (its own overflow self-heal). Gate all three
nudge injection sites (anthropic/openai/responses) by !pluginMode, matching the
injectTools gating, so plugin mode is fully clean: no tool injection, no nudge
injection, summary carrier = the agent's own compress tool call.
Replace hoistMidSystemMessages (hoisted mid-stream system messages to the head
as separate system messages) with systemToUser (converts them to role "user",
leaving them at their anchor position).
Why: strict OpenAI-compatible backends (SGLang) require exactly ONE system
message at index 0 and reject a mid-conversation or second system message with
400 "System message must be at the beginning" (#377). The kernel renders each
compressed block's summary as role "system" anchored mid-conversation; in proxy
mode (plain client) the compress tool call is ephemeral and preflight blocks
have none, so the acp_summary is the only carrier. Re-voicing it as a "user"
message satisfies SGLang's one-system rule AND keeps the head system message
(the prefix-cache anchor) byte-stable across compress turns — a new block no
longer invalidates the whole-conversation prefix.
In plugin/launcher mode the summary carrier is the compress tool call (in the
agent's own re-sent history), so the kernel's acp_summary is stripped by
stripKernelSummaries and systemToUser is a no-op there.
- src/util.ts: hoistMidSystemMessages -> systemToUser
- src/server.ts, src/loop/adapter-openai.ts: call sites + two-mode comments
- README.md: new "Two compression modes" section (carriers, why they differ,
why user not system/tool-call, coexistence determination)
- AGENTS.md: Key Design Decisions item 6 (two modes) + Before Making Changes
item 4 (every wire-touching change must be reasoned in BOTH modes)
- tests: new system-to-user.test.ts (7 tests), updated e2e-mid-system.test.ts
(summaries must ride on user messages, not leak into system), deleted
hoist-mid-system.test.ts
release v0.1.68
fix: omp plugin install/remove idempotency (scoped match, existence check, overlay redirect)
fix: stateless decompress — pure retrieval, no deactivate, cache retained (#400)
fix: /bili/ tunnel destination admission + management-plane tunnel block (#409)
The /bili/<absolute-url> zero-config branch forwarded to ANY destination
with no host/scheme admission — including the proxy's own /__bili/
management plane (the tunnel's inner connection originates from the proxy,
so the loopback admin gate passes; PoC in #409), link-local metadata
addresses, and, on --host 0.0.0.0 deployments, anything reachable from
this machine: a LAN peer could PUT /__bili/config over the tunnel and
rewrite upstream routing to exfiltrate API keys, despite the
'management stays loopback-only' promise. The CONNECT side already had
its gates (mitm.ts) — this adds the /bili/ counterpart:
- src/tunnel-guard.ts: destination × client admission. Self (same port on
any local address incl. 127/8) and link-local/metadata (literal or via
DNS resolution of the destination name) are ALWAYS denied. Loopback/
private destinations pass for loopback clients (the self-hosted
upstream case the launcher httpRewrites flow depends on: sglang
127.0.0.1:8199, ollama 11434) and are denied for remote clients unless
listed in BILI_TUNNEL_ALLOWED_HOSTS (host or host:port). Public passes.
- forward() stamps x-bili-tunnel: 1 on every tunnel forward; /__bili/*
rejects any request carrying it — the management plane stays
unreachable through the tunnel even when self-detection misses (NAT
hairpin, DNS-rebinding race between check and connect, chained bili).
- Startup logs the allowlist when set (the #405 silent-knob lesson).
Verified: typecheck, 891/891 tests (10 new), build; integration tests
reproduce the #409 PoC (403) and confirm 169.254.169.254 is denied
before any socket, local loopback upstreams still proxy, and the marker
reaches the upstream.
feat: instance governance — attach, liveness, handshake, graceful exit, atomic state (#417)
CI caught the stale assertion: win32 stopProxy is intentionally a no-op
(child.kill is TerminateProcess, zero flush); the test now pins that
contract on both platforms instead of assuming POSIX behavior.
L1 identity: proxy-origin file upgraded to JSON {origin, instanceId, pid,
startedAt, config fingerprint, launchToken} with atomic writes and legacy
plain-string read compatibility; /__bili/health echoes instanceId/pid/startedAt
(#403 read side).
L2 attach-or-spawn (#394): ensureProxyRunning attaches to a healthy,
config-compatible instance instead of doubling writers over one sessions dir;
stateDir instances.json registry prunes dead pids and warns loudly on dual
live instances.
L3 handshake (#407): no more probe-release-rebind TOCTOU — the child self-
binds, retries EADDRINUSE (+1 up to 16, then ephemeral) when launched with
BILI_LAUNCH_TOKEN, and reports the real origin via the instance file; forced
--debug on spawned proxies removed (12MB/day logs).
L4 graceful exit (#414): launcher children watch BILI_PARENT_PID and run the
flush path when the parent dies (Windows TerminateProcess never flushed);
stopProxy is a no-op kill on win32 (the watcher owns shutdown) and leaves
attached instances alone.
L5 state hygiene (#406, #403 write side): plugin-conversations.json gets
tmp+rename atomic writes, a dirty flag (dying paths no longer rewrite the
table), and corrupt-byte preservation + warning instead of silent zeroing;
plugin install refuses to freeze dead/unverifiable origins into client
configs (BILI_MCP_PROXY still wins).
L6 overlay antidote (#410): mergeOverlayEntry threads the generatedFile
exclusion through directory recursion (nested models.yml can no longer be
promoted into the real home); dead proxy URLs baked into a real models.yml
are unpacked mechanically on launch (live origins kept).
Three defects in the omp plugin idempotency checks (issue #392):
1. Whole-file, unscoped line matching: install/remove/status compared
every line of config.yml against the entry value, so a same-valued
line under any other key was counted as installed (blocking a needed
install) or deleted (corrupting unrelated config). Matching is now
scoped to the top-level extensions: block via ompExtensionItemLines().
2. No existence check, disagreeing with the loader: the launcher's
ompPluginLoadedFrom() uses a suffix match + fs.existsSync, but
install/remove used exact-string compare with no existence check.
A stale entry (target file gone) was reported 'installed' while the
launcher treated it as unloaded. All four paths now share OMP_ENTRY_RE
+ ompBlockLoaded(); status additionally reports 'broken' for a stale
entry.
3. PI_CODING_AGENT_DIR drift to the overlay: inside a 'bili omp' shell the
env points at the <home>-bili overlay whose config.yml can be a stale
copy (merge conflict leaves a .bili-conflict). ompConfigFile() now
detects the -bili overlay, redirects to the real home, and prints a
note about which file was actually touched.
Tests: existing roundtrip fixtures now materialize the gitignored dist
artifact (the existence check needs it); new test covers all four
acceptance criteria (out-of-block line doesn't block install, remove
preserves out-of-block lines, overlay redirect leaves overlay untouched,
stale entry reports 'broken').
release v0.1.67
fix: Windows overlay symlink EPERM fallback + fail-loudly + SQLite set merge
Unprivileged Windows processes lack SeCreateSymbolicLinkPrivilege, so
fs.symlinkSync throws EPERM and refreshOverlayHome's bare catch{} swallowed
every failure while still returning true — the bili omp/pi/hermes/dsh overlay
ended up hollow (only the generated config + pid marker, zero links), so the
client read an empty home and launched fresh/unconfigured every time (the
Windows root cause of #289).
- linkOverlayEntry: on EPERM/EACCES/EINVAL fall back per entry type — dir ->
symlink 'junction' (no privilege needed), file -> hardlink (same volume) ->
copy. The overlay now works without Developer Mode.
- refreshOverlayHome: track link success/failure per entry; when the overlay
comes out hollow (no entry resolvable) log a loud warning and return false
so the launcher aborts instead of pointing the client at an empty home.
Partial failures are reported, not silently dropped.
- mergeSqliteSet: move the SQLite three-piece set (.db/.db-wal/.db-shm) as a
unit with two-phase rollback — a locked/half-migrated set can no longer
corrupt the real home db.
- A write-through hardlink (nlink>1, same dev+ino as the real file) is
re-pointed on refresh, never merged back (data-loss guard).
Tests: 6 new cases in tests/launcher.test.ts simulate the Windows EPERM
condition on Linux by denying fs.symlinkSync for non-junction links.
typecheck / test (868) / build all green.
release v0.1.66
feat: surface compress rejection reasons to the model (#362 ③) + acp-kernel 0.0.47
- parseCompressInput now returns {ranges, diagnostics} so callers can
see WHY entries were dropped (kernel 0.0.47 invalidReasons)
- applyRanges failure receipt includes kind + dropped count + per-entry
reasons (max 8, 200 chars each) — the model sees exactly which entry
was rejected and why, instead of blind-retrying the same bad shape
- bump acp-kernel to 0.0.47 (persist retry backoff + spill files +
tmp-name-per-attempt; exact pin per AGENTS.md)
- .gitignore: tmp/ (e2e sandbox dirs leaked into a commit earlier today)
fix: warn on tool-call XML fragments + close-side truncated acp echo (#361)
fix: surface raw-dump failures and rate-limit telemetry path noise (#362)
Detect the non-acp tool-call XML close-fragment family (</invoke>,
</tool_calls>, </parameter>, antml: namespace, JSON-escaped forms) that a
model may echo from the context — the same source as acp tag echo that
produces a "fake completion" (tool call written as text, no tool_use block).
Warn-only, NOT stripped: these fragments are indistinguishable from
legitimate prose discussing tool-call code (see #295 review), so stripping
would corrupt real content. A once-per-stream warn in the server streaming
loop makes the leak attributable in bili.log.
Also: close-side truncated acp tag handling (TRUNC_CLOSE) mirroring
TRUNC_OPEN; fix dangling RENDER_TAG_DETECT reference (regression); tests.
A varying query (?ts=...) would split one endpoint into unbounded keys,
defeating the suppression and growing the map without bound.
- Raw dumps (INCOMING/REQ/RES/SSE) that fail now log a rate-limited warn (first failure immediate, repeats suppressed within a 60s window, cumulative count) instead of failing silently. A stopped raw/ dump (disk full, locked dir) was previously invisible.
- The 'unrecognized path' telemetry warn (client event-report endpoints forwarded unchanged) is now rate-limited per path (first 3 at warn, one 'suppressed' notice, then silent) instead of logging every hit (~20k lines in one user's capture).
- No change to forwarding behavior; observability only.
typecheck clean; 845 tests pass; build success.
release v0.1.65
fix: omp pck stamp for chat-completions with max_tokens; anthropic wire consumes + strips pck (#268)
fix: 404 unknown /__bili/ paths + actionable MITM CA-rejection warning (#346)
fix: actionable no-valid-ranges compress error + defensive object tool args
fix: hoist mid-conversation system messages to the leading prefix on the OpenAI wire (#355)
fix: pass through Claude auto-mode classifier requests untouched (#353)
docs: align per-model context declaration precedence with actual resolution order
fix: per-route model context declaration outranks the models.dev registry (#344)
fix: show real CA path in web UI + Windows ~ expansion doc note (#342)
release v0.1.64
fix: default upstream proxy to direct; add gost-style -F forward flag (#346)
Address the two non-blocking review nits on #357:
- help/PR copy advertised gost's --forward but only -F is implemented
(per the 'only -F for now' decision); drop --forward from the help text.
- CHANGELOG/PR claimed 'config' proxy wins regardless of mode, but a
top-level proxy config still requires mode auto/manual (pre-existing);
correct the claim to only -F / BILI_UPSTREAM_PROXY.
- Unset upstreamProxyMode now resolves to "direct" (matches the web UI's
直连(默认) and ZCode's default) instead of falling through to env/system
proxy auto-detection. Set mode "auto" to follow the system/env proxy; an
explicit proxy (-F / BILI_UPSTREAM_PROXY / config) still wins regardless.
- Add `-F <url>` (gost-style --forward) to set the upstream proxy for a run;
works for launcher cmds (before the client name) and `start`. HTTP/HTTPS
only for now (SOCKS5 is a follow-up).
- Log the resolved upstream-proxy decision once per unique (host, proxy,
source) — `[upstream-proxy] <host> via <proxy> (source=<source>)` — with
non-public hosts masked (#255), so a silently-picked env proxy is visible.
- Fix the misleading `bili pi --mitm-domain ...` help example (that form
forwarded the flag to the client; the working form is `bili --mitm-domain ... pi`).
Tests: default-direct (unset mode ignores HTTPS_PROXY), auto-mode opt-in
still follows the env proxy, and parseArgs -F placement (before/after client,
start cmd, composition).
CONFIGURATION.md claimed the per-model context declaration is primary and the
models.dev registry is only a fallback for undeclared models. The code
(src/server.ts native-window resolution) actually checks the WARM registry
(peekWindow) BEFORE the per-model declaration (configuredWindow), so for a
relay/private host whose model is listed in the registry, the registry value
silently outranks the user's declaration. This mismatch is what made the
#351 user's 200K declaration get ignored in favor of the registry's 1M.
Document the real resolution order and steer users to
compress.modelContextLimit (highest-priority source, always wins) for
reliably pinning the effective window. Behavior-preserving (docs only).
#349: when the model emits an empty compress call ({} -> missing-content)
the proxy returned 'Check your startId/endId parameters' — misleading, since
the real problem is no content at all. Replace with an actionable message
that tells the model to re-issue with a non-empty content array so it can
self-correct on the re-request round.
Also harden the OpenAI adapter: fn.arguments was only accepted as a string;
a non-standard upstream emitting it as an object was silently dropped
(-> same missing-content failure). JSON.stringify object args now.
typecheck clean; 824/824 tests pass (incl. new #349 regression); build ok.
The CONNECT e2e suite covered the MITM happy path, the idle-tunnel timeout,
and the remote policy, but never a blind tunnel to a non-whitelisted host
passing bytes both ways — the exact path ZCode's official site (zcode.z.ai)
uses. Add it: a local TCP server stands in for the upstream, and the test
asserts the round-trip plus the new 'tunnel ... established (blind TCP)' log.
Confirms the tunnel itself is sound, so the #346 zcode.z.ai failure is on the
ZCode/network side, not a bili tunnel bug.
tunnelThrough only logged on connect-timeout/failure/close, so a tunneled
host (e.g. ZCode's official site zcode.z.ai) that connects but then fails at a
higher layer left the log completely blind. Log the established tunnel,
mirroring doMitm's 'tunnel established' line but distinguishable (blind TCP,
not decrypted).
test: widen e2e mock pacing to keep streaming-span margin on slow CI runners
fix: generate real changelog in GitHub Releases via generate_release_notes
release v0.1.63
feat: codex compact family — combined #322/#323/#324/#325/#317/#319 + UA unification
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, so
the injected bili MCP tools become silently invisible and the model
fumbles for them. Users had no way to discover BILI_LAUNCHER_PLUGIN=0.
launcherInjectMcp now falls back to wire-injected flat tools when the
codex upstream host is loopback/RFC1918/ULA/link-local/.local and
BILI_LAUNCHER_PLUGIN is unset; BILI_LAUNCHER_PLUGIN=1 forces plugin
mode. claude is unaffected (it flattens MCP tools itself).
ework's pass (656f90d..ccd7036) and my pass (d4fe1ba) fixed the same
findings concurrently. This reconciles them, best mechanism per finding:
- MAJOR (summary loss after trigger-form forge): hybrid. The echoed
fc_bili_ item is REPLACED by a summary-carrying user message
(history-borne: kernel-compressible, kept by codex's user-message
retention) — and ework's forge-time capture into
session.metadata.codexForgedSummaries stays as the fallback: when the
echo does NOT come back, the captured summaries are re-injected into
the developer message. Exactly one path is active per turn (no
duplication), and neither an echoed-item loss nor unbounded permanent
injection can drop or bloat the handoff.
- tail-window cross-merge: keep ework's offset-0 guard (a head-to-head
match is never a truncation reattach) over the byte-size heuristic.
- endpoint compact: strip-on-all-paths (ework) + all-or-nothing state
commit with restore on gate-fail/rebuild-failure/error (mine).
- codexTurnIdentity: validated 'user' turns fall through to the legacy
precedence chain (stronger headers may own the id).
- pending plugin register can no longer be claimed by a subagent thread.
- codex window clamp exempts an operator's explicit
compress.modelContextLimit (operator tuning is never floored/clamped).
- trigger-form forge honors parsed.stream (JSON when not streaming) and
mints usage from max(lastInputTokens, payload estimate, 1).
- isCompactionTrigger reuses hasCompactionTrigger.
823/823 unit tests; full e2e incl. forge + pre-forge deep recall pass
against local sglang.
prepareResponsesCompact mutated state and then returned the original (unstripped) body on gate-fail/catch — the echoed fc_bili_ item reached upstream. Strip before the early return so pass-mode, gate-fail, and catch paths all forward a clean body.
Tail-window reattach now excludes offset 0: a head-to-head match is either a full-prefix case (step 1) or a distinct conversation sharing a templated head — never a truncation reattach (the retained suffix of a truncation starts strictly inside the stored chain). Closes the cross-merge of two anonymous conversations with identical templated heads.
The trigger-form forge put block summaries into the opaque compaction item, but the kernel deactivates blocks whose messages vanish from the truncated replay (syncBlocks keeps a block only while one of its effectiveMessageIds or its acp_summary_ message is present) — so everything that lived only in compressed blocks disappeared from the model's view on the first post-forge turn.
Fix: capture the active blocks' summaries in the kernel's render format at forge time and at echo-strip time (upgrade path for sessions forged by an older build) into session.metadata.codexForgedSummaries (persisted, exact-text dedup), and re-inject them into the developer message on every /responses turn.
Tests: unit coverage for renderForgedSummary/mergeForgedSummaries; mock-upstream integration test asserting the post-forge turn strips the echoed fc_bili_ item and re-injects the summary as a developer message; e2e forge phase now also recalls a sentinel that was compressed pre-forge (archive #6, not in codex's retained tail).
# Conflicts:
# src/server.ts
Model: glm-5.3 — test: e2e suite — real codex through bili (warmup/load/compress/purity/forge)
fix: warn when running process is stale relative to on-disk install (#327)
The /bili/ prefix section had no Claude Code entry. Add it (claude's undici
fetch ignores HTTPS_PROXY, so the /bili/ URL form via ANTHROPIC_BASE_URL is
the only manual option) plus a note that manual mode must set
CLAUDE_CODE_AUTO_COMPACT_WINDOW itself — the bili claude launcher does this
automatically (PR-D) but manual /bili/ users do not get the injection.
claude clamps the value down to its perceived model window, so over-setting
is safe. EN + zh-CN.
When BILI_CODEX_COMPACT=intercept, the client is codex (UA codex_cli_rs/),
and the ACP state is healthy (transform ok + steady-state < 90% of window +
an active block to hand off), bili forges a success response for codex's
native compaction requests and never contacts upstream — a deterministic
handoff to the ACP state:
- trigger form (input ends with compaction_trigger): forged 2-frame SSE
(response.output_item.done with one {type:compaction, id:fc_bili_*,
encrypted_content:bili:acp:<summary>} + response.completed with honest
usage). Matches codex's collect_compaction_output contract (exactly one
compaction item + response.completed; all other events ignored).
- endpoint form (POST /responses/compact): forged JSON {output:[...]} where
output is the rebuilt history (recent items + ACP summary), matching
codex's CompactHistoryResponse parser.
Otherwise (kill-switch off by default, ACP not keeping up >= 90%, or nothing
compressed yet) the request passes through to upstream and native compaction
backstops (rebased by the PR-C semantic gate). Bili-generated compaction
items (fc_bili_ prefix / bili:acp: sentinel) are stripped from subsequent
requests so codex's echoed item does not double-count.
Pre-flight: typecheck clean; 752/752 tests (7 unit + 5 e2e new); build ok;
forge code verified inlined in dist.
The rebase marker fired on ANY 2xx upstream response, so a failed,
incomplete, or truncated compaction stream scheduled a rebase on top of a
compaction that never happened (the #249 false-rebase bug): bili discarded
valid blocks and mis-tracked the ledger.
- new src/stream-terminal.ts: terminal-state observer for Responses
responses. SSE: response.completed -> completed, response.failed /
response.incomplete -> failed, no terminal event before EOF -> unknown.
JSON: output array -> completed, error field -> failed, unparseable /
empty / oversized -> unknown. unknown is the safe default (no rebase;
next request self-heals via kernel message-id deactivation).
- server.ts: both markNativeCompactionBoundary call sites now gate on
observed terminal === completed. 2xx empty body no longer rebases.
- trigger-path hardening in prepareResponses: a trailing
compaction_trigger request is detected BEFORE injection — no compress
system prompt, no compress tools, renderTags none, compressInjected
false (response flows through plain passthrough instead of the
rewriter), and resetAfterSuccess is set so its terminal state gates
the rebase. Nudge skip now keys off the same pre-injection detection.
- tests: 16 unit tests for the observer + e2e through the proxy covering
completed / failed / truncated / 2xx-error-JSON / empty-body /
JSON-output and the clean-forward assertions; finality-test control
moved to a separate session (a completed trigger now rebases the
compacting session, legitimately disarming its nudge).
Tail-pinned matching only reattached when the client retained <=
TAIL_WINDOW (8) items; a rolling-window client keeping 20-30 items
orphaned on every slide. Search the incoming's leading window across the
stored chain's per-item hashes at any offset (<= 57 offsets x 8 compares,
trivial). MAX_STORED_ITEMS 64 -> 128 so deeper windows stay searchable.
guardian_review / guardian_classifier (Feature string) / memory_consolidation
threads previously fell back to the legacy chain and landed on the root
session — the exact #150 isolation violation. Invert the discrimination:
only "user" joins the root session; every other thread_source with a valid
thread_id cross-check partitions by thread-id.
A client that drops its oldest messages (truncation) previously failed the
full-depth prefix match and was orphaned into a fresh pfa- session, stranding
the old session's compression blocks. resolve() now falls back to a
tail-window comparison: the incoming's leading items are matched against each
stored chain's trailing items (position-independent per-item hashes), so a
truncated replay reattaches its original session and the kernel reconciles by
message id (verified: blocks deactivate gracefully on truncation and revive on
full replay, no throw).
New pfa- sessions also record a best-effort lineage (discarded candidates +
reason truncated/forked) in session metadata for UI/debug only — never used
for matching. Live-edge overwrite semantics unchanged.
- src/prefix-affinity.ts: TAIL_WINDOW/MIN_TAIL_MATCH/MAX_STORED_ITEMS/
MIN_FORK_PREFIX; perItemHashes + lcpLength; resolve() gains tail-window
reattach + lineage; note() stores per-item hashes; AnonymousAffinity gains
via + itemHashes + lineage.
- src/server.ts: tail-window reattach log line; note() 4th arg; metadata
records via + lineage.
- tests/prefix-affinity.test.ts: note() call sites updated to 4 args.
- tests/tail-window-reconcile.test.ts (new): tail-window reattach, prefix/new
regressions, fork + truncated lineage, and a kernel reconcile anchor test.
Independent of PR-A (codex turn-metadata, #317). Pre-flight: typecheck OK,
746/746 tests, build OK.
Codex (>=0.147) reuses the root session-id header on subagent threads, so
the legacy identity chain mapped every thread of one task onto a single
session — subagents inherited the root's compression state, breaking #150's
isolation (a guardian subagent must read the user's original authorization
verbatim, never a compressed summary).
Add codexTurnIdentity(): when x-codex-turn-metadata is present and
cross-checked (metadata.thread_id === thread-id header), partition by
thread_source — "user" keeps the session-id header (current root
semantics, stable across turns), "subagent" gets its own thread-id (fresh
independent state per thread). Untrusted metadata (absent / unparseable /
mismatched / unknown thread_source) falls through to the legacy chain
unchanged, so no-metadata clients (omp pck, etc.) see zero regression.
The trusted id enters the verbatim session chain directly (no hashing, no
new dimensions) and bypasses subagentNamespace, leaving the kernel's
empty-instructions non-anchoring path untouched for metadata-less clients.
Tests: unit coverage for both thread_source values, cross-check mismatch,
JSON parse failure, unknown thread_source, missing headers, omp pck chain
unaffected; plus a server-level test proving subagent and root threads land
in distinct isolated sessions.
docs: add issue-work delivery requirements to AGENTS.md
release v0.1.62
feat: #189 compress observability + gated staged/tail-bias steering (smooth transition)
fix(logger): rotated log file orphan inode — reopen or degrade to stderr-only (#210)
release v0.1.61
fix: preflight compress fails fast on upstream errors instead of forwarding over-window payloads (#301)
fix: honor anthropic-beta context-1m to size the context window
fix: detect chained bili instances via x-bili-hop marker (#300)
fix: adopt complete terminal usage atomically in anthropic adapter (stitched-stream double-count)
release v0.1.60
feat: anonymous prefix-affinity session resolution (#309)
Clients that cannot send any identity signal (no headers, no body
session_id / prompt_cache_key) still replay their full history every
request. Resolve them by longest-prefix hash-chain affinity instead of
the #286 hard 400: h_i = sha256(h_{i-1} || msg_i) over the client-raw
messages, matched against per-partition (protocol | upstream | auth)
stored chains, fork-safe (divergent histories split on their next
request), deterministic ids (pfa-<hash16>) so identical replays
reattach after restart. Degenerate requests (empty / system-only)
keep the explicit 400. Logging on every resolution (session, match
depth, partition, tail hash).
- Observability (always on): applyRanges now records the last successful
compress (shrink ratio, fold point, blocks, tokens) on the session and logs
an [acp-compress-obs] line per compress.
- 3007 correlation: the replay/preflight retry callbacks now append a
[after compress: shrink N% foldPoint=X ...] suffix so a transient upstream
rejection is correlated with the rewrite that preceded it.
- Staged compression + prefix survival (gated behind BILI_MAX_SHRINK_PER_COMPRESS):
when a single compress shrinks context more than the configured fraction, the
tool result steers the model toward smaller, TAIL-biased ranges (keep the
stable prefix intact), and the nudge text gains a smooth-transition guidance
block. Off by default (legacy behavior).
- 10 new tests in tests/compress-observability.test.ts.
fix: tag-echo filter strips truncated tag fragments (#294)
When an Anthropic-format request carries an anthropic-beta header with a
larger-context beta (e.g. context-1m-2025-08-07), the client has negotiated a
1M-token upstream window. The proxy now sizes its own context window to match
(1,000,000), overriding the model-table default (200K for claude).
The beta is the most direct per-request evidence of the window the upstream
will serve, so it is resolved at the top of the native-window cascade and
re-read on every request (the header may appear/disappear between requests of
the same session). This fixes EMERGENCY / nudge / usage bands firing against
the wrong denominator — e.g. 261K input was 131% of 200K (EMERGENCY) when it
was really 26% of the 1M window the client negotiated.
- anthropicBetaContextWindow(): parses context-Nm betas (N x 1,000,000),
largest wins.
- e2e: EMERGENCY suppressed at 261K input under the 1M beta; fires at the same
input without the beta (200K). Plugin effectiveContextLimit is 1M with the
beta and drops to 200K when the header disappears (per-request).
When two bili instances are chained (A forward -> B), both run the
compression pipeline on the same request, double-compressing and
corrupting session state (root cause of #292).
- Outbound: stamp x-bili-hop: <instance-uuid> on every request this
instance actually processed (main forward + preflight summarization +
compress-loop calls via shared headers). A passthrough forward does NOT
claim processing, so a dumb-proxy bili in front of a processing one is
unaffected; any inbound marker keeps propagating down the chain.
- Inbound: if a request already carries x-bili-hop, an upstream bili already
processed it. Skip ALL processing (no tool/tag injection, no acp-loop, no
session state) and pass it through verbatim, logging a prominent [warn]
naming the upstream instance (self-loop vs foreign chain).
- e2e: two tests — B in isolation with a foreign marker (body reaches the
LLM byte-identical, no tool/tag injection, marker propagated), and the full
A->B chain (single-layer processing, B warns, one LLM request, marker is
A's not B's).
Clients never send this header, so single-instance use is unaffected.
release v0.1.59
test: normalize macOS /var->/private/var tmpdir paths in launcher rewrite tests
fix: recognize Codex context-overflow errors and learn conservative window when unparseable
release v0.1.58
fix: bili claude/codex auto-inject native MCP tools by default (#290)
refs #280
On macOS os.tmpdir() returns /var/folders/..., which is a symlink to
/private/var/folders/.... fs.realpathSync in the pi/omp http-rewrite
tests resolves through it, so the strictEqual path asserts compared a
realpath'd left side against an un-realpath'd right side and failed.
Canonicalize the mkdtempSync result up front; no-op on Linux.
Full suite 687/687 on Linux.
fix: keep Codex compaction_trigger final when injecting the compress nudge
docs: how to open PRs without the gh CLI (credential + REST API)
Documents the exact procedure the environment actually supports: no gh
binary, but git push works via the credential helper and the same
credential opens PRs through the GitHub REST API. This unblocked #282
(analysis was complete, PR creation was not).
Ultraworked with [Sisyphus](https://github.com/code-yeongyu/oh-my-opencode)
Co-authored-by: Sisyphus <clio-agent@sisyphuslabs.ai>
fix: floor fallback-derived context window at 100k after output-headroom reservation
fix: resolve registry window for relay hosts via provider-prefixed key scan
fix: body dumps off by default (ACP_DUMP_BODY) + reuse log-mask in dumps (#276)
feat: session id is the client-provided conversation value verbatim; 400 without identity (#286)
- dsh: force PI_CACHE_RETENTION=long so pi-ai stamps prompt_cache_key
(the dsh session uuid) on every request; the prompt_cache_retention
side-effect field is stripped proxy-side before forwarding upstream.
- hermes: write a bili-session-identity provider plugin into the
~/.hermes-bili overlay — it re-registers the bundled custom profile
(plus custom:<name> for every proxied provider) with the real hermes
session id as prompt_cache_key. Skipped when ~/.hermes already has a
plugins/ dir (never write through the overlay symlink).
- fix: prepareHermesHome now strips YAML quotes before matching api
values — quoted endpoints previously failed to rewrite silently.
- prompt_cache_retention stripped on the openai and responses paths
(OpenAI-host-only directive; third-party upstreams may reject it).
Close#290. BILI_LAUNCHER_PLUGIN now defaults to on for claude/codex
(both flags verified on claude 2.1.227 / codex 0.147.0 and soaked since
08-16); set BILI_LAUNCHER_PLUGIN=0 to force pure wire mode.
Strong-signal conversation identities (codex session-id/thread-id,
claude x-claude-code-session-id, opencode x-opencode-session,
Responses body session_id, prompt_cache_key) now derive the session id
from hash(protocol|conversation) only. The 4-dim hash
(protocol|upstream|credential|conversation) is retained for weak
signals (content fingerprints), where the credential/upstream
dimensions provide collision isolation.
Rationale: the credential dimension is a rotating OAuth bearer for
Codex/ChatGPT and the upstream dimension changes when users switch
relays — either change orphaned all compression state mid-session
(#280). The client's native session id is the only invariant bound to
the conversation.
One-time cost: existing persisted sessions are re-keyed once (old
records orphaned), accepted per issue discussion.
refs #280
Codex's native remote-compact request ends with a compaction_trigger
input item that the upstream requires to be the FINAL input item. The
compress nudge injection appended a user message after it, so any
nudge-armed session that hit a native compact got
400 "The 'compaction_trigger' item must be the final input item."
and the pre-sampling compact (and the turn) failed.
Skip the nudge when the final input item is a compaction_trigger —
the native compact IS the compression, and the developer-message /
tool injections (head of input) are unaffected.
Test: Responses e2e — trailing compaction_trigger stays final with no
nudge appended; control request keeps the nudge. Full suite 662/662.
closes#280
After a macOS sleep/wake (or any rebind), a restored session starts with
lastInputTokens=0, so the nudge is blind and only the preflight fit check
can catch an oversized replay. Two gaps left the Codex path stuck:
- Codex's overflow error (code context_window_exceeded / "ran out of room
in the model's context window") matched none of CONTEXT_OVERFLOW_PATTERNS,
so no self-heal ever armed and the same oversized payload was re-sent
forever.
- Even when the error is recognized, Codex's body carries no window number,
so parseOverflowWindow found nothing and the floor stayed 0.
Now: both Codex markers are recognized, and when no window is parseable the
proxy learns a conservative window from the rejected payload's own size
(only-shrink-never-grow). The next turn's self-heal re-centers the limit
and preflight compresses the history below it before forward.
Tests: 2 unit (Codex error recognition) + Codex e2e self-heal (400 without
window number -> learned window -> preflight fold on the retry) + new
Responses-protocol e2e for the issue's suggested restore scenario (fresh
session, lastInputTokens=0, raw history over the window -> preflight
compresses before forward). Full suite: 661/661 pass.
- Decouple all plaintext body dumps (dumps/req-*.json, raw/*-REQ.txt,
raw/*-RES.txt, raw/*-INCOMING.txt, req-*-REREQUEST.json) from --debug:
they are now opt-in via ACP_DUMP_BODY=1, so `bili <client>` users
(launcher hardcodes debug:true) no longer leak conversation bodies to
disk by default.
- Reuse maskHeadersForLog from src/log-mask.ts for dump header filtering:
now covers cookie/set-cookie (the old /key|auth|token/i regex missed
them) and non-public hosts.
- Mask the upstream URL in raw/*-REQ.txt (maskUrlForLog) and the incoming
path in raw/*-INCOMING.txt (maskUrlsInText) so non-public hosts don't
land in dump files.
- INCOMING dump now honors ACP_RAW_DUMP_DIR (was hardcoded to stateDir/raw).
- Add tests/dump-mask.test.ts: dumps off by default under --debug; with
ACP_DUMP_BODY=1 dumps carry no credentials and no non-public host; dumps
work without --debug.
tunnelThrough logged err.message verbatim; OS/undici error text embeds
the endpoint ("connect ECONNREFUSED 10.0.0.5:8443", "getaddrinfo
ENOTFOUND relay.internal"), re-leaking the host the template just
masked. Add maskHostInText() to log-mask.ts and apply it to the two
tunnel error log lines; formatUpstreamError now reuses the same helper
(also fixes the bracketed/bare IPv6 mismatch in its scrub).
+2 tests: unit coverage of maskHostInText, e2e CONNECT tunnel failure
asserting no raw target host in any captured log line.
bili.log and the launcher tmp log (bili-proxy-${port}.log) received
plaintext secrets and non-public upstream endpoints:
- 'forward GET/POST -> <upstreamUrl>' logged the full upstream URL on
every request (always-on, not debug-gated)
- '-> upstream headers:' / '<- upstream response headers:' logged
authorization / x-api-key / cookie / set-cookie values verbatim
- formatUpstreamError embedded the raw upstream URL and the failing
host (ECONNREFUSED 192.168.x.x, ENOTFOUND relay.internal)
- mitm/CONNECT logs exposed tunneled upstream host:port
New src/log-mask.ts:
- credential headers (key|auth|token|cookie) -> '<masked N chars>'
- non-public API hosts -> '<private-host>' (port kept); well-known
public endpoints (openai, chatgpt, anthropic, deepseek, googleapis,
azure, mistral, groq, ...) stay verbatim for debuggability
- userinfo/query/hash stripped from logged URLs (key-leak vectors)
- error message/address fields scrubbed of non-public hostnames
debug: true in the launcher is intentionally kept (per #255 triage);
dump-file gating and GC are tracked in separate issues.
release v0.1.57
fix: identity plugin binding survives model switches across sessions
fix: preflight-compress oversized context before forward on model switch
fix: stamp type:message on type-less Responses input items (omp wire form)
fix: omp chat-completions sessions get pck identity + /acp panel
release v0.1.56
fix: plugin passthrough review follow-ups (non-streaming strip parity, stream-cut tail flush, multi-line data, malformed parts)
regression: #257 omp native plugin mode + #258 drop whitespace-only Responses items
estimateCoreMessages/splitChunks/summary-cost used estimateTokensFast
(chars/4), which undercounts CJK ~4x. On a stable CJK session the loop's
fit-check therefore saw an undersized payload and broke without
compressing, forwarding an oversized body -> upstream 400. Use the
kernel's CJK-aware defaultCountTokens and floor the loop on the session's
measured lastInputTokens (which also covers system prompt + tool defs).
Adds a CJK e2e test (red before, green after). 633/633 pass.
A failed identity register left state.sid set, so the early-return guard
blocked every retry — the session stayed in wire mode (duplicate ACP tools)
for its whole life. Don't mark the sid done until the register succeeds, and
guard the tool loop with a separate toolsFor flag so a retry re-fetches the
manifest without re-registering tools. Make the retry interval injectable
(createBiliPlugin opts.retryIntervalMs) so the test can actually assert the
post-throttle retry.
omp 17.x mounts extension tools that omit loadMode under xd:// devices
(invisible to the main turn); declare loadMode:"essential" so the four
ACP tools reach the model natively. omp emits no before_provider_headers,
so bind the conversation via the launcher identity register (#162 path)
once tools are ready — bound sessions run in plugin mode with wire
injection suppressed. before_provider_request doubles as the retry
driver. Full native roundtrip e2e-verified against omp 17.3.8.
fix: net post-compress usage credit + hand launcher context windows to the proxy (#252)
release v0.1.55
merge: joint regression branch for PR#246 + PR#248 (dsh launcher + omp zero-config)
omp does not ship the bili plugin; the launcher now rides -e dist/agent/omp.js
when the config carries no loadable bili entry (mirrors pi, PR#227). omp keeps
wire-injected tools (its fork never surfaces extension tools to the model and
emits no before_provider_headers); the injected plugin provides the native /acp
command. Server log now prints the effective injectTools flag, not the raw one.
Stateless full-history replay clients without conversation headers (omp
without its plugin) minted a new session every request: the kernel identity
chain hashes the ENTIRE input array, which changes as the conversation
grows. New session per request meant the nudge always evaluated 0 tokens
(usage lands after the first forward), so 90%+-full contexts were never
compressed and the upstream affinity churned every turn. prompt_cache_key —
the client's own stable conversation id — now replaces only that
fingerprint; headers, body session_id and previous_response_id still win.
bili dsh now splices a --patch overlay (~/.dsh-bili/.bili-acp.patch.yml)
into dsh's argv, inserting dist/agent/dsh-acp.js (a cordis command plugin,
same shape as dsh-command-compact) into whatever profile boots: a real
/acp slash command on the web/tui surfaces.
- src/agent/dsh-acp.ts: /acp → status panel (fallback=latest), else
armed-but-idle info (#235 wording), else unreachable error
- src/agent/shared.ts: +fetchStatusLatest (conversationId=dsh placeholder;
server rejects the empty string)
- src/launcher.ts: +writeDshAcpPatch (independent of the settings rewrite,
exists with zero custom providers) +dshArgsWithPatch (prepend / after
'web' / skip for 'plugin' & --dump-default-config)
- tests: launcher 85 (incl. argv assertions in runLaunch dsh e2e),
tests/dsh-acp.test.ts 4 outcomes; 610/610
This reverts commit a2af88ffda.
- client-config: resolveDshHome / parseDshSettingsYaml / readDshConfig
- launcher: dsh route discovery, prepareDshHome overlay (settings.yaml line
rewrite, CRLF-preserving, siblings symlinked), runLaunch branch wiring
BILLION_CONTEXT_PROXY + DEEPSEEK_BASE_URL (built-in deepseek-official route)
- fix(openai adapter): emitCompletion usage numeric fields ?? 0 — undefined
prompt_tokens/completion_tokens serialized away and strict clients (dsh
mapUsage) compute NaN → non-JSON-serializable session events
- cli help, README/CONFIGURATION (en+zh), 7 new tests
release v0.1.54
[Fix] 限制 Responses round-2 重映射 message ID 长度
Sanitize over-long input item ids before the projection reads them so
conversations stuck on 400 recover on the next request. Complements the
root-cause fix (hashId remapping) in this branch.
fix: surface truncated upstream streams instead of silent end_turn
fix: allow remote clients when bound to a non-loopback host (#240)
- setupMitm: new allowRemoteClients flag; remote CONNECT accepted only for
whitelisted model hosts (blind tunnels stay loopback-only — no open relay)
- server: derive the flag from --host; honest 0.0.0.0 display + [security]
warning on non-loopback binds
- tests: #77 case extended to the full remote policy matrix
- docs: README/CONFIGURATION remote-agents section (en+zh)
Follow-up to #221 (PR #222): the reporter still sees the agent stop
while outputting a long conclusion. If the relay drops the SSE stream
mid-generation (before message_delta/message_stop), the loop received
no done event, finishReason stayed undefined, and emitCompletion
defaulted it to a clean end_turn — the client then ended the turn
mid-text with no indication anything went wrong, which reads as the
agent silently stopping.
Detect the abnormal termination (no done event in the round) and emit
an explicit [acp-proxy: upstream stream truncated (...)] error block
plus an error-level log, so truncation is visible and diagnosable
instead of masquerading as a normal turn end.
Refs https://github.com/ranxianglei/billion-context/issues/221
release v0.1.53
docs: README header badges + centered install command (match billion-context-pi)
release v0.1.52
fix(agent): /acp shows armed/idle info instead of warning before first request
A 404 from the status endpoint on a live proxy means the conversation
has sent no model request yet (e.g. /acp right after bili pi starts) —
not an error. Probe the manifest for liveness+version and show an info
notice; keep the old warning only when the proxy is unreachable.
release v0.1.51
fix: legacy billion-context-pi entry wrongly suppressed launcher -e injection
isPiEntry (PR #227) matched legacy npm:billion-context-pi entries as
'installed', so piPluginInstalled() returned true and the launcher
skipped -e — but that legacy package usually is not installed, leaving
pi with NO plugin at all (/acp gone). Add isBiliPiEntry() which only
matches billion-context itself; install/remove keep the broad isPiEntry
for legacy cleanup.
docs: recover deleted README usage detail into CONFIGURATION (en+zh)
fix: persistent overlay homes for pi/omp/hermes launchers (session history loss)
# Conflicts:
# README.md
# Conflicts:
# src/launcher.ts
docs: align README with the zh edition, drop plugin-install section
Review of the first overlay commit found three paths where overlay-created
data could be silently destroyed instead of merged back into the real home:
- a regular FILE shadowing a real-home entry was rm'd without merging
(client atomic-write-over-symlink lost its newer content)
- the dir merge was shallow: new files under subdirs that exist on both
sides were skipped and then deleted
- a type clash (real-home file vs overlay dir, or vice versa) aborted the
merge silently while the caller still rm'd the shadow subtree
mergeOverlayDir is replaced by a recursive mergeOverlayEntry that preserves
both sides (mtime-newer-wins for files, losers kept as
<name>.bili-conflict) and reports failure; the caller now removes the
shadow only when the merge fully succeeded, otherwise it is kept in place
with a warning. Also add a .bili-launch.pid marker that warns when two bili
launches share one overlay (each launch rewrites the generated config with
its own proxy origin).
Regression tests for all three loss paths plus the pid marker.
- English restructured to mirror README.zh-CN.md: Quickstart two ways
(launcher / URL change), Option 1 Launcher (intro + usage block only),
Option 2 URL change, no plugin-mode deep-dive, no mode matrix, no
redirect/CA or discovery tables, no temp-copy details, no subagent/
export sections.
- Drop the zh plugin-install note: bili pi now auto-injects the native
plugin via -e when not installed (#227), so install is never required.
- Status: mock test count 146 -> 500+.
fix: bili plugin install pi replaces legacy billion-context-pi entries
fix(loop): strip model-emitted render tags to break #206 echo feedback loop
The pi/omp/hermes launchers built their /bili/-rewritten config inside a
mkdtemp temp home (PI_CODING_AGENT_DIR / HERMES_HOME) and rm -rf'd it when
the client exited. Clients persist absolute session paths derived from that
temp home into shared state (e.g. omp ~/.omp/agent/terminal-sessions/pts-*
resume pointers), and any file the client created directly inside the temp
home (agent.db, models.db, ...) was destroyed on exit — making omp session
history invisible and leaking temp dirs when the process was killed.
Replace temp homes with stable persistent overlays next to the real home
(~/.omp/agent-bili, ~/.pi/agent-bili, ~/.hermes-bili): every launch
refreshes entry symlinks (dropping dead ones, merging overlay-created
entries that shadow real-home entries back into the real home) and rewrites
the generated config atomically. The overlay is never deleted, so paths
stayed resolvable across runs.
opencode/claude/codex launchers are unaffected (env/temp-file only).
- fix filter false-swallow after paired tags (wasPaired check)
- containsRenderTagText: detect JSON-escaped open tag with attrs
- non-stream gates use containsRenderTagText (catch bare lone tags)
- openai rewriteContentChunk preserves sibling choices
- add e2e tests (real server, compress round-trip) and non-stream tests
Models sometimes imitate the render tags injected into compressed history
and emit them in visible output. The client stores the echoed tags, replays
them next turn, and the imitation amplifies into unbounded repetition (#206).
Strip render tags from outgoing text in all three wire adapters (streaming
and non-streaming) with a streaming-safe filter; underscore-namespaced
text-protocol triggers and ordinary prose pass through untouched.
launcher pi branch prepends pi -e <dist/agent/pi.js> only when the user
has NOT run plugin install pi (mutually exclusive: pi dedups extensions
by resolved path, packages entry vs -e file would double-register).
Uninstalled users now get the native tool panel and /acp out of the box,
matching bili omp / bili opencode behavior.
isPiEntry now recognizes the legacy billion-context-pi npm form, dev
checkout paths, and Windows backslash paths, so install leaves exactly
one live bili plugin instead of stacking the old 0.1.x package next to
the new one. piStatus reports strictly on the current install root.
release v0.1.50
docs: cover all six launcher clients + backfill changelog 0.1.41-0.1.50
README keeps only the upstream-proxy (GFW) subsection; the reference
material (config file, top-level keys, providers, compression tuning,
env vars) lives in the dedicated CONFIGURATION docs. English README
mirrors the zh restructure (drop env-var table, Two ways, fix heading).
Removed detailed environment variable configuration section from the README file.
Removed duplicate sections and clarified usage instructions for the proxy and client configurations.
feat(launcher): add hermes to the bili launcher (/bili/ rewrite, no MITM)
fix(openai-sse): settle tool calls at finish, not per-chunk (name-split regression)
fix: survive upstream rejection of replayed thinking blocks in compress loop
PR #190 added fetchWithRetry (transient 429/5xx/risk-control replay retries,
BILI_REPLAY_RETRY_MAX opt-out) to the re-request path. Keep that machinery
and layer the #221 fix on top: a deterministic 4xx that survives transient
retries triggers one degraded retry with loop thinking blocks stripped
before the error is surfaced.
After a proxy-tool round (compress/decompress/acp_status/search_context),
the loop rebuilds the conversation and re-requests upstream. The rebuilt
assistant message replays thinking blocks collected from the intercepted
turn. Two failure modes halted the agent right after the ACP marker
(upstream issue #221, MiniMax M3 via Anthropic-protocol relay):
- signature-less thinking (relay never sends signature_delta) cannot be
replayed on Anthropic-protocol endpoints that validate -> 400
- even signed thinking replay is rejected by some relays/converter
backends
Both made the loop emit an error and end_turn, so the agent stopped
executing right after the ACP tool result.
Fixes:
- proactively drop signature-less thinking segments from the replay
(they are already streamed to the client verbatim in round 1)
- if the re-request is rejected with a 4xx, retry once with all
loop-injected thinking blocks stripped (degraded retry, at most one
per turn) before surfacing the error
Fixes https://github.com/ranxianglei/billion-context/issues/221
fix(loop): auto-retry acp-loop replay on transient upstream rejections (#189)
release v0.1.49
fix(config): correct stale context limits + registry-first resolution + Web UI parse-error guard
- CONTEXT_LIMIT_TABLE: deepseek 64K -> 128K (V2-era value caused constant
emergency compression out of the box); add minimax -> 204.8K
- registry: map api.minimax.chat / .com / .io so provider-qualified keys
like minimax/MiniMax-M2.1 resolve; new sync peekRegistryContext()
- server: native-window order is now plugin header -> warm registry cache
-> user route declaration -> built-in table -> async registry fallback.
A stale table can no longer lock out fresh registry data; cold start
still never blocks on a fetch.
- web/api: GET /__bili/config surfaces parseError for an unparseable
config; PUT returns 409 instead of silently rebuilding the file from {}
(which wiped every field the JSON loader could not read) — reported on
#212 as 'config overwritten after npm install'
- web/client: toast the parseError path
- tests: +8 (registry unit tests; broken-config PUT guard)
release v0.1.48
fix(plugin): stamp x-bili-plugin only after tools are registered
Review follow-up: the graceful-degradation claim (dead manifest endpoint
-> permanent wire mode) had no test. Drive session_start against a fake
proxy that 404s every request and assert the plugin stays silent.
The header tells the proxy the client owns the ACP tools natively (skip
wire injection). Stamping it synchronously while registerTools() is still
fetching the manifest sent the first provider request out tool-less in
one-shot runs. Gate the stamp on toolsReady; until then the request
rides the proxy's wire mode, and a dead manifest endpoint degrades
gracefully instead of stranding the session.
fix(responses): remap round-2 message lifecycle so codex stops dropping text deltas
fix(launcher): never reuse a proxy port — always spawn a fresh instance
release v0.1.47
feat(launcher): add opencode as a launch client + --bin flag
process.argv[1] stays at the global bin symlink (~/.local/.../bin/bili),
so path.dirname(argv[1]) pointed into the bin dir and the existsSync
guard silently skipped dist/agent/opencode.js (and dist/mcp.js in the
MCP configs). Resolve via selfPackageRoot() (import.meta.url-based,
symlink-safe through Node's realpath) instead.
Verified via the global bili: opencode run now loads the bili plugin
('[bili-opencode] plugin active') and the session log shows
dist/agent/opencode.js loading.
- src/agent/opencode.ts: 1.8KB plugin shipped as dist/agent/opencode.js;
registers the acp command via the config hook, renders the proxy's
buildStatusPanel through an ignored no-reply chat message. Active only
under BILLION_CONTEXT_PROXY (launcher), no-op otherwise.
- prepareOpencodeHttpRewrite(pluginPath?) injects the plugin's absolute
path into the throwaway OPENCODE_CONFIG copy so bili opencode needs no
install step and never touches the real opencode.json.
- handlePluginStatus(..., fallbackLatest): ?fallback=latest resolves an
unknown conversation to the most recently active session (opencode
plugins cannot stamp x-bili-plugin headers); response gains
fallback:true. Unchanged 404 without the param.
- The register-POST path was deliberately dropped: registering suppresses
wire tool injection, and this plugin cannot natively register the 4
tools (opencode tool hook needs zod). Wire injection + fallback status
keeps the model surface intact (verified: round-2 tools list ends with
compress,decompress,search_context,acp_status).
cert-MITM cannot intercept plaintext HTTP, so opencode's plaintext
providers (e.g. local zhipuai-lb) bypassed the proxy entirely. Mirror
the pi/omp pattern: write a /bili/-rewritten copy of opencode.json to a
temp dir and point OPENCODE_CONFIG at it (real config untouched);
HTTPS providers still ride cert-MITM.
- bili opencode: starts the proxy (cert-MITM for discovered HTTPS
domains), sets HTTPS_PROXY + NODE_EXTRA_CA_CERTS + BILLION_CONTEXT_PROXY
(so opencode-acp self-disables, see opencode-acp PR #335), and spawns
opencode.
- --bin <name>: override the client binary name for non-standard
installs (e.g. bili opencode --bin opencode-stable).
feat(plugin): add /acp command to show session ACP status
This reverts commit 36c8e031d4.
feat(compress): switch parseCompressInput to kernel parseCompressArgs + remove safeJsonParse (de-JSON)
The OpenAI adapter buffered ALL tool_call deltas and regenerated them with a
proxy-generated ID after finish_reason, breaking client-side tool_call parsing
(ID mismatch + reordering). Real tool calls now pass through as raw SSE events
(meta), preserving original position and ID — matching plugin-mode behavior.
Fixes#209
Avoids a name clash when a user has both billion-context and
billion-context-pi installed. The proxy now injects bili_compress /
bili_decompress / bili_search_context / bili_status at the wire level;
the compress loop maps bili_* → canonical names before dispatch. The
/acp command is renamed to /bili. The kernel's prompt text is
parameterized separately via withToolNames (acp-kernel PR #138).
Aligned with billion-context-pi's format: the panel now shows
billion-context@<version> under the header, read from package.json
at runtime (same pattern as src/cli.ts's VERSION constant).
conversationIdentityResponses (acp-kernel/wire) does NOT read prompt_cache_key —
it falls back to a content fingerprint with clientProvided:false, so a fix keyed
on responsesIdentity.clientProvided never fired for omp. Read the field directly
from the Responses body and record it unconditionally.
Two fixes for /acp in omp (worked in pi from the start):
1. Persist the plugin-conversation map across proxy restarts. The
conversationId->sessionId map in src/plugin.ts was in-memory only, so a
fresh/resumed proxy had an empty map and /acp said 'no ACP session yet'
until a new model request arrived. Now it is persisted to
<stateDir>/plugin-conversations.json (debounced write on record, flush on
the 3 shutdown paths, load after initSessions at startup).
2. Record omp's session id from the request body. The map is populated by
recordPluginSession, called only inside 'if (pluginAgent)' — and
pluginAgent comes from the x-bili-plugin header, which the plugin stamps in
the before_provider_headers event. omp has NO such event (its dist only
emits before_provider_request, body-only), so the header was never stamped
and pluginAgent stayed undefined for omp. omp sends its own session id in
the body as prompt_cache_key (a Responses-API field), which the kernel's
conversationIdentityResponses already extracts as the conversation identity.
Now, for responses requests with a client-provided identity, we call
recordPluginSession(responsesIdentity.value, session.id) unconditionally —
a no-op duplicate for pi, the only path for omp.
Verified: typecheck PASS, 526 tests pass, build PASS; e2e 'bili omp -p
<unique>' -> conversations file gains the omp session id, and
GET /__bili/plugin/status?conversationId=<id> returns ok:true with the
rendered buildStatusPanel.
The proxy's /__bili/plugin/status now renders the status with acp-kernel's
buildStatusPanel (same function billion-context-pi's /acp uses) and returns it
as a 'panel' field. The plugin displays that rendered panel verbatim, falling
back to the compact renderAcpStatus when absent — so /acp output matches
billion-context-pi exactly.
The agent plugin (pi/omp) now registers a single /acp slash command via
pi.registerCommand() that shows the current session's context-compression
status (context tokens/limit/%, in/out/cached, requests, blocks). It reads
the proxy's existing /__bili/plugin/status endpoint — no proxy changes.
Guarded by typeof pi.registerCommand === 'function' so hosts without the
API are unaffected; best-effort (warns, never throws) when the proxy is
undetected or the session is unknown.
Addresses the spirit of issue #53 (view session acp status). User asked for
exactly one command that only shows status (no subcommands).
feat(launcher): add omp to the bili launcher (no-config /bili/ rewrite + MITM)
omp (oh-my-pi) is pi-based and honors PI_CODING_AGENT_DIR, so the existing pi
isolated-home pattern is reused: the launcher reads ~/.omp/agent/models.yml
(never edits it), rewrites HTTP providers' baseUrl to the /bili/ prefix in a
temp agent dir, whitelists HTTPS providers for cert-MITM, and launches omp
pointed at the proxy.
- client-config.ts: OmpProvider/OmpConfig, resolveOmpHome, parseOmpYaml,
readOmpConfig; wired into loadClientConfig
- launcher.ts: omp in LAUNCH_CLIENTS/BaseClientName; launcherInjectMcp excludes
omp; discoverRoutes omp branch; prepareOmpHttpRewrite; runLaunch omp branch
- cli.ts: help text lists bili omp
- tests/launcher.test.ts: 8 new tests
refactor: ACP tool surface from acp-kernel 0.0.42 (Phase K1)
The 406-line static surface (schemas ×3 wire formats, prompt builders,
text tags, parseCompressInput, tool sets) moved verbatim to acp-kernel
compress-tools — single source shared with omp. This module keeps the
proxy's import paths and names stable: PROXY_TOOL_NAMES /
MUTATING_PROXY_TOOLS / READONLY_PROXY_TOOLS alias the kernel's ACP_*
names, parseCompressInput wires the kernel onWarn hook into logger.
Pins acp-kernel ^0.0.42 — to be set when the kernel release lands.
Verified: npm run typecheck clean, 514/514 tests pass against the local
kernel build (file: dep, uncommitted).
refactor(persist): delegate SessionStore mechanism to acp-kernel/persist
ci: per-PR npm preview builds (pr-artifact workflow)
On every PR to master: build, publish to npm under tag pr-<N>
(version <base>-pr.<N>.<run>), upload tarball+dist artifact, and
comment on the PR with install instructions (auto-updated per push).
Ported from opencode-acp's pr-artifact.yml, adapted per package.
fix(anthropic): round-2 thinking deltas carry raw — stop per-delta block fragmentation
Windows-22 CI runners take seconds on a cold loopback connect
(observed ~3s), racing the manifest fetch behind the fixed flush.
Same failure reproduces on master-side commit f755fe4 — pre-existing
flake, not from the round-2 thinking fix.
release v0.1.46
fix: re-inject the client's openai system around the kernel hoist (0.0.37)
Round >1 (proxy-tool/compress re-request rounds) withheld raw on
thinking_delta/signature_delta and re-wrapped EACH delta via
emitReasoning (a complete start+delta+stop block per chunk). Clients
assembled one thinking block per token fragment and replayed them in
subsequent requests — zcode showed a stack of duplicate '思考过程'
entries and the session accumulated dozens of reasoning fragments.
Mirror the 4deb563 vertical-text fix for text: attach raw in all
rounds, forward content_block_stop for thinking blocks in all rounds.
core.ts raw passthrough then preserves upstream framing byte-for-byte
(one block, signature included); the emitReasoning fallback stays as
a safety net for adapters that do not provide raw.
fix(loop): run applyCompression over the unpruned view
The streaming loop ctx passed processedMessages (pruned view with
acp_summary_* stripped) to applyCompression, so active blocks whose raws
were pruned had no resolvable anchor — bN promotion failed with the
self-contradictory 'already compressed' error. Add a dedicated
compressMessages field (unpruned originalMessages) used only by
applyCompression; wire rebuilds keep using the pruned view.
The kernel hoists the contiguous leading system/developer prefix out of
the openai fold space (system content is host runtime state - it must not
feed ids/fingerprints, and a compression covering it used to delete the
model's system prompt from every rebuilt payload). The proxy must now
carry it around the fold:
- prepareOpenai captures systemText from openaiToCore and re-injects it
(client system first, compress prompt after) into every rebuilt payload
- Prepared.openapiSystemText flows into pickAdapter so compress-loop
rounds re-inject it too, mirroring the anthropic adapter's
anthropicSystem path
- createOpenaiAdapter gains a clientSystem param
chore: acp-kernel 0.0.36, filter sub-viability nudge ranges via kernel viableRanges
chore: bump acp-kernel to 0.0.34
After a compress, provider risk-control may briefly reject the replay
request (GLM Coding Plan: 400 {"code":3007,"msg":"captcha verify failed"}
~1s after the context rewrite), and the error was passed straight into
the agent session. The replay request now retries transient upstream
failures with exponential backoff (3 attempts total, 1500ms base,
BILI_REPLAY_RETRY_BASE_MS override) on both the streaming loop and the
Responses-API JSON loop. Transient = 429/5xx or 4xx bodies matching
risk-control markers; plain 4xx still fail fast. Each retry logs a clear
"likely provider risk-control — retrying" line; exhausted failures are
surfaced with an "after N attempt(s)" suffix.
fix(plugin): codexRemove drops header-only bili block with no trailing newline
When the [mcp_servers.bili] table was header-only (no keys) and sat as the
final line with no trailing newline, after.indexOf('\n') was -1, so the
next-table search ran over a slice that still included the header itself and
matched it at index 0. end collapsed to start and the header line survived
the removal.
Compute firstNewline once and treat a missing newline as 'block runs to EOF'
so end = text.length. Adds a regression test covering the header-only,
no-trailing-newline case.
release v0.1.45
chore(deps): bump acp-kernel 0.0.31 -> 0.0.32
feat: builtin thin agent plugins (pi/omp/claude/codex/opencode) + bili plugin install CLI
chore(deps): bump acp-kernel 0.0.28 -> 0.0.31
release v0.1.44
fix(update): verify entry files and never overwrite a working install with a broken one (anti-brick)
Blockers:
- claude install/remove now honor CLAUDE as the binary path; tests inject
a missing stub so the failure-path assertion no longer depends on the
claude CLI being absent from the machine running the suite.
- `bili plugin install <agent> --origin` was silently dropped: the plugin
branch returns before the generic overrides->process.env merge, so a
stale ~/.local/state/billion-context/proxy-origin discovery file won.
Apply the flag to the env before dispatching the subcommand.
Nits:
- drop dead retryTimer field on RegisterState; cache empty session ids so
hosts without a sessionManager stop refetching the manifest per request.
- fetchJson: an already-aborted external signal never fires "abort";
forward the state immediately instead of waiting out the timeout.
- claude remove is a no-op when nothing is installed (matches other agents).
- proxy-origin discovery file: respect --host (collapse wildcard/localhost
binds to 127.0.0.1, bracket bare IPv6 literals) instead of hardcoding.
Tests: 467/467 pass, typecheck clean; --origin end-to-end verified against
a stale state file (9999 wins over 46657).
fix(compress): accept JSON-string content in parseCompressInput
fix: log non-2xx upstream responses (status + request-id + body snippet)
fix: answer WebSocket upgrades with a clean 426 so Codex falls back to HTTP immediately (#2)
fix(upstream): direct-connect path gets a 10s handshake timeout
fix(plugin): harden remembered-snapshot locking + activate registered flag
fix: VSCode Copilot BYOK — total_tokens in final chunk + cancel propagation (#177)
Two VSCode Copilot BYOK bugs (issue #177):
1. Every response ended with "Cannot read properties of undefined
(reading 'headerRequestId')". The proxy's OpenAI streaming adapter
rebuilt the final chunk's usage from the loop accumulator and dropped
total_tokens. VSCode Copilot's isApiUsage() requires prompt_tokens,
completion_tokens AND total_tokens to all be numbers; without it the
usage chunk was misclassified as a completion, so
solution.requestId.headerRequestId threw. emitCompletion now emits
total_tokens = inputTokens + outputTokens.
2. Clicking cancel mid-response left the proxy blocked waiting for the
upstream, and the per-session lock stayed held so the whole session
got stuck. The abort controller was created after the upstream fetch
and its signal only reached the compress loop, never the fetch. The
client-abort controller is now created before the fetch and its
signal is passed to fetchWithTimeout (externalSignal) and the
compress loop, so a client disconnect aborts the upstream request.
The write loop also short-circuits when the response is destroyed.
Adds tests/vscode-copilot.test.ts with regression tests for both fixes.
fix: harden context window — self-heal on upstream overflow + reserve output headroom
Non-strict-tool providers (e.g. vLLM openai-completions,
supportsStrictTools:false) stringify nested array arguments, so the model
may send compress.content as a JSON string instead of an array. Previously
Array.isArray(obj.content) was false and toRange(obj) returned null, so the
call silently parsed 0 ranges and compression never ran.
parseCompressInput now JSON.parse's a string content before the array check
(invalid JSON -> warn + 0 ranges, no crash), mirroring the fix in
billion-context-pi PR #164 (normalizeRanges in src/compress-tool.ts).
Adds a regression test for the JSON-string form.
Review fixes for the overflow self-heal:
1. markDirty on the error path: forward()'s non-2xx branch learns the
window (metadata.learnedContextLimits) and arms the emergency
(stats.lastInputTokens) but returns before forward()'s trailing
markDirty — both mutations lived only in memory and were lost on
restart, so the next overflow had to be re-learned. Schedule the save
in the branch that mutates. Pinned by a scheduleSave spy in the e2e
(on-disk assertions can't prove it: the prepare phase's debounced
save serializes the live session after the mutation anyway).
2. Strip upstream content-length/transfer-encoding unconditionally on
the error path: when readStreamToBuffer breaks the body (errBody
null) the response ends with no body, and a content-length claiming
bytes that never arrive leaves the client on a broken response.
3. Protocol-aware output headroom: Anthropic's Messages API enforces
the input limit independently of max_tokens (separate output budget),
so reserving maxOutput from the window shifted every nudge/truncate
band down by maxOutput on every Anthropic session with no safety
gain. OpenAI-family APIs count output against the window — reserve
there (and for unknown protocols, conservatively). New
shouldReserveOutputHeadroom() + unit tests.
Review fixes:
- Per-model learnedContextLimits: the learned window is keyed by the
model that overflowed. Previously a session-level scalar capped the
WHOLE session — after /model-switching to a bigger model mid-
conversation, the stale smaller window forced premature compression
for the rest of the session. handle() now looks up the current
request's model; the legacy scalar remains as a read fallback
(persisted sessions) and is still written when the model is unknown.
- inspectContextOverflow: recognize the newer Responses-API phrasing
'exceeds the model's maximum context size of N' (patterns only
covered 'context length') — self-heal now fires for /responses too.
- readStreamToBuffer: cap retained bytes at 1 MiB (stream still drained)
so a huge 400 body from a misbehaving upstream can't spike memory.
Tests: new unit case for the size phrasing; e2e now asserts per-model
keying (no legacy scalar) and adds a third request on a different
model asserting no cross-model crosstalk.
The kernel's nudge/truncate bands are a fraction of modelContextLimit, but
none of the layers reserved room for the model's OUTPUT. On a small window
(e.g. 100k) with a large max_tokens, context + output can exceed the window
and overflow before the nudge fires — the most common 'context blew up'
cause on a 100k window.
Reserve the request's exact max_tokens (the output budget for THIS turn)
from the window when handing it to the kernel, so the bands sit below
(window - maxOutput). Pure reserveOutputHeadroom() + per-request wiring in
handle(); unit-tested (4 cases). Complements the self-heal (reactive recovery)
with a preventive reservation — together they prevent + recover from the
context+output overflow.
When the configured context window is wrong — most commonly the 200k
fallback for an unknown model on a private relay (the MITM case) — the
nudge/truncate bands sit far above the real window and the upstream 400s on
every turn. The proxy previously passed that 400 through verbatim with no
recovery, so a non-compliant model left the session permanently stuck
400-ing (the context blows up and the session stalls).
forward()'s non-2xx path now buffers the (small) error body and inspects it
for a context-overflow (inspectContextOverflow in src/util.ts — a pure,
tested helper: only 400/413 with a recognized context-too-long marker counts,
and it deliberately does NOT match Bedrock's "too many tokens" throttle
phrase, a 429 the client should back off on). On detection it:
- learns the real window from the error body (OpenAI "maximum context
length is N", Anthropic "X tokens > Y maximum", ...) and persists it to
session.metadata.learnedContextLimit (a STABLE field — effectiveContextLimit
is re-resolved every turn and would be overwritten);
- handle() then self-heals: if the learned window is smaller than what was
resolved this turn, it re-centers reqConfig.modelContextLimit on it (spread
into a new object — never mutates the shared global config), so the
nudge/truncate bands drop below the real limit;
- arms an emergency shrink by raising lastInputTokens to the window (a lower
bound — it overflowed, so the context is at least that big), so the next
turn's usage is >=100% and the kernel's emergency nudge + tool-result
truncate fire;
- still passes the error through verbatim (no client-visible change).
Tests: tests/context-overflow.test.ts (11 unit cases) +
tests/e2e-overflow-selfheal.test.ts (overflow -> learn+arm+passthrough ->
recover, real usage report overwrites the armed value).
fix(usage): compute cache-hit accounting per protocol (no double-count)
fix: normalize localhost bind host; simplify codex web card to url-only
The roundtrip test asserted the installed extension path with a
forward-slash-only regex (dist\/agent\/omp\.js), which cannot match on
windows-latest where the path uses backslashes (D:\a\...\dist\agent\omp.js).
Use dist[\/]agent[\/]omp\.js so the assertion passes on both platforms.
- drop stray .bili-coop-build-stamp; .gitignore entry now matches the
actual harness filename (was .bili-thin-build-stamp)
- plugin list: per-row try/catch so one broken host config (e.g.
~/.claude.json) no longer crashes bili plugin list
- mcp: forwardTool gets a 60s timeout (wrapped as a readable error),
register fetch a 5s timeout; proxy origin resolved per request so a
restarted proxy on a new port self-heals
- requireDistFile warning: process.stderr.write instead of console.error
(AGENTS.md: console.error only in cli.ts/index.ts)
- tests: plugin list survives broken config; mcp forwardTool timeout
Some relays echo a schema-shaped usage in message_delta where
input_tokens/cache_read_input_tokens are present but 0 (the spec fields
are normally absent — message_start is authoritative, and the input
context is fixed within a turn). Merging the 0 overwrote message_start's
real values in both usage paths:
- loop mode (adapter-anthropic.ts): roundInput/roundCollapsed to 0 →
recordUsage set lastInputTokens to the cached portion only
- plugin mode (plugin.ts usageFromSseEvent): mergeUsageSample zeroed
acc.inputTokens → applyUsageSample collapsed the total to cached-only
The nudge/truncate denominator then under-reported by ~6x (e.g. 11 instead
of 66 in the regression fixture), so compression never fired on
cache-heavy sessions. A 0 can never be a legitimate update to a fixed
in-turn input, so only > 0 values are adopted.
Adds regression tests for both paths (verified to fail without the fix).
- mcp.ts: fetch manifest lazily on first tools/list instead of
fetch+exit(1) at startup; codex spawns MCP children with a sanitized
env, so a missing BILI_MCP_PROXY used to resolve the dead default
origin and kill the handshake ("connection closed: initialize
response") before stdin was ever attached. initialize now always
responds; tools/list reports a JSON-RPC error when the proxy is
unreachable instead of dropping the server for the whole session.
- plugin-install codex/claude/opencode: stamp the resolved proxy origin
(BILI_MCP_PROXY env > proxy-origin file > default) into the MCP
config at install time (TOML env block / claude -e flag / opencode
environment field); reinstalling codex refreshes a stale origin.
- verified live: codex exec spawns bili MCP, lists mcp__bili__* tools,
and completes an acp_status tools/call round-trip through the proxy.
- pi/omp ToolResult contract: {content:[{type:"text",text}],isError?}
(crashed pi live when forwarding tool output)
- strict /bili/ detection (first path segment + /https?:// next), protocol check
- registration: per-session-id single-flight, 10s retry throttle,
before_provider_headers fallback, hostile host shape defense
- AbortSignal passthrough, explicit timeout errors, args default
- createBiliPlugin(agent) factory; omp reports omp, custom agents supported
- plugin install: OPENCODE_CONFIG/OMP_CONFIG are file paths, CODEX_HOME/
CLAUDE_CONFIG_DIR are dirs; refuse to overwrite broken/non-object JSON;
installers throw -> CLI exits 1; omp YAML flow-style/multi-key/column-0
guards + quoted-entry removal; pi stale node_modules entries cleaned;
codex line-regex table match; claude exec 15s timeout
- cli: --origin/--agent flags, non-empty value validation, plugin-register
agent param + 5s timeout, HELP covers mcp/plugin-register
pi replaces the boot session while the awaited manifest fetch is in
flight; registerTool on the captured ctx then throws. Register per
session id instead, catch staleness and defer, and retry from
before_provider_headers (fires per request with a live ctx) as the
safety net.
- src/agent/shared.ts: proxy detection (/bili/ prefix, BILLION_CONTEXT_PROXY, kill switch), manifest fetch, tool forwarding, status fetch
- src/agent/pi.ts: pi/omp extension factory, zero runtime deps, registers manifest tools at session_start, stamps x-bili-plugin* headers
- src/plugin-install.ts: bili plugin install/remove/list for 5 hosts with backup+idempotency; PI_CODING_AGENT_DIR respected
- tests: 7 new tests (460 total green)
The streaming compression-loop caller built LoopCtx without `protocol`,
so recordUsage always took the Anthropic additive branch (prompt + cached)
and double-counted cached tokens for openai/responses responses on the
main streaming path — the exact bug this PR fixes, left unfixed here.
- server.ts: add `protocol: prepared.protocol` to the runCompressLoop ctx
- loop-core.test.ts: regression tests — protocol='responses' → total=
input_tokens (1000, not 1900); protocol unset → legacy additive
Three defensive cleanups in the cooperative-plugin feature (merged via
#161 + #163), found during review:
1. src/mcp.ts: the 'registered' flag was checked in initialize
(if (conversationId && !registered)) but never set to true, so the
guard was dead and a repeated initialize would re-issue the register
POST. Set registered = true once the register request is issued.
(Register endpoints are idempotent, so no live harm — the guard is now
actually functional.)
2. src/plugin.ts handlePluginTool: remembered.get(session.id) and the
derived messages/nudge were read BEFORE withSessionLock. A tool call
racing a concurrent model request (between recordPluginSession's
remembered.delete and rememberPluginMessages's remembered.set) could
capture an empty/stale snapshot and run executeProxyTool on the wrong
context. The read now happens inside the lock, where
rememberPluginMessages rewrites the snapshot atomically.
3. src/plugin.ts recordPluginSession: removed the out-of-lock
remembered.delete(sessionId) (binding step). remembered[sessionId] is
now only mutated under the session lock (rememberPluginMessages after
forward), closing the stale window that #2 read into.
No behavior change in the normal plugin flow (tool calls arrive after the
model response, never concurrent with it); this is lock-consistency
hardening. Full suite 453/453 pass; typecheck clean.
lastInputTokens / cache-hit rate were computed as `prompt + cached`
uniformly, which is only correct for Anthropic (where input_tokens is the
NEW portion and cache_read is reported separately). For OpenAI and
Responses the reported input total ALREADY includes the cached subset, so
adding it again double-counted cached tokens:
- lastInputTokens inflated by the cached amount -> the nudge decision
overestimates context size -> premature compression.
- the logged cache-hit % was deflated (cached/(prompt+cached) instead of
cached/prompt); e.g. a true 90% hit read ~47%.
- cumulative inputTokens was protocol-inconsistent (Anthropic new-only vs
OpenAI/Responses total), so the web cacheHitPct could exceed 100%.
Fix: normalize to the true TOTAL context per protocol via a shared
usageTotals() helper (util.ts) and thread the wire protocol into every
usage consumer:
- loop/core.ts recordUsage (streaming compress-loop path)
- server.ts non-streaming usage block
- plugin.ts applyUsageSample (plugin-mode passthrough)
Adapters are unchanged (they still emit protocol-native usage to the
client-facing completion). The only behavior change is the internal stats:
lastInputTokens, cumulative inputTokens, and the hit-rate are now the
protocol-correct total.
tests: add tests/usage-totals.test.ts pinning the per-protocol totals;
update the plugin streaming expectation from 55 (new-only) to 66 (true
total = input 55 + cache_read 11) so cacheHitPct stays a valid ratio.
- The handle() `upgrade === "websocket"` 426 check is unreachable once the
dedicated 'upgrade' listener is registered: Node routes upgrade requests
to 'upgrade' and never to the request handler. Verified on both the plain
path and the MITM-decrypted (bili codex) path. Replace with a pointer comment.
- Add socket.on("error") to the 'upgrade' handler so a client vanishing
mid-426-write cannot surface as an uncaught ECONNRESET.
Full suite 456/456, typecheck + build clean.
feat(launcher): plugin-in-launcher mode — native MCP tools via spawn-time injection (#162)
The web Update-branch merge (31efaa8) resolved the #161/#163 conflicts in
favor of the pre-f0d3ebd branch side, silently reverting the plugin-header
marker gating (session-id.ts) and its tests. This merge keeps the correct
union resolution: register symbols + pluginReportedContextWindow + gating.
Resolve conflicts with #161 (plugin protocol) landing on master:
- src/server.ts import: union — keep launcher register symbols
(handlePluginRegister/consumePluginRegisterFor/takePendingPluginRegister),
adopt master's pluginReportedContextWindow; drop pluginContextWindowHeader
(unused after master's rename, still exported as internal helper)
- tests/plugin-protocol.test.ts import: union of both sides' harness symbols
Verified: 453/453 tests pass, npm run typecheck clean.
feat: cooperative plugin protocol — agent-side native tools + proxy-owned engine
The proxied branch of connectThroughProxy already had a 10s CONNECT
handshake timeout, but the direct branch (no upstream proxy) relied on
the OS default tcp_syn_retries — a black-holed upstream (firewall drop,
routing hole) left half-open sockets hanging for ~127s.
Extract the direct path into connectDirect() with the same timeout
pattern (settled guard, socket destroy, timer cleanup);
connectThroughProxy now delegates to it, so both plain and TLS
outbound paths (connectTlsThroughProxy reuses it) are covered. The
proxy branch's hardcoded 10_000 now shares the CONNECT_TIMEOUT_MS
constant.
Tests: real-server success for both entry points, immediate
ECONNREFUSED still fast-fails, and a never-completing connect
(injected via the repo's _setConnectFactoryForTest seam — ESM
namespaces preclude module mocking) rejects with the timeout error in
milliseconds and destroys the socket.
Review finding on #163: injectMcp defaulted ON for claude/codex, changing
spawn args for every existing user while hosts older than the verified
builds (claude 2.1.227, codex 0.147.0) are untested against --mcp-config /
-c mcp_servers.*. Default-off keeps bili claude / bili codex spawns
byte-identical until the flags soak; a stderr hint tells users how to
opt in. Extracted launcherInjectMcp() with unit coverage; pi stays
excluded (native extension, billion-context-pi #154). Flip default to on
(with =0 opt-out) after a soak release.
release v0.1.43
buildCodexMcpArgs now takes a per-spawn UUID and injects
mcp_servers.bili.env.BILI_CONVERSATION_ID. The MCP shell
(src/mcp.ts) already reads it and self-registers headlessly on
initialize; this closes the loop — previously every `bili codex`
native tool call failed with 'no conversation id' because the
launcher never passed the id.
Binding works on the default MITM route: the first model request
that creates a NEW session consumes the pending registration.
Direct-URL mode still cannot bind (model traffic bypasses the
proxy) — that stays with the codex LLM-routing follow-up.
Also honors BILI_LAUNCHER_PLUGIN=0 in direct-URL mode for codex
(the direct branch previously ignored the escape hatch).
Tests: builder asserts the new -c line; new e2e drives the real
MCP shell over stdio with only BILI_CONVERSATION_ID (no
CLAUDE_CODE_SESSION_ID, no manual register) → headless self-
register → model request binds → tools/call acp_status succeeds.
Direct-URL routing was ON by default, so upgrading silently changed the
traffic route of every existing 'bili claude' / 'bili codex' setup:
- codex: LLM traffic stops going through the proxy -> compression/memory
silently stop working
- claude (OAuth subscription): subscription traffic breaks
- claude (custom relay): ANTHROPIC_BASE_URL is silently bypassed in favor
of https://api.anthropic.com
A changed traffic route is a compatibility concern and must be a deliberate
choice, not a silent upgrade. The default is now the classic transparent-
MITM route (byte-for-byte the pre-PR launcher behavior); direct URL is
opt-in via BILI_LAUNCHER_DIRECT=1 (BILI_LAUNCHER_MITM never shipped,
removed). Enabling direct mode prints a client-specific warning describing
the route change. MCP tool injection remains on by default — it is purely
additive and degrades gracefully if the MCP server fails to start.
- launcherDirectUrl: BILI_LAUNCHER_DIRECT === '1'
- runLaunch: direct-mode warnings (claude: relay bypassed unless
BILI_CLAUDE_UPSTREAM; codex: LLM traffic not proxied)
- README: compatibility-first default + opt-in documented
- test: default = MITM route; direct URL opt-in honored
- launcher.ts buildCodexMcpArgs: args were double-encoded
(JSON.stringify applied twice) so codex received a TOML string where it
expects a sequence — every 'bili codex' with injection died at startup
with: Error loading config.toml: invalid type: string ... expected a
sequence in mcp_servers.bili.args. Now emits args=["<script>"] (a JSON
array literal, which is valid TOML). Verified with codex-cli 0.147.0.
- cli.ts plugin-register: sends identity: true — the documented
'bili plugin-register $CLAUDE_SESSION_ID' usage registers a claude
session id that claude code puts on every model request, so it must
bind by identity match, not consume the next NEW session.
- plugin.ts takePendingPluginRegister: headless registrations now expire
after 10 min (PENDING_REGISTER_TTL_MS). The ts field was recorded but
never used; a stale/orphaned registration could otherwise be bound to
an unrelated later session, turning it into plugin mode with a foreign
conversation id.
- tests: codex -c args assertion now verifies the value is a TOML array
(would have caught the double-encoding); new TTL expiry test.
x-bili-plugin-context-window and x-bili-plugin-conversation were honored
from ANY request, not just plugin requests. A plain client (or any client
on a LAN-exposed deployment) could rewrite the nudge denominator and
steer the proxy's session identity by name alone.
- plugin.ts: pluginReportedContextWindow() only returns the reported
window when x-bili-plugin is present; server.ts uses it in the
native-window cascade.
- session-id.ts: x-bili-plugin-conversation is only consulted when the
marker header is present (the protocol always sends both together);
legacy client headers are unchanged.
- test: header gating unit cases (marker on/off, bad value, legacy
fall-through).
build: align acp-kernel to 0.0.28
Single kernel version across the ecosystem (omp #85 runs provider mode on
the kernel wire codec; billion-context-pi already runs 0.0.27):
subagent-namespace injectable anchor store, wire format detection,
transform-channel policy, effective-gate char unification, token snapshot
docs. persist.ts mergeState carries the new tokenSnapshot field
forward-compatibly (older session files default to an empty snapshot).
Full suite green on 0.0.28 (437/437).
log: model identity on the nudge diagnostic line
diagNudge now takes parsed.model and emits model=<id> on every nudge
line (all three prepare paths: anthropic / openai / responses). Same
motivation as omp #78: weak-model compress-rejection loops must be
attributable from the log alone, no cwd forensics.
- POST /__bili/plugin/register {conversationId, agent, identity}: identity
binding (claude: x-claude-code-session-id === CLAUDE_CODE_SESSION_ID on
every request, verified against claude 2.1.227) is race-free on any arrival
order; headless pending (codex spawn) binds the next NEW session
- src/mcp.ts: MCP stdio thin shell — manifest → 4 tools → /tool forward;
conversation id from CLAUDE_CODE_SESSION_ID (claude child env) or
BILI_CONVERSATION_ID (launcher); tsup second entry (dist/mcp.js)
- CLI: 'bili mcp' + 'bili plugin-register <id>'
- launcher direct-URL mode for claude/codex (default; BILI_LAUNCHER_MITM=1
opt-out): claude gets ANTHROPIC_BASE_URL via spawn env + --mcp-config
ephemeral JSON; codex gets -c mcp_servers.bili.* inline overrides.
BILI_LAUNCHER_PLUGIN=0 kills the injection (plain wire mode)
- zero host-config writes; tmp MCP config removed on exit
Verified: 450/450 tests (6 new: register validation, identity binding after
first request, headless pending binding, no cross-session leak, injection
builders, MCP shell stdio) + real e2e: 'bili claude -p' → proxy log
[plugin] tool acp_status executed via plugin (native MCP path, wire
injection suppressed), host config untouched.
refactor: adopt acp-kernel/wire — codecs move to the kernel
fix: resolve issues #150#151#152#154 (24h issue batch)
BILLION_CONTEXT_PROXY is exported by the launcher env builders (pi/codex/
claude) so agent-side plugins can detect the proxy in MITM transparent
mode (no /bili/ prefix to sniff). x-bili-plugin-context-window reports
the agent's own model window - the authoritative native source (outranks
table/registry; operator compress.modelContextLimit still wins) - most
valuable for private relays behind MITM. GET /__bili/plugin/status exposes
context-level usage (contextTokens = input+cache-read, the same value
nudges read) for plugin status UIs. Plugin stream passthrough now applies
usage before res.end() so the client's next request always sees it.
PLUGIN.md sections 4-5. Tests 426/426.
Outside half of the inside-outside cooperation (issue #1): an agent-side
plugin registers the 4 ACP tools natively (schemas served by
GET /__bili/plugin/manifest, single source of truth) and sends
x-bili-plugin / x-bili-plugin-conversation headers; the proxy suppresses
wire tool injection for that session, passes responses through verbatim
(with usage sniffing), and executes plugin-forwarded tool calls under the
session lock via POST /__bili/plugin/tool. Session identity becomes the
plugin's real conversation id. Wire mode unchanged (419 tests untouched).
Spec: PLUGIN.md. Tests: tests/plugin-protocol.test.ts (6).
Deletes local src/{anthropic,openai,responses,bili-message,message-id}.ts
(~1000 lines) and imports from acp-kernel/wire (0.0.25). Extraction was
verbatim (hashId byte-identical) so message ids and behavior are unchanged.
- kernel 0.0.24 → 0.0.25
- server.ts / loop adapters / tests now import from acp-kernel/wire
- orphan-gc.ts comment pointer updated
Regression: 419/419 tests green (the 9 codec-consumer test files now
exercise the kernel copy), build clean.
Consumers of this shared surface: proxy (this PR), omp Plan B (#52 follow-up).
parseCompressSettings whitelisted tuning fields only, so saving the compress
block from the web UI (GET shows the raw file block, incl. the injection
toggles) rewrote the file without injectTool/injectNudge. loadOptions then
fell back to the defaults and silently re-enabled the compress tool/nudge
for users who had set them to false.
- parseCompressSettings now accepts the two file-level boolean toggles
(matches FileConfig.compress / ProxyOptions.compress types)
- handleConfigPut log line reports which section actually changed instead
of always "network only"
- buildSession guards lastMessages with Array.isArray (corrupt v3 file
must not break export, matching the blockContents filter style)
- web UI compress card lists the toggles as supported fields
- regression: unchanged round-trip keeps injectTool in file + GET, and
parseCompressSettings unit coverage for the toggles
release v0.1.42
writeNow's per-session chain cleaned itself up via next.finally(...) — a
derived promise nobody holds. When the chain rejected (transient persist
failure: disk full, EPERM, ENOENT after a tmpdir vanished on the windows
CI runner), that orphan surfaced as a process-level unhandledRejection,
failing the whole test file in CI and crashing the proxy on default Node
settings. Attach .catch(() => {}) to the cleanup side-effect; the real
rejection still reaches the caller.
- Session.lastMessages: snapshot of the core message list at each request
(snapshotMessages in session.ts, called from all three prepare paths in
server.ts); empty snapshots never clobber a good one
- persist.ts: PERSIST_VERSION 3, PersistedSession.messages, round-trip in
buildRecord/buildSession
- export.ts: renderHandoff now renders the conversation itself — default is
the folded view via kernel prune() (exactly what the model saw: block
summaries in place of compressed ranges), --full renders every original
message; v2 session files (no snapshot) fall back to block summaries with
an honest note that client messages are unavailable
- tests: e2e asserts folded view omits compressed originals while --full
keeps them; export-uncompressed covers both v3 and v2 paths
renderHandoff claimed 'the session history below is the original
conversation' for 0-block sessions, but buildRecord only persists
compression state and compressed originals — uncompressed text is never
on disk, so nothing followed. Now states that honestly, drops the
'paste the block summaries' tail when there are no summaries, and the
empty-store message no longer implies persistence requires compression
(markDirty runs on every request).
Drives startServer against a mock upstream that reads the real <acp> ref
tags off the wire and answers the injected compress tool with a valid
range; asserts the block lands in the on-disk SessionStore and that
exportSession (what 'bili export' runs) renders the model-written
summary, omits originals by default, and includes them with --full.
Unlike cli-export.test.ts, no session state is hand-built.
test(e2e): GLM chat-completions bridge + streaming Responses e2e coverage
Review findings on #149:
- the 'glm bridge' has zero coupling to any GLM endpoint (local mock +
protocol converter simulating a chat-completions-only relay). Rename to
chat relay (file, identifiers, error strings, test titles) so it reads
as the generic OpenAI-compatible relay it is.
- bridge bug: opening a function_call item called
closeMessage(state, res, "") — any assistant text streamed before the
tool_call was dropped from output_text.done / output_item.done /
completed.output. Now passes the accumulated messageText.
- new regression test: mixed text→tool_call script (marker protocol)
asserts full text survives the message lifecycle alongside the
function_call item. Verified it fails against the old code.
410/410 tests, typecheck clean.
feat(compress): configurable compression prompts (three-level, risk-gated)
Regression review findings on #157:
- the three builder templates had a lone trailing space where master had an
empty line — the default-path system prompt differed by one byte from
pre-upgrade output, breaking the prefix-cache anchor. Verified fixed:
default-path output of all three builders is now BYTE-IDENTICAL to
origin/master (direct string comparison against a snapshot import).
- adds tests/e2e-compress-prompts.test.ts: real startServer proxy + mock
upstream capturing the wire body. Proves (1) provider+model sub-field
merged overrides reach the upstream system message, (2) overrides are
inert without acknowledgePromptsRisk (defaults on the wire + warn log),
(3) no-config path keeps the kernel default prompt.
Adds `prompts` + `acknowledgePromptsRisk` to CompressSettings (#156):
- prompts override object (compressPhilosophy / howToCompressRules /
tier2DistillRules / tier3CondenseRules), merged sub-field-wise across
global > provider > model levels
- overrides are inert until acknowledgePromptsRisk: true at the winning
level (kernel rules are load-bearing); one-time warn when ignored
- resolved via kernel resolvePrompts (non-string fields silently dropped)
- threaded consistently into system prompt, nudge text, and compress loop
- docs (EN/zh-CN) + 6 unit tests
fix: keep thinking signature on compressed re-request for DeepSeek Anthropic endpoint (400)
fix(security): follow-ups to #141 — admin Host gate bypass + port-0 regression
With port: 0 (dynamic assignment) adminTrustedHosts pinned to
'localhost:0' so every /__bili/ request 403'd. Use the request
socket's localPort instead. Adds regression test (dynamic port:
trusted Host 200, rebinding Host 403).
- #115: pin Host to trusted hosts BEFORE the no-Origin early-return; a
same-origin rebinding GET carries no Origin and previously bypassed
the Host check entirely
- #117: cap update-lock steal age (LOCK_MAX_AGE_MS=30min) so a crashed
holder whose pid was reused by an unrelated process cannot block
auto-update forever (shouldStealLock pure fn, exported)
- #116: verifyTarballIntegrity fails closed on unknown hash algorithms
instead of throwing; exported for tests
- #77: CONNECT 403 uses end() so the response flushes; consolidate
duplicated isLoopback into shared isLoopbackAddress (mitm copy missed
::ffff:127.x)
- #118: handshake timeout env-tunable for tests
Tests (+14): responses error-path full item lifecycle; MITM e2e
(happy path, handshake timeout, non-loopback 403); admin rebinding
e2e incl. no-Origin case; integrity + lock-steal units; hop-by-hop
static + Connection-named stripping e2e. Fix withTmpCa helper: a bare
'return fn()' ran finally on first await, restoring env mid-test.
typecheck clean, 389/389 tests, build 2.04 MB
fix(security): network/security hardening batch (#115#116#117#118#80#77#76#79#63#61#62)
Third protocol now has end-to-end coverage (chat/completions smoke already
existed; Responses covered in the previous commit):
- text passthrough: incremental text_delta forwarding with arrival-span
anti-buffering assert, message_start/message_delta/message_stop lifecycle,
injected compress/decompress/search_context/acp_status tools reach the
upstream in Anthropic flat format alongside user tools, system block gains
the compress prompt while preserving the original system text
- tool_use reconstruction: namespaced tool (agents.spawn_agent) block is
re-emitted with the SAME id + name + accumulated input_json_delta
(guards the Anthropic-side analog of issue #143)
- compress round-trip: intercepted tool_use triggers a 2nd upstream request
carrying a tool_result block (tool_use_id preserved), round-2 text streams
to the client, compress block never leaks to the client, exactly one
terminal message_stop across rounds
test: e2e coverage for three-level compress cascade
- tests/e2e/glm-bridge.ts: zero-dep Responses<->chat-completions converter
server (stand-in for codex_zhipu_proxy class of upstreams): converts
Responses requests to chat/completions (instructions->system, developer->system,
function_call/OUTPUT round-trip, tools flat->nested) and converts streamed
chat chunks back to a full Responses SSE lifecycle (reasoning items,
message items, function_call items, usage, completed).
- tests/e2e-responses-glm.test.ts: 6 end-to-end tests through startServer
codex->bili->bridge->mock-GLM covering previously untested paths:
* native responses streaming: incremental output_text.delta passthrough
(arrival-span anti-buffering assert) + injected compress tool converted
to nested chat format upstream
* marker protocol: text coalesced into one delta + rebuilt msg-proxy-
lifecycle (documents by-design behavior)
* issue #143 regression: namespaced function_call raw passthrough with
stable item id (marker AND native)
* issue #94 regression: reasoning item lifecycle end-to-end
* compress tool-call round-trip on responses path (2nd upstream request
carries role:tool result, round-2 text streams)
release v0.1.41
- adapter-anthropic: track thinking/redacted_thinking blocks; seal the
reasoning segment on content_block_stop so interleaved thinking blocks
each keep their own signature on rebuild
- core: reasoning segments replace the single-signature overwrite
accumulator; signature_delta events now concatenate (protocol may
fragment signatures across events)
- adapter-anthropic: implement emitReasoning so round>=2 thinking is
visible to the client (core already guarded on it)
- tests: drop console.log debug output, replace tautological
assert.ok(out.length >= 0), add split-signature and interleaved
thinking-block cases (3 tests, all passing)
When a session with extended thinking triggers compression, round-2
re-request rebuilds the assistant reasoning message. DeepSeek's Anthropic
endpoint rejects the request with 400 'content[].thinking must be passed
back' when the rebuilt thinking block omits the signature from round 1.
Capture thinking_delta and signature_delta in the Anthropic adapter
(round-1 raw passthrough preserved) and carry the signature through the
reasoning event into the rebuilt message as thinkingSignature.
fix(responses): pass non-proxy function_call items through raw (Codex agents.* unsupported call)
The Responses adapter previously swallowed EVERY function_call item
(added/arguments.delta/arguments.done/output_item.done) into a pending
map and re-emitted it via buildFunctionCallEvents with a NEW item id
(fc-proxy-...) and the arguments collapsed into a single delta. This
reconstruction is fine for proxy tools the loop must intercept
(compress/decompress/search_context/acp_status), but it corrupts the
wire format for real client-side tools: the streamed events use a
synthetic id while response.completed.output still carries the upstream's
original id, and strict clients that dispatch on the streamed lifecycle
(e.g. Codex multi-agent agents.* namespaced tools) reject the call
('unsupported call: spawn_agent').
custom_tool_call items already passed through raw; function_call did not.
This made namespaced function tools (agents.*) fail while custom-tool
based tools worked.
Fix: only intercept function_call items whose name is in PROXY_TOOL_NAMES;
all other function_call items are forwarded as raw meta (unchanged SSE),
exactly like custom_tool_call. A passthrough flag on the tool_call event
lets the loop count the call (so it still suppresses the re-request when
a real call was made) without re-emitting it.
Tests: tests/multi-agent-namespace.test.ts covers proxy interception
(compress still swallowed), raw passthrough of agents.spawn_agent
(original item id + namespaced name + call_id + args preserved, no
fc-proxy reconstruction), both textProtocol modes, and tool-declaration
survival through responsesToCore/patchResponsesInput. 375/375 pass.
Refs #143
Drive the real kernel core.processTurn with a 20000-token context under a 2w
window (usage 100% >= maxContextLimitPct 0.7) → shouldInject true; under a 100w
window (usage 2%) → false. Proves resolveRequestConfig's resolved Config actually
gates the compress hint, not just that thresholds are set.
Extract the per-request compress-config build (the former inline block at
server.ts:485-502) into a pure exported resolveRequestConfig() in
compress-settings.ts, so the global -> provider -> model cascade is
testable end-to-end. Behavior-preserving (full suite 376 passes).
tests/e2e-compress-cascade.test.ts drives resolveRequestConfig -- the
exact function server.ts now calls for every request -- and asserts:
model/provider/global deepest-field-wins merge, percent/absolute
modelContextLimit resolution, unknown-provider global fallback, and the
no-config base-unchanged early-return path.
Previously applyCompressSettings/mergeCompress/resolveCompress had zero
coverage (only resolveCompressProtocol/tool-injection was tested).
ci: add Windows + cross-platform regression; add devlog
First windows-latest CI run (PR #140) exposed two Windows-only test bugs:
1. tests/launcher.test.ts:380 - resolveClientCommand fallback asserted
endsWith("pi-coding-agent/dist/cli.js") with a forward slash. Windows
resolves the path with backslashes, so the check was always false.
Fix: normalize path.sep to "/" before endsWith.
2. tests/discover.test.ts:177 - rescan test wrote config.json twice within
microseconds. src/discover.ts mtimesEqual() compares statSync().mtimeMs;
NTFS can give back-to-back writes the same mtimeMs, so the rescan was
skipped and a stale result returned. Fix: fs.utimesSync the config to a
guaranteed-distinct mtime so the test is deterministic on every OS.
Local (ubuntu): typecheck clean, 370 tests pass, build OK.
Product behavior unchanged; only tests fixed.
Ref: dog/billion-context-pi#32
The Responses streaming error path (stream-error.ts, reached when
runCompressLoop throws) emitted a bare response.output_text.delta with NO
preceding response.output_item.added, NO item_id, and NO output_index.
A strict Responses client (codex / gpt-5-codex) that tracks item
lifecycle would see an orphan + malformed delta and crash exactly when
the compress loop already failed — the user got a crash instead of the
error text.
Fixed by mirroring buildMessageItemSequence (adapter-responses.ts): emit
output_item.added → content_part.added → output_text.delta → output_text.done
→ content_part.done → output_item.done, then response.completed with the
message item populated in output.
The happy-path streaming adapter was already correct (emitText/emitMarker
both route through buildMessageItemSequence, which always pairs
added+delta). Verification (via oracle agent trace):
- server.ts:1325 streaming branch → pickAdapter → createResponsesAdapter
→ runCompressLoop; emitText/emitMarker/emitToolCall all emit
output_item.added before their delta.
- rewriteResponsesSseStream (the suspected orphan site) was DEAD CODE:
imported at server.ts:50, zero call sites. Its orphan-delta sites
(msg_acp / msg_acp_note) could never fire. Deleted it and its
exclusive helpers; only rewriteResponsesJsonResponse (the live
non-streaming path) remains in stream-responses.ts.
conversationSignalOpenai and conversationSignalAnthropic truncated the
first user message to 200 chars before hashing it into the session id.
Two distinct conversations sharing the same first 200 chars (common when
a long system/context prefix or boilerplate precedes the real request)
collided onto one session id, cross-contaminating their compression
state.
The first user message is stable across turns (clients resend the full
history), and hashId streams sha256, so hashing the full content adds no
meaningful cost and eliminates the collision. conversationSignalResponses
already hashed the full input; this aligns the other two.
- #117 (MED): tryAcquireLock no longer steals a lock held by a LIVE
process, even past the old staleness threshold. A slow install
(download+extract+rebuild) can legitimately exceed 2min; stealing its
lock let two processes write the install dir concurrently → corruption.
Only a confirmed-dead holder (crash) is stolen. Removed LOCK_STALE_MS.
- #77 (open relay): CONNECT handler now rejects non-loopback clients
(403). Binding --host 0.0.0.0 previously turned the proxy into an
unauthenticated TCP relay to arbitrary host:port (lateral movement /
port-scan bounce). Local isLoopback gate added (server.ts isLoopback
can't be shared — circular import).
- #76 (socket leak): tunnelThrough arms an 'aborted' flag on the 15s
connect timeout; the in-flight connectThroughProxy().then() now
destroys its late upstream socket and returns instead of writing 200
to a destroyed client socket + piping a leaked upstream.
- #79 (DoS): per-host SecureContext cache capped at 64 (LRU via Map
insertion order, access-refreshed). Keygen is also gated to
MITM-whitelisted hosts (isMitmHost), so this stays small in practice.
- #80 (credential leak): UPSTREAM_HOP_HEADERS completed (proxy-authenticate,
proxy-authorization, proxy-connection, te, trailer, upgrade) and
Connection-header-named headers now stripped dynamically on both
request and response (RFC 7230 §6.1). proxy-authorization carried
client→proxy credentials that leaked to the model endpoint.
- #115 (HIGH DNS rebinding): isTrustedAdminOrigin now validates the Host
header against the proxy's own listen address (localhost/127.0.0.1/::1
:port), not merely that Origin matches Host. A DNS-rebinding attacker
resolving evil.com -> 127.0.0.1 could previously forge Origin==Host and
reach loopback-only management endpoints.
- #116 (HIGH supply-chain): verifyTarballIntegrity checks the downloaded
tarball against the npm registry dist.integrity (sha512-base64) or
legacy dist.shasum (sha1); refuses to install if neither is present.
- #118 (LOW slowloris): MITM doMitm arms a 10s handshake timeout on the
client-side TLSSocket so a CONNECT that never completes (or trickle-
feeds) the ClientHello cannot hold the socket + signed cert open.
- #63 (trivial): initSessions comment said "savedAt" but sorts by
createdAt; corrected.
#78 (direct upstream connection timeout) was already handled by
tunnelThrough's existing 15s connectTimer — no change needed.
Introduce devlog/ iteration tracking (adapted from billion-context-pi):
README, REQ/WORKLOG/DESIGN templates, and a dogfood entry for this
windows-regression-ci change.
Ref: dog/billion-context-pi#32
Add ubuntu-latest + windows-latest OS matrix (Node 22/24, fail-fast:false)
to the test job, plus npm cache. typecheck/test(incl e2e-proxy-smoke)/build
now run on both Linux and Windows, matching billion-context-pi.
Ref: dog/billion-context-pi#32
release v0.1.40
chore: bump acp-kernel to 0.0.23 (fixes#133 reasoning-pairing)
acp-kernel 0.0.23 fixes the root cause of DeepSeek thinking 400
(ranxianglei/billion-context#133): reasoning_content and its companion
assistant text/tool-call are now kept paired across compression
(adjustBoundariesForReasoningPairs + stripOrphanedReasoning, mirroring
the tool-pair mechanism). The bug is structurally impossible after this.
fix(launcher): resolve client binaries cross-platform for Windows (bili codex ENOENT)
fix: textProtocol 只抑制 message item,保留 reasoning/image 生命周期
On Windows npm-installed CLIs (pi/codex/claude) are .cmd shims; spawning the
bare or extensionless name failed (ENOENT on the git-bash shim, EINVAL on .cmd
without shell per CVE-2024-27980 security patch in Node 18+).
- Add resolveOnPath(): path.delimiter (was hardcoded ':') + win32 ext order
[.cmd, .bat, .exe, ''] so pi.cmd wins over the extensionless git-bash shim.
- resolveClientCommand() returns the resolved full path (pi/codex/claude).
- runClient() spawns with shell:true on win32 (required to execute .cmd/.bat).
- SpawnFn options type gains shell?: boolean.
Verified end-to-end on Win10 VM: bili test pi spawns proxy, MITM-intercepts
open.bigmodel.cn, generates CA at ~/.local/share/billion-context/ca/, pi trusts
it via NODE_EXTRA_CA_CERTS, GLM responds, compression pipeline active.
346/346 tests, typecheck clean.
feat(config): three-level ACP/compression tuning (global > provider > model)
- CompressSettings: modelContextLimit, maxContextLimit, emergencyThresholdPercent,
nudgeGrowthTokens, preserveRecent*, minCompressRange, tiers (per-field cascade)
- mergeCompress/resolveCompress/applyCompressSettings (src/compress-settings.ts)
- per-request merge in server.ts (zero-cost when unset)
- injectNudge gate wired into anthropic/openai nudge injection
- maps user-facing fields to acp-kernel 0.0.20 internal names
- bump acp-kernel 0.0.19 -> 0.0.20
- 20 tests + README EN/zh-CN docs
release v0.1.39
fix(openai): rewrite non-compliant finish_reason=stop to tool_calls when tool calls present
Some OpenAI-compatible upstreams (e.g. the model behind openclaw) return
finish_reason="stop" for a text+tool_calls response, violating the OpenAI
Chat Completions spec (which requires "tool_calls"). bili faithfully
re-emitted "stop", and the downstream parser (openclaw
openai-transport-stream) dropped all tool_call chunks because hasVisibleText
kept stopReason=stop — so tool calls were silently lost and the model
"replied once and stopped". Now, when the streamed response emitted tool
calls, bili overrides the non-compliant "stop" to "tool_calls" (spec-
compliant). finish_reason=tool_calls and text-only stop are unchanged.
release v0.1.38
fix(openai): preserve reasoning_content round-trip for thinking-mode models
OpenAI-compatible reasoning models (DeepSeek-R1, GLM-4.6 thinking, Qwen-QwQ)
emit reasoning_content (CoT) on assistant messages and require it be echoed
back. bili's OpenAI adapter dropped it (grep reasoning_content = 0), so every
re-request after a proxy tool/compress got HTTP 400 'The reasoning_content in
the thinking mode must be passed back to the API.'
Mirror the existing Anthropic thinking handling: openaiToCore parses
reasoning_content into a contentType:reasoning coreMessage; coreToOpenai
re-emits it; adapter-openai parseStream captures delta.reasoning_content and
forwards it + includes it in the re-request.
release v0.1.37
fix(forward): handle absolute URLs in HTTP-proxy mode
fix(anthropic): forward round-2 stream with proper framing (vertical-text bug)
feat(mitm): auto-discover model domains from client configs (no hardcoding)
HTTP proxy-mode requests (client sends full absolute URL in the request
line, e.g. POST http://127.0.0.1:8199/v1/messages) were mangled: with no
configured route, the forward path did opts.upstream + req.url, which for
an absolute req.url produced garbage like
"https://api.anthropic.comhttp://127.0.0.1:8199/v1/messages" -> ECONNRESET.
Now: when req.url is an absolute http(s) URL and there is no matching route,
derive upstreamOrigin from the URL's own origin, forward to the URL as-is,
and set Host to the URL's host. /bili/ prefix routing and MITM path-mode
(/v1/messages) are unchanged.
Regression test tests/forward-absolute-url.test.ts: sends a proxy-mode
absolute-URL request and asserts the upstream receives /v1/chat/completions
with the real Host (not the default upstream).
Round-2 text after a proxy-tool re-request (e.g. acp_status) was emitted
via emitText(ev.delta) per delta. For Anthropic, emitText/buildTextBlock
wraps EACH call in a full content_block (start+delta+stop), so every ~2-char
delta became its own block → clients rendered each chunk on its own line
(vertical text). The adapter also gated content_block_start/stop behind
'round === 1', so round 2 had no framing at all.
Fix: forward round-2 content_block_start/delta/stop with new client indices
(remove the round===1 gate; firstRoundOnly: round===1), and have core.ts
yield ev.raw in ALL rounds (not just round 1) when the adapter provides it.
OpenAI/Responses round-2 (no raw) still falls back to emitText. textProtocol
(marker) path unchanged.
Regression test: round-2 deltas carry raw (pure delta, not a full block) +
content_block_start/stop forwarded + message_start not re-emitted.
release v0.1.36
fix(loop): stream round-2+ text in real-time (was buffered → 'stuck after tool call')
Model-free end-to-end regression: spins up the real startServer + a stub
upstream (openai SSE). Round 1 returns a compress tool_call (bili intercepts
-> re-request); round 2 returns text deltas 40ms apart. Asserts:
1. upstream received >=2 requests (re-request happened)
2. client received 'Hello world!' (response complete)
3. round-2 text chunks span >=50ms (streamed in real-time, not buffered)
Would have caught the round-2 buffering bug (#122) and compress-loop
integration regressions. No real model, deterministic, CI-friendly.
After a proxy tool (compress/decompress/acp_status) triggers a re-request,
the V2 loop forwarded round-1 text via ev.raw (real-time) but BUFFERED
round-2+ text and emitted it all at once via emitText(resolvedText) after
the stream completed. Symptom: after any ACP tool call, the next output
appeared only after a long pause (looked like non-streaming).
Now round-2+ non-textProtocol text is streamed chunk-by-chunk via
emitText(ev.delta) as it arrives (mirrors round-1 streaming). textProtocol
still buffers (extractTextTriggers needs the full text to strip markers).
release v0.1.35
feat(responses): inject read-only ACP tools for codex (hybrid protocol)
hideConsumedCompressCalls hid ALL orphan compress records — including FAILED
ones (no block created → orphan → hidden). The model then saw neither its
previous attempt nor the failure, so it reset to 'attempt 1' every round
and looped (tmux experiment: 10 rounds all '第 1 次尝试').
Only hide when no compress/decompress failed this round. A failed attempt
must stay visible so the model can count it and decide to stop.
The kernel's suggestion ('Combine more messages into your range(s) to meet
the threshold.') is correct and helpful — it tells the model how to make a
retry succeed. Only the proxy's own 'Do not retry the same range.' directive
(removed in 6bad08b) was wrong. Restore the kernel message verbatim.
acp-kernel's min-range error appends a directive ('Combine more messages
into your range(s) to meet the threshold.') telling the model what to do.
Per the principle that the failure result reports facts only (model decides),
strip that sentence so the result is purely factual, e.g.
'Total compressible content too small (3955 chars across 1 range(s), min 5000).'
The proxy-tool result must report the failure FACT only, not instruct the
model. 'Do not retry the same range.' was a directive the model followed
poorly (retried slightly-different ranges, looping). Per the principle
that failure is the tool output and the model decides its own next action,
the result now states only what failed.
The mutatedThisTurn (compress/decompress) and readOnlyCalled (acp_status)
guards short-circuited a SECOND call in a turn to a no-op. This was wrong:
the model may legitimately compress multiple different ranges, and a no-op
'Already compressed' message after a FAILED first compress was inaccurate
and sent the model into a retry spiral (v10.txt).
The real fix is f898da9: the proxy-tool result (success OR failure) is fed
back as a normal function_call_output (Responses) / tool-result (other
protocols). The model sees why a compress failed and decides its next
action — retry a different range, or stop. MAX_LOOP_ROUNDS bounds runaway.
Removed in both V2 (runCompressLoop) and V1 (compressLoopResponsesJson).
Guard tests #11/#12/V1 rewritten to assert the second call now EXECUTES.
Add compressProtocol: "tools" | "marker" to ProviderRoute (the existing
URL-keyed providers/routes config). Default unset = native function tools
for ALL upstreams (incl chatgpt.com). Set "marker" on a specific upstream
to force the text-trigger protocol for that URL only.
resolveCompressProtocol does the same longest-prefix URL match as
resolveConfiguredContextLimit. Replaces the hardcoded isChatGptCodexUpstream
hostname check in the responsesTextProtocol decision.
Config example:
providers: { "https://chatgpt.com": { "compressProtocol": "marker" } }
codex was forced onto text markers (hybrid protocol) by two false signals in
isCodexResponsesLite: the additional_tools field (codex always sends it) and
the x-openai-internal-codex-responses-lite header (codex_exec always sends
it). Both were assumed to mean the host cannot coexist with a declared tools
field — disproven empirically: codex calls both its exec/code_mode tool AND
injected ACP tools in the same turn.
Now only ChatGPT-subscription upstream (chatgpt.com backend, which genuinely
may not support custom tools) and the explicit ACP_COMPRESS_PROTOCOL=text
override keep the text-marker protocol. All API codex (comfly, openai.com)
gets the standard path: compress/decompress/search_context/acp_status all as
function calls, buildCompressSystemPrompt, proper function_call_output
feedback (the f898da9 fix).
Verified end-to-end (codex_exec 0.147.0 via bili -> comfly):
- forwarded tools = [compress, decompress, search_context, acp_status]
- standard function-based system prompt (not hybrid marker)
- code_mode (exec) still works alongside ACP tools (echo test EXIT 0)
isCodexResponsesLite kept (header-only) for documentation; no longer gates
the protocol decision.
textProtocol mode (codex Responses) fed ALL proxy-tool results back as
role:system developer messages (buildVisibilityMarker), never as a proper
function_call_output. The Responses API expects function_call ->
function_call_output pairing; a bare system message is ignored by the
model, so it re-calls the same tool (#119 run-on loop, hit MAX_LOOP_ROUNDS).
Fix: track which calls were native function_calls (from parseStream) vs
text-protocol markers (from extractTextTriggers). Feed native function-call
results back as a proper assistant tool-call + tool function_call_output
pair even under textProtocol; markers keep the system-message visibility
form. Also feed the model's raw assistantText (not the marker-stripped
resolvedText) so the model can see it already acted.
Verified end-to-end (codex acp_status via bili): round-2 body now carries
function_call_output; model stops at round 2 (was rounds 3-9 run-on).
When codex calls acp_status as a function tool, some models (e.g. gpt-5.6-sol)
re-invoke it every round after receiving the result instead of emitting a
final answer, burning upstream calls until MAX_LOOP_ROUNDS. Observed in tmux
end-to-end test: 7 repeated acp_status calls (rounds 3-9) before the cap.
Mirror the compress mutatedThisTurn guard with a readOnlyCalled set: a second
acp_status in the same request returns a no-op ("Already called acp_status
this turn; the context status has not changed since. Generate your final
response now.") instead of re-executing.
acp_status is idempotent (no args) so capping at one per request is safe.
search_context is intentionally NOT capped — it takes a query arg and may
legitimately be called several times with different queries.
End-to-end (codex exec via bili 8788): with the cap, codex called acp_status
once in round 1, then emitted a clean final answer in round 2 (was: 7 repeats
hitting MAX_LOOP_ROUNDS=10).
codex (Responses text-protocol) previously used text markers for ALL ACP
tools because of an unvalidated assumption that declaring `tools` disables
code_mode. Direct comfly A/B testing disproved this: declaring function tools
(top-level or additional_tools) does NOT disable codex's exec/code_mode tools.
Switch codex to a hybrid protocol:
- compress stays a text marker (batch ranges + immediate STOP is a poor fit
for a single function call, and the marker path already works well).
- decompress/search_context/acp_status are now injected as real function
tools so the model can call them directly (it often skipped emitting the
text markers for these read-only tools).
The compress loop already merges text triggers and function tool_calls
(core.ts allCalls = [...calls, ...extracted.calls]), so both paths coexist
in one turn with no loop change.
- buildCompressHybridSystemPrompt: compress marker + 3 function tools.
- ACP_READONLY_TOOLS_RESPONSES: the 3 read-only tools (no compress).
- injectResponsesTool takes an optional toolsToAdd param.
- prepareResponses + V2 loop use the hybrid prompt + inject read-only tools
for textProtocol; non-textProtocol path unchanged (all 4 tools).
fix: novel review bugs (registry ESM, logger rotation, persist drain, dump error, tarball cap)
fix(proxy): strip redundant in-place acp_summary (let compress tool-call carry it)
feat: cert-MITM launcher (bili pi/codex/claude + bili test pi)
- Add incoming-request dump (gated on --debug): logs
'INCOMING previous_response_id=... input_items=N instructions=...' and
writes raw/<ts>-INCOMING.txt (method/URL/headers-creds-masked/exact body).
Complements the existing outgoing raw dump so we can see what the client
ACTUALLY sends vs what bili forwards.
- ACP_KEEP_RESPONSE_ID=1 skips the previous_response_id strip (diagnostic).
- Rewrite the strip comment: the old 'would shift prefix / break prompt-cache'
rationale was an unverified assumption. Incoming-dump proved codex sends
store:false and never sets previous_response_id, so the strip is a no-op for
codex — kept defensively for clients that do chain (store:true).
raw dump no longer needs ACP_RAW_DUMP env; --debug alone enables it. Also
log a one-line build-feature banner at startup in --debug mode so users can
confirm they're running the launcher build (registry 0.1.34 lacks it).
codex advertises x-codex-beta-features: remote_compaction_v2, asking the
upstream to do server-side context compaction. This conflicts with bili's
client-side compress (bili is the compression layer) and third-party
aggregators (e.g. comfly) reject it with 'invalid range / ref not found'.
Strip the feature so bili's compress is the sole compression mechanism.
The JSON body dump (req-*.json) re-formats via JSON.parse/stringify and
OMITS headers, so it can hide a per-turn header (e.g. x-codex-turn-metadata)
that an aggregator might use for account routing — a plausible cache-breaker.
ACP_RAW_DUMP writes the exact bytes: request method/URL/all headers (creds
masked)/exact body string, plus response status/all headers. Opt-in via env,
gated on --debug. Output: <stateDir>/raw/<ts>-<sid>-{REQ,RES}.txt.
A client config whose base_url is already /bili/-wrapped (e.g. codex
config.toml base_url = http://127.0.0.1:8787/bili/https://ai.comfly.org/v1)
was unwrapped by classify() to https, added to the MITM whitelist, but got
NO rewrite. The client kept using the original wrapped http://127.0.0.1 URL
and connected directly to the old proxy (localhost, NO_PROXY), bypassing the
launcher's spawned cert proxy entirely.
Fix: track wrapped HTTPS upstreams in a new httpsRewrites list and rewrite
the client base_url to the raw https upstream so HTTPS_PROXY routes the
request through the spawned proxy -> cert MITM. Affects codex (-c raw),
claude (ANTHROPIC_BASE_URL=raw), and pi (models.json baseUrl=raw). Clean
unwrapped HTTPS base_urls need no rewrite (unchanged).
release v0.1.34 — bump acp-kernel to 0.0.19 (fixed 50K nudge growth)
registry.ts: require('node:fs').statSync is undefined in ESM (caught by try/catch) -> disk-cache 24h TTL never honored, network on every cold load. Use imported statSync instead.
logger.ts: after log rotation the post-rotation write still targeted the ended (old) stream because 's' was const-captured; post-rotation lines were lost. Make 's' reassignable and rebind it to the new stream.
persist.ts: flushAll only awaited sessions with a pending debounce timer; a session whose timer had already fired but whose writeNow was still in-flight (tracked in writeChains) was not awaited, so process.exit(0) could kill a mid-flight fs.rename and lose state. Also drain writeChains via Promise.allSettled.
server.ts: dumpStreamToFile's createWriteStream had no 'error' listener; an async stream error would crash the process. Add best-effort debug-log error listener.
update.ts: tarball download read the whole body into memory with no cap; a corrupt/malicious tarball could exhaust memory. Stream the body with a 100MB cap and reject on overflow.
.gitignore + node_modules: node_modules was accidentally tracked as a symlink blob; untrack it. Change the ignore pattern from 'node_modules/' (dir-only, does not match a symlink) to 'node_modules' so it cannot be re-added.
fix: reap decompress temp files (bounded retention)
fix(config/session): honor env proxy by default + validate port + bound session pool
fix(responses): add mutatedThisTurn guard + use MAX_LOOP_ROUNDS in non-stream loop
fix: abort compress loop on client disconnect + drain race + session inFlight race
- ensureProxyRunning: wrap spawnImpl in try/finally so closeSync(logFd) runs
even if spawn throws (prevents fd leak on spawn failure).
- stopProxy: only attempt the POSIX process-group kill (process.kill(-pid))
on non-Windows; child.kill() is the cross-platform default. Windows no
longer silently skips the group kill path.
resolveDecompress writes a /tmp/acp-decompress-<id>-<ts>.txt for every large
(>10000 bytes) decompress and never cleaned up — long sessions leaked unbounded
files. Add bounded retention: track generated paths, unlink oldest past
BILI_DECOMPRESS_TMP_CAP (default 50), best-effort clean on beforeExit.
- forward() streaming: abort runCompressLoop when client closes (AbortSignal
threaded into the loop + upstream fetch) so a dead connection stops burning
API on re-requests.
- pipe: race drain vs close + break on destroyed, so a destroyed socket no
longer hangs the request for UPSTREAM_TIMEOUT_MS.
- readBody: destroy the socket when body exceeds MAX_REQUEST_BYTES.
- handle(): acquireInFlight immediately after getSession (outside the lock),
release in an outer finally — closes the LRU-eviction split-brain window.
- recordUsage: only bump cacheSamples when a real cached-tokens sample exists.
- resolveProxyDecision: only short-circuit to direct when mode is explicitly
set; the default now falls through to HTTP_PROXY/HTTPS_PROXY/ALL_PROXY env.
- loadOptions: reject ports outside 1-65535 instead of silently coercing.
- session: don't exceed MAX_SESSIONS when eviction can't free an in-flight
slot (throw instead of growing the Map unbounded).
compressLoopResponsesJson executed every compress/decompress call unconditionally
and capped the loop at a hardcoded 5. Port the runCompressLoop mutatedThisTurn
guard (second mutating call in a turn is a no-op) and use the shared
MAX_LOOP_ROUNDS constant instead of 5.
# Conflicts:
# src/server.ts
feat(acp_status): append compressible ranges to default overview (align with Pi)
refactor: remove V1 streaming loops (V2-only) + dedup forward headers/routes
V2 (runCompressLoop) is the default and only maintained loop. Delete the
three V1 streaming loops (compressLoopStream / compressLoopResponsesStream
/ compressLoopAnthropicStream) and make V2 unconditional. Keep
compressLoopResponsesJson (non-stream responses-text path) and
buildVisibilityMarker (shared by V2 adapters).
Also: extract buildForwardHeaders() to dedup the request-header build
(M1); make config.loadOptions() call loadRoutes() instead of duplicating
the route-loading block (M2).
Pure refactor, zero behavior change.
Fix A (no re-request after compress) hung the session: the model emits a
compress function_call, the proxy executes it, but without re-requesting
the model never receives the result and never generates its actual
response → client sees a tool-call-only completion and stalls.
The re-request is the standard function-calling continuation (proxy acts
as the client: execute tool, feed result back, model continues). Restore
it for ALL proxy tools including compress/decompress.
To prevent the 0-char spiral (model re-targeting a just-compressed range
on a stale view in round 2+), add a one-compress guard: after the first
compress/decompress in a request, subsequent mutating calls return a
no-op result instead of re-executing. The model sees 'already compressed,
continue normally' and generates its response. This keeps one effective
compress per request without cutting off continuation.
Fix A for the recurring 0-char compress failures and cache drops:
the V2 loop re-requested after every mutating proxy tool, but the
re-request body diverged from the original forward (different prefix)
→ cache miss every compress round, and the stale coreMessages view
let the model re-target a just-compressed range → 0-char failures.
Pi avoids this by doing one model generation per request. Align: after
a state-changing proxy tool (compress/decompress), complete immediately.
The compress prompt already encourages batching multiple ranges in one
trigger, so round 1 captures all compress calls. This also removes the
re-prune mechanism entirely (it was dead code under this flow and was
itself buggy: triggered on failed compresses too).
fix: visibility marker role user→developer
Round N+1 of the V2 compress loop reused round-1's coreMessages, where a
just-compressed range was still raw and still listed compressible. The model
re-targeted it; applyCompression saw it covered -> '0 chars across 1 range'
failure. Pi avoids this because every generation re-runs processTurn (re-prunes
covered messages); the proxy V2 loop ran multiple rounds within one request
without re-pruning.
After a compress/decompress mutates state, re-run processTurn on the original
untagged messages, fold newly-covered ranges out (stripped — the compress
tool-call carries the content), and refresh the nudge. Round N+1 now sees the
post-compression view and can only target uncovered ranges. Also fixes the
related 2-round cache drop (stale coreMessages diverged from round-1 prefix).
Compress result marker was role:'user' → model interprets as
'user wants status report' → emits conclusion → task interruption.
Changed to role:'developer' (system note) in all 3 textProtocol paths:
- loop/core.ts:246: 'user'→'system' (coreToResponses converts to developer)
- compress-loop-responses.ts:425 (JSON): 'user'→'developer'
- compress-loop-responses.ts:683 (SSE): 'user'→'developer'
Marker is NOT deleted — model still sees compress results.
release v0.1.33
The visibility marker condensed acp_status to its first 3 lines, so the
user never saw the compressible ranges / nudge status / block details the
model received. Since the full result is already in the model's context
(function protocol: tool-result text=pr.result), hiding it from the
client saves nothing. Stream the full result so user + model see the same.
The visibility marker condensed acp_status to its first 3 lines, so the
user never saw the compressible ranges / nudge status / block details the
model received. Since the full result is already in the model's context
(function protocol: tool-result text=pr.result), hiding it from the
client saves nothing. Stream the full result so user + model see the same.
Proxy's acp_status returned only the base buildStatusReport (breakdown +
blocks); the model never saw which refs were valid to compress, so it
guessed covered/protected refs → 0-char compress failures. Pi's
handleStatus appends formatRanges(compressibleRanges, protectedRanges) +
nudge status on top of the base report. Mirror that: capture turn.nudge
in prepare* → thread via Prepared/LoopCtx → handleAcpStatus appends
ranges for the default scope (drill-down scopes now also pass
scope/view/tool/sort/limit through, which the proxy previously ignored).
Proxy's acp_status returned only the base buildStatusReport (breakdown +
blocks); the model never saw which refs were valid to compress, so it
guessed covered/protected refs → 0-char compress failures. Pi's
handleStatus appends formatRanges(compressibleRanges, protectedRanges) +
nudge status on top of the base report. Mirror that: capture turn.nudge
in prepare* → thread via Prepared/LoopCtx → handleAcpStatus appends
ranges for the default scope (drill-down scopes now also pass
scope/view/tool/sort/limit through, which the proxy previously ignored).
acp-kernel's prune injects an in-place acp_summary_* system message at the
compressed range — correct for a generic library that can't assume the host
keeps tool-calls. But this proxy (like billion-context-pi) lets the compress
tool-call itself carry the summary: the model wrote it in the tool-call
arguments, and hideConsumedCompressCalls keeps active-block compress calls in
the stream. The in-place summary is redundant, and inserting it mid-stream
shifts the upstream prefix-cache breakpoint. Strip it at all 4 processTurn
call sites (3 forward prepare paths + count_tokens).
acp-kernel's prune injects an in-place acp_summary_* system message at the
compressed range — correct for a generic library that can't assume the host
keeps tool-calls. But this proxy (like billion-context-pi) lets the compress
tool-call itself carry the summary: the model wrote it in the tool-call
arguments, and hideConsumedCompressCalls keeps active-block compress calls in
the stream. The in-place summary is redundant, and inserting it mid-stream
shifts the upstream prefix-cache breakpoint. Strip it at all 4 processTurn
call sites (3 forward prepare paths + count_tokens).
spawn() spread ...process.env, so the bili proxy child inherited the
shell's https_proxy/all_proxy/etc. bili reads those (config.ts:227-229)
as upstream-proxy fallback → upstream got routed through whatever
corporate proxy was in the user's shell, causing hangs when that proxy
stalled. Now strip the 6 generic proxy vars (http(s)_proxy / all_proxy,
lower+upper) from the spawned env so bili connects direct. NO_PROXY is
kept (harmless). Explicit chaining remains available via the dedicated
BILI_UPSTREAM_PROXY env (config.ts:218).
Launcher-spawned bili proxies used stdio:"ignore", so all proxy
stdout/stderr (incl. errors) were discarded — a hung run left zero
evidence. Now redirect stdout+stderr to /tmp/bili-proxy-<port>.log and
force --debug, so the next hang is traceable via tail -f.
fix(#99): skip body decode for non-protocol passthrough requests
GET /models and other non-protocol requests (no /chat/completions,
/v1/messages, or /responses suffix) no longer attempt content-encoding
decoding on the request body. Previously the decode ran unconditionally
before protocol detection, causing 'invalid zstd data' errors when the
client sent a content-encoding header on a request whose body was empty
or not ours to decode.
The decode step now runs only after protocol is determined, and only for
known protocol requests with a non-empty body. Passthrough requests
forward the raw body verbatim.
bili pi-test runs pi through bili exactly like bili pi but injects
--no-extensions, so no pi extensions load. This avoids double-compression
with the billion-context-pi client extension and lets the proxy own
compression for clean testing. claude/codex unchanged.
- ClientName += pi-test; LAUNCH_CLIENTS += pi-test
- baseClientName(): normalizer (pi-test -> pi)
- piTestArgs(): pure arg-injector (prepends --no-extensions for pi-test)
- runLaunch uses base for proxy/discovery/env/rewrite; injects flag for pi-test
- cli HELP + README updated
- +3 tests (297 total)
The cert launcher only proxied HTTPS upstreams (cert MITM is HTTPS-only);
HTTP upstreams were skipped by extractDomains and went DIRECT (uncompressed).
Now bili <client> auto-proxies BOTH schemes in one run:
- HTTPS upstreams -> cert MITM (HTTPS_PROXY + CA env + domain whitelist).
- HTTP upstreams -> /bili/ baseURL rewrite (no CA; client->bili hop is
http localhost, bili forwards+compresses).
Per-client HTTP rewrite mechanism (config-free, no hardcoded URLs):
- codex: repeatable 'codex -c model_providers.<name>.base_url=<wrapped>'
+ '-c openai_base_url=<wrapped>'.
- claude: ANTHROPIC_BASE_URL=<wrapped> env.
- pi: transformed models.json in an isolated PI_CODING_AGENT_DIR temp
(siblings symlinked, only http provider baseUrls rewritten);
cleaned up on exit. (Pi honors PI_CODING_AGENT_DIR, not PI_HOME —
loadClientConfig fixed to resolvePiHome.)
Discovery split by scheme: discoverRoutes() returns {httpsDomains,
httpRewrites}; MITM whitelist is https-only; httpRewrites drive the
/bili/ args/env/models.json transform. Localhost http requests bypass
HTTPS_PROXY (https-only) + NO_PROXY includes 127.0.0.1 -> direct to bili.
Verified end-to-end: 'bili pi -p --provider zhipuai-lb' (http provider at
127.0.0.1:18081) -> 'started proxy ... (HTTP /bili/ rewrites: 1)' -> OK
(request compressed via /bili/ forward). typecheck clean; 295/295 tests;
build ok.
Add a `bili <client>` launcher that starts (or reuses) the billion-context
proxy and points a coding agent at it via cert-MITM, so its HTTPS LLM
traffic gets compressed without editing the client's config.
Domain discovery is fully dynamic — the launcher reads each client's OWN
config (codex config.toml, pi models.json, claude ANTHROPIC_BASE_URL) at
runtime to find the real upstream domains. No upstream URL is hardcoded
in the launcher; custom relay domains are auto-whitelisted via the
discovered set, so any provider the client is configured to use gets
MITM'd + compressed.
- src/launcher.ts: shared daemon module. Discover provider domains from
client config; ensure a proxy is running (reuse an existing one only if
healthy AND its MITM whitelist already covers the discovered domains;
otherwise spawn a fresh proxy with the discovered domains passed via
BILI_MITM_DOMAINS); build the client's proxy+CA env; run the client;
stop the proxy on exit if the launcher owned it.
- src/config.ts: BILI_MITM_DOMAINS env (CSV) appends to mitm.domains.
- src/mitm.ts: export DEFAULT_MITM_DOMAINS (single source of truth, reused
by the launcher's reuse-vs-spawn coverage check).
- src/cli.ts: `bili <client>` + `bili test pi` subcommands; repeatable
--mitm-domain; ACP_PORT env honored alongside --port.
- runTestPi: isolated smoke test (pi -p --no-session --no-extensions
--no-tools "Reply with exactly: OK"), reuses/provisions the proxy the
same way as a real run.
CA: ~/.local/share/billion-context/ca/root-ca.pem (auto-generated by bili).
Clients set HTTPS_PROXY + a CA-trust env (NODE_EXTRA_CA_CERTS for pi/claude;
SSL_CERT_FILE for codex).
Verified end-to-end: pi/claude/codex cert mode → real MITM + compression →
completion. typecheck clean; npm test 280/280; build ok.
release v0.1.32
feat: text triggers for all ACP tools in textProtocol mode
fix(loop): preserve system/preamble prefix in V2 loop re-request (Responses + Anthropic)
Same class of bug as the Responses (Codex) fix in this PR.
Round-2 buildRequest for the Anthropic protocol overwrote the top-level system field with systemPrompt (compress prompt only). But anthropicToCore does NOT put system into msgs (only user/assistant/tool/reasoning), so the client's original system is unrecoverable from coreMessages. Round-1 (prepareAnthropic) computed systemOut = injectSystem(parsed, opts) = [client system text] + [compress prompt] via buildSystem (which also preserves the cache_control marker). Round-2 sent only the compress prompt.
Result: round-2 lost the client system prompt (persona/instructions) AND the system block content diverged from round-1, breaking Anthropic's prefix cache (the system block is the cache anchor) on every compress re-request.
Fix: thread the original parsed.system from prepareAnthropic through pickAdapter into the anthropic adapter. buildRequest now reconstructs system = buildSystem(extractSystem(original) + '---' + systemPrompt, original) — the same merge injectSystem uses in round-1 — so both the client system text and the cache_control marker survive and the prefix matches byte-for-byte.
OpenAI Chat is unaffected: its system is a role:system message that rides inside coreMessages and is re-emitted + merged identically by injectOpenaiSystem in both rounds.
TextProtocol mode (Codex code_mode) cannot use function-call tools because
declaring any tools disables server-side tools. Previously only compress had a
text trigger; the other 3 ACP tools were completely inaccessible.
Add text triggers for all 4 ACP tools:
- <acp_status></acp_status> (no payload needed)
- <acp_search>{"query":"..."}</acp_search>
- <acp_decompress>{"blockId":"b5"}</acp_decompress>
- <acp_compress>{...}</acp_compress> (existing)
extractTextTriggers now generically scans for all 4 trigger types.
buildCompressTextSystemPrompt documents all 4 triggers with examples.
Also includes upstream header debug logging.
Round-2 buildRequest for the Responses (Codex) protocol rebuilt the upstream input via coreToResponses(coreMessages) with only the compress prompt as the developer message. This dropped two things round-1 preserved via the projection:
1. systemParts (the original 'instructions') — round-1 put [...systemParts, prompt] in the developer message; round-2 sent only prompt, so the developer prefix diverged.
2. preamble (additional_tools / mcp_list_tools) — round-1 kept them at their layout positions via patchResponsesInput; round-2 omitted them entirely.
Result: the round-1 -> round-2 input prefix diverged from input[0], so the upstream prompt cache took a full miss on every compress re-request, and round-2+ silently lost instructions (system) and the Codex local tool declarations.
Fix: thread the ResponsesProjection (computed once in prepareResponses) through pickAdapter into the responses adapter. buildRequest now calls patchResponsesInput(projection, coreMessages) — the same function round-1 uses — so preamble + patched original messages keep their exact prefix by construction, then injects the developer message as [...systemParts, prompt] to match round-1. Headers (prompt_cache_key) are already preserved by runCompressLoop; only the body was rebuilt.
OpenAI Chat and Anthropic are unaffected (OpenAI keeps system in msgs; Anthropic uses cache_control markers) — only the Responses path changes.
The Codex textProtocol output_item suppression (PR #93) was too broad — it
suppressed ALL non-function_call/non-custom_tool_call items, including
reasoning, image_generation_call, etc. Delta events for these items still
reached the client, but without a matching added/done pair → parse failure.
Fix: narrow the gate to item?.type === 'message'. Only message items are
suppressed in textProtocol (emitText handles them). Reasoning, image, and
other item types pass through with their full lifecycle intact.
fix(session-id): recognize x-claude-code-session-id from Claude Code CLI
fix: Codex textProtocol 输出重复 + SPEC 同步
Codex (responses-lite / textProtocol) saw text output twice: the upstream's
output_item.added/done events for message items (which carry the full text)
were forwarded to the client alongside the V2 loop's emitText output.
Fix: gate both output_item.added and output_item.done message-item branches
on !suppressTextLifecycle (same pattern as content_part/output_text.done).
For textProtocol, only emitText generates text. For non-textProtocol
(pi/openai), behavior is unchanged (suppressTextLifecycle=false).
Claude Code has sent a per-session UUID header since v2.1.86
(x-claude-code-session-id, per the official llm-gateway-protocol doc).
Add it first to the recognized header list so every Claude Code session
gets its own proxy session id. Previously all sessions sharing a
CLAUDE.md fell back to a content fingerprint of the first user message,
collapsed onto one session id, and accumulated context until the
upstream timed out.
The header name is client-specific — opencode/codex/zcode/curl never
send it, and the content-fingerprint fallback is untouched, so other
agents behave exactly as before.
Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
fix(responses): read-only proxy tools no longer drive the compress loop (炸锅)
V2 has passed all 247 tests, live tests across all 3 protocols
(openai/anthropic/responses), and double-oracle review. Default-on;
baseline loops remain as fallback behind ACP_LOOP_V2=0.
COMPRESS_TOOL (Anthropic input_schema) was missing required:['content']
while OpenAI and Responses schemas had it. Anthropic-protocol clients
(e.g. Claude Code) could still trigger the empty-compress 'No ranges
provided' loop. Aligns all 3 schemas.
- compress tool parameters: add required:['content'] so models can no
longer call compress() with no ranges (was causing repeated empty
'No ranges provided' loops, see issue #22 /tmp/p.txt)
- core.ts: log each round's tool calls (name + arg snippet), proxy tool
results (snippet), and real tool call count when debug mode is on
root cause: V2 loop received untagged originalMessages (pre-processTurn)
but round-1 forward used tagged processedMessages (post-processTurn).
re-request body had different prefix → upstream cache miss (50% → 2-3%).
fix: pass processedMessages to V2 loop (with fallback to originalMessages
if processTurn failed). now re-request prefix matches round-1.
also add diagnostic dump for re-request body (*-REREQUEST.json) to verify.
Real OpenAI with stream_options.include_usage=true sends usage as a
SEPARATE trailing chunk with choices:[] + usage, distinct from the
finish_reason chunk. The old 'if (!choice) continue' skipped this chunk,
losing usage → lastInputTokens=0 → nudge/compression trigger misjudged.
Fix: extract usage before the !choice guard.
Regression test: separated choices:[] + usage chunk → lastInputTokens>0.
247/247 pass.
When the model calls a non-mutating proxy tool (acp_status, search_context),
the V2 loop consumed the call but returned to the client with
finish_reason='tool_calls' and NO tool_calls in the body — causing the
client (pi/opencode) to hang indefinitely waiting for tool calls that
never arrive.
Root cause: the re-request condition was 'sawMutating && realCalls === 0',
which only re-requested for compress/decompress. Non-mutating proxy tools
fell through to emitCompletion with the model's original finish_reason
('tool_calls'), producing a degenerate response.
Fix: re-request whenever ANY proxy tool was consumed and no real tools
were emitted: 'proxyResults.length > 0 && realCalls === 0'. This feeds
the proxy result back to the model so it can continue, matching baseline
behavior. MAX_LOOP_ROUNDS=10 still bounds the loop if the model keeps
calling proxy tools without progress.
Updated 4 tests that asserted the old no-re-request behavior.
246/246 pass.
- Dumps now default-on when --debug is passed (no need for ACP_DUMP_REQ=1).
Set ACP_DUMP_REQ=0 to opt out.
- Dump dir moved from tmpdir (cleared on reboot) to persistent
~/.local/state/billion-context/dumps/ (alongside bili.log).
- ACP_DUMP_DIR still overrides the location.
- Existing dump improvements kept: full body (no 50K truncation),
pretty-printed, session ID in filename.
- New tool: tools/analyze-dumps.mjs — computes prefix-overlap % between
consecutive dumps. ~100% = stable prefix (no compression). Drop =
compression or prefix change. Shows first divergent message index.
Usage: node tools/analyze-dumps.mjs [dir] [session-id?]
F3 gap fix (adapter-anthropic.ts): tool_use content_block_start no longer
advances clientIndex. The suppressed tool_use is re-emitted post-stream
via emitToolCall (which does its own clientIndex++), so it must NOT
reserve a forwarded slot. Previously the indexMap.set(upstreamIndex,
clientIndex++) for tool_use created a gap: text@0->0, tool_use@1
(buffered but slot reserved), text@2->2 left index 1 empty. Now
text@2 remaps to client index 1 (sequential, no gap, no duplicate).
Found by F3 regression test (sisyphus-jjunior subagent) which the
initial F3 implementation failed.
Regression tests added (tests/loop-adapters.test.ts, +9 tests):
- F1 CRLF: all 3 adapters yield text from \r\n-separated SSE
- F2 custom_tool_call: responses meta firstRoundOnly=false (survives round 2+)
- F3 anthropic index: forwarded indices strictly sequential (no dup, no gap)
- F4 network drop: all 3 adapters complete normally on upstream read() throw
- F5 empty upstream: responses -> response.failed (not fabricated completed)
246/246 pass.
Oracle double-review (loop core + adapters) findings:
- F1 CRLF (all 3 adapters): normalize \r\n|\r -> \n in buffer BEFORE
indexOf('\n\n') split, else CRLF upstream (legal SSE, 0D0A0D0A) is
silently swallowed -> empty completion.
- F2 custom_tool_call (responses): Codex code_mode host-tool calls now
passthrough as meta firstRoundOnly:false (was suppressed in round 2+).
- F3 anthropic index collision: port baseline indexMap. Round-1 text
blocks now consume a client slot + remap raw index; tool_use blocks
also consume a client slot (suppressed). Prevents duplicate-index SDK
crash when text follows tool_use.
- F4 network-drop (all 3 adapters): wrap reader.read() in try/catch,
break on error so parseStream ends normally -> core reaches
emitCompletion (was: abrupt stream death -> client hang).
- F5 empty upstream (responses): emit done finishReason:'failed' +
emitCompletion builds response.failed envelope (was: fabricated
empty response.completed).
Loop core verified SAFE (B1/B2/B3 at source, termination bounded,
炸锅 root cause closed). S1/S2/S5/buildRequest/tool-assembly verified
correct by both reviewers.
237/237 pass.
- loop #7 (B2): successful compress KEEPS active record in round 2
(compressCallId threading prevents the re-compress loop class).
Requires a 7-message corpus + >=5000-char messages + >=50-char summary
to clear the kernel's protected-zone / minCompressRange / minSummaryLength gates.
- loop #8 (B3): textProtocol round-2 body has NO function_call items.
- loop #9 (S2): responses session.stats.lastInputTokens populated via usage.
- loop #10 (S3): upstream-500 mid-loop terminates cleanly (timer cleared).
- adapters S1: openai single finish chunk (no double emit).
- makeCtx now uses defaultConfig(200000) so applyCompression has full config.
- drain() parametrized with optional adapter arg for textProtocol tests.
237/237 pass.
New src/loop/ subsystem: protocol-agnostic core (runCompressLoop) + 3 thin
SSE adapters (responses/openai/anthropic). Fixes injection-persistence 炸锅:
- philosophy systemPrompt is TRANSIENT (passed fresh to buildRequest, never
in coreMessages) — mirrors bili-pi's transient system-prompt model.
- hideConsumedCompressCalls runs each round (per-round hygiene).
- loop-limit yields graceful emitCompletion, never a degenerate empty turn.
- readonly proxy tools (acp_status/search_context) surface marker + end
gracefully; only compress/decompress drive re-request.
- shared executeProxyTool (de-dups the x3 baseline copies).
Original compress-loop-*.ts kept as baseline. A/B via ACP_LOOP_V2=1.
Apply the same MUTATING/READONLY split from PR #90 (responses path) to
the OpenAI chat-completions loop (compress-loop.ts) and the Anthropic
loop (compress-loop-anthropic.ts).
acp_status/search_context now execute once, surface their result to the
client as a visibility marker, and end the turn WITHOUT a re-request.
Only compress/decompress drive the re-request loop.
Address oracle review (PR #90, merge-after-nits):
1. Q2 (should-fix): JSON path dropped read-only markers when a real tool
accompanied them; stream path surfaced them. Drop the realCalls===0 gate
on surfaceReadonlyJson so both paths behave identically.
2. Nit: wrap read-only tool execution in try/catch (both stream + JSON) so
one failing acp_status/search_context emits a FAILED marker instead of
killing the whole turn's stream.
3. Nit: add markers.length suffix to surfaceReadonlyJson message id to
match the stream path's defensive id scheme.
Tests (+7, 212→219):
- stream: search_context-only (no re-request)
- stream: decompress-only STILL drives the loop (locks no-behavior-change)
- stream: real tool + acp_status surfaces marker, forwards call, no re-request
- stream: mixed-round assertion tightened (count [ACP] >= 2)
- JSON: acp_status-only surfaces marker, no re-request
- JSON: real tool + acp_status surfaces marker AND preserves call (Q2 parity)
acp_status and search_context are read-only: they execute server-side once
and surface the result to the client. Treating them like compress/decompress
(mutation) forced a re-request every time, so a model that called acp_status
looped until the round cap (5) was hit and the whole turn was discarded.
Now only MUTATING proxy tools (compress/decompress) drive the re-request
loop; READONLY tools (acp_status/search_context) execute once and end the
turn. A mixed round (compress + acp_status) still re-requests because a
mutating tool is present.
Adds two regression tests covering the read-only-only and mixed cases.
Fixesdog/billion-context-pi#22 (bili (5).log)
release v0.1.31
refactor: remove codex auto-takeover, unify to manual config (closes#83)
- Delete codex-takeover.ts (~400 lines) + tests
- Remove enable/disable/status CLI + API endpoints
- Remove web UI toggle button + route status badges
- Add manual config snippet + copy button (matches API-key card style)
- Add WS 426 for /codex/responses upgrade (fast-fallback)
- Always allow /codex/ requests (no takeover state gate)
- Cleanup note for users who previously auto-enabled
release v0.1.30
fix: inject response.failed for empty upstream responses (fixes stuck-after-tool UX)
When the upstream relay returns an empty response (no text content, no
tool calls, no usage object), the proxy previously synthesized a fake
response.completed and forwarded it to the client. The client (codex)
then received a 'completed' response with no useful output and hung —
the 'stuck after tool call' UX symptom.
Now the proxy detects the empty condition and injects response.failed
with a clear error message, so the client retries immediately instead
of hanging until a timeout.
release v0.1.29
feat: codex takeover, history sync, and embedded web UI
Addresses test-quality review (PR#73 comment #5234982438):
- codex-takeover: corrupted state file degrades to disabled (not throw)
- codex-takeover: non-numeric sectionPrefixLength rejected (not wipe config)
- web-routing: PUT providers takes effect without separate /reload
- proxy-session-id: affinityToken test name now matches assertion (was
'does NOT embed API key' but only checked passthrough)
Review feedback from ranxianglei (PR#73 comments #3-#4):
1. [#9 critical] Restore Responses session-identity fallback chain
(responses.ts): header → body.session_id → metadata.session_id →
previous_response_id → content-fingerprint(hashId). The previous
generated-randomUUID() broke compression for headerless clients
(Pi, plain OpenAI SDK) — every request got a new session so
compression state never accumulated.
2. [#1+#2 critical] parseState robustness (codex-takeover.ts): corrupted
state file now returns undefined (treat as stale/recoverable) instead
of throwing, which would leave Codex CLI unusable. Added
sectionPrefixLength type validation (non-negative number) to prevent
NaN slicing that could wipe the entire config.toml.
3. [Comment#4 #2] PUT /__bili/config onChanged now also refreshes routes
(server.ts): previously only proxy fields were refreshed, so pure API
callers (non-Web-UI) had to also POST /reload for provider config to
take effect. Now PUT guarantees save-then-apply in one call.
4. Fix misleading context-window hint on routing page (page.ts).
Routing page restructured per README:
- Category A (API-key clients, /bili/ prefix): OpenCode, Codex (API key),
Pi, Claude Code (API key), Generic (Cursor/Aider/Continue). Each card
shows the full prefixed URL with a copy button.
- Category B (subscription/login clients): Codex Route takeover (existing),
Claude Code (MITM), ZCode (MITM). Shows proxy + CA cert setup.
- Removed Gemini CLI card (not in README supported list).
- Generic copy-btn handler for all static-URL cards.
- config.ts: upstream proxyMode default changed from 'auto' to 'direct'
(bili is itself a proxy; should not chain through system HTTP_PROXY by
default).
- codex-history.ts: indirect specifier so esbuild cannot rewrite the
dynamic import('node:sqlite') into __require('sqlite'), which fails
to resolve at runtime. Node 22.5+ resolves the native import directly.
- web/client.ts: remove byId() calls for route-codex-switch,
upstream-global-mode, upstream-provider-override — these elements were
removed during the routing/upstream restructure but their JS setters
remained, causing 'Cannot set properties of null' on page load.
- routing: rename 'Codex 路由' → '客户端接入'; Codex is one client card
alongside Claude Code / Gemini CLI / MITM placeholders (each with
setup instructions). Generic lead text covers both integration methods.
- upstream: default proxy mode 'direct' (was 'auto' which silently picked
up system HTTP_PROXY env — risky for a proxy tool). Relabel modes:
直连(默认) / 手动代理 / 自动(跟随系统).
- upstream: add '按 URL 覆盖' structured table — edit per-Provider upstream
proxy inline (empty = inherit global). No longer buried in raw JSON.
- settings: relabel '高级设置', clarify it's for raw providers JSON
(adding providers, model context windows, etc).
Custom Codex providers routinely omit requires_openai_auth and
supports_websockets (they are optional in Codex's native config).
The strict validation threw an error for any custom provider missing
them, surfacing a confusing warning on the web config page even though
the provider was fully functional.
Default both fields to false for custom providers (matching Codex's own
behavior: custom providers don't use OAuth and don't support websockets).
The builtin 'openai' provider still uses its documented defaults.
Tighten boolField to require a boolean fallback so the resolved fields
are always typed boolean.
- responses.ts: route opaque call items (computer_call, file_search_call,
web_search_call, image_generation_call, code_interpreter_call, mcp_call)
through compression via contentType 'reasoning' with 'responses-call' kind.
Track custom_tool_call/custom_tool_call_output fully (port master).
Lift developer/system messages out of layout into systemParts (avoid
duplication when re-injecting as developer message).
- server.ts: include systemParts (base instructions) in the injected
developer message alongside the compress prompt; restore base instructions
even when compress injection is disabled. Strip previous_response_id and
top-level instructions (lifted into developer message).
- tests: update codex-official + proxy-responses-review for new behavior
(instructions lifted into developer message; custom_tool_call tracked).
fix(cache): route opaque call items through compression + strip chaining fields
Two remaining prompt-cache killers found via full audit (ACP tag drift
was cleared — tags are deterministic):
1. OPAQUE_ITEM_TYPES still pinned computer_call / computer_call_output /
file_search_call / web_search_call / image_generation_call /
code_interpreter_call / mcp_call in the preamble at input[0..] every
turn — same unbounded-accumulation bug pattern as reasoning. They are
prior-response output items (conversation history), not host
directives. Now routed as tracked BiliMessages via pushVerbatimItem
(contentType 'reasoning' + rawResponsesItem), so the kernel hides them
once their turn is summarized. Only additional_tools + mcp_list_tools
(definitions) stay opaque. Stock Codex code_mode was latent (uses
custom_tool_call), but MCP/computer-use clients hit this directly.
2. server.ts rebuilt body forwarded previous_response_id + instructions
verbatim. This adapter replays the FULL conversation in input
(stateless), so previous_response_id made the upstream resolve stored
server-side state ON TOP of the input we sent (duplication + a prefix
that shifts every turn as the id advances) — breaking prompt-cache.
instructions was already lifted into the developer message at input[1],
so forwarding it again double-sent it and violated the responses_lite
contract. Both are now stripped from the rebuilt body.
contentType 'reasoning' is the only safe bucket for the call items: the
kernel filters/pairs tool-call & tool-result by toolCallId and injects
ACP tags into text, while reasoning items pass through untouched (no tag,
no filtering) and are still covered by compression blocks.
readProviders previously read only the inline config.providers entries
without normalizeUrlKey or merging the external ACP_PROVIDERS file, so the
web UI could show a different route set than the running proxy actually
used. Delegate to loadRoutes() (already used by the /reload endpoint) so
display == runtime.
Ported master's reasoning-through-compression (#71) and count_tokens (#72)
features onto PR#73's layout-based patching architecture:
responses.ts:
- Removed 'reasoning' from OPAQUE_ITEM_TYPES (master: reasoning is
conversation history, routed through compression, not preserved verbatim)
- Added droppedReasoning counter + shouldDropAllReasoning() escape hatch
(ACP_REASONING_KEEP=none)
- Added 'reasoning' case to responsesToCore switch: creates a tracked
BiliMessage (contentType 'reasoning', rawResponsesItem carried) so the
kernel hides it once its turn is summarized; dropped reasoning uses
'continue' to skip layout.push so it is fully removed
- Added reasoning re-emit to coreToResponses (assistant contentType
'reasoning' -> push raw verbatim)
- patchOriginalItem needs no change: reasoning items are preserved via
slot.original when their turn is live, dropped (nextById miss) when
compressed -- correct by construction
server.ts:
- Added countTokens detection (isCountTokensRequest) + prepareCountTokens
(read-only processTurn mirror so count_tokens reports post-compression
count; ACP_COUNT_TOKENS_PASSTHROUGH=1 escape hatch)
- Merged prepare ternary: countTokens -> anthropic -> openai ->
responsesCompact -> responses
Validation: typecheck clean, 216 tests pass (incl. 9 reasoning + 3
count_tokens + custom_tool_call fix), build ok.
Codex code_mode tools (shell_command/apply_patch, declared via
additional_tools) come back from upstream as custom_tool_call output
items, not function_call. In text-protocol mode (auto-enabled for Codex
by PR#73), the compress loop's realtime yield gate suppressed ALL
non-meta events to buffer output_text for compress-trigger scanning.
custom_tool_call items were neither captured (only function_call is) nor
re-emitted at end-of-turn (only output_text is) — so every code_mode
tool call was silently dropped, and Codex received only text. This is
the root cause of the reported '无法使用工具'.
Fix: tag custom_tool_call events with noBuffer so they bypass the
text-protocol suppression and stream to the client in realtime (the same
behavior that already works in non-text-protocol mode). output_text is
still buffered for trigger scanning. Also count custom_tool_call items
in [acp-diag] so code_mode usage is visible (allCalls is function_call
only).
Note: NOT tested against a live Codex session. Validated via:
- npm run typecheck (clean)
- npm test (202 pass, incl. new regression test that fails on the
unpatched code and passes with this fix)
Conflict note: touches only src/compress-loop-responses.ts (PR#73-only
file, not modified by master), so it will not conflict with the
responses.ts/server.ts merge conflicts that PR#73 currently has with
master.
release v0.1.28
fix: route Responses reasoning through compression pipeline (Codex cache explosion)
Addresses oracle review of PR #71 (blocking B1 + follow-ups):
- reasoning vanishes from output once its turn is covered by a block
(the central guarantee of the fix, previously untested)
- reasoning id is stable across turns (Codex re-sends same items)
- multiple reasoning items keep distinct ids and order
- reasoning without encrypted_content (older API) round-trips
Refs: dog/billion-context-pi#15
fix: compress count_tokens so Claude Code never trips autocompact
- Extract isCountTokensRequest() (pure) so the routing-detection + escape
hatch are unit-testable without spinning up an HTTP server (repo tests are
unit-level). Covers path-variant detection, non-count_tokens rejection, and
ACP_COUNT_TOKENS_PASSTHROUGH=1.
- Deepen the read-only test to a full JSON snapshot of session.state + stats,
catching nested mutations (block.survivedCount/generation, nudge baselines)
that the previous field-by-field check missed.
- Document the cross-repo read-only invariant (relies on acp-kernel syncBlocks
deep-cloning) and the load-bearing compressInjected:false on the Prepared
return (prevents the usage-capture tail from poisoning lastInputTokens).
- NIT: drop redundant endsWith clause, drop unnecessary `as Prepared` casts.
176 tests pass (173 + 3 new detection/escape-hatch).
Responses API `reasoning` items were treated as opaque preamble and
re-sent verbatim every turn, accumulating without bound (broke Codex's
prompt-cache prefix and blew up context). Anthropic `thinking` already
flowed through the compression pipeline as a tracked message and was
bounded; only the Responses path was broken.
Route `reasoning` the same way: convert each item to a tracked
BiliMessage (contentType "reasoning") that carries the original item
verbatim in rawResponsesItem. The kernel hides the message once its
range is compressed, so old reasoning drops automatically and never
needs to be merged into a text summary (which would corrupt the opaque
encrypted_content).
Escape hatch: ACP_REASONING_KEEP=none drops all reasoning items.
Refs: dog/billion-context-pi#15
- Add codex-takeover module to intercept and redirect codex traffic
- Add codex-history for persistent conversation history sync
- Add codex-provider for active provider capture
- Add content-encoding helpers for transparent body decoding
- Replace monolithic src/web.ts with modular src/web/ (api/client/page/styles)
- Refactor upstream-proxy: cleaner CONNECT handling, per-URL config
- Update responses/server/cli/config to wire new modules
- Add 6 new test files covering codex, web routing, content encoding
Co-Authored-By: AtomCode (GLM-5.2) <noreply@atomgit.com>
Route Anthropic /v1/messages/count_tokens through the proxy compression
pipeline (read-only processTurn, state discarded) so the relay returns the
POST-compression token count. CC sizes its autocompact window off this count;
reporting the compressed count keeps CC below its threshold indefinitely,
eliminating the autocompact-induced task drift reported in #68.
prepareCountTokens mirrors exactly what the next /v1/messages would forward
(covered messages pruned, summaries injected, refs tagged) but does NOT mutate
session.state, create blocks, or nudge. forward() does plain pipeThrough for
the non-streaming JSON response (compressInjected=false).
ACP_COUNT_TOKENS_PASSTHROUGH=1 restores the old unchanged-forward behavior.
release v0.1.27
feat: upstream proxy support (firewall / GFW)
F1 CRITICAL (mitm.ts): tunnelThrough attached 'connect' listener inside
connectThroughProxy().then(), but connectThroughProxy resolves ON/AFTER
the socket's 'connect' event — Node doesn't replay it, so the handler
never fired, established stayed false, client timed out after 15s (504).
Regression from master affecting ALL non-whitelisted MITM CONNECT tunnels
(default config included). Fixed: connectThroughProxy returns an already-
connected socket (direct: post-'connect'; proxied: post-handshake), so
start piping immediately in .then() — no second 'connect' wait.
F2 MINOR (upstream-proxy.ts): dispatcherCache never evicted → ProxyAgent
leak on URL rotation. Added resetProxyCache() (closes+clears), called on
config hot-reload.
F3 MINOR (upstream-proxy.ts): connectThroughProxy had no internal CONNECT
handshake timeout (only the outer 15s tunnel timer). Added a 10s
handshake timer that fails fast with a clear 'CONNECT handshake timeout'
error, independent of the tunnel-level timeout.
Other review findings (mitm:// scheme, longest-prefix, ''-means-direct,
undici ^7, zero-regression for /bili/ clients) confirmed correct.
Node 25 ships undici 7.24.4 as its global fetch. The undici@8.10.0 package's
ProxyAgent is incompatible with Node 25's fetch core — dispatcher protocol
mismatch surfaces as 'invalid onRequestStart method' / 'fetch failed' on
every proxied request. Pinned undici to ^7 (7.29.0 resolved) which matches
Node 25's bundled version, so ProxyAgent works with globalThis.fetch.
Verified: ProxyAgent via gost HTTP proxy → 401 from real upstream (request
reaches GLM through the proxy). tsc 0, 169 tests, build self-contained.
Problem: MITM (login clients like ZCode) and /bili/ (API-key clients) both
hit the same upstream host (e.g. open.bigmodel.cn). resolveUpstream built
identical rewrittenUrl for both (https://open.bigmodel.cn/...), so per-URL
config (proxy, context) couldn't tell them apart — a host-level key
configured one and implicitly the other.
Fix: MITM traffic now carries a synthetic `mitm://` scheme in rewrittenUrl
(for config lookup only). The real upstream stays https:// (in `upstream`,
used by fetch). Mapping is bijective: mitm://<host><path> ⟺ https://<host><path>.
forward() strips mitm:// back to https:// before calling fetch (fetch would
reject mitm://). compress-loop ctxs carry the https upstreamUrl unchanged.
Now you can give ZCode its own proxy without affecting API-key clients:
{
"providers": {
"mitm://open.bigmodel.cn": { "proxy": "http://20173" },
"https://open.bigmodel.cn/api/anthropic": { "proxy": "http://20172" }
}
}
Future-proof: Codex login (chatgpt.com) and Claude Code (api.anthropic.com)
will also be mitm:// keys when MITM support lands for them.
Verified: tsc 0, 169 tests pass, build OK. resolveProxy() now resolves
distinct proxies for mitm:// vs https:// keys to the same host (tested).
README (EN + ZH) documents the scheme distinction.
Allows billion-context to route its OWN outbound connections to model
providers through an HTTP proxy. Use case: a Codex/Claude Code user behind
the GFW runs v2rayA; configuring the upstream proxy lets api.openai.com
traffic reach the model instead of timing out.
Config (providers.json / billion-context.json):
{
"proxy": "http://127.0.0.1:20172", // global default
"providers": {
"https://api.openai.com/v1": {
"proxy": "http://127.0.0.1:20173", // per-URL overrides global
"models": { ... }
},
"https://open.bigmodel.cn/api/anthropic": {
// no proxy → inherits global
}
}
}
Rules: per-URL proxy overrides global; neither set = direct connect. Only
HTTP proxies supported (socks5 deferred).
BOTH outbound paths covered:
- /bili/ path-mode (fetch): undici ProxyAgent injected as dispatcher.
Applies to all 4 fetch sites (forward + 3 compress-loop round-trips).
- MITM CONNECT tunnels: tunnelThrough() now connects via HTTP CONNECT to
the proxy first, then pipes. MITM-decrypted hosts don't use this (their
outbound is fetch-based via handle→forward).
New files:
- src/upstream-proxy.ts: parseHttpProxy, resolveProxy (longest-prefix +
child-overrides-parent), proxyDispatcher (cached ProxyAgent),
connectThroughProxy (direct or HTTP CONNECT tunnel), connectTlsThroughProxy.
Changed:
- config.ts: ProviderRoute.proxy + ProxyOptions.proxy + FileConfig.proxy +
BILI_UPSTREAM_PROXY env + parseRouteEntry parses proxy field.
- fetch-util.ts: fetchWithTimeout takes FetchOptions (RequestInit +
dispatcher), Omit pattern avoids @types/node vs undici Dispatcher conflict.
- server.ts: forward injects dispatcher; 3 compress-loop ctxs carry proxyUrl;
setupMitm gets resolveProxyUrl callback for blind-tunnel outbound.
- mitm.ts: tunnelThrough rewritten async (connectThroughProxy promise).
- tsup.config.ts: noExternal += undici (devDep, inlined into dist).
- 3 compress-loop: Ctx + proxyUrl, fetch injects dispatcher.
Verified: tsc 0, 169 tests pass, build OK, dist self-contained (0 external
imports, 2.0M with undici inlined).
release v0.1.26
fix: 6 audit findings (lossless protocol + security + Windows + packaging)
Root cause: writeNow/flushSync were not serialized per-session — two
concurrent writes to the same session (debounce timer firing while a previous
write is in flight, or flushSync during eviction) both call fs.rename on the
same destination. On Linux rename is atomic; on Windows (MoveFileEx with
MOVEFILE_REPLACE_EXISTING) it throws EPERM/EBUSY/EACCES when the destination
is momentarily held (AV scan, search indexer, SMB share).
Fix:
- writeChains: Map<sessionId, Promise> serializes writeNow per session — each
write chains after the previous so two renames never race on the same file.
- renameWithRetry (async) + renameSync retry loop: on EPERM/EBUSY/EACCES,
brief backoff + retry (up to 3×). Transient Windows locks usually release
within ms, so this turns a hard failure into a near-transparent hiccup.
- flushSync body rewritten cleanly (the prior shape had mkdirSync separate
from the write+rename; consolidated and added the retry loop).
#1 (CRITICAL) protocol conversion was lossy: image->[image] placeholder,
thinking signature dropped (Anthropic rejects unsigned thinking), tool_result
.is_error dropped, OpenAI developer->system, image_url/input_image deleted.
Fix: BiliMessage sidecar (src/bili-message.ts) extends CoreMessage with
optional raw*/signature/isError/originalRole/image* fields. The kernel only
does {...msg} spread copies, so sidecar survives the pipeline to coreToX,
which replays the original block losslessly. 8 round-trip tests added.
#2b (MAJOR) session-id extractKey() lowercased the API key before hashing —
two distinct valid keys differing only in case collided into one hash bucket.
Remove toLowerCase; keep raw (trim only).
#3 (MAJOR) auto-update stale-lock takeover never deleted the stale lock file,
so writeFile({flag:'wx'}) always failed EEXIST -> catch returned null ->
a single crash during update permanently blocked all future auto-updates.
Add unlink(LOCK_FILE) before the wx write, with ENOENT tolerated.
#4 (MAJOR, security) /__bili/ management endpoints (config read/write, reload,
session stats) had no auth. With --host 0.0.0.0 a remote caller could rewrite
upstream routing to exfiltrate API keys. Gate all /__bili/ + /__acp/ paths to
loopback connections only (127.x / ::1); model-traffic endpoints stay open.
#6 (MINOR) build artifact was not self-contained: acp-kernel stayed as a
runtime dependency (dist had 'from "acp-kernel"'). tsup now sets
noExternal:[acp-kernel,node-forge] and acp-kernel moves to devDependencies,
keeping the exact pin (0.0.17). dist verified: 0 'from acp-kernel' refs.
release v0.1.25
feat: MITM transparent proxy for OAuth/subscription clients (ZCode, Codex, Claude Code)
Split 'Examples by client' into two clear categories:
A. API-key clients (pi/opencode/codex/cursor/...) — /bili/ prefix
B. Login/subscription clients (ZCode/codex-login/claude-code) — MITM mode
Adds a comparison table for login clients (ZCode/codex/claude-code) showing
their login type and hardcoded endpoint. Makes the 'why MITM' rationale apply
to the whole login-client category rather than only ZCode, since Codex
(ChatGPT account) and Claude Code (Claude subscription) hit the same OAuth-
hardcoded-endpoint wall.
Root cause: the compress/decompress/acp_status loops received
prepared.processedMessages as their ctx.messages. But processedMessages is
processTurn's OUTPUT, where messages already covered by a compression block
have their text replaced with placeholders/summaries. When the model later
called compress with refs pointing into those messages, collectBlockContent
found empty text → 'Total compressible content too small (0 chars)' and the
tool failed.
This only manifests AFTER the first successful compression in a session (round
1 has no blocks yet → processedMessages still has full text → works). From
round 2 on, every compress call into a folded region failed.
Fix: capture the raw CoreMessages from the protocol conversion (anthropicToCore/
openaiToCore/responsesToCore output, BEFORE processTurn) as prepared.originalMessages
and feed THOSE to all three loops. processTurn preserves message ids, so the
refs the model sees are identical — but originalMessages still carries the full
text for collectBlockContent to read.
Matches pai-acp's pattern (entriesToCoreMessages from raw session entries, not
processTurn output).
README (EN + zh-CN): add a dedicated 'ZCode (special: MITM mode)'
subsection under Quickstart Option A. ZCode is the one client that cannot
use the /bili/ prefix (its OAuth provider hardcodes the endpoint), so it
needs the MITM transparent-proxy path: set HTTP proxy + CA cert path in
ZCode settings. Explains why MITM is needed, the whitelist scope, the
one-time CA trust setup, and the security properties (local-only CA,
non-model traffic never decrypted).
logger.ts: rewrite to be crash-proof and self-healing:
- process.stderr.write wrapped in try/catch (EPIPE from a closed reader
used to throw an uncaughtException and kill the whole proxy — the real
reason bili.log was always empty).
- the held WriteStream now listens for 'error' and drops itself so the
next write reopens; stream.writable checked before write. Recovers from
orphaned fds (file unlinked/replaced by rebuild/restart).
Root cause of the vanishing-proxy + empty-bili.log mystery. log() called
process.stderr.write(line) WITHOUT try/catch. When the stderr reader closes
(terminal shut, piped process exited, broken pipe) write() throws EPIPE.
As an uncaught exception in a hot path, it killed the whole proxy — which is
why every bili start --debug session silently died and bili.log stayed 0 bytes
(the process never lived long enough to write much, and the file inode was
already detached).
Fixed: wrap BOTH stderr.write and the file-write block in try/catch so
logging can never crash the server. Combined with the earlier existsSync()
reopen, the log file is now reliable.
Critical: lastInputTokens = input_tokens only, ignoring cache_read_input_tokens.
Providers split the prompt into new (input_tokens) + cached (cache_read_input_tokens).
The TOTAL context that fills the window is the SUM. Using only input_tokens makes
a cached session look tiny — e.g. ZCode review showed usage=0% (601/1M) while the
real context was ~40k (601 new + ~40k cached system prompt). With cache_read
ignored, the nudge never fired and compression never happened even though the
context was genuinely large.
Fixed all 4 usage-capture sites (3 streaming loops + non-streaming path):
lastInputTokens = input_tokens + cache_read_input_tokens (total context)
inputTokens stat (for cost display) still accumulates only new input_tokens.
Also committed earlier this turn (86ce78f): non-streaming usage capture +
OpenAI stream_options.include_usage injection — both from researcher audit.
Two more gaps in the same class as commit 45aae16 (tokenCount must be the
real upstream input_tokens, never an estimate):
1. Non-streaming path (server.ts rewrite*JsonResponse branch) never read
json.usage → lastInputTokens stayed 0 → compression/truncate never
fired for non-streaming sessions. Now extracts input_tokens/prompt_tokens
(per-protocol field names) + cache + output and writes to session stats,
aligned with the streaming loops' onCacheUsage.
2. OpenAI Chat Completions streaming only sends usage in the final chunk
when stream_options.include_usage=true. The proxy never injected it,
so streaming OpenAI sessions whose client didn't set it also got
lastInputTokens=0. Now forced on for any streaming OpenAI request that
doesn't already opt in. Anthropic/Responses emit usage unconditionally,
so this is OpenAI-specific.
Found by researcher audit (del_mslfy6j8_shi9). Task two (self-built vs
kernel) all clear — no duplicates, kernel text constants correctly reused.
Root cause: createWriteStream opens the inode at startup and holds it. If the
path is later deleted/replaced (rebuild, restart, rm, logrotate), the stream
keeps writing to the now-unlinked inode — writes 'succeed' but vanish into a
black hole. The proxy appears to run but produces no log file, even though
diagNudge/forward logs fire normally.
Observed: PID 2359696's fd 22 -> bili.log.old (deleted); fresh bili.log was
0 bytes for the whole session, so nudge/growth diagnostics never surfaced.
Fix: check existsSync(logPath) before each write; reopen if the file has
vanished. Cheap stat per log line; only reopens on the rare delete case.
Root cause: tokenCount (drives the nudge/compress decision) was computed via
estimateTokensFast = chars/4. This undercounts CJK text 3-4x (Chinese chars
count as 0.25 token instead of ~1), so Chinese-heavy sessions (ZCode reviewing
a CN project: real 50k tokens) read as ~35k — never reaching the 50k compress
threshold. Compression never fired.
The upstream API already reports the authoritative input_tokens in every
usage (Anthropic message_delta, OpenAI prompt_tokens, Responses input_tokens).
That is the ground truth — no estimate (neither chars/4 nor a BPE tokenizer)
can beat it. Use it.
Changes:
- session.stats: add lastInputTokens (single-turn, overwritten each turn).
Existing cumulative inputTokens is kept for the web UI totals.
- compress-loop-{anthropic,openai,responses}.ts onCacheUsage/usage: also
set lastInputTokens = input (overwrite, not accumulate).
- server.ts three prepare* branches: tokenCount = session.stats.lastInputTokens.
First turn = 0 (no history yet, never triggers anyway). All estimateTokensFast
calls removed from the tokenCount path.
- persist.ts: round-trip lastInputTokens with backward-compat (old files
default to 0).
- tests: stats literals updated.
estimateTokensFast is still imported (used by buildStatusReport for the
acp_status display breakdown), but no longer drives any compress/nudge decision.
Root cause: contextTokens was computed from anthropicToCore()/responsesToCore()
output, but BOTH exclude the system prompt:
- Anthropic: body.system (huge for ZCode — workspace reminders) is collected
by extractSystem() but never added to msgs, so estimateTokensFast missed it.
- Responses: body.instructions (huge for Codex) + system/developer items are
collected into systemParts but never added to msgs.
- OpenAI: system is message[0] → already in msgs → already correct.
Effect: ZCode review session showed contextTokens=38584 while real input was
1.7M tokens. The undercount meant usage stayed at ~4% of 1M limit → nudge
threshold never crossed → compression never triggered, even at 1.7M tokens.
Fix: concatenate the system text into the tokenCount estimate for the
Anthropic and Responses paths. OpenAI path unchanged (already correct).
Root cause: compress-loop-anthropic emits a synthetic message_delta at the
end of the stream to close the response. It only carried output_tokens.
But GLM (and many providers) put the REAL input_tokens + cache_read in
message_delta — which the proxy SUPPRESSES. So standard clients (ZCode)
reading message_delta saw input=0 and reported a tiny context size
(e.g. 2166 for a 300k-token review).
Direct GLM→ZCode works fine; the proxy was the regression (introduced
when compress-loop-anthropic replaced the single-pass rewriter).
Fix: accumulate input_tokens + cache_read across rounds (alongside the
existing output_tokens accumulation) and include all three in the
synthetic message_delta usage. Not ZCode-specific — any Anthropic-spec
client that reads usage from message_delta now gets correct values.
Bug 1 (server.ts): cacheHitPct NaN when inputTokens=0 (divide-by-zero
on GLM which reports input_tokens=0 in message_start). Guard now
requires inputTokens>0.
Bug 2 (server.ts): non-streaming arrayBuffer() throw skipped
clearUpstreamTimer() (timeout leak) and markDirty() (lost compress
block persistence). Wrapped in try/finally so timer always clears.
Bug 3 (mitm.ts): MITM TLSSocket had no 'error' listener — a TLS
handshake error (client cert reject / abrupt disconnect) crashed the
whole proxy. Added error handler that destroys sockets + logs.
Bug 6 (registry.ts): providerFromHost used endsWith() with no '.'
boundary — 'api.openai.com' matched key 'penai.com'. Now requires
exact host match or '.subdomain' boundary.
Bug 7 (stream-openai.ts): routeOpenaiEvent only read tool_calls[0].
A chunk carrying 2+ tool calls dropped the args of the 2nd+. Now
loops over all entries, accumulating compress args + marking sawReal
for each.
conversationSignalAnthropic/Openai/Responses were called with only
opts.sessionHeader (a user-configured header name, usually unset), so the
conversation dimension fell through to the content fingerprint fallback —
hash of the first user message's first 200 chars. IDE clients like ZCode
inject a fixed <system-reminder> (workspace context) at the start of every
new conversation, so different conversations collided on the same 200-char
prefix → same sessionId → compression state (blocks) leaked across
sessions.
clientConversationHeader() already extracts the strongest signal
(x-session-id / x-session-affinity / x-acp-session / x-opencode-session),
but it was only used for the web-UI label, not for the session-id
derivation. Now the client header is preferred for session-id too;
content fingerprint remains the last-resort fallback for clients that
send no conversation header at all (pi).
The Anthropic path used rewriteSseStream — a single-pass rewriter that
intercepted proxy tool_use blocks and emitted their result as a text delta
TO THE CLIENT. That result never went back to the upstream LLM, so
query-type tools (acp_status, search_context) broke the model's reasoning
chain: the model called acp_status, proxy ran it, but the model never saw
the result → couldn't decide to compress next → client hung.
New compressLoopAnthropicStream mirrors compress-loop.ts (OpenAI chat) and
compress-loop-responses.ts: intercept proxy tool_use → execute → push
assistant(tool_use) + user(tool_result) back into the request → re-request
upstream → loop until the model stops calling proxy tools. The tool RESULT
reaches the model, so acp_status→compress flows work.
- New: src/compress-loop-anthropic.ts (multi-round SSE loop)
- server.ts: anthropic streaming branch now uses the loop (same pattern as
openai/responses branches) instead of rewriteSseStream
- Index remapping: proxy tool_use blocks suppressed, client sees sequential
indices continuing across rounds; round 2+ content gets remapped indices
- message_start forwarded in round 1 only; message_delta/message_stop always
suppressed and re-emitted with accumulated output_tokens at the end
When a proxy tool_use was converted to text (compress/decompress/
search_context/acp_status), the upstream message_delta was suppressed but
its usage discarded, and the synthetic replacement hardcoded usage:{}.
ZCode's Zod schema requires usage.output_tokens:number → 'Turn execution
failed'.
Capture upstream usage on both converted/non-converted paths; pass it to
buildStopReasonRewrite. Default output_tokens:0 so strict clients still
pass even if upstream omitted it.
stream.ts only intercepted COMPRESS_TOOL_NAME; decompress/search_context/
acp_status tool_use blocks passed through to the client → 'Tool not found'.
Mirror the OpenAI path: block all 4 PROXY_TOOL_NAMES tools, execute them
internally (executeAnthropicProxyTool, same logic as compress-loop.ts
executeProxyTool), emit the result as a text delta replacing the tool_use.
BlockState: isCompress→toolName:string|null. Non-streaming rewriteJsonResponse
updated the same way.
The system prompt (buildCompressSystemPrompt) tells the model it has five
context-management tools (compress/decompress/search_context/acp_status) and
documents how to call each. But the Anthropic request path only injected
COMPRESS_TOOL as a function tool, so clients on that path (ZCode, Claude
Code) saw the tool docs in the system prompt but could not actually call
decompress/search_context/acp_status — the model reported 'I can see them
described but they are not in my tool list'.
Add Anthropic-format definitions (name + description + input_schema) for the
other three tools and inject all four on the Anthropic path, matching what
the OpenAI and Responses paths already do. Fixes the mismatch surfaced by
the ZCode MITM test (prompt injected fine, tools did not).
node-forge is a CommonJS module that calls require('crypto'). When
tsup/esbuild inlines it into our ESM output, the __require shim throws
'Dynamic require of crypto is not supported' because require is undefined
in an ESM context. Inject a createRequire-based require at the top of the
bundle so the shim can load node built-ins. Verified: bili start --debug
now boots and prints the MITM banner.
Clients that lock the model endpoint behind an OAuth/subscription login
(ZCode coding plan, Codex ChatGPT account, Claude Code subscription) cannot
use the existing /bili/<url> path-routing mode — their auth is bound to a
non-standard endpoint they won't let you reconfigure. MITM mode lets them
route through the proxy transparently: the client sets httpProxy (an HTTP
CONNECT proxy) + trusts our root CA; we TLS-terminate whitelisted model
hosts, inject compression into the plaintext, then forward to the real
upstream with the client's Authorization header (OAuth token) preserved
verbatim, so subscription billing/discounts stay intact.
Architecture: CONNECT handler TLS-terminates with a node-forge-signed host
cert and feeds the decrypted socket back into the same http server via
emit('connection', tlsSocket) — the existing handle()/forward() pipeline
runs unchanged, with a socket marker telling resolveUpstream() the real
upstream origin. Non-whitelisted hosts are blind-tunnelled (never decrypted).
node-forge is a devDependency bundled inline by tsup — dist stays zero
runtime deps.
release v0.1.24
refactor: URL-keyed config (delete named routing)
Mirror the ZH README's flattened Quickstart: three Options (A zero-config /
B manual config file / C web UI) replace the old Option A + Option B with
Step 1/2/3 split. Drops the now-redundant standalone 'How routing works',
'Web UI', and 'Manual config file' subsections and the two screenshots —
that content lives in Option A / Configuration now. Fix the dead anchor
left behind (#how-routing-works / #路由怎么工作) in both langs to point at
Option A.
Add instructions for manual configuration and web UI settings.
Root used to return the same JSON health check as /__bili/health
(redundant). Now content-negotiates on Accept: text/html browsers get a
302 redirect to /__bili/ (so http://localhost:8787 lands in the UI),
while curl and health probes (Accept: */*) still get the JSON {ok:true}
response, keeping scripts and Docker-style probes working.
The provider URL was a single-line <input>, so long upstream URLs (e.g.
https://open.bigmodel.cn/api/anthropic) were truncated to their prefix.
Switch to a <textarea> that autosizes to content height, so the full URL
wraps and stays visible while remaining editable. Oninput triggers
autosize so edits reflow immediately.
All control endpoints (web UI, health, stats, config, reload) move from
/__acp/* to __bili/* to match the billion-context ("bili") brand. No
conflict with the zero-config /bili/<url> route (that matches
/bili/http:// or /bili/https:// only). README.md + README.zh-CN.md URLs
updated.
The Providers tab only showed the editable upstream URL input, not the
full client-facing base URL. Render a read-only snippet
(http://<host>:<port>/bili/<full-upstream-url>) with a Copy button under
each provider, so users can paste it straight into Pi / OpenCode / Codex /
Claude Code config. snippet() now hides its label div when the label is
empty (callers provide surrounding context).
- Providers tab now shows client-setup snippets (Pi/OpenCode/Codex/full
path) inline under each provider card, so one page has everything.
- Remove the separate Client Setup tab + renderSetup/dropdown/snippets.
- client-setup CSS block (dashed top border separates it from models).
- handleConfigPut: write normalized routes (not raw body) + 'provider key'
wording; dropProvider name/url mix-up fixed (key is the URL).
Routing is now a single mode: the /bili/ prefix. The client embeds the
full upstream URL after /bili/, and the proxy forwards to it verbatim.
The config file's providers block is now keyed by upstream URL (the same
string the client puts after /bili/) instead of by provider name. This
makes the config match what the user actually writes in the client config,
and removes a whole routing code path (named segment matching + reserved
words + longest-segment sort).
Changes:
- config.ts: ProviderRoute drops the url field (key IS the url).
resolveContextLimit(routes, upstreamUrl, model) does longest-prefix
matching on URL keys (a key matches when upstreamUrl === key or
startsWith(key + "/"); longest wins; never crosses hosts).
parseRouteEntry accepts {models} or null; rejects strings/bare urls.
- server.ts: resolveUpstream deletes the entire named-routing branch.
Only /bili/ detection remains. Context resolution uses the embedded
rewrittenUrl. Banner shows override count instead of name=url list.
Forward log simplified (single routing mode).
- web.ts: provider editor is URL+models only (no name field). Client
setup snippets build /bili/<full-url>. Save writes {models} form.
- README.md + README.zh-CN.md: Option B rewritten as "explicit context
overrides"; all examples use /bili/<url>; Manual config + Configuration
sections show new URL-keyed schema and matching rules.
- tests: routing.test.ts and zero-config-routing.test.ts rewritten for
the new schema. Fixed a pre-existing type error in
proxy-responses-review.test.ts (makeCtx was missing the session field
required by CompressLoopResponsesCtx).
refactor: zero-config prefix /p/ → /bili/ + README fixes
- Web UI: 'require restart' → Apply hot-reload (PR#52 shipped __acp/config/reload)
- Status: 141 → 146 passing tests
- Option B heading: was empty, now ties Step 1/2/3 to the named-provider path
- Other clients: stop contradicting zero-config; cross-reference instead
- Zero-config intro: note /bili/ doubles as client-extension self-detection signal
The /bili/ prefix doubles as a brand + self-detection signal:
client-side billion-context extensions (billion-context-pi /
opencode-acp) can check their own baseUrl for /bili/ and self-disable,
avoiding double compression when a proxy is already in the loop.
- resolveUpstream: detect /bili/<full-url>, provider tag 'bili'
- banner + README (EN + zh-CN): /p/ → /bili/ everywhere
- tests updated, all 146 pass
- no compat shim (pre-release, pure rename)
feat: zero-config routing (/p/ prefix) + auto-fetch context from models.dev
- resolveUpstream: detect /p/<full-url> prefix → strip and use verbatim
upstream. Lets users route without a config file at all — they prefix
the client baseURL with the proxy origin + /p/.
- registry.ts: fetch models.dev/models.json (301 providers), disk cache
+ 24h TTL + offline fallback. contextFromRegistry resolves per-model
context for zero-config routes that have no per-model config.
- server.ts: zero-config routes (provider 'p') look up context via
registry as a middle layer before the prefix table / default. Preload
registry on startup. Banner shows the /p/ usage hint.
- forward log: drop noisy raw req.url, show the final proxied URL as the
primary signal. Omit provider tag for zero-config (no meaningful name).
- README (EN + zh-CN): Quickstart now offers two paths — zero-config
(/p/ prefix, no config file) and named providers (config file).
## Zero-config routing (Issue #50)
Users no longer need a config file to route. Prefixing the client's
baseURL with the proxy origin + /p/ embeds the full upstream URL in
the path:
client baseURL: http://127.0.0.1:8787/p/https://api.openai.com/v1
resolveUpstream detects /p/http(s):// at the path start, strips the
prefix, and uses the embedded URL verbatim as the upstream. Returns
provider="p" so stats/labels can tell zero-config requests apart.
The /p/ prefix keeps zero-config requests physically separate from
control endpoints (/__acp/*) and named-config routes (/<name>/*) —
no collision possible.
Three modes now coexist:
- /p/https://... → zero-config (new)
- /zhipu/... → named provider (existing, unchanged)
- /__acp/... → control endpoints (existing)
## Auto-fetch context from models.dev (Issue #55)
models.dev/models.json is a 254KB registry (301 providers, every model
with limit.context) that opencode itself uses as its model registry.
We fetch + cache it (24h TTL, disk cache for offline) as the context-
window source for zero-config routes that have no per-model config.
src/registry.ts: loadRegistry() (fetch + disk cache + TTL), contextFromRegistry(model, host).
- providerFromHost maps known hosts → provider ids
(api.openai.com→openai, open.bigmodel.cn→zhipuai, etc.)
- lookup tries 'provider/model' then bare 'model'
Fallback chain: per-model config → registry (zero-config only) → prefix table → default.
Registry is preloaded in the background at startup (non-blocking).
Misses fall back gracefully; offline-first via disk cache.
## Verification
- typecheck ✅ · 146 test ✅ (141 + 5 new zero-config tests)
- registry lookup verified: gpt-5.1-codex=400K, glm-5.2=1M, claude-opus-4-0=200K
- zero-config /p/ routing: 5 tests cover strip, precedence, malformed, coexistence
feat: drop output (max_tokens) from config — no longer needed
Research (del_mskek600_3u6b) confirmed:
- The proxy NEVER reads the config `output` field — it forwards whatever
max_tokens the client sends, and omits it otherwise. So the field was a
dead config value that only added friction.
- All major APIs treat max_tokens as optional with a sensible default
(OpenAI/Responses/zhipu/DeepSeek). Anthropic requires it but its clients
always send it, so Anthropic routes are covered automatically.
- codex doesn't configure it at all; opencode defaults to 32000; pi to 16384.
Changes:
- Web UI: removed the output input row from the model editor; addModel no
longer seeds a default output; entries_models / saveProviders drop it.
- README (EN+ZH): all examples now show { context: N } without output;
Configuration section explains output is no longer needed (accepted for
backward compat, has no effect).
- ProviderRoute type: output kept as optional for backward compat (old
config files with output still parse, the value is just ignored).
context window is unchanged — still needed for compression timing.
feat: hot-reload routes via web UI Apply button
The web UI's Save wrote the config file but required a manual bili
restart to take effect. Users (especially on Windows, running bili
without an interactive terminal) hit this: they'd save config in the
browser, keep using the proxy, and all requests still went to the old
upstream because the running process never re-read the file.
## Changes
- config.ts: export loadRoutes() — re-reads routes from the config
file (same precedence as loadOptions: ACP_PROVIDERS path > inline
providers). Returns a fresh ProviderRoutes.
- server.ts: POST /__acp/config/reload — calls loadRoutes(), then
clears+refills opts.routes IN PLACE so all in-flight handle()
closures (which captured opts by ref) see the new routes. Only
routes reload — port/host/upstream can't change on a running server.
- web.ts: 'Apply' button next to 'Save'. Save writes the file (as
before); Apply hot-loads routes into the running process. Apply is
blocked if there are unsaved edits (prompts 'Save your changes first').
## UX
- Save → 'Saved N providers — click Apply to activate'
- Apply → 'Applied — N providers active (no restart needed)'
- Dirty notice: 'click Save then click Apply to activate without restart'
Verified: PUT writes config, POST reload updates in-memory routes
immediately, subsequent request routes to the new upstream without restart.
release v0.1.23
docs: sync README screenshots + clickable links (EN ↔ ZH)
EN Quickstart Step 1/2 now matches ZH: includes the web UI screenshots
(GitHub user-attachments) and makes http://localhost:8787/__acp/ a
clickable link. ZH Step 2 text rounded out from bare 'copy the below'
to a proper sentence. Both languages now visually consistent.
fix(windows): cross-platform auto-update
release v0.1.22
docs(agents): no-op release required before shipping auto-update changes
The auto-update code (src/update.ts) is load-bearing for every future
upgrade. A broken release bricks the upgrade path for anyone who hits
it — they can never auto-update again. This documents the safety rule:
any change to update.ts MUST be preceded by a no-op release that
validates the existing upgrade path is healthy end-to-end, using the
old (trusted) code, before the new code ships.
Includes the v0.1.22 concrete example (Windows fix staged in PR#44,
no-op validation release in PR#46 first).
Windows has no `tar` or `cp` in PATH by default, so auto-update
(extract npm tarball + copy over install dir) failed silently on
Windows, leaving users stuck on their installed version forever.
Replaced both external commands with cross-platform Node APIs:
- tar extraction: `tar` npm package (pure JS, same lib npm CLI uses)
via `tar.x({ file, cwd, strip: 1 })` — replaces `execFile("tar", ...)`
- dir copy: Node built-in `fs/promises` `cp()` with recursive+force —
replaces `execFile("cp", ...)`
`tar` is a build-time dep (devDependencies) — tsup bundles it into
dist at build time, so the runtime footprint (what users install) is
unchanged: still only acp-kernel as a runtime dependency.
Verified: real npm tarball extracts correctly (package.json version
matches), and fs.cp merges contents into target dir without clobbering
existing files (equivalent to `cp -r src/. dst/`).
release v0.1.21
feat: web config UI
- ZH: Step 2 points to web UI; deleted manual JSON content restored as
a separate "手动配置文件" section after the client-setup steps
- EN: Step 2 now recommends the web UI; manual JSON moved to a new
"Manual config file" section (same content, just relocated)
- Both: web UI is the primary path, hand-editing JSON is the fallback
for git-managed / scripted deployments
Remove outdated instructions and examples for configuration.
The web UI / stats label now reflects what the client actually sent
(opencode x-session-affinity, codex body.session_id). pi sends nothing,
so its label is empty (shown as "—") instead of a fake ses_<hash>.
The synthetic ses_<conversation> is still forwarded upstream as the
affinity header for sticky-routing — that behavior is unchanged.
- Web UI: left logo is now a link to the GitHub repo; added a GitHub
button (with octocat icon) top-right. Both open in a new tab.
- Server startup banner now prints the web UI URL:
acp-proxy listening on http://localhost:8787 — web UI: http://localhost:8787/__acp/
(also fixes 0.0.0.0 → localhost in the display)
- README (EN + ZH): Step 1 points users to the web UI as the easier
config path, with the banner shown
- Session gains: label (affinity token), inputTokens, cachedTokens,
outputTokens, cacheSamples, contextTokens (all persisted)
- server.ts: pass label into getSession, record contextTokens at each
prepare* (anthropic/openai/responses)
- stream.ts (anthropic): capture upstream usage from message_start
(input/cache_creation/cache_read) + message_delta (output) on the
non-converted path
- compress-loop.ts + compress-loop-responses.ts: record usage into session
alongside the existing [acp-usage] log line
- sendStats: return protocol/label/contextTokens/inputTokens/cachedTokens/
outputTokens/cacheSamples/cacheHitPct; drop the unused tokensSaved display
- web.ts Sessions tab: new columns (Protocol, Label, Requests, Context,
Cache hit, Input, Output, Last seen) with fmtTok helper (1.2K / 3.4M)
- persist.ts: PersistedSession + buildRecord/buildSession round-trip all new
fields (optional on read for backward compat with old session files)
- GET /__acp/ serves a single-page UI (dark theme, vanilla JS, zero deps)
- GET /__acp/config returns current providers + file path
- PUT /__acp/config validates and writes providers (merges into file,
preserves port/host/debug; never clobbers other fields)
- Providers tab: add/remove providers + per-model context/output,
inline editing, dirty-state indicator, save with restart notice
- Client Setup tab: dropdown of providers → copy-ready baseUrl/baseURL/
base_url snippets for Pi / OpenCode / Codex (full upstream path
reconstructed from provider URL tail)
- Sessions tab: auto-refreshing table from /__acp/stats (requests,
tokens saved, last seen)
- Export safeReadJson + parseRouteEntry from config.ts for web.ts reuse
- README: document Web UI section (EN + ZH)
release v0.1.20
docs: stepped Quickstart (start → configure → point agent → verify)
The template used to ship with a placeholder provider
(your-provider.example.com) which is neither a real default nor truly empty
— confusing. Make it honestly empty: { "providers": {} }. The log line
points users to the README Quickstart to fill it in. Proxy still runs
(falls back to the default upstream when no routes are declared).
- Pi/OpenCode/Codex: show before→after of the single baseUrl/baseURL/base_url
line the user already has, instead of re-declaring a whole provider
- Fix ZH full-width comma (,→,) in the config JSON (parse-breaking)
- Drop the config-name table (redundant with the Step 1 preview now)
- Move zhipu ahead of anthropic in the config example (it's the worked example)
- Sync EN to match ZH structure
Updated configuration instructions for providers and API keys.
release v0.1.19
fix: replace npm install -g with tarball-based auto-update
- Pi/OpenCode/Codex: show the actual config file each edits (models.json,
opencode.json, config.toml) instead of ANTHROPIC_BASE_URL env exports
- Remove Claude Code section (not tested)
- Fix: Pi is not inherently an Anthropic client — show openai-completions
(GLM) as the example, note anthropic-messages as an alternative
- Remove bili-comfly / relay name leak — keep generic <provider> placeholders
Code:
- config.ts: ensureConfigTemplate() writes a template providers file
(anthropic + zhipu examples) on first run, idempotent (never overwrites).
- cli.ts: call ensureConfigTemplate() before loadOptions() in main().
Docs (README + README.zh-CN):
- Drop the 'default to Anthropic / Path A vs Path B' duality that caused 404s
when users pointed clients at /anthropic without a config file.
- New 3-step flow everyone follows: start -> edit config -> point client.
- Step 3 splits per client: Pi / OpenCode / Codex / Claude Code / others.
- Routing section: note the no-providers edge case instead of a 'Path A'.
release v0.1.18
ci: enforce version-only-on-release-branches
Adds two layers of protection so the 'version' field can never be casually
bumped:
1. AGENTS.md §4 — new 'Version Bumps' subsection (sibling to the npm Publish
prohibition): two hard rules — version changes ONLY on *_release-v*
branches, and a release commit changes ONLY version. Includes the reason
(CI trigger detection + merge-conflict avoidance) and a canned reply for
when a human asks to bundle a version bump into content.
2. ci.yml — new 'version-guard' job that runs on every PR: if the branch is
NOT *_release-v* and the diff touches the '"version"' line in
package.json, the job fails with a ::error annotation. Release branches
skip the check.
This prevents the exact failure that hit PR#32 (version bundled into a
non-release context → three-way merge conflict + broken CI trigger).
- Download tarball from registry, extract to staging dir, verify, then
copy over install dir (no longer depends on npm install -g)
- Add cross-process lock (tryAcquireLock) to prevent concurrent updates
- Remove notified Set (Bug A): failed installs now retry next cycle
- Read version from disk on every check (readDiskVersion) instead of
startup constant, so in-place updates are detected without restart
- Return specific error messages on each failure step
- Supports any install method (npm, homebrew, manual), not just npm -g
docs: fix AGENTS.md to match actual CI publish workflow
- §4 Git Safety: npm publish is now CI-driven (release.yml), not manual
- §4 npm Publish Prohibition: reflect CI auto-publish; manual is fallback only
- §4 Local Install: npm install -g . does NOT create a symlink here (has bin+files)
- §5 Release Workflow: rewritten to show the real CI-driven flow (Agent prepares
PR, human merges, CI publishes+tags+creates release). Removes obsolete manual
'npm publish' step.
- §5 Cross-repo: clarified this is about bumping acp-kernel dep version
- §5 Auto-update testing: uses merge-PR flow instead of manual npm publish
- §3 Local Testing: note npm install -g . is acceptable
release v0.1.17
docs: add AGENTS.md development specification
Adapted from billion-context-pi AGENTS.md. Key additions specific to
billion-context:
- npm publish is human-only (NEVER NPM_ALLOW_DANGEROUS=1)
- Never npm install -g . (creates symlink), use registry or tarball
- All proxy logs must go through tee logger, not console.error
- Auto-update: 3min interval, startup check bypasses throttle
- Release workflow: Agent does 1-5, human does merge + publish
release v0.1.15
fix: route all proxy logs through tee logger (file + stderr)
Multiple modules bypassed the tee logger (src/logger.ts) by writing
directly to console.error/stderr. This caused the file log
(~/.local/state/billion-context/bili.log) to be missing entries that
appeared in the terminal — making file logs unreliable for debugging.
Fixes:
- persist.ts: getStore() now passes tee logger to SessionStore instead
of falling back to defaultLogger (console.error). defaultLogger
itself now routes through loggerLog() as a safety net.
- compress-loop-responses.ts: 3 console.error calls → loggerLog
([acp-diag] → debug, [acp-compress-args] ×2)
- compress-tool.ts: 3 console.error calls → loggerLog (warn level,
[acp-compress-input])
- config.ts: console.error → loggerLog (error level, [acp-config])
cli.ts (user-facing CLI errors) and index.ts (pre-logger startup crash)
intentionally keep console.error — they run before logger init or need
to write to stdout/stderr directly.
release v0.1.13
release v0.1.12
fix: retry auto-update on transient install failure
The notified.add(latest) was executed unconditionally, before checking
whether installLatest() actually succeeded. A single transient failure
(network hiccup, EACCES, registry 5xx) caused the version to be marked
as 'notified' anyway. Every subsequent 3-min cycle then hit the
'already installed this run' guard and skipped — so one transient error
permanently disabled auto-update for the entire process lifetime.
Fix: move notified.add(latest) inside the success branch. On failure,
do not add to notified so the next throttle cycle retries naturally.
Also fix stale comment in cli.ts:164 ('6h throttle' → 'the throttle';
CHECK_INTERVAL_MS is 3 min, not 6h) and bump version 0.1.10 → 0.1.11.
release v0.1.10
fix: make update throttle logs unambiguous
A freshly started proxy was inheriting a stale throttle timestamp from the
previous run, delaying the first real update check by up to the full interval
(180s). The user saw only 'check skipped (throttled)' lines for minutes on
startup, making auto-update look broken.
Now the first checkForUpdate() in each process always runs a real registry
fetch (marked '(startup check)' in the log); subsequent calls honor the
shared cross-process throttle. The 'first check done' marker lives in the
existing notified Set (as a sentinel key), so it resets on restart — which
is exactly when a bumped version takes effect anyway.
Verified: with a stale throttle file (1h old), startup → 10s later →
'checking npm registry for billion-context (startup check)…' immediately,
no throttle skip.
release v0.1.9
The old line 'check skipped (throttled, Ns until next)' only showed the
countdown, which looked wrong when it grew between consecutive skips (a real
check had reset the timestamp in between, but we only logged the skips).
Now log both axes explicitly:
'throttled — last checked 42s ago, retry in 138s'
and the real-check line records when the last check happened:
'checking npm registry … (last check 180s ago)'
Monotonic 'last checked Ns ago' can't look backwards; 'retry in Ms' is the
true countdown.
fix: logger rotation, SSE drain on shutdown, host docs
m3 (all platforms): logger only checked size at startup; a long-running proxy's
log grew without bound. Track bytesWritten and rotate at runtime when the
threshold is crossed (reopen the file). Rename failure (Windows .old held
open) is best-effort and retries next rotation.
m5 (all platforms): shutdown called server.close() fire-and-forget then
immediately process.exit(0) — yanking in-flight SSE streams mid-chunk. Now
server.close(cb) waits for keep-alive connections to drain before flushing
sessions and exiting; a 10s unref'd timeout forces exit if a client never
closes the connection.
M3 (docs, all platforms): document the host default choice. Default stays
127.0.0.1 (loopback, no auth) for safety. Added guidance for IPv6 (::) and
container (0.0.0.0) cases with a security warning, in both README.md and
README.zh-CN.md. No code change to the default.
release v0.1.8
fix: cross-platform robustness (port crash, Windows shutdown, persist, BOM, /tmp)
C1 (all platforms): server.listen() had no 'error' listener, so EADDRINUSE
(port taken) / EACCES (privileged port) threw an unhandled 'error' event and
aborted the process before graceful-shutdown could flush sessions. User hit
this as 'bili start --port 44445' crashing with a Node.js stack. Now caught,
logged with a human hint ('port N is already in use'), sessions flushed, and
exit(1).
M1 (Windows): SIGTERM is never delivered on Windows (Node can listen but the
kernel won't raise it), so taskkill / service managers / NSSM stopped the
proxy without triggering flushAllSessions — losing up to debounce-ms of
session state. Add SIGBREAK handler (raised by Ctrl+Break and most Windows
process terminators) to the same graceful-shutdown path.
M2 (Windows): persist.ts writeNow did writeFile+rename without a try/catch.
On Windows, fs.rename (MoveFileEx) throws EPERM/EBUSY when the destination
is held open by AV scan / search indexer / concurrent flushSync / SMB share.
The throw propagated, left an orphan .tmp-* file, and lost the write. Wrap
it: on failure, unlink the .tmp file and surface the error.
m1 (Windows): server.ts debug dump hardcoded '/tmp/...'. Windows has no
/tmp, so debug dumps silently failed (ENOENT swallowed by try/catch). Use
os.tmpdir().
m2 (all platforms): config.ts safeReadJson did not strip a UTF-8 BOM. Windows
Notepad saves UTF-8 'with BOM'; JSON.parse('\uFEFF...') throws SyntaxError,
silently dropping the whole config file. Strip leading BOM before parse.
m4 (all platforms): no global uncaughtException/unhandledRejection handlers.
Background work (compress loops, auto-update, initSessions) escaping the
per-request try/catch would abort the process (Node 20+ default). Add
top-level handlers that log loudly instead of crashing.
release v0.1.7
fix: route auto-update logs through the tee logger
Previously checkForUpdate only emitted to stderr via console.error, and only
on the success/failure path — the file log showed nothing about update
activity, and the periodic check was a silent black box. Now the full cycle
is observable in the log:
- startup: 'auto-update enabled (checking every 180s)'
- throttle skip: 'check skipped (throttled, Ns until next)'
- registry fetch: 'checking npm registry for billion-context…'
- no update: 'current=0.1.6 latest=0.1.6 (up to date)'
- new version: 'new version found: 0.1.6 → 0.1.7, installing…'
- installed: 'installed 0.1.6 → 0.1.7. Restart to finish.'
- already-notified: 'latest=0.1.7 already installed this run, awaiting restart'
- failures: registry error / install failure with reason
All go through logger.log() so they land in the file too, matching the
rest of the proxy's observability story.
release v0.1.6
fix: suppress duplicate auto-update notices within one process
release v0.1.5
docs: env vars primary config method + cleanup
- Configuration intro now leads with 'env vars (recommended) OR JSON file'
- Environment variables section moved before Config file
- Removed 4 stale condense env vars (feature deleted in v0.1.4)
- Added ACP_AUTO_UPDATE + ACP_LOG_FILE (missing from table)
- Fixed priority order: CLI flag > env var > config file > default
- Move 'Session identity' out of Configuration → new '## How sessions work'
(it describes proxy behavior, not configuration)
- Add README.zh-CN.md (Chinese translation) with cross-language toggle
links at the top of both files, mirroring billion-context-pi's pattern
release v0.1.4
fix: cache health — remove condense, fix isTitleGen, preserve cache_control
The OpenAI/chat path keyed isTitleGen on BOTH (maxTokens<=200) AND
(messages.length<=2). The second condition meant turn 1 of ANY conversation
(max_tokens=8192, 2 messages) had shouldInject=false → no compress prompt in
system, no compress tool. Turn 2+ → shouldInject=true → system bytes change.
This rewrite of the system block between turn 1 and turn 2+ breaks the provider
prefix cache for everything after the system prompt (i.e. the entire
conversation) on every conversation.
Fix: key isTitleGen ONLY on maxTokens<=200 (a real title-gen signal). Message
count was a false proxy for 'this is a throwaway request'. The Anthropic and
Responses paths never had this bug (they always inject, so their system is
stable from turn 1).
condenseOldToolResults was a proxy-layer mechanical condenser (stub large old
tool results to a 400-char head) introduced in v0.1.0. It runs EVERY turn and
rewrites stable history (full→stub as a sliding window moves), which breaks
provider prefix caching for everything after the shifted message.
With ACP's model-driven compress tool now handling context management, condense
is both redundant and harmful (the two are fundamentally opposed: condense
mutates stable history each turn; prefix caching requires byte-stable history).
Removed across 8 source files + 7 test files:
- anthropic.ts: condenseOldToolResults, CondenseOptions, CondenseResult,
CONDENSED_TAG
- openai.ts / responses.ts: import + re-export
- server.ts: applyCondense fn, 3 call sites (→ turn.messages), stats field
- config.ts: condense ProxyOptions/FileConfig block + 4 ACP_* env parses
- session.ts: condensedToolResults field
- persist.ts: field (deserialize still loads old files — extra field ignored)
- cli.ts / README.md: references
Tests: 145 → 139 (removed 6 condense-specific tests across 2 files).
- New src/logger.ts: writes every log line to BOTH a file (default
~/.local/state/billion-context/bili.log, XDG state dir) and stderr, so logs
persist even for backgrounded/no-tty runs. Auto-rotates at 10MB.
- ACP_LOG_FILE env / logFile config override the path; 'off' disables file.
- [acp-usage] cache-hit logging added to compress-loop.ts (OpenAI chat path)
so prefix-cache health is measurable on the pi/glm route (previously only
the Responses/Codex path logged cache stats).
feat: self-update (auto + manual)
release v0.1.3
release v0.1.2
feat: release CI (npm auto-publish on release branch)
feat: bili CLI + debug in config + README rewrite
- Add src/cli.ts: bili command with subcommands (start default), flags
(--port/--host/--config/--debug/--passthrough) that override the config
file and env. bili --help / --version supported.
- package.json: add 'bili' bin (keep 'bili-proxy' as alias).
- paths.ts: configFile() now respects BILI_CONFIG_FILE env (--config flag).
- server.ts: fix startup banner printing [object Object] for {url,models}
routes — now shows the real URL.
- README Usage rewritten: bili start is the primary entry, debug documented
as flag/env/config (three ways), routing examples updated.
fix: post-merge review fixes + per-route context window + XDG config
Two bugs from the latest proxy review:
1. readBody() called req.destroy() after rejecting — the client connection
was cut off with no HTTP response, leaving handle() unable to write 413.
Fix: reject with a typed BodyTooLargeError WITHOUT destroying; handle()
catches it and writes a clean 413 JSON response.
2. TEXT_PROTOCOL suppressed ALL output_text.delta/done and forged a
response.completed with output:[] even when NO compress trigger was
present, corrupting normal Responses output. Fix: only rebuild when a
trigger was actually caught; otherwise pass every event through verbatim.
Added 4 regression tests (3 readBody + 1 responses no-trigger passthrough).
Documents:
- XDG config file location (~/.config/billion-context/billion-context.json)
- Two provider forms: bare URL (simple) vs {url, models} (full, recommended)
- Per-model context/output declaration + why LLM /models API doesn't help
- All top-level config keys + all ACP_*/BILI_* env-var overrides
- Routing examples for Claude Code, Codex, zhipu, openai, deepseek
conversationSignalResponses now prefers body.session_id (Codex 0.147+
per-conversation UUID) over previous_response_id and content hashing.
Codex was sending this UUID but the proxy ignored it — a real bug.
For clients that send no session header at all (pi), forward() now
synthesizes x-session-id: ses_<conversation> so upstream sticky-routing
gets a stable key. Only injected when the client sent no session header
(does not shadow opencode's x-session-affinity or codex's body.session_id).
Documented per-client session-identity story in README. pi is flagged as
collision-risk for many concurrent conversations; Codex/OpenCode are safe.
Concurrent requests sharing a session id could interleave processTurn /
stream-rewriter mutations on session.state, corrupting it. Add
withSessionLock(session, fn) — a per-session promise chain that serializes
the prepare → forward critical section.
Also parse the request body once in handle() and pass the parsed object to
prepare*, eliminating duplicate JSON.parse on large payloads (review #3).
Move from bespoke ~/.bili/ to the XDG Base Directory Specification:
config: ~/.config/billion-context/billion-context.json
(/billion-context/billion-context.json)
data: ~/.local/share/billion-context/sessions/
(/billion-context/sessions/)
New config file (billion-context.json) consolidates ALL settings that
were previously scattered across ~15 ACP_* env vars. Structure mirrors
bili-pi's ~/.pi/acp.json + opencode's config.json:
{
"providers": { "zhipu": { "url": "...", "models": {...} } },
"port": 8787, "host": "127.0.0.1", "upstream": "...",
"modelContextLimit": 200000,
"condense": { "enabled": true, "keepRecentToolResults": 6, ... },
"compress": { "injectTool": true, "injectNudge": true },
"sessionHeader": "x-acp-session",
"log": true, "debug": false, "passthrough": false
}
Priority (high → low): env var > config file > built-in default.
Env vars still work (backward compat) — they override the file.
New src/paths.ts centralizes all XDG path resolution. providers.json
(legacy) still readable via providersPath in the config file.
LLM APIs do not return context windows; providers.models in the config
file is the source of truth (same approach as opencode/pi registries).
LLM APIs (/v1/models) do NOT return context windows — verified across
OpenAI/Anthropic/zhipu/comfly. The proxy was guessing from a hardcoded
table that was wrong for GLM (128K instead of 1M), causing spurious
frequent compression.
New providers.json format mirrors opencode's model registry:
{ "zhipu": { "url": "https://...", "models": {
"glm-5.2": { "context": 1000000, "output": 131072 }
}}}
Backward compatible: bare strings still accepted as { url }.
Resolution priority: per-route per-model → built-in table → env default.
The built-in table is now a FALLBACK only (users override via providers.json).
Added glm-5* → 1M to the table as fallback.
server.ts: resolveUpstream now uses routes[name].url. Per-request limit
resolution moved AFTER routing so the route's model map is consulted first.
resolveContextLimit(routes, provider, model) replaces lookupContextLimit
in the hot path (lookupContextLimit still exported for the table-only tests).
Review #2 findings (more comprehensive than #1):
#2 (compress-loop*.ts): fetch-timeout timer leak across compress-loop
rounds. activeClearTimer = clearTimer overwrote the previous round's
timer without clearing it, so rounds 1..N-1 each leaked a 10-min timer
that self-fired harmlessly but accumulated handles in the event loop
over a long session. Clear the previous timer before overwriting
(3 lines, both openai and responses variants).
#5 (orphan-gc.ts): orphanStreak was monkey-patched onto the kernel's
Block type via (block as unknown as {orphanStreak?:number}).orphanStreak
— fragile, no type safety, breaks if the kernel Block shape changes.
Replaced with a module-level WeakMap<object, number> keyed by the block
object. TS now type-checks the access; counts are GC'd when a block is
dropped.
#7 (README.md): stale test count (79 → 136) and missing env-var docs
(BILI_PERSIST, BILI_PERSIST_DEBOUNCE_MS, MAX_SESSIONS,
BILI_SESSIONS_DIR). Both fixed.
#8 (tests/*.test.ts): 7 type errors under tsc --noEmit (role: string
not narrowing to the union literal; Session literals missing new fields
after inFlight/persisted/blockContents were added). Fixed with 'as
const' on roles and the missing fields. Full tsc --noEmit now 0 errors.
#10 (persist.ts): comment referenced AGENTS.md which doesn't exist;
replaced with an inline note on the known concurrency limitation.
Review findings from a proxy-driven code review:
P0-1 (server.ts): forward() called res.writeHead() unconditionally, then
the !upstream.ok branch called it again → ERR_HTTP_HEADERS_SENT when the
upstream returned a non-2xx WITH a body and the rewriter was armed. Move
the error-branch writeHead to be the ONLY writeHead on that path; the 2xx
path writes headers after the error check.
P0-2 (session-id.ts): deriveSessionId took a firstUserContent param that
every caller passed as "" — a silent default that would collapse every
anonymous session onto hash("") if the clientConversation fallback ever
disappeared. Drop the misleading param; conversation is now a REQUIRED arg
(the per-protocol conversationSignal* helpers already compute it, with
their own first-user fallback). An empty conversation now throws loudly.
P1-1 (persist.ts): require("node:fs").unlinkSync in an ESM module —
require is undefined in ESM, so the orphan-temp cleanup was a no-op
(ReferenceError swallowed by the catch), leaking temp files. Import
unlinkSync at the top and call it directly.
P2-1 (server.ts): log a warn when a request path matches no known protocol
and is forwarded unchanged (helps spot misconfigured base URLs).
P1-2 (no per-request serialization) is the known limitation documented in
persist.ts — tracked for a separate PR.
P2-2 (CI typecheck failure) does not reproduce locally; typecheck passes.
feat: persistent + namespaced sessions, multi-tenant session isolation, stream robustness
Session files were a flat directory of opaque hashes — a human could not
tell which client/protocol/provider a session belonged to. Files are now
organized by protocol subdirectory with a provider-host prefix:
~/.bili/sessions/
├── anthropic/coding.dashscope.aliyuncs.com_<hash>.json
├── openai/open.bigmodel.cn_<hash>.json
└── responses/ai.comfly.org_<hash>.json
Changes:
- Session gains optional protocol/upstreamOrigin fields, captured at first
request (getSession accepts a meta arg) and persisted in the JSON body.
- persist.ts: relPathFor() builds <protocol>/<host>_<hash>.json; loadAll()
walks subdirs recursively; loadSync() tries the namespaced path then a
_unknown/ legacy fallback; mkdir creates the leaf subdir.
- hostLabel() uses the full hostname (unambiguous, grep-able) rather than
guessing the registrable domain (which mislabeled open.bigmodel.cn as
'open' instead of 'bigmodel').
- Legacy flat files (no protocol meta) still load via loadAll, and a
protocol-less session persists under _unknown/ so it never collides with
a protocol subdir.
Tests: 134 (was 131) — +3 for namespacing (protocol/provider layout,
protocol-less _unknown compat, loadSync meta-based lookup).
Previously deriveSessionId keyed on the conversation dimension alone (a
client header or a hash of the first user message). In a multi-tenant setup
(several clients, several accounts, several providers, several protocols)
this is unsafe:
- pi (acct A) and opencode (acct B) sending the same opening line would
collide on the same content fingerprint → their compression states
would bleed into each other.
- the same key hitting two providers (zhipu vs bailian) with the same
first message would also collide.
- an Anthropic-format and an OpenAI-format request carrying the same text
would collide even though they are different wire protocols.
The session is now keyed by FOUR dimensions, all AND-ed:
1. protocol — 'anthropic' | 'openai' | 'responses'
2. upstream — the resolved upstream ORIGIN (provider host), not the
full rewritten URL (so a path change doesn't fork state)
3. apiKey — normalized Authorization / x-api-key credential
4. conversation — client-provided signal when available
(x-session-affinity / x-acp-session / x-session-id /
x-opencode-session for OpenCode; previous_response_id
for Responses' native linkage), falling back to a
content fingerprint of the first user message
src/session-id.ts (new) centralizes this. The per-protocol derive functions
are renamed conversationSignal{Anthropic,Openai,Responses} — they now
contribute ONLY the conversation dimension; the other three are mixed in by
the caller. server.ts resolveUpstream is hoisted before prepare() so the
session can embed the provider origin, and route is threaded into forward().
affinityToken() (new) produces the conversation dimension ALONE, formatted
as OpenCode's 'ses_<hash>' x-session-affinity value. It is suitable for
forwarding to an upstream that understands that convention (e.g. a
multi-account load-balancer doing sticky routing), and contains NO key or
upstream so it is safe to send upstream and stable across account rotations.
Not wired into forwarding yet — only emitted when needed (future multi-acct
routing). Single-account direct upstreams already get automatic prefix
cache by content, so no header injection is needed there.
Tests: 131 (was 119) — +12 for session-id (4-dimension isolation,
client-signal priority, affinity-token format and key-independence, etc.).
P2.4: safeJsonParse was duplicated verbatim in stream-openai.ts and
stream-responses.ts (same 5 lines). Hoisted to util.ts as a single export;
both files now import it. No behavior change.
P2.5: compress-loop.ts and compress-loop-responses.ts imported
writeFileSync/mkdirSync/tmpdir/path.{dirname,join} but never called any of
them — leftover from removed debug-snapshot code. Removed the dead imports
(the .join() calls that remain are Array.prototype.join, not path.join).
P2.3 (merge the three SSE frame parsers) deliberately NOT done: the three
rewriters were just reviewed and stabilized (4 bug fixes last round); a
structural refactor to dedupe ~45 lines risks regressions for little gain.
Re-extract when a bug next shows up that affects all three paths at once —
that's when the shared boundary will be clear. sse-util.ts already holds
the genuinely-shared normalizeSseLineEndings from the previous CRLF round.
Addresses P1 findings from researcher review (del_msjs98bm_3r0t).
P1.2 — upstream error body swallowed by rewriter:
server.ts forward() routed compress-injected responses through the SSE
rewriter unconditionally. A non-2xx (auth, rate-limit, context_length_exceeded)
JSON error body has no SSE events, so the rewriter produced an empty stream
and the client lost the error detail. Now: if !upstream.ok, write the upstream
status + body through verbatim (same as the non-rewriter path) and skip the
rewriter entirely.
P1.1 — stream abort on rewriter throw:
The three streaming for-await loops in forward() had no try/catch. A throw
(decompress/search edge case, JSON.parse failure, fetch abort) propagated up
and skipped res.end(), leaving the client with a truncated stream and no
finish event. Now wrapped in try/catch; on throw, emitStreamError()
(src/stream-error.ts) emits a protocol-appropriate error delta + finish
sequence so the client ends cleanly:
- openai: error delta + finish_reason:stop + [DONE]
- anthropic: content_block_delta + message_stop
- responses: output_text.delta + response.completed
emitStreamError never throws (write/end failures swallowed) so it cannot
itself make things worse.
upstream-error detail in retry loop:
compressLoopStream / compressLoopResponsesStream included only the status code
in the error delta, not the upstream message. Now appends errText (truncated
to 200 chars) so the client sees WHY the retry failed.
activeClearTimer leak (OpenAI loop):
compressLoopStream assigned activeClearTimer but never called it on exit
(three return points + normal completion). The responses variant had a
finally guard; the OpenAI variant did not. Added a top-level try/finally
around the generator body that clears the timer on any exit, preventing
cross-round timer leaks and event-loop pinning.
Tests: 119 (was 114) — +5 for emitStreamError (openai/anthropic/responses
format, write-throws-safe, log callback).
Addresses CRITICAL/MAJOR findings from two independent reviewers on the
session-persistence PR.
C1 — silent write loss on eviction: flushSync swallowed all errors and
evictOldest deleted from memory unconditionally. A NEW session whose first
flush failed was permanently lost. flushSync now returns boolean; evictOldest
keeps a never-persisted session in memory when flush fails.
M1 — filename collision: safeFileName("a/b") === safeFileName("a_b"), so two
distinct sessions could clobber one file. Filenames are now sha256(id) hashes;
the real id lives in the body and is verified on load (loadSync rejects body id
mismatch; loadAll skips files whose name ≠ fileNameFor(bodyId)).
Me1 — temp file collision: .tmp-<id>-<pid> was not unique within a process,
so overlapping writes raced on rename. A monotonic counter now makes every
temp path unique.
M4 — blockContents cap defeated disk persistence: trimming the in-memory Map
then writing back persisted the REDUCED set, permanently losing originals.
Removed the per-session cap entirely (per user: disk is cheap; blockContents is
bounded by MAX_SESSIONS × session activity). persist.ts is now the source of
truth for full contents.
M5 — split-brain eviction: a session could be LRU-evicted mid-stream, flushing
a half-mutated snapshot then reloading a second object that raced writes to the
same file. Session now tracks inFlight; evictOldest skips in-flight sessions,
and the request path acquires/releases it around prepare→forward.
M7 — silent errors: all write paths (.catch(()=>{}), empty catch) now log.
m1/M6 — loadAll bypassed MAX_SESSIONS: initSessions now caps the loaded set to
the most recent MAX_SESSIONS so a large backlog cannot OOM on boot.
m3 — shutdown ordering: server.close() now runs BEFORE flushAll so a late
request cannot mutate state after its snapshot is taken.
KNOWN LIMITATION (documented): concurrent requests for the SAME session still
have no per-session serialization (pre-existing); persistence makes durable
state loss from a lost update more visible. A per-session mutex is a separate
PR. AGENTS.md notes this.
Tests: 15 (was 10) — +5 for flushSync bool return, loadSync body-id check,
loadAll filename/body mismatch, unique temp files, hasPending.
Disk persistence holds all sessions regardless of this cap — it only bounds
the in-memory originals cache for a single session. A typical large session
is ~100MB; 64MB caused silent decompress degradation on normal workloads.
Raised to 512MB so ordinary long sessions never hit it. Disk is cheap, and
hundreds of sessions at ~100MB each is still trivial storage.
PROBLEM: sessions lived only in process memory. A proxy restart dropped all
compression state. For sessions whose raw history had grown past the model's
context limit, the client re-sends full history on the next request — without
the saved block summaries there is nothing to fold it under, so the model
rejects the oversized request and the session hangs permanently. A restart
killed effectively every long-running session.
SOLUTION: disk-backed persistence, memory as a bounded cache.
src/persist.ts (SessionStore):
- One JSON file per session under ~/.bili/sessions/ (BILI_SESSIONS_DIR).
- Atomic write (temp file + rename) — survives a crash mid-write; a partial
temp is discarded on next load by the corrupt-file fallback.
- Debounced async writes (default 500ms, BILI_PERSIST_DEBOUNCE_MS): the hot
path never blocks on fs; bursts coalesce into one write.
- mergeInitialState forward-compat: fields missing on an old file get fresh
defaults, so a schema change never breaks existing files.
- loadAll() bulk-loads every session at startup (before listen).
- loadSync() reloads a single session on a memory miss (after LRU eviction).
- flushSync() writes immediately, used on memory eviction so a dirty evicted
session is not lost.
- flushAll() flushes pending writes, used on SIGTERM/SIGINT for graceful
shutdown.
- Path-traversal-safe filenames (sanitized); real id stored inside the file.
- BILI_PERSIST=0 disables persistence (for tests / ephemeral runs).
src/session.ts:
- getSession() miss now tries loadSync() from disk before creating fresh.
- evictOldest() flushSync()s the evicted session first → memory bounded by
MAX_SESSIONS (BILI_MAX_SESSIONS, default 256) while ALL sessions persist.
- cacheBlockContent() enforces a per-session byte cap
(BILI_BLOCK_CACHE_BYTES, default 64MB): oldest block originals are dropped
when exceeded (block summary stays active; decompress degrades to summary).
Bounds memory for very long sessions.
- markDirty() / flushAllSessions() exported for server.ts wiring.
src/server.ts:
- startServer() is now async: awaits initSessions() (bulk load) before listen.
- markDirty(session) after each prepare (processTurn + orphan GC) and once
after forward() completes (covers streaming compress/decompress mutations).
- SIGTERM/SIGINT handlers flush all dirty sessions then exit.
- startServer() returns Promise<Server>; src/index.ts updated.
src/stream.ts: applyRanges uses cacheBlockContent() (enforces the cap) instead
of raw Map.set.
Restart behavior now: client sends full raw history → proxy reloads its saved
CompressionState → processTurn folds history under the persisted block
summaries → model sees only the small compressed context → session survives.
Tests (10 new, 109 pass): round-trip, loadSync hit/miss, debounce coalescing,
bulk loadAll, flushSync eviction, flushAll shutdown, corrupt-file skip,
path-traversal sanitization, disabled mode.
feat: OpenAI Responses API support (/v1/responses)
This reverts commit e711d4e64a.
The proxy injects <acp tokens=".." type="..">mNNNNN</acp> tags into INPUT
messages so the model can reference them. When the model echoes a tag in its
OUTPUT (in-context learning), the metadata leaks to the client as visible
noise — most visible in Codex, which renders raw output text.
All three protocols had tag-echo DETECTION (warn logs) but none STRIPPED.
This adds a streaming-safe stripper to the responses path (stream + non-stream):
Stream (stream-responses.ts):
- stripInjectedTagsFeed/Flush: incremental buffer that drops complete echoed
tags, holds back a potential partial open marker at delta boundaries, and
drops unclosed tag fragments at stream end.
- routeResponsesEvent intercepts response.output_text.delta (both modes),
runs the delta through the stripper, emits the safe portion (or suppresses).
- Generator flushes the stripper buffer before the compress tail.
- Open marker <acp (space) and close </acp> are distinct from the text-
protocol trigger <acp_compress>/</acp_compress>, so they never interfere.
Non-stream (rewriteResponsesJsonResponse):
- Regex strip of <acp [^>]*>...</acp> from all output_text parts, regardless
of whether compress ran.
Tests (6 new, 105 pass): single-delta strip, split-delta strip, partial-open
hold+release, <acp_compress> not stripped, non-stream strip, no-tag unchanged.
Node's fetch (undici) transparently decodes gzip/br responses, so
response.body is plaintext. But response.headers still carries the
upstream 'content-encoding: gzip' marker. The proxy copied upstream
headers verbatim to the client (server.ts:520), so clients received
'content-encoding: gzip' over plaintext bytes and tried to decompress
them a second time — corrupting JSON/SSE parsing.
Add 'content-encoding' to UPSTREAM_HOP_HEADERS so it is dropped on both
sides: the request forward (client→upstream, line 508) and the response
copy-back (upstream→client, line 520). This is the standard reverse-
proxy fix for the fetch-auto-decompress trap.
Found by codex code review.
1. reapOrphanBlocks now checks the client's raw input (msgs) instead of
the kernel's folded output (turn.messages). The folded output never
contains the original ids of compressed messages, so the old param
would eventually count every block as orphaned. (In practice h_msg0
survives in the fold so it didn't reap, but msgs is the correct
semantic — it reflects what the client actually sent.)
2. prepareResponses logs the final tools[] forwarded upstream when
ACP_DEBUG is set, showing each tool's type:name (function vs
namespace vs custom) so we can confirm ACP tools are present.
3. forward() tool-name diagnostic fixed: Responses API uses flat
{name} not nested {function:{name}}, so it always showed '?'.
PROBLEM: the three wire converters (anthropic.ts/openai.ts/responses.ts)
assigned message ids as `raw-${idx}` — a *position* index, not an
identity. None of the three protocols attach a stable id to request-side
message items, so ids were derived from array position. As soon as a
client (or another plugin) deletes/reorders messages:
Turn1 [A,B,C,D,E,F] → compress B,C → effectiveMessageIds=[raw-1,raw-2]
Turn2 client drops B,C → sends [A,D,E,F] → D shifts to raw-1, E to raw-2
covered={raw-1,raw-2} now matches D and E → they get swallowed under
the B+C summary. Live content silently disappears.
FIX 1 — content fingerprints (src/message-id.ts):
id = h_${sha256(role|contentType|toolCallId|toolName|text)}
Two identical messages collide; a ClusterCounter appends _${N} so the
Nth duplicate gets a distinct id (prevents `covered` sets from
collapsing two unrelated turns onto one id and losing recent content).
Trade-off vs a real client id: deleting a duplicated message only
disturbs the cluster of that identity (local damage), never the whole
downstream (global damage like position). Fully distinct messages are
completely immune to reordering/deletion.
tool_call/tool_result bind to the protocol's call_id (tool_use.id /
tool_call_id / call_id), so two same-name same-args calls stay distinct.
FIX 2 — orphan block GC (src/orphan-gc.ts):
With fingerprints, a block whose effective ids the client has deleted
becomes an orphan: no anchor in the visible context, so its summary
floats to the top (insertAt ?? 0), polluting the model's view. After
each processTurn we count consecutive turns a block has had zero id
hits; at ORPHAN_THRESHOLD=3 it is deactivated and its blockContents
cache entry is dropped. Partial orphans (some ids present) are left
alone — deactivating on partial matches would destroy blocks during
normal incremental edits.
Tests (10 new, 99 pass):
- proxy-message-id: stability, reordering immunity, duplicate distinctness,
call_id binding, prefix-vs-mNNNNN non-collision (anthropic/openai/responses).
- proxy-orphan-gc: hit-keeps-active, threshold-reap, streak-reset, cache-clear.
- basic/proxy-basic: updated h_* prefix assertions.
Codex review found two issues with the decompress cache added in d75326d:
1. full:false semantics broken — cache was populated with full:true
unconditionally, so a decompress hit returned the complete original
even when the caller did not pass full:true (default is one-level:
direct messages + nested child summaries).
2. cache never cleared — successful decompress deactivated the block in
state but left the cached originals in session.blockContents, causing
unbounded memory growth and retained sensitive content over a long
session.
Fix:
- session.blockContents now stores {one, full} views; applyRanges caches
both at compress time.
- resolveDecompress (extracted to src/decompress-shared.ts, shared by
both compress-loop files) selects the view by args.full, deletes the
cache after a successful decompress, and only deactivates the block
when content was actually recovered.
Tests: 4 new (proxy-decompress-cache.test.ts) covering one vs full views,
cache deletion, and missing-cache fallback. 89 pass.
decompress used collectBlockContent(state, block, ctx.messages) which matches
block.effectiveMessageIds against CoreMessage.id in ctx.messages. But
ctx.messages is the POST-FOLD view (prepared.processedMessages): messages
already compressed in an earlier round are replaced by their summary and are
absent, so collectBlockContent returns 0 items. The block was then
deactivated unconditionally, discarding it and making later retries fail.
A second, deeper issue: message ids are raw-${idx}, regenerated every round
by responsesToCore, so ids are not stable across rounds — even keeping the
pre-fold messages would not guarantee a match.
Fix: cache the original content at compress time, when the source messages
are still present in this round's ctx.messages (a freshly-compressed block
has not been folded yet — folding happens in the next processTurn). The
cache lives on the session (session.blockContents: Map<blockId, {text,count}>).
decompress reads from the cache first and only falls back to
collectBlockContent. deactivateBlock now runs only when content was actually
recovered (count>0 or a cache hit), so a 0-result no longer poisons the block.
Applied to both the chat path (compress-loop.ts) and the responses path
(compress-loop-responses.ts). Originals are captured in stream.ts applyRanges
right after applyCompression, by diffing block ids before/after and calling
collectBlockContent on the new blocks with full:true.
Four protocol-correctness issues found by review.
1. Query string broke protocol detection (server.ts)
detectProtocol used url.endsWith("/responses") etc. A request like
/v1/responses?foo=1 did not match, so compression/injection silently
disabled. Strip the query string (url.split("?")[0]) before matching.
2. CRLF SSE events lost across ALL paths (not just responses)
The SSE splitter only matched \n\n. CRLF-emitting upstreams (\r\n\r\n)
dropped every event and forced synthesized response ids. Previously only
the responses path was fixed; now a shared normalizeSseLineEndings helper
(sse-util.ts) is applied at every split site: compress-loop.ts (chat),
compress-loop-responses.ts, stream.ts, stream-openai.ts, stream-responses.ts.
Also drains the residual SSE buffer at stream end in compress-loop.ts
(previously the last event was dropped if the stream lacked a trailing
blank line).
3. 10-min timeout did not cover the response body (fetch-util.ts)
fetchWithTimeout cleared the timer when fetch() resolved (headers only), so
a stalled SSE body was never aborted — contradicting the docstring.
Changed the API to return { response, clearTimer }; callers clear the
timer only after the body stream is fully consumed, so the timeout now
guards the full headers+body lifecycle. All three call sites updated
(compress-loop.ts, compress-loop-responses.ts multi-round loops use an
activeClearTimer cleared in the reader finally; server.ts passthrough
clears at every stream-consumed exit point).
injectResponsesTool only injected COMPRESS_TOOL_RESPONSES, but the compress loop
(compress-loop-responses.ts executeProxyTool) dispatches all 4 tool names in
PROXY_TOOL_NAMES, and the injected compress prompt documents search_context /
decompress usage. The model could read the instructions, try to call
search_context to locate a block, then decompress it — but those tools were
absent from the tools array, so the model reported 'cannot call search_context'.
This was a declaration/implementation mismatch: the loop was ready to execute
decompress/search_context/acp_status, but server.ts never declared them.
Fix: add DECOMPRESS_TOOL_RESPONSES, SEARCH_CONTEXT_TOOL_RESPONSES,
ACP_STATUS_TOOL_RESPONSES (Responses flat format), group them as
ACP_TOOLS_RESPONSES, and injectResponsesTool now appends all four (idempotent
on tool name, matching the OpenAI chat path which already injects all four via
ACP_TOOLS_OPENAI).
Addresses four protocol-correctness bugs found by reviewing the Responses
implementation. All covered by new tests (tests/proxy-responses-review.test.ts).
1. Standard function tools were rewritten as custom tools (responses.ts)
coreToResponses used PROXY_TOOL_NAMES.has(name) to pick function_call vs
custom_tool_call. A host's normal function tool (e.g. get_weather) hit the
else branch and was emitted as custom_tool_call — changing Responses API
semantics and risking upstream rejection.
Fix: track call_ids that entered as custom_tool_call/custom_tool_call_output
in a Set, and emit the original item type by call_id membership.
2. Fabricated success after a failed terminal (compress-loop-responses.ts)
classify only treated response.completed as terminal. response.failed and
response.incomplete passed through (default), then buildCompleted emitted a
synthetic response.completed — giving the client a contradictory state
machine (failed AND completed).
Fix: classify failed/incomplete/error as terminal (isMeta, no auto-yield),
capture the raw event, and at stream end replay it verbatim instead of
synthesizing completion.
3. Valid CRLF SSE streams were swallowed (compress-loop-responses.ts)
The SSE loop split on indexOf("\n\n"). A service emitting \r\n\r\n event
separators never matched, so events (including response.created/completed)
vanished and the proxy synthesized a bogus response id.
Fix: normalize CRLF / lone CR to LF after each decode; join multi-line
data: fields per SSE spec (previously only the first data: line was kept).
4. Unknown Responses input items silently dropped (responses.ts)
The default switch branch dropped anything outside the fixed type list, so
OpenAI adding new item types (or provider extension types) would lose data.
Fix: preserve unknown items verbatim in the preamble (forward-compat).
Also:
- ACP_DUMP_REQ=1 gates request body dumps (avoid concurrent file corruption)
- ACP_DEBUG logs input item types + rebuilt conversion stats
- [acp-usage] logs per-round cache hit ratio from response.completed usage
- [acp-compress-args]/[acp-compress-input] diagnostics for compress parse failures
Addresses review findings on the HTTP layer before public/long-running use:
- readBody: cap at 100 MB (reject oversized bodies with 413) to avoid
memory exhaustion; previously unbounded.
- All three upstream fetch sites (server.ts forward, compress-loop,
compress-loop-responses) now use fetchWithTimeout (10 min AbortController)
so a stuck upstream cannot hold a client connection open forever.
- config.ts safeReadJson no longer silently swallows parse errors (only
ENOENT is quiet); a malformed providers file now logs a warning.
- server.ts catch returns 413 for body-size rejections (vs generic 502).
- session.ts: documents the known limitation that state is in-process only
(restart drops it; multi-instance does not share) and that session id
defaults to a hash of the first user message (multi-tenant must send
x-acp-session). Persistence tracked as TODO.
New file: src/fetch-util.ts (fetchWithTimeout + MAX_REQUEST_BYTES).
Three layered bugs prevented Codex code_mode from working through the proxy.
All verified with real Codex CLI + comfly relay (gpt-5.6-sol, responses_lite).
1. additional_tools dropped (src/responses.ts)
responsesToCore's default branch discarded all unknown item types,
including `additional_tools` which carries the code_mode exec/wait tool
definitions and must stay at input[0] (openai/codex client.rs splice(0..0)).
Fix: OPAQUE_ITEM_TYPES set + preamble capture; prepareResponses re-prepends
them verbatim so code_mode tool exposure is preserved.
2. responses_lite contract broken (src/server.ts)
injectResponsesInstructions wrote the compress prompt into the top-level
`instructions` field, but responses_lite requires instructions="" (the
real prompt lives in input[1] developer message). A non-empty instructions
field took the response off the lite processing path.
Fix: inject compress prompt + base system as a developer message inside
input (after additional_tools, before conversation); top-level instructions
is never touched.
3. exec results dropped -> infinite loop (src/responses.ts)
custom_tool_call / custom_tool_call_output (code_mode exec call + result)
hit the default branch and were discarded. The model never saw its own
tool results, so it re-issued the same exec call every round.
Fix: add explicit cases in responsesToCore (-> assistant tool-call /
tool tool-result) and coreToResponses (-> custom_tool_call /
custom_tool_call_output), so exec history round-trips through compression.
Also:
- ACP_DUMP_REQ=1 gates request body dumps (avoid concurrent file corruption)
- ACP_DEBUG logs input item types + rebuilt conversion stats
- ACP_NO_COMPRESS_PROMPT / ACP_RENDER_NONE are debug toggles
- Text-protocol scaffolding (stream-responses.ts, buildCompressTextSystemPrompt)
remains in tree but is inert under the default function protocol.
Fixes the 'OutputTextDelta without active item' error from Codex. The
Responses API requires a message item to be declared via
response.output_item.added + response.content_part.added BEFORE any
response.output_text.delta for that item. Previously the proxy emitted
bare output_text.delta with an undeclared fixed item id.
Changes:
- classifyResponsesSseEvent: mark response.created/in_progress/completed
as isMeta so round 2+ suppresses them (merged stream = single open)
- buildMessageItemSequence replaces buildOutputTextDelta: emits the full
added+content_part.added+delta+done+content_part.done+item.done sequence
- Unified passthrough: round 2+ now passes upstream content events as-is
(preserving their own output_item.added declarations) instead of
re-wrapping deltas with a synthetic item id
- nextOutputIndex counter tracks emitted items across rounds
compressLoopResponsesStream mirrors compressLoopStream but for the
Responses API: accumulates function_call items (output_item.added +
function_call_arguments.delta), executes proxy-only calls, appends
function_call + function_call_output items to input[], and re-requests
(up to 5 rounds). Real function calls and plain text pass through.
This fixes the single-turn bug where a compress call ended the Codex
turn, leaving the model unable to continue its work.
- src/compress-loop-responses.ts (new): the loop + SSE event classifier
- src/compress-loop.ts: export buildVisibilityMarker (reused)
- src/server.ts: Responses branch now uses the loop instead of the
single-turn rewriter
- tests/proxy-responses-stream.test.ts: multi-round test with mocked
fetch (compress -> re-request -> continue)
Third protocol alongside anthropic + openai chat completions. This lets
billion-context serve clients that only speak the Responses API (notably
Codex 0.147, which dropped wire_api='chat' and forces 'responses').
New files:
- src/responses.ts: ResponsesRequestBody (input/items) <-> CoreMessage.
input items are typed (message/function_call/function_call_output) and
map closer to core messages than chat completions does.
- src/stream-responses.ts: Responses SSE rewriter. Responses carries the
event type in the SSE event: line (not in JSON data, unlike chat).
Suppresses compress function_call arguments, emits result as a text
delta + clean response.completed at the end.
- tests/proxy-responses-stream.test.ts: passthrough + compress-suppress.
server.ts wiring:
- detectProtocol: /responses -> 'responses'
- prepareResponses: responsesToCore -> processTurn(text-only) ->
coreToResponses -> inject instructions (system) + nudge (trailing
user item) + injectResponsesTool (flat Responses tool format).
- forward: responses branch uses rewriteResponsesSseStream (single-round;
multi-round compress loop is chat-completions-only for now).
- non-stream: rewriteResponsesJsonResponse.
compress-tool.ts: COMPRESS_TOOL_RESPONSES (flat {type:function,name,
parameters} form, not nested function:{...}).
Use kernel renderTags:"text-only" + forbid tag echoing in prompt
Request-side (server.ts): after processTurn, log renderTags strategy
and tag-injection counts — confirms text-only keeps tool messages
pristine while text messages stay tagged for the LLM.
Response-side (esbuild-safe): detect <acp tags leaking into model
output. Plain-function ctx.log survives tree-shaking, so detection
lives in server.ts response-forwarding loops (Buffer.toString) and
the two non-stream rewrite functions, NOT inside async generators
(esbuild DCEs ctx.log calls reachable only from generators).
Anthropic routeEvent keeps a fine-grained per-delta check since its
ctx.log survives.
Two related changes that keep tags out of model output and client history:
1. server.ts: pass renderTags:"text-only" to processTurn (both Anthropic
and OpenAI paths). The acp-kernel render-refs node now skips tool
messages (tool-call args / tool-result content) so structured JSON is
never polluted by an <acp> tag. This replaces the stripToolTags hack
(proxy-side post-processing) — the responsibility moves into the kernel
where ref assignment and tag rendering are properly separated.
- delete ACP_TAG_RE constant and stripToolTags function
- delete the two stripToolTags(turn.messages) call sites
2. compress-tool.ts: ACP TAGS prompt section now states tags are system
metadata injected by the proxy and must NEVER be echoed in responses;
use only the ref ID inside compress calls. The old wording encouraged
the model to 'use these annotations', which led it to reproduce the
tag syntax in its output (observed as </acip> typos in replies).
Tags never reach the client regardless — they exist only between proxy
and provider on the request path. This change ensures they also don't
leak into the model's own generated text.
fix: address all code review findings (condense, routing, session-id, dead code)
Seven review findings addressed:
1. condense was dead code — config.ts parsed ACP_CONDENSE_ENABLED /
ACP_KEEP_RECENT_TOOL_RESULTS / ACP_MIN_CHARS_TO_CONDENSE /
ACP_MAX_KEPT_CHARS into opts.condense but server.ts never called
condenseOldToolResults(). Now wired in via applyCondense() helper,
invoked after stripToolTags in both prepareAnthropic and prepareOpenai.
Session stats (condensedToolResults, tokensSaved) are incremented.
2. OpenAI streaming routing bug — forward() keyed useRewriter on the
GLOBAL opts.compress.injectTool flag, so when per-request
compressInjected=false (OpenAI title-gen: maxTokens<=200 ||
messages.length<=2), the OpenAI SSE stream was routed into
rewriteSseStream which is the Anthropic SSE rewriter
(content_block_*/message_delta). Now keys on per-request
prepared.compressInjected so title-gen falls through to plain
pipeThrough.
3. Session-id collision risk — replaced 32-bit FNV-1a hash with SHA-256
truncated to 64 bits (16 hex chars). Keeps determinism (required for
cross-turn session continuity) while making birthday-collision
negligible. For multi-agent setups, explicit x-acp-session header
remains the recommended mitigation. New shared util.ts:hashId().
4. Duplicate ctx declaration in forward() — removed the inner shadow.
5. Dedup hash — the identical FNV-1a hash() existed in both
anthropic.ts and openai.ts; consolidated into util.ts:hashId().
6. Dead exports removed — extractOpenaiSystem and validateToolPairing
(openai.ts) had no callers in src or tests.
7. Forward-streaming comment block — document the routing logic so the
Anthropic-only else-branch is clearly intentional.
All 79 tests pass. Typecheck clean.
fix: keep system prompt byte-stable for prefix cache
The nudge (which changes every turn — token count, growth %, dynamic
compress example) was being merged into the system prompt. The system
prompt is the prefix-cache anchor: any byte change at its tail
invalidates the entire cached prefix, so cache hit rate collapsed to
~0% across turns.
Reproduce before fix: consecutive identical requests saw system_len
jump 16871 → 23432 → 16871 ... each turn.
Fix mirrors pai-acp's design: the system block now carries ONLY the
static compress prompt (byte-stable); the nudge is appended as a
separate trailing user message instead. Verified system_len stays
constant across turns (16871 → 16871 → 16871).
- injectSystem(): drop nudge param, only inject static compress prompt
- prepareOpenai(): append nudge as trailing {role:user} message
- prepareAnthropic(): append nudge as trailing {role:user} message
79 tests pass. typecheck clean.
Replaces the key-based route table with a minimal {provider: url} map.
Agents point their base URL at the proxy using the provider name as a
path segment; the proxy strips the segment and forwards to that
provider's root. API keys are NEVER stored — they pass through to the
upstream untouched.
provider file: {"glm": "https://bigmodel.cn", ...}
agent baseURL: http://localhost:8787/v1/glm
proxy routes: /v1/glm/chat/completions → bigmodel.cn/v1/chat/completions
Context window is now auto-detected from body.model via a built-in
table (claude=200k, gpt-4o=128k, glm=128k, ...) — users no longer
configure a context limit.
End-to-end verified against a mock upstream (path rewrite + key
pass-through for both OpenAI and Anthropic auth headers). 79 tests pass.
Includes MINOR cleanup from the fork: reserved-path-segment guard
(v1/chat/completions/messages/models/api rejected as provider names),
longest-name-first matching, trailing-slash normalization.
Standalone HTTP reverse proxy that rewrites Anthropic/OpenAI streams with
acp-kernel compression. Any agent that can set a base URL (Claude Code,
Codex, Cursor, Aider) works without adapter code.
Inlined from acp-proxy with review fixes applied:
- C1 (stream.ts): message_delta/message_stop were dropped unconditionally,
hanging non-compress Anthropic streams. Now preserved unless a compress
tool_use was converted.
- M2 (stream.ts): replacement text block hardcoded index:0, reopening an
already-closed text block. Now reuses the suppressed tool_use's index.
- C2 (compress-loop.ts): decompress honored args.toFile from the model —
path traversal risk. Proxy mode now always writes to a temp dir.
67 tests pass (basic + sse + anthropic-stream regression).
refactor: drop the dead SSE rewriters that kept causing mis-triage
- parseCompressInput now returns {ranges, diagnostics} so callers can
see WHY entries were dropped (kernel 0.0.47 invalidReasons)
- applyRanges failure receipt includes kind + dropped count + per-entry
reasons (max 8, 200 chars each) — the model sees exactly which entry
was rejected and why, instead of blind-retrying the same bad shape
- bump acp-kernel to 0.0.47 (persist retry backoff + spill files +
tmp-name-per-attempt; exact pin per AGENTS.md)
- .gitignore: tmp/ (e2e sandbox dirs leaked into a commit earlier today)
Deletes local src/{anthropic,openai,responses,bili-message,message-id}.ts
(~1000 lines) and imports from acp-kernel/wire (0.0.25). Extraction was
verbatim (hashId byte-identical) so message ids and behavior are unchanged.
- kernel 0.0.24 → 0.0.25
- server.ts / loop adapters / tests now import from acp-kernel/wire
- orphan-gc.ts comment pointer updated
Regression: 419/419 tests green (the 9 codec-consumer test files now
exercise the kernel copy), build clean.
Consumers of this shared surface: proxy (this PR), omp Plan B (#52 follow-up).
Root cause: tokenCount (drives the nudge/compress decision) was computed via
estimateTokensFast = chars/4. This undercounts CJK text 3-4x (Chinese chars
count as 0.25 token instead of ~1), so Chinese-heavy sessions (ZCode reviewing
a CN project: real 50k tokens) read as ~35k — never reaching the 50k compress
threshold. Compression never fired.
The upstream API already reports the authoritative input_tokens in every
usage (Anthropic message_delta, OpenAI prompt_tokens, Responses input_tokens).
That is the ground truth — no estimate (neither chars/4 nor a BPE tokenizer)
can beat it. Use it.
Changes:
- session.stats: add lastInputTokens (single-turn, overwritten each turn).
Existing cumulative inputTokens is kept for the web UI totals.
- compress-loop-{anthropic,openai,responses}.ts onCacheUsage/usage: also
set lastInputTokens = input (overwrite, not accumulate).
- server.ts three prepare* branches: tokenCount = session.stats.lastInputTokens.
First turn = 0 (no history yet, never triggers anyway). All estimateTokensFast
calls removed from the tokenCount path.
- persist.ts: round-trip lastInputTokens with backward-compat (old files
default to 0).
- tests: stats literals updated.
estimateTokensFast is still imported (used by buildStatusReport for the
acp_status display breakdown), but no longer drives any compress/nudge decision.
condenseOldToolResults was a proxy-layer mechanical condenser (stub large old
tool results to a 400-char head) introduced in v0.1.0. It runs EVERY turn and
rewrites stable history (full→stub as a sliding window moves), which breaks
provider prefix caching for everything after the shifted message.
With ACP's model-driven compress tool now handling context management, condense
is both redundant and harmful (the two are fundamentally opposed: condense
mutates stable history each turn; prefix caching requires byte-stable history).
Removed across 8 source files + 7 test files:
- anthropic.ts: condenseOldToolResults, CondenseOptions, CondenseResult,
CONDENSED_TAG
- openai.ts / responses.ts: import + re-export
- server.ts: applyCondense fn, 3 call sites (→ turn.messages), stats field
- config.ts: condense ProxyOptions/FileConfig block + 4 ACP_* env parses
- session.ts: condensedToolResults field
- persist.ts: field (deserialize still loads old files — extra field ignored)
- cli.ts / README.md: references
Tests: 145 → 139 (removed 6 condense-specific tests across 2 files).
Review #2 findings (more comprehensive than #1):
#2 (compress-loop*.ts): fetch-timeout timer leak across compress-loop
rounds. activeClearTimer = clearTimer overwrote the previous round's
timer without clearing it, so rounds 1..N-1 each leaked a 10-min timer
that self-fired harmlessly but accumulated handles in the event loop
over a long session. Clear the previous timer before overwriting
(3 lines, both openai and responses variants).
#5 (orphan-gc.ts): orphanStreak was monkey-patched onto the kernel's
Block type via (block as unknown as {orphanStreak?:number}).orphanStreak
— fragile, no type safety, breaks if the kernel Block shape changes.
Replaced with a module-level WeakMap<object, number> keyed by the block
object. TS now type-checks the access; counts are GC'd when a block is
dropped.
#7 (README.md): stale test count (79 → 136) and missing env-var docs
(BILI_PERSIST, BILI_PERSIST_DEBOUNCE_MS, MAX_SESSIONS,
BILI_SESSIONS_DIR). Both fixed.
#8 (tests/*.test.ts): 7 type errors under tsc --noEmit (role: string
not narrowing to the union literal; Session literals missing new fields
after inFlight/persisted/blockContents were added). Fixed with 'as
const' on roles and the missing fields. Full tsc --noEmit now 0 errors.
#10 (persist.ts): comment referenced AGENTS.md which doesn't exist;
replaced with an inline note on the known concurrency limitation.
Previously deriveSessionId keyed on the conversation dimension alone (a
client header or a hash of the first user message). In a multi-tenant setup
(several clients, several accounts, several providers, several protocols)
this is unsafe:
- pi (acct A) and opencode (acct B) sending the same opening line would
collide on the same content fingerprint → their compression states
would bleed into each other.
- the same key hitting two providers (zhipu vs bailian) with the same
first message would also collide.
- an Anthropic-format and an OpenAI-format request carrying the same text
would collide even though they are different wire protocols.
The session is now keyed by FOUR dimensions, all AND-ed:
1. protocol — 'anthropic' | 'openai' | 'responses'
2. upstream — the resolved upstream ORIGIN (provider host), not the
full rewritten URL (so a path change doesn't fork state)
3. apiKey — normalized Authorization / x-api-key credential
4. conversation — client-provided signal when available
(x-session-affinity / x-acp-session / x-session-id /
x-opencode-session for OpenCode; previous_response_id
for Responses' native linkage), falling back to a
content fingerprint of the first user message
src/session-id.ts (new) centralizes this. The per-protocol derive functions
are renamed conversationSignal{Anthropic,Openai,Responses} — they now
contribute ONLY the conversation dimension; the other three are mixed in by
the caller. server.ts resolveUpstream is hoisted before prepare() so the
session can embed the provider origin, and route is threaded into forward().
affinityToken() (new) produces the conversation dimension ALONE, formatted
as OpenCode's 'ses_<hash>' x-session-affinity value. It is suitable for
forwarding to an upstream that understands that convention (e.g. a
multi-account load-balancer doing sticky routing), and contains NO key or
upstream so it is safe to send upstream and stable across account rotations.
Not wired into forwarding yet — only emitted when needed (future multi-acct
routing). Single-account direct upstreams already get automatic prefix
cache by content, so no header injection is needed there.
Tests: 131 (was 119) — +12 for session-id (4-dimension isolation,
client-signal priority, affinity-token format and key-independence, etc.).
PROBLEM: the three wire converters (anthropic.ts/openai.ts/responses.ts)
assigned message ids as `raw-${idx}` — a *position* index, not an
identity. None of the three protocols attach a stable id to request-side
message items, so ids were derived from array position. As soon as a
client (or another plugin) deletes/reorders messages:
Turn1 [A,B,C,D,E,F] → compress B,C → effectiveMessageIds=[raw-1,raw-2]
Turn2 client drops B,C → sends [A,D,E,F] → D shifts to raw-1, E to raw-2
covered={raw-1,raw-2} now matches D and E → they get swallowed under
the B+C summary. Live content silently disappears.
FIX 1 — content fingerprints (src/message-id.ts):
id = h_${sha256(role|contentType|toolCallId|toolName|text)}
Two identical messages collide; a ClusterCounter appends _${N} so the
Nth duplicate gets a distinct id (prevents `covered` sets from
collapsing two unrelated turns onto one id and losing recent content).
Trade-off vs a real client id: deleting a duplicated message only
disturbs the cluster of that identity (local damage), never the whole
downstream (global damage like position). Fully distinct messages are
completely immune to reordering/deletion.
tool_call/tool_result bind to the protocol's call_id (tool_use.id /
tool_call_id / call_id), so two same-name same-args calls stay distinct.
FIX 2 — orphan block GC (src/orphan-gc.ts):
With fingerprints, a block whose effective ids the client has deleted
becomes an orphan: no anchor in the visible context, so its summary
floats to the top (insertAt ?? 0), polluting the model's view. After
each processTurn we count consecutive turns a block has had zero id
hits; at ORPHAN_THRESHOLD=3 it is deactivated and its blockContents
cache entry is dropped. Partial orphans (some ids present) are left
alone — deactivating on partial matches would destroy blocks during
normal incremental edits.
Tests (10 new, 99 pass):
- proxy-message-id: stability, reordering immunity, duplicate distinctness,
call_id binding, prefix-vs-mNNNNN non-collision (anthropic/openai/responses).
- proxy-orphan-gc: hit-keeps-active, threshold-reap, streak-reset, cache-clear.
- basic/proxy-basic: updated h_* prefix assertions.
Standalone HTTP reverse proxy that rewrites Anthropic/OpenAI streams with
acp-kernel compression. Any agent that can set a base URL (Claude Code,
Codex, Cursor, Aider) works without adapter code.
Inlined from acp-proxy with review fixes applied:
- C1 (stream.ts): message_delta/message_stop were dropped unconditionally,
hanging non-compress Anthropic streams. Now preserved unless a compress
tool_use was converted.
- M2 (stream.ts): replacement text block hardcoded index:0, reopening an
already-closed text block. Now reuses the suppressed tool_use's index.
- C2 (compress-loop.ts): decompress honored args.toFile from the model —
path traversal risk. Proxy mode now always writes to a temp dir.
67 tests pass (basic + sse + anthropic-stream regression).