Skip to content

Pre-Deployment Governance (CLI)

The command-line interface logic for executing static and dynamic performance audits.

Provides the command-line interface for the Alnoms performance intelligence system. The CLI orchestrates static AST analysis, prescriptive remediation suggestions, dynamic profiling, and empirical doubling tests to detect performance bottlenecks before production.

Features
  • CI/CD Ready: Supports raw and pretty JSON outputs for automation pipelines.
  • Static Analysis: Detects inefficient loops, API calls, and performance traps.
  • Prescriptive Suggestions: Recommends O(1) fixes and optimized patterns.
  • Empirical Analysis: Integrates with Arprax Profiler for Big-O scaling analysis.

PerformanceCLI

Performance Intelligence CLI for complexity analysis and profiling.

Acts as the primary entrypoint for the Alnoms performance intelligence system. Provides a terminal interface for static analysis, dynamic profiling, empirical scaling tests, and structured performance reporting. Designed for both human-readable output and CI/CD automation.

Source code in src/alnoms/cli.py
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
class PerformanceCLI:
    """Performance Intelligence CLI for complexity analysis and profiling.

    Acts as the primary entrypoint for the Alnoms performance intelligence system.
    Provides a terminal interface for static analysis, dynamic profiling,
    empirical scaling tests, and structured performance reporting. Designed for
    both human-readable output and CI/CD automation.
    """

    @staticmethod
    def print_report(result: dict) -> None:
        """Renders a full performance intelligence report in human-readable format.

        This method produces a structured analysis report that includes detected
        performance patterns, static diagnostics, dynamic profiling bottlenecks,
        empirical scaling analysis, performance verdict, and simulated fixes.

        Args:
            result (dict): Output from `ScriptAnalyzer.analyze_file()` containing:
                - 'patterns' (list): Detected performance patterns.
                - 'profile' (list): Dynamic profiling data.
                - 'empirical' (list): Empirical scaling analysis data.
                - 'meta' (dict): Metadata including timestamp and version.
                - 'empirical_target' (str): The target function for scaling tests.
                - 'total_time' (float): Total execution time.
                - 'file' (str): The analyzed file path.
        """
        meta = result.get("meta", {})
        patterns = result.get("patterns", [])
        profile_data = result.get("profile", [])
        empirical_data = result.get("empirical")
        target_name = result.get("empirical_target")

        print("\n==================================================")
        print("āš–ļø PERFORMANCE REPORT")
        print("==================================================")
        print(f"File: {result.get('file', 'Unknown')}")
        print(f"Timestamp (UTC): {meta.get('timestamp', 'Unknown')}")
        print(f"Total Execution Time: {result.get('total_time', 0)}s\n")

        # ---------------------------------------------------------
        # 0. DETECTED INTENT
        # ---------------------------------------------------------
        if patterns:
            primary = patterns[0]
            intent = (
                primary.get("intent")
                or primary.get("issue")
                or primary.get("pattern_id")
            )
            print("🧠 DETECTED INTENT:")
            print(f"   {intent}\n")

        # ---------------------------------------------------------
        # 1. STATIC ANALYSIS & SUGGESTIONS
        # ---------------------------------------------------------
        print("🚨 STATIC ANALYSIS (Diagnostics & Suggestions)")
        print("-" * 50)

        if not patterns:
            print(
                "   āœ… No performance issues detected. Code is structurally efficient.\n"
            )
        else:
            for i, p in enumerate(patterns, 1):
                func_name = p.get("function", "global")
                line_no = p.get("line", "??")
                issue = p.get("issue", p.get("pattern_id"))

                print(
                    f"{i}. āš ļø ISSUE: {issue} (Function: {func_name} | Line: {line_no})"
                )

                if "explanation" in p:
                    print(f"   šŸ“– Explanation: {p['explanation']}")

                    dsa = p.get("dsa_meta")
                    if dsa:
                        complexity = dsa.get("complexity", "O(N)")
                        module_path = dsa.get("module", "builtin")
                        tier = dsa.get("tier", "OSS")

                        print(f"   šŸ’Š RECOMMENDED OPTIMIZATION: {complexity}")
                        print(f"   šŸ—ļø IMPLEMENTATION: {module_path}")
                        print(f"   šŸ” ACCESS TIER: {tier}")

                    costs = p.get("cost_estimate", {})
                    if "time" in costs:
                        print(f"   ā±ļø Complexity Shift: {costs['time']}")

                    snip = p.get("snippets")
                    if snip:
                        print("\n   šŸ’” SUGGESTED FIX:")
                        print("   --- BEFORE ---")
                        for line in snip["before"].split("\n"):
                            print(f"   |  {line}")
                        print("   --- AFTER ---")
                        for line in snip["after"].split("\n"):
                            print(f"   |  {line}")

                print()

        # ---------------------------------------------------------
        # 2. DYNAMIC PROFILING
        # ---------------------------------------------------------
        print("ā±ļø DYNAMIC PROFILING (Top Execution Bottlenecks)")
        print("-" * 50)

        if not profile_data:
            print("   āœ… No performance bottlenecks detected in execution.\n")
        else:
            for i, func in enumerate(profile_data, 1):
                print(
                    f"   {i}. {func['function']}() -> {func['time']}s ({func['percent']}%)"
                )
            print()

        # ---------------------------------------------------------
        # 3. EMPIRICAL SCALING
        # ---------------------------------------------------------
        if empirical_data:
            print(f"šŸ“ˆ EMPIRICAL SCALING ANALYSIS: {target_name}()")
            print("-" * 50)
            print(
                f"{'N':<10} | {'Time (s)':<12} | {'Ratio':<8} | {'Est. Complexity':<15}"
            )
            print("-" * 50)

            final_complexity = "O(1)"
            for row in empirical_data:
                r_str = f"{row['Ratio']:.2f}" if row["Ratio"] > 0 else "-"
                print(
                    f"{row['N']:<10} | {row['Time']:<12.5f} | {r_str:<8} | {row['Complexity']:<15}"
                )
                final_complexity = row["Complexity"]

            # ---------------------------------------------------------
            # 4. VERDICT
            # ---------------------------------------------------------
            print("\nāš–ļø VERDICT:")

            safe_tiers = ["O(1)", "O(log N)", "O(N)", "O(N log N)"]

            if final_complexity in safe_tiers:
                print(
                    f"āœ… PASSED: Function operates at {final_complexity}. Safe for scaling."
                )
            elif final_complexity == "O(N^2)":
                print(
                    f"āš ļø WARNING: Function operates at {final_complexity}. May not scale efficiently."
                )
            else:
                print(
                    f"āŒ RISK: Function operates at {final_complexity}. Review recommended."
                )

            # ---------------------------------------------------------
            # 5. CONTEXT
            # ---------------------------------------------------------
            print("\nšŸ“Œ CONTEXT")
            print("-" * 50)
            print(
                "   Empirical scaling validates asymptotic behavior under increasing load.\n"
            )

            # ---------------------------------------------------------
            # 6. IMPACT ESTIMATION
            # ---------------------------------------------------------
            print("šŸš€ EXPECTED IMPACT")
            print("-" * 50)
            print("   For N = 10,000:")
            print("     • O(N²) → ~100,000,000 operations")
            print("     • O(N)  → ~10,000 operations")
            print("   Estimated improvement: 100×–1000Ɨ depending on workload.\n")

            # ---------------------------------------------------------
            # 7. CONFIDENCE
            # ---------------------------------------------------------
            print("šŸ¤– CONFIDENCE")
            print("-" * 50)

            if final_complexity == "O(N^2)":
                print("   High — static and empirical signals agree.\n")
            else:
                print("   Medium — mixed signals between analysis methods.\n")

            # ---------------------------------------------------------
            # 8. SIMULATED FIX
            # ---------------------------------------------------------
            print("šŸ” AFTER OPTIMIZATION (SIMULATED)")
            print("-" * 50)
            print("   Expected Complexity: O(N)")
            print("   Behavior: Linear scaling with stable performance.")
            print("   Suggested Implementation:")
            print("       s = set(arr)")
            print("       for x in arr:")
            print("           if x in s:")
            print("               total += x\n")

        else:
            print("ā„¹ļø EMPIRICAL ANALYSIS SKIPPED\n")

        print("==================================================\n")

    @staticmethod
    def main() -> None:
        """Primary entry point for the Alnoms Command-Line Interface.

        Configures the argument parser and routes execution based on the selected
        subcommand. Supports two primary modes:
        1. `analyze`: Human-readable performance reporting with deep profiling options.
        2. `ci`: Headless execution mode that accepts multiple files, outputs strict
           JSON, and enforces Big-O complexity guardrails via system exit codes.

        Raises:
            SystemExit:
                - Exits with 0 on successful execution and clean compliance.
                - Exits with 1 if an internal error occurs or if the `ci` mode detects
                  a performance bottleneck that breaches the `--fail-on` threshold.
        """
        parser = argparse.ArgumentParser(
            prog="alnoms",
            description="šŸ”¬ Alnoms: Performance Intelligence System",
            formatter_class=argparse.RawDescriptionHelpFormatter,
            epilog="""
    Example Usage:
      alnoms analyze script.py
      alnoms analyze script.py --deep
      alnoms analyze script.py --deep --start-n 50 --rounds 3
      alnoms analyze script.py --json

      # CI/CD Execution
      alnoms ci file1.py file2.py --fail-on "O(N^3)"
                """,
        )

        # Global version flag
        parser.add_argument("-v", "--version", action="version", version="Alnoms 1.1.2")

        subparsers = parser.add_subparsers(dest="command", help="Commands")

        # --- 1. THE HUMAN COMMAND (analyze) ---
        analyze_parser = subparsers.add_parser(
            "analyze", help="Analyze a single Python file"
        )
        analyze_parser.add_argument("file", help="Python file path")

        audit_group = analyze_parser.add_argument_group("Analysis Options")
        audit_group.add_argument("--deep", action="store_true")
        audit_group.add_argument("--function", dest="target_override")
        audit_group.add_argument("--gen", dest="gen")
        audit_group.add_argument("--data", dest="data")
        audit_group.add_argument("--start-n", type=int, default=50)
        audit_group.add_argument("--rounds", type=int, default=3)

        output_group = analyze_parser.add_argument_group("Output Options")
        output_group.add_argument("--json", action="store_true")
        output_group.add_argument("--pretty", action="store_true")

        # --- 2. THE ROBOT COMMAND (ci) ---
        ci_parser = subparsers.add_parser(
            "ci", help="Headless execution for CI/CD pipelines"
        )
        ci_parser.add_argument("files", nargs="+", help="List of Python files to scan")
        ci_parser.add_argument(
            "--fail-on",
            default="",
            help="Complexity threshold to fail the build (e.g., O(N^3))",
        )

        args = parser.parse_args()

        if args.command == "analyze":
            try:
                result = ScriptAnalyzer.analyze_file(
                    path=args.file,
                    deep=args.deep,
                    target_override=args.target_override,
                    gen_name=args.gen,
                    data_file=args.data,
                    start_n=args.start_n,
                    rounds=args.rounds,
                )

                if args.json or args.pretty:
                    print(json.dumps(result, indent=2 if args.pretty else None))
                    return

                PerformanceCLI.print_report(result)

            except Exception as e:
                print(f"āŒ Error analyzing {args.file}: {str(e)}")
                sys.exit(1)

        elif args.command == "ci":
            # --- SEVERITY & SCORING MODEL ---

            severity_scoring = {
                "O(2^N)": {"level": "CRITICAL", "score": 7},
                "O(N^3)": {"level": "CRITICAL", "score": 6},
                "O(N^2)": {"level": "HIGH", "score": 5},
                "O(N log N)": {"level": "MEDIUM", "score": 4},
                "O(N)": {"level": "LOW", "score": 3},
                "O(log N)": {"level": "LOW", "score": 2},
                "O(1)": {"level": "LOW", "score": 1},
            }

            fail_score = severity_scoring.get(args.fail_on, {}).get("score", 99)

            raw_issues = []

            # --- 1. GATHER EVIDENCE ---

            for file_path in args.files:
                try:
                    with contextlib.redirect_stdout(io.StringIO()):
                        result = ScriptAnalyzer.analyze_file(path=file_path, deep=False)

                    patterns = result.get("patterns", [])

                    for p in patterns:
                        comp = p.get("static_complexity") or "O(N^2)"

                        meta = severity_scoring.get(comp, {"level": "HIGH", "score": 5})

                        raw_issues.append(
                            {
                                "file": file_path,
                                "function": p.get("function", "global"),
                                "complexity": comp,
                                "severity": meta["level"],
                                "_score": meta["score"],  # Internal sorting key
                                "issue": p.get("issue", "Unknown Bottleneck"),
                                "suggestion": p.get("explanation", ""),
                            }
                        )

                except Exception as e:
                    # Handle internal engine crashes gracefully in the new schema

                    raw_issues.append(
                        {
                            "file": file_path,
                            "function": "unknown",
                            "complexity": "Unknown",
                            "severity": "CRITICAL",
                            "_score": 99,
                            "issue": "Engine Crash",
                            "suggestion": str(e),
                        }
                    )

            # --- 2. SORT & PRIORITIZE ---

            # Sort by highest severity score first. If tied, sort alphabetically by file to ensure determinism.

            raw_issues.sort(key=lambda x: (x["_score"], x["file"]), reverse=True)

            # Strip the internal sorting key for clean JSON

            for issue in raw_issues:
                del issue["_score"]

            # --- 3. BUILD THE PAYLOAD ---

            payload = {}

            if not raw_issues:
                # Clean Pass

                payload = {
                    "decision": {
                        "status": "PASS",
                        "reason": "No performance regressions detected",
                        "confidence": "HIGH",
                    },
                    "primary_trigger": None,
                    "summary": {
                        "total_issues": 0,
                        "by_severity": {
                            "CRITICAL": 0,
                            "HIGH": 0,
                            "MEDIUM": 0,
                            "LOW": 0,
                        },
                        "worst_complexity": "O(1)",
                        "risk_level": "LOW",
                    },
                    "issues": [],
                    "metadata": {
                        "scanned_files": len(args.files),
                        "analysis_mode": "fast",
                        "timestamp": datetime.now(timezone.utc)
                        .isoformat()
                        .replace("+00:00", "Z"),
                    },
                }

            else:
                # Issues Found

                primary = raw_issues[0]

                # --- 3. BUILD THE PAYLOAD ---

                # Aggregate Summary
                summary = {
                    "total_issues": len(raw_issues),
                    "by_severity": {"CRITICAL": 0, "HIGH": 0, "MEDIUM": 0, "LOW": 0},
                    "worst_complexity": primary["complexity"],
                    "risk_level": primary["severity"],
                }
                for issue in raw_issues:
                    if issue["severity"] in summary["by_severity"]:
                        summary["by_severity"][issue["severity"]] += 1

                # --- THE STRICT GATE DECISION MODEL ---
                primary_score = severity_scoring.get(primary["complexity"], {}).get(
                    "score", 99
                )

                if primary["severity"] == "CRITICAL":
                    # Hard Rule: CRITICAL issues ALWAYS block. They cannot be bypassed.
                    is_blocked = True
                    reason = f"CRITICAL performance risk detected ({primary['complexity']} scaling)"
                elif primary_score >= fail_score:
                    # Threshold Rule: HIGH/MEDIUM issues block if they cross the user's defined limit.
                    is_blocked = True
                    reason = f"Performance regression threshold exceeded ({primary['complexity']} scaling)"
                else:
                    # Clean Pass
                    is_blocked = False
                    reason = "Code scales efficiently within acceptable limits."

                payload = {
                    "decision": {
                        "status": "BLOCK" if is_blocked else "PASS",
                        "reason": reason,
                        "confidence": "HIGH",
                    },
                    "primary_trigger": primary,
                    "summary": summary,
                    "issues": raw_issues,
                    "metadata": {
                        "scanned_files": len(args.files),
                        "analysis_mode": "fast",
                        "timestamp": datetime.now(timezone.utc)
                        .isoformat()
                        .replace("+00:00", "Z"),
                    },
                }

            # --- 4. OUTPUT ---

            print(json.dumps(payload, indent=2))

            if payload.get("decision", {}).get("status") == "BLOCK":
                sys.exit(1)

            sys.exit(0)
        else:
            parser.print_help()

main() staticmethod

Primary entry point for the Alnoms Command-Line Interface.

Configures the argument parser and routes execution based on the selected subcommand. Supports two primary modes: 1. analyze: Human-readable performance reporting with deep profiling options. 2. ci: Headless execution mode that accepts multiple files, outputs strict JSON, and enforces Big-O complexity guardrails via system exit codes.

Raises:

Type Description
SystemExit
  • Exits with 0 on successful execution and clean compliance.
  • Exits with 1 if an internal error occurs or if the ci mode detects a performance bottleneck that breaches the --fail-on threshold.
Source code in src/alnoms/cli.py
@staticmethod
def main() -> None:
    """Primary entry point for the Alnoms Command-Line Interface.

    Configures the argument parser and routes execution based on the selected
    subcommand. Supports two primary modes:
    1. `analyze`: Human-readable performance reporting with deep profiling options.
    2. `ci`: Headless execution mode that accepts multiple files, outputs strict
       JSON, and enforces Big-O complexity guardrails via system exit codes.

    Raises:
        SystemExit:
            - Exits with 0 on successful execution and clean compliance.
            - Exits with 1 if an internal error occurs or if the `ci` mode detects
              a performance bottleneck that breaches the `--fail-on` threshold.
    """
    parser = argparse.ArgumentParser(
        prog="alnoms",
        description="šŸ”¬ Alnoms: Performance Intelligence System",
        formatter_class=argparse.RawDescriptionHelpFormatter,
        epilog="""
Example Usage:
  alnoms analyze script.py
  alnoms analyze script.py --deep
  alnoms analyze script.py --deep --start-n 50 --rounds 3
  alnoms analyze script.py --json

  # CI/CD Execution
  alnoms ci file1.py file2.py --fail-on "O(N^3)"
            """,
    )

    # Global version flag
    parser.add_argument("-v", "--version", action="version", version="Alnoms 1.1.2")

    subparsers = parser.add_subparsers(dest="command", help="Commands")

    # --- 1. THE HUMAN COMMAND (analyze) ---
    analyze_parser = subparsers.add_parser(
        "analyze", help="Analyze a single Python file"
    )
    analyze_parser.add_argument("file", help="Python file path")

    audit_group = analyze_parser.add_argument_group("Analysis Options")
    audit_group.add_argument("--deep", action="store_true")
    audit_group.add_argument("--function", dest="target_override")
    audit_group.add_argument("--gen", dest="gen")
    audit_group.add_argument("--data", dest="data")
    audit_group.add_argument("--start-n", type=int, default=50)
    audit_group.add_argument("--rounds", type=int, default=3)

    output_group = analyze_parser.add_argument_group("Output Options")
    output_group.add_argument("--json", action="store_true")
    output_group.add_argument("--pretty", action="store_true")

    # --- 2. THE ROBOT COMMAND (ci) ---
    ci_parser = subparsers.add_parser(
        "ci", help="Headless execution for CI/CD pipelines"
    )
    ci_parser.add_argument("files", nargs="+", help="List of Python files to scan")
    ci_parser.add_argument(
        "--fail-on",
        default="",
        help="Complexity threshold to fail the build (e.g., O(N^3))",
    )

    args = parser.parse_args()

    if args.command == "analyze":
        try:
            result = ScriptAnalyzer.analyze_file(
                path=args.file,
                deep=args.deep,
                target_override=args.target_override,
                gen_name=args.gen,
                data_file=args.data,
                start_n=args.start_n,
                rounds=args.rounds,
            )

            if args.json or args.pretty:
                print(json.dumps(result, indent=2 if args.pretty else None))
                return

            PerformanceCLI.print_report(result)

        except Exception as e:
            print(f"āŒ Error analyzing {args.file}: {str(e)}")
            sys.exit(1)

    elif args.command == "ci":
        # --- SEVERITY & SCORING MODEL ---

        severity_scoring = {
            "O(2^N)": {"level": "CRITICAL", "score": 7},
            "O(N^3)": {"level": "CRITICAL", "score": 6},
            "O(N^2)": {"level": "HIGH", "score": 5},
            "O(N log N)": {"level": "MEDIUM", "score": 4},
            "O(N)": {"level": "LOW", "score": 3},
            "O(log N)": {"level": "LOW", "score": 2},
            "O(1)": {"level": "LOW", "score": 1},
        }

        fail_score = severity_scoring.get(args.fail_on, {}).get("score", 99)

        raw_issues = []

        # --- 1. GATHER EVIDENCE ---

        for file_path in args.files:
            try:
                with contextlib.redirect_stdout(io.StringIO()):
                    result = ScriptAnalyzer.analyze_file(path=file_path, deep=False)

                patterns = result.get("patterns", [])

                for p in patterns:
                    comp = p.get("static_complexity") or "O(N^2)"

                    meta = severity_scoring.get(comp, {"level": "HIGH", "score": 5})

                    raw_issues.append(
                        {
                            "file": file_path,
                            "function": p.get("function", "global"),
                            "complexity": comp,
                            "severity": meta["level"],
                            "_score": meta["score"],  # Internal sorting key
                            "issue": p.get("issue", "Unknown Bottleneck"),
                            "suggestion": p.get("explanation", ""),
                        }
                    )

            except Exception as e:
                # Handle internal engine crashes gracefully in the new schema

                raw_issues.append(
                    {
                        "file": file_path,
                        "function": "unknown",
                        "complexity": "Unknown",
                        "severity": "CRITICAL",
                        "_score": 99,
                        "issue": "Engine Crash",
                        "suggestion": str(e),
                    }
                )

        # --- 2. SORT & PRIORITIZE ---

        # Sort by highest severity score first. If tied, sort alphabetically by file to ensure determinism.

        raw_issues.sort(key=lambda x: (x["_score"], x["file"]), reverse=True)

        # Strip the internal sorting key for clean JSON

        for issue in raw_issues:
            del issue["_score"]

        # --- 3. BUILD THE PAYLOAD ---

        payload = {}

        if not raw_issues:
            # Clean Pass

            payload = {
                "decision": {
                    "status": "PASS",
                    "reason": "No performance regressions detected",
                    "confidence": "HIGH",
                },
                "primary_trigger": None,
                "summary": {
                    "total_issues": 0,
                    "by_severity": {
                        "CRITICAL": 0,
                        "HIGH": 0,
                        "MEDIUM": 0,
                        "LOW": 0,
                    },
                    "worst_complexity": "O(1)",
                    "risk_level": "LOW",
                },
                "issues": [],
                "metadata": {
                    "scanned_files": len(args.files),
                    "analysis_mode": "fast",
                    "timestamp": datetime.now(timezone.utc)
                    .isoformat()
                    .replace("+00:00", "Z"),
                },
            }

        else:
            # Issues Found

            primary = raw_issues[0]

            # --- 3. BUILD THE PAYLOAD ---

            # Aggregate Summary
            summary = {
                "total_issues": len(raw_issues),
                "by_severity": {"CRITICAL": 0, "HIGH": 0, "MEDIUM": 0, "LOW": 0},
                "worst_complexity": primary["complexity"],
                "risk_level": primary["severity"],
            }
            for issue in raw_issues:
                if issue["severity"] in summary["by_severity"]:
                    summary["by_severity"][issue["severity"]] += 1

            # --- THE STRICT GATE DECISION MODEL ---
            primary_score = severity_scoring.get(primary["complexity"], {}).get(
                "score", 99
            )

            if primary["severity"] == "CRITICAL":
                # Hard Rule: CRITICAL issues ALWAYS block. They cannot be bypassed.
                is_blocked = True
                reason = f"CRITICAL performance risk detected ({primary['complexity']} scaling)"
            elif primary_score >= fail_score:
                # Threshold Rule: HIGH/MEDIUM issues block if they cross the user's defined limit.
                is_blocked = True
                reason = f"Performance regression threshold exceeded ({primary['complexity']} scaling)"
            else:
                # Clean Pass
                is_blocked = False
                reason = "Code scales efficiently within acceptable limits."

            payload = {
                "decision": {
                    "status": "BLOCK" if is_blocked else "PASS",
                    "reason": reason,
                    "confidence": "HIGH",
                },
                "primary_trigger": primary,
                "summary": summary,
                "issues": raw_issues,
                "metadata": {
                    "scanned_files": len(args.files),
                    "analysis_mode": "fast",
                    "timestamp": datetime.now(timezone.utc)
                    .isoformat()
                    .replace("+00:00", "Z"),
                },
            }

        # --- 4. OUTPUT ---

        print(json.dumps(payload, indent=2))

        if payload.get("decision", {}).get("status") == "BLOCK":
            sys.exit(1)

        sys.exit(0)
    else:
        parser.print_help()

print_report(result) staticmethod

Renders a full performance intelligence report in human-readable format.

This method produces a structured analysis report that includes detected performance patterns, static diagnostics, dynamic profiling bottlenecks, empirical scaling analysis, performance verdict, and simulated fixes.

Parameters:

Name Type Description Default
result dict

Output from ScriptAnalyzer.analyze_file() containing: - 'patterns' (list): Detected performance patterns. - 'profile' (list): Dynamic profiling data. - 'empirical' (list): Empirical scaling analysis data. - 'meta' (dict): Metadata including timestamp and version. - 'empirical_target' (str): The target function for scaling tests. - 'total_time' (float): Total execution time. - 'file' (str): The analyzed file path.

required
Source code in src/alnoms/cli.py
@staticmethod
def print_report(result: dict) -> None:
    """Renders a full performance intelligence report in human-readable format.

    This method produces a structured analysis report that includes detected
    performance patterns, static diagnostics, dynamic profiling bottlenecks,
    empirical scaling analysis, performance verdict, and simulated fixes.

    Args:
        result (dict): Output from `ScriptAnalyzer.analyze_file()` containing:
            - 'patterns' (list): Detected performance patterns.
            - 'profile' (list): Dynamic profiling data.
            - 'empirical' (list): Empirical scaling analysis data.
            - 'meta' (dict): Metadata including timestamp and version.
            - 'empirical_target' (str): The target function for scaling tests.
            - 'total_time' (float): Total execution time.
            - 'file' (str): The analyzed file path.
    """
    meta = result.get("meta", {})
    patterns = result.get("patterns", [])
    profile_data = result.get("profile", [])
    empirical_data = result.get("empirical")
    target_name = result.get("empirical_target")

    print("\n==================================================")
    print("āš–ļø PERFORMANCE REPORT")
    print("==================================================")
    print(f"File: {result.get('file', 'Unknown')}")
    print(f"Timestamp (UTC): {meta.get('timestamp', 'Unknown')}")
    print(f"Total Execution Time: {result.get('total_time', 0)}s\n")

    # ---------------------------------------------------------
    # 0. DETECTED INTENT
    # ---------------------------------------------------------
    if patterns:
        primary = patterns[0]
        intent = (
            primary.get("intent")
            or primary.get("issue")
            or primary.get("pattern_id")
        )
        print("🧠 DETECTED INTENT:")
        print(f"   {intent}\n")

    # ---------------------------------------------------------
    # 1. STATIC ANALYSIS & SUGGESTIONS
    # ---------------------------------------------------------
    print("🚨 STATIC ANALYSIS (Diagnostics & Suggestions)")
    print("-" * 50)

    if not patterns:
        print(
            "   āœ… No performance issues detected. Code is structurally efficient.\n"
        )
    else:
        for i, p in enumerate(patterns, 1):
            func_name = p.get("function", "global")
            line_no = p.get("line", "??")
            issue = p.get("issue", p.get("pattern_id"))

            print(
                f"{i}. āš ļø ISSUE: {issue} (Function: {func_name} | Line: {line_no})"
            )

            if "explanation" in p:
                print(f"   šŸ“– Explanation: {p['explanation']}")

                dsa = p.get("dsa_meta")
                if dsa:
                    complexity = dsa.get("complexity", "O(N)")
                    module_path = dsa.get("module", "builtin")
                    tier = dsa.get("tier", "OSS")

                    print(f"   šŸ’Š RECOMMENDED OPTIMIZATION: {complexity}")
                    print(f"   šŸ—ļø IMPLEMENTATION: {module_path}")
                    print(f"   šŸ” ACCESS TIER: {tier}")

                costs = p.get("cost_estimate", {})
                if "time" in costs:
                    print(f"   ā±ļø Complexity Shift: {costs['time']}")

                snip = p.get("snippets")
                if snip:
                    print("\n   šŸ’” SUGGESTED FIX:")
                    print("   --- BEFORE ---")
                    for line in snip["before"].split("\n"):
                        print(f"   |  {line}")
                    print("   --- AFTER ---")
                    for line in snip["after"].split("\n"):
                        print(f"   |  {line}")

            print()

    # ---------------------------------------------------------
    # 2. DYNAMIC PROFILING
    # ---------------------------------------------------------
    print("ā±ļø DYNAMIC PROFILING (Top Execution Bottlenecks)")
    print("-" * 50)

    if not profile_data:
        print("   āœ… No performance bottlenecks detected in execution.\n")
    else:
        for i, func in enumerate(profile_data, 1):
            print(
                f"   {i}. {func['function']}() -> {func['time']}s ({func['percent']}%)"
            )
        print()

    # ---------------------------------------------------------
    # 3. EMPIRICAL SCALING
    # ---------------------------------------------------------
    if empirical_data:
        print(f"šŸ“ˆ EMPIRICAL SCALING ANALYSIS: {target_name}()")
        print("-" * 50)
        print(
            f"{'N':<10} | {'Time (s)':<12} | {'Ratio':<8} | {'Est. Complexity':<15}"
        )
        print("-" * 50)

        final_complexity = "O(1)"
        for row in empirical_data:
            r_str = f"{row['Ratio']:.2f}" if row["Ratio"] > 0 else "-"
            print(
                f"{row['N']:<10} | {row['Time']:<12.5f} | {r_str:<8} | {row['Complexity']:<15}"
            )
            final_complexity = row["Complexity"]

        # ---------------------------------------------------------
        # 4. VERDICT
        # ---------------------------------------------------------
        print("\nāš–ļø VERDICT:")

        safe_tiers = ["O(1)", "O(log N)", "O(N)", "O(N log N)"]

        if final_complexity in safe_tiers:
            print(
                f"āœ… PASSED: Function operates at {final_complexity}. Safe for scaling."
            )
        elif final_complexity == "O(N^2)":
            print(
                f"āš ļø WARNING: Function operates at {final_complexity}. May not scale efficiently."
            )
        else:
            print(
                f"āŒ RISK: Function operates at {final_complexity}. Review recommended."
            )

        # ---------------------------------------------------------
        # 5. CONTEXT
        # ---------------------------------------------------------
        print("\nšŸ“Œ CONTEXT")
        print("-" * 50)
        print(
            "   Empirical scaling validates asymptotic behavior under increasing load.\n"
        )

        # ---------------------------------------------------------
        # 6. IMPACT ESTIMATION
        # ---------------------------------------------------------
        print("šŸš€ EXPECTED IMPACT")
        print("-" * 50)
        print("   For N = 10,000:")
        print("     • O(N²) → ~100,000,000 operations")
        print("     • O(N)  → ~10,000 operations")
        print("   Estimated improvement: 100×–1000Ɨ depending on workload.\n")

        # ---------------------------------------------------------
        # 7. CONFIDENCE
        # ---------------------------------------------------------
        print("šŸ¤– CONFIDENCE")
        print("-" * 50)

        if final_complexity == "O(N^2)":
            print("   High — static and empirical signals agree.\n")
        else:
            print("   Medium — mixed signals between analysis methods.\n")

        # ---------------------------------------------------------
        # 8. SIMULATED FIX
        # ---------------------------------------------------------
        print("šŸ” AFTER OPTIMIZATION (SIMULATED)")
        print("-" * 50)
        print("   Expected Complexity: O(N)")
        print("   Behavior: Linear scaling with stable performance.")
        print("   Suggested Implementation:")
        print("       s = set(arr)")
        print("       for x in arr:")
        print("           if x in s:")
        print("               total += x\n")

    else:
        print("ā„¹ļø EMPIRICAL ANALYSIS SKIPPED\n")

    print("==================================================\n")