engine7 7.1.148 → 7.1.149
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/dist/bg-worker.mjs +141 -36
- package/dist/cli.mjs +2175 -1348
- package/dist/engine-startup.mjs +1362 -992
- package/dist/main.mjs +1374 -996
- package/dist/recall-worker.mjs +135 -29
- package/dist/topics-worker.mjs +123 -28
- package/dist/travel-gateway.mjs +265 -100
- package/dist/travel-host.mjs +63 -47
- package/dist/travel-node.mjs +2 -3
- package/dist/travel-oss-bundle-stream.mjs +1 -1
- package/dist/travel-pairing.mjs +271 -106
- package/dist/travel-sync-worker.mjs +223 -114
- package/dist/web-console.mjs +298 -50
- package/package.json +1 -1
|
@@ -289,14 +289,14 @@ var init_capture_cache = __esm({
|
|
|
289
289
|
});
|
|
290
290
|
|
|
291
291
|
// src/travel/apply-transaction.ts
|
|
292
|
-
import { closeSync as closeSync2, existsSync as existsSync4, fsyncSync as fsyncSync2, lstatSync as lstatSync4, mkdirSync as mkdirSync3, openSync as openSync2, readFileSync as
|
|
293
|
-
import { dirname as dirname2, join as
|
|
292
|
+
import { closeSync as closeSync2, existsSync as existsSync4, fsyncSync as fsyncSync2, linkSync, lstatSync as lstatSync4, mkdirSync as mkdirSync3, openSync as openSync2, readFileSync as readFileSync4, readSync as readSync2, renameSync as renameSync3, rmSync, writeFileSync as writeFileSync3 } from "node:fs";
|
|
293
|
+
import { dirname as dirname2, join as join5, resolve as resolve2 } from "node:path";
|
|
294
294
|
import { createHash as createHash2, randomUUID as randomUUID3 } from "node:crypto";
|
|
295
295
|
function assertPath(root, rel) {
|
|
296
296
|
if (!safeRel(rel)) throw Error("TRAVEL_UNSAFE_PATH");
|
|
297
297
|
let path5 = root;
|
|
298
298
|
for (const part of rel.split("/")) {
|
|
299
|
-
path5 =
|
|
299
|
+
path5 = join5(path5, part);
|
|
300
300
|
try {
|
|
301
301
|
if (lstatSync4(path5).isSymbolicLink()) throw Error("TRAVEL_UNSAFE_PATH");
|
|
302
302
|
} catch (e) {
|
|
@@ -305,7 +305,7 @@ function assertPath(root, rel) {
|
|
|
305
305
|
}
|
|
306
306
|
return path5;
|
|
307
307
|
}
|
|
308
|
-
function atomic(path5, body) {
|
|
308
|
+
function atomic(path5, body, exclusive = false) {
|
|
309
309
|
const temp = path5 + "." + randomUUID3() + ".tmp";
|
|
310
310
|
let fd;
|
|
311
311
|
try {
|
|
@@ -314,20 +314,21 @@ function atomic(path5, body) {
|
|
|
314
314
|
fsyncSync2(fd);
|
|
315
315
|
closeSync2(fd);
|
|
316
316
|
fd = void 0;
|
|
317
|
-
|
|
317
|
+
if (exclusive) linkSync(temp, path5);
|
|
318
|
+
else renameSync3(temp, path5);
|
|
318
319
|
} finally {
|
|
319
320
|
if (fd !== void 0) closeSync2(fd);
|
|
320
321
|
rmSync(temp, { force: true });
|
|
321
322
|
}
|
|
322
323
|
}
|
|
323
|
-
function save(root, journal) {
|
|
324
|
+
function save(root, journal, exclusive = false) {
|
|
324
325
|
const { digest: _, ...body } = journal;
|
|
325
|
-
atomic(journalFile(root), JSON.stringify({ ...body, digest: hash(JSON.stringify(body)) }));
|
|
326
|
+
atomic(journalFile(root), JSON.stringify({ ...body, digest: hash(JSON.stringify(body)) }), exclusive);
|
|
326
327
|
}
|
|
327
328
|
function read(root) {
|
|
328
329
|
if (!existsSync4(journalFile(root))) return;
|
|
329
|
-
const value = JSON.parse(
|
|
330
|
-
if (value.version !== 1 || value.root !==
|
|
330
|
+
const value = JSON.parse(readFileSync4(journalFile(root), "utf8")), { digest: digest2, ...body } = value;
|
|
331
|
+
if (value.version !== 1 || value.root !== resolve2(root) || !Number.isSafeInteger(value.pid) || value.pid < 1 || !/^[a-f0-9-]{36}$/.test(value.token) || typeof value.committed !== "boolean" || digest2 !== hash(JSON.stringify(body)) || !Array.isArray(value.entries) || value.entries.some((e) => !safeRel(e.rel) || e.oldSha !== void 0 && !/^[a-f0-9]{64}$/.test(e.oldSha) || e.newSha !== void 0 && !/^[a-f0-9]{64}$/.test(e.newSha))) throw Error("TRAVEL_APPLY_RECOVERY_INVALID");
|
|
331
332
|
return value;
|
|
332
333
|
}
|
|
333
334
|
function matches(path5, sha) {
|
|
@@ -360,14 +361,14 @@ function rollback(root, journal) {
|
|
|
360
361
|
renameSync3(old, dest);
|
|
361
362
|
} else if (!entry.oldSha) rmSync(dest, { force: true });
|
|
362
363
|
}
|
|
363
|
-
const state =
|
|
364
|
+
const state = join5(root, ".travel/sync-state.json");
|
|
364
365
|
if (journal.previousState !== null) atomic(state, journal.previousState);
|
|
365
366
|
else rmSync(state, { force: true });
|
|
366
367
|
rmSync(journalFile(root), { force: true });
|
|
367
368
|
rmSync(stage, { recursive: true, force: true });
|
|
368
369
|
}
|
|
369
370
|
function recoverPendingApply(root) {
|
|
370
|
-
root =
|
|
371
|
+
root = resolve2(root);
|
|
371
372
|
const journal = read(root);
|
|
372
373
|
if (!journal) return;
|
|
373
374
|
if (!journal.committed) {
|
|
@@ -385,24 +386,27 @@ function recoverPendingApply(root) {
|
|
|
385
386
|
}
|
|
386
387
|
function newApplyStage(root) {
|
|
387
388
|
const token = randomUUID3(), path5 = assertPath(root, ".travel-sync-staging/" + token);
|
|
388
|
-
mkdirSync3(
|
|
389
|
+
mkdirSync3(join5(path5, "new"), { recursive: true, mode: 448 });
|
|
389
390
|
return { token, path: path5 };
|
|
390
391
|
}
|
|
391
392
|
function promoteFiles(root, stage, entries, commit) {
|
|
392
393
|
let previousState = null;
|
|
393
394
|
try {
|
|
394
|
-
previousState =
|
|
395
|
+
previousState = readFileSync4(join5(root, ".travel/sync-state.json"), "utf8");
|
|
395
396
|
} catch {
|
|
396
397
|
}
|
|
397
|
-
const journal = { version: 1, pid: process.pid, token: stage.token, root:
|
|
398
|
+
const journal = { version: 1, pid: process.pid, token: stage.token, root: resolve2(root), committed: false, entries, previousState };
|
|
398
399
|
for (const entry of entries) {
|
|
399
400
|
const dest = assertPath(root, entry.rel);
|
|
400
401
|
if (entry.before ? fileFingerprint(dest) !== entry.before : existsSync4(dest)) throw Error("TRAVEL_DESTINATION_CHANGED");
|
|
401
402
|
}
|
|
402
|
-
mkdirSync3(
|
|
403
|
-
|
|
404
|
-
|
|
405
|
-
|
|
403
|
+
mkdirSync3(join5(root, ".travel"), { recursive: true, mode: 448 });
|
|
404
|
+
try {
|
|
405
|
+
save(root, journal, true);
|
|
406
|
+
} catch (error) {
|
|
407
|
+
if (error.code === "EEXIST") throw Error("TRAVEL_APPLY_BUSY");
|
|
408
|
+
throw error;
|
|
409
|
+
}
|
|
406
410
|
try {
|
|
407
411
|
for (const entry of entries) {
|
|
408
412
|
const dest = assertPath(root, entry.rel), old = assertPath(stage.path, "old/" + entry.rel);
|
|
@@ -430,7 +434,7 @@ var init_apply_transaction = __esm({
|
|
|
430
434
|
"src/travel/apply-transaction.ts"() {
|
|
431
435
|
"use strict";
|
|
432
436
|
init_capture_cache();
|
|
433
|
-
journalFile = (root) =>
|
|
437
|
+
journalFile = (root) => join5(root, ".travel/apply-transaction.json");
|
|
434
438
|
safeRel = (rel) => !!rel && !/[\\:\x00-\x1f]/.test(rel) && rel.split("/").every((p) => p && p !== "." && p !== "..");
|
|
435
439
|
hash = (body) => createHash2("sha256").update(body).digest("hex");
|
|
436
440
|
}
|
|
@@ -3229,8 +3233,8 @@ __export(secrets_env_exports, {
|
|
|
3229
3233
|
writeSecretsValue: () => writeSecretsValue
|
|
3230
3234
|
});
|
|
3231
3235
|
import { createHash as createHash4 } from "node:crypto";
|
|
3232
|
-
import { chmodSync, existsSync as existsSync9, mkdirSync as mkdirSync6, readFileSync as
|
|
3233
|
-
import { basename, dirname as dirname3, join as
|
|
3236
|
+
import { chmodSync, existsSync as existsSync9, mkdirSync as mkdirSync6, readFileSync as readFileSync7, writeFileSync as writeFileSync6 } from "node:fs";
|
|
3237
|
+
import { basename, dirname as dirname3, join as join11, normalize, resolve as resolve5 } from "node:path";
|
|
3234
3238
|
function secretsHome() {
|
|
3235
3239
|
return process.env.HOME || process.env.USERPROFILE || ".";
|
|
3236
3240
|
}
|
|
@@ -3241,17 +3245,17 @@ function stateKey(stateDir) {
|
|
|
3241
3245
|
return createHash4("sha256").update(normalizeStateDir(stateDir)).digest("hex").slice(0, 8);
|
|
3242
3246
|
}
|
|
3243
3247
|
function isDefaultStateDir(stateDir) {
|
|
3244
|
-
return normalizeStateDir(stateDir) === normalizeStateDir(
|
|
3248
|
+
return normalizeStateDir(stateDir) === normalizeStateDir(join11(secretsHome(), ".engine7"));
|
|
3245
3249
|
}
|
|
3246
3250
|
function secretsCandidates(stateDir, configName) {
|
|
3247
|
-
const shared =
|
|
3251
|
+
const shared = join11(secretsHome(), ".engine7-secrets", `${configName}.env`);
|
|
3248
3252
|
const inState = [
|
|
3249
|
-
|
|
3250
|
-
|
|
3253
|
+
join11(stateDir, "configs", `.env.${configName}`),
|
|
3254
|
+
join11(stateDir, "configs", ".env")
|
|
3251
3255
|
];
|
|
3252
3256
|
if (isDefaultStateDir(stateDir)) return [shared, ...inState];
|
|
3253
3257
|
return [
|
|
3254
|
-
|
|
3258
|
+
join11(secretsHome(), ".engine7-secrets", `${configName}-${stateKey(stateDir)}.env`),
|
|
3255
3259
|
shared,
|
|
3256
3260
|
...inState
|
|
3257
3261
|
];
|
|
@@ -3260,7 +3264,7 @@ function readSecretsEntries(stateDir, configName) {
|
|
|
3260
3264
|
for (const p of secretsCandidates(stateDir, configName)) {
|
|
3261
3265
|
if (!existsSync9(p)) continue;
|
|
3262
3266
|
const entries = [];
|
|
3263
|
-
for (const line of
|
|
3267
|
+
for (const line of readFileSync7(p, "utf-8").split("\n")) {
|
|
3264
3268
|
const t = line.trim();
|
|
3265
3269
|
if (!t || t.startsWith("#")) continue;
|
|
3266
3270
|
const eq = t.indexOf("=");
|
|
@@ -3287,7 +3291,7 @@ function loadSecretsEnv(stateDir, configName, options = {}) {
|
|
|
3287
3291
|
return selected.path;
|
|
3288
3292
|
}
|
|
3289
3293
|
function resolveConfigIdentity(configPath) {
|
|
3290
|
-
const abs =
|
|
3294
|
+
const abs = resolve5(configPath);
|
|
3291
3295
|
const cfgName = basename(abs, ".json");
|
|
3292
3296
|
const cfgDir = dirname3(abs);
|
|
3293
3297
|
const stateDir = basename(cfgDir) === "configs" ? dirname3(cfgDir) : cfgDir;
|
|
@@ -3296,7 +3300,7 @@ function resolveConfigIdentity(configPath) {
|
|
|
3296
3300
|
function engineStateDirOfConfig(configPath, env = process.env) {
|
|
3297
3301
|
let raw;
|
|
3298
3302
|
try {
|
|
3299
|
-
raw = JSON.parse(
|
|
3303
|
+
raw = JSON.parse(readFileSync7(configPath, "utf-8"));
|
|
3300
3304
|
} catch {
|
|
3301
3305
|
return null;
|
|
3302
3306
|
}
|
|
@@ -3312,13 +3316,13 @@ function loadSecretsEnvForConfig(configPath, options = {}) {
|
|
|
3312
3316
|
}
|
|
3313
3317
|
function writeSecretsValue(configPath, envKey, value) {
|
|
3314
3318
|
const { stateDir, configName } = resolveConfigIdentity(configPath);
|
|
3315
|
-
const dir =
|
|
3319
|
+
const dir = join11(secretsHome(), ".engine7-secrets");
|
|
3316
3320
|
mkdirSync6(dir, { recursive: true });
|
|
3317
3321
|
const file = secretsCandidates(stateDir, configName)[0];
|
|
3318
3322
|
const seedFrom = existsSync9(file) ? file : secretsCandidates(stateDir, configName).find((p) => existsSync9(p)) ?? null;
|
|
3319
3323
|
const lines = [];
|
|
3320
3324
|
if (seedFrom) {
|
|
3321
|
-
for (const line of
|
|
3325
|
+
for (const line of readFileSync7(seedFrom, "utf8").split(/\r?\n/)) {
|
|
3322
3326
|
if (new RegExp(`^\\s*${envKey}\\s*=`).test(line)) continue;
|
|
3323
3327
|
lines.push(line);
|
|
3324
3328
|
}
|
|
@@ -3349,7 +3353,7 @@ __export(oss_broker_exports, {
|
|
|
3349
3353
|
withOssBatches: () => withOssBatches
|
|
3350
3354
|
});
|
|
3351
3355
|
import { randomBytes } from "node:crypto";
|
|
3352
|
-
import { readFileSync as
|
|
3356
|
+
import { readFileSync as readFileSync8 } from "node:fs";
|
|
3353
3357
|
function clearOssRequests(config) {
|
|
3354
3358
|
cache.delete(config);
|
|
3355
3359
|
}
|
|
@@ -3409,8 +3413,8 @@ async function authorize(config, operations, background = false) {
|
|
|
3409
3413
|
const result = current.tail.catch(() => {
|
|
3410
3414
|
}).then(async () => {
|
|
3411
3415
|
const wait = 1e3 - (Date.now() - current.last);
|
|
3412
|
-
if (wait > 0) await new Promise((
|
|
3413
|
-
const timer = setTimeout(
|
|
3416
|
+
if (wait > 0) await new Promise((resolve6) => {
|
|
3417
|
+
const timer = setTimeout(resolve6, wait);
|
|
3414
3418
|
if (background) timer.unref();
|
|
3415
3419
|
});
|
|
3416
3420
|
try {
|
|
@@ -3463,7 +3467,7 @@ async function brokerOssConfig(configPath, options = {}) {
|
|
|
3463
3467
|
let providers;
|
|
3464
3468
|
try {
|
|
3465
3469
|
loadSecretsEnvForConfig2(configPath);
|
|
3466
|
-
const raw = makeEnvResolver2()(JSON.parse(
|
|
3470
|
+
const raw = makeEnvResolver2()(JSON.parse(readFileSync8(configPath, "utf8")));
|
|
3467
3471
|
providers = { ...raw.models?.providers, ...raw.providers };
|
|
3468
3472
|
} catch {
|
|
3469
3473
|
return null;
|
|
@@ -3537,8 +3541,8 @@ __export(oss_transport_exports, {
|
|
|
3537
3541
|
withOssBatches: () => withOssBatches
|
|
3538
3542
|
});
|
|
3539
3543
|
import { createHmac, createHash as createHash5 } from "node:crypto";
|
|
3540
|
-
import { readFileSync as
|
|
3541
|
-
import { join as
|
|
3544
|
+
import { readFileSync as readFileSync9, statSync as statSync2 } from "node:fs";
|
|
3545
|
+
import { join as join12 } from "node:path";
|
|
3542
3546
|
function ossFailureCode(error) {
|
|
3543
3547
|
const message = error instanceof Error ? error.message : "";
|
|
3544
3548
|
const code = /^TRAVEL_[A-Z_0-9]+/.exec(message)?.[0];
|
|
@@ -3563,12 +3567,12 @@ function loadOssConfig(stateDir) {
|
|
|
3563
3567
|
if (ak && sk && bucket && endpoint) return { accessKeyId: ak, accessKeySecret: sk, bucket, endpoint };
|
|
3564
3568
|
if (!stateDir) return null;
|
|
3565
3569
|
try {
|
|
3566
|
-
const ossJsonPath =
|
|
3570
|
+
const ossJsonPath = join12(stateDir, ".travel", "oss.json");
|
|
3567
3571
|
try {
|
|
3568
3572
|
if (statSync2(ossJsonPath).mode & 63) console.warn(`[travel] ${ossJsonPath} \u6743\u9650\u8FC7\u5BBD\uFF08\u5E94 600\uFF09\u2014\u2014OSS \u51ED\u636E\u53EF\u88AB\u540C\u673A\u5176\u4ED6\u7528\u6237\u8BFB\u53D6`);
|
|
3569
3573
|
} catch {
|
|
3570
3574
|
}
|
|
3571
|
-
const file = JSON.parse(
|
|
3575
|
+
const file = JSON.parse(readFileSync9(ossJsonPath, "utf8"));
|
|
3572
3576
|
if (file.accessKeyId && file.accessKeySecret && file.bucket && file.endpoint)
|
|
3573
3577
|
return { accessKeyId: file.accessKeyId, accessKeySecret: file.accessKeySecret, bucket: file.bucket, endpoint: file.endpoint };
|
|
3574
3578
|
} catch {
|
|
@@ -3737,7 +3741,7 @@ import { createHash as createHash6 } from "node:crypto";
|
|
|
3737
3741
|
import { gzip as gzip2, gunzip } from "node:zlib";
|
|
3738
3742
|
import { promisify as promisify2 } from "node:util";
|
|
3739
3743
|
import { statSync as statSync3 } from "node:fs";
|
|
3740
|
-
import { join as
|
|
3744
|
+
import { join as join13 } from "node:path";
|
|
3741
3745
|
function validateBundleRef(value) {
|
|
3742
3746
|
if (!value || !/^[a-f0-9]{64}$/.test(value.sha256) || !Number.isSafeInteger(value.plainBytes) || value.plainBytes < 12 || value.plainBytes > BUNDLE_RAW_MAX + BUNDLE_HEADER_MAX + 12 || !Array.isArray(value.refs) || !value.refs.length || value.refs.length > BUNDLE_REFS_MAX) throw Error("TRAVEL_INVALID_BUNDLE");
|
|
3743
3747
|
const seen = /* @__PURE__ */ new Set();
|
|
@@ -3755,7 +3759,7 @@ function bundleGroups(store, refs) {
|
|
|
3755
3759
|
let group = [], raw = 0, hint = 0;
|
|
3756
3760
|
for (const ref of refs) {
|
|
3757
3761
|
validateChunkRef(ref);
|
|
3758
|
-
const size = statSync3(
|
|
3762
|
+
const size = statSync3(join13(store.dir, ref.sha256 + ".gz")).size;
|
|
3759
3763
|
if (group.length && (raw + ref.bytes > BUNDLE_RAW_MAX || hint + size > 7.5 * 1048576 || group.length >= BUNDLE_REFS_MAX)) {
|
|
3760
3764
|
groups.push(group);
|
|
3761
3765
|
group = [];
|
|
@@ -3803,17 +3807,17 @@ var init_oss_bundle = __esm({
|
|
|
3803
3807
|
});
|
|
3804
3808
|
|
|
3805
3809
|
// src/travel/oss-resume.ts
|
|
3806
|
-
import { appendFileSync, existsSync as existsSync10, mkdirSync as mkdirSync7, readFileSync as
|
|
3807
|
-
import { join as
|
|
3810
|
+
import { appendFileSync, existsSync as existsSync10, mkdirSync as mkdirSync7, readFileSync as readFileSync10, renameSync as renameSync5, statSync as statSync4, writeFileSync as writeFileSync7 } from "node:fs";
|
|
3811
|
+
import { join as join14 } from "node:path";
|
|
3808
3812
|
import { createHash as createHash7, randomUUID as randomUUID4 } from "node:crypto";
|
|
3809
3813
|
async function openOssResume(stateDir, config, first) {
|
|
3810
3814
|
if (config.broker && !config.broker.tokenId) await ossHeadChunk(config, first.sha256);
|
|
3811
3815
|
const scope = createHash7("sha256").update(JSON.stringify([config.bucket, config.endpoint, config.broker?.url ?? "", config.broker?.tokenId ?? createHash7("sha256").update(config.accessKeyId ?? "").digest("hex")])).digest("hex");
|
|
3812
|
-
mkdirSync7(
|
|
3816
|
+
mkdirSync7(join14(stateDir, ".travel"), { recursive: true, mode: 448 });
|
|
3813
3817
|
const metadata = assertPath(stateDir, ".travel/oss-resume.json"), receipts = assertPath(stateDir, ".travel/oss-upload-receipts.jsonl");
|
|
3814
3818
|
let session;
|
|
3815
3819
|
try {
|
|
3816
|
-
const saved = JSON.parse(
|
|
3820
|
+
const saved = JSON.parse(readFileSync10(metadata, "utf8"));
|
|
3817
3821
|
if (saved.version === 2 && saved.scope === scope && /^(?:[a-f0-9]{64}|legacy)$/.test(saved.transferId) && Number.isSafeInteger(saved.createdAt) && saved.createdAt > Date.now() - KEEP_MS && saved.createdAt <= Date.now()) session = saved;
|
|
3818
3822
|
} catch {
|
|
3819
3823
|
}
|
|
@@ -3829,7 +3833,7 @@ async function openOssResume(stateDir, config, first) {
|
|
|
3829
3833
|
}
|
|
3830
3834
|
const packages = /* @__PURE__ */ new Map();
|
|
3831
3835
|
if (existsSync10(receipts) && statSync4(receipts).size <= 64 * 1048576) {
|
|
3832
|
-
for (const line of
|
|
3836
|
+
for (const line of readFileSync10(receipts, "utf8").split("\n")) try {
|
|
3833
3837
|
const value = JSON.parse(line);
|
|
3834
3838
|
if (value.scope !== scope || value.transferId !== session.transferId || !Number.isSafeInteger(value.at) || value.at < Date.now() - KEEP_MS || value.at > Date.now()) continue;
|
|
3835
3839
|
for (const ref of value.bundles ?? []) {
|
|
@@ -3863,21 +3867,21 @@ var init_oss_resume = __esm({
|
|
|
3863
3867
|
});
|
|
3864
3868
|
|
|
3865
3869
|
// src/travel/oss-bundle-stream.ts
|
|
3866
|
-
import { readFileSync as
|
|
3870
|
+
import { readFileSync as readFileSync11, existsSync as existsSync11 } from "node:fs";
|
|
3867
3871
|
import { fileURLToPath } from "node:url";
|
|
3868
3872
|
function openBundleStream(options) {
|
|
3869
3873
|
let source = options.source;
|
|
3870
3874
|
if (!source) {
|
|
3871
3875
|
for (const url of [new URL("./travel-oss-bundle-stream.mjs", import.meta.url), new URL("../../dist/travel-oss-bundle-stream.mjs", import.meta.url)]) if (existsSync11(fileURLToPath(url))) {
|
|
3872
|
-
source =
|
|
3876
|
+
source = readFileSync11(url, "utf8");
|
|
3873
3877
|
break;
|
|
3874
3878
|
}
|
|
3875
3879
|
}
|
|
3876
3880
|
if (!source) throw Error("TRAVEL_OSS_HELPER_MISSING");
|
|
3877
3881
|
const child = options.spawn("node --input-type=module -e " + quote(bootstrap)), pending = /* @__PURE__ */ new Map();
|
|
3878
3882
|
let sequence = 0, text = "", settled = false, closed = false, resolveReady, rejectReady;
|
|
3879
|
-
const ready = new Promise((
|
|
3880
|
-
resolveReady =
|
|
3883
|
+
const ready = new Promise((resolve6, reject) => {
|
|
3884
|
+
resolveReady = resolve6;
|
|
3881
3885
|
rejectReady = reject;
|
|
3882
3886
|
});
|
|
3883
3887
|
const fail = () => {
|
|
@@ -3928,8 +3932,8 @@ function openBundleStream(options) {
|
|
|
3928
3932
|
await ready;
|
|
3929
3933
|
if (closed) throw Error("TRAVEL_OSS_BUNDLE_CONNECTION_CLOSED");
|
|
3930
3934
|
const id = ++sequence;
|
|
3931
|
-
return new Promise((
|
|
3932
|
-
pending.set(id, { resolve:
|
|
3935
|
+
return new Promise((resolve6, reject) => {
|
|
3936
|
+
pending.set(id, { resolve: resolve6, reject });
|
|
3933
3937
|
child.stdin.write(JSON.stringify({ id, bundles }) + "\n", (error) => {
|
|
3934
3938
|
if (error) {
|
|
3935
3939
|
pending.delete(id);
|
|
@@ -4060,18 +4064,51 @@ var init_oss_bundle_lane = __esm({
|
|
|
4060
4064
|
// src/travel/sync-worker.ts
|
|
4061
4065
|
init_chunk_store();
|
|
4062
4066
|
init_capture_cache();
|
|
4063
|
-
import { join as
|
|
4067
|
+
import { join as join15 } from "node:path";
|
|
4068
|
+
import { readFileSync as readFileSync12 } from "node:fs";
|
|
4064
4069
|
import { createHash as createHash8 } from "node:crypto";
|
|
4065
4070
|
|
|
4071
|
+
// src/travel/ssh.ts
|
|
4072
|
+
function isConnectionError(error) {
|
|
4073
|
+
if (!(error instanceof Error)) return false;
|
|
4074
|
+
if (error.message === "TRAVEL_CONNECTION_CLOSED") return true;
|
|
4075
|
+
const { code, stderr } = error;
|
|
4076
|
+
if (["ECONNRESET", "EHOSTUNREACH", "ENETUNREACH", "ETIMEDOUT"].includes(String(code))) return true;
|
|
4077
|
+
if (code !== 255 && code !== "255" && stderr !== void 0) return false;
|
|
4078
|
+
const diagnostic = stderr ?? error.message;
|
|
4079
|
+
if (/Permission denied|Host key verification failed|REMOTE HOST IDENTIFICATION HAS CHANGED|Connection refused/i.test(diagnostic)) return false;
|
|
4080
|
+
return /^(?:(?:ssh: connect to host .+ port \d+: |client_loop: send disconnect: |kex_exchange_identification: |banner exchange: .+: )(?:No route to host|Network is unreachable|Connection timed out|Connection reset by peer|Broken pipe|Operation timed out)|Connection (?:reset|closed) by |Timeout, server .* not responding)/im.test(diagnostic);
|
|
4081
|
+
}
|
|
4082
|
+
|
|
4066
4083
|
// src/travel/tree-manifest.ts
|
|
4067
4084
|
init_chunk_store();
|
|
4068
4085
|
import { lstatSync as lstatSync3, readdirSync, existsSync as existsSync3 } from "node:fs";
|
|
4069
|
-
import { join as
|
|
4086
|
+
import { join as join4 } from "node:path";
|
|
4070
4087
|
|
|
4071
4088
|
// src/memory/topics-history/import-backups.ts
|
|
4072
4089
|
import { DatabaseSync, backup as backupSqlite } from "node:sqlite";
|
|
4073
4090
|
var TOPICS_BACKUP_RETENTION_MS = 7 * 864e5;
|
|
4074
4091
|
|
|
4092
|
+
// src/travel/exclusions.ts
|
|
4093
|
+
import { readFileSync as readFileSync3 } from "node:fs";
|
|
4094
|
+
import { isAbsolute, join as join3, posix, resolve } from "node:path";
|
|
4095
|
+
|
|
4096
|
+
// src/tools/selfie-references.ts
|
|
4097
|
+
var DEFAULT_REFERENCES = [
|
|
4098
|
+
{ name: "default", p: "images/xiaomei_clean_v2.png" },
|
|
4099
|
+
{ name: "v3", p: "images/xiaomei_clean_v3.png" },
|
|
4100
|
+
{ name: "base", p: "images/xiaomei_base_v3.jpg" },
|
|
4101
|
+
{ name: "blue", p: "images/xiaomei_blue_shirt_base.jpg" },
|
|
4102
|
+
{ name: "office", p: "images/xiaomei_office_final.jpg" }
|
|
4103
|
+
];
|
|
4104
|
+
function selfieReferences(config) {
|
|
4105
|
+
const cfg = config;
|
|
4106
|
+
const refs = cfg?.my_selfie?.references || cfg?.tools?.my_selfie?.references;
|
|
4107
|
+
if (!Array.isArray(refs) || refs.length === 0) return DEFAULT_REFERENCES;
|
|
4108
|
+
const valid = refs.filter((ref) => typeof ref?.name === "string" && typeof ref?.path === "string").map((ref) => ({ name: ref.name, p: ref.path }));
|
|
4109
|
+
return valid.length ? valid : DEFAULT_REFERENCES;
|
|
4110
|
+
}
|
|
4111
|
+
|
|
4075
4112
|
// src/travel/exclusions.ts
|
|
4076
4113
|
var EXCLUDED_NAMES = /* @__PURE__ */ new Set(["node_modules", ".venv", ".venvs", "venv", ".git", "__pycache__", ".cache", "cache", "caches", "tmp", "temp", ".playwright-mcp", ".DS_Store"]);
|
|
4077
4114
|
var EXCLUDED_TOP = /* @__PURE__ */ new Set(["logs", "diagnostics", "node_modules", "CogniFold", "everos-venv", "ollama-models", ".log-baseline.txt"]);
|
|
@@ -4097,17 +4134,46 @@ function excludeReason(relPath, isDir) {
|
|
|
4097
4134
|
}
|
|
4098
4135
|
var WORKSPACE_MEMORY_DIRS = /* @__PURE__ */ new Set(["topics", "memory", "inner-voice", ".calendar", ".proactive", ".system", "docs", "skills"]);
|
|
4099
4136
|
function workspaceTopFileReason(name, size) {
|
|
4100
|
-
if (!(/\.md$/i.test(name) || name === ".gitignore")) return `workspace \u9876\u5C42\u975E md \u6587\u4EF6\u4E0D\u968F\u884C\uFF08${name}\uFF09`;
|
|
4101
|
-
if (typeof size === "number" && size > WORKSPACE_BIG_FILE_BYTES) return `workspace \u9876\u5C42
|
|
4137
|
+
if (!(/\.md$/i.test(name) || name === ".gitignore" || name === ".skill-gate.json")) return `workspace \u9876\u5C42\u975E md \u6587\u4EF6\u4E0D\u968F\u884C\uFF08${name}\uFF09`;
|
|
4138
|
+
if (typeof size === "number" && size > WORKSPACE_BIG_FILE_BYTES) return `workspace \u9876\u5C42\u6587\u4EF6\u8D85 32MB \u4E0D\u968F\u884C\uFF08${name}\uFF09`;
|
|
4102
4139
|
return void 0;
|
|
4103
4140
|
}
|
|
4104
4141
|
var WORKSPACE_MEDIA_EXT = /\.(mp4|mov|mkv|webm|avi|m4v|flv|wmv|mpg|mpeg|mp3|wav|flac|aac|ogg|opus|m4a|zip|rar|7z|dmg|iso)$/i;
|
|
4105
4142
|
var WORKSPACE_BIG_FILE_BYTES = 32 * 1024 * 1024;
|
|
4106
4143
|
var RETURN_MEDIA_KEEP_MS = 14 * 24 * 3600 * 1e3;
|
|
4144
|
+
function isTopicsCleaningWorkPath(relPath) {
|
|
4145
|
+
return /^topics-history\/[a-f0-9]{64}\/(?:page-cache|batches\/[a-f0-9]{64}\/(?:snapshot|cleaned|work))(?:\/|$)/.test(relPath);
|
|
4146
|
+
}
|
|
4147
|
+
function isWorkspacePortraitPath(rel) {
|
|
4148
|
+
return rel.startsWith("workspace/") && /\.(?:png|jpe?g|webp)$/i.test(rel);
|
|
4149
|
+
}
|
|
4150
|
+
function withSelfieReferences(stateDir, configPath, exclude) {
|
|
4151
|
+
let config = {};
|
|
4152
|
+
try {
|
|
4153
|
+
config = JSON.parse(readFileSync3(configPath, "utf8")) ?? {};
|
|
4154
|
+
} catch {
|
|
4155
|
+
}
|
|
4156
|
+
const workspace = resolve(stateDir, "workspace");
|
|
4157
|
+
if (config.agents?.defaults?.workspace && resolve(config.agents.defaults.workspace) !== workspace) return exclude;
|
|
4158
|
+
const photos = /* @__PURE__ */ new Set();
|
|
4159
|
+
const parents = /* @__PURE__ */ new Set();
|
|
4160
|
+
for (const { p } of selfieReferences(config)) {
|
|
4161
|
+
if (!p || isAbsolute(p) || p.includes("..") || /[\\:\x00-\x1f]/.test(p)) continue;
|
|
4162
|
+
const rel = posix.join("workspace", p);
|
|
4163
|
+
if (!isWorkspacePortraitPath(rel)) continue;
|
|
4164
|
+
photos.add(rel);
|
|
4165
|
+
for (let dir = posix.dirname(rel); dir !== "workspace"; dir = posix.dirname(dir)) parents.add(dir);
|
|
4166
|
+
}
|
|
4167
|
+
return (rel, isDir, size, mtimeMs) => {
|
|
4168
|
+
if (!excludeReason(rel, isDir) && (isDir ? parents.has(rel) : photos.has(rel) && (size === void 0 || size <= WORKSPACE_BIG_FILE_BYTES))) return;
|
|
4169
|
+
return exclude(rel, isDir, size, mtimeMs);
|
|
4170
|
+
};
|
|
4171
|
+
}
|
|
4107
4172
|
function syncExcludeWith(mediaPolicy) {
|
|
4108
4173
|
return (relPath, isDir, size, mtimeMs) => {
|
|
4109
4174
|
const parts = relPath.split("/");
|
|
4110
4175
|
const name = parts[parts.length - 1];
|
|
4176
|
+
if (isTopicsCleaningWorkPath(relPath)) return "\u8BDD\u9898\u6E05\u6D17\u4E2D\u95F4\u6587\u4EF6\u7559\u5728\u539F\u673A";
|
|
4111
4177
|
if (parts[0] === "runtime") return "\u5F15\u64CE\u5B89\u88C5\u526F\u672C\u4E0D\u968F\u884C\uFF08\u4E91\u7AEF\u72EC\u7ACB\u5B89\u88C5\u5F53\u524D\u5F15\u64CE\uFF09";
|
|
4112
4178
|
if (!relPath.includes("/") && (relPath === "config.json" || relPath === "secrets.env")) {
|
|
4113
4179
|
return "\u5BF9\u7AEF\u5B89\u88C5\u4EA7\u7269\u4FDD\u62A4\uFF08config \u8D70 config-remap\uFF0Csecrets \u8D70 travel-setup\uFF09";
|
|
@@ -4141,8 +4207,9 @@ function syncExcludeWith(mediaPolicy) {
|
|
|
4141
4207
|
var syncExcludeReason = syncExcludeWith("none");
|
|
4142
4208
|
var syncExcludeToCloud = syncExcludeWith("none");
|
|
4143
4209
|
var syncExcludeToLocal = syncExcludeWith({ windowMs: RETURN_MEDIA_KEEP_MS });
|
|
4144
|
-
function syncExcludeWithDays(days) {
|
|
4145
|
-
|
|
4210
|
+
function syncExcludeWithDays(days, stateDir) {
|
|
4211
|
+
const exclude = days > 0 ? syncExcludeWith({ windowMs: days * 864e5 }) : syncExcludeWith("none");
|
|
4212
|
+
return stateDir ? withSelfieReferences(stateDir, join3(stateDir, "config.json"), exclude) : exclude;
|
|
4146
4213
|
}
|
|
4147
4214
|
|
|
4148
4215
|
// src/travel/tree-manifest.ts
|
|
@@ -4186,7 +4253,7 @@ async function buildTreeManifest(root, store, opts = {}) {
|
|
|
4186
4253
|
const walk = async (dir, rel) => {
|
|
4187
4254
|
for (const entry of readdirSync(dir, { withFileTypes: true })) {
|
|
4188
4255
|
const childRel = rel ? `${rel}/${entry.name}` : entry.name;
|
|
4189
|
-
const childPath =
|
|
4256
|
+
const childPath = join4(dir, entry.name);
|
|
4190
4257
|
if (entry.isSymbolicLink()) {
|
|
4191
4258
|
excluded.push(`${childRel} \u2014 symlink refused`);
|
|
4192
4259
|
continue;
|
|
@@ -4243,7 +4310,7 @@ async function buildTreeManifest(root, store, opts = {}) {
|
|
|
4243
4310
|
if (opts.requireStableDatabases) {
|
|
4244
4311
|
for (const [rel, before] of dbBefore) {
|
|
4245
4312
|
try {
|
|
4246
|
-
if (fileFingerprint(
|
|
4313
|
+
if (fileFingerprint(join4(root, rel)) !== before) throw Error();
|
|
4247
4314
|
} catch {
|
|
4248
4315
|
throw Error("TRAVEL_DATABASE_SOURCE_CHANGED");
|
|
4249
4316
|
}
|
|
@@ -4262,21 +4329,21 @@ import {
|
|
|
4262
4329
|
existsSync as existsSync6,
|
|
4263
4330
|
mkdirSync as mkdirSync4,
|
|
4264
4331
|
openSync as openSync5,
|
|
4265
|
-
readFileSync as
|
|
4332
|
+
readFileSync as readFileSync5,
|
|
4266
4333
|
readSync as readSync5,
|
|
4267
4334
|
renameSync as renameSync4,
|
|
4268
4335
|
rmSync as rmSync2,
|
|
4269
4336
|
writeFileSync as writeFileSync4
|
|
4270
4337
|
} from "node:fs";
|
|
4271
4338
|
import { createHash as createHash3 } from "node:crypto";
|
|
4272
|
-
import { join as
|
|
4339
|
+
import { join as join7, resolve as resolve3 } from "node:path";
|
|
4273
4340
|
init_capture_cache();
|
|
4274
4341
|
init_apply_transaction();
|
|
4275
4342
|
|
|
4276
4343
|
// src/travel/sqlite-snapshot.ts
|
|
4277
4344
|
import { DatabaseSync as DatabaseSync2 } from "node:sqlite";
|
|
4278
4345
|
import { closeSync as closeSync4, fstatSync as fstatSync3, openSync as openSync4, readSync as readSync4, readdirSync as readdirSync2, statSync } from "node:fs";
|
|
4279
|
-
import { join as
|
|
4346
|
+
import { join as join6 } from "node:path";
|
|
4280
4347
|
|
|
4281
4348
|
// src/travel/sqlite-wal.ts
|
|
4282
4349
|
import { closeSync as closeSync3, existsSync as existsSync5, fstatSync as fstatSync2, openSync as openSync3, readSync as readSync3 } from "node:fs";
|
|
@@ -4325,7 +4392,7 @@ function validateSqliteWal(path5) {
|
|
|
4325
4392
|
// src/travel/sqlite-snapshot.ts
|
|
4326
4393
|
function validateDatabaseSnapshots(root, files) {
|
|
4327
4394
|
for (const file of files) {
|
|
4328
|
-
const path5 =
|
|
4395
|
+
const path5 = join6(root, file), fd = openSync4(path5, "r"), header = Buffer.alloc(100);
|
|
4329
4396
|
try {
|
|
4330
4397
|
const size = fstatSync3(fd).size, n = readSync4(fd, header, 0, header.length, 0);
|
|
4331
4398
|
const encoded = header.readUInt16BE(16), pageSize = encoded === 1 ? 65536 : encoded;
|
|
@@ -4436,7 +4503,7 @@ var SYNC_STATE_FILE = ".travel/sync-state.json";
|
|
|
4436
4503
|
var DB_RE = /\.(db|sqlite)$/;
|
|
4437
4504
|
function readSyncState(root) {
|
|
4438
4505
|
try {
|
|
4439
|
-
const value = JSON.parse(
|
|
4506
|
+
const value = JSON.parse(readFileSync5(join7(root, SYNC_STATE_FILE), "utf8"));
|
|
4440
4507
|
if (value?.version !== 1 || !value.files || typeof value.files !== "object") throw new Error("bad");
|
|
4441
4508
|
const files = {};
|
|
4442
4509
|
for (const [rel, sha] of Object.entries(value.files)) {
|
|
@@ -4450,10 +4517,10 @@ function readSyncState(root) {
|
|
|
4450
4517
|
}
|
|
4451
4518
|
}
|
|
4452
4519
|
function writeSyncState(root, files) {
|
|
4453
|
-
mkdirSync4(
|
|
4454
|
-
const temp =
|
|
4520
|
+
mkdirSync4(join7(root, ".travel"), { recursive: true, mode: 448 });
|
|
4521
|
+
const temp = join7(root, ".travel", "sync-state.json.tmp");
|
|
4455
4522
|
writeFileSync4(temp, JSON.stringify({ version: 1, syncedAt: Date.now(), files }));
|
|
4456
|
-
renameSync4(temp,
|
|
4523
|
+
renameSync4(temp, join7(root, SYNC_STATE_FILE));
|
|
4457
4524
|
}
|
|
4458
4525
|
function fileSha(path5) {
|
|
4459
4526
|
try {
|
|
@@ -4476,10 +4543,11 @@ function fileSha(path5) {
|
|
|
4476
4543
|
}
|
|
4477
4544
|
async function applyTree(root, manifestValue, store, lastState, onProgress) {
|
|
4478
4545
|
const manifest = parseTreeManifest(manifestValue);
|
|
4479
|
-
root =
|
|
4546
|
+
root = resolve3(root);
|
|
4480
4547
|
if (ownEnginePid(root, 0) !== null) throw Error("TRAVEL_DESTINATION_RUNNING");
|
|
4481
4548
|
mkdirSync4(root, { recursive: true, mode: 448 });
|
|
4482
4549
|
recoverPendingApply(root);
|
|
4550
|
+
if (lastState === void 0) lastState = readSyncState(root);
|
|
4483
4551
|
const newRels = new Set(manifest.files.map((file) => file.rel));
|
|
4484
4552
|
const byRel = new Map(manifest.files.map((file) => [file.rel, file]));
|
|
4485
4553
|
for (const file of manifest.files) if (/\.(db|sqlite)-(wal|shm)$/.test(file.rel) && !byRel.has(file.rel.replace(/-(wal|shm)$/, ""))) throw Error("TRAVEL_DATABASE_FAMILY_INCOMPLETE");
|
|
@@ -4537,6 +4605,8 @@ async function applyTree(root, manifestValue, store, lastState, onProgress) {
|
|
|
4537
4605
|
if (lastState) {
|
|
4538
4606
|
for (const [rel, sha] of Object.entries(lastState.files)) {
|
|
4539
4607
|
if (newRels.has(rel)) continue;
|
|
4608
|
+
if (isTopicsCleaningWorkPath(rel)) continue;
|
|
4609
|
+
if (isWorkspacePortraitPath(rel) && syncExcludeReason(rel, false)) continue;
|
|
4540
4610
|
const dest = assertPath(root, rel);
|
|
4541
4611
|
if (!existsSync6(dest)) continue;
|
|
4542
4612
|
const before = fileFingerprint(dest);
|
|
@@ -4562,12 +4632,12 @@ async function applyTree(root, manifestValue, store, lastState, onProgress) {
|
|
|
4562
4632
|
const stage = newApplyStage(root);
|
|
4563
4633
|
try {
|
|
4564
4634
|
for (const file of changed.values()) {
|
|
4565
|
-
await store.restoreFile(file.content, assertPath(
|
|
4635
|
+
await store.restoreFile(file.content, assertPath(join7(stage.path, "new"), file.rel), file.mode);
|
|
4566
4636
|
applied++;
|
|
4567
4637
|
appliedBytes += file.content.bytes;
|
|
4568
4638
|
onProgress?.({ files: applied, bytes: appliedBytes });
|
|
4569
4639
|
}
|
|
4570
|
-
validateDatabaseSnapshots(
|
|
4640
|
+
validateDatabaseSnapshots(join7(stage.path, "new"), dbs);
|
|
4571
4641
|
validateDatabaseSnapshots(root, unchangedDbs);
|
|
4572
4642
|
if (ownEnginePid(root, 0) !== null) throw Error("TRAVEL_DESTINATION_RUNNING");
|
|
4573
4643
|
promoteFiles(root, stage, [...promotions.values()], () => {
|
|
@@ -4578,7 +4648,7 @@ async function applyTree(root, manifestValue, store, lastState, onProgress) {
|
|
|
4578
4648
|
rmSync2(assertPath(root, ".travel/pre-copy-only.json"), { force: true });
|
|
4579
4649
|
seedCaptureCache(root, manifest.files);
|
|
4580
4650
|
} catch (error) {
|
|
4581
|
-
if (!existsSync6(
|
|
4651
|
+
if (!existsSync6(join7(root, ".travel/apply-transaction.json"))) rmSync2(stage.path, { recursive: true, force: true });
|
|
4582
4652
|
throw error;
|
|
4583
4653
|
}
|
|
4584
4654
|
return { applied, deleted, skipped };
|
|
@@ -4588,14 +4658,14 @@ function sidecarsOf(dbRel) {
|
|
|
4588
4658
|
}
|
|
4589
4659
|
|
|
4590
4660
|
// src/travel/checked-apply.ts
|
|
4591
|
-
import { isAbsolute, join as
|
|
4661
|
+
import { isAbsolute as isAbsolute2, join as join8 } from "node:path";
|
|
4592
4662
|
init_chunk_store();
|
|
4593
4663
|
init_apply_transaction();
|
|
4594
4664
|
import { existsSync as existsSync7, mkdirSync as mkdirSync5, writeFileSync as writeFileSync5 } from "node:fs";
|
|
4595
4665
|
function markPreCopyOnly(root) {
|
|
4596
|
-
if (!
|
|
4666
|
+
if (!isAbsolute2(root)) throw Error("TRAVEL_UNSAFE_PATH");
|
|
4597
4667
|
if (ownEnginePid(root, 0) !== null) throw Error("TRAVEL_DESTINATION_RUNNING");
|
|
4598
|
-
mkdirSync5(
|
|
4668
|
+
mkdirSync5(join8(root, ".travel"), { recursive: true, mode: 448 });
|
|
4599
4669
|
writeFileSync5(assertPath(root, ".travel/pre-copy-only.json"), JSON.stringify({ version: 1, databasesVerified: false }), { mode: 384 });
|
|
4600
4670
|
return { marked: true };
|
|
4601
4671
|
}
|
|
@@ -4605,15 +4675,15 @@ function assertReturnSourceInactive(root) {
|
|
|
4605
4675
|
}
|
|
4606
4676
|
async function runCheckedCapture(root, days = 14) {
|
|
4607
4677
|
assertReturnSourceInactive(root);
|
|
4608
|
-
return buildTreeManifest(root, openChunkStore(
|
|
4678
|
+
return buildTreeManifest(root, openChunkStore(join8(root, ".travel/chunks")), { exclude: syncExcludeWithDays(days, root), requireStableDatabases: true });
|
|
4609
4679
|
}
|
|
4610
4680
|
async function runCheckedApply(root, manifest, pre = false) {
|
|
4611
|
-
if (!
|
|
4681
|
+
if (!isAbsolute2(root)) throw Error("TRAVEL_UNSAFE_PATH");
|
|
4612
4682
|
if (pre) {
|
|
4613
4683
|
markPreCopyOnly(root);
|
|
4614
4684
|
return { applied: 0, deleted: [], skipped: [] };
|
|
4615
4685
|
}
|
|
4616
|
-
return applyTree(root, manifest, openChunkStore(
|
|
4686
|
+
return applyTree(root, manifest, openChunkStore(join8(root, ".travel/chunks")));
|
|
4617
4687
|
}
|
|
4618
4688
|
|
|
4619
4689
|
// src/travel/sync-worker.ts
|
|
@@ -4662,15 +4732,15 @@ var ProtocolReader = class {
|
|
|
4662
4732
|
}
|
|
4663
4733
|
readLine() {
|
|
4664
4734
|
if (this.waiter) throw new Error("TRAVEL_PROTOCOL_BUSY");
|
|
4665
|
-
return new Promise((
|
|
4666
|
-
this.waiter = { kind: "line", resolve:
|
|
4735
|
+
return new Promise((resolve6) => {
|
|
4736
|
+
this.waiter = { kind: "line", resolve: resolve6 };
|
|
4667
4737
|
this.drain();
|
|
4668
4738
|
});
|
|
4669
4739
|
}
|
|
4670
4740
|
readBytes(count) {
|
|
4671
4741
|
if (this.waiter) throw new Error("TRAVEL_PROTOCOL_BUSY");
|
|
4672
|
-
return new Promise((
|
|
4673
|
-
this.waiter = { kind: "bytes", count, resolve:
|
|
4742
|
+
return new Promise((resolve6, reject) => {
|
|
4743
|
+
this.waiter = { kind: "bytes", count, resolve: resolve6, reject };
|
|
4674
4744
|
this.drain();
|
|
4675
4745
|
});
|
|
4676
4746
|
}
|
|
@@ -4684,9 +4754,9 @@ var ProtocolReader = class {
|
|
|
4684
4754
|
const rest = total.subarray(newline + 1);
|
|
4685
4755
|
this.buffer = rest.length ? [rest] : [];
|
|
4686
4756
|
this.buffered = rest.length;
|
|
4687
|
-
const
|
|
4757
|
+
const resolve6 = this.waiter.resolve;
|
|
4688
4758
|
this.waiter = null;
|
|
4689
|
-
|
|
4759
|
+
resolve6(line);
|
|
4690
4760
|
continue;
|
|
4691
4761
|
}
|
|
4692
4762
|
break;
|
|
@@ -4697,9 +4767,9 @@ var ProtocolReader = class {
|
|
|
4697
4767
|
const rest = total.subarray(this.waiter.count);
|
|
4698
4768
|
this.buffer = rest.length ? [rest] : [];
|
|
4699
4769
|
this.buffered = rest.length;
|
|
4700
|
-
const
|
|
4770
|
+
const resolve6 = this.waiter.resolve;
|
|
4701
4771
|
this.waiter = null;
|
|
4702
|
-
|
|
4772
|
+
resolve6(body);
|
|
4703
4773
|
continue;
|
|
4704
4774
|
}
|
|
4705
4775
|
break;
|
|
@@ -4714,8 +4784,13 @@ var ProtocolReader = class {
|
|
|
4714
4784
|
}
|
|
4715
4785
|
};
|
|
4716
4786
|
var WorkerConnection = class _WorkerConnection {
|
|
4717
|
-
constructor(child) {
|
|
4787
|
+
constructor(child, classifyExit = false) {
|
|
4718
4788
|
this.child = child;
|
|
4789
|
+
this.classifyExit = classifyExit;
|
|
4790
|
+
this.closed = new Promise((resolve6) => child.once("close", (code, signal) => resolve6({ code, signal })));
|
|
4791
|
+
child.stderr?.on("data", (chunk) => {
|
|
4792
|
+
this.stderr = (this.stderr + String(chunk)).slice(-4096);
|
|
4793
|
+
});
|
|
4719
4794
|
child.stdout.on("data", (chunk) => this.reader.push(chunk));
|
|
4720
4795
|
child.stdout.on("end", () => this.reader.end());
|
|
4721
4796
|
const die = () => {
|
|
@@ -4726,25 +4801,51 @@ var WorkerConnection = class _WorkerConnection {
|
|
|
4726
4801
|
child.stdout.on("error", die);
|
|
4727
4802
|
child.stdin.on("error", die);
|
|
4728
4803
|
child.on("error", die);
|
|
4729
|
-
child.on("exit",
|
|
4804
|
+
child.on("exit", () => {
|
|
4805
|
+
this.broken = true;
|
|
4806
|
+
this.onDied?.();
|
|
4807
|
+
});
|
|
4808
|
+
child.on("close", die);
|
|
4730
4809
|
}
|
|
4731
4810
|
reader = new ProtocolReader();
|
|
4732
4811
|
chain = Promise.resolve();
|
|
4733
4812
|
broken = false;
|
|
4813
|
+
stderr = "";
|
|
4814
|
+
closed;
|
|
4734
4815
|
// 1123 妹妹 review P0:字段按声明序初始化——died 的 executor 先跑,若
|
|
4735
4816
|
// onDied 声明在其后带默认值会把 resolve 覆盖成 noop,died 永远 pending,
|
|
4736
4817
|
// race 防挂整个落空。onDied 必须先声明且默认 null。
|
|
4737
4818
|
onDied = null;
|
|
4738
|
-
died = new Promise((
|
|
4739
|
-
this.onDied =
|
|
4819
|
+
died = new Promise((resolve6) => {
|
|
4820
|
+
this.onDied = resolve6;
|
|
4740
4821
|
});
|
|
4741
4822
|
static from(child) {
|
|
4742
4823
|
return new _WorkerConnection(child);
|
|
4743
4824
|
}
|
|
4744
4825
|
static async fromSource(child, source, root) {
|
|
4745
|
-
const worker = new _WorkerConnection(child);
|
|
4746
|
-
|
|
4747
|
-
|
|
4826
|
+
const worker = new _WorkerConnection(child, true);
|
|
4827
|
+
try {
|
|
4828
|
+
if ((await worker.request({ module: source, root })).bootstrap !== true) throw Error("TRAVEL_PROTOCOL_MISMATCH");
|
|
4829
|
+
return worker;
|
|
4830
|
+
} catch (error) {
|
|
4831
|
+
await worker.close();
|
|
4832
|
+
throw error;
|
|
4833
|
+
}
|
|
4834
|
+
}
|
|
4835
|
+
async disconnected() {
|
|
4836
|
+
if (!this.classifyExit) throw Error("TRAVEL_CONNECTION_CLOSED");
|
|
4837
|
+
let timer;
|
|
4838
|
+
try {
|
|
4839
|
+
const exit = await Promise.race([this.closed, new Promise((resolve6) => {
|
|
4840
|
+
timer = setTimeout(() => resolve6(null), 500);
|
|
4841
|
+
})]);
|
|
4842
|
+
if (!exit || isConnectionError(Object.assign(new Error("ssh"), { code: exit.code, stderr: this.stderr }))) {
|
|
4843
|
+
throw Error("TRAVEL_CONNECTION_CLOSED");
|
|
4844
|
+
}
|
|
4845
|
+
throw Error(`TRAVEL_WORKER_EXIT (${exit.code ?? exit.signal}): ${this.stderr.trim()}`);
|
|
4846
|
+
} finally {
|
|
4847
|
+
if (timer) clearTimeout(timer);
|
|
4848
|
+
}
|
|
4748
4849
|
}
|
|
4749
4850
|
enqueue(operation) {
|
|
4750
4851
|
const next = this.chain.then(operation, operation);
|
|
@@ -4754,24 +4855,24 @@ var WorkerConnection = class _WorkerConnection {
|
|
|
4754
4855
|
}
|
|
4755
4856
|
request(header, payload) {
|
|
4756
4857
|
return this.enqueue(async () => {
|
|
4757
|
-
if (this.broken)
|
|
4758
|
-
const written = new Promise((
|
|
4858
|
+
if (this.broken) return this.disconnected();
|
|
4859
|
+
const written = new Promise((resolve6) => this.child.stdin.write(JSON.stringify(header) + "\n", () => resolve6()));
|
|
4759
4860
|
await Promise.race([written, this.died]);
|
|
4760
|
-
if (this.broken)
|
|
4861
|
+
if (this.broken) return this.disconnected();
|
|
4761
4862
|
if (payload) {
|
|
4762
|
-
const payloadWritten = new Promise((
|
|
4863
|
+
const payloadWritten = new Promise((resolve6) => this.child.stdin.write(payload, () => resolve6()));
|
|
4763
4864
|
await Promise.race([payloadWritten, this.died]);
|
|
4764
|
-
if (this.broken)
|
|
4865
|
+
if (this.broken) return this.disconnected();
|
|
4765
4866
|
}
|
|
4766
4867
|
const line = await this.reader.readLine();
|
|
4767
|
-
if (line === null)
|
|
4868
|
+
if (line === null) return this.disconnected();
|
|
4768
4869
|
const reply = JSON.parse(line);
|
|
4769
4870
|
if (reply?.error) throw new Error(reply.detail ? `${reply.error}: ${reply.detail}` : reply.error);
|
|
4770
4871
|
return reply;
|
|
4771
4872
|
});
|
|
4772
4873
|
}
|
|
4773
|
-
hello(pre = false) {
|
|
4774
|
-
return this.request({ op: "hello", pre });
|
|
4874
|
+
hello(pre = false, owner) {
|
|
4875
|
+
return this.request({ op: "hello", pre, owner });
|
|
4775
4876
|
}
|
|
4776
4877
|
async has(refs) {
|
|
4777
4878
|
return (await this.request({ op: "has", refs })).missing;
|
|
@@ -4782,15 +4883,15 @@ var WorkerConnection = class _WorkerConnection {
|
|
|
4782
4883
|
/** 拉块:应答行后跟原始明文字节(to-local 方向)。 */
|
|
4783
4884
|
async get(sha2563, bytes) {
|
|
4784
4885
|
return this.enqueue(async () => {
|
|
4785
|
-
if (this.broken)
|
|
4786
|
-
const written = new Promise((
|
|
4886
|
+
if (this.broken) return this.disconnected();
|
|
4887
|
+
const written = new Promise((resolve6) => this.child.stdin.write(JSON.stringify({ op: "get", sha256: sha2563, bytes }) + "\n", () => resolve6()));
|
|
4787
4888
|
await Promise.race([written, this.died]);
|
|
4788
|
-
if (this.broken)
|
|
4889
|
+
if (this.broken) return this.disconnected();
|
|
4789
4890
|
const line = await this.reader.readLine();
|
|
4790
|
-
if (line === null)
|
|
4891
|
+
if (line === null) return this.disconnected();
|
|
4791
4892
|
const reply = JSON.parse(line);
|
|
4792
4893
|
if (reply?.error) throw new Error(reply.detail ? `${reply.error}: ${reply.detail}` : reply.error);
|
|
4793
|
-
return
|
|
4894
|
+
return this.reader.readBytes(reply.bytes).catch(() => this.disconnected());
|
|
4794
4895
|
});
|
|
4795
4896
|
}
|
|
4796
4897
|
async manifest(manifest) {
|
|
@@ -4810,11 +4911,11 @@ var WorkerConnection = class _WorkerConnection {
|
|
|
4810
4911
|
/** A single reader serializes descriptors and acknowledgements; payloads use OSS. */
|
|
4811
4912
|
pushViaOss(refs, config, receive, progress) {
|
|
4812
4913
|
return this.enqueue(async () => {
|
|
4813
|
-
if (this.broken)
|
|
4914
|
+
if (this.broken) return this.disconnected();
|
|
4814
4915
|
this.child.stdin.write(JSON.stringify({ op: "oss-push", refs, config }) + "\n");
|
|
4815
4916
|
for (; ; ) {
|
|
4816
4917
|
const line = await this.reader.readLine();
|
|
4817
|
-
if (line === null)
|
|
4918
|
+
if (line === null) return this.disconnected();
|
|
4818
4919
|
const value = JSON.parse(line);
|
|
4819
4920
|
if (value.error) throw Error(value.detail ? `${value.error}: ${value.detail}` : value.error);
|
|
4820
4921
|
if (value.progress) {
|
|
@@ -4848,20 +4949,20 @@ var WorkerConnection = class _WorkerConnection {
|
|
|
4848
4949
|
} catch {
|
|
4849
4950
|
}
|
|
4850
4951
|
await Promise.race([
|
|
4851
|
-
new Promise((
|
|
4952
|
+
new Promise((resolve6) => this.child.once("exit", () => resolve6())),
|
|
4852
4953
|
this.died,
|
|
4853
|
-
new Promise((
|
|
4854
|
-
setTimeout(
|
|
4954
|
+
new Promise((resolve6) => {
|
|
4955
|
+
setTimeout(resolve6, 1e4).unref?.();
|
|
4855
4956
|
})
|
|
4856
4957
|
]);
|
|
4857
4958
|
}
|
|
4858
4959
|
};
|
|
4859
4960
|
async function runWorker(stateDir) {
|
|
4860
|
-
const store = openChunkStore(
|
|
4961
|
+
const store = openChunkStore(join15(stateDir, ".travel/chunks"));
|
|
4861
4962
|
const reader = new ProtocolReader();
|
|
4862
4963
|
process.stdin.on("data", (chunk) => reader.push(chunk));
|
|
4863
4964
|
process.stdin.on("end", () => reader.end());
|
|
4864
|
-
const ended = new Promise((
|
|
4965
|
+
const ended = new Promise((resolve6) => process.stdin.on("end", resolve6));
|
|
4865
4966
|
let pendingManifest = null;
|
|
4866
4967
|
let capturedForReturn = false;
|
|
4867
4968
|
let databasePiecesSeeded = false;
|
|
@@ -4873,6 +4974,14 @@ async function runWorker(stateDir) {
|
|
|
4873
4974
|
const op = JSON.parse(line);
|
|
4874
4975
|
switch (op.op) {
|
|
4875
4976
|
case "hello": {
|
|
4977
|
+
if (op.owner !== void 0) {
|
|
4978
|
+
try {
|
|
4979
|
+
const owner = JSON.parse(readFileSync12(join15(stateDir, ".travel/owner.json"), "utf8"));
|
|
4980
|
+
if (owner.instanceId !== op.owner) throw Error("TRAVEL_WRONG_REMOTE_OWNER");
|
|
4981
|
+
} catch {
|
|
4982
|
+
throw Error("TRAVEL_WRONG_REMOTE_OWNER");
|
|
4983
|
+
}
|
|
4984
|
+
}
|
|
4876
4985
|
if (op.pre === true) markPreCopyOnly(stateDir);
|
|
4877
4986
|
reply = { ok: true, version: 3, stateDir, region: await cloudRegion() };
|
|
4878
4987
|
break;
|
|
@@ -4914,7 +5023,7 @@ async function runWorker(stateDir) {
|
|
|
4914
5023
|
if (typeof sha !== "string" || !/^[a-f0-9]{64}$/.test(sha) || !Number.isSafeInteger(bytes) || bytes < 1 || bytes > CHUNK_BYTES) throw new Error("TRAVEL_INVALID_CHUNK");
|
|
4915
5024
|
const body = await store.read(sha, bytes);
|
|
4916
5025
|
process.stdout.write(JSON.stringify({ ok: true, bytes: body.length }) + "\n");
|
|
4917
|
-
await new Promise((
|
|
5026
|
+
await new Promise((resolve6) => process.stdout.write(body, () => resolve6()));
|
|
4918
5027
|
reply = null;
|
|
4919
5028
|
break;
|
|
4920
5029
|
}
|