repositories / pi-ext

pi-ext

bugabingas pi extensions

owned by bugabinga

extensions/llmiterate/README.md

Raw

llmiterate

Manual editor-save bridge from comment markers to background Pi edits.

Behavior

llmiterate does not autostart. /llmiterate or /llmiterate on starts it for the exact real cwd. It loads global Pi settings, then <scope>/.pi/settings.json; there is no upward project-root search.

Start sequence:

  1. resolve cwd
  2. load config and state
  3. acquire singleton lock for the scope hash
  4. start watcher
  5. run initial scan
  6. queue matching markers

Marker syntax

One marker only:

llmiterate:

Marker must appear after a known comment prefix. Raw markers never run.

void main(final String[] a) {
  // llmiterate: implement argument validation
  if (a.length == 0) {
    // llmiterate: print usage and exit non-zero
  }
}
def parse(src):
    # llmiterate: implement parser fast path
-- llmiterate: add index for lookup query

Rules:

Comment prefixes

Prefixes are extension-scoped. Built-ins include:

file type prefix
.c/.cc/.cpp/.cs/.cts/.go/.h/.hpp/.java/.js/.jsx/.kt/.kts/.mts/.php/.scala/.swift/.ts/.tsx // /// /* /**
.rs // /// //! /* /**
.css /* /**
.scss/.less // /* /**
.py/.sh/.bash/.zsh/.fish/.rb/.pl/.pm/.r/.toml/.yaml/.yml/.dockerfile/.ex/.exs #
.md/.mdx/.html/.htm/.xml
.sql/.lua/.hs --
.lisp/.el/.clj/.scm ; ;; ;;;
.tex % %%
.erl %
.f/.f90 !

Unknown extensions do not run markers. Markdown only accepts HTML comments, so headings or bullets containing llmiterate: are inert.

Config commentPrefixes appends by extension:

{
  "llmiterate": {
    "commentPrefixes": {
      "foo": ["COMMENT"],
      "lua": ["--"]
    }
  }
}

Replacement behavior

For each marker block, the worker receives a prompt containing:

Before a job starts, llmiterate rechecks that the marker still exists at the same line with the same prompt hash. If it changed, the job is failed as stale and skipped.

After the worker finishes, llmiterate rechecks the file. If the same marker is still present at the same line with the same prompt hash, the job is marked failed. Otherwise that job is marked done.

Install / load

This extension is loaded through the root pi-ext package. See ../../README.md.

Commands / tools / settings

Commands:

/llmiterate                 toggle on/off for current cwd
/llmiterate on              start watcher for current cwd + initial scan
/llmiterate off             stop watcher + worker
/llmiterate status          show scope/counts/state path
/llmiterate explain <path>  explain include/exclude/marker decision
/llmiterate log             render latest worker transcript and open it in $VISUAL/$EDITOR

Debug commands, registered but not autocompleted:

/llmiterate scan            scan current scope now
/llmiterate forget          forget run state
/llmiterate reload          reload config + watcher

Tools: none.

Hooks:

Settings namespace: llmiterate in global Pi settings and <scope>/.pi/settings.json.

setting default behavior
enabled true false blocks /llmiterate on
include ["**/*"] glob allow-list
exclude built-in metadata/build/cache dirs/files merged with user excludes
debounceMs 3000 save debounce before scan
maxFileBytes 1000000 larger files skipped
timeoutMs 600000 RPC prompt timeout
maxWatchDirs 10000 watcher directory cap
commentPrefixes built-ins by extension appended by extension key
model unset passed to worker as --model when set
thinking unset off | minimal | low | medium | high | xhigh; passed as --thinking when set
notify errors off | errors | done | all; worker notifications obey this policy

Example:

{
  "llmiterate": {
    "enabled": true,
    "include": ["src/**/*.{ts,tsx,rs,java,md}", "docs/**/*.md"],
    "exclude": ["**/target/**", "**/dist/**"],
    "debounceMs": 3000,
    "maxFileBytes": 1000000,
    "timeoutMs": 600000,
    "maxWatchDirs": 10000,
    "model": "openai-codex/gpt-5.5",
    "notify": "errors",
    "commentPrefixes": {
      "foo": ["COMMENT"],
      "lua": ["--"]
    }
  }
}

Internals

Only one watcher may own an exact real cwd at a time. Another Pi session in the same cwd enters standby and reports the lock owner. Parent and child dirs are independent.

Watcher behavior:

llmiterate itself does not edit source files. It queues prompts. A background Pi RPC worker performs edits with normal Pi tools.

State file:

<platform-state-dir>/pi/llmiterate/<sha256(scope)>/state.json

Linux default:

${XDG_STATE_HOME:-~/.local/state}/pi/llmiterate/<sha256(scope)>/state.json

Statuses:

queued | running | done | failed

Duplicate saves do not start duplicate queued/running jobs. Failed markers remain retryable on next save or scan. Queued/running markers from a previous Pi process are reset to failed on state load. A marker becomes done after the worker finishes and the original same-line/same-hash marker no longer exists.

UI channels:

Markdown source
File outline
<!-- !llmiterate -->

# llmiterate

Manual editor-save bridge from comment markers to background Pi edits.

## Behavior

llmiterate does not autostart.
`/llmiterate` or `/llmiterate on` starts it for the exact real cwd.
It loads global Pi settings, then `<scope>/.pi/settings.json`; there is no
upward project-root search.

Start sequence:

1. resolve cwd
2. load config and state
3. acquire singleton lock for the scope hash
4. start watcher
5. run initial scan
6. queue matching markers

## Marker syntax

One marker only:

```text
llmiterate:
```

Marker must appear after a known comment prefix.
Raw markers never run.

```java
void main(final String[] a) {
  // llmiterate: implement argument validation
  if (a.length == 0) {
    // llmiterate: print usage and exit non-zero
  }
}
```

```py
def parse(src):
    # llmiterate: implement parser fast path
```

```sql
-- llmiterate: add index for lookup query
```

Rules:

- raw `llmiterate:` is ignored
- consecutive marker lines are grouped
- empty marker lines are ignored
- `!llmiterate` in a comment disables the whole file

## Comment prefixes

Prefixes are extension-scoped.
Built-ins include:

| file type                                                                                 | prefix            |
| ----------------------------------------------------------------------------------------- | ----------------- |
| .c/.cc/.cpp/.cs/.cts/.go/.h/.hpp/.java/.js/.jsx/.kt/.kts/.mts/.php/.scala/.swift/.ts/.tsx | // /// /* /**     |
| .rs                                                                                       | // /// //! /* /** |
| .css                                                                                      | /* /**            |
| .scss/.less                                                                               | // /* /**         |
| .py/.sh/.bash/.zsh/.fish/.rb/.pl/.pm/.r/.toml/.yaml/.yml/.dockerfile/.ex/.exs             | #                 |
| .md/.mdx/.html/.htm/.xml                                                                  | <!-- -->          |
| .sql/.lua/.hs                                                                             | --                |
| .lisp/.el/.clj/.scm                                                                       | ; ;; ;;;          |
| .tex                                                                                      | % %%              |
| .erl                                                                                      | %                 |
| .f/.f90                                                                                   | !                 |

Unknown extensions do not run markers.
Markdown only accepts HTML comments, so headings or bullets containing
`llmiterate:` are inert.

Config `commentPrefixes` appends by extension:

```json
{
  "llmiterate": {
    "commentPrefixes": {
      "foo": ["COMMENT"],
      "lua": ["--"]
    }
  }
}
```

## Replacement behavior

For each marker block, the worker receives a prompt containing:

- source location:
  `@file:startLine`
- marker text
- marker span to replace
- instruction to work in/around the file
- instruction to remove the marker unless impossible

Before a job starts, llmiterate rechecks that the marker still exists at the
same line with the same prompt hash.
If it changed, the job is failed as stale and skipped.

After the worker finishes, llmiterate rechecks the file.
If the same marker is still present at the same line with the same prompt hash,
the job is marked failed.
Otherwise that job is marked done.

## Install / load

This extension is loaded through the root pi-ext package.
See [../../README.md](../../README.md).

## Commands / tools / settings

Commands:

```text
/llmiterate                 toggle on/off for current cwd
/llmiterate on              start watcher for current cwd + initial scan
/llmiterate off             stop watcher + worker
/llmiterate status          show scope/counts/state path
/llmiterate explain <path>  explain include/exclude/marker decision
/llmiterate log             render latest worker transcript and open it in $VISUAL/$EDITOR
```

Debug commands, registered but not autocompleted:

```text
/llmiterate scan            scan current scope now
/llmiterate forget          forget run state
/llmiterate reload          reload config + watcher
```

Tools:
none.

Hooks:

- `session_start`:
  stores UI context and renders status
- `session_shutdown`:
  stops watcher/worker and releases lock

Settings namespace:
`llmiterate` in global Pi settings and `<scope>/.pi/settings.json`.

| setting           | default                                  | behavior                                                                            |
| ----------------- | ---------------------------------------- | ----------------------------------------------------------------------------------- |
| `enabled`         | `true`                                   | `false` blocks `/llmiterate on`                                                     |
| `include`         | `["**/*"]`                               | glob allow-list                                                                     |
| `exclude`         | built-in metadata/build/cache dirs/files | merged with user excludes                                                           |
| `debounceMs`      | `3000`                                   | save debounce before scan                                                           |
| `maxFileBytes`    | `1000000`                                | larger files skipped                                                                |
| `timeoutMs`       | `600000`                                 | RPC prompt timeout                                                                  |
| `maxWatchDirs`    | `10000`                                  | watcher directory cap                                                               |
| `commentPrefixes` | built-ins by extension                   | appended by extension key                                                           |
| `model`           | unset                                    | passed to worker as `--model` when set                                              |
| `thinking`        | unset                                    | `off \| minimal \| low \| medium \| high \| xhigh`; passed as `--thinking` when set |
| `notify`          | `errors`                                 | `off \| errors \| done \| all`; worker notifications obey this policy               |

Example:

```json
{
  "llmiterate": {
    "enabled": true,
    "include": ["src/**/*.{ts,tsx,rs,java,md}", "docs/**/*.md"],
    "exclude": ["**/target/**", "**/dist/**"],
    "debounceMs": 3000,
    "maxFileBytes": 1000000,
    "timeoutMs": 600000,
    "maxWatchDirs": 10000,
    "model": "openai-codex/gpt-5.5",
    "notify": "errors",
    "commentPrefixes": {
      "foo": ["COMMENT"],
      "lua": ["--"]
    }
  }
}
```

## Internals

Only one watcher may own an exact real cwd at a time.
Another Pi session in the same cwd enters standby and reports the lock owner.
Parent and child dirs are independent.

Watcher behavior:

- uses `fs.watch`
- respects `.gitignore` and `.ignore`
- applies `include`/`exclude`
- skips files over `maxFileBytes`
- debounces file events by `debounceMs`
- refreshes watched dirs after directory changes
- stops at `maxWatchDirs`

llmiterate itself does not edit source files.
It queues prompts.
A background Pi RPC worker performs edits with normal Pi tools.

State file:

```text
<platform-state-dir>/pi/llmiterate/<sha256(scope)>/state.json
```

Linux default:

```text
${XDG_STATE_HOME:-~/.local/state}/pi/llmiterate/<sha256(scope)>/state.json
```

Statuses:

```text
queued | running | done | failed
```

Duplicate saves do not start duplicate queued/running jobs.
Failed markers remain retryable on next save or scan.
Queued/running markers from a previous Pi process are reset to failed on state
load.
A marker becomes `done` after the worker finishes and the original
same-line/same-hash marker no longer exists.

UI channels:

- source file:
  final edits only
- footer/status:
  watching/standby/queued/running/watch errors
- notifications:
  command results and worker errors
- `/llmiterate log`:
  latest worker JSONL transcript rendered as markdown at
  `<state>/logs/latest.md`