Repository navigation
fix(sdk): reload stale sessions before starting btw - #5021
Conversation
|
@codex review |
🦋 Changeset detectedLatest commit: 239e819 The changes in this PR will be included in the next version bump. This PR includes changesets to release 1 package
Not sure what this means? Click here to learn what changesets are. Click here if you're a maintainer who wants to add another changeset to this PR |
Codex Review Summary: ✅ CompletedThis comment shows the latest Codex review activity on this pull request.
ℹ️ About Codex in GitHubYour team has set up Codex to review pull requests in this repo. Reviews are triggered when you
Codex reacts with 👀 while any review is running, comments if it has suggestions, and reacts with 👍 once all reviews finish with no findings. |
commit: |
There was a problem hiding this comment.
💡 Codex Review
Here are some automated review suggestions for this pull request.
Reviewed commit: 239e8192ef
ℹ️ About Codex in GitHub
Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you
- Open a pull request for review
- Mark a draft as ready
- Comment "@codex review".
If Codex has suggestions, it will comment; otherwise it will react with 👍.
Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".
| const session = this.requireLiveSession(input.sessionId); | ||
| await this.materializeMainAgent(session); | ||
| return session.accessor.get(ISessionBtwService).start(); | ||
| return this.withStaleReload(input.sessionId, async () => { |
There was a problem hiding this comment.
Prevent retrying a partially persisted BTW fork
When another client changes the main wire of an already-materialized session, the first SessionBtwService.start() can persist a child ID in AgentLifecycleService.buildAgentScope before loop.start() detects session.stale. Creation cleanup disposes the scope but leaves the metadata entry, so this retry allocates a second ID and leaves a phantom empty BTW child in the persisted roster. Clean up the failed fork before retrying this non-idempotent action.
AGENTS.md reference: AGENTS.md:L104-L104
Useful? React with 👍 / 👎.
… stale btw sessions Both upstream commits (5abe864, 49a3310) are ancestors whose effects the 2.7.0 merge resolution had dropped. The trusted marketplace set now derives only from KIMI_REGION_PROFILES instead of also trusting the retired coding.deva.msh.team URL (MoonshotAI#5087). startBtw goes back through withStaleReload so a session.stale reloads the session, re-wires it and retries (MoonshotAI#5021); the fork's engine auto-reloads a watched session, so the test drives one session.stale through ISessionBtwService.start instead of the cross-client flow. Ledger: 5090 equivalent, 5251 not-applicable.
Requirement or Bug
在另一个客户端更新过当前会话后,TUI 用户启动
/btw会遇到session.stale,侧边提问无法开始。本 PR 让/btw与 prompt、compact 等会话写入入口一样,自动加载最新会话并完成启动。Bug Reproduction Steps
/btw <question>。Failed to start /btw: Session ... was updated by another client; reload it before continuing;修改后,A 加载最新会话并显示/btw回答。Root Cause
这是根本修复。
SessionBtwService.start()派生子代理时会初始化会话日志,触发SessionRunGateService的版本检查。SDK 已有withStaleReload,负责在session.stale后重新加载会话、通知宿主刷新并重试一次;startBtw原先直接调用引擎,绕过了这层既有恢复逻辑。Code Changes
将
startBtw接入现有的会话恢复路径,保持派生子代理的逻辑和 SDK 接口不变。packages/node-sdk/src/sdk-rpc-client-v2.ts:startBtw复用withStaleReload,在重载后的 session scope 上重新获取主代理和 BTW 服务。packages/node-sdk/test/sdk-rpc-client-v2.test.ts:以两个真实 SDK 客户端覆盖“另一客户端写入并关闭后启动/btw”的场景,验证子代理创建和 stale-reload 回调。.changeset/fix-btw-stale-session.md:记录 CLI 的 patch 修复。Behavior Changes and Affected Users
/btwFailed to start /btw,用户需要手动重载/btw用户;同一 SDK 入口的其他调用方也获得一致行为/btw正常启动/btw用户session.stale;session.occupied等其他错误仍按原路径返回受影响模块与覆盖:
node-sdk的/btw启动入口:双客户端回归测试验证外部更新后的重载、重试和回调。KimiTUI与 mock 模型验证会话更新提示、侧边面板回答,以及无启动错误。验证结果
session.stale;修复后同一场景通过。pnpm --filter @moonshot-ai/kimi-code-sdk typecheck:通过。pnpm -w run build:packages && pnpm --filter @moonshot-ai/kimi-code build:通过。KimiTUI场景:显示Loaded updates from another client.,/btw返回 mock 回答BTW_OK。Checklist
/approve).(维护者直接报告的缺陷,无单独 issue。)gen-changesetsskill, or this PR needs no changeset.gen-docsskill, or this PR needs no doc update.(仅修复/btw对既有会话恢复机制的使用,现有用户文档无需更新。)