Turbocharge Claude Code, Cursor, Codex, Gemini & every coding agent: faster, cheaper, with contextual understanding specific to your codebase.
TypeScript ai-agentsanthropicclaude-codeclicode-graphcodexcontext-engineeringcursordeveloper-toolsgeminiknowledge-graphllmmcpmcp-serveropen-sourceopenaitree-sitter
fix(brain): borrow an installation token for public reads, not the anonymous rate limit
anirudhkumar-nanonets2a70cbdf8d01
diff --git a/src/app/brain-build.ts b/src/app/brain-build.tsindex 8711b14b..903a3d61 100644--- a/src/app/brain-build.ts+++ b/src/app/brain-build.ts@@ -15,7 +15,16 @@ import { buildGraph } from "../graph/build.js";import { contextDirFor } from "../context/node-file.js";import { loadGraphCached } from "../graph/load.js";import { checkoutRepository } from "./checkout.js";-import { appJwt, ghHeaders, installationFor, repoAccessGap, type AppCredentials, type Fetch, type RepoAccessGap } from "./identity.js";+import {+ appJwt,+ ghHeaders,+ installationFor,+ publicReadInstallation,+ repoAccessGap,+ type AppCredentials,+ type Fetch,+ type RepoAccessGap,+} from "./identity.js";import { buildDigest, postDigest, readCommits, readSymbols, readThreads, type RepoDigest } from "./history.js";import {budgetSources,@@ -79,13 +88,16 @@ export interface BrainBuildDeps {/*** Token used for a public repository the App is not installed on.*- * Optional, and the read works without it: a public repo clones anonymously- * and answers the API anonymously too. What it buys is the rate limit —- * anonymous is 60 requests an hour for the whole box, which the pull-request- * walk exhausts on the first repository of the day, after which every later- * build quietly degrades to commits only.+ * Optional. Without it the read borrows one of our own installation's tokens,+ * which GitHub answers for any public resource, and falls back to anonymous+ * if the App has no installations at all. Anonymous works but is 60 requests+ * an hour for the whole box — one pull-request walk — so it is the last+ * resort, not the plan.*/publicToken?: string;+ /** Whose installation to borrow for those reads. Ours, so a public read+ * spends our rate limit and not a customer's. */+ publicOwner?: string;}/** Raised when the App cannot see the repository. Distinct because the caller@@ -133,14 +145,17 @@ export async function buildRepoIntoBrain(// single-ref fetch (there is no origin/HEAD to resolve), so both are read// from the API rather than guessed.let token = "";+ let cloneToken: string | null = null;let meta: RepoMeta | null = null;if (installationId !== null) {token = await installationToken(deps, installationId, tag, api);meta = await repoMeta(job.owner, job.repo, token, deps.fetch, api);} else {- // No installation. If the repository answers anonymously and says it is- // public, that is all the access this read needs.- token = deps.publicToken ?? "";+ // No installation on this repository. That rules out nothing yet: a public+ // repo clones with no credential at all, and its API answers to any token+ // we hold. So find a credential for the reading, then ask the repo whether+ // it is in fact public.+ token = await publicReadToken(deps, api);meta = await repoMeta(job.owner, job.repo, token, deps.fetch, api);if (!meta || meta.isPrivate) {// Which of the two gaps it is decides what the UI can offer, so it is@@ -154,6 +169,10 @@ export async function buildRepoIntoBrain();}log(`${tag}: no installation, reading it as a public repository${token ? "" : " anonymously"}`);+ // The clone is the one part that stays anonymous. A public repository+ // serves it to anyone, and a token minted for a different account is not+ // something git is promised to accept on this one.+ cloneToken = "";}if (!meta) meta = { isPrivate: true, defaultBranch: "" };@@ -161,7 +180,7 @@ export async function buildRepoIntoBrain(owner: job.owner,repo: job.repo,ref: job.ref || meta.defaultBranch,- token,+ token: cloneToken ?? token,api: deps.githubHost,log,});@@ -269,6 +288,31 @@ async function repoMeta(owner: string, repo: string, token: string, fetchImpl: F}}+/**+ * A credential for reading a public repository we are not installed on.+ *+ * Order: a token configured for exactly this, then one of our own+ * installation's, then nothing. Every step of that ladder works — the+ * difference is only the rate limit, and the drop to anonymous is a factor of+ * eighty, so it is worth two extra calls to avoid.+ */+async function publicReadToken(deps: BrainBuildDeps, api: string): Promise<string> {+ if (deps.publicToken) return deps.publicToken;+ try {+ const id = await publicReadInstallation(+ deps.creds,+ deps.publicOwner ?? "trailhq",+ deps.fetch,+ (deps.now ?? Date.now)(),+ api,+ );+ if (id === null) return "";+ return await installationToken(deps, id, "public read", api);+ } catch {+ return "";+ }+}+/** Mint an installation token, the credential every authenticated read uses. */async function installationToken(deps: BrainBuildDeps, installationId: number, tag: string, api: string): Promise<string> {const res = await deps.fetch(`${api}/app/installations/${installationId}/access_tokens`, {diff --git a/src/app/identity.ts b/src/app/identity.tsindex 670c5508..526d9ad6 100644--- a/src/app/identity.ts+++ b/src/app/identity.ts@@ -94,6 +94,41 @@ export function ghHeaders(token: string): Record<string, string> {return headers;}+/**+ * An installation token for reading PUBLIC repositories the App is not+ * installed on.+ *+ * GitHub answers any public resource to any installation token, at 5,000+ * requests an hour. Anonymous answers the same resources at 60 an hour for the+ * whole box, which one repository's pull-request walk spends in a minute — so+ * without this, the second public repo of the hour reads as "we cannot see it".+ *+ * `owner` is our own account, so this borrows OUR installation's rate limit+ * rather than some customer's. Falls back to the first installation, and to+ * null when the App has none, in which case the caller reads anonymously.+ */+export async function publicReadInstallation(+ creds: AppCredentials,+ owner: string,+ fetchImpl: Fetch,+ nowMs: number = Date.now(),+ api = "https://api.github.com",+): Promise<number | null> {+ const res = await fetchImpl(`${api}/app/installations?per_page=100`, {+ headers: {+ authorization: `Bearer ${appJwt(creds, nowMs)}`,+ accept: "application/vnd.github+json",+ "user-agent": "graft-app",+ },+ });+ if (!res.ok) return null;+ const list = JSON.parse(await res.text()) as Array<{ id?: number; account?: { login?: string } }>;+ if (!Array.isArray(list) || list.length === 0) return null;+ const wanted = owner.toLowerCase();+ const mine = list.find((i) => (i.account?.login ?? "").toLowerCase() === wanted);+ return (mine ?? list[0]).id ?? null;+}+export async function installationFor(creds: AppCredentials,owner: string,diff --git a/src/app/main.ts b/src/app/main.tsindex 6566c759..745f3f03 100644--- a/src/app/main.ts+++ b/src/app/main.ts@@ -53,6 +53,7 @@ const { server, queue } = createApp({brainBuildSecret: process.env.GRAFT_BRAIN_BUILD_SECRET,brainBaseUrl: process.env.GRAFT_BRAIN_URL,publicToken: process.env.GRAFT_PUBLIC_GITHUB_TOKEN || process.env.GITHUB_TOKEN,+ publicOwner: process.env.GRAFT_PUBLIC_INSTALLATION_OWNER,});/**diff --git a/src/app/server.ts b/src/app/server.tsindex 96409161..dec4a586 100644--- a/src/app/server.ts+++ b/src/app/server.ts@@ -51,9 +51,11 @@ export interface AppConfig extends AppCredentials {/** Platform base URL the digest is posted to. Defaults to production. */brainBaseUrl?: string;/** Token for reading public repositories the App is not installed on.- * Optional — without it those reads are anonymous, and anonymous runs out of- * rate limit after about one repository an hour. */+ * Optional — without it the read borrows one of our own installation's+ * tokens, and only falls back to anonymous if the App has none. */publicToken?: string;+ /** Whose installation to borrow for that. Defaults to ours. */+ publicOwner?: string;/** Public origin, for the links put in comments, e.g. https://graft.example.com */publicUrl: string;/** Where pages are kept, so a restart does not strand the links already posted. */@@ -181,6 +183,7 @@ export function createApp(fetch: fetchImpl,api: config.api,publicToken: config.publicToken,+ publicOwner: config.publicOwner,log,now: seams.now,});