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

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

fix(server): t3_thread_launch without a workspace follows the worktree default (#38261)

Julius Marminge committed Nov 17, 2026 at 10:33 UTC e5d757f51b342a5ebc50312790e39a1baca61194
2 files changed +125 -5
apps/server/src/mcp/toolkits/project/handlers.test.ts
+53
@@ -51,6 +51,7 @@ const clientLaunchHarness = (input: {
51 readonly runtimeModeCeiling: RuntimeMode;
52 readonly defaultRuntimeMode?: RuntimeMode;
53 readonly newWorktreesStartFromOrigin?: boolean;
54 + readonly defaultThreadEnvMode?: "local" | "worktree";
55 readonly fallbackModel?: ModelSelection;
56 readonly providers?: readonly ServerProvider[];
57 /** Where Settings saves a project's model; the legacy project field stays null. */
@@ -83,6 +84,9 @@ const clientLaunchHarness = (input: {
84 ...(input.newWorktreesStartFromOrigin === undefined
85 ? {}
86 : { newWorktreesStartFromOrigin: input.newWorktreesStartFromOrigin }),
87 + ...(input.defaultThreadEnvMode === undefined
88 + ? {}
89 + : { defaultThreadEnvMode: input.defaultThreadEnvMode }),
90 ...(input.overrideModel === undefined
91 ? {}
92 : {
@@ -684,6 +688,55 @@ it.effect("a worktree launch that omits startFromOrigin follows the new-worktree
688 }),
689 );
690
691 +it.effect("a launch that omits its workspace follows the new-threads-in-a-worktree default", () =>
692 + Effect.gen(function* () {
693 + const fileSystem = yield* FileSystem.FileSystem;
694 + for (const source of ["setting", "t3.json"] as const) {
695 + const repo = yield* fileSystem.makeTempDirectoryScoped({ prefix: "t3-launch-env-mode-" });
696 + const launched: Array<ThreadLaunch.ThreadLaunchInput> = [];
697 + const { projectId, dependencies } = clientLaunchHarness({
698 + runtimeModeCeiling: "auto",
699 + launched,
700 + workspaceRoot: repo,
701 + ...(source === "setting" ? { defaultThreadEnvMode: "worktree" as const } : {}),
702 + });
703 + const git = yield* GitVcsDriver.GitVcsDriver.pipe(Effect.provide(dependencies));
704 + for (const args of [
705 + ["init", "-b", "main"],
706 + ["-c", "user.email=t@t", "-c", "user.name=t", "commit", "--allow-empty", "-m", "init"],
707 + ]) {
708 + yield* git.execute({ operation: "test.setupRepo", cwd: repo, args });
709 + }
710 + if (source === "t3.json")
711 + yield* fileSystem.writeFileString(
712 + `${repo}/t3.json`,
713 + JSON.stringify({ defaultThreadEnvMode: "worktree" }),
714 + );
715 + const toolkit = yield* ProjectToolkit.pipe(
716 + Effect.provide(
717 + McpToolAccess.HandlersLayer.layer(ProjectHandlers.layer).pipe(
718 + Layer.provide(dependencies),
719 + ),
720 + ),
721 + );
722 + for (const workspaceStrategy of [undefined, { type: "root" as const }]) {
723 + yield* toolkit
724 + .handle("t3_thread_launch", {
725 + title: "Fix",
726 + projectId,
727 + ...(workspaceStrategy === undefined ? {} : { workspaceStrategy }),
728 + })
729 + .pipe(Stream.unwrap, Stream.runCollect, Effect.provide(dependencies));
730 + }
731 +
732 + expect(
733 + launched.map((launch) => launch.workspaceStrategy.type),
734 + source,
735 + ).toEqual(["worktree", "root"]);
736 + }
737 + }).pipe(Effect.scoped, Effect.provide(NodeServices.layer)),
738 +);
739 +
740 it.effect("a launch into a new worktree creates nothing when its branch collides", () =>
741 Effect.gen(function* () {
742 const launched: Array<ThreadLaunch.ThreadLaunchInput> = [];
apps/server/src/mcp/toolkits/project/handlers.ts
+72 -5
@@ -3,7 +3,9 @@ import {
3 MessageId,
4 ThreadId,
5 OrchestratorMcpFailure,
6 + type OrchestrationV2ThreadLaunchWorkspaceStrategy,
7 ProjectId,
8 + T3_PROJECT_FILE_NAME,
9 } from "@t3tools/contracts";
10 import * as Digest from "effect/crypto/Digest";
11 import * as Effect from "effect/Effect";
@@ -25,7 +27,11 @@ import * as ServerSettings from "../../../serverSettings.ts";
27 import * as ProviderRegistry from "../../../provider/ProviderRegistry.ts";
28 import * as DateTime from "effect/DateTime";
29 import { resolveNewThreadModelSelection } from "@t3tools/shared/usageLimits";
28 -import { resolveProjectSettings } from "@t3tools/shared/projectSettings";
30 +import {
31 + resolveProjectFileBackedSetting,
32 + resolveProjectSettings,
33 +} from "@t3tools/shared/projectSettings";
34 +import { parseT3ProjectFile } from "@t3tools/shared/t3ProjectFile";
35 import { resolveRuntimeMode } from "../../delegatedTaskModes.ts";
36 import * as McpInvocationContext from "../../McpInvocationContext.ts";
37 import * as McpToolAccess from "../../McpToolAccess.ts";
@@ -93,6 +99,56 @@ const assertProjectWorktree = Effect.fn("mcp.assertProjectWorktree")(function* (
99 });
100 });
101
102 +/** The composer falls back to the project's t3.json when no setting picks the mode. */
103 +const projectFileThreadEnvMode = Effect.fn("mcp.projectFileThreadEnvMode")(function* (
104 + workspaceRoot: string,
105 +) {
106 + const fileSystem = yield* FileSystem.FileSystem;
107 + const contents = yield* fileSystem
108 + .readFileString(`${workspaceRoot}/${T3_PROJECT_FILE_NAME}`)
109 + .pipe(Effect.orElseSucceed(() => null));
110 + return resolveProjectFileBackedSetting(
111 + "defaultThreadEnvMode",
112 + null,
113 + contents === null ? null : parseT3ProjectFile(contents),
114 + ).value;
115 +});
116 +
117 +/** Branches from the checkout's current branch, as the composer does; a checkout with no commit or a detached HEAD stays shared. */
118 +const defaultWorktreeStrategy = Effect.fn("mcp.defaultWorktreeStrategy")(function* (
119 + workspaceRoot: string,
120 +): Effect.fn.Return<
121 + OrchestrationV2ThreadLaunchWorkspaceStrategy | undefined,
122 + OrchestratorMcpFailure,
123 + GitVcsDriver.GitVcsDriver
124 +> {
125 + const git = yield* GitVcsDriver.GitVcsDriver;
126 + const hasCommit = yield* git.resolveCommit({ cwd: workspaceRoot, revision: "HEAD" }).pipe(
127 + Effect.as(true),
128 + Effect.orElseSucceed(() => false),
129 + );
130 + if (!hasCommit) return undefined;
131 + // symbolic-ref skips the index, so a held index lock cannot drop the launch into the shared checkout.
132 + const head = yield* git
133 + .execute({
134 + operation: "mcp.defaultWorktreeStrategy",
135 + cwd: workspaceRoot,
136 + args: ["symbolic-ref", "--quiet", "--short", "HEAD"],
137 + allowNonZeroExit: true,
138 + timeoutMs: 5_000,
139 + })
140 + .pipe(Effect.orElseSucceed(() => null));
141 + if (head?.exitCode === 1) return undefined;
142 + const branch = head?.exitCode === 0 ? head.stdout.trim() : "";
143 + if (branch.length === 0)
144 + return yield* new OrchestratorMcpFailure({
145 + code: "orchestration_error",
146 + message:
147 + "Could not read the project's current branch for the new worktree. No thread was created. Retry, or pass workspaceStrategy.",
148 + });
149 + return { type: "worktree" as const, baseRef: branch };
150 +});
151 +
152 /** A fixed-length id, so a long key fits in the scratch folder name derived from it. */
153 const launchCommandId = Effect.fn("mcp.launchCommandId")(function* (
154 namespace: string,
@@ -207,17 +263,28 @@ export const layer = McpToolAccess.toLayer(ProjectToolkit, {
263 (settings) => resolveProjectSettings(settings, projectId, project).settings,
264 ),
265 );
266 + // An outside caller that names no workspace follows the new-threads-in-a-worktree
267 + // default, as the composer does.
268 + const requestedStrategy =
269 + input.workspaceStrategy === undefined &&
270 + caller === undefined &&
271 + project !== undefined &&
272 + projectSettings !== undefined &&
273 + (projectSettings.defaultThreadEnvMode ??
274 + (yield* projectFileThreadEnvMode(project.workspaceRoot))) === "worktree"
275 + ? yield* defaultWorktreeStrategy(project.workspaceRoot)
276 + : input.workspaceStrategy;
277 // An omitted startFromOrigin follows the new-worktree setting, as the composer and
278 // t3_worktree_handoff do. Otherwise a stale local branch silently becomes the base.
279 const strategy =
280 projectSettings !== undefined &&
214 - input.workspaceStrategy?.type === "worktree" &&
215 - input.workspaceStrategy.startFromOrigin === undefined
281 + requestedStrategy?.type === "worktree" &&
282 + requestedStrategy.startFromOrigin === undefined
283 ? {
217 - ...input.workspaceStrategy,
284 + ...requestedStrategy,
285 startFromOrigin: projectSettings.newWorktreesStartFromOrigin,
286 }
220 - : input.workspaceStrategy;
287 + : requestedStrategy;
288 // Reject unresolved base refs and blocked branch names before accepting a launch; worktree provisioning happens later.
289 if (!replaying && strategy?.type === "worktree")
290 yield* ThreadLaunch.ThreadLaunchService.pipe(