diff --git a/src/services/compile-backend.ts b/src/services/compile-backend.ts new file mode 100644 index 0000000..d0239a1 --- /dev/null +++ b/src/services/compile-backend.ts @@ -0,0 +1,37 @@ +/** + * Where Compile & run is sent. Every compile goes through one CompileBackend, so the app can move + * off godbolt.org to a service of its own (a self-hosted Compiler Explorer, or the checker service + * the proof stages plan, which will answer Mojo, Dafny and Lean) without touching the session or + * the views. The default is godbolt.org with the pinned compiler (src/mojo/generated/pin.ts). + * + * A backend answers exactly what godbolt answers: the same request body (mojoRequestBody, which + * tests/mojo.test.mjs holds to what the corpus pipeline sends) and the same answer shape + * (normaliseAnswer), so a stored answer and a live one mean the same thing. Rule 3 in AGENTS.md + * still holds whichever backend answers: the app never says a program compiles without one. + */ +import { compileMojo, GODBOLT_TARGET, type CompileOptions, type CompileOutcome, type CompileTarget } from './godbolt.js'; + +export interface CompileBackend { + /** A short id, such as 'godbolt'. */ + id: string; + /** The host a person sees in messages and tooltips, such as 'godbolt.org'. */ + host: string; + /** The compiler id the service runs, such as 'mojo_1_1_0'. */ + compiler: string; + compile(source: string, opts?: CompileOptions): Promise; +} + +/** A backend that speaks Compiler Explorer's API at `target`. */ +export function explorerBackend(id: string, target: CompileTarget): CompileBackend { + return { id, host: target.host, compiler: target.compiler, compile: (source, opts = {}) => compileMojo(source, opts, target) }; +} + +export const GODBOLT: CompileBackend = explorerBackend('godbolt', GODBOLT_TARGET); + +let current: CompileBackend = GODBOLT; + +/** The backend Compile & run uses now. */ +export function compileBackend(): CompileBackend { return current; } + +/** Send compiles somewhere else from now on (a self-hosted service, or a fake in a test). Returns the previous one. */ +export function setCompileBackend(b: CompileBackend): CompileBackend { const prev = current; current = b; return prev; } diff --git a/src/services/godbolt.ts b/src/services/godbolt.ts index 2f1fce2..4777a86 100644 --- a/src/services/godbolt.ts +++ b/src/services/godbolt.ts @@ -218,24 +218,31 @@ export function listingFunctions(asm: AsmLine[]): ListingFunction[] { export interface CompileOutcome { answer: Answer | null; error: string | null; seconds: number } -/** Ask godbolt. A network or HTTP failure is an outcome to show, not an exception. */ -export async function compileMojo(source: string, opts: { stdin?: string; level?: BuildLevel; signal?: AbortSignal; timeoutMs?: number } = {}): Promise { +/** What a compile asks for, whichever service answers it. */ +export interface CompileOptions { stdin?: string; level?: BuildLevel; signal?: AbortSignal; timeoutMs?: number } +/** Where a compile is sent: a service that speaks Compiler Explorer's API, at a base URL. */ +export interface CompileTarget { base: string; compiler: string; host: string } +export const GODBOLT_TARGET: CompileTarget = { base: GODBOLT_BASE, compiler: GODBOLT_COMPILER, host: 'godbolt.org' }; + +/** Ask godbolt (or another Compiler Explorer). A network or HTTP failure is an outcome to show, not an exception. */ +export async function compileMojo(source: string, opts: CompileOptions = {}, target: CompileTarget = GODBOLT_TARGET): Promise { const t0 = Date.now(); const ctl = new AbortController(); const timer = setTimeout(() => ctl.abort(), opts.timeoutMs ?? 60000); opts.signal?.addEventListener('abort', () => ctl.abort()); + const host = target.host; try { - const res = await fetch(mojoRequestUrl(), { + const res = await fetch(mojoRequestUrl(target.base, target.compiler), { method: 'POST', headers: { 'Content-Type': 'application/json', Accept: 'application/json' }, body: JSON.stringify(mojoRequestBody(source, opts.stdin ?? '', BUILD_ARGS[opts.level ?? 'O0'])), signal: ctl.signal, }); - if (res.status === 429) return { answer: null, error: 'godbolt.org is rate-limiting requests from here. Wait a minute and compile again.', seconds: (Date.now() - t0) / 1000 }; - if (!res.ok) return { answer: null, error: `godbolt.org answered ${res.status} ${res.statusText}.`, seconds: (Date.now() - t0) / 1000 }; + if (res.status === 429) return { answer: null, error: `${host} is rate-limiting requests from here. Wait a minute and compile again.`, seconds: (Date.now() - t0) / 1000 }; + if (!res.ok) return { answer: null, error: `${host} answered ${res.status} ${res.statusText}.`, seconds: (Date.now() - t0) / 1000 }; return { answer: normaliseAnswer(await res.json()), error: null, seconds: (Date.now() - t0) / 1000 }; } catch (e: any) { - const msg = e?.name === 'AbortError' ? 'godbolt.org did not answer in time.' : `Could not reach godbolt.org (${e?.message || e}).`; + const msg = e?.name === 'AbortError' ? `${host} did not answer in time.` : `Could not reach ${host} (${e?.message || e}).`; return { answer: null, error: msg, seconds: (Date.now() - t0) / 1000 }; } finally { clearTimeout(timer);