Repository navigation
fix(cursor): transcript 桥不再越过 turn_ended footer,偏移按原始字节计算 - #2009
Merged
Merged
Conversation
cursor-agent 的 agent-transcript JSONL 并非整体 append-only:每轮结束在文件尾
追加一行 {"type":"turn_ended",...} 状态 footer,下一轮先把它截掉、从其原字节
位置重写(footer 挪到新文件尾)。排水器按字节 offset 增量读,上一轮结束后
offset 停在 footer 之后,第 2 轮起的 user 行起始于 offset 之前,只能被读成
解析失败的行中残片 → 指纹匹配不上,该轮归因失败。adopt(EOF 基线)与零注入
resume(baselineJsonlCursor)的 attach 基线同样停在 footer 之后,attach 后
第一轮即丢 user 行。
另外,过期 offset 在重写后可能落进多字节字符中间(error/aborted footer 长达
86~287 字节,足以越过 user 行的 ASCII 前缀),原先「解码后再
Buffer.byteLength 反推字节数」的算法把每个孤立续字节算成 3 字节的 U+FFFD,
后续行起点与 newOffset 整体漂移(复现中越过 EOF 4 字节)。
改动:
- drainCursorTranscript:所有偏移按原始 0x0a 位置计算;newOffset 永不越过尾部
footer(完整行或可解析的无换行尾巴),footer 每次轮询重读、零事件、不重复;
其后出现 message 行即结束 footer 段,offset 照常前进
- 新增 baselineCursorTranscript:单次读取尾部 64 KiB 原始字节决定基线并回退到
尾部 footer 起点;探针起点切在行中/多字节字符中间时丢弃首段,偏移不漂移;
skipTail 保留 adopt「跳过旧 in-flight 尾巴」语义(footer 尾巴除外),
否则与 baselineJsonlCursor 一致
- worker codexBridgeAttach:cursor 的 skip-tail(adopt)与 baseline-existing
(零注入 resume)两条基线改用该快照,不再另做一次 stat
拆自 #932 的 e279f20,只取 turn_ended / 字节 offset 修复(不含默认模式开
兜底与归因租约刷新),并按该 PR 评审意见改为原始字节计算、单快照基线。
影响面:drain/baseline 为 cursor 专属;codexBridgeAttach 两处改动以
structuredBridgeIs('cursor') 守卫,其它 structured CLI 行为不变;jsonl-cursor
仅改注释;worker-structured-lifecycle-status 的 codexBridgeAttach 函数边界截取
补一条边界存在性断言。
Co-authored-by: deepcoldy <[email protected]>
Co-authored-by: Cursor <[email protected]>
Co-Authored-By: Claude Opus 5.5 <[email protected]>
baselineCursorTranscript 用阻塞的 openSync(path, 'r') 打开 transcript:若发现 的路径是 FIFO,open(2) 会无限等待写端,Cursor resume 时 codexBridgeAttach 同步 调用它,worker 卡死,catch 与回退都到不了(旧的 stat size 基线对 FIFO 得到 0、 立即返回)。drainCursorTranscript 先 stat 再 open,静态 FIFO 因 size 0 不会走到 open,但 stat→open 之间路径被换成 FIFO 时同样会阻塞。 两处统一改为 openRegularFile:以 O_RDONLY|O_NONBLOCK 打开,并用已打开 fd 的 fstat().isFile() 校验(无 stat→open 替换窗口)。FIFO / 目录 / 设备按无内容处理: 基线返回 offset 0(与旧 stat size 基线对 FIFO 的结果一致),drain 保持原 offset。 drain 改为同一 fd 上 fstat 取 size 再读,打开失败按原「缺失/不可读」语义保持 offset,读错误照旧上抛。 测试:新增目录用例;FIFO 用例放在 ts-runner 子进程里探测(60s 超时、SIGKILL), 回归时以超时失败而不是卡死测试进程。 Co-Authored-By: Claude Opus 5.5 <[email protected]>
deepcoldy
requested review from
47seek,
LucasIcarus,
xiaoxueSunn and
xiongz-c
as code owners
October 18, 2026 15:57
零注入 Cursor 会话 resume 时(baseline-existing,skipTail=false),若 transcript 最后一条记录是完整的 message 但没有结尾换行,基线把它留在 pending tail:单条 记录的文件返回 offset 0(baselineJsonlCursor 对无换行文件返回 EOF),多条记录 时停在最后一条的行首。Cursor 的 drain 会把可解析的 EOF 对象当作新事件消费,于是 这条历史 prompt / 回复以新时间戳被重新发出,CodexBridgeQueue 随之合成一个实时 本地 turn,或误匹配指纹相同的排队重试,而实际并没有新输入。 非 footer 尾巴恢复为历史跳过:完整记录(两种模式)、以及整个窗口没有任何换行 (baselineJsonlCursor 的单条残行规则)都跳到快照末尾;只有 footer 尾巴(完整或 正在写)回退到 footer 起点;换行之后的未写完残片在 resume 模式下仍与 baselineJsonlCursor 一致留作 pending。 测试:单条无换行 user 记录、单条无换行 footer、多条中最后一条无换行、单条无换行 残片,均断言基线与后续 drain 不重放历史;测试辅助 trailingFooterStart 支持无换行 footer。 Co-Authored-By: Claude Opus 5.5 <[email protected]>
Owner
Author
|
补充修复(2e421686f):零注入 resume 的基线(skipTail=false)原先会把「最后一条完整记录但无结尾换行」留作 pending tail——单条记录的文件得到 offset 0(旧 baselineJsonlCursor 为 EOF),多条时停在最后一条行首;Cursor 的 drain 会消费可解析的 EOF 对象,导致这条历史 prompt 以新时间戳重发、合成幻影本地 turn 或误匹配同指纹重试。现在非 footer 尾巴一律按历史跳过(完整记录、或整个文件无换行),只有 footer 尾巴回退到 footer 起点;换行后的未写完残片在 resume 模式仍与 baselineJsonlCursor 一致。新增单条无换行 user / 单条无换行 footer / 多条末条无换行 / 单条无换行残片 4 例回归,变异验证(回退为旧逻辑 4 例变红)。bun run build、相关 15 个 vitest 文件 475/475、bun test 逐文件均通过。 |
|
🚀 Released in v3.47.0 |
deepcoldy
added a commit
that referenced
this pull request
Oct 20, 2026
…ebase 于 #2009) #2009 已在 cursor 侧落地 turn_ended footer 回退、原始字节偏移、64KiB 有界 baselineCursorTranscript 与短读容忍,attach 对任何 EOF 形状都能确定性决策 (只有文件缺失时身份留 pending,由既有 1s poller 解析)。本 commit 在其之 上补齐方案 B 剩余部分——把 cursor 正式提入始终开启的结构化兜底,普通注入 模式下模型忘调 botmux send 不再整轮静默,全场景与 codex 对齐。 允许名单与投递能力: - structured-bridge-clis: cursor 从 ZERO_PROMPT 移入 ALWAYS; isStructuredBridgeFallbackActive 删除 cursor 特例;零注入列表仅余 antigravity - supportsTranscriptReplyDelivery('cursor') 随 ALWAYS 变为 true(dashboard/ bot-config 可显式 transcript;缺省仍 send,opt-in 语义不变) - cursor bridgeSpec.spawn 去掉 zeroPromptOnly;StructuredBridgeSpec.spawn 的 zeroPromptOnly 字段整体删除(无第二使用者),spawn 链接链简化为 spawnBridge && codexBridgeFallbackActive() - 文案:dashboard 中英文 unsupported 提示、bot-config enum hint、 reply-delivery / zero-prompt-support 注释补上 cursor 忙时归因租约(codex-bridge-queue): - refreshUnstartedHeadAttributionLease 刷新队列中全部未开始 head,并覆盖 submit-confirmed 租约:cursor argv 烘焙首轮在 init 被 confirmPendingTurn (最常见 spawn 路径),其 user 行同样晚刷;type-ahead 后继此前被前面未 开始的 confirmed head 挡住,会在同一波 prune 双双丢失 - 新增 maxSinceMarkMs 硬上限(worker 传 120s):屏幕 busy 是正则启发式, 吞 Enter/弹窗导致的假 busy 超过标记后 120s 不再续命,head 照常 20s 过 期,队列与 structured_start_timeout 必然恢复;rpc-active / 验证中的 head 保持各自租约 worker: - timer 的 cursor 分支在 CLI 忙(非 adopt)时每秒带 120s 上限刷新全部排队 head;倒计时实际从空闲起算,吞 Enter 保护语义不变 - codexBridgeFallbackActive 增加 scratch 闸门:普通/零注入 spawn 在全根 COW overlay 下宿主读不到 mirror,不激活兜底(botmux send 仍可用,不静默); adopt 不受影响;spawnCli 留存 resolved sandbox 模式,teardown 清除 - notify 收到不同 chatId(/new 轮转或观察器纠正早期 pid 探针的 sibling 误绑)时先排空旧转写再按 fresh/resume 重绑;同 chatId 维持 first-attach-wins - observeCursorCliSessionId 观察到 chatId 后在普通模式也通知结构化桥(此前 是 no-op);flushPending 标记分支注释更新为 cursor 全模式覆盖 测试:queue 新增忙时租约 4 例(慢镜像、argv confirmed 首轮、type-ahead 链 +硬上限、started/rpc-active 边界);structured-bridge-clis/spec/attach- wiring、reply-delivery、bot-config-store、dashboard-ipc、prompt-builder 随 允许名单更新;新增 cursor-plan-b-wiring 6 例源码级断言。直接相关 17 文件 893 例全绿;全量 33357 例的 61 个失败均为 bwrap/实验性 SQLite/满负载 hook 超时类既存环境项(同批文件单独成群在本分支与干净 master 均全绿)。 Co-Authored-By: Claude Code <[email protected]>
deepcoldy
added a commit
that referenced
this pull request
Oct 20, 2026
…ebase 于 #2009) baselineCursorTranscript 与短读容忍,attach 对任何 EOF 形状都能确定性决策 (只有文件缺失时身份留 pending,由既有 1s poller 解析)。本 commit 在其之 上补齐方案 B 剩余部分——把 cursor 正式提入始终开启的结构化兜底,普通注入 模式下模型忘调 botmux send 不再整轮静默,全场景与 codex 对齐。 允许名单与投递能力: - structured-bridge-clis: cursor 从 ZERO_PROMPT 移入 ALWAYS; isStructuredBridgeFallbackActive 删除 cursor 特例;零注入列表仅余 antigravity - supportsTranscriptReplyDelivery('cursor') 随 ALWAYS 变为 true(dashboard/ bot-config 可显式 transcript;缺省仍 send,opt-in 语义不变) - cursor bridgeSpec.spawn 去掉 zeroPromptOnly;StructuredBridgeSpec.spawn 的 zeroPromptOnly 字段整体删除(无第二使用者),spawn 链接链简化为 spawnBridge && codexBridgeFallbackActive() - 文案:dashboard 中英文 unsupported 提示、bot-config enum hint、 reply-delivery / zero-prompt-support 注释补上 cursor 忙时归因租约(codex-bridge-queue): - refreshUnstartedHeadAttributionLease 刷新队列中全部未开始 head,并覆盖 submit-confirmed 租约:cursor argv 烘焙首轮在 init 被 confirmPendingTurn (最常见 spawn 路径),其 user 行同样晚刷;type-ahead 后继此前被前面未 开始的 confirmed head 挡住,会在同一波 prune 双双丢失 - 新增 maxSinceMarkMs 硬上限(worker 传 120s):屏幕 busy 是正则启发式, 吞 Enter/弹窗导致的假 busy 超过标记后 120s 不再续命,head 照常 20s 过 期,队列与 structured_start_timeout 必然恢复;rpc-active / 验证中的 head 保持各自租约 worker: - timer 的 cursor 分支在 CLI 忙(非 adopt)时每秒带 120s 上限刷新全部排队 head;倒计时实际从空闲起算,吞 Enter 保护语义不变 - codexBridgeFallbackActive 增加 scratch 闸门:普通/零注入 spawn 在全根 COW overlay 下宿主读不到 mirror,不激活兜底(botmux send 仍可用,不静默); adopt 不受影响;spawnCli 留存 resolved sandbox 模式,teardown 清除 - notify 收到不同 chatId(/new 轮转或观察器纠正早期 pid 探针的 sibling 误绑)时先排空旧转写再按 fresh/resume 重绑;同 chatId 维持 first-attach-wins - observeCursorCliSessionId 观察到 chatId 后在普通模式也通知结构化桥(此前 是 no-op);flushPending 标记分支注释更新为 cursor 全模式覆盖 测试:queue 新增忙时租约 4 例(慢镜像、argv confirmed 首轮、type-ahead 链 +硬上限、started/rpc-active 边界);structured-bridge-clis/spec/attach- wiring、reply-delivery、bot-config-store、dashboard-ipc、prompt-builder 随 允许名单更新;新增 cursor-plan-b-wiring 6 例源码级断言。直接相关 17 文件 893 例全绿;全量 33357 例的 61 个失败均为 bwrap/实验性 SQLite/满负载 hook 超时类既存环境项(同批文件单独成群在本分支与干净 master 均全绿)。 Co-Authored-By: Claude Code <[email protected]>
deepcoldy
added a commit
that referenced
this pull request
Oct 22, 2026
merge master 后 CI 首跑 test (2/3) 红:test-hygiene 守门(#2057)检出 cursor-plan-b-wiring 的 6 个用例读 src/worker.ts 文本做断言却未标注。这些 断言守的是埋在 worker 闭包里、无行为入口的兜底链路不变量(忙时租约刷新+ 120s 硬上限、scratch 闸门三处读写、spawn 链接链、in-binding 轮转重绑、 observer 全模式通知),与 #2009 的 attach-wiring K3 同形态,按规则在 describe 总注 + 每个 it 上方补 K3 标注。非碰运气 rerun:本地复现红、修复 后 test-hygiene 26 例与 wiring 6 例全绿。
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
改了什么 & 为什么
cursor-agent 的 agent-transcript JSONL 不是整体 append-only 的:每轮结束时会在文件尾追加一行状态 footer
{"type":"turn_ended","status":"success"|"error"|"aborted",...}(40~300 字节)。下一轮开始写之前,cursor 先把这行 footer 截掉,从它原来的字节位置写入新一轮的内容,footer 随后被挪到新的文件尾。这带来两个 bug:
baselineJsonlCursor,两者都停在 footer 之后,attach 后的第一轮就会丢掉 user 行。Buffer.byteLength反推字节数」的算法会把每个孤立的 UTF-8 续字节算成 3 字节的 U+FFFD,导致后续行起点和newOffset整体漂移(复现中越过 EOF 4 字节)。本 PR 拆自 #932 的 e279f20,只取 turn_ended / 字节 offset 修复,并按该 PR 评审意见做了两处调整:偏移改在原始字节上计算;基线改为单快照决定。
drainCursorTranscriptnewOffset永远不越过尾部 footer(完整的 footer 行,或可解析的无换行 footer 尾巴都算)。baselineCursorTranscript(path, { skipTail })skipTail(adopt)保留原有的「跳过旧 in-flight 尾巴」语义,footer 尾巴除外;不带skipTail时结果与baselineJsonlCursor一致。workercodexBridgeAttachO_RDONLY | O_NONBLOCK打开,并对已打开的 fd 用fstat().isFile()校验,只接受普通文件。路径是 FIFO、目录或设备时按无内容处理,不会让 worker 阻塞在 open(2) 上。明确不带入:#932 的主体(默认注入模式也开 transcript 兜底,与 master 的有意设计相反)、
refreshUnstartedHeadAttributionLease归因租约刷新、以及后续 defer 状态机。影响面评估
cursorLateAttachMode的 birthtime 判定和 preamble 没有改。promptInjection: 'none'):fresh spawn 的排水、resume 的 baseline-existing、计时器和observeCliSessionId的迟到 attach 都受益。codexBridgeAttach的两处改动都以structuredBridgeIs('cursor')守卫,非 cursor 的行为逐字不变。共享的jsonl-cursor只改了注释。canRecover,而canRecover本来就排除了 cursor,所以 cursor 既不走planBridgeTurnRecovery,也不走其中的skipRecovery,与本 PR 的基线和 footer 回退没有交互。cursor 标记时记下的offsetAtMark只会写进 journal,不会被 cursor 消费。测试验证
构造复现(模拟每轮「截 footer → 原位重写 → 再追加 footer」)
assistant_final,user 行丢失newOffset越过 EOF 4 字节真 cursor-agent 实测(2026.09.23,同一 chat 用
--resume跑 3 轮,每轮之间排水一次)本机 96 份 transcript 普查:
turn_ended;命令与结果
bun run build:通过。cursor-transcript、structured-bridge-attach-wiring、worker-structured-lifecycle-status、jsonl-cursor、codex-subagent-usage-limit、cursor-cot-wiring、structured-bridge-spec、structured-bridge-clis、codex-bridge-queue、bridge-fallback-gate、cursor-usage、traex-worker-bridge-wiring、codex-worker-bridge-wiring、bridge-turn-journal、codex-adopt-recovery。bun test逐文件跑 4 个相关文件,全部通过。新增测试
skipTail语义,以及与baselineJsonlCursor的一致性。CodexBridgeQueue:零注入多轮归因、adopt 首轮归因。变异验证(临时回退修复,确认测试变红)
isFile校验残留问题
stripCursorReasoningLeak拦不住,需要单独跟进。🤖 Generated with Claude Code