@@ -320,6 +320,28 @@ function assertEngineeringRunProfile(run: RsiRunRecord, expected: ReturnType<typ |
320 | 320 | } |
321 | 321 | } |
322 | 322 |
|
| 323 | +function assertEngineeringRunIdentity( |
| 324 | + run: RsiRunRecord, |
| 325 | + config: RsiConfig, |
| 326 | + campaign: string, |
| 327 | + arm: "fixed-control" | "self-hosted", |
| 328 | + rootCommit: string, |
| 329 | + seed: string, |
| 330 | + runtime: NonNullable<RsiConfig["workerHarnessRuntime"]>, |
| 331 | + expectedProfile: ReturnType<typeof openRouterProfileById>, |
| 332 | +): void { |
| 333 | + const plannedRunId = engineeringRunId(campaign, arm) |
| 334 | + const expectedProfileId = expectedProfile.id === RSI_DEFAULT_OPENROUTER_PROFILE_ID ? undefined : expectedProfile.id |
| 335 | + if (run.runId !== plannedRunId || run.workerHarnessPairId !== campaign || run.workerHarnessMode !== arm || run.baseCommit !== rootCommit || run.workerHarnessSamplingSeed !== seed || run.model !== config.model || run.openRouterProfileId !== expectedProfileId || JSON.stringify(run.workerHarnessRuntime) !== JSON.stringify(runtime) || run.proofCampaign?.manifest.phaseMode !== "engineering-only") { |
| 336 | + throw new Error(`engineering replay found a ${arm} run with changed frozen identity`) |
| 337 | + } |
| 338 | + const frozenConfig = run.proofCampaign.manifest.settings?.effectiveConfig |
| 339 | + if (!frozenConfig || typeof frozenConfig !== "object" || Array.isArray(frozenConfig) || (frozenConfig as Record<string, unknown>).maxTokens !== config.maxTokens) { |
| 340 | + throw new Error(`engineering replay found a ${arm} run with a changed frozen output-token limit`) |
| 341 | + } |
| 342 | + assertEngineeringRunProfile(run, expectedProfile) |
| 343 | +} |
| 344 | + |
323 | 345 | export function assertProofWorkerProfile(config: RsiConfig, expected: ReturnType<typeof openRouterProfileById>, env: NodeJS.ProcessEnv = process.env): void { |
324 | 346 | const roles = resolveRoles(config.roles, env, config.model) |
325 | 347 | const worker = roles.worker |
@@ -473,20 +495,81 @@ export async function runEngineeringArm(config: RsiConfig, campaign: string, arm |
473 | 495 | const prior = before.runs.concat(before.activeRuns).filter((entry) => entry.workerHarnessPairId === campaign && entry.workerHarnessMode === arm) |
474 | 496 | if (prior.length > 1) throw new Error(`engineering replay found duplicate archived ${arm} runs`) |
475 | 497 | const existing = prior[0] |
476 | | - const plannedRunId = engineeringRunId(campaign, arm) |
477 | 498 | if (existing) { |
478 | | - if (existing.runId !== plannedRunId || existing.baseCommit !== rootCommit || existing.workerHarnessSamplingSeed !== seed || existing.model !== config.model || existing.openRouterProfileId !== (expectedProfile.id === RSI_DEFAULT_OPENROUTER_PROFILE_ID ? undefined : expectedProfile.id) || JSON.stringify(existing.workerHarnessRuntime) !== JSON.stringify(runtime) || existing.proofCampaign?.manifest.phaseMode !== "engineering-only") throw new Error(`engineering replay found a ${arm} run with changed frozen identity`) |
479 | | - const frozenConfig = existing.proofCampaign.manifest.settings?.effectiveConfig |
480 | | - if (!frozenConfig || typeof frozenConfig !== "object" || Array.isArray(frozenConfig) || (frozenConfig as Record<string, unknown>).maxTokens !== config.maxTokens) throw new Error(`engineering replay found a ${arm} run with a changed frozen output-token limit`) |
481 | | - assertEngineeringRunProfile(existing, expectedProfile) |
482 | | - if (existing.finishedAt && existing.proofCampaign.state === "finished") { |
483 | | - const arms = existing.proofCampaign.ledgerSnapshot?.arms as Array<Record<string, unknown>> | undefined |
484 | | - if (!arms?.some((entry) => entry.arm_id === arm && entry.state === "finished") || existing.proofCampaign.manifest.schedule.some((cell) => cell.phase === "confirmation")) throw new Error(`engineering replay found an invalid terminal ${arm} checkpoint`) |
| 499 | + assertEngineeringRunIdentity(existing, config, campaign, arm, rootCommit, seed, runtime, expectedProfile) |
| 500 | + const proofCampaign = existing.proofCampaign! |
| 501 | + if (existing.finishedAt && proofCampaign.state === "finished") { |
| 502 | + const arms = proofCampaign.ledgerSnapshot?.arms as Array<Record<string, unknown>> | undefined |
| 503 | + if (!arms?.some((entry) => entry.arm_id === arm && entry.state === "finished") || proofCampaign.manifest.schedule.some((cell) => cell.phase === "confirmation")) throw new Error(`engineering replay found an invalid terminal ${arm} checkpoint`) |
485 | 504 | return existing |
486 | 505 | } |
487 | 506 | if (before.runs.some((entry) => entry.runId === existing.runId)) throw new Error(`engineering ${arm} run is archived without a terminal campaign finish; refusing to create a second identity`) |
488 | 507 | } |
489 | | - return runner({ ...config, dryRun: false, workerHarnessRunId: plannedRunId, ...(existing ? { resumeRunId: existing.runId } : {}), workerHarnessMode: arm, workerHarnessPhase: "development", workerHarnessPairId: campaign, workerHarnessEngineeringOnly: true, workerHarnessRuntime: runtime }) |
| 508 | + return runner({ ...config, dryRun: false, workerHarnessRunId: engineeringRunId(campaign, arm), ...(existing ? { resumeRunId: existing.runId } : {}), workerHarnessMode: arm, workerHarnessPhase: "development", workerHarnessPairId: campaign, workerHarnessEngineeringOnly: true, workerHarnessRuntime: runtime }) |
| 509 | +} |
| 510 | + |
| 511 | +export async function runEngineeringArms(config: RsiConfig, campaign: string, rootCommit: string, seed: string, runtime: NonNullable<RsiConfig["workerHarnessRuntime"]>, runner: typeof runRsi = runRsi): Promise<RsiRunRecord[]> { |
| 512 | + const runs: RsiRunRecord[] = [] |
| 513 | + for (const arm of ["fixed-control", "self-hosted"] as const) { |
| 514 | + const run = await runEngineeringArm(config, campaign, arm, rootCommit, seed, runtime, runner) |
| 515 | + runs.push(run) |
| 516 | + if (run.proofCampaign?.state === "incomplete") break |
| 517 | + } |
| 518 | + return runs |
| 519 | +} |
| 520 | + |
| 521 | +export async function writeEngineeringStopReport(options: Options, run: RsiRunRecord): Promise<string> { |
| 522 | + if (run.proofCampaign?.state !== "incomplete" || !run.proofCampaign.ledgerSnapshot) throw new Error("engineering stop report requires a durable incomplete campaign checkpoint") |
| 523 | + const armId = run.proofCampaign.armId |
| 524 | + const events = (run.proofCampaign.ledgerSnapshot.events as Array<Record<string, unknown>> | undefined) ?? [] |
| 525 | + const failureEvents = events.filter((event) => event.arm_id === armId && event.event_type === "candidate-incomplete") |
| 526 | + if (failureEvents.length === 0) throw new Error("engineering stop report has no durable candidate-incomplete event") |
| 527 | + const body = { |
| 528 | + schemaVersion: 1 as const, |
| 529 | + outcome: "incomplete" as const, |
| 530 | + campaignId: run.proofCampaign.campaignId, |
| 531 | + manifestSha256: run.proofCampaign.manifestSha256, |
| 532 | + rootCommit: run.baseCommit, |
| 533 | + runId: run.runId, |
| 534 | + armId, |
| 535 | + reason: run.proofCampaign.incompleteReason ?? "candidate proof attempt remains unresolved", |
| 536 | + runReportPaths: run.reports, |
| 537 | + candidateFailureEvents: failureEvents, |
| 538 | + ledgerSnapshot: run.proofCampaign.ledgerSnapshot, |
| 539 | + ledgerSnapshotSha256: canonicalHash(run.proofCampaign.ledgerSnapshot), |
| 540 | + qualifiedEvidencePath: options.output, |
| 541 | + } |
| 542 | + const stopReport = { ...body, digestSha256: canonicalHash(body) } |
| 543 | + const stopPath = await assertSupervisorOnlyPlanPath(options.repo, `${options.output}.stop.json`) |
| 544 | + await fs.mkdir(path.dirname(stopPath), { recursive: true, mode: 0o700 }) |
| 545 | + try { |
| 546 | + const existing = JSON.parse((await readProtectedProofInput(options.repo, stopPath)).toString("utf8")) as { digestSha256?: string } |
| 547 | + if (existing.digestSha256 !== stopReport.digestSha256 || canonicalHash(Object.fromEntries(Object.entries(existing).filter(([key]) => key !== "digestSha256"))) !== stopReport.digestSha256) throw new Error("existing engineering stop report differs from the incomplete campaign checkpoint") |
| 548 | + } catch (error) { |
| 549 | + if ((error as NodeJS.ErrnoException).code !== "ENOENT") throw error |
| 550 | + await fs.writeFile(stopPath, `${JSON.stringify(stopReport, null, 2)}\n`, { mode: 0o600, flag: "wx" }) |
| 551 | + } |
| 552 | + return stopPath |
| 553 | +} |
| 554 | + |
| 555 | +export async function writePriorIncompleteEngineeringStopReport( |
| 556 | + options: Options, |
| 557 | + config: RsiConfig, |
| 558 | + campaign: string, |
| 559 | + rootCommit: string, |
| 560 | + seed: string, |
| 561 | + runtime: NonNullable<RsiConfig["workerHarnessRuntime"]>, |
| 562 | +): Promise<{ run: RsiRunRecord; stopPath: string } | undefined> { |
| 563 | + const expectedProfile = openRouterProfileForModel(config.model, config.roles?.worker?.openRouterProfile) |
| 564 | + assertProofWorkerProfile(config, expectedProfile, process.env) |
| 565 | + const archive = await readArchive(config.archiveDir) |
| 566 | + const prior = archive.runs.concat(archive.activeRuns).filter((run) => run.workerHarnessPairId === campaign && run.proofCampaign?.state === "incomplete") |
| 567 | + if (prior.length > 1) throw new Error("engineering replay found duplicate incomplete runs for the campaign") |
| 568 | + const run = prior[0] |
| 569 | + if (!run) return undefined |
| 570 | + if (run.workerHarnessMode !== "fixed-control" && run.workerHarnessMode !== "self-hosted") throw new Error("engineering replay found an incomplete run with an invalid arm identity") |
| 571 | + assertEngineeringRunIdentity(run, config, campaign, run.workerHarnessMode, rootCommit, seed, runtime, expectedProfile) |
| 572 | + return { run, stopPath: await writeEngineeringStopReport(options, run) } |
490 | 573 | } |
491 | 574 |
|
492 | 575 | export async function executeEngineeringPair(options: Options, env: NodeJS.ProcessEnv = process.env, runner: typeof runRsi = runRsi): Promise<EngineeringEvidenceRecord | undefined> { |
@@ -509,9 +592,15 @@ export async function executeEngineeringPair(options: Options, env: NodeJS.Proce |
509 | 592 | const config = await validateRuntimeEvaluatorIntegrity(preview.execution.configs[0], options) |
510 | 593 | assertProofWorkerProfile(config, openRouterProfileById(preview.model.profileId ?? RSI_DEFAULT_OPENROUTER_PROFILE_ID), process.env) |
511 | 594 | const campaign = campaignId(preview.seeds.engineering) |
512 | | - const runs: RsiRunRecord[] = [] |
513 | | - for (const arm of ["fixed-control", "self-hosted"] as const) { |
514 | | - runs.push(await runEngineeringArm(config, campaign, arm, preview.rootCommit, preview.seeds.engineering, preview.runtime, runner)) |
| 595 | + const priorIncomplete = await writePriorIncompleteEngineeringStopReport(options, config, campaign, preview.rootCommit, preview.seeds.engineering, preview.runtime) |
| 596 | + if (priorIncomplete) { |
| 597 | + throw new Error(`engineering campaign already has an incomplete terminal run ${priorIncomplete.run.runId}; stop report: ${priorIncomplete.stopPath}; a new run identity is required`) |
| 598 | + } |
| 599 | + const runs = await runEngineeringArms(config, campaign, preview.rootCommit, preview.seeds.engineering, preview.runtime, runner) |
| 600 | + const incompleteRun = runs.find((run) => run.proofCampaign?.state === "incomplete") |
| 601 | + if (incompleteRun) { |
| 602 | + const stopPath = await writeEngineeringStopReport(options, incompleteRun) |
| 603 | + throw new Error(`engineering stopped with unresolved proof accounting in ${incompleteRun.workerHarnessMode ?? "unknown"}; stop report: ${stopPath}; no further arm or qualified engineering evidence was written`) |
515 | 604 | } |
516 | 605 | const archive = await readArchive(config.archiveDir) |
517 | 606 | const savedRuns = runs.map((run) => archive.runs.concat(archive.activeRuns).filter((entry) => entry.runId === run.runId)) |
|