Skip to content

Commit dba8297

Browse files
authored
fix(server): reject worktree delegation from a checkout outside the project (pingdotgg#20213)
1 parent 04bd869 commit dba8297

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
@@ -1988,6 +1988,21 @@ const make = Effect.gen(function* () {
19881988
failure("invalid_request", "Unable to resolve the parent checkout commit."),
19891989
),
19901990
);
1991+
// Launch preparation creates the worktree from the project root, so a
1992+
// parent checkout in another repository (such as a Git folder inside
1993+
// Scratch) would hand it a commit it cannot find.
1994+
if (parent.thread.worktreePath !== null) {
1995+
yield* gitDriver
1996+
.resolveCommit({ cwd: project.workspaceRoot, revision: base.commitSha })
1997+
.pipe(
1998+
Effect.mapError(() =>
1999+
failure(
2000+
"invalid_request",
2001+
`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.`,
2002+
),
2003+
),
2004+
);
2005+
}
19912006
workspaceStrategy = { type: "worktree", baseRef: base.commitSha, startFromOrigin: false };
19922007
}
19932008
// 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
@@ -2837,21 +2837,29 @@ it.effect.each(
28372837
{ failSetup: false, failResolve: false },
28382838
{ failSetup: true, failResolve: false },
28392839
{ failSetup: false, failResolve: true },
2840+
// Only a parent with its own checkout can hold a commit the project lacks.
2841+
...(driver === "codex" ? [] : [{ failSetup: false, failResolve: false, foreignBase: true }]),
28402842
{ failSetup: false, failResolve: false, cancelSetup: true },
28412843
{ failSetup: false, failResolve: false, cancelSetup: true, cancelAsync: true },
2842-
].map((failures) => ({ driver, cancelSetup: false, cancelAsync: false, ...failures })),
2844+
].map((failures) => ({
2845+
driver,
2846+
cancelSetup: false,
2847+
cancelAsync: false,
2848+
foreignBase: false,
2849+
...failures,
2850+
})),
28432851
),
28442852
)(
2845-
"prepares $driver delegated worktrees before execution, setup failure=$failSetup, commit failure=$failResolve, cancellation=$cancelSetup, async=$cancelAsync",
2846-
({ driver, failSetup, failResolve, cancelSetup, cancelAsync }) =>
2853+
"prepares $driver delegated worktrees before execution, setup failure=$failSetup, commit failure=$failResolve, base outside project=$foreignBase, cancellation=$cancelSetup, async=$cancelAsync",
2854+
({ driver, failSetup, failResolve, foreignBase, cancelSetup, cancelAsync }) =>
28472855
Effect.gen(function* () {
28482856
const setupEntered = yield* Deferred.make<void>();
28492857
const allowSetup = yield* Deferred.make<void>();
28502858
const terminalClosed = yield* Deferred.make<void>();
28512859
const childInstanceId = ProviderInstanceId.make(`child-${driver}`);
28522860
const parentWorktree = driver === "codex" ? null : "/parent-worktree";
2853-
const resolveCommit = vi.fn(() =>
2854-
failResolve
2861+
const resolveCommit = vi.fn((input: { readonly cwd: string; readonly revision: string }) =>
2862+
failResolve || (foreignBase && input.cwd === "/repo")
28552863
? Effect.fail(
28562864
new GitCommandError({
28572865
operation: "resolveCommit",
@@ -2982,6 +2990,15 @@ it.effect.each(
29822990
assert.equal(harness.createWorktree.mock.calls.length, 0);
29832991
return;
29842992
}
2993+
if (foreignBase) {
2994+
const error = yield* service.delegateTask(scope, input).pipe(Effect.flip);
2995+
assert.equal(error.code, "invalid_request");
2996+
assert.include(error.message, 'Isolation "worktree" creates the child checkout');
2997+
assert.notInclude(error.message, "private Git failure details");
2998+
assert.isEmpty((yield* threads.getThreadProjection(parentThreadId)).subagents);
2999+
assert.equal(harness.createWorktree.mock.calls.length, 0);
3000+
return;
3001+
}
29853002
const delegated = yield* service.delegateTask(scope, input);
29863003
yield* Deferred.await(setupEntered);
29873004
const child = yield* threads.getThreadProjection(delegated.childThreadId);
@@ -2992,11 +3009,12 @@ it.effect.each(
29923009
assert.equal(child.thread.modelSelection.model, "child-model");
29933010
assert.deepEqual(resolveCommit.mock.calls, [
29943011
[{ cwd: parentWorktree ?? "/repo", revision: "HEAD" }],
3012+
...(parentWorktree === null ? [] : [[{ cwd: "/repo", revision: "parent-local-commit" }]]),
29953013
]);
29963014
assert.equal(harness.createWorktree.mock.calls[0]?.[0].refName, "parent-local-commit");
29973015
assert.equal((yield* service.delegateTask(scope, input)).taskId, delegated.taskId);
29983016
assert.equal(harness.createWorktree.mock.calls.length, 1);
2999-
assert.equal(resolveCommit.mock.calls.length, 1);
3017+
assert.equal(resolveCommit.mock.calls.length, parentWorktree === null ? 1 : 2);
30003018
if (!cancelAsync) assert.isEmpty(child.checkpointScopes);
30013019
const parent = yield* threads.getThreadProjection(parentThreadId);
30023020
assert.equal(parent.thread.worktreePath, parentWorktree);

0 commit comments

Comments
 (0)