@dench.com/cli 2.9.7 → 2.9.8
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 +40 -0
- package/fs-daemon +14 -1
- package/fs-daemon-health.ts +37 -0
- package/fs-daemon-pull-queue.ts +49 -0
- package/fs-daemon-pull.ts +111 -0
- package/fs-daemon-sync-tracker.ts +159 -0
- package/fs-daemon-workspace.ts +442 -0
- package/fs-daemon.ts +294 -536
- package/fs-workspace-worker.mjs +7 -0
- package/package.json +6 -1
|
@@ -0,0 +1,442 @@
|
|
|
1
|
+
/** Mounted I/O belongs to a PROCESS, not a worker thread: threads share
|
|
2
|
+
* libuv's pool. A stuck FUSE stat must not starve parent DNS or /tmp queues. */
|
|
3
|
+
import { spawn, type ChildProcess } from "node:child_process";
|
|
4
|
+
import { EventEmitter } from "node:events";
|
|
5
|
+
import { mkdir, readdir, readFile, stat, writeFile } from "node:fs/promises";
|
|
6
|
+
import { isAbsolute, join, relative, resolve } from "node:path";
|
|
7
|
+
import { fileURLToPath } from "node:url";
|
|
8
|
+
|
|
9
|
+
export type WorkspaceWorkerStatus = {
|
|
10
|
+
state: "starting" | "ready" | "stalled" | "backoff" | "stopped";
|
|
11
|
+
pid: number | null;
|
|
12
|
+
lastProgressAt: number | null;
|
|
13
|
+
restarts: number;
|
|
14
|
+
error: string | null;
|
|
15
|
+
};
|
|
16
|
+
export type WorkspaceFile = { absPath: string; size: number; mtimeMs: number };
|
|
17
|
+
type Request = { id: number; method: string; args: unknown[] };
|
|
18
|
+
type Reply = {
|
|
19
|
+
id?: number;
|
|
20
|
+
value?: unknown;
|
|
21
|
+
error?: { message: string; code?: string };
|
|
22
|
+
event?: string;
|
|
23
|
+
path?: string;
|
|
24
|
+
};
|
|
25
|
+
const IO_TIMEOUT_MS = 8_000;
|
|
26
|
+
const SCAN_TIMEOUT_MS = 60_000;
|
|
27
|
+
const MAX_PENDING = 32;
|
|
28
|
+
|
|
29
|
+
export function shouldIgnore(relPath: string): boolean {
|
|
30
|
+
const parts = relPath.split("/").filter(Boolean);
|
|
31
|
+
const blockedDirs = new Set([
|
|
32
|
+
".chrome-profile",
|
|
33
|
+
".dench",
|
|
34
|
+
".git",
|
|
35
|
+
".next",
|
|
36
|
+
".vercel",
|
|
37
|
+
"build",
|
|
38
|
+
"coverage",
|
|
39
|
+
"dist",
|
|
40
|
+
"node_modules",
|
|
41
|
+
]);
|
|
42
|
+
if (parts.some((part) => blockedDirs.has(part))) return true;
|
|
43
|
+
const basename = parts.at(-1)?.toLowerCase() ?? "";
|
|
44
|
+
if (basename === ".ds_store" || basename === "thumbs.db") return true;
|
|
45
|
+
if (basename.startsWith("._")) return true;
|
|
46
|
+
if (
|
|
47
|
+
basename === ".env" ||
|
|
48
|
+
(basename.startsWith(".env.") &&
|
|
49
|
+
![".env.example", ".env.sample", ".env.template"].includes(basename))
|
|
50
|
+
) {
|
|
51
|
+
return true;
|
|
52
|
+
}
|
|
53
|
+
if (basename.endsWith(".pem") || basename.endsWith(".key")) return true;
|
|
54
|
+
return false;
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
/** Reject incomplete snapshots. Treating EIO/timeout as an empty subtree can
|
|
58
|
+
* propagate false deletes to Convex. Symlinks are never traversed. */
|
|
59
|
+
export async function scanWorkspace(
|
|
60
|
+
workspace: string,
|
|
61
|
+
): Promise<WorkspaceFile[]> {
|
|
62
|
+
const result: WorkspaceFile[] = [];
|
|
63
|
+
const queue = [workspace];
|
|
64
|
+
for (let index = 0; index < queue.length; index++) {
|
|
65
|
+
const dir = queue[index];
|
|
66
|
+
const entries = await readdir(dir, { withFileTypes: true });
|
|
67
|
+
for (const entry of entries) {
|
|
68
|
+
const absPath = join(dir, entry.name);
|
|
69
|
+
if (shouldIgnore(relative(workspace, absPath))) continue;
|
|
70
|
+
if (entry.isDirectory()) queue.push(absPath);
|
|
71
|
+
else if (entry.isFile()) {
|
|
72
|
+
// Even ENOENT invalidates this snapshot: retry the whole scan before
|
|
73
|
+
// making any deletion decisions from a changing tree.
|
|
74
|
+
const info = await stat(absPath);
|
|
75
|
+
result.push({ absPath, size: info.size, mtimeMs: info.mtimeMs });
|
|
76
|
+
}
|
|
77
|
+
}
|
|
78
|
+
}
|
|
79
|
+
return result;
|
|
80
|
+
}
|
|
81
|
+
|
|
82
|
+
export class WorkspaceIo extends EventEmitter {
|
|
83
|
+
private child: ChildProcess | null = null;
|
|
84
|
+
private sequence = 0;
|
|
85
|
+
private closed = false;
|
|
86
|
+
private ready = false;
|
|
87
|
+
private restartTimes: number[] = [];
|
|
88
|
+
private restartTimer?: ReturnType<typeof setTimeout>;
|
|
89
|
+
private watching: boolean | null = null;
|
|
90
|
+
private pending = new Map<
|
|
91
|
+
number,
|
|
92
|
+
{
|
|
93
|
+
request: Request;
|
|
94
|
+
resolve: (value: unknown) => void;
|
|
95
|
+
reject: (error: Error) => void;
|
|
96
|
+
timer: ReturnType<typeof setTimeout>;
|
|
97
|
+
callerTimer?: ReturnType<typeof setTimeout>;
|
|
98
|
+
sent: boolean;
|
|
99
|
+
}
|
|
100
|
+
>();
|
|
101
|
+
private health: WorkspaceWorkerStatus = {
|
|
102
|
+
state: "starting",
|
|
103
|
+
pid: null,
|
|
104
|
+
lastProgressAt: null,
|
|
105
|
+
restarts: 0,
|
|
106
|
+
error: null,
|
|
107
|
+
};
|
|
108
|
+
|
|
109
|
+
constructor(
|
|
110
|
+
readonly workspace: string,
|
|
111
|
+
private readonly options: {
|
|
112
|
+
timeoutMs?: number;
|
|
113
|
+
restartDelayMs?: number;
|
|
114
|
+
spawnWorker?: () => ChildProcess;
|
|
115
|
+
} = {},
|
|
116
|
+
) {
|
|
117
|
+
super();
|
|
118
|
+
this.launch();
|
|
119
|
+
}
|
|
120
|
+
|
|
121
|
+
snapshot(): WorkspaceWorkerStatus {
|
|
122
|
+
return { ...this.health };
|
|
123
|
+
}
|
|
124
|
+
private changed() {
|
|
125
|
+
this.emit("status", this.snapshot());
|
|
126
|
+
}
|
|
127
|
+
private launch() {
|
|
128
|
+
if (this.closed || this.child) return;
|
|
129
|
+
this.ready = false;
|
|
130
|
+
this.health.state = "starting";
|
|
131
|
+
this.health.lastProgressAt = null;
|
|
132
|
+
const child =
|
|
133
|
+
this.options.spawnWorker?.() ??
|
|
134
|
+
spawn(
|
|
135
|
+
process.execPath,
|
|
136
|
+
[
|
|
137
|
+
fileURLToPath(new URL("./fs-workspace-worker.mjs", import.meta.url)),
|
|
138
|
+
resolve(this.workspace),
|
|
139
|
+
],
|
|
140
|
+
{
|
|
141
|
+
cwd: "/tmp",
|
|
142
|
+
stdio: ["ignore", "inherit", "inherit", "ipc"],
|
|
143
|
+
serialization: "advanced",
|
|
144
|
+
},
|
|
145
|
+
);
|
|
146
|
+
this.child = child;
|
|
147
|
+
this.health.pid = child.pid ?? null;
|
|
148
|
+
this.changed();
|
|
149
|
+
const bootTimer = setTimeout(
|
|
150
|
+
() => this.stall("workspace_io_timeout: startup"),
|
|
151
|
+
this.options.timeoutMs ?? IO_TIMEOUT_MS,
|
|
152
|
+
);
|
|
153
|
+
child.on("message", (message: Reply) => {
|
|
154
|
+
if (this.child !== child || this.health.state === "stalled") return;
|
|
155
|
+
if (message.event === "ready") {
|
|
156
|
+
clearTimeout(bootTimer);
|
|
157
|
+
this.ready = true;
|
|
158
|
+
this.health.state = "ready";
|
|
159
|
+
this.health.error = null;
|
|
160
|
+
this.changed();
|
|
161
|
+
for (const item of this.pending.values()) this.send(item.request);
|
|
162
|
+
if (this.watching !== null)
|
|
163
|
+
void this.call("watch", [this.watching]).catch(() => undefined);
|
|
164
|
+
return;
|
|
165
|
+
}
|
|
166
|
+
if (message.event && message.path) {
|
|
167
|
+
this.emit(message.event, message.path);
|
|
168
|
+
return;
|
|
169
|
+
}
|
|
170
|
+
if (message.event === "watch_error") {
|
|
171
|
+
this.stall("workspace_io_failed: watcher");
|
|
172
|
+
return;
|
|
173
|
+
}
|
|
174
|
+
const item =
|
|
175
|
+
message.id === undefined ? undefined : this.pending.get(message.id);
|
|
176
|
+
if (!item) return;
|
|
177
|
+
this.pending.delete(message.id!);
|
|
178
|
+
clearTimeout(item.timer);
|
|
179
|
+
clearTimeout(item.callerTimer);
|
|
180
|
+
if (message.error)
|
|
181
|
+
item.reject(
|
|
182
|
+
Object.assign(new Error(message.error.message), {
|
|
183
|
+
code: message.error.code,
|
|
184
|
+
}),
|
|
185
|
+
);
|
|
186
|
+
else {
|
|
187
|
+
this.health.lastProgressAt = Date.now();
|
|
188
|
+
this.changed();
|
|
189
|
+
item.resolve(message.value);
|
|
190
|
+
}
|
|
191
|
+
});
|
|
192
|
+
const exited = () => {
|
|
193
|
+
clearTimeout(bootTimer);
|
|
194
|
+
if (this.child !== child) return;
|
|
195
|
+
this.child = null;
|
|
196
|
+
this.ready = false;
|
|
197
|
+
this.health.pid = null;
|
|
198
|
+
this.rejectPending(new Error("workspace_io_unavailable: worker exited"));
|
|
199
|
+
if (this.closed) return;
|
|
200
|
+
this.health.state = "backoff";
|
|
201
|
+
const now = Date.now();
|
|
202
|
+
this.restartTimes = this.restartTimes.filter((at) => now - at < 60_000);
|
|
203
|
+
// At most two replacements per minute, and ONLY after observed exit.
|
|
204
|
+
// SIGKILL is not proof of exit for an uninterruptible D-state process.
|
|
205
|
+
const delay =
|
|
206
|
+
this.restartTimes.length >= 2
|
|
207
|
+
? Math.max(1, 60_000 - (now - this.restartTimes[0]))
|
|
208
|
+
: (this.options.restartDelayMs ?? 1_000);
|
|
209
|
+
this.changed();
|
|
210
|
+
this.restartTimer = setTimeout(() => {
|
|
211
|
+
if (this.closed) return;
|
|
212
|
+
this.restartTimes.push(Date.now());
|
|
213
|
+
this.health.restarts++;
|
|
214
|
+
this.launch();
|
|
215
|
+
}, delay);
|
|
216
|
+
};
|
|
217
|
+
child.once("exit", exited);
|
|
218
|
+
child.once("error", () => {
|
|
219
|
+
if (this.child !== child) return;
|
|
220
|
+
if (!child.pid) {
|
|
221
|
+
this.health.error = "workspace_io_failed: spawn";
|
|
222
|
+
exited();
|
|
223
|
+
} else {
|
|
224
|
+
// kill/send errors are NOT an exit notification. Retain ownership
|
|
225
|
+
// until the OS confirms exit, including EPERM and D-state failures.
|
|
226
|
+
this.stall("workspace_io_failed: child process");
|
|
227
|
+
}
|
|
228
|
+
});
|
|
229
|
+
}
|
|
230
|
+
private send(request: Request) {
|
|
231
|
+
const child = this.child;
|
|
232
|
+
const item = this.pending.get(request.id);
|
|
233
|
+
if (!child || !item || item.sent) return;
|
|
234
|
+
item.sent = true;
|
|
235
|
+
child.send(request, (error) => {
|
|
236
|
+
if (error && this.child === child) this.stall("workspace_io_failed: ipc");
|
|
237
|
+
});
|
|
238
|
+
}
|
|
239
|
+
private rejectPending(error: Error) {
|
|
240
|
+
for (const item of this.pending.values()) {
|
|
241
|
+
clearTimeout(item.timer);
|
|
242
|
+
clearTimeout(item.callerTimer);
|
|
243
|
+
item.reject(error);
|
|
244
|
+
}
|
|
245
|
+
this.pending.clear();
|
|
246
|
+
}
|
|
247
|
+
private stall(message: string) {
|
|
248
|
+
if (this.closed || this.health.state === "stalled") return;
|
|
249
|
+
this.health.state = "stalled";
|
|
250
|
+
this.health.error = message;
|
|
251
|
+
this.rejectPending(new Error(message));
|
|
252
|
+
this.changed();
|
|
253
|
+
try {
|
|
254
|
+
this.child?.kill("SIGKILL");
|
|
255
|
+
} catch {
|
|
256
|
+
/* Retain the unexited child. */
|
|
257
|
+
}
|
|
258
|
+
// No restart here! A stuck kernel syscall may ignore SIGKILL indefinitely.
|
|
259
|
+
}
|
|
260
|
+
call<T>(
|
|
261
|
+
method: string,
|
|
262
|
+
args: unknown[] = [],
|
|
263
|
+
timeoutMs?: number,
|
|
264
|
+
stallOnTimeout = timeoutMs === undefined,
|
|
265
|
+
): Promise<T> {
|
|
266
|
+
if (this.closed || !this.child || this.health.state === "stalled")
|
|
267
|
+
return Promise.reject(
|
|
268
|
+
new Error("workspace_io_unavailable: " + this.health.state),
|
|
269
|
+
);
|
|
270
|
+
if (this.pending.size >= MAX_PENDING)
|
|
271
|
+
return Promise.reject(new Error("workspace_io_busy: queue full"));
|
|
272
|
+
const id = ++this.sequence;
|
|
273
|
+
const request = { id, method, args };
|
|
274
|
+
const usedTimeout = timeoutMs ?? this.options.timeoutMs ?? IO_TIMEOUT_MS;
|
|
275
|
+
return new Promise<T>((resolveValue, reject) => {
|
|
276
|
+
// Caller expiry does not cancel a syscall already executing in the
|
|
277
|
+
// child. Keep its admission slot and independent liveness deadline.
|
|
278
|
+
const livenessTimeout = stallOnTimeout
|
|
279
|
+
? usedTimeout
|
|
280
|
+
: (this.options.timeoutMs ?? IO_TIMEOUT_MS);
|
|
281
|
+
const timer = setTimeout(() => {
|
|
282
|
+
if (this.pending.has(id))
|
|
283
|
+
this.stall("workspace_io_timeout: " + method);
|
|
284
|
+
}, Math.max(1, livenessTimeout));
|
|
285
|
+
const callerTimer = stallOnTimeout
|
|
286
|
+
? undefined
|
|
287
|
+
: setTimeout(() => {
|
|
288
|
+
const item = this.pending.get(id);
|
|
289
|
+
if (!item) return;
|
|
290
|
+
if (!item.sent) {
|
|
291
|
+
// Expired startup work must never be sent after ready.
|
|
292
|
+
this.pending.delete(id);
|
|
293
|
+
clearTimeout(item.timer);
|
|
294
|
+
}
|
|
295
|
+
item.reject(new Error("workspace_io_timeout: " + method));
|
|
296
|
+
}, Math.max(1, usedTimeout));
|
|
297
|
+
this.pending.set(id, {
|
|
298
|
+
request,
|
|
299
|
+
resolve: (value) => resolveValue(value as T),
|
|
300
|
+
reject,
|
|
301
|
+
timer,
|
|
302
|
+
callerTimer,
|
|
303
|
+
sent: false,
|
|
304
|
+
});
|
|
305
|
+
if (this.ready) this.send(request);
|
|
306
|
+
});
|
|
307
|
+
}
|
|
308
|
+
async watch(isFuse: boolean) {
|
|
309
|
+
this.watching = isFuse;
|
|
310
|
+
if (this.ready) await this.call("watch", [isFuse]);
|
|
311
|
+
}
|
|
312
|
+
readFile(path: string) {
|
|
313
|
+
return this.call<Buffer>("readFile", [path]);
|
|
314
|
+
}
|
|
315
|
+
writeFile(
|
|
316
|
+
path: string,
|
|
317
|
+
bytes: Uint8Array,
|
|
318
|
+
options?: { flag?: string },
|
|
319
|
+
timeoutMs?: number,
|
|
320
|
+
) {
|
|
321
|
+
return this.call<void>("writeFile", [path, bytes, options], timeoutMs);
|
|
322
|
+
}
|
|
323
|
+
stat(path: string) {
|
|
324
|
+
return this.call<{ isFile: boolean; size: number; mtimeMs: number }>(
|
|
325
|
+
"stat",
|
|
326
|
+
[path],
|
|
327
|
+
);
|
|
328
|
+
}
|
|
329
|
+
mkdir(path: string, timeoutMs?: number) {
|
|
330
|
+
return this.call<void>("mkdir", [path], timeoutMs);
|
|
331
|
+
}
|
|
332
|
+
scan() {
|
|
333
|
+
return this.call<WorkspaceFile[]>(
|
|
334
|
+
"scan",
|
|
335
|
+
[],
|
|
336
|
+
this.options.timeoutMs ?? SCAN_TIMEOUT_MS,
|
|
337
|
+
true,
|
|
338
|
+
);
|
|
339
|
+
}
|
|
340
|
+
close() {
|
|
341
|
+
this.closed = true;
|
|
342
|
+
clearTimeout(this.restartTimer);
|
|
343
|
+
this.health.state = "stopped";
|
|
344
|
+
this.rejectPending(new Error("workspace_io_unavailable: stopped"));
|
|
345
|
+
try {
|
|
346
|
+
this.child?.kill("SIGKILL");
|
|
347
|
+
} catch {
|
|
348
|
+
/* No replacement on shutdown. */
|
|
349
|
+
}
|
|
350
|
+
this.changed();
|
|
351
|
+
}
|
|
352
|
+
}
|
|
353
|
+
|
|
354
|
+
/** Entry point invoked ONLY by the child launcher. No Convex, DNS, control
|
|
355
|
+
* queue, or health-file operations belong here. */
|
|
356
|
+
export async function runWorkspaceWorker(workspace: string) {
|
|
357
|
+
let watcher: import("chokidar").FSWatcher | undefined;
|
|
358
|
+
const root = resolve(workspace);
|
|
359
|
+
const checked = (input: unknown) => {
|
|
360
|
+
if (typeof input !== "string")
|
|
361
|
+
throw new Error("workspace_io_invalid: path");
|
|
362
|
+
const path = resolve(input);
|
|
363
|
+
const rel = relative(root, path);
|
|
364
|
+
if (rel === ".." || rel.startsWith("../") || isAbsolute(rel))
|
|
365
|
+
throw new Error("workspace_io_invalid: outside root");
|
|
366
|
+
return path;
|
|
367
|
+
};
|
|
368
|
+
process.on("disconnect", () => process.exit(0));
|
|
369
|
+
process.on("message", async (request: Request) => {
|
|
370
|
+
try {
|
|
371
|
+
const [path, bytes, options] = request.args;
|
|
372
|
+
let value: unknown;
|
|
373
|
+
switch (request.method) {
|
|
374
|
+
case "readFile":
|
|
375
|
+
value = await readFile(checked(path));
|
|
376
|
+
break;
|
|
377
|
+
case "writeFile":
|
|
378
|
+
await writeFile(
|
|
379
|
+
checked(path),
|
|
380
|
+
bytes as Uint8Array,
|
|
381
|
+
options as { flag?: string },
|
|
382
|
+
);
|
|
383
|
+
break;
|
|
384
|
+
case "stat": {
|
|
385
|
+
const info = await stat(checked(path));
|
|
386
|
+
value = {
|
|
387
|
+
isFile: info.isFile(),
|
|
388
|
+
size: info.size,
|
|
389
|
+
mtimeMs: info.mtimeMs,
|
|
390
|
+
};
|
|
391
|
+
break;
|
|
392
|
+
}
|
|
393
|
+
case "mkdir":
|
|
394
|
+
await mkdir(checked(path), { recursive: true });
|
|
395
|
+
break;
|
|
396
|
+
case "scan":
|
|
397
|
+
value = await scanWorkspace(root);
|
|
398
|
+
break;
|
|
399
|
+
case "watch": {
|
|
400
|
+
if (watcher) break;
|
|
401
|
+
await mkdir(root, { recursive: true });
|
|
402
|
+
const { watch } = await import("chokidar");
|
|
403
|
+
const isFuse = Boolean(path);
|
|
404
|
+
watcher = watch(root, {
|
|
405
|
+
ignoreInitial: true,
|
|
406
|
+
persistent: true,
|
|
407
|
+
usePolling: isFuse,
|
|
408
|
+
interval: isFuse ? 2_500 : undefined,
|
|
409
|
+
binaryInterval: isFuse ? 5_000 : undefined,
|
|
410
|
+
awaitWriteFinish: isFuse
|
|
411
|
+
? false
|
|
412
|
+
: { stabilityThreshold: 100, pollInterval: 50 },
|
|
413
|
+
ignored: (path) => {
|
|
414
|
+
const rel = relative(root, path);
|
|
415
|
+
return rel.startsWith("..") || shouldIgnore(rel);
|
|
416
|
+
},
|
|
417
|
+
});
|
|
418
|
+
for (const event of ["add", "change", "unlink"] as const)
|
|
419
|
+
watcher.on(event, (path) => process.send?.({ event, path }));
|
|
420
|
+
watcher.on("error", () => process.send?.({ event: "watch_error" }));
|
|
421
|
+
break;
|
|
422
|
+
}
|
|
423
|
+
default:
|
|
424
|
+
throw new Error("workspace_io_invalid: method");
|
|
425
|
+
}
|
|
426
|
+
process.send?.({ id: request.id, value });
|
|
427
|
+
} catch (error) {
|
|
428
|
+
const code = (error as NodeJS.ErrnoException).code;
|
|
429
|
+
process.send?.({
|
|
430
|
+
id: request.id,
|
|
431
|
+
error: {
|
|
432
|
+
message:
|
|
433
|
+
"workspace_io_failed: " +
|
|
434
|
+
request.method +
|
|
435
|
+
(code ? " (" + code + ")" : ""),
|
|
436
|
+
code,
|
|
437
|
+
},
|
|
438
|
+
});
|
|
439
|
+
}
|
|
440
|
+
});
|
|
441
|
+
process.send?.({ event: "ready" });
|
|
442
|
+
}
|