@bendyline/gezel-sdk 1.0.10 → 1.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +28 -1
- package/dist/checks.d.ts +66 -2
- package/dist/checks.js +1337 -846
- 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");
|
|
@@ -601,6 +1064,13 @@ function cleanCitation(raw) {
|
|
|
601
1064
|
function normalizeForKnownMatch(p) {
|
|
602
1065
|
return p.trim().replace(/\\/g, "/").replace(/^\.\//, "").replace(/^\//, "").replace(/^workspace\//i, "").replace(/\/+$/, "").toLowerCase();
|
|
603
1066
|
}
|
|
1067
|
+
function globMatchesKnown(cited, known) {
|
|
1068
|
+
if (!/[*?]/.test(cited)) return false;
|
|
1069
|
+
const pattern = normalizeForKnownMatch(cited).replace(/[.+^${}()|[\]\\]/g, "\\$&").replace(/\*/g, "[^/]*").replace(/\?/g, "[^/]");
|
|
1070
|
+
const re = new RegExp(`^${pattern}$`);
|
|
1071
|
+
for (const path of known) if (re.test(path)) return true;
|
|
1072
|
+
return false;
|
|
1073
|
+
}
|
|
604
1074
|
async function citationsResolve(ws, file, opts = {}) {
|
|
605
1075
|
const content = await ws.read(file);
|
|
606
1076
|
if (content === null) {
|
|
@@ -644,7 +1114,7 @@ async function citationsResolve(ws, file, opts = {}) {
|
|
|
644
1114
|
continue;
|
|
645
1115
|
}
|
|
646
1116
|
if (corpus?.has(c.toLowerCase()) || await citedPathExists(c)) resolved.push(c);
|
|
647
|
-
else if (known.has(normalizeForKnownMatch(c))) forgiven.push(c);
|
|
1117
|
+
else if (known.has(normalizeForKnownMatch(c)) || globMatchesKnown(c, known)) forgiven.push(c);
|
|
648
1118
|
else unresolved.push(c);
|
|
649
1119
|
}
|
|
650
1120
|
if (cites.length - forgiven.length < min) {
|
|
@@ -720,196 +1190,46 @@ function valuesSubsetOf(outputText, sourceTexts, spec) {
|
|
|
720
1190
|
return {
|
|
721
1191
|
ok: false,
|
|
722
1192
|
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
|
-
);
|
|
1193
|
+
checked,
|
|
1194
|
+
invented
|
|
1195
|
+
};
|
|
1196
|
+
}
|
|
1197
|
+
if (invented.length > 0) {
|
|
1198
|
+
return {
|
|
1199
|
+
ok: false,
|
|
1200
|
+
detail: `${invented.length} value(s) in the output appear in no source: ${invented.slice(0, 3).join(", ")}${invented.length > 3 ? ", \u2026" : ""} \u2014 copy identifiers verbatim from the source data; never renumber or invent them.`,
|
|
1201
|
+
checked,
|
|
1202
|
+
invented
|
|
1203
|
+
};
|
|
879
1204
|
}
|
|
880
1205
|
return {
|
|
881
1206
|
ok: true,
|
|
882
|
-
detail: `
|
|
883
|
-
|
|
884
|
-
|
|
1207
|
+
detail: `all ${checked} output value(s) trace back to the sources`,
|
|
1208
|
+
checked,
|
|
1209
|
+
invented
|
|
885
1210
|
};
|
|
886
1211
|
}
|
|
887
1212
|
|
|
888
|
-
// ../core/src/checks/
|
|
889
|
-
var
|
|
890
|
-
"
|
|
891
|
-
"
|
|
892
|
-
"Index coverage and method",
|
|
893
|
-
"Systemic themes",
|
|
1213
|
+
// ../core/src/checks/security-report.ts
|
|
1214
|
+
var DEFAULT_SECTIONS = [
|
|
1215
|
+
"Verdict",
|
|
1216
|
+
"Systemic Themes",
|
|
894
1217
|
"Findings",
|
|
895
|
-
"
|
|
896
|
-
"
|
|
897
|
-
"Verified sound",
|
|
898
|
-
"Not assessed",
|
|
899
|
-
"Suggested deeper reviews"
|
|
1218
|
+
"Verified Safe",
|
|
1219
|
+
"Not Statically Verifiable"
|
|
900
1220
|
];
|
|
901
|
-
var
|
|
902
|
-
function
|
|
1221
|
+
var VALID_SEVERITIES = /* @__PURE__ */ new Set(["critical", "high", "medium", "low", "info"]);
|
|
1222
|
+
function str(x) {
|
|
903
1223
|
return typeof x === "string" ? x.trim() : "";
|
|
904
1224
|
}
|
|
905
|
-
function
|
|
1225
|
+
function escapeRe(s) {
|
|
906
1226
|
return s.replace(/[.*+?^${}()|[\]\\]/g, "\\$&");
|
|
907
1227
|
}
|
|
908
|
-
function
|
|
909
|
-
return new RegExp(`^#{1,4}\\s+${
|
|
1228
|
+
function hasSection(content, name) {
|
|
1229
|
+
return new RegExp(`^#{1,4}\\s+${escapeRe(name)}\\b`, "im").test(content);
|
|
910
1230
|
}
|
|
911
|
-
function
|
|
912
|
-
const m = new RegExp(`^(#{1,4})\\s+${
|
|
1231
|
+
function sectionBody(content, name) {
|
|
1232
|
+
const m = new RegExp(`^(#{1,4})\\s+${escapeRe(name)}\\b`, "im").exec(content);
|
|
913
1233
|
if (!m) return "";
|
|
914
1234
|
const level = m[1].length;
|
|
915
1235
|
const start = m.index + m[0].length;
|
|
@@ -917,50 +1237,29 @@ function sectionBody2(content, name) {
|
|
|
917
1237
|
const next = new RegExp(`^#{1,${level}}\\s+\\S`, "m").exec(rest);
|
|
918
1238
|
return next ? rest.slice(0, next.index) : rest;
|
|
919
1239
|
}
|
|
920
|
-
function
|
|
1240
|
+
function countThemeItems(section) {
|
|
921
1241
|
let n = 0;
|
|
922
1242
|
for (const line of section.split(/\r?\n/)) {
|
|
923
1243
|
if (/^\s*(#{3,4}\s+\S|[-*]\s+\S|\d+\.\s+\S|\*\*[^*]+\*\*)/.test(line)) n++;
|
|
924
1244
|
}
|
|
925
1245
|
return n;
|
|
926
1246
|
}
|
|
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) {
|
|
1247
|
+
function uniq(xs) {
|
|
949
1248
|
return [...new Set(xs)];
|
|
950
1249
|
}
|
|
951
|
-
async function
|
|
1250
|
+
async function securityReport(ws, reportFile, opts = {}) {
|
|
952
1251
|
const fail = (detail, count = 0, fabricated2 = []) => ({
|
|
953
1252
|
ok: false,
|
|
954
1253
|
detail,
|
|
955
1254
|
findingCount: count,
|
|
956
1255
|
fabricated: fabricated2
|
|
957
1256
|
});
|
|
958
|
-
const content = await
|
|
1257
|
+
const content = await ws.read(reportFile);
|
|
959
1258
|
if (content === null) {
|
|
960
|
-
return fail(`${reportFile} not found \u2014 write the
|
|
1259
|
+
return fail(`${reportFile} not found \u2014 write the security report before advancing.`);
|
|
961
1260
|
}
|
|
962
|
-
const findingsPath = opts.findings ?? "
|
|
963
|
-
const raw = await
|
|
1261
|
+
const findingsPath = opts.findings ?? "security-review/findings.json";
|
|
1262
|
+
const raw = await ws.read(findingsPath);
|
|
964
1263
|
if (raw === null) {
|
|
965
1264
|
return fail(
|
|
966
1265
|
`${findingsPath} not found \u2014 emit a machine-readable findings JSON alongside the report.`
|
|
@@ -978,498 +1277,266 @@ async function codebaseReviewReport(reports, workspace, reportFile, opts = {}) {
|
|
|
978
1277
|
if (!arr) {
|
|
979
1278
|
return fail(`${findingsPath} must be a JSON array of findings (or { "findings": [...] }).`);
|
|
980
1279
|
}
|
|
981
|
-
const citedPathExists = createCitedPathChecker(
|
|
1280
|
+
const citedPathExists = createCitedPathChecker(ws);
|
|
982
1281
|
const problems = [];
|
|
983
1282
|
const fabricated = [];
|
|
984
1283
|
let critHigh = 0;
|
|
985
1284
|
for (const [i, rawFinding] of arr.entries()) {
|
|
986
1285
|
const f = rawFinding ?? {};
|
|
987
|
-
const file =
|
|
988
|
-
const sev =
|
|
989
|
-
const remediation =
|
|
990
|
-
const title =
|
|
1286
|
+
const file = str(f.file ?? f.path);
|
|
1287
|
+
const sev = str(f.severity).toLowerCase();
|
|
1288
|
+
const remediation = str(f.remediation ?? f.fix ?? f.recommendation);
|
|
1289
|
+
const title = str(f.title ?? f.description ?? f.summary);
|
|
991
1290
|
const line = f.line ?? f.lineStart;
|
|
992
1291
|
const label = file || `#${i + 1}`;
|
|
993
1292
|
if (!file) problems.push(`finding #${i + 1} has no file`);
|
|
994
1293
|
else if (!await citedPathExists(file)) fabricated.push(file);
|
|
995
|
-
if (!
|
|
1294
|
+
if (!VALID_SEVERITIES.has(sev)) problems.push(`${label} has an invalid severity "${sev}"`);
|
|
996
1295
|
if (!remediation) problems.push(`${label} has no remediation`);
|
|
997
1296
|
if (!title) problems.push(`${label} has no title/description`);
|
|
998
|
-
if (
|
|
999
|
-
problems.push(`${label} is ${sev} but not pinned to a line`);
|
|
1000
|
-
}
|
|
1297
|
+
if (typeof line !== "number") problems.push(`${label} is not pinned to a line`);
|
|
1001
1298
|
if (sev === "critical" || sev === "high") critHigh++;
|
|
1002
1299
|
}
|
|
1003
1300
|
if (fabricated.length > 0) {
|
|
1004
1301
|
return fail(
|
|
1005
|
-
`findings cite ${fabricated.length} file(s) that don't exist in the workspace: ${
|
|
1302
|
+
`findings cite ${fabricated.length} file(s) that don't exist in the workspace: ${uniq(fabricated).slice(0, 5).join(", ")} \u2014 every finding must point at a real file:line (no fabricated citations).`,
|
|
1006
1303
|
arr.length,
|
|
1007
|
-
|
|
1304
|
+
uniq(fabricated)
|
|
1008
1305
|
);
|
|
1009
1306
|
}
|
|
1010
1307
|
if (problems.length > 0) {
|
|
1011
1308
|
return fail(
|
|
1012
|
-
`${problems.length} finding(s) are incomplete: ${problems.slice(0, 4).join("; ")} \u2014 every finding needs a real file, a severity,
|
|
1309
|
+
`${problems.length} finding(s) are incomplete: ${problems.slice(0, 4).join("; ")} \u2014 every finding needs a real file:line, a severity, and a concrete remediation.`,
|
|
1013
1310
|
arr.length
|
|
1014
1311
|
);
|
|
1015
1312
|
}
|
|
1016
|
-
const requiredSections = opts.requiredSections ??
|
|
1017
|
-
const missing = requiredSections.filter((s) => !
|
|
1313
|
+
const requiredSections = opts.requiredSections ?? DEFAULT_SECTIONS;
|
|
1314
|
+
const missing = requiredSections.filter((s) => !hasSection(content, s));
|
|
1018
1315
|
if (missing.length > 0) {
|
|
1019
1316
|
return fail(
|
|
1020
1317
|
`the report is missing required section(s): ${missing.map((s) => `## ${s}`).join(", ")}.`,
|
|
1021
1318
|
arr.length
|
|
1022
1319
|
);
|
|
1023
1320
|
}
|
|
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
1321
|
const themeThreshold = opts.themeThreshold ?? 3;
|
|
1039
1322
|
const minThemes = opts.minThemes ?? 2;
|
|
1040
1323
|
if (arr.length >= themeThreshold) {
|
|
1041
|
-
const themes =
|
|
1042
|
-
const items =
|
|
1324
|
+
const themes = sectionBody(content, "Systemic Themes");
|
|
1325
|
+
const items = countThemeItems(themes);
|
|
1043
1326
|
if (items < minThemes) {
|
|
1044
1327
|
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 };
|
|
1328
|
+
`the "Systemic Themes" section lists ${items} theme(s); a review with ${arr.length} findings needs \u2265 ${minThemes}, each naming a root cause and its blast radius.`,
|
|
1329
|
+
arr.length
|
|
1330
|
+
);
|
|
1173
1331
|
}
|
|
1174
|
-
|
|
1175
|
-
|
|
1176
|
-
|
|
1177
|
-
|
|
1178
|
-
|
|
1179
|
-
}
|
|
1332
|
+
if (!/root[\s-]?cause/i.test(themes) || !/(blast[\s-]?radius|impact|scope|reach)/i.test(themes)) {
|
|
1333
|
+
return fail(
|
|
1334
|
+
`the "Systemic Themes" section must analyze each theme's root cause and blast radius \u2014 that analysis is absent.`,
|
|
1335
|
+
arr.length
|
|
1336
|
+
);
|
|
1180
1337
|
}
|
|
1181
1338
|
}
|
|
1339
|
+
const verdict = sectionBody(content, "Verdict").toLowerCase();
|
|
1340
|
+
if (critHigh > 0 && /(safe[\s-]?to[\s-]?merge|no (security )?(issues|findings|vulnerabilities)|looks secure|no concerns|all clear)/.test(
|
|
1341
|
+
verdict
|
|
1342
|
+
) && !/(block|after[\s-]?fix|merge[\s-]?after|do not merge|concern|open (critical|high))/.test(
|
|
1343
|
+
verdict
|
|
1344
|
+
)) {
|
|
1345
|
+
return fail(
|
|
1346
|
+
`the verdict reads "safe" but there ${critHigh === 1 ? "is" : "are"} ${critHigh} critical/high finding(s) \u2014 the verdict must reflect the open severity.`,
|
|
1347
|
+
arr.length
|
|
1348
|
+
);
|
|
1349
|
+
}
|
|
1182
1350
|
return {
|
|
1183
1351
|
ok: true,
|
|
1184
|
-
detail: `
|
|
1185
|
-
|
|
1352
|
+
detail: `security report OK: ${arr.length} finding(s), all cite real files, required sections present${arr.length >= themeThreshold ? ", systemic themes analyzed" : ""}.`,
|
|
1353
|
+
findingCount: arr.length,
|
|
1354
|
+
fabricated: []
|
|
1186
1355
|
};
|
|
1187
1356
|
}
|
|
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;
|
|
1357
|
+
|
|
1358
|
+
// ../core/src/checks/codebase-review-report.ts
|
|
1359
|
+
var DEFAULT_SECTIONS2 = [
|
|
1360
|
+
"Executive summary",
|
|
1361
|
+
"Scorecard",
|
|
1362
|
+
"Index coverage and method",
|
|
1363
|
+
"Systemic themes",
|
|
1364
|
+
"Findings",
|
|
1365
|
+
"Quick wins",
|
|
1366
|
+
"Strategic recommendations",
|
|
1367
|
+
"Verified sound",
|
|
1368
|
+
"Not assessed",
|
|
1369
|
+
"Suggested deeper reviews"
|
|
1370
|
+
];
|
|
1371
|
+
var VALID_SEVERITIES2 = /* @__PURE__ */ new Set(["critical", "high", "medium", "low", "info"]);
|
|
1372
|
+
function str2(x) {
|
|
1373
|
+
return typeof x === "string" ? x.trim() : "";
|
|
1228
1374
|
}
|
|
1229
|
-
function
|
|
1230
|
-
|
|
1231
|
-
|
|
1232
|
-
|
|
1233
|
-
|
|
1234
|
-
|
|
1235
|
-
|
|
1236
|
-
|
|
1237
|
-
|
|
1238
|
-
|
|
1239
|
-
|
|
1240
|
-
|
|
1241
|
-
|
|
1242
|
-
|
|
1243
|
-
|
|
1244
|
-
|
|
1375
|
+
function escapeRe2(s) {
|
|
1376
|
+
return s.replace(/[.*+?^${}()|[\]\\]/g, "\\$&");
|
|
1377
|
+
}
|
|
1378
|
+
function hasSection2(content, name) {
|
|
1379
|
+
return new RegExp(`^#{1,4}\\s+${escapeRe2(name)}\\b`, "im").test(content);
|
|
1380
|
+
}
|
|
1381
|
+
function sectionBody2(content, name) {
|
|
1382
|
+
const m = new RegExp(`^(#{1,4})\\s+${escapeRe2(name)}\\b`, "im").exec(content);
|
|
1383
|
+
if (!m) return "";
|
|
1384
|
+
const level = m[1].length;
|
|
1385
|
+
const start = m.index + m[0].length;
|
|
1386
|
+
const rest = content.slice(start);
|
|
1387
|
+
const next = new RegExp(`^#{1,${level}}\\s+\\S`, "m").exec(rest);
|
|
1388
|
+
return next ? rest.slice(0, next.index) : rest;
|
|
1389
|
+
}
|
|
1390
|
+
function countThemeItems2(section) {
|
|
1391
|
+
let n = 0;
|
|
1392
|
+
for (const line of section.split(/\r?\n/)) {
|
|
1393
|
+
if (/^\s*(#{3,4}\s+\S|[-*]\s+\S|\d+\.\s+\S|\*\*[^*]+\*\*)/.test(line)) n++;
|
|
1245
1394
|
}
|
|
1246
|
-
return
|
|
1395
|
+
return n;
|
|
1247
1396
|
}
|
|
1248
|
-
function
|
|
1249
|
-
const
|
|
1250
|
-
|
|
1251
|
-
|
|
1252
|
-
|
|
1253
|
-
|
|
1254
|
-
|
|
1255
|
-
|
|
1256
|
-
return false;
|
|
1397
|
+
function scorecardRows(section) {
|
|
1398
|
+
const lines = section.split(/\r?\n/);
|
|
1399
|
+
let headerAt = -1;
|
|
1400
|
+
for (let i = 0; i < lines.length; i++) {
|
|
1401
|
+
const line = lines[i];
|
|
1402
|
+
if (/^\s*\|/.test(line) && /dimension/i.test(line) && /(grade|rating|score|health)/i.test(line)) {
|
|
1403
|
+
headerAt = i;
|
|
1404
|
+
break;
|
|
1257
1405
|
}
|
|
1258
1406
|
}
|
|
1259
|
-
|
|
1260
|
-
|
|
1261
|
-
|
|
1262
|
-
|
|
1263
|
-
|
|
1264
|
-
if (
|
|
1407
|
+
if (headerAt < 0) return -1;
|
|
1408
|
+
let rows = 0;
|
|
1409
|
+
for (let i = headerAt + 1; i < lines.length; i++) {
|
|
1410
|
+
const line = lines[i];
|
|
1411
|
+
if (!/^\s*\|/.test(line)) break;
|
|
1412
|
+
if (/^\s*\|[\s:|-]+\|?\s*$/.test(line)) continue;
|
|
1413
|
+
if (line.replace(/[|\s]/g, "").length === 0) continue;
|
|
1414
|
+
rows++;
|
|
1265
1415
|
}
|
|
1266
|
-
return
|
|
1267
|
-
}
|
|
1268
|
-
function csvShapeFailure(detail, headers = [], rowCount = 0) {
|
|
1269
|
-
return { ok: false, detail, headers, rowCount };
|
|
1416
|
+
return rows;
|
|
1270
1417
|
}
|
|
1271
|
-
function
|
|
1272
|
-
|
|
1273
|
-
return summary.length <= max ? summary : `${summary.slice(0, max - 3)}...`;
|
|
1418
|
+
function uniq2(xs) {
|
|
1419
|
+
return [...new Set(xs)];
|
|
1274
1420
|
}
|
|
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);
|
|
1421
|
+
async function codebaseReviewReport(reports, workspace, reportFile, opts = {}) {
|
|
1422
|
+
const fail = (detail, count = 0, fabricated2 = []) => ({
|
|
1423
|
+
ok: false,
|
|
1424
|
+
detail,
|
|
1425
|
+
findingCount: count,
|
|
1426
|
+
fabricated: fabricated2
|
|
1427
|
+
});
|
|
1428
|
+
const content = await reports.read(reportFile);
|
|
1429
|
+
if (content === null) {
|
|
1430
|
+
return fail(`${reportFile} not found \u2014 write the codebase review report before advancing.`);
|
|
1292
1431
|
}
|
|
1293
|
-
|
|
1294
|
-
|
|
1295
|
-
|
|
1296
|
-
|
|
1297
|
-
|
|
1298
|
-
|
|
1299
|
-
detail: `CSV header should be exactly: ${expected.join(", ")}; found: ${headers.join(", ")}`,
|
|
1300
|
-
...base
|
|
1301
|
-
};
|
|
1302
|
-
}
|
|
1432
|
+
const findingsPath = opts.findings ?? "codebase-review-findings.json";
|
|
1433
|
+
const raw = await reports.read(findingsPath);
|
|
1434
|
+
if (raw === null) {
|
|
1435
|
+
return fail(
|
|
1436
|
+
`${findingsPath} not found \u2014 emit a machine-readable findings JSON alongside the report.`
|
|
1437
|
+
);
|
|
1303
1438
|
}
|
|
1304
|
-
|
|
1305
|
-
|
|
1306
|
-
|
|
1307
|
-
|
|
1308
|
-
|
|
1309
|
-
|
|
1310
|
-
|
|
1311
|
-
}
|
|
1439
|
+
let parsed;
|
|
1440
|
+
try {
|
|
1441
|
+
parsed = JSON.parse(raw);
|
|
1442
|
+
} catch (e) {
|
|
1443
|
+
return fail(
|
|
1444
|
+
`${findingsPath} is not valid JSON (${e instanceof Error ? e.message : "parse error"}). Emit a JSON array of findings.`
|
|
1445
|
+
);
|
|
1312
1446
|
}
|
|
1313
|
-
const
|
|
1314
|
-
if (
|
|
1315
|
-
return {
|
|
1316
|
-
ok: false,
|
|
1317
|
-
detail: `CSV has ${dataRows.length} data row(s), need \u2265 ${minRows}`,
|
|
1318
|
-
...base
|
|
1319
|
-
};
|
|
1447
|
+
const arr = Array.isArray(parsed) ? parsed : parsed && typeof parsed === "object" && Array.isArray(parsed.findings) ? parsed.findings : null;
|
|
1448
|
+
if (!arr) {
|
|
1449
|
+
return fail(`${findingsPath} must be a JSON array of findings (or { "findings": [...] }).`);
|
|
1320
1450
|
}
|
|
1321
|
-
|
|
1322
|
-
|
|
1323
|
-
|
|
1324
|
-
|
|
1325
|
-
|
|
1326
|
-
|
|
1327
|
-
|
|
1328
|
-
|
|
1329
|
-
|
|
1330
|
-
|
|
1451
|
+
const citedPathExists = createCitedPathChecker(workspace);
|
|
1452
|
+
const problems = [];
|
|
1453
|
+
const fabricated = [];
|
|
1454
|
+
let critHigh = 0;
|
|
1455
|
+
for (const [i, rawFinding] of arr.entries()) {
|
|
1456
|
+
const f = rawFinding ?? {};
|
|
1457
|
+
const file = str2(f.file ?? f.path);
|
|
1458
|
+
const sev = str2(f.severity).toLowerCase();
|
|
1459
|
+
const remediation = str2(f.remediation ?? f.fix ?? f.recommendation);
|
|
1460
|
+
const title = str2(f.title ?? f.description ?? f.summary);
|
|
1461
|
+
const line = f.line ?? f.lineStart;
|
|
1462
|
+
const label = file || `#${i + 1}`;
|
|
1463
|
+
if (!file) problems.push(`finding #${i + 1} has no file`);
|
|
1464
|
+
else if (!await citedPathExists(file)) fabricated.push(file);
|
|
1465
|
+
if (!VALID_SEVERITIES2.has(sev)) problems.push(`${label} has an invalid severity "${sev}"`);
|
|
1466
|
+
if (!remediation) problems.push(`${label} has no remediation`);
|
|
1467
|
+
if (!title) problems.push(`${label} has no title/description`);
|
|
1468
|
+
if ((sev === "critical" || sev === "high") && typeof line !== "number") {
|
|
1469
|
+
problems.push(`${label} is ${sev} but not pinned to a line`);
|
|
1331
1470
|
}
|
|
1471
|
+
if (sev === "critical" || sev === "high") critHigh++;
|
|
1332
1472
|
}
|
|
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
|
-
}
|
|
1473
|
+
if (fabricated.length > 0) {
|
|
1474
|
+
return fail(
|
|
1475
|
+
`findings cite ${fabricated.length} file(s) that don't exist in the workspace: ${uniq2(fabricated).slice(0, 5).join(", ")} \u2014 every finding must point at a real file (no fabricated citations).`,
|
|
1476
|
+
arr.length,
|
|
1477
|
+
uniq2(fabricated)
|
|
1478
|
+
);
|
|
1352
1479
|
}
|
|
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
|
-
};
|
|
1480
|
+
if (problems.length > 0) {
|
|
1481
|
+
return fail(
|
|
1482
|
+
`${problems.length} finding(s) are incomplete: ${problems.slice(0, 4).join("; ")} \u2014 every finding needs a real file, a severity, a title, and a concrete remediation (critical/high pinned to a line).`,
|
|
1483
|
+
arr.length
|
|
1484
|
+
);
|
|
1366
1485
|
}
|
|
1367
|
-
|
|
1368
|
-
|
|
1486
|
+
const requiredSections = opts.requiredSections ?? DEFAULT_SECTIONS2;
|
|
1487
|
+
const missing = requiredSections.filter((s) => !hasSection2(content, s));
|
|
1488
|
+
if (missing.length > 0) {
|
|
1489
|
+
return fail(
|
|
1490
|
+
`the report is missing required section(s): ${missing.map((s) => `## ${s}`).join(", ")}.`,
|
|
1491
|
+
arr.length
|
|
1492
|
+
);
|
|
1369
1493
|
}
|
|
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
|
-
});
|
|
1494
|
+
const minRows = opts.minScorecardRows ?? 4;
|
|
1495
|
+
const rows = scorecardRows(sectionBody2(content, "Scorecard"));
|
|
1496
|
+
if (rows < 0) {
|
|
1497
|
+
return fail(
|
|
1498
|
+
'the "Scorecard" section has no per-dimension table \u2014 add a markdown table with Dimension and Grade columns.',
|
|
1499
|
+
arr.length
|
|
1500
|
+
);
|
|
1400
1501
|
}
|
|
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
|
-
}
|
|
1502
|
+
if (rows < minRows) {
|
|
1503
|
+
return fail(
|
|
1504
|
+
`the Scorecard table has ${rows} dimension row(s); grade at least ${minRows} dimensions (e.g. architecture, code quality, hygiene, security, tests).`,
|
|
1505
|
+
arr.length
|
|
1506
|
+
);
|
|
1507
|
+
}
|
|
1508
|
+
const themeThreshold = opts.themeThreshold ?? 3;
|
|
1509
|
+
const minThemes = opts.minThemes ?? 2;
|
|
1510
|
+
if (arr.length >= themeThreshold) {
|
|
1511
|
+
const themes = sectionBody2(content, "Systemic themes");
|
|
1512
|
+
const items = countThemeItems2(themes);
|
|
1513
|
+
if (items < minThemes) {
|
|
1514
|
+
return fail(
|
|
1515
|
+
`the "Systemic themes" section lists ${items} theme(s); a review with ${arr.length} findings needs \u2265 ${minThemes}, each naming a root cause and its blast radius.`,
|
|
1516
|
+
arr.length
|
|
1517
|
+
);
|
|
1436
1518
|
}
|
|
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
|
-
}
|
|
1519
|
+
if (!/root[\s-]?cause/i.test(themes) || !/(blast[\s-]?radius|impact|scope|reach)/i.test(themes)) {
|
|
1520
|
+
return fail(
|
|
1521
|
+
`the "Systemic themes" section must analyze each theme's root cause and blast radius \u2014 that analysis is absent.`,
|
|
1522
|
+
arr.length
|
|
1523
|
+
);
|
|
1446
1524
|
}
|
|
1447
1525
|
}
|
|
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
|
-
}
|
|
1526
|
+
const summary = sectionBody2(content, "Executive summary").toLowerCase();
|
|
1527
|
+
if (critHigh > 0 && /(excellent (health|shape|condition)|no (significant |material )?(issues|problems|findings)|nothing to fix|clean bill of health)/.test(
|
|
1528
|
+
summary
|
|
1529
|
+
) && !/(critical|high|however|but |concern|risk)/.test(summary)) {
|
|
1530
|
+
return fail(
|
|
1531
|
+
`the executive summary reads clean but there ${critHigh === 1 ? "is" : "are"} ${critHigh} critical/high finding(s) \u2014 the summary must reflect the open severity.`,
|
|
1532
|
+
arr.length
|
|
1533
|
+
);
|
|
1468
1534
|
}
|
|
1469
1535
|
return {
|
|
1470
1536
|
ok: true,
|
|
1471
|
-
detail:
|
|
1472
|
-
|
|
1537
|
+
detail: `codebase review OK: ${arr.length} finding(s), all cite real files, ${rows} scorecard dimension(s), required sections present${arr.length >= themeThreshold ? ", systemic themes analyzed" : ""}.`,
|
|
1538
|
+
findingCount: arr.length,
|
|
1539
|
+
fabricated: []
|
|
1473
1540
|
};
|
|
1474
1541
|
}
|
|
1475
1542
|
|
|
@@ -1690,6 +1757,425 @@ function wrapperReturnHint(outputLines) {
|
|
|
1690
1757
|
return `Hint: the function is returning an OBJECT where the caller expects an ARRAY \u2014 it received a wrapper like \`${example}\`. Return the array itself (\`return result;\`), not an object with it nested inside.`;
|
|
1691
1758
|
}
|
|
1692
1759
|
|
|
1760
|
+
// ../core/src/checks/figures.ts
|
|
1761
|
+
var MONEY_RE = /([-−]\s?)?([$€£])\s?(\d{1,3}(?:,\d{3})+|\d+)(?:\.(\d{1,2}))?(?![\d.,]*\d)/g;
|
|
1762
|
+
function parseMoney(text) {
|
|
1763
|
+
const out = [];
|
|
1764
|
+
for (const m of text.matchAll(MONEY_RE)) {
|
|
1765
|
+
const whole = Number(m[3].replace(/,/g, ""));
|
|
1766
|
+
const cents = m[4] ? Number(m[4].padEnd(2, "0")) / 100 : 0;
|
|
1767
|
+
const value = (whole + cents) * (m[1] ? -1 : 1);
|
|
1768
|
+
out.push({ value, symbol: m[2], index: m.index, end: m.index + m[0].length });
|
|
1769
|
+
}
|
|
1770
|
+
return out;
|
|
1771
|
+
}
|
|
1772
|
+
function extractMoneyAmounts(text) {
|
|
1773
|
+
return parseMoney(normalizeInlineEmphasis(text)).map((m) => Math.abs(m.value));
|
|
1774
|
+
}
|
|
1775
|
+
var same = (a, b) => Math.abs(a - b) < 5e-3;
|
|
1776
|
+
function fmt(value, symbol) {
|
|
1777
|
+
const abs = Math.abs(value).toLocaleString("en-US", {
|
|
1778
|
+
minimumFractionDigits: 2,
|
|
1779
|
+
maximumFractionDigits: 2
|
|
1780
|
+
});
|
|
1781
|
+
return `${value < 0 ? "-" : ""}${symbol}${abs}`;
|
|
1782
|
+
}
|
|
1783
|
+
var TOTAL_RE = /\b(sub-?\s?total|grand\s+total|total|amount\s+due|balance\s+due|totaal|subtotaal)\b/i;
|
|
1784
|
+
var SUBTOTAL_RE = /\bsub-?\s?total|\bsubtotaal/i;
|
|
1785
|
+
var DISCOUNT_RE = /\b(discount|korting|credit|coupon|less|deposit\s+paid|paid)\b/i;
|
|
1786
|
+
var TAX_RE = /\b(tax|vat|gst|hst|btw|sales\s+tax)\b/i;
|
|
1787
|
+
var PER_UNIT_RE = /\b(each|apiece|per|pp)\b|\/\s?(ea|each|person|head|dozen|hour|hr|unit)\b/i;
|
|
1788
|
+
var UNIT_COLUMN_RE = /\b(unit|each|per|rate)\b/i;
|
|
1789
|
+
var TOTAL_COLUMN_RE = /\b(total|subtotal|amount|line|sum)\b/i;
|
|
1790
|
+
var QTY_CELL_RE = /^\d+(?:\.\d+)?$/;
|
|
1791
|
+
var MONEY_SRC = "([$\u20AC\xA3])\\s?(\\d{1,3}(?:,\\d{3})+|\\d+)(?:\\.(\\d{1,2}))?";
|
|
1792
|
+
var QTY_TIMES_RE = new RegExp(
|
|
1793
|
+
`(\\d+(?:\\.\\d+)?)\\s*(?:\xD7|x|\\*|@)\\s*${MONEY_SRC}\\s*(?:=|\u2192|->)\\s*${MONEY_SRC}`,
|
|
1794
|
+
"i"
|
|
1795
|
+
);
|
|
1796
|
+
var TIMES_QTY_RE = new RegExp(
|
|
1797
|
+
`${MONEY_SRC}\\s*(?:\xD7|x|\\*)\\s*(\\d+(?:\\.\\d+)?)\\s*(?:=|\u2192|->)\\s*${MONEY_SRC}`,
|
|
1798
|
+
"i"
|
|
1799
|
+
);
|
|
1800
|
+
function moneyFrom(groups, at) {
|
|
1801
|
+
const whole = Number(groups[at + 1].replace(/,/g, ""));
|
|
1802
|
+
const cents = groups[at + 2] ? Number(groups[at + 2].padEnd(2, "0")) / 100 : 0;
|
|
1803
|
+
return whole + cents;
|
|
1804
|
+
}
|
|
1805
|
+
function cleanLabel(text) {
|
|
1806
|
+
return text.replace(/^\s*(?:[-*+]|\d+[.)])\s+/, "").replace(/[(\s:—–-]+$/, "").replace(/\s+/g, " ").trim();
|
|
1807
|
+
}
|
|
1808
|
+
function splitCells(line) {
|
|
1809
|
+
return line.trim().replace(/^\|/, "").replace(/\|$/, "").split("|").map((cell) => cell.trim());
|
|
1810
|
+
}
|
|
1811
|
+
function analyzeTableRow(no, cells, header, findings, checked) {
|
|
1812
|
+
const label = cleanLabel(cells[0] ?? "");
|
|
1813
|
+
const moneyCells = [];
|
|
1814
|
+
let qty = null;
|
|
1815
|
+
cells.forEach((cell, col) => {
|
|
1816
|
+
const money = parseMoney(cell);
|
|
1817
|
+
if (money.length === 1) moneyCells.push({ col, money: money[0] });
|
|
1818
|
+
else if (col > 0 && qty === null && QTY_CELL_RE.test(cell)) qty = Number(cell);
|
|
1819
|
+
});
|
|
1820
|
+
const total = TOTAL_RE.test(label) ? SUBTOTAL_RE.test(label) ? "subtotal" : "total" : null;
|
|
1821
|
+
const discount = DISCOUNT_RE.test(label);
|
|
1822
|
+
const amounts = moneyCells.map((c) => c.money);
|
|
1823
|
+
if (amounts.length === 0) {
|
|
1824
|
+
return { no, label, amounts, value: null, total, discount, unit: null };
|
|
1825
|
+
}
|
|
1826
|
+
const last = amounts[amounts.length - 1];
|
|
1827
|
+
if (moneyCells.length >= 2 && qty !== null) {
|
|
1828
|
+
const unit = amounts[0];
|
|
1829
|
+
checked.lineMath++;
|
|
1830
|
+
const expected = Math.round(qty * unit.value * 100) / 100;
|
|
1831
|
+
if (!same(expected, last.value)) {
|
|
1832
|
+
findings.push({
|
|
1833
|
+
kind: "line-math",
|
|
1834
|
+
line: no,
|
|
1835
|
+
message: `${label}: ${qty} \xD7 ${fmt(unit.value, unit.symbol)} is ${fmt(expected, unit.symbol)}, not ${fmt(last.value, last.symbol)}.`
|
|
1836
|
+
});
|
|
1837
|
+
}
|
|
1838
|
+
return { no, label, amounts, value: last.value, total, discount, unit };
|
|
1839
|
+
}
|
|
1840
|
+
if (moneyCells.length === 1 && qty !== null && !total) {
|
|
1841
|
+
const heading = header?.[moneyCells[0].col] ?? "";
|
|
1842
|
+
const isUnit = UNIT_COLUMN_RE.test(heading) && !TOTAL_COLUMN_RE.test(heading);
|
|
1843
|
+
const isLine = TOTAL_COLUMN_RE.test(heading);
|
|
1844
|
+
const value = isUnit ? Math.round(qty * last.value * 100) / 100 : isLine ? last.value : null;
|
|
1845
|
+
return { no, label, amounts, value, total, discount, unit: isUnit ? last : null };
|
|
1846
|
+
}
|
|
1847
|
+
return {
|
|
1848
|
+
no,
|
|
1849
|
+
label,
|
|
1850
|
+
amounts,
|
|
1851
|
+
value: last.value,
|
|
1852
|
+
total,
|
|
1853
|
+
discount,
|
|
1854
|
+
unit: total ? null : amounts[0]
|
|
1855
|
+
};
|
|
1856
|
+
}
|
|
1857
|
+
function analyzeTextLine(no, text, findings, checked) {
|
|
1858
|
+
const amounts = parseMoney(text);
|
|
1859
|
+
const label = cleanLabel(amounts.length > 0 ? text.slice(0, amounts[0].index) : text);
|
|
1860
|
+
const total = TOTAL_RE.test(label) ? SUBTOTAL_RE.test(label) ? "subtotal" : "total" : null;
|
|
1861
|
+
const discount = DISCOUNT_RE.test(label);
|
|
1862
|
+
if (amounts.length === 0) return { no, label, amounts, value: null, total, discount, unit: null };
|
|
1863
|
+
const forward = QTY_TIMES_RE.exec(text);
|
|
1864
|
+
const backward = forward ? null : TIMES_QTY_RE.exec(text);
|
|
1865
|
+
const math = forward ?? backward;
|
|
1866
|
+
if (math) {
|
|
1867
|
+
const groups = [...math];
|
|
1868
|
+
const qty = Number(forward ? groups[1] : groups[4]);
|
|
1869
|
+
const unitValue = moneyFrom(groups, forward ? 2 : 1);
|
|
1870
|
+
const lineValue = moneyFrom(groups, 5);
|
|
1871
|
+
const symbol = forward ? groups[2] : groups[1];
|
|
1872
|
+
const mathLabel = cleanLabel(text.slice(0, math.index)) || label || "A line";
|
|
1873
|
+
checked.lineMath++;
|
|
1874
|
+
const expected = Math.round(qty * unitValue * 100) / 100;
|
|
1875
|
+
if (!same(expected, lineValue)) {
|
|
1876
|
+
findings.push({
|
|
1877
|
+
kind: "line-math",
|
|
1878
|
+
line: no,
|
|
1879
|
+
message: `${mathLabel}: ${qty} \xD7 ${fmt(unitValue, symbol)} is ${fmt(expected, symbol)}, not ${fmt(lineValue, symbol)}.`
|
|
1880
|
+
});
|
|
1881
|
+
}
|
|
1882
|
+
const unit = amounts.find((m) => same(m.value, unitValue)) ?? amounts[0];
|
|
1883
|
+
return { no, label: mathLabel, amounts, value: lineValue, total, discount, unit };
|
|
1884
|
+
}
|
|
1885
|
+
if (amounts.length > 1) {
|
|
1886
|
+
return { no, label, amounts, value: null, total: null, discount, unit: null };
|
|
1887
|
+
}
|
|
1888
|
+
const only = amounts[0];
|
|
1889
|
+
if (!total && PER_UNIT_RE.test(text)) {
|
|
1890
|
+
return { no, label, amounts, value: null, total, discount, unit: only };
|
|
1891
|
+
}
|
|
1892
|
+
return { no, label, amounts, value: only.value, total, discount, unit: total ? null : only };
|
|
1893
|
+
}
|
|
1894
|
+
function candidateSums(run, breaks) {
|
|
1895
|
+
const starts = [0, ...breaks.filter((b) => b > 0 && b < run.length)];
|
|
1896
|
+
const sums = [];
|
|
1897
|
+
for (const start of starts) {
|
|
1898
|
+
const slice = run.slice(start);
|
|
1899
|
+
if (slice.length === 0) continue;
|
|
1900
|
+
if (slice.some((item) => item.value === null)) continue;
|
|
1901
|
+
const discounts = slice.filter((item) => item.discount && (item.value ?? 0) > 0);
|
|
1902
|
+
const base = slice.reduce((acc, item) => acc + (item.value ?? 0), 0);
|
|
1903
|
+
sums.push(base);
|
|
1904
|
+
const off = discounts.reduce((acc, item) => acc + (item.value ?? 0), 0);
|
|
1905
|
+
if (off > 0) sums.push(base - 2 * off);
|
|
1906
|
+
}
|
|
1907
|
+
return sums.length > 0 ? sums : null;
|
|
1908
|
+
}
|
|
1909
|
+
var MONTHS = {
|
|
1910
|
+
january: 1,
|
|
1911
|
+
jan: 1,
|
|
1912
|
+
february: 2,
|
|
1913
|
+
feb: 2,
|
|
1914
|
+
march: 3,
|
|
1915
|
+
mar: 3,
|
|
1916
|
+
april: 4,
|
|
1917
|
+
apr: 4,
|
|
1918
|
+
may: 5,
|
|
1919
|
+
june: 6,
|
|
1920
|
+
jun: 6,
|
|
1921
|
+
july: 7,
|
|
1922
|
+
jul: 7,
|
|
1923
|
+
august: 8,
|
|
1924
|
+
aug: 8,
|
|
1925
|
+
september: 9,
|
|
1926
|
+
sept: 9,
|
|
1927
|
+
sep: 9,
|
|
1928
|
+
october: 10,
|
|
1929
|
+
oct: 10,
|
|
1930
|
+
november: 11,
|
|
1931
|
+
nov: 11,
|
|
1932
|
+
december: 12,
|
|
1933
|
+
dec: 12
|
|
1934
|
+
};
|
|
1935
|
+
var WEEKDAYS = {
|
|
1936
|
+
sunday: 0,
|
|
1937
|
+
sun: 0,
|
|
1938
|
+
monday: 1,
|
|
1939
|
+
mon: 1,
|
|
1940
|
+
tuesday: 2,
|
|
1941
|
+
tue: 2,
|
|
1942
|
+
tues: 2,
|
|
1943
|
+
wednesday: 3,
|
|
1944
|
+
wed: 3,
|
|
1945
|
+
thursday: 4,
|
|
1946
|
+
thu: 4,
|
|
1947
|
+
thur: 4,
|
|
1948
|
+
thurs: 4,
|
|
1949
|
+
friday: 5,
|
|
1950
|
+
fri: 5,
|
|
1951
|
+
saturday: 6,
|
|
1952
|
+
sat: 6
|
|
1953
|
+
};
|
|
1954
|
+
var WEEKDAY_NAMES = [
|
|
1955
|
+
"Sunday",
|
|
1956
|
+
"Monday",
|
|
1957
|
+
"Tuesday",
|
|
1958
|
+
"Wednesday",
|
|
1959
|
+
"Thursday",
|
|
1960
|
+
"Friday",
|
|
1961
|
+
"Saturday"
|
|
1962
|
+
];
|
|
1963
|
+
var MONTH_NAMES = [
|
|
1964
|
+
"January",
|
|
1965
|
+
"February",
|
|
1966
|
+
"March",
|
|
1967
|
+
"April",
|
|
1968
|
+
"May",
|
|
1969
|
+
"June",
|
|
1970
|
+
"July",
|
|
1971
|
+
"August",
|
|
1972
|
+
"September",
|
|
1973
|
+
"October",
|
|
1974
|
+
"November",
|
|
1975
|
+
"December"
|
|
1976
|
+
];
|
|
1977
|
+
var WEEKDAY_SRC = "(Sunday|Monday|Tuesday|Wednesday|Thursday|Friday|Saturday|Sun|Mon|Tues|Tue|Wed|Thurs|Thur|Thu|Fri|Sat)\\.?,?\\s+";
|
|
1978
|
+
var MONTH_SRC = "(January|February|March|April|May|June|July|August|September|October|November|December|Jan|Feb|Mar|Apr|Jun|Jul|Aug|Sept|Sep|Oct|Nov|Dec)\\.?";
|
|
1979
|
+
var US_DATE_RE = new RegExp(
|
|
1980
|
+
`\\b(?:${WEEKDAY_SRC})?${MONTH_SRC}\\s+(\\d{1,2})(?:st|nd|rd|th)?\\b(?:,?\\s+(\\d{4}))?`,
|
|
1981
|
+
"gi"
|
|
1982
|
+
);
|
|
1983
|
+
var EU_DATE_RE = new RegExp(
|
|
1984
|
+
`\\b(?:${WEEKDAY_SRC})?(\\d{1,2})(?:st|nd|rd|th)?\\s+(?:of\\s+)?${MONTH_SRC}\\b(?:,?\\s+(\\d{4}))?`,
|
|
1985
|
+
"gi"
|
|
1986
|
+
);
|
|
1987
|
+
var ISO_DATE_RE2 = new RegExp(`\\b(?:${WEEKDAY_SRC})?(\\d{4})-(\\d{2})-(\\d{2})\\b`, "gi");
|
|
1988
|
+
var FUTURE_RE = /\b(due|by|deadline|confirm|deliver(?:y|ed)?|event|scheduled?|until|expires?|valid|starting|order|pick-?up|week of)\b/i;
|
|
1989
|
+
var PAST_RE = /\b(since|founded|established|est\.|opened|born|history|ago|last year|back in)\b/i;
|
|
1990
|
+
var STALE_DAYS = 180;
|
|
1991
|
+
function utcDay(year, month, day) {
|
|
1992
|
+
const at = Date.UTC(year, month - 1, day);
|
|
1993
|
+
const d = new Date(at);
|
|
1994
|
+
if (d.getUTCFullYear() !== year || d.getUTCMonth() !== month - 1 || d.getUTCDate() !== day) {
|
|
1995
|
+
return null;
|
|
1996
|
+
}
|
|
1997
|
+
return at;
|
|
1998
|
+
}
|
|
1999
|
+
function dateHits(text) {
|
|
2000
|
+
const hits = [];
|
|
2001
|
+
const seen = /* @__PURE__ */ new Set();
|
|
2002
|
+
const weekdayOf = (w) => w ? WEEKDAYS[w.toLowerCase().replace(/\.$/, "")] ?? null : null;
|
|
2003
|
+
for (const m of text.matchAll(US_DATE_RE)) {
|
|
2004
|
+
seen.add(m.index);
|
|
2005
|
+
hits.push({
|
|
2006
|
+
raw: m[0],
|
|
2007
|
+
weekday: weekdayOf(m[1]),
|
|
2008
|
+
month: MONTHS[m[2].toLowerCase()],
|
|
2009
|
+
day: Number(m[3]),
|
|
2010
|
+
year: m[4] ? Number(m[4]) : null
|
|
2011
|
+
});
|
|
2012
|
+
}
|
|
2013
|
+
for (const m of text.matchAll(EU_DATE_RE)) {
|
|
2014
|
+
if (seen.has(m.index)) continue;
|
|
2015
|
+
hits.push({
|
|
2016
|
+
raw: m[0],
|
|
2017
|
+
weekday: weekdayOf(m[1]),
|
|
2018
|
+
day: Number(m[2]),
|
|
2019
|
+
month: MONTHS[m[3].toLowerCase()],
|
|
2020
|
+
year: m[4] ? Number(m[4]) : null
|
|
2021
|
+
});
|
|
2022
|
+
}
|
|
2023
|
+
for (const m of text.matchAll(ISO_DATE_RE2)) {
|
|
2024
|
+
hits.push({
|
|
2025
|
+
raw: m[0],
|
|
2026
|
+
weekday: weekdayOf(m[1]),
|
|
2027
|
+
year: Number(m[2]),
|
|
2028
|
+
month: Number(m[3]),
|
|
2029
|
+
day: Number(m[4])
|
|
2030
|
+
});
|
|
2031
|
+
}
|
|
2032
|
+
return hits;
|
|
2033
|
+
}
|
|
2034
|
+
function checkDates(no, text, todayAt, findings, checked) {
|
|
2035
|
+
const todayYear = new Date(todayAt).getUTCFullYear();
|
|
2036
|
+
for (const hit of dateHits(text)) {
|
|
2037
|
+
let at = null;
|
|
2038
|
+
let year = hit.year;
|
|
2039
|
+
if (year !== null) {
|
|
2040
|
+
at = utcDay(year, hit.month, hit.day);
|
|
2041
|
+
} else {
|
|
2042
|
+
let best = null;
|
|
2043
|
+
for (const candidate of [todayYear - 1, todayYear, todayYear + 1]) {
|
|
2044
|
+
const cAt = utcDay(candidate, hit.month, hit.day);
|
|
2045
|
+
if (cAt === null) continue;
|
|
2046
|
+
if (!best || Math.abs(cAt - todayAt) < Math.abs(best.at - todayAt)) {
|
|
2047
|
+
best = { at: cAt, year: candidate };
|
|
2048
|
+
}
|
|
2049
|
+
}
|
|
2050
|
+
at = best?.at ?? null;
|
|
2051
|
+
year = best?.year ?? null;
|
|
2052
|
+
}
|
|
2053
|
+
if (at === null || year === null) continue;
|
|
2054
|
+
checked.dates++;
|
|
2055
|
+
const named = `${MONTH_NAMES[hit.month - 1]} ${hit.day}, ${year}`;
|
|
2056
|
+
if (hit.weekday !== null) {
|
|
2057
|
+
const actual = new Date(at).getUTCDay();
|
|
2058
|
+
if (actual !== hit.weekday) {
|
|
2059
|
+
findings.push({
|
|
2060
|
+
kind: "weekday",
|
|
2061
|
+
line: no,
|
|
2062
|
+
message: `${named} is a ${WEEKDAY_NAMES[actual]}, not a ${WEEKDAY_NAMES[hit.weekday]}.`
|
|
2063
|
+
});
|
|
2064
|
+
}
|
|
2065
|
+
}
|
|
2066
|
+
if (hit.year !== null && at < todayAt && !PAST_RE.test(text)) {
|
|
2067
|
+
const daysAgo = Math.round((todayAt - at) / 864e5);
|
|
2068
|
+
if (daysAgo > STALE_DAYS || daysAgo > 1 && FUTURE_RE.test(text)) {
|
|
2069
|
+
findings.push({
|
|
2070
|
+
kind: "stale-date",
|
|
2071
|
+
line: no,
|
|
2072
|
+
message: `${named} has already passed; check the date.`
|
|
2073
|
+
});
|
|
2074
|
+
}
|
|
2075
|
+
}
|
|
2076
|
+
}
|
|
2077
|
+
}
|
|
2078
|
+
function checkFigures(text, opts) {
|
|
2079
|
+
const findings = [];
|
|
2080
|
+
const checked = { sums: 0, lineMath: 0, dates: 0, prices: 0 };
|
|
2081
|
+
const [ty, tm, td] = opts.today.split("-").map(Number);
|
|
2082
|
+
const todayAt = utcDay(ty ?? 1970, tm ?? 1, td ?? 1) ?? 0;
|
|
2083
|
+
const known = opts.knownAmounts ?? [];
|
|
2084
|
+
const lines = [];
|
|
2085
|
+
let heading = null;
|
|
2086
|
+
let header = null;
|
|
2087
|
+
let run = [];
|
|
2088
|
+
let breaks = [];
|
|
2089
|
+
const statedTotals = [];
|
|
2090
|
+
const reset = () => {
|
|
2091
|
+
run = [];
|
|
2092
|
+
breaks = [];
|
|
2093
|
+
};
|
|
2094
|
+
const rawLines = text.split(/\r?\n/);
|
|
2095
|
+
for (let i = 0; i < rawLines.length; i++) {
|
|
2096
|
+
const no = i + 1;
|
|
2097
|
+
const raw = rawLines[i];
|
|
2098
|
+
const plain = normalizeInlineEmphasis(raw);
|
|
2099
|
+
checkDates(no, plain, todayAt, findings, checked);
|
|
2100
|
+
const trimmed = plain.trim();
|
|
2101
|
+
if (/^#{1,6}\s/.test(trimmed)) {
|
|
2102
|
+
heading = trimmed.replace(/^#+\s*/, "").trim();
|
|
2103
|
+
header = null;
|
|
2104
|
+
reset();
|
|
2105
|
+
}
|
|
2106
|
+
if (/^([-*_])(\s*\1){2,}\s*$/.test(trimmed)) {
|
|
2107
|
+
header = null;
|
|
2108
|
+
reset();
|
|
2109
|
+
continue;
|
|
2110
|
+
}
|
|
2111
|
+
if (trimmed.length === 0) continue;
|
|
2112
|
+
let line;
|
|
2113
|
+
if (trimmed.startsWith("|")) {
|
|
2114
|
+
if (/^\|?\s*:?-{2,}/.test(trimmed)) continue;
|
|
2115
|
+
const cells = splitCells(trimmed);
|
|
2116
|
+
line = analyzeTableRow(no, cells, header, findings, checked);
|
|
2117
|
+
if (line.amounts.length === 0 && !header) {
|
|
2118
|
+
header = cells;
|
|
2119
|
+
breaks.push(run.length);
|
|
2120
|
+
continue;
|
|
2121
|
+
}
|
|
2122
|
+
} else {
|
|
2123
|
+
if (header) header = null;
|
|
2124
|
+
line = analyzeTextLine(no, trimmed, findings, checked);
|
|
2125
|
+
}
|
|
2126
|
+
lines.push(line);
|
|
2127
|
+
if (line.total && line.value !== null) {
|
|
2128
|
+
const stated = line.value;
|
|
2129
|
+
statedTotals.push(stated);
|
|
2130
|
+
const sums = candidateSums(run, breaks);
|
|
2131
|
+
if (sums) {
|
|
2132
|
+
checked.sums++;
|
|
2133
|
+
if (!sums.some((sum) => same(sum, stated))) {
|
|
2134
|
+
const symbol = line.amounts[line.amounts.length - 1].symbol;
|
|
2135
|
+
const where = heading ? `Under "${heading}", the ` : "The ";
|
|
2136
|
+
findings.push({
|
|
2137
|
+
kind: "sum",
|
|
2138
|
+
line: no,
|
|
2139
|
+
message: `${where}${line.label.toLowerCase() || "total"} says ${fmt(stated, symbol)}, but the items above it add up to ${fmt(sums[0], symbol)}.`
|
|
2140
|
+
});
|
|
2141
|
+
}
|
|
2142
|
+
}
|
|
2143
|
+
if (line.total === "subtotal") {
|
|
2144
|
+
run = [{ value: stated, discount: false }];
|
|
2145
|
+
breaks = [];
|
|
2146
|
+
} else {
|
|
2147
|
+
reset();
|
|
2148
|
+
}
|
|
2149
|
+
continue;
|
|
2150
|
+
}
|
|
2151
|
+
if (line.amounts.length === 0) {
|
|
2152
|
+
breaks.push(run.length);
|
|
2153
|
+
continue;
|
|
2154
|
+
}
|
|
2155
|
+
run.push({ value: line.value, discount: line.discount });
|
|
2156
|
+
}
|
|
2157
|
+
if (known.length > 0) {
|
|
2158
|
+
for (const line of lines) {
|
|
2159
|
+
if (line.total || line.discount || !line.unit || TAX_RE.test(line.label)) continue;
|
|
2160
|
+
const price = Math.abs(line.unit.value);
|
|
2161
|
+
checked.prices++;
|
|
2162
|
+
if (known.some((k) => same(k, price))) continue;
|
|
2163
|
+
if (statedTotals.some((t) => same(t, price))) continue;
|
|
2164
|
+
const amount = fmt(price, line.unit.symbol);
|
|
2165
|
+
const item = line.label || "A line";
|
|
2166
|
+
findings.push({
|
|
2167
|
+
kind: "unsourced-price",
|
|
2168
|
+
line: line.no,
|
|
2169
|
+
item,
|
|
2170
|
+
amount,
|
|
2171
|
+
message: `${item} at ${amount}: this price didn't come from you.`
|
|
2172
|
+
});
|
|
2173
|
+
}
|
|
2174
|
+
}
|
|
2175
|
+
findings.sort((a, b) => a.line - b.line);
|
|
2176
|
+
return { findings, checked };
|
|
2177
|
+
}
|
|
2178
|
+
|
|
1693
2179
|
// ../core/src/checks/markdown.ts
|
|
1694
2180
|
function cleanHeading(text) {
|
|
1695
2181
|
return text.replace(/\s+#+\s*$/, "").replace(/\s+\{\[[^\]\r\n]+\]\}\s*$/, "").replace(/[*_`]/g, "").replace(/[–—]/g, "-").replace(/[^\p{L}\p{N}]+/gu, " ").trim().toLocaleLowerCase();
|
|
@@ -2245,6 +2731,7 @@ export {
|
|
|
2245
2731
|
MIN_INLINE_JS_BYTES,
|
|
2246
2732
|
MIN_JUDGE_EVIDENCE_SUBSTRING,
|
|
2247
2733
|
buildJudgePrompt,
|
|
2734
|
+
checkFigures,
|
|
2248
2735
|
citationsResolve,
|
|
2249
2736
|
citedPathKey,
|
|
2250
2737
|
cleanCitedPath,
|
|
@@ -2260,10 +2747,12 @@ export {
|
|
|
2260
2747
|
esmImports,
|
|
2261
2748
|
explainSniff,
|
|
2262
2749
|
extractInlineScripts,
|
|
2750
|
+
extractMoneyAmounts,
|
|
2263
2751
|
fileCountByExt,
|
|
2264
2752
|
fileMinBytes,
|
|
2265
2753
|
fileMinLines,
|
|
2266
2754
|
findImageRefs,
|
|
2755
|
+
frontmatterList,
|
|
2267
2756
|
gateResult,
|
|
2268
2757
|
grepMatches,
|
|
2269
2758
|
htmlCompleteSniff,
|
|
@@ -2273,6 +2762,7 @@ export {
|
|
|
2273
2762
|
isRealIsoDate,
|
|
2274
2763
|
jsonPathEquals,
|
|
2275
2764
|
jsonValid,
|
|
2765
|
+
listedFiles,
|
|
2276
2766
|
markdownHeadingsMatch,
|
|
2277
2767
|
mergeCorpusCoverageShards,
|
|
2278
2768
|
namedEntitiesConsistent,
|
|
@@ -2287,6 +2777,7 @@ export {
|
|
|
2287
2777
|
recordSchema,
|
|
2288
2778
|
requireOrderedSections,
|
|
2289
2779
|
resolveRelative,
|
|
2780
|
+
runSniff,
|
|
2290
2781
|
securityReport,
|
|
2291
2782
|
standaloneJsParses,
|
|
2292
2783
|
tableShape,
|