perf(chat): seek cached events by sequence - #4127
Conversation
8587ba9 to
fb59325
Compare
|
@huangruiteng 当前 head 2fb24a8 已同步 main,并带入同一已验证的 Node 26 fence-error 稳定化修复。验证:业务回归 1/1、fence parity 22/22、Ruff 与 diff check 通过;新 CI 已触发,请按此 head 进行独立技术复审。 |
73ee920 to
357a362
Compare
Signed-off-by: duanjialing.777 <duanjialing.777@bytedance.com>
357a362 to
bbaccd4
Compare
Signed-off-by: duanjialing.777 <duanjialing.777@bytedance.com>
Review — head
|
|
Reviewer note (PR body could not be edited by this account — leaving the delta here for the author/maintainer to fold in): Summary — add one bullet:
Issue Or Task — add:
Validation — add (verified on head
Type of Change: consider checking |
huangruiteng
left a comment
There was a problem hiding this comment.
动机
这个 PR 优化聊天事件游标的缓存命中路径:调用方只需要某个 sequence 之后的事件,不应先复制整段缓存再线性扫描前缀。审阅绑定 exact head 2fb24a891325b53f4191669f18e2240c5706dce5。
改动思路
实现利用现有事件序列单调递增的不变量,对缓存中的 sequence 使用 bisect_right 定位边界,再只返回所需尾段;新增用不可迭代前缀覆盖“不能退化为线性扫描”,并用有间隙序列覆盖游标语义。附带的 legacy writer-fence 文本归一化不改变 reason code 或 fail-closed 权限语义,并且该部分已进入当前 main,所以合并后的有效增量主要是 chat cursor 优化。
具体改动
loopx/chat_store.py:缓存命中时由“复制全列表 + 线性过滤”改为二分定位后切片,保留非法游标、空结果和 sequence gap 行为。tests/test_chat_event_cursor.py:新增 gapped sequence(1/4/7)与不可迭代前缀回归,证明不会扫描游标前的事件。legacy_writer_fence.ts:从底层文件系统错误文本中去除本地路径;现有 22 行 caller-parity 已实际构造不可读目录并覆盖该正向路径。
关键代码讲解
flush_events 仍在文件锁内分配 sequence、追加 JSONL,并以新列表替换缓存引用;compaction 也保持顺序。因此 events_after 消费的是已排序序列,bisect_right 对“严格晚于 cursor”与原实现一致。独立基线/新实现矩阵覆盖 None、边界、间隙、越界和非法 cursor,共 10/10 等价。
对主干的风险
我在 exact head 上运行了 61 个 Python 测试和 22 个 TypeScript caller-parity 测试,另做了 10/10 基线等价矩阵;在最新 main 的合成合并树上又通过 61 个 Python 测试与 22 个 TypeScript 测试。Ruff、diff check 与远端必需检查均通过。
非阻塞 P2:二分查找依赖 sequence 单调递增,这个约束目前由写入结构保证但没有在生产者附近明示。建议后续在 sequence 分配或二分调用旁补一条简洁不变量注释;已有 #4192 跟踪,无需为本 PR 增加运行时断言或新框架。
我的整体评价
这是一个边界清楚、行为保持且有真实性能回归覆盖的优化。默认行为、权限语义、错误分类和游标兼容性均未被悄然改变;相关 future-facing 检查已做,除已登记的不变量注释外不需要扩大范围。结论:APPROVE。
English verdict: APPROVE for exact head 2fb24a891325b53f4191669f18e2240c5706dce5. The cache-hit cursor path now seeks by monotonic sequence and returns only the required tail, with exact-head and current-main integration coverage. The only follow-up is a non-blocking P2 to document the sorted-sequence invariant already tracked in #4192.
Summary
sequenceto binary-search the cached cursor position.loadLegacyCoordinationWriterFenceread-failure reasons: strip the trailing absolute local path from Node fs error messages so the publicreasonmatches the frozen parity-fixture contract and does not leak local machine paths. (commit2fb24a89,legacy_writer_fence.ts:145-152)Issue Or Task
Validation
uv run --no-project --with ruff ruff check loopx/chat_store.py tests/test_chat_event_cursor.pyscripts/loopx canary premerge --from-git-diffgit diff --checktests/test_chat_event_cursor.py1/1; fence caller-parity TS 22/22; shadow fence parity e2e 26/26; chat store input-validation + active-turn 32/32; remote CI 19 checks, 0 failures (verified on head2fb24a89)Type of Change
LoopX Area
Technical Direction
Core control-plane hardening
Long-horizon benchmark evidence
Operator surface and IM integration
Shared Goal Authority and cross-host coordination
Architecture and research incubator
Target base branch:
mainDirection tracker or promotion unit: N/A
Shared-authority RFC fixture impact
N/A. This change does not alter the TypeScript control-plane migration or shared Goal Authority fixtures.
Boundary Checklist
.loopx/,.codex/goals/, liveACTIVE_GOAL_STATE.md, credentials, private benchmark traces, verifier output, raw agent sessions, internal document links, or local machine paths.Signed-off-bytrailer (git commit -s).