Skip to content

[PR] fix: interpret cache-diff byte offsets in original text coordinates - #6496

Merged
ranxianglei merged 16 commits into
ranxianglei:masterfrom
Afloat16:2026-11-12_cache-diff-byte-offsets
Nov 14, 2026
Merged

ranxianglei merged 16 commits into
ranxianglei:masterfrom
Afloat16:2026-11-12_cache-diff-byte-offsets

Conversation

@Afloat16

@Afloat16 Afloat16 commented Nov 13, 2026 •

Copy link
Copy Markdown
Contributor

Closes #6495

Problem

The offline cache-diff diagnostic computes the first differing UTF-8 byte correctly, but then uses that byte offset to compare JSON element spans and slice JavaScript strings, whose indices are UTF-16 code units. A long CJK or emoji prefix can therefore make a rewrite of an existing message look like a normal tail-field append. The diagnostic can also lose the message index or show an unrelated/empty evidence snippet, hiding the actual prompt-cache prefix break.

Changes

  • Convert a byte position to a text position before comparing parsed JSON spans and locating message evidence.
  • If a differing byte lies inside a UTF-8 code point, anchor the displayed evidence at that code point's start.
  • Keep public byte offsets, prefix measurements, attribution, and byte-based insertion checks in their existing byte coordinates.
  • Add regressions for messages/input/contents, CJK and emoji prefixes, real dump-file reports, and healthy append/ASCII controls.

This changes offline diagnosis only; it does not rewrite requests, change cache matching, or add configuration.

Validation

On unchanged master 9674cfa, the new suite reports 8 failures and 2 passing controls. With the fix, all 29 tests across the new Unicode suite and the existing cache-diff suite pass, including the real loopback HTTP dump-name check.

Passed: npm run typecheck; npm run build; npm run check:unused-exports; npm run check:test-import-graph; git diff --check.

The zero-token Codex preflight finds the installed CLI and built distribution, but its local model endpoint is unreachable; the real-model case is skipped. No full real-model E2E or upstream CI pass is claimed.

Prior work and submission

Checked 61 author PRs, 162 fork heads, the complete acp-cache-diff history, and cache-diff/offset/Unicode PR and issue searches. Prior #2870 and #2341 concern the live cache-ledger module; they do not fix this offline byte-to-text coordinate mismatch.

Current master integration

Merged upstream master fcc2e848486cc2152e36b6658c517fbfa51ec428 into this branch without textual conflicts. The published merge commit is 795c1b12449b3feaa7056b5e89eaf7062d932a35, with the original PR head b6c73fa3da5fc4e6e3b25688e9c6b1c7a164e3f3 as its first parent and current master as its second parent.

The cache-diff fix and its regression test file are byte-for-byte unchanged from the first parent; this update adds the current upstream history.

Fresh integration validation on Linux x64 / Node 24.22.1, after installing the locked dependencies:

  • All 29/29 tests across the focused suites above pass.
  • npm run typecheck, npm run build, npm run check:unused-exports, npm run check:test-import-graph and git diff --check pass.
  • The published tree cd679ce3c95c1d88548e0ac429b392b8b932ada6 exactly matches the locally tested tree.

The integration commit triggered hosted CI run 43377353291; its current result is available there. Complete hosted CI and real-model E2E are not claimed as passed by these local integration checks.

Review status

Linked to #6495 and ready for maintainer review. The latest integration validation includes upstream master fcc2e848486cc2152e36b6658c517fbfa51ec428, as recorded above. Current hosted results are available in the Checks tab.

Please apply the existing ci:full label to run the FULL matrix, including the required Windows / Node 24 checks. The default CORE matrix does not produce those checks.

@ranxianglei ranxianglei changed the title fix: interpret cache-diff byte offsets in original text coordinates [PR] fix: interpret cache-diff byte offsets in original text coordinates Nov 13, 2026
@Afloat16
Afloat16 marked this pull request as ready for review November 13, 2026 04:46
ework-agent and others added 13 commits November 14, 2026 04:35
The suite baseline preload (preload-storage-baseline.mjs) pins
BILI_STORAGE_UNIFIED="legacy" for the broad corpus. After ranxianglei#6480 flipped the
storage default to pure-sqlite, the notice-registry suite's visibleNotices
assertions expect the pure-sqlite default notice, which is gated on
unifiedSelectionIsSqlite (unified "sqlite" or absent) — the preload's
"legacy" made the notice silent, so the suite was born-red on master. Pin
the selection explicitly (ranxianglei#4011 lesson: a test that depends on the production
default passes locally but fails in CI).
…test

The dsh plugin lane (ci-e2e-dsh.yml) runs node --import tsx --test
WITHOUT the preload-storage-baseline.mjs preload, so BILI_STORAGE_UNIFIED
is unset and the storage mode defaults to 'full'. Under the pure-sqlite
default flip (5a80e2b, ranxianglei#6480), 'full' mode turns the JSON envelope
writer OFF — so the .content-store.json sidecar is never written, and
the test's readdirSync(namespace) hits ENOENT.

Pin BILI_STORAGE_JSON_WRITER=on in the test so the content-store
sidecar is written regardless of the storage mode. This keeps the
corrupted-store detection path exercised in both full and legacy modes.

Closes: dsh plugin lane red on master (run 43532980164, job 132255459726)
The pure-sqlite default flip (5a80e2b, ranxianglei#6480) turns the JSON envelope
writer OFF, so the .json session file this lane asserts on is never
written and 'proxy must persist the session' / 'no persisted session
carried compressed blocks' fail after the 20s wait. Pin the writer ON in
the spawned client's env (the bootstrapped proxy inherits it) so the
envelope this test was written for is still produced.

Closes: native opencode lane red on master (run 43547593803, job 132302438610)
Same pure-sqlite default flip (5a80e2b, ranxianglei#6480) as the opencode lane:
the JSON envelope writer is OFF, so 'proxy must persist the session
within 20000ms' fails — the on-disk .json session file is never written.
Pin the writer ON in the spawned dsh client's env (the bootstrapped
proxy inherits it).

Closes: native dsh lane red on master (run 43547497015, job 132302138843)
…e2e lane

Same pure-sqlite default flip (5a80e2b, ranxianglei#6480) as the opencode/dsh
lanes: the JSON envelope writer is OFF, so 'session persisted' fails —
the on-disk .json session file is never written. Pin the writer ON in
the spawned pi client's env (the bootstrapped proxy inherits it).

Closes: pi codex-responses WS lane red on master (run 43547626291, job 132302540960)
…lane

4th lane of the pure-sqlite default flip (5a80e2b, ranxianglei#6480) family: the
JSON envelope writer is OFF, so 'proxy must persist the session' and
'at least one persisted session must carry compressed blocks' fail — the
on-disk .json session file is never written. Pin the writer ON in the
spawned pi client's env (the bootstrapped proxy inherits it).

Closes: pi package-native lane red on ci-fixes (run 43558179054, job 132336647955)
Same pure-sqlite default flip (5a80e2b, ranxianglei#6480) family: the JSON writer
is OFF, so the per-session CCR content-store sidecar (.content-store.json)
that 'child persists its own companion and survives a proxy restart'
asserts on is never written (0 !== 1). The earlier b1bc17b pin only
covered tests/plugin-fork.test.ts, not this file. Pin the writer ON at
module load (before any SessionStore is constructed).

Closes: dsh plugin lane red on ci-fixes (run 43558179102, job 132336519539)
…lane

Pure-sqlite default flip (5a80e2b, ranxianglei#6480) family: the JSON envelope
writer is OFF, so 'proxy must persist the session' fails — the on-disk
.json session file is never written. Pin the writer ON in the spawned
opencode client's env (the bootstrapped proxy inherits it).
Pure-sqlite default flip (5a80e2b, ranxianglei#6480) family: the JSON envelope
writer is OFF, so the on-disk .json session envelope this lane asserts on
(exactly ONE persisted session under the raw key) is never written. Pin
the writer ON in the spawned dsh client's env.
Pure-sqlite default flip (5a80e2b, ranxianglei#6480) family: the JSON envelope
writer is OFF, so the on-disk .json session file this lane asserts on
(state survival across the OLD→NEW update) is never written. Pin the
writer ON in the spawned proxy's env (both update phases share envBase).

@ranxianglei ranxianglei left a comment

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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

APPROVE — correct, minimal, well-tested root-cause fix.

Verified locally on the head (which is merged up to current master 1ff89e8785 + ci-fixes, so no stale-base concern):

  • New Unicode suite: 8 tests fail on unchanged master, all 10 pass with the fix (teeth confirmed — I ran both).
  • Existing cache-diff suite: all 19 pass (no regression).
  • tsc --noEmit clean.

The fix adds textOffsetAtByte to convert the UTF-8 byte offset to a UTF-16 text offset (anchoring at a code-point start when the differing byte lands inside a multibyte char), and uses it for the JSON-span comparisons in compareWires and the locateMessage/snippetAround evidence in classifyPair. Public byte offsets, prefix measurements, attribution, and the byte-based isMidInsertionAppend check stay in byte coordinates — exactly the scope #6495 asks for. This fixes the coordinate mismatch at the source, not a symptom.

The 10 BILI_STORAGE_JSON_WRITER / BILI_STORAGE_UNIFIED test pins are the sanctioned ci-fixes merge (inherited failures from the pure-sqlite default #6480, which is already in master with those lanes still unpinned) — not unrelated churn.

Note for the maintainer: the author requested the ci:full label for the full Windows/Node 24 matrix; the CORE matrix is green. Label application is your call.


审核八问

  1. 是什么问题:离线 bili acp-cache-diff 诊断算出的是 UTF-8 字节偏移,却拿它去比 UTF-16 的 JSON span 索引、按字节下标切 JS 字符串;长 CJK/emoji 前缀会把「改写已有消息」误判成「正常尾部追加」,还丢消息索引、显示无关/空证据。
  2. 什么原因导致的:字节坐标与 UTF-16 代码单元坐标混用——prefixCompare 返回字节偏移,而 elementSpans/snippetAround/locateMessage 都在 UTF-16 字符串上按代码单元索引;多字节前缀下两者数值不等,比较/切片全部错位。
  3. 影响范围与严重程度:仅离线诊断的分类与证据展示,不涉及线上请求改写、缓存匹配或配置;触发条件 = 消息数组前存在长 CJK/emoji 前缀且发生改写。严重度:诊断准确性瑕疵(误分类 + 证据错位),不破坏可用性、不丢数据。
  4. 怎么修复的:新增 textOffsetAtByte 把字节偏移换算成 UTF-16 文本偏移(落在多字节码点内部时回退到该码点首字节,避免把残缺前缀解码成替换符);compareWires 三处 span 比较、classifyPair 的 locateMessage/snippetAround 改用文本偏移;公开字节偏移、前缀度量、归因、isMidInsertionAppend 字节检查保持字节坐标不变。
  5. 是否有回归问题:无。既有 19 条 cache-diff 测试全绿;改动只把「对 span/字符串的操作」从字节坐标切到文本坐标,字节坐标逻辑(前缀比较、归因、mid-insert 判定)一字未动。高危模式逐项过:无 fail-fast 变吞错、无阈值/时序/输出格式漂移(公开 byteOffset 字段仍是字节值)。
  6. 是否破坏缓存:不涉及。纯离线诊断,不触碰缓存/重试/增量游标/去重键。
  7. 是否改了设计方向:不涉及。无架构/边界/数据流转向。
  8. 是否引入了新的配置:不涉及。无新 env/config/默认值。

判定:可合(APPROVE)。

@ranxianglei

Copy link
Copy Markdown
Owner

🤖 Powered by ework · qwen3.8-27b

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

Reviewed PR #6496 (fix: interpret cache-diff byte offsets in original text coordinates) end-to-end and APPROVED it — GitHub review id 5902772035, posted on the PR thread.

What I verified (locally, on the head merged up to current master 1ff89e8785 + ci-fixes, so no stale-base concern):

  • New Unicode suite: 8 tests fail on unchanged master, all 10 pass with the fix (teeth confirmed by running both).
  • Existing cache-diff suite: all 19 pass (no regression).
  • tsc --noEmit clean.

Correctness: the fix adds textOffsetAtByte (byte→UTF-16 offset, anchoring at a code-point start when the differing byte is mid-codepoint) and uses it for the JSON-span comparisons in compareWires and the locateMessage/snippetAround evidence in classifyPair. Public byte offsets, prefix measurements, attribution, and the byte-based isMidInsertionAppend check stay in byte coordinates — exactly the scope #6495 asks for. Root-cause fix, not a symptom mask.

Diff: core fix (src/acp-cache-diff.ts + new test) is clean; the 10 BILI_STORAGE_JSON_WRITER/BILI_STORAGE_UNIFIED test pins are the sanctioned ci-fixes merge (inherited failures from the pure-sqlite default #6480, already in master with those lanes unpinned). No kernel/ changes → the kernel authorization boundary does not apply.

For the maintainer: the author requested the ci:full label for the full Windows/Node 24 matrix (CORE is green); label application is your call.


审核八问

  1. 是什么问题:离线 bili acp-cache-diff 诊断算出的是 UTF-8 字节偏移,却拿它去比 UTF-16 的 JSON span 索引、按字节下标切 JS 字符串;长 CJK/emoji 前缀会把「改写已有消息」误判成「正常尾部追加」,还丢消息索引、显示无关/空证据。
  2. 什么原因导致的:字节坐标与 UTF-16 代码单元坐标混用——prefixCompare 返回字节偏移,而 elementSpans/snippetAround/locateMessage 都在 UTF-16 字符串上按代码单元索引;多字节前缀下两者数值不等,比较/切片全部错位。
  3. 影响范围与严重程度:仅离线诊断的分类与证据展示,不涉及线上请求改写、缓存匹配或配置;触发条件 = 消息数组前存在长 CJK/emoji 前缀且发生改写。严重度:诊断准确性瑕疵(误分类 + 证据错位),不破坏可用性、不丢数据。
  4. 怎么修复的:新增 textOffsetAtByte 把字节偏移换算成 UTF-16 文本偏移(落在多字节码点内部时回退到该码点首字节,避免把残缺前缀解码成替换符);compareWires 三处 span 比较、classifyPair 的 locateMessage/snippetAround 改用文本偏移;公开字节偏移、前缀度量、归因、isMidInsertionAppend 字节检查保持字节坐标不变。
  5. 是否有回归问题:无。既有 19 条 cache-diff 测试全绿;改动只把「对 span/字符串的操作」从字节坐标切到文本坐标,字节坐标逻辑(前缀比较、归因、mid-insert 判定)一字未动。高危模式逐项过:无 fail-fast 变吞错、无阈值/时序/输出格式漂移(公开 byteOffset 字段仍是字节值)。
  6. 是否破坏缓存:不涉及。纯离线诊断,不触碰缓存/重试/增量游标/去重键。
  7. 是否改了设计方向:不涉及。无架构/边界/数据流转向。
  8. 是否引入了新的配置:不涉及。无新 env/config/默认值。

判定:可合(APPROVE)。

Resolve CI baseline conflicts using the current upstream test setup.

@ranxianglei ranxianglei left a comment

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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

Re-review of the refreshed head 26723e1bce47cf318aa285ab043bcfc1da5abd91 — still APPROVE.

The head moved from 49b74242e to 26723e1bce purely via a master merge (commit 26723e1bce "Merge master into …"). I verified:

  • The core fix (src/acp-cache-diff.ts + tests/acp-cache-diff-unicode.test.ts) is byte-identical to the head I approved — git diff 49b74242ec 26723e1bce -- src/acp-cache-diff.ts tests/acp-cache-diff-unicode.test.ts is empty.
  • The PR is now up-to-date with master (0 commits behind; origin/master is an ancestor of the new head).
  • The master merge touches src/storage/store.ts, test pins, and release files — none of which touch the core fix or its test.
  • Re-ran the targeted suites on the new head: 10/10 Unicode tests pass, 19/19 existing cache-diff tests pass, tsc --noEmit clean.

The fix remains a correct, minimal, well-tested root-cause fix: textOffsetAtByte converts the UTF-8 byte offset to a UTF-16 text offset (anchoring at a code-point start when the differing byte is mid-codepoint) and is used only for the JSON-span comparisons in compareWires and the locateMessage/snippetAround evidence in classifyPair. Public byte offsets, prefix measurements, attribution, and the byte-based isMidInsertionAppend check stay in byte coordinates. No kernel/ changes, no behavior drift in the live path, no new config.

For the maintainer: the author's earlier request for the ci:full label (full Windows/Node 24 matrix) is still open; CORE is green. Label application is your call.

@ranxianglei

Copy link
Copy Markdown
Owner

🤖 Powered by ework · qwen3.8-27b

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

Re-review of the refreshed head 26723e1bce — still APPROVE (GitHub review id 5903611283, posted on the PR thread).

The head moved from 49b74242e to 26723e1bce purely via a master merge (commit 26723e1bce "Merge master into …"). I verified:

  • The core fix (src/acp-cache-diff.ts + tests/acp-cache-diff-unicode.test.ts) is byte-identical to the head I approved — the diff between the two heads on those files is empty.
  • The PR is now up-to-date with master (0 commits behind; origin/master is an ancestor of the new head).
  • The master merge touches src/storage/store.ts, test pins, and release files — none of which touch the core fix or its test.
  • Re-ran the targeted suites on the new head: 10/10 Unicode tests pass, 19/19 existing cache-diff tests pass, tsc --noEmit clean.

The fix remains a correct, minimal, well-tested root-cause fix: textOffsetAtByte converts the UTF-8 byte offset to a UTF-16 text offset (anchoring at a code-point start when the differing byte is mid-codepoint) and is used only for the JSON-span comparisons in compareWires and the locateMessage/snippetAround evidence in classifyPair. Public byte offsets, prefix measurements, attribution, and the byte-based isMidInsertionAppend check stay in byte coordinates. No kernel/ changes, no behavior drift in the live path, no new config.

For the maintainer: the author's earlier request for the ci:full label (full Windows/Node 24 matrix) is still open; CORE is green. Label application is your call.


审核八问

  1. 是什么问题:离线 bili acp-cache-diff 诊断算出的是 UTF-8 字节偏移,却拿它去比 UTF-16 的 JSON span 索引、按字节下标切 JS 字符串;长 CJK/emoji 前缀会把「改写已有消息」误判成「正常尾部追加」,还丢消息索引、显示无关/空证据。
  2. 什么原因导致的:字节坐标与 UTF-16 代码单元坐标混用——prefixCompare 返回字节偏移,而 elementSpans/snippetAround/locateMessage 都在 UTF-16 字符串上按代码单元索引;多字节前缀下两者数值不等,比较/切片全部错位。
  3. 影响范围与严重程度:仅离线诊断的分类与证据展示,不涉及线上请求改写、缓存匹配或配置;触发条件 = 消息数组前存在长 CJK/emoji 前缀且发生改写。严重度:诊断准确性瑕疵(误分类 + 证据错位),不破坏可用性、不丢数据。
  4. 怎么修复的:新增 textOffsetAtByte 把字节偏移换算成 UTF-16 文本偏移(落在多字节码点内部时回退到该码点首字节,避免把残缺前缀解码成替换符);compareWires 三处 span 比较、classifyPair 的 locateMessage/snippetAround 改用文本偏移;公开字节偏移、前缀度量、归因、isMidInsertionAppend 字节检查保持字节坐标不变。
  5. 是否有回归问题:无。既有 19 条 cache-diff 测试全绿;改动只把「对 span/字符串的操作」从字节坐标切到文本坐标,字节坐标逻辑(前缀比较、归因、mid-insert 判定)一字未动。高危模式逐项过:无 fail-fast 变吞错、无阈值/时序/输出格式漂移(公开 byteOffset 字段仍是字节值)。本轮新增的 master 合并不触碰核心修复或其测试,无新增回归。
  6. 是否破坏缓存:不涉及。纯离线诊断,不触碰缓存/重试/增量游标/去重键。
  7. 是否改了设计方向:不涉及。无架构/边界/数据流转向。
  8. 是否引入了新的配置:不涉及。无新 env/config/默认值。

判定:可合(APPROVE)。

@ranxianglei
ranxianglei merged commit 1bee7c0 into ranxianglei:master Nov 14, 2026
50 checks passed
@ranxianglei

Copy link
Copy Markdown
Owner

Superseded by #6677 (merged as 3858b6956, 2026-11-14).

Landed into master via the real merge commit 1bee7c007f0 (Merge Afloat16/2026-11-12_cache-diff-byte-offsets (PR #6496)), so your original commits are preserved verbatim in master history — only hashes of the batch wrapper changed, your authorship and commit messages are intact.

The fix for #6495 is now in master. Closing this PR as superseded — thanks for the careful, well-tested fix.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

cache-diff misclassifies Unicode-prefixed message rewrites as healthy appends

2 participants