refactor(audit): reduce complexity of analyze_all in mcp.rs#1167
Draft
github-actions[bot] wants to merge 1 commit into
Draft
refactor(audit): reduce complexity of analyze_all in mcp.rs#1167github-actions[bot] wants to merge 1 commit into
github-actions[bot] wants to merge 1 commit into
Conversation
Extract EventAccumulators struct with on_tool_call/on_tool_error/ on_server_error/on_server_lifecycle methods to replace the large nested match block. Move directory validation into ensure_mcpg_logs_dir_exists, file-reading+dispatch into process_log_files, and result-building into build_tool_summaries and build_server_health_list. analyze_all is now a 6-line orchestrator; cognitive complexity drops from 19 to below the 15 threshold. 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 (threshold: 15). The function combined:matchforloop (files → lines)match(tool_call / tool_error / server_error / server_start|stop), each with severalifguardsAll of this lived in one ~200-line function with up to 5 levels of nesting.
What changed
Introduced
EventAccumulators— a struct that owns the accumulation state (per_tool,observed_servers,server_error_counts,failures,saw_recognizable_event) and exposes four focused methods:on_tool_call(&Value)on_tool_error(Value)on_server_error(Value)on_server_lifecycle(&Value)process_event(&str, Value)Extracted four free functions:
ensure_mcpg_logs_dir_exists— directory validation (3-arm match with early-return semantics)process_log_files— file reading + line dispatch loopbuild_tool_summaries— constructs and sortsVec<MCPToolSummary>from accumulated statebuild_server_health_list— builds server rollups from three accumulation maps and computeserror_rate/unreliableanalyze_allis now a 6-line orchestrator with zero nesting of its own.Before / After
analyze_allcognitive complexityanalyze_allVerification
cargo build— cleancargo test— all tests pass (including the MCP analyzer unit tests in the same file)cargo clippy --all-targets --all-features— cleancargo clippy ... -W clippy::cognitive_complexitywith threshold=15 — zero findings forsrc/audit/analyzers/mcp.rsWarning
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.