Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
43 changes: 43 additions & 0 deletions apps/server/src/project/knowledge/CodeIntelligence.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<string>) =>
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* () {
Expand Down
88 changes: 79 additions & 9 deletions apps/server/src/project/knowledge/codeIntelligenceWorker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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<typeof pinPublishedGeneration>;
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;
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -721,24 +772,43 @@ 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,
NodePath.join(target, name),
// 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();
Expand Down
Loading