@ricsam/r5d-worker 0.0.122 → 0.0.124

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.
Files changed (67) hide show
  1. package/README.md +6 -3
  2. package/dist/cjs/command-launcher.cjs +76 -2
  3. package/dist/cjs/control-command-policy.cjs +177 -0
  4. package/dist/cjs/main.cjs +541 -247
  5. package/dist/cjs/package.json +1 -1
  6. package/dist/cjs/project-checkout-garbage.cjs +32 -3
  7. package/dist/cjs/project-workspace-state.cjs +51 -35
  8. package/dist/cjs/project-worktrees.cjs +79 -34
  9. package/dist/cjs/recovery-journal-protocol.cjs +56 -0
  10. package/dist/cjs/recovery-journal-runtime.cjs +133 -0
  11. package/dist/cjs/recovery-journal-thread.cjs +9 -0
  12. package/dist/cjs/recovery-journal.cjs +735 -0
  13. package/dist/cjs/recovery-store.cjs +8 -0
  14. package/dist/cjs/session-file-mutations.cjs +61 -0
  15. package/dist/cjs/working-tree-mirror.cjs +1 -0
  16. package/dist/cjs/workspace-command-sync-policy.cjs +37 -8
  17. package/dist/cjs/workspace-filesystem-executor-thread.cjs +36 -0
  18. package/dist/cjs/workspace-filesystem-executor.cjs +327 -0
  19. package/dist/cjs/workspace-filesystem-job-types.cjs +134 -0
  20. package/dist/cjs/workspace-filesystem-jobs.cjs +57 -0
  21. package/dist/cjs/workspace-git-sync.cjs +275 -201
  22. package/dist/cjs/workspace-mount-hold-fence.cjs +120 -0
  23. package/dist/mjs/command-launcher.mjs +75 -2
  24. package/dist/mjs/control-command-policy.mjs +146 -0
  25. package/dist/mjs/main.mjs +549 -253
  26. package/dist/mjs/package.json +1 -1
  27. package/dist/mjs/project-checkout-garbage.mjs +30 -2
  28. package/dist/mjs/project-workspace-state.mjs +51 -35
  29. package/dist/mjs/project-worktrees.mjs +74 -34
  30. package/dist/mjs/recovery-journal-protocol.mjs +30 -0
  31. package/dist/mjs/recovery-journal-runtime.mjs +112 -0
  32. package/dist/mjs/recovery-journal-thread.mjs +8 -0
  33. package/dist/mjs/recovery-journal.mjs +687 -0
  34. package/dist/mjs/recovery-store.mjs +8 -0
  35. package/dist/mjs/session-file-mutations.mjs +37 -0
  36. package/dist/mjs/working-tree-mirror.mjs +1 -0
  37. package/dist/mjs/workspace-command-sync-policy.mjs +37 -8
  38. package/dist/mjs/workspace-filesystem-executor-thread.mjs +38 -0
  39. package/dist/mjs/workspace-filesystem-executor.mjs +287 -0
  40. package/dist/mjs/workspace-filesystem-job-types.mjs +106 -0
  41. package/dist/mjs/workspace-filesystem-jobs.mjs +51 -0
  42. package/dist/mjs/workspace-git-sync.mjs +264 -202
  43. package/dist/mjs/workspace-mount-hold-fence.mjs +95 -0
  44. package/dist/types/command-launcher.d.ts +42 -0
  45. package/dist/types/control-command-policy.d.ts +53 -0
  46. package/dist/types/main.d.ts +31 -19
  47. package/dist/types/project-checkout-garbage.d.ts +19 -2
  48. package/dist/types/project-workspace-state.d.ts +9 -9
  49. package/dist/types/project-worktrees.d.ts +49 -5
  50. package/dist/types/recovery-journal-protocol.d.ts +35 -0
  51. package/dist/types/recovery-journal-runtime.d.ts +12 -0
  52. package/dist/types/recovery-journal-stall-fixture.d.ts +1 -0
  53. package/dist/types/recovery-journal-thread.d.ts +1 -0
  54. package/dist/types/recovery-journal.d.ts +246 -0
  55. package/dist/types/recovery-store.d.ts +6 -0
  56. package/dist/types/session-file-mutations.d.ts +22 -0
  57. package/dist/types/workspace-command-sync-policy.d.ts +22 -7
  58. package/dist/types/workspace-filesystem-executor-thread.d.ts +1 -0
  59. package/dist/types/workspace-filesystem-executor.d.ts +123 -0
  60. package/dist/types/workspace-filesystem-job-types.d.ts +246 -0
  61. package/dist/types/workspace-filesystem-jobs.d.ts +7 -0
  62. package/dist/types/workspace-git-sync.d.ts +113 -7
  63. package/dist/types/workspace-mount-hold-fence.d.ts +42 -0
  64. package/package.json +1 -1
  65. package/dist/cjs/project-snapshot-recovery-runner.cjs +0 -171
  66. package/dist/mjs/project-snapshot-recovery-runner.mjs +0 -135
  67. package/dist/types/project-snapshot-recovery-runner.d.ts +0 -10
@@ -73,6 +73,14 @@ class WorkerRecoveryStore {
73
73
  close() {
74
74
  this.db.close();
75
75
  }
76
+ /**
77
+ * Run several store operations as one durable commit (one FULL fsync).
78
+ * Operations that open their own transaction nest as savepoints. A throw
79
+ * rolls the whole group back; callers treat that as a journal failure.
80
+ */
81
+ batch(operations) {
82
+ return this.db.transaction(operations)();
83
+ }
76
84
  row(requestId) {
77
85
  return this.db.query("SELECT * FROM operations WHERE request_id=? OR request_id=(SELECT operation_id FROM aliases WHERE request_id=?)").get(requestId, requestId);
78
86
  }
@@ -0,0 +1,61 @@
1
+ "use strict";
2
+ var __defProp = Object.defineProperty;
3
+ var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
4
+ var __getOwnPropNames = Object.getOwnPropertyNames;
5
+ var __hasOwnProp = Object.prototype.hasOwnProperty;
6
+ var __export = (target, all) => {
7
+ for (var name in all)
8
+ __defProp(target, name, { get: all[name], enumerable: true });
9
+ };
10
+ var __copyProps = (to, from, except, desc) => {
11
+ if (from && typeof from === "object" || typeof from === "function") {
12
+ for (let key of __getOwnPropNames(from))
13
+ if (!__hasOwnProp.call(to, key) && key !== except)
14
+ __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
15
+ }
16
+ return to;
17
+ };
18
+ var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
19
+ var session_file_mutations_exports = {};
20
+ __export(session_file_mutations_exports, {
21
+ SessionFileMutationTracker: () => SessionFileMutationTracker
22
+ });
23
+ module.exports = __toCommonJS(session_file_mutations_exports);
24
+ class SessionFileMutationTracker {
25
+ inFlight = /* @__PURE__ */ new Map();
26
+ /** Register a started mutation; it is forgotten when it settles, however it settles. */
27
+ track(sessionId, mutation) {
28
+ let mutations = this.inFlight.get(sessionId);
29
+ if (!mutations) {
30
+ mutations = /* @__PURE__ */ new Set();
31
+ this.inFlight.set(sessionId, mutations);
32
+ }
33
+ mutations.add(mutation);
34
+ const forget = () => {
35
+ const current = this.inFlight.get(sessionId);
36
+ if (!current) return;
37
+ current.delete(mutation);
38
+ if (current.size === 0) this.inFlight.delete(sessionId);
39
+ };
40
+ mutation.then(forget, forget);
41
+ return mutation;
42
+ }
43
+ count(sessionId) {
44
+ return this.inFlight.get(sessionId)?.size ?? 0;
45
+ }
46
+ /**
47
+ * Resolves once every mutation tracked for the session at the time of the
48
+ * call has settled. Mutations started later are not joined: the session's
49
+ * cancellation marker, recorded before this is awaited, makes their
50
+ * admission recheck refuse them.
51
+ */
52
+ settled(sessionId) {
53
+ const pending = [...this.inFlight.get(sessionId) ?? []];
54
+ if (pending.length === 0) return Promise.resolve();
55
+ return Promise.allSettled(pending).then(() => void 0);
56
+ }
57
+ }
58
+ // Annotate the CommonJS export names for ESM import in node:
59
+ 0 && (module.exports = {
60
+ SessionFileMutationTracker
61
+ });
@@ -416,6 +416,7 @@ function filesEqual(leftPath, rightPath, entry) {
416
416
  const length = Math.min(leftBuffer.length, entry.size - offset);
417
417
  const leftRead = import_node_fs.default.readSync(left, leftBuffer, 0, length, offset);
418
418
  const rightRead = import_node_fs.default.readSync(right, rightBuffer, 0, length, offset);
419
+ if (leftRead <= 0 || rightRead <= 0) return false;
419
420
  if (leftRead !== rightRead || !leftBuffer.subarray(0, leftRead).equals(rightBuffer.subarray(0, rightRead))) return false;
420
421
  offset += leftRead;
421
422
  }
@@ -25,22 +25,51 @@ __export(workspace_command_sync_policy_exports, {
25
25
  shouldSerializeWorkspaceCommand: () => shouldSerializeWorkspaceCommand
26
26
  });
27
27
  module.exports = __toCommonJS(workspace_command_sync_policy_exports);
28
+ var import_workspace_mount_hold_fence = require("./workspace-mount-hold-fence.cjs");
28
29
  function requiresCurrentSyncBarrier(target) {
29
30
  return target.type === "workspace" && target.rootProfile === "visible_projects";
30
31
  }
31
- async function reserveWorkspaceCommandAfterCurrentSync(target, coordinator, reserve) {
32
- if (!requiresCurrentSyncBarrier(target)) {
33
- reserve();
34
- return;
35
- }
32
+ async function reserveWorkspaceCommandAfterCurrentSync(target, coordinator, reserve, options = {}) {
36
33
  while (true) {
37
- const currentSync = coordinator.afterCurrent();
38
- await currentSync;
39
- if (currentSync !== coordinator.afterCurrent()) continue;
34
+ if (requiresCurrentSyncBarrier(target)) {
35
+ const currentSync = coordinator.afterCurrent();
36
+ await raceWithAbort(currentSync, options.signal);
37
+ if (currentSync !== coordinator.afterCurrent()) continue;
38
+ }
39
+ if (options.signal?.aborted) throw new import_workspace_mount_hold_fence.WorkspaceMountHoldAbortedError(abortReason(options.signal));
40
+ const hold = coordinator.mountHold(target, options.signal);
41
+ if (hold) {
42
+ await hold;
43
+ continue;
44
+ }
40
45
  reserve();
41
46
  return;
42
47
  }
43
48
  }
49
+ function raceWithAbort(waited, signal) {
50
+ if (!signal) return waited;
51
+ if (signal.aborted) return Promise.reject(new import_workspace_mount_hold_fence.WorkspaceMountHoldAbortedError(abortReason(signal)));
52
+ return new Promise((resolve, reject) => {
53
+ const onAbort = () => reject(new import_workspace_mount_hold_fence.WorkspaceMountHoldAbortedError(abortReason(signal)));
54
+ signal.addEventListener("abort", onAbort, { once: true });
55
+ waited.then(
56
+ () => {
57
+ signal.removeEventListener("abort", onAbort);
58
+ resolve();
59
+ },
60
+ (error) => {
61
+ signal.removeEventListener("abort", onAbort);
62
+ reject(error instanceof Error ? error : new Error(String(error)));
63
+ }
64
+ );
65
+ });
66
+ }
67
+ function abortReason(signal) {
68
+ const reason = signal.reason;
69
+ if (reason instanceof Error) return reason.message;
70
+ if (typeof reason === "string" && reason) return reason;
71
+ return "Workspace command reservation was aborted";
72
+ }
44
73
  function shouldSerializeWorkspaceCommand(target) {
45
74
  return target.type === "workspace" && target.rootProfile === "canonical_sync";
46
75
  }
@@ -0,0 +1,36 @@
1
+ "use strict";
2
+ var import_node_worker_threads = require("node:worker_threads");
3
+ var import_workspace_filesystem_job_types = require("./workspace-filesystem-job-types.cjs");
4
+ var import_workspace_filesystem_jobs = require("./workspace-filesystem-jobs.cjs");
5
+ if (import_node_worker_threads.isMainThread || !import_node_worker_threads.parentPort) {
6
+ throw new Error("The workspace filesystem executor thread must be started by the worker runtime");
7
+ }
8
+ const port = import_node_worker_threads.parentPort;
9
+ function runRequest(request) {
10
+ if (!(0, import_workspace_filesystem_job_types.isWorkspaceFilesystemJobKind)(request.kind)) {
11
+ return {
12
+ type: "failure",
13
+ id: request.id,
14
+ error: (0, import_workspace_filesystem_job_types.serializeWorkspaceFilesystemError)(new Error(`Unknown workspace filesystem job kind: ${String(request.kind)}`))
15
+ };
16
+ }
17
+ const operation = import_workspace_filesystem_jobs.workspaceFilesystemJobOperations[request.kind];
18
+ const previousEnvironment = process.env;
19
+ process.env = { ...request.environment };
20
+ try {
21
+ const result = operation(request.input, {
22
+ progress: (progress) => port.postMessage({ type: "progress", id: request.id, progress })
23
+ });
24
+ return { type: "result", id: request.id, result };
25
+ } catch (error) {
26
+ return { type: "failure", id: request.id, error: (0, import_workspace_filesystem_job_types.serializeWorkspaceFilesystemError)(error) };
27
+ } finally {
28
+ process.env = previousEnvironment;
29
+ }
30
+ }
31
+ port.on("message", (message) => {
32
+ if (!message || typeof message !== "object") return;
33
+ const request = message;
34
+ if (request.type !== "run" || typeof request.id !== "number") return;
35
+ port.postMessage(runRequest(request));
36
+ });
@@ -0,0 +1,327 @@
1
+ "use strict";
2
+ var __create = Object.create;
3
+ var __defProp = Object.defineProperty;
4
+ var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
5
+ var __getOwnPropNames = Object.getOwnPropertyNames;
6
+ var __getProtoOf = Object.getPrototypeOf;
7
+ var __hasOwnProp = Object.prototype.hasOwnProperty;
8
+ var __export = (target, all) => {
9
+ for (var name in all)
10
+ __defProp(target, name, { get: all[name], enumerable: true });
11
+ };
12
+ var __copyProps = (to, from, except, desc) => {
13
+ if (from && typeof from === "object" || typeof from === "function") {
14
+ for (let key of __getOwnPropNames(from))
15
+ if (!__hasOwnProp.call(to, key) && key !== except)
16
+ __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
17
+ }
18
+ return to;
19
+ };
20
+ var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
21
+ // If the importer is in node compatibility mode or this is not an ESM
22
+ // file that has been converted to a CommonJS file using a Babel-
23
+ // compatible transform (i.e. "__esModule" has not been set), then set
24
+ // "default" to the CommonJS "module.exports" for node compatibility.
25
+ isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target,
26
+ mod
27
+ ));
28
+ var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
29
+ var workspace_filesystem_executor_exports = {};
30
+ __export(workspace_filesystem_executor_exports, {
31
+ WORKSPACE_FILESYSTEM_EXECUTOR_MAX_QUEUED: () => WORKSPACE_FILESYSTEM_EXECUTOR_MAX_QUEUED,
32
+ WORKSPACE_FILESYSTEM_EXECUTOR_QUEUE_TIMEOUT_MS: () => WORKSPACE_FILESYSTEM_EXECUTOR_QUEUE_TIMEOUT_MS,
33
+ WORKSPACE_FILESYSTEM_EXECUTOR_STALL_LOG_INTERVAL_MS: () => WORKSPACE_FILESYSTEM_EXECUTOR_STALL_LOG_INTERVAL_MS,
34
+ WorkspaceFilesystemExecutor: () => WorkspaceFilesystemExecutor,
35
+ WorkspaceFilesystemExecutorError: () => WorkspaceFilesystemExecutorError,
36
+ WorkspaceFilesystemExecutorThreadExitedError: () => WorkspaceFilesystemExecutorThreadExitedError,
37
+ installWorkspaceFilesystemExecutorFailureHandler: () => installWorkspaceFilesystemExecutorFailureHandler,
38
+ workspaceFilesystemExecutor: () => workspaceFilesystemExecutor,
39
+ workspaceFilesystemExecutorTestHarness: () => workspaceFilesystemExecutorTestHarness,
40
+ workspaceFilesystemExecutorThreadModulePath: () => workspaceFilesystemExecutorThreadModulePath
41
+ });
42
+ module.exports = __toCommonJS(workspace_filesystem_executor_exports);
43
+ var import_node_path = __toESM(require("node:path"), 1);
44
+ var import_node_worker_threads = require("node:worker_threads");
45
+ var import_git_process_environment = require("./git-process-environment.cjs");
46
+ var import_workspace_filesystem_job_types = require("./workspace-filesystem-job-types.cjs");
47
+ const WORKSPACE_FILESYSTEM_EXECUTOR_MAX_QUEUED = 32;
48
+ const WORKSPACE_FILESYSTEM_EXECUTOR_QUEUE_TIMEOUT_MS = 5 * 6e4;
49
+ const WORKSPACE_FILESYSTEM_EXECUTOR_STALL_LOG_INTERVAL_MS = 3e4;
50
+ class WorkspaceFilesystemExecutorError extends Error {
51
+ name = "WorkspaceFilesystemExecutorError";
52
+ }
53
+ class WorkspaceFilesystemExecutorThreadExitedError extends WorkspaceFilesystemExecutorError {
54
+ constructor(exitCode, description, cause) {
55
+ super(
56
+ `Workspace filesystem executor thread exited${exitCode === null ? "" : ` with code ${exitCode}`} while running ${description}; the filesystem state is whatever a process crash would have left`,
57
+ cause === void 0 ? void 0 : { cause }
58
+ );
59
+ this.exitCode = exitCode;
60
+ this.description = description;
61
+ }
62
+ exitCode;
63
+ description;
64
+ }
65
+ class WorkspaceFilesystemExecutor {
66
+ threadModulePath;
67
+ log;
68
+ now;
69
+ maxQueued;
70
+ queueTimeoutMs;
71
+ stallLogIntervalMs;
72
+ worker = null;
73
+ queue = [];
74
+ running = null;
75
+ nextJobId = 1;
76
+ failed = null;
77
+ closed = false;
78
+ /** Invoked once when the executor fails closed (thread exit under a running job). */
79
+ onFailure;
80
+ constructor(options) {
81
+ this.threadModulePath = options.threadModulePath;
82
+ this.log = options.log ?? ((line) => process.stderr.write(`${line}
83
+ `));
84
+ this.now = options.now ?? (() => Date.now());
85
+ this.maxQueued = options.maxQueued ?? WORKSPACE_FILESYSTEM_EXECUTOR_MAX_QUEUED;
86
+ this.queueTimeoutMs = options.queueTimeoutMs ?? WORKSPACE_FILESYSTEM_EXECUTOR_QUEUE_TIMEOUT_MS;
87
+ this.stallLogIntervalMs = options.stallLogIntervalMs ?? WORKSPACE_FILESYSTEM_EXECUTOR_STALL_LOG_INTERVAL_MS;
88
+ }
89
+ /**
90
+ * Run one job to completion on the executor thread. Resolves with the
91
+ * job's result; rejects with the job's own error (properties preserved),
92
+ * with a queue-wait or capacity error when the job never started, or with
93
+ * WorkspaceFilesystemExecutorThreadExitedError when the thread died under it.
94
+ */
95
+ run(kind, input, options = {}) {
96
+ const description = (0, import_workspace_filesystem_job_types.describeWorkspaceFilesystemJob)(kind, input);
97
+ if (this.closed)
98
+ return Promise.reject(new WorkspaceFilesystemExecutorError(`Workspace filesystem executor is closed; refusing ${description}`));
99
+ if (this.failed) return Promise.reject(new WorkspaceFilesystemExecutorError(`${this.failed}; refusing ${description}`));
100
+ if (this.queue.length >= this.maxQueued) {
101
+ return Promise.reject(
102
+ new WorkspaceFilesystemExecutorError(
103
+ `Workspace filesystem executor queue is full (${this.queue.length} waiting behind ${this.describeRunning()}); refusing ${description}`
104
+ )
105
+ );
106
+ }
107
+ return new Promise((resolve, reject) => {
108
+ const job = {
109
+ id: this.nextJobId++,
110
+ kind,
111
+ input,
112
+ description,
113
+ onProgress: options.onProgress,
114
+ resolve: (result) => resolve(result),
115
+ reject,
116
+ queuedAtMs: this.now(),
117
+ queueTimer: void 0
118
+ };
119
+ this.queue.push(job);
120
+ if (this.running) {
121
+ job.queueTimer = setTimeout(() => this.expireQueued(job), this.queueTimeoutMs);
122
+ }
123
+ this.dispatch();
124
+ });
125
+ }
126
+ status() {
127
+ const running = this.running;
128
+ return {
129
+ running: running ? {
130
+ kind: running.kind,
131
+ description: running.description,
132
+ startedAtMs: running.startedAtMs,
133
+ ageMs: this.now() - running.startedAtMs
134
+ } : null,
135
+ queued: this.queue.length,
136
+ failed: this.failed
137
+ };
138
+ }
139
+ /**
140
+ * Stop accepting jobs, fail everything still queued and ask the thread to
141
+ * stop. A running job settles when the thread actually exits; a thread
142
+ * blocked in the kernel only exits with the process, which is the point.
143
+ */
144
+ close() {
145
+ if (this.closed) return;
146
+ this.closed = true;
147
+ for (const job of this.queue.splice(0)) {
148
+ if (job.queueTimer) clearTimeout(job.queueTimer);
149
+ job.reject(new WorkspaceFilesystemExecutorError(`Workspace filesystem executor is closed; ${job.description} did not start`));
150
+ }
151
+ const worker = this.worker;
152
+ if (worker && !this.running) {
153
+ this.worker = null;
154
+ void worker.terminate();
155
+ } else if (worker) {
156
+ void worker.terminate();
157
+ }
158
+ }
159
+ describeRunning() {
160
+ const running = this.running;
161
+ if (!running) return "an idle executor";
162
+ return `${running.description} (running for ${Math.round((this.now() - running.startedAtMs) / 1e3)}s)`;
163
+ }
164
+ expireQueued(job) {
165
+ const index = this.queue.indexOf(job);
166
+ if (index < 0) return;
167
+ this.queue.splice(index, 1);
168
+ job.queueTimer = void 0;
169
+ job.reject(
170
+ new WorkspaceFilesystemExecutorError(
171
+ `Workspace filesystem executor is blocked: ${this.describeRunning()} while ${job.description} waited ${Math.round((this.now() - job.queuedAtMs) / 1e3)}s for its turn; filesystem I/O may be stalled`
172
+ )
173
+ );
174
+ }
175
+ dispatch() {
176
+ if (this.running || this.closed || this.failed) return;
177
+ const job = this.queue.shift();
178
+ if (!job) {
179
+ this.worker?.unref();
180
+ return;
181
+ }
182
+ if (job.queueTimer) {
183
+ clearTimeout(job.queueTimer);
184
+ job.queueTimer = void 0;
185
+ }
186
+ let worker;
187
+ try {
188
+ worker = this.ensureWorker();
189
+ } catch (error) {
190
+ job.reject(error instanceof Error ? error : new Error(String(error)));
191
+ this.dispatch();
192
+ return;
193
+ }
194
+ const environment = {};
195
+ for (const [key, value] of Object.entries((0, import_git_process_environment.workerGitProcessEnvironment)(process.env))) if (value !== void 0) environment[key] = value;
196
+ const request = { type: "run", id: job.id, kind: job.kind, input: job.input, environment };
197
+ const startedAtMs = this.now();
198
+ const running = {
199
+ ...job,
200
+ startedAtMs,
201
+ stallTimer: setInterval(() => {
202
+ this.log(
203
+ `[r5d-worker] workspace filesystem job ${running.kind} (${running.description}) has run for ${Math.round((this.now() - startedAtMs) / 1e3)}s; filesystem I/O may be stalled (${this.queue.length} job(s) queued behind it)`
204
+ );
205
+ }, this.stallLogIntervalMs)
206
+ };
207
+ running.stallTimer.unref();
208
+ this.running = running;
209
+ worker.ref();
210
+ try {
211
+ worker.postMessage(request);
212
+ } catch (error) {
213
+ this.settleRunning(running, () => running.reject(error instanceof Error ? error : new Error(String(error))));
214
+ }
215
+ }
216
+ settleRunning(job, settle) {
217
+ if (this.running !== job) return;
218
+ clearInterval(job.stallTimer);
219
+ this.running = null;
220
+ settle();
221
+ this.dispatch();
222
+ }
223
+ ensureWorker() {
224
+ if (this.worker) return this.worker;
225
+ const worker = new import_node_worker_threads.Worker(this.threadModulePath);
226
+ this.worker = worker;
227
+ worker.on("message", (message) => this.onReply(worker, message));
228
+ worker.on("error", (error) => {
229
+ this.log(`[r5d-worker] workspace filesystem executor thread error: ${error.stack ?? error.message}`);
230
+ this.lastThreadError = error;
231
+ });
232
+ worker.on("exit", (code) => this.onExit(worker, code));
233
+ worker.unref();
234
+ return worker;
235
+ }
236
+ lastThreadError;
237
+ onReply(worker, message) {
238
+ if (this.worker !== worker) return;
239
+ const running = this.running;
240
+ if (!running || !message || typeof message !== "object" || message.id !== running.id) return;
241
+ if (message.type === "progress") {
242
+ running.onProgress?.(message.progress);
243
+ return;
244
+ }
245
+ if (message.type === "result") {
246
+ this.settleRunning(running, () => running.resolve(message.result));
247
+ return;
248
+ }
249
+ if (message.type === "failure") {
250
+ this.settleRunning(running, () => running.reject((0, import_workspace_filesystem_job_types.restoreWorkspaceFilesystemError)(message.error)));
251
+ }
252
+ }
253
+ onExit(worker, code) {
254
+ if (this.worker !== worker) return;
255
+ this.worker = null;
256
+ const cause = this.lastThreadError;
257
+ this.lastThreadError = void 0;
258
+ const running = this.running;
259
+ if (!running) {
260
+ if (!this.closed) this.dispatch();
261
+ return;
262
+ }
263
+ const failure = new WorkspaceFilesystemExecutorThreadExitedError(code, running.description, cause);
264
+ if (!this.closed) {
265
+ this.failed = `Workspace filesystem executor thread exited with code ${code} while running ${running.description}; the worker must restart and recover before it mutates the workspace again`;
266
+ this.log(`[r5d-worker] ${this.failed}`);
267
+ }
268
+ this.settleRunning(running, () => running.reject(failure));
269
+ for (const job of this.queue.splice(0)) {
270
+ if (job.queueTimer) clearTimeout(job.queueTimer);
271
+ job.reject(
272
+ new WorkspaceFilesystemExecutorError(
273
+ `${this.failed ?? "Workspace filesystem executor is closed"}; ${job.description} did not start`
274
+ )
275
+ );
276
+ }
277
+ if (!this.closed) this.onFailure?.(failure);
278
+ }
279
+ }
280
+ function workspaceFilesystemExecutorThreadModulePath() {
281
+ return import_node_path.default.join(import_node_path.default.dirname(__filename), `workspace-filesystem-executor-thread${import_node_path.default.extname(__filename)}`);
282
+ }
283
+ let processExecutor = null;
284
+ let inlineRunner = null;
285
+ function workspaceFilesystemExecutor() {
286
+ if (inlineRunner) return inlineRunner;
287
+ processExecutor ??= new WorkspaceFilesystemExecutor({ threadModulePath: workspaceFilesystemExecutorThreadModulePath() });
288
+ return processExecutor;
289
+ }
290
+ function installWorkspaceFilesystemExecutorFailureHandler(handler) {
291
+ processExecutor ??= new WorkspaceFilesystemExecutor({ threadModulePath: workspaceFilesystemExecutorThreadModulePath() });
292
+ processExecutor.onFailure = handler;
293
+ }
294
+ const workspaceFilesystemExecutorTestHarness = {
295
+ async runJobsInline(body) {
296
+ const { workspaceFilesystemJobOperations } = await import("./workspace-filesystem-jobs.cjs");
297
+ const previous = inlineRunner;
298
+ inlineRunner = {
299
+ async run(kind, input, options = {}) {
300
+ await Promise.resolve();
301
+ const operation = workspaceFilesystemJobOperations[kind];
302
+ return operation(input, { progress: (progress) => options.onProgress?.(progress) });
303
+ }
304
+ };
305
+ try {
306
+ return await body();
307
+ } finally {
308
+ inlineRunner = previous;
309
+ }
310
+ },
311
+ processExecutor() {
312
+ return processExecutor;
313
+ }
314
+ };
315
+ // Annotate the CommonJS export names for ESM import in node:
316
+ 0 && (module.exports = {
317
+ WORKSPACE_FILESYSTEM_EXECUTOR_MAX_QUEUED,
318
+ WORKSPACE_FILESYSTEM_EXECUTOR_QUEUE_TIMEOUT_MS,
319
+ WORKSPACE_FILESYSTEM_EXECUTOR_STALL_LOG_INTERVAL_MS,
320
+ WorkspaceFilesystemExecutor,
321
+ WorkspaceFilesystemExecutorError,
322
+ WorkspaceFilesystemExecutorThreadExitedError,
323
+ installWorkspaceFilesystemExecutorFailureHandler,
324
+ workspaceFilesystemExecutor,
325
+ workspaceFilesystemExecutorTestHarness,
326
+ workspaceFilesystemExecutorThreadModulePath
327
+ });
@@ -0,0 +1,134 @@
1
+ "use strict";
2
+ var __defProp = Object.defineProperty;
3
+ var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
4
+ var __getOwnPropNames = Object.getOwnPropertyNames;
5
+ var __hasOwnProp = Object.prototype.hasOwnProperty;
6
+ var __export = (target, all) => {
7
+ for (var name in all)
8
+ __defProp(target, name, { get: all[name], enumerable: true });
9
+ };
10
+ var __copyProps = (to, from, except, desc) => {
11
+ if (from && typeof from === "object" || typeof from === "function") {
12
+ for (let key of __getOwnPropNames(from))
13
+ if (!__hasOwnProp.call(to, key) && key !== except)
14
+ __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
15
+ }
16
+ return to;
17
+ };
18
+ var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
19
+ var workspace_filesystem_job_types_exports = {};
20
+ __export(workspace_filesystem_job_types_exports, {
21
+ WORKSPACE_FILESYSTEM_JOB_KINDS: () => WORKSPACE_FILESYSTEM_JOB_KINDS,
22
+ describeWorkspaceFilesystemJob: () => describeWorkspaceFilesystemJob,
23
+ isWorkspaceFilesystemJobKind: () => isWorkspaceFilesystemJobKind,
24
+ restoreWorkspaceFilesystemError: () => restoreWorkspaceFilesystemError,
25
+ serializeWorkspaceFilesystemError: () => serializeWorkspaceFilesystemError
26
+ });
27
+ module.exports = __toCommonJS(workspace_filesystem_job_types_exports);
28
+ const WORKSPACE_FILESYSTEM_JOB_KINDS = [
29
+ "checkout_durability_recovery",
30
+ "hydration_recovery",
31
+ "hydration_transaction",
32
+ "hydration_raw",
33
+ "checkout_transition_begin",
34
+ "checkout_transition_complete",
35
+ "outer_checkout_fsync",
36
+ "projection_merge",
37
+ "projection_mirror",
38
+ "projection_outer_apply",
39
+ "projection_receipt",
40
+ "project_snapshot_create",
41
+ "project_snapshot_apply",
42
+ "project_snapshot_persist",
43
+ "project_snapshot_rollback",
44
+ "project_snapshots_recover",
45
+ "project_branch_create",
46
+ "project_checkout_path_move"
47
+ ];
48
+ function isWorkspaceFilesystemJobKind(value) {
49
+ return typeof value === "string" && WORKSPACE_FILESYSTEM_JOB_KINDS.includes(value);
50
+ }
51
+ function summarizeIds(ids) {
52
+ if (ids.length === 0) return "no mounts";
53
+ const shown = ids.slice(0, 3).join(", ");
54
+ return ids.length > 3 ? `${ids.length} mounts (${shown}, \u2026)` : `${ids.length === 1 ? "mount" : "mounts"} ${shown}`;
55
+ }
56
+ function describeWorkspaceFilesystemJob(kind, input) {
57
+ switch (kind) {
58
+ case "checkout_durability_recovery":
59
+ case "hydration_recovery":
60
+ case "checkout_transition_begin":
61
+ case "checkout_transition_complete":
62
+ case "outer_checkout_fsync":
63
+ return `${kind.replaceAll("_", " ")} of ${input.workspacePath}`;
64
+ case "hydration_transaction": {
65
+ const hydration = input;
66
+ return `hydration of ${summarizeIds(hydration.hydrationMounts.map(({ id }) => id))} (durability ${summarizeIds(hydration.advancedDurabilityMounts.map(({ id }) => id))})`;
67
+ }
68
+ case "hydration_raw":
69
+ return `pre-clone hydration of ${summarizeIds(input.mounts.map(({ id }) => id))}`;
70
+ case "projection_merge":
71
+ return `merge projection of mount ${input.mount.id}`;
72
+ case "projection_mirror":
73
+ return `projection of mount ${input.mount.id}`;
74
+ case "projection_outer_apply": {
75
+ const apply = input;
76
+ return `outer-tree apply (${apply.removeMounts.length} removal(s), ${apply.materialize.length} merged subtree(s), ${apply.restoreFromHead.length} deferred restore(s))`;
77
+ }
78
+ case "projection_receipt":
79
+ return `projection receipt for ${summarizeIds(input.fsyncMounts.map(({ id }) => id))}`;
80
+ case "project_snapshot_create":
81
+ return `checkout snapshot of ${input.projectRoot}`;
82
+ case "project_snapshot_apply":
83
+ return `checkout snapshot restore into ${input.mirrors.map(({ targetPath }) => targetPath).join(", ") || "no checkouts"}`;
84
+ case "project_snapshot_persist":
85
+ return `checkout durability of ${input.snapshot.manifest.projectRoot}`;
86
+ case "project_snapshot_rollback":
87
+ return `checkout snapshot rollback of ${input.snapshot.manifest.projectRoot}`;
88
+ case "project_snapshots_recover":
89
+ return `interrupted checkout snapshot recovery under ${input.temporaryRoot}`;
90
+ case "project_branch_create": {
91
+ const create = input;
92
+ return `branch creation ${create.branchName} from ${create.sourceBranchName} in ${create.projectRoot}`;
93
+ }
94
+ case "project_checkout_path_move": {
95
+ const move = input;
96
+ return `checkout path move ${move.oldProjectRoot} \u2192 ${move.newProjectRoot}`;
97
+ }
98
+ }
99
+ }
100
+ function serializeWorkspaceFilesystemError(error, depth = 0) {
101
+ if (!(error instanceof Error)) return { name: "Error", message: String(error) };
102
+ const record = error;
103
+ const serialized = { name: error.name, message: error.message };
104
+ if (typeof error.stack === "string") serialized.stack = error.stack;
105
+ if (typeof record.code === "string") serialized.code = record.code;
106
+ if (record.branchMayExist === true) serialized.branchMayExist = true;
107
+ if (Array.isArray(record.cleanupFailures) && record.cleanupFailures.every((failure) => typeof failure === "string")) {
108
+ serialized.cleanupFailures = [...record.cleanupFailures];
109
+ }
110
+ if (depth < 4) {
111
+ if (Array.isArray(record.errors)) serialized.errors = record.errors.map((inner) => serializeWorkspaceFilesystemError(inner, depth + 1));
112
+ if (record.cause !== void 0) serialized.cause = serializeWorkspaceFilesystemError(record.cause, depth + 1);
113
+ }
114
+ return serialized;
115
+ }
116
+ function restoreWorkspaceFilesystemError(serialized) {
117
+ const cause = serialized.cause ? restoreWorkspaceFilesystemError(serialized.cause) : void 0;
118
+ const options = cause ? { cause } : void 0;
119
+ const error = serialized.errors ? new AggregateError(serialized.errors.map(restoreWorkspaceFilesystemError), serialized.message, options) : new Error(serialized.message, options);
120
+ error.name = serialized.name;
121
+ if (serialized.stack) error.stack = serialized.stack;
122
+ if (serialized.code !== void 0) error.code = serialized.code;
123
+ if (serialized.branchMayExist) error.branchMayExist = true;
124
+ if (serialized.cleanupFailures) error.cleanupFailures = [...serialized.cleanupFailures];
125
+ return error;
126
+ }
127
+ // Annotate the CommonJS export names for ESM import in node:
128
+ 0 && (module.exports = {
129
+ WORKSPACE_FILESYSTEM_JOB_KINDS,
130
+ describeWorkspaceFilesystemJob,
131
+ isWorkspaceFilesystemJobKind,
132
+ restoreWorkspaceFilesystemError,
133
+ serializeWorkspaceFilesystemError
134
+ });