Send Compile & run through a CompileBackend instead of straight to godbolt

Every compile went to godbolt.org from the session directly. The roadmap
moves compiles, and later Dafny and Lean checks, to a pinned service of the
app's own, and godbolt has no service guarantee, so where a compile goes is
now one replaceable piece.

- src/services/compile-backend.ts: a CompileBackend (id, host, compiler,
  compile), explorerBackend() for any service that speaks Compiler
  Explorer's API at a base URL, GODBOLT as the default, and
  compileBackend() / setCompileBackend().
- godbolt.ts: compileMojo takes a CompileTarget (base, compiler, host) and
  names that host in its messages; the request body is unchanged, so
  tests/mojo.test.mjs still holds it to what the corpus pipeline sends.
- Session.compile asks the current backend and records which host answered
  (CompileRecord.host; older records came from godbolt.org).
- The Compiler view names the host it is asking and the host that answered.
  The "Compiled on godbolt" badge keeps its wording until a second backend
  is deployed.

A test sends a compile through a fake backend and a refused local address.

Manual check list: open a Mojo exercise in the Developer area, Compile &
run, and see "Asking godbolt.org (mojo_1_1_0)…" then "Mojo 1.1.0 on
godbolt.org" in the Compiler view.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01FgvbRdoEheAHqsRmr2WuKZ
This commit is contained in:
alexmandClaude committed 2026-10-05 03:46:04 +00:00
1 parent 9e318a1eb5
commit d4730b7d5a
2 files changed
+50 -6

No files matched your search

+13 -6
View File
@@ -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<CompileOutcome> {
/** 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<CompileOutcome> {
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);