Skip to content

refactor: reduce complexity of process_agent_memory in cache_memory/execute.rs#531

Merged
jamesadevine merged 1 commit into
mainfrom
refactor/cache-memory-complexity-f9287800c9594a6d
May 13, 2026
Merged

refactor: reduce complexity of process_agent_memory in cache_memory/execute.rs#531
jamesadevine merged 1 commit into
mainfrom
refactor/cache-memory-complexity-f9287800c9594a6d

Conversation

@github-actions

Copy link
Copy Markdown
Contributor

What was complex

process_agent_memory in src/tools/cache_memory/execute.rs had 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 using continue to skip files — deeply nested, hard to follow, and difficult to test in isolation.

What changed

Extracted process_memory_file helper with a FileOutcome enum:

enum FileOutcome {
    Copied(u64),
    Skipped(String),
}

async fn process_memory_file(
    relative_path, source_file, canonical_base, dest_file, config, total_size
) -> Result<FileOutcome>

Each guard in the helper uses an early return (return Ok(FileOutcome::Skipped(reason))) instead of a continue in the outer loop. This eliminates all the nesting complexity at the call site.

The main loop in process_agent_memory collapses to a clean match:

match process_memory_file(...).await? {
    FileOutcome::Copied(size) => { total_size += size; copied_count += 1; }
    FileOutcome::Skipped(reason) => { skipped_count += 1; skipped_reasons.push(reason); }
}

Additionally removed the now-unnecessary if !skipped_reasons.is_empty() guard before logging (iterating an empty Vec is a no-op).

Before / After complexity

Metric Before After
Clippy cognitive complexity 33/25 ⚠️ below 25 ✅

Verification

  • All 87+ tests pass unchanged
  • cargo clippy --all-targets --all-features -W clippy::cognitive_complexity reports zero cognitive-complexity warnings
  • No behaviour changes — identical validation logic, same skip/copy outcomes

Generated by Cyclomatic Complexity Reducer · ● 804K ·

…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>
@jamesadevine jamesadevine marked this pull request as ready for review May 13, 2026 22:39
@jamesadevine jamesadevine merged commit 005d226 into main May 13, 2026
@jamesadevine jamesadevine deleted the refactor/cache-memory-complexity-f9287800c9594a6d branch May 13, 2026 22:40
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