fwdloop 0.35.2 → 0.37.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/README.md +1 -1
- package/bin/fwdloop +5 -2
- package/package.json +3 -3
- package/src/action-stop.js +1 -1
- package/src/auditchecks.js +41 -9
- package/src/authoring.js +78 -20
- package/src/blockasks.js +10 -12
- package/src/blockassemble.js +49 -43
- package/src/blockdraft.js +4 -2
- package/src/blockpick.js +10 -0
- package/src/canrun.js +2 -1
- package/src/cardlist.js +28 -5
- package/src/checkblock.js +610 -64
- package/src/checked.js +28 -33
- package/src/checklistsign.js +2 -1
- package/src/checklook.js +45 -0
- package/src/checkrows.js +202 -0
- package/src/cited-record.js +2 -2
- package/src/citedcheck.js +53 -12
- package/src/citedwords.js +7 -4
- package/src/closers.js +49 -2
- package/src/declaration.js +26 -4
- package/src/drafter.js +86 -62
- package/src/factwords.js +7 -4
- package/src/globprefix.js +26 -0
- package/src/model-step.js +22 -4
- package/src/pageask.js +6 -4
- package/src/pagedropwords.js +3 -11
- package/src/pagedry.js +75 -1
- package/src/pagefetch.js +11 -6
- package/src/pagelook.js +2 -1
- package/src/pagerules.js +8 -2
- package/src/panel/author.js +20 -8
- package/src/panel/authorcard.js +3 -3
- package/src/panel/data.js +3 -2
- package/src/panel/index.html +176 -41
- package/src/panel/result.js +40 -3
- package/src/pickrecord.js +14 -0
- package/src/planrebuild.js +55 -19
- package/src/ratelimit.js +117 -0
- package/src/report.js +216 -0
- package/src/rowresults.js +111 -0
- package/src/rubricstep.js +65 -2
- package/src/runner.js +61 -9
- package/src/send.js +19 -82
- package/src/shapebuild.js +281 -0
- package/src/shapelook.js +91 -0
- package/src/shapemenu.js +64 -0
- package/src/shaperesult.js +104 -0
- package/src/shaperows.js +263 -0
- package/src/shapewords.js +140 -0
- package/src/sheetbuild.js +41 -29
- package/src/sheetclaims.js +49 -1
- package/src/sheetfind.js +8 -0
- package/src/sheetlook.js +11 -1
- package/src/sheetrows.js +10 -64
- package/src/ticketrows.js +1 -1
- package/src/try-checks.js +39 -6
- package/src/types.js +1 -0
- package/src/whole-job.js +108 -16
- package/types/action-stop.d.ts +1 -1
- package/types/auditchecks.d.ts +13 -2
- package/types/authoring.d.ts +24 -4
- package/types/blockasks.d.ts +6 -10
- package/types/blockassemble.d.ts +13 -13
- package/types/blockdraft.d.ts +3 -2
- package/types/blockpick.d.ts +4 -0
- package/types/cardlist.d.ts +18 -2
- package/types/checkblock.d.ts +278 -28
- package/types/checked.d.ts +15 -6
- package/types/checklook.d.ts +36 -0
- package/types/checkrows.d.ts +119 -0
- package/types/citedcheck.d.ts +4 -2
- package/types/closers.d.ts +10 -1
- package/types/drafter.d.ts +8 -4
- package/types/globprefix.d.ts +5 -0
- package/types/model-step.d.ts +43 -2
- package/types/pageask.d.ts +5 -3
- package/types/pagedropwords.d.ts +0 -1
- package/types/pagedry.d.ts +27 -0
- package/types/pagefetch.d.ts +3 -2
- package/types/pagerules.d.ts +4 -0
- package/types/panel/result.d.ts +19 -2
- package/types/pickrecord.d.ts +10 -0
- package/types/ratelimit.d.ts +59 -0
- package/types/report.d.ts +46 -0
- package/types/rowresults.d.ts +28 -0
- package/types/rubricstep.d.ts +21 -0
- package/types/runner.d.ts +3 -1
- package/types/send.d.ts +13 -12
- package/types/shapebuild.d.ts +97 -0
- package/types/shapelook.d.ts +76 -0
- package/types/shapemenu.d.ts +40 -0
- package/types/shaperesult.d.ts +40 -0
- package/types/shaperows.d.ts +102 -0
- package/types/shapewords.d.ts +43 -0
- package/types/sheetbuild.d.ts +31 -13
- package/types/sheetclaims.d.ts +16 -0
- package/types/sheetfind.d.ts +1 -0
- package/types/sheetrows.d.ts +5 -36
- package/types/ticketrows.d.ts +1 -1
- package/types/try-checks.d.ts +2 -1
- package/types/types.d.ts +4 -0
- package/types/whole-job.d.ts +69 -3
package/src/shaperows.js
ADDED
|
@@ -0,0 +1,263 @@
|
|
|
1
|
+
// borrowed-from: bareloop src/shaperows.js@v0.61.0 (fc56e1f3)
|
|
2
|
+
// SHAPE JOBS (itemShape), docs/product/ITEM33-BUILD.md "Addendum 2026-11-18: shape jobs", clause 8 "Views" (and the rows' words of clause 4): the WORDS of the shape rows of the one list and the pure readers of a signed close's shape part.
|
|
3
|
+
// Modelled on src/sheetrows.js. The ROWS themselves live in the one owner, src/checkblock.js (`setShape`, `rowChoice`); this file only names them and reads them back, so the confirm card, the signed card, the Job tab, the
|
|
4
|
+
// Result tab and the report say the same thing.
|
|
5
|
+
// THE PART (what the views read and what `rubricStepShapeReds`, `buildStepSpec` and `lineBriefs` sign, check and brief):
|
|
6
|
+
// { class: 'shape', text, fields: [{ name, spec: {kind, required?, ...bounds}, rule?: <the ~ rule number it comes from>, row?: 'agent' }], minItems?, maxItems?, rules?: [<rule numbers>] }
|
|
7
|
+
// Rows of one shape line: a `[you]` row per `~` rule that has a field (the rule's own row, kind `rule`), an `[agent]` row per extra field (kind `shape`, key `L<n>.shape.<name>`), and the line's `[default]` row (key
|
|
8
|
+
// `L<n>.$shape`): the answer is a list of items, with the item count when the person's words gave one.
|
|
9
|
+
import { createRubric, rubricVocabulary } from 'bareguard';
|
|
10
|
+
import { lineRules } from './pagebits.js';
|
|
11
|
+
import { nameWhy, shapeNameRules, SHAPE_KINDS } from './shapemenu.js';
|
|
12
|
+
import { countCheck } from './blockpick.js';
|
|
13
|
+
|
|
14
|
+
const isRec = (/** @type {unknown} */ v) => v !== null && typeof v === 'object' && !Array.isArray(v);
|
|
15
|
+
|
|
16
|
+
/** the key of an `[agent]` shape row and of a line's `[default]` shape row @param {number} line @param {string} name */
|
|
17
|
+
export const shapeRowKey = (line, name) => `L${line}.shape.${name}`;
|
|
18
|
+
/** @param {number} line */
|
|
19
|
+
export const shapeDefaultKey = (line) => `L${line}.$shape`;
|
|
20
|
+
|
|
21
|
+
/** the words of the `[default]` row of a shape line, drawn only when the person's words gave an item count (hamr 2026-11-18: the card's `$` row is the Not checked line): `exactly 2 items` @param {{min?: number, max?: number}|null} [count] @returns {string|null} */
|
|
22
|
+
export function shapeDefaultWords(count = null) {
|
|
23
|
+
if (count && Number.isInteger(count.min) && count.min === count.max) return `the answer lists exactly ${count.min} item${count.min === 1 ? '' : 's'}`;
|
|
24
|
+
return null;
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
/**
|
|
28
|
+
* M19 am11 item 1: the item count a `~` rule's own words ARE ("exactly 4 list items" -> 4 to 4, "at least 3" -> at least 3, "at most 5" -> at most 5), or null. "at most 0" is no item count (bareguard's `maxItems` is at least 1): it stays a pick over the text.
|
|
29
|
+
* @param {string} text @returns {{min?: number, max?: number}|null}
|
|
30
|
+
*/
|
|
31
|
+
export function itemCountOf(text) {
|
|
32
|
+
const c = /** @type {any} */ (countCheck(text));
|
|
33
|
+
if (!c) return null;
|
|
34
|
+
if (c.rule === 'listItems') return { min: c.value, max: c.value };
|
|
35
|
+
if (c.rule === 'minListItems') return { min: c.value };
|
|
36
|
+
if (c.rule === 'maxListItems' && c.value >= 1) return { max: c.value };
|
|
37
|
+
return null;
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
/**
|
|
41
|
+
* M19 am11: the words of the rule row that holds a signed item count (the `[default]` row's words, `the answer lists exactly N items`, for an exact count; `at least N items` / `at most N items` beside it). Pure.
|
|
42
|
+
* @param {{minItems?: number, maxItems?: number}} part @returns {string|null}
|
|
43
|
+
*/
|
|
44
|
+
export function shapeCountWords(part) {
|
|
45
|
+
const lo = Number.isInteger(part.minItems) ? /** @type {number} */ (part.minItems) : null;
|
|
46
|
+
const hi = Number.isInteger(part.maxItems) ? /** @type {number} */ (part.maxItems) : null;
|
|
47
|
+
const s = (/** @type {number} */ n) => `${n} item${n === 1 ? '' : 's'}`;
|
|
48
|
+
if (lo !== null && lo === hi) return shapeDefaultWords({ min: lo, max: lo });
|
|
49
|
+
const w = [...(lo !== null ? [`at least ${s(lo)}`] : []), ...(hi !== null ? [`at most ${s(hi)}`] : [])];
|
|
50
|
+
return w.length ? `the answer lists ${w.join(' and ')}` : null;
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
/** why the truth of a shape line's items is not checked: the person judges it at Accept (the Not checked row of every shape line) */
|
|
54
|
+
export const SHAPE_TRUTH_PARTS = Object.freeze(['whether each one is true', 'you judge it at Accept']);
|
|
55
|
+
export const SHAPE_TRUTH_WORDS = SHAPE_TRUTH_PARTS.join(' \u2014 ');
|
|
56
|
+
|
|
57
|
+
/** @param {unknown} v @returns {string} */
|
|
58
|
+
const q = (v) => (typeof v === 'string' ? `"${v}"` : String(v));
|
|
59
|
+
|
|
60
|
+
/** the words of a shape card's heading, once above a shape line's rows (code's, never the model's) */
|
|
61
|
+
export const SHAPE_HEAD_PARTS = Object.freeze(['Shape checks', "code checks each item's form, not whether it is true"]);
|
|
62
|
+
export const SHAPE_HEAD = SHAPE_HEAD_PARTS.join(' \u00B7 ');
|
|
63
|
+
|
|
64
|
+
/** an upper bound `n` as the person's own form: "under N unit" when their words say under/fewer than/less than/below N+1 (so the signed N is "under N+1"), else "at most N unit" @param {number} n @param {string} unit @param {string} said */
|
|
65
|
+
export function upper(n, unit, said) {
|
|
66
|
+
const u = unit ? ` ${unit}` : '';
|
|
67
|
+
return [...String(said).matchAll(/(?:under|fewer than|less than|below)\s+(\d+)\b/gi)].some((m) => m[1] === String(n + 1)) ? `under ${n + 1}${u}` : `at most ${n}${u}`;
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
/** one bound range as words: `at least 3`, `at most 10`, `over 3`, `under 10` @param {Record<string, any>} s @param {string} said @returns {string[]} */
|
|
71
|
+
function range(s, said) {
|
|
72
|
+
return [
|
|
73
|
+
...(s.min !== undefined ? [`at least ${q(s.min)}`] : []), ...(s.gt !== undefined ? [`over ${q(s.gt)}`] : []),
|
|
74
|
+
...(s.max !== undefined ? [typeof s.max === 'number' ? upper(s.max, '', said) : `at most ${q(s.max)}`] : []), ...(s.lt !== undefined ? [`under ${q(s.lt)}`] : []),
|
|
75
|
+
];
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
/**
|
|
79
|
+
* A field spec as one plain sentence, written by code from the signed fields, in the person's own form where their words show it ("under 200 characters", not "at most 199"). Pure.
|
|
80
|
+
* @param {Record<string, any>} spec `{kind, required?, ...bounds}` @param {string} [said] the person's own rule for the field @returns {string}
|
|
81
|
+
*/
|
|
82
|
+
export function shapeFieldSentence(spec, said = '') {
|
|
83
|
+
const s = isRec(spec) ? spec : {};
|
|
84
|
+
/** @type {string} */
|
|
85
|
+
let out;
|
|
86
|
+
switch (s.kind) {
|
|
87
|
+
case 'text': {
|
|
88
|
+
const c = [
|
|
89
|
+
...(s.minChars === 1 ? ['not empty'] : s.minChars !== undefined ? [`at least ${s.minChars} characters`] : []), ...(s.maxChars !== undefined ? [upper(s.maxChars, 'characters', said)] : []),
|
|
90
|
+
...(s.minWords !== undefined ? [`at least ${s.minWords} words`] : []), ...(s.maxWords !== undefined ? [upper(s.maxWords, 'words', said)] : []),
|
|
91
|
+
];
|
|
92
|
+
out = c.length ? c.join(', ') : 'text';
|
|
93
|
+
break;
|
|
94
|
+
}
|
|
95
|
+
case 'number': out = ['a number', ...range(s, said)].join(', '); break;
|
|
96
|
+
case 'money': out = ['an amount of money', ...range(s, said)].join(', '); break;
|
|
97
|
+
case 'date': out = ['a date', ...range(s, said)].join(', '); break;
|
|
98
|
+
case 'time': out = ['a time', ...range(s, said)].join(', '); break;
|
|
99
|
+
case 'url': out = 'a web address'; break;
|
|
100
|
+
case 'oneOf': out = `one of: ${(Array.isArray(s.values) ? s.values : []).join(', ')}`; break;
|
|
101
|
+
case 'boolean': out = s.equals === true ? 'yes' : s.equals === false ? 'no' : 'yes or no'; break;
|
|
102
|
+
case 'list': {
|
|
103
|
+
const c = [...(s.minEntries !== undefined ? [`at least ${s.minEntries} entries`] : []), ...(s.maxEntries !== undefined ? [upper(s.maxEntries, 'entries', said)] : []), ...(s.unique === true ? ['no repeats'] : [])];
|
|
104
|
+
out = [`a list of ${isRec(s.of) ? shapeFieldSentence(s.of, said) : 'values'}`, ...c].join(', ');
|
|
105
|
+
break;
|
|
106
|
+
}
|
|
107
|
+
default: out = String(s.kind ?? 'a value');
|
|
108
|
+
}
|
|
109
|
+
return out;
|
|
110
|
+
}
|
|
111
|
+
|
|
112
|
+
/**
|
|
113
|
+
* The signed part of ONE shape line, from the fields that stand (the `[you]` rule fields and the `[agent]` fields the person has not dropped or left Suggested).
|
|
114
|
+
* @param {{text: string, fields: {name: string, spec: Record<string, any>, rule?: number|null, by?: 'you'|'agent'}[], itemCount?: {n?: number, min?: number, max?: number}|null, countRules?: number[]}} o `countRules` = the `~` rules whose words are the item count
|
|
115
|
+
* @returns {Record<string, any>}
|
|
116
|
+
*/
|
|
117
|
+
export function buildShapePart({ text, fields, itemCount = null, countRules = [] }) {
|
|
118
|
+
const rules = [...new Set([...fields.filter((f) => Number.isInteger(f.rule)).map((f) => /** @type {number} */ (f.rule)), ...countRules])].sort((a, b) => a - b);
|
|
119
|
+
// M19 am11: the count is `{n}` (code's per-input count: exactly n) or `{min?, max?}` (a `~` rule's own count: at least / at most)
|
|
120
|
+
const lo = itemCount === null ? null : Number.isInteger(itemCount.n) ? itemCount.n : Number.isInteger(itemCount.min) ? itemCount.min : null;
|
|
121
|
+
const hi = itemCount === null ? null : Number.isInteger(itemCount.n) ? itemCount.n : Number.isInteger(itemCount.max) ? itemCount.max : null;
|
|
122
|
+
return {
|
|
123
|
+
class: 'shape', text,
|
|
124
|
+
fields: fields.map((f) => ({ name: f.name, spec: f.spec, ...(Number.isInteger(f.rule) ? { rule: f.rule } : {}), ...(f.by === 'agent' ? { row: 'agent' } : {}) })),
|
|
125
|
+
...(lo !== null ? { minItems: lo } : {}), ...(hi !== null ? { maxItems: hi } : {}),
|
|
126
|
+
...(rules.length ? { rules } : {}),
|
|
127
|
+
};
|
|
128
|
+
}
|
|
129
|
+
|
|
130
|
+
/** the shape part of a rubric-step stage's params, or null: picked by the line's signed CLASS, never by a key of the answer file @param {any} params @returns {any} */
|
|
131
|
+
export const shapePartOf = (params) => (isRec(params) && Array.isArray(params.parts) ? params.parts.find((/** @type {any} */ p) => isRec(p) && p.class === 'shape' && Array.isArray(p.fields)) ?? null : null);
|
|
132
|
+
|
|
133
|
+
/** the signed shape part of job line `n` in a close, or null @param {any} closeDecl @param {number} n */
|
|
134
|
+
export function shapePartOfLine(closeDecl, n) {
|
|
135
|
+
for (const st of Array.isArray(closeDecl?.stages) ? closeDecl.stages : []) {
|
|
136
|
+
if (!isRec(st) || st.kind !== 'rubric-step' || !isRec(st.params) || st.params.step !== n) continue;
|
|
137
|
+
const p = shapePartOf(st.params);
|
|
138
|
+
if (p) return p;
|
|
139
|
+
}
|
|
140
|
+
return null;
|
|
141
|
+
}
|
|
142
|
+
|
|
143
|
+
/** the words of one signed field row: `name \u00B7 sentence` (the rule it comes from is the `~ [you]` tag's, the card's heading says what is checked) @param {any} f @param {string[]} rules the line's `~` rules @returns {string} */
|
|
144
|
+
export function shapeFieldText(f, rules) {
|
|
145
|
+
const said = Number.isInteger(f.rule) ? rules[f.rule - 1] : undefined;
|
|
146
|
+
return `${f.name} \u00B7 ${shapeFieldSentence(f.spec, said ?? '')}`;
|
|
147
|
+
}
|
|
148
|
+
|
|
149
|
+
/**
|
|
150
|
+
* The shape rows of a signed close, read from its shape parts: one per field (a `[you]` rule row's, an `[agent]` row's), then the line's `[default]` row. A close with no shape part has none.
|
|
151
|
+
* `rule` is the rule a `[you]` row is. Text is code's (never a model's).
|
|
152
|
+
* @param {any} closeDecl @param {any[]|null} [jobLines] the person's numbered lines
|
|
153
|
+
* @returns {{line: number, key: string, by: 'you'|'agent'|'default', text: string, name?: string, rule?: number}[]}
|
|
154
|
+
*/
|
|
155
|
+
export function shapeRowsOf(closeDecl, jobLines = null) {
|
|
156
|
+
const stages = isRec(closeDecl) && Array.isArray(closeDecl.stages) ? closeDecl.stages : [];
|
|
157
|
+
/** @type {{line: number, key: string, by: 'you'|'agent'|'default', text: string, name?: string, rule?: number, shape: true}[]} */
|
|
158
|
+
const out = [];
|
|
159
|
+
for (const st of stages) {
|
|
160
|
+
if (!isRec(st) || st.kind !== 'rubric-step' || !isRec(st.params)) continue;
|
|
161
|
+
const part = shapePartOf(st.params);
|
|
162
|
+
if (!part) continue;
|
|
163
|
+
const n = st.params.step;
|
|
164
|
+
const rules = lineRules((jobLines ?? []).find((/** @type {any} */ l) => isRec(l) && l.n === n));
|
|
165
|
+
for (const f of part.fields.filter(isRec)) {
|
|
166
|
+
out.push(Number.isInteger(f.rule) && f.row !== 'agent'
|
|
167
|
+
? { line: n, key: `L${n}.~${f.rule}`, by: 'you', text: shapeFieldText(f, rules), name: String(f.name), rule: f.rule, shape: true }
|
|
168
|
+
: { line: n, key: shapeRowKey(n, String(f.name)), by: 'agent', text: shapeFieldText(f, rules), name: String(f.name), shape: true });
|
|
169
|
+
}
|
|
170
|
+
const dw = shapeDefaultWords(Number.isInteger(part.minItems) ? { min: part.minItems, max: part.maxItems } : null);
|
|
171
|
+
if (dw !== null) out.push({ line: n, key: shapeDefaultKey(n), by: 'default', text: dw, shape: true });
|
|
172
|
+
}
|
|
173
|
+
return out;
|
|
174
|
+
}
|
|
175
|
+
|
|
176
|
+
/** the fields that say what a shape field IS: all but the names code gives it (the row's own key) — two drafts spell the same field alike @param {{name: string, kind: string, required?: boolean, bounds?: Record<string, any>}} f @returns {Record<string, any>} */
|
|
177
|
+
export const shapeFieldBody = (f) => ({ name: f.name, kind: f.kind, required: f.required === true, bounds: f.bounds ?? {} });
|
|
178
|
+
|
|
179
|
+
/** bareguard's own worked example item of `itemShape` (its address and values are bareguard's, not ours) @type {Record<string, any>} */
|
|
180
|
+
const WORKED = /** @type {any} */ (rubricVocabulary).rules.itemShape.example.output.items[0];
|
|
181
|
+
|
|
182
|
+
/** the answer field the `itemShape` check reads: the shape answer's one list (the step file is `{"results": [...]}`) */
|
|
183
|
+
export const SHAPE_FIELD = 'results';
|
|
184
|
+
|
|
185
|
+
/**
|
|
186
|
+
* The bareguard `itemShape` check of a signed shape part (clause 6, `buildStepSpec`): the answer field `results`, the item count the person's words gave, and one field spec per signed field.
|
|
187
|
+
* @param {any} part a `shape` part @param {string} id @param {any} [factPart] the step's fact part, when the line is facts+shape @returns {Record<string, any>}
|
|
188
|
+
*/
|
|
189
|
+
export function shapeItemCheck(part, id, factPart = null) {
|
|
190
|
+
const own = Object.fromEntries((Array.isArray(part.fields) ? part.fields : []).filter(isRec).map((f) => [String(f.name), f.spec]));
|
|
191
|
+
// FACTS + SHAPE (the rules-first spec, verified on real data in the shape-combined round 2 scratch run): the one list is also read by `cited`, and the itemShape is CLOSED, so it lists the keys the fact result carries:
|
|
192
|
+
// `quote` (text, required) and `values` (an object, one OPTIONAL field per signed fact: a number read from text for a money or number fact, text for every other shape). No `claim` key: `cited` reads claims
|
|
193
|
+
// that `flattenStep` builds from the signed fact names. The item COUNT stays with the fact part's own count checks (one fault, one report); the shape part's count is used only when the fact part has none.
|
|
194
|
+
const facts = isRec(factPart) && Array.isArray(factPart.facts) ? factPart.facts.filter(isRec) : null;
|
|
195
|
+
const factCounted = isRec(factPart) && isRec(factPart.count) && (Number.isInteger(factPart.count.min) || Number.isInteger(factPart.count.max));
|
|
196
|
+
const count = facts !== null && factCounted ? {} : { ...(Number.isInteger(part.minItems) ? { minItems: part.minItems } : {}), ...(Number.isInteger(part.maxItems) ? { maxItems: part.maxItems } : {}) };
|
|
197
|
+
const fields = facts === null ? own : {
|
|
198
|
+
quote: { kind: 'text', required: true },
|
|
199
|
+
values: { kind: 'object', fields: Object.fromEntries(facts.map((f) => [String(f.name), f.shape === 'money' || f.shape === 'number' ? { kind: 'number', fromText: true } : { kind: 'text' }])) },
|
|
200
|
+
...own,
|
|
201
|
+
};
|
|
202
|
+
return { id, rule: 'itemShape', field: SHAPE_FIELD, ...count, fields };
|
|
203
|
+
}
|
|
204
|
+
|
|
205
|
+
/**
|
|
206
|
+
* The SHAPE of a `shape` part (clause 5, called by `rubricStepShapeReds`): its fields are named legally and once, each spec has a kind on the menu, the count is whole numbers, a rule number is a whole number, and bareguard itself
|
|
207
|
+
* accepts the check (`createRubric`, which refuses an impossible bound or a field spec it does not know, naming the path). Pure, never throws.
|
|
208
|
+
* @param {any} part @param {string} w the path of the part @param {(code: string, path: string, detail: string) => void} red
|
|
209
|
+
*/
|
|
210
|
+
export function shapeShapeReds(part, w, red, factPart = null) {
|
|
211
|
+
const fs = part.fields;
|
|
212
|
+
if (!Array.isArray(fs) || fs.length === 0) { red('invalid-value', `${w}.fields`, 'a non-empty list of fields: {name, spec, rule?, row?}'); return; }
|
|
213
|
+
const rules = shapeNameRules();
|
|
214
|
+
const seen = new Set();
|
|
215
|
+
fs.forEach((/** @type {any} */ f, /** @type {number} */ i) => {
|
|
216
|
+
const fw = `${w}.fields[${i}]`;
|
|
217
|
+
if (!isRec(f)) { red('invalid-value', fw, '{name, spec, rule?, row?}'); return; }
|
|
218
|
+
const bad = nameWhy(f.name, rules);
|
|
219
|
+
if (bad !== null) red('invalid-value', `${fw}.name`, bad);
|
|
220
|
+
else if (seen.has(f.name)) red('invalid-value', `${fw}.name`, `the field name "${f.name}" is used twice`);
|
|
221
|
+
else seen.add(f.name);
|
|
222
|
+
if (factPart && (f.name === 'quote' || f.name === 'values')) red('invalid-value', `${fw}.name`, `"${f.name}" is a key of the fact results on this line, so it cannot name a shape field`);
|
|
223
|
+
if (!isRec(f.spec) || !Object.hasOwn(SHAPE_KINDS, String(f.spec.kind))) red('invalid-value', `${fw}.spec.kind`, `one of ${Object.keys(SHAPE_KINDS).join(' | ')}`);
|
|
224
|
+
if (Object.hasOwn(f, 'rule') && !(Number.isInteger(f.rule) && f.rule >= 1)) red('invalid-value', `${fw}.rule`, 'the number of the ~ rule the field comes from (an integer, 1 or more)');
|
|
225
|
+
if (Object.hasOwn(f, 'row') && f.row !== 'agent') red('invalid-value', `${fw}.row`, '"agent" (the tag of a field code made its own row) or absent');
|
|
226
|
+
});
|
|
227
|
+
for (const k of ['minItems', 'maxItems']) if (Object.hasOwn(part, k) && !(Number.isInteger(part[k]) && part[k] >= (k === 'maxItems' ? 1 : 0))) red('invalid-value', `${w}.${k}`, 'a whole number of items');
|
|
228
|
+
if (Number.isInteger(part.minItems) && Number.isInteger(part.maxItems) && part.minItems > part.maxItems) red('invalid-value', `${w}.maxItems`, 'maxItems is below minItems');
|
|
229
|
+
if (!isRec(part) || fs.some((/** @type {any} */ f) => !isRec(f) || !isRec(f.spec))) return;
|
|
230
|
+
// bareguard is the judge of whether the shape is SIGNABLE: ask it here, at $0
|
|
231
|
+
try {
|
|
232
|
+
createRubric({ schema: 1, goal: 'shape', inputs: [], checkpoints: { shape: { gating: false, checks: [shapeItemCheck(part, 'shape', factPart)] } } });
|
|
233
|
+
} catch (e) {
|
|
234
|
+
red('invalid-value', `${w}.fields`, `bareguard refuses this shape: ${String(/** @type {any} */ (e)?.message ?? e)}`);
|
|
235
|
+
}
|
|
236
|
+
}
|
|
237
|
+
|
|
238
|
+
/**
|
|
239
|
+
* One worked example item of a shape, written by code from the signed fields (never by a model): a value of each field's kind that the field's own bounds accept.
|
|
240
|
+
* @param {{name: string, spec: Record<string, any>}[]} fields @returns {Record<string, any>}
|
|
241
|
+
*/
|
|
242
|
+
export function shapeExampleItem(fields) {
|
|
243
|
+
const one = (/** @type {Record<string, any>} */ s) => {
|
|
244
|
+
switch (s.kind) {
|
|
245
|
+
case 'text': {
|
|
246
|
+
if (Number.isInteger(s.minWords) && s.minWords > 0) return Array.from({ length: s.minWords }, () => 'word').join(' ');
|
|
247
|
+
const lo = Math.max(Number.isInteger(s.minChars) ? s.minChars : 1, 1);
|
|
248
|
+
const len = Math.min(Math.max(12, lo), Number.isInteger(s.maxChars) ? s.maxChars : Infinity);
|
|
249
|
+
return 'Example text'.padEnd(len, '.').slice(0, len);
|
|
250
|
+
}
|
|
251
|
+
case 'number': return s.min ?? (s.gt !== undefined ? s.gt + 1 : s.max ?? (s.lt !== undefined ? s.lt - 1 : 1));
|
|
252
|
+
case 'oneOf': return Array.isArray(s.values) ? s.values[0] : 'value';
|
|
253
|
+
case 'money': { const c = Array.isArray(s.currency) ? s.currency[0] : s.currency; const u = Array.isArray(s.unit) ? s.unit[0] : s.unit; const a = s.min ?? (s.gt !== undefined ? s.gt + 1 : s.max ?? (s.lt !== undefined ? s.lt - 1 : 10)); return typeof c === 'string' && c.length === 1 ? `${c}${a}` : `${a} ${u ?? c ?? ''}`.trim(); }
|
|
254
|
+
case 'date': return s.min ?? '2026-01-15';
|
|
255
|
+
case 'time': return s.min ?? '09:30';
|
|
256
|
+
case 'url': return WORKED.link;
|
|
257
|
+
case 'boolean': return s.equals ?? true;
|
|
258
|
+
case 'list': return isRec(s.of) ? [one(s.of)] : [];
|
|
259
|
+
default: return 'value';
|
|
260
|
+
}
|
|
261
|
+
};
|
|
262
|
+
return Object.fromEntries(fields.map((f) => [f.name, one(f.spec)]));
|
|
263
|
+
}
|
|
@@ -0,0 +1,140 @@
|
|
|
1
|
+
// borrowed-from: bareloop src/shapewords.js@v0.61.0 (fc56e1f3)
|
|
2
|
+
// One wording differs from v0.61.0, by hamr's ask on 2026-11-20: the `(it is ...)` words of a `not-a-list` gap say `plain text`, `a number` and so on (MEASURED_WORDS), where v0.61.0 prints bareguard's raw `type:string`.
|
|
3
|
+
// SHAPE JOBS (itemShape), docs/product/ITEM33-BUILD.md "Addendum 2026-11-18: shape jobs", clause 7 "Words": the plain sentence of a bareguard `itemShape` gap, so a red says what to fix and never a raw code.
|
|
4
|
+
// bareguard's item reds read `#<i>:<path>:<reason>` (0-based item, dotted path, `name[j]` for a list entry; reasons `missing`, `wrong-type`, `not-one-of`, `bad-number|money|date|time|url`, `wrong-value`, `under`, `over`,
|
|
5
|
+
// `too-short|too-long:chars|words:<n><|><bound>`, `unknown-field:<key>`, and a list's `not-a-list`, `unreadable`, `count-under|count-over:<n><|><bound>`, `too-many:<n>><cap>`, `duplicate`); an item with no field part is
|
|
6
|
+
// `#<i>:item-not-object` or `#<i>:unreadable`. The whole-field gaps are `missing`, `unreadable`, `not-a-list`, `too-many`, `count-under`, `count-over`. A gap carries names, counts and bounds, never the worker's values, so
|
|
7
|
+
// neither does a sentence here. The item is shown 1-based ("item 2" for `#1`). A reason this table does not know falls back to its raw text: nothing is dropped.
|
|
8
|
+
// Pure. `plainShapeGap` is called by src/rubricstep.js `plainStepGap`; `plainShapeItem` and `itemFieldOf` by src/rowresults.js.
|
|
9
|
+
|
|
10
|
+
/** the kinds of value a reader names in a red, in a person's words */
|
|
11
|
+
const READS = Object.freeze({ 'bad-number': 'a number', 'bad-money': 'a money amount', 'bad-date': 'a date', 'bad-time': 'a time', 'bad-url': 'a web address' });
|
|
12
|
+
|
|
13
|
+
/** the words of a `not-a-list` gap's measured value (bareguard's `type:<kind>`); any other measured value is printed as it came */
|
|
14
|
+
export const MEASURED_WORDS = Object.freeze({ 'type:string': 'plain text', 'type:number': 'a number', 'type:boolean': 'true or false', 'type:object': 'an object', 'type:null': 'empty' });
|
|
15
|
+
|
|
16
|
+
/** the measured value of a `not-a-list` gap in a person's words, or `something else` when none came @param {unknown} measured @returns {string} */
|
|
17
|
+
export function measuredWords(measured) {
|
|
18
|
+
if (measured === undefined || measured === null) return 'something else';
|
|
19
|
+
return Object.hasOwn(MEASURED_WORDS, String(measured)) ? MEASURED_WORDS[/** @type {keyof typeof MEASURED_WORDS} */ (String(measured))] : String(measured);
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
/** one path as words: `tags[2]` is `tags, entry 3` (entries count from 1 for a person) @param {string} path @returns {string} */
|
|
23
|
+
const pathWords = (path) => path.replace(/\[(\d+)\]/g, (_m, j) => `, entry ${Number(j) + 1}`);
|
|
24
|
+
|
|
25
|
+
/**
|
|
26
|
+
* Split one item red into its item (0-based), its field path (null when the red is about the item as a whole) and its reason. Null when the text is not an item red.
|
|
27
|
+
* @param {unknown} item @returns {{i: number, path: string|null, reason: string}|null}
|
|
28
|
+
*/
|
|
29
|
+
export function parseShapeItem(item) {
|
|
30
|
+
const m = /^#(\d+):(.*)$/s.exec(String(item));
|
|
31
|
+
if (!m) return null;
|
|
32
|
+
const rest = m[2];
|
|
33
|
+
const i = Number(m[1]);
|
|
34
|
+
if (rest === 'item-not-object' || rest === 'unreadable' || rest.startsWith('unknown-field:')) return { i, path: null, reason: rest };
|
|
35
|
+
const at = rest.indexOf(':');
|
|
36
|
+
return at < 0 ? { i, path: null, reason: rest } : { i, path: rest.slice(0, at), reason: rest.slice(at + 1) };
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
/** the field a red names, as the signed spec names it: the first name of the path, a list entry's `[j]` and a nested `.name` left off @param {unknown} item @returns {string|null} */
|
|
40
|
+
export function itemFieldOf(item) {
|
|
41
|
+
const p = parseShapeItem(item);
|
|
42
|
+
return p && p.path !== null ? p.path.split(/[.[]/)[0] : null;
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
/**
|
|
46
|
+
* One item red as a plain sentence: `item 2: "summary" is too long (201 characters, at most 200)`.
|
|
47
|
+
* @param {unknown} item `#<i>:<path>:<reason>` @returns {string}
|
|
48
|
+
*/
|
|
49
|
+
export function plainShapeItem(item) {
|
|
50
|
+
const p = parseShapeItem(item);
|
|
51
|
+
if (p === null) return String(item);
|
|
52
|
+
const at = `item ${p.i + 1}`;
|
|
53
|
+
const r = p.reason;
|
|
54
|
+
if (p.path === null) {
|
|
55
|
+
if (r === 'item-not-object') return `${at} is not an object with named fields`;
|
|
56
|
+
if (r === 'unreadable') return `${at} could not be read`;
|
|
57
|
+
if (r.startsWith('unknown-field:')) return `${at} has a field "${r.slice('unknown-field:'.length)}" that the job does not ask for`;
|
|
58
|
+
return `${at}: ${r}`;
|
|
59
|
+
}
|
|
60
|
+
const f = `"${pathWords(p.path)}"`;
|
|
61
|
+
const w = /^(too-short|too-long):(chars|words):(\d+)([<>])(\d+)$/.exec(r);
|
|
62
|
+
if (w) {
|
|
63
|
+
const unit = w[2] === 'chars' ? 'characters' : 'words';
|
|
64
|
+
return w[1] === 'too-long' ? `${at}: ${f} is too long (${w[3]} ${unit}, at most ${w[5]})` : `${at}: ${f} is too short (${w[3]} ${unit}, at least ${w[5]})`;
|
|
65
|
+
}
|
|
66
|
+
const c = /^(count-under|count-over):(\d+)([<>])(\d+)$/.exec(r);
|
|
67
|
+
if (c) return c[1] === 'count-over' ? `${at}: ${f} has ${c[2]} entries, at most ${c[4]} are allowed` : `${at}: ${f} has ${c[2]} entries, at least ${c[4]} are required`;
|
|
68
|
+
const t = /^too-many:(\d+)>(\d+)$/.exec(r);
|
|
69
|
+
if (t) return `${at}: ${f} has too many entries to check (${t[1]} against a limit of ${t[2]})`;
|
|
70
|
+
if (r.startsWith('unknown-field:')) return `${at}: ${f} has a field "${r.slice('unknown-field:'.length)}" that the job does not ask for`;
|
|
71
|
+
if (Object.hasOwn(READS, r)) return `${at}: ${f} is not ${/** @type {Record<string, string>} */ (READS)[r]} as written`;
|
|
72
|
+
switch (r) {
|
|
73
|
+
case 'missing': return `${at}: ${f} is missing`;
|
|
74
|
+
case 'wrong-type': return `${at}: ${f} is not the kind of value the job asks for`;
|
|
75
|
+
case 'not-one-of': return `${at}: ${f} is not one of the allowed values`;
|
|
76
|
+
case 'wrong-value': return `${at}: ${f} does not have the value the job asks for`;
|
|
77
|
+
case 'under': return `${at}: ${f} is below the lowest value allowed`;
|
|
78
|
+
case 'over': return `${at}: ${f} is above the highest value allowed`;
|
|
79
|
+
case 'not-a-list': return `${at}: ${f} is not a list`;
|
|
80
|
+
case 'unreadable': return `${at}: ${f} could not be read`;
|
|
81
|
+
case 'duplicate': return `${at}: ${f} repeats an earlier entry`;
|
|
82
|
+
default: return `${at}: ${f} ${r}`;
|
|
83
|
+
}
|
|
84
|
+
}
|
|
85
|
+
|
|
86
|
+
/**
|
|
87
|
+
* One bareguard `itemShape` gap as plain lines (one per item red, or one for a whole-field gap).
|
|
88
|
+
* @param {{at: string, file: string, field?: string}} where `at` = `line N`, `file` = the step file's path, `field` = the answer field holding the items (default `results`)
|
|
89
|
+
* @param {any} g the gap @returns {string}
|
|
90
|
+
*/
|
|
91
|
+
export function plainShapeGap({ at, file, field = 'results' }, g) {
|
|
92
|
+
const f = `"${field}"`;
|
|
93
|
+
switch (g.kind) {
|
|
94
|
+
case 'missing': return `${at}: ${file} has no ${f} list — give a list of items, one object per item`;
|
|
95
|
+
case 'unreadable': return `${at}: ${f} in ${file} could not be read`;
|
|
96
|
+
case 'not-a-list': return `${at}: ${f} in ${file} is not a list (it is ${measuredWords(g.measured)}) — give a list of items`;
|
|
97
|
+
case 'too-many': return `${at}: ${f} lists too many items to check (${g.measured} against a limit of ${g.limit})`;
|
|
98
|
+
case 'count-under': return `${at}: ${f} lists ${g.measured} item(s), at least ${g.limit} are required`;
|
|
99
|
+
case 'count-over': return `${at}: ${f} lists ${g.measured} item(s), at most ${g.limit} are allowed`;
|
|
100
|
+
case 'item-shape': {
|
|
101
|
+
const items = Array.isArray(g.items) ? g.items : [];
|
|
102
|
+
const lines = items.map((/** @type {unknown} */ it) => `${at}: ${plainShapeItem(it)}`);
|
|
103
|
+
const more = typeof g.itemsTotal === 'number' ? g.itemsTotal - items.length : 0;
|
|
104
|
+
return [...lines, ...(more > 0 ? [`${at}: and ${more} more problem(s) in the items`] : [])].join('\n');
|
|
105
|
+
}
|
|
106
|
+
default: return `${at}: the items cannot be checked (${String(g.kind)})`;
|
|
107
|
+
}
|
|
108
|
+
}
|
|
109
|
+
|
|
110
|
+
/** the number of reds a gap counts for the stage's trend number: each item red is one, a whole-field gap is one @param {any} g @returns {number} */
|
|
111
|
+
export const shapeGapCount = (g) => (g.kind === 'item-shape' ? (typeof g.itemsTotal === 'number' ? g.itemsTotal : Array.isArray(g.items) ? g.items.length : 1) : 1);
|
|
112
|
+
|
|
113
|
+
/**
|
|
114
|
+
* The inverse of {@link plainShapeItem}: the item red (`#<i>:<path>:<reason>`) a plain sentence of ours says, or null when the text is none of ours. A red run records only the close's plain gap lines (the per-row record
|
|
115
|
+
* is the final close's), so the Result tab's grid reads them back through this; a test walks every reason through both directions.
|
|
116
|
+
* @param {string} text `item 3: "status" is not one of the allowed values` @returns {string|null}
|
|
117
|
+
*/
|
|
118
|
+
export function shapeItemOfPlain(text) {
|
|
119
|
+
const t = String(text).trim();
|
|
120
|
+
let m = /^item (\d+) (?:is not an object with named fields|could not be read|has a field "(.*)" that the job does not ask for)$/s.exec(t);
|
|
121
|
+
if (m) { const i = Number(m[1]) - 1; return t.endsWith('named fields') ? `#${i}:item-not-object` : t.endsWith('be read') ? `#${i}:unreadable` : `#${i}:unknown-field:${m[2]}`; }
|
|
122
|
+
m = /^item (\d+): "(.*?)" (.*)$/s.exec(t);
|
|
123
|
+
if (!m) return null;
|
|
124
|
+
const i = Number(m[1]) - 1;
|
|
125
|
+
const path = m[2].replace(/, entry (\d+)/g, (_x, j) => `[${Number(j) - 1}]`);
|
|
126
|
+
const r = m[3];
|
|
127
|
+
const at = (/** @type {string} */ reason) => `#${i}:${path}:${reason}`;
|
|
128
|
+
let w = /^is too (long|short) \((\d+) (characters|words), (?:at most|at least) (\d+)\)$/.exec(r);
|
|
129
|
+
if (w) return at(`too-${w[1]}:${w[3] === 'characters' ? 'chars' : 'words'}:${w[2]}${w[1] === 'long' ? '>' : '<'}${w[4]}`);
|
|
130
|
+
w = /^has (\d+) entries, (at most|at least) (\d+) (?:are allowed|are required)$/.exec(r);
|
|
131
|
+
if (w) return at(`count-${w[2] === 'at most' ? 'over' : 'under'}:${w[1]}${w[2] === 'at most' ? '>' : '<'}${w[3]}`);
|
|
132
|
+
w = /^has too many entries to check \((\d+) against a limit of (\d+)\)$/.exec(r);
|
|
133
|
+
if (w) return at(`too-many:${w[1]}>${w[2]}`);
|
|
134
|
+
const table = /** @type {Record<string, string>} */ ({
|
|
135
|
+
'is missing': 'missing', 'is not the kind of value the job asks for': 'wrong-type', 'is not one of the allowed values': 'not-one-of', 'does not have the value the job asks for': 'wrong-value',
|
|
136
|
+
'is below the lowest value allowed': 'under', 'is above the highest value allowed': 'over', 'is not a list': 'not-a-list', 'could not be read': 'unreadable', 'repeats an earlier entry': 'duplicate',
|
|
137
|
+
'is not a number as written': 'bad-number', 'is not a money amount as written': 'bad-money', 'is not a date as written': 'bad-date', 'is not a time as written': 'bad-time', 'is not a web address as written': 'bad-url',
|
|
138
|
+
});
|
|
139
|
+
return Object.hasOwn(table, r) ? at(table[r]) : null;
|
|
140
|
+
}
|
package/src/sheetbuild.js
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
|
-
//
|
|
2
|
-
//
|
|
1
|
+
// borrowed-from: bareloop src/sheetbuild.js@v0.60.0 (`buildSheetChecks`, and the `line` routing field `buildSheetLine` strips; bareloop copied `buildSheetLine` from this file, M17 am2/am3, so it stays as it is)
|
|
2
|
+
// M17 am2 + am3 (docs/wiki/the-module-ladder.md "M17 am2", "M17 am3"): the ONE builder of the sheet checks of a line. The AI never writes a sheet check: what it sends on the row-edit reply (`sheetChecks`) is read here only as a typed SUGGESTION (bareguard's rule fields plus the `covers` numbers); code refuses or builds every one on bareguard's shapes and writes the check itself.
|
|
3
3
|
// Ported from poc/f195-agentrows/build.mjs (F195 PASS, $0); no class code, no send-back loop, no word-to-column guess, no per-case validator patch.
|
|
4
4
|
// refusals (each by bareguard, in this order): the vocabulary (rule, op), the line's sheets, the schema (a field of the rule), `columnAt` (a column of the sheet's header), `checkTables`
|
|
5
5
|
// (and its problems), `groundedLiterals` (a number or date in the person's own words), `claimSchema` (an answer box), and for a `table` check `runTableChecks` on the sheet frozen at draft time.
|
|
@@ -58,7 +58,9 @@ export async function buildSheetLine({ line, tables, suggestions }) {
|
|
|
58
58
|
if (!SHEET_RULES.includes(f.rule) || !isRec(V.rules[f.rule]?.ops) || !Object.hasOwn(V.rules[f.rule].ops, f.op)) { refuse('vocabulary', `no ${String(f.rule)} op "${String(f.op)}"`); continue; }
|
|
59
59
|
const role = typeof f.table === 'string' ? f.table : roles.length === 1 ? roles[0] : null;
|
|
60
60
|
if (role === null || !roles.includes(role)) { refuse('tables', `"${String(f.table)}" is not one of the line's sheets`); continue; }
|
|
61
|
-
|
|
61
|
+
// `line` is the reply's own routing field (src/sheetoffer.js `sheetSuggestProp`), read by buildSheetChecks, never part of a check
|
|
62
|
+
const { line: _line, ...own } = f;
|
|
63
|
+
const check = { ...own, table: role };
|
|
62
64
|
const bad = Object.keys(check).filter((k) => k !== 'rule' && !Object.hasOwn(V.rules[f.rule].fields, k));
|
|
63
65
|
if (bad.length > 0) { refuse('schema', `field ${bad.join(', ')} is not a ${f.rule} field`); continue; }
|
|
64
66
|
const probe = { id: 'probe', ...check, ...(f.rule === 'cells' ? { claim: 'probe' } : {}) };
|
|
@@ -104,39 +106,49 @@ export async function buildSheetLine({ line, tables, suggestions }) {
|
|
|
104
106
|
return { rows, ruleRows, agentRows, checks: [...ruleRows, ...agentRows].map((p) => p.check), refused };
|
|
105
107
|
}
|
|
106
108
|
|
|
109
|
+
// borrowed-from: bareloop src/sheetbuild.js@v0.60.0 (`buildSheetChecks`: the suggestions and the sheet lines in, one result per line out; the check block (`setSheet`) holds the rows. fwd's own `buildSheetChecks` rewrote a declaration's
|
|
110
|
+
// `close.sheet` per step; the declaration is now made from the block's rows by src/blockassemble.js)
|
|
107
111
|
/**
|
|
108
|
-
*
|
|
109
|
-
*
|
|
110
|
-
* @param {{
|
|
111
|
-
* @returns {Promise<{
|
|
112
|
-
*
|
|
112
|
+
* Build the checks of every sheet line from the reply's suggestions, each routed to its job line by its own `line`. Every sheet line in `lines` gets a result, a line with no suggestion included (its rules are
|
|
113
|
+
* then Not checked with the no-pick sentence, never silently nothing). A suggestion that names no sheet line, or is not an object, is refused and listed with its helper.
|
|
114
|
+
* @param {{lines: {n: number, text?: string, rules?: string[], guardrail?: string, rule?: string}[], tables: Record<string, {header: string[], rows: any[]}>, suggestions: any[]}} o
|
|
115
|
+
* @returns {Promise<{built: Record<number, Awaited<ReturnType<typeof buildSheetLine>>>, checks: any[], notChecked: Record<string, string>, refused: {line: number|null, helper: string, why: string}[]}>}
|
|
116
|
+
* `notChecked`: `line N: "rule"` -> the refusal that named it (or `NO_VALUE_WHY`), for each rule of a sheet line that no check holds
|
|
113
117
|
*/
|
|
114
|
-
export async function buildSheetChecks({
|
|
118
|
+
export async function buildSheetChecks({ lines, tables, suggestions }) {
|
|
119
|
+
/** @type {Record<number, Awaited<ReturnType<typeof buildSheetLine>>>} */
|
|
120
|
+
const built = {};
|
|
115
121
|
/** @type {Record<string, string>} */
|
|
116
122
|
const notChecked = {};
|
|
117
|
-
/** @type {{line: number, helper: string, why: string}[]} */
|
|
123
|
+
/** @type {{line: number|null, helper: string, why: string}[]} */
|
|
118
124
|
const refused = [];
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
const
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
if (
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
const
|
|
129
|
-
|
|
130
|
-
for (const
|
|
131
|
-
const
|
|
132
|
-
steps.push({ ...st, close: built.checks.length > 0 ? { ...close, sheet: built.checks } : close });
|
|
125
|
+
/** @type {Map<number, any[]>} */
|
|
126
|
+
const byLine = new Map(lines.map((l) => [l.n, []]));
|
|
127
|
+
for (const s of suggestions) {
|
|
128
|
+
if (!isRec(s)) { refused.push({ line: null, helper: 'schema', why: 'a suggestion is not an object' }); continue; }
|
|
129
|
+
const mine = byLine.get(/** @type {number} */ (s.line));
|
|
130
|
+
if (mine === undefined) { refused.push({ line: Number.isInteger(s.line) ? s.line : null, helper: 'line', why: `line ${String(s.line)} is not a sheet line` }); continue; }
|
|
131
|
+
mine.push(s);
|
|
132
|
+
}
|
|
133
|
+
for (const line of lines) {
|
|
134
|
+
const r = await buildSheetLine({ line, tables, suggestions: byLine.get(line.n) ?? [] });
|
|
135
|
+
built[line.n] = r;
|
|
136
|
+
for (const x of r.refused) refused.push({ line: line.n, ...x });
|
|
137
|
+
for (const row of r.rows) if (row.state === 'not-checked') notChecked[`line ${line.n}: "${row.text}"`] = /** @type {string} */ (row.reason);
|
|
133
138
|
}
|
|
134
|
-
|
|
135
|
-
|
|
139
|
+
return { built, checks: lines.flatMap((l) => built[l.n].checks), notChecked, refused };
|
|
140
|
+
}
|
|
141
|
+
|
|
142
|
+
/**
|
|
143
|
+
* M17 am5 / P5 (fwd's own, kept from the declaration-rewriting builder): the green steps that read a sheet and hold no check after the build (no suggestion, or none code could build), each with its rules as Not-checked lines
|
|
144
|
+
* and the refusal that named each (else the no-pick sentence). What class such a step gets is the caller's to decide (src/blockassemble.js closeUnbuiltSheetSteps); this file writes no class.
|
|
145
|
+
* @param {{declaration: any, lines: {n: number, text?: string, rules?: string[], guardrail?: string, rule?: string}[], roles: string[], notChecked: Record<string, string>}} o
|
|
146
|
+
* @returns {{line: number, lost: string[]}[]}
|
|
147
|
+
*/
|
|
148
|
+
export function unbuiltSheetLines({ declaration, lines, roles, notChecked }) {
|
|
136
149
|
/** @type {{line: number, lost: string[]}[]} */
|
|
137
150
|
const unbuilt = [];
|
|
138
|
-
const
|
|
139
|
-
for (const st of steps) {
|
|
151
|
+
for (const st of isRec(declaration) && Array.isArray(declaration.steps) ? declaration.steps : []) {
|
|
140
152
|
if (!isRec(st) || !isRec(st.close) || st.close.class !== 'green' || (Array.isArray(st.close.sheet) && st.close.sheet.length > 0) || isRec(st.close.cited) || !Number.isInteger(st.fromLine)) continue;
|
|
141
153
|
if (!Array.isArray(st.reads) || !st.reads.some((/** @type {string} */ r) => roles.includes(r)) || unbuilt.some((u) => u.line === st.fromLine)) continue;
|
|
142
154
|
const line = lines.find((l) => l.n === st.fromLine);
|
|
@@ -144,5 +156,5 @@ export async function buildSheetChecks({ declaration, lines, tables }) {
|
|
|
144
156
|
if (rules.length === 0) continue;
|
|
145
157
|
unbuilt.push({ line: st.fromLine, lost: rules.map((r) => `line ${st.fromLine}: "${r}" \u2014 ${notChecked[`line ${st.fromLine}: "${r}"`] ?? NO_VALUE_WHY}`) });
|
|
146
158
|
}
|
|
147
|
-
return
|
|
159
|
+
return unbuilt;
|
|
148
160
|
}
|
package/src/sheetclaims.js
CHANGED
|
@@ -6,7 +6,7 @@
|
|
|
6
6
|
import { rubricVocabulary, claimSchema } from 'bareguard';
|
|
7
7
|
import { bareguardChecks } from './sheetchecks.js';
|
|
8
8
|
|
|
9
|
-
const isRec = (/** @type {unknown} */ v) => v !== null && typeof v === 'object' && !Array.isArray(v);
|
|
9
|
+
export const isRec = (/** @type {unknown} */ v) => v !== null && typeof v === 'object' && !Array.isArray(v);
|
|
10
10
|
|
|
11
11
|
/**
|
|
12
12
|
* The claim box of every `cells` check in the list: `[{claim, example}]`, one per claim name, in order. `example` is the op's worked-example answer (the box's shape
|
|
@@ -56,3 +56,51 @@ export function sheetBrief(checks) {
|
|
|
56
56
|
fields: Object.fromEntries(sheetClaims(checks).map(({ claim, example }) => [claim, example])),
|
|
57
57
|
};
|
|
58
58
|
}
|
|
59
|
+
|
|
60
|
+
// borrowed-from: bareloop src/sheetclaims.js@v0.60.0 (sheetTotalLines, PLAIN_CELL_NUMBER, ROW_MATH_OPS, fmt; the F356 strict number test). Adapted (M15 am3a): fwd's boxes are separate, so the rows come from the line's `rows` check of the same table and `where`.
|
|
61
|
+
export const ROW_MATH_OPS = Object.freeze(['sumIf', 'countIf']);
|
|
62
|
+
|
|
63
|
+
/** A strict plain number: optional sign, digits (commas only as thousands groups of three), optional decimals. `12,50`, `1e3`, `0x10` are not numbers. */
|
|
64
|
+
const PLAIN_CELL_NUMBER = /^[+-]?(?:\d{1,3}(?:,\d{3})+|\d+)(?:\.\d+)?$/;
|
|
65
|
+
const fmt = (/** @type {number} */ n, /** @type {number} */ d) => Number(n.toFixed(d)).toLocaleString('en-US', { minimumFractionDigits: d, maximumFractionDigits: d });
|
|
66
|
+
const sameWhere = (/** @type {any} */ a, /** @type {any} */ b) => a.table === b.table && JSON.stringify(a.where ?? null) === JSON.stringify(b.where ?? null);
|
|
67
|
+
|
|
68
|
+
/**
|
|
69
|
+
* The plain lines the report carries for each sumIf / countIf check: `Count of rows 3, 5, 9: 3 (computed by code from the rows named)` and `Total of rows 3, 5, 9: 1,234.50 (computed by code from the rows named)`.
|
|
70
|
+
* Computed from the FROZEN table and the rows the step named in the `rows` check of the same table and `where` (M15 am3a), never from a number the step wrote. A check with no such `rows` check, or whose box holds no list
|
|
71
|
+
* of integers, writes no line. A named row that is no row of the sheet adds nothing; a blank cell is skipped; a cell that is no strict number makes the total "not computed", naming the row and the column.
|
|
72
|
+
* @param {any[]} checks the step's sheet checks as signed @param {any} claims the step's boxes by claim name @param {Record<string, {header: string[], rows: {rowNumber: number, cells: string[]}[]}>} tables by input label
|
|
73
|
+
* @returns {string[]}
|
|
74
|
+
*/
|
|
75
|
+
export function sheetTotalLines(checks, claims, tables) {
|
|
76
|
+
/** @type {string[]} */
|
|
77
|
+
const out = [];
|
|
78
|
+
for (const c of checks) {
|
|
79
|
+
if (!isRec(c) || c.rule !== 'cells' || !ROW_MATH_OPS.includes(c.op)) continue;
|
|
80
|
+
const rowsCheck = checks.find((x) => isRec(x) && x.rule === 'cells' && x.op === 'rows' && typeof x.claim === 'string' && sameWhere(x, c));
|
|
81
|
+
const box = rowsCheck && isRec(claims) && isRec(claims[rowsCheck.claim]) ? claims[rowsCheck.claim].rows : undefined;
|
|
82
|
+
const table = tables[c.table];
|
|
83
|
+
if (!table || !Array.isArray(box) || !box.every((/** @type {unknown} */ x) => Number.isInteger(x))) continue;
|
|
84
|
+
const named = [...new Set(/** @type {number[]} */ (box))].sort((a, b) => a - b);
|
|
85
|
+
const byRow = new Map(table.rows.map((r) => [r.rowNumber, r]));
|
|
86
|
+
const rows = named.filter((n) => byRow.has(n));
|
|
87
|
+
const list = rows.length > 0 ? `rows ${rows.join(', ')}` : 'no rows';
|
|
88
|
+
if (c.op === 'countIf') { out.push(`Count of ${list}: ${rows.length} (computed by code from the rows named)`); continue; }
|
|
89
|
+
const col = table.header.indexOf(c.column);
|
|
90
|
+
let sum = 0;
|
|
91
|
+
let decimals = 0;
|
|
92
|
+
/** @type {number|null} */
|
|
93
|
+
let bad = null;
|
|
94
|
+
for (const n of rows) {
|
|
95
|
+
const cell = String(byRow.get(n)?.cells[col] ?? '').trim();
|
|
96
|
+
if (cell === '') continue;
|
|
97
|
+
if (!PLAIN_CELL_NUMBER.test(cell)) { bad = n; break; }
|
|
98
|
+
sum += Number(cell.replaceAll(',', ''));
|
|
99
|
+
decimals = Math.max(decimals, (cell.split('.')[1] ?? '').length);
|
|
100
|
+
}
|
|
101
|
+
out.push(bad === null
|
|
102
|
+
? `Total of ${list}: ${fmt(sum, Math.min(decimals, 6))} (computed by code from the rows named)`
|
|
103
|
+
: `Total of ${list}: not computed (row ${bad} has no number in the column "${c.column}")`);
|
|
104
|
+
}
|
|
105
|
+
return out;
|
|
106
|
+
}
|
package/src/sheetfind.js
CHANGED
|
@@ -48,5 +48,13 @@ export function findColumn(csvText, word) {
|
|
|
48
48
|
* @param {string} word @param {FoundAsk} found @param {number} line @returns {string}
|
|
49
49
|
*/
|
|
50
50
|
export const columnQuestion = (word, found, line, tied = false) => `no column '${word}'; '${found.column}' has numbers in ${found.numbers} of ${found.rows} rows. Use it?${columnTail(line, tied)}`;
|
|
51
|
+
|
|
52
|
+
// borrowed-from: bareloop src/sheetfind.js@v0.60.0 (columnTieQuestion; the tail is fwd's signed M17 am9 item 1, a closed question: no `Answer "agree"`)
|
|
53
|
+
/**
|
|
54
|
+
* The question for a tie (M17 am9): the real count, the columns in sheet order, then the tail of a `~` rule's closed question.
|
|
55
|
+
* @param {string} word @param {string[]} columns @param {number} line @returns {string}
|
|
56
|
+
*/
|
|
57
|
+
export const columnTieQuestion = (word, columns, line) => `I found ${columns.length} columns matching '${word}': ${columns.join(', ')}. Choose one. (your answer replaces that ~ rule on line ${line}).`;
|
|
58
|
+
|
|
51
59
|
/** @param {number} line @param {boolean} [tied] the question is about one of the person's `~` rules (M17 ruling 1: the answer replaces that rule; the words are the page asks' own) @returns {string} the answer words the column question ends with (M10 am29: the panel cuts exactly this) */
|
|
52
60
|
export const columnTail = (line, tied = false) => (tied ? ` Answer "agree", or type your own words (your answer replaces that ~ rule on line ${line}).` : ` Answer "agree", or type your own words (your answer becomes the agent's check on line ${line}).`);
|
package/src/sheetlook.js
CHANGED
|
@@ -10,7 +10,7 @@ import { parseCsv } from './csv.js';
|
|
|
10
10
|
import { MAP_UNREADABLE } from './pagemap.js';
|
|
11
11
|
import { tieOf } from './pagerules.js';
|
|
12
12
|
import { lineRules, countPhrase, wordsSaidOn, wordForms } from './pagebits.js';
|
|
13
|
-
import { findColumn, columnQuestion } from './sheetfind.js';
|
|
13
|
+
import { findColumn, columnQuestion, columnTieQuestion } from './sheetfind.js';
|
|
14
14
|
|
|
15
15
|
/** how many data rows the prompt shows (the header is always shown): enough to see what a column holds, never the whole sheet */
|
|
16
16
|
export const SHEET_PROMPT_ROWS = 5;
|
|
@@ -109,6 +109,16 @@ export function applySheetWords(text, words, jobLines, label) {
|
|
|
109
109
|
continue;
|
|
110
110
|
}
|
|
111
111
|
if (w.kind !== 'number') { dropped.push({ ...rec, reason: 'no-column' }); continue; }
|
|
112
|
+
// M17 am9: a tie on a `~` rule's word asks which column (a closed question: the tied columns and each one's reading); a tie on a line's own prose word stays Not checked
|
|
113
|
+
if (found.kind === 'tie' && at.rule !== undefined) {
|
|
114
|
+
ruleWords.add(`${at.line}\u0000${w.word.toLowerCase()}`);
|
|
115
|
+
if (tiedRules.has(`${at.line}\u0000${at.rule}`)) continue;
|
|
116
|
+
asks.push({
|
|
117
|
+
line: at.line, rule: at.rule, source: 'look', ask: 'reading', ruleText, question: columnTieQuestion(w.word, found.columns, at.line), word: w.word,
|
|
118
|
+
columns: found.columns, readings: Object.fromEntries(found.columns.map((c) => [c, columnRuleWords(w.word, c)])),
|
|
119
|
+
});
|
|
120
|
+
continue;
|
|
121
|
+
}
|
|
112
122
|
if (found.kind === 'tie') { dropped.push({ ...rec, reason: 'column-tie', other: found.columns.map((c) => `"${c}"`).join(', ') }); continue; }
|
|
113
123
|
if (found.kind === 'none') { dropped.push({ ...rec, reason: 'no-fit', other: String(found.rows) }); continue; }
|
|
114
124
|
// the person already named this column in a rule of the line (an answer they gave, or their own words): the word is that column, asked no more
|