Skip to content

Commit d46b90c

Browse files
fix(server): the Welcome page offers repositories you only worked on in worktrees (pingdotgg#33923)
Co-authored-by: Julius Marminge <[email protected]>
1 parent ee9e36c commit d46b90c

3 files changed

Lines changed: 144 additions & 47 deletions

File tree

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

Lines changed: 65 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -869,7 +869,7 @@ it.layer(NodeServices.layer)("AgentSessionScanner", (it) => {
869869
}),
870870
);
871871

872-
it.effect("skips linked git worktrees and reports the origin of real checkouts", () =>
872+
it.effect("folds linked git worktrees into their checkout and reports real origins", () =>
873873
Effect.gen(function* () {
874874
const path = yield* Path.Path;
875875
const fileSystem = yield* FileSystem.FileSystem;
@@ -905,32 +905,91 @@ it.layer(NodeServices.layer)("AgentSessionScanner", (it) => {
905905
path.join(submodule, ".git"),
906906
`gitdir: ${submoduleGitDir}\n`,
907907
);
908+
// A repository whose sessions all ran in a linked worktree is still offered.
909+
const worktreeOnly = yield* makeTempDir("t3code-workspace-worktree-only-");
910+
const worktreeOnlyCheckout = yield* makeTempDir("t3code-workspace-worktree-only-wt-");
911+
const worktreeOnlyGitDir = path.join(worktreeOnly, ".git", "worktrees", "wt");
912+
yield* fileSystem.makeDirectory(worktreeOnlyGitDir, { recursive: true });
913+
yield* fileSystem.writeFileString(
914+
path.join(worktreeOnly, ".git", "config"),
915+
'[remote "origin"]\n\turl = [email protected]:pingdotgg/other.git\n',
916+
);
917+
yield* fileSystem.writeFileString(path.join(worktreeOnlyGitDir, "commondir"), "../..\n");
918+
yield* fileSystem.writeFileString(
919+
path.join(worktreeOnlyCheckout, ".git"),
920+
`gitdir: ${worktreeOnlyGitDir}\n`,
921+
);
922+
923+
// Not offered: the config is unreadable, so the repository could be bare; and
924+
// the main checkout is reached through a symlink into T3's own state.
925+
const configBaseDir = yield* fileSystem.realPath(
926+
yield* makeTempDir("t3code-scanner-base-"),
927+
);
928+
const links = yield* makeTempDir("t3code-workspace-links-");
929+
const unknown = yield* makeTempDir("t3code-workspace-unknown-");
930+
yield* fileSystem.makeDirectory(path.join(configBaseDir, "state-repo"));
931+
yield* fileSystem.symlink(path.join(configBaseDir, "state-repo"), path.join(links, "repo"));
932+
const hiddenCheckouts: Array<string> = [];
933+
for (const [main, config] of [
934+
[unknown, null],
935+
[path.join(links, "repo"), "[core]\n"],
936+
] as const) {
937+
const gitDir = path.join(main, ".git", "worktrees", "wt");
938+
yield* fileSystem.makeDirectory(gitDir, { recursive: true });
939+
if (config !== null) {
940+
yield* fileSystem.writeFileString(path.join(main, ".git", "config"), config);
941+
}
942+
yield* fileSystem.writeFileString(path.join(gitDir, "commondir"), "../..\n");
943+
const checkout = yield* makeTempDir("t3code-workspace-hidden-wt-");
944+
yield* fileSystem.writeFileString(path.join(checkout, ".git"), `gitdir: ${gitDir}\n`);
945+
hiddenCheckouts.push(checkout);
946+
}
908947

909-
for (const [index, cwd] of [repo, worktree, plain, noRemote, submodule].entries()) {
948+
const sessionDirectories = [
949+
repo,
950+
worktree,
951+
plain,
952+
noRemote,
953+
submodule,
954+
worktreeOnlyCheckout,
955+
...hiddenCheckouts,
956+
];
957+
for (const [index, cwd] of sessionDirectories.entries()) {
910958
yield* writeTranscript({
911959
filePath: path.join(claudeHomePath, "projects", `-slug-${index}`, "a.jsonl"),
912960
contents: claudeSessionLine(cwd),
913961
mtimeMs: Date.parse(`2026-01-0${index + 1}T00:00:00.000Z`),
914962
});
915963
}
916964

917-
const result = yield* runScan({ claudeHomePath, codexHomePath });
965+
const result = yield* runScan({ claudeHomePath, codexHomePath, configBaseDir });
918966

919967
expect(
920-
result.candidates.map((candidate) => ({ path: candidate.path, git: candidate.git })),
968+
result.candidates.map((candidate) => ({
969+
path: candidate.path,
970+
git: candidate.git,
971+
threadCount: candidate.threadCount,
972+
})),
921973
).toEqual([
974+
{
975+
path: worktreeOnly,
976+
git: { remoteKey: "github.com/pingdotgg/other", repository: "pingdotgg/other" },
977+
threadCount: 1,
978+
},
922979
{
923980
path: submodule,
924981
git: {
925982
remoteKey: "gitlab.com/pingdotgg/libs/vendor",
926983
repository: "pingdotgg/libs/vendor",
927984
},
985+
threadCount: 1,
928986
},
929-
{ path: noRemote, git: { remoteKey: null, repository: null } },
930-
{ path: plain, git: null },
987+
{ path: noRemote, git: { remoteKey: null, repository: null }, threadCount: 1 },
988+
{ path: plain, git: null, threadCount: 1 },
931989
{
932990
path: repo,
933991
git: { remoteKey: "github.com/pingdotgg/t3code", repository: "pingdotgg/t3code" },
992+
threadCount: 2,
934993
},
935994
]);
936995
}),

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

Lines changed: 75 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -876,6 +876,20 @@ export const make = Effect.gen(function* () {
876876
return `path:${normalizeProjectPathForComparison(realPath)}`;
877877
});
878878

879+
/** Stats a directory that may be offered as a project, or none when it is missing or excluded. */
880+
const projectDirectoryStats = Effect.fn("AgentSessionScanner.projectDirectoryStats")(function* (
881+
directory: string,
882+
) {
883+
if (isExcludedProjectPath(directory)) return Option.none<FileSystem.File.Info>();
884+
const stats = yield* statOption(directory);
885+
if (Option.isNone(stats) || stats.value.type !== "Directory") return Option.none();
886+
const realPath = yield* fileSystem
887+
.realPath(directory)
888+
.pipe(Effect.orElseSucceed(() => directory));
889+
// Symlinks can disguise excluded worktree paths; re-check their targets.
890+
return isExcludedProjectPath(realPath) ? Option.none() : stats;
891+
});
892+
879893
// Paths whose own read outlived the deadline, mapped to when they may be read
880894
// again. Without this every later scan would wait out the deadline again for
881895
// each phase that touches them.
@@ -929,7 +943,11 @@ export const make = Effect.gen(function* () {
929943
) {
930944
type GitIdentity =
931945
| { readonly _tag: "Repository"; readonly git: AgentSessionProjectGit | null }
932-
| { readonly _tag: "Worktree" }
946+
| {
947+
readonly _tag: "Worktree";
948+
readonly mainCheckout: string | null;
949+
readonly git: AgentSessionProjectGit | null;
950+
}
933951
| { readonly _tag: "NotGit" };
934952
const identities = new Map<string, GitIdentity>();
935953
const repositories: Array<{ directory: string; gitDir: string; linkedWorktree: boolean }> = [];
@@ -962,8 +980,9 @@ export const make = Effect.gen(function* () {
962980
);
963981
for (const { directory, gitDir } of linkedWorktrees) {
964982
const commonDir = (commonDirTexts.get(path.join(gitDir, "commondir")) ?? "").trim();
965-
if (commonDir.length === 0) identities.set(directory, { _tag: "Worktree" });
966-
else
983+
if (commonDir.length === 0) {
984+
identities.set(directory, { _tag: "Worktree", mainCheckout: null, git: null });
985+
} else
967986
repositories.push({
968987
directory,
969988
gitDir: path.resolve(gitDir, commonDir),
@@ -976,20 +995,25 @@ export const make = Effect.gen(function* () {
976995
);
977996
for (const { directory, gitDir, linkedWorktree } of repositories) {
978997
const configText = configTexts.get(path.join(gitDir, "config")) ?? "";
998+
const originUrl = parseOriginUrlFromGitConfig(configText);
999+
const git: AgentSessionProjectGit = {
1000+
remoteKey:
1001+
originUrl === null ? null : sourceControlDefinitions.repositoryKeyFromRemote(originUrl),
1002+
repository: repositoryPathFromRemoteUrl(originUrl),
1003+
};
9791004
// Bare repositories have no main checkout, so offer their linked worktrees independently.
9801005
if (linkedWorktree && !isBareGitConfig(configText)) {
981-
identities.set(directory, { _tag: "Worktree" });
1006+
identities.set(directory, {
1007+
_tag: "Worktree",
1008+
// Only a common dir named .git sits inside its main checkout, and an
1009+
// unreadable config can't rule out a bare repository.
1010+
mainCheckout:
1011+
configText.length > 0 && path.basename(gitDir) === ".git" ? path.dirname(gitDir) : null,
1012+
git,
1013+
});
9821014
continue;
9831015
}
984-
const originUrl = parseOriginUrlFromGitConfig(configText);
985-
identities.set(directory, {
986-
_tag: "Repository",
987-
git: {
988-
remoteKey:
989-
originUrl === null ? null : sourceControlDefinitions.repositoryKeyFromRemote(originUrl),
990-
repository: repositoryPathFromRemoteUrl(originUrl),
991-
},
992-
});
1016+
identities.set(directory, { _tag: "Repository", git });
9931017
}
9941018
return identities;
9951019
});
@@ -1701,54 +1725,67 @@ export const make = Effect.gen(function* () {
17011725
const expanded = expandHomePath(candidate.cwd.trim(), homeDir);
17021726
if (!path.isAbsolute(expanded)) continue;
17031727
const resolved = path.resolve(expanded);
1704-
if (directories.has(resolved) || isExcludedProjectPath(resolved)) continue;
1705-
const stats = yield* statOption(resolved);
1706-
if (Option.isNone(stats) || stats.value.type !== "Directory") {
1707-
directories.set(resolved, Option.none());
1708-
continue;
1709-
}
1710-
const realPath = yield* fileSystem
1711-
.realPath(resolved)
1712-
.pipe(Effect.orElseSucceed(() => resolved));
1713-
// Symlinks can disguise excluded worktree paths; re-check their targets.
1714-
directories.set(resolved, isExcludedProjectPath(realPath) ? Option.none() : stats);
1728+
if (directories.has(resolved)) continue;
1729+
directories.set(resolved, yield* projectDirectoryStats(resolved));
17151730
}
17161731
const directoryGitIdentities = yield* readGitIdentities(
17171732
Array.from(directories).flatMap(([directory, stats]) =>
17181733
Option.isSome(stats) ? [directory] : [],
17191734
),
17201735
);
17211736

1722-
const directoryKeys = new Map<string, string>();
1723-
const gitIdentities = new Map<string, AgentSessionProjectGit | null>();
1737+
const directoryProjects = new Map<
1738+
string,
1739+
{
1740+
readonly key: string;
1741+
readonly path: string;
1742+
readonly git: AgentSessionProjectGit | null;
1743+
} | null
1744+
>();
17241745

17251746
for (const candidate of raw) {
17261747
const expanded = expandHomePath(candidate.cwd.trim(), homeDir);
17271748
if (!path.isAbsolute(expanded)) continue;
17281749
const resolved = path.resolve(expanded);
17291750
const stats = directories.get(resolved);
17301751
if (stats === undefined || Option.isNone(stats)) continue;
1731-
let key = directoryKeys.get(resolved);
1732-
if (key === undefined) {
1752+
let project = directoryProjects.get(resolved);
1753+
if (project === undefined) {
17331754
const gitIdentity = directoryGitIdentities.get(resolved);
1734-
if (gitIdentity === undefined || gitIdentity._tag === "Worktree") {
1735-
key = "";
1755+
if (gitIdentity === undefined) {
1756+
project = null;
1757+
} else if (gitIdentity._tag === "Worktree") {
1758+
// A linked worktree's sessions count toward its main checkout's project.
1759+
const { mainCheckout } = gitIdentity;
1760+
const mainStats =
1761+
mainCheckout === null ? Option.none() : yield* projectDirectoryStats(mainCheckout);
1762+
project =
1763+
mainCheckout !== null && Option.isSome(mainStats)
1764+
? {
1765+
key: yield* directoryIdentity(mainCheckout, mainStats.value),
1766+
path: mainCheckout,
1767+
git: gitIdentity.git,
1768+
}
1769+
: null;
17361770
} else {
1737-
key = yield* directoryIdentity(resolved, stats.value);
1738-
gitIdentities.set(key, gitIdentity._tag === "Repository" ? gitIdentity.git : null);
1771+
project = {
1772+
key: yield* directoryIdentity(resolved, stats.value),
1773+
path: resolved,
1774+
git: gitIdentity._tag === "Repository" ? gitIdentity.git : null,
1775+
};
17391776
}
1740-
directoryKeys.set(resolved, key);
1777+
directoryProjects.set(resolved, project);
17411778
}
1742-
if (key === "") continue;
1779+
if (project === null) continue;
17431780

1744-
const existing = merged.get(key);
1781+
const existing = merged.get(project.key);
17451782
if (!existing) {
1746-
merged.set(key, {
1747-
path: resolved,
1783+
merged.set(project.key, {
1784+
path: project.path,
17481785
sources: [candidate.source],
17491786
threadCount: candidate.threadCount,
17501787
lastActiveAtMs: candidate.lastActiveAtMs,
1751-
git: gitIdentities.get(key) ?? null,
1788+
git: project.git,
17521789
});
17531790
continue;
17541791
}

‎docs/user/welcome-page.md‎

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -74,9 +74,10 @@ three conversations, already checked. Projects already in T3 Code are not checke
7474
again. Uncheck the box to start without them, or
7575
select **Review all** to pick from everything found, with "Select all" and
7676
"Select none". Linked worktrees of bare repositories
77-
can be offered as separate projects. Other linked git worktrees, Codex scratch
78-
directories under `Documents/Codex`, and anything under `Downloads` are not
79-
offered.
77+
can be offered as separate projects. Conversations in other linked git worktrees
78+
count toward their main checkout, so a repository is offered even if you only
79+
worked in its worktrees. Codex scratch directories under `Documents/Codex`, and
80+
anything under `Downloads` are not offered.
8081

8182
A large or malformed history can reach the scan limit. T3 Code keeps the
8283
projects it found and warns when projects or conversations may be missing.

0 commit comments

Comments
 (0)