Skip to content

Commit 1446d5b

Browse files
n0mahdclaude
andcommitted
refactor(server): link library skills into a new worktree from the git manager
The Git driver imported the skills domain and linked a project's library skills into every new worktree it made. The worktree flows (the Git action, thread launch, turn start, the MCP tool and a pull request thread) all reach the driver through GitManager, which already owns worktree policy (the folder, submodules, the setup script), so the link step lives there now and the driver is git only again. The driver files are back to what they are upstream. Co-Authored-By: Claude Sonnet 5.5 <[email protected]>
1 parent 2a8a5ae commit 1446d5b

4 files changed

Lines changed: 173 additions & 122 deletions

File tree

‎apps/server/src/git/GitManager.test.ts‎

Lines changed: 154 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,8 @@ import * as Stream from "effect/Stream";
2323
import { TestClock } from "effect/testing";
2424
import { ChildProcessSpawner } from "effect/process";
2525
import { expect } from "vite-plus/test";
26+
import { HostProcessEnvironment } from "@t3tools/shared/hostProcess";
27+
import { symlinksSupported } from "@t3tools/shared/testing/symlinks";
2628
import type {
2729
ChangeRequest,
2830
GitActionProgressEvent,
@@ -285,6 +287,36 @@ function createBareRemote(): Effect.Effect<
285287
});
286288
}
287289

290+
/**
291+
* A repository whose project uses `db-migrations` from the skill library under `home` and `solo`
292+
* from a folder of its own, linked into `.agents/skills` and, for the library skill, also into
293+
* `.claude/skills`. It is committed, so a worktree has the same files but none of the links.
294+
*/
295+
function repoWithLibrarySkill() {
296+
return Effect.gen(function* () {
297+
const fileSystem = yield* FileSystem.FileSystem;
298+
const home = yield* makeTempDir("t3code-skill-home-");
299+
const cwd = yield* makeTempDir("t3code-git-manager-");
300+
yield* initRepo(cwd);
301+
const entry = NodePath.join(home, ".agents/skill-library/db-migrations");
302+
yield* fileSystem.makeDirectory(entry, { recursive: true });
303+
yield* fileSystem.writeFileString(
304+
NodePath.join(entry, "SKILL.md"),
305+
"---\nname: db-migrations\n---\n",
306+
);
307+
yield* fileSystem.makeDirectory(NodePath.join(cwd, "elsewhere/solo"), { recursive: true });
308+
for (const folder of [".agents/skills", ".claude/skills"]) {
309+
yield* fileSystem.makeDirectory(NodePath.join(cwd, folder), { recursive: true });
310+
yield* fileSystem.symlink(entry, NodePath.join(cwd, folder, "db-migrations"));
311+
}
312+
yield* fileSystem.symlink(
313+
NodePath.join(cwd, "elsewhere/solo"),
314+
NodePath.join(cwd, ".agents/skills/solo"),
315+
);
316+
return { home, cwd, entry };
317+
});
318+
}
319+
288320
function configureRemote(
289321
cwd: string,
290322
remoteName: string,
@@ -4868,6 +4900,128 @@ it.layer(layerGitManagerTest)("GitManager", (it) => {
48684900
}),
48694901
);
48704902

4903+
it.effect.skipIf(!symlinksSupported)(
4904+
"links a project's library skills into a new worktree, and leaves its other links behind",
4905+
() =>
4906+
Effect.gen(function* () {
4907+
const fileSystem = yield* FileSystem.FileSystem;
4908+
const { home, cwd, entry } = yield* repoWithLibrarySkill();
4909+
const { manager } = yield* makeManager();
4910+
const worktree = NodePath.join(yield* makeTempDir("t3code-git-worktrees-"), "feature");
4911+
4912+
const created = yield* manager
4913+
.createWorktree({
4914+
cwd,
4915+
path: worktree,
4916+
refName: "main",
4917+
newRefName: "feature/library-links",
4918+
})
4919+
.pipe(Effect.provideService(HostProcessEnvironment, { HOME: home }));
4920+
4921+
expect(created.worktree.path).toBe(worktree);
4922+
for (const folder of [".agents/skills", ".claude/skills"]) {
4923+
expect(yield* fileSystem.readLink(NodePath.join(worktree, folder, "db-migrations"))).toBe(
4924+
entry,
4925+
);
4926+
}
4927+
// A link to something other than the library is the project's own business.
4928+
expect(yield* fileSystem.exists(NodePath.join(worktree, ".agents/skills/solo"))).toBe(
4929+
false,
4930+
);
4931+
}),
4932+
);
4933+
4934+
it.effect.skipIf(!symlinksSupported)(
4935+
"links a project's library skills into the worktree of a pull request thread",
4936+
() =>
4937+
Effect.gen(function* () {
4938+
const fileSystem = yield* FileSystem.FileSystem;
4939+
const { home, cwd, entry } = yield* repoWithLibrarySkill();
4940+
const remoteDir = yield* createBareRemote();
4941+
yield* runGit(cwd, ["remote", "add", "origin", remoteDir]);
4942+
yield* runGit(cwd, ["push", "-u", "origin", "main"]);
4943+
yield* runGit(cwd, ["checkout", "-b", "feature/pr-links"]);
4944+
yield* fileSystem.writeFileString(NodePath.join(cwd, "pr.txt"), "pr\n");
4945+
yield* runGit(cwd, ["add", "pr.txt"]);
4946+
yield* runGit(cwd, ["commit", "-m", "PR branch"]);
4947+
yield* runGit(cwd, ["push", "-u", "origin", "feature/pr-links"]);
4948+
yield* runGit(cwd, ["push", "origin", "HEAD:refs/pull/78/head"]);
4949+
yield* runGit(cwd, ["checkout", "main"]);
4950+
const { manager } = yield* makeManager({
4951+
ghScenario: {
4952+
pullRequest: {
4953+
number: 78,
4954+
title: "Library links PR",
4955+
url: "https://github.com/pingdotgg/codething-mvp/pull/78",
4956+
baseRefName: "main",
4957+
headRefName: "feature/pr-links",
4958+
state: "open",
4959+
},
4960+
},
4961+
});
4962+
4963+
const result = yield* preparePullRequestThread(manager, {
4964+
cwd,
4965+
reference: "78",
4966+
mode: "worktree",
4967+
}).pipe(Effect.provideService(HostProcessEnvironment, { HOME: home }));
4968+
4969+
expect(result.worktreePath).not.toBeNull();
4970+
expect(
4971+
yield* fileSystem.readLink(
4972+
NodePath.join(result.worktreePath as string, ".agents/skills/db-migrations"),
4973+
),
4974+
).toBe(entry);
4975+
}),
4976+
);
4977+
4978+
it.effect.skipIf(!symlinksSupported)(
4979+
"makes the worktree all the same when the library links can't be made",
4980+
() =>
4981+
Effect.gen(function* () {
4982+
const fileSystem = yield* FileSystem.FileSystem;
4983+
const { home, cwd } = yield* repoWithLibrarySkill();
4984+
// On this branch `.agents` is a file, so no folder can be made under it.
4985+
yield* runGit(cwd, ["checkout", "-q", "-b", "agents-file"]);
4986+
yield* fileSystem.remove(NodePath.join(cwd, ".agents"), { recursive: true });
4987+
yield* fileSystem.writeFileString(NodePath.join(cwd, ".agents"), "not a folder\n");
4988+
yield* runGit(cwd, ["add", "-A"]);
4989+
yield* runGit(cwd, ["commit", "-q", "-m", "agents is a file"]);
4990+
yield* runGit(cwd, ["checkout", "-q", "main"]);
4991+
yield* fileSystem.makeDirectory(NodePath.join(cwd, ".agents/skills"), { recursive: true });
4992+
yield* fileSystem.symlink(
4993+
NodePath.join(home, ".agents/skill-library/db-migrations"),
4994+
NodePath.join(cwd, ".agents/skills/db-migrations"),
4995+
);
4996+
const { manager } = yield* makeManager();
4997+
const worktree = NodePath.join(yield* makeTempDir("t3code-git-worktrees-"), "feature");
4998+
const warnings: string[] = [];
4999+
const logger = Logger.make<unknown, void>(({ message }) => {
5000+
warnings.push(String(message));
5001+
});
5002+
5003+
const created = yield* manager
5004+
.createWorktree({
5005+
cwd,
5006+
path: worktree,
5007+
refName: "agents-file",
5008+
newRefName: "feature/no-links",
5009+
})
5010+
.pipe(
5011+
Effect.provideService(HostProcessEnvironment, { HOME: home }),
5012+
Effect.provideService(Logger.CurrentLoggers, new Set([logger])),
5013+
);
5014+
5015+
expect(created.worktree.path).toBe(worktree);
5016+
expect(
5017+
warnings.filter((line) => line.includes("could not link library skills")).length,
5018+
).toBe(1);
5019+
expect(yield* fileSystem.readFileString(NodePath.join(worktree, ".agents"))).toBe(
5020+
"not a folder\n",
5021+
);
5022+
}),
5023+
);
5024+
48715025
it.effect("prepares pull request threads in worktree mode on the PR head branch", () =>
48725026
Effect.gen(function* () {
48735027
const repoDir = yield* makeTempDir("t3code-git-manager-");

‎apps/server/src/git/GitManager.ts‎

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,7 @@ import type { GitManagerServiceError } from "@t3tools/contracts";
8383
import * as GitVcsDriver from "../vcs/GitVcsDriver.ts";
8484
import * as SourceControlProviderRegistry from "../sourceControl/SourceControlProviderRegistry.ts";
8585
import { detectPrTemplate } from "../sourceControl/PrTemplateDetection.ts";
86+
import { restoreLibraryLinks } from "../skills/SkillLibrary.ts";
8687
import type { ChangeRequest } from "@t3tools/contracts";
8788

8889
export interface GitActionProgressReporter {
@@ -760,6 +761,15 @@ export const make = Effect.gen(function* () {
760761
Effect.map((settings) => settings.worktreesDirectory),
761762
Effect.orElseSucceed(() => ""),
762763
);
764+
/**
765+
* A project's links to its library skills sit outside git, so a new checkout has none until
766+
* they are made. A failure is logged and goes no further: the checkout is made either way.
767+
*/
768+
const linkLibrarySkills = (project: string, worktree: string) =>
769+
restoreLibraryLinks({ project, worktree }).pipe(
770+
Effect.provideService(FileSystem.FileSystem, fileSystem),
771+
Effect.provideService(Path.Path, path),
772+
);
763773
const createWorktree: GitManager["Service"]["createWorktree"] = Effect.fn(
764774
"GitManager.createWorktree",
765775
)(function* (input, options) {
@@ -771,7 +781,13 @@ export const make = Effect.gen(function* () {
771781
Effect.orElseSucceed(() => null),
772782
);
773783
const worktreesDirectory = yield* readWorktreesDirectory;
774-
return yield* gitCore.createWorktree(input, { worktreesDirectory, ...options, submodules });
784+
const created = yield* gitCore.createWorktree(input, {
785+
worktreesDirectory,
786+
...options,
787+
submodules,
788+
});
789+
yield* linkLibrarySkills(input.cwd, created.worktree.path);
790+
return created;
775791
});
776792

777793
const readRepositoryInstructions = (cwd: string, fileName: string) =>
@@ -2667,6 +2683,7 @@ export const make = Effect.gen(function* () {
26672683
),
26682684
},
26692685
);
2686+
yield* linkLibrarySkills(input.cwd, worktree.worktree.path);
26702687
yield* ensureExistingWorktreeUpstream(worktree.worktree.path);
26712688
yield* maybeRunSetupScript(worktree.worktree.path);
26722689

‎apps/server/src/vcs/GitVcsDriverCore.test.ts‎

Lines changed: 1 addition & 113 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,7 @@
11
// @effect-diagnostics nodeBuiltinImport:off - realpathSync.native resolves Windows 8.3 short names, which the Effect realPath does not.
22
import * as NodeFS from "node:fs";
33
import * as NodeServices from "@effect/platform-node/NodeServices";
4-
import { HostProcessEnvironment, HostProcessPlatform } from "@t3tools/shared/hostProcess";
5-
import { symlinksSupported } from "@t3tools/shared/testing/symlinks";
4+
import { HostProcessPlatform } from "@t3tools/shared/hostProcess";
65
import { assert, it, describe } from "@effect/vitest";
76
import * as Deferred from "effect/Deferred";
87
import * as Effect from "effect/Effect";
@@ -3044,117 +3043,6 @@ it.layer(layerTest)("GitVcsDriver core integration", (it) => {
30443043
}),
30453044
);
30463045

3047-
describe("a project's library skills", () => {
3048-
/** A project that uses `db-migrations` from the library and `solo` from a folder of its own. */
3049-
const projectWithLibrarySkill = Effect.gen(function* () {
3050-
const fileSystem = yield* FileSystem.FileSystem;
3051-
const pathService = yield* Path.Path;
3052-
const home = yield* makeTmpDir("skill-home-");
3053-
const cwd = yield* makeTmpDir();
3054-
const { initialBranch } = yield* initRepoWithCommit(cwd);
3055-
const entry = pathService.join(home, ".agents/skill-library/db-migrations");
3056-
yield* writeTextFile(entry, "SKILL.md", "---\nname: db-migrations\n---\n");
3057-
yield* writeTextFile(cwd, "elsewhere/solo/SKILL.md", "---\nname: solo\n---\n");
3058-
for (const folder of [".agents/skills", ".claude/skills"]) {
3059-
yield* fileSystem.makeDirectory(pathService.join(cwd, folder), { recursive: true });
3060-
yield* fileSystem.symlink(entry, pathService.join(cwd, folder, "db-migrations"));
3061-
}
3062-
yield* fileSystem.symlink(
3063-
pathService.join(cwd, "elsewhere/solo"),
3064-
pathService.join(cwd, ".agents/skills/solo"),
3065-
);
3066-
return { home, cwd, entry, initialBranch };
3067-
});
3068-
3069-
it.effect.skipIf(!symlinksSupported)(
3070-
"links them into a new worktree, and leaves a project's other links behind",
3071-
() =>
3072-
Effect.gen(function* () {
3073-
const fileSystem = yield* FileSystem.FileSystem;
3074-
const pathService = yield* Path.Path;
3075-
const { home, cwd, entry, initialBranch } = yield* projectWithLibrarySkill;
3076-
const driver = yield* GitVcsDriver.GitVcsDriver;
3077-
3078-
const created = yield* driver
3079-
.createWorktree({
3080-
cwd,
3081-
path: pathService.join(yield* makeTmpDir("git-worktrees-"), "feature"),
3082-
refName: initialBranch,
3083-
newRefName: "feature/library-links",
3084-
})
3085-
.pipe(Effect.provideService(HostProcessEnvironment, { HOME: home }));
3086-
3087-
for (const folder of [".agents/skills", ".claude/skills"]) {
3088-
assert.equal(
3089-
yield* fileSystem.readLink(
3090-
pathService.join(created.worktree.path, folder, "db-migrations"),
3091-
),
3092-
entry,
3093-
);
3094-
}
3095-
// A link to something other than the library is the project's own business.
3096-
assert.equal(
3097-
yield* fileSystem.exists(
3098-
pathService.join(created.worktree.path, ".agents/skills/solo"),
3099-
),
3100-
false,
3101-
);
3102-
}),
3103-
);
3104-
3105-
it.effect.skipIf(!symlinksSupported)(
3106-
"makes the worktree all the same when the links can't be made",
3107-
() =>
3108-
Effect.gen(function* () {
3109-
const fileSystem = yield* FileSystem.FileSystem;
3110-
const pathService = yield* Path.Path;
3111-
const { home, cwd, initialBranch } = yield* projectWithLibrarySkill;
3112-
// On this branch `.agents` is a file, so no folder can be made under it.
3113-
yield* git(cwd, ["checkout", "-q", "-b", "agents-file"]);
3114-
yield* fileSystem.remove(pathService.join(cwd, ".agents"), { recursive: true });
3115-
yield* writeTextFile(cwd, ".agents", "not a folder\n");
3116-
yield* git(cwd, ["add", "-A"]);
3117-
yield* git(cwd, ["commit", "-q", "-m", "agents is a file"]);
3118-
yield* git(cwd, ["checkout", "-q", initialBranch]);
3119-
yield* fileSystem.makeDirectory(pathService.join(cwd, ".agents/skills"), {
3120-
recursive: true,
3121-
});
3122-
yield* fileSystem.symlink(
3123-
pathService.join(home, ".agents/skill-library/db-migrations"),
3124-
pathService.join(cwd, ".agents/skills/db-migrations"),
3125-
);
3126-
const driver = yield* GitVcsDriver.GitVcsDriver;
3127-
const worktree = pathService.join(yield* makeTmpDir("git-worktrees-"), "feature");
3128-
const warnings: string[] = [];
3129-
const logger = Logger.make<unknown, void>(({ message }) => {
3130-
warnings.push(String(message));
3131-
});
3132-
3133-
const created = yield* driver
3134-
.createWorktree({
3135-
cwd,
3136-
path: worktree,
3137-
refName: "agents-file",
3138-
newRefName: "feature/no-links",
3139-
})
3140-
.pipe(
3141-
Effect.provideService(HostProcessEnvironment, { HOME: home }),
3142-
Effect.provideService(Logger.CurrentLoggers, new Set([logger])),
3143-
);
3144-
3145-
assert.equal(created.worktree.path, worktree);
3146-
assert.equal(
3147-
warnings.filter((line) => line.includes("could not link library skills")).length,
3148-
1,
3149-
);
3150-
assert.equal(
3151-
yield* fileSystem.readFileString(pathService.join(worktree, ".agents")),
3152-
"not a folder\n",
3153-
);
3154-
}),
3155-
);
3156-
});
3157-
31583046
it.effect("resolves the submodule mode from the option, then t3.json", () =>
31593047
Effect.gen(function* () {
31603048
const fileSystem = yield* FileSystem.FileSystem;

‎apps/server/src/vcs/GitVcsDriverCore.ts‎

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,6 @@ import { parseT3ProjectFile } from "@t3tools/shared/t3ProjectFile";
3636
import { resolveProjectFileBackedSetting } from "@t3tools/shared/projectSettings";
3737
import { gitCommandDuration, gitCommandsTotal, withMetrics } from "../observability/Metrics.ts";
3838
import * as GitVcsDriver from "./GitVcsDriver.ts";
39-
import { restoreLibraryLinks } from "../skills/SkillLibrary.ts";
4039
import { resolveWorktreesDirectory } from "../worktreesDirectory.ts";
4140
import {
4241
parseRemoteNames,
@@ -3521,13 +3520,6 @@ export const makeGitVcsDriverCore = Effect.fn("makeGitVcsDriverCore")(function*
35213520
);
35223521
}
35233522

3524-
// A project's links to its library skills sit outside git, so the new checkout has none until
3525-
// they are made. This logs a failure and goes on: the worktree is made either way.
3526-
yield* restoreLibraryLinks({ project: input.cwd, worktree: worktreePath }).pipe(
3527-
Effect.provideService(FileSystem.FileSystem, fileSystem),
3528-
Effect.provideService(Path.Path, path),
3529-
);
3530-
35313523
if (input.newRefName && input.baseRefName) {
35323524
const remoteNames = yield* listRemoteNames(input.cwd).pipe(Effect.orElseSucceed(() => []));
35333525
const parsedBaseRef = parseRemoteRefWithRemoteNames(

0 commit comments

Comments
 (0)