Skip to content

fix(sdk): reload stale sessions before starting btw - #5021

Merged
7Sageer merged 1 commit into
MoonshotAI:mainfrom
7Sageer:fix/btw-stale-session
Nov 17, 2026
Merged

7Sageer merged 1 commit into
MoonshotAI:mainfrom
7Sageer:fix/btw-stale-session

Conversation

@7Sageer

@7Sageer 7Sageer commented Nov 17, 2026

Copy link
Copy Markdown
Collaborator

Requirement or Bug

在另一个客户端更新过当前会话后,TUI 用户启动 /btw 会遇到 session.stale,侧边提问无法开始。本 PR 让 /btw 与 prompt、compact 等会话写入入口一样,自动加载最新会话并完成启动。

Bug Reproduction Steps

  1. 客户端 A 打开并持有一个会话。
  2. 客户端 B 读取同一会话,写入内容后关闭,使 A 的会话版本落后于磁盘。
  3. A 执行 /btw <question>。
  4. 修改前,A 显示 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 接口不变。

 startBtw(sessionId)
+  withStaleReload(sessionId)
+    ├─ session.stale → reload session → notify host → retry once
   requireLiveSession(sessionId)
   materializeMainAgent(session)
   SessionBtwService.start()
  • 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

Behavior Before After Who relies on the old behavior Escape hatch
当前会话被另一客户端更新后启动 /btw TUI 显示 Failed to start /btw,用户需要手动重载 自动加载最新会话、刷新界面并重试一次启动 TUI 的 /btw 用户;同一 SDK 入口的其他调用方也获得一致行为 无需;恢复与 prompt、compact 相同的既有行为
会话未发生外部更新,或 /btw 正常启动 直接派生子代理 首次调用直接成功,不触发重载 现有 /btw 用户 行为不变
其他会话错误(如仍被另一客户端占用) 按原错误返回 仅处理 session.stale;session.occupied 等其他错误仍按原路径返回 多客户端会话用户 行为不变

受影响模块与覆盖:

  • node-sdk 的 /btw 启动入口:双客户端回归测试验证外部更新后的重载、重试和回调。
  • TUI:真实 KimiTUI 与 mock 模型验证会话更新提示、侧边面板回答,以及无启动错误。
  • 公共 SDK 方法签名、会话格式、CLI 参数、provider 配置均未改变。
验证结果
  • 未修复代码运行新增回归测试,稳定复现 session.stale;修复后同一场景通过。
  • SDK 相关测试:3 个文件,共 131 项通过。
  • pnpm --filter @moonshot-ai/kimi-code-sdk typecheck:通过。
  • 修改文件 lint:0 errors(已有代码的 4 条 warnings)。
  • pnpm -w run build:packages && pnpm --filter @moonshot-ai/kimi-code build:通过。
  • 真实 KimiTUI 场景:显示 Loaded updates from another client.,/btw 返回 mock 回答 BTW_OK。

Checklist

  • I have read the CONTRIBUTING document.
  • I have linked a related issue (external PRs: issue must have a maintainer's /approve).(维护者直接报告的缺陷,无单独 issue。)
  • I have added tests that prove my feature works.
  • The behavior-change table above is complete, and every removed behavior or flipped default is named in the changeset and either has an escape hatch or was explicitly approved by a maintainer in this PR.
  • Ran gen-changesets skill, or this PR needs no changeset.
  • Ran gen-docs skill, or this PR needs no doc update.(仅修复 /btw 对既有会话恢复机制的使用,现有用户文档无需更新。)

@7Hanrui

7Hanrui commented Nov 17, 2026

Copy link
Copy Markdown
Contributor

@codex review

@changeset-bot

changeset-bot Bot commented Nov 17, 2026

Copy link
Copy Markdown

🦋 Changeset detected

Latest commit: 239e819

The changes in this PR will be included in the next version bump.

This PR includes changesets to release 1 package
Name Type
@moonshot-ai/kimi-code Patch

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

@chatgpt-codex-connector

chatgpt-codex-connector Bot commented Nov 17, 2026 •

Copy link
Copy Markdown
Codex Review Summary: ✅ Completed

This comment shows the latest Codex review activity on this pull request.

Review Status Commit Review trigger
📝 Code Review ✅ Completed 2026-11-17T15:20:56.411845Z 239e819 Draft marked ready
ℹ️ 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" or "@codex security review".

Codex reacts with 👀 while any review is running, comments if it has suggestions, and reacts with 👍 once all reviews finish with no findings.

@7Sageer
7Sageer marked this pull request as ready for review November 17, 2026 15:16
@pkg-pr-new

pkg-pr-new Bot commented Nov 17, 2026

Copy link
Copy Markdown
pnpm dlx https://pkg.pr.new/@moonshot-ai/kimi-code@239e819
npx https://pkg.pr.new/@moonshot-ai/kimi-code@239e819

commit: 239e819

@chatgpt-codex-connector chatgpt-codex-connector Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

💡 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 () => {

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

P2 Badge 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 👍 / 👎.

@7Sageer
7Sageer merged commit 5ae9739 into MoonshotAI:main Nov 17, 2026
18 checks passed
7723qqq pushed a commit to 7723qqq/kimi-code that referenced this pull request Nov 25, 2026
… 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.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants