Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
66 changes: 66 additions & 0 deletions apps/server/src/mcp/OrchestratorMcpService.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<ReadonlyArray<ScheduledTask["workspaceStrategy"]>>([]);
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<ReadonlyArray<ScheduledTask["workspaceStrategy"]>>([]);
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<ReadonlyArray<ScheduledTask["workspaceStrategy"]>>([]);
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",
Expand Down
41 changes: 35 additions & 6 deletions apps/server/src/mcp/OrchestratorMcpService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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 }),
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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.
Expand All @@ -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,
Expand Down Expand Up @@ -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,
Expand Down
12 changes: 11 additions & 1 deletion apps/server/src/mcp/toolkits/orchestrator/handlers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -119,6 +128,7 @@ const handlers = {
enabled,
freshSession,
bindToCurrentThread,
workspace,
});
}),
),
Expand Down
2 changes: 1 addition & 1 deletion apps/server/src/provider/agentContextBudget.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{
"claudeSystemAppendBytes": 6245,
"codexAdditionalContextBytes": 7639,
"mcpToolListJsonBytes": 59026
"mcpToolListJsonBytes": 59176
}
4 changes: 3 additions & 1 deletion docs/orchestration-v2/orchestrator-mcp-server.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
2 changes: 2 additions & 0 deletions evals/mcp-tool-use/cases/pull-requests.ndjson
Original file line number Diff line number Diff line change
Expand Up @@ -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 <iframe sandbox=\"allow-scripts\" srcDoc={html} height={400} />;\n+ return <iframe title=\"HTML preview\" sandbox=\"allow-scripts\" srcDoc={html} height={400} />;\n }\n", "prs": [{"number": 124, "title": "fix(web): HTML preview frame gets an accessible title", "headRefName": "fix/html-preview-title", "reviewDecision": "REVIEW_REQUIRED", "reviewRequests": ["sam-acme"], "checks": [{"name": "Lint", "state": "pass"}, {"name": "Typecheck", "state": "pass"}, {"name": "Test Web", "state": "pass"}]}], "files": {"apps/web/src/HtmlPreview.tsx": "export function HtmlPreview({ html }: { html: string }) {\n return <iframe title=\"HTML preview\" sandbox=\"allow-scripts\" srcDoc={html} height={400} />;\n}\n"}}}, "prompt": "Sam will merge PR #124 once they've reviewed it. Watch it and tell me when it's merged.", "expect": {"calls": [{"tool": "watch_pull_request"}], "notCalls": ["Monitor", "ScheduleWakeup", "CronCreate", "schedule_task", "pull_request_merge"], "shellMustNot": ["\\bsleep \\d", "gh pr checks[^\\n]*--watch", "gh run watch", "\\buntil\\b[^\\n]*\\bgh (pr|run|api)\\b", "\\bwhile\\b[^\\n]*\\bgh (pr|api)\\b", "gh pr merge"], "textMustNot": ["(won't|will not|doesn't|does not|can't|cannot)[^.\\n]{0,40}(wake|notify|alert)[^.\\n]{0,40}(merge|merging|merged)", "(merge|merging|merged)[^.\\n]{0,40}(won't|will not|doesn't|does not)[^.\\n]{0,20}(wake|notify|trigger)", "check back"], "endsTurnAfter": "watch_pull_request"}, "notes": "pingdotgg/t3code#39726: since #26271 a watched thread is woken once when its PR merges or closes, but the tool description listed only checks, comments and conflicts. The agent will not merge it, so any gh pr merge fails. until may be either value."}
{"id": "schedule-new-thread-in-checkout", "area": "pull-requests", "kind": "bad", "pattern": "workspace asked, agent defers it to Settings", "experience": "Asked for a daily task that starts a new thread each run in the project checkout, agents created it with a new worktree per run and asked the user to switch Workspace to 'Use the project checkout' in Settings, because schedule_task had no workspace input.", "source": {"issue": "https://github.com/pingdotgg/t3code/issues/32491", "note": "Scheduled runs that had to share the project checkout asked the user, again and again, to set Workspace in Settings."}, "rule": "schedule_task workspace: Unbound only: the project checkout, or a new worktree per run (default).", "privacy": "commit-safe", "setup": {"role": "top", "context": "Today is Friday 2026-11-20, 15:00 local time.", "mocks": {"schedule_task": {"scheduledTaskId": "scheduled-task-7", "title": "Upload i18n screenshots", "prompt": "Run scripts/i18n-screenshots.sh and upload the results.", "enabled": true, "projectId": "project-acme-app", "boundThreadId": null, "freshSession": false, "schedule": {"type": "fixed_time", "timeOfDay": "09:00"}, "nextRunAt": "2026-11-21T09:00:00-08:00", "lastRunStatus": "never", "workspace": "checkout"}}, "shellMocks": [{"match": "\\bdate\\b", "output": "Friday 2026-11-20 15:00 PST"}]}, "prompt": "Set up a daily 9am job that runs scripts/i18n-screenshots.sh and uploads the results. Give each run its own new thread, but have it work in the project checkout, not a fresh worktree. The repo is huge.", "expect": {"calls": [{"tool": "schedule_task", "args": {"workspace": "checkout", "bindToCurrentThread": false}}], "notCalls": ["delegate_task", "run_scheduled_task_now"], "callsMustNot": [{"tool": "schedule_task", "args": {"workspace": "worktree"}}, {"tool": "schedule_task", "args": {"freshSession": true}}]}, "notes": "Before schedule_task took workspace, the only way to get new threads in the checkout was the Settings editor."}
Comment thread
coderabbitai[bot] marked this conversation as resolved.
Resolved
{"id": "control-schedule-new-thread-default-workspace", "area": "pull-requests", "kind": "control", "pattern": "new thread per run, no workspace asked", "experience": "Control: the user asks for a new thread each run and says nothing about the workspace. The default worktree is right; choosing the project checkout was not asked.", "source": {"pullRequest": "https://github.com/pingdotgg/t3code/pull/39760", "note": "Guards the checkout case against agents that start sending workspace: \"checkout\" for every new-thread task."}, "rule": "schedule_task workspace: Unbound only: the project checkout, or a new worktree per run (default).", "privacy": "commit-safe", "setup": {"role": "top", "context": "Today is Friday 2026-11-20, 15:00 local time.", "mocks": {"schedule_task": {"scheduledTaskId": "scheduled-task-8", "title": "Nightly test run", "prompt": "Run the test suite and report failures.", "enabled": true, "projectId": "project-acme-app", "boundThreadId": null, "freshSession": false, "schedule": {"type": "fixed_time", "timeOfDay": "02:00"}, "nextRunAt": "2026-11-21T02:00:00-08:00", "lastRunStatus": "never", "workspace": "worktree"}}, "shellMocks": [{"match": "\\bdate\\b", "output": "Friday 2026-11-20 15:00 PST"}]}, "prompt": "Set up a nightly 2am job that runs the test suite and reports any failures. Give each run its own new thread. Don't run it now.", "expect": {"calls": [{"tool": "schedule_task", "args": {"bindToCurrentThread": false}}], "notCalls": ["delegate_task", "run_scheduled_task_now"], "callsMustNot": [{"tool": "schedule_task", "args": {"workspace": "checkout"}}, {"tool": "schedule_task", "args": {"freshSession": true}}]}, "notes": "Control for schedule-new-thread-in-checkout."}
Loading
Loading