Problem
Intra-file declaration ordering in packages/kb/ is inconsistent. Some test files place module-level helper functions above their describe block, contrary to the house style of helpers-at-the-end. Some production files have non-alphabetized exports or helpers, an exported function stranded inside the // region | Helpers block (check.ts, create.ts), or a helper stranded among exports with no region wrapper (type-guards.ts, yaml-position.ts). The effect is that locating a function means guessing each file's ad-hoc ordering instead of relying on one predictable rule.
This issue originally scoped the work to test-file helper relocation; it is broadened here to a package-wide ordering pass, since the same inconsistency exists in production files and the goal is one uniform, ergonomic ordering rule.
Context
- The package already uses a
// region | Helpers convention to demarcate trailing non-exported helpers — including for single helpers (scaffolding.ts, exists.ts, load-schema.ts). "Always wrap helpers in a region" therefore codifies existing practice rather than imposing a new style.
- The separator-banner cleanup raised during planning has no targets in kb — a whole-repo scan found exactly 2 such lines, both in an unrelated
agents example file. Other house-style rules (leading-verb names, imperative comments, sentence case, doc descriptions) already scan clean in kb. The actionable work is declaration ordering only.
function-declaration hoisting makes the test-file relocation runtime-inert; every helper in scope is a function declaration, not an arrow const.
Proposed solution
Apply one ordering policy to every .ts file under packages/kb/src:
- Exported declarations first. The production entry point leads when a module has exactly one production-consumed export (e.g.
runCheck, runCreate); otherwise exported declarations are alphabetized case-insensitively. Test-only exports sort alphabetically among the rest — no separate tier.
- Non-exported helpers last, inside a
// region | Helpers block (always present when helpers exist), alphabetized within the region.
- Test files: relocate module-level helper functions below the
describe block(s), wrapped in // region | Helpers, alphabetized. Module-level test data and constants (fixture maps, sample strings) may remain at the top.
This is pure source reordering plus region-marker insertion; no runtime behavior changes.
Acceptance criteria
Must have
Should have
Problem
Intra-file declaration ordering in
packages/kb/is inconsistent. Some test files place module-level helper functions above theirdescribeblock, contrary to the house style of helpers-at-the-end. Some production files have non-alphabetized exports or helpers, an exported function stranded inside the// region | Helpersblock (check.ts,create.ts), or a helper stranded among exports with no region wrapper (type-guards.ts,yaml-position.ts). The effect is that locating a function means guessing each file's ad-hoc ordering instead of relying on one predictable rule.This issue originally scoped the work to test-file helper relocation; it is broadened here to a package-wide ordering pass, since the same inconsistency exists in production files and the goal is one uniform, ergonomic ordering rule.
Context
// region | Helpersconvention to demarcate trailing non-exported helpers — including for single helpers (scaffolding.ts,exists.ts,load-schema.ts). "Always wrap helpers in a region" therefore codifies existing practice rather than imposing a new style.agentsexample file. Other house-style rules (leading-verb names, imperative comments, sentence case, doc descriptions) already scan clean in kb. The actionable work is declaration ordering only.function-declaration hoisting makes the test-file relocation runtime-inert; every helper in scope is afunctiondeclaration, not an arrowconst.Proposed solution
Apply one ordering policy to every
.tsfile underpackages/kb/src:runCheck,runCreate); otherwise exported declarations are alphabetized case-insensitively. Test-only exports sort alphabetically among the rest — no separate tier.// region | Helpersblock (always present when helpers exist), alphabetized within the region.describeblock(s), wrapped in// region | Helpers, alphabetized. Module-level test data and constants (fixture maps, sample strings) may remain at the top.This is pure source reordering plus region-marker insertion; no runtime behavior changes.
Acceptance criteria
Must have
.tsfile, non-exported helper functions appear after the exported declarations, inside a// region | Helpersblock, alphabetized within the region..tsfile, exported declarations precede helpers; the sole production entry point leads when one exists, and remaining exports are alphabetized case-insensitively.describeblock(s) and wrapped in// region | Helpers; module-level test data and constants may remain at the top.Should have
describein any kb test file returns nothing, confirming the suite is fully aligned.