virtualmatter 0.6.5 → 0.6.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 +38 -7
- package/dist/agent-logs.js +33 -5
- package/dist/agentfiles.js +2 -2
- package/dist/agents-md.generated.js +1 -1
- package/dist/atomo.js +307 -23
- package/dist/files.js +200 -19
- package/dist/ignore.js +12 -0
- package/dist/index.js +227 -26
- package/dist/lock.js +208 -0
- package/dist/log-events.js +2 -2
- package/dist/mcp.js +137 -3
- package/dist/pull.js +2 -2
- package/dist/sync.js +305 -36
- package/dist/world-status.js +161 -0
- package/package.json +1 -1
package/dist/sync.js
CHANGED
|
@@ -11,16 +11,38 @@ import { startLogSync } from "./agent-logs.js";
|
|
|
11
11
|
* conflict: the local file stays exactly as it is, the remote version lands
|
|
12
12
|
* next to it as <name>.remote-conflict, and nothing is pushed until you save.
|
|
13
13
|
* Conflicts never kill the loop.
|
|
14
|
+
*
|
|
15
|
+
* Files too large to push back (over the session API's write cap - terrain
|
|
16
|
+
* and voxel data) are never pulled automatically: a two-way sync cannot keep
|
|
17
|
+
* them in step anyway, and re-downloading a 370 MB terrain on every engine
|
|
18
|
+
* save, three syncs at once, competed with the world it was mirroring
|
|
19
|
+
* (2026-10-07). `virtualmatter fetch <path>` downloads one on request.
|
|
14
20
|
*/
|
|
15
21
|
import fs from "node:fs";
|
|
16
22
|
import path from "node:path";
|
|
17
|
-
import { ConflictError, MAX_WRITE_BYTES } from "./files.js";
|
|
23
|
+
import { ConflictError, MAX_WRITE_BYTES, SessionLostError, removeStaleDownloadTemps, } from "./files.js";
|
|
18
24
|
import { isIgnoredPath } from "./ignore.js";
|
|
25
|
+
import { supervisingPids, watchSupervisors } from "./lock.js";
|
|
19
26
|
import { loadState, adoptLocal, localHash, recordLocal, saveState, sha256Hex, syncedHash, } from "./state.js";
|
|
20
27
|
const consoleLogger = {
|
|
21
28
|
info: (m) => console.log(m),
|
|
22
29
|
warn: (m) => console.warn(m),
|
|
23
30
|
};
|
|
31
|
+
/** Errors that must stop a running sync instead of being logged and skipped. */
|
|
32
|
+
function rethrowFatal(err) {
|
|
33
|
+
if (err instanceof SessionLostError)
|
|
34
|
+
throw err;
|
|
35
|
+
}
|
|
36
|
+
/** size:mtime of a file, or null when it does not exist - enough to tell that it was saved. */
|
|
37
|
+
function statKey(abs) {
|
|
38
|
+
try {
|
|
39
|
+
const st = fs.statSync(abs);
|
|
40
|
+
return `${st.size}:${st.mtimeMs}`;
|
|
41
|
+
}
|
|
42
|
+
catch {
|
|
43
|
+
return null;
|
|
44
|
+
}
|
|
45
|
+
}
|
|
24
46
|
/** How long after our own local write of a pulled file we ignore watcher events for it. */
|
|
25
47
|
const SELF_WRITE_GRACE_MS = 2000;
|
|
26
48
|
export const CONFLICT_SUFFIX = ".remote-conflict";
|
|
@@ -36,11 +58,15 @@ export class SyncEngine {
|
|
|
36
58
|
dirty = false;
|
|
37
59
|
/** relPath -> "size:mtime" of an oversized file already warned about, so the scan does not repeat it every few seconds. */
|
|
38
60
|
tooBigWarned = new Map();
|
|
61
|
+
/** relPath -> remote etag of a too-large file already reported as not pulled. */
|
|
62
|
+
largeSkipWarned = new Map();
|
|
63
|
+
includeLarge;
|
|
39
64
|
constructor(client, dir, state, opts = {}) {
|
|
40
65
|
this.client = client;
|
|
41
66
|
this.dir = dir;
|
|
42
67
|
this.log = opts.logger ?? consoleLogger;
|
|
43
68
|
this.now = opts.now ?? Date.now;
|
|
69
|
+
this.includeLarge = opts.includeLarge ?? false;
|
|
44
70
|
this.state = state;
|
|
45
71
|
this.state.local ??= {};
|
|
46
72
|
}
|
|
@@ -103,36 +129,92 @@ export class SyncEngine {
|
|
|
103
129
|
this.dirty = false;
|
|
104
130
|
this.persist();
|
|
105
131
|
}
|
|
132
|
+
/**
|
|
133
|
+
* Download the world's copy over the local file. The local file is checked
|
|
134
|
+
* again right before the rename: a save that landed while the download
|
|
135
|
+
* streamed would otherwise be replaced (the check that chose to pull is
|
|
136
|
+
* stale by then), so the world's copy goes beside it as a conflict instead.
|
|
137
|
+
* Returns "conflict" when that happened.
|
|
138
|
+
*/
|
|
106
139
|
async pullFile(relPath, etag) {
|
|
140
|
+
const abs = this.abs(relPath);
|
|
141
|
+
const before = statKey(abs);
|
|
107
142
|
this.recordSelfWrite(relPath);
|
|
108
143
|
// streamed: voxel and terrain files run to several GB
|
|
109
|
-
const
|
|
144
|
+
const res = await this.client.download(relPath, abs, {
|
|
145
|
+
commitPath: () => (statKey(abs) === before ? abs : abs + CONFLICT_SUFFIX),
|
|
146
|
+
});
|
|
147
|
+
if (res.path !== undefined && res.path !== abs) {
|
|
148
|
+
// we never wrote the file: the save that diverted us must reach the watcher
|
|
149
|
+
this.selfWrites.delete(relPath);
|
|
150
|
+
return (await this.settleConflict(relPath, res.etag ?? etag, res.hash)) ? "conflict" : "pulled";
|
|
151
|
+
}
|
|
110
152
|
this.recordSelfWrite(relPath); // the rename is the write the watcher sees; restart the grace from it
|
|
111
|
-
this.state.etags[relPath] =
|
|
112
|
-
adoptLocal(this.state, this.dir, relPath, hash);
|
|
153
|
+
this.state.etags[relPath] = res.etag ?? etag;
|
|
154
|
+
adoptLocal(this.state, this.dir, relPath, res.hash);
|
|
113
155
|
this.persist();
|
|
156
|
+
return "pulled";
|
|
114
157
|
}
|
|
115
158
|
/**
|
|
116
159
|
* Both sides changed: keep the local file untouched, put the remote copy
|
|
117
160
|
* beside it, and point the stored etag at the remote version so that the
|
|
118
161
|
* next deliberate save of the local file is what wins. The local record is
|
|
119
|
-
* left alone, so the file still reads as locally changed.
|
|
162
|
+
* left alone, so the file still reads as locally changed. Returns false
|
|
163
|
+
* when it was no conflict after all (see settleConflict).
|
|
120
164
|
*/
|
|
121
165
|
async writeConflict(relPath, remoteEtag) {
|
|
122
166
|
try {
|
|
123
167
|
const conflictPath = this.abs(relPath) + CONFLICT_SUFFIX;
|
|
124
|
-
const { etag } = await this.client.download(relPath, conflictPath);
|
|
125
|
-
|
|
126
|
-
if (e)
|
|
127
|
-
this.state.etags[relPath] = e;
|
|
128
|
-
this.persist();
|
|
129
|
-
this.log.warn(`conflict on ${relPath}: it changed both here and in the world. Your file is untouched; ` +
|
|
130
|
-
`the world's version is saved as ${relPath}${CONFLICT_SUFFIX} - merge, save, and it will push.`);
|
|
168
|
+
const { etag, hash } = await this.client.download(relPath, conflictPath);
|
|
169
|
+
return await this.settleConflict(relPath, etag ?? remoteEtag, hash);
|
|
131
170
|
}
|
|
132
171
|
catch (err) {
|
|
172
|
+
rethrowFatal(err);
|
|
133
173
|
this.log.warn(`conflict on ${relPath}, and fetching the remote version failed: ${String(err)}`);
|
|
174
|
+
return true;
|
|
134
175
|
}
|
|
135
176
|
}
|
|
177
|
+
/**
|
|
178
|
+
* The world's copy of `relPath` (content hash `remoteHash`) now sits in the
|
|
179
|
+
* conflict sidecar. When it is byte-identical to the local file there is
|
|
180
|
+
* nothing to merge - a push that landed but whose answer was lost, or
|
|
181
|
+
* another process that pushed the same save - so the sidecar goes and the
|
|
182
|
+
* file is recorded as in sync. Reporting those as conflicts (one on every
|
|
183
|
+
* push, 2026-10-07) is how real ones stop being noticed. Returns whether it
|
|
184
|
+
* was a real conflict.
|
|
185
|
+
*/
|
|
186
|
+
async settleConflict(relPath, remoteEtag, remoteHash) {
|
|
187
|
+
const conflictPath = this.abs(relPath) + CONFLICT_SUFFIX;
|
|
188
|
+
if (localHash(this.state, this.dir, relPath) === remoteHash) {
|
|
189
|
+
fs.rmSync(conflictPath, { force: true });
|
|
190
|
+
if (remoteEtag)
|
|
191
|
+
this.state.etags[relPath] = remoteEtag;
|
|
192
|
+
adoptLocal(this.state, this.dir, relPath, remoteHash);
|
|
193
|
+
this.persist();
|
|
194
|
+
return false;
|
|
195
|
+
}
|
|
196
|
+
if (remoteEtag)
|
|
197
|
+
this.state.etags[relPath] = remoteEtag;
|
|
198
|
+
this.persist();
|
|
199
|
+
this.log.warn(`conflict on ${relPath}: it changed both here and in the world. Your file is untouched; ` +
|
|
200
|
+
`the world's version is saved as ${relPath}${CONFLICT_SUFFIX} - merge, save, and it will push.`);
|
|
201
|
+
return true;
|
|
202
|
+
}
|
|
203
|
+
/**
|
|
204
|
+
* A file too large to push back, which sync therefore leaves alone in the
|
|
205
|
+
* world (see the header). Reported once per remote version.
|
|
206
|
+
*/
|
|
207
|
+
skipLarge(file) {
|
|
208
|
+
if (this.includeLarge || file.size <= MAX_WRITE_BYTES)
|
|
209
|
+
return false;
|
|
210
|
+
if (this.largeSkipWarned.get(file.path) !== file.etag) {
|
|
211
|
+
this.largeSkipWarned.set(file.path, file.etag);
|
|
212
|
+
const mb = (file.size / (1024 * 1024)).toFixed(1);
|
|
213
|
+
this.log.info(`not pulling ${file.path} (${mb} MB, changed in the world): sync leaves files too large to push back alone. ` +
|
|
214
|
+
`To download it: npx virtualmatter fetch ${JSON.stringify(file.path)}`);
|
|
215
|
+
}
|
|
216
|
+
return true;
|
|
217
|
+
}
|
|
136
218
|
/**
|
|
137
219
|
* A conflict the user has not resolved yet: the sidecar is still there and
|
|
138
220
|
* the local file has not been saved since it was written. Such a file is
|
|
@@ -173,8 +255,12 @@ export class SyncEngine {
|
|
|
173
255
|
const known = this.state.etags[file.path];
|
|
174
256
|
const current = localHash(this.state, this.dir, file.path);
|
|
175
257
|
if (current === null) {
|
|
176
|
-
|
|
177
|
-
|
|
258
|
+
if (this.skipLarge(file))
|
|
259
|
+
continue;
|
|
260
|
+
if ((await this.pullFile(file.path, file.etag)) === "conflict")
|
|
261
|
+
conflicts++;
|
|
262
|
+
else
|
|
263
|
+
pulled++;
|
|
178
264
|
continue;
|
|
179
265
|
}
|
|
180
266
|
if (current === file.etag) {
|
|
@@ -191,7 +277,12 @@ export class SyncEngine {
|
|
|
191
277
|
const remoteChanged = known !== file.etag;
|
|
192
278
|
const localChanged = this.localChanged(file.path);
|
|
193
279
|
if (!localChanged && remoteChanged) {
|
|
194
|
-
|
|
280
|
+
if (this.skipLarge(file))
|
|
281
|
+
continue;
|
|
282
|
+
if ((await this.pullFile(file.path, file.etag)) === "conflict") {
|
|
283
|
+
conflicts++;
|
|
284
|
+
continue;
|
|
285
|
+
}
|
|
195
286
|
this.log.info(`pulled ${file.path}`);
|
|
196
287
|
pulled++;
|
|
197
288
|
}
|
|
@@ -201,12 +292,17 @@ export class SyncEngine {
|
|
|
201
292
|
`Merge, save the file (or delete the ${CONFLICT_SUFFIX} file), and it will push.`);
|
|
202
293
|
continue;
|
|
203
294
|
}
|
|
204
|
-
if (await this.pushFile(file.path))
|
|
295
|
+
if ((await this.pushFile(file.path)) === "pushed")
|
|
205
296
|
pushed++;
|
|
206
297
|
}
|
|
207
298
|
else if (localChanged && remoteChanged) {
|
|
208
|
-
|
|
209
|
-
|
|
299
|
+
if (file.size > MAX_WRITE_BYTES && !this.includeLarge) {
|
|
300
|
+
// can never be pushed, and downloading a terrain-sized sidecar helps nobody
|
|
301
|
+
this.skipLarge(file);
|
|
302
|
+
continue;
|
|
303
|
+
}
|
|
304
|
+
if (await this.writeConflict(file.path, file.etag))
|
|
305
|
+
conflicts++;
|
|
210
306
|
}
|
|
211
307
|
}
|
|
212
308
|
for (const relPath of this.walkLocal()) {
|
|
@@ -214,7 +310,7 @@ export class SyncEngine {
|
|
|
214
310
|
continue;
|
|
215
311
|
if (this.state.etags[relPath] === undefined) {
|
|
216
312
|
// never synced: new here, create it remotely
|
|
217
|
-
if (await this.pushFile(relPath, { createOnly: true }))
|
|
313
|
+
if ((await this.pushFile(relPath, { createOnly: true })) === "pushed")
|
|
218
314
|
pushed++;
|
|
219
315
|
continue;
|
|
220
316
|
}
|
|
@@ -224,7 +320,7 @@ export class SyncEngine {
|
|
|
224
320
|
if (this.localChanged(relPath)) {
|
|
225
321
|
this.log.warn(`${relPath} was deleted in the world but edited here - re-creating it.`);
|
|
226
322
|
delete this.state.etags[relPath];
|
|
227
|
-
if (await this.pushFile(relPath, { createOnly: true }))
|
|
323
|
+
if ((await this.pushFile(relPath, { createOnly: true })) === "pushed")
|
|
228
324
|
pushed++;
|
|
229
325
|
}
|
|
230
326
|
}
|
|
@@ -250,29 +346,53 @@ export class SyncEngine {
|
|
|
250
346
|
yield relPath;
|
|
251
347
|
}
|
|
252
348
|
}
|
|
253
|
-
/**
|
|
349
|
+
/**
|
|
350
|
+
* Push the local file, unless it is unchanged since its last sync. With
|
|
351
|
+
* `force`, a world copy that changed since is overwritten instead of
|
|
352
|
+
* becoming a conflict.
|
|
353
|
+
*/
|
|
254
354
|
async pushFile(relPath, opts) {
|
|
255
355
|
if (this.tooBigToPush(relPath))
|
|
256
|
-
return
|
|
356
|
+
return "too-large";
|
|
257
357
|
const body = fs.readFileSync(this.abs(relPath));
|
|
258
358
|
const known = this.state.etags[relPath];
|
|
259
359
|
// unchanged since the last sync: nothing to send (a touch, or the
|
|
260
|
-
// watcher and the local scan both reporting the same save)
|
|
261
|
-
|
|
262
|
-
|
|
360
|
+
// watcher and the local scan both reporting the same save). Resending
|
|
361
|
+
// it is not harmless - every script write Lua-resets the whole world.
|
|
362
|
+
if (known !== undefined && syncedHash(this.state, relPath) === sha256Hex(body)) {
|
|
363
|
+
if (!opts?.force)
|
|
364
|
+
return "unchanged";
|
|
365
|
+
// forced: still nothing to send when the world already has exactly this
|
|
366
|
+
const current = (await this.client.list()).find((f) => f.path === relPath)?.etag;
|
|
367
|
+
if (current === sha256Hex(body))
|
|
368
|
+
return "unchanged";
|
|
369
|
+
}
|
|
263
370
|
const createOnly = opts?.createOnly ?? known === undefined;
|
|
264
371
|
try {
|
|
265
|
-
|
|
372
|
+
let etag;
|
|
373
|
+
try {
|
|
374
|
+
etag = await this.client.put(relPath, body, createOnly ? { createOnly: true } : { etag: known });
|
|
375
|
+
}
|
|
376
|
+
catch (err) {
|
|
377
|
+
if (!(err instanceof ConflictError) || !opts?.force)
|
|
378
|
+
throw err;
|
|
379
|
+
const current = (await this.client.list()).find((f) => f.path === relPath)?.etag;
|
|
380
|
+
if (current === sha256Hex(body))
|
|
381
|
+
etag = current;
|
|
382
|
+
else
|
|
383
|
+
etag = await this.client.put(relPath, body, current ? { etag: current } : { createOnly: true });
|
|
384
|
+
}
|
|
266
385
|
this.state.etags[relPath] = etag;
|
|
267
386
|
recordLocal(this.state, this.dir, relPath, body);
|
|
268
387
|
this.persist();
|
|
269
388
|
this.log.info(`pushed ${relPath}`);
|
|
270
|
-
return
|
|
389
|
+
return "pushed";
|
|
271
390
|
}
|
|
272
391
|
catch (err) {
|
|
273
392
|
if (err instanceof ConflictError) {
|
|
274
|
-
|
|
275
|
-
|
|
393
|
+
// false: the world already holds exactly this file (an earlier push
|
|
394
|
+
// that landed, or another process) - recorded as in sync, not sent
|
|
395
|
+
return (await this.writeConflict(relPath)) ? "conflict" : "unchanged";
|
|
276
396
|
}
|
|
277
397
|
throw err;
|
|
278
398
|
}
|
|
@@ -303,6 +423,105 @@ export class SyncEngine {
|
|
|
303
423
|
}
|
|
304
424
|
return true;
|
|
305
425
|
}
|
|
426
|
+
/**
|
|
427
|
+
* One-shot push of `paths` (relative to the folder), or of every file that
|
|
428
|
+
* changed here since its last sync when `paths` is null. Each file is
|
|
429
|
+
* reported, and files that did not change are not sent - so a deploy that
|
|
430
|
+
* names ten files resets the world's Lua only for the ones that moved.
|
|
431
|
+
*/
|
|
432
|
+
async pushPaths(paths, opts = {}) {
|
|
433
|
+
const out = [];
|
|
434
|
+
const targets = paths ?? [...this.walkLocal()].filter((p) => this.state.etags[p] === undefined || this.localChanged(p));
|
|
435
|
+
for (const relPath of targets) {
|
|
436
|
+
const norm = relPath.replace(/\\/g, "/");
|
|
437
|
+
if (isIgnoredPath(norm)) {
|
|
438
|
+
out.push({ path: norm, status: "ignored", detail: "sync never sends this path (temp, conflict or harness file)" });
|
|
439
|
+
continue;
|
|
440
|
+
}
|
|
441
|
+
if (!fs.existsSync(this.abs(norm)) || !fs.statSync(this.abs(norm)).isFile()) {
|
|
442
|
+
out.push({ path: norm, status: "missing", detail: "no such file in this folder" });
|
|
443
|
+
continue;
|
|
444
|
+
}
|
|
445
|
+
if (!opts.force && this.unresolvedConflict(norm)) {
|
|
446
|
+
out.push({ path: norm, status: "conflict", detail: `unresolved ${norm}${CONFLICT_SUFFIX}: merge and save, delete the sidecar, or pass --force` });
|
|
447
|
+
continue;
|
|
448
|
+
}
|
|
449
|
+
try {
|
|
450
|
+
const status = await this.pushFile(norm, { force: opts.force });
|
|
451
|
+
const detail = status === "too-large"
|
|
452
|
+
? `over the world's ${(MAX_WRITE_BYTES / (1024 * 1024)).toFixed(0)} MB per-file limit`
|
|
453
|
+
: status === "conflict"
|
|
454
|
+
? `the world's copy changed too; it is saved as ${norm}${CONFLICT_SUFFIX} (or pass --force to overwrite it)`
|
|
455
|
+
: undefined;
|
|
456
|
+
if (status === "pushed" && opts.force)
|
|
457
|
+
fs.rmSync(this.abs(norm) + CONFLICT_SUFFIX, { force: true });
|
|
458
|
+
out.push(detail ? { path: norm, status, detail } : { path: norm, status });
|
|
459
|
+
}
|
|
460
|
+
catch (err) {
|
|
461
|
+
rethrowFatal(err);
|
|
462
|
+
out.push({ path: norm, status: "failed", detail: err instanceof Error ? err.message : String(err) });
|
|
463
|
+
}
|
|
464
|
+
}
|
|
465
|
+
this.flush();
|
|
466
|
+
return out;
|
|
467
|
+
}
|
|
468
|
+
/**
|
|
469
|
+
* One-shot download of `paths` from the world, or of every file the world
|
|
470
|
+
* changed since the last sync when `paths` is null. Never overwrites local
|
|
471
|
+
* work: a file changed on both sides becomes a conflict sidecar. Files too
|
|
472
|
+
* large to push back are included when named, skipped otherwise unless
|
|
473
|
+
* the engine was built with includeLarge.
|
|
474
|
+
*/
|
|
475
|
+
async fetchPaths(paths) {
|
|
476
|
+
const remote = new Map((await this.client.list()).map((f) => [f.path, f]));
|
|
477
|
+
const out = [];
|
|
478
|
+
const named = paths !== null;
|
|
479
|
+
const targets = paths ??
|
|
480
|
+
[...remote.values()].filter((f) => !isIgnoredPath(f.path) && this.state.etags[f.path] !== f.etag).map((f) => f.path);
|
|
481
|
+
for (const relPath of targets) {
|
|
482
|
+
const norm = relPath.replace(/\\/g, "/");
|
|
483
|
+
if (isIgnoredPath(norm)) {
|
|
484
|
+
out.push({ path: norm, status: "ignored", detail: "sync never mirrors this path" });
|
|
485
|
+
continue;
|
|
486
|
+
}
|
|
487
|
+
const file = remote.get(norm);
|
|
488
|
+
if (!file) {
|
|
489
|
+
out.push({ path: norm, status: "not-in-world", detail: "the world has no file at this path" });
|
|
490
|
+
continue;
|
|
491
|
+
}
|
|
492
|
+
if (!named && this.skipLarge(file)) {
|
|
493
|
+
out.push({ path: norm, status: "skipped-large", detail: "name it to download it" });
|
|
494
|
+
continue;
|
|
495
|
+
}
|
|
496
|
+
try {
|
|
497
|
+
const current = localHash(this.state, this.dir, norm);
|
|
498
|
+
if (current === file.etag) {
|
|
499
|
+
this.state.etags[norm] = file.etag;
|
|
500
|
+
adoptLocal(this.state, this.dir, norm, current);
|
|
501
|
+
this.dirty = true;
|
|
502
|
+
out.push({ path: norm, status: "unchanged" });
|
|
503
|
+
continue;
|
|
504
|
+
}
|
|
505
|
+
if (current !== null && this.localChanged(norm)) {
|
|
506
|
+
const real = await this.writeConflict(norm, file.etag);
|
|
507
|
+
out.push(real
|
|
508
|
+
? { path: norm, status: "conflict", detail: `changed here too; the world's copy is ${norm}${CONFLICT_SUFFIX}` }
|
|
509
|
+
: { path: norm, status: "unchanged" });
|
|
510
|
+
continue;
|
|
511
|
+
}
|
|
512
|
+
const pulled = await this.pullFile(norm, file.etag);
|
|
513
|
+
out.push(pulled === "pulled"
|
|
514
|
+
? { path: norm, status: "pulled" }
|
|
515
|
+
: { path: norm, status: "conflict", detail: `saved while downloading; the world's copy is ${norm}${CONFLICT_SUFFIX}` });
|
|
516
|
+
}
|
|
517
|
+
catch (err) {
|
|
518
|
+
rethrowFatal(err);
|
|
519
|
+
out.push({ path: norm, status: "failed", detail: err instanceof Error ? err.message : String(err) });
|
|
520
|
+
}
|
|
521
|
+
}
|
|
522
|
+
this.flush();
|
|
523
|
+
return out;
|
|
524
|
+
}
|
|
306
525
|
/** Watcher callback for a local add/change. */
|
|
307
526
|
async handleLocalChange(relPath) {
|
|
308
527
|
const norm = relPath.replace(/\\/g, "/");
|
|
@@ -312,10 +531,14 @@ export class SyncEngine {
|
|
|
312
531
|
return;
|
|
313
532
|
if (!fs.existsSync(this.abs(norm)))
|
|
314
533
|
return;
|
|
534
|
+
// a save from before the conflict sidecar landed; the next save pushes
|
|
535
|
+
if (this.unresolvedConflict(norm))
|
|
536
|
+
return;
|
|
315
537
|
try {
|
|
316
538
|
await this.pushFile(norm);
|
|
317
539
|
}
|
|
318
540
|
catch (err) {
|
|
541
|
+
rethrowFatal(err);
|
|
319
542
|
this.log.warn(`push of ${norm} failed: ${String(err)}`);
|
|
320
543
|
}
|
|
321
544
|
}
|
|
@@ -341,6 +564,7 @@ export class SyncEngine {
|
|
|
341
564
|
await this.writeConflict(norm);
|
|
342
565
|
return;
|
|
343
566
|
}
|
|
567
|
+
rethrowFatal(err);
|
|
344
568
|
this.log.warn(`remote delete of ${norm} failed: ${String(err)}`);
|
|
345
569
|
}
|
|
346
570
|
}
|
|
@@ -375,6 +599,8 @@ export class SyncEngine {
|
|
|
375
599
|
this.persist();
|
|
376
600
|
continue;
|
|
377
601
|
}
|
|
602
|
+
if (this.skipLarge(file))
|
|
603
|
+
continue;
|
|
378
604
|
if (this.localChanged(file.path)) {
|
|
379
605
|
// writeConflict points the etag at this remote version, so the
|
|
380
606
|
// check at the top of the loop keeps it from firing again
|
|
@@ -382,7 +608,11 @@ export class SyncEngine {
|
|
|
382
608
|
continue;
|
|
383
609
|
}
|
|
384
610
|
}
|
|
385
|
-
|
|
611
|
+
else if (this.skipLarge(file)) {
|
|
612
|
+
continue;
|
|
613
|
+
}
|
|
614
|
+
if ((await this.pullFile(file.path, file.etag)) === "conflict")
|
|
615
|
+
continue;
|
|
386
616
|
pulled.push(file.path);
|
|
387
617
|
this.log.info(`pulled ${file.path}`);
|
|
388
618
|
}
|
|
@@ -406,10 +636,11 @@ export class SyncEngine {
|
|
|
406
636
|
if (known !== undefined && this.unresolvedConflict(relPath))
|
|
407
637
|
continue;
|
|
408
638
|
try {
|
|
409
|
-
if (await this.pushFile(relPath))
|
|
639
|
+
if ((await this.pushFile(relPath)) === "pushed")
|
|
410
640
|
pushed.push(relPath);
|
|
411
641
|
}
|
|
412
642
|
catch (err) {
|
|
643
|
+
rethrowFatal(err);
|
|
413
644
|
this.log.warn(`push of ${relPath} failed: ${String(err)}`);
|
|
414
645
|
}
|
|
415
646
|
}
|
|
@@ -421,9 +652,18 @@ export class SyncEngine {
|
|
|
421
652
|
export function touchesSkills(paths) {
|
|
422
653
|
return paths.some((p) => p.replace(/\\/g, "/").startsWith("Skills/"));
|
|
423
654
|
}
|
|
424
|
-
/**
|
|
655
|
+
/**
|
|
656
|
+
* Run the full watch loop until SIGINT/SIGTERM, the starting process exits
|
|
657
|
+
* (unless keepAlive), or the world's session is lost for good - then it
|
|
658
|
+
* throws, so a sync that can no longer push never looks like it still is.
|
|
659
|
+
* Not unit-tested directly; the decisions above are.
|
|
660
|
+
*/
|
|
425
661
|
export async function runSyncLoop(engine, dir, opts = {}) {
|
|
426
662
|
const { default: chokidar } = await import("chokidar");
|
|
663
|
+
const leftovers = removeStaleDownloadTemps(dir);
|
|
664
|
+
if (leftovers.length > 0) {
|
|
665
|
+
console.log(`removed ${leftovers.length} partial download${leftovers.length === 1 ? "" : "s"} an earlier run left behind`);
|
|
666
|
+
}
|
|
427
667
|
const { pulled, pushed, conflicts } = await engine.initialSync();
|
|
428
668
|
console.log(`initial sync: ${pulled} pulled, ${pushed} pushed` +
|
|
429
669
|
(conflicts ? `, ${conflicts} conflict${conflicts === 1 ? "" : "s"} (see above)` : "") +
|
|
@@ -433,6 +673,7 @@ export async function runSyncLoop(engine, dir, opts = {}) {
|
|
|
433
673
|
await opts.onSkillsChanged?.();
|
|
434
674
|
}
|
|
435
675
|
catch (err) {
|
|
676
|
+
rethrowFatal(err);
|
|
436
677
|
console.warn(`agent files refresh failed: ${String(err)}`);
|
|
437
678
|
}
|
|
438
679
|
};
|
|
@@ -450,9 +691,18 @@ export async function runSyncLoop(engine, dir, opts = {}) {
|
|
|
450
691
|
return rel.length > 0 && isIgnoredPath(rel);
|
|
451
692
|
},
|
|
452
693
|
});
|
|
694
|
+
let fatal = null;
|
|
695
|
+
let stop = async () => { };
|
|
453
696
|
let queue = Promise.resolve();
|
|
454
697
|
const enqueue = (fn) => {
|
|
455
|
-
queue = queue.then(fn).catch((err) =>
|
|
698
|
+
queue = queue.then(fn).catch((err) => {
|
|
699
|
+
if (err instanceof SessionLostError) {
|
|
700
|
+
fatal = err;
|
|
701
|
+
void stop();
|
|
702
|
+
return;
|
|
703
|
+
}
|
|
704
|
+
console.warn(String(err));
|
|
705
|
+
});
|
|
456
706
|
};
|
|
457
707
|
watcher.on("add", (p) => enqueue(() => engine.handleLocalChange(path.relative(dir, p))));
|
|
458
708
|
watcher.on("change", (p) => enqueue(() => engine.handleLocalChange(path.relative(dir, p))));
|
|
@@ -463,6 +713,7 @@ export async function runSyncLoop(engine, dir, opts = {}) {
|
|
|
463
713
|
await engine.scanLocal();
|
|
464
714
|
}
|
|
465
715
|
catch (err) {
|
|
716
|
+
rethrowFatal(err);
|
|
466
717
|
console.warn(`local scan failed: ${String(err)}`);
|
|
467
718
|
}
|
|
468
719
|
try {
|
|
@@ -470,20 +721,38 @@ export async function runSyncLoop(engine, dir, opts = {}) {
|
|
|
470
721
|
await refreshSkills();
|
|
471
722
|
}
|
|
472
723
|
catch (err) {
|
|
724
|
+
rethrowFatal(err);
|
|
473
725
|
console.warn(`remote poll failed: ${String(err)}`);
|
|
474
726
|
}
|
|
475
727
|
});
|
|
476
728
|
}, 3000);
|
|
729
|
+
let stopWatching = () => { };
|
|
477
730
|
await new Promise((resolve) => {
|
|
478
|
-
|
|
731
|
+
let stopped = false;
|
|
732
|
+
stop = async () => {
|
|
733
|
+
if (stopped)
|
|
734
|
+
return;
|
|
735
|
+
stopped = true;
|
|
479
736
|
clearInterval(interval);
|
|
737
|
+
stopWatching();
|
|
480
738
|
await watcher.close();
|
|
481
739
|
await stopLogs();
|
|
482
740
|
console.log("\nsync stopped.");
|
|
483
741
|
resolve();
|
|
484
742
|
};
|
|
485
|
-
process.once("SIGINT", stop);
|
|
486
|
-
process.once("SIGTERM", stop);
|
|
743
|
+
process.once("SIGINT", () => void stop());
|
|
744
|
+
process.once("SIGTERM", () => void stop());
|
|
745
|
+
process.once("SIGHUP", () => void stop());
|
|
746
|
+
if (!opts.keepAlive) {
|
|
747
|
+
stopWatching = watchSupervisors(supervisingPids(), () => {
|
|
748
|
+
console.log("\nThe process that started sync exited, so sync is stopping too (pass --keep-alive to run it detached).");
|
|
749
|
+
void stop();
|
|
750
|
+
});
|
|
751
|
+
}
|
|
487
752
|
});
|
|
753
|
+
if (fatal) {
|
|
754
|
+
throw new Error(`${fatal.message}\nsync stopped: edits saved from now on would NOT reach the world. ` +
|
|
755
|
+
"Run `npx virtualmatter sync` again once the world is reachable.");
|
|
756
|
+
}
|
|
488
757
|
}
|
|
489
758
|
export { loadState };
|