Skip to content

Commit 1aa9c57

Browse files
refactor(lint): split rule-explanations.ts into one file per rule family (#38808)
<!-- claude-projects-attribution: {"github_login":"huangyiirene"} --> _Requested by **Irene** · [project thread](https://claude.ai/code/project/chan_01TJXtQ1tVD26ecLDZxFS67x?thread=cmsg_01TJXtQ1tVD26ecLDZxFS67xAvpYyRcBsXXYFFKdX4WDo1)_ Fixes #38666 Clause-②: no Metadata-protocol: none ## 维护者速读 - 用户无感:命令行解释规则时打印的文字一字不变,只是存放这些解释的源文件从一个七千多行的大文件拆成按规则族分开的小文件。 - 为什么做:这是全仓最多人同时修改的文件,属于您批准的大文件拆分计划的第三张卡,拆开后各张加规则的卡不再互相冲突。 - 出错会怎样:最坏是某条规则的解释文字变了或丢了;已逐条比对全部规则的输出完全相同,出问题可直接整体撤回本合并。 - 要你做什么:变更超过三千行,并改了两份设计决策文档里的文件路径指针,需要您批准合并。 ## What changes - `packages/lint/src/rule-explanations.ts` was one 7,680-line table. It is now the index: the module docblock and the `RuleExplanation` interface (both verbatim), the family imports, `RULE_EXPLANATIONS` (the same object literal, so the same keys in the same insertion order) and `explainRule` (verbatim). A comment above the imports says a new entry goes into its family's file and a new family gets its own file. - `packages/lint/src/rule-explanations/` holds one file per family: one per `// ──` section header of the old file (65), plus `field-consumers.ts` for `field-no-consumers`, which sat above the first header. Each entry moved verbatim; the only edit on a moved line is `export` in front of its `const`. - `rule-explanations/shared.ts` holds the three paragraphs more than one family references (`UNPROVISIONED_ANCHOR_CAUSE` and `UNPROVISIONED_ANCHOR_REMEDY`, used by 7 families, and `AGGREGATE_PAIR_ROWS`, used by 2), moved with their docblocks. - Two entries moved to where their header says they belong: `security-owd-unset` (above the first header) into `security-posture.ts`, the family of the rule file that produces it, and `seed-value-outside-state-machine` (inside the assignment-rules section) under its own header, which was empty, in `seed-state-machine.ts`. - `rule-explanations.test.ts`: only the case "the module imports nothing" changes, to the split's equivalent invariant (claim amendment 7295823844): the index imports only `./rule-explanations/*.js` as values; every family file reached from it imports only siblings, plus `RuleExplanation` from the index as `import type`; no file re-exports with export-from. Every other lint test is byte-unchanged. - `docs/adr/0055-…md` and `docs/adr/0141-…md`: one symbol anchor each re-pointed from `rule-explanations.ts#NAME` to the family file that now declares `NAME`, path only, sentence unchanged (claim amendment 7296411486). `check:adr-symbol-anchors` reds without it. - A `patch` changeset for `@objectstack/lint`: the bundled `rule-explanations` and `index` entries change bytes (module order, source-path comments), with no behaviour or declaration change. - No file in the new set is over 2,000 lines: the largest is `record-writes.ts` at 623, and the index is 745. There is no `facade-delegation-registry.json` row: this is a table, not a facade. ## Tests <details><summary><code>os explain</code> for every rule id: 292 of 292 byte-identical</summary> The CLI was built once at the base (`3000662c`), then `os explain ID` ran for every key of `RULE_EXPLANATIONS` (292 ids, stdout, stderr and the exit code captured per id) against the lint `dist` built from the base and again against the lint `dist` built from this branch. The CLI reads the table only through `@objectstack/lint/rule-explanations`, so the lint build is the only variable. ```bash # from packages/cli, ids.txt = Object.keys(RULE_EXPLANATIONS), one per line cat ids.txt | xargs -P 6 -I{} sh -c 'f=$(printf %s "$1" | sed "s#/#__#g"); node bin/run.js explain "$1" > "$2/$f.out" 2>&1; echo "exit=$?" >> "$2/$f.out"' _ {} OUTDIR diff -r before/explain after/explain && echo "ALL 292 IDENTICAL" ``` Result: `ALL 292 IDENTICAL`; every output exits 0 and opens with its `Rule:` header; the per-file sha256 manifest hashes to `c0705a6eac1357072ee4593b166a969264aeab25c0450ef5e2cdea8eb97a0350` on both sides. `JSON.stringify(RULE_EXPLANATIONS)` from the ESM and the CJS entry hashes to `d031c3c5771bb2e806719a5fe5c1fd291cd643d24c979248999d9baefefa0d29` before and after (key order included). </details> <details><summary>Public declaration: all 8 <code>.d.ts</code> / <code>.d.cts</code> files byte-identical</summary> `cmp` of `packages/lint/dist/{index,runtime,runtime-HASH,rule-explanations}.d.{ts,cts}` before and after: `IDENTICAL` for all eight. (A first cut put the imports above the interface, which dropped the module docblock from `rule-explanations.d.ts`; the imports now follow the interface, so the docblock stays its leading comment.) </details> <details><summary>The published entry still imports nothing</summary> A count of import statements and `require(` calls in `dist/rule-explanations.js` and `dist/rule-explanations.cjs` reads `0` and `0`: with `splitting: false` the bundle inlines the family files. The bundles grow by about 3 KB (source-path comments). </details> <details><summary>Lint suite, typecheck, build</summary> At `939ffdd2` (the last source change; later commits are the changeset and the two ADR anchors): - `pnpm --filter @objectstack/lint build`: exit 0, `check-dts-emitted: 6/6`. - `pnpm --filter @objectstack/lint typecheck` (tsc plus `check:test-typecheck`): exit 0. - `pnpm --filter @objectstack/lint exec vitest run --maxWorkers=2`: `Test Files 255 passed (255)`, `Tests 10185 passed (10185)`. </details> <details><summary>The rewritten test can fail (three mutations, each restored to HEAD)</summary> Through `scripts/ablation-replace.mjs`, running `vitest run src/rule-explanations.test.ts -t imports`: 1. a family file (`view-refs.ts`) gains a value import of `../validate-field-consumers.js`: red, "view-refs.js imports outside its family directory". 2. that file imports the index as a value (`import { type RuleExplanation }`): red, "view-refs.js imports the index as a value". 3. the index gains a bare side-effect import of `./validate-field-consumers.js`: red, "expected 66 to be 67" (an import the reader cannot parse fails the count). Each run ended "restored: blob == HEAD, `git diff HEAD` is empty". </details> ## Acceptance notes - The claim's surface was amended twice by the dispatching seat: the one test case (amendment 7295823844) and the two ADR anchor lines (amendment 7296411486). - `packages/lint/tsup.config.ts` and `packages/cli/src/utils/format.ts` still say in a comment that the entry "imports nothing". That stays true of the published bundle, so they are untouched. - Volatile counts above (292 ids, 65 headers, file sizes) are readings of this head for review, not text in the tree. ## Verification Read first, by risk: `packages/lint/src/rule-explanations.ts` (the index: the imports and the unchanged `RULE_EXPLANATIONS` literal), `packages/lint/src/rule-explanations.test.ts` (the one rewritten case), `packages/lint/src/rule-explanations/shared.ts`. Two-minute check: `pnpm --filter @objectstack/lint build && node packages/cli/bin/run.js explain field-no-consumers` prints the same text as on `main`. <details><summary>Derived gates: 77 derived, 75 run green, 2 NOT MEASURED (local reconciliation)</summary> `node scripts/pm/dispatch-gates.mjs --commands` on this branch derived 77 commands (71 at `be77abe2`, plus 6 the ADR edit added at `22efc3478`; `be77abe2` and `22efc3478` differ only by the two ADR anchor lines). `--ran` reconciliation: "77 derived, 75 run, 2 NOT-MEASURED, 0 UNRUN". Two reds were seen and cleared: `check:adr-symbol-anchors` (the two ADR anchors, fixed in `22efc3478`, re-run green on that head) and `check-plugin-teardown-shape.mjs --self-test` (its pinned fixture commit was missing from the shallow clone; green after fetching it). The two NOT MEASURED gates need every package's `dist`, which a whole-repo build would give; CI runs them. ```text node scripts/check-adr-0087-registration.mjs --base origin/main :: exit 0 node scripts/check-adr-0087-registration.mjs --self-test :: exit 0 node scripts/check-adr-links.mjs :: exit 0 node scripts/check-adr-links.mjs --self-test :: exit 0 node scripts/check-changeset-no-major.mjs --base origin/main :: exit 0 node scripts/check-closing-keyword-parity.mjs :: exit 0 node scripts/check-closing-keyword-parity.mjs --self-test :: exit 0 node scripts/check-comment-mask-adoption.mjs :: exit 0 node scripts/check-comment-mask-adoption.mjs --self-test :: exit 0 node scripts/check-comment-mask-corpus.mjs :: exit 0 node scripts/check-dts-emitted.mjs --self-test :: exit 0 node scripts/check-empty-changeset.mjs --base origin/main :: exit 0 node scripts/check-empty-changeset.mjs --self-test :: exit 0 node scripts/check-issue-citations.mjs :: exit 0 node scripts/check-plugin-teardown-shape.mjs :: exit 0 node scripts/check-plugin-teardown-shape.mjs --self-test :: exit 0 node scripts/check-temporal-live-filter.mjs :: exit 0 node scripts/check-temporal-live-filter.mjs --self-test :: exit 0 node scripts/ci/assert-turbo-remote-cache.mjs --self-test :: exit 0 node scripts/ci/plan-test-core-shards.mjs --self-test :: exit 0 node scripts/ci/prune-turbo-cache.mjs --self-test :: exit 0 node scripts/ci/snapshot-next.mjs --self-test :: exit 0 node scripts/docs-audit/check-affected-docs.mjs :: exit 0 node scripts/docs-audit/check-drift-comment.mjs :: exit 0 node scripts/pm/check-governed-queue-guard.mjs --self-test :: exit 0 node scripts/pm/release-rehearsal-clone.mjs --self-test :: exit 0 node scripts/release-pending-publish.mjs --self-test :: exit 0 pnpm --filter @objectstack/lint run check:doc-formula-expressions :: exit 0 pnpm --filter @objectstack/spec run check:duration-unit-keys :: exit 0 pnpm check:adr-anchors :: exit 0 pnpm check:adr-index :: exit 0 pnpm check:adr-symbol-anchors :: exit 0 pnpm check:authz-resolver :: exit 0 pnpm check:changeset-gate-self-tests :: exit 0 pnpm check:ci-filter-parity :: exit 0 pnpm check:cli-command-ids :: exit 0 pnpm check:closed-ceremony-text :: exit 0 pnpm check:cross-package-test-inputs :: exit 0 pnpm check:doc-authoring :: exit 0 pnpm check:docs-transcript-drift :: exit 0 pnpm check:driver-memory-census :: exit 0 pnpm check:dts-closure :: exit 0 NOT-MEASURED pnpm check:dual-build-cjs-loads :: PREREQUISITE NOT MET (exit 3) — reads every package dist; a whole-repo build is outside the foreground budget; CI Lint & Repo Gates runs it after Build pnpm check:engine-double-contract :: exit 0 pnpm check:error-status-conformance :: exit 0 pnpm check:gitlink-declared :: exit 0 pnpm check:issue-citations :: exit 0 pnpm check:keyed-text-bounds :: exit 0 pnpm check:lean-entry-closure :: exit 0 pnpm check:logger-receiver-detach :: exit 0 pnpm check:nul-bytes :: exit 0 pnpm check:object-def-param-keys :: exit 0 pnpm check:objectql-double-limit :: exit 0 pnpm check:optional-error-sink :: exit 0 pnpm check:org-identifier :: exit 0 pnpm check:page-declaration-shape :: exit 0 pnpm check:platform-object-tenancy-census :: exit 0 pnpm check:pm-governed-merges :: exit 0 pnpm check:pm-prior-rulings :: exit 0 pnpm check:published-files :: exit 0 NOT-MEASURED pnpm check:published-readme-exports :: PREREQUISITE NOT MET (exit 3) — reads every package dist; a whole-repo build is outside the foreground budget; CI Lint & Repo Gates runs it after Build pnpm check:query-options-erasure :: exit 0 pnpm check:refd-timer-probe :: exit 0 pnpm check:registry-log-declared :: exit 0 pnpm check:rest-log-spy-declared :: exit 0 pnpm check:settings-bind-window :: exit 0 pnpm check:slot-lookup :: exit 0 pnpm check:sourcemap-no-sources-content :: exit 0 pnpm check:system-context-census :: exit 0 pnpm check:test-source-alias :: exit 0 pnpm check:tier-file-adoption :: exit 0 pnpm check:type-check-coverage :: exit 0 pnpm check:type-check-debt :: exit 0 pnpm check:undeclared-dep-imports :: exit 0 pnpm check:vouch-for-reference-callers :: exit 0 pnpm check:watch-hint-literal :: exit 0 pnpm check:where-matcher :: exit 0 ``` </details> Every family CI runs is read from this PR's check runs, not re-run here. --- _Generated by [Claude Code](https://claude.ai/code/session_01J8VVAoVoLCk7JiHvytWEqY)_ --------- Co-authored-by: Claude <noreply@anthropic.com>
1 parent 7625001 commit 1aa9c57

72 files changed

Lines changed: 7971 additions & 7328 deletions

File tree

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
---
2+
'@objectstack/lint': patch
3+
---
4+
5+
refactor(lint): the long-form rule explanations live in one source file per rule family
6+
7+
Clause-②: no
8+
Metadata-protocol: none
9+
10+
The table `os explain RULE_ID` prints moved, entry by entry and word for word, from one source file into one file per rule family. Nothing an author or a caller reads changes: `os explain` prints the same bytes for every rule id, `RULE_EXPLANATIONS` holds the same entries in the same key order, `explainRule` answers the same, and the published declaration files are byte-identical. The bundled `@objectstack/lint/rule-explanations` entry still imports nothing; only its bytes moved (module order and source-path comments).

‎docs/adr/0055-master-detail-controlled-by-parent.md‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -191,7 +191,7 @@ An ADR recording a **narrower** enforcement than the runtime performs errs in th
191191
- **Gate.** `packages/plugins/plugin-security/src/security-plugin-cbp-write-gate.ts#assertControlledByParentWrite` gains one leg, before the §3 chain walk, for a **by-id `update`** only. It admits the write when **all** hold: the detail's resolved relation carries the opt-in **and** the registered schema carries the `owner_id` column (`CbpRelation.ownerMayEdit`, resolved beside the relation in `security-plugin-cbp-relation.ts`); the write is **not** on behalf of another user; the stored row's `owner_id` is the caller's own `userId`; and the caller can **read** the master row, asked of the read door itself under the caller's context — the master's object grant, its row-level and sharing scope and, for a master that is itself `controlled_by_parent`, the chain the engine composes (ADR-0139 D6). A hidden row and a refused read are one answer, "not readable" (#7505). When any condition fails, the write takes the §3 walk and carries the walk's own refusal; the leg adds no envelope of its own.
192192
- **What the leg does not change.** Gate ① (the detail's own object-level `update` grant) and step 2.7 (the detail's own row-level policies, where `owner_id == current_user.id` now decides as authored) run before it, unchanged. A body that re-points the master reference still asks **edit** on the master it names: the owner may change their row, not move it under a master they cannot edit. The on-behalf-of path keeps both floors, as ADR-0090 D10 keeps them for every other relief.
193193
- **Lint.** `@objectstack/lint` gains `security-controlled-by-parent-owner-policy-without-opt-in` (`warning`): an `update` / `all` policy that keys a field on `current_user.id`, on a `controlled_by_parent` detail of the stack that has **not** opted in, is flagged with the opt-in as the fix. Advisory, because a narrowing can be meant and no runtime door refuses the shape.
194-
⚠️ **Amended 2026-10-27 (#26591 / PR #26658) — the opt-in is prescribed only where the gate honours it.** The leg above admits one caller, the user in the stored row's `owner_id`, and only on a detail whose registered schema carries that column (`CbpRelation.ownerMayEdit` resolves `false` otherwise), so the arm no longer prescribes the opt-in for every caller-keyed policy. Under the same rule id and the same `warning` tier, `packages/lint/src/validate-security-posture.ts#pushCbpOwnerPolicyWithoutOptIn` prescribes the opt-in only for an `update` / `all` policy that keys the caller on `owner_id`, on a `controlled_by_parent` detail that carries `owner_id` and has **not** opted in — the one shape the opt-in turns into an admission. Every other caller-keyed update policy — one keyed only on another user field (`owner == current_user.id`), or on `owner_id` where the detail carries no such column (`ownership: 'business_unit' | 'org' | 'none'`, or a platform-managed object) — goes to `packages/lint/src/validate-security-posture.ts#pushCbpCallerPolicyOffOwnerColumn` and is flagged in **both** states, opted in or not: the verdict says the opt-in admits only the stored `owner_id`, the remedy is to key the accountable person on `owner_id` (or to give the detail that column), and the opt-in is never named as the fix. The rule's `os explain` entry (`packages/lint/src/rule-explanations.ts#SECURITY_CBP_OWNER_POLICY_WITHOUT_OPT_IN_EXPLANATION`) says the same. The sentence above is kept as the arm as first landed; ⛔ it is no longer a statement of what the lint prescribes. Whether the runtime leg should widen to a named owner field is the maintainer's open question on #26591; this note records it and decides nothing.
194+
⚠️ **Amended 2026-10-27 (#26591 / PR #26658) — the opt-in is prescribed only where the gate honours it.** The leg above admits one caller, the user in the stored row's `owner_id`, and only on a detail whose registered schema carries that column (`CbpRelation.ownerMayEdit` resolves `false` otherwise), so the arm no longer prescribes the opt-in for every caller-keyed policy. Under the same rule id and the same `warning` tier, `packages/lint/src/validate-security-posture.ts#pushCbpOwnerPolicyWithoutOptIn` prescribes the opt-in only for an `update` / `all` policy that keys the caller on `owner_id`, on a `controlled_by_parent` detail that carries `owner_id` and has **not** opted in — the one shape the opt-in turns into an admission. Every other caller-keyed update policy — one keyed only on another user field (`owner == current_user.id`), or on `owner_id` where the detail carries no such column (`ownership: 'business_unit' | 'org' | 'none'`, or a platform-managed object) — goes to `packages/lint/src/validate-security-posture.ts#pushCbpCallerPolicyOffOwnerColumn` and is flagged in **both** states, opted in or not: the verdict says the opt-in admits only the stored `owner_id`, the remedy is to key the accountable person on `owner_id` (or to give the detail that column), and the opt-in is never named as the fix. The rule's `os explain` entry (`packages/lint/src/rule-explanations/security-posture.ts#SECURITY_CBP_OWNER_POLICY_WITHOUT_OPT_IN_EXPLANATION`) says the same. The sentence above is kept as the arm as first landed; ⛔ it is no longer a statement of what the lint prescribes. Whether the runtime leg should widen to a named owner field is the maintainer's open question on #26591; this note records it and decides nothing.
195195

196196
### What is out of scope, and why
197197

‎docs/adr/0141-inter-app-dependency-declared-checked-before-install.md‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -121,7 +121,7 @@ an installed package and validates its shape by reference to that very schema
121121
(`packages/metadata-protocol/src/protocol.ts#resolveWritePackageScope`); a declared dependency the registry cannot resolve
122122
stays in the closure and the walk stops there — an absent dependency is not a fault at the save door. A view bound outside
123123
the closure is refused `422`, and the refusal's fix line already reads "declare a dependency on the package that defines
124-
the object" (`packages/lint/src/rule-explanations.ts#LIST_VIEW_OBJECT_OUTSIDE_CLOSURE_EXPLANATION`). The same text records
124+
the object" (`packages/lint/src/rule-explanations/list-view-field-refs.ts#LIST_VIEW_OBJECT_OUTSIDE_CLOSURE_EXPLANATION`). The same text records
125125
the boundary of that judgement: `os validate`, `os build` and `os lint` "judge neither: a stack there is one app, and an
126126
unresolved binding may be a dependency package's object the stack does not carry". What `defineStack` does say about a
127127
reference across packages is measured in ADR-0130 §1.5 and pinned in `packages/spec/src/stack.zod.ts`: a lookup or

‎packages/lint/src/rule-explanations.test.ts‎

Lines changed: 35 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -139,9 +139,40 @@ describe('rule explanations (#22161)', () => {
139139
expect(indexBarrel.explainRule).toBe(explainRule);
140140
});
141141

142-
it('the module imports nothing, so its published entry stays free of the rule engine', () => {
143-
const source = readFileSync(join(HERE, 'rule-explanations.ts'), 'utf8');
144-
expect(source).not.toMatch(/^\s*import\s/m);
145-
expect(source).not.toMatch(/^\s*export\s[^\n]*\sfrom\s/m);
142+
it('the module imports nothing but its family files, so its published entry stays free of the rule engine', () => {
143+
// Every import statement of a file, as [type-only, specifier]; a line that
144+
// opens an import the pattern cannot read fails the count check below.
145+
const importsOf = (source: string): Array<[boolean, string]> => {
146+
const found = [...source.matchAll(/^\s*import\s+(type\s+)?\{[^}]*\}\s*from\s*'([^']+)';/gm)]
147+
.map((m): [boolean, string] => [m[1] !== undefined, m[2]!]);
148+
expect(found.length).toBe(source.match(/^\s*import\s/gm)?.length ?? 0);
149+
return found;
150+
};
151+
const index = readFileSync(join(HERE, 'rule-explanations.ts'), 'utf8');
152+
expect(index).not.toMatch(/^\s*export\s[^\n]*\sfrom\s/m);
153+
const families = importsOf(index).map(([typeOnly, spec]) => {
154+
expect(typeOnly, spec).toBe(false);
155+
expect(spec).toMatch(/^\.\/rule-explanations\/[a-z0-9-]+\.js$/);
156+
return spec.slice('./rule-explanations/'.length);
157+
});
158+
expect(families.length).toBeGreaterThan(0);
159+
// Each family file reached from the index, and each sibling it reaches.
160+
const seen = new Set<string>();
161+
const queue = [...families];
162+
while (queue.length > 0) {
163+
const file = queue.shift()!;
164+
if (seen.has(file)) continue;
165+
seen.add(file);
166+
const source = readFileSync(join(HERE, 'rule-explanations', file.replace(/\.js$/, '.ts')), 'utf8');
167+
expect(source, file).not.toMatch(/^\s*export\s[^\n]*\sfrom\s/m);
168+
for (const [typeOnly, spec] of importsOf(source)) {
169+
if (spec === '../rule-explanations.js') {
170+
expect(typeOnly, `${file} imports the index as a value`).toBe(true);
171+
continue;
172+
}
173+
expect(spec, `${file} imports outside its family directory`).toMatch(/^\.\/[a-z0-9-]+\.js$/);
174+
queue.push(spec.slice(2));
175+
}
176+
}
146177
});
147178
});

0 commit comments

Comments
 (0)