Repository navigation
fix(sdk): allow resuming occupied sessions - #4910
Conversation
🦋 Changeset detectedLatest commit: 1b91fed 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 |
There was a problem hiding this comment.
Copilot was unable to review this pull request because the user who requested the review has insufficient remaining quota. Try requesting a review at a lower effort level.
An organization owner can choose the repository organization as the billing source for reviews requested by members with a paid Copilot license. Learn about Copilot code review billing.
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: 1b91feddc2
ℹ️ 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".
| isLive: (taskId) => this.toolExecutor.handler(taskId)?.snapshot().status === 'running', | ||
| }); | ||
| const pendingWork = (snapshot.input.inbox?.normal?.length ?? 0) + (snapshot.input.inbox?.notify?.length ?? 0); | ||
| if (lease === undefined && (completions.length > 0 || pendingWork > 0)) lease = this.runGate.acquire(); |
There was a problem hiding this comment.
Allow read-only resume with a task running in the owner
If another client holds this session while a detached task is still running, its persisted task.started record is converted to lost by AgentTaskService.restore(). restoreTaskCompletions() then creates a lost-task notice in this process, which has no handler for the owner's task, so this condition calls acquire() and raises session.occupied; resume still cannot open history for these sessions. Only escalate for recovery notifications that are actually pending, not a task that may still be live behind the lock.
AGENTS.md reference: AGENTS.md:L113-L113
Useful? React with 👍 / 👎.
Requirement or Bug
当同一会话正在 Web 等另一个客户端中运行时,TUI 选择 resume 会直接报
session.occupied,用户无法查看已有历史。这个 PR 让 resume 可以恢复并显示历史,同时保留发送消息时的写入互斥与显式接管流程。Bug Reproduction Steps
kimi --resume <session-id>或 Sessions picker 恢复同一会话。This session is in use by another ... instance;修改后:历史正常显示,且提交新消息仍返回session.occupied。Root Cause
AgentLoopService.initializeLoop()在加载历史前无条件申请写锁,使只读恢复与后续写入共享同一个入口。另一个客户端持锁时,整个 resume 因此失败。这是针对锁层级的修复:初始化先尝试获取短期租约;没有待恢复写入时允许只读创建 loop,有待处理输入或任务完成通知时仍必须先取得写锁。
Code Changes
SessionRunGateService.tryAcquire()复用现有 stale 检查;仅将session.occupied转为无租约结果,其他错误继续抛出。AgentLoopService.initializeLoop()在没有待恢复工作时允许只读启动;存在待处理输入或需要补发的任务完成通知时,仍通过acquire()保留原拒绝行为。begin()的占用和 stale 语义不变。acquire()仍用于发送消息及其他写入路径,没有自动 takeover,也没有修改 TUI 的重试 / 接管交互。Screenshots / Recordings
真实构建的 CLI 在另一个客户端持锁时执行
--resume,历史消息正常显示,没有出现session.occupied。以下文本来自该进程的 PTY 输出,示例会话与目录均为临时数据。Behavior Changes and Affected Users
session.occupied失败session.occupiedsession.occupied,TUI 提供重试 / 接管sessionRunGate.test.ts:覆盖可选租约的占用结果、原有 direct hold 释放语义。rpc-events.test.ts:覆盖无恢复工作时只读启动、发送仍拒绝,以及崩溃通知恢复仍需锁且只投递一次。sdk-rpc-client-v2.test.ts:两个独立 engine 实例共享持久化 home,一方持锁,另一方恢复历史并确认写入拒绝。验证结果
agent-core-v2与node-sdkTypeScript 检查通过。sessionRunGate.test.ts、rpc-events.test.ts、coreLoopService.test.ts全部通过。sdk-rpc-client-v2.test.ts完整文件通过。v2-run-print.test.ts、kimi-tui-startup.test.ts通过。session.occupied失败,修复后通过。Checklist
/approve).(本 PR 为内部修复,无关联 issue;问题与复现步骤已在上方说明。)gen-changesetsskill, or this PR needs no changeset.gen-docsskill, or this PR needs no doc update.(现有文档未描述旧的占用拒绝行为;本次只修正该行为,无配置、命令或契约变化。)