@leolastforce/chalice-hashline-edit 0.0.0-stage → 4.4.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 +153 -2
- package/index.ts +282 -0
- package/package.json +79 -4
- package/prompts/find-guidelines.md +2 -0
- package/prompts/find-snippet.md +1 -0
- package/prompts/find.md +1 -0
- package/prompts/grep-guidelines.md +2 -0
- package/prompts/grep-snippet.md +1 -0
- package/prompts/grep.md +1 -0
- package/prompts/insert-guidelines.md +1 -0
- package/prompts/insert-snippet.md +1 -0
- package/prompts/insert.md +1 -0
- package/prompts/read-guidelines.md +2 -0
- package/prompts/read-snippet.md +1 -0
- package/prompts/read.md +1 -0
- package/prompts/replace-guidelines.md +4 -0
- package/prompts/replace-snippet.md +1 -0
- package/prompts/replace.md +1 -0
- package/prompts/undo-last-change-guidelines.md +1 -0
- package/prompts/undo-last-change-snippet.md +1 -0
- package/prompts/undo-last-change.md +1 -0
- package/src/anchor-registry.ts +977 -0
- package/src/auto-read-all-state.ts +27 -0
- package/src/auto-read-all.ts +477 -0
- package/src/batch.ts +767 -0
- package/src/commit.ts +137 -0
- package/src/config-ui.ts +212 -0
- package/src/config.ts +235 -0
- package/src/constants.ts +26 -0
- package/src/edit-common.ts +206 -0
- package/src/file-kind.ts +191 -0
- package/src/file-reader.ts +135 -0
- package/src/find.ts +244 -0
- package/src/fs-write.ts +231 -0
- package/src/glob.ts +186 -0
- package/src/grep.ts +690 -0
- package/src/hash-store/cache.ts +18 -0
- package/src/hash-store/retry.ts +44 -0
- package/src/hash-store/validation.ts +62 -0
- package/src/hash-store.ts +609 -0
- package/src/hashline/alphabet.ts +21 -0
- package/src/hashline/anchor-table.json +1 -0
- package/src/hashline/apply.ts +297 -0
- package/src/hashline/hash.ts +119 -0
- package/src/hashline/hasher.ts +47 -0
- package/src/hashline/index.ts +50 -0
- package/src/hashline/parse.ts +84 -0
- package/src/hashline/resolve.ts +373 -0
- package/src/insert.ts +258 -0
- package/src/normalize.ts +27 -0
- package/src/paths.ts +56 -0
- package/src/payload-contract.ts +133 -0
- package/src/prompts.ts +13 -0
- package/src/read.ts +286 -0
- package/src/replace-diff.ts +610 -0
- package/src/replace-render.ts +390 -0
- package/src/replace-response.ts +166 -0
- package/src/replace-undo.ts +270 -0
- package/src/replace.ts +318 -0
- package/src/served.ts +36 -0
- package/src/utils.ts +387 -0
- package/src/validation.ts +45 -0
- package/src/write-hook.ts +53 -0
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
export interface SnapshotCacheEntry {
|
|
2
|
+
checksum: string;
|
|
3
|
+
lineCount: number;
|
|
4
|
+
hashes: string[];
|
|
5
|
+
}
|
|
6
|
+
|
|
7
|
+
export const SNAPSHOT_CACHE_LIMIT = 256;
|
|
8
|
+
|
|
9
|
+
export const snapshotCache = new Map<string, SnapshotCacheEntry>();
|
|
10
|
+
|
|
11
|
+
export function cacheSnapshot(path: string, checksum: string, lineCount: number, hashes: string[]): void {
|
|
12
|
+
snapshotCache.delete(path);
|
|
13
|
+
snapshotCache.set(path, { checksum, lineCount, hashes: hashes.slice() });
|
|
14
|
+
if (snapshotCache.size > SNAPSHOT_CACHE_LIMIT) {
|
|
15
|
+
const oldest = snapshotCache.keys().next().value;
|
|
16
|
+
if (oldest !== undefined) snapshotCache.delete(oldest);
|
|
17
|
+
}
|
|
18
|
+
}
|
|
@@ -0,0 +1,44 @@
|
|
|
1
|
+
import { isBusyError } from "./validation";
|
|
2
|
+
|
|
3
|
+
const sleepSab = new Int32Array(new SharedArrayBuffer(4));
|
|
4
|
+
|
|
5
|
+
function sleepSync(ms: number): void {
|
|
6
|
+
Atomics.wait(sleepSab, 0, 0, ms);
|
|
7
|
+
}
|
|
8
|
+
|
|
9
|
+
const BUSY_RETRIES = 3;
|
|
10
|
+
const BUSY_RETRY_DELAY_MS = 50;
|
|
11
|
+
|
|
12
|
+
export function withBusyRetry<T>(fn: () => T): T {
|
|
13
|
+
let lastError: unknown;
|
|
14
|
+
for (let attempt = 0; attempt <= BUSY_RETRIES; attempt++) {
|
|
15
|
+
try {
|
|
16
|
+
return fn();
|
|
17
|
+
} catch (error) {
|
|
18
|
+
lastError = error;
|
|
19
|
+
if (!isBusyError(error) || attempt === BUSY_RETRIES) throw error;
|
|
20
|
+
sleepSync(BUSY_RETRY_DELAY_MS * (1 << attempt));
|
|
21
|
+
}
|
|
22
|
+
}
|
|
23
|
+
throw lastError;
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
export async function withBusyRetryAsync<T>(fn: () => T): Promise<T> {
|
|
27
|
+
let lastError: unknown;
|
|
28
|
+
for (let attempt = 0; attempt <= BUSY_RETRIES; attempt++) {
|
|
29
|
+
try {
|
|
30
|
+
return fn();
|
|
31
|
+
} catch (error) {
|
|
32
|
+
lastError = error;
|
|
33
|
+
if (!isBusyError(error) || attempt === BUSY_RETRIES) throw error;
|
|
34
|
+
await new Promise<void>((r) => setTimeout(r, BUSY_RETRY_DELAY_MS * (1 << attempt)));
|
|
35
|
+
}
|
|
36
|
+
}
|
|
37
|
+
throw lastError;
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
export function retriedWrite(stmt: { run(...params: (string | number | null)[]): unknown }): (...params: (string | number | null)[]) => void {
|
|
41
|
+
return (...params) => {
|
|
42
|
+
withBusyRetry(() => { stmt.run(...params); });
|
|
43
|
+
};
|
|
44
|
+
}
|
|
@@ -0,0 +1,62 @@
|
|
|
1
|
+
import { HASH_RE } from "../hashline/alphabet";
|
|
2
|
+
|
|
3
|
+
export function isValidHashList(value: unknown): value is string[] {
|
|
4
|
+
if (!Array.isArray(value)) return false;
|
|
5
|
+
for (const hash of value) {
|
|
6
|
+
if (typeof hash !== "string" || !HASH_RE.test(hash)) return false;
|
|
7
|
+
}
|
|
8
|
+
if (new Set(value).size !== value.length) return false;
|
|
9
|
+
return true;
|
|
10
|
+
}
|
|
11
|
+
|
|
12
|
+
export function parseHashList(raw: string, onInvalid: () => void, context?: string): string[] | undefined {
|
|
13
|
+
let parsed: unknown;
|
|
14
|
+
try {
|
|
15
|
+
parsed = JSON.parse(raw);
|
|
16
|
+
} catch (error) {
|
|
17
|
+
console.error(`[parseHashList]${context ? ` ${context}:` : ""} failed to parse stored hashes JSON:`, error);
|
|
18
|
+
onInvalid();
|
|
19
|
+
return undefined;
|
|
20
|
+
}
|
|
21
|
+
if (!isValidHashList(parsed)) {
|
|
22
|
+
console.error(`[parseHashList]${context ? ` ${context}:` : ""} stored hashes did not pass validation:`, Array.isArray(parsed) ? `length=${parsed.length} sample=${JSON.stringify(parsed.slice(0, 3))}` : (() => { try { return JSON.stringify(parsed)?.slice(0, 500) ?? String(parsed).slice(0, 500); } catch { return String(parsed).slice(0, 500); } })());
|
|
23
|
+
onInvalid();
|
|
24
|
+
return undefined;
|
|
25
|
+
}
|
|
26
|
+
return parsed;
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
export function parseStoredHashes(row: Record<string, unknown> | undefined, onInvalid: () => void): string[] | undefined {
|
|
30
|
+
if (!row) return undefined;
|
|
31
|
+
return parseHashList(row.hashes as string, onInvalid);
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
export function isValidSnapshot(value: unknown): value is { content: string; hashes: string[] } {
|
|
35
|
+
if (typeof value !== "object" || value === null) return false;
|
|
36
|
+
const v = value as Record<string, unknown>;
|
|
37
|
+
if (typeof v.content !== "string") return false;
|
|
38
|
+
return isValidHashList(v.hashes);
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
export function isCorruptionError(error: unknown): boolean {
|
|
42
|
+
if (error && typeof error === "object") {
|
|
43
|
+
const errcode = (error as { errcode?: unknown }).errcode;
|
|
44
|
+
if (typeof errcode === "number") {
|
|
45
|
+
return errcode === 11 || errcode === 24 || errcode === 26;
|
|
46
|
+
}
|
|
47
|
+
const code = (error as { code?: unknown }).code;
|
|
48
|
+
if (typeof code === "string" && /NOTADB|CORRUPT/.test(code)) return true;
|
|
49
|
+
}
|
|
50
|
+
return (
|
|
51
|
+
error instanceof Error &&
|
|
52
|
+
/corrupt|not a database|malformed|database disk image/i.test(error.message)
|
|
53
|
+
);
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
export function isBusyError(error: unknown): boolean {
|
|
57
|
+
if (error && typeof error === "object") {
|
|
58
|
+
const errcode = (error as { errcode?: unknown }).errcode;
|
|
59
|
+
if (typeof errcode === "number") return errcode === 5 || errcode === 6;
|
|
60
|
+
}
|
|
61
|
+
return error instanceof Error && /busy|locked/i.test(error.message);
|
|
62
|
+
}
|