dsh-better-edit 0.1.9 → 0.2.1
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/CHANGELOG.md +32 -0
- package/README.md +168 -52
- package/README.zh.md +122 -46
- package/assets/logo.svg +183 -18
- package/lib/contract.js +95 -0
- package/lib/drift.js +1 -1
- package/lib/edit-diff.js +2 -2
- package/lib/edit-engine.js +456 -0
- package/lib/edit-pipeline.js +41 -116
- package/lib/file-reader.js +1 -2
- package/lib/fs-bridge.js +2 -2
- package/lib/fs-write.js +3 -46
- package/lib/guidance.js +282 -0
- package/lib/hash-store.js +246 -13
- package/lib/hashline/apply.js +2 -2
- package/lib/hashline/hash.js +16 -206
- package/lib/hashline/index.js +4 -3
- package/lib/hashline/parse.js +1 -1
- package/lib/hashline/pure.js +213 -0
- package/lib/hashline/resolve.js +1 -5
- package/lib/hashline/served.js +2 -2
- package/lib/index.js +48 -32
- package/lib/paths.js +55 -1
- package/lib/prompts.js +53 -49
- package/lib/read-and-serve.js +46 -0
- package/lib/schema.js +1 -1
- package/lib/served-store.js +144 -183
- package/lib/tool-batch-edit.js +28 -280
- package/lib/tool-edit.js +37 -32
- package/lib/tool-read.js +8 -31
- package/lib/tool-undo.js +4 -13
- package/lib/types/contract.d.ts +39 -0
- package/lib/types/edit-engine.d.ts +179 -0
- package/lib/types/edit-pipeline.d.ts +10 -17
- package/lib/types/fs-write.d.ts +0 -1
- package/lib/types/guidance.d.ts +98 -0
- package/lib/types/hash-store.d.ts +73 -25
- package/lib/types/hashline/hash.d.ts +8 -13
- package/lib/types/hashline/index.d.ts +4 -3
- package/lib/types/hashline/pure.d.ts +26 -0
- package/lib/types/hashline/resolve.d.ts +0 -1
- package/lib/types/index.d.ts +4 -0
- package/lib/types/paths.d.ts +9 -0
- package/lib/types/prompts.d.ts +19 -9
- package/lib/types/read-and-serve.d.ts +39 -0
- package/lib/types/schema.d.ts +2 -16
- package/lib/types/served-store.d.ts +63 -32
- package/lib/types/tool-batch-edit.d.ts +4 -1
- package/lib/types/tool-undo.d.ts +1 -1
- package/lib/undo-edit.js +16 -1
- package/lib/write-hook.js +30 -50
- package/package.json +5 -3
- package/lib/served-state.js +0 -34
- package/lib/snapshot-store.js +0 -108
- package/lib/types/served-state.d.ts +0 -3
- package/lib/types/snapshot-store.d.ts +0 -14
- package/lib/types/undo-store.d.ts +0 -14
- package/lib/undo-store.js +0 -43
package/lib/fs-write.js
CHANGED
|
@@ -1,51 +1,8 @@
|
|
|
1
1
|
import { randomUUID } from "node:crypto";
|
|
2
|
-
import {
|
|
3
|
-
import { dirname, join
|
|
2
|
+
import { mkdir, open, readdir, rename, rm, stat, writeFile, } from "fs/promises";
|
|
3
|
+
import { dirname, join } from "node:path";
|
|
4
4
|
import { errCode } from "./utils.js";
|
|
5
|
-
|
|
6
|
-
const absolutePath = resolve(path);
|
|
7
|
-
const { root } = parse(absolutePath);
|
|
8
|
-
const parts = absolutePath
|
|
9
|
-
.slice(root.length)
|
|
10
|
-
.split(sep)
|
|
11
|
-
.filter((part) => part.length > 0);
|
|
12
|
-
const visitedSymlinks = new Set();
|
|
13
|
-
async function resParts(currentPath, remainingParts) {
|
|
14
|
-
if (remainingParts.length === 0) {
|
|
15
|
-
return currentPath;
|
|
16
|
-
}
|
|
17
|
-
const [nextPart, ...tail] = remainingParts;
|
|
18
|
-
const candidatePath = join(currentPath, nextPart);
|
|
19
|
-
try {
|
|
20
|
-
const candidateStats = await lstat(candidatePath);
|
|
21
|
-
if (!candidateStats.isSymbolicLink()) {
|
|
22
|
-
return resParts(candidatePath, tail);
|
|
23
|
-
}
|
|
24
|
-
if (visitedSymlinks.has(candidatePath)) {
|
|
25
|
-
const error = new Error(`Too many symbolic links while resolving ${path}`);
|
|
26
|
-
error.code = "ELOOP";
|
|
27
|
-
throw error;
|
|
28
|
-
}
|
|
29
|
-
visitedSymlinks.add(candidatePath);
|
|
30
|
-
const linkTargetPath = resolve(dirname(candidatePath), await readlink(candidatePath));
|
|
31
|
-
const targetParts = linkTargetPath
|
|
32
|
-
.slice(parse(linkTargetPath).root.length)
|
|
33
|
-
.split(sep)
|
|
34
|
-
.filter((part) => part.length > 0);
|
|
35
|
-
return resParts(parse(linkTargetPath).root, [
|
|
36
|
-
...targetParts,
|
|
37
|
-
...tail,
|
|
38
|
-
]);
|
|
39
|
-
}
|
|
40
|
-
catch (error) {
|
|
41
|
-
if (errCode(error) === "ENOENT") {
|
|
42
|
-
return join(candidatePath, ...tail);
|
|
43
|
-
}
|
|
44
|
-
throw error;
|
|
45
|
-
}
|
|
46
|
-
}
|
|
47
|
-
return resParts(root, parts);
|
|
48
|
-
}
|
|
5
|
+
import { resolveTarget } from "./paths.js";
|
|
49
6
|
const TEMP_PREFIX = ".tmp-";
|
|
50
7
|
const TEMP_UUID_RE = /^\.tmp-[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}$/;
|
|
51
8
|
const STALE_TEMP_MS = 60 * 60 * 1000;
|
package/lib/guidance.js
ADDED
|
@@ -0,0 +1,282 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Per-preset guidance overrides for the hashline tool sections.
|
|
3
|
+
*
|
|
4
|
+
* Each of the four `tool:*` prompt sections the plugin registers has a
|
|
5
|
+
* compiled default rendered from `prompts.ts` (byte-identical to the inline
|
|
6
|
+
* rendering `src/index.ts` uses today). A user can override any section's text
|
|
7
|
+
* and `order` with a plain markdown file keyed by agent preset: the override
|
|
8
|
+
* files live in the plugin's shared home (`$DSH_HOME/plugins/dsh-better-edit`,
|
|
9
|
+
* never the workspace store) and are resolved per section as
|
|
10
|
+
* `<preset>/<section>.md` → compiled default.
|
|
11
|
+
*
|
|
12
|
+
* An override file is pure prose unless it opens with a valid YAML front-matter
|
|
13
|
+
* fence carrying only an `order` key (`---` / `order: N` / `---`); anything
|
|
14
|
+
* else — a missing closing fence, a non-integer `order`, an unknown key —
|
|
15
|
+
* degrades the WHOLE file to prose so the mistake stays visible in the
|
|
16
|
+
* rendered section.
|
|
17
|
+
*
|
|
18
|
+
* Resolution is pure: no cordis services, no harness wiring. The installer
|
|
19
|
+
* ticket reads the resolved sections once per agent at session-start;
|
|
20
|
+
* nothing here watches or caches files.
|
|
21
|
+
* @module dsh-better-edit/guidance
|
|
22
|
+
*/
|
|
23
|
+
import { mkdir, readdir, readFile, writeFile } from "node:fs/promises";
|
|
24
|
+
import { join } from "node:path";
|
|
25
|
+
import { BATCH_EDIT_GUIDANCE, EDIT_GUIDANCE, READ_GUIDANCE, UNDO_GUIDANCE, } from "./prompts.js";
|
|
26
|
+
import { errCode } from "./utils.js";
|
|
27
|
+
/** The presets shipped by the harness, each seeded with editable guidance. */
|
|
28
|
+
export const DEFAULT_PRESETS = [
|
|
29
|
+
"standard",
|
|
30
|
+
"code",
|
|
31
|
+
"minimal",
|
|
32
|
+
"cordis",
|
|
33
|
+
];
|
|
34
|
+
/**
|
|
35
|
+
* Render one tool's guidance as its intro line, a blank line, then bullets.
|
|
36
|
+
* Uniform across the four sections: no tool-schema description is duplicated
|
|
37
|
+
* (that already reaches the model through the tool catalog).
|
|
38
|
+
*/
|
|
39
|
+
function guidanceText(g) {
|
|
40
|
+
return [g.intro, "", bulletLines(g.lines)].join("\n");
|
|
41
|
+
}
|
|
42
|
+
function bulletLines(lines) {
|
|
43
|
+
return lines.map((line) => `- ${line}`).join("\n");
|
|
44
|
+
}
|
|
45
|
+
/** The four sections, in default-order sequence. */
|
|
46
|
+
export const GUIDANCE_SECTIONS = [
|
|
47
|
+
{
|
|
48
|
+
name: "tool:read",
|
|
49
|
+
file: "read.md",
|
|
50
|
+
defaultOrder: 130,
|
|
51
|
+
renderDefault: () => guidanceText(READ_GUIDANCE),
|
|
52
|
+
},
|
|
53
|
+
{
|
|
54
|
+
name: "tool:edit",
|
|
55
|
+
file: "edit.md",
|
|
56
|
+
defaultOrder: 131,
|
|
57
|
+
renderDefault: () => guidanceText(EDIT_GUIDANCE),
|
|
58
|
+
},
|
|
59
|
+
{
|
|
60
|
+
name: "tool:batch_edit",
|
|
61
|
+
file: "batch_edit.md",
|
|
62
|
+
defaultOrder: 132,
|
|
63
|
+
renderDefault: () => guidanceText(BATCH_EDIT_GUIDANCE),
|
|
64
|
+
},
|
|
65
|
+
{
|
|
66
|
+
name: "tool:undo_last_edit",
|
|
67
|
+
file: "undo_last_edit.md",
|
|
68
|
+
defaultOrder: 133,
|
|
69
|
+
renderDefault: () => guidanceText(UNDO_GUIDANCE),
|
|
70
|
+
},
|
|
71
|
+
];
|
|
72
|
+
const SECTION_BY_NAME = new Map(GUIDANCE_SECTIONS.map((section) => [section.name, section]));
|
|
73
|
+
/** Render the compiled default text for one section. */
|
|
74
|
+
export function renderSectionDefault(name) {
|
|
75
|
+
const section = SECTION_BY_NAME.get(name);
|
|
76
|
+
if (!section)
|
|
77
|
+
throw new Error(`unknown guidance section: ${name}`);
|
|
78
|
+
return section.renderDefault();
|
|
79
|
+
}
|
|
80
|
+
function stripCR(line) {
|
|
81
|
+
return line.endsWith("\r") ? line.slice(0, -1) : line;
|
|
82
|
+
}
|
|
83
|
+
/**
|
|
84
|
+
* Parse an override file. Only a leading `---` fence whose lines are empty or
|
|
85
|
+
* `order: <integer>` is accepted; every other shape (no fence, no closing
|
|
86
|
+
* fence, unknown key, non-integer value) yields the whole file as prose.
|
|
87
|
+
*/
|
|
88
|
+
export function parseSectionFile(content) {
|
|
89
|
+
const lines = content.split("\n");
|
|
90
|
+
if (lines.length < 3 || stripCR(lines[0]) !== "---") {
|
|
91
|
+
return { text: content };
|
|
92
|
+
}
|
|
93
|
+
const close = lines.findIndex((line, index) => index > 0 && stripCR(line) === "---");
|
|
94
|
+
if (close < 0)
|
|
95
|
+
return { text: content };
|
|
96
|
+
let order;
|
|
97
|
+
for (let index = 1; index < close; index++) {
|
|
98
|
+
const line = stripCR(lines[index]);
|
|
99
|
+
if (line.trim() === "")
|
|
100
|
+
continue;
|
|
101
|
+
const match = /^order:\s*(-?\d+)\s*$/.exec(line);
|
|
102
|
+
if (!match)
|
|
103
|
+
return { text: content };
|
|
104
|
+
order = Number.parseInt(match[1], 10);
|
|
105
|
+
}
|
|
106
|
+
// Front-matter body: strip leading blank lines after the closing fence so
|
|
107
|
+
// `---\n…\n---\n\nbody` and `---\n…\n---\nbody` parse identically (the
|
|
108
|
+
// materialized preset files carry a blank line after the fence).
|
|
109
|
+
const body = lines.slice(close + 1);
|
|
110
|
+
let bodyStart = 0;
|
|
111
|
+
while (bodyStart < body.length && body[bodyStart].trim() === "")
|
|
112
|
+
bodyStart++;
|
|
113
|
+
return { order, text: body.slice(bodyStart).join("\n") };
|
|
114
|
+
}
|
|
115
|
+
function overrideCandidates(file, options) {
|
|
116
|
+
const candidates = [];
|
|
117
|
+
if (options.presetId !== undefined) {
|
|
118
|
+
candidates.push(join(options.homeDir, options.presetId, file));
|
|
119
|
+
}
|
|
120
|
+
return candidates;
|
|
121
|
+
}
|
|
122
|
+
/**
|
|
123
|
+
* Resolve one section's guidance: the first override file that exists wins,
|
|
124
|
+
* falling back to the compiled default. A missing file (ENOENT) advances the
|
|
125
|
+
* chain; any other read error propagates.
|
|
126
|
+
*/
|
|
127
|
+
export async function resolveSection(name, options) {
|
|
128
|
+
const section = SECTION_BY_NAME.get(name);
|
|
129
|
+
if (!section)
|
|
130
|
+
throw new Error(`unknown guidance section: ${name}`);
|
|
131
|
+
for (const candidate of overrideCandidates(section.file, options)) {
|
|
132
|
+
const content = await readFile(candidate, "utf-8").catch((error) => {
|
|
133
|
+
if (errCode(error) === "ENOENT")
|
|
134
|
+
return undefined;
|
|
135
|
+
throw error;
|
|
136
|
+
});
|
|
137
|
+
if (content === undefined)
|
|
138
|
+
continue;
|
|
139
|
+
const parsed = parseSectionFile(content);
|
|
140
|
+
return { order: parsed.order ?? section.defaultOrder, text: parsed.text };
|
|
141
|
+
}
|
|
142
|
+
return { order: section.defaultOrder, text: section.renderDefault() };
|
|
143
|
+
}
|
|
144
|
+
/**
|
|
145
|
+
* Resolve all four sections for a preset. `presetId === undefined` skips the
|
|
146
|
+
* `<preset>/` layer and resolves straight to the compiled defaults.
|
|
147
|
+
*/
|
|
148
|
+
export async function composeSections(presetId, homeDir) {
|
|
149
|
+
return Promise.all(GUIDANCE_SECTIONS.map(async (section) => {
|
|
150
|
+
const { order, text } = await resolveSection(section.name, {
|
|
151
|
+
presetId,
|
|
152
|
+
homeDir,
|
|
153
|
+
});
|
|
154
|
+
return { name: section.name, order, text };
|
|
155
|
+
}));
|
|
156
|
+
}
|
|
157
|
+
/**
|
|
158
|
+
* The root README: the per-preset customization convention users see in the
|
|
159
|
+
* plugin home.
|
|
160
|
+
*/
|
|
161
|
+
export const GUIDANCE_HOME_README = `# dsh-better-edit guidance
|
|
162
|
+
|
|
163
|
+
Each agent preset has its own guidance directory here: \`<preset>/<section>.md\`.
|
|
164
|
+
On first boot the plugin seeds the shipped presets (\`standard\`, \`code\`,
|
|
165
|
+
\`minimal\`, \`cordis\`) with the compiled defaults; existing files are never
|
|
166
|
+
overwritten, so your edits survive.
|
|
167
|
+
|
|
168
|
+
Each file is one tool section:
|
|
169
|
+
|
|
170
|
+
- \`read.md\` -> \`tool:read\`
|
|
171
|
+
- \`edit.md\` -> \`tool:edit\`
|
|
172
|
+
- \`batch_edit.md\` -> \`tool:batch_edit\`
|
|
173
|
+
- \`undo_last_edit.md\` -> \`tool:undo_last_edit\`
|
|
174
|
+
|
|
175
|
+
## Customize
|
|
176
|
+
|
|
177
|
+
Edit the \`<section>.md\` file for the preset and section you want to change —
|
|
178
|
+
the file body IS the text the model reads for that section. Files are read once
|
|
179
|
+
per agent at session-start, so edits apply to new sessions.
|
|
180
|
+
|
|
181
|
+
An optional YAML front-matter fence places the section at a custom position in
|
|
182
|
+
the assembled system prompt:
|
|
183
|
+
|
|
184
|
+
---
|
|
185
|
+
order: 150
|
|
186
|
+
---
|
|
187
|
+
|
|
188
|
+
<section text>
|
|
189
|
+
|
|
190
|
+
A file without front-matter keeps the default order. A malformed fence (a
|
|
191
|
+
missing closing \`---\`, a non-integer \`order\`, an unknown key) makes the whole
|
|
192
|
+
file plain prose.
|
|
193
|
+
|
|
194
|
+
## Fallback
|
|
195
|
+
|
|
196
|
+
A preset with no directory here, or a missing section file, falls back to the
|
|
197
|
+
compiled defaults in the plugin bundle. To customize a preset that has no
|
|
198
|
+
seeded directory, copy a seeded one to its name.
|
|
199
|
+
`;
|
|
200
|
+
export const GUIDANCE_HOME_README_ZH = `# dsh-better-edit 指引
|
|
201
|
+
|
|
202
|
+
每个 agent preset 在这里都有自己的指引目录:\`<preset>/<section>.md\`。首次启动时
|
|
203
|
+
插件会为随附的 preset(\`standard\`、\`code\`、\`minimal\`、\`cordis\`)写入编译内置的
|
|
204
|
+
默认内容;已有文件绝不被覆盖,因此你的编辑会保留。
|
|
205
|
+
|
|
206
|
+
每个文件对应一个工具片段:
|
|
207
|
+
|
|
208
|
+
- \`read.md\` -> \`tool:read\`
|
|
209
|
+
- \`edit.md\` -> \`tool:edit\`
|
|
210
|
+
- \`batch_edit.md\` -> \`tool:batch_edit\`
|
|
211
|
+
- \`undo_last_edit.md\` -> \`tool:undo_last_edit\`
|
|
212
|
+
|
|
213
|
+
## 自定义
|
|
214
|
+
|
|
215
|
+
编辑你想修改的 preset 与片段的 \`<section>.md\` 文件即可——文件正文就是该片段呈
|
|
216
|
+
现给模型的文本。文件在 agent 的 session-start 时读取一次,因此修改只影响新会话。
|
|
217
|
+
|
|
218
|
+
可以用可选的 YAML front-matter 栅栏把片段放到组装后系统提示中的自定义位置:
|
|
219
|
+
|
|
220
|
+
---
|
|
221
|
+
order: 150
|
|
222
|
+
---
|
|
223
|
+
|
|
224
|
+
<片段文本>
|
|
225
|
+
|
|
226
|
+
没有 front-matter 的文件保持默认顺序。格式错误的栅栏(缺少收尾 \`---\`、非整数
|
|
227
|
+
\`order\`、未知键)会让整个文件退化为纯文本。
|
|
228
|
+
|
|
229
|
+
## 回退
|
|
230
|
+
|
|
231
|
+
这里没有对应目录的 preset、或缺失某个片段文件时,会回退到插件包内的编译内置默认
|
|
232
|
+
值。要自定义没有种子目录的 preset,把一个种子目录复制成它的名字即可。
|
|
233
|
+
`;
|
|
234
|
+
/**
|
|
235
|
+
* Materialize per-preset guidance directories in the plugin home.
|
|
236
|
+
*
|
|
237
|
+
* For each of \`DEFAULT_PRESETS\` creates \`<preset>/{read,edit,batch_edit,
|
|
238
|
+
* undo_last_edit}.md\` rendered from the compiled defaults (with order
|
|
239
|
+
* front-matter), plus a root \`README.md\` documenting the convention.
|
|
240
|
+
* Idempotent: existing files are never rewritten (a user-edited file survives
|
|
241
|
+
* repeated calls) and missing directories are created on demand. Each file is
|
|
242
|
+
* written exclusively, so two concurrent first runs race safely.
|
|
243
|
+
*/
|
|
244
|
+
export async function ensurePresetGuidance(homeDir) {
|
|
245
|
+
await mkdir(homeDir, { recursive: true });
|
|
246
|
+
await Promise.all(DEFAULT_PRESETS.map(async (preset) => {
|
|
247
|
+
const dir = join(homeDir, preset);
|
|
248
|
+
await mkdir(dir, { recursive: true });
|
|
249
|
+
const existing = new Set(await readdir(dir));
|
|
250
|
+
await Promise.all(GUIDANCE_SECTIONS.map(async (section) => {
|
|
251
|
+
if (existing.has(section.file))
|
|
252
|
+
return;
|
|
253
|
+
const content = `---\norder: ${section.defaultOrder}\n---\n\n${section.renderDefault()}`;
|
|
254
|
+
await writeFile(join(dir, section.file), content, {
|
|
255
|
+
encoding: "utf-8",
|
|
256
|
+
flag: "wx",
|
|
257
|
+
}).catch((error) => {
|
|
258
|
+
// A concurrent writer landed first; never clobber it.
|
|
259
|
+
if (errCode(error) === "EEXIST")
|
|
260
|
+
return;
|
|
261
|
+
throw error;
|
|
262
|
+
});
|
|
263
|
+
}));
|
|
264
|
+
}));
|
|
265
|
+
const homeFiles = new Set(await readdir(homeDir));
|
|
266
|
+
const readmes = [
|
|
267
|
+
["README.md", GUIDANCE_HOME_README],
|
|
268
|
+
["README.zh.md", GUIDANCE_HOME_README_ZH],
|
|
269
|
+
];
|
|
270
|
+
await Promise.all(readmes.map(async ([file, content]) => {
|
|
271
|
+
if (homeFiles.has(file))
|
|
272
|
+
return;
|
|
273
|
+
await writeFile(join(homeDir, file), content, {
|
|
274
|
+
encoding: "utf-8",
|
|
275
|
+
flag: "wx",
|
|
276
|
+
}).catch((error) => {
|
|
277
|
+
if (errCode(error) === "EEXIST")
|
|
278
|
+
return;
|
|
279
|
+
throw error;
|
|
280
|
+
});
|
|
281
|
+
}));
|
|
282
|
+
}
|
package/lib/hash-store.js
CHANGED
|
@@ -1,13 +1,58 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The hash store — ONE deep persistence module for the hashline domain.
|
|
3
|
+
*
|
|
4
|
+
* Owns the sqlite db, the schema and migrations, corruption quarantine,
|
|
5
|
+
* busy-retry, WAL, the legacy-JSON migration, AND the three narrow row APIs
|
|
6
|
+
* the rest of the plugin needs: hash snapshots, undo entries, and served
|
|
7
|
+
* rows. The prepared statements are a private implementation detail — callers
|
|
8
|
+
* use domain methods, never SQL.
|
|
9
|
+
*
|
|
10
|
+
* Corrupt-row handling (parse the JSON column → validate against the hash
|
|
11
|
+
* alphabet → delete the corrupt row) lives here, once, for every row family.
|
|
12
|
+
* Cross-table cleanup (pruneMissing) lives here too — a sibling module never
|
|
13
|
+
* reaches into another family's rows.
|
|
14
|
+
* @module dsh-better-edit/hash-store
|
|
15
|
+
*/
|
|
1
16
|
import { existsSync } from "node:fs";
|
|
2
|
-
import { readFile, rename, mkdir } from "node:fs/promises";
|
|
17
|
+
import { readFile, rename, mkdir, stat } from "node:fs/promises";
|
|
3
18
|
import { dirname, join } from "node:path";
|
|
4
19
|
import { DatabaseSync } from "node:sqlite";
|
|
5
20
|
import { hashStorePath } from "./paths.js";
|
|
6
21
|
import { workspaceCwd } from "./workspace.js";
|
|
7
22
|
import { errCode, splitLines } from "./utils.js";
|
|
8
23
|
import { initHasher, contentChecksum } from "./hashline/hasher.js";
|
|
24
|
+
import { HASH_RE } from "./hashline/alphabet.js";
|
|
9
25
|
import { HASH_STORE_VERSION, HASH_STORE_BUSY_TIMEOUT, SERVED_TTL_MS } from "./constants.js";
|
|
10
|
-
|
|
26
|
+
export function isValidHashList(value) {
|
|
27
|
+
if (!Array.isArray(value))
|
|
28
|
+
return false;
|
|
29
|
+
for (const hash of value) {
|
|
30
|
+
if (typeof hash !== "string" || !HASH_RE.test(hash))
|
|
31
|
+
return false;
|
|
32
|
+
}
|
|
33
|
+
return true;
|
|
34
|
+
}
|
|
35
|
+
export function isValidSnapshot(value) {
|
|
36
|
+
if (typeof value !== "object" || value === null)
|
|
37
|
+
return false;
|
|
38
|
+
const v = value;
|
|
39
|
+
if (typeof v.content !== "string")
|
|
40
|
+
return false;
|
|
41
|
+
return isValidHashList(v.hashes);
|
|
42
|
+
}
|
|
43
|
+
/** A served-row array: per-position hash, or null for never-served slots. */
|
|
44
|
+
export function isValidServedList(value) {
|
|
45
|
+
if (!Array.isArray(value))
|
|
46
|
+
return false;
|
|
47
|
+
for (const entry of value) {
|
|
48
|
+
if (entry === null)
|
|
49
|
+
continue;
|
|
50
|
+
if (typeof entry !== "string" || !HASH_RE.test(entry))
|
|
51
|
+
return false;
|
|
52
|
+
}
|
|
53
|
+
return true;
|
|
54
|
+
}
|
|
55
|
+
// ---- db plumbing (private) --------------------------------------------------
|
|
11
56
|
export function isCorruptionError(error) {
|
|
12
57
|
if (error && typeof error === "object") {
|
|
13
58
|
const errcode = error.errcode;
|
|
@@ -68,7 +113,9 @@ function openDb(storePath) {
|
|
|
68
113
|
try {
|
|
69
114
|
db.close();
|
|
70
115
|
}
|
|
71
|
-
catch {
|
|
116
|
+
catch {
|
|
117
|
+
// best-effort close when the store build fails
|
|
118
|
+
}
|
|
72
119
|
throw error;
|
|
73
120
|
}
|
|
74
121
|
}
|
|
@@ -203,6 +250,157 @@ function buildStore(db) {
|
|
|
203
250
|
};
|
|
204
251
|
return { db, stmts };
|
|
205
252
|
}
|
|
253
|
+
/** Wire the domain methods over the prepared statements. */
|
|
254
|
+
function makeDomainStore(stmts) {
|
|
255
|
+
return {
|
|
256
|
+
engine: "node:sqlite",
|
|
257
|
+
getSnapshot(path, content, deleteCorrupt = true) {
|
|
258
|
+
const checksum = contentChecksum(content);
|
|
259
|
+
const lineCount = splitLines(content).length;
|
|
260
|
+
const row = stmts.get(path, checksum, lineCount);
|
|
261
|
+
if (!row)
|
|
262
|
+
return undefined;
|
|
263
|
+
try {
|
|
264
|
+
const parsed = JSON.parse(row.hashes);
|
|
265
|
+
if (isValidHashList(parsed))
|
|
266
|
+
return parsed;
|
|
267
|
+
if (deleteCorrupt)
|
|
268
|
+
stmts.deleteOne(path);
|
|
269
|
+
return undefined;
|
|
270
|
+
}
|
|
271
|
+
catch {
|
|
272
|
+
if (deleteCorrupt)
|
|
273
|
+
stmts.deleteOne(path);
|
|
274
|
+
return undefined;
|
|
275
|
+
}
|
|
276
|
+
},
|
|
277
|
+
upsertSnapshot(path, checksum, lineCount, hashes) {
|
|
278
|
+
stmts.upsert(path, checksum, lineCount, JSON.stringify(hashes), Date.now());
|
|
279
|
+
},
|
|
280
|
+
allKnownPaths() {
|
|
281
|
+
return stmts.allPaths();
|
|
282
|
+
},
|
|
283
|
+
allSnapshotHashes() {
|
|
284
|
+
return stmts.allHashes();
|
|
285
|
+
},
|
|
286
|
+
deleteSnapshot(path) {
|
|
287
|
+
stmts.deleteOne(path);
|
|
288
|
+
},
|
|
289
|
+
findSnapshotPaths(hashes) {
|
|
290
|
+
const rows = stmts.allHashes();
|
|
291
|
+
const matches = [];
|
|
292
|
+
for (const row of rows) {
|
|
293
|
+
try {
|
|
294
|
+
const parsed = JSON.parse(row.hashes);
|
|
295
|
+
if (!isValidHashList(parsed))
|
|
296
|
+
continue;
|
|
297
|
+
if (hashes.every((h) => parsed.includes(h)))
|
|
298
|
+
matches.push(row.path);
|
|
299
|
+
}
|
|
300
|
+
catch {
|
|
301
|
+
// unparseable row → skip it
|
|
302
|
+
}
|
|
303
|
+
}
|
|
304
|
+
return matches;
|
|
305
|
+
},
|
|
306
|
+
getUndo(path) {
|
|
307
|
+
const row = stmts.undoGet(path);
|
|
308
|
+
if (!row)
|
|
309
|
+
return undefined;
|
|
310
|
+
try {
|
|
311
|
+
const parsed = JSON.parse(row.hashes);
|
|
312
|
+
if (!isValidHashList(parsed)) {
|
|
313
|
+
stmts.undoDelete(path);
|
|
314
|
+
return undefined;
|
|
315
|
+
}
|
|
316
|
+
return {
|
|
317
|
+
content: row.content,
|
|
318
|
+
bom: row.bom,
|
|
319
|
+
ending: row.ending,
|
|
320
|
+
hashes: parsed,
|
|
321
|
+
resultContent: row.result_content,
|
|
322
|
+
};
|
|
323
|
+
}
|
|
324
|
+
catch {
|
|
325
|
+
stmts.undoDelete(path);
|
|
326
|
+
return undefined;
|
|
327
|
+
}
|
|
328
|
+
},
|
|
329
|
+
upsertUndo(path, entry) {
|
|
330
|
+
stmts.undoUpsert(path, entry.content, entry.bom, entry.ending, JSON.stringify(entry.hashes), entry.resultContent, Date.now());
|
|
331
|
+
},
|
|
332
|
+
deleteUndo(path) {
|
|
333
|
+
stmts.undoDelete(path);
|
|
334
|
+
},
|
|
335
|
+
getServed(sessionKey, path) {
|
|
336
|
+
const row = stmts.servedGet(sessionKey, path);
|
|
337
|
+
if (!row)
|
|
338
|
+
return [];
|
|
339
|
+
try {
|
|
340
|
+
const parsed = JSON.parse(row.hashes);
|
|
341
|
+
if (isValidServedList(parsed))
|
|
342
|
+
return parsed;
|
|
343
|
+
stmts.servedDelete(sessionKey, path);
|
|
344
|
+
return [];
|
|
345
|
+
}
|
|
346
|
+
catch {
|
|
347
|
+
stmts.servedDelete(sessionKey, path);
|
|
348
|
+
return [];
|
|
349
|
+
}
|
|
350
|
+
},
|
|
351
|
+
getServedReported(sessionKey, path) {
|
|
352
|
+
const row = stmts.servedGet(sessionKey, path);
|
|
353
|
+
if (!row)
|
|
354
|
+
return new Set();
|
|
355
|
+
const raw = row.reported;
|
|
356
|
+
if (typeof raw !== "string" || raw.length === 0)
|
|
357
|
+
return new Set();
|
|
358
|
+
try {
|
|
359
|
+
const parsed = JSON.parse(raw);
|
|
360
|
+
if (!Array.isArray(parsed))
|
|
361
|
+
return new Set();
|
|
362
|
+
return new Set(parsed.filter((h) => typeof h === "string" && HASH_RE.test(h)));
|
|
363
|
+
}
|
|
364
|
+
catch {
|
|
365
|
+
return new Set();
|
|
366
|
+
}
|
|
367
|
+
},
|
|
368
|
+
upsertServed(sessionKey, path, hashesJson) {
|
|
369
|
+
stmts.servedUpsert(sessionKey, path, hashesJson, Date.now());
|
|
370
|
+
},
|
|
371
|
+
upsertServedReported(sessionKey, path, reportedJson) {
|
|
372
|
+
stmts.servedReportedUpsert(sessionKey, path, reportedJson, Date.now());
|
|
373
|
+
},
|
|
374
|
+
clearServedReported(sessionKey, path) {
|
|
375
|
+
stmts.servedReportedClear(sessionKey, Date.now(), path);
|
|
376
|
+
},
|
|
377
|
+
deleteServed(sessionKey, path) {
|
|
378
|
+
stmts.servedDelete(sessionKey, path);
|
|
379
|
+
},
|
|
380
|
+
deleteServedByPath(path) {
|
|
381
|
+
stmts.servedDeletePath(path);
|
|
382
|
+
},
|
|
383
|
+
wipeServed(sessionKey) {
|
|
384
|
+
stmts.servedWipe(sessionKey);
|
|
385
|
+
},
|
|
386
|
+
pruneServedOlderThan(ts) {
|
|
387
|
+
stmts.servedPruneOlderThan(ts);
|
|
388
|
+
},
|
|
389
|
+
async pruneMissing() {
|
|
390
|
+
const rows = stmts.allPaths();
|
|
391
|
+
const missing = await statMissing(rows);
|
|
392
|
+
if (missing.length === 0)
|
|
393
|
+
return;
|
|
394
|
+
withStore(() => {
|
|
395
|
+
for (const path of missing) {
|
|
396
|
+
stmts.deleteOne(path);
|
|
397
|
+
stmts.undoDelete(path);
|
|
398
|
+
stmts.servedDeletePath(path);
|
|
399
|
+
}
|
|
400
|
+
});
|
|
401
|
+
},
|
|
402
|
+
};
|
|
403
|
+
}
|
|
206
404
|
function isHealthy(db) {
|
|
207
405
|
try {
|
|
208
406
|
const row = db.prepare("PRAGMA quick_check").get();
|
|
@@ -231,9 +429,32 @@ function shutdownDb(db) {
|
|
|
231
429
|
try {
|
|
232
430
|
db.exec("PRAGMA wal_checkpoint(TRUNCATE)");
|
|
233
431
|
}
|
|
234
|
-
catch {
|
|
432
|
+
catch {
|
|
433
|
+
// best-effort checkpoint before close
|
|
434
|
+
}
|
|
235
435
|
db.close();
|
|
236
436
|
}
|
|
437
|
+
const STAT_BATCH = 64;
|
|
438
|
+
async function statMissing(rows) {
|
|
439
|
+
const missing = [];
|
|
440
|
+
for (let i = 0; i < rows.length; i += STAT_BATCH) {
|
|
441
|
+
const batch = rows.slice(i, i + STAT_BATCH);
|
|
442
|
+
const results = await Promise.all(batch.map(async (row) => {
|
|
443
|
+
try {
|
|
444
|
+
await stat(row.path);
|
|
445
|
+
return undefined;
|
|
446
|
+
}
|
|
447
|
+
catch {
|
|
448
|
+
return row.path;
|
|
449
|
+
}
|
|
450
|
+
}));
|
|
451
|
+
for (const path of results) {
|
|
452
|
+
if (path !== undefined)
|
|
453
|
+
missing.push(path);
|
|
454
|
+
}
|
|
455
|
+
}
|
|
456
|
+
return missing;
|
|
457
|
+
}
|
|
237
458
|
async function openStore(storePath) {
|
|
238
459
|
// Multi-store: never close another workspace's store when opening this one.
|
|
239
460
|
await initHasher();
|
|
@@ -264,7 +485,8 @@ async function openStore(storePath) {
|
|
|
264
485
|
withBusyRetry(() => {
|
|
265
486
|
stmts.servedPruneOlderThan(Date.now() - SERVED_TTL_MS);
|
|
266
487
|
});
|
|
267
|
-
|
|
488
|
+
const store = makeDomainStore(stmts);
|
|
489
|
+
stores.set(storePath, { path: storePath, db, stmts, store });
|
|
268
490
|
if (!exitHandlerRegistered) {
|
|
269
491
|
exitHandlerRegistered = true;
|
|
270
492
|
process.once("exit", () => shutdownHashStore());
|
|
@@ -275,7 +497,7 @@ async function openStore(storePath) {
|
|
|
275
497
|
});
|
|
276
498
|
}
|
|
277
499
|
}
|
|
278
|
-
return
|
|
500
|
+
return store;
|
|
279
501
|
}
|
|
280
502
|
/** Resolve the store path for this call: explicit cwd, the active workspace, or the shared-home fallback. */
|
|
281
503
|
function storePathFor(cwd) {
|
|
@@ -291,7 +513,7 @@ export function loadHashStore(cwd) {
|
|
|
291
513
|
const storePath = storePathFor(cwd);
|
|
292
514
|
const cached = stores.get(storePath);
|
|
293
515
|
if (cached && cached.db.isOpen) {
|
|
294
|
-
return Promise.resolve(
|
|
516
|
+
return Promise.resolve(cached.store);
|
|
295
517
|
}
|
|
296
518
|
const existing = openings.get(storePath);
|
|
297
519
|
if (existing)
|
|
@@ -302,9 +524,10 @@ export function loadHashStore(cwd) {
|
|
|
302
524
|
openings.set(storePath, promise);
|
|
303
525
|
return promise;
|
|
304
526
|
}
|
|
305
|
-
/** The cached store for the active workspace (or the shared-home fallback), if open. */
|
|
527
|
+
/** The cached store entry for the active workspace (or the shared-home fallback), if open. */
|
|
306
528
|
function currentStore() {
|
|
307
|
-
|
|
529
|
+
const entry = stores.get(storePathFor());
|
|
530
|
+
return entry?.db.isOpen ? entry : undefined;
|
|
308
531
|
}
|
|
309
532
|
/** Close every open store (process exit, HMR, tests). */
|
|
310
533
|
export function shutdownHashStore() {
|
|
@@ -332,7 +555,9 @@ export function withStore(fn) {
|
|
|
332
555
|
try {
|
|
333
556
|
store.db.exec("ROLLBACK");
|
|
334
557
|
}
|
|
335
|
-
catch {
|
|
558
|
+
catch {
|
|
559
|
+
// best-effort rollback; the original error propagates
|
|
560
|
+
}
|
|
336
561
|
throw e;
|
|
337
562
|
}
|
|
338
563
|
});
|
|
@@ -400,6 +625,14 @@ async function migrateLegacy(db, storePath) {
|
|
|
400
625
|
console.error("Failed to rename legacy hash store after migration:", error);
|
|
401
626
|
}
|
|
402
627
|
}
|
|
403
|
-
|
|
404
|
-
|
|
405
|
-
export
|
|
628
|
+
// ---- async convenience helpers (load the active store, then delegate) ------
|
|
629
|
+
/** Find files whose stored snapshot hashes contain every given anchor. */
|
|
630
|
+
export async function findSnapshotPathsByHashes(hashes) {
|
|
631
|
+
const store = await loadHashStore();
|
|
632
|
+
return store.findSnapshotPaths(hashes);
|
|
633
|
+
}
|
|
634
|
+
/** Persist a hash snapshot for one path (async over the active store). */
|
|
635
|
+
export async function upsertSnapshotFor(path, checksum, lineCount, hashes) {
|
|
636
|
+
const store = await loadHashStore();
|
|
637
|
+
store.upsertSnapshot(path, checksum, lineCount, hashes);
|
|
638
|
+
}
|
package/lib/hashline/apply.js
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import { abortIf, splitLines } from "../utils.js";
|
|
2
|
-
import {
|
|
2
|
+
import { lineHashesPure, HASH_SEP } from "./pure.js";
|
|
3
3
|
import { AnchorMismatchError, verifyServedRange, } from "./served.js";
|
|
4
4
|
import { valEdit, stripBarePrefixes, stripDiffPrefixes, swapReversedRanges, warnUnicodeEsc, fmtMismatchWithServes, } from "./resolve.js";
|
|
5
5
|
export function buildIdx(content) {
|
|
@@ -85,7 +85,7 @@ function assemble(content, span, signal) {
|
|
|
85
85
|
export function applyEdit(content, edit, signal, precomputedHashes, filePath, served) {
|
|
86
86
|
abortIf(signal);
|
|
87
87
|
const lineIndex = buildIdx(content);
|
|
88
|
-
const fileHashes = precomputedHashes ??
|
|
88
|
+
const fileHashes = precomputedHashes ?? lineHashesPure(content);
|
|
89
89
|
const warnings = [];
|
|
90
90
|
const rangeFixed = swapReversedRanges(edit, fileHashes, warnings);
|
|
91
91
|
const prefixFixed = stripDiffPrefixes(stripBarePrefixes(rangeFixed, fileHashes, warnings), warnings);
|