Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions .changeset/36491-template-provenance-card-sha.md
Original file line number Diff line number Diff line change
@@ -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
2 changes: 1 addition & 1 deletion docs/qa/scenarios/builds/travel_expense.provenance.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"card": "travel_expense",
"cardSha256": "29162eefb46055941a0263686c2853246b4c408d41acb90c6abe22f68886a768",
"cardSha256": "212abedb6b59a5ac0c6921c25bdcc4dc057cbc25e866a9cd2ed962920368a00d",
"build": "travel_expense.json",
"sha256": "4ab7e603101e6adee0ede53563a13a9a830c8323d8e3f702222986042e726fd8",
"runner": "ec227fc7",
Expand Down
4 changes: 3 additions & 1 deletion packages/qa/scenarios/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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`).
Expand Down
49 changes: 46 additions & 3 deletions packages/qa/scenarios/src/runner/cli.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand Down Expand Up @@ -470,6 +470,46 @@ export function loadedCardStamp(loaded: Pick<LoadedScenario, 'fileSha256' | 'car
return { id: loaded.card.id, sha256: loaded.fileSha256 };
}

/** The provenance file beside a verified build: `builds/F.json` → `builds/F.provenance.json` (`docs/qa/scenarios/README.md`, Verified builds). */
export function templateProvenancePath(path: string): string {
return path.replace(/(\.json)?$/, '.provenance.json');
}

/**
* The line `run` prints when the `--template` file's provenance names another
* card file than the one the run grades: its `cardSha256` is the sha256 of the
* card file's bytes, the hash the record stamps (`CardStamp`), and it changes
* by PR with the card. Undefined when the two agree, when the template has no
* provenance file beside it, or when the provenance is another card's. A
* warning, never a refusal: the run still grades the card it loaded.
*/
export function templateProvenanceWarning(
path: string,
card: CardStamp,
read: (provenancePath: string) => 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<number> {
// `summary` reads records back; it runs nothing (`summary.ts`).
if (argv[0] === 'summary') return summaryMain(argv.slice(1));
Expand Down Expand Up @@ -592,11 +632,14 @@ export async function main(argv: readonly string[]): Promise<number> {
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));
Expand Down
58 changes: 58 additions & 0 deletions packages/qa/scenarios/test/runner/template-provenance.test.ts
Original file line number Diff line number Diff line change
@@ -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));
});
});
Loading