@devries-cameron20 / t3code Mirror · read-only / commits / dfd2187ccb
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, 13:30 UTC

fix(server): t3_thread_launch worktrees honor the new-worktree setting (#30226)

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

Brett Lyman committed Nov 14, 2026 at 18:10 UTC dfd2187ccb0f7d16b0ff6d13f79eb5236ca9c46f
7 files changed +172 -83
apps/server/src/mcp/toolkits/project/handlers.test.ts
+49
@@ -50,6 +50,7 @@ import { ProjectToolkit } from "./tools.ts";
50 const clientLaunchHarness = (input: {
51 readonly runtimeModeCeiling: RuntimeMode;
52 readonly defaultRuntimeMode?: RuntimeMode;
53 + readonly newWorktreesStartFromOrigin?: boolean;
54 readonly fallbackModel?: ModelSelection;
55 readonly providers?: readonly ServerProvider[];
56 /** Where Settings saves a project's model; the legacy project field stays null. */
@@ -79,6 +80,9 @@ const clientLaunchHarness = (input: {
80 ...(input.defaultRuntimeMode === undefined
81 ? {}
82 : { defaultRuntimeMode: input.defaultRuntimeMode }),
83 + ...(input.newWorktreesStartFromOrigin === undefined
84 + ? {}
85 + : { newWorktreesStartFromOrigin: input.newWorktreesStartFromOrigin }),
86 ...(input.overrideModel === undefined
87 ? {}
88 : {
@@ -604,6 +608,51 @@ it.effect("a launch into a new worktree creates nothing when its base ref has no
608 }),
609 );
610
611 +it.effect("a worktree launch that omits startFromOrigin follows the new-worktree setting", () =>
612 + Effect.gen(function* () {
613 + const cases = [
614 + { setting: true, given: undefined, expected: true },
615 + { setting: false, given: undefined, expected: false },
616 + { setting: true, given: false, expected: false },
617 + ];
618 + for (const { setting, given, expected } of cases) {
619 + const launched: Array<ThreadLaunch.ThreadLaunchInput> = [];
620 + const checked: Array<ThreadLaunch.ThreadLaunchInput["workspaceStrategy"]> = [];
621 + const { projectId, dependencies } = clientLaunchHarness({
622 + runtimeModeCeiling: "auto",
623 + launched,
624 + newWorktreesStartFromOrigin: setting,
625 + checkWorktreeBase: ({ workspaceStrategy }) =>
626 + Effect.sync(() => {
627 + checked.push(workspaceStrategy);
628 + }),
629 + });
630 + const toolkit = yield* ProjectToolkit.pipe(
631 + Effect.provide(
632 + McpToolAccess.HandlersLayer.layer(ProjectHandlers.layer).pipe(
633 + Layer.provide(dependencies),
634 + ),
635 + ),
636 + );
637 + yield* toolkit
638 + .handle("t3_thread_launch", {
639 + title: "Fix",
640 + projectId,
641 + workspaceStrategy: {
642 + type: "worktree",
643 + baseRef: "main",
644 + ...(given === undefined ? {} : { startFromOrigin: given }),
645 + },
646 + })
647 + .pipe(Stream.unwrap, Stream.runCollect, Effect.provide(dependencies));
648 +
649 + const strategy = { type: "worktree", baseRef: "main", startFromOrigin: expected };
650 + expect(checked).toEqual([strategy]);
651 + expect(launched.map((launch) => launch.workspaceStrategy)).toEqual([strategy]);
652 + }
653 + }),
654 +);
655 +
656 it.effect("a launch into a new worktree creates nothing when its branch collides", () =>
657 Effect.gen(function* () {
658 const launched: Array<ThreadLaunch.ThreadLaunchInput> = [];
apps/server/src/mcp/toolkits/project/handlers.ts
+23 -12
@@ -188,8 +188,28 @@ export const layer = McpToolAccess.toLayer(ProjectToolkit, {
188 if (provider !== undefined)
189 yield* OrchestrationMcp.requireAdvertisedModel(provider, model);
190 }
191 + const projectSettings =
192 + project === undefined
193 + ? undefined
194 + : yield* ServerSettings.ServerSettingsService.pipe(
195 + Effect.flatMap((settings) => settings.getSettings),
196 + Effect.mapError(unavailable),
197 + Effect.map(
198 + (settings) => resolveProjectSettings(settings, projectId, project).settings,
199 + ),
200 + );
201 + // An omitted startFromOrigin follows the new-worktree setting, as the composer and
202 + // t3_worktree_handoff do. Otherwise a stale local branch silently becomes the base.
203 + const strategy =
204 + projectSettings !== undefined &&
205 + input.workspaceStrategy?.type === "worktree" &&
206 + input.workspaceStrategy.startFromOrigin === undefined
207 + ? {
208 + ...input.workspaceStrategy,
209 + startFromOrigin: projectSettings.newWorktreesStartFromOrigin,
210 + }
211 + : input.workspaceStrategy;
212 // Reject unresolved base refs and blocked branch names before accepting a launch; worktree provisioning happens later.
192 - const strategy = input.workspaceStrategy;
213 if (!replaying && strategy?.type === "worktree")
214 yield* ThreadLaunch.ThreadLaunchService.pipe(
215 Effect.flatMap((launches) =>
@@ -206,16 +226,7 @@ export const layer = McpToolAccess.toLayer(ProjectToolkit, {
226 // A caller outside a thread has nothing to inherit, so it starts with the
227 // project's effective defaults, as the composer does. Its mode stays
228 // within the ceiling it was approved with.
209 - const projectDefaults =
210 - caller === undefined && project !== undefined
211 - ? yield* ServerSettings.ServerSettingsService.pipe(
212 - Effect.flatMap((settings) => settings.getSettings),
213 - Effect.mapError(unavailable),
214 - Effect.map(
215 - (settings) => resolveProjectSettings(settings, projectId, project).settings,
216 - ),
217 - )
218 - : undefined;
229 + const projectDefaults = caller === undefined ? projectSettings : undefined;
230 const modelSelection =
231 input.modelSelection ??
232 launchedModel ??
@@ -262,7 +273,7 @@ export const layer = McpToolAccess.toLayer(ProjectToolkit, {
273 modelSelection,
274 runtimeMode: startedRuntimeMode,
275 interactionMode,
265 - workspaceStrategy: input.workspaceStrategy ?? { type: "root" },
276 + workspaceStrategy: strategy ?? { type: "root" },
277 ...(input.message === undefined && attachments.length === 0
278 ? {}
279 : {
apps/server/src/orchestration-v2/ThreadLaunchService.test.ts
+73 -58
@@ -2176,67 +2176,82 @@ it.effect(
2176 }),
2177 );
2178
2179 -it.effect("schedules an accepted preparing message exactly once across concurrent retries", () =>
2180 - Effect.gen(function* () {
2181 - const setupEntered = yield* Deferred.make<void>();
2182 - const allowSetup = yield* Deferred.make<void>();
2183 - const harness = makeHarness({
2184 - runSetup: () =>
2185 - Deferred.succeed(setupEntered, undefined).pipe(
2186 - Effect.andThen(Deferred.await(allowSetup)),
2187 - Effect.as({ status: "no-script" as const }),
2188 - ),
2189 - });
2190 - yield* Effect.gen(function* () {
2191 - const launches = yield* ThreadLaunch.ThreadLaunchService;
2192 - const threads = yield* ThreadManagement.ThreadManagementService;
2193 - const input = launchInput({
2194 - command: "command:launch:accepted-before-fork",
2195 - thread: "thread:launch:accepted-before-fork",
2196 - message: "Resume preparation",
2197 - workspace: { type: "worktree", baseRef: "main" },
2179 +it.effect(
2180 + "schedules an accepted preparing message exactly once, from its accepted worktree base",
2181 + () =>
2182 + Effect.gen(function* () {
2183 + const setupEntered = yield* Deferred.make<void>();
2184 + const allowSetup = yield* Deferred.make<void>();
2185 + const fetched: Array<string> = [];
2186 + const harness = makeHarness({
2187 + fetchWorktreeBaseCommit: ({ refName }) =>
2188 + Effect.sync(() => {
2189 + fetched.push(refName);
2190 + return null;
2191 + }),
2192 + runSetup: () =>
2193 + Deferred.succeed(setupEntered, undefined).pipe(
2194 + Effect.andThen(Deferred.await(allowSetup)),
2195 + Effect.as({ status: "no-script" as const }),
2196 + ),
2197 });
2199 - const messageId = MessageId.make("message:launch:accepted-before-fork");
2198 + yield* Effect.gen(function* () {
2199 + const launches = yield* ThreadLaunch.ThreadLaunchService;
2200 + const threads = yield* ThreadManagement.ThreadManagementService;
2201 + const input = launchInput({
2202 + command: "command:launch:accepted-before-fork",
2203 + thread: "thread:launch:accepted-before-fork",
2204 + message: "Resume preparation",
2205 + workspace: { type: "worktree", baseRef: "main" },
2206 + });
2207 + const messageId = MessageId.make("message:launch:accepted-before-fork");
2208
2201 - yield* threads.dispatch({
2202 - type: "thread.create",
2203 - commandId: input.commandId,
2204 - threadId: input.threadId,
2205 - projectId: input.projectId,
2206 - title: input.title,
2207 - modelSelection: input.modelSelection,
2208 - runtimeMode: input.runtimeMode,
2209 - interactionMode: input.interactionMode,
2210 - branch: null,
2211 - worktreePath: null,
2212 - createdBy: input.createdBy,
2213 - creationSource: input.creationSource,
2214 - });
2215 - yield* threads.dispatch({
2216 - type: "message.dispatch",
2217 - commandId: CommandId.make(`${input.commandId}:initial-message`),
2218 - threadId: input.threadId,
2219 - messageId,
2220 - text: "Resume preparation",
2221 - attachments: [],
2222 - modelSelection: input.modelSelection,
2223 - dispatchMode: { type: "defer_start" },
2224 - createdBy: input.createdBy,
2225 - creationSource: input.creationSource,
2226 - });
2227 - const preparing = yield* threads.getThreadProjection(input.threadId);
2228 - assert.equal(preparing.runs[0]?.status, "preparing");
2209 + yield* threads.dispatch({
2210 + type: "thread.create",
2211 + commandId: input.commandId,
2212 + threadId: input.threadId,
2213 + projectId: input.projectId,
2214 + title: input.title,
2215 + modelSelection: input.modelSelection,
2216 + runtimeMode: input.runtimeMode,
2217 + interactionMode: input.interactionMode,
2218 + branch: null,
2219 + worktreePath: null,
2220 + createdBy: input.createdBy,
2221 + creationSource: input.creationSource,
2222 + });
2223 + yield* threads.dispatch({
2224 + type: "message.dispatch",
2225 + commandId: CommandId.make(`${input.commandId}:initial-message`),
2226 + threadId: input.threadId,
2227 + messageId,
2228 + text: "Resume preparation",
2229 + attachments: [],
2230 + modelSelection: input.modelSelection,
2231 + dispatchMode: {
2232 + type: "defer_start",
2233 + workspaceStrategy: { type: "worktree", baseRef: "main", startFromOrigin: true },
2234 + },
2235 + createdBy: input.createdBy,
2236 + creationSource: input.creationSource,
2237 + });
2238 + const preparing = yield* threads.getThreadProjection(input.threadId);
2239 + assert.equal(preparing.runs[0]?.status, "preparing");
2240
2230 - const [first, second] = yield* Effect.all([launches.launch(input), launches.launch(input)], {
2231 - concurrency: "unbounded",
2232 - });
2233 - yield* Deferred.await(setupEntered);
2234 - assert.isTrue(first.resumed);
2235 - assert.isTrue(second.resumed);
2236 - assert.equal(harness.runSetup.mock.calls.length, 1);
2237 - yield* Deferred.succeed(allowSetup, undefined);
2238 - }).pipe(Effect.provide(harness.layer));
2239 - }),
2241 + const [first, second] = yield* Effect.all(
2242 + [launches.launch(input), launches.launch(input)],
2243 + {
2244 + concurrency: "unbounded",
2245 + },
2246 + );
2247 + yield* Deferred.await(setupEntered);
2248 + assert.isTrue(first.resumed);
2249 + assert.isTrue(second.resumed);
2250 + assert.equal(harness.runSetup.mock.calls.length, 1);
2251 + assert.deepEqual(fetched, ["main"]);
2252 + yield* Deferred.succeed(allowSetup, undefined);
2253 + }).pipe(Effect.provide(harness.layer));
2254 + }),
2255 );
2256
2257 it.effect("creates a strong provider-thread mapping for an imported native session", () => {
apps/server/src/orchestration-v2/ThreadLaunchService.ts
+14 -9
@@ -1304,16 +1304,21 @@ const make = Effect.gen(function* () {
1304 return yield* releasePreparation(input.commandId);
1305 }
1306 const boundWorktreePath = current?.thread.worktreePath ?? null;
1307 + // A retry prepares the worktree the accepted launch asked for, not the
1308 + // retry's own strategy, whose defaults may resolve differently.
1309 + const accepted = current?.runs.find((run) => run.id === runId)?.workspacePreparation;
1310 const preparationStrategy: ThreadLaunchWorkspaceStrategy =
1308 - Option.isSome(launchReceipt) &&
1309 - workspaceStrategy.type === "root" &&
1310 - boundWorktreePath !== null
1311 - ? {
1312 - type: "existing_worktree",
1313 - worktreePath: boundWorktreePath,
1314 - branch: workspaceStrategy.branch,
1315 - }
1316 - : workspaceStrategy;
1311 + Option.isSome(launchReceipt) && accepted?.type === "worktree"
1312 + ? accepted
1313 + : Option.isSome(launchReceipt) &&
1314 + workspaceStrategy.type === "root" &&
1315 + boundWorktreePath !== null
1316 + ? {
1317 + type: "existing_worktree",
1318 + worktreePath: boundWorktreePath,
1319 + branch: workspaceStrategy.branch,
1320 + }
1321 + : workspaceStrategy;
1322 yield* schedulePreparation(
1323 { ...input, workspaceStrategy: preparationStrategy },
1324 threadId,
apps/server/src/vcs/GitVcsDriverCore.test.ts
+6 -1
@@ -5538,7 +5538,12 @@ it.layer(layerTest)("GitVcsDriver core integration", (it) => {
5538 const defaultBranch = yield* driver.resolveDefaultBranchName(cwd, "origin");
5539
5540 assert.notEqual(defaultBranch, "development");
5541 - assert.isNull(yield* driver.resolveWorktreeBaseRemote({ cwd, refName: "origin/HEAD" }));
5541 + for (const refName of ["origin/HEAD", "HEAD~1", "main^", "@{-1}", "-", ":/fix"])
5542 + assert.isNull(yield* driver.resolveWorktreeBaseRemote({ cwd, refName }));
5543 + assert.deepEqual(
5544 + yield* driver.resolveWorktreeBaseRemote({ cwd, refName: "feature/a\u00a0b" }),
5545 + { remoteName: "origin", branchName: "feature/a\u00a0b" },
5546 + );
5547 }),
5548 );
5549
apps/server/src/vcs/GitVcsDriverCore.ts
+6 -2
@@ -5547,8 +5547,12 @@ export const makeGitVcsDriverCore = Effect.fn("makeGitVcsDriverCore")(function*
5547 remoteName: "origin",
5548 branchName: input.refName,
5549 };
5550 - // `<remote>/HEAD` is the remote's default-branch pointer, not a branch it can serve.
5551 - return remoteNames.includes(remoteName) && branchName !== "HEAD"
5550 + // `<remote>/HEAD` is the remote's default-branch pointer, not a branch it can serve, and
5551 + // a revision expression such as `HEAD~1`, `@{-1}` or `:/text` is no branch name at all.
5552 + return remoteNames.includes(remoteName) &&
5553 + branchName !== "HEAD" &&
5554 + !branchName.startsWith("-") &&
5555 + !/[\x00-\x20\x7f~^:?*[\\]|\.\.|@\{/.test(branchName)
5556 ? { remoteName, branchName }
5557 : null;
5558 });
docs/orchestration-v2/orchestrator-mcp-server.md
+1 -1
@@ -390,7 +390,7 @@ this binding.
390
391 Pass the task in `message`. Project, model, and modes inherit when omitted;
392 workspace does not. `scratch: true` launches without a project, in a folder of
393 -its own under the environment's Scratch project. For stacked PRs, use the parent branch as `baseRef` with
393 +its own under the environment's Scratch project. A new worktree with `startFromOrigin` omitted follows the new-worktree setting. For stacked PRs, use the parent branch as `baseRef` with
394 `startFromOrigin: false`. The new thread may not run with broader runtime or
395 interaction modes than the caller. As for `delegate_task`, a runtime mode the
396 provider does not offer is refused, and an inherited or default one steps down