Skip to content

feat(agent): opencode v2 local compression kernel — no proxy, mode parity with bili pi - #909

Closed
ranxianglei wants to merge 10 commits into
2026-09-15_opencode-native-pluginfrom
2026-09-17_opencode-v2-local-kernel
Closed

ranxianglei wants to merge 10 commits into
2026-09-15_opencode-native-pluginfrom
2026-09-17_opencode-v2-local-kernel

Conversation

@ranxianglei

Copy link
Copy Markdown
Owner

Fixes #908. Stacked on #850 (same convention as #885: base is the #850 source branch so the stack lands together).

What

src/agent/opencode-local.ts — an opencode v2 plugin that runs the acp-kernel compression pipeline IN-PROCESS, giving opencode the same pure-local mode bili pi users get: no proxy hop, no BILLION_CONTEXT_PROXY, no MITM, wire traffic straight to the real upstream.

  • session registry keyed by conversation id; session.hook rebuilds the wire view through the kernel (folded messages, acp tags on text parts)
  • chat.params.tools injects the kernel compress/decompress/acp_status tool set; chat.parameters enforces the compress system prompt (with a local tag-format note — the shared prompt teaches proxy-style tags)
  • tool execution goes through the kernel, so acp_status renders the same panel as /acp, panel-first, with its own host identity (billion-context-opencode-local@version)

Fixes riding on this branch (recorded in #906)

  • v2 tool-result cores keyed by call id — v2's context projection gives tool-result messages no id; ${msg.id}:${i} collapsed every result onto one ref (undefined:0), orphaning each tool call and looping the model (the acp_status ×7 / "Tool result missing" bug). Keyed by tr_ call id now.
  • acp_status panel-first with own hostLabel through RewriteCtx/AcpStatusCtx + VERSION resolution for dist/agent/*.js depth
  • real echo strip — stripEchoes gate was a literal '\\x3cacp' that never matched real tag bytes; model-fabricated tags now stripped on ingest
  • weak-overflow abort guard (src/loop/core.ts) — a user ESC broke the parse loop with !sawDone && streamError === undefined, the exact shape the 高 usage 下的非 400 失败(流截断/超时)无任何 overflow 兜底 —— 会话直接死 #498 detector counts; 3 aborts in 15min would learn a shrunken window. Now && !signal?.aborted, mirroring the existing retry guards. Regression: tests/loop-truncation.test.ts [Bug] 消息引用编号与当前顺序不一致时,compress 范围提示会选错消息,导致“内容不足”失败 #887 T1 (aborted cut must not count; same cut without abort must).
  • opencode-local system array: spread-assignment instead of in-place push — a reused e.system or double-fired hook can no longer append duplicate compress prompts.

Pre-flight

  • npm run typecheck ✓
  • npm test — 1673 pass / 0 fail (incl. 11 new tests/opencode-local.test.ts tests)
  • npm run build ✓ (tsup entry includes src/agent/opencode-local.ts)

E2E (Docker-isolated, real opencode v2 + local sglang)

Four postures green: proxy / launcher / native-spawn / pure-local. Compression stress test in-container: model read ~200K tokens of real source, self-triggered compress 7× (105K folded), refs strictly monotonic, no id reuse, recall-from-summary verified, no tag leakage.

(Note: an early local commit accidentally tracked iso/build/opencode.exe (207MB rig build artifact); history was rewritten before the first successful push — the branch on origin has never carried it.)

Sisyphus added 8 commits September 17, 2026 21:45
… inside opencode with no proxy

Mode 3 for opencode v2, mirroring bili pi: session.event 'context.window.changed'
feeds the full transcript into the kernel, the wire view is rebuilt with synthetic
summaries + nudge, and the compress tool runs inside opencode (carrier = the tool
call, acp_summary suppressed). /compact is intercepted (markCompaction archives
blocks whose raw ids left the transcript). Remote proxy mode stays the default.
…ds collapsed every result onto one ref and looped the model

v2's context projection emits tool-result messages with id undefined, so the
positional core id became "undefined:0" for every call: the kernel deduped
them into one message and the rebuild orphaned all later tool calls — the
provider filled the gaps with opencode's 'Tool result missing' placeholder
and the model re-issued acp_status in a loop (repro'd live in the rig TUI).
Tool-result cores now carry tr_<callId> and the rebuild re-attaches each
original part by call id.
…e entries by call id (tr_) so results survive the rebuild instead of collapsing onto "undefined:0" and orphaning every tool call (the acp_status loop / "Tool result missing" bug)
… — hostLabel through RewriteCtx/AcpStatusCtx; fix VERSION resolution for dist/agent/*.js depth (package.json walk-up)
…g-format note

- acp_status default (no drilldown args) now leads with the same
  buildStatusPanel /acp renders, followed by the structured report;
  drilldown queries (scope/view/tool/sort/limit) keep the report alone
- stripEchoes gate compared against the literal 8-char string '\x3cacp'
  instead of the real tag bytes, so model-echoed tags were never
  stripped on ingest; use the actual hex escape
- the shared kernel prompts teach the PROXY tag format (example
  type="tool:bash", every message tagged); the local host renders
  text-only so the example mismatched reality and the model fabricated
  proxy-style tags (observed type="tool:acp_status" written before a
  tool call). withLocalTagFormatNote appends the actual format + the
  no-write rule to the local system prompt
…array in-place push

- loop/core.ts: a user ESC breaks the parse loop with !sawDone &&
  streamError === undefined — the exact shape the #498 weak-overflow
  detector counts. Three aborts inside 15min would learn a shrunken
  context window for the session (#570 false-positive family, MIN_EVENTS=3).
  Guard the counter with !signal?.aborted, mirroring the existing
  truncation/degenerate retry guards.
- agent/opencode-local.ts: build the compress system array with a
  spread instead of in-place push — if the host ever reuses e.system
  across requests or fires the hook twice per request, the push would
  append one full compress prompt per request and grow the window
  linearly.
- tests/loop-truncation.test.ts #887 T1: aborted cut vs control cut —
  abort must not count, the same cut without abort must.
@ranxianglei

Copy link
Copy Markdown
Owner Author

🤖 Powered by ework · qwen3.8-27b

[bot] 🏷 Reviewing #909 now. Plan: pull the head branch, diff against the #850 base, run typecheck + full test suite + build on the rebased head, and verify each of the five fixes riding on this branch (tool-result core keying, acp_status panel-first, echo strip, weak-overflow abort guard, system-array spread). Will report findings here with a mergeability verdict.

ework-agent added 2 commits September 17, 2026 22:03
The history rewrite that dropped the tracked iso/build artifact also swallowed
three unrelated dev-tool scripts (registry/codex-models snapshot refreshers,
dump analyzer) into commit e59776b, whose message describes the tr_ keying fix.
package.json still wires 'npm run registry:snapshot' / 'codex-models:snapshot'
to them, so the deletion broke those scripts. Restore verbatim from the #850
base.
src/compress-tool.ts (LOCAL_TAG_FORMAT_NOTE + its doc comment) and
src/agent/opencode-local.ts (stripEchoes comment) carried literal angle-bracket
tags; every other source file uses \x3c/\x3e escapes so agent editing tools
can't strip them. Runtime string values are byte-identical (verified: typecheck
+ opencode-local/loop-truncation/plugin-agent tests green).
@ranxianglei

Copy link
Copy Markdown
Owner Author

🤖 Powered by ework · qwen3.8-27b

[bot] 🏷 Review complete. I pulled the head, diffed against the #850 base (a119940), ran the full pre-flight, audited every riding fix, and fixed two problems directly on the branch (pushed, head is now e6e7abc).

Fixed on this branch (2 commits)

1. 50cfaaf — restore 3 scripts accidentally deleted. Commit e59776b's message describes the tr_-keying fix, but its diff also deletes scripts/update-codex-models-snapshot.mjs, scripts/update-registry-snapshot.mjs, and tools/analyze-dumps.mjs (−267 lines). All three exist on master and on the #850 base, and package.json still wires npm run registry:snapshot / codex-models:snapshot to the first two — as merged, those npm scripts would be broken. Looks like collateral of the history rewrite that dropped the tracked rig binary. Restored verbatim from the base; content byte-identical.

2. e6e7abc — hex-escape raw <acp> tags. AGENTS.md §3 mandates \x3c/\x3e escapes for any acp XML in source (so agent editing tools can't strip them) and every other file complies, but src/compress-tool.ts (LOCAL_TAG_FORMAT_NOTE string const + doc comment) and src/agent/opencode-local.ts (stripEchoes comment) carried literal angle-bracket tags. Converted all 6 occurrences. Runtime string values are byte-identical — verified with typecheck + the three affected test files (57/57 pass).

Riding fixes — verified

  • tr_ call-id keying (opencodeToCore): tool-result messages get no id in v2's projection, so the old ${msg.id}:${i} key collapsed every result onto one ref. Keying by tr_${callId} is correct; matches the textProtocol extracted-trigger tool results fall into the marker visibility branch (role:system) instead of tool-call/result pairing #906 repro.
  • abort guard (src/loop/core.ts:624): && !signal?.aborted added to the weak-overflow note condition; signal is the existing param used by the retry guards at :364/:403, so scope is right. The regression test ([Bug] 消息引用编号与当前顺序不一致时,compress 范围提示会选错消息,导致“内容不足”失败 #887 T1 in tests/loop-truncation.test.ts) is genuinely discriminating: same cut stream, 95% usage fallback, aborted run asserts zero "weak overflow signal" hits while the no-abort control asserts ≥1 ("1/3") — i.e. it would fail without the guard.
  • real echo strip: gate is now the actual tag bytes before stripAcpTags; the old literal never matched. Correct.
  • panel-first acp_status + own host identity: hostLabel flows through RewriteCtx/AcpStatusCtx, falls back to the proxy label, and the version walk-up handles dist/agent/*.js depth. One subtlety checked: the status handler runs processTurn({renderTags:"none"}) a second time per status call and discards the returned state — safe, because the kernel's contract is explicit state-in/state-out (pure pipeline over {messages, state}), both calls receive identical inputs, and ref assignment is idempotent over an already-assigned map (no number re-issue).
  • system-array spread: replaces in-place push, fixing unbounded growth when the host reuses e.system across requests. Residual: a hook firing twice on the same event object could still append once per fire (bounded). I considered a marker-prop guard but rejected it — unknown props risk leaking onto the wire for strict backends.

Non-blocking notes (no action taken)

  • memory Map (sid → last CoreMessage[]) in opencode-local never evicts; a long-lived opencode process with many concurrent sessions grows without bound. Proxy-side analog is orphan-gc; consider later.
  • Echo stripping covers assistant text parts only, not reasoning parts.
  • coreToOpencode groups by id.lastIndexOf(":") — assumes every produced id contains : (true today; would silently mis-group if the kernel ever emitted an un-namespaced id).

Pre-flight & E2E

  • npm run typecheck ✓ · npm test: 1673 pass / 0 fail / 2 skipped (matches your claim) · npm run build ✓ (emits dist/agent/opencode-local.js)
  • Full codex E2E (ACP_TEST_E2E=1, 4-phase core): could not complete in this environment — the default local sglang upstream refused connections (proxy log shows ECONNREFUSED on the forward hop; direct probe of the upstream also fails from here). Not a code signal: the proxy-side log shows the new pipeline executing correctly up to the forward (processTurn renderTags=text-only, nudge gating, session lock). Your Docker-isolated four-posture run + stress test remain the E2E evidence; the manual-dispatch CI workflow is the place to re-confirm with a live upstream secret.

Diff is otherwise clean — every line traces to the feature or the five recorded fixes. Everything verified above passes with my two commits included. Ready for you to merge (merges stay human-side).

中文摘要:修复了分支上两个问题——e59776b 误删的 3 个 snapshot/devtool 脚本(package.json 仍引用,会坏掉 npm scripts,已按 base 原样恢复)和两处源码里违反 hex-escape 规范的裸 acp 标签(运行时字节不变,已验证);五个随行修复逐条核对无误,全量测试通过,本环境缺本地上游跑不了完整 E2E(代理侧日志显示管线执行正常),可以合并。

@ranxianglei

Copy link
Copy Markdown
Owner Author

Closing — superseded by architectural decision (align on the opencode-native self-spawn-proxy pattern, i.e. exactly what bili pi does).

Why: this PR's in-process kernel has no wire seam, so it cannot do stream-time echo stripping (loop/tag-echo-filter.ts, #206/#717). Real-session forensics (opencode.db) showed the model forging <acp tokens="1" type="text">m00007</acp> tags that leak into the TUI and the persisted history — bili pi never leaks because its self-spawned proxy strips echoes on the stream before the client ever sees them.

The pi-parity architecture already exists on master: src/agent/opencode-native.ts (#885) — self-spawned proxy via the same ensureProxyRunning bootstrap, per-request URL rewrite through the v2 session.hook("http.request") seam (v2's equivalent of pi's fetch patch), x-bili-plugin header (plugin mode), forwardTool for tools — one-to-one isomorphic with pi-native.ts/pi.ts.

Salvaged from this PR into #910: the mode-independent loop fix (client abort miscounted as weak overflow, src/loop/core.ts + #887 T1 regression). The opencode-local.ts host and its tests are abandoned with this branch.

joshwapohlmann pushed a commit to joshwapohlmann/billion-context that referenced this pull request Sep 17, 2026
A client abort breaks the parse loop at the signal check with
!sawDone && streamError === undefined — the same shape as a truncation.
Three ESCs inside 15min armed a shrunken window (MIN_EVENTS=3) that
throttles the session below its true window (ranxianglei#570 false-positive family).

Refs ranxianglei#906. Re-lands the loop half of ranxianglei#909 on master (the opencode-local
half is superseded — see ranxianglei#909 close reason).
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.

1 participant