omp packages

@rakshazi/ttsr0.1.0

Attach rule reminders to matching write and edit results

rakshazi Published 4 days ago LGPL-3.0-or-later ~0 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

ttsr: rule reminders for write and edit

Install with pi install npm:@rakshazi/ttsr.

A port of oh-my-pi's TTSR (Time Traveling Stream Rules) to Pi, cut down to the non-blocking half. When a write or edit call carries text that matches a regex or an ast-grep pattern in one of the rule files, the tool result gets one extra text block naming the rule and pointing at the full file. Nothing is blocked, retried, aborted or re-prompted.

What the model sees

After a write whose content contains log.Fatal, the tool result ends with:

<rule-reminder>
TTSR rule reminder for the write above: the content matched 1 rule. The change was applied and nothing was blocked. Check it against the rule and fix a real violation in a follow-up edit.
- no-fatal.md: No log.Fatal outside main: return the error instead
  Read the full rule, with explanation and examples, at ~/.pi/agent/rules/no-fatal.md
</rule-reminder>

The original tool output stays untouched above it. Several rules can share one note, and each rule has its own cooldown.

Layout

Path Purpose
~/.pi/agent/extensions/ttsr/index.ts Extension entry: hooks, cooldown filter, session entry, note attachment
~/.pi/agent/extensions/ttsr/rules.ts Frontmatter parsing, condition compilation, matching, rendering
~/.pi/agent/extensions/ttsr/glob.ts Path glob compilation and the glob-shorthand test
~/.pi/agent/extensions/ttsr/rules.test.ts node:test suite, no Pi process needed
~/.pi/agent/extensions/ttsr/ast.ts Loads ast-grep and matches astCondition patterns
~/.pi/agent/extensions/package.json The package manifest of the whole repo; pins the two ast-grep packages
~/.pi/agent/extensions/ttsr/ast.test.ts AST matching on the real engine; fails when it is not installed
~/.pi/agent/extensions/ttsr/parity.test.ts oh-my-pi frontmatter parity and load-problem cases
~/.pi/agent/extensions/ttsr/ledger.test.ts Cooldown modes, resume, the session entry
~/.pi/agent/extensions/ttsr/harness.ts Test-only fake of the Pi API over a session branch
~/.pi/agent/rules/*.md The rule files it reads

The paths are those of a clone into ~/.pi/agent/extensions, where Pi discovers extensions/*/index.ts automatically and no settings entry is needed.

Install

Regex rules work as soon as the directory exists. astCondition rules need ast-grep, which comes as two npm packages with prebuilt binaries. pi install of the repo as a package installs them. A plain clone needs one command:

cd ~/.pi/agent/extensions && npm install

The .npmrc in the repo root keeps install scripts off and keeps Pi's own packages, declared as peers, out of node_modules. npm installs the prebuilt binary for the machine it runs on. When Pi runs in a container that mounts this directory, run the install somewhere with the same OS, CPU architecture and libc as the container. Without it the extension still loads, regex rules still fire, and one warning at session start says how many astCondition rules are off.

How it works

  1. session_start loads every .md file in the rules directory.
  2. tool_result for write and edit builds the payload and matches it against the loaded rules.
  3. For the rules that matched, it reads the cooldown state off the session branch, drops the ones still cooling down, records the rest in a ttsr-injection session entry and appends the note.

The payload is what lands on disk: content for write, every edits[].newText for edit. Removed text (oldText) is ignored, because a line being deleted cannot be a newly introduced violation.

Cooldown

How often a rule may remind is its repeatMode:

repeatMode Behaviour
once One reminder per session branch, ever
after-gap Again once REPEAT_GAP_TURNS (10) turns have passed
always Again on the next turn, never twice inside one turn

A rule without the key gets DEFAULT_REPEAT_MODE, which is after-gap. Both constants live in index.ts and stand in for oh-my-pi's ttsr.repeatMode and ttsr.repeatGap settings. oh-my-pi's own default is once, so a rule with no repeatMode reminds more often here than there; a rule that sets the key behaves the same in both.

The extension keeps no cooldown state in memory. The clock is the number of assistant messages on the current session branch, and the record of what fired is the ttsr-injection entries on that same branch ({ rules, at }, never sent to the model). So /resume, /reload, a fork and /tree all see exactly the cooldowns of the branch they land on.

Rule file format

---
description: "No log.Fatal outside main: return the error instead"
scope: "tool:edit(*.go), tool:write(*.go)"
condition:
  - 'log\.Fatal|os\.Exit\('
---

The body is the long form the model reads on demand.

condition takes the same regex in any of the three YAML shapes, so a one-pattern rule fits on one line:

---
description: Use bare `catch {` when the error binding is unused
scope: "tool:edit(*.ts), tool:write(*.ts)"
condition: 'catch \(_'
---

A one-line list holds several patterns, and the quotes may mix per item:

---
description: "No panic or log.Fatal"
condition: ["\\bpanic\\(", 'log\.Fatal']
---

scope and globs accept the same scalar, block-list and one-line-list forms. A # starts a comment at the start of a line or after whitespace outside quotes, so # why above a pattern and condition: 'x' # why both parse; a # inside quotes stays in the pattern.

Key Meaning
description The one line shown in the note. Optional, as in oh-my-pi: without it the file name minus .md is shown
condition Required unless the rule has an astCondition. One regex or a list of them: a scalar (condition: 'x'), a block list (condition: then one - 'x' line per pattern) or a one-line list (condition: ['x', "y"]). Any match triggers the rule. A leading (?ims) inline flag group is converted into JS flags, so (?m)^... works
scope Optional tool gate: tool:edit(<glob>), tool:write(<glob>), tool:<name> or bare tool. Without it the rule applies to both edit and write on any path. A declared scope that yields no tool token, such as scope: "text" or a typo like edit(*.go), makes the rule fail to load, so a slip cannot widen it
globs Optional extra path gate over the same glob syntax
astCondition One ast-grep pattern or a list of them, for example strings.SplitN($S, $SEP, 2). Any match triggers the rule, and a rule with both condition and astCondition fires when either hits. The language comes from the file extension. The pattern runs against the written text itself, so a statement on its own matches with no surrounding file
repeatMode Optional: once, after-gap or always, see Cooldown. Same key and meaning as oh-my-pi. An unknown value falls back to the default
enabled Optional. enabled: false switches the rule 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
ttsr_trigger, ttsrTrigger Legacy spellings of condition, read only when condition is absent

Keys may be written in kebab-case: repeat-mode is read as repeatMode, the way oh-my-pi reads it.

A condition entry shaped like a path glob is scope shorthand, as in oh-my-pi. condition: '*.rs' means "any edit or write to a .rs file", and condition: ['*.rs', 'unsafe'] means "unsafe in an edit or write to a .rs file". An entry counts as a glob when it has a glob character (? * [ ] { }), none of \ ^ $ + | ( ), and either a / or the exact form *.ext. So .* and log\.Fatal stay regexes.

Globs match against the write path and its basename, so *.go hits pkg/x.go, docs/swagger* hits /repo/docs/swagger.yaml, and **/*.go hits main.go as well as a/b/c.go. Brace alternation works like the shell and nests, so *.{ts,tsx} hits a.ts and pkg/a.tsx while *.{go,rs} stays off TypeScript files. A rule needs at least one trigger to load: a compilable condition or an astCondition. Keys this extension has no use for, such as interruptMode, are ignored.

Load problems

A file that declares a trigger (condition, ttsr_trigger, ttsrTrigger, astCondition or question) and still cannot work is reported once at session start, in a warning that names the file and the reason: a regex that does not compile, a scope with no tool token, an empty condition list, or a rule that has only a question (oh-my-pi hands those to a judge model, which does not exist here). A rule with one good and one bad pattern loads and the bad pattern is reported. Disabled files and files with no trigger key are skipped without a word.

Sharing the rules directory with oh-my-pi

The files use oh-my-pi's rule format, so one directory can serve both agents: make ~/.pi/agent/rules a symlink to ~/.omp/agent/rules and an edit lands in both at once. The keys this extension reads carry oh-my-pi's meaning, with one exception: a quoted enabled: "false" disables a rule only here.

To drop a rule, delete its file. To keep the file around while silencing it, set enabled: false.

Configuration

The rules directory is $PI_CODING_AGENT_DIR/rules when that variable is set, otherwise ~/.pi/agent/rules. The note 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/ttsr && node --test *.test.ts

The suites import the extension module through harness.ts, a fake pi object over an in-memory session branch, so they run on plain Node with no model call. A second harness over the same branch stands in for a resumed session. parity.test.ts holds the oh-my-pi frontmatter cases and load problems, ledger.test.ts the cooldown modes and the session entry, and ast.test.ts runs the real ast-grep engine: it fails outright when the packages are missing, and one of its cases starts a copy of the extension with no node_modules to check the warning and the fallback to regex rules. rules.test.ts covers parsing in every condition shape, comments, glob compilation, payload extraction, scope gating, one-line-condition rules firing end to end, cooldown behaviour, edit handling and sibling calls.

Limits

  • Non-blocking by design: no interruption, no retry, no turn injection.
  • question, agents and interruptMode are oh-my-pi keys this extension ignores. A judged rule (one with a question) reminds on its regex or AST prefilter alone, a rule with nothing but a question cannot run and is reported as a load problem, and a rule limited to some agents reminds for all of them.
  • AST matching covers Go, TypeScript, TSX and JavaScript. An astCondition on any other file type never matches.
  • A pattern ast-grep cannot make sense of never matches, and nothing reports it. Regex conditions are checked at load; AST patterns are only ever tried.
  • A file with globs and no trigger is a rulebook, which this extension skips. The sibling rulebooks extension handles it.
  • After a resume the turn count carries on from the branch, where oh-my-pi restarts the gap from zero. An after-gap rule can therefore remind sooner after a resume here than there.
  • If the session entry cannot be written, the note is still delivered and the rule may remind again on the next matching call.
  • Only the first 65536 characters of a payload are scanned (MAX_SCAN_CHARS in rules.ts); anything past that window is ignored, which bounds what a rule regex can cost.
  • Conditions run synchronously on the tool-result path, so a pathological regex in a local rule file can still stall one tool result inside that capped window. AST rules parse each piece of the payload once per rule: about 95 ms for one piece at the cap against ten Go rules, measured.
  • An edit with several replacements is parsed one replacement at a time for AST rules, so a pattern that spans two replacements does not match. Regex conditions scan them joined by newlines. Only the first 64 replacements are parsed (MAX_AST_PARTS in rules.ts), inside the same 65536-character budget.
  • A rule whose condition is .* or a glob shorthand is a path rule: it fires on any write or edit of a matching path, a pure deletion included.
  • A rule that is deleted from disk keeps its reminder text only until the next session reload.