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:
gitea committed 2026-10-11 19:33:35 +08:00
1 parent 8d7f930596
commit 9ece30255e
3 files changed
+90

No files matched your search

@@ -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);
});
});
+13
View File
@@ -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";