@bendyline/gezel-sdk 1.0.10 → 1.1.1
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 +66 -2
- package/dist/checks.js +1346 -848
- 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,102 +988,6 @@ 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");
|
|
@@ -585,13 +1048,19 @@ function stripFencedBlocks(text) {
|
|
|
585
1048
|
return text.replace(/^[ \t]*(`{3,}|~{3,})[^\n]*\n[\s\S]*?^[ \t]*\1[ \t]*$/gm, "").replace(/^[ \t]*(`{3,}|~{3,})[^\n]*\n[\s\S]*$/m, "");
|
|
586
1049
|
}
|
|
587
1050
|
var TASK_REF_RE = /^[A-Za-z0-9][A-Za-z0-9._-]*\/\d+$/;
|
|
1051
|
+
var ANGLE_SLOT_RE = /^<[^<>.:]*>$/;
|
|
1052
|
+
var NO_NAME_RE = /^[^\p{L}\p{N}]+$/u;
|
|
1053
|
+
var SLOT_WORDS = /* @__PURE__ */ new Set(["path", "url", "path/url", "file", "none", "n/a", "tbd"]);
|
|
1054
|
+
function isTemplateSlot(cap) {
|
|
1055
|
+
return ANGLE_SLOT_RE.test(cap) || NO_NAME_RE.test(cap) || SLOT_WORDS.has(cleanCitation(cap).toLowerCase());
|
|
1056
|
+
}
|
|
588
1057
|
function extractCitations(text, re) {
|
|
589
1058
|
const flags = re.flags.includes("g") ? re.flags : `${re.flags}g`;
|
|
590
1059
|
const global = new RegExp(re.source, flags);
|
|
591
1060
|
const out = [];
|
|
592
1061
|
for (const m of stripFencedBlocks(text).matchAll(global)) {
|
|
593
1062
|
const cap = m.slice(1).find((x) => x !== void 0) ?? m[0];
|
|
594
|
-
if (cap && !TASK_REF_RE.test(cap)) out.push(cap);
|
|
1063
|
+
if (cap && !TASK_REF_RE.test(cap) && !isTemplateSlot(cap)) out.push(cap);
|
|
595
1064
|
}
|
|
596
1065
|
return out;
|
|
597
1066
|
}
|
|
@@ -601,6 +1070,13 @@ function cleanCitation(raw) {
|
|
|
601
1070
|
function normalizeForKnownMatch(p) {
|
|
602
1071
|
return p.trim().replace(/\\/g, "/").replace(/^\.\//, "").replace(/^\//, "").replace(/^workspace\//i, "").replace(/\/+$/, "").toLowerCase();
|
|
603
1072
|
}
|
|
1073
|
+
function globMatchesKnown(cited, known) {
|
|
1074
|
+
if (!/[*?]/.test(cited)) return false;
|
|
1075
|
+
const pattern = normalizeForKnownMatch(cited).replace(/[.+^${}()|[\]\\]/g, "\\$&").replace(/\*/g, "[^/]*").replace(/\?/g, "[^/]");
|
|
1076
|
+
const re = new RegExp(`^${pattern}$`);
|
|
1077
|
+
for (const path of known) if (re.test(path)) return true;
|
|
1078
|
+
return false;
|
|
1079
|
+
}
|
|
604
1080
|
async function citationsResolve(ws, file, opts = {}) {
|
|
605
1081
|
const content = await ws.read(file);
|
|
606
1082
|
if (content === null) {
|
|
@@ -644,7 +1120,7 @@ async function citationsResolve(ws, file, opts = {}) {
|
|
|
644
1120
|
continue;
|
|
645
1121
|
}
|
|
646
1122
|
if (corpus?.has(c.toLowerCase()) || await citedPathExists(c)) resolved.push(c);
|
|
647
|
-
else if (known.has(normalizeForKnownMatch(c))) forgiven.push(c);
|
|
1123
|
+
else if (known.has(normalizeForKnownMatch(c)) || globMatchesKnown(c, known)) forgiven.push(c);
|
|
648
1124
|
else unresolved.push(c);
|
|
649
1125
|
}
|
|
650
1126
|
if (cites.length - forgiven.length < min) {
|
|
@@ -663,9 +1139,10 @@ async function citationsResolve(ws, file, opts = {}) {
|
|
|
663
1139
|
};
|
|
664
1140
|
}
|
|
665
1141
|
if (unresolved.length > 0) {
|
|
1142
|
+
const listed = unresolved.slice(0, 5).map((u) => `\`${u}\``).join(", ");
|
|
666
1143
|
return {
|
|
667
1144
|
ok: false,
|
|
668
|
-
detail: `${file} cites ${unresolved.length} source(s) that do not exist: ${
|
|
1145
|
+
detail: `${file} cites ${unresolved.length} source(s) that do not exist: ${listed}${unresolved.length > 5 ? ", \u2026" : ""} \u2014 every cited path must resolve to a real file in the workspace${corpus ? "/corpus" : ""} (no fabricated citations).`,
|
|
669
1146
|
resolved,
|
|
670
1147
|
unresolved,
|
|
671
1148
|
urls,
|
|
@@ -720,196 +1197,46 @@ function valuesSubsetOf(outputText, sourceTexts, spec) {
|
|
|
720
1197
|
return {
|
|
721
1198
|
ok: false,
|
|
722
1199
|
detail: `output carries ${checked} value(s) matching /${spec.pattern}/, need \u2265 ${min} \u2014 the transform must preserve the source values, not drop them.`,
|
|
723
|
-
checked,
|
|
724
|
-
invented
|
|
725
|
-
};
|
|
726
|
-
}
|
|
727
|
-
if (invented.length > 0) {
|
|
728
|
-
return {
|
|
729
|
-
ok: false,
|
|
730
|
-
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.`,
|
|
731
|
-
checked,
|
|
732
|
-
invented
|
|
733
|
-
};
|
|
734
|
-
}
|
|
735
|
-
return {
|
|
736
|
-
ok: true,
|
|
737
|
-
detail: `all ${checked} output value(s) trace back to the sources`,
|
|
738
|
-
checked,
|
|
739
|
-
invented
|
|
740
|
-
};
|
|
741
|
-
}
|
|
742
|
-
|
|
743
|
-
// ../core/src/checks/security-report.ts
|
|
744
|
-
var DEFAULT_SECTIONS = [
|
|
745
|
-
"Verdict",
|
|
746
|
-
"Systemic Themes",
|
|
747
|
-
"Findings",
|
|
748
|
-
"Verified Safe",
|
|
749
|
-
"Not Statically Verifiable"
|
|
750
|
-
];
|
|
751
|
-
var VALID_SEVERITIES = /* @__PURE__ */ new Set(["critical", "high", "medium", "low", "info"]);
|
|
752
|
-
function str(x) {
|
|
753
|
-
return typeof x === "string" ? x.trim() : "";
|
|
754
|
-
}
|
|
755
|
-
function escapeRe(s) {
|
|
756
|
-
return s.replace(/[.*+?^${}()|[\]\\]/g, "\\$&");
|
|
757
|
-
}
|
|
758
|
-
function hasSection(content, name) {
|
|
759
|
-
return new RegExp(`^#{1,4}\\s+${escapeRe(name)}\\b`, "im").test(content);
|
|
760
|
-
}
|
|
761
|
-
function sectionBody(content, name) {
|
|
762
|
-
const m = new RegExp(`^(#{1,4})\\s+${escapeRe(name)}\\b`, "im").exec(content);
|
|
763
|
-
if (!m) return "";
|
|
764
|
-
const level = m[1].length;
|
|
765
|
-
const start = m.index + m[0].length;
|
|
766
|
-
const rest = content.slice(start);
|
|
767
|
-
const next = new RegExp(`^#{1,${level}}\\s+\\S`, "m").exec(rest);
|
|
768
|
-
return next ? rest.slice(0, next.index) : rest;
|
|
769
|
-
}
|
|
770
|
-
function countThemeItems(section) {
|
|
771
|
-
let n = 0;
|
|
772
|
-
for (const line of section.split(/\r?\n/)) {
|
|
773
|
-
if (/^\s*(#{3,4}\s+\S|[-*]\s+\S|\d+\.\s+\S|\*\*[^*]+\*\*)/.test(line)) n++;
|
|
774
|
-
}
|
|
775
|
-
return n;
|
|
776
|
-
}
|
|
777
|
-
function uniq(xs) {
|
|
778
|
-
return [...new Set(xs)];
|
|
779
|
-
}
|
|
780
|
-
async function securityReport(ws, reportFile, opts = {}) {
|
|
781
|
-
const fail = (detail, count = 0, fabricated2 = []) => ({
|
|
782
|
-
ok: false,
|
|
783
|
-
detail,
|
|
784
|
-
findingCount: count,
|
|
785
|
-
fabricated: fabricated2
|
|
786
|
-
});
|
|
787
|
-
const content = await ws.read(reportFile);
|
|
788
|
-
if (content === null) {
|
|
789
|
-
return fail(`${reportFile} not found \u2014 write the security report before advancing.`);
|
|
790
|
-
}
|
|
791
|
-
const findingsPath = opts.findings ?? "security-review/findings.json";
|
|
792
|
-
const raw = await ws.read(findingsPath);
|
|
793
|
-
if (raw === null) {
|
|
794
|
-
return fail(
|
|
795
|
-
`${findingsPath} not found \u2014 emit a machine-readable findings JSON alongside the report.`
|
|
796
|
-
);
|
|
797
|
-
}
|
|
798
|
-
let parsed;
|
|
799
|
-
try {
|
|
800
|
-
parsed = JSON.parse(raw);
|
|
801
|
-
} catch (e) {
|
|
802
|
-
return fail(
|
|
803
|
-
`${findingsPath} is not valid JSON (${e instanceof Error ? e.message : "parse error"}). Emit a JSON array of findings.`
|
|
804
|
-
);
|
|
805
|
-
}
|
|
806
|
-
const arr = Array.isArray(parsed) ? parsed : parsed && typeof parsed === "object" && Array.isArray(parsed.findings) ? parsed.findings : null;
|
|
807
|
-
if (!arr) {
|
|
808
|
-
return fail(`${findingsPath} must be a JSON array of findings (or { "findings": [...] }).`);
|
|
809
|
-
}
|
|
810
|
-
const citedPathExists = createCitedPathChecker(ws);
|
|
811
|
-
const problems = [];
|
|
812
|
-
const fabricated = [];
|
|
813
|
-
let critHigh = 0;
|
|
814
|
-
for (const [i, rawFinding] of arr.entries()) {
|
|
815
|
-
const f = rawFinding ?? {};
|
|
816
|
-
const file = str(f.file ?? f.path);
|
|
817
|
-
const sev = str(f.severity).toLowerCase();
|
|
818
|
-
const remediation = str(f.remediation ?? f.fix ?? f.recommendation);
|
|
819
|
-
const title = str(f.title ?? f.description ?? f.summary);
|
|
820
|
-
const line = f.line ?? f.lineStart;
|
|
821
|
-
const label = file || `#${i + 1}`;
|
|
822
|
-
if (!file) problems.push(`finding #${i + 1} has no file`);
|
|
823
|
-
else if (!await citedPathExists(file)) fabricated.push(file);
|
|
824
|
-
if (!VALID_SEVERITIES.has(sev)) problems.push(`${label} has an invalid severity "${sev}"`);
|
|
825
|
-
if (!remediation) problems.push(`${label} has no remediation`);
|
|
826
|
-
if (!title) problems.push(`${label} has no title/description`);
|
|
827
|
-
if (typeof line !== "number") problems.push(`${label} is not pinned to a line`);
|
|
828
|
-
if (sev === "critical" || sev === "high") critHigh++;
|
|
829
|
-
}
|
|
830
|
-
if (fabricated.length > 0) {
|
|
831
|
-
return fail(
|
|
832
|
-
`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).`,
|
|
833
|
-
arr.length,
|
|
834
|
-
uniq(fabricated)
|
|
835
|
-
);
|
|
836
|
-
}
|
|
837
|
-
if (problems.length > 0) {
|
|
838
|
-
return fail(
|
|
839
|
-
`${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.`,
|
|
840
|
-
arr.length
|
|
841
|
-
);
|
|
842
|
-
}
|
|
843
|
-
const requiredSections = opts.requiredSections ?? DEFAULT_SECTIONS;
|
|
844
|
-
const missing = requiredSections.filter((s) => !hasSection(content, s));
|
|
845
|
-
if (missing.length > 0) {
|
|
846
|
-
return fail(
|
|
847
|
-
`the report is missing required section(s): ${missing.map((s) => `## ${s}`).join(", ")}.`,
|
|
848
|
-
arr.length
|
|
849
|
-
);
|
|
850
|
-
}
|
|
851
|
-
const themeThreshold = opts.themeThreshold ?? 3;
|
|
852
|
-
const minThemes = opts.minThemes ?? 2;
|
|
853
|
-
if (arr.length >= themeThreshold) {
|
|
854
|
-
const themes = sectionBody(content, "Systemic Themes");
|
|
855
|
-
const items = countThemeItems(themes);
|
|
856
|
-
if (items < minThemes) {
|
|
857
|
-
return fail(
|
|
858
|
-
`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.`,
|
|
859
|
-
arr.length
|
|
860
|
-
);
|
|
861
|
-
}
|
|
862
|
-
if (!/root[\s-]?cause/i.test(themes) || !/(blast[\s-]?radius|impact|scope|reach)/i.test(themes)) {
|
|
863
|
-
return fail(
|
|
864
|
-
`the "Systemic Themes" section must analyze each theme's root cause and blast radius \u2014 that analysis is absent.`,
|
|
865
|
-
arr.length
|
|
866
|
-
);
|
|
867
|
-
}
|
|
868
|
-
}
|
|
869
|
-
const verdict = sectionBody(content, "Verdict").toLowerCase();
|
|
870
|
-
if (critHigh > 0 && /(safe[\s-]?to[\s-]?merge|no (security )?(issues|findings|vulnerabilities)|looks secure|no concerns|all clear)/.test(
|
|
871
|
-
verdict
|
|
872
|
-
) && !/(block|after[\s-]?fix|merge[\s-]?after|do not merge|concern|open (critical|high))/.test(
|
|
873
|
-
verdict
|
|
874
|
-
)) {
|
|
875
|
-
return fail(
|
|
876
|
-
`the verdict reads "safe" but there ${critHigh === 1 ? "is" : "are"} ${critHigh} critical/high finding(s) \u2014 the verdict must reflect the open severity.`,
|
|
877
|
-
arr.length
|
|
878
|
-
);
|
|
1200
|
+
checked,
|
|
1201
|
+
invented
|
|
1202
|
+
};
|
|
1203
|
+
}
|
|
1204
|
+
if (invented.length > 0) {
|
|
1205
|
+
return {
|
|
1206
|
+
ok: false,
|
|
1207
|
+
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.`,
|
|
1208
|
+
checked,
|
|
1209
|
+
invented
|
|
1210
|
+
};
|
|
879
1211
|
}
|
|
880
1212
|
return {
|
|
881
1213
|
ok: true,
|
|
882
|
-
detail: `
|
|
883
|
-
|
|
884
|
-
|
|
1214
|
+
detail: `all ${checked} output value(s) trace back to the sources`,
|
|
1215
|
+
checked,
|
|
1216
|
+
invented
|
|
885
1217
|
};
|
|
886
1218
|
}
|
|
887
1219
|
|
|
888
|
-
// ../core/src/checks/
|
|
889
|
-
var
|
|
890
|
-
"
|
|
891
|
-
"
|
|
892
|
-
"Index coverage and method",
|
|
893
|
-
"Systemic themes",
|
|
1220
|
+
// ../core/src/checks/security-report.ts
|
|
1221
|
+
var DEFAULT_SECTIONS = [
|
|
1222
|
+
"Verdict",
|
|
1223
|
+
"Systemic Themes",
|
|
894
1224
|
"Findings",
|
|
895
|
-
"
|
|
896
|
-
"
|
|
897
|
-
"Verified sound",
|
|
898
|
-
"Not assessed",
|
|
899
|
-
"Suggested deeper reviews"
|
|
1225
|
+
"Verified Safe",
|
|
1226
|
+
"Not Statically Verifiable"
|
|
900
1227
|
];
|
|
901
|
-
var
|
|
902
|
-
function
|
|
1228
|
+
var VALID_SEVERITIES = /* @__PURE__ */ new Set(["critical", "high", "medium", "low", "info"]);
|
|
1229
|
+
function str(x) {
|
|
903
1230
|
return typeof x === "string" ? x.trim() : "";
|
|
904
1231
|
}
|
|
905
|
-
function
|
|
1232
|
+
function escapeRe(s) {
|
|
906
1233
|
return s.replace(/[.*+?^${}()|[\]\\]/g, "\\$&");
|
|
907
1234
|
}
|
|
908
|
-
function
|
|
909
|
-
return new RegExp(`^#{1,4}\\s+${
|
|
1235
|
+
function hasSection(content, name) {
|
|
1236
|
+
return new RegExp(`^#{1,4}\\s+${escapeRe(name)}\\b`, "im").test(content);
|
|
910
1237
|
}
|
|
911
|
-
function
|
|
912
|
-
const m = new RegExp(`^(#{1,4})\\s+${
|
|
1238
|
+
function sectionBody(content, name) {
|
|
1239
|
+
const m = new RegExp(`^(#{1,4})\\s+${escapeRe(name)}\\b`, "im").exec(content);
|
|
913
1240
|
if (!m) return "";
|
|
914
1241
|
const level = m[1].length;
|
|
915
1242
|
const start = m.index + m[0].length;
|
|
@@ -917,50 +1244,29 @@ function sectionBody2(content, name) {
|
|
|
917
1244
|
const next = new RegExp(`^#{1,${level}}\\s+\\S`, "m").exec(rest);
|
|
918
1245
|
return next ? rest.slice(0, next.index) : rest;
|
|
919
1246
|
}
|
|
920
|
-
function
|
|
1247
|
+
function countThemeItems(section) {
|
|
921
1248
|
let n = 0;
|
|
922
1249
|
for (const line of section.split(/\r?\n/)) {
|
|
923
1250
|
if (/^\s*(#{3,4}\s+\S|[-*]\s+\S|\d+\.\s+\S|\*\*[^*]+\*\*)/.test(line)) n++;
|
|
924
1251
|
}
|
|
925
1252
|
return n;
|
|
926
1253
|
}
|
|
927
|
-
function
|
|
928
|
-
const lines = section.split(/\r?\n/);
|
|
929
|
-
let headerAt = -1;
|
|
930
|
-
for (let i = 0; i < lines.length; i++) {
|
|
931
|
-
const line = lines[i];
|
|
932
|
-
if (/^\s*\|/.test(line) && /dimension/i.test(line) && /(grade|rating|score|health)/i.test(line)) {
|
|
933
|
-
headerAt = i;
|
|
934
|
-
break;
|
|
935
|
-
}
|
|
936
|
-
}
|
|
937
|
-
if (headerAt < 0) return -1;
|
|
938
|
-
let rows = 0;
|
|
939
|
-
for (let i = headerAt + 1; i < lines.length; i++) {
|
|
940
|
-
const line = lines[i];
|
|
941
|
-
if (!/^\s*\|/.test(line)) break;
|
|
942
|
-
if (/^\s*\|[\s:|-]+\|?\s*$/.test(line)) continue;
|
|
943
|
-
if (line.replace(/[|\s]/g, "").length === 0) continue;
|
|
944
|
-
rows++;
|
|
945
|
-
}
|
|
946
|
-
return rows;
|
|
947
|
-
}
|
|
948
|
-
function uniq2(xs) {
|
|
1254
|
+
function uniq(xs) {
|
|
949
1255
|
return [...new Set(xs)];
|
|
950
1256
|
}
|
|
951
|
-
async function
|
|
1257
|
+
async function securityReport(ws, reportFile, opts = {}) {
|
|
952
1258
|
const fail = (detail, count = 0, fabricated2 = []) => ({
|
|
953
1259
|
ok: false,
|
|
954
1260
|
detail,
|
|
955
1261
|
findingCount: count,
|
|
956
1262
|
fabricated: fabricated2
|
|
957
1263
|
});
|
|
958
|
-
const content = await
|
|
1264
|
+
const content = await ws.read(reportFile);
|
|
959
1265
|
if (content === null) {
|
|
960
|
-
return fail(`${reportFile} not found \u2014 write the
|
|
1266
|
+
return fail(`${reportFile} not found \u2014 write the security report before advancing.`);
|
|
961
1267
|
}
|
|
962
|
-
const findingsPath = opts.findings ?? "
|
|
963
|
-
const raw = await
|
|
1268
|
+
const findingsPath = opts.findings ?? "security-review/findings.json";
|
|
1269
|
+
const raw = await ws.read(findingsPath);
|
|
964
1270
|
if (raw === null) {
|
|
965
1271
|
return fail(
|
|
966
1272
|
`${findingsPath} not found \u2014 emit a machine-readable findings JSON alongside the report.`
|
|
@@ -978,498 +1284,266 @@ async function codebaseReviewReport(reports, workspace, reportFile, opts = {}) {
|
|
|
978
1284
|
if (!arr) {
|
|
979
1285
|
return fail(`${findingsPath} must be a JSON array of findings (or { "findings": [...] }).`);
|
|
980
1286
|
}
|
|
981
|
-
const citedPathExists = createCitedPathChecker(
|
|
1287
|
+
const citedPathExists = createCitedPathChecker(ws);
|
|
982
1288
|
const problems = [];
|
|
983
1289
|
const fabricated = [];
|
|
984
1290
|
let critHigh = 0;
|
|
985
1291
|
for (const [i, rawFinding] of arr.entries()) {
|
|
986
1292
|
const f = rawFinding ?? {};
|
|
987
|
-
const file =
|
|
988
|
-
const sev =
|
|
989
|
-
const remediation =
|
|
990
|
-
const title =
|
|
1293
|
+
const file = str(f.file ?? f.path);
|
|
1294
|
+
const sev = str(f.severity).toLowerCase();
|
|
1295
|
+
const remediation = str(f.remediation ?? f.fix ?? f.recommendation);
|
|
1296
|
+
const title = str(f.title ?? f.description ?? f.summary);
|
|
991
1297
|
const line = f.line ?? f.lineStart;
|
|
992
1298
|
const label = file || `#${i + 1}`;
|
|
993
1299
|
if (!file) problems.push(`finding #${i + 1} has no file`);
|
|
994
1300
|
else if (!await citedPathExists(file)) fabricated.push(file);
|
|
995
|
-
if (!
|
|
1301
|
+
if (!VALID_SEVERITIES.has(sev)) problems.push(`${label} has an invalid severity "${sev}"`);
|
|
996
1302
|
if (!remediation) problems.push(`${label} has no remediation`);
|
|
997
1303
|
if (!title) problems.push(`${label} has no title/description`);
|
|
998
|
-
if (
|
|
999
|
-
problems.push(`${label} is ${sev} but not pinned to a line`);
|
|
1000
|
-
}
|
|
1304
|
+
if (typeof line !== "number") problems.push(`${label} is not pinned to a line`);
|
|
1001
1305
|
if (sev === "critical" || sev === "high") critHigh++;
|
|
1002
1306
|
}
|
|
1003
1307
|
if (fabricated.length > 0) {
|
|
1004
1308
|
return fail(
|
|
1005
|
-
`findings cite ${fabricated.length} file(s) that don't exist in the workspace: ${
|
|
1309
|
+
`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).`,
|
|
1006
1310
|
arr.length,
|
|
1007
|
-
|
|
1311
|
+
uniq(fabricated)
|
|
1008
1312
|
);
|
|
1009
1313
|
}
|
|
1010
1314
|
if (problems.length > 0) {
|
|
1011
1315
|
return fail(
|
|
1012
|
-
`${problems.length} finding(s) are incomplete: ${problems.slice(0, 4).join("; ")} \u2014 every finding needs a real file, a severity,
|
|
1316
|
+
`${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.`,
|
|
1013
1317
|
arr.length
|
|
1014
1318
|
);
|
|
1015
1319
|
}
|
|
1016
|
-
const requiredSections = opts.requiredSections ??
|
|
1017
|
-
const missing = requiredSections.filter((s) => !
|
|
1320
|
+
const requiredSections = opts.requiredSections ?? DEFAULT_SECTIONS;
|
|
1321
|
+
const missing = requiredSections.filter((s) => !hasSection(content, s));
|
|
1018
1322
|
if (missing.length > 0) {
|
|
1019
1323
|
return fail(
|
|
1020
1324
|
`the report is missing required section(s): ${missing.map((s) => `## ${s}`).join(", ")}.`,
|
|
1021
1325
|
arr.length
|
|
1022
1326
|
);
|
|
1023
1327
|
}
|
|
1024
|
-
const minRows = opts.minScorecardRows ?? 4;
|
|
1025
|
-
const rows = scorecardRows(sectionBody2(content, "Scorecard"));
|
|
1026
|
-
if (rows < 0) {
|
|
1027
|
-
return fail(
|
|
1028
|
-
'the "Scorecard" section has no per-dimension table \u2014 add a markdown table with Dimension and Grade columns.',
|
|
1029
|
-
arr.length
|
|
1030
|
-
);
|
|
1031
|
-
}
|
|
1032
|
-
if (rows < minRows) {
|
|
1033
|
-
return fail(
|
|
1034
|
-
`the Scorecard table has ${rows} dimension row(s); grade at least ${minRows} dimensions (e.g. architecture, code quality, hygiene, security, tests).`,
|
|
1035
|
-
arr.length
|
|
1036
|
-
);
|
|
1037
|
-
}
|
|
1038
1328
|
const themeThreshold = opts.themeThreshold ?? 3;
|
|
1039
1329
|
const minThemes = opts.minThemes ?? 2;
|
|
1040
1330
|
if (arr.length >= themeThreshold) {
|
|
1041
|
-
const themes =
|
|
1042
|
-
const items =
|
|
1331
|
+
const themes = sectionBody(content, "Systemic Themes");
|
|
1332
|
+
const items = countThemeItems(themes);
|
|
1043
1333
|
if (items < minThemes) {
|
|
1044
1334
|
return fail(
|
|
1045
|
-
`the "Systemic
|
|
1046
|
-
arr.length
|
|
1047
|
-
);
|
|
1048
|
-
}
|
|
1049
|
-
if (!/root[\s-]?cause/i.test(themes) || !/(blast[\s-]?radius|impact|scope|reach)/i.test(themes)) {
|
|
1050
|
-
return fail(
|
|
1051
|
-
`the "Systemic themes" section must analyze each theme's root cause and blast radius \u2014 that analysis is absent.`,
|
|
1052
|
-
arr.length
|
|
1053
|
-
);
|
|
1054
|
-
}
|
|
1055
|
-
}
|
|
1056
|
-
const summary = sectionBody2(content, "Executive summary").toLowerCase();
|
|
1057
|
-
if (critHigh > 0 && /(excellent (health|shape|condition)|no (significant |material )?(issues|problems|findings)|nothing to fix|clean bill of health)/.test(
|
|
1058
|
-
summary
|
|
1059
|
-
) && !/(critical|high|however|but |concern|risk)/.test(summary)) {
|
|
1060
|
-
return fail(
|
|
1061
|
-
`the executive summary reads clean but there ${critHigh === 1 ? "is" : "are"} ${critHigh} critical/high finding(s) \u2014 the summary must reflect the open severity.`,
|
|
1062
|
-
arr.length
|
|
1063
|
-
);
|
|
1064
|
-
}
|
|
1065
|
-
return {
|
|
1066
|
-
ok: true,
|
|
1067
|
-
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" : ""}.`,
|
|
1068
|
-
findingCount: arr.length,
|
|
1069
|
-
fabricated: []
|
|
1070
|
-
};
|
|
1071
|
-
}
|
|
1072
|
-
|
|
1073
|
-
// ../core/src/checks/records.ts
|
|
1074
|
-
var ISO_DATE_RE = /^\d{4}-\d{2}-\d{2}$/;
|
|
1075
|
-
function isRealIsoDate(value) {
|
|
1076
|
-
if (!ISO_DATE_RE.test(value)) return false;
|
|
1077
|
-
const parts = value.split("-").map(Number);
|
|
1078
|
-
const y = parts[0] ?? 0;
|
|
1079
|
-
const m = parts[1] ?? 0;
|
|
1080
|
-
const d = parts[2] ?? 0;
|
|
1081
|
-
const dt = new Date(Date.UTC(y, m - 1, d));
|
|
1082
|
-
return dt.getUTCFullYear() === y && dt.getUTCMonth() === m - 1 && dt.getUTCDate() === d;
|
|
1083
|
-
}
|
|
1084
|
-
function checkCellType(value, type) {
|
|
1085
|
-
switch (type) {
|
|
1086
|
-
case "string":
|
|
1087
|
-
case "nonempty":
|
|
1088
|
-
return value.length > 0 ? { ok: true, detail: "" } : { ok: false, detail: "empty value" };
|
|
1089
|
-
case "number":
|
|
1090
|
-
return /^-?\d+(\.\d+)?$/.test(value) ? { ok: true, detail: "" } : { ok: false, detail: `"${value}" is not a number` };
|
|
1091
|
-
case "integer":
|
|
1092
|
-
return /^-?\d+$/.test(value) ? { ok: true, detail: "" } : { ok: false, detail: `"${value}" is not an integer` };
|
|
1093
|
-
case "boolean":
|
|
1094
|
-
return /^(true|false)$/i.test(value) ? { ok: true, detail: "" } : { ok: false, detail: `"${value}" is not a boolean` };
|
|
1095
|
-
case "date":
|
|
1096
|
-
case "iso-date":
|
|
1097
|
-
return isRealIsoDate(value) ? { ok: true, detail: "" } : { ok: false, detail: `"${value}" is not an ISO yyyy-mm-dd date` };
|
|
1098
|
-
case "email":
|
|
1099
|
-
return /^[^\s@]+@[^\s@]+\.[^\s@]+$/.test(value) ? { ok: true, detail: "" } : { ok: false, detail: `"${value}" is not an email` };
|
|
1100
|
-
default: {
|
|
1101
|
-
let re = null;
|
|
1102
|
-
try {
|
|
1103
|
-
re = new RegExp(type);
|
|
1104
|
-
} catch {
|
|
1105
|
-
return { ok: false, detail: `invalid column type /${type}/` };
|
|
1106
|
-
}
|
|
1107
|
-
return re.test(value) ? { ok: true, detail: "" } : { ok: false, detail: `"${value}" does not match /${type}/` };
|
|
1108
|
-
}
|
|
1109
|
-
}
|
|
1110
|
-
}
|
|
1111
|
-
function splitTableRow(line) {
|
|
1112
|
-
let s = line.trim();
|
|
1113
|
-
if (s.startsWith("|")) s = s.slice(1);
|
|
1114
|
-
if (s.endsWith("|")) s = s.slice(0, -1);
|
|
1115
|
-
return s.split(/(?<!\\)\|/).map((c) => c.trim().replace(/\\\|/g, "|"));
|
|
1116
|
-
}
|
|
1117
|
-
function isDelimiterRow(line) {
|
|
1118
|
-
if (!line.includes("-")) return false;
|
|
1119
|
-
const cells = splitTableRow(line);
|
|
1120
|
-
return cells.length > 0 && cells.every((c) => /^:?-{1,}:?$/.test(c.trim()));
|
|
1121
|
-
}
|
|
1122
|
-
function parseMarkdownTable(text) {
|
|
1123
|
-
const lines = text.split("\n");
|
|
1124
|
-
for (let i = 0; i < lines.length - 1; i++) {
|
|
1125
|
-
const header = lines[i];
|
|
1126
|
-
const delim = lines[i + 1];
|
|
1127
|
-
if (!header || !header.includes("|") || !delim || !isDelimiterRow(delim)) continue;
|
|
1128
|
-
const headers = splitTableRow(header);
|
|
1129
|
-
if (headers.length === 0) continue;
|
|
1130
|
-
const rows = [];
|
|
1131
|
-
for (let j = i + 2; j < lines.length; j++) {
|
|
1132
|
-
const l = lines[j];
|
|
1133
|
-
if (!l || l.trim() === "" || !l.includes("|")) break;
|
|
1134
|
-
rows.push(splitTableRow(l));
|
|
1135
|
-
}
|
|
1136
|
-
return { headers, rows };
|
|
1137
|
-
}
|
|
1138
|
-
return null;
|
|
1139
|
-
}
|
|
1140
|
-
function tableShape(text, spec) {
|
|
1141
|
-
const table = parseMarkdownTable(text);
|
|
1142
|
-
if (!table) {
|
|
1143
|
-
return {
|
|
1144
|
-
ok: false,
|
|
1145
|
-
detail: 'no Markdown table found \u2014 provide a "| header | \u2026 |" row with a "|---|" delimiter.',
|
|
1146
|
-
headers: [],
|
|
1147
|
-
rowCount: 0
|
|
1148
|
-
};
|
|
1149
|
-
}
|
|
1150
|
-
const headersLower = table.headers.map((h) => h.toLowerCase());
|
|
1151
|
-
const base = { headers: table.headers, rowCount: table.rows.length };
|
|
1152
|
-
for (const col of spec.requiredColumns ?? []) {
|
|
1153
|
-
if (!headersLower.includes(col.toLowerCase())) {
|
|
1154
|
-
return {
|
|
1155
|
-
ok: false,
|
|
1156
|
-
detail: `table is missing required column "${col}" \u2014 found: ${table.headers.join(", ")}.`,
|
|
1157
|
-
...base
|
|
1158
|
-
};
|
|
1159
|
-
}
|
|
1160
|
-
}
|
|
1161
|
-
const minRows = spec.minRows ?? 0;
|
|
1162
|
-
if (table.rows.length < minRows) {
|
|
1163
|
-
return {
|
|
1164
|
-
ok: false,
|
|
1165
|
-
detail: `table has ${table.rows.length} row(s), need \u2265 ${minRows}.`,
|
|
1166
|
-
...base
|
|
1167
|
-
};
|
|
1168
|
-
}
|
|
1169
|
-
for (const [col, type] of Object.entries(spec.columnTypes ?? {})) {
|
|
1170
|
-
const idx = headersLower.indexOf(col.toLowerCase());
|
|
1171
|
-
if (idx === -1) {
|
|
1172
|
-
return { ok: false, detail: `column-type check names unknown column "${col}".`, ...base };
|
|
1335
|
+
`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.`,
|
|
1336
|
+
arr.length
|
|
1337
|
+
);
|
|
1173
1338
|
}
|
|
1174
|
-
|
|
1175
|
-
|
|
1176
|
-
|
|
1177
|
-
|
|
1178
|
-
|
|
1179
|
-
}
|
|
1339
|
+
if (!/root[\s-]?cause/i.test(themes) || !/(blast[\s-]?radius|impact|scope|reach)/i.test(themes)) {
|
|
1340
|
+
return fail(
|
|
1341
|
+
`the "Systemic Themes" section must analyze each theme's root cause and blast radius \u2014 that analysis is absent.`,
|
|
1342
|
+
arr.length
|
|
1343
|
+
);
|
|
1180
1344
|
}
|
|
1181
1345
|
}
|
|
1346
|
+
const verdict = sectionBody(content, "Verdict").toLowerCase();
|
|
1347
|
+
if (critHigh > 0 && /(safe[\s-]?to[\s-]?merge|no (security )?(issues|findings|vulnerabilities)|looks secure|no concerns|all clear)/.test(
|
|
1348
|
+
verdict
|
|
1349
|
+
) && !/(block|after[\s-]?fix|merge[\s-]?after|do not merge|concern|open (critical|high))/.test(
|
|
1350
|
+
verdict
|
|
1351
|
+
)) {
|
|
1352
|
+
return fail(
|
|
1353
|
+
`the verdict reads "safe" but there ${critHigh === 1 ? "is" : "are"} ${critHigh} critical/high finding(s) \u2014 the verdict must reflect the open severity.`,
|
|
1354
|
+
arr.length
|
|
1355
|
+
);
|
|
1356
|
+
}
|
|
1182
1357
|
return {
|
|
1183
1358
|
ok: true,
|
|
1184
|
-
detail: `
|
|
1185
|
-
|
|
1359
|
+
detail: `security report OK: ${arr.length} finding(s), all cite real files, required sections present${arr.length >= themeThreshold ? ", systemic themes analyzed" : ""}.`,
|
|
1360
|
+
findingCount: arr.length,
|
|
1361
|
+
fabricated: []
|
|
1186
1362
|
};
|
|
1187
1363
|
}
|
|
1188
|
-
|
|
1189
|
-
|
|
1190
|
-
|
|
1191
|
-
|
|
1192
|
-
|
|
1193
|
-
|
|
1194
|
-
|
|
1195
|
-
|
|
1196
|
-
|
|
1197
|
-
|
|
1198
|
-
|
|
1199
|
-
|
|
1200
|
-
|
|
1201
|
-
|
|
1202
|
-
|
|
1203
|
-
|
|
1204
|
-
|
|
1205
|
-
}
|
|
1206
|
-
} else {
|
|
1207
|
-
cell += ch;
|
|
1208
|
-
}
|
|
1209
|
-
} else if (ch === '"') {
|
|
1210
|
-
inQuotes = true;
|
|
1211
|
-
} else if (ch === ",") {
|
|
1212
|
-
row.push(cell);
|
|
1213
|
-
cell = "";
|
|
1214
|
-
} else if (ch === "\n") {
|
|
1215
|
-
row.push(cell);
|
|
1216
|
-
rows.push(row);
|
|
1217
|
-
row = [];
|
|
1218
|
-
cell = "";
|
|
1219
|
-
} else if (ch !== "\r") {
|
|
1220
|
-
cell += ch;
|
|
1221
|
-
}
|
|
1222
|
-
}
|
|
1223
|
-
if (started && (cell.length > 0 || row.length > 0)) {
|
|
1224
|
-
row.push(cell);
|
|
1225
|
-
rows.push(row);
|
|
1226
|
-
}
|
|
1227
|
-
return rows;
|
|
1364
|
+
|
|
1365
|
+
// ../core/src/checks/codebase-review-report.ts
|
|
1366
|
+
var DEFAULT_SECTIONS2 = [
|
|
1367
|
+
"Executive summary",
|
|
1368
|
+
"Scorecard",
|
|
1369
|
+
"Index coverage and method",
|
|
1370
|
+
"Systemic themes",
|
|
1371
|
+
"Findings",
|
|
1372
|
+
"Quick wins",
|
|
1373
|
+
"Strategic recommendations",
|
|
1374
|
+
"Verified sound",
|
|
1375
|
+
"Not assessed",
|
|
1376
|
+
"Suggested deeper reviews"
|
|
1377
|
+
];
|
|
1378
|
+
var VALID_SEVERITIES2 = /* @__PURE__ */ new Set(["critical", "high", "medium", "low", "info"]);
|
|
1379
|
+
function str2(x) {
|
|
1380
|
+
return typeof x === "string" ? x.trim() : "";
|
|
1228
1381
|
}
|
|
1229
|
-
function
|
|
1230
|
-
|
|
1231
|
-
|
|
1232
|
-
|
|
1233
|
-
|
|
1234
|
-
|
|
1235
|
-
|
|
1236
|
-
|
|
1237
|
-
|
|
1238
|
-
|
|
1239
|
-
|
|
1240
|
-
|
|
1241
|
-
|
|
1242
|
-
|
|
1243
|
-
|
|
1244
|
-
|
|
1382
|
+
function escapeRe2(s) {
|
|
1383
|
+
return s.replace(/[.*+?^${}()|[\]\\]/g, "\\$&");
|
|
1384
|
+
}
|
|
1385
|
+
function hasSection2(content, name) {
|
|
1386
|
+
return new RegExp(`^#{1,4}\\s+${escapeRe2(name)}\\b`, "im").test(content);
|
|
1387
|
+
}
|
|
1388
|
+
function sectionBody2(content, name) {
|
|
1389
|
+
const m = new RegExp(`^(#{1,4})\\s+${escapeRe2(name)}\\b`, "im").exec(content);
|
|
1390
|
+
if (!m) return "";
|
|
1391
|
+
const level = m[1].length;
|
|
1392
|
+
const start = m.index + m[0].length;
|
|
1393
|
+
const rest = content.slice(start);
|
|
1394
|
+
const next = new RegExp(`^#{1,${level}}\\s+\\S`, "m").exec(rest);
|
|
1395
|
+
return next ? rest.slice(0, next.index) : rest;
|
|
1396
|
+
}
|
|
1397
|
+
function countThemeItems2(section) {
|
|
1398
|
+
let n = 0;
|
|
1399
|
+
for (const line of section.split(/\r?\n/)) {
|
|
1400
|
+
if (/^\s*(#{3,4}\s+\S|[-*]\s+\S|\d+\.\s+\S|\*\*[^*]+\*\*)/.test(line)) n++;
|
|
1245
1401
|
}
|
|
1246
|
-
return
|
|
1402
|
+
return n;
|
|
1247
1403
|
}
|
|
1248
|
-
function
|
|
1249
|
-
const
|
|
1250
|
-
|
|
1251
|
-
|
|
1252
|
-
|
|
1253
|
-
|
|
1254
|
-
|
|
1255
|
-
|
|
1256
|
-
return false;
|
|
1404
|
+
function scorecardRows(section) {
|
|
1405
|
+
const lines = section.split(/\r?\n/);
|
|
1406
|
+
let headerAt = -1;
|
|
1407
|
+
for (let i = 0; i < lines.length; i++) {
|
|
1408
|
+
const line = lines[i];
|
|
1409
|
+
if (/^\s*\|/.test(line) && /dimension/i.test(line) && /(grade|rating|score|health)/i.test(line)) {
|
|
1410
|
+
headerAt = i;
|
|
1411
|
+
break;
|
|
1257
1412
|
}
|
|
1258
1413
|
}
|
|
1259
|
-
|
|
1260
|
-
|
|
1261
|
-
|
|
1262
|
-
|
|
1263
|
-
|
|
1264
|
-
if (
|
|
1414
|
+
if (headerAt < 0) return -1;
|
|
1415
|
+
let rows = 0;
|
|
1416
|
+
for (let i = headerAt + 1; i < lines.length; i++) {
|
|
1417
|
+
const line = lines[i];
|
|
1418
|
+
if (!/^\s*\|/.test(line)) break;
|
|
1419
|
+
if (/^\s*\|[\s:|-]+\|?\s*$/.test(line)) continue;
|
|
1420
|
+
if (line.replace(/[|\s]/g, "").length === 0) continue;
|
|
1421
|
+
rows++;
|
|
1265
1422
|
}
|
|
1266
|
-
return
|
|
1267
|
-
}
|
|
1268
|
-
function csvShapeFailure(detail, headers = [], rowCount = 0) {
|
|
1269
|
-
return { ok: false, detail, headers, rowCount };
|
|
1423
|
+
return rows;
|
|
1270
1424
|
}
|
|
1271
|
-
function
|
|
1272
|
-
|
|
1273
|
-
return summary.length <= max ? summary : `${summary.slice(0, max - 3)}...`;
|
|
1425
|
+
function uniq2(xs) {
|
|
1426
|
+
return [...new Set(xs)];
|
|
1274
1427
|
}
|
|
1275
|
-
function
|
|
1276
|
-
|
|
1277
|
-
|
|
1278
|
-
|
|
1279
|
-
|
|
1280
|
-
|
|
1281
|
-
|
|
1282
|
-
|
|
1283
|
-
|
|
1284
|
-
|
|
1285
|
-
return csvShapeFailure("CSV has no rows");
|
|
1286
|
-
}
|
|
1287
|
-
const headers = (grid[0] ?? []).map((h) => h.trim());
|
|
1288
|
-
const dataRows = grid.slice(1);
|
|
1289
|
-
const base = { headers, rowCount: dataRows.length };
|
|
1290
|
-
if (headers.length === 0 || headers.every((h) => h.length === 0)) {
|
|
1291
|
-
return csvShapeFailure("CSV header row is empty", headers, dataRows.length);
|
|
1428
|
+
async function codebaseReviewReport(reports, workspace, reportFile, opts = {}) {
|
|
1429
|
+
const fail = (detail, count = 0, fabricated2 = []) => ({
|
|
1430
|
+
ok: false,
|
|
1431
|
+
detail,
|
|
1432
|
+
findingCount: count,
|
|
1433
|
+
fabricated: fabricated2
|
|
1434
|
+
});
|
|
1435
|
+
const content = await reports.read(reportFile);
|
|
1436
|
+
if (content === null) {
|
|
1437
|
+
return fail(`${reportFile} not found \u2014 write the codebase review report before advancing.`);
|
|
1292
1438
|
}
|
|
1293
|
-
|
|
1294
|
-
|
|
1295
|
-
|
|
1296
|
-
|
|
1297
|
-
|
|
1298
|
-
|
|
1299
|
-
detail: `CSV header should be exactly: ${expected.join(", ")}; found: ${headers.join(", ")}`,
|
|
1300
|
-
...base
|
|
1301
|
-
};
|
|
1302
|
-
}
|
|
1439
|
+
const findingsPath = opts.findings ?? "codebase-review-findings.json";
|
|
1440
|
+
const raw = await reports.read(findingsPath);
|
|
1441
|
+
if (raw === null) {
|
|
1442
|
+
return fail(
|
|
1443
|
+
`${findingsPath} not found \u2014 emit a machine-readable findings JSON alongside the report.`
|
|
1444
|
+
);
|
|
1303
1445
|
}
|
|
1304
|
-
|
|
1305
|
-
|
|
1306
|
-
|
|
1307
|
-
|
|
1308
|
-
|
|
1309
|
-
|
|
1310
|
-
|
|
1311
|
-
}
|
|
1446
|
+
let parsed;
|
|
1447
|
+
try {
|
|
1448
|
+
parsed = JSON.parse(raw);
|
|
1449
|
+
} catch (e) {
|
|
1450
|
+
return fail(
|
|
1451
|
+
`${findingsPath} is not valid JSON (${e instanceof Error ? e.message : "parse error"}). Emit a JSON array of findings.`
|
|
1452
|
+
);
|
|
1312
1453
|
}
|
|
1313
|
-
const
|
|
1314
|
-
if (
|
|
1315
|
-
return {
|
|
1316
|
-
ok: false,
|
|
1317
|
-
detail: `CSV has ${dataRows.length} data row(s), need \u2265 ${minRows}`,
|
|
1318
|
-
...base
|
|
1319
|
-
};
|
|
1454
|
+
const arr = Array.isArray(parsed) ? parsed : parsed && typeof parsed === "object" && Array.isArray(parsed.findings) ? parsed.findings : null;
|
|
1455
|
+
if (!arr) {
|
|
1456
|
+
return fail(`${findingsPath} must be a JSON array of findings (or { "findings": [...] }).`);
|
|
1320
1457
|
}
|
|
1321
|
-
|
|
1322
|
-
|
|
1323
|
-
|
|
1324
|
-
|
|
1325
|
-
|
|
1326
|
-
|
|
1327
|
-
|
|
1328
|
-
|
|
1329
|
-
|
|
1330
|
-
|
|
1458
|
+
const citedPathExists = createCitedPathChecker(workspace);
|
|
1459
|
+
const problems = [];
|
|
1460
|
+
const fabricated = [];
|
|
1461
|
+
let critHigh = 0;
|
|
1462
|
+
for (const [i, rawFinding] of arr.entries()) {
|
|
1463
|
+
const f = rawFinding ?? {};
|
|
1464
|
+
const file = str2(f.file ?? f.path);
|
|
1465
|
+
const sev = str2(f.severity).toLowerCase();
|
|
1466
|
+
const remediation = str2(f.remediation ?? f.fix ?? f.recommendation);
|
|
1467
|
+
const title = str2(f.title ?? f.description ?? f.summary);
|
|
1468
|
+
const line = f.line ?? f.lineStart;
|
|
1469
|
+
const label = file || `#${i + 1}`;
|
|
1470
|
+
if (!file) problems.push(`finding #${i + 1} has no file`);
|
|
1471
|
+
else if (!await citedPathExists(file)) fabricated.push(file);
|
|
1472
|
+
if (!VALID_SEVERITIES2.has(sev)) problems.push(`${label} has an invalid severity "${sev}"`);
|
|
1473
|
+
if (!remediation) problems.push(`${label} has no remediation`);
|
|
1474
|
+
if (!title) problems.push(`${label} has no title/description`);
|
|
1475
|
+
if ((sev === "critical" || sev === "high") && typeof line !== "number") {
|
|
1476
|
+
problems.push(`${label} is ${sev} but not pinned to a line`);
|
|
1331
1477
|
}
|
|
1478
|
+
if (sev === "critical" || sev === "high") critHigh++;
|
|
1332
1479
|
}
|
|
1333
|
-
|
|
1334
|
-
|
|
1335
|
-
|
|
1336
|
-
|
|
1337
|
-
|
|
1338
|
-
|
|
1339
|
-
...base
|
|
1340
|
-
};
|
|
1341
|
-
}
|
|
1342
|
-
for (let i = 0; i < dataRows.length; i++) {
|
|
1343
|
-
const value = (dataRows[i]?.[index] ?? "").trim();
|
|
1344
|
-
if (value.length > 0 && !allowedValues.includes(value)) {
|
|
1345
|
-
return {
|
|
1346
|
-
ok: false,
|
|
1347
|
-
detail: `CSV row ${i + 2} column "${column}" has "${value}", expected one of: ${allowedValues.join(", ")}`,
|
|
1348
|
-
...base
|
|
1349
|
-
};
|
|
1350
|
-
}
|
|
1351
|
-
}
|
|
1480
|
+
if (fabricated.length > 0) {
|
|
1481
|
+
return fail(
|
|
1482
|
+
`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).`,
|
|
1483
|
+
arr.length,
|
|
1484
|
+
uniq2(fabricated)
|
|
1485
|
+
);
|
|
1352
1486
|
}
|
|
1353
|
-
|
|
1354
|
-
|
|
1355
|
-
|
|
1356
|
-
|
|
1357
|
-
|
|
1358
|
-
}
|
|
1359
|
-
function recordSchema(text, spec) {
|
|
1360
|
-
if (text === null) {
|
|
1361
|
-
return {
|
|
1362
|
-
ok: false,
|
|
1363
|
-
detail: "deliverable not found \u2014 write the records file before advancing.",
|
|
1364
|
-
rowCount: 0
|
|
1365
|
-
};
|
|
1487
|
+
if (problems.length > 0) {
|
|
1488
|
+
return fail(
|
|
1489
|
+
`${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).`,
|
|
1490
|
+
arr.length
|
|
1491
|
+
);
|
|
1366
1492
|
}
|
|
1367
|
-
|
|
1368
|
-
|
|
1493
|
+
const requiredSections = opts.requiredSections ?? DEFAULT_SECTIONS2;
|
|
1494
|
+
const missing = requiredSections.filter((s) => !hasSection2(content, s));
|
|
1495
|
+
if (missing.length > 0) {
|
|
1496
|
+
return fail(
|
|
1497
|
+
`the report is missing required section(s): ${missing.map((s) => `## ${s}`).join(", ")}.`,
|
|
1498
|
+
arr.length
|
|
1499
|
+
);
|
|
1369
1500
|
}
|
|
1370
|
-
const
|
|
1371
|
-
const
|
|
1372
|
-
|
|
1373
|
-
|
|
1374
|
-
|
|
1375
|
-
|
|
1376
|
-
|
|
1377
|
-
} catch (err) {
|
|
1378
|
-
const msg = err instanceof Error ? err.message : String(err);
|
|
1379
|
-
return { ok: false, detail: `not valid JSON: ${msg.slice(0, 160)}`, rowCount: 0 };
|
|
1380
|
-
}
|
|
1381
|
-
if (!Array.isArray(parsed)) {
|
|
1382
|
-
return {
|
|
1383
|
-
ok: false,
|
|
1384
|
-
detail: `expected a top-level JSON array, got ${typeof parsed}`,
|
|
1385
|
-
rowCount: 0
|
|
1386
|
-
};
|
|
1387
|
-
}
|
|
1388
|
-
records = parsed;
|
|
1389
|
-
} else {
|
|
1390
|
-
const grid = parseCsv(text).filter((r) => r.some((c) => c.trim().length > 0));
|
|
1391
|
-
if (grid.length === 0) return { ok: false, detail: "CSV has no rows", rowCount: 0 };
|
|
1392
|
-
const headers = (grid[0] ?? []).map((h) => h.trim());
|
|
1393
|
-
records = grid.slice(1).map((cells) => {
|
|
1394
|
-
const o = {};
|
|
1395
|
-
headers.forEach((h, i) => {
|
|
1396
|
-
o[h] = (cells[i] ?? "").trim();
|
|
1397
|
-
});
|
|
1398
|
-
return o;
|
|
1399
|
-
});
|
|
1501
|
+
const minRows = opts.minScorecardRows ?? 4;
|
|
1502
|
+
const rows = scorecardRows(sectionBody2(content, "Scorecard"));
|
|
1503
|
+
if (rows < 0) {
|
|
1504
|
+
return fail(
|
|
1505
|
+
'the "Scorecard" section has no per-dimension table \u2014 add a markdown table with Dimension and Grade columns.',
|
|
1506
|
+
arr.length
|
|
1507
|
+
);
|
|
1400
1508
|
}
|
|
1401
|
-
|
|
1402
|
-
|
|
1403
|
-
|
|
1404
|
-
|
|
1405
|
-
|
|
1406
|
-
|
|
1407
|
-
|
|
1408
|
-
|
|
1409
|
-
|
|
1410
|
-
|
|
1411
|
-
|
|
1412
|
-
|
|
1413
|
-
|
|
1414
|
-
|
|
1415
|
-
|
|
1416
|
-
|
|
1417
|
-
if (required) {
|
|
1418
|
-
return {
|
|
1419
|
-
ok: false,
|
|
1420
|
-
detail: `record ${i} is missing required field "${f.name}"`,
|
|
1421
|
-
rowCount: records.length
|
|
1422
|
-
};
|
|
1423
|
-
}
|
|
1424
|
-
continue;
|
|
1425
|
-
}
|
|
1426
|
-
if (f.type && f.type !== "string") {
|
|
1427
|
-
const v = checkCellType(String(raw), f.type);
|
|
1428
|
-
if (!v.ok) {
|
|
1429
|
-
return {
|
|
1430
|
-
ok: false,
|
|
1431
|
-
detail: `record ${i} field "${f.name}": ${v.detail}`,
|
|
1432
|
-
rowCount: records.length
|
|
1433
|
-
};
|
|
1434
|
-
}
|
|
1435
|
-
}
|
|
1509
|
+
if (rows < minRows) {
|
|
1510
|
+
return fail(
|
|
1511
|
+
`the Scorecard table has ${rows} dimension row(s); grade at least ${minRows} dimensions (e.g. architecture, code quality, hygiene, security, tests).`,
|
|
1512
|
+
arr.length
|
|
1513
|
+
);
|
|
1514
|
+
}
|
|
1515
|
+
const themeThreshold = opts.themeThreshold ?? 3;
|
|
1516
|
+
const minThemes = opts.minThemes ?? 2;
|
|
1517
|
+
if (arr.length >= themeThreshold) {
|
|
1518
|
+
const themes = sectionBody2(content, "Systemic themes");
|
|
1519
|
+
const items = countThemeItems2(themes);
|
|
1520
|
+
if (items < minThemes) {
|
|
1521
|
+
return fail(
|
|
1522
|
+
`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.`,
|
|
1523
|
+
arr.length
|
|
1524
|
+
);
|
|
1436
1525
|
}
|
|
1437
|
-
if (
|
|
1438
|
-
|
|
1439
|
-
|
|
1440
|
-
|
|
1441
|
-
|
|
1442
|
-
detail: `record ${i} has unexpected field(s) ${extra.join(", ")} \u2014 allowed: ${fieldNames.join(", ")}`,
|
|
1443
|
-
rowCount: records.length
|
|
1444
|
-
};
|
|
1445
|
-
}
|
|
1526
|
+
if (!/root[\s-]?cause/i.test(themes) || !/(blast[\s-]?radius|impact|scope|reach)/i.test(themes)) {
|
|
1527
|
+
return fail(
|
|
1528
|
+
`the "Systemic themes" section must analyze each theme's root cause and blast radius \u2014 that analysis is absent.`,
|
|
1529
|
+
arr.length
|
|
1530
|
+
);
|
|
1446
1531
|
}
|
|
1447
1532
|
}
|
|
1448
|
-
|
|
1449
|
-
|
|
1450
|
-
|
|
1451
|
-
|
|
1452
|
-
|
|
1453
|
-
|
|
1454
|
-
|
|
1455
|
-
|
|
1456
|
-
const seen = /* @__PURE__ */ new Set();
|
|
1457
|
-
for (let i = 0; i < records.length; i++) {
|
|
1458
|
-
const key = String(records[i][spec.uniqueBy] ?? "");
|
|
1459
|
-
if (seen.has(key)) {
|
|
1460
|
-
return {
|
|
1461
|
-
ok: false,
|
|
1462
|
-
detail: `duplicate ${spec.uniqueBy} "${key}" at record ${i} \u2014 values must be unique`,
|
|
1463
|
-
rowCount: records.length
|
|
1464
|
-
};
|
|
1465
|
-
}
|
|
1466
|
-
seen.add(key);
|
|
1467
|
-
}
|
|
1533
|
+
const summary = sectionBody2(content, "Executive summary").toLowerCase();
|
|
1534
|
+
if (critHigh > 0 && /(excellent (health|shape|condition)|no (significant |material )?(issues|problems|findings)|nothing to fix|clean bill of health)/.test(
|
|
1535
|
+
summary
|
|
1536
|
+
) && !/(critical|high|however|but |concern|risk)/.test(summary)) {
|
|
1537
|
+
return fail(
|
|
1538
|
+
`the executive summary reads clean but there ${critHigh === 1 ? "is" : "are"} ${critHigh} critical/high finding(s) \u2014 the summary must reflect the open severity.`,
|
|
1539
|
+
arr.length
|
|
1540
|
+
);
|
|
1468
1541
|
}
|
|
1469
1542
|
return {
|
|
1470
1543
|
ok: true,
|
|
1471
|
-
detail:
|
|
1472
|
-
|
|
1544
|
+
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" : ""}.`,
|
|
1545
|
+
findingCount: arr.length,
|
|
1546
|
+
fabricated: []
|
|
1473
1547
|
};
|
|
1474
1548
|
}
|
|
1475
1549
|
|
|
@@ -1690,6 +1764,425 @@ function wrapperReturnHint(outputLines) {
|
|
|
1690
1764
|
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.`;
|
|
1691
1765
|
}
|
|
1692
1766
|
|
|
1767
|
+
// ../core/src/checks/figures.ts
|
|
1768
|
+
var MONEY_RE = /([-−]\s?)?([$€£])\s?(\d{1,3}(?:,\d{3})+|\d+)(?:\.(\d{1,2}))?(?![\d.,]*\d)/g;
|
|
1769
|
+
function parseMoney(text) {
|
|
1770
|
+
const out = [];
|
|
1771
|
+
for (const m of text.matchAll(MONEY_RE)) {
|
|
1772
|
+
const whole = Number(m[3].replace(/,/g, ""));
|
|
1773
|
+
const cents = m[4] ? Number(m[4].padEnd(2, "0")) / 100 : 0;
|
|
1774
|
+
const value = (whole + cents) * (m[1] ? -1 : 1);
|
|
1775
|
+
out.push({ value, symbol: m[2], index: m.index, end: m.index + m[0].length });
|
|
1776
|
+
}
|
|
1777
|
+
return out;
|
|
1778
|
+
}
|
|
1779
|
+
function extractMoneyAmounts(text) {
|
|
1780
|
+
return parseMoney(normalizeInlineEmphasis(text)).map((m) => Math.abs(m.value));
|
|
1781
|
+
}
|
|
1782
|
+
var same = (a, b) => Math.abs(a - b) < 5e-3;
|
|
1783
|
+
function fmt(value, symbol) {
|
|
1784
|
+
const abs = Math.abs(value).toLocaleString("en-US", {
|
|
1785
|
+
minimumFractionDigits: 2,
|
|
1786
|
+
maximumFractionDigits: 2
|
|
1787
|
+
});
|
|
1788
|
+
return `${value < 0 ? "-" : ""}${symbol}${abs}`;
|
|
1789
|
+
}
|
|
1790
|
+
var TOTAL_RE = /\b(sub-?\s?total|grand\s+total|total|amount\s+due|balance\s+due|totaal|subtotaal)\b/i;
|
|
1791
|
+
var SUBTOTAL_RE = /\bsub-?\s?total|\bsubtotaal/i;
|
|
1792
|
+
var DISCOUNT_RE = /\b(discount|korting|credit|coupon|less|deposit\s+paid|paid)\b/i;
|
|
1793
|
+
var TAX_RE = /\b(tax|vat|gst|hst|btw|sales\s+tax)\b/i;
|
|
1794
|
+
var PER_UNIT_RE = /\b(each|apiece|per|pp)\b|\/\s?(ea|each|person|head|dozen|hour|hr|unit)\b/i;
|
|
1795
|
+
var UNIT_COLUMN_RE = /\b(unit|each|per|rate)\b/i;
|
|
1796
|
+
var TOTAL_COLUMN_RE = /\b(total|subtotal|amount|line|sum)\b/i;
|
|
1797
|
+
var QTY_CELL_RE = /^\d+(?:\.\d+)?$/;
|
|
1798
|
+
var MONEY_SRC = "([$\u20AC\xA3])\\s?(\\d{1,3}(?:,\\d{3})+|\\d+)(?:\\.(\\d{1,2}))?";
|
|
1799
|
+
var QTY_TIMES_RE = new RegExp(
|
|
1800
|
+
`(\\d+(?:\\.\\d+)?)\\s*(?:\xD7|x|\\*|@)\\s*${MONEY_SRC}\\s*(?:=|\u2192|->)\\s*${MONEY_SRC}`,
|
|
1801
|
+
"i"
|
|
1802
|
+
);
|
|
1803
|
+
var TIMES_QTY_RE = new RegExp(
|
|
1804
|
+
`${MONEY_SRC}\\s*(?:\xD7|x|\\*)\\s*(\\d+(?:\\.\\d+)?)\\s*(?:=|\u2192|->)\\s*${MONEY_SRC}`,
|
|
1805
|
+
"i"
|
|
1806
|
+
);
|
|
1807
|
+
function moneyFrom(groups, at) {
|
|
1808
|
+
const whole = Number(groups[at + 1].replace(/,/g, ""));
|
|
1809
|
+
const cents = groups[at + 2] ? Number(groups[at + 2].padEnd(2, "0")) / 100 : 0;
|
|
1810
|
+
return whole + cents;
|
|
1811
|
+
}
|
|
1812
|
+
function cleanLabel(text) {
|
|
1813
|
+
return text.replace(/^\s*(?:[-*+]|\d+[.)])\s+/, "").replace(/[(\s:—–-]+$/, "").replace(/\s+/g, " ").trim();
|
|
1814
|
+
}
|
|
1815
|
+
function splitCells(line) {
|
|
1816
|
+
return line.trim().replace(/^\|/, "").replace(/\|$/, "").split("|").map((cell) => cell.trim());
|
|
1817
|
+
}
|
|
1818
|
+
function analyzeTableRow(no, cells, header, findings, checked) {
|
|
1819
|
+
const label = cleanLabel(cells[0] ?? "");
|
|
1820
|
+
const moneyCells = [];
|
|
1821
|
+
let qty = null;
|
|
1822
|
+
cells.forEach((cell, col) => {
|
|
1823
|
+
const money = parseMoney(cell);
|
|
1824
|
+
if (money.length === 1) moneyCells.push({ col, money: money[0] });
|
|
1825
|
+
else if (col > 0 && qty === null && QTY_CELL_RE.test(cell)) qty = Number(cell);
|
|
1826
|
+
});
|
|
1827
|
+
const total = TOTAL_RE.test(label) ? SUBTOTAL_RE.test(label) ? "subtotal" : "total" : null;
|
|
1828
|
+
const discount = DISCOUNT_RE.test(label);
|
|
1829
|
+
const amounts = moneyCells.map((c) => c.money);
|
|
1830
|
+
if (amounts.length === 0) {
|
|
1831
|
+
return { no, label, amounts, value: null, total, discount, unit: null };
|
|
1832
|
+
}
|
|
1833
|
+
const last = amounts[amounts.length - 1];
|
|
1834
|
+
if (moneyCells.length >= 2 && qty !== null) {
|
|
1835
|
+
const unit = amounts[0];
|
|
1836
|
+
checked.lineMath++;
|
|
1837
|
+
const expected = Math.round(qty * unit.value * 100) / 100;
|
|
1838
|
+
if (!same(expected, last.value)) {
|
|
1839
|
+
findings.push({
|
|
1840
|
+
kind: "line-math",
|
|
1841
|
+
line: no,
|
|
1842
|
+
message: `${label}: ${qty} \xD7 ${fmt(unit.value, unit.symbol)} is ${fmt(expected, unit.symbol)}, not ${fmt(last.value, last.symbol)}.`
|
|
1843
|
+
});
|
|
1844
|
+
}
|
|
1845
|
+
return { no, label, amounts, value: last.value, total, discount, unit };
|
|
1846
|
+
}
|
|
1847
|
+
if (moneyCells.length === 1 && qty !== null && !total) {
|
|
1848
|
+
const heading = header?.[moneyCells[0].col] ?? "";
|
|
1849
|
+
const isUnit = UNIT_COLUMN_RE.test(heading) && !TOTAL_COLUMN_RE.test(heading);
|
|
1850
|
+
const isLine = TOTAL_COLUMN_RE.test(heading);
|
|
1851
|
+
const value = isUnit ? Math.round(qty * last.value * 100) / 100 : isLine ? last.value : null;
|
|
1852
|
+
return { no, label, amounts, value, total, discount, unit: isUnit ? last : null };
|
|
1853
|
+
}
|
|
1854
|
+
return {
|
|
1855
|
+
no,
|
|
1856
|
+
label,
|
|
1857
|
+
amounts,
|
|
1858
|
+
value: last.value,
|
|
1859
|
+
total,
|
|
1860
|
+
discount,
|
|
1861
|
+
unit: total ? null : amounts[0]
|
|
1862
|
+
};
|
|
1863
|
+
}
|
|
1864
|
+
function analyzeTextLine(no, text, findings, checked) {
|
|
1865
|
+
const amounts = parseMoney(text);
|
|
1866
|
+
const label = cleanLabel(amounts.length > 0 ? text.slice(0, amounts[0].index) : text);
|
|
1867
|
+
const total = TOTAL_RE.test(label) ? SUBTOTAL_RE.test(label) ? "subtotal" : "total" : null;
|
|
1868
|
+
const discount = DISCOUNT_RE.test(label);
|
|
1869
|
+
if (amounts.length === 0) return { no, label, amounts, value: null, total, discount, unit: null };
|
|
1870
|
+
const forward = QTY_TIMES_RE.exec(text);
|
|
1871
|
+
const backward = forward ? null : TIMES_QTY_RE.exec(text);
|
|
1872
|
+
const math = forward ?? backward;
|
|
1873
|
+
if (math) {
|
|
1874
|
+
const groups = [...math];
|
|
1875
|
+
const qty = Number(forward ? groups[1] : groups[4]);
|
|
1876
|
+
const unitValue = moneyFrom(groups, forward ? 2 : 1);
|
|
1877
|
+
const lineValue = moneyFrom(groups, 5);
|
|
1878
|
+
const symbol = forward ? groups[2] : groups[1];
|
|
1879
|
+
const mathLabel = cleanLabel(text.slice(0, math.index)) || label || "A line";
|
|
1880
|
+
checked.lineMath++;
|
|
1881
|
+
const expected = Math.round(qty * unitValue * 100) / 100;
|
|
1882
|
+
if (!same(expected, lineValue)) {
|
|
1883
|
+
findings.push({
|
|
1884
|
+
kind: "line-math",
|
|
1885
|
+
line: no,
|
|
1886
|
+
message: `${mathLabel}: ${qty} \xD7 ${fmt(unitValue, symbol)} is ${fmt(expected, symbol)}, not ${fmt(lineValue, symbol)}.`
|
|
1887
|
+
});
|
|
1888
|
+
}
|
|
1889
|
+
const unit = amounts.find((m) => same(m.value, unitValue)) ?? amounts[0];
|
|
1890
|
+
return { no, label: mathLabel, amounts, value: lineValue, total, discount, unit };
|
|
1891
|
+
}
|
|
1892
|
+
if (amounts.length > 1) {
|
|
1893
|
+
return { no, label, amounts, value: null, total: null, discount, unit: null };
|
|
1894
|
+
}
|
|
1895
|
+
const only = amounts[0];
|
|
1896
|
+
if (!total && PER_UNIT_RE.test(text)) {
|
|
1897
|
+
return { no, label, amounts, value: null, total, discount, unit: only };
|
|
1898
|
+
}
|
|
1899
|
+
return { no, label, amounts, value: only.value, total, discount, unit: total ? null : only };
|
|
1900
|
+
}
|
|
1901
|
+
function candidateSums(run, breaks) {
|
|
1902
|
+
const starts = [0, ...breaks.filter((b) => b > 0 && b < run.length)];
|
|
1903
|
+
const sums = [];
|
|
1904
|
+
for (const start of starts) {
|
|
1905
|
+
const slice = run.slice(start);
|
|
1906
|
+
if (slice.length === 0) continue;
|
|
1907
|
+
if (slice.some((item) => item.value === null)) continue;
|
|
1908
|
+
const discounts = slice.filter((item) => item.discount && (item.value ?? 0) > 0);
|
|
1909
|
+
const base = slice.reduce((acc, item) => acc + (item.value ?? 0), 0);
|
|
1910
|
+
sums.push(base);
|
|
1911
|
+
const off = discounts.reduce((acc, item) => acc + (item.value ?? 0), 0);
|
|
1912
|
+
if (off > 0) sums.push(base - 2 * off);
|
|
1913
|
+
}
|
|
1914
|
+
return sums.length > 0 ? sums : null;
|
|
1915
|
+
}
|
|
1916
|
+
var MONTHS = {
|
|
1917
|
+
january: 1,
|
|
1918
|
+
jan: 1,
|
|
1919
|
+
february: 2,
|
|
1920
|
+
feb: 2,
|
|
1921
|
+
march: 3,
|
|
1922
|
+
mar: 3,
|
|
1923
|
+
april: 4,
|
|
1924
|
+
apr: 4,
|
|
1925
|
+
may: 5,
|
|
1926
|
+
june: 6,
|
|
1927
|
+
jun: 6,
|
|
1928
|
+
july: 7,
|
|
1929
|
+
jul: 7,
|
|
1930
|
+
august: 8,
|
|
1931
|
+
aug: 8,
|
|
1932
|
+
september: 9,
|
|
1933
|
+
sept: 9,
|
|
1934
|
+
sep: 9,
|
|
1935
|
+
october: 10,
|
|
1936
|
+
oct: 10,
|
|
1937
|
+
november: 11,
|
|
1938
|
+
nov: 11,
|
|
1939
|
+
december: 12,
|
|
1940
|
+
dec: 12
|
|
1941
|
+
};
|
|
1942
|
+
var WEEKDAYS = {
|
|
1943
|
+
sunday: 0,
|
|
1944
|
+
sun: 0,
|
|
1945
|
+
monday: 1,
|
|
1946
|
+
mon: 1,
|
|
1947
|
+
tuesday: 2,
|
|
1948
|
+
tue: 2,
|
|
1949
|
+
tues: 2,
|
|
1950
|
+
wednesday: 3,
|
|
1951
|
+
wed: 3,
|
|
1952
|
+
thursday: 4,
|
|
1953
|
+
thu: 4,
|
|
1954
|
+
thur: 4,
|
|
1955
|
+
thurs: 4,
|
|
1956
|
+
friday: 5,
|
|
1957
|
+
fri: 5,
|
|
1958
|
+
saturday: 6,
|
|
1959
|
+
sat: 6
|
|
1960
|
+
};
|
|
1961
|
+
var WEEKDAY_NAMES = [
|
|
1962
|
+
"Sunday",
|
|
1963
|
+
"Monday",
|
|
1964
|
+
"Tuesday",
|
|
1965
|
+
"Wednesday",
|
|
1966
|
+
"Thursday",
|
|
1967
|
+
"Friday",
|
|
1968
|
+
"Saturday"
|
|
1969
|
+
];
|
|
1970
|
+
var MONTH_NAMES = [
|
|
1971
|
+
"January",
|
|
1972
|
+
"February",
|
|
1973
|
+
"March",
|
|
1974
|
+
"April",
|
|
1975
|
+
"May",
|
|
1976
|
+
"June",
|
|
1977
|
+
"July",
|
|
1978
|
+
"August",
|
|
1979
|
+
"September",
|
|
1980
|
+
"October",
|
|
1981
|
+
"November",
|
|
1982
|
+
"December"
|
|
1983
|
+
];
|
|
1984
|
+
var WEEKDAY_SRC = "(Sunday|Monday|Tuesday|Wednesday|Thursday|Friday|Saturday|Sun|Mon|Tues|Tue|Wed|Thurs|Thur|Thu|Fri|Sat)\\.?,?\\s+";
|
|
1985
|
+
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)\\.?";
|
|
1986
|
+
var US_DATE_RE = new RegExp(
|
|
1987
|
+
`\\b(?:${WEEKDAY_SRC})?${MONTH_SRC}\\s+(\\d{1,2})(?:st|nd|rd|th)?\\b(?:,?\\s+(\\d{4}))?`,
|
|
1988
|
+
"gi"
|
|
1989
|
+
);
|
|
1990
|
+
var EU_DATE_RE = new RegExp(
|
|
1991
|
+
`\\b(?:${WEEKDAY_SRC})?(\\d{1,2})(?:st|nd|rd|th)?\\s+(?:of\\s+)?${MONTH_SRC}\\b(?:,?\\s+(\\d{4}))?`,
|
|
1992
|
+
"gi"
|
|
1993
|
+
);
|
|
1994
|
+
var ISO_DATE_RE2 = new RegExp(`\\b(?:${WEEKDAY_SRC})?(\\d{4})-(\\d{2})-(\\d{2})\\b`, "gi");
|
|
1995
|
+
var FUTURE_RE = /\b(due|by|deadline|confirm|deliver(?:y|ed)?|event|scheduled?|until|expires?|valid|starting|order|pick-?up|week of)\b/i;
|
|
1996
|
+
var PAST_RE = /\b(since|founded|established|est\.|opened|born|history|ago|last year|back in)\b/i;
|
|
1997
|
+
var STALE_DAYS = 180;
|
|
1998
|
+
function utcDay(year, month, day) {
|
|
1999
|
+
const at = Date.UTC(year, month - 1, day);
|
|
2000
|
+
const d = new Date(at);
|
|
2001
|
+
if (d.getUTCFullYear() !== year || d.getUTCMonth() !== month - 1 || d.getUTCDate() !== day) {
|
|
2002
|
+
return null;
|
|
2003
|
+
}
|
|
2004
|
+
return at;
|
|
2005
|
+
}
|
|
2006
|
+
function dateHits(text) {
|
|
2007
|
+
const hits = [];
|
|
2008
|
+
const seen = /* @__PURE__ */ new Set();
|
|
2009
|
+
const weekdayOf = (w) => w ? WEEKDAYS[w.toLowerCase().replace(/\.$/, "")] ?? null : null;
|
|
2010
|
+
for (const m of text.matchAll(US_DATE_RE)) {
|
|
2011
|
+
seen.add(m.index);
|
|
2012
|
+
hits.push({
|
|
2013
|
+
raw: m[0],
|
|
2014
|
+
weekday: weekdayOf(m[1]),
|
|
2015
|
+
month: MONTHS[m[2].toLowerCase()],
|
|
2016
|
+
day: Number(m[3]),
|
|
2017
|
+
year: m[4] ? Number(m[4]) : null
|
|
2018
|
+
});
|
|
2019
|
+
}
|
|
2020
|
+
for (const m of text.matchAll(EU_DATE_RE)) {
|
|
2021
|
+
if (seen.has(m.index)) continue;
|
|
2022
|
+
hits.push({
|
|
2023
|
+
raw: m[0],
|
|
2024
|
+
weekday: weekdayOf(m[1]),
|
|
2025
|
+
day: Number(m[2]),
|
|
2026
|
+
month: MONTHS[m[3].toLowerCase()],
|
|
2027
|
+
year: m[4] ? Number(m[4]) : null
|
|
2028
|
+
});
|
|
2029
|
+
}
|
|
2030
|
+
for (const m of text.matchAll(ISO_DATE_RE2)) {
|
|
2031
|
+
hits.push({
|
|
2032
|
+
raw: m[0],
|
|
2033
|
+
weekday: weekdayOf(m[1]),
|
|
2034
|
+
year: Number(m[2]),
|
|
2035
|
+
month: Number(m[3]),
|
|
2036
|
+
day: Number(m[4])
|
|
2037
|
+
});
|
|
2038
|
+
}
|
|
2039
|
+
return hits;
|
|
2040
|
+
}
|
|
2041
|
+
function checkDates(no, text, todayAt, findings, checked) {
|
|
2042
|
+
const todayYear = new Date(todayAt).getUTCFullYear();
|
|
2043
|
+
for (const hit of dateHits(text)) {
|
|
2044
|
+
let at = null;
|
|
2045
|
+
let year = hit.year;
|
|
2046
|
+
if (year !== null) {
|
|
2047
|
+
at = utcDay(year, hit.month, hit.day);
|
|
2048
|
+
} else {
|
|
2049
|
+
let best = null;
|
|
2050
|
+
for (const candidate of [todayYear - 1, todayYear, todayYear + 1]) {
|
|
2051
|
+
const cAt = utcDay(candidate, hit.month, hit.day);
|
|
2052
|
+
if (cAt === null) continue;
|
|
2053
|
+
if (!best || Math.abs(cAt - todayAt) < Math.abs(best.at - todayAt)) {
|
|
2054
|
+
best = { at: cAt, year: candidate };
|
|
2055
|
+
}
|
|
2056
|
+
}
|
|
2057
|
+
at = best?.at ?? null;
|
|
2058
|
+
year = best?.year ?? null;
|
|
2059
|
+
}
|
|
2060
|
+
if (at === null || year === null) continue;
|
|
2061
|
+
checked.dates++;
|
|
2062
|
+
const named = `${MONTH_NAMES[hit.month - 1]} ${hit.day}, ${year}`;
|
|
2063
|
+
if (hit.weekday !== null) {
|
|
2064
|
+
const actual = new Date(at).getUTCDay();
|
|
2065
|
+
if (actual !== hit.weekday) {
|
|
2066
|
+
findings.push({
|
|
2067
|
+
kind: "weekday",
|
|
2068
|
+
line: no,
|
|
2069
|
+
message: `${named} is a ${WEEKDAY_NAMES[actual]}, not a ${WEEKDAY_NAMES[hit.weekday]}.`
|
|
2070
|
+
});
|
|
2071
|
+
}
|
|
2072
|
+
}
|
|
2073
|
+
if (hit.year !== null && at < todayAt && !PAST_RE.test(text)) {
|
|
2074
|
+
const daysAgo = Math.round((todayAt - at) / 864e5);
|
|
2075
|
+
if (daysAgo > STALE_DAYS || daysAgo > 1 && FUTURE_RE.test(text)) {
|
|
2076
|
+
findings.push({
|
|
2077
|
+
kind: "stale-date",
|
|
2078
|
+
line: no,
|
|
2079
|
+
message: `${named} has already passed; check the date.`
|
|
2080
|
+
});
|
|
2081
|
+
}
|
|
2082
|
+
}
|
|
2083
|
+
}
|
|
2084
|
+
}
|
|
2085
|
+
function checkFigures(text, opts) {
|
|
2086
|
+
const findings = [];
|
|
2087
|
+
const checked = { sums: 0, lineMath: 0, dates: 0, prices: 0 };
|
|
2088
|
+
const [ty, tm, td] = opts.today.split("-").map(Number);
|
|
2089
|
+
const todayAt = utcDay(ty ?? 1970, tm ?? 1, td ?? 1) ?? 0;
|
|
2090
|
+
const known = opts.knownAmounts ?? [];
|
|
2091
|
+
const lines = [];
|
|
2092
|
+
let heading = null;
|
|
2093
|
+
let header = null;
|
|
2094
|
+
let run = [];
|
|
2095
|
+
let breaks = [];
|
|
2096
|
+
const statedTotals = [];
|
|
2097
|
+
const reset = () => {
|
|
2098
|
+
run = [];
|
|
2099
|
+
breaks = [];
|
|
2100
|
+
};
|
|
2101
|
+
const rawLines = text.split(/\r?\n/);
|
|
2102
|
+
for (let i = 0; i < rawLines.length; i++) {
|
|
2103
|
+
const no = i + 1;
|
|
2104
|
+
const raw = rawLines[i];
|
|
2105
|
+
const plain = normalizeInlineEmphasis(raw);
|
|
2106
|
+
checkDates(no, plain, todayAt, findings, checked);
|
|
2107
|
+
const trimmed = plain.trim();
|
|
2108
|
+
if (/^#{1,6}\s/.test(trimmed)) {
|
|
2109
|
+
heading = trimmed.replace(/^#+\s*/, "").trim();
|
|
2110
|
+
header = null;
|
|
2111
|
+
reset();
|
|
2112
|
+
}
|
|
2113
|
+
if (/^([-*_])(\s*\1){2,}\s*$/.test(trimmed)) {
|
|
2114
|
+
header = null;
|
|
2115
|
+
reset();
|
|
2116
|
+
continue;
|
|
2117
|
+
}
|
|
2118
|
+
if (trimmed.length === 0) continue;
|
|
2119
|
+
let line;
|
|
2120
|
+
if (trimmed.startsWith("|")) {
|
|
2121
|
+
if (/^\|?\s*:?-{2,}/.test(trimmed)) continue;
|
|
2122
|
+
const cells = splitCells(trimmed);
|
|
2123
|
+
line = analyzeTableRow(no, cells, header, findings, checked);
|
|
2124
|
+
if (line.amounts.length === 0 && !header) {
|
|
2125
|
+
header = cells;
|
|
2126
|
+
breaks.push(run.length);
|
|
2127
|
+
continue;
|
|
2128
|
+
}
|
|
2129
|
+
} else {
|
|
2130
|
+
if (header) header = null;
|
|
2131
|
+
line = analyzeTextLine(no, trimmed, findings, checked);
|
|
2132
|
+
}
|
|
2133
|
+
lines.push(line);
|
|
2134
|
+
if (line.total && line.value !== null) {
|
|
2135
|
+
const stated = line.value;
|
|
2136
|
+
statedTotals.push(stated);
|
|
2137
|
+
const sums = candidateSums(run, breaks);
|
|
2138
|
+
if (sums) {
|
|
2139
|
+
checked.sums++;
|
|
2140
|
+
if (!sums.some((sum) => same(sum, stated))) {
|
|
2141
|
+
const symbol = line.amounts[line.amounts.length - 1].symbol;
|
|
2142
|
+
const where = heading ? `Under "${heading}", the ` : "The ";
|
|
2143
|
+
findings.push({
|
|
2144
|
+
kind: "sum",
|
|
2145
|
+
line: no,
|
|
2146
|
+
message: `${where}${line.label.toLowerCase() || "total"} says ${fmt(stated, symbol)}, but the items above it add up to ${fmt(sums[0], symbol)}.`
|
|
2147
|
+
});
|
|
2148
|
+
}
|
|
2149
|
+
}
|
|
2150
|
+
if (line.total === "subtotal") {
|
|
2151
|
+
run = [{ value: stated, discount: false }];
|
|
2152
|
+
breaks = [];
|
|
2153
|
+
} else {
|
|
2154
|
+
reset();
|
|
2155
|
+
}
|
|
2156
|
+
continue;
|
|
2157
|
+
}
|
|
2158
|
+
if (line.amounts.length === 0) {
|
|
2159
|
+
breaks.push(run.length);
|
|
2160
|
+
continue;
|
|
2161
|
+
}
|
|
2162
|
+
run.push({ value: line.value, discount: line.discount });
|
|
2163
|
+
}
|
|
2164
|
+
if (known.length > 0) {
|
|
2165
|
+
for (const line of lines) {
|
|
2166
|
+
if (line.total || line.discount || !line.unit || TAX_RE.test(line.label)) continue;
|
|
2167
|
+
const price = Math.abs(line.unit.value);
|
|
2168
|
+
checked.prices++;
|
|
2169
|
+
if (known.some((k) => same(k, price))) continue;
|
|
2170
|
+
if (statedTotals.some((t) => same(t, price))) continue;
|
|
2171
|
+
const amount = fmt(price, line.unit.symbol);
|
|
2172
|
+
const item = line.label || "A line";
|
|
2173
|
+
findings.push({
|
|
2174
|
+
kind: "unsourced-price",
|
|
2175
|
+
line: line.no,
|
|
2176
|
+
item,
|
|
2177
|
+
amount,
|
|
2178
|
+
message: `${item} at ${amount}: this price didn't come from you.`
|
|
2179
|
+
});
|
|
2180
|
+
}
|
|
2181
|
+
}
|
|
2182
|
+
findings.sort((a, b) => a.line - b.line);
|
|
2183
|
+
return { findings, checked };
|
|
2184
|
+
}
|
|
2185
|
+
|
|
1693
2186
|
// ../core/src/checks/markdown.ts
|
|
1694
2187
|
function cleanHeading(text) {
|
|
1695
2188
|
return text.replace(/\s+#+\s*$/, "").replace(/\s+\{\[[^\]\r\n]+\]\}\s*$/, "").replace(/[*_`]/g, "").replace(/[–—]/g, "-").replace(/[^\p{L}\p{N}]+/gu, " ").trim().toLocaleLowerCase();
|
|
@@ -2245,6 +2738,7 @@ export {
|
|
|
2245
2738
|
MIN_INLINE_JS_BYTES,
|
|
2246
2739
|
MIN_JUDGE_EVIDENCE_SUBSTRING,
|
|
2247
2740
|
buildJudgePrompt,
|
|
2741
|
+
checkFigures,
|
|
2248
2742
|
citationsResolve,
|
|
2249
2743
|
citedPathKey,
|
|
2250
2744
|
cleanCitedPath,
|
|
@@ -2260,10 +2754,12 @@ export {
|
|
|
2260
2754
|
esmImports,
|
|
2261
2755
|
explainSniff,
|
|
2262
2756
|
extractInlineScripts,
|
|
2757
|
+
extractMoneyAmounts,
|
|
2263
2758
|
fileCountByExt,
|
|
2264
2759
|
fileMinBytes,
|
|
2265
2760
|
fileMinLines,
|
|
2266
2761
|
findImageRefs,
|
|
2762
|
+
frontmatterList,
|
|
2267
2763
|
gateResult,
|
|
2268
2764
|
grepMatches,
|
|
2269
2765
|
htmlCompleteSniff,
|
|
@@ -2273,6 +2769,7 @@ export {
|
|
|
2273
2769
|
isRealIsoDate,
|
|
2274
2770
|
jsonPathEquals,
|
|
2275
2771
|
jsonValid,
|
|
2772
|
+
listedFiles,
|
|
2276
2773
|
markdownHeadingsMatch,
|
|
2277
2774
|
mergeCorpusCoverageShards,
|
|
2278
2775
|
namedEntitiesConsistent,
|
|
@@ -2287,6 +2784,7 @@ export {
|
|
|
2287
2784
|
recordSchema,
|
|
2288
2785
|
requireOrderedSections,
|
|
2289
2786
|
resolveRelative,
|
|
2787
|
+
runSniff,
|
|
2290
2788
|
securityReport,
|
|
2291
2789
|
standaloneJsParses,
|
|
2292
2790
|
tableShape,
|