From 3df41b2512a9fb16030938876fbd657f48850939 Mon Sep 17 00:00:00 2001 From: Antony Rizzitelli Date: Sun, 15 Nov 2026 17:27:20 +0000 Subject: [PATCH 1/7] refactor: remove the api-diff probes and prove scripts --- .agents/probes/dms-dev.cjs | 76 -- .agents/probes/interface-assertions-1.cjs | 497 ------------- .../probes/interface-components-builder.cjs | 268 ------- .../interface-components-owner-order.cjs | 384 ---------- .../interface-components-read-order.cjs | 438 ------------ .../probes/interface-components-routes.cjs | 435 ------------ .../interface-components-type-boundaries.cjs | 113 --- .agents/probes/interface-dms-layers.cjs | 655 ------------------ .agents/probes/playground.cjs | 354 ---------- .agents/probes/request-context.cjs | 24 - .agents/probes/runtime-declarations.d.ts | 14 - .agents/probes/tsconfig.json | 12 - .../probes/untyped/interface-assertions-1.cjs | 33 - .../untyped/interface-assertions-1.d.cts | 59 -- .../untyped/interface-components-routes.cjs | 26 - .../untyped/interface-components-routes.d.cts | 64 -- .agents/probes/untyped/tsconfig.json | 15 - .agents/skills/verify-dms/SKILL.md | 12 - package.json | 3 +- .../scripts/probes/capture-frontend-probe.cjs | 23 - .../scripts/probes/frontend-behavior.cjs | 606 ---------------- .../scripts/probes/frontend-legacy.cjs | 620 ----------------- .../probes/frontend-probe-controls.cjs | 322 --------- .../scripts/prove-comment-emit.mjs | 171 ----- .../scripts/prove-component-registration.mjs | 259 ------- 25 files changed, 1 insertion(+), 5482 deletions(-) delete mode 100644 .agents/probes/dms-dev.cjs delete mode 100644 .agents/probes/interface-assertions-1.cjs delete mode 100644 .agents/probes/interface-components-builder.cjs delete mode 100644 .agents/probes/interface-components-owner-order.cjs delete mode 100644 .agents/probes/interface-components-read-order.cjs delete mode 100644 .agents/probes/interface-components-routes.cjs delete mode 100644 .agents/probes/interface-components-type-boundaries.cjs delete mode 100644 .agents/probes/interface-dms-layers.cjs delete mode 100644 .agents/probes/playground.cjs delete mode 100644 .agents/probes/request-context.cjs delete mode 100644 .agents/probes/runtime-declarations.d.ts delete mode 100644 .agents/probes/tsconfig.json delete mode 100644 .agents/probes/untyped/interface-assertions-1.cjs delete mode 100644 .agents/probes/untyped/interface-assertions-1.d.cts delete mode 100644 .agents/probes/untyped/interface-components-routes.cjs delete mode 100644 .agents/probes/untyped/interface-components-routes.d.cts delete mode 100644 .agents/probes/untyped/tsconfig.json delete mode 100644 packages/dms/frontend-vue/scripts/probes/capture-frontend-probe.cjs delete mode 100644 packages/dms/frontend-vue/scripts/probes/frontend-behavior.cjs delete mode 100644 packages/dms/frontend-vue/scripts/probes/frontend-legacy.cjs delete mode 100644 packages/dms/frontend-vue/scripts/probes/frontend-probe-controls.cjs delete mode 100644 packages/dms/frontend-vue/scripts/prove-comment-emit.mjs delete mode 100644 packages/dms/frontend-vue/scripts/prove-component-registration.mjs diff --git a/.agents/probes/dms-dev.cjs b/.agents/probes/dms-dev.cjs deleted file mode 100644 index d34d60224..000000000 --- a/.agents/probes/dms-dev.cjs +++ /dev/null @@ -1,76 +0,0 @@ -const assert = require("node:assert/strict"); -const { spawnSync } = require("node:child_process"); -const { mkdtempSync, mkdirSync, rmSync, symlinkSync, writeFileSync } = require("node:fs"); -const { tmpdir } = require("node:os"); -const path = require("node:path"); - -const root = path.resolve(process.argv[2]); -const script = path.join(root, "packages/dms/skills/dms-project/scripts/dms-dev.sh"); -const fixture = mkdtempSync(path.join(tmpdir(), "dms-dev-probe-")); -const project = path.join(fixture, "empty project"); -mkdirSync(project); -const bin = path.join(fixture, "bin directory"); -const aliases = path.join(fixture, "aliases"); -mkdirSync(bin); -mkdirSync(aliases); -const absoluteLink = path.join(bin, "dms-dev"); -const relativeLink = path.join(aliases, "dms-relative"); -symlinkSync(script, absoluteLink); -symlinkSync("../bin directory/dms-dev", relativeLink); -writeFileSync(path.join(project, "package.json"), '{"name":"probe","private":true}'); -const backendLog = path.join(fixture, "backend.log"); -const frontendLog = path.join(fixture, "frontend.log"); -writeFileSync(backendLog, Array.from({ length: 67 }, (_, i) => `back-${i}\n`).join("")); -writeFileSync(frontendLog, Array.from({ length: 63 }, (_, i) => `front-${i}\n`).join("")); -/** @type {{ args: string[]; relative: boolean; executable: string | null; status: number | null; stdout: string; stderr: string }[]} */ -const observations = []; -/** - * @param {string[]} args - * @param {number} expectedStatus - * @param {NodeJS.ProcessEnv} [extraEnv] - * @param {boolean} [relative] - * @param {string} [executable] - * @returns {string} - */ -function run(args, expectedStatus, extraEnv = {}, relative = false, executable) { - const result = spawnSync(executable ?? "bash", executable ? args : [relative ? path.basename(script) : script, ...args], { - cwd: relative ? path.dirname(script) : fixture, - env: { ...process.env, DMS_BACK_DIR: "", DMS_BACK_LOG: backendLog, DMS_FRONT_LOG: frontendLog, ...extraEnv }, - encoding: "utf8", - timeout: 10_000, - }); - assert.ifError(result.error); - assert.equal(result.status, expectedStatus, args.join(" ")); - const normalize = (/** @type {string} */ text) => text.split(fixture).join(""); - observations.push({ args, relative, executable: executable ? path.basename(executable) : null, status: result.status, stdout: normalize(result.stdout), stderr: normalize(result.stderr) }); - return result.stdout; -} -try { - const help = run(["--help"], 0); - assert.match(help, /^Usage: dms-dev\.sh \n/); - for (const args of [[], ["-h"], ["help"]]) assert.equal(run(args, 0), help); - assert.equal(run(["--help"], 0, {}, true), help); - assert.equal(run(["unknown"], 2), help); - for (const link of [absoluteLink, relativeLink]) { - const linkHelp = help.replace("Usage: dms-dev.sh", `Usage: ${path.basename(link)}`); - assert.equal(run(["--help"], 0, {}, false, link), linkHelp); - assert.equal(run(["-h"], 0, {}, false, link), linkHelp); - assert.equal(run(["unknown"], 2, {}, false, link), linkHelp); - } - assert.equal(run(["--help"], 0, { PATH: `${bin}:${process.env.PATH}` }, false, "dms-dev"), help.replace("Usage: dms-dev.sh", "Usage: dms-dev")); - for (const command of ["start", "stop", "status", "restart-back", "restart-front"]) { - assert.match(run([command], 2), /DMS_BACK_DIR is not set/); - } - assert.match(run(["status"], 2, { DMS_BACK_DIR: path.join(fixture, "missing") }), /does not exist/); - assert.match(run(["status"], 2, { DMS_BACK_DIR: fixture }), /has no package\.json/); - assert.match(run(["status"], 0, { DMS_BACK_DIR: project }), /\(none\)/); - assert.match(run(["stop"], 0, { DMS_BACK_DIR: project }), /nothing running\./); - assert.equal(run(["back-log", "3"], 0), "back-64\nback-65\nback-66\n"); - assert.equal(run(["front-log", "2"], 0), "front-61\nfront-62\n"); - assert.equal(run(["back-log"], 0).split("\n")[0], "back-7"); - assert.equal(run(["front-log"], 0).split("\n")[0], "front-3"); - assert.equal(run(["back-log", "0"], 0), ""); - process.stdout.write(JSON.stringify(observations)); -} finally { - rmSync(fixture, { recursive: true, force: true }); -} diff --git a/.agents/probes/interface-assertions-1.cjs b/.agents/probes/interface-assertions-1.cjs deleted file mode 100644 index 7d8de927b..000000000 --- a/.agents/probes/interface-assertions-1.cjs +++ /dev/null @@ -1,497 +0,0 @@ -const path = require("node:path"); - -const root = process.argv[2]; - -/** api-diff runs a scratch copy of this file, so relative requires must start from the tree under test. */ -module.filename = path.join(root, ".agents/probes/interface-assertions-1.cjs"); - -const assert = require("node:assert/strict"); - -const { z } = require("../../packages/interface-dms/node_modules/zod"); - -const untyped = require("./untyped/interface-assertions-1.cjs"); - -/** - * The one place this probe decodes serialized observations: JSON text holds values of no declared shape. - * @param {string} text - * @returns {unknown} - */ -function parseJson(text) { - return JSON.parse(text); -} - -/** - * @param {unknown} value - * @returns {value is Record} - */ -function isJsonObject(value) { - return value instanceof Object && !Array.isArray(value); -} - -/** - * @param {unknown} value - * @returns {Record} - */ -function asObject(value) { - if (isJsonObject(value)) return value; - throw new TypeError("Expected a JSON object"); -} - -const { HTTPResult } = require("../../packages/interface-dms/node_modules/@antelopejs/interface-api"); - -/** - * Names a thrown value without rethrowing it: an `Error` or an `HTTPResult` by its class and message, anything else by its string form. - * @param {unknown} error - * @returns {{ name: string, message: string }} - */ -function thrownOf(error) { - if (error instanceof Error) return { name: error.constructor.name, message: String(error.message) }; - if (error instanceof HTTPResult) return { name: error.constructor.name, message: "undefined" }; - return { name: "thrown", message: String(error) }; -} - -/** - * @template {object} T - * @param {boolean | import("../../packages/interface-dms/dist/base/table-view.js").RowActionConfig | undefined} action - */ -function ruleOf(action) { - return action === undefined || action === true || action === false ? undefined : action.rule; -} - -/** - * @param {unknown} value - * @returns {unknown[]} - */ -function asArray(value) { - if (Array.isArray(value)) return value; - throw new TypeError("Expected a JSON array"); -} - -/** - * Reads through a proxy exactly as a proxy without a `get` trap would. - * @param {object} target - * @param {PropertyKey} key - * @param {unknown} receiver - * @returns {unknown} - */ -function forwardRead(target, key, receiver) { - // oxlint-disable-next-line anti-slop/no-reflect-get -- The get trap forwards the read with the trap's receiver, which only Reflect.get takes, so a getter on the proxied zod _def runs with the proxy as this and its own reads reach the log. - return Reflect.get(target, key, receiver); -} - -/** - * @param {{ _def: object }} schema - * @param {string[]} log - * @param {string} label - */ -function recordDefReads(schema, log, label) { - const proxy = new Proxy(schema._def, { - get(target, key, receiver) { - log.push(`${label}.${String(key)}`); - return forwardRead(target, key, receiver); - }, - }); - const view = {}; - Object.setPrototypeOf(view, schema); - return Object.defineProperty(view, "_def", { value: proxy }); -} - -/** - * @param {import("./untyped/interface-assertions-1.cjs").Describer} describeSchema - * @returns {Record} - */ -function observeDefReads(describeSchema) { - /** @type {Record} */ - const observed = {}; - for (const [name, schema] of Object.entries({ - optionalString: z.string().optional(), - defaultString: z.string().default("x"), - lazyString: z.lazy(() => z.string()), - tuple: z.tuple([z.string()]), - object: z.object({ a: z.string() }), - record: z.record(z.string(), z.number()), - enumeration: z.enum(["a"]), - })) { - /** @type {string[]} */ - const reads = []; - try { - describeSchema(recordDefReads(schema, reads, name)); - } catch (error) { - reads.push(`threw ${thrownOf(error).message}`); - } - observed[`reads:${name}`] = reads; - } - return observed; -} - -/** - * @param {import("./untyped/interface-assertions-1.cjs").Describer} describeSchema - * @param {import("./untyped/interface-assertions-1.cjs").Describer} describeOptions - * @returns {unknown} - */ -function describeBattery(describeSchema, describeOptions) { - /** @type {{ lazy: number, factory: number, receivers: boolean[] }} */ - const counts = { lazy: 0, factory: 0, receivers: [] }; - const nativeEnum = { A: "x", B: 2 }; - const lazy = z.lazy(() => { - counts.lazy += 1; - return z.string(); - }); - const withFactory = z.string().default("seed"); - const factory = withFactory._def.defaultValue; - withFactory._def.defaultValue = function () { - counts.factory += 1; - counts.receivers.push(this === withFactory._def); - return factory(); - }; - const deep = (() => { - /** @type {import("../../packages/interface-dms/node_modules/zod").ZodTypeAny} */ - let schema = z.object({ leaf: z.string() }); - for (let i = 0; i < 9; i += 1) schema = z.object({ next: schema }); - return schema; - })(); - const schemas = { - string: z.string(), - optionalNullable: z.number().optional().nullable(), - literal: z.literal("a"), - literalNumber: z.literal(3), - literalNaN: z.literal(Number.NaN), - literalNull: z.literal(null), - union: z.union([z.literal("a"), z.literal("b")]), - mixedUnion: z.union([z.string(), z.number()]), - discriminated: z.discriminatedUnion("kind", [ - z.object({ kind: z.literal("a") }), - z.object({ kind: z.literal("b"), extra: z.string() }), - ]), - object: z.object({ - a: z.string().default("hi"), - b: z.array(z.number()), - c: z.tuple([z.string(), z.number()]), - d: z.record(z.string(), z.boolean()), - e: z.enum(["p", "q"]), - f: z.nativeEnum(nativeEnum), - }), - intersection: z.intersection( - z.object({ a: z.string() }), - z.object({ b: z.number() }), - ), - lazy, - lazyInObject: z.object({ inner: lazy }), - effects: z.string().transform((value) => value), - pipeline: z.string().pipe(z.string()), - branded: z.string().brand("probe"), - readonly: z.string().readonly(), - caught: z.string().catch("fallback"), - promise: z.promise(z.string()), - withFactory, - defaultObject: z.object({ x: z.number() }).default({ x: 1 }), - defaultDate: z.date().default(new Date(0)), - deepWrappers: z.string().optional().optional().optional().optional().optional().optional().optional().optional().optional(), - deepObjects: deep, - described: z.string().describe("text").optional(), - primitives: z.object({ - big: z.bigint(), - date: z.date(), - any: z.any(), - nan: z.nan(), - map: z.map(z.string(), z.string()), - set: z.set(z.string()), - fn: z.function(), - symbol: z.symbol(), - nil: z.null(), - undef: z.undefined(), - nothing: z.void(), - never: z.never(), - unknown: z.unknown(), - }), - malformedUnion: { _def: { typeName: "ZodUnion" } }, - malformedOptions: { _def: { typeName: "ZodUnion", options: undefined } }, - malformedTuple: { _def: { typeName: "ZodTuple" } }, - malformedOptional: { _def: { typeName: "ZodOptional" } }, - foreign: { _def: { typeName: undefined } }, - foreignName: { _def: { typeName: "ZodFuture" } }, - }; - /** @type {Record} */ - const observed = {}; - for (const [name, schema] of Object.entries(schemas)) { - const before = { ...counts, receivers: counts.receivers.length }; - /** @type {Record} */ - const record = {}; - /** @type {[string, () => unknown][]} */ - const modes = [ - ["schema", () => describeSchema(schema)], - ["options", () => describeOptions(schema)], - ]; - for (const [mode, run] of modes) { - try { - record[mode] = run(); - } catch (error) { - record[mode] = { thrown: thrownOf(error).message }; - } - } - record.calls = { - lazy: counts.lazy - before.lazy, - factory: counts.factory - before.factory, - receivers: counts.receivers.slice(before.receivers), - }; - observed[name] = record; - } - Object.assign(observed, observeDefReads(describeSchema)); - return parseJson( - JSON.stringify(observed, (_key, /** @type {unknown} */ value) => - value === undefined ? "__undefined" : value, - ), - ); -} - -function observeDescribe() { - return describeBattery(untyped.describeSchema, untyped.describeOptions); -} - -function observeParser() { - const { parseValue, parseObject } = require("../../packages/interface-dms/dist/utils/internal/value-parser.js"); - const inputs = [ - "true", "false", "null", "undefined", "TRUE", "toString", "constructor", - "__proto__", "hasOwnProperty", "", " ", "42", "0", "-1.5", "1e3", "NaN", - "2024-01-01", "2024-01-01T12:00:00Z", '{"a":1}', "[1,2]", "(x) => x", "text", - ]; - const parsed = inputs.map((input) => { - const value = parseValue(input); - return { - input, - kind: Object.prototype.toString.call(value), - json: JSON.stringify(value === undefined ? "__undefined" : value), - }; - }); - const object = parseObject(inputs.map((input) => ({ key: `k:${input}`, value: input }))); - return { parsed, keys: Object.keys(object) }; -} - -/** - * @param {unknown} value - * @returns {value is Record} - */ -function isObject(value) { - return Object(value) === value; -} - -/** - * @param {unknown} rowActions - * @returns {unknown} - */ -function userEditRule(rowActions) { - if (!isObject(rowActions) || !("edit" in rowActions) || !isObject(rowActions.edit)) return undefined; - return "rule" in rowActions.edit ? rowActions.edit.rule : undefined; -} - -function observeArchiveRules() { - const { applyArchiveModeDefaultRules } = require("../../packages/interface-dms/dist/base/helpers/archive-mode-helpers.js"); - const rule = { field: "other", equals: 1 }; - const cases = { - none: undefined, - empty: {}, - allTrue: { edit: true, duplicate: true, archive: true, restore: true, delete: true }, - allFalse: { edit: false, duplicate: false, archive: false, restore: false, delete: false }, - disabled: { edit: { isEnabled: false }, delete: { isEnabled: false, rule } }, - configured: { edit: { rule }, archive: { isEnabled: true }, restore: { label: "x", rule }, delete: { rule, isVisible: false } }, - withExtras: { details: true, add: false, edit: { icon: "i" } }, - keyOrder: { delete: true, edit: true }, - }; - /** @type {Record} */ - const observed = {}; - for (const [name, rowActions] of Object.entries(cases)) { - const result = applyArchiveModeDefaultRules(rowActions, "archived"); - const configured = userEditRule(rowActions); - const editRule = ruleOf(result?.edit); - observed[name] = { - keys: result === undefined ? null : Object.keys(result), - json: JSON.stringify(result ?? "__undefined"), - composedKeepsUserRule: - (editRule !== undefined && "and" in editRule ? editRule.and[1] : undefined) === configured && configured !== undefined, - }; - } - const first = applyArchiveModeDefaultRules({}, "archived"); - const second = applyArchiveModeDefaultRules({}, "archived"); - assert.ok(first && second, "applyArchiveModeDefaultRules returns the row actions"); - observed.freshRulePerCall = ruleOf(first.edit) !== ruleOf(second.edit); - observed.distinctRulePerAction = ruleOf(first.edit) !== ruleOf(first.duplicate); - return observed; -} - -function observeChoices() { - /** @type {Record} */ - const observed = {}; - for (const [name, options] of Object.entries({ - single: { items: [{ value: "a", label: "A" }, { value: 2, label: "B" }] }, - multiple: { multiple: true, items: [{ value: "a", label: "A" }, { value: true, label: "B" }] }, - emptySingle: { items: [] }, - emptyMultiple: { multiple: true, items: [] }, - })) { - /** @type {string[]} */ - const entries = []; - const type = untyped.selectTypeOf(options); - for (const input of ["a", "2", 2, "true", ["a"], ["a", "b"], [], undefined]) { - try { - entries.push(JSON.stringify(type.getValidation().parse(input)) ?? "__undefined"); - } catch (error) { - entries.push(`rejected:${thrownOf(error).name}`); - } - } - observed[name] = entries; - } - return observed; -} - -async function observeFormPages() { - const helpers = require("../../packages/interface-dms/dist/base/table-view/internal/factory-helpers.js"); - /** @type {string[]} */ - const reads = []; - const watch = (/** @type {object} */ target, /** @type {string} */ label) => - new Proxy(target, { - get(inner, key, receiver) { - reads.push(`${label}.${String(key)}`); - return forwardRead(inner, key, receiver); - }, - }); - /** @type {Record} */ - const observed = {}; - for (const [name, pages] of Object.entries({ - none: undefined, - empty: {}, - slugs: { new: { urlSlug: "create" }, edit: { urlSlug: "change/:id" }, details: { urlSlug: "show/:id" } }, - partial: { edit: { urlSlug: "x" } }, - blank: { new: { urlSlug: "" } }, - })) { - reads.length = 0; - const urls = helpers.buildFormPageUrls("/page//base", "table", pages && watch(pages, name)); - observed[name] = { urls, keys: Object.keys(urls), reads: reads.slice() }; - } - observed.kinds = helpers.FORM_PAGE_KINDS; - const { resolveTableViewGrants } = require("../../packages/interface-dms/dist/base/table-view/internal/request-filter.js"); - const permissions = require("../../packages/interface-dms/dist/permissions.js"); - const original = permissions.HasPermission; - /** @type {string[]} */ - const asked = []; - permissions.HasPermission = async (granted, id) => { - asked.push(`${id}:start`); - await Promise.resolve(); - asked.push(`${id}:end`); - return granted.has(id); - }; - try { - const grants = await resolveTableViewGrants(new Set(["p.add", "p.view", "p.export"]), "p"); - observed.grants = { grants, keys: Object.keys(grants), asked }; - } finally { - permissions.HasPermission = original; - } - return observed; -} - -async function observeReceivers() { - const { authorizeAction } = untyped; - const { getTableViewMetaFor } = require("../../packages/interface-dms/dist/base/table-view/internal/meta.js"); - const { ArchiveField } = require("../../packages/interface-dms/dist/base/table-view/meta.js"); - /** @type {Record} */ - const observed = {}; - /** @type {string[]} */ - const reads = []; - class Controller {} - const bare = {}; - Object.setPrototypeOf(bare, Controller.prototype); - const receiver = new Proxy(bare, { - get(target, key, self) { - reads.push(String(key)); - return forwardRead(target, key, self); - }, - }); - const meta = getTableViewMetaFor(receiver); - observed.sameMetaPerClass = getTableViewMetaFor(new Controller()) === meta; - observed.constructorReads = reads.slice(); - reads.length = 0; - const tenantAccess = require("../../packages/interface-dms/dist/tenant-access.js"); - const originalAssert = tenantAccess.AssertTenantAccess; - tenantAccess.AssertTenantAccess = async () => { - throw new Error("tenant gate reached"); - }; - const { User } = require("../../packages/interface-dms/dist/auth/db/tables/users.table.js"); - const user = new User(); - user._id = "u"; - const outcomes = []; - try { - for (const target of [receiver, {}, { constructor: Controller }, null, undefined, 1, "text"]) { - try { - await authorizeAction(target, "list", user, "tenant"); - outcomes.push("returned"); - } catch (error) { - const failure = thrownOf(error); - outcomes.push(`${failure.name}:${failure.message.slice(0, 60)}`); - } - } - } finally { - tenantAccess.AssertTenantAccess = originalAssert; - } - observed.authorize = outcomes; - observed.authorizeReads = reads.slice(); - const archived = []; - class Archiving {} - for (const key of ["archivedAt", Symbol.for("sym"), 7]) { - try { - ArchiveField()(Archiving.prototype, key); - archived.push(`ok:${String(key)}`); - } catch (error) { - const failure = thrownOf(error); - archived.push(`${failure.name}:${failure.message.slice(0, 60)}`); - } - } - observed.archiveField = archived; - observed.archiveFieldSet = getTableViewMetaFor(new Archiving()).archiveField ?? null; - return observed; -} - -/** - * @param {unknown} left - * @param {unknown} right - */ -function sameJson(left, right) { - return JSON.stringify(left) === JSON.stringify(right); -} - -function negativeControls() { - /** @type {Record} */ - const controls = {}; - const real = observeParser(); - const mutated = structuredClone(real); - mutated.parsed[0].json = "false"; - controls.parserDetectsChangedKeyword = !sameJson(real, mutated); - - const reordered = describeBattery( - untyped.rerunDescribeSchema, - untyped.rerunDescribeOptions, - ); - const twiceLazy = asObject(parseJson(JSON.stringify(reordered))); - const lazyCalls = asObject(asObject(twiceLazy.lazy).calls); - lazyCalls.lazy = Number(lazyCalls.lazy) + 1; - controls.describeDetectsExtraGetterCall = !sameJson(reordered, twiceLazy); - const readsMoved = asObject(parseJson(JSON.stringify(reordered))); - asArray(readsMoved["reads:optionalString"]).reverse(); - controls.describeDetectsReadOrder = - asArray(asObject(reordered)["reads:optionalString"]).length > 1 && - !sameJson(reordered, readsMoved); - assert.ok(Object.values(controls).every(Boolean), JSON.stringify(controls)); - return controls; -} - -async function main() { - /** @type {Record} */ - const observations = {}; - for (const [name, run] of Object.entries({ describe: observeDescribe, parser: observeParser, archiveRules: observeArchiveRules, choices: observeChoices, formPages: observeFormPages, receivers: observeReceivers })) { - observations[name] = await run(); - } - observations.controls = negativeControls(); - process.stdout.write(`${JSON.stringify(observations)}\n`); -} - -main().catch((/** @type {unknown} */ error) => { - process.stderr.write(`${error instanceof Error ? error.stack : String(error)}\n`); - process.exit(1); -}); diff --git a/.agents/probes/interface-components-builder.cjs b/.agents/probes/interface-components-builder.cjs deleted file mode 100644 index b2bbb40c9..000000000 --- a/.agents/probes/interface-components-builder.cjs +++ /dev/null @@ -1,268 +0,0 @@ -const path = require("node:path"); - -const { - execFileSync, -} = require("node:child_process"); - -const root = process.argv[2]; - -/** api-diff runs a scratch copy of this file, so relative requires must start from the tree under test. */ -module.filename = path.join(root, ".agents/probes/interface-components-builder.cjs"); - -const assert = require("node:assert/strict"); - -/** @typedef {InstanceType} Page */ - -/** - * @param {string} text - * @returns {unknown} - */ -function parseJson(text) { - return JSON.parse(text); -} - -const { - Action, - Component, - ComponentBuilder, - ComponentTarget, - getPermissionIdRef, -} = require("../../packages/interface-dms/dist/component.js"); - -async function observeUploadConstraintReads() { - const { PageMetadata } = require("../../packages/interface-dms/dist/page/metadata.js"); - const { Controller } = require("../../packages/interface-dms/node_modules/@antelopejs/interface-api"); - /** @type {{ SignUploadToken: typeof import("../../packages/interface-dms/dist/uploads.js").SignUploadToken }} */ - const uploads = require("../../packages/interface-dms/dist/uploads.js"); - const priorSigner = uploads.SignUploadToken; - const observed = []; - try { - for (const mode of ["undefined", "null", "false", "number", "string", "array", "proxy"]) { - const page = new PageMetadata(class ConstraintReadProbe extends Controller("") {}); - page["skipComponentPermissions"] = true; - page["componentMap"].set("constraint.component", { access: "own" }); - /** @type {unknown[]} */ - const trace = []; - let maxReads = 0; - let mimeReads = 0; - const proxy = new Proxy({}, { - get(_target, key, receiver) { - trace.push([key, receiver === proxy]); - if (key === "maxSize") return ++maxReads === 1 ? 17 : 29; - if (key === "allowedMimetypes") return ++mimeReads === 1 ? [17] : [false, "image/png", 31]; - }, - }); - /** @type {Record} */ - const values = { undefined, null: null, false: false, number: 17, string: "plain", array: [], proxy }; - const options = { - get constraints() { trace.push(["constraints", this === options]); return values[mode]; }, - }; - /** @type {unknown} */ - let constraints; - uploads.SignUploadToken = async (registration) => { - constraints = "constraints" in registration ? registration.constraints : undefined; - page["detached"] = true; - return "constraint-probe-token"; - }; - await page["registerNativeUploadField"]({ disabled: true, component: { options } }, "probe", "constraint.component", []); - observed.push({ mode, trace, constraints: constraints ?? null }); - } - } finally { - uploads.SignUploadToken = priorSigner; - } - assert.deepEqual(observed, [ - ...["undefined", "null", "false", "number", "string"].map((mode) => ({ mode, trace: [["constraints", true]], constraints: null })), - { mode: "array", trace: [["constraints", true]], constraints: {} }, - { mode: "proxy", trace: [["constraints", true], ["maxSize", true], ["maxSize", true], ["allowedMimetypes", true], ["allowedMimetypes", true]], constraints: { maxSize: 29, allowedMimetypes: ["image/png"] } }, - ]); - return observed; -} - -function observeDefaultTypesFresh() { - const script = ` - const { createRequire } = require("node:module"); - const { join } = require("node:path"); - const load = createRequire(join(process.argv[1], "packages/interface-dms/package.json")); - const core = load("./dist/base/data-types/core.js"); - const registrations = []; - const register = core.RegisterDataType; - core.RegisterDataType = (id) => { - registrations.push(id); - return register(id); - }; - load(process.argv[2]); - const defaults = load("./dist/base/data-types/default-types.js"); - const descriptors = (value) => Reflect.ownKeys(value).map((key) => { - const descriptor = Object.getOwnPropertyDescriptor(value, key); - return [String(key), descriptor.enumerable, descriptor.configurable, - descriptor.writable, Boolean(descriptor.get), Boolean(descriptor.set)]; - }); - const observations = Object.entries(defaults.DefaultDataTypes).map(([name, Type]) => { - if (typeof Type !== "function") return [name, "not a constructor"]; - const options = { marker: 73, min: 2, max: 11, minLength: 2, maxLength: 7, - items: [{ label: "First", value: "first" }, { label: "Other", value: "other" }], - dataApiController: class Related {}, keyMapping: { label: "title", value: "_id", parent: "parent" } }; - try { - const instance = new Type(options); - let validation; - try { - const schema = instance.getValidation(); - validation = [undefined, null, "x", "first", 1, 5, true, [], {}].map((value) => schema.safeParse(value).success); - } catch (error) { validation = { error: error.message }; } - const registered = core.getDataTypeId(instance); - return [name, Type.name, registered, Object.keys(instance), - instance.options === options, Object.getPrototypeOf(instance).constructor === Type, - registered ? core.CreateDataType(registered, options) instanceof Type : false, validation]; - } catch (error) { return [name, { error: error.message }]; } - }); - process.stdout.write(JSON.stringify({ registrations, observations, - exports: descriptors(defaults), namespace: descriptors(defaults.DefaultDataTypes), - select: defaults.SELECT_DISPLAYS, boolean: defaults.BOOLEAN_DISPLAYS })); - `; - return [ - "./dist/base/data-types/default-types.js", - "./dist/base/data-types/index.js", - "./dist/base/table-view.js", - ].map((entry) => parseJson(execFileSync(process.execPath, ["-e", script, root, entry], { encoding: "utf8" }))); -} - -function observeDataTypesAndHints() { - const { blockFetchUrlOption } = require("../../packages/interface-dms/dist/base/display.js"); - const { getUiHints } = require("../../packages/interface-dms/dist/base/block-registry/internal/ui.js"); - const { DataType, RegisterDataType, RegisterDataCompareMode, serializeType } = - require("../../packages/interface-dms/dist/base/data-types/index.js"); - /** @type {unknown[]} */ - const registrations = []; - class EmptyMode { - constructor() { - registrations.push("empty"); - } - filter() { - return false; - } - } - class KeptMode { - constructor() { - registrations.push("kept"); - } - filter() { - return false; - } - } - RegisterDataCompareMode("")(EmptyMode); - RegisterDataCompareMode("probe-kept")(KeptMode); - class ProbeType extends DataType { - constructor() { - super([EmptyMode, KeptMode], KeptMode, { marker: 61 }); - } - defaultInputComponent() { - return { componentName: "probe", options: {} }; - } - /** @returns {never} */ - getValidation() { - throw new TypeError("ProbeType has no validation"); - } - } - RegisterDataType("probe-type")(ProbeType); - /** @type {[string | undefined, import("../../packages/interface-dms/dist/base/block-registry/types.js").BlockResponseShape | undefined][]} */ - const sources = [ - [undefined, undefined], - ["period", undefined], - ["period", "series"], - ["", "series"], - ]; - const hints = sources.map(([period, response]) => { - const value = getUiHints( - blockFetchUrlOption("probe source", period, response), - ); - assert.ok(value, "getUiHints describes a fetch url option"); - return { value, keys: Object.keys(value) }; - }); - return { hints, registrations, type: serializeType(new ProbeType()) }; -} - -async function observe() { - const child = new ComponentBuilder("child").options({ label: "nested" }); - /** @type {ComponentBuilder>} */ - const builder = new ComponentBuilder("parent"); - builder - .options({ label: "original", watchActions: [{ forged: true }] }) - .mergeOptions({ label: "merged", marker: 17 }) - .child("nested", child, { slot: "aside", weight: 3 }) - .watch("load", "refresh", { params: { nonce: 7 } }) - .watch("change", "refresh", { params: { nonce: 19 } }) - .watchOn("nested", "load", "refresh", { params: { nonce: 23 } }) - .watchFilter(async (_permissions, watches) => watches.slice(1)) - .onFilter(async (_permissions, options) => ({ ...options, marker: 29 })); - const view = new Proxy(builder, {}); - const sync = view.serializeSync(); - const serialized = await view.serialize(); - /** @type {unknown} */ - const filtered = await view.onFilterCallback?.( - new Set(["page.component.refresh"]), - serialized.options, - "page.component", - { tenantId: "tenant", user: undefined }, - ); - const shadowedCallFilter = Object.assign( - /** @this {{ metadata: { name: string } }} @param {unknown} _permissions @param {Record} options */ - function (_permissions, options) { - return { ...options, receiver: this.metadata.name }; - }, - { call: undefined }, - ); - /** @type {ComponentBuilder>} */ - const shadowedCallBuilder = new ComponentBuilder("probe"); - shadowedCallBuilder - .options({ x: 17 }) - .onFilter(shadowedCallFilter); - /** @type {unknown} */ - const shadowedCall = await shadowedCallBuilder.onFilterCallback?.( - new Set(), - { x: 17 }, - "page.probe", - { tenantId: "tenant", user: undefined }, - ); - const plain = new Component( - { componentName: "positional", options: { x: 31 } }, - { name: "positional metadata" }, - () => undefined, - ); - const target = new ComponentTarget(plain, ["left", "right"]); - const priorGet = Object.getOwnPropertyDescriptor(getPermissionIdRef, "get"); - assert.ok(priorGet, "getPermissionIdRef.get is an own property"); - getPermissionIdRef.get = (component) => - component === plain ? "page.plain" : undefined; - const action = new Action( - "edit", - { title: "Edit", dependencies: ["view"] }, - plain, - ); - const permission = action.toPermission(); - Object.defineProperty(getPermissionIdRef, "get", priorGet); - return { - sync, - serialized, - filtered, - shadowedCall, - ownKeys: Object.keys(builder), - positional: { - metadata: plain.metadata, - info: plain.serializeSync(), - created: plain.onPageCreated !== undefined, - targetRoot: target.root === plain, - path: target.path, - }, - permission, - uploadConstraintReads: await observeUploadConstraintReads(), - defaultTypesFresh: observeDefaultTypesFresh(), - dataTypesAndHints: observeDataTypesAndHints(), - }; -} - -observe() - .then((result) => process.stdout.write(JSON.stringify(result))) - .catch((/** @type {unknown} */ error) => { - process.stderr.write(String(error instanceof Error ? error.stack : error)); - process.exitCode = 1; - }); diff --git a/.agents/probes/interface-components-owner-order.cjs b/.agents/probes/interface-components-owner-order.cjs deleted file mode 100644 index e1814c36b..000000000 --- a/.agents/probes/interface-components-owner-order.cjs +++ /dev/null @@ -1,384 +0,0 @@ -const path = require("node:path"); - -const root = process.argv[2]; - -/** api-diff runs a scratch copy of this file, so relative requires must start from the tree under test. */ -module.filename = path.join(root, ".agents/probes/interface-components-owner-order.cjs"); - -const assert = require("node:assert/strict"); - -/** @typedef {import("../../packages/interface-dms/dist/component.js").ComponentBuilder<{ marker: string }>} ComponentBuilderInstance */ -/** @typedef {import("../../packages/interface-dms/dist/page/metadata.js").PageMetadata} Page */ - -/** - * @param {unknown} value - * @returns {value is Record} - */ -function isObject(value) { - return Object(value) === value; -} - -/** - * @param {unknown} value - * @returns {value is Function} - */ -function isFunction(value) { - return typeof value === "function"; -} - -/** - * @param {unknown} value - * @returns {number} - */ -function watchCount(value) { - assert.ok(isObject(value) && "watchActions" in value && Array.isArray(value.watchActions), "Expected watch actions"); - return value.watchActions.length; -} - -/** - * @param {unknown} value - * @returns {Function} - */ -function ownMethod(value) { - assert.ok(isFunction(value), "Expected a function"); - return value; -} - -const { - ComponentBuilder, -} = require("../../packages/interface-dms/dist/component.js"); -const { Controller } = require("../../packages/interface-dms/node_modules/@antelopejs/interface-api"); - -/** - * @param {typeof import("../../packages/interface-dms/dist/page/metadata.js")["PageMetadata"]} PageMetadata - * @param {typeof import("../../packages/interface-dms/dist/page/internal/registry.js")["pageExtensions"]} pageExtensions - * @param {boolean} throws - */ -async function ownerExtensionComponent(PageMetadata, pageExtensions, throws) { - const page = new PageMetadata(class ExtensionOwnerAccessorProbe extends Controller("/probe") {}); - const before = new ComponentBuilder("before").options({ marker: "before" }); - const after = new ComponentBuilder("after").options({ marker: "after" }); - /** @type {import("../../packages/interface-dms/dist/page/types.js").PageExtensionComponent} */ - const contribution = { key: "child", component: before, side: "end", order: 0 }; - /** @type {import("../../packages/interface-dms/dist/page/types.js").PageExtensionInfo} */ - const info = { - extensionName: "owner-accessor-probe", - targetFullId: `owner-accessor-probe-${throws}`, - components: [contribution], - }; - /** @type {unknown[]} */ - const trace = []; - page["prepareComponent"] = /** @this {Page} @param {[ComponentBuilderInstance, string]} args */ function (...args) { - trace.push(["prepare", this === page, args.length, args[0].serializeSync().options?.marker, args[1]]); - throw new Error("owner-accessor-prepare-stop"); - }; - Object.defineProperty(page, throws ? "unregisterComponent" : "prepareComponent", { - get() { - trace.push(["get", this === page]); - contribution.component = after; - return /** @this {Page} @param {[ComponentBuilderInstance, string]} args */ function (...args) { - trace.push(["call", this === page, args.length, args[0].serializeSync().options?.marker, args[1]]); - return async () => ({ key: args[1], serialized: args[0].serializeSync() }); - }; - }, - }); - const prior = pageExtensions.get(info.targetFullId); - pageExtensions.set(info.targetFullId, [info]); - try { - await page["addExtension"](info); - return trace; - } finally { - if (prior === undefined) pageExtensions.delete(info.targetFullId); - else pageExtensions.set(info.targetFullId, prior); - } -} - -/** - * @param {typeof import("../../packages/interface-dms/dist/page/metadata.js")["PageMetadata"]} PageMetadata - * @param {typeof import("../../packages/interface-dms/dist/page/internal/registry.js")["pageExtensions"]} pageExtensions - */ -function ownerAccessorCases(PageMetadata, pageExtensions) { - return { - async preparePermissionId() { - const page = new PageMetadata(class PrepareOwnerAccessorProbe extends Controller("/probe") {}); - page["skipComponentPermissions"] = true; - page["pagePermissionId"] = "before"; - const component = new ComponentBuilder("child").options({ marker: 17 }); - /** @type {unknown[]} */ - const trace = []; - Object.defineProperty(page, "bindNativeUploadFields", { - get() { - trace.push(["get", this === page]); - page["pagePermissionId"] = "after"; - return /** @this {Page} @param {[{ componentName: string }, string]} args */ async function (...args) { - trace.push(["call", this === page, args.length, args[0].componentName, args[1]]); - }; - }, - }); - try { - await page["prepareComponent"](component, "child")(); - return trace; - } finally { - page.Dispose(); - } - }, - async uploadComponentName() { - const page = new PageMetadata(class UploadNameOwnerAccessorProbe extends Controller("/probe") {}); - page["componentMap"].set("page.child", { access: "own" }); - const serialized = { componentName: "before", options: { marker: 17 } }; - /** @type {unknown[]} */ - const trace = []; - Object.defineProperty(page, "bindUploadOptions", { - get() { - trace.push(["get", this === page]); - serialized.componentName = "after"; - return /** @this {Page} @param {unknown[]} args */ async function (...args) { - trace.push(["call", this === page, args.length, args[0] === serialized.options, args[1], args[2], args[3]]); - }; - }, - }); - await page["bindNativeUploadFields"](serialized, "page.child"); - return trace; - }, - async uploadChildId() { - const page = new PageMetadata(class UploadChildOwnerAccessorProbe extends Controller("/probe") {}); - page["componentMap"].set("page.child", { access: "own" }); - const component = new ComponentBuilder("child").options({ marker: 17 }).serializeSync(); - const child = { id: "before", component }; - const serialized = { componentName: "parent", children: [child] }; - /** @type {unknown[]} */ - const trace = []; - const bind = ownMethod(page["bindNativeUploadFields"]); - Object.defineProperty(page, "bindNativeUploadFields", { - get() { - trace.push(["get", this === page]); - child.id = "after"; - return /** @this {Page} @param {unknown[]} args */ async function (...args) { - trace.push(["call", this === page, args.length, args[0] === component, args[1]]); - }; - }, - }); - await bind.call(page, serialized, "page.child"); - return trace; - }, - async extensionPrepareComponent() { - return ownerExtensionComponent(PageMetadata, pageExtensions, false); - }, - async extensionUnregisterComponent() { - return ownerExtensionComponent(PageMetadata, pageExtensions, true); - }, - async unregisterNodeComponent() { - const page = new PageMetadata(class UnregisterNodeOwnerAccessorProbe extends Controller("/probe") {}); - page["registersComponentPermissions"] = () => true; - /** @type {unknown[]} */ - const trace = []; - let component = "before"; - const node = { - access: "shared", - get component() { - trace.push(["component", this === node]); - return component; - }, - }; - Object.defineProperty(page, "actionPermissions", { - get() { - trace.push(["get", this === page]); - component = "after"; - return /** @this {Page} @param {unknown[]} args */ function (...args) { - trace.push(["call", this === page, args.length, args[0]]); - return []; - }; - }, - }); - page["unregisterComponentNode"](node); - return trace; - }, - async uploadPathIterator() { - const page = new PageMetadata(class UploadPathOwnerAccessorProbe extends Controller("/probe") {}); - const bind = ownMethod(page["bindUploadOptions"]); - /** @type {unknown[]} */ - const trace = []; - let segment = "before"; - const path = { - *[Symbol.iterator]() { - trace.push(["iterate", this === path]); - yield segment; - }, - }; - Object.defineProperty(page, "bindUploadOptions", { - get() { - trace.push(["get", this === page]); - segment = "after"; - return /** @this {Page} @param {unknown[]} args */ async function (...args) { - trace.push(["call", this === page, args.length, args[0], args[1], args[2], args[3]]); - }; - }, - }); - await bind.call(page, { child: 17 }, "probe", "page.child", path); - return trace; - }, - }; -} - -async function observeOwnerAccessorOrder() { - const { PageMetadata } = require("../../packages/interface-dms/dist/page/metadata.js"); - const { pageExtensions } = require("../../packages/interface-dms/dist/page/internal/registry.js"); - /** @type {Record} */ - const results = {}; - /** @type {Record} */ - const expected = {}; - const failures = []; - const cases = ownerAccessorCases(PageMetadata, pageExtensions); - expected.preparePermissionId = [["get", true], ["call", true, 2, "child", "after.child"]]; - expected.uploadComponentName = [["get", true], ["call", true, 4, true, "after", "page.child", []]]; - expected.uploadChildId = [["get", true], ["call", true, 2, true, "page.child.after"]]; - expected.extensionPrepareComponent = [["get", true], ["call", true, 2, "after", "child"]]; - expected.extensionUnregisterComponent = [["prepare", true, 2, "before", "child"], ["get", true], ["call", true, 2, "after", "child"]]; - expected.unregisterNodeComponent = [["get", true], ["component", true], ["call", true, 1, "after"]]; - expected.uploadPathIterator = [["get", true], ["iterate", true], ["call", true, 4, 17, "probe", "page.child", ["after", "child"]]]; - for (const [name, run] of Object.entries(cases)) { - try { - results[name] = await run(); - assert.deepEqual(results[name], expected[name], name); - } catch (error) { - failures.push(error); - } - } - if (failures.length) { - throw new AggregateError(failures, `Owner accessor order failed: ${JSON.stringify(results)}\n${failures.map((error) => (error instanceof Error ? error.stack : String(error))).join("\n")}`); - } - return results; -} - -async function observePublicWatchFilterOrder() { - const { GetMetadata } = require("../../packages/interface-dms/node_modules/@antelopejs/interface-core"); - const { DataAPIMeta } = require("../../packages/interface-dms/node_modules/@antelopejs/interface-data-api/dist/metadata.js"); - const { TableView } = require("../../packages/interface-dms/dist/base/table-view/factory.js"); - const results = []; - for (let trial = 0; trial < 3; trial++) { - class TableController extends Controller("/probe") {} - class Table {} - GetMetadata(TableController, DataAPIMeta).tableClass = Table; - let expired = false; - let reads = 0; - /** @type {unknown[]} */ - const trace = []; - const builder = TableView(TableController, { - realtime: false, - formContainer: { type: "drawer" }, - }) - .watch("load", "refresh") - .watchFilter((_permissions, watches) => { - trace.push("watchFilter"); - return expired ? [] : watches; - }); - const options = builder.serializeSync().options; - Object.defineProperty(options, "enableTableExport", { - enumerable: true, - get() { - if (++reads === 2) { - queueMicrotask(() => queueMicrotask(() => { - expired = true; - trace.push("expire"); - })); - } - return false; - }, - }); - const filter = builder.onFilterCallback; - assert.ok(filter); - /** @type {unknown} */ - const result = await filter.call(builder, new Set(["*"]), options, "page.table", { tenantId: "tenant", user: undefined }); - results.push({ trace, watches: watchCount(result) }); - } - assert.deepEqual(results, Array.from({ length: 3 }, () => ({ - trace: ["watchFilter", "expire"], - watches: 1, - })), "A forwarding async callback must not delay the public watchFilter"); - return results; -} - -async function observePublicPageScheduling() { - const { ImplementInterface } = require("../../packages/interface-dms/node_modules/@antelopejs/interface-core"); - const { PageMetadata, GetPageLayoutBySlug } = require("../../packages/interface-dms/dist/page.js"); - const { SignUploadToken } = require("../../packages/interface-dms/dist/uploads.js"); - const { Schema } = require("../../packages/interface-dms/node_modules/@antelopejs/interface-database/dist/schema.js"); - const { RoleModel, TenantMemberModel } = require("../../packages/interface-dms/dist/db/index.js"); - const database = new Schema("public-scheduling-probe", {}).instance("probe"); - const memberModel = new TenantMemberModel(database); - const roleModel = new RoleModel(database); - ImplementInterface({ SignUploadToken }, { SignUploadToken: async () => "timing-token" }); - /** @type {unknown[]} */ - const trace = []; - let ticks = 0; - let active = true; - const tick = () => { - if (active) { - ticks++; - queueMicrotask(tick); - } - }; - class TimedComponent extends ComponentBuilder { - serialize() { - trace.push(["serialize", ticks]); - return super.serialize(); - } - } - const page = new PageMetadata(class PublicSchedulingProbe extends Controller("/probe") {}); - page.SetInfo("public-scheduling-probe", "/public-scheduling-probe", { - displayName: "Scheduling", - publicAccess: true, - noComponentPermissions: true, - }, { componentName: "layout" }); - page.SetComponent("first", new TimedComponent("first").options({ - nested: { field: { type: "file", component: { options: {} } } }, - })); - page.SetComponent("second", new TimedComponent("second").options({ marker: 17 })); - queueMicrotask(tick); - try { - await page.Register(); - trace.push(["registered", ticks]); - await page.SyncExtensions(); - trace.push(["synced", ticks]); - const layout = GetPageLayoutBySlug("/public-scheduling-probe"); - assert.ok(layout); - const result = await layout({ user: undefined, memberModel, roleModel, tenantId: "tenant" }); - trace.push(["layout", ticks]); - assert.deepEqual(Object.keys(result.components), ["first", "second"]); - active = false; - const failure = new Error("request getter"); - /** @type {Promise | undefined} */ - let rejected; - assert.doesNotThrow(() => { - rejected = layout({ - user: undefined, - memberModel, - roleModel, - tenantId: "tenant", - /** @returns {never} */ - get query() { throw failure; }, - }); - }); - assert.ok(rejected); - await assert.rejects(rejected, (/** @type {unknown} */ error) => error === failure); - return trace; - } finally { - active = false; - page.Dispose(); - } -} - -async function observe() { - return { - ownerAccessorOrder: await observeOwnerAccessorOrder(), - publicWatchFilterOrder: await observePublicWatchFilterOrder(), - publicPageScheduling: await observePublicPageScheduling(), - }; -} - -observe() - .then((result) => process.stdout.write(JSON.stringify(result))) - .catch((/** @type {unknown} */ error) => { - process.stderr.write(String(error instanceof Error ? error.stack : error)); - process.exitCode = 1; - }); diff --git a/.agents/probes/interface-components-read-order.cjs b/.agents/probes/interface-components-read-order.cjs deleted file mode 100644 index 14644800e..000000000 --- a/.agents/probes/interface-components-read-order.cjs +++ /dev/null @@ -1,438 +0,0 @@ -const path = require("node:path"); - -const root = process.argv[2]; - -/** api-diff runs a scratch copy of this file, so relative requires must start from the tree under test. */ -module.filename = path.join(root, ".agents/probes/interface-components-read-order.cjs"); - -const assert = require("node:assert/strict"); -const { Controller } = require("../../packages/interface-dms/node_modules/@antelopejs/interface-api"); - -/** @typedef {InstanceType} Page */ -/** @typedef {import("../../packages/interface-dms/node_modules/@antelopejs/interface-api/dist/route-registry").RouteHandler} Route */ - -/** - * @param {unknown} error - * @returns {string | undefined} - */ -function messageOf(error) { - return error instanceof Error ? error.message : undefined; -} - -/** - * @param {unknown} value - * @returns {Map} - */ -function asMap(value) { - assert.ok(value instanceof Map, "Expected a Map"); - return value; -} - -/** - * Reads through a proxy exactly as a proxy without a `get` trap would. - * @param {object} target - * @param {PropertyKey} key - * @param {unknown} receiver - * @returns {unknown} - */ -function forwardRead(target, key, receiver) { - // oxlint-disable-next-line anti-slop/no-reflect-get -- The get trap forwards the read with the trap's receiver, which only Reflect.get takes, so a getter on the proxied object keeps the proxy as this. - return Reflect.get(target, key, receiver); -} - -function observePageInfoPrecedence() { - const { PageMetadata } = require("../../packages/interface-dms/dist/page/metadata.js"); - const { GetMetadata } = require("../../packages/interface-dms/node_modules/@antelopejs/interface-core"); - const results = []; - for (const mode of ["explicit", "menu", "ancestor", "default", "controller"]) { - /** @type {unknown[]} */ - const trace = []; - const explicit = { componentName: "explicit-layout" }; - const menuLayout = { componentName: "menu-layout" }; - const ancestorLayout = { componentName: "ancestor-layout" }; - /** @type {import("../../packages/interface-dms/dist/page/types").CategoryInfo} */ - const ancestor = { id: "root", displayName: "Root", fullId: "root", fullSlug: "/root", category: undefined, layout: ancestorLayout }; - /** @type {import("../../packages/interface-dms/dist/page/types").PageInfo} */ - const categoryInfo = { - id: "category", displayName: "Category", fullId: "root.category", fullSlug: "/category", layoutUrl: "/category/pagelayout", - category: ancestor, hidden: true, publicAccess: true, authOnly: true, - memberAccess: mode === "menu" ? "self" : true, - bypassTenantAccessGate: true, - }; - const category = new Proxy( - categoryInfo, - { - get(target, key, receiver) { - trace.push(`category.${String(key)}`); - return forwardRead(target, key, receiver); - }, - }, - ); - class CategoryController extends Controller("/probe") {} - GetMetadata(CategoryController, PageMetadata).pageInfo = category; - class ParentController extends Controller("/probe") {} - GetMetadata(ParentController, PageMetadata).pageInfo = { id: "other", displayName: "Other", fullId: "other", fullSlug: "/parent-only", layoutUrl: "/parent-only/pagelayout", category: undefined }; - /** @type {import("../../packages/interface-dms/dist/page/types").MenuOptions} */ - const options = { - displayName: "Asymmetric page", - hidden: false, - publicAccess: false, - authOnly: false, - memberAccess: mode === "explicit" ? "self" : false, - bypassTenantAccessGate: false, - parent: ParentController, - }; - if (mode !== "default") { - options.category = mode === "controller" ? CategoryController : category; - } - if (mode === "explicit" || mode === "menu") options.layout = menuLayout; - const menu = new Proxy(options, { - get(target, key, receiver) { - trace.push(`menu.${String(key)}`); - return forwardRead(target, key, receiver); - }, - }); - class PageController extends Controller("/probe") {} - const page = new PageMetadata(PageController); - const receiver = new Proxy(page, { - set(target, key, value, owner) { - trace.push(`write.${String(key)}`); - return Reflect.set(target, key, value, owner); - }, - }); - const returned = receiver.SetInfo( - "leaf", - "/root//asymmetric/", - menu, - mode === "explicit" ? explicit : undefined, - ); - const info = page.pageInfo; - assert.ok(info); - const pageCategory = info.category; - const snapshot = { - ...info, - category: pageCategory && "fullId" in pageCategory ? pageCategory.fullId : undefined, - }; - results.push({ - mode, - info: snapshot, - keys: Object.keys(info), - layout: page.layout, - identity: [ - page.layout === explicit, - page.layout === menuLayout, - page.layout === ancestorLayout, - info.category === category, - ], - returnsUndefined: returned === undefined, - trace, - }); - } - return results; -} - -async function observeExtractedPageCalls() { - const { PageMetadata } = require("../../packages/interface-dms/dist/page/metadata.js"); - const api = require("../../packages/interface-dms/node_modules/@antelopejs/interface-api"); - const permissions = require("../../packages/interface-dms/dist/permissions.js"); - const { pageLayoutHandlers } = require("../../packages/interface-dms/dist/page/internal/registry.js"); - const priorRegister = api.RegisterRoute; - const priorUnregister = api.UnregisterRoute; - const priorPermission = permissions.HasPermission; - /** @type {Route | undefined} */ - let route; - /** @type {unknown[]} */ - const calls = []; - const page = new PageMetadata(class ForwardingProbe extends Controller("/probe") {}); - api.RegisterRoute = (value) => { - route = value; - return 17; - }; - api.UnregisterRoute = () => {}; - try { - page["resolvePageLayout"] = /** @this {Page} @param {unknown[]} args */ function (...args) { - calls.push({ - receiver: this === page, - argc: args.length, - options: args[1] ?? null, - }); - return Promise.resolve({ marker: 17 }); - }; - page["registerLayoutRoute"]({ - fullSlug: "/extraction-probe", - layoutUrl: "/extraction-probe/layout", - }); - assert.ok(route); - await route.callback.call({ - pageQuery: { q: "17" }, - pageParams: { id: "31" }, - }); - const handler = pageLayoutHandlers.get("/extraction-probe"); - assert.ok(handler); - const { Schema } = require("../../packages/interface-dms/node_modules/@antelopejs/interface-database/dist/schema.js"); - const { RoleModel, TenantMemberModel } = require("../../packages/interface-dms/dist/db/index.js"); - const database = new Schema("read-order-probe", {}).instance(); - await handler({ - user: undefined, - memberModel: new TenantMemberModel(database), - roleModel: new RoleModel(database), - tenantId: "tenant", - }); - page.Dispose(); - } finally { - api.RegisterRoute = priorRegister; - api.UnregisterRoute = priorUnregister; - } - permissions.HasPermission = async () => false; - let pageInfoReads = 0; - /** @type {unknown} */ - let layout; - try { - const owner = new PageMetadata(class LazyReadProbe extends Controller("/probe") {}); - owner["layoutRef"] = { - layout: { componentName: "probe" }, - components: { secret: { componentName: "secret", options: { marker: 31 } } }, - }; - owner["pagePermissionId"] = "page"; - const receiver = new Proxy(owner, { - get(target, key, receiver) { - if (key === "pageInfo") { - pageInfoReads++; - return { publicAccess: pageInfoReads > 1 }; - } - return forwardRead(target, key, receiver); - }, - }); - layout = await receiver["resolvePageLayout"]({ tenantId: "tenant" }); - } finally { - permissions.HasPermission = priorPermission; - } - let recursiveMethodReads = 0; - const receiver = new Proxy(new PageMetadata(class RecursionProbe extends Controller("/probe") {}), { - get(target, key, owner) { - if (key === "bindUploadOptions") recursiveMethodReads++; - return forwardRead(target, key, owner); - }, - }); - await receiver["bindUploadOptions"]({ a: { b: 17 } }, "probe", "page.probe", []); - return { calls, pageInfoReads, layout, recursiveMethodReads }; -} - -/** - * @param {() => Page} fresh - */ -function publishOrder(fresh) { - return [false, true].map((readAuthOnly) => { - const page = fresh(); - /** @type {unknown[]} */ - const trace = []; - const info = { - fullId: `order-publish-${readAuthOnly}`, - isModuleRoot: true, - get noComponentPermissions() { - trace.push(["noComponentPermissions", page["moduleScoped"], this === info]); - if (!readAuthOnly) throw new Error("order-stop"); - return false; - }, - get authOnly() { - trace.push(["authOnly", page["moduleScoped"], this === info]); - throw new Error("order-stop"); - }, - }; - try { - page["publish"](info); - } catch (error) { - if (messageOf(error) !== "order-stop") throw error; - } - /** @type {unknown} */ - const moduleScoped = page["moduleScoped"]; - const state = { trace, moduleScoped }; - page.Dispose(); - return state; - }); -} - -/** - * @param {() => Page} fresh - */ -function notifyOrder(fresh) { - return ["absent", "replace", "distinct"].map((mode) => { - const page = fresh(); - /** @type {unknown[]} */ - const trace = []; - let calls = 0; - let reads = 0; - let live = new Set(); - const first = live; - const second = new Set(); - Object.defineProperty(page, "notifiedComponents", { - get() { - trace.push(["notified", ++reads, this === page]); - return mode === "distinct" && reads > 1 ? second : live; - }, - }); - const component = { - get onPageCreated() { - trace.push(["hook", this === component]); - if (mode === "absent") return undefined; - if (mode === "replace") live = new Set([component]); - return /** @this {object} @param {unknown} owner */ function (owner) { - calls++; - trace.push(["call", this === component, owner === page]); - }; - }, - }; - page["notifyPageCreated"]({ component, depth: 0, permissionId: "order.component" }); - return { mode, trace, calls, firstContains: first.has(component), secondContains: second.has(component) }; - }); -} - -/** - * @param {() => Page} fresh - */ -function takenKeysOrder(fresh) { - return [false, true].map((throws) => { - const page = fresh(); - /** @type {unknown[]} */ - const trace = []; - let live = new Map(); - Object.defineProperty(page, "pageInfo", { - get() { trace.push(["pageInfo", this === page]); return { fullId: "order-page" }; }, - }); - const own = new Proxy({ own: {} }, { - ownKeys(target) { - trace.push(["ownKeys"]); - if (throws) throw new Error("order-stop"); - live = new Map([[{ extensionName: "replacement" }, [{ key: "injected" }]]]); - return Reflect.ownKeys(target); - }, - }); - Object.defineProperty(page, "ownComponents", { - get() { trace.push(["own", this === page]); return own; }, - }); - Object.defineProperty(page, "extensionEntries", { - get() { trace.push(["entries", this === page]); return live; }, - }); - /** @type {unknown[] | undefined} */ - let keys; - try { - keys = [...asMap(page["takenComponentKeys"]())]; - } catch (error) { - if (!throws || messageOf(error) !== "order-stop") throw error; - } - return { throws, trace, keys: keys ?? null }; - }); -} - -async function observeEvaluationOrder() { - const { PageMetadata } = require("../../packages/interface-dms/dist/page/metadata.js"); - const fresh = () => new PageMetadata(class EvaluationOrderProbe extends Controller("/probe") {}); - /** @type {Record} */ - const results = {}; - results.publish = publishOrder(fresh); - const api = require("../../packages/interface-dms/node_modules/@antelopejs/interface-api"); - const register = api.RegisterRoute; - const unregister = api.UnregisterRoute; - /** @type {Route | undefined} */ - let route; - api.RegisterRoute = (value) => { route = value; return 31; }; - api.UnregisterRoute = () => {}; - try { - class First extends Controller("/probe") {} - class Second extends Controller("/probe") {} - /** @type {unknown[]} */ - const trace = []; - let reads = 0; - const receiver = new Proxy(fresh(), { - get(target, key, owner) { - if (key === "target") { - trace.push(["target", ++reads, owner === receiver]); - return reads === 1 ? First : Second; - } - return forwardRead(target, key, owner); - }, - }); - receiver["registerLayoutRoute"]({ - fullSlug: "/order-route", - get layoutUrl() { trace.push(["layoutUrl"]); return "/order-route/layout"; }, - }); - assert.ok(route); - results.layoutRoute = { trace: [...trace], secondController: route.controller === Second }; - receiver.Dispose(); - } finally { - api.RegisterRoute = register; - api.UnregisterRoute = unregister; - } - results.notify = notifyOrder(fresh); - /** @type {unknown[]} */ - const addExtension = []; - results.addExtension = addExtension; - for (const throws of [false, true]) { - const page = fresh(); - page["takenComponentKeys"] = () => new Map(); - const stale = asMap(page["extensionEntries"]); - const replacement = new Map(); - /** @type {unknown[]} */ - const trace = []; - let live = stale; - Object.defineProperty(page, "extensionEntries", { - get() { trace.push(["entries", this === page]); return live; }, - }); - const info = { - extensionName: "order-extension", - targetFullId: "order-page", - get components() { - trace.push(["components", this === info]); - if (throws) throw new Error("order-stop"); - live = replacement; - return []; - }, - }; - try { - await page["addExtension"](info); - } catch (error) { - if (!throws || messageOf(error) !== "order-stop") throw error; - } - addExtension.push({ throws, trace, liveContains: replacement.has(info), staleContains: stale.has(info) }); - } - results.takenKeys = takenKeysOrder(fresh); - assert.deepEqual(results, { - publish: [ - { trace: [["noComponentPermissions", true, true]], moduleScoped: true }, - { trace: [["noComponentPermissions", true, true], ["authOnly", true, true]], moduleScoped: true }, - ], - layoutRoute: { - trace: [["target", 1, true], ["layoutUrl"], ["target", 2, true]], - secondController: true, - }, - notify: [ - { mode: "absent", trace: [["hook", true]], calls: 0, firstContains: false, secondContains: false }, - { mode: "replace", trace: [["hook", true], ["notified", 1, true]], calls: 0, firstContains: false, secondContains: false }, - { mode: "distinct", trace: [["hook", true], ["notified", 1, true], ["notified", 2, true], ["hook", true], ["call", true, true]], calls: 1, firstContains: false, secondContains: true }, - ], - addExtension: [ - { throws: false, trace: [["components", true], ["entries", true], ["components", true]], liveContains: true, staleContains: false }, - { throws: true, trace: [["components", true]], liveContains: false, staleContains: false }, - ], - takenKeys: [ - { throws: false, trace: [["pageInfo", true], ["own", true], ["ownKeys"], ["entries", true]], keys: [["own", "order-page"], ["injected", "replacement"]] }, - { throws: true, trace: [["pageInfo", true], ["own", true], ["ownKeys"]], keys: null }, - ], - }); - return results; -} - -async function observe() { - return { - pageInfoPrecedence: observePageInfoPrecedence(), - extractedPageCalls: await observeExtractedPageCalls(), - evaluationOrder: await observeEvaluationOrder(), - }; -} - -observe() - .then((result) => process.stdout.write(JSON.stringify(result))) - .catch((/** @type {unknown} */ error) => { - process.stderr.write(String(error instanceof Error ? error.stack : error)); - process.exitCode = 1; - }); diff --git a/.agents/probes/interface-components-routes.cjs b/.agents/probes/interface-components-routes.cjs deleted file mode 100644 index 03886b9c1..000000000 --- a/.agents/probes/interface-components-routes.cjs +++ /dev/null @@ -1,435 +0,0 @@ -const path = require("node:path"); - -const root = process.argv[2]; - -/** api-diff runs a scratch copy of this file, so relative requires must start from the tree under test. */ -module.filename = path.join(root, ".agents/probes/interface-components-routes.cjs"); - -/** - * @param {unknown} error - * @returns {string | undefined} - */ -function messageOf(error) { - return error instanceof Error ? error.message : undefined; -} - -const { - ComponentBuilder, -} = require("../../packages/interface-dms/dist/component.js"); -const { IncomingMessage, ServerResponse } = require("node:http"); -const { Socket } = require("node:net"); -const untyped = require("./untyped/interface-components-routes.cjs"); -const { HTTPResult } = require("../../packages/interface-dms/node_modules/@antelopejs/interface-api"); -const { User } = require("../../packages/interface-dms/dist/auth/db/tables/users.table.js"); - -/** - * A request context over an unconnected socket, with no headers. - * @returns {import("../../packages/interface-dms/node_modules/@antelopejs/interface-api").RequestContext} - */ -function requestContext() { - const rawRequest = new IncomingMessage(new Socket()); - rawRequest.headers = {}; - return { - rawRequest, - rawResponse: new ServerResponse(rawRequest), - url: new URL("http://probe/"), - routeParameters: {}, - response: new HTTPResult(), - }; -} - -/** - * @param {string} id - */ -function probeUser(id) { - const user = new User(); - user._id = id; - return user; -} - -/** - * @param {{ CheckTenantAccess: typeof import("../../packages/interface-dms/dist/tenant-access.js").CheckTenantAccess }} tenantAccess - */ -function openTenantGate(tenantAccess) { - const previousGate = tenantAccess.CheckTenantAccess; - /** @type {unknown[]} */ - const gates = []; - tenantAccess.CheckTenantAccess = async (/** @type {unknown[]} */ ...args) => { - gates.push(args); - return { allowed: true }; - }; - return { - gates, - restore() { - tenantAccess.CheckTenantAccess = previousGate; - }, - }; -} - -/** - * @param {object} receiver - */ -function bodyRewritingGuards(receiver) { - /** @type {unknown[]} */ - const events = []; - return { - events, - guards: { - /** - * @this {object} - * @param {unknown} _context - * @param {{ body: { count?: number, discard?: unknown } }} request - */ - new: function (_context, request) { - const value = request.body; - events.push({ guardReceiver: this === receiver }); - const { count } = value; - if (count === undefined) throw new TypeError("The guarded body has no count"); - value.count = count + 7; - delete value.discard; - return () => { - events.push("after"); - }; - }, - }, - }; -} - -async function observeRoutes() { - const { GetMetadata } = require("../../packages/interface-dms/node_modules/@antelopejs/interface-core"); - const { TableViewMeta } = require("../../packages/interface-dms/dist/base/table-view/meta.js"); - const { createGuardedWriteRoute } = require("../../packages/interface-dms/dist/base/table-view/internal/guards.js"); - const { withActionCheck } = require("../../packages/interface-dms/dist/base/table-view/internal/auth.js"); - const { withRealtimeMutation, withPresenceAcquire } = require("../../packages/interface-dms/dist/base/table-view/internal/realtime.js"); - const tenantAccess = require("../../packages/interface-dms/dist/tenant-access.js"); - class ProbeController {} - const receiver = new Proxy(new ProbeController(), {}); - const meta = GetMetadata(ProbeController, TableViewMeta); - meta.options.realtime = false; - /** @type {unknown[]} */ - const calls = []; - const context = requestContext(); - /** - * One params object every route receives, so each base call can check it gets that exact object: the row id the edit, delete and mutation routes read, and a valid `new` route parameter, which takes only optional fields. - * @type {{ id: string } & import("../../packages/interface-dms/node_modules/@antelopejs/interface-data-api/dist/components").Parameters.NewParameters} - */ - const params = { id: "row-31" }; - const body = Buffer.from('{"count":17,"discard":true}'); - const sentinel = { returned: 43 }; - const base = { - method: "post", - args: [], - /** - * @this {object} - * @param {unknown[]} args - */ - async func(...args) { - calls.push({ - receiver: this === receiver, - context: args[0] === context, - params: args[1] === params, - tail: args.slice(2), - }); - return sentinel; - }, - }; - const mutation = withRealtimeMutation( - { eventType: "updated", extractIds: () => [] }, - base, - ); - const presence = withPresenceAcquire(base); - /** @type {unknown} */ - const mutationResult = await untyped.callRoute(mutation, receiver, [ - context, - params, - "left", - 47, - context, - "session-1", - probeUser("user-1"), - ]); - /** @type {unknown} */ - const presenceResult = await untyped.callRoute(presence, receiver, [ - context, - params, - "right", - context, - "session-2", - "1", - probeUser("user-2"), - ]); - const gate = openTenantGate(tenantAccess); - /** @type {unknown} */ - let authResult; - try { - const checked = withActionCheck("probe", base); - authResult = await checked.func.call( - receiver, - context, - params, - "auth", - probeUser("user-3"), - ); - } finally { - gate.restore(); - } - const { events, guards } = bodyRewritingGuards(receiver); - meta.setControllerGuards(guards); - const guarded = createGuardedWriteRoute( - { - ...base, - async func(...args) { - events.push("write"); - return base.func.apply(this, args); - }, - }, - "new", - ); - /** @type {unknown} */ - const guardResult = await guarded.func.call( - receiver, - context, - params, - body, - "trailing", - ); - meta.setControllerGuards({}); - /** @type {boolean | undefined} */ - let untouched; - const unguarded = createGuardedWriteRoute( - { - ...base, - func: async (_context, _params, value) => { - untouched = value === body; - }, - }, - "new", - ); - await unguarded.func.call(receiver, context, params, body); - return { - calls, - events, - gates: gate.gates, - untouched, - results: [mutationResult, presenceResult, authResult, guardResult].map( - (result) => result === sentinel, - ), - decoratorArgs: [mutation.args.length, presence.args.length], - }; -} - -function observeFactoryHelpers() { - const helpers = require("../../packages/interface-dms/dist/base/table-view/internal/factory-helpers.js"); - const actions = Array.from({ length: 128 }, (_, mask) => { - const bit = (/** @type {number} */ index) => !!(mask & (1 << index)); - const capabilities = { - hasNewForm: bit(0), - hasEditForm: bit(1), - hasViewForm: bit(2), - hasDeleteEndpoint: bit(3), - archiveMode: bit(4), - isExportEnabled: bit(5), - formsArePages: bit(6), - }; - const builder = new ComponentBuilder("capabilities").options({}); - helpers.registerTableViewActions(builder, capabilities); - return Object.entries(builder.actions).map(([id, action]) => [ - id, - action.definition, - ]); - }); - const frames = [ - { pageSlug: "/work/:id", formSlug: "/work/:id/items/:id/edit" }, - { pageSlug: "/:id/:id", formSlug: "/:id/:id/items/:id/edit" }, - { pageSlug: "/plain", formSlug: "/plain/items/new" }, - ]; - const routes = frames.map((frame) => ({ - redirect: helpers.buildFormRedirectUrl(frame), - defaults: helpers.buildFilterSubmitDefaults( - { - queryParamFilters: { search: { field: "query" } }, - routeParamFilters: { id: { field: "owner" } }, - }, - frame, - ), - })); - const urls = [ - undefined, - { - new: { urlSlug: "../create" }, - edit: { urlSlug: "/absolute/:id" }, - details: { urlSlug: "nested/:id" }, - }, - ].map((pages) => helpers.buildFormPageUrls("/work/:id/", "items", pages)); - const displays = untyped.serializeTableViewDisplays({ - displays: [{ id: "cards" }, { id: "kanban", options: { marker: 9 } }], - card: { fields: ["name"], minWidth: 317, skeletonHeight: 91 }, - kanban: { groupBy: "status" }, - }); - const rowActions = untyped.serializeRowActions({ - add: { confirm: { title: "Add" } }, - edit: true, - delete: { rule: { field: "status", equals: "draft" } }, - custom: [{ id: "open", label: "Open", target: { type: "url", url: "/open" } }], - }); - return { actions, routes, urls, displays, rowActions }; -} - -function observePageAndTableMetadata() { - const { PageMetadata } = require("../../packages/interface-dms/dist/page/metadata.js"); - const { TableViewMeta } = require("../../packages/interface-dms/dist/base/table-view/meta.js"); - const { Controller } = require("../../packages/interface-dms/node_modules/@antelopejs/interface-api"); - class ParentController extends Controller("") {} - class ChildController extends Controller("") {} - const parent = new PageMetadata(ParentController); - parent.SetInfo( - "parent", - "/work/:id", - { - displayName: "Parent", - hidden: true, - }, - { componentName: "parent-layout" }, - ); - const child = new PageMetadata(ChildController); - child.SetInfo( - "child", - "/work/:id/child", - { - displayName: "Child", - category: parent.pageInfo, - }, - { componentName: "child-layout" }, - ); - /** @type {ComponentBuilder} */ - const first = new ComponentBuilder("first"); - first.options({ enableTableExport: false, searchable: false, formComponents: {} }); - /** @type {ComponentBuilder} */ - const second = new ComponentBuilder("second"); - second.options({ enableTableExport: true, searchable: true, formComponents: {} }); - parent.SetComponent("first", first); - child.SetComponent("second", second); - child.inherit(parent); - const meta = new TableViewMeta(ParentController); - meta.addComponentBuilder(first, "write"); - meta.addComponentBuilder(second, "read"); - meta.addComponentBuilder(first, "write"); - meta.recordGateBypassRegistration(false); - meta.recordGateBypassRegistration(true); - meta.setOptions({ caption: "Parent", rowIdKey: "identity" }); - const inherited = new TableViewMeta(ChildController); - inherited.inherit(meta); - return { - pageInfo: child.pageInfo, - layout: child.layout, - componentKeys: Object.keys(child.components), - componentIdentity: [ - child.components.first === first, - child.components.second === second, - ], - targetIdentity: [ - parent.target === ParentController, - child.target === ChildController, - ], - builderIdentity: meta.componentBuilders.map((builder) => - builder === first ? "first" : "second", - ), - writers: meta.writingComponentBuilders.map((builder) => builder === first), - mixedGateRegistrations: meta.hasMixedGateBypassRegistrations, - inheritedOptions: inherited.options, - copiedOptions: inherited.options !== meta.options, - inheritedBuilders: inherited.componentBuilders.length, - }; -} - -async function observeTableViewLifecycle() { - const { TableView } = require("../../packages/interface-dms/dist/base/table-view/factory.js"); - const { PageMetadata } = require("../../packages/interface-dms/dist/page/metadata.js"); - const { permissionMap } = require("../../packages/interface-dms/dist/page/internal/registry.js"); - const permissions = require("../../packages/interface-dms/dist/permissions.js"); - const { GetMetadata } = require("../../packages/interface-dms/node_modules/@antelopejs/interface-core"); - const { DataAPIMeta } = require("../../packages/interface-dms/node_modules/@antelopejs/interface-data-api/dist/metadata.js"); - const { Controller: ApiController } = require("../../packages/interface-dms/node_modules/@antelopejs/interface-api"); - const previous = permissions.HasPermission; - /** @type {unknown[]} */ - const calls = []; - permissions.HasPermission = async (held, id) => { - calls.push(id); - return held.has(id); - }; - try { - const results = []; - /** @type {import("../../packages/interface-dms/dist/base/table-view.js").FormContainer["type"][]} */ - const containers = ["drawer", "page"]; - for (const type of containers) { - class Controller extends ApiController("") {} - class Table {} - Controller.location = "/probe"; - GetMetadata(Controller, DataAPIMeta).tableClass = Table; - const builder = TableView(Controller, { - caption: "Lifecycle", - realtime: false, - formContainer: { type }, - rowClick: "double", - rowIdKey: "identity", - }); - const receiver = new Proxy(builder, {}); - class PageController extends ApiController("") {} - const parent = new PageMetadata(PageController); - parent.SetInfo( - "page", - "/work/:id", - { - displayName: "Page", - }, - { componentName: "layout" }, - ); - permissionMap.set(builder, "page.table"); - permissionMap.set(PageController, "page"); - receiver.onPageCreated?.(parent); - const serialized = receiver.serializeSync(); - /** @type {unknown} */ - const filtered = await receiver.onFilterCallback?.( - new Set(["page.table.edit", "page.table.view"]), - serialized.options, - "page.table", - { tenantId: "tenant", user: undefined }, - ); - /** @type {string | undefined} */ - let missingPage; - try { - receiver.onPageCreated?.(new PageMetadata(class EmptyPage extends ApiController("") {})); - } catch (error) { - missingPage = messageOf(error); - } - results.push({ - serialized, - filtered, - missingPage, - optionKeys: Object.keys(serialized.options ?? {}), - actions: Object.keys(builder.actions), - }); - permissionMap.delete(builder); - permissionMap.delete(PageController); - } - return { results, permissionCalls: calls }; - } finally { - permissions.HasPermission = previous; - } -} - -async function observe() { - return { - routes: await observeRoutes(), - factoryHelpers: observeFactoryHelpers(), - metadata: observePageAndTableMetadata(), - lifecycle: await observeTableViewLifecycle(), - }; -} - -observe() - .then((result) => process.stdout.write(JSON.stringify(result))) - .catch((/** @type {unknown} */ error) => { - process.stderr.write(String(error instanceof Error ? error.stack : error)); - process.exitCode = 1; - }); diff --git a/.agents/probes/interface-components-type-boundaries.cjs b/.agents/probes/interface-components-type-boundaries.cjs deleted file mode 100644 index b2f724b99..000000000 --- a/.agents/probes/interface-components-type-boundaries.cjs +++ /dev/null @@ -1,113 +0,0 @@ -const path = require("node:path"); - -const root = process.argv[2]; - -/** api-diff runs a scratch copy of this file, so relative requires must start from the tree under test. */ -module.filename = path.join(root, ".agents/probes/interface-components-type-boundaries.cjs"); - -const assert = require("node:assert/strict"); -const { IncomingMessage, ServerResponse } = require("node:http"); -const { Socket } = require("node:net"); -const { HTTPResult } = require("../../packages/interface-dms/node_modules/@antelopejs/interface-api"); - -/** - * @param {import("node:http").IncomingHttpHeaders} headers - * @returns {import("../../packages/interface-dms/node_modules/@antelopejs/interface-api").RequestContext} - */ -function requestContext(headers) { - const rawRequest = new IncomingMessage(new Socket()); - rawRequest.headers = headers; - return { - rawRequest, - rawResponse: new ServerResponse(rawRequest), - url: new URL("http://probe/"), - routeParameters: {}, - response: new HTTPResult(), - }; -} - -/** - * Builds an object that inherits from `prototype` and owns `own`, as `Object.create(prototype)` followed by `Object.assign` does. - * @param {object} prototype - * @param {Record} own - */ -function inherit(prototype, own) { - const created = {}; - Object.setPrototypeOf(created, prototype); - return Object.assign(created, own); -} - -async function observeTypeBoundaries() { - const api = require("../../packages/interface-dms/node_modules/@antelopejs/interface-api"); - const { CreateJwtAuthDecorator } = require("../../packages/interface-dms/dist/auth/internal/jwt-auth-decorator.js"); - const { getAuthenticatedRequestTenantId } = require("../../packages/interface-dms/dist/auth/internal/request-authenticators.js"); - const { isPrimitive } = require("../../packages/interface-dms/dist/utils/internal/type-check.js"); - const { CreateDataType } = require("../../packages/interface-dms/dist/base/data-types/index.js"); - const { collectFileRefs, remapRecordFileKeys } = require("../../packages/interface-dms/dist/base/helpers/file-refs.js"); - const { RegistrationIdentity } = require("../../packages/interface-dms/dist/page/internal/registry.js"); - const priorProvider = api.SetParameterProvider; - const authenticated = []; - /** @type {unknown[]} */ - const reads = []; - /** @type {{ provide(context: ReturnType): unknown } | undefined} */ - let provider; - api.SetParameterProvider = (_target, _key, _index, value) => { provider = { provide: value }; }; - try { - for (const value of [null, false, 17, "plain", {}, { _id: 23 }, inherit({ - /** @this {{ marker?: number }} */ - get _id() { reads.push(this.marker === 31); return "user-31"; }, - }, { marker: 31 })]) { - const decorate = CreateJwtAuthDecorator({ - authenticator: async () => ({ tenantId: "tenant-17" }), - validator: async () => value, - }); - decorate()({}, "user"); - const ctx = requestContext({ authorization: "Bearer token" }); - assert.ok(provider); - const result = await provider.provide(ctx); - authenticated.push({ - identity: result === value, - tenant: getAuthenticatedRequestTenantId(ctx) ?? null, - }); - } - } finally { - api.SetParameterProvider = priorProvider; - } - /** @type {unknown[]} */ - const keyReads = []; - const node = inherit({ - get key() { keyReads.push(this === node); return "image-17"; }, - }, { extra: 31 }); - const columns = { image: { type: CreateDataType("image", { storage: "proof" }) } }; - const refs = collectFileRefs(columns, { image: node }); - const remapped = await remapRecordFileKeys(columns, { image: node }, async (key, storage) => `${storage}/${key}`); - const identity = new RegistrationIdentity("typed-local-probe"); - const original = {}; - identity.remember(original); - let tokenReads = 0; - const view = new Proxy(original, { - get(target, key) { - if (key === Symbol.for("@antelopejs/interface-dms:pageRegistration")) tokenReads++; - /** @type {unknown} */ - const value = Object.getOwnPropertyDescriptor(target, key)?.value; - return value; - }, - }); - return { - authenticated, - reads, - primitives: [undefined, null, false, 0, "text", 17n, Symbol("x"), {}, [], () => 1].map(isPrimitive), - refs, - remapped, - keyReads, - resolved: identity.resolve(view) === original, - tokenReads, - }; -} - -observeTypeBoundaries() - .then((typeBoundaries) => process.stdout.write(JSON.stringify({ typeBoundaries }))) - .catch((/** @type {unknown} */ error) => { - process.stderr.write(String(error instanceof Error ? error.stack : error)); - process.exitCode = 1; - }); diff --git a/.agents/probes/interface-dms-layers.cjs b/.agents/probes/interface-dms-layers.cjs deleted file mode 100644 index 2603412e2..000000000 --- a/.agents/probes/interface-dms-layers.cjs +++ /dev/null @@ -1,655 +0,0 @@ -const fs = require("node:fs"); -const os = require("node:os"); -const nodePath = require("node:path"); -const { sep } = nodePath; - -/** Orders strings by UTF-16 code unit, the order `sort()` uses with no comparator. - * @param {string} left - * @param {string} right - */ -function byCodeUnit(left, right) { - if (left < right) return -1; - return left > right ? 1 : 0; -} - -const root = process.argv[2]; -const report = process.env.INTERFACE_DMS_LAYERS_REPORT === "1"; -const packageRoot = nodePath.join(root, "packages/interface-dms"); -const realDist = nodePath.join(packageRoot, "dist"); - -/** @type {Array<[oldEntry: string, names: string[], newEntry: string]>} */ -const MOVED = [ - ["base/types/watch.js", ["ComponentId"], "watch-actions.js"], - ["base/types/tone.js", ["TONES"], "tones.js"], - ["base/types/export-status.js", ["ExportStatus"], "export-status.js"], - ["base/types/button.js", ["ButtonVariant"], "component-options/button.js"], - [ - "base/types/row-action-operators.js", - ["FIELD_OPERATORS"], - "component-options/row-action.js", - ], - [ - "base/data-types/core.js", - [ - "DataType", - "RegisterDataType", - "RegisterDataCompareMode", - "CreateDataType", - "getDataTypeId", - "serializeType", - ], - "component-options/data-type.js", - ], - [ - "base/confirm-dialog.js", - ["serializeConfirmDialog"], - "component-options/confirm-dialog.js", - ], - [ - "base/internal/confirm-dialog.js", - ["isConfirmFrom", "serializeActionConfirm"], - "component-options/internal/confirm-dialog.js", - ], - [ - "base/types/internal/action-target.js", - ["serializeActionTarget"], - "component-options/internal/action-target.js", - ], - [ - "base/internal/form-schema.js", - ["serializeFormField"], - "component-options/internal/form-field.js", - ], - [ - "base/layouts.js", - ["DefaultLayout", "FormPageLayout", "SettingsLayout", "EmptyLayout"], - "component-options/page-layout.js", - ], - [ - "base/internal/button-availability.js", - ["applyButtonAvailability"], - "component-options/internal/button-availability.js", - ], - [ - "base/table-view/internal/options.js", - ["TABLE_VIEW_COMPONENT_NAME"], - "component-options/internal/page-layout.js", - ], -]; - -const NEW_MODULES = new Set([ - "component-options/action-target.js", - "component-options/button.js", - "component-options/confirm-dialog.js", - "component-options/data-type.js", - "component-options/form-field.js", - "component-options/internal/button-availability.js", - "component-options/internal/form-field.js", - "component-options/internal/page-layout.js", - "component-options/internal/action-target.js", - "component-options/internal/confirm-dialog.js", - "component-options/page-layout.js", - "component-options/row-action.js", - "export-status.js", - "invite-extension-payloads.js", - "tones.js", - "watch-actions.js", -]); - -const MOVED_FROM = new Map(Object.entries({ - "component-options/data-type.js": "base/data-types/core.js", - "component-options/page-layout.js": "base/layouts.js", - "component-options/confirm-dialog.js": "base/confirm-dialog.js", - "component-options/internal/confirm-dialog.js": - "base/internal/confirm-dialog.js", - "component-options/internal/action-target.js": - "base/types/internal/action-target.js", - "component-options/internal/form-field.js": "base/internal/form-schema.js", - "component-options/internal/button-availability.js": - "base/internal/button-availability.js", - "component-options/internal/page-layout.js": - "base/table-view/internal/options.js", - "component-options/row-action.js": "base/types/row-action-operators.js", - "component-options/button.js": "base/types/button.js", - "export-status.js": "base/types/export-status.js", - "tones.js": "base/types/tone.js", - "watch-actions.js": "base/types/watch.js", -})); - -const ISOLATED = [ - "index.js", - "auth/index.js", - "auth/db/index.js", - "auth/db/models/index.js", - "auth/db/tables/index.js", - "base/index.js", - "base/block-registry/index.js", - "base/data-types/index.js", - "base/types/index.js", - "data-controllers/index.js", - "db/index.js", - "db/models/index.js", - "db/tables/index.js", - "html-render/index.js", - "notifications/index.js", - "page.js", - "component.js", - "component-slots.js", - "guards.js", - "hooks.js", - "invites.js", - "invite-resolution.js", - "invite-extensions.js", - "layout-banners.js", - "permission-gate.js", - "permissions.js", - "quick-actions.js", - "tenant-ownership.js", - "base/confirm-dialog.js", - "base/data-types/core.js", - "base/form-types.js", - "base/internal/button-availability.js", - "base/internal/confirm-dialog.js", - "base/internal/form-schema.js", - "base/internal/layouts.js", - "base/layouts.js", - "base/table-view/internal/options.js", - "base/types/action-target.js", - "base/types/button.js", - "base/types/confirm-dialog.js", - "base/types/custom-button.js", - "base/types/export-status.js", - "base/types/form-values.js", - "base/types/internal/action-target.js", - "base/types/row-action-operators.js", - "base/types/row-action.js", - "base/types/size.js", - "base/types/tone.js", - "base/types/watch.js", - "db/internal/exportJobs.model.js", - "db/tables/exportJobs.table.js", - "db/tables/user_invites.table.js", - "internal/component-watches.js", - "invite-extensions/types.js", - "page/internal/layout-filter.js", - "page/internal/metadata-info.js", - "page/internal/nav-badges.js", - "page/internal/page-record.js", - "page/internal/table-view-ids.js", - "page/layout-filter.js", - "page/roots.js", - "page/types.js", -]; - -const mapMethods = Object.getOwnPropertyDescriptors(Map.prototype); -const weakMapMethods = Object.getOwnPropertyDescriptors(WeakMap.prototype); -const setMethods = Object.getOwnPropertyDescriptors(Set.prototype); -const originalMapSet = defined(mapMethods.set.value); -const originalMapHas = defined(mapMethods.has.value); -const originalWeakMapSet = defined(weakMapMethods.set.value); -const originalWeakMapHas = defined(weakMapMethods.has.value); -const originalWeakMapGet = defined(weakMapMethods.get.value); -const originalSetAdd = defined(setMethods.add.value); -const originalSetHas = defined(setMethods.has.value); - -/** - * @typedef {{ - * distRoot: string; - * labels: WeakMap; - * ordinals: Map; - * inserts: Map; - * }} Recording - */ - -/** @type {Recording | undefined} */ -let recording; -let inside = false; - -/** - * @param {unknown} value - * @returns {value is string} - */ -function isString(value) { - return typeof value === "string"; -} - -/** - * @param {unknown} value - * @returns {value is number} - */ -function isNumber(value) { - return typeof value === "number"; -} - -/** - * @param {unknown} value - * @returns {value is boolean} - */ -function isBoolean(value) { - return typeof value === "boolean"; -} - -/** - * @param {unknown} value - * @returns {value is bigint} - */ -function isBigInt(value) { - return typeof value === "bigint"; -} - -/** - * @param {unknown} value - * @returns {value is Function} - */ -function isFunction(value) { - return typeof value === "function"; -} - -/** - * @template T - * @param {T | undefined} value - * @returns {T} - */ -function defined(value) { - if (value === undefined) throw new TypeError("Expected a defined value"); - return value; -} - -/** - * @param {unknown} value - * @returns {value is symbol} - */ -function isSymbol(value) { - return typeof value === "symbol"; -} - -/** - * The text `typeof` gives, built from the guards above. - * @param {unknown} value - * @returns {string} - */ -function typeName(value) { - if (value === undefined) return "undefined"; - if (isString(value)) return "string"; - if (isNumber(value)) return "number"; - if (isBoolean(value)) return "boolean"; - if (isBigInt(value)) return "bigint"; - if (isSymbol(value)) return "symbol"; - if (isFunction(value)) return "function"; - return "object"; -} - -/** - * @param {unknown} value - * @returns {value is Record} - */ -function isObject(value) { - return Object(value) === value; -} - -/** - * @param {unknown} value - * @returns {string | null} - */ -function asLabel(value) { - if (value === null || isString(value)) return value; - throw new TypeError("Expected a recorded label"); -} - -/** - * @param {unknown} key - * @returns {string} - */ -function describeKey(key) { - if (isString(key) || isNumber(key)) return String(key); - if (isSymbol(key)) return key.toString(); - if (isFunction(key)) return `fn:${key.name || "anonymous"}`; - if (key === null || !isObject(key)) return String(key); - const tag = Reflect.getPrototypeOf(key)?.constructor?.name ?? "null"; - for (const field of ["id", "name", "key"]) { - const own = Object.getOwnPropertyDescriptor(key, field); - if (own && "value" in own && isString(own.value)) { - return `obj:${tag}:${own.value}`; - } - } - return `obj:${tag}`; -} - -/** - * @param {Recording} recording - * @param {object} container - * @returns {string | null} - */ -function containerLabel(recording, container) { - if (originalWeakMapHas.call(recording.labels, container)) { - return asLabel(originalWeakMapGet.call(recording.labels, container)); - } - const limit = Error.stackTraceLimit; - Error.stackTraceLimit = 80; - const stack = new Error().stack ?? ""; - Error.stackTraceLimit = limit; - const prefix = recording.distRoot + sep; - /** @type {string | undefined} */ - let file; - for (const line of stack.split("\n").slice(1)) { - const at = line.indexOf(prefix); - if (at === -1) continue; - const rest = line.slice(at + prefix.length); - file = rest.slice(0, rest.indexOf(".js:") + 3).split(sep).join("/"); - break; - } - let label = null; - if (file) { - const canonical = MOVED_FROM.get(file) ?? file; - const ordinal = (recording.ordinals.get(canonical) ?? 0) + 1; - originalMapSet.call(recording.ordinals, canonical, ordinal); - label = `${canonical}#${ordinal}`; - } - originalWeakMapSet.call(recording.labels, container, label); - return label; -} - -/** - * @param {object} container - * @param {unknown} key - * @param {boolean} isNew - */ -function record(container, key, isNew) { - if (!recording || inside || !isNew) return; - inside = true; - try { - const label = containerLabel(recording, container); - if (label) { - const list = recording.inserts.get(label) ?? []; - list.push(describeKey(key)); - originalMapSet.call(recording.inserts, label, list); - } - } finally { - inside = false; - } -} - -/** @param {unknown} key @param {unknown} value */ -Map.prototype.set = function set(key, value) { - record(this, key, !originalMapHas.call(this, key)); - return originalMapSet.call(this, key, value); -}; -/** @param {WeakKey} key @param {unknown} value */ -WeakMap.prototype.set = function set(key, value) { - record(this, key, !originalWeakMapHas.call(this, key)); - return originalWeakMapSet.call(this, key, value); -}; -/** @param {unknown} value */ -Set.prototype.add = function add(value) { - record(this, value, !originalSetHas.call(this, value)); - return originalSetAdd.call(this, value); -}; - -function clearCache() { - for (const key of Object.keys(require.cache)) delete require.cache[key]; -} - -function globalsSnapshot() { - return { - globals: Reflect.ownKeys(globalThis).map(String), - listeners: process - .eventNames() - .map((name) => `${String(name)}:${process.listenerCount(name)}`), - }; -} - -/** - * The one place this probe reads a module out of a dist tree: the modules are inspected as opaque records of exports. - * @param {string} distRoot - * @param {string} entry - * @returns {Record} - */ -function loadDist(distRoot, entry) { - return exportsOf(require(nodePath.join(distRoot, entry))); -} - -/** - * @param {unknown} value - * @returns {Record} - */ -function exportsOf(value) { - if (isObject(value)) return value; - throw new TypeError("Expected a module's exports to be an object"); -} - -/** - * @param {string} distRoot - * @param {string} entry - */ -function loadAlone(distRoot, entry) { - clearCache(); - const before = globalsSnapshot(); - recording = { - distRoot, - labels: new WeakMap(), - ordinals: new Map(), - inserts: new Map(), - }; - /** @type {string | undefined} */ - let error; - try { - loadDist(distRoot, entry); - } catch (caught) { - error = String(caught && (isObject(caught) ? caught.message : undefined)); - } - const { inserts } = recording; - recording = undefined; - const after = globalsSnapshot(); - const registries = Object.fromEntries( - [...inserts].sort(([a], [b]) => a.localeCompare(b)), - ); - const loadSet = Object.keys(require.cache) - .map((file) => - file.startsWith(distRoot + sep) - ? nodePath.relative(distRoot, file).split(sep).join("/") - : `npm:${file.slice(file.lastIndexOf(`node_modules${sep}`) + 13)}`, - ) - .sort(byCodeUnit); - return { - error, - registries, - globalsAdded: after.globals.filter((key) => !before.globals.includes(key)), - listenersChanged: after.listeners.filter( - (entry) => !before.listeners.includes(entry), - ), - loadSet, - }; -} - -/** - * @param {object} exportsObject - * @returns {string[]} - */ -function describeExports(exportsObject) { - return Reflect.ownKeys(exportsObject).map((key) => { - const d = defined(Object.getOwnPropertyDescriptor(exportsObject, key)); - const flags = `${d.enumerable ? "e" : "-"}${d.configurable ? "c" : "-"}`; - if (!("value" in d)) return `${String(key)} accessor ${flags}`; - /** @type {unknown} */ - const value = d.value; - const type = isFunction(value) - ? `function ${value.name}/${value.length}` - : value === null - ? "null" - : typeName(value); - return `${String(key)} data ${flags}${d.writable ? "w" : "-"} ${type}`; - }); -} - -/** - * @param {string} distRoot - * @returns {string[]} - */ -function listEntries(distRoot) { - /** @type {string[]} */ - const files = []; - (function walk(/** @type {string} */ directory) { - for (const item of fs.readdirSync(directory, { withFileTypes: true })) { - const full = nodePath.join(directory, item.name); - if (item.isDirectory()) walk(full); - else if (item.name.endsWith(".js")) { - files.push(nodePath.relative(distRoot, full).split(sep).join("/")); - } - } - })(distRoot); - return files.filter((file) => !NEW_MODULES.has(file)).sort(byCodeUnit); -} - -/** - * @param {string} distRoot - * @returns {Record} - */ -function observeExports(distRoot) { - clearCache(); - loadDist(distRoot, "index.js"); - /** @type {Record} */ - const entries = {}; - for (const entry of listEntries(distRoot)) { - entries[entry] = describeExports(loadDist(distRoot, entry)); - } - return entries; -} - -/** - * @param {string} distRoot - */ -function observeIdentity(distRoot) { - clearCache(); - loadDist(distRoot, "index.js"); - /** @type {[string, Record][]} */ - const all = listEntries(distRoot).map((entry) => [ - entry, - loadDist(distRoot, entry), - ]); - /** @type {{ old: string, type: string, aliases: string[], newPathSameObject: boolean }[]} */ - const result = []; - for (const [oldEntry, names, newEntry] of MOVED) { - const oldModule = loadDist(distRoot, oldEntry); - const newFile = nodePath.join(distRoot, newEntry); - const newModule = fs.existsSync(newFile) ? loadDist(distRoot, newEntry) : undefined; - for (const name of names) { - const value = oldModule[name]; - const aliases = all - .filter(([, exported]) => Object.hasOwn(exported, name)) - .map(([entry, exported]) => `${entry}:${exported[name] === value}`); - result.push({ - old: `${oldEntry}#${name}`, - type: typeName(value), - aliases, - newPathSameObject: newModule === undefined || newModule[name] === value, - }); - } - } - return result; -} - -/** - * @param {string} distRoot - */ -function observeIsolated(distRoot) { - /** @type {Record} */ - const result = {}; - for (const entry of ISOLATED) { - const { loadSet, ...observed } = loadAlone(distRoot, entry); - result[entry] = report ? { ...observed, loadSet } : observed; - } - return result; -} - -/** - * @template Observation - * @param {(distRoot: string) => Observation} run - * @returns {Observation} - */ -function withScratchCopy(run) { - const scratch = fs.mkdtempSync(nodePath.join(os.tmpdir(), "interface-dms-layers-")); - try { - const scratchPackage = nodePath.join(scratch, "packages/interface-dms"); - fs.mkdirSync(scratchPackage, { recursive: true }); - fs.cpSync(realDist, nodePath.join(scratchPackage, "dist"), { - recursive: true, - filter: (path) => !path.endsWith(".map") && !path.endsWith(".d.ts"), - }); - fs.copyFileSync( - nodePath.join(packageRoot, "package.json"), - nodePath.join(scratchPackage, "package.json"), - ); - fs.symlinkSync( - nodePath.join(packageRoot, "node_modules"), - nodePath.join(scratchPackage, "node_modules"), - "dir", - ); - fs.symlinkSync(nodePath.join(root, "node_modules"), nodePath.join(scratch, "node_modules"), "dir"); - return run(nodePath.join(scratchPackage, "dist")); - } finally { - clearCache(); - fs.rmSync(scratch, { recursive: true, force: true }); - } -} - -/** - * @param {string} distRoot - * @param {string} file - * @param {(code: string) => string} edit - * @param {() => unknown} observe - * @returns {boolean} - */ -function mutated(distRoot, file, edit, observe) { - const path = nodePath.join(distRoot, file); - const original = fs.readFileSync(path, "utf8"); - const before = JSON.stringify(observe()); - fs.writeFileSync(path, edit(original)); - try { - return JSON.stringify(observe()) !== before; - } finally { - fs.writeFileSync(path, original); - } -} - -function negativeControls() { - return withScratchCopy((distRoot) => ({ - registryOrderDetected: mutated( - distRoot, - "base/data-types/default-types.js", - (code) => - code.replace('"use strict";', '"use strict";\nrequire("./default/files");'), - () => loadAlone(distRoot, "base/data-types/index.js").registries, - ), - descriptorDetected: mutated( - distRoot, - "base/layouts.js", - (code) => - `${code}\n{ const kept = exports.DefaultLayout; Object.defineProperty(exports, "DefaultLayout", { enumerable: true, configurable: true, get: () => kept }); }\n`, - () => observeExports(distRoot)["base/layouts.js"], - ), - identityDetected: mutated( - distRoot, - "base/types/index.js", - (code) => - code.replace( - '__exportStar(require("./watch"), exports);', - 'Object.defineProperty(exports, "ComponentId", { enumerable: true, value: { ...require("./watch").ComponentId } });\n__exportStar(require("./watch"), exports);', - ), - () => observeIdentity(distRoot), - ), - loadSideEffectDetected: mutated( - distRoot, - "page/roots.js", - (code) => `${code}\nrequire("../base/data-types");\n`, - () => loadAlone(distRoot, "page.js").registries, - ), - })); -} - -const observations = { - exports: observeExports(realDist), - movedValues: observeIdentity(realDist), - isolated: observeIsolated(realDist), - negativeControls: negativeControls(), -}; -process.stdout.write(JSON.stringify(observations)); diff --git a/.agents/probes/playground.cjs b/.agents/probes/playground.cjs deleted file mode 100644 index ab8c0278e..000000000 --- a/.agents/probes/playground.cjs +++ /dev/null @@ -1,354 +0,0 @@ -const { execFileSync } = require("node:child_process"); -const { existsSync, readFileSync } = require("node:fs"); -const { createRequire, Module } = require("node:module"); -const path = require("node:path"); - -/** Orders strings by UTF-16 code unit, the order `sort()` uses with no comparator. - * @param {string} left - * @param {string} right - */ -function byCodeUnit(left, right) { - if (left < right) return -1; - return left > right ? 1 : 0; -} - -const root = process.argv[2]; -const playground = path.join(root, "packages/dms/playground"); - -/** api-diff runs a scratch copy of this file, so relative requires must start from the tree under test. */ -module.filename = path.join(root, ".agents/probes/playground.cjs"); - -execFileSync("pnpm", ["--dir", playground, "exec", "tsc"], { - cwd: root, - stdio: "pipe", -}); - -const NativeDate = Date; -Reflect.set(global, "Date", class extends NativeDate { - /** @param {[] | [value: number | string | Date] | [year: number, monthIndex: number, date?: number, hours?: number, minutes?: number, seconds?: number, ms?: number]} args */ - constructor(...args) { - if (args.length === 0) super("2026-10-26T12:00:00.000Z"); - else if (args.length === 1) super(args[0]); - else super(...args); - } - static now() { - return new NativeDate("2026-10-26T12:00:00.000Z").getTime(); - } -}); - -let randomState = 1; -Math.random = () => { - randomState = (Math.imul(randomState, 1664525) + 1013904223) >>> 0; - return randomState / 2 ** 32; -}; - -/** - * @param {unknown} value - * @returns {value is Function} - */ -function isFunction(value) { - return typeof value === "function"; -} - -/** - * @param {import("../../packages/dms/playground/node_modules/@antelopejs/interface-core/dist/decorators").Class} value - * @returns {value is ConstructorParameters[0]} - */ -function isControllerClass(value) { - return "location" in value && typeof value.location === "string" && "extend" in value && isFunction(value.extend); -} - -/** - * @param {unknown} value - * @returns {value is symbol} - */ -function isSymbol(value) { - return typeof value === "symbol"; -} - -/** - * @param {unknown} value - * @returns {value is Record} - */ -function isObject(value) { - return Object(value) === value; -} - -/** - * @param {unknown} value - * @returns {Record} - */ -function exportsOf(value) { - if (isObject(value)) return value; - throw new TypeError("Expected a module's exports to be an object"); -} - -/** - * @param {unknown} value - * @param {Set} [ancestors] - * @returns {unknown} - */ -function snapshot(value, ancestors = new Set()) { - if (value === undefined) return { type: "undefined" }; - if (isFunction(value)) - return { - type: "function", - name: value.name, - length: value.length, - async: value.constructor.name === "AsyncFunction", - }; - if (isSymbol(value)) return String(value); - if (value instanceof NativeDate) - return { type: "Date", milliseconds: value.getTime() }; - if (isObject(value)) { - if (ancestors.has(value)) return { type: "circular" }; - const next = new Set(ancestors).add(value); - if (Array.isArray(value)) return value.map((item) => snapshot(item, next)); - return Object.entries(value).map(([key, item]) => [ - key, - snapshot(item, next), - ]); - } - return value; -} - -/** - * @param {Record} value - */ -function descriptors(value) { - return Object.entries(Object.getOwnPropertyDescriptors(value)).map( - ([key, descriptor]) => [ - key, - { - writable: descriptor.writable, - enumerable: descriptor.enumerable, - configurable: descriptor.configurable, - get: snapshot(descriptor.get), - set: snapshot(descriptor.set), - value: snapshot("value" in descriptor ? descriptor.value : value[key]), - }, - ], - ); -} - -const leaves = { - "feature-index/blocks-charts": [ - "feature-index/blocks", - "CHART_TYPE_FEATURES CHART_OPTION_FEATURES CARD_FEATURES PERIOD_FEATURES", - ], - "feature-index/blocks-content": [ - "feature-index/blocks", - "STAT_GROUP_FEATURES KEY_VALUE_FEATURES NAV_CARD_FEATURES ACTIVITY_FEATURES METER_FEATURES BANNER_FEATURES", - ], - "feature-index/blocks-data": [ - "feature-index/blocks", - "STAT_GROUP KEY_VALUE NAV_CARDS FEEDBACK ACTIVITY METERS SETTINGS CODE GUARD LIVE PAGE_TOOLBAR PAGE_TOOLBAR_REPORT RECORD CATALOG CHART_DASHBOARD CHART_XY CHART_CIRCULAR CHART_REALTIME CHART_ADVANCED CHART_OPTIONS CHART_CARDS CHART_TOP_LIST CHART_PERIOD CHART_METRICS CHART_STATIC CHART_SCOPES GRID_SIMPLE GRID_STACKED GRID_HYBRID GRID_SPACER GRID_DENSE HSTACK VSTACK SPACER STACK_COMBINED TABS_BASIC TABS_COUNTS TABS_KEEP_ALIVE TABS_PERSIST TABS_VARIANTS MULTI_COMPONENT feature", - ], - "feature-index/blocks-foundations": [ - "feature-index/blocks", - "PLUMBING PERIOD_AND_REALTIME COMPOSED_TEXTS LIVE_FEATURES REGISTRY_FEATURES", - ], - "feature-index/blocks-layout": [ - "feature-index/blocks", - "CONTAINER_FEATURES EMPTY_AND_CODE_FEATURES GUARD_FEATURES LAYOUT_FEATURES TAB_FEATURES", - ], - "feature-index/table-view-actions": [ - "feature-index/table-view", - "ROW_ACTIONS", - ], - "feature-index/table-view-data": [ - "feature-index/table-view", - "V2_DESIGN SUBLINES SOURCE_TOKENS CARD_REORDER COLUMN_HIDDEN SIDE_PANEL CARD_ACTIONS REQUEST_LOG_GAPS PAGE_TOKENS VISIBLE_QUERY_FILTERS URL_TAB_FOLLOW DOWNLOAD_TARGET LATEST PAGES BY_STATUS_SLUG feature", - ], - "feature-index/table-view-displays": [ - "feature-index/table-view", - "DISPLAYS_AND_CARDS CELLS SOURCES DATA_AND_TENANCY", - ], - "feature-index/table-view-layout": [ - "feature-index/table-view", - "FORMS_AND_CONTAINERS LAYOUT_AND_PAGING FILTERS_TABS_VIEWS EMPTY_STATES_AND_REALTIME", - ], - "table-view/random-descriptions": [ - "table-view/defaultValues", - "RANDOM_DESCRIPTIONS", - ], - "table-view/random-names": ["table-view/defaultValues", "RANDOM_NAMES"], - "table-view/random-task-names": [ - "table-view/defaultValues", - "RANDOM_TASK_NAMES", - ], - "table-view/sample-addresses": [ - "table-view/defaultValues", - "SAMPLE_ADDRESSES", - ], -}; - -/** @type {Record>} */ -const virtualLeaves = {}; -const capture = Symbol.for("playground-probe.capture"); -Reflect.set(global, capture, (/** @type {string} */ name, /** @type {[string, unknown][]} */ entries) => { - /** @type {Record} */ - const exports = {}; - Object.defineProperty(exports, "__esModule", { value: true }); - for (const [key, value] of entries) exports[key] = value; - virtualLeaves[name] = exports; -}); - -const extension = Module._extensions[".js"]; -Module._extensions[".js"] = (module, filename) => { - const additions = Object.entries(leaves).filter( - ([name, [owner]]) => - filename === path.join(playground, "dist", owner + ".js") && - !existsSync(path.join(playground, "dist", name + ".js")), - ); - if (!additions.length) return extension(module, filename); - const captures = additions - .map( - ([name, [, names]]) => - `global[Symbol.for("playground-probe.capture")](${JSON.stringify(name)}, [${names - .split(" ") - .map((key) => `[${JSON.stringify(key)}, ${key}]`) - .join(",")}]);`, - ) - .join("\n"); - module._compile(readFileSync(filename, "utf8") + "\n" + captures, filename); -}; - -const local = createRequire(path.join(playground, "package.json")); - -/** - * @param {string} file - * @returns {Record} - */ -function loadBuilt(file) { - return exportsOf(local(path.join(playground, "dist", file))); -} - -const core = require("../../packages/dms/playground/node_modules/@antelopejs/interface-core"); -const { RunWithModuleContext } = require("../../packages/dms/playground/node_modules/@antelopejs/interface-core/dist/modules.js"); -/** - * @param {unknown} value - * @returns {unknown} - */ -function serializedWhenAble(value) { - if (isObject(value) && "serializeSync" in value && isFunction(value.serializeSync)) { - return value.serializeSync() ?? value; - } - return value; -} - -/** @type {unknown[]} */ -const registrations = []; -const load = Module._load; -/** - * @param {string} request - * @param {Module | null | undefined} parent - * @param {boolean} main - */ -Module._load = function (request, parent, main) { - if (request === "@antelopejs/interface-dms/page") { - const result = require("../../packages/dms/playground/node_modules/@antelopejs/interface-dms/dist/page.js"); - return { - ...result, - RegisterPage: - (/** @type {Parameters} */ ...args) => - (/** @type {Parameters>[0]} */ target) => { - registrations.push([ - "page", - target.name, - isControllerClass(target) ? core.GetMetadata(target, result.PageMetadata).pageInfo : null, - Object.entries(target).map(([key, value]) => [key, serializedWhenAble(value)]), - ]); - result.RegisterPage(...args)(target); - }, - }; - } - if (request === "@antelopejs/interface-database-decorators") { - const decorators = require("../../packages/dms/playground/node_modules/@antelopejs/interface-database-decorators"); - return { - ...decorators, - RegisterTable: - (/** @type {Parameters} */ ...args) => - (/** @type {Parameters>[0]} */ target) => { - registrations.push(["table", target.name, args]); - decorators.RegisterTable(...args)(target); - }, - }; - } - return load(request, parent, main); -}; - -RunWithModuleContext({ module: "playground-probe" }, () => - loadBuilt("index.js"), -); - -const dataModules = [ - "feature-index/blocks", - "feature-index/table-view", - "table-view/defaultValues", -]; -/** @type {Record} */ -const data = Object.fromEntries( - dataModules.map((name) => { - const exports = loadBuilt(name + ".js"); - if (exports !== loadBuilt(name + ".js")) - throw new Error("Module cache identity changed"); - return [name, descriptors(exports)]; - }), -); -/** - * @param {string} file - * @param {string} name - * @returns {unknown[]} - */ -function builtList(file, name) { - const value = loadBuilt(file)[name]; - if (!Array.isArray(value)) throw new TypeError(`${file} does not export a list named ${name}`); - return value; -} - -const catalog = builtList("feature-index/catalog.js", "PLAYGROUND_FEATURES"); -const blocks = builtList("feature-index/blocks.js", "BLOCKS_FEATURES"); -const tables = builtList("feature-index/table-view.js", "TABLE_VIEW_FEATURES"); -for (const [index, item] of [...blocks, ...tables].entries()) { - if (catalog[index] !== item) - throw new Error("Catalog no longer shares feature objects"); -} -data.catalog = snapshot(catalog); - -const files = execFileSync( - "git", - ["ls-files", "--", "packages/dms/playground/src"], - { cwd: root, encoding: "utf8" }, -) - .trim() - .split("\n"); -const names = new Set([ - ...files - .filter((file) => file.endsWith(".ts") && !file.endsWith(".test.ts")) - .map((file) => - file.replace("packages/dms/playground/src/", "").replace(/\.ts$/, ""), - ), - ...Object.keys(leaves), -]); -const modules = RunWithModuleContext({ module: "playground-probe" }, () => - Object.fromEntries( - [...names] - .sort(byCodeUnit) - .map((name) => [ - name, - descriptors( - virtualLeaves[name] ?? loadBuilt(name + ".js"), - ), - ]), - ), -); -const blocksRegistered = require("../../packages/dms/playground/node_modules/@antelopejs/interface-dms/dist/base/block-types.js").ListBlockTypes(); -process.stdout.write( - JSON.stringify({ - data, - registrations: snapshot(registrations), - blocksRegistered: snapshot(blocksRegistered), - modules, - }), -); diff --git a/.agents/probes/request-context.cjs b/.agents/probes/request-context.cjs deleted file mode 100644 index 578c37890..000000000 --- a/.agents/probes/request-context.cjs +++ /dev/null @@ -1,24 +0,0 @@ -const { IncomingMessage, ServerResponse } = require("node:http"); -const { Socket } = require("node:net"); -const { HTTPResult } = require("../../packages/interface-dms/node_modules/@antelopejs/interface-api"); - -/** - * A request context for a probe that calls a parameter provider or a route - * directly: an unconnected request carrying `headers`, its response, and an - * empty URL and route. - * @param {import("node:http").IncomingHttpHeaders} headers - * @returns {import("../../packages/interface-dms/node_modules/@antelopejs/interface-api").RequestContext} - */ -function requestContext(headers) { - const rawRequest = new IncomingMessage(new Socket()); - rawRequest.headers = headers; - return { - rawRequest, - rawResponse: new ServerResponse(rawRequest), - url: new URL("http://probe/"), - routeParameters: {}, - response: new HTTPResult(), - }; -} - -module.exports = { requestContext }; diff --git a/.agents/probes/runtime-declarations.d.ts b/.agents/probes/runtime-declarations.d.ts deleted file mode 100644 index abd8e4d68..000000000 --- a/.agents/probes/runtime-declarations.d.ts +++ /dev/null @@ -1,14 +0,0 @@ -import "node:module"; - -declare module "module" { - interface Module { - _compile(content: string, filename: string): void; - } - - namespace Module { - let _extensions: Record void>; - - // oxlint-disable-next-line anti-slop/no-unknown-returns -- Node's undocumented Module._load answers the exports of whatever module the request names, so its declaration has no narrower return. - let _load: (request: string, parent: Module | null | undefined, isMain: boolean) => unknown; - } -} diff --git a/.agents/probes/tsconfig.json b/.agents/probes/tsconfig.json deleted file mode 100644 index 1a4518b10..000000000 --- a/.agents/probes/tsconfig.json +++ /dev/null @@ -1,12 +0,0 @@ -{ - "extends": "../../tsconfig.json", - "compilerOptions": { - "allowJs": true, - "checkJs": true, - "noEmit": true, - "incremental": false, - "types": ["node"] - }, - "include": ["./**/*.cjs", "./runtime-declarations.d.ts"], - "exclude": ["./untyped/**"] -} diff --git a/.agents/probes/untyped/interface-assertions-1.cjs b/.agents/probes/untyped/interface-assertions-1.cjs deleted file mode 100644 index 8a4f45cc9..000000000 --- a/.agents/probes/untyped/interface-assertions-1.cjs +++ /dev/null @@ -1,33 +0,0 @@ -const describeModule = require("../../../packages/interface-dms/dist/base/block-registry/internal/describe.js"); -const { SelectType } = require("../../../packages/interface-dms/dist/base/data-types/default/choices.js"); -const { authorizeAction } = require("../../../packages/interface-dms/dist/base/table-view/internal/auth.js"); - -/** - * @param {unknown} schema - */ -function rerunDescribeSchema(schema) { - return describeModule.describeSchema(schema); -} - -/** - * @param {unknown} schema - */ -function rerunDescribeOptions(schema) { - return describeModule.describeOptions(schema); -} - -/** - * @param {unknown} options - */ -function selectTypeOf(options) { - return new SelectType(options); -} - -module.exports = { - describeSchema: describeModule.describeSchema, - describeOptions: describeModule.describeOptions, - rerunDescribeSchema, - rerunDescribeOptions, - selectTypeOf, - authorizeAction, -}; diff --git a/.agents/probes/untyped/interface-assertions-1.d.cts b/.agents/probes/untyped/interface-assertions-1.d.cts deleted file mode 100644 index e5e4aaaf5..000000000 --- a/.agents/probes/untyped/interface-assertions-1.d.cts +++ /dev/null @@ -1,59 +0,0 @@ -/** A zod schema, or a hand-made `_def` stand-in for one, which the battery feeds describeSchema on purpose. */ -export interface SchemaLike { - readonly _def: { - readonly typeName?: string; - readonly description?: string; - }; -} - -/** What a describe function answers, as far as the probe reads it: JSON-shaped data. */ -export type Observation = - | string - | number - | boolean - | null - | undefined - | readonly Observation[] - | { readonly [key: string]: Observation }; - -/** A set of options for SelectType, one of which carries a value outside the declared contract. */ -export interface ChoiceOptions { - readonly multiple?: boolean; - readonly items: readonly { - readonly value: string | number | boolean; - readonly label: string; - }[]; -} - -export type ChoiceInput = string | number | readonly string[] | undefined; - -export interface ChoiceType { - getValidation(): { parse(input: ChoiceInput): Observation }; -} - -export type Describer = (schema: SchemaLike) => Observation; - -export const describeSchema: Describer; -export const describeOptions: Describer; -export const rerunDescribeSchema: Describer; -export const rerunDescribeOptions: Describer; -export const selectTypeOf: (options: ChoiceOptions) => ChoiceType; -/** A controller receiver, or a value that is none, which the battery feeds authorizeAction on purpose. */ -export type ReceiverLike = - | { readonly constructor?: unknown } - | string - | number - | null - | undefined; - -/** The user authorizeAction checks, as far as the probe sets it. */ -export interface ProbeUser { - _id: string; -} - -export const authorizeAction: ( - receiver: ReceiverLike, - actionId: string, - user: ProbeUser, - tenantId: string, -) => Promise | undefined>; diff --git a/.agents/probes/untyped/interface-components-routes.cjs b/.agents/probes/untyped/interface-components-routes.cjs deleted file mode 100644 index 548ea4ecf..000000000 --- a/.agents/probes/untyped/interface-components-routes.cjs +++ /dev/null @@ -1,26 +0,0 @@ -const helpers = require("../../../packages/interface-dms/dist/base/table-view/internal/factory-helpers.js"); - -/** - * @param {{ func: (this: object, ...args: unknown[]) => unknown }} route - * @param {object} receiver - * @param {unknown[]} args - */ -function callRoute(route, receiver, args) { - return route.func.call(receiver, ...args); -} - -/** - * @param {unknown} input - */ -function serializeTableViewDisplays(input) { - return helpers.serializeTableViewDisplays(input); -} - -/** - * @param {unknown} input - */ -function serializeRowActions(input) { - return helpers.serializeRowActions(input); -} - -module.exports = { callRoute, serializeTableViewDisplays, serializeRowActions }; diff --git a/.agents/probes/untyped/interface-components-routes.d.cts b/.agents/probes/untyped/interface-components-routes.d.cts deleted file mode 100644 index 19ee0af03..000000000 --- a/.agents/probes/untyped/interface-components-routes.d.cts +++ /dev/null @@ -1,64 +0,0 @@ -import type { RequestContext } from "../../../packages/interface-dms/node_modules/@antelopejs/interface-api"; -import type { User } from "../../../packages/interface-dms/dist/auth/db/tables/users.table.js"; - -/** What a route or a serializer answers, as far as the probe reads it: JSON-shaped data. */ -export type Observation = - | string - | number - | boolean - | null - | undefined - | readonly Observation[] - | { readonly [key: string]: Observation }; - -export declare class Receiver {} - -export interface RouteParams { - readonly id: string; -} - -/** One argument of a base route call, where the probe puts a sentinel in a slot that declares a user. */ -export type RouteArgument = string | number | RequestContext | RouteParams | User; - -export interface Route { - readonly func: ( - this: Receiver, - ...args: never[] - ) => Observation | Promise; -} - -/** The display sources the probe hands serializeTableViewDisplays, with numeric card widths and an unknown kanban key. */ -export interface DisplayInput { - readonly displays: readonly { - readonly id: string; - readonly options?: { readonly marker: number }; - }[]; - readonly card: { - readonly fields: readonly string[]; - readonly minWidth: number; - readonly skeletonHeight: number; - }; - readonly kanban: { readonly groupBy: string }; -} - -/** The row actions the probe hands serializeRowActions, with a custom action whose target type is unknown. */ -export interface RowActionInput { - readonly add: { readonly confirm: { readonly title: string } }; - readonly edit: boolean; - readonly delete: { - readonly rule: { readonly field: string; readonly equals: string }; - }; - readonly custom: readonly { - readonly id: string; - readonly label: string; - readonly target: { readonly type: string; readonly url: string }; - }[]; -} - -export function callRoute( - route: Route, - receiver: Receiver, - args: readonly RouteArgument[], -): Observation | Promise; -export function serializeTableViewDisplays(input: DisplayInput): Observation; -export function serializeRowActions(input: RowActionInput): Observation; diff --git a/.agents/probes/untyped/tsconfig.json b/.agents/probes/untyped/tsconfig.json deleted file mode 100644 index 28ac5096d..000000000 --- a/.agents/probes/untyped/tsconfig.json +++ /dev/null @@ -1,15 +0,0 @@ -{ - "extends": "../../../tsconfig.json", - "compilerOptions": { - "allowJs": true, - "noEmit": true, - "incremental": false, - "types": ["node"] - }, - "files": [ - "./interface-assertions-1.cjs", - "./interface-assertions-1.d.cts", - "./interface-components-routes.cjs", - "./interface-components-routes.d.cts" - ] -} diff --git a/.agents/skills/verify-dms/SKILL.md b/.agents/skills/verify-dms/SKILL.md index 46d06c2bc..a737f80e2 100644 --- a/.agents/skills/verify-dms/SKILL.md +++ b/.agents/skills/verify-dms/SKILL.md @@ -184,18 +184,6 @@ The skill is not Trusted yet. It becomes Trusted when it passes a shadow run of - `up` and the frontend build install from the npm registry. A package published minutes earlier can answer 404 until the registry propagates it; `up` then fails on its `frontend-build` or `project-build` log. Run `up` again a few minutes later. - Writing a journey: a check (`tab.visible`, `tab.shows`, `tab.at`, `ctx.equal`…) only counts inside `ctx.verify`. Uppercase styling changes `innerText`, so match such text case-insensitively. `ctx.api` signs the account in again on a 401; pass `token` to observe a refused token. The raw Playwright page is `tab.page`, for popups, downloads and visually hidden inputs. `ctx.stopDatabase()` stops the run's mongod, for a database-down state, and kills the next database's standby so none that booted without a database is reused. `ctx.startDatabase()` starts it again at the same URL with the same data. It resolves once mongod listens, before the backend has reconnected, so wait for the recovered state with a check that polls. A journey that ends with mongod stopped has it started again. Both refuse a `--mongo-url` run. A journey that drives what only development serves sets `backend: "dev"`, one that drives a reload sets `backend: "watch"`, and one that asserts their absence sets `backend: "production"`, so neither depends on how `up` was run; a journey on a `shared` database cannot set it. A journey whose modules spawn a process that shares a file of the checkout with every backend, such as the dms-assistant sidecar and its `sidecar.lock`, sets `standby: false`: its fresh database gets a backend started for it instead of the standby booted before it, and no standby boots while it runs, while the other journeys keep theirs; a journey on a `shared` database cannot set it either. -## Component probes - -`antelopejs-api-diff --probe ` captures one file, so the `interface-dms` component coverage in `.agents/probes/` is five standalone scripts. Pass the ones whose keys your change reaches, one api-diff run each. - -| Probe | Keys it prints | -|---|---| -| `interface-components-builder.cjs` | `sync`, `serialized`, `filtered`, `shadowedCall`, `ownKeys`, `positional`, `permission`, `uploadConstraintReads`, `defaultTypesFresh`, `dataTypesAndHints` | -| `interface-components-routes.cjs` | `routes`, `factoryHelpers`, `metadata`, `lifecycle` | -| `interface-components-read-order.cjs` | `pageInfoPrecedence`, `extractedPageCalls`, `evaluationOrder` | -| `interface-components-type-boundaries.cjs` | `typeBoundaries` | -| `interface-components-owner-order.cjs` | `ownerAccessorOrder`, `publicWatchFilterOrder`, `publicPageScheduling` | - ## Feature map [features/README.md](features/README.md). diff --git a/package.json b/package.json index 50b2ddf8f..1eb58b6fe 100644 --- a/package.json +++ b/package.json @@ -23,8 +23,7 @@ "dev": "pnpm --dir packages/dms dev", "frontend:dev": "pnpm --dir packages/dms frontend:dev", "build": "pnpm --filter @antelopejs/dms build", - "typecheck": "pnpm --dir packages/interface-dms typecheck && pnpm --dir packages/interface-dms-builder typecheck && pnpm --dir packages/dms typecheck && pnpm --dir packages/dms/frontend-vue typecheck:tests && pnpm run typecheck:probes", - "typecheck:probes": "tsc -p .agents/probes/tsconfig.json", + "typecheck": "pnpm --dir packages/interface-dms typecheck && pnpm --dir packages/interface-dms-builder typecheck && pnpm --dir packages/dms typecheck && pnpm --dir packages/dms/frontend-vue typecheck:tests", "lint": "pnpm --dir packages/dms/playground run -s types:prepare && oxlint && node packages/dms/playground/checks/raw-sources.cjs && pnpm run deps && pnpm --dir packages/dms/frontend-vue lint", "lint:fix": "oxlint --fix && pnpm --dir packages/dms/frontend-vue lint:fix", "format": "oxfmt . && pnpm --dir packages/dms/frontend-vue format", diff --git a/packages/dms/frontend-vue/scripts/probes/capture-frontend-probe.cjs b/packages/dms/frontend-vue/scripts/probes/capture-frontend-probe.cjs deleted file mode 100644 index 682915114..000000000 --- a/packages/dms/frontend-vue/scripts/probes/capture-frontend-probe.cjs +++ /dev/null @@ -1,23 +0,0 @@ -"use strict"; - -const path = require("node:path"); -const { createRequire } = require("node:module"); -/** @type {(id: string) => typeof import("esbuild")} */ -const loadEsbuild = createRequire(require.resolve("vite/package.json")); -const { buildSync } = loadEsbuild("esbuild"); - -/** @param {string} outfile */ -function captureProbe(outfile) { - buildSync({ - entryPoints: [path.join(__dirname, "frontend-behavior.cjs")], - outfile, - bundle: true, - platform: "node", - format: "cjs", - logLevel: "silent", - }); -} - -module.exports = { captureProbe }; - -if (require.main === module) captureProbe(path.resolve(process.argv[2])); diff --git a/packages/dms/frontend-vue/scripts/probes/frontend-behavior.cjs b/packages/dms/frontend-vue/scripts/probes/frontend-behavior.cjs deleted file mode 100644 index fac4fe186..000000000 --- a/packages/dms/frontend-vue/scripts/probes/frontend-behavior.cjs +++ /dev/null @@ -1,606 +0,0 @@ -"use strict"; - -const fs = require("node:fs"); -const os = require("node:os"); -const path = require("node:path"); -const vm = require("node:vm"); -const { execFileSync } = require("node:child_process"); -const { createRequire } = require("node:module"); -const { pathToFileURL } = require("node:url"); - -const legacy = require("./frontend-legacy.cjs"); -const { frontend, layers, ts, evaluate, load, isObjectLike } = legacy; - -/** @typedef {{ dirs: string[]; presets: string[]; imports: Array<{ from: string; name: string }> }} UnimportInput */ - -/** - * @param {URL} url - * @returns {Promise<{ createUnimport: (options: UnimportInput) => import("unimport").Unimport }>} - */ -const importUnimport = (url) => import(url); - -/** @type {(filename: string) => { autoImportDirectories: (roots: Array<{ id: string; root: string }>, builds: Array<{ id: string; setup: unknown }>, fail: (message: string) => never) => string[] }} */ -const loadBuildLoader = load; - -/** @type {(source: string, filename: string, globals: legacy.ProbeBindings, imports: legacy.ProbeBindings) => { default: unknown }} */ -const evaluateBuildFile = evaluate; - -/** @type {{ runInNewContext: (code: string, context: object) => Array> }} */ -const defaultsVm = vm; - -/** - * @param {string} templates - * @returns {{ config: import("typescript").SourceFile; configuredImports: import("typescript").ArrayLiteralExpression }} - */ -function templateImports(templates) { - const config = ts.createSourceFile( - "vite.ts", - fs.readFileSync(path.join(templates, "vite.config.ts"), "utf8"), - ts.ScriptTarget.Latest, - true, - ); - /** @type {{ initializer?: import("typescript").Expression }} */ - const configured = {}; - /** @param {import("typescript").Node} node */ - const visit = (node) => { - if ( - ts.isPropertyAssignment(node) && - node.name.getText(config) === "imports" - ) - configured.initializer = node.initializer; - ts.forEachChild(node, visit); - }; - visit(config); - const configuredImports = configured.initializer; - if (!configuredImports || !ts.isArrayLiteralExpression(configuredImports)) - throw new TypeError("The template Vite config has no imports array"); - return { config, configuredImports }; -} - -async function autoImports() { - const buildReq = createRequire( - path.join(frontend, "node_modules/@antelopejs/dms-frontend/package.json"), - ); - const templates = path.join( - path.dirname(buildReq.resolve("@antelopejs/dms-frontend/package.json")), - "templates/vue", - ); - const { autoImportDirectories } = loadBuildLoader( - path.join(templates, "frontend-build-loader.ts"), - ); - const buildFile = path.join(frontend, "dms.frontend.build.ts"); - const { default: setup } = evaluateBuildFile( - fs.readFileSync(buildFile, "utf8"), - buildFile, - {}, - { - "#dms/frontend-build": { - /** @param {unknown} value */ - defineDmsFrontendBuild: (value) => value, - }, - }, - ); - const dirs = autoImportDirectories( - [{ id: "dms", root: frontend }], - [{ id: "dms", setup }], - (message) => { - throw new Error(message); - }, - ); - const { config, configuredImports } = templateImports(templates); - const defaults = defaultsVm.runInNewContext( - `[${configuredImports.elements - .filter(ts.isObjectLiteralExpression) - .map((node) => node.getText(config)) - .join(",")} ]`.replaceAll("import.meta.dirname", "templateRoot"), - { - templateRoot: templates, - /** @param {unknown} value */ - normalizePath: (value) => value, - resolve: path.resolve, - }, - ); - const { createUnimport } = await importUnimport( - pathToFileURL(buildReq.resolve("unimport")), - ); - const importer = createUnimport({ - dirs, - presets: configuredImports.elements - .filter(ts.isStringLiteral) - .map((node) => node.text), - imports: defaults.flatMap((value) => - Object.entries(value).flatMap(([from, names]) => - names.map((name) => ({ from, name })), - ), - ), - }); - await importer.init(); - const entries = await importer.getImports(); - const injection = await importer.injectImports( - `void [${entries - .filter((entry) => !entry.type) - .map((entry) => entry.as ?? entry.name) - .join(",")}];`, - ); - const injected = new Set( - injection.imports.map((entry) => entry.as ?? entry.name), - ); - const temporary = fs.mkdtempSync( - path.join(os.tmpdir(), "frontend-signatures-"), - ); - try { - fs.symlinkSync( - path.join(frontend, "node_modules"), - path.join(temporary, "node_modules"), - ); - const globals = path.join(temporary, "globals.d.ts"); - fs.writeFileSync(globals, await importer.generateTypeDeclarations()); - const options = { - target: ts.ScriptTarget.ES2022, - module: ts.ModuleKind.ESNext, - moduleResolution: ts.ModuleResolutionKind.Bundler, - strict: true, - skipLibCheck: true, - allowImportingTsExtensions: true, - resolveJsonModule: true, - types: [], - noEmit: true, - baseUrl: frontend, - paths: { - "#dms/frontend-module": [path.join(templates, "frontend-module.ts")], - ...Object.fromEntries( - ["auth", "core", "layout", "onboarding", "ui"].map((name) => [ - `#dms-${name}/*`, - [path.join(frontend, `layers/dms-${name}/*`)], - ]), - ), - }, - }; - const files = entries - .filter((entry) => path.isAbsolute(entry.from)) - .map((entry) => entry.from); - const program = ts.createProgram([...files, globals], options); - return signatures(program, { entries, globals, options, injected }); - } finally { - fs.rmSync(temporary, { recursive: true, force: true }); - } -} - -/** - * @param {import("typescript").SourceFile} source - * @returns {source is import("typescript").SourceFile & { parseDiagnostics: readonly unknown[] }} - */ -function hasParseDiagnostics(source) { - return ( - Object.hasOwn(source, "parseDiagnostics") && - "parseDiagnostics" in source && - Array.isArray(source.parseDiagnostics) - ); -} - -/** @param {import("typescript").Program} program */ -function describeTypes(program) { - const checker = program.getTypeChecker(); - const flags = - ts.TypeFormatFlags.NoTruncation | - ts.TypeFormatFlags.InTypeAlias | - ts.TypeFormatFlags.UseStructuralFallback; - /** @type {Map} */ - const dictionaries = new Map(); - for (const source of program.getSourceFiles()) { - for (const node of source.statements.filter(ts.isTypeAliasDeclaration)) { - if ( - !source.fileName.startsWith(frontend) && - !( - ts.isTypeReferenceNode(node.type) && - node.type.typeName.getText(source) === "Record" - ) - ) - continue; - const type = checker.getTypeAtLocation(node); - const index = type.getStringIndexType(); - if ( - index && - !type.getProperties().length && - !type.getCallSignatures().length - ) - dictionaries.set( - node.name.text, - `Record`, - ); - } - } - /** - * @param {string} text - * @returns {string} - */ - const normalize = (text) => { - const normalized = text - .replaceAll(frontend.replaceAll("\\", "/"), "") - .replace(/import\("[^"]*\/layers\/[^"]*"\)\./g, "") - .replace(/import\("[^"]*\/node_modules\/([^"]*)"\)\./g, "$1.") - .replace( - /\b[A-Za-z_$][\w$]*\b/g, - (name) => dictionaries.get(name) ?? name, - ) - .replace(/\{ \[x: string\]: ([^;{}]+); \}/g, "Record") - .replace(/(__@\w+)@\d+/g, "$1") - .replace(/"[^"]+"(?: \| "[^"]+")+/g, (union) => - union.split(" | ").sort().join(" | "), - ); - const source = ts.createSourceFile( - "signature.ts", - `type Signature = ${normalized};`, - ts.ScriptTarget.Latest, - true, - ); - const declaration = source.statements[0]; - if (!hasParseDiagnostics(source)) - throw new TypeError("The TypeScript parser reports no parse diagnostics"); - if ( - source.parseDiagnostics.length || - !ts.isTypeAliasDeclaration(declaration) || - !ts.isTypeLiteralNode(declaration.type) || - !declaration.type.members.every(ts.isPropertySignature) - ) - return normalized; - const members = [...declaration.type.members].sort((left, right) => - left.name.getText(source).localeCompare(right.name.getText(source)), - ); - return `{ ${members.map((member) => member.getText(source)).join(" ")} }`; - }; - /** - * @param {import("typescript").Type} type - * @param {Set} [seen] - * @returns {string} - */ - const describe = (type, seen = new Set()) => { - if (seen.has(type)) - return normalize(checker.typeToString(type, undefined, flags)); - const visited = new Set([...seen, type]); - if (type.isUnion()) - return type.types - .map((member) => `(${describe(member, visited)})`) - .sort() - .join(" | "); - const index = type.getStringIndexType(); - if ( - index && - !type.getProperties().length && - !type.getCallSignatures().length - ) - return `Record`; - return normalize(checker.typeToString(type, undefined, flags)); - }; - return { describe, normalize }; -} - -/** - * @param {import("typescript").Program} program - * @param {{ entries: import("unimport").Import[]; globals: string; options: import("typescript").CompilerOptions; injected: Set }} context - */ -function signatures(program, { entries, globals, options, injected }) { - const checker = program.getTypeChecker(); - const { describe, normalize } = describeTypes(program); - const inventory = entries.map((entry) => { - const from = path.isAbsolute(entry.from) - ? entry.from - : ts.resolveModuleName(entry.from, globals, options, ts.sys) - .resolvedModule?.resolvedFileName; - const source = program.getSourceFile(from); - if (!source) throw new Error(`Missing auto-import source ${entry.from}`); - const owner = checker.getSymbolAtLocation(source); - const exported = (owner ? checker.getExportsOfModule(owner) : []).find( - (symbol) => symbol.name === entry.name, - ); - const identity = { - name: entry.as ?? entry.name, - typeOnly: !!entry.type, - injected: !entry.type && injected.has(entry.as ?? entry.name), - }; - if (!exported) - return { - ...identity, - signature: "", - declaration: normalize(source.getText()), - }; - const symbol = - exported.flags & ts.SymbolFlags.Alias - ? checker.getAliasedSymbol(exported) - : exported; - const declaration = - symbol.valueDeclaration ?? - symbol.declarations?.[0] ?? - exported.declarations[0]; - const type = - symbol.flags & ts.SymbolFlags.Value - ? checker.getTypeOfSymbolAtLocation(symbol, declaration) - : checker.getDeclaredTypeOfSymbol(symbol); - return { - ...identity, - signature: describe(type), - members: - entry.type || - type.getConstructSignatures().length || - symbol.flags & ts.SymbolFlags.Enum - ? type - .getProperties() - .map((member) => ({ - name: member.name.replace(/(__@\w+)@\d+/g, "$1"), - optional: !!(member.flags & ts.SymbolFlags.Optional), - signature: describe( - checker.getTypeOfSymbolAtLocation(member, declaration), - ), - })) - .sort((left, right) => left.name.localeCompare(right.name)) - : [], - }; - }); - return [ - ...new Map( - inventory.map((entry) => [JSON.stringify(entry), entry]), - ).values(), - ].sort((left, right) => - JSON.stringify(left).localeCompare(JSON.stringify(right)), - ); -} - -/** - * @typedef {{ default: { setup: (sdk: object) => void } }} FrontendEntry - */ - -/** @type {(source: string, filename: string, globals: legacy.ProbeBindings, imports: legacy.ProbeBindings, transformers: import("typescript").CustomTransformers) => FrontendEntry} */ -const evaluateFrontendEntry = evaluate; - -/** @type {(source: string, filename: string, globals: legacy.ProbeBindings, imports: legacy.ProbeBindings) => { default: () => void }} */ -const evaluateRegister = evaluate; - -/** - * @param {unknown} value - * @returns {value is { loader: unknown }} - */ -function hasLoader(value) { - const holder = isObjectLike(value) || typeof value === "function"; - return holder && "loader" in value && Boolean(value.loader); -} - -/** - * @param {import("typescript").Expression} expression - * @returns {string} - */ -function patternText(expression) { - if (!ts.isStringLiteralLike(expression)) - throw new TypeError("import.meta.glob takes string literal patterns"); - return expression.text; -} - -function registrations() { - /** @type {unknown[][]} */ - const events = []; - /** - * @param {string} file - * @returns {FrontendEntry} - */ - const evaluateEntry = (file) => { - /** @type {import("typescript").TransformerFactory} */ - const transform = (context) => (node) => { - /** @type {import("typescript").Visitor} */ - const visit = (node) => { - if ( - ts.isPropertyAccessExpression(node) && - node.getText().startsWith("import.meta.env.") - ) - return ts.factory.createFalse(); - if ( - ts.isCallExpression(node) && - node.expression.getText() === "import.meta.glob" - ) { - const arg = node.arguments[0]; - const patterns = ts.isArrayLiteralExpression(arg) - ? arg.elements.map(patternText) - : [patternText(arg)]; - const files = fs.globSync( - patterns.filter((pattern) => !pattern.startsWith("!")), - { - cwd: path.dirname(file), - exclude: patterns - .filter((pattern) => pattern.startsWith("!")) - .map((pattern) => pattern.slice(1)), - }, - ); - return ts.factory.createObjectLiteralExpression( - files.map((file) => - ts.factory.createPropertyAssignment( - ts.factory.createStringLiteral( - file.startsWith(".") ? file : `./${file}`, - ), - ts.factory.createIdentifier("probeLoader"), - ), - ), - ); - } - return ts.visitEachChild(node, visit, context); - }; - return ts.visitNode(node, visit); - }; - const imports = new Proxy( - {}, - { - has: () => true, - get: (_, name) => { - if (name === "vue") - return { defineAsyncComponent: () => ({ loader: true }) }; - if (name === "#dms/frontend-module") - return { useDmsRuntimeConfig: () => ({ public: {} }) }; - const specifier = String(name); - if (specifier.startsWith(".")) { - const target = path.resolve(path.dirname(file), specifier); - if (target.includes("/app/entries")) - return evaluateEntry( - target.endsWith(".ts") ? target : `${target}.ts`, - ); - } - return { default: name, __esModule: true }; - }, - }, - ); - return evaluateFrontendEntry( - fs.readFileSync(file, "utf8"), - file, - { probeLoader: () => {} }, - imports, - { before: [transform] }, - ); - }; - const module = evaluateEntry(path.join(frontend, "dms.frontend.ts")).default; - /** @type {Record} */ - const sdkTarget = { options: { public: {} } }; - const sdk = new Proxy(sdkTarget, { - get(target, key) { - if (key in target) return target[key]; - return /** @param {...unknown} args */ (...args) => - events.push([ - key, - ...args.map((value) => (hasLoader(value) ? "" : value)), - ]); - }, - }); - module.setup(sdk); - const registerFile = path.join(layers, "dms-ui/app/plugins/register.ts"); - const registrations = new Proxy( - {}, - { - has: () => true, - get: () => - new Proxy( - {}, - { get: (_, name) => () => events.push(["register", String(name)]) }, - ), - }, - ); - evaluateRegister( - fs.readFileSync(registerFile, "utf8"), - registerFile, - { - /** @param {unknown} setup */ - defineDmsPlugin: (setup) => setup, - }, - registrations, - ).default(); - return events; -} - -/** - * @typedef {{ - * success: boolean; - * testResults: Array<{ assertionResults: Array<{ fullName: string; status: string }> }>; - * }} VitestReport - */ - -/** - * @param {unknown} value - * @returns {value is { fullName: string; status: string }} - */ -function isAssertionResult(value) { - return ( - isObjectLike(value) && - Object.hasOwn(value, "fullName") && - typeof value.fullName === "string" && - Object.hasOwn(value, "status") && - typeof value.status === "string" - ); -} - -/** - * @param {unknown} value - * @returns {value is { assertionResults: Array<{ fullName: string; status: string }> }} - */ -function isFileResult(value) { - return ( - isObjectLike(value) && - Object.hasOwn(value, "assertionResults") && - Array.isArray(value.assertionResults) && - value.assertionResults.every(isAssertionResult) - ); -} - -/** - * @param {unknown} value - * @returns {value is VitestReport} - */ -function isVitestReport(value) { - return ( - isObjectLike(value) && - Object.hasOwn(value, "success") && - typeof value.success === "boolean" && - Object.hasOwn(value, "testResults") && - Array.isArray(value.testResults) && - value.testResults.every(isFileResult) - ); -} - -/** - * @param {unknown} error - * @returns {error is { stack: string }} - */ -function hasStack(error) { - return ( - isObjectLike(error) && "stack" in error && typeof error.stack === "string" - ); -} - -function behavioralTests() { - const temporary = fs.mkdtempSync(path.join(os.tmpdir(), "frontend-tests-")); - const report = path.join(temporary, "report.json"); - try { - const tests = - `download-action-target table-row-action-errors table-metric-cells table-pills-cell table-view-sources table-view-reorder table-view-expanded-row-details repeater-table form-create-wording form-submit-clear form-server-field-errors instant-save site-layout-resync site-layout-refresh realtime-shared-connection permission-preview` - .split(" ") - .map((name) => `tests/${name}.test.ts`); - execFileSync( - "pnpm", - [ - "exec", - "vitest", - "run", - ...tests, - "--maxWorkers=2", - "--reporter=json", - `--outputFile=${report}`, - ], - { cwd: frontend, stdio: "pipe", maxBuffer: 16 * 1024 * 1024 }, - ); - /** @type {unknown} */ - const result = JSON.parse(fs.readFileSync(report, "utf8")); - if (!isVitestReport(result) || !result.success) - throw new Error("Frontend behavioral tests failed"); - return result.testResults - .flatMap((file) => - file.assertionResults.map((test) => ({ - name: test.fullName, - status: test.status, - })), - ) - .sort((left, right) => left.name.localeCompare(right.name)); - } finally { - fs.rmSync(temporary, { recursive: true, force: true }); - } -} - -(async () => { - /** @type {Awaited> & { autoImports: Awaited>; registrations: ReturnType; behavioralTests?: ReturnType }} */ - const result = { - ...(await legacy.observe()), - autoImports: await autoImports(), - registrations: registrations(), - }; - if (process.argv.includes("--tests")) - result.behavioralTests = behavioralTests(); - process.stdout.write(`${JSON.stringify(result)}\n`); -})().catch( - /** @param {unknown} error */ - (error) => { - process.stderr.write(`${hasStack(error) ? error.stack : undefined}\n`); - process.exitCode = 1; - }, -); diff --git a/packages/dms/frontend-vue/scripts/probes/frontend-legacy.cjs b/packages/dms/frontend-vue/scripts/probes/frontend-legacy.cjs deleted file mode 100644 index c2ca67229..000000000 --- a/packages/dms/frontend-vue/scripts/probes/frontend-legacy.cjs +++ /dev/null @@ -1,620 +0,0 @@ -"use strict"; - -const fs = require("node:fs"); -const path = require("node:path"); -const vm = require("node:vm"); -const { createRequire } = require("node:module"); - -const root = path.resolve(process.argv[2]); -const frontend = path.join(root, "packages/dms/frontend-vue"); - -/** - * @typedef {Omit & { - * registerTS: (load: () => typeof import("typescript")) => void; - * }} CompilerSfc - */ - -/** - * @typedef {{ - * (id: "typescript"): typeof import("typescript"); - * (id: "@vue/compiler-sfc"): CompilerSfc; - * (id: "vue"): { - * mergeDefaults: (raw: unknown, defaults: unknown) => unknown; - * mergeModels: (first: unknown, second: unknown) => unknown; - * }; - * (id: "vite"): typeof import("vite"); - * (id: "node:path"): typeof import("node:path"); - * }} FrontendRequire - */ - -/** @type {FrontendRequire} */ -const req = createRequire(path.join(frontend, "package.json")); -const ts = { - ...req("typescript"), - findConfigFile: () => path.join(root, ".dependency-cruiser.tsconfig.json"), -}; -const sfc = req("@vue/compiler-sfc"); -sfc.registerTS(() => ts); -const layers = path.join(frontend, "layers"); -const componentManifest = { - "dms-auth/app/components/": ["OAuthButtons.vue"], - "dms-auth/app/custom-pages/auth/": [ - "2fa.vue", - "backup.vue", - "forgot-validation.vue", - "login.vue", - "oauth-complete.vue", - "signup.vue", - "validate.vue", - ], - "dms-layout/app/build/components/": [ - "layout/DashboardHeader.vue", - "layout/DashboardSidePanel.vue", - "layout/DashboardSidebar.vue", - "layout/LayoutFooter.vue", - "layout/PageHeaderActionBar.vue", - "notification/NotificationPopover.vue", - "pages/modules/ModuleStoreDependencyPicker.vue", - "pages/modules/ModuleStoreOperation.vue", - "pages/modules/ModuleTile.vue", - "pages/settings/members/MemberRolePicker.vue", - "pages/settings/notification/NotificationInboxDisplay.vue", - "pages/settings/profile/ProfilePersonalInfo.vue", - "pages/settings/profile/ProfileSummaryRow.vue", - "pages/settings/roles/RolePanelHead.vue", - "pages/settings/security/SecurityEditPanel.vue", - "pages/settings/security/SecurityEmail.vue", - "pages/settings/security/SecurityPanelField.vue", - "pages/settings/security/SecuritySessions.vue", - "pages/settings/security/SecurityStatus.vue", - "pages/settings/security/SecurityTwoFactor.vue", - "pages/settings/theme/CssVariablesList.vue", - "pages/settings/theme/ThemePreview.vue", - ], - "dms-layout/app/components/": [ - "navigation/AccordionItemSync.vue", - "navigation/NavigationMenu.vue", - ], - "dms-layout/app/custom-pages/settings/": ["roles.vue", "shortcuts.vue"], - "dms-layout/app/emails/components/": ["EmailLayout.vue"], - "dms-layout/app/": ["error.vue"], - "dms-layout/app/pages/": ["[...slug].vue"], - "dms-onboarding/app/build/components/": [ - "onboarding/StepIndicator.vue", - "onboarding/steps/StepReady.vue", - ], - "dms-ui/app/build/components/": [ - "blocks/BlockActions.vue", - "containers/drawer/DynamicDrawer.vue", - "containers/modal/DynamicModal.vue", - "containers/row/RowHeader.vue", - "form/FormFieldControl.vue", - "form/FormFieldNotes.vue", - "form/PasswordInput.vue", - "period/PeriodRangeCalendar.vue", - "sortable/SortableList.vue", - "stat/StatCell.vue", - "table-view/CardAvatar.vue", - "table-view/CardsDisplay.vue", - "table-view/ExpandedRowDetail.vue", - "table-view/IdentityCell.vue", - "table-view/LiveHoldBanner.vue", - "table/TableActions.vue", - "table/FiltersRow.vue", - "table/TableMenu.vue", - "table/MenuColumns.vue", - "table/MenuDensity.vue", - "table/MenuFilter.vue", - "table/MenuImport.vue", - "table/MenuPage.vue", - "table/MenuRoot.vue", - "table/MenuSort.vue", - "table/MenuViewMode.vue", - "table/TablePagination.vue", - "table/RowSelection.vue", - "table/TableRoot.vue", - "table/TableTabs.vue", - "table/TableViews.vue", - ], - "dms-ui/app/components/": ` - PasswordStrength.vue RecursiveComponent.vue activity/ActivityItem.vue - banner/NoticeBanner.vue block-preview/BlockPreview.vue blocks/BannerBlock.vue - blocks/CardBlock.vue blocks/KeyValueListBlock.vue blocks/LiveToggleBlock.vue - card/CardPanel.vue card/NavCard.vue chart/ApexChartHost.vue chart/ChartBlock.vue - chart/internal/SparklineChart.vue check-list/CheckList.vue check-list/PasswordRules.vue - code-snippet/CodeSnippet.vue confirm/ConfirmModal.vue empty-state/EmptyState.vue - field-row/FieldRow.vue flow-canvas/FlowCanvas.vue form/FormBlock.vue form/LocalizedField.vue - icon-well/IconWell.vue key-value-list/KeyValueList.vue link/AutoLink.vue - list-row/ListRow.vue master-detail/MasterDetail.vue meter/MeterBar.vue meter/MeterBlock.vue - period/PeriodSelector.vue record-card/RecordCard.vue section-header/EyebrowLabel.vue - section-header/SectionHeader.vue section/SectionPanel.vue segmented/SegmentedControl.vue - stat-group/StatGroup.vue status-pill/StatusPill.vue tab/TabBlock.vue - table-view/CascaderPath.vue table-view/KanbanBoard.vue table-view/KanbanDisplay.vue - table-view/TableView.vue top-list/TopListCard.vue - ` - .trim() - .split(/\s+/), - "dms-ui/app/components/form/components/": [ - "InputCalendar.vue", - "InputCascader.vue", - "ChoiceCards.vue", - "DatePicker.vue", - "DatePickerRange.vue", - "DisplayValue.vue", - "InputFile.vue", - "InputImage.vue", - "InputAddress.vue", - "InputColor.vue", - "InputNumber.vue", - "InputPercentage.vue", - "InputTags.vue", - "KeyValue.vue", - "InputRepeater.vue", - "RepeaterTable.vue", - "RichText.vue", - ], -}; -const compilerOptions = { - module: ts.ModuleKind.CommonJS, - target: ts.ScriptTarget.ES2022, -}; - -/** @type {Record unknown>} */ -const dependencyLoaders = { - "node:path": () => req("node:path"), - vite: () => req("vite"), -}; - -/** @typedef {Record} ProbeBindings */ - -/** - * Runs one transpiled source in a fresh context and returns its exports. The - * exports follow the file, so each caller binds the type of the file it loads. - * @param {string} source - * @param {string} filename - * @param {ProbeBindings} [globals] - * @param {ProbeBindings} [imports] - * @param {import("typescript").CustomTransformers} [transformers] - * @returns {any} - */ -function evaluate(source, filename, globals = {}, imports = {}, transformers) { - const module = { exports: {} }; - /** - * @param {string} specifier - * @returns {unknown} - */ - const localRequire = (specifier) => { - if (specifier in imports) return imports[specifier]; - if (specifier.startsWith(".")) { - const target = path.resolve(path.dirname(filename), specifier); - return load(target.endsWith(".ts") ? target : `${target}.ts`, globals); - } - const loader = dependencyLoaders[specifier]; - if (!loader) throw new Error(`Unexpected probe dependency ${specifier}`); - return loader(); - }; - const code = ts.transpileModule(source, { - compilerOptions, - fileName: filename, - transformers, - }).outputText; - vm.runInNewContext( - code, - { - module, - exports: module.exports, - require: localRequire, - Headers, - TextEncoder, - TextDecoder, - btoa, - atob, - ...globals, - }, - { filename }, - ); - return module.exports; -} - -/** - * @param {string} filename - * @param {ProbeBindings} [globals] - * @returns {any} - */ -function load(filename, globals) { - return evaluate(fs.readFileSync(filename, "utf8"), filename, globals); -} - -/** - * @typedef {{ - * (url: string, options: object): Promise; - * raw: (url: string, options: object) => Promise; - * }} AuthFetch - */ - -/** @type {(filename: string, globals: ProbeBindings) => { useAuthFetch: () => { $authFetch: AuthFetch } }} */ -const loadAuthFetch = load; - -/** @param {boolean} raw */ -async function authRetry(raw) { - /** @type {number[]} */ - const reads = []; - /** @type {Array<{ unauthorized: true } | { token: string | null; retry: string | null }>} */ - const calls = []; - let refreshes = 0; - /** - * @param {unknown} _request - * @param {{ headers: Headers }} options - */ - const baseFetch = async (_request, options) => { - calls.push( - calls.length - ? { - token: options.headers.get("authorization"), - retry: options.headers.get("x-auth-retry"), - } - : { unauthorized: true }, - ); - if (calls.length === 1) throw { response: { status: 401 } }; - return calls.at(-1); - }; - baseFetch.raw = baseFetch; - const filename = path.join( - layers, - "dms-core/app/composables/auth/useAuthFetch.ts", - ); - const { useAuthFetch } = loadAuthFetch(filename, { - HTTP_UNAUTHORIZED: 401, - useDmsUserSession: () => ({ loggedIn: { value: true } }), - useSessionRecovery: () => ({ - refreshSession: async () => { - refreshes++; - return true; - }, - reconcileSession: async () => {}, - redirectToAuth: async () => {}, - }), - useDmsRuntimeConfig: () => ({ public: { dms: { baseURL: "/api" } } }), - useDmsApp: () => ({ $i18n: { locale: { value: "en" } } }), - /** - * @param {unknown} _key - * @param {() => unknown} initial - */ - useDmsState: (_key, initial) => ({ value: initial() }), - $fetch: { create: () => baseFetch }, - }); - const options = Object.defineProperty({}, "headers", { - enumerable: true, - get() { - reads.push(reads.length + 1); - return { authorization: `token${reads.length}` }; - }, - }); - const { $authFetch } = useAuthFetch(); - const result = await (raw ? $authFetch.raw : $authFetch)( - "/resource", - options, - ); - return { result, reads, refreshes, calls }; -} - -/** @type {(filename: string) => { writeAt: (tree: object, path: string[], value: unknown) => void; readAt: (tree: object, path: string[]) => unknown }} */ -const loadPreferences = load; - -function preferences() { - const filename = path.join( - layers, - "dms-core/app/build/composables/user/preferencesCookie.ts", - ); - const { writeAt, readAt } = loadPreferences(filename); - /** @type {string[]} */ - const events = []; - /** @type {{ leaf?: string }} */ - const first = {}; - /** @type {{ leaf?: string }} */ - const second = {}; - let reads = 0; - const tree = Object.defineProperty({}, "branch", { - enumerable: true, - get() { - events.push(`get:${++reads}`); - return reads === 1 ? first : second; - }, - }); - writeAt(tree, ["branch", "leaf"], "written"); - const plain = {}; - writeAt(plain, ["a", "b"], 7); - /** @type {string[]} */ - const creationEvents = []; - /** @type {{ leaf?: string }} */ - const afterAssignment = {}; - /** @type {{ leaf?: string }} */ - let assigned; - let creationReads = 0; - const creatingTree = Object.defineProperty({}, "branch", { - get() { - creationEvents.push(`get:${++creationReads}`); - return creationReads === 1 ? undefined : afterAssignment; - }, - /** @param {{ leaf?: string }} value */ - set(value) { - creationEvents.push("set"); - assigned = value; - }, - }); - writeAt(creatingTree, ["branch", "leaf"], "created"); - return { - events, - first: first.leaf ?? null, - second: second.leaf ?? null, - plain: readAt(plain, ["a", "b"]), - creation: { - events: creationEvents, - assigned: assigned.leaf ?? null, - reread: afterAssignment.leaf ?? null, - }, - }; -} - -/** - * @typedef {{ - * setup: ( - * props: object, - * context: { expose: () => void }, - * ) => { - * HANDOFF_ACTIONS: object; - * runHandoffAction: (handoff: object) => unknown; - * }; - * }} OauthComponent - */ - -/** @type {(source: string, filename: string, globals: ProbeBindings, imports: ProbeBindings) => { default: OauthComponent }} */ -const evaluateOauth = evaluate; - -function oauth() { - const filename = path.join( - layers, - "dms-auth/app/custom-pages/auth/oauth-complete.vue", - ); - const { descriptor } = sfc.parse(fs.readFileSync(filename, "utf8"), { - filename, - }); - const compiled = sfc.compileScript(descriptor, { id: "oauth-probe" }); - const { default: component } = evaluateOauth( - compiled.content, - filename, - { - useDmsRoute: () => ({ query: {} }), - useHomepage: () => "/home", - useDmsApp: () => ({}), - /** @param {() => unknown} get */ - computed: (get) => ({ - get value() { - return get(); - }, - }), - onMounted: () => {}, - }, - { - vue: { - /** @param {object} options */ - defineComponent: (options) => options, - }, - }, - ); - const exposed = component.setup({}, { expose() {} }); - /** @type {string[]} */ - const events = []; - const actions = exposed.HANDOFF_ACTIONS; - Object.defineProperty(actions, "none", { - get() { - events.push("action:get"); - /** - * @this {unknown} - * @param {{ token: string }} handoff - */ - return function (handoff) { - events.push( - this === actions ? "receiver:actions" : "receiver:undefined", - ); - return handoff.token; - }; - }, - }); - const handoff = { - get kind() { - events.push("kind:get"); - return "none"; - }, - token: "handoff-token", - }; - const result = exposed.runHandoffAction(handoff); - return { result, events }; -} - -/** - * @param {unknown} value - * @returns {value is (...args: never[]) => unknown} - */ -function isCallable(value) { - return typeof value === "function"; -} - -/** - * @param {unknown} value - * @returns {value is Record} - */ -function isObjectLike(value) { - return typeof value === "object" && value !== null; -} - -/** - * @param {unknown} value - * @returns {value is unknown[]} - */ -function isList(value) { - return Array.isArray(value); -} - -/** - * @param {unknown} error - * @returns {error is { message: string }} - */ -function hasMessage(error) { - return ( - isObjectLike(error) && - "message" in error && - typeof error.message === "string" - ); -} - -/** @param {string} expression */ -function runtimeDeclaration(expression) { - try { - const javascript = ts.transpileModule(`module.exports = (${expression});`, { - compilerOptions, - }).outputText; - const module = { exports: {} }; - const vue = req("vue"); - vm.runInNewContext(javascript, { - module, - _mergeDefaults: vue.mergeDefaults, - _mergeModels: vue.mergeModels, - }); - /** - * @param {unknown} value - * @returns {unknown} - */ - const serialize = (value) => { - if (value === undefined) return { undefined: true }; - if (isCallable(value)) - return { - function: value.name || null, - code: value.toString().replace(/\s+/g, " "), - }; - if (isList(value)) return Array.from(value, serialize); - if (isObjectLike(value)) - return Object.fromEntries( - Object.entries(value).map(([key, entry]) => [key, serialize(entry)]), - ); - return value; - }; - return { status: "evaluated", value: serialize(module.exports) }; - } catch (error) { - const reason = hasMessage(error) ? error.message : undefined; - return { status: "runtime-unresolved", reason }; - } -} - -/** @typedef {{ compiled: string; runtime: ReturnType }} DeclarationContract */ - -function contracts() { - const manifest = Object.entries(componentManifest).flatMap( - ([prefix, files]) => files.map((file) => `${prefix}${file}`), - ); - const printer = ts.createPrinter({ removeComments: true }); - /** @param {string} source */ - const normalize = (source) => - printer - .printFile( - ts.createSourceFile( - "contract.ts", - source, - ts.ScriptTarget.Latest, - true, - ), - ) - .trim(); - return manifest.map((relative) => { - const filename = path.join(layers, relative); - const { descriptor } = sfc.parse(fs.readFileSync(filename, "utf8"), { - filename, - }); - try { - const compiled = sfc.compileScript(descriptor, { - id: "contract", - fs: { - fileExists: (file) => - fs.existsSync(file) && fs.statSync(file).isFile(), - readFile: (file) => fs.readFileSync(file, "utf8"), - }, - }); - const ast = ts.createSourceFile( - "compiled.ts", - compiled.content, - ts.ScriptTarget.Latest, - true, - ); - /** @type {{ path: string; status: string; props: DeclarationContract | null; emits: DeclarationContract | null; macros: string[]; [key: string]: unknown }} */ - const result = { - path: relative, - status: "compiled", - props: null, - emits: null, - macros: [], - }; - /** @param {import("typescript").Node} node */ - const inspect = (node) => { - if ( - ts.isPropertyAssignment(node) && - ["props", "emits"].includes(node.name.getText(ast)) - ) { - const expression = node.initializer.getText(ast); - result[node.name.getText(ast)] = { - compiled: normalize(expression), - runtime: runtimeDeclaration(expression), - }; - } - ts.forEachChild(node, inspect); - }; - inspect(ast); - const raw = ts.createSourceFile( - "source.ts", - descriptor.scriptSetup?.content ?? "", - ts.ScriptTarget.Latest, - true, - ); - /** @param {import("typescript").Node} node */ - const macro = (node) => { - if ( - ts.isCallExpression(node) && - ["defineExpose", "defineSlots", "defineOptions"].includes( - node.expression.getText(raw), - ) - ) { - result.macros.push(normalize(node.getText(raw))); - } - ts.forEachChild(node, macro); - }; - macro(raw); - return result; - } catch (error) { - if (!hasMessage(error)) throw error; - return { - path: relative, - status: "compiler-unresolved", - reason: error.message - .split("\n") - .find((line) => line.trim()) - .replaceAll(root, ""), - }; - } - }); -} - -module.exports = { - frontend, - layers, - req, - ts, - evaluate, - load, - isObjectLike, - observe: async () => ({ - authRetry: await authRetry(false), - authRawRetry: await authRetry(true), - preferences: preferences(), - oauth: oauth(), - componentContracts: contracts(), - }), -}; diff --git a/packages/dms/frontend-vue/scripts/probes/frontend-probe-controls.cjs b/packages/dms/frontend-vue/scripts/probes/frontend-probe-controls.cjs deleted file mode 100644 index 5e283923c..000000000 --- a/packages/dms/frontend-vue/scripts/probes/frontend-probe-controls.cjs +++ /dev/null @@ -1,322 +0,0 @@ -"use strict"; - -const assert = require("node:assert/strict"); -const fs = require("node:fs"); -const os = require("node:os"); -const path = require("node:path"); -const { execFileSync } = require("node:child_process"); -const { createRequire } = require("node:module"); -const { captureProbe } = require("./capture-frontend-probe.cjs"); - -/** @typedef {{ name: string; signature: string; injected: boolean }} AutoImport */ - -/** @typedef {{ autoImports: AutoImport[]; registrations: unknown[] }} ProbeResult */ - -/** - * @param {unknown} value - * @returns {value is Record} - */ -function isObjectLike(value) { - return typeof value === "object" && value !== null; -} - -/** - * @param {unknown} value - * @returns {value is AutoImport} - */ -function isAutoImport(value) { - return ( - isObjectLike(value) && - Object.hasOwn(value, "name") && - typeof value.name === "string" && - Object.hasOwn(value, "signature") && - typeof value.signature === "string" && - Object.hasOwn(value, "injected") && - typeof value.injected === "boolean" - ); -} - -/** - * @param {unknown} value - * @returns {value is ProbeResult} - */ -function isProbeResult(value) { - return ( - isObjectLike(value) && - Object.hasOwn(value, "autoImports") && - Array.isArray(value.autoImports) && - value.autoImports.every(isAutoImport) && - Object.hasOwn(value, "registrations") && - Array.isArray(value.registrations) - ); -} - -/** - * @param {unknown} value - * @returns {value is import("typescript").Node & { name: { text: string } }} - */ -function hasTextName(value) { - return ( - isObjectLike(value) && - Object.hasOwn(value, "name") && - isObjectLike(value.name) && - typeof value.name.text === "string" - ); -} - -/** - * @type {(from: string) => (id: string) => typeof import("typescript")} - */ -const createTypeScriptRequire = createRequire; - -const root = path.resolve(process.argv[2]); -const frontend = path.join(root, "packages/dms/frontend-vue"); -const temporary = fs.mkdtempSync(path.join(os.tmpdir(), "frontend-controls-")); -const copy = path.join(temporary, "packages/dms/frontend-vue"); -const probe = path.join(temporary, "captured-probe.cjs"); -const run = () => { - /** @type {unknown} */ - const result = JSON.parse( - execFileSync( - process.execPath, - ["--max-old-space-size=650", probe, temporary], - { - encoding: "utf8", - stdio: ["ignore", "pipe", "pipe"], - maxBuffer: 64 * 1024 * 1024, - }, - ), - ); - if (!isProbeResult(result)) - throw new TypeError("The captured probe printed an unexpected result"); - return result; -}; -/** - * @param {ProbeResult} result - * @param {string} name - */ -const entry = (result, name) => - result.autoImports.find((item) => item.name === name); - -try { - fs.mkdirSync(copy, { recursive: true }); - for (const file of [ - "layers", - "package.json", - "dms.frontend.ts", - "dms.frontend.build.ts", - ]) - fs.cpSync(path.join(frontend, file), path.join(copy, file), { - recursive: true, - }); - fs.symlinkSync( - path.join(frontend, "node_modules"), - path.join(copy, "node_modules"), - ); - fs.symlinkSync( - path.join(root, "node_modules"), - path.join(temporary, "node_modules"), - ); - fs.symlinkSync( - path.join(root, "packages/interface-dms"), - path.join(temporary, "packages/interface-dms"), - ); - captureProbe(probe); - const baseline = run(); - const bucket = path.join( - copy, - "layers/dms-ui/app/composables/chart/apexDatetimeBuckets.ts", - ); - const originalBucket = fs.readFileSync(bucket, "utf8"); - const ts = createTypeScriptRequire(path.join(frontend, "package.json"))( - "typescript", - ); - const ast = ts.createSourceFile( - bucket, - originalBucket, - ts.ScriptTarget.Latest, - true, - ); - /** @type {import("typescript").TypeNode | undefined} */ - let timestamp; - /** @param {import("typescript").Node} node */ - const visit = (node) => { - if (ts.isParameter(node) && node.name.getText(ast) === "timestamp") - timestamp = node.type; - ts.forEachChild(node, visit); - }; - const formatter = ast.statements.find( - (node) => hasTextName(node) && node.name.text === "bucketAxisFormatter", - ); - visit(formatter); - assert.ok(timestamp); - const narrowed = - originalBucket.slice(0, timestamp.getStart(ast)) + - "number" + - originalBucket.slice(timestamp.end); - assert.notEqual( - narrowed, - originalBucket, - "The baseline bucket callback must accept unknown", - ); - fs.writeFileSync(bucket, narrowed); - const narrowResult = run(); - assert.notDeepEqual( - entry(narrowResult, "bucketAxisFormatter"), - entry(baseline, "bucketAxisFormatter"), - ); - assert.match( - entry(narrowResult, "bucketAxisFormatter").signature, - /timestamp\?: number/, - ); - fs.writeFileSync(bucket, originalBucket); - - const extra = path.join( - copy, - "layers/dms-ui/app/composables/internal/probeExtraAutoImport.ts", - ); - fs.mkdirSync(path.dirname(extra), { recursive: true }); - fs.writeFileSync( - extra, - "/** @internal */\nexport function probeExtraAutoImport(value: unknown): unknown { return value; }\n", - ); - const extraResult = run(); - assert.equal(entry(baseline, "probeExtraAutoImport"), undefined); - assert.equal(entry(extraResult, "probeExtraAutoImport").injected, true); - assert.match( - entry(extraResult, "probeExtraAutoImport").signature, - /value: unknown/, - ); - fs.rmSync(extra); - - const register = path.join(copy, "layers/dms-ui/app/plugins/register.ts"); - const originalRegister = fs.readFileSync(register, "utf8"); - const reordered = originalRegister - .replace(" registerBlockFunctions();\n", "") - .replace( - " registerDefaultDataTypes();", - " registerBlockFunctions();\n registerDefaultDataTypes();", - ); - assert.notEqual(reordered, originalRegister); - fs.writeFileSync(register, reordered); - const reorderResult = run(); - assert.equal( - reorderResult.registrations.length, - baseline.registrations.length, - ); - assert.notDeepEqual(reorderResult.registrations, baseline.registrations); - const firstDifference = baseline.registrations.findIndex( - (event, index) => - JSON.stringify(event) !== - JSON.stringify(reorderResult.registrations[index]), - ); - fs.writeFileSync(register, originalRegister); - - fs.writeFileSync( - extra, - 'export const probePropertyOrder: { first: string; second: number } = { first: "", second: 0 };\n', - ); - const propertyOrder = entry(run(), "probePropertyOrder"); - fs.writeFileSync( - extra, - 'export const probePropertyOrder: { second: number; first: string } = { first: "", second: 0 };\n', - ); - assert.deepEqual(entry(run(), "probePropertyOrder"), propertyOrder); - fs.writeFileSync( - extra, - 'export const probePropertyOrder: { second: string; first: string } = { first: "", second: "" };\n', - ); - assert.notDeepEqual(entry(run(), "probePropertyOrder"), propertyOrder); - - fs.writeFileSync( - extra, - "export function probeExtraAutoImport(value: unknown): Record { return {}; }\n", - ); - const explicit = run(); - const owner = path.join(copy, "layers/dms-ui/app/build/probeTypeOwner.ts"); - fs.writeFileSync( - owner, - "export function probeTypeOwner(value: unknown): Record { return {}; }\n", - ); - fs.writeFileSync( - extra, - "import { probeTypeOwner } from '../../build/probeTypeOwner';\nexport function probeExtraAutoImport(value: Parameters[0]): ReturnType { return {}; }\n", - ); - const derived = run(); - assert.deepEqual( - entry(explicit, "probeExtraAutoImport"), - entry(derived, "probeExtraAutoImport"), - ); - const runtimeFile = path.join(copy, "dms.frontend.ts"); - const runtimeSource = fs.readFileSync(runtimeFile, "utf8"); - const runtimeAst = ts.createSourceFile( - runtimeFile, - runtimeSource, - ts.ScriptTarget.Latest, - true, - ); - /** - * @param {import("typescript").Node} node - * @returns {node is import("typescript").VariableStatement} - */ - const isComponentsDeclaration = (node) => - ts.isVariableStatement(node) && - node.declarationList.declarations[0].name.getText(runtimeAst) === - "components"; - const components = runtimeAst.statements.find(isComponentsDeclaration); - const initializer = components?.declarationList.declarations[0].initializer; - if (initializer?.getText(runtimeAst).includes("import.meta.glob")) { - const entryFile = path.join( - copy, - "layers/dms-ui/app/entries/probe-components.ts", - ); - const prefix = `${path.relative(path.dirname(entryFile), copy)}/`; - const expression = initializer - .getText(runtimeAst) - .replaceAll("./layers/", `${prefix}layers/`); - /** @type {Set} */ - const references = new Set(); - /** @param {import("typescript").Node} node */ - const collectReferences = (node) => { - if (ts.isIdentifier(node)) references.add(node.text); - ts.forEachChild(node, collectReferences); - }; - collectReferences(initializer); - const imports = runtimeAst.statements - .filter((node) => { - if (!ts.isImportDeclaration(node) || !node.importClause) return false; - const { name, namedBindings } = node.importClause; - const bindings = namedBindings - ? ts.isNamedImports(namedBindings) - ? namedBindings.elements.map((binding) => binding.name) - : [namedBindings.name] - : []; - return [name, ...bindings].some( - (binding) => binding && references.has(binding.text), - ); - }) - .map((node) => - node.getText(runtimeAst).replaceAll("./layers/", `${prefix}layers/`), - ) - .join("\n"); - fs.mkdirSync(path.dirname(entryFile), { recursive: true }); - fs.writeFileSync( - entryFile, - `${imports}\nconst local = ${expression};\nexport const components = Object.fromEntries(Object.entries(local).map(([name, loader]) => [name.replace(${JSON.stringify(prefix)}, './'), loader]));\n`, - ); - fs.writeFileSync( - runtimeFile, - runtimeSource.slice(0, components.getStart(runtimeAst)) + - "import { components } from './layers/dms-ui/app/entries/probe-components';" + - runtimeSource.slice(components.end), - ); - assert.deepEqual(run().registrations, baseline.registrations); - } else { - assert.match(runtimeSource, /entries/); - } - process.stdout.write( - `${JSON.stringify({ autoImports: baseline.autoImports.length, registrations: baseline.registrations.length, bucketAxisFormatter: { before: entry(baseline, "bucketAxisFormatter").signature, after: entry(narrowResult, "bucketAxisFormatter").signature }, extraAutoImport: entry(extraResult, "probeExtraAutoImport"), registrationReorder: { firstDifference, before: baseline.registrations[firstDifference], after: reorderResult.registrations[firstDifference] }, ownerDerivedEquivalent: true, objectPropertyOrderEquivalent: true, objectPropertyTypeChangeDetected: true, capturedSingleFile: true, importedEntryMaps: true })}\n`, - ); -} finally { - fs.rmSync(temporary, { recursive: true, force: true }); -} diff --git a/packages/dms/frontend-vue/scripts/prove-comment-emit.mjs b/packages/dms/frontend-vue/scripts/prove-comment-emit.mjs deleted file mode 100644 index ad6a4a1af..000000000 --- a/packages/dms/frontend-vue/scripts/prove-comment-emit.mjs +++ /dev/null @@ -1,171 +0,0 @@ -import assert from "node:assert/strict"; -import { execFileSync } from "node:child_process"; -import { readFileSync } from "node:fs"; -import { createRequire } from "node:module"; -import { join } from "node:path"; - -/** - * @param {string[]} args - * @returns {string} - */ -const git = (...args) => - execFileSync("git", args, { encoding: "utf8", maxBuffer: 64 * 1024 * 1024 }); -const root = git("rev-parse", "--show-toplevel").trim(); -const area = "packages/dms/frontend-vue"; -const requireFrom = join(root, area, "package.json"); -/** @type {(id: string) => typeof import("typescript")} */ -const loadTypeScript = createRequire(requireFrom); -/** @type {(id: string) => typeof import("prettier")} */ -const loadPrettier = createRequire(requireFrom); -/** @type {(id: string) => typeof import("vue/compiler-sfc")} */ -const loadCompilerSfc = createRequire(requireFrom); -const ts = loadTypeScript("typescript"); -const prettier = loadPrettier("prettier"); -const { parse, compileTemplate } = loadCompilerSfc("vue/compiler-sfc"); -const base = process.argv[2]; -assert.ok(base, "Usage: node frontend-comment-emit.mjs "); -const head = process.argv.includes("--working") - ? "WORKTREE" - : git("rev-parse", "HEAD").trim(); -const printer = ts.createPrinter({ removeComments: true }); - -/** - * @param {string} source - * @param {string} file - * @returns {Promise} - */ -async function scriptSyntax(source, file) { - const parsed = ts.createSourceFile( - file, - source, - ts.ScriptTarget.Latest, - true, - ); - return prettier.format(printer.printFile(parsed), { parser: "typescript" }); -} - -/** - * @param {string} source - * @param {string} file - */ -async function sourceEmit(source, file) { - const tree = ts.createSourceFile(file, source, ts.ScriptTarget.Latest, true); - assert.deepEqual(tree.parseDiagnostics, [], `${file}: syntax diagnostics`); - return { - syntax: await scriptSyntax(source, file), - javascript: file.endsWith(".d.ts") - ? null - : await scriptSyntax( - ts.transpileModule(source, { - fileName: file, - compilerOptions: { - target: ts.ScriptTarget.ESNext, - module: ts.ModuleKind.ESNext, - removeComments: true, - sourceMap: false, - inlineSourceMap: false, - newLine: ts.NewLineKind.LineFeed, - }, - }).outputText, - `${file}.js`, - ), - }; -} - -/** - * @param {string} source - * @param {string} file - */ -async function vueOutput(source, file) { - const { descriptor, errors } = parse(source, { filename: file }); - assert.deepEqual(errors, [], `${file}: SFC parse errors`); - const scripts = await Promise.all( - [descriptor.script, descriptor.scriptSetup].map(async (script) => - script - ? { - attributes: script.attrs, - ...(await sourceEmit(script.content, `${file}.ts`)), - } - : null, - ), - ); - const template = descriptor.template; - /** @param {boolean} ssr */ - function render(ssr) { - if (!template) return null; - const result = compileTemplate({ - source: template.content, - filename: file, - id: "comment-proof", - ssr, - ssrCssVars: descriptor.cssVars, - scoped: descriptor.styles.some((style) => style.scoped), - compilerOptions: { comments: true, expressionPlugins: ["typescript"] }, - }); - assert.deepEqual(result.errors, [], `${file}: template compile errors`); - return result.code; - } - return { - scripts, - templateAttributes: template?.attrs, - render: render(false), - ssr: render(true), - styles: descriptor.styles.map((style) => ({ - attributes: style.attrs, - content: style.content, - })), - custom: descriptor.customBlocks.map((block) => ({ - type: block.type, - attributes: block.attrs, - content: block.content, - })), - }; -} - -/** - * @param {string} ref - * @returns {string[]} - */ -function files(ref) { - const paths = - ref === "WORKTREE" - ? git( - "ls-files", - "--cached", - "--others", - "--exclude-standard", - "--", - area, - ) - : git("ls-tree", "-r", "--name-only", ref, "--", area); - return paths - .trim() - .split("\n") - .filter((file) => /\.(?:[cm]?js|ts|vue|css)$/.test(file)) - .filter((file) => !file.startsWith(`${area}/tests/`)) - .filter((file) => file !== `${area}/scripts/prove-comment-emit.mjs`); -} - -const before = files(base); -const after = files(head); -assert.deepEqual(after, before, "Product source file set changed"); -for (const file of before) { - /** @param {string} ref */ - const compileProduct = (ref) => { - const source = - ref === "WORKTREE" - ? readFileSync(join(root, file), "utf8") - : git("show", `${ref}:${file}`); - if (file.endsWith(".vue")) return vueOutput(source, file); - if (file.endsWith(".css")) return source; - return sourceEmit(source, file); - }; - assert.deepEqual( - await compileProduct(head), - await compileProduct(base), - `${file}: non-comment source or emit changed`, - ); -} -console.log( - `Frontend comment emit proof: ${before.length} product files unchanged against ${base} (${head === "WORKTREE" ? "working tree" : `committed HEAD ${head}`}).`, -); diff --git a/packages/dms/frontend-vue/scripts/prove-component-registration.mjs b/packages/dms/frontend-vue/scripts/prove-component-registration.mjs deleted file mode 100644 index 4f1864502..000000000 --- a/packages/dms/frontend-vue/scripts/prove-component-registration.mjs +++ /dev/null @@ -1,259 +0,0 @@ -import assert from "node:assert/strict"; -import { execFileSync } from "node:child_process"; -import { readFileSync, writeFileSync } from "node:fs"; -import { dirname, join, relative, resolve } from "node:path"; -import { fileURLToPath } from "node:url"; -import { createRequire } from "node:module"; -import { z } from "zod"; - -const frontend = resolve(dirname(fileURLToPath(import.meta.url)), ".."); -const root = resolve(frontend, "../../.."); -/** @type {(id: string) => typeof import("vite")} */ -const require = createRequire(import.meta.url); -const { createServer } = require("vite"); -const baseRef = process.argv[2] ?? "HEAD^"; -assert(process.argv[3], "Pass the baseline worktree as the third argument"); -const baseRoot = resolve(process.argv[3]); -const baseFrontend = join(baseRoot, "packages/dms/frontend-vue"); -assert.equal( - execFileSync("git", ["rev-parse", "HEAD"], { - cwd: baseRoot, - encoding: "utf8", - }), - execFileSync("git", ["rev-parse", baseRef], { cwd: root, encoding: "utf8" }), - "The baseline files belong to the requested revision", -); -const base = execFileSync( - "git", - ["show", `${baseRef}:packages/dms/frontend-vue/dms.frontend.ts`], - { cwd: root, encoding: "utf8" }, -); -const renamed = new Map( - execFileSync( - "git", - ["diff", "--name-status", "--find-renames=100%", baseRef, "HEAD"], - { cwd: root, encoding: "utf8" }, - ) - .trim() - .split("\n") - .filter((line) => line.startsWith("R100\t")) - .map((line) => { - const [, before, after] = line.split("\t"); - return [ - relative(frontend, resolve(root, after)), - relative(frontend, resolve(root, before)), - ]; - }), -); -const current = readFileSync(join(frontend, "dms.frontend.ts"), "utf8"); -const baseId = join(baseFrontend, "__registry-proof-base.ts"); -const currentId = join(frontend, "__registry-proof-current.ts"); -const server = await createServer({ - root: frontend, - configFile: false, - optimizeDeps: { noDiscovery: true }, - server: { middlewareMode: true, watch: null }, - plugins: [ - { - name: "registration-observer", - enforce: "pre", - resolveId(id, importer) { - if ([baseId, currentId].includes(id)) return id; - if (id === "#dms/frontend-module") return "\0registry-sdk"; - if (id.endsWith(".vue")) { - const file = id.startsWith("/layers/") - ? resolve(frontend, "." + id) - : resolve(dirname(importer), id); - return ( - "\0registry-component:" + - relative( - file.startsWith(baseFrontend + "/") ? baseFrontend : frontend, - file, - ) - ); - } - if (/\/app\/(?:plugins|middleware)\//.test(id) || id.endsWith(".css")) - return "\0registry-inert:" + id; - }, - load(id) { - if ([baseId, currentId].includes(id)) - return ( - (id === baseId ? base : current) + - (id === currentId && process.env.REGISTRATION_PROOF_DROP_COMPONENT - ? "\ndelete components[Object.keys(components)[0]];" - : "") + - (id === currentId && process.env.REGISTRATION_PROOF_REVERSE_ORDER - ? "\nconst originalSortedEntries = sortedEntries; sortedEntries = loaders => originalSortedEntries(loaders).reverse();" - : "") + - "\nexport const registryMaps = { components, customPages, dynamicPages, layouts, errorPages };" - ); - if (id === "\0registry-sdk") - return "export const useDmsRuntimeConfig = () => ({ public: { dms: {} } });"; - if (id.startsWith("\0registry-component:")) { - const target = id.slice("\0registry-component:".length); - return `export default { target: ${JSON.stringify(renamed.get(target) ?? target)} };`; - } - if (id.startsWith("\0registry-inert:")) - return `function plugin() {} plugin.registryId = ${JSON.stringify(id)}; export default plugin;`; - }, - }, - ], -}); -/** - * A module as the observer plugin loads it: the layer's registration function and the maps it fills. - * - * @typedef {object} RegistryModule - * @property {{ setup: (sdk: object) => unknown }} default - * @property {Record unknown>>} registryMaps - */ - -/** - * @param {unknown} value - * @returns {value is Record} - */ -function isRecord(value) { - return typeof value === "object" && value !== null; -} - -/** - * @param {unknown} value - * @returns {value is () => unknown} - */ -function isLoader(value) { - return typeof value === "function"; -} - -/** - * @param {unknown} value - * @returns {value is Record unknown>} - */ -function isLoaderMap(value) { - return isRecord(value) && Object.values(value).every(isLoader); -} - -/** - * @param {unknown} value - * @returns {value is RegistryModule} - */ -function isRegistryModule(value) { - return ( - isRecord(value) && - isRecord(value.default) && - typeof value.default.setup === "function" && - isRecord(value.registryMaps) && - Object.values(value.registryMaps).every(isLoaderMap) - ); -} - -/** - * @param {unknown} value - * @returns {value is { default: { target: unknown } }} - */ -function hasTarget(value) { - return ( - isRecord(value) && - isRecord(value.default) && - Object.hasOwn(value.default, "target") - ); -} - -/** - * @param {unknown} value - * @returns {value is ((...args: never[]) => unknown) & { registryId?: unknown }} - */ -function isFunction(value) { - return z.function().safeParse(value).success; -} - -/** - * @param {string} id - * @returns {Promise} - */ -async function loadRegistry(id) { - /** @type {unknown} */ - const module = await server.ssrLoadModule(id); - if (!isRegistryModule(module)) - throw new Error(`${id} exports no registration setup and registry maps`); - return module; -} - -/** - * @param {RegistryModule} module - * @returns {Promise<{ events: unknown[][], maps: Record }>} - */ -async function registration(module) { - /** @type {unknown[][]} */ - const events = []; - /** @type {Record} */ - const target = { options: { public: {} } }; - const sdk = new Proxy(target, { - get(target, name) { - if (name in target) return target[name]; - return (/** @type {unknown[]} */ ...args) => - events.push([ - name, - ...args.map((value) => { - if (isFunction(value)) - return { - function: value.name, - arity: value.length, - importedModule: value.registryId, - }; - return isRecord(value) && value.__asyncLoader - ? { kind: "async-component" } - : value; - }), - ]); - }, - }); - module.default.setup(sdk); - /** @type {Record} */ - const maps = {}; - for (const [name, entries] of Object.entries(module.registryMaps)) { - maps[name] = []; - for (const [key, load] of Object.entries(entries).sort(([a], [b]) => - a.localeCompare(b), - )) { - const loaded = await load(); - if (!hasTarget(loaded)) - throw new Error(`${key} loads no default export with a target`); - maps[name].push([key, loaded.default.target]); - } - } - return { events, maps }; -} -try { - const before = await registration(await loadRegistry(baseId)); - const after = await registration(await loadRegistry(currentId)); - assert.deepEqual( - after.maps, - before.maps, - "Vite-expanded registration keys and loader targets", - ); - assert.deepEqual( - after.events, - before.events, - "complete setup registration names, order, aliases and options", - ); - assert( - !after.maps.components.some( - ([key]) => - key.endsWith("/table/TableBody.vue") || - key.endsWith("/ImagePresentation.vue"), - ), - ); - if (process.env.REGISTRATION_PROOF_OUTPUT) - writeFileSync( - process.env.REGISTRATION_PROOF_OUTPUT, - JSON.stringify( - { before, after, identicalFileRenames: [...renamed] }, - null, - 2, - ), - ); - console.log( - `PASS Vite registration parity against ${baseRef} (${after.maps.components.length} component loaders; ${after.events.length} registration events)`, - ); -} finally { - await server.close(); -} From 89de1ab583a285cf63617cccdcdac31ac38c84a1 Mon Sep 17 00:00:00 2001 From: Antony Rizzitelli Date: Sun, 15 Nov 2026 17:27:21 +0000 Subject: [PATCH 2/7] refactor: replace the release-range checks with semver --- .agents/skills/formal-release-range/SKILL.md | 113 ------- .../skills/formal-release-range/manifest.mjs | 109 ------- .../formal-release-range/manifest.test.mjs | 62 ---- .agents/skills/formal-release-range/probe.mjs | 109 ------- .agents/skills/verify-dms/verify.config.ts | 10 - .claude/skills/formal-release-range | 1 - .github/workflows/release.yml | 31 +- checks/bounded-range.test.mjs | 107 ------ checks/dms-range.mjs | 68 ---- checks/dms-range.test.mjs | 123 ------- checks/release-range.mjs | 98 ------ checks/release-range.test.mjs | 304 ------------------ package.json | 2 +- .../interface-dms/scripts/raise-dms-range.mjs | 27 +- 14 files changed, 32 insertions(+), 1132 deletions(-) delete mode 100644 .agents/skills/formal-release-range/SKILL.md delete mode 100644 .agents/skills/formal-release-range/manifest.mjs delete mode 100644 .agents/skills/formal-release-range/manifest.test.mjs delete mode 100644 .agents/skills/formal-release-range/probe.mjs delete mode 120000 .claude/skills/formal-release-range delete mode 100644 checks/bounded-range.test.mjs delete mode 100644 checks/dms-range.mjs delete mode 100644 checks/dms-range.test.mjs delete mode 100644 checks/release-range.mjs delete mode 100644 checks/release-range.test.mjs diff --git a/.agents/skills/formal-release-range/SKILL.md b/.agents/skills/formal-release-range/SKILL.md deleted file mode 100644 index 0e323ce63..000000000 --- a/.agents/skills/formal-release-range/SKILL.md +++ /dev/null @@ -1,113 +0,0 @@ ---- -name: formal-release-range -description: Checks the release rules that the interfaces in the tree satisfy the ranges the DMS module publishes for them (satisfiesImplementedRange for interface-dms, satisfiesBoundedRange for interface-dms-builder) against semver, prereleases included. Use when checks/release-range.mjs, the validate job of .github/workflows/release.yml, the interface-dms or interface-dms-builder version, or their ranges in packages/dms/package.json change. ---- - -# Verify the release range rule - -The `validate` job of the module release refuses to publish `@antelopejs/dms` unless the interface version in the tree satisfies the range the module publishes for it, and that range is `>=0.M.p <0.(M+1).0`. Without the cap, a breaking interface minor would reach a DMS that does not implement it. If the rule lets a bad pair through, a module is published against an interface release that does not exist, and it installs broken. - -The rule is `satisfiesImplementedRange` in `checks/release-range.mjs`. It runs with no dependency, before any install, and the workflow imports it. The properties drive that function and compare it with `semver`. - -The same job refuses the release unless the `@antelopejs/interface-dms-builder` version in the tree satisfies the range the module declares for it. The module consumes that interface rather than implementing it, so its range is any `>=A.B.C =0.M.p` and one `<0.(M+1).0` comparator and the text is `>=0.M.p <0.(M+1).0`. It catches a cap that is not the next minor, a loose operator, a union, a prerelease bound, a leading zero and a missing end anchor. -2. **Membership.** For every stable version, the rule and `semver.satisfies` agree. It catches string comparison, an inclusive cap, an exclusive floor, a dropped patch compare, and a dropped major. -3. **Cap.** No version in the next minor or any later release is accepted, prereleases and build metadata included. -4. **Prerelease and build metadata.** For every version, prereleases and build metadata included, the rule and `semver.satisfies` agree. -5. **Bounded membership.** For every `>=A.B.C -``` - -`dms-control formal` runs it on the base and on HEAD of a diff that touches the coverage record. The same output means the diff kept the covered behaviour (`verify-dms` verdict rule 5). A changed line fails it. - -## Findings on main - -None. The four disagreements recorded before #538 are fixed, and the strict properties now hold. F1 was a prerelease of the floor (`0.9.1-next.0`), F2 a prerelease inside the range (`0.9.5-rc.1`), F3 build metadata (`0.9.5+build.0`), and F4 a leading zero in a range field (`>=0.09.1 <0.10.0`). The rule now rejects every prerelease version, because neither bound carries one, ignores build metadata, and refuses a number with a leading zero. - -## Bounds and exclusions - -Each property runs 1000 cases with default seed `20261029`, overridable with `FC_SEED`. -Floors are `0.minor.patch` with minor 0 to 4 or 8 to 11 (the second band crosses the `0.9`/`0.10` width change) and patch 0 to 4. -Versions put the major at 0 (weight 8) or 1 to 4, the minor around the floor minor or 0 to 12, and the patch around the floor patch or 0 to 4. Prerelease is absent or `next`, `alpha` or `rc` followed by `.0` to `.4`. Build metadata is absent, `build.0` or `sha.abc123`. -Ranges are the canonical range for the floor and 29 mutations of it (cap and floor changes, other operators, spacing, `v` prefix, prerelease or build bounds, a union, a wildcard, empty, missing, and leading zeros). -An explicit table pins the three oracle readings for seven versions on `>=0.9.1 <0.10.0`, and another pins ten malformed or long versions against `semver.satisfies`. - -Bounded membership runs 1000 cases with default seed `20261114`, also overridable with `FC_SEED`. Each floor and cap part is 0 to 2 or 8 to 11, so floors above the cap occur too. The version is the floor, the cap or any such triple, followed by nothing, a `next` or `rc` prerelease `.0` to `.2`, or `+build.0` or `+sha.abc123`. The bounded shape test refuses 18 other spellings, among them leading and trailing junk, and a table pins six versions or ranges around 2^53 - 1 against `semver.satisfies`. - -The properties do not draw numbers beyond 2^53 (only the unsafe-integers table does), versions or ranges with a leading `v` or `=` on the version, version strings that are not valid semver, or any other check in the `validate` step. The workflow wiring (the step imports `./checks/release-range.mjs` as an ES module) was confirmed by extracting the step and running it, not by a test: a test that read `release.yml` would break the repo's rule that no test reads the file it covers. -`satisfiesImplementedRange` only accepts the exact spelling `>=0.M.p <0.(M+1).0`. A semver-equivalent spelling (two spaces, a trailing space) is rejected on purpose. - -## Coverage record - -Last synchronized product commit is the one that added the interface-dms-builder rule (ANT-2585), which reran every scratch break below. - -| File | What it covers | -| --- | --- | -| `checks/release-range.mjs` | `satisfiesImplementedRange`, `satisfiesBoundedRange` and the version and range patterns they read | -| `checks/release-range.test.mjs` | The properties, the generators and the oracle table | -| `checks/bounded-range.test.mjs` | The bounded-range properties and their generators | -| `.github/workflows/release.yml` | The `validate` job step that imports the module, with `actions/setup-node` before it, and the interface-dms-builder manifest it reads | -| `package.json` | The root `test` script that runs the check, and the `fast-check` and `semver` dev dependencies | - -Compare changes since the sync commit against this table before trusting an old pass. -After an intended change to the rule or the range shape, update the oracle, generators and this record together. -Move the sync commit forward only after confirming the properties still describe the changed code and rerunning them, including a scratch break. - -## Confirm the check can fail - -In a scratch copy of `checks/release-range.mjs`, `checks/release-range.test.mjs` and `checks/bounded-range.test.mjs` outside the repo, apply one break at a time to the module, run both test files, and restore it. These breaks were caught at the sync commit with the default seeds (`20261029` and `20261114`). Every break made at least one file exit 1, and the restored copy exited 0 on both. - -| Scratch break | Failing observation | -| --- | --- | -| Numbers compared as strings | Range fails for `>=0.9.0 <0.10.0`, membership for `0.10.0` on `>=0.1.1 <0.2.0`. Bounded membership fails for `8.0.0` on `>=1.1.9 <10.2.11`. | -| Drop the patch comparison | Membership fails for `0.9.0` on `>=0.9.3 <0.10.0`. Bounded membership fails too. | -| Ignore the major | Membership fails for `1.2.2` on `>=0.2.2 <0.3.0`, cap for `1.2.1` on `>=0.2.1 <0.3.0`. Bounded membership fails too. | -| Exclusive floor | Range fails for `>=0.0.4 <0.1.0` at the floor itself. Membership and bounded membership fail too. | -| Inclusive cap | Membership and cap fail for `0.11.0` on `>=0.10.1 <0.11.0`. Bounded membership fails for `9.1.11` on `>=1.1.0 <9.1.11`. | -| Drop `capMinor === floorMinor + 1` | Range fails for `>=0.11.1 <0.13.0` ("cap skips a minor"). | -| Drop `capMajor === 0` | Range fails for `>=0.0.3 <1.1.0` ("cap major one"). | -| Drop `capPatch === 0` | Range fails for `>=0.3.1 <0.4.1` ("cap patch"). | -| Drop the `^` of the range pattern | Bounded shape fails: it accepts `<0.4.1 >=0.4.0 <1.0.0` and `x>=0.4.0 <1.0.0`. | -| Drop the `$` of the range pattern | Range fails for `>=0.9.3 <0.10.0 ` ("trailing space"). Bounded shape fails too. | -| Accept a prerelease | Prerelease and the oracle table fail. Bounded membership fails for `1.8.2-next.0` on `>=1.8.2 <9.0.2`. | -| Refuse build metadata | Prerelease, malformed versions and the oracle table fail. Bounded membership fails for `8.0.0+build.0` on `>=1.1.9 <10.2.11`. | -| Accept a leading zero (`\d+`) | Range fails for `>=0.00.4 <0.1.0`. The leading-zero test, malformed versions and bounded shape fail too. | -| Accept an empty build suffix | Malformed versions fails for `0.9.5+`. | -| Accept `_` in build metadata | Malformed versions fails for `0.9.5+a_b`. | -| Drop the 256-character cap | Malformed versions fails for a 257-character version. | -| Drop the safe-integer guard in the range | Unsafe integers fails for `0.4.1` on `>=0.4.0 <9007199254740992.0.0`, `>=0.4.0 <1.9007199254740992.0`, and `1.0.0` on `>=0.9007199254740992.0 <2.0.0`. | -| Drop the safe-integer guard in the version | Unsafe integers fails for `0.9007199254740992.0` and `0.4.99999999999999999999` on `>=0.4.0 <1.0.0`. | diff --git a/.agents/skills/formal-release-range/manifest.mjs b/.agents/skills/formal-release-range/manifest.mjs deleted file mode 100644 index d8ea4bfe8..000000000 --- a/.agents/skills/formal-release-range/manifest.mjs +++ /dev/null @@ -1,109 +0,0 @@ -/** - * @typedef {{ - * name?: string; - * version?: string; - * private?: boolean; - * license?: string; - * repository?: { type?: string, url?: string, directory?: string }; - * publishConfig?: { registry?: string, access?: string, provenance?: boolean }; - * antelopeJs?: { implements?: string[] }; - * dependencies?: Record; - * }} Manifest - */ - -/** - * @param {unknown} value - * @returns {value is Record} - */ -function isRecord(value) { - return typeof value === "object" && value !== null && !Array.isArray(value); -} - -/** - * @param {unknown} value - * @returns {value is string} - */ -function isString(value) { - return typeof value === "string"; -} - -/** - * @param {unknown} value - * @returns {value is boolean} - */ -function isBoolean(value) { - return typeof value === "boolean"; -} - -/** - * @param {unknown} value - * @param {string} key - * @param {(field: unknown) => boolean} isField - * @returns {boolean} Whether the value is an object that holds the field absent or passing - */ -function optionally(value, key, isField) { - return isRecord(value) && (!Object.hasOwn(value, key) || isField(value[key])); -} - -/** - * @param {unknown} value - * @returns {boolean} - */ -function isStringList(value) { - return Array.isArray(value) && value.every(isString); -} - -/** - * @param {unknown} value - * @returns {boolean} - */ -function isStringRecord(value) { - return isRecord(value) && Object.values(value).every(isString); -} - -/** - * @param {unknown} value - * @returns {boolean} - */ -function isRepository(value) { - return optionally(value, "type", isString) && optionally(value, "url", isString) && optionally(value, "directory", isString); -} - -/** - * @param {unknown} value - * @returns {boolean} - */ -function isPublishConfig(value) { - return optionally(value, "registry", isString) && optionally(value, "access", isString) && optionally(value, "provenance", isBoolean); -} - -/** - * Whether a parsed document holds the manifest fields the probe reads with the types it relies on. Other fields pass through untouched, since the probe writes the manifest back out. - * @param {unknown} value - * @returns {value is Manifest} - */ -export function isManifest(value) { - return ( - isRecord(value) && - optionally(value, "name", isString) && - optionally(value, "version", isString) && - optionally(value, "private", isBoolean) && - optionally(value, "license", isString) && - optionally(value, "repository", isRepository) && - optionally(value, "publishConfig", isPublishConfig) && - optionally(value, "antelopeJs", (field) => optionally(field, "implements", isStringList)) && - optionally(value, "dependencies", isStringRecord) - ); -} - -/** - * @param {string} text The manifest's JSON text - * @param {string} file Where the text came from, named when it is refused - * @returns {Manifest} The decoded manifest; a document that is not one is refused - */ -export function parseManifest(text, file) { - /** @type {unknown} */ - const manifest = JSON.parse(text); - if (!isManifest(manifest)) throw new Error(`${file} is not a package manifest`); - return manifest; -} diff --git a/.agents/skills/formal-release-range/manifest.test.mjs b/.agents/skills/formal-release-range/manifest.test.mjs deleted file mode 100644 index b26e5d3c2..000000000 --- a/.agents/skills/formal-release-range/manifest.test.mjs +++ /dev/null @@ -1,62 +0,0 @@ -import assert from "node:assert/strict"; -import test from "node:test"; -import { isManifest, parseManifest } from "./manifest.mjs"; - -const MANIFEST = { - name: "@antelopejs/dms", - version: "0.9.5", - private: false, - license: "Apache-2.0", - repository: { type: "git", url: "git+https://github.com/AntelopeJS/dms.git", directory: "packages/dms" }, - publishConfig: { registry: "https://registry.npmjs.org", access: "public", provenance: true }, - antelopeJs: { implements: ["dms@0"] }, - dependencies: { "@antelopejs/interface-dms": ">=0.9.1 <0.10.0" }, -}; - -await test("isManifest accepts a full manifest, a bare one and fields it does not read", () => { - assert.equal(isManifest(MANIFEST), true); - assert.equal(isManifest({}), true); - assert.equal(isManifest({ ...MANIFEST, scripts: { build: "tsc" } }), true); - assert.equal(isManifest({ antelopeJs: {} }), true); -}); - -await test("isManifest refuses a value that is not an object", () => { - assert.equal(isManifest(null), false); - assert.equal(isManifest([MANIFEST]), false); - assert.equal(isManifest("{}"), false); -}); - -await test("isManifest refuses each field the probe reads when it has the wrong type", () => { - for (const broken of [ - { name: 1 }, - { version: ["0.9.5"] }, - { private: "false" }, - { license: null }, - { repository: "git" }, - { repository: { url: 3 } }, - { repository: { type: [] } }, - { repository: { directory: false } }, - { publishConfig: [] }, - { publishConfig: { registry: 1 } }, - { publishConfig: { access: true } }, - { publishConfig: { provenance: "yes" } }, - { antelopeJs: "dms" }, - { antelopeJs: { implements: "dms@0" } }, - { antelopeJs: { implements: [1] } }, - { dependencies: ["a"] }, - { dependencies: { a: 1 } }, - ]) { - assert.equal(isManifest({ ...MANIFEST, ...broken }), false, JSON.stringify(broken)); - } -}); - -await test("isManifest reads own fields only", () => { - assert.equal(isManifest(Object.create({ name: 1 })), true); -}); - -await test("parseManifest returns the decoded manifest and names the file it refuses", () => { - assert.deepEqual(parseManifest(JSON.stringify(MANIFEST), "packages/dms/package.json"), MANIFEST); - assert.throws(() => parseManifest('{"name":1}', "packages/dms/package.json"), /packages\/dms\/package\.json is not a package manifest/); - assert.throws(() => parseManifest("[]", "a/package.json"), /a\/package\.json/); - assert.throws(() => parseManifest("not json", "a/package.json"), SyntaxError); -}); diff --git a/.agents/skills/formal-release-range/probe.mjs b/.agents/skills/formal-release-range/probe.mjs deleted file mode 100644 index 979724667..000000000 --- a/.agents/skills/formal-release-range/probe.mjs +++ /dev/null @@ -1,109 +0,0 @@ -#!/usr/bin/env node -/** - * Prints what the `validate` step of `.github/workflows/release.yml` decides - * in a checkout, one line per case: the step's node script, extracted from - * the workflow wherever the rule lives (inline, or imported from - * `checks/release-range.mjs`), runs against fixture package manifests built - * from the checkout's own, and its exit code is the decision; a crash also - * prints the error's name, so it never reads as a refusal. dms-control - * runs it on the base and on HEAD: the same lines mean the diff kept the - * covered behaviour. - * - * Usage: node .agents/skills/formal-release-range/probe.mjs - */ -import { spawnSync } from "node:child_process"; -import { cpSync, existsSync, mkdirSync, mkdtempSync, readFileSync, rmSync, writeFileSync } from "node:fs"; -import { tmpdir } from "node:os"; -import path from "node:path"; -import { parseManifest } from "./manifest.mjs"; - -/** @typedef {import("./manifest.mjs").Manifest} Manifest */ - -const VERSIONS = ["0.8.9", "0.9.0", "0.9.1", "0.9.5", "0.9.1-next.0", "0.9.5-rc.1", "0.9.5+build.0", "0.10.0", "0.10.0-next.0", "1.9.1"]; -const RANGES = [">=0.9.1 <0.10.0", ">=0.9.1 <0.11.0", ">0.9.1 <0.10.0", ">=0.9.1 <=0.10.0", "^0.9.1", ">=0.09.1 <0.10.0", ">=0.9.1 <0.10.0", ""]; -const CANONICAL = { version: "0.9.5", range: ">=0.9.1 <0.10.0" }; -const BUILDER_VERSIONS = ["0.3.9", "0.4.0", "0.4.1", "0.10.0", "0.4.1-next.0", "0.4.1+build.0", "1.0.0"]; -const BUILDER_RANGES = [">=0.4.0 <1.0.0", ">=0.4.0 <0.5.0", "^0.4.0", ">=0.4.0 <=1.0.0", ">=0.04.0 <1.0.0", ""]; -const BUILDER_CANONICAL = { version: "0.4.1", range: ">=0.4.0 <1.0.0" }; -/** @type {Record Manifest>} */ -const MANIFEST_BREAKS = { - "name": (pkg) => ({ ...pkg, name: "@antelopejs/dms-renamed" }), - "private": (pkg) => ({ ...pkg, private: true }), - "license": (pkg) => ({ ...pkg, license: "MIT" }), - "repository": (pkg) => ({ ...pkg, repository: { ...pkg.repository, url: "git+https://example.com/dms.git" } }), - "registry": (pkg) => ({ ...pkg, publishConfig: { ...pkg.publishConfig, registry: "https://registry.example.com/" } }), - "access": (pkg) => ({ ...pkg, publishConfig: { ...pkg.publishConfig, access: "restricted" } }), - "provenance": (pkg) => ({ ...pkg, publishConfig: { ...pkg.publishConfig, provenance: false } }), - "implements": (pkg) => ({ ...pkg, antelopeJs: { ...pkg.antelopeJs, implements: [...(pkg.antelopeJs?.implements ?? []), "@antelopejs/interface-extra"] } }), -}; - -/** @param {string} checkout */ -function validateStep(checkout) { - const workflow = readFileSync(path.join(checkout, ".github", "workflows", "release.yml"), "utf8"); - const match = /^(\s*)node((?: --\S+)*) <<'NODE'\n([\s\S]*?)\n\1NODE$/m.exec(workflow); - if (!match) throw new Error("release.yml has no node <<'NODE' script"); - const indent = match[1]; - const body = match[3].split("\n").map((line) => (line.startsWith(indent) ? line.slice(indent.length) : line)).join("\n"); - return { flags: match[2].trim().split(/\s+/).filter(Boolean), body }; -} - -/** - * @param {string} checkout - * @param {{ flags: string[], body: string }} step - * @param {Record} manifests - */ -function decide(checkout, step, manifests) { - const dir = mkdtempSync(path.join(tmpdir(), "release-range-probe-")); - try { - for (const [file, value] of Object.entries(manifests)) { - mkdirSync(path.dirname(path.join(dir, file)), { recursive: true }); - writeFileSync(path.join(dir, file), JSON.stringify(value)); - } - if (existsSync(path.join(checkout, "checks"))) cpSync(path.join(checkout, "checks"), path.join(dir, "checks"), { recursive: true }); - const run = spawnSync(process.execPath, step.flags, { cwd: dir, input: step.body, encoding: "utf8" }); - const crash = /^(\w*Error)(?: \[(\w+)\])?/m.exec(run.stderr ?? ""); - return crash ? `${run.status} ${crash[1]}${crash[2] ? ` ${crash[2]}` : ""}` : `${run.status}`; - } finally { - rmSync(dir, { recursive: true, force: true }); - } -} - -const checkout = path.resolve(process.argv[2] ?? "."); -const step = validateStep(checkout); - -/** - * The one place this probe reads a manifest of the checkout; a document that is not a manifest is refused. - * @param {string} file - * @returns {Manifest} - */ -function readManifest(file) { - return parseManifest(readFileSync(file, "utf8"), file); -} - -const pkg = readManifest(path.join(checkout, "packages", "dms", "package.json")); -const iface = readManifest(path.join(checkout, "packages", "interface-dms", "package.json")); -const builderFile = path.join(checkout, "packages", "interface-dms-builder", "package.json"); -const builder = existsSync(builderFile) ? readManifest(builderFile) : undefined; -const manifests = (/** @type {Manifest} */ dms, /** @type {string} */ version, builderVersion = BUILDER_CANONICAL.version) => ({ - "packages/dms/package.json": dms, - "packages/interface-dms/package.json": { ...iface, version }, - ...(builder && { "packages/interface-dms-builder/package.json": { ...builder, version: builderVersion } }), -}); -const withRange = (/** @type {string} */ range, builderRange = BUILDER_CANONICAL.range) => ({ - ...pkg, - dependencies: { ...pkg.dependencies, "@antelopejs/interface-dms": range, "@antelopejs/interface-dms-builder": builderRange }, -}); - -for (const range of RANGES) { - for (const version of VERSIONS) { - console.log(`range ${JSON.stringify(range)} version ${version}\texit ${decide(checkout, step, manifests(withRange(range), version))}`); - } -} -for (const range of builder ? BUILDER_RANGES : []) { - for (const version of BUILDER_VERSIONS) { - console.log(`builder range ${JSON.stringify(range)} version ${version}\texit ${decide(checkout, step, manifests(withRange(CANONICAL.range, range), CANONICAL.version, version))}`); - } -} -for (const [name, broken] of Object.entries(MANIFEST_BREAKS)) { - console.log(`manifest ${name}\texit ${decide(checkout, step, manifests(broken(withRange(CANONICAL.range)), CANONICAL.version))}`); -} diff --git a/.agents/skills/verify-dms/verify.config.ts b/.agents/skills/verify-dms/verify.config.ts index 907ea84fb..be2dd18f8 100644 --- a/.agents/skills/verify-dms/verify.config.ts +++ b/.agents/skills/verify-dms/verify.config.ts @@ -180,7 +180,6 @@ const config: VerifyConfig = { "packages/interface-dms/package.json", "packages/*/.release-it.json", ".github/workflows/release*.yml", - "checks/release-range.mjs", ], funnels: [], why: "the package names, their files, their interface range and the release workflows claim names in the npm registry", @@ -276,15 +275,6 @@ const config: VerifyConfig = { passing: /(?<=^holds +(?:\w+, )*)(\w+)(?=,| \()/gm, behaviour: { kind: "emit" }, }, - { - name: "formal-release-range", - covers: ["checks/release-range.mjs", "checks/release-range.test.mjs", "checks/bounded-range.test.mjs", ".github/workflows/release.yml"], - files: ["checks/release-range.test.mjs", "checks/bounded-range.test.mjs", ".agents/skills/formal-release-range/**"], - command: ["node", "--test", "--test-reporter=tap", "checks/release-range.test.mjs", "checks/bounded-range.test.mjs"], - violation: /^not ok \d+ - (.+?)(?: # TODO.*)?$/gm, - passing: /^ok \d+ - (.+?)(?: # TODO.*)?$/gm, - behaviour: { kind: "probe", command: ["node", ".agents/skills/formal-release-range/probe.mjs"] }, - }, ], }; diff --git a/.claude/skills/formal-release-range b/.claude/skills/formal-release-range deleted file mode 120000 index 316b05c36..000000000 --- a/.claude/skills/formal-release-range +++ /dev/null @@ -1 +0,0 @@ -../../.agents/skills/formal-release-range \ No newline at end of file diff --git a/.github/workflows/release.yml b/.github/workflows/release.yml index 5daaded67..eb0336e8e 100644 --- a/.github/workflows/release.yml +++ b/.github/workflows/release.yml @@ -42,19 +42,8 @@ jobs: test "$GITHUB_REF" = "refs/heads/$release_branch" node --input-type=module <<'NODE' import { readFileSync } from 'node:fs' - import { satisfiesBoundedRange, satisfiesImplementedRange } from './checks/release-range.mjs' - const readJson = (path) => JSON.parse(readFileSync(path, 'utf8')) - const pkg = readJson('./packages/dms/package.json') - const iface = readJson('./packages/interface-dms/package.json') - const builder = readJson('./packages/interface-dms-builder/package.json') - // The interface packages of this repository the runtime may provide, by manifest. - const implementable = new Map([ - ['@antelopejs/interface-dms', iface], - ['@antelopejs/interface-dms-builder', builder], - ]) - const implementsInTree = (name) => - implementable.has(name) && - satisfiesImplementedRange(implementable.get(name).version, pkg.dependencies[name]) + const pkg = JSON.parse(readFileSync('./packages/dms/package.json', 'utf8')) + const inTree = ['@antelopejs/interface-dms', '@antelopejs/interface-dms-builder'] if ( pkg.name !== '@antelopejs/dms' || pkg.private === true || @@ -63,13 +52,21 @@ jobs: pkg.publishConfig.registry !== 'https://registry.npmjs.org/' || pkg.publishConfig.access !== 'public' || pkg.publishConfig.provenance !== true || - !satisfiesBoundedRange(builder.version, pkg.dependencies['@antelopejs/interface-dms-builder']) || - // The runtime provides interface-dms, and otherwise only interface packages of this - // repository, each at a range its version in the tree satisfies. !pkg.antelopeJs.implements.includes('@antelopejs/interface-dms') || - !pkg.antelopeJs.implements.every(implementsInTree) + !pkg.antelopeJs.implements.every((name) => inTree.includes(name)) ) process.exit(1) NODE + # The runtime implements both in-tree interfaces, so each range must admit + # the version in the tree and stop below its next minor. + - name: Validate the interface ranges + run: | + for name in interface-dms interface-dms-builder; do + version=$(node -p "require('./packages/$name/package.json').version") + range=$(node -p "require('./packages/dms/package.json').dependencies['@antelopejs/$name']") + next_minor=$(npx --yes semver@7 -i minor "$version") + npx --yes semver@7 -r "$range" "$version" >/dev/null + if npx --yes semver@7 -r "$range" "$next_minor" >/dev/null; then exit 1; fi + done # A module released while an interface it depends on has unreleased source # points at a published interface that lacks what the module calls. Release # the interface first. diff --git a/checks/bounded-range.test.mjs b/checks/bounded-range.test.mjs deleted file mode 100644 index 33324834d..000000000 --- a/checks/bounded-range.test.mjs +++ /dev/null @@ -1,107 +0,0 @@ -import assert from "node:assert/strict"; -import { test } from "node:test"; -import fc from "fast-check"; -import semver from "semver"; -import { satisfiesBoundedRange } from "./release-range.mjs"; - -const SEED = Number(process.env.FC_SEED ?? 20261114); -const NUM_RUNS = 1000; - -/** - * Runs a property with the fixed seed. Rerun with the printed `seed` and - * `path` in `FC_SEED` to reproduce a failure and its shrinking. - * @template T - * @param {fc.Arbitrary} arbitrary - * @param {(value: T) => boolean | void} predicate - */ -const assertProperty = (arbitrary, predicate) => - fc.assert(fc.property(arbitrary, predicate), { - seed: SEED, - numRuns: NUM_RUNS, - }); - -/** Numbers 0 to 2 and 8 to 11, where a decimal-width bug (`9` against `10`) shows. */ -const part = fc.oneof( - fc.integer({ min: 0, max: 2 }), - fc.integer({ min: 8, max: 11 }), -); -const triple = fc.tuple(part, part, part).map((numbers) => numbers.join(".")); -const suffix = fc.option( - fc.oneof( - fc - .tuple(fc.constantFrom("next", "rc"), fc.integer({ min: 0, max: 2 })) - .map(([tag, id]) => `-${tag}.${id}`), - fc.constantFrom("+build.0", "+sha.abc123"), - ), - { nil: "" }, -); - -const cases = fc.tuple(triple, triple).chain(([floor, cap]) => - fc - .tuple( - fc - .constantFrom(floor, cap) - .chain((bound) => fc.oneof(fc.constant(bound), triple)), - suffix, - ) - .map(([plain, tail]) => ({ floor, cap, version: plain + tail })), -); - -await test("membership: the rule agrees with semver.satisfies on every >=A.B.C { - assertProperty(cases, ({ floor, cap, version }) => { - const range = `>=${floor} <${cap}`; - return ( - satisfiesBoundedRange(version, range) === semver.satisfies(version, range) - ); - }); -}); - -await test("shape: every other spelling of a range is refused, even when semver admits the version (catches a pattern without its start or end anchor, or one that reads any comparator)", () => { - const version = "0.4.1"; - const spellings = [ - "^0.4.0", - "~0.4.0", - "0.4.x", - "*", - ">=0.4.0", - "<1.0.0", - ">0.4.0 <1.0.0", - ">=0.4.0 <=1.0.0", - ">=0.4.0 <1.0.0", - ">=0.4.0 <1.0.0 ", - ">=v0.4.0 <1.0.0", - ">=0.4.0-0 <1.0.0", - ">=0.4.0 <1.0.0 || >=2.0.0 <3.0.0", - "<0.4.1 >=0.4.0 <1.0.0", - "x>=0.4.0 <1.0.0", - ">=0.04.0 <1.0.0", - "", - undefined, - ]; - const accepted = spellings.filter((range) => - satisfiesBoundedRange(version, range), - ); - assert.deepEqual(accepted, []); -}); - -await test("unsafe integers: a number beyond 2^53 - 1 in the version or the range is refused, as semver refuses it (catches a missing or partial safe-integer guard on any part)", () => { - const observed = [ - ["0.9007199254740992.0", ">=0.4.0 <1.0.0"], - ["0.4.99999999999999999999", ">=0.4.0 <1.0.0"], - ["0.4.1", ">=0.4.0 <9007199254740992.0.0"], - ["0.4.1", ">=0.4.0 <1.9007199254740992.0"], - ["1.0.0", ">=0.9007199254740992.0 <2.0.0"], - ["0.4.1", ">=0.4.0 <9007199254740991.0.0"], - ].map(([version, range]) => [ - satisfiesBoundedRange(version, range), - semver.satisfies(version, range), - ]); - assert.deepEqual(observed, [ - [false, false], - [false, false], - [false, false], - [false, false], - [false, false], - [true, true], - ]); -}); diff --git a/checks/dms-range.mjs b/checks/dms-range.mjs deleted file mode 100644 index 43b6dc0b6..000000000 --- a/checks/dms-range.mjs +++ /dev/null @@ -1,68 +0,0 @@ -import { satisfiesImplementedRange } from "./release-range.mjs"; - -const NUMBER = String.raw`(0|[1-9]\d*)`; -const PLAIN_RELEASE = new RegExp(String.raw`^${NUMBER}\.${NUMBER}\.${NUMBER}$`); -const DMS_DEPENDENCY = /("@antelopejs\/interface-dms":\s*")([^"]*)(")/g; - -/** - * The range the DMS module must publish once the interface version in the tree - * falls outside the one it has: the canonical `>=0.M.p <0.(M+1).0` with the - * bumped version as its floor. A range that already satisfies - * `satisfiesImplementedRange` stays as it is, even when its floor lags the - * version. - * - * The rule only covers the 0.x line, so a major of 1 or more is refused rather - * than given a range the release workflow would reject. A prerelease or a - * version with build metadata is refused too: no canonical range admits a - * prerelease. The written range is checked against the same rule before it is - * returned. - * - * @param {string} version The bumped `version` of the interface package. - * @param {string | undefined} range The module's current dependency range on it. - * @returns {string | undefined} The range to write, or undefined when none is needed. - * @throws {Error} When the version has no canonical range to raise to. - */ -export const raisedDmsRange = (version, range) => { - if (satisfiesImplementedRange(version, range)) return undefined; - const release = PLAIN_RELEASE.exec(version); - if (!release) { - throw new Error( - `Cannot raise the DMS range for "${version}": only a plain 0.M.p release has a canonical range`, - ); - } - const [, major, minor, patch] = release; - if (major !== "0") { - throw new Error( - `Cannot raise the DMS range for ${version}: the release rule only covers 0.x`, - ); - } - const raised = `>=0.${minor}.${patch} <0.${Number(minor) + 1}.0`; - if (!satisfiesImplementedRange(version, raised)) { - throw new Error( - `Cannot raise the DMS range for ${version}: "${raised}" fails the release rule`, - ); - } - return raised; -}; - -/** - * Rewrites the `@antelopejs/interface-dms` dependency of a module manifest and - * nothing else, so the file keeps its formatting. - * - * @param {string} manifest The text of the module's `package.json`. - * @param {string} range The range to write. - * @returns {string} The manifest text with the new range. - * @throws {Error} When the manifest does not declare the dependency exactly once. - */ -export const withDmsRange = (manifest, range) => { - const matches = [...manifest.matchAll(DMS_DEPENDENCY)]; - if (matches.length !== 1) { - throw new Error( - `Expected one "@antelopejs/interface-dms" dependency, found ${matches.length}`, - ); - } - return manifest.replace( - DMS_DEPENDENCY, - (_, head, __, tail) => `${head}${range}${tail}`, - ); -}; diff --git a/checks/dms-range.test.mjs b/checks/dms-range.test.mjs deleted file mode 100644 index 30885ca35..000000000 --- a/checks/dms-range.test.mjs +++ /dev/null @@ -1,123 +0,0 @@ -import assert from "node:assert/strict"; -import test from "node:test"; -import { raisedDmsRange, withDmsRange } from "./dms-range.mjs"; -import { satisfiesImplementedRange } from "./release-range.mjs"; - -const CURRENT = ">=0.13.0 <0.14.0"; - -await test("a version the range admits writes nothing (catches a hook that rewrites the range on every release)", () => { - assert.equal(raisedDmsRange("0.13.4", CURRENT), undefined); - assert.equal(raisedDmsRange("0.13.0", CURRENT), undefined); -}); - -await test("a floor that lags the version is kept (catches a hook that moves the floor to the version)", () => { - assert.equal(raisedDmsRange("0.13.9", ">=0.13.2 <0.14.0"), undefined); -}); - -await test("a minor outside the range writes the canonical next range with the version as floor (catches a floor at .0 or a cap that is not the next minor)", () => { - assert.equal(raisedDmsRange("0.14.0", CURRENT), ">=0.14.0 <0.15.0"); - assert.equal(raisedDmsRange("0.14.2", CURRENT), ">=0.14.2 <0.15.0"); -}); - -await test("the minor is read as a number (catches string comparison and a cap built by editing digits)", () => { - assert.equal(raisedDmsRange("0.9.5", ">=0.8.0 <0.9.0"), ">=0.9.5 <0.10.0"); - assert.equal(raisedDmsRange("0.10.0", ">=0.9.1 <0.10.0"), ">=0.10.0 <0.11.0"); -}); - -await test("a version below the range is not left behind (catches a hook that only handles a bump up)", () => { - assert.equal(raisedDmsRange("0.12.9", CURRENT), ">=0.12.9 <0.13.0"); -}); - -await test("a range of another shape is replaced by the canonical one (catches a hook that trusts any range containing the version)", () => { - assert.equal(raisedDmsRange("0.13.4", "^0.13.0"), ">=0.13.4 <0.14.0"); - assert.equal(raisedDmsRange("0.13.4", undefined), ">=0.13.4 <0.14.0"); -}); - -await test("every written range passes the release rule (catches a spelling the rule rejects)", () => { - for (const version of [ - "0.0.1", - "0.9.5", - "0.10.0", - "0.14.0", - "0.14.2", - "0.99.7", - ]) { - const raised = raisedDmsRange(version, "workspace:*"); - assert.ok( - raised !== undefined && satisfiesImplementedRange(version, raised), - version, - ); - } -}); - -await test("a major of 1 or more is refused (catches a range such as >=1.0.0 <1.1.0 that the release rule rejects)", () => { - assert.throws(() => raisedDmsRange("1.0.0", CURRENT), /only covers 0\.x/); - assert.throws(() => raisedDmsRange("2.3.1", CURRENT), /only covers 0\.x/); -}); - -await test("a prerelease or build metadata is refused when the range does not admit it (catches a floor such as >=0.14.0-next.0 that the rule rejects)", () => { - assert.throws( - () => raisedDmsRange("0.14.0-next.0", CURRENT), - /plain 0\.M\.p release/, - ); - assert.throws( - () => raisedDmsRange("0.13.4-rc.1", CURRENT), - /plain 0\.M\.p release/, - ); - assert.throws( - () => raisedDmsRange("0.14.0+build.1", CURRENT), - /plain 0\.M\.p release/, - ); -}); - -await test("a malformed version is refused (catches a parser that reads a leading v, a short version or leading zeros)", () => { - for (const version of [ - "", - "abc", - "v0.14.0", - "0.14", - "0.14.0.1", - "0.014.0", - "00.14.0", - " 0.14.0", - ]) { - assert.throws( - () => raisedDmsRange(version, CURRENT), - /plain 0\.M\.p release/, - version, - ); - } -}); - -await test("a version too long for the rule is refused (catches a hook that does not check its output against the rule)", () => { - assert.throws( - () => raisedDmsRange(`0.14.${"1".repeat(300)}`, CURRENT), - /fails the release rule/, - ); -}); - -const MANIFEST = `{ - "dependencies": { - "@antelopejs/interface-dms": ">=0.13.0 <0.14.0", - "@antelopejs/interface-dms-builder": ">=0.4.0 <1.0.0" - }, - "antelopeJs": { - "implements": [ - "@antelopejs/interface-dms" - ] - } -} -`; - -await test("only the interface dependency is rewritten (catches a global replace of the package name and the -builder neighbour)", () => { - assert.equal( - withDmsRange(MANIFEST, ">=0.14.0 <0.15.0"), - MANIFEST.replace(">=0.13.0 <0.14.0", ">=0.14.0 <0.15.0"), - ); -}); - -await test("a manifest without exactly one interface dependency is refused (catches a silent no-op or a double rewrite)", () => { - assert.throws(() => withDmsRange("{}", ">=0.14.0 <0.15.0"), /found 0/); - const twice = `{"a":{"@antelopejs/interface-dms":"x"},"b":{"@antelopejs/interface-dms":"y"}}`; - assert.throws(() => withDmsRange(twice, ">=0.14.0 <0.15.0"), /found 2/); -}); diff --git a/checks/release-range.mjs b/checks/release-range.mjs deleted file mode 100644 index e0f236559..000000000 --- a/checks/release-range.mjs +++ /dev/null @@ -1,98 +0,0 @@ -const MAX_VERSION_LENGTH = 256; -const NUMBER = String.raw`(0|[1-9]\d*)`; -const TRIPLE = String.raw`${NUMBER}\.${NUMBER}\.${NUMBER}`; -const BOUNDED = new RegExp(String.raw`^>=${TRIPLE} <${TRIPLE}$`); -const RELEASE = new RegExp( - String.raw`^${TRIPLE}(?:\+[0-9A-Za-z-]+(?:\.[0-9A-Za-z-]+)*)?$`, -); - -/** - * @param {readonly number[]} left - * @param {readonly number[]} right - * @returns {number} Negative, zero or positive as `left` sorts before, with or after `right`. - */ -const compare = (left, right) => - left.map((part, index) => part - (right[index] ?? 0)).find((d) => d !== 0) ?? - 0; - -/** - * The floor and cap of a `>=A.B.C { - const bounds = BOUNDED.exec(range ?? ""); - if (!bounds) return undefined; - const numbers = bounds.slice(1).map(Number); - if (!numbers.every(Number.isSafeInteger)) return undefined; - return { floor: numbers.slice(0, 3), cap: numbers.slice(3) }; -}; - -/** - * The `major.minor.patch` of a release version, or undefined for a prerelease, - * a number with a leading zero or beyond 2^53 - 1, or a version longer than 256 - * characters. - * - * @param {string} version - * @returns {number[] | undefined} - */ -const parseRelease = (version) => { - const release = - version.length <= MAX_VERSION_LENGTH ? RELEASE.exec(version) : null; - const numbers = release?.slice(1).map(Number); - return numbers?.every(Number.isSafeInteger) ? numbers : undefined; -}; - -/** - * Whether a version in the tree satisfies a `>=A.B.C { - const bounds = parseBounded(range); - const release = parseRelease(version); - if (!bounds || !release) return false; - return ( - compare(release, bounds.floor) >= 0 && compare(release, bounds.cap) < 0 - ); -}; - -/** - * Whether the interface version sitting in the tree satisfies the range the - * module publishes for it, as `semver.satisfies` with default options decides. - * The module implements the interface, so the range must cap it below the next - * minor (`>=0.M.p <0.(M+1).0`): a breaking interface minor must never reach a - * DMS that does not implement it. A floor that lags the tree only widens what - * consumers may install, so the version has to satisfy the range, not equal its - * floor. - * - * @param {string} version The `version` of the interface package in the tree. - * @param {string | undefined} range The module's dependency range on it. - * @returns {boolean} True when the range has the implemented shape and admits the version. - */ -export const satisfiesImplementedRange = (version, range) => { - const bounds = parseBounded(range); - if (!bounds) return false; - const [, floorMinor] = bounds.floor; - const [capMajor, capMinor, capPatch] = bounds.cap; - return ( - capMajor === 0 && - capMinor === floorMinor + 1 && - capPatch === 0 && - satisfiesBoundedRange(version, range) - ); -}; diff --git a/checks/release-range.test.mjs b/checks/release-range.test.mjs deleted file mode 100644 index 660ebd526..000000000 --- a/checks/release-range.test.mjs +++ /dev/null @@ -1,304 +0,0 @@ -import assert from "node:assert/strict"; -import { test } from "node:test"; -import fc from "fast-check"; -import semver from "semver"; -import { satisfiesImplementedRange } from "./release-range.mjs"; - -/** @typedef {{ minor: number, patch: number }} Floor */ -/** @typedef {{ major: number, minor: number, patch: number, pre?: string, build?: string }} VersionParts */ - -const SEED = Number(process.env.FC_SEED ?? 20261029); -const NUM_RUNS = 1000; - -/** - * Runs a property with the fixed seed. Rerun with the printed `seed` and - * `path` in `FC_SEED` to reproduce a failure and its shrinking. - * @template T - * @param {fc.Arbitrary} arbitrary - * @param {(value: T) => boolean | void} predicate - */ -const assertProperty = (arbitrary, predicate) => - fc.assert(fc.property(arbitrary, predicate), { - seed: SEED, - numRuns: NUM_RUNS, - }); - -/** - * @param {Floor} floor - * @returns {string} - */ -const canonicalRange = ({ minor, patch }) => - `>=0.${minor}.${patch} <0.${minor + 1}.0`; - -/** - * @param {VersionParts} version - * @returns {string} - */ -const formatVersion = ({ major, minor, patch, pre, build }) => - `${major}.${minor}.${patch}${pre === undefined ? "" : `-${pre}`}${build === undefined ? "" : `+${build}`}`; - -/** - * @param {string} version - * @param {string | undefined} range - * @returns {boolean} - */ -const rule = (version, range) => satisfiesImplementedRange(version, range); -/** - * @param {string} version - * @param {string} range - * @returns {boolean} - */ -const oracle = (version, range) => semver.satisfies(version, range); -/** - * @param {string} version - * @param {string} range - * @returns {boolean} - */ -const oracleWithPrereleases = (version, range) => - semver.satisfies(version, range, { includePrerelease: true }); - -/** - * Floors `0.minor.patch` with minor and patch 0 to 4, plus the minors 8 to 11 - * where a decimal-width bug (`0.9.x` against `0.10.0`) shows. - */ -const floors = fc.record({ - minor: fc.oneof( - fc.integer({ min: 0, max: 4 }), - fc.integer({ min: 8, max: 11 }), - ), - patch: fc.integer({ min: 0, max: 4 }), -}); - -const prereleases = fc - .tuple(fc.constantFrom("next", "alpha", "rc"), fc.integer({ min: 0, max: 4 })) - .map(([tag, id]) => `${tag}.${id}`); -const builds = fc.constantFrom("build.0", "sha.abc123"); - -/** - * @param {Floor} floor - * @param {boolean} suffixes - */ -const versionsAround = (floor, suffixes) => - fc.record({ - major: fc.oneof( - { weight: 8, arbitrary: fc.constant(0) }, - { weight: 1, arbitrary: fc.integer({ min: 1, max: 4 }) }, - ), - minor: fc.oneof( - fc.integer({ min: Math.max(0, floor.minor - 1), max: floor.minor + 2 }), - fc.integer({ min: 0, max: 12 }), - ), - patch: fc.oneof( - fc.integer({ min: Math.max(0, floor.patch - 1), max: floor.patch + 1 }), - fc.integer({ min: 0, max: 4 }), - ), - pre: suffixes - ? fc.option(prereleases, { nil: undefined }) - : fc.constant(undefined), - build: suffixes - ? fc.option(builds, { nil: undefined }) - : fc.constant(undefined), - }); - -/** @param {boolean} suffixes */ -const cases = (suffixes) => - floors.chain((floor) => - versionsAround(floor, suffixes).map((version) => ({ - floor, - range: canonicalRange(floor), - version, - text: formatVersion(version), - })), - ); -const stableCases = cases(false); -const suffixedCases = cases(true); - -/** @type {[string, (floor: Floor) => string | undefined][]} */ -const mutations = [ - ["canonical", (f) => canonicalRange(f)], - ["cap skips a minor", (f) => `>=0.${f.minor}.${f.patch} <0.${f.minor + 2}.0`], - [ - "cap on the floor minor", - (f) => `>=0.${f.minor}.${f.patch} <0.${f.minor}.9`, - ], - ["cap patch", (f) => `>=0.${f.minor}.${f.patch} <0.${f.minor + 1}.1`], - ["cap inclusive", (f) => `>=0.${f.minor}.${f.patch} <=0.${f.minor + 1}.0`], - ["floor exclusive", (f) => `>0.${f.minor}.${f.patch} <0.${f.minor + 1}.0`], - ["major one", (f) => `>=1.${f.minor}.${f.patch} <1.${f.minor + 1}.0`], - ["cap major one", (f) => `>=0.${f.minor}.${f.patch} <1.${f.minor + 1}.0`], - ["caret", (f) => `^0.${f.minor}.${f.patch}`], - ["tilde", (f) => `~0.${f.minor}.${f.patch}`], - ["exact", (f) => `0.${f.minor}.${f.patch}`], - ["floor only", (f) => `>=0.${f.minor}.${f.patch}`], - ["cap only", (f) => `<0.${f.minor + 1}.0`], - ["two spaces", (f) => `>=0.${f.minor}.${f.patch} <0.${f.minor + 1}.0`], - ["tab", (f) => `>=0.${f.minor}.${f.patch}\t<0.${f.minor + 1}.0`], - ["trailing space", (f) => `${canonicalRange(f)} `], - ["no space", (f) => `>=0.${f.minor}.${f.patch}<0.${f.minor + 1}.0`], - ["v prefix", (f) => `>=v0.${f.minor}.${f.patch} <0.${f.minor + 1}.0`], - ["prerelease cap", (f) => `>=0.${f.minor}.${f.patch} <0.${f.minor + 1}.0-0`], - [ - "prerelease floor", - (f) => `>=0.${f.minor}.${f.patch}-0 <0.${f.minor + 1}.0`, - ], - ["build floor", (f) => `>=0.${f.minor}.${f.patch}+b <0.${f.minor + 1}.0`], - ["union", (f) => `${canonicalRange(f)} || >=0.20.0 <0.21.0`], - ["reversed", (f) => `<0.${f.minor + 1}.0 >=0.${f.minor}.${f.patch}`], - ["wildcard", (f) => `0.${f.minor}.x`], - ["star", () => "*"], - ["empty", () => ""], - ["missing", () => undefined], - [ - "leading zero floor minor", - (f) => `>=0.0${f.minor}.${f.patch} <0.${f.minor + 1}.0`, - ], - [ - "leading zero floor patch", - (f) => `>=0.${f.minor}.0${f.patch} <0.${f.minor + 1}.0`, - ], - [ - "leading zero cap minor", - (f) => `>=0.${f.minor}.${f.patch} <0.0${f.minor + 1}.0`, - ], -]; - -const candidates = floors.chain((floor) => - fc.constantFrom(...mutations).map(([name, build]) => ({ - floor, - name, - range: build(floor), - })), -); - -/** - * The floor and cap `semver` reads from a range, or undefined when `semver` - * cannot parse it or it is anything but one `>=` and one `<` comparator on - * plain `0.minor.patch` and `0.(minor+1).0` versions. `semver` drops a `>=0.0.0` - * floor, so a lone `<0.1.0` stands for the floor `0.0.0`. - * @param {string | undefined} range - * @returns {Floor | undefined} - */ -const semverFloor = (range) => { - if (range === undefined) return undefined; - /** @type {readonly import("semver").Comparator[]} */ - let comparators; - try { - const { set } = new semver.Range(range); - if (set.length !== 1) return undefined; - comparators = set[0]; - } catch { - return undefined; - } - const [lower, upper] = - comparators.length === 1 - ? [new semver.Comparator(">=0.0.0"), comparators[0]] - : comparators; - if (comparators.length > 2 || upper === undefined) return undefined; - /** - * @param {import("semver").Comparator} c - * @returns {boolean} - */ - const plain = (c) => - c.semver.prerelease.length === 0 && c.semver.build.length === 0; - if (lower.operator !== ">=" || upper.operator !== "<") return undefined; - if (!plain(lower) || !plain(upper) || lower.semver.major !== 0) - return undefined; - if (upper.semver.major !== 0 || upper.semver.patch !== 0) return undefined; - if (upper.semver.minor !== lower.semver.minor + 1) return undefined; - return { minor: lower.semver.minor, patch: lower.semver.patch }; -}; - -/** - * @param {{ floor: Floor, range: string | undefined }} input - * @returns {boolean} - */ -const acceptsOnlyCanonical = ({ floor, range }) => { - const parsed = semverFloor(range); - const probe = `0.${(parsed ?? floor).minor}.${(parsed ?? floor).patch}`; - return ( - rule(probe, range) === - (parsed !== undefined && range === canonicalRange(parsed)) - ); -}; -await test("range: the rule accepts exactly the canonical >=0.M.p <0.(M+1).0 range semver parses (catches a cap that is not the next minor, a loose operator, a union or a leading zero)", () => { - assertProperty(candidates, acceptsOnlyCanonical); -}); - -await test("range: a leading zero in the floor or cap is rejected like semver rejects the range", () => { - for (const range of [ - ">=0.09.1 <0.10.0", - ">=0.9.01 <0.10.0", - ">=0.9.1 <0.010.0", - ]) { - assert.equal(semver.validRange(range), null); - assert.equal(rule("0.9.1", range), false); - } -}); - -await test("membership: stable versions agree with semver.satisfies on every canonical range (catches string comparison, an inclusive cap, an exclusive floor, a missing patch compare)", () => { - assertProperty( - stableCases, - ({ range, text }) => rule(text, range) === oracle(text, range), - ); -}); - -await test("cap: no version of the next minor or any later release is accepted, prereleases included (catches ordering a prerelease below its release)", () => { - assertProperty( - suffixedCases.filter( - ({ floor, version }) => version.major > 0 || version.minor > floor.minor, - ), - ({ range, text }) => rule(text, range) === false, - ); -}); - -await test("prerelease and build metadata: the rule agrees with semver.satisfies (catches a dropped prerelease or a build suffix read as part of the patch)", () => { - assertProperty( - suffixedCases, - ({ range, text }) => rule(text, range) === oracle(text, range), - ); -}); - -await test("malformed versions: the rule agrees with semver.satisfies (catches a leading zero, an empty or foreign build suffix, or no length cap in the version)", () => { - const range = ">=0.9.1 <0.10.0"; - for (const version of [ - "0.09.5", - "00.9.5", - "0.9.05", - "0.9.5+", - "0.9.5+a_b", - "0.9.5+a..b", - "0.9.5-", - "0.9.5+build-1.x", - `0.9.5+${"a".repeat(250)}`, - `0.9.5+${"a".repeat(251)}`, - ]) { - assert.equal(rule(version, range), oracle(version, range), version); - } -}); - -await test("oracle choice: only the default options state the promise, includePrerelease admits the breaking minor's prerelease", () => { - const range = ">=0.9.1 <0.10.0"; - const observed = [ - "0.9.1", - "0.9.5", - "0.10.0", - "0.9.1-next.0", - "0.9.5-rc.1", - "0.10.0-next.0", - "0.9.5+build.0", - ].map((version) => [ - version, - rule(version, range), - oracle(version, range), - oracleWithPrereleases(version, range), - ]); - assert.deepEqual(observed, [ - ["0.9.1", true, true, true], - ["0.9.5", true, true, true], - ["0.10.0", false, false, false], - ["0.9.1-next.0", false, false, false], - ["0.9.5-rc.1", false, false, true], - ["0.10.0-next.0", false, false, true], - ["0.9.5+build.0", true, true, true], - ]); -}); diff --git a/package.json b/package.json index 1eb58b6fe..034bc8e15 100644 --- a/package.json +++ b/package.json @@ -28,7 +28,7 @@ "lint:fix": "oxlint --fix && pnpm --dir packages/dms/frontend-vue lint:fix", "format": "oxfmt . && pnpm --dir packages/dms/frontend-vue format", "knip": "pnpm --dir packages/interface-dms knip && pnpm --dir packages/interface-dms-builder knip && pnpm --dir packages/dms knip", - "test": "pnpm --dir packages/dms check:backend && pnpm --dir packages/interface-dms test && pnpm --dir packages/dms/playground test && node --test checks/release-range.test.mjs checks/bounded-range.test.mjs checks/dms-range.test.mjs checks/unreleased-interfaces.test.mjs checks/affected-packages.test.mjs .agents/formal/maps.test.mjs .agents/skills/formal-release-range/manifest.test.mjs .agents/skills/verify-dms/rows.test.mjs packages/dms/bin/control/backend-records.test.mjs packages/dms/bin/control/backend-mode.test.mjs packages/dms/bin/control/base.test.mjs packages/dms/bin/control/backend.test.mjs packages/dms/bin/control/browser-wire.test.mjs packages/dms/bin/control/build-cache.test.mjs packages/dms/bin/control/build-key-refusals.test.mjs packages/dms/bin/control/build-key.test.mjs packages/dms/bin/control/build-layout.test.mjs packages/dms/bin/control/consumer-install.test.mjs packages/dms/bin/control/forwarder.test.mjs packages/dms/bin/control/frontend-mode.test.mjs packages/dms/bin/control/frontend-workspace.test.mjs packages/dms/bin/control/identity.test.mjs packages/dms/bin/control/journey.test.mjs packages/dms/bin/control/json.test.mjs packages/dms/bin/control/mail.test.mjs packages/dms/bin/control/module-root.test.mjs packages/dms/bin/control/mongo-server.test.mjs packages/dms/bin/control/mongo.test.mjs packages/dms/bin/control/namespaces.test.mjs packages/dms/bin/control/pid-guard.test.mjs packages/dms/bin/control/pid-reuse.test.mjs packages/dms/bin/control/processes.test.mjs packages/dms/bin/control/renderer.test.mjs packages/dms/bin/control/run-ports.test.mjs packages/dms/bin/control/run-state.test.mjs packages/dms/bin/control/tab.test.mjs packages/dms/bin/control/timing.test.mjs packages/dms/bin/control/verify/behaviour.test.mjs packages/dms/bin/control/verify/decode.test.mjs packages/dms/bin/control/verify/doors.test.mjs packages/dms/bin/control/verify/formal.test.mjs packages/dms/bin/control/verify/map.test.mjs packages/dms/scripts/backend-automation.test.mjs packages/dms/scripts/dms-dev.test.mjs packages/dms/scripts/playground-api.test.mjs", + "test": "pnpm --dir packages/dms check:backend && pnpm --dir packages/interface-dms test && pnpm --dir packages/dms/playground test && node --test checks/unreleased-interfaces.test.mjs checks/affected-packages.test.mjs .agents/formal/maps.test.mjs .agents/skills/verify-dms/rows.test.mjs packages/dms/bin/control/backend-records.test.mjs packages/dms/bin/control/backend-mode.test.mjs packages/dms/bin/control/base.test.mjs packages/dms/bin/control/backend.test.mjs packages/dms/bin/control/browser-wire.test.mjs packages/dms/bin/control/build-cache.test.mjs packages/dms/bin/control/build-key-refusals.test.mjs packages/dms/bin/control/build-key.test.mjs packages/dms/bin/control/build-layout.test.mjs packages/dms/bin/control/consumer-install.test.mjs packages/dms/bin/control/forwarder.test.mjs packages/dms/bin/control/frontend-mode.test.mjs packages/dms/bin/control/frontend-workspace.test.mjs packages/dms/bin/control/identity.test.mjs packages/dms/bin/control/journey.test.mjs packages/dms/bin/control/json.test.mjs packages/dms/bin/control/mail.test.mjs packages/dms/bin/control/module-root.test.mjs packages/dms/bin/control/mongo-server.test.mjs packages/dms/bin/control/mongo.test.mjs packages/dms/bin/control/namespaces.test.mjs packages/dms/bin/control/pid-guard.test.mjs packages/dms/bin/control/pid-reuse.test.mjs packages/dms/bin/control/processes.test.mjs packages/dms/bin/control/renderer.test.mjs packages/dms/bin/control/run-ports.test.mjs packages/dms/bin/control/run-state.test.mjs packages/dms/bin/control/tab.test.mjs packages/dms/bin/control/timing.test.mjs packages/dms/bin/control/verify/behaviour.test.mjs packages/dms/bin/control/verify/decode.test.mjs packages/dms/bin/control/verify/doors.test.mjs packages/dms/bin/control/verify/formal.test.mjs packages/dms/bin/control/verify/map.test.mjs packages/dms/scripts/backend-automation.test.mjs packages/dms/scripts/dms-dev.test.mjs packages/dms/scripts/playground-api.test.mjs", "prepare": "if [ -e .git ] && command -v git >/dev/null 2>&1 && command -v lefthook >/dev/null 2>&1 && GIT_DIR=.git GIT_WORK_TREE=. git rev-parse --git-dir >/dev/null 2>&1; then GIT_DIR=.git GIT_WORK_TREE=. lefthook install; fi", "checks": "antelopejs-checks", "checks:prepare": "pnpm --filter @antelopejs/interface-dms --filter @antelopejs/interface-dms-builder build", diff --git a/packages/interface-dms/scripts/raise-dms-range.mjs b/packages/interface-dms/scripts/raise-dms-range.mjs index db3135615..35d272dee 100644 --- a/packages/interface-dms/scripts/raise-dms-range.mjs +++ b/packages/interface-dms/scripts/raise-dms-range.mjs @@ -1,9 +1,11 @@ import fs from "node:fs"; import { fileURLToPath } from "node:url"; +import semver from "semver"; import { z } from "zod"; -import { raisedDmsRange, withDmsRange } from "../../../checks/dms-range.mjs"; -const interfaceManifest = z +const DMS_DEPENDENCY = /("@antelopejs\/interface-dms":\s*")([^"]*)(")/g; + +const { version } = z .object({ version: z.string() }) .parse( JSON.parse( @@ -14,15 +16,20 @@ const dmsPath = fileURLToPath( new URL("../../dms/package.json", import.meta.url), ); const dmsText = fs.readFileSync(dmsPath, "utf8"); -const dmsManifest = z +const range = z .object({ dependencies: z.object({ "@antelopejs/interface-dms": z.string() }), }) - .parse(JSON.parse(dmsText)); + .parse(JSON.parse(dmsText)).dependencies["@antelopejs/interface-dms"]; -const raised = raisedDmsRange( - interfaceManifest.version, - dmsManifest.dependencies["@antelopejs/interface-dms"], -); -if (raised !== undefined) - fs.writeFileSync(dmsPath, withDmsRange(dmsText, raised)); +const nextMinor = semver.inc(version, "minor"); +if (!semver.satisfies(version, range) || semver.satisfies(nextMinor, range)) { + const raised = `>=${version} <${nextMinor}`; + fs.writeFileSync( + dmsPath, + dmsText.replace( + DMS_DEPENDENCY, + (_, head, __, tail) => head + raised + tail, + ), + ); +} From 0fea9b5b7a0940b40afe371dc67a718224b94741 Mon Sep 17 00:00:00 2001 From: Antony Rizzitelli Date: Sun, 15 Nov 2026 17:27:23 +0000 Subject: [PATCH 3/7] refactor(dms): remove the backend import and loader scanner --- packages/dms/package.json | 12 +- packages/dms/scripts/backend-checks.cjs | 454 ------------------ packages/dms/scripts/backend-exemptions.cjs | 72 --- .../dms/scripts/backend-import-target.cjs | 446 ----------------- .../scripts/backend-runtime-boundaries.cjs | 27 -- 5 files changed, 6 insertions(+), 1005 deletions(-) delete mode 100644 packages/dms/scripts/backend-exemptions.cjs delete mode 100644 packages/dms/scripts/backend-import-target.cjs delete mode 100644 packages/dms/scripts/backend-runtime-boundaries.cjs diff --git a/packages/dms/package.json b/packages/dms/package.json index 1df21f346..050ce17fa 100644 --- a/packages/dms/package.json +++ b/packages/dms/package.json @@ -77,13 +77,13 @@ "frontend:build": "pnpm --dir playground frontend:build", "frontend:start": "pnpm --dir playground frontend:start", "typecheck": "node scripts/unless-prebuilt.mjs pnpm --filter @antelopejs/interface-dms --filter @antelopejs/interface-dms-builder exec tsc && tsc --noEmit", - "test": "node scripts/unless-prebuilt.mjs pnpm run build && NODE_OPTIONS=\"${NODE_OPTIONS:+$NODE_OPTIONS }--require=\\\"$PWD/scripts/backend-runtime-boundaries.cjs\\\"\" ajs module test .", + "test": "node scripts/unless-prebuilt.mjs pnpm run build && ajs module test .", "check:backend": "node scripts/unless-prebuilt.mjs pnpm run build && node scripts/backend-checks.cjs", - "test:unit": "pnpm run check:backend && node scripts/backend-checks.test.mjs && node scripts/docs-scripts.test.mjs && node scripts/json-record.test.mjs && node scripts/test-files.test.mjs && DMS_TEST_DIR=dist/test/unit NODE_OPTIONS=\"${NODE_OPTIONS:+$NODE_OPTIONS }--require=\\\"$PWD/scripts/backend-runtime-boundaries.cjs\\\"\" ajs module test . $(node scripts/test-files.mjs dist/test/unit dist/test/unit/builder)", - "test:integration": "node scripts/unless-prebuilt.mjs pnpm run build && DMS_TEST_DIR=dist/test/integration NODE_OPTIONS=\"${NODE_OPTIONS:+$NODE_OPTIONS }--require=\\\"$PWD/scripts/backend-runtime-boundaries.cjs\\\"\" ajs module test .", - "test:builder": "node scripts/unless-prebuilt.mjs pnpm run build && DMS_TEST_DIR=dist/test/unit/builder NODE_OPTIONS=\"${NODE_OPTIONS:+$NODE_OPTIONS }--require=\\\"$PWD/scripts/backend-runtime-boundaries.cjs\\\"\" ajs module test .", - "test:builder:1": "node scripts/unless-prebuilt.mjs pnpm run build && DMS_TEST_DIR=dist/test/unit/builder NODE_OPTIONS=\"${NODE_OPTIONS:+$NODE_OPTIONS }--require=\\\"$PWD/scripts/backend-runtime-boundaries.cjs\\\"\" ajs module test . $(node scripts/test-files.mjs dist/test/unit/builder --shard=1/2)", - "test:builder:2": "node scripts/unless-prebuilt.mjs pnpm run build && DMS_TEST_DIR=dist/test/unit/builder NODE_OPTIONS=\"${NODE_OPTIONS:+$NODE_OPTIONS }--require=\\\"$PWD/scripts/backend-runtime-boundaries.cjs\\\"\" ajs module test . $(node scripts/test-files.mjs dist/test/unit/builder --shard=2/2)", + "test:unit": "pnpm run check:backend && node scripts/backend-checks.test.mjs && node scripts/docs-scripts.test.mjs && node scripts/json-record.test.mjs && node scripts/test-files.test.mjs && DMS_TEST_DIR=dist/test/unit ajs module test . $(node scripts/test-files.mjs dist/test/unit dist/test/unit/builder)", + "test:integration": "node scripts/unless-prebuilt.mjs pnpm run build && DMS_TEST_DIR=dist/test/integration ajs module test .", + "test:builder": "node scripts/unless-prebuilt.mjs pnpm run build && DMS_TEST_DIR=dist/test/unit/builder ajs module test .", + "test:builder:1": "node scripts/unless-prebuilt.mjs pnpm run build && DMS_TEST_DIR=dist/test/unit/builder ajs module test . $(node scripts/test-files.mjs dist/test/unit/builder --shard=1/2)", + "test:builder:2": "node scripts/unless-prebuilt.mjs pnpm run build && DMS_TEST_DIR=dist/test/unit/builder ajs module test . $(node scripts/test-files.mjs dist/test/unit/builder --shard=2/2)", "test:watch": "tsc -w & tsc-alias -w", "release": "pnpm --filter @antelopejs/interface-dms build && pnpm --dir frontend-vue install --frozen-lockfile && release-it", "prepack": "pnpm run build", diff --git a/packages/dms/scripts/backend-checks.cjs b/packages/dms/scripts/backend-checks.cjs index 6427f90aa..fcc05393a 100644 --- a/packages/dms/scripts/backend-checks.cjs +++ b/packages/dms/scripts/backend-checks.cjs @@ -1,19 +1,10 @@ const assert = require("node:assert/strict"); const { readdirSync, readFileSync } = require("node:fs"); const nodePath = require("node:path"); -const { sep } = nodePath; const join = nodePath.join.bind(nodePath); const { satisfies, valid, validRange } = require("semver"); -const ts = require("typescript"); const { ACTIONS, TRIGGERS } = require("./backend-automation.cjs"); const { readJsonRecord, textAt } = require("./helpers/json-record.mjs"); -const checkTarget = require("./backend-import-target.cjs"); -const { - constructorExemptions, - builtinExemptions, - requireResolveExemptions, - loaderExemptions, -} = require("./backend-exemptions.cjs"); const root = nodePath.resolve(__dirname, ".."); @@ -110,449 +101,4 @@ for (const layer of ["frontend-vue", "playground/frontend-vue"]) { } } -/** @type {Set} */ -const usedRequireResolveExemptions = new Set(); -const sourceExemptions = { ...constructorExemptions, ...builtinExemptions }; -/** @type {Set} */ -const usedExemptions = new Set(); -const processMembers = new Set([ - "env", - "pid", - "cwd", - "stdout", - "exit", - "stderr", - "execPath", - "hrtime", -]); - -/** - * @param {ts.Node} node - * @returns {boolean} - */ -function memberName(node) { - const parent = node.parent; - return ( - (parent.name === node && - [ - ts.SyntaxKind.PropertyAccessExpression, - ts.SyntaxKind.PropertyAssignment, - ts.SyntaxKind.PropertySignature, - ts.SyntaxKind.PropertyDeclaration, - ts.SyntaxKind.MethodDeclaration, - ].includes(parent.kind)) || - (ts.isBindingElement(parent) && parent.propertyName === node) || - ts.isQualifiedName(parent) - ); -} - -/** - * @param {ts.Node} node - * @param {string} path - * @param {Record} exemptions - * @returns {boolean} - */ -function exempt(node, path, exemptions) { - const source = node.getSourceFile(); - const file = nodePath.relative(join(root, "src"), path).split(sep).join("/"); - const line = - source.getLineAndCharacterOfPosition(node.getStart(source)).line + 1; - const location = `${file}:${line}`; - if (!Object.hasOwn(exemptions, location)) return false; - usedExemptions.add(location); - return true; -} - -/** - * @param {ts.StringLiteral} expression - * @param {string} path - */ -function checkBuiltin(expression, path) { - if (["module", "node:module", "vm", "node:vm"].includes(expression.text)) { - assert.ok( - expression.text === "node:module" && - ts.isImportDeclaration(expression.parent) && - exempt(expression.parent, path, builtinExemptions), - `${path} builtin loader imports are forbidden outside pinned exemptions`, - ); - } -} - -/** - * @param {ts.Node} node - * @param {string} path - */ -function checkConstructor(node, path) { - if (ts.isPropertyAccessExpression(node) && node.name.text === "constructor") { - assert.ok( - exempt(node.name, path, constructorExemptions), - `${path} constructor access is forbidden outside pinned exemptions`, - ); - } - if ( - ts.isElementAccessExpression(node) && - ts.isStringLiteralLike(node.argumentExpression) - ) { - assert.notEqual( - node.argumentExpression.text, - "constructor", - `${path} computed constructor access is forbidden`, - ); - } -} - -/** - * @param {ts.SourceFile} source - * @param {string} path - */ -function checkSourceRoots(source, path) { - const file = nodePath.relative(join(root, "src"), path).split(sep).join("/"); - const lines = source.text.split(/\r?\n/); - /** @type {ts.TypeChecker | undefined} */ - let checker; - for (const [location, text] of Object.entries(sourceExemptions)) { - if (!location.startsWith(`${file}:`)) continue; - const line = Number(location.slice(file.length + 1)); - assert.equal( - lines[line - 1], - text, - `${location} loader exemption moved or changed`, - ); - } - /** - * @param {ts.Identifier} node - * @returns {boolean | undefined} - */ - function localBinding(node) { - if (!checker) { - const options = { - noResolve: true, - noLib: true, - types: [], - allowJs: true, - }; - const host = ts.createCompilerHost(options); - host.getSourceFile = (name) => - nodePath.resolve(name) === path ? source : undefined; - checker = ts.createProgram([path], options, host).getTypeChecker(); - } - const symbol = ts.isShorthandPropertyAssignment(node.parent) - ? checker.getShorthandAssignmentValueSymbol(node.parent) - : checker.getSymbolAtLocation(node); - return symbol?.declarations?.some( - (declaration) => - declaration.getSourceFile() === source && - !( - ts.getCombinedModifierFlags(declaration) & ts.ModifierFlags.Ambient - ) && - (ts.isParameter(declaration) || - ts.isBindingElement(declaration) || - (ts.isVariableDeclaration(declaration) && - (declaration.initializer || - ts.isForOfStatement(declaration.parent.parent) || - ts.isForInStatement(declaration.parent.parent)) && - declaration.parent.flags & ts.NodeFlags.BlockScoped)), - ); - } - /** @param {ts.Identifier} node */ - function sourceRoot(node) { - if (!["module", "exports", "process"].includes(node.text)) return; - const parent = node.parent; - assert.ok( - !( - ts.isPropertyAccessExpression(parent) && - parent.name === node && - ts.isIdentifier(parent.expression) && - ["global", "globalThis"].includes(parent.expression.text) - ), - `${path} global loader-root access is forbidden`, - ); - if (memberName(node)) return; - if (["module", "exports"].includes(node.text) && localBinding(node)) return; - const dotted = - ts.isPropertyAccessExpression(parent) && - parent.expression === node && - !parent.questionDotToken; - if (node.text === "module") { - assert.ok( - dotted && parent.name.text === "exports", - `${path} module is allowed only as module.exports`, - ); - } else if (node.text === "exports") { - assert.ok( - dotted && - ts.isBinaryExpression(parent.parent) && - parent.parent.left === parent && - parent.parent.operatorToken.kind === ts.SyntaxKind.EqualsToken, - `${path} exports is allowed only in exports. assignments`, - ); - } else if (node.text === "process") { - assert.ok( - dotted && processMembers.has(parent.name.text), - `${path} process must use an allowlisted dotted member`, - ); - } - } - /** @param {ts.Node} node */ - function visit(node) { - if (ts.isIdentifier(node)) sourceRoot(node); - checkConstructor(node, path); - ts.forEachChild(node, visit); - } - visit(source); -} - -/** - * @param {ts.Identifier} node - * @param {ts.SourceFile} source - * @param {string} path - * @returns {boolean} - */ -function pinnedRequireResolve(node, source, path) { - const member = node.parent; - if ( - !ts.isPropertyAccessExpression(member) || - member.expression !== node || - member.name.text !== "resolve" - ) - return false; - const line = - source.getLineAndCharacterOfPosition(node.getStart(source)).line + 1; - const location = `${nodePath.relative(root, path).split(sep).join("/")}:${line}`; - assert.ok( - Object.hasOwn(requireResolveExemptions, location), - `${location} require.resolve is forbidden outside pinned exemptions`, - ); - assert.equal( - source.text.split(/\r?\n/)[line - 1], - requireResolveExemptions[location], - `${location} require.resolve exemption moved or changed`, - ); - usedRequireResolveExemptions.add(location); - return true; -} - -/** - * @param {string} name - * @returns {boolean} - */ -function sensitiveName(name) { - return [ - "require", - "createRequire", - "mainModule", - "eval", - "Function", - ].includes(name); -} -/** - * @param {ts.Node} node - * @returns {boolean} - */ -function computedReceiver(node) { - while ( - ts.isParenthesizedExpression(node) || - ts.isAsExpression(node) || - ts.isSatisfiesExpression(node) || - ts.isNonNullExpression(node) || - ts.isTypeAssertionExpression(node) - ) - node = node.expression; - return ( - ts.isIdentifier(node) && - ["module", "process", "globalThis", "global"].includes(node.text) - ); -} -/** - * @param {ts.Identifier} node - * @param {ts.SourceFile} source - * @param {string} path - * @returns {boolean} - */ -function pinnedLoader(node, source, path) { - const line = - source.getLineAndCharacterOfPosition(node.getStart(source)).line + 1; - const location = `${nodePath.relative(root, path).split(sep).join("/")}:${line}`; - if (!Object.hasOwn(loaderExemptions, location)) return false; - assert.equal( - source.text.split(/\r?\n/)[line - 1], - loaderExemptions[location], - `${location} loader exemption moved or changed`, - ); - return true; -} - -/** - * @param {ts.SourceFile} source - * @param {string} path - * @param {string} boundary - * @param {boolean} compiled - */ -function checkSource(source, path, boundary, compiled) { - /** - * @param {ts.Node | undefined} expression - * @param {boolean} [runtime] - * @param {boolean} [esm] - */ - function checkSpecifier( - expression, - runtime = false, - esm = !runtime && /\.(?:mjs|mts|js|jsx)$/.test(path), - ) { - const line = - source.getLineAndCharacterOfPosition(expression?.getStart(source) ?? 0) - .line + 1; - assert.ok( - expression && ts.isStringLiteral(expression), - `${path}:${line} imports must use a plain string literal specifier`, - ); - if (!compiled) checkBuiltin(expression, path); - checkTarget(expression.text, path, boundary, compiled, { runtime, esm }); - } - /** - * @param {ts.Node} node - * @returns {boolean} - */ - function metadataType(node) { - const value = ts.isArrayLiteralExpression(node.parent) ? node.parent : node; - const call = value.parent; - return ( - compiled && - ts.isCallExpression(call) && - ts.isIdentifier(call.expression) && - call.expression.text === "__metadata" && - call.arguments[1] === value && - ts.isStringLiteral(call.arguments[0]) && - ["design:type", "design:paramtypes", "design:returntype"].includes( - call.arguments[0].text, - ) - ); - } - /** @param {ts.Identifier} node */ - function checkIdentifier(node) { - if ( - node.text === "require" && - !pinnedRequireResolve(node, source, path) && - !pinnedLoader(node, source, path) - ) { - assert.ok( - ts.isCallExpression(node.parent) && - node.parent.expression === node && - !node.parent.questionDotToken, - `${path} indirect require is forbidden`, - ); - checkSpecifier(node.parent.arguments[0], true); - } - assert.ok( - !["createRequire", "mainModule", "eval", "Function"].includes( - node.text, - ) || - (node.text === "Function" && metadataType(node)) || - (node.text === "createRequire" && pinnedLoader(node, source, path)), - `${path} loader construction or dynamic evaluation is forbidden: ${node.text}`, - ); - } - /** @param {ts.Node} node */ - function visit(node) { - let specifier; - if (ts.isImportDeclaration(node) || ts.isExportDeclaration(node)) { - specifier = node.moduleSpecifier; - } else if (ts.isExternalModuleReference(node)) { - specifier = node.expression; - } else if (ts.isImportTypeNode(node)) { - checkSpecifier( - ts.isLiteralTypeNode(node.argument) - ? node.argument.literal - : node.argument, - ); - } else if ( - ts.isCallExpression(node) && - node.expression.kind === ts.SyntaxKind.ImportKeyword - ) { - const native = compiled || /\.(?:mjs|mts|cjs|js|jsx)$/.test(path); - checkSpecifier(node.arguments[0], native, native); - } - if (specifier) - checkSpecifier(specifier, ts.isExternalModuleReference(node)); - if (ts.isIdentifier(node)) checkIdentifier(node); - if (ts.isElementAccessExpression(node)) { - assert.ok( - !computedReceiver(node.expression) && - !( - ts.isStringLiteral(node.argumentExpression) && - sensitiveName(node.argumentExpression.text) - ), - `${path} computed loader access is forbidden`, - ); - } - if ( - (ts.isBindingElement(node) || - ts.isImportSpecifier(node) || - ts.isExportSpecifier(node)) && - node.propertyName - ) { - assert.ok( - !ts.isComputedPropertyName(node.propertyName) && - !sensitiveName(node.propertyName.text), - `${path} indirect loader binding is forbidden`, - ); - } - ts.forEachChild(node, visit); - } - visit(source); -} - -/** - * @param {string} directory - * @param {string} [boundary] - */ -function checkImports(directory, boundary = directory) { - for (const entry of readdirSync(directory, { withFileTypes: true })) { - const path = join(directory, entry.name); - assert.ok( - !entry.isSymbolicLink(), - `${path} backend dependency files must not use symbolic links`, - ); - if (entry.isDirectory()) { - checkImports(path, boundary); - continue; - } - /** @type {boolean} */ - const compiled = boundary === join(root, "dist"); - assert.ok( - compiled || entry.name !== "package.json", - `${path} backend source package manifests are forbidden`, - ); - assert.ok( - /\.(?:ts|tsx|cts|mts|js|jsx|cjs|mjs|json|map|tsbuildinfo)$/.test(path), - `${path} unsupported backend executable suffix; dependency-cruiser must parse every executable file`, - ); - if (/\.(?:json|map|tsbuildinfo)$/.test(entry.name)) { - JSON.parse(readFileSync(path, "utf8")); - continue; - } - const source = ts.createSourceFile( - path, - readFileSync(path, "utf8"), - ts.ScriptTarget.Latest, - true, - ); - checkTarget.checkCapabilities(source, path); - if (!compiled) checkSourceRoots(source, path); - checkSource(source, path, boundary, compiled); - } -} - -checkImports(join(root, "dist")); -checkImports(join(root, "src")); -assert.equal( - usedExemptions.size, - Object.keys(sourceExemptions).length, - "Every pinned loader exemption must still be used", -); -assert.equal( - usedRequireResolveExemptions.size, - Object.keys(requireResolveExemptions).length, - "Every pinned require.resolve exemption must still be used", -); process.stdout.write("Backend invariant checks passed.\n"); diff --git a/packages/dms/scripts/backend-exemptions.cjs b/packages/dms/scripts/backend-exemptions.cjs deleted file mode 100644 index db14e0a81..000000000 --- a/packages/dms/scripts/backend-exemptions.cjs +++ /dev/null @@ -1,72 +0,0 @@ -const constructorExemptions = { - "implementations/dms-base/search-route.ts:417": - " const entryInstance = model.constructor.fromDatabase(entry);", - "implementations/dms-base/table-view.ts:82": - " const stored = model.constructor.fromDatabase(entry);", - "implementations/dms-base/table-view.ts:217": - " const owner = controller.constructor;", - "implementations/dms-base/table-view.ts:422": - " const stored = model.constructor.fromDatabase(row);", - "test/unit/implementations/dms-base/table-view-shared-controller.test.ts:66": - " const meta = GetMetadata(instance.constructor, TableViewMeta);", - "test/unit/implementations/dms-base/table-view-shared-controller.test.ts:77": - " const meta = GetMetadata(instance.constructor, TableViewMeta);", -}; -const builtinExemptions = { - "implementations/dms-builder/engine/module-loader.ts:1": - 'import { createRequire } from "node:module";', - "test/unit/interfaces/dms-base/table-view-evaluation-order.fixture.ts:1": - 'import Module, { findPackageJSON } from "node:module";', -}; -const requireResolveExemptions = { - "src/implementations/dms-builder/engine/module-loader.ts:29": - " return require.resolve(specifier);", - "dist/implementations/dms-builder/engine/module-loader.js:22": - " return require.resolve(specifier);", - "src/store/compatibility.ts:56": - " const entryFolder = path.dirname(require.resolve(INTERFACE_ENTRY));", - "dist/store/compatibility.js:45": - " const entryFolder = node_path_1.default.dirname(require.resolve(INTERFACE_ENTRY));", -}; - -/** - * The builder engine loads modules its sources cannot name: the block catalog, - * the DataTypes and the catalog providers carry their specifiers as data, and - * a project's own files resolve packages from where they sit. One file holds - * every such load. - */ -const loaderExemptions = { - "src/implementations/dms-builder/engine/module-loader.ts:1": - 'import { createRequire } from "node:module";', - "src/implementations/dms-builder/engine/module-loader.ts:12": - " const loaded: unknown = require(specifier);", - "src/implementations/dms-builder/engine/module-loader.ts:18": - " return createRequire(file);", - "src/implementations/dms-builder/engine/module-loader.ts:34": - " return Object.entries(require.cache).map(([file, entry]) => {", - "dist/implementations/dms-builder/engine/module-loader.js:11": - " const loaded = require(specifier);", - "dist/implementations/dms-builder/engine/module-loader.js:15": - " return (0, node_module_1.createRequire)(file);", - "dist/implementations/dms-builder/engine/module-loader.js:25": - " return Object.entries(require.cache).map(([file, entry]) => {", - "src/test/unit/builder/helpers/module-cache.ts:8": - " Object.assign(require.cache, { [file]: { exports } });", - "src/test/unit/builder/helpers/module-cache.ts:13": - " delete require.cache[file];", - "dist/test/unit/builder/helpers/module-cache.js:6": - " Object.assign(require.cache, { [file]: { exports } });", - "dist/test/unit/builder/helpers/module-cache.js:9": - " delete require.cache[file];", - "src/test/integration/mcp-startup-modules.test.ts:6": - "const tsMorphFilesAtStart = Object.keys(require.cache).filter((file) =>", - "dist/test/integration/mcp-startup-modules.test.js:9": - "const tsMorphFilesAtStart = Object.keys(require.cache).filter((file) => file.includes(TS_MORPH_SEGMENT));", -}; - -module.exports = { - loaderExemptions, - constructorExemptions, - builtinExemptions, - requireResolveExemptions, -}; diff --git a/packages/dms/scripts/backend-import-target.cjs b/packages/dms/scripts/backend-import-target.cjs deleted file mode 100644 index 822e6310c..000000000 --- a/packages/dms/scripts/backend-import-target.cjs +++ /dev/null @@ -1,446 +0,0 @@ -const assert = require("node:assert/strict"); -const { execFileSync } = require("node:child_process"); -const { existsSync, realpathSync } = require("node:fs"); -const { createRequire, isBuiltin } = require("node:module"); -const nodePath = require("node:path"); -const { sep } = nodePath; -const { fileURLToPath, pathToFileURL } = require("node:url"); -const ts = require("typescript"); -const { optionalText, readJsonRecord } = require("./helpers/json-record.mjs"); -const { forbidden } = require("../../../.dependency-cruiser/backend.cjs"); - -const root = nodePath.resolve(__dirname, ".."); -const workspace = nodePath.resolve(root, "../.."); -const processPins = { - "src/utils/package-manager.ts:1": - 'import { execFile } from "node:child_process";', - "src/utils/package-manager.ts:3": 'import { promisify } from "node:util";', - "src/utils/package-manager.ts:5": - "const execFileAsync = promisify(execFile);", - "src/utils/package-manager.ts:59": - 'return platform() === "win32"\n ? execFileAsync(windowsCommandLine(manager, args), {\n ...options,\n shell: "cmd.exe",\n })\n : execFileAsync(manager, args, options);', - "src/implementations/dms-builder/engine/probe-process.ts:1": - 'import { spawn } from "node:child_process";', - "src/test/helpers/replica-process.ts:1": - 'import { type ChildProcess, spawn } from "node:child_process";', - "src/test/unit/interfaces/dms-base/table-view-evaluation-order.test.ts:1": - 'import { execFile } from "node:child_process";', - "src/test/unit/interfaces/dms-base/table-view-evaluation-order.test.ts:2": - 'import { resolve } from "node:path";', - "src/test/unit/interfaces/dms-base/table-view-evaluation-order.test.ts:3": - 'import { promisify } from "node:util";', - "src/test/unit/interfaces/dms-base/table-view-evaluation-order.test.ts:7": - 'const FIXTURE = resolve(__dirname, "table-view-evaluation-order.fixture.js");', - "src/test/unit/interfaces/dms-base/table-view-evaluation-order.test.ts:13": - "const runProcess = promisify(execFile);", - "src/test/unit/interfaces/dms-base/table-view-evaluation-order.test.ts:18": - "const { stdout } = await runProcess(process.execPath, [FIXTURE]);", - "src/test/unit/implementations/dms/permissions-standalone.test.ts:1": - 'import { execFile } from "node:child_process";', - "src/test/unit/implementations/dms/permissions-standalone.test.ts:2": - 'import { resolve } from "node:path";', - "src/test/unit/implementations/dms/permissions-standalone.test.ts:3": - 'import { promisify } from "node:util";', - "src/test/unit/implementations/dms/permissions-standalone.test.ts:7": - 'const FIXTURE = resolve(__dirname, "permissions-standalone.fixture.js");', - "src/test/unit/implementations/dms/permissions-standalone.test.ts:10": - "const runProcess = promisify(execFile);", - "src/test/unit/implementations/dms/permissions-standalone.test.ts:15": - "const { stdout } = await runProcess(process.execPath, [FIXTURE]);", - "src/implementations/dms-builder/engine/project-env.ts:1": - 'import { spawnSync } from "node:child_process";', - "dist/implementations/dms-builder/engine/project-env.js:15": - 'const node_child_process_1 = require("node:child_process");', - "dist/implementations/dms-builder/engine/project-env.js:64": - 'const checked = (0, node_child_process_1.spawnSync)("git", ["check-ignore", "-q", file], {\n cwd: directory,\n });', - "src/test/unit/builder/helpers/processes.ts:1": - 'import { spawnSync } from "node:child_process";', - "dist/test/unit/builder/helpers/processes.js:8": - 'const node_child_process_1 = require("node:child_process");', - "dist/test/unit/builder/helpers/processes.js:11": - '(0, node_child_process_1.spawnSync)("git", ["init", "-q"], { cwd: directory });', - "dist/test/unit/builder/helpers/processes.js:14": - 'const run = (0, node_child_process_1.spawnSync)(script, {\n cwd: directory,\n shell: true,\n encoding: "utf8",\n env: {\n ...process.env,\n PATH: `${binDirectory}${node_path_1.default.delimiter}${process.env.PATH}`,\n },\n });', - "src/test/unit/builder/helpers/database-servers.ts:1": - 'import { execFile, spawnSync } from "node:child_process";', - "src/test/unit/builder/helpers/database-servers.ts:5": - 'import { promisify } from "node:util";', - "src/test/unit/builder/helpers/database-servers.ts:8": - "const run = promisify(execFile);", - "dist/test/unit/builder/helpers/database-servers.js:16": - 'const node_child_process_1 = require("node:child_process");', - "dist/test/unit/builder/helpers/database-servers.js:20": - 'const node_util_1 = require("node:util");', - "dist/test/unit/builder/helpers/database-servers.js:22": - "const run = (0, node_util_1.promisify)(node_child_process_1.execFile);", - "dist/test/unit/builder/helpers/database-servers.js:27": - 'exports.dockerAvailable = (0, node_child_process_1.spawnSync)("docker", ["version", "--format", "{{.Server.Os}}"], {\n encoding: "utf8",\n}).stdout?.trim() === "linux";', - "dist/utils/package-manager.js:5": - 'const node_child_process_1 = require("node:child_process");', - "dist/utils/package-manager.js:7": - 'const node_util_1 = require("node:util");', - "dist/utils/package-manager.js:8": - "const execFileAsync = (0, node_util_1.promisify)(node_child_process_1.execFile);", - "dist/utils/package-manager.js:21": - 'return (0, node_os_1.platform)() === "win32"\n ? execFileAsync(windowsCommandLine(manager, args), {\n ...options,\n shell: "cmd.exe",\n })\n : execFileAsync(manager, args, options);', - "dist/implementations/dms-builder/engine/probe-process.js:7": - 'const node_child_process_1 = require("node:child_process");', - "dist/implementations/dms-builder/engine/probe-process.js:80": - 'const child = (0, node_child_process_1.spawn)(process.execPath, [CHILD], {\n env: childEnvironment(request),\n stdio: ["ignore", "pipe", "pipe"],\n windowsHide: true,\n });', - "dist/test/helpers/replica-process.js:4": - 'const node_child_process_1 = require("node:child_process");', - "dist/test/helpers/replica-process.js:114": - 'const child = (0, node_child_process_1.spawn)(process.execPath, [AJS, "project", "run", "--project", directory], {\n cwd: directory,\n env: { ...process.env, REPLICA_HOST_HOLD_MS: String(holdMs) },\n });', - "dist/test/unit/interfaces/dms-base/table-view-evaluation-order.test.js:3": - 'const node_child_process_1 = require("node:child_process");', - "dist/test/unit/interfaces/dms-base/table-view-evaluation-order.test.js:4": - 'const node_path_1 = require("node:path");', - "dist/test/unit/interfaces/dms-base/table-view-evaluation-order.test.js:5": - 'const node_util_1 = require("node:util");', - "dist/test/unit/interfaces/dms-base/table-view-evaluation-order.test.js:8": - 'const FIXTURE = (0, node_path_1.resolve)(__dirname, "table-view-evaluation-order.fixture.js");', - "dist/test/unit/interfaces/dms-base/table-view-evaluation-order.test.js:12": - "const runProcess = (0, node_util_1.promisify)(node_child_process_1.execFile);", - "dist/test/unit/interfaces/dms-base/table-view-evaluation-order.test.js:16": - "const { stdout } = await runProcess(process.execPath, [FIXTURE]);", - "dist/test/unit/implementations/dms/permissions-standalone.test.js:3": - 'const node_child_process_1 = require("node:child_process");', - "dist/test/unit/implementations/dms/permissions-standalone.test.js:4": - 'const node_path_1 = require("node:path");', - "dist/test/unit/implementations/dms/permissions-standalone.test.js:5": - 'const node_util_1 = require("node:util");', - "dist/test/unit/implementations/dms/permissions-standalone.test.js:8": - 'const FIXTURE = (0, node_path_1.resolve)(__dirname, "permissions-standalone.fixture.js");', - "dist/test/unit/implementations/dms/permissions-standalone.test.js:10": - "const runProcess = (0, node_util_1.promisify)(node_child_process_1.execFile);", - "dist/test/unit/implementations/dms/permissions-standalone.test.js:14": - "const { stdout } = await runProcess(process.execPath, [FIXTURE]);", - "src/test/helpers/mcp.ts:1": - 'import { execFileSync } from "node:child_process";', - "src/test/helpers/mcp.ts:29": - 'const listing = execFileSync("icacls", [path], { encoding: "utf8" });', - "dist/test/helpers/mcp.js:7": - 'const node_child_process_1 = require("node:child_process");', - "dist/test/helpers/mcp.js:22": - 'const listing = (0, node_child_process_1.execFileSync)("icacls", [path], { encoding: "utf8" });', - "src/utils/owner-only-file.ts:1": - 'import { execFile } from "node:child_process";', - "src/utils/owner-only-file.ts:5": 'import { promisify } from "node:util";', - "src/utils/owner-only-file.ts:7": - "const execFileAsync = promisify(execFile);", - "src/utils/owner-only-file.ts:55": - 'await execFileAsync(\n powershell,\n ["-NoProfile", "-NonInteractive", "-Command", script],\n {\n env: { ...process.env, [TARGET_ENV]: target },\n timeout: POWERSHELL_TIMEOUT_MS,\n windowsHide: true,\n },\n );', - "dist/utils/owner-only-file.js:4": - 'const node_child_process_1 = require("node:child_process");', - "dist/utils/owner-only-file.js:8": - 'const node_util_1 = require("node:util");', - "dist/utils/owner-only-file.js:9": - "const execFileAsync = (0, node_util_1.promisify)(node_child_process_1.execFile);", - "dist/utils/owner-only-file.js:35": - 'await execFileAsync(powershell, ["-NoProfile", "-NonInteractive", "-Command", script], {\n env: { ...process.env, [TARGET_ENV]: target },\n timeout: POWERSHELL_TIMEOUT_MS,\n windowsHide: true,\n });', -}; - -/** - * @param {ts.SourceFile} source - * @param {string} path - */ -function checkCapabilities(source, path) { - const file = nodePath.relative(root, path).split(sep).join("/"); - const pins = Object.keys(processPins).filter((key) => - key.startsWith(`${file}:`), - ); - const used = new Set(); - const names = new Set([ - "execFile", - "execFileSync", - "execFileAsync", - "runProcess", - "FIXTURE", - "node_child_process_1", - "promisify", - "node_util_1", - ]); - if ( - file.includes("table-view-evaluation-order.test.") || - file.includes("permissions-standalone.test.") - ) { - names.add("resolve"); - names.add("node_path_1"); - } - /** @param {ts.Node} node */ - function visit(node) { - if (ts.isIdentifier(node) || ts.isStringLiteral(node)) { - assert.notEqual( - node.text, - "NODE_OPTIONS", - `${path} NODE_OPTIONS loader configuration is forbidden`, - ); - if ( - pins.length && - (names.has(node.text) || - ["child_process", "node:child_process"].includes(node.text)) - ) { - let statement = node; - while ( - statement.parent && - !ts.isSourceFile(statement.parent) && - !ts.isBlock(statement.parent) - ) - statement = statement.parent; - const line = - source.getLineAndCharacterOfPosition(statement.getStart(source)) - .line + 1; - const key = `${file}:${line}`; - assert.equal( - statement.getText(source), - processPins[key], - `${key} process instrumentation moved, changed or added`, - ); - used.add(key); - } - } - ts.forEachChild(node, visit); - } - visit(source); - assert.equal( - used.size, - pins.length, - `${path} every process instrumentation pin must remain used`, - ); -} - -/** - * @param {string} value - * @param {string} importer - * @param {boolean} esm - * @returns {string} - */ -function resolveRuntime(value, importer, esm) { - if (!esm) return createRequire(importer).resolve(value); - const url = execFileSync( - process.execPath, - [ - "--experimental-import-meta-resolve", - "--input-type=module", - "-e", - "process.stdout.write(import.meta.resolve(process.argv[1], process.argv[2]))", - value, - pathToFileURL(importer).href, - ], - { encoding: "utf8", stdio: ["ignore", "pipe", "pipe"] }, - ); - return fileURLToPath(url); -} - -/** - * @param {string} path - * @returns {string} - */ -function policyPath(path) { - const file = nodePath - .relative(workspace, realpathSync(path)) - .split(sep) - .join("/"); - return file.startsWith("packages/dms/dist/") - ? file.replace("/dist/", "/src/").replace(/\.(c|m)?js$/, ".$1ts") - : file; -} - -/** - * @param {string | string[]} pattern - * @param {string} path - * @returns {boolean} - */ -function matches(pattern, path) { - return (Array.isArray(pattern) ? pattern : [pattern]).some((value) => - new RegExp(value).test(path), - ); -} - -/** - * @param {{ path?: string | string[], pathNot?: string | string[] }} selector - * @param {string} path - * @returns {boolean} - */ -function applies(selector, path) { - return ( - (!selector.path || matches(selector.path, path)) && - (!selector.pathNot || !matches(selector.pathNot, path)) - ); -} - -/** - * @param {string} importer - * @param {string} target - */ -function checkEdge(importer, target) { - const from = policyPath(importer); - const to = policyPath(target); - for (const rule of forbidden) { - assert.ok( - !applies(rule.from, from) || !applies(rule.to, to), - `${importer} -> ${target} violates ${rule.name}`, - ); - } -} - -/** - * @param {string} path - * @returns {{ directory: string, name: string }} - */ -function owner(path) { - for ( - let directory = nodePath.dirname(path); - ; - directory = nodePath.dirname(directory) - ) { - const manifest = nodePath.join(directory, "package.json"); - if (existsSync(manifest)) { - const name = optionalText(readJsonRecord(manifest), "name"); - if (name) return { directory, name }; - } - assert.notEqual( - directory, - nodePath.dirname(directory), - `${path} has no package owner`, - ); - } -} - -/** - * @param {string} value - * @param {string} importer - * @param {boolean} runtime - * @param {boolean} esm - */ -function checkPackage(value, importer, runtime, esm) { - assert.ok( - /^(?:node:[\w/-]+|(?:@[\w.-]+\/)?[\w-][\w.-]*(?:\/[\w.-]+)*)$/.test( - value, - ) && - !value.split("/").some((part) => part === "." || part === "..") && - !/^@antelopejs\/dms(?:\/|$)/.test(value), - `${importer} imports must use another package specifier: ${value}`, - ); - assert.ok( - ![ - "process", - "node:process", - "worker_threads", - "node:worker_threads", - ].includes(value) && - (!["child_process", "node:child_process"].includes(value) || - Object.keys(processPins).some((key) => - key.startsWith( - `${nodePath.relative(root, importer).split(sep).join("/")}:`, - ), - )), - `${importer} builtin loader imports are forbidden: ${value}`, - ); - if (isBuiltin(value)) return; - let target; - try { - target = resolveRuntime(value, importer, esm); - } catch (error) { - assert.ok( - !runtime, - `${importer} cannot resolve runtime package ${value}: ${error.message}`, - ); - target = ts.resolveModuleName( - value, - importer, - { - moduleResolution: ts.ModuleResolutionKind.Bundler, - resolveJsonModule: true, - }, - ts.sys, - ).resolvedModule?.resolvedFileName; - } - assert.ok(target, `${importer} cannot resolve package ${value}`); - target = realpathSync(target); - const requested = value.startsWith("@") - ? value.split("/").slice(0, 2).join("/") - : value.split("/")[0]; - const actual = owner(target); - assert.equal( - requested, - actual.name, - `${importer} package ${value} resolves to another package owner: ${target}`, - ); - if (target.startsWith(nodePath.join(workspace, "packages") + sep)) { - assert.equal( - realpathSync( - resolveRuntime( - value, - nodePath.join(actual.directory, "package.json"), - esm, - ), - ), - target, - `${importer} in-repo targets must use their own public package specifier: ${value}`, - ); - } - checkEdge(importer, target); -} - -/** - * @param {string} value - * @param {string} path - * @param {string} boundary - * @param {boolean} compiled - * @param {{ runtime: boolean, esm: boolean }} flags - */ -module.exports = function checkTarget( - value, - path, - boundary, - compiled, - { runtime, esm }, -) { - if (!value.startsWith(".")) { - checkPackage(value, path, compiled || runtime || esm, esm); - return; - } - let target; - let importer = path; - if (!compiled && runtime && /\.[cm]?tsx?$/.test(path)) { - assert.ok( - (nodePath.resolve(nodePath.dirname(path), value) + sep).startsWith( - boundary + sep, - ), - `${path} relative imports must resolve inside backend src: ${value}`, - ); - importer = nodePath - .join(root, "dist", nodePath.relative(nodePath.join(root, "src"), path)) - .replace(/\.(c|m)?tsx?$/, ".$1js"); - boundary = nodePath.join(root, "dist"); - } - if (compiled || runtime) { - try { - target = resolveRuntime(value, importer, esm); - if (/\.d\.[cm]?ts$/.test(target)) target = undefined; - } catch { - target = undefined; - } - } else { - target = ts.resolveModuleName( - value, - path, - { - moduleResolution: ts.ModuleResolutionKind.Bundler, - allowJs: true, - resolveJsonModule: true, - }, - ts.sys, - ).resolvedModule?.resolvedFileName; - } - assert.ok( - target && - (realpathSync(target) + sep).startsWith(realpathSync(boundary) + sep), - `${path} relative imports must resolve inside backend ${nodePath.basename(boundary)}; use package exports for other packages: ${value}`, - ); - checkEdge(path, target); -}; - -module.exports.checkCapabilities = checkCapabilities; -module.exports.checkEdge = checkEdge; diff --git a/packages/dms/scripts/backend-runtime-boundaries.cjs b/packages/dms/scripts/backend-runtime-boundaries.cjs deleted file mode 100644 index a42142bcc..000000000 --- a/packages/dms/scripts/backend-runtime-boundaries.cjs +++ /dev/null @@ -1,27 +0,0 @@ -const { registerHooks } = require("node:module"); -const { fileURLToPath } = require("node:url"); -const path = require("node:path"); -const { sep } = path; -const { checkEdge } = require("./backend-import-target.cjs"); - -const root = path.resolve(__dirname, ".."); -const boundaries = [ - path.join(root, "src") + sep, - path.join(root, "dist") + sep, -]; - -registerHooks({ - resolve(specifier, context, nextResolve) { - const result = nextResolve(specifier, context); - if ( - context.parentURL?.startsWith("file:") && - result.url.startsWith("file:") - ) { - const importer = fileURLToPath(context.parentURL); - if (boundaries.some((boundary) => importer.startsWith(boundary))) { - checkEdge(importer, fileURLToPath(result.url)); - } - } - return result; - }, -}); From 622a7072ad4d1d6ce9896e62d669d0323bcabfab Mon Sep 17 00:00:00 2001 From: Antony Rizzitelli Date: Sun, 15 Nov 2026 17:29:19 +0000 Subject: [PATCH 4/7] refactor(dms): remove the playground import scanner --- .../dms/playground/checks/aliases-context.cjs | 57 -- .../dms/playground/checks/aliases-permit.cjs | 175 ------- .../dms/playground/checks/aliases-scripts.cjs | 101 ---- .../playground/checks/aliases-specifiers.cjs | 152 ------ .../dms/playground/checks/aliases-vue.cjs | 127 ----- packages/dms/playground/checks/aliases.cjs | 84 --- packages/dms/playground/checks/fixtures.mjs | 118 ----- .../dms/playground/checks/fixtures.test.mjs | 20 - .../dms/playground/checks/frontend-tools.cjs | 81 --- .../dms/playground/checks/imports.test.mjs | 495 ------------------ .../dms/playground/checks/loaded-values.mjs | 48 -- .../dms/playground/checks/manifest.test.mjs | 69 --- .../dms/playground/checks/raw-sources.cjs | 24 +- .../dms/playground/checks/styles.test.mjs | 78 --- packages/dms/playground/checks/syntax.cjs | 142 ----- packages/dms/playground/package.json | 2 +- 16 files changed, 15 insertions(+), 1758 deletions(-) delete mode 100644 packages/dms/playground/checks/aliases-context.cjs delete mode 100644 packages/dms/playground/checks/aliases-permit.cjs delete mode 100644 packages/dms/playground/checks/aliases-scripts.cjs delete mode 100644 packages/dms/playground/checks/aliases-specifiers.cjs delete mode 100644 packages/dms/playground/checks/aliases-vue.cjs delete mode 100644 packages/dms/playground/checks/aliases.cjs delete mode 100644 packages/dms/playground/checks/fixtures.mjs delete mode 100644 packages/dms/playground/checks/fixtures.test.mjs delete mode 100644 packages/dms/playground/checks/frontend-tools.cjs delete mode 100644 packages/dms/playground/checks/imports.test.mjs delete mode 100644 packages/dms/playground/checks/loaded-values.mjs delete mode 100644 packages/dms/playground/checks/manifest.test.mjs delete mode 100644 packages/dms/playground/checks/styles.test.mjs delete mode 100644 packages/dms/playground/checks/syntax.cjs diff --git a/packages/dms/playground/checks/aliases-context.cjs b/packages/dms/playground/checks/aliases-context.cjs deleted file mode 100644 index e87652964..000000000 --- a/packages/dms/playground/checks/aliases-context.cjs +++ /dev/null @@ -1,57 +0,0 @@ -const { realpathSync } = require("node:fs"); -const nodePath = require("node:path"); -const ts = require("typescript"); - -const root = nodePath.resolve(__dirname, ".."); -const repository = nodePath.resolve(root, "../../.."); -const config = ts.readConfigFile( - nodePath.join(repository, ".dependency-cruiser.tsconfig.json"), - (path) => ts.sys.readFile(path), -); -const { options } = ts.parseJsonConfigFileContent( - config.config, - ts.sys, - repository, -); -const cache = ts.createModuleResolutionCache( - repository, - (path) => path, - options, -); -const server = realpathSync(nodePath.join(root, "src")); -const client = realpathSync(nodePath.join(root, "frontend-vue")); -const layers = ["core", "ui", "layout", "auth", "onboarding"].map((layer) => - realpathSync(nodePath.join(root, `../frontend-vue/layers/dms-${layer}/app`)), -); -const skippedDirectories = new Set([ - "node_modules", - "dist", - ".nuxt", - ".output", -]); -const codeExtensions = new Set([ - ".ts", - ".tsx", - ".mts", - ".cts", - ".js", - ".jsx", - ".mjs", - ".cjs", - ".vue", -]); -/** @type {string[]} */ -const failures = []; - -module.exports = { - root, - repository, - options, - cache, - server, - client, - layers, - skippedDirectories, - codeExtensions, - failures, -}; diff --git a/packages/dms/playground/checks/aliases-permit.cjs b/packages/dms/playground/checks/aliases-permit.cjs deleted file mode 100644 index 2f2774688..000000000 --- a/packages/dms/playground/checks/aliases-permit.cjs +++ /dev/null @@ -1,175 +0,0 @@ -const { realpathSync } = require("node:fs"); -const { isBuiltin } = require("node:module"); -const nodePath = require("node:path"); -const { sep } = nodePath; -const ts = require("typescript"); -const { - repository, - options, - cache, - server, - client, - layers, - skippedDirectories, - codeExtensions, -} = require("./aliases-context.cjs"); - -const rankRules = require("../../../../.dependency-cruiser/playground.cjs") - .forbidden.filter((rule) => rule.name.endsWith("-no-upward")) - .map((rule) => ({ - from: new RegExp(rule.from.path), - to: new RegExp(rule.to.path), - })); -const loaderLibraries = new Set([ - "module", - "vm", - "worker_threads", - "child_process", - "process", -]); -const nonExecutableKinds = new Map([ - [".json", "json"], - ["?raw", "vite"], - ["?url", "vite"], -]); - -/** - * @param {string} path - * @param {string} area - * @returns {boolean} - */ -function inside(path, area) { - const suffix = nodePath.relative(area, path); - return ( - suffix !== ".." && - !suffix.startsWith(`..${sep}`) && - !nodePath.isAbsolute(suffix) - ); -} - -/** - * @param {string} specifier - * @param {string} importer - * @returns {string | undefined} - */ -function fallbackTarget(specifier, importer) { - if (specifier.startsWith(".") || nodePath.isAbsolute(specifier)) { - const target = nodePath.resolve(nodePath.dirname(importer), specifier); - return ts.sys.fileExists(target) ? target : undefined; - } - for (const [pattern, targets] of Object.entries(options.paths)) { - const [prefix, suffix = ""] = pattern.split("*"); - if (!specifier.startsWith(prefix) || !specifier.endsWith(suffix)) continue; - const match = specifier.slice( - prefix.length, - specifier.length - suffix.length, - ); - for (const target of targets) { - const path = nodePath.resolve( - options.baseUrl, - target.replace("*", match), - ); - if (ts.sys.fileExists(path)) return path; - } - } - return undefined; -} - -/** - * @param {string} importer - * @param {string} target - * @param {string | undefined} query - * @param {boolean} viteAssets - * @returns {boolean} - */ -function permittedEdge(importer, target, query, viteAssets) { - if ([server, client, ...layers].some((area) => inside(target, area))) { - const extension = nodePath.extname(target); - const inert = nonExecutableKinds.get(query ?? extension); - if ( - target.split(sep).some((part) => skippedDirectories.has(part)) || - extension === "" || - !( - (query === undefined && codeExtensions.has(extension)) || - inert === "json" || - (inert === "vite" && inside(importer, client) && viteAssets) - ) - ) - return false; - } - const from = nodePath.relative(repository, importer).split(sep).join("/"); - const to = nodePath.relative(repository, target).split(sep).join("/"); - if (rankRules.some((rule) => rule.from.test(from) && rule.to.test(to))) - return false; - return ( - inside(importer, client) || - !/(?:^|\/)(?:frontend-vue|@antelopejs\/dms-frontend(?:-vue)?)(?:\/|$)/.test( - to, - ) - ); -} - -/** - * @param {string} specifier - * @param {string} importer - * @param {boolean} viteAssets - * @returns {boolean} - */ -function permitted(specifier, importer, viteAssets) { - importer = realpathSync(importer); - const isClient = inside(importer, client); - const [source, ...queryParts] = specifier.replaceAll("\\", "/").split("?"); - let request = source; - const query = queryParts.length ? `?${queryParts.join("?")}` : undefined; - if (loaderLibraries.has(request.replace(/^node:/, ""))) return false; - if (!isClient && /^@antelopejs\/dms-frontend(?:-vue)?(?:\/|$)/.test(request)) - return false; - if (request === "#dms/frontend-module" && isClient) { - request = "@antelopejs/dms-frontend/templates/vue/frontend-module.ts"; - } - if (isBuiltin(request)) return query === undefined; - const resolved = ts.resolveModuleName( - request, - importer, - options, - ts.sys, - cache, - ).resolvedModule; - const segments = request.split("/"); - const packageSpecifier = /^(?:@[\w.-]+\/)?[\w-][\w.-]*(?:\/[\w.-]+)*$/.test( - request, - ); - const target = - fallbackTarget(request, importer) ?? resolved?.resolvedFileName; - if (!target) return false; - const canonical = realpathSync(target); - if ( - ![target, canonical].every((path) => - permittedEdge(importer, path, query, viteAssets), - ) - ) - return false; - const skipped = [target, canonical].some((path) => - path.split(sep).some((part) => skippedDirectories.has(part)), - ); - if ( - packageSpecifier && - !segments.some((part) => [".", "..", "node_modules"].includes(part)) && - resolved?.isExternalLibraryImport - ) { - return query === undefined; - } - if (skipped) return false; - return ( - inside(canonical, isClient ? client : server) || - (isClient && - layers.some( - (area) => - request.startsWith( - `#${nodePath.basename(nodePath.dirname(area))}/app/`, - ) && inside(canonical, area), - )) - ); -} - -module.exports = { fallbackTarget, permitted }; diff --git a/packages/dms/playground/checks/aliases-scripts.cjs b/packages/dms/playground/checks/aliases-scripts.cjs deleted file mode 100644 index 5713310b1..000000000 --- a/packages/dms/playground/checks/aliases-scripts.cjs +++ /dev/null @@ -1,101 +0,0 @@ -const ts = require("typescript"); -const { - unwrap, - constantValue, - importMeta, - propertyName, - loaderReference, -} = require("./syntax.cjs"); -const { failures } = require("./aliases-context.cjs"); -const { checkSpecifier, checkGlob } = require("./aliases-specifiers.cjs"); - -const globalMethods = new Map([ - ["window", new Set(["addEventListener", "removeEventListener"])], - ["process", new Set(["uptime"])], -]); - -/** - * @param {ts.CallExpression} node - * @param {string} path - * @param {WeakSet} allowed - */ -function checkCall(node, path, allowed) { - const callee = unwrap(node.expression); - if ( - (ts.isPropertyAccessExpression(callee) || - ts.isElementAccessExpression(callee)) && - importMeta(unwrap(callee.expression)) && - propertyName(callee) === "glob" - ) { - allowed.add(unwrap(callee.expression)); - if (!checkGlob(node, path)) - failures.push(`${path}: unsupported playground glob`); - } else if ( - ts.isIdentifier(node.expression) && - node.expression.text === "require" && - !node.questionDotToken && - node.arguments.length === 1 - ) { - allowed.add(node.expression); - checkSpecifier(constantValue(node.arguments[0]), path, false); - } else if (node.expression.kind === ts.SyntaxKind.ImportKeyword) { - allowed.add(node.expression); - checkSpecifier( - node.arguments[0] ? constantValue(node.arguments[0]) : undefined, - path, - ); - } else if ( - ts.isPropertyAccessExpression(callee) && - ts.isIdentifier(callee.expression) && - globalMethods.get(callee.expression.text)?.has(callee.name.text) - ) { - allowed.add(callee.expression); - } -} - -/** - * @param {string} text - * @param {string} path - * @param {ts.ScriptKind} [kind] - */ -function checkScript(text, path, kind = ts.getScriptKindFromFileName(path)) { - const source = ts.createSourceFile( - path, - text, - ts.ScriptTarget.Latest, - true, - kind, - ); - if (source.parseDiagnostics.length) { - for (const diagnostic of source.parseDiagnostics) - failures.push( - `${path}: invalid playground script: ${ts.flattenDiagnosticMessageText(diagnostic.messageText, " ")}`, - ); - return; - } - const allowed = new WeakSet(); - /** @param {ts.Node} node */ - function visit(node) { - if (ts.isCallExpression(node)) checkCall(node, path, allowed); - if ( - (ts.isImportDeclaration(node) || ts.isExportDeclaration(node)) && - node.moduleSpecifier - ) { - checkSpecifier(constantValue(node.moduleSpecifier), path); - } else if (ts.isExternalModuleReference(node) && node.expression) { - checkSpecifier(constantValue(node.expression), path, false); - } else if ( - ts.isImportTypeNode(node) && - ts.isLiteralTypeNode(node.argument) - ) { - checkSpecifier(constantValue(node.argument.literal), path); - } - const unsupported = !allowed.has(node) && loaderReference(node); - if (unsupported) - failures.push(`${path}: unsupported playground loader ${unsupported}`); - ts.forEachChild(node, visit); - } - visit(source); -} - -module.exports = { checkScript }; diff --git a/packages/dms/playground/checks/aliases-specifiers.cjs b/packages/dms/playground/checks/aliases-specifiers.cjs deleted file mode 100644 index a47ebfcd6..000000000 --- a/packages/dms/playground/checks/aliases-specifiers.cjs +++ /dev/null @@ -1,152 +0,0 @@ -const nodePath = require("node:path"); -const { sep } = nodePath; -const ts = require("typescript"); -const { z } = require("zod"); -const { unwrap, constantValue } = require("./syntax.cjs"); -const { - glob: { globSync }, -} = require("./frontend-tools.cjs"); -const { options, failures } = require("./aliases-context.cjs"); -const { permitted } = require("./aliases-permit.cjs"); - -const specifierSchema = z.string(); - -/** @param {string} path */ -const globPath = (path) => path.split(sep).join("/"); -const globOptions = z - .object({ - query: z.literal("?raw").optional(), - import: z.literal("default").optional(), - eager: z.boolean().optional(), - }) - .strict(); - -/** - * @param {unknown} value - * @param {string} path - * @param {boolean} [viteAssets] - */ -function checkSpecifier(value, path, viteAssets = true) { - const specifier = specifierSchema.safeParse(value); - if (!specifier.success) { - failures.push( - `${path}: playground import must be a static constant string`, - ); - } else if ( - specifier.data.includes("#", 1) || - specifier.data.includes("%") || - specifier.data.split("?")[0].toLowerCase().endsWith(".css") || - !permitted(specifier.data, path, viteAssets) - ) { - failures.push(`${path}: forbidden playground import ${specifier.data}`); - } -} - -/** - * @param {string} pattern - * @param {string} importer - * @returns {{ pattern: string, request: (file: string) => string } | undefined} - */ -function globPattern(pattern, importer) { - if (pattern.startsWith("./") || pattern.startsWith("../")) { - return { - pattern: globPath(nodePath.resolve(nodePath.dirname(importer), pattern)), - request: (file) => - `./${nodePath.relative(nodePath.dirname(importer), file).split(sep).join("/")}`, - }; - } - if (!/^#dms-(?:core|ui|layout|auth|onboarding)\/app\//.test(pattern)) - return undefined; - for (const [alias, targets] of Object.entries(options.paths)) { - const prefix = alias.replace(/\*$/, ""); - if (!pattern.startsWith(prefix)) continue; - const directory = nodePath.resolve( - options.baseUrl, - targets[0].replace(/\*$/, ""), - ); - return { - pattern: globPath( - nodePath.resolve(directory, pattern.slice(prefix.length)), - ), - request: (file) => - prefix + nodePath.relative(directory, file).split(sep).join("/"), - }; - } - return undefined; -} - -/** - * @param {ts.Expression | undefined} argument - * @returns {z.infer | false | undefined} - */ -function permittedGlobSettings(argument) { - const settings = argument && unwrap(argument); - const values = {}; - if (settings) { - if (!ts.isObjectLiteralExpression(settings)) return false; - for (const property of settings.properties) { - if (!ts.isPropertyAssignment(property)) return false; - const key = ts.isIdentifier(property.name) - ? property.name.text - : constantValue(property.name); - const value = constantValue(property.initializer); - if (key === undefined || value === undefined) return false; - values[key] = value; - } - } - const parsed = globOptions.safeParse(values); - return parsed.success ? parsed.data : undefined; -} - -/** - * @param {ts.CallExpression} node - * @param {string} importer - * @returns {boolean} - */ -function checkGlob(node, importer) { - const argument = node.arguments[0] && unwrap(node.arguments[0]); - const expressions = - argument && ts.isArrayLiteralExpression(argument) - ? argument.elements - : [argument]; - const parsed = z - .array(z.string()) - .safeParse( - expressions.map((expression) => expression && constantValue(expression)), - ); - const settings = permittedGlobSettings(node.arguments[1]); - if (!parsed.success || !settings || node.arguments.length > 2) return false; - /** @type {NonNullable>[]} */ - const positives = []; - /** @type {NonNullable>[]} */ - const negatives = []; - for (const pattern of parsed.data) { - const negative = pattern.startsWith("!"); - const resolved = globPattern( - negative ? pattern.slice(1) : pattern, - importer, - ); - if (!resolved) return false; - (negative ? negatives : positives).push(resolved); - } - for (const positive of positives) { - for (const file of globSync(positive.pattern, { - absolute: true, - dot: false, - expandDirectories: false, - ignore: [ - "**/node_modules/**", - ...negatives.map((negative) => negative.pattern), - ], - })) { - if (nodePath.resolve(file) !== importer) - checkSpecifier( - positive.request(file) + (settings.query ?? ""), - importer, - ); - } - } - return true; -} - -module.exports = { checkSpecifier, checkGlob }; diff --git a/packages/dms/playground/checks/aliases-vue.cjs b/packages/dms/playground/checks/aliases-vue.cjs deleted file mode 100644 index 49abe6f08..000000000 --- a/packages/dms/playground/checks/aliases-vue.cjs +++ /dev/null @@ -1,127 +0,0 @@ -const { readFileSync } = require("node:fs"); -const ts = require("typescript"); -const { - vue: { parse, compileTemplate }, - css, -} = require("./frontend-tools.cjs"); -const { failures } = require("./aliases-context.cjs"); -const { fallbackTarget, permitted } = require("./aliases-permit.cjs"); -const { checkSpecifier } = require("./aliases-specifiers.cjs"); -const { checkScript } = require("./aliases-scripts.cjs"); - -const styleNodes = new Set([ - "StyleSheet", - "Rule", - "SelectorList", - "Selector", - "ClassSelector", - "Block", - "Declaration", - "Value", - "Identifier", - "Dimension", - "Function", - "Number", - "Combinator", - "Operator", - "Percentage", - "PseudoClassSelector", - "Hash", -]); -const styleFunctions = new Set([ - "var", - "color-mix", - "radial-gradient", - "linear-gradient", -]); -const vueScriptKinds = new Map([ - ["js", ts.ScriptKind.JS], - ["jsx", ts.ScriptKind.JSX], - ["ts", ts.ScriptKind.TS], - ["tsx", ts.ScriptKind.TSX], -]); - -/** - * @param {import("vue/compiler-sfc").SFCStyleBlock} block - * @param {string} path - */ -function checkStyle(block, path) { - if (block.src !== undefined) { - failures.push(`${path}: external playground styles are forbidden`); - return; - } - try { - if ((block.lang ?? "css") !== "css" || block.content.includes("\\")) - throw new Error("unsupported style language or escape"); - const ast = css.parse(block.content, { - parseCustomProperty: true, - onParseError(error) { - throw error; - }, - }); - css.walk(ast, (node) => { - if ( - !styleNodes.has(node.type) || - (node.type === "Function" && !styleFunctions.has(node.name)) || - (node.type === "PseudoClassSelector" && node.name !== "hover") - ) - throw new Error(`unsupported style node ${node.type}`); - }); - } catch { - failures.push(`${path}: unsupported playground inline style`); - } -} - -/** @param {string} path */ -function checkVue(path) { - const { descriptor, errors } = parse(readFileSync(path, "utf8"), { - filename: path, - }); - if (errors.length) throw new Error(`${path}: ${errors.join("; ")}`); - for (const block of descriptor.customBlocks) - failures.push( - `${path}: playground custom block ${block.type} is forbidden`, - ); - for (const block of descriptor.styles) checkStyle(block, path); - for (const block of [descriptor.script, descriptor.scriptSetup]) { - if (!block) continue; - if (block.src !== undefined) { - failures.push(`${path}: external playground scripts are forbidden`); - continue; - } - const kind = vueScriptKinds.get(block.lang ?? "js"); - if (kind === undefined) { - failures.push( - `${path}: unsupported playground script language ${block.lang}`, - ); - continue; - } - checkScript(block.content, path, kind); - } - const template = descriptor.template; - if (!template) return; - let filename = path; - let source = template.content; - if (template.src !== undefined) { - checkSpecifier(template.src, path, false); - if (!permitted(template.src, path, false)) return; - filename = fallbackTarget(template.src, path); - if (!filename) { - failures.push(`${path}: unsupported playground template source`); - return; - } - source = readFileSync(filename, "utf8"); - } - const compiled = compileTemplate({ - source, - filename, - id: path, - preprocessLang: template.lang, - compilerOptions: { expressionPlugins: ["typescript"] }, - }); - if (compiled.errors.length) - throw new Error(`${path}: ${compiled.errors.join("; ")}`); - checkScript(compiled.code, filename, ts.ScriptKind.TS); -} - -module.exports = { checkVue }; diff --git a/packages/dms/playground/checks/aliases.cjs b/packages/dms/playground/checks/aliases.cjs deleted file mode 100644 index ae365dadb..000000000 --- a/packages/dms/playground/checks/aliases.cjs +++ /dev/null @@ -1,84 +0,0 @@ -const { readdirSync, readFileSync } = require("node:fs"); -const nodePath = require("node:path"); -const { z } = require("zod"); -const { - root, - client, - skippedDirectories, - codeExtensions, - failures, -} = require("./aliases-context.cjs"); -const { checkScript } = require("./aliases-scripts.cjs"); -const { checkVue } = require("./aliases-vue.cjs"); - -const trustedManifests = new Map([ - [ - nodePath.join(root, "package.json"), - new Set([ - "name", - "version", - "private", - "main", - "dmsFrontend", - "packageManager", - "scripts", - "devDependencies", - "antelopeJs", - "dmsFrontend", - "dependencies", - ]), - ], - [ - nodePath.join(client, "package.json"), - new Set(["name", "version", "private", "type", "main", "engines"]), - ], -]); - -/** @param {string} path */ -function checkManifest(path) { - const fields = trustedManifests.get(path); - if (!fields) { - failures.push( - `${path}: additional playground package metadata is forbidden`, - ); - return; - } - const manifest = z.unknown().parse(JSON.parse(readFileSync(path, "utf8"))); - if (manifest === null || manifest === undefined) - throw new TypeError(`${path}: playground package manifest is null`); - for (const field of Object.keys(manifest)) - if (!fields.has(field)) - failures.push(`${path}: unsupported playground package field ${field}`); -} - -/** @param {string} directory */ -function checkDirectory(directory) { - for (const entry of readdirSync(directory, { withFileTypes: true })) { - const path = nodePath.join(directory, entry.name); - if (entry.isSymbolicLink()) { - failures.push(`${path}: playground source symlinks are forbidden`); - } else if (entry.isDirectory()) { - if (!skippedDirectories.has(entry.name)) { - checkDirectory(path); - } - } else if (entry.name === "package.json") { - checkManifest(path); - } else if (entry.name.endsWith(".css")) { - failures.push(`${path}: playground CSS files are forbidden`); - } else if (entry.name.endsWith(".vue")) { - checkVue(path); - } else if (codeExtensions.has(nodePath.extname(entry.name))) { - checkScript(readFileSync(path, "utf8"), path); - } - } -} - -checkManifest(nodePath.join(root, "package.json")); -checkDirectory(nodePath.join(root, "src")); -checkDirectory(nodePath.join(root, "frontend-vue")); -if (failures.length) { - process.stderr.write(`${failures.join("\n")}\n`); - process.exitCode = 1; -} else { - process.stdout.write("Playground import checks passed.\n"); -} diff --git a/packages/dms/playground/checks/fixtures.mjs b/packages/dms/playground/checks/fixtures.mjs deleted file mode 100644 index b7587cf50..000000000 --- a/packages/dms/playground/checks/fixtures.mjs +++ /dev/null @@ -1,118 +0,0 @@ -import { spawnSync } from "node:child_process"; -import { randomUUID } from "node:crypto"; -import { rmSync, writeFileSync } from "node:fs"; -import nodePath from "node:path"; -import assert from "node:assert/strict"; - -export const root = nodePath.resolve(import.meta.dirname, ".."); -const scanner = nodePath.join(import.meta.dirname, "aliases.cjs"); - -/** - * Writes every fixture, scans the tree once, and removes the fixtures. - * - * @param {readonly (readonly [path: string, source: string, ...unknown[]])[]} fixtures - */ -function scan(fixtures) { - const paths = fixtures.map(([path]) => path); - assert.equal(new Set(paths).size, paths.length, "fixture paths repeat"); - try { - for (const [path, source] of fixtures) writeFileSync(path, source); - return spawnSync(process.execPath, [scanner], { encoding: "utf8" }); - } finally { - for (const path of paths) rmSync(path, { force: true }); - } -} - -/** - * Asserts that the tree passes with every fixture in it. - * - * @param {readonly (readonly [path: string, source: string])[]} fixtures - */ -export function assertAccepted(fixtures) { - const result = scan(fixtures); - assert.equal(result.status, 0, result.stdout + result.stderr); -} - -/** - * The scanner's diagnostic kinds, each the message prefix of one failure site. - * A kind that prints without failing the scan leaves the exit status at 0 only - * when no other kind shares the scan, so a rejected batch holds one kind set. - */ -const kinds = /** @type {const} */ ({ - static: "playground import must be a static constant string", - forbidden: "forbidden playground import ", - loader: "unsupported playground loader ", - parse: "invalid playground script: ", - extStyle: "external playground styles are forbidden", - inlineStyle: "unsupported playground inline style", - customBlock: "playground custom block ", - extScript: "external playground scripts are forbidden", - metadata: "additional playground package metadata is forbidden", -}); - -/** @typedef {keyof typeof kinds} Kind */ - -/** - * @param {string} key - * @returns {key is Kind} - */ -function isKind(key) { - return Object.hasOwn(kinds, key); -} - -/** - * @param {string} line - * @param {string} path - * @returns {Kind | undefined} - */ -function kindOf(line, path) { - const message = line.slice(path.length + 2); - return Object.keys(kinds) - .filter(isKind) - .find((kind) => message.startsWith(kinds[kind])); -} - -/** - * Asserts that each fixture fails the scan on its own path with exactly the - * given kinds. A diagnostic names the file that holds the import, so each - * fixture proves its own rejection, and the fixtures share one scan because - * they share one kind set: a kind that stops failing the scan turns the exit - * status to 0 here, where a sibling of another kind could hide it. - * - * @param {Kind | readonly Kind[]} expected the kinds every fixture reports - * @param {readonly (readonly [path: string, source: string, failures?: number | null])[]} fixtures - * `failures` is the diagnostic count expected on `path`, 1 by default; null accepts any non-empty set. - */ -export function assertRejected(expected, fixtures) { - const want = [expected].flat().sort(); - assert.ok(fixtures.length > 0, "a rejected batch needs a fixture"); - for (const [path, , failures = 1] of fixtures) - assert.notEqual(failures, 0, `${path} expects no diagnostic`); - const result = scan(fixtures); - assert.equal(result.status, 1, result.stdout + result.stderr); - const lines = result.stderr.split("\n"); - for (const [path, , failures = 1] of fixtures) { - const diagnostics = lines.filter((line) => line.startsWith(`${path}:`)); - if (failures === null) { - assert.ok(diagnostics.length > 0, `${path}\n${result.stderr}`); - } else { - assert.equal(diagnostics.length, failures, `${path}\n${result.stderr}`); - } - const found = [ - ...new Set(diagnostics.map((line) => kindOf(line, path) ?? line)), - ].sort(); - assert.deepEqual(found, want, `${path}\n${result.stderr}`); - } -} - -/** - * @param {string} area - * @param {string} [extension] - */ -export function fixturePath(area, extension = "ts") { - return nodePath.join( - root, - area, - `__import_guard_${randomUUID().replaceAll("-", "")}.${extension}`, - ); -} diff --git a/packages/dms/playground/checks/fixtures.test.mjs b/packages/dms/playground/checks/fixtures.test.mjs deleted file mode 100644 index 2ce6842c2..000000000 --- a/packages/dms/playground/checks/fixtures.test.mjs +++ /dev/null @@ -1,20 +0,0 @@ -import assert from "node:assert/strict"; -import { test } from "node:test"; -import { assertRejected, fixturePath } from "./fixtures.mjs"; - -await test("a rejected fixture that expects no diagnostic is refused before any scan", () => { - assert.throws( - () => assertRejected("forbidden", [[fixturePath("src"), "export {};", 0]]), - assert.AssertionError, - ); -}); - -await test("a rejected batch is refused when a fixture reports a kind it did not declare", () => { - assert.throws( - () => - assertRejected("forbidden", [ - [fixturePath("src"), "import(unknownSpecifier);"], - ]), - assert.AssertionError, - ); -}); diff --git a/packages/dms/playground/checks/frontend-tools.cjs b/packages/dms/playground/checks/frontend-tools.cjs deleted file mode 100644 index 909a7f686..000000000 --- a/packages/dms/playground/checks/frontend-tools.cjs +++ /dev/null @@ -1,81 +0,0 @@ -const { createRequire } = require("node:module"); -const nodePath = require("node:path"); -const { - isCallable, - isCssNode, - isRecord, - isTextList, -} = require("./loaded-values.mjs"); - -const frontendRequire = createRequire( - nodePath.resolve(__dirname, "../../frontend-vue/package.json"), -); - -/** - * `tinyglobby`, which vite depends on, read for the one call the checks make. - * @returns {{ globSync: (patterns: string | string[], options: { cwd?: string, absolute: boolean, dot?: boolean, expandDirectories?: boolean, ignore: string[] }) => string[] }} - */ -function loadGlob() { - /** @type {unknown} */ - const loaded = createRequire(frontendRequire.resolve("vite/package.json"))( - "tinyglobby", - ); - if (!isRecord(loaded) || !isCallable(loaded.globSync)) - throw new TypeError("tinyglobby exports no globSync"); - const read = loaded.globSync; - return { - globSync(patterns, options) { - const matches = read(patterns, options); - if (!isTextList(matches)) - throw new TypeError( - "tinyglobby.globSync did not return a list of paths", - ); - return matches; - }, - }; -} - -/** - * `css-tree`, which jsdom depends on and which publishes no declarations, read for the two calls the checks make. - * @returns {{ - * parse: (css: string, options: { parseCustomProperty: boolean, onParseError: (error: Error) => void }) => object, - * walk: (ast: object, visit: (node: { type: string, name?: string }) => void) => void, - * }} - */ -function loadCssTree() { - /** @type {unknown} */ - const loaded = createRequire(frontendRequire.resolve("jsdom/package.json"))( - "css-tree", - ); - if ( - !isRecord(loaded) || - !isCallable(loaded.parse) || - !isCallable(loaded.walk) - ) - throw new TypeError("css-tree exports no parse and walk"); - const read = loaded.parse; - const visitAll = loaded.walk; - return { - parse(css, options) { - const ast = read(css, options); - if (!isRecord(ast)) - throw new TypeError("css-tree.parse did not return a syntax tree"); - return ast; - }, - walk(ast, visit) { - visitAll(ast, (/** @type {unknown} */ node) => { - if (!isCssNode(node)) - throw new TypeError( - "css-tree.walk visited something that is no node", - ); - visit(node); - }); - }, - }; -} - -module.exports = { - vue: require("../../frontend-vue/node_modules/vue/compiler-sfc"), - glob: loadGlob(), - css: loadCssTree(), -}; diff --git a/packages/dms/playground/checks/imports.test.mjs b/packages/dms/playground/checks/imports.test.mjs deleted file mode 100644 index e2f40ab27..000000000 --- a/packages/dms/playground/checks/imports.test.mjs +++ /dev/null @@ -1,495 +0,0 @@ -import assert from "node:assert/strict"; -import { spawnSync } from "node:child_process"; -import { randomUUID } from "node:crypto"; -import { mkdirSync, rmSync, symlinkSync, writeFileSync } from "node:fs"; -import nodePath from "node:path"; -import { test } from "node:test"; -import { - assertAccepted, - assertRejected, - fixturePath, - root, -} from "./fixtures.mjs"; - -const scanner = nodePath.join(import.meta.dirname, "aliases.cjs"); - -/** @typedef {[path: string, source: string, failures?: number | null]} Fixture */ - -await test("checks resolved import targets regardless of expression spelling or Vue source form", (t) => { - const id = `__import_guard_${randomUUID().replaceAll("-", "")}`; - const hidden = nodePath.join( - root, - `../frontend-vue/layers/dms-core/app/node_modules/${id}`, - ); - const dist = nodePath.join(root, `dist/${id}.js`); - for (const path of [nodePath.join(hidden, "index.js"), dist]) { - mkdirSync(nodePath.dirname(path), { recursive: true }); - writeFileSync(path, "module.exports = 1;\n"); - } - t.after(() => { - rmSync(hidden, { recursive: true }); - rmSync(dist); - }); - - /** @type {{ path: string, extension: string, wrap: (code: string) => string, client: boolean }[]} */ - const contexts = [ - { path: "src", extension: "ts", wrap: (code) => code, client: false }, - { - path: "frontend-vue/app", - extension: "ts", - wrap: (code) => code, - client: true, - }, - { - path: "frontend-vue/app", - extension: "vue", - wrap: (code) => ``, - client: true, - }, - { - path: "frontend-vue/app", - extension: "vue", - wrap: (code) => ``, - client: true, - }, - ]; - /** @type {Fixture[]} */ - const mixed = []; - /** @type {Fixture[]} */ - const forbidden = []; - /** @type {Fixture[]} */ - const accepted = []; - for (const context of contexts) { - const ownDist = context.client ? `../../dist/${id}` : `../dist/${id}`; - const backend = context.client ? "../../../src/index" : "../../src/index"; - mixed.push([ - fixturePath(context.path, context.extension), - context.wrap( - [ - `require((${JSON.stringify(ownDist)} satisfies string));`, - `import((${JSON.stringify(backend)} as string));`, - `import "#dms-core/app/node_modules/${id}/index";`, - 'require(`#dms-core/${"../../../src"}/index`);', - 'require("#dms-core/" + "../../../src/index");', - "import(unknownSpecifier);", - ].join("\n"), - ), - 6, - ]); - const control = context.client - ? 'require(("#dms-core/app/utils/http-status" satisfies string));' - : 'require(("./sections" satisfies string));'; - accepted.push([ - fixturePath(context.path, context.extension), - context.wrap(control + '\nimport "@antelopejs/interface-dms/page";'), - ]); - if (!context.client) { - forbidden.push([ - fixturePath(context.path, context.extension), - 'require(("#dms-core/app/utils/http-status" satisfies string));', - ]); - } - } - assertRejected(["forbidden", "static"], mixed); - assertRejected("forbidden", forbidden); - assertRejected("extScript", [ - [ - fixturePath("frontend-vue/app", "vue"), - '', - ], - [ - fixturePath("frontend-vue/app", "vue"), - '', - ], - ]); - assertAccepted(accepted); -}); - -await test("rejects client import.meta.glob access to backend and interface sources", () => { - assertRejected( - "forbidden", - ["../../../src/**/*.ts", "../../../../interface-dms/src/**/*.ts"].map( - (pattern) => [ - fixturePath("frontend-vue/app"), - `import.meta.glob(${JSON.stringify(pattern)});`, - null, - ], - ), - ); - assertAccepted([ - [ - fixturePath("frontend-vue/app"), - [ - 'import "#dms-core/app/utils/http-status";', - 'import "@antelopejs/interface-dms/page";', - 'import "./composables/usePageSetupLog";', - 'import.meta.glob("./composables/*.ts");', - ].join("\n"), - ], - ]); -}); - -await test("rejects client new URL access to backend source through import.meta.url", () => { - assertRejected("loader", [ - [ - fixturePath("frontend-vue/app"), - 'new URL("../../../src/index.ts", import.meta.url);', - null, - ], - ]); -}); - -await test("rejects wrapped client relative dynamic imports into shared Vue layers", () => { - assertRejected("forbidden", [ - [ - fixturePath("frontend-vue/app"), - 'import(("../../../frontend-vue/layers/dms-core/app/utils/http-status" satisfies string));', - null, - ], - ]); -}); - -await test("rejects server module.require and require.resolve access to own dist", (t) => { - const id = `__import_guard_${randomUUID().replaceAll("-", "")}`; - const dist = nodePath.join(root, "dist", `${id}.js`); - mkdirSync(nodePath.dirname(dist), { recursive: true }); - writeFileSync(dist, "module.exports = 1;\n"); - t.after(() => rmSync(dist)); - assertRejected( - "loader", - [ - `module.require("../dist/${id}.js");`, - `require.resolve("../dist/${id}.js");`, - `const load = require; load("../dist/${id}.js");`, - `const key = "require"; globalThis[key]("../dist/${id}.js");`, - `import.meta.resolve("../dist/${id}.js");`, - `const glob = import.meta.glob; glob("../dist/*.js");`, - ].map((source) => [fixturePath("src"), source, null]), - ); -}); - -await test("rejects same-tier and upward chart imports through wrapped require", () => { - const targets = ["../quick-actions", "../index"]; - assertRejected( - "forbidden", - targets.map((target) => [ - fixturePath("src/chart"), - `require((${JSON.stringify(target)} satisfies string));`, - null, - ]), - ); - assertRejected( - "loader", - targets.map((target) => [ - fixturePath("src/chart"), - `(0, require)(${JSON.stringify(target)});`, - null, - ]), - ); - assertAccepted([ - [fixturePath("src/chart"), 'require(("./category" satisfies string));'], - ]); -}); - -await test("rejects server type imports from the client frontend-module package", () => { - assertRejected("forbidden", [ - [ - fixturePath("src"), - 'import type * as FrontendModule from "@antelopejs/dms-frontend/templates/vue/frontend-module.ts";', - null, - ], - ]); -}); - -await test("rejects backend loaders in Vue templates rather than scanning only scripts", () => { - assertRejected( - "forbidden", - [ - ``, - ``, - ``, - ``, - '', - ].map((source) => [fixturePath("frontend-vue/app", "vue"), source, null]), - ); - assertAccepted( - ["./composables/usePageSetupLog", "#dms-core/app/utils/http-status"].map( - (target) => [ - fixturePath("frontend-vue/app", "vue"), - ``, - ], - ), - ); -}); - -await test("rejects scanned file symlinks instead of canonicalizing chart imports into quick-actions", (t) => { - const id = `__import_guard_${randomUUID().replaceAll("-", "")}`; - const target = nodePath.join(root, "src/quick-actions", `${id}.ts`); - const link = nodePath.join(root, "src/chart", `${id}.ts`); - t.after(() => { - rmSync(link, { force: true }); - rmSync(target, { force: true }); - }); - writeFileSync(target, 'require("./page");\n'); - symlinkSync(target, link, "file"); - const result = spawnSync(process.execPath, [scanner], { encoding: "utf8" }); - assert.equal(result.status, 1, result.stdout + result.stderr); - assert.ok( - result.stderr.split("\n").some((line) => line.startsWith(`${link}:`)), - result.stderr, - ); -}); - -await test("rejects external Vue scripts rather than accepting uninspected contents", (t) => { - const vue = fixturePath("frontend-vue/app", "vue"); - const script = vue.replace(/\.vue$/, ".inc"); - t.after(() => rmSync(script, { force: true })); - writeFileSync(script, 'import "../../../src/index.ts"; export default {};\n'); - assertRejected("extScript", [ - [ - vue, - ``, - null, - ], - ]); -}); - -await test("rejects source edges into every directory skipped by the scanner", (t) => { - const directory = fixturePath("frontend-vue/app").replace(/\.ts$/, ""); - t.after(() => rmSync(directory, { recursive: true, force: true })); - assertRejected( - "forbidden", - [".nuxt", ".output", "dist", "node_modules"].map((skipped) => { - const hidden = nodePath.join(directory, skipped, "hidden.ts"); - mkdirSync(nodePath.dirname(hidden), { recursive: true }); - writeFileSync(hidden, 'import "../../../../../src/index.ts";\n'); - return [ - fixturePath("frontend-vue/app"), - `import "./${nodePath.basename(directory)}/${skipped}/hidden.ts";`, - null, - ]; - }), - ); - assertAccepted([ - [ - fixturePath("frontend-vue/app"), - 'import "./composables/usePageSetupLog";', - ], - ]); -}); - -await test("checks JSX loaders with the declared script kind and rejects parse recovery", () => { - /** @type {Fixture[]} */ - const rejected = []; - /** @type {Fixture[]} */ - const accepted = []; - for (const extension of ["tsx", "jsx"]) { - const specifiers = [ - '("../../../src/index.ts")', - '"../../../src/" + "index.ts"', - ]; - if (extension === "tsx") - specifiers.push('("../../../src/index.ts" satisfies string)'); - for (const specifier of specifiers) { - const source = `export const value =
{import(${specifier})}
;`; - rejected.push( - [fixturePath("frontend-vue/app", extension), source, null], - [ - fixturePath("frontend-vue/app", "vue"), - ``, - null, - ], - ); - } - const source = - 'export const value =
{import("./composables/usePageSetupLog")}
;'; - accepted.push( - [fixturePath("frontend-vue/app", extension), source], - [ - fixturePath("frontend-vue/app", "vue"), - ``, - ], - ); - } - assertRejected("forbidden", rejected); - assertAccepted(accepted); - assertRejected("parse", [ - [fixturePath("frontend-vue/app"), "export const incomplete = ;", null], - [ - fixturePath("frontend-vue/app", "vue"), - '', - null, - ], - ]); -}); - -await test("rejects uninspected Vite inc targets even beside a TypeScript shadow", (t) => { - const bridge = fixturePath("frontend-vue/app", "inc"); - const shadow = `${bridge}.ts`; - const fragment = `${bridge}#shadow.ts`; - const encoded = `${bridge}%23shadow.ts`; - t.after(() => { - rmSync(bridge, { force: true }); - rmSync(shadow, { force: true }); - rmSync(fragment, { force: true }); - rmSync(encoded, { force: true }); - }); - writeFileSync( - bridge, - 'export { runInBatches } from "../../../src/utils/run-in-batches.ts";', - ); - writeFileSync(shadow, "export {};\n"); - writeFileSync(fragment, "export {};\n"); - writeFileSync(encoded, "export {};\n"); - assertRejected( - "forbidden", - [bridge, fragment, encoded].map((target) => [ - fixturePath("frontend-vue/app"), - `export * from "./${nodePath.basename(target)}";`, - ]), - ); -}); - -await test("rejects uninspected Node inc targets that can execute backend code", (t) => { - const bridge = fixturePath("src", "inc"); - t.after(() => rmSync(bridge, { force: true })); - writeFileSync( - bridge, - 'module.exports = require("../../src/utils/run-in-batches.ts");', - ); - assertRejected("forbidden", [ - [fixturePath("src", "cjs"), `require("./${nodePath.basename(bridge)}");`], - ]); -}); - -await test("rejects extensionless executable targets rather than trusting their local path", (t) => { - const bridge = fixturePath("src").replace(/\.ts$/, ""); - t.after(() => rmSync(bridge, { force: true })); - writeFileSync( - bridge, - 'module.exports = require("../../src/utils/run-in-batches.ts");', - ); - assertRejected("forbidden", [ - [fixturePath("src", "cjs"), `require("./${nodePath.basename(bridge)}");`], - ]); -}); - -await test("permits inert target kinds without treating Node require as a Vite asset loader", (t) => { - const prefix = fixturePath("frontend-vue/app").replace(/\.ts$/, ""); - const sources = new Map([ - [".json", '{"ok":true}'], - [".inc", 'require("../../../src/utils/run-in-batches.ts");'], - ]); - t.after(() => { - for (const suffix of sources.keys()) - rmSync(prefix + suffix, { force: true }); - }); - for (const [suffix, source] of sources) - writeFileSync(prefix + suffix, source); - const name = nodePath.basename(prefix); - assertAccepted( - [ - `import "./${name}.json"; import "./${name}.inc?raw"; import "./${name}.inc?url"; require("./${name}.json");`, - `import.meta.glob("./${name}.inc", { query: "?raw" });`, - ].map((source) => [fixturePath("frontend-vue/app"), source]), - ); - assertRejected( - "forbidden", - [ - `import "./${name}.inc?raw&unmodeled";`, - 'import "../../../src/utils/run-in-batches.ts?raw";', - ...[".css", ".inc?raw", ".inc?url"].map( - (suffix) => `require("./${name}${suffix}");`, - ), - `import asset = require("./${name}.css");`, - ].map((source) => [fixturePath("frontend-vue/app"), source]), - ); -}); - -await test("rejects scanned directory symlinks instead of silently skipping own-dist imports", (t) => { - const id = `__import_guard_${randomUUID().replaceAll("-", "")}`; - const target = nodePath.join(root, id); - const link = nodePath.join(root, "src", id); - const dist = nodePath.join(root, "dist", `${id}.js`); - t.after(() => { - rmSync(link, { force: true }); - rmSync(target, { recursive: true, force: true }); - rmSync(dist, { force: true }); - }); - mkdirSync(target); - mkdirSync(nodePath.dirname(dist), { recursive: true }); - writeFileSync(dist, "module.exports = 1;\n"); - writeFileSync( - nodePath.join(target, "index.ts"), - `require("../dist/${id}.js");\n`, - ); - symlinkSync(target, link, "dir"); - const result = spawnSync(process.execPath, [scanner], { encoding: "utf8" }); - assert.equal(result.status, 1, result.stdout + result.stderr); - assert.ok( - result.stderr.split("\n").some((line) => line.startsWith(`${link}:`)), - result.stderr, - ); -}); - -for (const { name, metadata, request } of [ - { - name: "nested imports/types/default", - metadata: { - imports: { "#bridge": { types: "./safe.ts", default: "./bridge.inc" } }, - }, - request: "#bridge", - }, - { - name: "self exports", - metadata: { - name: "guard-self", - exports: { ".": { types: "./safe.ts", default: "./bridge.inc" } }, - }, - request: "guard-self", - }, - { - name: "directory main/types", - metadata: { main: "./bridge.inc", types: "./safe.ts" }, - request: ".", - }, -]) { - await test(`rejects package metadata resolution through ${name}`, (t) => { - const directory = fixturePath("src").replace(/\.ts$/, ""); - mkdirSync(directory); - t.after(() => rmSync(directory, { recursive: true, force: true })); - writeFileSync(nodePath.join(directory, "safe.ts"), "export {};\n"); - writeFileSync( - nodePath.join(directory, "bridge.inc"), - 'module.exports = require("../../../src/index.ts");', - ); - writeFileSync( - nodePath.join(directory, "probe.cjs"), - `require(${JSON.stringify(request)});`, - ); - assertRejected("metadata", [ - [nodePath.join(directory, "package.json"), JSON.stringify(metadata)], - ]); - }); -} - -await test("rejects hidden/nested package.json across walked roots", (t) => { - assertRejected( - "metadata", - ["src", "frontend-vue", "frontend-vue/app"].map((area) => { - const directory = fixturePath(area).replace(/\.ts$/, ""); - const nested = nodePath.join(directory, ".hidden", "nested"); - mkdirSync(nested, { recursive: true }); - t.after(() => rmSync(directory, { recursive: true, force: true })); - return [nodePath.join(nested, "package.json"), "{}"]; - }), - ); -}); - -await import("./manifest.test.mjs"); -await import("./styles.test.mjs"); -await import("./fixtures.test.mjs"); diff --git a/packages/dms/playground/checks/loaded-values.mjs b/packages/dms/playground/checks/loaded-values.mjs deleted file mode 100644 index 2ee899d92..000000000 --- a/packages/dms/playground/checks/loaded-values.mjs +++ /dev/null @@ -1,48 +0,0 @@ -/** - * Checks on what a module loaded by path returned, so the checks read it as - * `unknown` and narrow it instead of trusting a declared type. - */ - -/** - * @param {unknown} value - * @returns {value is Record} True for an object that is not an array or null - */ -export function isRecord(value) { - return typeof value === "object" && value !== null && !Array.isArray(value); -} - -/** - * @param {unknown} value - * @returns {value is (...args: unknown[]) => unknown} True for a function; its result is read as `unknown` - */ -export function isCallable(value) { - return typeof value === "function"; -} - -/** - * @param {unknown} value - * @returns {value is string} True for a string - */ -function isText(value) { - return typeof value === "string"; -} - -/** - * @param {unknown} value - * @returns {value is string[]} True for a list of strings - */ -export function isTextList(value) { - return Array.isArray(value) && value.every(isText); -} - -/** - * @param {unknown} value - * @returns {value is { type: string, name?: string }} True for an object with a string `type` and, when it has one, a string `name` - */ -export function isCssNode(value) { - return ( - isRecord(value) && - isText(value.type) && - (value.name === undefined || isText(value.name)) - ); -} diff --git a/packages/dms/playground/checks/manifest.test.mjs b/packages/dms/playground/checks/manifest.test.mjs deleted file mode 100644 index db5608040..000000000 --- a/packages/dms/playground/checks/manifest.test.mjs +++ /dev/null @@ -1,69 +0,0 @@ -import assert from "node:assert/strict"; -import { spawnSync } from "node:child_process"; -import { mkdtempSync, rmSync, writeFileSync } from "node:fs"; -import { tmpdir } from "node:os"; -import path from "node:path"; -import { test } from "node:test"; - -const root = path.resolve(import.meta.dirname, ".."); -const scanner = path.join(import.meta.dirname, "aliases.cjs"); - -await test("rejects browser remapping and unknown fields in both trusted manifests", (t) => { - const fixtures = mkdtempSync(path.join(tmpdir(), "playground-manifests-")); - t.after(() => rmSync(fixtures, { recursive: true, force: true })); - const preload = path.join(fixtures, "manifest.cjs"); - writeFileSync( - preload, - ` - const fs = require("node:fs"); - const read = fs.readFileSync; - const fixtures = new Map(JSON.parse(process.env.PLAYGROUND_MANIFEST_FIXTURES)); - fs.readFileSync = (file, ...options) => - fixtures.has(file) ? fixtures.get(file) : read(file, ...options); - `, - ); - const manifests = [root, path.join(root, "frontend-vue")].map((directory) => - path.join(directory, "package.json"), - ); - /** @param {string} fixture */ - const scan = (fixture) => - spawnSync(process.execPath, ["--require", preload, scanner], { - encoding: "utf8", - env: { - ...process.env, - PLAYGROUND_MANIFEST_FIXTURES: JSON.stringify( - manifests.map((manifest) => [manifest, fixture]), - ), - }, - }); - const plain = scan('{"name":"guard-fixture"}'); - assert.equal(plain.status, 0, plain.stdout + plain.stderr); - - const fields = [ - "browser", - "module", - "types", - "imports", - "exports", - "react-native", - "workspaces", - "futureResolver", - "__proto__", - ]; - const remap = JSON.stringify({ - "./app/safe.ts": "../../src/utils/run-in-batches.ts", - }); - const remapped = scan( - `{"name":"guard-fixture",${fields.map((field) => `${JSON.stringify(field)}:${remap}`).join(",")}}`, - ); - assert.equal(remapped.status, 1, remapped.stdout + remapped.stderr); - const lines = remapped.stderr.split("\n"); - for (const manifest of manifests) - for (const field of fields) - assert.ok( - lines.includes( - `${manifest}: unsupported playground package field ${field}`, - ), - `${manifest} ${field}\n${remapped.stderr}`, - ); -}); diff --git a/packages/dms/playground/checks/raw-sources.cjs b/packages/dms/playground/checks/raw-sources.cjs index ecd685698..66b630a98 100644 --- a/packages/dms/playground/checks/raw-sources.cjs +++ b/packages/dms/playground/checks/raw-sources.cjs @@ -2,6 +2,7 @@ const { execFileSync } = require("node:child_process"); const { createRequire } = require("node:module"); const { existsSync, + globSync, lstatSync, readFileSync, realpathSync, @@ -13,10 +14,7 @@ const ts = require("typescript"); const playground = nodePath.resolve(__dirname, ".."); const repository = nodePath.resolve(playground, "../../.."); -const { - vue: { parse }, - glob: { globSync }, -} = require("./frontend-tools.cjs"); +const { parse } = require("../../frontend-vue/node_modules/vue/compiler-sfc"); const { ANTELOPE_IGNORE_PATTERNS, } = require("@antelopejs/tooling-configs/oxc/lint"); @@ -204,12 +202,18 @@ function addGlob(call, file) { return; } const cwd = nodePath.resolve(repository, nodePath.dirname(file)); - for (const match of globSync(sources, { - cwd, - absolute: true, - ignore: ["**/node_modules/**"], - })) - rawFiles.add(repositoryPath(match)); + const excluded = sources.filter((pattern) => pattern.startsWith("!")); + for (const match of globSync( + sources.filter((pattern) => !pattern.startsWith("!")), + { + cwd, + exclude: [ + ...excluded.map((pattern) => pattern.slice(1)), + "**/node_modules/**", + ], + }, + )) + rawFiles.add(repositoryPath(nodePath.resolve(cwd, match))); } /** diff --git a/packages/dms/playground/checks/styles.test.mjs b/packages/dms/playground/checks/styles.test.mjs deleted file mode 100644 index 01c809ec8..000000000 --- a/packages/dms/playground/checks/styles.test.mjs +++ /dev/null @@ -1,78 +0,0 @@ -import { rmSync, writeFileSync } from "node:fs"; -import { basename } from "node:path"; -import { test } from "node:test"; -import { assertAccepted, assertRejected, fixturePath } from "./fixtures.mjs"; - -await test("rejects inline and external Vue custom blocks", () => { - assertRejected( - "customBlock", - [ - '{"message":"hello"}', - '', - ].map((block) => [ - fixturePath("frontend-vue/app", "vue"), - `${block}`, - ]), - ); -}); - -await test("rejects CSS module imports and CSS @import files", (t) => { - const style = fixturePath("frontend-vue/app", "css"); - t.after(() => rmSync(style, { force: true })); - writeFileSync(style, '@import "../../../src/index.ts";'); - assertRejected( - "forbidden", - ["", "?raw", "?url"].map((suffix) => [ - fixturePath("frontend-vue/app"), - `import "./${basename(style)}${suffix}";`, - null, - ]), - ); -}); - -await test("rejects Vue style-src including safe targets", () => { - assertRejected( - "extStyle", - ["./composables/usePageSetupLog.ts", "../../../src/index.ts"].map( - (source) => [ - fixturePath("frontend-vue/app", "vue"), - ``, - ], - ), - ); -}); - -await test("rejects inline CSS @import/url, escapes, and unknown syntax", () => { - assertRejected( - "inlineStyle", - [ - '@import "../../../src/index.ts";', - '.probe { background: url("../../../src/index.ts"); }', - String.raw`@\69mport "../../../src/index.ts";`, - String.raw`.probe { background: u\72l("../../../src/index.ts"); }`, - '.probe { --asset: url("../../../src/index.ts"); }', - '.probe { background: image-set("../../../src/index.ts" 1x); }', - '.probe { background: var(--asset, url("../../../src/index.ts")); }', - ".probe { color: ; broken }", - ].map((source) => [ - fixturePath("frontend-vue/app", "vue"), - ``, - ]), - ); -}); - -await test("preserves safe existing inline styles", () => { - assertAccepted([ - [ - fixturePath("frontend-vue/app", "vue"), - ``, - ], - ]); -}); diff --git a/packages/dms/playground/checks/syntax.cjs b/packages/dms/playground/checks/syntax.cjs deleted file mode 100644 index 1b168a0bd..000000000 --- a/packages/dms/playground/checks/syntax.cjs +++ /dev/null @@ -1,142 +0,0 @@ -const ts = require("typescript"); - -const loaderNames = new Set([ - "require", - "createRequire", - "eval", - "Function", - "System", - "Worker", - "URL", - "SharedWorker", - "importScripts", - "_load", - "_resolveFilename", - "getBuiltinModule", - "dlopen", - "mainModule", - "loadModule", - "globalThis", - "global", - "module", - "document", - "window", - "self", - "process", -]); -/** - * @param {ts.Node} node - * @returns {ts.Node} - */ -function unwrap(node) { - while ( - ts.isParenthesizedExpression(node) || - ts.isAsExpression(node) || - ts.isTypeAssertionExpression(node) || - ts.isNonNullExpression(node) || - ts.isSatisfiesExpression(node) - ) { - node = node.expression; - } - return node; -} -/** - * Whether a `+` operand evaluates to text, so the operator concatenates. - * @param {ts.Node} expression - * @returns {boolean} - */ -function producesText(expression) { - const node = unwrap(expression); - if (ts.isStringLiteralLike(node) || ts.isTemplateExpression(node)) - return true; - return ( - ts.isBinaryExpression(node) && - node.operatorToken.kind === ts.SyntaxKind.PlusToken && - (producesText(node.left) || producesText(node.right)) - ); -} -/** - * @param {ts.Node} expression - * @returns {string | number | boolean | null | undefined} - */ -function constantValue(expression) { - const node = unwrap(expression); - if (ts.isStringLiteralLike(node)) return node.text; - if (ts.isNumericLiteral(node)) return Number(node.text); - if (node.kind === ts.SyntaxKind.TrueKeyword) return true; - if (node.kind === ts.SyntaxKind.FalseKeyword) return false; - if (node.kind === ts.SyntaxKind.NullKeyword) return null; - if (ts.isTemplateExpression(node)) { - let text = node.head.text; - for (const span of node.templateSpans) { - const value = constantValue(span.expression); - if (value === undefined) return undefined; - text += String(value) + span.literal.text; - } - return text; - } - if ( - ts.isBinaryExpression(node) && - node.operatorToken.kind === ts.SyntaxKind.PlusToken - ) { - const left = constantValue(node.left); - const right = constantValue(node.right); - if (left === undefined || right === undefined) return undefined; - return producesText(node.left) || producesText(node.right) - ? String(left) + String(right) - : Number(left) + Number(right); - } - return undefined; -} -/** - * @param {ts.Node} node - * @returns {boolean} - */ -function importMeta(node) { - return ( - ts.isMetaProperty(node) && node.keywordToken === ts.SyntaxKind.ImportKeyword - ); -} -/** - * @param {ts.Node} node - * @returns {string | number | boolean | null | undefined} - */ -function propertyName(node) { - if (ts.isPropertyAccessExpression(node)) return node.name.text; - if (ts.isElementAccessExpression(node) && node.argumentExpression) - return constantValue(node.argumentExpression); - return undefined; -} -/** - * @param {ts.Node} node - * @returns {string | undefined} - */ -function loaderReference(node) { - if (importMeta(node)) return "import.meta"; - if (node.kind === ts.SyntaxKind.ImportKeyword) return "import"; - if (ts.isIdentifier(node) && ["module", "document"].includes(node.text)) { - const property = [ - ts.isPropertyAccessExpression, - ts.isPropertyAssignment, - ts.isPropertySignature, - ts.isPropertyDeclaration, - ].some((matches) => matches(node.parent)); - if (property && node.parent.name === node) return undefined; - } - if (ts.isIdentifier(node) && loaderNames.has(node.text)) return node.text; - const property = propertyName(node); - if ( - (loaderNames.has(property) && !["module", "document"].includes(property)) || - property === "constructor" || - property === "import" - ) - return `property ${property}`; - return undefined; -} -module.exports = { - unwrap, - constantValue, - importMeta, - propertyName, - loaderReference, -}; diff --git a/packages/dms/playground/package.json b/packages/dms/playground/package.json index 88cde9a4a..64622f923 100644 --- a/packages/dms/playground/package.json +++ b/packages/dms/playground/package.json @@ -7,7 +7,7 @@ "packageManager": "pnpm@10.2.0", "scripts": { "build": "rimraf dist && tsc", - "test": "pnpm build && node checks/aliases.cjs && node --test checks/imports.test.mjs dist/prototype-keys.test.js dist/form/demo-forms.test.js && node --import ./checks/resolve-ts.mjs --test checks/docs-fixtures.test.mjs", + "test": "pnpm build && node --test dist/prototype-keys.test.js dist/form/demo-forms.test.js && node --import ./checks/resolve-ts.mjs --test checks/docs-fixtures.test.mjs", "dev": "ajs project run -w", "frontend:dev": "ajs dms dev", "types:prepare": "ajs dms prepare-types -q --out .frontend-vue-types --layer ../frontend-vue --module frontend-vue --local-package @antelopejs/dms=.. --local-package @antelopejs/interface-dms=../../interface-dms", From 01b0a1017fc78b4b7cec339b705f44a4c527a6d0 Mon Sep 17 00:00:00 2001 From: Antony Rizzitelli Date: Sun, 15 Nov 2026 17:32:30 +0000 Subject: [PATCH 5/7] refactor(dms): remove the verdict gates and behaviour fingerprints --- .../SKILL.md | 4 +- .../formal-onboarding-bootstrap/SKILL.md | 2 +- .agents/skills/formal-realtime-tabs/SKILL.md | 2 +- .../skills/formal-tenant-lifecycle/SKILL.md | 6 +- .agents/skills/verify-dms/SKILL.md | 63 +- .agents/skills/verify-dms/verify.config.ts | 29 +- guardrails.suppressions.json | 6 +- package.json | 2 +- packages/dms/bin/control/commands-verify.mjs | 57 +- packages/dms/bin/control/options.mjs | 1 - packages/dms/bin/control/run-state.mjs | 2 +- packages/dms/bin/control/timing.mjs | 17 +- packages/dms/bin/control/timing.test.mjs | 11 - packages/dms/bin/control/verify/behaviour.mjs | 183 ------ .../dms/bin/control/verify/behaviour.test.mjs | 472 --------------- packages/dms/bin/control/verify/decode.mjs | 148 +---- .../dms/bin/control/verify/decode.test.mjs | 150 +---- packages/dms/bin/control/verify/emit.mjs | 539 ------------------ packages/dms/bin/control/verify/formal.mjs | 240 +------- .../dms/bin/control/verify/formal.test.mjs | 177 ++---- packages/dms/bin/control/verify/judgement.mjs | 101 ---- packages/dms/bin/control/verify/map.test.mjs | 73 --- packages/dms/bin/control/verify/types.mjs | 22 +- packages/dms/bin/control/verify/verdict.mjs | 327 ----------- packages/dms/bin/dms-control.mjs | 2 - 25 files changed, 126 insertions(+), 2510 deletions(-) delete mode 100644 packages/dms/bin/control/verify/behaviour.mjs delete mode 100644 packages/dms/bin/control/verify/behaviour.test.mjs delete mode 100644 packages/dms/bin/control/verify/emit.mjs delete mode 100644 packages/dms/bin/control/verify/judgement.mjs delete mode 100644 packages/dms/bin/control/verify/verdict.mjs diff --git a/.agents/skills/formal-default-tenant-provisioning/SKILL.md b/.agents/skills/formal-default-tenant-provisioning/SKILL.md index f9dedd74a..9269b8e44 100644 --- a/.agents/skills/formal-default-tenant-provisioning/SKILL.md +++ b/.agents/skills/formal-default-tenant-provisioning/SKILL.md @@ -39,11 +39,11 @@ One run each, Docker Java 21. Every configuration runs 2 provisioning steps. Hea - Exit 1 means a rule changed. `BROKEN` names a rule that held and now breaks. `CHANGED` names a finding whose shortest counterexample changed length. `HOLDS` names a finding that no longer breaks. TLC's output is kept in a printed temporary folder. The run stops at the first configuration that changed. - Exit 2 means TLC or Java could not run, or the arguments were wrong. -The lines that name a changed rule match `^(?:BROKEN|CHANGED|HOLDS) +(\w+)`. A rule that still holds prints as lowercase `holds`. A finding is judged by the length of its shortest counterexample alone, so a longer new counterexample exits 0. While F1 stands, `dms-control formal` fails any diff that changes the covered behaviour. A `BROKEN` trace comes from a one-worker rerun, so its length is stable. +The lines that name a changed rule match `^(?:BROKEN|CHANGED|HOLDS) +(\w+)`. A rule that still holds prints as lowercase `holds`. A finding is judged by the length of its shortest counterexample alone, so a longer new counterexample exits 0. While F1 stands, `dms-control formal` prints `VIOLATED` for this check. A `BROKEN` trace comes from a one-worker rerun, so its length is stable. ## When to run it -Run `check.mjs --full` when a change touches a file in the coverage record below, or this folder. Run `history.mjs` too when the change touches this folder. A changed rule (exit 1) makes the Verdict FAIL. The recorded finding fails it only when the diff changes what the covered code does, which `dms-control formal` decides by comparing the covered files' JavaScript statements on the base and on HEAD (`verify-dms` verdict rule 5). `dms-control formal` runs `check.mjs` only. CI runs `check.mjs` and `history.mjs` on every push. +Run `check.mjs --full` when a change touches a file in the coverage record below, or this folder. Run `history.mjs` too when the change touches this folder. A changed rule (exit 1) makes `dms-control formal` print `ERROR` or `VIOLATED`, and the verifier explains it in the reply (`verify-dms` verdict rule 5). `dms-control formal` runs `check.mjs` only. CI runs `check.mjs` and `history.mjs` on every push. ## The rules diff --git a/.agents/skills/formal-onboarding-bootstrap/SKILL.md b/.agents/skills/formal-onboarding-bootstrap/SKILL.md index 3100d46eb..10afe5498 100644 --- a/.agents/skills/formal-onboarding-bootstrap/SKILL.md +++ b/.agents/skills/formal-onboarding-bootstrap/SKILL.md @@ -40,7 +40,7 @@ The lines that name a changed rule match `^(?:BROKEN|CHANGED|HOLDS) +(\w+)`. A r ## When to run it -Run `--full` when a change touches a file in the coverage record below, or this folder. A changed rule (exit 1) makes the Verdict FAIL. The recorded findings fail it only when the diff changes what the covered code does, which `dms-control formal` decides by comparing the covered files' JavaScript statements on the base and on HEAD (`verify-dms` verdict rule 5). The CI configuration catches every scratch break in the last section, so it is the one to run on every push. +Run `--full` when a change touches a file in the coverage record below, or this folder. A changed rule (exit 1) makes `dms-control formal` print `ERROR` or `VIOLATED`, and the verifier explains it in the reply (`verify-dms` verdict rule 5). The CI configuration catches every scratch break in the last section, so it is the one to run on every push. ## The rules diff --git a/.agents/skills/formal-realtime-tabs/SKILL.md b/.agents/skills/formal-realtime-tabs/SKILL.md index b0e3b99f2..b1b7a63e3 100644 --- a/.agents/skills/formal-realtime-tabs/SKILL.md +++ b/.agents/skills/formal-realtime-tabs/SKILL.md @@ -42,7 +42,7 @@ The lines that name a changed rule match `^(?:BROKEN|CHANGED|HOLDS) +(\w+)`. A r ## When to run it -Run `--full` when a change touches a file in the coverage record below, or this folder. A changed rule (exit 1) makes the Verdict FAIL. The recorded findings fail it only when the diff changes what the covered code does, which `dms-control formal` decides by comparing the covered files' JavaScript statements on the base and on HEAD (`verify-dms` verdict rule 5). The CI configurations catch every scratch break in the last section, so they are the ones to run on every push. +Run `--full` when a change touches a file in the coverage record below, or this folder. A changed rule (exit 1) makes `dms-control formal` print `ERROR` or `VIOLATED`, and the verifier explains it in the reply (`verify-dms` verdict rule 5). The CI configurations catch every scratch break in the last section, so they are the ones to run on every push. ## The rules diff --git a/.agents/skills/formal-tenant-lifecycle/SKILL.md b/.agents/skills/formal-tenant-lifecycle/SKILL.md index 553fea264..626c8c3c6 100644 --- a/.agents/skills/formal-tenant-lifecycle/SKILL.md +++ b/.agents/skills/formal-tenant-lifecycle/SKILL.md @@ -38,11 +38,11 @@ One run each, Docker Java 21, load near 40. The two-owner healthy run takes most The lines that name a changed rule match `^(?:BROKEN|CHANGED|HOLDS) +(\w+)`. A rule that still holds prints as lowercase `holds`. -No rule has a recorded finding now. If one is recorded, it is judged by the length of its shortest counterexample alone, so a longer new counterexample exits 0; while a finding stands, `dms-control formal` fails any diff that changes the covered behaviour. A `BROKEN` trace comes from a run on every core, so its length, and which of two broken rules it names, can vary. Findings run on one worker, so their lengths are stable. +No rule has a recorded finding now. If one is recorded, it is judged by the length of its shortest counterexample alone, so a longer new counterexample exits 0; while a finding stands, `dms-control formal` prints `VIOLATED` for this check. A `BROKEN` trace comes from a run on every core, so its length, and which of two broken rules it names, can vary. Findings run on one worker, so their lengths are stable. ## When to run it -Run `--full` when a change touches a file in the coverage record below, or this folder. A changed rule (exit 1) makes the Verdict FAIL. The recorded finding fails it only when the diff changes what the covered code does, which `dms-control formal` decides by comparing the covered files' JavaScript statements on the base and on HEAD (`verify-dms` verdict rule 5). CI runs the CI configuration on every push. +Run `--full` when a change touches a file in the coverage record below, or this folder. A changed rule (exit 1) makes `dms-control formal` print `ERROR` or `VIOLATED`, and the verifier explains it in the reply (`verify-dms` verdict rule 5). CI runs the CI configuration on every push. ## The rules @@ -125,7 +125,7 @@ The model leaves out: - the atomicity of `atomicMutation`. The model takes it from the database adapter, and the integration test "arbitrates simultaneous closure and admission on one revision" exercises it on MongoDB. - a process that dies. For these rules, that is an actor that stalls forever, which the model already allows. - a dropped `tenant_lifecycle` row. A new row would be open, and the docs forbid dropping it. `cleanupTenantData` leaves it in place. -- the status boundary of `isClientErrorStatus` (lifecycle.ts:22). `WorkRefuses` and `WorkFails` are free choices; only the verdict gate sees such a change. +- the status boundary of `isClientErrorStatus` (lifecycle.ts:22). `WorkRefuses` and `WorkFails` are free choices, so no check sees such a change. - the operator. `releaseTenantLifecycleAttempt` (lifecycle.ts:142-147) retires an admission on the operator's word, which no rule can check. The model never calls it. - the pause between release tries (lifecycle.ts:60). Time is not modeled; a pause is a stall, which the model already allows. - nested calls. The docstring of `runTenantLifecycleOperation` says each call takes its own admission, as `changeTenantMembers` says. Each model attempt is one top-level call. diff --git a/.agents/skills/verify-dms/SKILL.md b/.agents/skills/verify-dms/SKILL.md index a737f80e2..0d8ebc664 100644 --- a/.agents/skills/verify-dms/SKILL.md +++ b/.agents/skills/verify-dms/SKILL.md @@ -7,7 +7,7 @@ description: "Drive the AntelopeJS DMS (@antelopejs/dms backend and its Vue dash Every deterministic step is a `dms-control` command. You choose which journeys to run and judge what they show; you never write a multi-step shell recipe. When a journey you need does not exist, add it to `journeys/.mjs`, describe it in its feature file, and run it. -`dms-control` is the bin of `@antelopejs/dms` (`packages/dms/bin/dms-control.mjs`). Run it from the repository root with Node 24. On Linux it tells each process it started from a later one given the same pid by the start time `/proc` records, so it needs the `/proc` of its own PID namespace: under `unshare --pid` without `--mount-proc` it refuses to start or stop any process. On a system with no `/proc` at all, such as macOS, it tells processes apart by pid alone, as it always did. `up` records its PID and time namespaces in `run.json`. A command that only reads the run or calls its API (`mail`, `timings`, `api`, `onboard`, `verdict`) runs from any namespace. Every other command refuses, before it changes anything, when it runs in another one, so run it where `up` ran. +`dms-control` is the bin of `@antelopejs/dms` (`packages/dms/bin/dms-control.mjs`). Run it from the repository root with Node 24. On Linux it tells each process it started from a later one given the same pid by the start time `/proc` records, so it needs the `/proc` of its own PID namespace: under `unshare --pid` without `--mount-proc` it refuses to start or stop any process. On a system with no `/proc` at all, such as macOS, it tells processes apart by pid alone, as it always did. `up` records its PID and time namespaces in `run.json`. A command that only reads the run or calls its API (`mail`, `timings`, `api`, `onboard`) runs from any namespace. Every other command refuses, before it changes anything, when it runs in another one, so run it where `up` ran. ```bash C="node packages/dms/bin/dms-control.mjs" @@ -41,7 +41,7 @@ $C up --playground ~/code/dms/packages/dms/playground --renderer ~/code/dms-fron - `--renderer ` is a checkout of `@antelopejs/dms-frontend` that is installed and built (`dist/index.js` exists). `up` refuses any other folder before it starts anything. - After the playground copy is laid out, the run's `node_modules` is the playground's with one change: `@antelopejs/dms-frontend` is the checkout. `ajs dms build` and `ajs dms start` run from `/dist/index.js`, and the checkout resolves its own dependencies from its own `node_modules`. The playground, its modules and its installed `dms-frontend` are not touched. -- The run records `renderer`. `headSha` and the HEAD that `doctor` and `verdict` compare are the renderer checkout's, since that is the code under test. `doctor` also fails a renderer `dist` older than its `src`, and `versions` prints the checkout's version. +- The run records `renderer`. `headSha` and the HEAD that `doctor` compares are the renderer checkout's, since that is the code under test. `doctor` also fails a renderer `dist` older than its `src`, and `versions` prints the checkout's version. - The base-version backend (`withBaseBackend`, used by the `auth/base-written-data-upgrade` journey) is the playground's repository at `--base`, which a renderer run does not verify, so it refuses to start with `--renderer`. A skill in the renderer's repository names both in `verify.config.ts`, and `up` and `journey` need no flag: @@ -75,7 +75,7 @@ $C journey "$RUN" smoke $C journey "$RUN" settings-members [journey-id...] ``` -- `run` is the smoke feature and the features the diff touches. When the diff changes a shared file (the `shared` globs of `verify.config.ts`: package.json files, lockfiles, pnpm workspace files, the interface contract and the config), `run` is the smoke feature and then every feature, and `shared` lists the files that forced it. Before a release run `plan --all` and pass `--all` to `verdict` too, so the verdict requires every feature. +- `run` is the smoke feature and the features the diff touches. When the diff changes a shared file (the `shared` globs of `verify.config.ts`: package.json files, lockfiles, pnpm workspace files, the interface contract and the config), `run` is the smoke feature and then every feature, and `shared` lists the files that forced it. Before a release run `plan --all` and run every feature it lists. - Each feature file in [features/](features/README.md) lists its source globs (`covers:`), its docs pages (`docs:`) and its journeys; the journeys are in `journeys/.mjs`. - A journey starts on an empty database of the run, served by a backend that has served nothing else, with the administrator onboarded, unless it says otherwise. It drives the dashboard by ARIA role and name through playwright-core, one browser context per account, and reads every mutation back through a second view: a reload, the API, another account's tab, the captured mail, or a backend restart. - To look at the product yourself, use these commands, each a single step on the run's long-lived browser or API: @@ -109,7 +109,7 @@ Everything goes under `$RUN/evidence/`: - `browser/`, `api/`, `onboard/`, `auth/` for the interactive commands, and `doctor.json`; - `timings.jsonl`: one JSON object per measured phase, appended by `up`, `journey`, `formal` and `browser` as they run: `{ "phase", "ms", "startedAt", "ok" }` plus `feature`, `journey`, `step`, `command`, `action` or `check` where they apply. `ok` is false when the phase threw, and for a `journey` entry when the journey failed. A `journey` that throws, from its browser launch on, still writes its entry. A `prepare` command that fails writes nothing: `up` has not created the run yet. `ok` says whether the phase completed, not whether a browser action or a check inside it passed. The phases are `prepare` (one per command of the config), `up` (the whole of it) with `layout`, `mongod`, `mail`, `project-build`, `frontend-build` (with `frontend-prepare` inside it, the workspace setup and install the build cache keys on; on a hit, `frontend-build` is that plus the restore), `backend-start` and `frontend-start` inside it, `journey` (one whole journey) with `browser-launch`, `fresh-database`, `onboard` and `journey-run` inside it, `backend-stop` (each time the run stops backends: those a fresh database or `reset` replaces, and a restart inside a journey), `backend-start` (each backend the run starts and waits for: the first inside `up`, a restart inside a journey, and a fresh database or `reset` with no standby to take), `standby-promote` (a fresh database or `reset` waiting for its standby to answer and checking the build; `ok` is false when the standby is not taken and a backend starts on the spot instead), `standby-start` (the launch of the next database's standby, which then boots in the background; not counted in `total`), `aria` (each browser step) and `screenshot` (each `screenshot.png` written: a checking step once a tab was driven, a failed browser step or sign-in, `tab.snapshot` and the `screenshot` action; a passing click, fill or navigation records none), both with `step` set to the folder under `evidence/`, `formal` (one per check) and `browser-command` (one interactive `browser` round trip). -`$C timings "$RUN"` reads `timings.jsonl` and prints one row per phase (each journey under its own name) with the count, the total in seconds, the median and the maximum in milliseconds, then `total`: the sum of `prepare`, `up`, the journeys and `formal`, the phases that contain the others. The verdict carries the same table in its `Timing` section, and its journeys table a `Time` column. A run from an earlier release has no `timings.jsonl`, and `timings` prints an empty table for it. +`$C timings "$RUN"` reads `timings.jsonl` and prints one row per phase (each journey under its own name) with the count, the total in seconds, the median and the maximum in milliseconds, then `total`: the sum of `prepare`, `up`, the journeys and `formal`, the phases that contain the others. A run from an earlier release has no `timings.jsonl`, and `timings` prints an empty table for it. Read the evidence before you judge a PASS: a check passing proves only what it asserts. @@ -123,56 +123,19 @@ $C down "$RUN" ## Verdict rules -1. **Run every feature whose `covers:` globs the diff touches, plus the smoke journey** (onboard → login → invite a member → reload). `plan` prints that list as `run`. A diff that changes a shared file runs every feature, and so does a release (`--all`); the verdict fails any journey of those features that did not run. +State the verdict in your own reply. Start it with the result, then the doors, then what each rule below showed. + +1. **Run every feature whose `covers:` globs the diff touches, plus the smoke journey** (onboard → login → invite a member → reload). `plan` prints that list as `run`. A diff that changes a shared file runs every feature, and so does a release (`--all`). A journey of those features that did not run fails the verdict. 2. **Unmapped surface fails the verdict.** User-facing code is `packages/dms/src/**`, `packages/dms/frontend-vue/**` and `packages/interface-dms/src/**`, tests and tool configs excluded (`verify.config.ts`). A changed file there that no feature glob covers is an unmapped surface: the PR adds it to a feature with its journeys. `map` checks the whole tree, and CI runs it. 3. **Every registered journey of a feature in `run` must have run and passed.** A missing journey fails as "not run", so run whole features. Known findings stay as failing journeys (see the feature files' Gotchas): they fail a verdict only when `run` includes their feature, which a shared file or `--all` makes every feature. -4. **Docs.** Each touched feature lists its `docs:` pages. Judge every page against what the journeys showed. A page that no longer matches fails the verdict. -5. **Formal checks.** `verify.config.ts` lists each formal check with the files it covers and its own files. Run `$C formal "$RUN"` before the verdict: it runs the full configuration of every check whose covered files or own files the diff touches and records it under `evidence/formal//`. For each such check it also decides, without judgement, whether the diff changed what the covered code does: - - `emit` compares the structure of the covered files' JavaScript on the base and on HEAD: node kinds, names and literal values, with types, comments, formatting, parentheses and export keywords left out. Each use of an imported binding stands for its origin (the resolved module and the imported name), so an import retargeted to another module or alias counts as changed; a function declaration of a covered module is its name alone, and any other binding of a covered module is keyed by module and name. A function declaration refers to the top-level variables and classes of its own file by file and name, so moving it next to a different variable of the same name counts as changed. Exports are keyed by the origin they export, so a re-export or an export alias retargeted counts as changed. Each file must keep its top-level statements and side-effect imports in order; function declarations, which are hoisted, may move between covered files with their imports. So a type-only edit, a reformat or moving a function declaration counts as unchanged, while reordering statements or moving any other statement counts as changed. `emit` judges behaviour, not the public surface: exports are the api-diff's job; - - `probe` runs the check's probe on a checkout of each and compares its output (release-range runs the release workflow's `validate` step on fixture manifests; a crash prints the error's name). - - Then: - - a covered check that did not run, or exits non-zero (a tool error, or a check that no longer matches its record), fails the verdict; - - recorded findings fail the verdict only when the covered behaviour changed; when it did not, the verdict reports them as known findings; - - when the covered behaviour changed, or the diff changed the check's own files, `formal` also runs the check on the base. A rule the base broke counts as fixed only when HEAD reports it passing and the covered behaviour changed; one HEAD reports passing though the covered behaviour is unchanged was dropped by the edit to the check, and fails the verdict. When the edit corrects a wrong model, the verdict stays FAIL: show in the PR body the replay that does not reproduce the finding, and the owner judges it. A rule the base broke that HEAD does not report at all fails the verdict. A fix that leaves no violated rule passes. - - `--all` runs the checks that do not cover the diff too, without gating. -6. **One-way doors.** The section comes right after the result line and is mandatory; a verdict without it is void. It holds `None` with a reason, or one row per door: kind, location, what cannot be undone, containment. `$C doors` prints the `One-way-door:` line for the diff from the path rules in `verify.config.ts`. Every file they flag needs a row, or a `falsePositives` entry saying why it is not a door. -7. **Result.** FAIL if any of rules 1 to 6 fail. PASS+NOTES when something is not verified or needs the owner's attention. PASS otherwise. - -Write your judgement to `$RUN/evidence/judgement.json`: - -```json -{ - "doors": { - "none": "the diff changes only the verification skill", - "rows": [], - "falsePositives": [{ "kind": "contract", "location": "packages/dms/package.json", "reason": "only a devDependency changed" }] - }, - "docs": [{ "page": "packages/dms/docs/03.auth-and-tenancy/01.authentication.md", "matches": true, "note": "" }], - "notVerified": ["What no journey reached, and why"], - "attention": ["What the owner should look at that the skill cannot judge"], - "verifier": "the fresh verifier thread's URL; it did not write the change" -} -``` - -A door row is `{ "kind", "location", "irreversible", "containment" }`, where `kind` is a `doors` key of `verify.config.ts`. `verifier` is required. - -Then render the verdict: - -```bash -$C verdict "$RUN" --pr # writes evidence/verdict.md; exit 1 on FAIL -``` - -- `verdict` computes the result from `feature.json`, the plan, the formal results and the judgement, in the shape of the outer-loop sample verdict. -- A new push voids the verdict: verify again on the new head. -- Report the verdict and what the verifier saw in your own reply. Nothing is posted to GitHub. - -Give a verdict only from a thread that did not write the change, and name it in `verifier`. +4. **Docs.** Each touched feature lists its `docs:` pages. Judge every page against what the journeys showed, and name in the reply each page and whether it matches. A page that no longer matches fails the verdict. +5. **Formal checks.** `verify.config.ts` lists each formal check with the files it covers and its own files. Run `$C formal "$RUN"`. It runs the full configuration of every check whose covered files or own files the diff touches, or of every check with `--all`, and records each under `evidence/formal//`. It prints `VIOLATED`, `HOLDS` or `ERROR` for each check with its violated rules and exit code. A touched check that exits non-zero or reports a violated rule needs your explanation in the reply. +6. **One-way doors.** The reply lists them right after the result line, and a reply without the list is void. It holds `None` with a reason, or one row per door: kind, location, what cannot be undone, containment. `$C doors` prints the `One-way-door:` line for the diff from the path rules in `verify.config.ts`. Every file they flag needs a row, or a sentence saying why it is not a door. +7. **Result.** FAIL if any of rules 1 to 6 fail. PASS+NOTES when something is not verified or needs the owner's attention; list what no journey reached, and why, and what the owner should look at that the skill cannot judge. PASS otherwise. -## Trust +A new push voids the verdict: verify again on the new head. Nothing is posted to GitHub. -The skill is not Trusted yet. It becomes Trusted when it passes a shadow run of 10 consecutive agent PRs, and its last maintain pass (`maintain-verification-skill`, run by the gardener) is newer than the last change to the repository or its `@antelopejs` dependencies and at most 30 days old. The verdict header says `not trusted yet` until then. +Give a verdict only from a thread that did not write the change, and name that thread in the reply. ## Gotchas diff --git a/.agents/skills/verify-dms/verify.config.ts b/.agents/skills/verify-dms/verify.config.ts index be2dd18f8..389445c64 100644 --- a/.agents/skills/verify-dms/verify.config.ts +++ b/.agents/skills/verify-dms/verify.config.ts @@ -8,31 +8,16 @@ export interface DoorRule { why: string; } -/** - * How the gate decides whether a diff changed what the covered code does: - * `emit` compares the JavaScript statements of the covered files on the - * base and on HEAD (types, comments, formatting, imports and export - * keywords left out); `probe` runs a command on a checkout of each, the - * checkout's path as last argument, and compares what it prints. - */ -export type Behaviour = { kind: "emit" } | { kind: "probe"; command: string[] }; - /** * A formal check: the source files it covers, the check's own files, the - * command that runs it, how its output names a violated and a passing rule, - * how the gate decides that a diff changed the covered behaviour, and the - * command run on the base when it did or when the check's own files changed - * (`command` when absent). + * command that runs it and how its output names a violated rule. */ export interface FormalCheck { name: string; covers: string[]; files: string[]; command: string[]; - baseCommand?: string[]; violation: RegExp | null; - passing: RegExp | null; - behaviour: Behaviour; } /** The repo-owned layer of the verification skill, read by `dms-control`. */ @@ -205,10 +190,7 @@ const config: VerifyConfig = { ], files: [".agents/skills/formal-onboarding-bootstrap/**"], command: ["node", ".agents/skills/formal-onboarding-bootstrap/check.mjs", "--full"], - baseCommand: ["node", ".agents/skills/formal-onboarding-bootstrap/check.mjs"], violation: /^(?:finding|BROKEN|CHANGED|HOLDS) +(\w+)/gm, - passing: /(?<=^holds +(?:\w+, )*)(\w+)(?=,| \()/gm, - behaviour: { kind: "emit" }, }, { name: "formal-tenant-lifecycle", @@ -221,10 +203,7 @@ const config: VerifyConfig = { ], files: [".agents/skills/formal-tenant-lifecycle/**"], command: ["node", ".agents/skills/formal-tenant-lifecycle/check.mjs", "--full"], - baseCommand: ["node", ".agents/skills/formal-tenant-lifecycle/check.mjs"], violation: /^(?:finding|BROKEN|CHANGED|HOLDS) +(\w+)/gm, - passing: /(?<=^holds +(?:\w+, )*)(\w+)(?=,| \()/gm, - behaviour: { kind: "emit" }, }, { name: "formal-default-tenant-provisioning", @@ -236,10 +215,7 @@ const config: VerifyConfig = { ], files: [".agents/skills/formal-default-tenant-provisioning/**"], command: ["node", ".agents/skills/formal-default-tenant-provisioning/check.mjs", "--full"], - baseCommand: ["node", ".agents/skills/formal-default-tenant-provisioning/check.mjs"], violation: /^(?:finding|BROKEN|CHANGED|HOLDS) +(\w+)/gm, - passing: /(?<=^holds +(?:\w+, )*)(\w+)(?=,| \()/gm, - behaviour: { kind: "emit" }, }, { name: "formal-realtime-tabs", @@ -270,10 +246,7 @@ const config: VerifyConfig = { ], files: [".agents/skills/formal-realtime-tabs/**"], command: ["node", ".agents/skills/formal-realtime-tabs/check.mjs", "--full"], - baseCommand: ["node", ".agents/skills/formal-realtime-tabs/check.mjs"], violation: /^(?:finding|BROKEN|CHANGED|HOLDS) +(\w+)/gm, - passing: /(?<=^holds +(?:\w+, )*)(\w+)(?=,| \()/gm, - behaviour: { kind: "emit" }, }, ], }; diff --git a/guardrails.suppressions.json b/guardrails.suppressions.json index ebd91b35a..04596bd4b 100644 --- a/guardrails.suppressions.json +++ b/guardrails.suppressions.json @@ -8,11 +8,11 @@ "anti-slop/no-known-value-widening": 1, "anti-slop/no-object-parameters": 1, "anti-slop/no-reflect-apply": 1, - "anti-slop/no-reflect-get": 2, + "anti-slop/no-reflect-get": 0, "anti-slop/no-runtime-typeof": 0, "anti-slop/no-shape-in-symbol-names": 16, "anti-slop/no-unknown-parameters": 38, - "anti-slop/no-unknown-returns": 7, + "anti-slop/no-unknown-returns": 6, "anti-slop/no-unsafe-dictionary-type": 79, "anti-slop/require-safety-comment-for-type-assertion": 0, "eslint/max-params": 6, @@ -23,7 +23,7 @@ "typescript/no-misused-spread": 0, "typescript/no-redundant-type-constituents": 1, "typescript/no-unsafe-assignment": 2, - "typescript/no-unsafe-return": 2, + "typescript/no-unsafe-return": 1, "typescript/unbound-method": 0, "unicorn/no-useless-spread": 0, "vue/no-v-html": 0 diff --git a/package.json b/package.json index 034bc8e15..ef39c47f7 100644 --- a/package.json +++ b/package.json @@ -28,7 +28,7 @@ "lint:fix": "oxlint --fix && pnpm --dir packages/dms/frontend-vue lint:fix", "format": "oxfmt . && pnpm --dir packages/dms/frontend-vue format", "knip": "pnpm --dir packages/interface-dms knip && pnpm --dir packages/interface-dms-builder knip && pnpm --dir packages/dms knip", - "test": "pnpm --dir packages/dms check:backend && pnpm --dir packages/interface-dms test && pnpm --dir packages/dms/playground test && node --test checks/unreleased-interfaces.test.mjs checks/affected-packages.test.mjs .agents/formal/maps.test.mjs .agents/skills/verify-dms/rows.test.mjs packages/dms/bin/control/backend-records.test.mjs packages/dms/bin/control/backend-mode.test.mjs packages/dms/bin/control/base.test.mjs packages/dms/bin/control/backend.test.mjs packages/dms/bin/control/browser-wire.test.mjs packages/dms/bin/control/build-cache.test.mjs packages/dms/bin/control/build-key-refusals.test.mjs packages/dms/bin/control/build-key.test.mjs packages/dms/bin/control/build-layout.test.mjs packages/dms/bin/control/consumer-install.test.mjs packages/dms/bin/control/forwarder.test.mjs packages/dms/bin/control/frontend-mode.test.mjs packages/dms/bin/control/frontend-workspace.test.mjs packages/dms/bin/control/identity.test.mjs packages/dms/bin/control/journey.test.mjs packages/dms/bin/control/json.test.mjs packages/dms/bin/control/mail.test.mjs packages/dms/bin/control/module-root.test.mjs packages/dms/bin/control/mongo-server.test.mjs packages/dms/bin/control/mongo.test.mjs packages/dms/bin/control/namespaces.test.mjs packages/dms/bin/control/pid-guard.test.mjs packages/dms/bin/control/pid-reuse.test.mjs packages/dms/bin/control/processes.test.mjs packages/dms/bin/control/renderer.test.mjs packages/dms/bin/control/run-ports.test.mjs packages/dms/bin/control/run-state.test.mjs packages/dms/bin/control/tab.test.mjs packages/dms/bin/control/timing.test.mjs packages/dms/bin/control/verify/behaviour.test.mjs packages/dms/bin/control/verify/decode.test.mjs packages/dms/bin/control/verify/doors.test.mjs packages/dms/bin/control/verify/formal.test.mjs packages/dms/bin/control/verify/map.test.mjs packages/dms/scripts/backend-automation.test.mjs packages/dms/scripts/dms-dev.test.mjs packages/dms/scripts/playground-api.test.mjs", + "test": "pnpm --dir packages/dms check:backend && pnpm --dir packages/interface-dms test && pnpm --dir packages/dms/playground test && node --test checks/unreleased-interfaces.test.mjs checks/affected-packages.test.mjs .agents/formal/maps.test.mjs .agents/skills/verify-dms/rows.test.mjs packages/dms/bin/control/backend-records.test.mjs packages/dms/bin/control/backend-mode.test.mjs packages/dms/bin/control/base.test.mjs packages/dms/bin/control/backend.test.mjs packages/dms/bin/control/browser-wire.test.mjs packages/dms/bin/control/build-cache.test.mjs packages/dms/bin/control/build-key-refusals.test.mjs packages/dms/bin/control/build-key.test.mjs packages/dms/bin/control/build-layout.test.mjs packages/dms/bin/control/consumer-install.test.mjs packages/dms/bin/control/forwarder.test.mjs packages/dms/bin/control/frontend-mode.test.mjs packages/dms/bin/control/frontend-workspace.test.mjs packages/dms/bin/control/identity.test.mjs packages/dms/bin/control/journey.test.mjs packages/dms/bin/control/json.test.mjs packages/dms/bin/control/mail.test.mjs packages/dms/bin/control/module-root.test.mjs packages/dms/bin/control/mongo-server.test.mjs packages/dms/bin/control/mongo.test.mjs packages/dms/bin/control/namespaces.test.mjs packages/dms/bin/control/pid-guard.test.mjs packages/dms/bin/control/pid-reuse.test.mjs packages/dms/bin/control/processes.test.mjs packages/dms/bin/control/renderer.test.mjs packages/dms/bin/control/run-ports.test.mjs packages/dms/bin/control/run-state.test.mjs packages/dms/bin/control/tab.test.mjs packages/dms/bin/control/timing.test.mjs packages/dms/bin/control/verify/decode.test.mjs packages/dms/bin/control/verify/doors.test.mjs packages/dms/bin/control/verify/formal.test.mjs packages/dms/bin/control/verify/map.test.mjs packages/dms/scripts/backend-automation.test.mjs packages/dms/scripts/dms-dev.test.mjs packages/dms/scripts/playground-api.test.mjs", "prepare": "if [ -e .git ] && command -v git >/dev/null 2>&1 && command -v lefthook >/dev/null 2>&1 && GIT_DIR=.git GIT_WORK_TREE=. git rev-parse --git-dir >/dev/null 2>&1; then GIT_DIR=.git GIT_WORK_TREE=. lefthook install; fi", "checks": "antelopejs-checks", "checks:prepare": "pnpm --filter @antelopejs/interface-dms --filter @antelopejs/interface-dms-builder build", diff --git a/packages/dms/bin/control/commands-verify.mjs b/packages/dms/bin/control/commands-verify.mjs index 5605213f1..a9d1ce02a 100644 --- a/packages/dms/bin/control/commands-verify.mjs +++ b/packages/dms/bin/control/commands-verify.mjs @@ -1,18 +1,12 @@ import { join } from "node:path"; import { print } from "./commands-stack.mjs"; import { runJourneyFile } from "./journey.mjs"; -import { loadRun, readRun } from "./run-state.mjs"; +import { loadRun } from "./run-state.mjs"; import { timed } from "./timing.mjs"; import { doorLine, flaggedDoors, patchFiles } from "./verify/doors.mjs"; -import { - formalGate, - runFormalCheck, - touchedFormalChecks, -} from "./verify/formal.mjs"; -import { readJudgement } from "./verify/judgement.mjs"; +import { runFormalCheck, touchedFormalChecks } from "./verify/formal.mjs"; import { checkMap, planFor } from "./verify/map.mjs"; import { journeysFile, loadSkill } from "./verify/skill.mjs"; -import { renderVerdict } from "./verify/verdict.mjs"; /** * @typedef {import("./options.mjs").Values} Values @@ -108,64 +102,31 @@ async function commandFormal(dir, values) { const plan = planFor(skill, state.baseRef); const touched = touchedFormalChecks(skill, plan.changed); const checks = values.all ? skill.config.formal : touched; - const diff = { - baseSha: plan.baseSha, - headSha: plan.headSha, - changed: plan.changed, - }; for (const check of checks) { - const gate = touched.includes(check); const result = await timed( state, "formal", - () => runFormalCheck(skill, state, check, gate ? diff : null), + () => runFormalCheck(skill, state, check), { check: check.name }, ); - const weight = formalGate(result); - const outcome = gate - ? weight.fails - ? "FAIL" - : "PASS" - : result.violated.length > 0 + const outcome = + result.violated.length > 0 ? "VIOLATED" : result.exitCode === 0 ? "HOLDS" : "ERROR"; + const scope = touched.includes(check) ? "" : " (does not cover the diff)"; print( - `${outcome} ${check.name}: ${gate ? weight.outcome : `${result.violated.join(", ") || "no violated rule"} (does not cover the diff)`} (exit ${result.exitCode})`, + `${outcome} ${check.name}: ${result.violated.join(", ") || "no violated rule"}${scope} (exit ${result.exitCode})`, ); } if (checks.length === 0) print("no formal check covers the diff"); return 0; } -/** - * @param {string} dir The run root - * @param {Values} values The options of the command line - * @returns {Promise} The exit code - */ -async function commandVerdict(dir, values) { - const pr = Number(values.pr); - if (!Number.isInteger(pr) || pr <= 0) { - print("verdict needs --pr "); - return 2; - } - const state = readRun(dir); - const skill = await skillOf(values, state); - const verdict = await renderVerdict( - skill, - state, - pr, - readJudgement(skill, join(state.evidence, "judgement.json")), - values.all, - ); - print(verdict.markdown); - return verdict.result === "FAIL" ? 1 : 0; -} - /** * Runs a command of the verification harness: `map`, `plan`, `journey`, - * `doors`, `formal` and `verdict`. + * `doors` and `formal`. * * @param {string} command The command * @param {string[]} rest Its positional arguments @@ -184,8 +145,6 @@ export function runVerifyCommand(command, rest, values) { return commandDoors(rest[0], values); case "formal": return commandFormal(rest[0], values); - case "verdict": - return commandVerdict(rest[0], values); default: return Promise.resolve(undefined); } diff --git a/packages/dms/bin/control/options.mjs b/packages/dms/bin/control/options.mjs index 227fbd900..1c805443e 100644 --- a/packages/dms/bin/control/options.mjs +++ b/packages/dms/bin/control/options.mjs @@ -28,7 +28,6 @@ export const OPTIONS = /** @type {const} */ ({ key: { type: "string" }, path: { type: "string" }, body: { type: "string" }, - pr: { type: "string" }, all: { type: "boolean" }, }); diff --git a/packages/dms/bin/control/run-state.mjs b/packages/dms/bin/control/run-state.mjs index 23c7b6758..d76986d8b 100644 --- a/packages/dms/bin/control/run-state.mjs +++ b/packages/dms/bin/control/run-state.mjs @@ -216,7 +216,7 @@ const isStoredRun = objectOf( * creates or reads that folder. An account that `down` left without its * `password` reads with an empty one. Only a command that never reads, * starts, signals or forgets a process the run recorded opens its run here: - * `mail`, `timings`, `api`, `onboard` and `verdict`. Every other command + * `mail`, `timings`, `api` and `onboard`. Every other command * opens it with {@link loadRun}. * * @param {string} dir The run root, as `up` printed it diff --git a/packages/dms/bin/control/timing.mjs b/packages/dms/bin/control/timing.mjs index 448e5fa35..795c65708 100644 --- a/packages/dms/bin/control/timing.mjs +++ b/packages/dms/bin/control/timing.mjs @@ -181,7 +181,7 @@ export function summarize(entries) { * @param {number} ms A duration * @returns {string} It in seconds with one decimal */ -export function formatSeconds(ms) { +function formatSeconds(ms) { return (ms / MS_PER_SECOND).toFixed(1); } @@ -227,18 +227,3 @@ export function formatTimings(summary) { ) .join("\n"); } - -/** - * The summary as the Markdown table of the verdict's Timing section. - * - * @param {TimingSummary} summary The summary - * @returns {string[]} The lines of the table - */ -export function markdownTimings(summary) { - const [header, ...body] = cellsOf(summary); - return [ - `| ${header.join(" | ")} |`, - `|${header.map(() => "---").join("|")}|`, - ...body.map((row) => `| ${row.join(" | ")} |`), - ]; -} diff --git a/packages/dms/bin/control/timing.test.mjs b/packages/dms/bin/control/timing.test.mjs index 53e9cd00d..eb38aec54 100644 --- a/packages/dms/bin/control/timing.test.mjs +++ b/packages/dms/bin/control/timing.test.mjs @@ -5,7 +5,6 @@ import { join } from "node:path"; import test from "node:test"; import { formatTimings, - markdownTimings, readTimings, summarize, timed, @@ -121,16 +120,6 @@ await test("the plain table aligns its columns and ends with the total line", () ); }); -await test("the Markdown table has the same cells as the plain one", () => { - const summary = summarize([entry("up", 2000, "00")]); - assert.deepEqual(markdownTimings(summary), [ - "| phase | count | total s | median ms | max ms |", - "|---|---|---|---|---|", - "| up | 1 | 2.0 | 2000 | 2000 |", - "| total | | 2.0 | | |", - ]); -}); - await test("timed records the phase and returns what the action returned", async (t) => { const run = scratchRun(t); const value = await timed(run, "mail", () => 7, { step: "01-open" }); diff --git a/packages/dms/bin/control/verify/behaviour.mjs b/packages/dms/bin/control/verify/behaviour.mjs deleted file mode 100644 index 025d5ead8..000000000 --- a/packages/dms/bin/control/verify/behaviour.mjs +++ /dev/null @@ -1,183 +0,0 @@ -import { spawnSync } from "node:child_process"; -import { mkdtempSync, rmSync, symlinkSync } from "node:fs"; -import { tmpdir } from "node:os"; -import { join } from "node:path"; -import { git } from "../git.mjs"; -import { emitFingerprint } from "./emit.mjs"; - -const DETAIL_LIMIT = 5; -const OUTPUT_LIMIT = 64 * 1024 * 1024; - -/** - * @param {string} repo The checkout - * @param {string} sha The commit - * @returns {string} The scratch worktree - */ -function worktreeAt(repo, sha) { - const dir = mkdtempSync(join(tmpdir(), "dms-control-behaviour-")); - git(["worktree", "add", "--quiet", "--detach", "--force", dir, sha], repo); - symlinkSync(join(repo, "node_modules"), join(dir, "node_modules")); - return dir; -} - -/** - * Runs `action` in a throwaway worktree of `sha` whose `node_modules` links - * the checkout's, then removes it. - * - * @param {string} repo The checkout - * @param {string} sha The commit - * @param {(dir: string) => Result} action What to run there - * @returns {Result} What `action` returned - * @template Result - */ -export function inWorktree(repo, sha, action) { - const dir = worktreeAt(repo, sha); - try { - return action(dir); - } finally { - git(["worktree", "remove", "--force", dir], repo); - rmSync(dir, { recursive: true, force: true }); - } -} - -/** - * @param {string} repo The checkout - * @param {string[]} command The probe command - * @param {string} sha The commit - * @returns {string[]} The lines the probe printed - */ -function probeFingerprint(repo, command, sha) { - const [program, ...args] = command; - return inWorktree(repo, sha, (dir) => { - const run = spawnSync(program, [...args, dir], { - cwd: repo, - encoding: "utf8", - maxBuffer: OUTPUT_LIMIT, - }); - if (run.status !== 0) - throw new Error( - `behaviour probe ${command.join(" ")} failed on ${sha}:\n${run.stderr}`, - ); - return run.stdout.split("\n").filter(Boolean); - }); -} - -/** - * @param {{ structure: string, text: string }[]} left The items - * @param {{ structure: string, text: string }[]} right The items to subtract - * @returns {{ structure: string, text: string }[]} The items of `left` that `right` lacks - */ -function difference(left, right) { - /** @type {Map} */ - const counts = new Map(); - for (const item of right) - counts.set(item.structure, (counts.get(item.structure) ?? 0) + 1); - return left.filter((item) => { - const count = counts.get(item.structure) ?? 0; - if (count === 0) return true; - counts.set(item.structure, count - 1); - return false; - }); -} - -/** - * @param {string} path The covered file - * @param {{ structure: string, text: string }[]} base Its statements at the base - * @param {{ structure: string, text: string }[]} head Its statements at HEAD - * @returns {{ removed: string[], added: string[] }} The first statement that differs - */ -function sequenceChanges(path, base, head) { - const length = Math.max(base.length, head.length); - for (let index = 0; index < length; index += 1) { - if (base[index]?.structure !== head[index]?.structure) { - return { - removed: [ - `${path} statement ${index + 1}: ${base[index]?.text ?? "(none)"}`, - ], - added: [ - `${path} statement ${index + 1}: ${head[index]?.text ?? "(none)"}`, - ], - }; - } - } - return { removed: [], added: [] }; -} - -/** - * @param {ReturnType} base The fingerprint at the base - * @param {ReturnType} head The fingerprint at HEAD - * @returns {{ removed: string[], added: string[] }} What differs - */ -function compareEmit(base, head) { - const removed = difference(base.functions, head.functions).map( - (item) => `hoisted ${item.text}`, - ); - const added = difference(head.functions, base.functions).map( - (item) => `hoisted ${item.text}`, - ); - for (const path of new Set([ - ...Object.keys(base.files), - ...Object.keys(head.files), - ])) { - const change = sequenceChanges( - path, - base.files[path] ?? [], - head.files[path] ?? [], - ); - removed.push(...change.removed); - added.push(...change.added); - } - return { removed, added }; -} - -/** - * @param {string[]} base The probe lines at the base - * @param {string[]} head The probe lines at HEAD - * @returns {{ removed: string[], added: string[] }} What differs - */ -function compareProbe(base, head) { - /** - * @param {string[]} list Probe lines - * @returns {{ structure: string, text: string }[]} The lines as items - */ - const lines = (list) => list.map((line) => ({ structure: line, text: line })); - if (JSON.stringify(base) === JSON.stringify(head)) - return { removed: [], added: [] }; - const removed = difference(lines(base), lines(head)).map((item) => item.text); - const added = difference(lines(head), lines(base)).map((item) => item.text); - return removed.length + added.length > 0 - ? { removed, added } - : { removed: ["(order)"], added: ["(order)"] }; -} - -/** - * Whether a diff changed the behaviour of the code a formal check covers, - * decided without judgement. For `emit`, each covered file keeps its - * top-level statements, in order, with the same structure, and the covered - * function declarations stay the same wherever they live; for `probe`, the - * check's probe prints the same lines on a checkout of the base and of HEAD. - * - * @param {import("./skill.mjs").Skill} skill The skill - * @param {Pick} check The formal check - * @param {import("./types.mjs").Commits} commits The two commits - * @returns {import("./types.mjs").BehaviourChange} The decision and what differs - */ -export function behaviourChange(skill, check, commits) { - const probe = - check.behaviour?.kind === "probe" ? check.behaviour.command : null; - const change = probe - ? compareProbe( - probeFingerprint(skill.repo, probe, commits.baseSha), - probeFingerprint(skill.repo, probe, commits.headSha), - ) - : compareEmit( - emitFingerprint(skill.repo, commits.baseSha, check.covers), - emitFingerprint(skill.repo, commits.headSha, check.covers), - ); - return { - method: probe ? `probe ${probe.join(" ")}` : "emit", - changed: change.removed.length + change.added.length > 0, - removed: change.removed.slice(0, DETAIL_LIMIT), - added: change.added.slice(0, DETAIL_LIMIT), - }; -} diff --git a/packages/dms/bin/control/verify/behaviour.test.mjs b/packages/dms/bin/control/verify/behaviour.test.mjs deleted file mode 100644 index 5fe7e7734..000000000 --- a/packages/dms/bin/control/verify/behaviour.test.mjs +++ /dev/null @@ -1,472 +0,0 @@ -import assert from "node:assert/strict"; -import { execFileSync } from "node:child_process"; -import { mkdirSync, mkdtempSync, rmSync, writeFileSync } from "node:fs"; -import { tmpdir } from "node:os"; -import { dirname, join } from "node:path"; -import test from "node:test"; -import { behaviourChange } from "./behaviour.mjs"; - -const COVERS = ["src/a.ts", "src/b.ts"]; -const IMPORT_OUTPUT = 'import { FAILURE, SUCCESS } from "./output";\n'; - -/** - * @param {string} repo - * @param {...string} args - */ -function git(repo, ...args) { - return execFileSync("git", args, { cwd: repo, encoding: "utf8" }).trim(); -} - -/** - * @param {string} repo - * @param {Record} files - * @param {string} message - */ -function commit(repo, files, message) { - for (const [path, content] of Object.entries(files)) { - mkdirSync(dirname(join(repo, path)), { recursive: true }); - writeFileSync(join(repo, path), content); - } - git(repo, "add", "-A"); - git( - repo, - "-c", - "user.name=t", - "-c", - "user.email=t@example.com", - "-c", - "commit.gpgsign=false", - "commit", - "-q", - "-m", - message, - ); - return git(repo, "rev-parse", "HEAD"); -} - -/** - * @param {import("node:test").TestContext} t - * @param {Record} base - * @param {Record} head - * @param {string[]} [covers] - */ -function changed(t, base, head, covers = COVERS) { - const repo = mkdtempSync(join(tmpdir(), "dms-control-emit-test-")); - t.after(() => rmSync(repo, { recursive: true, force: true })); - git(repo, "init", "-q"); - const baseSha = commit(repo, base, "base"); - const headSha = commit(repo, head, "head"); - const check = { covers, behaviour: { kind: "emit" } }; - return behaviourChange({ repo }, check, { baseSha, headSha }).changed; -} - -const FUNCTION = "export function add(a, b) {\n return a + b;\n}\n"; -const OUTPUT = { - "src/output.ts": "export const FAILURE = 1;\nexport const SUCCESS = 0;\n", -}; - -await test("a function declaration moved verbatim to another covered file is unchanged", (t) => { - assert.equal( - changed( - t, - { "src/a.ts": FUNCTION, "src/b.ts": "export const label = 'b';\n" }, - { - "src/a.ts": 'export { add } from "./b";\n', - "src/b.ts": `export const label = 'b';\n${FUNCTION}`, - }, - ), - false, - ); -}); - -await test("an import that moves with its use to another covered file is unchanged", (t) => { - const use = `${IMPORT_OUTPUT}export function code() {\n return FAILURE + SUCCESS;\n}\n`; - assert.equal( - changed( - t, - { ...OUTPUT, "src/a.ts": use, "src/b.ts": "export const label = 1;\n" }, - { - ...OUTPUT, - "src/a.ts": 'export { code } from "./b";\n', - "src/b.ts": `export const label = 1;\n${use}`, - }, - ), - false, - ); -}); - -await test("a comment, a type annotation and a reformat are unchanged", (t) => { - assert.equal( - changed( - t, - { "src/a.ts": "const total = [1, 2].reduce((sum, n) => sum + n, 0);\n" }, - { - "src/a.ts": - "/** The total. */\nconst total: number = [1, 2].reduce(\n (sum: number, n: number) => (sum + n),\n 0,\n);\n", - }, - ), - false, - ); -}); - -await test("single quotes against double quotes are unchanged", (t) => { - assert.equal( - changed(t, { "src/a.ts": "run('x');\n" }, { "src/a.ts": 'run("x");\n' }), - false, - ); -}); - -await test("a body edit is changed", (t) => { - assert.equal( - changed( - t, - { "src/a.ts": FUNCTION }, - { "src/a.ts": FUNCTION.replace("a + b", "a - b") }, - ), - true, - ); -}); - -await test("swapping two statements of a covered file is changed", (t) => { - assert.equal( - changed( - t, - { "src/a.ts": "await first();\nawait second();\n" }, - { "src/a.ts": "await second();\nawait first();\n" }, - ), - true, - ); -}); - -await test("moving a statement that is not a function declaration to another covered file is changed", (t) => { - assert.equal( - changed( - t, - { - "src/a.ts": "const limit = 0;\nrun(limit);\n", - "src/b.ts": "run(1);\n", - }, - { - "src/a.ts": "run(limit);\n", - "src/b.ts": "const limit = 0;\nrun(1);\n", - }, - ), - true, - ); -}); - -await test("importing the same name under an alias that points elsewhere is changed", (t) => { - const use = (specifier) => - `import { ${specifier} } from "./output";\nexport function code() {\n return CANCELLED + FAILURE;\n}\n`; - assert.equal( - changed( - t, - { ...OUTPUT, "src/a.ts": use("CANCELLED, FAILURE") }, - { ...OUTPUT, "src/a.ts": use("FAILURE as CANCELLED, FAILURE") }, - ), - true, - ); -}); - -await test("importing a binding from another module is changed", (t) => { - const use = (module) => - `import { FAILURE } from "${module}";\nexport function code() {\n return FAILURE;\n}\n`; - assert.equal( - changed( - t, - { ...OUTPUT, "src/a.ts": use("./output") }, - { ...OUTPUT, "src/a.ts": use("./other") }, - ), - true, - ); -}); - -await test("retargeting a re-export is changed", (t) => { - assert.equal( - changed( - t, - { "src/a.ts": 'export { add } from "./one";\n' }, - { "src/a.ts": 'export { add } from "./two";\n' }, - ), - true, - ); -}); - -await test("removing a side-effect import is changed", (t) => { - assert.equal( - changed( - t, - { "src/a.ts": 'import "./init";\nrun();\n' }, - { "src/a.ts": "run();\n" }, - ), - true, - ); -}); - -await test("swapping two assignments to the same field is changed", (t) => { - assert.equal( - changed( - t, - { "src/a.ts": "target.value = 1;\ntarget.value = 2;\n" }, - { "src/a.ts": "target.value = 2;\ntarget.value = 1;\n" }, - ), - true, - ); -}); - -await test("swapping two registrations pushed to the same list is changed", (t) => { - assert.equal( - changed( - t, - { "src/a.ts": "handlers.push(first);\nhandlers.push(second);\n" }, - { "src/a.ts": "handlers.push(second);\nhandlers.push(first);\n" }, - ), - true, - ); -}); - -await test("importing min instead of max under the same alias, both in covered files, is changed", (t) => { - const use = (name) => - `import { ${name} as pick } from "./b";\nexport function best() {\n return pick(1, 2);\n}\n`; - const lib = { - "src/b.ts": - "export function max(a, b) {\n return a > b ? a : b;\n}\nexport function min(a, b) {\n return a < b ? a : b;\n}\n", - }; - assert.equal( - changed( - t, - { ...lib, "src/a.ts": use("max") }, - { ...lib, "src/a.ts": use("min") }, - ), - true, - ); -}); - -/** - * @param {import("node:test").TestContext} t - * @param {Record} base - * @param {Record} head - */ -function changedAll(t, base, head) { - return changed(t, base, head, Object.keys(base)); -} - -const OPS = - "export function add(a, b) {\n return a + b;\n}\nexport function sub(a, b) {\n return a - b;\n}\n"; - -await test("retargeting a re-export of a covered module to another of its exports is changed", (t) => { - assert.equal( - changedAll( - t, - { "src/ops.ts": OPS, "src/index.ts": 'export { add } from "./ops";\n' }, - { - "src/ops.ts": OPS, - "src/index.ts": 'export { sub as add } from "./ops";\n', - }, - ), - true, - ); -}); - -await test("a re-export of a covered module under its own name, reflowed, is unchanged", (t) => { - assert.equal( - changedAll( - t, - { "src/ops.ts": OPS, "src/index.ts": 'export { add } from "./ops";\n' }, - { "src/ops.ts": OPS, "src/index.ts": 'export { add } from "./ops";\n\n' }, - ), - false, - ); -}); - -await test("retargeting a local export alias is changed", (t) => { - const local = (target) => - `function f() {\n return 1;\n}\nfunction g() {\n return 2;\n}\nexport { ${target} as run };\n`; - assert.equal( - changedAll(t, { "src/a.ts": local("f") }, { "src/a.ts": local("g") }), - true, - ); - assert.equal( - changedAll( - t, - { "src/a.ts": local("f") }, - { "src/a.ts": `${local("f")}\n` }, - ), - false, - ); -}); - -await test("exporting a local under its own name, by keyword or by list, is unchanged", (t) => { - assert.equal( - changedAll( - t, - { "src/a.ts": "export const limit = 1;\n" }, - { "src/a.ts": "const limit = 1;\nexport { limit };\n" }, - ), - false, - ); -}); - -await test("swapping two side-effect imports is changed", (t) => { - assert.equal( - changedAll( - t, - { "src/a.ts": 'import "./first";\nimport "./second";\nrun();\n' }, - { "src/a.ts": 'import "./second";\nimport "./first";\nrun();\n' }, - ), - true, - ); -}); - -await test("a function moved verbatim into a file with a different top-level variable of the same name is changed", (t) => { - const log = "export function report() {\n return Logger.channel;\n}\n"; - assert.equal( - changedAll( - t, - { - "src/a.ts": `const Logger = { channel: "a" };\nexport const keep = 1;\n${log}`, - "src/b.ts": - 'const Logger = { channel: "b" };\nexport const other = 1;\n', - }, - { - "src/a.ts": - 'const Logger = { channel: "a" };\nexport const keep = 1;\n', - "src/b.ts": `const Logger = { channel: "b" };\nexport const other = 1;\n${log}`, - }, - ), - true, - ); -}); - -await test("a function moved with the variable it reads, or reading it through an import, is judged by that variable", (t) => { - const log = "export function report() {\n return Logger.channel;\n}\n"; - assert.equal( - changedAll( - t, - { - "src/a.ts": `export const Logger = { channel: "a" };\n${log}`, - "src/b.ts": "export const other = 1;\n", - }, - { - "src/a.ts": 'export const Logger = { channel: "a" };\n', - "src/b.ts": `import { Logger } from "./a";\nexport const other = 1;\n${log}`, - }, - ), - false, - ); -}); - -await test("an import aliased to a name that a local variable also has is changed when a function switches to the local", (t) => { - const lib = "export const x = 1;\n"; - const use = (name) => - `import { x as y } from "./lib";\nconst x = 2;\nexport function value() {\n return ${name};\n}\n`; - assert.equal( - changedAll( - t, - { "src/lib.ts": lib, "src/a.ts": use("y") }, - { "src/lib.ts": lib, "src/a.ts": use("x") }, - ), - true, - ); -}); - -const NON_LITERAL_SPECIFIERS = [ - ["an identifier", "import x from foo;\nx();\n", "import x from bar;\nx();\n"], - [ - "a template without substitutions", - "import x from `./a`;\nx();\n", - "import x from `./b`;\nx();\n", - ], - ["a numeric literal", "import x from 1;\nx();\n", "import x from 2;\nx();\n"], - ["an export-star identifier", "export * from foo;\n", "export * from bar;\n"], - [ - "a named re-export identifier", - "export { a } from foo;\n", - "export { a } from bar;\n", - ], - ["a call", "import x from a();\nx();\n", "import x from b();\nx();\n"], - [ - "a template with substitutions", - "import x from `./${a}`;\nx();\n", - "import x from `./${b}`;\nx();\n", - ], -]; - -for (const [kind, base, head] of NON_LITERAL_SPECIFIERS) { - await test(`retargeting a module specifier written as ${kind} is changed`, (t) => { - assert.equal(changed(t, { "src/a.ts": base }, { "src/a.ts": head }), true); - }); -} - -await test("retargeting an identifier specifier in a Vue script is changed", (t) => { - const vue = (specifier) => - `\n\n`; - assert.equal( - changed(t, { "src/a.vue": vue("foo") }, { "src/a.vue": vue("bar") }, [ - "src/a.vue", - ]), - true, - ); -}); - -const SAME_TEXT_SPECIFIERS = [ - [ - "a bare package against the same name as an identifier", - 'import ts from "typescript";\nts.version;\n', - "import ts from typescript;\nts.version;\n", - ], - [ - "a bare package against the same name as a template", - 'import ts from "typescript";\nts.version;\n', - "import ts from `typescript`;\nts.version;\n", - ], - [ - "a quoted number against the number", - 'import x from "1";\nx();\n', - "import x from 1;\nx();\n", - ], - [ - "a quoted relative path against the same text as a template", - 'import x from "./a";\nx();\n', - "import x from `./a`;\nx();\n", - ], -]; - -for (const [kind, base, head] of SAME_TEXT_SPECIFIERS) { - await test(`${kind} is changed`, (t) => { - assert.equal(changed(t, { "src/a.ts": base }, { "src/a.ts": head }), true); - }); -} - -const EXPRESSION_SPECIFIERS = [ - ["a call expression", "import x from a();\nx();\n"], - [ - "a template literal with substitutions", - "import x from `./${name}`;\nx();\n", - ], -]; - -for (const [kind, expression] of EXPRESSION_SPECIFIERS) { - await test(`a module specifier that is ${kind} is changed against a string on either commit`, (t) => { - const valid = 'import x from "./a";\nx();\n'; - assert.equal( - changed(t, { "src/a.ts": valid }, { "src/a.ts": expression }), - true, - ); - assert.equal( - changed(t, { "src/a.ts": expression }, { "src/a.ts": valid }), - true, - ); - }); -} - -await test("reformatting a module specifier that is an expression is unchanged", (t) => { - assert.equal( - changed( - t, - { "src/a.ts": "import x from a('1');\nx();\n" }, - { "src/a.ts": 'import x from (a( "1" ));\nx();\n' }, - ), - false, - ); -}); diff --git a/packages/dms/bin/control/verify/decode.mjs b/packages/dms/bin/control/verify/decode.mjs index 56344b183..c6b520afd 100644 --- a/packages/dms/bin/control/verify/decode.mjs +++ b/packages/dms/bin/control/verify/decode.mjs @@ -1,121 +1,23 @@ -import { existsSync, readFileSync } from "node:fs"; import { arrayOf, decode, - either, instanceOf, - isBoolean, - isNumber, isString, nullable, - oneOf, recordOf, objectOf, } from "../json.mjs"; /** - * Guards for the JSON the verification harness reads back: results it wrote, - * the verifier's judgement, the statuses GitHub answers and the skill's - * config. Each is checked against the typedef it narrows to, and a value - * that does not match is refused with an error that names where it was read. + * Guard for the JSON the verification harness reads back, the skill's + * config. It is checked against the typedef it narrows to, and a value that + * does not match is refused with an error that names where it was read. * * @typedef {import("../json.mjs").Guard} AnyGuard */ const strings = arrayOf(isString); -const rules = objectOf({ - exitCode: nullable(isNumber), - violated: strings, - passing: strings, -}); - -/** @type {import("../json.mjs").Guard} The result `runFormalCheck` records in `result.json` */ -export const isFormalResult = objectOf( - { - name: isString, - exitCode: nullable(isNumber), - violated: strings, - passing: strings, - folder: isString, - }, - { - commits: objectOf({ baseSha: isString, headSha: isString }), - behaviour: objectOf({ - method: isString, - changed: isBoolean, - removed: strings, - added: strings, - }), - checkChanged: isBoolean, - base: nullable(rules), - fixed: strings, - dropped: strings, - missing: strings, - }, -); - -/** @type {import("../json.mjs").Guard} The journey results a run records in `/feature.json` */ -const isFeatureResults = arrayOf( - objectOf( - { - feature: isString, - journey: isString, - title: isString, - entryPoint: isString, - status: oneOf(["PASS", "FAIL"]), - database: isString, - steps: arrayOf( - objectOf({ - name: isString, - checks: arrayOf( - objectOf({ name: isString, pass: isBoolean, detail: isString }), - ), - }), - ), - error: nullable(isString), - folder: isString, - }, - { ms: isNumber }, - ), -); - -/** @type {import("../json.mjs").Guard} The verifier's `judgement.json` */ -export const isRawJudgement = objectOf( - {}, - { - verifier: isString, - notVerified: strings, - attention: strings, - docs: arrayOf( - objectOf({ page: isString }, { matches: isBoolean, note: isString }), - ), - doors: objectOf( - {}, - { - none: isString, - rows: arrayOf( - objectOf( - {}, - { - kind: isString, - location: isString, - irreversible: isString, - containment: isString, - }, - ), - ), - falsePositives: arrayOf( - objectOf( - {}, - { kind: isString, location: isString, reason: isString }, - ), - ), - }, - ), - }, -); - const isRegexOrNull = nullable(instanceOf(RegExp)); const isFormalCheck = objectOf( @@ -124,16 +26,8 @@ const isFormalCheck = objectOf( covers: strings, command: strings, violation: isRegexOrNull, - passing: isRegexOrNull, - }, - { - files: strings, - baseCommand: strings, - behaviour: either( - objectOf({ kind: oneOf(["emit"]) }), - objectOf({ kind: oneOf(["probe"]), command: strings }), - ), }, + { files: strings }, ); const isDoorRule = objectOf({ @@ -181,37 +75,3 @@ export function decodeSkillConfig(exported, source) { notShared: config.notShared ?? [], }; } - -/** - * @param {unknown} stored What `result.json` holds - * @param {string} source The file - * @returns {import("./types.mjs").FormalResult} The result - * @throws When the value is not a formal check result - */ -export function decodeFormalResult(stored, source) { - return decode(isFormalResult, stored, source, "a formal check result"); -} - -/** - * @param {unknown} stored What `judgement.json` holds - * @param {string} source The file - * @returns {import("./types.mjs").RawJudgement} The judgement - * @throws When the value is not a judgement - */ -export function decodeRawJudgement(stored, source) { - return decode(isRawJudgement, stored, source, "a verifier's judgement"); -} - -/** - * The journey results a run recorded for a feature. - * - * @param {string} file The feature's `feature.json` - * @returns {import("./types.mjs").FeatureResult[]} The results, none when the journeys did not run - * @throws When the file is not a list of journey results - */ -export function readFeatureResults(file) { - if (!existsSync(file)) return []; - /** @type {unknown} */ - const stored = JSON.parse(readFileSync(file, "utf8")); - return decode(isFeatureResults, stored, file, "a list of journey results"); -} diff --git a/packages/dms/bin/control/verify/decode.test.mjs b/packages/dms/bin/control/verify/decode.test.mjs index 6f88e6752..423582266 100644 --- a/packages/dms/bin/control/verify/decode.test.mjs +++ b/packages/dms/bin/control/verify/decode.test.mjs @@ -4,9 +4,6 @@ import { mkdirSync, mkdtempSync, rmSync, writeFileSync } from "node:fs"; import { tmpdir } from "node:os"; import { join } from "node:path"; import test from "node:test"; -import { readFeatureResults } from "./decode.mjs"; -import { readFormalResult } from "./formal.mjs"; -import { readJudgement } from "./judgement.mjs"; import { loadSkill } from "./skill.mjs"; /** @@ -19,103 +16,6 @@ function scratch(t) { return dir; } -/** - * @param {string} file The file - * @param {unknown} value What it holds as JSON - */ -function writeJson(file, value) { - mkdirSync(join(file, ".."), { recursive: true }); - writeFileSync(file, JSON.stringify(value)); -} - -const FORMAL = { - name: "formal-add", - exitCode: 0, - violated: [], - passing: ["AddWorks"], - folder: "formal/formal-add", -}; - -await test("a formal result is read as recorded", (t) => { - const evidence = scratch(t); - writeJson(join(evidence, "formal", "formal-add", "result.json"), { - ...FORMAL, - base: null, - fixed: [], - }); - assert.deepEqual(readFormalResult({ evidence }, "formal-add"), { - ...FORMAL, - base: null, - fixed: [], - }); - assert.equal(readFormalResult({ evidence }, "formal-other"), undefined); -}); - -await test("a formal result whose rules are no list is refused", (t) => { - const evidence = scratch(t); - writeJson(join(evidence, "formal", "formal-add", "result.json"), { - ...FORMAL, - violated: "AddWorks", - }); - assert.throws( - () => readFormalResult({ evidence }, "formal-add"), - /result\.json is malformed: expected a formal check result/, - ); -}); - -const RESULT = { - feature: "smoke", - journey: "onboard", - title: "Onboard", - entryPoint: "/", - status: "PASS", - database: "dms_run", - steps: [ - { name: "open", checks: [{ name: "loads", pass: true, detail: "" }] }, - ], - error: null, - folder: "smoke/onboard", -}; - -await test("journey results are read as recorded and none when absent", (t) => { - const dir = scratch(t); - assert.deepEqual(readFeatureResults(join(dir, "feature.json")), []); - writeJson(join(dir, "feature.json"), [RESULT]); - assert.deepEqual(readFeatureResults(join(dir, "feature.json")), [RESULT]); -}); - -await test("a journey result keeps its duration, and one whose duration is no number is refused", (t) => { - const dir = scratch(t); - writeJson(join(dir, "feature.json"), [{ ...RESULT, ms: 4200 }]); - assert.equal(readFeatureResults(join(dir, "feature.json"))[0].ms, 4200); - writeJson(join(dir, "feature.json"), [{ ...RESULT, ms: "4.2s" }]); - assert.throws( - () => readFeatureResults(join(dir, "feature.json")), - /malformed: expected a list of journey results/, - ); -}); - -await test("a journey result with a status that is neither PASS nor FAIL is refused", (t) => { - const dir = scratch(t); - writeJson(join(dir, "feature.json"), [{ ...RESULT, status: "PASSED" }]); - assert.throws( - () => readFeatureResults(join(dir, "feature.json")), - /feature\.json is malformed: expected a list of journey results/, - ); -}); - -await test("a journey result whose check does not say pass or fail is refused", (t) => { - const dir = scratch(t); - const steps = [ - { name: "open", checks: [{ name: "loads", pass: "yes", detail: "" }] }, - ]; - writeJson(join(dir, "feature.json"), [{ ...RESULT, steps }]); - assert.throws( - () => readFeatureResults(join(dir, "feature.json")), - /malformed: expected a list of journey results/, - ); -}); - const CONFIG = `export default { playground: "playground", prepare: [["pnpm", "install"]], @@ -205,7 +105,7 @@ await test("a config whose formal check pattern is a string is refused", async ( t, CONFIG.replace( "formal: []", - 'formal: [{ name: "f", covers: [], command: ["node"], violation: "^x", passing: null }]', + 'formal: [{ name: "f", covers: [], command: ["node"], violation: "^x" }]', ), ); await assert.rejects( @@ -221,51 +121,3 @@ await test("a config module without a default export is refused", async (t) => { /verify\.config\.ts is malformed/, ); }); - -const JUDGEMENT = { - verifier: "thread", - notVerified: ["ui"], - attention: [], - docs: [{ page: "docs/a.md", matches: true, note: "ok" }], - doors: { none: "no door" }, -}; - -await test("a judgement is read into the verdict's shape", async (t) => { - const skill = await loadSkill(skillFolder(t, CONFIG), tmpdir()); - const file = join(scratch(t), "judgement.json"); - writeJson(file, JUDGEMENT); - assert.deepEqual(readJudgement(skill, file), { - notVerified: ["ui"], - attention: [], - docs: [{ page: "docs/a.md", matches: true, note: "ok" }], - doorsNone: "no door", - doors: [], - falsePositives: [], - verifier: "thread", - }); -}); - -await test("a judgement whose lists hold other than strings is refused", async (t) => { - const skill = await loadSkill(skillFolder(t, CONFIG), tmpdir()); - const file = join(scratch(t), "judgement.json"); - writeJson(file, { ...JUDGEMENT, notVerified: "ui" }); - assert.throws( - () => readJudgement(skill, file), - /judgement\.json is malformed: expected a verifier's judgement/, - ); - writeJson(file, { - ...JUDGEMENT, - docs: [{ page: "docs/a.md", matches: "yes" }], - }); - assert.throws( - () => readJudgement(skill, file), - /malformed: expected a verifier/, - ); -}); - -await test("a judgement that is no object is refused", async (t) => { - const skill = await loadSkill(skillFolder(t, CONFIG), tmpdir()); - const file = join(scratch(t), "judgement.json"); - writeJson(file, ["verifier"]); - assert.throws(() => readJudgement(skill, file), /is malformed/); -}); diff --git a/packages/dms/bin/control/verify/emit.mjs b/packages/dms/bin/control/verify/emit.mjs deleted file mode 100644 index 98b3840de..000000000 --- a/packages/dms/bin/control/verify/emit.mjs +++ /dev/null @@ -1,539 +0,0 @@ -import { spawnSync } from "node:child_process"; -import { createRequire } from "node:module"; -import { dirname, extname, join, posix } from "node:path"; -import { PACKAGE_ROOT } from "../launch.mjs"; - -/** @typedef {typeof import("typescript")} TypeScript */ -/** @typedef {import("typescript").Node} Node */ -/** @typedef {{ modules: Set, functions: Set }} Covered The covered modules and function names */ -/** @typedef {{ structure: string, text: string, hoisted: boolean }} Item */ -/** @typedef {{ path: string, file: import("typescript").SourceFile }} Script */ - -const CODE_EXTENSIONS = new Set([ - ".ts", - ".mts", - ".cts", - ".tsx", - ".js", - ".mjs", - ".cjs", - ".jsx", -]); -const OUTPUT_LIMIT = 64 * 1024 * 1024; -const TEXT_LENGTH = 160; - -/** - * @param {string} repo - * @returns {TypeScript} - */ -function loadTypeScript(repo) { - for (const folder of [repo, join(repo, "packages", "dms"), PACKAGE_ROOT]) { - try { - // oxlint-disable-next-line typescript/no-unsafe-return -- createRequire loads TypeScript by a path known only at run time, from the checkout under test first and the dms package last, and Node types what require answers as any. - return createRequire(join(folder, "package.json"))("typescript"); - } catch { - continue; - } - } - throw new Error( - "dms-control needs the typescript package in the repository to compare covered code", - ); -} - -/** - * @param {string} repo - * @param {string} sha - * @param {string} path - * @returns {string | null} - */ -function fileAt(repo, sha, path) { - const shown = spawnSync("git", ["show", `${sha}:${path}`], { - cwd: repo, - encoding: "utf8", - maxBuffer: OUTPUT_LIMIT, - }); - return shown.status === 0 ? shown.stdout : null; -} - -/** - * @param {TypeScript} ts - * @param {Node} node - * @param {Map} bindings - * @returns {string} - */ -function leafOf(ts, node, bindings) { - if (ts.isIdentifier(node)) - return propertyName(ts, node) - ? node.text - : (bindings.get(node.text) ?? node.text); - if (ts.isPrivateIdentifier(node)) return node.text; - if (ts.isNumericLiteral(node)) return String(Number(node.text)); - if (ts.isStringLiteralLike(node) || ts.isTemplateLiteralToken(node)) - return JSON.stringify(node.text); - if (ts.isBigIntLiteral(node) || ts.isRegularExpressionLiteral(node)) - return node.text; - if (ts.isPrefixUnaryExpression(node) || ts.isPostfixUnaryExpression(node)) - return ts.SyntaxKind[node.operator]; - if (ts.isVariableDeclarationList(node)) - return String(node.flags & (ts.NodeFlags.Let | ts.NodeFlags.Const)); - if (ts.isMetaProperty(node)) return ts.SyntaxKind[node.keywordToken]; - return ""; -} - -const MODULE_EXTENSION = /\.(?:[cm]?[jt]s|[jt]sx)$/; - -/** - * The module a specifier names. Only a string literal names a module; any - * other expression, which only a file that does not parse as a module can - * hold, is keyed by its structure, so it never reads as the module its text - * spells. - * - * @param {TypeScript} ts - * @param {string} path The file that holds the specifier - * @param {import("typescript").Expression} specifier - * @returns {string} - */ -function moduleOf(ts, path, specifier) { - return ts.isStringLiteral(specifier) - ? moduleKey(path, specifier.text) - : `expression ${structureOf(ts, specifier, new Map())}`; -} - -/** - * @param {string} path - * @param {string} specifier The text of the module specifier - * @returns {string} - */ -function moduleKey(path, specifier) { - if (!specifier.startsWith(".")) return specifier; - return posix.join(dirname(path), specifier).replace(MODULE_EXTENSION, ""); -} - -/** - * @param {string} path - * @returns {string} - */ -function stripExtension(path) { - return path.replace(MODULE_EXTENSION, ""); -} - -/** - * @param {TypeScript} ts - * @param {Node} node - * @returns {boolean} - */ -function propertyName(ts, node) { - const parent = node.parent; - return ( - parent !== undefined && - parent.name === node && - (ts.isPropertyAccessExpression(parent) || - ts.isPropertyAssignment(parent) || - ts.isMethodDeclaration(parent) || - ts.isPropertyDeclaration(parent) || - ts.isGetAccessor(parent) || - ts.isSetAccessor(parent) || - ts.isEnumMember(parent)) - ); -} - -/** - * A binding from a covered module is its name alone when it is a function - * declaration, which may move between covered files, and `module#name` - * otherwise, so a variable or class of one file never reads as one of the same - * name in another. - * - * @param {Covered} covered The covered modules and function names - * @param {string} module The resolved module - * @param {string} name The imported name - * @returns {string} The origin - */ -function bindingOrigin(covered, module, name) { - return covered.modules.has(module) && covered.functions.has(name) - ? name - : `${module}#${name}`; -} - -/** - * @param {TypeScript} ts - * @param {import("typescript").ImportClause} clause - * @param {string} module - * @param {Covered} covered - * @returns {[string, string][]} - */ -function importedBindings(ts, clause, module, covered) { - const named = clause.namedBindings; - /** @type {[string, string][]} */ - const bindings = []; - if (clause.name) - bindings.push([ - clause.name.text, - bindingOrigin(covered, module, "default"), - ]); - if (named && ts.isNamespaceImport(named)) - bindings.push([named.name.text, bindingOrigin(covered, module, "*")]); - if (named && ts.isNamedImports(named)) - for (const element of named.elements) - bindings.push([ - element.name.text, - bindingOrigin( - covered, - module, - (element.propertyName ?? element.name).text, - ), - ]); - return bindings; -} - -/** - * @param {TypeScript} ts - * @param {import("typescript").ExportDeclaration} statement - * @param {string} module - * @param {Covered} covered - * @returns {string[]} - */ -function reexportTexts(ts, statement, module, covered) { - const exported = statement.exportClause; - const inside = covered.modules.has(module); - if (exported === undefined) return inside ? [] : [`export * from ${module}`]; - if (ts.isNamespaceExport(exported)) - return inside ? [] : [`export * as ${exported.name.text} from ${module}`]; - return exported.elements.flatMap((element) => { - const origin = bindingOrigin( - covered, - module, - (element.propertyName ?? element.name).text, - ); - return origin === element.name.text - ? [] - : [`export ${origin} as ${element.name.text}`]; - }); -} - -/** - * The exports of a file that name a local or imported binding, keyed by the - * origin of what they export. An export under its own name of a local needs no - * entry, the same as an `export` keyword, so only a renamed local and any - * imported binding does. - * @param {TypeScript} ts - * @param {import("typescript").ExportDeclaration} statement - * @param {Map} bindings - * @param {Map} locals - * @returns {string[]} - */ -function localExportTexts(ts, statement, bindings, locals) { - const exported = statement.exportClause; - if (exported === undefined || !ts.isNamedExports(exported)) return []; - return exported.elements.flatMap((element) => { - const name = (element.propertyName ?? element.name).text; - const imported = bindings.get(name); - const origin = imported ?? locals.get(name) ?? name; - const own = imported === undefined && name === element.name.text; - return own || origin === element.name.text - ? [] - : [`export ${origin} as ${element.name.text}`]; - }); -} - -/** - * Where each value-imported binding of a file comes from: the resolved - * module and the imported name, so an alias or a swapped specifier changes - * what a use refers to. A binding from a covered module is its name alone - * when it is a function declaration, so moving the declaration between - * covered files keeps every use the same; any other binding is keyed by its - * module. Side-effect imports keep their order in the file. Re-exports and - * renamed or imported exports become entries keyed by the origin they export. - * - * @param {TypeScript} ts The TypeScript module - * @param {string} path The file - * @param {Covered} covered The covered modules and function names - * @param {import("typescript").SourceFile} file The emitted source file - * @param {Map} locals The top-level variables and classes of the file, keyed by file and name - * @returns {{ bindings: Map, entries: { structure: string, text: string, hoisted: boolean }[], ordered: { structure: string, text: string, hoisted: boolean }[] }} The origins; `entries` may move freely, `ordered` keep their order in the file - */ -function originsOf(ts, path, covered, file, locals) { - /** @type {Map} */ - const bindings = new Map(); - /** @type {string[]} */ - const texts = []; - /** @type {string[]} */ - const ordered = []; - /** @type {import("typescript").ExportDeclaration[]} */ - const exports = []; - for (const statement of file.statements) { - if ( - !(ts.isImportDeclaration(statement) || ts.isExportDeclaration(statement)) - ) - continue; - const specifier = statement.moduleSpecifier; - if (specifier === undefined && ts.isExportDeclaration(statement)) - exports.push(statement); - if (specifier === undefined) continue; - const module = moduleOf(ts, path, specifier); - if (ts.isImportDeclaration(statement)) { - if (statement.importClause === undefined) { - ordered.push(`import ${module}`); - continue; - } - for (const [name, origin] of importedBindings( - ts, - statement.importClause, - module, - covered, - )) - bindings.set(name, origin); - } else texts.push(...reexportTexts(ts, statement, module, covered)); - } - for (const statement of exports) - texts.push(...localExportTexts(ts, statement, bindings, locals)); - /** - * @param {string} text - * @param {boolean} hoisted - * @returns {Item} - */ - const item = (text, hoisted) => ({ structure: text, text, hoisted }); - return { - bindings, - entries: texts.map((text) => item(text, true)), - ordered: ordered.map((text) => item(text, false)), - }; -} - -/** - * @param {TypeScript} ts - * @param {Node} node - * @returns {boolean} - */ -function dropped(ts, node) { - return ( - node.kind === ts.SyntaxKind.ExportKeyword || - node.kind === ts.SyntaxKind.DefaultKeyword - ); -} - -/** - * The structure of a node: its kind, its identifier name or literal value, - * and its children's structure, with parentheses, positions, trivia, quote - * style and line breaks left out, so formatting never changes it. - * - * @param {TypeScript} ts The TypeScript module - * @param {Node} node A node of the emitted JavaScript - * @param {Map} bindings The origin of each imported binding - * @returns {string} The structure - */ -function structureOf(ts, node, bindings) { - if (ts.isParenthesizedExpression(node)) - return structureOf(ts, node.expression, bindings); - /** @type {string[]} */ - const children = []; - ts.forEachChild(node, (child) => { - if (!dropped(ts, child)) children.push(structureOf(ts, child, bindings)); - }); - return `${ts.SyntaxKind[node.kind]}(${leafOf(ts, node, bindings)}|${children.join(",")})`; -} - -/** - * @param {TypeScript} ts - * @param {string} path - * @param {string} source - * @returns {Script} - */ -function parseScript(ts, path, source) { - const { outputText } = ts.transpileModule(source, { - fileName: path, - compilerOptions: { - target: ts.ScriptTarget.ES2022, - module: ts.ModuleKind.ESNext, - removeComments: true, - experimentalDecorators: true, - }, - }); - return { - path, - file: ts.createSourceFile( - "emit.js", - outputText, - ts.ScriptTarget.ES2022, - true, - ts.ScriptKind.JS, - ), - }; -} - -/** - * @param {string} text - * @returns {string} - */ -function readable(text) { - const flat = text.replace(/\s+/g, " ").trim(); - return flat.length > TEXT_LENGTH ? `${flat.slice(0, TEXT_LENGTH)}…` : flat; -} - -/** - * @param {TypeScript} ts - * @param {import("typescript").BindingName} name - * @returns {string[]} - */ -function declaredNames(ts, name) { - return ts.isIdentifier(name) - ? [name.text] - : name.elements.flatMap((element) => - ts.isOmittedExpression(element) ? [] : declaredNames(ts, element.name), - ); -} - -/** - * @param {TypeScript} ts - * @param {import("typescript").Statement} statement - * @returns {statement is import("typescript").FunctionDeclaration & { name: import("typescript").Identifier }} - */ -function isFunctionDeclaration(ts, statement) { - return ( - ts.isFunctionDeclaration(statement) && - Boolean(statement.body) && - statement.name !== undefined - ); -} - -/** - * The top-level variables, classes and enums of a script, each keyed by its file and name: what a function moved into another file must not pick up by name. - * @param {TypeScript} ts - * @param {Script} script - * @returns {Map} - */ -function localsOf(ts, script) { - const file = stripExtension(script.path); - const names = script.file.statements.flatMap((statement) => { - if (ts.isVariableStatement(statement)) - return statement.declarationList.declarations.flatMap((declaration) => - declaredNames(ts, declaration.name), - ); - return (ts.isClassDeclaration(statement) || - ts.isEnumDeclaration(statement)) && - statement.name - ? [statement.name.text] - : []; - }); - return new Map(names.map((name) => [name, `${file}#${name}`])); -} - -/** - * @param {TypeScript} ts - * @param {Script} script - * @param {Covered} covered - * @returns {Item[]} - */ -function scriptItems(ts, script, covered) { - const locals = localsOf(ts, script); - const origins = originsOf(ts, script.path, covered, script.file, locals); - const inFunctions = new Map([...locals, ...origins.bindings]); - const statements = script.file.statements - .filter( - (statement) => - !ts.isImportDeclaration(statement) && - !ts.isExportDeclaration(statement), - ) - .map((statement) => { - const hoisted = - ts.isFunctionDeclaration(statement) && Boolean(statement.body); - return { - structure: structureOf( - ts, - statement, - hoisted ? inFunctions : origins.bindings, - ), - text: readable(statement.getText(script.file)), - hoisted, - }; - }); - return [...origins.entries, ...origins.ordered, ...statements]; -} - -/** - * A covered file as its scripts and the items that are not scripts: a Vue template, or the whole text of any other file. - * @param {TypeScript} ts - * @param {string} path - * @param {string} source - */ -function parsedFile(ts, path, source) { - if (path.endsWith(".vue")) { - const template = /]*>([\s\S]*)<\/template>/.exec(source); - const markup = template ? template[1].replace(/\s+/g, " ").trim() : null; - return { - path, - scripts: [...source.matchAll(/]*>([\s\S]*?)<\/script>/g)].map( - ([, body]) => parseScript(ts, `${path}.ts`, body), - ), - others: - markup === null - ? [] - : [ - { - structure: `template(${markup})`, - text: readable(`