Skip to content

A desktop coding agent with free access to frontier models.

TypeScript

fix(capture): archive the enclosing repo's .git when a session starts in a repo subfolder

ogsamrated839b9da68d
diff --git a/apps/server/src/session-archive/enclosing.ts b/apps/server/src/session-archive/enclosing.ts
new file mode 100644
index 00000000..20fdbda1
--- /dev/null
+++ b/apps/server/src/session-archive/enclosing.ts
@@ -0,0 +1,188 @@
+/**
+ * Capture v2: the `.git` of the repository a session folder sits inside.
+ *
+ * A session started in a subfolder of a repository (`repo/packages/app`)
+ * archived only that subfolder, so its archive had no git at all. The
+ * enclosing repository's git directory is archived byte for byte under
+ * `__enclosing_repo__/.git/` (credentials removed from the archived config,
+ * like every other repository's), never any other file of the repository
+ * outside the session folder. `state.json` says where it belongs: the
+ * repository's `repos` item (`position: "above"`, `path: "../.."`) gets
+ * `git_dir: "__enclosing_repo__/.git"`, `archived: "byte_exact"` and
+ * `session_subdir` (the session folder inside the work tree, `packages/app`).
+ *
+ * Replay: put the folder at `<session_subdir>` of an empty directory and move
+ * `__enclosing_repo__/.git` to that directory's top.
+ *
+ * Left out (metadata only, listed in `excluded`): a repository whose `.git`
+ * is a gitfile (a linked worktree or submodule: its objects live elsewhere),
+ * one at or above the home directory (git's discovery stops there), one in
+ * the app's own state, and a `.git` over MAX_ENCLOSING_GIT_BYTES. Identical
+ * in the CLI and the desktop app.
+ */
+import { lstat, readdir } from "node:fs/promises";
+import { isAbsolute, join, relative, resolve, sep } from "node:path";
+
+import type { ExcludedList } from "./capture-v2.js";
+import {
+ ENCLOSING_REPO_ROOT_NAME,
+ emptyExcludedCounts,
+ emptyScanMetrics,
+ scanArchiveTree,
+ scrubConfigEntry,
+ type ArchiveHashCache,
+ type ExcludedCounts,
+ type ScanOptions,
+ type ScannedEntry,
+} from "./manifest.js";
+import { isGitConfigPath } from "./git-scrub.js";
+import { runRepoGit, type RepoState } from "./repos.js";
+
+export { ENCLOSING_REPO_ROOT_NAME };
+/** The archive path of the enclosing repository's git directory. */
+export const ENCLOSING_GIT_DIR = `${ENCLOSING_REPO_ROOT_NAME}/.git`;
+/** A larger enclosing `.git` is left out (listed as too_large). */
+export const MAX_ENCLOSING_GIT_BYTES = 1024 * 1024 * 1024;
+
+export type EnclosingRepo = {
+ /** The work tree's top, absolute. */
+ top: string;
+ /** Its git directory (`<top>/.git`), absolute. */
+ gitDir: string;
+ /** The session folder inside the work tree (`packages/app`), portable. */
+ sessionSubdir: string;
+};
+
+function inside(parent: string, child: string): boolean {
+ const rel = relative(parent, child);
+ return rel === "" || (rel !== ".." && !rel.startsWith(`..${sep}`) && !isAbsolute(rel));
+}
+
+/** The repository whose work tree holds `root` from above (not `root` itself), or null. */
+export async function findEnclosingRepo(root: string): Promise<EnclosingRepo | null> {
+ const run = await runRepoGit(root, ["rev-parse", "--show-toplevel", "--absolute-git-dir"], { maxBytes: 64 * 1024 });
+ if (!run.ok) return null;
+ const [top, gitDir] = run.stdout.toString("utf8").split(/\r?\n/);
+ if (!top || !gitDir) return null;
+ const absRoot = resolve(root);
+ const rel = relative(resolve(top), absRoot);
+ if (rel === "" || rel === ".." || rel.startsWith(`..${sep}`) || isAbsolute(rel)) return null;
+ // Only a real `<top>/.git` folder: a gitfile's objects live elsewhere.
+ if (resolve(gitDir) !== resolve(top, ".git")) return null;
+ try {
+ const stats = await lstat(gitDir);
+ if (!stats.isDirectory()) return null;
+ } catch {
+ return null;
+ }
+ return { top: resolve(top), gitDir: resolve(gitDir), sessionSubdir: rel.split(sep).join("/") };
+}
+
+/** Whether the bytes under `dir` stay within `cap` (lstat only, stops as soon as they do not). */
+async function withinBytes(dir: string, cap: number): Promise<boolean> {
+ let total = 0;
+ const stack = [dir];
+ while (stack.length > 0) {
+ const current = stack.pop()!;
+ let names;
+ try {
+ names = await readdir(current, { withFileTypes: true });
+ } catch {
+ continue;
+ }
+ for (const entry of names) {
+ const path = join(current, entry.name);
+ if (entry.isDirectory()) {
+ stack.push(path);
+ continue;
+ }
+ if (!entry.isFile()) continue;
+ try {
+ total += (await lstat(path)).size;
+ } catch {
+ continue;
+ }
+ if (total > cap) return false;
+ }
+ }
+ return true;
+}
+
+/** Archive entries in the shape the archiver merges (like an outside scan), and the repository they came from. */
+export type EnclosingScan = { entries: ScannedEntry[]; gone: Set<string>; excluded: ExcludedCounts; repo: EnclosingRepo | null };
+
+/**
+ * The enclosing repository's `.git` as archive entries under
+ * ENCLOSING_GIT_DIR (read from their own absolute paths at pack time), or
+ * none. Never throws.
+ */
+export async function scanEnclosingGit(
+ root: string,
+ options: { appDirs?: readonly string[]; home?: string | null; hashCache?: ArchiveHashCache; excludedList?: ExcludedList; maxBytes?: number; signal?: AbortSignal } = {},
+): Promise<EnclosingScan> {
+ const empty: EnclosingScan = { entries: [], gone: new Set(), excluded: emptyExcludedCounts(), repo: null };
+ let repo: EnclosingRepo | null;
+ try {
+ repo = await findEnclosingRepo(root);
+ } catch {
+ return empty;
+ }
+ if (!repo) return empty;
+ const home = options.home ?? null;
+ if (home && inside(repo.top, resolve(home))) return { ...empty, repo: null };
+ if ((options.appDirs ?? []).some((dir) => inside(resolve(dir), repo.gitDir))) return { ...empty, repo: null };
+ if (!(await withinBytes(repo.gitDir, options.maxBytes ?? MAX_ENCLOSING_GIT_BYTES))) {
+ options.excludedList?.add({ path: ENCLOSING_GIT_DIR, type: "dir", reason: "too_large" });
+ return { ...empty, repo: null };
+ }
+ try {
+ const metrics = emptyScanMetrics();
+ // No ignore rules inside a git directory (the CLI's scan would otherwise ask git about each path).
+ const scanOptions = {
+ includeIgnored: true,
+ ...(options.hashCache ? { hashCache: options.hashCache } : {}),
+ ...(options.signal ? { signal: options.signal } : {}),
+ metrics,
+ } as ScanOptions;
+ const scan = await scanArchiveTree(repo.gitDir, scanOptions);
+ const entries: ScannedEntry[] = [{
+ path: ENCLOSING_REPO_ROOT_NAME, type: "dir", size: 0, sha256: null, mode: 0o755, mtime: 0, statKey: "0:0:0:0", dev: 0,
+ }];
+ const gitStats = await lstat(repo.gitDir, { bigint: true });
+ entries.push({ path: ENCLOSING_GIT_DIR, type: "dir", size: 0, sha256: null, mode: Number(gitStats.mode & 0o7777n), mtime: Number(gitStats.mtimeNs / 1_000_000_000n), statKey: "0:0:0:0", dev: Number(gitStats.dev) });
+ for (const entry of scan.entries) {
+ // A symlink inside .git is never followed nor archived (pack reads files by absolute path only).
+ if (entry.type === "symlink") {
+ options.excludedList?.add({ path: `${ENCLOSING_GIT_DIR}/${entry.path}`, type: "file", reason: "special" });
+ continue;
+ }
+ const source = join(repo.gitDir, ...entry.path.split("/"));
+ const path = `${ENCLOSING_GIT_DIR}/${entry.path}`;
+ if (entry.type === "dir") {
+ entries.push({ ...entry, path });
+ continue;
+ }
+ const moved: ScannedEntry = { ...entry, path, source };
+ if (isGitConfigPath(path)) {
+ if (moved.size > 1024 * 1024) {
+ options.excludedList?.add({ path, type: "file", reason: "too_large", size: moved.size });
+ continue;
+ }
+ delete moved.source;
+ if (!(await scrubConfigEntry(source, moved, metrics))) continue;
+ }
+ entries.push(moved);
+ }
+ return { entries, gone: new Set(), excluded: scan.excluded, repo };
+ } catch {
+ return empty;
+ }
+}
+
+/** The `repos` items of state.json with the enclosing repository marked as archived (see the module comment). */
+export function markEnclosingRepo(repos: RepoState[], enclosing: EnclosingRepo | null): RepoState[] {
+ if (!enclosing) return repos;
+ return repos.map((repo) => repo.position === "above"
+ ? { ...repo, git_dir: ENCLOSING_GIT_DIR, archived: "byte_exact" as const, session_subdir: enclosing.sessionSubdir }
+ : repo);
+}
diff --git a/apps/server/src/session-archive/index.ts b/apps/server/src/session-archive/index.ts
index 80f35b91..ab5c5545 100644
--- a/apps/server/src/session-archive/index.ts
+++ b/apps/server/src/session-archive/index.ts
@@ -67,6 +67,7 @@ import {
type StateKind,
} from "./capture-v2.js";
import { discoverRepos } from "./repos.js";
+import { ENCLOSING_REPO_ROOT_NAME, markEnclosingRepo, scanEnclosingGit, type EnclosingRepo, type EnclosingScan } from "./enclosing.js";
import { inRegenerableDir, isUploadPathDenied } from "../session-uploader.js";
/** Capture v2 (#14): a touched file the folder scan did not hold larger than this is left out, with its path, size and hash. */
@@ -116,9 +117,11 @@ export const POLICY_TTL_MS = 5 * 60_000;
/** The hash cache of a root's outside files is kept under the root's key with this suffix. */
const OUTSIDE_CACHE_SUFFIX = "outside";
+/** The hash cache of the enclosing repository's .git (enclosing.ts) is kept under the root's key with this suffix. */
+const ENCLOSING_CACHE_SUFFIX = "enclosing";
/** Adds an outside scan (outside.ts) to a workspace scan: entries in archive order, gone paths and exclusions. */
-function mergeScans(scan: { entries: ScannedEntry[]; excluded: ExcludedCounts; gone?: Set<string> }, outside: TouchedScanResult | null): void {
+function mergeScans(scan: { entries: ScannedEntry[]; excluded: ExcludedCounts; gone?: Set<string> }, outside: Pick<TouchedScanResult, "entries" | "gone" | "excluded"> | null): void {
if (!outside) return;
if (outside.entries.length > 0) scan.entries = [...scan.entries, ...outside.entries].sort((left, right) => compareArchivePaths(left.path, right.path));
for (const path of outside.gone) scan.gone?.add(path);
@@ -357,7 +360,7 @@ function repoMarkers(entries: readonly ArchiveEntry[]): Array<{ path: string; gi
if (entry.type === "symlink") continue;
const parts = entry.path.split("/");
if (parts[parts.length - 1] !== ".git" || parts.slice(0, -1).includes(".git")) continue;
- if (parts[0] === ATTACHMENTS_ROOT_NAME || isOutsideArchivePath(entry.path)) continue;
+ if (parts[0] === ATTACHMENTS_ROOT_NAME || parts[0] === ENCLOSING_REPO_ROOT_NAME || isOutsideArchivePath(entry.path)) continue;
markers.push({ path: entry.path, gitfile: entry.type === "file" });
}
return markers;
@@ -1169,6 +1172,7 @@ export class SessionArchiver {
/** The chain's entry list after this archive: the next baseline. */
let next: readonly ArchiveEntry[];
let git: ArchiveGit | null = null;
+ let enclosing: EnclosingRepo | null = null;
if (touched) {
const baseline = kind === "delta" ? await this.readBaseline(state) : [];
if (!baseline) {
@@ -1214,6 +1218,12 @@ export class SessionArchiver {
// The files outside the workspace the session touched (outside.ts).
mergeScans(scan, await this.scanOutside(state, baseline, signal));
if (v2) mergeScans(scan, await this.attachmentEntries(sessionId));
+ // Capture v2: the .git of the repository above a session folder (enclosing.ts).
+ if (v2) {
+ const found = await this.scanEnclosing(state.root, excludedList, signal);
+ enclosing = found.repo;
+ mergeScans(scan, found);
+ }
fullScan = scan.entries;
files = scan.entries;
excluded = scan.excluded;
@@ -1249,7 +1259,7 @@ export class SessionArchiver {
...(options.reason ? { reason: options.reason } : {}),
...(kind === "base" ? { startCapture: prescan?.capture ?? "partial", startCaptureMs: prescan?.result.ms ?? 0 } : {}),
// Every repository of the session (repos.ts), whatever the chain's marker.
- repos: await discoverRepos(state.root, repoMarkers(touched ? [] : fullScan)),
+ repos: markEnclosingRepo(await discoverRepos(state.root, repoMarkers(touched ? [] : fullScan)), enclosing),
excluded: excludedList,
scrubbed: listed.filter((entry) => entry.scrubbed).map((entry) => entry.path),
attachments,
@@ -1521,6 +1531,21 @@ export class SessionArchiver {
return scan;
}
+ /** Capture v2: the enclosing repository's .git (enclosing.ts), hashed with its own cache. */
+ private async scanEnclosing(root: string, excludedList: ExcludedList | undefined, signal?: AbortSignal): Promise<EnclosingScan> {
+ const cacheKey = stateKey(`${root}\0${ENCLOSING_CACHE_SUFFIX}`);
+ const cache = await this.loadHashCache(cacheKey);
+ let home: string | null = null;
+ try {
+ home = (await import("node:os")).homedir() || null;
+ } catch {
+ home = null;
+ }
+ const scan = await scanEnclosingGit(root, { appDirs: this.appDirs, home, hashCache: cache, ...(excludedList ? { excludedList } : {}), ...(signal ? { signal } : {}) });
+ if (cache.changed) await this.saveHashCache(cacheKey, cache).catch(() => undefined);
+ return scan;
+ }
+
/** The entry list after the chain's last archive; null when it cannot be read (the chain cannot go on). */
private async readBaseline(state: SessionState): Promise<ArchiveEntry[] | null> {
return state.baseline ? parseBaselineText(await readText(join(this.dirs.baselines, state.baseline))) : null;
diff --git a/apps/server/src/session-archive/manifest.ts b/apps/server/src/session-archive/manifest.ts
index 510307d4..19d97799 100644
--- a/apps/server/src/session-archive/manifest.ts
+++ b/apps/server/src/session-archive/manifest.ts
@@ -26,6 +26,8 @@ export const RESERVED_ROOT_NAME = "__omnirush__";
export const OUTSIDE_ROOT_NAME = "__outside__";
/** Capture v2: prompt attachments (attachments.ts); a workspace entry of that name is reserved too. */
export { ATTACHMENTS_ROOT_NAME };
+/** Capture v2: the `.git` of the repository above the session folder (enclosing.ts); a workspace entry of that name is reserved too. */
+export const ENCLOSING_REPO_ROOT_NAME = "__enclosing_repo__";
/** A git config file larger than this is left out (`too_large`): it is never read whole to scrub it, and never archived raw. */
const MAX_SCRUBBED_CONFIG_BYTES = 1024 * 1024;
export const STAT_CONCURRENCY = 64;
@@ -669,7 +671,7 @@ export async function scanArchiveTree(root: string, options: ScanOptions = {}):
for (const bytes of raw) {
const name = decodeName(bytes);
if (name === null) excluded.non_utf8 += 1;
- else if (!dir.rel && (name === RESERVED_ROOT_NAME || name === OUTSIDE_ROOT_NAME || name === ATTACHMENTS_ROOT_NAME)) {
+ else if (!dir.rel && (name === RESERVED_ROOT_NAME || name === OUTSIDE_ROOT_NAME || name === ATTACHMENTS_ROOT_NAME || name === ENCLOSING_REPO_ROOT_NAME)) {
excluded.reserved += 1;
note(name, "reserved", "dir");
}
diff --git a/apps/server/src/session-archive/repos.ts b/apps/server/src/session-archive/repos.ts
index 037617e9..abee0d42 100644
--- a/apps/server/src/session-archive/repos.ts
+++ b/apps/server/src/session-archive/repos.ts
@@ -7,8 +7,8 @@
* the archive scan;
* - a repository whose work tree starts above the root (`"above"`): the
* root is a folder inside it; its relative position is recorded
- * (`path: "../.."`), its `.git` is never archived (it is outside the
- * project folder), only this metadata.
+ * (`path: "../.."`); its `.git` alone is archived under
+ * `__enclosing_repo__/.git` (enclosing.ts), never its other files.
*
* Each gets HEAD, branch (also on an unborn branch: a repository without
* commits), the remote without userinfo, the tracked-file count, the dirty
@@ -58,6 +58,8 @@ export type RepoState = {
staged_files: string[];
stash_count: number;
archived: "byte_exact" | "metadata_only";
+ /** An enclosing repository (`position: "above"`): the session folder inside its work tree (its `.git` is archived under `__enclosing_repo__/.git`, enclosing.ts). */
+ session_subdir?: string;
safe_directory_override?: true;
error: RepoError | null;
};
@@ -295,6 +297,8 @@ export async function readRepoState(root: string, located: { top: string; gitDir
staged_files: stagedListed.kept,
stash_count: stash.ok ? Number.parseInt(stash.stdout.toString("utf8").trim(), 10) || 0 : 0,
archived: position !== "above" && insideRoot ? "byte_exact" : "metadata_only",
+ // The session folder inside an enclosing repository's work tree (`packages/app`).
+ ...(position === "above" ? { session_subdir: portable(relative(realpathOr(top), realpathOr(root))) } : {}),
...(safeDirectory ? { safe_directory_override: true as const } : {}),
error: failed ? errorOf(failed) : null,
};
diff --git a/apps/server/src/session-archive/touched.ts b/apps/server/src/session-archive/touched.ts
index 69b8f284..71ea746a 100644
--- a/apps/server/src/session-archive/touched.ts
+++ b/apps/server/src/session-archive/touched.ts
@@ -21,6 +21,7 @@ import {
HASH_READ_BYTES,
OPEN_ENTRY_FLAGS,
OUTSIDE_ROOT_NAME,
+ ENCLOSING_REPO_ROOT_NAME,
RESERVED_ROOT_NAME,
STAT_CONCURRENCY,
compareArchivePaths,
@@ -231,7 +232,7 @@ export async function scanTouchedFiles(root: string, paths: Iterable<string>, op
excluded.non_utf8 += 1;
return;
}
- if (parts[0] === RESERVED_ROOT_NAME || parts[0] === OUTSIDE_ROOT_NAME) {
+ if (parts[0] === RESERVED_ROOT_NAME || parts[0] === OUTSIDE_ROOT_NAME || parts[0] === ENCLOSING_REPO_ROOT_NAME) {
excluded.reserved += 1;
return;
}