@rakshazi/rulebooks0.1.0
Attach path-scoped rule files to the first matching tool result
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
rulebooks: path-scoped rule files, injected once
Install with pi install npm:@rakshazi/rulebooks.
The first time a read, write or edit touches a path that a rulebook covers, the full text of that rulebook is appended to the tool result. It is delivered once per session branch and stays out of the context until then, so a Go rulebook costs nothing in a session that never opens a .go file.
This is the Pi counterpart of oh-my-pi's rulebook rules. oh-my-pi lists each rulebook in the system prompt and lets the model fetch it; Pi has no such listing, so the text arrives with the first matching file instead.
What the model sees
After the first read of a Go file, the tool result ends with:
<rulebook-reminder>
The read above touched a path covered by 2 rulebooks. The full text follows and is not repeated this session.
<rulebook path="~/.pi/agent/rules/style-idioms.md">
...the whole body of style-idioms.md...
</rulebook>
<rulebook path="~/.pi/agent/rules/style.md">
...the whole body of style.md...
</rulebook>
</rulebook-reminder>
The original tool output stays untouched above it, and structuredContent is passed through. Several rulebooks that match the same call share one block, sorted by file name.
Layout
| Path | Purpose |
|---|---|
~/.pi/agent/extensions/rulebooks/index.ts |
Extension entry: hooks, the injected set, the session entry |
~/.pi/agent/extensions/rulebooks/rules.ts |
Frontmatter parsing, glob compilation, classification, rendering |
~/.pi/agent/extensions/rulebooks/rules.test.ts |
node:test suite, no Pi process needed |
~/.pi/agent/rules/*.md |
The rule files it reads |
Pi discovers extensions/*/index.ts automatically. The extension has no dependencies and imports nothing from its sibling ttsr.
Which files are rulebooks
The rule files use oh-my-pi's format, so ~/.pi/agent/rules can be a symlink to ~/.omp/agent/rules and both agents read the same files. Two extensions share that directory, and every file belongs to at most one of them:
| A file with | Goes to |
|---|---|
enabled: false |
Nobody |
Any trigger: a non-empty condition, ttsr_trigger, ttsrTrigger, astCondition, or a question |
ttsr, which reminds on matching content |
alwaysApply: true, a body, no trigger |
rulebooks, for every path |
globs, a body, no trigger |
rulebooks, for the matching paths |
| Anything else | Nobody |
The trigger check comes first, as it does in oh-my-pi, so a file with both globs and a condition is a TTSR rule and never a rulebook.
Rule file format
---
description: "Go style: naming, errors, tests"
globs:
- "**/*.go"
---
# Go style
The body is what gets injected, whole.
| Key | Meaning |
|---|---|
globs |
Path globs, as a scalar, a block list or a one-line list (globs: ["**/*.go", "*.mod"]). Required unless alwaysApply is set |
alwaysApply |
alwaysApply: true makes the rulebook match every path. globs on the same file are then ignored, as in oh-my-pi |
description |
Optional. Kept for oh-my-pi, which shows it in its rule listing; the file name minus .md stands in when absent |
enabled |
enabled: false switches the file off, here and in oh-my-pi. Leave it unquoted: a quoted "false" is a string to oh-my-pi and keeps the rule on there |
Keys may be written in kebab-case: always-apply is read as alwaysApply. A # starts a comment at the start of a line or after whitespace outside quotes.
Globs match against the path and its basename, so *.go hits pkg/x.go and **/*.go hits main.go as well as a/b/c.go. * and ? stay inside one path segment, ** crosses segments, and brace alternation works like the shell and nests: *.{ts,tsx} hits a.ts and pkg/a.tsx.
A file with no body after the frontmatter is not loaded: there would be nothing to inject.
How it works
session_startloads every.mdfile in the rules directory and rebuilds the set of already-injected rulebooks from the session.tool_resultforread,writeandedittakes the call'spath, finds the rulebooks whose globs match and drops the ones already injected.- The rest are recorded in a
rulebook-injectionsession entry ({ rules }, never sent to the model) and their bodies are appended to the tool result.
The injected set follows the session branch. /resume, /reload and a fork rebuild it from the entries on the branch, and /tree rebuilds it for the branch it lands on.
Compaction is the one event that makes a rulebook come back. When a compaction cuts the turn that carried the injection out of the context, the text is gone for the model, so the entry before the cut no longer counts and the rulebook is injected again on the next matching path. An injection that survives the cut stays recorded.
Configuration
The rules directory is $PI_CODING_AGENT_DIR/rules when that variable is set, otherwise ~/.pi/agent/rules. The block prints the literal ~/.pi/agent/rules/... path in the default case and the resolved path when the agent dir is relocated.
Tests
cd ~/.pi/agent/extensions/rulebooks && node --test *.test.ts
The suite imports the extension module with a fake pi object, so it runs on plain Node with no model call. It covers parsing in every globs shape, comments, kebab-case keys, glob compilation with braces, the split between rulebooks and TTSR rules, enabled and alwaysApply, and the hook end to end: first injection, silence afterwards, resume, and both compaction cases.
Limits
- An
alwaysApplyrulebook arrives with the firstread,writeoreditof any file. oh-my-pi puts such a rule into the prompt from the first turn, so a session that never touches a file sees it there and never here. - Only
read,writeandedittrigger an injection. A file reached throughbash,greporfinddoes not. - The whole body is injected, with no size cap. A long rulebook costs its full length once per session, and again after a compaction that drops it.
- A rulebook without a
descriptionis loaded here and skipped by oh-my-pi, which needs the description for its listing. - A file that fails to qualify is skipped without a warning.
loadRulebooksreturns the list inskippedand the extension never shows it. The only warnings are a rules directory it cannot read and a session entry it cannot write. - A rule file deleted from disk keeps being matched until the next session start or reload.