From 9c82541be9588894de19008117250ec2be87d928 Mon Sep 17 00:00:00 2001 From: Raimonds Korzenevskis Date: Sun, 22 Nov 2026 17:41:02 +0200 Subject: [PATCH] perf(server): a new worktree's code index starts from its main checkout's --- .../knowledge/CodeIntelligence.test.ts | 43 +++++++++ .../knowledge/codeIntelligenceWorker.ts | 88 +++++++++++++++++-- 2 files changed, 122 insertions(+), 9 deletions(-) diff --git a/apps/server/src/project/knowledge/CodeIntelligence.test.ts b/apps/server/src/project/knowledge/CodeIntelligence.test.ts index aab525c5575db..4b7d621a00361 100644 --- a/apps/server/src/project/knowledge/CodeIntelligence.test.ts +++ b/apps/server/src/project/knowledge/CodeIntelligence.test.ts @@ -1283,6 +1283,49 @@ it.effect("indexes a worktree for its active agent and never merely because it i ), ); +it.effect("starts a linked worktree's first index from its main checkout's", () => + withFixture((fixture) => + Effect.gen(function* () { + // Git reports physical paths, and the main checkout's index is keyed by its path. + const root = NodeFS.realpathSync(fixture.root); + const linked = NodePath.join(NodeFS.realpathSync(fixture.worktree), "linked"); + const git = (...args: ReadonlyArray) => + NodeChildProcess.execFileSync( + "git", + ["-c", "user.email=t3@example.com", "-c", "user.name=T3", ...args], + { cwd: root, encoding: "utf8" }, + ); + git("init", "--quiet"); + git("add", "--all"); + git("commit", "--quiet", "-m", "fixture"); + git("worktree", "add", "--quiet", "--detach", linked); + yield* Effect.promise(() => + NodeFSP.writeFile( + NodePath.join(linked, "src/work.ts"), + "export function workOnly() { return 2; }\n", + ), + ); + const worktree = { threadId: worktreeThreadId, path: linked }; + // The worktree lists only its new file, so `greet` can only come from the main checkout's. + const { run, refresh, indexRoot } = yield* checkoutService( + root, + (cwd) => (cwd === linked ? ["src/work.ts"] : ["src/defs.ts"]), + { worktree, repository: true }, + ); + assert.deepEqual(yield* refresh(), { _tag: "Published", generation: 1 }); + + assert.deepEqual(yield* refresh(worktreeThreadId), { _tag: "Published", generation: 1 }); + assert.equal(yield* indexedSymbolCount(indexRoot(linked), "greet"), 1); + const own = yield* run({ operation: "symbols", name: "workOnly" }, worktreeThreadId); + assert.ok(own.nodes.some((node) => node.path === "src/work.ts")); + // The main checkout's own index is untouched and still serves. + const main = yield* run({ operation: "freshness" }); + assert.equal(main.index?.generation, 1); + assert.equal(main.index?.state, "ready"); + }), + ), +); + it.effect("leaves a worktree index stale until its agent acts again", () => withFixture((fixture) => Effect.gen(function* () { diff --git a/apps/server/src/project/knowledge/codeIntelligenceWorker.ts b/apps/server/src/project/knowledge/codeIntelligenceWorker.ts index 9d6733b07002c..4347dc6b8b6d7 100644 --- a/apps/server/src/project/knowledge/codeIntelligenceWorker.ts +++ b/apps/server/src/project/knowledge/codeIntelligenceWorker.ts @@ -36,8 +36,11 @@ import type { import { CHECKOUT_CONFIG_ENTRIES, clearCheckoutSources, + generationDirectory, + indexRootFor, linkCheckoutSources, } from "./codeIndexLayout.ts"; +import { pinPublishedGeneration } from "./codeIndexLocks.ts"; const CATALOG_REVISION = "@colbymchenry/codegraph@1.6.2"; // Programming languages outside the SDK's catalog, which it reports as unknown like any asset. @@ -318,6 +321,53 @@ function headCommit(checkoutPath: string): string | null { } } +/** The repository's main checkout when `checkoutPath` is a linked worktree of one. */ +function mainCheckoutOf(checkoutPath: string): string | null { + try { + const [gitDir, commonDir] = NodeChildProcess.execFileSync( + "git", + ["rev-parse", "--path-format=absolute", "--git-dir", "--git-common-dir"], + { cwd: checkoutPath, encoding: "utf8", timeout: 2_000, stdio: ["ignore", "pipe", "ignore"] }, + ) + .trim() + .split("\n"); + // A bare repository's worktrees have no main checkout to share an index with. + if (!gitDir || !commonDir || gitDir === commonDir || NodePath.basename(commonDir) !== ".git") + return null; + return NodePath.dirname(commonDir); + } catch { + return null; + } +} + +interface SeedGeneration { + readonly indexRoot: string; + readonly directory: string; + readonly release: () => void; +} + +/** + * The main checkout's published generation, pinned against retirement, for a linked worktree + * that has none yet. Its paths are checkout-relative, so the worktree only re-parses what differs. + */ +function pinMainCheckoutGeneration(input: CodeIndexBuildInput): SeedGeneration | null { + const main = mainCheckoutOf(input.checkoutPath); + if (main === null) return null; + const indexRoot = indexRootFor(NodePath.dirname(input.indexRoot), main); + if (indexRoot === input.indexRoot) return null; + let pin: ReturnType; + try { + pin = pinPublishedGeneration(indexRoot); + } catch { + return null; + } + if (pin.generation === null) { + pin.release(); + return null; + } + return { indexRoot, directory: generationDirectory(pin.generation), release: pin.release }; +} + // Mirrors the SDK's file limit: it never parses a larger file and stores a hash of a size // stamp in place of its content. const MAX_SOURCE_FILE_SIZE_BYTES = 1024 * 1024; @@ -671,9 +721,10 @@ function hasUnauthorizedFiles(checkoutPath: string, graph: CodeGraphInstance): b } /** - * Builds the generation `input.to` from a copy of the published one, or from scratch, and reports - * whether it differs from the published one. An unchanged checkout is detected before anything is - * copied, and then `input.to` is never created. + * Builds the generation `input.to` from a copy of the published one, from a copy of the main + * checkout's when a linked worktree has none, or from scratch, and reports whether it differs from + * the published one. An unchanged checkout is detected before anything is copied, and then + * `input.to` is never created. */ export async function buildIndexInWorker( input: CodeIndexBuildInput, @@ -721,9 +772,10 @@ export async function buildIndexInWorker( const target = NodePath.join(input.indexRoot, input.to); // Exclusive: a leftover entry, link or not, fails the build instead of being written through. NodeFS.mkdirSync(target); - if (from !== null) { + let copied = false; + const copyDatabase = (sourceRoot: string, sourceDirectory: string) => { for (const name of INDEX_DATABASE_FILES) { - const source = NodePath.join(input.indexRoot, from, name); + const source = NodePath.join(sourceRoot, sourceDirectory, name); if (NodeFS.existsSync(source)) { NodeFS.copyFileSync( source, @@ -731,14 +783,32 @@ export async function buildIndexInWorker( // FICLONE falls back to a normal copy where reflinks are unavailable. NodeFS.constants.COPYFILE_EXCL | NodeFS.constants.COPYFILE_FICLONE, ); + copied = true; + } + } + }; + if (from !== null) { + copyDatabase(input.indexRoot, from); + } else { + const seed = pinMainCheckoutGeneration(input); + if (seed !== null) { + try { + const meta = readIndexMeta(seed.indexRoot, seed.directory); + // The same rules that let a successor inherit its predecessor's files. + if ( + meta.configurationDigest === configurationDigest && + (!isListingTruncated(input) || meta.ranked) + ) + copyDatabase(seed.indexRoot, seed.directory); + } finally { + seed.release(); } } } useIndexDirectory(input.to); - let graph = - from !== null - ? await CodeGraph.open(input.indexRoot, { sync: false }) - : await CodeGraph.init(input.indexRoot, { index: false }); + let graph = copied + ? await CodeGraph.open(input.indexRoot, { sync: false }) + : await CodeGraph.init(input.indexRoot, { index: false }); try { if (hasUnauthorizedFiles(input.checkoutPath, graph)) { graph.close();