Skip to content

[PR] fix(opencode): acknowledge V2 turns and preserve rolling cache marks - #5742

Merged
ranxianglei merged 5 commits into
ranxianglei:masterfrom
mariopasquali:2026-11-05_opencode-incremental-cache
Nov 5, 2026
Merged

ranxianglei merged 5 commits into
ranxianglei:masterfrom
mariopasquali:2026-11-05_opencode-incremental-cache

Conversation

@mariopasquali

Copy link
Copy Markdown
Contributor

OpenCode V2 uses http.request to divert incremental turns, but an attached proxy has no fetch interceptor to collect their acknowledgement. Register http.response and correlate its acknowledgement with the outgoing request's turn-id header. This lets the next turn send a delta.

OpenCode also moves its ephemeral cache marks as the conversation grows. Including those marks in prefix alignment makes unchanged history look edited and forces a full resync. Align content separately from its cache decoration and carry validated mark updates for acknowledged wire messages. The server replaces their marks and opaque-block decorations, preserving signatures, tool arguments and media bytes.

The optional retouch: [{ index, message }] envelope is sent only when the capability probe explicitly advertises cacheControlRetouch: true. Without that advertisement, a moving mark keeps the original full request. Unknown/cold wire spans or changed payloads return HTTP 409 before applying the delta; the existing resync path recovers. The extension is documented in PLUGIN.md and adds no storage schema or configuration setting. Marks also follow client-pinned IDs instead of being left keyed by discarded derived IDs.

Validation:

  • The rolling-cache regression fails on pinned current master and passes with this patch.
  • Capability tests cover absent/false/true support. V2 hook tests cover acknowledgement attribution and host-safe hook failures.
  • Full-resend comparisons cover rolling marks, cold recovery, documents, images, multimodal tool results and signed thinking. Signature/is_error removal is refused and an unchanged valid retry succeeds.
  • Typecheck, 14 targeted test files (runner reports 140 passed, 0 failed), build and distribution checks passed on commit a6ea2a0af152c3ea7270e95dbb5e8db4d2f06e60.

The credential-forwarding fix from PR #5661 is already in the base and is not duplicated. These are loopback adapter/proxy tests; fresh live OpenCode 2.2 end-to-end qualification and production rollout are separate. The change reduces resends, without claiming to eliminate the server's whole-history processing cost.

Fixes #5740

ework-agent and others added 2 commits November 5, 2026 14:36
…decar residual (ranxianglei#4599)

01fcc5d (ranxianglei#5656/ranxianglei#5584, merged via ranxianglei#5687) diverts tool-less dsh single-shot
requests onto the verbatim side lane (kernel no-touch); the residual for main
turn 1 is documented in the commit's own comment. Five pre-existing pins
assumed turn 1 rides the full pipeline and went born-red at merge (master red
since; every PR head inherits it).

Test-only realignment codifying the documented behavior:
- issue2419/2446/2447/2481 guard-stamp subtests: two turns; the BLOCKED ref
  is witnessed on the first FULL-PIPELINE turn (turn 2). No fold can happen
  before turn 2 (nothing compressible, no action surface), so protection
  against every possible fold is unchanged.
- issue2817: stats.requests off by one (turn 1 uncounted); the persona-fold
  teeth are intact, assertions shifted -1.
- issue5403 A/B golden: turn 0 rides the side lane in BOTH phases (the gate
  ignores the rollout env), so rb[0] is unstamped by design; stamps resume at
  turn 1. Byte equality holds at i=0 (both phases forward verbatim).

No src/, kernel/, config, or version changes. Locally green: all six files +
side-lane-invariants + issue5656-single-shot-sidecar + dsh-persona-fingerprint.
@ranxianglei ranxianglei changed the title fix(opencode): acknowledge V2 turns and preserve rolling cache marks [PR] fix(opencode): acknowledge V2 turns and preserve rolling cache marks Nov 5, 2026
@ranxianglei

Copy link
Copy Markdown
Owner

🤖 Powered by ework · qwen3.8-27b

(ai: vllm-qwen/qwen3.8-27b)

查重: 不重复。 检索了 open+closed 的 issue/PR:没有其他条目针对 OpenCode V2 增量车道的 ack 丢失或滚动 cache mark 触发全量 resync。最接近的在途 [PR] 行 "fix(incremental): continue repeated-message identities across delta turns" 是不同机制(重复消息的 msgid 身份延续,不是 ack 归因/装饰对齐)。PR 正文引用的 credential-forwarding 修复已合入 master(base 包含它,未重复搬运)。本 PR 的目标 issue 即其来源 issue,符合一 issue 一 PR。

标签: bug opencodev2 scope/wiring p2(理由:V2 native 车道增量路径的功能缺陷——attach 模式 seed 后拿不到 ack 无法推进 delta、滚动 cache mark 每轮强制全量 resync;有绕行(全量重发仍可用)、无数据丢失,故 p2 而非 p1)。

审核进行中:已拉取分支(单提交 a6ea2a0af,base 即当前 master),正在逐文件读 diff、核对既有行为与测试有效性,随后按结构化八问给出结论。

@ranxianglei

Copy link
Copy Markdown
Owner

🤖 Powered by ework · qwen3.8-27b

(ai: vllm-qwen/qwen3.8-27b)

七问(CI 修复轮)

1. 什么红了? 全部 11 个 test 分片(windows-22 ×5、ubuntu-22 ×3、ubuntu-24 ×3),head a6ea2a0af,run 42019914812。

2. 什么原因? 不是本 PR 的代码——是继承性失败:master(0aace2f20)自身就红,#5656 sidecar 残留家族的五枚 dsh 回归钉(issue2446/issue2447 的 session-level guard-stamp、issue2481 runtime-context、issue2817 crosswire persona fold、issue5403/#5418 watermark fastpath)在裸 master 上以相同签名失败。

3. 继承还是自引入? 继承,本地 A/B 钉死:同一棵 built 树上,裸 master 精确挂其中 3 枚(2446/2447 guard-stamp + 2817),merge ci-fixes 后 5 枚全绿;本 PR 新增的 rolling cache_control 等测试在 CI 里全过(如 ok 2375–2382)。抽了三个平台分片日志(ub22-s1 / win22-s1 / ub24-s3),失败集合 = 且仅是上述 dsh pin,无任何其他失败。

4. 怎么修的? 把 origin/ci-fixes(0470bec62,重对齐五枚 dsh pin,只动测试文件)merge 进 PR 分支,零冲突,新 head b7f6e0bfd。PR 原有 diff 一字未动。

5. 回归风险? 无。ci-fixes 只改 5 个测试文件(钉对齐),不碰 src/kernel/wire/config;本 PR 行为面不变。

6. 现在状态? 新 head b7f6e0bfd 全量 run 已绿:37 success / 14 skipped / 0 failed。PR 可合,等人工合并。

7. 残留? master 本身仍红(同家族),路线 A/B 决策在 #5722(ci-fixes 暂留 A)。本分支已含 re-pin 提交 0470bec——若 B(#5709 源收窄)先落地,按舰队惯例由哨兵统一清扫,本 PR 无需单独动作。已在 #4599 留一行记录(PR #5742 红,已 merge ci-fixes,sha=b7f6e0bfd)。

@ranxianglei

Copy link
Copy Markdown
Owner

🤖 Powered by ework · qwen3.8-27b

[bot] 🏷
(ai: vllm-qwen/qwen3.8-27b)

CI 修复收工确认:本 PR 的 11 个红 test 分片为继承性失败(master 自身红的 #5656 sidecar 残留家族,五枚 dsh 回归钉),非本 PR 代码问题——本地 A/B 已钉死。已将 origin/ci-fixes(0470bec,仅重对齐 5 个测试文件)merge 进 PR 分支并推送,新 head b7f6e0b,PR 原 diff 未动。

结果:新 head 全量 CI 已绿(0 failed / 0 pending),PR 可合并,等人工合并。已在 #4599 留一行协调记录;详细七问见上一条回复。无需决策、无新增 issue。

…ing-cache A/B comparisons

Review fix on top of a6ea2a0. The new suite's byte-exact A/B assertions
compared raw rendered history across two separately-run arms; ranxianglei#4313 message
timestamps are on by default and each arm's kernel renders carry that arm's
first-sight wall clock in time="..." attributes, so 8/10 tests failed
deterministically whenever the arms crossed a second boundary (they always
do). Apply the same normalization tests/issue5403-watermark-fastpath.test.ts
already sanctions (ranxianglei#5471): mask only the time attribute before comparing.
Product code untouched; with this, 10/10 pass standalone.
@ranxianglei

Copy link
Copy Markdown
Owner

🤖 Powered by ework · qwen3.8-27b

(ai: vllm-qwen/qwen3.8-27b)

PR #5742 审核结论(独立复审,非复读 issue)

查重:不重复(首条已报)。标签:bug / opencodev2 / scope/wiring / p2。

结构化八问

1. 是什么问题
OpenCode V2 增量车道有两个缺陷:①attach/spawn 模式下代理没有 fetch 拦截器,V2 回合的确认(ack)无人收集——seed 之后永远无法推进到 delta,每轮退化为全量重发;②OpenCode 会随对话增长移动 ephemeral cache_control 标记,而前缀对齐把标记算进内容哈希——内容没变却被判成"编辑过",每次标记移动都强制全量 resync,服务端标记只增不减地累积而不是跟随当前 wire 装饰。

2. 什么原因导致的
两个独立根因:①ack 收集路径单一依赖客户端 fetch 拦截器,缺 http.response 这条缝;②对齐身份 = hash(role+完整内容含 cache_control),把"消息内容"与"缓存装饰"混为一谈,且服务端 canonical 没有 wire→core span 映射,装饰更新没有安全通道可走。修法分别打到了这两个根因上,不是治标。

3. 影响范围与严重程度
opencode V2 插件车道(anthropic wire、增量 divert 生效的会话)。修复前:attach 模式永久卡 seed(功能可用但成本/性能退化);rolling-mark 会话每次标记移动触发一次全量 resync。p2:能用、有明显问题、有绕行(全量重发本身正确)。

4. 怎么修复的
①注册 http.response hook,按出站请求头 x-bili-turn-id 关联 ack——与既有 fetch 拦截路径幂等共存(首个消费者认领,第二个空操作);hook 注册包 try/catch,宿主不支持该缝时回落到 30s TTL,绝不向宿主抛错。②对齐哈希剥离块级 cache_control(仅对齐用),原始 wire hash 保留给边界/ack 语义不变;新增能力门控的 retouch 通道:probe 显式通告 cacheControlRetouch: true,客户端仅在收到该通告时发送 retouch:[{index,message}];服务端逐项验证后原子应用(cold canonical/未知 span/内容变化 → 409,走既有 resync 恢复),替换标记与 opaque-block 装饰,签名/工具参数/media 字节逐字保留;标记跟随客户端钉定的 id。

5. 是否有回归问题(逐项审过既有行为)

  • msgIds-pin 分支的 derived→pinned cacheControls 重映射:applyMsgIdPin 返回非变异拷贝(flat.msgs 保留派生 id),顺序正确 ✓
  • 收缩守卫 !aligned || hashes.length < sent.length → resync 保持原样,retouch 循环无越界窗口 ✓
  • wireHash、公开边界哈希孪生、服务端 hashWireBoundary 均未动,ack 水位语义不变 ✓
  • marksVersion++/renderPrefix=null 失效复用既有路径;resync 先删旧标记再重建,不会累积超过 Anthropic 4-breakpoint 上限 ✓
  • 行为变更披露(旧值→新值):planIncrementalTurn 为所有插件车道共享,但只有 opencode-native 传 capability=true。其他车道的 anthropic-wire 会话遇到纯装饰漂移时:旧行为=前缀失配→lane resync;新行为=skip(走常规全量重发管道,lane 状态保留,下轮重新 divert)。两条路都正确恢复、无数据丢失,但这是语义位移,特此明示。
  • 另注:服务端 probe 无条件通告 cacheControlRetouch: true(无灰度开关/kill-switch);爆炸半径受限于客户端能力门控 + 409 兜底 + 唯一发送方是 opencode-V2,可接受但记录在案。

6. 是否破坏缓存
不破坏,方向相反:对齐分离消除的是假 resync,真实前缀缓存收益不受影响;边界哈希仍用原始 wire 字节。

7. 是否改了设计方向
未改。不动 kernel/、存储 schema、配置面;retouch 是协议 v1 的可选扩展,靠显式能力通告门控,老服务端收不到(客户端不发)、新服务端对老客户端无害(字段缺席即跳过)。PLUGIN.md 文档与实现一致。

8. 是否引入新配置
无。无新 env/config/默认值。

测试有效性 + 一处已在分支上修掉的缺陷

A/B byte-exact 对比、精确 mode 序列断言(含 ["seed","delta","delta","delta:409","seed","delta","delta"])、伪造/畸形 retouch 负例行、原子拒绝+重试断言——断言有牙,不是恒真测试。但我本地定向复跑发现新测试文件有一处缺陷并已就地修掉(小问题直接修,additive commit fd625c8d2,产品代码零改动):其原始字节对比与墙钟竞态——#4313 消息时间戳默认开启,两个 A/B arm 各自渲染各自的 first-sight time="…" 属性,本环境可重复 8/10 失败;我套用了仓库既有的 #5471 认可归一化条款(与 tests/issue5403-watermark-fastpath.test.ts 同一条款,只屏蔽 time 属性)。修复后本地 10/10 通过。这也说明原断言是构造性 flaky(你的 140 passed 报告与 CI 偶绿都取决于 arm 是否恰好落在同一秒内)。

验证状态与判定

判定:方向正确、代码质量达标、测试缺陷已就地修复 → 建议 CI 在 fd625c8d2 上全绿后合并(owner 合并)。

…msgid-lane-flow settle flake (3!==4, green locally 6/6; see ranxianglei#4599)
@ranxianglei
ranxianglei marked this pull request as ready for review November 5, 2026 15:12
@ranxianglei
ranxianglei merged commit 9a2d51d into ranxianglei:master Nov 5, 2026
51 checks passed
@github-actions

github-actions Bot commented Nov 5, 2026

Copy link
Copy Markdown
Contributor

📦 Merged Build Published

This merge to master was published as 0.0.1043-master.9a2d51d (rolling master tag).

npm install -g billion-context@0.0.1043-master.9a2d51d  # this exact build
npm install -g billion-context@master  # always the newest merged build

npm view billion-context@master prints the current version at any time.

ranxianglei pushed a commit to mariopasquali/billion-context that referenced this pull request Nov 5, 2026
…anxianglei#4542/etc (conflict resolution for ranxianglei#5743)

# Conflicts:
#	tests/opencode-rolling-cache-marks.test.ts
ranxianglei pushed a commit to mariopasquali/billion-context that referenced this pull request Nov 6, 2026
…oth seams

ranxianglei#5741 redo, per maintainer direction: stop patching the derived-id symptom
(content clusters continued across delta turns) and give the lane what it was
designed to receive — the host's own session-scoped message ids.

What was wrong with the premise: opencode 2.x was assumed to expose no host
ids ("weakest of the four host seams"). It does: ctx.session.hook("context")
fires immediately before EVERY provider-request assembly carrying the full
{ id, role, content }[] conversation plus sessionID (verified live on
@opencode/cli 2.0.4, anthropic AND openai-compatible providers). V1 keeps
its experimental.chat.messages.transform seam as before; both now feed one
per-session store. With real ids riding, byte-identical repeated prompts get
distinct vouched ids (msg_r1 vs msg_r2), so the server-side pin creates one
row per occurrence and the collision class of ranxianglei#5741 disappears at the root.

Changes:
- opencode-v2.ts: register the "context" hook in createOpencodeV2Setup
  (shared by V1+V2 hosts; try/catch — hosts without the seam keep the
  documented id-less degrade; never fail startup over it)
- opencode-msgids.ts: deriveEntry now proves BOTH event shapes onto the wire
  1:1 with semantic part tokens (text bytes; tool call/result anchored by
  provider tool-use id, not input bytes; reasoning↔thinking; media↔image);
  host role "tool" normalizes to wire "user"; bookkeeping parts skip; ANY
  unrecognized shape still drops the whole field (宁可降级不可错位)
- tests/opencode-msgids.test.ts: tool-session end-to-end through the V2 seam,
  the ranxianglei#5741 repeated-prompt scenario (distinct fresh ids per occurrence),
  reasoning/thinking alignment, unknown-part and duplicate-id degrade paths

Server side needed NO changes: the claimed-set validation and pin remap
landed with ranxianglei#5742/ranxianglei#5743 already. continueContentClusters stays as the
fallback path for hosts/turns without a usable harvest.

Verified: tsc --noEmit clean; opencode-incremental/msgids/rolling-marks,
incremental-content-clusters, msgid-pin/algebra/lane-flow, dsh/pi-msgids,
incremental-lane, issue5403 fastpath, pins-wire-id-mints all green.
ranxianglei pushed a commit to Louie0806/billion-context that referenced this pull request Nov 14, 2026
…oth seams

(content clusters continued across delta turns) and give the lane what it was
designed to receive — the host's own session-scoped message ids.

What was wrong with the premise: opencode 2.x was assumed to expose no host
ids ("weakest of the four host seams"). It does: ctx.session.hook("context")
fires immediately before EVERY provider-request assembly carrying the full
{ id, role, content }[] conversation plus sessionID (verified live on
@opencode/cli 2.0.4, anthropic AND openai-compatible providers). V1 keeps
its experimental.chat.messages.transform seam as before; both now feed one
per-session store. With real ids riding, byte-identical repeated prompts get
distinct vouched ids (msg_r1 vs msg_r2), so the server-side pin creates one
row per occurrence and the collision class of ranxianglei#5741 disappears at the root.

Changes:
- opencode-v2.ts: register the "context" hook in createOpencodeV2Setup
  (shared by V1+V2 hosts; try/catch — hosts without the seam keep the
  documented id-less degrade; never fail startup over it)
- opencode-msgids.ts: deriveEntry now proves BOTH event shapes onto the wire
  1:1 with semantic part tokens (text bytes; tool call/result anchored by
  provider tool-use id, not input bytes; reasoning↔thinking; media↔image);
  host role "tool" normalizes to wire "user"; bookkeeping parts skip; ANY
  unrecognized shape still drops the whole field (宁可降级不可错位)
- tests/opencode-msgids.test.ts: tool-session end-to-end through the V2 seam,
  the ranxianglei#5741 repeated-prompt scenario (distinct fresh ids per occurrence),
  reasoning/thinking alignment, unknown-part and duplicate-id degrade paths

Server side needed NO changes: the claimed-set validation and pin remap
landed with ranxianglei#5742/ranxianglei#5743 already. continueContentClusters stays as the
fallback path for hosts/turns without a usable harvest.

Verified: tsc --noEmit clean; opencode-incremental/msgids/rolling-marks,
incremental-content-clusters, msgid-pin/algebra/lane-flow, dsh/pi-msgids,
incremental-lane, issue5403 fastpath, pins-wire-id-mints all green.

(cherry picked from commit 2fa9dc4)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

OpenCode V2 incremental turns lose acknowledgements and resync on rolling cache marks

2 participants