Skip to content

Commit f2915da

Browse files
fix(server): preserve linked checkout sessions during import
1 parent b560473 commit f2915da

2 files changed

Lines changed: 222 additions & 189 deletions

File tree

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

Lines changed: 119 additions & 105 deletions
Original file line numberDiff line numberDiff line change
@@ -47,13 +47,17 @@ interface ScannerTestInput {
4747
readonly importedWorkspaceRoots?: ReadonlyArray<string>;
4848
readonly configBaseDir?: string;
4949
readonly providerInstances?: ContractServerSettings["providerInstances"];
50+
readonly worktreesDirectory?: string;
51+
readonly previousWorktreesDirectories?: ReadonlyArray<string>;
5052
}
5153

5254
const layerScannerTest = (input: ScannerTestInput) =>
5355
AgentSessionScanner.layer.pipe(
5456
Layer.provide(
5557
Layer.mergeAll(
5658
ServerSettings.layerTest({
59+
worktreesDirectory: input.worktreesDirectory ?? "",
60+
previousWorktreesDirectories: input.previousWorktreesDirectories ?? [],
5761
providerInstances: {
5862
[ProviderInstanceId.make("claudeAgent")]: {
5963
driver: ProviderDriverKind.make("claudeAgent"),
@@ -136,34 +140,6 @@ const addLinkedWorktree = Effect.fn("AgentSessionScanner.test.addLinkedWorktree"
136140
},
137141
);
138142

139-
const writeClaudeSession = Effect.fn("AgentSessionScanner.test.writeClaudeSession")(
140-
function* (input: {
141-
readonly claudeHomePath: string;
142-
readonly sessionId: string;
143-
readonly cwd: string;
144-
readonly mtimeMs: number;
145-
}) {
146-
const path = yield* Path.Path;
147-
yield* writeTranscript({
148-
filePath: path.join(input.claudeHomePath, "projects", input.sessionId, "session.jsonl"),
149-
contents: `${JSON.stringify({
150-
type: "user",
151-
cwd: input.cwd,
152-
sessionId: input.sessionId,
153-
message: { role: "user", content: "Fix the project" },
154-
})}\n`,
155-
mtimeMs: input.mtimeMs,
156-
});
157-
},
158-
);
159-
160-
const importedWorktrees = (outcomes: ReadonlyArray<AgentSessionScanner.AgentSessionRecentThread>) =>
161-
outcomes.flatMap((outcome) =>
162-
outcome._tag === "Importable"
163-
? [{ sessionId: outcome.thread.providerSessionId, worktree: outcome.worktree }]
164-
: [],
165-
);
166-
167143
const claudeSessionLine = (cwd: string) =>
168144
`${JSON.stringify({ type: "user", cwd, sessionId: "s1" })}\n${JSON.stringify({ type: "assistant" })}\n`;
169145

@@ -2438,87 +2414,125 @@ it.layer(NodeServices.layer)("AgentSessionScanner", (it) => {
24382414
}),
24392415
);
24402416

2441-
it.effect("imports sessions from linked worktrees of the project with their branch", () =>
2442-
Effect.gen(function* () {
2443-
const path = yield* Path.Path;
2444-
const nowMs = Date.parse("2026-08-24T12:00:00.000Z");
2445-
yield* TestClock.setTime(nowMs);
2446-
const claudeHomePath = yield* makeTempDir("t3code-claude-home-");
2447-
const codexHomePath = yield* makeTempDir("t3code-codex-home-");
2448-
const configBaseDir = yield* makeTempDir("t3code-scanner-base-");
2449-
const repo = yield* makeTempDir("t3code-workspace-repo-");
2450-
const otherRepo = yield* makeTempDir("t3code-workspace-other-");
2451-
2452-
const worktree = path.join(repo, ".claude", "worktrees", "feature");
2453-
const managedWorktree = path.join(configBaseDir, "worktrees", "repo", "managed");
2454-
const otherWorktree = path.join(otherRepo, "elsewhere");
2455-
const commonGitDir = path.join(repo, ".git");
2456-
yield* addLinkedWorktree({ commonGitDir, directory: worktree, branch: "feature" });
2457-
yield* addLinkedWorktree({ commonGitDir, directory: managedWorktree, branch: "managed" });
2458-
yield* addLinkedWorktree({
2459-
commonGitDir: path.join(otherRepo, ".git"),
2460-
directory: otherWorktree,
2461-
branch: "elsewhere",
2462-
});
2463-
2464-
for (const [index, session] of [
2465-
{ sessionId: "root-session", cwd: repo },
2466-
{ sessionId: "worktree-session", cwd: worktree },
2467-
{ sessionId: "managed-session", cwd: managedWorktree },
2468-
{ sessionId: "other-session", cwd: otherWorktree },
2469-
].entries()) {
2470-
yield* writeClaudeSession({
2471-
...session,
2472-
claudeHomePath,
2473-
mtimeMs: nowMs - (index + 1) * 60 * 1000,
2417+
it.effect.each(["directory", "pointer"] as const)(
2418+
"imports only the project's linked worktrees with their branches when .git is a %s",
2419+
(layout) =>
2420+
Effect.gen(function* () {
2421+
const path = yield* Path.Path;
2422+
const fileSystem = yield* FileSystem.FileSystem;
2423+
const nowMs = Date.parse("2026-08-24T12:00:00.000Z");
2424+
yield* TestClock.setTime(nowMs);
2425+
const claudeHomePath = yield* makeTempDir("t3code-claude-home-");
2426+
const codexHomePath = yield* makeTempDir("t3code-codex-home-");
2427+
const configBaseDir = yield* makeTempDir("t3code-scanner-base-");
2428+
const repo = yield* makeTempDir("t3code-workspace-repo-");
2429+
const otherRepo = yield* makeTempDir("t3code-workspace-other-");
2430+
const commonGitDir =
2431+
layout === "directory"
2432+
? path.join(repo, ".git")
2433+
: yield* makeTempDir("t3code-separate-git-dir-");
2434+
if (layout === "pointer") {
2435+
yield* fileSystem.writeFileString(path.join(repo, ".git"), `gitdir: ${commonGitDir}\n`);
2436+
}
2437+
2438+
const worktree = path.join(repo, ".claude", "worktrees", "feature");
2439+
const externalWorktree = yield* makeTempDir("t3code-workspace-feature-");
2440+
const managedWorktree = path.join(configBaseDir, "worktrees", "repo", "managed");
2441+
const customDirectory = yield* makeTempDir("t3code-custom-worktrees-");
2442+
const previousDirectory = yield* makeTempDir("t3code-previous-worktrees-");
2443+
const customWorktree = path.join(customDirectory, "repo", "custom");
2444+
const previousWorktree = path.join(previousDirectory, "repo", "previous");
2445+
const otherWorktree = path.join(repo, "unrelated");
2446+
yield* addLinkedWorktree({ commonGitDir, directory: worktree, branch: "feature" });
2447+
yield* addLinkedWorktree({
2448+
commonGitDir,
2449+
directory: externalWorktree,
2450+
branch: "external",
2451+
});
2452+
yield* addLinkedWorktree({ commonGitDir, directory: managedWorktree, branch: "managed" });
2453+
yield* addLinkedWorktree({ commonGitDir, directory: customWorktree, branch: "custom" });
2454+
yield* addLinkedWorktree({
2455+
commonGitDir,
2456+
directory: previousWorktree,
2457+
branch: "previous",
2458+
});
2459+
yield* addLinkedWorktree({
2460+
commonGitDir: path.join(otherRepo, ".git"),
2461+
directory: otherWorktree,
2462+
branch: "elsewhere",
24742463
});
2475-
}
2476-
2477-
const outcomes = yield* runRecentThreadOutcomes({
2478-
claudeHomePath,
2479-
codexHomePath,
2480-
configBaseDir,
2481-
workspaceRoot: repo,
2482-
});
2483-
2484-
expect(importedWorktrees(outcomes)).toEqual([
2485-
{ sessionId: "root-session", worktree: null },
2486-
{ sessionId: "worktree-session", worktree: { path: worktree, branch: "feature" } },
2487-
]);
2488-
}),
2489-
);
2490-
2491-
it.effect("imports linked worktree sessions when the project's .git is a pointer", () =>
2492-
Effect.gen(function* () {
2493-
const path = yield* Path.Path;
2494-
const fileSystem = yield* FileSystem.FileSystem;
2495-
const nowMs = Date.parse("2026-08-24T12:00:00.000Z");
2496-
yield* TestClock.setTime(nowMs);
2497-
const claudeHomePath = yield* makeTempDir("t3code-claude-home-");
2498-
const codexHomePath = yield* makeTempDir("t3code-codex-home-");
2499-
const repo = yield* makeTempDir("t3code-workspace-repo-");
2500-
const commonGitDir = yield* makeTempDir("t3code-separate-git-dir-");
2501-
yield* fileSystem.writeFileString(path.join(repo, ".git"), `gitdir: ${commonGitDir}\n`);
25022464

2503-
const worktree = path.join(repo, ".claude", "worktrees", "feature");
2504-
yield* addLinkedWorktree({ commonGitDir, directory: worktree, branch: "feature" });
2505-
yield* writeClaudeSession({
2506-
claudeHomePath,
2507-
sessionId: "worktree-session",
2508-
cwd: worktree,
2509-
mtimeMs: nowMs,
2510-
});
2465+
let worktreesDirectory = customDirectory;
2466+
const previousWorktreesDirectories = [previousDirectory, path.parse(repo).root];
2467+
const sessions = [
2468+
{ sessionId: "root-session", cwd: repo },
2469+
{ sessionId: "worktree-session", cwd: worktree },
2470+
{ sessionId: "external-session", cwd: externalWorktree },
2471+
{ sessionId: "managed-session", cwd: managedWorktree },
2472+
{ sessionId: "custom-session", cwd: customWorktree },
2473+
{ sessionId: "previous-session", cwd: previousWorktree },
2474+
{ sessionId: "other-session", cwd: otherWorktree },
2475+
];
2476+
if (symlinksSupported) {
2477+
const aliases = yield* makeTempDir("t3code-managed-aliases-");
2478+
for (const [name, directory, branch] of [
2479+
["default", path.join(configBaseDir, "worktrees"), "managed"],
2480+
["custom", customDirectory, "custom"],
2481+
["previous", previousDirectory, "previous"],
2482+
] as const) {
2483+
const alias = path.join(aliases, name);
2484+
yield* fileSystem.symlink(directory, alias);
2485+
sessions.push({
2486+
sessionId: `${name}-alias-session`,
2487+
cwd: path.join(alias, "repo", branch),
2488+
});
2489+
if (name === "custom") worktreesDirectory = alias;
2490+
if (name === "previous") previousWorktreesDirectories[0] = alias;
2491+
}
2492+
const rootAlias = path.join(aliases, "root");
2493+
yield* fileSystem.symlink(path.parse(repo).root, rootAlias);
2494+
previousWorktreesDirectories.push(rootAlias);
2495+
const escapedAlias = path.join(customDirectory, "external");
2496+
yield* fileSystem.symlink(externalWorktree, escapedAlias);
2497+
sessions.push({ sessionId: "managed-escape-session", cwd: escapedAlias });
2498+
}
2499+
2500+
for (const [index, session] of sessions.entries()) {
2501+
yield* writeTranscript({
2502+
filePath: path.join(claudeHomePath, "projects", session.sessionId, "session.jsonl"),
2503+
contents: `${JSON.stringify({
2504+
type: "user",
2505+
...session,
2506+
message: { role: "user", content: "Fix the project" },
2507+
})}\n`,
2508+
mtimeMs: nowMs - (index + 1) * 60 * 1000,
2509+
});
2510+
}
25112511

2512-
const outcomes = yield* runRecentThreadOutcomes({
2513-
claudeHomePath,
2514-
codexHomePath,
2515-
workspaceRoot: repo,
2516-
});
2512+
const outcomes = yield* runRecentThreadOutcomes({
2513+
claudeHomePath,
2514+
codexHomePath,
2515+
configBaseDir,
2516+
workspaceRoot: repo,
2517+
worktreesDirectory,
2518+
previousWorktreesDirectories,
2519+
});
25172520

2518-
expect(importedWorktrees(outcomes)).toEqual([
2519-
{ sessionId: "worktree-session", worktree: { path: worktree, branch: "feature" } },
2520-
]);
2521-
}),
2521+
expect(
2522+
outcomes.flatMap((outcome) =>
2523+
outcome._tag === "Importable"
2524+
? [{ sessionId: outcome.thread.providerSessionId, worktree: outcome.worktree }]
2525+
: [],
2526+
),
2527+
).toEqual([
2528+
{ sessionId: "root-session", worktree: null },
2529+
{ sessionId: "worktree-session", worktree: { path: worktree, branch: "feature" } },
2530+
{
2531+
sessionId: "external-session",
2532+
worktree: { path: externalWorktree, branch: "external" },
2533+
},
2534+
]);
2535+
}),
25222536
);
25232537

25242538
it.effect("uses one deterministic provider instance for a shared session home", () =>

0 commit comments

Comments
 (0)