|
| 1 | +import { readFile, readdir } from "node:fs/promises"; |
| 2 | +import { join, relative, sep } from "node:path"; |
| 3 | +import { fileURLToPath } from "node:url"; |
| 4 | + |
| 5 | +// Every numeric REA_ limit under src must be an analysis setting or a |
| 6 | +// documented exception with a reason. restart-only is durable. per-call is a |
| 7 | +// launch-captured limit whose runtime migration deletes the registry entry. |
| 8 | +// The three limits moving on #9868, #9865, and #9836 are not in this tree; |
| 9 | +// once their names appear here they must join ANALYSIS_SETTINGS. |
| 10 | + |
| 11 | +const root = fileURLToPath(new URL("../../", import.meta.url)); |
| 12 | +const definitionsPath = join(root, "src/domain/analysisSettingDefinitions.ts"); |
| 13 | +const registryPath = join(root, "src/domain/numericLimitRegistry.ts"); |
| 14 | + |
| 15 | +const listFiles = async (dir) => { |
| 16 | + const output = []; |
| 17 | + for (const entry of await readdir(dir, { withFileTypes: true })) { |
| 18 | + const path = join(dir, entry.name); |
| 19 | + if (entry.isDirectory()) { |
| 20 | + if (entry.name === "node_modules" || entry.name === "dist") continue; |
| 21 | + output.push(...(await listFiles(path))); |
| 22 | + } else if ( |
| 23 | + entry.name.endsWith(".ts") && |
| 24 | + !entry.name.endsWith(".test.ts") && |
| 25 | + !entry.name.endsWith(".fixture.ts") |
| 26 | + ) |
| 27 | + output.push(path); |
| 28 | + } |
| 29 | + return output; |
| 30 | +}; |
| 31 | + |
| 32 | +const isNumericLimit = (name) => |
| 33 | + /(?:_BYTES|_MS|_MIB|_SECONDS|_POLLS|_JOBS|_ELEMENTS|_COUNT|_SESSIONS|_COMMANDS)$/u.test( |
| 34 | + name, |
| 35 | + ) || |
| 36 | + /_MAX_/u.test(name) || |
| 37 | + /_(?:TIMEOUT|DEADLINE|BUDGET)(?:_|$)/u.test(name) || |
| 38 | + /_HEAP(?:_|$)/u.test(name); |
| 39 | + |
| 40 | +const namesIn = (source, pattern) => |
| 41 | + new Set([...source.matchAll(pattern)].map((match) => match[1])); |
| 42 | + |
| 43 | +const failures = []; |
| 44 | +const definitions = await readFile(definitionsPath, "utf8"); |
| 45 | +const registry = await readFile(registryPath, "utf8"); |
| 46 | +const runtime = namesIn(definitions, /environment:\s*"(REA_[A-Z0-9_]+)"/gu); |
| 47 | +const declaredNames = [ |
| 48 | + ...registry.matchAll(/name:\s*"(REA_[A-Z0-9_]+)"/gu), |
| 49 | +].map((match) => match[1]); |
| 50 | +const entries = [ |
| 51 | + ...registry.matchAll( |
| 52 | + /name:\s*"(REA_[A-Z0-9_]+)"\s*,\s*update_mode:\s*"(runtime|restart)"\s*,\s*policy:\s*"(restart-only|per-call)"\s*,\s*read:\s*"(startup|per-call)"\s*,\s*reason:\s*"((?:\\.|[^"\\])*)"/gu, |
| 53 | + ), |
| 54 | +]; |
| 55 | +const listed = new Map(); |
| 56 | +for (const match of entries) { |
| 57 | + const [, name, updateMode, policy, read, reason] = match; |
| 58 | + if (listed.has(name)) failures.push(`${name} is listed twice`); |
| 59 | + listed.set(name, { updateMode, policy, read, reason }); |
| 60 | + if (reason.trim().length < 40) |
| 61 | + failures.push(`${name} needs a reason that states why`); |
| 62 | + if (updateMode !== "restart") |
| 63 | + failures.push( |
| 64 | + `${name} is outside ANALYSIS_SETTINGS, so its update_mode must be restart`, |
| 65 | + ); |
| 66 | + if (runtime.has(name)) |
| 67 | + failures.push( |
| 68 | + `${name} is both a runtime setting and on the exception list`, |
| 69 | + ); |
| 70 | + if (policy === "per-call" && read !== "per-call") |
| 71 | + failures.push(`${name} is per-call policy but is not read per call`); |
| 72 | + if (policy === "restart-only" && read !== "startup") |
| 73 | + failures.push(`${name} is restart-only but is not read at startup`); |
| 74 | +} |
| 75 | +for (const name of declaredNames) { |
| 76 | + if (!listed.has(name)) |
| 77 | + failures.push(`${name} is registered without update_mode`); |
| 78 | +} |
| 79 | +const settingBodies = [ |
| 80 | + ...definitions.matchAll(/\n {2}"[^"]+": \{([\s\S]*?)\n {2}\},/gu), |
| 81 | +]; |
| 82 | +if (settingBodies.length === 0) |
| 83 | + failures.push("ANALYSIS_SETTINGS did not parse into setting bodies"); |
| 84 | +for (const [, body] of settingBodies) { |
| 85 | + if (!/updateMode:\s*"(?:runtime|restart)"/u.test(body)) |
| 86 | + failures.push("an analysis setting is missing updateMode"); |
| 87 | +} |
| 88 | + |
| 89 | +const discovered = new Map(); |
| 90 | +for (const path of await listFiles(join(root, "src"))) { |
| 91 | + const source = await readFile(path, "utf8"); |
| 92 | + for (const match of source.matchAll(/(?<![A-Z0-9_])REA_[A-Z0-9_]+/gu)) { |
| 93 | + const name = match[0]; |
| 94 | + if (!isNumericLimit(name)) continue; |
| 95 | + const relativePath = relative(root, path).split(sep).join("/"); |
| 96 | + discovered.set(name, relativePath); |
| 97 | + } |
| 98 | +} |
| 99 | + |
| 100 | +for (const [name, file] of [...discovered].sort(([left], [right]) => |
| 101 | + left < right ? -1 : left > right ? 1 : 0, |
| 102 | +)) { |
| 103 | + if (!runtime.has(name) && !listed.has(name)) |
| 104 | + failures.push( |
| 105 | + `${name} in ${file} is neither an analysis setting nor docs/design/rea-limit-inventory.md via src/domain/numericLimitRegistry.ts`, |
| 106 | + ); |
| 107 | +} |
| 108 | +for (const name of listed.keys()) { |
| 109 | + if (!discovered.has(name)) |
| 110 | + failures.push(`${name} is registered but no longer appears in src`); |
| 111 | +} |
| 112 | + |
| 113 | +const forbidden = |
| 114 | + /(?<![A-Za-z0-9_])(?:rea_restart_required|restartRequired|restart_required)(?![A-Za-z0-9_])|effect:\s*"(?:immediate|next_process)"|SettingChangeEffect/gu; |
| 115 | +const scanRoots = ["src", "tests", "docs", ".agents", "scripts"].map((dir) => |
| 116 | + join(root, dir), |
| 117 | +); |
| 118 | +const skipDir = new Set([ |
| 119 | + "node_modules", |
| 120 | + "dist", |
| 121 | + ".turbo", |
| 122 | + ".cache", |
| 123 | + "rea-numeric-limits.mjs", |
| 124 | +]); |
| 125 | +const listText = async (dir) => { |
| 126 | + const output = []; |
| 127 | + let entries; |
| 128 | + try { |
| 129 | + entries = await readdir(dir, { withFileTypes: true }); |
| 130 | + } catch { |
| 131 | + return output; |
| 132 | + } |
| 133 | + for (const entry of entries) { |
| 134 | + if (skipDir.has(entry.name)) continue; |
| 135 | + const path = join(dir, entry.name); |
| 136 | + if (entry.isDirectory()) output.push(...(await listText(path))); |
| 137 | + else if (/\.(?:ts|tsx|mjs|js|md|json|py|yml|yaml)$/u.test(entry.name)) |
| 138 | + output.push(path); |
| 139 | + } |
| 140 | + return output; |
| 141 | +}; |
| 142 | +for (const dir of scanRoots) { |
| 143 | + for (const path of await listText(dir)) { |
| 144 | + if (path.endsWith(`${sep}rea-numeric-limits.mjs`)) continue; |
| 145 | + const source = await readFile(path, "utf8"); |
| 146 | + const hit = source.match(forbidden); |
| 147 | + if (hit) |
| 148 | + failures.push( |
| 149 | + `${relative(root, path).split(sep).join("/")} still uses ${[...new Set(hit)].join(", ")}`, |
| 150 | + ); |
| 151 | + } |
| 152 | +} |
| 153 | + |
| 154 | +if (failures.length > 0) { |
| 155 | + console.error(`Numeric REA_ limit check failed:\n${failures.join("\n")}`); |
| 156 | + process.exit(1); |
| 157 | +} |
| 158 | +console.log( |
| 159 | + `Numeric REA_ limits: ${runtime.size} runtime, ${[...listed.values()].filter((entry) => entry.policy === "restart-only").length} restart-only, ${[...listed.values()].filter((entry) => entry.policy === "per-call").length} per-call pending migration, ${discovered.size} discovered.`, |
| 160 | +); |
0 commit comments