refactor: reduce complexity of process_agent_memory in cache_memory/execute.rs#531
Merged
jamesadevine merged 1 commit intoMay 13, 2026
Merged
Conversation
…xecute.rs Extract per-file validation logic into process_memory_file helper. The process_agent_memory loop had 6 sequential guard checks (canonicalize containment, path safety, extension filter, size budget, content scan, copy) each with its own skip-and-continue branch. Clippy reported cognitive complexity 33/25. Changes: - Add FileOutcome enum (Copied(u64) | Skipped(String)) - Extract process_memory_file: all per-file checks use early-return instead of continue-in-outer-loop; returns Ok(FileOutcome) so caller has no nesting - process_agent_memory loop collapses to a single match on FileOutcome Cognitive complexity: 33 → below 25 (no Clippy warning). All existing tests pass unchanged. Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
What was complex
process_agent_memoryinsrc/tools/cache_memory/execute.rshad a Clippy cognitive complexity score of 33/25. The main loop processed each file with six sequential guard checks (symlink-containment, path safety, extension filter, size budget, content scan, copy) each usingcontinueto skip files — deeply nested, hard to follow, and difficult to test in isolation.What changed
Extracted
process_memory_filehelper with aFileOutcomeenum:Each guard in the helper uses an early return (
return Ok(FileOutcome::Skipped(reason))) instead of acontinuein the outer loop. This eliminates all the nesting complexity at the call site.The main loop in
process_agent_memorycollapses to a cleanmatch:Additionally removed the now-unnecessary
if !skipped_reasons.is_empty()guard before logging (iterating an emptyVecis a no-op).Before / After complexity
Verification
cargo clippy --all-targets --all-features -W clippy::cognitive_complexityreports zero cognitive-complexity warnings