Skip to content

Commit f8437ea

Browse files
n0mahdclaude
andcommitted
fix(server): link a library skill into a worktree at the project's own folder
A worktree is a checkout of the whole repository, so when a project is a folder inside its repository (a monorepo's apps/site) its folder in the worktree is under the worktree's root. The links a new worktree gets, and the ones removed from a project's worktrees when it stops using the skill, were joined onto the worktree's root, so they landed in or were looked for in the wrong folder. Both now go through the project's folder in the repository, as `git rev-parse --show-prefix` gives it. Co-Authored-By: Claude Sonnet 5.5 <[email protected]>
1 parent 1446d5b commit f8437ea

7 files changed

Lines changed: 178 additions & 22 deletions

File tree

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

Lines changed: 36 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -290,14 +290,17 @@ function createBareRemote(): Effect.Effect<
290290
/**
291291
* A repository whose project uses `db-migrations` from the skill library under `home` and `solo`
292292
* 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.
293+
* `.claude/skills`. The project is the repository's `subfolder` when given, else the repository
294+
* itself. The links are untracked, so a worktree has the same files but none of them.
294295
*/
295-
function repoWithLibrarySkill() {
296+
function repoWithLibrarySkill(subfolder = "") {
296297
return Effect.gen(function* () {
297298
const fileSystem = yield* FileSystem.FileSystem;
298299
const home = yield* makeTempDir("t3code-skill-home-");
299-
const cwd = yield* makeTempDir("t3code-git-manager-");
300-
yield* initRepo(cwd);
300+
const root = yield* makeTempDir("t3code-git-manager-");
301+
yield* initRepo(root);
302+
const cwd = NodePath.join(root, subfolder);
303+
yield* fileSystem.makeDirectory(cwd, { recursive: true });
301304
const entry = NodePath.join(home, ".agents/skill-library/db-migrations");
302305
yield* fileSystem.makeDirectory(entry, { recursive: true });
303306
yield* fileSystem.writeFileString(
@@ -313,7 +316,7 @@ function repoWithLibrarySkill() {
313316
NodePath.join(cwd, "elsewhere/solo"),
314317
NodePath.join(cwd, ".agents/skills/solo"),
315318
);
316-
return { home, cwd, entry };
319+
return { home, root, cwd, entry };
317320
});
318321
}
319322

@@ -4931,6 +4934,34 @@ it.layer(layerGitManagerTest)("GitManager", (it) => {
49314934
}),
49324935
);
49334936

4937+
it.effect.skipIf(!symlinksSupported)(
4938+
"links a project's library skills at its own folder of the worktree when it is a folder in its repository",
4939+
() =>
4940+
Effect.gen(function* () {
4941+
const fileSystem = yield* FileSystem.FileSystem;
4942+
const { home, cwd, entry } = yield* repoWithLibrarySkill("apps/site");
4943+
const { manager } = yield* makeManager();
4944+
const worktree = NodePath.join(yield* makeTempDir("t3code-git-worktrees-"), "feature");
4945+
4946+
yield* manager
4947+
.createWorktree({
4948+
cwd,
4949+
path: worktree,
4950+
refName: "main",
4951+
newRefName: "feature/site-links",
4952+
})
4953+
.pipe(Effect.provideService(HostProcessEnvironment, { HOME: home }));
4954+
4955+
expect(
4956+
yield* fileSystem.readLink(
4957+
NodePath.join(worktree, "apps/site/.agents/skills/db-migrations"),
4958+
),
4959+
).toBe(entry);
4960+
// The worktree's root is not the project, so nothing lands there.
4961+
expect(yield* fileSystem.exists(NodePath.join(worktree, ".agents"))).toBe(false);
4962+
}),
4963+
);
4964+
49344965
it.effect.skipIf(!symlinksSupported)(
49354966
"links a project's library skills into the worktree of a pull request thread",
49364967
() =>

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

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -766,10 +766,22 @@ export const make = Effect.gen(function* () {
766766
* they are made. A failure is logged and goes no further: the checkout is made either way.
767767
*/
768768
const linkLibrarySkills = (project: string, worktree: string) =>
769-
restoreLibraryLinks({ project, worktree }).pipe(
770-
Effect.provideService(FileSystem.FileSystem, fileSystem),
771-
Effect.provideService(Path.Path, path),
772-
);
769+
gitCore
770+
.execute({
771+
operation: "GitManager.linkLibrarySkills",
772+
cwd: project,
773+
args: ["rev-parse", "--show-prefix"],
774+
allowNonZeroExit: true,
775+
})
776+
.pipe(
777+
// The project is a folder inside the repository when this isn't empty, and the worktree
778+
// has it at the same path under its own root.
779+
Effect.map((result) => (result.exitCode === 0 ? result.stdout.trim() : "")),
780+
Effect.orElseSucceed(() => ""),
781+
Effect.flatMap((prefix) => restoreLibraryLinks({ project, worktree, prefix })),
782+
Effect.provideService(FileSystem.FileSystem, fileSystem),
783+
Effect.provideService(Path.Path, path),
784+
);
773785
const createWorktree: GitManager["Service"]["createWorktree"] = Effect.fn(
774786
"GitManager.createWorktree",
775787
)(function* (input, options) {

‎apps/server/src/skills/SkillGitExclude.ts‎

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -141,6 +141,29 @@ export const worktreesOf = Effect.fn("SkillGitExclude.worktreesOf")(function* (
141141
.map((line) => line.slice("worktree ".length));
142142
});
143143

144+
/**
145+
* The project's folder relative to its repository's root, as `git rev-parse --show-prefix` says:
146+
* empty when the project is the root, and outside a git repository. A checkout of the repository
147+
* has the project at that path under its own root.
148+
*/
149+
export const projectPrefixOf = Effect.fn("SkillGitExclude.projectPrefixOf")(function* (
150+
projectRoot: string,
151+
) {
152+
const vcs = yield* VcsProcess.VcsProcess;
153+
const result = yield* vcs
154+
.run({
155+
operation: "SkillGitExclude.projectPrefixOf",
156+
command: "git",
157+
args: ["rev-parse", "--show-prefix"],
158+
cwd: projectRoot,
159+
allowNonZeroExit: true,
160+
timeoutMs: 5_000,
161+
maxOutputBytes: 16 * 1024,
162+
})
163+
.pipe(Effect.orElseSucceed(() => undefined));
164+
return result === undefined || result.exitCode !== 0 ? "" : result.stdout.trim();
165+
});
166+
144167
/**
145168
* Keeps a file T3 Code has just created in a project out of git: Claude Code's own
146169
* `.claude/settings.local.json`, which is the user's and not the repository's. Claude Code does

‎apps/server/src/skills/SkillLibrary.test.ts‎

Lines changed: 26 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -265,7 +265,7 @@ it.layer(NodeServices.layer, { excludeTestServices: true })("SkillLibrary", (it)
265265
yield* fs.makeDirectory(path.join(worktree, ".agents/skills/alpha"), { recursive: true });
266266
yield* fs.writeFileString(path.join(worktree, ".agents/skills/alpha/SKILL.md"), "kept");
267267

268-
yield* restoreLibraryLinks({ project: web, worktree }).pipe(
268+
yield* restoreLibraryLinks({ project: web, worktree, prefix: "" }).pipe(
269269
Effect.provideService(HostProcessEnvironment, { HOME: home }),
270270
);
271271

@@ -280,17 +280,38 @@ it.layer(NodeServices.layer, { excludeTestServices: true })("SkillLibrary", (it)
280280
}),
281281
);
282282

283+
it.effect.skipIf(!symlinksSupported)(
284+
"makes the links in the project's own folder of a worktree of the whole repository",
285+
() =>
286+
Effect.gen(function* () {
287+
const { fs, path, home, library, web } = yield* makeMachine;
288+
const worktree = path.join(home, "worktrees/monorepo-feature");
289+
290+
// The project is `apps/web` of its repository, and the worktree is the repository.
291+
yield* restoreLibraryLinks({ project: web, worktree, prefix: "apps/web/" }).pipe(
292+
Effect.provideService(HostProcessEnvironment, { HOME: home }),
293+
);
294+
295+
expect(
296+
yield* fs.readLink(path.join(worktree, "apps/web/.agents/skills/db-migrations")),
297+
).toBe(path.join(library, "db-migrations"));
298+
expect(yield* fs.exists(path.join(worktree, ".agents"))).toBe(false);
299+
}),
300+
);
301+
283302
it.effect("does nothing for a project without links, or one that has gone", () =>
284303
Effect.gen(function* () {
285304
const { fs, path, home, marketing } = yield* makeMachine;
286305
const worktree = path.join(home, "worktrees/marketing-feature");
287306

288-
yield* restoreLibraryLinks({ project: marketing, worktree }).pipe(
289-
Effect.provideService(HostProcessEnvironment, { HOME: home }),
290-
);
291-
yield* restoreLibraryLinks({ project: path.join(home, "repos/gone"), worktree }).pipe(
307+
yield* restoreLibraryLinks({ project: marketing, worktree, prefix: "" }).pipe(
292308
Effect.provideService(HostProcessEnvironment, { HOME: home }),
293309
);
310+
yield* restoreLibraryLinks({
311+
project: path.join(home, "repos/gone"),
312+
worktree,
313+
prefix: "",
314+
}).pipe(Effect.provideService(HostProcessEnvironment, { HOME: home }));
294315

295316
expect(yield* fs.exists(worktree)).toBe(false);
296317
}),

‎apps/server/src/skills/SkillLibrary.ts‎

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -141,9 +141,17 @@ export const libraryLinksOf = Effect.fn("SkillLibrary.libraryLinksOf")(function*
141141
* Links to a project's library skills into a worktree that was just made from it. The links sit
142142
* outside git, so a worktree has none until they are made. Nothing in the way is replaced, and a
143143
* failure is logged and goes no further: a worktree without the links is still a worktree.
144+
*
145+
* A worktree is a checkout of the whole repository, so a project that is a folder inside its
146+
* repository is at `prefix` under the worktree's root.
144147
*/
145148
export const restoreLibraryLinks = Effect.fn("SkillLibrary.restoreLibraryLinks")(
146-
function* (input: { readonly project: string; readonly worktree: string }) {
149+
function* (input: {
150+
readonly project: string;
151+
readonly worktree: string;
152+
/** The project's folder relative to its repository's root, empty when it is the root. */
153+
readonly prefix: string;
154+
}) {
147155
const fileSystem = yield* FileSystem.FileSystem;
148156
const path = yield* Path.Path;
149157
const environment = yield* HostProcessEnvironment;
@@ -161,7 +169,7 @@ export const restoreLibraryLinks = Effect.fn("SkillLibrary.restoreLibraryLinks")
161169
);
162170
if (target === undefined) continue;
163171
if (path.dirname(path.resolve(path.dirname(linkPath), target)) !== library) continue;
164-
const created = path.join(input.worktree, folder, name);
172+
const created = path.join(input.worktree, input.prefix, folder, name);
165173
yield* fileSystem.makeDirectory(path.dirname(created), { recursive: true });
166174
// A bare create: something already there, such as a skill the project commits, stays.
167175
yield* fileSystem.symlink(target, created).pipe(

‎apps/server/src/skills/SkillPlacement.test.ts‎

Lines changed: 63 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1729,14 +1729,19 @@ it.layer(NodeServices.layer, { excludeTestServices: true })("SkillPlacement", (i
17291729
});
17301730

17311731
describe("the git worktrees of a project that uses a library skill", () => {
1732-
/** A worktree of `project` with the links the worktree hook makes in it. */
1733-
const addWorktree = (project: string, name: string) =>
1732+
/**
1733+
* A worktree of the repository `project` is in, with the links the worktree hook makes in it.
1734+
* `prefix` is the project's folder in the repository, as git says it.
1735+
*/
1736+
const addWorktree = (project: string, name: string, prefix = "") =>
17341737
Effect.gen(function* () {
17351738
const fs = yield* FileSystem.FileSystem;
17361739
const path = yield* Path.Path;
1737-
const worktree = path.join(path.dirname(project), `${path.basename(project)}-${name}`);
1740+
const up = prefix.split("/").filter((segment) => segment !== "");
1741+
const root = path.resolve(project, ...up.map(() => ".."));
1742+
const worktree = path.join(path.dirname(root), `${path.basename(root)}-${name}`);
17381743
yield* git(project, ["worktree", "add", "-q", "-b", name, worktree]);
1739-
yield* restoreLibraryLinks({ project, worktree }).pipe(
1744+
yield* restoreLibraryLinks({ project, worktree, prefix }).pipe(
17401745
Effect.provideService(FileSystem.FileSystem, fs),
17411746
Effect.provideService(Path.Path, path),
17421747
);
@@ -1848,6 +1853,60 @@ it.layer(NodeServices.layer, { excludeTestServices: true })("SkillPlacement", (i
18481853
);
18491854
}),
18501855
);
1856+
1857+
it.effect.skipIf(!symlinksSupported)(
1858+
"keeps its links at the same folder of each worktree, and removes them from there",
1859+
() =>
1860+
Effect.gen(function* () {
1861+
const { fs, path, home, api, write, library } = yield* makeMachine;
1862+
// `repos/mono` is the repository; the project is its `apps/site` folder.
1863+
const mono = path.join(home, "repos/mono");
1864+
const site = path.join(mono, "apps/site");
1865+
yield* write("repos/mono/apps/site/README.md", "# site\n");
1866+
yield* git(mono, ["init", "-q", "-b", "main"]);
1867+
yield* git(mono, ["config", "core.excludesFile", path.join(home, "global-ignore")]);
1868+
yield* git(mono, ["add", "-A"]);
1869+
yield* git(mono, ["commit", "-q", "-m", "init"]);
1870+
// Untracked, like the skill of the other projects, so a worktree starts without it.
1871+
yield* write(
1872+
"repos/mono/apps/site/.agents/skills/db-migrations/SKILL.md",
1873+
skillFile("db-migrations"),
1874+
);
1875+
yield* withManager(home, [site, api], ({ manager, catalog }) =>
1876+
Effect.gen(function* () {
1877+
const verify = refOf(
1878+
(yield* catalog.list({ cwd: site })).skills,
1879+
"project",
1880+
"db-migrations",
1881+
);
1882+
yield* manager.place({
1883+
cwd: site,
1884+
skills: [verify],
1885+
to: { kind: "projects", cwds: [site, api] },
1886+
});
1887+
const entry = path.join(library, "db-migrations");
1888+
const worktree = yield* addWorktree(site, "feature", "apps/site/");
1889+
// The link is in the project's folder of the worktree, not at its root.
1890+
expect(
1891+
yield* fs.readLink(path.join(worktree, "apps/site/.agents/skills/db-migrations")),
1892+
).toBe(entry);
1893+
expect(yield* fs.exists(path.join(worktree, ".agents"))).toBe(false);
1894+
1895+
// The project stops using the skill: the worktree's link goes too.
1896+
const skill = refOf((yield* catalog.list({})).skills, "global", "db-migrations");
1897+
const moved = yield* manager.place({
1898+
skills: [skill],
1899+
to: { kind: "projects", cwds: [api] },
1900+
});
1901+
expect(moved.outcomes[0]).toMatchObject({ status: "changed", blocked: [] });
1902+
expect(yield* fs.exists(path.join(site, ".agents/skills/db-migrations"))).toBe(false);
1903+
expect(
1904+
yield* fs.exists(path.join(worktree, "apps/site/.agents/skills/db-migrations")),
1905+
).toBe(false);
1906+
}),
1907+
);
1908+
}),
1909+
);
18511910
});
18521911

18531912
describe("Claude's local settings file", () => {

‎apps/server/src/skills/SkillPlacement.ts‎

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ import {
4848

4949
import * as VcsProcess from "../vcs/VcsProcess.ts";
5050
import type * as SkillCatalog from "./SkillCatalog.ts";
51-
import { updateExclude, worktreesOf } from "./SkillGitExclude.ts";
51+
import { projectPrefixOf, updateExclude, worktreesOf } from "./SkillGitExclude.ts";
5252
import { LIBRARY_FOLDER, libraryLinksOf, linkLeadsTo, type LibraryLink } from "./SkillLibrary.ts";
5353
import { createLink, removeLink, type RemoveLinkResult } from "./SkillLinks.ts";
5454
import { moveRecord, type LockScope, type MoveRecordResult } from "./SkillLockFiles.ts";
@@ -293,13 +293,15 @@ export const makeSkillPlacement = Effect.fnUntraced(function* (deps: PlacementDe
293293
for (const project of new Set(links.map((link) => link.project))) {
294294
const own = yield* realPath(project);
295295
const worktrees = yield* inContext(worktreesOf(project));
296+
// Each worktree holds the whole repository, so the project's folder is under its root.
297+
const prefix = yield* inContext(projectPrefixOf(project));
296298
for (const worktree of worktrees) {
297299
// The checkout the project is in (or is inside) keeps what it has.
298300
const real = yield* realPath(worktree);
299301
if (own === real || own.startsWith(`${real}${path.sep}`)) continue;
300302
const found: Array<{ path: string; target: string }> = [];
301303
for (const link of links.filter((item) => item.project === project)) {
302-
const created = path.join(worktree, link.folder, path.basename(link.path));
304+
const created = path.join(worktree, prefix, link.folder, path.basename(link.path));
303305
const target = yield* fileSystem.readLink(created).pipe(
304306
Effect.map((value): string | undefined => value),
305307
Effect.orElseSucceed(() => undefined),

0 commit comments

Comments
 (0)