Freeze capabilities/helpers.ts with a size ratchet
The file is the registry's catch-all (~85 exports over unrelated domains), and index.ts re-exports all of it into every capability's namespace — which is why helpers get re-derived rather than reused, and why unrelated work keeps landing here. A comment alone would not stop the next addition, so the ceiling is enforced by test: helpers.freeze.test.ts fails when the file grows past its line or export count, and the failure message names where the logic should go instead. Both ceilings only ever drop — moving an export out to a focused module is the intended change, followed by lowering the constant. Verified by temporarily adding an export: both assertions fire. Records the policy in .agents/rules/typescript-style.md as well, since the ratchet explains what to do and the rule explains why.
This commit is contained in:
1 parent
8d7f930596
commit
9ece30255e
3 files changed
+90
No files matched your search
@@ -25,3 +25,15 @@ When modifying or adding TypeScript files in `src/`:
|
|||||||
|
|
||||||
4. **Architecture**:
|
4. **Architecture**:
|
||||||
- Prefer small, cohesive domain functions over large generic utility files.
|
- 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.
|
||||||
@@ -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);
|
||||||
|
});
|
||||||
|
});
|
||||||
@@ -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 { z } from "zod";
|
||||||
import type { KyInstance } from "ky";
|
import type { KyInstance } from "ky";
|
||||||
import type { CorpConfig } from "../../../core/config/types.js";
|
import type { CorpConfig } from "../../../core/config/types.js";
|
||||||
|
|||||||
Reference in new issue
Block a user