fwdloop 0.21.0 → 0.22.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/bin/fwdloop +85 -34
- package/package.json +4 -3
- package/src/audit-fold.js +5 -1
- package/src/authoring.js +32 -6
- package/src/checked.js +18 -1
- package/src/cited-record.js +138 -0
- package/src/cited-source.js +49 -0
- package/src/citedcheck.js +306 -0
- package/src/citedwords.js +197 -0
- package/src/closers.js +56 -7
- package/src/ctx-tools.js +2 -14
- package/src/declaration.js +85 -5
- package/src/diary.js +2 -2
- package/src/docx.js +16 -2
- package/src/draft-steps.js +1 -1
- package/src/drafter.js +292 -25
- package/src/flow.js +33 -7
- package/src/flowmodel.js +21 -0
- package/src/input-facts.js +20 -3
- package/src/model-step.js +30 -5
- package/src/pageask.js +136 -0
- package/src/pagebits.js +107 -0
- package/src/pagedropwords.js +171 -0
- package/src/pagedry.js +120 -0
- package/src/pagefetch.js +164 -0
- package/src/pagelook.js +155 -0
- package/src/pagemap.js +55 -0
- package/src/pageplace.js +103 -0
- package/src/pagerules.js +318 -0
- package/src/panel/author.js +41 -20
- package/src/panel/authorcard.js +11 -1
- package/src/panel/authorflows.js +21 -15
- package/src/panel/authorstart.js +2 -4
- package/src/panel/authortrigger.js +14 -4
- package/src/panel/data.js +83 -2
- package/src/panel/index.html +279 -12
- package/src/panel/result.js +295 -0
- package/src/panel/server.js +27 -3
- package/src/primitives.js +2 -3
- package/src/provider.js +5 -2
- package/src/redact.js +17 -0
- package/src/runner.js +83 -12
- package/src/schedule.js +3 -3
- package/src/setup.js +16 -1
- package/src/sheetfind.js +50 -0
- package/src/sheetlook.js +157 -0
- package/src/signed-text.js +11 -1
- package/src/thinking.js +1 -1
- package/src/try-checks.js +6 -0
- package/src/types.js +3 -2
- package/types/authoring.d.ts +19 -2
- package/types/checked.d.ts +10 -0
- package/types/cited-record.d.ts +21 -0
- package/types/cited-source.d.ts +18 -0
- package/types/citedcheck.d.ts +95 -0
- package/types/citedwords.d.ts +97 -0
- package/types/closers.d.ts +55 -3
- package/types/ctx-tools.d.ts +0 -1
- package/types/declaration.d.ts +15 -1
- package/types/docx.d.ts +14 -0
- package/types/drafter.d.ts +244 -2
- package/types/flow.d.ts +21 -0
- package/types/flowmodel.d.ts +15 -0
- package/types/input-facts.d.ts +12 -2
- package/types/model-step.d.ts +35 -3
- package/types/pageask.d.ts +75 -0
- package/types/pagebits.d.ts +32 -0
- package/types/pagedropwords.d.ts +40 -0
- package/types/pagedry.d.ts +38 -0
- package/types/pagefetch.d.ts +86 -0
- package/types/pagelook.d.ts +75 -0
- package/types/pagemap.d.ts +31 -0
- package/types/pageplace.d.ts +22 -0
- package/types/pagerules.d.ts +86 -0
- package/types/panel/authorcard.d.ts +2 -0
- package/types/panel/authorflows.d.ts +2 -0
- package/types/panel/data.d.ts +26 -0
- package/types/panel/result.d.ts +156 -0
- package/types/provider.d.ts +3 -1
- package/types/redact.d.ts +1 -0
- package/types/runner.d.ts +156 -7
- package/types/schedule.d.ts +0 -11
- package/types/setup.d.ts +13 -0
- package/types/sheetfind.d.ts +46 -0
- package/types/sheetlook.d.ts +120 -0
- package/types/thinking.d.ts +1 -1
- package/types/types.d.ts +11 -3
package/src/pagerules.js
ADDED
|
@@ -0,0 +1,318 @@
|
|
|
1
|
+
// borrowed-from: bareloop src/pagerules.js@1a0712b
|
|
2
|
+
// Copy of bareloop's file; the three number patterns (BEFORE, AFTER, AFTER_CUR) are written as literals, not built from NUM (this repo bans dynamic patterns in src/); only the import lines point at fwdloop's src/pagebits.js (the pieces of facts.js, factwords.js, labelscan.js, rubricstep.js the rules need).
|
|
3
|
+
// IDEA A CORE (docs/product/ITEM33-BUILD.md "Addendum 2026-10-18 — Idea A core: build spec", section 4.2): the plain code rules that
|
|
4
|
+
// keep or drop each page word the AI's map named. The AI never grades: it names words, code keeps only what the saved page proves.
|
|
5
|
+
// Pure - no I/O, no clock, no model. The rule bodies are PORTED from the frozen POC (scratch idea-a-v2/rules.mjs and
|
|
6
|
+
// rules-v3-candidate.mjs; sha256 recorded in commit 0bcc5fe), with the OUT branches (R2, R2a, R2b, R2c, R3, R3U, V4, pageClaims) deleted.
|
|
7
|
+
|
|
8
|
+
import { lineRules } from './pagebits.js';
|
|
9
|
+
import { MAX_CURRENCIES, MAX_UNITS, MAX_UNIT_CHARS, FACT_OPS, boundOk } from './pagebits.js';
|
|
10
|
+
import { countExact, countPhrase, squash, normLabel } from './pagebits.js';
|
|
11
|
+
|
|
12
|
+
/**
|
|
13
|
+
* @typedef {{op: string, value?: string|number, lo?: string|number, hi?: string|number}} Reading what a `~` rule means as a limit (PRD v1.107 Part 5b): the same shape as a signed fact rule
|
|
14
|
+
* @typedef {{name: string, from?: string, shape: string, words?: string[], currency?: string[], unit?: string[], reading?: Reading}} MapFact the AI's map, one fact
|
|
15
|
+
* @typedef {{facts: MapFact[]}} PageMap
|
|
16
|
+
* @typedef {{line: number, name: string, shape: string, from: string, words: string[], unit?: string[], currency?: string[], forRule?: number, reading?: Reading}} KeptFact
|
|
17
|
+
* `words` = what the page proves: the labels of a money/number/time fact, or the alternative phrases of a phrase fact (a place is a phrase)
|
|
18
|
+
* @typedef {{line: number, rule?: number, fact: string, word?: string, reason: string, other?: string}} Dropped `other` = what the drop is compared to: the first label (R1), the primary form (R1L), the fact it would have repeated (S1)
|
|
19
|
+
*/
|
|
20
|
+
|
|
21
|
+
/** @param {unknown} v @returns {v is Record<string, any>} */
|
|
22
|
+
const isRec = (v) => v !== null && typeof v === 'object' && !Array.isArray(v);
|
|
23
|
+
|
|
24
|
+
/**
|
|
25
|
+
* Which job line, and which `~` rule under it, a map fact carries out. The map writes `from` as "line N", "line N rule M", "rule M" or
|
|
26
|
+
* "job line"; anything else is the first job line.
|
|
27
|
+
* @param {unknown} from @param {{n: number, text?: string, rule?: string, rules?: string[]}[]} jobLines
|
|
28
|
+
* @returns {{line: number, rule?: number}}
|
|
29
|
+
*/
|
|
30
|
+
export function tieOf(from, jobLines) {
|
|
31
|
+
const s = String(from ?? '');
|
|
32
|
+
const lineM = /line\s*(\d+)/i.exec(s);
|
|
33
|
+
const ruleM = /rule\s*(\d+)/i.exec(s);
|
|
34
|
+
const first = jobLines[0]?.n ?? 1;
|
|
35
|
+
let line = lineM && jobLines.some((l) => l.n === Number(lineM[1])) ? Number(lineM[1]) : null;
|
|
36
|
+
const rule = ruleM ? Number(ruleM[1]) : undefined;
|
|
37
|
+
if (line === null && rule !== undefined) line = jobLines.find((l) => lineRules(l).length >= rule)?.n ?? null;
|
|
38
|
+
const out = { line: line ?? first };
|
|
39
|
+
if (rule !== undefined && lineRules(jobLines.find((l) => l.n === out.line)).length >= rule) return { ...out, rule };
|
|
40
|
+
return out;
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
/**
|
|
44
|
+
* Is this a legal `reading` for a fact of this shape? Only a time or money fact carries one, with the op set and the bound types of a signed
|
|
45
|
+
* fact rule (src/facts.js): no other key, no missing bound. A malformed one makes the whole map answer malformed (no repair).
|
|
46
|
+
* @param {unknown} shape @param {unknown} r @returns {boolean}
|
|
47
|
+
*/
|
|
48
|
+
export function validReading(shape, r) {
|
|
49
|
+
if (shape !== 'time' && shape !== 'money') return false;
|
|
50
|
+
if (!isRec(r) || !FACT_OPS.includes(r.op)) return false;
|
|
51
|
+
const keys = Object.keys(r).sort().join();
|
|
52
|
+
return r.op === 'between' ? keys === 'hi,lo,op' && boundOk(shape, r.lo) && boundOk(shape, r.hi) : keys === 'op,value' && boundOk(shape, r.value);
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
/** @param {string} text @returns {string[]} the page's lines, white space squashed (the unit every per-line rule counts in) */
|
|
56
|
+
export const linesOf = (text) => text.split('\n').map(squash);
|
|
57
|
+
|
|
58
|
+
/**
|
|
59
|
+
* How many times a label occurs inside ONE page line, at most (plain indexOf on squashed lines, non-overlapping): the N2 measure.
|
|
60
|
+
* @param {string[]} lines @param {string} phrase @returns {number}
|
|
61
|
+
*/
|
|
62
|
+
function maxPerLine(lines, phrase) {
|
|
63
|
+
const p = squash(phrase).trim();
|
|
64
|
+
if (!p) return 0;
|
|
65
|
+
let max = 0;
|
|
66
|
+
for (const ln of lines) {
|
|
67
|
+
let n = 0;
|
|
68
|
+
for (let i = ln.indexOf(p); i !== -1; i = ln.indexOf(p, i + p.length)) n += 1;
|
|
69
|
+
if (n > max) max = n;
|
|
70
|
+
}
|
|
71
|
+
return max;
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
/** the value each shape reads right after a label: a money or number is digits (a sign may come first), a time is h:mm and AM/PM */
|
|
75
|
+
const READ = {
|
|
76
|
+
money: /^\s*[$€£]?\s*(\d[\d,]*(?:\.\d+)?)/,
|
|
77
|
+
time: /^\s*(\d{1,2}:\d{2}[\s\u202f\u00a0]?[AaPp][Mm])/,
|
|
78
|
+
number: /^\s*(\d[\d,]*(?:\.\d+)?)/,
|
|
79
|
+
};
|
|
80
|
+
|
|
81
|
+
/**
|
|
82
|
+
* R1 reader: the set of values of a fact's shape written right after each occurrence of a label on the page.
|
|
83
|
+
* @param {string[]} lines @param {string} label @param {'money'|'time'|'number'} shape @returns {Set<string>}
|
|
84
|
+
*/
|
|
85
|
+
function valueSet(lines, label, shape) {
|
|
86
|
+
/** @type {Set<string>} */
|
|
87
|
+
const s = new Set();
|
|
88
|
+
const p = squash(label).trim();
|
|
89
|
+
if (!p) return s;
|
|
90
|
+
for (const ln of lines) {
|
|
91
|
+
for (let i = ln.indexOf(p); i !== -1; i = ln.indexOf(p, i + p.length)) {
|
|
92
|
+
const m = READ[shape].exec(ln.slice(i + p.length));
|
|
93
|
+
if (m) s.add(m[1]);
|
|
94
|
+
}
|
|
95
|
+
}
|
|
96
|
+
return s;
|
|
97
|
+
}
|
|
98
|
+
/** @param {Set<string>} a @param {Set<string>} b @returns {boolean} */
|
|
99
|
+
const sameSet = (a, b) => a.size === b.size && [...a].every((x) => b.has(x));
|
|
100
|
+
|
|
101
|
+
/** a number as the page writes it: digits, thousands commas, optional decimals (a currency sign next to it is not part of it) */
|
|
102
|
+
const BEFORE = /(\d[\d,]*(?:\.\d+)?)\s*$/;
|
|
103
|
+
const AFTER = /^\s*(?:[$€£]\s*)?(\d[\d,]*(?:\.\d+)?)/;
|
|
104
|
+
const AFTER_CUR = /^\s*(\d[\d,]*(?:\.\d+)?)/;
|
|
105
|
+
const stripCommas = (/** @type {string} */ s) => s.replace(/,/g, '');
|
|
106
|
+
|
|
107
|
+
/** @param {string[]} lines @param {string} w @returns {[string, number][]} every occurrence of w as [its line, where it starts] */
|
|
108
|
+
function occs(lines, w) {
|
|
109
|
+
/** @type {[string, number][]} */
|
|
110
|
+
const out = [];
|
|
111
|
+
for (const l of lines) for (let i = l.indexOf(w); i !== -1; i = l.indexOf(w, i + w.length)) out.push([l, i]);
|
|
112
|
+
return out;
|
|
113
|
+
}
|
|
114
|
+
|
|
115
|
+
/**
|
|
116
|
+
* U1: how many times a label has a number right BEFORE it (whitespace only between) and right AFTER it (whitespace and one currency sign
|
|
117
|
+
* between). A marker the page writes after the number ("490 total") has B > 0 and A = 0.
|
|
118
|
+
* @param {string[]} lines @param {string} label @returns {{B: number, A: number}}
|
|
119
|
+
*/
|
|
120
|
+
function beforeAfter(lines, label) {
|
|
121
|
+
let B = 0;
|
|
122
|
+
let A = 0;
|
|
123
|
+
for (const [l, i] of occs(lines, label)) {
|
|
124
|
+
if (BEFORE.test(l.slice(0, i))) B += 1;
|
|
125
|
+
if (AFTER.test(l.slice(i + label.length))) A += 1;
|
|
126
|
+
}
|
|
127
|
+
return { B, A };
|
|
128
|
+
}
|
|
129
|
+
/** R1L: the numbers a UNIT form reads - the one right before each occurrence @param {string[]} lines @param {string} u @returns {Set<string>} */
|
|
130
|
+
const unitSet = (lines, u) => new Set(occs(lines, u).map(([l, i]) => BEFORE.exec(l.slice(0, i))?.[1]).filter((v) => typeof v === 'string').map((v) => stripCommas(/** @type {string} */ (v))));
|
|
131
|
+
/** R1L: the numbers a CURRENCY form reads - the one right after each occurrence @param {string[]} lines @param {string} c @returns {Set<string>} */
|
|
132
|
+
const curSet = (lines, c) => new Set(occs(lines, c).map(([l, i]) => AFTER_CUR.exec(l.slice(i + c.length))?.[1]).filter((v) => typeof v === 'string').map((v) => stripCommas(/** @type {string} */ (v))));
|
|
133
|
+
|
|
134
|
+
/** S1: a fact name that counts results ("the second flight") cannot be checked per result - one result is one record, ask for N results instead */
|
|
135
|
+
const ORDINALS = ['first', 'second', 'third', 'fourth', 'fifth', 'sixth', 'seventh', 'eighth', 'ninth', 'tenth', 'latest', 'earliest', 'newest', 'oldest', 'last', 'next', 'previous', 'final', 'nth'];
|
|
136
|
+
|
|
137
|
+
/** the symbols a currency may be (src/facts.js); a code like "EUR" is a word read after the number, so it is a unit */
|
|
138
|
+
const CURRENCY_SYMBOLS = ['$', '€', '£'];
|
|
139
|
+
/** the shapes that read a value after a label */
|
|
140
|
+
const VALUE_SHAPES = ['money', 'time', 'number'];
|
|
141
|
+
|
|
142
|
+
/**
|
|
143
|
+
* Apply the rules to the AI's map over one saved page. Returns the kept facts and, for everything a rule removed, one `dropped` entry.
|
|
144
|
+
* The order of the rules is the POC's (apply-v3.mjs): found-on-page, then the rules that read the page. A place is read as a phrase.
|
|
145
|
+
* @param {string} text the frozen page text @param {PageMap} mapping @param {{n: number, text?: string, rule?: string, rules?: string[]}[]} jobLines
|
|
146
|
+
* @returns {{facts: KeptFact[], dropped: Dropped[]}}
|
|
147
|
+
*/
|
|
148
|
+
export function applyPageRules(text, mapping, jobLines) {
|
|
149
|
+
/** @type {Dropped[]} */
|
|
150
|
+
const dropped = [];
|
|
151
|
+
const all = Array.isArray(mapping?.facts) ? mapping.facts.filter(isRec) : [];
|
|
152
|
+
/** @type {Map<MapFact, {line: number, rule?: number}>} */
|
|
153
|
+
const ties = new Map(all.map((f) => [f, tieOf(f.from, jobLines)]));
|
|
154
|
+
const drop = (/** @type {MapFact} */ f, /** @type {string|undefined} */ word, /** @type {string} */ reason, /** @type {string|undefined} */ other = undefined) => {
|
|
155
|
+
const t = /** @type {{line: number, rule?: number}} */ (ties.get(f));
|
|
156
|
+
dropped.push({ line: t.line, ...(t.rule === undefined ? {} : { rule: t.rule }), fact: String(f.name ?? ''), ...(word === undefined ? {} : { word }), reason, ...(other === undefined ? {} : { other }) });
|
|
157
|
+
};
|
|
158
|
+
|
|
159
|
+
// ---- S1, name part: a fact whose name counts results is refused before anything else reads it ----
|
|
160
|
+
const list = all.filter((f) => {
|
|
161
|
+
const hit = String(f.name ?? '').toLowerCase().split(/[^a-z]+/).find((w) => ORDINALS.includes(w));
|
|
162
|
+
if (hit === undefined) return true;
|
|
163
|
+
drop(f, undefined, 's1-name');
|
|
164
|
+
return false;
|
|
165
|
+
});
|
|
166
|
+
|
|
167
|
+
// ---- found-on-page, then U1 and R1L for the facts that read an amount ----
|
|
168
|
+
const lines = linesOf(text);
|
|
169
|
+
/** @type {{f: MapFact, kept: string[], cur: string[], uni: string[], first?: string|null, anchorless?: boolean}[]} */
|
|
170
|
+
const pending = [];
|
|
171
|
+
for (const f of list) {
|
|
172
|
+
const words = (Array.isArray(f.words) ? f.words : []).map((w) => String(w)).filter((w) => w.trim() !== '');
|
|
173
|
+
if (f.shape === 'place' || f.shape === 'phrase') {
|
|
174
|
+
const kept = [];
|
|
175
|
+
for (const w of words) {
|
|
176
|
+
const n = f.shape === 'phrase' ? countPhrase(text, w) : countExact(text, w);
|
|
177
|
+
if (n >= 1) kept.push(w); else drop(f, w, 'not-on-page');
|
|
178
|
+
}
|
|
179
|
+
pending.push({ f, kept, cur: [], uni: [] });
|
|
180
|
+
} else if (VALUE_SHAPES.includes(String(f.shape))) {
|
|
181
|
+
const amount = f.shape === 'money' || f.shape === 'number';
|
|
182
|
+
/** labels the page holds (and, for an amount, that have a number right after them) */
|
|
183
|
+
const labels = [];
|
|
184
|
+
/** the AI's labels as U1 leaves them, a word the page does not hold included: the first of these is R1's reference */
|
|
185
|
+
const asListed = [];
|
|
186
|
+
/** U1: labels the page writes after the number */
|
|
187
|
+
const moved = [];
|
|
188
|
+
for (const w0 of words) {
|
|
189
|
+
const w = normLabel(w0);
|
|
190
|
+
if (!w) continue;
|
|
191
|
+
if (countExact(text, w) < 1) { asListed.push(w0); drop(f, w0, 'not-on-page'); continue; }
|
|
192
|
+
if (amount) {
|
|
193
|
+
const { B, A } = beforeAfter(lines, w);
|
|
194
|
+
if (A === 0 && B > 0) { moved.push(w); continue; }
|
|
195
|
+
if (A === 0 && B === 0) { drop(f, w, 'no-value'); continue; }
|
|
196
|
+
}
|
|
197
|
+
asListed.push(w0);
|
|
198
|
+
labels.push(w);
|
|
199
|
+
}
|
|
200
|
+
/** @type {string[]} */ let uni = [];
|
|
201
|
+
/** @type {string[]} */ let cur = [];
|
|
202
|
+
if (f.shape === 'money') {
|
|
203
|
+
// the forms: units first (the U1-moved words, then the AI's listed units), then currency signs. SORT, never drop for the box (money
|
|
204
|
+
// build v2): the box is position only - a sign (before the number) is a currency, any other form (a word after it) is a unit - so every
|
|
205
|
+
// form the AI listed in either box is pooled and moved to the box its own shape belongs to
|
|
206
|
+
const listedCurrencies = (Array.isArray(f.currency) ? f.currency : []).map(String);
|
|
207
|
+
// a currency SIGN listed in BOTH unit and currency is said once: the unit copy goes (fwd F71 drafts)
|
|
208
|
+
const listedUnits = (Array.isArray(f.unit) ? f.unit : []).map(String).filter((u) => {
|
|
209
|
+
if (!(CURRENCY_SYMBOLS.includes(u) && listedCurrencies.includes(u))) return true;
|
|
210
|
+
drop(f, u, 'sign-twice');
|
|
211
|
+
return false;
|
|
212
|
+
});
|
|
213
|
+
// a WORD listed in both boxes is one form, said once (a record of the double listing is kept, as R1L always said it)
|
|
214
|
+
for (const w of new Set(listedCurrencies.filter((c) => !CURRENCY_SYMBOLS.includes(c) && listedUnits.includes(c)))) drop(f, w, 'r1l', w);
|
|
215
|
+
const pool = [...moved, ...listedUnits, ...listedCurrencies];
|
|
216
|
+
const units = [...new Set(pool.filter((w) => !CURRENCY_SYMBOLS.includes(w)))].filter((u) => {
|
|
217
|
+
if (countPhrase(text, u) >= 1) return true;
|
|
218
|
+
drop(f, u, 'not-on-page');
|
|
219
|
+
return false;
|
|
220
|
+
});
|
|
221
|
+
const curs = [...new Set(pool.filter((w) => CURRENCY_SYMBOLS.includes(w)))].filter((c) => {
|
|
222
|
+
if (countExact(text, c) >= 1) return true;
|
|
223
|
+
drop(f, c, 'not-on-page');
|
|
224
|
+
return false;
|
|
225
|
+
});
|
|
226
|
+
/** @type {{kind: 'unit'|'currency', w: string}[]} */
|
|
227
|
+
const forms = [...units.map((u) => /** @type {const} */ ({ kind: 'unit', w: u })), ...curs.map((c) => /** @type {const} */ ({ kind: 'currency', w: c }))];
|
|
228
|
+
if (forms.length) {
|
|
229
|
+
// R1L: every form reads the same set of numbers as the primary (the first form); one that reads more or fewer is dropped
|
|
230
|
+
const read = (/** @type {{kind: string, w: string}} */ x) => (x.kind === 'unit' ? unitSet(lines, x.w) : curSet(lines, x.w));
|
|
231
|
+
const ps = read(forms[0]);
|
|
232
|
+
const keep = [forms[0]];
|
|
233
|
+
// the same word listed as both a unit and a currency is ONE form: it is never compared with itself, only said (the unit, listed first, stays)
|
|
234
|
+
for (const x of forms.slice(1)) { if (x.w !== forms[0].w && sameSet(read(x), ps)) keep.push(x); else drop(f, x.w, 'r1l', forms[0].w); }
|
|
235
|
+
uni = keep.filter((x) => x.kind === 'unit').map((x) => x.w);
|
|
236
|
+
cur = keep.filter((x) => x.kind === 'currency').map((x) => x.w);
|
|
237
|
+
}
|
|
238
|
+
// the guard's list limits (src/facts.js), applied here so a longer list is cut by code and SAID, never refused later: the first forms stay
|
|
239
|
+
uni = uni.filter((u, i) => {
|
|
240
|
+
if (u.length > MAX_UNIT_CHARS) { drop(f, u, 'unit-too-long'); return false; }
|
|
241
|
+
if (i >= MAX_UNITS) { drop(f, u, 'limit-unit'); return false; }
|
|
242
|
+
return true;
|
|
243
|
+
});
|
|
244
|
+
cur = cur.filter((c, i) => {
|
|
245
|
+
if (i < MAX_CURRENCIES) return true;
|
|
246
|
+
drop(f, c, 'limit-currency');
|
|
247
|
+
return false;
|
|
248
|
+
});
|
|
249
|
+
}
|
|
250
|
+
pending.push({ f, kept: labels, cur, uni, first: asListed.length ? normLabel(asListed[0]) : null, anchorless: amount && asListed.length === 0 });
|
|
251
|
+
} else drop(f, undefined, 'bad-shape');
|
|
252
|
+
}
|
|
253
|
+
|
|
254
|
+
// ---- R1, same-value: the labels after the AI's FIRST listed one are kept only if they read the same set of values as it does ----
|
|
255
|
+
for (const p of pending) {
|
|
256
|
+
if (!VALUE_SHAPES.includes(String(p.f.shape))) continue;
|
|
257
|
+
const shape = /** @type {'money'|'time'|'number'} */ (p.f.shape);
|
|
258
|
+
const ref = p.first ? valueSet(lines, p.first, shape) : new Set();
|
|
259
|
+
p.kept = p.kept.filter((w) => {
|
|
260
|
+
if (w === p.first) return true;
|
|
261
|
+
if (sameSet(valueSet(lines, w, shape), ref)) return true;
|
|
262
|
+
drop(p.f, w, 'r1', p.first ?? undefined);
|
|
263
|
+
return false;
|
|
264
|
+
});
|
|
265
|
+
}
|
|
266
|
+
|
|
267
|
+
// ---- N2, per line: a label written 2+ times inside one page line cannot say which value is meant, so it is unusable. (R2 grow-left is OUT.) ----
|
|
268
|
+
for (const p of pending) {
|
|
269
|
+
if (!VALUE_SHAPES.includes(String(p.f.shape))) continue;
|
|
270
|
+
p.kept = p.kept.filter((w) => {
|
|
271
|
+
if (maxPerLine(lines, w) <= 1) return true;
|
|
272
|
+
drop(p.f, w, 'n2');
|
|
273
|
+
return false;
|
|
274
|
+
});
|
|
275
|
+
}
|
|
276
|
+
|
|
277
|
+
// ---- the kept facts, in the POC's order: places, phrases, then values ----
|
|
278
|
+
/** @type {KeptFact[]} */
|
|
279
|
+
const signed = [];
|
|
280
|
+
const make = (/** @type {{f: MapFact, kept: string[], cur: string[], uni: string[]}} */ p) => {
|
|
281
|
+
const t = /** @type {{line: number, rule?: number}} */ (ties.get(p.f));
|
|
282
|
+
/** @type {KeptFact} */
|
|
283
|
+
const out = { line: t.line, name: String(p.f.name), shape: p.f.shape === 'place' ? 'phrase' : String(p.f.shape), from: String(p.f.from ?? ''), words: [...p.kept] };
|
|
284
|
+
if (p.f.shape === 'money') {
|
|
285
|
+
if (p.uni.length) out.unit = [...p.uni];
|
|
286
|
+
if (p.cur.length) out.currency = [...p.cur];
|
|
287
|
+
}
|
|
288
|
+
if (t.rule !== undefined) out.forRule = t.rule;
|
|
289
|
+
if (validReading(out.shape, p.f.reading)) out.reading = /** @type {Reading} */ ({ ...p.f.reading });
|
|
290
|
+
return out;
|
|
291
|
+
};
|
|
292
|
+
for (const kind of ['place', 'phrase', 'value']) {
|
|
293
|
+
for (const p of pending) {
|
|
294
|
+
const isValue = VALUE_SHAPES.includes(String(p.f.shape));
|
|
295
|
+
if (kind === 'value' ? !isValue : p.f.shape !== kind) continue;
|
|
296
|
+
// a fact left with no label is dropped, unless U1 moved every label to a unit/currency form of a money fact (it then reads that form
|
|
297
|
+
// alone); a money fact also needs a currency or a unit the page holds
|
|
298
|
+
const forms = p.cur.length + p.uni.length;
|
|
299
|
+
const ok = p.f.shape === 'money' ? forms > 0 && (p.kept.length > 0 || p.anchorless === true) : p.kept.length > 0;
|
|
300
|
+
if (!ok) { drop(p.f, undefined, 'no-label'); continue; }
|
|
301
|
+
signed.push(make(p));
|
|
302
|
+
}
|
|
303
|
+
}
|
|
304
|
+
// ---- S1, shape and label part: two facts of one result may not share a shape AND a label (they would read the same value and can never differ).
|
|
305
|
+
// The first one stays; a fact with no label is keyed by its first unit or currency, so two different amounts are never confused. ----
|
|
306
|
+
/** @type {Map<string, string>} */
|
|
307
|
+
const seen = new Map();
|
|
308
|
+
const kept = signed.filter((k) => {
|
|
309
|
+
const key = `${k.shape}\u0000${k.words[0] ?? k.unit?.[0] ?? k.currency?.[0] ?? ''}`;
|
|
310
|
+
const first = seen.get(key);
|
|
311
|
+
if (first === undefined) { seen.set(key, k.name); return true; }
|
|
312
|
+
const f = /** @type {MapFact} */ (all.find((x) => String(x.name) === k.name));
|
|
313
|
+
drop(f, undefined, 's1', first);
|
|
314
|
+
return false;
|
|
315
|
+
});
|
|
316
|
+
const order = list.map((f) => String(f.name));
|
|
317
|
+
return { facts: kept.sort((a, b) => order.findIndex((n) => a.name.startsWith(n)) - order.findIndex((n) => b.name.startsWith(n))), dropped };
|
|
318
|
+
}
|
package/src/panel/author.js
CHANGED
|
@@ -38,6 +38,7 @@ import { fileURLToPath } from 'node:url';
|
|
|
38
38
|
import {
|
|
39
39
|
ANSWER_FILE, NOT_CHECKED_FILE, OPEN_QUESTION_SAY, PANEL_DRAFTS_DIR, readQuestions, readoutHeadIndexes, scrub, signDraft,
|
|
40
40
|
} from '../authoring.js';
|
|
41
|
+
import { ruleOfAnswer } from '../pageask.js';
|
|
41
42
|
import { checkFlowName, nextRunId, readFileInside, readdirInside } from '../flow.js';
|
|
42
43
|
import { readModelRecord } from '../flowmodel.js';
|
|
43
44
|
import { isFwdloopAlive } from '../liveness.js';
|
|
@@ -47,7 +48,7 @@ import {
|
|
|
47
48
|
import {
|
|
48
49
|
appendAnswersToJob, capFloorFor, capFloorOf, capFloorUsd, cardFields, checkCard, checkInputRows, parseInputLines, parseJobBox,
|
|
49
50
|
} from './authorcard.js';
|
|
50
|
-
import { checkedLines,
|
|
51
|
+
import { checkedLines, notCheckedView } from '../checked.js';
|
|
51
52
|
import {
|
|
52
53
|
readPriceConfig, resolveSlot, resolveUsableModel, tickedLines,
|
|
53
54
|
} from '../provider.js';
|
|
@@ -89,6 +90,8 @@ const LIVE_PHASES = new Set(['drafting', 'revising', 'redrafting']);
|
|
|
89
90
|
const ANSWERS_NOT_DRAFTED_SAY = 'Your answers are saved, but the plan was not drafted again with them (the process did not start). Nothing was signed. Draft again from the card.';
|
|
90
91
|
const REDRAFT_RED_SAY = 'The plan drafted with your answers did not pass the checks. Nothing can be signed until a plan is green. Your answers are kept in the card: change it and draft again.';
|
|
91
92
|
const BLANK_ANSWER_SAY = 'Write an answer. A question can\'t be skipped; the plan waits for it.';
|
|
93
|
+
/** M10 am2 item 1: the sentence for a blank answer to a question the page look raised (bareloop's words). */
|
|
94
|
+
const LOOK_BLANK_ANSWER_SAY = 'An answer is needed here — there is no skip. Type it in the box below and press Send.';
|
|
92
95
|
const ONE_LINE_ANSWER_SAY = 'Write the answer on one line. It is added to your job as one guardrail line.';
|
|
93
96
|
const PLAN_DIR_RE = /^draft-(\d+)$/;
|
|
94
97
|
const REVISE_RED_SAY = 'The revised plan did not pass the checks. Nothing can be signed until a plan is green. Your edits are kept: change the card and draft again.';
|
|
@@ -157,7 +160,8 @@ export function createAuthor(opts) {
|
|
|
157
160
|
const running = childRunning(dir);
|
|
158
161
|
const first = resultOf(dir, 'draft', keys);
|
|
159
162
|
// a first plan that asked questions (green or red) is read by `withRevises` too: it is the ONE function that decides the phase of every plan
|
|
160
|
-
|
|
163
|
+
const firstQs = first && first.phase !== 'green' ? readQuestions(join(dir, 'draft')) : undefined; // read once; withRevises reuses it
|
|
164
|
+
if (first && (first.phase === 'green' || (firstQs?.questions.length ?? 0) > 0)) return withRevises(base, dir, first, keys, firstQs);
|
|
161
165
|
if (first) return { ...base, ...first };
|
|
162
166
|
// no result yet: the child is the only thing that can still produce one
|
|
163
167
|
if (running) return { ...base, phase: 'drafting' };
|
|
@@ -234,9 +238,9 @@ export function createAuthor(opts) {
|
|
|
234
238
|
* A red or stopped newest plan means nothing is signable (an older green plan was drafted from a different card), and the card shown is the one
|
|
235
239
|
* last submitted. `phase` is `revising` / `redrafting` only while the newest plan's child runs. A revise uses one of the 2; an answer redraft
|
|
236
240
|
* (marked by `redraft-<n>.json`) uses none. One reader of every such fact: each `card-<n>.json`, its folder and log, its questions and answers.
|
|
237
|
-
* @param {any} base @param {string} dir @param {any} first the first plan's result (green, or red that asked questions) @param {string[]} keys
|
|
241
|
+
* @param {any} base @param {string} dir @param {any} first the first plan's result (green, or red that asked questions) @param {string[]} keys @param {ReturnType<typeof readQuestions>} [firstQsRead] the first plan's questions when the caller already read them
|
|
238
242
|
*/
|
|
239
|
-
function withRevises(base, dir, first, keys) {
|
|
243
|
+
function withRevises(base, dir, first, keys, firstQsRead) {
|
|
240
244
|
const names = readdirInside(dir, '.');
|
|
241
245
|
const ns = names.map((n) => CARD_RE.exec(n)?.[1]).filter((x) => x !== undefined).map(Number).sort((a, b) => a - b);
|
|
242
246
|
const redrafts = new Set(ns.filter((n) => readJson(dir, `redraft-${n}.json`) !== null));
|
|
@@ -262,17 +266,18 @@ export function createAuthor(opts) {
|
|
|
262
266
|
});
|
|
263
267
|
const cur = /** @type {{ phase: string, kind: string, say?: string }} */ (newest > 0 ? revises[revises.length - 1] : { phase: first.phase, kind: 'first' });
|
|
264
268
|
// a first plan that asked questions did not end red, it ended by asking (open or answered): its plan-checks line is never the red one (am15 item 3)
|
|
265
|
-
const
|
|
269
|
+
const firstQs = firstQsRead ?? readQuestions(join(dir, 'draft')); // the first plan's questions, read once per poll
|
|
270
|
+
const asked = firstQs.questions.length > 0;
|
|
266
271
|
const shared = { ...base, card, revises, revisesLeft, stepList: asked ? stepListOf(dir, 'draft', keys, true) : (first.stepList ?? null) };
|
|
267
272
|
const rel = newest > 0 ? `draft-${newest}` : 'draft';
|
|
268
273
|
if (cur.phase === 'running') return { ...shared, phase: cur.kind === 'answers' ? 'redrafting' : 'revising' };
|
|
269
274
|
if (cur.phase === 'stopped') return { ...shared, phase: 'stopped', say: cur.say };
|
|
270
275
|
const res = newest > 0 ? resultOf(dir, rel, keys) : first;
|
|
271
276
|
// the plan's questions: any open one makes the phase `questions-open`, whatever colour the plan is (the answers are what it was waiting for)
|
|
272
|
-
const q = readQuestions(join(dir, rel));
|
|
277
|
+
const q = rel === 'draft' ? firstQs : readQuestions(join(dir, rel));
|
|
273
278
|
if (q.open.length > 0) {
|
|
274
279
|
const lineText = (line) => parseJobBox(typeof card?.job === 'string' ? card.job : '').steps[line - 1]?.text ?? null;
|
|
275
|
-
const questions = q.questions.map((x) => ({ k: x.k, line: x.line, lineText: lineText(x.line), question: scrub(x.question, keys), answered: x.answer !== null }));
|
|
280
|
+
const questions = q.questions.map((x) => ({ k: x.k, line: x.line, lineText: lineText(x.line), question: scrub(x.question, keys), answered: x.answer !== null, ...(x.source === 'look' ? { source: 'look' } : {}) }));
|
|
276
281
|
return { ...shared, stepList: stepListOf(dir, rel, keys, true), phase: 'questions-open', plan: rel, questions, openK: q.open[0].k, total: q.questions.length };
|
|
277
282
|
}
|
|
278
283
|
if (q.questions.length > 0) return { ...shared, phase: 'stopped', say: ANSWERS_NOT_DRAFTED_SAY };
|
|
@@ -292,7 +297,7 @@ export function createAuthor(opts) {
|
|
|
292
297
|
const prose = readFileInside(dir, `${rel}/prose.txt`);
|
|
293
298
|
const parsed = prose.ok ? parseSignedText(prose.text) : null;
|
|
294
299
|
const opts = parsed?.ok === true ? { askLines: parsed.arbiter.asks.map((x) => x.line) } : { hasAsk: true };
|
|
295
|
-
return { checked: checkedLines(decl, opts), notChecked:
|
|
300
|
+
return { checked: checkedLines(decl, opts), notChecked: notCheckedView(readJson(dir, `${rel}/${NOT_CHECKED_FILE}`)?.notChecked, decl) };
|
|
296
301
|
}
|
|
297
302
|
|
|
298
303
|
/** The answers an answer-redraft was drafted with, as the marker recorded them: `[{ line, lineText, question, answer }]` (scrubbed). @param {string} dir @param {number} n @param {string[]} keys */
|
|
@@ -697,22 +702,34 @@ export function createAuthor(opts) {
|
|
|
697
702
|
if (v.phase !== 'questions-open') return { status: 409, body: { ok: false, refused: 'no-question-open', say: 'This draft has no question waiting for an answer.' } };
|
|
698
703
|
const b = body && typeof body === 'object' ? body : {};
|
|
699
704
|
if (b.k !== v.openK) return { status: 409, body: { ok: false, refused: 'stale-question', say: 'That is not the question now open. Reload and answer the one shown.' } };
|
|
700
|
-
const
|
|
701
|
-
|
|
702
|
-
|
|
705
|
+
const planDir = join(dir, v.plan);
|
|
706
|
+
const full = readQuestions(planDir).questions;
|
|
707
|
+
const q = full.find((x) => x.k === v.openK);
|
|
708
|
+
if (q === undefined) return { status: 409, body: { ok: false, refused: 'stale-question', say: 'That is not the question now open. Reload and answer the one shown.' } };
|
|
709
|
+
// M10 piece 3 (am2): a question the page look raised takes bareloop's answer words. A blank answer gets bareloop's sentence; a newline is folded to a
|
|
710
|
+
// space (one `~` line is one line) instead of refused. Every other question keeps its own gate (blank, one line).
|
|
711
|
+
const look = q.source === 'look';
|
|
712
|
+
const answer = typeof b.answer === 'string' ? (look ? b.answer.replace(/\s*\n\s*/g, ' ') : b.answer).trim() : '';
|
|
713
|
+
if (answer === '') return { status: 400, body: { ok: false, refused: 'blank-answer', say: look ? LOOK_BLANK_ANSWER_SAY : BLANK_ANSWER_SAY } };
|
|
714
|
+
if (!look && /[\r\n]/.test(answer)) return { status: 400, body: { ok: false, refused: 'one-line', say: ONE_LINE_ANSWER_SAY } };
|
|
703
715
|
if (keys.some((k) => k.length >= 8 && answer.includes(k))) return { status: 400, body: { ok: false, refused: 'key', say: 'This answer contains an API key. Keys go in Settings only. Nothing was saved.' } };
|
|
704
716
|
if (childRunning(dir)) return { status: 409, body: { ok: false, refused: 'draft-live', say: 'The draft process is still finishing. Try again in a moment.' } };
|
|
705
|
-
const planDir = join(dir, v.plan);
|
|
706
|
-
const q = v.questions.find((x) => x.k === v.openK);
|
|
707
717
|
const last = v.questions.filter((x) => !x.answered).length === 1;
|
|
708
718
|
const answerFile = join(planDir, ANSWER_FILE(v.openK));
|
|
719
|
+
const at = new Date().toISOString();
|
|
720
|
+
// what this answer does (ruleOfAnswer, the one place): the `~` rule it adds, or the rule it leaves Not checked
|
|
721
|
+
const effect = ruleOfAnswer(q, answer);
|
|
709
722
|
// the whole redraft is checked at $0 BEFORE the answer is written, so a refusal leaves the question open and nothing on disk
|
|
710
723
|
/** @type {any} */
|
|
711
724
|
let redraft = null;
|
|
712
725
|
if (last) {
|
|
713
|
-
const all =
|
|
714
|
-
|
|
715
|
-
|
|
726
|
+
const all = full.map((x) => {
|
|
727
|
+
const typed = x.k === v.openK ? answer : x.answer;
|
|
728
|
+
return { line: x.line, question: x.question, answer: typed, ...ruleOfAnswer(x, typed), ruleNo: x.ruleNo };
|
|
729
|
+
});
|
|
730
|
+
const adds = all.filter((x) => x.rule !== null);
|
|
731
|
+
const grown = appendAnswersToJob(typeof v.card.job === 'string' ? v.card.job : '', adds.map((x) => ({ line: x.line, answer: /** @type {string} */ (x.rule) })));
|
|
732
|
+
if (grown.added !== adds.length) return { status: 409, body: { ok: false, refused: 'no-such-line', say: 'An answer is about a job line the card no longer has. Draft again from the card. Nothing was signed.' } };
|
|
716
733
|
const vet = vetCard({ ...v.card, job: grown.job }, loaded, draftedSoFar(dir, v.card));
|
|
717
734
|
if (!vet.ok) return vet.reply;
|
|
718
735
|
for (const other of listIds()) {
|
|
@@ -720,10 +737,14 @@ export function createAuthor(opts) {
|
|
|
720
737
|
const ov = view(other, keys);
|
|
721
738
|
if (ov && LIVE_PHASES.has(ov.phase)) return { status: 409, body: { ok: false, refused: 'draft-live', draftId: other, say: 'A draft is already running. Wait for it, or abandon it, before answering.' } };
|
|
722
739
|
}
|
|
723
|
-
redraft = { vet, all };
|
|
740
|
+
redraft = { vet, all, left: all.filter((x) => x.left).map((x) => ({ line: x.line, rule: x.ruleNo })) };
|
|
724
741
|
}
|
|
725
742
|
try {
|
|
726
|
-
|
|
743
|
+
// the human row of the draft log (am2 item 4): the question, the answer, the line, the time; a look ask also carries what code proposed and the rule the answer added, as their own fields
|
|
744
|
+
writeFileSync(answerFile, `${JSON.stringify({
|
|
745
|
+
k: v.openK, line: q.line, question: q.question, answer, at,
|
|
746
|
+
...(look ? { source: 'look', ask: q.ask, ruleNo: q.ruleNo, ruleText: q.ruleText, proposal: q.proposal ?? null, rule: effect.rule } : {}),
|
|
747
|
+
})}\n`, { mode: 0o600, flag: 'wx' });
|
|
727
748
|
} catch (e) {
|
|
728
749
|
return { status: 409, body: { ok: false, refused: 'already-answered', say: /** @type {any} */ (e)?.code === 'EEXIST' ? 'That question already has an answer.' : 'The answer could not be saved. Nothing was sent.' } };
|
|
729
750
|
}
|
|
@@ -735,10 +756,10 @@ export function createAuthor(opts) {
|
|
|
735
756
|
try {
|
|
736
757
|
writeFileSync(files[0], `${JSON.stringify(redraft.vet.card, null, 2)}\n`, { mode: 0o600, flag: 'wx' });
|
|
737
758
|
writeFileSync(files[1], redraft.vet.prose, { mode: 0o600, flag: 'wx' });
|
|
738
|
-
writeFileSync(files[2], `${JSON.stringify({ fromPlan: v.plan, answers: redraft.all.map((x) => ({ line: x.line, question: x.question, answer: x.answer })) })}\n`, { mode: 0o600, flag: 'wx' });
|
|
759
|
+
writeFileSync(files[2], `${JSON.stringify({ fromPlan: v.plan, answers: redraft.all.map((x) => ({ line: x.line, question: x.question, answer: x.answer })), ...(redraft.left.length ? { left: redraft.left } : {}) })}\n`, { mode: 0o600, flag: 'wx' });
|
|
739
760
|
child = spawnDetached({
|
|
740
761
|
bin,
|
|
741
|
-
argv: ['draft', files[1], '--out', join(dir, `draft-${n}`), '--root', realpathSync(root), '--name', redraft.vet.card.flowName, '--no-questions', ...modelArgv(redraft.vet.card)],
|
|
762
|
+
argv: ['draft', files[1], '--out', join(dir, `draft-${n}`), '--root', realpathSync(root), '--name', redraft.vet.card.flowName, '--no-questions', ...(redraft.left.length ? ['--left', redraft.left.map((x) => `${x.line}.${x.rule}`).join(',')] : []), ...modelArgv(redraft.vet.card)],
|
|
742
763
|
env: loaded.env,
|
|
743
764
|
logPath: join(dir, `redraft-${n}.log`),
|
|
744
765
|
});
|
package/src/panel/authorcard.js
CHANGED
|
@@ -15,6 +15,7 @@ import {
|
|
|
15
15
|
import { isAbsolute, join } from 'node:path';
|
|
16
16
|
|
|
17
17
|
import { checkFlowName } from '../flow.js';
|
|
18
|
+
import { isWebUrl } from '../pagefetch.js';
|
|
18
19
|
import { checkSendDestination } from '../runner.js';
|
|
19
20
|
import { parseSignedText } from '../signed-text.js';
|
|
20
21
|
import { capTooSmallText } from '../draftspend.js';
|
|
@@ -177,6 +178,11 @@ export function cardFields(body) {
|
|
|
177
178
|
};
|
|
178
179
|
}
|
|
179
180
|
|
|
181
|
+
/** M10 am3: is this web address whole (a host after the scheme, no spaces)? The ONE reader of a typed page address. @param {string} v */
|
|
182
|
+
export function pageAddressOk(v) {
|
|
183
|
+
return /^https?:\/\/[^\s/?#]+\S*$/i.test(v);
|
|
184
|
+
}
|
|
185
|
+
|
|
180
186
|
/** The Run-a-signed-flow door's rows (a role is fixed by the signed flow): what the page sent, as `{role, path}`. @param {any} v */
|
|
181
187
|
export function runInputRows(v) {
|
|
182
188
|
return (Array.isArray(v) ? v : []).map((r) => ({ role: str(r?.role).trim(), path: str(r?.path).trim() }));
|
|
@@ -193,7 +199,7 @@ export function cardToProse(card) {
|
|
|
193
199
|
const out = [...jobFileLines(steps, card.askWait), '', ARBITER_HEADING];
|
|
194
200
|
out.push(`guardrail: cap $${capText(Number(card.capUsd))} per run`);
|
|
195
201
|
if (card.destination !== '') out.push(`guardrail: send at line ${steps.length} to file:${card.destination}`);
|
|
196
|
-
for (const i of parseInputLines(card.inputs)) out.push(`guardrail: source ${i.role} = file:${i.path}`);
|
|
202
|
+
for (const i of parseInputLines(card.inputs)) out.push(`guardrail: source ${i.role} = ${isWebUrl(i.path) ? 'page' : 'file'}:${i.path}`); // M10 am3: a web address is a page
|
|
197
203
|
return `${out.join('\n')}\n`;
|
|
198
204
|
}
|
|
199
205
|
|
|
@@ -279,6 +285,10 @@ export function checkInputRows(rows) {
|
|
|
279
285
|
seen.add(row.role);
|
|
280
286
|
if (row.path === '') { no(`The input "${row.role}" has a role but no path.`); return; }
|
|
281
287
|
if (CONTROL_RE.test(row.path)) { no(`The path for "${row.role}" has a character that cannot be in a path.`); return; }
|
|
288
|
+
if (isWebUrl(row.path)) { // M10 am3: a web page's address is signed with the job; there is no file to look at
|
|
289
|
+
if (!pageAddressOk(row.path)) no(`The address for "${row.role}" must be a full http:// or https:// address with no spaces.`);
|
|
290
|
+
return;
|
|
291
|
+
}
|
|
282
292
|
if (!isAbsolute(row.path)) { no(`The path for "${row.role}" must be a full path starting with "/".`); return; }
|
|
283
293
|
const why = fileProblem(row.path);
|
|
284
294
|
if (why) no(`The path for "${row.role}" ${why}`);
|
package/src/panel/authorflows.js
CHANGED
|
@@ -34,6 +34,7 @@ import { checkValues } from './authorvalues.js';
|
|
|
34
34
|
import { getRunControls, getRunGlyph } from './data.js';
|
|
35
35
|
import { SCHEDULE_BUSY_SAY } from '../summary-words.js';
|
|
36
36
|
import { providerKeys } from './spawn.js';
|
|
37
|
+
import { pageAddressesOf, pageAddressRefusal } from '../pagefetch.js';
|
|
37
38
|
import { readModelRecord, writeModelRecord } from '../flowmodel.js';
|
|
38
39
|
|
|
39
40
|
/** M6 amendment 1: the sentence the page shows and the sign route refuses with, for a job with a run running or waiting at its ask. @param {string} runId */
|
|
@@ -89,22 +90,22 @@ export function createFlowsDoor(opts) {
|
|
|
89
90
|
};
|
|
90
91
|
}
|
|
91
92
|
|
|
92
|
-
/** The source paths one run froze: role -> path, from that run's `inputs.json`. Blank = none. @param {string} runDir @param {string[]} roles */
|
|
93
|
-
function sourcesOf(runDir, roles) {
|
|
93
|
+
/** The source paths one run froze: role -> path, from that run's `inputs.json`. Blank = none. @param {string} runDir @param {string[]} roles @param {Record<string, string>} [pages] M10 am3: page role -> signed address */
|
|
94
|
+
function sourcesOf(runDir, roles, pages = {}) {
|
|
94
95
|
/** @type {Record<string, string>} */
|
|
95
|
-
const out = Object.fromEntries(roles.map((r) => [r, '']));
|
|
96
|
+
const out = Object.fromEntries(roles.map((r) => [r, pages[r] ?? ''])); // M10 am3: a page input shows its signed address, whatever a run froze
|
|
96
97
|
if (runDir === '') return out;
|
|
97
98
|
const f = readFileInside(runDir, 'inputs.json');
|
|
98
99
|
let rows = [];
|
|
99
100
|
try { rows = f.ok ? JSON.parse(f.text) : []; } catch { rows = []; }
|
|
100
101
|
for (const row of Array.isArray(rows) ? rows : []) {
|
|
101
|
-
if (row && typeof row.id === 'string' && roles.includes(row.id) && typeof row.source === 'string') out[row.id] = row.source;
|
|
102
|
+
if (row && typeof row.id === 'string' && roles.includes(row.id) && !Object.hasOwn(pages, row.id) && typeof row.source === 'string') out[row.id] = row.source;
|
|
102
103
|
}
|
|
103
104
|
return out;
|
|
104
105
|
}
|
|
105
106
|
|
|
106
|
-
/** The source paths the newest run of this flow used: role -> path, from that run's `inputs.json`. Blank = none. @param {string} flowDir @param {string[]} roles */
|
|
107
|
-
function lastSources(flowDir, roles) {
|
|
107
|
+
/** The source paths the newest run of this flow used: role -> path, from that run's `inputs.json`. Blank = none. @param {string} flowDir @param {string[]} roles @param {Record<string, string>} [pages] */
|
|
108
|
+
function lastSources(flowDir, roles, pages = {}) {
|
|
108
109
|
let newest = null;
|
|
109
110
|
for (const id of listRunIds(flowDir)) {
|
|
110
111
|
const r = resolveRunDir(flowDir, id);
|
|
@@ -113,8 +114,8 @@ export function createFlowsDoor(opts) {
|
|
|
113
114
|
try { t = lstatSync(join(r.runDir, 'inputs.json')).mtimeMs; } catch { continue; }
|
|
114
115
|
if (newest === null || t > newest.t || (t === newest.t && id > newest.id)) newest = { t, id, runDir: r.runDir };
|
|
115
116
|
}
|
|
116
|
-
if (newest === null) return { runId: null, sources: sourcesOf('', roles) };
|
|
117
|
-
return { runId: newest.id, sources: sourcesOf(newest.runDir, roles) };
|
|
117
|
+
if (newest === null) return { runId: null, sources: sourcesOf('', roles, pages) };
|
|
118
|
+
return { runId: newest.id, sources: sourcesOf(newest.runDir, roles, pages) };
|
|
118
119
|
}
|
|
119
120
|
|
|
120
121
|
/** The values one run ran with: the flow's own, with the run's newest signed version laid over (`applyRunValues`). Unreadable = the flow's own. @param {any} read @param {string} name @param {string} runDir */
|
|
@@ -140,7 +141,7 @@ export function createFlowsDoor(opts) {
|
|
|
140
141
|
if (end === null) continue; // not ended (running, parked, died): neither green nor not green
|
|
141
142
|
if (end.outcome === 'complete') {
|
|
142
143
|
green += 1;
|
|
143
|
-
passed.push({ runId: id, ...runValuesOf(read, name, r.runDir), sources: sourcesOf(r.runDir, roles) });
|
|
144
|
+
passed.push({ runId: id, ...runValuesOf(read, name, r.runDir), sources: sourcesOf(r.runDir, roles, pageAddressesOf(read.arbiter)) });
|
|
144
145
|
} else notGreen += 1;
|
|
145
146
|
if (end.spendComplete === true && Number.isFinite(end.spentUsd) && end.spentUsd >= 0) { spendSum += end.spentUsd; spendN += 1; }
|
|
146
147
|
if (Number.isFinite(end.wallMs) && end.wallMs > 0) { wallSum += end.wallMs; wallN += 1; }
|
|
@@ -220,11 +221,16 @@ export function createFlowsDoor(opts) {
|
|
|
220
221
|
/** @type {{field: string, say: string}[]} */
|
|
221
222
|
const refusals = checked.ok ? [...modelReds] : [...checked.refusals, ...modelReds];
|
|
222
223
|
if (!checked.ok && checked.status === 409) return reply(409, { refused: 'monthly', say: checked.say });
|
|
223
|
-
|
|
224
|
+
// M10 am3: a page input's address is the job's, signed; the page shows it read-only, a blank row is the signed one, and any other address is refused naming the input
|
|
225
|
+
const pages = pageAddressesOf(read.arbiter);
|
|
226
|
+
const rows = runInputRows(body?.inputs).map((r) => (Object.hasOwn(pages, r.role) && r.path === '' ? { ...r, path: pages[r.role] } : r));
|
|
224
227
|
refusals.push(...checkInputRows(rows));
|
|
228
|
+
rows.forEach((r, n) => {
|
|
229
|
+
if (Object.hasOwn(pages, r.role) && r.path !== pages[r.role]) refusals.push({ field: `inputs.${n}`, say: pageAddressRefusal(r.role, pages[r.role]) });
|
|
230
|
+
});
|
|
225
231
|
const declared = (read.arbiter.sources ?? []).map((s) => s.role);
|
|
226
232
|
for (const role of declared) {
|
|
227
|
-
if (!rows.some((r) => r.role === role)) refusals.push({ field: 'inputs', say: `This flow needs an input named "${role}". Add it.` });
|
|
233
|
+
if (!Object.hasOwn(pages, role) && !rows.some((r) => r.role === role)) refusals.push({ field: 'inputs', say: `This flow needs an input named "${role}". Add it.` });
|
|
228
234
|
}
|
|
229
235
|
rows.forEach((r, n) => {
|
|
230
236
|
if (r.role !== '' && !declared.includes(r.role)) refusals.push({ field: `inputs.${n}`, say: `This flow has no input named "${r.role}" (it takes: ${declared.join(', ') || 'none'}).` });
|
|
@@ -240,7 +246,7 @@ export function createFlowsDoor(opts) {
|
|
|
240
246
|
flow, flowSignatureHash: read.signature.flow, runId: null, version: 0, values,
|
|
241
247
|
});
|
|
242
248
|
return {
|
|
243
|
-
ok: /** @type {true} */ (true), flow, read, declared, rows, runId, values, differs: checked.differs || runModel !== null, hash, newPick, flowDir: join(realRoot, flow), planFrom,
|
|
249
|
+
ok: /** @type {true} */ (true), flow, read, declared, rows: declared.map((role) => rows.find((r) => r.role === role) ?? { role, path: pages[role] }), runId, values, differs: checked.differs || runModel !== null, hash, newPick, flowDir: join(realRoot, flow), planFrom,
|
|
244
250
|
};
|
|
245
251
|
}
|
|
246
252
|
|
|
@@ -289,10 +295,10 @@ export function createFlowsDoor(opts) {
|
|
|
289
295
|
function entryFor(realRoot, name, read, left) {
|
|
290
296
|
const roles = (read.arbiter.sources ?? []).map((s) => s.role);
|
|
291
297
|
const flowDir = join(realRoot, name);
|
|
292
|
-
const last = lastSources(flowDir, roles);
|
|
298
|
+
const last = lastSources(flowDir, roles, pageAddressesOf(read.arbiter));
|
|
293
299
|
const { passed, stats } = runsOf(flowDir, name, read, roles);
|
|
294
300
|
return {
|
|
295
|
-
flow: name, capUsd: read.arbiter.capUsd, roles, nextRunId: nextRunId(flowDir), lastRunId: last.runId, lastSources: last.sources, leftThisMonth: left,
|
|
301
|
+
flow: name, capUsd: read.arbiter.capUsd, roles, pages: pageAddressesOf(read.arbiter), nextRunId: nextRunId(flowDir), lastRunId: last.runId, lastSources: last.sources, leftThisMonth: left,
|
|
296
302
|
runs: passed, stats,
|
|
297
303
|
...formFacts(read, home, flowDir),
|
|
298
304
|
// M6 am5/am6: the job as the card Reuse workflow opens (`ok:true, card`), or why the card cannot hold it (`ok:false, say`). Nothing blocks it: a reuse never changes the job it is opened from.
|
|
@@ -411,7 +417,7 @@ export function createFlowsDoor(opts) {
|
|
|
411
417
|
return {
|
|
412
418
|
status: 200,
|
|
413
419
|
body: {
|
|
414
|
-
ok: true, flow: entryFor(realRoot, flow, read, leftThisMonth()), pick: { runId, ...runValuesOf(read, flow, rr.runDir), sources: sourcesOf(rr.runDir, roles) },
|
|
420
|
+
ok: true, flow: entryFor(realRoot, flow, read, leftThisMonth()), pick: { runId, ...runValuesOf(read, flow, rr.runDir), sources: sourcesOf(rr.runDir, roles, pageAddressesOf(read.arbiter)) },
|
|
415
421
|
},
|
|
416
422
|
};
|
|
417
423
|
},
|