Skip to content

fix: mechanically pin subagent session ids into fold blocks (#1702) - #1704

Closed
ranxianglei wants to merge 4 commits into
masterfrom
2026-09-29_mechanical-preserve-fold-commit
Closed

ranxianglei wants to merge 4 commits into
masterfrom
2026-09-29_mechanical-preserve-fold-commit

Conversation

@ranxianglei

Copy link
Copy Markdown
Owner

Root cause (#1563 → #1702)

The kernel's fold protection is tool-name-keyed and content-blind (ALWAYS_PROTECTED_TOOLS = ["compress","acp_rule"]; soft zone = last-5-msgs / 5K tokens). An opencode subagent dispatch pair — the ses_ session id in the tool-call args plus its result — is an ordinary exchange to the kernel: a few parent-agent turns later it slides out of the fresh zone and folds. The id's only survival channel was then the model's free-text summary — probabilistic fidelity (#1563 A/B probe: 20% id loss for weak models; #475 prompt-naming still truncated ids), which broke subagent resume ("Subagent session not found").

Fix — mechanical fidelity at fold commit

New module src/mechanical-preserve.ts, hooked into applyRanges (src/stream.ts) — the single adoption point for every fold path (model-driven compress, preflight overflow, in-place refolds). After the model output exists and before any consumer reads the block state (fingerprint excerpts, token estimate, persistence), the code extracts ses_[A-Za-z0-9]+ ids from the covered originals (machine read, not model transcription) and pins them as a machine appendix line on each changed block summary:

<model-written summary, byte-untouched>

[acp-mechanical-preserve] ses_abc123…, ses_def456…
  • Idempotent — strip-then-re-pin normalizes even lines a model echoed into its own summary; refolds converge to one canonical line per block.
  • Tier-transparent — T2 distillation re-pins automatically: child summaries (carrying appendix lines) are themselves covered text of the parent fold.
  • Zero-cost when inert — no matching id in the covered range → model summary stays byte-identical (test-pinned). Lanes whose host runs its own kernel (pi-style) never see folded originals → structurally a no-op.
  • Scope deliberately narrow (折叠时按消息类型机械保真:opencode 子代理派发对的 seg id 由代码强制写入压缩块(#1563 根因修复) #1702) — subagent dispatch ids only; commit hashes / PR numbers can extend ID_PATTERNS later, generalization explicitly deferred. Prompt layer untouched — no summary-writing rule changed in any channel.

Behavior change (flagged per policy)

Folds covering content with ses_ ids now append one machine line (~tens of bytes) to the stored block summary.

  • Old: summary = exactly the model-written text.
  • New: summary = model text + \n\n[acp-mechanical-preserve] <ids>.
  • Why: the fix itself — id survival must not depend on model transcription quality.
  • Folds without such ids remain byte-identical to before. The line is deterministic and carries a stable marker prefix, so consumers can ignore or render it predictably.

Config surface

None — no field/env/CLI flag added, renamed, or re-semantics'd.

Both compression modes (§6)

  • Proxy mode: fold commits run here → pinned.
  • Plugin mode (opencode native V1/V2): plugins forward tool execution to the proxy (forwardTool → /__bili/plugin/tool); folds execute proxy-side with full history → pinned.
  • Host-side kernel lanes (pi / billion-context-pi): bili never sees folded originals → structurally unchanged (no-op).

Evidence

  • npm run typecheck — clean
  • npm test — 3246 pass / 0 fail / 12 skipped (default skips)
  • npm run build — success (+ dist-import-annotations OK)
  • New regression suite tests/mechanical-preserve.test.ts (14 tests): mechanical acceptance assertions (full-string match inside the block regardless of what the model summary says), the opecode2 压缩的时候会截断sub agent的 session_id #1563 truncated-id failure mode, T2 propagation, refold normalization, zero-cost byte-identity, plus extract/strip/pin unit coverage incl. the per-block cap.
  • E2E (e2e-codex): local run blocked — the local sglang upstream was down at run time (connection refused on forward; proxy pipeline itself processed the request correctly per its log). CI e2e auto-runs on this PR (hot-file trigger covers src/stream.ts) against the configured upstream.
  • Docs: TECHNICAL-NOTES.md new section "Mechanical identifier preservation at fold commit (折叠时按消息类型机械保真:opencode 子代理派发对的 seg id 由代码强制写入压缩块(#1563 根因修复) #1702)".

Closes #1702 (subtask of #1563).

@github-actions

github-actions Bot commented Sep 29, 2026 •

Copy link
Copy Markdown
Contributor

📦 Built Package Artifact

Branch: 2026-09-29_mechanical-preserve-fold-commit (dfb3f36)

Option A — Install this exact build (recommended)

npm install -g billion-context@0.0.1659-pr.1704.d063bdd

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-pr1704.tgz
npm install -g package

Install instructions are refreshed on each push.

@ranxianglei

Copy link
Copy Markdown
Owner Author

🤖 Powered by ework · qwen3.8-27b

[bot] 🏷 收到,开始按 PR 审查规范走(审原 issue #1702/#1563 → 回归清点 → 代码主关 → 测试有效性 → diff 整洁度)。对应 GitHub PR 为 #1704(分支 2026-09-29_mechanical-preserve-fold-commit,单 commit,4 files +386/-0)。先核对代码与 CI,再在本地对 rebase 后的 head 跑 typecheck/test/build,结论稍后回帖。

ework-agent added 2 commits September 29, 2026 23:03
#1563 root-cause fix: fold protection is tool-name-keyed and content-blind, so opencode subagent dispatch pairs (ses_ ids in tool-call args + results) fold like ordinary exchanges and the ids survived only via the model's free-text summary — probabilistic fidelity (A/B probe behind #1563: 20% id loss for weak models; #475 prompt-naming still truncated ids). At fold commit (applyRanges, the single adoption point for every fold path — model-driven compress, preflight overflow, in-place refolds), extract ses_ ids from the covered originals by code and pin a machine appendix line on each changed block summary: after model output exists, before any consumer reads the summary. Idempotent (strip-then-re-pin normalizes model-echoed lines), tier-transparent (T2 re-pins via child summaries), zero-cost when no ids are covered, prompt layer untouched. Closes #1702.
@ranxianglei
ranxianglei force-pushed the 2026-09-29_mechanical-preserve-fold-commit branch from 3066d3e to 2799fb1 Compare September 29, 2026 15:15
…self-exemption suite (#1707)

The T9 teardown restored by the import fix still used plain recursive rmSync, which #1646 deliberately replaced everywhere with retry-hardened rmrf because it races active writers with ENOTEMPTY (this file's own comment at the store-dir teardown says so). Migrate the one remaining call site so no non-hardened teardown is left.
@ranxianglei

Copy link
Copy Markdown
Owner Author

🤖 Powered by ework · qwen3.8-27b

[bot] 🏷 Review complete — mergeable once CI goes green on the new head dfb3f367 (expected; the prior red was master's fault, now fixed in-branch). Details below.

① Original issue (#1702 ← #1563): direction verified

The root-cause chain holds: kernel fold protection is tool-name-keyed and content-blind (ALWAYS_PROTECTED_TOOLS covers only the kernel's own tools; soft zone = last-5-msgs/5K tokens), so an opencode subagent dispatch pair folds like any ordinary exchange and the ses_ id survived only via model transcription (A/B probe: 20% loss for weak models; #475 prompt-naming still truncated ids). The fix attacks the root cause — mechanical extraction of ids from the covered originals at fold commit, not a prompt-level nudge (which would be symptom-treatment: you'd still be trusting the model to transcribe). Correct layer, correct decision point.

② Regression audit (existing behaviors on touched paths)

Enumerated every consumer of block summaries around the hook site in applyRanges (src/stream.ts) and checked each:

  • Hook placement is correct: runs after the content-cache + CCR adoption loops, before every summary consumer — anchorTok estimate (stream.ts:463), [feature] compress 回执加摘要指纹 + inline decompress 回执给再折叠指引 #1294 fingerprint receipt (stream.ts:492-494), persistence. All consumers see the identical pinned text; fingerprint matches what's stored. ✓
  • Single adoption point claim verified: production callers are exactly src/loop/core.ts:238 (model-driven compress — used by both wire adapters AND the plugin tool route), src/preflight.ts:1131 (overflow), src/stream-openai.ts:25, src/stream-responses.ts:39. Native-compaction path bypasses applyRanges — that's the documented out-of-scope boundary, unchanged by this PR. ✓
  • Wire impact: the appended machine line renders into model-visible block-summary text — intended, that is the survival channel. Folds without ses_ ids stay byte-identical (test-pinned with strict equality). Deterministic marker prefix → search_context/decompress/web render it predictably. ✓
  • Behavior-change flag: correctly disclosed per policy (old: summary = model text only; new: + \n\n[acp-mechanical-preserve] <ids> only when ids are covered). No silent drift elsewhere.
  • Config surface: none added/renamed/re-semantics'd. ✓
  • Both §6 modes: proxy mode folds run here → pinned. Plugin mode routing verified end-to-end: forwardTool → /__bili/plugin/tool (server.ts:1247) → handlePluginTool (plugin.ts:899) → remembered full-history snapshot (plugin.ts:991-992) → executeProxyTool (core.ts:226) → applyRanges (core.ts:238) → hook runs proxy-side with full history. Pi-style host-kernel lanes never show bili folded originals → structural no-op. The parallel CCR-comment correction (bacc573e) sharpens exactly this lane distinction and is accurate. ✓
  • ctx.messages vs ctx.compressMessages: the two views differ only in render-tag decoration, so id extraction is identical either way; the hook matches the adjacent content-cache loop's usage. Non-issue.

③ Code review

Module logic sound: idempotent strip-then-re-pin (normalizes lines a model echoed into its own summary), zero-cost fast path returns the same string reference (no copy, no mutation) when inert, per-block cap (100) with truncation logged, global-flag regex lastIndex reset done correctly. Tier transparency works because child summaries (carrying appendix lines) are themselves covered text of the parent fold.

④ Test validity

14 tests, real teeth: mechanical full-string assertions regardless of what the model summary says; the #1563 truncated-id failure mode; T2 propagation (asserts a genuinely new parent block); refold normalization (decompress → re-fold → exactly one canonical line); zero-cost byte-identity as strict equality. Integration tests drive the real kernel (createCore/assignRefs) + parseCompressInput + applyRanges — no mock-only assertions. Each test maps to a stated invariant.

⑤ Diff cleanliness

Purpose-related files only, no whitespace churn, no lock/generated noise.

CI forensics — the 4 red test jobs were NOT this PR

ci.yml checks out the merge tree (master+head). Master carried a broken tests/issue1086-chain-self-exemption.test.ts since merge 1ffae958 (#1690): a chimera where parent c7615962 was clean (post-#1646 rmrf migration, zero rmSync calls) and parent e5c94fce was clean (11 calls + proper import), but the merge kept master's import line while taking the PR-side T9 body containing a bare rmSync at line 728 → deterministic ReferenceError on all 4 test-matrix jobs for every new PR. First observed on this PR's run 36584933764 (its other 8 workflows — e2e, hermetic registry, 4 real-client lanes, build, version-guard — were all green on head 3066d3e4). Filed as #1707 with full root cause. Fixed in-branch: 2799fb1a (restore import) + my dfb3f367 (migrate that call site to retry-hardened rmrf per #1646 direction, so no non-hardened teardown is left).

Local evidence (rebased onto current master 8293a542)

  • typecheck: clean
  • npm test: 3285 tests / 3273 pass / 0 fail / 12 skipped (on bacc573e; my dfb3f367 is a 2-line test-only change whose file I re-ran: 21/21)
  • build: success + dist-import-annotations OK
  • One transient local flake observed under concurrent load (#437 idle-timeout stream test; passes isolated) — noted, not a blocker.
  • E2E: local upstream was down at authoring time (per PR body); CI e2e ran green on the previous head and auto-reruns on dfb3f367 (hot-file trigger).

Not merging — human-only. Please merge after the dfb3f367 CI run goes green.

中文摘要:审查了子代理 session id 折叠丢失的根因修复(折叠提交时由代码把 ses_ id 机械写入压缩块,不依赖模型转写)——方向正确、双压缩模式均验证、测试有牙且覆盖全部声明不变量;CI 四个红 job 是 master 上 #1690 合并事故导致测试文件缺 import(与本 PR 无关,已立 #1707),已在 PR 分支顺手修复(恢复 import + 迁移到 rmrf),本地全量验证绿,CI 转绿后可合并。

@ranxianglei

Copy link
Copy Markdown
Owner Author

Closing per the review direction in this thread: the replacement is #1722 — structured-field capture (no regex), sidecar storage in session.metadata, render-time attach on receipt + carrier, byte-clean stored summaries, opencode-only gate. The re-usable pieces from this PR (test harness shape, ses_ shape predicate) carried over. Thanks for the groundwork!

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 子代理派发对的 seg id 由代码强制写入压缩块(#1563 根因修复)

1 participant