From 61f88e61f0a779de80e5957027723ed0ecd3b9c5 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Emre=20S=C4=B1rmal=C4=B1?= Date: Tue, 24 Nov 2026 19:36:26 +0300 Subject: [PATCH] sidecall: fall back to omp for Pi side calls and drop unknown flags When pi_cli is unset, resolve the Pi executable via pi on PATH first, then oh-my-pi's omp on PATH (or bare omp when OMPCODE is set), falling back to pi so missing-CLI errors stay unchanged. Generalize pi_call's flag retry from --no-mcp alone to all --flags named as unknown options/flags in stderr, so oh-my-pi's multi-flag error (--no-prompt-templates, --no-mcp) strips both and succeeds in one retry. Document oh-my-pi (omp 19.1.1, experimental) and pi_cli fallback behavior, and cover both behaviors in tests/test_live_core.py. --- README.md | 1 + plugin/README.md | 6 +++-- tests/test_live_core.py | 42 ++++++++++++++++++++++++++++++ tokeneconomizer/plugin/sidecall.py | 36 ++++++++++++++++++++++--- 4 files changed, 79 insertions(+), 6 deletions(-) diff --git a/README.md b/README.md index e7b237db..490d790b 100644 --- a/README.md +++ b/README.md @@ -288,6 +288,7 @@ uninstalling, using Copilot with your own model key, and troubleshooting. | Codex | 0.153.1 to 0.171.0 | not supported | | Copilot CLI | 1.0.83 to 1.0.114 | not supported | | Pi | 1.2.0 to 1.5.2 | 0.99.x: load the extension by path (below) | +| oh-my-pi (`omp`, experimental) | 19.1.1 | not tested | | Cursor Agent (experimental) | 3.24.9 and cursor-agent 2026.11.05, macOS and Linux (below) | not tested | | Grok Build (experimental) | 1.0.70 | not tested | | opencode (experimental) | 1.19.5 and 2.2.0 | not tested | diff --git a/plugin/README.md b/plugin/README.md index 6b5c138f..0cd722d5 100644 --- a/plugin/README.md +++ b/plugin/README.md @@ -361,7 +361,9 @@ after the loop guard's redirect gets a notice: press **Esc** to abort it, or Ctr on your default model (`paid_model` names another). If that model's provider comes from an extension (a company gateway, a package's provider), list the extension's file in `pi_extensions` so the calls can reach it; it loads with no tools. If the first `pi` on your PATH -is a wrapper the calls should not run, set `pi_cli` to the real one. +is a wrapper the calls should not run, set `pi_cli` to the real one. oh-my-pi (`omp`) works too: +with no `pi` on PATH, `pi_cli` falls back to `omp` (or `OMPCODE=1` names it), and a side call +leaves out the flags that CLI reports unknown (`--no-prompt-templates`, `--no-mcp`). **Update.** `pi update --extensions` (plain `pi update` does not update extensions). @@ -935,7 +937,7 @@ first session there ends. | `local_roles` | `judge` | which side calls `local_model` takes: `judge`, `nudge` (the redirect writer), `proposer`, separated by commas | | `local_judge_confidence` | `0.9` | how sure a stuck verdict from the on-device judge must be to count; below it the turn is spared | | `pi_extensions` | `""` | Pi only: extension files (comma-separated paths) the plugin's own calls load, for a model whose provider an extension registers | -| `pi_cli` | `""` | Pi only: the `pi` the plugin's own calls run; empty: the first on PATH | +| `pi_cli` | `""` | Pi only: the `pi` the plugin's own calls run; empty: the first `pi` on PATH, else oh-my-pi's `omp` | | `tool_roles` | none | tools the mechanisms do not know by name, each given the role of a built-in one: `tool=role` entries separated by commas, a tool by name or pattern, a role `shell`, `edit` or `read` (`mcp__serena__replace_content=edit,mcp__serena__execute_shell_command=shell`). Micro-skills read a shell tool's commands, and an edit tool's file counts as edited; tool pruning still knows each tool by its own name | | `shadow` | `false` | decide and log everything, act on nothing | | `mode` | `auto` | `interactive` or `headless` to override what the plugin detects | diff --git a/tests/test_live_core.py b/tests/test_live_core.py index a128eadd..a1a362cb 100644 --- a/tests/test_live_core.py +++ b/tests/test_live_core.py @@ -1430,6 +1430,47 @@ def fake_run(cmd, **kw): assert calls[-1][0][-4:-2] == ["--model", "openrouter/anthropic/claude-haiku-4.5"] +@pytest.mark.real_sidecalls +def test_a_pi_side_call_drops_every_flag_the_cli_reports_unknown(monkeypatch): + """oh-my-pi (`omp`) rejects two of Pi's flags in one error line; both are left out and the + call retried, rather than failing the side call.""" + import subprocess + from tokeneconomizer.plugin import sidecall + monkeypatch.setattr(sidecall, "program", lambda cli: [cli]) + calls = [] + + def fake_run(cmd, **kw): + calls.append(cmd) + if "--no-prompt-templates" in cmd or "--no-mcp" in cmd: + return subprocess.CompletedProcess( + cmd, 1, stdout="", + stderr="Error: unknown flags: --no-prompt-templates, --no-mcp\n") + return subprocess.CompletedProcess( + cmd, 0, stdout="\n".join(json.dumps(e) for e in PI_JSON_RUN), stderr="") + + monkeypatch.setattr(sidecall.subprocess, "run", fake_run) + text, eff = sidecall.pi_call("propose facts", cli="omp") + assert text == "- use python -m pytest" and eff and eff > 0 + assert "--no-prompt-templates" not in calls[-1] and "--no-mcp" not in calls[-1] + assert "--offline" in calls[-1] and calls[-1][-2:] == ["--", "propose facts"] + + +def test_a_pi_side_call_prefers_pi_and_falls_back_to_oh_my_pi_s_omp(monkeypatch): + from tokeneconomizer.plugin import sidecall + monkeypatch.delenv("OMPCODE", raising=False) + monkeypatch.setattr(sidecall.shutil, "which", + lambda name: {"pi": "/usr/local/bin/pi"}.get(name)) + assert sidecall.pi_cli() == "/usr/local/bin/pi" + monkeypatch.setattr(sidecall.shutil, "which", + lambda name: {"omp": "/usr/local/bin/omp"}.get(name)) + assert sidecall.pi_cli() == "/usr/local/bin/omp" + monkeypatch.setattr(sidecall.shutil, "which", lambda name: None) + monkeypatch.setenv("OMPCODE", "1") + assert sidecall.pi_cli() == "omp" + monkeypatch.delenv("OMPCODE") + assert sidecall.pi_cli() == "pi" + + @pytest.mark.real_sidecalls def test_a_pi_side_call_loads_the_provider_extensions_and_the_pi_the_person_names( monkeypatch, tmp_path): @@ -1438,6 +1479,7 @@ def test_a_pi_side_call_loads_the_provider_extensions_and_the_pi_the_person_name import subprocess from tokeneconomizer.plugin import sidecall monkeypatch.setattr(sidecall, "program", lambda cli: [cli]) + monkeypatch.setattr(sidecall, "pi_cli", lambda: "pi") calls = [] def fake_run(cmd, **kw): diff --git a/tokeneconomizer/plugin/sidecall.py b/tokeneconomizer/plugin/sidecall.py index f9e6d9d2..b10b3ab0 100644 --- a/tokeneconomizer/plugin/sidecall.py +++ b/tokeneconomizer/plugin/sidecall.py @@ -171,6 +171,18 @@ def resume(host, root=None): return False +def pi_cli(): + """The Pi executable side calls run: `pi` on PATH, else oh-my-pi's `omp`, the fork this + plugin also serves. A session inside oh-my-pi carries `OMPCODE=1`, so the bare `omp` is used + even where PATH lookup cannot find it. Falls back to `pi`, so the error stays the + CLI-not-found one.""" + found = shutil.which("pi") + if found: + return found + # oh-my-pi's binary is `omp`, not `pi`; a session inside it carries OMPCODE=1. + return shutil.which("omp") or ("omp" if os.environ.get("OMPCODE") else "pi") + + def claude_cli(): """The Claude Code executable to start: `claude` on PATH (with PATHEXT on Windows, so a `claude.cmd` shim too), else the native installer's `~/.local/bin/claude` -- which is often @@ -367,6 +379,18 @@ def codex_call(prompt, model="", cli="codex", timeout=TIMEOUT_S, effort=""): PI_FLAGS = ("--print", "--mode", "json", "--no-session", "--no-tools", "--no-extensions", "--no-skills", "--no-prompt-templates", "--no-context-files", "--offline") +# A flag this CLI does not have: Pi says `Unknown option: --no-mcp`; oh-my-pi says +# `unknown flags: --no-prompt-templates, --no-mcp` (one line, any number of names). +_UNKNOWN_FLAG_RE = re.compile(r"unknown (?:option|flag)s?:?\s*(.*)", re.IGNORECASE) + + +def _unknown_flags(stderr): + """The `--flags` a CLI's error names as unknown, which a side call leaves out and retries: + the two Pi implementations do not have the same set (oh-my-pi drops `--no-prompt-templates` + and `--no-mcp`), and probing `--help` would cost a second start.""" + m = _UNKNOWN_FLAG_RE.search(stderr or "") + return set(re.findall(r"--[\w-]+", m.group(1))) if m else set() + def pi_extension_args(extensions): """`--extension ` for each extension a side call loads despite `--no-extensions` (Pi @@ -383,14 +407,18 @@ def pi_call(prompt, model="", cli="pi", timeout=TIMEOUT_S, extensions=()): model_args = ["--model", model] if model else [] ext_args = pi_extension_args(extensions) exe = program(cli) + flags = [*PI_FLAGS, "--no-mcp"] # Pi before 1.0 and oh-my-pi reject the last one with tempfile.TemporaryDirectory(prefix="te-side-", ignore_cleanup_errors=True) as cwd: - for extra in (["--no-mcp"], []): - cmd = [*exe, *PI_FLAGS, *extra, *ext_args, *model_args, "--", prompt] + for _ in range(len(flags) + 1): + cmd = [*exe, *flags, *ext_args, *model_args, "--", prompt] out = subprocess.run(cmd, capture_output=True, encoding="utf-8", errors="replace", timeout=timeout, env=_env(), cwd=cwd, stdin=subprocess.DEVNULL) - if "Unknown option: --no-mcp" not in (out.stderr or ""): + unknown = _unknown_flags(out.stderr) + kept = [f for f in flags if f not in unknown] + if not unknown or kept == flags: break + flags = kept text, usage = trace.pi_json_reply(out.stdout) if not text: _no_reply("pi", out) @@ -1022,7 +1050,7 @@ def _host_call(host_name, config): effort = getattr(config, f"{host_name}_paid_effort", "") kw = {"effort": effort} if effort else {} if host_name == "pi": - cli = os.path.expanduser(config.pi_cli) if config.pi_cli else "pi" + cli = os.path.expanduser(config.pi_cli) if config.pi_cli else pi_cli() extensions = list(config.pi_extensions) return lambda prompt, _m="agent": pi_call(prompt, model=model, cli=cli, extensions=extensions)