Skip to content

Commit 68c24c4

Browse files
fix(server): worktree containers are told apart by bare state and index, and no longer stop startup
Comparing HEAD's files with the folder was wrong in both directions: a worktree named like a tracked folder made a container pass, and a checkout whose tracked files were all deleted was refused. A folder whose `.git` file points at metadata inside it, with a linked worktree inside it, is now a container when the repository is bare, or when it has commits but no index. The bare case is the layout as cloned; it used to be added as a plain folder, and Initialize Git then turned it into the broken repository. A checkout with deleted files keeps its index and a fresh one has no commit, so both stay accepted. Starting the server in a container with auto-bootstrap on aborted startup. The refused folder is now skipped with a warning and the server starts without a bootstrap project. Co-Authored-By: Claude Fable 5.2 <[email protected]>
1 parent 9d1c95f commit 68c24c4

5 files changed

Lines changed: 76 additions & 59 deletions

File tree

‎apps/server/src/project/ProjectService.test.ts‎

Lines changed: 15 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -805,21 +805,24 @@ it.effect("refuses a worktree container as a new project and as a new workspace
805805
yield* git(fixture, "clone", "--bare", source, `${container}/.bare`);
806806
yield* fileSystem.writeFileString(`${container}/.git`, "gitdir: ./.bare\n");
807807
yield* git(container, "worktree", "add", "develop", "develop");
808-
yield* git(container, "config", "core.bare", "false");
809808

810809
yield* Effect.gen(function* () {
811810
const service = yield* ProjectService.ProjectService;
812-
const rejected = yield* service
813-
.create({
814-
commandId: CommandId.make("command:container:create"),
815-
projectId: ProjectId.make("project:container"),
816-
title: "Container",
817-
workspaceRoot: container,
818-
})
819-
.pipe(Effect.flip);
820-
assert.equal(rejected._tag, "ProjectInvalidError");
821-
assert.include(rejected.message, "Add one of its worktrees");
822-
assert.isTrue(Option.isNone(yield* service.getByWorkspaceRoot(container)));
811+
// As cloned the container is bare; Initialize Git or a config edit clears core.bare.
812+
for (const coreBare of ["true", "false"]) {
813+
yield* git(container, "config", "core.bare", coreBare);
814+
const rejected = yield* service
815+
.create({
816+
commandId: CommandId.make(`command:container:create:${coreBare}`),
817+
projectId: ProjectId.make(`project:container:${coreBare}`),
818+
title: "Container",
819+
workspaceRoot: container,
820+
})
821+
.pipe(Effect.flip);
822+
assert.equal(rejected._tag, "ProjectInvalidError", `core.bare=${coreBare}`);
823+
assert.include(rejected.message, "Add one of its worktrees");
824+
assert.isTrue(Option.isNone(yield* service.getByWorkspaceRoot(container)));
825+
}
823826

824827
const projectId = ProjectId.make("project:container:worktree");
825828
const worktree = yield* service.create({

‎apps/server/src/project/worktreeContainer.test.ts‎

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ const git = Effect.fn("worktreeContainerTest.git")(function* (
2222
it.layer(ProcessRunner.layer.pipe(Layer.provideMerge(NodeServices.layer)))(
2323
"isWorktreeContainer",
2424
(it) => {
25-
it.effect("is true only for a work tree that holds worktrees and none of HEAD's files", () =>
25+
it.effect("is true only for a folder of worktrees around metadata that is no checkout", () =>
2626
Effect.gen(function* () {
2727
const fileSystem = yield* FileSystem.FileSystem;
2828
const path = yield* Path.Path;
@@ -60,13 +60,9 @@ it.layer(ProcessRunner.layer.pipe(Layer.provideMerge(NodeServices.layer)))(
6060
yield* fileSystem.symlink(container, linked);
6161
assert.isTrue(yield* isWorktreeContainer(linked), "container through a symlink");
6262

63-
const indexed = yield* makeContainer("indexed", false);
64-
yield* git(indexed, "read-tree", "HEAD");
65-
assert.isTrue(yield* isWorktreeContainer(indexed), "container that gained an index");
66-
67-
// Git reports no work tree here, so the folder is a plain one, as before.
63+
// The issue's steps leave core.bare set; Initialize Git would clear it later.
6864
const bare = yield* makeContainer("bare", true);
69-
assert.isFalse(yield* isWorktreeContainer(bare), "core.bare container");
65+
assert.isTrue(yield* isWorktreeContainer(bare), "core.bare container");
7066

7167
// Git allows an orphan worktree before the first commit: no index, yet a valid checkout.
7268
const fresh = yield* makeSeparateGitDirCheckout("fresh");

‎apps/server/src/project/worktreeContainer.ts‎

Lines changed: 13 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -16,10 +16,10 @@ const parseGitfileTarget = (contents: string): string | undefined => {
1616
const splitNul = (stdout: string) => stdout.split("\0").filter((entry) => entry !== "");
1717

1818
/**
19-
* Whether a folder only holds a repository's metadata and its worktrees, with nothing checked out
20-
* in the folder itself, while Git still reports it as a work tree. The index is no evidence either
21-
* way (a fresh checkout has none, a container can gain one), so this compares HEAD with the folder.
22-
* Anything it cannot establish answers false.
19+
* Whether a folder only holds a repository's metadata and worktrees: its `.git` file points at
20+
* metadata inside it, a linked worktree lives inside it, and either the repository is bare or it
21+
* has commits but no index. A checkout whose tracked files are all deleted keeps its index, and a
22+
* fresh one has no commit. Anything this cannot establish answers false.
2323
*/
2424
export const makeIsWorktreeContainer = Effect.gen(function* () {
2525
const fileSystem = yield* FileSystem.FileSystem;
@@ -67,20 +67,16 @@ export const makeIsWorktreeContainer = Effect.gen(function* () {
6767
Effect.orElseSucceed(() => null),
6868
);
6969

70-
// With core.bare set Git sees no work tree here and the folder is added as a plain one.
71-
const insideWorkTree = yield* git(["rev-parse", "--is-inside-work-tree"]);
72-
if (insideWorkTree?.trim() !== "true") return false;
73-
74-
// Fails for an unborn HEAD, where a fresh checkout and a container look the same.
75-
const tracked = yield* git(["ls-tree", "-z", "--name-only", "HEAD"]);
76-
if (tracked === null) return false;
77-
const trackedNames = splitNul(tracked);
78-
if (trackedNames.length === 0) return false;
79-
for (const name of trackedNames) {
80-
const checkedOut = yield* fileSystem
81-
.exists(path.join(workspaceRoot, name))
70+
// Initializing Git in a bare container clears core.bare, so it is refused while still bare.
71+
const isBare = yield* git(["rev-parse", "--is-bare-repository"]);
72+
if (isBare === null) return false;
73+
if (isBare.trim() !== "true") {
74+
const hasCommit = yield* git(["rev-parse", "--quiet", "--verify", "HEAD^{commit}"]);
75+
if (hasCommit === null) return false;
76+
const hasIndex = yield* fileSystem
77+
.exists(path.join(gitDir.value, "index"))
8278
.pipe(Effect.orElseSucceed(() => true));
83-
if (checkedOut) return false;
79+
if (hasIndex) return false;
8480
}
8581

8682
const worktrees = yield* git(["worktree", "list", "--porcelain", "-z"]);

‎apps/server/src/serverRuntimeStartup.test.ts‎

Lines changed: 27 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,7 @@ it.effect.each([
5252
"disabled-default",
5353
"launch-failure",
5454
"project-failure",
55+
"project-refused",
5556
] as const)("keeps bootstrap launch failures separate from project failures: %s", (scenario) =>
5657
Effect.gen(function* () {
5758
const projectId = ProjectId.make("bootstrap-project");
@@ -155,22 +156,28 @@ it.effect.each([
155156
bootstrap: () =>
156157
scenario === "project-failure"
157158
? Effect.die("project bootstrap failed")
158-
: Effect.succeed({
159-
created: true,
160-
project: {
161-
id: projectId,
162-
title: "Project",
163-
workspaceRoot: "/bootstrap-project",
164-
repositoryIdentity: null,
165-
faviconPath: null,
166-
scripts: [],
167-
createdAt: "2026-01-01T00:00:00Z",
168-
updatedAt: "2026-01-01T00:00:00Z",
169-
deletedAt: null,
170-
defaultModelSelection:
171-
scenario === "disabled-default" ? { instanceId, model: "custom-model" } : null,
172-
},
173-
}),
159+
: scenario === "project-refused"
160+
? Effect.fail(
161+
new ProjectService.ProjectInvalidError({ projectId, detail: "refused folder" }),
162+
)
163+
: Effect.succeed({
164+
created: true,
165+
project: {
166+
id: projectId,
167+
title: "Project",
168+
workspaceRoot: "/bootstrap-project",
169+
repositoryIdentity: null,
170+
faviconPath: null,
171+
scripts: [],
172+
createdAt: "2026-01-01T00:00:00Z",
173+
updatedAt: "2026-01-01T00:00:00Z",
174+
deletedAt: null,
175+
defaultModelSelection:
176+
scenario === "disabled-default"
177+
? { instanceId, model: "custom-model" }
178+
: null,
179+
},
180+
}),
174181
}),
175182
Layer.mock(ThreadManagement.ThreadManagementService)({
176183
countThreads: () => Effect.succeed(0),
@@ -223,7 +230,10 @@ it.effect.each([
223230
} else {
224231
assert.isDefined(welcome);
225232
if (welcome?.type === "welcome") {
226-
assert.equal(welcome.payload.bootstrapProjectId, projectId);
233+
assert.equal(
234+
welcome.payload.bootstrapProjectId,
235+
scenario === "project-refused" ? undefined : projectId,
236+
);
227237
assert.isUndefined(welcome.payload.bootstrapThreadId);
228238
}
229239
}

‎apps/server/src/serverRuntimeStartup.ts‎

Lines changed: 18 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -270,12 +270,24 @@ const resolveAutoBootstrapWelcomeTargets = Effect.gen(function* () {
270270

271271
if (serverConfig.autoBootstrapProjectFromCwd) {
272272
const threadModelSelection = getAutoBootstrapThreadModelSelection();
273-
const { project } = yield* projects.bootstrap({
274-
commandId: CommandId.make(yield* randomUUID),
275-
projectId: ProjectId.make(yield* randomUUID),
276-
title: path.basename(serverConfig.cwd) || "project",
277-
workspaceRoot: serverConfig.cwd,
278-
});
273+
const bootstrapped = yield* projects
274+
.bootstrap({
275+
commandId: CommandId.make(yield* randomUUID),
276+
projectId: ProjectId.make(yield* randomUUID),
277+
title: path.basename(serverConfig.cwd) || "project",
278+
workspaceRoot: serverConfig.cwd,
279+
})
280+
.pipe(
281+
// A folder the project rules refuse must not stop the server from starting there.
282+
Effect.catchTags({
283+
ProjectInvalidError: (error) =>
284+
Effect.logWarning("Skipped bootstrap project", { detail: error.detail }).pipe(
285+
Effect.as(undefined),
286+
),
287+
}),
288+
);
289+
if (bootstrapped === undefined) return {} satisfies AutoBootstrapWelcomeTargets;
290+
const { project } = bootstrapped;
279291
const existingThread = (yield* threads.listActiveShellIndex()).find(
280292
(thread) => thread.projectId === project.id && isListedThreadLineage(thread.lineage),
281293
);

0 commit comments

Comments
 (0)