From 5336c070040648d09f2baae788fa94137f1e6b32 Mon Sep 17 00:00:00 2001 From: ranxianglei Date: Wed, 7 Oct 2026 00:28:59 +0800 Subject: [PATCH 1/3] =?UTF-8?q?feat(e2e):=20real-codex=20persona=20CI=20?= =?UTF-8?q?=E2=80=94=20URL=20+=20launcher=20lanes,=20exec-resume=20-c=20mo?= =?UTF-8?q?del=20fix?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - tests/e2e/e2e-codex-persona.test.ts: two lanes against the REAL codex binary (pinned 0.147.0, fake upstream, zero tokens): * URL lane (/bili/ prefix routing): grow+fold, then AGENTS.md edit + gpt-5-codex switch, then a PURE gpt-5.1 switch — asserts fold state survives both drifts on one session, persona carrier byte-stable per model, switched model actually on the wire. * launcher lane (bili codex overlay, #2197): same shape; pins the exec-resume argv contract. - src/launcher.ts codexRewriteInsertionIndex: insert the provider rewrites BEFORE the resume subcommand — any -c after resume makes codex-cli 0.147.0 silently drop the user's earlier -c model=… (bisected against the real binary; launcher drift turns used to revert to the config default model without this). - tests/e2e/fake-upstream.mjs: oracle rows gain insLen (additive). - .github/workflows/ci-e2e-codex-persona.yml: dedicated lane, 15min, ACP_TEST_E2E_FAKE=1, failure artifacts. --- .github/workflows/ci-e2e-codex-persona.yml | 58 +++ src/launcher.ts | 52 ++- tests/e2e/e2e-codex-persona.test.ts | 510 +++++++++++++++++++++ tests/e2e/fake-upstream.mjs | 3 +- tests/launcher.test.ts | 59 +++ 5 files changed, 679 insertions(+), 3 deletions(-) create mode 100644 .github/workflows/ci-e2e-codex-persona.yml create mode 100644 tests/e2e/e2e-codex-persona.test.ts diff --git a/.github/workflows/ci-e2e-codex-persona.yml b/.github/workflows/ci-e2e-codex-persona.yml new file mode 100644 index 0000000000..31852122ca --- /dev/null +++ b/.github/workflows/ci-e2e-codex-persona.yml @@ -0,0 +1,58 @@ +name: e2e-codex-persona + +on: + pull_request: + workflow_dispatch: + +concurrency: + group: e2e-codex-persona-${{ github.workflow }}-${{ github.ref }} + cancel-in-progress: true + +jobs: + e2e-codex-persona: + name: real codex persona/continuity in BOTH modes (URL + launcher) vs fake upstream (zero tokens) + runs-on: ubuntu-latest + timeout-minutes: 15 + steps: + - uses: actions/checkout@v4 + + - uses: actions/setup-node@v4 + with: + node-version: 22 + cache: npm + + # Pinned like ci-e2e-fake.yml (#815): codex releases change the baseline + # prompt/input assembly; bump deliberately after re-running this suite + # locally on the new version. A version bump is ALSO the one real-world + # trigger that drifts `instructions` mid-session (#2250) — if the bump + # flips assertInstructionsStable, that is the suite doing its job: + # re-verify the persona migrate path keeps the single-summarization + # contract before unpinning. + - name: Install codex CLI + run: npm i -g @openai/codex@0.147.0 + + - name: Print codex version + run: codex --version + + - name: Install deps + run: npm ci + + - name: Build + run: npm run build + + - name: Run real-codex persona/continuity E2E (URL mode + launcher mode) + run: ACP_TEST_E2E_FAKE=1 node --import tsx --test tests/e2e/e2e-codex-persona.test.ts + + - name: Collect E2E artifacts + if: failure() + run: | + mkdir -p tmp/e2e-artifacts + for d in tmp/e2e-codex-persona-*; do [ -d "$d" ] && cp -r "$d" tmp/e2e-artifacts/ || true; done + + - name: Upload artifacts + uses: actions/upload-artifact@v4 + if: failure() + with: + name: e2e-codex-persona-artifacts + path: tmp/e2e-artifacts + if-no-files-found: ignore diff --git a/src/launcher.ts b/src/launcher.ts index 582e16ab3a..da4ec2a677 100644 --- a/src/launcher.ts +++ b/src/launcher.ts @@ -977,9 +977,57 @@ export function buildCodexArgs( rewrites.push("-c", `${r.key}=${r.realUpstream}`); } if (rewrites.length === 0) return [...extra]; + const at = codexRewriteInsertionIndex(extra); + return [...extra.slice(0, at), ...rewrites, ...extra.slice(at)]; +} + +/** codex subcommand flags that take NO value (clap booleans). Used only to + * decide whether the trailing non-dash token is a free-standing positional + * (safe insertion point) — see codexRewriteInsertionIndex. */ +const CODEX_BOOLEAN_FLAGS = new Set([ + "--skip-git-repo-check", + "--last", + "--full-auto", + "--dangerously-bypass-approvals-and-sandbox", + "--no-daemon", + "--search", + "--no-search", + "--help", + "--version", +]); + +/** Where buildCodexArgs splices its rewrites into the user's argv. + * + * #2260(D): everything past a `--` is positional to codex's parser, so the + * rewrites must land BEFORE it (appending turned them into prompt text and + * silently bypassed the proxy). + * + * #2289: codex-cli 0.147.0 parses `exec resume` such that a `-c` flag placed + * AFTER the positional prompt silently DROPS an earlier `-c model=…` on the + * same command line (bisected 2026-10-07: resume + trailing `-c` loses the + * override; fresh exec, or resume without a trailing `-c`, both keep it). + * Since the rewrites ARE trailing `-c` flags, appending them after the user's + * prompt broke every `bili codex exec resume … ` turn for users + * passing `-c model=…` — the model silently reverted to the config default. + * Insert before the `resume` subcommand when present (any `-c` AFTER + * `resume` suppresses the user's earlier `-c`s — verified against 0.147.0: + * [-c model=B, resume, --last, -c provider…, prompt] still drops model=B), + * otherwise before the trailing prompt — but ONLY when the last token is + * provably a free-standing positional (its predecessor is a known boolean + * flag or itself a positional) — never between a value-taking flag and its + * value, and never into flags we do not recognise (those keep the legacy + * append, which is at worst today's behavior). */ +export function codexRewriteInsertionIndex(extra: readonly string[]): number { const sep = extra.indexOf("--"); - if (sep === -1) return [...extra, ...rewrites]; - return [...extra.slice(0, sep), ...rewrites, ...extra.slice(sep)]; + if (sep !== -1) return sep; + const resume = extra.indexOf("resume"); + if (resume !== -1) return resume; // keep every -c ahead of the resume subcommand + const last = extra.length - 1; + if (last < 0) return 0; + if (extra[last].startsWith("-")) return extra.length; // flags-only tail — append + const prev = last >= 1 ? extra[last - 1] : undefined; + if (prev === undefined || !prev.startsWith("-") || CODEX_BOOLEAN_FLAGS.has(prev)) return last; + return extra.length; // prev may be a value-taking flag whose value IS the last token — do not split } /** diff --git a/tests/e2e/e2e-codex-persona.test.ts b/tests/e2e/e2e-codex-persona.test.ts new file mode 100644 index 0000000000..536443fdd3 --- /dev/null +++ b/tests/e2e/e2e-codex-persona.test.ts @@ -0,0 +1,510 @@ +// Real-codex persona/continuity E2E (#2250 CI follow-up): drives the REAL +// codex binary (pinned 0.147.0 in CI) through bili in BOTH integration modes +// and proves a mid-session identity perturbation — model switch (-c model=…) +// plus an AGENTS.md edit — keeps the compression state: exactly ONE +// summarization across the whole run, the folded summary still on the wire, +// planted sentinels retained. +// +// Probe findings recorded against codex-cli 0.147.0 (2026-10-07, see the +// linked issue): the top-level `instructions` field is BYTE-STABLE under every +// mid-session trigger we could produce — model switch (unknown AND known +// families), AGENTS.md add/edit, even experimental_instructions_file. Real +// instructions drift only arrives with codex VERSION upgrades (prompt +// assembly changes), which is exactly the #2250 upgrade-shaped scenario the +// resolver covers via migrate. This suite therefore pins two contracts: +// (a) today's stable-instructions runs never lose state (no re-fold), and +// (b) if a future codex drifts instructions mid-session, the persona +// migrate path keeps ONE session — a fork would re-fold from zero and +// surface as a SECOND summarization, tripping the same oracle. +// +// Modes: +// A. URL mode ("改url"): config.toml base_url points at the /bili/ prefix. +// B. Launcher mode ("bili 启动器"): plain provider base_url; `bili codex` +// wraps it via its overlay. Sequential launcher runs resume --last. +// +// Deterministic, zero tokens (fake-upstream.mjs). Gate: ACP_TEST_E2E_FAKE=1. + +import { spawn, spawnSync } from "node:child_process"; +import fs from "node:fs"; +import net from "node:net"; +import os from "node:os"; +import path from "node:path"; +import { strict as assert } from "node:assert"; +import { test } from "node:test"; +import { assertPortDead } from "../port-race.js"; + +const CODEX_BIN = process.env.E2E_CODEX_BIN ?? "codex"; +const DIST = process.env.E2E_BILI_DIST ?? path.resolve(import.meta.dirname, "../../dist/index.js"); +const MODEL = process.env.E2E_MODEL ?? "qwen3.8-27b"; +// Probe fact (0.147.0): a resume-time `-c model=` override is +// IGNORED (the request rides the session's model) while known-family overrides +// (gpt-5-codex, gpt-5.1) ARE applied. The perturbation therefore uses a known +// family and the oracle VERIFIES the switch actually landed on the wire. +const MODEL_B = process.env.E2E_MODEL_B ?? "gpt-5-codex"; +const MODEL_C = process.env.E2E_MODEL_C ?? "gpt-5.1"; +const TMO = Number(process.env.E2E_TMO ?? 120_000); +const FAKE_UPSTREAM = path.join(import.meta.dirname, "fake-upstream.mjs"); +const WORK_ROOT = path.join(process.cwd(), "tmp"); +fs.mkdirSync(WORK_ROOT, { recursive: true }); +// codex discovers AGENTS.md by walking UP from its spawn cwd (#815): keep the +// cwd outside the repo tree. This suite WRITES an AGENTS.md there on purpose +// (the perturbation under test) — the guard below still guards the pre-turn +// hermeticity, checked before any write. +const CWD_ROOT = path.join(os.tmpdir(), "billion-context-e2e"); +fs.mkdirSync(CWD_ROOT, { recursive: true }); + +function repoDocLeak(dir: string): string | null { + let d = dir; + for (;;) { + if (fs.existsSync(path.join(d, ".git")) || fs.existsSync(path.join(d, "AGENTS.md"))) return d; + const parent = path.dirname(d); + if (parent === d) return null; + d = parent; + } +} + +// #2197: resolve codex through PATH/PATHEXT instead of a bare spawn; on a +// lane where the gate is on and the binary is missing we must fail loudly. +function resolveCodexBin(): string | null { + if (CODEX_BIN.includes(path.sep) || CODEX_BIN.includes("/")) return fs.existsSync(CODEX_BIN) ? CODEX_BIN : null; + const exts = process.platform === "win32" + ? (process.env.PATHEXT ?? ".CMD;.EXE;.BAT;.COM;", "").split(";").filter(Boolean).map((e) => e.toLowerCase()) + : [""]; + for (const dir of (process.env.PATH ?? "").split(path.delimiter).filter(Boolean)) { + for (const ext of exts) { + const candidate = path.join(dir, CODEX_BIN + ext); + try { if (fs.statSync(candidate).isFile()) return candidate; } catch { /* keep looking */ } + } + } + return null; +} +const CODEX_RESOLVED = resolveCodexBin(); +const run = process.env.ACP_TEST_E2E_FAKE === "1"; +if (run && !CODEX_RESOLVED) { + throw new Error(`ACP_TEST_E2E_FAKE=1 but no "${CODEX_BIN}" resolvable via PATH — refusing to silently skip (#2197)`); +} +const skipReason = !run + ? "set ACP_TEST_E2E_FAKE=1 (real codex + local fake upstream; deterministic, zero tokens)" + : (!CODEX_RESOLVED || spawnSync(CODEX_RESOLVED, ["--version"], { timeout: 15_000 }).status !== 0 + ? `codex binary "${CODEX_BIN}" not found on PATH` + : undefined); + +function filler(i: number, lines: number): string { + const out: string[] = []; + for (let n = 0; n < lines; n += 1) { + out.push(`doc#${String(i).padStart(2, "0")} line${String(n).padStart(4, "0")} checksum ${(n * 7919 + i * 104729) % 999983}`); + } + return out.join("\n"); +} + +type OracleEntry = { t: number; model: string; stream: boolean; isSummary: boolean; inputLen: number; insLen?: number; input: unknown[] }; + +function flatContent(c: unknown): string { + if (typeof c === "string") return c; + if (Array.isArray(c)) return c.map((p) => (p && typeof p === "object" && "text" in p ? String((p as { text: unknown }).text) : "")).join(""); + return String(c ?? ""); +} +function allInputText(input: unknown[]): string { + return (input || []).map((it) => flatContent((it as { content?: unknown }).content)).join("\n"); +} +function readOracle(reqLog: string): OracleEntry[] { + if (!fs.existsSync(reqLog)) return []; + return fs.readFileSync(reqLog, "utf8").split("\n").filter((l) => l.trim()).map((l) => JSON.parse(l) as OracleEntry); +} + +function freePort(): Promise { + return new Promise((resolve) => { + const s = net.createServer(); + s.listen(0, "127.0.0.1", () => { + const p = (s.address() as net.AddressInfo).port; + s.close(() => resolve(p)); + }); + }); +} + +function windowEnv(contextWindow: number): Record { + return { BILI_LAUNCHER_MODEL_WINDOWS: JSON.stringify({ [MODEL]: contextWindow, [MODEL_B]: contextWindow, [MODEL_C]: contextWindow }) }; +} + +/** Shared assertions: the compression state survived the perturbation turns. + * A session fork or a state loss BOTH reshape the post-perturbation wire the + * same way: the pre-drift filler docs reappear raw (the new/blank session has + * no blocks) and the payload balloons until a re-fold lands — if it lands at + * all (a re-fold can even trip the #2147 dead-end breaker and fail the turn). + * So the oracle is the wire AFTER the perturbation: summary + sentinels + + * the drift marker present, pre-drift filler docs absent, payload bounded. */ +function assertStateSurvived(oracle: OracleEntry[], planted: number[], marker: string, droppedDoc: string, label: string): void { + const mains = oracle.filter((o) => !o.isSummary); + assert.ok(mains.length >= 2, `${label}: expected several forwarded requests`); + const lastMain = allInputText(mains[mains.length - 1].input); + assert.match(lastMain, /\[Compressed conversation section\]/, `${label}: the folded summary must still be on the wire after the perturbation`); + for (const s of planted) { + assert.ok(lastMain.includes(String(s)), `${label}: sentinel ${s} must survive into the post-perturbation payload`); + } + assert.ok(lastMain.includes(marker), `${label}: the post-perturbation turn's prompt must be present`); + assert.ok(!lastMain.includes(droppedDoc), `${label}: pre-drift filler (${droppedDoc}) must stay folded — its reappearance means the fold state was lost or re-folded from zero`); + const lens = mains.map((m) => m.inputLen); + // Loose absolute bound, not a cross-run ratio: at the 60k window the wire + // legitimately grows while unfolded fillers sit in the well (min≈61k early + // vs ≈16 0k late is fine). What this catches is the catastrophe the suite + // exists for — fold state lost → the ENTIRE raw history replays (~290k: + // 3×7 8k fillers + carrier + drifts) instead of the folded view. + assert.ok(Math.max(...lens) <= 220_000, `${label}: forwarded payload looks like a full raw replay, not a folded view (max=${Math.max(...lens)})`); +} + +/** Codex 0.147.0 fact: the persona carrier is byte-stable across every + * mid-session perturbation we ship today. On this wire the carrier shows up + * as the leading `developer` item of input (instructions is empty through + * the proxy), so that is what we measure. If a future codex drifts the + * persona mid-session (the #2250 upgrade shape), this fires and the persona + * migrate path (not the raw-key continuity) becomes the load-bearing half; + * the state-survival oracle above covers both. + * + * Two-phase contract (`preCount` = number of main requests before the + * perturbation): + * - pre-perturbation mains: exactly ONE unique carrier length; + * - post-perturbation mains: exactly ONE unique carrier length, allowed to + * differ from the pre value (the AGENTS.md edit is an intentional persona + * drift that the #2250 migrate path absorbs). + * In the URL lane the second drift turn is a PURE model switch (no file + * edit), so the post-phase uniqueness also pins the #2250 claim itself: + * switching the model alone must NOT move the persona carrier by a byte. */ +function assertPersonaCarrier(oracle: OracleEntry[], label: string): void { + // MAIN requests only: bili's own summarization calls carry bili's + // compressor persona instead. + const mains = oracle.filter((o) => !o.isSummary); + const carrierLen = (o: OracleEntry): number => { + const head = o.input?.[0]; + if (!head || typeof head !== "object" || (head as { role?: string }).role !== "developer") return -1; + return flatContent((head as { content?: unknown }).content).length; + }; + // Observed codex 0.147.0 facts this oracle must allow for: + // - the run's FIRST request (fresh `exec`, no resume) carries a shorter + // carrier than resumed turns (~+3.1k resume guidance) → skip row 0; + // - each MODEL FAMILY embeds its own guidance section in the carrier + // (qwen≈35k, gpt-5-codex≈56k, gpt-5.1≈77k) → group by model. + // What it pins: within one model, the carrier must be byte-stable across + // every turn — random prompt-assembly drift (the shape that would silently + // fork sessions if #2250 regressed) fires here with >1 unique length. + const groups = new Map(); + for (const o of mains.slice(1)) { + const len = carrierLen(o); + if (len < 0) continue; + const key = o.model ?? "?"; + const list = groups.get(key) ?? []; + list.push(len); + groups.set(key, list); + } + for (const [model, lens] of groups) { + assert.equal(new Set(lens).size, 1, `${label}: persona carrier drifted within model ${model} (lengths ${[...new Set(lens)].join(", ")}) — codex prompt assembly is not deterministic across turns, or bili mutated the carrier per-request; re-check the #2250 migrate path`); + } +} + +function waitFor(url: string, ms: number, label = "service"): Promise { + return new Promise((resolve, reject) => { + const started = Date.now(); + const poll = (): void => { + fetch(url).then((r) => (r.ok ? resolve() : retry())).catch(retry); + }; + const retry = (): void => { + if (Date.now() - started > ms) { reject(new Error(`${label} did not come up within ${ms}ms`)); return; } + setTimeout(poll, 250); + }; + poll(); + }); +} + +type UrlCtx = { + work: string; + codexCwd: string; + codexHome: string; + xdg: { config: string; cache: string; state: string }; + port: number; + fakePort: number; + reqLog: string; + fakePid?: number; + proxyPid?: number; + resumed: boolean; + turnCount: number; +}; + +async function startUrlCtx(contextWindow: number): Promise { + const work = fs.mkdtempSync(path.join(WORK_ROOT, "e2e-codex-persona-")); + const ctx: UrlCtx = { + work, + codexCwd: fs.mkdtempSync(path.join(CWD_ROOT, "url-cwd-")), + codexHome: path.join(work, "codex-home"), + xdg: { config: path.join(work, "xdg-config"), cache: path.join(work, "xdg-cache"), state: path.join(work, "xdg-state") }, + port: await freePort(), + fakePort: await freePort(), + reqLog: path.join(work, "fake-requests.jsonl"), + resumed: false, + turnCount: 0, + }; + for (const d of [ctx.codexHome, ctx.xdg.config, ctx.xdg.cache, ctx.xdg.state]) fs.mkdirSync(d, { recursive: true }); + + await assertPortDead(ctx.fakePort); + const fake = spawn(process.execPath, [FAKE_UPSTREAM], { + env: { ...process.env, FAKE_PORT: String(ctx.fakePort), FAKE_HOST: "127.0.0.1", FAKE_REQLOG: ctx.reqLog, FAKE_MODEL: MODEL }, + stdio: ["ignore", "ignore", "pipe"], + }); + ctx.fakePid = fake.pid; + await waitFor(`http://127.0.0.1:${ctx.fakePort}/v1/models`, 15_000); + + fs.writeFileSync(path.join(ctx.codexHome, "config.toml"), [ + `model = "${MODEL}"`, + 'model_provider = "e2e"', + "model_context_window = 60000", + "", + "[model_providers.e2e]", + 'name = "OpenAI"', + `base_url = "http://127.0.0.1:${ctx.port}/bili/http://127.0.0.1:${ctx.fakePort}/v1"`, + 'wire_api = "responses"', + 'env_key = "E2E_UPSTREAM_KEY"', + "", + ].join("\n")); + + await assertPortDead(ctx.port); + const logPath = path.join(work, "bili.log"); + const proxy = spawn(process.execPath, [DIST, "start", "--port", String(ctx.port), "--no-auto-update"], { + env: { + ...process.env, + XDG_CONFIG_HOME: ctx.xdg.config, + XDG_CACHE_HOME: ctx.xdg.cache, + XDG_STATE_HOME: ctx.xdg.state, + BILLION_CONTEXT_NO_AUTO_UPDATE: "1", + ...windowEnv(contextWindow), + }, + stdio: ["ignore", "ignore", "pipe"], + }); + ctx.proxyPid = proxy.pid; + proxy.stderr!.on("data", (c: Buffer) => { try { fs.appendFileSync(logPath, c); } catch { /* noop */ } }); + await waitFor(`http://127.0.0.1:${ctx.port}/__bili/health`, 30_000, "bili proxy"); + return ctx; +} + +function urlLogs(ctx: UrlCtx): string { + const parts: string[] = []; + const stateLog = path.join(ctx.xdg.state, "billion-context", "bili.log"); + if (fs.existsSync(stateLog)) parts.push(fs.readFileSync(stateLog, "utf8")); + try { parts.push(fs.readFileSync(path.join(ctx.work, "bili.log"), "utf8")); } catch { /* noop */ } + return parts.join(""); +} + +async function urlTurn(ctx: UrlCtx, prompt: string, extraArgs: string[] = []): Promise<{ code: number; last: string }> { + ctx.turnCount += 1; + const label = `t${ctx.turnCount}`; + const lastFile = path.join(ctx.work, `${label}.last`); + const args = ["exec", "--skip-git-repo-check", "--output-last-message", lastFile, ...extraArgs]; + if (ctx.resumed) args.push("resume", "--last"); + args.push(prompt); + return new Promise((resolve, reject) => { + const child = spawn(CODEX_BIN, args, { + cwd: ctx.codexCwd, + env: { ...process.env, CODEX_HOME: ctx.codexHome, E2E_UPSTREAM_KEY: "fake", RUST_LOG: "error" }, + stdio: ["ignore", "ignore", "pipe"], + }); + const timer = setTimeout(() => { + try { child.kill("SIGKILL"); } catch { /* noop */ } + reject(new Error(`turn ${label} timed out after ${TMO}ms`)); + }, TMO); + child.on("exit", (code) => { + clearTimeout(timer); + const last = fs.existsSync(lastFile) ? fs.readFileSync(lastFile, "utf8").trim() : ""; + ctx.resumed = true; + resolve({ code: code ?? -1, last }); + }); + }); +} + +test("URL mode: model switch + AGENTS.md edit mid-session keep the compression state (#2250)", { skip: skipReason, timeout: 600_000 }, async (t) => { + const leak = repoDocLeak(path.join(CWD_ROOT, "url-cwd-")); + assert.ok(!leak, `#815 precondition broken: ${leak} holds .git/AGENTS.md above the hermetic cwd`); + + // 60k window (codex's own default) with 1400-line loads: the fold + // equilibrium leaves ~14%W headroom, and the drift turn's fixed overhead + // (~4.3k resume/AGENTS) + slim bulk must fit inside it — at small windows + // (18k/24k) the well folds DRY and the drift turn dies structurally in + // #1841/#2147 preflight fail-fast instead of exercising identity. At 60k + // exactly one load turn folds and the well keeps unfolded fillers, so even + // if a drift turn does trigger preflight it finds material, never dead-ends. + const ctx = await startUrlCtx(60_000); + t.after(() => { + for (const pid of [ctx.proxyPid, ctx.fakePid]) { + if (pid) { try { process.kill(pid, "SIGKILL"); } catch { /* gone */ } } + } + }); + + const planted = [4781, 2903, 6577]; + const warm = await urlTurn(ctx, `档案摘要:\n${planted.map((s) => `本档案哨兵值 = ${s}`).join("\n")}\n\n请确认收到, 只回复: 收到#1`); + assert.equal(warm.code, 0, `warmup failed (code=${warm.code}); log:\n${urlLogs(ctx)}`); + + for (let k = 2; k <= 4; k += 1) { + const r = await urlTurn(ctx, `${filler(k, 1400)}\n\n请确认已读取档案#k, 只回复: 收到#${k}`); + assert.equal(r.code, 0, `load turn ${k} failed (code=${r.code}); log:\n${urlLogs(ctx)}`); + } + + const before = readOracle(ctx.reqLog); + assert.ok(before.some((o) => o.isSummary), "a real fold must have happened before the perturbation"); + + // The perturbation: a fresh AGENTS.md lands in the walk-up dir AND the + // model switches to a known family. Drift turns carry a SLIM bulk + // (~120 lines ≈ 1.6k tokens) so the window is not the thing under test. + fs.writeFileSync(path.join(ctx.codexCwd, "AGENTS.md"), "- persona-e2e rule ALPHA (added mid-session)\n"); + const drift1 = await urlTurn(ctx, `${filler(9, 120)}\n\n切换后第一轮: 只回复: 收到#5`, ["-c", `model=${MODEL_B}`]); + assert.equal(drift1.code, 0, `drift turn (model switch) failed (code=${drift1.code}); log:\n${urlLogs(ctx)}`); + + // PURE model switch (no file edit): the persona carrier must stay at the + // drift1 length — pinned by assertPersonaCarrier (per-model byte stability). + const drift2 = await urlTurn(ctx, `${filler(10, 120)}\n\n切换后第二轮: 只回复: 收到#6`, ["-c", `model=${MODEL_C}`]); + assert.equal(drift2.code, 0, `drift turn (known-family switch) failed (code=${drift2.code}); log:\n${urlLogs(ctx)}`); + + const oracle = readOracle(ctx.reqLog); + assertStateSurvived(oracle, planted, "收到#6", "doc#02 ", "URL mode"); + assertPersonaCarrier(oracle, "URL mode"); + const driftMains = oracle.filter((o) => !o.isSummary).slice(-2); + assert.deepEqual(driftMains.map((m) => m.model), [MODEL_B, MODEL_C], "both drift turns must actually carry the switched model on the wire (an ignored -c override would make this suite hollow)"); + // The fake acks the FIRST `收到#N` it sees in the joined user text, and at + // the 60k window earlier turns legitimately survive unfolded — so assert a + // real round-trip landed, not a specific N (the wire-side assertions above + // already prove THIS turn reached the switched model). + assert.match(drift2.last, /^收到#\d+$/, "the fake upstream must have answered the post-switch turn"); +}); + +type LaunchCtx = { + work: string; + codexCwd: string; + codexHome: string; + xdg: { config: string; cache: string; state: string }; + fakePort: number; + reqLog: string; + fakePid?: number; + resumed: boolean; + turnCount: number; +}; + +async function startLaunchCtx(contextWindow: number): Promise { + const work = fs.mkdtempSync(path.join(WORK_ROOT, "e2e-codex-persona-launch-")); + const ctx: LaunchCtx = { + work, + codexCwd: fs.mkdtempSync(path.join(CWD_ROOT, "launch-cwd-")), + codexHome: path.join(work, "codex-home"), + xdg: { config: path.join(work, "xdg-config"), cache: path.join(work, "xdg-cache"), state: path.join(work, "xdg-state") }, + fakePort: await freePort(), + reqLog: path.join(work, "fake-requests.jsonl"), + resumed: false, + turnCount: 0, + }; + for (const d of [ctx.codexHome, ctx.xdg.config, ctx.xdg.cache, ctx.xdg.state]) fs.mkdirSync(d, { recursive: true }); + + await assertPortDead(ctx.fakePort); + const fake = spawn(process.execPath, [FAKE_UPSTREAM], { + env: { ...process.env, FAKE_PORT: String(ctx.fakePort), FAKE_HOST: "127.0.0.1", FAKE_REQLOG: ctx.reqLog, FAKE_MODEL: MODEL }, + stdio: ["ignore", "ignore", "pipe"], + }); + ctx.fakePid = fake.pid; + await waitFor(`http://127.0.0.1:${ctx.fakePort}/v1/models`, 15_000); + + // Launcher mode: PLAIN provider base_url — no /bili/ prefix; `bili codex` + // owns the routing via its overlay (#2197 contract). The window must ride + // codex's OWN config (model_context_window): the wrapper forwards what + // codex perceives, not our env map (#321 PR-D), so 60000 here would keep + // the proxy at 60k and never fold. + fs.writeFileSync(path.join(ctx.codexHome, "config.toml"), [ + `model = "${MODEL}"`, + 'model_provider = "e2e"', + `model_context_window = ${contextWindow}`, + "", + "[model_providers.e2e]", + 'name = "OpenAI"', + `base_url = "http://127.0.0.1:${ctx.fakePort}/v1"`, + 'wire_api = "responses"', + 'env_key = "E2E_UPSTREAM_KEY"', + "", + ].join("\n")); + return ctx; +} + +async function launchTurn(ctx: LaunchCtx, prompt: string, extraArgs: string[] = []): Promise<{ code: number; last: string; launcherOut: string }> { + ctx.turnCount += 1; + const label = `t${ctx.turnCount}`; + const lastFile = path.join(ctx.work, `${label}.last`); + const args = ["exec", "--skip-git-repo-check", "--output-last-message", lastFile, ...extraArgs]; + if (ctx.resumed) args.push("resume", "--last"); + args.push(prompt); + + const childEnv: NodeJS.ProcessEnv = { ...process.env }; + for (const k of [ + "BILLION_CONTEXT_PROXY", "BILI_PROVIDER_REWRITES", "BILI_MITM_HOSTS", "BILI_MCP_PROXY", + "BILI_NATIVE_CLAUDE", "BILLION_CONTEXT_PLUGIN", "BILI_ZONE_PORT", "BILI_CLAUDE_NATIVE_PORT", + "BILI_UPSTREAM_PROXY", "ACP_PORT", "SSL_CERT_FILE", + "HTTP_PROXY", "HTTPS_PROXY", "ALL_PROXY", "NO_PROXY", + "http_proxy", "https_proxy", "all_proxy", "no_proxy", + ]) delete childEnv[k]; + const launcherLog = path.join(ctx.work, `${label}.launcher.log`); + const child = spawn(process.execPath, [DIST, "codex", ...args], { + cwd: ctx.codexCwd, + env: { + ...childEnv, + CODEX_HOME: ctx.codexHome, + XDG_CONFIG_HOME: ctx.xdg.config, + XDG_CACHE_HOME: ctx.xdg.cache, + XDG_STATE_HOME: ctx.xdg.state, + BILLION_CONTEXT_NO_AUTO_UPDATE: "1", + BILI_CLIENT_BIN: CODEX_BIN, + E2E_UPSTREAM_KEY: "fake", + RUST_LOG: "error", + ...windowEnv(60_000), + }, + stdio: ["ignore", "ignore", "pipe"], + }); + child.stderr!.on("data", (c: Buffer) => { try { fs.appendFileSync(launcherLog, c); } catch { /* noop */ } }); + const code = await new Promise((resolve, reject) => { + const timer = setTimeout(() => { try { child.kill("SIGKILL"); } catch { /* noop */ } reject(new Error(`bili codex exec timed out (${label})`)); }, TMO * 2); + child.on("exit", (c) => { clearTimeout(timer); resolve(c ?? -1); }); + }); + ctx.resumed = true; + return { + code, + last: fs.existsSync(lastFile) ? fs.readFileSync(lastFile, "utf8").trim() : "", + launcherOut: fs.existsSync(launcherLog) ? fs.readFileSync(launcherLog, "utf8") : "", + }; +} + +test("launcher mode: model switch + AGENTS.md edit across sequential bili codex runs keep the compression state (#2250)", { skip: skipReason, timeout: 600_000 }, async (t) => { + const leak = repoDocLeak(path.join(CWD_ROOT, "launch-cwd-")); + assert.ok(!leak, `#815 precondition broken: ${leak} holds .git/AGENTS.md above the hermetic cwd`); + + const ctx = await startLaunchCtx(60_000); + t.after(() => { + if (ctx.fakePid) { try { process.kill(ctx.fakePid, "SIGKILL"); } catch { /* gone */ } } + }); + + const planted = [4781, 2903, 6577]; + const warm = await launchTurn(ctx, `档案摘要:\n${planted.map((s) => `本档案哨兵值 = ${s}`).join("\n")}\n\n请确认收到, 只回复: 收到#1`); + assert.equal(warm.code, 0, `warmup failed (code=${warm.code})\nlauncher stderr:\n${warm.launcherOut.slice(-4000)}`); + + for (let k = 2; k <= 4; k += 1) { + const r = await launchTurn(ctx, `${filler(k, 1400)}\n\n请确认已读取档案#k, 只回复: 收到#${k}`); + assert.equal(r.code, 0, `load turn ${k} failed (code=${r.code})\nlauncher stderr:\n${r.launcherOut.slice(-4000)}`); + } + + const before = readOracle(ctx.reqLog); + assert.ok(before.some((o) => o.isSummary), "a real fold must have happened before the perturbation"); + // #2197: the launcher must actually own the routing (no /bili/ in config). + assert.ok( + before.some((o) => !o.isSummary && allInputText(o.input).includes("\x3cacp ")), + "no non-summary upstream request carries bili's ACP tags — the provider bypassed the proxy", + ); + + fs.writeFileSync(path.join(ctx.codexCwd, "AGENTS.md"), "- persona-e2e launch rule ALPHA (added mid-session)\n"); + const drift = await launchTurn(ctx, `${filler(9, 120)}\n\n切换后第一轮: 只回复: 收到#5`, ["-c", `model=${MODEL_B}`]); + assert.equal(drift.code, 0, `drift turn failed (code=${drift.code})\nlauncher stderr:\n${drift.launcherOut.slice(-4000)}`); + + const oracle = readOracle(ctx.reqLog); + assertStateSurvived(oracle, planted, "收到#5", "doc#02 ", "launcher mode"); + assertPersonaCarrier(oracle, "launcher mode"); + const driftMain = oracle.filter((o) => !o.isSummary).slice(-1)[0]; + assert.equal(driftMain.model, MODEL_B, "the drift turn must actually carry the switched model on the wire (an ignored -c override would make this suite hollow)"); + assert.match(drift.last, /^收到#\d+$/, "the fake upstream must have answered the post-switch turn"); +}); diff --git a/tests/e2e/fake-upstream.mjs b/tests/e2e/fake-upstream.mjs index d733e3277f..cf4998e0b2 100644 --- a/tests/e2e/fake-upstream.mjs +++ b/tests/e2e/fake-upstream.mjs @@ -124,7 +124,8 @@ const server = http.createServer((req, res) => { if (summary) text = buildFakeSummary(allInputText(parsed.input)); else text = answerFor(lastUserText(parsed.input)); try { - fs.appendFileSync(REQLOG, JSON.stringify({ t: Date.now(), model: parsed.model, stream: !!parsed.stream, isSummary: summary, inputLen: raw.length, input: parsed.input }) + "\n"); + const ins = typeof parsed.instructions === "string" ? parsed.instructions : ""; + fs.appendFileSync(REQLOG, JSON.stringify({ t: Date.now(), model: parsed.model, stream: !!parsed.stream, isSummary: summary, inputLen: raw.length, insLen: ins.length, input: parsed.input }) + "\n"); } catch { /* noop */ } const inTok = estIn(raw); if (parsed.stream) sse(res, messageEvents(text, inTok)); diff --git a/tests/launcher.test.ts b/tests/launcher.test.ts index bae20a2165..54a4958017 100644 --- a/tests/launcher.test.ts +++ b/tests/launcher.test.ts @@ -2780,6 +2780,65 @@ test("buildCodexArgs: #2260(D) last-wins holds across `--` — a user -c before ]); }); +test("buildCodexArgs: #2289 resume — rewrites insert BEFORE the `resume` subcommand so the user's -c model=… survives codex's exec-resume parsing", () => { + const rewrites: HttpRewrite[] = [ + { key: "model_providers.x.base_url", realUpstream: "http://up.local/v1" }, + ]; + const out = buildCodexArgs( + "http://h:p", + rewrites, + [], + ["exec", "--skip-git-repo-check", "-c", "model=gpt-5-codex", "resume", "--last", "turn two prompt"], + ); + // rewrites sit ahead of `resume` — a -c AFTER the resume subcommand makes + // codex-cli 0.147.0 drop the earlier `-c model=…` on exec resume (bisected + // against the real binary: [-c model=B, resume, --last, -c provider…, prompt] + // still loses model=B; see issue #2289). + assert.deepEqual(out, [ + "exec", "--skip-git-repo-check", "-c", "model=gpt-5-codex", + "-c", `model_providers.x.base_url=${wrapUpstream("http://h:p", "http://up.local/v1")}`, + "resume", "--last", "turn two prompt", + ]); +}); + +test("buildCodexArgs: #2289 never splits a value-taking flag from its value — trailing `-c ` keeps the legacy append", () => { + const rewrites: HttpRewrite[] = [ + { key: "model_providers.x.base_url", realUpstream: "http://up.local/v1" }, + ]; + const out = buildCodexArgs( + "http://h:p", + rewrites, + [], + ["exec", "-c", "model=gpt-5-codex"], + ); + assert.deepEqual(out, [ + "exec", "-c", "model=gpt-5-codex", + "-c", `model_providers.x.base_url=${wrapUpstream("http://h:p", "http://up.local/v1")}`, + ]); +}); + +test("buildCodexArgs: #2289 flags-only tail and unknown-flag tails keep the append behavior", () => { + const rewrites: HttpRewrite[] = [ + { key: "model_providers.x.base_url", realUpstream: "http://up.local/v1" }, + ]; + assert.deepEqual( + buildCodexArgs("http://h:p", rewrites, [], ["exec", "--some-unknown-flag", "maybe-a-value"]).slice(-2), + ["-c", `model_providers.x.base_url=${wrapUpstream("http://h:p", "http://up.local/v1")}`], + ); +}); + +test("buildCodexArgs: #2289 `resume` with no trailing prompt still anchors the rewrites before it", () => { + const rewrites: HttpRewrite[] = [ + { key: "model_providers.x.base_url", realUpstream: "http://up.local/v1" }, + ]; + assert.deepEqual( + buildCodexArgs("http://h:p", rewrites, [], ["exec", "resume", "--last"]), + ["exec", + "-c", `model_providers.x.base_url=${wrapUpstream("http://h:p", "http://up.local/v1")}`, + "resume", "--last"], + ); +}); + test("buildClaudeEnv: ANTHROPIC_BASE_URL rewrite sets env + keeps HTTPS_PROXY/CA", () => { const rewrites: HttpRewrite[] = [ { key: "ANTHROPIC_BASE_URL", realUpstream: "http://relay.local/anthropic" }, From cdcc1fb60a83649ac93870ca17f3e582ee96bdbb Mon Sep 17 00:00:00 2001 From: ework-agent Date: Wed, 7 Oct 2026 00:45:37 +0800 Subject: [PATCH 2/3] docs(#2281): correct issue refs from nonexistent #2289 to #2281 The launcher -c insertion fix and its tests cited #2289, which does not exist; the bug is tracked under #2281 (the issue this PR closes). Comment- and-test-name-only change, no behavior delta. --- src/launcher.ts | 2 +- tests/launcher.test.ts | 10 +++++----- 2 files changed, 6 insertions(+), 6 deletions(-) diff --git a/src/launcher.ts b/src/launcher.ts index da4ec2a677..81bc3da869 100644 --- a/src/launcher.ts +++ b/src/launcher.ts @@ -1002,7 +1002,7 @@ const CODEX_BOOLEAN_FLAGS = new Set([ * rewrites must land BEFORE it (appending turned them into prompt text and * silently bypassed the proxy). * - * #2289: codex-cli 0.147.0 parses `exec resume` such that a `-c` flag placed + * #2281: codex-cli 0.147.0 parses `exec resume` such that a `-c` flag placed * AFTER the positional prompt silently DROPS an earlier `-c model=…` on the * same command line (bisected 2026-10-07: resume + trailing `-c` loses the * override; fresh exec, or resume without a trailing `-c`, both keep it). diff --git a/tests/launcher.test.ts b/tests/launcher.test.ts index 54a4958017..2509546c69 100644 --- a/tests/launcher.test.ts +++ b/tests/launcher.test.ts @@ -2780,7 +2780,7 @@ test("buildCodexArgs: #2260(D) last-wins holds across `--` — a user -c before ]); }); -test("buildCodexArgs: #2289 resume — rewrites insert BEFORE the `resume` subcommand so the user's -c model=… survives codex's exec-resume parsing", () => { +test("buildCodexArgs: #2281 resume — rewrites insert BEFORE the `resume` subcommand so the user's -c model=… survives codex's exec-resume parsing", () => { const rewrites: HttpRewrite[] = [ { key: "model_providers.x.base_url", realUpstream: "http://up.local/v1" }, ]; @@ -2793,7 +2793,7 @@ test("buildCodexArgs: #2289 resume — rewrites insert BEFORE the `resume` subco // rewrites sit ahead of `resume` — a -c AFTER the resume subcommand makes // codex-cli 0.147.0 drop the earlier `-c model=…` on exec resume (bisected // against the real binary: [-c model=B, resume, --last, -c provider…, prompt] - // still loses model=B; see issue #2289). + // still loses model=B; see issue #2281). assert.deepEqual(out, [ "exec", "--skip-git-repo-check", "-c", "model=gpt-5-codex", "-c", `model_providers.x.base_url=${wrapUpstream("http://h:p", "http://up.local/v1")}`, @@ -2801,7 +2801,7 @@ test("buildCodexArgs: #2289 resume — rewrites insert BEFORE the `resume` subco ]); }); -test("buildCodexArgs: #2289 never splits a value-taking flag from its value — trailing `-c ` keeps the legacy append", () => { +test("buildCodexArgs: #2281 never splits a value-taking flag from its value — trailing `-c ` keeps the legacy append", () => { const rewrites: HttpRewrite[] = [ { key: "model_providers.x.base_url", realUpstream: "http://up.local/v1" }, ]; @@ -2817,7 +2817,7 @@ test("buildCodexArgs: #2289 never splits a value-taking flag from its value — ]); }); -test("buildCodexArgs: #2289 flags-only tail and unknown-flag tails keep the append behavior", () => { +test("buildCodexArgs: #2281 flags-only tail and unknown-flag tails keep the append behavior", () => { const rewrites: HttpRewrite[] = [ { key: "model_providers.x.base_url", realUpstream: "http://up.local/v1" }, ]; @@ -2827,7 +2827,7 @@ test("buildCodexArgs: #2289 flags-only tail and unknown-flag tails keep the appe ); }); -test("buildCodexArgs: #2289 `resume` with no trailing prompt still anchors the rewrites before it", () => { +test("buildCodexArgs: #2281 `resume` with no trailing prompt still anchors the rewrites before it", () => { const rewrites: HttpRewrite[] = [ { key: "model_providers.x.base_url", realUpstream: "http://up.local/v1" }, ]; From 126f03c0ca9cf8c57f75e283266160966bbaa76e Mon Sep 17 00:00:00 2001 From: ework-agent Date: Wed, 7 Oct 2026 00:58:19 +0800 Subject: [PATCH 3/3] fix(ci): nest if-no-files-found under with/ so ci-e2e-codex-persona dispatches (#2281) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit The new persona lane's upload-artifact step declared 'if-no-files-found' as a STEP-level key (sibling of uses/with) instead of an input under with/. That is not a valid Actions step property, so the workflow failed schema validation on every push and dispatched ZERO jobs (conclusion=failure, no steps ran) — the lane never actually executed even though it was green-absent from required checks. Move it under with/ (a documented upload-artifact input). No test or code change. --- .github/workflows/ci-e2e-codex-persona.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.github/workflows/ci-e2e-codex-persona.yml b/.github/workflows/ci-e2e-codex-persona.yml index 31852122ca..7091e192fa 100644 --- a/.github/workflows/ci-e2e-codex-persona.yml +++ b/.github/workflows/ci-e2e-codex-persona.yml @@ -55,4 +55,4 @@ jobs: with: name: e2e-codex-persona-artifacts path: tmp/e2e-artifacts - if-no-files-found: ignore + if-no-files-found: ignore