refactor: reduce complexity of analyze_all in src/audit/analyzers/mcp.rs#1090
Draft
github-actions[bot] wants to merge 1 commit into
Draft
refactor: reduce complexity of analyze_all in src/audit/analyzers/mcp.rs#1090github-actions[bot] wants to merge 1 commit into
github-actions[bot] wants to merge 1 commit into
Conversation
Extract EventAccumulators struct with per-event handler methods (on_tool_call, on_tool_error, on_server_error, on_server_lifecycle) to replace a deeply-nested for+for+match inside analyze_all. Also extract four helpers: - validate_logs_dir_exists — replaces the inline metadata guard - process_log_contents — handles the per-line parse/dispatch loop - build_tool_summaries — builds and sorts the MCPToolSummary list - build_server_stats — builds and sorts the MCPServerStats list analyze_all is now a linear 20-line orchestration function. Cognitive complexity: 19 → below 10 (zero clippy::cognitive_complexity warnings at threshold=10). All 2062 tests pass. 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
analyze_allinsrc/audit/analyzers/mcp.rshad a cognitive complexity of 19 (Clippy threshold=10). The function contained a deeply-nested structure:On top of the parsing loop, the function also inlined the directory validation guard and 40+ lines of server-rollup building, making it hard to read and test each concern in isolation.
What changed
New
EventAccumulatorsstruct + implGroups the five accumulator variables (
saw_recognizable_event,per_tool,observed_servers,server_error_counts,failures) into a struct. The impl provides aprocess_eventdispatcher and four focused handlers:on_tool_call— increments call count, tracks server/tool sizeson_tool_error— increments error count, appends to failure liston_server_error— increments server error counter, appends to failure liston_server_lifecycle— records the server nameFour extracted helper functions
validate_logs_dir_existsmatch tokio::fs::metadata(...)guard at the top ofanalyze_allprocess_log_contentsfor line in contents.lines()loopbuild_tool_summariesbuild_server_statsanalyze_allafter refactorBefore / after complexity
analyze_allcognitive complexityVerification
cargo test --bin ado-aw)audit::analyzers::mcpunit tests passcargo clippy --all-targets --all-features— no new warningsclippy::cognitive_complexitywarnings forsrc/audit/analyzers/mcp.rsat threshold=10Warning
Firewall blocked 2 domains
The following domains were blocked by the firewall during workflow execution:
spsprodeus21.vssps.visualstudio.comspsprodweu4.vssps.visualstudio.comSee Network Configuration for more information.