Skip to content
116 changes: 39 additions & 77 deletions builder/build.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,6 @@ function allClaims(m) {
}

const tomlString = (text) => JSON.stringify(text);
const rustString = (text) => `"${text.replace(/\\/g, '\\\\').replace(/"/g, '\\"')}"`;

function idsToml(m, claims) {
const current = TEMPLATE['model/ids.toml'];
Expand All @@ -69,86 +68,49 @@ function scenarios(m, claims) {
return `${text.replace(/\n$/, '')}${facet.join('')}\n`;
}

/** `crates/conformance/tests/<suite>.rs`, in the layout `cargo fmt` gives it. */
export function rustTests(m, claims) {
const props = claims.filter((c) => c.kind === 'property');
const facets = claims.filter((c) => c.kind === 'facet');
const helpers = [
'use std::path::PathBuf;',
'',
'fn root() -> PathBuf {',
' PathBuf::from(env!("CARGO_MANIFEST_DIR"))',
' .ancestors()',
' .nth(2)',
' .expect("crates/conformance is two below the root")',
' .to_path_buf()',
'}',
'',
'/// The body of the top-level declaration named `name`, up to its closing brace.',
"fn declaration<'a>(model: &'a str, name: &str) -> &'a str {",
' let start = model',
' .find(&format!("\\n name: {name},\\n"))',
' .unwrap_or_else(|| panic!("{name} is not declared"));',
' let end = model[start..]',
' .find("\\n}\\n")',
' .unwrap_or_else(|| panic!("{name} is not closed"));',
' &model[start..start + end]',
'}',
'',
'fn module(path: &str) -> String {',
' std::fs::read_to_string(root().join(path)).expect("the module")',
'}',
];
if (props.length)
helpers.push(
'',
'/// The condition no request may meet, and the theorem that says none does.',
'fn pinned(breaks: &str, condition: &str, theorem: &str) {',
` let model = module(${rustString(`app/src/${m.proofs}.lex.tex`)});`,
' let found = declaration(&model, breaks);',
' assert!(',
' found.contains(condition),',
' "the condition of {breaks} changed:\\n{found}"',
' );',
' let holds = declaration(&model, theorem);',
' let statement = format!("statement: (({breaks} request) = false),");',
' assert!(',
' holds.contains(&statement),',
' "{theorem} must state that no request meets {breaks}:\\n{holds}"',
' );',
'}',
);
if (facets.length)
helpers.push(
'',
'/// A facet theorem: proved by `decide`, about exactly this predicate.',
'fn holds(path: &str, theorem: &str, statement: &str) {',
' let model = module(path);',
' let found = declaration(&model, theorem);',
' assert!(',
' found.contains(&format!("statement: {statement},")),',
' "{theorem} must state {statement}:\\n{found}"',
' );',
' assert!(',
' found.contains("proof: decide{}"),',
' "{theorem} is proved by decide"',
' );',
'}',
);
const tests = [
...props.map((c) => ['', `/// \`${c.id}\`: ${c.statement}`, '#[test]', `fn ${c.test}() {`, ' pinned(', ` ${rustString(c.breaks)},`, ` ${rustString(`body: ${c.condition}`)},`, ` ${rustString(c.theorem)},`, ' );', '}'].join('\n')),
...facets.map((c) => ['', `/// \`${c.id}\`: ${c.statement}`, '#[test]', `fn ${c.test}() {`, ' holds(', ` ${rustString(`app/src/${c.module}.lex.tex`)},`, ` ${rustString(c.theorem)},`, ` ${rustString(c.holds)},`, ' );', '}'].join('\n')),
];
/**
* `crates/conformance/tests/<suite>.rs`: one test per claim, of one app or a whole
* set, in one file: a helper `cargo fmt` lays out, then the tests, one line each, in a
* `#[rustfmt::skip]` module. Each test pins what its theorem
* is about (a property's condition and that no request meets it; a facet
* theorem's statement and that `decide` proves it), so a weakened statement
* that still verifies fails the gate. One line per claim keeps the generated
* Rust small, as the template's only-Prism ratchet asks.
*/
export function rustTests(suite, claims, proofs) {
const lit = (s) => JSON.stringify(s);
const pins = (c) =>
c.kind === 'property'
? [[c.proofs ?? proofs, c.breaks, `body: ${c.condition}`], [c.proofs ?? proofs, c.theorem, `statement: ((${c.breaks} request) = false),`]]
: [[c.module, c.theorem, `statement: ${c.holds},`], [c.module, c.theorem, 'proof: decide{}']];
// The repository's honesty gate reads each test's name from a `#[test]` line and the `fn` line after it.
const rows = claims.flatMap((c) => [
` // ${c.id}: ${c.statement.replace(/\s+/g, ' ')}`,
' #[test]',
` fn ${c.test}() { ${pins(c).map((p) => `super::pins(${p.map(lit).join(', ')});`).join(' ')} }`,
]);
return [
`//! The \`${m.suite}\` suite: one test per scenario in \`features/suites/${m.suite}.feature\`.`,
`//! The \`${suite}\` suite: one test per claim, each named for its ID.`,
'//!',
'//! Written by the Prism-DevRel builder from the app\'s prompt. The theorems are',
'//! Written by the Prism-DevRel builder from the prompts. The theorems are',
'//! checked by the Lean kernel when `just vv` runs `prismpm verify` on `app/`.',
'//! What these tests hold fixed is what each theorem is about, so a weakened',
'//! statement that still verifies fails the gate.',
'//! statement that still verifies fails the gate. One line per claim keeps the',
'//! generated Rust small, as the template\'s only-Prism ratchet asks.',
'',
...helpers,
...tests,
'/// The top-level declaration `name` of `app/src/<module>.lex.tex` contains `needle`.',
'fn pins(module: &str, name: &str, needle: &str) {',
' let model = std::fs::read_to_string(format!("../../app/src/{module}.lex.tex")).unwrap();',
' let found = &model[model.find(&format!("\\n name: {name},\\n")).expect(name)..];',
' let found = &found[..found.find("\\n}\\n").expect("closed")];',
' assert!(found.contains(needle), "{name}: {needle}\\n{found}");',
'}',
'',
'#[rustfmt::skip]',
'mod claims {',
'',
...rows,
'}',
'',
].join('\n');
}
Expand Down Expand Up @@ -219,7 +181,7 @@ export async function buildApp(input, { lastModified } = {}) {
const claims = allClaims(m);
files['model/ids.toml'] = idsToml(m, claims);
files[`features/suites/${m.suite}.feature`] = scenarios(m, claims);
files[`crates/conformance/tests/${m.suite}.rs`] = rustTests(m, claims);
files[`crates/conformance/tests/${m.suite}.rs`] = rustTests(m.suite, claims, m.proofs);
files['CONFORMANCE.md'] = conformance(m, claims);
files['site/app.json'] = appJson(m.spec, TEMPLATE['site/app.json'], { repository: m.prompt.repository ?? `OWNER/prism-${m.spec.name}` });
files[`openspec/specs/${m.spec.name}/spec.md`] = renderSpecDocument(m.spec, {});
Expand Down
4 changes: 2 additions & 2 deletions builder/facets.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -251,15 +251,15 @@ export function compliance(m) {
| List.cons item rest => (((project item index) =? position) && ((hasEvidence (project item evidence)) && (obligationsEvidenced (position + 1) rest))))`, { parameters: ['position : Nat', 'items : (List Obligation)'], recursive: 'items' }),
theorem('everyObligationEvidenced', '((obligationsEvidenced 0 (obligations)) = true)'),
];
return moduleText({ name: 'Compliance', glossaries: [BOOL, NAT, ARCH], imports: ['Architecture', 'Design', 'Quality', 'Security', ...(m.modules ?? [m.module])], title: 'standards reference', declarations: decls });
return moduleText({ name: 'Compliance', glossaries: [BOOL, NAT, ARCH], imports: ['Architecture', ...(m.withoutDesign ? [] : ['Design']), 'Quality', 'Security', ...(m.modules ?? [m.module])], title: 'standards reference', declarations: decls });
}

/** The facet modules, by path under app/. */
export function facetModules(m) {
return {
'src/Architecture.lex.tex': architecture(m),
'src/Compliance.lex.tex': compliance(m),
'src/Design.lex.tex': design(m),
...(m.withoutDesign ? {} : { 'src/Design.lex.tex': design(m) }),
'src/Features.lex.tex': features(m),
'src/Quality.lex.tex': quality(m),
'src/Security.lex.tex': security(m),
Expand Down
24 changes: 16 additions & 8 deletions builder/set.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -244,7 +244,8 @@ export function mergeModels(set, models) {
// ---- Compliance: one obligation per standard and subject, with every app's evidence.
const obligations = new Map();
for (const m of models)
for (const o of m.compliance) {
// prismpm verify does not regenerate the design/ files a set's merged views emit (PP5004), so a set ships no Design module yet.
for (const o of m.compliance.filter((x) => x.subject !== 'Design views')) {
// The three facet theorems are the set's; every other obligation is an app's, so no evidence list outgrows a list part.
const own = !['Architecture description', 'Product quality', 'Application security and risk', 'Control set'].includes(o.subject);
const subject = own ? `${m.title}: ${o.subject}` : o.subject;
Expand Down Expand Up @@ -272,6 +273,7 @@ export function mergeModels(set, models) {
security,
quality,
compliance: [...obligations.values()],
withoutDesign: true,
};
}

Expand Down Expand Up @@ -425,6 +427,7 @@ function scenarios(suite, purpose, claims) {

function siteJson(set, router, claims) {
const app = JSON.parse(TEMPLATE['site/app.json']);
app.promise = set.promise ?? { headline: set.title, detail: set.purpose };
const url = `https://github.com/${set.repository}`;
const [owner, repo] = set.repository.split('/');
const props = claims.filter((c) => c.kind === 'property');
Expand All @@ -446,7 +449,7 @@ function siteJson(set, router, claims) {
claim: props.map((c) => `${c.id}: ${c.statement.replace(/^The app/, `the ${c.app} app`)}.`).join(' '),
proposition: { name: 'dispatch', text: router.apps.map((m) => `${m.spec.name} request := ${m.module}.judge request`).join('\n'), plain: set.purpose },
theorems: props.map((c) => ({ name: c.theorem, statement: `no request meets ${c.breaks}: ${c.statement.replace(/^The app/, `the ${c.app} app`)}`, how: 'Proved by linear arithmetic, checked by the Lean kernel by prismpm verify in CI', where: `app/src/${c.proofs}.lex.tex` })),
pinned: props.map((c) => ({ name: c.test, what: `${c.id}'s condition and the theorem that no request meets it. A string-matching test, not a theorem: a planted defect fails it.`, where: `crates/conformance/tests/${c.suite}.rs` })),
pinned: props.map((c) => ({ name: c.test, what: `${c.id}'s condition and the theorem that no request meets it. A string-matching test, not a theorem: a planted defect fails it.`, where: SET_TESTS })),
attestations: {
proof: { id: 'not recorded: the proof ran in the SDK image, not on the UOR MCP server', url: `${url}/actions/workflows/bootstrap.yml` },
build: { id: 'printed by prismpm verify in every pages run', url: `${url}/actions/workflows/pages.yml` },
Expand Down Expand Up @@ -510,9 +513,10 @@ export async function buildSet(input, prompts, { lastModified, current = () => u
// lexlean.toml: the set's modules, and every other entrypoint the project already has.
const ownedSrc = Object.keys(files).filter((p) => p.startsWith('app/src/') && !p.startsWith('app/src/Foundation/') && !p.startsWith('app/src/Production/'));
const before = current('app/lexlean.toml') ?? '';
const kept = [...(/^entrypoints = \[(.*)\]$/m.exec(before)?.[1] ?? '').matchAll(/"([^"]+)"/g)].map((x) => x[1]).filter((e) => !ownedSrc.includes(`app/${e}`) && current(`app/${e}`) !== undefined);
const kept = [...(/^entrypoints = \[(.*)\]$/m.exec(before)?.[1] ?? '').matchAll(/"([^"]+)"/g)].map((x) => x[1]).filter((e) => !ownedSrc.includes(`app/${e}`) && !(host ? [] : [...TEMPLATE_EXAMPLE, 'app/src/Design.lex.tex']).includes(`app/${e}`) && current(`app/${e}`) !== undefined);
const entrypoints = [...new Set([...ownedSrc.map((p) => p.slice('app/'.length)), ...kept])].sort();
const lexicons = before.includes('[[lexicon_source]]') ? before.slice(before.indexOf('[[lexicon_source]]'), before.indexOf('[limits]')).replace(/\n*$/, '\n\n') : [['lexlean.std.nat', 'builtin'], ...['prism.arch', 'prism.qual', 'prism.sec'].map((p) => [p, 'path'])].map(([p, k]) => (k === 'builtin' ? `[[lexicon_source]]\npackage = "${p}"\nkind = "builtin"\n` : `[[lexicon_source]]\npackage = "${p}"\nkind = "path"\npath = "language/${p}"\n`)).join('\n') + '\n';
const declared = (text) => text + ['prism.arch', 'prism.qual', 'prism.sec'].filter((p) => !text.includes(`package = "${p}"`)).map((p) => `[[lexicon_source]]\npackage = "${p}"\nkind = "path"\npath = "language/${p}"\n\n`).join('');
const lexicons = before.includes('[[lexicon_source]]') ? declared(before.slice(before.indexOf('[[lexicon_source]]'), before.indexOf('[limits]')).replace(/\n*$/, '\n\n')) : [['lexlean.std.nat', 'builtin'], ...['prism.arch', 'prism.qual', 'prism.sec'].map((p) => [p, 'path'])].map(([p, k]) => (k === 'builtin' ? `[[lexicon_source]]\npackage = "${p}"\nkind = "builtin"\n` : `[[lexicon_source]]\npackage = "${p}"\nkind = "path"\npath = "language/${p}"\n`)).join('\n') + '\n';
if (host) {
const missing = ['prism.arch', 'prism.qual', 'prism.sec'].filter((p) => !before.includes(`package = "${p}"`)).map((p) => `[[lexicon_source]]\npackage = "${p}"\nkind = "path"\npath = "language/${p}"\n\n`).join('');
app('lexlean.toml', before.replace(/^entrypoints = \[.*\]$/m, `entrypoints = [${entrypoints.map((e) => `"${e}"`).join(', ')}]`).replace('[limits]', `${missing}[limits]`));
Expand Down Expand Up @@ -549,7 +553,6 @@ export async function buildSet(input, prompts, { lastModified, current = () => u
claims.push(...own);
if (own.length) {
files[`features/suites/${suite}.feature`] = scenarios(suite, m.spec.purpose, own);
files[`crates/conformance/tests/${suite}.rs`] = rustTests({ suite, proofs: m.proofs }, own);
}
}
const setPrefix = pascal(set.name).replace(/[a-z0-9]/g, '');
Expand All @@ -560,11 +563,16 @@ export async function buildSet(input, prompts, { lastModified, current = () => u
if ([...prefixes.values()].includes(setPrefix)) throw new SpecError(`The set's prefix ${setPrefix} is also an app's; rename the set.`, 'name');
claims.push(...facets);
files[`features/suites/${setSuite}.feature`] = scenarios(setSuite, set.purpose, facets);
files[`crates/conformance/tests/${setSuite}.rs`] = rustTests({ suite: setSuite }, facets);
const owned = new Set([setSuite, ...models.map((m) => m.suite)]);
// A new test file is new non-Prism code, so a fresh instance keeps the set's suite in the template's.
files[host ? `crates/conformance/tests/${setSuite}.rs` : SET_TESTS] = rustTests(setSuite, claims);
// The template's example app (suite `contract`) is replaced, so its register row goes with it.
const owned = new Set([setSuite, ...models.map((m) => m.suite), ...(host ? [] : ['contract'])]);
files['model/ids.toml'] = idsToml(current('model/ids.toml') ?? TEMPLATE['model/ids.toml'], owned, claims);
if (!host) files['site/app.json'] = siteJson(set, router, claims);
return { files, remove: [...TEMPLATE_EXAMPLE, 'app/prompt.json'], set, models, router, claims };
const perApp = [setSuite, ...models.map((m) => m.suite)].map((s) => `crates/conformance/tests/${s}.rs`);
return { files, remove: [...TEMPLATE_EXAMPLE, 'app/prompt.json', 'app/src/Design.lex.tex', ...perApp].filter((p) => !(p in files)), set, models, router, claims };
}

const SET_TESTS = 'crates/conformance/tests/contract.rs';

export { describeConditions };
Loading