omp packages

pi-code-hygiene0.4.0

Run codehygiene on every file Pi writes or edits and report readability problems that linters miss.

yivas Published 2 days ago MIT ~1.1K 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-code-hygiene

A small Pi extension that runs codehygiene on every file the agent writes or edits, so readability problems that linters miss show up without anyone having to remember to ask.

Built on codehygiene, by Brian Bell. The analysis, the model and the idea of running it from a hook are his work: brianbell-x/codehygiene. This package is an independent Pi integration and is not affiliated with or endorsed by him. It contains none of his code: it loads the two files of his checkout from a folder you create, after checking their hashes.

Install · Modes · Limits · Contributing · Security

codehygiene scores geometry and structure instead of syntax. In the version this package pins it reports six kinds: deep nesting, dense or over-packed lines, cramped or ragged layout, duplicated blocks, repetitive parallel equality or branch ladders, and comments that echo the code beside them or leave commented-out code behind. Its author recommends running it from a hook in the agent harness rather than as a skill. This package is that hook. It does not replace pi-lsp, type checkers or your project's linter, and it does not check behavior or security.

Status: version 0.4.0. It is checked on Windows with Node 26: its own test suite, and runs against a real codehygiene install. It targets the extension API of Pi 1.5.2. Other platforms are untested. codehygiene is an early preview and its author documents false positives, so the note it produces says plainly that it may be wrong and is information only.

Install

The extension needs codehygiene installed outside the package: a checkout pinned to one commit, and no more than that. Its analyser is plain JavaScript with no dependencies, so there is no interpreter and no venv. These commands create the folder under %LOCALAPPDATA%\pi-hygiene (PowerShell):

$dir = Join-Path $env:LOCALAPPDATA "pi-hygiene"
New-Item -ItemType Directory -Force $dir | Out-Null
git clone https://github.com/brianbell-x/codehygiene.git "$dir\src"
git -C "$dir\src" checkout d56e737afef4f2362c45794e073599de86492a9b

# The extension only runs when the two files in this manifest still have these hashes.
$files = "engine.js", "weights.npz"
$sha = [System.Security.Cryptography.SHA256]::Create()
$files | ForEach-Object {
  $bytes = [System.IO.File]::ReadAllBytes("$dir\src\codehygiene\$_")
  $hash = [System.BitConverter]::ToString($sha.ComputeHash($bytes)).Replace("-", "").ToLower()
  "$hash *codehygiene/$_"
} | Set-Content "$dir\manifest.sha256" -Encoding ascii

Check that the two lines start with these hashes: 0f9c49595f6e226b07cc086cb4156f7d0dc33ac859a9505bc1ff21fe30b174d0 for engine.js and 32c687c8affd3afab83705cbbb499e45c01392d21c288c8cc07dc2bdbc64b51a for weights.npz. If you check out another commit, the manifest records whatever you installed; reading that code first is up to you. A hash detects accidental changes in that folder, not someone who can already write to it.

Then install the extension and reload Pi:

pi install npm:pi-code-hygiene@0.4.0

To check a working tree without installing it, pi -ne -e <clone>/src/index.js loads that file and nothing else; /hygiene status there shows the version it actually loaded.

Then restart Pi, not /reload. A packaged extension is a .js module in a "type": "module" package, and the loader hands it to Node's native import(), whose registry is keyed by URL and never invalidated; /reload clears Pi's own cache and asks again, but the same URL returns the module already evaluated. Restarting the process is the only way to load a new version. /hygiene shows the version in memory and the one on disk, and says STALE when they differ.

Requirements: Node.js 22.19 or later and Pi with the tool_result extension event. pi install also accepts the path of a local clone of the repository.

Modes

Mode What happens after a successful edit or write
shadow Analyzes the file in the background and appends one line to state/shadow.jsonl. The tool result is not changed and nothing waits.
on (default) Waits for the analysis (up to 3 s) and appends a short note with up to two findings of probability 0.55 or more.
fix Like on, but assertive and narrower: see Fix mode.
off Does nothing.

Choose the mode in config.json (%LOCALAPPDATA%\pi-hygiene\config.json), with /hygiene, or with the PI_CODE_HYGIENE_MODE environment variable. The plain mode file of earlier versions is still read; nothing writes it any more.

Each key is resolved on its own, in this order: the environment variable for the mode, then config.json, then the old mode file, then the default. config.json is read on every edit, so a change takes effect at once; when nothing holds a mode name the mode is on. A file that says something else is a typo, and a typo must not turn an intended off into a running analyser: when no source is valid and a file was there saying something else, the mode is off, while an unusable environment variable leaves the file or the default in charge, as before. Surrounding space and capital letters are ignored, and an empty value counts as unset. A value this version does not know never starts the mode it names and never overrides the other setting: if the environment variable is wrong, the file applies, and if the file is wrong, the mode is off unless the environment variable is valid. A value that was set but not understood is written once per session to the log as bad_mode, so a silent stop leaves a trace; it is recorded as "unprintable" unless it is a short printable string, and when both settings are wrong it is the environment one that is named. fix needs 0.2.0 or later: an older version ignores it and falls back to the mode file, or to shadow, its own default, when there is none. In on mode a note looks like this:

[code-hygiene] Heuristic readability observations (may be wrong, info only):
  line 9: deep_nesting (0.92)
  line 5: dense_line (0.89)

The note is information. The extension never marks an edit as failed, never asks the agent to continue, and does not show the same note twice in a row for a file. Any failure (missing or changed analyser files, a deadline missed, the scorer throwing) is recorded and otherwise ignored.

The only setting outside config.json is PI_CODE_HYGIENE_HOME, which moves the install folder.

Settings and /hygiene

/hygiene with no arguments opens a menu: the status line first, then status and diagnostics, mode, threshold and notes, installation, log, and reset. In the terminal it draws its own list: the entry under the cursor shows what it does on the same line, the others stay quiet, and escape closes the menu or goes back to it, so there is no row for that. Cancelling changes nothing, and the two actions that overwrite something ask first. With an argument it does that one thing and exits, which is what scripts and RPC clients use:

/hygiene status
/hygiene mode fix
/hygiene set threshold 0.65
/hygiene set rounds 2
/hygiene check
/hygiene reset --yes

Commands are dispatched by Pi in interactive and RPC modes. In -p and --mode json they are not dispatched at all, so the argument form is for RPC clients and interactive use. An RPC client has no cursor, so there the menu is a plain list with the description on every line and a last row to close it or go back.

config.json holds these keys:

Key Default What it changes
version 1 Schema of this file, not of the package
mode on What happens after each analysis
threshold 0.55 Threshold passed to codehygiene, from 0.40 to 0.90 in steps of 0.05
maxFindings 2 Findings per note, in both on and fix, from 1 to 3
rounds 2 Rounds per file in fix mode, from 1 to 4; 1 is one note and then silence
{
  "version": 1,
  "mode": "on",
  "threshold": 0.55,
  "maxFindings": 2,
  "rounds": 2
}

A value that is present but not understood falls back to its own default and is reported once per session as bad_config, with the key and at most 16 printable characters of the value. An empty string counts as unset. An unreadable file is kept as config.json.bad before it is replaced. Unknown keys and a newer version are left alone. The menu is the only thing that writes the file, it says when a save failed, and it says when PI_CODE_HYGIENE_MODE will override the saved mode. The values are for this install, so they apply to every session and to any subagent that uses it.

Only the global threshold is editable. fix judges every kind 0.7.0 emits at 0.65, and the 0.75 entries it keeps name two kinds 0.7.0 dropped. The 0.65 and 0.75 bars are constants of the package, and so are the 200 KB file limit, the 3 s time limit, two concurrent analyses and the note budgets.

Fix mode

on is information the agent can ignore, and agents mostly do. fix asks for action, with limits meant to keep it from chasing false positives. It is experimental: that it changes what an agent does is not measured, only that it shows the right findings. Its limits are starting points, not results of a trial.

  • It reports only findings in code the agent wrote during the current run (within one line of it). Before the agent's first edit of a file in a run, the extension keeps the file's lines in memory; "the code you wrote" is what the edit changed, worked out against that copy. A run starts with a submitted prompt. A message sent while the agent is still working joins the same run, so it does not reset the budgets; the next prompt sent while the agent is idle starts a new run. A file that does not exist yet counts as all written. A file it cannot read, that is over 200 KB, or that would be skipped anyway (node_modules and similar) is not reported, and neither is a file whose lines it could not keep.
  • It works in rounds per file, to follow the cycle codehygiene's author describes for agents: fix, re-check, stop. The first note opens a round. Two rounds is the default and rounds in config.json moves it from one to four; with one, the first note is the only one. The next edit of that file that touches a flagged line (or a write) is the re-check, and what is still flagged by that edit is listed once more as the final pass. After that the file is silent for the rest of the agent run. Edits that touch nothing flagged can still bring notes about new findings, up to three notes per file.
  • A note lists at most maxFindings findings, two by default, best first, within 600 characters. They have probability 0.65 or more. duplicate_block is never reported: it needs both copies to be in the edit, and codehygiene gives one line.
  • Each finding comes with what usually fixes it, a fixed text of this package. Nothing from your code goes into a note.
  • A run gets at most eight notes or about 4,000 characters of them, as a cost limit, and it tracks the first 32 files it sees. With three findings, which needs maxFindings raised from its default, and a recipe each, a note can reach 600 characters, so when the room runs out the weakest one is dropped.
  • It adds one fixed rule to the system prompt, only in this mode: the user's requested scope and the project's linter come first, and untouched code is not to be edited to improve a score.
[code-hygiene] Heuristic findings (they may be wrong) in lines you just changed:
  line 10: deep_nesting (0.95) -> flatten with a guard clause or early return
  line 11: repetitive_structure (0.90) -> fold the copies into one loop or table if it reads better
Fix only the real ones, in code you just wrote. Skip any fix that would change behavior, drop required
error handling or clash with the project linter, and name what you skipped in one sentence.
Do not edit untouched code to lower the score. (pass 1 of 2)

The limits are starting points, not measured values. Compare fix with on on your own work before you keep it. Every record says the version of the extension that wrote it (v), and each session starts with one record of its own: outcome session, the version in memory and the one on disk (disk_version), stale, the mode that applies, the text it came from (mode_raw), where it came from (mode_source), the threshold, the note limits, the schema of the configuration, and the moment the module was loaded. That line is written even in off mode, because it is how you tell that the extension is loaded at all. A record of an analysis or of a skipped edit says the mode, the tool, the file id and the outcome; a record of a rejected mode value says that its outcome was bad_mode, plus the value; a rejected setting is bad_config. Some carry more:

  • An analysis that produced a result records the score and counts, how many findings the file had, and how long the wait and the run took. If the same file was analyzed earlier in the session and the file did not change in the meantime, it also says how many findings were resolved, persisted or new.
  • When a note is shown, on and fix say how many findings it carried. A fix record may also carry the round, whether the edit was a re-check of an earlier note, how many flagged findings were resolved or persisted, and a count per reason for the findings it held back: outside_edit, threshold, excluded, repeated, cap, rounds, file_cap, run_cap or no_baseline.
  • A record of something that produced no result carries the reason it was skipped, or the failure that ended the analysis: timeout, aborted, worker_error or worker_restart. Every record names the engine that produced it (engine, js for the worker thread) and the version of the extension (v).

The field names are in src/index.js, where the record is written. What the note says is never logged, and neither is any text or hash of your code.

Limits and privacy

  • It sees edits made through Pi's edit and write tools. Changes made by bash, by an editor, or by a subagent that does not load the extension are not analyzed.
  • It analyzes files with the extensions codehygiene documents (.py .ts .tsx .js .jsx .mjs .cjs .rs .go .c .h .cc .cpp .cxx .hpp .zig .java .cs .lua), up to 200 KB, outside node_modules, .git, .venv, venv, dist, build, target and __pycache__.
  • At most two analyses are accepted at once and at most eight wait; the rest are skipped. They run one after another inside a single worker thread, because the scorer is synchronous. The 3 s limit counts the wait for a turn as well as the wait for a free slot, so an edit is never held longer than that. A newer edit of the same file replaces the pending analysis. A file that changes while it is being analyzed produces no note.
  • Each analysis runs in a worker thread that loads the scorer and unpacks the model once, then answers every analysis after it. Nothing is started as a process. On this package's own source and test files the round trip measures 25 ms to 235 ms, the first one included, which pays for the worker and the model. A synthetic file at the 200 KB cap takes about 2 s, so the 3 s limit is closer on large files, and in on mode that time is added to the edit. The figures come from one machine (Windows, Node 26) and one install: read them as a shape, not as a promise.
  • The JavaScript scorer and the Python command line of the same upstream checkout agree on this package's own 17 source and test files and on a set of deliberate cases, finding by finding. npm run parity runs that comparison and reports every difference. The script ships with the package but is not part of npm test, because it needs the analyser's Python side, which the package does not carry. The widest gap it has found in a probability is 4.4e-5, which is the reference's rounding to four decimals and also the limit of what it can see.
  • Everything is local. The extension makes no network requests, and the upstream update check is never run. The log holds counts, timings, versions, the engine that produced them and a salted hash of the path: no code, no paths and no text from the findings. Once per session it may also hold a rejected value, at most 16 printable characters of the configuration, or the word unprintable. The log is rotated to shadow.jsonl.1 when it passes 1 MiB, and truncated instead of renamed when another process holds it open.
  • config.json holds only the settings above. If it cannot be parsed it is copied to config.json.bad before a new one is written, so a file edited by hand is recoverable but stays in the install folder.
  • codehygiene scores the whole file, not the lines you changed, so older problems can show up after a small edit in shadow and on. fix filters them out.
  • The score is sensitive: a small harmless edit can move a file's score a lot, and the findings are heuristic. In fix mode an agent can still act on a wrong finding; the notes tell it to skip fixes that change behavior.
  • Moving the pin from 0.5.1 to 0.7.0 cut the diagnostics reported on this package's own 14 source and test files, at threshold 0.55, from 102 to 24, both counts taken on the tree that ships this line. That is how many flags came out, not how right they are: without labelling each one, fewer flags could also mean fewer real problems caught. 0.7.0 also dropped the naming, ceremony and swallowed-error circuits and reports six geometric kinds instead.
  • The worker is a thread of the Pi process, not a sandbox: the scorer runs with the same permissions as the extension. It keeps the analysis off the thread that draws the interface, and it is ended when it misses its deadline, but it is not a security boundary. See SECURITY.md.

Troubleshooting

/hygiene status says install: ok or the reason it is not, and /hygiene then Installation lists the folder, engine.js and weights.npz, says which of them is missing, and runs a real analysis when you accept the check. The reasons are:

  • engine_missing or weights_missing: one of the two files is not in src/codehygiene under the install folder. Check out the pinned commit again.
  • manifest_missing or manifest_invalid: manifest.sha256 is absent, unreadable, or does not pin both files. Rebuild it with the commands above.
  • pin_mismatch: a file does not hash to its line in the manifest. Something in that folder changed; reading the difference is up to you before you fix the hashes.

While an install problem lasts, every edit is recorded as skipped and nothing is analyzed. A record with outcome timeout means the analysis missed the 3 s deadline: the worker is ended and a later edit starts a new one, so the next analysis is unaffected. worker_error means the worker or the scorer failed; nothing is shown to the agent, the worker is ended and the next edit starts a new one. worker_restart is what an analysis that was still waiting gets when a different one ran out of time and the worker had to be replaced: it never started, so it is not a failure of the scorer. A file over the 200 KB cap, or in a folder such as node_modules or dist, is skipped on purpose and says so with skip in the record.

A note that says other instead of a kind name means the scorer reported a kind this version of the extension does not know. It reaches no note text and no log line beyond the word, by design.

Keeping up with codehygiene

codehygiene publishes no releases or tags, only commits on its main branch, so this repository pins one commit in compat/codehygiene.json. A weekly GitHub Actions workflow (upstream-watch.yml) compares that commit with upstream main and opens one issue when they differ. The issue holds only checked facts: both commits, the date, how many commits apart they are, whether weights.npz changed, and whether upstream was archived or renamed. Nothing from upstream's text goes into it. Moving the pin is a decision made by hand; closing the issue declines that commit. The workflow can also be run manually, and GitHub pauses scheduled workflows after 60 days without repository activity.

Documentation and support

Issues are welcome in GitHub Issues, pull requests as described in CONTRIBUTING.md, and security reports through SECURITY.md. No response time is promised. Participation follows the Code of Conduct. Changes between versions are listed in the changelog.

codehygiene is a separate project by Brian Bell (brianbell-x/codehygiene); this package neither bundles nor redistributes it. Its pyproject.toml declares the MIT license, but its repository has no LICENSE file, so read its terms before you rely on it. The commit this package expects is d56e737 (codehygiene 0.7.0). This package is released under the MIT License.