pi-syntax-guard0.1.0
Pi extension that blocks syntax errors before the write and edit tools touch the file
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.
Reports 0
No reports yet.
Readme
pi-syntax-guard
A Pi extension that blocks write and edit tool calls whose result would not parse. Parsing uses
tree-sitter grammars from @xberg-io/tree-sitter-language-pack,
so one dependency covers the common languages without a compiler toolchain.
Install
pi install npm:pi-syntax-guard
Try it for one session without installing:
pi -e npm:pi-syntax-guard
Requires pi 1.7 or newer (the extension reads settings through SettingsManager) and either pi's compiled binary
(Bun) or Node.js 22.19+. On a platform without a language-pack binding (for example Termux or FreeBSD) the extension
still loads, reports itself as unavailable, and leaves every call alone.
How it works
The extension listens to the tool_call event:
A
writecall carries the whole file, so the guard parsescontentdirectly.An
editcall is replayed in memory against the file on disk, then the predicted result is parsed. The replay mirrors pi's edit rules: everyoldTextmust match exactly, uniquely, and without overlapping another edit.A parse that reports an error or a missing node blocks the call, and the file on disk stays untouched. The block reports one line per fault,
path:line:columnfollowed by the offending text, with the path relative to the session cwd:src/a.ts:12:5 missing "}" src/a.ts:14:9 unexpected "="The model reads every reported fault as the tool result, with no prose around it. The TUI draws the same lines in the tool row on the theme's
toolErrorBg, with the position linked to the file so a click opens it. The block collapses to five faults plus theapp.tools.expandhint, like pi's own renderers; expanding shows every fault. The diff above it is drawn by pi'seditrenderer and keeps its own length. Both views render one shared value, so they cannot disagree.The block carries 20 faults inline. A longer list is saved to a
pi-syntax-guard-<hex>.txtfile under the OS temp directory, and the block ends with[Showing 20 of N. Full list: <path>], so the model can read every fault without spending its context on hundreds of lines. A save that fails says so instead of dropping the list. The parser stops collecting only at 500 faults, a safety cap; such a block ends with... more faults exist.A
.jsonfile that fails the strict grammar is retried withjson5, so comments and trailing commas intsconfig.jsonand similar files stay valid. A real JSON error fails both grammars and is still blocked.
The guard fails open. A language outside the allowlist, a call it cannot predict, a file it cannot read, a file above the size limit, or a missing grammar lets the call through. Only a completed parse with a proven fault blocks.
Configuration
Settings live under the syntax-guard key in settings.json (global, or project .pi/settings.json with project
trust). Every field is optional:
{
"syntax-guard": {
"enabled": true,
"autoWarm": true,
"languages": ["typescript", "tsx", "javascript", "python", "json"],
"warmLanguages": ["typescript", "python"],
"exclude": ["**/*.generated.ts", "docs/**"],
"maxFileBytes": 2000000,
"parseTimeoutMs": 2000
}
}
| Field | Default | Meaning |
|---|---|---|
enabled |
true |
false disables the guard for the session. |
languages |
25 built-in languages | Complete replacement for the checked set. Unknown names are dropped with a warning. |
autoWarm |
true |
Warm warmLanguages in a worker when a session starts. |
warmLanguages |
languages, or 12 defaults |
Grammars to download and load. [] opts out. |
exclude |
[] |
minimatch patterns, relative to the session cwd, that stay unchecked. |
maxFileBytes |
2000000 |
Larger candidate content is not parsed. |
parseTimeoutMs |
2000 |
A parse that exceeds this budget is abandoned. |
Built-in languages: typescript, tsx, javascript, python, rust, go, java, c, cpp, csharp, ruby,
php, bash, lua, swift, kotlin, json, yaml, toml, css, html, vue, svelte, nix.
sql is not checked by default: no single grammar accepts PostgreSQL dollar quoting and T-SQL syntax together, so
checking .sql files would block valid writes. Add "sql" to languages to opt in.
Grammars
A grammar must be cached on disk before the guard can use it. tool_call never downloads: a network fetch there would
stall the agent turn. Warm-up runs in a worker thread instead, and the first one on a machine fetches a 23.7 MB platform
bundle, then extracts only the requested grammars (JSON is 20 KB, TypeScript 1.8 MB, Rust 2.4 MB). The cache is shared
across projects, so the cost is paid once per machine.
Commands:
/syntax-guard statusprints the effective language list, cached grammars, and warm list./syntax-guard warm [language...]warms the named languages, or the configured warm list.
Limitations
- Grammar errors only. The guard does not type check, resolve imports, or run linters.
- Fuzzy edit matches are not replayed. When an
oldTextonly matches after pi's whitespace and quote normalization, the call is allowed unchecked rather than predicted. - Markdown, lockfiles, and every language outside the allowlist are never parsed.
- Two
editcalls that touch the same file in one assistant message are each predicted against the file as it is on disk. If the first leaves the file unparsable and only the second completes it, the first can be blocked. Batch changes to one file into a singleeditcall. - A guard bug never blocks a call: it is reported once as
internal-errorand the call runs unchecked. - A guard block is drawn only while the session is running. A guard block reopened from a session file, and a block shown in an HTML export, fall back to the plain tool error text.
- The extension runs under both pi's compiled binary (Bun) and the npm package (Node >= 22.19).
Development
The repository uses devenv for the toolchain, pnpm for dependencies, and git hooks for formatting, linting, type checking, and tests.
devenv shell # node, pnpm, git hooks
pnpm install # after changing package.json
pnpm verify # prettier check, eslint, tsc, vitest
Native integration tests skip unless a grammar is cached. Warm one first:
node -e 'require("@xberg-io/tree-sitter-language-pack").prefetch(["typescript"])'
License
MIT