@echo-agent/coding 0.7.0 → 0.9.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/src/tools/fs.ts DELETED
@@ -1,424 +0,0 @@
1
- // 文件三件套:read_file / write_file / edit_file。
2
- //
3
- // 这些是**产品层工具**(对外部世界动手),所以住这儿不住 core(判据 2026-08-04 拍定)。
4
- // 路径纪律:相对路径以 ctx.workspace 解析,**解析结果必须落在 workspace 之内**——
5
- // 越界一律拒绝(`../../etc/passwd` 这类,不管是模型手滑还是注入)。workspace 是 session 级事实(2026-09-01),
6
- // 既是起点也是边界,一个字段。这道边界是**六件工作区工具共用的一条**(`resolveInWorkspace`,search.ts 也走它),
7
- // 解 symlink(review 2026-09-07:此前只比字符串前缀,工作区里一条软链就能读写外面)。bash 不在这道边界内——
8
- // 它是有意留的出口,像 Claude Code 的 Bash 一样能 cd 到任何地方,看着它的是 permission 策略,不是路径。
9
- //
10
- // description 与结果文本是模型逐字读的资产(全英文,2026-09-01);跨工具的用法(先读后改、glob/grep 优先)
11
- // 在 `prompt.ts` 的 `tool:workspace` 段,这里只讲单个工具自己的语义。
12
-
13
- import { mkdir, readFile, realpath, stat, writeFile } from "node:fs/promises";
14
- import { dirname, isAbsolute, join, relative, resolve, sep } from "node:path";
15
- import { toolError, toolOk, type ModelTool, type ToolExecutionContext } from "@echo-agent/core";
16
- import { isImagePath, loadImageFile } from "@echo-agent/base";
17
-
18
- /** 读回显的上限:超过就按整行截断并标注——一个 50MB 的日志不该挤爆上下文。 */
19
- const READ_CAP = 50_000;
20
- const READ_LINES_DEFAULT = 2000;
21
- /** 「只差空白」的提示里最多原样列出几行、每行多长。 */
22
- const HINT_LINES = 20;
23
- const HINT_LINE_CHARS = 200;
24
-
25
- /**
26
- * 盘上字节 → 文本。三种情形:
27
- * - 含 NUL 字节:二进制(与 grep 同一条判据),不解码;
28
- * - 合法 UTF-8:`utf8: true`。BOM 原样留在文本里,改完写回时还在;
29
- * - 不是合法 UTF-8(Latin-1、GBK……):按 UTF-8 宽松解,解不通的字节成 U+FFFD,`utf8: false`。
30
- * 这种文本**只能看不能写回**——写回去那些字节就永久变成了替换符。
31
- */
32
- type Decoded = { kind: "binary" } | { kind: "text"; text: string; utf8: boolean };
33
-
34
- function decodeFile(bytes: Uint8Array): Decoded {
35
- if (bytes.includes(0)) return { kind: "binary" };
36
- try {
37
- return { kind: "text", text: new TextDecoder("utf-8", { fatal: true, ignoreBOM: true }).decode(bytes), utf8: true };
38
- } catch {
39
- return { kind: "text", text: new TextDecoder("utf-8", { ignoreBOM: true }).decode(bytes), utf8: false };
40
- }
41
- }
42
-
43
- /** edit_file / write_file 拒改非 UTF-8 文件时给模型的话。 */
44
- function notUtf8Text(path: string): string {
45
- return (
46
- `${path} is not valid UTF-8 (it may be Latin-1, GBK or another legacy encoding). Changing it here would turn every byte ` +
47
- "that does not decode into U+FFFD, so it is refused. Change it with bash in a way that keeps its encoding " +
48
- "(for example `LC_ALL=C sed` for an ASCII-only change, or iconv to convert it deliberately)."
49
- );
50
- }
51
-
52
- /** CRLF 占多数才算 CRLF 文件;一样多(含都没有)不算。 */
53
- function isCrlf(text: string): boolean {
54
- const crlf = text.split("\r\n").length - 1;
55
- const lf = text.split("\n").length - 1 - crlf;
56
- return crlf > lf;
57
- }
58
-
59
- /**
60
- * **改前必读、读后未变**(2026-09-01 用户拍板,照 Claude Code 的 Edit / Write 门):
61
- * `edit_file` / `write_file` 只接受本会话用 `read_file` 读过、且盘上自那次读之后没被别人改过的文件
62
- * (mtime 相等判据)。此前只在 description 里写了一句「先读」——那是纪律不是门:模型没读就改、
63
- * 或者用户在编辑器里刚改过的文件被模型按旧内容覆盖,都不会被拦住。
64
- *
65
- * 一张表跟着一次 `makeFsTools()` 走(= 一个 agent):键是绝对路径,值是读到 / 我们自己写完时的 mtime。
66
- * 新建文件(盘上不存在)不需要先读。
67
- */
68
- type SeenFiles = Map<string, number>;
69
-
70
- export function makeFsTools(): ModelTool[] {
71
- const seen: SeenFiles = new Map();
72
- return [readFileTool(seen), writeFileTool(seen), editFileTool(seen)] as ModelTool[];
73
- }
74
-
75
- /**
76
- * 改之前的门。返回 null = 可以改;否则是给模型看的拒绝理由。
77
- * `exists` 由调用方决定新建文件算不算(`write_file` 新建不用先读)。
78
- */
79
- async function assertFreshlyRead(seen: SeenFiles, abs: string, path: string): Promise<string | null> {
80
- const st = await stat(abs).catch(() => null);
81
- if (st === null) return null; // 不存在:write_file 新建;edit_file 随后读文件时会报 File not found
82
- const readAt = seen.get(abs);
83
- if (readAt === undefined) return `Read ${path} with read_file before changing it`;
84
- if (st.mtimeMs !== readAt) return `${path} changed on disk since you read it; read it again before changing it`;
85
- return null;
86
- }
87
-
88
- /** 读完 / 写完之后记下盘上的 mtime。 */
89
- async function markSeen(seen: SeenFiles, abs: string): Promise<void> {
90
- const st = await stat(abs).catch(() => null);
91
- if (st !== null) seen.set(abs, st.mtimeMs);
92
- }
93
-
94
- /** 解析结果:`abs` 是可以读写的绝对路径;越界时 `error` 是给模型看的拒绝理由。 */
95
- export type SafePath = { ok: true; abs: string } | { ok: false; error: string };
96
-
97
- /**
98
- * 公共面上的那一份:判据与 {@link resolveInWorkspace} 相同,只回路径或 null(越界)。
99
- * 六件工具自己用 `resolveInWorkspace`——它多带一句给模型看的拒绝理由。
100
- */
101
- export async function resolveSafe(ctx: ToolExecutionContext, path: string): Promise<string | null> {
102
- const r = await resolveInWorkspace(ctx, path);
103
- return r.ok ? r.abs : null;
104
- }
105
-
106
- /**
107
- * 解析 + 越界守卫:六件工作区工具(read_file / write_file / edit_file / list_dir / glob / grep)共用的**唯一**边界。
108
- *
109
- * 判据是**两边都取 realpath 再比**:目标的真实位置(**最近的已存在祖先** realpath 之后接上余下的段)
110
- * 必须落在工作区根的真实位置之内。只比字面前缀挡不住软链——工作区里一条 `link -> /etc`,`link/passwd`
111
- * 字面上完全在工作区内;只对目标取 realpath 又会误拒——工作区路径本身经软链(macOS 的 `/var` → `/private/var`、
112
- * 用户自己的软链目录)时,模型按 realpath 形式给的绝对路径字面上不在工作区下。用祖先是因为目标本身可能还不存在
113
- * (write_file 正要创建它);祖先在工作区之内,那么在它下面新建的东西也在。
114
- * **write_file 必须先过这一关再 mkdir**:`mkdir(..., {recursive:true})` 会顺着软链在外面把目录建出来。
115
- *
116
- * 返回的是工作区字面形式的路径(不是 realpath):后面的读写照常跟着软链走,结果仍在工作区内;结果文案里也不该
117
- * 冒出 `/private/var/...` 这种展开后的路径。给的是 realpath 形式时换算回工作区形式。
118
- *
119
- * 字面在工作区内、真实位置在外(monorepo 里 `node_modules` 软链到工作区外的兄弟包):仍拒,拒绝理由说明是软链、
120
- * 指到哪,并指向 bash——文件工具守着工作区,读工作区外的东西是 bash 的事。
121
- */
122
- export async function resolveInWorkspace(ctx: ToolExecutionContext, path: string): Promise<SafePath> {
123
- const root = resolve(ctx.workspace);
124
- const abs = isAbsolute(path) ? resolve(path) : resolve(root, path);
125
- const literalInside = abs === root || abs.startsWith(root + sep);
126
- const outside: SafePath = { ok: false, error: `Path outside the workspace: '${path}'` };
127
-
128
- let realRoot: string;
129
- try {
130
- realRoot = await realpath(root);
131
- } catch {
132
- // 工作区目录本身不存在 → 里面不可能有软链;只剩字面判据,后面的读写会自己报 not found
133
- return literalInside ? { ok: true, abs } : outside;
134
- }
135
- const real = await realTarget(abs);
136
- if (real === null) return outside; // 读不出来(权限、循环软链):按越界拒,不猜
137
- if (real === realRoot || real.startsWith(realRoot + sep)) {
138
- return { ok: true, abs: literalInside ? abs : join(root, relative(realRoot, real)) };
139
- }
140
- if (!literalInside) return outside;
141
- return {
142
- ok: false,
143
- error:
144
- `Path outside the workspace: '${path}' leads through a symlink to ${real}, which is outside the workspace. ` +
145
- "The file tools stay inside the workspace; read files outside it with bash.",
146
- };
147
- }
148
-
149
- /** 目标的真实位置:最近的已存在祖先取 realpath,再接上不存在的那几段。null = 读不出来(权限、循环软链)。 */
150
- async function realTarget(abs: string): Promise<string | null> {
151
- let cursor = abs;
152
- for (;;) {
153
- try {
154
- const real = await realpath(cursor);
155
- return cursor === abs ? real : join(real, relative(cursor, abs));
156
- } catch (e) {
157
- if ((e as { code?: string }).code !== "ENOENT") return null;
158
- const parent = dirname(cursor);
159
- if (parent === cursor) return abs; // 一路到文件系统根都不存在
160
- cursor = parent;
161
- }
162
- }
163
- }
164
-
165
- function readFileTool(seen: SeenFiles): ModelTool<{ path: string; offset?: number; limit?: number }> {
166
- return {
167
- kind: "model",
168
- name: "read_file",
169
- label: "读文件",
170
- // 只读:模型一口气读五个文件时同批同跑。`seen` 只按路径记一条 mtime,同批互不覆盖。
171
- concurrent: true,
172
- // 自己会分页(行数 + `READ_CAP` 截断并教 offset/limit),不走 core 的外存——外存再读一遍是绕路(2026-11-02,同 Claude Code 的 Read)
173
- maxInlineTokens: Infinity,
174
- description:
175
- `Read a file with line numbers. One call returns at most ${READ_LINES_DEFAULT} lines (limit) or ${READ_CAP} characters, ` +
176
- "always ending at a whole line; when more remains, the note at the end gives the offset to continue from. " +
177
- "Read a file before editing it. A note at the end also says when the file has CRLF line endings or is not valid UTF-8. " +
178
- "PNG, JPEG, GIF and WebP files are returned as images you can see; large ones are scaled down first. Other binary files are refused.",
179
- parameters: {
180
- type: "object",
181
- properties: {
182
- path: { type: "string", description: "Path relative to the workspace" },
183
- offset: { type: "number", description: "First line to read (1-based), default from the start" },
184
- limit: { type: "number", description: `Maximum number of lines, default ${READ_LINES_DEFAULT}` },
185
- },
186
- required: ["path"],
187
- },
188
- async execute({ path, offset, limit }, ctx) {
189
- const safe = await resolveInWorkspace(ctx, path);
190
- if (!safe.ok) return toolError(safe.error);
191
- const abs = safe.abs;
192
- // 图片按图片返回(2026-11-02,照 Claude Code 的 Read):大了先缩放。文件不存在、或扩展名是图片但内容不是,
193
- // 落到下面照文本读(不存在就报 File not found)
194
- if (isImagePath(abs)) {
195
- const loaded = await loadImageFile(abs);
196
- if (loaded.ok) {
197
- const { width, height } = loaded.info;
198
- const scaled = loaded.original === undefined ? "" : `, scaled down from ${loaded.original.width}×${loaded.original.height}`;
199
- return toolOk(`Image file ${path} (${width}×${height}${scaled})`, { path, width, height }, [loaded.image]);
200
- }
201
- if (loaded.reason === "too_large") {
202
- return toolError(`Cannot read ${path} as an image: ${loaded.message}. Make a smaller copy (for example with ImageMagick via bash) and read that.`);
203
- }
204
- }
205
- let bytes: Uint8Array;
206
- try {
207
- bytes = await readFile(abs);
208
- } catch (e) {
209
- return toolError(readErrText(e, path));
210
- }
211
- const decoded = decodeFile(bytes);
212
- // 二进制不进上下文:几千个替换符对模型毫无用处,还占掉一大块窗口
213
- if (decoded.kind === "binary") {
214
- return toolError(`${path} is a binary file (${bytes.byteLength} bytes); read_file shows text only. Inspect it with bash (for example \`file\` or \`xxd | head\`).`);
215
- }
216
- await markSeen(seen, abs); // 从这一刻起它才算「读过」:edit_file / write_file 的门看这个
217
- const page = renderLines(path, decoded.text, offset, limit);
218
- if (!page.ok) return toolError(page.error);
219
- const notes: string[] = [];
220
- if (isCrlf(decoded.text)) {
221
- notes.push("(This file has CRLF line endings: in edit_file, write the line breaks of a multi-line old_string as \\r\\n; new_string's line breaks are written as CRLF.)");
222
- }
223
- if (!decoded.utf8) {
224
- notes.push("(This file is not valid UTF-8; bytes that do not decode are shown as U+FFFD. edit_file and write_file refuse it; change it with bash, keeping its encoding.)");
225
- }
226
- return toolOk([page.text, ...notes].join("\n"), { path, lines: page.total });
227
- },
228
- };
229
- }
230
-
231
- /**
232
- * 带行号的一页。行按 `\n` 切,以换行结尾的文件不多出一个空的「最后一行」。
233
- * 截断永远落在整行上(行数 `limit` 或字符数 `READ_CAP`,先到者为准),尾注写**实际**显示到第几行、
234
- * 下一次的 offset——模型照它续读一行不漏。只有一行本身就超过 `READ_CAP` 时才截半行,并明说。
235
- */
236
- function renderLines(
237
- path: string,
238
- text: string,
239
- offset: number | undefined,
240
- limit: number | undefined,
241
- ): { ok: true; text: string; total: number } | { ok: false; error: string } {
242
- const lines = text.split("\n");
243
- if (lines.at(-1) === "") lines.pop();
244
- const total = lines.length;
245
- if (total === 0) return { ok: true, text: "(empty file)", total };
246
- const from = Math.max(1, Math.floor(offset ?? 1));
247
- if (from > total) return { ok: false, error: `offset ${from} is past the end of ${path} (${total} lines)` };
248
- const end = Math.min(total, from - 1 + Math.max(1, Math.floor(limit ?? READ_LINES_DEFAULT))); // 1-based,含
249
- const out: string[] = [];
250
- let size = 0;
251
- let last = from - 1;
252
- let longLine: number | null = null;
253
- for (let n = from; n <= end; n++) {
254
- const row = `${n}\t${lines[n - 1]}`;
255
- if (size + row.length + 1 > READ_CAP) {
256
- if (out.length === 0) {
257
- out.push(row.slice(0, READ_CAP)); // 一行就装不下:截这一行,并说清楚
258
- longLine = n;
259
- last = n;
260
- }
261
- break;
262
- }
263
- out.push(row);
264
- size += row.length + 1;
265
- last = n;
266
- }
267
- const notes: string[] = [];
268
- if (longLine !== null) {
269
- notes.push(`…[line ${longLine} is ${lines[longLine - 1]!.length} characters long and was cut at ${READ_CAP}; view the rest of it with bash]`);
270
- }
271
- if (last < total) {
272
- const capped = last < end && longLine === null ? `output capped at ${READ_CAP} characters; ` : "";
273
- notes.push(`…(${capped}showing lines ${from}-${last} of ${total}; continue with offset=${last + 1})`);
274
- }
275
- return { ok: true, text: [out.join("\n"), ...notes].join("\n"), total };
276
- }
277
-
278
- function writeFileTool(seen: SeenFiles): ModelTool<{ path: string; content: string }> {
279
- return {
280
- kind: "model",
281
- name: "write_file",
282
- label: "写文件",
283
- description:
284
- "Write a whole file (created if missing, parent directories created). To change an existing file prefer edit_file; overwriting loses whatever you did not notice. " +
285
- "An existing file must have been read with read_file in this session and be unchanged on disk since; an existing binary or non-UTF-8 file is refused.",
286
- parameters: {
287
- type: "object",
288
- properties: {
289
- path: { type: "string", description: "Path relative to the workspace" },
290
- content: { type: "string", description: "The full file content" },
291
- },
292
- required: ["path", "content"],
293
- },
294
- async execute({ path, content }, ctx) {
295
- const safe = await resolveInWorkspace(ctx, path); // 必须在 mkdir 之前:递归建目录会顺着软链把目录建到外面
296
- if (!safe.ok) return toolError(safe.error);
297
- const abs = safe.abs;
298
- const refused = await assertFreshlyRead(seen, abs, path); // 新建不用先读;覆盖已有的必须
299
- if (refused !== null) return toolError(refused);
300
- // 覆盖一个不是 UTF-8 的文件 = 悄悄把它换成另一种编码:拒,与 edit_file 同一条
301
- const existing = await readFile(abs).catch(() => null);
302
- if (existing !== null) {
303
- const decoded = decodeFile(existing);
304
- if (decoded.kind === "binary") return toolError(`${path} is a binary file; write_file does not overwrite binary files. Use bash if you mean to replace it.`);
305
- if (!decoded.utf8) return toolError(notUtf8Text(path));
306
- }
307
- await mkdir(dirname(abs), { recursive: true });
308
- await writeFile(abs, content, "utf8");
309
- await markSeen(seen, abs); // 自己写的算读过:接着 edit_file 不用再读一遍
310
- return toolOk(`Wrote ${path} (${content.length} characters)`, { path, bytes: content.length });
311
- },
312
- };
313
- }
314
-
315
- function editFileTool(seen: SeenFiles): ModelTool<{ path: string; old_string: string; new_string: string; replace_all?: boolean }> {
316
- return {
317
- kind: "model",
318
- name: "edit_file",
319
- label: "改文件",
320
- description:
321
- "Exact replacement: old_string must match the file content byte for byte (including indentation and line endings) and occur exactly once. " +
322
- "If it matches several places, give more surrounding context or set replace_all: true. " +
323
- "In a file with CRLF line endings, new_string's line breaks are written as CRLF. Only UTF-8 text files can be edited. " +
324
- "The file must have been read with read_file in this session and be unchanged on disk since.",
325
- parameters: {
326
- type: "object",
327
- properties: {
328
- path: { type: "string" },
329
- old_string: { type: "string", description: "The exact text to replace (byte for byte, including indentation)" },
330
- new_string: { type: "string", description: "The replacement text" },
331
- replace_all: { type: "boolean", description: "Replace every match; default false (the match must be unique)" },
332
- },
333
- required: ["path", "old_string", "new_string"],
334
- },
335
- async execute({ path, old_string, new_string, replace_all }, ctx) {
336
- const safe = await resolveInWorkspace(ctx, path);
337
- if (!safe.ok) return toolError(safe.error);
338
- const abs = safe.abs;
339
- if (old_string === new_string) return toolError("old_string and new_string are identical; nothing to change");
340
- // 空 old_string 会在每个字符之间插入(split("") 匹配文件长度次),replace_all 时静默毁掉整个文件(review 2026-09-07)
341
- if (old_string === "") {
342
- return toolError("old_string must not be empty; to insert text, include the line you are inserting next to as context (or use write_file for a new file)");
343
- }
344
- const refused = await assertFreshlyRead(seen, abs, path);
345
- if (refused !== null) return toolError(refused);
346
- let bytes: Uint8Array;
347
- try {
348
- bytes = await readFile(abs);
349
- } catch (e) {
350
- return toolError(readErrText(e, path));
351
- }
352
- const decoded = decodeFile(bytes);
353
- if (decoded.kind === "binary") return toolError(`${path} is a binary file; edit_file changes text files only.`);
354
- if (!decoded.utf8) return toolError(notUtf8Text(path)); // 写回去,解不通的字节就永久成了替换符
355
- const raw = decoded.text;
356
- const count = raw.split(old_string).length - 1;
357
- if (count === 0) return toolError(noMatchText(raw, old_string));
358
- if (count > 1 && replace_all !== true) {
359
- return toolError(`${count} matches: add surrounding context to make old_string unique, or set replace_all: true`);
360
- }
361
- // CRLF 文件里按文件的换行风格写回:模型写的 new_string 多半只用 \n,不换就成了混合行尾
362
- const replacement = isCrlf(raw) ? new_string.replace(/\r?\n/g, "\r\n") : new_string;
363
- // 替换文本用函数给:字符串形式会解释 `$&` / `$$` 这类模式,new_string 里带 `$` 的代码就会被替错
364
- const next = replace_all === true ? raw.split(old_string).join(replacement) : raw.replace(old_string, () => replacement);
365
- await writeFile(abs, next, "utf8");
366
- await markSeen(seen, abs); // 连续几次 edit 不用中间重读
367
- return toolOk(`Replaced ${replace_all === true ? count : 1} occurrence(s) in ${path}`, { path, replaced: count });
368
- },
369
- };
370
- }
371
-
372
- /**
373
- * 匹配失败时告诉模型**为什么**,好让下一次一次改对。只认两种可判定的近似,**只提示、不替它改**
374
- * (静默的模糊替换会改到模型没看准的地方):
375
- * - 换行不同:文件是 CRLF,old_string 用的是 \n——把 old_string 的 \n 写成 \r\n 就能精确匹配;
376
- * - 只差空白:按行比、每行折叠空白后相等(缩进 tab / 空格、行尾空格),把文件里那几行原样(JSON 引号包着,
377
- * tab 与行尾空格看得见)给出来。
378
- * 两样都不是:原来那句。
379
- */
380
- function noMatchText(raw: string, old: string): string {
381
- const generic = "No match: old_string must equal the file content byte for byte (including indentation). Read the file with read_file and copy it exactly";
382
- const crlf = isCrlf(raw);
383
- if (crlf && !old.includes("\r") && old.includes("\n") && raw.includes(old.replace(/\n/g, "\r\n"))) {
384
- return "No match: this file has CRLF (\\r\\n) line endings and old_string uses \\n. old_string matches once its line breaks are written as \\r\\n; retry that way (new_string's line breaks are converted to CRLF for you).";
385
- }
386
- const fileLines = raw.split("\n").map((l) => l.replace(/\r$/, ""));
387
- const at = whitespaceInsensitiveLine(fileLines, old);
388
- if (at === null) return generic;
389
- const span = old.replace(/\r/g, "").split("\n").length;
390
- const shown = fileLines
391
- .slice(at, at + Math.min(span, HINT_LINES))
392
- .map((l, i) => `${at + i + 1}: ${JSON.stringify(l.length > HINT_LINE_CHARS ? `${l.slice(0, HINT_LINE_CHARS)}…` : l)}`);
393
- return [
394
- `No match: old_string matches lines ${at + 1}-${at + span} only when whitespace is ignored, so the indentation or spacing differs ` +
395
- "(tabs versus spaces, trailing spaces). Those lines in the file, quoted so every tab and space shows:",
396
- ...shown,
397
- `Copy their whitespace exactly into old_string${crlf ? " (the file has CRLF line endings: write line breaks as \\r\\n)" : ""}.`,
398
- ].join("\n");
399
- }
400
-
401
- /**
402
- * old_string 在文件里**忽略空白**时从第几行(0-based)开始匹配;匹配不上 = null。
403
- * 每行折叠空白再比:首行可以只是文件那一行的结尾、末行可以只是开头(old_string 不必从行首起、到行尾止),中间整行相等。
404
- */
405
- function whitespaceInsensitiveLine(fileLines: readonly string[], old: string): number | null {
406
- const norm = (s: string): string => s.replace(/\s+/g, " ").trim();
407
- const want = old.replace(/\r/g, "").split("\n").map(norm);
408
- if (want.every((w) => w === "")) return null;
409
- const have = fileLines.map(norm);
410
- for (let i = 0; i + want.length <= have.length; i++) {
411
- if (want.length === 1) {
412
- if (have[i]!.includes(want[0]!)) return i;
413
- continue;
414
- }
415
- let ok = have[i]!.endsWith(want[0]!) && have[i + want.length - 1]!.startsWith(want.at(-1)!);
416
- for (let k = 1; ok && k < want.length - 1; k++) ok = have[i + k] === want[k];
417
- if (ok) return i;
418
- }
419
- return null;
420
- }
421
-
422
- function readErrText(e: unknown, path: string): string {
423
- return (e as { code?: string }).code === "ENOENT" ? `File not found: ${path}` : `Read failed: ${String(e)}`;
424
- }