pi-edit-first 0.0.0-stage → 0.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +21 -0
- package/README.md +44 -2
- package/extensions/edit-first.ts +157 -0
- package/package.json +14 -4
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 sorinirimies
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/README.md
CHANGED
|
@@ -1,3 +1,45 @@
|
|
|
1
|
-
#
|
|
1
|
+
# pi-edit-first
|
|
2
2
|
|
|
3
|
-
|
|
3
|
+
Pi extension that cuts output tokens by making the agent **edit, not rewrite**.
|
|
4
|
+
|
|
5
|
+
Enforcement lives in a `tool_call` hook, so it adds **zero tokens to the system prompt**.
|
|
6
|
+
The model only sees a one-line reason when a call is blocked.
|
|
7
|
+
|
|
8
|
+
## Rules
|
|
9
|
+
|
|
10
|
+
| # | Blocks | Tells the model |
|
|
11
|
+
|---|---|---|
|
|
12
|
+
| 1 | `write` over an existing file longer than `maxWriteLines` (default 40) | use `edit` with targeted replacements |
|
|
13
|
+
| 2 | `write` of `Cargo.toml`, `settings.gradle[.kts]`, `package.json` in a dir with no project yet | run `cargo init` / `android create` / `npm init -y` |
|
|
14
|
+
|
|
15
|
+
## Install
|
|
16
|
+
|
|
17
|
+
```bash
|
|
18
|
+
pi install npm:pi-edit-first
|
|
19
|
+
# or
|
|
20
|
+
pi install git:github.com/sorinirimies/pi-edit-first
|
|
21
|
+
```
|
|
22
|
+
|
|
23
|
+
Applies to every pi session (including pi run as an external agent in editors).
|
|
24
|
+
|
|
25
|
+
## Commands
|
|
26
|
+
|
|
27
|
+
- `/edit-first` — status and block count
|
|
28
|
+
- `/edit-first off` / `on` — disable / enable for this session
|
|
29
|
+
- `/edit-first allow <path>` — allow one full rewrite of `<path>` this session
|
|
30
|
+
|
|
31
|
+
## Config (optional)
|
|
32
|
+
|
|
33
|
+
`~/.pi/agent/edit-first.json`:
|
|
34
|
+
|
|
35
|
+
```json
|
|
36
|
+
{ "maxWriteLines": 40, "scaffold": true, "ignore": ["CHANGELOG.md", "*.lock"] }
|
|
37
|
+
```
|
|
38
|
+
|
|
39
|
+
## Notes
|
|
40
|
+
|
|
41
|
+
- A block costs one extra round trip, far cheaper than a whole-file rewrite.
|
|
42
|
+
- Only affects the `write` tool; `edit` is never blocked.
|
|
43
|
+
- Does not affect other agents (e.g. Zed's native agent).
|
|
44
|
+
|
|
45
|
+
MIT
|
|
@@ -0,0 +1,157 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* pi-edit-first — make the agent edit, not rewrite.
|
|
3
|
+
*
|
|
4
|
+
* Zero prompt tokens: enforcement happens in a `tool_call` hook. The model only
|
|
5
|
+
* sees a short reason when a call is blocked.
|
|
6
|
+
*
|
|
7
|
+
* Rules
|
|
8
|
+
* 1. `write` over an existing file longer than `maxWriteLines` is blocked → use `edit`.
|
|
9
|
+
* 2. `write` of a project manifest (Cargo.toml, settings.gradle[.kts], package.json)
|
|
10
|
+
* in a directory with no project yet is blocked → use a scaffolder.
|
|
11
|
+
*
|
|
12
|
+
* Commands
|
|
13
|
+
* /edit-first status + block count
|
|
14
|
+
* /edit-first off | on disable / enable for this session
|
|
15
|
+
* /edit-first allow <path> allow one full rewrite of <path> this session
|
|
16
|
+
*
|
|
17
|
+
* Config (optional): <agent dir>/edit-first.json
|
|
18
|
+
* { "maxWriteLines": 40, "scaffold": true, "ignore": ["CHANGELOG.md", "*.lock"] }
|
|
19
|
+
*/
|
|
20
|
+
|
|
21
|
+
import { existsSync } from "node:fs";
|
|
22
|
+
import { readFile } from "node:fs/promises";
|
|
23
|
+
import { homedir } from "node:os";
|
|
24
|
+
import { basename, dirname, isAbsolute, join, resolve } from "node:path";
|
|
25
|
+
import type { ExtensionAPI } from "@earendil-works/pi-coding-agent";
|
|
26
|
+
|
|
27
|
+
interface Config {
|
|
28
|
+
maxWriteLines: number;
|
|
29
|
+
scaffold: boolean;
|
|
30
|
+
ignore: string[];
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
const DEFAULTS: Config = { maxWriteLines: 40, scaffold: true, ignore: [] };
|
|
34
|
+
|
|
35
|
+
function agentDir(): string {
|
|
36
|
+
if (process.env.PI_CODING_AGENT_DIR) return process.env.PI_CODING_AGENT_DIR;
|
|
37
|
+
if (process.env.XDG_CONFIG_HOME) return join(process.env.XDG_CONFIG_HOME, "pi", "agent");
|
|
38
|
+
return join(homedir(), ".pi", "agent");
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
async function loadConfig(): Promise<Config> {
|
|
42
|
+
try {
|
|
43
|
+
const p = JSON.parse(await readFile(join(agentDir(), "edit-first.json"), "utf8"));
|
|
44
|
+
return {
|
|
45
|
+
maxWriteLines:
|
|
46
|
+
Number.isFinite(p.maxWriteLines) && p.maxWriteLines > 0 ? p.maxWriteLines : DEFAULTS.maxWriteLines,
|
|
47
|
+
scaffold: typeof p.scaffold === "boolean" ? p.scaffold : DEFAULTS.scaffold,
|
|
48
|
+
ignore: Array.isArray(p.ignore) ? p.ignore.filter((x: unknown) => typeof x === "string") : [],
|
|
49
|
+
};
|
|
50
|
+
} catch {
|
|
51
|
+
return { ...DEFAULTS };
|
|
52
|
+
}
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
/** Minimal glob: `*` matches anything except `/`; matched against path or basename. */
|
|
56
|
+
function matchesIgnore(path: string, patterns: string[]): boolean {
|
|
57
|
+
return patterns.some((pat) => {
|
|
58
|
+
const re = new RegExp(`^${pat.replace(/[.+^${}()|[\]\\]/g, "\\$&").replace(/\*/g, "[^/]*")}$`);
|
|
59
|
+
return re.test(path) || re.test(basename(path));
|
|
60
|
+
});
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
function countLines(text: string): number {
|
|
64
|
+
return text === "" ? 0 : text.split("\n").length;
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
/** Manifest → what to run instead, and a marker that proves a project already exists. */
|
|
68
|
+
const SCAFFOLDS: Record<string, { hint: string; exists: string[] }> = {
|
|
69
|
+
"Cargo.toml": { hint: "run `cargo init` / `cargo new`", exists: ["Cargo.toml"] },
|
|
70
|
+
"settings.gradle.kts": {
|
|
71
|
+
hint: "run `android create` or `gradle init`",
|
|
72
|
+
exists: ["settings.gradle.kts", "settings.gradle"],
|
|
73
|
+
},
|
|
74
|
+
"settings.gradle": {
|
|
75
|
+
hint: "run `android create` or `gradle init`",
|
|
76
|
+
exists: ["settings.gradle.kts", "settings.gradle"],
|
|
77
|
+
},
|
|
78
|
+
"package.json": { hint: "run `npm init -y`", exists: ["package.json"] },
|
|
79
|
+
};
|
|
80
|
+
|
|
81
|
+
export default function editFirst(pi: ExtensionAPI) {
|
|
82
|
+
let config: Config = { ...DEFAULTS };
|
|
83
|
+
let enabled = true;
|
|
84
|
+
let blocked = 0;
|
|
85
|
+
const allowed = new Set<string>();
|
|
86
|
+
|
|
87
|
+
pi.on("session_start", async () => {
|
|
88
|
+
config = await loadConfig();
|
|
89
|
+
enabled = true;
|
|
90
|
+
blocked = 0;
|
|
91
|
+
allowed.clear();
|
|
92
|
+
});
|
|
93
|
+
|
|
94
|
+
pi.on("tool_call", async (event, ctx) => {
|
|
95
|
+
if (!enabled || event.toolName !== "write") return undefined;
|
|
96
|
+
|
|
97
|
+
const raw = event.input?.path;
|
|
98
|
+
if (typeof raw !== "string" || raw === "") return undefined;
|
|
99
|
+
const abs = isAbsolute(raw) ? raw : resolve(ctx.cwd, raw);
|
|
100
|
+
|
|
101
|
+
if (allowed.has(abs) || matchesIgnore(raw, config.ignore)) return undefined;
|
|
102
|
+
|
|
103
|
+
const deny = (reason: string) => {
|
|
104
|
+
blocked++;
|
|
105
|
+
if (ctx.hasUI) ctx.ui.notify(`edit-first: blocked write to ${raw}`, "warning");
|
|
106
|
+
return { block: true as const, reason };
|
|
107
|
+
};
|
|
108
|
+
|
|
109
|
+
if (existsSync(abs)) {
|
|
110
|
+
let lines = 0;
|
|
111
|
+
try {
|
|
112
|
+
lines = countLines(await readFile(abs, "utf8"));
|
|
113
|
+
} catch {
|
|
114
|
+
return undefined;
|
|
115
|
+
}
|
|
116
|
+
if (lines > config.maxWriteLines) {
|
|
117
|
+
return deny(
|
|
118
|
+
`"${raw}" exists (${lines} lines). Do not rewrite whole files: use the edit tool with small targeted replacements. ` +
|
|
119
|
+
`If a full rewrite is truly required, ask the user to run /edit-first allow ${raw}.`,
|
|
120
|
+
);
|
|
121
|
+
}
|
|
122
|
+
return undefined;
|
|
123
|
+
}
|
|
124
|
+
|
|
125
|
+
if (config.scaffold) {
|
|
126
|
+
const scaffold = SCAFFOLDS[basename(abs)];
|
|
127
|
+
if (scaffold && dirname(abs) === resolve(ctx.cwd)) {
|
|
128
|
+
const hasProject = scaffold.exists.some((f) => existsSync(join(dirname(abs), f)));
|
|
129
|
+
if (!hasProject) {
|
|
130
|
+
return deny(
|
|
131
|
+
`No project here yet. Do not hand-write ${basename(abs)}: ${scaffold.hint}, then fill in the logic.`,
|
|
132
|
+
);
|
|
133
|
+
}
|
|
134
|
+
}
|
|
135
|
+
}
|
|
136
|
+
return undefined;
|
|
137
|
+
});
|
|
138
|
+
|
|
139
|
+
pi.registerCommand("edit-first", {
|
|
140
|
+
description: "Status, on/off, or `allow <path>` for pi-edit-first",
|
|
141
|
+
handler: async (args, ctx) => {
|
|
142
|
+
const [cmd, ...rest] = (args ?? "").trim().split(/\s+/).filter(Boolean);
|
|
143
|
+
if (cmd === "off") enabled = false;
|
|
144
|
+
else if (cmd === "on") enabled = true;
|
|
145
|
+
else if (cmd === "allow" && rest.length > 0) {
|
|
146
|
+
const p = rest.join(" ");
|
|
147
|
+
allowed.add(isAbsolute(p) ? p : resolve(ctx.cwd, p));
|
|
148
|
+
ctx.ui.notify(`edit-first: full rewrite of ${p} allowed this session`, "info");
|
|
149
|
+
return;
|
|
150
|
+
}
|
|
151
|
+
ctx.ui.notify(
|
|
152
|
+
`edit-first: ${enabled ? "on" : "off"} · maxWriteLines=${config.maxWriteLines} · scaffold=${config.scaffold} · blocked=${blocked}`,
|
|
153
|
+
"info",
|
|
154
|
+
);
|
|
155
|
+
},
|
|
156
|
+
});
|
|
157
|
+
}
|
package/package.json
CHANGED
|
@@ -1,6 +1,16 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "pi-edit-first",
|
|
3
|
-
"version": "0.
|
|
4
|
-
"
|
|
5
|
-
"
|
|
6
|
-
|
|
3
|
+
"version": "0.1.0",
|
|
4
|
+
"description": "Pi extension that cuts output tokens: blocks whole-file `write` rewrites and hand-written project manifests, steering the agent to targeted `edit` calls and scaffolders. Zero prompt tokens.",
|
|
5
|
+
"keywords": ["pi-package", "pi-extension", "tokens", "edit", "guard"],
|
|
6
|
+
"license": "MIT",
|
|
7
|
+
"author": "sorinirimies",
|
|
8
|
+
"repository": {
|
|
9
|
+
"type": "git",
|
|
10
|
+
"url": "git+https://github.com/sorinirimies/pi-edit-first.git"
|
|
11
|
+
},
|
|
12
|
+
"homepage": "https://github.com/sorinirimies/pi-edit-first#readme",
|
|
13
|
+
"files": ["extensions", "README.md", "LICENSE"],
|
|
14
|
+
"pi": { "extensions": ["./extensions/edit-first.ts"] },
|
|
15
|
+
"peerDependencies": { "@earendil-works/pi-coding-agent": "*" }
|
|
16
|
+
}
|