@hcflabs/pi-hashline0.3.0
Anchor-addressed file edits for Pi: read serves a 5-letter anchor for every line, and replace or insert refuses stale anchors.
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
@hcflabs/pi-hashline
Edit files by anchor instead of line number. Read returns one 5-letter anchor for every line, and replace or insert lands on exactly that line or refuses a stale anchor. A refused edit returns the current anchored rows, so the retry needs no extra read.
Install
pi install npm:@hcflabs/pi-hashline
The extension replaces the built-in read tool and turns off the built-in edit tool. Run read on any file. Each row must look like abdef│line text, where abdef is the line anchor.
To return to the built-in tools, run /hashline off, or set enabled to false in the config file.
Works with
| Package | Relationship | Purpose |
|---|---|---|
@hcflabs/pi-config-store |
Required | Safe loading and reporting of the package config file. |
Use
Read a file, then replace one line by its anchor:
{ "remove_from": "abdef", "remove_to": "abdef", "text": " console.log('hi');" }
The result is a post-edit diff. Rows that start with + or a space carry live anchors, so the next edit needs no read.
| Surface | Type | Purpose |
|---|---|---|
read |
tool | Serve a text file as anchor│content rows. Replaces the built-in read tool. |
replace |
tool | Remove a range of lines and write replacement text. |
insert |
tool | Add lines after or before an anchor line. |
undo_last_change |
tool | Revert the most recent anchored edit on one file. |
/hashline |
command | Show or change the enabled state and the diff context. |
Commands are for humans. Tools are for the agent.
Replace
replace takes remove_from and remove_to anchors, and one text string. Use the same anchor twice for one line. An empty text deletes the range. A trailing line break in text sets the last written line ending; it does not add a blank line.
The anchors must come from a served row. A path argument is optional. When you pass it, it must own the anchors.
Insert
insert takes anchor, direction (after or before), and one text string. An empty text inserts one blank line. The anchor line stays in place. To seed an empty file, read it and use replace on the empty-line row.
Undo
undo_last_change takes path and reverts the last anchored replace or insert on that file. The record is single-level and lasts for the session. A successful write does not clear the record, but undo refuses when the file changed after the edit.
Stale anchors
An edit is refused when an anchor is unknown, was never shown, or no longer matches the file on disk. The error carries the current anchored rows when the extension can locate them. Use those rows for the retry, or call read again.
Flow
Read mints one anchor for each line and serves the rows. The session state keeps the anchors, the served set, and the file content that the model saw.
Replace and insert check the anchors against that state, then read the file again inside the file mutation queue. When the disk content differs from the served content, the extension carries anchors of unchanged lines and refuses the edit with current rows. A live edit writes the file, keeps the anchors of untouched lines, mints anchors for changed lines, and returns a post-edit diff.
After the built-in write tool writes a file, the extension appends anchored rows to the result. The next edit can use those anchors.
Config
Package-owned: ~/.pi/agent/config/pi-hashline/config.json
| Name | Description | Values | Default |
|---|---|---|---|
enabled |
Offer anchored read, replace, insert, and undo. When false, the built-in read and edit tools stay active. | boolean | true |
diffContextLines |
Context lines around a post-edit diff. | integer, 0 to 10 | 1 |
The /hashline command writes the file: /hashline on, /hashline off, and /hashline diff <0-10>. An invalid value falls back to its default and the config reporter shows one warning. Fix the file, or run /hashline again to replace it.
State and storage
Anchors, served rows, and undo records live in memory for one session. A new session, a resume, or a reload starts with no records, so earlier anchors are stale. Read the file again after a restart.
The extension writes no generated files. Its only file is the config file.
Limits and recovery
| Limit | Value |
|---|---|
| Read output | 2000 lines or 50KB, whichever comes first |
| Single row | 50KB; larger lines show an anchor-keeping marker |
| File size | 100MB and 200,000 lines |
| Undo | One record for each file, session only |
| Code | Meaning | Recovery |
|---|---|---|
E_STALE_ANCHOR |
The anchor is unknown, or its line changed on disk. | Use the rows in the error, or read the file. |
E_RANGE_STALE |
A boundary anchor was never shown, or the range changed. | Use the rows in the error, or read the file. |
E_UNDO_STALE |
The file changed after the edit. | Read the file, then edit again. |
E_UNDO_NONE |
The session has no undo record for the file. | Nothing to revert; edit the file again. |
E_BAD_SHAPE |
An anchor is malformed, a range spans files, or the text has a NUL byte. | Send 5-letter lowercase anchors and text without NUL bytes. |
E_FILE_TOO_LARGE |
The file is over 100MB or 200,000 lines. | Use write for a full rewrite. |
E_BINARY, E_UTF16 |
The file has NUL bytes or a UTF-16 or UTF-32 byte order mark. | Edit it as UTF-8 first. |
E_NOT_FOUND, E_IS_DIRECTORY |
The path is missing or is a directory. | Check the path. |
E_PATH_MISMATCH |
The path argument does not own the anchors. |
Remove path or pass the owner file. |
A file with mixed line endings is rewritten with its most common ending. A byte order mark is kept.