omp packages

pi-better-calls0.3.0

Preflight and repair the tool calls weaker models get wrong: edit oldText mismatches, typo'd read paths, oversized writes, broken bash blocks and off-PATH binaries, and unknown background task ids.

voorjaar Published 8 days ago MIT ~728 downloads/mo

Untested on omp

This is a pi package. No one has reported whether it works on omp yet. If you try it, add your result below.

Did it work for you?

Result

One report per person. A new report replaces your old one.

Reports 0

No reports yet.

Readme

pi-better-calls

Preflight and repair the tool calls that weaker local models get wrong, before the tool runs — and answer the failure with the fact the model would otherwise have to guess again: the text that is actually in the file, the path that actually exists, the task id that is actually running.

This is a pi coding-agent extension.

Why

An audit of 65,094 tool calls across 9 sessions of a local model driving pi (subagent/bench/pi_tool_error_audit.py) found the failures are not random:

tool failed / total failure rate
edit 588 / 7,198 8.2%
bash 1,429 / 44,916 3.2%
read 83 / 3,758 2.2%
write 6 / 2,346 0.3%

The strongest predictor for edit was how many replacements one call carried:

edits in one call 1 2–3 4 5 6 8 10
failure rate 4.6% 10.0% 14.8% 18.4% 22.1% 27.3% 45.5%

And the failure families are mechanical, so they are catchable in advance:

family count
edit: oldText not found in the file 323
edit: PARTIAL APPLY (first edits landed, a later one did not) 129
edit: refused because the file was never read 71
edit: bad argument shape (e.g. edits as a JSON string) 49
read: path does not exist 84
bg_kill: "Task X is completed, not running" (20% of all bg_kill calls) 34
bg_run: "bg_run requires isAgent boolean" 30
bash: cd/source target missing 68
ask_user_question: a question object with no question field seen live
bash: shell syntax (unclosed block, unbalanced quote) 27

Install

pi install npm:pi-better-calls

No build step: pi loads index.ts directly.

What it does

Repairs, applied in place (no prompt to the model)

situation action
edit with edits written as a JSON string parsed into the array the schema wants
edit with a taskId-style prefix, or bg_logs/bg_status given a prefix of a known id expanded to the real id
bg_run without isAgent (or with a non-boolean) set to false, the safe default
bg_run with a multi-word name shortened to the 2–6 word label the footer dock expects
ask_user_question with an over-long header or label trimmed to the 16 / 60 character limits
bash calling a binary that lives off the default PATH (brew, hf, a venv tool) prepends PATH="$PATH:<dir>" from your pathHints

Preflights: guide mode (default) appends the finding to the result, strict mode blocks the call

situation what the model gets back
edit oldText is not in the file, but similar text is the verbatim text that is there (whitespace-insensitive match, with line number) to copy
edit oldText matches several places "not unique", plus the candidate spans to disambiguate with
edit whose edits match overlapping regions refused before anything is written, so the file is never half-edited
edit with more than maxEditsPerCall replacements refused, with the measured failure rates quoted
edit/write on a path that does not exist the closest real file name in that directory
read past the end of a file the real line count
read on a directory the ls command that works
write with an oversized single payload refused with a chunking plan
bash whose cd/source/[ -f ] target does not exist refused before the call, with the near match
bash with an unclosed for/while/if/case or unbalanced quoting refused with the missing done/fi/esac
ask_user_question whose question object has no question field refused, naming the index and saying the question text is what is missing
ask_user_question with 1 or 5 options, or an option with no description refused with the same list of every problem in one message
ask_user_question that authors Other / Type something. refused - the harness appends that row itself
bg_kill/bg_logs/bg_status/bg_wait_for_task with an unknown or already-finished id the tasks that actually exist, with their status

Every finding names the tool and the condition in plain language, so the model corrects the next call instead of re-guessing.

Posture is a config choice, and the default is not a gate. In guide mode (default) the call runs and the finding is appended to its result as [pi-better-calls] …; in strict mode the call is blocked with the same text, and every "refused" in the table above means refused. Guide is the default for two reasons: most of these conditions are ones the tool reports for itself (bg_kill with an id that is not running already answers "Task X is completed, not running"), and a preflight that refuses first creates a place for the preflight to be wrong — 0.2.1 refused four commands that would have run fine, and 0.2.2 found that its own task-id registry had never matched what bg_run actually prints. The one rule that blocks in both modes is edit content validation: a replacement that is not in the file lets the others land and leaves the file half-edited, which is a mutation rather than a wasted call. A finding already shown three times in a session is not repeated.

What the bash preflight deliberately does not refuse. A keyword that is not a verb is prose. An echo label containing the word source is not a source command; a file named gitea-pushes-for-voorjaar-repos.md does not open a for block; mkdir -p DIR && cd DIR makes the directory it then enters; ssh host 'cd /remote/dir' names a path on another machine. All four were refusals until 0.2.1, each one blocking a command that would have run fine.

Guidance, injected only where it helps

Rules are appended to pi's promptGuidelines, gated by the tools actually in the prompt and by an optional model list, so a strong model that never makes these mistakes does not pay for them:

Use edit with exactly one replacement per call: one oldText/newText pair, then a separate call for the next change. Several calls cost nothing; batching is what costs. Measured failure rate: 1 edit 4.6%, 2-3 edits 10%, 4 edits 15%, 10 edits 46%.
Use edit by copying oldText verbatim from the most recent read of that file, including indentation and trailing spaces.
Use edit with oldText short enough to be unique but long enough to be unambiguous. If another instruction tells you to batch several changes into one edit call, read that as one replacement per call too - a later mismatch leaves the file half-edited.
Use edit by copying oldText verbatim from the most recent read of that file, including indentation and trailing spaces.
Use bash only after checking that every cd/source target in the command exists.

Configuration

~/.pi/agent/pi-better-calls.json (point elsewhere with PI_BETTER_CALLS_CONFIG):

{
  "enabled": true,
  "models": ["qwen3.8", "strata"],
  "tools": ["edit", "read", "write", "bash", "bg_run", "bg_kill", "bg_logs", "bg_status", "bg_wait_for_task"],
  "maxEditsPerCall": 2,
  "mode": "guide",
  "suggest": true,
  "maxFileBytes": 2000000,
  "maxWriteBytes": 200000,
  "pathHints": [
    { "name": "hf", "dir": "~/Library/Python/3.9/bin" },
    { "name": "brew", "dir": "/opt/homebrew/bin" }
  ]
}
key meaning
enabled master switch; PI_BETTER_CALLS=off overrides it
models substrings matched against provider/modelId for the guidance only; empty means every model. Preflights always run when enabled
tools which preflights are active
maxEditsPerCall how many replacements one edit call may carry (default 2)
mode guide (default): the call runs and the finding is appended to its result. strict: the call is blocked with the finding
suggest include exact candidate spans and near paths in block reasons
maxFileBytes files larger than this are not opened for matching (the tool answers instead)
maxWriteBytes refuse a single write payload above this size
pathHints binaries to rescue with a PATH prefix

/better-calls status|on|off shows the effective settings, whether the model gate matches the current model, the tasks it has learned about, and the repairs it has applied.

What it deliberately does not do

  • It never edits files, never rewrites a command's meaning, and never rewrites a tool's result.
  • It does not touch network tools (web_search, fetch_content): their failures are the network's, not the model's.
  • It does not invent a replacement for a oldText that is simply absent — it shows what is there instead.
  • It is a per-session guard: task ids it knows come from what it saw in this session.

Development

git clone https://github.com/voorjaar/pi-better-calls.git
cd pi-better-calls
npm test          # node --experimental-strip-types selftest.mjs

The selftest drives the real index.ts against a fake pi host and real temp files: 35 checks covering each repair, each preflight (including the ask_user_question shape: missing question text, 1 or 5 options, a reserved label, an over-long header), the model gate, and config reporting.

License

MIT — see LICENSE.