bareloop 0.55.0 → 0.56.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/CHANGELOG.md +47 -0
- package/bareloop.context.md +4 -2
- package/package.json +2 -2
- package/src/authorflow.js +141 -264
- package/src/authoring.js +2 -3
- package/src/authorjob.js +117 -88
- package/src/authorreadout.js +9 -15
- package/src/authorrun.js +13 -4
- package/src/behaviour.js +14 -0
- package/src/blockasks.js +43 -0
- package/src/checkblock.js +772 -0
- package/src/checklist.js +128 -0
- package/src/checkpick.js +30 -312
- package/src/checkwords.js +3 -1
- package/src/declaredclose.js +30 -1
- package/src/destpoint.js +3 -1
- package/src/draftspend.js +46 -0
- package/src/facts.js +8 -1
- package/src/fitcheck.js +5 -3
- package/src/pagemap.js +15 -3
- package/src/panel/authorroutes.js +15 -3
- package/src/panel/authorsession.js +133 -46
- package/src/panel/index.html +194 -22
- package/src/panel/jobplan.js +85 -33
- package/src/panel/server.js +52 -7
- package/src/pickrecord.js +19 -1
- package/src/placerule.js +355 -0
- package/src/promptregisters.js +11 -0
- package/src/questionbox.js +112 -0
- package/src/rowedit.js +115 -0
- package/src/rubricstep.js +212 -125
- package/src/run.js +10 -2
- package/src/userrun.js +46 -1
- package/types/authorflow.d.ts +32 -139
- package/types/authorjob.d.ts +24 -6
- package/types/authorreadout.d.ts +4 -10
- package/types/behaviour.d.ts +12 -0
- package/types/blockasks.d.ts +37 -0
- package/types/checkblock.d.ts +361 -0
- package/types/checklist.d.ts +134 -0
- package/types/checkpick.d.ts +3 -140
- package/types/declaredclose.d.ts +4 -0
- package/types/destpoint.d.ts +2 -0
- package/types/draftspend.d.ts +21 -0
- package/types/fitcheck.d.ts +7 -1
- package/types/pagemap.d.ts +4 -0
- package/types/panel/authorsession.d.ts +13 -2
- package/types/panel/jobgrid.d.ts +8 -8
- package/types/panel/jobplan.d.ts +48 -10
- package/types/panel/server.d.ts +14 -13
- package/types/pickrecord.d.ts +12 -0
- package/types/placerule.d.ts +194 -0
- package/types/questionbox.d.ts +87 -0
- package/types/rowedit.d.ts +32 -0
- package/types/rubricstep.d.ts +91 -48
- package/types/run.d.ts +10 -1
|
@@ -0,0 +1,772 @@
|
|
|
1
|
+
// THE CHECK BLOCK of a rubric job (hamr 2026-11-12: "goal again is simplfication and one flow one place ai suggestes ! code adds $ and code decides what worked/doable").
|
|
2
|
+
// ONE store: `rows`. Nothing else holds a check. Rows are `~` (the person's own rule), `!` (the agent's suggestion: a look fact, or an AI edit) and `$` (code's defaults).
|
|
3
|
+
// The AI's whole part is a list of proposed ROW EDITS; code judges each edit alone, matches every row against the saved page, builds every check through the bareguard-backed
|
|
4
|
+
// shapes, and says why a row it cannot build is Not checked. The block never removes a row: only the person (`applyAnswer`) or code (`match`) changes one.
|
|
5
|
+
// fromJob -> addLook -> reconcile( match -> build -> ask ), then, while a question is open or a reply waits: apply it and reconcile the WHOLE list again.
|
|
6
|
+
// Pure: no I/O, no clock, no model. Rubric jobs only (repo jobs never run through it).
|
|
7
|
+
import { countExact, countPhrase, checkFactWords, keptSetOf } from './factwords.js';
|
|
8
|
+
import { isCountable, keepOrDrop, countCheck, partOfPick, plainWords, sameRuleWords } from './checkpick.js';
|
|
9
|
+
import { dropRows } from './pagedropwords.js';
|
|
10
|
+
import { boundInWords, rubricStepShapeReds, defaultCitedText, readLimit, holdsManyBounds, NEGATING_RE } from './rubricstep.js';
|
|
11
|
+
import { validReading } from './pagerules.js';
|
|
12
|
+
import { weekOnly, weekTokens, WEEK_ONLY_WORDS, MAX_BANG } from './checklist.js';
|
|
13
|
+
import { fitPlacePhrase } from './placerule.js';
|
|
14
|
+
import { ruleWords } from './facts.js';
|
|
15
|
+
import { SIGNED_ROW_FIELDS } from './declaredclose.js';
|
|
16
|
+
import { factSentence, countSentence } from './facts.js';
|
|
17
|
+
|
|
18
|
+
/**
|
|
19
|
+
* A row. `kind`: rule, fact, phrase, count, cited, numbers, floor. `by`: you (`~`), agent (`!`), default (`$`). A `~` row may carry `links` (the look's facts tied to it),
|
|
20
|
+
* `pick`, `aiReading`, `left`. `state` is built or not-checked; a not-checked row always has a plain `reason`.
|
|
21
|
+
* @typedef {Record<string, any>} Row
|
|
22
|
+
*/
|
|
23
|
+
|
|
24
|
+
/** the card's fixed words, one spelling (the renderer reads them; a test reads them too) */
|
|
25
|
+
export const CARD_T = Object.freeze({ agreed: 'you agreed:', count: 'result count', cited: 'every claim is backed by a quote copied from', theInput: 'the input', numbers: 'every number in the text is in a checked quote of line', withWords: 'with the words' });
|
|
26
|
+
|
|
27
|
+
/** the four row edits an AI reply may hold */
|
|
28
|
+
export const EDIT_OPS = Object.freeze(['add', 'reading', 'pick', 'not-checkable']);
|
|
29
|
+
|
|
30
|
+
const norm = (/** @type {unknown} */ t) => String(t ?? '').toLowerCase().replace(/\s+/g, ' ').trim();
|
|
31
|
+
const isRec = (/** @type {unknown} */ v) => v !== null && typeof v === 'object' && !Array.isArray(v);
|
|
32
|
+
const proved = (/** @type {unknown} */ text, /** @type {unknown} */ w) => typeof text === 'string' && typeof w === 'string' && w.trim() !== '' && (countExact(text, w) > 0 || countPhrase(text, w) > 0);
|
|
33
|
+
const cleanName = (/** @type {unknown} */ n) => String(n ?? '').replace(/[^A-Za-z _-]/g, ' ').replace(/\s+/g, ' ').trim().slice(0, 64);
|
|
34
|
+
/** does `text` hold `w` as whole words (case and spacing aside)? One reader for "its words appear in the job" */
|
|
35
|
+
const inText = (/** @type {unknown} */ text, /** @type {unknown} */ w) => { const a = norm(w); return a !== '' && new RegExp(`(?<![\\p{L}\\p{N}])${a.replace(/[.*+?^${}()|[\]\\]/g, '\\$&')}(?![\\p{L}\\p{N}])`, 'u').test(norm(text)); };
|
|
36
|
+
const wordSet = (/** @type {unknown} */ t) => new Set(norm(t).match(/[\p{L}\p{N}]+/gu) ?? []);
|
|
37
|
+
|
|
38
|
+
/** the plain reasons code gives, in one place (the card, the Job tab and the Audit read them from the row) */
|
|
39
|
+
export const REASON = Object.freeze({
|
|
40
|
+
left: 'You chose to leave it Not checked, so you judge it at Accept.',
|
|
41
|
+
nothingOnPage: 'Your own words are on the line; the machine has no check for them, so you judge it at Accept.',
|
|
42
|
+
cantCheck: "I can't check this on the page; waiting for your answer",
|
|
43
|
+
noRule: 'No rule can check this, so you judge it at Accept.',
|
|
44
|
+
waitingPick: 'waiting for your answer on the proposed check',
|
|
45
|
+
waitingReading: 'waiting for your answer on the reading',
|
|
46
|
+
onePerLine: 'this line finds facts, so it has no text to quote',
|
|
47
|
+
manyInputs: 'the job has more than one input and code cannot pick which one the claims quote',
|
|
48
|
+
oneSidedTime: 'a time limit the AI proposes must be a range, so you judge it at Accept.',
|
|
49
|
+
countNeedsFact: 'a result count needs a fact to count; this line has none',
|
|
50
|
+
lookNoWords: "the page does not hold the words of the look's fact for this rule",
|
|
51
|
+
afterLast: 'no more tries: Start over (rows are never changed by the AI after the last try)',
|
|
52
|
+
noPick: 'No check was picked for it, so you judge it at Accept.',
|
|
53
|
+
negated: 'it says what is not needed, so there is nothing to check',
|
|
54
|
+
dropped: 'You dropped it.',
|
|
55
|
+
suggested: 'Suggested: not in your job, so it is not checked unless you add it.',
|
|
56
|
+
});
|
|
57
|
+
|
|
58
|
+
/** the card lines: rows in, lines out. ONE pure function of the rows; nothing else may feed it. @param {Row[]} rows */
|
|
59
|
+
export function renderCard(rows) {
|
|
60
|
+
return rows.filter((r) => r.kind !== 'floor' && r.state !== 'suggested' && r.state !== 'dropped' && !r.hidden).map((r) => {
|
|
61
|
+
let body;
|
|
62
|
+
if (r.kind === 'rule') body = `"${r.text}"${r.pick ? ` -> ${plainWords(r.pick)}` : ''}${r.agreedText ? ` (${CARD_T.agreed} ${r.agreedText})` : ''}`;
|
|
63
|
+
else if (r.kind === 'count') body = `${CARD_T.count} ${r.min ?? ''}-${r.max ?? ''}`;
|
|
64
|
+
else if (r.kind === 'cited') body = `${CARD_T.cited} "${r.source ?? CARD_T.theInput}"`;
|
|
65
|
+
else if (r.kind === 'numbers') body = `${CARD_T.numbers} ${(r.after ?? []).join(', ')}`;
|
|
66
|
+
else body = `${r.kind} "${r.name ?? r.words?.[0] ?? ''}"${r.words?.length ? ` ${CARD_T.withWords} ${r.words.map((/** @type {string} */ w) => `"${w}"`).join(', ')}` : ''}`;
|
|
67
|
+
return { key: r.key, line: r.line, by: r.by, state: r.state, reason: r.reason ?? null, text: `${r.by === 'you' ? '~' : r.by === 'agent' ? '!' : '$'} ${body}` };
|
|
68
|
+
});
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
/**
|
|
72
|
+
* The facts a place fit builds (hamr 2026-11-11, plain split names): one value keeps the fact's own name; a version AND a date become TWO facts, "<name> version" and "<name> date", each with its
|
|
73
|
+
* own place rule. The words version, date and "and" leave the base name first, so "release version" is not named "release version version".
|
|
74
|
+
* @param {string} name @param {{kind: string, value: string, rule: any}[]} strips @param {(n: string, extra: any) => any} mk
|
|
75
|
+
*/
|
|
76
|
+
function placeFacts(name, strips, mk) {
|
|
77
|
+
if (strips.length === 1) return [mk(name, { shape: 'phrase', phrase: strips[0].value, place: strips[0].rule })];
|
|
78
|
+
const base = String(name).replace(/\b(?:version|date|and)\b/gi, ' ').replace(/\s+/g, ' ').trim();
|
|
79
|
+
const nm = (/** @type {string} */ sfx) => (base === '' ? sfx : `${base.slice(0, 64 - sfx.length - 1).trimEnd()} ${sfx}`);
|
|
80
|
+
return strips.map((s) => mk(nm(s.kind), { shape: 'phrase', phrase: s.value, place: s.rule }));
|
|
81
|
+
}
|
|
82
|
+
|
|
83
|
+
export class CheckBlock {
|
|
84
|
+
/** @param {{texts: Record<string, string>, labels: string[], maxTries?: number, countScope?: (ruleText: string) => boolean, asks?: boolean, split?: boolean}} o the frozen input texts, their labels, how many AI replies (tries) may still change a row, whether code scopes a counting `~` rule in (a pick is asked), and whether a person can be asked at all (`asks`: false = a rule code cannot settle is Not checked, a reading code proposes is built as the AI's proposal) */
|
|
85
|
+
constructor({ texts, labels, maxTries = 3, countScope = () => true, asks = true, split = false }) {
|
|
86
|
+
/** the product path sets it: `!` rows split into job-derived and suggested, a suggestion code cannot build is hidden (hamr 2026-11-13). Off, every `!` row is built as before (the older callers and their tests) */
|
|
87
|
+
this.split = split;
|
|
88
|
+
this.countScope = countScope;
|
|
89
|
+
this.asks = asks;
|
|
90
|
+
this.texts = texts;
|
|
91
|
+
this.labels = labels;
|
|
92
|
+
this.maxTries = maxTries;
|
|
93
|
+
/** @type {Row[]} the ONE store */
|
|
94
|
+
this.rows = [];
|
|
95
|
+
/** @type {{n: number, text: string}[]} */
|
|
96
|
+
this.lines = [];
|
|
97
|
+
/** @type {any[]} the look's facts as given (a `reading` edit names one) */
|
|
98
|
+
this.lookFacts = [];
|
|
99
|
+
/** @type {{facts: any[], dropped: any[]}|null} the look as the draft gate reads it (null = no look report: the gate proves a label against the list code made) */
|
|
100
|
+
this.look = null;
|
|
101
|
+
/** @type {any[]} replies waiting to be applied by `drive` */
|
|
102
|
+
this.queue = [];
|
|
103
|
+
/** @type {any[]} the open questions, set by `ask` */
|
|
104
|
+
this.open = [];
|
|
105
|
+
/** @type {string[]} the keys of questions a person did not answer */
|
|
106
|
+
this.unanswered = [];
|
|
107
|
+
this.tries = 0;
|
|
108
|
+
/** @type {any[]} every reply's record: each edit, kept or refused with its own reason */
|
|
109
|
+
this.deltaLog = [];
|
|
110
|
+
/** @type {any} */
|
|
111
|
+
this.closeCache = null;
|
|
112
|
+
}
|
|
113
|
+
|
|
114
|
+
textOf(/** @type {string|undefined} */ source) { return this.texts[source ?? this.labels[0] ?? ''] ?? Object.values(this.texts)[0] ?? ''; }
|
|
115
|
+
/** @param {number} line @param {string} [kind] */
|
|
116
|
+
rowsOn(line, kind) { return this.rows.filter((r) => r.line === line && (kind === undefined || r.kind === kind)); }
|
|
117
|
+
ruleRows(/** @type {number} */ line) { return this.rows.filter((r) => r.kind === 'rule' && r.line === line); }
|
|
118
|
+
/** every word the person wrote on a line: the line and its `~` rules */
|
|
119
|
+
said(/** @type {number} */ line) { return [this.lines.find((l) => l.n === line)?.text ?? '', ...this.ruleRows(line).map((r) => r.text)].join(' '); }
|
|
120
|
+
/** @param {Row} r @returns {Row} the row as stored (a repeated key gets `#2`) */
|
|
121
|
+
addRow(r) {
|
|
122
|
+
const key = this.rows.some((x) => x.key === r.key) ? `${r.key}#${this.rows.filter((x) => x.key.startsWith(r.key)).length + 1}` : r.key;
|
|
123
|
+
const row = { state: 'not-checked', reason: 'not yet matched', words: [], ...r, key };
|
|
124
|
+
this.rows.push(row);
|
|
125
|
+
return row;
|
|
126
|
+
}
|
|
127
|
+
|
|
128
|
+
// 1. the person's rules, and the `$` floor of each line
|
|
129
|
+
/** @param {{n: number, text: string, rules?: string[]}[]} jobLines */
|
|
130
|
+
fromJob(jobLines) {
|
|
131
|
+
for (const l of jobLines) {
|
|
132
|
+
this.lines.push({ n: l.n, text: l.text });
|
|
133
|
+
(l.rules ?? []).forEach((t, i) => this.addRow({ key: `L${l.n}.~${i + 1}`, line: l.n, kind: 'rule', by: 'you', from: 'rule', origin: 'job', text: t, rule: i + 1 }));
|
|
134
|
+
this.addRow({ key: `L${l.n}.$floor`, line: l.n, kind: 'floor', by: 'default', from: 'default', state: 'built', reason: null, text: "the file exists and its text is not empty (code's own floor)" });
|
|
135
|
+
}
|
|
136
|
+
}
|
|
137
|
+
|
|
138
|
+
// 2. the look: a fact tied to a `~` rule is a LINK on that row, never a drop; any other fact is a `!` row
|
|
139
|
+
/**
|
|
140
|
+
* @param {any[]} facts the look's kept facts @param {any[]} [dropped] the look's drops (`look.dropped`): a whole fact a page rule left out. Its plain sentence (src/pagedropwords.js)
|
|
141
|
+
* becomes the `why` of the `~` rule it was tied to, or a Not checked `!` row of its own, so every drop shows under Not checked.
|
|
142
|
+
*/
|
|
143
|
+
addLook(facts, dropped = undefined) {
|
|
144
|
+
if (Array.isArray(dropped)) this.look = { facts: this.lookFacts, dropped };
|
|
145
|
+
dropped ??= [];
|
|
146
|
+
for (const d of dropRows(dropped.filter((/** @type {any} */ x) => x && x.reason !== 'person-left')).filter((x) => x.full)) {
|
|
147
|
+
const why = d.sentences.join(' ');
|
|
148
|
+
const rr = d.rule === undefined ? undefined : this.ruleRows(d.line).find((r) => r.rule === d.rule);
|
|
149
|
+
if (rr) rr.lookWhy = why;
|
|
150
|
+
else this.addRow({ key: `L${d.line}.fact.${norm(d.fact)}`, line: d.line, kind: 'fact', by: 'agent', from: 'look', name: d.fact, shape: 'phrase', words: [], lookWhy: why });
|
|
151
|
+
}
|
|
152
|
+
for (const f of facts) {
|
|
153
|
+
this.lookFacts.push(f);
|
|
154
|
+
// a fact the look tied to a COUNTING `~` rule stays a fact of its own: the pick path owns that rule (R10, hamr 2026-11-09), so the rule uses no link
|
|
155
|
+
const tied = Number.isInteger(f.forRule) ? this.ruleRows(f.line).find((r) => r.rule === f.forRule) : undefined;
|
|
156
|
+
const row = tied && !isCountable(tied.text) ? tied : undefined;
|
|
157
|
+
const carried = { name: f.name, shape: f.shape, words: [...(f.words ?? [])], unit: f.unit, currency: f.currency, decimal: f.decimal, lang: f.lang, source: f.source };
|
|
158
|
+
if (row) { (row.links ??= []).push({ ...carried, reading: f.reading }); continue; }
|
|
159
|
+
this.addRow({ key: `L${f.line}.fact.${norm(f.name)}`, line: f.line, kind: 'fact', by: 'agent', from: 'look', ...carried });
|
|
160
|
+
}
|
|
161
|
+
}
|
|
162
|
+
|
|
163
|
+
// 3. can code check it on the saved page? Never the AI. `row.can` = {ok, how, ...} or {ok:false, why} or {ok:false, ask, ...}
|
|
164
|
+
match() {
|
|
165
|
+
// live draft 3 (hamr 2026-11-13: "anything not in your original job that code can check goes to Suggested"): a `!` fact whose page words are longer than what the job says ("Boston Logan International
|
|
166
|
+
// Airport" for the job's "Boston") says the JOB's word; the row is then job-derived (inJob below, the one owner) and its phrase check uses that word
|
|
167
|
+
if (this.split) for (const r of this.rows) if (this.isBang(r) && (r.kind === 'fact' || r.kind === 'phrase')) this.takeJobWord(r);
|
|
168
|
+
for (const r of this.rows) {
|
|
169
|
+
r.can = null;
|
|
170
|
+
if (r.kind === 'floor') continue;
|
|
171
|
+
if (r.left) r.can = { ok: false, why: REASON.left };
|
|
172
|
+
else if (r.lookWhy && r.kind === 'fact' && !r.words.length) r.can = { ok: false, why: r.lookWhy };
|
|
173
|
+
else if (r.folded) r.can = { ok: false, why: REASON.noPick };
|
|
174
|
+
else if (r.parent) this.matchFollow(r);
|
|
175
|
+
else if (r.kind === 'rule') this.matchRule(r);
|
|
176
|
+
else if (r.kind === 'fact' || r.kind === 'phrase') this.matchBang(r);
|
|
177
|
+
else if (r.kind === 'count') this.matchCount(r);
|
|
178
|
+
}
|
|
179
|
+
// N-1 (hamr 2026-11-13): code splits the `!` rows. Words (or the count number) in the job line or its ~ rules = JOB-DERIVED (built, Drop). Not in the job = SUGGESTED: held out of the close, offered
|
|
180
|
+
// only when code has already proved it can build it (build() trials it), and switched on by the person's Add. The person's Drop removes a row for good. Never the AI.
|
|
181
|
+
for (const r of this.split ? this.rows : []) {
|
|
182
|
+
r.derived = undefined; r.held = false;
|
|
183
|
+
if (!this.isBang(r)) continue;
|
|
184
|
+
r.derived = this.inJob(r);
|
|
185
|
+
// a Drop is undone through Suggested (live draft 3): the row was proved buildable, so it is offered again with Add; only a row code can still build is offered
|
|
186
|
+
if (r.can?.ok && r.choice !== 'add' && (!r.derived || r.choice === 'drop')) r.held = true;
|
|
187
|
+
}
|
|
188
|
+
}
|
|
189
|
+
|
|
190
|
+
/** a `!` row: the agent's fact, phrase or count (a look fact tied to a rule is a link on that rule's row, never a row of its own) */
|
|
191
|
+
isBang(/** @type {Row} */ r) { return r.by === 'agent' && (r.kind === 'fact' || r.kind === 'phrase' || r.kind === 'count') && !r.parent; }
|
|
192
|
+
|
|
193
|
+
/**
|
|
194
|
+
* The job's own word for a `!` row: when none of its page words is in the job line or its `~` rules but a proper name the job holds (capital or digit first, 3+ characters) is a whole-word part of one
|
|
195
|
+
* page word and the page holds it, the row's words become that job word. The page's long name is kept as `pageWords`; the row's other fields are untouched.
|
|
196
|
+
*/
|
|
197
|
+
takeJobWord(/** @type {Row} */ r) {
|
|
198
|
+
if (this.inJob(r)) return;
|
|
199
|
+
const said = this.said(r.line);
|
|
200
|
+
const names = said.match(/[\p{Lu}\p{N}][\p{L}\p{N}'’-]*(?: [\p{Lu}\p{N}][\p{L}\p{N}'’-]*)*/gu) ?? [];
|
|
201
|
+
for (const w of (r.words ?? []).filter((/** @type {unknown} */ x) => typeof x === 'string')) {
|
|
202
|
+
const hit = names.filter((n) => n.length >= 3 && inText(w, n) && proved(this.textOf(r.source), n)).sort((a, b) => b.length - a.length)[0];
|
|
203
|
+
if (hit) { r.pageWords = r.words; r.words = [hit]; return; }
|
|
204
|
+
}
|
|
205
|
+
}
|
|
206
|
+
|
|
207
|
+
/** are the row's words (or its count number, or the name it asks for a fact by) in the job line or its `~` rules? Normalised, case-insensitive */
|
|
208
|
+
inJob(/** @type {Row} */ r) {
|
|
209
|
+
const said = this.said(r.line);
|
|
210
|
+
if (r.kind === 'count') return [r.min, r.max].filter((x) => Number.isInteger(x)).some((n) => new RegExp(`(?<![\\d.])${n}(?![\\d.])`).test(said));
|
|
211
|
+
// a fact the person asked for BY NAME ("the release heading") is in the job too: the look's page words rarely echo the job's own words
|
|
212
|
+
return (r.words ?? []).some((/** @type {string} */ w) => inText(said, w)) || (r.kind === 'fact' && typeof r.name === 'string' && inText(said, r.name));
|
|
213
|
+
}
|
|
214
|
+
|
|
215
|
+
/** a `~` line an agreed answer wrote (or a carried pair partner) is checked by the rule it was agreed from: one check, both numbers */
|
|
216
|
+
matchFollow(/** @type {Row} */ r) {
|
|
217
|
+
const parent = this.rows.find((x) => x.key === r.parent);
|
|
218
|
+
r.can = parent?.can?.ok ? { ok: true, how: 'follow' } : { ok: false, why: parent?.can?.why ?? 'the rule it was agreed from is not checked' };
|
|
219
|
+
}
|
|
220
|
+
|
|
221
|
+
/** a `!` count row is built unless a `~` rule on its line already names the number */
|
|
222
|
+
matchCount(/** @type {Row} */ r) {
|
|
223
|
+
const nums = [r.min, r.max].filter((x) => Number.isInteger(x)).map(String);
|
|
224
|
+
const said = this.ruleRows(r.line).find((x) => nums.some((n) => new RegExp(`(?<![\\d.])${n}(?![\\d.])`).test(x.text)));
|
|
225
|
+
r.can = said ? { ok: false, why: `a ~ rule already says it: line ${r.line} rule ${said.rule} names the number` } : { ok: true, how: 'count' };
|
|
226
|
+
}
|
|
227
|
+
|
|
228
|
+
matchRule(/** @type {Row} */ r) {
|
|
229
|
+
if (isCountable(r.text)) { this.matchCountRule(r); return; }
|
|
230
|
+
// F-a (hamr 2026-11-13): a rule that carries a negation ("nonstop is not mandatory") says what is NOT needed; its words can never give a phrase bound ("must say"). The one negation reader is NEGATING_RE.
|
|
231
|
+
const negated = NEGATING_RE.test(r.text);
|
|
232
|
+
const isPhrase = (/** @type {any} */ l) => l.shape === 'phrase' || l.shape === 'place';
|
|
233
|
+
if (negated && (r.links ?? []).length && (r.links ?? []).every(isPhrase)) { r.can = { ok: false, why: REASON.negated }; return; }
|
|
234
|
+
const links = (r.links ?? []).filter((/** @type {any} */ l) => !(negated && isPhrase(l)) && l.words.some((/** @type {string} */ w) => proved(this.textOf(l.source), w)));
|
|
235
|
+
const said = this.said(r.line);
|
|
236
|
+
let ownWhy = null;
|
|
237
|
+
for (const l of links) {
|
|
238
|
+
let reading = l.reading;
|
|
239
|
+
let ownWords = false;
|
|
240
|
+
if (l.shape === 'money' || l.shape === 'time') {
|
|
241
|
+
// the person's words win (hamr 2026-11-13): code's reading of the `~` words beats the look's and the AI's; theirs is used only when code cannot read the words
|
|
242
|
+
const rd = this.wordsReading(l, r.text);
|
|
243
|
+
if (rd) { reading = rd; ownWords = true; }
|
|
244
|
+
else if (reading === undefined) {
|
|
245
|
+
const own = readLimit(l.shape, r.text, l);
|
|
246
|
+
ownWhy = 'why' in own ? own.why : 'a bound in the rule is not written in your own words';
|
|
247
|
+
}
|
|
248
|
+
}
|
|
249
|
+
reading ??= r.aiReading?.reading;
|
|
250
|
+
if (l.shape === 'money' || l.shape === 'time' || l.shape === 'number' || l.shape === 'date') {
|
|
251
|
+
if (reading === undefined || !validReading(l.shape, reading)) continue; // code does not guess a limit; the AI may still propose one
|
|
252
|
+
const bounds = reading.op === 'between' ? [reading.lo, reading.hi] : [reading.value];
|
|
253
|
+
const unsaid = bounds.some((b) => !boundInWords(b, said));
|
|
254
|
+
if (unsaid && !r.agreed && this.asks) { r.can = { ok: false, ask: 'reading', link: l, reading, proposal: `${l.name} ${ruleWords({ shape: l.shape, unit: l.unit, currency: l.currency, rule: reading })}`, why: REASON.waitingReading }; return; }
|
|
255
|
+
if (unsaid && l.shape === 'time' && reading.op !== 'between') { r.can = { ok: false, why: REASON.oneSidedTime }; return; }
|
|
256
|
+
r.can = { ok: true, how: 'fact', link: l, reading, aiProposal: unsaid && !ownWords && !r.agreed, ...(ownWords ? { readBy: 'your words' } : {}) };
|
|
257
|
+
return;
|
|
258
|
+
}
|
|
259
|
+
// one owner of the week-only decision (hamr 2026-11-11): a version or a date in a linked phrase is fixed words only when the person's own `~` rule names that token; else a place rule fits it or it stays Not checked
|
|
260
|
+
const week = l.shape === 'phrase' ? l.words.filter((/** @type {string} */ w) => proved(this.textOf(l.source), w) && weekOnly(w)) : [];
|
|
261
|
+
if (week.length && !weekTokens(week.join(' ')).some((k) => norm(r.text).includes(norm(k)))) {
|
|
262
|
+
const fits = week.map((/** @type {string} */ w) => fitPlacePhrase(w, this.textOf(l.source)));
|
|
263
|
+
if (fits.some((x) => x === null)) { r.can = { ok: false, why: WEEK_ONLY_WORDS }; return; }
|
|
264
|
+
r.can = { ok: true, how: 'fact', link: l, reading: undefined, strips: fits[0] ?? [] };
|
|
265
|
+
return;
|
|
266
|
+
}
|
|
267
|
+
r.can = { ok: true, how: 'fact', link: l, reading: undefined };
|
|
268
|
+
return;
|
|
269
|
+
}
|
|
270
|
+
if (r.links?.length && !links.length) { r.can = { ok: false, why: REASON.lookNoWords }; return; }
|
|
271
|
+
if (r.origin === 'typed' || r.cantAsked) { r.can = { ok: false, why: r.aiReason ?? r.lookWhy ?? REASON.nothingOnPage }; return; }
|
|
272
|
+
if (r.links?.length) { r.can = { ok: false, why: r.aiReason ?? `the look tied a fact to this rule but gave no limit, and ${ownWhy ? `in your words ${ownWhy}` : 'your own words give none code can read'}, so you judge it at Accept.` }; return; }
|
|
273
|
+
r.can = this.asks ? { ok: false, ask: 'cant-check', why: r.lookWhy ?? REASON.cantCheck } : { ok: false, why: r.lookWhy ?? REASON.noRule };
|
|
274
|
+
}
|
|
275
|
+
|
|
276
|
+
/** a `~` rule that counts words, bullets, lines or sections: a settled pick builds it; else code proposes the check and asks */
|
|
277
|
+
matchCountRule(/** @type {Row} */ r) {
|
|
278
|
+
if (!r.pick && (r.cantAsked || !this.countScope(r.text))) { r.can = { ok: false, why: r.aiReason ?? REASON.noRule }; return; }
|
|
279
|
+
if (r.pick) { r.can = { ok: true, how: 'pick' }; return; }
|
|
280
|
+
const proposal = r.pickProposal ?? countCheck(r.text);
|
|
281
|
+
if (!this.asks) r.can = { ok: false, why: REASON.noRule };
|
|
282
|
+
else if (proposal) r.can = { ok: false, ask: 'check', proposal, why: REASON.waitingPick };
|
|
283
|
+
else r.can = r.origin === 'typed' ? { ok: false, why: REASON.noRule } : { ok: false, ask: 'cant-check', why: REASON.cantCheck };
|
|
284
|
+
}
|
|
285
|
+
|
|
286
|
+
/** a `!` fact or phrase row: its words must be on the page; a version or a date is true only today, so only a place rule's fit may carry it */
|
|
287
|
+
matchBang(/** @type {Row} */ r) {
|
|
288
|
+
const text = this.textOf(r.source);
|
|
289
|
+
const w = (r.words ?? []).filter((/** @type {unknown} */ x) => typeof x === 'string' && x.trim());
|
|
290
|
+
const provedWords = w.filter((/** @type {string} */ x) => proved(text, x));
|
|
291
|
+
if (provedWords.length === 0) { r.can = { ok: false, why: `the words ${w.map((/** @type {string} */ x) => `"${x}"`).join(', ') || '(none)'} are not in the input` }; return; }
|
|
292
|
+
// F-a: a `!` row whose words are in a rule that says what is NOT needed is refused alone, naming the rule
|
|
293
|
+
const no = this.ruleRows(r.line).find((x) => NEGATING_RE.test(x.text) && provedWords.some((/** @type {string} */ p) => inText(x.text, p)));
|
|
294
|
+
if (no) { r.can = { ok: false, why: `your rule ${no.rule} says it is not needed` }; return; }
|
|
295
|
+
if (provedWords.some(weekOnly)) {
|
|
296
|
+
if (r.shape !== 'phrase' && r.kind !== 'phrase') { r.can = { ok: false, why: WEEK_ONLY_WORDS }; return; }
|
|
297
|
+
// every week-only word must fit a place rule (else Not checked, as before); the fact is built from the first one, like a `!` row's first proved word
|
|
298
|
+
const fits = provedWords.filter(weekOnly).map((/** @type {string} */ x) => fitPlacePhrase(x, text));
|
|
299
|
+
if (fits.some((x) => x === null)) { r.can = { ok: false, why: WEEK_ONLY_WORDS }; return; }
|
|
300
|
+
r.can = { ok: true, how: 'place', strips: fits[0] ?? [] };
|
|
301
|
+
return;
|
|
302
|
+
}
|
|
303
|
+
r.can = { ok: true, how: 'bang', word: provedWords[0] };
|
|
304
|
+
}
|
|
305
|
+
|
|
306
|
+
// 4. code makes every check from the rows; a row it cannot build keeps its place with its plain reason. Returns the close's stages, parts and the row map.
|
|
307
|
+
build() {
|
|
308
|
+
/** @type {any[]} */ const stages = [];
|
|
309
|
+
/** @type {any[]} */ const parts = [];
|
|
310
|
+
/** @type {{part: number, line: number, rows: string[], origin: string}[]} */ const map = [];
|
|
311
|
+
/** @type {Set<number>} */ const factLines = new Set();
|
|
312
|
+
for (const l of this.lines) {
|
|
313
|
+
/** @type {any[]} */ const lineParts = [];
|
|
314
|
+
/** @type {string[][]} */ const lineRows = [];
|
|
315
|
+
this.buildLine(l, lineParts, lineRows, factLines);
|
|
316
|
+
if (lineParts.length) stages.push({ name: `line-${l.n}`, kind: 'rubric-step', fromLine: [l.n], params: { step: l.n, parts: lineParts } });
|
|
317
|
+
lineParts.forEach((p, i) => {
|
|
318
|
+
for (const k of lineRows[i] ?? []) { const row = this.rows.find((x) => x.key === k); if (row) row.part = { line: l.n, part: i }; }
|
|
319
|
+
map.push({ part: parts.length, line: l.n, rows: lineRows[i] ?? [], origin: 'code' });
|
|
320
|
+
parts.push(p);
|
|
321
|
+
});
|
|
322
|
+
}
|
|
323
|
+
// a suggestion code has not built stays off the card (Audit only, F-b); a held one is trialled: shown under Suggested only when code has proved it builds (hamr 2026-11-13)
|
|
324
|
+
for (const r of this.rows.filter((x) => x.held)) this.trialOffer(r);
|
|
325
|
+
if (this.split) for (const r of this.rows) r.hidden = this.isBang(r) && r.state === 'not-checked' && !r.lookWhy;
|
|
326
|
+
this.closeCache = { stages, parts, map };
|
|
327
|
+
return this.closeCache;
|
|
328
|
+
}
|
|
329
|
+
|
|
330
|
+
/** build the line as if this held row were on: when it builds, `offer` is the row as the card would draw it; else the row is hidden with code's reason. The rows' own state is restored */
|
|
331
|
+
trialOffer(/** @type {Row} */ r) {
|
|
332
|
+
const l = this.lines.find((x) => x.n === r.line);
|
|
333
|
+
if (!l) return;
|
|
334
|
+
const mine = this.rowsOn(r.line);
|
|
335
|
+
const snap = mine.map((x) => ({ x, state: x.state, reason: x.reason, part: x.part, factNames: x.factNames }));
|
|
336
|
+
r.held = false;
|
|
337
|
+
/** @type {any[]} */ const parts = [];
|
|
338
|
+
/** @type {string[][]} */ const rows = [];
|
|
339
|
+
this.buildLine(l, parts, rows, new Set());
|
|
340
|
+
const ok = r.state === 'built';
|
|
341
|
+
const reason = r.reason;
|
|
342
|
+
const i = rows.findIndex((k) => k.includes(r.key));
|
|
343
|
+
const part = i >= 0 ? parts[i] : null;
|
|
344
|
+
const names = r.factNames ?? [];
|
|
345
|
+
const text = !ok || !part ? null : r.kind === 'count' ? countSentence(part) : (part.facts ?? []).filter((/** @type {any} */ f) => names.includes(f.name)).map((/** @type {any} */ f) => factSentence(f, part.source)).join('; ');
|
|
346
|
+
for (const a of snap) { a.x.state = a.state; a.x.reason = a.reason; a.x.part = a.part; a.x.factNames = a.factNames; }
|
|
347
|
+
r.held = true;
|
|
348
|
+
if (ok && text) r.offer = { text };
|
|
349
|
+
else { r.offer = null; r.held = false; r.state = 'not-checked'; r.reason = String(reason ?? 'not checked'); }
|
|
350
|
+
}
|
|
351
|
+
|
|
352
|
+
/** one line's parts: a pick part per count rule, ONE fact part carrying every fact row of the line (and its result count), then the `$` defaults */
|
|
353
|
+
buildLine(/** @type {{n: number, text: string}} */ l, /** @type {any[]} */ lineParts, /** @type {string[][]} */ lineRows, /** @type {Set<number>} */ factLines) {
|
|
354
|
+
/** @type {any[]} */ const facts = [];
|
|
355
|
+
/** @type {{r: Row, src: string}[]} */ const factRows = [];
|
|
356
|
+
/** @type {Row|null} */ let countRow = null;
|
|
357
|
+
/** @type {number[]} */ const rulesCarried = [];
|
|
358
|
+
/** the person's picks of this line: they follow the line's fact part on the card, as they always did (the facts, then the picks, then the defaults) @type {{p: any, key: string}[]} */
|
|
359
|
+
const pickParts = [];
|
|
360
|
+
for (const r of this.rowsOn(l.n)) {
|
|
361
|
+
if (r.kind === 'floor') continue;
|
|
362
|
+
r.part = null;
|
|
363
|
+
if (r.parent) continue; // a follow row copies its parent's end state after the line is built
|
|
364
|
+
if (!r.can?.ok) { r.state = 'not-checked'; r.reason = r.can?.why ?? 'not checked'; continue; }
|
|
365
|
+
if (r.held) { r.state = 'suggested'; r.reason = r.choice === 'drop' ? REASON.dropped : REASON.suggested; continue; }
|
|
366
|
+
if (r.can.how === 'pick') {
|
|
367
|
+
const p = partOfPick(r.pick, r.text, [r.rule, ...this.followsOf(r).map((/** @type {Row} */ f) => f.rule)]);
|
|
368
|
+
const reds = rubricStepShapeReds({ step: l.n, parts: [p] }, 'trial');
|
|
369
|
+
if (reds.length) { r.state = 'not-checked'; r.reason = `the pick is not a legal check: ${reds[0].detail}`; continue; }
|
|
370
|
+
pickParts.push({ p, key: r.key }); r.state = 'built'; r.reason = null;
|
|
371
|
+
continue;
|
|
372
|
+
}
|
|
373
|
+
if (r.can.how === 'count') { countRow = r; continue; }
|
|
374
|
+
const built = this.factsOf(r, facts);
|
|
375
|
+
if ('why' in built) { r.state = 'not-checked'; r.reason = `code could not build a legal check from it: ${built.why}`; continue; }
|
|
376
|
+
facts.push(...built.fresh);
|
|
377
|
+
r.factNames = built.names;
|
|
378
|
+
factRows.push({ r, src: built.src });
|
|
379
|
+
if (r.kind === 'rule') rulesCarried.push(r.rule, ...this.followsOf(r).map((/** @type {Row} */ f) => f.rule));
|
|
380
|
+
r.state = 'built'; r.reason = null;
|
|
381
|
+
}
|
|
382
|
+
if (facts.length) {
|
|
383
|
+
const part = {
|
|
384
|
+
text: facts.map((f) => f.name).join('; '), class: 'fact', source: factRows[0]?.src, facts,
|
|
385
|
+
...(rulesCarried.length ? { rules: rulesCarried } : {}),
|
|
386
|
+
...(countRow ? { count: { ...(Number.isInteger(countRow.min) ? { min: countRow.min } : {}), ...(Number.isInteger(countRow.max) ? { max: countRow.max } : {}) } } : {}),
|
|
387
|
+
};
|
|
388
|
+
const reds = rubricStepShapeReds({ step: l.n, parts: [part] }, 'trial');
|
|
389
|
+
if (reds.length) for (const x of [...factRows.map((f) => f.r), ...(countRow ? [countRow] : [])]) { x.state = 'not-checked'; x.reason = `code could not build the line's fact part: ${reds[0]?.detail}`; x.part = null; }
|
|
390
|
+
else { lineParts.push(part); lineRows.push([...factRows.map((f) => f.r.key), ...(countRow ? [countRow.key] : [])]); factLines.add(l.n); if (countRow) { countRow.state = 'built'; countRow.reason = null; } }
|
|
391
|
+
} else if (countRow) { countRow.state = 'not-checked'; countRow.reason = REASON.countNeedsFact; }
|
|
392
|
+
for (const { p, key } of pickParts) { lineParts.push(p); lineRows.push([key]); }
|
|
393
|
+
this.buildDefaults(l, lineParts, lineRows, factLines);
|
|
394
|
+
for (const f of this.rowsOn(l.n).filter((x) => x.parent)) {
|
|
395
|
+
const par = this.rows.find((x) => x.key === f.parent);
|
|
396
|
+
f.state = par?.state ?? 'not-checked'; f.reason = par?.state === 'built' ? null : (par?.reason ?? 'not checked'); f.part = par?.part ?? null;
|
|
397
|
+
}
|
|
398
|
+
}
|
|
399
|
+
|
|
400
|
+
/** the follow rows of a row (its agreed or carried `~` lines) */
|
|
401
|
+
followsOf(/** @type {Row} */ r) { return this.rows.filter((x) => x.parent === r.key); }
|
|
402
|
+
|
|
403
|
+
/**
|
|
404
|
+
* The fact(s) a fact-like row builds, checked alone through the bareguard shapes and against the facts already on the line.
|
|
405
|
+
* @param {Row} r @param {any[]} onLine the facts already built on this line @returns {{fresh: any[], src: string, names: string[]} | {why: string}}
|
|
406
|
+
*/
|
|
407
|
+
factsOf(r, onLine) {
|
|
408
|
+
const base = r.kind === 'rule' ? r.can.link : r;
|
|
409
|
+
const src = base.source ?? this.labels[0] ?? '';
|
|
410
|
+
const mk = (/** @type {string} */ name, /** @type {any} */ extra) => ({ name: cleanName(name), ...extra });
|
|
411
|
+
const kindFields = (/** @type {any} */ f, /** @type {string} */ anchor, /** @type {any} */ extra) => mk(f.name, {
|
|
412
|
+
shape: f.shape, anchor, ...(f.unit ? { unit: f.unit } : {}), ...(f.currency ? { currency: f.currency } : {}),
|
|
413
|
+
...(f.decimal && (f.shape === 'money' || f.shape === 'number') ? { decimal: f.decimal } : {}), ...(f.lang && f.shape === 'date' ? { lang: f.lang } : {}), ...extra,
|
|
414
|
+
});
|
|
415
|
+
/** @type {any[]} */ let fs;
|
|
416
|
+
if (r.kind === 'rule') {
|
|
417
|
+
const w = base.words.find((/** @type {string} */ x) => proved(this.textOf(src), x)) ?? base.words[0];
|
|
418
|
+
fs = r.can.strips ? placeFacts(base.name, r.can.strips, mk)
|
|
419
|
+
: base.shape === 'phrase' ? [mk(base.name, { shape: 'phrase', phrase: w })]
|
|
420
|
+
: [kindFields(base, w, { ...(r.can.reading ? { rule: r.can.reading } : {}), ...(r.can.aiProposal ? { aiProposal: true } : {}) })];
|
|
421
|
+
} else if (r.can.how === 'place') fs = placeFacts(r.name ?? 'phrase', r.can.strips, mk);
|
|
422
|
+
else if (r.shape === 'phrase' || r.kind === 'phrase') fs = [mk(r.name ?? `phrase ${r.words[0]}`, { shape: 'phrase', phrase: r.can.word })];
|
|
423
|
+
else fs = [kindFields(r, r.can.word, {})];
|
|
424
|
+
const fresh = [];
|
|
425
|
+
const names = fs.map((f) => f.name);
|
|
426
|
+
for (const f of fs) {
|
|
427
|
+
const reds = rubricStepShapeReds({ step: r.line, parts: [{ text: 'trial', class: 'fact', source: src, facts: [f] }] }, 'trial');
|
|
428
|
+
if (reds.length) return { why: String(reds[0]?.detail) };
|
|
429
|
+
// the draft gate's own word checks (src/factwords.js), run here on this fact alone so a word it would refuse (a label the look dropped or never kept, a unit or sign not on the page, words
|
|
430
|
+
// found fewer times than the line's result count) makes THIS row Not checked in the gate's plain words, never a red on code's whole plan
|
|
431
|
+
const cnt = this.rowsOn(r.line, 'count').find((x) => x.can?.ok);
|
|
432
|
+
const wordReds = checkFactWords({ part: { source: src, facts: [f], ...(cnt ? { count: { ...(Number.isInteger(cnt.min) ? { min: cnt.min } : {}), ...(Number.isInteger(cnt.max) ? { max: cnt.max } : {}) } } : {}) }, text: this.textOf(src), line: r.line, kept: keptSetOf(this.look, src) });
|
|
433
|
+
if (wordReds.length) return { why: String(wordReds[0].detail) };
|
|
434
|
+
const same = [...onLine, ...fresh].find((x) => x.name.toLowerCase() === f.name.toLowerCase());
|
|
435
|
+
if (same && JSON.stringify(same) === JSON.stringify(f)) continue; // the very same fact already carries another rule: one fact, both rules
|
|
436
|
+
if (same) return { why: `the name "${f.name}" is used twice on this line` };
|
|
437
|
+
fresh.push(f);
|
|
438
|
+
}
|
|
439
|
+
return { fresh, src, names };
|
|
440
|
+
}
|
|
441
|
+
|
|
442
|
+
/** the `$` defaults of a line, decided from what the line ended with: a text line quotes its input (one input only) and ties its numbers to the fact lines before it */
|
|
443
|
+
buildDefaults(/** @type {{n: number}} */ l, /** @type {any[]} */ lineParts, /** @type {string[][]} */ lineRows, /** @type {Set<number>} */ factLines) {
|
|
444
|
+
const cited = this.rowsOn(l.n, 'cited')[0] ?? this.addRow({ key: `L${l.n}.$cited`, line: l.n, kind: 'cited', by: 'default', from: 'default', source: this.labels[0] });
|
|
445
|
+
const numbers = this.rowsOn(l.n, 'numbers')[0] ?? null;
|
|
446
|
+
cited.part = null;
|
|
447
|
+
if (factLines.has(l.n)) { cited.state = 'not-checked'; cited.reason = REASON.onePerLine; return; }
|
|
448
|
+
const only = this.labels.length === 1 ? this.labels[0] : null;
|
|
449
|
+
if (only) { lineParts.push({ text: defaultCitedText(only), class: 'countable', check: 'cited', source: only }); lineRows.push([cited.key]); cited.state = 'built'; cited.reason = null; }
|
|
450
|
+
else { cited.state = 'not-checked'; cited.reason = REASON.manyInputs; }
|
|
451
|
+
const earlier = [...factLines].filter((n) => n < l.n);
|
|
452
|
+
if (!earlier.length) return;
|
|
453
|
+
const nr = numbers ?? this.addRow({ key: `L${l.n}.$numbers`, line: l.n, kind: 'numbers', by: 'default', from: 'default', after: earlier });
|
|
454
|
+
lineParts.push({ text: 'every number in the text is in a checked quote', class: 'countable', check: 'numbersFromFacts', from: earlier });
|
|
455
|
+
lineRows.push([nr.key]); nr.state = 'built'; nr.reason = null; nr.part = null;
|
|
456
|
+
}
|
|
457
|
+
|
|
458
|
+
// 5. the code-forced questions for rows code cannot settle (a person answers; the CHOICE decides, never the words). Cap 3 is the caller's (MAX_QUESTIONS).
|
|
459
|
+
ask() {
|
|
460
|
+
this.open = [];
|
|
461
|
+
for (const r of this.rows) {
|
|
462
|
+
if (!r.can?.ask) continue;
|
|
463
|
+
this.open.push({ key: `${r.key}:${r.can.ask}`, rowKey: r.key, line: r.line, ruleText: r.text, ask: r.can.ask, proposal: r.can.proposal ?? null, offers: { agree: r.can.proposal != null, drop: true } });
|
|
464
|
+
}
|
|
465
|
+
return this.open;
|
|
466
|
+
}
|
|
467
|
+
hasOpenQuestions() { return this.open.length > 0; }
|
|
468
|
+
hasPendingDelta() { return this.queue.length > 0; }
|
|
469
|
+
|
|
470
|
+
// 6. the person's answer: a row edit from the typed CHOICE (agree | drop | rule)
|
|
471
|
+
/** @param {string} qKey @param {string} choice @param {string} [text] @returns {{ok: true} | {ok: false, why: string}} */
|
|
472
|
+
applyAnswer(qKey, choice, text) {
|
|
473
|
+
const q = this.open.find((x) => x.key === qKey);
|
|
474
|
+
const r = q ? this.rows.find((x) => x.key === q.rowKey) : undefined;
|
|
475
|
+
if (!q || !r) return { ok: false, why: 'out of date' };
|
|
476
|
+
if (choice === 'agree') {
|
|
477
|
+
if (!q.offers.agree) return { ok: false, why: 'that choice is not offered for this question' };
|
|
478
|
+
let said = String(q.proposal);
|
|
479
|
+
if (q.ask === 'check') {
|
|
480
|
+
const res = keepOrDrop(r.text, /** @type {any} */ ({ ...q.proposal }));
|
|
481
|
+
if (!res.kept) return { ok: false, why: String(res.why) };
|
|
482
|
+
r.pick = res.check; r.from = 'pick'; r.agreedText = plainWords(/** @type {any} */ (res.check));
|
|
483
|
+
said = r.agreedText;
|
|
484
|
+
} else { r.agreed = true; r.agreedText = said; }
|
|
485
|
+
// hamr 2026-11-12 (ruling 1): an agreed answer is a `~` rule in plain words, a new line with the SAME rule number a typed rule gets; an edited added line (pairWith) IS that line, so its words are replaced
|
|
486
|
+
if (r.pairWith !== undefined) r.text = said;
|
|
487
|
+
else {
|
|
488
|
+
// F318: agreeing to words the line already holds as a `~` rule adds no second line (the pick cites the rule that is there); a reading always adds its line
|
|
489
|
+
const held = q.ask === 'check' ? this.ruleRows(r.line).filter((x) => sameRuleWords(x.text, said)).at(-1) : undefined;
|
|
490
|
+
if (held === undefined) this.addRow({ key: `L${r.line}.~${this.ruleRows(r.line).length + 1}`, line: r.line, kind: 'rule', by: 'you', from: 'rule', origin: 'agreed', parent: r.key, text: said, rule: this.ruleRows(r.line).length + 1 });
|
|
491
|
+
else if (held !== r) this.follow(r, held);
|
|
492
|
+
}
|
|
493
|
+
return { ok: true };
|
|
494
|
+
}
|
|
495
|
+
if (choice === 'drop') { r.left = true; return { ok: true }; }
|
|
496
|
+
if (choice === 'rule') {
|
|
497
|
+
const said = String(text ?? '').replace(/\s+/g, ' ').trim();
|
|
498
|
+
if (!said) return { ok: false, why: 'an answer is needed' };
|
|
499
|
+
r.cantAsked = true;
|
|
500
|
+
const n = this.ruleRows(r.line).length + 1;
|
|
501
|
+
this.addRow({ key: `L${r.line}.~${n}`, line: r.line, kind: 'rule', by: 'you', from: 'rule', origin: 'typed', text: said, rule: n });
|
|
502
|
+
return { ok: true };
|
|
503
|
+
}
|
|
504
|
+
return { ok: false, why: 'not one of the choices' };
|
|
505
|
+
}
|
|
506
|
+
|
|
507
|
+
/** the `~` row `partner` is checked by `source` (one check, both rule numbers) */
|
|
508
|
+
follow(/** @type {Row} */ source, /** @type {Row} */ partner) { partner.parent = source.key; partner.origin = 'agreed'; }
|
|
509
|
+
|
|
510
|
+
/**
|
|
511
|
+
* Revise keeps the picks of the last draft (am17/am18/am21, `src/pickrecord.js` `matchRecord`): a kept pick settles its rule and its added `~` line follows it; a kept left rule is
|
|
512
|
+
* left; an edited added line (`pairRule`) is the row asked and its source follows it.
|
|
513
|
+
* @param {{picks?: {line: number, rule: number, check: any, added?: string, own?: boolean}[], left?: {line: number, rule: number}[]}} kept
|
|
514
|
+
* @param {{line: number, rule: number, pairRule?: number}[]} [asked] the rules still asked (a `pairRule` marks an edited added line)
|
|
515
|
+
*/
|
|
516
|
+
carry({ picks = [], left = [] }, asked = []) {
|
|
517
|
+
const rowOf = (/** @type {number} */ line, /** @type {number} */ rule) => this.ruleRows(line).find((x) => x.rule === rule);
|
|
518
|
+
for (const p of picks) {
|
|
519
|
+
const r = rowOf(p.line, p.rule);
|
|
520
|
+
if (!r) continue;
|
|
521
|
+
if (p.check) { r.pick = p.check; r.from = 'pick'; r.keptFromLast = true; r.agreedText = plainWords(p.check); } else { r.cantAsked = true; r.keptFromLast = true; }
|
|
522
|
+
if (typeof p.added === 'string') { const add = this.ruleRows(p.line).filter((x) => x !== r && !x.parent && sameRuleWords(x.text, String(p.added))).at(-1); if (add) this.follow(r, add); }
|
|
523
|
+
}
|
|
524
|
+
for (const l of left) { const r = rowOf(l.line, l.rule); if (r) { r.left = true; r.keptFromLast = true; } }
|
|
525
|
+
for (const a of asked) if (a.pairRule !== undefined) { const r = rowOf(a.line, a.rule); if (r) { r.pairWith = a.pairRule; const src = rowOf(a.line, a.pairRule); if (src && src !== r) this.follow(r, src); } }
|
|
526
|
+
}
|
|
527
|
+
|
|
528
|
+
// 7. the AI's reply: a list of proposed ROW edits, each judged ALONE; a bad edit is refused on its own and costs the rest nothing
|
|
529
|
+
/** @param {{label?: string, pre?: boolean, edits?: any[]}} delta `pre` = a reply that is not a drafter try (the suggest call): it does not count against the tries */
|
|
530
|
+
applyDelta(delta) {
|
|
531
|
+
const pre = delta.pre === true;
|
|
532
|
+
const rec = { label: delta.label, pre, edits: /** @type {any[]} */ ([]) };
|
|
533
|
+
if (!pre) this.tries += 1;
|
|
534
|
+
const over = !pre && this.tries > this.maxTries;
|
|
535
|
+
const kept = { adds: 0 }; // the cap counts the checks KEPT from this ONE reply, in order (MAX_BANG): every extra is refused alone
|
|
536
|
+
for (const e of delta.edits ?? []) {
|
|
537
|
+
const out = { ...e, accepted: false };
|
|
538
|
+
try { Object.assign(out, over ? { refusedBecause: REASON.afterLast } : this.applyEdit(e, kept)); }
|
|
539
|
+
catch (x) { out.refusedBecause = `the edit could not be read: ${String(/** @type {any} */ (x)?.message ?? x).slice(0, 80)}`; }
|
|
540
|
+
rec.edits.push(out);
|
|
541
|
+
}
|
|
542
|
+
this.deltaLog.push(rec);
|
|
543
|
+
return rec;
|
|
544
|
+
}
|
|
545
|
+
|
|
546
|
+
applyEdit(/** @type {any} */ e, /** @type {{adds: number}} */ kept) {
|
|
547
|
+
if (!isRec(e) || !EDIT_OPS.includes(e.op)) return { refusedBecause: `"${e?.op}" is not a row edit (the AI never sends a check or a part)` };
|
|
548
|
+
if (!Number.isInteger(e.line) || !this.lines.some((l) => l.n === e.line)) return { refusedBecause: 'names a job line that does not exist' };
|
|
549
|
+
if (e.op === 'add') {
|
|
550
|
+
if (kept.adds >= MAX_BANG) return { refusedBecause: `more than ${MAX_BANG} checks proposed` };
|
|
551
|
+
const res = this.editAdd(e);
|
|
552
|
+
if (res.accepted) kept.adds += 1;
|
|
553
|
+
return res;
|
|
554
|
+
}
|
|
555
|
+
if (e.op === 'reading') return this.editReading(e);
|
|
556
|
+
if (e.op === 'pick') return this.editPick(e);
|
|
557
|
+
return this.editNot(e);
|
|
558
|
+
}
|
|
559
|
+
|
|
560
|
+
editAdd(/** @type {any} */ e) {
|
|
561
|
+
if (e.kind === 'count') return this.addCount(e);
|
|
562
|
+
if (e.kind !== 'fact' && e.kind !== 'phrase') return { refusedBecause: 'an add is a fact, a phrase or a count' };
|
|
563
|
+
// an input the job does not have is refused alone (it would otherwise reach the draft gate as a red on code's own plan and stop the whole draft)
|
|
564
|
+
if (e.source !== undefined && e.source !== null && !this.labels.includes(e.source)) return { refusedBecause: `"${String(e.source).slice(0, 60)}" is not one of the inputs` };
|
|
565
|
+
// a job with more than one input never guesses which one an add quotes (code fills a missing source only when there is exactly one)
|
|
566
|
+
if ((e.source === undefined || e.source === null) && this.labels.length > 1) return { refusedBecause: `names no input; the job has ${this.labels.length}: ${this.labels.map((/** @type {string} */ l) => `"${l}"`).join(', ')}` };
|
|
567
|
+
const words = (Array.isArray(e.words) ? e.words : []).filter((/** @type {unknown} */ w) => typeof w === 'string' && w.trim());
|
|
568
|
+
if (!words.length) return { refusedBecause: 'no words given' };
|
|
569
|
+
if (!words.some((/** @type {string} */ w) => proved(this.textOf(e.source), w))) return { refusedBecause: `the words ${words.map((/** @type {string} */ w) => `"${w}"`).join(', ')} are not in the input` };
|
|
570
|
+
const name = cleanName(e.name ?? words[0]);
|
|
571
|
+
if (!name) return { refusedBecause: 'no usable name' };
|
|
572
|
+
const dup = this.onTheList(e.line, name, words);
|
|
573
|
+
if (dup) return { refusedBecause: dup };
|
|
574
|
+
const row = this.addRow({ key: `L${e.line}.${e.kind === 'phrase' ? 'phrase' : 'fact'}.${norm(e.kind === 'phrase' ? words[0] : name)}`, line: e.line, kind: e.kind, by: 'agent', from: 'agent', name, shape: e.shape ?? 'phrase', words, unit: e.unit, currency: e.currency, decimal: e.decimal, lang: e.lang, source: e.source });
|
|
575
|
+
return { accepted: true, landedKey: row.key };
|
|
576
|
+
}
|
|
577
|
+
|
|
578
|
+
/** @returns {string|null} the plain reason an add repeats what the list already holds (a look fact tied to a `~` rule, a `~` rule's own words, another `!` row), else null */
|
|
579
|
+
onTheList(/** @type {number} */ line, /** @type {string} */ name, /** @type {string[]} */ words) {
|
|
580
|
+
for (const rr of this.ruleRows(line)) {
|
|
581
|
+
for (const l of rr.links ?? []) {
|
|
582
|
+
const lw = (l.words ?? []).map(norm);
|
|
583
|
+
if (norm(l.name) === norm(name) || words.some((w) => lw.includes(norm(w)))) return `already on the list: the look's fact "${l.name}" is tied to your rule ${rr.rule} on line ${line}`;
|
|
584
|
+
}
|
|
585
|
+
}
|
|
586
|
+
const wset = wordSet(words[0]);
|
|
587
|
+
for (const rr of this.ruleRows(line)) {
|
|
588
|
+
const rs = wordSet(rr.text);
|
|
589
|
+
if (wset.size && [...wset].every((w) => rs.has(w))) return `already on the list: a ~ rule says it (line ${line} rule ${rr.rule})`;
|
|
590
|
+
}
|
|
591
|
+
if (this.rows.some((r) => (r.kind === 'fact' || r.kind === 'phrase') && r.line === line && (norm(r.name) === norm(name) || norm(r.words?.[0]) === norm(words[0])))) return 'already on the list';
|
|
592
|
+
return null;
|
|
593
|
+
}
|
|
594
|
+
|
|
595
|
+
addCount(/** @type {any} */ e) {
|
|
596
|
+
const ok = (/** @type {unknown} */ x) => Number.isInteger(x) && /** @type {number} */ (x) >= 1;
|
|
597
|
+
if (![e.min, e.max].some(Number.isInteger) || [e.min, e.max].some((x) => x !== undefined && !ok(x)) || (Number.isInteger(e.min) && Number.isInteger(e.max) && e.min > e.max)) return { refusedBecause: 'no legal number given' };
|
|
598
|
+
if (this.rowsOn(e.line, 'count').length) return { refusedBecause: 'already on the list: this line has a result count' };
|
|
599
|
+
const row = this.addRow({ key: `L${e.line}.count.${e.min ?? ''}-${e.max ?? ''}`, line: e.line, kind: 'count', by: 'agent', from: 'agent', min: e.min, max: e.max });
|
|
600
|
+
return { accepted: true, landedKey: row.key };
|
|
601
|
+
}
|
|
602
|
+
|
|
603
|
+
/** the `~` rule rows of a line an edit names by number */
|
|
604
|
+
targetsOf(/** @type {any} */ e) {
|
|
605
|
+
return (Array.isArray(e.rules) ? e.rules : []).map((/** @type {number} */ n) => this.ruleRows(e.line).find((r) => r.rule === n)).filter((/** @type {Row|undefined} */ r) => r !== undefined);
|
|
606
|
+
}
|
|
607
|
+
|
|
608
|
+
/** the one owner of "what do the person's own words say": code's reading of a `~` rule for a money/time fact, or undefined when it cannot read it */
|
|
609
|
+
wordsReading(/** @type {any} */ l, /** @type {string} */ text) {
|
|
610
|
+
if (l.shape !== 'money' && l.shape !== 'time') return undefined;
|
|
611
|
+
const own = readLimit(l.shape, text, l);
|
|
612
|
+
const rd = 'reading' in own ? own.reading : undefined;
|
|
613
|
+
return rd && validReading(l.shape, rd) && (rd.op === 'between' ? [rd.lo, rd.hi] : [rd.value]).every((b) => boundInWords(b, text)) ? rd : undefined;
|
|
614
|
+
}
|
|
615
|
+
|
|
616
|
+
editReading(/** @type {any} */ e) {
|
|
617
|
+
const targets = this.targetsOf(e);
|
|
618
|
+
if (!targets.length) return { refusedBecause: 'names no ~ rule of this line' };
|
|
619
|
+
const fact = this.lookFacts.find((f) => f.line === e.line && norm(f.name) === norm(e.fact));
|
|
620
|
+
if (!fact) return { refusedBecause: `no look fact named "${String(e.fact ?? '').slice(0, 60)}" on line ${e.line}` };
|
|
621
|
+
if (!validReading(fact.shape, e.reading)) return { refusedBecause: 'not a legal reading for that fact shape' };
|
|
622
|
+
const many = targets.find((/** @type {Row} */ r) => holdsManyBounds(fact.shape, r.text));
|
|
623
|
+
if (many) return { refusedBecause: `your rule ${many.rule} holds more than one amount or range, so one reading would be a wrong check` };
|
|
624
|
+
const read = targets.map((/** @type {Row} */ r) => ({ r, rd: this.wordsReading(fact, r.text) })).find((x) => x.rd && JSON.stringify(x.rd) !== JSON.stringify(e.reading));
|
|
625
|
+
if (read) return { refusedBecause: `code reads your words in rule ${read.r.rule} itself, so that reading is not used` };
|
|
626
|
+
const open = targets.filter((/** @type {Row} */ r) => !r.links?.some((/** @type {any} */ l) => l.reading) && !r.aiReading);
|
|
627
|
+
if (!open.length) return { refusedBecause: 'already on the list: the look or an earlier reading already reads that rule' };
|
|
628
|
+
for (const r of open) {
|
|
629
|
+
r.aiReading = { fact: fact.name, reading: e.reading };
|
|
630
|
+
if (!r.links?.length) (r.links ??= []).push({ name: fact.name, shape: fact.shape, words: [...(fact.words ?? [])], unit: fact.unit, currency: fact.currency, decimal: fact.decimal, lang: fact.lang, source: fact.source });
|
|
631
|
+
}
|
|
632
|
+
return { accepted: true, landedKey: open[0].key };
|
|
633
|
+
}
|
|
634
|
+
|
|
635
|
+
editPick(/** @type {any} */ e) {
|
|
636
|
+
const r = this.targetsOf(e)[0];
|
|
637
|
+
if (!r) return { refusedBecause: 'names no ~ rule of this line' };
|
|
638
|
+
if (!isCountable(r.text)) return { refusedBecause: 'not a count rule' };
|
|
639
|
+
const res = keepOrDrop(r.text, /** @type {any} */ ({ ...e.check }));
|
|
640
|
+
if (!res.kept) return { refusedBecause: String(res.why) };
|
|
641
|
+
r.pickProposal = res.check;
|
|
642
|
+
return { accepted: true, landedKey: r.key };
|
|
643
|
+
}
|
|
644
|
+
|
|
645
|
+
editNot(/** @type {any} */ e) {
|
|
646
|
+
const r = this.ruleRows(e.line).find((x) => x.rule === e.rule);
|
|
647
|
+
if (!r) return { refusedBecause: 'names no ~ rule of this line' };
|
|
648
|
+
if (r.can?.ok) return { refusedBecause: 'code can check it: code decides what is doable' };
|
|
649
|
+
const reason = String(e.reason ?? '').replace(/\s+/g, ' ').trim().slice(0, 200);
|
|
650
|
+
if (!reason) return { refusedBecause: 'no reason given' };
|
|
651
|
+
r.aiReason = reason;
|
|
652
|
+
return { accepted: true, landedKey: r.key };
|
|
653
|
+
}
|
|
654
|
+
|
|
655
|
+
// 8. whole-list reconcile, every time
|
|
656
|
+
reconcile() { this.match(); const c = this.build(); this.ask(); return c; }
|
|
657
|
+
|
|
658
|
+
/** the loop: while a question is open or a reply waits, apply it and reconcile the WHOLE list again. `answer(q)` gives {choice, text} or null (a person who did not answer) */
|
|
659
|
+
drive({ answer }) {
|
|
660
|
+
this.unanswered = [];
|
|
661
|
+
this.reconcile();
|
|
662
|
+
for (let guard = 0; (this.hasOpenQuestions() || this.hasPendingDelta()) && guard < 200; guard += 1) {
|
|
663
|
+
const q = this.open.find((x) => !this.unanswered.includes(x.key));
|
|
664
|
+
if (q) {
|
|
665
|
+
const a = answer(q);
|
|
666
|
+
const res = a ? this.applyAnswer(q.key, a.choice, a.text) : { ok: false };
|
|
667
|
+
if (!res.ok) { this.unanswered.push(q.key); continue; }
|
|
668
|
+
this.reconcile();
|
|
669
|
+
continue;
|
|
670
|
+
}
|
|
671
|
+
const d = this.queue.shift();
|
|
672
|
+
if (!d) break;
|
|
673
|
+
this.applyDelta(d);
|
|
674
|
+
this.reconcile();
|
|
675
|
+
}
|
|
676
|
+
return this;
|
|
677
|
+
}
|
|
678
|
+
|
|
679
|
+
card() { return renderCard(this.rows); }
|
|
680
|
+
|
|
681
|
+
/**
|
|
682
|
+
* The rows as the close shape the card readers (`checksOfClose`, `refusedOf`, `cardOfClose`) already read: the block's stages, the picks code records and the signed list. The confirm card
|
|
683
|
+
* and the signed card read the rows through the SAME readers, so a row shown at confirm is the row signed. Reconciles first (the whole list, every time).
|
|
684
|
+
*/
|
|
685
|
+
closeView() {
|
|
686
|
+
const c = this.reconcile();
|
|
687
|
+
return { stages: c.stages, picks: this.picksOf(c), list: this.signedRows() };
|
|
688
|
+
}
|
|
689
|
+
|
|
690
|
+
/** the picks code records: a countable part carrying the `~` rule numbers it settles @param {{parts: any[], map: {part: number, line: number}[]}} c the build @returns {{line: number, part: any}[]} */
|
|
691
|
+
picksOf(c) {
|
|
692
|
+
return c.map.flatMap((m) => { const part = c.parts[m.part]; return part && part.class === 'countable' && Array.isArray(part.rules) ? [{ line: m.line, part }] : []; });
|
|
693
|
+
}
|
|
694
|
+
|
|
695
|
+
/** the standing ruling "a run needs one check": one row that is not the floor is built */
|
|
696
|
+
hasBuiltCheck() { return this.rows.some((r) => r.state === 'built' && r.kind !== 'floor'); }
|
|
697
|
+
|
|
698
|
+
/**
|
|
699
|
+
* The signed list (`closeDecl.list`): the signed fields of every row, never its words. A suggestion not added is signed `dropped` ("suggested, not added"), a suggestion code could not build
|
|
700
|
+
* `dropped` with code's reason (it is on the Audit, never on the card), and a built row carries the names of the facts it made (`facts`), so every reader can tag each fact by its row's kind.
|
|
701
|
+
*/
|
|
702
|
+
signedRows() {
|
|
703
|
+
return this.rows.map((r) => {
|
|
704
|
+
const gone = r.state === 'suggested' || r.hidden === true;
|
|
705
|
+
const row = { ...r, ...(gone ? { state: 'dropped' } : {}), ...(r.state === 'built' && r.factNames?.length ? { facts: r.factNames } : {}) };
|
|
706
|
+
return Object.fromEntries(SIGNED_ROW_FIELDS.filter((f) => row[f] !== undefined && row[f] !== null).map((f) => [f, row[f]]));
|
|
707
|
+
});
|
|
708
|
+
}
|
|
709
|
+
|
|
710
|
+
/** the suggestions code has proved it can build, as the card offers them (Add): `{key, line, text}` */
|
|
711
|
+
suggestedView() {
|
|
712
|
+
return this.rows.filter((r) => r.state === 'suggested' && r.offer?.text).map((r) => ({ key: r.key, line: r.line, text: String(r.offer.text), ...(r.choice === 'drop' ? { dropped: true } : {}) }));
|
|
713
|
+
}
|
|
714
|
+
|
|
715
|
+
/** the suggestions code could not build, in one plain sentence each: the Audit's draft lines (never the card) */
|
|
716
|
+
unbuiltSuggestions() {
|
|
717
|
+
return this.rows.filter((r) => r.hidden === true).map((r) => ({
|
|
718
|
+
line: r.line, text: `A suggested check was not built (${r.kind} ${(r.words ?? []).length ? (r.words ?? []).map((/** @type {string} */ w) => `"${w}"`).join(', ') : `"${r.name ?? ''}"`}): ${String(r.reason ?? 'not checked').replace(/^code could not build a legal check from it: /, '').replace(/\s*Nothing was signed\.\s*$/, '')}`,
|
|
719
|
+
}));
|
|
720
|
+
}
|
|
721
|
+
|
|
722
|
+
/**
|
|
723
|
+
* The person's Drop or Add on a `!` row (hamr 2026-11-13: instant, $0, no AI; the choice decides). The whole list is reconciled again, so an Add is judged by code like every other row.
|
|
724
|
+
* @param {string} key @param {'drop'|'add'} choice @returns {{ok: true} | {ok: false, why: string}}
|
|
725
|
+
*/
|
|
726
|
+
rowChoice(key, choice) {
|
|
727
|
+
const r = this.rows.find((x) => x.key === key);
|
|
728
|
+
if (!r) return { ok: false, why: 'there is no such check on the list' };
|
|
729
|
+
if (r.by === 'you') return { ok: false, why: 'that check comes from a rule you wrote; edit the job to change it' };
|
|
730
|
+
if (r.by !== 'agent' || !this.isBang(r)) return { ok: false, why: 'that check is code\'s own and stays' };
|
|
731
|
+
if (choice !== 'drop' && choice !== 'add') return { ok: false, why: 'not one of the choices' };
|
|
732
|
+
if (choice === 'drop' && r.state !== 'built') return { ok: false, why: 'only a check that is on the card can be dropped' };
|
|
733
|
+
if (choice === 'add' && r.state !== 'suggested') return { ok: false, why: 'only a suggested check can be added' };
|
|
734
|
+
const before = r.choice;
|
|
735
|
+
r.choice = choice;
|
|
736
|
+
this.reconcile();
|
|
737
|
+
if (choice === 'add' && r.state !== 'built') { const why = String(r.reason ?? 'not checked'); r.choice = before; this.reconcile(); return { ok: false, why }; }
|
|
738
|
+
return { ok: true };
|
|
739
|
+
}
|
|
740
|
+
|
|
741
|
+
/** the person's Drop/Add choices, tied to the line's words (not its number) and the row's own name, so a Revise keeps them @returns {{lineWords: string, occurrence: number, tail: string, choice: string}[]} */
|
|
742
|
+
choicesRecord() {
|
|
743
|
+
const seen = new Map();
|
|
744
|
+
const key = new Map(this.lines.map((l) => { const w = norm(l.text); const o = seen.get(w) ?? 0; seen.set(w, o + 1); return [l.n, { lineWords: w, occurrence: o }]; }));
|
|
745
|
+
return this.rows.filter((r) => r.choice === 'drop' || r.choice === 'add').map((r) => ({ ...(key.get(r.line) ?? { lineWords: '', occurrence: 0 }), tail: r.key.replace(/^L\d+\./, ''), choice: String(r.choice) }));
|
|
746
|
+
}
|
|
747
|
+
|
|
748
|
+
/** a Revise keeps the last draft's Drop/Add choices on the rows that are still there (same line words, same row); code still judges every one @param {{lineWords: string, occurrence: number, tail: string, choice: string}[]} rec */
|
|
749
|
+
carryChoices(rec) {
|
|
750
|
+
const seen = new Map();
|
|
751
|
+
const lines = this.lines.map((l) => { const w = norm(l.text); const o = seen.get(w) ?? 0; seen.set(w, o + 1); return { n: l.n, w, o }; });
|
|
752
|
+
for (const c of Array.isArray(rec) ? rec : []) {
|
|
753
|
+
if (!c || (c.choice !== 'drop' && c.choice !== 'add')) continue;
|
|
754
|
+
const line = lines.find((l) => l.w === c.lineWords && l.o === c.occurrence);
|
|
755
|
+
const r = line ? this.rows.find((x) => x.line === line.n && this.isBang(x) && x.key.replace(/^L\d+\./, '') === c.tail) : undefined;
|
|
756
|
+
if (r) { r.choice = c.choice; r.keptFromLast = true; }
|
|
757
|
+
}
|
|
758
|
+
}
|
|
759
|
+
|
|
760
|
+
/**
|
|
761
|
+
* per line, the shape of `retentionOfList` (counts only): `aiParts` = the rows the agent proposed (and the default cited), `kept` = those built, `dropped` = the rest, `picks` = a `~` row built from a pick
|
|
762
|
+
* @returns {{line: number, aiParts: number, kept: number, dropped: number, picks: boolean}[]}
|
|
763
|
+
*/
|
|
764
|
+
retention() {
|
|
765
|
+
return this.lines.map((l) => {
|
|
766
|
+
const mine = this.rowsOn(l.n).filter((r) => r.kind !== 'floor');
|
|
767
|
+
const ai = mine.filter((r) => r.by === 'agent' || (r.by === 'default' && r.kind === 'cited'));
|
|
768
|
+
const kept = ai.filter((r) => r.state === 'built').length;
|
|
769
|
+
return { line: l.n, aiParts: ai.length, kept, dropped: ai.length - kept, picks: mine.some((r) => r.from === 'pick' && r.state === 'built') };
|
|
770
|
+
});
|
|
771
|
+
}
|
|
772
|
+
}
|