Skip to content
Merged
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
8 changes: 4 additions & 4 deletions src/review/rag-index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -315,15 +315,15 @@ export async function indexRepo(
let skipped = 0;
let capped = false;
for (const entry of tree) {
if (stored >= MAX_CHUNKS_PER_REPO) {
capped = true;
break;
}
const known = knownChunks.get(entry.path);
if (entry.sha && known?.blobSha && known.blobSha === entry.sha) {
skipped += 1;
continue; // unchanged since the last full index — skip the fetch/chunk/embed entirely
}
if (stored >= MAX_CHUNKS_PER_REPO && (!known || known.count <= 0)) {
capped = true;
break;
}
const text = await fetchFileText(env, repoFullName, entry.path, ref, token, admissionKey);
if (text === null) continue;
const chunks = chunkFile(entry.path, text, namespace);
Expand Down
38 changes: 38 additions & 0 deletions test/unit/rag-index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -426,6 +426,44 @@ describe("indexRepo: embedding cache (#4365) — skip unchanged files by git blo
expect(result.files).toBe(1); // only one of the two new files fit under the cap
expect(await countChunks(env, PROJECT, "gittensory")).toBe(MAX_CHUNKS_PER_REPO); // never exceeds the cap
});

it("refreshes changed files in a full reindex even when the repository is already capped", async () => {
const { env, ai } = indexEnv();
const ns = ragNamespace(PROJECT, "gittensory");
await env.DB.batch(
Array.from({ length: MAX_CHUNKS_PER_REPO }, (_, i) => {
const path = i === 0 ? "src/changed.ts" : `src/existing${i}.ts`;
return env.DB.prepare("INSERT INTO repo_chunks (id, project, repo, path, chunk_index, kind, text, blob_sha) VALUES (?,?,?,?,?,?,?,?)").bind(
`${ns}|${path}::0`,
PROJECT,
"gittensory",
path,
0,
"code",
i === 0 ? "old changed file" : "old",
i === 0 ? "sha-old" : `sha-existing-${i}`,
);
}),
);
stubGithub({
tree: [
{ path: "src/changed.ts", size: 30, sha: "sha-new" },
...Array.from({ length: MAX_CHUNKS_PER_REPO - 1 }, (_, i) => ({ path: `src/existing${i + 1}.ts`, size: 10, sha: `sha-existing-${i + 1}` })),
{ path: "src/new.ts", size: 10, sha: "sha-new-file" },
],
files: { "src/changed.ts": "export const changed = true;\n", "src/new.ts": "export const n = 1;\n" },
});

const result = await indexRepo(env, PROJECT, REPO);

expect(result).toMatchObject({ indexed: 1, files: 1, capped: true });
expect(ai.run).toHaveBeenCalledTimes(1);
expect(await countChunks(env, PROJECT, "gittensory")).toBe(MAX_CHUNKS_PER_REPO);
const row = await env.DB.prepare("SELECT text, blob_sha FROM repo_chunks WHERE project=? AND repo=? AND path=?")
.bind(PROJECT, "gittensory", "src/changed.ts")
.first<{ text: string; blob_sha: string }>();
expect(row).toEqual({ text: "export const changed = true;\n", blob_sha: "sha-new" });
});
});

describe("indexRepo: MAX_CHUNKS_PER_REPO cap holds", () => {
Expand Down