Skip to content

Commit 3325bfc

Browse files
committed
fix(server): honor worktree prefixes before automatic naming
1 parent e923627 commit 3325bfc

2 files changed

Lines changed: 110 additions & 14 deletions

File tree

‎apps/server/src/orchestration-v2/ThreadLaunchService.test.ts‎

Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ import {
2525
ScheduledTaskId,
2626
type ServerProvider,
2727
ThreadId,
28+
TextGenerationError,
2829
} from "@t3tools/contracts";
2930
import * as Cause from "effect/Cause";
3031
import * as Deferred from "effect/Deferred";
@@ -914,6 +915,82 @@ const preparationSettled = (threadId: ThreadId) =>
914915
return yield* threads.getThreadProjection(threadId);
915916
});
916917

918+
it.effect.each([
919+
{ prefix: "my-prefix", collision: null, expected: /^my-prefix\/[0-9a-f]{8}$/ },
920+
{ prefix: "my-prefix", collision: "my-prefix", expected: /^my-prefix-[0-9a-f]{8}$/ },
921+
{ prefix: "team/topic", collision: "team", expected: /^team-topic-[0-9a-f]{8}$/ },
922+
{ prefix: "team/topic", collision: "team/topic", expected: /^team-topic-[0-9a-f]{8}$/ },
923+
{ prefix: "", collision: null, expected: /^[0-9a-f]{8}$/ },
924+
])("keeps prefix '$prefix' with collision '$collision' when naming fails", (scenario) =>
925+
Effect.gen(function* () {
926+
const namingAttempted = yield* Deferred.make<void>();
927+
const harness = makeHarness({
928+
serverSettings: {
929+
branchNamePrefix: "global-prefix",
930+
projectSettingsOverrides: {
931+
[projectId]: { branchNamingMode: "static", branchNamePrefix: scenario.prefix },
932+
},
933+
},
934+
hasCommit: ({ refName }) => Effect.succeed(refName === `refs/heads/${scenario.collision}`),
935+
generateBranchName: () =>
936+
Deferred.succeed(namingAttempted, undefined).pipe(
937+
Effect.andThen(
938+
Effect.fail(
939+
new TextGenerationError({ operation: "generateBranchName", detail: "Unavailable" }),
940+
),
941+
),
942+
),
943+
});
944+
yield* Effect.gen(function* () {
945+
const launches = yield* ThreadLaunch.ThreadLaunchService;
946+
const launched = yield* launches.launch(
947+
launchInput({
948+
command: "prefix-launch",
949+
thread: "prefix-thread",
950+
message: "Fix the parser",
951+
workspace: { type: "worktree", baseRef: "main" },
952+
}),
953+
);
954+
const projection = yield* preparationSettled(launched.threadId);
955+
assert.match(projection.thread.branch!, scenario.expected);
956+
yield* Deferred.await(namingAttempted);
957+
assert.equal(projection.runs[0]?.status, "starting");
958+
assert.equal(harness.createWorktree.mock.calls[0]?.[0]?.newRefName, projection.thread.branch);
959+
assert.equal(harness.renameBranch.mock.calls.length, 0);
960+
assert.isFalse(projection.turnItems.some((item) => item.type === "error"));
961+
}).pipe(Effect.provide(harness.layer));
962+
}),
963+
);
964+
965+
it.effect("renames a temporary branch with a configured prefix when generation succeeds", () => {
966+
const harness = makeHarness({
967+
serverSettings: { branchNamingMode: "static", branchNamePrefix: "my-prefix" },
968+
generateBranchName: () => Effect.succeed({ branch: "my-prefix/fix-parser" }),
969+
});
970+
return Effect.gen(function* () {
971+
const launches = yield* ThreadLaunch.ThreadLaunchService;
972+
const threads = yield* ThreadManagement.ThreadManagementService;
973+
const launched = yield* launches.launch(
974+
launchInput({
975+
command: "prefix-rename",
976+
thread: "prefix-rename-thread",
977+
message: "Fix the parser",
978+
workspace: { type: "worktree", baseRef: "main" },
979+
}),
980+
);
981+
yield* threads.streamStoredEventsFrom({ threadId: launched.threadId }).pipe(
982+
Stream.filter((stored) => stored.commandId === "prefix-rename:branch-rename"),
983+
Stream.runHead,
984+
);
985+
const projection = yield* threads.getThreadProjection(launched.threadId);
986+
assert.equal(projection.thread.branch, "my-prefix/fix-parser");
987+
assert.match(
988+
harness.renameBranch.mock.calls[0]?.[0]?.oldBranch ?? "",
989+
/^my-prefix\/[0-9a-f]{8}$/,
990+
);
991+
}).pipe(Effect.provide(harness.layer));
992+
});
993+
917994
function scheduledTaskInput(id: string, targetProjectId: ProjectId) {
918995
return {
919996
id: ScheduledTaskId.make(id),

‎apps/server/src/orchestration-v2/ThreadLaunchService.ts‎

Lines changed: 33 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -33,8 +33,8 @@ import * as Stream from "effect/Stream";
3333
import {
3434
buildTemporaryWorktreeBranchName,
3535
flattenTemporaryWorktreeBranchName,
36+
formatGeneratedBranchName,
3637
isTemporaryWorktreeBranch,
37-
WORKTREE_BRANCH_PREFIX,
3838
} from "@t3tools/shared/git";
3939

4040
import * as GitWorkflow from "../git/GitWorkflowService.ts";
@@ -363,10 +363,23 @@ const make = Effect.gen(function* () {
363363
);
364364

365365
const requestedBranch = input.workspaceStrategy.branch;
366+
const generatedTemporaryBranch =
367+
input.workspaceStrategy.type === "worktree" && requestedBranch === undefined;
366368
let branch: string | null;
367-
if (input.workspaceStrategy.type === "worktree" && requestedBranch === undefined) {
369+
if (generatedTemporaryBranch) {
368370
const uuid = yield* randomUuidV4;
369371
branch = buildTemporaryWorktreeBranchName(() => uuid.replaceAll("-", ""));
372+
const settings = resolveProjectSettings(
373+
yield* serverSettings.getSettings,
374+
input.projectId,
375+
).settings;
376+
if (settings.branchNamingMode === "static") {
377+
branch = formatGeneratedBranchName(branch.slice(-8), {
378+
mode: "static",
379+
prefix: settings.branchNamePrefix,
380+
instructions: "",
381+
});
382+
}
370383
} else {
371384
branch = requestedBranch ?? null;
372385
}
@@ -410,17 +423,23 @@ const make = Effect.gen(function* () {
410423
}
411424
}
412425
if (shouldFetch) yield* setupTracker.stageStatus(threadId, "fetch", "done");
413-
if (
414-
branch !== null &&
415-
isTemporaryWorktreeBranch(branch) &&
416-
(yield* git
417-
.hasCommit({
418-
cwd: project.workspaceRoot,
419-
refName: `refs/heads/${WORKTREE_BRANCH_PREFIX}`,
420-
})
421-
.pipe(Effect.mapError(mapError(input, "provision-worktree", threadId))))
422-
) {
423-
branch = flattenTemporaryWorktreeBranchName(branch);
426+
if (branch !== null && (generatedTemporaryBranch || isTemporaryWorktreeBranch(branch))) {
427+
const parts = branch.split("/");
428+
for (let depth = 1; depth < parts.length; depth++) {
429+
if (
430+
yield* git
431+
.hasCommit({
432+
cwd: project.workspaceRoot,
433+
refName: `refs/heads/${parts.slice(0, depth).join("/")}`,
434+
})
435+
.pipe(Effect.mapError(mapError(input, "provision-worktree", threadId)))
436+
) {
437+
branch = generatedTemporaryBranch
438+
? branch.replaceAll("/", "-")
439+
: flattenTemporaryWorktreeBranchName(branch);
440+
break;
441+
}
442+
}
424443
}
425444
yield* setupTracker.stageStatus(threadId, "checkout", "running");
426445
const worktree = yield* git
@@ -470,7 +489,7 @@ const make = Effect.gen(function* () {
470489
worktreePath !== null &&
471490
branch !== null &&
472491
initialMessage !== undefined &&
473-
isTemporaryWorktreeBranch(branch)
492+
(generatedTemporaryBranch || isTemporaryWorktreeBranch(branch))
474493
) {
475494
const oldBranch = branch;
476495
const worktreeCwd = worktreePath;

0 commit comments

Comments
 (0)