Skip to content
Draft
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
14 changes: 8 additions & 6 deletions apps/server/src/git/GitManager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -743,9 +743,12 @@ export const make = Effect.gen(function* () {
}
return result;
});
const readWorktreesDirectory = serverSettingsService.getSettings.pipe(
Effect.map((settings) => settings.worktreesDirectory),
Effect.orElseSucceed(() => ""),
const readWorktreeCreationSettings = serverSettingsService.getSettings.pipe(
Effect.map((settings) => ({
worktreesDirectory: settings.worktreesDirectory,
worktreeTemplates: settings.enableWorktreeTemplates,
})),
Effect.orElseSucceed(() => ({ worktreesDirectory: "", worktreeTemplates: false })),
);
const createWorktree: GitManager["Service"]["createWorktree"] = Effect.fn(
"GitManager.createWorktree",
Expand All @@ -757,9 +760,8 @@ export const make = Effect.gen(function* () {
Effect.map(({ settings }) => settings.worktreeSubmodules),
Effect.orElseSucceed(() => null),
);
const worktreesDirectory = yield* readWorktreesDirectory;
const result = yield* gitCore.createWorktree(input, {
worktreesDirectory,
...(yield* readWorktreeCreationSettings),
...options,
submodules,
});
Expand Down Expand Up @@ -2980,7 +2982,7 @@ export const make = Effect.gen(function* () {
path: null,
},
{
worktreesDirectory: yield* readWorktreesDirectory,
...(yield* readWorktreeCreationSettings),
submodules: yield* projectSettingsFor(input).pipe(
Effect.map(({ settings }) => settings.worktreeSubmodules),
Effect.orElseSucceed(() => null),
Expand Down
2 changes: 2 additions & 0 deletions apps/server/src/git/GitWorkflowService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,7 @@ export class GitWorkflowService extends Context.Service<
readonly removeWorktree: (
input: VcsRemoveWorktreeInput,
) => Effect.Effect<void, GitCommandError>;
readonly captureWorktreeTemplate: (worktreePath: string) => Effect.Effect<void>;
readonly pruneWorktrees: (input: {
readonly cwd: string;
}) => Effect.Effect<void, GitCommandError>;
Expand Down Expand Up @@ -461,6 +462,7 @@ export const make = Effect.gen(function* () {
Effect.andThen(git.removeWorktree(input)),
);
}),
captureWorktreeTemplate: git.captureWorktreeTemplate,
pruneWorktrees: Effect.fn("GitWorkflowService.pruneWorktrees")(function (input) {
return ensureGitCommand("GitWorkflowService.pruneWorktrees", input.cwd).pipe(
Effect.andThen(git.pruneWorktrees(input)),
Expand Down
14 changes: 14 additions & 0 deletions apps/server/src/orchestration-v2/ThreadLaunchService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -230,6 +230,17 @@ const make = Effect.gen(function* () {
cause,
});

// A worktree whose setup succeeded seeds later ones with the same lockfiles, off the launch's path.
const captureWorktreeTemplate = (worktreePath: string) =>
serverSettings.getSettings.pipe(
Effect.flatMap((settings) =>
settings.enableWorktreeTemplates ? git.captureWorktreeTemplate(worktreePath) : Effect.void,
),
Effect.ignore,
Effect.forkIn(preparationScope),
Effect.asVoid,
);

const readReceipt = (input: ThreadLaunchInput, commandId: CommandId) =>
receipts
.getByCommandId(commandId)
Expand Down Expand Up @@ -606,12 +617,15 @@ const make = Effect.gen(function* () {
},
}));
if (setup.completion) {
const createdHere = createdWorktreePath;
const awaitCompletion = Effect.gen(function* () {
const completion = yield* setup.completion!;
yield* setupTracker.stage(threadId, "setup-script", {
status: completion.exitCode === 0 ? "done" : "failed",
detail: `exited with ${completion.exitCode ?? "no exit code"}`,
});
if (completion.exitCode === 0 && createdHere !== null)
yield* captureWorktreeTemplate(createdHere);
if (completion.exitCode !== 0 && !setup.async)
return yield* mapError(
input,
Expand Down
4 changes: 4 additions & 0 deletions apps/server/src/vcs/GitVcsDriver.ts
Original file line number Diff line number Diff line change
Expand Up @@ -144,6 +144,8 @@ export interface CreateWorktreeOptions {
/** Null or omitted defers to the checkout's t3.json. */
readonly submodules?: WorktreeSubmodules | null;
readonly worktreesDirectory?: string;
/** Clone a captured copy-on-write template when one matches; false also removes the templates. */
readonly worktreeTemplates?: boolean;
}

export interface GitCommitProgress {
Expand Down Expand Up @@ -422,6 +424,8 @@ export class GitVcsDriver extends Context.Service<
readonly removeWorktree: (
input: VcsRemoveWorktreeInput,
) => Effect.Effect<void, GitCommandError>;
/** Keeps this set-up worktree as the template for its lockfiles; logs and skips on any failure. */
readonly captureWorktreeTemplate: (worktreePath: string) => Effect.Effect<void>;
readonly pruneWorktrees: (input: {
readonly cwd: string;
}) => Effect.Effect<void, GitCommandError>;
Expand Down
49 changes: 48 additions & 1 deletion apps/server/src/vcs/GitVcsDriverCore.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ import * as GitVcsDriver from "./GitVcsDriver.ts";
import { removeEmptyDirectory } from "./removeEmptyDirectory.ts";
import { explicitBareRepositoryEnv, gitConfigEnv } from "./VcsProcess.ts";
import { removeLinkedWorktree } from "./worktreeRemoval.ts";
import { makeWorktreeTemplates } from "./worktreeTemplates.ts";
import { resolveWorktreesDirectory } from "../worktreesDirectory.ts";
import {
parseRemoteNames,
Expand Down Expand Up @@ -1234,6 +1235,10 @@ export const makeGitVcsDriverCore = Effect.fn("makeGitVcsDriverCore")(function*
),
);

const templates = yield* makeWorktreeTemplates((operation, cwd, args, options) =>
runGitStdoutWithOptions(operation, cwd, args, options),
);

const branchExists = (cwd: string, refName: string): Effect.Effect<boolean, GitCommandError> =>
executeGit(
"GitVcsDriver.branchExists",
Expand Down Expand Up @@ -4157,7 +4162,18 @@ export const makeGitVcsDriverCore = Effect.fn("makeGitVcsDriverCore")(function*
"git worktree add failed",
);
}
const args = ["worktree", "add", "--", worktreePath, input.newRefName ?? input.refName];
if (options?.worktreeTemplates === false) yield* templates.removeAll(worktreePath);
const template = options?.worktreeTemplates
? yield* templates.find({ cwd: input.cwd, refName: input.refName, worktreePath })
: null;
const args = [
"worktree",
"add",
...(template ? ["--no-checkout"] : []),
"--",
worktreePath,
input.newRefName ?? input.refName,
];
const progress = options?.progress;
const onCheckoutProgress = progress?.onCheckoutProgress;

Expand Down Expand Up @@ -4200,6 +4216,26 @@ export const makeGitVcsDriverCore = Effect.fn("makeGitVcsDriverCore")(function*
if (progress?.onWorktreeClaimed) {
yield* progress.onWorktreeClaimed(worktreePath);
}
if (template) {
yield* templates.clone(template, worktreePath, onCheckoutProgress).pipe(
Effect.catch((cause) =>
Effect.logWarning("worktree template clone failed; checking out instead", {
worktreePath,
cause,
}).pipe(
Effect.andThen(templates.clear(worktreePath)),
Effect.andThen(
runGit(
"GitVcsDriver.createWorktree.checkout",
worktreePath,
["-c", `checkout.workers=${checkoutWorkers}`, "reset", "--hard", "-q"],
{ timeoutMs: null },
),
),
),
),
);
}

// worktree add leaves submodules empty, potentially omitting project skills, tools or source.
const hasSubmodules = yield* fileSystem
Expand Down Expand Up @@ -4986,6 +5022,17 @@ export const makeGitVcsDriverCore = Effect.fn("makeGitVcsDriverCore")(function*
Effect.ensuring(invalidateWorktreeCreationCaches(input.cwd)),
);
}),
captureWorktreeTemplate: Effect.fn("GitVcsDriver.captureWorktreeTemplate")(
function* (worktreePath) {
yield* templates
.capture(worktreePath)
.pipe(
Effect.catch((cause) =>
Effect.logWarning("worktree template capture skipped", { worktreePath, cause }),
),
);
},
),
fetchRefIntoBranch: Effect.fnUntraced(function* (input) {
return yield* withListRefsInvalidation(input.cwd, fetchRefIntoBranch(input));
}),
Expand Down
Loading