Skip to content

Commit 43174b5

Browse files
n4mr3gclaude
andcommitted
fix(server): match worktrees when the project's .git is a gitdir pointer
A submodule or a --separate-git-dir checkout has a .git file, so the root's git dir identity was null and its linked worktrees never matched. readGitIdentity already resolves the pointer; reuse its git dir. Co-Authored-By: Claude Opus 5.5 <[email protected]>
1 parent 243dd64 commit 43174b5

2 files changed

Lines changed: 108 additions & 49 deletions

File tree

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

Lines changed: 100 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -124,6 +124,55 @@ const writeTranscript = Effect.fn("AgentSessionScanner.test.writeTranscript")(fu
124124
yield* fileSystem.utimes(input.filePath, seconds, seconds);
125125
});
126126

127+
/** Lays out a linked worktree the way `git worktree add` does, without running git. */
128+
const addLinkedWorktree = Effect.fn("AgentSessionScanner.test.addLinkedWorktree")(
129+
function* (input: {
130+
readonly commonGitDir: string;
131+
readonly directory: string;
132+
readonly branch: string;
133+
}) {
134+
const fileSystem = yield* FileSystem.FileSystem;
135+
const path = yield* Path.Path;
136+
const gitDir = path.join(input.commonGitDir, "worktrees", input.branch);
137+
yield* fileSystem.makeDirectory(gitDir, { recursive: true });
138+
yield* fileSystem.makeDirectory(input.directory, { recursive: true });
139+
yield* fileSystem.writeFileString(path.join(gitDir, "commondir"), "../..\n");
140+
yield* fileSystem.writeFileString(
141+
path.join(gitDir, "HEAD"),
142+
`ref: refs/heads/${input.branch}\n`,
143+
);
144+
yield* fileSystem.writeFileString(path.join(input.directory, ".git"), `gitdir: ${gitDir}\n`);
145+
},
146+
);
147+
148+
const writeClaudeSession = Effect.fn("AgentSessionScanner.test.writeClaudeSession")(
149+
function* (input: {
150+
readonly claudeHomePath: string;
151+
readonly sessionId: string;
152+
readonly cwd: string;
153+
readonly mtimeMs: number;
154+
}) {
155+
const path = yield* Path.Path;
156+
yield* writeTranscript({
157+
filePath: path.join(input.claudeHomePath, "projects", input.sessionId, "session.jsonl"),
158+
contents: `${JSON.stringify({
159+
type: "user",
160+
cwd: input.cwd,
161+
sessionId: input.sessionId,
162+
message: { role: "user", content: "Fix the project" },
163+
})}\n`,
164+
mtimeMs: input.mtimeMs,
165+
});
166+
},
167+
);
168+
169+
const importedWorktrees = (outcomes: ReadonlyArray<AgentSessionScanner.AgentSessionRecentThread>) =>
170+
outcomes.flatMap((outcome) =>
171+
outcome._tag === "Importable"
172+
? [{ sessionId: outcome.thread.providerSessionId, worktree: outcome.worktree }]
173+
: [],
174+
);
175+
127176
/** Claude session line: the first record carries the real `cwd`. */
128177
const claudeSessionLine = (cwd: string) =>
129178
`${JSON.stringify({ type: "user", cwd, sessionId: "s1" })}\n${JSON.stringify({ type: "assistant" })}\n`;
@@ -2418,7 +2467,6 @@ it.layer(NodeServices.layer)("AgentSessionScanner", (it) => {
24182467
it.effect("imports sessions from linked worktrees of the project with their branch", () =>
24192468
Effect.gen(function* () {
24202469
const path = yield* Path.Path;
2421-
const fileSystem = yield* FileSystem.FileSystem;
24222470
const nowMs = Date.parse("2026-08-24T12:00:00.000Z");
24232471
yield* TestClock.setTime(nowMs);
24242472
const claudeHomePath = yield* makeTempDir("t3code-claude-home-");
@@ -2427,45 +2475,27 @@ it.layer(NodeServices.layer)("AgentSessionScanner", (it) => {
24272475
const repo = yield* makeTempDir("t3code-workspace-repo-");
24282476
const otherRepo = yield* makeTempDir("t3code-workspace-other-");
24292477

2430-
const addWorktree = Effect.fn("addWorktree")(function* (input: {
2431-
readonly repo: string;
2432-
readonly directory: string;
2433-
readonly name: string;
2434-
}) {
2435-
const gitDir = path.join(input.repo, ".git", "worktrees", input.name);
2436-
yield* fileSystem.makeDirectory(gitDir, { recursive: true });
2437-
yield* fileSystem.makeDirectory(input.directory, { recursive: true });
2438-
yield* fileSystem.writeFileString(path.join(gitDir, "commondir"), "../..\n");
2439-
yield* fileSystem.writeFileString(
2440-
path.join(gitDir, "HEAD"),
2441-
`ref: refs/heads/${input.name}\n`,
2442-
);
2443-
yield* fileSystem.writeFileString(
2444-
path.join(input.directory, ".git"),
2445-
`gitdir: ${gitDir}\n`,
2446-
);
2447-
});
24482478
const worktree = path.join(repo, ".claude", "worktrees", "feature");
24492479
const managedWorktree = path.join(configBaseDir, "worktrees", "repo", "managed");
24502480
const otherWorktree = path.join(otherRepo, "elsewhere");
2451-
yield* addWorktree({ repo, directory: worktree, name: "feature" });
2452-
yield* addWorktree({ repo, directory: managedWorktree, name: "managed" });
2453-
yield* addWorktree({ repo: otherRepo, directory: otherWorktree, name: "elsewhere" });
2454-
2455-
for (const [index, [sessionId, cwd]] of [
2456-
["root-session", repo],
2457-
["worktree-session", worktree],
2458-
["managed-session", managedWorktree],
2459-
["other-session", otherWorktree],
2481+
const commonGitDir = path.join(repo, ".git");
2482+
yield* addLinkedWorktree({ commonGitDir, directory: worktree, branch: "feature" });
2483+
yield* addLinkedWorktree({ commonGitDir, directory: managedWorktree, branch: "managed" });
2484+
yield* addLinkedWorktree({
2485+
commonGitDir: path.join(otherRepo, ".git"),
2486+
directory: otherWorktree,
2487+
branch: "elsewhere",
2488+
});
2489+
2490+
for (const [index, session] of [
2491+
{ sessionId: "root-session", cwd: repo },
2492+
{ sessionId: "worktree-session", cwd: worktree },
2493+
{ sessionId: "managed-session", cwd: managedWorktree },
2494+
{ sessionId: "other-session", cwd: otherWorktree },
24602495
].entries()) {
2461-
yield* writeTranscript({
2462-
filePath: path.join(claudeHomePath, "projects", `-slug-${index}`, `${sessionId}.jsonl`),
2463-
contents: `${JSON.stringify({
2464-
type: "user",
2465-
cwd,
2466-
sessionId,
2467-
message: { role: "user", content: "Fix the project" },
2468-
})}\n`,
2496+
yield* writeClaudeSession({
2497+
...session,
2498+
claudeHomePath,
24692499
mtimeMs: nowMs - (index + 1) * 60 * 1000,
24702500
});
24712501
}
@@ -2477,19 +2507,46 @@ it.layer(NodeServices.layer)("AgentSessionScanner", (it) => {
24772507
workspaceRoot: repo,
24782508
});
24792509

2480-
expect(
2481-
outcomes.flatMap((outcome) =>
2482-
outcome._tag === "Importable"
2483-
? [{ sessionId: outcome.thread.providerSessionId, worktree: outcome.worktree }]
2484-
: [],
2485-
),
2486-
).toEqual([
2510+
expect(importedWorktrees(outcomes)).toEqual([
24872511
{ sessionId: "root-session", worktree: null },
24882512
{ sessionId: "worktree-session", worktree: { path: worktree, branch: "feature" } },
24892513
]);
24902514
}),
24912515
);
24922516

2517+
it.effect("imports linked worktree sessions when the project's .git is a pointer", () =>
2518+
Effect.gen(function* () {
2519+
const path = yield* Path.Path;
2520+
const fileSystem = yield* FileSystem.FileSystem;
2521+
const nowMs = Date.parse("2026-08-24T12:00:00.000Z");
2522+
yield* TestClock.setTime(nowMs);
2523+
const claudeHomePath = yield* makeTempDir("t3code-claude-home-");
2524+
const codexHomePath = yield* makeTempDir("t3code-codex-home-");
2525+
const repo = yield* makeTempDir("t3code-workspace-repo-");
2526+
const commonGitDir = yield* makeTempDir("t3code-separate-git-dir-");
2527+
yield* fileSystem.writeFileString(path.join(repo, ".git"), `gitdir: ${commonGitDir}\n`);
2528+
2529+
const worktree = path.join(repo, ".claude", "worktrees", "feature");
2530+
yield* addLinkedWorktree({ commonGitDir, directory: worktree, branch: "feature" });
2531+
yield* writeClaudeSession({
2532+
claudeHomePath,
2533+
sessionId: "worktree-session",
2534+
cwd: worktree,
2535+
mtimeMs: nowMs,
2536+
});
2537+
2538+
const outcomes = yield* runRecentThreadOutcomes({
2539+
claudeHomePath,
2540+
codexHomePath,
2541+
workspaceRoot: repo,
2542+
});
2543+
2544+
expect(importedWorktrees(outcomes)).toEqual([
2545+
{ sessionId: "worktree-session", worktree: { path: worktree, branch: "feature" } },
2546+
]);
2547+
}),
2548+
);
2549+
24932550
it.effect("uses one deterministic provider instance for a shared session home", () =>
24942551
Effect.gen(function* () {
24952552
const path = yield* Path.Path;

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

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -704,7 +704,11 @@ export const make = Effect.gen(function* () {
704704
const readGitIdentity = Effect.fn("AgentSessionScanner.readGitIdentity")(function* (
705705
directory: string,
706706
): Effect.fn.Return<
707-
| { readonly _tag: "Repository"; readonly git: AgentSessionProjectGit | null }
707+
| {
708+
readonly _tag: "Repository";
709+
readonly gitDir: string;
710+
readonly git: AgentSessionProjectGit | null;
711+
}
708712
| { readonly _tag: "Worktree"; readonly gitDir: string }
709713
| { readonly _tag: "NotGit" }
710714
> {
@@ -729,6 +733,7 @@ export const make = Effect.gen(function* () {
729733
const originUrl = parseOriginUrlFromGitConfig(configText);
730734
return {
731735
_tag: "Repository",
736+
gitDir,
732737
git: {
733738
remoteKey: originUrl === null ? null : normalizeGitRemoteUrl(originUrl),
734739
repository: parseGitHubRepositoryNameWithOwnerFromRemoteUrl(originUrl),
@@ -1384,14 +1389,11 @@ export const make = Effect.gen(function* () {
13841389
const root = path.resolve(expandHomePath(workspaceRoot, homeDir));
13851390
const realRoot = yield* fileSystem.realPath(root).pipe(Effect.orElseSucceed(() => root));
13861391
if (isExcludedProjectPath(root) || isExcludedProjectPath(realRoot)) return Stream.empty;
1387-
const rootGitDir = path.join(root, ".git");
1388-
const rootGitDirStats = yield* statOption(rootGitDir);
1392+
const rootGit = yield* readGitIdentity(root);
13891393
const project = {
13901394
identity: yield* directoryIdentity(root),
13911395
gitDirIdentity:
1392-
Option.isSome(rootGitDirStats) && rootGitDirStats.value.type === "Directory"
1393-
? yield* directoryIdentity(rootGitDir, rootGitDirStats.value)
1394-
: null,
1396+
rootGit._tag === "Repository" ? yield* directoryIdentity(rootGit.gitDir) : null,
13951397
};
13961398
const nowMs = DateTime.toEpochMillis(yield* DateTime.now);
13971399
const cutoffMs = nowMs - RECENT_THREAD_WINDOW_MS;

0 commit comments

Comments
 (0)