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
39 changes: 39 additions & 0 deletions apps/server/src/mcp/WorktreeMcpService.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -702,10 +702,49 @@ describe("t3_worktree_status", () => {
attached: true,
worktreePath: "/worktrees/project/existing",
branch: "feature/existing",
detached: false,
defaultStartFromOrigin: true,
});
});
});

it.effect("reports detached HEAD when an attached worktree has branch null", () => {
const harness = makeHarness({
thread: {
worktreePath: "/worktrees/project/existing",
branch: null,
},
});
return Effect.gen(function* () {
const result = yield* runStatus(harness);
expect(result).toMatchObject({
attached: true,
worktreePath: "/worktrees/project/existing",
branch: null,
detached: true,
defaultStartFromOrigin: false,
});
});
});

it.effect("reports detached as false when the thread is not attached to a worktree", () => {
const harness = makeHarness({
thread: {
worktreePath: null,
branch: null,
},
});
return Effect.gen(function* () {
const result = yield* runStatus(harness);
expect(result).toMatchObject({
attached: false,
worktreePath: null,
branch: null,
detached: false,
defaultStartFromOrigin: false,
});
});
});
});

describe("t3_worktree_list", () => {
Expand Down
1 change: 1 addition & 0 deletions apps/server/src/mcp/WorktreeMcpService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -511,6 +511,7 @@ const make = Effect.gen(function* () {
attached: projection.thread.worktreePath !== null,
worktreePath: projection.thread.worktreePath,
branch: projection.thread.branch,
detached: projection.thread.worktreePath !== null && projection.thread.branch === null,
projectWorkspaceRoot: project.workspaceRoot,
defaultStartFromOrigin,
};
Expand Down
174 changes: 174 additions & 0 deletions apps/server/src/orchestration-v2/WorktreeBranchSync.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@ it.effect.each([false, true])(
Effect.provide(
Layer.mergeAll(
FileSystem.layerNoop({
exists: () => Effect.succeed(true),
realPath: (path) => Effect.succeed(path === "/link/worktree" ? "/worktree" : path),
}),
Layer.mock(ProjectionStore.ProjectionStoreV2)({
Expand Down Expand Up @@ -108,3 +109,176 @@ it.effect.each([false, true])(
);
},
);

it.effect(
"reconciles detached HEAD when the worktree exists and clears stale branch metadata",
() => {
let threads: ReadonlyArray<OrchestrationV2ThreadShell> = [
{
id: ThreadId.make("detached-thread"),
branch: "feature/stale",
worktreePath: "/worktree",
} as OrchestrationV2ThreadShell,
];
const commands: OrchestrationV2ServerCommand[] = [];
return Effect.gen(function* () {
const reconcile = yield* WorktreeBranchSync.makeReconcile;
yield* reconcile({ cwd: "/worktree", refName: null });
assert.deepEqual(
commands.map((command) => {
assert.equal(command.type, "thread.metadata.update");
if (command.type !== "thread.metadata.update") throw new Error("Unexpected command");
return [
command.threadId,
command.branch,
command.expectedBranch,
command.expectedWorktreePath,
];
}),
[["detached-thread", null, "feature/stale", "/worktree"]],
);
assert.equal(threads[0]?.branch, null);
}).pipe(
Effect.provide(
Layer.mergeAll(
FileSystem.layerNoop({
exists: () => Effect.succeed(true),
realPath: (path) => Effect.succeed(path),
}),
Layer.mock(ProjectionStore.ProjectionStoreV2)({
getThreadWorktreePaths: () =>
Effect.succeed([
{ threadId: ThreadId.make("detached-thread"), worktreePath: "/worktree" },
]),
}),
Layer.mock(GitWorkflowService.GitWorkflowService)({
repositoryRoot: (cwd) => Effect.succeed(cwd),
currentBranch: () => Effect.succeed(null),
}),
Layer.mock(ThreadManagement.ThreadManagementService)({
getShellSnapshot: () =>
Effect.succeed({
schemaVersion: 2,
snapshotSequence: 0,
threads,
archivedThreads: [],
}),
dispatch: (command) =>
Effect.sync(() => {
commands.push(command);
if (command.type === "thread.metadata.update" && command.branch !== undefined) {
const branch = command.branch;
threads = threads.map((thread) =>
thread.id === command.threadId && thread.branch === command.expectedBranch
? { ...thread, branch }
: thread,
);
}
return { sequence: commands.length, storedEvents: [] };
}),
}),
),
),
);
},
);

it.effect("ignores detached HEAD events when the worktree directory does not exist", () => {
const commands: OrchestrationV2ServerCommand[] = [];
const threads: ReadonlyArray<OrchestrationV2ThreadShell> = [
{
id: ThreadId.make("missing-worktree"),
branch: "feature/stale",
worktreePath: "/worktree",
} as OrchestrationV2ThreadShell,
];
return Effect.gen(function* () {
const reconcile = yield* WorktreeBranchSync.makeReconcile;
yield* reconcile({ cwd: "/worktree", refName: null });
assert.deepEqual(commands, []);
assert.equal(threads[0]?.branch, "feature/stale");
}).pipe(
Effect.provide(
Layer.mergeAll(
FileSystem.layerNoop({
exists: () => Effect.succeed(false),
realPath: (path) => Effect.succeed(path),
}),
Layer.mock(ProjectionStore.ProjectionStoreV2)({
getThreadWorktreePaths: () =>
Effect.succeed([
{ threadId: ThreadId.make("missing-worktree"), worktreePath: "/worktree" },
]),
}),
Layer.mock(GitWorkflowService.GitWorkflowService)({
currentBranch: () => Effect.succeed(null),
}),
Layer.mock(ThreadManagement.ThreadManagementService)({
getShellSnapshot: () =>
Effect.succeed({
schemaVersion: 2,
snapshotSequence: 0,
threads,
archivedThreads: [],
}),
dispatch: (command) =>
Effect.sync(() => {
commands.push(command);
return { sequence: commands.length, storedEvents: [] };
}),
}),
),
),
);
});

it.effect(
"ignores detached HEAD events when the worktree directory is no longer a repository",
() => {
const commands: OrchestrationV2ServerCommand[] = [];
return Effect.gen(function* () {
const reconcile = yield* WorktreeBranchSync.makeReconcile;
yield* reconcile({ cwd: "/worktree", refName: null });
assert.deepEqual(commands, []);
}).pipe(
Effect.provide(
Layer.mergeAll(
FileSystem.layerNoop({
exists: () => Effect.succeed(true),
realPath: (path) => Effect.succeed(path),
}),
Layer.mock(ProjectionStore.ProjectionStoreV2)({
getThreadWorktreePaths: () =>
Effect.succeed([
{ threadId: ThreadId.make("pruned-worktree"), worktreePath: "/worktree" },
]),
}),
Layer.mock(GitWorkflowService.GitWorkflowService)({
repositoryRoot: () => Effect.succeed(null),
currentBranch: () => Effect.succeed(null),
}),
Layer.mock(ThreadManagement.ThreadManagementService)({
getShellSnapshot: () =>
Effect.succeed({
schemaVersion: 2,
snapshotSequence: 0,
threads: [
{
id: ThreadId.make("pruned-worktree"),
branch: "feature/kept",
worktreePath: "/worktree",
} as OrchestrationV2ThreadShell,
],
archivedThreads: [],
}),
dispatch: (command) =>
Effect.sync(() => {
commands.push(command);
return { sequence: commands.length, storedEvents: [] };
}),
}),
),
),
);
},
);
6 changes: 5 additions & 1 deletion apps/server/src/orchestration-v2/WorktreeBranchSync.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@ export const makeReconcile = Effect.gen(function* () {
readonly cwd: string;
readonly refName: string | null;
}) {
if (input.refName === null) return;
const paths = yield* projections.getThreadWorktreePaths({ includeArchived: true });
const matchingPaths = new Set<string>();
for (const worktreePath of new Set(paths.map((thread) => thread.worktreePath))) {
Expand All @@ -43,6 +42,11 @@ export const makeReconcile = Effect.gen(function* () {
thread.worktreePath === matchingThreads.get(thread.id) && thread.branch !== input.refName,
);
if (sharing.length === 0) return;
if (input.refName === null) {
if (!(yield* fs.exists(input.cwd))) return;
// currentBranch is also null outside a repository, e.g. a worktree whose admin dir was pruned.
if ((yield* git.repositoryRoot(input.cwd)) === null) return;
}
// Status events may queue behind a generated rename or another checkout.
if ((yield* git.currentBranch(input.cwd)) !== input.refName) return;
for (const thread of sharing) {
Expand Down
3 changes: 3 additions & 0 deletions packages/contracts/src/worktreeMcp.ts
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,9 @@ export const WorktreeMcpStatusResult = Schema.Struct({
attached: Schema.Boolean,
worktreePath: Schema.NullOr(TrimmedNonEmptyString),
branch: Schema.NullOr(GitRefName),
detached: Schema.Boolean.annotate({
description: "True when the attached worktree is on a detached HEAD (`branch` is `null`).",
}),
projectWorkspaceRoot: TrimmedNonEmptyString,
defaultStartFromOrigin: Schema.Boolean.annotate({
description: "`startFromOrigin` default for `t3_worktree_handoff`.",
Expand Down
Loading