Skip to content

fix(sop): reuse the conversation workspace on dispatch - #1284

Merged
crispinhu27 merged 9 commits into
mainfrom
feature/conversation-sop-workspace
Nov 24, 2026
Merged

crispinhu27 merged 9 commits into
mainfrom
feature/conversation-sop-workspace

Conversation

@crispinhu27

Copy link
Copy Markdown
Collaborator

What changed

An SOP dispatched from a project chat now mounts the chat's current workspace on its original execution machine, using the SOP's configured sandbox. It waits for the current chat turn to release the workspace and restores a checkpoint if the workspace was reclaimed.

  • Reuse existing repository directories by matching their Git origin. New SOP branches start at the local HEAD and retain staged and unstaged edits.
  • Pass the workspace to child SOPs and retain it for retry when the conversation is deleted.
  • Serialize SOPs using the same conversation workspace.

Scope

  • Platform core / API / runner

Compatibility

Reusing an existing Git checkout on a Runner requires Runner 0.12.1 or newer. The handoff uses the existing run context and checkpoint fields.

Validation

  • Go regressions for chat dispatch, local checkout and checkpoint restoration, and execution with a real local Runner passed after rebase.
  • The Docker MySQL workspace admission race passed across two stores for 10 repetitions.
  • go vet ./..., python3 scripts/check-public.py, and git diff --check passed.
  • The full Go suite had macOS test failures and a timeout. The two failures in the full MySQL suite also reproduced on the base branch.

Safety

  • The patch preserves existing workspace files and uncommitted Git changes.
  • The submission consists of public source, documentation, and test fixtures.

@Robert0622

Copy link
Copy Markdown
Collaborator

Reviewed b652452. The HTTP dispatch paths, local checkout handoff, nested SOP, checkpoint handoff, and real Runner test passed. The Runner kept the local HEAD and staged changes.

  • [P2] Avoid a MySQL deadlock when dispatches compete for a shared workspace. writeRunOnce locks the missing SOP run row before it locks the conversation workspace row. Under MySQL's default isolation, the competing transaction can keep the gap lock while it waits for the workspace row, blocking the first transaction's insert. The PR's concurrent admission test hit error 1213 in 1 of 10 runs. With a personal Runner with no run limit and the two dispatches synchronized at the workspace lock, I reproduced it 3 of 3 times on MySQL 8.4. The losing request returns a database error instead of the workspace conflict. Please take the workspace lock before the missing-ID lookup, or use an admission transaction that avoids the gap lock while retaining the workspace conflict check. Run write · Workspace lock

  • [P2] Restore the borrowed checkout from the SOP workspace view. After reclamation, the SOP workspace endpoint looks up the owning conversation's checkpoint and returns its saved timestamp. However, restore eligibility still checks the borrowing SOP, so the response is restorable:false and its restore endpoint returns HTTP 409 (子 SOP 共享父任务工作区,请从父任务操作) for an authorized project owner. In the same test, restoring through the owning run recovered the saved file. The SOP's restore path should resolve the owning workspace for eligibility and restoration, while keeping the project and active-workspace checks. Checkpoint eligibility · Restore guard

@Robert0622

Copy link
Copy Markdown
Collaborator

Re-reviewed 6229869. The two functional fixes check out. The controlled MySQL 8.4 admission test passed five times, and both synchronous and asynchronous restore from the SOP workspace tab recovered the saved file. The local Runner handoff also passed. The recovered child SOP failed once in the combined local run, then passed all five focused reruns.

Please bring checkout preparation inside the SOP’s configured sandbox. The borrowed-worktree preparation still runs at the host layer. The sandbox selected for the SOP should cover the Git worktree operations used to take over the conversation checkout as well.

@Robert0622 Robert0622 left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

  • P2 工作区根目录已有 Git 仓库时,SOP 会跳过嵌套的目标检出。 在根目录检出仓库 A,在 custom/checkout 检出 SOP 绑定的仓库 B,留下本地提交和未提交修改,再从项目对话派发。预期按 origin 复用 B。实测扫描在根目录的 .git 处停止,SOP 重新克隆 B 到 Catalog-checkout,校验节点因工作目录错误失败。最新复现为 run-1795522152253354722。检出扫描

  • P3 借用工作区可访问时,SOP 的活动续期接口仍返回 409。 等借用工作区的 SOP 成功结束且会话目录仍存在,再读工作区状态并调用 POST /api/runs/:id/workspace/use。预期返回 200 并更新共享工作区的活动时间。实测状态显示 available:true,续期却使用已标记回收的 SOP ID,返回 409,持有者的活动时间未更新。用同一账号从持有者入口调用返回 200。活动续期

实测

  • 指定 Compose 项目源码构建并更新成功。平台容器标签和镜像内提交标记均匹配 PR head,健康接口返回 200。
  • 使用提供的 Git 测试账号访问测试仓库,鉴权请求返回 200,git ls-remote 和只读克隆均成功。
  • 通过测试 fixture 建立会话绑定和租约,再调用部署的 /mcp 确认派发。SOP 等待租约释放,同一工作区的并发派发返回 409。MySQL 8.4 并发准入用例通过 10 次。
  • 平台和 Runner 0.12.2 上的 Docker SOP 均在配置的沙箱内识别来源并切换分支,保留本地 HEAD、暂存和未暂存修改,产物写回原检出。根目录加入另一份检出后,目录误选可重复复现。
  • 回收共享目录后,从 SOP 入口异步恢复返回 202,轮询到 ready,原产物恢复。派发、父子 SOP 交接和运行详情页恢复的定向用例通过。
  • Native 平台和 Runner 定向用例均在 bwrap 挂载 /proc 时因本机权限限制失败。

Robert0622
Robert0622 previously approved these changes Nov 24, 2026

@Robert0622 Robert0622 left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

复测 72c8fd5(fix(sop): reuse nested checkouts and refresh the shared workspace),上一轮的两个问题都已修复,通过。

已修复

  • [P2] 根目录已有 Git 仓库时跳过嵌套检出:SandboxCheckouts 改用 CheckoutsIncludingNested。构造「根目录是仓库 A(origin=outer.git),custom/checkout 是仓库 B(origin=inner.git)」:Checkouts 只返回 .,CheckoutsIncludingNested 返回 . 和 custom/checkout(origin 正确),node_modules 下的仓库仍被跳过,扫描深度上限不变。
  • [P3] 借用工作区的 SOP 续期返回 409:POST /workspace/use 现在续期持有者会话的检查点。PR 新增的 TestSOPWorkspaceActivityTouchesConversationOwner 在 head 通过;把三个非测试文件换回 3e37f9b 后同一用例失败(409「工作区正在使用或保存」)。
  • 顺带修掉一个问题:旧代码里跨项目的 POST /api/runs/:id/workspace/use 返回 200,现在返回 409。PR 更新的 TestSOPWorkspaceRestoresConversationCheckpoint 在旧代码上能复现这个 200。

实测

  • 原生部署 PR head(Go 1.26.11,MySQL 8.4.7,全新库),健康检查 200。
  • go vet ./... 通过。./internal/gitwork、./internal/runtime 全部通过。
  • ./internal/httpapi 和 ./internal/engine 的 Workspace/SOP 相关用例在本机有失败,原因都是本机没有 bwrap、Git 推送身份和 Chromium,在 main 上失败的用例相同。另有 4 个恢复类用例在并发跑时超时,单独重跑全部通过。
  • 本机没有 bwrap,无法运行 TestSOPReusesConversationCheckoutAfterTurn(错误是 bwrap sandbox unavailable),所以嵌套检出的端到端效果只在扫描函数层面验证过。

非阻塞(P3)

  • 嵌套检出的修复在 Runner 二进制里(internal/runner/workspace.go:318 → runtime.SandboxCheckouts)。平台升级后,没有升级的 Runner 仍然找不到嵌套检出,而且这里没有版本门槛。建议在发布说明里写明需要同时升级 Runner。

Keep the shared workspace owner and IDE restore behavior from main. Require Runner 0.12.3 for nested checkout reuse and document the upgrade.
Keep the Runner capability gates and Git progress while preserving project credential scoping and output redaction.
Robert0622
Robert0622 previously approved these changes Nov 24, 2026

@Robert0622 Robert0622 left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

复测 0454b91(范围 72c8fd5..0454b91:两个合并 main 的提交 926128a、0454b91)。上一轮在 72c8fd5 上已经通过,这两次合并的冲突处理我逐个核对过,没有发现问题,通过。

926128a 的冲突处理

  • internal/httpapi/run_workspace.go、workspace_restore.go:放弃本 PR 原来的 workspaceLifecycleTarget,改用 main(#1294)的 workspaceOwner。workspaceOwner 同样要求持有者 run 存在、不是个人工作区、并且和 SOP 属于同一个项目,所以借用对话工作区的 SOP 仍然会解析到持有者会话。TestSOPWorkspaceActivityTouchesConversationOwner 仍然通过。
  • 行为变化:跨项目的 POST /api/runs/:id/workspace/{restore,use} 从 409 变成 410(共享工作区所属运行已不可用),测试也相应改了。这是 main 的统一处理,可以接受。
  • Runner 门槛从 0.12.2 升到 0.12.3,新增 TestRunnerSupportsConversationSOPWorkspaceWithNestedCheckouts(0.12.2 → false,0.12.3 → true)。CHANGELOG 和 docs/conversation-workspaces.md 写明需要同时升级执行机器。上一轮我提的非阻塞 P3(嵌套检出没有 Runner 版本门槛)也一起解决了。

0454b91 的冲突处理

  • internal/runtime/process.go:processWriter 同时保留了本 PR 的 observe: command.observeOutput 和 #1295 的 redact,stdout、stderr 都有。
  • WithSandboxGit 在自动检出时把项目凭据环境清空(projectCredentialEnvKey{} 设为 nil),新增 TestSandboxGitPreparationExcludesProjectCredentialEnvironment:检出用的 git 进程里看不到 PROJECT_TEST_TOKEN。项目凭据不会泄漏到自动 Git 操作里。
  • 合并后 Runner 上报 0.12.3,高于 main 的 0.12.1(项目凭据),版本门槛不冲突。

实测

  • 原生部署 head(Go 1.26.11,MySQL 8.4.7 全新库,按 PR 重新构建 chatbot),健康检查 200。chatbot 487/487 通过;go vet ./... 通过。
  • ./internal/domain、./internal/gitwork、./internal/repo 全部通过。./internal/httpapi、./internal/runtime、./internal/runner、./internal/engine 里,和 main(177d881)比只多出 TestSOPReusesConversationCheckoutAfterTurn、TestSOPReusesRunnerConversationCheckout 两个失败。这两个是本 PR 新增的端到端用例,报错都是 bwrap sandbox unavailable,本机没法运行 bwrap(容器不允许创建用户命名空间)。其余失败和 main 相同。合并涉及的 TestSOPWorkspaceActivityTouchesConversationOwner、TestSOPWorkspaceRestoresConversationCheckpoint、TestWorkspaceRestoreEligibilityUsesWorkspaceEditAccess、TestSharedRunnerWorkspaceRestoreRequiresConnectedRunner 都通过。
  • 因为没有 bwrap,嵌套检出复用的端到端效果没能在本机跑,建议合入前在有 bwrap 的环境跑一下这两个用例。

Apply project Runner grants when a new SOP borrows a conversation workspace. Keep responsible-user authentication and model selection.

@Robert0622 Robert0622 left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

复测 ad3052e(范围 0454b91..ad3052e):94896d4 合并了 main(#1304、#1285),ad3052e 修了一个 MySQL 测试的等待逻辑。冲突处理和新增的权限检查我都核对过,也在部署环境里用真实模型走通了,没有发现问题,通过。

94896d4 的冲突处理(和 #1285 的执行机器共享合并)

  • sopRunnerAuthMode(internal/engine/sop_executors.go:38):原来的写法是「对话固定在非本人的机器上就用个人凭证」,现在拆成两部分。平台执行机器(OwnerUserID == 0)在这里直接返回 personal;其余情况交给 #1285 的 RuntimeAuthModeForUser,非机器主人用 runner-login 类 CLI 时同样返回 personal。我逐个情况比对过,和合并前的效果一致。runnerLogin、configured、defaults 三处都统一改用这个函数。
  • startWorkflowDefinition(internal/engine/run_start.go:165):借用对话工作区的 SOP 新增了一个检查。如果执行机器属于个人,就用 CanUseRunner 校验当前责任人在这个项目里还能不能用这台机器,不能用就返回 403 ErrRunnerProjectDenied。这个检查放在版本门槛之前、创建 run 之前,被拒绝时不会留下 run。
  • dispatchRunnerTask 保留了 #1285 的 projectShare 提示,authMode 改用 sopRunnerAuthMode,两边都没丢。

ad3052e

  • 修改前,entered 的缓冲只有 2。如果同一个锁查询被触发第三次,测试会卡住。现在加了 select,release 关闭以后直接放行。我在 MySQL 8.4.7 上把 TestMySQLConcurrentSOPsCannotClaimSameConversationWorkspace 连跑 15 次,全部通过。

部署实测(本机原生部署,MySQL 8.4.7 全新库,执行机器为本 PR 构建的 Runner 0.12.3)

  • 准备:admin 把自己的执行机器共享给项目 p84,bob 是项目普通成员。共享前,bob 在这台机器上建对话返回「无权使用此执行机器」;共享后可以建。
  • 对话:bob 的对话用 CodeBuddy(iOA,deepseek-v4.1-flash),模型自己打开工作区,执行 echo from-conversation-turn2 > marker.txt。
  • 借用工作区的脚本 SOP:bob 带 X-AgentFlow-Chat-Session 启动 SOP。run 事件里显示「复用对话工作区 run-…」,脚本在对话的 workspace 目录里执行,cat marker.txt 输出 from-conversation-turn2,SOP 执行成功。
  • 借用工作区的 Agent SOP:节点用 Claude Code,模型是 bob 默认的 DeepSeek deepseek-flash,在同一个工作区里读到了 marker。这台机器的 claude-code 认证方式是「本机登录」,但 CLI 进程里注入的是 bob 自己的 DeepSeek 地址和 Key,没有用 admin 机器上的本机登录。共享机器上 SOP 用责任人凭证这一点,合并后的行为正确。
  • 并发保护:前一个借用工作区的 SOP 还没结束时,再启动一个,返回「对话工作区已有未结束的 SOP:工作区正在使用或保存」。
  • 撤销共享:admin 把共享项目清空后,bob 再从对话启动 SOP,返回 403「执行机器未向该项目共享,或你没有该项目的执行权限」,项目里的 run 数量没有变化(前后都是 3 个)。bob 在原对话里继续发消息,也返回 403「当前无法使用会话绑定的执行机器…」。

测试

  • 构建通过,go vet ./... 通过,chatbot 测试 493/493 通过。
  • ./internal/domain、./internal/gitwork 全部通过。
  • 上一轮因为本机没有 bwrap 没跑成的两个端到端用例,这次用 bwrap 替身跑了,都通过:TestSOPReusesConversationCheckoutAfterTurn(engine)和 TestSOPReusesRunnerConversationCheckout(runner)。替身只处理 --chdir 和 bind 映射,不做真正隔离,所以只能证明嵌套检出复用的流程走通,不能证明隔离效果。
  • 下面这些也通过:TestConversationSOPDispatchUsesBoundWorkspace(包括新增的撤销共享两个子用例)、TestGenericSOPExecutorPrefersRunnerLoginOverSharedKey、TestSharedRunnerSOPUsesProjectDefaultAndResponsibleUserCredentials、TestRunnerSupportsConversationSOPWorkspaceWithNestedCheckouts。
  • ./internal/repo、./internal/runner、./internal/engine、./internal/httpapi、./internal/runtime 里的其余失败用例,main(c3dd1b4)上同样失败,没有新增失败。原因是本机没有 Chromium 和 Docker,测试进程还继承了外层环境的 Git 身份变量。

合入提醒:本 PR 和 #1279 在 internal/runner/daemon.go 的 runnerVersion、internal/engine/runner_dispatch.go 上有冲突。如果 #1279 先合,本 PR 合入时保留 0.12.3 就行;如果本 PR 先合,#1279 要升到 0.12.4(详见 #1279 的评审)。

@crispinhu27
crispinhu27 merged commit f3d4f41 into main Nov 24, 2026
16 checks passed
@crispinhu27
crispinhu27 deleted the feature/conversation-sop-workspace branch November 24, 2026 17:46
Robert0622 pushed a commit that referenced this pull request Nov 24, 2026
Conflicts take main's text with this PR's rule applied: user-facing SOP
wording becomes LOOP. The same rule is applied to the messages, comments,
prompts and tests that #1284 and #1279 added since the previous merge.

Co-Authored-By: Claude Code <noreply@anthropic.com>
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.

2 participants