fwdloop 0.21.0 → 0.23.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/bin/fwdloop +85 -34
- package/package.json +4 -3
- package/src/ask.js +20 -4
- package/src/audit-fold.js +5 -1
- package/src/authoring.js +36 -6
- package/src/canrun.js +5 -0
- package/src/checked.js +32 -4
- package/src/cited-record.js +138 -0
- package/src/cited-source.js +49 -0
- package/src/citedcheck.js +321 -0
- package/src/citedwords.js +197 -0
- package/src/closers.js +37 -198
- package/src/csv.js +69 -12
- package/src/ctx-tools.js +2 -14
- package/src/declaration.js +143 -5
- package/src/diary.js +2 -2
- package/src/docx.js +16 -2
- package/src/draft-steps.js +1 -1
- package/src/drafter.js +322 -33
- package/src/flow.js +33 -7
- package/src/flowmodel.js +21 -0
- package/src/input-facts.js +20 -3
- package/src/model-step.js +38 -21
- package/src/pageask.js +136 -0
- package/src/pagebits.js +107 -0
- package/src/pagedropwords.js +171 -0
- package/src/pagedry.js +120 -0
- package/src/pagefetch.js +164 -0
- package/src/pagelook.js +155 -0
- package/src/pagemap.js +55 -0
- package/src/pageplace.js +103 -0
- package/src/pagerules.js +318 -0
- package/src/panel/author.js +43 -21
- package/src/panel/authorcard.js +20 -3
- package/src/panel/authorflows.js +21 -15
- package/src/panel/authorstart.js +2 -4
- package/src/panel/authortrigger.js +14 -4
- package/src/panel/data.js +102 -6
- package/src/panel/index.html +321 -12
- package/src/panel/result.js +301 -0
- package/src/panel/server.js +27 -3
- package/src/primitives.js +2 -3
- package/src/provider.js +5 -2
- package/src/redact.js +17 -0
- package/src/runner.js +102 -14
- package/src/schedule.js +3 -3
- package/src/setup.js +16 -1
- package/src/sheet-record.js +57 -0
- package/src/sheetblock.js +170 -0
- package/src/sheetchecks.js +206 -0
- package/src/sheetclaims.js +78 -0
- package/src/sheetfind.js +50 -0
- package/src/sheetlook.js +157 -0
- package/src/sheetoffer.js +125 -0
- package/src/sheetrun.js +105 -0
- package/src/signed-text.js +11 -1
- package/src/thinking.js +1 -1
- package/src/try-checks.js +12 -0
- package/src/types.js +4 -11
- package/types/ask.d.ts +2 -1
- package/types/authoring.d.ts +19 -2
- package/types/checked.d.ts +19 -3
- package/types/cited-record.d.ts +21 -0
- package/types/cited-source.d.ts +18 -0
- package/types/citedcheck.d.ts +95 -0
- package/types/citedwords.d.ts +97 -0
- package/types/closers.d.ts +37 -10
- package/types/csv.d.ts +25 -4
- package/types/ctx-tools.d.ts +0 -1
- package/types/declaration.d.ts +20 -1
- package/types/docx.d.ts +14 -0
- package/types/drafter.d.ts +264 -8
- package/types/flow.d.ts +21 -0
- package/types/flowmodel.d.ts +15 -0
- package/types/input-facts.d.ts +12 -2
- package/types/model-step.d.ts +40 -19
- package/types/pageask.d.ts +75 -0
- package/types/pagebits.d.ts +32 -0
- package/types/pagedropwords.d.ts +40 -0
- package/types/pagedry.d.ts +38 -0
- package/types/pagefetch.d.ts +86 -0
- package/types/pagelook.d.ts +75 -0
- package/types/pagemap.d.ts +31 -0
- package/types/pageplace.d.ts +22 -0
- package/types/pagerules.d.ts +86 -0
- package/types/panel/authorcard.d.ts +4 -1
- package/types/panel/authorflows.d.ts +2 -0
- package/types/panel/data.d.ts +26 -0
- package/types/panel/result.d.ts +156 -0
- package/types/provider.d.ts +3 -1
- package/types/redact.d.ts +1 -0
- package/types/runner.d.ts +162 -9
- package/types/schedule.d.ts +0 -11
- package/types/setup.d.ts +13 -0
- package/types/sheet-record.d.ts +24 -0
- package/types/sheetblock.d.ts +116 -0
- package/types/sheetchecks.d.ts +70 -0
- package/types/sheetclaims.d.ts +26 -0
- package/types/sheetfind.d.ts +46 -0
- package/types/sheetlook.d.ts +120 -0
- package/types/sheetoffer.d.ts +25 -0
- package/types/sheetrun.d.ts +35 -0
- package/types/thinking.d.ts +1 -1
- package/types/types.d.ts +27 -14
|
@@ -0,0 +1,138 @@
|
|
|
1
|
+
// borrowed-from: bareloop src/panel/server.js@2959217 (proofOf, moneyAsRead, resultCell - the row a Result tab shows, read off bareguard's evidence)
|
|
2
|
+
// M10 piece 4: the ONE writer of `<run>/cited-checks.json`, the record the Result tab's reader (`readCitedChecks`, src/panel/result.js) takes. One line per
|
|
3
|
+
// job line, written the moment that line's cited check has run, from the check's own answer: nothing here decides a verdict.
|
|
4
|
+
//
|
|
5
|
+
// Shape (the reader's, copied from bareloop): { lines: [ { n, source, passed, facts: [{name, kind: 'phrase'|'value', sentence}],
|
|
6
|
+
// rows: [{n, quote, cells: [one text per fact], proof?: {text, marks}}] | null, notChecked: string[] } ] }.
|
|
7
|
+
// Extra keys the reader ignores and the separate checker (scripts/check-cited-run.mjs) reads: per line `check` {sha256 of the frozen text, the signed facts,
|
|
8
|
+
// the count} and per row `values`.
|
|
9
|
+
// Throws away: bareguard's evidence beyond the marks of the claim's own spans; nothing: a red line keeps its judged rows (am9); only a stopped check has none.
|
|
10
|
+
import { writeFileSync, renameSync } from 'node:fs';
|
|
11
|
+
import { join } from 'node:path';
|
|
12
|
+
import { readFileInside } from './flow.js';
|
|
13
|
+
import { moneyAs } from './pageask.js';
|
|
14
|
+
import { factSentence, countSentence, CITED_ID } from './citedcheck.js';
|
|
15
|
+
|
|
16
|
+
/** the record's name in the run's own folder; the Result tab's reader takes it from here */
|
|
17
|
+
export const CITED_CHECKS_FILE = 'cited-checks.json';
|
|
18
|
+
|
|
19
|
+
const isRec = (/** @type {unknown} */ v) => v !== null && typeof v === 'object' && !Array.isArray(v);
|
|
20
|
+
|
|
21
|
+
/**
|
|
22
|
+
* What a result's check read, from bareguard's `full.evidence` entry for the claim, for the proof fold. `null` = no evidence, or an entry that is not about
|
|
23
|
+
* this quote: the page then shows the plain quote. `text` is bareguard's normalized quote; `marks` are `[start, end]` UTF-16 offsets into it, sorted,
|
|
24
|
+
* overlaps merged. Every span the check read EXCEPT a bind-only membership token that is not this claim's (`canon` present, `matched` absent).
|
|
25
|
+
* @param {any} ev @param {unknown} quote @returns {{text: string, marks: number[][]}|null}
|
|
26
|
+
*/
|
|
27
|
+
function proofOf(ev, quote) {
|
|
28
|
+
if (!ev || typeof ev.text !== 'string' || !Array.isArray(ev.spans) || typeof quote !== 'string') return null;
|
|
29
|
+
if (quote.replace(/\*\*|__/g, '').replace(/\s+/g, ' ').trim() !== ev.text) return null;
|
|
30
|
+
const spans = ev.spans
|
|
31
|
+
.filter((/** @type {any} */ sp) => sp && Number.isInteger(sp.start) && Number.isInteger(sp.end) && sp.start >= 0 && sp.end > sp.start && sp.end <= ev.text.length
|
|
32
|
+
&& !(sp.canon !== undefined && sp.matched !== true))
|
|
33
|
+
.map((/** @type {any} */ sp) => [sp.start, sp.end])
|
|
34
|
+
.sort((/** @type {number[]} */ x, /** @type {number[]} */ y) => x[0] - y[0] || x[1] - y[1]);
|
|
35
|
+
/** @type {number[][]} */
|
|
36
|
+
const marks = [];
|
|
37
|
+
for (const sp of spans) {
|
|
38
|
+
const last = marks[marks.length - 1];
|
|
39
|
+
if (last && sp[0] < last[1]) last[1] = Math.max(last[1], sp[1]);
|
|
40
|
+
else marks.push([sp[0], sp[1]]);
|
|
41
|
+
}
|
|
42
|
+
return { text: ev.text, marks };
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
/**
|
|
46
|
+
* A money fact's amount as the page wrote it ("144 US dollars"): the quote's text from the first to the last of the value's own currency / unit / value spans
|
|
47
|
+
* in bareguard's evidence for this claim. `null` = nothing located, or more than a short run (the caller keeps the fact's own spelling).
|
|
48
|
+
* @param {any} ev @param {number} j the fact's index (bareguard's decide index) @returns {string|null}
|
|
49
|
+
*/
|
|
50
|
+
function moneyAsRead(ev, j) {
|
|
51
|
+
const own = ev.spans.filter((/** @type {any} */ sp) => sp && sp.decide === j && ['value', 'currency', 'unit'].includes(sp.role)
|
|
52
|
+
&& Number.isInteger(sp.start) && Number.isInteger(sp.end) && sp.start >= 0 && sp.end > sp.start && sp.end <= ev.text.length
|
|
53
|
+
&& !(sp.canon !== undefined && sp.matched !== true));
|
|
54
|
+
if (own.length === 0) return null;
|
|
55
|
+
const t = ev.text.slice(Math.min(...own.map((/** @type {any} */ sp) => sp.start)), Math.max(...own.map((/** @type {any} */ sp) => sp.end))).trim();
|
|
56
|
+
return t !== '' && t.length <= 40 ? t : null;
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
/** a result value as the step wrote it, for a table cell */
|
|
60
|
+
function cellText(/** @type {unknown} */ v) {
|
|
61
|
+
if (typeof v === 'string') return v;
|
|
62
|
+
if (typeof v === 'number' && Number.isFinite(v)) return String(v);
|
|
63
|
+
return '';
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
/**
|
|
67
|
+
* One job line's record from the check's own answer.
|
|
68
|
+
* @param {{n: number, part: any, results: unknown, check: Awaited<ReturnType<typeof import('./citedcheck.js').checkCited>>, confirmed?: string[]}} o
|
|
69
|
+
* `confirmed` = names of facts whose limit the person agreed to (the Result tab's "(you confirmed)")
|
|
70
|
+
* @returns {Record<string, any>}
|
|
71
|
+
*/
|
|
72
|
+
export function citedLineOf({
|
|
73
|
+
n, part, results, check, confirmed = [],
|
|
74
|
+
}) {
|
|
75
|
+
const passed = check.verdict === 'green';
|
|
76
|
+
const list = Array.isArray(results) ? results : [];
|
|
77
|
+
/** @type {any[]|null} */
|
|
78
|
+
let rows = null;
|
|
79
|
+
// M10 am9 item 1: a red line keeps the results the check judged (bareloop's readJudged); `passed` stays false, so the page never ticks it. A stopped check
|
|
80
|
+
// judged nothing: no rows.
|
|
81
|
+
if (passed || check.verdict === 'red') {
|
|
82
|
+
rows = list.map((/** @type {any} */ r, /** @type {number} */ k) => {
|
|
83
|
+
const values = isRec(r) && isRec(r.values) ? r.values : {};
|
|
84
|
+
const quote = isRec(r) && typeof r.quote === 'string' ? r.quote : '';
|
|
85
|
+
const ev = check.evidence.find((/** @type {any} */ e) => e && e.check === CITED_ID && e.claim === k) ?? null;
|
|
86
|
+
const proof = proofOf(ev, quote);
|
|
87
|
+
return {
|
|
88
|
+
n: k + 1,
|
|
89
|
+
quote,
|
|
90
|
+
...(proof ? { proof } : {}),
|
|
91
|
+
cells: part.facts.map((/** @type {any} */ f, /** @type {number} */ j) => {
|
|
92
|
+
if (f.shape === 'phrase') return String(f.phrase);
|
|
93
|
+
const v = cellText(values[f.name]);
|
|
94
|
+
if (f.shape === 'money') return (proof ? moneyAsRead(ev, j) : null) ?? (/^\d[\d.,]*$/.test(v.trim()) ? moneyAs(f, v.trim(), true) : v);
|
|
95
|
+
return v;
|
|
96
|
+
}),
|
|
97
|
+
values: Object.fromEntries(part.facts.filter((/** @type {any} */ f) => f.shape !== 'phrase').map((/** @type {any} */ f) => [f.name, cellText(values[f.name])])),
|
|
98
|
+
};
|
|
99
|
+
});
|
|
100
|
+
}
|
|
101
|
+
const countRow = countSentence(part);
|
|
102
|
+
return {
|
|
103
|
+
n,
|
|
104
|
+
source: part.source,
|
|
105
|
+
passed,
|
|
106
|
+
facts: part.facts.map((/** @type {any} */ f) => ({
|
|
107
|
+
name: f.name, kind: f.shape === 'phrase' ? 'phrase' : 'value', sentence: factSentence(f, part.source), ...(confirmed.includes(f.name) ? { confirmed: true } : {}),
|
|
108
|
+
})),
|
|
109
|
+
rows,
|
|
110
|
+
notChecked: check.verdict === 'stopped' && check.fault ? [check.fault] : [],
|
|
111
|
+
check: {
|
|
112
|
+
sha256: check.sha256, facts: part.facts.map((/** @type {any} */ f) => ({ ...f })), ...(part.count === undefined ? {} : { count: { ...part.count } }), ...(countRow ? { countSentence: countRow } : {}),
|
|
113
|
+
},
|
|
114
|
+
};
|
|
115
|
+
}
|
|
116
|
+
|
|
117
|
+
/**
|
|
118
|
+
* Write one job line into the run's record: the line with the same `n` is replaced (a later try of the same step wins), the others stay, lines are kept in
|
|
119
|
+
* order. An unreadable existing record is started again (this function is its only writer). Atomic: a reader sees the old file or the new one.
|
|
120
|
+
* @param {string} runDir @param {Record<string, any>} line @returns {void}
|
|
121
|
+
*/
|
|
122
|
+
export function writeCitedLine(runDir, line) {
|
|
123
|
+
const file = join(runDir, CITED_CHECKS_FILE);
|
|
124
|
+
/** @type {Record<string, any>[]} */
|
|
125
|
+
let lines = [];
|
|
126
|
+
const had = readFileInside(runDir, CITED_CHECKS_FILE);
|
|
127
|
+
if (had.ok) {
|
|
128
|
+
try {
|
|
129
|
+
const rec = JSON.parse(had.text);
|
|
130
|
+
if (rec && Array.isArray(rec.lines)) lines = rec.lines.filter((/** @type {any} */ l) => isRec(l) && l.n !== line.n);
|
|
131
|
+
} catch { lines = []; }
|
|
132
|
+
}
|
|
133
|
+
lines.push(line);
|
|
134
|
+
lines.sort((a, b) => a.n - b.n);
|
|
135
|
+
const tmp = `${file}.tmp`;
|
|
136
|
+
writeFileSync(tmp, `${JSON.stringify({ lines }, null, 2)}\n`);
|
|
137
|
+
renameSync(tmp, file);
|
|
138
|
+
}
|
|
@@ -0,0 +1,49 @@
|
|
|
1
|
+
// M10 piece 4: the text a cited check reads - THE RUN'S OWN frozen copy of a web page or a text/markdown doc (`<run>/inputs/<file>`, written at run start by
|
|
2
|
+
// `freezeInputs`), checked against the sha256 `inputs.json` recorded when it was frozen. The drafting copy lives in the draft folder and is never read here.
|
|
3
|
+
// Same three outcomes as the CSV reader (`readInputCells`): not a text input of this run, a refusal that names why, or the text.
|
|
4
|
+
// A .docx input (M10 piece 5, item 2 "a quote in a doc") is served as its paragraph text (src/docx.js says what that throws away), its sha256 taken over the file's BYTES from the same
|
|
5
|
+
// read the text comes from. Throws away: nothing for a page or a text file. A .csv input is not served here (it is read as cells).
|
|
6
|
+
// A refusal carries `code`: 'no-manifest' / 'not-an-input' / 'not-text' (this is not a text source of this run) or 'unreadable' (it is one, and it cannot be trusted:
|
|
7
|
+
// the copy is missing or changed); a step that merely READS a role (src/runner.js readArtifactsMapChecked) halts only on 'unreadable'.
|
|
8
|
+
import { createHash } from 'node:crypto';
|
|
9
|
+
import { basename } from 'node:path';
|
|
10
|
+
import { readFileInside, resolveInside } from './flow.js';
|
|
11
|
+
import { readDocxSource } from './docx.js';
|
|
12
|
+
|
|
13
|
+
/** the frozen files served as text: a saved page (.txt) or a text/markdown document. Any other kind (a spreadsheet, a .docx, a binary) is never served as text. */
|
|
14
|
+
const TEXT_EXTS = Object.freeze(['.txt', '.md', '.markdown']);
|
|
15
|
+
|
|
16
|
+
/**
|
|
17
|
+
* @param {string} runDir the run's own folder @param {string} role the signed input's role
|
|
18
|
+
* @param {{docx?: boolean}} [o] `docx`: also serve a .docx as its text. Off by default: a step that merely READS a role is never handed a .docx here (it has `readDocx`); only the
|
|
19
|
+
* cited CHECK asks for it, so a quote a step took from a .docx is proved on the same text `readDocx` gives.
|
|
20
|
+
* @returns {{ok: true, text: string, sha256: string, frozen: string}|{ok: false, red: string, code: 'no-manifest'|'not-an-input'|'not-text'|'unreadable'}}
|
|
21
|
+
*/
|
|
22
|
+
export function readRunSource(runDir, role, { docx = false } = {}) {
|
|
23
|
+
const manifest = readFileInside(runDir, 'inputs.json');
|
|
24
|
+
if (!manifest.ok) return { ok: false, code: manifest.missing ? 'no-manifest' : 'unreadable', red: manifest.missing ? `input "${role}": this run has no frozen inputs` : `input "${role}": ${manifest.red}` };
|
|
25
|
+
let entries;
|
|
26
|
+
try { entries = JSON.parse(manifest.text); } catch (err) { return { ok: false, code: 'unreadable', red: `input "${role}": inputs.json is not valid JSON — ${err.message}` }; }
|
|
27
|
+
const entry = Array.isArray(entries) ? entries.find((e) => e?.id === role) : undefined;
|
|
28
|
+
if (!entry || typeof entry.frozen !== 'string' || typeof entry.sha256 !== 'string') return { ok: false, code: 'not-an-input', red: `input "${role}": this run froze no input with that name` };
|
|
29
|
+
const lower = entry.frozen.toLowerCase();
|
|
30
|
+
if (docx && lower.endsWith('.docx')) return readRunDocx(runDir, role, entry);
|
|
31
|
+
if (!TEXT_EXTS.some((x) => lower.endsWith(x))) return { ok: false, code: 'not-text', red: `input "${role}": a quote can be checked against a web page, a text file or a .docx here, not "${basename(entry.frozen)}"` };
|
|
32
|
+
const file = readFileInside(runDir, `inputs/${basename(entry.frozen)}`);
|
|
33
|
+
if (!file.ok) return { ok: false, code: 'unreadable', red: file.missing ? `input "${role}": the frozen copy is missing` : `input "${role}": ${file.red}` };
|
|
34
|
+
const sha256 = createHash('sha256').update(file.text, 'utf8').digest('hex');
|
|
35
|
+
if (sha256 !== entry.sha256) return { ok: false, code: 'unreadable', red: `input "${role}": the frozen copy changed since it was frozen` };
|
|
36
|
+
return { ok: true, text: file.text, sha256, frozen: basename(entry.frozen) };
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
/** the run's own frozen .docx, through the same safe path check as every other read (no symlink, nothing outside the run folder), proved against the recorded sha256
|
|
40
|
+
* @param {string} runDir @param {string} role @param {{frozen: string, sha256: string}} entry
|
|
41
|
+
* @returns {{ok: true, text: string, sha256: string, frozen: string}|{ok: false, red: string, code: 'unreadable'}} */
|
|
42
|
+
function readRunDocx(runDir, role, entry) {
|
|
43
|
+
const where = resolveInside(runDir, `inputs/${basename(entry.frozen)}`);
|
|
44
|
+
if (!where.ok) return { ok: false, code: 'unreadable', red: where.missing ? `input "${role}": the frozen copy is missing` : `input "${role}": ${where.red}` };
|
|
45
|
+
const doc = readDocxSource(where.full);
|
|
46
|
+
if (!doc.ok) return { ok: false, code: 'unreadable', red: `input "${role}": ${doc.red}` };
|
|
47
|
+
if (doc.sha256 !== entry.sha256) return { ok: false, code: 'unreadable', red: `input "${role}": the frozen copy changed since it was frozen` };
|
|
48
|
+
return { ok: true, text: doc.text, sha256: doc.sha256, frozen: basename(entry.frozen) };
|
|
49
|
+
}
|
|
@@ -0,0 +1,321 @@
|
|
|
1
|
+
// borrowed-from: bareloop src/facts.js@0a34846 (FACT_SHAPES, MAX_FACTS, factDecides, factClaims, countSentence, plainFactItem; factSentence without the AI-proposal tail)
|
|
2
|
+
// borrowed-from: bareloop src/rubricstep.js@0a34846 (buildStepSpec fact branch, flattenStep fact branch, gradeStep core, plainStepGap fact branch)
|
|
3
|
+
// M10 piece 4 (docs/wiki/the-module-ladder.md "M10", item 2, "green grows"): a value points to its source - a quote on the run's own saved page, or a
|
|
4
|
+
// quote in a doc. The check is bareguard's `cited` + `decides`, built by code from a fact part: the quote is word for word in the frozen text, every
|
|
5
|
+
// value is in the quote (compared by kind), every value passes its number rule (under / over / between), and the results are as many as the part says
|
|
6
|
+
// (a harness-set count with a min and a max). No model grades. The claim TEXT bareguard's number scan reads is written here from the values, never by
|
|
7
|
+
// the step.
|
|
8
|
+
//
|
|
9
|
+
// A fact part is `{source: <input role>, facts: [{name, shape, unit?, currency?, phrase?, anchor?, until?, rule?}], count?: {min?, max?}}`. Since M10 am7 item 1
|
|
10
|
+
// the DRAFTER writes it (`close.cited`) and src/citedwords.js proves it at $0; `factsOfLook` is what the page look says a part must hold for a place (R2c).
|
|
11
|
+
//
|
|
12
|
+
// Throws away: a kept fact's alternative labels beyond the first (bareguard signs ONE anchor per fact); a place word R2a / R2b dropped (src/pageplace.js
|
|
13
|
+
// says why in its `trace`; the phrases left are ALL signed, R2c). The evidence offsets are bareguard's and are carried out for the Result tab's proof fold; no verdict is derived from them.
|
|
14
|
+
// Third outcomes, each named, never green: `stopped` (the rubric could not be built, or bareguard stopped), a source that is not the frozen text.
|
|
15
|
+
|
|
16
|
+
import { createHash } from 'node:crypto';
|
|
17
|
+
import { createRubric, checkStep } from 'bareguard';
|
|
18
|
+
import { asList, FACT_OPS, boundOk, MAX_CURRENCIES, MAX_UNITS, MAX_UNIT_CHARS } from './pagebits.js';
|
|
19
|
+
import { ruleWords, moneyAs } from './pageask.js';
|
|
20
|
+
|
|
21
|
+
/** the shapes a fact may have (bareguard's value kinds, plus `phrase` = a presence decide) */
|
|
22
|
+
export const FACT_SHAPES = Object.freeze(['money', 'number', 'date', 'time', 'phrase', 'text']);
|
|
23
|
+
/** bareguard signs at most this many decides on one `cited` check */
|
|
24
|
+
export const MAX_FACTS = 10;
|
|
25
|
+
/** the step's own checkpoint id in the rubric */
|
|
26
|
+
const CHECKPOINT = 'cited';
|
|
27
|
+
export const CITED_ID = 'facts';
|
|
28
|
+
|
|
29
|
+
/** every field a fact part may carry, and every field a fact may carry */
|
|
30
|
+
const PART_FIELDS = Object.freeze(['source', 'facts', 'count']);
|
|
31
|
+
const FACT_FIELDS = Object.freeze(['name', 'shape', 'unit', 'currency', 'phrase', 'anchor', 'until', 'rule']);
|
|
32
|
+
const NAME_RE = /^[A-Za-z][A-Za-z _-]{0,63}$/;
|
|
33
|
+
|
|
34
|
+
const isRec = (/** @type {unknown} */ v) => v !== null && typeof v === 'object' && !Array.isArray(v);
|
|
35
|
+
|
|
36
|
+
/** @param {string} t @returns {string} the sha256 of the text, hex */
|
|
37
|
+
export const sha256Text = (t) => createHash('sha256').update(t, 'utf8').digest('hex');
|
|
38
|
+
|
|
39
|
+
/**
|
|
40
|
+
* A money unit list with an entry listed twice, equal after the whitespace normalization bareguard's quote matcher applies (bareguard 0.26.0: its
|
|
41
|
+
* rubric refuses such a list), keeps the first of them. The one place a unit list is built into a signed fact.
|
|
42
|
+
* @param {string[]} units @returns {string[]}
|
|
43
|
+
*/
|
|
44
|
+
function unitsOnce(units) {
|
|
45
|
+
const seen = new Set();
|
|
46
|
+
return units.filter((u) => {
|
|
47
|
+
const key = String(u).split('**').join('').split('__').join('').replace(/\s+/g, ' ').trim();
|
|
48
|
+
if (seen.has(key)) return false;
|
|
49
|
+
seen.add(key);
|
|
50
|
+
return true;
|
|
51
|
+
});
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
/**
|
|
55
|
+
* A kept look fact as signed facts. The label that comes first becomes the anchor and the limit the look read (`reading`) becomes the number rule. A PLACE
|
|
56
|
+
* fact (the look gave it `phrases`, src/pageplace.js: R2, R2a, R2b) becomes one phrase fact per phrase, because every one of them must be in the quote
|
|
57
|
+
* (R2c); the old choice, the first kept word, is gone (in 2 of 3 saved F71 drafts that word was on no result line, FINDINGS F72). A plain phrase fact
|
|
58
|
+
* ("Nonstop") keeps its first word.
|
|
59
|
+
* @param {any} k a fact from `lookFactOf` (src/pagelook.js) @returns {Record<string, any>[]}
|
|
60
|
+
*/
|
|
61
|
+
export function factsOfLook(k) {
|
|
62
|
+
if (k.shape === 'phrase' || k.shape === 'place') {
|
|
63
|
+
const phrases = Array.isArray(k.phrases) ? k.phrases : [k.phrase ?? k.words?.[0] ?? ''];
|
|
64
|
+
return phrases.map((/** @type {unknown} */ p, /** @type {number} */ i) => ({ name: phrases.length > 1 ? `${k.name} ${String.fromCharCode(97 + i)}` : String(k.name), shape: 'phrase', phrase: String(p) }));
|
|
65
|
+
}
|
|
66
|
+
/** @type {Record<string, any>} */
|
|
67
|
+
const f = { name: String(k.name), shape: k.shape };
|
|
68
|
+
const anchor = k.labels?.[0] ?? k.label ?? k.words?.[0];
|
|
69
|
+
if (anchor !== undefined) f.anchor = String(anchor);
|
|
70
|
+
if (f.shape === 'money') {
|
|
71
|
+
if (k.unit !== undefined) f.unit = unitsOnce(k.unit);
|
|
72
|
+
if (k.currency !== undefined) f.currency = [...k.currency];
|
|
73
|
+
}
|
|
74
|
+
if (isRec(k.reading)) f.rule = { ...k.reading };
|
|
75
|
+
return [f];
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
/**
|
|
79
|
+
* What a fact part may carry, as reds the caller can show before anything is signed or run. Whether bareguard can SIGN the rubric it builds is asked of
|
|
80
|
+
* bareguard itself (`buildCitedSpec` + createRubric); this covers the fields the part owns.
|
|
81
|
+
* @param {any} part @returns {string[]}
|
|
82
|
+
*/
|
|
83
|
+
export function factPartReds(part) {
|
|
84
|
+
/** @type {string[]} */
|
|
85
|
+
const reds = [];
|
|
86
|
+
if (!isRec(part) || typeof part.source !== 'string' || part.source.trim() === '') return ['a fact part needs the role of the input its quotes come from'];
|
|
87
|
+
if (!Array.isArray(part.facts) || part.facts.length === 0 || part.facts.length > MAX_FACTS) return [`a fact part needs 1 to ${MAX_FACTS} facts${Array.isArray(part.facts) && part.facts.length > MAX_FACTS ? ` (it has ${part.facts.length}; a place counts one fact for each phrase it needs, so a line with many places or many rules is too much for one check)` : ''}`];
|
|
88
|
+
for (const k of Object.keys(part)) if (!PART_FIELDS.includes(k)) reds.push(`a fact part has no field "${k}" - its fields are ${PART_FIELDS.join(', ')}`);
|
|
89
|
+
const seen = new Set();
|
|
90
|
+
part.facts.forEach((/** @type {any} */ f, /** @type {number} */ j) => {
|
|
91
|
+
const w = `fact ${j + 1}`;
|
|
92
|
+
if (!isRec(f) || typeof f.name !== 'string' || f.name.trim() === '') { reds.push(`${w}: needs a name`); return; }
|
|
93
|
+
for (const k of Object.keys(f)) if (!FACT_FIELDS.includes(k)) reds.push(`${w} "${f.name}": "${k}" is not a field of a fact - its fields are ${FACT_FIELDS.join(', ')}`);
|
|
94
|
+
// the name is in the claim text bareguard scans for numbers: letters only, so no digit can red the claim
|
|
95
|
+
if (!NAME_RE.test(f.name)) reds.push(`${w} "${f.name}": a fact's name is letters, spaces, "_" or "-" only (no digits), at most 64 characters`);
|
|
96
|
+
if (seen.has(f.name)) reds.push(`${w}: the name "${f.name}" is used twice`);
|
|
97
|
+
seen.add(f.name);
|
|
98
|
+
if (!FACT_SHAPES.includes(f.shape)) { reds.push(`${w} "${f.name}": the shape is not one of ${FACT_SHAPES.join(', ')}`); return; }
|
|
99
|
+
if (f.shape === 'phrase' && (typeof f.phrase !== 'string' || f.phrase === '')) reds.push(`${w} "${f.name}": a phrase fact needs its words`);
|
|
100
|
+
if (f.shape === 'money' && asList(f.unit).length + asList(f.currency).length === 0) reds.push(`${w} "${f.name}": money needs a unit or a currency`);
|
|
101
|
+
if (f.shape !== 'money' && (f.unit !== undefined || f.currency !== undefined)) reds.push(`${w} "${f.name}": only money has a unit or a currency`);
|
|
102
|
+
if (f.shape === 'phrase') for (const k of ['anchor', 'until', 'rule']) if (f[k] !== undefined) reds.push(`${w} "${f.name}": a phrase is fixed words the quote must contain; it has no ${k}`);
|
|
103
|
+
if (f.shape !== 'phrase' && f.phrase !== undefined) reds.push(`${w} "${f.name}": only a phrase fact carries phrase`);
|
|
104
|
+
if (f.until !== undefined && (f.anchor === undefined || f.shape !== 'text')) reds.push(`${w} "${f.name}": until closes a text fact that has an anchor, and nothing else`);
|
|
105
|
+
if (asList(f.currency).length > MAX_CURRENCIES) reds.push(`${w} "${f.name}": at most ${MAX_CURRENCIES} currencies`);
|
|
106
|
+
if (asList(f.unit).length > MAX_UNITS || asList(f.unit).some((u) => u.length > MAX_UNIT_CHARS)) reds.push(`${w} "${f.name}": at most ${MAX_UNITS} units of ${MAX_UNIT_CHARS} characters`);
|
|
107
|
+
if (f.rule !== undefined) {
|
|
108
|
+
const r = f.rule;
|
|
109
|
+
const ok = f.shape !== 'phrase' && f.shape !== 'text' && isRec(r) && FACT_OPS.includes(r.op)
|
|
110
|
+
&& (r.op === 'between' ? boundOk(f.shape, r.lo) && boundOk(f.shape, r.hi) : boundOk(f.shape, r.value));
|
|
111
|
+
if (!ok) reds.push(`${w} "${f.name}": the rule is not under / over / between with a bound of the fact's kind`);
|
|
112
|
+
}
|
|
113
|
+
});
|
|
114
|
+
const c = part.count;
|
|
115
|
+
if (c !== undefined) {
|
|
116
|
+
const ok = isRec(c) && Object.keys(c).every((k) => k === 'min' || k === 'max') && (c.min !== undefined || c.max !== undefined)
|
|
117
|
+
&& ['min', 'max'].every((k) => c[k] === undefined || (Number.isInteger(c[k]) && c[k] >= 1)) && (c.min === undefined || c.max === undefined || c.min <= c.max);
|
|
118
|
+
if (!ok) reds.push('the result count is {min, max}: whole numbers, 1 or more, min no more than max');
|
|
119
|
+
}
|
|
120
|
+
return reds;
|
|
121
|
+
}
|
|
122
|
+
|
|
123
|
+
/**
|
|
124
|
+
* bareguard's `decides` list for a fact part: one decide per fact, in the fact order (so `d<j>` is the fact's own index).
|
|
125
|
+
* @param {any} part @returns {Record<string, any>[]}
|
|
126
|
+
*/
|
|
127
|
+
export function factDecides(part) {
|
|
128
|
+
return part.facts.map((/** @type {any} */ f) => {
|
|
129
|
+
if (f.shape === 'phrase') return { phrases: [f.phrase] };
|
|
130
|
+
/** @type {Record<string, any>} */
|
|
131
|
+
const d = { valueKind: f.shape };
|
|
132
|
+
if (f.shape === 'money') { if (f.unit !== undefined) d.unit = f.unit; if (f.currency !== undefined) d.currency = f.currency; }
|
|
133
|
+
if (f.anchor !== undefined) d.anchor = f.anchor;
|
|
134
|
+
if (f.until !== undefined) d.until = f.until;
|
|
135
|
+
if (f.rule) {
|
|
136
|
+
d.op = f.rule.op;
|
|
137
|
+
if (f.rule.op === 'between') { d.lo = f.rule.lo; d.hi = f.rule.hi; } else d.value = f.rule.value;
|
|
138
|
+
}
|
|
139
|
+
return d;
|
|
140
|
+
});
|
|
141
|
+
}
|
|
142
|
+
|
|
143
|
+
/**
|
|
144
|
+
* The claims bareguard reads, built from the step's `results`. The claim TEXT is written here from the values the step gave (`name: value; name: value`),
|
|
145
|
+
* a phrase fact contributes its own signed words. A result that is not an object, or has no string quote, still becomes a claim (an empty one) so
|
|
146
|
+
* bareguard reds it by name rather than the result vanishing.
|
|
147
|
+
* @param {any} part @param {unknown} results @returns {{claim: string, quote: string, values?: Record<string, string>}[]}
|
|
148
|
+
*/
|
|
149
|
+
export function factClaims(part, results) {
|
|
150
|
+
const list = Array.isArray(results) ? results : [];
|
|
151
|
+
return list.map((/** @type {any} */ r) => {
|
|
152
|
+
const given = isRec(r) && isRec(r.values) ? r.values : null;
|
|
153
|
+
/** @type {Record<string, any>} */
|
|
154
|
+
const values = {};
|
|
155
|
+
/** @type {string[]} */
|
|
156
|
+
const said = [];
|
|
157
|
+
part.facts.forEach((/** @type {any} */ f, /** @type {number} */ j) => {
|
|
158
|
+
if (f.shape === 'phrase') { said.push(`${f.name}: ${f.phrase}`); return; }
|
|
159
|
+
const v = given && Object.hasOwn(given, f.name) ? given[f.name] : undefined;
|
|
160
|
+
// a JSON number is the value as written ("389"): converted, never repaired; anything else is left for bareguard to red
|
|
161
|
+
const s = typeof v === 'number' && Number.isFinite(v) ? String(v) : v;
|
|
162
|
+
if (typeof s === 'string') { values[`d${j}`] = s; said.push(`${f.name}: ${s}`); } else if (s !== undefined) values[`d${j}`] = s;
|
|
163
|
+
});
|
|
164
|
+
return { claim: said.join('; '), quote: isRec(r) && typeof r.quote === 'string' ? r.quote : '', values };
|
|
165
|
+
});
|
|
166
|
+
}
|
|
167
|
+
|
|
168
|
+
/**
|
|
169
|
+
* The rubric spec for one fact part: ONE `cited` check (the quote is in the frozen input, every value is in the quote and meets its rule), then the count
|
|
170
|
+
* as a min and a max on the harness-set count of results.
|
|
171
|
+
* @param {any} part @param {string} sha256 the frozen text's sha256 @returns {Record<string, any>}
|
|
172
|
+
*/
|
|
173
|
+
export function buildCitedSpec(part, sha256) {
|
|
174
|
+
/** @type {Record<string, any>[]} */
|
|
175
|
+
const checks = [{ id: CITED_ID, rule: 'cited', claims: 'claims', source: part.source, decides: factDecides(part) }];
|
|
176
|
+
if (part.count?.min !== undefined) checks.push({ id: 'count-min', rule: 'min', field: 'count', value: part.count.min });
|
|
177
|
+
if (part.count?.max !== undefined) checks.push({ id: 'count-max', rule: 'max', field: 'count', value: part.count.max });
|
|
178
|
+
return { schema: 1, goal: `facts from ${part.source}`, inputs: [{ name: part.source, sha256 }], checkpoints: { [CHECKPOINT]: { gating: true, checks } } };
|
|
179
|
+
}
|
|
180
|
+
|
|
181
|
+
/**
|
|
182
|
+
* One fact as a plain row, written by code from the signed fields: `fact · price — money in US dollars, under 150 US dollars, after "From", quoted from "flights"`.
|
|
183
|
+
* @param {any} f @param {string} source @returns {string}
|
|
184
|
+
*/
|
|
185
|
+
export function factSentence(f, source) {
|
|
186
|
+
/** @type {string[]} */
|
|
187
|
+
const bits = [];
|
|
188
|
+
if (f.shape === 'phrase') bits.push('phrase', `must say "${f.phrase}"`);
|
|
189
|
+
else {
|
|
190
|
+
bits.push(f.shape === 'money' ? `money in ${[...asList(f.unit), ...asList(f.currency)].join(' or ')}` : f.shape);
|
|
191
|
+
if (f.rule) bits.push(ruleWords(f));
|
|
192
|
+
else if (f.anchor === undefined) bits.push('as the quote writes it');
|
|
193
|
+
if (f.anchor !== undefined) bits.push(f.until !== undefined ? `the words after "${f.anchor}" up to "${f.until}"` : `after "${f.anchor}"`);
|
|
194
|
+
}
|
|
195
|
+
bits.push(`quoted from "${source}"`);
|
|
196
|
+
return `fact · ${f.name} — ${bits.join(', ')}`;
|
|
197
|
+
}
|
|
198
|
+
|
|
199
|
+
/** @param {any} part @returns {string|null} the "N results" row of a fact part */
|
|
200
|
+
export function countSentence(part) {
|
|
201
|
+
const c = isRec(part.count) ? part.count : null;
|
|
202
|
+
if (!c) return null;
|
|
203
|
+
const n = (/** @type {number} */ v) => `${v} result${v === 1 ? '' : 's'}`;
|
|
204
|
+
if (c.min !== undefined && c.max !== undefined) return c.min === c.max ? `fact · results — exactly ${n(c.min)}, each quoted from "${part.source}"` : `fact · results — ${c.min} to ${c.max} results, each quoted from "${part.source}"`;
|
|
205
|
+
return c.min !== undefined ? `fact · results — at least ${n(c.min)}, each quoted from "${part.source}"` : `fact · results — at most ${n(c.max)}, each quoted from "${part.source}"`;
|
|
206
|
+
}
|
|
207
|
+
|
|
208
|
+
/**
|
|
209
|
+
* One fact as the STEP is told it: what to write for it and where in the quote it sits, in plain words, from the signed fields. A phrase fact has no value to
|
|
210
|
+
* write - the quote has to say it. (bareloop's factBrief.)
|
|
211
|
+
* @param {any} f @returns {string}
|
|
212
|
+
*/
|
|
213
|
+
export function factBrief(f) {
|
|
214
|
+
if (f.shape === 'phrase') return `${f.name} - no value to write; the quote you copy must say "${f.phrase}"`;
|
|
215
|
+
/** @type {string[]} */
|
|
216
|
+
const bits = [f.shape === 'money' ? `an amount in ${[...asList(f.unit), ...asList(f.currency)].join(' or ')}` : f.shape === 'text' ? 'words' : `a ${f.shape}`];
|
|
217
|
+
if (f.anchor !== undefined) bits.push(f.until !== undefined ? `the words right after "${f.anchor}" up to "${f.until}"` : `right after "${f.anchor}"`);
|
|
218
|
+
if (f.rule) bits.push(`must be ${ruleWords(f)}`);
|
|
219
|
+
return `${f.name} - ${bits.join(', ')}`;
|
|
220
|
+
}
|
|
221
|
+
|
|
222
|
+
/**
|
|
223
|
+
* How a step that quotes a saved page is told to answer: the form of its answer and the facts each result must hold (typed fields in, plain words out).
|
|
224
|
+
* The step is never told a verdict or a way around the check; it is told what to copy and what to write.
|
|
225
|
+
* @param {any} part @returns {{answer: string, eachResultHolds: string[], howMany: string|null}}
|
|
226
|
+
*/
|
|
227
|
+
export function citedBrief(part) {
|
|
228
|
+
const c = isRec(part.count) ? part.count : null;
|
|
229
|
+
const n = (/** @type {number} */ v) => `${v} result${v === 1 ? '' : 's'}`;
|
|
230
|
+
const howMany = c === null ? null : c.min !== undefined && c.max !== undefined ? (c.min === c.max ? `Give exactly ${n(c.min)}.` : `Give ${c.min} to ${c.max} results.`) : c.min !== undefined ? `Give at least ${n(c.min)}.` : `Give at most ${n(c.max)}.`;
|
|
231
|
+
const money = Array.isArray(part.facts) && part.facts.some((/** @type {any} */ f) => f.shape === 'money');
|
|
232
|
+
return {
|
|
233
|
+
answer: `Answer with "results": one entry per thing you found, each {"quote": <the words of that one result, copied word for word from the input "${part.source}">, "values": {<fact name>: <the value, written exactly as the quote writes it>}}. A quote that is not in the input word for word is refused.${money ? ` A money value is only the digits of the amount ("30", not "30 US dollars" and not "$30").` : ''}`,
|
|
234
|
+
eachResultHolds: part.facts.map(factBrief),
|
|
235
|
+
howMany,
|
|
236
|
+
};
|
|
237
|
+
}
|
|
238
|
+
|
|
239
|
+
/**
|
|
240
|
+
* Every plan-card row a fact part carries: its facts, then its count (bareloop's factRows).
|
|
241
|
+
* @param {any} part @returns {string[]}
|
|
242
|
+
*/
|
|
243
|
+
export function factRows(part) {
|
|
244
|
+
const rows = (Array.isArray(part.facts) ? part.facts : []).map((/** @type {any} */ f) => factSentence(f, part.source));
|
|
245
|
+
const c = countSentence(part);
|
|
246
|
+
return [...rows, ...(c ? [c] : [])];
|
|
247
|
+
}
|
|
248
|
+
|
|
249
|
+
/**
|
|
250
|
+
* One bareguard `cited` item (`#<result>:d<j>:<reason>[:<detail>]`, or `#<result>:<reason>`) as a sentence. The names and numbers are bareguard's
|
|
251
|
+
* measurements and the signed facts, never inferred here.
|
|
252
|
+
* @param {string} item @param {any} part @returns {string}
|
|
253
|
+
*/
|
|
254
|
+
export function plainFactItem(item, part) {
|
|
255
|
+
const m = /^#(\d+):(?:d(\d+):)?([a-z-]+)(?::([\s\S]*))?$/.exec(item);
|
|
256
|
+
if (!m) return item;
|
|
257
|
+
const at = `result ${Number(m[1]) + 1}`;
|
|
258
|
+
const f = m[2] !== undefined && Array.isArray(part.facts) ? part.facts[Number(m[2])] : null;
|
|
259
|
+
const who = f ? `${at}, fact "${f.name}"` : at;
|
|
260
|
+
const detail = m[4] ?? '';
|
|
261
|
+
const say = (/** @type {string} */ s) => `${who}: ${s}`;
|
|
262
|
+
switch (m[3]) {
|
|
263
|
+
case 'quote-not-found': return `${at}: the quote is not found word for word in "${part.source}"`;
|
|
264
|
+
case 'empty-quote': return `${at}: the quote is empty — copy the words from "${part.source}" that hold the facts`;
|
|
265
|
+
case 'quote-too-long': return `${at}: the quote is too long — copy only that one result's own words, not more`;
|
|
266
|
+
case 'numbers': return `${at}: the number ${detail} is not in the quote the way the value writes it — write each value as the quote writes it (for example "9:27 AM", not "09:27")`;
|
|
267
|
+
case 'value-over': case 'value-under': return say(`the value in the quote is ${m[3] === 'value-over' ? 'above' : 'below'} what the rule allows${f && f.rule ? ` (${ruleWords(f)})` : ''}`);
|
|
268
|
+
case 'value-none': return say(`the quote holds no ${f ? f.shape : 'value'}${f && f.anchor !== undefined ? ` after "${f.anchor}"` : ''}`);
|
|
269
|
+
case 'value-many': return say(`the quote holds more than one ${f ? f.shape : 'value'} — quote only the words that hold this one`);
|
|
270
|
+
case 'value-malformed': return say(`a ${f ? f.shape : 'value'} in the quote is not written in a form the check can read`);
|
|
271
|
+
case 'anchor-missing': return say(`the label "${f ? f.anchor : ''}" is not in the quote`);
|
|
272
|
+
case 'anchor-ambiguous': return say(`the label "${f ? f.anchor : ''}" is in the quote more than once`);
|
|
273
|
+
case 'until-missing': return say(`"${f ? f.until : ''}" does not follow the label "${f ? f.anchor : ''}" in the quote`);
|
|
274
|
+
case 'phrase-missing': return say(`the quote does not say "${detail}"`);
|
|
275
|
+
case 'claim-value-missing': return say('no value was given in "values"');
|
|
276
|
+
case 'claim-value-malformed': return say(`the value is not written as a value of this kind${f && f.shape === 'money' ? ' - write only the digits of the amount (for example 30), no unit or sign' : ''}`);
|
|
277
|
+
case 'claim-value-absent': return say('the value is not in the quote as a whole word');
|
|
278
|
+
case 'claim-value-unknown': return say('this value is not one of the signed facts');
|
|
279
|
+
case 'claim-value-mismatch': return say(`the value is not what the quote holds (${detail.replace('!=', ' is not ')})`);
|
|
280
|
+
default: return `${who}: ${m[3]}${detail ? ` ${detail}` : ''}`;
|
|
281
|
+
}
|
|
282
|
+
}
|
|
283
|
+
|
|
284
|
+
/** @param {any} g a bareguard gap @param {any} part @returns {string[]} one sentence per thing wrong */
|
|
285
|
+
function plainGap(g, part) {
|
|
286
|
+
if (g.check === 'cited') {
|
|
287
|
+
if (g.kind === 'missing' || g.kind === 'no-claims') return [`the step has no results — add one result per thing you found: {"quote": ..., "values": {...}}`];
|
|
288
|
+
if (g.kind === 'unsupported') return (g.items ?? []).map((/** @type {string} */ it) => plainFactItem(it, part));
|
|
289
|
+
if (g.kind === 'too-many') return [`the results hold too many different quotes to check (${g.measured} against a limit of ${g.limit})`];
|
|
290
|
+
return [`the results cannot be checked (${g.kind})`];
|
|
291
|
+
}
|
|
292
|
+
if (g.check === 'min' || g.check === 'max') {
|
|
293
|
+
return [g.check === 'min' ? `${g.measured ?? 0} result(s) are listed, at least ${g.limit} are required` : `${g.measured} results are listed, at most ${g.limit} are allowed`];
|
|
294
|
+
}
|
|
295
|
+
return [`${g.check ?? 'check'}: ${g.kind ?? 'failed'}`];
|
|
296
|
+
}
|
|
297
|
+
|
|
298
|
+
/**
|
|
299
|
+
* Check one step's results against a fact part and the frozen text they must be quoted from.
|
|
300
|
+
* `text` is the run's OWN frozen copy of the source (the caller reads it from the run's folder; this function never reads a file). Its sha256 is taken
|
|
301
|
+
* here and signed into the rubric, so bareguard compares the text it is handed with the text the rubric was built for.
|
|
302
|
+
* @param {{part: any, text: string, results: unknown}} o
|
|
303
|
+
* @returns {Promise<{verdict: 'green'|'red'|'stopped', gapLines: string[], fault: string|null, sha256: string, claims: any[], evidence: any[]}>}
|
|
304
|
+
*/
|
|
305
|
+
export async function checkCited({ part, text, results }) {
|
|
306
|
+
const sha256 = sha256Text(text);
|
|
307
|
+
const none = { claims: /** @type {any[]} */ ([]), evidence: /** @type {any[]} */ ([]), sha256 };
|
|
308
|
+
const reds = factPartReds(part);
|
|
309
|
+
if (reds.length) return { verdict: 'stopped', gapLines: [], fault: `the facts to check are not valid: ${reds[0]}`, ...none };
|
|
310
|
+
let rubric;
|
|
311
|
+
try { rubric = createRubric(buildCitedSpec(part, sha256)); } catch (e) {
|
|
312
|
+
return { verdict: 'stopped', gapLines: [], fault: `the check could not be built: ${/** @type {Error} */ (e).message}`, ...none };
|
|
313
|
+
}
|
|
314
|
+
const claims = factClaims(part, results);
|
|
315
|
+
// `count` is set HERE from the list itself: the step never writes a number the check trusts
|
|
316
|
+
const r = await checkStep(rubric, CHECKPOINT, { count: claims.length, claims }, { inputs: { [part.source]: text } });
|
|
317
|
+
if (r.verdict === 'stopped') return { verdict: 'stopped', gapLines: [], fault: r.fault ? `${r.fault.kind}: ${r.fault.detail}` : 'the check stopped', ...none, claims };
|
|
318
|
+
const evidence = Array.isArray(r.full?.evidence) ? r.full.evidence : [];
|
|
319
|
+
if (r.verdict === 'green') return { verdict: 'green', gapLines: [], fault: null, sha256, claims, evidence };
|
|
320
|
+
return { verdict: 'red', gapLines: r.full.gaps.flatMap((/** @type {any} */ g) => plainGap(g, part)), fault: null, sha256, claims, evidence };
|
|
321
|
+
}
|