@devries-cameron20 / t3code-1 / commits / dba8297da4
Fork 0

The best way to control Claude Code, Codex and more from any computer or phone

fix(server): reject worktree delegation from a checkout outside the project (#20213)

Nikita Maximilian Grimm committed Oct 18, 2026 at 21:11 UTC dba8297da4f25cb1063d623ec8a4bf3ec1f01715
2 files changed +39 -6
apps/server/src/mcp/OrchestratorMcpService.ts
+15
@@ -1988,6 +1988,21 @@ const make = Effect.gen(function* () {
1988 failure("invalid_request", "Unable to resolve the parent checkout commit."),
1989 ),
1990 );
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 + }
2006 workspaceStrategy = { type: "worktree", baseRef: base.commitSha, startFromOrigin: false };
2007 }
2008 // ThreadManagementService.dispatch is the chokepoint for this command
apps/server/src/orchestration-v2/ThreadLaunchService.test.ts
+24 -6
@@ -2837,21 +2837,29 @@ it.effect.each(
2837 { failSetup: false, failResolve: false },
2838 { failSetup: true, failResolve: false },
2839 { 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 }]),
2842 { failSetup: false, failResolve: false, cancelSetup: true },
2843 { 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 + })),
2851 ),
2852 )(
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 }) =>
2855 Effect.gen(function* () {
2856 const setupEntered = yield* Deferred.make<void>();
2857 const allowSetup = yield* Deferred.make<void>();
2858 const terminalClosed = yield* Deferred.make<void>();
2859 const childInstanceId = ProviderInstanceId.make(`child-${driver}`);
2860 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")
2863 ? Effect.fail(
2864 new GitCommandError({
2865 operation: "resolveCommit",
@@ -2982,6 +2990,15 @@ it.effect.each(
2990 assert.equal(harness.createWorktree.mock.calls.length, 0);
2991 return;
2992 }
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 + }
3002 const delegated = yield* service.delegateTask(scope, input);
3003 yield* Deferred.await(setupEntered);
3004 const child = yield* threads.getThreadProjection(delegated.childThreadId);
@@ -2992,11 +3009,12 @@ it.effect.each(
3009 assert.equal(child.thread.modelSelection.model, "child-model");
3010 assert.deepEqual(resolveCommit.mock.calls, [
3011 [{ cwd: parentWorktree ?? "/repo", revision: "HEAD" }],
3012 + ...(parentWorktree === null ? [] : [[{ cwd: "/repo", revision: "parent-local-commit" }]]),
3013 ]);
3014 assert.equal(harness.createWorktree.mock.calls[0]?.[0].refName, "parent-local-commit");
3015 assert.equal((yield* service.delegateTask(scope, input)).taskId, delegated.taskId);
3016 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);
3018 if (!cancelAsync) assert.isEmpty(child.checkpointScopes);
3019 const parent = yield* threads.getThreadProjection(parentThreadId);
3020 assert.equal(parent.thread.worktreePath, parentWorktree);