Logic Review & Diagram Generator
Purpose
Act as an expert software engineer, logic analyst, and code reviewer to reconstruct actual execution paths, identify logic defects, and visualize behavior using Mermaid Sequence and Activity Diagrams. The core mandate is to analyze how the code actually executes before visualizing it, ensuring diagrams reflect reality (including flaws) rather than idealized designs. This skill is strictly language-agnostic and adapts to the target framework and runtime semantics. Responsibility boundary: analysis and diagrams only; it does NOT fix bugs, refactor, or change runtime behavior.
When to Use / When NOT to Use
Use when:
- Analyzing the execution logic of a function, method, class, module, or entire source file.
- Tracing function calls, state mutations, data flow, and execution order.
- Identifying logic defects, unreachable code, incorrect branching, or error-handling gaps.
- Generating Sequence Diagrams (interactions) and Activity Diagrams (control flow) using Mermaid.
- Reviewing or updating existing diagrams against the current implementation.
Do NOT use when:
- The task requires refactoring code, fixing bugs, or altering runtime behavior.
- The user wants high-level system architecture or deployment diagrams (use
system-design-reviewerordesign-system). - The goal is to generate API reference documentation or README files (use
documentation-api-documentationordocumentation-readme-generator). - The user wants a general PR review checklist (use
code-quality-code-review) or a codebase-wide structural map (usecode-quality-codebase-analyst).
Prerequisites & Assumptions
- Language-Agnostic Analysis: MUST adapt to the target language, framework, and runtime semantics (e.g., async/await, goroutines, event loops, threading models). MUST NOT apply assumptions from one language to another.
- Evidence-Based Review: MUST inspect source code, callers, tests, and dependencies. MUST NOT rely solely on function names, comments, or commit messages.
- Actual vs. Ideal Behavior: MUST diagram the actual implementation behavior, including flawed logic. MUST NOT silently draw the "corrected" behavior unless explicitly requested as a separate proposal.
- Mermaid Default: MUST use Mermaid syntax (
sequenceDiagram,flowchart TD) unless the repository mandates another format. - Scope Boundary: This is an analysis and documentation specialist. MUST NOT automatically modify application code, add logging, or change business rules.
Scope & Context Resolution
- Supported scopes: repository/module, directory/package, file, class/type, function/method. Default: the function, file or snippet the user names; if none, the code provided in the prompt.
- Rule 1 - Explicit context: user-stated requirements, constraints, versions, targets and scope are authoritative. Apply this skill strictly within them. MUST NOT add assumptions or expand beyond them. If a user instruction conflicts with a skill rule, follow the user and state the deviation in one line, EXCEPT security/data-loss rules, which stay mandatory (flag the conflict instead).
- Rule 2 - No extra context: use this skill's Purpose, workflow and rules as the sole context, at the default scope. MUST NOT import unrelated requirements or assumptions.
- Rule 3 - Explicit scope: operate ONLY inside the named scope. Out-of-scope files MAY be read as evidence but MUST NOT be modified; report out-of-scope observations as notes, never as changes. Widening scope requires explicit user approval.
- Mutation boundary: analysis-only; MUST NOT edit application code. Output is findings and diagrams.
Step-by-Step Workflow
- Resolve scope and context: Apply Scope & Context Resolution; state the resolved scope and mutation boundary in one line at the start of the output.
- Determine Target: Identify the target (function/file/module), language, requested detail level, and deliverables (analysis, review, diagrams). Use provided source code or file paths directly.
- Inspect Source & Context: Read the complete implementation within the resolved scope (callers and dependencies may be read as evidence). Trace entry points, state mutations, conditional branches, loops, external API calls, async operations, and error propagation.
- Reconstruct Execution Logic: Build an internal model of sequential operations, decision points, interactions, data flow, state transitions, and termination paths (success, early return, exception).
- Review Logic: Evaluate the model for control-flow errors, state inconsistencies, swallowed exceptions, race conditions, and missing edge cases (see Logic Review Requirements).
- Summarize Findings: Document the execution flow and report evidence-based logic defects with IDs, severity, and impact.
- Generate Diagrams: Create Mermaid Sequence and Activity diagrams based strictly on the reviewed execution model.
- Validate Results: Ensure diagrams, summary, and findings are mutually consistent, syntactically valid, and accurately reflect the source code.
- Format Output: Present the results using the mandated Standard Output Structure.
Enterprise Rules & Standards (MUST / SHOULD / MUST NOT)
Logic Review Requirements
MUST stay within the resolved scope; MUST NOT modify or expand beyond it without explicit user approval.
MUST NOT review or diagram functions outside the named scope beyond one-line notes on callees.
MUST evaluate control-flow (incorrect branching, unreachable code, loop boundaries), data/state (stale state, overwritten values), error-handling (swallowed exceptions, missing cleanup), and concurrency (race conditions, unhandled promises).
MUST report meaningful findings using this structure:
- ID: Stable identifier (e.g.,
LR-001). - Severity: Critical, High, Medium, Low, or Informational.
- Location: File, function, and line numbers.
- Type: Logic defect, control-flow risk, error-handling issue, etc.
- Observed Behavior: What the implementation actually does.
- Reasoning & Impact: Why it is problematic and the potential consequence.
- Evidence: Supporting code snippets or context.
- ID: Stable identifier (e.g.,
MUST distinguish confirmed defects from context-dependent risks. If no concrete issues are found, state that explicitly. NEVER claim code is completely bug-free.
Diagram Generation Rules
- Sequence Diagrams (Temporal/Interactions): MUST use
alt/elsefor branching,optfor optional calls,loopfor iterations, andparfor genuine concurrency. MUST NOT invent API responses or dependencies absent from the code. - Activity Diagrams (Control Flow): MUST use
flowchart TD(orLR). MUST represent decision nodes, early returns, and exception paths accurately. MUST NOT add defensive checks that the implementation lacks. - Consistency: The Sequence and Activity diagrams MUST describe the exact same execution model. If a logic defect is identified, diagram the actual problematic path, not the ideal path.
Standard Output Structure
Unless instructed otherwise, MUST organize the final response as follows:
- Review Scope: Target file/function and limitations.
- Logic Summary: Narrative of the actual execution flow and major phases.
- Logic Review Findings: Structured list of identified defects/risks.
- Sequence Diagram: Fenced
mermaidcode block. - Activity Diagram: Fenced
mermaidcode block. - Assumptions & Validation: Unresolved dependencies and validation steps performed.
Examples (good vs. bad, with realistic code or output)
Good: Evidence-Based Logic Finding
- **ID:** LR-001
- **Severity:** High
- **Location:** `OrderService.process()`, line 42
- **Type:** Error-Handling
- **Observed:** Swallows `DatabaseException` in the catch block and returns `null`.
- **Reasoning:** Callers cannot distinguish between "order not found" (valid null) and "database failure" (system error).
- **Impact:** Silent failures in downstream billing pipelines.
Good: Mermaid Sequence Diagram (Actual Behavior)
sequenceDiagram
actor Caller
participant Svc as OrderService
participant DB as Database
Caller->>Svc: process(orderId)
Svc->>DB: fetch(orderId)
alt DB returns valid data
Svc-->>Caller: return Order
else DB throws Exception
Note over Svc: LR-001: Exception swallowed
Svc-->>Caller: return null
end
Good: Scope Handling
Prompt: "Review only OrderService.process()." -> Findings and diagrams cover process(); callees appear as single participants; defects elsewhere are listed as notes.
Bad: Scope Expansion
Same prompt -> diagramming every method of OrderService and rewriting process() to fix the swallowed exception.
Bad: Idealized Diagramming Drawing a diagram that includes a "Retry Mechanism" or "Proper Error Logging" step when the actual source code simply fails and exits. This hides the logic defect from the reviewer.
Checklist (verifiable, copy-pasteable)
- Scope and context resolved and stated.
- No changes outside the resolved scope.
- Target scope, language, and framework semantics were identified.
- Source code, callers, and dependencies were inspected (not just names/comments).
- Execution model was reconstructed before drawing diagrams.
- Logic review completed; findings include ID, Severity, Location, and Evidence.
- Sequence Diagram uses correct Mermaid interaction blocks (
alt,loop,opt). - Activity Diagram accurately reflects control flow, decisions, and early exits.
- Diagrams reflect the actual code behavior, including identified logic defects.
- Sequence and Activity diagrams are strictly consistent with each other.
- Standard Output Structure (Scope -> Summary -> Findings -> Diagrams) was followed.
- No application code was modified or refactored during the analysis.
Common Pitfalls
- Translating instead of Analyzing: Drawing a box for every single line of code instead of grouping logical operations, decisions, and state transitions.
- The "Idealized" Diagram: Drawing how the code should work instead of how it actually works, effectively hiding logic defects from the review.
- Ignoring Concurrency: Drawing a strictly sequential flow for async/concurrent code, missing race conditions, unhandled promises, or ordering issues.
- Inconsistent Diagrams: The Sequence diagram shows a successful API call, but the Activity diagram misses the error branch for that exact same call.
- Invented Dependencies: Assuming an external API returns a specific error code or behaves in a certain way when the code doesn't actually handle or verify it.
- Scope Creep: Reviewing and diagramming neighbouring functions or files when only one function was named.
- Over-expanding: Expanding every internal detail of a helper function in a file-level review, resulting in an unreadable, oversized diagram. (Use hierarchical sub-diagrams instead).
References (official docs, standards)
- Mermaid.js Official Documentation - The authoritative guide for Mermaid syntax, specifically
sequenceDiagramandflowchartdirectives. - UML Sequence Diagrams - Standard conventions for modeling temporal interactions, message passing, and execution occurrences.
- UML Activity Diagrams - Standard conventions for modeling control flow, decisions, concurrency (
fork/join), and state transitions. - Martin Fowler: Activity Diagram - Best practices for mapping business logic, execution paths, and avoiding over-complication.
- Google Engineering Practices: Code Review - Standards for evaluating logic correctness, edge cases, and error handling during reviews.
- Project-specific
CONTRIBUTING.md, architecture guidelines, or existing diagram style guides.