TypeScript
fix(server): onboarding offers worktrees of a bare repository (#16688)
Chris Allenacf5f3bde515
diff --git a/apps/server/src/project/AgentSessionScanner.test.ts b/apps/server/src/project/AgentSessionScanner.test.tsindex aadee3853af..743d9a008d3 100644--- a/apps/server/src/project/AgentSessionScanner.test.ts+++ b/apps/server/src/project/AgentSessionScanner.test.ts@@ -978,9 +978,12 @@ it.layer(NodeServices.layer)("AgentSessionScanner", (it) => {path.join(repo, ".git", "config"),'[core]\n\tbare = false\n[remote "origin"]\n\turl = [email protected]:pingdotgg/t3code.git\n\tfetch = +refs/heads/*:refs/remotes/origin/*\n',);+ const worktreeGitDir = path.join(repo, ".git", "worktrees", "wt");+ yield* fileSystem.makeDirectory(worktreeGitDir, { recursive: true });+ yield* fileSystem.writeFileString(path.join(worktreeGitDir, "commondir"), "../..\n");yield* fileSystem.writeFileString(path.join(worktree, ".git"),- `gitdir: ${path.join(repo, ".git", "worktrees", "wt")}\n`,+ `gitdir: ${worktreeGitDir}\n`,);yield* fileSystem.makeDirectory(path.join(noRemote, ".git"));yield* fileSystem.writeFileString(path.join(noRemote, ".git", "config"), "[core]\n");@@ -1022,6 +1025,75 @@ it.layer(NodeServices.layer)("AgentSessionScanner", (it) => {}),);+ it.effect(+ "offers bare-repository worktrees separately and imports only their own sessions",+ () =>+ Effect.gen(function* () {+ const path = yield* Path.Path;+ const fileSystem = yield* FileSystem.FileSystem;+ const nowMs = Date.parse("2026-08-24T12:00:00.000Z");+ yield* TestClock.setTime(nowMs);+ const claudeHomePath = yield* makeTempDir("t3code-claude-home-");+ const codexHomePath = yield* makeTempDir("t3code-codex-home-");+ const configBaseDir = yield* makeTempDir("t3code-scanner-base-");+ const bare = yield* makeTempDir("t3code-workspace-bare-");+ const bareGitDir = path.join(bare, ".git");+ yield* fileSystem.makeDirectory(bareGitDir);+ yield* fileSystem.writeFileString(+ path.join(bareGitDir, "config"),+ '[core] bare = "true" ; bare repository\n[remote "origin"]\nurl = [email protected]:pingdotgg/t3code.git\n',+ );+ const dev = path.join(bare, "dev");+ const prod = path.join(bare, "prod");+ const managed = path.join(configBaseDir, "worktrees", "t3code", "sandbox");+ for (const [index, cwd] of [bare, dev, prod, managed].entries()) {+ if (cwd !== bare) {+ const gitDir = path.join(bareGitDir, "worktrees", path.basename(cwd));+ yield* fileSystem.makeDirectory(gitDir, { recursive: true });+ yield* fileSystem.writeFileString(path.join(gitDir, "commondir"), "../..\n");+ yield* fileSystem.makeDirectory(cwd, { recursive: true });+ yield* fileSystem.writeFileString(path.join(cwd, ".git"), `gitdir: ${gitDir}\n`);+ }+ yield* writeTranscript({+ filePath: path.join(+ codexHomePath,+ "sessions",+ "2026",+ "08",+ "24",+ `rollout-${index}.jsonl`,+ ),+ contents: [+ encodeTranscriptRecord({+ type: "session_meta",+ payload: { id: `session-${index}`, cwd },+ }),+ encodeTranscriptRecord({+ type: "event_msg",+ payload: { type: "user_message", message: `Work in ${cwd}` },+ }),+ ].join("\n"),+ mtimeMs: nowMs - index * 1_000,+ });+ }++ const input = { claudeHomePath, codexHomePath, configBaseDir };+ const result = yield* runScan(input);+ const git = { remoteKey: "github.com/pingdotgg/t3code", repository: "pingdotgg/t3code" };+ expect(+ result.candidates.map((candidate) => ({ path: candidate.path, git: candidate.git })),+ ).toEqual([+ { path: bare, git },+ { path: dev, git },+ { path: prod, git },+ ]);+ const threads = yield* runRecentThreads({ ...input, workspaceRoot: dev });+ expect(threads.map((thread) => thread.providerSessionId)).toEqual(["session-1"]);+ const managedThreads = yield* runRecentThreads({ ...input, workspaceRoot: managed });+ expect(managedThreads).toEqual([]);+ }),+ );+it.effect("excludes sandboxes under the configured worktrees dir without .t3 in the path", () =>Effect.gen(function* () {const path = yield* Path.Path;diff --git a/apps/server/src/project/AgentSessionScanner.ts b/apps/server/src/project/AgentSessionScanner.tsindex 037450345aa..e516ce0e53b 100644--- a/apps/server/src/project/AgentSessionScanner.ts+++ b/apps/server/src/project/AgentSessionScanner.ts@@ -26,6 +26,7 @@ import * as Stream from "effect/Stream";import {normalizeGitRemoteUrl,+ isBareGitConfig,parseGitHubRepositoryNameWithOwnerFromRemoteUrl,parseOriginUrlFromGitConfig,} from "@t3tools/shared/git";@@ -687,11 +688,6 @@ export const make = Effect.gen(function* () {return `path:${normalizeProjectPathForComparison(realPath)}`;});- /**- * When it points into a `worktrees/` directory the checkout is a linked worktree, which- * onboarding skips because its history belongs to the main checkout. Submodules use the same- * pointer shape but live under `modules/`, and are offered like any other repository.- */const readGitIdentity = Effect.fn("AgentSessionScanner.readGitIdentity")(function* (directory: string,): Effect.fn.Return<@@ -710,11 +706,21 @@ export const make = Effect.gen(function* () {const target = /^gitdir:\s*(.+)$/m.exec(pointer)?.[1]?.trim();if (target === undefined || target.length === 0) return { _tag: "NotGit" } as const;gitDir = path.resolve(directory, target);- if (/[\\/]worktrees[\\/][^\\/]+[\\/]?$/.test(gitDir)) return { _tag: "Worktree" } as const;+ }+ const linkedWorktree =+ gitStats.value.type !== "Directory" && /[\\/]worktrees[\\/][^\\/]+[\\/]?$/.test(gitDir);+ if (linkedWorktree) {+ const commonDir = yield* fileSystem+ .readFileString(path.join(gitDir, "commondir"))+ .pipe(Effect.orElseSucceed(() => ""));+ if (commonDir.trim().length === 0) return { _tag: "Worktree" } as const;+ gitDir = path.resolve(gitDir, commonDir.trim());}const configText = yield* fileSystem.readFileString(path.join(gitDir, "config")).pipe(Effect.orElseSucceed(() => ""));+ // Bare repositories have no main checkout, so offer their linked worktrees independently.+ if (linkedWorktree && !isBareGitConfig(configText)) return { _tag: "Worktree" } as const;const originUrl = parseOriginUrlFromGitConfig(configText);return {_tag: "Repository",diff --git a/docs/user/welcome-wizard.md b/docs/user/welcome-wizard.mdindex 41e70a8dccd..5747d72606e 100644--- a/docs/user/welcome-wizard.md+++ b/docs/user/welcome-wizard.md@@ -66,7 +66,8 @@ one group. Directories that are not git repositories sit under "Other folders".The default selection includes git repositories active within the last 30 dayswith at least three conversations. Use the checkboxes, or "Select all" and-"Select none", to change the selection. Linked git worktrees, Codex scratch+"Select none", to change the selection. Linked worktrees of bare repositories+can be offered as separate projects. Other linked git worktrees, Codex scratchdirectories under `Documents/Codex`, and anything under `Downloads` are notoffered.diff --git a/packages/shared/src/git.ts b/packages/shared/src/git.tsindex 9ee615bf2ef..32b7fbf8858 100644--- a/packages/shared/src/git.ts+++ b/packages/shared/src/git.ts@@ -196,6 +196,26 @@ function parseGitConfigValue(raw: string): string {return out.trim();}+export function isBareGitConfig(configText: string): boolean {+ let inCore = false;+ let bare = false;+ for (const rawLine of configText.split(/\r?\n/)) {+ let line = rawLine.trim();+ // Git accepts the first setting on the section header's own line.+ const header = /^\[\s*([^\]]*?)\s*\](.*)$/.exec(line);+ if (header !== null) {+ inCore = header[1]!.toLowerCase() === "core";+ line = header[2]!.trim();+ }+ if (!inCore) continue;+ const match = /^bare\s*(?:=(.*)|[#;].*)?$/i.exec(line);+ if (match === null) continue;+ const value = match[1] === undefined ? "true" : parseGitConfigValue(match[1]).toLowerCase();+ bare = value === "true" || value === "yes" || value === "on" || value === "1";+ }+ return bare;+}+export function parseOriginUrlFromGitConfig(configText: string): string | null {let section: string | null = null;let originUrl: string | null = null;