bareloop 0.49.0 → 0.50.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/src/pagefetch.js CHANGED
@@ -9,7 +9,7 @@
9
9
  // proven in step 0 ('read' and 'full' returned almost nothing from a results page). The text that comes back is the input. An empty
10
10
  // fetch is a plain-words stop, never a silent empty input. Nothing here touches a model.
11
11
 
12
- import { SECRET_PATTERNS } from './validate.js';
12
+ import { SECRET_PATTERNS, redactSecrets } from './validate.js';
13
13
  import { TAG } from './labelscan.js';
14
14
 
15
15
  /** the session options (incognito: no auth ever enters; blockPrivateNetwork: SSRF guard); ONE frozen object so a test can pin them */
@@ -30,7 +30,18 @@ export const MIN_PAGE_CHARS = 200;
30
30
  export const MIN_CONTENT_CHARS = 100;
31
31
  /** the page reader's landmark roles for a site's own chrome (the ARIA names of header, menu and footer); everything under one is not the page's content */
32
32
  const CHROME_ROLES = new Set(['banner', 'navigation', 'contentinfo']);
33
- /** our own deadline around one whole fetch (a hung browser leaves no trace; this is the only thing that fires on the absence of an answer) */
33
+ /** how many times one fetch is tried when it comes back header-only or times out (a real Dell page was header-only on one try and whole on the next) */
34
+ export const FETCH_ATTEMPTS = 3;
35
+ /** the pause between attempts (ms) */
36
+ export const RETRY_WAIT_MS = 3000;
37
+ /** a header-only snapshot is re-taken (same open page) every this many ms until content shows up (live: Dell de-de gave header-only 3 times in ~5 s
38
+ * each, while its content arrived ~18-35 s after load; a fast refetch hits the same unsettled page) */
39
+ export const SETTLE_INTERVAL_MS = 2000;
40
+ /** the most one attempt keeps re-reading a header-only page (ms) before it counts as header-only; the per-attempt deadline below still bounds the whole attempt */
41
+ export const SETTLE_BUDGET_MS = 20_000;
42
+ /** how many characters of a header-only page's content (after the snapshot header) the failure record keeps */
43
+ export const HEAD_KEEP_CHARS = 300;
44
+ /** our own deadline around ONE attempt (a fetch that retries can take up to FETCH_ATTEMPTS of these, plus the pauses); a hung browser leaves no trace, this is the only thing that fires on the absence of an answer */
34
45
  export const PAGE_FETCH_TIMEOUT_MS = 90_000;
35
46
 
36
47
  /** @param {unknown} v @returns {boolean} an http(s) address (the one spelling of "this Inputs value is a web address") */
@@ -70,13 +81,18 @@ export function pageContentChars(text) {
70
81
  * snapshot: (o?: {mode?: string}) => Promise<string>, close: () => Promise<void>}} FetchPage the part of barebrowse's page handle the fetch uses
71
82
  */
72
83
 
84
+ /** @typedef {{chars: number, ms: number, head: string}} AttemptRecord what a header-only attempt saw: content characters, elapsed ms, the first characters of the content (secret-masked) */
85
+
73
86
  /**
74
87
  * Fetch one page's text. `connect` is injected (a test never reaches the network); the default is barebrowse's own `connect`.
88
+ * The whole fetch (own browser, own page, closed each time) is tried up to FETCH_ATTEMPTS times, but ONLY when an attempt ends header-only (the page
89
+ * loaded its menus and not its content) or times out (our deadline, or the page load's own timeout). A bot challenge, a refused or private address, any
90
+ * other load error, a near-empty page and a non-text answer are final on the first attempt.
75
91
  * @param {string} url
76
- * @param {{connect?: (opts: Record<string, any>) => Promise<FetchPage>, timeoutMs?: number, idleMs?: number, idleTimeoutMs?: number, closeWaitMs?: number}} [o]
77
- * @returns {Promise<{ok: true, text: string, url: string}|{ok: false, error: string}>} `error` is plain words, without a spend line
92
+ * @param {{connect?: (opts: Record<string, any>) => Promise<FetchPage>, timeoutMs?: number, idleMs?: number, idleTimeoutMs?: number, closeWaitMs?: number, retryWaitMs?: number, settleMs?: number, settleIntervalMs?: number}} [o] `retryWaitMs` - the pause between attempts (a test passes 0); `settleMs`/`settleIntervalMs` - the in-attempt re-read budget and pace for a header-only page
93
+ * @returns {Promise<{ok: true, text: string, url: string}|{ok: false, error: string, attempts?: AttemptRecord[]}>} `error` is plain words, without a spend line
78
94
  */
79
- export async function fetchPageText(url, { connect, timeoutMs = PAGE_FETCH_TIMEOUT_MS, idleMs = NETWORK_IDLE_MS, idleTimeoutMs = NETWORK_IDLE_TIMEOUT_MS, closeWaitMs = 3000 } = {}) {
95
+ export async function fetchPageText(url, { connect, timeoutMs = PAGE_FETCH_TIMEOUT_MS, idleMs = NETWORK_IDLE_MS, idleTimeoutMs = NETWORK_IDLE_TIMEOUT_MS, closeWaitMs = 3000, retryWaitMs = RETRY_WAIT_MS, settleMs = SETTLE_BUDGET_MS, settleIntervalMs = SETTLE_INTERVAL_MS } = {}) {
80
96
  let u;
81
97
  try { u = new URL(url.trim()); } catch { return { ok: false, error: `${url} is not a web address bareloop can read` }; }
82
98
  if (u.protocol !== 'http:' && u.protocol !== 'https:') return { ok: false, error: `${url} is not an http or https address` };
@@ -88,6 +104,30 @@ export async function fetchPageText(url, { connect, timeoutMs = PAGE_FETCH_TIMEO
88
104
  else {
89
105
  try { open = /** @type {any} */ ((await import('barebrowse')).connect); } catch (e) { return { ok: false, error: `the page reader (barebrowse) could not be loaded: ${/** @type {Error} */ (e).message}` }; }
90
106
  }
107
+ /** @type {{ok: false, error: string}} */
108
+ let last = { ok: false, error: '' };
109
+ /** @type {AttemptRecord[]} */
110
+ const attempts = [];
111
+ for (let attempt = 1; attempt <= FETCH_ATTEMPTS; attempt += 1) {
112
+ const r = await attemptOnce(u, open, { timeoutMs, idleMs, idleTimeoutMs, closeWaitMs, settleMs, settleIntervalMs });
113
+ if (r.ok) return r;
114
+ if (!r.retry) return { ok: false, error: r.error, ...(attempts.length || r.record ? { attempts: r.record ? [...attempts, r.record] : attempts } : {}) };
115
+ if (r.record) attempts.push(r.record);
116
+ last = { ok: false, error: r.error };
117
+ if (attempt < FETCH_ATTEMPTS && retryWaitMs > 0) await new Promise((res) => setTimeout(res, retryWaitMs));
118
+ }
119
+ return { ok: false, error: `${last.error} (tried ${FETCH_ATTEMPTS} times)`, ...(attempts.length ? { attempts } : {}) };
120
+ }
121
+
122
+ /**
123
+ * ONE attempt: opens its own browser and page and always closes them. `retry: true` marks the two endings worth another go (header-only, timeout).
124
+ * @param {URL} u @param {(opts: Record<string, any>) => Promise<FetchPage>} open
125
+ * @param {{timeoutMs: number, idleMs: number, idleTimeoutMs: number, closeWaitMs: number, settleMs: number, settleIntervalMs: number}} o
126
+ * @returns {Promise<{ok: true, text: string, url: string}|{ok: false, error: string, retry: boolean, record?: AttemptRecord}>}
127
+ */
128
+ async function attemptOnce(u, open, { timeoutMs, idleMs, idleTimeoutMs, closeWaitMs, settleMs, settleIntervalMs }) {
129
+ const t0 = Date.now();
130
+ let stopped = false;
91
131
  /** @type {NodeJS.Timeout|undefined} */
92
132
  let timer;
93
133
  /** @type {Promise<FetchPage>|undefined} */
@@ -101,19 +141,31 @@ export async function fetchPageText(url, { connect, timeoutMs = PAGE_FETCH_TIMEO
101
141
  await p.goto(u.href, GOTO_TIMEOUT_MS);
102
142
  // bounded: a page that never goes quiet (polling, streaming) is read as it stands - the wait is for a page that is still filling
103
143
  try { await p.waitForNetworkIdle({ idle: idleMs, timeout: idleTimeoutMs }); } catch { /* read what is there */ }
104
- return p.snapshot({ ...SNAPSHOT_OPTIONS });
144
+ let snap = await p.snapshot({ ...SNAPSHOT_OPTIONS });
145
+ // header-only: the page's scripts may still be filling it (network idle came early). Keep the page open and re-read it, up to settleMs;
146
+ // only then does this attempt count as header-only. The attempt's own deadline still bounds all of it.
147
+ const settleEnd = Date.now() + settleMs;
148
+ while (!stopped && Date.now() < settleEnd && isHeaderOnly(snap)) {
149
+ await new Promise((res) => setTimeout(res, Math.min(settleIntervalMs, Math.max(0, settleEnd - Date.now()))));
150
+ if (stopped) break;
151
+ snap = await p.snapshot({ ...SNAPSHOT_OPTIONS });
152
+ }
153
+ return snap;
105
154
  })(),
106
- new Promise((_, reject) => { timer = setTimeout(() => reject(new Error(`no answer inside ${Math.round(timeoutMs / 1000)} seconds`)), timeoutMs); }),
155
+ new Promise((_, reject) => { timer = setTimeout(() => reject(Object.assign(new Error(`no answer inside ${Math.round(timeoutMs / 1000)} seconds`), { deadline: true })), timeoutMs); }),
107
156
  ]);
108
- if (typeof text !== 'string') return { ok: false, error: `${u.href} did not come back as text` };
109
- if (/\[BOT CHALLENGE DETECTED\]/.test(text)) return { ok: false, error: `${u.href} asked for a human check (a bot challenge), so the page could not be read` };
157
+ if (typeof text !== 'string') return { ok: false, retry: false, error: `${u.href} did not come back as text` };
158
+ if (/\[BOT CHALLENGE DETECTED\]/.test(text)) return { ok: false, retry: false, error: `${u.href} asked for a human check (a bot challenge), so the page could not be read` };
110
159
  const size = text.trim().length;
111
- if (size < MIN_PAGE_CHARS) return { ok: false, error: `${u.href} came back with ${size === 0 ? 'no text' : `only ${size} characters of text`} (less than ${MIN_PAGE_CHARS}) - the page did not load, or holds nothing to read` };
112
- if (pageContentChars(text) < MIN_CONTENT_CHARS) return { ok: false, error: `${u.href} came back with only the site's own header and menus - the page did not load its content, so there is nothing to read` };
160
+ if (size < MIN_PAGE_CHARS) return { ok: false, retry: false, error: `${u.href} came back with ${size === 0 ? 'no text' : `only ${size} characters of text`} (less than ${MIN_PAGE_CHARS}) - the page did not load, or holds nothing to read` };
161
+ if (pageContentChars(text) < MIN_CONTENT_CHARS) return { ok: false, retry: true, error: `${u.href} came back with only the site's own header and menus - the page did not load its content, so there is nothing to read`, record: headerRecord(text, Date.now() - t0) };
113
162
  return { ok: true, text, url: u.href };
114
163
  } catch (e) {
115
- return { ok: false, error: `${u.href} could not be read: ${/** @type {Error} */ (e)?.message ?? String(e)}` };
164
+ const msg = /** @type {Error} */ (e)?.message ?? String(e);
165
+ const timedOut = /** @type {any} */ (e)?.deadline === true || /time[d ]?-?out/i.test(msg);
166
+ return { ok: false, retry: timedOut, error: `${u.href} could not be read: ${msg}` };
116
167
  } finally {
168
+ stopped = true;
117
169
  if (timer !== undefined) clearTimeout(timer);
118
170
  // always close the browser, also when the deadline fired while it was still starting or loading
119
171
  if (opening) {
@@ -124,3 +176,14 @@ export async function fetchPageText(url, { connect, timeoutMs = PAGE_FETCH_TIMEO
124
176
  }
125
177
  }
126
178
  }
179
+
180
+ /** @param {unknown} text @returns {boolean} text long enough to pass the near-empty floor, yet with no content outside the site's own chrome (the one case worth re-reading) */
181
+ function isHeaderOnly(text) {
182
+ return typeof text === 'string' && !/\[BOT CHALLENGE DETECTED\]/.test(text) && text.trim().length >= MIN_PAGE_CHARS && pageContentChars(text) < MIN_CONTENT_CHARS;
183
+ }
184
+
185
+ /** the short failure record of a header-only attempt; the head is secret-masked (the log is append-only) @param {string} text @param {number} ms @returns {AttemptRecord} */
186
+ function headerRecord(text, ms) {
187
+ const body = text.split('\n').filter((l) => !/^url: /.test(l) && !/^[\d,]+ chars → /.test(l) && !/^hint: /.test(l)).join(' ').replace(/\s+/g, ' ').trim();
188
+ return { chars: pageContentChars(text), ms, head: redactSecrets(body.slice(0, HEAD_KEEP_CHARS)) };
189
+ }
package/src/pagemap.js CHANGED
@@ -16,7 +16,7 @@ import { AUTHOR_CALL_TIMEOUT_MS, AUTHOR_SCOUT_MAX_TOKENS, SURVEY_CAUSES } from '
16
16
  import { lineRules, boundInWords, wordsSaidOn } from './rubricstep.js';
17
17
  import { applyPageRules, validReading } from './pagerules.js';
18
18
  import { dryReadMoney, dryReadTimes, dryReadPhrases } from './pagedry.js';
19
- import { ruleWords } from './facts.js';
19
+ import { ruleWords, FACT_LANGS } from './facts.js';
20
20
  import { lookFactOf } from './inputlook.js';
21
21
 
22
22
  const require = createRequire(import.meta.url);
@@ -25,7 +25,9 @@ const { Loop } = require('bare-agent');
25
25
  /** the label of the one paid call, as the drafting book and the progress line see it */
26
26
  export const MAP_CALL_LABEL = 'page-map';
27
27
  /** the shapes the map may name; a `place` is read by code as a phrase */
28
- export const MAP_SHAPES = Object.freeze(['place', 'money', 'time', 'phrase']);
28
+ export const MAP_SHAPES = Object.freeze(['place', 'money', 'time', 'date', 'number', 'phrase']);
29
+ /** the languages a date's month names may be signed in (one list with src/facts.js) */
30
+ const MAP_LANGS = Object.keys(FACT_LANGS);
29
31
 
30
32
  export const MAP_SYSTEM = 'You map a person\'s job to the exact words of ONE saved web page. You never grade and never invent words: every string you return must be copied from the page text, character for character.';
31
33
 
@@ -40,12 +42,15 @@ export function mapPrompt({ jobLines, label, text }) {
40
42
  return `THE JOB (each ~ rule applies to the line above it):\n${job}\n\n`
41
43
  + 'A "result" is one item the page lists (one flight, one stay, one row). For each job line and for each rule, say which fact(s) every result must carry, and give the PAGE\'S OWN WORDS for it.\n\n'
42
44
  + 'Return ONLY a JSON object, no prose, no code fence:\n'
43
- + '{"facts":[{"name":"<letters and spaces only>","from":"<line N | line N rule M>","shape":"place|money|time|phrase","words":["<page words>", ...],"currency":["<symbol>"],"unit":["<words after the amount>"],"reading":<optional, see below>}]}\n\n'
45
+ + `{"facts":[{"name":"<letters and spaces only>","from":"<line N | line N rule M>","shape":"place|money|time|date|number|phrase","words":["<page words>", ...],"currency":["<symbol>"],"unit":["<words after the amount>"],"lang":"<${MAP_LANGS.join("|")}>","decimal":"<. or ,>","reading":<optional, see below>}]}\n\n`
44
46
  + 'shape meanings:\n'
45
47
  + '- place: words = ways the page writes the place (a city, an airport name), copied from the page.\n'
46
48
  + '- money: words = the label(s) written right BEFORE the amount, copied from the page. currency / unit = how the page writes the currency ("$") and/or the words after the amount ("US dollars"); give every spelling the page uses, as lists.\n'
47
49
  + '- time: words = the label(s) written right BEFORE the time that is the one the rule is about, copied from the page.\n'
50
+ + `- date: words = the label(s) written right BEFORE the date that is the one the rule is about, copied from the page. lang = the language the page writes the month name in (${MAP_LANGS.join(' ')}); leave lang out when the date is written 2026-10-26.\n`
51
+ + '- number: words = the label(s) written right BEFORE a plain number (a rating, a count), copied from the page. decimal = the mark the page writes before the decimals: "." or ",". Give decimal on a money fact too when the page writes a comma before the decimals.\n'
48
52
  + '- phrase: words = fixed words a result\'s text must contain, copied from the page.\n'
53
+ + 'A label must appear ONCE on its page line and sit right before the value; when the value stands alone on its line, give no label.\n'
49
54
  + 'Several words in one list are alternatives for the same fact. Put the best one first.\n'
50
55
  + 'reading (optional, only on a time or money fact): when a ~ rule is a limit on that fact that the page cannot hold in words (for example "not a red-eye" is a limit on the departure time), give your own best reading of the rule as that limit: {"op":"between","lo":<bound>,"hi":<bound>} or {"op":"lt|lte|gt|gte","value":<bound>}. A time bound is a "HH:MM" string on a 24-hour clock; a money bound is a number. Leave reading out when a page word already carries the rule.\n\n'
51
56
  + `PAGE TEXT (the input "${label}"):\n${text}`;
@@ -60,6 +65,7 @@ export function parseMapAnswer(text) {
60
65
  /** @type {any} */
61
66
  let m;
62
67
  try { m = JSON.parse(String(text ?? '').trim()); } catch { return { ok: false }; }
68
+ const notBlank = (/** @type {string} */ w) => w.trim() !== '';
63
69
  const strs = (/** @type {unknown} */ v) => Array.isArray(v) && v.every((x) => typeof x === 'string');
64
70
  if (m === null || typeof m !== 'object' || Array.isArray(m) || !Array.isArray(m.facts)) return { ok: false };
65
71
  for (const f of m.facts) {
@@ -67,6 +73,12 @@ export function parseMapAnswer(text) {
67
73
  if (typeof f.name !== 'string' || !MAP_SHAPES.includes(f.shape) || !strs(f.words)) return { ok: false };
68
74
  if (f.from !== undefined && typeof f.from !== 'string') return { ok: false };
69
75
  for (const k of ['currency', 'unit']) if (f[k] !== undefined && !strs(f[k])) return { ok: false };
76
+ if (f.lang !== undefined && !(typeof f.lang === 'string' && MAP_LANGS.includes(f.lang))) return { ok: false };
77
+ if (f.decimal !== undefined && !(f.decimal === '.' || f.decimal === ',')) return { ok: false };
78
+ // a blank entry (empty or only spaces) names nothing: it is left out here, with no note, and the rest of the fact stands. This is not a JSON
79
+ // repair - the answer was well-formed; a list left empty is absent (words stays a list, `[]`: the fact then reads by its signs alone)
80
+ f.words = f.words.filter(notBlank);
81
+ for (const k of ['currency', 'unit']) if (f[k] !== undefined) { f[k] = f[k].filter(notBlank); if (f[k].length === 0) delete f[k]; }
70
82
  // PRD v1.107 Part 5b: a `reading` is a limit of the signed-rule shape on a time or money fact; any other is a malformed answer (no repair)
71
83
  if (f.reading !== undefined && !validReading(f.shape, f.reading)) return { ok: false };
72
84
  }
package/src/pagerules.js CHANGED
@@ -10,11 +10,11 @@ import { normLabel } from './labelscan.js';
10
10
 
11
11
  /**
12
12
  * @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
13
- * @typedef {{name: string, from?: string, shape: string, words?: string[], currency?: string[], unit?: string[], reading?: Reading}} MapFact the AI's map, one fact
13
+ * @typedef {{name: string, from?: string, shape: string, words?: string[], currency?: string[], unit?: string[], reading?: Reading, lang?: string, decimal?: string}} MapFact the AI's map, one fact
14
14
  * @typedef {{facts: MapFact[]}} PageMap
15
- * @typedef {{line: number, name: string, shape: string, from: string, words: string[], unit?: string[], currency?: string[], forRule?: number, reading?: Reading}} KeptFact
15
+ * @typedef {{line: number, name: string, shape: string, from: string, words: string[], unit?: string[], currency?: string[], forRule?: number, reading?: Reading, lang?: string, decimal?: string}} KeptFact
16
16
  * `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)
17
- * @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), the phrase it looked for (phrase-no-result)
17
+ * @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 box a sign listed twice was kept in (sign-twice: unit or currency), the fact it would have repeated (S1), the phrase it looked for (phrase-no-result)
18
18
  */
19
19
 
20
20
  /** @param {unknown} v @returns {v is Record<string, any>} */
@@ -75,6 +75,8 @@ const READ = {
75
75
  money: /^\s*[$€£]?\s*(\d[\d,]*(?:\.\d+)?)/,
76
76
  time: /^\s*(\d{1,2}:\d{2}[\s\u202f\u00a0]?[AaPp][Mm])/,
77
77
  number: /^\s*(\d[\d,]*(?:\.\d+)?)/,
78
+ // R1 same-value only: the date words right after a label, up to the 4-digit year (a day, a month name, a year; or ISO)
79
+ date: /^\s*(\d{4}-\d{2}-\d{2}|\S+(?:\s\S+){0,3}\s\d{4})(?!\d)/,
78
80
  };
79
81
 
80
82
  /**
@@ -123,6 +125,21 @@ export function numberAtEnd(s) {
123
125
  const p = firstDigit(e);
124
126
  return p === -1 ? undefined : s.slice(p, e);
125
127
  }
128
+ /** a number as a page writes it where the decimal mark is a comma: groups of three joined by "." or a space (or none), then an optional comma and decimals */
129
+ export const NUM_COMMA = '\\d{1,3}(?:[. ]\\d{3})+(?:,\\d+)?|\\d+(?:,\\d+)?';
130
+ /**
131
+ * {@link numberAtEnd} for a comma-decimal page: the number written at the very end of `s` (only whitespace after it), as the page writes it
132
+ * ("1.057,65", "1 089", "48,49"). Only the last 24 characters are read (no price is longer), so a long run of digits cannot cost more than that.
133
+ * @param {string} s @returns {string|undefined}
134
+ */
135
+ export function numberAtEndComma(s) {
136
+ const t = s.trimEnd();
137
+ const tail = t.slice(-24);
138
+ const m = new RegExp(`(?<![\\d,.])(?:${NUM_COMMA})$`).exec(tail);
139
+ // a match that starts the tail while the text goes on to the left of it may be the end of a longer run: not a number of its own
140
+ if (!m || (m.index === 0 && t.length > tail.length && /[\d,.]/.test(t[t.length - tail.length - 1]))) return undefined;
141
+ return m[0];
142
+ }
126
143
  const AFTER = new RegExp(`^\\s*(?:[$€£]\\s*)?(${NUM})`);
127
144
  const AFTER_CUR = new RegExp(`^\\s*(${NUM})`);
128
145
  const stripCommas = (/** @type {string} */ s) => s.replace(/,/g, '');
@@ -159,8 +176,123 @@ const ORDINALS = ['first', 'second', 'third', 'fourth', 'fifth', 'sixth', 'seven
159
176
 
160
177
  /** 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 */
161
178
  const CURRENCY_SYMBOLS = ['$', '€', '£'];
179
+
180
+ /**
181
+ * The page FORMS of a currency sign in `lines`: `glued` = right after a number ("29,99€"), `spaced` = after a number and one space ("29,99 €"),
182
+ * `before` = in front of a number it does not itself follow ("€399", "€ 399").
183
+ * @param {string[]} lines @param {string} sign @returns {{glued: boolean, spaced: boolean, before: boolean}}
184
+ */
185
+ function signForms(lines, sign) {
186
+ let glued = false; let spaced = false; let before = false;
187
+ for (const [l, i] of occs(lines, sign)) {
188
+ const head = l.slice(0, i);
189
+ if (/\d$/.test(head)) glued = true;
190
+ else if (/\d $/.test(head)) spaced = true;
191
+ else if (/^ ?\d/.test(l.slice(i + sign.length))) before = true;
192
+ }
193
+ return { glued, spaced, before };
194
+ }
195
+ /** the char codes of the digits and of the "." and "," a number is written with */
196
+ const isDigitCode = (/** @type {number} */ c) => c >= 48 && c <= 57;
197
+ const isMarkCode = (/** @type {number} */ c) => c === 46 || c === 44;
198
+ /**
199
+ * The number that ends a string: its last run of digits, "." and ",", from the run's first digit (undefined when the run holds no digit). One
200
+ * backward scan, the linear form of /\d[\d.,]*$/ (that one re-scans a long digit run from every digit).
201
+ * @param {string} s @returns {string | undefined}
202
+ */
203
+ function numberBefore(s) {
204
+ let j = s.length;
205
+ while (j > 0 && (isDigitCode(s.charCodeAt(j - 1)) || isMarkCode(s.charCodeAt(j - 1)))) j--;
206
+ while (j < s.length && isMarkCode(s.charCodeAt(j))) j++;
207
+ return j < s.length ? s.slice(j) : undefined;
208
+ }
209
+ /** a number without the "." and "," marks that end it; the linear form of .replace(/[.,]+$/, '') (that one is quadratic on a long run of marks before a digit) @param {string | undefined} n */
210
+ function trimMarks(n) {
211
+ if (n === undefined) return undefined;
212
+ let e = n.length;
213
+ while (e > 0 && isMarkCode(n.charCodeAt(e - 1))) e--;
214
+ return n.slice(0, e);
215
+ }
216
+ /**
217
+ * How the page writes the decimals of the amounts a money fact's signs touch (a sign right after a number, spaced or glued, or right before one).
218
+ * A CLEAR comma amount ends in a comma and exactly two digits ("1.058,47", "17,00"), a CLEAR dot amount in a dot and exactly two ("29.99"); a comma or
219
+ * dot and exactly three digits ("1,299", "1.299") is ambiguous and counts as neither. Only one kind seen = that mark; both or neither = undefined.
220
+ * @param {string[]} lines @param {string[]} signs @returns {',' | '.' | undefined}
221
+ */
222
+ function pageDecimal(lines, signs) {
223
+ let comma = false; let dot = false;
224
+ for (const sgn of signs) {
225
+ for (const [l, i] of occs(lines, sgn)) {
226
+ const before = numberBefore(l.slice(0, i).replace(/ $/, ''));
227
+ const after = trimMarks(/^ ?(\d[\d.,]*)/.exec(l.slice(i + sgn.length))?.[1]);
228
+ for (const n of [before, after]) {
229
+ if (n === undefined) continue;
230
+ if (/,\d{2}$/.test(n)) comma = true;
231
+ else if (/\.\d{2}$/.test(n)) dot = true;
232
+ }
233
+ }
234
+ }
235
+ return comma === dot ? undefined : comma ? ',' : '.';
236
+ }
162
237
  /** the shapes that read a value after a label */
163
- const VALUE_SHAPES = ['money', 'time', 'number'];
238
+ const VALUE_SHAPES = ['money', 'time', 'number', 'date'];
239
+
240
+ /** the full month names of each language a date may be signed in (bareguard 0.27 reads these and short forms; one full name on the page proves the language is the page's) */
241
+ const MONTHS_FULL = /** @type {Record<string, string[]>} */ ({
242
+ en: ['january', 'february', 'march', 'april', 'may', 'june', 'july', 'august', 'september', 'october', 'november', 'december'],
243
+ de: ['januar', 'februar', 'märz', 'april', 'mai', 'juni', 'juli', 'august', 'september', 'oktober', 'november', 'dezember'],
244
+ fr: ['janvier', 'février', 'mars', 'avril', 'mai', 'juin', 'juillet', 'août', 'septembre', 'octobre', 'novembre', 'décembre'],
245
+ es: ['enero', 'febrero', 'marzo', 'abril', 'mayo', 'junio', 'julio', 'agosto', 'septiembre', 'octubre', 'noviembre', 'diciembre'],
246
+ it: ['gennaio', 'febbraio', 'marzo', 'aprile', 'maggio', 'giugno', 'luglio', 'agosto', 'settembre', 'ottobre', 'novembre', 'dicembre'],
247
+ pt: ['janeiro', 'fevereiro', 'março', 'abril', 'maio', 'junho', 'julho', 'agosto', 'setembro', 'outubro', 'novembro', 'dezembro'],
248
+ nl: ['januari', 'februari', 'maart', 'april', 'mei', 'juni', 'juli', 'augustus', 'september', 'oktober', 'november', 'december'],
249
+ });
250
+ /**
251
+ * Does the text hold a comma-decimal amount: a number that stands alone (no digit, comma or point before or after it), its whole part one run of
252
+ * digits or groups of three joined by "." or a space, then a comma and one or two decimals. "133,330 chars" (a thousands-grouped count) is not one.
253
+ * The linear form of /(?<![\d,.])\d{1,3}(?:[. ]\d{3})*,\d{1,2}(?![\d,]|\.\d)|(?<![\d,.])\d+,\d{1,2}(?![\d,]|\.\d)/ (that one re-scans a long
254
+ * run of groups from every group). A start is a digit with no digit, comma or point before it; its digit run is the whole part, or the first group
255
+ * when the run is up to three digits and groups of three follow, and only the end of that chain can carry the comma.
256
+ * @param {string} s @returns {boolean}
257
+ */
258
+ function hasCommaDecimal(s) {
259
+ const n = s.length;
260
+ const digitsAt = (/** @type {number} */ q) => { let e = q; while (e < n && isDigitCode(s.charCodeAt(e))) e++; return e; };
261
+ const decimals = (/** @type {number} */ q) => {
262
+ if (s[q] !== ',') return false;
263
+ let e = q + 1;
264
+ while (e < q + 4 && isDigitCode(s.charCodeAt(e))) e++;
265
+ if (e - q - 1 < 1 || e - q - 1 > 2) return false;
266
+ return s[e] !== ',' && !(s[e] === '.' && isDigitCode(s.charCodeAt(e + 1)));
267
+ };
268
+ /** where the chain of ([. ]\d{3}) groups that starts at a position ends; each position is walked once @type {Map<number, number>} */
269
+ const chainEnd = new Map();
270
+ const endOf = (/** @type {number} */ from) => {
271
+ /** @type {number[]} */
272
+ const path = [];
273
+ let q = from; let end;
274
+ for (;;) {
275
+ const seen = chainEnd.get(q);
276
+ if (seen !== undefined) { end = seen; break; }
277
+ path.push(q);
278
+ if ((s[q] === '.' || s[q] === ' ') && isDigitCode(s.charCodeAt(q + 1)) && isDigitCode(s.charCodeAt(q + 2)) && isDigitCode(s.charCodeAt(q + 3))) q += 4;
279
+ else { end = q; break; }
280
+ }
281
+ for (const x of path) chainEnd.set(x, end);
282
+ return end;
283
+ };
284
+ for (let p = 0; p < n;) {
285
+ if (!isDigitCode(s.charCodeAt(p))) { p++; continue; }
286
+ const d = digitsAt(p);
287
+ const prev = p === 0 ? 0 : s.charCodeAt(p - 1);
288
+ if (p === 0 || !(isDigitCode(prev) || isMarkCode(prev))) {
289
+ if (decimals(d)) return true;
290
+ if (d - p <= 3 && decimals(endOf(d))) return true;
291
+ }
292
+ p = d;
293
+ }
294
+ return false;
295
+ }
164
296
 
165
297
  /**
166
298
  * 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.
@@ -180,13 +312,26 @@ export function applyPageRules(text, mapping, jobLines) {
180
312
  };
181
313
 
182
314
  // ---- S1, name part: a fact whose name counts results is refused before anything else reads it ----
183
- const list = all.filter((f) => {
184
- const hit = String(f.name ?? '').toLowerCase().split(/[^a-z]+/).find((w) => ORDINALS.includes(w));
315
+ const named = all.filter((f) => {
316
+ // "last" directly followed by a word ending in "ed" ("last updated", "last modified") names WHEN a thing changed, not which result: it is not a counting name
317
+ const hit = String(f.name ?? '').toLowerCase().split(/[^a-z]+/).find((w, i, ws) => ORDINALS.includes(w) && !(w === 'last' && /ed$/.test(ws[i + 1] ?? '')));
185
318
  if (hit === undefined) return true;
186
319
  drop(f, undefined, 's1-name');
187
320
  return false;
188
321
  });
189
322
 
323
+ // ---- a signed `lang` / `decimal` must really be on the page, else the whole fact is Not checked: a date in a language the page does not
324
+ // write, or a decimal comma on a page with no comma-decimal amount, could only ever read nothing (or the wrong thing). A field on a shape that
325
+ // does not take it (lang off a date, decimal off a number or money) is not carried; the rest of the fact stands. ----
326
+ const list = named.filter((f) => {
327
+ if (f.shape === 'date' && f.lang !== undefined) {
328
+ const months = Object.hasOwn(MONTHS_FULL, String(f.lang)) ? MONTHS_FULL[String(f.lang)] : [];
329
+ if (!months.some((m) => countPhrase(text, m) >= 1)) { drop(f, undefined, 'lang-not-on-page', String(f.lang)); return false; }
330
+ }
331
+ if ((f.shape === 'number' || f.shape === 'money') && f.decimal === ',' && !hasCommaDecimal(squash(text))) { drop(f, undefined, 'decimal-not-on-page', ','); return false; }
332
+ return true;
333
+ });
334
+
190
335
  // ---- found-on-page, then U1 and R1L for the facts that read an amount ----
191
336
  const lines = linesOf(text);
192
337
  /** @type {{f: MapFact, kept: string[], cur: string[], uni: string[], first?: string|null, anchorless?: boolean}[]} */
@@ -223,39 +368,68 @@ export function applyPageRules(text, mapping, jobLines) {
223
368
  /** @type {string[]} */ let uni = [];
224
369
  /** @type {string[]} */ let cur = [];
225
370
  if (f.shape === 'money') {
226
- // the forms: units first (the U1-moved words, then the AI's listed units), then currency signs. SORT, never drop for the box (money
227
- // 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
228
- // form the AI listed in either box is pooled and moved to the box its own shape belongs to
371
+ // the forms: units first (the U1-moved words, then the AI's listed units), then currency signs. SORT, never drop for the box: the box is
372
+ // position only. A WORD is a unit. A currency SIGN is filed by how the PAGE writes it next to the number (bareguard 0.27, ruling #68): glued
373
+ // to the number, after it or before it, is a currency; spaced after it is a unit; both boxes when the page writes both. The box the AI
374
+ // listed it in does not matter, and a sign listed in both boxes whose page form needs only one is said once ('sign-twice').
229
375
  const listedCurrencies = (Array.isArray(f.currency) ? f.currency : []).map(String);
230
- // a currency SIGN listed in BOTH unit and currency is said once: the unit copy goes (fwd F71 drafts)
231
376
  // bareguard 0.26: two units equal after whitespace normalization are one form, so trim + collapse here and the twins collapse in the pool
232
- const listedUnits = (Array.isArray(f.unit) ? f.unit : []).map((u) => String(u).replace(/\s+/g, ' ').trim()).filter((u) => {
233
- if (!(CURRENCY_SYMBOLS.includes(u) && listedCurrencies.includes(u))) return true;
234
- drop(f, u, 'sign-twice');
235
- return false;
236
- });
377
+ const listedUnits = (Array.isArray(f.unit) ? f.unit : []).map((u) => String(u).replace(/\s+/g, ' ').trim());
378
+ const plainUnits = listedUnits.filter((u) => !CURRENCY_SYMBOLS.includes(u));
379
+ const plainCurrencies = listedCurrencies.filter((c) => !CURRENCY_SYMBOLS.includes(c));
380
+ // the lines that hold this fact's label are where its sign sits next to ITS number; with no label (U1 moved it, or none) the whole page
381
+ const scope = labels.length ? lines.filter((l) => labels.some((w) => l.includes(w))) : lines;
382
+ /** @type {string[]} */ const signCur = [];
383
+ /** @type {string[]} */ const signUni = [];
384
+ for (const sgn of new Set([...listedCurrencies, ...listedUnits].filter((x) => CURRENCY_SYMBOLS.includes(x)))) {
385
+ let fm = signForms(scope, sgn);
386
+ if (!fm.glued && !fm.spaced && !fm.before) fm = signForms(lines, sgn);
387
+ const asCur = fm.glued || fm.before;
388
+ const asUni = fm.spaced;
389
+ if (asCur || !asUni) signCur.push(sgn); // a sign with no number next to it stays a currency: the not-on-page / reads-no-result rules speak
390
+ if (asUni) signUni.push(sgn);
391
+ if (listedCurrencies.includes(sgn) && listedUnits.includes(sgn) && !(asCur && asUni)) drop(f, sgn, 'sign-twice', asUni ? 'unit' : 'currency');
392
+ }
237
393
  // a WORD listed in both boxes is one form, said once (a record of the double listing is kept, as R1L always said it)
238
- for (const w of new Set(listedCurrencies.filter((c) => !CURRENCY_SYMBOLS.includes(c) && listedUnits.includes(c)))) drop(f, w, 'r1l', w);
239
- const pool = [...moved, ...listedUnits, ...listedCurrencies];
240
- const units = [...new Set(pool.filter((w) => !CURRENCY_SYMBOLS.includes(w)))].filter((u) => {
241
- if (countPhrase(text, u) >= 1) return true;
394
+ for (const w of new Set(plainCurrencies.filter((c) => plainUnits.includes(c)))) drop(f, w, 'r1l', w);
395
+ const units = [...new Set([...moved, ...plainUnits, ...plainCurrencies, ...signUni])].filter((u) => {
396
+ if (CURRENCY_SYMBOLS.includes(u) ? countExact(text, u) >= 1 : countPhrase(text, u) >= 1) return true;
242
397
  drop(f, u, 'not-on-page');
243
398
  return false;
244
399
  });
245
- const curs = [...new Set(pool.filter((w) => CURRENCY_SYMBOLS.includes(w)))].filter((c) => {
400
+ const curs = [...new Set(signCur)].filter((c) => {
246
401
  if (countExact(text, c) >= 1) return true;
247
402
  drop(f, c, 'not-on-page');
248
403
  return false;
249
404
  });
250
405
  /** @type {{kind: 'unit'|'currency', w: string}[]} */
251
406
  const forms = [...units.map((u) => /** @type {const} */ ({ kind: 'unit', w: u })), ...curs.map((c) => /** @type {const} */ ({ kind: 'currency', w: c }))];
407
+ // A page read where the form is written (see R1L): a sign after its number reads the number before it, before its number the one after
408
+ const read = (/** @type {{kind: string, w: string}} */ x) => {
409
+ if (!CURRENCY_SYMBOLS.includes(x.w)) return x.kind === 'unit' ? unitSet(lines, x.w) : curSet(lines, x.w);
410
+ const fm = signForms(lines, x.w);
411
+ return x.kind === 'unit' || fm.glued || !fm.before ? unitSet(lines, x.w) : curSet(lines, x.w);
412
+ };
413
+ // a form that reads NO amount anywhere on the page ("ohne MwSt." alone on its own line) is dropped BEFORE R1L, so R1L never takes it as the
414
+ // reference and then drops the form that does read. When no form reads anything, nothing is dropped here and the later rules speak.
415
+ if (forms.some((x) => read(x).size > 0)) {
416
+ for (let i = forms.length - 1; i >= 0; i -= 1) {
417
+ if (read(forms[i]).size > 0) continue;
418
+ drop(f, forms[i].w, 'reads-no-amount');
419
+ forms.splice(i, 1);
420
+ }
421
+ }
252
422
  if (forms.length) {
253
- // R1L: every form reads the same set of numbers as the primary (the first form); one that reads more or fewer is dropped
254
- const read = (/** @type {{kind: string, w: string}} */ x) => (x.kind === 'unit' ? unitSet(lines, x.w) : curSet(lines, x.w));
423
+ // R1L: every form reads the same set of numbers as the primary (the first form that reads); one that reads more or fewer is dropped.
255
424
  const ps = read(forms[0]);
256
425
  const keep = [forms[0]];
257
- // 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)
258
- 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); }
426
+ // one SIGN filed in both boxes by its page forms is ONE symbol read in two forms (bareguard 0.27 counts the same amount as one price): it is
427
+ // never compared with itself
428
+ for (const x of forms.slice(1)) {
429
+ if (CURRENCY_SYMBOLS.includes(x.w) && x.w === forms[0].w) keep.push(x);
430
+ else if (x.w !== forms[0].w && sameSet(read(x), ps)) keep.push(x);
431
+ else drop(f, x.w, 'r1l', forms[0].w);
432
+ }
259
433
  uni = keep.filter((x) => x.kind === 'unit').map((x) => x.w);
260
434
  cur = keep.filter((x) => x.kind === 'currency').map((x) => x.w);
261
435
  }
@@ -309,6 +483,15 @@ export function applyPageRules(text, mapping, jobLines) {
309
483
  if (p.uni.length) out.unit = [...p.uni];
310
484
  if (p.cur.length) out.currency = [...p.cur];
311
485
  }
486
+ if (p.f.shape === 'date' && p.f.lang !== undefined) out.lang = String(p.f.lang);
487
+ if ((p.f.shape === 'number' || p.f.shape === 'money') && p.f.decimal !== undefined) out.decimal = String(p.f.decimal);
488
+ // CODE picks the decimal mark of a money fact from how the page writes the amounts its signs touch, over the AI's word when the page is clear
489
+ // (a "1.058,47 €" page read with "." would read wrong); both kinds or neither, the AI's word stands. A clear dot page keeps what was signed
490
+ // ("." is the reader's default, so no field is added).
491
+ if (p.f.shape === 'money') {
492
+ const mark = pageDecimal(lines, [...(out.currency ?? []), ...(out.unit ?? [])]);
493
+ if (mark === ',') out.decimal = ',';
494
+ }
312
495
  if (t.rule !== undefined) out.forRule = t.rule;
313
496
  if (validReading(out.shape, p.f.reading)) out.reading = /** @type {Reading} */ ({ ...p.f.reading });
314
497
  return out;
@@ -776,6 +776,8 @@ export function createSession(card, deps = {}) {
776
776
  if (RUBRIC_FILES) {
777
777
  const proved = await proveRubricInputs({ inputs: INPUTS, into, destination: card.destination, ...(deps.fetchUrl ? { fetchUrl: deps.fetchUrl } : {}) });
778
778
  prep = proved.ok ? proved.prep : { stop: proved.error, code: proved.code };
779
+ // what each header-only page read came back with (already secret-masked at capture), so the next live failure shows it
780
+ if (!proved.ok && proved.attempts?.length) appendDraftLog(outDir, { kind: 'page-fetch-attempts', no: Math.max(0, state.steps.length - 1), attempts: proved.attempts, at: new Date().toISOString() });
779
781
  } else {
780
782
  prep = await prepareSource({
781
783
  source: SOURCE, into, ...(isRepoLike || destIsDir ? { destination: card.destination } : {}),
@@ -5248,8 +5248,8 @@
5248
5248
  fitCardBoxes();
5249
5249
  }
5250
5250
  msgInput.addEventListener("input", renderMain);
5251
- // P7 — the numbered boxes. A plain job line is numbered 1, 2, 3; a line starting `~` is a rule for the line above and has no
5252
- // number; Inputs numbers every line; empty lines have none. A hidden mirror (same font/width/padding) measures each logical
5251
+ // P7 — the numbered boxes. A plain job line is numbered 1, 2, 3; a line starting `~` is a rule for the line above: it shows `~` in
5252
+ // the gutter and takes no number; Inputs numbers every line; empty lines have none. A hidden mirror (same font/width/padding) measures each logical
5253
5253
  // line's wrapped height so every number stays level with its own line — the same rules src/jobblock.js parses by.
5254
5254
  var NUMBERED_IDS = ["jf-job", "jf-inputs"];
5255
5255
  var FIT_IDS = ["jf-job", "jf-inputs", "jf-dest"];
@@ -5261,7 +5261,8 @@
5261
5261
  var n = 0;
5262
5262
  var nums = lines.map(function(l){
5263
5263
  var t = l.trim();
5264
- if(t === "" || (isJob && t.charAt(0) === "~")) return "";
5264
+ if(t === "") return "";
5265
+ if(isJob && t.charAt(0) === "~") return "~";
5265
5266
  n += 1; return String(n);
5266
5267
  });
5267
5268
  mir.style.width = ta.clientWidth ? ta.clientWidth + "px" : "";