From 3dbdad06a8e06abcf9a14c5d6a75d644c92ef7ac Mon Sep 17 00:00:00 2001 From: codeasier Date: Sat, 3 Oct 2026 11:44:09 +0800 Subject: [PATCH 1/2] refactor(cli): split command groups and shared output adapters --- AGENTS.md | 10 +- src/pt_snap_cli/AGENTS.md | 2 + src/pt_snap_cli/cli.py | 613 +----------------- src/pt_snap_cli/cli_output.py | 214 ++++++ src/pt_snap_cli/cli_reports.py | 120 ++++ src/pt_snap_cli/cli_skills.py | 277 ++++++++ .../test_memory_fragmentation_contract.py | 8 +- .../test_memory_peak_breakdown_contract.py | 10 +- 8 files changed, 666 insertions(+), 588 deletions(-) create mode 100644 src/pt_snap_cli/cli_output.py create mode 100644 src/pt_snap_cli/cli_reports.py create mode 100644 src/pt_snap_cli/cli_skills.py diff --git a/AGENTS.md b/AGENTS.md index 106a89b..2132622 100644 --- a/AGENTS.md +++ b/AGENTS.md @@ -23,7 +23,8 @@ Run these commands from the repository root. - Apply formatting: `black .` - Type check: `python -m basedpyright --pythonpath "$(python -c 'import sys; print(sys.executable)')"` (covers all of `src/pt_snap_cli`, including `snapshot/`; the gate is zero - errors and warnings are informational. Do not add a snapshot + errors; API/model/representation Any/Unknown checks are errors and other + warnings are informational. Do not add a snapshot `executionEnvironments` downgrade.) `tests/run_tests.sh` is tied to a developer-specific Conda path and writes @@ -51,6 +52,7 @@ when they exist. | Surface | Entry point | Responsibility | | --- | --- | --- | | CLI | `src/pt_snap_cli/cli.py` via `pt_snap_cli.cli:_safe_call` | Typer commands for focus, import, split, metadata, capabilities, overview, query, reports, config, and skill install/list | +| CLI groups/output | `src/pt_snap_cli/cli_reports.py`, `cli_skills.py`, `cli_output.py` | Report/skill command registration and shared terminal/JSON error adaptation; registered by `cli.py` | | Python API | `src/pt_snap_cli/api.py` (`SnapshotAnalyzer`) | Programmatic focus, query, and metadata facade | | Product services | `src/pt_snap_cli/core/` | Shared focus, import, split, query, report, metadata, and error semantics | | Database access | `src/pt_snap_cli/context.py` | Read-only SQLite validation, connection management, and device discovery | @@ -147,15 +149,15 @@ templates under category subdirectories are included by | Change | Start here | Focused tests | | --- | --- | --- | -| CLI options or terminal rendering | `src/pt_snap_cli/cli.py` and the owning core service | `tests/test_cli.py`, `tests/test_completion.py` | +| CLI options or terminal rendering | `src/pt_snap_cli/cli.py`, `cli_reports.py`, `cli_skills.py`, `cli_output.py`, and the owning core service | `tests/test_cli.py`, `tests/test_cli_json.py`, `tests/test_completion.py` | | Focus precedence or persistence | `src/pt_snap_cli/config.py`, `src/pt_snap_cli/core/focus_service.py`, `src/pt_snap_cli/context.py` | `tests/test_config.py`, `tests/test_context.py`, `tests/core/test_focus_service.py` | | Context/executor caching | `src/pt_snap_cli/core/context_cache.py`, `src/pt_snap_cli/api.py`, `src/pt_snap_cli/core/query_service.py` | `tests/core/test_context_cache.py`, `tests/test_snapshot_analyzer_cache.py`, `tests/test_query_cache_perf.py` | | Query schema, SQL, or categories | `src/pt_snap_cli/query/`, `src/pt_snap_cli/core/query_service.py` | `tests/query/`, `tests/core/test_query_service.py` | | Python API behavior | `src/pt_snap_cli/api.py` | `tests/test_api.py`, `tests/test_contract_cli_api.py` | | Snapshot import or metadata | `src/pt_snap_cli/core/import_service.py`, `src/pt_snap_cli/core/import_metadata.py`, `src/pt_snap_cli/core/snapshot_import_backend.py` | `tests/core/test_import_*.py`, `tests/test_snapshot_db.py` | | Snapshot splitting or replay | `src/pt_snap_cli/core/split_service.py`, `src/pt_snap_cli/snapshot/` | `tests/core/test_split_service.py`, `tests/snapshot/` | -| Reports | `src/pt_snap_cli/core/report_service.py`, report commands in `src/pt_snap_cli/cli.py` | `tests/core/test_report_service.py`, report cases in `tests/test_cli.py` | -| Skill list/install destinations or catalog packaging | `src/pt_snap_cli/core/skill_service.py`, `src/pt_snap_cli/bundled_skills/`, skill commands in `src/pt_snap_cli/cli.py` | `tests/core/test_skill_service.py`, `tests/test_bundled_skills.py`, skill cases in `tests/test_cli.py` | +| Reports | `src/pt_snap_cli/core/report_service.py`, `src/pt_snap_cli/cli_reports.py` | `tests/core/test_report_service.py`, report cases in `tests/test_cli.py` | +| Skill list/install destinations or catalog packaging | `src/pt_snap_cli/core/skill_service.py`, `src/pt_snap_cli/bundled_skills/`, `src/pt_snap_cli/cli_skills.py` | `tests/core/test_skill_service.py`, `tests/test_bundled_skills.py`, skill cases in `tests/test_cli.py` | | Agent setup, Ascend NPU collection, diagnostics, or skill evaluation | `skills/`, `tests/skills/`, and the referenced CLI/query surfaces | `tests/skills/` | | Packaging or release | `pyproject.toml`, `.github/workflows/` | `tests/test_package.py`, `tests/test_release_workflow.py` | | Executable fixtures | `tests/fixtures/snapshots/` | `tests/test_fixture_provenance.py` before any deserializing suite | diff --git a/src/pt_snap_cli/AGENTS.md b/src/pt_snap_cli/AGENTS.md index d489ae5..25d8149 100644 --- a/src/pt_snap_cli/AGENTS.md +++ b/src/pt_snap_cli/AGENTS.md @@ -12,6 +12,8 @@ | `__init__.py` | Package exports and version wiring. | | `api.py` | High-level `SnapshotAnalyzer` API for library callers. | | `cli.py` | Typer CLI entrypoint for focus, import, split, metadata, capabilities, overview, query, report, config, and skill commands. | +| `cli_reports.py`, `cli_skills.py` | Report and skill Typer groups registered by `cli.py`; call shared core services. | +| `cli_output.py` | Common JSON-mode state, error emission, and terminal output helpers for all command modules. | | `completion.py` | Shell completion helpers for templates, categories, and device IDs. | | `config.py` | Atomic focus persistence and resolution across explicit paths, environment, project focus, and legacy global config. | | `context.py` | Read-only SQLite context with schema validation and device discovery. | diff --git a/src/pt_snap_cli/cli.py b/src/pt_snap_cli/cli.py index 5be27c7..620f864 100644 --- a/src/pt_snap_cli/cli.py +++ b/src/pt_snap_cli/cli.py @@ -5,14 +5,11 @@ import json import logging import shlex -import shutil import sys -import textwrap from collections.abc import Mapping, Sequence -from contextvars import ContextVar from dataclasses import asdict from pathlib import Path -from typing import Annotated, Any, Literal, NoReturn, cast +from typing import Annotated, cast import typer from typer.exceptions import Abort, Exit @@ -28,18 +25,44 @@ ClickAbort = Abort from pt_snap_cli import __version__ +from pt_snap_cli.cli_output import ( + JSON_MODE as _JSON_MODE, +) +from pt_snap_cli.cli_output import ( + echo_callstack_layout as _echo_callstack_layout, +) +from pt_snap_cli.cli_output import ( + emit_json as _emit_json, +) +from pt_snap_cli.cli_output import ( + error as _error, +) +from pt_snap_cli.cli_output import ( + error_from_exc as _error_from_exc, +) +from pt_snap_cli.cli_output import ( + json_flag as _json_flag, +) +from pt_snap_cli.cli_output import ( + json_mode as _json_mode, +) +from pt_snap_cli.cli_output import ( + print_database_overview as _print_database_overview, +) +from pt_snap_cli.cli_output import ( + print_skill_listings as _print_skill_listings, +) +from pt_snap_cli.cli_reports import report_app +from pt_snap_cli.cli_skills import skill_app from pt_snap_cli.completion import ( complete_categories, complete_device_ids, - complete_skill_names, - complete_skill_targets, complete_template_names, ) from pt_snap_cli.config import ENV_DB_PATH from pt_snap_cli.core import ( CapabilityService, DatabaseMissingError, - DatabaseOverview, DatabaseSchemaError, FocusFileInvalidError, FocusNotConfiguredError, @@ -53,19 +76,11 @@ ImportToolMissingError, InvalidCategoryError, InvalidDeviceError, - InvalidSkillTargetError, JsonValue, OverviewService, - PeakMemoryReport, QueryExecutionError, QueryService, - ReportService, SkillCatalogError, - SkillInstallError, - SkillInstallReport, - SkillListing, - SkillNotFoundError, - SkillService, SnapshotFileInvalidError, SplitError, SplitOptions, @@ -74,19 +89,11 @@ TemplateInfo, TemplateNotFoundError, TemplateRenderError, - classify_error, dumps_json, json_error, json_success, ) from pt_snap_cli.core.error_codes import DATABASE_NOT_FOUND, ERROR, INVALID_PARAMETER -from pt_snap_cli.core.models import SKILL_RESTART_ACTIONS, SKILL_RESTART_HINT -from pt_snap_cli.core.skill_service import ( - format_skill_install_target, - format_skill_location, - human_skill_summary, - parse_host_option, -) from pt_snap_cli.query.config import OUTPUT_COLUMN_OPTIONAL from pt_snap_cli.query.executor import reported_sql_limit from pt_snap_cli.query.registry import discover_categories, get_query @@ -108,45 +115,10 @@ add_completion=True, context_settings={"help_option_names": ["-h", "--help"]}, ) -report_app = typer.Typer(help="Generate memory analysis reports") -skill_app = typer.Typer(help="Manage bundled agent skills", no_args_is_help=True) app.add_typer(report_app, name="report") app.add_typer(skill_app, name="skill") -# Typer 0.27+ vendors Click as typer._click. Reading click.get_current_context() -# therefore misses the live command and would keep JSON errors on stdout. -_JSON_MODE: ContextVar[bool] = ContextVar("pt_snap_json_mode", default=False) - - -def _json_mode() -> bool: - return _JSON_MODE.get() - - -def _set_json_mode(value: bool) -> bool: - _JSON_MODE.set(bool(value)) - return value - - -def _json_flag() -> Any: - return typer.Option( - "--json", - help="Emit machine-readable JSON", - callback=_set_json_mode, - ) - - -def _emit_json(payload: object) -> None: - try: - typer.echo(dumps_json(payload)) - except TypeError as exc: - _error( - str(exc), - code=ERROR, - hint="Result contained a value that cannot be serialized to JSON.", - ) - - def _focus_fields(state: FocusState) -> dict[str, object]: db_path = str(state.db_path) if state.db_path is not None else None return { @@ -231,7 +203,7 @@ def _query_service() -> QueryService: return QueryService(_focus_service()) -def _echo_output_schema_column(column: Mapping[str, Any]) -> None: +def _echo_output_schema_column(column: Mapping[str, object]) -> None: typer.echo(f" {column['column']}: {column['type']}") for key in OUTPUT_COLUMN_OPTIONAL: if key not in column: @@ -239,46 +211,12 @@ def _echo_output_schema_column(column: Mapping[str, Any]) -> None: value = column[key] if key == "interpretation_limits": typer.echo(" interpretation_limits:") - for item in value: + for item in cast(list[str], value): typer.echo(f" - {item}") else: typer.echo(f" {key}: {value}") -def _skill_service() -> SkillService: - return SkillService() - - -def _skill_dest_dir( - dest_dir: Path | None, - target: str | None, - *, - project: bool = False, - user: bool = False, -) -> Path | None: - if dest_dir is None: - return None - if target: - _error( - "--dir cannot be combined with --target.", - code=INVALID_PARAMETER, - hint="Use either --dir or --target.", - ) - if project: - _error( - "--dir cannot be combined with --project.", - code=INVALID_PARAMETER, - hint="Use either --dir or --project.", - ) - if user: - _error( - "--dir cannot be combined with --user.", - code=INVALID_PARAMETER, - hint="Use either --dir or --user.", - ) - return dest_dir - - def version_callback(value: bool) -> None: if value: typer.echo(f"pt-snap-cli version {__version__}") @@ -819,7 +757,7 @@ def query_database( state = None source = state.source if state is not None else "configured" path = state.db_path if state is not None else db_path - extra = [] + extra: list[str] = [] if state is not None and state.focus_file: extra.append(f"Focus file: {state.focus_file}") extra.append( @@ -849,435 +787,6 @@ def query_database( query_service.close() -@report_app.command("peak-memory") -def report_peak_memory( - db_path: Annotated[ - Path | None, typer.Argument(help="Path to database file (optional if configured)") - ] = None, - device: Annotated[ - int | None, - typer.Option( - "--device", - "-d", - help="Device ID to report", - autocompletion=complete_device_ids, - ), - ] = None, - metric: Annotated[ - Literal["active", "allocated", "reserved"], - typer.Option(help="Peak metric to report: active, allocated, or reserved"), - ] = "active", - include_static: Annotated[ - bool, - typer.Option("--include-static/--exclude-static", help="Include static memory group"), - ] = True, - limit: Annotated[int, typer.Option("--limit", "-n", help="Maximum callstack groups")] = 20, - json_output: Annotated[bool, _json_flag()] = False, -) -> None: - """Generate a peak memory attribution report.""" - focus_service = _focus_service() - report_service = ReportService(focus_service) - try: - report = report_service.peak_memory_report( - db_path=db_path, - device_id=device, - metric=metric, - include_static=include_static, - limit=limit, - ) - except ValueError as e: - _error(str(e), code=INVALID_PARAMETER, hint="Use --metric active, allocated, or reserved.") - except FocusFileInvalidError as e: - _error_from_exc(e) - except FocusNotConfiguredError as e: - _error_from_exc( - e, - extra_lines=( - "Use 'pt-snap focus ' to set a project database, or provide db_path argument.", - ), - ) - except DatabaseMissingError: - try: - state = focus_service.get_focus(explicit_db_path=db_path, explicit_device_id=device) - except FocusFileInvalidError: - state = None - source = state.source if state is not None else "configured" - path = state.db_path if state is not None else db_path - extra = [] - if state is not None and state.focus_file: - extra.append(f"Focus file: {state.focus_file}") - extra.append( - "Use 'pt-snap focus ' to set a new project database, or provide db_path argument." - ) - _error( - f"Database from {source} focus not found: {path}", - code=DATABASE_NOT_FOUND, - hint="Use 'pt-snap focus ' or pass a database path that exists.", - extra_lines=tuple(extra), - ) - except InvalidDeviceError as e: - _error_from_exc(e) - except ( - TemplateNotFoundError, - TemplateRenderError, - QueryExecutionError, - DatabaseSchemaError, - ) as e: - _error_from_exc(e, text_prefix="Error generating report: ") - finally: - report_service.close() - - if json_output: - typer.echo(json.dumps(asdict(report), indent=2)) - return - - _print_peak_memory_report(report) - - -def _print_peak_memory_report(report: PeakMemoryReport) -> None: - typer.secho("Peak memory report", fg=typer.colors.GREEN, bold=True) - typer.echo(f"Device: {report.device_id}") - typer.echo(f"Metric: {report.metric}") - typer.echo(f"Event ID: {report.event_id}") - typer.echo() - - typer.secho("Peak counters:", fg=typer.colors.GREEN, bold=True) - if report.peak: - typer.echo( - f" allocated: {report.peak.get('peak_allocated')} at event {report.peak.get('peak_allocated_event_id')}" - ) - typer.echo( - f" active: {report.peak.get('peak_active')} at event {report.peak.get('peak_active_event_id')}" - ) - typer.echo( - f" reserved: {report.peak.get('peak_reserved')} at event {report.peak.get('peak_reserved_event_id')}" - ) - else: - typer.echo(" No peak counters found.") - typer.echo() - - typer.secho("Allocator gap:", fg=typer.colors.GREEN, bold=True) - if report.allocator_gap: - gap = report.allocator_gap - typer.echo( - f" active peak event: {gap.get('peak_active_event_id')} reserved-active gap={gap.get('reserved_active_gap_at_active_peak')}" - ) - typer.echo( - f" allocated peak event: {gap.get('peak_allocated_event_id')} reserved-allocated gap={gap.get('reserved_allocated_gap_at_allocated_peak')}" - ) - typer.echo( - f" reserved peak event: {gap.get('peak_reserved_event_id')} reserved-active gap={gap.get('reserved_active_gap_at_reserved_peak')}" - ) - typer.echo(f" all peaks same event: {bool(gap.get('all_peaks_same_event'))}") - else: - typer.echo(" No allocator gap data found.") - typer.echo() - - typer.secho("Active memory by callstack:", fg=typer.colors.GREEN, bold=True) - if not report.callstack_groups: - typer.echo(" No active memory callstack groups found.") - return - for index, row in enumerate(report.callstack_groups, start=1): - typer.echo( - f" [{index}] {row.get('category')} {row.get('size_bytes')} bytes, {row.get('block_count')} blocks ({row.get('percent_of_active_blocks')}%)" - ) - typer.echo(f" {row.get('callstack')}") - - -@skill_app.command("list") -def skill_list( - target: Annotated[ - str | None, - typer.Option( - "--target", - "-t", - help="Comma-separated hosts to inspect: agents,claude,cursor,codex", - autocompletion=complete_skill_targets, - ), - ] = None, - project: Annotated[ - bool, typer.Option("--project", help="Inspect only project-local skill directories") - ] = False, - user: Annotated[ - bool, typer.Option("--user", help="Inspect only user-level skill directories") - ] = False, - dest_dir: Annotated[ - Path | None, - typer.Option( - "--dir", - help="Inspect this skills directory instead of built-in agent/Claude/Cursor/Codex paths", - ), - ] = None, - json_output: Annotated[bool, _json_flag()] = False, -) -> None: - """List bundled agent skills and whether they are installed.""" - if project and user: - _error( - "--project and --user cannot be used together.", - code=INVALID_PARAMETER, - hint="Choose either --project or --user.", - ) - custom_dir = _skill_dest_dir(dest_dir, target, project=project, user=user) - service = _skill_service() - try: - listings = service.list_skills( - hosts=None if custom_dir is not None else parse_host_option(target), - scopes=( - None - if custom_dir is not None - else (("project",) if project else (("user",) if user else None)) - ), - dest_dir=custom_dir, - ) - except (SkillCatalogError, InvalidSkillTargetError) as e: - _error_from_exc(e) - - if json_output: - typer.echo(json.dumps(service.listing_to_dict(listings), indent=2)) - return - - if not listings: - typer.echo("No bundled agent skills are available.") - return - - _print_skill_listings(listings) - - -@skill_app.command("install") -def skill_install( - names: Annotated[ - list[str] | None, - typer.Argument( - help="Skill names to install. Omit to install every bundled skill.", - autocompletion=complete_skill_names, - ), - ] = None, - target: Annotated[ - str | None, - typer.Option( - "--target", - "-t", - help="Comma-separated hosts: agents,claude,cursor,codex (default: agents,claude)", - autocompletion=complete_skill_targets, - ), - ] = None, - project: Annotated[ - bool, typer.Option("--project", help="Install into the current project") - ] = False, - force: Annotated[ - bool, typer.Option("--force", help="Replace existing skills that differ") - ] = False, - dest_dir: Annotated[ - Path | None, - typer.Option( - "--dir", - help="Install into this skills directory (Windows, other agents, or a custom path)", - ), - ] = None, - json_output: Annotated[bool, _json_flag()] = False, -) -> None: - """Install bundled agent skills into shared agent, Claude, Cursor, Codex, or custom directories.""" - custom_dir = _skill_dest_dir(dest_dir, target, project=project) - service = _skill_service() - try: - report = service.install_skills( - names, - hosts=None if custom_dir is not None else parse_host_option(target), - scope="project" if project else "user", - force=force, - dest_dir=custom_dir, - ) - except ( - SkillCatalogError, - SkillNotFoundError, - InvalidSkillTargetError, - SkillInstallError, - ) as e: - _error_from_exc(e) - - if json_output: - typer.echo(json.dumps(service.install_report_to_dict(report), indent=2)) - return - - _print_skill_mutation_report(report) - - -@skill_app.command("upgrade") -def skill_upgrade( - names: Annotated[ - list[str] | None, - typer.Argument( - help="Skill names to upgrade. Omit to upgrade every installed bundled skill.", - autocompletion=complete_skill_names, - ), - ] = None, - target: Annotated[ - str | None, - typer.Option( - "--target", - "-t", - help="Comma-separated hosts: agents,claude,cursor,codex (default: agents,claude)", - autocompletion=complete_skill_targets, - ), - ] = None, - project: Annotated[ - bool, typer.Option("--project", help="Upgrade skills in the current project") - ] = False, - dest_dir: Annotated[ - Path | None, - typer.Option( - "--dir", - help="Upgrade skills in this directory instead of a built-in host", - ), - ] = None, - json_output: Annotated[bool, _json_flag()] = False, -) -> None: - """Replace outdated installed skills with the bundled copies.""" - custom_dir = _skill_dest_dir(dest_dir, target, project=project) - service = _skill_service() - try: - report = service.upgrade_skills( - names, - hosts=None if custom_dir is not None else parse_host_option(target), - scope="project" if project else "user", - dest_dir=custom_dir, - ) - except ( - SkillCatalogError, - SkillNotFoundError, - InvalidSkillTargetError, - SkillInstallError, - ) as e: - _error_from_exc(e) - - if json_output: - typer.echo(json.dumps(service.install_report_to_dict(report), indent=2)) - return - - _print_skill_mutation_report(report) - - -@skill_app.command("uninstall") -def skill_uninstall( - names: Annotated[ - list[str] | None, - typer.Argument( - help="Skill names to uninstall. Omit to uninstall every bundled skill.", - autocompletion=complete_skill_names, - ), - ] = None, - target: Annotated[ - str | None, - typer.Option( - "--target", - "-t", - help=( - "Comma-separated hosts: agents,claude,cursor,codex. " - "Defaults to agents,claude when --project is set" - ), - autocompletion=complete_skill_targets, - ), - ] = None, - project: Annotated[ - bool, typer.Option("--project", help="Uninstall skills from the current project") - ] = False, - dest_dir: Annotated[ - Path | None, - typer.Option( - "--dir", - help="Uninstall skills from this directory instead of a built-in host", - ), - ] = None, - json_output: Annotated[bool, _json_flag()] = False, -) -> None: - """Remove bundled agent skills. - - Without --target, --project, or --dir, remove every SKILL.md copy that - `pt-snap skill list` would report. A same-named path without SKILL.md - aborts the whole uninstall. Use those flags to limit destinations. - """ - custom_dir = _skill_dest_dir(dest_dir, target, project=project) - service = _skill_service() - try: - unfiltered = custom_dir is None and target is None and not project - report = service.uninstall_skills( - names, - hosts=None if custom_dir is not None else parse_host_option(target), - scope="project" if project else "user", - dest_dir=custom_dir, - all_locations=unfiltered, - ) - except ( - SkillCatalogError, - SkillNotFoundError, - InvalidSkillTargetError, - SkillInstallError, - ) as e: - _error_from_exc(e) - - if json_output: - typer.echo(json.dumps(service.install_report_to_dict(report), indent=2)) - return - - _print_skill_mutation_report(report) - - -def _print_skill_listings(listings: list[SkillListing]) -> None: - width = max(shutil.get_terminal_size((80, 24)).columns - 2, 40) - width = min(width, 88) - status_colors = { - "installed": typer.colors.GREEN, - "outdated": typer.colors.YELLOW, - "missing": typer.colors.RED, - } - for index, item in enumerate(listings): - if index: - typer.echo() - typer.secho(item.name, fg=typer.colors.GREEN, bold=True) - locations = ", ".join( - format_skill_location(location) - for location in item.locations - if location.status != "missing" - ) - typer.echo(" Status ", nl=False) - typer.secho(item.status, fg=status_colors.get(item.status, typer.colors.WHITE)) - typer.echo(f" Locations {locations or '—'}") - summary = human_skill_summary(item.description) - if summary: - typer.echo( - textwrap.fill( - summary, - width=width, - initial_indent=" ", - subsequent_indent=" ", - ) - ) - - -def _print_skill_mutation_report(report: SkillInstallReport) -> None: - if not report.results: - typer.echo("No matching skills were found.") - return - - messages = { - "already_installed": "Already installed", - "updated": "Updated", - "installed": "Installed", - "not_installed": "Not installed", - "uninstalled": "Uninstalled", - } - for item in report.results: - prefix = messages.get(item.action, item.action.capitalize()) - line = f"{prefix} {item.name} -> {format_skill_install_target(item)}" - if item.action in {"installed", "updated", "uninstalled"}: - typer.secho(line, fg=typer.colors.GREEN) - else: - typer.echo(line) - if any(item.action in SKILL_RESTART_ACTIONS for item in report.results): - typer.echo() - typer.secho(SKILL_RESTART_HINT, fg=typer.colors.YELLOW) - - @app.command("config") def show_config( clear: Annotated[bool, typer.Option("--clear", help="Clear all configuration")] = False, @@ -1375,62 +884,6 @@ def show_database_overview( _print_database_overview(overview) -def _print_database_overview(overview: DatabaseOverview) -> None: - inspection = overview.metadata - typer.echo(f"Database: {overview.db_path}") - typer.echo(f"Focus source: {overview.focus_source}") - typer.echo(f"Import metadata: {inspection.status}") - if inspection.reason is not None: - typer.echo(f"Metadata reason: {inspection.reason}") - if not overview.devices: - typer.echo("Devices: none") - return - typer.echo("Devices:") - for device in overview.devices: - if device.first_event_id is None or device.last_event_id is None: - typer.echo(f" {device.device_id}: no events") - else: - typer.echo( - f" {device.device_id}: events {device.first_event_id}..{device.last_event_id}" - ) - - -def _echo_callstack_layout(layout: str | None, error: str | None = None) -> None: - if layout == "v1": - typer.echo("Callstack layout: v1 (inline text)") - elif layout == "v2": - typer.echo("Callstack layout: v2 (deduplicated)") - elif error: - typer.secho(f"Warning: {error}", fg=typer.colors.YELLOW) - - -def _error( - message: str, - *, - code: str | None = None, - hint: str | None = None, - extra_lines: tuple[str, ...] = (), - text_prefix: str = "Error: ", -) -> NoReturn: - if _json_mode(): - typer.echo(dumps_json(json_error(code or ERROR, message, hint)), err=True) - raise typer.Exit(1) from None - typer.secho(f"{text_prefix}{message}", fg=typer.colors.RED) - for line in extra_lines: - typer.echo(line) - raise typer.Exit(1) from None - - -def _error_from_exc( - exc: BaseException, - *, - extra_lines: tuple[str, ...] = (), - text_prefix: str = "Error: ", -) -> NoReturn: - code, hint = classify_error(exc) - _error(str(exc), code=code, hint=hint, extra_lines=extra_lines, text_prefix=text_prefix) - - # Flag-only options used when a parse failure happens before the --json callback. # A following --json is treated as a value of the previous option otherwise. _FLAG_ONLY_OPTIONS = frozenset( diff --git a/src/pt_snap_cli/cli_output.py b/src/pt_snap_cli/cli_output.py new file mode 100644 index 0000000..8be388e --- /dev/null +++ b/src/pt_snap_cli/cli_output.py @@ -0,0 +1,214 @@ +"""Shared CLI output and JSON error adaptation; no product decisions live here.""" + +from __future__ import annotations + +import shutil +import textwrap +from contextvars import ContextVar +from typing import NoReturn, cast + +import typer +from typer.models import OptionInfo + +from pt_snap_cli.core import ( + DatabaseOverview, + PeakMemoryReport, + SkillInstallReport, + SkillListing, + classify_error, + dumps_json, + json_error, +) +from pt_snap_cli.core.error_codes import ERROR +from pt_snap_cli.core.models import SKILL_RESTART_ACTIONS, SKILL_RESTART_HINT +from pt_snap_cli.core.skill_service import ( + format_skill_install_target, + format_skill_location, + human_skill_summary, +) + +# Typer 0.27+ vendors Click, so its context cannot be read via click.get_current_context. +JSON_MODE: ContextVar[bool] = ContextVar("pt_snap_json_mode", default=False) + + +def json_mode() -> bool: + return JSON_MODE.get() + + +def _set_json_mode(value: bool) -> bool: + _ = JSON_MODE.set(bool(value)) + return value + + +def json_flag() -> OptionInfo: + return cast( + OptionInfo, + typer.Option("--json", help="Emit machine-readable JSON", callback=_set_json_mode), + ) + + +def emit_json(payload: object) -> None: + try: + typer.echo(dumps_json(payload)) + except TypeError as exc: + error( + str(exc), + code=ERROR, + hint="Result contained a value that cannot be serialized to JSON.", + ) + + +def error( + message: str, + *, + code: str | None = None, + hint: str | None = None, + extra_lines: tuple[str, ...] = (), + text_prefix: str = "Error: ", +) -> NoReturn: + if json_mode(): + typer.echo(dumps_json(json_error(code or ERROR, message, hint)), err=True) + raise typer.Exit(1) from None + typer.secho(f"{text_prefix}{message}", fg=typer.colors.RED) + for line in extra_lines: + typer.echo(line) + raise typer.Exit(1) from None + + +def error_from_exc( + exc: BaseException, + *, + extra_lines: tuple[str, ...] = (), + text_prefix: str = "Error: ", +) -> NoReturn: + code, hint = classify_error(exc) + error(str(exc), code=code, hint=hint, extra_lines=extra_lines, text_prefix=text_prefix) + + +def echo_callstack_layout(layout: str | None, error: str | None = None) -> None: + if layout == "v1": + typer.echo("Callstack layout: v1 (inline text)") + elif layout == "v2": + typer.echo("Callstack layout: v2 (deduplicated)") + elif error: + typer.secho(f"Warning: {error}", fg=typer.colors.YELLOW) + + +def print_peak_memory_report(report: PeakMemoryReport) -> None: + typer.secho("Peak memory report", fg=typer.colors.GREEN, bold=True) + typer.echo(f"Device: {report.device_id}") + typer.echo(f"Metric: {report.metric}") + typer.echo(f"Event ID: {report.event_id}") + typer.echo() + + typer.secho("Peak counters:", fg=typer.colors.GREEN, bold=True) + if report.peak: + typer.echo( + f" allocated: {report.peak.get('peak_allocated')} at event {report.peak.get('peak_allocated_event_id')}" + ) + typer.echo( + f" active: {report.peak.get('peak_active')} at event {report.peak.get('peak_active_event_id')}" + ) + typer.echo( + f" reserved: {report.peak.get('peak_reserved')} at event {report.peak.get('peak_reserved_event_id')}" + ) + else: + typer.echo(" No peak counters found.") + typer.echo() + + typer.secho("Allocator gap:", fg=typer.colors.GREEN, bold=True) + if report.allocator_gap: + gap = report.allocator_gap + typer.echo( + f" active peak event: {gap.get('peak_active_event_id')} reserved-active gap={gap.get('reserved_active_gap_at_active_peak')}" + ) + typer.echo( + f" allocated peak event: {gap.get('peak_allocated_event_id')} reserved-allocated gap={gap.get('reserved_allocated_gap_at_allocated_peak')}" + ) + typer.echo( + f" reserved peak event: {gap.get('peak_reserved_event_id')} reserved-active gap={gap.get('reserved_active_gap_at_reserved_peak')}" + ) + typer.echo(f" all peaks same event: {bool(gap.get('all_peaks_same_event'))}") + else: + typer.echo(" No allocator gap data found.") + typer.echo() + + typer.secho("Active memory by callstack:", fg=typer.colors.GREEN, bold=True) + if not report.callstack_groups: + typer.echo(" No active memory callstack groups found.") + return + for index, row in enumerate(report.callstack_groups, start=1): + typer.echo( + f" [{index}] {row.get('category')} {row.get('size_bytes')} bytes, {row.get('block_count')} blocks ({row.get('percent_of_active_blocks')}%)" + ) + typer.echo(f" {row.get('callstack')}") + + +def print_skill_listings(listings: list[SkillListing]) -> None: + width = max(shutil.get_terminal_size((80, 24)).columns - 2, 40) + width = min(width, 88) + status_colors = { + "installed": typer.colors.GREEN, + "outdated": typer.colors.YELLOW, + "missing": typer.colors.RED, + } + for index, item in enumerate(listings): + if index: + typer.echo() + typer.secho(item.name, fg=typer.colors.GREEN, bold=True) + locations = ", ".join( + format_skill_location(location) + for location in item.locations + if location.status != "missing" + ) + typer.echo(" Status ", nl=False) + typer.secho(item.status, fg=status_colors.get(item.status, typer.colors.WHITE)) + typer.echo(f" Locations {locations or '—'}") + summary = human_skill_summary(item.description) + if summary: + typer.echo( + textwrap.fill(summary, width=width, initial_indent=" ", subsequent_indent=" ") + ) + + +def print_skill_mutation_report(report: SkillInstallReport) -> None: + if not report.results: + typer.echo("No matching skills were found.") + return + messages = { + "already_installed": "Already installed", + "updated": "Updated", + "installed": "Installed", + "not_installed": "Not installed", + "uninstalled": "Uninstalled", + } + for item in report.results: + prefix = messages.get(item.action, item.action.capitalize()) + line = f"{prefix} {item.name} -> {format_skill_install_target(item)}" + if item.action in {"installed", "updated", "uninstalled"}: + typer.secho(line, fg=typer.colors.GREEN) + else: + typer.echo(line) + if any(item.action in SKILL_RESTART_ACTIONS for item in report.results): + typer.echo() + typer.secho(SKILL_RESTART_HINT, fg=typer.colors.YELLOW) + + +def print_database_overview(overview: DatabaseOverview) -> None: + inspection = overview.metadata + typer.echo(f"Database: {overview.db_path}") + typer.echo(f"Focus source: {overview.focus_source}") + typer.echo(f"Import metadata: {inspection.status}") + if inspection.reason is not None: + typer.echo(f"Metadata reason: {inspection.reason}") + if not overview.devices: + typer.echo("Devices: none") + return + typer.echo("Devices:") + for device in overview.devices: + if device.first_event_id is None or device.last_event_id is None: + typer.echo(f" {device.device_id}: no events") + else: + typer.echo( + f" {device.device_id}: events {device.first_event_id}..{device.last_event_id}" + ) diff --git a/src/pt_snap_cli/cli_reports.py b/src/pt_snap_cli/cli_reports.py new file mode 100644 index 0000000..4923846 --- /dev/null +++ b/src/pt_snap_cli/cli_reports.py @@ -0,0 +1,120 @@ +"""Report command group, backed by the shared report service.""" + +from __future__ import annotations + +import json +from dataclasses import asdict +from pathlib import Path +from typing import Annotated, Literal + +import typer + +from pt_snap_cli.cli_output import ( + error as _error, +) +from pt_snap_cli.cli_output import ( + error_from_exc as _error_from_exc, +) +from pt_snap_cli.cli_output import ( + json_flag as _json_flag, +) +from pt_snap_cli.cli_output import ( + print_peak_memory_report as _print_peak_memory_report, +) +from pt_snap_cli.completion import complete_device_ids +from pt_snap_cli.core import ( + DatabaseMissingError, + DatabaseSchemaError, + FocusFileInvalidError, + FocusNotConfiguredError, + FocusService, + InvalidDeviceError, + QueryExecutionError, + ReportService, + TemplateNotFoundError, + TemplateRenderError, +) +from pt_snap_cli.core.error_codes import DATABASE_NOT_FOUND, INVALID_PARAMETER + +report_app = typer.Typer(help="Generate memory analysis reports") + + +@report_app.command("peak-memory") +def report_peak_memory( + db_path: Annotated[ + Path | None, typer.Argument(help="Path to database file (optional if configured)") + ] = None, + device: Annotated[ + int | None, + typer.Option( + "--device", "-d", help="Device ID to report", autocompletion=complete_device_ids + ), + ] = None, + metric: Annotated[ + Literal["active", "allocated", "reserved"], + typer.Option(help="Peak metric to report: active, allocated, or reserved"), + ] = "active", + include_static: Annotated[ + bool, + typer.Option("--include-static/--exclude-static", help="Include static memory group"), + ] = True, + limit: Annotated[int, typer.Option("--limit", "-n", help="Maximum callstack groups")] = 20, + json_output: Annotated[bool, _json_flag()] = False, +) -> None: + """Generate a peak memory attribution report.""" + focus_service = FocusService() + report_service = ReportService(focus_service) + try: + report = report_service.peak_memory_report( + db_path=db_path, + device_id=device, + metric=metric, + include_static=include_static, + limit=limit, + ) + except ValueError as e: + _error(str(e), code=INVALID_PARAMETER, hint="Use --metric active, allocated, or reserved.") + except FocusFileInvalidError as e: + _error_from_exc(e) + except FocusNotConfiguredError as e: + _error_from_exc( + e, + extra_lines=( + "Use 'pt-snap focus ' to set a project database, or provide db_path argument.", + ), + ) + except DatabaseMissingError: + try: + state = focus_service.get_focus(explicit_db_path=db_path, explicit_device_id=device) + except FocusFileInvalidError: + state = None + source = state.source if state is not None else "configured" + path = state.db_path if state is not None else db_path + extra: list[str] = [] + if state is not None and state.focus_file: + extra.append(f"Focus file: {state.focus_file}") + extra.append( + "Use 'pt-snap focus ' to set a new project database, or provide db_path argument." + ) + _error( + f"Database from {source} focus not found: {path}", + code=DATABASE_NOT_FOUND, + hint="Use 'pt-snap focus ' or pass a database path that exists.", + extra_lines=tuple(extra), + ) + except InvalidDeviceError as e: + _error_from_exc(e) + except ( + TemplateNotFoundError, + TemplateRenderError, + QueryExecutionError, + DatabaseSchemaError, + ) as e: + _error_from_exc(e, text_prefix="Error generating report: ") + finally: + report_service.close() + + if json_output: + typer.echo(json.dumps(asdict(report), indent=2)) + return + _print_peak_memory_report(report) diff --git a/src/pt_snap_cli/cli_skills.py b/src/pt_snap_cli/cli_skills.py new file mode 100644 index 0000000..14e8ea9 --- /dev/null +++ b/src/pt_snap_cli/cli_skills.py @@ -0,0 +1,277 @@ +"""Skill command group; destination and mutation semantics belong to SkillService.""" + +from __future__ import annotations + +import json +from pathlib import Path +from typing import Annotated + +import typer + +from pt_snap_cli.cli_output import ( + error as _error, +) +from pt_snap_cli.cli_output import ( + error_from_exc as _error_from_exc, +) +from pt_snap_cli.cli_output import ( + json_flag as _json_flag, +) +from pt_snap_cli.cli_output import ( + print_skill_listings as _print_skill_listings, +) +from pt_snap_cli.cli_output import ( + print_skill_mutation_report as _print_skill_mutation_report, +) +from pt_snap_cli.completion import complete_skill_names, complete_skill_targets +from pt_snap_cli.core import ( + InvalidSkillTargetError, + SkillCatalogError, + SkillInstallError, + SkillNotFoundError, + SkillService, +) +from pt_snap_cli.core.error_codes import INVALID_PARAMETER +from pt_snap_cli.core.skill_service import parse_host_option + +skill_app = typer.Typer(help="Manage bundled agent skills", no_args_is_help=True) + + +def _skill_dest_dir( + dest_dir: Path | None, + target: str | None, + *, + project: bool = False, + user: bool = False, +) -> Path | None: + if dest_dir is None: + return None + if target: + _error( + "--dir cannot be combined with --target.", + code=INVALID_PARAMETER, + hint="Use either --dir or --target.", + ) + if project: + _error( + "--dir cannot be combined with --project.", + code=INVALID_PARAMETER, + hint="Use either --dir or --project.", + ) + if user: + _error( + "--dir cannot be combined with --user.", + code=INVALID_PARAMETER, + hint="Use either --dir or --user.", + ) + return dest_dir + + +@skill_app.command("list") +def skill_list( + target: Annotated[ + str | None, + typer.Option( + "--target", + "-t", + help="Comma-separated hosts to inspect: agents,claude,cursor,codex", + autocompletion=complete_skill_targets, + ), + ] = None, + project: Annotated[ + bool, typer.Option("--project", help="Inspect only project-local skill directories") + ] = False, + user: Annotated[ + bool, typer.Option("--user", help="Inspect only user-level skill directories") + ] = False, + dest_dir: Annotated[ + Path | None, + typer.Option( + "--dir", + help="Inspect this skills directory instead of built-in agent/Claude/Cursor/Codex paths", + ), + ] = None, + json_output: Annotated[bool, _json_flag()] = False, +) -> None: + """List bundled agent skills and whether they are installed.""" + if project and user: + _error( + "--project and --user cannot be used together.", + code=INVALID_PARAMETER, + hint="Choose either --project or --user.", + ) + custom_dir = _skill_dest_dir(dest_dir, target, project=project, user=user) + service = SkillService() + try: + listings = service.list_skills( + hosts=None if custom_dir is not None else parse_host_option(target), + scopes=( + None + if custom_dir is not None + else (("project",) if project else (("user",) if user else None)) + ), + dest_dir=custom_dir, + ) + except (SkillCatalogError, InvalidSkillTargetError) as e: + _error_from_exc(e) + if json_output: + typer.echo(json.dumps(service.listing_to_dict(listings), indent=2)) + return + if not listings: + typer.echo("No bundled agent skills are available.") + return + _print_skill_listings(listings) + + +@skill_app.command("install") +def skill_install( + names: Annotated[ + list[str] | None, + typer.Argument( + help="Skill names to install. Omit to install every bundled skill.", + autocompletion=complete_skill_names, + ), + ] = None, + target: Annotated[ + str | None, + typer.Option( + "--target", + "-t", + help="Comma-separated hosts: agents,claude,cursor,codex (default: agents,claude)", + autocompletion=complete_skill_targets, + ), + ] = None, + project: Annotated[ + bool, typer.Option("--project", help="Install into the current project") + ] = False, + force: Annotated[ + bool, typer.Option("--force", help="Replace existing skills that differ") + ] = False, + dest_dir: Annotated[ + Path | None, + typer.Option( + "--dir", + help="Install into this skills directory (Windows, other agents, or a custom path)", + ), + ] = None, + json_output: Annotated[bool, _json_flag()] = False, +) -> None: + """Install bundled agent skills into shared agent, Claude, Cursor, Codex, or custom directories.""" + custom_dir = _skill_dest_dir(dest_dir, target, project=project) + service = SkillService() + try: + report = service.install_skills( + names, + hosts=None if custom_dir is not None else parse_host_option(target), + scope="project" if project else "user", + force=force, + dest_dir=custom_dir, + ) + except (SkillCatalogError, SkillNotFoundError, InvalidSkillTargetError, SkillInstallError) as e: + _error_from_exc(e) + if json_output: + typer.echo(json.dumps(service.install_report_to_dict(report), indent=2)) + return + _print_skill_mutation_report(report) + + +@skill_app.command("upgrade") +def skill_upgrade( + names: Annotated[ + list[str] | None, + typer.Argument( + help="Skill names to upgrade. Omit to upgrade every installed bundled skill.", + autocompletion=complete_skill_names, + ), + ] = None, + target: Annotated[ + str | None, + typer.Option( + "--target", + "-t", + help="Comma-separated hosts: agents,claude,cursor,codex (default: agents,claude)", + autocompletion=complete_skill_targets, + ), + ] = None, + project: Annotated[ + bool, typer.Option("--project", help="Upgrade skills in the current project") + ] = False, + dest_dir: Annotated[ + Path | None, + typer.Option("--dir", help="Upgrade skills in this directory instead of a built-in host"), + ] = None, + json_output: Annotated[bool, _json_flag()] = False, +) -> None: + """Replace outdated installed skills with the bundled copies.""" + custom_dir = _skill_dest_dir(dest_dir, target, project=project) + service = SkillService() + try: + report = service.upgrade_skills( + names, + hosts=None if custom_dir is not None else parse_host_option(target), + scope="project" if project else "user", + dest_dir=custom_dir, + ) + except (SkillCatalogError, SkillNotFoundError, InvalidSkillTargetError, SkillInstallError) as e: + _error_from_exc(e) + if json_output: + typer.echo(json.dumps(service.install_report_to_dict(report), indent=2)) + return + _print_skill_mutation_report(report) + + +@skill_app.command("uninstall") +def skill_uninstall( + names: Annotated[ + list[str] | None, + typer.Argument( + help="Skill names to uninstall. Omit to uninstall every bundled skill.", + autocompletion=complete_skill_names, + ), + ] = None, + target: Annotated[ + str | None, + typer.Option( + "--target", + "-t", + help=( + "Comma-separated hosts: agents,claude,cursor,codex. " + "Defaults to agents,claude when --project is set" + ), + autocompletion=complete_skill_targets, + ), + ] = None, + project: Annotated[ + bool, typer.Option("--project", help="Uninstall skills from the current project") + ] = False, + dest_dir: Annotated[ + Path | None, + typer.Option( + "--dir", help="Uninstall skills from this directory instead of a built-in host" + ), + ] = None, + json_output: Annotated[bool, _json_flag()] = False, +) -> None: + """Remove bundled agent skills. + + Without --target, --project, or --dir, remove every SKILL.md copy that + `pt-snap skill list` would report. A same-named path without SKILL.md + aborts the whole uninstall. Use those flags to limit destinations. + """ + custom_dir = _skill_dest_dir(dest_dir, target, project=project) + service = SkillService() + try: + unfiltered = custom_dir is None and target is None and not project + report = service.uninstall_skills( + names, + hosts=None if custom_dir is not None else parse_host_option(target), + scope="project" if project else "user", + dest_dir=custom_dir, + all_locations=unfiltered, + ) + except (SkillCatalogError, SkillNotFoundError, InvalidSkillTargetError, SkillInstallError) as e: + _error_from_exc(e) + if json_output: + typer.echo(json.dumps(service.install_report_to_dict(report), indent=2)) + return + _print_skill_mutation_report(report) diff --git a/tests/skills/test_memory_fragmentation_contract.py b/tests/skills/test_memory_fragmentation_contract.py index 8f77052..763d27a 100644 --- a/tests/skills/test_memory_fragmentation_contract.py +++ b/tests/skills/test_memory_fragmentation_contract.py @@ -1,6 +1,10 @@ import re from pathlib import Path +from typer.testing import CliRunner + +from pt_snap_cli.cli import app + SKILL_PATH = Path("skills/pt-snap-memory-fragmentation/SKILL.md") TEMPLATE_ROOT = Path("src/pt_snap_cli/query/templates") CLI_PATH = Path("src/pt_snap_cli/cli.py") @@ -26,7 +30,9 @@ def test_memory_fragmentation_skill_uses_current_pt_snap_surfaces() -> None: assert "pt-snap report peak-memory" in skill for command in ('@app.command("focus")', '@app.command("metadata")', '@app.command("query")'): assert command in cli - assert '@report_app.command("peak-memory")' in cli + report_help = CliRunner().invoke(app, ["report", "peak-memory", "--help"]) + assert report_help.exit_code == 0 + assert "--metric" in report_help.output for template, path in templates.items(): assert path.is_file() diff --git a/tests/skills/test_memory_peak_breakdown_contract.py b/tests/skills/test_memory_peak_breakdown_contract.py index b6b7380..96946f8 100644 --- a/tests/skills/test_memory_peak_breakdown_contract.py +++ b/tests/skills/test_memory_peak_breakdown_contract.py @@ -2,8 +2,11 @@ from pathlib import Path +from typer.testing import CliRunner + +from pt_snap_cli.cli import app + SKILL_PATH = Path("skills/pt-snap-memory-peak-breakdown/SKILL.md") -CLI_PATH = Path("src/pt_snap_cli/cli.py") TEMPLATE_DIR = Path("src/pt_snap_cli/query/templates") @@ -27,7 +30,6 @@ def test_skill_frontmatter_and_agent_routing() -> None: def test_skill_references_current_report_and_templates() -> None: skill = _skill() - cli = CLI_PATH.read_text(encoding="utf-8") templates = { "memory_peak": TEMPLATE_DIR / "statistical/memory_peak.yaml", "allocator_gap": TEMPLATE_DIR / "statistical/allocator_gap.yaml", @@ -37,7 +39,9 @@ def test_skill_references_current_report_and_templates() -> None: ), } - assert '@report_app.command("peak-memory")' in cli + report_help = CliRunner().invoke(app, ["report", "peak-memory", "--help"]) + assert report_help.exit_code == 0 + assert "--metric" in report_help.output assert "pt-snap report peak-memory --help" in skill for name, path in templates.items(): assert f" {name}:" in path.read_text(encoding="utf-8") From 38ec8bf42469dd8a12528db5d6f5f974098df10c Mon Sep 17 00:00:00 2001 From: codeasier Date: Sat, 3 Oct 2026 11:49:30 +0800 Subject: [PATCH 2/2] test(cli): normalize colored report help in skill contracts --- tests/skills/test_memory_fragmentation_contract.py | 2 +- tests/skills/test_memory_peak_breakdown_contract.py | 3 ++- 2 files changed, 3 insertions(+), 2 deletions(-) diff --git a/tests/skills/test_memory_fragmentation_contract.py b/tests/skills/test_memory_fragmentation_contract.py index 763d27a..f2cbfac 100644 --- a/tests/skills/test_memory_fragmentation_contract.py +++ b/tests/skills/test_memory_fragmentation_contract.py @@ -32,7 +32,7 @@ def test_memory_fragmentation_skill_uses_current_pt_snap_surfaces() -> None: assert command in cli report_help = CliRunner().invoke(app, ["report", "peak-memory", "--help"]) assert report_help.exit_code == 0 - assert "--metric" in report_help.output + assert "--metric" in re.sub(r"\x1b\[[0-9;]*m", "", report_help.output) for template, path in templates.items(): assert path.is_file() diff --git a/tests/skills/test_memory_peak_breakdown_contract.py b/tests/skills/test_memory_peak_breakdown_contract.py index 96946f8..2065e53 100644 --- a/tests/skills/test_memory_peak_breakdown_contract.py +++ b/tests/skills/test_memory_peak_breakdown_contract.py @@ -1,5 +1,6 @@ from __future__ import annotations +import re from pathlib import Path from typer.testing import CliRunner @@ -41,7 +42,7 @@ def test_skill_references_current_report_and_templates() -> None: report_help = CliRunner().invoke(app, ["report", "peak-memory", "--help"]) assert report_help.exit_code == 0 - assert "--metric" in report_help.output + assert "--metric" in re.sub(r"\x1b\[[0-9;]*m", "", report_help.output) assert "pt-snap report peak-memory --help" in skill for name, path in templates.items(): assert f" {name}:" in path.read_text(encoding="utf-8")