import { branchSha, listRefs } from "../git/refs"; import type { RepoRow } from "~/lib/types"; import { repoSearch } from "./repo-search-do"; import { backfillRepos } from "../backfill"; /** Queue message kept for jobs already in flight; indexing now lives in the repo's RepoSearch object. */ export type CodeIndexMessage = { kind: "code.index"; repoId: string; job?: unknown }; export async function indexRepoCode(message: CodeIndexMessage): Promise { await repoSearch(message.repoId).kick(message.repoId); } /** Never fails the caller: the index reconciles on the next push or search. */ export async function requestCodeIndex(repoId: string): Promise { try { await repoSearch(repoId).kick(repoId); } catch (error) { console.error("Code index kick failed", repoId, error); } } /** Kicks an index pass when the default branch head is not the indexed commit; returns whether one is pending. */ export async function ensureCodeIndex(repo: RepoRow): Promise { if (repo.status !== "ready") return false; const head = branchSha(await listRefs(repo), repo.default_branch); if (!head) return false; return repoSearch(repo.id).ensure(repo.id, head); } /** The repository was deleted: its index goes with it. */ export async function dropCodeIndex(repoId: string): Promise { try { await repoSearch(repoId).destroy(); } catch (error) { console.warn("Code index drop failed; the object clears itself on its next pass", repoId, error); } } /** * Cron step until done: wakes every repository's index once so indexes built before file sizes were kept * (language breakdown) rebuild themselves. New pushes reconcile on their own. */ export async function backfillCodeIndexSchema(): Promise { await backfillRepos("code-index-schema-2", (repoId) => repoSearch(repoId).kick(repoId)); }