pi-hashline-edit-pro 2.7.1 → 2.8.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/README.md +35 -34
- package/index.ts +11 -9
- package/package.json +2 -2
- package/prompts/grep-guidelines.md +3 -6
- package/prompts/grep-snippet.md +1 -1
- package/prompts/grep.md +1 -1
- package/prompts/insert-guidelines.md +3 -6
- package/prompts/insert-snippet.md +1 -1
- package/prompts/insert.md +1 -1
- package/prompts/read-guidelines.md +2 -2
- package/prompts/read-snippet.md +1 -1
- package/prompts/read.md +1 -1
- package/prompts/replace-guidelines.md +5 -8
- package/prompts/replace-snippet.md +1 -1
- package/prompts/replace.md +1 -1
- package/prompts/undo-last-change-guidelines.md +2 -3
- package/prompts/undo-last-change-snippet.md +1 -1
- package/prompts/undo-last-change.md +1 -1
- package/src/constants.ts +4 -2
- package/src/file-kind.ts +8 -6
- package/src/file-reader.ts +25 -11
- package/src/fs-write.ts +28 -2
- package/src/grep.ts +137 -43
- package/src/hash-store.ts +57 -17
- package/src/hashline/hash.ts +16 -9
- package/src/hashline/index.ts +1 -0
- package/src/hashline/parse.ts +17 -3
- package/src/hashline/resolve.ts +9 -8
- package/src/insert.ts +11 -16
- package/src/payload-contract.ts +102 -0
- package/src/read.ts +32 -11
- package/src/replace-diff.ts +108 -19
- package/src/replace-render.ts +47 -43
- package/src/replace-response.ts +4 -1
- package/src/replace-undo.ts +17 -12
- package/src/replace.ts +44 -106
- package/src/served.ts +26 -5
- package/src/utils.ts +71 -0
- package/src/write-hook.ts +59 -0
- package/src/replace-normalize.ts +0 -13
|
@@ -0,0 +1,59 @@
|
|
|
1
|
+
import type { ExtensionAPI } from "@earendil-works/pi-coding-agent";
|
|
2
|
+
import { HASH_CLASS } from "./hashline/alphabet";
|
|
3
|
+
import { HASH_SEP } from "./hashline/hash";
|
|
4
|
+
import { loadHashStore } from "./hash-store";
|
|
5
|
+
import { getServed } from "./served";
|
|
6
|
+
import { resolveInCwd } from "./fs-write";
|
|
7
|
+
import { abortIf, splitLines, isRec, normalizeFilePath } from "./utils";
|
|
8
|
+
|
|
9
|
+
const HASH_ECHO_RE = new RegExp(`^(${HASH_CLASS})${HASH_SEP}`);
|
|
10
|
+
|
|
11
|
+
function searchEcho(lines: string[], served: ReadonlySet<string>): { line: number; hash: string } | undefined {
|
|
12
|
+
for (let i = 0; i < lines.length; i++) {
|
|
13
|
+
const match = HASH_ECHO_RE.exec(lines[i]!);
|
|
14
|
+
if (match && served.has(match[1]!)) return { line: i + 1, hash: match[1]! };
|
|
15
|
+
}
|
|
16
|
+
return undefined;
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
export function findServedHashEcho(content: string, served: ReadonlySet<string>): { line: number; hash: string } | undefined {
|
|
20
|
+
return searchEcho(splitLines(content), served);
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
export function findEditHashEcho(lines: string[], served: ReadonlySet<string>): { line: number; hash: string } | undefined {
|
|
24
|
+
return searchEcho(lines, served);
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
export async function servedHashEchoDenial(rawPath: string, content: string, cwd: string, signal?: AbortSignal): Promise<string | undefined> {
|
|
28
|
+
abortIf(signal);
|
|
29
|
+
const { resolved } = await resolveInCwd(rawPath, cwd);
|
|
30
|
+
abortIf(signal);
|
|
31
|
+
const store = await loadHashStore();
|
|
32
|
+
const served = getServed(store, resolved);
|
|
33
|
+
if (!served || served.size === 0) return undefined;
|
|
34
|
+
const echo = findServedHashEcho(content, served);
|
|
35
|
+
if (!echo) return undefined;
|
|
36
|
+
return `[E_WRITE_HASH_ECHO] Refused write to ${rawPath}: line ${echo.line} begins with the exact ${echo.hash}${HASH_SEP} anchor served for this file. Remove the copied anchors and retry. Nothing was written.`;
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
export function registerWriteHook(pi: ExtensionAPI): void {
|
|
40
|
+
pi.on("tool_call", async (event, ctx) => {
|
|
41
|
+
if (event.toolName !== "write") return;
|
|
42
|
+
const input = event.input as Record<string, unknown> | undefined;
|
|
43
|
+
if (!input || !isRec(input)) return;
|
|
44
|
+
const normalized = { ...input };
|
|
45
|
+
normalizeFilePath(normalized);
|
|
46
|
+
const rawPath = normalized.path as unknown;
|
|
47
|
+
const content = normalized.content as unknown;
|
|
48
|
+
if (typeof rawPath !== "string" || typeof content !== "string") return;
|
|
49
|
+
const signal = ctx.signal;
|
|
50
|
+
try {
|
|
51
|
+
const reason = await servedHashEchoDenial(rawPath, content, ctx.cwd, signal);
|
|
52
|
+
if (reason !== undefined) return { block: true, reason };
|
|
53
|
+
} catch (error) {
|
|
54
|
+
if (signal?.aborted) throw error;
|
|
55
|
+
console.error("write hook failed:", error);
|
|
56
|
+
}
|
|
57
|
+
return;
|
|
58
|
+
});
|
|
59
|
+
}
|
package/src/replace-normalize.ts
DELETED
|
@@ -1,13 +0,0 @@
|
|
|
1
|
-
import { isRec, normalizeFilePath } from "./utils";
|
|
2
|
-
|
|
3
|
-
export function normReq(input: unknown): unknown {
|
|
4
|
-
if (!isRec(input)) {
|
|
5
|
-
return input;
|
|
6
|
-
}
|
|
7
|
-
|
|
8
|
-
const record: Record<string, unknown> = { ...input };
|
|
9
|
-
|
|
10
|
-
normalizeFilePath(record);
|
|
11
|
-
|
|
12
|
-
return record;
|
|
13
|
-
}
|