Skip to content

Normalize declaration ordering across the kb package (helpers at end, alphabetized) #767

Description

@williamthorsen

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:

  1. 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.
  2. Non-exported helpers last, inside a // region | Helpers block (always present when helpers exist), alphabetized within the region.
  3. 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

  • In every kb .ts file, non-exported helper functions appear after the exported declarations, inside a // region | Helpers block, alphabetized within the region.
  • In every kb .ts file, exported declarations precede helpers; the sole production entry point leads when one exists, and remaining exports are alphabetized case-insensitively.
  • In every kb test file, module-level helper functions are placed after the describe block(s) and wrapped in // region | Helpers; module-level test data and constants may remain at the top.
  • No behavior change: the full kb suite passes unchanged, and lint, typecheck, and format are clean. (Serves as the test criterion — existing tests cover the reordered code; no new behavior is introduced.)

Should have

  • A scan for helper-function declarations preceding the first describe in any kb test file returns nothing, confirming the suite is fully aligned.

Metadata

Metadata

Assignees

No one assigned

    Labels

    refactoringImprovement to code without change in functionalityscope:kb

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions