diff --git a/CONFIGURATION.md b/CONFIGURATION.md index b7ec2cb075..85eaef3bb6 100644 --- a/CONFIGURATION.md +++ b/CONFIGURATION.md @@ -356,9 +356,10 @@ Environment variables take precedence over the config file. They are useful for | `BILLION_CONTEXT_PROXY` | Exported by the launcher; client-side bili plugins/extensions detect it and self-disable their own compression (no double compression). | | `BILLION_CONTEXT_PLUGIN` | Set `0` to disable plugin mode entirely (wire-level tool injection resumes). | | `BILI_LAUNCHER_MODEL_WINDOWS` | Internal: the launcher hands the client's own per-model context windows (pi `models.json`, omp `models.yml`, opencode `models..limit`, codex `model_context_window`) to the spawned proxy as JSON, so the nudge denominator matches the real window for self-hosted models. Only the launcher sets it — no user configuration. | -| `BILI_LAUNCHER_PLUGIN` | Set `0` to disable the launcher's bili MCP server injection for claude/codex (pure wire mode). Native MCP tools are injected by default. See [Launcher Reference](#launcher-reference). | +| `BILI_LAUNCHER_PLUGIN` | Set `0` to disable the launcher's bili MCP server injection for claude/codex (pure wire mode); `1` forces plugin mode. Default: injected — except codex with a local/private upstream (sglang/vllm/ollama cannot parse codex's namespace tool type, so bili auto-falls back to wire tools there). See [Launcher Reference](#launcher-reference). | | `BILI_LAUNCHER_DIRECT` | Set `1` for direct-URL routing in the launcher (drop MITM/CA trust). See [Launcher Reference](#launcher-reference). | | `BILI_CLAUDE_UPSTREAM` | claude direct mode: your relay endpoint, when `ANTHROPIC_BASE_URL` already points at a relay the launcher would otherwise bypass. | +| `BILI_CODEX_COMPACT` | Codex native-compaction handling. Default `intercept`: bili intercepts codex's compaction requests and forges a local handoff to the ACP state when the safety gate passes (transform ok + steady-state usage < 90% of the window + at least one active compressed block) — trigger form forges a 2-frame SSE, endpoint form forges `{output}` — and never contacts upstream. Forged ACP summaries are re-injected as a history-borne handoff message (developer-message fallback) so compressed content stays visible after codex truncates its history. Set `pass` to opt out and forward codex's compaction requests upstream (native compaction backstops). On any gate failure the request passes through untouched. | --- @@ -449,6 +450,20 @@ Run `codex login` as usual; the OAuth token travels in the `Authorization` heade "baseUrl": "http://localhost:8787/bili/https://api.anthropic.com" ``` +**Claude Code** — set the `ANTHROPIC_BASE_URL` env var to the `/bili/` URL. (claude's undici fetch ignores `HTTPS_PROXY`, so the `/bili/` URL form is the only manual option — cert MITM cannot intercept it.) + +```bash +export ANTHROPIC_BASE_URL="http://localhost:8787/bili/https://api.anthropic.com" +``` + +> **Auto-compact alignment (manual mode only).** The `bili claude` launcher automatically sets `CLAUDE_CODE_AUTO_COMPACT_WINDOW` to bili's effective window for your model, so claude's own auto-compact threshold lines up with bili's compression budget. In manual `/bili/` mode you must do this yourself — otherwise claude may run its own local auto-compact (a "summarize the conversation" turn) on a threshold that doesn't match bili's window. That is usually harmless (same session-id, so bili re-derives state from the truncation) but noisier than needed. Set it to bili's effective window for your model: +> +> ```bash +> export CLAUDE_CODE_AUTO_COMPACT_WINDOW= +> ``` +> +> claude clamps this value **down** to the window it perceives for the model (never up), so over-setting is safe. You can also set it persistently via claude's settings (`autoCompactWindow`). + **Other API-key clients** (Cursor / Aider / Continue …) — wherever the upstream URL is configured, prepend `http://localhost:8787/bili/`. Nothing else changes. The `/bili/` prefix doubles as a **self-detection signal**: billion-context client extensions (billion-context-pi / opencode-acp) recognize it in their own baseUrl and self-disable, so you never get double compression. @@ -545,6 +560,7 @@ The `/bili/` rewrite modes write a **temp copy** — the real config is never ed - **omp** — does NOT ship the plugin; the launcher auto-injects `-e dist/agent/omp.js` when the config carries no loadable bili entry (same zero-config ride as pi). Two omp-specific mechanics make the plugin fully native there: omp 17.x mounts extension tools that omit `loadMode` under its `xd://` device URLs (invisible to the model's main turn), so the plugin registers its tools with `loadMode: "essential"` — the model gets the four ACP tools natively; and since omp's fork emits no `before_provider_headers`, the plugin binds the conversation via the launcher identity register (`POST /__bili/plugin/register`, keyed by omp's session id = `prompt_cache_key`/`x-session-id`) — bound sessions run in plugin mode (wire injection suppressed) with the native `/acp` command. - **opencode** — the temp config appends the thin plugin automatically. - **claude / codex** — on by default: the launcher injects a single `bili` MCP server (`--mcp-config` for claude, `-c mcp_servers.bili.*` for codex — both ephemeral, nothing written to host config), so the host gets native tools out of the box (verified with claude 2.1.227 / codex 0.147.0). `BILI_LAUNCHER_PLUGIN=0` falls back to plain wire mode — for hosts older than the verified builds that have not been tested against the injection flags. +- **codex + self-hosted upstream auto-fallback** — codex 0.147 ships MCP tools to the model as a `namespace` tool type; self-hosted servers (sglang/vllm/ollama/llama.cpp) do not parse it, leaving the tools silently invisible. When the codex upstream host is loopback/private (`127.0.0.1`, RFC1918, ULA, `.local`, …) and `BILI_LAUNCHER_PLUGIN` is unset, bili automatically uses wire mode instead (flat tools every server understands) and says so on stderr. `BILI_LAUNCHER_PLUGIN=1` forces plugin mode regardless. - **hermes** — no plugin API; always wire mode. - **dsh** — the launcher always splices a `--patch ` flag into dsh's argv (written to `~/.dsh-bili/.bili-acp.patch.yml`), inserting `dist/agent/dsh-acp.js` into the profile's loader tree: the native `/acp` command, same shape as dsh's own `/compact`. Works on every profile that composes the commands service (web/tui interactive surfaces; the `headless` one-shot driver sends its task straight to the model and parses no commands — `/compact` behaves the same there). Subcommand forms are handled: `dsh web` gets the flag after `web`, `dsh plugin`/`--dump-default-config` take none. diff --git a/CONFIGURATION.zh-CN.md b/CONFIGURATION.zh-CN.md index ff51a49030..e196d512ff 100644 --- a/CONFIGURATION.zh-CN.md +++ b/CONFIGURATION.zh-CN.md @@ -356,9 +356,10 @@ | `BILLION_CONTEXT_PROXY` | launcher 会导出它;客户端侧 bili 插件/扩展检测到后自禁用自身压缩(避免双重压缩)。 | | `BILLION_CONTEXT_PLUGIN` | 设 `0` 彻底关闭插件模式(恢复 wire 层工具注入)。 | | `BILI_LAUNCHER_MODEL_WINDOWS` | 内部使用:launcher 把客户端自身配置里的逐模型上下文窗口(pi `models.json`、omp `models.yml`、opencode `models..limit`、codex `model_context_window`)以 JSON 传给自己拉起的代理,让 nudge 分母对自托管模型也用真实窗口。只有 launcher 会设置,无需用户配置。 | -| `BILI_LAUNCHER_PLUGIN` | 设 `0` 关闭 launcher 为 claude/codex 注入 bili MCP 服务器(退回纯 wire 模式)。原生 MCP 工具默认注入。见[启动器参考](#启动器参考)。 | +| `BILI_LAUNCHER_PLUGIN` | 设 `0` 关闭 launcher 为 claude/codex 注入 bili MCP 服务器(退回纯 wire 模式);设 `1` 强制插件模式。默认注入——但 codex 上游为本地/私网地址时自动退回 wire 模式(sglang/vllm/ollama 不解析 codex 的 namespace 工具类型)。见[启动器参考](#启动器参考)。 | | `BILI_LAUNCHER_DIRECT` | 设 `1` 启用 launcher 直连 URL 路由(放弃 MITM/CA 信任)。见[启动器参考](#启动器参考)。 | | `BILI_CLAUDE_UPSTREAM` | claude 直连模式:当 `ANTHROPIC_BASE_URL` 已指向某个 relay 时,用它指定你的 relay 端点(否则会被旁路)。 | +| `BILI_CODEX_COMPACT` | codex 原生压缩处理。默认 `intercept`:安全门通过时(transform 成功 + 稳态用量 < 窗口 90% + 至少一个活跃压缩块)拦截 codex 的压缩请求,在本地伪造向 ACP 状态的交接——trigger 形态伪造 2 帧 SSE,endpoint 形态伪造 `{output}`——且不接触上游。伪造的 ACP 摘要经历史承载交接消息注入(缺席时 developer 消息兜底),保证 codex 截断历史后压缩内容仍可见。设为 `pass` 可退出,把 codex 的压缩请求转发给上游(原生压缩兜底)。任一安全门失败则原样透传。 | --- @@ -449,6 +450,20 @@ openai_base_url = "http://localhost:8787/bili/https://chatgpt.com/backend-api/co "baseUrl": "http://localhost:8787/bili/https://api.anthropic.com" ``` +**Claude Code** —— 把 `ANTHROPIC_BASE_URL` 环境变量设成 `/bili/` URL。(claude 的 undici fetch 忽略 `HTTPS_PROXY`,所以 `/bili/` URL 形式是唯一的手动方式 —— 证书 MITM 拦不到它。) + +```bash +export ANTHROPIC_BASE_URL="http://localhost:8787/bili/https://api.anthropic.com" +``` + +> **自动压缩对齐(仅手动模式)。** `bili claude` launcher 会自动把 `CLAUDE_CODE_AUTO_COMPACT_WINDOW` 设成 bili 对你模型的有效窗口,让 claude 自己的自动压缩阈值与 bili 的压缩预算对齐。手动 `/bili/` 模式下需要你自己设 —— 否则 claude 可能在与 bili 窗口不一致的阈值上跑它自己的本地自动压缩(一次“总结对话”轮次)。这通常无害(同一 session-id,bili 会从截断中重新推导状态),但比必要的更吵。把它设成 bili 对你模型的有效窗口: +> +> ```bash +> export CLAUDE_CODE_AUTO_COMPACT_WINDOW= +> ``` +> +> claude 会把这个值**向下**钳制到它自己感知的模型窗口(不会向上),所以设大了是安全的。也可以持久化到 claude 的 settings(`autoCompactWindow`)里。 + **其他 API-key 客户端(Cursor / Aider / Continue ……)** —— 只要配置了上游 URL,前面加 `http://localhost:8787/bili/` 就行,其他都不用改。 `/bili/` 前缀还是个**自检测信号**:billion-context 的客户端扩展(billion-context-pi / opencode-acp)能在自己的 baseUrl 里认出它并自禁用,避免双层压缩。 @@ -545,6 +560,7 @@ Claude Code 的 undici fetch 忽略 `HTTPS_PROXY`,所以证书 MITM 拦不到 - **omp** —— 发行版不自带插件;启动器在配置里没有可加载的 bili 条目时自动注入 `-e dist/agent/omp.js`(与 pi 相同的零配置搭车)。两个 omp 专属机制让插件在那里完全原生:omp 17.x 会把未声明 `loadMode` 的扩展工具挂到 `xd://` 设备 URL 下(模型主回合看不到),插件因此用 `loadMode: "essential"` 注册 —— 模型直接拿到四个 ACP 原生工具;omp 分叉不发 `before_provider_headers`,插件改走启动器身份注册(`POST /__bili/plugin/register`,以 omp 会话 id = `prompt_cache_key`/`x-session-id` 为键)绑定会话 —— 绑定后的会话进入插件模式(抑制 wire 注入)并带有原生 `/acp` 命令。 - **opencode** —— 临时配置自动追加薄插件。 - **claude / codex** —— 默认开启:启动器注入单个 `bili` MCP 服务器(claude 用 `--mcp-config`,codex 用 `-c mcp_servers.bili.*` —— 都是临时生效,不写宿主配置),开箱即原生工具(已在 claude 2.1.227 / codex 0.147.0 验证)。`BILI_LAUNCHER_PLUGIN=0` 退回纯 wire 模式 —— 适用于早于已验证版本、未针对注入参数测试的宿主。 +- **codex + 自建上游自动回退** —— codex 0.147 把 MCP 工具以 `namespace` 工具类型发给模型;自建推理服务(sglang/vllm/ollama/llama.cpp)不解析该类型,工具会静默失明。当 codex 上游主机是环回/私网地址(`127.0.0.1`、RFC1918、ULA、`.local` 等)且未设置 `BILI_LAUNCHER_PLUGIN` 时,bili 自动改用 wire 模式(扁平工具,所有服务都认识)并在 stderr 说明。`BILI_LAUNCHER_PLUGIN=1` 可强制插件模式。 - **hermes** —— 无插件 API;永远 wire 模式。 - **dsh** —— 启动器始终在 dsh 的 argv 里拼接 `--patch `(写入 `~/.dsh-bili/.bili-acp.patch.yml`),把 `dist/agent/dsh-acp.js` 插进 profile 的加载树:原生 `/acp` 命令,与 dsh 自带 `/compact` 同一形态。在任何组合了 commands 服务的 profile(web/tui 交互表面)都可用;`headless` 一次性驱动器把任务直接发给模型、不解析命令(原生 `/compact` 在那里同样不可用)。子命令形态已处理:`dsh web` 的 flag 插在 `web` 之后,`dsh plugin`/`--dump-default-config` 不注入。 diff --git a/package.json b/package.json index 9754168dcc..36d1551ef4 100644 --- a/package.json +++ b/package.json @@ -11,6 +11,7 @@ "scripts": { "build": "tsup", "registry:snapshot": "node scripts/update-registry-snapshot.mjs", + "codex-models:snapshot": "node scripts/update-codex-models-snapshot.mjs", "typecheck": "tsc --noEmit --project tsconfig.build.json", "test": "node --import tsx --test tests/*.test.ts", "start": "node dist/index.js", diff --git a/scripts/update-codex-models-snapshot.mjs b/scripts/update-codex-models-snapshot.mjs new file mode 100644 index 0000000000..32e6ef91cb --- /dev/null +++ b/scripts/update-codex-models-snapshot.mjs @@ -0,0 +1,88 @@ +#!/usr/bin/env node +/** Refresh src/codex-models-snapshot.json from openai/codex (main branch). + +The snapshot is the SLIM form of codex's bundled model table +(codex-rs/models-manager/models.json) — only the fields bili consumes for +budget alignment (#321 PR-E1): slug, contextWindow, maxContextWindow, and the +optional autoCompactTokenLimit / effectiveContextWindowPercent. The full +upstream file is ~424KB of tool/modality metadata bili never reads; the slim +form keeps the bundle small and the contract explicit. + +Run manually or before a release: + npm run codex-models:snapshot + +Node's global fetch ignores http(s)_proxy env vars, so the script tries the +configured shell proxy first (undici ProxyAgent) and falls back to a direct +connection. If BOTH fail the existing snapshot is left untouched (exit 1). +*/ +import { writeFile } from "node:fs/promises"; +import path from "node:path"; +import { fileURLToPath } from "node:url"; + +const SOURCE_URL = "https://raw.githubusercontent.com/openai/codex/main/codex-rs/models-manager/models.json"; +const OUT_FILE = path.join(path.dirname(fileURLToPath(import.meta.url)), "..", "src", "codex-models-snapshot.json"); +const proxyUrl = process.env.https_proxy || process.env.HTTPS_PROXY || process.env.http_proxy || process.env.HTTP_PROXY; + +async function attempt(dispatcher) { + const res = await fetch(SOURCE_URL, { + ...(dispatcher ? { dispatcher } : {}), + signal: AbortSignal.timeout(20_000), + headers: { Accept: "application/json" }, + }); + if (!res.ok) throw new Error(`HTTP ${res.status}`); + return res.json(); +} + +async function fetchFull() { + if (proxyUrl) { + try { + const { ProxyAgent } = await import("undici"); + const full = await attempt(new ProxyAgent({ uri: proxyUrl })); + console.log(`fetched openai/codex via proxy ${proxyUrl}`); + return full; + } catch (e) { + console.log(`proxy attempt failed (${e.message}); trying direct`); + } + } + const full = await attempt(undefined); + console.log("fetched openai/codex direct"); + return full; +} + +let full; +try { + full = await fetchFull(); +} catch (e) { + console.error(`could not fetch ${SOURCE_URL}: ${e.message}`); + console.error(`keeping the existing ${path.basename(OUT_FILE)} untouched`); + process.exit(1); +} + +const models = Array.isArray(full?.models) ? full.models : null; +if (!models || models.length === 0) { + console.error("unexpected upstream shape: expected { models: [...] }"); + console.error(`keeping the existing ${path.basename(OUT_FILE)} untouched`); + process.exit(1); +} + +const slim = models.map((m) => { + const e = { slug: m.slug }; + for (const [k, key] of [ + ["context_window", "contextWindow"], + ["max_context_window", "maxContextWindow"], + ["auto_compact_token_limit", "autoCompactTokenLimit"], + ["effective_context_window_percent", "effectiveContextWindowPercent"], + ]) { + if (m[k] !== null && m[k] !== undefined) e[key] = m[k]; + } + return e; +}); + +const body = JSON.stringify({ + source: `openai/codex main — codex-rs/models-manager/models.json`, + fetchedAt: new Date().toISOString().replace(/\.\d+Z$/, "Z"), + count: slim.length, + models: slim, +}) + "\n"; +await writeFile(OUT_FILE, body, "utf8"); +console.log(`wrote ${OUT_FILE} (${slim.length} models, ${(body.length / 1024).toFixed(1)} KB)`); diff --git a/src/client-config.ts b/src/client-config.ts index 510829baf6..7e4496dc2c 100644 --- a/src/client-config.ts +++ b/src/client-config.ts @@ -8,6 +8,12 @@ import path from "node:path"; export interface ClaudeSettings { anthropicBaseUrl?: string; + /** Model claude runs: settings `env.ANTHROPIC_MODEL` ?? top-level `model`. */ + model?: string; + /** The user's explicit auto-compact window (settings `autoCompactWindow` + * or `env.CLAUDE_CODE_AUTO_COMPACT_WINDOW`) — when set, the launcher + * must NOT override it with its own budget injection (#321). */ + autoCompactWindow?: number; } export interface ModelWindow { @@ -28,6 +34,12 @@ export interface CodexProvider { export interface CodexConfig { modelProvider?: string; openaiBaseUrl?: string; + /** Top-level `model` — the model codex runs (budget alignment, #321). */ + model?: string; + /** Top-level `model_context_window` override (if set). */ + contextWindow?: number; + /** Top-level `model_auto_compact_token_limit` override (if set). */ + autoCompactLimit?: number; /** Top-level `model` + `model_context_window` override pair (if set). */ modelWindows?: ModelWindow[]; providers: Record; @@ -173,18 +185,35 @@ export function readClaudeSettings(homeDir: string, cwd: string, env: NodeJS.Pro path.join(cwd, ".claude", "settings.json"), ]; let anthropicBaseUrl: string | undefined; + let model: string | undefined; + let autoCompactWindow: number | undefined; for (const f of files) { const obj = readJsonObject(f); const settingsEnv = obj?.env; if (settingsEnv && typeof settingsEnv === "object" && !Array.isArray(settingsEnv)) { - const v = (settingsEnv as Record).ANTHROPIC_BASE_URL; + const e = settingsEnv as Record; + const v = e.ANTHROPIC_BASE_URL; if (nonEmpty(v)) anthropicBaseUrl = v; + // env-block values beat same-file top-level settings (claude applies + // the env block as real environment, which outranks settings). + const m = e.ANTHROPIC_MODEL; + if (nonEmpty(m)) model = String(m); + const acw = Number(e.CLAUDE_CODE_AUTO_COMPACT_WINDOW); + if (Number.isFinite(acw) && acw > 0) autoCompactWindow = acw; } + const tm = obj?.model; + if (nonEmpty(tm) && model === undefined) model = String(tm); + const tacw = Number(obj?.autoCompactWindow); + if (Number.isFinite(tacw) && tacw > 0 && autoCompactWindow === undefined) autoCompactWindow = tacw; } // Honor a shell-exported ANTHROPIC_BASE_URL (claude's native override) so // the launcher wraps the relay the user actually uses, not the default. if (!anthropicBaseUrl && nonEmpty(env.ANTHROPIC_BASE_URL)) anthropicBaseUrl = env.ANTHROPIC_BASE_URL; - return anthropicBaseUrl ? { anthropicBaseUrl } : {}; + return { + ...(anthropicBaseUrl ? { anthropicBaseUrl } : {}), + ...(model ? { model } : {}), + ...(autoCompactWindow ? { autoCompactWindow } : {}), + }; } /** @@ -198,6 +227,7 @@ export function parseCodexToml(text: string): CodexConfig { let curProvider: string | null = null; let codexModel: string | undefined; let codexContextWindow: number | undefined; + let codexAutoCompactLimit: number | undefined; for (const rawLine of text.split(/\r?\n/)) { const line = rawLine.trim(); if (!line || line.startsWith("#")) continue; @@ -224,10 +254,14 @@ export function parseCodexToml(text: string): CodexConfig { } else if (curProvider && key === "base_url") { result.providers[curProvider].baseUrl = val; } - } else if (numMatch && table === "" && numMatch[1] === "model_context_window") { - codexContextWindow = Number(numMatch[2]); + } else if (numMatch && table === "") { + if (numMatch[1] === "model_context_window") codexContextWindow = Number(numMatch[2]); + else if (numMatch[1] === "model_auto_compact_token_limit") codexAutoCompactLimit = Number(numMatch[2]); } } + if (codexModel) result.model = codexModel; + if (codexContextWindow) result.contextWindow = codexContextWindow; + if (codexAutoCompactLimit) result.autoCompactLimit = codexAutoCompactLimit; const win = toModelWindow(codexModel, codexContextWindow); if (win) result.modelWindows = [win]; return result; diff --git a/src/codex-compact.ts b/src/codex-compact.ts new file mode 100644 index 0000000000..c009d7281d --- /dev/null +++ b/src/codex-compact.ts @@ -0,0 +1,159 @@ +import { randomUUID } from "node:crypto"; +import type { CompressionBlock } from "acp-kernel"; +import type { Session } from "./session.js"; + +export const CODEX_COMPACT_ID_PREFIX = "fc_bili_"; +export const CODEX_COMPACT_SENTINEL = "bili:acp:"; + +const CODEX_UA_PREFIXES = ["codex_cli_rs/", "codex_exec/"]; + +export type CodexCompactMode = "intercept" | "pass"; + +// Read per-request (not cached at startup) so a running proxy can flip the +// kill-switch without a restart. +export function codexCompactMode(): CodexCompactMode { + const v = process.env.BILI_CODEX_COMPACT?.trim().toLowerCase(); + return v === "pass" ? "pass" : "intercept"; +} + +// The `originator` header is only sent for non-default thread originators, so +// the UA prefix (DEFAULT_ORIGINATOR in codex's default_client.rs) is the +// reliable client signal. +export function isCodexClient(headers: Record): boolean { + const ua = headers["user-agent"]; + if (!ua) return false; + const s = Array.isArray(ua) ? ua[0] : ua; + return typeof s === "string" && CODEX_UA_PREFIXES.some((p) => s.startsWith(p)); +} + +export function hasCompactionTrigger(input: unknown): boolean { + if (!Array.isArray(input)) return false; + const last = input[input.length - 1] as { type?: unknown } | undefined; + return last?.type === "compaction_trigger"; +} + +// Recognize a compaction item WE generated (id prefix or sentinel in the blob). +// Codex echoes it back in the next request; stripping it keeps the summary +// sourced from state (no double-count). Real OpenAI blobs carry neither marker, +// so they are left untouched. +export function isBiliCompactionItem(item: unknown): boolean { + const it = item as { type?: unknown; id?: unknown; encrypted_content?: unknown } | null; + if (!it || it.type !== "compaction") return false; + if (typeof it.id === "string" && it.id.startsWith(CODEX_COMPACT_ID_PREFIX)) return true; + if (typeof it.encrypted_content === "string" && it.encrypted_content.startsWith(CODEX_COMPACT_SENTINEL)) return true; + return false; +} + +export function stripBiliCompactionItems(input: T[]): T[] { + return input.filter((item) => !isBiliCompactionItem(item)); +} + +// The summary text a forged blob carries (sentinel-prefixed plaintext). +export function extractBiliSummary(item: unknown): string | undefined { + const it = item as { encrypted_content?: unknown } | null; + if (!it || typeof it.encrypted_content !== "string") return undefined; + if (!it.encrypted_content.startsWith(CODEX_COMPACT_SENTINEL)) return undefined; + const text = it.encrypted_content.slice(CODEX_COMPACT_SENTINEL.length); + return text.length > 0 ? text : undefined; +} + +// An echoed fc_bili_ compaction item is REPLACED (in place) by a plain user +// message carrying the extracted summary — a history-borne handoff the kernel +// can fold again and codex's retention keeps. Rare bounded duplication (a +// block whose anchors survived the truncation also renders) is accepted over +// data loss. Marker items without an extractable blob (id-prefix-only, e.g. +// minted by an older build) are still dropped; real OpenAI blobs pass through +// untouched. +export function replaceBiliCompactionItems(input: T[]): { items: T[]; replaced: number; dropped: number } { + const items: T[] = []; + let replaced = 0; + let dropped = 0; + for (const item of input) { + if (!isBiliCompactionItem(item)) { + items.push(item); + continue; + } + const summary = extractBiliSummary(item); + if (summary === undefined) { + dropped++; + continue; + } + items.push({ + type: "message", + role: "user", + content: [{ type: "input_text", text: `[bili] context summary after compaction:\n${summary}` }], + } as T); + replaced++; + } + return { items, replaced, dropped }; +} + +// Safety valve: forge ONLY when the transform succeeded, the steady-state +// context is below the 90% threshold (codex's own auto-compact point — above +// it, bili's ACP has failed to keep up and native compaction must backstop), +// and there is at least one active block (a real summary to hand off). +export function codexCompactGate(session: Session, effectiveLimit: number, transformOk: boolean): boolean { + if (!transformOk || effectiveLimit <= 0) return false; + if (session.stats.lastInputTokens >= effectiveLimit * 0.9) return false; + return session.state.blocks.some((b) => b.active); +} + +// Minimal legal forged reply for the trigger form. Streaming: exactly one +// compaction output item + response.completed (codex's parser, +// codex-api/src/sse/responses.rs, ignores every other event kind and requires +// response.completed to parse with id + usage). Non-streaming: the JSON +// response shape {id, output, usage}. +export function buildTriggerForgeBody( + summaryText: string, + usage: { inputTokens: number; outputTokens: number; totalTokens: number }, + stream: boolean, +): { body: string; contentType: string } { + const compactionItem = { + type: "compaction", + id: `${CODEX_COMPACT_ID_PREFIX}${randomUUID()}`, + encrypted_content: `${CODEX_COMPACT_SENTINEL}${summaryText}`, + }; + const usageJson = { + input_tokens: usage.inputTokens, + output_tokens: usage.outputTokens, + total_tokens: usage.totalTokens, + }; + const id = `resp_bili_${randomUUID()}`; + if (!stream) { + return { + body: JSON.stringify({ id, output: [compactionItem], usage: usageJson }), + contentType: "application/json", + }; + } + const frame1 = `data: ${JSON.stringify({ type: "response.output_item.done", item: compactionItem })}\n\n`; + const frame2 = `data: ${JSON.stringify({ type: "response.completed", response: { id, usage: usageJson } })}\n\n`; + return { body: frame1 + frame2, contentType: "text/event-stream" }; +} + +// The kernel renders block summaries as system messages with this header +// (acp-kernel SUMMARY_HEADER). Reuse the exact format so the model reads a +// captured handoff summary the same way it reads a live kernel-rendered one. +const FORGED_SUMMARY_HEADER = "[Compressed conversation section]"; + +export function renderForgedSummary(block: Pick): string { + const body = block.summary.trim(); + const topicLine = block.topic ? `${FORGED_SUMMARY_HEADER} — ${block.topic}` : FORGED_SUMMARY_HEADER; + return body.length === 0 ? topicLine : `${topicLine}\n${body}`; +} + +// The forged compaction item is opaque to the model and codex truncates its +// history to [compaction_item, tail…], so the kernel deactivates the blocks +// covering the truncated prefix on the next replay and their summaries would +// vanish from the model's view. Capture them (kernel render format) at forge +// time; the server re-injects them into the developer message every turn +// (endpoint-form semantics). Append-only + exact-text dedup: a second forge +// accumulates, re-capture of deactivated blocks adds nothing. +export function mergeForgedSummaries(existing: string[] | undefined, blocks: readonly CompressionBlock[]): string[] { + const merged = existing ? [...existing] : []; + for (const block of blocks) { + if (!block.active) continue; + const rendered = renderForgedSummary(block); + if (!merged.includes(rendered)) merged.push(rendered); + } + return merged; +} diff --git a/src/codex-models-snapshot.json b/src/codex-models-snapshot.json new file mode 100644 index 0000000000..1b07d59d27 --- /dev/null +++ b/src/codex-models-snapshot.json @@ -0,0 +1,57 @@ +{ + "source": "openai/codex main (commit 868c9ed) \u2014 codex-rs/models-manager/models.json", + "fetchedAt": "2026-08-28T17:09:46Z", + "count": 10, + "models": [ + { + "slug": "gpt-5.6-sol", + "contextWindow": 272000, + "maxContextWindow": 872000 + }, + { + "slug": "gpt-5.6-terra", + "contextWindow": 272000, + "maxContextWindow": 872000 + }, + { + "slug": "gpt-5.6-luna", + "contextWindow": 272000, + "maxContextWindow": 872000 + }, + { + "slug": "gpt-daybreak-blue-latest", + "contextWindow": 272000, + "maxContextWindow": 872000 + }, + { + "slug": "gpt-daybreak-red-latest", + "contextWindow": 372000, + "maxContextWindow": 372000 + }, + { + "slug": "gpt-5.5", + "contextWindow": 272000, + "maxContextWindow": 272000 + }, + { + "slug": "gpt-5.4", + "contextWindow": 272000, + "maxContextWindow": 1000000 + }, + { + "slug": "gpt-5.4-mini", + "contextWindow": 272000, + "maxContextWindow": 272000 + }, + { + "slug": "gpt-5.2", + "contextWindow": 272000, + "maxContextWindow": 272000 + }, + { + "slug": "codex-auto-review", + "contextWindow": 272000, + "maxContextWindow": 872000 + } + ] +} diff --git a/src/codex-models.ts b/src/codex-models.ts new file mode 100644 index 0000000000..732318a0f6 --- /dev/null +++ b/src/codex-models.ts @@ -0,0 +1,98 @@ +import snapshot from "./codex-models-snapshot.json" with { type: "json" }; +import { isCodexClient } from "./codex-compact.js"; + +export { isCodexClient }; + +/** One entry of codex's bundled model table (slim form — see + * scripts/update-codex-models-snapshot.mjs). Mirrors the window fields of + * codex-rs `protocol/src/openai_models.rs` ModelInfo that drive its budget. */ +export interface CodexModelEntry { + slug: string; + contextWindow?: number; + maxContextWindow?: number; + autoCompactTokenLimit?: number; + effectiveContextWindowPercent?: number; +} + +interface CodexModelsSnapshot { + source: string; + fetchedAt: string; + count: number; + models: CodexModelEntry[]; +} + +const SNAPSHOT = snapshot as CodexModelsSnapshot; +const TABLE: CodexModelEntry[] = SNAPSHOT.models; + +/** codex's unknown-model fallback window (codex-rs + * models-manager/src/model_info.rs `model_info_from_slug`: + * context_window = max_context_window = 272_000). A model that matches NO + * table slug is NOT "unperceived" by codex — it auto-compacts at 90% of + * this, so the min() alignment must treat it as perceived at 272K. */ +export const CODEX_FALLBACK_CONTEXT_WINDOW = 272_000; + +/** codex's perceived window for a model = resolved_context_window() = + * context_window.or(max_context_window) (openai_models.rs). */ +function resolvedWindow(m: CodexModelEntry): number { + return m.contextWindow ?? m.maxContextWindow ?? CODEX_FALLBACK_CONTEXT_WINDOW; +} + +/** Emulates codex's table lookup (models-manager/src/manager.rs + * `construct_model_info_from_candidates`): longest-prefix match where the + * REQUESTED model starts with the table slug, then a single namespaced-suffix + * retry (`custom/gpt-5.3-codex` → match on `gpt-5.3-codex`) for provider-like + * namespaces, then the 272K fallback. Config overrides are intentionally NOT + * emulated — a user's `model_context_window` override can only clamp the + * perception DOWN (to max_context_window), never raise it, so the table + * value is the safe upper bound for alignment. */ +function lookupWindow(model: string): number { + const direct = longestPrefixMatch(model); + if (direct) return resolvedWindow(direct); + const slash = model.indexOf("/"); + if (slash > 0) { + const namespace = model.slice(0, slash); + const suffix = model.slice(slash + 1); + if (!suffix.includes("/") && /^[A-Za-z0-9_-]+$/.test(namespace)) { + const m = longestPrefixMatch(suffix); + if (m) return resolvedWindow(m); + } + } + return CODEX_FALLBACK_CONTEXT_WINDOW; +} + +function longestPrefixMatch(model: string): CodexModelEntry | undefined { + let best: CodexModelEntry | undefined; + for (const m of TABLE) { + if (!model.startsWith(m.slug)) continue; + if (!best || m.slug.length > best.slug.length) best = m; + } + return best; +} + +/** The context window codex BELIEVES a model has (its own bundled table + + * 272K fallback). codex auto-compacts at 90% of this and hard-stops at 95%, + * so bili must never budget a codex client above it (#321 PR-E1). */ +export function codexWindowForModel(model: string): number { + return lookupWindow(model); +} + +export function codexAlignedWindow( + limit: number, + model: string, + headers: Record, +): { limit: number; clamped: boolean } { + if (!isCodexClient(headers)) return { limit, clamped: false }; + const w = codexWindowForModel(model); + if (limit > w) return { limit: w, clamped: true }; + return { limit, clamped: false }; +} + +/** Test hook: replace the bundled table (mirrors registry._setForTest). */ +export function _setCodexTableForTest(models: CodexModelEntry[]): void { + TABLE.length = 0; + TABLE.push(...models); +} + +export function _resetCodexTableForTest(): void { + _setCodexTableForTest([...SNAPSHOT.models]); +} diff --git a/src/launcher.ts b/src/launcher.ts index 20b2235750..14d328755c 100644 --- a/src/launcher.ts +++ b/src/launcher.ts @@ -42,7 +42,9 @@ import { selfPackageRoot, isBiliPiEntry, ompPluginLoadedFrom } from "./plugin-in function selfDistFile(name: string): string { return path.join(selfPackageRoot(), "dist", name); } -import { nonEmpty, resolvePiHome, resolveOmpHome, resolveHermesHome, resolveDshHome, loadClientConfig, collectModelWindows, type ClientConfig, resolveOpencodeConfigFile, type OpencodeConfig, type OpencodeProvider, type HermesConfig, type HermesProvider } from "./client-config.js"; +import { nonEmpty, resolvePiHome, resolveOmpHome, resolveHermesHome, resolveDshHome, loadClientConfig, collectModelWindows, type ClientConfig, type CodexConfig, resolveOpencodeConfigFile, type OpencodeConfig, type OpencodeProvider, type HermesConfig, type HermesProvider } from "./client-config.js"; +import { loadRoutes, resolveConfiguredContextLimit, lookupContextLimit, type ProviderRoutes } from "./config.js"; +import { contextFromRegistry } from "./registry.js"; export { type ClaudeSettings, @@ -349,6 +351,108 @@ export function buildCodexArgs( return args; } +/** + * PR-D (#321): budget alignment for launcher-spawned agents. Resolves the + * context window bili will use as its compression denominator for `model` — + * the same chain the proxy applies, minus the per-request-only sources + * (anthropic-beta header, plugin report, launcher window): + * per-route per-model config declaration → built-in CONTEXT_LIMIT_TABLE + * → models.dev registry (snapshot-first, offline-safe). + */ +export async function resolveLauncherWindow( + model: string | undefined, + routes: ProviderRoutes, + upstreamUrl: string | undefined, +): Promise { + if (!model) return undefined; + let host: string | undefined; + if (upstreamUrl) { + try { + host = new URL(upstreamUrl).hostname; + } catch { + host = undefined; + } + } + return ( + resolveConfiguredContextLimit(routes, upstreamUrl, model) ?? + lookupContextLimit(model) ?? + (await contextFromRegistry(model, host)) + ); +} + +/** + * PR-D (#321): codex's auto-compact budget is keyed off the window CODEX + * believes the model has (its bundled model table — bili has no say in it), + * while bili's ACP compression is keyed off bili's own window resolution. + * Two uncoordinated budgets (#292): when bili's window exceeds codex's + * perception, codex's ledger (server-reported usage, which bili sees) crosses + * codex's ~90% threshold first and fires its native compaction ahead of ACP. + * + * Injecting `-c model_context_window= -c model_auto_compact_token_limit=` + * (W = bili's effective window) makes codex's auto-compact threshold 90%×W — + * ACP (≈55%×W) always fires first, and codex's LOCAL compaction (benign for + * bili: same-session truncation the kernel deactivates by message id) only + * backstops when ACP fails, before codex's 95% hard cap. codex clamps both + * values to its own max_context_window, so an over-generous W degrades to + * codex's own perception instead of overshooting. + * + * Returns [] (no injection) when: + * - no model is configured (nothing to resolve a window for), + * - the user already set `model_context_window` in codex's config.toml + * (bili's proxy uses exactly that value as its launcher window — the + * budget is already aligned by the user's own declaration), + * - bili resolves no window for the model (no authoritative value to inject). + * A user-set `model_auto_compact_token_limit` is honored (not overridden). + */ +/** The base URL codex will actually call: the selected provider's + * `base_url`, else top-level `openai_base_url`, else codex's built-in + * OpenAI default (model-provider-info: `https://api.openai.com/v1`). */ +export function codexUpstreamUrl(codex: CodexConfig | undefined): string { + const provider = codex?.modelProvider ? codex?.providers?.[codex.modelProvider]?.baseUrl : undefined; + return provider ?? codex?.openaiBaseUrl ?? "https://api.openai.com/v1"; +} + +export async function resolveCodexBudgetArgs(opts: { + model: string | undefined; + clientWindow: number | undefined; + clientAutoCompactLimit: number | undefined; + routes: ProviderRoutes; + upstreamUrl: string | undefined; +}): Promise { + const { model, clientWindow, clientAutoCompactLimit, routes, upstreamUrl } = opts; + if (!model || clientWindow) return []; + const window = await resolveLauncherWindow(model, routes, upstreamUrl); + if (!window) return []; + const limit = clientAutoCompactLimit ?? window; + return ["-c", `model_context_window=${window}`, "-c", `model_auto_compact_token_limit=${limit}`]; +} + +/** + * PR-D (#321): claude-code's auto-compact window is a single env knob — + * `CLAUDE_CODE_AUTO_COMPACT_WINDOW` outranks settings and is clamped DOWN to + * the model window claude itself perceives (never up), so injecting bili's + * window is always safe: it tightens claude's threshold to bili's budget when + * bili's window is smaller, and is a no-op when it is larger. + * + * Returns {} (no injection) when: no model resolvable, the user already set + * an explicit auto-compact window (settings `autoCompactWindow` or + * `env.CLAUDE_CODE_AUTO_COMPACT_WINDOW`, or a shell-exported env var), or + * bili resolves no window for the model. + */ +export async function resolveClaudeBudgetEnv(opts: { + model: string | undefined; + userAutoCompactWindow: number | undefined; + shellAutoCompactWindow: string | undefined; + routes: ProviderRoutes; + upstreamUrl: string | undefined; +}): Promise { + const { model, userAutoCompactWindow, shellAutoCompactWindow, routes, upstreamUrl } = opts; + if (!model || userAutoCompactWindow !== undefined || nonEmpty(shellAutoCompactWindow)) return {}; + const window = await resolveLauncherWindow(model, routes, upstreamUrl); + if (!window) return {}; + return { CLAUDE_CODE_AUTO_COMPACT_WINDOW: String(window) }; +} + export function buildClaudeEnv( origin: string, caPath: string, @@ -378,6 +482,48 @@ export function launcherDirectUrl(env: NodeJS.ProcessEnv): boolean { return env.BILI_LAUNCHER_DIRECT === "1"; } +/** True when the host points at self-hosted inference: loopback, RFC1918, + * link-local, IPv6 ULA, or an mDNS/LAN name. Those servers (sglang/vllm/ + * ollama/llama.cpp) do not understand codex's `namespace` tool type, so + * MCP-injected tools would be silently invisible to the model. */ +export function isPrivateUpstreamHost(raw: string): boolean { + let host: string; + try { + host = new URL(raw).hostname.toLowerCase(); + } catch { + return false; + } + if (host === "localhost" || host.endsWith(".local") || host.endsWith(".lan") || host.endsWith(".internal")) return true; + if (host.startsWith("[")) host = host.slice(1, -1); + if (host.includes(":")) { + if (host === "::1" || host === "::") return true; + const first = host.split(":")[0] ?? ""; + if (/^f[cd]/.test(first)) return true; + if (/^fe[89ab]/.test(first)) return true; + const dotted = /::ffff:(\d+\.\d+\.\d+\.\d+)$/.exec(host); + if (dotted && isPrivateIPv4(dotted[1]!)) return true; + const hex = /::ffff:([0-9a-f]{1,4}):([0-9a-f]{1,4})$/.exec(host); + if (hex) { + const w1 = Number.parseInt(hex[1]!, 16); + const w2 = Number.parseInt(hex[2]!, 16); + if (isPrivateIPv4(`${w1 >> 8}.${w1 & 0xff}.${w2 >> 8}.${w2 & 0xff}`)) return true; + } + return false; + } + if (/^\d+\.\d+\.\d+\.\d+$/.test(host)) return isPrivateIPv4(host); + return false; +} + +function isPrivateIPv4(host: string): boolean { + const a = Number.parseInt(host.split(".")[0] ?? "", 10); + const b = Number.parseInt(host.split(".")[1] ?? "", 10); + if (a === 127 || a === 10 || a === 0) return true; + if (a === 172 && b >= 16 && b <= 31) return true; + if (a === 192 && b === 168) return true; + if (a === 169 && b === 254) return true; + return false; +} + /** Plugin-in-launcher MCP injection is ON by default for claude/codex * (zero-config, mirroring the pi/omp/opencode auto-injection): the launcher * injects a single `bili` MCP server so the host gets native tools instead @@ -385,9 +531,22 @@ export function launcherDirectUrl(env: NodeJS.ProcessEnv): boolean { * pure wire mode — for hosts older than the verified builds (claude 2.1.227, * codex 0.147.0) that have not been tested against `--mcp-config` / * `-c mcp_servers.*`. pi/omp/opencode/hermes/dsh are always excluded — they - * have their own native plugin surface (or none). */ -export function launcherInjectMcp(env: NodeJS.ProcessEnv, base: string): boolean { - return base !== "pi" && base !== "omp" && base !== "opencode" && base !== "hermes" && base !== "dsh" && env.BILI_LAUNCHER_PLUGIN !== "0"; + * have their own native plugin surface (or none). + * + * codex auto-fallback: codex 0.147 ships MCP tools to the model as a + * `namespace` tool type. Self-hosted upstreams (sglang/vllm/ollama) do not + * parse it — the injected tools become silently invisible and the model + * fumbles for them. When the codex upstream is a local/private endpoint and + * the user has not chosen explicitly, wire mode (flat tools every server + * understands) is the sane default. `BILI_LAUNCHER_PLUGIN=1` forces plugin + * mode regardless of the upstream. */ +export function launcherInjectMcp(env: NodeJS.ProcessEnv, base: string, codexUpstream?: string): boolean { + if (base === "pi" || base === "omp" || base === "opencode" || base === "hermes" || base === "dsh") return false; + if (env.BILI_LAUNCHER_PLUGIN === "0") return false; + if (base === "codex" && env.BILI_LAUNCHER_PLUGIN === undefined && codexUpstream !== undefined && isPrivateUpstreamHost(codexUpstream)) { + return false; + } + return true; } /** Ephemeral MCP config for --mcp-config / -c mcp_servers.bili.*: a single @@ -1278,6 +1437,9 @@ export async function runLaunch(params: RunLaunchParams, deps: LauncherDeps = {} const config = loadClientConfig(process.env, process.cwd()); const base = baseClientName(params.client); const routes = discoverRoutes(base, config); + // bili's own route graph (same sources the spawned proxy child reads — + // used to resolve the budget-alignment window, #321). + const biliRoutes = loadRoutes(process.env); const domains = dedupeInOrder([...routes.httpsDomains, ...(params.mitmDomains ?? [])]); const handle = await ensureProxyRunning({ host, port, passthrough, debug, mitmDomains: domains, modelWindows: collectModelWindows(config) }, deps); console.error( @@ -1311,11 +1473,16 @@ export async function runLaunch(params: RunLaunchParams, deps: LauncherDeps = {} ); } } - const injectMcp = launcherInjectMcp(process.env, base); + const codexUpstream = base === "codex" ? codexUpstreamUrl(config.codex) : undefined; + const injectMcp = launcherInjectMcp(process.env, base, codexUpstream); if (injectMcp) { console.error(`bili: injecting native bili MCP tools for ${base} (disable with BILI_LAUNCHER_PLUGIN=0).`); } else if (base === "claude" || base === "codex") { - console.error("bili: native MCP tools disabled (BILI_LAUNCHER_PLUGIN=0) — running in pure wire mode."); + if (process.env.BILI_LAUNCHER_PLUGIN === "0") { + console.error("bili: native MCP tools disabled (BILI_LAUNCHER_PLUGIN=0) — running in pure wire mode."); + } else { + console.error(`bili: codex upstream ${codexUpstream} is local/private — self-hosted models cannot see codex namespace MCP tools; using wire-injected flat tools instead (force MCP with BILI_LAUNCHER_PLUGIN=1).`); + } } const origin = handle.origin; if (base === "pi") { @@ -1436,6 +1603,17 @@ export async function runLaunch(params: RunLaunchParams, deps: LauncherDeps = {} } else { env = buildCodexEnv(origin, resolveCombinedCaPath(process.env), process.env); clientArgs = buildCodexArgs(origin, routes.httpRewrites, routes.httpsRewrites, clientArgs); + const budgetArgs = await resolveCodexBudgetArgs({ + model: config.codex?.model, + clientWindow: config.codex?.contextWindow, + clientAutoCompactLimit: config.codex?.autoCompactLimit, + routes: biliRoutes, + upstreamUrl: codexUpstreamUrl(config.codex), + }); + if (budgetArgs.length > 0) { + clientArgs = [...budgetArgs, ...clientArgs]; + console.error(`bili: codex budget aligned — ${budgetArgs.slice(2).join(", ")} (model: ${config.codex?.model})`); + } if (injectMcp && codexConversationId) clientArgs = [...buildCodexMcpArgs(origin, codexConversationId), ...clientArgs]; } } else { @@ -1443,6 +1621,17 @@ export async function runLaunch(params: RunLaunchParams, deps: LauncherDeps = {} ? buildClaudePluginEnv(origin, true, process.env) : buildClaudeEnv(origin, ca, routes.httpRewrites, routes.httpsRewrites, process.env); if (directUrl) env.BILLION_CONTEXT_PROXY = origin; + const claudeBudget = await resolveClaudeBudgetEnv({ + model: nonEmpty(process.env.ANTHROPIC_MODEL) ? process.env.ANTHROPIC_MODEL : config.claude?.model, + userAutoCompactWindow: config.claude?.autoCompactWindow, + shellAutoCompactWindow: process.env.CLAUDE_CODE_AUTO_COMPACT_WINDOW, + routes: biliRoutes, + upstreamUrl: config.claude?.anthropicBaseUrl ?? "https://api.anthropic.com", + }); + Object.assign(env, claudeBudget); + if (claudeBudget.CLAUDE_CODE_AUTO_COMPACT_WINDOW !== undefined) { + console.error(`bili: claude budget aligned — CLAUDE_CODE_AUTO_COMPACT_WINDOW=${claudeBudget.CLAUDE_CODE_AUTO_COMPACT_WINDOW}`); + } if (injectMcp) { const mcpFile = path.join(os.tmpdir(), `bili-mcp-${Date.now()}.json`); fs.writeFileSync(mcpFile, JSON.stringify(buildMcpConfig(origin))); diff --git a/src/prefix-affinity.ts b/src/prefix-affinity.ts index 0a456f9bb9..22a8dbed97 100644 --- a/src/prefix-affinity.ts +++ b/src/prefix-affinity.ts @@ -45,6 +45,28 @@ const MAX_TRACKED_SESSIONS = 256; /** Chains unused for this long stop matching (sessions may outlive tracking). */ const TTL_MS = 7 * 24 * 60 * 60 * 1000; +/** Tail-window reattach window (#316 / PR-B): the incoming's LEADING items + * are searched for as a contiguous run at ANY offset inside each stored + * chain's per-item hashes (the incoming head is the retained suffix of the + * stored history, but a rolling-window client may retain any number of + * items, so the match offset must be free — not pinned to the stored tail). */ +const TAIL_WINDOW = 8; + +/** Minimum window size (items) for a tail-window reattach to be trusted. A + * 1-2 item window is too weak a signal (a single shared message is common); + * below this the request falls through to a new session as before. */ +const MIN_TAIL_MATCH = 3; + +/** Per tracked chain, store at most this many per-item hashes (the trailing + * ones). Bounds memory (256 chains × 128 × 64B ≈ 2MB) and keeps the tail + * window (8) comfortably available. Chains deeper than this lose their head, + * so fork-lineage prefix detection is best-effort for very long chains. */ +const MAX_STORED_ITEMS = 128; + +/** Minimum shared prefix (items) to record a "forked" lineage on a new + * session. UI/debug only — never used for matching. */ +const MIN_FORK_PREFIX = 3; + export interface AnonymousAffinity { /** Stable session id: "pfa-" + short hash of (tail, depth). */ sessionId: string; @@ -56,6 +78,16 @@ export interface AnonymousAffinity { incomingDepth: number; /** Chain hash of the incoming tail (log correlation). */ tailHash: string; + /** How the session was resolved: a full-prefix match, a tail-window + * reattach (truncated replay), or a brand-new session. */ + via: "prefix" | "tail-window" | "new"; + /** Per-item hashes of the incoming (trailing, capped at MAX_STORED_ITEMS) — + * passed to note() so the tracked chain can serve future tail-window + * reattach + fork-lineage lookups. */ + itemHashes: string[]; + /** Lineage for a NEW session: the discarded match candidates and why they + * were abandoned. Recorded for UI/debug only — NEVER used for matching. */ + lineage?: { parents: string[]; reason: "truncated" | "forked"; sharedPrefix?: number }; } interface ChainEntry { @@ -63,6 +95,9 @@ interface ChainEntry { depth: number; tailHash: string; lastSeen: number; + /** Per-item hashes (position-independent sha256 of each canonical + * message), trailing, capped at MAX_STORED_ITEMS. */ + itemHashes: string[]; } /** Deterministic JSON with recursively sorted object keys, so two replays @@ -104,6 +139,22 @@ function chainHashes(messages: unknown[]): string[] { return bytes >= MIN_CANONICAL_BYTES ? hashes : []; } +/** Position-INDEPENDENT per-item hashes: itemHashes[i] = sha256(canonical(msg_i)). + * Unlike the progressive chainHashes (which depend on the full prefix and so + * cannot match across a truncation), these let a window of the incoming head + * be compared against a window of a stored tail item-for-item. */ +function perItemHashes(messages: unknown[]): string[] { + return messages.map((m) => sha256(stableStringify(m))); +} + +/** Length of the longest common prefix of two per-item hash arrays. */ +function lcpLength(a: string[], b: string[]): number { + const n = Math.min(a.length, b.length); + let i = 0; + while (i < n && a[i] === b[i]) i++; + return i; +} + export class PrefixAffinityResolver { private trackedChains = new Map(); @@ -115,40 +166,114 @@ export class PrefixAffinityResolver { resolve(messages: unknown[]): AnonymousAffinity | null { const hashes = chainHashes(messages); if (hashes.length === 0 || !hasUserMessage(messages)) return null; - const tailHash = hashes[hashes.length - 1]!; + const incomingDepth = hashes.length; + const tailHash = hashes[incomingDepth - 1]!; + const incItemHashes = perItemHashes(messages); + const storedItemHashes = incItemHashes.slice(-MAX_STORED_ITEMS); const tracked = this.trackedChains; this.expire(tracked); + // 1. Full-depth prefix match (the original radix-style resolution). let best: ChainEntry | undefined; for (const entry of tracked.values()) { - if (entry.depth > hashes.length) continue; + if (entry.depth > incomingDepth) continue; if (hashes[entry.depth - 1] !== entry.tailHash) continue; if (!best || entry.depth > best.depth || (entry.depth === best.depth && entry.lastSeen > best.lastSeen)) best = entry; } - if (best) { return { sessionId: best.sessionId, matchedDepth: best.depth, storedDepth: best.depth, - incomingDepth: hashes.length, + incomingDepth, + tailHash, + via: "prefix", + itemHashes: storedItemHashes, + }; + } + + // 2. Tail-window reattach (#316 / PR-B): a client that dropped its + // oldest messages replays a retained suffix of the stored history + // (plus new appends), so the incoming's LEADING items must appear + // as a contiguous run somewhere inside the stored chain's item + // hashes — at any offset, not just the stored tail (a rolling-window + // client may retain more items than TAIL_WINDOW). Offset 0 is + // excluded: a head-to-head match is either a full-prefix case + // (step 1) or a distinct conversation sharing a templated head — + // never a truncation reattach (the retained suffix of a truncation + // starts strictly inside the stored chain). + const w = Math.min(TAIL_WINDOW, incomingDepth); + const candidates: ChainEntry[] = []; + if (w >= MIN_TAIL_MATCH) { + for (const entry of tracked.values()) { + const stored = entry.itemHashes; + for (let j = 1; j + w <= stored.length; j++) { + let match = true; + for (let i = 0; i < w; i++) { + if (incItemHashes[i] !== stored[j + i]) { + match = false; + break; + } + } + if (match) { + candidates.push(entry); + break; + } + } + } + } + if (candidates.length === 1) { + const entry = candidates[0]!; + const w = Math.min(TAIL_WINDOW, incomingDepth, entry.depth); + return { + sessionId: entry.sessionId, + matchedDepth: w, + storedDepth: entry.depth, + incomingDepth, tailHash, + via: "tail-window", + itemHashes: storedItemHashes, }; } - // No stored chain is a prefix of the incoming history: this request - // starts a session, anchored deterministically on its current tail so - // an identical replay after a proxy restart reattaches the same id. - const sessionId = `pfa-${sha256(`${tailHash}\u0000${hashes.length}`).slice(0, 16)}`; - return { sessionId, matchedDepth: 0, storedDepth: 0, incomingDepth: hashes.length, tailHash }; + // 3. New session, anchored deterministically on its current tail so an + // identical replay after a proxy restart reattaches the same id. + // Record lineage (UI/debug only) for the discarded candidates. + let lineage: AnonymousAffinity["lineage"]; + if (candidates.length > 1) { + lineage = { parents: candidates.map((c) => c.sessionId), reason: "truncated" }; + } else { + let forkParent: ChainEntry | undefined; + let forkLcp = 0; + for (const entry of tracked.values()) { + if (entry.depth > MAX_STORED_ITEMS) continue; + const lcp = lcpLength(incItemHashes, entry.itemHashes); + if (lcp >= MIN_FORK_PREFIX && lcp > forkLcp) { + forkLcp = lcp; + forkParent = entry; + } + } + if (forkParent) lineage = { parents: [forkParent.sessionId], reason: "forked", sharedPrefix: forkLcp }; + } + const sessionId = `pfa-${sha256(`${tailHash}\u0000${incomingDepth}`).slice(0, 16)}`; + return { + sessionId, + matchedDepth: 0, + storedDepth: 0, + incomingDepth, + tailHash, + via: "new", + itemHashes: storedItemHashes, + ...(lineage ? { lineage } : {}), + }; } /** Record the chain of a session (on creation and after every anonymous * request — the incoming history is the truth, appends extend it). */ - note(sessionId: string, depth: number, tailHash: string): void { + note(sessionId: string, depth: number, tailHash: string, itemHashes: string[]): void { const tracked = this.trackedChains; tracked.delete(sessionId); - tracked.set(sessionId, { sessionId, depth, tailHash, lastSeen: Date.now() }); + tracked.set(sessionId, { sessionId, depth, tailHash, itemHashes, lastSeen: Date.now() }); while (tracked.size > MAX_TRACKED_SESSIONS) { const oldest = [...tracked.values()].sort((a, b) => a.lastSeen - b.lastSeen)[0]; if (!oldest) break; diff --git a/src/server.ts b/src/server.ts index 60d2eac1a2..c2af8e51b3 100644 --- a/src/server.ts +++ b/src/server.ts @@ -1,13 +1,14 @@ import http from "node:http"; import fs from "node:fs"; import { randomUUID } from "node:crypto"; -import { createCore, type CompressionCore, type CompressionState, type Config, type CoreMessage, type NudgeDecision, type Prompts, defaultPrompts, estimateTokensFast, renderNudgeText, deactivateBlock, viableRanges } from "acp-kernel"; +import { createCore, type CompressionCore, type CompressionState, type Config, type CoreMessage, type NudgeDecision, type Prompts, defaultPrompts, defaultCountTokens, estimateTokensFast, renderNudgeText, deactivateBlock, viableRanges } from "acp-kernel"; import { resolveCompress, resolveCompressPrompts, resolveRequestConfig } from "./compress-settings.js"; import type { ProxyOptions } from "./config.js"; import { loadOptions, loadRoutes } from "./config.js"; import { resetProxyCache } from "./upstream-proxy.js"; import { FALLBACK_EFFECTIVE_WINDOW_FLOOR, lookupContextLimit, resolveConfiguredContextLimit, resolveCompressProtocol } from "./config.js"; import { contextFromRegistry, loadRegistry, peekRegistryContext } from "./registry.js"; +import { codexAlignedWindow } from "./codex-models.js"; import { fetchWithTimeout, MAX_REQUEST_BYTES } from "./fetch-util.js"; import { formatUpstreamError, getUpstreamConnectionStatus, recordUpstreamConnection, resolveProxy, resolveProxyDecision, proxyDispatcher } from "./upstream-proxy.js"; import { maskHeaderForLog, maskHeadersForLog, maskHostPortForLog, maskUrlForLog, maskUrlsInText } from "./log-mask.js"; @@ -53,10 +54,12 @@ import { defaultLogFile, stateDir, proxyOriginFile } from "./paths.js"; import { compressLoopResponsesJson } from "./compress-loop-responses.js"; import { runCompressLoop, pickAdapter } from "./loop/index.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"; import { rewriteResponsesJsonResponse } from "./stream-responses.js"; +import { observeResponsesTerminalState } from "./stream-terminal.js"; import { emitStreamError } from "./stream-error.js"; -import { affinityToken, clientConversationHeader, preferPromptCacheKeyIdentity, type ConversationIdentity } from "./session-id.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, pipePluginJson, pipePluginResponsesWithStrip, pipeThroughWithUsage, pluginAgentHeader, pluginConversationHeader, pluginReportedContextWindow, recordPluginSession, rememberPluginMessages, takePendingPluginRegister } from "./plugin.js"; import { setupMitm, readMitmUpstream } from "./mitm.js"; @@ -444,11 +447,15 @@ type Prepared = { * rebuilt payload and compress-loop round must re-inject it. */ openaiSystemText?: string; nudge?: NudgeDecision; - /** Effective compression prompts for this request (three-level cascade, - * defaults to the kernel's defaultPrompts). Carried so the compress loop - * in forward() rebuilds the SAME system prompt the request was prepared - * with. */ + /** Effective compression prompts for this request (three-level cascade, + * defaults to the kernel's defaultPrompts). Carried so the compress loop + * in forward() rebuilds the SAME system prompt the request was prepared + * with. */ prompts?: Prompts; + /** Set when a codex native-compaction request was intercepted and a + * success response was forged locally (BILI_CODEX_COMPACT=intercept + + * gate passed). forward() serves `body` without contacting upstream. */ + codexForge?: { kind: "endpoint" | "trigger"; body: string; contentType: string }; }; @@ -799,6 +806,25 @@ async function handle( if (native) nativeFromFallback = false; } reqConfig = resolveRequestConfig(config, opts.routes, embeddedUrl, model, native, opts.compress); + // #321 PR-E1: a codex client carries its OWN window perception + // (bundled model table + 272K unknown-model fallback) and + // auto-compacts at 90% of it. If bili's budget exceeds what codex + // believes, codex's native compaction fires first — the #292 + // misalignment. Cap the effective window at codex's perception. + // An operator's explicit compress.modelContextLimit is exempt + // (operator tuning is owned by the operator — never floored and + // never clamped); the clamped value is authoritative for this + // client (codex's own config), so it also clears the + // low-confidence fallback flag. + const aligned = operatorWindowTuned + ? { limit: reqConfig.modelContextLimit, clamped: false } + : codexAlignedWindow(reqConfig.modelContextLimit, model, req.headers); + if (aligned.clamped) { + const before = reqConfig.modelContextLimit; + reqConfig = { ...reqConfig, modelContextLimit: aligned.limit }; + nativeFromFallback = false; + log("info", `[codex] effective window clamped ${before} → ${aligned.limit} (codex's own perception for model=${model}; ACP now compresses before codex's native auto-compact)`); + } reqPrompts = resolveCompressPrompts(resolveCompress(opts.routes, embeddedUrl, model, opts.compress)); } } @@ -834,11 +860,22 @@ async function handle( // shared 200-char prefix and leak compression state across sessions. const clientConv = clientConversationHeader(req.headers); const convHeader = clientConv ?? sessionHeader; + // Codex turn-metadata partitioning (#316 / PR-A): when the explicit + // Codex turn metadata is present and cross-checked against the + // thread-id header, partition compression state by thread_source. + // Root ("user") turns keep the session-id header (current semantics, + // stable across turns); subagents get their own thread-id (fresh + // independent state per thread, #150). Untrusted metadata (absent / + // unparseable / mismatched / unknown thread_source) → undefined, and + // the legacy chain below is unchanged. + const codexTurn = protocol === "responses" ? codexTurnIdentity(req.headers) : undefined; const responsesIdentity = protocol === "responses" - ? preferPromptCacheKeyIdentity( - conversationIdentityResponses(parsed as ResponsesRequestBody, convHeader), - parsed as ResponsesRequestBody, - ) + ? (codexTurn + ? { value: codexTurn.value, source: "header" as const, clientProvided: true } + : preferPromptCacheKeyIdentity( + conversationIdentityResponses(parsed as ResponsesRequestBody, convHeader), + parsed as ResponsesRequestBody, + )) : undefined; // OpenAI chat mirrors the responses pck promotion: clients that replay // full history statelessly (omp chat-completions via a relay) send NO @@ -863,10 +900,16 @@ async function handle( ? conversationSignalAnthropic(parsed as AnthropicRequestBody, convHeader) : protocol === "openai" ? openaiIdentity?.value ?? openaiSignal - : subagentNamespace( - responsesIdentity?.value ?? conversationSignalResponses(parsed as ResponsesRequestBody, convHeader), - (parsed as ResponsesRequestBody).instructions, - ); + : codexTurn + // Trusted Codex turn id enters the verbatim session chain + // directly — do NOT route it through subagentNamespace (the + // kernel's empty-instructions non-anchoring path is left + // untouched for metadata-less clients). + ? codexTurn.value + : subagentNamespace( + responsesIdentity?.value ?? conversationSignalResponses(parsed as ResponsesRequestBody, convHeader), + (parsed as ResponsesRequestBody).instructions, + ); // The session ID is the client-provided conversation value VERBATIM — // no hash, no protocol/credential/upstream dimensions (#286): those // are all mutable mid-conversation (bearer rotation, relay switching, @@ -900,12 +943,16 @@ async function handle( : { error: { type: "invalid_request_error", message: NO_IDENTITY_MESSAGE } })); return; } - if (anonAffinity.matchedDepth > 0) { + if (anonAffinity.via === "tail-window") { + log("info", `[prefix-affinity] anonymous ${protocol} request → session ${anonAffinity.sessionId} (tail-window reattach window=${anonAffinity.matchedDepth}/${anonAffinity.incomingDepth}, tail=${anonAffinity.tailHash.slice(0, 8)}; truncated replay reattached, kernel reconciles by message id)`); + loggerLog("info", `[prefix-affinity] session ${anonAffinity.sessionId} reattached via tail-window (window ${anonAffinity.matchedDepth}/${anonAffinity.incomingDepth}, tail=${anonAffinity.tailHash.slice(0, 8)})`); + } else if (anonAffinity.matchedDepth > 0) { log("info", `[prefix-affinity] anonymous ${protocol} request → session ${anonAffinity.sessionId} (prefix match depth=${anonAffinity.matchedDepth}/${anonAffinity.incomingDepth}, tail=${anonAffinity.tailHash.slice(0, 8)}; fork semantics: diverged histories split on their next request)`); loggerLog("info", `[prefix-affinity] session ${anonAffinity.sessionId} matched at depth ${anonAffinity.matchedDepth}/${anonAffinity.incomingDepth} (tail=${anonAffinity.tailHash.slice(0, 8)})`); } else { - log("info", `[prefix-affinity] new anonymous session ${anonAffinity.sessionId} (depth=${anonAffinity.incomingDepth}, tail=${anonAffinity.tailHash.slice(0, 8)})`); - loggerLog("info", `[prefix-affinity] new session ${anonAffinity.sessionId} at depth ${anonAffinity.incomingDepth} (tail=${anonAffinity.tailHash.slice(0, 8)})`); + const lineage = anonAffinity.lineage ? `; lineage=${anonAffinity.lineage.reason} of ${anonAffinity.lineage.parents.join(",")}` : ""; + log("info", `[prefix-affinity] new anonymous session ${anonAffinity.sessionId} (depth=${anonAffinity.incomingDepth}, tail=${anonAffinity.tailHash.slice(0, 8)}${lineage})`); + loggerLog("info", `[prefix-affinity] new session ${anonAffinity.sessionId} at depth ${anonAffinity.incomingDepth} (tail=${anonAffinity.tailHash.slice(0, 8)}${lineage})`); } } const sessionId = anonAffinity ? anonAffinity.sessionId : conversation; @@ -929,10 +976,12 @@ async function handle( : clientConversationHeader(req.headers); const session = getSession(sessionId, { protocol, upstreamOrigin, label: clientLabel ?? (anonAffinity ? "prefix-affinity" : undefined) }); if (anonAffinity) { - prefixAffinity.note(sessionId, anonAffinity.incomingDepth, anonAffinity.tailHash); + prefixAffinity.note(sessionId, anonAffinity.incomingDepth, anonAffinity.tailHash, anonAffinity.itemHashes); session.metadata.anonymousPrefixAffinity = { depth: anonAffinity.incomingDepth, tailHash: anonAffinity.tailHash, + via: anonAffinity.via, + ...(anonAffinity.lineage ? { lineage: anonAffinity.lineage } : {}), }; } // Launcher-mode binding (#162): prefer identity — claude code sends @@ -947,7 +996,10 @@ async function handle( pluginConversation = clientConv ?? conversation; } } - if (!pluginAgent && session.stats.requests === 0) { + if (!pluginAgent && session.stats.requests === 0 && codexTurnIdentity(req.headers) === undefined) { + // A codex subagent thread mints a fresh session too, but it must + // not claim the ROOT conversation's pending register (the plugin + // binding belongs to the root session, #317). const pending = takePendingPluginRegister(); if (pending) { pluginAgent = pending.agent; @@ -1047,7 +1099,7 @@ async function handle( : protocol === "openai" ? prepareOpenai(parsed as OpenAIRequestBody, req, opts, core, reqConfig, reqPrompts, log, session, pluginMode) : responsesCompact - ? prepareResponsesCompact(bodyBuffer, parsed as ResponsesRequestBody, session) + ? prepareResponsesCompact(bodyBuffer, parsed as ResponsesRequestBody, session, req, core, reqConfig, log) : prepareResponses(parsed as ResponsesRequestBody, req, opts, core, reqConfig, reqPrompts, log, session, responsesIdentity!, pluginMode, upstreamOrigin); prepared = runPrepare(); if (!countTokens && !responsesCompact) { @@ -1375,12 +1427,28 @@ function prepareResponses( log("info", `[${sessionId}] reconciled ACP state after native Responses compact boundary`); } + // A codex client echoes our forged compaction item back in the next + // request; replace it with a plain summary-carrying user message so the + // handoff rides the replayable history (kernel-compressible, retained by + // codex's own user-message rule) instead of a foreign opaque blob. Real + // OpenAI blobs carry no bili marker and pass through untouched. + let echoReplaced = false; + if (Array.isArray(parsed.input)) { + const { items, replaced, dropped } = replaceBiliCompactionItems(parsed.input); + if (replaced > 0 || dropped > 0) { + echoReplaced = true; + log("info", `[${sessionId}] replaced ${replaced} echoed bili compaction item(s) with summary handoff message(s)${dropped > 0 ? `, dropped ${dropped} legacy marker item(s)` : ""}`); + parsed.input = items as typeof parsed.input; + } + } + let processedMessages: CoreMessage[] = []; let originalMessages: CoreMessage[] = []; let nudge: NudgeDecision | undefined; let responsesProjection: ResponsesProjection | undefined; let rebuiltInput: ResponseInputItem[] | string = parsed.input; let toolsOut = parsed.tools; + let transformOk = false; // #242: over-long input item ids (poisoned rollouts) 400 upstream on every // request; rewrite them to short deterministic ids before anything reads @@ -1401,6 +1469,10 @@ function prepareResponses( const shouldInject = opts.compress.injectTool; const injectTools = shouldInject && !pluginMode; + // Codex native remote-compact request: no compress prompt/tools (the model + // produces the compaction itself), no acp tags, plain passthrough so the + // response terminal state can gate the rebase marker. + const isCompactionTrigger = hasCompactionTrigger(parsed.input); // Route config is keyed by the upstream THIS request goes to (#286: a // session can outlive its first relay — session.meta.upstreamOrigin is // first-wins and would silently ignore the new relay's route settings). @@ -1416,7 +1488,7 @@ function prepareResponses( log("info", `[${sessionId}] input items: ${Array.isArray(parsed.input) ? parsed.input.map((i: ResponseInputItem) => i.type).join(",") : "(string)"}`); } const tokenCount = session.stats.lastInputTokens; - const turn = core.processTurn({ messages: msgs, state: session.state, config, tokenCount, renderTags: process.env.ACP_RENDER_NONE ? "none" : "text-only" }); + const turn = core.processTurn({ messages: msgs, state: session.state, config, tokenCount, renderTags: process.env.ACP_RENDER_NONE || isCompactionTrigger ? "none" : "text-only" }); session.state = turn.state; // The fold from last turn's compress has now materialized in state — // future usage reports are post-fold reality, drop the credit. @@ -1436,27 +1508,32 @@ function prepareResponses( processedMessages = stripKernelSummaries(turn.messages, turn.state); reapOrphanBlocks(session, msgs, deactivateBlock); rebuiltInput = patchResponsesInput(projection, processedMessages); - if (shouldInject && !process.env.ACP_NO_COMPRESS_PROMPT) { + // Fallback path: when the echo did NOT come back this turn (client + // dropped it / restarted), the history-borne handoff is absent and the + // forge-time captured summaries are re-injected into the developer + // message so the pre-compaction content is never lost. When the echo + // DID come back, the replacement message carries the summaries and + // the injection is suppressed to avoid duplicating them. + const forgedSummaries = echoReplaced + ? [] + : (session.metadata.codexForgedSummaries as string[] | undefined) ?? []; + if (shouldInject && !isCompactionTrigger && !process.env.ACP_NO_COMPRESS_PROMPT) { const prompt = responsesTextProtocol ? buildCompressHybridSystemPrompt(prompts) : buildCompressSystemPrompt(prompts); - const devContent = [...projection.systemParts, prompt].join("\n\n---\n\n"); + const devContent = [...projection.systemParts, ...forgedSummaries, prompt].join("\n\n---\n\n"); rebuiltInput = injectResponsesDeveloperMessage(rebuiltInput, devContent); if (!process.env.ACP_NO_INJECT_TOOL && injectTools) { toolsOut = responsesTextProtocol ? injectResponsesTool(parsed.tools, ACP_READONLY_TOOLS_RESPONSES) : injectResponsesTool(parsed.tools); } - } else if (projection.systemParts.length > 0) { - rebuiltInput = injectResponsesDeveloperMessage(rebuiltInput, projection.systemParts.join("\n\n---\n\n")); + } else if (projection.systemParts.length > 0 || forgedSummaries.length > 0) { + const devContent = [...projection.systemParts, ...forgedSummaries].join("\n\n---\n\n"); + rebuiltInput = injectResponsesDeveloperMessage(rebuiltInput, devContent); } - // Codex's native remote-compact request ends with a `compaction_trigger` - // item that the upstream requires to be the FINAL input item (else 400 - // "must be the final input item"). A nudge appended after it would break - // Codex's own compaction, and is redundant — the native compact IS the - // compression. Skip the nudge for such requests. - const triggerFinal = Array.isArray(rebuiltInput) - && rebuiltInput.length > 0 - && rebuiltInput[rebuiltInput.length - 1]!.type === "compaction_trigger"; - if (opts.compress.injectNudge && turn.nudge?.shouldInject && shouldInject && !triggerFinal) { + // A nudge appended after a trailing `compaction_trigger` would break + // the upstream's "must be the final input item" requirement and is + // redundant — the native compact IS the compression. + if (opts.compress.injectNudge && turn.nudge?.shouldInject && shouldInject && !isCompactionTrigger) { try { const rendered = renderNudgeText(turn.nudge, prompts); if (rendered.text) { @@ -1469,11 +1546,44 @@ function prepareResponses( } catch { } } + transformOk = true; } catch (err) { log("warn", `[${sessionId}] kernel transform failed, forwarding unchanged: ${String(err)}`); processedMessages = []; } + // E2 trigger form: codex's native remote-compaction request (final input item + // is compaction_trigger). When the kill-switch is on, the client is codex, and + // the safety gate passes, forge a success SSE (one compaction item + + // response.completed) and skip upstream — a deterministic handoff to the ACP + // state instead of a foreign compaction blob. + let codexForge: Prepared["codexForge"] | undefined; + if (transformOk + && codexCompactMode() === "intercept" + && isCodexClient(req.headers) + && hasCompactionTrigger(parsed.input) + && codexCompactGate(session, config.modelContextLimit, transformOk)) { + const summaries = session.state.blocks.filter((b) => b.active).map((b) => b.summary); + const prevForged = session.metadata.codexForgedSummaries as string[] | undefined; + const captured = mergeForgedSummaries(prevForged, session.state.blocks); + if (captured.length !== (prevForged?.length ?? 0)) { + session.metadata.codexForgedSummaries = captured; + markDirty(session); + } + // Codex recomputes its ledger from the next real request, but the + // usage we mint here must not read as an empty context: fall back to + // estimating the trigger payload itself when lastInputTokens is + // stale/zero. The reply honors parsed.stream (JSON body when not + // streaming). + const est = defaultCountTokens(typeof parsed.input === "string" ? parsed.input : JSON.stringify(parsed.input ?? "")); + const total = Math.max(session.stats.lastInputTokens, est, 1); + codexForge = { + kind: "trigger", + ...buildTriggerForgeBody(summaries.join("\n\n"), { inputTokens: total, outputTokens: 0, totalTokens: total }, stream), + }; + log("info", `[${sessionId}] codex compact intercepted (trigger); forged SSE with ${summaries.length} block summary(s), upstream not contacted`); + } + const rebuilt: ResponsesRequestBody = { ...parsed, input: rebuiltInput, tools: toolsOut }; // Route with the upstream THIS request goes to — session.meta.upstreamOrigin // is first-wins and would keep injecting pck toward a relay we switched @@ -1522,11 +1632,13 @@ function prepareResponses( responsesProjection, protocol: "responses", stream, - compressInjected: injectTools, + compressInjected: injectTools && !isCompactionTrigger, pluginMode, responsesTextProtocol, nudge, prompts, + resetAfterSuccess: isCompactionTrigger, + codexForge, }; } @@ -1576,10 +1688,24 @@ export function prepareCountTokens( } } -function prepareResponsesCompact(body: Buffer, parsed: ResponsesRequestBody, session: Session): Prepared { +function prepareResponsesCompact( + body: Buffer, + parsed: ResponsesRequestBody, + session: Session, + req: http.IncomingMessage, + core: CompressionCore, + config: Config, + log: (level: string, msg: string) => void, +): Prepared { ++session.stats.requests; - return { - body, + // A bili-forged compaction item is never for the upstream (it carries our + // sentinel blob) — strip it on every forwarding path, same as the normal + // /responses pipeline does. + const cleaned = Array.isArray(parsed.input) ? stripBiliCompactionItems(parsed.input) : parsed.input; + const stripped = Array.isArray(parsed.input) && cleaned.length !== parsed.input.length; + const forgeBody: ResponsesRequestBody = { ...parsed, input: cleaned }; + const base: Prepared = { + body: stripped ? Buffer.from(JSON.stringify(forgeBody)) : body, session, processedMessages: [], originalMessages: [], @@ -1588,6 +1714,43 @@ function prepareResponsesCompact(body: Buffer, parsed: ResponsesRequestBody, ses compressInjected: false, resetAfterSuccess: true, }; + if (codexCompactMode() !== "intercept" || !isCodexClient(req.headers) || !Array.isArray(parsed.input)) { + return base; + } + // The state commit below is all-or-nothing: every non-forge path restores + // the pre-turn state so a passthrough compact is not raced against a + // half-applied fold (the upstream's own compaction boundary is handled by + // markNativeCompactionBoundary + rebase instead). + const prevState = session.state; + // E2 endpoint form: /responses/compact. When the gate passes, run the same + // fold pipeline as a normal turn and forge the compacted history as + // {"output": [...]} — a deterministic handoff to the ACP state instead of a + // foreign compaction blob. + let transformOk = false; + try { + const projection = responsesToCore(forgeBody); + const turn = core.processTurn({ messages: projection.msgs, state: session.state, config, tokenCount: session.stats.lastInputTokens, renderTags: process.env.ACP_RENDER_NONE ? "none" : "text-only" }); + session.state = turn.state; + transformOk = true; + if (!codexCompactGate(session, config.modelContextLimit, transformOk)) { + session.state = prevState; + return base; + } + const processed = stripKernelSummaries(turn.messages, turn.state); + const output = patchResponsesInput(projection, processed); + if (typeof output === "string") { + session.state = prevState; + return base; + } + snapshotMessages(session, projection.msgs); + markDirty(session); + log("info", `[${session.id}] codex compact intercepted (endpoint); forged history with ${output.length} item(s), upstream not contacted`); + return { ...base, codexForge: { kind: "endpoint", body: JSON.stringify({ output }), contentType: "application/json" } }; + } catch (err) { + session.state = prevState; + log("warn", `[${session.id}] codex compact forge failed (${String(err)}); passing through to upstream`); + return base; + } } export function isChatGptCodexUpstream(upstream: string | undefined): boolean { @@ -1867,6 +2030,14 @@ async function forward( instanceId: string, affinity?: string, ): Promise { + // E2: a codex native-compaction request intercepted in prepare() carries a + // forged success response — serve it without contacting upstream. + if (prepared?.codexForge) { + log("info", `[${prepared.session.id}] codex compact served locally (${prepared.codexForge.kind}); upstream not contacted`); + res.writeHead(200, { "content-type": prepared.codexForge.contentType }); + res.end(prepared.codexForge.body); + return; + } // #300: stamp the chain marker ONLY when this instance actually processed // the request (prepared !== null). A passthrough forward (prepared === null) // must NOT claim processing — otherwise a downstream processing bili would @@ -2127,8 +2298,7 @@ async function forward( res.end(); clearUpstreamTimer(); if (prepared?.resetAfterSuccess) { - markNativeCompactionBoundary(prepared.session); - log("info", `[${prepared.session.id}] native Responses compact completed; rebase scheduled for next Responses turn`); + log("warn", `[${prepared.session.id}] native compact response had no body; rebase NOT scheduled`); } return; } @@ -2160,11 +2330,21 @@ async function forward( prepared.compressInjected && prepared.processedMessages.length > 0; if (!useRewriter || prepared === null) { - await pipeThrough(upstream.body, res); - clearUpstreamTimer(); - if (prepared?.resetAfterSuccess) { - markNativeCompactionBoundary(prepared.session); - log("info", `[${prepared.session.id}] native Responses compact completed; rebase scheduled for next Responses turn`); + if (prepared && prepared.resetAfterSuccess) { + const [toClient, toObserve] = upstream.body.tee(); + const observed = observeResponsesTerminalState(toObserve, prepared.stream); + await pipeThrough(toClient, res); + clearUpstreamTimer(); + const terminal = await observed; + if (terminal === "completed") { + markNativeCompactionBoundary(prepared.session); + log("info", `[${prepared.session.id}] native Responses compact completed; rebase scheduled for next Responses turn`); + } else { + log("warn", `[${prepared.session.id}] native compact response terminal=${terminal}; rebase NOT scheduled`); + } + } else { + await pipeThrough(upstream.body, res); + clearUpstreamTimer(); } return; } diff --git a/src/session-id.ts b/src/session-id.ts index c9efad0aa1..be0bd78f74 100644 --- a/src/session-id.ts +++ b/src/session-id.ts @@ -89,3 +89,72 @@ export function preferPromptCacheKeyIdentity( if (pck.length === 0) return identity; return { ...identity, value: pck, source: "prompt-cache-key", clientProvided: true }; } + +/** + * Codex turn-metadata partitioning (#316 / PR-A). + * + * Codex (>=0.147) sends `x-codex-turn-metadata` (JSON) on every Responses + * request carrying `thread_source` ("user" for the root thread, "subagent" + * for spawned agent threads) and `thread_id`. Subagent threads REUSE the + * root's `session-id` header, so the legacy identity chain maps every thread + * of one task onto a single session — subagents inherit the root's + * compression state, which breaks #150's isolation (a guardian subagent must + * read the user's original authorization verbatim, never a compressed + * summary). + * + * When the metadata is present AND cross-checked (metadata.thread_id === + * `thread-id` header), partition by thread_source: + * - "user" → undefined (validated, but the id resolves through the + * legacy precedence chain — binding the session-id header + * directly would leapfrog stronger headers such as + * x-bili-plugin-conversation and flip the identity when a + * wrapper adds/drops the metadata mid-session; the legacy + * chain reads the same session-id header for plain codex + * traffic, so the root value is unchanged) + * - anything else → the `thread-id` header (fresh independent state per + * thread; self-contained replay is lossless). codex's + * ThreadSource serializes more than user/subagent — + * "guardian_review" (review sessions), "memory_consolidation", + * and arbitrary Feature strings such as "guardian_classifier" + * (guardian-v2 async scorer) — all of which are internal + * threads that need #150 isolation just as much, so the + * discrimination is inverted: only "user" joins the root + * session, every other source gets its own thread state. + * A non-string/empty thread_source, unparseable JSON, or missing/mismatched + * thread_id → undefined: the caller falls through to the legacy chain + * unchanged. + */ +export type CodexTurnIdentity = { + value: string; + threadSource: string; +}; + +export function codexTurnIdentity(headers: Record): CodexTurnIdentity | undefined { + const raw = headers["x-codex-turn-metadata"]; + if (typeof raw !== "string" || raw.trim().length === 0) return undefined; + let parsed: unknown; + try { + parsed = JSON.parse(raw); + } catch { + return undefined; + } + if (typeof parsed !== "object" || parsed === null || Array.isArray(parsed)) return undefined; + const record = parsed as Record; + const threadSource = record["thread_source"]; + if (typeof threadSource !== "string" || threadSource.trim().length === 0) return undefined; + const metaThreadId = record["thread_id"]; + if (typeof metaThreadId !== "string" || metaThreadId.trim().length === 0) return undefined; + const threadHeader = headers["thread-id"]; + if (typeof threadHeader !== "string" || threadHeader.trim() !== metaThreadId.trim()) return undefined; + if (threadSource.trim() === "user") { + // A root turn's headers are validated (the pair cross-checks), but the + // session id still resolves through the legacy precedence chain: + // binding directly to the session-id header here would leapfrog + // stronger headers (e.g. x-bili-plugin-conversation) and flip the + // identity when a wrapper adds or drops the metadata mid-session. The + // legacy chain reads the same session-id header for plain codex + // traffic, so the root value is unchanged. + return undefined; + } + return { value: threadHeader.trim(), threadSource }; +} diff --git a/src/stream-terminal.ts b/src/stream-terminal.ts new file mode 100644 index 0000000000..bbf187af6f --- /dev/null +++ b/src/stream-terminal.ts @@ -0,0 +1,109 @@ +// #321 PR-C: terminal-state observer for native-compaction responses. The +// rebase marker may only fire on "completed": a failed/incomplete/truncated +// stream means the client kept its history, and rebasing on a compaction that +// never happened discards valid blocks (#249). "unknown" is the safe default +// (no rebase; the next request self-heals via kernel message-id deactivation). + +export type TerminalState = "completed" | "failed" | "unknown"; + +const SUCCEEDED = "response.completed"; +const FAILED_EVENTS = new Set(["response.failed", "response.incomplete"]); +const MAX_JSON_BYTES = 16 << 20; + +export async function observeResponsesTerminalState(stream: ReadableStream, isStream: boolean): Promise { + return isStream ? observeSseTerminalState(stream) : observeJsonTerminalState(stream); +} + +function terminalFromSseLine(line: string): TerminalState | null { + if (line.startsWith("event:")) { + const name = line.slice(6).trim(); + if (name === SUCCEEDED) return "completed"; + if (FAILED_EVENTS.has(name)) return "failed"; + return null; + } + if (line.startsWith("data:")) { + const data = line.slice(5).trim(); + if (!data.startsWith("{")) return null; + try { + const type = (JSON.parse(data) as { type?: unknown }).type; + if (type === SUCCEEDED) return "completed"; + if (type === "response.failed" || type === "response.incomplete") return "failed"; + } catch { + return null; + } + } + return null; +} + +async function observeSseTerminalState(stream: ReadableStream): Promise { + const reader = stream.getReader(); + const decoder = new TextDecoder(); + let buf = ""; + let state: TerminalState = "unknown"; + try { + for (;;) { + const { done, value } = await reader.read(); + if (done) break; + buf += decoder.decode(value, { stream: true }); + let nl: number; + while ((nl = buf.indexOf("\n")) >= 0) { + const line = buf.slice(0, nl).replace(/\r$/, ""); + buf = buf.slice(nl + 1); + const next = terminalFromSseLine(line); + if (next !== null) { + state = next; + break; + } + } + if (state !== "unknown") { + // Release this tee branch; the client branch keeps draining the source. + await reader.cancel().catch(() => {}); + break; + } + } + } catch { + return "unknown"; + } finally { + reader.releaseLock(); + } + return state; +} + +async function observeJsonTerminalState(stream: ReadableStream): Promise { + const reader = stream.getReader(); + const chunks: Buffer[] = []; + let size = 0; + let overflow = false; + try { + for (;;) { + const { done, value } = await reader.read(); + if (done) break; + if (!overflow) { + size += value.length; + if (size <= MAX_JSON_BYTES) { + chunks.push(Buffer.from(value)); + } else { + overflow = true; + } + } + } + } catch { + return "unknown"; + } finally { + reader.releaseLock(); + } + if (overflow) return "unknown"; + const text = Buffer.concat(chunks).toString("utf8").trim(); + if (!text) return "unknown"; + let body: unknown; + try { + body = JSON.parse(text); + } catch { + return "unknown"; + } + if (typeof body !== "object" || body === null) return "unknown"; + const rec = body as Record; + if (rec.error !== undefined) return "failed"; + if (Array.isArray(rec.output)) return "completed"; + return "unknown"; +} diff --git a/tests/codex-compact-e2e.test.ts b/tests/codex-compact-e2e.test.ts new file mode 100644 index 0000000000..d97aec0b9f --- /dev/null +++ b/tests/codex-compact-e2e.test.ts @@ -0,0 +1,290 @@ +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 } 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 { listSessions, _resetSessionsForTest } from "../src/session.ts"; + +// Issue #321 PR-E2: conditional interception + forgery of codex's native +// compaction requests. When BILI_CODEX_COMPACT=intercept, the client is codex, +// and the ACP state is healthy (transform ok + steady-state < 90% + an active +// block to hand off), bili forges a success response and never contacts +// upstream — a deterministic handoff to the ACP state. Otherwise (kill-switch +// off, ACP not keeping up, or nothing compressed yet) the request passes +// through to upstream and native compaction backstops. + +const CODEX_UA = "codex_cli_rs/0.1.0 (linux x86_64)"; +const SESSION = "trig-sess"; + +function sse(type: string, data: unknown): string { + return `event: ${type}\ndata: ${JSON.stringify(data)}\n\n`; +} +function completed(inputTokens: number): string { + return sse("response.completed", { + response: { id: "resp_done", status: "completed", output: [], usage: { input_tokens: inputTokens, output_tokens: 5, total_tokens: inputTokens + 5 } }, + }); +} +function fcEvents(callId: string, args: string): string { + return [ + sse("response.output_item.added", { item: { type: "function_call", id: `fc_${callId}`, call_id: callId, name: "compress" }, output_index: 0 }), + sse("response.function_call_arguments.delta", { item_id: `fc_${callId}`, delta: args }), + sse("response.output_item.done", { item: { type: "function_call", id: `fc_${callId}`, call_id: callId, name: "compress", arguments: args }, output_index: 0 }), + ].join(""); +} +// 7 messages × ~4400 chars (~1100 tokens each, ~7700 total). Below the 10k +// window so preflight never fires; above the recent-tail protection so +// m00001/m00002 are compressible by the model. +function conversation() { + const input: { type: string; role: string; content: string }[] = []; + for (let i = 0; i < 7; i++) { + input.push({ type: "message", role: i % 2 === 0 ? "user" : "assistant", content: `Message ${i} of the working session. ` + `WORK_${i}_content_`.repeat(290) }); + } + return input; +} + +type Harness = { + proxy: http.Server; + upstream: http.Server; + bodies: string[]; + url: string; + compactUrl: string; +}; + +async function withHarness(opts: { mode?: string; firstTurnTokens: number }, fn: (h: Harness) => Promise): Promise { + const bodies: string[] = []; + const upstream = http.createServer((req, res) => { + const chunks: Buffer[] = []; + req.on("data", (c: Buffer) => chunks.push(c)); + req.on("end", () => { + const raw = Buffer.concat(chunks).toString("utf8"); + bodies.push(raw); + if ((req.url ?? "").includes("/responses/compact")) { + res.writeHead(200, { "content-type": "application/json" }); + res.end(JSON.stringify({ output: [] })); + return; + } + res.writeHead(200, { "content-type": "text/event-stream", "cache-control": "no-cache" }); + if (bodies.length === 1) { + // First turn: the model compresses the oldest pair → active block. + const compressArgs = JSON.stringify({ + content: [{ startId: "m00001", endId: "m00002", topic: "setup", summary: "MAIN-SUMMARY-SETUP-CONTEXT-FOLDED-BY-COMPRESSION-LONG-ENOUGH-FOR-KERNEL-MIN-LENGTH-CHECK" }], + }); + res.write(fcEvents("call_p", compressArgs)); + } + res.write(completed(opts.firstTurnTokens)); + res.end(); + }); + }); + upstream.listen(0, "127.0.0.1"); + await once(upstream, "listening"); + const upstreamPort = upstream.address().port; + + if (opts.mode === undefined) delete process.env.BILI_CODEX_COMPACT; + else process.env.BILI_CODEX_COMPACT = opts.mode; + + _setStoreForTest(new SessionStore({ enabled: false })); + _resetSessionsForTest(); + 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: { "gpt-resp": { context: 10_000 } } } }, + modelContextLimit: 10_000, + kernelConfig: defaultConfig(10_000), + 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 proxyPort = proxy.address().port; + const base = `http://127.0.0.1:${proxyPort}/bili/http://127.0.0.1:${upstreamPort}/v1`; + const h: Harness = { proxy, upstream, bodies, url: `${base}/responses`, compactUrl: `${base}/responses/compact` }; + try { + await fn(h); + } finally { + proxy.close(); + await once(proxy, "close"); + upstream.close(); + await once(upstream, "close"); + delete process.env.BILI_CODEX_COMPACT; + } +} + +// Turn 1: a normal turn where the model compresses the oldest pair, leaving an +// active block. Returns the upstream-request count after setup. +async function setupCompressedSession(h: Harness): Promise { + const r1 = await fetch(h.url, { + method: "POST", + headers: { "content-type": "application/json" }, + body: JSON.stringify({ model: "gpt-resp", stream: true, session_id: SESSION, instructions: "You are the test coding agent.", input: conversation() }), + }); + assert.equal(r1.status, 200); + await r1.text(); + const s = listSessions().find((x) => x.meta.label === SESSION); + assert.ok(s, "session exists"); + assert.ok((s!.state.blocks ?? []).some((b) => b.active), "setup created an active block"); + return h.bodies.length; +} + +test("e2e E2 (trigger form): intercept + healthy ACP → forged 2-frame SSE, upstream untouched", async () => { + await withHarness({ mode: "intercept", firstTurnTokens: 1000 }, async (h) => { + const afterSetup = await setupCompressedSession(h); + const s = listSessions().find((x) => x.meta.label === SESSION)!; + assert.ok(s.stats.lastInputTokens < 9000, "low steady-state usage → gate passes"); + + const r2 = await fetch(h.url, { + method: "POST", + headers: { "content-type": "application/json", "user-agent": CODEX_UA }, + body: JSON.stringify({ model: "gpt-resp", stream: true, session_id: SESSION, instructions: "You are the test coding agent.", input: [...conversation(), { type: "compaction_trigger" }] }), + }); + assert.equal(r2.status, 200, "intercepted compact returns 200"); + assert.equal(r2.headers.get("content-type"), "text/event-stream", "forged SSE"); + const frames = (await r2.text()).split("\n\n").filter((f) => f.startsWith("data: ")); + assert.equal(frames.length, 2, "exactly two data frames"); + const e1 = JSON.parse(frames[0]!.slice("data: ".length)) as { type: string; item: { type: string; id: string; encrypted_content: string } }; + assert.equal(e1.type, "response.output_item.done"); + assert.equal(e1.item.type, "compaction"); + assert.ok(e1.item.id.startsWith("fc_bili_"), "bili compaction id prefix"); + assert.ok(e1.item.encrypted_content.startsWith("bili:acp:"), "sentinel in blob"); + const e2 = JSON.parse(frames[1]!.slice("data: ".length)) as { type: string; response: { id: string; usage: { total_tokens: number } } }; + assert.equal(e2.type, "response.completed"); + assert.ok(e2.response.id.startsWith("resp_bili_"), "forged response id"); + + assert.equal(h.bodies.length, afterSetup, "upstream NOT contacted for the intercepted compact"); + }); +}); + +test("e2e E2 (trigger form): kill-switch off (BILI_CODEX_COMPACT=pass) → forwarded to upstream", async () => { + await withHarness({ mode: "pass", firstTurnTokens: 1000 }, async (h) => { + const afterSetup = await setupCompressedSession(h); + const r2 = await fetch(h.url, { + method: "POST", + headers: { "content-type": "application/json", "user-agent": CODEX_UA }, + body: JSON.stringify({ model: "gpt-resp", stream: true, session_id: SESSION, instructions: "You are the test coding agent.", input: [...conversation(), { type: "compaction_trigger" }] }), + }); + assert.equal(r2.status, 200); + await r2.text(); + assert.equal(h.bodies.length, afterSetup + 1, "compact request forwarded to upstream"); + }); +}); + +test("e2e E2 (trigger form): intercept + ACP not keeping up (≥90%) → forwarded (native backstop)", async () => { + await withHarness({ mode: "intercept", firstTurnTokens: 15000 }, async (h) => { + const afterSetup = await setupCompressedSession(h); + const s = listSessions().find((x) => x.meta.label === SESSION)!; + assert.ok(s.stats.lastInputTokens >= 9000, "high steady-state usage → gate fails"); + const r2 = await fetch(h.url, { + method: "POST", + headers: { "content-type": "application/json", "user-agent": CODEX_UA }, + body: JSON.stringify({ model: "gpt-resp", stream: true, session_id: SESSION, instructions: "You are the test coding agent.", input: [...conversation(), { type: "compaction_trigger" }] }), + }); + assert.equal(r2.status, 200); + await r2.text(); + assert.equal(h.bodies.length, afterSetup + 1, "unhealthy ACP → compact forwarded to upstream"); + }); +}); + +test("e2e E2 (trigger form): intercept + nothing compressed yet → forwarded (no summary to hand off)", async () => { + await withHarness({ mode: "intercept", firstTurnTokens: 1000 }, async (h) => { + // Fresh session, no setup compression — the compact request is the first. + const r = await fetch(h.url, { + method: "POST", + headers: { "content-type": "application/json", "user-agent": CODEX_UA }, + body: JSON.stringify({ model: "gpt-resp", stream: true, session_id: "fresh-sess", instructions: "You are the test coding agent.", input: [...conversation(), { type: "compaction_trigger" }] }), + }); + assert.equal(r.status, 200); + const text = await r.text(); + assert.ok(!text.includes("fc_bili_"), "no active block → compact NOT forged (forwarded to upstream)"); + assert.ok(!text.includes("resp_bili_"), "no forged response id"); + assert.ok(h.bodies.length >= 1, "compact request reached upstream"); + }); +}); + +test("e2e E2 (trigger form): post-forge turn — echo replaced by a history-borne handoff; dev re-injection only when the echo is absent", async () => { + await withHarness({ mode: "intercept", firstTurnTokens: 1000 }, async (h) => { + const afterSetup = await setupCompressedSession(h); + + // Turn 2: codex native auto-compact trigger → intercepted + forged. + const r2 = await fetch(h.url, { + method: "POST", + headers: { "content-type": "application/json", "user-agent": CODEX_UA }, + body: JSON.stringify({ model: "gpt-resp", stream: true, session_id: SESSION, instructions: "You are the test coding agent.", input: [...conversation(), { type: "compaction_trigger" }] }), + }); + assert.equal(r2.status, 200); + const frames = (await r2.text()).split("\n\n").filter((f) => f.startsWith("data: ")); + const e1 = JSON.parse(frames[0]!.slice("data: ".length)) as { item: { type: string; id: string; encrypted_content: string } }; + assert.equal(e1.item.type, "compaction", "forge returned the compaction item"); + + // Turn 3: codex replays [forged compaction item, retained tail, new + // turn]. The echo is REPLACED by a summary-carrying user message — a + // history-borne handoff the kernel can fold again — and the developer + // re-injection is suppressed for this turn to avoid duplication. + const r3 = await fetch(h.url, { + method: "POST", + headers: { "content-type": "application/json", "user-agent": CODEX_UA }, + body: JSON.stringify({ model: "gpt-resp", stream: true, session_id: SESSION, instructions: "You are the test coding agent.", input: [e1.item, ...conversation().slice(-2), { type: "message", role: "user", content: "continue the work" }] }), + }); + assert.equal(r3.status, 200); + await r3.text(); + assert.equal(h.bodies.length, afterSetup + 1, "post-forge turn forwarded to upstream exactly once"); + + const s = listSessions().find((x) => x.meta.label === SESSION)!; + const captured = s.metadata.codexForgedSummaries as string[] | undefined; + assert.ok(Array.isArray(captured) && captured.length > 0, "forge captured the active block summaries"); + assert.ok(captured.some((t) => t.includes("MAIN-SUMMARY-SETUP")), "captured summary is the setup block's"); + + const fwd = h.bodies[h.bodies.length - 1]; + assert.ok(!fwd.includes("fc_bili_"), "echoed bili compaction item replaced before forwarding"); + const fwdBody = JSON.parse(fwd) as { input: Array<{ type: string; role?: string; content?: unknown }> }; + const handoff = fwdBody.input.find((i) => JSON.stringify(i).includes("[bili] context summary after compaction")); + assert.ok(handoff, "summary handoff user message present in forwarded input"); + assert.ok(JSON.stringify(handoff).includes("MAIN-SUMMARY-SETUP"), "pre-compaction summary carried by the handoff"); + const dev = fwdBody.input.find((i) => i.type === "message" && i.role === "developer"); + assert.ok(!dev || !JSON.stringify(dev).includes("MAIN-SUMMARY-SETUP"), "developer re-injection suppressed while the echo handoff carries the summary"); + + // Turn 4: the echo did NOT come back (codex dropped / restarted) — + // now the captured summaries must surface via the developer-message + // re-injection fallback instead. + const r4 = await fetch(h.url, { + method: "POST", + headers: { "content-type": "application/json", "user-agent": CODEX_UA }, + body: JSON.stringify({ model: "gpt-resp", stream: true, session_id: SESSION, instructions: "You are the test coding agent.", input: [...conversation().slice(-2), { type: "message", role: "user", content: "still here" }] }), + }); + assert.equal(r4.status, 200); + await r4.text(); + const fwd4 = h.bodies[h.bodies.length - 1]; + const fwd4Body = JSON.parse(fwd4) as { input: Array<{ type: string; role?: string; content?: unknown }> }; + const dev4 = fwd4Body.input.find((i) => i.type === "message" && i.role === "developer"); + assert.ok(dev4, "developer message present when the echo is absent"); + assert.ok(JSON.stringify(dev4).includes("MAIN-SUMMARY-SETUP"), "captured summary re-injected via the fallback path"); + }); +}); + +test("e2e E2 (endpoint form): intercept + healthy ACP → forged JSON {output}, upstream untouched", async () => { + await withHarness({ mode: "intercept", firstTurnTokens: 1000 }, async (h) => { + const afterSetup = await setupCompressedSession(h); + const r2 = await fetch(h.compactUrl, { + method: "POST", + headers: { "content-type": "application/json", "user-agent": CODEX_UA }, + body: JSON.stringify({ model: "gpt-resp", stream: false, session_id: SESSION, instructions: "You are the test coding agent.", input: conversation() }), + }); + assert.equal(r2.status, 200, "intercepted endpoint compact returns 200"); + assert.ok((r2.headers.get("content-type") ?? "").includes("application/json"), "forged JSON"); + const parsed = JSON.parse(await r2.text()) as { output: unknown[] }; + assert.ok(Array.isArray(parsed.output), "output is an array"); + assert.ok(parsed.output.length > 0, "output carries the compacted history"); + assert.equal(h.bodies.length, afterSetup, "upstream NOT contacted for the intercepted endpoint compact"); + }); +}); diff --git a/tests/codex-compact.test.ts b/tests/codex-compact.test.ts new file mode 100644 index 0000000000..c2724e81b0 --- /dev/null +++ b/tests/codex-compact.test.ts @@ -0,0 +1,204 @@ +import assert from "node:assert/strict"; +import test from "node:test"; + +process.env.NODE_ENV = "test"; + +import { createInitialState, type CompressionBlock } from "acp-kernel"; +import type { Session } from "../src/session.ts"; +import { + codexCompactMode, + isCodexClient, + hasCompactionTrigger, + isBiliCompactionItem, + stripBiliCompactionItems, + replaceBiliCompactionItems, + codexCompactGate, + buildTriggerForgeBody, + renderForgedSummary, + mergeForgedSummaries, + CODEX_COMPACT_ID_PREFIX, + CODEX_COMPACT_SENTINEL, +} from "../src/codex-compact.ts"; + +test("codexCompactMode: kill-switch two states + default + case/trim", () => { + const prev = process.env.BILI_CODEX_COMPACT; + try { + delete process.env.BILI_CODEX_COMPACT; + assert.equal(codexCompactMode(), "intercept", "default is intercept"); + process.env.BILI_CODEX_COMPACT = "pass"; + assert.equal(codexCompactMode(), "pass"); + process.env.BILI_CODEX_COMPACT = "intercept"; + assert.equal(codexCompactMode(), "intercept"); + process.env.BILI_CODEX_COMPACT = "INTERCEPT"; + assert.equal(codexCompactMode(), "intercept", "case-insensitive"); + process.env.BILI_CODEX_COMPACT = " pass "; + assert.equal(codexCompactMode(), "pass", "trimmed"); + process.env.BILI_CODEX_COMPACT = "banana"; + assert.equal(codexCompactMode(), "intercept", "unknown value stays on intercept"); + } finally { + if (prev === undefined) delete process.env.BILI_CODEX_COMPACT; + else process.env.BILI_CODEX_COMPACT = prev; + } +}); + +test("isCodexClient: UA prefix detection (Node lowercases header keys)", () => { + assert.equal(isCodexClient({ "user-agent": "codex_cli_rs/0.1.0 (linux x86_64)" }), true); + assert.equal(isCodexClient({ "user-agent": "codex_cli_rs/0.2.1" }), true); + assert.equal(isCodexClient({ "user-agent": "codex_exec/0.147.0 (linux x86_64)" }), true, "exec-mode originator (codex 0.147 real-device UA)"); + assert.equal(isCodexClient({ "user-agent": "openai-node/3.0" }), false); + assert.equal(isCodexClient({}), false, "no UA"); + assert.equal(isCodexClient({ "user-agent": ["codex_cli_rs/0.1.0", "other"] }), true, "array UA takes first"); + assert.equal(isCodexClient({ "user-agent": ["other", "codex_cli_rs/0.1.0"] }), false, "array UA first is not codex"); +}); + +test("hasCompactionTrigger: only a FINAL compaction_trigger counts", () => { + assert.equal(hasCompactionTrigger([{ type: "message" }, { type: "compaction_trigger" }]), true); + assert.equal(hasCompactionTrigger([{ type: "compaction_trigger" }]), true); + assert.equal(hasCompactionTrigger([{ type: "message" }]), false); + assert.equal(hasCompactionTrigger([{ type: "compaction_trigger" }, { type: "message" }]), false, "trigger must be final"); + assert.equal(hasCompactionTrigger([]), false); + assert.equal(hasCompactionTrigger("a string"), false); +}); + +test("isBiliCompactionItem: our-vs-real-blob distinction", () => { + assert.equal(isBiliCompactionItem({ type: "compaction", id: `${CODEX_COMPACT_ID_PREFIX}abc` }), true, "id prefix"); + assert.equal(isBiliCompactionItem({ type: "compaction", encrypted_content: `${CODEX_COMPACT_SENTINEL}summary` }), true, "sentinel"); + assert.equal(isBiliCompactionItem({ type: "compaction", id: "fc_real", encrypted_content: "opaque-blob" }), false, "real OpenAI blob untouched"); + assert.equal(isBiliCompactionItem({ type: "message", role: "user", content: "hi" }), false, "not a compaction item"); + assert.equal(isBiliCompactionItem(null), false); +}); + +test("stripBiliCompactionItems: removes ours, keeps real blobs + messages", () => { + const ours = { type: "compaction", id: `${CODEX_COMPACT_ID_PREFIX}x`, encrypted_content: `${CODEX_COMPACT_SENTINEL}s` }; + const real = { type: "compaction", id: "fc_real", encrypted_content: "opaque" }; + const msg = { type: "message", role: "user", content: "hi" }; + const out = stripBiliCompactionItems([ours, real, msg]); + assert.equal(out.length, 2); + assert.ok(out.includes(real), "real blob kept"); + assert.ok(out.includes(msg), "message kept"); + assert.ok(!out.includes(ours), "ours removed"); +}); + +function block(active: boolean): CompressionBlock { + return { + blockId: "b1", runId: "r1", tier: 1, summary: "S", + directMessageIds: [], effectiveMessageIds: [], directBlockIds: [], + compressedTokens: 100, createdAt: 0, survivedCount: 0, generation: "young", active, + }; +} + +function mockSession(lastInputTokens: number, blocks: CompressionBlock[]): Session { + const state = createInitialState(); + state.blocks = blocks; + return { + id: "test", + meta: {}, + stats: { requests: 0, tokensSaved: 0, inputTokens: 0, cachedTokens: 0, outputTokens: 0, cacheSamples: 0, lastInputTokens, compressCreditTokens: 0, contextTokens: 0 }, + metadata: {}, + state, + createdAt: 0, + lastSeen: 0, + blockContents: new Map(), + inFlight: 0, + persisted: false, + }; +} + +test("codexCompactGate: safety-valve matrix", () => { + const limit = 10_000; + assert.equal(codexCompactGate(mockSession(1_000, [block(true)]), limit, true), true, "healthy + active block + transform ok"); + assert.equal(codexCompactGate(mockSession(1_000, [block(true)]), limit, false), false, "transform failed → pass through"); + assert.equal(codexCompactGate(mockSession(1_000, [block(false)]), limit, true), false, "no active block → pass through"); + assert.equal(codexCompactGate(mockSession(1_000, []), limit, true), false, "no blocks → pass through"); + assert.equal(codexCompactGate(mockSession(9_000, [block(true)]), limit, true), false, "at 90% → ACP not keeping up → pass through"); + assert.equal(codexCompactGate(mockSession(9_500, [block(true)]), limit, true), false, "above 90% → pass through"); + assert.equal(codexCompactGate(mockSession(8_999, [block(true)]), limit, true), true, "just below 90% → forge"); + assert.equal(codexCompactGate(mockSession(1_000, [block(true)]), 0, true), false, "zero limit → pass through"); +}); + +test("replaceBiliCompactionItems: echo becomes a summary handoff message in place", () => { + const ours = { type: "compaction", id: `${CODEX_COMPACT_ID_PREFIX}x`, encrypted_content: `${CODEX_COMPACT_SENTINEL}summary text` }; + const real = { type: "compaction", id: "fc_real", encrypted_content: "opaque" }; + const msg = { type: "message", role: "user", content: [{ type: "input_text", text: "hi" }] }; + const { items, replaced, dropped } = replaceBiliCompactionItems([msg, ours, real]); + assert.equal(replaced, 1); + assert.equal(dropped, 0); + assert.equal(items.length, 3, "position-preserving replacement, real blob kept"); + const handoff = items[1] as { type: string; role: string; content: { type: string; text: string }[] }; + assert.equal(handoff.type, "message"); + assert.equal(handoff.role, "user"); + assert.equal(handoff.content[0]!.type, "input_text"); + assert.ok(handoff.content[0]!.text.includes("summary text"), "extracted summary rides in the message"); + assert.ok(items.includes(real) && items.includes(msg), "neighbors untouched"); +}); + +test("replaceBiliCompactionItems: legacy id-only marker items still drop; empty blob drops", () => { + const legacy = { type: "compaction", id: `${CODEX_COMPACT_ID_PREFIX}old`, encrypted_content: "not-ours" }; + const empty = { type: "compaction", id: `${CODEX_COMPACT_ID_PREFIX}e`, encrypted_content: CODEX_COMPACT_SENTINEL }; + const { items, replaced, dropped } = replaceBiliCompactionItems([legacy, empty]); + assert.equal(replaced, 0); + assert.equal(dropped, 2); + assert.equal(items.length, 0); +}); + +test("buildTriggerForgeBody: minimal legal 2-frame stream", () => { + const { body: sse, contentType } = buildTriggerForgeBody("SUMMARY-TEXT", { inputTokens: 1000, outputTokens: 0, totalTokens: 1000 }, true); + assert.equal(contentType, "text/event-stream"); + const frames = sse.split("\n\n").filter((f) => f.length > 0); + assert.equal(frames.length, 2, "exactly two data frames"); + const e1 = JSON.parse(frames[0]!.slice("data: ".length)) as { type: string; item: { type: string; id: string; encrypted_content: string } }; + assert.equal(e1.type, "response.output_item.done"); + assert.equal(e1.item.type, "compaction"); + assert.ok(e1.item.id.startsWith(CODEX_COMPACT_ID_PREFIX), "compaction id has bili prefix"); + assert.ok(e1.item.encrypted_content.startsWith(CODEX_COMPACT_SENTINEL), "blob carries the sentinel"); + assert.ok(e1.item.encrypted_content.includes("SUMMARY-TEXT"), "summary text in blob"); + const e2 = JSON.parse(frames[1]!.slice("data: ".length)) as { type: string; response: { id: string; usage: { input_tokens: number; output_tokens: number; total_tokens: number } } }; + assert.equal(e2.type, "response.completed"); + assert.ok(e2.response.id.startsWith("resp_bili_"), "completed response id"); + assert.equal(e2.response.usage.input_tokens, 1000); + assert.equal(e2.response.usage.output_tokens, 0); + assert.equal(e2.response.usage.total_tokens, 1000); +}); + +test("buildTriggerForgeBody: non-streaming JSON shape", () => { + const { body, contentType } = buildTriggerForgeBody("S", { inputTokens: 7, outputTokens: 0, totalTokens: 7 }, false); + assert.equal(contentType, "application/json"); + const parsed = JSON.parse(body) as { id: string; output: { type: string; id: string; encrypted_content: string }[]; usage: { input_tokens: number; total_tokens: number } }; + assert.ok(parsed.id.startsWith("resp_bili_")); + assert.equal(parsed.output.length, 1); + assert.equal(parsed.output[0]!.type, "compaction"); + assert.ok(parsed.output[0]!.id.startsWith(CODEX_COMPACT_ID_PREFIX)); + assert.ok(parsed.output[0]!.encrypted_content.startsWith(CODEX_COMPACT_SENTINEL)); + assert.equal(parsed.usage.input_tokens, 7); + assert.equal(parsed.usage.total_tokens, 7); +}); + +function blockWith(summary: string, active: boolean, topic?: string): CompressionBlock { + const b = block(active); + b.summary = summary; + if (topic !== undefined) b.topic = topic; + return b; +} + +test("renderForgedSummary: kernel render format (topic, no topic, empty body)", () => { + assert.equal(renderForgedSummary({ summary: "S", topic: "T" }), "[Compressed conversation section] — T\nS"); + assert.equal(renderForgedSummary({ summary: "S" }), "[Compressed conversation section]\nS"); + assert.equal(renderForgedSummary({ summary: " " }), "[Compressed conversation section]"); +}); + +test("mergeForgedSummaries: append active, skip inactive, dedup, accumulate across forges", () => { + const a = blockWith("SUMMARY-A", true, "Topic A"); + const b = blockWith("SUMMARY-B", true); + const c = blockWith("SUMMARY-C", false); + let merged = mergeForgedSummaries(undefined, [a, b, c]); + assert.equal(merged.length, 2, "inactive block skipped"); + assert.equal(merged[0], "[Compressed conversation section] — Topic A\nSUMMARY-A"); + assert.equal(merged[1], "[Compressed conversation section]\nSUMMARY-B"); + merged = mergeForgedSummaries(merged, [a, b]); + assert.equal(merged.length, 2, "exact-text dedup on re-capture"); + const d = blockWith("SUMMARY-D", true); + merged = mergeForgedSummaries(merged, [d]); + assert.equal(merged.length, 3, "second forge accumulates"); + assert.equal(merged[2], "[Compressed conversation section]\nSUMMARY-D"); + assert.deepEqual(mergeForgedSummaries(undefined, []), []); +}); diff --git a/tests/codex-models.test.ts b/tests/codex-models.test.ts new file mode 100644 index 0000000000..06c36dcdfe --- /dev/null +++ b/tests/codex-models.test.ts @@ -0,0 +1,84 @@ +import assert from "node:assert/strict"; +import test from "node:test"; + +process.env.NODE_ENV = "test"; + +import { + CODEX_FALLBACK_CONTEXT_WINDOW, + _resetCodexTableForTest, + _setCodexTableForTest, + codexAlignedWindow, + codexWindowForModel, + isCodexClient, +} from "../src/codex-models.ts"; + +// #321 PR-E1: codex carries its own window perception (bundled model table + +// 272K unknown-model fallback) and auto-compacts at 90% of it. bili must cap +// a codex client's effective window at that perception, or codex's native +// compaction fires first (the #292 misalignment). + +const CODEX_UA = "codex_cli_rs/0.53.0 (linux 6.8.0; x86_64) cli"; + +test("snapshot integrity: every entry has a slug and a resolvable window", () => { + assert.ok(codexWindowForModel("gpt-5.5") > 0); + assert.equal(CODEX_FALLBACK_CONTEXT_WINDOW, 272_000); +}); + +test("in-table exact slug: context_window wins over max_context_window", () => { + assert.equal(codexWindowForModel("gpt-5.5"), 272_000); + assert.equal(codexWindowForModel("gpt-5.4"), 272_000, "gpt-5.4 max is 1M but codex resolves context_window=272K"); + assert.equal(codexWindowForModel("gpt-daybreak-red-latest"), 372_000); +}); + +test("longest-prefix match: model starting with a table slug inherits it", () => { + assert.equal(codexWindowForModel("gpt-5.5-mini"), 272_000, "gpt-5.5-mini → gpt-5.5"); + assert.equal(codexWindowForModel("gpt-5.4-turbo"), 272_000, "gpt-5.4-turbo → gpt-5.4"); + assert.equal(codexWindowForModel("gpt-5.4-mini-x"), 272_000, "longest slug wins (gpt-5.4-mini over gpt-5.4)"); +}); + +test("namespaced suffix retry: provider-like namespace stripped once", () => { + assert.equal(codexWindowForModel("custom/gpt-5.5"), 272_000); + assert.equal(codexWindowForModel("openai/gpt-5.4"), 272_000); +}); + +test("not in table: 272K fallback (codex's model_info_from_slug), NOT unlimited", () => { + assert.equal(codexWindowForModel("qwen3.8-27b"), 272_000); + assert.equal(codexWindowForModel("my-custom-model"), 272_000); + assert.equal(codexWindowForModel("custom/gpt-5.3-codex"), 272_000, "suffix miss → fallback"); + assert.equal(codexWindowForModel("a/b/gpt-5.5"), 272_000, "double slash → no retry → fallback"); +}); + +test("isCodexClient: UA prefixes codex_cli_rs/ and codex_exec/", () => { + assert.equal(isCodexClient({ "user-agent": CODEX_UA }), true); + assert.equal(isCodexClient({ "user-agent": "codex_exec/0.147.0" }), true, "exec-mode UA"); + assert.equal(isCodexClient({ "user-agent": "node-fetch/3.1" }), false); + assert.equal(isCodexClient({}), false); + assert.equal(isCodexClient({ "user-agent": [CODEX_UA, "node-fetch/3.1"] }), true, "array headers (first entry)"); + assert.equal(isCodexClient({ "user-agent": ["node-fetch/3.1", CODEX_UA] }), false, "array headers use first entry"); + assert.equal(isCodexClient({ "user-agent": "Codex_CLI_RS/0.53.0" }), false, "case-sensitive"); +}); + +test("codexAlignedWindow: min() semantics per acceptance (in-table / not-in-table / user override)", () => { + const codex = { "user-agent": CODEX_UA }; + const other = { "user-agent": "node-fetch/3.1" }; + // in-table: bili 400K (built-in table) → clamped to codex's 272K + assert.deepEqual(codexAlignedWindow(400_000, "gpt-5.5", codex), { limit: 272_000, clamped: true }); + // not-in-table: bili 1M → clamped to the 272K fallback + assert.deepEqual(codexAlignedWindow(1_000_000, "qwen3.8-27b", codex), { limit: 272_000, clamped: true }); + // bili below perception: untouched (min keeps bili's) + assert.deepEqual(codexAlignedWindow(200_000, "gpt-5.5", codex), { limit: 200_000, clamped: false }); + // equal: untouched + assert.deepEqual(codexAlignedWindow(272_000, "gpt-5.5", codex), { limit: 272_000, clamped: false }); + // non-codex client: never touched + assert.deepEqual(codexAlignedWindow(400_000, "gpt-5.5", other), { limit: 400_000, clamped: false }); + // user override below perception (PR-D style -c model_context_window=100000): untouched + assert.deepEqual(codexAlignedWindow(100_000, "gpt-5.5", codex), { limit: 100_000, clamped: false }); +}); + +test("test hooks: table replacement + reset", () => { + _setCodexTableForTest([{ slug: "test-model", contextWindow: 12_345 }]); + assert.equal(codexWindowForModel("test-model"), 12_345); + assert.equal(codexWindowForModel("test-model-x"), 12_345); + _resetCodexTableForTest(); + assert.equal(codexWindowForModel("gpt-5.5"), 272_000); +}); diff --git a/tests/codex-turn-isolation.test.ts b/tests/codex-turn-isolation.test.ts new file mode 100644 index 0000000000..de1f81fff1 --- /dev/null +++ b/tests/codex-turn-isolation.test.ts @@ -0,0 +1,140 @@ +import test from "node:test"; +import assert from "node:assert/strict"; +import http from "node:http"; +import { once } from "node:events"; +import { defaultConfig } from "acp-kernel"; +import { startServer } from "../src/server.ts"; +import { SessionStore, _setStoreForTest } from "../src/persist.ts"; +import { _resetSessionsForTest, peekSession } from "../src/session.ts"; +import type { ProxyOptions } from "../src/config.ts"; +import { _setForTest as setRegistryForTest } from "../src/registry.ts"; + +const ROOT_SESSION = "01a048b8-c704-7c00-8000-000000000000"; +const SUB_THREAD = "01a048b8-c728-7c00-8000-000000000000"; + +function listen(server: http.Server): Promise { + if (server.listening) return Promise.resolve(); + return once(server, "listening").then(() => undefined); +} + +function close(server: http.Server): Promise { + return new Promise((resolve, reject) => server.close((error) => (error ? reject(error) : resolve()))); +} + +function sseBlock(type: string, data: Record): string { + return `event: ${type}\ndata: ${JSON.stringify({ type, ...data })}\n\n`; +} + +/** Minimal valid Responses-API SSE upstream: one assistant "ok" message. */ +function startMockResponsesUpstream(): http.Server { + const server = http.createServer((_req, res) => { + res.writeHead(200, { "content-type": "text/event-stream", "cache-control": "no-cache" }); + res.write(sseBlock("response.created", { response: { id: "resp_1", status: "in_progress" } })); + res.write( + sseBlock("response.output_item.added", { + output_index: 0, + item: { type: "message", id: "msg_1", role: "assistant", status: "in_progress", content: [] }, + }), + ); + res.write(sseBlock("response.output_text.delta", { item_id: "msg_1", output_index: 0, content_index: 0, delta: "ok" })); + res.write(sseBlock("response.output_text.done", { item_id: "msg_1", output_index: 0, text: "ok" })); + res.write( + sseBlock("response.output_item.done", { + output_index: 0, + item: { type: "message", id: "msg_1", role: "assistant", status: "completed", content: [{ type: "output_text", text: "ok" }] }, + }), + ); + res.write( + sseBlock("response.completed", { + response: { + id: "resp_1", + status: "completed", + output: [{ type: "message", id: "msg_1", role: "assistant", status: "completed", content: [{ type: "output_text", text: "ok" }] }], + usage: { input_tokens: 10, output_tokens: 2 }, + }, + }), + ); + res.end(); + }); + server.listen(0, "127.0.0.1"); + return server; +} + +const rootMeta = (over: Record = {}) => + JSON.stringify({ request_kind: "turn", thread_source: "user", thread_id: ROOT_SESSION, turn_id: "turn-1", window_id: "win-1", ...over }); +const subMeta = (over: Record = {}) => + JSON.stringify({ request_kind: "turn", thread_source: "subagent", thread_id: SUB_THREAD, turn_id: "turn-2", window_id: "win-1", ...over }); + +test("codex turn-metadata partition (#316 / PR-A): subagent and root threads get isolated sessions", async () => { + _setStoreForTest(new SessionStore({ enabled: false })); + setRegistryForTest({}); + _resetSessionsForTest(); + const upstream = startMockResponsesUpstream(); + await listen(upstream); + const upstreamPort = (upstream.address() as { port: number }).port; + const opts: ProxyOptions = { + port: 0, + host: "127.0.0.1", + upstream: "http://127.0.0.1", + routes: { [`http://127.0.0.1:${upstreamPort}`]: { models: { "gpt-test": { context: 1_000_000 } } } } as ProxyOptions["routes"], + modelContextLimit: 1_000_000, + kernelConfig: defaultConfig(1_000_000), + compress: { injectTool: false, injectNudge: false }, + promptCache: { routing: "auto" }, + sessionHeader: "x-acp-session", + log: false, + debug: false, + passthrough: false, + autoUpdate: false, + mitm: { enabled: false, domains: [] }, + }; + const proxy = await startServer(opts); + await listen(proxy); + const proxyPort = (proxy.address() as { port: number }).port; + const url = `http://127.0.0.1:${proxyPort}/bili/http://127.0.0.1:${upstreamPort}/v1/responses`; + const post = async (headers: Record, body: Record) => { + const res = await fetch(url, { + method: "POST", + headers: { "content-type": "application/json", ...headers }, + body: JSON.stringify(body), + duplex: "half", + } as RequestInit); + const text = await res.text(); + return { status: res.status, text }; + }; + try { + // 1. Root thread: session-id == thread-id == ROOT, thread_source user. + const rootRes = await post( + { "session-id": ROOT_SESSION, "thread-id": ROOT_SESSION, "x-codex-turn-metadata": rootMeta() }, + { model: "gpt-test", stream: true, instructions: "", input: [{ type: "message", role: "user", content: [{ type: "input_text", text: "root turn 1" }] }] }, + ); + assert.equal(rootRes.status, 200, `root request: ${rootRes.text}`); + assert.ok(peekSession(ROOT_SESSION), "root request must create the session keyed by the session-id header"); + + // 2. Subagent: REUSES the root session-id but carries its own thread-id. + // It must land in a SEPARATE session keyed by the thread-id, not the + // root session (this is the #150 isolation the bug broke). + const subRes = await post( + { "session-id": ROOT_SESSION, "thread-id": SUB_THREAD, "x-codex-turn-metadata": subMeta() }, + { model: "gpt-test", stream: true, instructions: "", input: [{ type: "message", role: "user", content: [{ type: "input_text", text: "subagent turn 1" }] }] }, + ); + assert.equal(subRes.status, 200, `subagent request: ${subRes.text}`); + const subSession = peekSession(SUB_THREAD); + assert.ok(subSession, "subagent request must create a session keyed by the thread-id header"); + assert.notEqual(subSession!.id, ROOT_SESSION, "subagent session must be distinct from the root session"); + assert.notEqual(subSession, peekSession(ROOT_SESSION), "subagent and root must be separate session objects (isolated compression state)"); + + // 3. Root identity is stable across turns: a later root turn (churned + // turn_id/window_id) must reuse the SAME root session. + const rootRes2 = await post( + { "session-id": ROOT_SESSION, "thread-id": ROOT_SESSION, "x-codex-turn-metadata": rootMeta({ turn_id: "turn-2", window_id: "win-9" }) }, + { model: "gpt-test", stream: true, instructions: "", input: [{ type: "message", role: "user", content: [{ type: "input_text", text: "root turn 2" }] }] }, + ); + assert.equal(rootRes2.status, 200, `root turn 2: ${rootRes2.text}`); + assert.equal(peekSession(ROOT_SESSION)!.stats.requests, 2, "two root turns must accumulate on the same root session"); + assert.equal(peekSession(SUB_THREAD)!.stats.requests, 1, "subagent session must not absorb root turns"); + } finally { + await close(proxy); + await close(upstream); + } +}); diff --git a/tests/codex-window-align.test.ts b/tests/codex-window-align.test.ts new file mode 100644 index 0000000000..6b2db02845 --- /dev/null +++ b/tests/codex-window-align.test.ts @@ -0,0 +1,160 @@ +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 } 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 { listSessions } from "../src/session.ts"; + +// #321 PR-E1: a codex client (UA `codex_cli_rs/…`) carries its own window +// perception and auto-compacts at 90% of it. The proxy caps the effective +// window at codex's perception (min(bili, codex)), so ACP compresses before +// codex's native compaction can fire (the #292 misalignment). The cap is +// per-request (the UA may appear/disappear between requests of one session) +// and observable via the plugin-reported effectiveContextLimit. + +const CODEX_UA = "codex_cli_rs/0.53.0 (linux 6.8.0; x86_64) cli"; + +function okJson(model: string): string { + return JSON.stringify({ id: "msg_s", type: "message", role: "assistant", model, content: [{ type: "text", text: "s" }], stop_reason: "end_turn", usage: { input_tokens: 500, output_tokens: 5 } }); +} + +interface Rig { + proxyPort: number; + upstreamPort: number; + proxy: http.Server; + upstream: http.Server; +} + +async function startRig(): Promise { + const upstream = http.createServer((req, res) => { + const chunks: Buffer[] = []; + req.on("data", (c: Buffer) => chunks.push(c)); + req.on("end", () => { + const raw = Buffer.concat(chunks).toString("utf8"); + let model = "gpt-5.5"; + try { model = (JSON.parse(raw) as { model?: string }).model ?? model; } catch { /* keep default */ } + res.writeHead(200, { "content-type": "application/json" }); + res.end(okJson(model)); + }); + }); + upstream.listen(0, "127.0.0.1"); + 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}`]: {} }, + modelContextLimit: 200_000, + kernelConfig: defaultConfig(200_000), + 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 proxyPort = proxy.address().port; + return { proxyPort, upstreamPort, proxy, upstream }; +} + +function url(rig: Rig): string { + return `http://127.0.0.1:${rig.proxyPort}/bili/http://127.0.0.1:${rig.upstreamPort}/v1/messages`; +} + +async function post(rig: Rig, session: string, model: string, codex: boolean): Promise { + const headers: Record = { + "content-type": "application/json", + "x-acp-session": session, + "x-bili-plugin": "test-agent", + }; + if (codex) headers["user-agent"] = CODEX_UA; + const r = await fetch(url(rig), { method: "POST", headers, body: JSON.stringify({ model, max_tokens: 1024, stream: false, messages: [{ role: "user", content: "hi" }] }) }); + await r.text(); + return r.status; +} + +function effectiveLimit(session: string): number | undefined { + return listSessions().find((s) => s.meta.label === session)?.metadata.effectiveContextLimit; +} + +async function closeRig(rig: Rig): Promise { + rig.proxy.close(); + await once(rig.proxy, "close"); + rig.upstream.close(); + await once(rig.upstream, "close"); +} + +test("e2e: codex UA + in-table model → window clamped to codex's perception", async () => { + const rig = await startRig(); + try { + assert.equal(await post(rig, "cw-in", "gpt-5.5", true), 200); + assert.equal(effectiveLimit("cw-in"), 272_000, "gpt-5.5: bili table 400K clamped to codex 272K"); + } finally { + await closeRig(rig); + } +}); + +test("e2e: non-codex client → no clamp", async () => { + const rig = await startRig(); + try { + assert.equal(await post(rig, "cw-plain", "gpt-5.5", false), 200); + assert.equal(effectiveLimit("cw-plain"), 400_000, "control: built-in table 400K untouched"); + } finally { + await closeRig(rig); + } +}); + +test("e2e: codex UA + prefix-matched model → clamped via longest-prefix", async () => { + const rig = await startRig(); + try { + assert.equal(await post(rig, "cw-prefix", "gpt-5.5-mini", true), 200); + assert.equal(effectiveLimit("cw-prefix"), 272_000, "gpt-5.5-mini → gpt-5.5 (272K)"); + } finally { + await closeRig(rig); + } +}); + +test("e2e: codex UA + not-in-table model → clamped to the 272K fallback", async () => { + const rig = await startRig(); + try { + assert.equal(await post(rig, "cw-fallback", "glm-5", true), 200); + assert.equal(effectiveLimit("cw-fallback"), 272_000, "glm-5: bili table 1M clamped to codex's unknown-model 272K"); + } finally { + await closeRig(rig); + } +}); + +test("e2e: codex UA + bili window below perception → untouched (min keeps bili's)", async () => { + const rig = await startRig(); + try { + assert.equal(await post(rig, "cw-below", "claude-sonnet-4-5", true), 200); + assert.equal(effectiveLimit("cw-below"), 200_000, "200K < 272K perception → no clamp"); + } finally { + await closeRig(rig); + } +}); + +test("e2e: per-request resolution — UA disappears, window reverts", async () => { + const rig = await startRig(); + try { + assert.equal(await post(rig, "cw-perreq", "gpt-5.5", true), 200); + assert.equal(effectiveLimit("cw-perreq"), 272_000, "with codex UA → clamped"); + assert.equal(await post(rig, "cw-perreq", "gpt-5.5", false), 200); + assert.equal(effectiveLimit("cw-perreq"), 400_000, "same session, UA gone → 400K again"); + } finally { + await closeRig(rig); + } +}); diff --git a/tests/compaction-trigger-finality.test.ts b/tests/compaction-trigger-finality.test.ts index 7b9c9f0a9f..b81441eafa 100644 --- a/tests/compaction-trigger-finality.test.ts +++ b/tests/compaction-trigger-finality.test.ts @@ -82,10 +82,10 @@ test("e2e #280r2 (Responses): trailing compaction_trigger stays final — nudge const proxyPort = proxy.address().port; const url = `http://127.0.0.1:${proxyPort}/bili/http://127.0.0.1:${upstreamPort}/v1/responses`; - const post = (input: unknown) => fetch(url, { + const post = (input: unknown, session = "trig-sess") => fetch(url, { method: "POST", headers: { "content-type": "application/json" }, - body: JSON.stringify({ model: "gpt-resp", stream: true, session_id: "trig-sess", instructions: "You are the test coding agent.", input }), + body: JSON.stringify({ model: "gpt-resp", stream: true, session_id: session, instructions: "You are the test coding agent.", input }), }); try { @@ -108,9 +108,15 @@ test("e2e #280r2 (Responses): trailing compaction_trigger stays final — nudge assert.equal(lastItem.type, "compaction_trigger", "compaction_trigger is the final input item"); assert.ok(!bodies[bodies.length - 1].includes("Context limit reached"), "no nudge appended after the trigger"); - // Control: same armed session, normal trailing user message — the - // nudge must still be injected. - const r3 = await post([...conversation(), { type: "message", role: "user", content: "What should we do next?" }]); + // Control: normal trailing user message — the nudge must still be + // injected. Runs on a SEPARATE session: since #321 PR-C a completed + // trigger response marks a native-compaction boundary, so the next + // turn on trig-sess rebases (usage reset) and the nudge would be + // legitimately unarmed there. The warmup turn arms the usage report. + const r3a = await post(conversation(), "trig-ctl"); + assert.equal(r3a.status, 200); + await r3a.text(); + const r3 = await post([...conversation(), { type: "message", role: "user", content: "What should we do next?" }], "trig-ctl"); assert.equal(r3.status, 200); await r3.text(); assert.ok(bodies[bodies.length - 1].includes("Context limit reached"), "nudge still injected on a normal request"); diff --git a/tests/e2e/e2e-codex.test.ts b/tests/e2e/e2e-codex.test.ts index 19a1a10509..d0f253a892 100644 --- a/tests/e2e/e2e-codex.test.ts +++ b/tests/e2e/e2e-codex.test.ts @@ -280,6 +280,16 @@ test("e2e codex: warmup / load / ACP compress / purity / (forge)", { skip: skipR ["-c", "model_auto_compact_token_limit=3000"]); assert.strictEqual(f2.code, 0, `post-forge exit=${f2.code}`); assert.ok(f2.last.includes("9600"), `post-forge recall wrong: ${f2.last}`); + // Pre-forge compressed recall: 档案#6's sentinel was folded into an ACP + // block in phase 3 (phase 4 proved it recallable from the summary) and is + // NOT in codex's retained tail after the forge — it must survive via the + // forged-summary handoff (developer-message re-injection), not via the + // opaque compaction item. + const f3 = await turnExpect(ctx, "只回答一个数字: 档案#6 的哨兵值是多少?", + (last) => digits(last).includes(SENTINELS[5]), + ["-c", "model_auto_compact_token_limit=999999"]); + assert.strictEqual(f3.code, 0, `pre-forge-compressed recall exit=${f3.code}`); + assert.ok(digits(f3.last).includes(SENTINELS[5]), `pre-forge compressed value lost after forge: ${f3.last}`); const sessionsDir = path.join(ctx.codexHome, "sessions"); let rolloutHasForgeItem = false; if (fs.existsSync(sessionsDir)) { diff --git a/tests/launcher-plugin-mode.test.ts b/tests/launcher-plugin-mode.test.ts index 2524e19d66..335109f594 100644 --- a/tests/launcher-plugin-mode.test.ts +++ b/tests/launcher-plugin-mode.test.ts @@ -11,7 +11,7 @@ import { startServer, type ProxyOptions } from "../src/server.ts"; import { SessionStore, _setStoreForTest } from "../src/persist.ts"; import { _setForTest as setRegistryForTest } from "../src/registry.ts"; import { _resetPluginStateForTest } from "../src/plugin.ts"; -import { buildClaudePluginEnv, buildCodexMcpArgs, buildMcpConfig, launcherDirectUrl, launcherInjectMcp } from "../src/launcher.ts"; +import { buildClaudePluginEnv, buildCodexMcpArgs, buildMcpConfig, isPrivateUpstreamHost, launcherDirectUrl, launcherInjectMcp } from "../src/launcher.ts"; // Launcher mode (#162): hosts that cannot attach per-request headers // (claude/codex spawned by `bili claude` / `bili codex`) bind into plugin mode @@ -236,7 +236,27 @@ test("launcher injection builders: direct-URL env, MCP config JSON, codex -c arg // hosts older than the verified builds (claude 2.1.227, codex 0.147.0). assert.equal(launcherInjectMcp({}, "claude"), true, "plugin injection on by default (claude)"); assert.equal(launcherInjectMcp({}, "codex"), true, "plugin injection on by default (codex)"); - assert.equal(launcherInjectMcp({ BILI_LAUNCHER_PLUGIN: "0" }, "claude"), false, "explicit opt-out honored (claude)"); + assert.equal(launcherInjectMcp({}, "codex", "http://127.0.0.1:8199/v1"), false, "loopback codex upstream falls back to wire tools"); + assert.equal(launcherInjectMcp({}, "codex", "http://localhost:8000/v1"), false, "localhost falls back"); + assert.equal(launcherInjectMcp({}, "codex", "http://192.168.1.5:8000/v1"), false, "RFC1918 codex upstream falls back"); + assert.equal(launcherInjectMcp({}, "codex", "http://10.0.0.3:11434/v1"), false, "10/8 falls back (ollama-style)"); + assert.equal(launcherInjectMcp({}, "codex", "http://[::1]:8080/v1"), false, "IPv6 loopback falls back"); + assert.equal(launcherInjectMcp({}, "codex", "http://[fd00::1]:8080/v1"), false, "IPv6 ULA falls back"); + assert.equal(launcherInjectMcp({}, "codex", "http://mybox.local:8000/v1"), false, "mDNS name falls back"); + assert.equal(launcherInjectMcp({ BILI_LAUNCHER_PLUGIN: "1" }, "codex", "http://127.0.0.1:8199/v1"), true, "explicit opt-in overrides the local-upstream fallback"); + assert.equal(launcherInjectMcp({ BILI_LAUNCHER_PLUGIN: "0" }, "codex", "http://127.0.0.1:8199/v1"), false, "explicit opt-out still honored"); + assert.equal(launcherInjectMcp({}, "codex", "https://api.openai.com/v1"), true, "public codex upstream keeps MCP tools"); + assert.equal(launcherInjectMcp({}, "codex", "https://gwv1701.comfly.org/v1"), true, "public relay keeps MCP tools"); + assert.equal(launcherInjectMcp({}, "codex", "not a url"), true, "unparseable upstream: keep MCP tools (conservative)"); + assert.equal(launcherInjectMcp({}, "claude", "http://127.0.0.1:8199"), true, "auto-fallback is codex-only (claude flattens MCP tools itself)"); + assert.equal(isPrivateUpstreamHost("http://127.0.0.1:8199/v1"), true); + assert.equal(isPrivateUpstreamHost("http://172.16.0.1/v1"), true); + assert.equal(isPrivateUpstreamHost("http://172.32.0.1/v1"), false, "172 outside 16-31 is public"); + assert.equal(isPrivateUpstreamHost("http://169.254.1.1/v1"), true, "link-local"); + assert.equal(isPrivateUpstreamHost("http://8.8.8.8/v1"), false); + assert.equal(isPrivateUpstreamHost("http://[::ffff:10.1.2.3]:9/v1"), true, "IPv4-mapped IPv6"); + assert.equal(isPrivateUpstreamHost("http://[2001:db8::1]:9/v1"), false, "global IPv6"); + assert.equal(isPrivateUpstreamHost(""), false, "empty string unparseable"); assert.equal(launcherInjectMcp({ BILI_LAUNCHER_PLUGIN: "0" }, "claude"), false, "explicit opt-out honored (claude)"); assert.equal(launcherInjectMcp({ BILI_LAUNCHER_PLUGIN: "0" }, "codex"), false, "explicit opt-out honored (codex)"); assert.equal(launcherInjectMcp({ BILI_LAUNCHER_PLUGIN: "1" }, "claude"), true, "explicit opt-in still works"); assert.equal(launcherInjectMcp({ BILI_LAUNCHER_PLUGIN: "1" }, "pi"), false, "pi always excluded (native extension #154)"); diff --git a/tests/launcher.test.ts b/tests/launcher.test.ts index 23fa7d5f32..170c1f2e02 100644 --- a/tests/launcher.test.ts +++ b/tests/launcher.test.ts @@ -48,12 +48,18 @@ import { findFreePort, ensureProxyRunning, stopProxy, + resolveLauncherWindow, + resolveCodexBudgetArgs, + resolveClaudeBudgetEnv, + codexUpstreamUrl, + readClaudeSettings, type SpawnChild, type SpawnFn, runLaunch, type ClientConfig, type HttpRewrite, } from "../src/launcher.ts"; +import { _setForTest as registrySetForTest, _resetForTest as registryResetForTest } from "../src/registry.ts"; test("isLaunchClient: pi/claude/codex/omp/opencode/pi-test true, others false", () => { assert.equal(isLaunchClient("pi"), true); @@ -1721,3 +1727,327 @@ test("runLaunch omp: launcher hands per-model windows to the spawned proxy", asy fs.rmSync(home, { recursive: true, force: true }); } }); + +// --- PR-D (#321): launcher budget alignment (codex -c / claude env) --- + +test("codexUpstreamUrl: provider base_url > openai_base_url > built-in default", () => { + assert.equal(codexUpstreamUrl(undefined), "https://api.openai.com/v1"); + assert.equal(codexUpstreamUrl({ providers: {} }), "https://api.openai.com/v1"); + assert.equal( + codexUpstreamUrl({ providers: {}, openaiBaseUrl: "https://relay.example.com/v1" }), + "https://relay.example.com/v1", + ); + assert.equal( + codexUpstreamUrl({ + modelProvider: "relay", + openaiBaseUrl: "https://openai.example.com/v1", + providers: { relay: { baseUrl: "https://relay.example.com/v1" } }, + }), + "https://relay.example.com/v1", + ); + assert.equal( + codexUpstreamUrl({ modelProvider: "missing", openaiBaseUrl: "https://openai.example.com/v1", providers: {} }), + "https://openai.example.com/v1", + ); +}); + +test("resolveLauncherWindow: config route > built-in table > registry > nothing", async () => { + const routes = { "https://api.openai.com/v1": { models: { "gpt-x": { context: 123456 } } } }; + registrySetForTest({}); + try { + assert.equal(await resolveLauncherWindow("gpt-x", routes, "https://api.openai.com/v1"), 123456); + assert.equal(await resolveLauncherWindow("gpt-5.5", {}, "https://api.openai.com/v1"), 400000); + registrySetForTest({ "openai/bili-fallback-model": { limit: { context: 333333 } } }); + assert.equal(await resolveLauncherWindow("bili-fallback-model", {}, "https://api.openai.com/v1"), 333333); + assert.equal(await resolveLauncherWindow("bili-nonexistent-model-xyz", {}, "https://api.openai.com/v1"), undefined); + assert.equal(await resolveLauncherWindow(undefined, routes, "https://api.openai.com/v1"), undefined); + } finally { + registryResetForTest(); + } +}); + +test("resolveCodexBudgetArgs: injects window + same-value limit from bili's chain", async () => { + registrySetForTest({}); + try { + assert.deepEqual( + await resolveCodexBudgetArgs({ model: "gpt-5.5", clientWindow: undefined, clientAutoCompactLimit: undefined, routes: {}, upstreamUrl: "https://api.openai.com/v1" }), + ["-c", "model_context_window=400000", "-c", "model_auto_compact_token_limit=400000"], + ); + const routes = { "https://relay.example.com/v1": { models: { "gpt-x": { context: 123456 } } } }; + assert.deepEqual( + await resolveCodexBudgetArgs({ model: "gpt-x", clientWindow: undefined, clientAutoCompactLimit: undefined, routes, upstreamUrl: "https://relay.example.com/v1" }), + ["-c", "model_context_window=123456", "-c", "model_auto_compact_token_limit=123456"], + ); + registrySetForTest({ "openai/bili-fallback-model": { limit: { context: 333333 } } }); + assert.deepEqual( + await resolveCodexBudgetArgs({ model: "bili-fallback-model", clientWindow: undefined, clientAutoCompactLimit: undefined, routes: {}, upstreamUrl: "https://api.openai.com/v1" }), + ["-c", "model_context_window=333333", "-c", "model_auto_compact_token_limit=333333"], + ); + } finally { + registryResetForTest(); + } +}); + +test("resolveCodexBudgetArgs: no injection when user self-aligned or unresolvable", async () => { + registrySetForTest({}); + try { + assert.deepEqual(await resolveCodexBudgetArgs({ model: "gpt-5.5", clientWindow: 1000000, clientAutoCompactLimit: undefined, routes: {}, upstreamUrl: "https://api.openai.com/v1" }), []); + assert.deepEqual(await resolveCodexBudgetArgs({ model: undefined, clientWindow: undefined, clientAutoCompactLimit: undefined, routes: {}, upstreamUrl: "https://api.openai.com/v1" }), []); + assert.deepEqual(await resolveCodexBudgetArgs({ model: "bili-nonexistent-model-xyz", clientWindow: undefined, clientAutoCompactLimit: undefined, routes: {}, upstreamUrl: "https://api.openai.com/v1" }), []); + } finally { + registryResetForTest(); + } +}); + +test("resolveCodexBudgetArgs: honors user-set model_auto_compact_token_limit", async () => { + assert.deepEqual( + await resolveCodexBudgetArgs({ model: "gpt-5.5", clientWindow: undefined, clientAutoCompactLimit: 111111, routes: {}, upstreamUrl: "https://api.openai.com/v1" }), + ["-c", "model_context_window=400000", "-c", "model_auto_compact_token_limit=111111"], + ); +}); + +test("resolveClaudeBudgetEnv: injects CLAUDE_CODE_AUTO_COMPACT_WINDOW from bili's chain", async () => { + registrySetForTest({}); + try { + assert.deepEqual( + await resolveClaudeBudgetEnv({ model: "claude-sonnet-4-5", userAutoCompactWindow: undefined, shellAutoCompactWindow: undefined, routes: {}, upstreamUrl: "https://api.anthropic.com" }), + { CLAUDE_CODE_AUTO_COMPACT_WINDOW: "200000" }, + ); + const routes = { "https://relay.example.com": { models: { "claude-x": { context: 123456 } } } }; + assert.deepEqual( + await resolveClaudeBudgetEnv({ model: "claude-x", userAutoCompactWindow: undefined, shellAutoCompactWindow: undefined, routes, upstreamUrl: "https://relay.example.com" }), + { CLAUDE_CODE_AUTO_COMPACT_WINDOW: "123456" }, + ); + registrySetForTest({ "anthropic/bili-fallback-model": { limit: { context: 333333 } } }); + assert.deepEqual( + await resolveClaudeBudgetEnv({ model: "bili-fallback-model", userAutoCompactWindow: undefined, shellAutoCompactWindow: undefined, routes: {}, upstreamUrl: "https://api.anthropic.com" }), + { CLAUDE_CODE_AUTO_COMPACT_WINDOW: "333333" }, + ); + } finally { + registryResetForTest(); + } +}); + +test("resolveClaudeBudgetEnv: no injection when user self-aligned or unresolvable", async () => { + registrySetForTest({}); + try { + assert.deepEqual(await resolveClaudeBudgetEnv({ model: "claude-sonnet-4-5", userAutoCompactWindow: 300000, shellAutoCompactWindow: undefined, routes: {}, upstreamUrl: "https://api.anthropic.com" }), {}); + assert.deepEqual(await resolveClaudeBudgetEnv({ model: "claude-sonnet-4-5", userAutoCompactWindow: undefined, shellAutoCompactWindow: "250000", routes: {}, upstreamUrl: "https://api.anthropic.com" }), {}); + assert.deepEqual(await resolveClaudeBudgetEnv({ model: undefined, userAutoCompactWindow: undefined, shellAutoCompactWindow: undefined, routes: {}, upstreamUrl: "https://api.anthropic.com" }), {}); + assert.deepEqual(await resolveClaudeBudgetEnv({ model: "bili-nonexistent-model-xyz", userAutoCompactWindow: undefined, shellAutoCompactWindow: undefined, routes: {}, upstreamUrl: "https://api.anthropic.com" }), {}); + } finally { + registryResetForTest(); + } +}); + +test("parseCodexToml: stores model / model_context_window / model_auto_compact_token_limit", () => { + const cfg = parseCodexToml(` +model = "gpt-5.5" +model_context_window = 1000000 +model_auto_compact_token_limit = 900000 + +[model_providers.relay] +base_url = "https://relay.example.com/v1" +`); + assert.equal(cfg.model, "gpt-5.5"); + assert.equal(cfg.contextWindow, 1000000); + assert.equal(cfg.autoCompactLimit, 900000); + assert.deepEqual(cfg.modelWindows, [{ id: "gpt-5.5", contextWindow: 1000000 }]); + const bare = parseCodexToml(`model = "gpt-5.5"\n`); + assert.equal(bare.model, "gpt-5.5"); + assert.equal(bare.contextWindow, undefined); + assert.equal(bare.autoCompactLimit, undefined); + assert.equal(bare.modelWindows, undefined); +}); + +test("readClaudeSettings: model from env block / top-level, autoCompactWindow from both forms", () => { + const home = fs.mkdtempSync(path.join(os.tmpdir(), "bili-claude-settings-")); + try { + const settingsDir = path.join(home, ".claude"); + fs.mkdirSync(settingsDir, { recursive: true }); + // top-level model + autoCompactWindow + fs.writeFileSync(path.join(settingsDir, "settings.json"), JSON.stringify({ model: "claude-sonnet-4-5", autoCompactWindow: 300000 })); + let cfg = readClaudeSettings(home, os.tmpdir(), {}); + assert.equal(cfg.model, "claude-sonnet-4-5"); + assert.equal(cfg.autoCompactWindow, 300000); + // env block beats same-file top-level + fs.writeFileSync( + path.join(settingsDir, "settings.json"), + JSON.stringify({ model: "claude-sonnet-4-5", autoCompactWindow: 300000, env: { ANTHROPIC_MODEL: "claude-opus-4-5", CLAUDE_CODE_AUTO_COMPACT_WINDOW: "250000" } }), + ); + cfg = readClaudeSettings(home, os.tmpdir(), {}); + assert.equal(cfg.model, "claude-opus-4-5"); + assert.equal(cfg.autoCompactWindow, 250000); + // nothing set → empty object + fs.writeFileSync(path.join(settingsDir, "settings.json"), JSON.stringify({})); + cfg = readClaudeSettings(home, os.tmpdir(), {}); + assert.deepEqual(cfg, {}); + } finally { + fs.rmSync(home, { recursive: true, force: true }); + } +}); + +test("runLaunch codex: budget args injected for MITM mode (built-in table window)", async () => { + const home = fs.mkdtempSync(path.join(os.tmpdir(), "bili-codex-budget-")); + const prevHome = process.env.HOME; + const prevUserProfile = process.env.USERPROFILE; + const prevClientBin = process.env.BILI_CLIENT_BIN; + const prevAnthropicModel = process.env.ANTHROPIC_MODEL; + const prevAutoCompact = process.env.CLAUDE_CODE_AUTO_COMPACT_WINDOW; + process.env.HOME = home; + if (prevUserProfile !== undefined) process.env.USERPROFILE = home; + delete process.env.ANTHROPIC_MODEL; + delete process.env.CLAUDE_CODE_AUTO_COMPACT_WINDOW; + const fakeCodex = path.join(home, "fake-codex"); + fs.writeFileSync(fakeCodex, ""); + process.env.BILI_CLIENT_BIN = fakeCodex; + const codexHome = path.join(home, ".codex"); + fs.mkdirSync(codexHome, { recursive: true }); + fs.writeFileSync(path.join(codexHome, "config.toml"), 'model = "gpt-5.5"\n'); + + const clientArgsSeen: string[][] = []; + const spawnImpl: SpawnFn = (cmd, args) => { + if (cmd === fakeCodex) { + clientArgsSeen.push([...args]); + const child = makeFakeChild(0); + const orig = child.on.bind(child); + (child as { on: SpawnChild["on"] }).on = (event, listener) => { + orig(event, listener); + if (event === "exit") setTimeout(() => listener(0, null), 0); + return child; + }; + return child; + } + return makeFakeChild(42422); + }; + const fetchImpl = async () => ({ ok: true }); + const prevExit = process.exit; + process.exit = (() => undefined) as typeof process.exit; + + try { + await runLaunch( + { client: "codex", clientArgs: [], overrides: {} }, + { fetchImpl, spawnImpl, sleep: () => Promise.resolve() }, + ); + assert.equal(clientArgsSeen.length, 1); + const args = clientArgsSeen[0]; + const i = args.indexOf("model_context_window=400000"); + assert.ok(i > -1, `expected model_context_window=400000 in ${JSON.stringify(args)}`); + assert.equal(args[i - 1], "-c"); + const j = args.indexOf("model_auto_compact_token_limit=400000"); + assert.ok(j > -1, `expected model_auto_compact_token_limit=400000 in ${JSON.stringify(args)}`); + assert.equal(args[j - 1], "-c"); + + // user self-aligned (model_context_window in config.toml) → no injection + fs.writeFileSync(path.join(codexHome, "config.toml"), 'model = "gpt-5.5"\nmodel_context_window = 1000000\n'); + clientArgsSeen.length = 0; + await runLaunch( + { client: "codex", clientArgs: [], overrides: {} }, + { fetchImpl, spawnImpl, sleep: () => Promise.resolve() }, + ); + assert.equal(clientArgsSeen.length, 1); + assert.ok(!clientArgsSeen[0].some((a) => a.startsWith("model_context_window=")), JSON.stringify(clientArgsSeen[0])); + + // no model in config.toml → no injection + fs.writeFileSync(path.join(codexHome, "config.toml"), ""); + clientArgsSeen.length = 0; + await runLaunch( + { client: "codex", clientArgs: [], overrides: {} }, + { fetchImpl, spawnImpl, sleep: () => Promise.resolve() }, + ); + assert.equal(clientArgsSeen.length, 1); + assert.ok(!clientArgsSeen[0].some((a) => a.startsWith("model_context_window=")), JSON.stringify(clientArgsSeen[0])); + } finally { + process.exit = prevExit; + process.env.HOME = prevHome; + if (prevUserProfile === undefined) delete process.env.USERPROFILE; + else process.env.USERPROFILE = prevUserProfile; + if (prevClientBin === undefined) delete process.env.BILI_CLIENT_BIN; + else process.env.BILI_CLIENT_BIN = prevClientBin; + if (prevAnthropicModel === undefined) delete process.env.ANTHROPIC_MODEL; + else process.env.ANTHROPIC_MODEL = prevAnthropicModel; + if (prevAutoCompact === undefined) delete process.env.CLAUDE_CODE_AUTO_COMPACT_WINDOW; + else process.env.CLAUDE_CODE_AUTO_COMPACT_WINDOW = prevAutoCompact; + fs.rmSync(home, { recursive: true, force: true }); + } +}); + +test("runLaunch claude: CLAUDE_CODE_AUTO_COMPACT_WINDOW injected (built-in table window)", async () => { + const home = fs.mkdtempSync(path.join(os.tmpdir(), "bili-claude-budget-")); + const prevHome = process.env.HOME; + const prevUserProfile = process.env.USERPROFILE; + const prevClientBin = process.env.BILI_CLIENT_BIN; + const prevAnthropicModel = process.env.ANTHROPIC_MODEL; + const prevAutoCompact = process.env.CLAUDE_CODE_AUTO_COMPACT_WINDOW; + process.env.HOME = home; + if (prevUserProfile !== undefined) process.env.USERPROFILE = home; + delete process.env.ANTHROPIC_MODEL; + delete process.env.CLAUDE_CODE_AUTO_COMPACT_WINDOW; + const fakeClaude = path.join(home, "fake-claude"); + fs.writeFileSync(fakeClaude, ""); + process.env.BILI_CLIENT_BIN = fakeClaude; + const claudeDir = path.join(home, ".claude"); + fs.mkdirSync(claudeDir, { recursive: true }); + fs.writeFileSync(path.join(claudeDir, "settings.json"), JSON.stringify({ model: "claude-sonnet-4-5" })); + + const clientEnvs: (NodeJS.ProcessEnv | undefined)[] = []; + const spawnImpl: SpawnFn = (cmd, args, opts) => { + if (cmd === fakeClaude) { + clientEnvs.push((opts as { env?: NodeJS.ProcessEnv } | undefined)?.env); + const child = makeFakeChild(0); + const orig = child.on.bind(child); + (child as { on: SpawnChild["on"] }).on = (event, listener) => { + orig(event, listener); + if (event === "exit") setTimeout(() => listener(0, null), 0); + return child; + }; + return child; + } + return makeFakeChild(42422); + }; + const fetchImpl = async () => ({ ok: true }); + const prevExit = process.exit; + process.exit = (() => undefined) as typeof process.exit; + + try { + await runLaunch( + { client: "claude", clientArgs: [], overrides: {} }, + { fetchImpl, spawnImpl, sleep: () => Promise.resolve() }, + ); + assert.equal(clientEnvs.length, 1); + assert.equal(clientEnvs[0]?.CLAUDE_CODE_AUTO_COMPACT_WINDOW, "200000"); + + // user self-aligned (settings autoCompactWindow) → no injection + fs.writeFileSync(path.join(claudeDir, "settings.json"), JSON.stringify({ model: "claude-sonnet-4-5", autoCompactWindow: 300000 })); + clientEnvs.length = 0; + await runLaunch( + { client: "claude", clientArgs: [], overrides: {} }, + { fetchImpl, spawnImpl, sleep: () => Promise.resolve() }, + ); + assert.equal(clientEnvs.length, 1); + assert.equal(clientEnvs[0]?.CLAUDE_CODE_AUTO_COMPACT_WINDOW, undefined); + + // shell-exported ANTHROPIC_MODEL wins over settings model + fs.writeFileSync(path.join(claudeDir, "settings.json"), JSON.stringify({ model: "claude-sonnet-4-5" })); + process.env.ANTHROPIC_MODEL = "claude-opus-4-5"; + clientEnvs.length = 0; + await runLaunch( + { client: "claude", clientArgs: [], overrides: {} }, + { fetchImpl, spawnImpl, sleep: () => Promise.resolve() }, + ); + assert.equal(clientEnvs.length, 1); + assert.equal(clientEnvs[0]?.CLAUDE_CODE_AUTO_COMPACT_WINDOW, "200000"); + } finally { + process.exit = prevExit; + process.env.HOME = prevHome; + if (prevUserProfile === undefined) delete process.env.USERPROFILE; + else process.env.USERPROFILE = prevUserProfile; + if (prevClientBin === undefined) delete process.env.BILI_CLIENT_BIN; + else process.env.BILI_CLIENT_BIN = prevClientBin; + if (prevAnthropicModel === undefined) delete process.env.ANTHROPIC_MODEL; + else process.env.ANTHROPIC_MODEL = prevAnthropicModel; + if (prevAutoCompact === undefined) delete process.env.CLAUDE_CODE_AUTO_COMPACT_WINDOW; + else process.env.CLAUDE_CODE_AUTO_COMPACT_WINDOW = prevAutoCompact; + fs.rmSync(home, { recursive: true, force: true }); + } +}); diff --git a/tests/native-compact-terminal.test.ts b/tests/native-compact-terminal.test.ts new file mode 100644 index 0000000000..7aa25b5e71 --- /dev/null +++ b/tests/native-compact-terminal.test.ts @@ -0,0 +1,193 @@ +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 } 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 { listSessions } from "../src/session.ts"; + +// #321 PR-C: the native-compaction rebase marker must fire ONLY when the +// upstream response semantically completed. failed/incomplete/truncated +// streams, 2xx error JSON, and empty bodies must NOT schedule a rebase. +// Also: a trailing compaction_trigger request must be forwarded WITHOUT the +// compress prompt/tools and with the trigger still final (trigger hardening). + +function sse(type: string, data: unknown): string { + return `event: ${type}\ndata: ${JSON.stringify(data)}\n\n`; +} + +function conversation() { + const input: { type: string; role: string; content: string }[] = []; + for (let i = 0; i < 4; i++) { + input.push({ type: "message", role: i % 2 === 0 ? "user" : "assistant", content: `Message ${i}: ` + `WORK_${i}_content_`.repeat(120) }); + } + return input; +} + +function pendingRebase(sessionId: string): boolean | undefined { + const s = listSessions().find((x) => x.meta.label === sessionId); + if (!s) return undefined; + const b = s.metadata.nativeCompactionBoundary as Record | undefined; + return b ? (b.pendingRebase as boolean) : undefined; +} + +test("e2e #321 PR-C: rebase only on completed; trigger request forwarded clean", async () => { + _setStoreForTest(new SessionStore({ enabled: false })); + setRegistryForTest({}); + const bodies: string[] = []; + const upstream = http.createServer((req, res) => { + const chunks: Buffer[] = []; + req.on("data", (c: Buffer) => chunks.push(c)); + req.on("end", () => { + const raw = Buffer.concat(chunks).toString("utf8"); + bodies.push(raw); + const mode = (req.url?.split("?")[1] ?? "").includes("mode=") ? new URLSearchParams(req.url?.split("?")[1]).get("mode") : undefined; + if (req.url?.includes("/responses/compact")) { + if (mode === "json-error") { + res.writeHead(200, { "content-type": "application/json" }); + res.end(JSON.stringify({ error: { message: "compact failed" } })); + return; + } + if (mode === "empty") { + res.writeHead(200, { "content-type": "application/json" }); + res.end(); + return; + } + res.writeHead(200, { "content-type": "application/json" }); + res.end(JSON.stringify({ id: "resp_c", status: "completed", output: [{ type: "message", role: "user", content: "summary" }], usage: { input_tokens: 10, output_tokens: 1 } })); + return; + } + res.writeHead(200, { "content-type": "text/event-stream", "cache-control": "no-cache" }); + if (mode === "failed") { + res.write(sse("response.created", { type: "response.created", response: { id: "r1" } })); + res.write(sse("response.failed", { type: "response.failed", response: { id: "r1", status: "failed", error: { code: "boom" } } })); + res.end(); + return; + } + if (mode === "truncated") { + res.write(sse("response.created", { type: "response.created", response: { id: "r1" } })); + res.write(sse("response.output_item.added", { type: "response.output_item.added", item: { type: "message" } })); + res.end(); + return; + } + res.write(sse("response.created", { type: "response.created", response: { id: "r1" } })); + res.write(sse("response.completed", { type: "response.completed", response: { id: "r1", status: "completed", output: [], usage: { input_tokens: 100, output_tokens: 5, total_tokens: 105 } } })); + res.end(); + }); + }); + upstream.listen(0, "127.0.0.1"); + await once(upstream, "listening"); + const upstreamPort = upstream.address().port; + + 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: { "gpt-resp": { context: 10_000 } } } }, + modelContextLimit: 10_000, + kernelConfig: defaultConfig(10_000), + 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 proxyPort = proxy.address().port; + const base = `http://127.0.0.1:${proxyPort}/bili/http://127.0.0.1:${upstreamPort}/v1`; + + const postResponses = (path: string, body: unknown) => fetch(`${base}${path}`, { + method: "POST", + headers: { "content-type": "application/json" }, + body: JSON.stringify(body), + }); + + try { + // 1. Trigger form, SSE completed → rebase scheduled. The forwarded + // body must NOT carry the compress prompt/tools, must keep the + // client instructions, and the trigger must stay final. + const r1 = await postResponses("/responses?mode=completed", { + model: "gpt-resp", + stream: true, + session_id: "term-sess", + instructions: "You are the test coding agent.", + tools: [{ type: "custom", name: "shell" }], + input: [...conversation(), { type: "compaction_trigger" }], + }); + assert.equal(r1.status, 200); + await r1.text(); + const fwd = JSON.parse(bodies[bodies.length - 1]) as { input: { type: string; content?: unknown }[]; tools?: { name?: string }[] }; + assert.equal(fwd.input[fwd.input.length - 1].type, "compaction_trigger", "trigger stays final"); + const devContent = String(fwd.input.find((i) => i.type === "message" && typeof i.content === "string" && String(i.content).includes("You are the test coding agent."))?.content ?? ""); + assert.ok(devContent.includes("You are the test coding agent."), "client instructions still forwarded"); + assert.ok(!JSON.stringify(fwd).includes("Compression Philosophy"), "compress prompt NOT injected"); + assert.ok(!(fwd.tools ?? []).some((t) => t.name === "compress"), "compress tool NOT injected"); + assert.equal(pendingRebase("term-sess"), true, "completed trigger response schedules the rebase"); + + // Reconcile on the next turn: state reset, marker consumed. + const r1b = await postResponses("/responses", { + model: "gpt-resp", + stream: true, + session_id: "term-sess", + input: [{ type: "message", role: "user", content: "fresh start" }], + }); + assert.equal(r1b.status, 200); + await r1b.text(); + assert.equal(pendingRebase("term-sess"), false, "rebase reconciled on next turn"); + const s1 = listSessions().find((x) => x.meta.label === "term-sess"); + assert.equal(s1?.blockContents.size, 0, "blocks cleared by the rebase"); + + // 2. Trigger form, SSE failed → NO rebase. + const r2 = await postResponses("/responses?mode=failed", { + model: "gpt-resp", + stream: true, + session_id: "term-failed", + input: [...conversation(), { type: "compaction_trigger" }], + }); + assert.equal(r2.status, 200); + await r2.text(); + assert.equal(pendingRebase("term-failed"), undefined, "failed stream must not schedule a rebase"); + + // 3. Trigger form, SSE truncated (no terminal event) → NO rebase. + const r3 = await postResponses("/responses?mode=truncated", { + model: "gpt-resp", + stream: true, + session_id: "term-trunc", + input: [...conversation(), { type: "compaction_trigger" }], + }); + assert.equal(r3.status, 200); + await r3.text(); + assert.equal(pendingRebase("term-trunc"), undefined, "truncated stream must not schedule a rebase"); + + // 4. Endpoint form, 2xx JSON error body → NO rebase. + const r4 = await postResponses("/responses/compact?mode=json-error", { model: "gpt-resp", session_id: "term-jsonerr" }); + assert.equal(r4.status, 200); + await r4.text(); + assert.equal(pendingRebase("term-jsonerr"), undefined, "2xx error JSON must not schedule a rebase"); + + // 5. Endpoint form, empty body → NO rebase. + const r5 = await postResponses("/responses/compact?mode=empty", { model: "gpt-resp", session_id: "term-empty" }); + assert.equal(r5.status, 200); + await r5.text(); + assert.equal(pendingRebase("term-empty"), undefined, "empty body must not schedule a rebase"); + + // 6. Endpoint form, JSON with output → rebase (the #249 happy path). + const r6 = await postResponses("/responses/compact", { model: "gpt-resp", session_id: "term-jsonok" }); + assert.equal(r6.status, 200); + await r6.text(); + assert.equal(pendingRebase("term-jsonok"), true, "JSON output response schedules the rebase"); + } finally { + proxy.close(); + await once(proxy, "close"); + upstream.close(); + await once(upstream, "close"); + } +}); diff --git a/tests/prefix-affinity.test.ts b/tests/prefix-affinity.test.ts index 7680b55648..f51691b5b3 100644 --- a/tests/prefix-affinity.test.ts +++ b/tests/prefix-affinity.test.ts @@ -15,7 +15,7 @@ test("prefix-affinity: append-only continuation resolves to the same session", ( assert.ok(a); assert.equal(a.matchedDepth, 0); assert.match(a.sessionId, /^pfa-[0-9a-f]{16}$/); - r.note(a.sessionId, a.incomingDepth, a.tailHash); + r.note(a.sessionId, a.incomingDepth, a.tailHash, a.itemHashes); const b = r.resolve([user("hello there friend"), { role: "assistant", content: "hi" }, user("second question")]); assert.ok(b); @@ -27,7 +27,7 @@ test("prefix-affinity: append-only continuation resolves to the same session", ( test("prefix-affinity: different conversations with distinct roots stay separate", () => { const r = new PrefixAffinityResolver(); const a = r.resolve([user("project one setup question")]); - r.note(a!.sessionId, a!.incomingDepth, a!.tailHash); + r.note(a!.sessionId, a!.incomingDepth, a!.tailHash, a!.itemHashes); const b = r.resolve([user("totally different topic opener")]); assert.notEqual(b!.sessionId, a!.sessionId, "different content must not collide"); assert.equal(b!.matchedDepth, 0); @@ -37,12 +37,12 @@ test("prefix-affinity: shared opening, divergent continuation forks on the next const r = new PrefixAffinityResolver(); const shared = [user("same opening message with substance"), { role: "assistant", content: "ok" }]; const a = r.resolve(shared); - r.note(a!.sessionId, a!.incomingDepth, a!.tailHash); + r.note(a!.sessionId, a!.incomingDepth, a!.tailHash, a!.itemHashes); // Branch A extends first — it keeps the session. const aNext = r.resolve([...shared, user("branch A continuation")]); assert.equal(aNext!.sessionId, a!.sessionId); - r.note(aNext!.sessionId, aNext!.incomingDepth, aNext!.tailHash); + r.note(aNext!.sessionId, aNext!.incomingDepth, aNext!.tailHash, aNext!.itemHashes); // Branch B diverges: its history does not extend the stored chain (hash // at stored depth differs), so it starts its own session. @@ -54,7 +54,7 @@ test("prefix-affinity: shared opening, divergent continuation forks on the next test("prefix-affinity: identical replay after restart reattaches the same deterministic id", () => { const first = new PrefixAffinityResolver(); const a = first.resolve([user("persistent conversation anchor")]); - first.note(a!.sessionId, a!.incomingDepth, a!.tailHash); + first.note(a!.sessionId, a!.incomingDepth, a!.tailHash, a!.itemHashes); // Fresh process: no in-memory index, same first-turn content. const second = new PrefixAffinityResolver(); @@ -66,7 +66,7 @@ test("prefix-affinity: trimmed history no longer matches — safe new session", const r = new PrefixAffinityResolver(); const full = [user("first message with content"), { role: "assistant", content: "r1" }, user("second message")]; const a = r.resolve(full); - r.note(a!.sessionId, a!.incomingDepth, a!.tailHash); + r.note(a!.sessionId, a!.incomingDepth, a!.tailHash, a!.itemHashes); // Client-side microcompact drops the middle turn: the stored 3-deep chain // is NOT a prefix of the trimmed history (it is longer). @@ -80,7 +80,7 @@ test("prefix-affinity: no environmental partitioning — same content survives c const r = new PrefixAffinityResolver(); const history = [user("same words across rotating credentials")]; const a = r.resolve(history); - r.note(a!.sessionId, a!.incomingDepth, a!.tailHash); + r.note(a!.sessionId, a!.incomingDepth, a!.tailHash, a!.itemHashes); // #286 lesson: bearer rotation / relay switching / protocol translation // happen MID-CONVERSATION. There is no partition surface at all, so the // identical replay keeps resolving to the same session. @@ -106,7 +106,7 @@ test("prefix-affinity: system+user openai shape (shared system must not collide) const r = new PrefixAffinityResolver(); const sys = { role: "system", content: "You are ZCode, a shared IDE system prompt injected into every conversation." }; const a = r.resolve([sys, user("question about project A")]); - r.note(a!.sessionId, a!.incomingDepth, a!.tailHash); + r.note(a!.sessionId, a!.incomingDepth, a!.tailHash, a!.itemHashes); const b = r.resolve([sys, user("question about project B")]); assert.notEqual(b!.sessionId, a!.sessionId, "identical system prefix must not merge distinct conversations"); }); @@ -115,7 +115,7 @@ test("prefix-affinity: LRU cap bounds tracked sessions", () => { const r = new PrefixAffinityResolver(); for (let i = 0; i < 262; i++) { const a = r.resolve([user(`unique conversation number ${i} with filler content`)]); - r.note(a!.sessionId, a!.incomingDepth, a!.tailHash); + r.note(a!.sessionId, a!.incomingDepth, a!.tailHash, a!.itemHashes); } assert.equal(r.trackedSessionIds().length, 256); }); @@ -130,7 +130,7 @@ test("prefix-affinity: stableStringify sorts keys recursively", () => { test("prefix-affinity: key-order differences across replays still match", () => { const r = new PrefixAffinityResolver(); const a = r.resolve([{ role: "user", content: "key order robustness check", meta: { x: 1, y: 2 } }]); - r.note(a!.sessionId, a!.incomingDepth, a!.tailHash); + r.note(a!.sessionId, a!.incomingDepth, a!.tailHash, a!.itemHashes); const b = r.resolve([{ meta: { y: 2, x: 1 }, content: "key order robustness check", role: "user" }]); assert.equal(b!.sessionId, a!.sessionId, "same logical message in different key order must match"); }); diff --git a/tests/proxy-session-id.test.ts b/tests/proxy-session-id.test.ts index a314121379..928a53cab3 100644 --- a/tests/proxy-session-id.test.ts +++ b/tests/proxy-session-id.test.ts @@ -1,6 +1,6 @@ import { test } from "node:test"; import assert from "node:assert/strict"; -import { affinityToken, clientConversationHeader, preferPromptCacheKeyIdentity } from "../src/session-id.ts"; +import { affinityToken, clientConversationHeader, codexTurnIdentity, preferPromptCacheKeyIdentity } from "../src/session-id.ts"; import { conversationIdentityResponses, conversationSignalResponses } from "acp-kernel/wire"; test("preferPromptCacheKeyIdentity: fingerprint + prompt_cache_key → stable client-provided identity (omp stateless replay)", () => { @@ -136,3 +136,102 @@ test("conversationIdentityResponses: identical anonymous openers share a content assert.equal(a, b); assert.match(a, /^[0-9a-f]{16}$/); }); + +// ---- codexTurnIdentity (#316 / PR-A): codex turn-metadata partitioning ---- + +const ROOT_SESSION = "01a048b8-c704-7c00-8000-000000000000"; +const SUB_THREAD = "01a048b8-c728-7c00-8000-000000000000"; +const rootMeta = (over: Record = {}) => + JSON.stringify({ request_kind: "turn", thread_source: "user", thread_id: ROOT_SESSION, turn_id: "turn-1", window_id: "win-1", ...over }); +const subMeta = (over: Record = {}) => + JSON.stringify({ request_kind: "turn", thread_source: "subagent", thread_id: SUB_THREAD, turn_id: "turn-2", window_id: "win-1", ...over }); + +test("codexTurnIdentity: thread_source user → undefined (legacy precedence chain owns the root id)", () => { + const id = codexTurnIdentity({ + "session-id": ROOT_SESSION, + "thread-id": ROOT_SESSION, + "x-codex-turn-metadata": rootMeta(), + }); + assert.equal(id, undefined, "validated root turn must fall through to the legacy chain (stronger headers may win)"); +}); + +test("codexTurnIdentity: thread_source subagent → thread-id header (fresh per-thread state, #150)", () => { + // A subagent REUSES the root's session-id header but carries its own + // thread-id — it must resolve to the thread-id, NOT the root session. + const id = codexTurnIdentity({ + "session-id": ROOT_SESSION, + "thread-id": SUB_THREAD, + "x-codex-turn-metadata": subMeta(), + }); + assert.deepEqual(id, { value: SUB_THREAD, threadSource: "subagent" }); +}); + +test("codexTurnIdentity: root identity is stable across turns (turn_id/window_id churn is irrelevant)", () => { + const a = codexTurnIdentity({ "session-id": ROOT_SESSION, "thread-id": ROOT_SESSION, "x-codex-turn-metadata": rootMeta({ turn_id: "turn-1", window_id: "w1" }) }); + const b = codexTurnIdentity({ "session-id": ROOT_SESSION, "thread-id": ROOT_SESSION, "x-codex-turn-metadata": rootMeta({ turn_id: "turn-99", window_id: "w42" }) }); + assert.deepEqual(a, b); + assert.equal(a, undefined); +}); + +test("codexTurnIdentity: metadata.thread_id must equal the thread-id header (cross-check, PR #249)", () => { + // metadata says one thread, header says another → do not trust. + const mismatch = codexTurnIdentity({ + "session-id": ROOT_SESSION, + "thread-id": SUB_THREAD, + "x-codex-turn-metadata": rootMeta({ thread_id: "01a048b8-ffff-7c00-8000-000000000000" }), + }); + assert.equal(mismatch, undefined); + // header missing entirely → do not trust. + const noHeader = codexTurnIdentity({ + "session-id": ROOT_SESSION, + "x-codex-turn-metadata": subMeta(), + }); + assert.equal(noHeader, undefined); +}); + +test("codexTurnIdentity: JSON parse failure / non-object → do not trust (legacy chain)", () => { + assert.equal(codexTurnIdentity({ "session-id": ROOT_SESSION, "thread-id": ROOT_SESSION, "x-codex-turn-metadata": "{not json" }), undefined); + assert.equal(codexTurnIdentity({ "session-id": ROOT_SESSION, "thread-id": ROOT_SESSION, "x-codex-turn-metadata": "42" }), undefined); + assert.equal(codexTurnIdentity({ "session-id": ROOT_SESSION, "thread-id": ROOT_SESSION, "x-codex-turn-metadata": "[1,2,3]" }), undefined); + assert.equal(codexTurnIdentity({ "session-id": ROOT_SESSION, "thread-id": ROOT_SESSION, "x-codex-turn-metadata": "null" }), undefined); + assert.equal(codexTurnIdentity({ "session-id": ROOT_SESSION, "thread-id": ROOT_SESSION, "x-codex-turn-metadata": " " }), undefined); +}); + +test("codexTurnIdentity: non-string/missing thread_source → do not trust (legacy chain)", () => { + assert.equal(codexTurnIdentity({ "session-id": ROOT_SESSION, "thread-id": ROOT_SESSION, "x-codex-turn-metadata": rootMeta({ thread_source: undefined }) }), undefined); + assert.equal(codexTurnIdentity({ "session-id": ROOT_SESSION, "thread-id": ROOT_SESSION, "x-codex-turn-metadata": rootMeta({ thread_source: 42 }) }), undefined); + assert.equal(codexTurnIdentity({ "session-id": ROOT_SESSION, "thread-id": ROOT_SESSION, "x-codex-turn-metadata": rootMeta({ thread_source: " " }) }), undefined); + assert.equal(codexTurnIdentity({ "session-id": ROOT_SESSION, "thread-id": ROOT_SESSION, "x-codex-turn-metadata": rootMeta({ thread_id: 123 }) }), undefined); +}); + +test("codexTurnIdentity: any non-user thread_source partitions by thread-id (guardian family, #150)", () => { + // codex ThreadSource serializes more than user/subagent: review sessions + // send "guardian_review", the guardian-v2 async scorer sends + // "guardian_classifier" (Feature string), and memory consolidation sends + // "memory_consolidation". All are internal threads that must NOT inherit + // the root session's compression state — the discrimination is inverted: + // only "user" joins the root, everything else gets its own thread state. + for (const source of ["subagent", "guardian_review", "guardian_classifier", "memory_consolidation", "collab_spawn", "system"]) { + const id = codexTurnIdentity({ + "session-id": ROOT_SESSION, + "thread-id": SUB_THREAD, + "x-codex-turn-metadata": subMeta({ thread_source: source, thread_id: SUB_THREAD }), + }); + assert.deepEqual(id, { value: SUB_THREAD, threadSource: source }, `thread_source=${source}`); + } +}); + +test("codexTurnIdentity: user turn with no session-id header → do not trust (legacy chain)", () => { + assert.equal(codexTurnIdentity({ "thread-id": ROOT_SESSION, "x-codex-turn-metadata": rootMeta() }), undefined); +}); + +test("codexTurnIdentity: no metadata header (omp / other Responses clients) → undefined, legacy chain untouched", () => { + // omp-style stateless replay: prompt_cache_key identity, no codex headers. + // codexTurnIdentity must return undefined so the pck promotion chain runs. + const id = codexTurnIdentity({}); + assert.equal(id, undefined); + const body = { input: [{ type: "message", role: "user", content: "hi" }], prompt_cache_key: "omp-pck-1" }; + const promoted = preferPromptCacheKeyIdentity(conversationIdentityResponses(body, undefined), body); + assert.equal(promoted!.source, "prompt-cache-key"); + assert.equal(promoted!.value, "omp-pck-1"); +}); diff --git a/tests/stream-terminal.test.ts b/tests/stream-terminal.test.ts new file mode 100644 index 0000000000..9b592f298e --- /dev/null +++ b/tests/stream-terminal.test.ts @@ -0,0 +1,117 @@ +import test from "node:test"; +import assert from "node:assert/strict"; +import { observeResponsesTerminalState } from "../src/stream-terminal.ts"; + +function sseStream(chunks: string[]): ReadableStream { + const enc = new TextEncoder(); + return new ReadableStream({ + start(controller) { + for (const c of chunks) controller.enqueue(enc.encode(c)); + controller.close(); + }, + }); +} + +function sse(type: string, data: unknown): string { + return `event: ${type}\ndata: ${JSON.stringify(data)}\n\n`; +} + +test("SSE: response.completed terminal event → completed", async () => { + const stream = sseStream([ + sse("response.created", { type: "response.created", response: { id: "r1" } }), + sse("response.output_item.added", { type: "response.output_item.added", item: { type: "compaction" } }), + sse("response.completed", { type: "response.completed", response: { id: "r1", status: "completed" } }), + ]); + assert.equal(await observeResponsesTerminalState(stream, true), "completed"); +}); + +test("SSE: response.failed → failed", async () => { + const stream = sseStream([ + sse("response.created", { type: "response.created", response: { id: "r1" } }), + sse("response.failed", { type: "response.failed", response: { status: "failed", error: { code: "boom" } } }), + ]); + assert.equal(await observeResponsesTerminalState(stream, true), "failed"); +}); + +test("SSE: response.incomplete → failed", async () => { + const stream = sseStream([ + sse("response.incomplete", { type: "response.incomplete", response: { status: "incomplete" } }), + ]); + assert.equal(await observeResponsesTerminalState(stream, true), "failed"); +}); + +test("SSE: stream ends without terminal event (truncation) → unknown", async () => { + const stream = sseStream([ + sse("response.created", { type: "response.created", response: { id: "r1" } }), + sse("response.output_item.added", { type: "response.output_item.added", item: { type: "message" } }), + ]); + assert.equal(await observeResponsesTerminalState(stream, true), "unknown"); +}); + +test("SSE: empty stream → unknown", async () => { + assert.equal(await observeResponsesTerminalState(sseStream([]), true), "unknown"); +}); + +test("SSE: data-line type fallback when event: lines are stripped", async () => { + const stream = sseStream([ + `data: ${JSON.stringify({ type: "response.created", response: { id: "r1" } })}\n\n`, + `data: ${JSON.stringify({ type: "response.completed", response: { id: "r1" } })}\n\n`, + ]); + assert.equal(await observeResponsesTerminalState(stream, true), "completed"); + const failed = sseStream([`data: ${JSON.stringify({ type: "response.failed", response: {} })}\n\n`]); + assert.equal(await observeResponsesTerminalState(failed, true), "failed"); +}); + +test("SSE: CRLF endings and events split across chunks", async () => { + const full = sse("response.created", { type: "response.created" }) + sse("response.completed", { type: "response.completed" }); + const crlf = full.replace(/\n/g, "\r\n"); + const mid = Math.floor(crlf.length / 2); + assert.equal(await observeResponsesTerminalState(sseStream([crlf.slice(0, mid), crlf.slice(mid)]), true), "completed"); +}); + +test("SSE: terminal event wins even if more events follow", async () => { + const stream = sseStream([ + sse("response.completed", { type: "response.completed", response: { id: "r1" } }), + sse("response.output_item.added", { type: "response.output_item.added", item: {} }), + ]); + assert.equal(await observeResponsesTerminalState(stream, true), "completed"); +}); + +test("SSE: non-JSON data lines are ignored, not fatal", async () => { + const stream = sseStream([ + "data: [DONE]\n\n", + sse("response.completed", { type: "response.completed", response: { id: "r1" } }), + ]); + assert.equal(await observeResponsesTerminalState(stream, true), "completed"); +}); + +test("JSON: output array present → completed", async () => { + const stream = sseStream([JSON.stringify({ id: "r1", status: "completed", output: [{ type: "compaction", encrypted_content: "x" }], usage: {} })]); + assert.equal(await observeResponsesTerminalState(stream, false), "completed"); +}); + +test("JSON: error field present → failed", async () => { + const stream = sseStream([JSON.stringify({ error: { message: "compact failed" } })]); + assert.equal(await observeResponsesTerminalState(stream, false), "failed"); +}); + +test("JSON: unparseable body → unknown", async () => { + assert.equal(await observeResponsesTerminalState(sseStream(["502 bad gateway"]), false), "unknown"); +}); + +test("JSON: non-object body → unknown", async () => { + assert.equal(await observeResponsesTerminalState(sseStream(["42"]), false), "unknown"); +}); + +test("JSON: empty body → unknown", async () => { + assert.equal(await observeResponsesTerminalState(sseStream([]), false), "unknown"); +}); + +test("JSON: body without output field → unknown", async () => { + assert.equal(await observeResponsesTerminalState(sseStream([JSON.stringify({ id: "r1", status: "completed" })]), false), "unknown"); +}); + +test("JSON: oversized body → unknown (safe default)", async () => { + const big = JSON.stringify({ output: ["x".repeat((16 << 20) + 1)] }); + assert.equal(await observeResponsesTerminalState(sseStream([big]), false), "unknown"); +}); diff --git a/tests/tail-window-reconcile.test.ts b/tests/tail-window-reconcile.test.ts new file mode 100644 index 0000000000..7d8d32a1e0 --- /dev/null +++ b/tests/tail-window-reconcile.test.ts @@ -0,0 +1,176 @@ +import assert from "node:assert"; +import test from "node:test"; + +process.env.NODE_ENV = "test"; + +import { PrefixAffinityResolver } from "../src/prefix-affinity.ts"; +import { createCore, createInitialState, defaultConfig } from "acp-kernel"; +import { anthropicToCore, type AnthropicRequestBody } from "acp-kernel/wire"; + +function user(text: string): Record { + return { role: "user", content: text }; +} + +// N distinct user messages (content long enough to clear MIN_CANONICAL_BYTES). +function chain(n: number, prefix = "msg"): Record[] { + const out: Record[] = []; + for (let i = 0; i < n; i++) out.push(user(`${prefix} ${i} with enough substance to hash`)); + return out; +} + +test("tail-window: truncated replay (drop oldest) reattaches the original session", () => { + const r = new PrefixAffinityResolver(); + const full = chain(10); + const a = r.resolve(full)!; + assert.equal(a.via, "new"); + r.note(a.sessionId, a.incomingDepth, a.tailHash, a.itemHashes); + + // Client keeps a fixed recent window: drops the 2 oldest, appends 1 new. + // Incoming head (first 8) == stored tail (last 8) → tail-window reattach. + const truncated = [...full.slice(2), user("a brand new tenth-plus turn here")]; + const b = r.resolve(truncated)!; + assert.equal(b.sessionId, a.sessionId, "truncated replay must reattach the original session"); + assert.equal(b.via, "tail-window"); + assert.equal(b.matchedDepth, 8, "window is capped at TAIL_WINDOW=8"); + assert.equal(b.storedDepth, 10); + assert.equal(b.incomingDepth, 9); +}); + +test("tail-window: rolling-window client retaining > TAIL_WINDOW items reattaches at any offset", () => { + // A dsh-style web client keeps a 20-30 item rolling window: the retained + // suffix is LONGER than TAIL_WINDOW, so the incoming head matches the + // stored chain in the middle, not at its tail. The any-offset substring + // search must still find it (tail-pinned matching would orphan it). + const r = new PrefixAffinityResolver(); + const full = chain(30); + const a = r.resolve(full)!; + assert.equal(a.via, "new"); + r.note(a.sessionId, a.incomingDepth, a.tailHash, a.itemHashes); + + // Sliding window: retains the last 20 (> TAIL_WINDOW=8), appends 1 new. + const slid = [...full.slice(10), user("a brand new turn after the window slid")]; + const b = r.resolve(slid)!; + assert.equal(b.sessionId, a.sessionId, "a >8-item rolling window must reattach the original session"); + assert.equal(b.via, "tail-window"); + assert.equal(b.matchedDepth, 8); + + // Deeper slides keep reattaching as the window keeps sliding. + r.note(b.sessionId, b.incomingDepth, b.tailHash, b.itemHashes); + const slid2 = [...full.slice(20), user("yet another turn after sliding again")]; + const c = r.resolve(slid2)!; + assert.equal(c.sessionId, a.sessionId, "a second slide must reattach the same session"); + assert.equal(c.via, "tail-window"); +}); + +test("tail-window: append-only continuation still resolves via prefix (regression)", () => { + const r = new PrefixAffinityResolver(); + const base = chain(4); + const a = r.resolve(base)!; + r.note(a.sessionId, a.incomingDepth, a.tailHash, a.itemHashes); + const b = r.resolve([...base, user("an appended continuation turn")])!; + assert.equal(b.sessionId, a.sessionId); + assert.equal(b.via, "prefix", "a true prefix extension must not be misread as a tail reattach"); + assert.equal(b.matchedDepth, 4); +}); + +test("tail-window: brand-new conversation resolves via new (regression)", () => { + const r = new PrefixAffinityResolver(); + const a = r.resolve(chain(3, "alpha"))!; + r.note(a.sessionId, a.incomingDepth, a.tailHash, a.itemHashes); + const b = r.resolve(chain(3, "beta"))!; + assert.equal(b.via, "new"); + assert.equal(b.matchedDepth, 0); + assert.notEqual(b.sessionId, a.sessionId); +}); + +test("tail-window: distinct conversation sharing a long templated head must NOT merge (offset-0 guard)", () => { + const r = new PrefixAffinityResolver(); + const sharedHead = chain(8, "templated-onboarding"); + const aFull = [...sharedHead, ...chain(4, "conv-a-body")]; + const a = r.resolve(aFull)!; + r.note(a.sessionId, a.incomingDepth, a.tailHash, a.itemHashes); + + // B shares the entire 8-item head and diverges after it. A truncation + // reattach can never start at the stored head (such a client would be + // caught by the full-prefix step), so B must NOT be merged into A. + const bIncoming = [...sharedHead, ...chain(3, "conv-b-body")]; + const b = r.resolve(bIncoming)!; + assert.equal(b.via, "new", "a shared templated head must not reattach the other conversation"); + assert.notEqual(b.sessionId, a.sessionId); +}); + +test("fork lineage: divergent continuation records forked lineage (UI/debug only)", () => { + const r = new PrefixAffinityResolver(); + const shared = chain(4, "shared"); + const a = r.resolve(shared)!; + r.note(a.sessionId, a.incomingDepth, a.tailHash, a.itemHashes); + + // Replace the 4th item: shares a 3-item prefix, diverges at item 4. + const divergent = [...shared.slice(0, 3), user("a divergent fourth turn here")]; + const b = r.resolve(divergent)!; + assert.equal(b.via, "new", "a divergence must not steal the stored chain"); + assert.notEqual(b.sessionId, a.sessionId); + assert.equal(b.lineage?.reason, "forked"); + assert.deepEqual(b.lineage?.parents, [a.sessionId]); + assert.equal(b.lineage?.sharedPrefix, 3); +}); + +test("truncated lineage: ambiguous multi-candidate records truncated lineage", () => { + const r = new PrefixAffinityResolver(); + // Two tracked chains that share the same trailing 8 items but differ in + // their oldest items. A truncated replay whose head == that shared tail is + // ambiguous → new session with a "truncated" lineage naming both parents. + const tail = chain(8, "common-tail"); + const aFull = [...chain(2, "branch-a-head"), ...tail]; + const bFull = [...chain(2, "branch-b-head"), ...tail]; + const a = r.resolve(aFull)!; + r.note(a.sessionId, a.incomingDepth, a.tailHash, a.itemHashes); + const b = r.resolve(bFull)!; + r.note(b.sessionId, b.incomingDepth, b.tailHash, b.itemHashes); + + const incoming = [...tail, user("a fresh turn after truncation")]; + const c = r.resolve(incoming)!; + assert.equal(c.via, "new", "an ambiguous tail match must not guess a parent"); + assert.equal(c.lineage?.reason, "truncated"); + assert.ok(c.lineage?.parents.includes(a.sessionId)); + assert.ok(c.lineage?.parents.includes(b.sessionId)); +}); + +// Kernel reconcile anchor (#316 / PR-B): a truncated replay against +// block-bearing state must NOT throw, must deactivate the affected block +// gracefully (active → false), and must revive it (survivedCount++) when the +// full history returns. This is what makes tail-window reattach safe with zero +// kernel changes. +test("kernel reconcile: truncated replay deactivates blocks, full replay revives them (no throw)", () => { + const core = createCore(); + const config = defaultConfig(200000); + const state = createInitialState(); + const body: AnthropicRequestBody = { model: "claude-test", messages: [] }; + for (let i = 0; i < 40; i++) body.messages.push({ role: i % 2 === 0 ? "user" : "assistant", content: `message ${i} ${"y".repeat(500)}` }); + const { msgs } = anthropicToCore(body); + const turn = core.processTurn({ messages: msgs, state, config, tokenCount: 9999, renderTags: "text-only" }); + const res = core.applyCompression({ + ranges: [{ startRef: "m00001", endRef: "m00015", summary: "early history summary long enough to pass min length check".repeat(3) }], + state: turn.state, config, messages: turn.messages, + }); + let st = res.state; + const block = st.blocks[0]; + assert.ok(block, "a compression block should exist"); + assert.equal(block.active, true, "block starts active"); + const survivedBefore = block.survivedCount; + + // Truncated replay: drop the 10 oldest messages (which include the covered ones). + const truncated = core.processTurn({ messages: msgs.slice(10), state: st, config, tokenCount: 9999, renderTags: "text-only" }); + st = truncated.state; + const deactivated = st.blocks.find((b) => b.blockId === block.blockId); + assert.ok(deactivated, "block still present after truncation"); + assert.equal(deactivated.active, false, "block must deactivate gracefully when its messages are truncated away"); + + // Full replay: the messages return → the block revives. + const full = core.processTurn({ messages: msgs, state: st, config, tokenCount: 9999, renderTags: "text-only" }); + st = full.state; + const revived = st.blocks.find((b) => b.blockId === block.blockId); + assert.ok(revived, "block still present after full replay"); + assert.equal(revived.active, true, "block must revive when its messages return"); + assert.equal(revived.survivedCount, survivedBefore + 1, "survivedCount must increment on revival"); +});