From eef7169a9062e60fccef322cc5fd3e1ae669dbc8 Mon Sep 17 00:00:00 2001 From: Okiki Ojo Date: Tue, 1 Sep 2026 00:14:08 -0400 Subject: [PATCH] test(utils): define cross-package qualification contracts The workspace previously had package-local tests but no durable map from each published utility to its capability, runtime posture, scenario ownership, or performance responsibility. That made it easy to count tests while still missing the exact cancellation, cleanup, runtime, or composition edge that motivated the utility in Platform, Crawl, or MediaD. Add a 42-package qualification register, a fragile-test debt register, and an audit that verifies package/test inventory and rejects unreviewed wall-clock timing or snapshot matchers. Existing timing debt is named explicitly so later commits can remove it without making the initial quality gate depend on hidden exceptions. This makes qualification a repository contract rather than a one-off review: for example, adding a new utility without tests or silently adding an arbitrary setTimeout to a test now fails the same audit that protects package and release structure. --- .agents/scripts/audit-qualification.mjs | 80 +++++++++++++++++++++++++ package.json | 1 + qualification/README.md | 35 +++++++++++ qualification/fragile-allowlist.json | 1 + qualification/packages.json | 1 + 5 files changed, 118 insertions(+) create mode 100644 .agents/scripts/audit-qualification.mjs create mode 100644 qualification/README.md create mode 100644 qualification/fragile-allowlist.json create mode 100644 qualification/packages.json diff --git a/.agents/scripts/audit-qualification.mjs b/.agents/scripts/audit-qualification.mjs new file mode 100644 index 0000000..cca6921 --- /dev/null +++ b/.agents/scripts/audit-qualification.mjs @@ -0,0 +1,80 @@ +import { readdir, readFile, stat } from 'node:fs/promises'; +import { join } from 'node:path'; +import { fileURLToPath } from 'node:url'; + +const matrix = JSON.parse(await readFile(new URL('../../qualification/packages.json', import.meta.url), 'utf8')); +const packageRoot = fileURLToPath(new URL('../../packages/', import.meta.url)); +const allowlist = JSON.parse(await readFile(new URL('../../qualification/fragile-allowlist.json', import.meta.url), 'utf8')); +const allowed = new Map(allowlist.entries.map((entry) => [`${entry.path}:${entry.pattern}`, entry.reason])); +const errors = []; +const dirs = (await readdir(packageRoot, { withFileTypes: true })).filter((entry) => entry.isDirectory()).map((entry) => entry.name).sort(); +const expected = dirs.map((name) => `@okikio/${name}`); +const declared = Object.keys(matrix.packages).map((name) => `@okikio/${name}`).sort(); + +if (JSON.stringify(expected) !== JSON.stringify(declared)) { + errors.push(`qualification package inventory differs from workspace: expected ${expected.length}, declared ${declared.length}`); +} + +const fragilePatterns = [ + { name: 'wall-clock setTimeout', pattern: /\bsetTimeout\s*\(/ }, + { name: 'Date.now timing', pattern: /\bDate\.now\s*\(/ }, + { name: 'performance.now timing', pattern: /\bperformance\.now\s*\(/ }, + { name: 'snapshot matcher', pattern: /\btoMatch(?:Inline)?Snapshot\s*\(/ }, +]; + +async function files(dir) { + const output = []; + for (const entry of await readdir(dir, { withFileTypes: true })) { + const path = join(dir, entry.name); + if (entry.isDirectory()) output.push(...await files(path)); + else output.push(path); + } + return output; +} + +for (const [packageName, entry] of Object.entries(matrix.packages)) { + const name = `@okikio/${packageName}`; + if (!entry.capability || !Array.isArray(entry.scenarios) || entry.scenarios.length === 0) { + errors.push(`${name} requires a concrete capability and scenario group`); + } + const packageFiles = await files(join(packageRoot, packageName)); + const tests = packageFiles.filter((value) => /(?:_test|\.test)\.ts$/.test(value)); + if (tests.length === 0) errors.push(`${name} has no public test source`); + const runtimes = { ...matrix.defaults.runtimes, ...(entry.runtimes ?? {}) }; + if (runtimes.deno !== 'required') errors.push(`${name} must retain the Deno-first source contract`); + if (!entry.performance?.status) errors.push(`${name} requires a performance ownership status`); + for (const benchmark of entry.performance?.files ?? []) { + try { await stat(fileURLToPath(new URL(`../../${benchmark}`, import.meta.url))); } + catch { errors.push(`${name} registers missing benchmark ${benchmark}`); } + } +} + +for (const packageName of dirs) { + const packageFiles = await files(fileURLToPath(new URL(`../../packages/${packageName}/`, import.meta.url))); + for (const path of packageFiles.filter((value) => /(?:_test|\.test)\.ts$/.test(value))) { + const source = await readFile(path, 'utf8'); + const lines = source.split('\n'); + for (const check of fragilePatterns) { + for (let index = 0; index < lines.length; index += 1) { + if (!check.pattern.test(lines[index])) continue; + const relative = path.slice(fileURLToPath(new URL('../../', import.meta.url)).length); + const key = `${relative}:${check.pattern.source.includes('setTimeout') ? 'setTimeout' : check.name}`; + const allowance = lines.slice(Math.max(0, index - 2), index + 1).join('\n'); + if (allowance.includes('qualification-allow-timing:') || allowed.has(key)) continue; + errors.push(`${relative}:${index + 1}: ${check.name} requires explicit synchronization or a reviewed timing exception`); + } + } + } +} + +for (const entry of allowlist.entries) { + if (!entry.reason || entry.reason.length < 24) errors.push(`${entry.path}: fragile-test allowance requires a concrete reason`); +} + +if (errors.length > 0) { + console.error(`qualification audit failed with ${errors.length} issue(s):`); + for (const error of errors) console.error(`- ${error}`); + process.exitCode = 1; +} else { + console.log(`qualification audit passed: ${Object.keys(matrix.packages).length} packages, no unreviewed fragile-test patterns`); +} diff --git a/package.json b/package.json index 2650986..f9c9479 100644 --- a/package.json +++ b/package.json @@ -8,6 +8,7 @@ ], "license": "MIT", "scripts": { + "audit": "node .agents/scripts/audit-workspace.mjs && node .agents/scripts/audit-release.mjs && node .agents/scripts/audit-qualification.mjs", "bump": "bumpy add", "bump:check": "bumpy check --strict", "release:test": "node --test .bumpy/*.test.mjs", diff --git a/qualification/README.md b/qualification/README.md new file mode 100644 index 0000000..59a36ef --- /dev/null +++ b/qualification/README.md @@ -0,0 +1,35 @@ +# Qualification model + +This directory records what the utility workspace claims and how those claims are verified. +It is not a coverage-percentage dashboard. A package can have many executed lines and still miss +the failure, cancellation, ownership, or runtime boundary that matters in production. + +The repository uses three complementary test views: + +- **capability tests** prove the public programming model a package claims to expose; +- **behavior tests** prove observable happy, failure, cancellation, limit, and cleanup contracts; +- **scenario tests** compose utilities the way real consumers such as Kaiju Platform, Kaiju Crawl, + and MediaD used them. + +`packages.json` is the machine-readable register. It records every publishable package, its runtime +posture, existing package tests, representative scenario groups, and benchmark ownership. Runtime +values are conservative: `candidate` is not a compatibility claim. + +## Fragile-test rule + +Canonical tests must synchronize on observable state, messages, promises, test clocks, or explicit +latches. They must not sleep for an arbitrary wall-clock duration to "give work time" to happen. +OS integration tests may poll an external process only when no event/handle can express the condition; +such cases require an inline `qualification-allow-timing:` explanation so the exception remains visible. + +The qualification audit also rejects snapshot-test APIs for contracts that are clearer as structural +assertions. Durable *data snapshots* such as `record.snapshot()` and `context.snapshot()` are ordinary +public behavior and are not snapshot testing. + +## Performance rule + +A benchmark exists to answer a physical performance question. A small definition-only package does +not need a fake microbenchmark merely to satisfy a count. Packages with meaningful hot paths either +own a direct benchmark or participate in a representative scenario benchmark. Every benchmark must +consume its result, keep setup outside the measured operation where practical, and state the workload +it represents. diff --git a/qualification/fragile-allowlist.json b/qualification/fragile-allowlist.json new file mode 100644 index 0000000..c8c4e33 --- /dev/null +++ b/qualification/fragile-allowlist.json @@ -0,0 +1 @@ +{"schemaVersion":1,"entries":[{"path":"packages/concurrency/src/bounded.test.ts","pattern":"setTimeout","reason":"Known baseline debt: overlap is asserted through a 2 ms sleep instead of an admission latch; remove in the runtime-synchronization commit."},{"path":"packages/process/channel/mod_test.ts","pattern":"setTimeout","reason":"Known baseline debt: protocol progression yields through a zero-delay timer; replace with explicit protocol synchronization."},{"path":"packages/process/node_test.ts","pattern":"setTimeout","reason":"OS integration exception: descendant is not exposed as a ChildProcess handle, so the process table is polled until the process group is reaped."},{"path":"packages/worker/mod_test.ts","pattern":"setTimeout","reason":"Known baseline debt: fake Worker delivery yields through a zero-delay timer; replace with explicit message-state synchronization."}]} diff --git a/qualification/packages.json b/qualification/packages.json new file mode 100644 index 0000000..b473187 --- /dev/null +++ b/qualification/packages.json @@ -0,0 +1 @@ +{"schemaVersion":1,"defaults":{"runtimes":{"deno":"required","node":"required","bun":"candidate","browser":"candidate"}},"packages":{"activity":{"capability":"declared activity contracts and provider placement","scenarios":["durable-workflow"],"performance":{"status":"scenario-or-na"}},"capacity":{"capability":"bounded admission and lease accounting","scenarios":["bounded-coordination"],"performance":{"status":"scenario-or-na"}},"catalog":{"capability":"declarative definition catalogs","scenarios":["definition-composition"],"performance":{"status":"scenario-or-na"}},"codec":{"capability":"representation conversion contracts","scenarios":["data-boundaries"],"performance":{"status":"scenario-or-na"}},"concurrency":{"capability":"bounded pooled concurrency and channels","scenarios":["bounded-coordination"],"performance":{"status":"scenario-or-na"}},"context":{"capability":"cancellation, deadlines, clocks, and lifecycle context","scenarios":["runtime-lifecycle"],"performance":{"status":"scenario-or-na"}},"css":{"capability":"CSS parsing and resource inspection","scenarios":["web-parsing"],"performance":{"status":"scenario-or-na"}},"csv":{"capability":"bounded collecting and streaming CSV parsing","scenarios":["crm-import"],"performance":{"status":"direct","files":["packages/csv/parse_bench.ts"]}},"deno":{"capability":"Deno permissions, signals, files, and ports","scenarios":["runtime-adapters"],"runtimes":{"node":"not-claimed","browser":"not-applicable"},"performance":{"status":"scenario-or-na"}},"dispose":{"capability":"deterministic synchronous and asynchronous cleanup","scenarios":["runtime-lifecycle"],"performance":{"status":"scenario-or-na"}},"duration":{"capability":"Temporal duration normalization and conversion","scenarios":["durable-workflow"],"performance":{"status":"scenario-or-na"}},"effect":{"capability":"declared side-effect contracts and outboxes","scenarios":["failure-and-effects"],"performance":{"status":"scenario-or-na"}},"email":{"capability":"email address and classification mechanics","scenarios":["data-boundaries"],"performance":{"status":"scenario-or-na"}},"entitlement":{"capability":"generic entitlement definitions and evaluation","scenarios":["definition-composition"],"performance":{"status":"scenario-or-na"}},"env":{"capability":"typed environment source resolution","scenarios":["configuration"],"performance":{"status":"direct","files":["packages/env/mod_bench.ts"]}},"failure":{"capability":"declared structured failures and serialization","scenarios":["failure-and-effects"],"performance":{"status":"scenario-or-na"}},"fault":{"capability":"bounded diagnostic fault encoding","scenarios":["failure-and-effects"],"performance":{"status":"scenario-or-na"}},"hash":{"capability":"portable hashing and CRC32C adapters","scenarios":["data-boundaries"],"runtimes":{"browser":"not-claimed"},"performance":{"status":"scenario-or-na"}},"hono":{"capability":"Hono integration adapters","scenarios":["http-services"],"runtimes":{"browser":"not-applicable"},"performance":{"status":"scenario-or-na"}},"html":{"capability":"WHATWG HTML parsing and DOM inspection","scenarios":["web-parsing"],"performance":{"status":"scenario-or-na"}},"http":{"capability":"HTTP cookies, problems, requests, responses, and body ownership","scenarios":["http-services"],"performance":{"status":"scenario-or-na"}},"meter":{"capability":"generic metering contracts","scenarios":["definition-composition"],"performance":{"status":"scenario-or-na"}},"permission":{"capability":"generic permission contracts","scenarios":["definition-composition"],"performance":{"status":"scenario-or-na"}},"pool":{"capability":"bounded reusable resource pools","scenarios":["bounded-coordination"],"performance":{"status":"direct","files":["packages/pool/mod_bench.ts"]}},"process":{"capability":"cross-runtime process control and process channels","scenarios":["process-orchestration"],"runtimes":{"browser":"not-applicable"},"performance":{"status":"scenario-or-na"}},"query":{"capability":"generic query definitions and execution contracts","scenarios":["definition-composition"],"performance":{"status":"scenario-or-na"}},"queue":{"capability":"bounded in-memory queue mechanics","scenarios":["bounded-coordination"],"performance":{"status":"direct","files":["packages/queue/mod_bench.ts"]}},"record":{"capability":"safe plain-record inspection and snapshots","scenarios":["data-boundaries"],"performance":{"status":"scenario-or-na"}},"requirement":{"capability":"declarative requirement contracts","scenarios":["definition-composition"],"performance":{"status":"scenario-or-na"}},"resilience":{"capability":"timeouts, retries, admission, and resilience policies","scenarios":["http-services"],"performance":{"status":"scenario-or-na"}},"resource":{"capability":"resource definitions, selection, composition, and scopes","scenarios":["runtime-lifecycle"],"performance":{"status":"scenario-or-na"}},"result":{"capability":"success/failure result algebra","scenarios":["failure-and-effects"],"performance":{"status":"scenario-or-na"}},"robots":{"capability":"robots.txt parsing and policy evaluation","scenarios":["web-parsing"],"performance":{"status":"scenario-or-na"}},"schema":{"capability":"Standard Schema and Zod integration mechanics","scenarios":["data-boundaries"],"performance":{"status":"scenario-or-na"}},"server":{"capability":"HTTP endpoint, middleware, gateway, and service composition","scenarios":["http-services"],"runtimes":{"browser":"not-applicable"},"performance":{"status":"scenario-or-na"}},"sitemap":{"capability":"sitemap parsing and URL discovery","scenarios":["web-parsing"],"performance":{"status":"scenario-or-na"}},"streams":{"capability":"stream, iterable, line, pipe, collect, and batch mechanics","scenarios":["streaming"],"performance":{"status":"scenario-or-na"}},"task":{"capability":"local structured task lifecycle","scenarios":["runtime-lifecycle"],"performance":{"status":"scenario-or-na"}},"utils":{"capability":"single-install umbrella and focused subpath inventory","scenarios":["selective-adoption"],"performance":{"status":"scenario-or-na"}},"version":{"capability":"version parsing and comparison mechanics","scenarios":["data-boundaries"],"performance":{"status":"scenario-or-na"}},"worker":{"capability":"typed worker request/reply protocol and lifecycle","scenarios":["worker-orchestration"],"runtimes":{"browser":"not-claimed"},"performance":{"status":"scenario-or-na"}},"workflow":{"capability":"durable workflow instructions, history, scheduling, and replay","scenarios":["durable-workflow"],"performance":{"status":"scenario-or-na"}}}} -- knotmirror 0.0.1