SPB Git

spb/autoscholar

Public
29.9 KB · 791 lines typescript
1import { spawn } from "node:child_process";2import { createHash } from "node:crypto";3import {4  appendFile,5  chmod,6  copyFile,7  cp,8  mkdir,9  readdir,10  readFile,11  rm,12  stat,13  writeFile,14} from "node:fs/promises";15import { join } from "node:path";16import { query, type SDKMessage } from "@anthropic-ai/claude-agent-sdk";17import type { ArtifactKind, RunEventInput, RunManifest, StageKey } from "@autoscholar/schemas";18import { MetricsFile } from "@autoscholar/schemas";19import { syncArtifacts, writeChecksums } from "./artifacts.js";20import { buildLatex, listFiles, renderPages } from "./latex.js";21import {22  createPolicyHook,23  PLATFORM_SYSTEM_APPEND,24  RESEARCH_ALLOWED_TOOLS,25  RESEARCH_DISALLOWED_TOOLS,26  sandboxSettings,27} from "./policy.js";28import { STAGE_SPECS, type StageContext, type StageSpec } from "./stages.js";29import { createResearchTools } from "./tools.js";3031export interface RunnerJob {32  runId: string;33  attemptNo: number;34  manifest: RunManifest;35  instructionText: string;36  resumeFromStage: StageKey | null;37  checkpoint: Record<string, unknown> | null;38}3940export interface RunnerHost {41  workspaceRoot: string;42  apiKey: string;43  extraDomains: string[];44  tectonicBin?: string;45  tectonicCache?: string;46  latexTemplateDir: string;47  workerName: string;48  sdkVersion: string;49  toolchain: Record<string, string>;50  emit: (ev: RunEventInput) => void;51  stage: (report: {52    stage: StageKey;53    status: "running" | "completed" | "failed" | "skipped";54    costUsd: number;55    turns: number;56    usage?: Record<string, unknown>;57    error?: string;58    checkpoint?: Record<string, unknown>;59  }) => Promise<void>;60  upload: (path: string, kind: ArtifactKind, contentType: string, buf: Buffer) => Promise<void>;61  isCancelled: () => boolean;62  /** Optional: fetch the latest artifacts of this run from the control plane (resume on a different node). */63  fetchArtifacts?: () => Promise<Array<{ path: string; read: () => Promise<Buffer> }>>;64}6566export interface RunOutcome {67  outcome: "completed" | "failed" | "cancelled" | "budget_exceeded" | "timeout";68  error?: string;69  costUsd: number;70  turns: number;71  stagesCompleted: StageKey[];72  resourceUsage: { wallSeconds: number; diskBytes: number; peakRssBytes: number };73}7475interface SessionResult {76  costUsd: number;77  turns: number;78  subtype: string;79  resultText: string;80  usage: Record<string, unknown>;81  denied: number;82}8384async function dirSize(dir: string): Promise<number> {85  let total = 0;86  for (const f of await listFiles(dir)) {87    try {88      total += (await stat(join(dir, f))).size;89    } catch {90      /* ignore */91    }92  }93  return total;94}9596export class ResearchRunner {97  private readonly ws: string;98  private spent = 0;99  private turns = 0;100  private readonly started = Date.now();101  private completed: StageKey[] = [];102  private notes: string[] = [];103  private peakRss = 0;104105  constructor(106    private readonly job: RunnerJob,107    private readonly host: RunnerHost,108  ) {109    this.ws = join(host.workspaceRoot, "runs", job.runId);110  }111112  private emit(113    kind: string,114    message: string,115    data?: Record<string, unknown>,116    level: RunEventInput["level"] = "info",117    stage?: StageKey,118  ) {119    const ev: RunEventInput = {120      kind,121      message: message.slice(0, 4000),122      data,123      level,124      stage,125      at: new Date().toISOString(),126    };127    this.host.emit(ev);128    appendFile(129      join(this.ws, "provenance/events.jsonl"),130      `${JSON.stringify({ ...ev, runId: this.job.runId, attemptNo: this.job.attemptNo })}\n`,131    ).catch(() => {});132    const rss = process.memoryUsage().rss;133    if (rss > this.peakRss) this.peakRss = rss;134  }135136  private remainingBudget(): number {137    return this.job.manifest.maxBudgetUsd - this.spent;138  }139  private remainingMinutes(): number {140    return this.job.manifest.maxRuntimeMinutes - (Date.now() - this.started) / 60_000;141  }142143  /** Creates the isolated workspace: frozen CLAUDE.md as project instructions, contract directories, LaTeX class. */144  async prepareWorkspace(): Promise<{ resumed: boolean; startIndex: number }> {145    const ws = this.ws;146    let resumed = false;147    let startIndex = 0;148    const existed = await stat(ws)149      .then((s) => s.isDirectory())150      .catch(() => false);151    const cpStage = (this.job.checkpoint?.stage as StageKey | undefined) ?? this.job.resumeFromStage;152    if (existed && cpStage && STAGE_SPECS.some((s) => s.key === cpStage)) {153      const idx = STAGE_SPECS.findIndex((s) => s.key === cpStage);154      // verify the frozen instruction is intact; otherwise restart from scratch155      const frozen = await readFile(join(ws, "CLAUDE.md"), "utf8").catch(() => "");156      if (createHash("sha256").update(frozen).digest("hex") === this.job.manifest.instructionSha256) {157        resumed = true;158        startIndex = idx + 1;159        this.completed =160          (this.job.checkpoint?.completedStages as StageKey[] | undefined) ??161          STAGE_SPECS.slice(0, idx + 1).map((s) => s.key);162        this.spent = Number(this.job.checkpoint?.spentUsd ?? 0);163      }164    }165    if (!resumed) {166      if (existed) {167        await chmod(join(ws, "input"), 0o755).catch(() => {});168        for (const f of ["CLAUDE.md", "input/CLAUDE.md", "input/run.json"])169          await chmod(join(ws, f), 0o644).catch(() => {});170        await rm(ws, { recursive: true, force: true });171      }172      for (const d of [173        "input",174        "plan",175        "sources",176        "data",177        "code",178        "results/tables",179        "results/figures",180        "manuscript",181        "review",182        "provenance",183        "home/.claude",184        ".autoscholar",185        ".cache/uv",186        ".cache/tmp",187      ])188        await mkdir(join(ws, d), { recursive: true });189      await writeFile(join(ws, "CLAUDE.md"), this.job.instructionText);190      await writeFile(join(ws, "input/CLAUDE.md"), this.job.instructionText);191      await chmod(join(ws, "CLAUDE.md"), 0o444);192      await chmod(join(ws, "input/CLAUDE.md"), 0o444);193      await writeFile(194        join(ws, "input/run.json"),195        `${JSON.stringify({ ...this.job.manifest, workerName: this.host.workerName, attemptNo: this.job.attemptNo, frozenAt: new Date().toISOString() }, null, 2)}\n`,196      );197      await chmod(join(ws, "input/run.json"), 0o444);198      await chmod(join(ws, "input"), 0o555);199      await writeFile(200        join(ws, "home/.claude/settings.json"),201        JSON.stringify({ env: {}, includeCoAuthoredBy: false }, null, 2),202      );203      for (const f of await readdir(this.host.latexTemplateDir).catch(() => [] as string[])) {204        if (/\.(cls|sty|bst|mplstyle)$/.test(f))205          await copyFile(join(this.host.latexTemplateDir, f), join(ws, "manuscript", f));206      }207      await writeFile(join(ws, ".gitignore"), "home/\n.cache/\n.autoscholar/\n");208    }209    this.emit(210      "workspace.ready",211      resumed ? `resumed workspace at stage index ${startIndex}` : "fresh isolated workspace prepared",212      { workspace: ws, resumed, instructionSha256: this.job.manifest.instructionSha256 },213    );214    return { resumed, startIndex };215  }216217  private buildOptions(218    spec: StageSpec,219    maxBudgetUsd: number,220    maxTurns: number,221    abort: AbortController,222    denied: { n: number },223  ) {224    const ws = this.ws;225    const reviewer = !!spec.reviewer;226    const hook = createPolicyHook({227      workspace: ws,228      frozenPaths: ["CLAUDE.md", "input", "provenance", ".gitignore"],229      writableOnly: reviewer ? ["review/"] : undefined,230      onDenied: (tool, reason, input) => {231        denied.n++;232        this.emit("policy.denied", `${tool} denied: ${reason}`, { tool, reason, input }, "warn", spec.key);233      },234    });235    const tools = createResearchTools({236      workspace: ws,237      tectonicBin: this.host.tectonicBin,238      tectonicCache: this.host.tectonicCache,239      emit: (k, m, d) => this.emit(k, m, d, "info", spec.key),240    });241    const reviewerAppend = reviewer242      ? "\n\n# Reviewer session\nYou are the independent referee for this manuscript. You did not author it. You may only write files under review/. Be rigorous, specific and fair; blockers only for fabricated/unverifiable/invalid science."243      : "";244    return {245      cwd: ws,246      model: reviewer ? this.job.manifest.reviewerModel : this.job.manifest.model,247      systemPrompt: {248        type: "preset" as const,249        preset: "claude_code" as const,250        append: PLATFORM_SYSTEM_APPEND + reviewerAppend,251      },252      settingSources: ["project" as const],253      permissionMode: "dontAsk" as const,254      allowedTools: RESEARCH_ALLOWED_TOOLS,255      disallowedTools: RESEARCH_DISALLOWED_TOOLS,256      hooks: { PreToolUse: [{ hooks: [hook] }] },257      sandbox: sandboxSettings(ws, this.host.extraDomains),258      mcpServers: { autoscholar: tools },259      maxBudgetUsd,260      maxTurns,261      abortController: abort,262      persistSession: false,263      includePartialMessages: false,264      env: {265        PATH: "/opt/homebrew/bin:/usr/local/bin:/usr/bin:/bin:/usr/sbin:/sbin",266        HOME: join(ws, "home"),267        CLAUDE_CONFIG_DIR: join(ws, "home/.claude"),268        ANTHROPIC_API_KEY: this.host.apiKey,269        TMPDIR: join(ws, ".cache/tmp"),270        UV_CACHE_DIR: join(ws, ".cache/uv"),271        TECTONIC_CACHE_DIR: this.host.tectonicCache ?? join(ws, ".cache/tectonic"),272        LANG: "en_US.UTF-8",273        LC_ALL: "en_US.UTF-8",274        SOURCE_DATE_EPOCH: "1700000000",275        MPLBACKEND: "Agg",276        PYTHONDONTWRITEBYTECODE: "1",277        CLAUDE_CODE_DISABLE_AUTO_MEMORY: "1",278        DISABLE_TELEMETRY: "1",279        DISABLE_AUTOUPDATER: "1",280        CLAUDE_CODE_DISABLE_NONESSENTIAL_TRAFFIC: "1",281        AUTOSCHOLAR_RUN_ID: this.job.runId,282      },283      stderr: (d: string) => {284        if (/error|fatal|sandbox/i.test(d))285          this.emit("sdk.stderr", d.slice(0, 500), undefined, "debug", spec.key);286      },287    };288  }289290  private async session(291    spec: StageSpec,292    prompt: string,293    limits: { maxBudgetUsd: number; maxTurns: number; timeoutMs: number },294  ): Promise<SessionResult> {295    const abort = new AbortController();296    const denied = { n: 0 };297    const timer = setTimeout(() => abort.abort(new Error("stage wall-clock limit")), limits.timeoutMs);298    const cancelPoll = setInterval(() => {299      if (this.host.isCancelled()) abort.abort(new Error("cancelled"));300    }, 5000);301    const result: SessionResult = {302      costUsd: 0,303      turns: 0,304      subtype: "unknown",305      resultText: "",306      usage: {},307      denied: 0,308    };309    const t0 = Date.now();310    try {311      const q = query({312        prompt,313        options: this.buildOptions(spec, limits.maxBudgetUsd, limits.maxTurns, abort, denied),314      });315      let toolCalls = 0;316      for await (const msg of q as AsyncIterable<SDKMessage>) {317        if (msg.type === "system" && (msg as { subtype?: string }).subtype === "init") {318          const m = msg as unknown as {319            model?: string;320            claude_code_version?: string;321            tools?: string[];322            mcp_servers?: Array<{ name: string; status: string }>;323          };324          this.emit(325            "session.init",326            `session started (${m.model ?? "model"}; claude code ${m.claude_code_version ?? "?"})`,327            { tools: m.tools?.length, mcp: m.mcp_servers },328            "debug",329            spec.key,330          );331        } else if (msg.type === "assistant") {332          const content =333            (334              msg as unknown as {335                message: {336                  content: Array<{337                    type: string;338                    text?: string;339                    name?: string;340                    input?: Record<string, unknown>;341                  }>;342                };343              }344            ).message.content ?? [];345          for (const c of content) {346            if (c.type === "text" && c.text?.trim())347              this.emit("assistant.text", c.text.slice(0, 600), undefined, "debug", spec.key);348            if (c.type === "tool_use") {349              toolCalls++;350              const summary =351                c.name === "Bash"352                  ? String(c.input?.command ?? "").slice(0, 200)353                  : c.name === "Write" || c.name === "Edit" || c.name === "Read"354                    ? String(c.input?.file_path ?? "")355                    : c.name?.startsWith("mcp__")356                      ? JSON.stringify(c.input).slice(0, 200)357                      : "";358              this.emit("tool.use", `${c.name}: ${summary}`, { tool: c.name }, "debug", spec.key);359            }360          }361        } else if (msg.type === "result") {362          const r = msg as unknown as {363            subtype: string;364            total_cost_usd?: number;365            num_turns?: number;366            result?: string;367            usage?: Record<string, unknown>;368            modelUsage?: Record<string, unknown>;369            errors?: string[];370          };371          result.subtype = r.subtype;372          result.costUsd = Number(r.total_cost_usd ?? 0);373          result.turns = Number(r.num_turns ?? 0);374          result.resultText = (r.result ?? (r.errors ?? []).join("; ")).slice(0, 4000);375          const u = (r.usage ?? {}) as Record<string, number>;376          result.usage = {377            model: spec.reviewer ? this.job.manifest.reviewerModel : this.job.manifest.model,378            inputTokens: u.input_tokens ?? 0,379            outputTokens: u.output_tokens ?? 0,380            cacheReadTokens: u.cache_read_input_tokens ?? 0,381            cacheCreateTokens: u.cache_creation_input_tokens ?? 0,382            modelUsage: r.modelUsage,383            toolCalls,384          };385        }386      }387    } catch (e) {388      result.subtype = abort.signal.aborted389        ? `aborted:${(abort.signal.reason as Error)?.message ?? "abort"}`390        : `error:${(e as Error).message.slice(0, 300)}`;391    } finally {392      clearTimeout(timer);393      clearInterval(cancelPoll);394    }395    result.denied = denied.n;396    this.spent += result.costUsd;397    this.turns += result.turns;398    this.emit(399      "session.end",400      `${spec.key} session ${result.subtype}: ${result.turns} turns, $${result.costUsd.toFixed(3)}, ${Math.round((Date.now() - t0) / 1000)}s, ${denied.n} policy denials`,401      { subtype: result.subtype, costUsd: result.costUsd, turns: result.turns, denied: denied.n },402      "info",403      spec.key,404    );405    return result;406  }407408  private async stageContext(extra: Partial<StageContext> = {}): Promise<StageContext> {409    return {410      filesPresent: await listFiles(this.ws),411      previousStageNotes: this.notes.slice(-6).join("\n"),412      ...extra,413    };414  }415416  private async syncAndCheckpoint(417    stage: StageKey,418    status: "completed" | "failed",419    cost: number,420    turns: number,421    usage?: Record<string, unknown>,422    error?: string,423  ) {424    await syncArtifacts(this.ws, stage, this.host.upload, (k, m, d) => this.emit(k, m, d, "info", stage));425    if (status === "completed") this.completed.push(stage);426    await this.host.stage({427      stage,428      status,429      costUsd: cost,430      turns,431      usage,432      error,433      checkpoint:434        status === "completed"435          ? { stage, completedStages: this.completed, spentUsd: this.spent, at: new Date().toISOString() }436          : undefined,437    });438  }439440  /** Platform-side reproduction (stage 8, before the agent's revision session). */441  private async reproduce(): Promise<Record<string, unknown>> {442    const out: Record<string, unknown> = { at: new Date().toISOString() };443    const manuscript = join(this.ws, "manuscript");444    const cleanDir = join(this.ws, "_repro/manuscript");445    await rm(join(this.ws, "_repro"), { recursive: true, force: true });446    await mkdir(cleanDir, { recursive: true });447    for (const f of await readdir(manuscript))448      if (/\.(tex|bib|cls|sty|bst|pdf|png|jpg|csv)$/.test(f) && f !== "main.pdf")449        await cp(join(manuscript, f), join(cleanDir, f), { recursive: true });450    for (const d of ["results"])451      if (await stat(join(this.ws, d)).catch(() => null))452        await cp(join(this.ws, d), join(this.ws, "_repro", d), { recursive: true });453    const build = await buildLatex(cleanDir, {454      tectonicBin: this.host.tectonicBin,455      cacheDir: this.host.tectonicCache,456    });457    out.cleanPdfBuild = {458      ok: build.ok,459      pages: build.pages,460      durationMs: build.durationMs,461      logTail: build.log.slice(-1500),462    };463    if (build.ok && build.pdfPath) {464      const a = createHash("sha256")465        .update(await readFile(build.pdfPath))466        .digest("hex");467      out.cleanPdfSha256 = a;468    }469    const script = join(this.ws, "code/reproduce.sh");470    if (await stat(script).catch(() => null)) {471      const reproDir = join(this.ws, "_repro/work");472      await mkdir(reproDir, { recursive: true });473      for (const d of ["code", "data"])474        if (await stat(join(this.ws, d)).catch(() => null))475          await cp(join(this.ws, d), join(reproDir, d), {476            recursive: true,477            filter: (s) => !/\/(\.venv|node_modules|__pycache__)(\/|$)/.test(s),478          });479      await mkdir(join(reproDir, "results/tables"), { recursive: true });480      await mkdir(join(reproDir, "results/figures"), { recursive: true });481      await mkdir(join(reproDir, "home"), { recursive: true });482      const profile = `(version 1)\n(allow default)\n(deny file-write*)\n(allow file-write* (subpath "${reproDir}") (subpath "/private/tmp") (subpath "/tmp") (subpath "/private/var/folders") (literal "/dev/null") (regex #"^/dev/tty") (subpath "${this.host.tectonicCache ?? join(this.ws, ".cache/tectonic")}"))\n(deny file-read* (subpath "/Users") (subpath "/etc/autoscholar") (subpath "/var/autoscholar/runs") (subpath "/private/var/autoscholar/runs"))\n(allow file-read* (subpath "${reproDir}") (subpath "${this.ws}/.cache"))\n`;483      const profilePath = join(this.ws, "_repro/repro.sb");484      await writeFile(profilePath, profile);485      const t0 = Date.now();486      const res = await new Promise<{ code: number | null; log: string }>((resolve) => {487        const child = spawn("/usr/bin/sandbox-exec", ["-f", profilePath, "/bin/bash", "code/reproduce.sh"], {488          cwd: reproDir,489          env: {490            PATH: "/opt/homebrew/bin:/usr/local/bin:/usr/bin:/bin",491            HOME: join(reproDir, "home"),492            TMPDIR: "/tmp",493            UV_CACHE_DIR: join(this.ws, ".cache/uv"),494            MPLBACKEND: "Agg",495            LANG: "en_US.UTF-8",496            PYTHONDONTWRITEBYTECODE: "1",497          },498          stdio: ["ignore", "pipe", "pipe"],499        });500        let log = "";501        const cap = (d: Buffer) => {502          log += d.toString();503          if (log.length > 200_000) log = log.slice(-150_000);504        };505        child.stdout.on("data", cap);506        child.stderr.on("data", cap);507        const timer = setTimeout(() => child.kill("SIGKILL"), 40 * 60_000);508        child.on("close", (code) => {509          clearTimeout(timer);510          resolve({ code, log });511        });512        child.on("error", (e) => resolve({ code: -1, log: `spawn error: ${e.message}` }));513      });514      out.reproduceScript = {515        ran: res.code !== -1,516        exitCode: res.code,517        durationMs: Date.now() - t0,518        logTail: res.log.slice(-3000),519      };520      try {521        const orig = MetricsFile.parse(522          JSON.parse(await readFile(join(this.ws, "results/metrics.json"), "utf8")),523        );524        const regen = MetricsFile.parse(525          JSON.parse(await readFile(join(reproDir, "results/metrics.json"), "utf8")),526        );527        const compared: Array<{ key: string; original: unknown; reproduced: unknown; match: boolean }> = [];528        for (const [k, v] of Object.entries(orig.metrics)) {529          const r = regen.metrics[k];530          const match =531            typeof v === "number" && typeof r === "number"532              ? Math.abs(v - r) <= Math.max(1e-9, Math.abs(v) * 1e-6)533              : JSON.stringify(v) === JSON.stringify(r);534          compared.push({ key: k, original: v, reproduced: r, match });535        }536        out.metricsCompared = compared.length;537        out.metricsMismatched = compared.filter((c) => !c.match).slice(0, 25);538        out.metricsMissingInRerun = compared.filter((c) => c.reproduced === undefined).length;539      } catch (e) {540        out.metricsComparison = `not compared: ${(e as Error).message.slice(0, 200)}`;541      }542    } else out.reproduceScript = { ran: false, reason: "code/reproduce.sh not present" };543    await mkdir(join(this.ws, "results"), { recursive: true });544    await writeFile(join(this.ws, "results/reproduction.json"), `${JSON.stringify(out, null, 2)}\n`);545    return out;546  }547548  async run(): Promise<RunOutcome> {549    const { startIndex } = await this.prepareWorkspace();550    const finish = async (outcome: RunOutcome["outcome"], error?: string): Promise<RunOutcome> => {551      await this.finalize(outcome, error);552      return {553        outcome,554        error,555        costUsd: this.spent,556        turns: this.turns,557        stagesCompleted: this.completed,558        resourceUsage: {559          wallSeconds: (Date.now() - this.started) / 1000,560          diskBytes: await dirSize(this.ws),561          peakRssBytes: this.peakRss,562        },563      };564    };565    const total = this.job.manifest.maxBudgetUsd;566    for (let i = startIndex; i < STAGE_SPECS.length; i++) {567      const spec = STAGE_SPECS[i]!;568      if (this.host.isCancelled()) return finish("cancelled", "cancellation requested");569      if (this.remainingBudget() < Math.max(0.25, total * 0.02))570        return finish(571          "budget_exceeded",572          `budget exhausted before ${spec.key} ($${this.spent.toFixed(2)} of $${total})`,573        );574      if (this.remainingMinutes() < 2) return finish("timeout", `runtime cap reached before ${spec.key}`);575      await this.host.stage({ stage: spec.key, status: "running", costUsd: 0, turns: 0 });576      this.emit("stage.start", `stage ${i}: ${spec.key}`, undefined, "info", spec.key);577      const stageBudget = Math.min(this.remainingBudget() * 0.9, Math.max(1, total * spec.budgetShare * 1.6));578      const stageMinutes = Math.min(579        this.remainingMinutes() * 0.9,580        Math.max(5, this.job.manifest.maxRuntimeMinutes * spec.wallMinutesShare * 1.8),581      );582      let ctx: StageContext;583      if (spec.key === "reproduction") {584        this.emit(585          "reproduction.start",586          "platform re-executing analysis and clean LaTeX build",587          undefined,588          "info",589          spec.key,590        );591        const repro = await this.reproduce();592        const pdf = join(this.ws, "manuscript/main.pdf");593        let previews: string[] = [];594        if (await stat(pdf).catch(() => null)) {595          const pages = Number((repro.cleanPdfBuild as { pages?: number })?.pages ?? 0) || 4;596          const picks = [...new Set([1, 2, Math.max(1, Math.floor(pages / 2)), pages])].filter(597            (p) => p >= 1 && p <= pages,598          );599          previews = await renderPages(pdf, join(this.ws, "review/previews"), picks);600          this.emit(601            "reproduction.previews",602            previews.length603              ? `rendered ${previews.length} page previews`604              : "page previews unavailable (pdftoppm missing)",605            { previews: previews.map((p) => p.replace(`${this.ws}/`, "")) },606            "info",607            spec.key,608          );609        }610        ctx = await this.stageContext({611          reproduction: repro,612          previewFiles: previews.map((p) => p.replace(`${this.ws}/`, "")),613        });614      } else ctx = await this.stageContext();615      let res = await this.session(spec, spec.prompt(this.job.manifest, ctx), {616        maxBudgetUsd: stageBudget,617        maxTurns: spec.maxTurns,618        timeoutMs: stageMinutes * 60_000,619      });620      if (res.subtype.startsWith("aborted:cancelled"))621        return finish("cancelled", `cancelled during ${spec.key}`);622      if (res.subtype === "error_max_budget_usd" && this.remainingBudget() < 1) {623        await this.syncAndCheckpoint(624          spec.key,625          "failed",626          res.costUsd,627          res.turns,628          res.usage,629          "budget exhausted",630        );631        return finish("budget_exceeded", `budget exhausted during ${spec.key}`);632      }633      // manuscript stage: the platform guarantees a PDF build attempt634      if (spec.key === "manuscript" || spec.key === "reproduction") {635        const b = await buildLatex(join(this.ws, "manuscript"), {636          tectonicBin: this.host.tectonicBin,637          cacheDir: this.host.tectonicCache,638        });639        this.emit(640          "latex.build",641          b.ok ? `platform build ok: ${b.pages} pages` : "platform build failed",642          { ok: b.ok, pages: b.pages, log: b.log.slice(-1200) },643          b.ok ? "info" : "warn",644          spec.key,645        );646        if (!b.ok && this.remainingBudget() > 1 && !this.host.isCancelled()) {647          const fix = await this.session(648            spec,649            `${spec.prompt(this.job.manifest, await this.stageContext())}\n\n# BUILD FAILED — fix it\nThe platform's clean tectonic build of manuscript/main.tex failed. Log:\n\`\`\`\n${b.log.slice(-6000)}\n\`\`\`\nFix the LaTeX (or bibliography) and call latex_build until it succeeds. Do not remove scientific content to make it compile.`,650            { maxBudgetUsd: Math.min(this.remainingBudget() * 0.5, 4), maxTurns: 60, timeoutMs: 20 * 60_000 },651          );652          res = {653            ...res,654            costUsd: res.costUsd + fix.costUsd,655            turns: res.turns + fix.turns,656            denied: res.denied + fix.denied,657          };658          const b2 = await buildLatex(join(this.ws, "manuscript"), {659            tectonicBin: this.host.tectonicBin,660            cacheDir: this.host.tectonicCache,661          });662          this.emit(663            "latex.build",664            b2.ok ? `rebuild ok: ${b2.pages} pages` : "rebuild failed",665            { ok: b2.ok, pages: b2.pages },666            b2.ok ? "info" : "error",667            spec.key,668          );669          if (!b2.ok) {670            await this.syncAndCheckpoint(671              spec.key,672              "failed",673              res.costUsd,674              res.turns,675              res.usage,676              "LaTeX build failed after fix attempt",677            );678            return finish("failed", `LaTeX build failed in ${spec.key}`);679          }680        } else if (!b.ok) {681          await this.syncAndCheckpoint(682            spec.key,683            "failed",684            res.costUsd,685            res.turns,686            res.usage,687            "LaTeX build failed",688          );689          return finish("failed", `LaTeX build failed in ${spec.key}`);690        }691      }692      let problems = await spec.check(this.ws, this.job.manifest);693      if (problems.some((p) => p.startsWith("INFEASIBLE"))) {694        await this.syncAndCheckpoint(695          spec.key,696          "failed",697          res.costUsd,698          res.turns,699          res.usage,700          problems.join("; "),701        );702        return finish(703          "failed",704          problems.find((p) => p.startsWith("INFEASIBLE")),705        );706      }707      if (problems.length && this.remainingBudget() > 1 && !this.host.isCancelled()) {708        this.emit(709          "stage.check",710          `post-conditions not met, running a fix session: ${problems.join("; ")}`,711          { problems },712          "warn",713          spec.key,714        );715        const fix = await this.session(716          spec,717          `${spec.prompt(this.job.manifest, await this.stageContext())}\n\n# INCOMPLETE — the deterministic post-conditions of this stage failed:\n- ${problems.join("\n- ")}\nComplete exactly what is missing (do not redo finished work).`,718          {719            maxBudgetUsd: Math.min(this.remainingBudget() * 0.5, Math.max(1, stageBudget * 0.5)),720            maxTurns: Math.ceil(spec.maxTurns / 2),721            timeoutMs: Math.max(5, stageMinutes * 0.5) * 60_000,722          },723        );724        res = {725          ...res,726          costUsd: res.costUsd + fix.costUsd,727          turns: res.turns + fix.turns,728          denied: res.denied + fix.denied,729        };730        if (fix.subtype.startsWith("aborted:cancelled"))731          return finish("cancelled", `cancelled during ${spec.key}`);732        problems = await spec.check(this.ws, this.job.manifest);733      }734      if (problems.length) {735        await this.syncAndCheckpoint(736          spec.key,737          "failed",738          res.costUsd,739          res.turns,740          res.usage,741          problems.join("; "),742        );743        return finish("failed", `${spec.key}: ${problems.join("; ")}`);744      }745      this.notes.push(`[${spec.key}] ${res.resultText.replace(/\s+/g, " ").slice(0, 400)}`);746      await this.syncAndCheckpoint(spec.key, "completed", res.costUsd, res.turns, res.usage);747    }748    return finish("completed");749  }750751  private async finalize(outcome: RunOutcome["outcome"], error?: string) {752    try {753      const summary = {754        runId: this.job.runId,755        attemptNo: this.job.attemptNo,756        researchMode: this.job.manifest.researchMode,757        stagesCompleted: this.completed,758        costUsd: Number(this.spent.toFixed(4)),759        turns: this.turns,760        model: this.job.manifest.model,761        reviewerModel: this.job.manifest.reviewerModel,762        workerName: this.host.workerName,763        startedAt: new Date(this.started).toISOString(),764        endedAt: new Date().toISOString(),765        sdkVersion: this.host.sdkVersion,766        toolchain: this.host.toolchain,767        synthetic: false,768        outcome,769        error: error ?? null,770      };771      await mkdir(join(this.ws, "provenance"), { recursive: true });772      await writeFile(join(this.ws, "provenance/run-summary.json"), `${JSON.stringify(summary, null, 2)}\n`);773      await rm(join(this.ws, "_repro"), { recursive: true, force: true });774      await writeChecksums(this.ws);775      await syncArtifacts(this.ws, "reproduction", this.host.upload, (k, m, d) => this.emit(k, m, d));776      this.emit(777        "run.finalized",778        `run ${outcome}${error ? `: ${error}` : ""} — $${this.spent.toFixed(3)}, ${this.turns} turns, ${this.completed.length} stages`,779        { outcome, costUsd: this.spent, turns: this.turns },780        outcome === "completed" ? "info" : "warn",781      );782    } catch (e) {783      this.emit("run.finalize_error", (e as Error).message, undefined, "error");784    }785  }786}787788export async function runResearch(job: RunnerJob, host: RunnerHost): Promise<RunOutcome> {789  return new ResearchRunner(job, host).run();790}791
Files · 87b4e55c3b9fbcc2cd191a1e469b33de0a24ccc8
Menu

Theme