@tangle-network/agent-app 0.63.43 → 0.63.45
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/{DesignCanvas-VKZ7Y3JU.js → DesignCanvas-Q65UV4D5.js} +3 -2
- package/dist/{DesignCanvasEditor-3V53YLLZ.js → DesignCanvasEditor-THEFESEX.js} +3 -2
- package/dist/app-auth/index.js +2 -1
- package/dist/app-auth/index.js.map +1 -1
- package/dist/chat-routes/index.js +9 -7
- package/dist/chat-routes/index.js.map +1 -1
- package/dist/{chunk-F25HBFAM.js → chunk-32O4Q3GK.js} +14 -4
- package/dist/{chunk-F25HBFAM.js.map → chunk-32O4Q3GK.js.map} +1 -1
- package/dist/{chunk-ZGGEYYEQ.js → chunk-4E2MUVGL.js} +3 -3
- package/dist/chunk-5VE4IDSU.js +75 -0
- package/dist/chunk-5VE4IDSU.js.map +1 -0
- package/dist/{chunk-CTREXL3O.js → chunk-67CSI5PU.js} +2 -65
- package/dist/chunk-67CSI5PU.js.map +1 -0
- package/dist/{chunk-DPTNKH22.js → chunk-H6BRJU5M.js} +1 -73
- package/dist/chunk-H6BRJU5M.js.map +1 -0
- package/dist/chunk-INW4FSID.js +66 -0
- package/dist/chunk-INW4FSID.js.map +1 -0
- package/dist/{chunk-RZMQ2J5P.js → chunk-L63GYPYY.js} +3 -3
- package/dist/{chunk-XTQLZG6M.js → chunk-YQUZQHAD.js} +2 -2
- package/dist/design-canvas/index.js +2 -1
- package/dist/design-canvas/index.js.map +1 -1
- package/dist/design-canvas-react/engine.js +2 -1
- package/dist/design-canvas-react/index.js +3 -2
- package/dist/design-canvas-react/index.js.map +1 -1
- package/dist/design-canvas-react/lazy.js +1 -1
- package/dist/platform/index.js +2 -1
- package/dist/platform/index.js.map +1 -1
- package/dist/public-consultation/index.js +6 -4
- package/dist/public-consultation/index.js.map +1 -1
- package/dist/sandbox/index.js +7 -5
- package/dist/sequences/index.js +2 -1
- package/dist/sequences/index.js.map +1 -1
- package/dist/signoff/browser-budget.d.ts +6 -0
- package/dist/signoff/cli.js +1 -1
- package/dist/signoff/index.js +1 -1
- package/dist/skills-placement/box.js +4 -2
- package/dist/skills-placement/box.js.map +1 -1
- package/dist/vault/store/box-state.d.ts +125 -0
- package/dist/vault/store/coordinator.d.ts +9 -1
- package/dist/vault/store/index.d.ts +4 -0
- package/dist/vault/store/index.js +1227 -4
- package/dist/vault/store/index.js.map +1 -1
- package/dist/vault/store/sandbox-engine.d.ts +19 -0
- package/dist/vault/store/sandbox-hydrate.d.ts +59 -0
- package/dist/vault/store/sandbox-reconcile.d.ts +55 -0
- package/dist/vault/store/sandbox-sync.d.ts +37 -1
- package/dist/vault/store/store.d.ts +10 -1
- package/dist/web/index.js +7 -5
- package/package.json +1 -1
- package/dist/chunk-CTREXL3O.js.map +0 -1
- package/dist/chunk-DPTNKH22.js.map +0 -1
- /package/dist/{DesignCanvas-VKZ7Y3JU.js.map → DesignCanvas-Q65UV4D5.js.map} +0 -0
- /package/dist/{DesignCanvasEditor-3V53YLLZ.js.map → DesignCanvasEditor-THEFESEX.js.map} +0 -0
- /package/dist/{chunk-ZGGEYYEQ.js.map → chunk-4E2MUVGL.js.map} +0 -0
- /package/dist/{chunk-RZMQ2J5P.js.map → chunk-L63GYPYY.js.map} +0 -0
- /package/dist/{chunk-XTQLZG6M.js.map → chunk-YQUZQHAD.js.map} +0 -0
|
@@ -1,6 +1,14 @@
|
|
|
1
1
|
import {
|
|
2
2
|
compareIncarnationBaseline
|
|
3
3
|
} from "../../chunk-GS7VWV26.js";
|
|
4
|
+
import {
|
|
5
|
+
sniffMime
|
|
6
|
+
} from "../../chunk-5VE4IDSU.js";
|
|
7
|
+
import {
|
|
8
|
+
readSandboxBinaryBytes,
|
|
9
|
+
shellQuote,
|
|
10
|
+
statSandboxFileSize
|
|
11
|
+
} from "../../chunk-INW4FSID.js";
|
|
4
12
|
import {
|
|
5
13
|
sha256Hex
|
|
6
14
|
} from "../../chunk-HY57QRRW.js";
|
|
@@ -383,6 +391,94 @@ function flattenVaultTreePaths(nodes) {
|
|
|
383
391
|
});
|
|
384
392
|
}
|
|
385
393
|
|
|
394
|
+
// src/vault/store/box-state.ts
|
|
395
|
+
var VAULT_BOX_STATE_KEY = "vaultBoxState";
|
|
396
|
+
var VAULT_BOX_STATE_SCHEMA = "vault-box-state-v1";
|
|
397
|
+
var VAULT_BOX_MUTATION_STALE_MS = 10 * 6e4;
|
|
398
|
+
var VAULT_BOX_SYNC_MAX_SKIPS = 50;
|
|
399
|
+
function emptyVaultBoxState() {
|
|
400
|
+
return { schema: VAULT_BOX_STATE_SCHEMA, generation: 0, inFlight: null, sync: null, bootstrap: null };
|
|
401
|
+
}
|
|
402
|
+
function isRecord2(value) {
|
|
403
|
+
return typeof value === "object" && value !== null && !Array.isArray(value);
|
|
404
|
+
}
|
|
405
|
+
function parseIdentityRecord(value) {
|
|
406
|
+
if (!isRecord2(value)) return null;
|
|
407
|
+
if (typeof value.sandboxId !== "string" || !value.sandboxId) return null;
|
|
408
|
+
if (typeof value.filesystemIncarnationId !== "string" || !value.filesystemIncarnationId) return null;
|
|
409
|
+
return { sandboxId: value.sandboxId, filesystemIncarnationId: value.filesystemIncarnationId };
|
|
410
|
+
}
|
|
411
|
+
function parseVaultBoxState(value) {
|
|
412
|
+
if (value === void 0 || value === null) return emptyVaultBoxState();
|
|
413
|
+
if (!isRecord2(value) || value.schema !== VAULT_BOX_STATE_SCHEMA || typeof value.generation !== "number" || !Number.isSafeInteger(value.generation) || value.generation < 0) {
|
|
414
|
+
return { ...emptyVaultBoxState(), generation: Date.now() };
|
|
415
|
+
}
|
|
416
|
+
const inFlight = isRecord2(value.inFlight) && typeof value.inFlight.operationId === "string" && typeof value.inFlight.startedAt === "number" ? { operationId: value.inFlight.operationId, startedAt: value.inFlight.startedAt } : null;
|
|
417
|
+
const sync = parseSync(value.sync);
|
|
418
|
+
const bootstrap = parseBootstrap(value.bootstrap);
|
|
419
|
+
return { schema: VAULT_BOX_STATE_SCHEMA, generation: value.generation, inFlight, sync, bootstrap };
|
|
420
|
+
}
|
|
421
|
+
function isSyncedBoxWriter(state, writer) {
|
|
422
|
+
return writer?.kind === "sandbox" && typeof writer.id === "string" && state.sync !== null && writer.id === state.sync.sandboxId;
|
|
423
|
+
}
|
|
424
|
+
function withMutationStarted(state, operationId, now) {
|
|
425
|
+
return { ...state, inFlight: { operationId, startedAt: now } };
|
|
426
|
+
}
|
|
427
|
+
function withMutationFinished(state) {
|
|
428
|
+
return { ...state, generation: state.generation + 1, inFlight: null };
|
|
429
|
+
}
|
|
430
|
+
function withSyncInvalidated(state) {
|
|
431
|
+
return { ...state, generation: state.generation + 1, sync: null };
|
|
432
|
+
}
|
|
433
|
+
function withSync(state, sync) {
|
|
434
|
+
return { ...state, sync };
|
|
435
|
+
}
|
|
436
|
+
function withBootstrap(state, bootstrap) {
|
|
437
|
+
return { ...state, bootstrap };
|
|
438
|
+
}
|
|
439
|
+
function healStaleMutation(state, now) {
|
|
440
|
+
if (!state.inFlight || now - state.inFlight.startedAt < VAULT_BOX_MUTATION_STALE_MS) return null;
|
|
441
|
+
return withMutationFinished(state);
|
|
442
|
+
}
|
|
443
|
+
function viewOfVaultBoxState(state) {
|
|
444
|
+
return { generation: state.generation, inFlight: state.inFlight !== null, sync: state.sync, bootstrap: state.bootstrap };
|
|
445
|
+
}
|
|
446
|
+
function parseVaultBoxStateView(value) {
|
|
447
|
+
if (!isRecord2(value) || typeof value.generation !== "number" || typeof value.inFlight !== "boolean") return null;
|
|
448
|
+
const sync = parseSync(value.sync);
|
|
449
|
+
const bootstrap = parseBootstrap(value.bootstrap);
|
|
450
|
+
return { generation: value.generation, inFlight: value.inFlight, sync, bootstrap };
|
|
451
|
+
}
|
|
452
|
+
function parseVaultBoxSyncSkips(value) {
|
|
453
|
+
if (!Array.isArray(value) || value.length > VAULT_BOX_SYNC_MAX_SKIPS) return null;
|
|
454
|
+
const skips = [];
|
|
455
|
+
for (const entry of value) {
|
|
456
|
+
if (!isRecord2(entry) || typeof entry.path !== "string" || !entry.path || entry.reason !== "too_large" || typeof entry.detail !== "string") return null;
|
|
457
|
+
skips.push({ path: entry.path, reason: "too_large", detail: entry.detail.slice(0, 500) });
|
|
458
|
+
}
|
|
459
|
+
return skips;
|
|
460
|
+
}
|
|
461
|
+
function parseSync(value) {
|
|
462
|
+
const identity = parseIdentityRecord(value);
|
|
463
|
+
if (!identity || !isRecord2(value) || typeof value.contract !== "string" || !value.contract || typeof value.generation !== "number" || typeof value.syncedAt !== "string") return null;
|
|
464
|
+
const skipped = parseVaultBoxSyncSkips(value.skipped ?? []);
|
|
465
|
+
return skipped ? { ...identity, contract: value.contract, generation: value.generation, skipped, syncedAt: value.syncedAt } : null;
|
|
466
|
+
}
|
|
467
|
+
function parseBootstrap(value) {
|
|
468
|
+
const identity = parseIdentityRecord(value);
|
|
469
|
+
if (!identity || !isRecord2(value) || typeof value.marker !== "string" || !value.marker || typeof value.at !== "string") return null;
|
|
470
|
+
return { ...identity, marker: value.marker, at: value.at };
|
|
471
|
+
}
|
|
472
|
+
function sameBox(record, box) {
|
|
473
|
+
return record !== null && record.sandboxId === box.sandboxId && record.filesystemIncarnationId === box.filesystemIncarnationId;
|
|
474
|
+
}
|
|
475
|
+
function vaultBoxInSync(view, box, contract) {
|
|
476
|
+
return view !== null && !view.inFlight && view.sync !== null && view.sync.contract === contract && view.sync.generation === view.generation && sameBox(view.sync, box);
|
|
477
|
+
}
|
|
478
|
+
function sandboxBootstrapRecorded(view, box, marker) {
|
|
479
|
+
return view !== null && view.bootstrap !== null && view.bootstrap.marker === marker && sameBox(view.bootstrap, box);
|
|
480
|
+
}
|
|
481
|
+
|
|
386
482
|
// src/vault/store/store.ts
|
|
387
483
|
var VaultFileNotFoundError = class extends VaultPathError {
|
|
388
484
|
code = "vault.file_not_found";
|
|
@@ -485,6 +581,62 @@ function createVaultRuntime(config) {
|
|
|
485
581
|
const body = await response.json();
|
|
486
582
|
return body.state;
|
|
487
583
|
}
|
|
584
|
+
const localSandboxBaselines = /* @__PURE__ */ new Map();
|
|
585
|
+
async function readVaultSandboxBaseline(workspaceId, sandboxId) {
|
|
586
|
+
validateWorkspaceId(workspaceId);
|
|
587
|
+
if (!sandboxId) throw new Error("Sandbox ID is required for Vault baseline reads");
|
|
588
|
+
const namespace = coordinator;
|
|
589
|
+
if (!namespace) return localSandboxBaselines.get(`${workspaceId}:${sandboxId}`) ?? null;
|
|
590
|
+
const response = await postVaultCoordinator(namespace, workspaceId, "baseline/get", { workspaceId, sandboxId });
|
|
591
|
+
if (!response.ok) await throwVaultCoordinatorError(response, "Vault baseline read failed");
|
|
592
|
+
const body = await response.json();
|
|
593
|
+
return body.baseline;
|
|
594
|
+
}
|
|
595
|
+
async function writeVaultSandboxBaseline(baseline) {
|
|
596
|
+
validateWorkspaceId(baseline.workspaceId);
|
|
597
|
+
if (!baseline.sandboxId) throw new Error("Sandbox ID is required for Vault baseline writes");
|
|
598
|
+
const namespace = coordinator;
|
|
599
|
+
if (!namespace) {
|
|
600
|
+
localSandboxBaselines.set(`${baseline.workspaceId}:${baseline.sandboxId}`, baseline);
|
|
601
|
+
return;
|
|
602
|
+
}
|
|
603
|
+
const response = await postVaultCoordinator(namespace, baseline.workspaceId, "baseline/set", baseline);
|
|
604
|
+
if (!response.ok) await throwVaultCoordinatorError(response, "Vault baseline write failed");
|
|
605
|
+
}
|
|
606
|
+
async function readVaultBoxState(workspaceId) {
|
|
607
|
+
validateWorkspaceId(workspaceId);
|
|
608
|
+
const namespace = coordinator;
|
|
609
|
+
if (!namespace) return null;
|
|
610
|
+
try {
|
|
611
|
+
const response = await postVaultCoordinator(namespace, workspaceId, "box-state/get", { workspaceId });
|
|
612
|
+
if (!response.ok) return null;
|
|
613
|
+
const body = await response.json();
|
|
614
|
+
return parseVaultBoxStateView(body.state);
|
|
615
|
+
} catch {
|
|
616
|
+
return null;
|
|
617
|
+
}
|
|
618
|
+
}
|
|
619
|
+
async function recordVaultBoxSync(workspaceId, record) {
|
|
620
|
+
validateWorkspaceId(workspaceId);
|
|
621
|
+
const namespace = coordinator;
|
|
622
|
+
if (!namespace) return;
|
|
623
|
+
const response = await postVaultCoordinator(namespace, workspaceId, "box-state/sync", record);
|
|
624
|
+
if (!response.ok) await throwVaultCoordinatorError(response, "Vault box sync record failed");
|
|
625
|
+
}
|
|
626
|
+
async function invalidateVaultBoxSync(workspaceId) {
|
|
627
|
+
validateWorkspaceId(workspaceId);
|
|
628
|
+
const namespace = coordinator;
|
|
629
|
+
if (!namespace) return;
|
|
630
|
+
const response = await postVaultCoordinator(namespace, workspaceId, "box-state/invalidate", { workspaceId });
|
|
631
|
+
if (!response.ok) await throwVaultCoordinatorError(response, "Vault box sync invalidation failed");
|
|
632
|
+
}
|
|
633
|
+
async function recordSandboxBootstrap(workspaceId, confirmation) {
|
|
634
|
+
validateWorkspaceId(workspaceId);
|
|
635
|
+
const namespace = coordinator;
|
|
636
|
+
if (!namespace) return;
|
|
637
|
+
const response = await postVaultCoordinator(namespace, workspaceId, "box-state/bootstrap", confirmation);
|
|
638
|
+
if (!response.ok) await throwVaultCoordinatorError(response, "Sandbox bootstrap record failed");
|
|
639
|
+
}
|
|
488
640
|
return {
|
|
489
641
|
setVaultKV,
|
|
490
642
|
getVaultKV,
|
|
@@ -494,7 +646,15 @@ function createVaultRuntime(config) {
|
|
|
494
646
|
throwVaultCoordinatorError,
|
|
495
647
|
enqueueLocalVaultMutation,
|
|
496
648
|
commitVaultMutation,
|
|
497
|
-
readVaultFileState
|
|
649
|
+
readVaultFileState,
|
|
650
|
+
readVaultSandboxBaseline,
|
|
651
|
+
writeVaultSandboxBaseline,
|
|
652
|
+
readVaultBoxState,
|
|
653
|
+
recordVaultBoxSync,
|
|
654
|
+
invalidateVaultBoxSync,
|
|
655
|
+
recordSandboxBootstrap,
|
|
656
|
+
/** The product's frontmatter codec, for engines that read and write whole Vault documents. */
|
|
657
|
+
codec: config.codec
|
|
498
658
|
};
|
|
499
659
|
}
|
|
500
660
|
function createVaultFiles(runtime, config) {
|
|
@@ -786,6 +946,17 @@ function createVaultStore(config) {
|
|
|
786
946
|
|
|
787
947
|
// src/vault/store/coordinator.ts
|
|
788
948
|
var VAULT_FENCE_STORAGE_PREFIX = "vaultFence:";
|
|
949
|
+
var VAULT_BASELINE_CHUNK_CHARS = 32e3;
|
|
950
|
+
var VAULT_BASELINE_CHUNKED_SCHEMA = "vault-sandbox-baseline-chunked-v1";
|
|
951
|
+
function isChunkedVaultBaselineHead(value) {
|
|
952
|
+
return Boolean(value) && typeof value === "object" && value.schema === VAULT_BASELINE_CHUNKED_SCHEMA && Number.isInteger(value.chunks);
|
|
953
|
+
}
|
|
954
|
+
function vaultBaselineKey(sandboxId) {
|
|
955
|
+
return `vaultBaseline:${sandboxId}`;
|
|
956
|
+
}
|
|
957
|
+
function vaultBaselineChunkKey(sandboxId, index) {
|
|
958
|
+
return `vaultBaselineChunk:${sandboxId}:${index}`;
|
|
959
|
+
}
|
|
789
960
|
function vaultCoordinatorJson(body, status = 200) {
|
|
790
961
|
return new Response(JSON.stringify(body), {
|
|
791
962
|
status,
|
|
@@ -984,8 +1155,33 @@ function createVaultCoordinator(options) {
|
|
|
984
1155
|
}
|
|
985
1156
|
return outcome;
|
|
986
1157
|
};
|
|
1158
|
+
const applyWithBoxGeneration = async () => {
|
|
1159
|
+
let movesGeneration = false;
|
|
1160
|
+
let outcome;
|
|
1161
|
+
let failure;
|
|
1162
|
+
try {
|
|
1163
|
+
movesGeneration = !isSyncedBoxWriter(await readVaultBoxState(), body.writer);
|
|
1164
|
+
if (movesGeneration) await updateVaultBoxState((state) => withMutationStarted(state, body.operationId, Date.now()));
|
|
1165
|
+
outcome = await apply();
|
|
1166
|
+
} catch (error) {
|
|
1167
|
+
failure = error;
|
|
1168
|
+
}
|
|
1169
|
+
if (movesGeneration) {
|
|
1170
|
+
try {
|
|
1171
|
+
await updateVaultBoxState(withMutationFinished);
|
|
1172
|
+
} catch (error) {
|
|
1173
|
+
console.error("[vault] box generation did not move after a mutation", {
|
|
1174
|
+
workspaceId: body.workspaceId,
|
|
1175
|
+
operationId: body.operationId,
|
|
1176
|
+
error: error instanceof Error ? error.message : String(error)
|
|
1177
|
+
});
|
|
1178
|
+
}
|
|
1179
|
+
}
|
|
1180
|
+
if (failure === void 0 && outcome) return outcome;
|
|
1181
|
+
throw failure ?? new Error("Vault mutation produced no result");
|
|
1182
|
+
};
|
|
987
1183
|
try {
|
|
988
|
-
const outcome = await exclusive(() => options.aroundMutation ? options.aroundMutation(body,
|
|
1184
|
+
const outcome = await exclusive(() => options.aroundMutation ? options.aroundMutation(body, applyWithBoxGeneration) : applyWithBoxGeneration());
|
|
989
1185
|
return vaultCoordinatorJson(outcome, outcome.status === "conflict" ? 409 : 200);
|
|
990
1186
|
} catch (error) {
|
|
991
1187
|
if (error instanceof VaultReadConvergenceError) return vaultConvergenceResponse(error);
|
|
@@ -993,6 +1189,149 @@ function createVaultCoordinator(options) {
|
|
|
993
1189
|
return vaultCoordinatorJson({ error: error instanceof Error ? error.message : "Vault mutation failed" }, 500);
|
|
994
1190
|
}
|
|
995
1191
|
}
|
|
1192
|
+
async function readVaultSandboxBaseline(sandboxId) {
|
|
1193
|
+
const stored = await storage.get(vaultBaselineKey(sandboxId));
|
|
1194
|
+
if (!stored) return null;
|
|
1195
|
+
if (!isChunkedVaultBaselineHead(stored)) return stored;
|
|
1196
|
+
const chunks = [];
|
|
1197
|
+
for (let index = 0; index < stored.chunks; index++) {
|
|
1198
|
+
const chunk = await storage.get(vaultBaselineChunkKey(sandboxId, index));
|
|
1199
|
+
if (typeof chunk !== "string") throw new Error(`Vault baseline for sandbox ${sandboxId} is missing chunk ${index} of ${stored.chunks}`);
|
|
1200
|
+
chunks.push(chunk);
|
|
1201
|
+
}
|
|
1202
|
+
return JSON.parse(chunks.join(""));
|
|
1203
|
+
}
|
|
1204
|
+
async function writeVaultSandboxBaseline(baseline) {
|
|
1205
|
+
const key = vaultBaselineKey(baseline.sandboxId);
|
|
1206
|
+
const previous = await storage.get(key);
|
|
1207
|
+
const previousChunks = isChunkedVaultBaselineHead(previous) ? previous.chunks : 0;
|
|
1208
|
+
const serialized = JSON.stringify(baseline);
|
|
1209
|
+
let chunks = 0;
|
|
1210
|
+
if (serialized.length <= VAULT_BASELINE_CHUNK_CHARS) {
|
|
1211
|
+
await storage.put(key, baseline);
|
|
1212
|
+
} else {
|
|
1213
|
+
chunks = Math.ceil(serialized.length / VAULT_BASELINE_CHUNK_CHARS);
|
|
1214
|
+
for (let index = 0; index < chunks; index++) {
|
|
1215
|
+
const start = index * VAULT_BASELINE_CHUNK_CHARS;
|
|
1216
|
+
await storage.put(vaultBaselineChunkKey(baseline.sandboxId, index), serialized.slice(start, start + VAULT_BASELINE_CHUNK_CHARS));
|
|
1217
|
+
}
|
|
1218
|
+
const head = { schema: VAULT_BASELINE_CHUNKED_SCHEMA, chunks, updatedAt: baseline.updatedAt };
|
|
1219
|
+
await storage.put(key, head);
|
|
1220
|
+
}
|
|
1221
|
+
for (let index = chunks; index < previousChunks; index++) {
|
|
1222
|
+
await storage.delete(vaultBaselineChunkKey(baseline.sandboxId, index));
|
|
1223
|
+
}
|
|
1224
|
+
}
|
|
1225
|
+
async function handleVaultBaselineGet(request) {
|
|
1226
|
+
const body = await request.json().catch(() => null);
|
|
1227
|
+
const workspaceId = typeof body?.workspaceId === "string" ? body.workspaceId : "";
|
|
1228
|
+
const sandboxId = typeof body?.sandboxId === "string" ? body.sandboxId : "";
|
|
1229
|
+
if (!workspaceId || !sandboxId) return vaultCoordinatorJson({ error: "Missing workspaceId or sandboxId" }, 400);
|
|
1230
|
+
try {
|
|
1231
|
+
validateWorkspaceId(workspaceId);
|
|
1232
|
+
return vaultCoordinatorJson({ baseline: await readVaultSandboxBaseline(sandboxId) });
|
|
1233
|
+
} catch (error) {
|
|
1234
|
+
if (error instanceof VaultPathError) return vaultPathErrorResponse(error);
|
|
1235
|
+
throw error;
|
|
1236
|
+
}
|
|
1237
|
+
}
|
|
1238
|
+
async function handleVaultBaselineSet(request) {
|
|
1239
|
+
const body = await request.json().catch(() => null);
|
|
1240
|
+
const validSchema = body?.schema === "vault-sandbox-baseline-v2" || body?.schema === "vault-sandbox-baseline-v3" && Boolean(body.filesystemIncarnationId);
|
|
1241
|
+
if (!body || !validSchema || !body.sandboxId || !body.workspaceId) {
|
|
1242
|
+
return vaultCoordinatorJson({ error: "Invalid Vault sandbox baseline" }, 400);
|
|
1243
|
+
}
|
|
1244
|
+
try {
|
|
1245
|
+
validateWorkspaceId(body.workspaceId);
|
|
1246
|
+
if (body.expectedVaultGeneration !== void 0) {
|
|
1247
|
+
const state = await readVaultBoxState();
|
|
1248
|
+
if (state.inFlight || state.generation !== body.expectedVaultGeneration) return vaultCoordinatorJson({ saved: false });
|
|
1249
|
+
}
|
|
1250
|
+
const { expectedVaultGeneration: _expected, ...baseline } = body;
|
|
1251
|
+
await writeVaultSandboxBaseline(baseline);
|
|
1252
|
+
return vaultCoordinatorJson({ saved: true });
|
|
1253
|
+
} catch (error) {
|
|
1254
|
+
if (error instanceof VaultPathError) return vaultPathErrorResponse(error);
|
|
1255
|
+
throw error;
|
|
1256
|
+
}
|
|
1257
|
+
}
|
|
1258
|
+
async function readVaultBoxState() {
|
|
1259
|
+
return parseVaultBoxState(await storage.get(VAULT_BOX_STATE_KEY));
|
|
1260
|
+
}
|
|
1261
|
+
async function updateVaultBoxState(change) {
|
|
1262
|
+
const next = change(await readVaultBoxState());
|
|
1263
|
+
await storage.put(VAULT_BOX_STATE_KEY, next);
|
|
1264
|
+
return next;
|
|
1265
|
+
}
|
|
1266
|
+
async function handleVaultBoxStateGet() {
|
|
1267
|
+
let state = await readVaultBoxState();
|
|
1268
|
+
const healed = healStaleMutation(state, Date.now());
|
|
1269
|
+
if (healed) {
|
|
1270
|
+
await storage.put(VAULT_BOX_STATE_KEY, healed);
|
|
1271
|
+
state = healed;
|
|
1272
|
+
}
|
|
1273
|
+
return vaultCoordinatorJson({ state: viewOfVaultBoxState(state) });
|
|
1274
|
+
}
|
|
1275
|
+
async function handleVaultBoxStateSync(request) {
|
|
1276
|
+
const body = await request.json().catch(() => null);
|
|
1277
|
+
const text = (value) => typeof value === "string" && value ? value : null;
|
|
1278
|
+
const sandboxId = text(body?.sandboxId);
|
|
1279
|
+
const filesystemIncarnationId = text(body?.filesystemIncarnationId);
|
|
1280
|
+
const contract = text(body?.contract);
|
|
1281
|
+
const generation = body?.generation;
|
|
1282
|
+
const skipped = parseVaultBoxSyncSkips(body?.skipped ?? []);
|
|
1283
|
+
if (!sandboxId || !filesystemIncarnationId || !contract || typeof generation !== "number" || !Number.isSafeInteger(generation) || !skipped) {
|
|
1284
|
+
return vaultCoordinatorJson({ error: "Invalid Vault box sync" }, 400);
|
|
1285
|
+
}
|
|
1286
|
+
await updateVaultBoxState((state) => withSync(state, {
|
|
1287
|
+
sandboxId,
|
|
1288
|
+
filesystemIncarnationId,
|
|
1289
|
+
contract,
|
|
1290
|
+
generation,
|
|
1291
|
+
skipped,
|
|
1292
|
+
syncedAt: (/* @__PURE__ */ new Date()).toISOString()
|
|
1293
|
+
}));
|
|
1294
|
+
return vaultCoordinatorJson({ saved: true });
|
|
1295
|
+
}
|
|
1296
|
+
async function handleVaultBoxStateInvalidate() {
|
|
1297
|
+
await updateVaultBoxState(withSyncInvalidated);
|
|
1298
|
+
return vaultCoordinatorJson({ invalidated: true });
|
|
1299
|
+
}
|
|
1300
|
+
async function handleVaultBoxStateBootstrap(request) {
|
|
1301
|
+
const body = await request.json().catch(() => null);
|
|
1302
|
+
const text = (value) => typeof value === "string" && value ? value : null;
|
|
1303
|
+
const sandboxId = text(body?.sandboxId);
|
|
1304
|
+
const filesystemIncarnationId = text(body?.filesystemIncarnationId);
|
|
1305
|
+
const marker = text(body?.marker);
|
|
1306
|
+
if (!sandboxId || !filesystemIncarnationId || !marker) return vaultCoordinatorJson({ error: "Invalid sandbox bootstrap record" }, 400);
|
|
1307
|
+
await updateVaultBoxState((state) => withBootstrap(state, { sandboxId, filesystemIncarnationId, marker, at: (/* @__PURE__ */ new Date()).toISOString() }));
|
|
1308
|
+
return vaultCoordinatorJson({ saved: true });
|
|
1309
|
+
}
|
|
1310
|
+
async function handle(request) {
|
|
1311
|
+
if (request.method !== "POST") return null;
|
|
1312
|
+
switch (new URL(request.url).pathname) {
|
|
1313
|
+
case "/vault/mutate":
|
|
1314
|
+
return handleVaultMutation(request);
|
|
1315
|
+
case "/vault/head/get":
|
|
1316
|
+
return handleVaultHeadGet(request);
|
|
1317
|
+
case "/vault/head/list":
|
|
1318
|
+
return handleVaultHeadList(request);
|
|
1319
|
+
case "/vault/baseline/get":
|
|
1320
|
+
return handleVaultBaselineGet(request);
|
|
1321
|
+
case "/vault/baseline/set":
|
|
1322
|
+
return handleVaultBaselineSet(request);
|
|
1323
|
+
case "/vault/box-state/get":
|
|
1324
|
+
return handleVaultBoxStateGet();
|
|
1325
|
+
case "/vault/box-state/sync":
|
|
1326
|
+
return handleVaultBoxStateSync(request);
|
|
1327
|
+
case "/vault/box-state/invalidate":
|
|
1328
|
+
return handleVaultBoxStateInvalidate();
|
|
1329
|
+
case "/vault/box-state/bootstrap":
|
|
1330
|
+
return handleVaultBoxStateBootstrap(request);
|
|
1331
|
+
default:
|
|
1332
|
+
return null;
|
|
1333
|
+
}
|
|
1334
|
+
}
|
|
996
1335
|
return {
|
|
997
1336
|
exclusive,
|
|
998
1337
|
settled,
|
|
@@ -1001,11 +1340,30 @@ function createVaultCoordinator(options) {
|
|
|
1001
1340
|
readVaultHeadFenced,
|
|
1002
1341
|
handleVaultHeadGet,
|
|
1003
1342
|
handleVaultHeadList,
|
|
1004
|
-
handleVaultMutation
|
|
1343
|
+
handleVaultMutation,
|
|
1344
|
+
readVaultSandboxBaseline,
|
|
1345
|
+
writeVaultSandboxBaseline,
|
|
1346
|
+
handleVaultBaselineGet,
|
|
1347
|
+
handleVaultBaselineSet,
|
|
1348
|
+
readVaultBoxState,
|
|
1349
|
+
updateVaultBoxState,
|
|
1350
|
+
handle
|
|
1005
1351
|
};
|
|
1006
1352
|
}
|
|
1007
1353
|
|
|
1008
1354
|
// src/vault/store/sandbox-sync.ts
|
|
1355
|
+
var SANDBOX_VAULT_SYNC_CONTRACT = "agent-app-sandbox-vault-sync-v1";
|
|
1356
|
+
var VAULT_BASE64_BODY_MARKER = "[base64]\n";
|
|
1357
|
+
function vaultBinaryPayload(codec, raw) {
|
|
1358
|
+
const parsed = codec.parse(raw);
|
|
1359
|
+
if (!parsed.succeeded || parsed.frontmatter.encoding !== "base64" || !parsed.body.startsWith(VAULT_BASE64_BODY_MARKER)) {
|
|
1360
|
+
return null;
|
|
1361
|
+
}
|
|
1362
|
+
return parsed.body.slice(VAULT_BASE64_BODY_MARKER.length).trim();
|
|
1363
|
+
}
|
|
1364
|
+
function vaultBinaryDocument(codec, fields, base64) {
|
|
1365
|
+
return codec.serialize({ ...fields, encoding: "base64" }, `${VAULT_BASE64_BODY_MARKER}${base64}`);
|
|
1366
|
+
}
|
|
1009
1367
|
var VAULT_SANDBOX_BASELINE_SCHEMA = "vault-sandbox-baseline-v3";
|
|
1010
1368
|
var VAULT_SANDBOX_BASELINE_V2_SCHEMA = "vault-sandbox-baseline-v2";
|
|
1011
1369
|
var VAULT_SANDBOX_BASELINE_V1_SCHEMA = "vault-sandbox-baseline-v1";
|
|
@@ -1138,7 +1496,851 @@ async function mapWithConcurrency(values, concurrency, mapper) {
|
|
|
1138
1496
|
await Promise.all(workers);
|
|
1139
1497
|
return results;
|
|
1140
1498
|
}
|
|
1499
|
+
|
|
1500
|
+
// src/vault/store/sandbox-hydrate.ts
|
|
1501
|
+
var VAULT_IO_CONCURRENCY = 8;
|
|
1502
|
+
var BINARY_HYDRATION_CONCURRENCY = 2;
|
|
1503
|
+
var VAULT_HYDRATION_HEAD_PAGE_SIZE = 100;
|
|
1504
|
+
var VAULT_HYDRATION_MAX_PAGES = 200;
|
|
1505
|
+
var VAULT_HYDRATION_BATCH_CHARS = 2e6;
|
|
1506
|
+
var VAULT_TEXT_WRITE_MAX_JSON_BYTES = 1024 * 1024 - 16 * 1024;
|
|
1507
|
+
var encoder = new TextEncoder();
|
|
1508
|
+
async function hydrateSandboxVault(config, box, workspaceId, options = {}) {
|
|
1509
|
+
try {
|
|
1510
|
+
const contract = config.contract ?? SANDBOX_VAULT_SYNC_CONTRACT;
|
|
1511
|
+
const incarnation = readReadySandboxFilesystemIncarnation(box);
|
|
1512
|
+
const identity = box.id && incarnation.succeeded ? { sandboxId: box.id, filesystemIncarnationId: incarnation.value.id } : null;
|
|
1513
|
+
const protectedPaths = Boolean(options.conflictPaths?.size || options.appliedPaths?.size) || options.pathsMissingFromBox !== void 0;
|
|
1514
|
+
const view = identity ? await config.vault.readVaultBoxState(workspaceId) : null;
|
|
1515
|
+
if (identity && !protectedPaths && vaultBoxInSync(view, identity, contract)) {
|
|
1516
|
+
return { succeeded: true, value: { written: 0, deleted: 0, unchanged: 0, processedHeads: 0, inSync: true } };
|
|
1517
|
+
}
|
|
1518
|
+
const vaultDir = box.fs.mkdir(config.root, { recursive: true });
|
|
1519
|
+
vaultDir.catch(() => {
|
|
1520
|
+
});
|
|
1521
|
+
let written = 0;
|
|
1522
|
+
let deleted = 0;
|
|
1523
|
+
let unchanged = 0;
|
|
1524
|
+
let processedHeadCount = 0;
|
|
1525
|
+
for (let pageIndex = 0; pageIndex < VAULT_HYDRATION_MAX_PAGES; pageIndex += 1) {
|
|
1526
|
+
const page = await hydratePage(config, box, workspaceId, options, vaultDir);
|
|
1527
|
+
written += page.written;
|
|
1528
|
+
deleted += page.deleted;
|
|
1529
|
+
unchanged += page.unchanged;
|
|
1530
|
+
processedHeadCount = page.processedHeadCount;
|
|
1531
|
+
if (page.complete) {
|
|
1532
|
+
if (identity && view && !view.inFlight) {
|
|
1533
|
+
await config.vault.recordVaultBoxSync(workspaceId, { ...identity, contract, generation: view.generation, skipped: [] }).catch((error) => console.warn("[vault] box sync record failed", {
|
|
1534
|
+
workspaceId,
|
|
1535
|
+
error: error instanceof Error ? error.message : String(error)
|
|
1536
|
+
}));
|
|
1537
|
+
}
|
|
1538
|
+
return { succeeded: true, value: { written, deleted, unchanged, processedHeads: processedHeadCount } };
|
|
1539
|
+
}
|
|
1540
|
+
if (page.cursor === null) {
|
|
1541
|
+
throw new VaultHydrationIncompleteError({
|
|
1542
|
+
phase: "hydrate",
|
|
1543
|
+
processedPaths: processedHeadCount,
|
|
1544
|
+
totalPaths: null,
|
|
1545
|
+
remainingPaths: null,
|
|
1546
|
+
committedGroups: 0
|
|
1547
|
+
}, "the Vault head listing reported more pages but returned no resume cursor");
|
|
1548
|
+
}
|
|
1549
|
+
}
|
|
1550
|
+
throw new VaultHydrationIncompleteError({
|
|
1551
|
+
phase: "hydrate",
|
|
1552
|
+
processedPaths: processedHeadCount,
|
|
1553
|
+
totalPaths: null,
|
|
1554
|
+
remainingPaths: null,
|
|
1555
|
+
committedGroups: 0
|
|
1556
|
+
}, `the Vault head listing did not finish within ${VAULT_HYDRATION_MAX_PAGES} pages`);
|
|
1557
|
+
} catch (error) {
|
|
1558
|
+
const typed = error instanceof VaultHydrationError || error instanceof VaultHydrationIncompleteError || error instanceof VaultFilesystemIncarnationError ? error : new VaultHydrationError(`Failed to hydrate workspace Vault for ${workspaceId}`, error);
|
|
1559
|
+
return { succeeded: false, error: typed };
|
|
1560
|
+
}
|
|
1561
|
+
}
|
|
1562
|
+
async function hydratePage(config, box, workspaceId, options, vaultDir) {
|
|
1563
|
+
const { vault } = config;
|
|
1564
|
+
if (!box.id) throw new Error(`Cannot hydrate Vault for ${workspaceId}: sandbox id is missing`);
|
|
1565
|
+
const incarnation = readReadySandboxFilesystemIncarnation(box);
|
|
1566
|
+
if (!incarnation.succeeded) throw incarnation.error;
|
|
1567
|
+
const normalized = normalizeVaultSandboxBaseline(await vault.readVaultSandboxBaseline(workspaceId, box.id));
|
|
1568
|
+
const baselineMatchesIncarnation = normalized.baseline?.schema === VAULT_SANDBOX_BASELINE_SCHEMA && normalized.baseline.filesystemIncarnationId === incarnation.value.id;
|
|
1569
|
+
const previousBaseline = baselineMatchesIncarnation ? normalized.baseline : null;
|
|
1570
|
+
const legacyBaseline = baselineMatchesIncarnation ? normalized.legacy : false;
|
|
1571
|
+
const cursor = previousBaseline?.progress?.operation === "hydrate" ? previousBaseline.progress.cursor : void 0;
|
|
1572
|
+
const page = await vault.listVaultHeadPage(workspaceId, { cursor, limit: VAULT_HYDRATION_HEAD_PAGE_SIZE });
|
|
1573
|
+
const baselineFiles = { ...previousBaseline?.files ?? {} };
|
|
1574
|
+
const conflictPaths = options.conflictPaths ?? /* @__PURE__ */ new Set();
|
|
1575
|
+
const appliedPaths = options.appliedPaths ?? /* @__PURE__ */ new Set();
|
|
1576
|
+
const pathsMissingFromBox = await options.pathsMissingFromBox ?? /* @__PURE__ */ new Set();
|
|
1577
|
+
const skippedPaths = /* @__PURE__ */ new Set();
|
|
1578
|
+
const changedHeads = page.entries.filter((entry) => {
|
|
1579
|
+
if (conflictPaths.has(entry.path) || appliedPaths.has(entry.path)) return false;
|
|
1580
|
+
if (legacyBaseline) return true;
|
|
1581
|
+
const previous = pathsMissingFromBox.has(entry.path) ? void 0 : baselineFiles[entry.path];
|
|
1582
|
+
return !previous || previous.revision !== entry.revision || previous.contentHash !== entry.contentHash || previous.deleted !== entry.deleted;
|
|
1583
|
+
});
|
|
1584
|
+
const unseenBaselinePaths = new Set(
|
|
1585
|
+
previousBaseline?.progress?.operation === "hydrate" ? previousBaseline.progress.unseenBaselinePaths : Object.keys(previousBaseline?.files ?? {})
|
|
1586
|
+
);
|
|
1587
|
+
for (const entry of page.entries) unseenBaselinePaths.delete(entry.path);
|
|
1588
|
+
const missingBaselinePaths = page.complete ? [...unseenBaselinePaths].filter((path) => !conflictPaths.has(path) && !appliedPaths.has(path) && baselineFiles[path] !== void 0 && !baselineFiles[path]?.deleted) : [];
|
|
1589
|
+
const pathsToDelete = [
|
|
1590
|
+
...changedHeads.filter((entry) => entry.deleted && baselineFiles[entry.path] !== void 0 && !baselineFiles[entry.path]?.deleted).map((entry) => entry.path),
|
|
1591
|
+
...missingBaselinePaths
|
|
1592
|
+
];
|
|
1593
|
+
await vaultDir;
|
|
1594
|
+
let deleted = 0;
|
|
1595
|
+
for (const path of pathsToDelete) {
|
|
1596
|
+
const target = boxPath(config, path);
|
|
1597
|
+
if (!await box.fs.exists(target)) continue;
|
|
1598
|
+
await box.fs.delete(target);
|
|
1599
|
+
deleted += 1;
|
|
1600
|
+
}
|
|
1601
|
+
const binaryHashes = /* @__PURE__ */ new Map();
|
|
1602
|
+
let written = 0;
|
|
1603
|
+
const liveHeads = changedHeads.filter((entry) => !entry.deleted);
|
|
1604
|
+
for (let next = 0; next < liveHeads.length; ) {
|
|
1605
|
+
const batch = [];
|
|
1606
|
+
const snapshots = [];
|
|
1607
|
+
let batchChars = 0;
|
|
1608
|
+
while (next < liveHeads.length && batchChars < VAULT_HYDRATION_BATCH_CHARS) {
|
|
1609
|
+
const slice = liveHeads.slice(next, next + VAULT_IO_CONCURRENCY);
|
|
1610
|
+
next += slice.length;
|
|
1611
|
+
batch.push(...slice);
|
|
1612
|
+
const read = await mapWithConcurrency(slice, VAULT_IO_CONCURRENCY, async (entry) => readSnapshot(config, workspaceId, entry, skippedPaths));
|
|
1613
|
+
for (const snapshot of read) {
|
|
1614
|
+
if (!snapshot) continue;
|
|
1615
|
+
snapshots.push(snapshot);
|
|
1616
|
+
batchChars += snapshot.kind === "text" ? snapshot.content.length : snapshot.payload.length;
|
|
1617
|
+
}
|
|
1618
|
+
}
|
|
1619
|
+
await writeTextSnapshots(config, box, snapshots.filter((snapshot) => snapshot.kind === "text"));
|
|
1620
|
+
const binaries = snapshots.filter((snapshot) => snapshot.kind === "binary");
|
|
1621
|
+
await writeBinaries(config, box, workspaceId, binaries);
|
|
1622
|
+
for (const binary of binaries) binaryHashes.set(binary.path, binary.onDiskHash);
|
|
1623
|
+
written += snapshots.length;
|
|
1624
|
+
}
|
|
1625
|
+
const appliedBinaryHashes = await mapWithConcurrency(
|
|
1626
|
+
page.entries.filter((entry) => appliedPaths.has(entry.path) && !entry.deleted),
|
|
1627
|
+
VAULT_IO_CONCURRENCY,
|
|
1628
|
+
async (entry) => {
|
|
1629
|
+
const state = await vault.readVaultFileState(workspaceId, entry.path);
|
|
1630
|
+
if (state.deleted || state.content === null) return null;
|
|
1631
|
+
const payload = vaultBinaryPayload(vault.codec, state.content);
|
|
1632
|
+
return payload === null ? null : [entry.path, await sha256Hex(base64ToBytes(payload))];
|
|
1633
|
+
}
|
|
1634
|
+
);
|
|
1635
|
+
for (const entry of appliedBinaryHashes) if (entry) binaryHashes.set(entry[0], entry[1]);
|
|
1636
|
+
for (const entry of page.entries) {
|
|
1637
|
+
if (conflictPaths.has(entry.path) || skippedPaths.has(entry.path)) continue;
|
|
1638
|
+
const previous = baselineFiles[entry.path];
|
|
1639
|
+
const onDiskHash = binaryHashes.get(entry.path);
|
|
1640
|
+
if (onDiskHash !== void 0) {
|
|
1641
|
+
baselineFiles[entry.path] = { revision: entry.revision, contentHash: entry.contentHash, deleted: entry.deleted, binary: true, onDiskHash };
|
|
1642
|
+
} else if (appliedPaths.has(entry.path) && previous?.binary && previous.revision === entry.revision && previous.contentHash === entry.contentHash && previous.deleted === entry.deleted) {
|
|
1643
|
+
baselineFiles[entry.path] = previous;
|
|
1644
|
+
} else {
|
|
1645
|
+
baselineFiles[entry.path] = { revision: entry.revision, contentHash: entry.contentHash, deleted: entry.deleted };
|
|
1646
|
+
}
|
|
1647
|
+
}
|
|
1648
|
+
for (const path of missingBaselinePaths) delete baselineFiles[path];
|
|
1649
|
+
const processedHeadCount = (previousBaseline?.progress?.operation === "hydrate" ? previousBaseline.progress.processedHeadCount : 0) + page.entries.length;
|
|
1650
|
+
const baselineUnchanged = page.complete && previousBaseline !== null && !legacyBaseline && !previousBaseline.progress && sameBaselineFiles(previousBaseline.files, baselineFiles);
|
|
1651
|
+
if (!baselineUnchanged) {
|
|
1652
|
+
await vault.writeVaultSandboxBaseline({
|
|
1653
|
+
schema: VAULT_SANDBOX_BASELINE_SCHEMA,
|
|
1654
|
+
workspaceId,
|
|
1655
|
+
sandboxId: box.id,
|
|
1656
|
+
filesystemIncarnationId: incarnation.value.id,
|
|
1657
|
+
files: baselineFiles,
|
|
1658
|
+
...page.complete || page.cursor === null ? {} : {
|
|
1659
|
+
progress: {
|
|
1660
|
+
operation: "hydrate",
|
|
1661
|
+
cursor: page.cursor,
|
|
1662
|
+
processedHeadCount,
|
|
1663
|
+
unseenBaselinePaths: [...unseenBaselinePaths].sort()
|
|
1664
|
+
}
|
|
1665
|
+
},
|
|
1666
|
+
updatedAt: (/* @__PURE__ */ new Date()).toISOString()
|
|
1667
|
+
});
|
|
1668
|
+
}
|
|
1669
|
+
const protectedInPage = page.entries.filter((entry) => conflictPaths.has(entry.path) || appliedPaths.has(entry.path)).length;
|
|
1670
|
+
return {
|
|
1671
|
+
complete: page.complete,
|
|
1672
|
+
cursor: page.cursor,
|
|
1673
|
+
processedHeadCount,
|
|
1674
|
+
written,
|
|
1675
|
+
deleted,
|
|
1676
|
+
unchanged: Math.max(0, page.entries.length - changedHeads.length - protectedInPage)
|
|
1677
|
+
};
|
|
1678
|
+
}
|
|
1679
|
+
async function readSnapshot(config, workspaceId, entry, skippedPaths) {
|
|
1680
|
+
try {
|
|
1681
|
+
const state = await config.vault.readVaultFileState(workspaceId, entry.path);
|
|
1682
|
+
if (state.deleted || state.content === null || !state.revision || !state.contentHash) {
|
|
1683
|
+
throw new Error(`Vault head is incomplete for ${entry.path}`);
|
|
1684
|
+
}
|
|
1685
|
+
const payload = vaultBinaryPayload(config.vault.codec, state.content);
|
|
1686
|
+
if (payload !== null) {
|
|
1687
|
+
return { kind: "binary", path: entry.path, payload, onDiskHash: await sha256Hex(base64ToBytes(payload)) };
|
|
1688
|
+
}
|
|
1689
|
+
return { kind: "text", path: entry.path, content: state.content };
|
|
1690
|
+
} catch (error) {
|
|
1691
|
+
if (error instanceof Error && error.code === "vault.file_not_found") {
|
|
1692
|
+
skippedPaths.add(entry.path);
|
|
1693
|
+
return null;
|
|
1694
|
+
}
|
|
1695
|
+
throw new VaultHydrationError(`Failed to read Vault file during sandbox hydration: ${entry.path}`, error);
|
|
1696
|
+
}
|
|
1697
|
+
}
|
|
1698
|
+
async function writeTextSnapshots(config, box, texts) {
|
|
1699
|
+
let request = [];
|
|
1700
|
+
let requestBytes = 0;
|
|
1701
|
+
const flush = async () => {
|
|
1702
|
+
if (request.length === 0) return;
|
|
1703
|
+
await box.fs.writeMany(request);
|
|
1704
|
+
request = [];
|
|
1705
|
+
requestBytes = 0;
|
|
1706
|
+
};
|
|
1707
|
+
for (const text of texts) {
|
|
1708
|
+
const path = boxPath(config, text.path);
|
|
1709
|
+
const bytes = encoder.encode(JSON.stringify({ path, content: text.content })).byteLength;
|
|
1710
|
+
if (bytes > VAULT_TEXT_WRITE_MAX_JSON_BYTES) {
|
|
1711
|
+
const data = encoder.encode(text.content);
|
|
1712
|
+
const result = await box.fs.uploadData(path, data);
|
|
1713
|
+
const size = result?.size;
|
|
1714
|
+
if (typeof size === "number" && size !== data.byteLength) {
|
|
1715
|
+
throw new VaultHydrationError(`Failed to upload Vault file ${text.path}`, new Error(`expected ${data.byteLength} uploaded bytes, received ${size}`));
|
|
1716
|
+
}
|
|
1717
|
+
continue;
|
|
1718
|
+
}
|
|
1719
|
+
if (requestBytes + bytes > VAULT_TEXT_WRITE_MAX_JSON_BYTES) await flush();
|
|
1720
|
+
request.push({ path, content: text.content });
|
|
1721
|
+
requestBytes += bytes;
|
|
1722
|
+
}
|
|
1723
|
+
await flush();
|
|
1724
|
+
}
|
|
1725
|
+
async function writeBinaries(config, box, workspaceId, binaries) {
|
|
1726
|
+
await mapWithConcurrency(binaries, BINARY_HYDRATION_CONCURRENCY, async (binary) => {
|
|
1727
|
+
try {
|
|
1728
|
+
const bytes = base64ToBytes(binary.payload);
|
|
1729
|
+
const result = await box.fs.uploadData(boxPath(config, binary.path), bytes);
|
|
1730
|
+
const size = result?.size;
|
|
1731
|
+
if (typeof size === "number" && size !== bytes.byteLength) {
|
|
1732
|
+
throw new Error(`expected ${bytes.byteLength} uploaded bytes, received ${size}`);
|
|
1733
|
+
}
|
|
1734
|
+
} catch (error) {
|
|
1735
|
+
throw new VaultHydrationError(`Failed to upload binary Vault file ${binary.path} for ${workspaceId}`, error);
|
|
1736
|
+
}
|
|
1737
|
+
});
|
|
1738
|
+
if (binaries.length === 0) return;
|
|
1739
|
+
const verification = await box.exec(`sha256sum -z -- ${binaries.map((binary) => shellQuote(boxPath(config, binary.path))).join(" ")}`);
|
|
1740
|
+
if (verification.exitCode !== 0) {
|
|
1741
|
+
throw new VaultHydrationError(
|
|
1742
|
+
`Failed to verify hydrated binary Vault files for ${workspaceId}`,
|
|
1743
|
+
new Error(verification.stderr || `sha256sum exited ${verification.exitCode}`)
|
|
1744
|
+
);
|
|
1745
|
+
}
|
|
1746
|
+
const observed = /* @__PURE__ */ new Map();
|
|
1747
|
+
const rootPrefix = `${config.root}/`;
|
|
1748
|
+
for (const record of verification.stdout.split("\0").filter(Boolean)) {
|
|
1749
|
+
const parsed = /^([a-f0-9]{64}) [ *]([\s\S]+)$/.exec(record);
|
|
1750
|
+
if (!parsed) {
|
|
1751
|
+
throw new VaultHydrationError(`Malformed sha256sum record while verifying hydrated binaries for ${workspaceId}`, new Error(record));
|
|
1752
|
+
}
|
|
1753
|
+
const observedPath = parsed[2];
|
|
1754
|
+
observed.set(observedPath.startsWith(rootPrefix) ? observedPath.slice(rootPrefix.length) : observedPath, parsed[1]);
|
|
1755
|
+
}
|
|
1756
|
+
for (const binary of binaries) {
|
|
1757
|
+
if (observed.get(binary.path) !== binary.onDiskHash) {
|
|
1758
|
+
throw new VaultHydrationError(
|
|
1759
|
+
`Hydrated binary Vault file ${binary.path} does not match its expected bytes`,
|
|
1760
|
+
new Error(`expected ${binary.onDiskHash}, box reported ${observed.get(binary.path) ?? "nothing"}`)
|
|
1761
|
+
);
|
|
1762
|
+
}
|
|
1763
|
+
}
|
|
1764
|
+
}
|
|
1765
|
+
function sameBaselineFiles(previous, next) {
|
|
1766
|
+
const paths = Object.keys(next);
|
|
1767
|
+
if (paths.length !== Object.keys(previous).length) return false;
|
|
1768
|
+
return paths.every((path) => {
|
|
1769
|
+
const a = previous[path];
|
|
1770
|
+
const b = next[path];
|
|
1771
|
+
return a !== void 0 && a.revision === b.revision && a.contentHash === b.contentHash && a.deleted === b.deleted && Boolean(a.binary) === Boolean(b.binary) && (a.onDiskHash ?? null) === (b.onDiskHash ?? null);
|
|
1772
|
+
});
|
|
1773
|
+
}
|
|
1774
|
+
function boxPath(config, vaultPath) {
|
|
1775
|
+
return `${config.root}/${vaultPath}`;
|
|
1776
|
+
}
|
|
1777
|
+
function base64ToBytes(base64) {
|
|
1778
|
+
const binary = atob(base64.replace(/\s+/g, ""));
|
|
1779
|
+
const bytes = new Uint8Array(binary.length);
|
|
1780
|
+
for (let index = 0; index < binary.length; index += 1) bytes[index] = binary.charCodeAt(index);
|
|
1781
|
+
return bytes;
|
|
1782
|
+
}
|
|
1783
|
+
|
|
1784
|
+
// src/vault/store/sandbox-reconcile.ts
|
|
1785
|
+
var VAULT_IO_CONCURRENCY2 = 8;
|
|
1786
|
+
var VAULT_SYNC_GROUP_SIZE = 32;
|
|
1787
|
+
var VAULT_SYNC_MAX_GROUPS = 4;
|
|
1788
|
+
var INVALID_UTF8_SIDECAR_MESSAGE = "File is not valid UTF-8; request base64 instead";
|
|
1789
|
+
var vaultReconciliationTasks = /* @__PURE__ */ new Map();
|
|
1790
|
+
function assertSandboxVaultReconciliation(workspaceId, reconciliation) {
|
|
1791
|
+
if (!reconciliation.succeeded) {
|
|
1792
|
+
if (reconciliation.blockedReason === "filesystem_incarnation_missing") {
|
|
1793
|
+
throw new VaultFilesystemIncarnationError(
|
|
1794
|
+
"vault.filesystem_incarnation_missing",
|
|
1795
|
+
"Sandbox filesystem incarnation identity is unavailable"
|
|
1796
|
+
);
|
|
1797
|
+
}
|
|
1798
|
+
if (reconciliation.blockedReason === "filesystem_incarnation_not_ready") {
|
|
1799
|
+
throw new VaultFilesystemIncarnationError(
|
|
1800
|
+
"vault.filesystem_incarnation_not_ready",
|
|
1801
|
+
"Sandbox filesystem incarnation is not ready"
|
|
1802
|
+
);
|
|
1803
|
+
}
|
|
1804
|
+
const detail = reconciliation.rejectedPaths.map((rejection) => `${rejection.path}: ${rejection.message}`).join("; ");
|
|
1805
|
+
throw new VaultHydrationError(
|
|
1806
|
+
`Sandbox Vault reconciliation failed for ${workspaceId}`,
|
|
1807
|
+
new Error(detail || "reconciliation failed without a path diagnostic")
|
|
1808
|
+
);
|
|
1809
|
+
}
|
|
1810
|
+
if (reconciliation.refusedDeletionPaths.length > 0) {
|
|
1811
|
+
console.error("[sandbox] sandbox Vault deletion refused; use the authenticated Vault API", {
|
|
1812
|
+
workspaceId,
|
|
1813
|
+
paths: reconciliation.refusedDeletionPaths
|
|
1814
|
+
});
|
|
1815
|
+
}
|
|
1816
|
+
if (!reconciliation.complete) {
|
|
1817
|
+
throw new VaultHydrationIncompleteError(
|
|
1818
|
+
reconciliation.progress,
|
|
1819
|
+
"sandbox-to-KV reconciliation has more mutation groups to commit"
|
|
1820
|
+
);
|
|
1821
|
+
}
|
|
1822
|
+
if (reconciliation.conflicts.length > 0) {
|
|
1823
|
+
console.error("[sandbox] sandbox Vault edits diverged from their KV heads; both copies retained", {
|
|
1824
|
+
workspaceId,
|
|
1825
|
+
conflicts: reconciliation.conflicts.map((conflict) => ({
|
|
1826
|
+
path: conflict.path,
|
|
1827
|
+
expectedRevision: conflict.expectedRevision,
|
|
1828
|
+
currentRevision: conflict.currentRevision
|
|
1829
|
+
}))
|
|
1830
|
+
});
|
|
1831
|
+
}
|
|
1832
|
+
}
|
|
1833
|
+
function blockedSandboxVaultSync(root, error, reason, totalPaths = 0) {
|
|
1834
|
+
const rejection = error instanceof VaultFilesystemIncarnationError ? sandboxVaultRejection(root, "authority", error.code, error) : error;
|
|
1835
|
+
return {
|
|
1836
|
+
succeeded: false,
|
|
1837
|
+
complete: false,
|
|
1838
|
+
rejectedPaths: [rejection],
|
|
1839
|
+
binaryDriftPaths: [],
|
|
1840
|
+
refusedDeletionPaths: [],
|
|
1841
|
+
pathsMissingFromBox: [],
|
|
1842
|
+
operationId: null,
|
|
1843
|
+
changes: [],
|
|
1844
|
+
appliedPaths: [],
|
|
1845
|
+
conflicts: [],
|
|
1846
|
+
progress: {
|
|
1847
|
+
phase: "reconcile",
|
|
1848
|
+
processedPaths: 0,
|
|
1849
|
+
totalPaths,
|
|
1850
|
+
remainingPaths: totalPaths,
|
|
1851
|
+
committedGroups: 0
|
|
1852
|
+
},
|
|
1853
|
+
blockedReason: reason
|
|
1854
|
+
};
|
|
1855
|
+
}
|
|
1856
|
+
async function readSandboxVaultManifest(config, box, workspaceId, sessionId) {
|
|
1857
|
+
const command = `if [ ! -d ${shellQuote(config.root)} ]; then printf '%s' 'Sandbox Vault directory is missing' >&2; exit 42; fi; find ${shellQuote(config.root)} -name '.*' -prune -o -type f -exec sha256sum -z -- {} +`;
|
|
1858
|
+
let listing;
|
|
1859
|
+
try {
|
|
1860
|
+
listing = sessionId ? await box.exec(command, { sessionId }) : await box.exec(command);
|
|
1861
|
+
} catch (error) {
|
|
1862
|
+
throw new Error(`Failed to list sandbox Vault for ${workspaceId}`, { cause: error });
|
|
1863
|
+
}
|
|
1864
|
+
if (listing.exitCode === 42) {
|
|
1865
|
+
return {
|
|
1866
|
+
entries: /* @__PURE__ */ new Map(),
|
|
1867
|
+
rejectedPaths: [sandboxVaultRejection(
|
|
1868
|
+
config.root,
|
|
1869
|
+
"manifest",
|
|
1870
|
+
"sandbox.vault_directory_missing",
|
|
1871
|
+
new Error(`Sandbox Vault directory is missing for ${workspaceId}`)
|
|
1872
|
+
)],
|
|
1873
|
+
directoryPresent: false
|
|
1874
|
+
};
|
|
1875
|
+
}
|
|
1876
|
+
if (listing.exitCode !== 0) {
|
|
1877
|
+
throw new Error(`Failed to list sandbox Vault for ${workspaceId}: ${listing.stderr}`);
|
|
1878
|
+
}
|
|
1879
|
+
const rootPrefix = `${config.root}/`;
|
|
1880
|
+
const entries = /* @__PURE__ */ new Map();
|
|
1881
|
+
const rejectedPaths = [];
|
|
1882
|
+
for (const record of listing.stdout.split("\0").filter(Boolean)) {
|
|
1883
|
+
const parsed = /^([a-f0-9]{64}) [ *]([\s\S]+)$/.exec(record);
|
|
1884
|
+
const absolutePath = parsed?.[2] ?? record;
|
|
1885
|
+
const path = absolutePath.startsWith(rootPrefix) ? absolutePath.slice(rootPrefix.length) : absolutePath;
|
|
1886
|
+
try {
|
|
1887
|
+
if (!parsed) throw new Error("Sandbox Vault manifest contains a malformed sha256sum record");
|
|
1888
|
+
if (!absolutePath.startsWith(rootPrefix)) {
|
|
1889
|
+
throw new Error("Sandbox Vault manifest path is outside the Vault root");
|
|
1890
|
+
}
|
|
1891
|
+
if (!isWorkspaceFileExportable(path)) continue;
|
|
1892
|
+
validateVaultFilePath(path);
|
|
1893
|
+
vaultHeadKey(workspaceId, path);
|
|
1894
|
+
if (entries.has(path)) throw new Error("Sandbox Vault manifest contains a duplicate path");
|
|
1895
|
+
entries.set(path, { path, contentHash: parsed[1] });
|
|
1896
|
+
} catch (error) {
|
|
1897
|
+
rejectedPaths.push(sandboxVaultRejection(
|
|
1898
|
+
path,
|
|
1899
|
+
"manifest",
|
|
1900
|
+
error instanceof VaultStorageKeyError ? error.code : "sandbox.invalid_path",
|
|
1901
|
+
error
|
|
1902
|
+
));
|
|
1903
|
+
}
|
|
1904
|
+
}
|
|
1905
|
+
return { entries, rejectedPaths, directoryPresent: true };
|
|
1906
|
+
}
|
|
1907
|
+
function isInvalidUtf8Error(error) {
|
|
1908
|
+
if (!(error instanceof Error)) return false;
|
|
1909
|
+
if (error.code === "INVALID_UTF8") return true;
|
|
1910
|
+
return error.message.includes(INVALID_UTF8_SIDECAR_MESSAGE);
|
|
1911
|
+
}
|
|
1912
|
+
async function readChangedSandboxBinaryFile(config, box, entry, sessionId) {
|
|
1913
|
+
const absolutePath = `${config.root}/${entry.path}`;
|
|
1914
|
+
const stat = await statSandboxFileSize(box, absolutePath, { sessionId });
|
|
1915
|
+
if (!stat.succeeded) {
|
|
1916
|
+
throw new Error(`Failed to stat binary Vault file ${entry.path}: ${stat.error}`);
|
|
1917
|
+
}
|
|
1918
|
+
const read = await readSandboxBinaryBytes(box, absolutePath, stat.value, { sessionId });
|
|
1919
|
+
if (!read.succeeded) {
|
|
1920
|
+
throw new Error(`Failed to read binary Vault file ${entry.path}: ${read.error}`);
|
|
1921
|
+
}
|
|
1922
|
+
const { bytes, size } = read.value;
|
|
1923
|
+
const onDiskHash = await sha256Hex(bytes);
|
|
1924
|
+
if (entry.contentHash !== onDiskHash) {
|
|
1925
|
+
throw new Error("Sandbox Vault file changed after its manifest was captured");
|
|
1926
|
+
}
|
|
1927
|
+
const filename = entry.path.split("/").pop() || entry.path;
|
|
1928
|
+
const content = vaultBinaryDocument(config.vault.codec, {
|
|
1929
|
+
type: "upload",
|
|
1930
|
+
created: (/* @__PURE__ */ new Date()).toISOString().split("T")[0],
|
|
1931
|
+
originalName: filename,
|
|
1932
|
+
size,
|
|
1933
|
+
encoding: "base64",
|
|
1934
|
+
mime: sniffMime(filename, config.fileTypes)
|
|
1935
|
+
}, bytesToBase64(bytes));
|
|
1936
|
+
return {
|
|
1937
|
+
content,
|
|
1938
|
+
contentHash: await sha256Hex(content),
|
|
1939
|
+
binary: true,
|
|
1940
|
+
onDiskHash
|
|
1941
|
+
};
|
|
1942
|
+
}
|
|
1943
|
+
async function readChangedSandboxFile(config, box, entry, sessionId) {
|
|
1944
|
+
const absolutePath = `${config.root}/${entry.path}`;
|
|
1945
|
+
let content;
|
|
1946
|
+
try {
|
|
1947
|
+
content = sessionId ? await box.read(absolutePath, { sessionId }) : await box.read(absolutePath);
|
|
1948
|
+
} catch (error) {
|
|
1949
|
+
if (isInvalidUtf8Error(error)) {
|
|
1950
|
+
return readChangedSandboxBinaryFile(config, box, entry, sessionId);
|
|
1951
|
+
}
|
|
1952
|
+
throw error;
|
|
1953
|
+
}
|
|
1954
|
+
const contentHash = await sha256Hex(content);
|
|
1955
|
+
if (entry.contentHash !== contentHash) {
|
|
1956
|
+
throw new Error("Sandbox Vault file changed after its manifest was captured");
|
|
1957
|
+
}
|
|
1958
|
+
return { content, contentHash };
|
|
1959
|
+
}
|
|
1960
|
+
async function reconcileSandboxVault(config, box, workspaceId, options = {}) {
|
|
1961
|
+
if (!box.id) throw new Error(`Cannot reconcile Vault for ${workspaceId}: sandbox id is missing`);
|
|
1962
|
+
const incarnation = readReadySandboxFilesystemIncarnation(box);
|
|
1963
|
+
if (!incarnation.succeeded) {
|
|
1964
|
+
const reason = incarnation.error.code === "vault.filesystem_incarnation_missing" ? "filesystem_incarnation_missing" : "filesystem_incarnation_not_ready";
|
|
1965
|
+
return blockedSandboxVaultSync(config.root, incarnation.error, reason);
|
|
1966
|
+
}
|
|
1967
|
+
const reconciliationKey = [
|
|
1968
|
+
config.root,
|
|
1969
|
+
workspaceId,
|
|
1970
|
+
box.id,
|
|
1971
|
+
incarnation.value.id,
|
|
1972
|
+
options.sessionId ?? "sandbox-root"
|
|
1973
|
+
].join(":");
|
|
1974
|
+
const existingTask = vaultReconciliationTasks.get(reconciliationKey);
|
|
1975
|
+
if (existingTask) return existingTask;
|
|
1976
|
+
const task = reconcileSandboxVaultFilesToKv(
|
|
1977
|
+
config,
|
|
1978
|
+
box,
|
|
1979
|
+
workspaceId,
|
|
1980
|
+
box.id,
|
|
1981
|
+
incarnation.value,
|
|
1982
|
+
options
|
|
1983
|
+
);
|
|
1984
|
+
vaultReconciliationTasks.set(reconciliationKey, task);
|
|
1985
|
+
try {
|
|
1986
|
+
const result = await task;
|
|
1987
|
+
if (!result.succeeded || !result.complete || result.conflicts.length > 0 || result.refusedDeletionPaths.length > 0) {
|
|
1988
|
+
await config.vault.invalidateVaultBoxSync(workspaceId).catch((error) => console.warn("[vault] box sync invalidation failed", {
|
|
1989
|
+
workspaceId,
|
|
1990
|
+
error: error instanceof Error ? error.message : String(error)
|
|
1991
|
+
}));
|
|
1992
|
+
}
|
|
1993
|
+
return result;
|
|
1994
|
+
} finally {
|
|
1995
|
+
if (vaultReconciliationTasks.get(reconciliationKey) === task) {
|
|
1996
|
+
vaultReconciliationTasks.delete(reconciliationKey);
|
|
1997
|
+
}
|
|
1998
|
+
}
|
|
1999
|
+
}
|
|
2000
|
+
async function reconcileSandboxVaultFilesToKv(config, box, workspaceId, sandboxId, incarnation, options) {
|
|
2001
|
+
const storedBaseline = await config.vault.readVaultSandboxBaseline(workspaceId, sandboxId);
|
|
2002
|
+
const { baseline, legacy } = normalizeVaultSandboxBaseline(storedBaseline);
|
|
2003
|
+
if (!baseline || baseline.schema !== VAULT_SANDBOX_BASELINE_SCHEMA || baseline.filesystemIncarnationId !== incarnation.id) {
|
|
2004
|
+
return {
|
|
2005
|
+
succeeded: true,
|
|
2006
|
+
complete: true,
|
|
2007
|
+
rejectedPaths: [],
|
|
2008
|
+
binaryDriftPaths: [],
|
|
2009
|
+
refusedDeletionPaths: [],
|
|
2010
|
+
pathsMissingFromBox: [],
|
|
2011
|
+
operationId: options.operationId ?? null,
|
|
2012
|
+
changes: [],
|
|
2013
|
+
appliedPaths: [],
|
|
2014
|
+
conflicts: [],
|
|
2015
|
+
progress: {
|
|
2016
|
+
phase: "reconcile",
|
|
2017
|
+
processedPaths: 0,
|
|
2018
|
+
totalPaths: 0,
|
|
2019
|
+
remainingPaths: 0,
|
|
2020
|
+
committedGroups: 0
|
|
2021
|
+
},
|
|
2022
|
+
baselineResetReason: baseline ? "filesystem_incarnation_changed" : "baseline_missing"
|
|
2023
|
+
};
|
|
2024
|
+
}
|
|
2025
|
+
const manifest = await readSandboxVaultManifest(config, box, workspaceId, options.sessionId);
|
|
2026
|
+
if (!manifest.directoryPresent) {
|
|
2027
|
+
return blockedSandboxVaultSync(
|
|
2028
|
+
config.root,
|
|
2029
|
+
manifest.rejectedPaths[0],
|
|
2030
|
+
"vault_directory_missing",
|
|
2031
|
+
Object.keys(baseline.files).length
|
|
2032
|
+
);
|
|
2033
|
+
}
|
|
2034
|
+
const liveBaselinePaths = Object.entries(baseline.files).filter(([, file]) => !file.deleted).map(([path]) => path);
|
|
2035
|
+
const wouldDeletePaths = liveBaselinePaths.filter((path) => !manifest.entries.has(path)).sort();
|
|
2036
|
+
const renameTargets = /* @__PURE__ */ new Map();
|
|
2037
|
+
for (const [path, entry] of manifest.entries) {
|
|
2038
|
+
const base = baseline.files[path];
|
|
2039
|
+
if (base && !base.deleted) continue;
|
|
2040
|
+
renameTargets.set(entry.contentHash, (renameTargets.get(entry.contentHash) ?? 0) + 1);
|
|
2041
|
+
}
|
|
2042
|
+
const refusedDeletionPaths = wouldDeletePaths.filter((path) => {
|
|
2043
|
+
const hash = baseline.files[path]?.contentHash;
|
|
2044
|
+
const available = hash ? renameTargets.get(hash) ?? 0 : 0;
|
|
2045
|
+
if (available === 0) return true;
|
|
2046
|
+
renameTargets.set(hash, available - 1);
|
|
2047
|
+
return false;
|
|
2048
|
+
});
|
|
2049
|
+
if (baseline.progress?.operation === "hydrate") {
|
|
2050
|
+
return {
|
|
2051
|
+
succeeded: manifest.rejectedPaths.length === 0,
|
|
2052
|
+
complete: true,
|
|
2053
|
+
rejectedPaths: manifest.rejectedPaths,
|
|
2054
|
+
binaryDriftPaths: [],
|
|
2055
|
+
refusedDeletionPaths,
|
|
2056
|
+
pathsMissingFromBox: wouldDeletePaths,
|
|
2057
|
+
operationId: options.operationId ?? null,
|
|
2058
|
+
changes: [],
|
|
2059
|
+
appliedPaths: [],
|
|
2060
|
+
conflicts: [],
|
|
2061
|
+
progress: {
|
|
2062
|
+
phase: "reconcile",
|
|
2063
|
+
processedPaths: 0,
|
|
2064
|
+
totalPaths: manifest.entries.size,
|
|
2065
|
+
remainingPaths: 0,
|
|
2066
|
+
committedGroups: 0
|
|
2067
|
+
},
|
|
2068
|
+
...refusedDeletionPaths.length > 0 ? { blockedReason: "sandbox_deletion_denied" } : {}
|
|
2069
|
+
};
|
|
2070
|
+
}
|
|
2071
|
+
const rejectedPaths = [...manifest.rejectedPaths];
|
|
2072
|
+
const binaryDriftPaths = [];
|
|
2073
|
+
const candidatePaths = /* @__PURE__ */ new Set();
|
|
2074
|
+
for (const [path, entry] of manifest.entries) {
|
|
2075
|
+
const base = baseline.files[path];
|
|
2076
|
+
if (base?.binary) {
|
|
2077
|
+
if (entry.contentHash !== base.onDiskHash) binaryDriftPaths.push(path);
|
|
2078
|
+
continue;
|
|
2079
|
+
}
|
|
2080
|
+
if (!base || base.deleted || base.contentHash !== entry.contentHash) {
|
|
2081
|
+
candidatePaths.add(path);
|
|
2082
|
+
}
|
|
2083
|
+
}
|
|
2084
|
+
for (const path of wouldDeletePaths) {
|
|
2085
|
+
if (!refusedDeletionPaths.includes(path)) candidatePaths.add(path);
|
|
2086
|
+
}
|
|
2087
|
+
const candidatePathList = [...candidatePaths];
|
|
2088
|
+
const candidateUnits = buildSandboxVaultRenameUnits(
|
|
2089
|
+
candidatePathList,
|
|
2090
|
+
(path) => {
|
|
2091
|
+
const base = baseline.files[path];
|
|
2092
|
+
const entry = manifest.entries.get(path);
|
|
2093
|
+
if ((!base || base.deleted) && entry) {
|
|
2094
|
+
return { path, kind: "put", contentHash: entry.contentHash };
|
|
2095
|
+
}
|
|
2096
|
+
if (base && !base.deleted && !entry && base.contentHash) {
|
|
2097
|
+
return { path, kind: "delete", contentHash: base.contentHash };
|
|
2098
|
+
}
|
|
2099
|
+
return { path, kind: null, contentHash: null };
|
|
2100
|
+
},
|
|
2101
|
+
[...candidatePathList].sort()
|
|
2102
|
+
);
|
|
2103
|
+
const candidateBudget = VAULT_SYNC_GROUP_SIZE * VAULT_SYNC_MAX_GROUPS;
|
|
2104
|
+
const selectedCandidatePaths = [];
|
|
2105
|
+
for (const unit of candidateUnits) {
|
|
2106
|
+
if (selectedCandidatePaths.length > 0 && selectedCandidatePaths.length + unit.length > candidateBudget) {
|
|
2107
|
+
break;
|
|
2108
|
+
}
|
|
2109
|
+
selectedCandidatePaths.push(...unit);
|
|
2110
|
+
}
|
|
2111
|
+
const snapshots = /* @__PURE__ */ new Map();
|
|
2112
|
+
await mapWithConcurrency(
|
|
2113
|
+
selectedCandidatePaths.filter((path) => manifest.entries.has(path)),
|
|
2114
|
+
VAULT_IO_CONCURRENCY2,
|
|
2115
|
+
async (path) => {
|
|
2116
|
+
const entry = manifest.entries.get(path);
|
|
2117
|
+
if (!entry) throw new Error(`Sandbox Vault manifest entry is missing for ${path}`);
|
|
2118
|
+
try {
|
|
2119
|
+
snapshots.set(path, await readChangedSandboxFile(config, box, entry, options.sessionId));
|
|
2120
|
+
} catch (error) {
|
|
2121
|
+
rejectedPaths.push(sandboxVaultRejection(
|
|
2122
|
+
path,
|
|
2123
|
+
"read",
|
|
2124
|
+
"sandbox.read_failed",
|
|
2125
|
+
error
|
|
2126
|
+
));
|
|
2127
|
+
}
|
|
2128
|
+
}
|
|
2129
|
+
);
|
|
2130
|
+
const readableCandidates = selectedCandidatePaths.filter((path) => !manifest.entries.has(path) || snapshots.has(path));
|
|
2131
|
+
const currentStates = new Map(await mapWithConcurrency(
|
|
2132
|
+
readableCandidates,
|
|
2133
|
+
VAULT_IO_CONCURRENCY2,
|
|
2134
|
+
async (path) => [path, await config.vault.readVaultFileState(workspaceId, path)]
|
|
2135
|
+
));
|
|
2136
|
+
const changes = [];
|
|
2137
|
+
for (const path of readableCandidates.sort()) {
|
|
2138
|
+
const snapshot = snapshots.get(path);
|
|
2139
|
+
const current = currentStates.get(path);
|
|
2140
|
+
if (!current) throw new Error(`Vault state missing for ${path}`);
|
|
2141
|
+
const base = baseline.files[path];
|
|
2142
|
+
if (!base) {
|
|
2143
|
+
if (!snapshot || !current.deleted && current.contentHash === snapshot.contentHash) {
|
|
2144
|
+
continue;
|
|
2145
|
+
}
|
|
2146
|
+
changes.push({
|
|
2147
|
+
path,
|
|
2148
|
+
kind: "put",
|
|
2149
|
+
content: snapshot.content,
|
|
2150
|
+
contentHash: snapshot.contentHash,
|
|
2151
|
+
expectedRevision: null
|
|
2152
|
+
});
|
|
2153
|
+
continue;
|
|
2154
|
+
}
|
|
2155
|
+
if (snapshot) {
|
|
2156
|
+
if (!base.deleted && base.contentHash === snapshot.contentHash) continue;
|
|
2157
|
+
changes.push({
|
|
2158
|
+
path,
|
|
2159
|
+
kind: "put",
|
|
2160
|
+
content: snapshot.content,
|
|
2161
|
+
contentHash: snapshot.contentHash,
|
|
2162
|
+
expectedRevision: base.revision
|
|
2163
|
+
});
|
|
2164
|
+
} else if (!base.deleted) {
|
|
2165
|
+
changes.push({
|
|
2166
|
+
path,
|
|
2167
|
+
kind: "delete",
|
|
2168
|
+
contentHash: base.contentHash,
|
|
2169
|
+
expectedRevision: base.revision
|
|
2170
|
+
});
|
|
2171
|
+
}
|
|
2172
|
+
}
|
|
2173
|
+
const pendingPutsByHash = /* @__PURE__ */ new Map();
|
|
2174
|
+
for (const change of changes) {
|
|
2175
|
+
if (change.kind !== "put" || !change.contentHash) continue;
|
|
2176
|
+
pendingPutsByHash.set(change.contentHash, (pendingPutsByHash.get(change.contentHash) ?? 0) + 1);
|
|
2177
|
+
}
|
|
2178
|
+
const authorizedChanges = changes.filter((change) => {
|
|
2179
|
+
if (change.kind !== "delete") return true;
|
|
2180
|
+
const hash = change.contentHash;
|
|
2181
|
+
const available = hash ? pendingPutsByHash.get(hash) ?? 0 : 0;
|
|
2182
|
+
if (!hash || available === 0) {
|
|
2183
|
+
refusedDeletionPaths.push(change.path);
|
|
2184
|
+
return false;
|
|
2185
|
+
}
|
|
2186
|
+
pendingPutsByHash.set(hash, available - 1);
|
|
2187
|
+
return true;
|
|
2188
|
+
});
|
|
2189
|
+
refusedDeletionPaths.sort();
|
|
2190
|
+
const groups = groupSandboxVaultChanges(authorizedChanges, VAULT_SYNC_GROUP_SIZE);
|
|
2191
|
+
const groupsToCommit = groups.slice(0, VAULT_SYNC_MAX_GROUPS);
|
|
2192
|
+
const stagedChanges = groupsToCommit.flat().map((change) => ({
|
|
2193
|
+
path: change.path,
|
|
2194
|
+
kind: change.kind,
|
|
2195
|
+
expectedRevision: change.expectedRevision,
|
|
2196
|
+
contentHash: change.kind === "put" ? change.contentHash : null
|
|
2197
|
+
}));
|
|
2198
|
+
const stagedFingerprint = await sha256Hex(JSON.stringify(stagedChanges));
|
|
2199
|
+
const operationId = options.operationId ?? (stagedChanges.length > 0 ? `sandbox:${sandboxId}:${stagedFingerprint}` : null);
|
|
2200
|
+
if (operationId && options.onStaged) {
|
|
2201
|
+
await options.onStaged({ operationId, changes: stagedChanges });
|
|
2202
|
+
}
|
|
2203
|
+
const appliedPaths = [];
|
|
2204
|
+
const appliedFiles = [];
|
|
2205
|
+
const conflicts = [];
|
|
2206
|
+
for (const group of groupsToCommit) {
|
|
2207
|
+
const fingerprint = await sha256Hex(JSON.stringify(group.map((change) => ({
|
|
2208
|
+
path: change.path,
|
|
2209
|
+
kind: change.kind,
|
|
2210
|
+
expectedRevision: change.expectedRevision,
|
|
2211
|
+
contentHash: change.contentHash
|
|
2212
|
+
}))));
|
|
2213
|
+
try {
|
|
2214
|
+
const result = await config.vault.commitVaultMutation({
|
|
2215
|
+
workspaceId,
|
|
2216
|
+
operationId: options.operationId ? `${options.operationId}:${fingerprint}` : `sandbox:${sandboxId}:${fingerprint}`,
|
|
2217
|
+
writer: { kind: "sandbox", id: sandboxId },
|
|
2218
|
+
changes: group.map(({ contentHash: _contentHash, ...change }) => change)
|
|
2219
|
+
});
|
|
2220
|
+
if (result.status === "applied") {
|
|
2221
|
+
appliedFiles.push(...result.files);
|
|
2222
|
+
appliedPaths.push(...result.files.map((file) => file.path));
|
|
2223
|
+
} else {
|
|
2224
|
+
conflicts.push(...result.conflicts);
|
|
2225
|
+
}
|
|
2226
|
+
} catch (error) {
|
|
2227
|
+
for (const change of group) {
|
|
2228
|
+
rejectedPaths.push(sandboxVaultRejection(
|
|
2229
|
+
change.path,
|
|
2230
|
+
"commit",
|
|
2231
|
+
"vault.commit_failed",
|
|
2232
|
+
error
|
|
2233
|
+
));
|
|
2234
|
+
}
|
|
2235
|
+
}
|
|
2236
|
+
}
|
|
2237
|
+
if (appliedFiles.length > 0 || binaryDriftPaths.length > 0) {
|
|
2238
|
+
const baselineFiles = legacy ? Object.fromEntries(Object.entries(baseline.files).map(([path, file]) => [
|
|
2239
|
+
path,
|
|
2240
|
+
file.deleted ? file : { ...file, revision: null }
|
|
2241
|
+
])) : { ...baseline.files };
|
|
2242
|
+
for (const file of appliedFiles) {
|
|
2243
|
+
const snapshot = snapshots.get(file.path);
|
|
2244
|
+
baselineFiles[file.path] = snapshot?.binary ? {
|
|
2245
|
+
revision: file.revision,
|
|
2246
|
+
contentHash: file.contentHash,
|
|
2247
|
+
deleted: file.deleted,
|
|
2248
|
+
binary: true,
|
|
2249
|
+
onDiskHash: snapshot.onDiskHash ?? null
|
|
2250
|
+
} : {
|
|
2251
|
+
revision: file.revision,
|
|
2252
|
+
contentHash: file.contentHash,
|
|
2253
|
+
deleted: file.deleted
|
|
2254
|
+
};
|
|
2255
|
+
}
|
|
2256
|
+
for (const path of binaryDriftPaths) {
|
|
2257
|
+
baselineFiles[path] = {
|
|
2258
|
+
revision: null,
|
|
2259
|
+
contentHash: null,
|
|
2260
|
+
deleted: false,
|
|
2261
|
+
binary: true,
|
|
2262
|
+
onDiskHash: null
|
|
2263
|
+
};
|
|
2264
|
+
}
|
|
2265
|
+
await config.vault.writeVaultSandboxBaseline({
|
|
2266
|
+
schema: VAULT_SANDBOX_BASELINE_SCHEMA,
|
|
2267
|
+
workspaceId,
|
|
2268
|
+
sandboxId,
|
|
2269
|
+
filesystemIncarnationId: incarnation.id,
|
|
2270
|
+
files: baselineFiles,
|
|
2271
|
+
updatedAt: (/* @__PURE__ */ new Date()).toISOString()
|
|
2272
|
+
});
|
|
2273
|
+
}
|
|
2274
|
+
const processedPaths = selectedCandidatePaths.length;
|
|
2275
|
+
const remainingPaths = candidatePaths.size - processedPaths;
|
|
2276
|
+
return {
|
|
2277
|
+
succeeded: rejectedPaths.length === 0,
|
|
2278
|
+
complete: remainingPaths === 0,
|
|
2279
|
+
rejectedPaths,
|
|
2280
|
+
binaryDriftPaths,
|
|
2281
|
+
refusedDeletionPaths,
|
|
2282
|
+
pathsMissingFromBox: wouldDeletePaths,
|
|
2283
|
+
...refusedDeletionPaths.length > 0 ? { blockedReason: "sandbox_deletion_denied" } : {},
|
|
2284
|
+
operationId,
|
|
2285
|
+
changes: stagedChanges,
|
|
2286
|
+
appliedPaths,
|
|
2287
|
+
conflicts,
|
|
2288
|
+
progress: {
|
|
2289
|
+
phase: "reconcile",
|
|
2290
|
+
processedPaths,
|
|
2291
|
+
totalPaths: candidatePaths.size,
|
|
2292
|
+
remainingPaths,
|
|
2293
|
+
committedGroups: groupsToCommit.length
|
|
2294
|
+
}
|
|
2295
|
+
};
|
|
2296
|
+
}
|
|
2297
|
+
async function probeSandboxVaultLoss(config, box, workspaceId) {
|
|
2298
|
+
if (!box.id) return [];
|
|
2299
|
+
const incarnation = readReadySandboxFilesystemIncarnation(box);
|
|
2300
|
+
if (!incarnation.succeeded) return [];
|
|
2301
|
+
const { baseline } = normalizeVaultSandboxBaseline(
|
|
2302
|
+
await config.vault.readVaultSandboxBaseline(workspaceId, box.id)
|
|
2303
|
+
);
|
|
2304
|
+
if (!baseline || baseline.schema !== VAULT_SANDBOX_BASELINE_SCHEMA || baseline.filesystemIncarnationId !== incarnation.value.id) {
|
|
2305
|
+
return [];
|
|
2306
|
+
}
|
|
2307
|
+
const livePaths = Object.entries(baseline.files).filter(([, file]) => !file.deleted).map(([path]) => path);
|
|
2308
|
+
if (livePaths.length === 0) return [];
|
|
2309
|
+
try {
|
|
2310
|
+
const result = await box.exec(
|
|
2311
|
+
`find ${shellQuote(config.root)} -type f -print -quit 2>/dev/null`
|
|
2312
|
+
);
|
|
2313
|
+
if (result.stdout.trim().length > 0) return [];
|
|
2314
|
+
} catch {
|
|
2315
|
+
return [];
|
|
2316
|
+
}
|
|
2317
|
+
return livePaths;
|
|
2318
|
+
}
|
|
2319
|
+
function bytesToBase64(bytes) {
|
|
2320
|
+
let binary = "";
|
|
2321
|
+
for (let offset = 0; offset < bytes.length; offset += 32768) {
|
|
2322
|
+
binary += String.fromCharCode(...bytes.subarray(offset, offset + 32768));
|
|
2323
|
+
}
|
|
2324
|
+
return btoa(binary);
|
|
2325
|
+
}
|
|
2326
|
+
|
|
2327
|
+
// src/vault/store/sandbox-engine.ts
|
|
2328
|
+
function createSandboxVaultSync(config) {
|
|
2329
|
+
return {
|
|
2330
|
+
root: config.root,
|
|
2331
|
+
hydrate: (box, workspaceId, options) => hydrateSandboxVault(config, box, workspaceId, options),
|
|
2332
|
+
reconcile: (box, workspaceId, options) => reconcileSandboxVault(config, box, workspaceId, options),
|
|
2333
|
+
probeLoss: (box, workspaceId) => probeSandboxVaultLoss(config, box, workspaceId),
|
|
2334
|
+
assertReconciliation: assertSandboxVaultReconciliation
|
|
2335
|
+
};
|
|
2336
|
+
}
|
|
1141
2337
|
export {
|
|
2338
|
+
SANDBOX_VAULT_SYNC_CONTRACT,
|
|
2339
|
+
VAULT_BASE64_BODY_MARKER,
|
|
2340
|
+
VAULT_BOX_MUTATION_STALE_MS,
|
|
2341
|
+
VAULT_BOX_STATE_KEY,
|
|
2342
|
+
VAULT_BOX_STATE_SCHEMA,
|
|
2343
|
+
VAULT_BOX_SYNC_MAX_SKIPS,
|
|
1142
2344
|
VAULT_HEAD_SCHEMA,
|
|
1143
2345
|
VAULT_HISTORY_MAX_REVISIONS,
|
|
1144
2346
|
VAULT_KV_KEY_MAX_BYTES,
|
|
@@ -1159,26 +2361,41 @@ export {
|
|
|
1159
2361
|
VaultReadConvergenceError,
|
|
1160
2362
|
VaultStorageKeyError,
|
|
1161
2363
|
applyVaultMutationBatch,
|
|
2364
|
+
assertSandboxVaultReconciliation,
|
|
1162
2365
|
buildSandboxVaultRenameUnits,
|
|
1163
2366
|
buildTreeFromPaths,
|
|
2367
|
+
createSandboxVaultSync,
|
|
1164
2368
|
createVaultCoordinator,
|
|
1165
2369
|
createVaultStore,
|
|
2370
|
+
emptyVaultBoxState,
|
|
1166
2371
|
flattenVaultTreePaths,
|
|
1167
2372
|
groupSandboxVaultChanges,
|
|
2373
|
+
healStaleMutation,
|
|
2374
|
+
hydrateSandboxVault,
|
|
1168
2375
|
isHiddenVaultFilePath,
|
|
2376
|
+
isSyncedBoxWriter,
|
|
1169
2377
|
mapWithConcurrency,
|
|
1170
2378
|
normalizeVaultSandboxBaseline,
|
|
1171
2379
|
normalizeVaultWriterProvenance,
|
|
2380
|
+
parseVaultBoxState,
|
|
2381
|
+
parseVaultBoxStateView,
|
|
2382
|
+
parseVaultBoxSyncSkips,
|
|
1172
2383
|
parseVaultHeadMetadata,
|
|
1173
2384
|
parseVaultPathErrorResponse,
|
|
2385
|
+
probeSandboxVaultLoss,
|
|
1174
2386
|
readReadySandboxFilesystemIncarnation,
|
|
1175
2387
|
readVaultHeadState,
|
|
1176
2388
|
readVaultHeadStateConverged,
|
|
1177
2389
|
readVaultHeadStateForErasure,
|
|
2390
|
+
reconcileSandboxVault,
|
|
2391
|
+
sandboxBootstrapRecorded,
|
|
1178
2392
|
sandboxVaultRejection,
|
|
1179
2393
|
validateVaultFilePath,
|
|
1180
2394
|
validateVaultMutationKeys,
|
|
1181
2395
|
validateWorkspaceId,
|
|
2396
|
+
vaultBinaryDocument,
|
|
2397
|
+
vaultBinaryPayload,
|
|
2398
|
+
vaultBoxInSync,
|
|
1182
2399
|
vaultConvergenceResponse,
|
|
1183
2400
|
vaultCoordinatorJson,
|
|
1184
2401
|
vaultErasureJobKey,
|
|
@@ -1197,6 +2414,12 @@ export {
|
|
|
1197
2414
|
vaultPathErrorResponseBody,
|
|
1198
2415
|
vaultRevisionKey,
|
|
1199
2416
|
vaultRevisionPrefix,
|
|
1200
|
-
vaultWriterActor
|
|
2417
|
+
vaultWriterActor,
|
|
2418
|
+
viewOfVaultBoxState,
|
|
2419
|
+
withBootstrap,
|
|
2420
|
+
withMutationFinished,
|
|
2421
|
+
withMutationStarted,
|
|
2422
|
+
withSync,
|
|
2423
|
+
withSyncInvalidated
|
|
1201
2424
|
};
|
|
1202
2425
|
//# sourceMappingURL=index.js.map
|