[content] DaySentence: Candidates held to every rule; library:verify for the author

This commit is contained in:
2026-11-02 08:55:06 +01:00
parent cdbaf56195
commit 5934523e45
6 changed files with 280 additions and 5 deletions
+30 -5
View File
@@ -5,14 +5,21 @@
* Passes when the library has no problems AND every fixture fails with the rule
* it names. A fixture that passes means a rule has stopped firing: that fails
* the run, the same as a bad sentence.
*
* It also holds the candidates in content/candidates/ to every rule but
* verification (day-sentence.md P8), and the app's build runs it first, so what
* ships is what passed (P5). ROSEA_LIBRARY_DIR swaps the library folder, for the
* e2e build only; the app's Vite config reads the same variable.
*/
import { readFileSync, readdirSync } from 'node:fs';
import { join } from 'node:path';
import { existsSync, readFileSync, readdirSync } from 'node:fs';
import { join, relative, resolve } from 'node:path';
import { fileURLToPath } from 'node:url';
import { validateCandidates } from '@rosea/content/candidates';
import { validateEntry, validateLibrary, type Rule } from '@rosea/content/validate';
const root = fileURLToPath(new URL('..', import.meta.url));
const libraryDir = join(root, 'content', 'library');
const libraryDir = resolve(root, process.env['ROSEA_LIBRARY_DIR'] ?? join('content', 'library'));
const candidatesDir = join(root, 'content', 'candidates');
const fixturesDir = join(root, 'content', 'fixtures');
const jsonIn = (dir: string): string[] =>
@@ -27,7 +34,7 @@ const entries: unknown[] = [];
for (const file of jsonIn(libraryDir)) {
const data: unknown = JSON.parse(readFileSync(join(libraryDir, file), 'utf8'));
if (!Array.isArray(data)) {
console.error(`content/library/${file}: must be an array of entries`);
console.error(`${relative(root, libraryDir)}/${file}: must be an array of entries`);
failed = true;
continue;
}
@@ -44,9 +51,27 @@ const sunset = entries.filter(
).length;
console.log(
`library: ${entries.length} entr${entries.length === 1 ? 'y' : 'ies'} in ${jsonIn(libraryDir).length} files, ${sunset} sunset (release needs 365)`
`library (${relative(root, libraryDir).replaceAll('\\', '/')}): ${entries.length} entr${entries.length === 1 ? 'y' : 'ies'} in ${jsonIn(libraryDir).length} files, ${sunset} sunset (release needs 365)`
);
// ── the candidates: proposed, not verified ─────────────────────────────────
const candidates: unknown[] = [];
const candidateFiles = existsSync(candidatesDir) ? jsonIn(candidatesDir) : [];
for (const file of candidateFiles) {
const data: unknown = JSON.parse(readFileSync(join(candidatesDir, file), 'utf8'));
if (!Array.isArray(data)) {
console.error(`content/candidates/${file}: must be an array of candidates`);
failed = true;
continue;
}
candidates.push(...data);
}
for (const p of validateCandidates(candidates, entries)) {
console.error(`candidate ${p.id} [${p.rule}] ${p.message}`);
failed = true;
}
console.log(`candidates: ${candidates.length} in ${candidateFiles.length} files, waiting for a person to verify them`);
// ── the fixtures: each must FAIL, with its named rule ───────────────────────
interface Fixture {
expect: Rule;
+59
View File
@@ -0,0 +1,59 @@
/**
* `pnpm library:verify <id…> --by <name>` — the author's command (day-sentence.md P8).
*
* Run it only after reading each sentence at its `check.url` and confirming the words
* and their public-domain status yourself. It moves those candidates from
* content/candidates/ into content/library/, stamped with your name and today's
* date, and refuses (changing nothing) if the library would then fail
* `validate:library`.
*
* **Agents never run this.** `verifiedBy` records that a person checked the source
* (phase0-workspace.md D4); an agent that runs it makes that record false.
*/
import { readFileSync, readdirSync, writeFileSync, existsSync } from 'node:fs';
import { join } from 'node:path';
import { fileURLToPath } from 'node:url';
import { verifyCandidates, type Files } from '@rosea/content/candidates';
import { localDay } from '@rosea/engine';
const root = fileURLToPath(new URL('..', import.meta.url));
const libraryDir = join(root, 'content', 'library');
const candidatesDir = join(root, 'content', 'candidates');
const args = process.argv.slice(2);
const byAt = args.indexOf('--by');
const by = byAt >= 0 ? (args[byAt + 1] ?? '') : '';
const ids = args.filter((a, i) => a !== '--by' && i !== byAt + 1 && !a.startsWith('--'));
if (byAt < 0 || ids.length === 0) {
console.error('usage: pnpm library:verify <id> [<id>…] --by <your name>');
process.exit(2);
}
const read = (dir: string): Files => {
if (!existsSync(dir)) return {};
const files: Record<string, unknown[]> = {};
for (const f of readdirSync(dir).filter((f) => f.endsWith('.json')).sort()) {
const data: unknown = JSON.parse(readFileSync(join(dir, f), 'utf8'));
if (!Array.isArray(data)) throw new Error(`${dir}/${f}: must be an array`);
files[f] = data;
}
return files;
};
// The day on this machine's own clock and zone, as a person would write it.
const day = localDay(Date.now(), Intl.DateTimeFormat().resolvedOptions().timeZone);
const result = verifyCandidates({ candidates: read(candidatesDir), library: read(libraryDir), ids, by, day });
if (!result.ok) {
for (const p of result.problems) console.error(p);
console.error('Nothing was moved.');
process.exit(1);
}
const write = (dir: string, files: Files): void => {
for (const [f, entries] of Object.entries(files)) writeFileSync(join(dir, f), `${JSON.stringify(entries, null, 2)}\n`);
};
write(libraryDir, result.library);
write(candidatesDir, result.candidates);
console.log(`Verified by ${by.trim()} on ${day}: ${result.moved.join(', ')}. Run pnpm validate:library, then commit.`);