@devries-cameron20 / t3code Mirror · read-only / commits / c2300780e0
Fork 0

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

Mirrored from https://github.com/Camo67/t3code.git

Synced Nov 21, 22:15 UTC

fix(server): reject worktree launches in non-Git projects (#34279)

Co-authored-by: Julius Marminge <julius0216@outlook.com>

Lakshmi Tanmay committed Nov 12, 2026 at 02:53 UTC c2300780e008e021a8c771a2a79f9705204fcfc0
3 files changed +101 -10
apps/server/src/mcp/toolkits/project/handlers.ts
+8 -5
@@ -171,11 +171,14 @@ export const layer = McpToolAccess.toLayer(ProjectToolkit, {
171 (error) =>
172 new OrchestratorMcpFailure({
173 code: "invalid_request",
174 - message: `${error.message} No thread was created. Pass a branch, tag, or commit that exists there${
175 - strategy.startFromOrigin === true
176 - ? "; the project has no usable remote to fetch it from."
177 - : ", or set startFromOrigin:true if it exists only on a remote."
178 - }`,
174 + message:
175 + error._tag === "ThreadLaunchNotRepositoryError"
176 + ? `${error.message} No thread was created. Use workspaceStrategy: {type: "root"}, or pass the projectId of a Git repository.`
177 + : `${error.message} No thread was created. Pass a branch, tag, or commit that exists there${
178 + strategy.startFromOrigin === true
179 + ? "; the project has no usable remote to fetch it from."
180 + : ", or set startFromOrigin:true if it exists only on a remote."
181 + }`,
182 }),
183 ),
184 );
apps/server/src/orchestration-v2/ThreadLaunchService.test.ts
+61
@@ -203,6 +203,7 @@ function makeHarness(options: HarnessOptions = {}) {
203 listShells: () => Effect.die("unused"),
204 }),
205 Layer.mock(GitWorkflow.GitWorkflowService)({
206 + repositoryRoot: () => Effect.succeed(project.workspaceRoot),
207 createWorktree,
208 renameBranch,
209 fetchWorktreeBaseCommit:
@@ -911,6 +912,7 @@ function scratchHarness() {
912 return Option.some(`/scratch/folder-${claimed.length}`);
913 }),
914 }),
915 + git: { repositoryRoot: (cwd) => Effect.succeed(cwd === scratchRoot ? null : cwd) },
916 remoteExists: (input) =>
917 input.cwd === scratchRoot
918 ? Effect.fail(notARepository("GitWorkflowService.remoteExists", input.cwd))
@@ -982,6 +984,7 @@ it.effect("launches a worktree request for Scratch at the root, in its own folde
984 const harness = scratchHarness();
985 return Effect.gen(function* () {
986 const launches = yield* ThreadLaunch.ThreadLaunchService;
987 + yield* launches.checkWorktreeBase({ projectId, workspaceStrategy: worktreeStrategy });
988 const launched = yield* launches.launch(
989 launchInput({
990 command: "command:launch:scratch-worktree",
@@ -2406,6 +2409,64 @@ it.effect.each([0, 1])("releases an async setup before its completion with exit
2409 }),
2410 );
2411
2412 +it.effect.each(["origin/master", ":/initial"])(
2413 + "refuses a worktree in a non-Git project before checking base %s",
2414 + (baseRef) =>
2415 + Effect.gen(function* () {
2416 + const harness = makeHarness({
2417 + git: { repositoryRoot: () => Effect.succeed(null) },
2418 + });
2419 + yield* Effect.gen(function* () {
2420 + const launches = yield* ThreadLaunch.ThreadLaunchService;
2421 + const result = yield* launches
2422 + .checkWorktreeBase({
2423 + projectId,
2424 + workspaceStrategy: { type: "worktree", baseRef, startFromOrigin: true },
2425 + })
2426 + .pipe(Effect.result);
2427 + assert.isTrue(Result.isFailure(result));
2428 + if (Result.isFailure(result)) {
2429 + assert.instanceOf(result.failure, ThreadLaunch.ThreadLaunchNotRepositoryError);
2430 + assert.deepInclude(result.failure, {
2431 + projectId,
2432 + projectTitle: project.title,
2433 + workspaceRoot: project.workspaceRoot,
2434 + });
2435 + }
2436 + yield* launches.checkWorktreeBase({ projectId, workspaceStrategy: { type: "root" } });
2437 + yield* launches.checkWorktreeBase({
2438 + projectId,
2439 + workspaceStrategy: { type: "existing_worktree", worktreePath: "/existing-worktree" },
2440 + });
2441 + }).pipe(Effect.provide(harness.layer));
2442 + }),
2443 +);
2444 +
2445 +it.effect("leaves uncertain Git detection failures to workspace preparation", () =>
2446 + Effect.gen(function* () {
2447 + const harness = makeHarness({
2448 + git: {
2449 + repositoryRoot: () =>
2450 + Effect.fail(
2451 + new GitCommandError({
2452 + operation: "detect-repository",
2453 + command: "git",
2454 + cwd: project.workspaceRoot,
2455 + detail: "Detection failed.",
2456 + }),
2457 + ),
2458 + },
2459 + hasCommit: () => Effect.succeed(true),
2460 + });
2461 + yield* ThreadLaunch.ThreadLaunchService.pipe(
2462 + Effect.flatMap((launches) =>
2463 + launches.checkWorktreeBase({ projectId, workspaceStrategy: worktreeStrategy }),
2464 + ),
2465 + Effect.provide(harness.layer),
2466 + );
2467 + }),
2468 +);
2469 +
2470 it.effect.each([
2471 { name: "is missing", baseRef: "t3/renamed", startFromOrigin: false, origin: true },
2472 {
apps/server/src/orchestration-v2/ThreadLaunchService.ts
+32 -5
@@ -166,6 +166,19 @@ export class ThreadLaunchBaseRefError extends Schema.TaggedError<ThreadLaunchBas
166 }
167 }
168
169 +export class ThreadLaunchNotRepositoryError extends Schema.TaggedError<ThreadLaunchNotRepositoryError>()(
170 + "ThreadLaunchNotRepositoryError",
171 + {
172 + projectId: ProjectId,
173 + projectTitle: Schema.String,
174 + workspaceRoot: Schema.String,
175 + },
176 +) {
177 + override get message(): string {
178 + return `Project "${this.projectTitle}" (${this.workspaceRoot}) is not a Git repository.`;
179 + }
180 +}
181 +
182 export class ThreadLaunchService extends Context.Service<
183 ThreadLaunchService,
184 {
@@ -178,7 +191,7 @@ export class ThreadLaunchService extends Context.Service<
191 /** Check before creation for early errors; launch otherwise accepts and reports base failures inside the thread. */
192 readonly checkWorktreeBase: (
193 input: Pick<ThreadLaunchInput, "projectId" | "workspaceStrategy">,
181 - ) => Effect.Effect<void, ThreadLaunchBaseRefError>;
194 + ) => Effect.Effect<void, ThreadLaunchBaseRefError | ThreadLaunchNotRepositoryError>;
195 readonly retryPreparation: (
196 input: ThreadLaunchPreparationInput,
197 ) => Effect.Effect<
@@ -263,13 +276,27 @@ const make = Effect.gen(function* () {
276 "ThreadLaunchService.checkWorktreeBase",
277 )(function* (input) {
278 const strategy = input.workspaceStrategy;
279 + // launch() turns a Scratch worktree request into a root launch.
280 + if (strategy.type !== "worktree" || (yield* managedFolders.isScratchProject(input.projectId)))
281 + return;
282 + const project = Option.getOrUndefined(
283 + yield* projects.getById(input.projectId).pipe(Effect.orElseSucceed(() => Option.none())),
284 + );
285 + if (project === undefined) return;
286 + const cwd = project.workspaceRoot;
287 + const repositoryRoot = yield* git
288 + .repositoryRoot(cwd)
289 + .pipe(Effect.orElseSucceed(() => undefined));
290 + if (repositoryRoot === null)
291 + return yield* new ThreadLaunchNotRepositoryError({
292 + projectId: project.id,
293 + projectTitle: project.title,
294 + workspaceRoot: cwd,
295 + });
296 // `:/text` is a commit search, which cannot be asked for a commit type.
267 - if (strategy.type !== "worktree" || strategy.baseRef.startsWith(":")) return;
297 + if (strategy.baseRef.startsWith(":")) return;
298 const { baseRef } = strategy;
299 const unresolved = yield* Effect.gen(function* () {
270 - const project = Option.getOrUndefined(yield* projects.getById(input.projectId));
271 - if (project === undefined) return undefined;
272 - const cwd = project.workspaceRoot;
300 // `git worktree add` reads `-` as the previous checkout.
301 if (yield* git.hasCommit({ cwd, refName: baseRef === "-" ? "@{-1}" : baseRef }))
302 return undefined;