Repository navigation
feat(session): 新增只读结构化会话轮次接口 - #3597
Conversation
- turns.sqlite 改用 WAL + busy_timeout 3000ms,与 session-store 等跨进程 存储对齐:多进程饱和竞争下复现过 SQLITE_BUSY 逃逸(DELETE 日志 + 500ms), WAL 下同负载对照零错误,读不再阻塞写 - reactivateClosedSession 的 resumeSessionTurns 包 try/catch,与其余所有 调用点一致:损坏/锁故障的只读旁路存储不再阻断会话重开(NOTADB 探针复现) - 补两条回归用例,摘除守卫均咬红 Co-Authored-By: Claude Fable 5 <[email protected]>
自动评审初步意见(首审)整体评价:这是「结构化会话轮次」两步计划的第一步,只读接口 + 事件溯源式 SQLite 存储,设计克制(capabilities 为空、owner actor 留第二步),鉴权边界 fail-closed,测试覆盖了合并、迟到回执、关闭栅栏、重启补清理、游标和身份拒绝等关键行为。以下两处 P2 已按仓库约定直接追加修复到本分支(commit P2-1:turns.sqlite 未启用 WAL,busy_timeout 仅 500ms
探针证据(bun 引擎、fenced 环境、2 写者各 50 轮 + 1 读者):
P2-2:reactivateClosedSession 中 resumeSessionTurns 无异常保护其余所有调用点(closeSession、daemon/worker/trigger 的写入包装)都对该旁路存储做了 try/catch best-effort,唯独 修复与验证
不阻断的观察项(供后续增量参考)
另外:评审前已在本地基于最新 |
- events 表新增按整轮(turn_id)粒度的单会话上限(默认 2000 条)与 TTL(默认 7 天)增量裁剪,写入时按需触发:优先淘汰已过期或最旧的已完成轮次,保护当前/最新及未完成活跃轮次,同步清理对应 pending_inbound / pending_native,保持 seq 游标单调递增与读模型一致 - publicMessageText 扩展本地路径匹配边界,覆盖全角/半角冒号、等号、中英文括号与引号等非空白引导符后的本地路径与 Windows 盘符路径,同时保留闭合标点且不误伤 http/https 等协议 URL - 补齐单测覆盖上限/TTL 裁剪、活跃轮次保护、游标分页单调性、关闭重开一致性及路径脱敏正反例
|
已基于
本地验证
|
deepcoldy
left a comment
There was a problem hiding this comment.
复审结论(head ee1a218fdd71)
发现 3 个 P2,均有实际探针复现;前两处会让已经执行的用户消息在只读轮次接口中永久漏记,第三处使新增的事件硬上限在单轮次会话中失效。
1. P2 — deferred submit 确认成功后没有补记轮次输入
位置:src/worker.ts:24637、src/worker.ts:22633-22669
正常写入只有在 result?.submitted !== false 时才调用 recordStructuredInput。当 Codex 的 3.2 秒 in-band history 轮询未命中、返回 { submitted: false, recheck } 后,后续 scheduleSubmitFailureNotify 的 suppress-confirmed 分支只补 acknowledgeNativeInputConsumed / onConfirmed,没有调用 recordStructuredInput。而 updateSessionTurnNativeAnchor 在没有既有 anchor 时只写 pending_native,读接口又要求存在 input:* 事件,因此该轮次始终不可见;后续回执也无法让它出现在快照中。
探针:复用真实 worker + fake Codex 的 lazy-rollout 夹具,让 history 延迟 6 秒,确保只有 deferred recheck 能看到提交。探针中 CLI final、native_input_consumed 和 cli_session_id 均正常产生,但 readSessionTurns(...).turns 为 []。
建议在 deferred 确认成功路径补记当时准备好的 submittedText 与 structuredSourceTurnIds(不要改用 logicalMsg,Codex 准备阶段可能追加 correlation marker),并覆盖 observeQueuedActivationReceipt 这条独立确认通道;可用上述现有 integration 夹具加读模型断言。
2. P2 — opening activation 合并多条缓冲消息时只保留最后一条 inbound
位置:src/daemon.ts:22667-22721
releaseQueuedActivationReservationNow 把 pendingFollowUps 合成一个 CLI turn,并用 bufferedTurnIds 的最后一只作为 turnId,但这里既没有像 buildReservedInitialInput 那样调用 mergeStagedSessionTurnInputs,也没有把来源 ID 传到 worker。worker 最终只按最后的 turnId 读取 pending_inbound,所以同一次执行中的较早消息不会进入该轮次的 inbound。
探针:在现有 R7 coalesced-serial (two buffered) 场景中,实际 worker IPC 的 content 同时包含 BUFFERED_ONE / BUFFERED_TWO,但 structuredSourceTurnIds 为 undefined;按当前 worker 的默认来源解析只会载入 om_buf_2。
最简修法是在 durable admission 前把 bufferedTurnIds 预合并到最后一只 turn 的 staged payload(也可完整贯穿 CliTurnPayload / queued tail / worker IPC 的来源 ID 字段)。回归测试应验证实际读模型包含两条 inbound,而不只验证合并后的 prompt 文本。
3. P2 — 单轮次会话完全绕过事件数硬上限
位置:src/services/session-turns.ts:166
pruneSessionEvents 在 turns.length <= 1 时直接返回,因此一个长期活跃、不断收到 progress/anchor 事件的 turn 可以无限增长,maxEventsPerSession 不会生效。第三阶段的“守住硬上限”也无法覆盖这种形状。
fenced Bun 探针:设置 maxEventsPerSession: 4,同一 turn 写入 1 条 inbound、1 条 anchor 和 6 条 progress 后,读回仍为 8 条事件(replyCount=6),超出上限没有被裁剪。
建议为保护中的最新轮次增加轮内裁剪策略(至少保留 input、最新 anchor 和最新回执快照),并增加“只有一个 turn、事件数超过 cap”的回归用例。
验证
- 最新 head:
bun run build通过。 - 最新 head:
bun run test -- test/session-turns.test.ts test/session-store.test.ts,195 条通过。 - 最新 head:
bun test test/session-turns.test.ts test/worker-codex-instance.integration.test.ts,14 条通过。 - WAL、重开兜底、dashboard 管理 token 门禁及 CLI 回执的既有定向测试均通过;上述 3 个场景是现有测试未覆盖的缺口。
- 最新 CI 的 Node
test (1/4..4/4)及其余检查通过,但bun-test (1/4)中worker-codex-instance.integration.test.ts有一次 12 秒启动等待超时;同一 Bun 测试在本地方向重跑通过,建议重跑该 shard 确认是否为瞬时启动延迟。
- activation 合并多条缓冲消息时,先把各 bufferedTurnId 的 staged inbound 合并进最后归属 turnId 再持久化,避免读模型只剩最后一条消息 - pruneSessionEvents 新增第四阶段轮内裁剪:单轮次会话超上限时删被取代的 旧 anchor、较早 progress 与其他旧回执,始终保留全部 input、最新 anchor 与最新回执,守住 maxEventsPerSession 硬上限 - 两条回归用例,摘除守卫均咬红(R7 two-buffered / 单轮次 cap=4) Co-Authored-By: Claude Fable 5 <[email protected]>
对复审 #pullrequestreview-5986202849 的独立复核与代修(head
|
- deferred submit 确认成功后按当时准备好的 submittedText 与来源 turnId 补记输入,避免整轮只落 pending_native 而不可见 - queued activation 独立 receipt watcher 走同一补记边界,保留 adapter 注入内容与正式 cliSessionId - deferred 结果仅有 unproven candidate 时禁止继承旧 native thread,待 owned proof 再补锚 - 增加 history 延迟 6 秒的真实 worker 子进程读模型回归、真实 SQLite 锚点及可控 timer 用例;各条补记与回退守卫的变异均咬红 Co-Authored-By: Kimi Code <[email protected]>
|
P2-1 已修复并追加提交 修复内容
回归与变异
异常场景探针
实际验证
|
deepcoldy
left a comment
There was a problem hiding this comment.
合并增量复审(head 9ba5e41d31415b751fe40859a3a3bdf66a2cca6e)
结论:未发现新的阻断问题,可以按 merge-standard 继续。
- 合并双亲为
2642417de/189f370dfc,tree 为095f8c0a6d86140591ea78aaf4b65884d1e7e20b,与预测树一致;src/services/session-turns.ts在合并前后未发生变化。 - #3606(dispatch 个人工具):新增 consent/身份校验和 relay 分流不改变 daemon staging、worker 输入锚点或 CLI 回执归属。dispatch consent 的附加卡片不会作为用户任务输入进入轮次;目标会话仍沿既有 trigger/turn 身份接线。
- #3631(限额快照):
recordLimitedFailedTurn只增加terminalIdentityTurnIds终态保护,作用于 failed-turn recovery,不改变轮次事件、游标或回执读写语义,未发现串扰。 - 额外用真实
startOutboxWatcher+ 宿主cmdSend(provider mock)验证 sandbox relay:在正确的 worker/turn 身份绑定下,消息 id、response kind 和正文均写入宿主 turns.sqlite。未把“沙盒内直接访问共享 DB 不可达”误判为生产 relay 问题。
范围说明
提交图显示 2642417de 与 189f370dfc 的共同祖先是 f5e3b0485d,且 a1770e7d0c 不在 2642417de 的祖先链上。因此这次 merge 实际带入 29 个主干提交,不是文字中的 4 个;本次已按实际合并树检查所有 session-turns 相交文件,而不是只检查最后 4 个提交。
实际验证
- CI run
44672488857:attempt=1,15/15 全绿,无重跑。 bun run build:通过。- PR / dispatch / recovery / CLI 28 文件:968/968。
- 额外 CLI 回执交叉 6 文件:144/144。
event-dispatcher.test.ts:整文件复跑 757/757(首次并行混跑有一个慢插件 ACK 时序失败,隔离及整文件复跑均通过)。- Bun 按文件隔离运行:PR receipt 41、failed-turn recovery 66、限额终态 2、turn-cli-identity 87、dispatch consent 6 + 18、lazy-rollout 8,合计 228/228。
- 工作树干净,未追加或修改提交。
deepcoldy
left a comment
There was a problem hiding this comment.
第二次合并增量复审(head 016eab3c7f196aec7ef1aadab703945993312767)
结论:合并增量未发现新的阻断问题,可以按 merge-standard 继续。
- 合并双亲为
9ba5e41d3/411e9d032,tree 为9ca25bbf5d0469e59ba53d5f9647d253e848b18c,与预测树一致;src/services/session-turns.ts未因合并改变。 - #3567 / WebTerminal 首次输入:dirty wire、协议回包和提交前拒绝均发生在
recordStructuredInput之前,不会把未实际写入的输入记成已提交。onInputPrepared仍捕获实际发送内容;submitGuard、steerable 和 WebTerminal 字段只影响准入。 - #3635 / sandbox 叶子身份:收紧进程与叶子发布围栏不改变 input hash 的生成方式;native session 仍要求正式证明,和本 PR 的 unproven 不回退旧 thread 防线一致。
- #3632 / credentialsSourceDir:只改变 hook/envelope 与 CLI home 的选择,worker 继续 hash 实际提交文本,未发现 staging 或回执归属变化。
- #3610 / tmux outage:死 pane 与 backend loss 的拒绝、重排队路径不会提前生成锚点;恢复输入仍沿用
PendingCliInput的来源 turnId。 - harness 同时保留 WebTerminal 依赖解析与
recordStructuredInput/updateSessionTurnNativeAnchor空绑定。#3642 的启动卡终态改动不涉及轮次读模型。
实际验证
- CI run
44678382439:attempt=1,15/15 全绿,无重跑。 bun run build:通过。- PR + WebTerminal + sandbox + credential + tmux overlap 25 文件:1266/1266。
- worker 输入准备相邻 5 文件:89/89。
- 启动卡、rename、command-handler、dashboard 4 文件:914/914。
- Bun 按文件隔离:PR receipt 41、WebTerminal input 175、raw input 21、credential source 39、lazy-rollout 8,合计 284/284。
- 工作树干净,未追加或修改提交。
|
@deepcoldy 你好,最新提交 |
deepcoldy
left a comment
There was a problem hiding this comment.
非相邻豁免合并树背书(016eab3c7 + 881cc9608f)
认可该豁免合并树,可以按既定流程 squash-pr;落地树应等于 864efa9b2c57e58a201fcec4ac86b11fa5421936。
- 独立执行指定
git merge-tree --write-tree --no-messages 881cc9608f 016eab3c7,输出与预测 tree864efa9b完全一致。 - 在工作树临时生成同一合并结果,
git write-tree也等于864efa9b;验证后已merge --abort恢复016eab3c7,未保留 merge commit,作者分支未改动。 - 合并后 hunk 位置与 PR 轮次接线无交叠;
src/services/session-turns.ts在016eab3c7与864efa9b之间逐字不变。 - #3529:只影响 tmux socket/root 的兼容选择、持久化与恢复,不改变输入准备、提交确认或轮次事件。
- #3634:只增加 Antigravity 限额历史、过期去重与恢复状态,不改变 turnId、input hash 或回执归属;与本 PR 的 failed-turn / native anchor 边界无串扰。
合成树验证
bun run build:通过。- PR + quota + tmux 21 文件:807/807。
- Bun 隔离运行 session-turns / receipts / antigravity-ansi-usage-limit:53/53。
016eab3c7既有 CI run44678382439:attempt=1,15/15 全绿,无重跑。
|
已合并(squash,落地
未尽事项(不阻断,供后续增量):
再次感谢贡献,欢迎继续参与。 |
|
🚀 Released in v3.59.23 |
… and send as owner (#10770) Closes #10764 Implements #10874 Upstream BotMux support in deepcoldy/botmux#3597 (`GET /api/sessions/:id/turns`, `botmux.session-turns.v1`) and deepcoldy/botmux#3599 (`/api/trigger` top-level `actor: "owner"`) merged on 2026-11-20; older BotMux versions (before `3.59.23`) automatically fall back to wrapper parsing and external-event triggers with a one-line composer notice. ## What changed and why Combines #10764 (wrapper and `botmux send` decoding) and #10874 (structured turn reads and owner-actor sending) into one PR so reading and sending ship together with automatic version detection and per-turn fallback. - **Version auto-detection (read-only probe, cached per BotMux instance).** `server/internal/node/hostbotmux_turns.go` probes capabilities via read-only `GET /api/sessions/:id/turns` (never probing with a real message and never retrying a delivered turn). Results are cached per BotMux instance (dashboard loopback URL, token, and CLI binary file stamp, bounded by TTL) and re-probed after dashboard restart, token rotation, or CLI upgrade; `externalTriggerNotice` preserves the last known negative result for the same dashboard instance and CLI stamp across TTL expiry while the session list re-probes stale capabilities in the background. A `404` without `unknown_session` caches the older-BotMux fallback; a `200` with `schema: "botmux.session-turns.v1"` enables structured reads and checks `capabilities` for `ownerActor`. - **Bounded, off-hot-path turn caching and live re-push.** `session.items` reads use a 200 ms budget (`botmuxTurnsReadTimeout`), live pushes read only the in-memory cache and refresh asynchronously (re-pushing changed items or whole folds when late or updated turns arrive), dashboard CLI refresh runs only on 401/403/redirect (not transport errors or caller cancellation), transport/non-OK/`unknown_session` errors back off, and the turn cache is bounded by idle expiry, an LRU session cap, and a per-session turn cap. - **Structured turn reads first, wrapper parsing as per-turn fallback.** `server/internal/transcript/botmux_turns.go` and `server/internal/node/hostbotmux_turns.go` incrementally fetch `GET /api/sessions/:id/turns` by `seq` cursor and decorate only BotMux sessions (including offline BotMux transcripts). `ApplyBotmuxTurns` matches entry IDs (exact, then `#`-prefix) across all loaded user items before falling back to untruncated `anchor.inputSha256` (only when unambiguous and not anchored to another loaded entry). Page, fold-expand, and `after_seq` reads decorate through a bounded `File.TurnWindow` so reply-only slices keep their enclosing user anchor. Matched turns populate `Item.botmux` (inbound text, sender display name/type, attachment count and safe `attachment_meta`, thread placement, mentions, and full reply text including `--content-file` / `--card-file` replies) before `FoldWork` runs. Replies attach only to `botmux send` calls with a succeeded tool result (skipping running/errored/non-OK sends) via receipt `messageId` and structural shell parsing (`mvdan.cc/sh`, supporting multiple sends in one Bash call, media-only sends, and conservative rejection of unquoted shell expansions). Turns without a structured record and `404` older BotMux instances fall back per turn to wrapper and `botmux send` parsing (`server/internal/transcript/botmux.go`), leaving unknown trigger sources empty rather than fabricating Persona. `Item.text` stays untouched (full original text loadable on demand for **Original**); `openId` and `messageId` never leave the node. - **Send as owner when supported.** When probed capabilities include `ownerActor`, `botmuxHost.Prompt` (`server/internal/node/hostbotmux.go`) pins `/api/trigger` to the probed dashboard base/token, sends top-level `actor: "owner"` with `target.kind: "turn"`, `botId`, and `sessionId` (without `envelope.payload` or `envelope.rawText`), and verifies the echoed `actor: "owner"` in the response. If an instance fails to echo `actor: "owner"`, no resend occurs and that negative state survives subsequent structured reads for the same dashboard instance until re-probe or restart. - **Web UI and first-screen budget.** Keeps the persisted three-mode sidebar botmux chip (`decoded` → `botmux · raw` → `hidden`, sharing the legacy display key and reading narrow display state so the quick filter bar stays memoized across session pushes) and lazy-loaded Lark message/reply rendering. When `Session.botmux_external_trigger` is set (older BotMux before `3.59.23` or `ownerActor` absent; stored on `SessionHierarchy` to keep `Session` inside its 1,528-byte size class while emitting the same top-level `"botmux_external_trigger"` wire key), `AgentStatusLine` (lazy-loaded) renders a one-line notice under the composer recommending BotMux `3.59.23+`. Conditional composer notices (`AttentionNote`, `ConflictNote`, `FailureNote`, `StoppedAgentLine`, `CodexReadOnlyLine`) and `QueueStrip` are lazy-loaded to keep the first screen within `FIRST_SCREEN_BUDGET` without raising the budget. - **Fixtures and docs.** `spec/fixtures/botmux/cases.json` retains all existing wrapper/send cases and adds synthetic `turns_cases` covering `botmux.session-turns.v1`, `ownerActor` present/absent, `cliEntryId` and `inputSha256` matching, `--content-file` / `--card-file` replies, mixed old/new turns with per-turn fallback, and `404` fallback. Updated `docs/protocol/protocol.md`, `docs/integrations/engines.md`, and `docs/web/web.md` to document the order: structured turns first, wrapper parsing as fallback. ## Verification Verified at current PR head `538bc0852a` (merged with `upstream/main` `e8b934017f`): - Conflict resolution in `538bc0852a`: - `docs/web/web.md` — kept the three-mode `botmux` quick-filter chip description and Hub visibility sync from this PR while incorporating the updated per-computer inventory switch location (`**botmux · On / Off** under machine Configuration, **Also show botmux sessions**`) from `upstream/main` (`#16205`). - Earlier merge resolutions preserved (`d3ddd38dba`, `629d638d63`, `6901818d1c`): - `web/src/views/chat/ChatView.tsx` (`defaultManager` / `decisionLaterKey` kept without unused `alsoOpenText`), `web/src/views/chat/FailureNote.tsx` (`FailureNoteProps` controlled props + `failure-note-state.ts` split), `server/internal/proto/session_hierarchy.go` and `session_wire.go` (`View`/`SetView` + `BotmuxExternalTrigger`/`SetBotmuxExternalTrigger`, `hierarchyWireLayout`), and named wire types in `server/internal/proto/item.go` (`BotmuxKind`, `BotmuxSenderType`, `BotmuxAttachmentKind`, `MIMEType`). - Go: - `cd server && go build ./...` — pass - `cd server && go test ./internal/proto/ ./internal/transcript/ -count=1` — pass (includes `TestSessionFitsItsSizeClass`, `TestSessionWireMatchesReflect`, `TestSessionViewWire`, `TestSessionBotmuxExternalTriggerWire`, `TestBotmuxFixtures`, `TestBotmuxParseAndFold`, `TestBotmuxTurnsFixtures`, `TestBotmuxInputHashRequiresCompatibleAnchor`, `TestBotmuxDecorateAndFoldSkipRunningAndErroredSends`, and `TurnWindow` tests) - `cd server && go test -run TestBotmux ./internal/node/ -count=1` — pass - `cd server && go test -run TestToolView ./internal/node/ -count=1` — pass (verifying `toolViews.attach` coexistence with `applyBotmuxTurns`) - `cd server && go vet ./internal/proto/ ./internal/transcript/ ./internal/node/` — clean - Web: - `cd web && npx tsc -b && npm run lint` — pass - `cd web && npx vitest run src/lib/botmux-mode.test.ts src/lib/botmux.test.ts src/lib/agents-display.test.tsx src/views/chat/botmux-messages.test.tsx src/views/chat/FailureNote.test.tsx src/views/chat/FailureNote.controlled.test.tsx src/views/chat/CodexReadOnlyLine.test.tsx src/views/chat/StoppedAgentLine.test.tsx src/views/SidebarPagedTodo.test.tsx src/views/agents/HistoryBrowser.test.tsx src/startup-preload.test.ts src/views/chat/ToolViews.test.tsx src/lib/turns.test.ts src/views/chat/ChatView.*.test.tsx src/views/chat/SegmentView.*.test.tsx` — 43 files, 459 tests passed - `cd web && npm run build` — pass (`first screen en 479,102 B, ja 481,660 B, zh-Hans 479,766 B; 4,749 B left under FIRST_SCREEN_BUDGET (ja)`) - Repo guards: - `git diff --check upstream/main..HEAD` — clean - `python3 scripts/privacy_check.py` and `python3 scripts/privacy_check.py --range upstream/main..HEAD` — pass (`privacy_check: clean (2 values checked)`) - `python3 scripts/test/test_doc_paths.py` (21 tests), `python3 scripts/test/test_doc_sections.py` (6 tests), `python3 scripts/test/test_doc_go_paths.py` (3 tests), `python3 scripts/test/test_doc_wire.py` (16 tests) — all pass - `python3 scripts/ci/web_font_ratchet.py` — pass (exit 0; `3213 hard-coded px font size(s) in the baseline, none above it`) - `python3 scripts/ci/wire_types_ratchet.py --base upstream/main` and `python3 scripts/ci/merge_wire_types.py --base upstream/main --head 538bc08` — pass (`none above the baseline`, `the merge adds no weak type`) - Not run: - Full web test suite (`npm test`), Apple/Android/HarmonyOS/Desktop builds, and hosted CI (explicit-dispatch only). ## Apple performance Not applicable (no `apple/` changes). ## Design principles self-check - [x] 1 Defaults first: decoded mode by default, automatic capability detection with zero new configuration. - [x] 2 Progressive disclosure: original transcript text stays one click away; external-trigger notice is a single quiet line only on older BotMux. - [x] 3 User control: three-mode botmux chip preserved and reversible. - [x] 4 Consistency first: reuses existing user bubble, Markdown, status line, and tokens. - [x] 5 Model and product separate: read decoding is display-only; `actor: "owner"` is used only when BotMux explicitly advertises `ownerActor`. - [x] 6 Checkable: covered by shared fixtures (`spec/fixtures/botmux/cases.json`), Go unit/integration tests, and web unit tests. - [x] 7 Fast, local-first and stateful: read-only capability probe cached per BotMux instance; incremental `seq` cursor reads; first screen stays within `FIRST_SCREEN_BUDGET`. - [x] Other platforms: not applicable in this PR (node transcript/send and web UI; mobile clients are out of scope here). - [x] Parity list row updated: not applicable (no parity table row for BotMux display decoding). ## Why this must change the core Transcript decoding is shared product truth: `server/internal/transcript` turns a runtime's on-disk turns into the one conversation every client and the assistant read, so structural botmux decoding and its fallback have to land in the core next to the other runtime decoders.
Refs #3595
改动
GET /api/sessions/:sessionId/turns,返回botmux.session-turns.v1。能力列表为空,owner actor 留在第二步。botmux send成功后追加回执。游标按更新序号推进,迟到回复会重新返回同一轮的快照。验证
bun run build(含 typecheck 和公开域名审计)通过。仓库没有独立lint脚本,git diff --check通过。git init -b。以 PR CI 为准。待维护者确认
最新验证
f36ae156补齐了test/helpers/worker-function-harness.ts与test/worker-traex-rpc-context.test.ts的recordStructuredInput/updateSessionTurnNativeAnchor默认绑定,并保持notifyNativeTranscriptConsumedLarkTurn在无userUuid时的三参调用兼容。、bun-test (1-4/4)、bun-binary、bun-binary-musl、bun-binary-darwin、network-boundary均为SUCCESS`。