fwdloop 0.21.0 → 0.22.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/audit-fold.js +5 -1
- package/src/authoring.js +32 -6
- package/src/checked.js +18 -1
- package/src/cited-record.js +138 -0
- package/src/cited-source.js +49 -0
- package/src/citedcheck.js +306 -0
- package/src/citedwords.js +197 -0
- package/src/closers.js +56 -7
- package/src/ctx-tools.js +2 -14
- package/src/declaration.js +85 -5
- package/src/diary.js +2 -2
- package/src/docx.js +16 -2
- package/src/draft-steps.js +1 -1
- package/src/drafter.js +292 -25
- package/src/flow.js +33 -7
- package/src/flowmodel.js +21 -0
- package/src/input-facts.js +20 -3
- package/src/model-step.js +30 -5
- 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 +41 -20
- package/src/panel/authorcard.js +11 -1
- 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 +83 -2
- package/src/panel/index.html +279 -12
- package/src/panel/result.js +295 -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 +83 -12
- package/src/schedule.js +3 -3
- package/src/setup.js +16 -1
- package/src/sheetfind.js +50 -0
- package/src/sheetlook.js +157 -0
- package/src/signed-text.js +11 -1
- package/src/thinking.js +1 -1
- package/src/try-checks.js +6 -0
- package/src/types.js +3 -2
- package/types/authoring.d.ts +19 -2
- package/types/checked.d.ts +10 -0
- 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 +55 -3
- package/types/ctx-tools.d.ts +0 -1
- package/types/declaration.d.ts +15 -1
- package/types/docx.d.ts +14 -0
- package/types/drafter.d.ts +244 -2
- 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 +35 -3
- 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 +2 -0
- 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 +156 -7
- package/types/schedule.d.ts +0 -11
- package/types/setup.d.ts +13 -0
- package/types/sheetfind.d.ts +46 -0
- package/types/sheetlook.d.ts +120 -0
- package/types/thinking.d.ts +1 -1
- package/types/types.d.ts +11 -3
|
@@ -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,306 @@
|
|
|
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 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
|
|
41
|
+
* 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
|
|
42
|
+
* (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
|
|
43
|
+
* ("Nonstop") keeps its first word.
|
|
44
|
+
* @param {any} k a fact from `lookFactOf` (src/pagelook.js) @returns {Record<string, any>[]}
|
|
45
|
+
*/
|
|
46
|
+
export function factsOfLook(k) {
|
|
47
|
+
if (k.shape === 'phrase' || k.shape === 'place') {
|
|
48
|
+
const phrases = Array.isArray(k.phrases) ? k.phrases : [k.phrase ?? k.words?.[0] ?? ''];
|
|
49
|
+
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) }));
|
|
50
|
+
}
|
|
51
|
+
/** @type {Record<string, any>} */
|
|
52
|
+
const f = { name: String(k.name), shape: k.shape };
|
|
53
|
+
const anchor = k.labels?.[0] ?? k.label ?? k.words?.[0];
|
|
54
|
+
if (anchor !== undefined) f.anchor = String(anchor);
|
|
55
|
+
if (f.shape === 'money') {
|
|
56
|
+
if (k.unit !== undefined) f.unit = [...k.unit];
|
|
57
|
+
if (k.currency !== undefined) f.currency = [...k.currency];
|
|
58
|
+
}
|
|
59
|
+
if (isRec(k.reading)) f.rule = { ...k.reading };
|
|
60
|
+
return [f];
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
/**
|
|
64
|
+
* 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
|
|
65
|
+
* bareguard itself (`buildCitedSpec` + createRubric); this covers the fields the part owns.
|
|
66
|
+
* @param {any} part @returns {string[]}
|
|
67
|
+
*/
|
|
68
|
+
export function factPartReds(part) {
|
|
69
|
+
/** @type {string[]} */
|
|
70
|
+
const reds = [];
|
|
71
|
+
if (!isRec(part) || typeof part.source !== 'string' || part.source.trim() === '') return ['a fact part needs the role of the input its quotes come from'];
|
|
72
|
+
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)` : ''}`];
|
|
73
|
+
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(', ')}`);
|
|
74
|
+
const seen = new Set();
|
|
75
|
+
part.facts.forEach((/** @type {any} */ f, /** @type {number} */ j) => {
|
|
76
|
+
const w = `fact ${j + 1}`;
|
|
77
|
+
if (!isRec(f) || typeof f.name !== 'string' || f.name.trim() === '') { reds.push(`${w}: needs a name`); return; }
|
|
78
|
+
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(', ')}`);
|
|
79
|
+
// the name is in the claim text bareguard scans for numbers: letters only, so no digit can red the claim
|
|
80
|
+
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`);
|
|
81
|
+
if (seen.has(f.name)) reds.push(`${w}: the name "${f.name}" is used twice`);
|
|
82
|
+
seen.add(f.name);
|
|
83
|
+
if (!FACT_SHAPES.includes(f.shape)) { reds.push(`${w} "${f.name}": the shape is not one of ${FACT_SHAPES.join(', ')}`); return; }
|
|
84
|
+
if (f.shape === 'phrase' && (typeof f.phrase !== 'string' || f.phrase === '')) reds.push(`${w} "${f.name}": a phrase fact needs its words`);
|
|
85
|
+
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`);
|
|
86
|
+
if (f.shape !== 'money' && (f.unit !== undefined || f.currency !== undefined)) reds.push(`${w} "${f.name}": only money has a unit or a currency`);
|
|
87
|
+
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}`);
|
|
88
|
+
if (f.shape !== 'phrase' && f.phrase !== undefined) reds.push(`${w} "${f.name}": only a phrase fact carries phrase`);
|
|
89
|
+
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`);
|
|
90
|
+
if (asList(f.currency).length > MAX_CURRENCIES) reds.push(`${w} "${f.name}": at most ${MAX_CURRENCIES} currencies`);
|
|
91
|
+
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`);
|
|
92
|
+
if (f.rule !== undefined) {
|
|
93
|
+
const r = f.rule;
|
|
94
|
+
const ok = f.shape !== 'phrase' && f.shape !== 'text' && isRec(r) && FACT_OPS.includes(r.op)
|
|
95
|
+
&& (r.op === 'between' ? boundOk(f.shape, r.lo) && boundOk(f.shape, r.hi) : boundOk(f.shape, r.value));
|
|
96
|
+
if (!ok) reds.push(`${w} "${f.name}": the rule is not under / over / between with a bound of the fact's kind`);
|
|
97
|
+
}
|
|
98
|
+
});
|
|
99
|
+
const c = part.count;
|
|
100
|
+
if (c !== undefined) {
|
|
101
|
+
const ok = isRec(c) && Object.keys(c).every((k) => k === 'min' || k === 'max') && (c.min !== undefined || c.max !== undefined)
|
|
102
|
+
&& ['min', 'max'].every((k) => c[k] === undefined || (Number.isInteger(c[k]) && c[k] >= 1)) && (c.min === undefined || c.max === undefined || c.min <= c.max);
|
|
103
|
+
if (!ok) reds.push('the result count is {min, max}: whole numbers, 1 or more, min no more than max');
|
|
104
|
+
}
|
|
105
|
+
return reds;
|
|
106
|
+
}
|
|
107
|
+
|
|
108
|
+
/**
|
|
109
|
+
* 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).
|
|
110
|
+
* @param {any} part @returns {Record<string, any>[]}
|
|
111
|
+
*/
|
|
112
|
+
export function factDecides(part) {
|
|
113
|
+
return part.facts.map((/** @type {any} */ f) => {
|
|
114
|
+
if (f.shape === 'phrase') return { phrases: [f.phrase] };
|
|
115
|
+
/** @type {Record<string, any>} */
|
|
116
|
+
const d = { valueKind: f.shape };
|
|
117
|
+
if (f.shape === 'money') { if (f.unit !== undefined) d.unit = f.unit; if (f.currency !== undefined) d.currency = f.currency; }
|
|
118
|
+
if (f.anchor !== undefined) d.anchor = f.anchor;
|
|
119
|
+
if (f.until !== undefined) d.until = f.until;
|
|
120
|
+
if (f.rule) {
|
|
121
|
+
d.op = f.rule.op;
|
|
122
|
+
if (f.rule.op === 'between') { d.lo = f.rule.lo; d.hi = f.rule.hi; } else d.value = f.rule.value;
|
|
123
|
+
}
|
|
124
|
+
return d;
|
|
125
|
+
});
|
|
126
|
+
}
|
|
127
|
+
|
|
128
|
+
/**
|
|
129
|
+
* 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`),
|
|
130
|
+
* 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
|
|
131
|
+
* bareguard reds it by name rather than the result vanishing.
|
|
132
|
+
* @param {any} part @param {unknown} results @returns {{claim: string, quote: string, values?: Record<string, string>}[]}
|
|
133
|
+
*/
|
|
134
|
+
export function factClaims(part, results) {
|
|
135
|
+
const list = Array.isArray(results) ? results : [];
|
|
136
|
+
return list.map((/** @type {any} */ r) => {
|
|
137
|
+
const given = isRec(r) && isRec(r.values) ? r.values : null;
|
|
138
|
+
/** @type {Record<string, any>} */
|
|
139
|
+
const values = {};
|
|
140
|
+
/** @type {string[]} */
|
|
141
|
+
const said = [];
|
|
142
|
+
part.facts.forEach((/** @type {any} */ f, /** @type {number} */ j) => {
|
|
143
|
+
if (f.shape === 'phrase') { said.push(`${f.name}: ${f.phrase}`); return; }
|
|
144
|
+
const v = given && Object.hasOwn(given, f.name) ? given[f.name] : undefined;
|
|
145
|
+
// a JSON number is the value as written ("389"): converted, never repaired; anything else is left for bareguard to red
|
|
146
|
+
const s = typeof v === 'number' && Number.isFinite(v) ? String(v) : v;
|
|
147
|
+
if (typeof s === 'string') { values[`d${j}`] = s; said.push(`${f.name}: ${s}`); } else if (s !== undefined) values[`d${j}`] = s;
|
|
148
|
+
});
|
|
149
|
+
return { claim: said.join('; '), quote: isRec(r) && typeof r.quote === 'string' ? r.quote : '', values };
|
|
150
|
+
});
|
|
151
|
+
}
|
|
152
|
+
|
|
153
|
+
/**
|
|
154
|
+
* 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
|
|
155
|
+
* as a min and a max on the harness-set count of results.
|
|
156
|
+
* @param {any} part @param {string} sha256 the frozen text's sha256 @returns {Record<string, any>}
|
|
157
|
+
*/
|
|
158
|
+
export function buildCitedSpec(part, sha256) {
|
|
159
|
+
/** @type {Record<string, any>[]} */
|
|
160
|
+
const checks = [{ id: CITED_ID, rule: 'cited', claims: 'claims', source: part.source, decides: factDecides(part) }];
|
|
161
|
+
if (part.count?.min !== undefined) checks.push({ id: 'count-min', rule: 'min', field: 'count', value: part.count.min });
|
|
162
|
+
if (part.count?.max !== undefined) checks.push({ id: 'count-max', rule: 'max', field: 'count', value: part.count.max });
|
|
163
|
+
return { schema: 1, goal: `facts from ${part.source}`, inputs: [{ name: part.source, sha256 }], checkpoints: { [CHECKPOINT]: { gating: true, checks } } };
|
|
164
|
+
}
|
|
165
|
+
|
|
166
|
+
/**
|
|
167
|
+
* 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"`.
|
|
168
|
+
* @param {any} f @param {string} source @returns {string}
|
|
169
|
+
*/
|
|
170
|
+
export function factSentence(f, source) {
|
|
171
|
+
/** @type {string[]} */
|
|
172
|
+
const bits = [];
|
|
173
|
+
if (f.shape === 'phrase') bits.push('phrase', `must say "${f.phrase}"`);
|
|
174
|
+
else {
|
|
175
|
+
bits.push(f.shape === 'money' ? `money in ${[...asList(f.unit), ...asList(f.currency)].join(' or ')}` : f.shape);
|
|
176
|
+
if (f.rule) bits.push(ruleWords(f));
|
|
177
|
+
else if (f.anchor === undefined) bits.push('as the quote writes it');
|
|
178
|
+
if (f.anchor !== undefined) bits.push(f.until !== undefined ? `the words after "${f.anchor}" up to "${f.until}"` : `after "${f.anchor}"`);
|
|
179
|
+
}
|
|
180
|
+
bits.push(`quoted from "${source}"`);
|
|
181
|
+
return `fact · ${f.name} — ${bits.join(', ')}`;
|
|
182
|
+
}
|
|
183
|
+
|
|
184
|
+
/** @param {any} part @returns {string|null} the "N results" row of a fact part */
|
|
185
|
+
export function countSentence(part) {
|
|
186
|
+
const c = isRec(part.count) ? part.count : null;
|
|
187
|
+
if (!c) return null;
|
|
188
|
+
const n = (/** @type {number} */ v) => `${v} result${v === 1 ? '' : 's'}`;
|
|
189
|
+
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}"`;
|
|
190
|
+
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}"`;
|
|
191
|
+
}
|
|
192
|
+
|
|
193
|
+
/**
|
|
194
|
+
* 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
|
|
195
|
+
* write - the quote has to say it. (bareloop's factBrief.)
|
|
196
|
+
* @param {any} f @returns {string}
|
|
197
|
+
*/
|
|
198
|
+
export function factBrief(f) {
|
|
199
|
+
if (f.shape === 'phrase') return `${f.name} - no value to write; the quote you copy must say "${f.phrase}"`;
|
|
200
|
+
/** @type {string[]} */
|
|
201
|
+
const bits = [f.shape === 'money' ? `an amount in ${[...asList(f.unit), ...asList(f.currency)].join(' or ')}` : f.shape === 'text' ? 'words' : `a ${f.shape}`];
|
|
202
|
+
if (f.anchor !== undefined) bits.push(f.until !== undefined ? `the words right after "${f.anchor}" up to "${f.until}"` : `right after "${f.anchor}"`);
|
|
203
|
+
if (f.rule) bits.push(`must be ${ruleWords(f)}`);
|
|
204
|
+
return `${f.name} - ${bits.join(', ')}`;
|
|
205
|
+
}
|
|
206
|
+
|
|
207
|
+
/**
|
|
208
|
+
* 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).
|
|
209
|
+
* The step is never told a verdict or a way around the check; it is told what to copy and what to write.
|
|
210
|
+
* @param {any} part @returns {{answer: string, eachResultHolds: string[], howMany: string|null}}
|
|
211
|
+
*/
|
|
212
|
+
export function citedBrief(part) {
|
|
213
|
+
const c = isRec(part.count) ? part.count : null;
|
|
214
|
+
const n = (/** @type {number} */ v) => `${v} result${v === 1 ? '' : 's'}`;
|
|
215
|
+
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)}.`;
|
|
216
|
+
const money = Array.isArray(part.facts) && part.facts.some((/** @type {any} */ f) => f.shape === 'money');
|
|
217
|
+
return {
|
|
218
|
+
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").` : ''}`,
|
|
219
|
+
eachResultHolds: part.facts.map(factBrief),
|
|
220
|
+
howMany,
|
|
221
|
+
};
|
|
222
|
+
}
|
|
223
|
+
|
|
224
|
+
/**
|
|
225
|
+
* Every plan-card row a fact part carries: its facts, then its count (bareloop's factRows).
|
|
226
|
+
* @param {any} part @returns {string[]}
|
|
227
|
+
*/
|
|
228
|
+
export function factRows(part) {
|
|
229
|
+
const rows = (Array.isArray(part.facts) ? part.facts : []).map((/** @type {any} */ f) => factSentence(f, part.source));
|
|
230
|
+
const c = countSentence(part);
|
|
231
|
+
return [...rows, ...(c ? [c] : [])];
|
|
232
|
+
}
|
|
233
|
+
|
|
234
|
+
/**
|
|
235
|
+
* One bareguard `cited` item (`#<result>:d<j>:<reason>[:<detail>]`, or `#<result>:<reason>`) as a sentence. The names and numbers are bareguard's
|
|
236
|
+
* measurements and the signed facts, never inferred here.
|
|
237
|
+
* @param {string} item @param {any} part @returns {string}
|
|
238
|
+
*/
|
|
239
|
+
export function plainFactItem(item, part) {
|
|
240
|
+
const m = /^#(\d+):(?:d(\d+):)?([a-z-]+)(?::([\s\S]*))?$/.exec(item);
|
|
241
|
+
if (!m) return item;
|
|
242
|
+
const at = `result ${Number(m[1]) + 1}`;
|
|
243
|
+
const f = m[2] !== undefined && Array.isArray(part.facts) ? part.facts[Number(m[2])] : null;
|
|
244
|
+
const who = f ? `${at}, fact "${f.name}"` : at;
|
|
245
|
+
const detail = m[4] ?? '';
|
|
246
|
+
const say = (/** @type {string} */ s) => `${who}: ${s}`;
|
|
247
|
+
switch (m[3]) {
|
|
248
|
+
case 'quote-not-found': return `${at}: the quote is not found word for word in "${part.source}"`;
|
|
249
|
+
case 'empty-quote': return `${at}: the quote is empty — copy the words from "${part.source}" that hold the facts`;
|
|
250
|
+
case 'quote-too-long': return `${at}: the quote is too long — copy only that one result's own words, not more`;
|
|
251
|
+
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")`;
|
|
252
|
+
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)})` : ''}`);
|
|
253
|
+
case 'value-none': return say(`the quote holds no ${f ? f.shape : 'value'}${f && f.anchor !== undefined ? ` after "${f.anchor}"` : ''}`);
|
|
254
|
+
case 'value-many': return say(`the quote holds more than one ${f ? f.shape : 'value'} — quote only the words that hold this one`);
|
|
255
|
+
case 'value-malformed': return say(`a ${f ? f.shape : 'value'} in the quote is not written in a form the check can read`);
|
|
256
|
+
case 'anchor-missing': return say(`the label "${f ? f.anchor : ''}" is not in the quote`);
|
|
257
|
+
case 'anchor-ambiguous': return say(`the label "${f ? f.anchor : ''}" is in the quote more than once`);
|
|
258
|
+
case 'until-missing': return say(`"${f ? f.until : ''}" does not follow the label "${f ? f.anchor : ''}" in the quote`);
|
|
259
|
+
case 'phrase-missing': return say(`the quote does not say "${detail}"`);
|
|
260
|
+
case 'claim-value-missing': return say('no value was given in "values"');
|
|
261
|
+
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' : ''}`);
|
|
262
|
+
case 'claim-value-absent': return say('the value is not in the quote as a whole word');
|
|
263
|
+
case 'claim-value-unknown': return say('this value is not one of the signed facts');
|
|
264
|
+
case 'claim-value-mismatch': return say(`the value is not what the quote holds (${detail.replace('!=', ' is not ')})`);
|
|
265
|
+
default: return `${who}: ${m[3]}${detail ? ` ${detail}` : ''}`;
|
|
266
|
+
}
|
|
267
|
+
}
|
|
268
|
+
|
|
269
|
+
/** @param {any} g a bareguard gap @param {any} part @returns {string[]} one sentence per thing wrong */
|
|
270
|
+
function plainGap(g, part) {
|
|
271
|
+
if (g.check === 'cited') {
|
|
272
|
+
if (g.kind === 'missing' || g.kind === 'no-claims') return [`the step has no results — add one result per thing you found: {"quote": ..., "values": {...}}`];
|
|
273
|
+
if (g.kind === 'unsupported') return (g.items ?? []).map((/** @type {string} */ it) => plainFactItem(it, part));
|
|
274
|
+
if (g.kind === 'too-many') return [`the results hold too many different quotes to check (${g.measured} against a limit of ${g.limit})`];
|
|
275
|
+
return [`the results cannot be checked (${g.kind})`];
|
|
276
|
+
}
|
|
277
|
+
if (g.check === 'min' || g.check === 'max') {
|
|
278
|
+
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`];
|
|
279
|
+
}
|
|
280
|
+
return [`${g.check ?? 'check'}: ${g.kind ?? 'failed'}`];
|
|
281
|
+
}
|
|
282
|
+
|
|
283
|
+
/**
|
|
284
|
+
* Check one step's results against a fact part and the frozen text they must be quoted from.
|
|
285
|
+
* `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
|
|
286
|
+
* here and signed into the rubric, so bareguard compares the text it is handed with the text the rubric was built for.
|
|
287
|
+
* @param {{part: any, text: string, results: unknown}} o
|
|
288
|
+
* @returns {Promise<{verdict: 'green'|'red'|'stopped', gapLines: string[], fault: string|null, sha256: string, claims: any[], evidence: any[]}>}
|
|
289
|
+
*/
|
|
290
|
+
export async function checkCited({ part, text, results }) {
|
|
291
|
+
const sha256 = sha256Text(text);
|
|
292
|
+
const none = { claims: /** @type {any[]} */ ([]), evidence: /** @type {any[]} */ ([]), sha256 };
|
|
293
|
+
const reds = factPartReds(part);
|
|
294
|
+
if (reds.length) return { verdict: 'stopped', gapLines: [], fault: `the facts to check are not valid: ${reds[0]}`, ...none };
|
|
295
|
+
let rubric;
|
|
296
|
+
try { rubric = createRubric(buildCitedSpec(part, sha256)); } catch (e) {
|
|
297
|
+
return { verdict: 'stopped', gapLines: [], fault: `the check could not be built: ${/** @type {Error} */ (e).message}`, ...none };
|
|
298
|
+
}
|
|
299
|
+
const claims = factClaims(part, results);
|
|
300
|
+
// `count` is set HERE from the list itself: the step never writes a number the check trusts
|
|
301
|
+
const r = await checkStep(rubric, CHECKPOINT, { count: claims.length, claims }, { inputs: { [part.source]: text } });
|
|
302
|
+
if (r.verdict === 'stopped') return { verdict: 'stopped', gapLines: [], fault: r.fault ? `${r.fault.kind}: ${r.fault.detail}` : 'the check stopped', ...none, claims };
|
|
303
|
+
const evidence = Array.isArray(r.full?.evidence) ? r.full.evidence : [];
|
|
304
|
+
if (r.verdict === 'green') return { verdict: 'green', gapLines: [], fault: null, sha256, claims, evidence };
|
|
305
|
+
return { verdict: 'red', gapLines: r.full.gaps.flatMap((/** @type {any} */ g) => plainGap(g, part)), fault: null, sha256, claims, evidence };
|
|
306
|
+
}
|
|
@@ -0,0 +1,197 @@
|
|
|
1
|
+
// borrowed-from: bareloop src/factwords.js@2f93962 (keptSetOf, checkFactWords: the page-word path, where the label must be one the page-word map KEPT; bareloop's
|
|
2
|
+
// code-made label list (src/labelscan.js) is the other path and is not copied - a source with no look is refused as unread instead)
|
|
3
|
+
// M10 amendment 7 item 1 (docs/wiki/the-module-ladder.md): the facts a cited step checks are WRITTEN by the drafter and CHECKED by code at $0, before
|
|
4
|
+
// anything is signed. Code refuses, naming the job line, when: a label is not one the page-read kept (label-off-list); a word is not on the page
|
|
5
|
+
// (word-missing); a label shows up fewer times than the result count (label-rare, phrase-rare); a page rule dropped that word (word-dropped). Tighter than
|
|
6
|
+
// bareloop (signed): a number rule that is not in the person's own words on that line (their `~` rule, or a reading they agreed to, which is a `~` line
|
|
7
|
+
// too) is refused (number-unsigned). And, from item 2 (R2c), a place the page-read grew to phrases must have every phrase in the part (place-missing).
|
|
8
|
+
//
|
|
9
|
+
// Pure: no I/O, no model. Compared white-space blind. Third outcome, named: a source with no page-read (md text, a page the look never read) is a red
|
|
10
|
+
// `unread` - a check whose words were never proved against the source is never signed. What this throws away: nothing; every red is a sentence.
|
|
11
|
+
|
|
12
|
+
import { asList, countExact, countPhrase, normLabel, wordsSaidOn } from './pagebits.js';
|
|
13
|
+
import { dropSentence } from './pagedropwords.js';
|
|
14
|
+
import { factsOfLook } from './citedcheck.js';
|
|
15
|
+
|
|
16
|
+
/** @typedef {{code: string, detail: string}} WordRed */
|
|
17
|
+
|
|
18
|
+
/**
|
|
19
|
+
* What the page-word map KEPT and DROPPED for one input: the sets the proof reads.
|
|
20
|
+
* @param {{facts?: any[], dropped?: any[]}|null|undefined} look @param {string} source
|
|
21
|
+
* @returns {{labels: Set<string>|null, words: Set<string>, dropped: Map<string, any>}|null} null = the page was not read by the map; `labels: null` = a document (no map, any label in the text)
|
|
22
|
+
*/
|
|
23
|
+
export function keptSetOf(look, source) {
|
|
24
|
+
if (!look || !Array.isArray(look.dropped)) return null;
|
|
25
|
+
const facts = (Array.isArray(look.facts) ? look.facts : []).filter((f) => f && f.source === source && Array.isArray(f.words));
|
|
26
|
+
const set = (/** @type {string[]} */ a) => new Set(a.map((w) => normLabel(String(w))));
|
|
27
|
+
/** @type {Map<string, any>} */
|
|
28
|
+
const dropped = new Map();
|
|
29
|
+
for (const d of look.dropped) if (d.source === source && typeof d.word === 'string' && !dropped.has(normLabel(d.word))) dropped.set(normLabel(d.word), d);
|
|
30
|
+
return {
|
|
31
|
+
labels: set(facts.filter((f) => f.shape !== 'phrase').flatMap((f) => f.words)),
|
|
32
|
+
words: set(facts.flatMap((f) => [...f.words, ...(f.phrases ?? []), ...(f.unit ?? []), ...(f.currency ?? [])])),
|
|
33
|
+
dropped,
|
|
34
|
+
};
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
/**
|
|
38
|
+
* The reds for ONE fact part against its frozen source text and what the page-word map kept.
|
|
39
|
+
* @param {{part: any, text: string, line: number, kept: NonNullable<ReturnType<typeof keptSetOf>>}} o
|
|
40
|
+
* @returns {WordRed[]}
|
|
41
|
+
*/
|
|
42
|
+
export function factWordReds({
|
|
43
|
+
part, text, line, kept,
|
|
44
|
+
}) {
|
|
45
|
+
/** @type {WordRed[]} */
|
|
46
|
+
const reds = [];
|
|
47
|
+
const src = part.source;
|
|
48
|
+
const red = (/** @type {string} */ code, /** @type {string} */ msg) => reds.push({ code, detail: `line ${line}: ${msg}` });
|
|
49
|
+
for (const f of Array.isArray(part.facts) ? part.facts : []) {
|
|
50
|
+
const who = `fact "${f.name}"`;
|
|
51
|
+
// a word the page rules DROPPED (and no other fact kept) can never come back through the drafter
|
|
52
|
+
const dropRed = (/** @type {string} */ what, /** @type {unknown} */ w) => {
|
|
53
|
+
const n = normLabel(String(w));
|
|
54
|
+
const d = kept.dropped.get(n);
|
|
55
|
+
if (!d || kept.words.has(n)) return false;
|
|
56
|
+
red('word-dropped', `${who}: the ${what} ${JSON.stringify(n)} cannot be used. ${dropSentence(d)}`);
|
|
57
|
+
return true;
|
|
58
|
+
};
|
|
59
|
+
if (f.anchor !== undefined && f.shape !== 'phrase') {
|
|
60
|
+
const label = normLabel(String(f.anchor));
|
|
61
|
+
if (dropRed('label', f.anchor)) { /* said above */ } else if (kept.labels !== null && !kept.labels.has(label)) {
|
|
62
|
+
const some = [...kept.labels].slice(0, 12).map((l) => JSON.stringify(l)).join(', ');
|
|
63
|
+
red('label-off-list', `${who}: the label ${JSON.stringify(label)} is not one of the labels the page words for "${src}" kept. A label is picked from those, never typed${some ? `: ${some}` : ' (there are none - leave the label out)'}.`);
|
|
64
|
+
} else if (countExact(text, label) < 1) {
|
|
65
|
+
red('word-missing', `${who}: the label ${JSON.stringify(label)} is not in the input "${src}".`);
|
|
66
|
+
} else if (Number.isInteger(part.count?.min) && countExact(text, label) < part.count.min) {
|
|
67
|
+
const n = part.count.min;
|
|
68
|
+
const seen = countExact(text, label);
|
|
69
|
+
red('label-rare', `${who}: the label ${JSON.stringify(label)} is in the input "${src}" only ${seen} time${seen === 1 ? '' : 's'}, but this line asks for ${n} results, so a label that sits in each result must be there at least ${n} times. Pick a label that is counted ${n} or more times.`);
|
|
70
|
+
}
|
|
71
|
+
}
|
|
72
|
+
if (f.until !== undefined) dropRed('closing label', f.until);
|
|
73
|
+
if (f.until !== undefined && countExact(text, f.until) < 1) red('word-missing', `${who}: ${JSON.stringify(f.until)} is not in the input "${src}". Copy the closing words from the input, or leave them out.`);
|
|
74
|
+
if (f.shape === 'phrase' && typeof f.phrase === 'string') {
|
|
75
|
+
const seen = countPhrase(text, f.phrase);
|
|
76
|
+
if (seen < 1) red('word-missing', `${who}: ${JSON.stringify(f.phrase)} is not in the input "${src}". Copy the words the input really uses, or remove the check.`);
|
|
77
|
+
else if (Number.isInteger(part.count?.min) && seen < part.count.min) {
|
|
78
|
+
const n = part.count.min;
|
|
79
|
+
red('phrase-rare', `${who}: the words ${JSON.stringify(f.phrase)} are in the input "${src}" only ${seen} time${seen === 1 ? '' : 's'}, but this line asks for ${n} results - each result's own quote must say them. Pick words every result says, or drop this check.`);
|
|
80
|
+
}
|
|
81
|
+
}
|
|
82
|
+
for (const u of asList(f.unit)) dropRed('unit', u);
|
|
83
|
+
for (const c of asList(f.currency)) dropRed('currency', c);
|
|
84
|
+
for (const u of asList(f.unit)) if (countPhrase(text, u) < 1) red('word-missing', `${who}: the unit ${JSON.stringify(u)} is not in the input "${src}". Copy the words the input really uses.`);
|
|
85
|
+
for (const c of asList(f.currency)) if (countExact(text, c) < 1) red('word-missing', `${who}: the currency ${JSON.stringify(c)} is not in the input "${src}". Copy the sign the input really uses.`);
|
|
86
|
+
}
|
|
87
|
+
return reds;
|
|
88
|
+
}
|
|
89
|
+
|
|
90
|
+
/**
|
|
91
|
+
* The numbers a person said in their words, by KIND. A number inside a time (05:00), a date (2026-11-18) or an amount ($150) is part of that token, not
|
|
92
|
+
* a bare number: "5" is not said by "05:00", "15" and "50" are not said by "$150". (bareloop's `boundInWords` is a substring test and accepts all of those; this
|
|
93
|
+
* reader is ours - FINDINGS-worthy, see the commit.) Times compare without a leading zero on the hour ("05:00" = "5:00"), spaces dropped, case folded.
|
|
94
|
+
* @param {string} text
|
|
95
|
+
* @returns {{bare: Set<number>, amount: Set<number>, time: Set<string>, date: Set<string>}}
|
|
96
|
+
*/
|
|
97
|
+
export function numbersSaid(text) {
|
|
98
|
+
const out = { bare: new Set(), amount: new Set(), time: new Set(), date: new Set() };
|
|
99
|
+
for (const m of String(text).matchAll(/(\d{1,2}:\d{2}(?:\s?[AaPp][Mm])?)|(\d{1,4}[-/.]\d{1,2}[-/.]\d{1,4})|([$\u20ac\u00a3]\s?\d[\d,]*(?:\.\d+)?)|(\d[\d,]*(?:\.\d+)?)/g)) {
|
|
100
|
+
if (m[1]) out.time.add(timeKey(m[1]));
|
|
101
|
+
else if (m[2]) out.date.add(m[2]);
|
|
102
|
+
else if (m[3]) out.amount.add(Number(m[3].replace(/[^\d.]/g, '')));
|
|
103
|
+
else out.bare.add(Number(m[4].replace(/,/g, '')));
|
|
104
|
+
}
|
|
105
|
+
return out;
|
|
106
|
+
}
|
|
107
|
+
/** @param {string} t @returns {string} a time token as compared: no spaces, lower case, no leading zero on the hour */
|
|
108
|
+
const timeKey = (t) => t.replace(/\s+/g, '').toLowerCase().replace(/^0(?=\d:)/, '');
|
|
109
|
+
|
|
110
|
+
/**
|
|
111
|
+
* Tighter than bareloop (signed, am7 item 1): every number in a fact's rule must be in the person's own words on this job line (the line and its `~` rules;
|
|
112
|
+
* an agreed reading is a `~` line too), as the right KIND of token: a money or number bound is a bare number or an amount, a time bound is a time, a date bound
|
|
113
|
+
* is a date. A bound the AI made up, or one that only sits inside another token, is refused.
|
|
114
|
+
* @param {{part: any, jobLine: {n: number, text?: string, rule?: string, rules?: string[]}}} o @returns {WordRed[]}
|
|
115
|
+
*/
|
|
116
|
+
export function numberRuleReds({ part, jobLine }) {
|
|
117
|
+
/** @type {WordRed[]} */
|
|
118
|
+
const reds = [];
|
|
119
|
+
const said = numbersSaid(wordsSaidOn(jobLine));
|
|
120
|
+
const isSaid = (/** @type {string} */ shape, /** @type {unknown} */ b) => {
|
|
121
|
+
if (shape === 'money' || shape === 'number') return typeof b === 'number' && (said.bare.has(b) || said.amount.has(b));
|
|
122
|
+
if (shape === 'time') return typeof b === 'string' && said.time.has(timeKey(b));
|
|
123
|
+
if (shape === 'date') return typeof b === 'string' && said.date.has(b);
|
|
124
|
+
return false;
|
|
125
|
+
};
|
|
126
|
+
for (const f of Array.isArray(part.facts) ? part.facts : []) {
|
|
127
|
+
if (!f.rule || typeof f.rule !== 'object') continue;
|
|
128
|
+
const bounds = f.rule.op === 'between' ? [f.rule.lo, f.rule.hi] : [f.rule.value];
|
|
129
|
+
for (const b of bounds) {
|
|
130
|
+
if (!isSaid(f.shape, b)) {
|
|
131
|
+
reds.push({
|
|
132
|
+
code: 'number-unsigned',
|
|
133
|
+
detail: `line ${jobLine.n}: fact "${f.name}" has the number ${b} in its rule, but ${b} is not in your words on line ${jobLine.n}. A number rule must come from a ~ rule you wrote or a reading you agreed to. Change the ~ rule on line ${jobLine.n} to say it, or leave this rule out.`,
|
|
134
|
+
});
|
|
135
|
+
}
|
|
136
|
+
}
|
|
137
|
+
}
|
|
138
|
+
return reds;
|
|
139
|
+
}
|
|
140
|
+
|
|
141
|
+
const NUMBER_WORDS = Object.freeze({ one: 1, two: 2, three: 3, four: 4, five: 5, six: 6, seven: 7, eight: 8, nine: 9, ten: 10 });
|
|
142
|
+
|
|
143
|
+
/**
|
|
144
|
+
* Signed (am7 item 1): "List 3 ..." is the count, 3 results. A count the person never said as a bare number or a word number (not inside a time, a date or an
|
|
145
|
+
* amount) is refused. Same reader as the number rules.
|
|
146
|
+
* @param {{part: any, jobLine: {n: number, text?: string, rule?: string, rules?: string[]}}} o @returns {WordRed[]}
|
|
147
|
+
*/
|
|
148
|
+
export function countReds({ part, jobLine }) {
|
|
149
|
+
/** @type {WordRed[]} */
|
|
150
|
+
const reds = [];
|
|
151
|
+
const text = wordsSaidOn(jobLine);
|
|
152
|
+
const said = numbersSaid(text).bare;
|
|
153
|
+
for (const w of text.toLowerCase().split(/[^\p{L}\p{N}]+/u)) { const n = NUMBER_WORDS[/** @type {'one'} */ (w)]; if (n !== undefined) said.add(n); }
|
|
154
|
+
const c = part.count && typeof part.count === 'object' ? part.count : {};
|
|
155
|
+
for (const k of ['min', 'max']) {
|
|
156
|
+
if (c[k] === undefined || said.has(c[k])) continue;
|
|
157
|
+
reds.push({ code: 'count-unsigned', detail: `line ${jobLine.n}: the result count has the number ${c[k]}, but ${c[k]} is not in your words on line ${jobLine.n}. The count is the number you asked for ("List 3" is 3 results); say it on the line, or leave the count out.` });
|
|
158
|
+
}
|
|
159
|
+
return reds;
|
|
160
|
+
}
|
|
161
|
+
|
|
162
|
+
/**
|
|
163
|
+
* R2c (am7 item 2): a place the page-read grew to phrases must have EVERY phrase as a phrase fact of the part, or a flipped route passes.
|
|
164
|
+
* @param {{part: any, line: number, look: {facts?: any[]}|null|undefined}} o @returns {WordRed[]}
|
|
165
|
+
*/
|
|
166
|
+
export function placeReds({ part, line, look }) {
|
|
167
|
+
/** @type {WordRed[]} */
|
|
168
|
+
const reds = [];
|
|
169
|
+
const have = new Set((Array.isArray(part.facts) ? part.facts : []).filter((/** @type {any} */ f) => f.shape === 'phrase').map((/** @type {any} */ f) => String(f.phrase)));
|
|
170
|
+
for (const k of Array.isArray(look?.facts) ? look.facts : []) {
|
|
171
|
+
if (k.line !== line || k.source !== part.source || !Array.isArray(k.phrases)) continue;
|
|
172
|
+
for (const f of factsOfLook(k)) {
|
|
173
|
+
if (!have.has(f.phrase)) {
|
|
174
|
+
reds.push({
|
|
175
|
+
code: 'place-missing',
|
|
176
|
+
detail: `line ${line}: the place ${JSON.stringify(k.name)} must be checked with the words ${JSON.stringify(f.phrase)} (the page writes the place with its role: a result that leaves the other way does not say them). Add a phrase fact with exactly those words${k.phrases.length > 1 ? `, and one for each of: ${k.phrases.map((/** @type {string} */ p) => JSON.stringify(p)).join(', ')}` : ''}.`,
|
|
177
|
+
});
|
|
178
|
+
}
|
|
179
|
+
}
|
|
180
|
+
}
|
|
181
|
+
return reds;
|
|
182
|
+
}
|
|
183
|
+
|
|
184
|
+
/**
|
|
185
|
+
* Every word red for one cited part, in the order a person fixes them.
|
|
186
|
+
* @param {{part: any, text: string|undefined, line: number, jobLine: any, look: {facts?: any[], dropped?: any[]}|null|undefined, doc?: boolean}} o @returns {WordRed[]}
|
|
187
|
+
*/
|
|
188
|
+
export function citedWordReds({
|
|
189
|
+
part, text, line, jobLine, look, doc = false,
|
|
190
|
+
}) {
|
|
191
|
+
// M10 piece 5 (item 2): a DOCUMENT has no page-word map; its words are proved on its text alone (a label need not be one a map kept)
|
|
192
|
+
const kept = doc ? { labels: null, words: new Set(), dropped: new Map() } : keptSetOf(look, part.source);
|
|
193
|
+
if (typeof text !== 'string' || kept === null) {
|
|
194
|
+
return [{ code: 'unread', detail: `line ${line}: the input "${part.source}" was not read here to prove the words against it, so a check on it cannot be drafted. A check on quotes works on a saved web page.` }];
|
|
195
|
+
}
|
|
196
|
+
return [...factWordReds({ part, text, line, kept }), ...numberRuleReds({ part, jobLine }), ...countReds({ part, jobLine }), ...placeReds({ part, line, look })];
|
|
197
|
+
}
|