Skip to content

Turbocharge Claude Code, Cursor, Codex, Gemini & every coding agent: faster, cheaper, with contextual understanding specific to your codebase.

feat(brain): answer repository access on its own, without reading the repo

anirudhkumar-nanonets4f39d193841b
diff --git a/src/app/brain-build.ts b/src/app/brain-build.ts
index 28136bdc..83c01c30 100644
--- a/src/app/brain-build.ts
+++ b/src/app/brain-build.ts
@@ -113,6 +113,55 @@ export class RepoNotAccessibleError extends Error {
}
}
+/** What a repository lets us do, before any of the work is started. */
+export interface RepoAccess {
+ /** True when we can read it: installed on it, or it is public. */
+ accessible: boolean;
+ isPrivate: boolean;
+ /** How we would read it — for the log, and for deciding nothing else. */
+ via: "installation" | "public" | "none";
+ /** Set only when accessible is false: what to tell the user, and where to
+ * send them. Same shape RepoNotAccessibleError carries. */
+ reason?: string;
+ gap?: RepoAccessGap;
+}
+
+/**
+ * Can we read this repository at all?
+ *
+ * The cheap half of a build, on its own: an installation lookup and at most
+ * one repository lookup, about a second in total, with no clone and no graph.
+ *
+ * It exists as its own call because the answer decides which screen the user
+ * sees next, and the rest of the read takes minutes. Waiting for the whole
+ * build to find out means either holding the person there or guessing from a
+ * timer, and a guess is wrong exactly when the box is busy — which is when
+ * two people are onboarding at once.
+ */
+export async function checkRepoAccess(job: { owner: string; repo: string }, deps: BrainBuildDeps): Promise<RepoAccess> {
+ const api = deps.api ?? "https://api.github.com";
+ const installationId = await installationFor(deps.creds, job.owner, job.repo, deps.fetch, (deps.now ?? Date.now)(), api);
+ if (installationId !== null) {
+ const token = await installationToken(deps, installationId, `${job.owner}/${job.repo}`, api);
+ const meta = await repoMeta(job.owner, job.repo, token, deps.fetch, api);
+ return { accessible: true, isPrivate: meta?.isPrivate ?? true, via: "installation" };
+ }
+ const meta = await repoMeta(job.owner, job.repo, await publicReadToken(deps, api), deps.fetch, api);
+ if (meta && !meta.isPrivate) return { accessible: true, isPrivate: false, via: "public" };
+
+ const gap = await repoAccessGap(deps.creds, job.owner, deps.fetch, (deps.now ?? Date.now)(), api);
+ return {
+ accessible: false,
+ isPrivate: true,
+ via: "none",
+ reason:
+ gap.reason === "repo_not_selected"
+ ? `graft is installed on ${job.owner} but ${job.owner}/${job.repo} is not in the list of repositories it can see`
+ : `graft is not installed on ${job.owner}`,
+ gap,
+ };
+}
+
/**
* Read the repository and hand its history to the brain.
*
diff --git a/src/app/server.ts b/src/app/server.ts
index dec4a586..c1bc1ad7 100644
--- a/src/app/server.ts
+++ b/src/app/server.ts
@@ -20,7 +20,7 @@ import { createServer, type IncomingMessage, type Server, type ServerResponse }
import { timingSafeEqual } from "node:crypto";
import { jobKey, reviewJobFor, type ReviewJob } from "./events.js";
import { InstallationTokens, verifySignature, type AppCredentials, type Fetch } from "./identity.js";
-import { buildRepoIntoBrain, RepoNotAccessibleError, type BrainBuildJob } from "./brain-build.js";
+import { buildRepoIntoBrain, checkRepoAccess, RepoNotAccessibleError, type BrainBuildJob } from "./brain-build.js";
import { PageStore } from "./pages.js";
import { WorkQueue } from "./queue.js";
import { reviewInChildProcess } from "./review-process.js";
@@ -35,6 +35,9 @@ export interface AppSeams {
/** Swapped out so a test can assert what the route handed the builder without
* cloning a repository or reaching GitHub. */
brainBuild?: typeof buildRepoIntoBrain;
+ /** Swapped out so a test can assert the access route's answers without
+ * reaching GitHub. */
+ brainAccess?: typeof checkRepoAccess;
/** Swapped out to assert what the queue was handed, without a clone — and, being
* called in-process, without the fork the default reviewer does. */
review?: typeof reviewPullRequest;
@@ -146,6 +149,48 @@ export function createApp(
return send(res, 202, "application/json", JSON.stringify({ queued: true }));
}
+ // Can we read this repository? The cheap question, answered on its own.
+ //
+ // Same secret as /brain/build, because it is the same caller and the same
+ // trust. Kept separate from it because it costs a second where the build
+ // costs minutes, and the platform needs the answer before it can decide
+ // which screen to show.
+ if (req.method === "POST" && url.pathname === "/brain/access") {
+ if (!config.brainBuildSecret) return send(res, 404, "text/plain", "not found");
+ if (!bearerMatches(header(req, "authorization"), config.brainBuildSecret)) {
+ return send(res, 401, "text/plain", "unauthorized");
+ }
+ const body = await readBody(req);
+ if (body === null) return send(res, 413, "text/plain", "payload too large");
+ let ask: { owner?: string; repo?: string };
+ try {
+ ask = JSON.parse(body) as { owner?: string; repo?: string };
+ } catch {
+ return send(res, 400, "text/plain", "bad json");
+ }
+ if (!ask.owner || !ask.repo) {
+ return send(res, 400, "application/json", JSON.stringify({ error: "owner and repo are required" }));
+ }
+ try {
+ const access = await (seams.brainAccess ?? checkRepoAccess)({ owner: ask.owner, repo: ask.repo }, {
+ creds: config,
+ fetch: fetchImpl,
+ api: config.api,
+ publicToken: config.publicToken,
+ publicOwner: config.publicOwner,
+ log,
+ now: seams.now,
+ });
+ // 200 either way: "we cannot see it" is an answer, not a failure, and
+ // the platform turns it into a choice rather than an error.
+ return send(res, 200, "application/json", JSON.stringify(access));
+ } catch (e) {
+ const msg = e instanceof Error ? e.message : String(e);
+ log(`brain access ${ask.owner}/${ask.repo} failed: ${msg}`);
+ return send(res, 502, "application/json", JSON.stringify({ error: msg }));
+ }
+ }
+
// Build one repository into a brain, on demand. Called by the platform when
// someone pastes a repository URL during onboarding — there is no webhook
// behind it, so it authenticates with a bearer token rather than a payload