Skip to content

Model: qwen3.8-27b — feat: codex compression-budget coordination (PR-D/E1/C/E2) - #326

Closed
ranxianglei wants to merge 1 commit into
masterfrom
2026-08-28_codex-compact-coordination
Closed

ranxianglei wants to merge 1 commit into
masterfrom
2026-08-28_codex-compact-coordination

Conversation

@ranxianglei

Copy link
Copy Markdown
Owner

Model: qwen3.8-27b

Closes #320.

What changed

PR-D — launcher budget coordination. bili codex now injects -c model_context_window=<N> and -c model_auto_compact_token_limit=<N> (same value; codex internally clamps the compact limit to min(configured, 90% x window)). N comes from the same window-resolution chain the server uses — resolveStaticWindow(model, host, routes, upstream, launcherWindows) = launcher windows > registry peek > per-route config > model table — so the launcher and the proxy can never disagree. The value is additionally clamped to codex's own model table (codexModelWindow) so bili never tells codex a window bigger than codex assumes. Logged as [bili] codex budget: model=... window=... (source=...).

PR-E1 — anonymous-mode window clamp. Bundled codex model-table snapshot (src/codex-models-snapshot.json, 10 models, generated from openai/codex codex-rs/models-manager/models.json via npm run codex:snapshot — same refresh pattern as the models.dev registry snapshot). When the request carries the codex client header (x-codex-turn-metadata) and the model is in the table, the effective window clamps to min(bili window, codex table) (applyCodexWindowClamp). Model not in table → codex has no knowledge → no clamp.

PR-C — semantic rebasing gate. The native-compaction boundary is now marked only on a confirmed success: SSE streams are observed for the terminal event (response.completed → mark; response.failed/response.incomplete → no mark; stream end without terminal → unknown → no mark), non-SSE JSON bodies are buffered and parsed (2xx + parseable → mark), and empty-body 2xx no longer marks (previously any 2xx did — the #249 bug). Trigger-final requests now take a read-only projection: sanitized original input, no kernel state mutation, no compress prompt/tools, no nudge, no preflight.

PR-E2 — conditional interception + forge (kill-switch BILI_CODEX_COMPACT=intercept, default pass). When the switch is on, the client is codex, the request is a compaction request (trailing compaction_trigger or /responses/compact), and ACP state is healthy (steady-state usage below the window AND compression state exists), bili answers locally instead of forwarding: V2 → SSE with exactly one compaction output item + response.completed; V1 → JSON {output:[compaction item]} (contract verified against codex's collect_compaction_output / ResponseCompleted parsers). The ACP summary is recorded under the forged item id (bounded map) and replayed as a developer message on the next request — deterministic handoff, no boundary heuristics. Unhealthy ACP (or non-codex, or switch off) passes through to native compaction, where PR-C gates the rebase correctly.

Pre-flight

  • Full test suite: 763/763 pass (740 pre-existing + 23 new in tests/codex-compact-coordination.test.ts: 12 unit + 11 e2e covering kill-switch pass/intercept, V2+V1 forge, unhealthy/over-limit/non-codex pass-through, PR-C completed/failed/incomplete/truncated/empty-body, PR-E1 clamp vs control).
  • npm run typecheck clean; npm run build clean (snapshot JSON inlined into dist).
  • New e2e tests use in-body server close (proxy first) — the t.after + closeAllConnections pattern deadlocked against the proxy's pooled upstream connections.

Notes

  • E2 real-machine validation (local sglang responses endpoint + codex custom provider) is the follow-up before flipping the kill-switch default to intercept — mock-upstream validation is in this PR.
  • Codex Feature::TokenBudget (UnderDevelopment, default off) tracked as an observation item per the issue.

PR-D: bili codex launcher injects -c model_context_window /
model_auto_compact_token_limit from bili's effective window (single
source of truth shared with the server pipeline), so codex's
auto-compact ledger aligns with bili and ACP fires first.

PR-E1: bundled codex model-table snapshot (scripts/update-codex-models-snapshot.mjs,
same refresh pattern as registry-snapshot); when the client is codex
(x-codex-turn-metadata header) and the model is in the table, the
effective window clamps to min(bili window, codex table).

PR-C: semantic rebasing gate — the native-compaction boundary is now
marked only on a confirmed response.completed (SSE observer / JSON
parse); failed/incomplete/truncated/empty-body outcomes leave ACP
state untouched. Trigger-final requests take a read-only projection
(no kernel state mutation, no injections, no preflight).

PR-E2: conditional interception + forge (kill-switch
BILI_CODEX_COMPACT=intercept, default pass). When ACP state is
healthy, codex compaction requests (trailing compaction_trigger or
/responses/compact) are answered locally: V2 SSE with a single
compaction item + response.completed, or V1 JSON {output:[compaction]}.
The ACP summary is recorded by forged item id and replayed as a
developer message on the next request, giving a deterministic handoff.
Unhealthy ACP passes through to native compaction (PR-C gates the
rebase correctly).

Tests: tests/codex-compact-coordination.test.ts (12 unit + 11 e2e).
Full suite 763/763, typecheck + build clean.
@github-actions

Copy link
Copy Markdown
Contributor

📦 Built Package Artifact

Branch: 2026-08-28_codex-compact-coordination (e59495a)

Option A — Install from npm PR tag (recommended)

npm install -g billion-context@pr-326

Each push to this PR publishes a new version under the pr-326 npm tag.

Option B — Download artifact

  1. Download the artifact from the Actions run
  2. Extract the tarball and install:
tar xzf billion-context-pr326.tgz
npm install -g package

This comment is automatically updated on each push.

@ranxianglei

Copy link
Copy Markdown
Owner Author

🤖 ework agent · vllm-qwen/qwen3.8-27b

[bot] 收到。PR #326(分支 2026-08-28_codex-compact-coordination,单 commit e59495a,基于 master 744e658)已存在且 open,9 文件 +1018/−35。现在在本地独立复验:checkout 分支 → npm run typecheck + 全量 npm test + npm run build,并对照 #320 验收标准逐条审查代码(PR-D 注入链 / E1 快照与 min() 钳制 / C 四类终态门 / E2 两态 kill-switch 与伪造 contract)。结果稍后更新在本 issue。

@ranxianglei

Copy link
Copy Markdown
Owner Author

🤖 ework agent · vllm-qwen/qwen3.8-27b

[bot] Independent verification of PR #326 (branch 2026-08-28_codex-compact-coordination, head e59495a, base master/744e658). I re-ran the full pre-flight locally, reviewed the diff against #320's acceptance criteria, and verified the forged-compaction wire contract against the actual codex source. Details below.

Pre-flight (re-run by me, all pass)

  • npm run typecheck — clean
  • npm test — 763/763 pass (740 pre-existing + 23 new)
  • npm run build — clean (snapshot JSON inlined into dist/index.js)

Code review — all four PRs meet #320 acceptance

  • PR-D (launcher budget): codexBudgetArgs (src/launcher.ts) resolves the window via the shared resolveStaticWindow chain (launcher > registry > config > table) and clamps to min(bili, codexModelWindow) so bili never tells codex a bigger window than codex assumes. Injects model_context_window + model_auto_compact_token_limit (same value — codex clamps the compact limit to 90% internally). Verified the min/override/out-of-table paths.
  • PR-E1 (window clamp): applyCodexWindowClamp (src/server.ts:2621) keys on x-codex-turn-metadata + in-table model, clamps to min(bili, codex table), no clamp when the model is unknown to codex. Called after the async registry upgrade, before resolveRequestConfig. Verified in-table/user-override/out-of-table/non-codex/native-missing.
  • PR-C (semantic rebasing gate): boundary marked only on a confirmed terminal — SSE observed for response.completed (mark) vs response.failed/response.incomplete/truncated (no mark); empty-body 2xx no longer marks (the Fix Codex Responses session continuity #249 bug); non-SSE JSON buffered + parsed. Trigger-final requests take a read-only projection (sanitized input, no kernel mutation, no compress prompt/tools/nudge, skipPreflight). Verified all five terminal states.
  • PR-E2 (conditional interception + forge): kill-switch BILI_CODEX_COMPACT (default pass), gates on codex client + compaction request + healthy ACP (steady usage < window AND compression state exists). Forges V2 SSE (one compaction output item + response.completed) / V1 JSON, records the summary under the forged item id (bounded map), resets ACP state, and replays as a developer message on the next request via rewriteForgedCompactions. Unhealthy/non-codex/switch-off pass through to native compaction (where PR-C gates correctly).

Codex wire contract — verified against source (/tmp/codex-src)

I checked the forged format against codex's actual parsers:

  • collect_compaction_output (core/src/compact_remote_v2.rs:419) requires response.completed and exactly one Compaction output item — the forged stream emits exactly one response.output_item.done + response.completed. ✓
  • ResponseItem::Compaction (protocol/src/models.rs:1190) = {id?, encrypted_content: string} — matches the forged item. ✓
  • ResponseCompleted (codex-api/src/sse/responses.rs:118) requires id + optional usage{input_tokens,output_tokens,total_tokens} — the forged usage:{1,0,1} parses and resets codex's token ledger to ~0 (the intended "ledger 归零"). ✓
  • build_v2_compacted_history appends the compaction item to the tail of the new history, so it rides in the next request's input with its id preserved → bili's rewriteForgedCompactions (src/server.ts:2638) matches on type==="compaction" && id and swaps it for the developer message before forwarding. The plaintext summary never reaches the upstream. ✓
  • The replayed developer message uses content: <string> (src/server.ts:2646) — valid per the OpenAI Responses spec for message input items; it's sent to the upstream, not parsed by codex.

Tests — 23 total, comprehensive

Coverage maps cleanly to #320: kill-switch pass/intercept, V2+V1 forge, unhealthy/over-limit/non-codex pass-through, PR-C's five terminal states, and E1 clamp-vs-control. The e2e forge test simulates codex's next turn by sending the compaction item in the input (mock-level, matching the PR's stated scope — real codex serialization is the real-machine follow-up).

  • Minor nit: the PR body says "12 unit + 11 e2e" — it's actually 11 unit + 12 e2e (= 23). Cosmetic only.

CI status — 5/6 green, 1 flaky cell

  • ✅ test ubuntu/22, ubuntu/24, windows/24, version-guard, build-artifact
  • ❌ test (windows-latest, 22) — but this is a known-flaky cell, not a regression: it also failed on master at 946b00b (release v0.1.62) and passes on the surrounding master commits (744e658, f1df8f9, dd4be03, 7b6fd75). The likely culprit is one of the new e2e tests' HTTP-server close pattern (the PR notes already flag that t.after+closeAllConnections deadlocked against pooled upstream connections; the in-body close workaround is the platform-sensitive part). I couldn't pull the job log or trigger a re-run (this bot's token lacks actions log/rerun scope — both endpoints 404).

Recommendation

  1. Re-run the test (windows-latest, 22) check to confirm it's a flake (I can't — token scope). Given it failed on master previously, a green re-run is expected and sufficient to clear the PR.
  2. If it reproduces, the new e2e tests' server-close pattern is the place to harden for Windows/Node-22 (e.g. explicit closeAllConnections()/socket destroy on the upstream before close(), or a bounded close timeout).
  3. The real-machine E2 validation (local sglang responses endpoint + codex custom provider) remains the documented follow-up before flipping the kill-switch default to intercept — mock-upstream validation is in this PR.

Bottom line: the code, contract, and tests are solid and meet #320's acceptance criteria. The only open item is the flaky windows/22 CI cell, which is pre-existing on master and should clear on re-run. I won't merge (human-only per AGENTS.md) — please re-run the check and merge when green.

ranxianglei added a commit that referenced this pull request Nov 11, 2026
- custom type acp-nudge (was bili-nudge); compact one-liner
  [ACP nudge] <pct>% · T<tier>[ EMERGENCY][ · top range a–b]
- the compact record line is ALWAYS on (no debug config needed):
  one per user turn, persistent in the session file, pi-visible /
  model-invisible (type:custom never enters LLM context)
- the verbatim wire text becomes a transient terminal notification
  (debug switch on: ACP_DEBUG / config debug), shown once per turn
  after the model answered — never persisted
- dedup per (session, turn) via a stable key of the last text user
  message (anthropic tool_result user messages skipped)
- bus events carry the record metadata (pct/tier/emergency/topRange/
  turnKey); emitters compute it from the PRE-nudge array
ranxianglei added a commit that referenced this pull request Nov 11, 2026
…otify only

Per owner review: the always-on compact record line (acp-nudge CustomEntry,
billion-context-pi #326 parity) is unnecessary — the transient verbatim
terminal notification (debug switch on) is the whole point of this feature.

- src/agent/pi.ts: no more registerEntryRenderer/appendEntry wiring; one
  after_provider_response hook shows the verbatim wire text once per user
  turn via ctx.ui.notify (consumes+clears the pending event, turnKey dedup)
- src/nudge-debug.ts: drop ACP_NUDGE_CUSTOM_TYPE + formatNudgeRecord
- tests: wiring test asserts the pi host carries exactly one extra
  after_provider_response subscription vs omp (the #3248 ack channel
  subscribes both lanes); formatNudgeRecord test removed
- docs en+zh, architecture.md: transient notify only, never persisted
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

REQ: codex 压缩预算协调 (launcher -c 注入) + 匿名代理拦截伪造 + 语义重基门 (PR-C/D/E)

1 participant