diff --git a/PLUGIN.md b/PLUGIN.md index 8eeae76a96..8faf25363b 100644 --- a/PLUGIN.md +++ b/PLUGIN.md @@ -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:` 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. diff --git a/devlog/2026-09-02_acp-loop-truncation-retry/DESIGN.md b/devlog/2026-09-02_acp-loop-truncation-retry/DESIGN.md new file mode 100644 index 0000000000..990ead4dc0 --- /dev/null +++ b/devlog/2026-09-02_acp-loop-truncation-retry/DESIGN.md @@ -0,0 +1,109 @@ +# DESIGN: acp-loop truncation retry + well-formed Anthropic error stream + +## Problem shape + +`runCompressLoop` is an `AsyncGenerator`: 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_` 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: ]`); +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). diff --git a/devlog/2026-09-02_acp-loop-truncation-retry/REQ.md b/devlog/2026-09-02_acp-loop-truncation-retry/REQ.md new file mode 100644 index 0000000000..1a6ebf9406 --- /dev/null +++ b/devlog/2026-09-02_acp-loop-truncation-retry/REQ.md @@ -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. diff --git a/devlog/2026-09-02_acp-loop-truncation-retry/WORKLOG.md b/devlog/2026-09-02_acp-loop-truncation-retry/WORKLOG.md new file mode 100644 index 0000000000..bd41022ada --- /dev/null +++ b/devlog/2026-09-02_acp-loop-truncation-retry/WORKLOG.md @@ -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 | +|--------|-------------| +| `` | 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. diff --git a/package.json b/package.json index aac35ef2bc..343a83ea38 100644 --- a/package.json +++ b/package.json @@ -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", diff --git a/src/agent/pi.ts b/src/agent/pi.ts index 967f844ffe..e5fca5f17c 100644 --- a/src/agent/pi.ts +++ b/src/agent/pi.ts @@ -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(() => {}); + }); }; } diff --git a/src/decompress-shared.ts b/src/decompress-shared.ts index e95db1165e..3a91801355 100644 --- a/src/decompress-shared.ts +++ b/src/decompress-shared.ts @@ -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 @@ -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); diff --git a/src/fake-completion.ts b/src/fake-completion.ts new file mode 100644 index 0000000000..db30b6aba5 --- /dev/null +++ b/src/fake-completion.ts @@ -0,0 +1,124 @@ +import type { WireProtocol } from "./util.js"; +import { containsToolCallXmlFragment } from "./loop/tag-echo-filter.js"; + +// #371 (root-cause follow-up to #361): a small model can WRITE a tool call as +// plain text (echoing the tool-call XML template from the context) instead of +// emitting a real tool block. With no tool block the agent ends the turn early +// — a "fake completion": the tool never ran but the client shows success. The +// proxy detects this shape (tool-call structure present + no real tool block) +// and retries once with a corrective hint, bounded per turn and per session. + +export const FAKE_COMPLETION_HINT = + "[billion-context] Your last reply described a tool call as plain text instead of invoking it, so no tool actually ran. " + + "To act, invoke the tool through the proper tool-calling mechanism (emit a tool_use / tool_calls / function_call block). " + + "Do not write tool-call markup as text in your reply."; + +// One value serves two bounds: max retries within a single request, and the +// consecutive-fake-completion-turn cap after which a session stops retrying. +// +// DISABLED BY DEFAULT (0): the fallback must buffer the whole response before +// the client sees it (a fake completion is only knowable at end-of-stream), +// which defeats incremental streaming. That cost is only worth paying for the +// low-frequency fake-completion case (small models via gateways), so it is +// opt-in: set BILI_FAKE_COMPLETION_RETRIES=2 to enable. 0 = pre-#371 passthrough. +export function maxFakeCompletionRetries(): number { + const n = Number.parseInt(process.env.BILI_FAKE_COMPLETION_RETRIES ?? "0", 10); + return Number.isFinite(n) && n >= 0 ? n : 0; +} + +// OOM guard for a pathological upstream; LLM responses are bounded by max_tokens +// and normally well under 1 MiB. +export function fakeBufCap(): number { + const n = Number.parseInt(process.env.BILI_FAKE_BUF_CAP ?? String(16 * 1024 * 1024), 10); + return Number.isFinite(n) && n > 0 ? n : 16 * 1024 * 1024; +} + +// "Does this raw response (full SSE stream or JSON body) carry a REAL tool +// block?" A real block means the model actually invoked a tool → not a fake +// completion. Each protocol marks its tool block differently on the wire. +const ANTHROPIC_TOOL_BLOCK = /"type"\s*:\s*"tool_use"/; +const OPENAI_TOOL_BLOCK = /"tool_calls"\s*:\s*\[\s*\{/; +const RESPONSES_TOOL_BLOCK = /"type"\s*:\s*"function_call"/; + +export function hasToolBlock(protocol: WireProtocol, rawText: string): boolean { + switch (protocol) { + case "anthropic": + return ANTHROPIC_TOOL_BLOCK.test(rawText); + case "openai": + return OPENAI_TOOL_BLOCK.test(rawText); + case "responses": + return RESPONSES_TOOL_BLOCK.test(rawText); + } +} + +// Opening tool tag (no leading '/'): the model started writing a call as text — +// the strongest signal. Literal '<' or JSON-escaped '\u003c'; optional antml: +// namespace; case-insensitive, matching containsToolCallXmlFragment. +const TOOL_OPEN = + /\x3c(?!\s*\/)(?:antml:)?(invoke|tool_calls|tool_call|parameter|parameters)\b|\\u003c(?!\s*\/)(?:antml:)?(invoke|tool_calls|tool_call|parameter|parameters)\b/i; + +// Closing tool tags (global): an echoed template tail carries 2+ distinct +// closers ( + ) even when the openers were folded away +// earlier in the context (the #361 shape). +const TOOL_CLOSE_GLOBAL = + /\x3c\/(?:antml:)?(invoke|tool_calls|tool_call|parameter|parameters)\b|\\u003c\/(?:antml:)?(invoke|tool_calls|tool_call|parameter|parameters)\b/gi; + +// Structural guard on top of containsToolCallXmlFragment: require an opening +// tool tag OR 2+ distinct closing tags. A LONE closing tag in prose (a model +// discussing tool-call code) does not qualify — that is the false positive the +// plain fragment detector would otherwise trigger on. +export function hasToolCallStructure(text: string): boolean { + if (!containsToolCallXmlFragment(text)) return false; + if (TOOL_OPEN.test(text)) return true; + const closes = new Set(); + for (const m of text.matchAll(TOOL_CLOSE_GLOBAL)) { + const name = m[1] ?? m[2]; + if (name) closes.add(name.toLowerCase()); + } + return closes.size >= 2; +} + +export function isFakeCompletion(protocol: WireProtocol, rawText: string): boolean { + return hasToolCallStructure(rawText) && !hasToolBlock(protocol, rawText); +} + +// Appends the hint as a trailing user message, merged into the last user +// message when present (avoids back-to-back user turns on strict providers). +// Returns the hinted JSON, or null if the body is unparseable (skip the retry). +export function injectFakeCompletionHint(protocol: WireProtocol, body: string | Buffer): string | null { + const raw = typeof body === "string" ? body : body.toString("utf8"); + let obj: Record; + try { + obj = JSON.parse(raw) as Record; + } catch { + return null; + } + if (protocol === "responses") { + const input = obj.input; + if (!Array.isArray(input)) return null; + const arr = input as Record[]; + const last = arr[arr.length - 1]; + if (last && typeof last === "object" && last.role === "user") { + const c = last.content; + if (typeof c === "string") last.content = `${c}\n\n${FAKE_COMPLETION_HINT}`; + else if (Array.isArray(c)) last.content = [...c, { type: "input_text", text: FAKE_COMPLETION_HINT }]; + else last.content = FAKE_COMPLETION_HINT; + } else { + arr.push({ role: "user", content: [{ type: "input_text", text: FAKE_COMPLETION_HINT }] }); + } + return JSON.stringify(obj); + } + const messages = obj.messages; + if (!Array.isArray(messages)) return null; + const arr = messages as Record[]; + const last = arr[arr.length - 1]; + if (last && typeof last === "object" && last.role === "user") { + const c = last.content; + if (typeof c === "string") last.content = `${c}\n\n${FAKE_COMPLETION_HINT}`; + else if (Array.isArray(c)) last.content = [...c, { type: "text", text: FAKE_COMPLETION_HINT }]; + else last.content = FAKE_COMPLETION_HINT; + } else { + arr.push({ role: "user", content: FAKE_COMPLETION_HINT }); + } + return JSON.stringify(obj); +} diff --git a/src/launcher.ts b/src/launcher.ts index 9ecca8b132..f2c2f98098 100644 --- a/src/launcher.ts +++ b/src/launcher.ts @@ -649,29 +649,36 @@ function livePidHoldsOverlay(overlay: string): number | undefined { } /** - * Link one real-home entry into the overlay: plain symlink first (the Linux/ - * macOS path, and the Windows path when Developer Mode grants - * SeCreateSymbolicLinkPrivilege). On EPERM/EACCES/EINVAL — the default for an - * unprivileged Windows process, #381 — fall back to privilege-free links so - * the overlay is populated instead of silently hollow: directories → junction - * (mklink /J), files → hardlink (same volume, write-through) → copy. + * Link one real-home entry into the overlay. The reparse-point kind is chosen + * explicitly from the target's type: on Windows a directory must be a junction + * (privilege-free, unambiguous) — libuv's type-omitted default creates a + * file-tag symlink for dirs that Win32 readdir(withFileTypes) and some + * backup/indexers don't follow (#381 review) — while a file is a 'file' + * symlink (needs SeCreateSymbolicLinkPrivilege). Non-Windows ignores the type, + * so "dir"/"file" are just plain symlinks there. On EPERM/EACCES/EINVAL — the + * default for an unprivileged Windows process, #381 — a file falls back to a + * privilege-free hardlink (same volume, write-through) → copy; a directory + * retries the junction. */ function linkOverlayEntry(realHome: string, overlay: string, entry: string): boolean { const target = path.join(realHome, entry); const link = path.join(overlay, entry); - try { - fs.symlinkSync(target, link); - return true; - } catch (err) { - const code = (err as NodeJS.ErrnoException).code; - if (code !== "EPERM" && code !== "EACCES" && code !== "EINVAL") return false; - } let st: fs.Stats; try { st = fs.lstatSync(target); } catch { return false; } + const kind: "dir" | "file" | "junction" = st.isDirectory() + ? (process.platform === "win32" ? "junction" : "dir") + : "file"; + try { + fs.symlinkSync(target, link, kind); + return true; + } catch (err) { + const code = (err as NodeJS.ErrnoException).code; + if (code !== "EPERM" && code !== "EACCES" && code !== "EINVAL") return false; + } if (st.isDirectory()) { try { fs.symlinkSync(target, link, "junction"); @@ -706,26 +713,61 @@ function isWriteThroughHardlink(overlayPath: string, realPath: string, st: fs.St } } -/** SQLite three-piece set for a main-db name. A WAL is only valid against its - * exact main db, so the set must move as a unit — splitting it corrupts the +/** SQLite set for a main-db name: the db plus its WAL, shared-memory, and + * rollback-journal sidecars. A WAL/journal is only valid against its exact + * main db, so the set must move as a unit — splitting it corrupts the * database (#381). */ function sqliteSetMembers(base: string): string[] { - return [base, `${base}-wal`, `${base}-shm`]; + return [base, `${base}-wal`, `${base}-shm`, `${base}-journal`]; +} + +/** A `.bili-conflict` target that does not already exist, so a retry + * round never silently overwrites a previous round's preserved loser (#381 + * review): renameSync clobbers an existing target, so append `.1`, `.2`, … + * until the name is free. */ +function freeConflictName(dst: string): string { + let candidate = `${dst}.bili-conflict`; + let n = 1; + while (n < 100000) { + try { + fs.lstatSync(candidate); + candidate = `${dst}.bili-conflict.${n}`; + n += 1; + } catch { + return candidate; + } + } + return candidate; } /** Move a SQLite set (see sqliteSetMembers) from overlay to real home as one - * unit (#381): all present members move together, and if any rename fails - * (real db open/locked on Windows) the moved ones roll back and the set stays - * for the next launch. Per member mtime-newer wins (loser → .bili-conflict). */ + * unit (#381). The authoritative generation is decided ONCE by the main db's + * mtime — a WAL/journal is only valid against its exact main db, so the whole + * set must come from a single side: per-member mtime adjudication could splice + * a newer main db with a newer WAL from the other side and corrupt the + * database. The winner's members become the real home's active set; every + * losing member is preserved as `.bili-conflict` (never overwritten). A + * set with no main db on either side (orphan sidecars) is stale residue and is + * preserved wholesale as conflicts, never moved in as an active db. If any + * rename fails (real db open/locked on Windows) the moved ones roll back and + * the set stays for the next launch. */ function mergeSqliteSet(overlay: string, realHome: string, base: string): boolean { - const members = sqliteSetMembers(base).filter((m) => { + const members = sqliteSetMembers(base); + const statFile = (dir: string, m: string): fs.Stats | undefined => { try { - return fs.lstatSync(path.join(overlay, m)).isFile(); + const st = fs.lstatSync(path.join(dir, m)); + return st.isFile() ? st : undefined; } catch { - return false; + return undefined; } - }); - if (members.length === 0) return true; + }; + const oMain = statFile(overlay, base); + const rMain = statFile(realHome, base); + let winner: "overlay" | "real" | "orphan"; + if (oMain && rMain) winner = rMain.mtimeMs >= oMain.mtimeMs ? "real" : "overlay"; + else if (oMain) winner = "overlay"; + else if (rMain) winner = "real"; + else winner = "orphan"; const undo: (() => void)[] = []; const rollback = (): void => { for (const step of undo.reverse()) { @@ -735,45 +777,38 @@ function mergeSqliteSet(overlay: string, realHome: string, base: string): boolea } undo.length = 0; }; + const movePreserving = (src: string, dst: string): void => { + let dstStat: fs.Stats | undefined; + try { + dstStat = fs.lstatSync(dst); + } catch {} + if (dstStat) { + if (dstStat.isDirectory()) throw new Error("target is a directory"); + const conflict = freeConflictName(dst); + fs.renameSync(dst, conflict); + undo.push(() => fs.renameSync(conflict, dst)); + } + fs.renameSync(src, dst); + undo.push(() => fs.renameSync(dst, src)); + }; + const preserveAsConflict = (src: string, name: string): void => { + const conflict = freeConflictName(path.join(realHome, name)); + fs.renameSync(src, conflict); + undo.push(() => fs.renameSync(conflict, src)); + }; try { for (const m of members) { - const src = path.join(overlay, m); - const dst = path.join(realHome, m); - let dstStat: fs.Stats | undefined; - try { - dstStat = fs.lstatSync(dst); - } catch {} - if (dstStat && dstStat.isDirectory()) { - rollback(); - return false; - } - const srcStat = fs.lstatSync(src); - if (dstStat && dstStat.mtimeMs >= srcStat.mtimeMs) { - try { - fs.renameSync(src, `${dst}.bili-conflict`); - } catch { - rollback(); - return false; - } - undo.push(() => fs.renameSync(`${dst}.bili-conflict`, src)); - continue; - } - if (dstStat) { - try { - fs.renameSync(dst, `${dst}.bili-conflict`); - } catch { - rollback(); - return false; - } - undo.push(() => fs.renameSync(`${dst}.bili-conflict`, dst)); - } - try { - fs.renameSync(src, dst); - } catch { - rollback(); - return false; + const o = statFile(overlay, m); + const r = statFile(realHome, m); + if (winner === "overlay") { + if (o) movePreserving(path.join(overlay, m), path.join(realHome, m)); + else if (r) preserveAsConflict(path.join(realHome, m), m); + } else if (winner === "real") { + if (o) preserveAsConflict(path.join(overlay, m), m); + } else { + if (o) preserveAsConflict(path.join(overlay, m), m); + else if (r) preserveAsConflict(path.join(realHome, m), m); } - undo.push(() => fs.renameSync(dst, src)); } return true; } catch { @@ -782,7 +817,10 @@ function mergeSqliteSet(overlay: string, realHome: string, base: string): boolea } } -function refreshOverlayHome(realHome: string, overlay: string, generatedFile: string): boolean { +function refreshOverlayHome(realHome: string, overlay: string, generatedFile: string | string[]): boolean { + const generatedFiles = new Set(Array.isArray(generatedFile) ? generatedFile : [generatedFile]); + const isGeneratedDraft = (name: string): boolean => + [...generatedFiles].some((g) => name.startsWith(`.${g}.`) && name.endsWith(".tmp")); try { fs.mkdirSync(overlay, { recursive: true }); } catch { @@ -814,7 +852,7 @@ function refreshOverlayHome(realHome: string, overlay: string, generatedFile: st // re-points the db; any other set moves wholesale. const dbSets: { base: string; keepSidecars: boolean }[] = []; for (const entry of overlayEntries) { - if (!entry.endsWith(".db") || entry === generatedFile) continue; + if (!entry.endsWith(".db") || generatedFiles.has(entry)) continue; const members = sqliteSetMembers(entry); if (!members.some((m) => m !== entry && overlayEntries.includes(m))) continue; let mainSt: fs.Stats | undefined; @@ -832,9 +870,9 @@ function refreshOverlayHome(realHome: string, overlay: string, generatedFile: st } } for (const entry of overlayEntries) { - if (entry === generatedFile) continue; + if (generatedFiles.has(entry)) continue; const overlayPath = path.join(overlay, entry); - if (entry.startsWith(`.${generatedFile}.`) && entry.endsWith(".tmp")) { + if (isGeneratedDraft(entry)) { try { fs.unlinkSync(overlayPath); } catch {} @@ -864,7 +902,7 @@ function refreshOverlayHome(realHome: string, overlay: string, generatedFile: st try { fs.unlinkSync(overlayPath); } catch {} - } else if (mergeOverlayEntry(overlayPath, realPath, generatedFile)) { + } else if (mergeOverlayEntry(overlayPath, realPath, generatedFiles)) { try { fs.rmSync(overlayPath, { recursive: true, force: true }); } catch {} @@ -886,7 +924,7 @@ function refreshOverlayHome(realHome: string, overlay: string, generatedFile: st let total = 0; const linkFailures: string[] = []; for (const entry of realEntries) { - if (entry === generatedFile) continue; + if (generatedFiles.has(entry)) continue; total += 1; const overlayPath = path.join(overlay, entry); let present = false; @@ -925,7 +963,7 @@ function refreshOverlayHome(realHome: string, overlay: string, generatedFile: st return true; } -function mergeOverlayEntry(src: string, dst: string, excludedName?: string): boolean { +function mergeOverlayEntry(src: string, dst: string, excludedNames?: ReadonlySet): boolean { let st: fs.Stats; try { st = fs.lstatSync(src); @@ -939,7 +977,7 @@ function mergeOverlayEntry(src: string, dst: string, excludedName?: string): boo if (st.isDirectory()) { if (dstStat && !dstStat.isDirectory()) { try { - fs.renameSync(dst, `${dst}.bili-conflict`); + fs.renameSync(dst, freeConflictName(dst)); dstStat = undefined; } catch { return false; @@ -961,14 +999,14 @@ function mergeOverlayEntry(src: string, dst: string, excludedName?: string): boo // #410: generated configs must never merge back into the real // home, at ANY depth — a nested promote bakes proxy URLs into // the user's real config. - if (excludedName !== undefined && entry === excludedName) continue; - if (!mergeOverlayEntry(path.join(src, entry), path.join(dst, entry), excludedName)) ok = false; + if (excludedNames?.has(entry)) continue; + if (!mergeOverlayEntry(path.join(src, entry), path.join(dst, entry), excludedNames)) ok = false; } return ok; } if (dstStat && dstStat.isDirectory()) { try { - fs.renameSync(src, `${dst}.bili-conflict`); + fs.renameSync(src, freeConflictName(dst)); return true; } catch { return false; @@ -990,7 +1028,7 @@ function mergeOverlayEntry(src: string, dst: string, excludedName?: string): boo } if (dstStat && dstStat.mtimeMs >= st.mtimeMs) { try { - fs.renameSync(src, `${dst}.bili-conflict`); + fs.renameSync(src, freeConflictName(dst)); return true; } catch { return false; @@ -998,7 +1036,7 @@ function mergeOverlayEntry(src: string, dst: string, excludedName?: string): boo } if (dstStat) { try { - fs.renameSync(dst, `${dst}.bili-conflict`); + fs.renameSync(dst, freeConflictName(dst)); } catch { return false; } @@ -1038,50 +1076,69 @@ function writeOverlayFileAtomic(overlay: string, fileName: string, contents: str } } +function writePiCompactionDisabledSettings(piHome: string, overlay: string): void { + let settings: Record = {}; + try { + const parsed: unknown = JSON.parse(fs.readFileSync(path.join(piHome, "settings.json"), "utf8")); + if (parsed && typeof parsed === "object" && !Array.isArray(parsed)) { + settings = parsed as Record; + } + } catch {} + const compactionRaw = settings.compaction; + const compaction = + compactionRaw && typeof compactionRaw === "object" && !Array.isArray(compactionRaw) + ? { ...(compactionRaw as Record) } + : {}; + compaction.enabled = false; + settings.compaction = compaction; + writeOverlayFileAtomic(overlay, "settings.json", JSON.stringify(settings, null, 2)); +} + export function preparePiHttpRewrite( piHome: string, origin: string, httpRewrites: HttpRewrite[], httpsRewrites: HttpRewrite[], ): string | undefined { - if (httpRewrites.length === 0 && httpsRewrites.length === 0) return undefined; - const modelsPath = path.join(piHome, "models.json"); - let txt: string; - try { - txt = fs.readFileSync(modelsPath, "utf8"); - } catch { - return undefined; - } - let parsed: unknown; + if (!fs.existsSync(piHome)) return undefined; + const overlay = `${piHome}-bili`; + // #447: the overlay always carries a settings.json disabling pi's native + // auto-compaction (its summarizer must never fire alongside bili's ACP + // compression); models.json is generated only when present, so both stay + // out of the real-home symlink set. + const generated: string[] = ["settings.json"]; + let modelsRoot: Record | undefined; try { - parsed = JSON.parse(txt); - } catch { - return undefined; - } - if (!parsed || typeof parsed !== "object" || Array.isArray(parsed)) return undefined; - const root = parsed as Record; - const providersVal = root.providers; - if (providersVal && typeof providersVal === "object" && !Array.isArray(providersVal)) { - const providers = providersVal as Record; - for (const r of httpRewrites) { - const prov = providers[r.key]; - if (prov && typeof prov === "object" && !Array.isArray(prov)) { - const p = prov as { baseUrl?: unknown }; - const existing = typeof p.baseUrl === "string" ? p.baseUrl : r.realUpstream; - p.baseUrl = wrapUpstream(origin, unwrapUpstream(existing)); - } + const parsed: unknown = JSON.parse(fs.readFileSync(path.join(piHome, "models.json"), "utf8")); + if (parsed && typeof parsed === "object" && !Array.isArray(parsed)) { + modelsRoot = parsed as Record; + generated.push("models.json"); } - for (const r of httpsRewrites) { - const prov = providers[r.key]; - if (prov && typeof prov === "object" && !Array.isArray(prov)) { - const p = prov as { baseUrl?: unknown }; - p.baseUrl = r.realUpstream; + } catch {} + if (!refreshOverlayHome(piHome, overlay, generated)) return undefined; + if (modelsRoot) { + const providersVal = modelsRoot.providers; + if (providersVal && typeof providersVal === "object" && !Array.isArray(providersVal)) { + const providers = providersVal as Record; + for (const r of httpRewrites) { + const prov = providers[r.key]; + if (prov && typeof prov === "object" && !Array.isArray(prov)) { + const p = prov as { baseUrl?: unknown }; + const existing = typeof p.baseUrl === "string" ? p.baseUrl : r.realUpstream; + p.baseUrl = wrapUpstream(origin, unwrapUpstream(existing)); + } + } + for (const r of httpsRewrites) { + const prov = providers[r.key]; + if (prov && typeof prov === "object" && !Array.isArray(prov)) { + const p = prov as { baseUrl?: unknown }; + p.baseUrl = r.realUpstream; + } } } + writeOverlayFileAtomic(overlay, "models.json", JSON.stringify(modelsRoot)); } - const overlay = `${piHome}-bili`; - if (!refreshOverlayHome(piHome, overlay, "models.json")) return undefined; - writeOverlayFileAtomic(overlay, "models.json", JSON.stringify(root)); + writePiCompactionDisabledSettings(piHome, overlay); return overlay; } @@ -1842,7 +1899,7 @@ export async function runLaunch(params: RunLaunchParams, deps: LauncherDeps = {} // Native tooling out of the box: when the user has NOT installed the // plugin, ride pi's `-e ` (loads for this run only, writes // nothing) instead of leaving them on wire-mode fallback. When they - // HAVE installed it, settings.json (symlinked into the overlay home) + // HAVE installed it, settings.json (merged into the overlay home) // already loads it — adding `-e` too would double-register. const piExt = selfDistFile("agent/pi.js"); if (piExt && fs.existsSync(piExt) && !piPluginInstalled(resolvePiHome(process.env))) { diff --git a/src/loop/adapter-anthropic.ts b/src/loop/adapter-anthropic.ts index 175ca4502f..c986abf5c3 100644 --- a/src/loop/adapter-anthropic.ts +++ b/src/loop/adapter-anthropic.ts @@ -120,6 +120,35 @@ export function createAnthropicAdapter(requestBody: Record, ori const model = (requestBody.model as string) ?? undefined; let messageId: string | undefined; let clientIndex = 0; + let messageStartForwarded = false; + const openBlocks: number[] = []; + + const removeOpenBlock = (index: number): void => { + const i = openBlocks.indexOf(index); + if (i >= 0) openBlocks.splice(i, 1); + }; + + // #413: emitError must terminate a well-formed stream even when the + // upstream died before sending message_start — synthesize the start frame + // so content blocks are never orphaned (strict clients reject them). + const buildSyntheticMessageStart = (): Buffer => { + const extra: Record = {}; + if (model) extra.model = model; + const msg: Record = { + id: messageId ?? `msg_acp_error_${Date.now()}`, + type: "message", + role: "assistant", + content: [], + stop_reason: null, + stop_sequence: null, + usage: { input_tokens: 0, output_tokens: 0 }, + ...extra, + }; + return Buffer.from( + `event: message_start\ndata: ${JSON.stringify({ type: "message_start", message: msg })}\n\n`, + "utf8", + ); + }; const buildTextBlock = (index: number, text: string): Buffer => Buffer.from( @@ -206,6 +235,7 @@ export function createAnthropicAdapter(requestBody: Record, ori if (typeof u.input_tokens === "number") roundInput = u.input_tokens; if (typeof u.cache_read_input_tokens === "number") roundCached = u.cache_read_input_tokens; if (round === 1) { + messageStartForwarded = true; yield { kind: "meta", chunk: rawBuf, firstRoundOnly: true } as ParsedStreamEvent; } } else if (type === "ping") { @@ -221,6 +251,7 @@ export function createAnthropicAdapter(requestBody: Record, ori if (block.type === "thinking" || block.type === "redacted_thinking") thinkingIndexes.add(upstreamIndex); const ci = clientIndex++; indexMap.set(upstreamIndex, ci); + openBlocks.push(ci); yield { kind: "meta", chunk: remapIndexInEvent(eventStr, ci), firstRoundOnly: round === 1 } as ParsedStreamEvent; } } else if (type === "content_block_delta") { @@ -272,10 +303,12 @@ export function createAnthropicAdapter(requestBody: Record, ori // Seal the current thinking segment so interleaved thinking // blocks each keep their own signature on rebuild. const ci = indexMap.get(upstreamIndex) ?? upstreamIndex; + removeOpenBlock(ci); yield { kind: "meta", chunk: remapIndexInEvent(eventStr, ci), firstRoundOnly: false } as ParsedStreamEvent; yield { kind: "reasoning", delta: "", blockEnd: true } as ParsedStreamEvent; } else { const ci = indexMap.get(upstreamIndex) ?? upstreamIndex; + removeOpenBlock(ci); if (lastTextIndex !== null) { const tail = tagFilter.flush(); if (tail.length > 0) { @@ -385,8 +418,17 @@ export function createAnthropicAdapter(requestBody: Record, ori }, emitError(message) { - const errBlock = buildTextBlock(clientIndex++, `\n[acp-proxy: ${message}]\n`); - return Buffer.concat([errBlock, buildTerminal("end_turn", 0, 0, 0)]); + const parts: Buffer[] = []; + if (!messageStartForwarded) { + parts.push(buildSyntheticMessageStart()); + messageStartForwarded = true; + } + for (const index of openBlocks.splice(0)) { + parts.push(Buffer.from(`event: content_block_stop\ndata: ${JSON.stringify({ type: "content_block_stop", index })}\n\n`, "utf8")); + } + parts.push(buildTextBlock(clientIndex++, `\n[acp-proxy: ${message}]\n`)); + parts.push(buildTerminal("end_turn", 0, 0, 0)); + return Buffer.concat(parts); }, }; } diff --git a/src/loop/adapter-responses.ts b/src/loop/adapter-responses.ts index fad8fa2b4d..c2f2a9865e 100644 --- a/src/loop/adapter-responses.ts +++ b/src/loop/adapter-responses.ts @@ -252,6 +252,19 @@ export function createResponsesAdapter(textProtocol?: boolean, projection?: Resp let responseObj: Record | null = null; let terminalRaw: Buffer | null = null; let terminalKind: string | null = null; + // #440: response.failed with no preceding response.created is an orphan that + // crashes strict clients (codex) — same class as the anthropic gap (#413). + let createdForwarded = false; + let createdRespId: string | null = null; + const ensureCreated = (): Buffer | null => { + if (createdForwarded) return null; + createdForwarded = true; + if (!createdRespId) createdRespId = `resp-proxy-${Date.now()}`; + return Buffer.from( + `event: response.created\ndata: ${JSON.stringify({ type: "response.created", response: { id: createdRespId, status: "in_progress", output: [] } })}\n\n`, + "utf8", + ); + }; return { buildRequest(coreMessages, systemPrompt, requestBody) { @@ -318,6 +331,11 @@ export function createResponsesAdapter(textProtocol?: boolean, projection?: Resp } if (type === "response.created" || type === "response.in_progress") { + if (type === "response.created") { + const resp = obj.response as Record | undefined; + if (resp && typeof resp.id === "string") createdRespId = resp.id; + if (round === 1) createdForwarded = true; + } yield { kind: "meta", chunk: rawBuf, firstRoundOnly: true } as ParsedStreamEvent; } else if (type === "response.output_item.added") { const item = obj.item as Record | undefined; @@ -457,7 +475,7 @@ export function createResponsesAdapter(textProtocol?: boolean, projection?: Resp } } if (!terminalKind) { - yield { kind: "done", finishReason: "failed" } as ParsedStreamEvent; + yield { kind: "done", finishReason: "failed", truncated: true } as ParsedStreamEvent; } }, @@ -484,15 +502,19 @@ export function createResponsesAdapter(textProtocol?: boolean, projection?: Resp return terminalRaw; } if (!responseObj && opts?.finishReason === "failed") { + const parts: Buffer[] = []; + const created = ensureCreated(); + if (created) parts.push(created); const failed = { - id: `resp-error-${Date.now()}`, + id: createdRespId ?? `resp-error-${Date.now()}`, status: "failed", error: { code: "server_error", message: "upstream returned no response" }, }; - return Buffer.from( + parts.push(Buffer.from( `event: response.failed\ndata: ${JSON.stringify({ type: "response.failed", response: failed })}\n\n`, "utf8", - ); + )); + return Buffer.concat(parts); } let resp = responseObj ? ({ ...responseObj } as Record) @@ -517,15 +539,19 @@ export function createResponsesAdapter(textProtocol?: boolean, projection?: Resp }, emitError(message) { + const parts: Buffer[] = []; + const created = ensureCreated(); + if (created) parts.push(created); const resp = { - id: `resp-error-${Date.now()}`, + id: createdRespId ?? `resp-error-${Date.now()}`, status: "failed", error: { code: "server_error", message }, }; - return Buffer.from( + parts.push(Buffer.from( `event: response.failed\ndata: ${JSON.stringify({ type: "response.failed", response: resp })}\n\n`, "utf8", - ); + )); + return Buffer.concat(parts); }, extractTextTriggers(text) { diff --git a/src/loop/core.ts b/src/loop/core.ts index 7f0e3c8d27..e0dafed800 100644 --- a/src/loop/core.ts +++ b/src/loop/core.ts @@ -8,7 +8,7 @@ import { type CoreMessage, type NudgeDecision, } from "acp-kernel"; -import { lastCompressSuffix, type Session } from "../session.js"; +import { lastCompressSuffix, preCompactionArchiveOf, type Session } from "../session.js"; import type { BiliMessage } from "acp-kernel/wire"; import { parseCompressInput, @@ -67,7 +67,7 @@ export type ParsedStreamEvent = | { kind: "reasoning"; delta: string; raw?: Buffer; signature?: string; blockEnd?: boolean } | { kind: "tool_call"; name: string; callId: string; arguments: string; passthrough?: boolean } | { kind: "usage"; inputTokens?: number; outputTokens?: number; cachedTokens?: number } - | { kind: "done"; finishReason?: string; suppressCompletion?: boolean } + | { kind: "done"; finishReason?: string; suppressCompletion?: boolean; truncated?: boolean } | { kind: "meta"; chunk: Buffer; firstRoundOnly?: boolean }; export interface EmitCompletionOpts { @@ -148,6 +148,8 @@ function handleAcpStatus(args: Record, ctx: LoopCtx): string { const nudge = ctx.nudge; const ranges = nudge?.compressibleRanges ?? []; const protectedRanges = nudge?.protectedRanges ?? []; + const archive = preCompactionArchiveOf(ctx.session); + const archivedIds = Object.keys(archive); const extra: string[] = []; if (nudge) { extra.push(""); @@ -157,6 +159,13 @@ function handleAcpStatus(args: Record, ctx: LoopCtx): string { extra.push(""); extra.push(formatRanges(ranges, protectedRanges)); } + if (archivedIds.length > 0) { + extra.push(""); + extra.push(`PRE-COMPACTION ARCHIVE — ${archivedIds.length} block(s): content was replaced by the client's native compaction summary, so it is no longer in the session history and decompress is unavailable.`); + for (const id of archivedIds) { + extra.push(` ${id} — ${archive[id].reason}`); + } + } return extra.length > 0 ? `${base}\n${extra.join("\n")}` : base; } @@ -206,8 +215,30 @@ export async function* runCompressLoop( ): AsyncGenerator { let activeClearTimer: (() => void) | null = null; let currentUpstream = upstream; + let roundBody: Record = requestBody; const coreMessages: CoreMessage[] = [...ctx.messages]; let degradedRetried = false; + let truncationRetried = false; + + const fetchUpstream = (body: Record) => + fetchWithRetry( + requestOptions.url, + { + method: "POST", + headers: requestOptions.headers, + body: JSON.stringify(body), + ...(ctx.proxyUrl ? { dispatcher: proxyDispatcher(ctx.proxyUrl) } : {}), + }, + undefined, + signal, + (info) => { + // #189: correlate the rejection with the rewrite that + // preceded it (shrink ratio + fold point), if any. + const lc = lastCompressSuffix(ctx.session.lastCompress); + ctx.log(`[acp-proxy: upstream rejected replay (HTTP ${info.status}: ${info.detail.slice(0, 120)}); likely provider risk-control — retrying in ${info.delayMs}ms (attempt ${info.attempt}/${info.maxAttempts})${lc}]`); + loggerLog("warn", `[acp-loop] upstream rejected replay (HTTP ${info.status}); retrying in ${info.delayMs}ms (attempt ${info.attempt}/${info.maxAttempts})${lc}`); + }, + ); try { for (let round = 1; round <= MAX_LOOP_ROUNDS; round++) { @@ -221,15 +252,34 @@ export async function* runCompressLoop( let finishReason: string | undefined; let sawDone = false; let suppressCompletion = false; - - for await (const ev of adapter.parseStream(currentUpstream, round)) { - if (signal?.aborted) break; + let truncatedDone = false; + let forwardedAny = false; + const fwd = (chunk: Buffer): Buffer => { + forwardedAny = true; + return chunk; + }; + + for (;;) { + assistantText = ""; + assistantReasoning = ""; + reasoningSegments.length = 0; + reasoningSealed = true; + calls.length = 0; + usage = {}; + finishReason = undefined; + sawDone = false; + suppressCompletion = false; + truncatedDone = false; + forwardedAny = false; + + for await (const ev of adapter.parseStream(currentUpstream, round)) { + if (signal?.aborted) break; if (ev.kind === "text") { assistantText += ev.delta; if (!ctx.textProtocol && ev.raw) { - yield ev.raw; + yield fwd(ev.raw); } else if (!ctx.textProtocol && round > 1 && ev.delta.length > 0) { - yield adapter.emitText(ev.delta); + yield fwd(adapter.emitText(ev.delta)); } } else if (ev.kind === "reasoning") { assistantReasoning += ev.delta; @@ -244,28 +294,69 @@ export async function* runCompressLoop( if (ev.blockEnd) reasoningSealed = true; if (!ctx.textProtocol) { if (ev.raw) { - yield ev.raw; + yield fwd(ev.raw); } else if (round > 1 && ev.delta.length > 0 && adapter.emitReasoning) { - yield adapter.emitReasoning(ev.delta); + yield fwd(adapter.emitReasoning(ev.delta)); } } } else if (ev.kind === "tool_call") { - calls.push({ name: ev.name, callId: ev.callId, arguments: ev.arguments, passthrough: ev.passthrough }); - } else if (ev.kind === "usage") { - usage = { - inputTokens: ev.inputTokens, - outputTokens: ev.outputTokens, - cachedTokens: ev.cachedTokens, - }; - } else if (ev.kind === "done") { - sawDone = true; - finishReason = ev.finishReason; - suppressCompletion = ev.suppressCompletion === true; - } else if (ev.kind === "meta") { - if (round === 1 || !ev.firstRoundOnly) { - yield ev.chunk; + calls.push({ name: ev.name, callId: ev.callId, arguments: ev.arguments, passthrough: ev.passthrough }); + } else if (ev.kind === "usage") { + usage = { + inputTokens: ev.inputTokens, + outputTokens: ev.outputTokens, + cachedTokens: ev.cachedTokens, + }; + } else if (ev.kind === "done") { + sawDone = true; + finishReason = ev.finishReason; + suppressCompletion = ev.suppressCompletion === true; + truncatedDone = ev.truncated === true; + } else if (ev.kind === "meta") { + if (round === 1 || !ev.firstRoundOnly) { + yield fwd(ev.chunk); + } + } + } + + // #413: zero-side-effect truncation — the client received + // nothing from this attempt (no text/reasoning/meta/tool + // bytes), so re-fetching the same round is invisible to it. + // One retry per request; 200+early-EOF flakiness (common on + // relays) no longer lands in the agent session. + // `truncatedDone` covers adapters that surface truncation as a + // synthetic failed done (responses) instead of ending with + // !sawDone (anthropic) — same retry, both shapes. + if ( + (!sawDone || truncatedDone) && + !forwardedAny && + calls.length === 0 && + !(ctx.textProtocol && assistantText.length > 0) && + !signal?.aborted && + !truncationRetried + ) { + truncationRetried = true; + ctx.log(`[acp-loop] round ${round}: upstream truncated before any content reached the client; retrying fetch once`); + try { + const respResult = await fetchUpstream(roundBody); + if (!respResult.response.body) { + respResult.clearTimer(); + throw new UpstreamHttpError(respResult.response.status, "(empty response body)", 1); + } + currentUpstream = respResult.response.body as ReadableStream; + if (activeClearTimer) activeClearTimer(); + activeClearTimer = respResult.clearTimer; + continue; + } catch (e) { + if (e instanceof UpstreamHttpError) { + const suffix = e.attempts > 1 ? ` after ${e.attempts} attempt(s)` : ""; + ctx.log(`[acp-loop] round ${round}: truncation retry failed (upstream error ${e.status}${suffix}: ${e.body.slice(0, 200)})`); + } else { + ctx.log(`[acp-loop] round ${round}: truncation retry failed (${e instanceof Error ? e.message : String(e)})`); + } } } + break; } if ( @@ -435,7 +526,6 @@ export async function* runCompressLoop( const partialReasoning = assistantReasoning.length; const msg = `upstream stream truncated (no completion event; round ${round}, ${partialText} text chars + ${partialReasoning} reasoning chars received)`; ctx.log(`[acp-loop] round ${round}: ${msg}`); - loggerLog("error", `[acp-loop] ${msg}`); yield adapter.emitError(msg); return; } @@ -461,26 +551,6 @@ export async function* runCompressLoop( if (signal?.aborted) break; - const fetchUpstream = (body: Record) => - fetchWithRetry( - requestOptions.url, - { - method: "POST", - headers: requestOptions.headers, - body: JSON.stringify(body), - ...(ctx.proxyUrl ? { dispatcher: proxyDispatcher(ctx.proxyUrl) } : {}), - }, - undefined, - signal, - (info) => { - // #189: correlate the rejection with the rewrite that - // preceded it (shrink ratio + fold point), if any. - const lc = lastCompressSuffix(ctx.session.lastCompress); - ctx.log(`[acp-proxy: upstream rejected replay (HTTP ${info.status}: ${info.detail.slice(0, 120)}); likely provider risk-control — retrying in ${info.delayMs}ms (attempt ${info.attempt}/${info.maxAttempts})${lc}]`); - loggerLog("warn", `[acp-loop] upstream rejected replay (HTTP ${info.status}); retrying in ${info.delayMs}ms (attempt ${info.attempt}/${info.maxAttempts})${lc}`); - }, - ); - let newBody = adapter.buildRequest(coreMessages, systemPrompt, requestBody); if (process.env.ACP_DUMP_BODY === "1") { try { @@ -530,6 +600,7 @@ export async function* runCompressLoop( } currentUpstream = respResult.response.body as ReadableStream; + roundBody = newBody; if (activeClearTimer) activeClearTimer(); activeClearTimer = respResult.clearTimer; } diff --git a/src/loop/tag-echo-filter.ts b/src/loop/tag-echo-filter.ts index 4f1510babb..cac2cb8186 100644 --- a/src/loop/tag-echo-filter.ts +++ b/src/loop/tag-echo-filter.ts @@ -56,6 +56,17 @@ export function containsRenderTagText(s: string): boolean { return RENDER_TAG_DETECT.test(s); } +// #468: some upstreams stream a model-imitated render tag in tokenizer-sized +// fragments ("\x3cac", "p tokens", ...) so no single chunk ever trips +// RENDER_TAG_DETECT. Per-chunk gates must also engage when the chunk contains +// or ends with the head of a render tag, so the streaming state machine can +// stitch it back together. Pure-prose chunks still skip the machine +// (byte-identical passthrough); only chunks with a `<`-head tail ("<", ", res: import("node:http").ServerResponse, - session: Session, protocol: WireProtocol, + session?: Session, log?: (msg: string) => void, ): Promise { const reader = stream.getReader(); @@ -604,6 +637,7 @@ export async function pipePluginChatWithStrip( } let rebuilt: Record | null = null; let droppedText = false; + let keptText = false; let hadText = false; for (let ci = 0; ci < choices.length; ci++) { const ch = choices[ci] as Record | null; @@ -614,10 +648,14 @@ export async function pipePluginChatWithStrip( const v = dd[field]; if (typeof v !== "string") continue; hadText = true; - if (!containsRenderTagText(v) && !anyPending()) continue; + if (!mayStartRenderTag(v) && !anyPending()) { + if (v.length > 0) keptText = true; + continue; + } const index = typeof ch?.["index"] === "number" ? ch["index"] : ci; const [clean, changed] = pushField(field, index, v); if (clean.length === 0) droppedText = true; + else keptText = true; if (changed) { if (!rebuilt) { rebuilt = { ...ev, choices: choices.map((c) => ({ ...(c as Record), delta: { ...((c as Record)["delta"] as Record) } })) }; @@ -627,9 +665,11 @@ export async function pipePluginChatWithStrip( } } if (rebuilt) { - // Delta carried nothing but the (now-stripped) text: drop the whole - // chunk instead of forwarding an empty content delta. - if (droppedText && !hadTextOtherThanTextFields(rebuilt["choices"])) { + // Delta carried no visible text after stripping: drop the whole + // chunk instead of forwarding an empty content delta. Only when + // EVERY managed text field emptied out — a sibling field with real + // content must survive (#463). + if (droppedText && !keptText && !hadTextOtherThanTextFields(rebuilt["choices"])) { return ""; } return rebuildEvent(rawEvent, rebuilt); @@ -648,7 +688,7 @@ export async function pipePluginChatWithStrip( return rawEvent + "\n\n"; } const raw = d[field] as string; - if (!containsRenderTagText(raw) && !anyPending()) { + if (!mayStartRenderTag(raw) && !anyPending()) { return rawEvent + "\n\n"; } const [clean, changed] = pushField(field, index, raw); @@ -691,7 +731,7 @@ export async function pipePluginChatWithStrip( } const rest = flushTails(); if (rest.length > 0 && !res.destroyed && !res.writableEnded) await write(rest); - if (acc.inputTokens !== undefined || acc.outputTokens !== undefined || acc.cachedTokens !== undefined) { + if (session && (acc.inputTokens !== undefined || acc.outputTokens !== undefined || acc.cachedTokens !== undefined)) { applyUsageSample(session, acc, protocol); markDirty(session); } @@ -727,11 +767,14 @@ function hadTextOtherThanTextFields(choices: unknown): boolean { * tag-echo state machine the compress loop uses. A held tail is flushed as a * final delta before the first done/completed event so the client's assembled * text never loses content. - */ + * + * Also serves proxy-mode Responses SSE that skipped compress injection + * (#460, e.g. ACP_NO_INJECT_TOOL). Pass no session there — see + * pipePluginChatWithStrip for why usage accounting must be skipped. */ export async function pipePluginResponsesWithStrip( stream: ReadableStream, res: import("node:http").ServerResponse, - session: Session, + session?: Session, log?: (msg: string) => void, ): Promise { const reader = stream.getReader(); @@ -802,7 +845,7 @@ export async function pipePluginResponsesWithStrip( await write(rawEvent + "\n\n"); continue; } - if (!containsRenderTagText(delta) && !tagFilter.pending()) { + if (!mayStartRenderTag(delta) && !tagFilter.pending()) { await write(rawEvent + "\n\n"); continue; } @@ -827,7 +870,7 @@ export async function pipePluginResponsesWithStrip( const rest = flushTail(""); if (rest.length > 0) await write(rest); } - if (acc.inputTokens !== undefined || acc.outputTokens !== undefined || acc.cachedTokens !== undefined) { + if (session && (acc.inputTokens !== undefined || acc.outputTokens !== undefined || acc.cachedTokens !== undefined)) { applyUsageSample(session, acc, "responses"); markDirty(session); } @@ -859,9 +902,11 @@ function rebuildEvent(rawEvent: string, ev: Record): string { export async function pipePluginJson( stream: ReadableStream, res: import("node:http").ServerResponse, - session: Session, + session?: Session, protocol?: WireProtocol, ): Promise { + // Also serves proxy-mode JSON responses that skipped compress injection + // (#460 residual) — pass no session there so usage accounting stays off. const reader = stream.getReader(); const chunks: Buffer[] = []; for (;;) { @@ -874,7 +919,7 @@ export async function pipePluginJson( try { const json = JSON.parse(text) as Record; const usage = json["usage"] as Record | undefined; - if (usage) { + if (session && usage) { const input = num(usage["prompt_tokens"]) ?? num(usage["input_tokens"]); if (input !== undefined) { applyUsageSample(session, { diff --git a/src/server.ts b/src/server.ts index afedc02d78..8ba2602df4 100644 --- a/src/server.ts +++ b/src/server.ts @@ -41,9 +41,9 @@ import { conversationSignalResponses, subagentNamespace, } from "acp-kernel/wire"; -import { getSession, listSessions, type Session, initSessions, markDirty, flushAllSessions, acquireInFlight, releaseInFlight, withSessionLock, markNativeCompactionBoundary, reconcileNativeCompactionBoundary, snapshotMessages } from "./session.js"; +import { getSession, listSessions, type Session, initSessions, markDirty, flushAllSessions, acquireInFlight, releaseInFlight, withSessionLock, markNativeCompactionBoundary, reconcileNativeCompactionBoundary, snapshotMessages, applyCompactionArchive } from "./session.js"; import { COMPRESS_TOOL, ACP_TOOLS_ANTHROPIC, ACP_TOOLS_OPENAI, ACP_TOOLS_RESPONSES, ACP_READONLY_TOOLS_RESPONSES, COMPRESS_TOOL_NAME, buildCompressSystemPrompt, buildCompressHybridSystemPrompt, withStagedCompressGuidance } from "./compress-tool.js"; -import { rewriteSseStream, rewriteJsonResponse, type RewriteCtx } from "./stream.js"; +import { rewriteJsonResponse, type RewriteCtx } from "./stream.js"; import { applyRanges } from "./stream.js"; import { preflightCompress, estimateCoreMessages } from "./preflight.js"; import { renderUI, handleConfigGet, handleConfigPut } from "./web/index.js"; @@ -55,6 +55,7 @@ import { atomicWriteInstanceFile, clearProxyInstanceFile, isPidAlive, registerIn import { compressLoopResponsesJson } from "./compress-loop-responses.js"; import { runCompressLoop, pickAdapter } from "./loop/index.js"; import { containsToolCallXmlFragment } from "./loop/tag-echo-filter.js"; +import { isFakeCompletion, injectFakeCompletionHint, maxFakeCompletionRetries, fakeBufCap } from "./fake-completion.js"; import { sanitizeResponsesInputIds, dropWhitespaceResponsesMessages, normalizeResponsesMessageItems } from "./loop/adapter-responses.js"; import { codexCompactMode, isCodexClient, hasCompactionTrigger, stripBiliCompactionItems, replaceBiliCompactionItems, codexCompactGate, buildTriggerForgeBody, mergeForgedSummaries } from "./codex-compact.js"; import { rewriteOpenaiJsonResponse } from "./stream-openai.js"; @@ -63,10 +64,10 @@ import { observeResponsesTerminalState } from "./stream-terminal.js"; import { emitStreamError } from "./stream-error.js"; import { affinityToken, clientConversationHeader, codexTurnIdentity, preferPromptCacheKeyIdentity, type ConversationIdentity } from "./session-id.js"; import { prefixAffinity, type AnonymousAffinity } from "./prefix-affinity.js"; -import { consumePluginRegisterFor, flushConversations, handlePluginManifest, handlePluginRegister, handlePluginStatus, handlePluginTool, loadConversations, pipePluginChatWithStrip, pipePluginJson, pipePluginResponsesWithStrip, pluginAgentHeader, pluginConversationHeader, pluginReportedContextWindow, recordPluginSession, rememberPluginMessages, takePendingPluginRegister } from "./plugin.js"; +import { consumePluginRegisterFor, flushConversations, handlePluginCompact, handlePluginManifest, handlePluginRegister, handlePluginStatus, handlePluginTool, loadConversations, pipePluginChatWithStrip, pipePluginJson, pipePluginResponsesWithStrip, pluginAgentHeader, pluginConversationHeader, pluginReportedContextWindow, recordPluginSession, rememberPluginMessages, takePendingPluginRegister } from "./plugin.js"; import { setupMitm, readMitmUpstream } from "./mitm.js"; import type { BiliMessage } from "acp-kernel/wire"; -import { systemToUser, isLoopbackAddress, inspectContextOverflow, reserveOutputHeadroom, shouldReserveOutputHeadroom, usageTotals } from "./util.js"; +import { systemToUser, isLoopbackAddress, inspectContextOverflow, reserveOutputHeadroom, shouldReserveOutputHeadroom, usageTotals, type WireProtocol } from "./util.js"; import { BILI_TUNNEL_HEADER, checkTunnelDestination, tunnelAllowlistFromEnv } from "./tunnel-guard.js"; import { decodeRequestBody } from "./content-encoding.js"; @@ -753,6 +754,17 @@ async function handle( return; } } + if (req.method === "POST" && req.url === "/__bili/plugin/compact") { + try { + const body = await readBody(req); + handlePluginCompact(body.toString("utf8"), res); + return; + } catch (err) { + res.writeHead(err instanceof BodyTooLargeError ? 413 : 400, { "content-type": "application/json" }); + res.end(JSON.stringify({ ok: false, error: String(err) })); + return; + } + } // Unknown /__bili/ or /__acp/ path → 404 locally. These are bili's own // management prefixes; forwarding would leak the internal path to the // upstream (which 403s it) — #346. @@ -1233,6 +1245,7 @@ async function handle( // otherwise the request is degenerate (output >= whole window) and the // self-heal above handles the resulting overflow. Feeds reqConfig (→ // processTurn `config`), so diagNudge shows the reserved window (no extra log). + const nativeWindow = reqConfig.modelContextLimit; if (shouldReserveOutputHeadroom(protocol)) { const p = parsed as Record; const rawMax = p.max_tokens ?? p.max_completion_tokens ?? p.max_output_tokens; @@ -1264,10 +1277,10 @@ async function handle( : protocol === "anthropic" ? prepareAnthropic(parsed as AnthropicRequestBody, req, opts, core, reqConfig, reqPrompts, log, session, pluginMode) : protocol === "openai" - ? prepareOpenai(parsed as OpenAIRequestBody, req, opts, core, reqConfig, reqPrompts, log, session, pluginMode) + ? prepareOpenai(parsed as OpenAIRequestBody, req, opts, core, reqConfig, reqPrompts, log, session, pluginMode, nativeWindow) : responsesCompact ? prepareResponsesCompact(bodyBuffer, parsed as ResponsesRequestBody, session, req, core, reqConfig, log) - : prepareResponses(parsed as ResponsesRequestBody, req, opts, core, reqConfig, reqPrompts, log, session, responsesIdentity!, pluginMode, upstreamOrigin); + : prepareResponses(parsed as ResponsesRequestBody, req, opts, core, reqConfig, reqPrompts, log, session, responsesIdentity!, pluginMode, upstreamOrigin, nativeWindow); prepared = runPrepare(); if (!countTokens && !responsesCompact) { const outcome = await preflightCompressIfNeeded( @@ -1382,7 +1395,9 @@ function diagNudge(turn: { nudge?: { shouldInject: boolean; reason: string; cont // "INJECT" only when the nudge actually reaches the upstream payload. When // armed but suppressed by config/mode, say so explicitly so the log never // lies about delivery (#451, same class as #413). - const inject = !n.shouldInject ? "idle" : willInject ? `INJECT T${n.tier ?? "?"}` : `ARMED-SUPPRESSED T${n.tier ?? "?"}`; + const inject = willInject + ? (n.shouldInject ? `INJECT T${n.tier ?? "?"}` : `INJECT-ESC T${n.tier ?? "?"}`) + : (n.shouldInject ? `ARMED-SUPPRESSED T${n.tier ?? "?"}` : "idle"); const modelTag = model ? ` model=${model}` : ""; return `[${sessionId}] nudge ${inject}: usage=${pct} (${tokenCount}/${limit}), growth=${growth}/${floor} (ref=${ref}, interval=${interval}), pendingT1=${pendingT1}/${interval}${modelTag}, reason="${n.reason.slice(0, 120)}"`; } @@ -1428,6 +1443,7 @@ function prepareAnthropic( // estimates to the kernel. extractSystem(parsed.system); const tokenCount = session.stats.lastInputTokens; + const activeBefore = new Set(session.state.blocks.filter((b) => b.active).map((b) => b.blockId)); const turn = core.processTurn({ messages: msgs, state: session.state, config, tokenCount, renderTags: "text-only" }); session.state = turn.state; // The fold from last turn's compress has now materialized in state — @@ -1444,9 +1460,10 @@ function prepareAnthropic( if (t) session.meta.title = t; } log("info", diagTagSummary(turn.messages, sessionId, "text-only")); - const willInjectNudge = opts.compress.injectNudge && !!turn.nudge?.shouldInject; + const willInjectNudge = opts.compress.injectNudge && !!turn.nudge && (turn.nudge.shouldInject || emergencyNudge(turn.nudge)); log("info", diagNudge(turn, sessionId, tokenCount, config.modelContextLimit, parsed.model, willInjectNudge)); processedMessages = stripKernelSummaries(turn.messages, turn.state); + applyCompactionArchive(session, activeBefore, new Set(msgs.map((m) => m.id)), log); reapOrphanBlocks(session, msgs, deactivateBlock); rebuiltMessages = coreToAnthropic(processedMessages as BiliMessage[], cacheControls); @@ -1485,6 +1502,70 @@ function prepareAnthropic( return { body: JSON.stringify(rebuilt), session, processedMessages, originalMessages, anthropicSystem: parsed.system, protocol: "anthropic", stream, compressInjected: injectTools, pluginMode, nudge, prompts, renderTags: "text-only" } as Prepared; } +// #453 hard backstop: cap the forwarded output budget so input+output can never +// exceed the window on request-rebuilding upstreams (vLLM rejects an oversized +// total instead of clamping). Non-Anthropic only — Anthropic enforces its input +// limit independently of max_tokens (see shouldReserveOutputHeadroom). The proxy +// owns both sides of the sum, so capping output to (window - input - margin) +// makes the overflow impossible even when the agent ignores the compress nudge. +const OUTPUT_CLAMP_MARGIN_PCT = 0.05; +const OUTPUT_CLAMP_MIN_MARGIN = 2048; +const OUTPUT_CLAMP_FLOOR = 1024; +// #453 mitigation: host-side escalation line. The kernel already force-injects +// every turn once usage >= nudge.maxContextLimitPct (its pressure branch has no +// cadence gate), so the only cadence-silent zone is BELOW that line. Force the +// nudge each turn once usage reaches this — kept under the default 0.75 +// over-limit line so it fills the pre-limit silent climb seen in #453/#14. Pure +// host-side: renderNudgeText does not depend on shouldInject. +const EMERGENCY_NUDGE_ESCALATION_PCT = 0.7; + +/** Conservative outbound-input estimate: the larger of the upstream-reported + * previous-turn input (real tokenizer count, already includes system+tools) and + * a fresh count of the rebuilt conversation text + system + tool definitions + * (needed on turn 1 / right after a shrink, when lastInputTokens lags). */ +export function estimateInputTokens(processedMessages: CoreMessage[], systemText: string | undefined, tools: unknown, lastInputTokens: number): number { + const est = estimateCoreMessages(processedMessages) + + defaultCountTokens(systemText ?? "") + + defaultCountTokens(JSON.stringify(tools ?? [])); + return Math.max(lastInputTokens > 0 ? lastInputTokens : 0, est); +} + +/** Output-budget cap so input+output <= window. Returns the clamped budget, or + * undefined when no reduction is needed (requested already fits, or the cap + * drops below OUTPUT_CLAMP_FLOOR — i.e. input alone nearly fills the window, + * which is preflight/self-heal territory, not output starvation). */ +export function clampOutputBudget(requested: number, inputEstimate: number, nativeWindow: number): number | undefined { + const margin = Math.max(OUTPUT_CLAMP_MIN_MARGIN, Math.ceil(inputEstimate * OUTPUT_CLAMP_MARGIN_PCT)); + const cap = nativeWindow - inputEstimate - margin; + if (cap < OUTPUT_CLAMP_FLOOR || cap >= requested) return undefined; + return cap; +} + +// Only override genuine cadence silences: skip the kernel's deliberate +// "nothing compressible to offer" suppression (empty ranges). +export function emergencyNudge(nudge: NudgeDecision | null | undefined, escalationPct: number = EMERGENCY_NUDGE_ESCALATION_PCT): boolean { + if (!nudge || nudge.shouldInject) return false; + if (nudge.compressibleRanges.length === 0) return false; + return nudge.contextUsage >= escalationPct; +} + +function clampOutgoingOutput( + rebuilt: Record, + field: "max_tokens" | "max_completion_tokens" | "max_output_tokens", + ctx: { systemText: string; tools: unknown; processedMessages: CoreMessage[]; lastInputTokens: number; nativeWindow: number }, + sessionId: string, + log: (level: string, msg: string) => void, +): void { + const raw = rebuilt[field]; + if (typeof raw !== "number") return; + const inputEstimate = estimateInputTokens(ctx.processedMessages, ctx.systemText, ctx.tools, ctx.lastInputTokens); + const capped = clampOutputBudget(raw, inputEstimate, ctx.nativeWindow); + if (capped !== undefined) { + rebuilt[field] = capped; + log("info", `[${sessionId}] output budget clamped ${raw} -> ${capped} (input~${inputEstimate}, window=${ctx.nativeWindow}); prevents input+output overflow (#453)`); + } +} + function prepareOpenai( parsed: OpenAIRequestBody, req: http.IncomingMessage, @@ -1495,6 +1576,7 @@ function prepareOpenai( log: (level: string, msg: string) => void, session: Session, pluginMode: boolean, + nativeWindow: number, ): Prepared { const sessionId = session.id; const stream = parsed.stream === true; @@ -1528,6 +1610,7 @@ function prepareOpenai( // tokenCount = upstream's real input_tokens from the previous turn // (see anthropic branch comment). Never an estimate. const tokenCount = session.stats.lastInputTokens; + const activeBefore = new Set(session.state.blocks.filter((b) => b.active).map((b) => b.blockId)); const turn = core.processTurn({ messages: msgs, state: session.state, config, tokenCount, renderTags: "text-only" }); session.state = turn.state; // The fold from last turn's compress has now materialized in state — @@ -1544,9 +1627,10 @@ function prepareOpenai( if (t) session.meta.title = t; } log("info", diagTagSummary(turn.messages, sessionId, "text-only")); - const willInjectNudge = opts.compress.injectNudge && !!turn.nudge?.shouldInject && shouldInject; + const willInjectNudge = opts.compress.injectNudge && !!turn.nudge && shouldInject && (turn.nudge.shouldInject || emergencyNudge(turn.nudge)); log("info", diagNudge(turn, sessionId, tokenCount, config.modelContextLimit, parsed.model, willInjectNudge)); processedMessages = stripKernelSummaries(turn.messages, turn.state); + applyCompactionArchive(session, activeBefore, new Set(msgs.map((m) => m.id)), log); reapOrphanBlocks(session, msgs, deactivateBlock); rebuiltMessages = systemToUser(coreToOpenai(processedMessages as BiliMessage[])); @@ -1584,6 +1668,7 @@ function prepareOpenai( } const rebuilt: OpenAIRequestBody = { ...parsed, messages: rebuiltMessages, tools: toolsOut as OpenAITool[] | undefined }; + clampOutgoingOutput(rebuilt as Record, typeof (parsed as Record).max_completion_tokens === "number" ? "max_completion_tokens" : "max_tokens", { systemText: openaiSystemText, tools: toolsOut, processedMessages, lastInputTokens: session.stats.lastInputTokens, nativeWindow }, sessionId, log); // prompt_cache_retention is an OpenAI-host-only cache directive; the dsh // launcher forces PI_CACHE_RETENTION=long (for the session-id // prompt_cache_key) which makes the client also emit it. Third-party @@ -1618,6 +1703,7 @@ function prepareResponses( identity: ConversationIdentity, pluginMode: boolean, upstreamOrigin: string, + nativeWindow: number, ): Prepared { const sessionId = session.id; const stream = parsed.stream === true; @@ -1704,7 +1790,7 @@ function prepareResponses( if (t) session.meta.title = t; } log("info", diagTagSummary(turn.messages, sessionId, "text-only")); - const willInjectNudge = opts.compress.injectNudge && !!turn.nudge?.shouldInject && shouldInject && !isCompactionTrigger; + const willInjectNudge = opts.compress.injectNudge && !!turn.nudge && shouldInject && !isCompactionTrigger && (turn.nudge.shouldInject || emergencyNudge(turn.nudge)); log("info", diagNudge(turn, sessionId, tokenCount, config.modelContextLimit, parsed.model, willInjectNudge)); processedMessages = stripKernelSummaries(turn.messages, turn.state); reapOrphanBlocks(session, msgs, deactivateBlock); @@ -1790,6 +1876,9 @@ function prepareResponses( } const rebuilt: ResponsesRequestBody = { ...parsed, input: rebuiltInput, tools: toolsOut }; + if (!isCompactionTrigger) { + clampOutgoingOutput(rebuilt as Record, "max_output_tokens", { systemText: (responsesProjection?.systemParts ?? []).join("\n"), tools: toolsOut, processedMessages, lastInputTokens: session.stats.lastInputTokens, nativeWindow }, sessionId, log); + } // Route with the upstream THIS request goes to — session.meta.upstreamOrigin // is first-wins and would keep injecting pck toward a relay we switched // away from (same class of bug as the compressProtocol fix above, #286). @@ -2561,7 +2650,7 @@ async function forward( if (prepared.protocol === "responses") { await pipePluginResponsesWithStrip(upstream.body as ReadableStream, res, prepared.session, (msg) => log("info", `[${prepared.session.id}] ${msg}`)); } else { - await pipePluginChatWithStrip(upstream.body as ReadableStream, res, prepared.session, prepared.protocol, (msg) => log("info", `[${prepared.session.id}] ${msg}`)); + await pipePluginChatWithStrip(upstream.body as ReadableStream, res, prepared.protocol, prepared.session, (msg) => log("info", `[${prepared.session.id}] ${msg}`)); } } else { await pipePluginJson(upstream.body as ReadableStream, res, prepared.session, prepared.protocol); @@ -2569,21 +2658,48 @@ async function forward( clearUpstreamTimer(); return; } + // #371: detect + retry a fake completion for every non-plugin streaming + // response (any turn, not just compress-injected). Buffering is required: + // the retry re-requests before the client sees the fake completion. + let responseBody: ReadableStream = upstream.body; + if (prepared !== null && prepared.stream && maxFakeCompletionRetries() > 0) { + const resolvedBuf = await resolveFakeCompletion(upstream.body, { + protocol: prepared.protocol, + body, + upstreamUrl, + reqHeaders: buildForwardHeaders(headers), + proxyUrl, + signal: clientAbort.signal, + session: prepared.session, + log, + }); + responseBody = bufferToStream(resolvedBuf); + } // We only rewrite when THIS request actually had the compress tool - // injected (per-request). For the OpenAI title-gen path - // (`compressInjected === false`) we must NOT route the stream into a - // rewriter — `rewriteSseStream` below is the *Anthropic* SSE rewriter - // and would mishandle OpenAI `choices[].delta` events. Plain passthrough - // is correct there. + // injected (per-request). Non-injected requests (OpenAI title-gen + // exclusion, ACP_NO_INJECT_TOOL, auto-mode classifier bypass) must NOT + // enter the compress loop — but their chat SSE still gets render-tag + // echo stripping (#460) below, so history-borne tags echoed in model + // prose cannot leak to the client and amplify via its replay. const useRewriter = prepared !== null && prepared.compressInjected && prepared.processedMessages.length > 0; if (!useRewriter || prepared === null) { if (prepared && prepared.resetAfterSuccess) { - const [toClient, toObserve] = upstream.body.tee(); + const [toClient, toObserve] = responseBody.tee(); const observed = observeResponsesTerminalState(toObserve, prepared.stream); - await pipeThrough(toClient, res); + const tagLog = (msg: string) => log("info", `[${prepared.session.id}] ${msg}`); + // #460 residual: a native compaction turn is by definition the + // compression-triggered one, so its context necessarily carries + // render tags — its echoed prose is the likeliest leak of any + // Responses stream. Same pipe as the non-injected branch below; + // no session, so usage accounting stays off. + if ((upstream.headers.get("content-type") ?? "").includes("text/event-stream")) { + await pipePluginResponsesWithStrip(toClient, res, undefined, tagLog); + } else { + await pipeThrough(toClient, res); + } clearUpstreamTimer(); const terminal = await observed; if (terminal === "completed") { @@ -2592,8 +2708,37 @@ async function forward( } else { log("warn", `[${prepared.session.id}] native compact response terminal=${terminal}; rebase NOT scheduled`); } + } else if ( + prepared && + prepared.stream && + (upstream.headers.get("content-type") ?? "").includes("text/event-stream") + ) { + // #460: same strip pipes as plugin mode; byte-identical for + // tag-free streams. No session is 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 (see pipePluginChatWithStrip docs). + const p = prepared; + const tagLog = (msg: string) => log("info", `[${p.session.id}] ${msg}`); + if (p.protocol === "responses") { + await pipePluginResponsesWithStrip(responseBody, res, undefined, tagLog); + } else { + await pipePluginChatWithStrip(responseBody, res, p.protocol, undefined, tagLog); + } + clearUpstreamTimer(); + } else if ( + prepared && + (upstream.headers.get("content-type") ?? "").includes("application/json") + ) { + // #460 residual: the non-streaming twin of the branch above. The + // compress loop's JSON rewriters strip render tags from every round, + // so a non-injected JSON response must not hand the model's echoes + // back untouched. Same pipe as plugin mode; no session, so usage + // accounting stays off for the same reason as above. + await pipePluginJson(responseBody, res, undefined, prepared.protocol); + clearUpstreamTimer(); } else { - await pipeThrough(upstream.body, res); + await pipeThrough(responseBody, res); clearUpstreamTimer(); } return; @@ -2607,10 +2752,10 @@ async function forward( debug: opts.debug, }; if (prepared.stream) { - let streamToRead = upstream.body as ReadableStream; + let streamToRead = responseBody; let dumpRaw: Promise | undefined; if (opts.dumpSse) { - const [a, b] = (upstream.body as ReadableStream).tee(); + const [a, b] = responseBody.tee(); streamToRead = a; dumpRaw = dumpStreamToFile(b, opts.dumpSse, `${Date.now()}-${safeSessionId(prepared.session.id)}-raw.sse`); } @@ -2776,6 +2921,78 @@ async function readStreamToBuffer(stream: ReadableStream, maxBytes = return Buffer.concat(chunks); } +function bufferToStream(buf: Buffer): ReadableStream { + return new ReadableStream({ + start(controller) { + controller.enqueue(new Uint8Array(buf)); + controller.close(); + }, + }); +} + +// #371: buffer the raw upstream response, detect a fake completion (tool-call +// XML, no real tool block), and — bounded per turn and per session — re-request +// upstream with a corrective hint. Returns the bytes to stream to the client +// (the retry's response when a retry recovered, else the original). The session +// streak (metadata.fakeCompletionStreak) counts consecutive fake-completion +// turns: it gates retries (skip once >= cap) and resets to 0 on a clean turn. +async function resolveFakeCompletion( + stream: ReadableStream, + opts: { + protocol: WireProtocol; + body: string | Buffer; + upstreamUrl: string; + reqHeaders: Record; + proxyUrl?: string; + signal: AbortSignal; + session: Session; + log: (level: string, msg: string) => void; + }, +): Promise { + let buffer = await readStreamToBuffer(stream, fakeBufCap()); + const max = maxFakeCompletionRetries(); + const sid = opts.session.id; + const priorStreak = (opts.session.metadata.fakeCompletionStreak as number | undefined) ?? 0; + if (max > 0 && priorStreak < max && isFakeCompletion(opts.protocol, buffer.toString("utf8"))) { + for (let attempt = 1; attempt <= max && !opts.signal.aborted; attempt++) { + const hinted = injectFakeCompletionHint(opts.protocol, opts.body); + if (hinted === null) break; + opts.log("warn", `[${sid}] fake completion (tool-call XML, no tool block); retry ${attempt}/${max} with corrective hint`); + let r: Awaited>; + try { + r = await fetchWithTimeout( + opts.upstreamUrl, + { + method: "POST", + headers: opts.reqHeaders, + body: hinted, + ...(opts.proxyUrl ? { dispatcher: proxyDispatcher(opts.proxyUrl) } : {}), + }, + undefined, + opts.signal, + ); + } catch (e) { + opts.log("warn", `[${sid}] fake-completion retry failed: ${String(e)}; presenting original`); + break; + } + try { + if (!r.response.ok || !r.response.body) { + opts.log("warn", `[${sid}] fake-completion retry rejected (HTTP ${r.response.status}); presenting original`); + break; + } + buffer = await readStreamToBuffer(r.response.body, fakeBufCap()); + } finally { + r.clearTimer(); + } + if (!isFakeCompletion(opts.protocol, buffer.toString("utf8"))) break; + } + } + const stillFake = isFakeCompletion(opts.protocol, buffer.toString("utf8")); + opts.session.metadata.fakeCompletionStreak = stillFake ? priorStreak + 1 : 0; + markDirty(opts.session); + return buffer; +} + async function pipeThrough(stream: ReadableStream, res: http.ServerResponse): Promise { const reader = stream.getReader(); try { diff --git a/src/session.ts b/src/session.ts index c180c2ab38..a313166309 100644 --- a/src/session.ts +++ b/src/session.ts @@ -287,6 +287,82 @@ export function reconcileNativeCompactionBoundary(session: Session): boolean { return true; } +/** Mark a client-side native compaction (omp /compact on the anthropic wire). + * The sid does NOT rotate on in-session compaction, so the per-sid registry + * reuses the same key with stale state. Consumed by the NEXT processTurn via + * applyCompactionArchive (#395). Distinct from nativeCompactionBoundary + * (Responses/codex, which rebases by resetting state). */ +export function markCompactionBoundary(session: Session): void { + session.metadata.compactionBoundary = { + at: Date.now(), + pending: true, + }; + markDirty(session); +} + +type PreCompactionArchive = Record; + +function readPreCompactionArchive(session: Session): PreCompactionArchive { + const raw = session.metadata.preCompactionArchive; + if (raw && typeof raw === "object" && !Array.isArray(raw)) { + return raw as PreCompactionArchive; + } + return {}; +} + +export function preCompactionArchiveOf(session: Session): PreCompactionArchive { + return readPreCompactionArchive(session); +} + +// Must run AFTER the processTurn that followed markCompactionBoundary (so +// syncBlocks has deactivated the blocks whose raw ids left the shortened +// history). Blocks active in `activeBefore` but inactive now are archived; +// byRaw/byRef are pruned to liveRawIds (stops the #390 additive leak). +// nextIndex is left alone so a freed ref slot is never re-allocated onto a +// retained tail's live tag. +export function applyCompactionArchive( + session: Session, + activeBefore: Set, + liveRawIds: Set, + log: (level: string, msg: string) => void, +): void { + const boundary = session.metadata.compactionBoundary; + if (!boundary || typeof boundary !== "object" || !(boundary as Record).pending) { + return; + } + const activeAfter = new Set(session.state.blocks.filter((b) => b.active).map((b) => b.blockId)); + const deactivated = [...activeBefore].filter((id) => !activeAfter.has(id)); + if (deactivated.length > 0) { + const archive = readPreCompactionArchive(session); + const at = Date.now(); + for (const id of deactivated) { + archive[id] = { at, reason: "content replaced by client native compaction summary" }; + } + session.metadata.preCompactionArchive = archive; + } + + const { byRaw, byRef } = session.state.messageRefs; + const prunedByRaw: Record = {}; + for (const [rawId, ref] of Object.entries(byRaw)) { + if (liveRawIds.has(rawId)) prunedByRaw[rawId] = ref; + } + const prunedByRef: Record = {}; + for (const [ref, rawId] of Object.entries(byRef)) { + if (liveRawIds.has(rawId)) prunedByRef[ref] = rawId; + } + session.state.messageRefs.byRaw = prunedByRaw; + session.state.messageRefs.byRef = prunedByRef; + + session.metadata.compactionBoundary = { + ...(boundary as Record), + pending: false, + archivedAt: Date.now(), + archivedBlocks: deactivated, + }; + markDirty(session); + log("info", `[${session.id}] native compaction boundary: archived ${deactivated.length} pre-compaction block(s)${deactivated.length > 0 ? ` (${deactivated.join(", ")})` : ""}; pruned ref maps to ${prunedByRaw.length} live raw id(s)`); +} + /** Flush a session to disk and drop it from memory (LRU eviction). Refuses to * evict sessions that are in-flight or whose flush failed (would lose a * never-persisted session permanently). Returns true if a slot was freed. */ diff --git a/src/stream-error.ts b/src/stream-error.ts index 15ec43e886..9e76093e7f 100644 --- a/src/stream-error.ts +++ b/src/stream-error.ts @@ -5,7 +5,7 @@ import type http from "node:http"; * failure and ends cleanly, instead of a bare socket close. * * WHY: server.ts forward() routes streams through protocol rewriters - * (rewriteSseStream / runCompressLoop). If a + * (runCompressLoop / the tag-echo passthrough pipes). If a * rewriter throws (e.g. executeProxyTool hits an edge case, JSON.parse fails), * the `for await` loop aborts and — without this — `res.end()` is skipped, * leaving the client with a truncated stream and no finish event. The request diff --git a/src/stream-openai.ts b/src/stream-openai.ts index 56dda84962..04340ae4bc 100644 --- a/src/stream-openai.ts +++ b/src/stream-openai.ts @@ -2,184 +2,8 @@ import type { CompressionCore, Config, CoreMessage } from "acp-kernel"; import type { Session } from "./session.js"; import { COMPRESS_TOOL_NAME, parseCompressInput } from "./compress-tool.js"; import { applyRanges, type RewriteCtx } from "./stream.js"; -import { normalizeSseLineEndings } from "./sse-util.js"; import { containsRenderTagText, stripAcpTags } from "./loop/tag-echo-filter.js"; -type StreamState = { - compressIndices: Set; - args: Record; - converted: boolean; - sawReal: boolean; - finishReason: string | null; - finishObj: Record | null; - done: boolean; -}; - -function newState(): StreamState { - return { - compressIndices: new Set(), - args: {}, - converted: false, - sawReal: false, - finishReason: null, - finishObj: null, - done: false, - }; -} - -export async function* rewriteOpenaiSseStream( - upstream: ReadableStream, - ctx: RewriteCtx, -): AsyncGenerator { - const reader = upstream.getReader(); - const decoder = new TextDecoder("utf-8"); - let buf = ""; - const state = newState(); - let output = ""; - let eventNum = 0; - const flush = (): Buffer => { - const out = Buffer.from(output, "utf8"); - output = ""; - return out; - }; - try { - for (;;) { - const { done, value } = await reader.read(); - if (done) break; - buf += decoder.decode(value, { stream: true }); - buf = normalizeSseLineEndings(buf); - let idx: number; - while ((idx = buf.indexOf("\n\n")) !== -1) { - const rawEvent = buf.slice(0, idx); - buf = buf.slice(idx + 2); - const routed = routeOpenaiEvent(rawEvent, state); - eventNum++; - if (ctx.debug) { - const dl = rawEvent.split("\n").find(l => l.startsWith("data:")); - const data = dl ? dl.slice(5).trim() : ""; - let summary = data.slice(0, 80); - try { - const obj = JSON.parse(data); - const tc = obj.choices?.[0]?.delta?.tool_calls?.[0]; - if (tc?.function?.name) summary = `tool_call name=${tc.function.name} idx=${tc.index} args_len=${(tc.function.arguments ?? "").length}`; - else if (obj.choices?.[0]?.finish_reason) summary = `finish=${obj.choices[0].finish_reason}`; - } catch { /* best-effort debug summary */ } - ctx.log(`sse[${eventNum}] routed=${routed === null ? "SUPPRESSED" : "passthrough"} | ${summary}`); - } - if (routed === null) continue; - output += routed; - if (output.length >= 8192) yield flush(); - } - } - output += buildOpenaiTail(state, ctx); - if (output) yield flush(); - } finally { - reader.releaseLock(); - } -} - -function routeOpenaiEvent(rawEvent: string, state: StreamState): string | null { - const dataLine = extractDataLine(rawEvent); - if (dataLine === null) return rawEvent + "\n\n"; - if (dataLine === "[DONE]") { - state.done = true; - return state.converted ? null : rawEvent + "\n\n"; - } - let obj: Record; - try { - obj = JSON.parse(dataLine); - } catch { - return rawEvent + "\n\n"; - } - const choices = obj.choices as Array> | undefined; - const choice = choices?.[0]; - if (!choice) return rawEvent + "\n\n"; - const finishReason = choice.finish_reason; - if (typeof finishReason === "string" && finishReason !== "null") { - state.finishReason = finishReason; - if (!state.converted) { - state.done = true; - return rawEvent + "\n\n"; - } - state.finishObj = obj; - return null; - } - const delta = choice.delta as { content?: string; tool_calls?: Array> } | undefined; - if (delta && Array.isArray(delta.tool_calls) && delta.tool_calls.length > 0) { - // Process EVERY tool call in the delta, not just tool_calls[0]. A - // single SSE chunk can carry multiple tool calls (different indices); - // reading only [0] dropped the args of the 2nd+ and failed to mark - // them as real/compress. - let allCompress = true; - for (const raw of delta.tool_calls) { - const entry = raw as { - index?: number; - function?: { name?: string; arguments?: string }; - }; - const tidx = entry.index ?? 0; - const name = entry.function?.name; - if (typeof name === "string") { - if (name === COMPRESS_TOOL_NAME) { - state.compressIndices.add(tidx); - state.converted = true; - } else { - state.sawReal = true; - } - } - if (state.compressIndices.has(tidx)) { - const frag = entry.function?.arguments; - if (typeof frag === "string") state.args[tidx] = (state.args[tidx] ?? "") + frag; - } else { - allCompress = false; - } - } - // Suppress the whole event only if every tool call in this chunk is a - // compress call. If a real tool call shares the chunk, pass it through - // verbatim (multi-tool-per-chunk is rare; rewriting a partial delta - // is fragile, so we accept the edge case rather than risk corruption). - if (allCompress) return null; - return rawEvent + "\n\n"; - } - return rawEvent + "\n\n"; -} - -function buildOpenaiTail(state: StreamState, ctx: RewriteCtx): string { - if (!state.converted) return ""; - const base = (state.finishObj ?? { object: "chat.completion.chunk" }) as Record; - let out = ""; - const sortedIndices = [...state.compressIndices].sort((a, b) => a - b); - for (const tidx of sortedIndices) { - const raw = state.args[tidx] ?? ""; - let parsed: unknown = {}; - try { - parsed = raw ? JSON.parse(raw) : {}; - } catch { - parsed = {}; - } - const note = applyRanges(parseCompressInput(parsed), ctx); - out += `data: ${JSON.stringify({ ...base, choices: [{ index: 0, delta: { content: note + "\n" }, finish_reason: null }] })}\n\n`; - } - let finalReason: string; - if (state.converted) { - finalReason = state.sawReal ? "tool_calls" : "stop"; - } else { - finalReason = state.finishReason ?? "stop"; - } - out += `data: ${JSON.stringify({ ...base, choices: [{ index: 0, delta: {}, finish_reason: finalReason }] })}\n\n`; - out += "data: [DONE]\n\n"; - return out; -} - -function extractDataLine(rawEvent: string): string | null { - const lines = rawEvent.split("\n"); - for (const l of lines) { - if (l.startsWith("data:")) { - return l.slice(5).trim(); - } - } - return null; -} - export function rewriteOpenaiJsonResponse(body: unknown, ctx: RewriteCtx): unknown { if (!body || typeof body !== "object") return body; const b = body as { diff --git a/src/stream-responses.ts b/src/stream-responses.ts index 5780249dae..ea7607d4c9 100644 --- a/src/stream-responses.ts +++ b/src/stream-responses.ts @@ -2,6 +2,7 @@ import type { CompressionCore, Config, CoreMessage } from "acp-kernel"; import type { Session } from "./session.js"; import { COMPRESS_TOOL_NAME, parseCompressInput } from "./compress-tool.js"; import { applyRanges, type RewriteCtx } from "./stream.js"; +import { containsRenderTagText, stripResponsesText } from "./loop/tag-echo-filter.js"; /** * Responses API (non-streaming) JSON rewriter: strips compress function_call @@ -17,6 +18,17 @@ export function rewriteResponsesJsonResponse(body: unknown, ctx: RewriteCtx): un status?: string; }; if (!Array.isArray(b.output)) return body; + const probe = JSON.stringify(b.output); + // #460 residual: the other two wires' JSON rewriters strip model-emitted + // render tags from prose (stream.ts on content[].text, stream-openai.ts on + // message.content); this wire must too. Strip the upstream items here, on + // every body — before the compress note is synthesized (so the record we + // inject is never edited) and before the !converted early return below + // (which previously handed a tag-bearing body back untouched). + if (containsRenderTagText(probe)) { + ctx.log(`[warn: tag echo] non-stream responses output contains , - ctx: RewriteCtx, -): AsyncGenerator { - const reader = upstream.getReader(); - const decoder = new TextDecoder("utf-8"); - let buf = ""; - const blocks = new Map(); - let convertedAny = false; - let sawRealToolUse = false; - let capturedUsage: Record | undefined; - let output = ""; - - const flush = (): Buffer => { - const out = Buffer.from(output, "utf8"); - output = ""; - return out; - }; - - try { - for (;;) { - const { done, value } = await reader.read(); - if (done) break; - buf += decoder.decode(value, { stream: true }); - buf = normalizeSseLineEndings(buf); - let idx: number; - while ((idx = buf.indexOf("\n\n")) !== -1) { - const rawEvent = buf.slice(0, idx); - buf = buf.slice(idx + 2); - const ev = parseSseEvent(rawEvent); - if (!ev) continue; - const routed = routeEvent(ev, blocks, ctx, (c) => (convertedAny = c || convertedAny), () => (sawRealToolUse = true), () => convertedAny, (u) => (capturedUsage = u)); - if (routed === NOOP) continue; - output += routed; - if (output.length >= 8192) yield flush(); - } - } - if (convertedAny) { - const finalDelta = buildStopReasonRewrite(sawRealToolUse, capturedUsage); - if (finalDelta) output += finalDelta; - } - if (buf) { - output += buf; - buf = ""; - } - if (output) yield flush(); - } finally { - reader.releaseLock(); - } -} - -function routeEvent( - ev: SseEvent, - blocks: Map, - ctx: RewriteCtx, - markConverted: (v: boolean) => void, - markRealToolUse: () => void, - getConverted: () => boolean, - setUsage: (u: Record) => void, -): string | typeof NOOP { - const d = ev.data; - if (!d || typeof d !== "object") return emitEvent(ev); - const t = (d as { type?: string }).type; - if (t === "content_block_start") { - const index = (d as { index?: number }).index ?? 0; - const cb = (d as { content_block?: { type?: string; name?: string } }).content_block; - if (cb?.type === "tool_use" && typeof cb.name === "string" && PROXY_TOOL_NAMES.has(cb.name)) { - blocks.set(index, { toolName: cb.name, json: "", hadRealToolUse: false }); - markConverted(true); - return NOOP; - } - if (cb?.type === "tool_use") markRealToolUse(); - return emitEvent(ev); - } - if (t === "content_block_delta") { - const index = (d as { index?: number }).index ?? 0; - const st = blocks.get(index); - if (st && st.toolName) { - const partial = (d as { delta?: { partial_json?: string } }).delta?.partial_json; - if (typeof partial === "string") st.json += partial; - return NOOP; - } - const dt = (d as { delta?: { text?: string } }).delta; - if (dt?.text && containsRenderTagText(dt.text)) { - ctx.log(`[warn: tag echo] model emitted }).usage; - if (u) { - setUsage(u); - if (!getConverted()) { - const out = u.output_tokens; - if (typeof out === "number") ctx.session.stats.outputTokens += out; - } - } - return getConverted() ? NOOP : emitEvent(ev); - } - if (t === "message_start" && !getConverted()) { - const u = (d as { message?: { usage?: Record } }).message?.usage; - if (u) { - const inp = u.input_tokens; - const cc = u.cache_creation_input_tokens; - const cr = u.cache_read_input_tokens; - if (typeof inp === "number") ctx.session.stats.inputTokens += inp; - if (typeof cr === "number") { - ctx.session.stats.cachedTokens += cr; - ctx.session.stats.cacheSamples += 1; - } else if (typeof inp === "number") { - ctx.session.stats.cacheSamples += 1; - } - // Anthropic bills cache writes separately; track them under input - // so the cumulative input reflects real prompt cost. - if (typeof cc === "number") ctx.session.stats.inputTokens += cc; - } - } - if (t === "message_stop") { - return getConverted() ? NOOP : emitEvent(ev); - } - return emitEvent(ev); -} - -function emitToolReplacement(toolName: string, jsonInput: string, ctx: RewriteCtx, index: number): string { - let parsed: unknown = {}; - try { - parsed = jsonInput ? JSON.parse(jsonInput) : {} - } catch { - parsed = {}; - } - const args = (parsed && typeof parsed === "object" ? parsed : {}) as Record; - const text = executeAnthropicProxyTool(toolName, args, ctx); - // Reuse the suppressed tool_use block's own index instead of hardcoding 0. - // Hardcoding 0 re-opens an already-closed text block (index 0 is usually a - // preceding text block), producing malformed SSE. - return ( - `event: content_block_start\n` + - `data: ${JSON.stringify({ type: "content_block_start", index, content_block: { type: "text", text: "" } })}\n\n` + - `event: content_block_delta\n` + - `data: ${JSON.stringify({ type: "content_block_delta", index, delta: { type: "text_delta", text } })}\n\n` + - `event: content_block_stop\n` + - `data: ${JSON.stringify({ type: "content_block_stop", index })}\n\n` - ); -} - // Dispatch all four ACP proxy tools to the same logic the OpenAI/Responses // path uses (compress-loop.ts executeProxyTool). compress mutates context // (handled by applyRanges); the other three are read-only queries whose result -// is emitted as a text delta replacing the intercepted tool_use block. +// becomes a text block replacing the intercepted tool_use. function executeAnthropicProxyTool(toolName: string, args: Record, ctx: RewriteCtx): string { if (toolName === COMPRESS_TOOL_NAME) { return applyRanges(parseCompressInput(args), ctx); @@ -310,42 +139,6 @@ export function applyRanges(parsed: ReturnType, ctx: } } -function buildStopReasonRewrite(sawRealToolUse: boolean, usage?: Record): string { - const stop_reason = sawRealToolUse ? "tool_use" : "end_turn"; - // Preserve upstream usage (output_tokens etc.). Default output_tokens:0 so - // strict clients (ZCode Zod) that require output_tokens:number still pass - // even if the upstream omitted it. - const u = { output_tokens: 0, ...(usage ?? {}) }; - return ( - `event: message_delta\n` + - `data: ${JSON.stringify({ type: "message_delta", delta: { stop_reason }, usage: u })}\n\n` + - `event: message_stop\n` + - `data: ${JSON.stringify({ type: "message_stop" })}\n\n` - ); -} - -type SseEvent = { raw: string; data: unknown | null }; - -function parseSseEvent(raw: string): SseEvent | null { - const lines = raw.split("\n"); - let dataLine: string | null = null; - for (const l of lines) { - if (l.startsWith("data:")) { - dataLine = l.slice(5).trim(); - } - } - if (dataLine === null) return { raw, data: null }; - try { - return { raw, data: JSON.parse(dataLine) }; - } catch { - return { raw, data: dataLine }; - } -} - -function emitEvent(ev: SseEvent): string { - return ev.raw + "\n\n"; -} - export function rewriteJsonResponse(body: unknown, ctx: RewriteCtx): unknown { if (!body || typeof body !== "object") return body; const b = body as { content?: unknown[]; stop_reason?: string }; diff --git a/tests/basic.test.ts b/tests/basic.test.ts index 4c1b0cf5ea..40c7572a93 100644 --- a/tests/basic.test.ts +++ b/tests/basic.test.ts @@ -247,72 +247,6 @@ test("conversationSignalOpenai is stable for same first-user content", () => { assert.ok(a.length > 0); }); -test("OpenAI SSE rewriter passes through real tool calls when no compress detected", async () => { - const { rewriteOpenaiSseStream } = await import("../src/stream-openai.js"); - const { createCore, createInitialState, defaultConfig } = await import("acp-kernel"); - const core = createCore(); - const state = createInitialState(); - const config = defaultConfig(200000); - const ctx = { core, config, messages: [], session: { id: "s1", meta: {}, stats: { requests: 0, tokensSaved: 0, inputTokens: 0, cachedTokens: 0, outputTokens: 0, cacheSamples: 0, lastInputTokens: 0, contextTokens: 0 }, metadata: {}, state }, log: () => {} }; - - // Simulate a provider that sends the LAST tool_call arguments fragment - // in the SAME chunk as finish_reason (common in GLM/OpenAI-compatible APIs). - // Before the fix, the rewriter suppressed this entire chunk, dropping the - // last arguments fragment → opencode got SchemaError(Missing key at ["command"]). - const sse = [ - `data: ${JSON.stringify({ object: "chat.completion.chunk", choices: [{ index: 0, delta: { role: "assistant" as const, tool_calls: [{ index: 0, id: "call_1", type: "function", function: { name: "bash", arguments: "{\"command\":\"" } }] }, finish_reason: null }] })}\n\n`, - `data: ${JSON.stringify({ object: "chat.completion.chunk", choices: [{ index: 0, delta: { tool_calls: [{ index: 0, function: { arguments: "echo test\"}" } }] }, finish_reason: null }] })}\n\n`, - `data: ${JSON.stringify({ object: "chat.completion.chunk", choices: [{ index: 0, delta: {}, finish_reason: "tool_calls" }] })}\n\n`, - `data: [DONE]\n\n`, - ].join(""); - const upstream = new ReadableStream({ - start(c) { - c.enqueue(new TextEncoder().encode(sse)); - c.close(); - }, - }); - const chunks: string[] = []; - for await (const buf of rewriteOpenaiSseStream(upstream, ctx as never)) { - chunks.push(buf.toString("utf8")); - } - const combined = chunks.join(""); - assert.ok(combined.includes('"bash"'), "tool name must pass through"); - assert.ok(combined.includes("echo test"), "tool arguments must pass through"); - assert.ok(combined.includes("tool_calls"), "finish_reason tool_calls must be present"); - assert.ok(combined.includes("[DONE]"), "[DONE] marker must be present"); - assert.ok(!combined.includes("acp-proxy:"), "no compress note expected"); -}); - -test("OpenAI SSE rewriter suppresses compress tool call and injects note", async () => { - const { rewriteOpenaiSseStream } = await import("../src/stream-openai.js"); - const { createCore, createInitialState, defaultConfig } = await import("acp-kernel"); - const core = createCore(); - const config = defaultConfig(200000); - const state = createInitialState(); - const ctx = { core, config, messages: [], session: { id: "s1", meta: {}, stats: { requests: 0, tokensSaved: 0, inputTokens: 0, cachedTokens: 0, outputTokens: 0, cacheSamples: 0, lastInputTokens: 0, contextTokens: 0 }, metadata: {}, state }, log: () => {} }; - - const sse = [ - `data: ${JSON.stringify({ object: "chat.completion.chunk", choices: [{ index: 0, delta: { tool_calls: [{ index: 0, id: "call_1", type: "function", function: { name: "compress", arguments: '{"content":[{"startId":"m00001","endId":"m00001","summary":"test"}]}' } }] }, finish_reason: null }] })}\n\n`, - `data: ${JSON.stringify({ object: "chat.completion.chunk", choices: [{ index: 0, delta: {}, finish_reason: "tool_calls" }] })}\n\n`, - `data: [DONE]\n\n`, - ].join(""); - const upstream = new ReadableStream({ - start(c) { - c.enqueue(new TextEncoder().encode(sse)); - c.close(); - }, - }); - const chunks: string[] = []; - for await (const buf of rewriteOpenaiSseStream(upstream, ctx as never)) { - chunks.push(buf.toString("utf8")); - } - const combined = chunks.join(""); - assert.ok(!combined.includes('"compress"'), "compress tool name must be suppressed"); - assert.ok(combined.includes('"stop"'), "finish_reason should be stop"); - assert.ok(combined.includes("[DONE]"), "[DONE] must be present"); -}); - - test("ACP tag regex strips tag prefix from tool-call arguments", () => { const ACP_TAG_RE = /^\x3cacp [^>]*\x3e[^\x3c]*\x3c\/acp\x3e\n?/; const tagged = '\x3cacp tokens="50" type="bash"\x3em00005\x3c/acp\x3e\n{"command":"echo hello"}'; diff --git a/tests/compaction-archive.test.ts b/tests/compaction-archive.test.ts new file mode 100644 index 0000000000..354f6fdb0d --- /dev/null +++ b/tests/compaction-archive.test.ts @@ -0,0 +1,116 @@ +import { test } from "node:test"; +import assert from "node:assert/strict"; +import { createCore, createInitialState, defaultConfig, type CoreMessage } from "acp-kernel"; +import type { Session } from "../src/session.ts"; +import { applyCompactionArchive, markCompactionBoundary, preCompactionArchiveOf } from "../src/session.ts"; +import { resolveDecompress } from "../src/decompress-shared.ts"; +import { executeProxyTool, type LoopCtx } from "../src/loop/core.ts"; + +function makeSession(): Session { + return { + id: `test-${Math.random().toString(36).slice(2)}`, + meta: {}, + stats: { requests: 0, tokensSaved: 0, inputTokens: 0, cachedTokens: 0, outputTokens: 0, cacheSamples: 0, lastInputTokens: 0, contextTokens: 0 }, + metadata: {}, + state: createInitialState(), + createdAt: Date.now(), + lastSeen: Date.now(), + blockContents: new Map(), + inFlight: 0, + persisted: false, + }; +} + +const noLog = (_level: string, _msg: string): void => {}; + +// One active block over m00001-m00007; tail = m00008-m00012 (omp's post-compact resend). +function sessionWithActiveBlock(): { session: Session; blockId: string; tail: CoreMessage[] } { + const session = makeSession(); + const core = createCore(); + const config = defaultConfig(200000); + const msgs: CoreMessage[] = []; + for (let i = 0; i < 12; i++) { + msgs.push({ id: `h_distinct${i}`, role: i % 2 === 0 ? "user" : "assistant", contentType: "text", text: `message ${i} ${"x".repeat(2000)}` }); + } + const turn = core.processTurn({ messages: msgs, state: session.state, config, tokenCount: 9999, renderTags: "text-only" }); + session.state = turn.state; + const res = core.applyCompression({ + ranges: [{ startRef: "m00001", endRef: "m00007", summary: "compressed early history that is long enough to pass the min summary length check" }], + messages: turn.messages, + state: turn.state, + config, + }); + session.state = res.state; + const block = session.state.blocks[session.state.blocks.length - 1]; + const tail: CoreMessage[] = []; + for (let i = 7; i < 12; i++) { + tail.push({ id: `h_distinct${i}`, role: i % 2 === 0 ? "user" : "assistant", contentType: "text", text: `message ${i} ${"x".repeat(2000)}` }); + } + return { session, blockId: block.blockId, tail }; +} + +// Mirrors server.ts prepareAnthropic: capture active ids → shortened-history +// turn (syncBlocks deactivates) → applyCompactionArchive. +function runShortenedTurn(session: Session, tail: CoreMessage[], boundary: boolean): string { + const core = createCore(); + const config = defaultConfig(200000); + const activeBefore = new Set(session.state.blocks.filter((b) => b.active).map((b) => b.blockId)); + if (boundary) markCompactionBoundary(session); + const turn = core.processTurn({ messages: tail, state: session.state, config, tokenCount: 9999, renderTags: "text-only" }); + session.state = turn.state; + applyCompactionArchive(session, activeBefore, new Set(tail.map((m) => m.id)), noLog); + return [...activeBefore].join(","); +} + +test("compaction archive: shortened-history resend degrades the block into an auditable archive", () => { + const { session, blockId, tail } = sessionWithActiveBlock(); + runShortenedTurn(session, tail, true); + const block = session.state.blocks.find((b) => b.blockId === blockId); + assert.equal(block?.active, false, "syncBlocks deactivates the block whose raw ids left the history"); + const archive = preCompactionArchiveOf(session); + assert.ok(archive[blockId] !== undefined, "block is recorded in the pre-compaction archive (not silently deactivated)"); + assert.match(archive[blockId]!.reason, /native compaction/); + const boundary = session.metadata.compactionBoundary as Record; + assert.equal(boundary.pending, false, "boundary is consumed"); + assert.ok(Array.isArray(boundary.archivedBlocks) && (boundary.archivedBlocks as string[]).includes(blockId), "archivedBlocks records the generation"); +}); + +test("compaction archive: WITHOUT the boundary the block is silently deactivated (old behavior preserved)", () => { + const { session, blockId, tail } = sessionWithActiveBlock(); + runShortenedTurn(session, tail, false); + const block = session.state.blocks.find((b) => b.blockId === blockId); + assert.equal(block?.active, false, "still deactivated by syncBlocks"); + const archive = preCompactionArchiveOf(session); + assert.equal(archive[blockId], undefined, "no archive entry without a marked boundary"); +}); + +test("compaction archive: byRaw/byRef are pruned to live ids (stops the additive leak)", () => { + const { session, tail } = sessionWithActiveBlock(); + const before = Object.keys(session.state.messageRefs.byRaw).length; + runShortenedTurn(session, tail, true); + const liveIds = new Set(tail.map((m) => m.id)); + for (const rawId of Object.keys(session.state.messageRefs.byRaw)) { + assert.ok(liveIds.has(rawId), `byRaw entry ${rawId} should have been pruned`); + } + assert.ok(Object.keys(session.state.messageRefs.byRaw).length < before, "byRaw shrank after the boundary"); +}); + +test("compaction archive: decompress on an archived block gives an explicit reason, not silent/wrong content", () => { + const { session, blockId, tail } = sessionWithActiveBlock(); + runShortenedTurn(session, tail, true); + const ctx = { core: createCore(), config: defaultConfig(200000), messages: tail, session, log: (_msg: string) => {} }; + const out = resolveDecompress({ blockId }, ctx); + assert.match(out, /decompress FAILED/); + assert.match(out, /pre-compaction archive/); + assert.match(out, /no longer reachable/); +}); + +test("compaction archive: acp_status surfaces the archive instead of listing it as active", () => { + const { session, blockId, tail } = sessionWithActiveBlock(); + runShortenedTurn(session, tail, true); + const ctx: LoopCtx = { core: createCore(), config: defaultConfig(200000), messages: tail, session, log: (_msg: string) => {} }; + const out = executeProxyTool("acp_status", {}, ctx); + assert.match(out, /PRE-COMPACTION ARCHIVE/); + assert.match(out, /decompress is unavailable/); + assert.ok(out.includes(blockId), "acp_status names the archived block"); +}); diff --git a/tests/fake-completion.test.ts b/tests/fake-completion.test.ts new file mode 100644 index 0000000000..9fea9a3c46 --- /dev/null +++ b/tests/fake-completion.test.ts @@ -0,0 +1,273 @@ +import assert from "node:assert"; +import http from "node:http"; +import { once } from "node:events"; +import test from "node:test"; + +process.env.NODE_ENV = "test"; +// The fake-completion fallback is opt-in (disabled by default to preserve +// incremental streaming); these tests exercise it, so enable it explicitly. +process.env.BILI_FAKE_COMPLETION_RETRIES = "2"; + +import { defaultConfig } from "acp-kernel"; +import { startServer, type ProxyOptions } from "../src/server.ts"; +import { SessionStore, _setStoreForTest } from "../src/persist.ts"; +import { _setForTest as setRegistryForTest } from "../src/registry.ts"; +import { + isFakeCompletion, + hasToolBlock, + hasToolCallStructure, + injectFakeCompletionHint, +} from "../src/fake-completion.ts"; + +// Hex escapes so no literal tag sequence appears in this file's source. +const LT = "\x3c"; +const GT = "\x3e"; + +// The #361 shape: the model echoed a tool call as TEXT — an opening invoke +// plus closing tags — with no real tool block. A distinctive marker lets the +// integration tests prove this text is discarded on retry. +const FAKE_XML = `${LT}invoke name="read_file"${GT}\n${LT}parameter name="path"${GT}/fake/completion/marker${LT}/parameter${GT}\n${LT}/invoke${GT}\n${LT}/tool_calls${GT}`; + +test("hasToolBlock: anthropic detects a tool_use block", () => { + assert.equal(hasToolBlock("anthropic", '{"type":"tool_use","name":"x"}'), true); + assert.equal(hasToolBlock("anthropic", '{"type":"text","text":"hi"}'), false); +}); + +test("hasToolBlock: openai detects a non-empty tool_calls array", () => { + assert.equal(hasToolBlock("openai", '{"choices":[{"delta":{"tool_calls":[{"index":0}]}}]}'), true); + assert.equal(hasToolBlock("openai", '{"choices":[{"delta":{"content":"hi"}}]}'), false); +}); + +test("hasToolBlock: responses detects a function_call item", () => { + assert.equal(hasToolBlock("responses", '{"type":"function_call","name":"x"}'), true); + assert.equal(hasToolBlock("responses", '{"type":"message"}'), false); +}); + +test("hasToolCallStructure: an opening tool tag is a structure", () => { + assert.equal(hasToolCallStructure(`${LT}invoke name="x"${GT}`), true); +}); + +test("hasToolCallStructure: two distinct closing tags (the #361 shape) is a structure", () => { + assert.equal(hasToolCallStructure(`${LT}/invoke${GT} ${LT}/tool_calls${GT}`), true); +}); + +test("hasToolCallStructure: a single closing tag is NOT a structure (legit prose)", () => { + assert.equal(hasToolCallStructure(`the ${LT}/invoke${GT} tag closes a call`), false); +}); + +test("hasToolCallStructure: plain text with no tool XML is not a structure", () => { + assert.equal(hasToolCallStructure("just plain text about tools"), false); +}); + +test("isFakeCompletion: anthropic tool-XML with no tool_use block is a fake completion", () => { + assert.equal(isFakeCompletion("anthropic", FAKE_XML), true); +}); + +test("isFakeCompletion: openai tool-XML with no tool_calls is a fake completion", () => { + assert.equal(isFakeCompletion("openai", FAKE_XML), true); +}); + +test("isFakeCompletion: responses tool-XML with no function_call is a fake completion", () => { + assert.equal(isFakeCompletion("responses", FAKE_XML), true); +}); + +test("isFakeCompletion: tool-XML WITH a real tool block is NOT a fake completion", () => { + assert.equal(isFakeCompletion("anthropic", `${FAKE_XML} {"type":"tool_use"}`), false); + assert.equal(isFakeCompletion("openai", `${FAKE_XML} "tool_calls":[{`), false); + assert.equal(isFakeCompletion("responses", `${FAKE_XML} {"type":"function_call"}`), false); +}); + +test("isFakeCompletion: legit prose with a single closing tag is NOT a fake completion", () => { + assert.equal(isFakeCompletion("anthropic", `the ${LT}/invoke${GT} tag closes a call`), false); +}); + +test("isFakeCompletion: plain text is not a fake completion", () => { + assert.equal(isFakeCompletion("anthropic", "all done, nothing to do"), false); +}); + +test("injectFakeCompletionHint: anthropic merges the hint into the last user message", () => { + const body = JSON.stringify({ model: "m", messages: [{ role: "user", content: "hello" }] }); + const out = injectFakeCompletionHint("anthropic", body); + assert.ok(out !== null); + const parsed = JSON.parse(out!) as { messages: Array<{ role: string; content: string }> }; + assert.equal(parsed.messages.length, 1); + assert.ok(parsed.messages[0]!.content.includes("hello")); + assert.ok(parsed.messages[0]!.content.includes("tool-calling mechanism")); +}); + +test("injectFakeCompletionHint: anthropic appends a new user message when the last is assistant", () => { + const body = JSON.stringify({ model: "m", messages: [{ role: "user", content: "hi" }, { role: "assistant", content: "yo" }] }); + const out = injectFakeCompletionHint("anthropic", body); + assert.ok(out !== null); + const parsed = JSON.parse(out!) as { messages: Array<{ role: string; content: string }> }; + assert.equal(parsed.messages.length, 3); + assert.equal(parsed.messages[2]!.role, "user"); + assert.ok(parsed.messages[2]!.content.includes("tool-calling mechanism")); +}); + +test("injectFakeCompletionHint: openai appends the hint to messages", () => { + const body = JSON.stringify({ model: "m", messages: [{ role: "user", content: "hi" }] }); + const out = injectFakeCompletionHint("openai", body); + assert.ok(out !== null); + const parsed = JSON.parse(out!) as { messages: Array<{ role: string; content: string }> }; + const last = parsed.messages[parsed.messages.length - 1]!; + assert.equal(last.role, "user"); + assert.ok(last.content.includes("tool-calling mechanism")); +}); + +test("injectFakeCompletionHint: responses merges an input_text part into the last user input", () => { + const body = JSON.stringify({ model: "m", input: [{ role: "user", content: [{ type: "input_text", text: "hi" }] }] }); + const out = injectFakeCompletionHint("responses", body); + assert.ok(out !== null); + const parsed = JSON.parse(out!) as { input: Array<{ role: string; content: Array<{ type: string; text?: string }> }> }; + const last = parsed.input[parsed.input.length - 1]!; + const texts = last.content.map((c) => c.text ?? "").join(""); + assert.ok(texts.includes("hi")); + assert.ok(texts.includes("tool-calling mechanism")); +}); + +test("injectFakeCompletionHint: returns null for an unparseable body", () => { + assert.equal(injectFakeCompletionHint("anthropic", "not json"), null); +}); + +interface Harness { + proxyPort: number; + upstreamPort: number; + captured: { body: string }[]; + close(): Promise; +} + +function anthropicSse(event: string, data: unknown): string { + return `event: ${event}\ndata: ${JSON.stringify(data)}\n\n`; +} + +function startHarness(scripts: string[][]): Promise { + const captured: { body: string }[] = []; + let call = 0; + const upstream = http.createServer((req, res) => { + const chunks: Buffer[] = []; + req.on("data", (c: Buffer) => chunks.push(c)); + req.on("end", () => { + captured.push({ body: Buffer.concat(chunks).toString() }); + res.writeHead(200, { "content-type": "text/event-stream" }); + const script = scripts[Math.min(call, scripts.length - 1)]!; + call += 1; + for (const line of script) res.write(line); + res.end(); + }); + }); + upstream.listen(0, "127.0.0.1"); + return (async () => { + await once(upstream, "listening"); + const upstreamPort = upstream.address().port; + _setStoreForTest(new SessionStore({ enabled: false })); + setRegistryForTest({}); + const proxy = await startServer({ + port: 0, + host: "127.0.0.1", + upstream: "http://127.0.0.1", + routes: { [`http://127.0.0.1:${upstreamPort}`]: { models: { "claude-test": { context: 400_000 } } } }, + modelContextLimit: 400_000, + kernelConfig: defaultConfig(400_000), + compress: { injectTool: false, injectNudge: false }, + sessionHeader: "x-acp-session", + log: false, + debug: false, + passthrough: false, + autoUpdate: false, + mitm: { enabled: false, domains: [] }, + } as ProxyOptions); + await once(proxy, "listening"); + const proxyPort = proxy.address().port; + return { + proxyPort, + upstreamPort, + captured, + close: async () => { + proxy.close(); + await once(proxy, "close"); + upstream.close(); + await once(upstream, "close"); + }, + }; + })(); +} + +async function callAnthropic(h: Harness, session: string, messages: Array<{ role: string; content: string }>): Promise { + const resp = await fetch(`http://127.0.0.1:${h.proxyPort}/bili/http://127.0.0.1:${h.upstreamPort}/v1/messages`, { + method: "POST", + headers: { "content-type": "application/json", "x-acp-session": session }, + body: JSON.stringify({ model: "claude-test", max_tokens: 1024, stream: true, system: "You are a test assistant.", messages }), + }); + assert.equal(resp.status, 200); + let raw = ""; + for await (const chunk of resp.body) raw += Buffer.from(chunk).toString("utf8"); + return raw; +} + +function fakeCompletionScript(): string[] { + return [ + anthropicSse("message_start", { type: "message_start", message: { id: "msg_fc_1", role: "assistant", usage: { input_tokens: 10 } } }), + anthropicSse("content_block_start", { type: "content_block_start", index: 0, content_block: { type: "text", text: "" } }), + anthropicSse("content_block_delta", { type: "content_block_delta", index: 0, delta: { type: "text_delta", text: FAKE_XML } }), + anthropicSse("content_block_stop", { type: "content_block_stop", index: 0 }), + anthropicSse("message_delta", { type: "message_delta", delta: { stop_reason: "end_turn", stop_sequence: null }, usage: { output_tokens: 5 } }), + anthropicSse("message_stop", { type: "message_stop" }), + ]; +} + +function realToolUseScript(): string[] { + return [ + anthropicSse("message_start", { type: "message_start", message: { id: "msg_fc_2", role: "assistant", usage: { input_tokens: 10 } } }), + anthropicSse("content_block_start", { type: "content_block_start", index: 0, content_block: { type: "tool_use", id: "toolu_fc_1", name: "read_file", input: {} } }), + anthropicSse("content_block_delta", { type: "content_block_delta", index: 0, delta: { type: "input_json_delta", partial_json: '{"path":"/foo"}' } }), + anthropicSse("content_block_stop", { type: "content_block_stop", index: 0 }), + anthropicSse("message_delta", { type: "message_delta", delta: { stop_reason: "tool_use", stop_sequence: null }, usage: { output_tokens: 5 } }), + anthropicSse("message_stop", { type: "message_stop" }), + ]; +} + +test("e2e #371: fake completion triggers a retry that yields a real tool call", async () => { + const h = await startHarness([fakeCompletionScript(), realToolUseScript()]); + try { + const raw = await callAnthropic(h, "fc-retry", [{ role: "user", content: "read the file" }]); + assert.equal(h.captured.length, 2, `expected exactly 2 upstream requests (original + 1 retry), got ${h.captured.length}`); + assert.ok(raw.includes('"type":"tool_use"'), "client should receive the real tool_use block from the retry"); + assert.ok(!raw.includes("/fake/completion/marker"), "the fake-completion text must not leak to the client"); + } finally { + await h.close(); + } +}); + +test("e2e #371: retry cap — always-fake upstream exhausts retries, then the session streak blocks further retries", async () => { + const h = await startHarness([fakeCompletionScript()]); + try { + await callAnthropic(h, "fc-cap", [{ role: "user", content: "t1" }]); + assert.equal(h.captured.length, 3, `turn 1: expected 3 requests (1 + 2 retries), got ${h.captured.length}`); + await callAnthropic(h, "fc-cap", [{ role: "user", content: "t2" }]); + assert.equal(h.captured.length, 6, `turn 2: expected 6 total (1 + 2 retries), got ${h.captured.length}`); + await callAnthropic(h, "fc-cap", [{ role: "user", content: "t3" }]); + assert.equal(h.captured.length, 7, `turn 3: expected 7 total (streak cap blocks the retry), got ${h.captured.length}`); + } finally { + await h.close(); + } +}); + +test("e2e #371: legit prose with a single closing tag does NOT trigger a retry", async () => { + const legitScript = [ + anthropicSse("message_start", { type: "message_start", message: { id: "msg_legit", role: "assistant", usage: { input_tokens: 10 } } }), + anthropicSse("content_block_start", { type: "content_block_start", index: 0, content_block: { type: "text", text: "" } }), + anthropicSse("content_block_delta", { type: "content_block_delta", index: 0, delta: { type: "text_delta", text: `The ${LT}/invoke${GT} tag closes a tool call in the template.` } }), + anthropicSse("content_block_stop", { type: "content_block_stop", index: 0 }), + anthropicSse("message_delta", { type: "message_delta", delta: { stop_reason: "end_turn", stop_sequence: null }, usage: { output_tokens: 5 } }), + anthropicSse("message_stop", { type: "message_stop" }), + ]; + const h = await startHarness([legitScript]); + try { + const raw = await callAnthropic(h, "fc-legit", [{ role: "user", content: "explain the template" }]); + assert.equal(h.captured.length, 1, `legit prose must NOT trigger a retry, got ${h.captured.length} requests`); + assert.ok(raw.includes("closes a tool call"), "legit prose must pass through to the client"); + } finally { + await h.close(); + } +}); diff --git a/tests/fixtures/glm-bash-toolcall.sse b/tests/fixtures/glm-bash-toolcall.sse deleted file mode 100644 index 64e5981ca2..0000000000 --- a/tests/fixtures/glm-bash-toolcall.sse +++ /dev/null @@ -1,6 +0,0 @@ -data: {"id":"20260805130314dc9b3fb152004c86","created":1785906194,"object":"chat.completion.chunk","model":"glm-5.2","choices":[{"index":0,"delta":{"tool_calls":[{"id":"call_09ba73f6ae4b4eab87dbda86","index":0,"type":"function","function":{"name":"bash","arguments":"{\"command\":\"echo capture-test\",\"description\":\"Print capture-test to stdout\"}"}}]}}]} - -data: {"id":"20260805130314dc9b3fb152004c86","created":1785906194,"object":"chat.completion.chunk","model":"glm-5.2","choices":[{"index":0,"finish_reason":"tool_calls","delta":{"role":"assistant","content":""}}],"usage":{"prompt_tokens":5330,"completion_tokens":23,"total_tokens":5353,"prompt_tokens_details":{"cached_tokens":5248},"completion_tokens_details":{"reasoning_tokens":0}}} - -data: [DONE] - diff --git a/tests/fixtures/glm-text-response.sse b/tests/fixtures/glm-text-response.sse deleted file mode 100644 index b0964432e3..0000000000 --- a/tests/fixtures/glm-text-response.sse +++ /dev/null @@ -1,8 +0,0 @@ -data: {"id":"2026080513031951cfe364bca449f5","created":1785906199,"object":"chat.completion.chunk","model":"glm-5.2","choices":[{"index":0,"delta":{"role":"assistant","content":"Done"}}]} - -data: {"id":"2026080513031951cfe364bca449f5","created":1785906199,"object":"chat.completion.chunk","model":"glm-5.2","choices":[{"index":0,"delta":{"role":"assistant","content":"."}}]} - -data: {"id":"2026080513031951cfe364bca449f5","created":1785906199,"object":"chat.completion.chunk","model":"glm-5.2","choices":[{"index":0,"finish_reason":"stop","delta":{"role":"assistant","content":""}}],"usage":{"prompt_tokens":5360,"completion_tokens":4,"total_tokens":5364,"prompt_tokens_details":{"cached_tokens":5312},"completion_tokens_details":{"reasoning_tokens":0}}} - -data: [DONE] - diff --git a/tests/fixtures/glm-titlegen-reasoning.sse b/tests/fixtures/glm-titlegen-reasoning.sse deleted file mode 100644 index 1631196138..0000000000 --- a/tests/fixtures/glm-titlegen-reasoning.sse +++ /dev/null @@ -1,38 +0,0 @@ -data: {"id":"20260805130314d8871eee54bd48b0","created":1785906194,"object":"chat.completion.chunk","model":"glm-4.5","choices":[{"index":0,"delta":{"role":"assistant","reasoning_content":"\n"}}]} - -data: {"id":"20260805130314d8871eee54bd48b0","created":1785906194,"object":"chat.completion.chunk","model":"glm-4.5","choices":[{"index":0,"delta":{"role":"assistant","reasoning_content":"Title"}}]} - -data: {"id":"20260805130314d8871eee54bd48b0","created":1785906194,"object":"chat.completion.chunk","model":"glm-4.5","choices":[{"index":0,"delta":{"role":"assistant","reasoning_content":" generation for"}}]} - -data: {"id":"20260805130314d8871eee54bd48b0","created":1785906194,"object":"chat.completion.chunk","model":"glm-4.5","choices":[{"index":0,"delta":{"role":"assistant","reasoning_content":" task request"}}]} - -data: {"id":"20260805130314d8871eee54bd48b0","created":1785906194,"object":"chat.completion.chunk","model":"glm-4.5","choices":[{"index":0,"delta":{"role":"assistant","reasoning_content":" to echo"}}]} - -data: {"id":"20260805130314d8871eee54bd48b0","created":1785906194,"object":"chat.completion.chunk","model":"glm-4.5","choices":[{"index":0,"delta":{"role":"assistant","reasoning_content":" a"}}]} - -data: {"id":"20260805130314d8871eee54bd48b0","created":1785906194,"object":"chat.completion.chunk","model":"glm-4.5","choices":[{"index":0,"delta":{"role":"assistant","reasoning_content":" test string"}}]} - -data: {"id":"20260805130314d8871eee54bd48b0","created":1785906194,"object":"chat.completion.chunk","model":"glm-4.5","choices":[{"index":0,"delta":{"role":"assistant","reasoning_content":" via bash"}}]} - -data: {"id":"20260805130314d8871eee54bd48b0","created":1785906194,"object":"chat.completion.chunk","model":"glm-4.5","choices":[{"index":0,"delta":{"role":"assistant","reasoning_content":" tool"}}]} - -data: {"id":"20260805130314d8871eee54bd48b0","created":1785906194,"object":"chat.completion.chunk","model":"glm-4.5","choices":[{"index":0,"delta":{"role":"assistant","reasoning_content":". Running"}}]} - -data: {"id":"20260805130314d8871eee54bd48b0","created":1785906194,"object":"chat.completion.chunk","model":"glm-4.5","choices":[{"index":0,"delta":{"role":"assistant","reasoning_content":" command with"}}]} - -data: {"id":"20260805130314d8871eee54bd48b0","created":1785906194,"object":"chat.completion.chunk","model":"glm-4.5","choices":[{"index":0,"delta":{"role":"assistant","reasoning_content":" echo"}}]} - -data: {"id":"20260805130314d8871eee54bd48b0","created":1785906194,"object":"chat.completion.chunk","model":"glm-4.5","choices":[{"index":0,"delta":{"role":"assistant","reasoning_content":" for capture"}}]} - -data: {"id":"20260805130314d8871eee54bd48b0","created":1785906194,"object":"chat.completion.chunk","model":"glm-4.5","choices":[{"index":0,"delta":{"role":"assistant","reasoning_content":"-test."}}]} - -data: {"id":"20260805130314d8871eee54bd48b0","created":1785906194,"object":"chat.completion.chunk","model":"glm-4.5","choices":[{"index":0,"delta":{"role":"assistant","content":"Bash echo ca"}}]} - -data: {"id":"20260805130314d8871eee54bd48b0","created":1785906194,"object":"chat.completion.chunk","model":"glm-4.5","choices":[{"index":0,"delta":{"role":"assistant","content":"pture-test"}}]} - -data: {"id":"20260805130314d8871eee54bd48b0","created":1785906194,"object":"chat.completion.chunk","model":"glm-4.5","choices":[{"index":0,"delta":{"role":"assistant","content":""}}]} - -data: {"id":"20260805130314d8871eee54bd48b0","created":1785906194,"object":"chat.completion.chunk","model":"glm-4.5","choices":[{"index":0,"finish_reason":"stop","delta":{"role":"assistant","content":""}}],"usage":{"prompt_tokens":2026,"completion_tokens":30,"total_tokens":2056,"prompt_tokens_details":{"cached_tokens":0},"completion_tokens_details":{"reasoning_tokens":23}}} - -data: [DONE] - diff --git a/tests/issue453-backstop.test.ts b/tests/issue453-backstop.test.ts new file mode 100644 index 0000000000..4f03fc0ef3 --- /dev/null +++ b/tests/issue453-backstop.test.ts @@ -0,0 +1,206 @@ +import assert from "node:assert/strict"; +import http from "node:http"; +import { once } from "node:events"; +import test from "node:test"; + +process.env.NODE_ENV = "test"; + +import { defaultConfig, type NudgeDecision } from "acp-kernel"; +import { startServer, type ProxyOptions, clampOutputBudget, estimateInputTokens, emergencyNudge } from "../src/server.ts"; +import { SessionStore, _setStoreForTest } from "../src/persist.ts"; +import { _setForTest as setRegistryForTest } from "../src/registry.ts"; + +// #453: plugin mode has no hard backstop when the agent ignores the compress +// nudge — context grows past the window and the upstream 400s on input+output. +// Two fixes land here: +// 1) clampOutputBudget/estimateInputTokens — cap the forwarded output budget +// so input+output can never exceed the window (the hard backstop). +// 2) emergencyNudge — force the nudge every turn once usage hits the +// escalation line even when the kernel's cadence gate would stay silent. + +// ---- pure-helper unit tests ------------------------------------------------- + +test("clampOutputBudget: reduces an oversized request so input+output fits", () => { + // window 262144, input ~130k, requested 131072 (the #453 repro shape). + const capped = clampOutputBudget(131_072, 130_000, 262_144); + assert.ok(capped !== undefined, "must clamp"); + const margin = Math.max(2048, Math.ceil(130_000 * 0.05)); + assert.equal(capped, 262_144 - 130_000 - margin, "cap = window - input - margin"); + assert.ok(capped < 131_072, "clamped below the requested budget"); + assert.ok((capped ?? 0) + 130_000 <= 262_144, "input + clamped output never exceeds the window"); +}); + +test("clampOutputBudget: no-op when the request already fits", () => { + assert.equal(clampOutputBudget(8_192, 10_000, 200_000), undefined); +}); + +test("clampOutputBudget: no-op when input alone nearly fills the window (preflight territory)", () => { + // input 199k of a 200k window leaves no sane output budget -> leave untouched. + assert.equal(clampOutputBudget(100_000, 199_000, 200_000), undefined); +}); + +test("estimateInputTokens: prefers the real previous-turn count when it lags high", () => { + assert.equal(estimateInputTokens([], "", [], 50_000), 50_000); +}); + +test("estimateInputTokens: counts system + tools on turn 1 (lastInputTokens=0)", () => { + const baseline = estimateInputTokens([], "", [], 0); + const withSystem = estimateInputTokens([], "x".repeat(4_000), [{ name: "tool" }], 0); + assert.ok(withSystem > baseline, "adding system + tools raises the estimate"); + assert.ok(withSystem > 900, "a 4000-char system contributes ~1000 tokens"); +}); + +function mkNudge(over: Partial> = {}): NudgeDecision { + const breakdown = { usage: 0, growth: 0, growthReference: 0, effectiveThreshold: 0, nudgeGrowthTokens: 0, growthFloor: 0, hasPendingNudge: 0, overLimit: 0, emergencyOverride: 0, pendingT1: 0, pendingT2: 0, pendingT3: 0 }; + return { shouldInject: false, reason: "test", compressibleRanges: [], contextUsage: 0, tier: null, breakdown, ...over }; +} + +test("emergencyNudge: never overrides an armed nudge", () => { + assert.equal(emergencyNudge(mkNudge({ shouldInject: true, contextUsage: 0.99, compressibleRanges: [{ startRef: "a", endRef: "b", count: 1, tokens: 1, toolPct: 0, textPct: 0 }] })), false); +}); + +test("emergencyNudge: fires at the escalation line with compressible content", () => { + const ranges = [{ startRef: "a", endRef: "b", count: 1, tokens: 1, toolPct: 0, textPct: 0 }]; + assert.equal(emergencyNudge(mkNudge({ contextUsage: 0.75, compressibleRanges: ranges })), true, "0.75 >= 0.70 default"); + assert.equal(emergencyNudge(mkNudge({ contextUsage: 0.6, compressibleRanges: ranges })), false, "below the line"); +}); + +test("emergencyNudge: respects a custom escalation line and empty ranges", () => { + const ranges = [{ startRef: "a", endRef: "b", count: 1, tokens: 1, toolPct: 0, textPct: 0 }]; + assert.equal(emergencyNudge(mkNudge({ contextUsage: 0.75, compressibleRanges: ranges }), 0.8), false, "0.75 < custom 0.8"); + assert.equal(emergencyNudge(mkNudge({ contextUsage: 0.85, compressibleRanges: ranges }), 0.8), true, "0.85 >= custom 0.8"); + assert.equal(emergencyNudge(mkNudge({ contextUsage: 0.99 })), false, "empty ranges = nothing to offer, no override"); + assert.equal(emergencyNudge(null), false); + assert.equal(emergencyNudge(undefined), false); +}); + +// ---- integration: real proxy + upstream ------------------------------------- + +function okJson(promptTokens: number): string { + return JSON.stringify({ + id: "chatcmpl-1", + object: "chat.completion", + choices: [{ index: 0, message: { role: "assistant", content: "ok" }, finish_reason: "stop" }], + usage: { prompt_tokens: promptTokens, completion_tokens: 3, total_tokens: promptTokens + 3 }, + }); +} + +interface DriveOpts { + sessionId: string; + window: number; + turn1PromptTokens: number; + turn2Body: Record; + extraHeaders?: Record; +} + +async function drive(o: DriveOpts): Promise<{ turn2Body: Record; turn2MessageCount: number }> { + const received: Record[] = []; + const upstream = http.createServer((req, res) => { + const chunks: Buffer[] = []; + req.on("data", (c: Buffer) => chunks.push(c)); + req.on("end", () => { + received.push(JSON.parse(Buffer.concat(chunks).toString("utf8"))); + res.writeHead(200, { "content-type": "application/json" }); + res.end(okJson(o.turn1PromptTokens)); + }); + }); + upstream.listen(0, "127.0.0.1"); + await once(upstream, "listening"); + const upPort = (upstream.address() as { port: number }).port; + + _setStoreForTest(new SessionStore({ enabled: false })); + setRegistryForTest({}); + const proxy = await startServer({ + port: 0, host: "127.0.0.1", upstream: "http://127.0.0.1", + routes: { [`http://127.0.0.1:${upPort}`]: { models: { m: { context: o.window } } } }, + modelContextLimit: o.window, kernelConfig: defaultConfig(o.window), + compress: { injectTool: true, injectNudge: true }, + promptCache: { routing: "auto" }, sessionHeader: "x-acp-session", + log: false, debug: false, passthrough: false, autoUpdate: false, + mitm: { enabled: false, domains: [] }, + } as ProxyOptions); + await once(proxy, "listening"); + const pPort = (proxy.address() as { port: number }).port; + + try { + const url = `http://127.0.0.1:${pPort}/bili/http://127.0.0.1:${upPort}/v1/chat/completions`; + const headers = { "content-type": "application/json", "x-acp-session": o.sessionId, ...(o.extraHeaders ?? {}) }; + const r1 = await fetch(url, { method: "POST", headers, body: JSON.stringify({ model: "m", max_tokens: 20_000, messages: [{ role: "user", content: "hello" }] }) }); + assert.equal(r1.status, 200); + await r1.text(); + const r2 = await fetch(url, { method: "POST", headers, body: JSON.stringify(o.turn2Body) }); + assert.equal(r2.status, 200); + await r2.text(); + assert.equal(received.length, 2, "both turns reached the upstream"); + const turn2Body = received[1]!; + return { turn2Body, turn2MessageCount: ((turn2Body.messages ?? []) as unknown[]).length }; + } finally { + proxy.close(); + await once(proxy, "close"); + upstream.close(); + await once(upstream, "close"); + } +} + +// A 29-message history shaped so compressible ranges exist (long text well +// outside the protected recent zone) while its CORE estimate stays modest. +function turn2Compressible(): Record[] { + const longText = "y".repeat(20_000); + const filler: { role: "user" | "assistant"; content: string }[] = []; + for (let i = 1; i <= 12; i++) { + filler.push({ role: "user", content: `q${i} ` + "f".repeat(997) }); + filler.push({ role: "assistant", content: `a${i} ` + "e".repeat(997) }); + } + return [ + { role: "user", content: "hello" }, + { role: "assistant", content: "ok" }, + { role: "user", content: "continue" }, + { role: "assistant", content: longText }, + ...filler, + { role: "user", content: "now summarize" }, + ]; +} + +test("#453 clamp: oversized max_tokens is reduced when input+system nearly fills the window", async () => { + // window 100k, requested max_tokens 40k -> reserved window 60k. The core + // conversation estimates ~48k (under 60k, so preflight skips — it ignores the + // system), but the 60k-char system pushes the TRUE input past 60k. Only the + // clamp sees the system, so it lowers max_tokens to keep input+output under 100k. + const fired = await drive({ + sessionId: "clamp-fire", + window: 100_000, + turn1PromptTokens: 5_000, + turn2Body: { + model: "m", max_tokens: 40_000, + messages: [ + { role: "system", content: "z".repeat(60_000) }, + { role: "user", content: "hi" }, + { role: "assistant", content: "y".repeat(192_000) }, + { role: "user", content: "now" }, + ], + }, + }); + const out = typeof fired.turn2Body.max_tokens === "number" ? fired.turn2Body.max_tokens : undefined; + assert.ok(out !== undefined, "forwarded body carries a numeric max_tokens"); + assert.ok(out! < 40_000, `max_tokens clamped below the 40k request (got ${out})`); + assert.ok(out! > 0, "clamped budget stays positive"); + + // Control: no oversized system -> input fits -> max_tokens passes through. + const ctrl = await drive({ + sessionId: "clamp-ctrl", + window: 100_000, + turn1PromptTokens: 5_000, + turn2Body: { model: "m", max_tokens: 40_000, messages: turn2Compressible() }, + }); + assert.equal(ctrl.turn2Body.max_tokens, 40_000, "unclamped when input fits the window"); +}); + +test("#453 escalation: nudge injected at the escalation line though the kernel cadence-stays silent", async () => { + // window 100k, max_tokens 20k -> reserved 80k. Usage = lastInputTokens/80k. + // 57.6k -> 72% (>= 0.70 escalation line, < 0.75 over-limit) vs 48k -> 60%. + // Fresh session: growth not ready, so the kernel arms nothing at either + // point; only the host-side escalation adds the trailing nudge at 72%. + const escalated = await drive({ sessionId: "esc-on", window: 100_000, turn1PromptTokens: 57_600, turn2Body: { model: "m", max_tokens: 20_000, messages: turn2Compressible() } }); + const quiet = await drive({ sessionId: "esc-off", window: 100_000, turn1PromptTokens: 48_000, turn2Body: { model: "m", max_tokens: 20_000, messages: turn2Compressible() } }); + assert.equal(escalated.turn2MessageCount, quiet.turn2MessageCount + 1, "escalation adds exactly the trailing nudge message"); +}); diff --git a/tests/launcher-plugin-mode.test.ts b/tests/launcher-plugin-mode.test.ts index 335109f594..e7890556d6 100644 --- a/tests/launcher-plugin-mode.test.ts +++ b/tests/launcher-plugin-mode.test.ts @@ -418,3 +418,40 @@ test("mcp stdio shell (codex style): BILI_CONVERSATION_ID self-register → head assert.equal(status.ok, true, "conversation bound after the model request"); assert.equal(status.pluginAgent, "mcp"); }); + +// POST /__bili/plugin/compact (#395 / PR #421): the omp agent notifies the +// proxy of an in-session native compaction; the boundary must land on the +// bound session and be consumed by the next model request. +test("plugin compact endpoint: validation, boundary marking, consumption on next turn", async () => { + const rig = await startRig(); + try { + const post = (body: string): Promise => fetch(rig.proxyUrl("/__bili/plugin/compact"), { + method: "POST", + headers: { "content-type": "application/json" }, + body, + }); + assert.equal((await post("not json")).status, 400, "invalid JSON rejected"); + assert.equal((await post("{}")).status, 400, "missing conversationId rejected"); + assert.equal((await post(JSON.stringify({ conversationId: "never-registered" }))).status, 404, "unknown conversation rejected"); + + const { listSessions } = await import("../src/session.ts"); + const before = new Set(listSessions().map((s) => s.id)); + await register(rig, "compact-conv", { agent: "pi" }); + await postModel(rig, "compact-req-1"); + const mine = listSessions().find((s) => !before.has(s.id)); + assert.ok(mine, "the model request created a new session"); + + const okRes = await post(JSON.stringify({ conversationId: "compact-conv" })); + assert.equal(okRes.status, 200, "compact accepted for a bound conversation"); + assert.equal((await okRes.json() as { ok: boolean }).ok, true); + const boundary = mine!.metadata.compactionBoundary as { pending: boolean } | undefined; + assert.equal(boundary?.pending, true, "boundary marked pending on the session"); + + await postModel(rig, "compact-req-1"); + const consumed = mine!.metadata.compactionBoundary as { pending: boolean; archivedBlocks?: string[] }; + assert.equal(consumed.pending, false, "boundary consumed by the next model request"); + assert.ok(Array.isArray(consumed.archivedBlocks), "archivedBlocks recorded"); + } finally { + await rig.closeAll(); + } +}); diff --git a/tests/launcher.test.ts b/tests/launcher.test.ts index 2a4ac54032..e3835dfeb1 100644 --- a/tests/launcher.test.ts +++ b/tests/launcher.test.ts @@ -1,7 +1,8 @@ import { test } from "node:test"; import assert from "node:assert/strict"; import fs from "node:fs"; -import type { PathLike, SymlinkType } from "node:fs"; +import type { PathLike } from "node:fs"; +type SymlinkKind = "dir" | "file" | "junction"; import net from "node:net"; import os from "node:os"; import path from "node:path"; @@ -938,36 +939,51 @@ test("preparePiHttpRewrite: rewrites matching provider, leaves others, symlinks assert.equal(out.providers.b.baseUrl, "https://secure.example.com"); assert.equal(fs.lstatSync(path.join(tmp!, "auth.json")).isSymbolicLink(), true); assert.equal(fs.realpathSync(path.join(tmp!, "auth.json")), path.join(home, "auth.json")); + const settings = JSON.parse(fs.readFileSync(path.join(tmp!, "settings.json"), "utf8")); + assert.equal(settings.compaction.enabled, false, "pi native compaction disabled"); } finally { if (tmp) fs.rmSync(tmp, { recursive: true, force: true }); fs.rmSync(home, { recursive: true, force: true }); } }); -test("preparePiHttpRewrite: returns undefined when no rewrites", () => { - assert.equal(preparePiHttpRewrite("/whatever", "http://127.0.0.1:8787", [], []), undefined); +test("preparePiHttpRewrite: non-existent home → undefined", () => { + assert.equal(preparePiHttpRewrite("/nonexistent-bili-pi-home-xyz", "http://127.0.0.1:8787", [], []), undefined); }); -test("preparePiHttpRewrite: returns undefined when models.json missing or unparseable", () => { - const home = fs.mkdtempSync(path.join(os.tmpdir(), "bili-pihome-")); - const rw: HttpRewrite[] = [{ key: "a", realUpstream: "http://x/v1" }]; +test("preparePiHttpRewrite: no rewrites → overlay built, pi native compaction disabled, models.json copied verbatim", () => { + const home = fs.realpathSync(fs.mkdtempSync(path.join(os.tmpdir(), "bili-pihome-"))); + fs.writeFileSync(path.join(home, "models.json"), JSON.stringify({ providers: { a: { baseUrl: "https://keep.example.com" } } })); try { - assert.equal(preparePiHttpRewrite(home, "http://127.0.0.1:8787", rw, []), undefined); - fs.writeFileSync(path.join(home, "models.json"), "not-json{"); - assert.equal(preparePiHttpRewrite(home, "http://127.0.0.1:8787", rw, []), undefined); + const overlay = preparePiHttpRewrite(home, "http://127.0.0.1:8787", [], []); + assert.ok(typeof overlay === "string" && overlay.length > 0); + const settings = JSON.parse(fs.readFileSync(path.join(overlay!, "settings.json"), "utf8")); + assert.equal(settings.compaction.enabled, false, "pi native compaction disabled"); + const out = JSON.parse(fs.readFileSync(path.join(overlay!, "models.json"), "utf8")); + assert.equal(out.providers.a.baseUrl, "https://keep.example.com", "no rewrite → baseUrl untouched"); } finally { fs.rmSync(home, { recursive: true, force: true }); + fs.rmSync(`${home}-bili`, { recursive: true, force: true }); } }); -test("preparePiHttpRewrite: returns undefined when models.json is not an object", () => { - const home = fs.mkdtempSync(path.join(os.tmpdir(), "bili-pihome-")); - const rw: HttpRewrite[] = [{ key: "a", realUpstream: "http://x/v1" }]; +test("preparePiHttpRewrite: preserves other settings.json keys, disables only compaction", () => { + const home = fs.realpathSync(fs.mkdtempSync(path.join(os.tmpdir(), "bili-pihome-"))); + fs.writeFileSync( + path.join(home, "settings.json"), + JSON.stringify({ packages: ["/opt/pi-plugin"], theme: "dark", compaction: { reserveTokens: 9999 } }), + ); try { - fs.writeFileSync(path.join(home, "models.json"), JSON.stringify([1, 2, 3])); - assert.equal(preparePiHttpRewrite(home, "http://127.0.0.1:8787", rw, []), undefined); + const overlay = preparePiHttpRewrite(home, "http://127.0.0.1:8787", [], []); + assert.ok(typeof overlay === "string"); + const settings = JSON.parse(fs.readFileSync(path.join(overlay!, "settings.json"), "utf8")); + assert.equal(settings.compaction.enabled, false, "compaction disabled"); + assert.equal(settings.compaction.reserveTokens, 9999, "other compaction keys preserved"); + assert.deepEqual(settings.packages, ["/opt/pi-plugin"], "packages preserved"); + assert.equal(settings.theme, "dark", "theme preserved"); } finally { fs.rmSync(home, { recursive: true, force: true }); + fs.rmSync(`${home}-bili`, { recursive: true, force: true }); } }); @@ -1262,7 +1278,7 @@ test("prepareOmpHttpRewrite: EPERM fallback links dir→junction, file→hardlin fs.writeFileSync(path.join(home, "settings.json"), "real-settings"); const rw = [{ key: "a", realUpstream: "http://example.com/v1" }]; const realSymlinkSync = fs.symlinkSync; - fs.symlinkSync = ((target: PathLike, link: PathLike, type?: SymlinkType) => { + fs.symlinkSync = ((target: PathLike, link: PathLike, type?: SymlinkKind) => { if (type === "junction") return realSymlinkSync(target, link, "junction"); throw denyErr("EPERM"); }) as typeof fs.symlinkSync; @@ -1292,7 +1308,7 @@ test("prepareOmpHttpRewrite: EPERM fallback copies file when hardlink also denie const rw = [{ key: "a", realUpstream: "http://example.com/v1" }]; const realSymlinkSync = fs.symlinkSync; const realLinkSync = fs.linkSync; - fs.symlinkSync = ((target: PathLike, link: PathLike, type?: SymlinkType) => { + fs.symlinkSync = ((target: PathLike, link: PathLike, type?: SymlinkKind) => { if (type === "junction") return realSymlinkSync(target, link, "junction"); throw denyErr("EPERM"); }) as typeof fs.symlinkSync; @@ -1404,7 +1420,7 @@ test("prepareOmpHttpRewrite: write-through hardlink is re-pointed, never merged const rw = [{ key: "a", realUpstream: "http://example.com/v1" }]; const overlay = `${home}-bili`; const realSymlinkSync = fs.symlinkSync; - fs.symlinkSync = ((target: PathLike, link: PathLike, type?: SymlinkType) => { + fs.symlinkSync = ((target: PathLike, link: PathLike, type?: SymlinkKind) => { if (type === "junction") return realSymlinkSync(target, link, "junction"); throw denyErr("EPERM"); }) as typeof fs.symlinkSync; @@ -1427,6 +1443,91 @@ test("prepareOmpHttpRewrite: write-through hardlink is re-pointed, never merged } }); +test("prepareOmpHttpRewrite: SQLite set adjudicated by main db — no cross-side WAL splice (#381)", () => { + const home = fs.realpathSync(fs.mkdtempSync(path.join(os.tmpdir(), "bili-omphome-"))); + fs.writeFileSync(path.join(home, "models.yml"), ["providers:", " a:", " baseUrl: http://example.com/v1"].join("\n")); + const now = Date.now(); + fs.writeFileSync(path.join(home, "agent.db"), "real-db"); + fs.writeFileSync(path.join(home, "agent.db-wal"), "real-wal"); + const overlay = `${home}-bili`; + fs.mkdirSync(overlay); + fs.writeFileSync(path.join(overlay, "agent.db"), "overlay-db"); + fs.writeFileSync(path.join(overlay, "agent.db-wal"), "overlay-wal"); + // main db: real newer than overlay; wal: overlay newer than real — the + // cross-generational splice per-member mtime adjudication would produce. + fs.utimesSync(path.join(home, "agent.db"), new Date(now - 10000), new Date(now - 10000)); + fs.utimesSync(path.join(home, "agent.db-wal"), new Date(now - 50000), new Date(now - 50000)); + fs.utimesSync(path.join(overlay, "agent.db"), new Date(now - 60000), new Date(now - 60000)); + fs.utimesSync(path.join(overlay, "agent.db-wal"), new Date(now - 5000), new Date(now - 5000)); + const rw = [{ key: "a", realUpstream: "http://example.com/v1" }]; + let tmp: string | undefined; + try { + tmp = prepareOmpHttpRewrite(home, "http://127.0.0.1:8787", rw, []); + assert.equal(tmp, overlay); + assert.equal(fs.readFileSync(path.join(home, "agent.db"), "utf8"), "real-db", "real main db (newer) wins"); + assert.equal(fs.readFileSync(path.join(home, "agent.db-wal"), "utf8"), "real-wal", "wal comes from the SAME side as the db (no splice)"); + assert.equal(fs.readFileSync(path.join(home, "agent.db.bili-conflict"), "utf8"), "overlay-db", "losing overlay db preserved"); + assert.equal(fs.readFileSync(path.join(home, "agent.db-wal.bili-conflict"), "utf8"), "overlay-wal", "losing overlay wal preserved"); + } finally { + if (tmp) fs.rmSync(tmp, { recursive: true, force: true }); + fs.rmSync(home, { recursive: true, force: true }); + } +}); + +test("prepareOmpHttpRewrite: SQLite -journal sidecar moves with the set, not separately (#381)", () => { + const home = fs.realpathSync(fs.mkdtempSync(path.join(os.tmpdir(), "bili-omphome-"))); + fs.writeFileSync(path.join(home, "models.yml"), ["providers:", " a:", " baseUrl: http://example.com/v1"].join("\n")); + const now = Date.now(); + fs.writeFileSync(path.join(home, "agent.db"), "real-db"); + const overlay = `${home}-bili`; + fs.mkdirSync(overlay); + fs.writeFileSync(path.join(overlay, "agent.db"), "overlay-db"); + fs.writeFileSync(path.join(overlay, "agent.db-journal"), "overlay-journal"); + // real main db newer than overlay's; the overlay journal must NOT be + // spliced in as an active file against the real (newer) db. + fs.utimesSync(path.join(home, "agent.db"), new Date(now - 10000), new Date(now - 10000)); + fs.utimesSync(path.join(overlay, "agent.db"), new Date(now - 60000), new Date(now - 60000)); + fs.utimesSync(path.join(overlay, "agent.db-journal"), new Date(now - 5000), new Date(now - 5000)); + const rw = [{ key: "a", realUpstream: "http://example.com/v1" }]; + let tmp: string | undefined; + try { + tmp = prepareOmpHttpRewrite(home, "http://127.0.0.1:8787", rw, []); + assert.equal(tmp, overlay); + assert.equal(fs.readFileSync(path.join(home, "agent.db"), "utf8"), "real-db", "real main db (newer) wins"); + assert.equal(fs.existsSync(path.join(home, "agent.db-journal")), false, "journal NOT spliced in as an active file"); + assert.equal(fs.readFileSync(path.join(home, "agent.db-journal.bili-conflict"), "utf8"), "overlay-journal", "journal preserved as a conflict (same side as the losing db)"); + } finally { + if (tmp) fs.rmSync(tmp, { recursive: true, force: true }); + fs.rmSync(home, { recursive: true, force: true }); + } +}); + +test("prepareOmpHttpRewrite: conflict rename never clobbers a previous round's loser (#381)", () => { + const home = fs.realpathSync(fs.mkdtempSync(path.join(os.tmpdir(), "bili-omphome-"))); + fs.writeFileSync(path.join(home, "models.yml"), ["providers:", " a:", " baseUrl: http://example.com/v1"].join("\n")); + const now = Date.now(); + fs.writeFileSync(path.join(home, "agent.db"), "real-db"); + fs.writeFileSync(path.join(home, "agent.db.bili-conflict"), "previous-loser"); + const overlay = `${home}-bili`; + fs.mkdirSync(overlay); + fs.writeFileSync(path.join(overlay, "agent.db"), "overlay-db-v1"); + fs.writeFileSync(path.join(overlay, "agent.db-wal"), "overlay-wal-v1"); + fs.utimesSync(path.join(home, "agent.db"), new Date(now - 10000), new Date(now - 10000)); + fs.utimesSync(path.join(overlay, "agent.db"), new Date(now - 60000), new Date(now - 60000)); + const rw = [{ key: "a", realUpstream: "http://example.com/v1" }]; + let tmp: string | undefined; + try { + tmp = prepareOmpHttpRewrite(home, "http://127.0.0.1:8787", rw, []); + assert.equal(tmp, overlay); + assert.equal(fs.readFileSync(path.join(home, "agent.db"), "utf8"), "real-db", "real main db (newer) wins"); + assert.equal(fs.readFileSync(path.join(home, "agent.db.bili-conflict"), "utf8"), "previous-loser", "previous round's loser NOT overwritten"); + assert.equal(fs.readFileSync(path.join(home, "agent.db.bili-conflict.1"), "utf8"), "overlay-db-v1", "this round's loser gets a suffixed name"); + } finally { + if (tmp) fs.rmSync(tmp, { recursive: true, force: true }); + fs.rmSync(home, { recursive: true, force: true }); + } +}); + test("readOpencodeConfig: reads provider baseURLs from opencode.json", () => { const dir = fs.mkdtempSync(path.join(os.tmpdir(), "oc-cfg-")); try { diff --git a/tests/loop-responses-orphan.test.ts b/tests/loop-responses-orphan.test.ts new file mode 100644 index 0000000000..1993b9836b --- /dev/null +++ b/tests/loop-responses-orphan.test.ts @@ -0,0 +1,156 @@ +import { test } from "node:test"; +import assert from "node:assert/strict"; +import type { Config, CoreMessage } from "acp-kernel"; +import { createCore, createInitialState } from "acp-kernel"; +import type { Session } from "../src/session.ts"; +import { runCompressLoop, createResponsesAdapter } from "../src/loop/index.ts"; +import { buildCompressSystemPrompt } from "../src/compress-tool.ts"; + +// #440: responses adapter must not emit response.failed with no preceding +// response.created (orphan stream crashes codex; same class as #413). + +const BODY = { model: "gpt-5", input: [], stream: true }; + +function sse(event: string, obj: Record): string { + return `event: ${event}\ndata: ${JSON.stringify({ type: event, ...obj })}\n\n`; +} + +function makeCtx(id: string) { + return { + core: createCore(), + config: { modelContextLimit: 200000 } as Config, + messages: [] as CoreMessage[], + session: { + id, + meta: {}, + stats: { requests: 0, tokensSaved: 0, inputTokens: 0, cachedTokens: 0, outputTokens: 0, cacheSamples: 0, lastInputTokens: 0, contextTokens: 0 }, + metadata: {}, + state: createInitialState(), + createdAt: Date.now(), + lastSeen: Date.now(), + blockContents: new Map(), + inFlight: 0, + persisted: false, + } as unknown as Session, + log: () => {}, + protocol: "responses" as const, + }; +} + +async function drain(stream: ReadableStream, ctx: ReturnType): Promise { + const chunks: Buffer[] = []; + for await (const chunk of runCompressLoop( + stream, + ctx, + BODY, + { url: "http://mock", headers: {} }, + createResponsesAdapter(), + buildCompressSystemPrompt(), + )) { + chunks.push(chunk); + } + return Buffer.concat(chunks).toString("utf8"); +} + +interface OutEvent { event: string; data: Record } + +function parseOut(out: string): OutEvent[] { + const events: OutEvent[] = []; + for (const block of out.split("\n\n")) { + let event = ""; + const dataLines: string[] = []; + for (const line of block.split("\n")) { + if (line.startsWith("event:")) event = line.slice(6).trim(); + else if (line.startsWith("data:")) dataLines.push(line.slice(5).replace(/^ /, "")); + } + if (!event || dataLines.length === 0) continue; + try { + events.push({ event, data: JSON.parse(dataLines.join("\n")) as Record }); + } catch { } + } + return events; +} + +function assertCreatedBeforeFailed(events: OutEvent[]): void { + const failedIdx = events.findIndex((e) => e.event === "response.failed"); + assert.ok(failedIdx >= 0, "stream has a response.failed"); + const createdIdx = events.findIndex((e) => e.event === "response.created"); + assert.ok(createdIdx >= 0, "stream has a response.created"); + assert.ok(createdIdx < failedIdx, `response.created (idx ${createdIdx}) precedes response.failed (idx ${failedIdx})`); +} + +function mockFetch(handler: () => Response): { calls: () => number; restore: () => void } { + let n = 0; + const orig = globalThis.fetch; + globalThis.fetch = (async () => { + n++; + return handler(); + }) as typeof fetch; + return { calls: () => n, restore: () => { globalThis.fetch = orig; } }; +} + +test("#440 T1: 0-event EOF → first event is response.created, failed preceded by created", async () => { + process.env.BILI_REPLAY_RETRY_MAX = "1"; + const ctx = makeCtx("resp-orphan-t1"); + const mock = mockFetch(() => new Response('{"error":"boom"}', { status: 500, headers: { "content-type": "application/json" } })); + try { + const out = await drain(new Response("", { status: 200 }).body!, ctx); + assert.equal(mock.calls(), 1, "zero-side-effect truncation retried exactly once (responses adapter too)"); + const events = parseOut(out); + assert.ok(events.length > 0, "stream non-empty"); + assert.equal(events[0].event, "response.created", `first event must be response.created (got ${events[0].event})`); + assertCreatedBeforeFailed(events); + } finally { + mock.restore(); + delete process.env.BILI_REPLAY_RETRY_MAX; + } +}); + +test("#440 T2: truncation after created forwarded → single created, failed references same id, no retry", async () => { + const round1 = sse("response.created", { response: { id: "resp_1", status: "in_progress", output: [] } }); + const ctx = makeCtx("resp-orphan-t2"); + const mock = mockFetch(() => new Response("{}", { status: 200 })); + try { + const out = await drain(new Response(round1, { status: 200 }).body!, ctx); + assert.equal(mock.calls(), 0, "no retry: created already reached the client"); + const events = parseOut(out); + const created = events.filter((e) => e.event === "response.created"); + assert.equal(created.length, 1, `exactly one response.created (no synthetic duplicate); got ${created.length}`); + assertCreatedBeforeFailed(events); + const failed = events.find((e) => e.event === "response.failed")!; + const failedId = (failed.data.response as Record)?.id; + assert.equal(failedId, "resp_1", "failed references the forwarded created's id"); + } finally { + mock.restore(); + } +}); + +test("#440 T4: 0-event EOF → retry succeeds → clean completion, no failed event", async () => { + const created = sse("response.created", { response: { id: "resp_retry", status: "in_progress", output: [] } }); + const item = sse("response.output_item.added", { output_index: 0, item: { type: "message", id: "msg_1", role: "assistant", status: "in_progress", content: [] } }); + const textDelta = sse("response.output_text.delta", { item_id: "msg_1", output_index: 0, content_index: 0, delta: "RETRY OK" }); + const textDone = sse("response.output_text.done", { item_id: "msg_1", output_index: 0, content_index: 0, text: "RETRY OK" }); + const itemDone = sse("response.output_item.done", { output_index: 0, item: { type: "message", id: "msg_1", role: "assistant", status: "completed", content: [{ type: "output_text", text: "RETRY OK", annotations: [] }] } }); + const completed = sse("response.completed", { response: { id: "resp_retry", status: "completed", output: [] } }); + const mock = mockFetch(() => new Response(created + item + textDelta + textDone + itemDone + completed, { status: 200 })); + const ctx = makeCtx("resp-orphan-t4"); + try { + const out = await drain(new Response("", { status: 200 }).body!, ctx); + assert.equal(mock.calls(), 1, "retried once"); + assert.ok(out.includes("RETRY OK"), "retried round's content delivered"); + assert.ok(!out.includes("response.failed"), "no failed event on successful retry"); + } finally { + mock.restore(); + } +}); + +test("#440 T3: emitError with no created forwarded → synthesizes created before failed", () => { + const adapter = createResponsesAdapter(); + const out = adapter.emitError("upstream stream truncated").toString("utf8"); + const events = parseOut(out); + assert.equal(events[0].event, "response.created", "emitError leads with a synthesized response.created"); + assertCreatedBeforeFailed(events); + const createdId = (events[0].data.response as Record)?.id; + const failedId = (events.find((e) => e.event === "response.failed")!.data.response as Record)?.id; + assert.equal(createdId, failedId, "synthesized created and failed share an id"); +}); diff --git a/tests/loop-truncation-reasoning.test.ts b/tests/loop-truncation-reasoning.test.ts new file mode 100644 index 0000000000..a8c2ff507d --- /dev/null +++ b/tests/loop-truncation-reasoning.test.ts @@ -0,0 +1,85 @@ +import { test } from "node:test"; +import assert from "node:assert/strict"; +import type { Config, CoreMessage } from "acp-kernel"; +import { createCore, createInitialState } from "acp-kernel"; +import type { Session } from "../src/session.ts"; +import { runCompressLoop, createOpenaiAdapter } from "../src/loop/index.ts"; +import { buildCompressSystemPrompt } from "../src/compress-tool.ts"; + +// #413 review: the truncation-retry predicate must treat live-forwarded +// reasoning chunks as client-visible side effects. The openai adapter yields +// reasoning events with `raw` (adapter-openai.ts:328) and no meta precedes a +// pure reasoning_content chunk — if those yields don't set forwardedAny, a +// round-1 truncation after reasoning-only output retries and the client sees +// the partial reasoning TWICE. + +const OPENAI_BODY = { model: "glm", messages: [], stream: true, max_tokens: 10 }; + +function makeCtx(id: string): { + core: ReturnType; + config: Config; + messages: CoreMessage[]; + session: Session; + log: (m: string) => void; +} { + return { + core: createCore(), + config: { modelContextLimit: 200000 } as Config, + messages: [], + session: { + id, + meta: {}, + stats: { requests: 0, tokensSaved: 0, inputTokens: 0, cachedTokens: 0, outputTokens: 0, cacheSamples: 0, lastInputTokens: 0, contextTokens: 0 }, + metadata: {}, + state: createInitialState(), + createdAt: Date.now(), + lastSeen: Date.now(), + blockContents: new Map(), + inFlight: 0, + persisted: false, + }, + log: () => {}, + }; +} + +async function drain(stream: ReadableStream, ctx: ReturnType): Promise { + const adapter = createOpenaiAdapter(OPENAI_BODY); + const chunks: Buffer[] = []; + for await (const chunk of runCompressLoop(stream, ctx, OPENAI_BODY, { url: "http://mock", headers: {} }, adapter, buildCompressSystemPrompt())) { + chunks.push(chunk); + } + return Buffer.concat(chunks).toString("utf8"); +} + +function mockFetch(handler: () => Response): { calls: () => number; restore: () => void } { + let n = 0; + const orig = globalThis.fetch; + globalThis.fetch = (async () => { + n++; + return handler(); + }) as typeof fetch; + return { calls: () => n, restore: () => { globalThis.fetch = orig; } }; +} + +const sse = (obj: unknown): string => `data: ${JSON.stringify(obj)}\n\n`; + +test("truncation after live reasoning chunks must not retry (reasoning already reached the client)", async () => { + const partial = sse({ id: "c1", choices: [{ index: 0, delta: { reasoning_content: "partial thought before EOF" } }] }); + const full = sse({ id: "c2", choices: [{ index: 0, delta: { reasoning_content: "second attempt reasoning" } }] }) + + sse({ id: "c2", choices: [{ index: 0, delta: { content: "FINAL ANSWER" } }] }) + + sse({ id: "c2", choices: [{ index: 0, delta: {}, finish_reason: "stop" }] }) + + "data: [DONE]\n\n"; + const mock = mockFetch(() => new Response(full, { status: 200 })); + const ctx = makeCtx("trunc-reasoning"); + try { + const out = await drain(new Response(partial, { status: 200 }).body!, ctx); + assert.equal(mock.calls(), 0, "no retry: reasoning bytes were already forwarded to the client"); + const firstIdx = out.indexOf("partial thought before EOF"); + assert.ok(firstIdx >= 0, "partial reasoning was forwarded to the client"); + assert.equal(out.indexOf("partial thought before EOF", firstIdx + 1), -1, "partial reasoning appears exactly once (no duplicated attempt)"); + assert.ok(!out.includes("second attempt reasoning"), "retry stream content never fetched"); + assert.ok(out.includes("upstream stream truncated"), "truncation surfaced to client"); + } finally { + mock.restore(); + } +}); diff --git a/tests/loop-truncation.test.ts b/tests/loop-truncation.test.ts new file mode 100644 index 0000000000..f788b16516 --- /dev/null +++ b/tests/loop-truncation.test.ts @@ -0,0 +1,230 @@ +import { test } from "node:test"; +import assert from "node:assert/strict"; +import type { Config, CoreMessage } from "acp-kernel"; +import { createCore, createInitialState } from "acp-kernel"; +import type { Session } from "../src/session.ts"; +import { runCompressLoop, createAnthropicAdapter } from "../src/loop/index.ts"; +import { buildCompressSystemPrompt } from "../src/compress-tool.ts"; +import { log as loggerLog, setLogCapture } from "../src/logger.ts"; + +// #413: upstream truncation (200 + early SSE EOF) — zero-side-effect retry, +// well-formed Anthropic error stream (message_start before content blocks, +// all blocks closed), single-point logging. + +const ANTHROPIC_BODY = { model: "claude", messages: [], stream: true, max_tokens: 10 }; + +function makeCtx(id: string, logSink?: string[], wireThroughLogger = false): { + core: ReturnType; + config: Config; + messages: CoreMessage[]; + session: Session; + log: (m: string) => void; +} { + return { + core: createCore(), + config: { modelContextLimit: 200000 } as Config, + messages: [], + session: { + id, + meta: {}, + stats: { requests: 0, tokensSaved: 0, inputTokens: 0, cachedTokens: 0, outputTokens: 0, cacheSamples: 0, lastInputTokens: 0, contextTokens: 0 }, + metadata: {}, + state: createInitialState(), + createdAt: Date.now(), + lastSeen: Date.now(), + blockContents: new Map(), + inFlight: 0, + persisted: false, + }, + log: (m: string) => { + logSink?.push(m); + if (wireThroughLogger) loggerLog("info", `[${id}] ${m}`); + }, + }; +} + +async function drain(stream: ReadableStream, ctx: ReturnType): Promise { + const adapter = createAnthropicAdapter(ANTHROPIC_BODY); + const chunks: Buffer[] = []; + for await (const chunk of runCompressLoop(stream, ctx, ANTHROPIC_BODY, { url: "http://mock", headers: {} }, adapter, buildCompressSystemPrompt())) { + chunks.push(chunk); + } + return Buffer.concat(chunks).toString("utf8"); +} + +function mockFetch(handler: (call: number) => Response): { calls: () => number; restore: () => void } { + let n = 0; + const orig = globalThis.fetch; + globalThis.fetch = (async () => { + n++; + return handler(n); + }) as typeof fetch; + return { calls: () => n, restore: () => { globalThis.fetch = orig; } }; +} + +const ev = (type: string, data: unknown): string => `event: ${type}\ndata: ${JSON.stringify(data)}\n\n`; + +const MESSAGE_START = ev("message_start", { + type: "message_start", + message: { + id: "msg_1", type: "message", role: "assistant", model: "claude", + content: [], stop_reason: null, stop_sequence: null, + usage: { input_tokens: 5, output_tokens: 0 }, + }, +}); +const textStart = (index: number) => ev("content_block_start", { type: "content_block_start", index, content_block: { type: "text", text: "" } }); +const textDelta = (index: number, text: string) => ev("content_block_delta", { type: "content_block_delta", index, delta: { type: "text_delta", text } }); +const textBlock = (index: number, text: string) => textStart(index) + textDelta(index, text) + blockStop(index); +const thinkingStart = (index: number) => ev("content_block_start", { type: "content_block_start", index, content_block: { type: "thinking", thinking: "" } }); +const thinkingDelta = (index: number, thinking: string) => ev("content_block_delta", { type: "content_block_delta", index, delta: { type: "thinking_delta", thinking } }); +const blockStop = (index: number) => ev("content_block_stop", { type: "content_block_stop", index }); +const toolUseBlock = (index: number, id: string, name: string, input: string) => + ev("content_block_start", { type: "content_block_start", index, content_block: { type: "tool_use", id, name, input: {} } }) + + ev("content_block_delta", { type: "content_block_delta", index, delta: { type: "input_json_delta", partial_json: input } }) + + blockStop(index); +const MESSAGE_DELTA = (stopReason = "end_turn") => ev("message_delta", { type: "message_delta", delta: { stop_reason: stopReason, stop_sequence: null }, usage: { input_tokens: 5, output_tokens: 3 } }); +const MESSAGE_STOP = ev("message_stop", { type: "message_stop" }); + +interface SseEvent { type: string; data: Record; } + +function parseSse(s: string): SseEvent[] { + const out: SseEvent[] = []; + for (const block of s.split("\n\n")) { + if (!block.trim()) continue; + let type = ""; + const dataLines: string[] = []; + for (const line of block.split("\n")) { + if (line.startsWith("event:")) type = line.slice(6).trim(); + else if (line.startsWith("data:")) dataLines.push(line.slice(5).replace(/^ /, "")); + } + if (!type) continue; + const jsonStr = dataLines.join("\n").trim(); + out.push({ type, data: jsonStr ? (JSON.parse(jsonStr) as Record) : {} }); + } + return out; +} + +// Anthropic stream protocol invariants: message_start first, every +// content_block_start matched by a stop before the terminal, message_stop last. +function assertAnthropicStreamSchema(s: string): void { + const events = parseSse(s); + assert.ok(events.length > 0, "stream non-empty"); + assert.equal(events[0].type, "message_start", `first event must be message_start (got ${events[0].type})`); + const open = new Map(); + let sawTerminal = false; + for (const e of events) { + if (e.type === "content_block_start") { + const idx = e.data.index as number; + const cb = (e.data.content_block ?? {}) as Record; + assert.ok(!open.has(idx), `block ${idx} started twice`); + open.set(idx, String(cb.type)); + } else if (e.type === "content_block_stop") { + const idx = e.data.index as number; + assert.ok(open.has(idx), `content_block_stop for never-started block ${idx}`); + open.delete(idx); + } else if (e.type === "message_delta" || e.type === "message_stop") { + assert.equal(open.size, 0, `all blocks closed before ${e.type} (still open: ${[...open.entries()].map(([i, t]) => `${i}:${t}`).join(", ")})`); + sawTerminal = true; + } + } + assert.ok(sawTerminal, "stream has message_delta/message_stop terminal"); + assert.equal(events[events.length - 1].type, "message_stop", "last event is message_stop"); +} + +test("#413 T1: 0-event EOF → retried once; failed retry → well-formed error stream, single log line", async () => { + process.env.BILI_REPLAY_RETRY_MAX = "1"; + const captured: { level: string; msg: string }[] = []; + setLogCapture((level, msg) => captured.push({ level, msg })); + const logSink: string[] = []; + const ctx = makeCtx("trunc-t1", logSink, true); + const mock = mockFetch(() => new Response('{"error":"boom"}', { status: 500, headers: { "content-type": "application/json" } })); + try { + const out = await drain(new Response("", { status: 200 }).body!, ctx); + assert.equal(mock.calls(), 1, "zero-side-effect truncation retried exactly once"); + assertAnthropicStreamSchema(out); + assert.ok(out.includes("upstream stream truncated"), "truncation surfaced to client"); + const truncCaptured = captured.filter((l) => l.msg.includes("upstream stream truncated")); + assert.equal(truncCaptured.length, 1, `truncation logged exactly once (got ${truncCaptured.length}): ${JSON.stringify(truncCaptured)}`); + assert.equal(truncCaptured[0].level, "info", "the single line is the ctx.log info line — no global error duplicate"); + assert.equal(logSink.filter((l) => l.includes("upstream stream truncated")).length, 1, "ctx.log called once for the truncation event"); + } finally { + mock.restore(); + setLogCapture(null); + delete process.env.BILI_REPLAY_RETRY_MAX; + } +}); + +test("#413 T2: 0-event EOF → retry succeeds → one clean stream, no error surfaced", async () => { + const round2 = [MESSAGE_START, textBlock(0, "Hello retry"), MESSAGE_DELTA(), MESSAGE_STOP].join(""); + const mock = mockFetch(() => new Response(round2, { status: 200 })); + const ctx = makeCtx("trunc-t2"); + try { + const out = await drain(new Response("", { status: 200 }).body!, ctx); + assert.equal(mock.calls(), 1, "retried once"); + assertAnthropicStreamSchema(out); + assert.ok(out.includes("Hello retry"), "retried round's content delivered"); + assert.ok(!out.includes("upstream stream truncated"), "no error surfaced on successful retry"); + } finally { + mock.restore(); + } +}); + +test("#413 T3: truncation with open thinking block → block closed, no retry (content already forwarded)", async () => { + const round1 = [MESSAGE_START, thinkingStart(0), thinkingDelta(0, "let me think")].join(""); + const mock = mockFetch(() => new Response("{}", { status: 200 })); + const ctx = makeCtx("trunc-t3"); + try { + const out = await drain(new Response(round1, { status: 200 }).body!, ctx); + assert.equal(mock.calls(), 0, "no retry when content already reached the client"); + assertAnthropicStreamSchema(out); + assert.ok(out.includes("upstream stream truncated"), "truncation surfaced to client"); + } finally { + mock.restore(); + } +}); + +test("#413 T4: truncation with open text block → block closed before error block", async () => { + const round1 = [MESSAGE_START, textStart(0), textDelta(0, "partial")].join(""); + const mock = mockFetch(() => new Response("{}", { status: 200 })); + const ctx = makeCtx("trunc-t4"); + try { + const out = await drain(new Response(round1, { status: 200 }).body!, ctx); + assert.equal(mock.calls(), 0, "no retry when content already reached the client"); + assertAnthropicStreamSchema(out); + assert.ok(out.includes("partial"), "already-forwarded text preserved"); + assert.ok(out.includes("upstream stream truncated"), "truncation surfaced to client"); + } finally { + mock.restore(); + } +}); + +test("#413 T5: retry itself truncated → no second retry (one per request)", async () => { + const mock = mockFetch(() => new Response("", { status: 200 })); + const ctx = makeCtx("trunc-t5"); + try { + const out = await drain(new Response("", { status: 200 }).body!, ctx); + assert.equal(mock.calls(), 1, "exactly one retry per request"); + assertAnthropicStreamSchema(out); + assert.ok(out.includes("upstream stream truncated"), "truncation surfaced after exhausted retry"); + } finally { + mock.restore(); + } +}); + +test("#413 T6: round-2 truncation (after proxy tool) retries with the round-2 body", async () => { + const round1 = [MESSAGE_START, toolUseBlock(0, "toolu_1", "acp_status", "{}"), MESSAGE_DELTA("tool_use"), MESSAGE_STOP].join(""); + const round2 = [MESSAGE_START, textBlock(0, "after retry"), MESSAGE_DELTA(), MESSAGE_STOP].join(""); + const mock = mockFetch((call) => + call === 1 ? new Response("", { status: 200 }) : new Response(round2, { status: 200 }), + ); + const ctx = makeCtx("trunc-t6"); + try { + const out = await drain(new Response(round1, { status: 200 }).body!, ctx); + assert.equal(mock.calls(), 2, "re-request + one truncation retry"); + assert.ok(out.includes("[ACP]"), "proxy tool marker surfaced to client"); + assert.ok(out.includes("after retry"), "retried round-2 content delivered"); + assertAnthropicStreamSchema(out); + } finally { + mock.restore(); + } +}); diff --git a/tests/plugin-passthrough-tag-strip-chat.test.ts b/tests/plugin-passthrough-tag-strip-chat.test.ts index 11b1e994b3..07079efdc9 100644 --- a/tests/plugin-passthrough-tag-strip-chat.test.ts +++ b/tests/plugin-passthrough-tag-strip-chat.test.ts @@ -68,7 +68,7 @@ test("plugin chat passthrough strips render tags from delta.content", async () = chatChunk({}, { choices: [{ index: 0, delta: {}, finish_reason: "stop" }], usage: { prompt_tokens: 100, completion_tokens: 5 } }), DONE, ]; - await pipePluginChatWithStrip(streamOf(events), res, session, "openai"); + await pipePluginChatWithStrip(streamOf(events), res, "openai", session); const text = out.join(""); assert.ok(!text.includes("m00042"), "render tag content must not pass through"); assert.ok(text.includes("leading trailing"), "non-tag prose survives"); @@ -85,7 +85,7 @@ test("plugin chat passthrough strips tags split across deltas and flushes tail b chatChunk({ content: `0042${TAG_CLOSE} tail` }), DONE, ]; - await pipePluginChatWithStrip(streamOf(events), res, session, "openai"); + await pipePluginChatWithStrip(streamOf(events), res, "openai", session); const text = out.join(""); assert.ok(!text.includes("m00042"), "split tag fully swallowed"); assert.ok(text.includes("ok "), "first delta prose passes"); @@ -104,7 +104,7 @@ test("plugin chat passthrough strips echo from reasoning_content too", async () chatChunk({ reasoning_content: `think ${TAG_OPEN}m00007${TAG_CLOSE} done` }), DONE, ]; - await pipePluginChatWithStrip(streamOf(events), res, session, "openai"); + await pipePluginChatWithStrip(streamOf(events), res, "openai", session); const text = out.join(""); assert.ok(!text.includes("m00007"), "reasoning echo stripped"); assert.ok(text.includes("think done"), "reasoning prose survives"); @@ -120,7 +120,7 @@ test("plugin chat passthrough drops a chunk whose delta stripped to empty", asyn chatChunk({ content: "more" }), DONE, ]; - await pipePluginChatWithStrip(streamOf(events), res, session, "openai"); + await pipePluginChatWithStrip(streamOf(events), res, "openai", session); const text = out.join(""); assert.ok(!text.includes("m1") && !text.includes("m2"), "echo-only chunk stripped"); const dataLines = text.split("\n").filter((l) => l.startsWith("data:")).filter((l) => !l.includes("[DONE]")); @@ -130,6 +130,35 @@ test("plugin chat passthrough drops a chunk whose delta stripped to empty", asyn } }); +test("plugin chat passthrough keeps sibling text when one field is a pure tag echo (#463)", async () => { + const out: string[] = []; + const res = makeRes(out); + const session = makeSession(); + const events = [ + chatChunk({ content: "real answer", reasoning_content: `${TAG_OPEN}m00044${TAG_CLOSE}` }), + DONE, + ]; + await pipePluginChatWithStrip(streamOf(events), res, "openai", session); + const text = out.join(""); + assert.ok(!text.includes("m00044"), "echo-only sibling field stripped"); + assert.ok(text.includes("real answer"), "sibling field with real content must survive"); +}); + +test("plugin chat passthrough still drops a chunk where every managed field is a pure tag echo (#463)", async () => { + const out: string[] = []; + const res = makeRes(out); + const session = makeSession(); + const events = [ + chatChunk({ content: `${TAG_OPEN}m1${TAG_CLOSE}`, reasoning_content: `${TAG_OPEN}m2${TAG_CLOSE}` }), + DONE, + ]; + await pipePluginChatWithStrip(streamOf(events), res, "openai", session); + const text = out.join(""); + assert.ok(!text.includes("m1") && !text.includes("m2"), "echoes stripped"); + const dataLines = text.split("\n").filter((l) => l.startsWith("data:")).filter((l) => !l.includes("[DONE]")); + assert.equal(dataLines.length, 0, "all-echo chunk dropped"); +}); + test("plugin chat passthrough keeps tool_calls deltas byte-identical", async () => { const out: string[] = []; const res = makeRes(out); @@ -137,18 +166,67 @@ test("plugin chat passthrough keeps tool_calls deltas byte-identical", async () const toolChunk = chatChunk({ tool_calls: [{ index: 0, id: "call_1", type: "function", function: { name: "compress", arguments: "{\"content\":[]}" } }] }); const plain = chatChunk({ content: "plain text no tags" }); const events = [plain, toolChunk, chatChunk({}, { choices: [{ index: 0, delta: {}, finish_reason: "tool_calls" }] }), DONE]; - await pipePluginChatWithStrip(streamOf(events), res, session, "openai"); + await pipePluginChatWithStrip(streamOf(events), res, "openai", session); const text = out.join(""); assert.ok(text.includes(toolChunk.trim()), "tool_calls delta untouched"); assert.ok(text.includes(plain.trim()), "clean delta untouched"); }); +test("plugin chat passthrough strips a tag streamed in tokenizer-sized fragments (#468)", async () => { + const out: string[] = []; + const res = makeRes(out); + const session = makeSession(); + const whole = `ok ${TAG_OPEN}m00042${TAG_CLOSE} tail`; + const micro = whole.match(/.{1,4}/g) ?? []; + const events = [...micro.map((piece) => chatChunk({ content: piece })), DONE]; + await pipePluginChatWithStrip(streamOf(events), res, "openai", session); + const text = out.join(""); + assert.ok(!text.includes("m00042"), "micro-fragmented tag never reassembles downstream"); + assert.ok(!text.includes("\x3cacp") && !text.includes("\x3c/acp"), "no tag fragment survives"); + const joined = text + .split("\n") + .filter((l) => l.startsWith("data:")) + .filter((l) => !l.includes("[DONE]")) + .map((l) => JSON.parse(l.slice(5).trim()) as { choices?: Array<{ delta?: { content?: string } }> }) + .map((c) => c.choices?.[0]?.delta?.content ?? "") + .join(""); + assert.equal(joined, "ok tail", "surrounding prose reassembles to the tag-free text"); + const dataLines = text.split("\n").filter((l) => l.startsWith("data:")).filter((l) => !l.includes("[DONE]")); + for (const l of dataLines) { + assert.doesNotThrow(() => JSON.parse(l.slice(5).trim()), "every forwarded data line stays valid JSON"); + } +}); + +test("plugin anthropic passthrough strips a tag streamed in tokenizer-sized fragments (#468)", async () => { + const out: string[] = []; + const res = makeRes(out); + const session = makeSession(); + const whole = `ok ${TAG_OPEN}m00042${TAG_CLOSE} tail`; + const micro = whole.match(/.{1,4}/g) ?? []; + const events = [ + ...micro.map((piece) => `event: content_block_delta\ndata: ${JSON.stringify({ type: "content_block_delta", index: 0, delta: { type: "text_delta", text: piece } })}\n\n`), + `event: message_delta\ndata: ${JSON.stringify({ type: "message_delta", delta: { stop_reason: "end_turn" } })}\n\n`, + ]; + await pipePluginChatWithStrip(streamOf(events), res, "anthropic", session); + const text = out.join(""); + assert.ok(!text.includes("m00042"), "micro-fragmented tag never reassembles downstream"); + assert.ok(!text.includes("\x3cacp") && !text.includes("\x3c/acp"), "no tag fragment survives"); + const joined = text + .split("\n") + .filter((l) => l.startsWith("data:")) + .map((l) => JSON.parse(l.slice(5).trim()) as { delta?: { type?: string; text?: string } }) + .filter((ev) => ev.delta?.type === "text_delta") + .map((ev) => ev.delta?.text ?? "") + .join(""); + assert.equal(joined, "ok tail", "surrounding prose reassembles to the tag-free text"); +}); + test("plugin chat passthrough resolves held tail at stream end without [DONE]", async () => { const out: string[] = []; const res = makeRes(out); const session = makeSession(); const events = [chatChunk({ content: `prose ${TAG_OPEN}m0` })]; - await pipePluginChatWithStrip(streamOf(events), res, session, "openai"); + await pipePluginChatWithStrip(streamOf(events), res, "openai", session); const text = out.join(""); assert.ok(text.includes("prose "), "clean prefix passes"); assert.ok(!text.includes("m0"), "unclosed tag content dropped at stream end"); @@ -165,7 +243,7 @@ test("plugin anthropic passthrough strips render tags from text_delta and thinki `event: message_delta\ndata: ${JSON.stringify({ type: "message_delta", delta: { stop_reason: "end_turn" }, usage: { output_tokens: 3 } })}\n\n`, `event: message_stop\ndata: ${JSON.stringify({ type: "message_stop" })}\n\n`, ]; - await pipePluginChatWithStrip(streamOf(events), res, session, "anthropic"); + await pipePluginChatWithStrip(streamOf(events), res, "anthropic", session); const text = out.join(""); assert.ok(!text.includes("m00009") && !text.includes("m00010"), "render tags stripped from both delta types"); assert.ok(text.includes("hi bye"), "text prose survives"); @@ -182,7 +260,7 @@ test("plugin anthropic passthrough strips tags split across deltas and flushes b `event: content_block_delta\ndata: ${JSON.stringify({ type: "content_block_delta", index: 0, delta: { type: "text_delta", text: `0042${TAG_CLOSE} tail` } })}\n\n`, `event: message_delta\ndata: ${JSON.stringify({ type: "message_delta", delta: { stop_reason: "end_turn" } })}\n\n`, ]; - await pipePluginChatWithStrip(streamOf(events), res, session, "anthropic"); + await pipePluginChatWithStrip(streamOf(events), res, "anthropic", session); const text = out.join(""); assert.ok(!text.includes("m00042"), "split tag swallowed"); assert.ok(text.includes("tail"), "tail flushed before message_delta"); diff --git a/tests/plugin-passthrough-tag-strip.test.ts b/tests/plugin-passthrough-tag-strip.test.ts index ae559a25d1..08efc32a22 100644 --- a/tests/plugin-passthrough-tag-strip.test.ts +++ b/tests/plugin-passthrough-tag-strip.test.ts @@ -94,6 +94,30 @@ test("plugin passthrough strips tags split across deltas and flushes tail before assert.ok((blocks[flushIdx] ?? "").includes("msg_1"), "flushed tail delta carries item_id"); }); +test("plugin passthrough strips a tag streamed in tokenizer-sized fragments (#468)", async () => { + const out: string[] = []; + const res = makeRes(out); + const session = makeSession(); + const whole = `ok ${TAG_OPEN}m00042${TAG_CLOSE} tail`; + const micro = whole.match(/.{1,4}/g) ?? []; + const events = [ + ...micro.map((piece) => sse({ type: "response.output_text.delta", item_id: "msg_1", output_index: 0, delta: piece })), + sse({ type: "response.completed", response: { usage: { input_tokens: 7, output_tokens: 3 } } }), + ]; + await pipePluginResponsesWithStrip(streamOf(events), res, session); + const text = out.join(""); + assert.ok(!text.includes("m00042"), "micro-fragmented tag never reassembles downstream"); + assert.ok(!text.includes("\x3cacp") && !text.includes("\x3c/acp"), "no tag fragment survives"); + const joined = text + .split("\n") + .filter((l) => l.startsWith("data:")) + .map((l) => JSON.parse(l.slice(5).trim()) as { type?: string; delta?: string }) + .filter((ev) => ev.type === "response.output_text.delta" && typeof ev.delta === "string") + .map((ev) => ev.delta as string) + .join(""); + assert.equal(joined, "ok tail", "surrounding prose reassembles to the tag-free text"); +}); + test("plugin passthrough keeps function_call and non-tag events byte-identical", async () => { const out: string[] = []; const res = makeRes(out); @@ -167,7 +191,8 @@ test("plugin passthrough resolves held tail at stream end without a done-family ]; await pipePluginResponsesWithStrip(streamOf(events2), res2, session); const text2 = out2.join(""); - assert.ok(text2.includes(`"delta":"prose \x3cac"`), "ambiguous prefix passes through untouched at stream end"); + assert.ok(text2.includes(`"delta":"prose "`), "clean prefix passes once the ambiguous head is held"); + assert.ok(text2.includes(`"delta":"\x3cac"`), "ambiguous prefix flushed as its own delta at stream end, never lost"); }); test("plugin passthrough rebuildEvent collapses multi-line data payloads into one line", async () => { diff --git a/tests/proxy-anthropic-stream.test.ts b/tests/proxy-anthropic-stream.test.ts deleted file mode 100644 index 4317816f83..0000000000 --- a/tests/proxy-anthropic-stream.test.ts +++ /dev/null @@ -1,89 +0,0 @@ -import { test } from "node:test"; -import assert from "node:assert/strict"; -import { rewriteSseStream } from "../src/stream.ts"; -import { COMPRESS_TOOL_NAME } from "../src/compress-tool.ts"; -import { createCore, createInitialState, defaultConfig } from "acp-kernel"; - -function makeCtx() { - const core = createCore(); - const config = defaultConfig(200000); - const state = createInitialState(); - return { ctx: { core, config, messages: [], session: { id: "t", meta: {}, stats: { requests: 0, tokensSaved: 0, inputTokens: 0, cachedTokens: 0, outputTokens: 0, cacheSamples: 0, lastInputTokens: 0, contextTokens: 0 }, metadata: {}, state }, log: () => {} } as never }; -} - -function evt(type: string, data: object): string { - return `event: ${type}\ndata: ${JSON.stringify(data)}\n\n`; -} - -async function rewrite(sse: string, ctx: never): Promise { - const upstream = new ReadableStream({ start(c) { c.enqueue(new TextEncoder().encode(sse)); c.close(); } }); - const out: string[] = []; - for await (const buf of rewriteSseStream(upstream, ctx)) out.push(buf.toString("utf8")); - return out.join(""); -} - -// C1 regression: a plain text response (no compress tool_use) must preserve -// message_delta + message_stop. Before the fix these were unconditionally -// dropped, so every non-compress Anthropic stream hung the client. -test("C1: plain text response preserves message_delta and message_stop", async () => { - const { ctx } = makeCtx(); - const sse = [ - evt("message_start", { type: "message_start", message: { id: "msg_1", type: "message", role: "assistant" as const, content: [], model: "claude", stop_reason: null, usage: { input_tokens: 10, output_tokens: 0 } } }), - evt("content_block_start", { type: "content_block_start", index: 0, content_block: { type: "text", text: "" } }), - evt("content_block_delta", { type: "content_block_delta", index: 0, delta: { type: "text_delta", text: "Hello" } }), - evt("content_block_stop", { type: "content_block_stop", index: 0 }), - evt("message_delta", { type: "message_delta", delta: { stop_reason: "end_turn" }, usage: { output_tokens: 1 } }), - evt("message_stop", { type: "message_stop" }), - ].join(""); - const out = await rewrite(sse, ctx); - assert.ok(out.includes('"Hello"'), "text content must pass through"); - assert.ok(out.includes('"message_delta"'), "message_delta must be preserved (C1)"); - assert.ok(out.includes('"message_stop"'), "message_stop must be preserved (C1)"); - assert.ok(out.includes('"end_turn"'), "original stop_reason must survive"); -}); - -// When a compress tool_use IS present, the original message_delta is suppressed -// and a rewritten one (end_turn, no real tool_use) is emitted at the end. -test("compress tool_use suppresses original message_delta and emits rewritten stop", async () => { - const { ctx } = makeCtx(); - const sse = [ - evt("message_start", { type: "message_start", message: { id: "msg_2", type: "message", role: "assistant" as const, content: [], model: "claude", stop_reason: null, usage: { input_tokens: 10, output_tokens: 0 } } }), - evt("content_block_start", { type: "content_block_start", index: 0, content_block: { type: "tool_use", id: "tu_1", name: COMPRESS_TOOL_NAME, input: {} } }), - evt("content_block_delta", { type: "content_block_delta", index: 0, delta: { type: "input_json_delta", partial_json: '{"content":[{"startId":"m00001","endId":"m00001","summary":"x"}]}' } }), - evt("content_block_stop", { type: "content_block_stop", index: 0 }), - evt("message_delta", { type: "message_delta", delta: { stop_reason: "tool_use" }, usage: { output_tokens: 1 } }), - evt("message_stop", { type: "message_stop" }), - ].join(""); - const out = await rewrite(sse, ctx); - assert.ok(out.includes("Compressed") || out.includes("Compression FAILED"), "compress note must be emitted"); - assert.ok(out.includes('"end_turn"'), "rewritten stop_reason must be end_turn"); -}); - -// M2 regression: the replacement text block must reuse the suppressed tool_use's -// index, not hardcode 0. Here compress is at index 2 (after two text blocks). -test("M2: replacement block uses the suppressed tool_use index, not hardcoded 0", async () => { - const { ctx } = makeCtx(); - const sse = [ - evt("message_start", { type: "message_start", message: { id: "msg_3", type: "message", role: "assistant" as const, content: [], model: "claude", stop_reason: null, usage: { input_tokens: 10, output_tokens: 0 } } }), - evt("content_block_start", { type: "content_block_start", index: 0, content_block: { type: "text", text: "" } }), - evt("content_block_delta", { type: "content_block_delta", index: 0, delta: { type: "text_delta", text: "thinking" } }), - evt("content_block_stop", { type: "content_block_stop", index: 0 }), - evt("content_block_start", { type: "content_block_start", index: 1, content_block: { type: "text", text: "" } }), - evt("content_block_delta", { type: "content_block_delta", index: 1, delta: { type: "text_delta", text: "more" } }), - evt("content_block_stop", { type: "content_block_stop", index: 1 }), - evt("content_block_start", { type: "content_block_start", index: 2, content_block: { type: "tool_use", id: "tu_2", name: COMPRESS_TOOL_NAME, input: {} } }), - evt("content_block_delta", { type: "content_block_delta", index: 2, delta: { type: "input_json_delta", partial_json: '{"content":[{"startId":"m00001","endId":"m00001","summary":"x"}]}' } }), - evt("content_block_stop", { type: "content_block_stop", index: 2 }), - evt("message_delta", { type: "message_delta", delta: { stop_reason: "tool_use" }, usage: { output_tokens: 1 } }), - evt("message_stop", { type: "message_stop" }), - ].join(""); - const out = await rewrite(sse, ctx); - // The replacement content_block must reference index 2 (the tool_use - // position), proving it reused the suppressed block's index, not 0. - // (The original index:2 tool_use was suppressed, so any index:2 in the - // output comes only from the replacement block.) - assert.ok(out.includes('"index":2'), "replacement content_block must use index 2 (M2)"); - // Replacement emitted some compress note (success or failed — messages is - // empty here so applyRanges will report failure, which is fine). - assert.ok(/ompress/i.test(out), "a compress note must be emitted for the tool_use"); -}); diff --git a/tests/proxy-basic.test.ts b/tests/proxy-basic.test.ts index c13a8cc9eb..a0b7c251cf 100644 --- a/tests/proxy-basic.test.ts +++ b/tests/proxy-basic.test.ts @@ -198,72 +198,6 @@ test("deriveSessionIdOpenai is stable for same first-user content", () => { assert.ok(a.length > 0); }); -test("OpenAI SSE rewriter passes through real tool calls when no compress detected", async () => { - const { rewriteOpenaiSseStream } = await import("../src/stream-openai.js"); - const { createCore, createInitialState, defaultConfig } = await import("acp-kernel"); - const core = createCore(); - const state = createInitialState(); - const config = defaultConfig(200000); - const ctx = { core, config, messages: [], session: { id: "s1", meta: {}, stats: { requests: 0, tokensSaved: 0, inputTokens: 0, cachedTokens: 0, outputTokens: 0, cacheSamples: 0, lastInputTokens: 0, contextTokens: 0 }, metadata: {}, state }, log: () => {} }; - - // Simulate a provider that sends the LAST tool_call arguments fragment - // in the SAME chunk as finish_reason (common in GLM/OpenAI-compatible APIs). - // Before the fix, the rewriter suppressed this entire chunk, dropping the - // last arguments fragment → opencode got SchemaError(Missing key at ["command"]). - const sse = [ - `data: ${JSON.stringify({ object: "chat.completion.chunk", choices: [{ index: 0, delta: { role: "assistant" as const, tool_calls: [{ index: 0, id: "call_1", type: "function", function: { name: "bash", arguments: "{\"command\":\"" } }] }, finish_reason: null }] })}\n\n`, - `data: ${JSON.stringify({ object: "chat.completion.chunk", choices: [{ index: 0, delta: { tool_calls: [{ index: 0, function: { arguments: "echo test\"}" } }] }, finish_reason: null }] })}\n\n`, - `data: ${JSON.stringify({ object: "chat.completion.chunk", choices: [{ index: 0, delta: {}, finish_reason: "tool_calls" }] })}\n\n`, - `data: [DONE]\n\n`, - ].join(""); - const upstream = new ReadableStream({ - start(c) { - c.enqueue(new TextEncoder().encode(sse)); - c.close(); - }, - }); - const chunks: string[] = []; - for await (const buf of rewriteOpenaiSseStream(upstream, ctx as never)) { - chunks.push(buf.toString("utf8")); - } - const combined = chunks.join(""); - assert.ok(combined.includes('"bash"'), "tool name must pass through"); - assert.ok(combined.includes("echo test"), "tool arguments must pass through"); - assert.ok(combined.includes("tool_calls"), "finish_reason tool_calls must be present"); - assert.ok(combined.includes("[DONE]"), "[DONE] marker must be present"); - assert.ok(!combined.includes("acp-proxy:"), "no compress note expected"); -}); - -test("OpenAI SSE rewriter suppresses compress tool call and injects note", async () => { - const { rewriteOpenaiSseStream } = await import("../src/stream-openai.js"); - const { createCore, createInitialState, defaultConfig } = await import("acp-kernel"); - const core = createCore(); - const config = defaultConfig(200000); - const state = createInitialState(); - const ctx = { core, config, messages: [], session: { id: "s1", meta: {}, stats: { requests: 0, tokensSaved: 0, inputTokens: 0, cachedTokens: 0, outputTokens: 0, cacheSamples: 0, lastInputTokens: 0, contextTokens: 0 }, metadata: {}, state }, log: () => {} }; - - const sse = [ - `data: ${JSON.stringify({ object: "chat.completion.chunk", choices: [{ index: 0, delta: { tool_calls: [{ index: 0, id: "call_1", type: "function", function: { name: "compress", arguments: '{"content":[{"startId":"m00001","endId":"m00001","summary":"test"}]}' } }] }, finish_reason: null }] })}\n\n`, - `data: ${JSON.stringify({ object: "chat.completion.chunk", choices: [{ index: 0, delta: {}, finish_reason: "tool_calls" }] })}\n\n`, - `data: [DONE]\n\n`, - ].join(""); - const upstream = new ReadableStream({ - start(c) { - c.enqueue(new TextEncoder().encode(sse)); - c.close(); - }, - }); - const chunks: string[] = []; - for await (const buf of rewriteOpenaiSseStream(upstream, ctx as never)) { - chunks.push(buf.toString("utf8")); - } - const combined = chunks.join(""); - assert.ok(!combined.includes('"compress"'), "compress tool name must be suppressed"); - assert.ok(combined.includes('"stop"'), "finish_reason should be stop"); - assert.ok(combined.includes("[DONE]"), "[DONE] must be present"); -}); - - test("ACP tag regex strips tag prefix from tool-call arguments", () => { const ACP_TAG_RE = /^\x3cacp [^>]*\x3e[^\x3c]*\x3c\/acp\x3e\n?/; const tagged = '\x3cacp tokens="50" type="bash"\x3em00005\x3c/acp\x3e\n{"command":"echo hello"}'; diff --git a/tests/proxy-noinject-tag-strip.test.ts b/tests/proxy-noinject-tag-strip.test.ts new file mode 100644 index 0000000000..31022cb42e --- /dev/null +++ b/tests/proxy-noinject-tag-strip.test.ts @@ -0,0 +1,414 @@ +import assert from "node:assert"; +import http from "node:http"; +import { once } from "node:events"; +import test from "node:test"; + +process.env.NODE_ENV = "test"; + +import { defaultConfig } from "acp-kernel"; +import { startServer, type ProxyOptions } from "../src/server.ts"; +import { SessionStore, _setStoreForTest } from "../src/persist.ts"; +import { _setForTest as setRegistryForTest } from "../src/registry.ts"; + +// Render-tag pieces are assembled from hex escapes so no literal tag sequence +// appears in this file's source. +const LT = "\x3c"; +const GT = "\x3e"; +const TAG = (id: string, tokens = 177) => `${LT}acp tokens="${tokens}" type="text"${GT}${id}${LT}/acp${GT}`; +const OPEN_MARK = `${LT}acp `; +const CLOSE_MARK = `${LT}/acp${GT}`; + +interface Harness { + proxyPort: number; + upstreamPort: number; + close(): Promise; +} + +function anthropicSse(event: string, data: unknown): string { + return `event: ${event}\ndata: ${JSON.stringify(data)}\n\n`; +} + +function openaiChunk(data: unknown): string { + return `data: ${JSON.stringify(data)}\n\n`; +} + +async function startHarness(compress: { injectTool: boolean; injectNudge: boolean }, scripts: string[][], json: boolean): Promise { + let call = 0; + const upstream = http.createServer((req, res) => { + req.on("data", () => {}); + req.on("end", () => { + res.writeHead(200, { "content-type": json ? "application/json" : "text/event-stream" }); + const script = scripts[Math.min(call, scripts.length - 1)]!; + call += 1; + for (const line of script) res.write(line); + res.end(); + }); + }); + upstream.listen(0, "127.0.0.1"); + return (async () => { + await once(upstream, "listening"); + const upstreamPort = upstream.address().port; + _setStoreForTest(new SessionStore({ enabled: false })); + setRegistryForTest({}); + const proxy = await startServer({ + port: 0, + host: "127.0.0.1", + upstream: "http://127.0.0.1", + routes: { [`http://127.0.0.1:${upstreamPort}`]: { models: { "claude-test": { context: 400_000 }, "gpt-test": { context: 400_000 } } } }, + modelContextLimit: 400_000, + kernelConfig: defaultConfig(400_000), + promptCache: { routing: "auto" }, + compress, + sessionHeader: "x-acp-session", + log: false, + debug: false, + passthrough: false, + autoUpdate: false, + mitm: { enabled: false, domains: [] }, + } as ProxyOptions); + await once(proxy, "listening"); + const proxyPort = proxy.address().port; + return { + proxyPort, + upstreamPort, + close: async () => { + proxy.close(); + await once(proxy, "close"); + upstream.close(); + await once(upstream, "close"); + }, + }; + })(); +} + +function tagEchoAnthropicScript(): string[] { + const full = `好的,总结如下 ${TAG("m00155")}${TAG("m00155", 44)}${TAG("m00156", 33)}另外 5 « 6 成立${TAG("m00157")}完毕`; + const splitAt = [7, 20, 26, 41, 48, 60]; + const parts: string[] = []; + let prev = 0; + for (const s of splitAt) { + parts.push(full.slice(prev, s)); + prev = s; + } + parts.push(full.slice(prev)); + return [ + anthropicSse("message_start", { type: "message_start", message: { id: "msg_ninj_1", role: "assistant", usage: { input_tokens: 20 } } }), + anthropicSse("content_block_start", { type: "content_block_start", index: 0, content_block: { type: "text", text: "" } }), + ...parts.map((p) => anthropicSse("content_block_delta", { type: "content_block_delta", index: 0, delta: { type: "text_delta", text: p } })), + anthropicSse("content_block_stop", { type: "content_block_stop", index: 0 }), + anthropicSse("message_delta", { type: "message_delta", delta: { stop_reason: "end_turn", stop_sequence: null }, usage: { output_tokens: 9 } }), + anthropicSse("message_stop", { type: "message_stop" }), + ]; +} + +function tagEchoOpenAiScript(): string[] { + const full = `title: ${TAG("m00009", 12)}summary ok`; + const parts = [full.slice(0, 6), full.slice(6, 22), full.slice(22)]; + const base = { id: "chatcmpl_ninj", object: "chat.completion.chunk", created: 1, model: "gpt-test" }; + return [ + openaiChunk({ ...base, choices: [{ index: 0, delta: { role: "assistant", content: "" } }] }), + ...parts.map((p) => openaiChunk({ ...base, choices: [{ index: 0, delta: { content: p } }] })), + openaiChunk({ ...base, choices: [{ index: 0, delta: {}, finish_reason: "stop" }] }), + "data: [DONE]\n\n", + ]; +} + +function cleanAnthropicText(raw: string): string { + return [...raw.matchAll(/"type":"text_delta","text":"((?:[^"\\]|\\.)*)"/g)] + .map((m) => JSON.parse(`"${m[1]}"`) as string) + .join(""); +} + +function cleanOpenAiContent(raw: string): string { + let out = ""; + for (const line of raw.split("\n")) { + if (!line.startsWith("data:") || line.slice(5).trim() === "[DONE]") continue; + try { + const obj = JSON.parse(line.slice(5).trim()) as { choices?: Array<{ delta?: { content?: string } }> }; + out += obj.choices?.[0]?.delta?.content ?? ""; + } catch {} + } + return out; +} + +test("#460: non-injected anthropic SSE — echoed render tags stripped, prose intact", async () => { + const h = await startHarness({ injectTool: false, injectNudge: false }, [tagEchoAnthropicScript()]); + try { + const resp = await fetch(`http://127.0.0.1:${h.proxyPort}/bili/http://127.0.0.1:${h.upstreamPort}/v1/messages`, { + method: "POST", + headers: { "content-type": "application/json", "x-acp-session": "noinject-anth" }, + body: JSON.stringify({ model: "claude-test", max_tokens: 1024, stream: true, system: "You are a test assistant.", messages: [{ role: "user", content: "hello" }] }), + }); + assert.equal(resp.status, 200); + let raw = ""; + for await (const chunk of resp.body) raw += Buffer.from(chunk).toString("utf8"); + assert.equal(raw.includes(OPEN_MARK), false, "client stream leaked a render open tag"); + assert.equal(raw.includes(CLOSE_MARK), false, "client stream leaked a render close tag"); + assert.ok(cleanAnthropicText(raw).endsWith("好的,总结如下 另外 5 « 6 成立完毕"), `unexpected client text: ${JSON.stringify(cleanAnthropicText(raw))}`); + assert.ok(raw.includes("message_stop"), "terminal event missing"); + } finally { + await h.close(); + } +}); + +test("#460: non-injected anthropic SSE without tags — byte-identical passthrough", async () => { + const script = [ + anthropicSse("message_start", { type: "message_start", message: { id: "msg_ninj_2", role: "assistant", usage: { input_tokens: 20 } } }), + anthropicSse("content_block_start", { type: "content_block_start", index: 0, content_block: { type: "text", text: "" } }), + anthropicSse("content_block_delta", { type: "content_block_delta", index: 0, delta: { type: "text_delta", text: "plain " } }), + anthropicSse("content_block_delta", { type: "content_block_delta", index: 0, delta: { type: "text_delta", text: "prose" } }), + anthropicSse("content_block_stop", { type: "content_block_stop", index: 0 }), + anthropicSse("message_delta", { type: "message_delta", delta: { stop_reason: "end_turn", stop_sequence: null }, usage: { output_tokens: 2 } }), + anthropicSse("message_stop", { type: "message_stop" }), + ]; + const h = await startHarness({ injectTool: false, injectNudge: false }, [script]); + try { + const resp = await fetch(`http://127.0.0.1:${h.proxyPort}/bili/http://127.0.0.1:${h.upstreamPort}/v1/messages`, { + method: "POST", + headers: { "content-type": "application/json", "x-acp-session": "noinject-anth-clean" }, + body: JSON.stringify({ model: "claude-test", max_tokens: 1024, stream: true, system: "s", messages: [{ role: "user", content: "hello" }] }), + }); + assert.equal(resp.status, 200); + let raw = ""; + for await (const chunk of resp.body) raw += Buffer.from(chunk).toString("utf8"); + assert.equal(raw, script.join(""), "tag-free stream must pass through byte-identical"); + } finally { + await h.close(); + } +}); + +test("#460: openai title-gen (max_tokens<=200) SSE — echoed render tags stripped, [DONE] intact", async () => { + const h = await startHarness({ injectTool: true, injectNudge: true }, [tagEchoOpenAiScript()]); + try { + const resp = await fetch(`http://127.0.0.1:${h.proxyPort}/bili/http://127.0.0.1:${h.upstreamPort}/v1/chat/completions`, { + method: "POST", + headers: { "content-type": "application/json", "x-acp-session": "noinject-openai-title" }, + body: JSON.stringify({ model: "gpt-test", max_tokens: 100, stream: true, messages: [{ role: "user", content: "hello" }] }), + }); + assert.equal(resp.status, 200); + let raw = ""; + for await (const chunk of resp.body) raw += Buffer.from(chunk).toString("utf8"); + assert.equal(raw.includes(OPEN_MARK), false, "client stream leaked a render open tag"); + assert.equal(raw.includes(CLOSE_MARK), false, "client stream leaked a render close tag"); + assert.equal(cleanOpenAiContent(raw), "title: summary ok", `unexpected content: ${JSON.stringify(cleanOpenAiContent(raw))}`); + assert.ok(raw.includes("[DONE]"), "[DONE] missing"); + assert.ok(raw.includes('"finish_reason":"stop"'), "finish_reason chunk missing"); + } finally { + await h.close(); + } +}); + +function responsesSse(event: string, data: unknown): string { + return `event: ${event}\ndata: ${JSON.stringify(data)}\n\n`; +} + +function chunkText(text: string, step: number): string[] { + const out: string[] = []; + for (let i = 0; i < text.length; i += step) out.push(text.slice(i, i + step)); + return out; +} + +function cleanResponsesDeltaText(raw: string): string { + let out = ""; + for (const line of raw.split("\n")) { + if (!line.startsWith("data:")) continue; + try { + const obj = JSON.parse(line.slice(5).trim()) as { type?: string; delta?: string }; + out += obj?.type === "response.output_text.delta" ? obj.delta ?? "" : ""; + } catch {} + } + return out; +} + +function tagEchoResponsesScript(): string[] { + const full = `压缩前 ${TAG("m00155")}${TAG("m00156", 33)}后 5 « 6 成立`; + const chunk = chunkText(full, 7); + let item = "item_ninj_r1"; + return [ + responsesSse("response.created", { type: "response.created", response: { id: "resp_ninj_r1", status: "in_progress", output: [] } }), + responsesSse("response.output_item.added", { type: "response.output_item.added", output_index: 0, item: { type: "message", role: "assistant", content: [] } }), + responsesSse("response.content_part.added", { type: "response.content_part.added", item_id: item, output_index: 0, part: { type: "output_text", text: "" } }), + ...chunk.map((p) => responsesSse("response.output_text.delta", { type: "response.output_text.delta", item_id: item, output_index: 0, delta: p })), + responsesSse("response.output_text.done", { type: "response.output_text.done", item_id: item, output_index: 0, text: full }), + responsesSse("response.content_part.done", { type: "response.content_part.done", item_id: item, output_index: 0, part: { type: "output_text", text: full } }), + responsesSse("response.output_item.done", { type: "response.output_item.done", output_index: 0, item: { type: "message", role: "assistant", content: [{ type: "output_text", text: full }] } }), + responsesSse("response.completed", { type: "response.completed", response: { id: "resp_ninj_r1", status: "completed", output: [{ type: "message", role: "assistant", content: [{ type: "output_text", text: full }] }], usage: { input_tokens: 30, output_tokens: 9, total_tokens: 39 } } }), + ]; +} + +function cleanResponsesScript(): string[] { + const text = "plain prose 5 < 6 holds"; + return [ + responsesSse("response.created", { type: "response.created", response: { id: "resp_ninj_r2", status: "in_progress", output: [] } }), + responsesSse("response.output_item.added", { type: "response.output_item.added", output_index: 0, item: { type: "message", role: "assistant", content: [] } }), + responsesSse("response.output_text.delta", { type: "response.output_text.delta", item_id: "item_ninj_r2", output_index: 0, delta: "plain " }), + responsesSse("response.output_text.delta", { type: "response.output_text.delta", item_id: "item_ninj_r2", output_index: 0, delta: "prose 5 < 6 holds" }), + responsesSse("response.output_text.done", { type: "response.output_text.done", item_id: "item_ninj_r2", output_index: 0, text }), + responsesSse("response.output_item.done", { type: "response.output_item.done", output_index: 0, item: { type: "message", role: "assistant", content: [{ type: "output_text", text }] } }), + responsesSse("response.completed", { type: "response.completed", response: { id: "resp_ninj_r2", status: "completed", output: [{ type: "message", role: "assistant", content: [{ type: "output_text", text }] }], usage: { input_tokens: 28, output_tokens: 5, total_tokens: 33 } } }), + ]; +} + +test("#460 residual: native Responses compaction (resetAfterSuccess) SSE — echoed render tags stripped, response.completed intact", async () => { + const h = await startHarness({ injectTool: true, injectNudge: true }, [tagEchoResponsesScript()]); + try { + const resp = await fetch(`http://127.0.0.1:${h.proxyPort}/bili/http://127.0.0.1:${h.upstreamPort}/v1/responses`, { + method: "POST", + headers: { "content-type": "application/json", "x-acp-session": "noinject-resp-compact" }, + body: JSON.stringify({ model: "gpt-test", stream: true, session_id: "noinject-resp-compact", instructions: "You are a test agent.", input: [{ type: "message", role: "user", content: "hello" }, { type: "compaction_trigger" }] }), + }); + assert.equal(resp.status, 200, "native compact request must still forward to upstream"); + let raw = ""; + for await (const chunk of resp.body) raw += Buffer.from(chunk).toString("utf8"); + assert.equal(raw.includes(OPEN_MARK), false, "client stream leaked a render open tag"); + assert.equal(raw.includes(CLOSE_MARK), false, "client stream leaked a render close tag"); + assert.equal(cleanResponsesDeltaText(raw), "压缩前 后 5 « 6 成立", `unexpected client text: ${JSON.stringify(cleanResponsesDeltaText(raw))}`); + assert.ok(raw.includes("response.completed"), "terminal event missing"); + } finally { + await h.close(); + } +}); + +test("#460 residual: tag-free Responses compaction SSE — byte-identical passthrough", async () => { + const h = await startHarness({ injectTool: true, injectNudge: true }, [cleanResponsesScript()]); + try { + const resp = await fetch(`http://127.0.0.1:${h.proxyPort}/bili/http://127.0.0.1:${h.upstreamPort}/v1/responses`, { + method: "POST", + headers: { "content-type": "application/json", "x-acp-session": "noinject-resp-clean" }, + body: JSON.stringify({ model: "gpt-test", stream: true, session_id: "noinject-resp-clean", instructions: "s", input: [{ type: "message", role: "user", content: "hello" }, { type: "compaction_trigger" }] }), + }); + assert.equal(resp.status, 200); + let raw = ""; + for await (const chunk of resp.body) raw += Buffer.from(chunk).toString("utf8"); + assert.equal(raw, cleanResponsesScript().join(""), "tag-free compact stream must pass through byte-identical"); + } finally { + await h.close(); + } +}); + +test("#460 residual: non-injected NON-STREAMING openai JSON — echoed render tags stripped, body stays valid JSON", async () => { + const full = `title: ${TAG("m00009", 12)}summary ok`; + const completion = { id: "chatcmpl_ninj_json", object: "chat.completion", created: 1, model: "gpt-test", choices: [{ index: 0, message: { role: "assistant", content: full }, finish_reason: "stop" }] }; + const h = await startHarness({ injectTool: true, injectNudge: true }, [[JSON.stringify(completion)]], true); + try { + const resp = await fetch(`http://127.0.0.1:${h.proxyPort}/bili/http://127.0.0.1:${h.upstreamPort}/v1/chat/completions`, { + method: "POST", + headers: { "content-type": "application/json", "x-acp-session": "noinject-openai-json" }, + body: JSON.stringify({ model: "gpt-test", max_tokens: 100, stream: false, messages: [{ role: "user", content: "hello" }] }), + }); + assert.equal(resp.status, 200); + let bodyText = ""; + for await (const chunk of resp.body) bodyText += Buffer.from(chunk).toString("utf8"); + try { + const parsed = JSON.parse(bodyText) as { choices?: Array<{ message?: { content?: string } }> }; + assert.equal(parsed.choices?.[0]?.message?.content, "title: summary ok", `unexpected content: ${JSON.stringify(bodyText)}`); + } catch { + assert.ok(false, "client body is not valid JSON"); + } + assert.equal(bodyText.includes(OPEN_MARK), false, "client body leaked a render open tag"); + assert.equal(bodyText.includes(CLOSE_MARK), false, "client body leaked a render close tag"); + } finally { + await h.close(); + } +}); + +test("#460 residual: non-injected NON-STREAMING responses JSON — echoed render tags stripped from output items", async () => { + const full = `title: ${TAG("m00009", 12)}summary ok`; + const response = { id: "resp_ninj_json", status: "completed", output: [{ type: "message", role: "assistant", content: [{ type: "output_text", text: full }] }] }; + const h = await startHarness({ injectTool: true, injectNudge: true }, [[JSON.stringify(response)]], true); + try { + const resp = await fetch(`http://127.0.0.1:${h.proxyPort}/bili/http://127.0.0.1:${h.upstreamPort}/v1/responses`, { + method: "POST", + headers: { "content-type": "application/json", "x-acp-session": "noinject-resp-json" }, + body: JSON.stringify({ model: "gpt-test", stream: false, session_id: "noinject-resp-json", input: [{ type: "message", role: "user", content: "hello" }] }), + }); + assert.equal(resp.status, 200); + let bodyText = ""; + for await (const chunk of resp.body) bodyText += Buffer.from(chunk).toString("utf8"); + assert.equal(bodyText.includes(OPEN_MARK), false, "client body leaked a render open tag"); + let text: string | undefined; + try { + const parsed = JSON.parse(bodyText) as { output?: Array<{ content?: Array<{ text: string }> }> }; + text = parsed.output?.[0]?.content?.[0]?.text; + } catch {} + assert.equal(text, "title: summary ok", `unexpected body: ${JSON.stringify(bodyText)}`); + } finally { + await h.close(); + } +}); + +test("#460 residual: tag-free NON-STREAMING JSON — byte-identical passthrough", async () => { + const completion = { id: "chatcmpl_ninj_clean", object: "chat.completion", created: 1, model: "gpt-test", choices: [{ index: 0, message: { role: "assistant", content: "plain 5 < 6 summary" }, finish_reason: "stop" }] }; + const h = await startHarness({ injectTool: true, injectNudge: true }, [[JSON.stringify(completion)]], true); + try { + const resp = await fetch(`http://127.0.0.1:${h.proxyPort}/bili/http://127.0.0.1:${h.upstreamPort}/v1/chat/completions`, { + method: "POST", + headers: { "content-type": "application/json", "x-acp-session": "noinject-openai-clean" }, + body: JSON.stringify({ model: "gpt-test", max_tokens: 100, stream: false, messages: [{ role: "user", content: "hello" }] }), + }); + assert.equal(resp.status, 200); + let bodyText = ""; + for await (const chunk of resp.body) bodyText += Buffer.from(chunk).toString("utf8"); + assert.equal(bodyText, JSON.stringify(completion), "tag-free JSON body must pass through byte-identical"); + } finally { + await h.close(); + } +}); + +function tagEchoOpenAiSplitScript(): string[] { + const full = `title: ${TAG("m00009", 12)}summary ok`; + const chunk = chunkText(full, 5); + const base = { id: "chatcmpl_split", object: "chat.completion.chunk", created: 1, model: "gpt-test" }; + return [ + openaiChunk({ ...base, choices: [{ index: 0, delta: { role: "assistant", content: "" } }] }), + ...chunk.map((p) => openaiChunk({ ...base, choices: [{ index: 0, delta: { content: p } }] })), + openaiChunk({ ...base, choices: [{ index: 0, delta: {}, finish_reason: "stop" }] }), + "data: [DONE]\n\n", + ]; +} + +test("#468: openai SSE with the render OPEN tag split across chunks — stripped, not echoed", async () => { + const h = await startHarness({ injectTool: false, injectNudge: false }, [tagEchoOpenAiSplitScript()]); + try { + const resp = await fetch(`http://127.0.0.1:${h.proxyPort}/bili/http://127.0.0.1:${h.upstreamPort}/v1/chat/completions`, { + method: "POST", + headers: { "content-type": "application/json", "x-acp-session": "noinject-openai-split" }, + body: JSON.stringify({ model: "gpt-test", max_tokens: 1024, stream: true, messages: [{ role: "user", content: "hello" }] }), + }); + assert.equal(resp.status, 200); + let raw = ""; + for await (const chunk of resp.body) raw += Buffer.from(chunk).toString("utf8"); + assert.equal(raw.includes(OPEN_MARK), false, "client stream leaked a render open tag"); + assert.equal(raw.includes(CLOSE_MARK), false, "client stream leaked a render close tag"); + assert.equal(cleanOpenAiContent(raw), "title: summary ok", `unexpected content: ${JSON.stringify(cleanOpenAiContent(raw))}`); + assert.ok(raw.includes("[DONE]"), "[DONE] missing"); + } finally { + await h.close(); + } +}); + +test("#475 G3: fake-completion buffering + streaming request answered with JSON — stripped body still delivered", async () => { + // With BILI_FAKE_COMPLETION_RETRIES>0 and a streaming request, the #378 + // buffering block consumes upstream.body before the ladder; the G3 JSON + // strip branch must read responseBody, not the drained upstream.body. + process.env.BILI_FAKE_COMPLETION_RETRIES = "1"; + const full = `title: ${TAG("m00009", 12)}summary ok`; + const completion = { id: "chatcmpl_fc_json", object: "chat.completion", created: 1, model: "gpt-test", choices: [{ index: 0, message: { role: "assistant", content: full }, finish_reason: "stop" }] }; + const h = await startHarness({ injectTool: false, injectNudge: false }, [[JSON.stringify(completion)]], true); + try { + const resp = await fetch(`http://127.0.0.1:${h.proxyPort}/bili/http://127.0.0.1:${h.upstreamPort}/v1/chat/completions`, { + method: "POST", + headers: { "content-type": "application/json", "x-acp-session": "noinject-fc-json" }, + body: JSON.stringify({ model: "gpt-test", max_tokens: 100, stream: true, messages: [{ role: "user", content: "hello" }] }), + }); + assert.equal(resp.status, 200); + let bodyText = ""; + for await (const chunk of resp.body) bodyText += Buffer.from(chunk).toString("utf8"); + const parsed = JSON.parse(bodyText) as { choices?: Array<{ message?: { content?: string } }> }; + assert.equal(parsed.choices?.[0]?.message?.content, "title: summary ok", `unexpected body: ${JSON.stringify(bodyText)}`); + assert.equal(bodyText.includes(OPEN_MARK), false, "client body leaked a render open tag"); + } finally { + delete process.env.BILI_FAKE_COMPLETION_RETRIES; + await h.close(); + } +}); diff --git a/tests/proxy-sse.test.ts b/tests/proxy-sse.test.ts deleted file mode 100644 index a3e4f557a1..0000000000 --- a/tests/proxy-sse.test.ts +++ /dev/null @@ -1,152 +0,0 @@ -import { test } from "node:test"; -import assert from "node:assert/strict"; -import { readFileSync } from "node:fs"; -import { rewriteOpenaiSseStream } from "../src/stream-openai.ts"; -import { createCore, createInitialState, defaultConfig } from "acp-kernel"; - -function makeCtx() { - const core = createCore(); - const config = defaultConfig(200000); - const state = createInitialState(); - return { core, config, state, ctx: { core, config, messages: [], session: { id: "t", meta: {}, stats: { requests: 0, tokensSaved: 0, inputTokens: 0, cachedTokens: 0, outputTokens: 0, cacheSamples: 0, lastInputTokens: 0, contextTokens: 0 }, metadata: {}, state }, log: () => {} } as never }; -} - -function parseToolCalls(sse: string) { - const frags: Record = {}; - const names: Record = {}; - let finish: string | null = null; - let done = false; - let content = ""; - let reasoning = ""; - for (const evt of sse.split("\n\n")) { - const dl = evt.split("\n").find((l) => l.startsWith("data:")); - if (!dl) continue; - const data = dl.slice(5).trim(); - if (data === "[DONE]") { done = true; continue; } - try { - const obj = JSON.parse(data); - const ch = obj.choices?.[0]; - if (ch?.finish_reason) finish = ch.finish_reason; - const tcs = ch?.delta?.tool_calls; - if (Array.isArray(tcs)) for (const tc of tcs) { - const i = tc.index ?? 0; - if (tc.function?.name) names[i] = tc.function.name; - if (tc.function?.arguments) frags[i] = (frags[i] ?? "") + tc.function.arguments; - } - if (ch?.delta?.content) content += ch.delta.content; - if (ch?.delta?.reasoning_content) reasoning += ch.delta.reasoning_content; - } catch {} - } - return { frags, names, finish, done, content, reasoning }; -} - -async function rewrite(sse: string, ctx: never) { - const upstream = new ReadableStream({ start(c) { c.enqueue(new TextEncoder().encode(sse)); c.close(); } }); - const out: string[] = []; - for await (const buf of rewriteOpenaiSseStream(upstream, ctx)) out.push(buf.toString("utf8")); - return out.join(""); -} - -const FIXTURE_DIR = new URL("../tests/fixtures/", import.meta.url); - -function fixture(name: string): string { - return readFileSync(new URL(name, FIXTURE_DIR), "utf8"); -} - -test("fixture: real GLM bash tool call passes through with intact arguments", async () => { - const { ctx } = makeCtx(); - const raw = fixture("glm-bash-toolcall.sse"); - const out = await rewrite(raw, ctx); - const parsed = parseToolCalls(out); - assert.equal(parsed.names[0], "bash"); - assert.equal(parsed.finish, "tool_calls"); - assert.equal(parsed.done, true); - const args = JSON.parse(parsed.frags[0]); - assert.equal(args.command, "echo capture-test"); - assert.ok(args.description, "description key preserved"); -}); - -test("fixture: real GLM plain text response passes through verbatim", async () => { - const { ctx } = makeCtx(); - const raw = fixture("glm-text-response.sse"); - const out = await rewrite(raw, ctx); - const parsed = parseToolCalls(out); - assert.equal(parsed.finish, "stop"); - assert.equal(parsed.done, true); - assert.equal(parsed.content, "Done."); -}); - -test("fixture: real GLM reasoning_content (title gen) passes through unmodified", async () => { - const { ctx } = makeCtx(); - const raw = fixture("glm-titlegen-reasoning.sse"); - const out = await rewrite(raw, ctx); - const parsed = parseToolCalls(out); - assert.ok(parsed.reasoning.length > 0, "reasoning_content must survive"); - assert.ok(parsed.content.length > 0, "content must survive"); - assert.equal(parsed.done, true); - assert.ok(!out.includes("acp-proxy:"), "no compress note in pure text stream"); -}); - -test("synthetic: tool call arguments fragmented across many chunks stay intact", async () => { - const { ctx } = makeCtx(); - const frags = ['{"comm', 'and":"echo', ' frag-test",', '"description"', ':"run it"}']; - const chunks = [ - `data: ${JSON.stringify({ object: "chat.completion.chunk", choices: [{ index: 0, delta: { tool_calls: [{ index: 0, id: "c1", type: "function", function: { name: "bash", arguments: frags[0] } }] }, finish_reason: null }] })}\n\n`, - ...frags.slice(1).map((f) => `data: ${JSON.stringify({ object: "chat.completion.chunk", choices: [{ index: 0, delta: { tool_calls: [{ index: 0, function: { arguments: f } }] }, finish_reason: null }] })}\n\n`), - `data: ${JSON.stringify({ object: "chat.completion.chunk", choices: [{ index: 0, delta: {}, finish_reason: "tool_calls" }] })}\n\n`, - `data: [DONE]\n\n`, - ].join(""); - const out = await rewrite(chunks, ctx); - const parsed = parseToolCalls(out); - assert.equal(parsed.names[0], "bash"); - assert.equal(parsed.finish, "tool_calls"); - assert.equal(parsed.done, true); - const args = JSON.parse(parsed.frags[0]); - assert.equal(args.command, "echo frag-test"); - assert.equal(args.description, "run it"); -}); - -test("synthetic: last arguments fragment co-located with finish_reason stays intact", async () => { - const { ctx } = makeCtx(); - const chunks = [ - `data: ${JSON.stringify({ object: "chat.completion.chunk", choices: [{ index: 0, delta: { tool_calls: [{ index: 0, id: "c1", type: "function", function: { name: "read", arguments: '{"filePa' } }] }, finish_reason: null }] })}\n\n`, - `data: ${JSON.stringify({ object: "chat.completion.chunk", choices: [{ index: 0, delta: { tool_calls: [{ index: 0, function: { arguments: 'th":"/a/b.ts"}' } }] }, finish_reason: "tool_calls" }] })}\n\n`, - `data: [DONE]\n\n`, - ].join(""); - const out = await rewrite(chunks, ctx); - const parsed = parseToolCalls(out); - assert.equal(parsed.names[0], "read"); - const args = JSON.parse(parsed.frags[0]); - assert.equal(args.filePath, "/a/b.ts"); - assert.equal(parsed.finish, "tool_calls"); -}); - -test("synthetic: multiple parallel tool calls all pass through", async () => { - const { ctx } = makeCtx(); - const chunks = [ - `data: ${JSON.stringify({ object: "chat.completion.chunk", choices: [{ index: 0, delta: { tool_calls: [{ index: 0, id: "c0", type: "function", function: { name: "bash", arguments: '{"command":"echo a"}' } }] }, finish_reason: null }] })}\n\n`, - `data: ${JSON.stringify({ object: "chat.completion.chunk", choices: [{ index: 0, delta: { tool_calls: [{ index: 1, id: "c1", type: "function", function: { name: "read", arguments: '{"filePath":"/x"}' } }] }, finish_reason: null }] })}\n\n`, - `data: ${JSON.stringify({ object: "chat.completion.chunk", choices: [{ index: 0, delta: {}, finish_reason: "tool_calls" }] })}\n\n`, - `data: [DONE]\n\n`, - ].join(""); - const out = await rewrite(chunks, ctx); - const parsed = parseToolCalls(out); - assert.equal(Object.keys(parsed.names).length, 2); - assert.equal(parsed.names[0], "bash"); - assert.equal(parsed.names[1], "read"); - assert.equal(JSON.parse(parsed.frags[0]).command, "echo a"); - assert.equal(JSON.parse(parsed.frags[1]).filePath, "/x"); -}); - -test("synthetic: usage object in finish chunk survives passthrough", async () => { - const { ctx } = makeCtx(); - const usage = { prompt_tokens: 5330, completion_tokens: 23, total_tokens: 5353 }; - const chunks = [ - `data: ${JSON.stringify({ object: "chat.completion.chunk", choices: [{ index: 0, delta: { tool_calls: [{ index: 0, id: "c1", type: "function", function: { name: "bash", arguments: '{"command":"ls"}' } }] }, finish_reason: null }] })}\n\n`, - `data: ${JSON.stringify({ object: "chat.completion.chunk", choices: [{ index: 0, delta: { content: "" }, finish_reason: "tool_calls" }], usage })}\n\n`, - `data: [DONE]\n\n`, - ].join(""); - const out = await rewrite(chunks, ctx); - assert.ok(out.includes('"prompt_tokens":5330'), "usage object must survive"); - assert.ok(out.includes('"completion_tokens":23'), "completion_tokens must survive"); -}); diff --git a/tests/sse-fixtures.test.ts b/tests/sse-fixtures.test.ts deleted file mode 100644 index b05c9730d2..0000000000 --- a/tests/sse-fixtures.test.ts +++ /dev/null @@ -1,152 +0,0 @@ -import { test } from "node:test"; -import assert from "node:assert/strict"; -import { readFileSync } from "node:fs"; -import { rewriteOpenaiSseStream } from "../src/stream-openai.ts"; -import { createCore, createInitialState, defaultConfig } from "acp-kernel"; - -function makeCtx() { - const core = createCore(); - const config = defaultConfig(200000); - const state = createInitialState(); - return { core, config, state, ctx: { core, config, messages: [], session: { id: "t", meta: {}, stats: { requests: 0, tokensSaved: 0, inputTokens: 0, cachedTokens: 0, outputTokens: 0, cacheSamples: 0, contextTokens: 0 }, metadata: {}, state }, log: () => {} } as never }; -} - -function parseToolCalls(sse: string) { - const frags: Record = {}; - const names: Record = {}; - let finish: string | null = null; - let done = false; - let content = ""; - let reasoning = ""; - for (const evt of sse.split("\n\n")) { - const dl = evt.split("\n").find((l) => l.startsWith("data:")); - if (!dl) continue; - const data = dl.slice(5).trim(); - if (data === "[DONE]") { done = true; continue; } - try { - const obj = JSON.parse(data); - const ch = obj.choices?.[0]; - if (ch?.finish_reason) finish = ch.finish_reason; - const tcs = ch?.delta?.tool_calls; - if (Array.isArray(tcs)) for (const tc of tcs) { - const i = tc.index ?? 0; - if (tc.function?.name) names[i] = tc.function.name; - if (tc.function?.arguments) frags[i] = (frags[i] ?? "") + tc.function.arguments; - } - if (ch?.delta?.content) content += ch.delta.content; - if (ch?.delta?.reasoning_content) reasoning += ch.delta.reasoning_content; - } catch {} - } - return { frags, names, finish, done, content, reasoning }; -} - -async function rewrite(sse: string, ctx: never) { - const upstream = new ReadableStream({ start(c) { c.enqueue(new TextEncoder().encode(sse)); c.close(); } }); - const out: string[] = []; - for await (const buf of rewriteOpenaiSseStream(upstream, ctx)) out.push(buf.toString("utf8")); - return out.join(""); -} - -const FIXTURE_DIR = new URL("../tests/fixtures/", import.meta.url); - -function fixture(name: string): string { - return readFileSync(new URL(name, FIXTURE_DIR), "utf8"); -} - -test("fixture: real GLM bash tool call passes through with intact arguments", async () => { - const { ctx } = makeCtx(); - const raw = fixture("glm-bash-toolcall.sse"); - const out = await rewrite(raw, ctx); - const parsed = parseToolCalls(out); - assert.equal(parsed.names[0], "bash"); - assert.equal(parsed.finish, "tool_calls"); - assert.equal(parsed.done, true); - const args = JSON.parse(parsed.frags[0]); - assert.equal(args.command, "echo capture-test"); - assert.ok(args.description, "description key preserved"); -}); - -test("fixture: real GLM plain text response passes through verbatim", async () => { - const { ctx } = makeCtx(); - const raw = fixture("glm-text-response.sse"); - const out = await rewrite(raw, ctx); - const parsed = parseToolCalls(out); - assert.equal(parsed.finish, "stop"); - assert.equal(parsed.done, true); - assert.equal(parsed.content, "Done."); -}); - -test("fixture: real GLM reasoning_content (title gen) passes through unmodified", async () => { - const { ctx } = makeCtx(); - const raw = fixture("glm-titlegen-reasoning.sse"); - const out = await rewrite(raw, ctx); - const parsed = parseToolCalls(out); - assert.ok(parsed.reasoning.length > 0, "reasoning_content must survive"); - assert.ok(parsed.content.length > 0, "content must survive"); - assert.equal(parsed.done, true); - assert.ok(!out.includes("acp-proxy:"), "no compress note in pure text stream"); -}); - -test("synthetic: tool call arguments fragmented across many chunks stay intact", async () => { - const { ctx } = makeCtx(); - const frags = ['{"comm', 'and":"echo', ' frag-test",', '"description"', ':"run it"}']; - const chunks = [ - `data: ${JSON.stringify({ object: "chat.completion.chunk", choices: [{ index: 0, delta: { tool_calls: [{ index: 0, id: "c1", type: "function", function: { name: "bash", arguments: frags[0] } }] }, finish_reason: null }] })}\n\n`, - ...frags.slice(1).map((f) => `data: ${JSON.stringify({ object: "chat.completion.chunk", choices: [{ index: 0, delta: { tool_calls: [{ index: 0, function: { arguments: f } }] }, finish_reason: null }] })}\n\n`), - `data: ${JSON.stringify({ object: "chat.completion.chunk", choices: [{ index: 0, delta: {}, finish_reason: "tool_calls" }] })}\n\n`, - `data: [DONE]\n\n`, - ].join(""); - const out = await rewrite(chunks, ctx); - const parsed = parseToolCalls(out); - assert.equal(parsed.names[0], "bash"); - assert.equal(parsed.finish, "tool_calls"); - assert.equal(parsed.done, true); - const args = JSON.parse(parsed.frags[0]); - assert.equal(args.command, "echo frag-test"); - assert.equal(args.description, "run it"); -}); - -test("synthetic: last arguments fragment co-located with finish_reason stays intact", async () => { - const { ctx } = makeCtx(); - const chunks = [ - `data: ${JSON.stringify({ object: "chat.completion.chunk", choices: [{ index: 0, delta: { tool_calls: [{ index: 0, id: "c1", type: "function", function: { name: "read", arguments: '{"filePa' } }] }, finish_reason: null }] })}\n\n`, - `data: ${JSON.stringify({ object: "chat.completion.chunk", choices: [{ index: 0, delta: { tool_calls: [{ index: 0, function: { arguments: 'th":"/a/b.ts"}' } }] }, finish_reason: "tool_calls" }] })}\n\n`, - `data: [DONE]\n\n`, - ].join(""); - const out = await rewrite(chunks, ctx); - const parsed = parseToolCalls(out); - assert.equal(parsed.names[0], "read"); - const args = JSON.parse(parsed.frags[0]); - assert.equal(args.filePath, "/a/b.ts"); - assert.equal(parsed.finish, "tool_calls"); -}); - -test("synthetic: multiple parallel tool calls all pass through", async () => { - const { ctx } = makeCtx(); - const chunks = [ - `data: ${JSON.stringify({ object: "chat.completion.chunk", choices: [{ index: 0, delta: { tool_calls: [{ index: 0, id: "c0", type: "function", function: { name: "bash", arguments: '{"command":"echo a"}' } }] }, finish_reason: null }] })}\n\n`, - `data: ${JSON.stringify({ object: "chat.completion.chunk", choices: [{ index: 0, delta: { tool_calls: [{ index: 1, id: "c1", type: "function", function: { name: "read", arguments: '{"filePath":"/x"}' } }] }, finish_reason: null }] })}\n\n`, - `data: ${JSON.stringify({ object: "chat.completion.chunk", choices: [{ index: 0, delta: {}, finish_reason: "tool_calls" }] })}\n\n`, - `data: [DONE]\n\n`, - ].join(""); - const out = await rewrite(chunks, ctx); - const parsed = parseToolCalls(out); - assert.equal(Object.keys(parsed.names).length, 2); - assert.equal(parsed.names[0], "bash"); - assert.equal(parsed.names[1], "read"); - assert.equal(JSON.parse(parsed.frags[0]).command, "echo a"); - assert.equal(JSON.parse(parsed.frags[1]).filePath, "/x"); -}); - -test("synthetic: usage object in finish chunk survives passthrough", async () => { - const { ctx } = makeCtx(); - const usage = { prompt_tokens: 5330, completion_tokens: 23, total_tokens: 5353 }; - const chunks = [ - `data: ${JSON.stringify({ object: "chat.completion.chunk", choices: [{ index: 0, delta: { tool_calls: [{ index: 0, id: "c1", type: "function", function: { name: "bash", arguments: '{"command":"ls"}' } }] }, finish_reason: null }] })}\n\n`, - `data: ${JSON.stringify({ object: "chat.completion.chunk", choices: [{ index: 0, delta: { content: "" }, finish_reason: "tool_calls" }], usage })}\n\n`, - `data: [DONE]\n\n`, - ].join(""); - const out = await rewrite(chunks, ctx); - assert.ok(out.includes('"prompt_tokens":5330'), "usage object must survive"); - assert.ok(out.includes('"completion_tokens":23'), "completion_tokens must survive"); -}); diff --git a/tests/tag-echo.test.ts b/tests/tag-echo.test.ts index 68558f4ff6..637177e0b9 100644 --- a/tests/tag-echo.test.ts +++ b/tests/tag-echo.test.ts @@ -4,9 +4,10 @@ import type { Config, CoreMessage } from "acp-kernel"; import { createCore, createInitialState } from "acp-kernel"; import type { Session } from "../src/session.ts"; import { runCompressLoop, createOpenaiAdapter, createAnthropicAdapter, createResponsesAdapter } from "../src/loop/index.ts"; -import { stripAcpTags, createTagEchoFilter, containsRenderTagText, containsToolCallXmlFragment } from "../src/loop/tag-echo-filter.ts"; +import { stripAcpTags, createTagEchoFilter, containsRenderTagText, containsToolCallXmlFragment, mayStartRenderTag } from "../src/loop/tag-echo-filter.ts"; import { rewriteJsonResponse } from "../src/stream.ts"; import { rewriteOpenaiJsonResponse } from "../src/stream-openai.ts"; +import { rewriteResponsesJsonResponse } from "../src/stream-responses.ts"; import { buildCompressSystemPrompt } from "../src/compress-tool.ts"; const TAG = (ref: string, tokens = 177) => `\x3cacp tokens="${tokens}" type="text">${ref}\x3c/acp>`; @@ -295,6 +296,45 @@ test("non-stream openai rewriteOpenaiJsonResponse strips echoed tags", () => { assert.equal(parsed.choices[0].message.content, "answer done"); }); +test("non-stream responses rewriteResponsesJsonResponse strips echoed tags with no compress call (#460)", () => { + const body = { + id: "resp_1", + output: [{ type: "message", role: "assistant", content: [{ type: "output_text", text: `answer ${TAG("m00155")} done` }] }], + status: "incomplete", + }; + const rewritten = rewriteResponsesJsonResponse(structuredClone(body), { core: createCore(), config: { modelContextLimit: 200000 } as Config, messages: [], session: makeCtx("ns-responses").session, log: () => {} }); + const parsed = rewritten as { output: Array<{ content: Array<{ text: string }> }> }; + assert.equal(parsed.output[0].content[0].text, "answer done"); + assert.equal(JSON.stringify(rewritten).includes(OPEN), false, "a render tag survived the responses JSON rewrite"); +}); + +test("non-stream responses rewriteResponsesJsonResponse strips sibling prose but never the compress note (#460)", () => { + const args = "{\"content\":[]}"; + const clean = { + id: "resp_c", + output: [ + { type: "message", role: "assistant", content: [{ type: "output_text", text: "plain prose" }] }, + { type: "function_call", name: "compress", call_id: "c1", arguments: args }, + ], + }; + const echoed = structuredClone(clean) as { output: Array<{ content: Array<{ text: string }> }> }; + echoed.output[0].content[0].text = `plain prose${TAG("m00155")}`; + const cleanOut = rewriteResponsesJsonResponse(structuredClone(clean), { core: createCore(), config: { modelContextLimit: 200000 } as Config, messages: [], session: makeCtx("ns-note-clean").session, log: () => {} }); + const echoOut = rewriteResponsesJsonResponse(echoed, { core: createCore(), config: { modelContextLimit: 200000 } as Config, messages: [], session: makeCtx("ns-note-echo").session, log: () => {} }); + const note = (r: unknown) => { + const o = (r as { output?: Array<{ content?: Array<{ text: string }> }> }).output ?? []; + return o[0]?.content?.[0]?.text ?? ""; + }; + const prose = (r: unknown) => { + const o = (r as { output?: Array<{ content?: Array<{ text: string }> }> }).output ?? []; + return o[1]?.content?.[0]?.text ?? ""; + }; + assert.equal(note(cleanOut), note(echoOut), "the synthesized compress record must not be edited by the strip"); + assert.equal(prose(cleanOut), prose(echoOut), "echoed sibling prose must be stripped to the same text as clean prose"); + assert.ok(note(echoOut).length > 0, "no note was injected at all"); + assert.equal(JSON.stringify(echoOut).includes(OPEN), false, "no render tag reached the client body"); +}); + test("non-stream rewriters leave tag-free text untouched", async () => { const anthropicBody = { id: "m", content: [{ type: "text", text: "clean 5 < 6 text" }], usage: { input_tokens: 1, output_tokens: 1 } }; const cc = makeCtx("ns-clean"); @@ -399,3 +439,18 @@ test("tag-echo filter does not strip tool-call XML fragments (warn-only, #361)", const out = f.push(`done ${LT}/invoke> ${LT}/tool_calls>`) + f.flush(); assert.equal(out, `done ${LT}/invoke> ${LT}/tool_calls>`); }); + +test("mayStartRenderTag engages on complete tags and tag-head tails, not prose", () => { + assert.equal(mayStartRenderTag(TAG("m0042")), true); + assert.equal(mayStartRenderTag(`prose ${OPEN}tokens="1"`), true); + assert.equal(mayStartRenderTag("ok \x3c"), true); + assert.equal(mayStartRenderTag("ok \x3c/"), true); + assert.equal(mayStartRenderTag("ok \x3ca"), true); + assert.equal(mayStartRenderTag("ok \x3cac"), true); + assert.equal(mayStartRenderTag(`x ${LT}/ac`), true); + assert.equal(mayStartRenderTag(""), false); + assert.equal(mayStartRenderTag("plain text"), false); + assert.equal(mayStartRenderTag("a < b"), false); + assert.equal(mayStartRenderTag("x\x3caction y"), false); + assert.equal(mayStartRenderTag("\x3cdiv>"), false); +});