Skip to content

refactor: split process_mode_tuned into per-mode render_* helpers (cc 129 -> thin dispatcher) - #769

Merged
yvgude merged 2 commits into
yvgude:mainfrom
cedric013:refactor/ctx-read-process-mode-dispatch
Jul 9, 2026
Merged

refactor: split process_mode_tuned into per-mode render_* helpers (cc 129 -> thin dispatcher)#769
yvgude merged 2 commits into
yvgude:mainfrom
cedric013:refactor/ctx-read-process-mode-dispatch

Conversation

@cedric013

@cedric013 cedric013 commented Jul 9, 2026

Copy link
Copy Markdown
Contributor

Summary

Closes #770.

process_mode_tuned (rust/src/tools/ctx_read/render.rs) was a 557-line match mode block — cognitive complexity 129, and the hottest ctx_read path. This splits it into a thin dispatcher plus one render_* helper per non-trivial mode.

  • Extracts the 5 large arms — signatures, map, aggressive, entropy, task — into standalone functions.
  • Introduces a Copy RenderCtx<'a> bundle (file_ref / short / ext / file_path / original_tokens / crp_mode / line_count / task) so each helper takes 2-3 params instead of 10 (stays under too-many-arguments-threshold). tuning is threaded only to the arms that use it (entropy, task).
  • The match is now a thin dispatcher; the small arms (raw/auto/full/anchored/reference/lines:/density:/unknown) are unchanged.

Behaviour is byte-identical: the arm bodies were moved verbatim and only rustfmt-reindented (hence the large diff). Each mode is now independently testable and per-function complexity is far lower.

Test plan

  • cargo test --lib -- tools::ctx_read -> 70 passed, 0 failed (incl. process_mode_output_is_byte_stable_across_calls determinism test and the per-mode tests)
  • cargo build --lib
  • cargo fmt --check
  • cargo clippy --lib --all-features -- -D warnings clean for render.rs (the only remaining error is the unrelated pre-existing rules_overhead.rs:143 lint on main, fixed by fix: silence clippy::map_unwrap_or in rules_overhead (unblocks Clippy CI) #767)

Notes for reviewers

  • Risk areas / edge cases: none intended — pure extraction, no logic change. The byte-stable determinism test and the per-mode tests are the safety net.
  • Review tip: the diff is dominated by reindentation (12->4 spaces) of the moved bodies; the logic is unchanged line-for-line within each new render_*.
  • Backwards compatibility: output is byte-for-byte identical.

cedric013 added 2 commits July 9, 2026 14:18
process_mode_tuned was a 557-line match (cognitive complexity 129), the hottest ctx_read path. Extract the 5 large arms (signatures, map, aggressive, entropy, task) into standalone render_* functions taking a Copy RenderCtx bundle (file_ref/short/ext/file_path/original_tokens/crp_mode/line_count/task); tuning is threaded only to the arms that use it. The dispatcher is now a thin match. Behaviour is byte-identical (bodies moved verbatim, then rustfmt-reindented); each mode arm is now independently testable and the per-function complexity is far lower.

@yvgude yvgude left a comment

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Reviewed the ctx_read process_mode_tuned refactor in detail.

Verified:

  • RenderCtx<'a> is a clean Copy bundle that keeps helper signatures under clippy's too-many-arguments threshold
  • All 5 extracted render_* helpers (signatures, map, aggressive, entropy, task) are verbatim moves with rustfmt-reindentation only
  • The thin dispatcher correctly delegates with one-liners; small arms (raw, auto, full, anchored, reference, lines:, density:) stay inline — sensible scope
  • tuning is threaded only to entropy and task (the arms that need it)
  • Byte-identical outputprocess_mode_output_is_byte_stable_across_calls determinism test passes, protecting prompt-cache stability (#498)
  • 70/70 ctx_read tests green
  • rules_overhead.rs clippy fix is identical to what landed in #765

No behavioral changes, pure structural improvement. CC 129 → thin dispatcher.

Approved.

@yvgude
yvgude merged commit 0aebc9f into yvgude:main Jul 9, 2026
26 checks passed
@github-actions github-actions Bot locked and limited conversation to collaborators Jul 9, 2026
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

refactor: process_mode_tuned is a 557-line match (cognitive complexity 129) on the hot ctx_read path

2 participants