Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
44 commits
Select commit Hold shift + click to select a range
a2b2822
fix: #381 review — SQLite set adjudication, -journal, conflict rename…
Aug 31, 2026
8777114
fix: archive blocks across omp native compaction instead of silently …
Aug 31, 2026
1b258ee
docs: document POST /__bili/plugin/compact in the plugin protocol (#395)
Aug 31, 2026
29fc896
fix: acp-loop zero-side-effect truncation retry + well-formed anthrop…
Sep 2, 2026
70850f3
fix: responses adapter synthesizes response.created before orphan res…
Sep 2, 2026
037070d
fix: bili pi launcher disables pi's native auto-compaction (#447)
Sep 2, 2026
3175990
fix: keep sibling text when a mixed chunk's other field is a pure tag…
Sep 2, 2026
cf959cb
fix(#460): strip render-tag echoes from non-injected proxy-mode chat SSE
Sep 2, 2026
55a1232
fix(#453): hard backstop for plugin-mode context overflow
Sep 2, 2026
0dde055
feat: detect + retry fake completions (tool call written as text, #371)
Aug 31, 2026
0c0bf3f
fix(#452): engage tag-echo filter on tag-head tails so split-delta ta…
ranxianglei Sep 2, 2026
60e76f6
Merge pull request #469 from ranxianglei/2026-09-02_split-delta-tag-s…
ranxianglei Sep 2, 2026
803dd0c
merge master (split-delta gate from #469) into mixed-field-drop (#462)
ranxianglei Sep 2, 2026
051c3d2
Merge pull request #464 from ranxianglei/2026-09-03_plugin-chat-mixed…
ranxianglei Sep 2, 2026
abac2c2
release v0.1.72
ranxianglei Sep 2, 2026
400ff21
Merge pull request #471 from ranxianglei/2026-09-03_release-v0.1.72
ranxianglei Sep 2, 2026
b612263
Merge pull request #466 from ranxianglei/2026-09-03_proxy-noinject-ss…
ranxianglei Sep 2, 2026
ab7cbe7
docs: retarget the sibling-text fix refs to #463 (the #462 refs point…
Sep 2, 2026
fe9b234
test: fix pipePluginChatWithStrip call order after #466 signature swap
ranxianglei Sep 2, 2026
9883c6c
fix(test): repair the swapped pipePluginChatWithStrip args left by th…
Sep 2, 2026
8477a72
Merge pull request #472 from ranxianglei/2026-09-03_fix-466-test-call…
ranxianglei Sep 2, 2026
9895a8b
fix(#460 residual): strip render-tag echoes on the paths that skip co…
Sep 2, 2026
6e9092b
refactor: drop the dead SSE rewriters that kept causing mis-triage
Sep 2, 2026
6bc3ddf
Merge remote-tracking branch 'origin/master' into verify-378
ranxianglei Sep 2, 2026
892ffa2
fix: non-inject SSE strip must read the buffered body after fake-comp…
ranxianglei Sep 2, 2026
67285ae
Merge pull request #378 from ranxianglei/2026-08-31_fake-completion-r…
ranxianglei Sep 2, 2026
e92ea74
release v0.1.73
ranxianglei Sep 2, 2026
e2d2e38
Merge pull request #474 from ranxianglei/2026-09-03_release-v0.1.73
ranxianglei Sep 2, 2026
f7f6f1c
Merge branch 'pr-440' into verify-440-444
ranxianglei Sep 2, 2026
5301346
Merge branch 'pr-444' into verify-440-444
ranxianglei Sep 2, 2026
261978b
fix: truncation retry must count live-forwarded reasoning chunks; ext…
ranxianglei Sep 2, 2026
bac23c3
Merge branch 'master' into 2026-09-02_acp-loop-truncation-retry
ranxianglei Sep 3, 2026
135f832
Merge pull request #440 from ranxianglei/2026-09-02_acp-loop-truncati…
ranxianglei Sep 3, 2026
2053e39
Merge branch 'pr-475' into verify-475
ranxianglei Sep 3, 2026
8cbeebb
fix: G3 JSON strip branch must read responseBody, not the drained ups…
ranxianglei Sep 3, 2026
0dd1ed8
Merge pull request #475 from ranxianglei/2026-09-03_tag-echo-gaps-v2
ranxianglei Sep 3, 2026
ba95dd4
release v0.1.74
ranxianglei Sep 3, 2026
0647309
Merge pull request #477 from ranxianglei/2026-09-03_release-v0.1.74
ranxianglei Sep 3, 2026
8e15e4f
Merge branch 'master' into 2026-08-31_windows-overlay-review-fixes
ranxianglei Sep 3, 2026
09ef208
Merge pull request #396 from ranxianglei/2026-08-31_windows-overlay-r…
ranxianglei Sep 3, 2026
4fa7fcc
merge master: resolve plugin.js import (master's pipePluginChatWithSt…
ranxianglei Sep 3, 2026
1000c12
Merge pull request #421 from ranxianglei/2026-08-31_omp-native-compac…
ranxianglei Sep 3, 2026
cdbf2bc
Merge pull request #448 from ranxianglei/2026-09-02_pi-disable-native…
ranxianglei Sep 3, 2026
ba857b2
merge pr-467 (verify)
ranxianglei Sep 3, 2026
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 12 additions & 2 deletions PLUGIN.md
Original file line number Diff line number Diff line change
Expand Up @@ -115,11 +115,21 @@ Context-level visibility for plugin UIs (status bars / slash commands):

`contextTokens` is the last reported context size (input + cache-read) — the same value the nudge decision reads. Errors: `400` missing `conversationId`, `404` unknown conversation.

### 5. MITM transparent-proxy mode
### 5. `POST /__bili/plugin/compact`

Notify the proxy that the agent performed an **in-session native compaction** (e.g. omp's `/compact` or its auto threshold): the next model request re-sends a shortened history (compaction summary + retained tail) under the SAME conversation id. Fire-and-forget is fine — a failed notification must never break the agent's compaction.

```json
{ "conversationId": "the same value you send as x-bili-plugin-conversation" }
```

Effect: the proxy marks a one-shot compaction boundary on the session. On the next model request, blocks that were active before the compaction but no longer anchor into the shortened history are downgraded to a pre-compaction archive (listed by `acp_status` with a reason; `decompress` on one returns an explicit "unavailable" error instead of failing silently), and stale `byRaw`/`byRef` mappings are pruned to the live ids. Errors: `400` invalid JSON / missing `conversationId`, `404` unknown conversation.

### 6. MITM transparent-proxy mode

The `/bili/` prefix is absent in MITM mode, so URL-based detection cannot work. Instead the proxy's own launcher (`bili pi` / `bili codex` / `bili claude`) exports `BILLION_CONTEXT_PROXY=http://127.0.0.1:<port>` in the child env, next to the `HTTPS_PROXY` + CA vars it already sets. A plugin detects cooperative mode by reading that env var (the proxy origin for all `/__bili/plugin/*` calls); everything else (headers, tool forwarding, status) is identical — the `x-bili-plugin*` headers pass through the MITM tunnel into the same pipeline. This is also where `x-bili-plugin-context-window` matters most: MITM upstreams are often private relays the models.dev registry doesn't know.

### 6. Lifecycle of one compression (what the plugin does)
### 7. Lifecycle of one compression (what the plugin does)

1. Model replies with a native `compress` tool call (args contain `startId`/`endId` refs it read from the tag-annotated context).
2. The agent ends the assistant turn; the plugin's tool handler fires.
Expand Down
109 changes: 109 additions & 0 deletions devlog/2026-09-02_acp-loop-truncation-retry/DESIGN.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,109 @@
# DESIGN: acp-loop truncation retry + well-formed Anthropic error stream

## Problem shape

`runCompressLoop` is an `AsyncGenerator<Buffer>`: it reads the upstream SSE
stream, forwards client-facing chunks as it parses them, and only knows the
stream was truncated *after* the parse loop ends without a done event
(`!sawDone`). `fetchWithRetry` cannot see this failure — it inspects
`response.ok` after headers and never reads the body. So recovery has to live
at loop level, where the "did I already commit to the client?" state is known.

## Retry predicate — the zero-side-effect invariant

A truncated round can be transparently replayed **iff** nothing observable
happened. All of these must hold at the end of the parse loop:

- `!sawDone` — the stream actually truncated;
- `!forwardedAny` — no chunk was yielded to the client this attempt. A
per-attempt flag set by `fwd()` wrapping the five yield sites in the parse
loop (raw text, synthetic text, raw reasoning, synthetic reasoning, meta).
Ping-only streams count as forwarded (conservative: the client saw
something, so a retry would duplicate it);
- `calls.length === 0` — no tool call was collected. The `tool_call` /
marker / `emitText` yields happen *after* the parse loop, so they are not
yet covered by `forwardedAny` and need explicit checks:
- `calls.length === 0` rules out tool_call and marker yields (both are
derived from `calls`);
- `!(ctx.textProtocol && assistantText.length > 0)` rules out the
text-protocol `emitText` yield (only emitted when textProtocol and the
round produced text);
- `!signal?.aborted` — the client is still connected;
- `!truncationRetried` — function-level flag: at most ONE retry per request
(the issue says "走一次"). The flag is never reset, so a second truncated
round (round 2+) also fails fast.

Placement: the check runs **after** the parse loop and **before**
`recordUsage`. This matters: a round-2+ `message_start` sets `usage` without
forwarding anything, so recording before the retry check would double-count
the retried attempt's tokens.

Mechanics: the parse loop is wrapped in an inner `for (;;)` attempt loop.
On a qualifying truncation the loop calls `fetchUpstream(roundBody)` (the
hoisted `fetchWithRetry` wrapper — same backoff, same `BILI_REPLAY_RETRY_*`
env knobs, same #189 rejection logging) and `continue`s; the outer round
number is unchanged, so a round-1 retry re-parses as round 1 and its
`message_start` is forwarded exactly once (attempt 1 forwarded nothing).
Per-attempt state (text/reasoning/calls/usage/finishReason/sawDone/
suppressCompletion/forwardedAny) is reset at the top of the attempt loop.

`roundBody` (new): the re-request path replaces the body each round
(`compressMessages`); the old code re-fetched with `requestOptions` + a local
`newBody`. The retry must re-send **the body that produced the truncated
stream** — on round 1 that is the original `requestBody`, on round N it is
the compressed body. `roundBody` is updated right before the
`currentUpstream` swap, so it always holds the last body actually sent.

Failure of the retry itself falls through to the existing truncation error
path (empty body on the retry response is converted to
`UpstreamHttpError` so it is caught by the same handler).

## Adapter: tracking what was forwarded

The Anthropic adapter closure gains two pieces of state:

- `messageStartForwarded: boolean` — set where the round-1 `message_start`
is forwarded (always forwarded in round 1, so the flag is set there);
- `openBlocks: number[]` — client indices of blocks whose
`content_block_start` was forwarded but whose `content_block_stop` was not.
Pushed in the non-tool `content_block_start` branch (forwarded in all
rounds); removed in the `content_block_stop` branches via an `indexOf`
guard (tool_use blocks are never pushed — they are buffered in `pending`
and emitted as a whole later).

`emitError(message)` now emits, in order:

1. synthetic `message_start` (if `!messageStartForwarded`) —
`buildSyntheticMessageStart()`: the captured `messageId` if any, else a
generated `msg_acp_error_<ts>` id; empty `content`, null stop reason,
zero usage, the request model if known. Zero usage is honest: nothing
completed.
2. `content_block_stop` for every index in `openBlocks` (drained);
3. the existing error text block (`[acp-proxy: <message>]`);
4. the existing terminal (`message_delta` + `message_stop`).

Result: the stream is always well-formed — starting frame first, every block
closed, terminal last — regardless of where the truncation hit (before
`message_start`, mid-thinking-block, mid-text-block, or after everything).

On the retry path the adapter state is all-initial at re-parse time
(`messageId` undefined, `clientIndex` 0, flag false, no open blocks), so the
re-parsed stream is shaped exactly like a fresh first parse — consistent.

## Logging

The truncation path keeps `ctx.log` (session-prefixed `info` line, which is
what session-level dashboards key on) and drops the global
`loggerLog("error", ...)` duplicate. One event, one line. (The upstream-HTTP
failure path on re-request still double-logs — same pattern, out of scope,
reported in the PR.)

## What is NOT retried (and why)

- Anything that forwarded a single byte to the client (retry would emit a
duplicate/partial stream the client cannot reconcile);
- Any round that collected a tool call or ran a proxy tool (side effects);
- Text-protocol rounds that accumulated text (the post-loop `emitText` yield
would double it);
- Aborted requests;
- Second and later truncations within one request (one retry, per the issue).
57 changes: 57 additions & 0 deletions devlog/2026-09-02_acp-loop-truncation-retry/REQ.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
# REQ: acp-loop upstream truncation — zero-side-effect retry, well-formed Anthropic error stream, single-point logging

Issue #413: when the upstream LLM returns 200 but the SSE stream ends early
(no `message_delta`/`message_stop`), the compress loop (`src/loop/core.ts`)
fails fast with `emitError` and no recovery. Three concrete defects, all
confirmed by code read + red tests before the fix:

1. **No retry for zero-side-effect truncation.** `fetchWithRetry` only wraps
the re-request fetches (it inspects `response.ok` after headers and never
reads the body — mid-stream EOF is only observable at loop level via the
missing done event). A round that forwarded nothing to the client
(`0 text + 0 reasoning + 0 executed tools`, nothing yielded) is a
zero-side-effect retry candidate; the loop instead surfaced the upstream
flake to the host session. Production evidence: 3 truncation events in
3 days, two of them 0+0 (2026-08-31T00:35:24Z, 2026-08-31T06:03:43Z).
2. **Orphan Anthropic stream on first-round truncation.** The adapter's
`emitError` is stateless w.r.t. what was already forwarded: it emits
`content_block_start/delta/stop + message_delta + message_stop` directly.
If the stream truncated before `message_start` reached the client, the
client sees a `content_block_*` sequence with no starting frame —
malformed per the Anthropic stream protocol. Open `thinking` blocks left
by the truncation are never closed either. The responses branch already
fixed the same class (see `src/stream-error.ts` comment); the Anthropic
adapter was missed.
3. **Double logging.** Every truncation event was logged twice: `ctx.log`
(→ `log("info", "[sid] ...")`) plus a global `loggerLog("error", ...)` —
both tee to the same file + stderr.

## Acceptance criteria (from the issue)

- Stub upstream: headers then 0-event EOF → exactly 1 retry observed; after
the failed retry the client stream passes "SSE start frame before
content_block" schema validation.
- Truncation fixture with open blocks: every `content_block_start` has a
matching `content_block_stop` before stream end.
- Each single truncation produces exactly 1 log line.

## Constraints

- The retry must be provably side-effect-free: nothing forwarded to the
client, no tool calls executed, no usage recorded, no state mutation.
- At most ONE retry per request (the issue says "走一次").
- No new dependencies; `fetchWithRetry` reuse (same backoff/env knobs).
- The re-request path's behavior (degraded retry, #189 rejection logging)
is unchanged; only its `fetchUpstream` definition is hoisted.

## Non-goals (reported in the PR, not fixed)

- `adapter-responses.ts` `emitError`: a `response.failed` without a prior
`response.created` is the same orphan class if the stream truncated before
the created event.
- `core.ts` upstream-error path (HTTP failure on re-request) also double-logs
(`ctx.log` + `loggerLog("error")`) — same pattern; the issue only reported
the truncation one.
- Truncation with `reRequest = true` (truncated after a complete proxy tool
call, before the done event) is silently swallowed — the loop continues to
the next round. Arguably benign; unchanged.
58 changes: 58 additions & 0 deletions devlog/2026-09-02_acp-loop-truncation-retry/WORKLOG.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
# WORKLOG: acp-loop upstream truncation — zero-side-effect retry, well-formed Anthropic error stream, single-point logging

- Task ID: `2026-09-02_acp-loop-truncation-retry`
- Home Repo: `billion-context`
- Status: Done
- Updated: 2026-09-02

## 1. Summary

- **What was done**: (1) the compress loop now retries once when an upstream
stream truncates (200 + early EOF) after forwarding nothing to the client;
(2) the Anthropic adapter's `emitError` now synthesizes a `message_start`
if none was forwarded and closes every block still open at error time;
(3) the truncation event is logged once (via `ctx.log`) instead of twice.
- **Why**: zero-side-effect truncations are the most common recoverable
upstream flake (3 in 3 days in production, two of them 0+0); failing fast
lands the flake in the host session. The orphan stream shape violates the
Anthropic stream protocol and can crash strict clients (the responses
branch already fixed its own copy of this class). Double logging makes
incident triage count events wrong.
- **Behavior / compatibility changes**: Yes, three —
1. A first-round (or any-round) truncation that forwarded nothing and
executed no tool calls now triggers exactly one retry through
`fetchWithRetry` before the error is surfaced. Clients see either a
clean successful stream or the error stream — never a partial one.
2. The Anthropic error stream is now always well-formed: starts with
`message_start`, every `content_block_start` has a matching
`content_block_stop`, ends with `message_delta` + `message_stop`.
3. Truncation is logged once at `info` (session-prefixed) instead of twice
(`info` + `error`).
- **Risk level**: Low. The retry is gated on a strict zero-side-effect
predicate (nothing forwarded, no tool calls, nothing accumulated for the
host, not aborted, not already retried); the adapter change only adds
frames that strict clients require; logging change is cosmetic.

## 2. Change Log

### Commits

| Commit | Description |
|--------|-------------|
| `<sha>` | fix: acp-loop zero-side-effect truncation retry + well-formed anthropic error stream (#413) |

### Key Files

- `src/loop/core.ts` — retry loop around the per-round parse; hoisted
`fetchUpstream`; dropped the second (global error) log line on truncation;
`roundBody` tracks the last body actually sent so a retry re-sends the
compressed round body, not the original request.
- `src/loop/adapter-anthropic.ts` — `messageStartForwarded` / `openBlocks`
tracking in the adapter closure; `buildSyntheticMessageStart()`;
`emitError` closes open blocks and backfills the starting frame.
- `tests/loop-truncation.test.ts` — new; 6 tests covering all three
acceptance criteria plus retry-body tracking and the once-per-request cap.

## 3. Design & Implementation Notes

See `DESIGN.md` for the dataflow and the safety invariants.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "billion-context",
"version": "0.1.71",
"version": "0.1.74",
"description": "Universal context-compression proxy for AI coding agents. Sits between any agent and its model API, rewriting Anthropic/OpenAI streams with acp-kernel compression. Any agent that can set a base URL (Claude Code, Codex, Cursor, Aider) works out of the box.",
"type": "module",
"main": "dist/index.js",
Expand Down
16 changes: 16 additions & 0 deletions src/agent/pi.ts
Original file line number Diff line number Diff line change
Expand Up @@ -304,6 +304,22 @@ export function createBiliPlugin(agentOverride?: string, opts?: { retryIntervalM
state.sid = undefined;
void registerTools(pi, ctx, state, agent).catch((err: unknown) => console.error(`bili-plugin(${agent}): ${err instanceof Error ? err.message : String(err)}`));
});
// omp fires session_compact on in-session native compaction (sid does
// not rotate), so the proxy reuses stale state — notify it to archive
// the now-unreachable blocks (#395). Fire-and-forget: a failed
// notification must never break the agent's compaction.
pi.on("session_compact", (_event, ctx) => {
const proxyBase = proxyBaseForCtx(ctx);
if (proxyBase === undefined) return;
const sid = sessionIdOf(ctx);
if (sid === undefined || sid.length === 0) return;
fetch(`${proxyBase}/__bili/plugin/compact`, {
method: "POST",
headers: { "content-type": "application/json" },
body: JSON.stringify({ conversationId: sid }),
signal: AbortSignal.timeout(5000),
}).catch(() => {});
});
};
}

Expand Down
6 changes: 5 additions & 1 deletion src/decompress-shared.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import {
import { mkdirSync, unlinkSync, writeFileSync } from "node:fs";
import { dirname, join } from "node:path";
import { tmpdir } from "node:os";
import type { Session } from "./session.js";
import { preCompactionArchiveOf, type Session } from "./session.js";

/** Bounded retention for large-decompress temp files. Each decompress with
* body > 10000 writes one file under tmpdir(); the reaper unlinks oldest past
Expand Down Expand Up @@ -78,6 +78,10 @@ export function resolveDecompress(
const blockId = rawBlockId.trim();
const block = ctx.core.decompress(blockId, ctx.session.state);
if (!block) return `[Block ${blockId} not found]`;
const archived = preCompactionArchiveOf(ctx.session);
if (archived[blockId] !== undefined) {
return `[decompress FAILED: block ${blockId} is a pre-compaction archive — its content was in the history BEFORE the client's native compaction and is no longer reachable (replaced by the client's compaction summary). decompress is unavailable for archived blocks.]`;
}

const full = args.full === true;
const cached = ctx.session.blockContents.get(blockId);
Expand Down
Loading
Loading