diff --git a/.agents/rules/typescript-style.md b/.agents/rules/typescript-style.md index 2d5592b..e1f92d3 100644 --- a/.agents/rules/typescript-style.md +++ b/.agents/rules/typescript-style.md @@ -25,3 +25,15 @@ When modifying or adding TypeScript files in `src/`: 4. **Architecture**: - Prefer small, cohesive domain functions over large generic utility files. + +5. **Frozen Utility Modules**: + - `src/domains/seal/capabilities/helpers.ts` is FROZEN: no new exports. It is + the historical catch-all (~85 exports, re-exported into every capability's + namespace via `export *`), and `helpers.freeze.test.ts` fails CI when it + grows past its line/export ceiling. + - Put new logic in a focused module beside the domain that owns it. Moving an + existing export OUT of `helpers.ts` is encouraged — then lower the ceiling + constants in the ratchet test. + - Apply the same instinct to any other module you are tempted to treat as a + scrap drawer: if the file's name is `helpers`/`utils`/`common`, it is + already a sign the logic belongs somewhere more specific. diff --git a/src/domains/seal/capabilities/helpers.freeze.test.ts b/src/domains/seal/capabilities/helpers.freeze.test.ts new file mode 100644 index 0000000..c161030 --- /dev/null +++ b/src/domains/seal/capabilities/helpers.freeze.test.ts @@ -0,0 +1,65 @@ +import { describe, expect, test } from "bun:test"; +import { readFileSync } from "node:fs"; +import { join } from "node:path"; + +/** + * Size ratchet for `helpers.ts` (see the FROZEN header at the top of that file). + * + * The module is the historical dumping ground for anything a capability needed, + * and `export *` from index.ts leaks all of it into every capability's + * namespace. A comment alone does not stop the next addition, so the ceiling is + * enforced here: adding an export (or growing the file) fails CI with a message + * that says where the logic should go instead. + * + * Both ceilings may go DOWN — moving an export out to a focused module is the + * intended fix. Only `helpers.ts` is measured; sibling modules are unrestricted. + */ + +const HELPERS_PATH = join(import.meta.dirname, "helpers.ts"); + +// Lower these only when logic actually leaves the file. +const MAX_LINES = 1694; +const MAX_EXPORTS = 85; + +function measure(source: string) { + const lines = source.split("\n").length; + // Top-level `export` declarations only; re-exports and `export { x } from` + // lists would be counted by a looser pattern. + const exports = source.match(/^export (?:async )?(?:function|const|class|type|interface|enum) /gm); + return { lines, exports: exports?.length ?? 0 }; +} + +describe("capabilities/helpers.ts freeze", () => { + const source = readFileSync(HELPERS_PATH, "utf8"); + const { lines, exports } = measure(source); + + test("stays under the line ceiling", () => { + expect( + lines, + `helpers.ts is ${lines} lines (ceiling ${MAX_LINES}). It is frozen: add new ` + + `logic to a focused module next to its domain instead of growing this grab-bag, ` + + `or move an existing export out to lower the ceiling.` + ).toBeLessThanOrEqual(MAX_LINES); + }); + + test("stays under the export ceiling", () => { + expect( + exports, + `helpers.ts exports ${exports} symbols (ceiling ${MAX_EXPORTS}). New exports are ` + + `not accepted here — index.ts re-exports the whole file into every capability's ` + + `namespace. Give the function a home beside the domain that owns it.` + ).toBeLessThanOrEqual(MAX_EXPORTS); + }); + + test("the FROZEN header stays in place", () => { + expect(source.startsWith("// FROZEN")).toBe(true); + expect(source).toContain("helpers.freeze.test.ts"); + }); + + test("measures what it claims to measure", () => { + // Guard the ratchet itself: a pattern that matches nothing would silently + // pin the export ceiling at 0 and pass forever. + expect(exports).toBeGreaterThan(0); + expect(lines).toBeGreaterThan(MAX_LINES - 200); + }); +}); diff --git a/src/domains/seal/capabilities/helpers.ts b/src/domains/seal/capabilities/helpers.ts index a52d552..7c7fd2e 100644 --- a/src/domains/seal/capabilities/helpers.ts +++ b/src/domains/seal/capabilities/helpers.ts @@ -1,3 +1,16 @@ +// FROZEN — see helpers.freeze.test.ts, which fails the build if this file grows. +// +// This module started as shared plumbing for the capability registry and has +// been the default place to drop anything for years. It now holds ~85 exports +// spanning unrelated concerns (base URLs, rule versions, Beijing timestamps, +// generic object paths), and `export *` in index.ts pushes every one of them +// into every capability's namespace — which is why the same helper gets +// re-derived instead of reused. +// +// Rules while frozen: +// - No new exports. Put new logic in a focused module next to its domain. +// - Existing exports may move out (update importers) — the budget only drops. +// - Fixes to existing exports are fine; the line/export ceiling is the guard. import { z } from "zod"; import type { KyInstance } from "ky"; import type { CorpConfig } from "../../../core/config/types.js";