Skip to content

fix: classify opencode v2 title-gen by host-declared intent, not token budget (#1699) - #1703

Merged
ranxianglei merged 3 commits into
masterfrom
2026-09-29_opencode-v2-titlegen-intent
Sep 30, 2026
Merged

ranxianglei merged 3 commits into
masterfrom
2026-09-29_opencode-v2-titlegen-intent

Conversation

@ranxianglei

@ranxianglei ranxianglei commented Sep 29, 2026 •

Copy link
Copy Markdown
Owner

Closes #1699

Problem

opencode v2 sends title-generation requests with no max_tokens (options {} for kind === "title"). bili's isTitleGen / side-request heuristic defaults a missing output budget to 8192, so those requests were misclassified as normal main turns and got the full ~11 KiB compress philosophy system prompt + ACP compress tool injected into the title model → garbage session titles (an English chat sentence, or an empty first line).

Root cause

The side-request classification keys off the token budget — exactly the field opencode v2 omits for title-gen. The intent ("this is a side/title request") exists on the host but was never forwarded to the proxy.

Fix (intent-based, inert-safe)

Forward the host's per-request persona id on a new internal plugin-protocol header, and treat a known side-request agent as a side request by intent:

  • src/agent/opencode-v2.ts — the opencode v2 plugin stamps x-bili-plugin-agent: <persona> from its own request scope (the shared createOpencodeV2Setup covers both launcher and native V2 modes).
  • src/plugin.ts — new PLUGIN_REQUEST_AGENT_HEADER constant + pluginRequestAgentHeader() reader, gated on plugin announcement so a plain client cannot declare a side request by name to dodge compression (same gate as the window/model headers).
  • src/server/side-request.ts — isSideRequest(parsed, requestAgent?): a known side agent (SIDE_REQUEST_AGENTS = {"title"}) is a side request regardless of budget. Main personas (build/plan/general) are deliberately absent, so real turns keep compression. An unknown persona id is inert (falls back to the budget heuristic).
  • src/server.ts — the existing verbatim side-request gate passes the intent through; such requests forward verbatim (no injection, kernel state untouched), consistent with how tiny-budget title-gen already routed.

This reuses bili's existing title-gen routing (verbatim passthrough) rather than inventing a new mechanism, and is inert-safe: when the header is absent (older hosts, all other clients) behavior is byte-for-byte unchanged.

Behavior change (intended)

An opencode v2 title-gen request without max_tokens now routes verbatim (no compress prompt/tool injected) instead of being treated as a main turn. Old: full ~11 KiB philosophy prompt + tool injected. New: nothing injected. Only requests carrying x-bili-plugin-agent: title are affected; every other request is untouched.

Config surface

None. The new item is an internal plugin↔proxy protocol header (alongside the existing x-bili-plugin-model / -context-window / -max-output), not a user-facing config field, env var, or CLI flag. No config-schema change.

Verification (rebased onto current master)

Review follow-up (2026-09-29, post-review rebase)

  • Rebased onto current master (8293a542) — clean, no conflicts; branch is now linear (supersedes the interim "Update branch" merge commit, tree-identical apart from the fix below).
  • Drive-by commit (pre-existing master breakage found during review): master's CI is currently RED — tests/issue1086-chain-self-exemption.test.ts:728 calls bare rmSync, which the test: teardown rmSync ENOTEMPTY race — most test cleanups lack retry options (CI ubuntu-latest Node 22 flake) #1646/test: harden all recursive rmSync teardowns with retry options #1655 teardown hardening removed from the file's imports when converting teardowns to the retry-hardened rmrf helper (tests/tmp-rm.ts); every other teardown in the file already uses rmrf, this one was missed. Deterministic ReferenceError, so every PR merge-ref run (and master itself) fails here. Fixed with the same rmrf(root) helper — no import change needed. Labeled as its own commit.
  • Re-verified on the rebased head: npm run typecheck clean · npm run build success (dist-import-annotations OK) · full suite 3273 tests / 3261 pass / 0 fail / 12 skip.

@github-actions

github-actions Bot commented Sep 29, 2026 •

Copy link
Copy Markdown
Contributor

📦 Built Package Artifact

Branch: 2026-09-29_opencode-v2-titlegen-intent (29d302b)

Option A — Install this exact build (recommended)

npm install -g billion-context@0.0.1652-pr.1703.0d0acf5

This exact version is immutable and reproducible. The rolling pr tag tracks the newest test build across all PRs.

Option B — Download artifact

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

Install instructions are refreshed on each push.

ework-agent added 2 commits September 29, 2026 22:43
…n budget (#1699)

opencode v2 sends title-gen requests with NO max_tokens (options {} for
kind==="title"), so bili's isTitleGen / side-request heuristic — which defaults
a missing output budget to 8192 — misclassified them as main turns and injected
the full ~11KiB compress philosophy prompt + ACP compress tool into the title
model. Result: garbage session titles (English chat sentence / empty first line).

Forward the host's per-request persona id on a new x-bili-plugin-agent header
(stamped by the opencode v2 plugin from its own request scope) and treat a known
side-request agent ("title") as a side request BY INTENT, routing it verbatim
(no injection, kernel state untouched) regardless of output budget. Main personas
(build/plan/general) are deliberately absent from the set, so real turns keep
compression. The header reader is gated on plugin announcement so a plain client
cannot declare a side request by name to dodge compression.

Behavior change (intended): an opencode v2 title-gen request WITHOUT max_tokens
now routes verbatim instead of being treated as a main turn. Previously it got
the full compress philosophy prompt + tool injected; now it does not. This only
affects requests that carry x-bili-plugin-agent: title; every other request is
untouched.
…1655 hardening dropped the import but missed this call)

Drive-by fix for a pre-existing master breakage found while reviewing
PR #1703: master's CI is red because this finally block calls rmSync,
which #1646/#1655 removed from the imports when converting teardowns to
the retry-hardened rmrf helper (tests/tmp-rm.ts). Every other teardown
in this file already uses rmrf; this one was missed. Without it, every
PR merge-ref run fails here.
@ranxianglei
ranxianglei force-pushed the 2026-09-29_opencode-v2-titlegen-intent branch from 908163c to fdc4d40 Compare September 29, 2026 14:53
@ranxianglei

Copy link
Copy Markdown
Owner Author

🤖 Powered by ework · qwen3.8-27b

[bot] 🏷 Review complete — PR is in good shape; I rebased it onto current master and fixed a pre-existing master test breakage that was red-flagging every job. Details below.

① Original issue direction (#1699): correct

Root-cause chain verified end-to-end: opencode v2 title-gen omits max_tokens (options {} for kind === "title") → bili's budget heuristic defaults a missing budget to 8192 → request misclassified as a main turn → ~11 KiB compress philosophy prompt + ACP compress tool injected into the title model. The PR implements the issue's proposed fix #2 (intent header) and correctly avoids fix #1's trap (treating missing max_tokens as title-gen would misclassify other clients that legitimately omit it) and defers fix #3 (per-agent config = owner-gated config surface).

Duplicate screening: not a duplicate of anything open/closed. Related-but-distinct: #1309/#1431 (auxiliary-request isolation via anchor affinity) share the side-request problem family but address session routing, not classification — flagging so the overlap is visible when you settle the unified strategy under #1309.

② Regression audit — existing behaviors on touched paths, each verdict

  1. src/agent/opencode-v2.ts stampHeaders — adds x-bili-plugin-agent only when the event carries a non-empty string agent id; all existing headers untouched. Verified the shared createOpencodeV2Setup covers BOTH modes (launcher: src/agent/opencode.ts:164; native V2: src/agent/opencode-native.ts:736). Old proxies ignore the unknown header. → preserved.
  2. src/plugin.ts — new constant + reader gated on plugin announcement, mirroring the window/model sibling headers exactly. No existing symbol touched. → no change.
  3. src/server/side-request.ts — intent check runs before the [bug] bili dsh 长会话中 max_output_tokens 被压到 ≤200,主请求被误判为 side request 并绕过压缩 #546 tools-guard. Documented tradeoff: an announced host declaring title on a tools-carrying body wins over the heuristic; worst case is compression skipped for that one request (no kernel-state corruption, verbatim passthrough is the safest handling). Main personas deliberately absent from SIDE_REQUEST_AGENTS, unknown ids inert (exact-match set, falls back to budget). → intended change confined to agent=title; everything else byte-identical — the inert-safe claim holds.
  4. src/server.ts verbatim gate — passes the intent through the existing passthrough path (already serving tiny-budget title-gen); path logic untouched; log line wording now conditional (agent=<id> vs max_tokens<=200) — cosmetic. → preserved except the intended case.
  5. Non-plugin clients — no header ⇒ requestAgent === undefined ⇒ identical classification as before. Codex/pi/dsh lanes unaffected (they never stamp this header); real-client codex e2e runs in CI on this head since the diff touches src/server.ts.

③ Code review

Clean and minimal (+98/-4 across 5 files, every line on-purpose). Two observations (non-blocking):

  • The header value is unvalidated (no length/format check like the model header) — harmless by construction: only exact Set membership "title" does anything, everything else is inert.
  • Gate nuance: a client could technically send x-bili-plugin itself — but announcing plugin mode already opts into plugin-mode semantics (existing design), so the marginal delta is nil. There's no explicit "non-announced client sending x-bili-plugin-agent cannot bypass" test; given the above I'd call it low-priority rather than a gap worth blocking on.

④ Test validity

The two new tests have teeth. Unit: intent outranks both the budget heuristic and the tools guard; build main persona stays a main turn even without budget; unknown persona inert; same body without the header stays non-side (regression guard). E2E contrast: control without the header reproduces the actual defect (messages rewritten + forwarded payload grew >1000 B — the reported ~11 KiB bloat), with the header asserts deepEqual verbatim messages. Both would catch a regression. ✓

⑤ Diff cleanliness

Clean — no unrelated churn; the import-line union keeping isPluginFoldCallId (#1567) alongside the new symbol is justified.

⚠️ Why CI was red — and what I did

All four test jobs failed on one pre-existing master breakage unrelated to this PR: ✖ #1101 T9: BILI_CHAIN_CONTENT env parse → ReferenceError: rmSync is not defined at tests/issue1086-chain-self-exemption.test.ts:728. Root cause: the #1646/#1655 teardown hardening converted teardowns to the retry-hardened rmrf helper (tests/tmp-rm.ts) and dropped rmSync from this file's imports — but missed this one finally-block call (every other teardown in the file already uses rmrf). Deterministic failure, so master's own CI has been red too (verified failing runs at a425f889 and 8293a542), and every PR merge-ref run inherits it.

Actions taken (all verified locally):

  • Rebased onto current master 8293a542 — clean, no conflicts; branch is now linear (supersedes the interim "Update branch" merge commit; I verified the trees are identical apart from the fix below).
  • Drive-by commit test: T9 teardown uses rmrf helper... — replaces the bare rmSync(root, {recursive:true, force:true}) with rmrf(root), matching test: teardown rmSync ENOTEMPTY race — most test cleanups lack retry options (CI ubuntu-latest Node 22 flake) #1646's hardening intent (retry options included), no import change needed. Labeled as its own commit per policy.
  • Re-ran the full pre-flight on the rebased head: npm run typecheck clean · npm run build success (dist-import-annotations OK) · full suite 3273 tests / 3261 pass / 0 fail / 12 skip.
  • Force-pushed the updated branch (head fdc4d406) and appended a "Review follow-up" section to the PR description. CI re-runs on the new head; expected green.

Behavior change (intended, restated per policy)

opencode v2 title-gen request without max_tokens: old = full ~11 KiB philosophy prompt + compress tool injected; new = forwarded verbatim, nothing injected, kernel state untouched. Only requests carrying x-bili-plugin-agent: title are affected; every other request is byte-for-byte unchanged. Config surface: none (internal plugin↔proxy protocol header, alongside x-bili-plugin-model etc.).

I can't merge PRs — system rules forbid it. With CI green on fdc4d406, this is merge-ready: #1703


中文摘要:修复了 opencode v2 标题生成请求因缺少 max_tokens 被误判为主轮次、从而注入 ~11 KiB 压缩提示词+工具导致标题乱码的问题(新增 x-bili-plugin-agent 意图头按意图分类,无头时行为逐字节不变;单测+e2e 对照测试均能钉住回归);review 中发现 master 自身 CI 全红(#1655 teardown 加固漏改一处 rmSync 调用致 ReferenceError,阻塞所有 PR),已顺带用一行 rmrf 修复并单独成 commit,rebase 到最新 master 后本地 typecheck/构建/3273 测试全过——预期 CI 转绿后即可合并。

…batim routing

CI failed 'not ok 3' on the v2 lane: v2's title-gen request re-sends the
first user message (the filler) to the title model concurrently with the
main request, and after #1703 it routes VERBATIM — no ACP ref tags, no
injected tools. Whichever row lands first in the reqLog is a race; when
the title row won, find() matched it and the ref-tag assertion failed.

- fake-upstream-chat.mjs: oracle rows now carry title:true (the detector
  already existed to answer title calls inertly).
- test 3: skip title rows when locating the filler turn; add a v2-gated
  pin that title rows must arrive verbatim (lastUserRef null, no compress
  tool) — the #1699 regression pin. v1 title calls carry neither the v2
  agent header nor max_tokens and were never side-classified; unchanged.

Local: v2.0.3 lane 4/4, v1.14.47 lane 4/4, typecheck clean.
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.

opencode v2 title-gen requests get injected with compression philosophy (misclassified by isTitleGen)

1 participant