|
142 | 142 | import { execSync } from 'node:child_process'; |
143 | 143 | import { mkdirSync, mkdtempSync, readFileSync, writeFileSync } from 'node:fs'; |
144 | 144 | import { tmpdir } from 'node:os'; |
145 | | -import { dirname, join, resolve } from 'node:path'; |
| 145 | +import { basename, dirname, join, resolve } from 'node:path'; |
146 | 146 | import { fileURLToPath } from 'node:url'; |
147 | 147 |
|
148 | 148 | import { formatRefusal, libraryOf, loadScenarios, readCapabilityAreas, type LoadedScenario } from '../loader.js'; |
@@ -470,6 +470,46 @@ export function loadedCardStamp(loaded: Pick<LoadedScenario, 'fileSha256' | 'car |
470 | 470 | return { id: loaded.card.id, sha256: loaded.fileSha256 }; |
471 | 471 | } |
472 | 472 |
|
| 473 | +/** The provenance file beside a verified build: `builds/F.json` → `builds/F.provenance.json` (`docs/qa/scenarios/README.md`, Verified builds). */ |
| 474 | +export function templateProvenancePath(path: string): string { |
| 475 | + return path.replace(/(\.json)?$/, '.provenance.json'); |
| 476 | +} |
| 477 | + |
| 478 | +/** |
| 479 | + * The line `run` prints when the `--template` file's provenance names another |
| 480 | + * card file than the one the run grades: its `cardSha256` is the sha256 of the |
| 481 | + * card file's bytes, the hash the record stamps (`CardStamp`), and it changes |
| 482 | + * by PR with the card. Undefined when the two agree, when the template has no |
| 483 | + * provenance file beside it, or when the provenance is another card's. A |
| 484 | + * warning, never a refusal: the run still grades the card it loaded. |
| 485 | + */ |
| 486 | +export function templateProvenanceWarning( |
| 487 | + path: string, |
| 488 | + card: CardStamp, |
| 489 | + read: (provenancePath: string) => string | undefined = (p) => { |
| 490 | + try { |
| 491 | + return readFileSync(p, 'utf8'); |
| 492 | + } catch { |
| 493 | + return undefined; |
| 494 | + } |
| 495 | + }, |
| 496 | +): string | undefined { |
| 497 | + const provenancePath = templateProvenancePath(path); |
| 498 | + const text = read(provenancePath); |
| 499 | + if (text === undefined) return undefined; |
| 500 | + const file = basename(provenancePath); |
| 501 | + let provenance: { card?: unknown; cardSha256?: unknown }; |
| 502 | + try { |
| 503 | + provenance = JSON.parse(text); |
| 504 | + } catch { |
| 505 | + return `template provenance: ${file} is not JSON, so nothing says which card the template was verified against`; |
| 506 | + } |
| 507 | + if (typeof provenance.card === 'string' && provenance.card !== card.id) return undefined; |
| 508 | + if (typeof provenance.cardSha256 !== 'string') return `template provenance: ${file} names no cardSha256, so nothing says which ${card.id} card the template was verified against`; |
| 509 | + if (provenance.cardSha256 === card.sha256) return undefined; |
| 510 | + return `template provenance: ${file} names card ${card.id} sha256 ${provenance.cardSha256.slice(0, 12)}, but the card this run grades is sha256 ${card.sha256.slice(0, 12)} — refresh its cardSha256 by PR`; |
| 511 | +} |
| 512 | + |
473 | 513 | export async function main(argv: readonly string[]): Promise<number> { |
474 | 514 | // `summary` reads records back; it runs nothing (`summary.ts`). |
475 | 515 | if (argv[0] === 'summary') return summaryMain(argv.slice(1)); |
@@ -592,11 +632,14 @@ export async function main(argv: readonly string[]): Promise<number> { |
592 | 632 | if (loaded.card.asOf !== undefined && loaded.card.asOf !== scenario.card.asOf) { |
593 | 633 | console.log(`dates: the card's ${loaded.card.asOf} is run as ${scenario.card.asOf}, every card date moved by the same days`); |
594 | 634 | } |
| 635 | + // The card file the run graded, as it was loaded (`CardStamp`): the record and the scorecard name its hash. |
| 636 | + const card = loadedCardStamp(loaded); |
| 637 | + // A verified build's provenance names the card file it was verified against; one that names another is stale. |
| 638 | + const staleProvenance = args.template ? templateProvenanceWarning(args.template, card) : undefined; |
| 639 | + if (staleProvenance) console.log(`warning: ${staleProvenance}`); |
595 | 640 | publishedVersions = new Set(); |
596 | 641 | const cardRun = await runCard(scenario, driver, ops, { modes: args.modes, iterateRun: args.iterateRun, admin: ADMIN, ...(template ? { templateArtifact: template } : {}), ...(args.askRepeats !== undefined ? { askRepeats: args.askRepeats } : {}), ...(promptChoice ? { promptChoice } : {}), ...(schemaMasking === 'off' ? { schemaMasking } : {}) }); |
597 | 642 | const packages: PackageSource = workspace ? workspace.source : { source: 'published', versions: [...publishedVersions].sort() }; |
598 | | - // The card file the run graded, as it was loaded (`CardStamp`): the record and the scorecard name its hash. |
599 | | - const card = loadedCardStamp(loaded); |
600 | 643 | const record = withSuppliedTemplate(runRecord({ run: `${date}-scenario-${scenario.card.id}`, date, ...(framework ? { framework } : {}), packages, driver: info, runner: 'scenario-runner', evidenceDir: work, card, ...(schemaMasking === 'off' ? { schemaMasking: 'off' as const } : {}) }, cardRun), cardRun, supplied); |
601 | 644 | const file = join(outDir, `${date}-scenario-${scenario.card.id}.json`); |
602 | 645 | writeFileSync(file, JSON.stringify(record, null, 2)); |
|
0 commit comments