Skip to content

refactor(optimize): split 1948-line optimize.ts into focused modules#572

Open
tzone85 wants to merge 1 commit into
getagentseal:mainfrom
tzone85:refactor/split-optimize
Open

refactor(optimize): split 1948-line optimize.ts into focused modules#572
tzone85 wants to merge 1 commit into
getagentseal:mainfrom
tzone85:refactor/split-optimize

Conversation

@tzone85

@tzone85 tzone85 commented Jun 28, 2026

Copy link
Copy Markdown

What

src/optimize.ts was a 1948-line god file (2.4× a healthy ceiling) mixing constants, types, JSONL scanning, ~18 waste-detectors, session helpers, health math, and rendering. Split into 9 cohesive modules under src/optimize/, with optimize.ts kept as a barrel so the public API (runOptimize, scanAndDetect) and every re-exported symbol stay unchanged.

Module Lines
constants.ts 186
types.ts 121
scan.ts 205
detectors-reads.ts 324
detectors-mcp.ts 419
detectors-ghosts.ts 174
detectors-sessions.ts 376
health.ts 94
render.ts 153
optimize.ts (barrel) 141

Behaviour-verbatim

Functions relocated verbatim (one detector incidentally Prettier-reformatted — token-identical, behaviour-neutral).

Verification

  • tsc --noEmit clean; tsup build OK.
  • 558 tests pass (incl. 76 optimize.test.ts + 32 optimize-fs.test.ts).
  • HEAD-monolith vs this refactor → byte-identical CLI output on the same data.
  • Reviewed: barrel-complete (all 36 exports reachable), no duplicate re-exports, single shared resultCache, no circular imports.
src/optimize.ts was a 1948-line god file (2.4x the project's healthy ceiling)
mixing constants, types, JSONL scanning, ~18 waste-detectors, session helpers,
health/trend math, and terminal rendering. Split into 9 cohesive modules under
src/optimize/, with optimize.ts kept as a barrel so the public API
(runOptimize, scanAndDetect) and every re-exported symbol stay unchanged:

- constants.ts (186)          — all thresholds, colours, regexes, name sets
- types.ts (121)              — all shared types
- scan.ts (205)               — JSONL collection + session scanning
- detectors-reads.ts (324)    — junk/duplicate reads, claude.md bloat, ratio, cache
- detectors-mcp.ts (419)      — MCP coverage, schema cost, tool coverage, unused
- detectors-ghosts.ts (174)   — ghost agents/skills/commands, bash bloat
- detectors-sessions.ts (376) — low-worth/context-bloat/outlier session detectors
- health.ts (94)              — health grade, urgency, trend
- render.ts (153)             — terminal report rendering
- optimize.ts (141)           — scanAndDetect + runOptimize + barrel re-exports

Behaviour-verbatim relocation (one detector incidentally Prettier-reformatted;
token-identical). Verified: tsc --noEmit clean; tsup build OK; 558 tests pass
(incl. 108 optimize tests); HEAD-monolith vs this refactor produce byte-identical
CLI output on the same data; autoreview confirmed barrel-complete, no duplicate
re-exports, single shared resultCache, no circular imports.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

1 participant