diff --git a/apps/server/src/mcp/OrchestratorMcpService.test.ts b/apps/server/src/mcp/OrchestratorMcpService.test.ts index 2dad1fe68ee6d..1652a7d0e59e7 100644 --- a/apps/server/src/mcp/OrchestratorMcpService.test.ts +++ b/apps/server/src/mcp/OrchestratorMcpService.test.ts @@ -4366,6 +4366,72 @@ describe("OrchestratorMcpService provider resolution", () => { }), ); + it.effect("creates a task that runs in the project checkout", () => + Effect.gen(function* () { + const saved = yield* Ref.make>([]); + const mcp = yield* OrchestratorMcpService.OrchestratorMcpService.pipe( + Effect.provide(service(masterRepository, saved)), + ); + yield* mcp.scheduleTask(client, { + projectId, + prompt: "Check the build", + schedule, + workspace: "checkout", + }); + assert.deepEqual(yield* Ref.get(saved), [{ type: "root" }]); + }), + ); + + it.effect.each([ + { + name: "unbinding into the checkout in one call", + task: { threadId: ThreadId.make("thread:scheduled-base"), threadMode: "continue" }, + update: { bindToCurrentThread: false, workspace: "checkout" }, + expected: { type: "root" }, + }, + { + name: "moving an unbound task from the checkout to worktrees", + task: {}, + update: { workspace: "worktree" }, + expected: { type: "worktree", baseRef: "origin/master", startFromOrigin: true }, + }, + { + name: "asking for worktrees again keeps the saved base branch", + task: { workspaceStrategy: { type: "worktree", baseRef: "release" } }, + update: { workspace: "worktree" }, + expected: { type: "worktree", baseRef: "release" }, + }, + ] as const)("$name", ({ task, update, expected }) => + Effect.gen(function* () { + const saved = yield* Ref.make>([]); + const mcp = yield* OrchestratorMcpService.OrchestratorMcpService.pipe( + Effect.provide(service(masterRepository, saved, { ...existing, ...task })), + ); + yield* mcp.updateScheduledTask(client, { scheduledTaskId: existing.id, ...update }); + assert.deepEqual(yield* Ref.get(saved), [expected]); + }), + ); + + it.effect("refuses a workspace for a task that stays bound to its thread", () => + Effect.gen(function* () { + const saved = yield* Ref.make>([]); + const mcp = yield* OrchestratorMcpService.OrchestratorMcpService.pipe( + Effect.provide( + service(masterRepository, saved, { + ...existing, + threadId: ThreadId.make("thread:scheduled-base"), + threadMode: "continue", + }), + ), + ); + const refused = yield* mcp + .updateScheduledTask(client, { scheduledTaskId: existing.id, workspace: "checkout" }) + .pipe(Effect.flip); + assert.include(refused.message, "bindToCurrentThread: false"); + assert.deepEqual(yield* Ref.get(saved), []); + }), + ); + it.effect.each([ { name: "no remote, only master", diff --git a/apps/server/src/mcp/OrchestratorMcpService.ts b/apps/server/src/mcp/OrchestratorMcpService.ts index 07b137c61e354..3d5d255bbf173 100644 --- a/apps/server/src/mcp/OrchestratorMcpService.ts +++ b/apps/server/src/mcp/OrchestratorMcpService.ts @@ -285,6 +285,14 @@ function errorMessage(error: unknown): string { return error instanceof Error ? error.message : String(error); } +const SCHEDULED_TASK_WORKSPACE = { + root: "checkout", + worktree: "worktree", + existing_worktree: "existing_worktree", +} as const; +const WORKSPACE_NEEDS_UNBOUND = + "workspace applies only to tasks that start a new thread. Pass bindToCurrentThread: false, or leave workspace out."; + function scheduledTaskSummary(task: ScheduledTask, mayRun: boolean): OrchestratorMcpScheduledTask { return { scheduledTaskId: task.id, @@ -297,6 +305,9 @@ function scheduledTaskSummary(task: ScheduledTask, mayRun: boolean): Orchestrato schedule: task.schedule, nextRunAt: task.nextRunAt, lastRunStatus: task.lastRunStatus, + ...(task.threadId === null + ? { workspace: SCHEDULED_TASK_WORKSPACE[task.workspaceStrategy.type] } + : {}), ...(task.webhook?.url == null || !mayRun || task.blockedByPolicy !== undefined ? {} : { webhookUrl: task.webhook.url }), @@ -2279,8 +2290,8 @@ const make = Effect.gen(function* () { const scheduledTaskWorkspaceStrategy = Effect.fn( "OrchestratorMcpService.scheduledTaskWorkspaceStrategy", - )(function* (boundToThread: boolean, projectId: ProjectId) { - if (boundToThread) return { type: "root" as const }; + )(function* (workspace: "checkout" | "worktree", projectId: ProjectId) { + if (workspace === "checkout") return { type: "root" as const }; const { workspaceRoot } = yield* requireProject(projectId); return { type: "worktree" as const, @@ -2387,6 +2398,9 @@ const make = Effect.gen(function* () { : "bindToCurrentThread binds to this thread, which belongs to a different project.", ); } + if (bindToCurrentThread && input.workspace !== undefined) { + return yield* failure("invalid_request", WORKSPACE_NEEDS_UNBOUND); + } const modelSelection = parent?.thread.modelSelection ?? (yield* projectDefaultModelSelection(project)); // Never end on a lone high surrogate, which renders as a broken character. @@ -2406,7 +2420,10 @@ const make = Effect.gen(function* () { : bindToCurrentThread ? "continue" : "new_thread", - workspaceStrategy: yield* scheduledTaskWorkspaceStrategy(bindToCurrentThread, projectId), + workspaceStrategy: yield* scheduledTaskWorkspaceStrategy( + bindToCurrentThread ? "checkout" : (input.workspace ?? "worktree"), + projectId, + ), modelSelection, runtimeMode: limits.runtimeMode, interactionMode: limits.interactionMode, @@ -2489,11 +2506,23 @@ const make = Effect.gen(function* () { : input.bindToCurrentThread && parent !== undefined ? parent.thread.id : null; - // Reset the workspace only when the binding flips, so unbinding cannot leave runs in the shared checkout. + if (threadId !== null && input.workspace !== undefined) { + return yield* failure("invalid_request", WORKSPACE_NEEDS_UNBOUND); + } + // Without an explicit workspace, unbinding defaults to a worktree so runs do not silently share the checkout. + const workspace = + input.workspace ?? + ((threadId === null) === (existing.threadId === null) + ? undefined + : threadId === null + ? "worktree" + : "checkout"); const workspaceStrategy = - (threadId === null) === (existing.threadId === null) + workspace === undefined || + (existing.threadId === null && + SCHEDULED_TASK_WORKSPACE[existing.workspaceStrategy.type] === workspace) ? existing.workspaceStrategy - : yield* scheduledTaskWorkspaceStrategy(threadId !== null, existing.projectId); + : yield* scheduledTaskWorkspaceStrategy(workspace, existing.projectId); const upsertInput: ScheduledTaskUpsertInput = { id: existing.id, title: input.title ?? existing.title, diff --git a/apps/server/src/mcp/toolkits/orchestrator/handlers.ts b/apps/server/src/mcp/toolkits/orchestrator/handlers.ts index d90a118be468e..2d94f7279cb57 100644 --- a/apps/server/src/mcp/toolkits/orchestrator/handlers.ts +++ b/apps/server/src/mcp/toolkits/orchestrator/handlers.ts @@ -106,7 +106,16 @@ const handlers = { { refused: "schedule tasks in this environment" }, ), McpToolAccess.writes( - ({ scheduledTaskId, prompt, title, schedule, enabled, freshSession, bindToCurrentThread }) => + ({ + scheduledTaskId, + prompt, + title, + schedule, + enabled, + freshSession, + bindToCurrentThread, + workspace, + }) => Effect.gen(function* () { if (scheduledTaskId === undefined) return yield* Effect.die("unreachable"); const scope = yield* McpInvocationContext.McpInvocationContext; @@ -119,6 +128,7 @@ const handlers = { enabled, freshSession, bindToCurrentThread, + workspace, }); }), ), diff --git a/apps/server/src/provider/agentContextBudget.json b/apps/server/src/provider/agentContextBudget.json index e089e5a84218a..0aa47c8068105 100644 --- a/apps/server/src/provider/agentContextBudget.json +++ b/apps/server/src/provider/agentContextBudget.json @@ -1,5 +1,5 @@ { "claudeSystemAppendBytes": 6245, "codexAdditionalContextBytes": 7639, - "mcpToolListJsonBytes": 59026 + "mcpToolListJsonBytes": 59176 } diff --git a/docs/orchestration-v2/orchestrator-mcp-server.md b/docs/orchestration-v2/orchestrator-mcp-server.md index 71d8845e74c75..b7be73ea9e44c 100644 --- a/docs/orchestration-v2/orchestrator-mcp-server.md +++ b/docs/orchestration-v2/orchestrator-mcp-server.md @@ -497,7 +497,9 @@ Without a signature, anyone with the webhook URL can trigger a run. Signature checks, filters, and dedupe can skip a webhook request. `bindToCurrentThread` defaults to `true` only when the caller has a thread in the task's project; otherwise it defaults to `false`. Bound runs post into -that thread; unbound runs each launch a fresh thread in the task's project. +that thread; unbound runs each launch a fresh thread in the task's project, +in a new worktree from the default branch unless `workspace: "checkout"` puts +them in the project checkout. A task that stays bound rejects `workspace`. Explicit `bindToCurrentThread: true` without a caller thread in that project is rejected. Pausing uses `schedule_task` with `scheduledTaskId` and `enabled: false`; deletion is permanent. diff --git a/evals/mcp-tool-use/cases/pull-requests.ndjson b/evals/mcp-tool-use/cases/pull-requests.ndjson index c2b049d3f4724..d127a1fc789cb 100644 --- a/evals/mcp-tool-use/cases/pull-requests.ndjson +++ b/evals/mcp-tool-use/cases/pull-requests.ndjson @@ -58,3 +58,5 @@ {"id": "control-ci-why-no-rerun", "area": "pull-requests", "kind": "control", "pattern": "question about a failing check", "experience": "Control: the user asks why CI failed. Reading the log is right; re-running it was not asked.", "rule": "Re-run only when asked", "privacy": "commit-safe", "setup": {"role": "top", "world": {"branch": "fix/retry-queue", "commits": [{"sha": "9f3c2a1", "subject": "fix(queue): retry a failed job once"}], "diff": "diff --git a/src/queue.ts b/src/queue.ts\n--- a/src/queue.ts\n+++ b/src/queue.ts\n@@ -4,3 +4,4 @@ export async function run(job: Job) {\n try {\n return await job.execute();\n- } catch {\n+ } catch (error) {\n+ if (job.attempts > 1) throw error;\n return run({ ...job, attempts: job.attempts + 1 });\n", "files": {"src/queue.ts": "export async function run(job: Job) {\n try {\n return await job.execute();\n } catch (error) {\n if (job.attempts > 1) throw error;\n return run({ ...job, attempts: job.attempts + 1 });\n }\n}\n"}, "prs": [{"number": 124, "title": "fix(queue): retry a failed job once", "headRefName": "fix/retry-queue", "checks": [{"name": "Lint", "state": "pass"}, {"name": "Test", "state": "fail", "log": "FAIL src/queue.test.ts > gives up after one retry\nAssertionError: expected 2 calls, got 3\n at src/queue.test.ts:18:5"}]}]}}, "prompt": "Why is CI failing on #124? Don't change anything yet.", "expect": {"notCalls": ["ci_rerun", "pull_request_merge"], "shellMustNot": ["\\bgh run rerun\\b"]}, "notes": "Control for ci-flake-rerun-through-gh."} {"id": "land-stack-in-one-merge", "area": "pull-requests", "kind": "bad", "pattern": "a green, approved stack merged layer by layer or through gh stack merge", "experience": "Asked to land a two-PR stack, the agent had only a one-PR merge tool, so it merged the bottom layer alone, which restarts CI on the layer above, or fell back to gh stack merge in the shell.", "rule": "pull_request_merge stack:true on the top PR merges every layer below it at once.", "privacy": "commit-safe", "setup": {"role": "top", "context": "PR #124 (feature/error-detail, on main) and PR #125 (feature/frozen-host, on top of #124) form a native GitHub stack and are linked to this thread.", "world": {"repo": "acme/app", "branch": "feature/frozen-host", "stack": 300, "linkedPullRequests": [{"number": 124, "state": "open", "title": "feat(server): include the failing step in connection errors"}, {"number": 125, "state": "open", "title": "feat(server): detect a frozen host and say so"}], "prs": [{"number": 124, "title": "feat(server): include the failing step in connection errors", "headRefName": "feature/error-detail", "baseRefName": "main", "headRefOid": "e1d2c3b", "reviewDecision": "APPROVED", "mergeable": "MERGEABLE", "checks": [{"name": "CI", "state": "pass"}]}, {"number": 125, "title": "feat(server): detect a frozen host and say so", "headRefName": "feature/frozen-host", "baseRefName": "feature/error-detail", "headRefOid": "f7e6d5c", "reviewDecision": "APPROVED", "mergeable": "MERGEABLE", "checks": [{"name": "CI", "state": "pass"}]}]}}, "prompt": "Both PRs in the stack are approved and green. Land the stack.", "expect": {"calls": [{"tool": "pull_request_merge", "args": {"stack": true, "expectedHeadSha": "f7e6d5c"}, "argsAnyOf": [{"number": 125}, {"url": "~/pull/125"}, {"number": null, "url": null}]}], "maxCalls": {"pull_request_merge": 1}, "shellMustNot": ["gh stack merge", "gh pr merge", "gh api[^\\n]*/merge"], "callsMustNot": [{"tool": "pull_request_merge", "args": {"auto": true}}]}, "maxSteps": 8, "source": {"request": "juliusmarminge (maintainer) request, 2026-11-18: the PR tools could read stacks but not merge one; layer-by-layer merges restart CI above and can reorder a GitHub stack"}} {"id": "watch-for-teammate-merge-no-own-poll", "area": "pull-requests", "kind": "bad", "pattern": "assumes a merge will not wake it, so polls or asks the user", "experience": "Told a teammate would merge the PR and asked to report when it merged, agents read watch_pull_request's wake list (checks, comments, conflicts), concluded a merge would not wake them, and ran their own polling or asked the user to check back.", "rule": "watch_pull_request: T3 wakes you on ... and once on merge or close unless you did it via CLI", "privacy": "commit-safe", "setup": {"role": "top", "priorTurns": [{"role": "user", "text": "The HTML preview frame has no accessible name. Fix it and open a PR."}, {"role": "assistant", "text": "Opened [PR #124](https://github.com/acme/app/pull/124). The preview frame now has the title \"HTML preview\".", "toolCalls": [{"tool": "Bash", "input": {"command": "git push -u origin fix/html-preview-title && gh pr create --fill"}, "result": "https://github.com/acme/app/pull/124"}, {"tool": "link_pull_request", "input": {"url": "https://github.com/acme/app/pull/124"}, "result": {"host": "github.com", "repository": "acme/app", "number": 124, "url": "https://github.com/acme/app/pull/124", "alreadyLinked": false}}]}], "mocks": {"watch_pull_request": "default", "link_pull_request": "default"}, "world": {"branch": "fix/html-preview-title", "commits": [{"sha": "abc1234", "subject": "fix(web): HTML preview frame gets an accessible title"}], "diff": "diff --git a/apps/web/src/HtmlPreview.tsx b/apps/web/src/HtmlPreview.tsx\n--- a/apps/web/src/HtmlPreview.tsx\n+++ b/apps/web/src/HtmlPreview.tsx\n@@ -1,3 +1,3 @@\n export function HtmlPreview({ html }: { html: string }) {\n- return