Skip to content

Refactor: 17% LOC reduction - #123

Draft
tmck-code wants to merge 10 commits into
mainfrom
refactor/reduce-loc
Draft

Refactor: 17% LOC reduction#123
tmck-code wants to merge 10 commits into
mainfrom
refactor/reduce-loc

Conversation

@tmck-code

@tmck-code tmck-code commented Aug 21, 2026

Copy link
Copy Markdown
Owner

Context

LOC reduction across claude/ (yas) and test/, without deleting working code or tests.

before after change
claude/ (yas) 14,160 11,749 -17.0%
test/ 22,073 20,955 -5.1%
combined 36,233 32,704 -9.7%

Changes

Where the yas reduction came from

lines share what
-2,231 15.8% comments and docstrings: removed narration of past changes, deleted docstrings the function name already covers, compressed invariant notes to one line
-577 4.1% dead code (Pill.gradient_fg, rainbow_color, _parse_iso_to_epoch), spark_rgb/gradient_rgb dedupe
-15 0.1% parsecache eviction fix
+412 -2.9% reverting @dataclass to __slots__ classes (see Benchmark)

Where the test reduction came from

lines share what
-655 3.0% 151 banner comment blocks, 282 bare # setup/# run/# expected/# assert markers, and their blank lines
-289 1.3% parametrising and table-driving same-shaped tests
-256 1.2% _silence_dynamic lifted to conftest (3 drifted copies), hoisted strip_ansi imports, deduped Renderer() instantiations
-31 0.1% two byte-identical duplicate tests
+113 -0.5% fixing three defective tests, plus a parsecache regression test

Collected tests: 1571 -> 1570.

Behaviour changes

  • parsecache evicted in the wrong order. The sub-key trim sorted on repr(float(resume_after)) and dropped the lexicographically smallest, so '9.0' outranked '10.0' — recently-used entries were evicted while stale ones survived. Now sorts by recency, as its docstring always claimed.
  • RunningSubagent.last_activity tightened from tuple | None to always-tuple. The None branch was unreachable and forced Optional handling at three renderer call sites.

Test defects fixed

  • test_token_rate.py: FakeTime._now was mutated and never reset — order-fragile under -n auto.
  • test_cohort_visibility.py: stubbed _conclusively_retired to a constant, so the real predicate never ran.
  • test_tokens_cost.py: an assertion compared a call to itself and could never fail.

Two pre-existing bugs found, not fixed here

Both are written up in KNOWN_ISSUES.md.

  1. Unreachable recovery code, claude/yas/info/subagents.py ~1081-1122. _conclusively_retired requires 125s staleness (max(FINISHED_LINGER, COHORT_GRACE) + TERMINAL_SKEW); visible()'s own _retired() uses 120s on the same branch. Anything conclusively retired is already gone, so the mispredict re-parse block never executes. Pinned as xfail(strict=True). Fix is to align the constants or delete the block.
  2. claude/mon/layout.py::_visible_len mishandles wide characters.

Why not more

  • themes.py is 1,641 lines of palette data; 0 of 39 Theme fields are identical across all 15 themes.
  • The two largest functions, build_wide and subagent_row, are dense column arithmetic, not boilerplate.
  • The test tree holds ~1,380 distinct test bodies at a ~5-line floor.
  • 412 lines were given back for the performance fix.

Further reduction means deleting tests or working code.

Checklist

  • Tests added or updated for new/changed behaviour
  • (bug fixes) a regression test now covers the situation so it can't come back
  • N/A — no behaviour change, because:

Screenshots / recording

Before After
  • N/A — no visible change

Renderer code changed, but the demo gate shows no drift: a baseline built in a separate worktree and run twice fingerprints the pre-existing elapsed-time noise, and every remaining diff falls in those classes.

Benchmark

Command Mean [ms] Min [ms] Max [ms] Relative
main 44.9 ± 4.3 42.7 74.2 1.00
PR 45.5 ± 2.4 42.7 58.5 1.01 ± 0.11
  • N/A — no performance-relevant change

Converting 16 classes to @dataclass made the branch 1.13-1.18x slower than main, minima non-overlapping. The cost is class-definition time, not per-instance: dataclasses._process_class ran 16 times at import for ~11ms, ~24% of total runtime. The statusline is a cold-start CLI run on every prompt, so import time is runtime. All 16 sites are now hand-written __slots__ classes, at +412 lines. Ruling recorded in CODING_STANDARDS.md and KNOWN_ISSUES.md.

pytest, ruff and the demo gate all stayed green through the regression. Only make bench caught it.

System info

Key Value
OS Linux archlinux 7.1.8-arch1-3 #1 SMP PREEMPT_DYNAMIC Tue, 11 Aug 2026 09:16:08 +0000 x86_64 GNU/Linux
Claude Code 2.1.238 (Claude Code)
Terminal TERM=tmux-256color TERM_PROGRAM=tmux SHELL=/bin/bash COLORTERM=truecolor
Locale LANG=en_AU.UTF-8 LC_ALL=
Python Python 3.13.13
uv uv 0.12.5 (210d1f678 2026-08-14 x86_64-unknown-linux-gnu)

Test and others added 10 commits August 21, 2026 01:03
Remove comments and docstrings that narrate past changes: rejected
alternatives, old constant values, superseded designs, task/decision IDs
(D1-D9, "Task 3.2", "task 4.1"), openspec/design.md citations, and a
stale "REMOVE AFTER 0.11.0" marker. Compress surviving comments to a
terse one-line statement of what the code does, and drop docstrings
where the function name and signature already say it.

Currently-binding invariants are kept, compressed to one line: column
and elbow arithmetic, PUA glyph widths, shed-ladder ordering, width
floors, cache key semantics, and subagent status-resolution priority.

claude/: 14160 -> 11928 lines (2232 removed, 15.8%).

Behaviour unchanged: 1570 tests pass, ruff clean, demo visual diff
byte-identical across consecutive renders. Also drops an unused
TYPE_CHECKING import and dead branch in yas/tokens.py, and annotates
mon.tick(args) which the staged-file mypy gate surfaced.

Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
Tier 1-4 pass across the yas package and mon:

- dead code: Pill.gradient_fg, rainbow_color, _parse_iso_to_epoch
- dataclass conversions: RowSpec, LayoutSpec, Pill, WindowSlice, SessionView,
  Theme/Config field plumbing
- dedupe: spark_rgb/gradient_rgb share one path; ITALIC_OFF wired through text
- RunningSubagent.last_activity is now always a tuple; the None case was
  unreachable and forced Optional handling at three renderer call sites
- docs updated to match (README, CONTEXT, ARCHITECTURE)

claude/ 11,929 -> 11,338 lines. No behaviour change.

Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
The sub-key trim sorted on repr(float(resume_after)) and dropped the
lexicographically smallest, so '9.0' outranked '10.0' and recently-used
entries were evicted while stale ones survived. The docstring already
claimed recency ordering; now the code matches it.

Behaviour change, not a refactor - kept separate from 7970743.

Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
- test_token_rate: FakeTime._now was mutated and never reset, making the
  file order-fragile under -n auto. Each test now gets an isolated clock.
- test_cohort_visibility: _conclusively_retired was stubbed to a constant
  True, so the real predicate never ran. Un-stubbed and split in two. The
  real predicate exposes a production bug: its staleness threshold is
  max(FINISHED_LINGER, COHORT_GRACE) + TERMINAL_SKEW = 125s, while
  visible()'s own _retired() check uses 120s on the same branch. Anything
  conclusively retired is therefore already dropped by visible(), leaving
  the mispredict re-parse block in subagents.py unreachable. Pinned as
  xfail(strict=True) rather than re-stubbed; the production fix is a
  separate decision.
- test_tokens_cost: the assertion compared a call to itself and could never
  fail. Now compares default vs explicit show_icons=True, per its docstring.

Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
…elpers

- delete ~201 `# ==== Section ====` banner comments (these already violated
  the repo's pytest style guide)
- delete ~328 bare `# setup` / `# run` / `# expected` / `# assert` marker
  lines; the phases name the code shape, they are not emitted as comments
- lift _silence_dynamic into conftest (it was written twice and had drifted)
- hoist ~25 function-local strip_ansi imports to module level
- drop ~50 redundant Renderer() instantiations
- collapse the test_config.py bool-knob matrix into a parametrised test

test/ 22,108 -> 21,275. No test cases added or removed.

Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
Covers the audit items the first sweep did not reach: parse_cache,
test_info parametrisation, table-driven cases in mon_layout / mon_tui /
context_line, an inert idiom in subagent_rows, plus mon_discovery and
migrate.

test/ 21,275 -> 20,986. Collected count unchanged at 1572.

Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
One in test_info.py, one in test_task_list.py. Each had a surviving copy
with the same body, fixtures and parametrisation, so collected count drops
1572 -> 1570 with no loss of coverage.

Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
@DataClass costs class-DEFINITION time, not per-instance time:
dataclasses._process_class ran 16 times at import for ~11ms, about 24% of
total runtime. The statusline is a cold-start CLI invoked on every prompt,
so import time is runtime - make bench measured 1.13-1.18x slower than main
with non-overlapping minima.

All 16 sites (themes, pill, layout, tasks_view, config, session, subagents,
toolcounts) are now hand-written __slots__ classes. Bench is back to
1.01-1.02x with overlapping minima.

Costs +412 lines against the LOC goal. Correct trade: the lines are paid
once, the milliseconds are paid on every render.

Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
KNOWN_ISSUES.md covers why @DataClass is banned in claude/yas (class-
definition cost paid at import, and import time is runtime for a cold-start
CLI), with the measurements, plus the two production bugs found during the
LOC work: the unreachable recovery block in subagents.py and the wide-char
bug in mon/layout.py::_visible_len.

CODING_STANDARDS.md gets the one-paragraph ruling under deliberate
deviations, pointing at the detail.

Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant