Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
113 changes: 113 additions & 0 deletions apps/server/src/orchestration-v2/ThreadLaunchService.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@ import {
ScheduledTaskId,
type ServerProvider,
ThreadId,
TextGenerationError,
ServerSettingsError,
} from "@t3tools/contracts";
import * as Cause from "effect/Cause";
import * as Deferred from "effect/Deferred";
Expand Down Expand Up @@ -126,6 +128,7 @@ interface HarnessOptions {
readonly generateTitle?: TextGeneration.TextGeneration["Service"]["generateThreadTitle"];
readonly generateBranchName?: TextGeneration.TextGeneration["Service"]["generateBranchName"];
readonly serverSettings?: Parameters<typeof ServerSettings.layerTest>[0];
readonly getLaunchSettings?: ServerSettings.ServerSettingsService["Service"]["getSettings"];
readonly providers?: ReadonlyArray<ServerProvider>;
readonly git?: Partial<GitWorkflow.GitWorkflowService["Service"]>;
}
Expand Down Expand Up @@ -228,6 +231,11 @@ function makeHarness(options: HarnessOptions = {}) {
layerThreadManagement,
layerReceipts,
IdAllocator.layer,
options.getLaunchSettings === undefined
? Layer.empty
: Layer.mock(ServerSettings.ServerSettingsService)({
getSettings: options.getLaunchSettings,
}),
),
),
);
Expand Down Expand Up @@ -914,6 +922,111 @@ const preparationSettled = (threadId: ThreadId) =>
return yield* threads.getThreadProjection(threadId);
});

it.effect.each([
{ prefix: "my-prefix", collision: null, expected: /^my-prefix\/[0-9a-f]{8}$/ },
{ prefix: "my-prefix", collision: "my-prefix", expected: /^my-prefix-[0-9a-f]{8}$/ },
{ prefix: "team/topic", collision: "team", expected: /^team-topic-[0-9a-f]{8}$/ },
{ prefix: "team/topic", collision: "team/topic", expected: /^team-topic-[0-9a-f]{8}$/ },
{ prefix: "", collision: null, expected: /^[0-9a-f]{8}$/ },
])("keeps prefix '$prefix' with collision '$collision' when naming fails", (scenario) =>
Effect.gen(function* () {
const namingAttempted = yield* Deferred.make<void>();
const harness = makeHarness({
serverSettings: {
branchNamePrefix: "global-prefix",
projectSettingsOverrides: {
[projectId]: { branchNamingMode: "static", branchNamePrefix: scenario.prefix },
},
},
hasCommit: ({ refName }) => Effect.succeed(refName === `refs/heads/${scenario.collision}`),
generateBranchName: () =>
Deferred.succeed(namingAttempted, undefined).pipe(
Effect.andThen(
Effect.fail(
new TextGenerationError({ operation: "generateBranchName", detail: "Unavailable" }),
),
),
),
});
yield* Effect.gen(function* () {
const launches = yield* ThreadLaunch.ThreadLaunchService;
const launched = yield* launches.launch(
launchInput({
command: "prefix-launch",
thread: "prefix-thread",
message: "Fix the parser",
workspace: { type: "worktree", baseRef: "main" },
}),
);
const projection = yield* preparationSettled(launched.threadId);
assert.match(projection.thread.branch!, scenario.expected);
yield* Deferred.await(namingAttempted);
assert.equal(projection.runs[0]?.status, "starting");
assert.equal(harness.createWorktree.mock.calls[0]?.[0]?.newRefName, projection.thread.branch);
assert.equal(harness.renameBranch.mock.calls.length, 0);
assert.isFalse(projection.turnItems.some((item) => item.type === "error"));
}).pipe(Effect.provide(harness.layer));
}),
);

it.effect(
"starts a worktree with the default temporary name when settings secrets cannot be read",
() => {
const harness = makeHarness({
getLaunchSettings: Effect.fail(
new ServerSettingsError({ settingsPath: "/settings.json", operation: "read-secret" }),
),
});
return Effect.gen(function* () {
const launches = yield* ThreadLaunch.ThreadLaunchService;
const launched = yield* launches.launch(
launchInput({
command: "settings-fallback",
thread: "settings-fallback-thread",
message: "Fix the parser",
workspace: { type: "worktree", baseRef: "main" },
}),
);
const projection = yield* preparationSettled(launched.threadId);
assert.equal(projection.runs[0]?.status, "starting");
assert.match(projection.thread.branch!, /^t3\/[0-9a-f]{8}$/);
assert.equal(harness.createWorktree.mock.calls[0]?.[0]?.newRefName, projection.thread.branch);
assert.equal(harness.runSetup.mock.calls.length, 1);
assert.equal(harness.renameBranch.mock.calls.length, 0);
assert.isFalse(projection.turnItems.some((item) => item.type === "error"));
}).pipe(Effect.provide(harness.layer));
},
);

it.effect("renames a temporary branch with a configured prefix when generation succeeds", () => {
const harness = makeHarness({
serverSettings: { branchNamingMode: "static", branchNamePrefix: "my-prefix" },
generateBranchName: () => Effect.succeed({ branch: "my-prefix/fix-parser" }),
});
return Effect.gen(function* () {
const launches = yield* ThreadLaunch.ThreadLaunchService;
const threads = yield* ThreadManagement.ThreadManagementService;
const launched = yield* launches.launch(
launchInput({
command: "prefix-rename",
thread: "prefix-rename-thread",
message: "Fix the parser",
workspace: { type: "worktree", baseRef: "main" },
}),
);
yield* threads.streamStoredEventsFrom({ threadId: launched.threadId }).pipe(
Stream.filter((stored) => stored.commandId === "prefix-rename:branch-rename"),
Stream.runHead,
);
const projection = yield* threads.getThreadProjection(launched.threadId);
assert.equal(projection.thread.branch, "my-prefix/fix-parser");
assert.match(
harness.renameBranch.mock.calls[0]?.[0]?.oldBranch ?? "",
/^my-prefix\/[0-9a-f]{8}$/,
);
}).pipe(Effect.provide(harness.layer));
});

function scheduledTaskInput(id: string, targetProjectId: ProjectId) {
return {
id: ScheduledTaskId.make(id),
Expand Down
47 changes: 33 additions & 14 deletions apps/server/src/orchestration-v2/ThreadLaunchService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,8 @@ import * as Stream from "effect/Stream";
import {
buildTemporaryWorktreeBranchName,
flattenTemporaryWorktreeBranchName,
formatGeneratedBranchName,
isTemporaryWorktreeBranch,
WORKTREE_BRANCH_PREFIX,
} from "@t3tools/shared/git";

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

const requestedBranch = input.workspaceStrategy.branch;
const generatedTemporaryBranch =
input.workspaceStrategy.type === "worktree" && requestedBranch === undefined;
let branch: string | null;
if (input.workspaceStrategy.type === "worktree" && requestedBranch === undefined) {
if (generatedTemporaryBranch) {
const uuid = yield* randomUuidV4;
branch = buildTemporaryWorktreeBranchName(() => uuid.replaceAll("-", ""));
const settings = yield* serverSettings.getSettings.pipe(
Effect.map((settings) => resolveProjectSettings(settings, input.projectId).settings),
Effect.orElseSucceed(() => null),
);
if (settings?.branchNamingMode === "static") {
branch = formatGeneratedBranchName(branch.slice(-8), {
mode: "static",
prefix: settings.branchNamePrefix,
instructions: "",
});
}
} else {
branch = requestedBranch ?? null;
}
Expand Down Expand Up @@ -410,17 +423,23 @@ const make = Effect.gen(function* () {
}
}
if (shouldFetch) yield* setupTracker.stageStatus(threadId, "fetch", "done");
if (
branch !== null &&
isTemporaryWorktreeBranch(branch) &&
(yield* git
.hasCommit({
cwd: project.workspaceRoot,
refName: `refs/heads/${WORKTREE_BRANCH_PREFIX}`,
})
.pipe(Effect.mapError(mapError(input, "provision-worktree", threadId))))
) {
branch = flattenTemporaryWorktreeBranchName(branch);
if (branch !== null && (generatedTemporaryBranch || isTemporaryWorktreeBranch(branch))) {
const parts = branch.split("/");
for (let depth = 1; depth < parts.length; depth++) {
if (
yield* git
.hasCommit({
cwd: project.workspaceRoot,
refName: `refs/heads/${parts.slice(0, depth).join("/")}`,
})
.pipe(Effect.mapError(mapError(input, "provision-worktree", threadId)))
) {
branch = generatedTemporaryBranch
? branch.replaceAll("/", "-")
: flattenTemporaryWorktreeBranchName(branch);
break;
}
}
}
yield* setupTracker.stageStatus(threadId, "checkout", "running");
const worktree = yield* git
Expand Down Expand Up @@ -470,7 +489,7 @@ const make = Effect.gen(function* () {
worktreePath !== null &&
branch !== null &&
initialMessage !== undefined &&
isTemporaryWorktreeBranch(branch)
(generatedTemporaryBranch || isTemporaryWorktreeBranch(branch))
) {
const oldBranch = branch;
const worktreeCwd = worktreePath;
Expand Down
Loading