Skip to content

Commit 8b3e057

Browse files
committed
fix(server): reject worktree delegation from a checkout outside the project
delegate_task resolved the base commit in the parent's checkout but created the child worktree from the project root. When the parent works in another repository, such as a Git folder inside Scratch, preparation failed after the child was created. Check that the project repository has the commit and fail the tool call with a clear error before creating the child.
1 parent e3b35d1 commit 8b3e057

2 files changed

Lines changed: 39 additions & 6 deletions

File tree

‎apps/server/src/mcp/OrchestratorMcpService.ts‎

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1951,6 +1951,21 @@ const make = Effect.gen(function* () {
19511951
failure("invalid_request", "Unable to resolve the parent checkout commit."),
19521952
),
19531953
);
1954+
// Launch preparation creates the worktree from the project root, so a
1955+
// parent checkout in another repository (such as a Git folder inside
1956+
// Scratch) would hand it a commit it cannot find.
1957+
if (parent.thread.worktreePath !== null) {
1958+
yield* gitDriver
1959+
.resolveCommit({ cwd: project.workspaceRoot, revision: base.commitSha })
1960+
.pipe(
1961+
Effect.mapError(() =>
1962+
failure(
1963+
"invalid_request",
1964+
`Isolation "worktree" creates the child checkout in project "${project.title}" (${project.workspaceRoot}), which does not contain the parent checkout's commit. Use isolation "shared" to work in the parent's checkout.`,
1965+
),
1966+
),
1967+
);
1968+
}
19541969
workspaceStrategy = { type: "worktree", baseRef: base.commitSha, startFromOrigin: false };
19551970
}
19561971
// ThreadManagementService.dispatch is the chokepoint for this command

‎apps/server/src/orchestration-v2/ThreadLaunchService.test.ts‎

Lines changed: 24 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2830,21 +2830,29 @@ it.effect.each(
28302830
{ failSetup: false, failResolve: false },
28312831
{ failSetup: true, failResolve: false },
28322832
{ failSetup: false, failResolve: true },
2833+
// Only a parent with its own checkout can hold a commit the project lacks.
2834+
...(driver === "codex" ? [] : [{ failSetup: false, failResolve: false, foreignBase: true }]),
28332835
{ failSetup: false, failResolve: false, cancelSetup: true },
28342836
{ failSetup: false, failResolve: false, cancelSetup: true, cancelAsync: true },
2835-
].map((failures) => ({ driver, cancelSetup: false, cancelAsync: false, ...failures })),
2837+
].map((failures) => ({
2838+
driver,
2839+
cancelSetup: false,
2840+
cancelAsync: false,
2841+
foreignBase: false,
2842+
...failures,
2843+
})),
28362844
),
28372845
)(
2838-
"prepares $driver delegated worktrees before execution, setup failure=$failSetup, commit failure=$failResolve, cancellation=$cancelSetup, async=$cancelAsync",
2839-
({ driver, failSetup, failResolve, cancelSetup, cancelAsync }) =>
2846+
"prepares $driver delegated worktrees before execution, setup failure=$failSetup, commit failure=$failResolve, base outside project=$foreignBase, cancellation=$cancelSetup, async=$cancelAsync",
2847+
({ driver, failSetup, failResolve, foreignBase, cancelSetup, cancelAsync }) =>
28402848
Effect.gen(function* () {
28412849
const setupEntered = yield* Deferred.make<void>();
28422850
const allowSetup = yield* Deferred.make<void>();
28432851
const terminalClosed = yield* Deferred.make<void>();
28442852
const childInstanceId = ProviderInstanceId.make(`child-${driver}`);
28452853
const parentWorktree = driver === "codex" ? null : "/parent-worktree";
2846-
const resolveCommit = vi.fn(() =>
2847-
failResolve
2854+
const resolveCommit = vi.fn((input: { readonly cwd: string; readonly revision: string }) =>
2855+
failResolve || (foreignBase && input.cwd === "/repo")
28482856
? Effect.fail(
28492857
new GitCommandError({
28502858
operation: "resolveCommit",
@@ -2975,6 +2983,15 @@ it.effect.each(
29752983
assert.equal(harness.createWorktree.mock.calls.length, 0);
29762984
return;
29772985
}
2986+
if (foreignBase) {
2987+
const error = yield* service.delegateTask(scope, input).pipe(Effect.flip);
2988+
assert.equal(error.code, "invalid_request");
2989+
assert.include(error.message, 'Isolation "worktree" creates the child checkout');
2990+
assert.notInclude(error.message, "private Git failure details");
2991+
assert.isEmpty((yield* threads.getThreadProjection(parentThreadId)).subagents);
2992+
assert.equal(harness.createWorktree.mock.calls.length, 0);
2993+
return;
2994+
}
29782995
const delegated = yield* service.delegateTask(scope, input);
29792996
yield* Deferred.await(setupEntered);
29802997
const child = yield* threads.getThreadProjection(delegated.childThreadId);
@@ -2985,11 +3002,12 @@ it.effect.each(
29853002
assert.equal(child.thread.modelSelection.model, "child-model");
29863003
assert.deepEqual(resolveCommit.mock.calls, [
29873004
[{ cwd: parentWorktree ?? "/repo", revision: "HEAD" }],
3005+
...(parentWorktree === null ? [] : [[{ cwd: "/repo", revision: "parent-local-commit" }]]),
29883006
]);
29893007
assert.equal(harness.createWorktree.mock.calls[0]?.[0].refName, "parent-local-commit");
29903008
assert.equal((yield* service.delegateTask(scope, input)).taskId, delegated.taskId);
29913009
assert.equal(harness.createWorktree.mock.calls.length, 1);
2992-
assert.equal(resolveCommit.mock.calls.length, 1);
3010+
assert.equal(resolveCommit.mock.calls.length, parentWorktree === null ? 1 : 2);
29933011
if (!cancelAsync) assert.isEmpty(child.checkpointScopes);
29943012
const parent = yield* threads.getThreadProjection(parentThreadId);
29953013
assert.equal(parent.thread.worktreePath, parentWorktree);

0 commit comments

Comments
 (0)