@bendyline/gezel-sdk 1.0.9 → 1.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +28 -1
- package/dist/checks.d.ts +84 -2
- package/dist/checks.js +1352 -852
- package/dist/index.d.ts +5 -711
- package/dist/index.js +102 -91
- package/dist/portable.d.ts +742 -0
- package/dist/portable.js +102 -0
- package/dist/{types-DCBb1goX.d.ts → types-rOo0kM35.d.ts} +1 -1
- package/package.json +6 -2
package/dist/checks.js
CHANGED
|
@@ -1,33 +1,3 @@
|
|
|
1
|
-
// ../core/src/checks/workspace-exists.ts
|
|
2
|
-
function createCitedPathChecker(ws) {
|
|
3
|
-
let listing = null;
|
|
4
|
-
const probes = /* @__PURE__ */ new Map();
|
|
5
|
-
const loadListing = () => {
|
|
6
|
-
listing ??= ws.list().then((files) => new Set(files.map((f) => citedPathKey(f))));
|
|
7
|
-
return listing;
|
|
8
|
-
};
|
|
9
|
-
return async (cited) => {
|
|
10
|
-
const probe = cleanCitedPath(cited);
|
|
11
|
-
if (!probe) return false;
|
|
12
|
-
if ((await loadListing()).has(probe.toLowerCase())) return true;
|
|
13
|
-
let hit = probes.get(probe);
|
|
14
|
-
if (!hit) {
|
|
15
|
-
hit = ws.read(probe).then(
|
|
16
|
-
(content) => content !== null,
|
|
17
|
-
() => false
|
|
18
|
-
);
|
|
19
|
-
probes.set(probe, hit);
|
|
20
|
-
}
|
|
21
|
-
return hit;
|
|
22
|
-
};
|
|
23
|
-
}
|
|
24
|
-
function cleanCitedPath(p) {
|
|
25
|
-
return p.trim().replace(/^`+|`+$/g, "").replace(/[:#].*$/, "").replace(/^\.\//, "").replace(/^\/+/, "").replace(/^workspace\//i, "");
|
|
26
|
-
}
|
|
27
|
-
function citedPathKey(p) {
|
|
28
|
-
return cleanCitedPath(p).toLowerCase();
|
|
29
|
-
}
|
|
30
|
-
|
|
31
1
|
// ../core/src/checks/html.ts
|
|
32
2
|
var MIN_INLINE_JS_BYTES = 2048;
|
|
33
3
|
var SCRIPT_RE = /<script\b([^>]*)>([\s\S]*?)<\/script\s*>/gi;
|
|
@@ -134,109 +104,698 @@ function htmlGameSniff(html, minJsBytes = 400) {
|
|
|
134
104
|
return hasSurface && scriptClosed && jsSubstantial;
|
|
135
105
|
}
|
|
136
106
|
|
|
137
|
-
// ../core/src/checks/
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
107
|
+
// ../core/src/checks/records.ts
|
|
108
|
+
var ISO_DATE_RE = /^\d{4}-\d{2}-\d{2}$/;
|
|
109
|
+
function isRealIsoDate(value) {
|
|
110
|
+
if (!ISO_DATE_RE.test(value)) return false;
|
|
111
|
+
const parts = value.split("-").map(Number);
|
|
112
|
+
const y = parts[0] ?? 0;
|
|
113
|
+
const m = parts[1] ?? 0;
|
|
114
|
+
const d = parts[2] ?? 0;
|
|
115
|
+
const dt = new Date(Date.UTC(y, m - 1, d));
|
|
116
|
+
return dt.getUTCFullYear() === y && dt.getUTCMonth() === m - 1 && dt.getUTCDate() === d;
|
|
142
117
|
}
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
118
|
+
function checkCellType(value, type) {
|
|
119
|
+
switch (type) {
|
|
120
|
+
case "string":
|
|
121
|
+
case "nonempty":
|
|
122
|
+
return value.length > 0 ? { ok: true, detail: "" } : { ok: false, detail: "empty value" };
|
|
123
|
+
case "number":
|
|
124
|
+
return /^-?\d+(\.\d+)?$/.test(value) ? { ok: true, detail: "" } : { ok: false, detail: `"${value}" is not a number` };
|
|
125
|
+
case "integer":
|
|
126
|
+
return /^-?\d+$/.test(value) ? { ok: true, detail: "" } : { ok: false, detail: `"${value}" is not an integer` };
|
|
127
|
+
case "boolean":
|
|
128
|
+
return /^(true|false)$/i.test(value) ? { ok: true, detail: "" } : { ok: false, detail: `"${value}" is not a boolean` };
|
|
129
|
+
case "date":
|
|
130
|
+
case "iso-date":
|
|
131
|
+
return isRealIsoDate(value) ? { ok: true, detail: "" } : { ok: false, detail: `"${value}" is not an ISO yyyy-mm-dd date` };
|
|
132
|
+
case "email":
|
|
133
|
+
return /^[^\s@]+@[^\s@]+\.[^\s@]+$/.test(value) ? { ok: true, detail: "" } : { ok: false, detail: `"${value}" is not an email` };
|
|
134
|
+
default: {
|
|
135
|
+
let re = null;
|
|
136
|
+
try {
|
|
137
|
+
re = new RegExp(type);
|
|
138
|
+
} catch {
|
|
139
|
+
return { ok: false, detail: `invalid column type /${type}/` };
|
|
140
|
+
}
|
|
141
|
+
return re.test(value) ? { ok: true, detail: "" } : { ok: false, detail: `"${value}" does not match /${type}/` };
|
|
142
|
+
}
|
|
143
|
+
}
|
|
147
144
|
}
|
|
148
|
-
|
|
149
|
-
let
|
|
150
|
-
|
|
151
|
-
|
|
145
|
+
function splitTableRow(line) {
|
|
146
|
+
let s = line.trim();
|
|
147
|
+
if (s.startsWith("|")) s = s.slice(1);
|
|
148
|
+
if (s.endsWith("|")) s = s.slice(0, -1);
|
|
149
|
+
return s.split(/(?<!\\)\|/).map((c) => c.trim().replace(/\\\|/g, "|"));
|
|
152
150
|
}
|
|
153
|
-
function
|
|
154
|
-
if (
|
|
155
|
-
const
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
|
|
151
|
+
function isDelimiterRow(line) {
|
|
152
|
+
if (!line.includes("-")) return false;
|
|
153
|
+
const cells = splitTableRow(line);
|
|
154
|
+
return cells.length > 0 && cells.every((c) => /^:?-{1,}:?$/.test(c.trim()));
|
|
155
|
+
}
|
|
156
|
+
function parseMarkdownTable(text) {
|
|
157
|
+
const lines = text.split("\n");
|
|
158
|
+
for (let i = 0; i < lines.length - 1; i++) {
|
|
159
|
+
const header = lines[i];
|
|
160
|
+
const delim = lines[i + 1];
|
|
161
|
+
if (!header || !header.includes("|") || !delim || !isDelimiterRow(delim)) continue;
|
|
162
|
+
const headers = splitTableRow(header);
|
|
163
|
+
if (headers.length === 0) continue;
|
|
164
|
+
const rows = [];
|
|
165
|
+
for (let j = i + 2; j < lines.length; j++) {
|
|
166
|
+
const l = lines[j];
|
|
167
|
+
if (!l || l.trim() === "" || !l.includes("|")) break;
|
|
168
|
+
rows.push(splitTableRow(l));
|
|
162
169
|
}
|
|
163
|
-
return
|
|
164
|
-
}
|
|
165
|
-
|
|
166
|
-
const pngSignature = [137, 80, 78, 71, 13, 10, 26, 10].every(
|
|
167
|
-
(value, index) => bytes[index] === value
|
|
168
|
-
);
|
|
169
|
-
const png = pngSignature && ascii(12, 16) === "IHDR" && containsAscii("IDAT") && containsAscii("IEND");
|
|
170
|
-
const jpeg = bytes[0] === 255 && bytes[1] === 216 && bytes[2] === 255 && bytes[bytes.length - 2] === 255 && bytes[bytes.length - 1] === 217;
|
|
171
|
-
const gif = (ascii(0, 6) === "GIF87a" || ascii(0, 6) === "GIF89a") && bytes.includes(44) && bytes[bytes.length - 1] === 59;
|
|
172
|
-
const webp = ascii(0, 4) === "RIFF" && ascii(8, 12) === "WEBP" && ["VP8 ", "VP8L", "VP8X"].includes(ascii(12, 16));
|
|
173
|
-
return bytes.length >= 1024 && (png || jpeg || gif || webp);
|
|
170
|
+
return { headers, rows };
|
|
171
|
+
}
|
|
172
|
+
return null;
|
|
174
173
|
}
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
|
|
178
|
-
|
|
179
|
-
|
|
180
|
-
|
|
181
|
-
|
|
182
|
-
|
|
183
|
-
|
|
184
|
-
}
|
|
185
|
-
|
|
186
|
-
|
|
187
|
-
|
|
188
|
-
if (!
|
|
189
|
-
|
|
190
|
-
|
|
191
|
-
|
|
192
|
-
|
|
193
|
-
|
|
194
|
-
if (!RASTER_EXTS.has(fileExt)) return true;
|
|
195
|
-
const bytes = await readBytes(p).catch(() => null);
|
|
196
|
-
return bytes !== null && isValidRasterAsset(bytes);
|
|
197
|
-
})
|
|
198
|
-
);
|
|
199
|
-
matched = matched.filter((_, i) => verdicts[i]);
|
|
174
|
+
function tableShape(text, spec) {
|
|
175
|
+
const table = parseMarkdownTable(text);
|
|
176
|
+
if (!table) {
|
|
177
|
+
return {
|
|
178
|
+
ok: false,
|
|
179
|
+
detail: 'no Markdown table found \u2014 provide a "| header | \u2026 |" row with a "|---|" delimiter.',
|
|
180
|
+
headers: [],
|
|
181
|
+
rowCount: 0
|
|
182
|
+
};
|
|
183
|
+
}
|
|
184
|
+
const headersLower = table.headers.map((h) => h.toLowerCase());
|
|
185
|
+
const base = { headers: table.headers, rowCount: table.rows.length };
|
|
186
|
+
for (const col of spec.requiredColumns ?? []) {
|
|
187
|
+
if (!headersLower.includes(col.toLowerCase())) {
|
|
188
|
+
return {
|
|
189
|
+
ok: false,
|
|
190
|
+
detail: `table is missing required column "${col}" \u2014 found: ${table.headers.join(", ")}.`,
|
|
191
|
+
...base
|
|
192
|
+
};
|
|
200
193
|
}
|
|
201
194
|
}
|
|
202
|
-
|
|
195
|
+
const minRows = spec.minRows ?? 0;
|
|
196
|
+
if (table.rows.length < minRows) {
|
|
203
197
|
return {
|
|
204
198
|
ok: false,
|
|
205
|
-
detail: `
|
|
206
|
-
|
|
199
|
+
detail: `table has ${table.rows.length} row(s), need \u2265 ${minRows}.`,
|
|
200
|
+
...base
|
|
207
201
|
};
|
|
208
202
|
}
|
|
209
|
-
const
|
|
210
|
-
|
|
211
|
-
|
|
212
|
-
|
|
203
|
+
for (const [col, type] of Object.entries(spec.columnTypes ?? {})) {
|
|
204
|
+
const idx = headersLower.indexOf(col.toLowerCase());
|
|
205
|
+
if (idx === -1) {
|
|
206
|
+
return { ok: false, detail: `column-type check names unknown column "${col}".`, ...base };
|
|
207
|
+
}
|
|
208
|
+
for (let r = 0; r < table.rows.length; r++) {
|
|
209
|
+
const cell = (table.rows[r]?.[idx] ?? "").trim();
|
|
210
|
+
const v = checkCellType(cell, type);
|
|
211
|
+
if (!v.ok) {
|
|
212
|
+
return { ok: false, detail: `row ${r + 1} column "${col}": ${v.detail}`, ...base };
|
|
213
|
+
}
|
|
214
|
+
}
|
|
215
|
+
}
|
|
216
|
+
return {
|
|
213
217
|
ok: true,
|
|
214
|
-
detail: `
|
|
215
|
-
|
|
216
|
-
} : {
|
|
217
|
-
ok: false,
|
|
218
|
-
detail: `found ${matched.length}${verified} ${ext.join("/")} file(s)${where}, need \u2265 ${min} \u2014 create the missing ${ext[0] ?? ""} file(s)${where ? ` under ${dir.replace(/\/+$/, "")}/` : ""}.${stubNote}`,
|
|
219
|
-
matched
|
|
218
|
+
detail: `table has ${table.headers.length} column(s) and ${table.rows.length} row(s)`,
|
|
219
|
+
...base
|
|
220
220
|
};
|
|
221
221
|
}
|
|
222
|
-
|
|
223
|
-
const
|
|
224
|
-
let
|
|
225
|
-
|
|
226
|
-
|
|
227
|
-
|
|
228
|
-
|
|
229
|
-
|
|
230
|
-
|
|
231
|
-
|
|
232
|
-
|
|
233
|
-
|
|
234
|
-
|
|
235
|
-
|
|
236
|
-
|
|
222
|
+
function parseCsv(text) {
|
|
223
|
+
const rows = [];
|
|
224
|
+
let row = [];
|
|
225
|
+
let cell = "";
|
|
226
|
+
let inQuotes = false;
|
|
227
|
+
let started = false;
|
|
228
|
+
const s = text.replace(/^\uFEFF/, "");
|
|
229
|
+
for (let i = 0; i < s.length; i++) {
|
|
230
|
+
const ch = s[i];
|
|
231
|
+
started = true;
|
|
232
|
+
if (inQuotes) {
|
|
233
|
+
if (ch === '"') {
|
|
234
|
+
if (s[i + 1] === '"') {
|
|
235
|
+
cell += '"';
|
|
236
|
+
i++;
|
|
237
|
+
} else {
|
|
238
|
+
inQuotes = false;
|
|
239
|
+
}
|
|
240
|
+
} else {
|
|
241
|
+
cell += ch;
|
|
242
|
+
}
|
|
243
|
+
} else if (ch === '"') {
|
|
244
|
+
inQuotes = true;
|
|
245
|
+
} else if (ch === ",") {
|
|
246
|
+
row.push(cell);
|
|
247
|
+
cell = "";
|
|
248
|
+
} else if (ch === "\n") {
|
|
249
|
+
row.push(cell);
|
|
250
|
+
rows.push(row);
|
|
251
|
+
row = [];
|
|
252
|
+
cell = "";
|
|
253
|
+
} else if (ch !== "\r") {
|
|
254
|
+
cell += ch;
|
|
237
255
|
}
|
|
238
256
|
}
|
|
239
|
-
|
|
257
|
+
if (started && (cell.length > 0 || row.length > 0)) {
|
|
258
|
+
row.push(cell);
|
|
259
|
+
rows.push(row);
|
|
260
|
+
}
|
|
261
|
+
return rows;
|
|
262
|
+
}
|
|
263
|
+
function csvQuoteError(text) {
|
|
264
|
+
let inQuotes = false;
|
|
265
|
+
const s = text.replace(/^\uFEFF/, "");
|
|
266
|
+
for (let i = 0; i < s.length; i++) {
|
|
267
|
+
const ch = s[i];
|
|
268
|
+
if (inQuotes) {
|
|
269
|
+
if (ch === '"') {
|
|
270
|
+
if (s[i + 1] === '"') {
|
|
271
|
+
i++;
|
|
272
|
+
} else {
|
|
273
|
+
inQuotes = false;
|
|
274
|
+
}
|
|
275
|
+
}
|
|
276
|
+
} else if (ch === '"') {
|
|
277
|
+
inQuotes = true;
|
|
278
|
+
}
|
|
279
|
+
}
|
|
280
|
+
return inQuotes ? "CSV has an unclosed quoted field" : null;
|
|
281
|
+
}
|
|
282
|
+
function dataTableSniff(text) {
|
|
283
|
+
const stripped = text.replace(/^\uFEFF/, "").trim();
|
|
284
|
+
if (stripped.length === 0) return false;
|
|
285
|
+
if (stripped[0] === "[") {
|
|
286
|
+
try {
|
|
287
|
+
const parsed = JSON.parse(stripped);
|
|
288
|
+
return Array.isArray(parsed) && parsed.length > 0;
|
|
289
|
+
} catch {
|
|
290
|
+
return false;
|
|
291
|
+
}
|
|
292
|
+
}
|
|
293
|
+
const md = parseMarkdownTable(stripped);
|
|
294
|
+
if (md && md.headers.length > 0 && md.rows.length > 0) return true;
|
|
295
|
+
const grid = parseCsv(stripped).filter((r) => r.some((c) => c.trim().length > 0));
|
|
296
|
+
if (grid.length >= 2) {
|
|
297
|
+
const cols = grid[0]?.length ?? 0;
|
|
298
|
+
if (cols >= 2 && grid.slice(1).some((r) => r.length === cols)) return true;
|
|
299
|
+
}
|
|
300
|
+
return false;
|
|
301
|
+
}
|
|
302
|
+
function csvShapeFailure(detail, headers = [], rowCount = 0) {
|
|
303
|
+
return { ok: false, detail, headers, rowCount };
|
|
304
|
+
}
|
|
305
|
+
function summarizeCsvRow(row, max = 180) {
|
|
306
|
+
const summary = row.map((cell) => cell.trim()).join(" | ");
|
|
307
|
+
return summary.length <= max ? summary : `${summary.slice(0, max - 3)}...`;
|
|
308
|
+
}
|
|
309
|
+
function csvShape(text, spec) {
|
|
310
|
+
if (text === null) {
|
|
311
|
+
return csvShapeFailure("CSV file not found");
|
|
312
|
+
}
|
|
313
|
+
const quoteError = csvQuoteError(text);
|
|
314
|
+
if (quoteError) {
|
|
315
|
+
return csvShapeFailure(quoteError);
|
|
316
|
+
}
|
|
317
|
+
const grid = parseCsv(text).filter((r) => r.some((c) => c.trim().length > 0));
|
|
318
|
+
if (grid.length === 0) {
|
|
319
|
+
return csvShapeFailure("CSV has no rows");
|
|
320
|
+
}
|
|
321
|
+
const headers = (grid[0] ?? []).map((h) => h.trim());
|
|
322
|
+
const dataRows = grid.slice(1);
|
|
323
|
+
const base = { headers, rowCount: dataRows.length };
|
|
324
|
+
if (headers.length === 0 || headers.every((h) => h.length === 0)) {
|
|
325
|
+
return csvShapeFailure("CSV header row is empty", headers, dataRows.length);
|
|
326
|
+
}
|
|
327
|
+
if (spec.exactColumns) {
|
|
328
|
+
const expected = spec.exactColumns;
|
|
329
|
+
const matches = headers.length === expected.length && headers.every((header, index) => header === expected[index]);
|
|
330
|
+
if (!matches) {
|
|
331
|
+
return {
|
|
332
|
+
ok: false,
|
|
333
|
+
detail: `CSV header should be exactly: ${expected.join(", ")}; found: ${headers.join(", ")}`,
|
|
334
|
+
...base
|
|
335
|
+
};
|
|
336
|
+
}
|
|
337
|
+
}
|
|
338
|
+
for (const column of spec.requiredColumns ?? []) {
|
|
339
|
+
if (!headers.includes(column)) {
|
|
340
|
+
return {
|
|
341
|
+
ok: false,
|
|
342
|
+
detail: `CSV is missing required column "${column}" \u2014 found: ${headers.join(", ")}`,
|
|
343
|
+
...base
|
|
344
|
+
};
|
|
345
|
+
}
|
|
346
|
+
}
|
|
347
|
+
const minRows = spec.minRows ?? 0;
|
|
348
|
+
if (dataRows.length < minRows) {
|
|
349
|
+
return {
|
|
350
|
+
ok: false,
|
|
351
|
+
detail: `CSV has ${dataRows.length} data row(s), need \u2265 ${minRows}`,
|
|
352
|
+
...base
|
|
353
|
+
};
|
|
354
|
+
}
|
|
355
|
+
if (spec.consistentColumns ?? true) {
|
|
356
|
+
for (let i = 0; i < dataRows.length; i++) {
|
|
357
|
+
const row = dataRows[i] ?? [];
|
|
358
|
+
if (row.length !== headers.length) {
|
|
359
|
+
return {
|
|
360
|
+
ok: false,
|
|
361
|
+
detail: `CSV row ${i + 2} has ${row.length} column(s), expected ${headers.length}. Keep empty placeholders as adjacent commas so every row matches the header. Header order: ${headers.join(" | ")}. Row ${i + 2}: ${summarizeCsvRow(row)}`,
|
|
362
|
+
...base
|
|
363
|
+
};
|
|
364
|
+
}
|
|
365
|
+
}
|
|
366
|
+
}
|
|
367
|
+
for (const [column, allowedValues] of Object.entries(spec.allowedValues ?? {})) {
|
|
368
|
+
const index = headers.indexOf(column);
|
|
369
|
+
if (index === -1) {
|
|
370
|
+
return {
|
|
371
|
+
ok: false,
|
|
372
|
+
detail: `CSV allowed-values check names unknown column "${column}"`,
|
|
373
|
+
...base
|
|
374
|
+
};
|
|
375
|
+
}
|
|
376
|
+
for (let i = 0; i < dataRows.length; i++) {
|
|
377
|
+
const value = (dataRows[i]?.[index] ?? "").trim();
|
|
378
|
+
if (value.length > 0 && !allowedValues.includes(value)) {
|
|
379
|
+
return {
|
|
380
|
+
ok: false,
|
|
381
|
+
detail: `CSV row ${i + 2} column "${column}" has "${value}", expected one of: ${allowedValues.join(", ")}`,
|
|
382
|
+
...base
|
|
383
|
+
};
|
|
384
|
+
}
|
|
385
|
+
}
|
|
386
|
+
}
|
|
387
|
+
return {
|
|
388
|
+
ok: true,
|
|
389
|
+
detail: `CSV has ${headers.length} column(s) and ${dataRows.length} data row(s)`,
|
|
390
|
+
...base
|
|
391
|
+
};
|
|
392
|
+
}
|
|
393
|
+
function recordSchema(text, spec) {
|
|
394
|
+
if (text === null) {
|
|
395
|
+
return {
|
|
396
|
+
ok: false,
|
|
397
|
+
detail: "deliverable not found \u2014 write the records file before advancing.",
|
|
398
|
+
rowCount: 0
|
|
399
|
+
};
|
|
400
|
+
}
|
|
401
|
+
if (spec.fields.length === 0) {
|
|
402
|
+
return { ok: false, detail: 'no fields configured \u2014 set the schema "fields".', rowCount: 0 };
|
|
403
|
+
}
|
|
404
|
+
const stripped = text.replace(/^\uFEFF/, "");
|
|
405
|
+
const fmt2 = spec.format && spec.format !== "auto" ? spec.format : /^\s*[[{]/.test(stripped) ? "json" : "csv";
|
|
406
|
+
let records;
|
|
407
|
+
if (fmt2 === "json") {
|
|
408
|
+
let parsed;
|
|
409
|
+
try {
|
|
410
|
+
parsed = JSON.parse(stripped);
|
|
411
|
+
} catch (err) {
|
|
412
|
+
const msg = err instanceof Error ? err.message : String(err);
|
|
413
|
+
return { ok: false, detail: `not valid JSON: ${msg.slice(0, 160)}`, rowCount: 0 };
|
|
414
|
+
}
|
|
415
|
+
if (!Array.isArray(parsed)) {
|
|
416
|
+
return {
|
|
417
|
+
ok: false,
|
|
418
|
+
detail: `expected a top-level JSON array, got ${typeof parsed}`,
|
|
419
|
+
rowCount: 0
|
|
420
|
+
};
|
|
421
|
+
}
|
|
422
|
+
records = parsed;
|
|
423
|
+
} else {
|
|
424
|
+
const grid = parseCsv(text).filter((r) => r.some((c) => c.trim().length > 0));
|
|
425
|
+
if (grid.length === 0) return { ok: false, detail: "CSV has no rows", rowCount: 0 };
|
|
426
|
+
const headers = (grid[0] ?? []).map((h) => h.trim());
|
|
427
|
+
records = grid.slice(1).map((cells) => {
|
|
428
|
+
const o = {};
|
|
429
|
+
headers.forEach((h, i) => {
|
|
430
|
+
o[h] = (cells[i] ?? "").trim();
|
|
431
|
+
});
|
|
432
|
+
return o;
|
|
433
|
+
});
|
|
434
|
+
}
|
|
435
|
+
const fieldNames = spec.fields.map((f) => f.name);
|
|
436
|
+
const allowExtra = spec.allowExtraFields ?? false;
|
|
437
|
+
for (let i = 0; i < records.length; i++) {
|
|
438
|
+
const row = records[i];
|
|
439
|
+
if (typeof row !== "object" || row === null || Array.isArray(row)) {
|
|
440
|
+
return {
|
|
441
|
+
ok: false,
|
|
442
|
+
detail: `record ${i} is not an object: ${JSON.stringify(row)?.slice(0, 120)}`,
|
|
443
|
+
rowCount: records.length
|
|
444
|
+
};
|
|
445
|
+
}
|
|
446
|
+
for (const f of spec.fields) {
|
|
447
|
+
const required = f.required !== false;
|
|
448
|
+
const raw = row[f.name];
|
|
449
|
+
const present = Object.prototype.hasOwnProperty.call(row, f.name) && raw !== "" && raw !== null && raw !== void 0;
|
|
450
|
+
if (!present) {
|
|
451
|
+
if (required) {
|
|
452
|
+
return {
|
|
453
|
+
ok: false,
|
|
454
|
+
detail: `record ${i} is missing required field "${f.name}"`,
|
|
455
|
+
rowCount: records.length
|
|
456
|
+
};
|
|
457
|
+
}
|
|
458
|
+
continue;
|
|
459
|
+
}
|
|
460
|
+
if (f.type && f.type !== "string") {
|
|
461
|
+
const v = checkCellType(String(raw), f.type);
|
|
462
|
+
if (!v.ok) {
|
|
463
|
+
return {
|
|
464
|
+
ok: false,
|
|
465
|
+
detail: `record ${i} field "${f.name}": ${v.detail}`,
|
|
466
|
+
rowCount: records.length
|
|
467
|
+
};
|
|
468
|
+
}
|
|
469
|
+
}
|
|
470
|
+
}
|
|
471
|
+
if (!allowExtra) {
|
|
472
|
+
const extra = Object.keys(row).filter((k) => !fieldNames.includes(k));
|
|
473
|
+
if (extra.length > 0) {
|
|
474
|
+
return {
|
|
475
|
+
ok: false,
|
|
476
|
+
detail: `record ${i} has unexpected field(s) ${extra.join(", ")} \u2014 allowed: ${fieldNames.join(", ")}`,
|
|
477
|
+
rowCount: records.length
|
|
478
|
+
};
|
|
479
|
+
}
|
|
480
|
+
}
|
|
481
|
+
}
|
|
482
|
+
if (spec.minRows !== void 0 && records.length < spec.minRows) {
|
|
483
|
+
return {
|
|
484
|
+
ok: false,
|
|
485
|
+
detail: `${records.length} record(s), need \u2265 ${spec.minRows}`,
|
|
486
|
+
rowCount: records.length
|
|
487
|
+
};
|
|
488
|
+
}
|
|
489
|
+
if (spec.uniqueBy) {
|
|
490
|
+
const seen = /* @__PURE__ */ new Set();
|
|
491
|
+
for (let i = 0; i < records.length; i++) {
|
|
492
|
+
const key = String(records[i][spec.uniqueBy] ?? "");
|
|
493
|
+
if (seen.has(key)) {
|
|
494
|
+
return {
|
|
495
|
+
ok: false,
|
|
496
|
+
detail: `duplicate ${spec.uniqueBy} "${key}" at record ${i} \u2014 values must be unique`,
|
|
497
|
+
rowCount: records.length
|
|
498
|
+
};
|
|
499
|
+
}
|
|
500
|
+
seen.add(key);
|
|
501
|
+
}
|
|
502
|
+
}
|
|
503
|
+
return {
|
|
504
|
+
ok: true,
|
|
505
|
+
detail: `${records.length} record(s) validate against the schema`,
|
|
506
|
+
rowCount: records.length
|
|
507
|
+
};
|
|
508
|
+
}
|
|
509
|
+
|
|
510
|
+
// ../core/src/checks/text.ts
|
|
511
|
+
function countDistinctMatches(text, pattern) {
|
|
512
|
+
const seen = /* @__PURE__ */ new Set();
|
|
513
|
+
const re = pattern.flags.includes("g") ? pattern : new RegExp(pattern.source, `${pattern.flags}g`);
|
|
514
|
+
for (const m of text.matchAll(re)) {
|
|
515
|
+
const key = (m[1] ?? m[0]).toLowerCase();
|
|
516
|
+
if (key.length > 0) seen.add(key);
|
|
517
|
+
}
|
|
518
|
+
return seen.size;
|
|
519
|
+
}
|
|
520
|
+
function requireOrderedSections(text, headers) {
|
|
521
|
+
let cursor = 0;
|
|
522
|
+
for (const header of headers) {
|
|
523
|
+
const escaped = header.replace(/[.*+?^${}()|[\]\\]/g, "\\$&");
|
|
524
|
+
const re = new RegExp(`^#+\\s+${escaped}\\s*$`, "im");
|
|
525
|
+
const slice = text.slice(cursor);
|
|
526
|
+
const m = slice.match(re);
|
|
527
|
+
if (!m || m.index === void 0) {
|
|
528
|
+
return { ok: false, missing: header, foundIndex: cursor };
|
|
529
|
+
}
|
|
530
|
+
cursor += m.index + m[0].length;
|
|
531
|
+
}
|
|
532
|
+
return { ok: true };
|
|
533
|
+
}
|
|
534
|
+
function jsonValid(content) {
|
|
535
|
+
try {
|
|
536
|
+
JSON.parse(content);
|
|
537
|
+
return { ok: true };
|
|
538
|
+
} catch (err) {
|
|
539
|
+
return { ok: false, error: err instanceof Error ? err.message : String(err) };
|
|
540
|
+
}
|
|
541
|
+
}
|
|
542
|
+
function parseJsonPath(path) {
|
|
543
|
+
const trimmed = path.trim().replace(/^\$\.?/, "");
|
|
544
|
+
if (trimmed.length === 0) return [];
|
|
545
|
+
const parts = [];
|
|
546
|
+
for (const rawPart of trimmed.split(".")) {
|
|
547
|
+
if (rawPart.length === 0) return null;
|
|
548
|
+
const keyMatch = rawPart.match(/^[^\[\]]+/);
|
|
549
|
+
if (keyMatch) parts.push(keyMatch[0]);
|
|
550
|
+
const rest = rawPart.slice(keyMatch?.[0].length ?? 0);
|
|
551
|
+
if (rest.length === 0) continue;
|
|
552
|
+
const bracketRe = /\[(\d+)\]/g;
|
|
553
|
+
let consumed = "";
|
|
554
|
+
for (const match of rest.matchAll(bracketRe)) {
|
|
555
|
+
consumed += match[0];
|
|
556
|
+
parts.push(Number(match[1]));
|
|
557
|
+
}
|
|
558
|
+
if (consumed !== rest) return null;
|
|
559
|
+
}
|
|
560
|
+
return parts;
|
|
561
|
+
}
|
|
562
|
+
function getPathValue(root, path) {
|
|
563
|
+
let cursor = root;
|
|
564
|
+
for (const part of path) {
|
|
565
|
+
if (typeof part === "number") {
|
|
566
|
+
if (!Array.isArray(cursor) || part >= cursor.length) return void 0;
|
|
567
|
+
cursor = cursor[part];
|
|
568
|
+
continue;
|
|
569
|
+
}
|
|
570
|
+
if (cursor === null || typeof cursor !== "object" || !(part in cursor)) return void 0;
|
|
571
|
+
cursor = cursor[part];
|
|
572
|
+
}
|
|
573
|
+
return cursor;
|
|
574
|
+
}
|
|
575
|
+
function printableValue(value) {
|
|
576
|
+
return typeof value === "string" ? JSON.stringify(value) : String(value);
|
|
577
|
+
}
|
|
578
|
+
async function jsonPathEquals(ws, file, path, expected, label) {
|
|
579
|
+
const content = await ws.read(file);
|
|
580
|
+
if (content === null) {
|
|
581
|
+
return { ok: false, detail: `${file} not found`, actual: void 0 };
|
|
582
|
+
}
|
|
583
|
+
let parsed;
|
|
584
|
+
try {
|
|
585
|
+
parsed = JSON.parse(content);
|
|
586
|
+
} catch (err) {
|
|
587
|
+
const detail = err instanceof Error ? err.message : String(err);
|
|
588
|
+
return { ok: false, detail: `${file} is not valid JSON: ${detail}`, actual: void 0 };
|
|
589
|
+
}
|
|
590
|
+
const parts = parseJsonPath(path);
|
|
591
|
+
if (parts === null) {
|
|
592
|
+
return { ok: false, detail: `${path} is not a supported JSON path`, actual: void 0 };
|
|
593
|
+
}
|
|
594
|
+
const actual = getPathValue(parsed, parts);
|
|
595
|
+
if (Object.is(actual, expected)) {
|
|
596
|
+
return { ok: true, detail: `${file} ${path} equals ${printableValue(expected)}`, actual };
|
|
597
|
+
}
|
|
598
|
+
const guidance = label ? `: ${label}` : "";
|
|
599
|
+
return {
|
|
600
|
+
ok: false,
|
|
601
|
+
detail: `${file} ${path} should equal ${printableValue(expected)} but was ${printableValue(actual)}${guidance}`,
|
|
602
|
+
actual
|
|
603
|
+
};
|
|
604
|
+
}
|
|
605
|
+
|
|
606
|
+
// ../core/src/checks/sniff.ts
|
|
607
|
+
function runSniff(name, content) {
|
|
608
|
+
switch (name) {
|
|
609
|
+
case "nonempty":
|
|
610
|
+
return content.trim().length > 0;
|
|
611
|
+
case "json-valid":
|
|
612
|
+
return jsonValid(content).ok;
|
|
613
|
+
case "html-complete":
|
|
614
|
+
return htmlCompleteSniff(content);
|
|
615
|
+
case "html-game":
|
|
616
|
+
return htmlGameSniff(content);
|
|
617
|
+
case "data-table":
|
|
618
|
+
return dataTableSniff(content);
|
|
619
|
+
}
|
|
620
|
+
}
|
|
621
|
+
|
|
622
|
+
// ../core/src/checks/workspace-exists.ts
|
|
623
|
+
function createCitedPathChecker(ws) {
|
|
624
|
+
let listing = null;
|
|
625
|
+
const probes = /* @__PURE__ */ new Map();
|
|
626
|
+
const loadListing = () => {
|
|
627
|
+
listing ??= ws.list().then((files) => new Set(files.map((f) => citedPathKey(f))));
|
|
628
|
+
return listing;
|
|
629
|
+
};
|
|
630
|
+
return async (cited) => {
|
|
631
|
+
const probe = cleanCitedPath(cited);
|
|
632
|
+
if (!probe) return false;
|
|
633
|
+
if ((await loadListing()).has(probe.toLowerCase())) return true;
|
|
634
|
+
let hit = probes.get(probe);
|
|
635
|
+
if (!hit) {
|
|
636
|
+
hit = ws.read(probe).then(
|
|
637
|
+
(content) => content !== null,
|
|
638
|
+
() => false
|
|
639
|
+
);
|
|
640
|
+
probes.set(probe, hit);
|
|
641
|
+
}
|
|
642
|
+
return hit;
|
|
643
|
+
};
|
|
644
|
+
}
|
|
645
|
+
function cleanCitedPath(p) {
|
|
646
|
+
return p.trim().replace(/^`+|`+$/g, "").replace(/[:#].*$/, "").replace(/^\.\//, "").replace(/^\/+/, "").replace(/^workspace\//i, "");
|
|
647
|
+
}
|
|
648
|
+
function citedPathKey(p) {
|
|
649
|
+
return cleanCitedPath(p).toLowerCase();
|
|
650
|
+
}
|
|
651
|
+
|
|
652
|
+
// ../core/src/checks/files.ts
|
|
653
|
+
async function fileMinBytes(ws, file, bytes, trim = false) {
|
|
654
|
+
const content = await ws.read(file);
|
|
655
|
+
const n = (trim ? content?.trim() : content)?.length ?? 0;
|
|
656
|
+
return n >= bytes ? { ok: true, detail: `${file} is ${n} bytes` } : { ok: false, detail: `${file} is ${n} bytes, need \u2265 ${bytes}` };
|
|
657
|
+
}
|
|
658
|
+
function frontmatterList(content, key) {
|
|
659
|
+
const block = /^---\r?\n([\s\S]*?)\r?\n---/.exec(content);
|
|
660
|
+
if (!block) return null;
|
|
661
|
+
const lines = (block[1] ?? "").split(/\r?\n/);
|
|
662
|
+
const at = lines.findIndex((line2) => line2.trimStart().startsWith(`${key}:`));
|
|
663
|
+
if (at < 0) return null;
|
|
664
|
+
const clean = (value) => value.trim().replace(/^['"]|['"]$/g, "").trim();
|
|
665
|
+
const line = lines[at] ?? "";
|
|
666
|
+
const inline = line.slice(line.indexOf(":") + 1).trim();
|
|
667
|
+
if (inline) {
|
|
668
|
+
return inline.replace(/^\[|\]$/g, "").split(",").map(clean).filter(Boolean);
|
|
669
|
+
}
|
|
670
|
+
const items = [];
|
|
671
|
+
for (const next of lines.slice(at + 1)) {
|
|
672
|
+
const item = /^\s*-\s+(.*)$/.exec(next);
|
|
673
|
+
if (!item) break;
|
|
674
|
+
const value = clean(item[1] ?? "");
|
|
675
|
+
if (value) items.push(value);
|
|
676
|
+
}
|
|
677
|
+
return items;
|
|
678
|
+
}
|
|
679
|
+
async function listedFiles(ws, file, key, pathTemplate, minBytes = 1) {
|
|
680
|
+
const content = await ws.read(file);
|
|
681
|
+
if (content === null) return { ok: false, detail: `${file} not found`, missing: [] };
|
|
682
|
+
const values = frontmatterList(content, key);
|
|
683
|
+
if (!values || values.length === 0) {
|
|
684
|
+
return {
|
|
685
|
+
ok: false,
|
|
686
|
+
detail: `${file} lists no \`${key}\` in its frontmatter \u2014 add the list this step produces a file for.`,
|
|
687
|
+
missing: []
|
|
688
|
+
};
|
|
689
|
+
}
|
|
690
|
+
const missing = [];
|
|
691
|
+
for (const value of values) {
|
|
692
|
+
const slug = value.toLowerCase().replace(/[^a-z0-9]+/g, "-").replace(/^-+|-+$/g, "");
|
|
693
|
+
const path = pathTemplate.split("{value}").join(slug);
|
|
694
|
+
if (((await ws.read(path))?.length ?? 0) < minBytes) missing.push(path);
|
|
695
|
+
}
|
|
696
|
+
return missing.length === 0 ? { ok: true, detail: `every ${key} entry in ${file} has its file`, missing } : {
|
|
697
|
+
ok: false,
|
|
698
|
+
detail: `${file} lists ${key} ${values.join(", ")}, but ${missing.join(", ")} ${missing.length === 1 ? "is" : "are"} missing or empty \u2014 write ${missing.length === 1 ? "it" : "each one"} before advancing.`,
|
|
699
|
+
missing
|
|
700
|
+
};
|
|
701
|
+
}
|
|
702
|
+
async function fileMinLines(ws, file, minLines) {
|
|
703
|
+
const content = await ws.read(file);
|
|
704
|
+
const lines = (content ?? "").split("\n").filter((l) => l.trim().length > 0).length;
|
|
705
|
+
return lines >= minLines ? { ok: true, detail: `${file} has ${lines} non-blank lines` } : { ok: false, detail: `${file} has ${lines} non-blank line(s), need \u2265 ${minLines}` };
|
|
706
|
+
}
|
|
707
|
+
async function totalMinBytes(ws, files, bytes) {
|
|
708
|
+
let total = 0;
|
|
709
|
+
for (const f of files) total += (await ws.read(f))?.length ?? 0;
|
|
710
|
+
return total >= bytes ? { ok: true, detail: `${files.join(" + ")} total ${total} bytes` } : { ok: false, detail: `${files.join(" + ")} total ${total} bytes, need \u2265 ${bytes}` };
|
|
711
|
+
}
|
|
712
|
+
function isValidRasterAsset(bytes) {
|
|
713
|
+
if (bytes.length < 12) return false;
|
|
714
|
+
const containsAscii = (token) => {
|
|
715
|
+
const wanted = [...token].map((char) => char.charCodeAt(0));
|
|
716
|
+
outer: for (let i = 0; i <= bytes.length - wanted.length; i++) {
|
|
717
|
+
for (let j = 0; j < wanted.length; j++) {
|
|
718
|
+
if (bytes[i + j] !== wanted[j]) continue outer;
|
|
719
|
+
}
|
|
720
|
+
return true;
|
|
721
|
+
}
|
|
722
|
+
return false;
|
|
723
|
+
};
|
|
724
|
+
const ascii = (from, to) => String.fromCharCode(...bytes.slice(from, to));
|
|
725
|
+
const pngSignature = [137, 80, 78, 71, 13, 10, 26, 10].every(
|
|
726
|
+
(value, index) => bytes[index] === value
|
|
727
|
+
);
|
|
728
|
+
const png = pngSignature && ascii(12, 16) === "IHDR" && containsAscii("IDAT") && containsAscii("IEND");
|
|
729
|
+
const jpeg = bytes[0] === 255 && bytes[1] === 216 && bytes[2] === 255 && bytes[bytes.length - 2] === 255 && bytes[bytes.length - 1] === 217;
|
|
730
|
+
const gif = (ascii(0, 6) === "GIF87a" || ascii(0, 6) === "GIF89a") && bytes.includes(44) && bytes[bytes.length - 1] === 59;
|
|
731
|
+
const webp = ascii(0, 4) === "RIFF" && ascii(8, 12) === "WEBP" && ["VP8 ", "VP8L", "VP8X"].includes(ascii(12, 16));
|
|
732
|
+
return bytes.length >= 1024 && (png || jpeg || gif || webp);
|
|
733
|
+
}
|
|
734
|
+
var RASTER_EXTS = /* @__PURE__ */ new Set(["png", "jpg", "jpeg", "gif", "webp"]);
|
|
735
|
+
async function fileCountByExt(ws, ext, min, dir, opts) {
|
|
736
|
+
const all = await ws.list();
|
|
737
|
+
const exts = new Set(ext.map((e) => e.toLowerCase().replace(/^\./, "")));
|
|
738
|
+
const dirPrefix = dir ? `${dir.toLowerCase().replace(/\/+$/, "")}/` : null;
|
|
739
|
+
let matched = all.filter((p) => {
|
|
740
|
+
const lower = p.toLowerCase();
|
|
741
|
+
if (dirPrefix && !lower.startsWith(dirPrefix)) return false;
|
|
742
|
+
return exts.has(lower.split(".").pop() ?? "");
|
|
743
|
+
});
|
|
744
|
+
let unverifiable = false;
|
|
745
|
+
if (opts?.verifyImageBytes) {
|
|
746
|
+
const readBytes = ws.readBytes?.bind(ws);
|
|
747
|
+
if (!readBytes) {
|
|
748
|
+
unverifiable = true;
|
|
749
|
+
} else {
|
|
750
|
+
const verdicts = await Promise.all(
|
|
751
|
+
matched.map(async (p) => {
|
|
752
|
+
const fileExt = p.toLowerCase().split(".").pop() ?? "";
|
|
753
|
+
if (!RASTER_EXTS.has(fileExt)) return true;
|
|
754
|
+
const bytes = await readBytes(p).catch(() => null);
|
|
755
|
+
return bytes !== null && isValidRasterAsset(bytes);
|
|
756
|
+
})
|
|
757
|
+
);
|
|
758
|
+
matched = matched.filter((_, i) => verdicts[i]);
|
|
759
|
+
}
|
|
760
|
+
}
|
|
761
|
+
if (unverifiable) {
|
|
762
|
+
return {
|
|
763
|
+
ok: false,
|
|
764
|
+
detail: `cannot verify image bytes for ${ext.join("/")} files \u2014 this workspace view serves no binary reads, so the count would only prove filenames exist`,
|
|
765
|
+
matched
|
|
766
|
+
};
|
|
767
|
+
}
|
|
768
|
+
const where = dir ? ` in ${dir.replace(/\/+$/, "")}/` : "";
|
|
769
|
+
const verified = opts?.verifyImageBytes ? " real (byte-verified)" : "";
|
|
770
|
+
const stubNote = opts?.verifyImageBytes ? " A file with an image name but placeholder/text bytes does not count \u2014 generate the actual image." : "";
|
|
771
|
+
return matched.length >= min ? {
|
|
772
|
+
ok: true,
|
|
773
|
+
detail: `found ${matched.length}${verified} ${ext.join("/")} file(s)${where}`,
|
|
774
|
+
matched
|
|
775
|
+
} : {
|
|
776
|
+
ok: false,
|
|
777
|
+
detail: `found ${matched.length}${verified} ${ext.join("/")} file(s)${where}, need \u2265 ${min} \u2014 create the missing ${ext[0] ?? ""} file(s)${where ? ` under ${dir.replace(/\/+$/, "")}/` : ""}.${stubNote}`,
|
|
778
|
+
matched
|
|
779
|
+
};
|
|
780
|
+
}
|
|
781
|
+
async function cssMinBytes(ws, bytes, file = "index.html") {
|
|
782
|
+
const html = await ws.read(file) ?? "";
|
|
783
|
+
let css = 0;
|
|
784
|
+
for (const m of html.matchAll(/<style\b[^>]*>([\s\S]*?)<\/style>/gi)) {
|
|
785
|
+
css += (m[1] ?? "").trim().length;
|
|
786
|
+
}
|
|
787
|
+
for (const m of html.matchAll(/\bstyle\s*=\s*(?:"([^"]*)"|'([^']*)')/gi)) {
|
|
788
|
+
css += (m[1] ?? m[2] ?? "").trim().length;
|
|
789
|
+
}
|
|
790
|
+
for (const m of html.matchAll(
|
|
791
|
+
/<link\b[^>]*rel=["']?stylesheet["']?[^>]*href=["']([^"']+)["']/gi
|
|
792
|
+
)) {
|
|
793
|
+
const href = m[1];
|
|
794
|
+
if (href && !/^https?:/i.test(href)) {
|
|
795
|
+
css += (await ws.read(href.replace(/^\.?\//, "")))?.length ?? 0;
|
|
796
|
+
}
|
|
797
|
+
}
|
|
798
|
+
return css >= bytes ? { ok: true, detail: `CSS is ${css} bytes (inline + linked in ${file})` } : { ok: false, detail: `CSS is ${css} bytes (inline + linked in ${file}), need \u2265 ${bytes}` };
|
|
240
799
|
}
|
|
241
800
|
async function containsPattern(ws, file, pattern, flags, label) {
|
|
242
801
|
const content = await ws.read(file);
|
|
@@ -429,106 +988,13 @@ function standaloneJsParses(content, file = "") {
|
|
|
429
988
|
}
|
|
430
989
|
}
|
|
431
990
|
|
|
432
|
-
// ../core/src/checks/text.ts
|
|
433
|
-
function countDistinctMatches(text, pattern) {
|
|
434
|
-
const seen = /* @__PURE__ */ new Set();
|
|
435
|
-
const re = pattern.flags.includes("g") ? pattern : new RegExp(pattern.source, `${pattern.flags}g`);
|
|
436
|
-
for (const m of text.matchAll(re)) {
|
|
437
|
-
const key = (m[1] ?? m[0]).toLowerCase();
|
|
438
|
-
if (key.length > 0) seen.add(key);
|
|
439
|
-
}
|
|
440
|
-
return seen.size;
|
|
441
|
-
}
|
|
442
|
-
function requireOrderedSections(text, headers) {
|
|
443
|
-
let cursor = 0;
|
|
444
|
-
for (const header of headers) {
|
|
445
|
-
const escaped = header.replace(/[.*+?^${}()|[\]\\]/g, "\\$&");
|
|
446
|
-
const re = new RegExp(`^#+\\s+${escaped}\\s*$`, "im");
|
|
447
|
-
const slice = text.slice(cursor);
|
|
448
|
-
const m = slice.match(re);
|
|
449
|
-
if (!m || m.index === void 0) {
|
|
450
|
-
return { ok: false, missing: header, foundIndex: cursor };
|
|
451
|
-
}
|
|
452
|
-
cursor += m.index + m[0].length;
|
|
453
|
-
}
|
|
454
|
-
return { ok: true };
|
|
455
|
-
}
|
|
456
|
-
function jsonValid(content) {
|
|
457
|
-
try {
|
|
458
|
-
JSON.parse(content);
|
|
459
|
-
return { ok: true };
|
|
460
|
-
} catch (err) {
|
|
461
|
-
return { ok: false, error: err instanceof Error ? err.message : String(err) };
|
|
462
|
-
}
|
|
463
|
-
}
|
|
464
|
-
function parseJsonPath(path) {
|
|
465
|
-
const trimmed = path.trim().replace(/^\$\.?/, "");
|
|
466
|
-
if (trimmed.length === 0) return [];
|
|
467
|
-
const parts = [];
|
|
468
|
-
for (const rawPart of trimmed.split(".")) {
|
|
469
|
-
if (rawPart.length === 0) return null;
|
|
470
|
-
const keyMatch = rawPart.match(/^[^\[\]]+/);
|
|
471
|
-
if (keyMatch) parts.push(keyMatch[0]);
|
|
472
|
-
const rest = rawPart.slice(keyMatch?.[0].length ?? 0);
|
|
473
|
-
if (rest.length === 0) continue;
|
|
474
|
-
const bracketRe = /\[(\d+)\]/g;
|
|
475
|
-
let consumed = "";
|
|
476
|
-
for (const match of rest.matchAll(bracketRe)) {
|
|
477
|
-
consumed += match[0];
|
|
478
|
-
parts.push(Number(match[1]));
|
|
479
|
-
}
|
|
480
|
-
if (consumed !== rest) return null;
|
|
481
|
-
}
|
|
482
|
-
return parts;
|
|
483
|
-
}
|
|
484
|
-
function getPathValue(root, path) {
|
|
485
|
-
let cursor = root;
|
|
486
|
-
for (const part of path) {
|
|
487
|
-
if (typeof part === "number") {
|
|
488
|
-
if (!Array.isArray(cursor) || part >= cursor.length) return void 0;
|
|
489
|
-
cursor = cursor[part];
|
|
490
|
-
continue;
|
|
491
|
-
}
|
|
492
|
-
if (cursor === null || typeof cursor !== "object" || !(part in cursor)) return void 0;
|
|
493
|
-
cursor = cursor[part];
|
|
494
|
-
}
|
|
495
|
-
return cursor;
|
|
496
|
-
}
|
|
497
|
-
function printableValue(value) {
|
|
498
|
-
return typeof value === "string" ? JSON.stringify(value) : String(value);
|
|
499
|
-
}
|
|
500
|
-
async function jsonPathEquals(ws, file, path, expected, label) {
|
|
501
|
-
const content = await ws.read(file);
|
|
502
|
-
if (content === null) {
|
|
503
|
-
return { ok: false, detail: `${file} not found`, actual: void 0 };
|
|
504
|
-
}
|
|
505
|
-
let parsed;
|
|
506
|
-
try {
|
|
507
|
-
parsed = JSON.parse(content);
|
|
508
|
-
} catch (err) {
|
|
509
|
-
const detail = err instanceof Error ? err.message : String(err);
|
|
510
|
-
return { ok: false, detail: `${file} is not valid JSON: ${detail}`, actual: void 0 };
|
|
511
|
-
}
|
|
512
|
-
const parts = parseJsonPath(path);
|
|
513
|
-
if (parts === null) {
|
|
514
|
-
return { ok: false, detail: `${path} is not a supported JSON path`, actual: void 0 };
|
|
515
|
-
}
|
|
516
|
-
const actual = getPathValue(parsed, parts);
|
|
517
|
-
if (Object.is(actual, expected)) {
|
|
518
|
-
return { ok: true, detail: `${file} ${path} equals ${printableValue(expected)}`, actual };
|
|
519
|
-
}
|
|
520
|
-
const guidance = label ? `: ${label}` : "";
|
|
521
|
-
return {
|
|
522
|
-
ok: false,
|
|
523
|
-
detail: `${file} ${path} should equal ${printableValue(expected)} but was ${printableValue(actual)}${guidance}`,
|
|
524
|
-
actual
|
|
525
|
-
};
|
|
526
|
-
}
|
|
527
|
-
|
|
528
991
|
// ../core/src/checks/grounding.ts
|
|
529
992
|
function normalizeDigitGroups(text) {
|
|
530
993
|
return text.replace(/(\d)[,\s](?=\d{3}(?:\D|$))/g, "$1");
|
|
531
994
|
}
|
|
995
|
+
function normalizeInlineEmphasis(text) {
|
|
996
|
+
return text.replace(/\*\*|__|`/g, "");
|
|
997
|
+
}
|
|
532
998
|
function safeRegExp(src, flags) {
|
|
533
999
|
try {
|
|
534
1000
|
return new RegExp(src, flags);
|
|
@@ -537,7 +1003,9 @@ function safeRegExp(src, flags) {
|
|
|
537
1003
|
}
|
|
538
1004
|
}
|
|
539
1005
|
function valueGrounding(text, facts, opts = {}) {
|
|
540
|
-
const haystack =
|
|
1006
|
+
const haystack = normalizeInlineEmphasis(
|
|
1007
|
+
opts.normalizeDigits === false ? text : normalizeDigitGroups(text)
|
|
1008
|
+
);
|
|
541
1009
|
const signals = [];
|
|
542
1010
|
const decoysDetected = [];
|
|
543
1011
|
let firstFailure = null;
|
|
@@ -579,13 +1047,14 @@ var DEFAULT_CITATION_RE = (
|
|
|
579
1047
|
function stripFencedBlocks(text) {
|
|
580
1048
|
return text.replace(/^[ \t]*(`{3,}|~{3,})[^\n]*\n[\s\S]*?^[ \t]*\1[ \t]*$/gm, "").replace(/^[ \t]*(`{3,}|~{3,})[^\n]*\n[\s\S]*$/m, "");
|
|
581
1049
|
}
|
|
1050
|
+
var TASK_REF_RE = /^[A-Za-z0-9][A-Za-z0-9._-]*\/\d+$/;
|
|
582
1051
|
function extractCitations(text, re) {
|
|
583
1052
|
const flags = re.flags.includes("g") ? re.flags : `${re.flags}g`;
|
|
584
1053
|
const global = new RegExp(re.source, flags);
|
|
585
1054
|
const out = [];
|
|
586
1055
|
for (const m of stripFencedBlocks(text).matchAll(global)) {
|
|
587
1056
|
const cap = m.slice(1).find((x) => x !== void 0) ?? m[0];
|
|
588
|
-
if (cap) out.push(cap);
|
|
1057
|
+
if (cap && !TASK_REF_RE.test(cap)) out.push(cap);
|
|
589
1058
|
}
|
|
590
1059
|
return out;
|
|
591
1060
|
}
|
|
@@ -595,6 +1064,13 @@ function cleanCitation(raw) {
|
|
|
595
1064
|
function normalizeForKnownMatch(p) {
|
|
596
1065
|
return p.trim().replace(/\\/g, "/").replace(/^\.\//, "").replace(/^\//, "").replace(/^workspace\//i, "").replace(/\/+$/, "").toLowerCase();
|
|
597
1066
|
}
|
|
1067
|
+
function globMatchesKnown(cited, known) {
|
|
1068
|
+
if (!/[*?]/.test(cited)) return false;
|
|
1069
|
+
const pattern = normalizeForKnownMatch(cited).replace(/[.+^${}()|[\]\\]/g, "\\$&").replace(/\*/g, "[^/]*").replace(/\?/g, "[^/]");
|
|
1070
|
+
const re = new RegExp(`^${pattern}$`);
|
|
1071
|
+
for (const path of known) if (re.test(path)) return true;
|
|
1072
|
+
return false;
|
|
1073
|
+
}
|
|
598
1074
|
async function citationsResolve(ws, file, opts = {}) {
|
|
599
1075
|
const content = await ws.read(file);
|
|
600
1076
|
if (content === null) {
|
|
@@ -638,7 +1114,7 @@ async function citationsResolve(ws, file, opts = {}) {
|
|
|
638
1114
|
continue;
|
|
639
1115
|
}
|
|
640
1116
|
if (corpus?.has(c.toLowerCase()) || await citedPathExists(c)) resolved.push(c);
|
|
641
|
-
else if (known.has(normalizeForKnownMatch(c))) forgiven.push(c);
|
|
1117
|
+
else if (known.has(normalizeForKnownMatch(c)) || globMatchesKnown(c, known)) forgiven.push(c);
|
|
642
1118
|
else unresolved.push(c);
|
|
643
1119
|
}
|
|
644
1120
|
if (cites.length - forgiven.length < min) {
|
|
@@ -714,196 +1190,46 @@ function valuesSubsetOf(outputText, sourceTexts, spec) {
|
|
|
714
1190
|
return {
|
|
715
1191
|
ok: false,
|
|
716
1192
|
detail: `output carries ${checked} value(s) matching /${spec.pattern}/, need \u2265 ${min} \u2014 the transform must preserve the source values, not drop them.`,
|
|
717
|
-
checked,
|
|
718
|
-
invented
|
|
719
|
-
};
|
|
720
|
-
}
|
|
721
|
-
if (invented.length > 0) {
|
|
722
|
-
return {
|
|
723
|
-
ok: false,
|
|
724
|
-
detail: `${invented.length} value(s) in the output appear in no source: ${invented.slice(0, 3).join(", ")}${invented.length > 3 ? ", \u2026" : ""} \u2014 copy identifiers verbatim from the source data; never renumber or invent them.`,
|
|
725
|
-
checked,
|
|
726
|
-
invented
|
|
727
|
-
};
|
|
728
|
-
}
|
|
729
|
-
return {
|
|
730
|
-
ok: true,
|
|
731
|
-
detail: `all ${checked} output value(s) trace back to the sources`,
|
|
732
|
-
checked,
|
|
733
|
-
invented
|
|
734
|
-
};
|
|
735
|
-
}
|
|
736
|
-
|
|
737
|
-
// ../core/src/checks/security-report.ts
|
|
738
|
-
var DEFAULT_SECTIONS = [
|
|
739
|
-
"Verdict",
|
|
740
|
-
"Systemic Themes",
|
|
741
|
-
"Findings",
|
|
742
|
-
"Verified Safe",
|
|
743
|
-
"Not Statically Verifiable"
|
|
744
|
-
];
|
|
745
|
-
var VALID_SEVERITIES = /* @__PURE__ */ new Set(["critical", "high", "medium", "low", "info"]);
|
|
746
|
-
function str(x) {
|
|
747
|
-
return typeof x === "string" ? x.trim() : "";
|
|
748
|
-
}
|
|
749
|
-
function escapeRe(s) {
|
|
750
|
-
return s.replace(/[.*+?^${}()|[\]\\]/g, "\\$&");
|
|
751
|
-
}
|
|
752
|
-
function hasSection(content, name) {
|
|
753
|
-
return new RegExp(`^#{1,4}\\s+${escapeRe(name)}\\b`, "im").test(content);
|
|
754
|
-
}
|
|
755
|
-
function sectionBody(content, name) {
|
|
756
|
-
const m = new RegExp(`^(#{1,4})\\s+${escapeRe(name)}\\b`, "im").exec(content);
|
|
757
|
-
if (!m) return "";
|
|
758
|
-
const level = m[1].length;
|
|
759
|
-
const start = m.index + m[0].length;
|
|
760
|
-
const rest = content.slice(start);
|
|
761
|
-
const next = new RegExp(`^#{1,${level}}\\s+\\S`, "m").exec(rest);
|
|
762
|
-
return next ? rest.slice(0, next.index) : rest;
|
|
763
|
-
}
|
|
764
|
-
function countThemeItems(section) {
|
|
765
|
-
let n = 0;
|
|
766
|
-
for (const line of section.split(/\r?\n/)) {
|
|
767
|
-
if (/^\s*(#{3,4}\s+\S|[-*]\s+\S|\d+\.\s+\S|\*\*[^*]+\*\*)/.test(line)) n++;
|
|
768
|
-
}
|
|
769
|
-
return n;
|
|
770
|
-
}
|
|
771
|
-
function uniq(xs) {
|
|
772
|
-
return [...new Set(xs)];
|
|
773
|
-
}
|
|
774
|
-
async function securityReport(ws, reportFile, opts = {}) {
|
|
775
|
-
const fail = (detail, count = 0, fabricated2 = []) => ({
|
|
776
|
-
ok: false,
|
|
777
|
-
detail,
|
|
778
|
-
findingCount: count,
|
|
779
|
-
fabricated: fabricated2
|
|
780
|
-
});
|
|
781
|
-
const content = await ws.read(reportFile);
|
|
782
|
-
if (content === null) {
|
|
783
|
-
return fail(`${reportFile} not found \u2014 write the security report before advancing.`);
|
|
784
|
-
}
|
|
785
|
-
const findingsPath = opts.findings ?? "security-review/findings.json";
|
|
786
|
-
const raw = await ws.read(findingsPath);
|
|
787
|
-
if (raw === null) {
|
|
788
|
-
return fail(
|
|
789
|
-
`${findingsPath} not found \u2014 emit a machine-readable findings JSON alongside the report.`
|
|
790
|
-
);
|
|
791
|
-
}
|
|
792
|
-
let parsed;
|
|
793
|
-
try {
|
|
794
|
-
parsed = JSON.parse(raw);
|
|
795
|
-
} catch (e) {
|
|
796
|
-
return fail(
|
|
797
|
-
`${findingsPath} is not valid JSON (${e instanceof Error ? e.message : "parse error"}). Emit a JSON array of findings.`
|
|
798
|
-
);
|
|
799
|
-
}
|
|
800
|
-
const arr = Array.isArray(parsed) ? parsed : parsed && typeof parsed === "object" && Array.isArray(parsed.findings) ? parsed.findings : null;
|
|
801
|
-
if (!arr) {
|
|
802
|
-
return fail(`${findingsPath} must be a JSON array of findings (or { "findings": [...] }).`);
|
|
803
|
-
}
|
|
804
|
-
const citedPathExists = createCitedPathChecker(ws);
|
|
805
|
-
const problems = [];
|
|
806
|
-
const fabricated = [];
|
|
807
|
-
let critHigh = 0;
|
|
808
|
-
for (const [i, rawFinding] of arr.entries()) {
|
|
809
|
-
const f = rawFinding ?? {};
|
|
810
|
-
const file = str(f.file ?? f.path);
|
|
811
|
-
const sev = str(f.severity).toLowerCase();
|
|
812
|
-
const remediation = str(f.remediation ?? f.fix ?? f.recommendation);
|
|
813
|
-
const title = str(f.title ?? f.description ?? f.summary);
|
|
814
|
-
const line = f.line ?? f.lineStart;
|
|
815
|
-
const label = file || `#${i + 1}`;
|
|
816
|
-
if (!file) problems.push(`finding #${i + 1} has no file`);
|
|
817
|
-
else if (!await citedPathExists(file)) fabricated.push(file);
|
|
818
|
-
if (!VALID_SEVERITIES.has(sev)) problems.push(`${label} has an invalid severity "${sev}"`);
|
|
819
|
-
if (!remediation) problems.push(`${label} has no remediation`);
|
|
820
|
-
if (!title) problems.push(`${label} has no title/description`);
|
|
821
|
-
if (typeof line !== "number") problems.push(`${label} is not pinned to a line`);
|
|
822
|
-
if (sev === "critical" || sev === "high") critHigh++;
|
|
823
|
-
}
|
|
824
|
-
if (fabricated.length > 0) {
|
|
825
|
-
return fail(
|
|
826
|
-
`findings cite ${fabricated.length} file(s) that don't exist in the workspace: ${uniq(fabricated).slice(0, 5).join(", ")} \u2014 every finding must point at a real file:line (no fabricated citations).`,
|
|
827
|
-
arr.length,
|
|
828
|
-
uniq(fabricated)
|
|
829
|
-
);
|
|
830
|
-
}
|
|
831
|
-
if (problems.length > 0) {
|
|
832
|
-
return fail(
|
|
833
|
-
`${problems.length} finding(s) are incomplete: ${problems.slice(0, 4).join("; ")} \u2014 every finding needs a real file:line, a severity, and a concrete remediation.`,
|
|
834
|
-
arr.length
|
|
835
|
-
);
|
|
836
|
-
}
|
|
837
|
-
const requiredSections = opts.requiredSections ?? DEFAULT_SECTIONS;
|
|
838
|
-
const missing = requiredSections.filter((s) => !hasSection(content, s));
|
|
839
|
-
if (missing.length > 0) {
|
|
840
|
-
return fail(
|
|
841
|
-
`the report is missing required section(s): ${missing.map((s) => `## ${s}`).join(", ")}.`,
|
|
842
|
-
arr.length
|
|
843
|
-
);
|
|
844
|
-
}
|
|
845
|
-
const themeThreshold = opts.themeThreshold ?? 3;
|
|
846
|
-
const minThemes = opts.minThemes ?? 2;
|
|
847
|
-
if (arr.length >= themeThreshold) {
|
|
848
|
-
const themes = sectionBody(content, "Systemic Themes");
|
|
849
|
-
const items = countThemeItems(themes);
|
|
850
|
-
if (items < minThemes) {
|
|
851
|
-
return fail(
|
|
852
|
-
`the "Systemic Themes" section lists ${items} theme(s); a review with ${arr.length} findings needs \u2265 ${minThemes}, each naming a root cause and its blast radius.`,
|
|
853
|
-
arr.length
|
|
854
|
-
);
|
|
855
|
-
}
|
|
856
|
-
if (!/root[\s-]?cause/i.test(themes) || !/(blast[\s-]?radius|impact|scope|reach)/i.test(themes)) {
|
|
857
|
-
return fail(
|
|
858
|
-
`the "Systemic Themes" section must analyze each theme's root cause and blast radius \u2014 that analysis is absent.`,
|
|
859
|
-
arr.length
|
|
860
|
-
);
|
|
861
|
-
}
|
|
862
|
-
}
|
|
863
|
-
const verdict = sectionBody(content, "Verdict").toLowerCase();
|
|
864
|
-
if (critHigh > 0 && /(safe[\s-]?to[\s-]?merge|no (security )?(issues|findings|vulnerabilities)|looks secure|no concerns|all clear)/.test(
|
|
865
|
-
verdict
|
|
866
|
-
) && !/(block|after[\s-]?fix|merge[\s-]?after|do not merge|concern|open (critical|high))/.test(
|
|
867
|
-
verdict
|
|
868
|
-
)) {
|
|
869
|
-
return fail(
|
|
870
|
-
`the verdict reads "safe" but there ${critHigh === 1 ? "is" : "are"} ${critHigh} critical/high finding(s) \u2014 the verdict must reflect the open severity.`,
|
|
871
|
-
arr.length
|
|
872
|
-
);
|
|
1193
|
+
checked,
|
|
1194
|
+
invented
|
|
1195
|
+
};
|
|
1196
|
+
}
|
|
1197
|
+
if (invented.length > 0) {
|
|
1198
|
+
return {
|
|
1199
|
+
ok: false,
|
|
1200
|
+
detail: `${invented.length} value(s) in the output appear in no source: ${invented.slice(0, 3).join(", ")}${invented.length > 3 ? ", \u2026" : ""} \u2014 copy identifiers verbatim from the source data; never renumber or invent them.`,
|
|
1201
|
+
checked,
|
|
1202
|
+
invented
|
|
1203
|
+
};
|
|
873
1204
|
}
|
|
874
1205
|
return {
|
|
875
1206
|
ok: true,
|
|
876
|
-
detail: `
|
|
877
|
-
|
|
878
|
-
|
|
1207
|
+
detail: `all ${checked} output value(s) trace back to the sources`,
|
|
1208
|
+
checked,
|
|
1209
|
+
invented
|
|
879
1210
|
};
|
|
880
1211
|
}
|
|
881
1212
|
|
|
882
|
-
// ../core/src/checks/
|
|
883
|
-
var
|
|
884
|
-
"
|
|
885
|
-
"
|
|
886
|
-
"Index coverage and method",
|
|
887
|
-
"Systemic themes",
|
|
1213
|
+
// ../core/src/checks/security-report.ts
|
|
1214
|
+
var DEFAULT_SECTIONS = [
|
|
1215
|
+
"Verdict",
|
|
1216
|
+
"Systemic Themes",
|
|
888
1217
|
"Findings",
|
|
889
|
-
"
|
|
890
|
-
"
|
|
891
|
-
"Verified sound",
|
|
892
|
-
"Not assessed",
|
|
893
|
-
"Suggested deeper reviews"
|
|
1218
|
+
"Verified Safe",
|
|
1219
|
+
"Not Statically Verifiable"
|
|
894
1220
|
];
|
|
895
|
-
var
|
|
896
|
-
function
|
|
1221
|
+
var VALID_SEVERITIES = /* @__PURE__ */ new Set(["critical", "high", "medium", "low", "info"]);
|
|
1222
|
+
function str(x) {
|
|
897
1223
|
return typeof x === "string" ? x.trim() : "";
|
|
898
1224
|
}
|
|
899
|
-
function
|
|
1225
|
+
function escapeRe(s) {
|
|
900
1226
|
return s.replace(/[.*+?^${}()|[\]\\]/g, "\\$&");
|
|
901
1227
|
}
|
|
902
|
-
function
|
|
903
|
-
return new RegExp(`^#{1,4}\\s+${
|
|
1228
|
+
function hasSection(content, name) {
|
|
1229
|
+
return new RegExp(`^#{1,4}\\s+${escapeRe(name)}\\b`, "im").test(content);
|
|
904
1230
|
}
|
|
905
|
-
function
|
|
906
|
-
const m = new RegExp(`^(#{1,4})\\s+${
|
|
1231
|
+
function sectionBody(content, name) {
|
|
1232
|
+
const m = new RegExp(`^(#{1,4})\\s+${escapeRe(name)}\\b`, "im").exec(content);
|
|
907
1233
|
if (!m) return "";
|
|
908
1234
|
const level = m[1].length;
|
|
909
1235
|
const start = m.index + m[0].length;
|
|
@@ -911,50 +1237,29 @@ function sectionBody2(content, name) {
|
|
|
911
1237
|
const next = new RegExp(`^#{1,${level}}\\s+\\S`, "m").exec(rest);
|
|
912
1238
|
return next ? rest.slice(0, next.index) : rest;
|
|
913
1239
|
}
|
|
914
|
-
function
|
|
1240
|
+
function countThemeItems(section) {
|
|
915
1241
|
let n = 0;
|
|
916
1242
|
for (const line of section.split(/\r?\n/)) {
|
|
917
1243
|
if (/^\s*(#{3,4}\s+\S|[-*]\s+\S|\d+\.\s+\S|\*\*[^*]+\*\*)/.test(line)) n++;
|
|
918
1244
|
}
|
|
919
1245
|
return n;
|
|
920
1246
|
}
|
|
921
|
-
function
|
|
922
|
-
const lines = section.split(/\r?\n/);
|
|
923
|
-
let headerAt = -1;
|
|
924
|
-
for (let i = 0; i < lines.length; i++) {
|
|
925
|
-
const line = lines[i];
|
|
926
|
-
if (/^\s*\|/.test(line) && /dimension/i.test(line) && /(grade|rating|score|health)/i.test(line)) {
|
|
927
|
-
headerAt = i;
|
|
928
|
-
break;
|
|
929
|
-
}
|
|
930
|
-
}
|
|
931
|
-
if (headerAt < 0) return -1;
|
|
932
|
-
let rows = 0;
|
|
933
|
-
for (let i = headerAt + 1; i < lines.length; i++) {
|
|
934
|
-
const line = lines[i];
|
|
935
|
-
if (!/^\s*\|/.test(line)) break;
|
|
936
|
-
if (/^\s*\|[\s:|-]+\|?\s*$/.test(line)) continue;
|
|
937
|
-
if (line.replace(/[|\s]/g, "").length === 0) continue;
|
|
938
|
-
rows++;
|
|
939
|
-
}
|
|
940
|
-
return rows;
|
|
941
|
-
}
|
|
942
|
-
function uniq2(xs) {
|
|
1247
|
+
function uniq(xs) {
|
|
943
1248
|
return [...new Set(xs)];
|
|
944
1249
|
}
|
|
945
|
-
async function
|
|
1250
|
+
async function securityReport(ws, reportFile, opts = {}) {
|
|
946
1251
|
const fail = (detail, count = 0, fabricated2 = []) => ({
|
|
947
1252
|
ok: false,
|
|
948
1253
|
detail,
|
|
949
1254
|
findingCount: count,
|
|
950
1255
|
fabricated: fabricated2
|
|
951
1256
|
});
|
|
952
|
-
const content = await
|
|
1257
|
+
const content = await ws.read(reportFile);
|
|
953
1258
|
if (content === null) {
|
|
954
|
-
return fail(`${reportFile} not found \u2014 write the
|
|
1259
|
+
return fail(`${reportFile} not found \u2014 write the security report before advancing.`);
|
|
955
1260
|
}
|
|
956
|
-
const findingsPath = opts.findings ?? "
|
|
957
|
-
const raw = await
|
|
1261
|
+
const findingsPath = opts.findings ?? "security-review/findings.json";
|
|
1262
|
+
const raw = await ws.read(findingsPath);
|
|
958
1263
|
if (raw === null) {
|
|
959
1264
|
return fail(
|
|
960
1265
|
`${findingsPath} not found \u2014 emit a machine-readable findings JSON alongside the report.`
|
|
@@ -972,498 +1277,266 @@ async function codebaseReviewReport(reports, workspace, reportFile, opts = {}) {
|
|
|
972
1277
|
if (!arr) {
|
|
973
1278
|
return fail(`${findingsPath} must be a JSON array of findings (or { "findings": [...] }).`);
|
|
974
1279
|
}
|
|
975
|
-
const citedPathExists = createCitedPathChecker(
|
|
1280
|
+
const citedPathExists = createCitedPathChecker(ws);
|
|
976
1281
|
const problems = [];
|
|
977
1282
|
const fabricated = [];
|
|
978
1283
|
let critHigh = 0;
|
|
979
1284
|
for (const [i, rawFinding] of arr.entries()) {
|
|
980
1285
|
const f = rawFinding ?? {};
|
|
981
|
-
const file =
|
|
982
|
-
const sev =
|
|
983
|
-
const remediation =
|
|
984
|
-
const title =
|
|
1286
|
+
const file = str(f.file ?? f.path);
|
|
1287
|
+
const sev = str(f.severity).toLowerCase();
|
|
1288
|
+
const remediation = str(f.remediation ?? f.fix ?? f.recommendation);
|
|
1289
|
+
const title = str(f.title ?? f.description ?? f.summary);
|
|
985
1290
|
const line = f.line ?? f.lineStart;
|
|
986
1291
|
const label = file || `#${i + 1}`;
|
|
987
1292
|
if (!file) problems.push(`finding #${i + 1} has no file`);
|
|
988
1293
|
else if (!await citedPathExists(file)) fabricated.push(file);
|
|
989
|
-
if (!
|
|
1294
|
+
if (!VALID_SEVERITIES.has(sev)) problems.push(`${label} has an invalid severity "${sev}"`);
|
|
990
1295
|
if (!remediation) problems.push(`${label} has no remediation`);
|
|
991
1296
|
if (!title) problems.push(`${label} has no title/description`);
|
|
992
|
-
if (
|
|
993
|
-
problems.push(`${label} is ${sev} but not pinned to a line`);
|
|
994
|
-
}
|
|
1297
|
+
if (typeof line !== "number") problems.push(`${label} is not pinned to a line`);
|
|
995
1298
|
if (sev === "critical" || sev === "high") critHigh++;
|
|
996
1299
|
}
|
|
997
1300
|
if (fabricated.length > 0) {
|
|
998
1301
|
return fail(
|
|
999
|
-
`findings cite ${fabricated.length} file(s) that don't exist in the workspace: ${
|
|
1302
|
+
`findings cite ${fabricated.length} file(s) that don't exist in the workspace: ${uniq(fabricated).slice(0, 5).join(", ")} \u2014 every finding must point at a real file:line (no fabricated citations).`,
|
|
1000
1303
|
arr.length,
|
|
1001
|
-
|
|
1304
|
+
uniq(fabricated)
|
|
1002
1305
|
);
|
|
1003
1306
|
}
|
|
1004
1307
|
if (problems.length > 0) {
|
|
1005
1308
|
return fail(
|
|
1006
|
-
`${problems.length} finding(s) are incomplete: ${problems.slice(0, 4).join("; ")} \u2014 every finding needs a real file, a severity,
|
|
1309
|
+
`${problems.length} finding(s) are incomplete: ${problems.slice(0, 4).join("; ")} \u2014 every finding needs a real file:line, a severity, and a concrete remediation.`,
|
|
1007
1310
|
arr.length
|
|
1008
1311
|
);
|
|
1009
1312
|
}
|
|
1010
|
-
const requiredSections = opts.requiredSections ??
|
|
1011
|
-
const missing = requiredSections.filter((s) => !
|
|
1313
|
+
const requiredSections = opts.requiredSections ?? DEFAULT_SECTIONS;
|
|
1314
|
+
const missing = requiredSections.filter((s) => !hasSection(content, s));
|
|
1012
1315
|
if (missing.length > 0) {
|
|
1013
1316
|
return fail(
|
|
1014
1317
|
`the report is missing required section(s): ${missing.map((s) => `## ${s}`).join(", ")}.`,
|
|
1015
1318
|
arr.length
|
|
1016
1319
|
);
|
|
1017
1320
|
}
|
|
1018
|
-
const minRows = opts.minScorecardRows ?? 4;
|
|
1019
|
-
const rows = scorecardRows(sectionBody2(content, "Scorecard"));
|
|
1020
|
-
if (rows < 0) {
|
|
1021
|
-
return fail(
|
|
1022
|
-
'the "Scorecard" section has no per-dimension table \u2014 add a markdown table with Dimension and Grade columns.',
|
|
1023
|
-
arr.length
|
|
1024
|
-
);
|
|
1025
|
-
}
|
|
1026
|
-
if (rows < minRows) {
|
|
1027
|
-
return fail(
|
|
1028
|
-
`the Scorecard table has ${rows} dimension row(s); grade at least ${minRows} dimensions (e.g. architecture, code quality, hygiene, security, tests).`,
|
|
1029
|
-
arr.length
|
|
1030
|
-
);
|
|
1031
|
-
}
|
|
1032
1321
|
const themeThreshold = opts.themeThreshold ?? 3;
|
|
1033
1322
|
const minThemes = opts.minThemes ?? 2;
|
|
1034
1323
|
if (arr.length >= themeThreshold) {
|
|
1035
|
-
const themes =
|
|
1036
|
-
const items =
|
|
1324
|
+
const themes = sectionBody(content, "Systemic Themes");
|
|
1325
|
+
const items = countThemeItems(themes);
|
|
1037
1326
|
if (items < minThemes) {
|
|
1038
1327
|
return fail(
|
|
1039
|
-
`the "Systemic
|
|
1040
|
-
arr.length
|
|
1041
|
-
);
|
|
1042
|
-
}
|
|
1043
|
-
if (!/root[\s-]?cause/i.test(themes) || !/(blast[\s-]?radius|impact|scope|reach)/i.test(themes)) {
|
|
1044
|
-
return fail(
|
|
1045
|
-
`the "Systemic themes" section must analyze each theme's root cause and blast radius \u2014 that analysis is absent.`,
|
|
1046
|
-
arr.length
|
|
1047
|
-
);
|
|
1048
|
-
}
|
|
1049
|
-
}
|
|
1050
|
-
const summary = sectionBody2(content, "Executive summary").toLowerCase();
|
|
1051
|
-
if (critHigh > 0 && /(excellent (health|shape|condition)|no (significant |material )?(issues|problems|findings)|nothing to fix|clean bill of health)/.test(
|
|
1052
|
-
summary
|
|
1053
|
-
) && !/(critical|high|however|but |concern|risk)/.test(summary)) {
|
|
1054
|
-
return fail(
|
|
1055
|
-
`the executive summary reads clean but there ${critHigh === 1 ? "is" : "are"} ${critHigh} critical/high finding(s) \u2014 the summary must reflect the open severity.`,
|
|
1056
|
-
arr.length
|
|
1057
|
-
);
|
|
1058
|
-
}
|
|
1059
|
-
return {
|
|
1060
|
-
ok: true,
|
|
1061
|
-
detail: `codebase review OK: ${arr.length} finding(s), all cite real files, ${rows} scorecard dimension(s), required sections present${arr.length >= themeThreshold ? ", systemic themes analyzed" : ""}.`,
|
|
1062
|
-
findingCount: arr.length,
|
|
1063
|
-
fabricated: []
|
|
1064
|
-
};
|
|
1065
|
-
}
|
|
1066
|
-
|
|
1067
|
-
// ../core/src/checks/records.ts
|
|
1068
|
-
var ISO_DATE_RE = /^\d{4}-\d{2}-\d{2}$/;
|
|
1069
|
-
function isRealIsoDate(value) {
|
|
1070
|
-
if (!ISO_DATE_RE.test(value)) return false;
|
|
1071
|
-
const parts = value.split("-").map(Number);
|
|
1072
|
-
const y = parts[0] ?? 0;
|
|
1073
|
-
const m = parts[1] ?? 0;
|
|
1074
|
-
const d = parts[2] ?? 0;
|
|
1075
|
-
const dt = new Date(Date.UTC(y, m - 1, d));
|
|
1076
|
-
return dt.getUTCFullYear() === y && dt.getUTCMonth() === m - 1 && dt.getUTCDate() === d;
|
|
1077
|
-
}
|
|
1078
|
-
function checkCellType(value, type) {
|
|
1079
|
-
switch (type) {
|
|
1080
|
-
case "string":
|
|
1081
|
-
case "nonempty":
|
|
1082
|
-
return value.length > 0 ? { ok: true, detail: "" } : { ok: false, detail: "empty value" };
|
|
1083
|
-
case "number":
|
|
1084
|
-
return /^-?\d+(\.\d+)?$/.test(value) ? { ok: true, detail: "" } : { ok: false, detail: `"${value}" is not a number` };
|
|
1085
|
-
case "integer":
|
|
1086
|
-
return /^-?\d+$/.test(value) ? { ok: true, detail: "" } : { ok: false, detail: `"${value}" is not an integer` };
|
|
1087
|
-
case "boolean":
|
|
1088
|
-
return /^(true|false)$/i.test(value) ? { ok: true, detail: "" } : { ok: false, detail: `"${value}" is not a boolean` };
|
|
1089
|
-
case "date":
|
|
1090
|
-
case "iso-date":
|
|
1091
|
-
return isRealIsoDate(value) ? { ok: true, detail: "" } : { ok: false, detail: `"${value}" is not an ISO yyyy-mm-dd date` };
|
|
1092
|
-
case "email":
|
|
1093
|
-
return /^[^\s@]+@[^\s@]+\.[^\s@]+$/.test(value) ? { ok: true, detail: "" } : { ok: false, detail: `"${value}" is not an email` };
|
|
1094
|
-
default: {
|
|
1095
|
-
let re = null;
|
|
1096
|
-
try {
|
|
1097
|
-
re = new RegExp(type);
|
|
1098
|
-
} catch {
|
|
1099
|
-
return { ok: false, detail: `invalid column type /${type}/` };
|
|
1100
|
-
}
|
|
1101
|
-
return re.test(value) ? { ok: true, detail: "" } : { ok: false, detail: `"${value}" does not match /${type}/` };
|
|
1102
|
-
}
|
|
1103
|
-
}
|
|
1104
|
-
}
|
|
1105
|
-
function splitTableRow(line) {
|
|
1106
|
-
let s = line.trim();
|
|
1107
|
-
if (s.startsWith("|")) s = s.slice(1);
|
|
1108
|
-
if (s.endsWith("|")) s = s.slice(0, -1);
|
|
1109
|
-
return s.split(/(?<!\\)\|/).map((c) => c.trim().replace(/\\\|/g, "|"));
|
|
1110
|
-
}
|
|
1111
|
-
function isDelimiterRow(line) {
|
|
1112
|
-
if (!line.includes("-")) return false;
|
|
1113
|
-
const cells = splitTableRow(line);
|
|
1114
|
-
return cells.length > 0 && cells.every((c) => /^:?-{1,}:?$/.test(c.trim()));
|
|
1115
|
-
}
|
|
1116
|
-
function parseMarkdownTable(text) {
|
|
1117
|
-
const lines = text.split("\n");
|
|
1118
|
-
for (let i = 0; i < lines.length - 1; i++) {
|
|
1119
|
-
const header = lines[i];
|
|
1120
|
-
const delim = lines[i + 1];
|
|
1121
|
-
if (!header || !header.includes("|") || !delim || !isDelimiterRow(delim)) continue;
|
|
1122
|
-
const headers = splitTableRow(header);
|
|
1123
|
-
if (headers.length === 0) continue;
|
|
1124
|
-
const rows = [];
|
|
1125
|
-
for (let j = i + 2; j < lines.length; j++) {
|
|
1126
|
-
const l = lines[j];
|
|
1127
|
-
if (!l || l.trim() === "" || !l.includes("|")) break;
|
|
1128
|
-
rows.push(splitTableRow(l));
|
|
1129
|
-
}
|
|
1130
|
-
return { headers, rows };
|
|
1131
|
-
}
|
|
1132
|
-
return null;
|
|
1133
|
-
}
|
|
1134
|
-
function tableShape(text, spec) {
|
|
1135
|
-
const table = parseMarkdownTable(text);
|
|
1136
|
-
if (!table) {
|
|
1137
|
-
return {
|
|
1138
|
-
ok: false,
|
|
1139
|
-
detail: 'no Markdown table found \u2014 provide a "| header | \u2026 |" row with a "|---|" delimiter.',
|
|
1140
|
-
headers: [],
|
|
1141
|
-
rowCount: 0
|
|
1142
|
-
};
|
|
1143
|
-
}
|
|
1144
|
-
const headersLower = table.headers.map((h) => h.toLowerCase());
|
|
1145
|
-
const base = { headers: table.headers, rowCount: table.rows.length };
|
|
1146
|
-
for (const col of spec.requiredColumns ?? []) {
|
|
1147
|
-
if (!headersLower.includes(col.toLowerCase())) {
|
|
1148
|
-
return {
|
|
1149
|
-
ok: false,
|
|
1150
|
-
detail: `table is missing required column "${col}" \u2014 found: ${table.headers.join(", ")}.`,
|
|
1151
|
-
...base
|
|
1152
|
-
};
|
|
1153
|
-
}
|
|
1154
|
-
}
|
|
1155
|
-
const minRows = spec.minRows ?? 0;
|
|
1156
|
-
if (table.rows.length < minRows) {
|
|
1157
|
-
return {
|
|
1158
|
-
ok: false,
|
|
1159
|
-
detail: `table has ${table.rows.length} row(s), need \u2265 ${minRows}.`,
|
|
1160
|
-
...base
|
|
1161
|
-
};
|
|
1162
|
-
}
|
|
1163
|
-
for (const [col, type] of Object.entries(spec.columnTypes ?? {})) {
|
|
1164
|
-
const idx = headersLower.indexOf(col.toLowerCase());
|
|
1165
|
-
if (idx === -1) {
|
|
1166
|
-
return { ok: false, detail: `column-type check names unknown column "${col}".`, ...base };
|
|
1328
|
+
`the "Systemic Themes" section lists ${items} theme(s); a review with ${arr.length} findings needs \u2265 ${minThemes}, each naming a root cause and its blast radius.`,
|
|
1329
|
+
arr.length
|
|
1330
|
+
);
|
|
1167
1331
|
}
|
|
1168
|
-
|
|
1169
|
-
|
|
1170
|
-
|
|
1171
|
-
|
|
1172
|
-
|
|
1173
|
-
}
|
|
1332
|
+
if (!/root[\s-]?cause/i.test(themes) || !/(blast[\s-]?radius|impact|scope|reach)/i.test(themes)) {
|
|
1333
|
+
return fail(
|
|
1334
|
+
`the "Systemic Themes" section must analyze each theme's root cause and blast radius \u2014 that analysis is absent.`,
|
|
1335
|
+
arr.length
|
|
1336
|
+
);
|
|
1174
1337
|
}
|
|
1175
1338
|
}
|
|
1339
|
+
const verdict = sectionBody(content, "Verdict").toLowerCase();
|
|
1340
|
+
if (critHigh > 0 && /(safe[\s-]?to[\s-]?merge|no (security )?(issues|findings|vulnerabilities)|looks secure|no concerns|all clear)/.test(
|
|
1341
|
+
verdict
|
|
1342
|
+
) && !/(block|after[\s-]?fix|merge[\s-]?after|do not merge|concern|open (critical|high))/.test(
|
|
1343
|
+
verdict
|
|
1344
|
+
)) {
|
|
1345
|
+
return fail(
|
|
1346
|
+
`the verdict reads "safe" but there ${critHigh === 1 ? "is" : "are"} ${critHigh} critical/high finding(s) \u2014 the verdict must reflect the open severity.`,
|
|
1347
|
+
arr.length
|
|
1348
|
+
);
|
|
1349
|
+
}
|
|
1176
1350
|
return {
|
|
1177
1351
|
ok: true,
|
|
1178
|
-
detail: `
|
|
1179
|
-
|
|
1352
|
+
detail: `security report OK: ${arr.length} finding(s), all cite real files, required sections present${arr.length >= themeThreshold ? ", systemic themes analyzed" : ""}.`,
|
|
1353
|
+
findingCount: arr.length,
|
|
1354
|
+
fabricated: []
|
|
1180
1355
|
};
|
|
1181
1356
|
}
|
|
1182
|
-
|
|
1183
|
-
|
|
1184
|
-
|
|
1185
|
-
|
|
1186
|
-
|
|
1187
|
-
|
|
1188
|
-
|
|
1189
|
-
|
|
1190
|
-
|
|
1191
|
-
|
|
1192
|
-
|
|
1193
|
-
|
|
1194
|
-
|
|
1195
|
-
|
|
1196
|
-
|
|
1197
|
-
|
|
1198
|
-
|
|
1199
|
-
}
|
|
1200
|
-
} else {
|
|
1201
|
-
cell += ch;
|
|
1202
|
-
}
|
|
1203
|
-
} else if (ch === '"') {
|
|
1204
|
-
inQuotes = true;
|
|
1205
|
-
} else if (ch === ",") {
|
|
1206
|
-
row.push(cell);
|
|
1207
|
-
cell = "";
|
|
1208
|
-
} else if (ch === "\n") {
|
|
1209
|
-
row.push(cell);
|
|
1210
|
-
rows.push(row);
|
|
1211
|
-
row = [];
|
|
1212
|
-
cell = "";
|
|
1213
|
-
} else if (ch !== "\r") {
|
|
1214
|
-
cell += ch;
|
|
1215
|
-
}
|
|
1216
|
-
}
|
|
1217
|
-
if (started && (cell.length > 0 || row.length > 0)) {
|
|
1218
|
-
row.push(cell);
|
|
1219
|
-
rows.push(row);
|
|
1220
|
-
}
|
|
1221
|
-
return rows;
|
|
1357
|
+
|
|
1358
|
+
// ../core/src/checks/codebase-review-report.ts
|
|
1359
|
+
var DEFAULT_SECTIONS2 = [
|
|
1360
|
+
"Executive summary",
|
|
1361
|
+
"Scorecard",
|
|
1362
|
+
"Index coverage and method",
|
|
1363
|
+
"Systemic themes",
|
|
1364
|
+
"Findings",
|
|
1365
|
+
"Quick wins",
|
|
1366
|
+
"Strategic recommendations",
|
|
1367
|
+
"Verified sound",
|
|
1368
|
+
"Not assessed",
|
|
1369
|
+
"Suggested deeper reviews"
|
|
1370
|
+
];
|
|
1371
|
+
var VALID_SEVERITIES2 = /* @__PURE__ */ new Set(["critical", "high", "medium", "low", "info"]);
|
|
1372
|
+
function str2(x) {
|
|
1373
|
+
return typeof x === "string" ? x.trim() : "";
|
|
1222
1374
|
}
|
|
1223
|
-
function
|
|
1224
|
-
|
|
1225
|
-
|
|
1226
|
-
|
|
1227
|
-
|
|
1228
|
-
|
|
1229
|
-
|
|
1230
|
-
|
|
1231
|
-
|
|
1232
|
-
|
|
1233
|
-
|
|
1234
|
-
|
|
1235
|
-
|
|
1236
|
-
|
|
1237
|
-
|
|
1238
|
-
|
|
1375
|
+
function escapeRe2(s) {
|
|
1376
|
+
return s.replace(/[.*+?^${}()|[\]\\]/g, "\\$&");
|
|
1377
|
+
}
|
|
1378
|
+
function hasSection2(content, name) {
|
|
1379
|
+
return new RegExp(`^#{1,4}\\s+${escapeRe2(name)}\\b`, "im").test(content);
|
|
1380
|
+
}
|
|
1381
|
+
function sectionBody2(content, name) {
|
|
1382
|
+
const m = new RegExp(`^(#{1,4})\\s+${escapeRe2(name)}\\b`, "im").exec(content);
|
|
1383
|
+
if (!m) return "";
|
|
1384
|
+
const level = m[1].length;
|
|
1385
|
+
const start = m.index + m[0].length;
|
|
1386
|
+
const rest = content.slice(start);
|
|
1387
|
+
const next = new RegExp(`^#{1,${level}}\\s+\\S`, "m").exec(rest);
|
|
1388
|
+
return next ? rest.slice(0, next.index) : rest;
|
|
1389
|
+
}
|
|
1390
|
+
function countThemeItems2(section) {
|
|
1391
|
+
let n = 0;
|
|
1392
|
+
for (const line of section.split(/\r?\n/)) {
|
|
1393
|
+
if (/^\s*(#{3,4}\s+\S|[-*]\s+\S|\d+\.\s+\S|\*\*[^*]+\*\*)/.test(line)) n++;
|
|
1239
1394
|
}
|
|
1240
|
-
return
|
|
1395
|
+
return n;
|
|
1241
1396
|
}
|
|
1242
|
-
function
|
|
1243
|
-
const
|
|
1244
|
-
|
|
1245
|
-
|
|
1246
|
-
|
|
1247
|
-
|
|
1248
|
-
|
|
1249
|
-
|
|
1250
|
-
return false;
|
|
1397
|
+
function scorecardRows(section) {
|
|
1398
|
+
const lines = section.split(/\r?\n/);
|
|
1399
|
+
let headerAt = -1;
|
|
1400
|
+
for (let i = 0; i < lines.length; i++) {
|
|
1401
|
+
const line = lines[i];
|
|
1402
|
+
if (/^\s*\|/.test(line) && /dimension/i.test(line) && /(grade|rating|score|health)/i.test(line)) {
|
|
1403
|
+
headerAt = i;
|
|
1404
|
+
break;
|
|
1251
1405
|
}
|
|
1252
1406
|
}
|
|
1253
|
-
|
|
1254
|
-
|
|
1255
|
-
|
|
1256
|
-
|
|
1257
|
-
|
|
1258
|
-
if (
|
|
1407
|
+
if (headerAt < 0) return -1;
|
|
1408
|
+
let rows = 0;
|
|
1409
|
+
for (let i = headerAt + 1; i < lines.length; i++) {
|
|
1410
|
+
const line = lines[i];
|
|
1411
|
+
if (!/^\s*\|/.test(line)) break;
|
|
1412
|
+
if (/^\s*\|[\s:|-]+\|?\s*$/.test(line)) continue;
|
|
1413
|
+
if (line.replace(/[|\s]/g, "").length === 0) continue;
|
|
1414
|
+
rows++;
|
|
1259
1415
|
}
|
|
1260
|
-
return
|
|
1261
|
-
}
|
|
1262
|
-
function csvShapeFailure(detail, headers = [], rowCount = 0) {
|
|
1263
|
-
return { ok: false, detail, headers, rowCount };
|
|
1416
|
+
return rows;
|
|
1264
1417
|
}
|
|
1265
|
-
function
|
|
1266
|
-
|
|
1267
|
-
return summary.length <= max ? summary : `${summary.slice(0, max - 3)}...`;
|
|
1418
|
+
function uniq2(xs) {
|
|
1419
|
+
return [...new Set(xs)];
|
|
1268
1420
|
}
|
|
1269
|
-
function
|
|
1270
|
-
|
|
1271
|
-
|
|
1272
|
-
|
|
1273
|
-
|
|
1274
|
-
|
|
1275
|
-
|
|
1276
|
-
|
|
1277
|
-
|
|
1278
|
-
|
|
1279
|
-
return csvShapeFailure("CSV has no rows");
|
|
1280
|
-
}
|
|
1281
|
-
const headers = (grid[0] ?? []).map((h) => h.trim());
|
|
1282
|
-
const dataRows = grid.slice(1);
|
|
1283
|
-
const base = { headers, rowCount: dataRows.length };
|
|
1284
|
-
if (headers.length === 0 || headers.every((h) => h.length === 0)) {
|
|
1285
|
-
return csvShapeFailure("CSV header row is empty", headers, dataRows.length);
|
|
1421
|
+
async function codebaseReviewReport(reports, workspace, reportFile, opts = {}) {
|
|
1422
|
+
const fail = (detail, count = 0, fabricated2 = []) => ({
|
|
1423
|
+
ok: false,
|
|
1424
|
+
detail,
|
|
1425
|
+
findingCount: count,
|
|
1426
|
+
fabricated: fabricated2
|
|
1427
|
+
});
|
|
1428
|
+
const content = await reports.read(reportFile);
|
|
1429
|
+
if (content === null) {
|
|
1430
|
+
return fail(`${reportFile} not found \u2014 write the codebase review report before advancing.`);
|
|
1286
1431
|
}
|
|
1287
|
-
|
|
1288
|
-
|
|
1289
|
-
|
|
1290
|
-
|
|
1291
|
-
|
|
1292
|
-
|
|
1293
|
-
detail: `CSV header should be exactly: ${expected.join(", ")}; found: ${headers.join(", ")}`,
|
|
1294
|
-
...base
|
|
1295
|
-
};
|
|
1296
|
-
}
|
|
1432
|
+
const findingsPath = opts.findings ?? "codebase-review-findings.json";
|
|
1433
|
+
const raw = await reports.read(findingsPath);
|
|
1434
|
+
if (raw === null) {
|
|
1435
|
+
return fail(
|
|
1436
|
+
`${findingsPath} not found \u2014 emit a machine-readable findings JSON alongside the report.`
|
|
1437
|
+
);
|
|
1297
1438
|
}
|
|
1298
|
-
|
|
1299
|
-
|
|
1300
|
-
|
|
1301
|
-
|
|
1302
|
-
|
|
1303
|
-
|
|
1304
|
-
|
|
1305
|
-
}
|
|
1439
|
+
let parsed;
|
|
1440
|
+
try {
|
|
1441
|
+
parsed = JSON.parse(raw);
|
|
1442
|
+
} catch (e) {
|
|
1443
|
+
return fail(
|
|
1444
|
+
`${findingsPath} is not valid JSON (${e instanceof Error ? e.message : "parse error"}). Emit a JSON array of findings.`
|
|
1445
|
+
);
|
|
1306
1446
|
}
|
|
1307
|
-
const
|
|
1308
|
-
if (
|
|
1309
|
-
return {
|
|
1310
|
-
ok: false,
|
|
1311
|
-
detail: `CSV has ${dataRows.length} data row(s), need \u2265 ${minRows}`,
|
|
1312
|
-
...base
|
|
1313
|
-
};
|
|
1447
|
+
const arr = Array.isArray(parsed) ? parsed : parsed && typeof parsed === "object" && Array.isArray(parsed.findings) ? parsed.findings : null;
|
|
1448
|
+
if (!arr) {
|
|
1449
|
+
return fail(`${findingsPath} must be a JSON array of findings (or { "findings": [...] }).`);
|
|
1314
1450
|
}
|
|
1315
|
-
|
|
1316
|
-
|
|
1317
|
-
|
|
1318
|
-
|
|
1319
|
-
|
|
1320
|
-
|
|
1321
|
-
|
|
1322
|
-
|
|
1323
|
-
|
|
1324
|
-
|
|
1451
|
+
const citedPathExists = createCitedPathChecker(workspace);
|
|
1452
|
+
const problems = [];
|
|
1453
|
+
const fabricated = [];
|
|
1454
|
+
let critHigh = 0;
|
|
1455
|
+
for (const [i, rawFinding] of arr.entries()) {
|
|
1456
|
+
const f = rawFinding ?? {};
|
|
1457
|
+
const file = str2(f.file ?? f.path);
|
|
1458
|
+
const sev = str2(f.severity).toLowerCase();
|
|
1459
|
+
const remediation = str2(f.remediation ?? f.fix ?? f.recommendation);
|
|
1460
|
+
const title = str2(f.title ?? f.description ?? f.summary);
|
|
1461
|
+
const line = f.line ?? f.lineStart;
|
|
1462
|
+
const label = file || `#${i + 1}`;
|
|
1463
|
+
if (!file) problems.push(`finding #${i + 1} has no file`);
|
|
1464
|
+
else if (!await citedPathExists(file)) fabricated.push(file);
|
|
1465
|
+
if (!VALID_SEVERITIES2.has(sev)) problems.push(`${label} has an invalid severity "${sev}"`);
|
|
1466
|
+
if (!remediation) problems.push(`${label} has no remediation`);
|
|
1467
|
+
if (!title) problems.push(`${label} has no title/description`);
|
|
1468
|
+
if ((sev === "critical" || sev === "high") && typeof line !== "number") {
|
|
1469
|
+
problems.push(`${label} is ${sev} but not pinned to a line`);
|
|
1325
1470
|
}
|
|
1471
|
+
if (sev === "critical" || sev === "high") critHigh++;
|
|
1326
1472
|
}
|
|
1327
|
-
|
|
1328
|
-
|
|
1329
|
-
|
|
1330
|
-
|
|
1331
|
-
|
|
1332
|
-
|
|
1333
|
-
...base
|
|
1334
|
-
};
|
|
1335
|
-
}
|
|
1336
|
-
for (let i = 0; i < dataRows.length; i++) {
|
|
1337
|
-
const value = (dataRows[i]?.[index] ?? "").trim();
|
|
1338
|
-
if (value.length > 0 && !allowedValues.includes(value)) {
|
|
1339
|
-
return {
|
|
1340
|
-
ok: false,
|
|
1341
|
-
detail: `CSV row ${i + 2} column "${column}" has "${value}", expected one of: ${allowedValues.join(", ")}`,
|
|
1342
|
-
...base
|
|
1343
|
-
};
|
|
1344
|
-
}
|
|
1345
|
-
}
|
|
1473
|
+
if (fabricated.length > 0) {
|
|
1474
|
+
return fail(
|
|
1475
|
+
`findings cite ${fabricated.length} file(s) that don't exist in the workspace: ${uniq2(fabricated).slice(0, 5).join(", ")} \u2014 every finding must point at a real file (no fabricated citations).`,
|
|
1476
|
+
arr.length,
|
|
1477
|
+
uniq2(fabricated)
|
|
1478
|
+
);
|
|
1346
1479
|
}
|
|
1347
|
-
|
|
1348
|
-
|
|
1349
|
-
|
|
1350
|
-
|
|
1351
|
-
|
|
1352
|
-
}
|
|
1353
|
-
function recordSchema(text, spec) {
|
|
1354
|
-
if (text === null) {
|
|
1355
|
-
return {
|
|
1356
|
-
ok: false,
|
|
1357
|
-
detail: "deliverable not found \u2014 write the records file before advancing.",
|
|
1358
|
-
rowCount: 0
|
|
1359
|
-
};
|
|
1480
|
+
if (problems.length > 0) {
|
|
1481
|
+
return fail(
|
|
1482
|
+
`${problems.length} finding(s) are incomplete: ${problems.slice(0, 4).join("; ")} \u2014 every finding needs a real file, a severity, a title, and a concrete remediation (critical/high pinned to a line).`,
|
|
1483
|
+
arr.length
|
|
1484
|
+
);
|
|
1360
1485
|
}
|
|
1361
|
-
|
|
1362
|
-
|
|
1486
|
+
const requiredSections = opts.requiredSections ?? DEFAULT_SECTIONS2;
|
|
1487
|
+
const missing = requiredSections.filter((s) => !hasSection2(content, s));
|
|
1488
|
+
if (missing.length > 0) {
|
|
1489
|
+
return fail(
|
|
1490
|
+
`the report is missing required section(s): ${missing.map((s) => `## ${s}`).join(", ")}.`,
|
|
1491
|
+
arr.length
|
|
1492
|
+
);
|
|
1363
1493
|
}
|
|
1364
|
-
const
|
|
1365
|
-
const
|
|
1366
|
-
|
|
1367
|
-
|
|
1368
|
-
|
|
1369
|
-
|
|
1370
|
-
|
|
1371
|
-
} catch (err) {
|
|
1372
|
-
const msg = err instanceof Error ? err.message : String(err);
|
|
1373
|
-
return { ok: false, detail: `not valid JSON: ${msg.slice(0, 160)}`, rowCount: 0 };
|
|
1374
|
-
}
|
|
1375
|
-
if (!Array.isArray(parsed)) {
|
|
1376
|
-
return {
|
|
1377
|
-
ok: false,
|
|
1378
|
-
detail: `expected a top-level JSON array, got ${typeof parsed}`,
|
|
1379
|
-
rowCount: 0
|
|
1380
|
-
};
|
|
1381
|
-
}
|
|
1382
|
-
records = parsed;
|
|
1383
|
-
} else {
|
|
1384
|
-
const grid = parseCsv(text).filter((r) => r.some((c) => c.trim().length > 0));
|
|
1385
|
-
if (grid.length === 0) return { ok: false, detail: "CSV has no rows", rowCount: 0 };
|
|
1386
|
-
const headers = (grid[0] ?? []).map((h) => h.trim());
|
|
1387
|
-
records = grid.slice(1).map((cells) => {
|
|
1388
|
-
const o = {};
|
|
1389
|
-
headers.forEach((h, i) => {
|
|
1390
|
-
o[h] = (cells[i] ?? "").trim();
|
|
1391
|
-
});
|
|
1392
|
-
return o;
|
|
1393
|
-
});
|
|
1494
|
+
const minRows = opts.minScorecardRows ?? 4;
|
|
1495
|
+
const rows = scorecardRows(sectionBody2(content, "Scorecard"));
|
|
1496
|
+
if (rows < 0) {
|
|
1497
|
+
return fail(
|
|
1498
|
+
'the "Scorecard" section has no per-dimension table \u2014 add a markdown table with Dimension and Grade columns.',
|
|
1499
|
+
arr.length
|
|
1500
|
+
);
|
|
1394
1501
|
}
|
|
1395
|
-
|
|
1396
|
-
|
|
1397
|
-
|
|
1398
|
-
|
|
1399
|
-
|
|
1400
|
-
|
|
1401
|
-
|
|
1402
|
-
|
|
1403
|
-
|
|
1404
|
-
|
|
1405
|
-
|
|
1406
|
-
|
|
1407
|
-
|
|
1408
|
-
|
|
1409
|
-
|
|
1410
|
-
|
|
1411
|
-
if (required) {
|
|
1412
|
-
return {
|
|
1413
|
-
ok: false,
|
|
1414
|
-
detail: `record ${i} is missing required field "${f.name}"`,
|
|
1415
|
-
rowCount: records.length
|
|
1416
|
-
};
|
|
1417
|
-
}
|
|
1418
|
-
continue;
|
|
1419
|
-
}
|
|
1420
|
-
if (f.type && f.type !== "string") {
|
|
1421
|
-
const v = checkCellType(String(raw), f.type);
|
|
1422
|
-
if (!v.ok) {
|
|
1423
|
-
return {
|
|
1424
|
-
ok: false,
|
|
1425
|
-
detail: `record ${i} field "${f.name}": ${v.detail}`,
|
|
1426
|
-
rowCount: records.length
|
|
1427
|
-
};
|
|
1428
|
-
}
|
|
1429
|
-
}
|
|
1502
|
+
if (rows < minRows) {
|
|
1503
|
+
return fail(
|
|
1504
|
+
`the Scorecard table has ${rows} dimension row(s); grade at least ${minRows} dimensions (e.g. architecture, code quality, hygiene, security, tests).`,
|
|
1505
|
+
arr.length
|
|
1506
|
+
);
|
|
1507
|
+
}
|
|
1508
|
+
const themeThreshold = opts.themeThreshold ?? 3;
|
|
1509
|
+
const minThemes = opts.minThemes ?? 2;
|
|
1510
|
+
if (arr.length >= themeThreshold) {
|
|
1511
|
+
const themes = sectionBody2(content, "Systemic themes");
|
|
1512
|
+
const items = countThemeItems2(themes);
|
|
1513
|
+
if (items < minThemes) {
|
|
1514
|
+
return fail(
|
|
1515
|
+
`the "Systemic themes" section lists ${items} theme(s); a review with ${arr.length} findings needs \u2265 ${minThemes}, each naming a root cause and its blast radius.`,
|
|
1516
|
+
arr.length
|
|
1517
|
+
);
|
|
1430
1518
|
}
|
|
1431
|
-
if (
|
|
1432
|
-
|
|
1433
|
-
|
|
1434
|
-
|
|
1435
|
-
|
|
1436
|
-
detail: `record ${i} has unexpected field(s) ${extra.join(", ")} \u2014 allowed: ${fieldNames.join(", ")}`,
|
|
1437
|
-
rowCount: records.length
|
|
1438
|
-
};
|
|
1439
|
-
}
|
|
1519
|
+
if (!/root[\s-]?cause/i.test(themes) || !/(blast[\s-]?radius|impact|scope|reach)/i.test(themes)) {
|
|
1520
|
+
return fail(
|
|
1521
|
+
`the "Systemic themes" section must analyze each theme's root cause and blast radius \u2014 that analysis is absent.`,
|
|
1522
|
+
arr.length
|
|
1523
|
+
);
|
|
1440
1524
|
}
|
|
1441
1525
|
}
|
|
1442
|
-
|
|
1443
|
-
|
|
1444
|
-
|
|
1445
|
-
|
|
1446
|
-
|
|
1447
|
-
|
|
1448
|
-
|
|
1449
|
-
|
|
1450
|
-
const seen = /* @__PURE__ */ new Set();
|
|
1451
|
-
for (let i = 0; i < records.length; i++) {
|
|
1452
|
-
const key = String(records[i][spec.uniqueBy] ?? "");
|
|
1453
|
-
if (seen.has(key)) {
|
|
1454
|
-
return {
|
|
1455
|
-
ok: false,
|
|
1456
|
-
detail: `duplicate ${spec.uniqueBy} "${key}" at record ${i} \u2014 values must be unique`,
|
|
1457
|
-
rowCount: records.length
|
|
1458
|
-
};
|
|
1459
|
-
}
|
|
1460
|
-
seen.add(key);
|
|
1461
|
-
}
|
|
1526
|
+
const summary = sectionBody2(content, "Executive summary").toLowerCase();
|
|
1527
|
+
if (critHigh > 0 && /(excellent (health|shape|condition)|no (significant |material )?(issues|problems|findings)|nothing to fix|clean bill of health)/.test(
|
|
1528
|
+
summary
|
|
1529
|
+
) && !/(critical|high|however|but |concern|risk)/.test(summary)) {
|
|
1530
|
+
return fail(
|
|
1531
|
+
`the executive summary reads clean but there ${critHigh === 1 ? "is" : "are"} ${critHigh} critical/high finding(s) \u2014 the summary must reflect the open severity.`,
|
|
1532
|
+
arr.length
|
|
1533
|
+
);
|
|
1462
1534
|
}
|
|
1463
1535
|
return {
|
|
1464
1536
|
ok: true,
|
|
1465
|
-
detail:
|
|
1466
|
-
|
|
1537
|
+
detail: `codebase review OK: ${arr.length} finding(s), all cite real files, ${rows} scorecard dimension(s), required sections present${arr.length >= themeThreshold ? ", systemic themes analyzed" : ""}.`,
|
|
1538
|
+
findingCount: arr.length,
|
|
1539
|
+
fabricated: []
|
|
1467
1540
|
};
|
|
1468
1541
|
}
|
|
1469
1542
|
|
|
@@ -1684,6 +1757,425 @@ function wrapperReturnHint(outputLines) {
|
|
|
1684
1757
|
return `Hint: the function is returning an OBJECT where the caller expects an ARRAY \u2014 it received a wrapper like \`${example}\`. Return the array itself (\`return result;\`), not an object with it nested inside.`;
|
|
1685
1758
|
}
|
|
1686
1759
|
|
|
1760
|
+
// ../core/src/checks/figures.ts
|
|
1761
|
+
var MONEY_RE = /([-−]\s?)?([$€£])\s?(\d{1,3}(?:,\d{3})+|\d+)(?:\.(\d{1,2}))?(?![\d.,]*\d)/g;
|
|
1762
|
+
function parseMoney(text) {
|
|
1763
|
+
const out = [];
|
|
1764
|
+
for (const m of text.matchAll(MONEY_RE)) {
|
|
1765
|
+
const whole = Number(m[3].replace(/,/g, ""));
|
|
1766
|
+
const cents = m[4] ? Number(m[4].padEnd(2, "0")) / 100 : 0;
|
|
1767
|
+
const value = (whole + cents) * (m[1] ? -1 : 1);
|
|
1768
|
+
out.push({ value, symbol: m[2], index: m.index, end: m.index + m[0].length });
|
|
1769
|
+
}
|
|
1770
|
+
return out;
|
|
1771
|
+
}
|
|
1772
|
+
function extractMoneyAmounts(text) {
|
|
1773
|
+
return parseMoney(normalizeInlineEmphasis(text)).map((m) => Math.abs(m.value));
|
|
1774
|
+
}
|
|
1775
|
+
var same = (a, b) => Math.abs(a - b) < 5e-3;
|
|
1776
|
+
function fmt(value, symbol) {
|
|
1777
|
+
const abs = Math.abs(value).toLocaleString("en-US", {
|
|
1778
|
+
minimumFractionDigits: 2,
|
|
1779
|
+
maximumFractionDigits: 2
|
|
1780
|
+
});
|
|
1781
|
+
return `${value < 0 ? "-" : ""}${symbol}${abs}`;
|
|
1782
|
+
}
|
|
1783
|
+
var TOTAL_RE = /\b(sub-?\s?total|grand\s+total|total|amount\s+due|balance\s+due|totaal|subtotaal)\b/i;
|
|
1784
|
+
var SUBTOTAL_RE = /\bsub-?\s?total|\bsubtotaal/i;
|
|
1785
|
+
var DISCOUNT_RE = /\b(discount|korting|credit|coupon|less|deposit\s+paid|paid)\b/i;
|
|
1786
|
+
var TAX_RE = /\b(tax|vat|gst|hst|btw|sales\s+tax)\b/i;
|
|
1787
|
+
var PER_UNIT_RE = /\b(each|apiece|per|pp)\b|\/\s?(ea|each|person|head|dozen|hour|hr|unit)\b/i;
|
|
1788
|
+
var UNIT_COLUMN_RE = /\b(unit|each|per|rate)\b/i;
|
|
1789
|
+
var TOTAL_COLUMN_RE = /\b(total|subtotal|amount|line|sum)\b/i;
|
|
1790
|
+
var QTY_CELL_RE = /^\d+(?:\.\d+)?$/;
|
|
1791
|
+
var MONEY_SRC = "([$\u20AC\xA3])\\s?(\\d{1,3}(?:,\\d{3})+|\\d+)(?:\\.(\\d{1,2}))?";
|
|
1792
|
+
var QTY_TIMES_RE = new RegExp(
|
|
1793
|
+
`(\\d+(?:\\.\\d+)?)\\s*(?:\xD7|x|\\*|@)\\s*${MONEY_SRC}\\s*(?:=|\u2192|->)\\s*${MONEY_SRC}`,
|
|
1794
|
+
"i"
|
|
1795
|
+
);
|
|
1796
|
+
var TIMES_QTY_RE = new RegExp(
|
|
1797
|
+
`${MONEY_SRC}\\s*(?:\xD7|x|\\*)\\s*(\\d+(?:\\.\\d+)?)\\s*(?:=|\u2192|->)\\s*${MONEY_SRC}`,
|
|
1798
|
+
"i"
|
|
1799
|
+
);
|
|
1800
|
+
function moneyFrom(groups, at) {
|
|
1801
|
+
const whole = Number(groups[at + 1].replace(/,/g, ""));
|
|
1802
|
+
const cents = groups[at + 2] ? Number(groups[at + 2].padEnd(2, "0")) / 100 : 0;
|
|
1803
|
+
return whole + cents;
|
|
1804
|
+
}
|
|
1805
|
+
function cleanLabel(text) {
|
|
1806
|
+
return text.replace(/^\s*(?:[-*+]|\d+[.)])\s+/, "").replace(/[(\s:—–-]+$/, "").replace(/\s+/g, " ").trim();
|
|
1807
|
+
}
|
|
1808
|
+
function splitCells(line) {
|
|
1809
|
+
return line.trim().replace(/^\|/, "").replace(/\|$/, "").split("|").map((cell) => cell.trim());
|
|
1810
|
+
}
|
|
1811
|
+
function analyzeTableRow(no, cells, header, findings, checked) {
|
|
1812
|
+
const label = cleanLabel(cells[0] ?? "");
|
|
1813
|
+
const moneyCells = [];
|
|
1814
|
+
let qty = null;
|
|
1815
|
+
cells.forEach((cell, col) => {
|
|
1816
|
+
const money = parseMoney(cell);
|
|
1817
|
+
if (money.length === 1) moneyCells.push({ col, money: money[0] });
|
|
1818
|
+
else if (col > 0 && qty === null && QTY_CELL_RE.test(cell)) qty = Number(cell);
|
|
1819
|
+
});
|
|
1820
|
+
const total = TOTAL_RE.test(label) ? SUBTOTAL_RE.test(label) ? "subtotal" : "total" : null;
|
|
1821
|
+
const discount = DISCOUNT_RE.test(label);
|
|
1822
|
+
const amounts = moneyCells.map((c) => c.money);
|
|
1823
|
+
if (amounts.length === 0) {
|
|
1824
|
+
return { no, label, amounts, value: null, total, discount, unit: null };
|
|
1825
|
+
}
|
|
1826
|
+
const last = amounts[amounts.length - 1];
|
|
1827
|
+
if (moneyCells.length >= 2 && qty !== null) {
|
|
1828
|
+
const unit = amounts[0];
|
|
1829
|
+
checked.lineMath++;
|
|
1830
|
+
const expected = Math.round(qty * unit.value * 100) / 100;
|
|
1831
|
+
if (!same(expected, last.value)) {
|
|
1832
|
+
findings.push({
|
|
1833
|
+
kind: "line-math",
|
|
1834
|
+
line: no,
|
|
1835
|
+
message: `${label}: ${qty} \xD7 ${fmt(unit.value, unit.symbol)} is ${fmt(expected, unit.symbol)}, not ${fmt(last.value, last.symbol)}.`
|
|
1836
|
+
});
|
|
1837
|
+
}
|
|
1838
|
+
return { no, label, amounts, value: last.value, total, discount, unit };
|
|
1839
|
+
}
|
|
1840
|
+
if (moneyCells.length === 1 && qty !== null && !total) {
|
|
1841
|
+
const heading = header?.[moneyCells[0].col] ?? "";
|
|
1842
|
+
const isUnit = UNIT_COLUMN_RE.test(heading) && !TOTAL_COLUMN_RE.test(heading);
|
|
1843
|
+
const isLine = TOTAL_COLUMN_RE.test(heading);
|
|
1844
|
+
const value = isUnit ? Math.round(qty * last.value * 100) / 100 : isLine ? last.value : null;
|
|
1845
|
+
return { no, label, amounts, value, total, discount, unit: isUnit ? last : null };
|
|
1846
|
+
}
|
|
1847
|
+
return {
|
|
1848
|
+
no,
|
|
1849
|
+
label,
|
|
1850
|
+
amounts,
|
|
1851
|
+
value: last.value,
|
|
1852
|
+
total,
|
|
1853
|
+
discount,
|
|
1854
|
+
unit: total ? null : amounts[0]
|
|
1855
|
+
};
|
|
1856
|
+
}
|
|
1857
|
+
function analyzeTextLine(no, text, findings, checked) {
|
|
1858
|
+
const amounts = parseMoney(text);
|
|
1859
|
+
const label = cleanLabel(amounts.length > 0 ? text.slice(0, amounts[0].index) : text);
|
|
1860
|
+
const total = TOTAL_RE.test(label) ? SUBTOTAL_RE.test(label) ? "subtotal" : "total" : null;
|
|
1861
|
+
const discount = DISCOUNT_RE.test(label);
|
|
1862
|
+
if (amounts.length === 0) return { no, label, amounts, value: null, total, discount, unit: null };
|
|
1863
|
+
const forward = QTY_TIMES_RE.exec(text);
|
|
1864
|
+
const backward = forward ? null : TIMES_QTY_RE.exec(text);
|
|
1865
|
+
const math = forward ?? backward;
|
|
1866
|
+
if (math) {
|
|
1867
|
+
const groups = [...math];
|
|
1868
|
+
const qty = Number(forward ? groups[1] : groups[4]);
|
|
1869
|
+
const unitValue = moneyFrom(groups, forward ? 2 : 1);
|
|
1870
|
+
const lineValue = moneyFrom(groups, 5);
|
|
1871
|
+
const symbol = forward ? groups[2] : groups[1];
|
|
1872
|
+
const mathLabel = cleanLabel(text.slice(0, math.index)) || label || "A line";
|
|
1873
|
+
checked.lineMath++;
|
|
1874
|
+
const expected = Math.round(qty * unitValue * 100) / 100;
|
|
1875
|
+
if (!same(expected, lineValue)) {
|
|
1876
|
+
findings.push({
|
|
1877
|
+
kind: "line-math",
|
|
1878
|
+
line: no,
|
|
1879
|
+
message: `${mathLabel}: ${qty} \xD7 ${fmt(unitValue, symbol)} is ${fmt(expected, symbol)}, not ${fmt(lineValue, symbol)}.`
|
|
1880
|
+
});
|
|
1881
|
+
}
|
|
1882
|
+
const unit = amounts.find((m) => same(m.value, unitValue)) ?? amounts[0];
|
|
1883
|
+
return { no, label: mathLabel, amounts, value: lineValue, total, discount, unit };
|
|
1884
|
+
}
|
|
1885
|
+
if (amounts.length > 1) {
|
|
1886
|
+
return { no, label, amounts, value: null, total: null, discount, unit: null };
|
|
1887
|
+
}
|
|
1888
|
+
const only = amounts[0];
|
|
1889
|
+
if (!total && PER_UNIT_RE.test(text)) {
|
|
1890
|
+
return { no, label, amounts, value: null, total, discount, unit: only };
|
|
1891
|
+
}
|
|
1892
|
+
return { no, label, amounts, value: only.value, total, discount, unit: total ? null : only };
|
|
1893
|
+
}
|
|
1894
|
+
function candidateSums(run, breaks) {
|
|
1895
|
+
const starts = [0, ...breaks.filter((b) => b > 0 && b < run.length)];
|
|
1896
|
+
const sums = [];
|
|
1897
|
+
for (const start of starts) {
|
|
1898
|
+
const slice = run.slice(start);
|
|
1899
|
+
if (slice.length === 0) continue;
|
|
1900
|
+
if (slice.some((item) => item.value === null)) continue;
|
|
1901
|
+
const discounts = slice.filter((item) => item.discount && (item.value ?? 0) > 0);
|
|
1902
|
+
const base = slice.reduce((acc, item) => acc + (item.value ?? 0), 0);
|
|
1903
|
+
sums.push(base);
|
|
1904
|
+
const off = discounts.reduce((acc, item) => acc + (item.value ?? 0), 0);
|
|
1905
|
+
if (off > 0) sums.push(base - 2 * off);
|
|
1906
|
+
}
|
|
1907
|
+
return sums.length > 0 ? sums : null;
|
|
1908
|
+
}
|
|
1909
|
+
var MONTHS = {
|
|
1910
|
+
january: 1,
|
|
1911
|
+
jan: 1,
|
|
1912
|
+
february: 2,
|
|
1913
|
+
feb: 2,
|
|
1914
|
+
march: 3,
|
|
1915
|
+
mar: 3,
|
|
1916
|
+
april: 4,
|
|
1917
|
+
apr: 4,
|
|
1918
|
+
may: 5,
|
|
1919
|
+
june: 6,
|
|
1920
|
+
jun: 6,
|
|
1921
|
+
july: 7,
|
|
1922
|
+
jul: 7,
|
|
1923
|
+
august: 8,
|
|
1924
|
+
aug: 8,
|
|
1925
|
+
september: 9,
|
|
1926
|
+
sept: 9,
|
|
1927
|
+
sep: 9,
|
|
1928
|
+
october: 10,
|
|
1929
|
+
oct: 10,
|
|
1930
|
+
november: 11,
|
|
1931
|
+
nov: 11,
|
|
1932
|
+
december: 12,
|
|
1933
|
+
dec: 12
|
|
1934
|
+
};
|
|
1935
|
+
var WEEKDAYS = {
|
|
1936
|
+
sunday: 0,
|
|
1937
|
+
sun: 0,
|
|
1938
|
+
monday: 1,
|
|
1939
|
+
mon: 1,
|
|
1940
|
+
tuesday: 2,
|
|
1941
|
+
tue: 2,
|
|
1942
|
+
tues: 2,
|
|
1943
|
+
wednesday: 3,
|
|
1944
|
+
wed: 3,
|
|
1945
|
+
thursday: 4,
|
|
1946
|
+
thu: 4,
|
|
1947
|
+
thur: 4,
|
|
1948
|
+
thurs: 4,
|
|
1949
|
+
friday: 5,
|
|
1950
|
+
fri: 5,
|
|
1951
|
+
saturday: 6,
|
|
1952
|
+
sat: 6
|
|
1953
|
+
};
|
|
1954
|
+
var WEEKDAY_NAMES = [
|
|
1955
|
+
"Sunday",
|
|
1956
|
+
"Monday",
|
|
1957
|
+
"Tuesday",
|
|
1958
|
+
"Wednesday",
|
|
1959
|
+
"Thursday",
|
|
1960
|
+
"Friday",
|
|
1961
|
+
"Saturday"
|
|
1962
|
+
];
|
|
1963
|
+
var MONTH_NAMES = [
|
|
1964
|
+
"January",
|
|
1965
|
+
"February",
|
|
1966
|
+
"March",
|
|
1967
|
+
"April",
|
|
1968
|
+
"May",
|
|
1969
|
+
"June",
|
|
1970
|
+
"July",
|
|
1971
|
+
"August",
|
|
1972
|
+
"September",
|
|
1973
|
+
"October",
|
|
1974
|
+
"November",
|
|
1975
|
+
"December"
|
|
1976
|
+
];
|
|
1977
|
+
var WEEKDAY_SRC = "(Sunday|Monday|Tuesday|Wednesday|Thursday|Friday|Saturday|Sun|Mon|Tues|Tue|Wed|Thurs|Thur|Thu|Fri|Sat)\\.?,?\\s+";
|
|
1978
|
+
var MONTH_SRC = "(January|February|March|April|May|June|July|August|September|October|November|December|Jan|Feb|Mar|Apr|Jun|Jul|Aug|Sept|Sep|Oct|Nov|Dec)\\.?";
|
|
1979
|
+
var US_DATE_RE = new RegExp(
|
|
1980
|
+
`\\b(?:${WEEKDAY_SRC})?${MONTH_SRC}\\s+(\\d{1,2})(?:st|nd|rd|th)?\\b(?:,?\\s+(\\d{4}))?`,
|
|
1981
|
+
"gi"
|
|
1982
|
+
);
|
|
1983
|
+
var EU_DATE_RE = new RegExp(
|
|
1984
|
+
`\\b(?:${WEEKDAY_SRC})?(\\d{1,2})(?:st|nd|rd|th)?\\s+(?:of\\s+)?${MONTH_SRC}\\b(?:,?\\s+(\\d{4}))?`,
|
|
1985
|
+
"gi"
|
|
1986
|
+
);
|
|
1987
|
+
var ISO_DATE_RE2 = new RegExp(`\\b(?:${WEEKDAY_SRC})?(\\d{4})-(\\d{2})-(\\d{2})\\b`, "gi");
|
|
1988
|
+
var FUTURE_RE = /\b(due|by|deadline|confirm|deliver(?:y|ed)?|event|scheduled?|until|expires?|valid|starting|order|pick-?up|week of)\b/i;
|
|
1989
|
+
var PAST_RE = /\b(since|founded|established|est\.|opened|born|history|ago|last year|back in)\b/i;
|
|
1990
|
+
var STALE_DAYS = 180;
|
|
1991
|
+
function utcDay(year, month, day) {
|
|
1992
|
+
const at = Date.UTC(year, month - 1, day);
|
|
1993
|
+
const d = new Date(at);
|
|
1994
|
+
if (d.getUTCFullYear() !== year || d.getUTCMonth() !== month - 1 || d.getUTCDate() !== day) {
|
|
1995
|
+
return null;
|
|
1996
|
+
}
|
|
1997
|
+
return at;
|
|
1998
|
+
}
|
|
1999
|
+
function dateHits(text) {
|
|
2000
|
+
const hits = [];
|
|
2001
|
+
const seen = /* @__PURE__ */ new Set();
|
|
2002
|
+
const weekdayOf = (w) => w ? WEEKDAYS[w.toLowerCase().replace(/\.$/, "")] ?? null : null;
|
|
2003
|
+
for (const m of text.matchAll(US_DATE_RE)) {
|
|
2004
|
+
seen.add(m.index);
|
|
2005
|
+
hits.push({
|
|
2006
|
+
raw: m[0],
|
|
2007
|
+
weekday: weekdayOf(m[1]),
|
|
2008
|
+
month: MONTHS[m[2].toLowerCase()],
|
|
2009
|
+
day: Number(m[3]),
|
|
2010
|
+
year: m[4] ? Number(m[4]) : null
|
|
2011
|
+
});
|
|
2012
|
+
}
|
|
2013
|
+
for (const m of text.matchAll(EU_DATE_RE)) {
|
|
2014
|
+
if (seen.has(m.index)) continue;
|
|
2015
|
+
hits.push({
|
|
2016
|
+
raw: m[0],
|
|
2017
|
+
weekday: weekdayOf(m[1]),
|
|
2018
|
+
day: Number(m[2]),
|
|
2019
|
+
month: MONTHS[m[3].toLowerCase()],
|
|
2020
|
+
year: m[4] ? Number(m[4]) : null
|
|
2021
|
+
});
|
|
2022
|
+
}
|
|
2023
|
+
for (const m of text.matchAll(ISO_DATE_RE2)) {
|
|
2024
|
+
hits.push({
|
|
2025
|
+
raw: m[0],
|
|
2026
|
+
weekday: weekdayOf(m[1]),
|
|
2027
|
+
year: Number(m[2]),
|
|
2028
|
+
month: Number(m[3]),
|
|
2029
|
+
day: Number(m[4])
|
|
2030
|
+
});
|
|
2031
|
+
}
|
|
2032
|
+
return hits;
|
|
2033
|
+
}
|
|
2034
|
+
function checkDates(no, text, todayAt, findings, checked) {
|
|
2035
|
+
const todayYear = new Date(todayAt).getUTCFullYear();
|
|
2036
|
+
for (const hit of dateHits(text)) {
|
|
2037
|
+
let at = null;
|
|
2038
|
+
let year = hit.year;
|
|
2039
|
+
if (year !== null) {
|
|
2040
|
+
at = utcDay(year, hit.month, hit.day);
|
|
2041
|
+
} else {
|
|
2042
|
+
let best = null;
|
|
2043
|
+
for (const candidate of [todayYear - 1, todayYear, todayYear + 1]) {
|
|
2044
|
+
const cAt = utcDay(candidate, hit.month, hit.day);
|
|
2045
|
+
if (cAt === null) continue;
|
|
2046
|
+
if (!best || Math.abs(cAt - todayAt) < Math.abs(best.at - todayAt)) {
|
|
2047
|
+
best = { at: cAt, year: candidate };
|
|
2048
|
+
}
|
|
2049
|
+
}
|
|
2050
|
+
at = best?.at ?? null;
|
|
2051
|
+
year = best?.year ?? null;
|
|
2052
|
+
}
|
|
2053
|
+
if (at === null || year === null) continue;
|
|
2054
|
+
checked.dates++;
|
|
2055
|
+
const named = `${MONTH_NAMES[hit.month - 1]} ${hit.day}, ${year}`;
|
|
2056
|
+
if (hit.weekday !== null) {
|
|
2057
|
+
const actual = new Date(at).getUTCDay();
|
|
2058
|
+
if (actual !== hit.weekday) {
|
|
2059
|
+
findings.push({
|
|
2060
|
+
kind: "weekday",
|
|
2061
|
+
line: no,
|
|
2062
|
+
message: `${named} is a ${WEEKDAY_NAMES[actual]}, not a ${WEEKDAY_NAMES[hit.weekday]}.`
|
|
2063
|
+
});
|
|
2064
|
+
}
|
|
2065
|
+
}
|
|
2066
|
+
if (hit.year !== null && at < todayAt && !PAST_RE.test(text)) {
|
|
2067
|
+
const daysAgo = Math.round((todayAt - at) / 864e5);
|
|
2068
|
+
if (daysAgo > STALE_DAYS || daysAgo > 1 && FUTURE_RE.test(text)) {
|
|
2069
|
+
findings.push({
|
|
2070
|
+
kind: "stale-date",
|
|
2071
|
+
line: no,
|
|
2072
|
+
message: `${named} has already passed; check the date.`
|
|
2073
|
+
});
|
|
2074
|
+
}
|
|
2075
|
+
}
|
|
2076
|
+
}
|
|
2077
|
+
}
|
|
2078
|
+
function checkFigures(text, opts) {
|
|
2079
|
+
const findings = [];
|
|
2080
|
+
const checked = { sums: 0, lineMath: 0, dates: 0, prices: 0 };
|
|
2081
|
+
const [ty, tm, td] = opts.today.split("-").map(Number);
|
|
2082
|
+
const todayAt = utcDay(ty ?? 1970, tm ?? 1, td ?? 1) ?? 0;
|
|
2083
|
+
const known = opts.knownAmounts ?? [];
|
|
2084
|
+
const lines = [];
|
|
2085
|
+
let heading = null;
|
|
2086
|
+
let header = null;
|
|
2087
|
+
let run = [];
|
|
2088
|
+
let breaks = [];
|
|
2089
|
+
const statedTotals = [];
|
|
2090
|
+
const reset = () => {
|
|
2091
|
+
run = [];
|
|
2092
|
+
breaks = [];
|
|
2093
|
+
};
|
|
2094
|
+
const rawLines = text.split(/\r?\n/);
|
|
2095
|
+
for (let i = 0; i < rawLines.length; i++) {
|
|
2096
|
+
const no = i + 1;
|
|
2097
|
+
const raw = rawLines[i];
|
|
2098
|
+
const plain = normalizeInlineEmphasis(raw);
|
|
2099
|
+
checkDates(no, plain, todayAt, findings, checked);
|
|
2100
|
+
const trimmed = plain.trim();
|
|
2101
|
+
if (/^#{1,6}\s/.test(trimmed)) {
|
|
2102
|
+
heading = trimmed.replace(/^#+\s*/, "").trim();
|
|
2103
|
+
header = null;
|
|
2104
|
+
reset();
|
|
2105
|
+
}
|
|
2106
|
+
if (/^([-*_])(\s*\1){2,}\s*$/.test(trimmed)) {
|
|
2107
|
+
header = null;
|
|
2108
|
+
reset();
|
|
2109
|
+
continue;
|
|
2110
|
+
}
|
|
2111
|
+
if (trimmed.length === 0) continue;
|
|
2112
|
+
let line;
|
|
2113
|
+
if (trimmed.startsWith("|")) {
|
|
2114
|
+
if (/^\|?\s*:?-{2,}/.test(trimmed)) continue;
|
|
2115
|
+
const cells = splitCells(trimmed);
|
|
2116
|
+
line = analyzeTableRow(no, cells, header, findings, checked);
|
|
2117
|
+
if (line.amounts.length === 0 && !header) {
|
|
2118
|
+
header = cells;
|
|
2119
|
+
breaks.push(run.length);
|
|
2120
|
+
continue;
|
|
2121
|
+
}
|
|
2122
|
+
} else {
|
|
2123
|
+
if (header) header = null;
|
|
2124
|
+
line = analyzeTextLine(no, trimmed, findings, checked);
|
|
2125
|
+
}
|
|
2126
|
+
lines.push(line);
|
|
2127
|
+
if (line.total && line.value !== null) {
|
|
2128
|
+
const stated = line.value;
|
|
2129
|
+
statedTotals.push(stated);
|
|
2130
|
+
const sums = candidateSums(run, breaks);
|
|
2131
|
+
if (sums) {
|
|
2132
|
+
checked.sums++;
|
|
2133
|
+
if (!sums.some((sum) => same(sum, stated))) {
|
|
2134
|
+
const symbol = line.amounts[line.amounts.length - 1].symbol;
|
|
2135
|
+
const where = heading ? `Under "${heading}", the ` : "The ";
|
|
2136
|
+
findings.push({
|
|
2137
|
+
kind: "sum",
|
|
2138
|
+
line: no,
|
|
2139
|
+
message: `${where}${line.label.toLowerCase() || "total"} says ${fmt(stated, symbol)}, but the items above it add up to ${fmt(sums[0], symbol)}.`
|
|
2140
|
+
});
|
|
2141
|
+
}
|
|
2142
|
+
}
|
|
2143
|
+
if (line.total === "subtotal") {
|
|
2144
|
+
run = [{ value: stated, discount: false }];
|
|
2145
|
+
breaks = [];
|
|
2146
|
+
} else {
|
|
2147
|
+
reset();
|
|
2148
|
+
}
|
|
2149
|
+
continue;
|
|
2150
|
+
}
|
|
2151
|
+
if (line.amounts.length === 0) {
|
|
2152
|
+
breaks.push(run.length);
|
|
2153
|
+
continue;
|
|
2154
|
+
}
|
|
2155
|
+
run.push({ value: line.value, discount: line.discount });
|
|
2156
|
+
}
|
|
2157
|
+
if (known.length > 0) {
|
|
2158
|
+
for (const line of lines) {
|
|
2159
|
+
if (line.total || line.discount || !line.unit || TAX_RE.test(line.label)) continue;
|
|
2160
|
+
const price = Math.abs(line.unit.value);
|
|
2161
|
+
checked.prices++;
|
|
2162
|
+
if (known.some((k) => same(k, price))) continue;
|
|
2163
|
+
if (statedTotals.some((t) => same(t, price))) continue;
|
|
2164
|
+
const amount = fmt(price, line.unit.symbol);
|
|
2165
|
+
const item = line.label || "A line";
|
|
2166
|
+
findings.push({
|
|
2167
|
+
kind: "unsourced-price",
|
|
2168
|
+
line: line.no,
|
|
2169
|
+
item,
|
|
2170
|
+
amount,
|
|
2171
|
+
message: `${item} at ${amount}: this price didn't come from you.`
|
|
2172
|
+
});
|
|
2173
|
+
}
|
|
2174
|
+
}
|
|
2175
|
+
findings.sort((a, b) => a.line - b.line);
|
|
2176
|
+
return { findings, checked };
|
|
2177
|
+
}
|
|
2178
|
+
|
|
1687
2179
|
// ../core/src/checks/markdown.ts
|
|
1688
2180
|
function cleanHeading(text) {
|
|
1689
2181
|
return text.replace(/\s+#+\s*$/, "").replace(/\s+\{\[[^\]\r\n]+\]\}\s*$/, "").replace(/[*_`]/g, "").replace(/[–—]/g, "-").replace(/[^\p{L}\p{N}]+/gu, " ").trim().toLocaleLowerCase();
|
|
@@ -1700,13 +2192,15 @@ function headingLevelHint(file, documentHeadings, h2s, expected) {
|
|
|
1700
2192
|
return `${file} puts its ${expected} slides at \`##\`${title}, but slides are split on H1 \u2014 as written this converts to a single slide. Promote each \`## Slide\` heading to \`# \` and drop the document title.`;
|
|
1701
2193
|
}
|
|
1702
2194
|
function outlineSlideHeadings(markdown) {
|
|
1703
|
-
const
|
|
2195
|
+
const explicit = [];
|
|
2196
|
+
const numberedOnly = [];
|
|
1704
2197
|
for (const match of markdown.matchAll(/^#{2,6}\s+(.+?)\s*$/gm)) {
|
|
1705
2198
|
const raw = match[1].trim();
|
|
1706
|
-
const numbered = /^(
|
|
1707
|
-
if (numbered?.[
|
|
2199
|
+
const numbered = /^(slide\s+)?\d+(?:\s*[.:\-–—]\s*|\s+)(\S.*)$/i.exec(raw);
|
|
2200
|
+
if (!numbered?.[2]) continue;
|
|
2201
|
+
(numbered[1] ? explicit : numberedOnly).push(numbered[2].trim());
|
|
1708
2202
|
}
|
|
1709
|
-
return
|
|
2203
|
+
return explicit.length > 0 ? explicit : numberedOnly;
|
|
1710
2204
|
}
|
|
1711
2205
|
async function markdownHeadingsMatch(ws, file, outlineFile, outlineWs = ws) {
|
|
1712
2206
|
const [document, outline] = await Promise.all([ws.read(file), outlineWs.read(outlineFile)]);
|
|
@@ -2237,6 +2731,7 @@ export {
|
|
|
2237
2731
|
MIN_INLINE_JS_BYTES,
|
|
2238
2732
|
MIN_JUDGE_EVIDENCE_SUBSTRING,
|
|
2239
2733
|
buildJudgePrompt,
|
|
2734
|
+
checkFigures,
|
|
2240
2735
|
citationsResolve,
|
|
2241
2736
|
citedPathKey,
|
|
2242
2737
|
cleanCitedPath,
|
|
@@ -2252,10 +2747,12 @@ export {
|
|
|
2252
2747
|
esmImports,
|
|
2253
2748
|
explainSniff,
|
|
2254
2749
|
extractInlineScripts,
|
|
2750
|
+
extractMoneyAmounts,
|
|
2255
2751
|
fileCountByExt,
|
|
2256
2752
|
fileMinBytes,
|
|
2257
2753
|
fileMinLines,
|
|
2258
2754
|
findImageRefs,
|
|
2755
|
+
frontmatterList,
|
|
2259
2756
|
gateResult,
|
|
2260
2757
|
grepMatches,
|
|
2261
2758
|
htmlCompleteSniff,
|
|
@@ -2265,10 +2762,12 @@ export {
|
|
|
2265
2762
|
isRealIsoDate,
|
|
2266
2763
|
jsonPathEquals,
|
|
2267
2764
|
jsonValid,
|
|
2765
|
+
listedFiles,
|
|
2268
2766
|
markdownHeadingsMatch,
|
|
2269
2767
|
mergeCorpusCoverageShards,
|
|
2270
2768
|
namedEntitiesConsistent,
|
|
2271
2769
|
normalizeDigitGroups,
|
|
2770
|
+
normalizeInlineEmphasis,
|
|
2272
2771
|
notContainsPattern,
|
|
2273
2772
|
parseCsv,
|
|
2274
2773
|
parseJudgeVerdict,
|
|
@@ -2278,6 +2777,7 @@ export {
|
|
|
2278
2777
|
recordSchema,
|
|
2279
2778
|
requireOrderedSections,
|
|
2280
2779
|
resolveRelative,
|
|
2780
|
+
runSniff,
|
|
2281
2781
|
securityReport,
|
|
2282
2782
|
standaloneJsParses,
|
|
2283
2783
|
tableShape,
|