From 353b5919dd25a392806c29c35af6912efec6807a Mon Sep 17 00:00:00 2001 From: Qian Zhang Date: Wed, 11 Nov 2026 11:15:34 -0500 Subject: [PATCH 1/2] Escape pip extras in installer hints so Rich doesn't swallow them `powermcp install` printed its pip hints through Rich, which reads `[andes,ltspice]` as a markup tag and drops it. The dry-run and non-interactive hints read `pip install powermcp` with no extras, so anyone copying them installed none of the selected tools. The actual pip subprocess call was unaffected. Escape the spec in every wizard message that shows it, and escape the user-derived values (config and client paths, detected paths, unknown tool ids, exception text) that also go through Rich markup. doctor already escaped its table; its config-file line now does too. Co-Authored-By: Claude Opus 5.5 --- powermcp/doctor.py | 2 +- powermcp/wizard.py | 25 +++++++++-------- tests/test_wizard.py | 64 ++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 79 insertions(+), 12 deletions(-) diff --git a/powermcp/doctor.py b/powermcp/doctor.py index 35c3b7cd..8ab0c278 100644 --- a/powermcp/doctor.py +++ b/powermcp/doctor.py @@ -236,4 +236,4 @@ def run_doctor(tool: str | None = None) -> None: ) for note in solver_notes: console.print(f"[dim]{note}[/]") - console.print(f"\n[dim]Config file: {cfg.config_path()}[/]") + console.print(f"\n[dim]Config file: {escape(str(cfg.config_path()))}[/]") diff --git a/powermcp/wizard.py b/powermcp/wizard.py index d994b341..b1393509 100644 --- a/powermcp/wizard.py +++ b/powermcp/wizard.py @@ -13,6 +13,7 @@ from pathlib import Path from rich.console import Console +from rich.markup import escape from . import config as cfg from .clients import CLIENT_NAMES, configure @@ -135,7 +136,7 @@ def run_wizard( _capture_paths(selected) _pip_install(selected, assume_yes=yes, dry_run=dry_run) _write_clients(selected, client_names, dry_run) - console.print(f"\n[green]Done.[/] PowerMCP config: {cfg.config_path()}") + console.print(f"\n[green]Done.[/] PowerMCP config: {escape(str(cfg.config_path()))}") def _resolve_selection( @@ -163,7 +164,7 @@ def _resolve_selection( unknown.append(name) if unknown: console.print( - f"[red]Unknown tool id(s) ignored:[/] {', '.join(unknown)} " + f"[red]Unknown tool id(s) ignored:[/] {escape(', '.join(unknown))} " f"(run `powermcp list` for valid ids)" ) if unavailable: @@ -211,7 +212,7 @@ def _interactive_select(client_names: list[str]) -> list[Tool]: ).ask() except Exception as exc: console.print( - f"[yellow]Interactive picker unavailable ({exc}); defaulting to core tools. " + f"[yellow]Interactive picker unavailable ({escape(str(exc))}); defaulting to core tools. " f"Use `--tools ` or `--all`.[/]" ) return [TOOLS[n] for n in CORE] @@ -252,12 +253,12 @@ def _capture_paths(selected: list[Tool]) -> None: default = existing or detect(t.name, ck.key) or "" label = f"[{t.display}] {ck.prompt}" if default and not existing: - console.print(f"[dim] detected: {default}[/]") + console.print(f"[dim] detected: {escape(str(default))}[/]") try: answer = questionary.path(label, default=default).ask() except Exception as exc: # prompt backend unavailable console.print( - f"[yellow]Could not prompt for {t.name}.{ck.key} ({exc}); " + f"[yellow]Could not prompt for {t.name}.{ck.key} ({escape(str(exc))}); " f"set it later with `powermcp config set {t.name}.{ck.key} `.[/]" ) answer = None @@ -295,13 +296,15 @@ def _capture_paths(selected: list[Tool]) -> None: def _pip_install(selected: list[Tool], *, assume_yes: bool = False, dry_run: bool = False) -> None: extras = sorted({t.extra for t in selected if t.extra}) spec = "powermcp" + (f"[{','.join(extras)}]" if extras else "") + # Rich would read `[andes,ltspice]` as a markup tag and drop it from the hint. + shown = escape(spec) if dry_run: - console.print(f"[cyan](dry-run)[/] would run: pip install {spec}") + console.print(f"[cyan](dry-run)[/] would run: pip install {shown}") return if assume_yes: proceed = True elif not _tty(): - console.print(f"[yellow]Non-interactive; skipping dependency install. Run:[/] pip install {spec}") + console.print(f"[yellow]Non-interactive; skipping dependency install. Run:[/] pip install {shown}") return else: import questionary @@ -313,12 +316,12 @@ def _pip_install(selected: list[Tool], *, assume_yes: bool = False, dry_run: boo except Exception: proceed = False if not proceed: - console.print(f"[yellow]Skipped. Install later with:[/] pip install {spec}") + console.print(f"[yellow]Skipped. Install later with:[/] pip install {shown}") return - console.print(f"[cyan]Installing[/] {spec} ...") + console.print(f"[cyan]Installing[/] {shown} ...") result = subprocess.run([sys.executable, "-m", "pip", "install", spec], check=False) if result.returncode != 0: - console.print(f"[red]pip install failed (exit {result.returncode}).[/] Install manually: pip install {spec}") + console.print(f"[red]pip install failed (exit {result.returncode}).[/] Install manually: pip install {shown}") def _write_clients(selected: list[Tool], client_names: list[str], dry_run: bool) -> None: @@ -339,6 +342,6 @@ def _write_clients(selected: list[Tool], client_names: list[str], dry_run: bool) results = configure(client_names, tool_names, dry_run=dry_run) for client, path in results.items(): if path: - console.print(f"[green]Configured {client}:[/] {path}") + console.print(f"[green]Configured {client}:[/] {escape(str(path))}") elif not dry_run: console.print(f"[yellow]Skipped {client} (unknown client name).[/]") diff --git a/tests/test_wizard.py b/tests/test_wizard.py index 4a6d09c7..12a98a33 100644 --- a/tests/test_wizard.py +++ b/tests/test_wizard.py @@ -142,3 +142,67 @@ def ask(self): fake_q.checkbox = lambda *a, **k: _Q() monkeypatch.setitem(sys.modules, "questionary", fake_q) assert wizard._interactive_select([]) == [] + + +# The pip hint is printed through Rich, which reads `[andes]` as a markup tag +# and drops it, so the hint used to read `pip install powermcp` with no extras. +# Copying it installed nothing the selection needed. +def _andes_selection(): + return wizard._resolve_selection(yes=False, tools="andes", select_all=False, client_names=[]) + + +@pytest.mark.parametrize("mode", ["dry_run", "non_tty"]) +def test_pip_hint_keeps_extras(monkeypatch, capsys, mode): + monkeypatch.setattr(wizard, "_tty", lambda: False) + monkeypatch.setitem(sys.modules, "questionary", None) # must not prompt + monkeypatch.setattr(wizard.subprocess, "run", lambda *a, **k: pytest.fail("pip must not run")) + wizard._pip_install(_andes_selection(), dry_run=(mode == "dry_run")) + out = capsys.readouterr().out + assert "pip install powermcp[andes]" in out + + +def test_pip_hint_keeps_extras_when_declined(monkeypatch, capsys): + monkeypatch.setattr(wizard, "_tty", lambda: True) + prompts = [] + fake_q = types.ModuleType("questionary") + + class _Q: + def ask(self): + return False + + def _confirm(message, **kw): + prompts.append(message) + return _Q() + + fake_q.confirm = _confirm + monkeypatch.setitem(sys.modules, "questionary", fake_q) + wizard._pip_install(_andes_selection()) + assert prompts and "pip install powermcp[andes]" in prompts[0] # questionary prints it verbatim + assert "Install later with: pip install powermcp[andes]" in capsys.readouterr().out + + +def test_pip_hint_keeps_extras_when_install_fails(monkeypatch, capsys): + calls = [] + + def _run(cmd, **kw): + calls.append(cmd) + return types.SimpleNamespace(returncode=1) + + monkeypatch.setattr(wizard.subprocess, "run", _run) + wizard._pip_install(_andes_selection(), assume_yes=True) + assert calls[0][-1] == "powermcp[andes]" # the real command was never affected + out = capsys.readouterr().out + assert "Installing powermcp[andes]" in out + assert "Install manually: pip install powermcp[andes]" in out + + +def test_done_line_keeps_bracketed_config_path(monkeypatch, capsys, tmp_path): + from rich.console import Console + + home = tmp_path / "cfg[old]" # `[old]` reads as a Rich style tag + monkeypatch.setenv("POWERMCP_HOME", str(home)) + monkeypatch.setattr(wizard, "console", Console(width=1000)) # no wrapping of the long path + wizard.run_wizard(yes=False, tools="andes", clients="none", dry_run=True) + out = capsys.readouterr().out + assert f"PowerMCP config: {home / 'config.toml'}" in out + assert "pip install powermcp[andes]" in out From fc2ba45af016dd6ba1f11b0d3b448d11e254167a Mon Sep 17 00:00:00 2001 From: Qian Zhang Date: Wed, 11 Nov 2026 11:17:35 -0500 Subject: [PATCH 2/2] Keep long pip hints and config paths on one line Without a TTY Rich renders at 80 columns and folds any word wider than that. With every Windows tool selected the spec is ~110 characters, so the hint split `powermcp[...]` across lines and a copied command broke. Long config paths in the "Done" and "Configured" lines wrapped the same way. Print those lines with soft_wrap=True so they stay intact. Co-Authored-By: Claude Opus 5.5 --- powermcp/doctor.py | 2 +- powermcp/wizard.py | 24 ++++++++++++++++-------- tests/test_wizard.py | 17 ++++++++++++++++- 3 files changed, 33 insertions(+), 10 deletions(-) diff --git a/powermcp/doctor.py b/powermcp/doctor.py index 8ab0c278..11e52918 100644 --- a/powermcp/doctor.py +++ b/powermcp/doctor.py @@ -236,4 +236,4 @@ def run_doctor(tool: str | None = None) -> None: ) for note in solver_notes: console.print(f"[dim]{note}[/]") - console.print(f"\n[dim]Config file: {escape(str(cfg.config_path()))}[/]") + console.print(f"\n[dim]Config file: {escape(str(cfg.config_path()))}[/]", soft_wrap=True) diff --git a/powermcp/wizard.py b/powermcp/wizard.py index b1393509..37dbe8ce 100644 --- a/powermcp/wizard.py +++ b/powermcp/wizard.py @@ -136,7 +136,7 @@ def run_wizard( _capture_paths(selected) _pip_install(selected, assume_yes=yes, dry_run=dry_run) _write_clients(selected, client_names, dry_run) - console.print(f"\n[green]Done.[/] PowerMCP config: {escape(str(cfg.config_path()))}") + console.print(f"\n[green]Done.[/] PowerMCP config: {escape(str(cfg.config_path()))}", soft_wrap=True) def _resolve_selection( @@ -296,15 +296,20 @@ def _capture_paths(selected: list[Tool]) -> None: def _pip_install(selected: list[Tool], *, assume_yes: bool = False, dry_run: bool = False) -> None: extras = sorted({t.extra for t in selected if t.extra}) spec = "powermcp" + (f"[{','.join(extras)}]" if extras else "") - # Rich would read `[andes,ltspice]` as a markup tag and drop it from the hint. + # Rich would read `[andes,ltspice]` as a markup tag and drop it from the hint, + # and would fold a spec wider than the console (80 columns without a TTY) + # across lines; soft_wrap leaves the copyable command on one line. shown = escape(spec) if dry_run: - console.print(f"[cyan](dry-run)[/] would run: pip install {shown}") + console.print(f"[cyan](dry-run)[/] would run: pip install {shown}", soft_wrap=True) return if assume_yes: proceed = True elif not _tty(): - console.print(f"[yellow]Non-interactive; skipping dependency install. Run:[/] pip install {shown}") + console.print( + f"[yellow]Non-interactive; skipping dependency install. Run:[/] pip install {shown}", + soft_wrap=True, + ) return else: import questionary @@ -316,12 +321,15 @@ def _pip_install(selected: list[Tool], *, assume_yes: bool = False, dry_run: boo except Exception: proceed = False if not proceed: - console.print(f"[yellow]Skipped. Install later with:[/] pip install {shown}") + console.print(f"[yellow]Skipped. Install later with:[/] pip install {shown}", soft_wrap=True) return - console.print(f"[cyan]Installing[/] {shown} ...") + console.print(f"[cyan]Installing[/] {shown} ...", soft_wrap=True) result = subprocess.run([sys.executable, "-m", "pip", "install", spec], check=False) if result.returncode != 0: - console.print(f"[red]pip install failed (exit {result.returncode}).[/] Install manually: pip install {shown}") + console.print( + f"[red]pip install failed (exit {result.returncode}).[/] Install manually: pip install {shown}", + soft_wrap=True, + ) def _write_clients(selected: list[Tool], client_names: list[str], dry_run: bool) -> None: @@ -342,6 +350,6 @@ def _write_clients(selected: list[Tool], client_names: list[str], dry_run: bool) results = configure(client_names, tool_names, dry_run=dry_run) for client, path in results.items(): if path: - console.print(f"[green]Configured {client}:[/] {escape(str(path))}") + console.print(f"[green]Configured {client}:[/] {escape(str(path))}", soft_wrap=True) elif not dry_run: console.print(f"[yellow]Skipped {client} (unknown client name).[/]") diff --git a/tests/test_wizard.py b/tests/test_wizard.py index 12a98a33..25d51583 100644 --- a/tests/test_wizard.py +++ b/tests/test_wizard.py @@ -201,8 +201,23 @@ def test_done_line_keeps_bracketed_config_path(monkeypatch, capsys, tmp_path): home = tmp_path / "cfg[old]" # `[old]` reads as a Rich style tag monkeypatch.setenv("POWERMCP_HOME", str(home)) - monkeypatch.setattr(wizard, "console", Console(width=1000)) # no wrapping of the long path + monkeypatch.setattr(wizard, "console", Console(width=80)) # the path is wider than this wizard.run_wizard(yes=False, tools="andes", clients="none", dry_run=True) out = capsys.readouterr().out assert f"PowerMCP config: {home / 'config.toml'}" in out assert "pip install powermcp[andes]" in out + + +def test_long_pip_hint_stays_on_one_line(monkeypatch, capsys): + # Without a TTY Rich renders at 80 columns and used to fold a wider spec + # (every Windows tool) mid-word, so a copied hint split the spec in two. + from rich.console import Console + + monkeypatch.setattr(sys, "platform", "win32") + monkeypatch.setattr(wizard, "_surge_supported", lambda: True) + monkeypatch.setattr(wizard, "console", Console(width=80)) + selected = wizard._resolve_selection(yes=False, tools="all", select_all=False, client_names=[]) + spec = "powermcp[" + ",".join(sorted({t.extra for t in selected if t.extra})) + "]" + assert len(spec) > 80 + wizard._pip_install(selected, dry_run=True) + assert f"pip install {spec}" in capsys.readouterr().out