diff --git a/.changeset/36491-template-provenance-card-sha.md b/.changeset/36491-template-provenance-card-sha.md new file mode 100644 index 00000000000..615219a66cc --- /dev/null +++ b/.changeset/36491-template-provenance-card-sha.md @@ -0,0 +1,8 @@ +--- +'@objectstack/scenarios': patch +--- + +`run --template F` warns when `F.provenance.json` beside the template names another `cardSha256` than the sha256 of the card file the run grades, so a verified build whose provenance went stale after a card edit is named before the card runs. The run still grades the card it loaded. + +Clause-②: no +Metadata-protocol: none diff --git a/docs/qa/scenarios/builds/travel_expense.provenance.json b/docs/qa/scenarios/builds/travel_expense.provenance.json index cd6ecc42715..69dcc0fec28 100644 --- a/docs/qa/scenarios/builds/travel_expense.provenance.json +++ b/docs/qa/scenarios/builds/travel_expense.provenance.json @@ -1,6 +1,6 @@ { "card": "travel_expense", - "cardSha256": "29162eefb46055941a0263686c2853246b4c408d41acb90c6abe22f68886a768", + "cardSha256": "212abedb6b59a5ac0c6921c25bdcc4dc057cbc25e866a9cd2ed962920368a00d", "build": "travel_expense.json", "sha256": "4ab7e603101e6adee0ede53563a13a9a830c8323d8e3f702222986042e726fd8", "runner": "ec227fc7", diff --git a/packages/qa/scenarios/README.md b/packages/qa/scenarios/README.md index 059fc93012e..304eeddd3bf 100644 --- a/packages/qa/scenarios/README.md +++ b/packages/qa/scenarios/README.md @@ -288,7 +288,9 @@ files only: no model, no network, and no score of its own — every figure is th - **Template and ask modes** author no first build: the row is the build they were supplied. `run --template F` writes the file's name and sha256 at the record's top level (`template`), so runs on one template share a row; a record that names no file (written before the key) is a row of its own, never summed with another; an ask mode on - `this run’s from-scratch build` takes that run's scratch author. + `this run’s from-scratch build` takes that run's scratch author. When `F.provenance.json` sits beside the + template and names the same card with another `cardSha256` than the sha256 of the card file the run grades, `run` + prints a `warning: template provenance:` line before the card runs, and grades the card anyway. - **Never summed silently**: a table's runs must share the prompt choice, the package source, the ask repeats and the driver (a replay is its own driver), the ask skills (`--ask-skills`: names and sha256; a record without them reads `none`), the door arm (`--modes door`, below; a record without one reads `skills: none · rules: none`), the card file they graded (`card`, below; a record without it reads `card: not recorded`), and the schema masking of the apps they booted (`--schema-masking off`, below; a record without it reads `schema masking: on`). diff --git a/packages/qa/scenarios/src/runner/cli.ts b/packages/qa/scenarios/src/runner/cli.ts index db493b8f7b1..7e5cbb4d804 100644 --- a/packages/qa/scenarios/src/runner/cli.ts +++ b/packages/qa/scenarios/src/runner/cli.ts @@ -142,7 +142,7 @@ import { execSync } from 'node:child_process'; import { mkdirSync, mkdtempSync, readFileSync, writeFileSync } from 'node:fs'; import { tmpdir } from 'node:os'; -import { dirname, join, resolve } from 'node:path'; +import { basename, dirname, join, resolve } from 'node:path'; import { fileURLToPath } from 'node:url'; import { formatRefusal, libraryOf, loadScenarios, readCapabilityAreas, type LoadedScenario } from '../loader.js'; @@ -470,6 +470,46 @@ export function loadedCardStamp(loaded: Pick string | undefined = (p) => { + try { + return readFileSync(p, 'utf8'); + } catch { + return undefined; + } + }, +): string | undefined { + const provenancePath = templateProvenancePath(path); + const text = read(provenancePath); + if (text === undefined) return undefined; + const file = basename(provenancePath); + let provenance: { card?: unknown; cardSha256?: unknown }; + try { + provenance = JSON.parse(text); + } catch { + return `template provenance: ${file} is not JSON, so nothing says which card the template was verified against`; + } + if (typeof provenance.card === 'string' && provenance.card !== card.id) return undefined; + if (typeof provenance.cardSha256 !== 'string') return `template provenance: ${file} names no cardSha256, so nothing says which ${card.id} card the template was verified against`; + if (provenance.cardSha256 === card.sha256) return undefined; + 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`; +} + export async function main(argv: readonly string[]): Promise { // `summary` reads records back; it runs nothing (`summary.ts`). if (argv[0] === 'summary') return summaryMain(argv.slice(1)); @@ -592,11 +632,14 @@ export async function main(argv: readonly string[]): Promise { if (loaded.card.asOf !== undefined && loaded.card.asOf !== scenario.card.asOf) { console.log(`dates: the card's ${loaded.card.asOf} is run as ${scenario.card.asOf}, every card date moved by the same days`); } + // The card file the run graded, as it was loaded (`CardStamp`): the record and the scorecard name its hash. + const card = loadedCardStamp(loaded); + // A verified build's provenance names the card file it was verified against; one that names another is stale. + const staleProvenance = args.template ? templateProvenanceWarning(args.template, card) : undefined; + if (staleProvenance) console.log(`warning: ${staleProvenance}`); publishedVersions = new Set(); 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 } : {}) }); const packages: PackageSource = workspace ? workspace.source : { source: 'published', versions: [...publishedVersions].sort() }; - // The card file the run graded, as it was loaded (`CardStamp`): the record and the scorecard name its hash. - const card = loadedCardStamp(loaded); 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); const file = join(outDir, `${date}-scenario-${scenario.card.id}.json`); writeFileSync(file, JSON.stringify(record, null, 2)); diff --git a/packages/qa/scenarios/test/runner/template-provenance.test.ts b/packages/qa/scenarios/test/runner/template-provenance.test.ts new file mode 100644 index 00000000000..eb9cf9aeb47 --- /dev/null +++ b/packages/qa/scenarios/test/runner/template-provenance.test.ts @@ -0,0 +1,58 @@ +// Copyright (c) 2026 ObjectStack. Licensed under the Apache-2.0 license. + +/** + * A verified build's provenance names the card file it was verified against + * (`builds/F.provenance.json` `cardSha256`, the scenarios README's Verified + * builds); `run --template F` warns when it names another card file + * than the one the run grades (`templateProvenanceWarning`, `src/runner/cli.ts`). + */ + +import { mkdtempSync, writeFileSync } from 'node:fs'; +import { tmpdir } from 'node:os'; +import { join } from 'node:path'; + +import { describe, expect, it } from 'vitest'; + +import { templateProvenancePath, templateProvenanceWarning } from '../../src/runner/cli.js'; + +const A = 'a'.repeat(64); +const B = 'b'.repeat(64); +const card = { id: 'travel_expense', sha256: A }; +const reading = (text: string | undefined) => () => text; + +describe('templateProvenanceWarning — run --template F against a card', () => { + it('reads the provenance beside the template', () => { + expect(templateProvenancePath('cards/builds/x.json')).toBe('cards/builds/x.provenance.json'); + }); + + it('says nothing when the provenance names the card file the run grades', () => { + expect(templateProvenanceWarning('t.json', card, reading(JSON.stringify({ card: 'travel_expense', cardSha256: A })))).toBeUndefined(); + }); + + it('warns, naming both hashes, when the provenance names another card file', () => { + const line = templateProvenanceWarning('t.json', card, reading(JSON.stringify({ card: 'travel_expense', cardSha256: B }))); + expect(line).toBeDefined(); + expect(line).toContain('t.provenance.json'); + expect(line).toContain(A.slice(0, 12)); + expect(line).toContain(B.slice(0, 12)); + }); + + it('warns when the provenance is unreadable as JSON or names no card hash', () => { + expect(templateProvenanceWarning('t.json', card, reading('{'))).toBeDefined(); + expect(templateProvenanceWarning('t.json', card, reading(JSON.stringify({ card: 'travel_expense' })))).toBeDefined(); + }); + + it("says nothing for a template with no provenance, or one verified on another card", () => { + expect(templateProvenanceWarning('t.json', card, reading(undefined))).toBeUndefined(); + expect(templateProvenanceWarning('t.json', card, reading(JSON.stringify({ card: 'order_to_cash', cardSha256: B })))).toBeUndefined(); + }); + + it('reads the provenance file from disk beside the template by default', () => { + const dir = mkdtempSync(join(tmpdir(), 'scenario-provenance-')); + const template = join(dir, 'travel_expense.json'); + writeFileSync(template, '{}'); + expect(templateProvenanceWarning(template, card)).toBeUndefined(); + writeFileSync(join(dir, 'travel_expense.provenance.json'), JSON.stringify({ card: 'travel_expense', cardSha256: B })); + expect(templateProvenanceWarning(template, card)).toContain(B.slice(0, 12)); + }); +});