@tangle-network/agent-app 0.63.59 → 0.63.60
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/theme/tokens.css +2 -4
- package/dist/vault/store/index.js +330 -259
- package/dist/vault/store/index.js.map +1 -1
- package/dist/vault/store/sandbox-hydrate.d.ts +22 -1
- package/dist/vault/store/sandbox-reconcile.d.ts +11 -3
- package/dist/vault/store/sandbox-sync.d.ts +26 -0
- package/package.json +5 -5
|
@@ -1,4 +1,5 @@
|
|
|
1
1
|
import {
|
|
2
|
+
assessVaultDeletionBatch,
|
|
2
3
|
compareIncarnationBaseline
|
|
3
4
|
} from "../../chunk-GS7VWV26.js";
|
|
4
5
|
import {
|
|
@@ -1511,9 +1512,10 @@ async function hydrateSandboxVault(config, box, workspaceId, options = {}) {
|
|
|
1511
1512
|
const incarnation = readReadySandboxFilesystemIncarnation(box);
|
|
1512
1513
|
const identity = box.id && incarnation.succeeded ? { sandboxId: box.id, filesystemIncarnationId: incarnation.value.id } : null;
|
|
1513
1514
|
const protectedPaths = Boolean(options.conflictPaths?.size || options.appliedPaths?.size) || options.pathsMissingFromBox !== void 0;
|
|
1514
|
-
const
|
|
1515
|
+
const ownsSync = options.recordSync !== false;
|
|
1516
|
+
const view = identity && ownsSync ? await config.vault.readVaultBoxState(workspaceId) : null;
|
|
1515
1517
|
if (identity && !protectedPaths && vaultBoxInSync(view, identity, contract)) {
|
|
1516
|
-
return { succeeded: true, value: { written: 0, deleted: 0, unchanged: 0, processedHeads: 0, inSync: true } };
|
|
1518
|
+
return { succeeded: true, value: { written: 0, deleted: 0, unchanged: 0, processedHeads: 0, inSync: true, skipped: [], deferred: null } };
|
|
1517
1519
|
}
|
|
1518
1520
|
const vaultDir = box.fs.mkdir(config.root, { recursive: true });
|
|
1519
1521
|
vaultDir.catch(() => {
|
|
@@ -1522,20 +1524,23 @@ async function hydrateSandboxVault(config, box, workspaceId, options = {}) {
|
|
|
1522
1524
|
let deleted = 0;
|
|
1523
1525
|
let unchanged = 0;
|
|
1524
1526
|
let processedHeadCount = 0;
|
|
1527
|
+
const run = { progress: options.progress ?? { remainingChars: Infinity, writtenPaths: [] }, skipped: [] };
|
|
1525
1528
|
for (let pageIndex = 0; pageIndex < VAULT_HYDRATION_MAX_PAGES; pageIndex += 1) {
|
|
1526
|
-
const page = await hydratePage(config, box, workspaceId, options, vaultDir);
|
|
1529
|
+
const page = await hydratePage(config, box, workspaceId, options, vaultDir, run);
|
|
1527
1530
|
written += page.written;
|
|
1528
1531
|
deleted += page.deleted;
|
|
1529
1532
|
unchanged += page.unchanged;
|
|
1530
1533
|
processedHeadCount = page.processedHeadCount;
|
|
1531
|
-
if (page.complete) {
|
|
1532
|
-
|
|
1533
|
-
|
|
1534
|
+
if (page.complete || page.deferred) {
|
|
1535
|
+
const stats = { written, deleted, unchanged, processedHeads: processedHeadCount, skipped: run.skipped, deferred: page.deferred };
|
|
1536
|
+
if (page.deferred) return { succeeded: true, value: stats };
|
|
1537
|
+
if (identity && view && !view.inFlight && run.skipped.length <= VAULT_BOX_SYNC_MAX_SKIPS) {
|
|
1538
|
+
await config.vault.recordVaultBoxSync(workspaceId, { ...identity, contract, generation: view.generation, skipped: run.skipped }).catch((error) => console.warn("[vault] box sync record failed", {
|
|
1534
1539
|
workspaceId,
|
|
1535
1540
|
error: error instanceof Error ? error.message : String(error)
|
|
1536
1541
|
}));
|
|
1537
1542
|
}
|
|
1538
|
-
return { succeeded: true, value:
|
|
1543
|
+
return { succeeded: true, value: stats };
|
|
1539
1544
|
}
|
|
1540
1545
|
if (page.cursor === null) {
|
|
1541
1546
|
throw new VaultHydrationIncompleteError({
|
|
@@ -1559,18 +1564,25 @@ async function hydrateSandboxVault(config, box, workspaceId, options = {}) {
|
|
|
1559
1564
|
return { succeeded: false, error: typed };
|
|
1560
1565
|
}
|
|
1561
1566
|
}
|
|
1562
|
-
async function hydratePage(config, box, workspaceId, options, vaultDir) {
|
|
1567
|
+
async function hydratePage(config, box, workspaceId, options, vaultDir, run) {
|
|
1563
1568
|
const { vault } = config;
|
|
1569
|
+
const stage = options.onStage ?? (() => {
|
|
1570
|
+
});
|
|
1564
1571
|
if (!box.id) throw new Error(`Cannot hydrate Vault for ${workspaceId}: sandbox id is missing`);
|
|
1572
|
+
const sandboxId = box.id;
|
|
1565
1573
|
const incarnation = readReadySandboxFilesystemIncarnation(box);
|
|
1566
1574
|
if (!incarnation.succeeded) throw incarnation.error;
|
|
1567
|
-
|
|
1575
|
+
let startedAt = Date.now();
|
|
1576
|
+
const normalized = normalizeVaultSandboxBaseline(await vault.readVaultSandboxBaseline(workspaceId, sandboxId));
|
|
1577
|
+
stage("baseline_read", startedAt);
|
|
1568
1578
|
const baselineMatchesIncarnation = normalized.baseline?.schema === VAULT_SANDBOX_BASELINE_SCHEMA && normalized.baseline.filesystemIncarnationId === incarnation.value.id;
|
|
1569
1579
|
const previousBaseline = baselineMatchesIncarnation ? normalized.baseline : null;
|
|
1570
1580
|
const legacyBaseline = baselineMatchesIncarnation ? normalized.legacy : false;
|
|
1571
1581
|
const cursor = previousBaseline?.progress?.operation === "hydrate" ? previousBaseline.progress.cursor : void 0;
|
|
1582
|
+
startedAt = Date.now();
|
|
1572
1583
|
const page = await vault.listVaultHeadPage(workspaceId, { cursor, limit: VAULT_HYDRATION_HEAD_PAGE_SIZE });
|
|
1573
|
-
|
|
1584
|
+
stage("head_list", startedAt, { entries: page.entries.length });
|
|
1585
|
+
const baselineFiles = legacyBaseline ? Object.fromEntries(Object.entries(previousBaseline?.files ?? {}).map(([path, file]) => [path, file.deleted ? file : { ...file, revision: null }])) : { ...previousBaseline?.files ?? {} };
|
|
1574
1586
|
const conflictPaths = options.conflictPaths ?? /* @__PURE__ */ new Set();
|
|
1575
1587
|
const appliedPaths = options.appliedPaths ?? /* @__PURE__ */ new Set();
|
|
1576
1588
|
const pathsMissingFromBox = await options.pathsMissingFromBox ?? /* @__PURE__ */ new Set();
|
|
@@ -1599,16 +1611,47 @@ async function hydratePage(config, box, workspaceId, options, vaultDir) {
|
|
|
1599
1611
|
deleted += 1;
|
|
1600
1612
|
}
|
|
1601
1613
|
const binaryHashes = /* @__PURE__ */ new Map();
|
|
1614
|
+
const recordEntry = (entry) => {
|
|
1615
|
+
const previous = baselineFiles[entry.path];
|
|
1616
|
+
const onDiskHash = binaryHashes.get(entry.path);
|
|
1617
|
+
if (onDiskHash !== void 0) {
|
|
1618
|
+
baselineFiles[entry.path] = { revision: entry.revision, contentHash: entry.contentHash, deleted: entry.deleted, binary: true, onDiskHash };
|
|
1619
|
+
} else if (previous?.binary && previous.revision === entry.revision && previous.contentHash === entry.contentHash && previous.deleted === entry.deleted) {
|
|
1620
|
+
baselineFiles[entry.path] = previous;
|
|
1621
|
+
} else {
|
|
1622
|
+
baselineFiles[entry.path] = { revision: entry.revision, contentHash: entry.contentHash, deleted: entry.deleted };
|
|
1623
|
+
}
|
|
1624
|
+
};
|
|
1625
|
+
const writeBaseline = async (progress) => {
|
|
1626
|
+
const writeStartedAt = Date.now();
|
|
1627
|
+
await vault.writeVaultSandboxBaseline({
|
|
1628
|
+
schema: VAULT_SANDBOX_BASELINE_SCHEMA,
|
|
1629
|
+
workspaceId,
|
|
1630
|
+
sandboxId,
|
|
1631
|
+
filesystemIncarnationId: incarnation.value.id,
|
|
1632
|
+
files: baselineFiles,
|
|
1633
|
+
...progress ? { progress } : {},
|
|
1634
|
+
updatedAt: (/* @__PURE__ */ new Date()).toISOString()
|
|
1635
|
+
});
|
|
1636
|
+
stage("baseline_write", writeStartedAt);
|
|
1637
|
+
};
|
|
1602
1638
|
let written = 0;
|
|
1639
|
+
let readMs = 0;
|
|
1640
|
+
let writeMs = 0;
|
|
1641
|
+
let deferredHeads = [];
|
|
1603
1642
|
const liveHeads = changedHeads.filter((entry) => !entry.deleted);
|
|
1643
|
+
const headsByPath = new Map(liveHeads.map((entry) => [entry.path, entry]));
|
|
1604
1644
|
for (let next = 0; next < liveHeads.length; ) {
|
|
1605
|
-
|
|
1645
|
+
if (run.progress.remainingChars <= 0) {
|
|
1646
|
+
deferredHeads = liveHeads.slice(next);
|
|
1647
|
+
break;
|
|
1648
|
+
}
|
|
1606
1649
|
const snapshots = [];
|
|
1607
1650
|
let batchChars = 0;
|
|
1651
|
+
const readStartedAt = Date.now();
|
|
1608
1652
|
while (next < liveHeads.length && batchChars < VAULT_HYDRATION_BATCH_CHARS) {
|
|
1609
1653
|
const slice = liveHeads.slice(next, next + VAULT_IO_CONCURRENCY);
|
|
1610
1654
|
next += slice.length;
|
|
1611
|
-
batch.push(...slice);
|
|
1612
1655
|
const read = await mapWithConcurrency(slice, VAULT_IO_CONCURRENCY, async (entry) => readSnapshot(config, workspaceId, entry, skippedPaths));
|
|
1613
1656
|
for (const snapshot of read) {
|
|
1614
1657
|
if (!snapshot) continue;
|
|
@@ -1616,11 +1659,46 @@ async function hydratePage(config, box, workspaceId, options, vaultDir) {
|
|
|
1616
1659
|
batchChars += snapshot.kind === "text" ? snapshot.content.length : snapshot.payload.length;
|
|
1617
1660
|
}
|
|
1618
1661
|
}
|
|
1619
|
-
|
|
1620
|
-
|
|
1621
|
-
|
|
1622
|
-
|
|
1623
|
-
|
|
1662
|
+
run.progress.remainingChars -= batchChars;
|
|
1663
|
+
readMs += Date.now() - readStartedAt;
|
|
1664
|
+
const writeStartedAt = Date.now();
|
|
1665
|
+
const present = config.skipFilesAlreadyOnBox ? await pathsAlreadyOnBox(config, box, snapshots.filter((snapshot) => !baselineFiles[snapshot.path] && !pathsMissingFromBox.has(snapshot.path))) : /* @__PURE__ */ new Set();
|
|
1666
|
+
const toWrite = snapshots.filter((snapshot) => !present.has(snapshot.path));
|
|
1667
|
+
const refused = [
|
|
1668
|
+
...await writeTextSnapshots(config, box, toWrite.filter((snapshot) => snapshot.kind === "text")),
|
|
1669
|
+
...await writeBinaries(config, box, workspaceId, toWrite.filter((snapshot) => snapshot.kind === "binary"))
|
|
1670
|
+
];
|
|
1671
|
+
writeMs += Date.now() - writeStartedAt;
|
|
1672
|
+
for (const skip of refused) skippedPaths.add(skip.path);
|
|
1673
|
+
run.skipped.push(...refused);
|
|
1674
|
+
for (const snapshot of snapshots) {
|
|
1675
|
+
if (snapshot.kind === "binary" && !skippedPaths.has(snapshot.path)) binaryHashes.set(snapshot.path, snapshot.onDiskHash);
|
|
1676
|
+
}
|
|
1677
|
+
for (const snapshot of toWrite) if (!skippedPaths.has(snapshot.path)) run.progress.writtenPaths.push(snapshot.path);
|
|
1678
|
+
written += toWrite.length - refused.length;
|
|
1679
|
+
if (next < liveHeads.length) {
|
|
1680
|
+
for (const snapshot of snapshots) {
|
|
1681
|
+
const entry = headsByPath.get(snapshot.path);
|
|
1682
|
+
if (entry && !skippedPaths.has(snapshot.path)) recordEntry(entry);
|
|
1683
|
+
}
|
|
1684
|
+
await writeBaseline(previousBaseline?.progress);
|
|
1685
|
+
}
|
|
1686
|
+
}
|
|
1687
|
+
stage("kv_read", Date.now() - readMs, { heads: liveHeads.length - deferredHeads.length, deferred: deferredHeads.length });
|
|
1688
|
+
stage("sandbox_write", Date.now() - writeMs, { files: written });
|
|
1689
|
+
const previousProcessedHeadCount = previousBaseline?.progress?.operation === "hydrate" ? previousBaseline.progress.processedHeadCount : 0;
|
|
1690
|
+
const protectedInPage = page.entries.filter((entry) => conflictPaths.has(entry.path) || appliedPaths.has(entry.path)).length;
|
|
1691
|
+
const unchanged = Math.max(0, page.entries.length - changedHeads.length - protectedInPage);
|
|
1692
|
+
if (deferredHeads.length > 0) {
|
|
1693
|
+
return {
|
|
1694
|
+
deferred: { pendingPaths: deferredHeads.map((entry) => entry.path), morePages: !page.complete },
|
|
1695
|
+
complete: false,
|
|
1696
|
+
cursor: cursor ?? null,
|
|
1697
|
+
processedHeadCount: previousProcessedHeadCount,
|
|
1698
|
+
written,
|
|
1699
|
+
deleted,
|
|
1700
|
+
unchanged
|
|
1701
|
+
};
|
|
1624
1702
|
}
|
|
1625
1703
|
const appliedBinaryHashes = await mapWithConcurrency(
|
|
1626
1704
|
page.entries.filter((entry) => appliedPaths.has(entry.path) && !entry.deleted),
|
|
@@ -1635,46 +1713,38 @@ async function hydratePage(config, box, workspaceId, options, vaultDir) {
|
|
|
1635
1713
|
for (const entry of appliedBinaryHashes) if (entry) binaryHashes.set(entry[0], entry[1]);
|
|
1636
1714
|
for (const entry of page.entries) {
|
|
1637
1715
|
if (conflictPaths.has(entry.path) || skippedPaths.has(entry.path)) continue;
|
|
1638
|
-
|
|
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
|
-
}
|
|
1716
|
+
recordEntry(entry);
|
|
1647
1717
|
}
|
|
1648
1718
|
for (const path of missingBaselinePaths) delete baselineFiles[path];
|
|
1649
|
-
const processedHeadCount =
|
|
1719
|
+
const processedHeadCount = previousProcessedHeadCount + page.entries.length;
|
|
1650
1720
|
const baselineUnchanged = page.complete && previousBaseline !== null && !legacyBaseline && !previousBaseline.progress && sameBaselineFiles(previousBaseline.files, baselineFiles);
|
|
1651
1721
|
if (!baselineUnchanged) {
|
|
1652
|
-
await
|
|
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
|
-
});
|
|
1722
|
+
await writeBaseline(page.complete || page.cursor === null ? void 0 : { operation: "hydrate", cursor: page.cursor, processedHeadCount, unseenBaselinePaths: [...unseenBaselinePaths].sort() });
|
|
1668
1723
|
}
|
|
1669
|
-
|
|
1670
|
-
|
|
1671
|
-
|
|
1672
|
-
|
|
1673
|
-
|
|
1674
|
-
|
|
1675
|
-
|
|
1676
|
-
|
|
1677
|
-
}
|
|
1724
|
+
return { deferred: null, complete: page.complete, cursor: page.cursor, processedHeadCount, written, deleted, unchanged };
|
|
1725
|
+
}
|
|
1726
|
+
async function pathsAlreadyOnBox(config, box, snapshots) {
|
|
1727
|
+
const present = /* @__PURE__ */ new Set();
|
|
1728
|
+
if (snapshots.length === 0) return present;
|
|
1729
|
+
const expected = /* @__PURE__ */ new Map();
|
|
1730
|
+
for (const snapshot of snapshots) {
|
|
1731
|
+
expected.set(snapshot.path, snapshot.kind === "binary" ? snapshot.onDiskHash : await sha256Hex(snapshot.content));
|
|
1732
|
+
}
|
|
1733
|
+
const rootPrefix = `${config.root}/`;
|
|
1734
|
+
const command = `for p in ${[...expected.keys()].map((path) => shellQuote(rootPrefix + path)).join(" ")}; do r="$(cd -P "\${p%/*}" 2>/dev/null && printf '%s/%s\\n' "$(pwd -P)" "\${p##*/}")" && [ "$r" = "$p" ] && [ ! -L "$p" ] && [ -f "$p" ] && sha256sum -z -- "$p"; done 2>/dev/null; exit 0`;
|
|
1735
|
+
let stdout;
|
|
1736
|
+
try {
|
|
1737
|
+
stdout = (await box.exec(command)).stdout;
|
|
1738
|
+
} catch {
|
|
1739
|
+
return present;
|
|
1740
|
+
}
|
|
1741
|
+
for (const record of stdout.split("\0").filter(Boolean)) {
|
|
1742
|
+
const parsed = /^([a-f0-9]{64}) [ *]([\s\S]+)$/.exec(record);
|
|
1743
|
+
if (!parsed) continue;
|
|
1744
|
+
const path = parsed[2].startsWith(rootPrefix) ? parsed[2].slice(rootPrefix.length) : parsed[2];
|
|
1745
|
+
if (expected.get(path) === parsed[1]) present.add(path);
|
|
1746
|
+
}
|
|
1747
|
+
return present;
|
|
1678
1748
|
}
|
|
1679
1749
|
async function readSnapshot(config, workspaceId, entry, skippedPaths) {
|
|
1680
1750
|
try {
|
|
@@ -1696,11 +1766,12 @@ async function readSnapshot(config, workspaceId, entry, skippedPaths) {
|
|
|
1696
1766
|
}
|
|
1697
1767
|
}
|
|
1698
1768
|
async function writeTextSnapshots(config, box, texts) {
|
|
1769
|
+
const skipped = [];
|
|
1699
1770
|
let request = [];
|
|
1700
1771
|
let requestBytes = 0;
|
|
1701
1772
|
const flush = async () => {
|
|
1702
1773
|
if (request.length === 0) return;
|
|
1703
|
-
await box.fs.writeMany(request);
|
|
1774
|
+
await (config.writeText ? config.writeText(box, request) : box.fs.writeMany(request));
|
|
1704
1775
|
request = [];
|
|
1705
1776
|
requestBytes = 0;
|
|
1706
1777
|
};
|
|
@@ -1708,12 +1779,8 @@ async function writeTextSnapshots(config, box, texts) {
|
|
|
1708
1779
|
const path = boxPath(config, text.path);
|
|
1709
1780
|
const bytes = encoder.encode(JSON.stringify({ path, content: text.content })).byteLength;
|
|
1710
1781
|
if (bytes > VAULT_TEXT_WRITE_MAX_JSON_BYTES) {
|
|
1711
|
-
const
|
|
1712
|
-
|
|
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
|
-
}
|
|
1782
|
+
const skip = await uploadFile(config, box, text.path, encoder.encode(text.content));
|
|
1783
|
+
if (skip) skipped.push(skip);
|
|
1717
1784
|
continue;
|
|
1718
1785
|
}
|
|
1719
1786
|
if (requestBytes + bytes > VAULT_TEXT_WRITE_MAX_JSON_BYTES) await flush();
|
|
@@ -1721,21 +1788,31 @@ async function writeTextSnapshots(config, box, texts) {
|
|
|
1721
1788
|
requestBytes += bytes;
|
|
1722
1789
|
}
|
|
1723
1790
|
await flush();
|
|
1791
|
+
return skipped;
|
|
1724
1792
|
}
|
|
1725
|
-
async function
|
|
1726
|
-
|
|
1727
|
-
|
|
1728
|
-
|
|
1729
|
-
|
|
1730
|
-
|
|
1731
|
-
|
|
1732
|
-
|
|
1733
|
-
}
|
|
1734
|
-
} catch (error) {
|
|
1735
|
-
throw new VaultHydrationError(`Failed to upload binary Vault file ${binary.path} for ${workspaceId}`, error);
|
|
1793
|
+
async function uploadFile(config, box, vaultPath, data) {
|
|
1794
|
+
let result;
|
|
1795
|
+
try {
|
|
1796
|
+
result = await box.fs.uploadData(boxPath(config, vaultPath), data);
|
|
1797
|
+
} catch (error) {
|
|
1798
|
+
const { code, status } = error ?? {};
|
|
1799
|
+
if (config.skipOversizeFiles && error instanceof Error && (code === "PAYLOAD_TOO_LARGE" || code === "FILE_TOO_LARGE" || status === 413)) {
|
|
1800
|
+
console.warn("[vault] file is too large to hydrate; the agent reads it through the Vault API", { path: vaultPath, detail: error.message });
|
|
1801
|
+
return { path: vaultPath, reason: "too_large", detail: error.message };
|
|
1736
1802
|
}
|
|
1737
|
-
|
|
1738
|
-
|
|
1803
|
+
throw new VaultHydrationError(`Failed to upload Vault file ${vaultPath}`, error);
|
|
1804
|
+
}
|
|
1805
|
+
const size = result?.size;
|
|
1806
|
+
if (typeof size === "number" && size !== data.byteLength) {
|
|
1807
|
+
throw new VaultHydrationError(`Failed to upload Vault file ${vaultPath}`, new Error(`expected ${data.byteLength} uploaded bytes, received ${size}`));
|
|
1808
|
+
}
|
|
1809
|
+
return null;
|
|
1810
|
+
}
|
|
1811
|
+
async function writeBinaries(config, box, workspaceId, snapshots) {
|
|
1812
|
+
const skips = await mapWithConcurrency(snapshots, BINARY_HYDRATION_CONCURRENCY, (binary) => uploadFile(config, box, binary.path, base64ToBytes(binary.payload)));
|
|
1813
|
+
const skipped = skips.filter((skip) => skip !== null);
|
|
1814
|
+
const binaries = snapshots.filter((binary) => !skipped.some((skip) => skip.path === binary.path));
|
|
1815
|
+
if (binaries.length === 0) return skipped;
|
|
1739
1816
|
const verification = await box.exec(`sha256sum -z -- ${binaries.map((binary) => shellQuote(boxPath(config, binary.path))).join(" ")}`);
|
|
1740
1817
|
if (verification.exitCode !== 0) {
|
|
1741
1818
|
throw new VaultHydrationError(
|
|
@@ -1761,6 +1838,7 @@ async function writeBinaries(config, box, workspaceId, binaries) {
|
|
|
1761
1838
|
);
|
|
1762
1839
|
}
|
|
1763
1840
|
}
|
|
1841
|
+
return skipped;
|
|
1764
1842
|
}
|
|
1765
1843
|
function sameBaselineFiles(previous, next) {
|
|
1766
1844
|
const paths = Object.keys(next);
|
|
@@ -1841,6 +1919,7 @@ function blockedSandboxVaultSync(root, error, reason, totalPaths = 0) {
|
|
|
1841
1919
|
pathsMissingFromBox: [],
|
|
1842
1920
|
operationId: null,
|
|
1843
1921
|
changes: [],
|
|
1922
|
+
convergedChanges: [],
|
|
1844
1923
|
appliedPaths: [],
|
|
1845
1924
|
conflicts: [],
|
|
1846
1925
|
progress: {
|
|
@@ -1853,8 +1932,13 @@ function blockedSandboxVaultSync(root, error, reason, totalPaths = 0) {
|
|
|
1853
1932
|
blockedReason: reason
|
|
1854
1933
|
};
|
|
1855
1934
|
}
|
|
1935
|
+
function excluded(config, path) {
|
|
1936
|
+
return (config.excludeDirs ?? []).some((dir) => path === dir || path.startsWith(`${dir}/`));
|
|
1937
|
+
}
|
|
1856
1938
|
async function readSandboxVaultManifest(config, box, workspaceId, sessionId) {
|
|
1857
|
-
const
|
|
1939
|
+
const excludedDirs = (config.excludeDirs ?? []).map((dir) => ` -o -path ${shellQuote(`${config.root}/${dir}`)}`).join("");
|
|
1940
|
+
const pruned = excludedDirs ? `\\( -name '.*'${excludedDirs} \\)` : `-name '.*'`;
|
|
1941
|
+
const command = `if [ ! -d ${shellQuote(config.root)} ]; then printf '%s' 'Sandbox Vault directory is missing' >&2; exit 42; fi; find ${shellQuote(config.root)} -mindepth 1 ${pruned} -prune -o -type f -exec sha256sum -z -- {} +`;
|
|
1858
1942
|
let listing;
|
|
1859
1943
|
try {
|
|
1860
1944
|
listing = sessionId ? await box.exec(command, { sessionId }) : await box.exec(command);
|
|
@@ -1969,28 +2053,22 @@ async function reconcileSandboxVault(config, box, workspaceId, options = {}) {
|
|
|
1969
2053
|
workspaceId,
|
|
1970
2054
|
box.id,
|
|
1971
2055
|
incarnation.value.id,
|
|
1972
|
-
options.sessionId ?? "sandbox-root"
|
|
2056
|
+
options.sessionId ?? "sandbox-root",
|
|
2057
|
+
options.canonicalPathsOnly ? "canonical" : "all"
|
|
1973
2058
|
].join(":");
|
|
1974
2059
|
const existingTask = vaultReconciliationTasks.get(reconciliationKey);
|
|
1975
2060
|
if (existingTask) return existingTask;
|
|
1976
|
-
const task =
|
|
1977
|
-
config,
|
|
1978
|
-
|
|
1979
|
-
|
|
1980
|
-
|
|
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
|
-
}));
|
|
2061
|
+
const task = (async () => {
|
|
2062
|
+
const { supersededPaths = 0, ...result } = await reconcileSandboxVaultFilesToKv(config, box, workspaceId, box.id, incarnation.value, options);
|
|
2063
|
+
if (!result.succeeded || !result.complete || result.conflicts.length > 0 || result.refusedDeletionPaths.length > 0 || result.binaryDriftPaths.length > 0 || supersededPaths > 0) {
|
|
2064
|
+
options.onPhase?.("box-sync-invalidate");
|
|
2065
|
+
await config.vault.invalidateVaultBoxSync(workspaceId);
|
|
1992
2066
|
}
|
|
1993
2067
|
return result;
|
|
2068
|
+
})();
|
|
2069
|
+
vaultReconciliationTasks.set(reconciliationKey, task);
|
|
2070
|
+
try {
|
|
2071
|
+
return await task;
|
|
1994
2072
|
} finally {
|
|
1995
2073
|
if (vaultReconciliationTasks.get(reconciliationKey) === task) {
|
|
1996
2074
|
vaultReconciliationTasks.delete(reconciliationKey);
|
|
@@ -1998,76 +2076,96 @@ async function reconcileSandboxVault(config, box, workspaceId, options = {}) {
|
|
|
1998
2076
|
}
|
|
1999
2077
|
}
|
|
2000
2078
|
async function reconcileSandboxVaultFilesToKv(config, box, workspaceId, sandboxId, incarnation, options) {
|
|
2079
|
+
const scanStartedAt = Date.now();
|
|
2080
|
+
options.onPhase?.("baseline-read");
|
|
2001
2081
|
const storedBaseline = await config.vault.readVaultSandboxBaseline(workspaceId, sandboxId);
|
|
2002
2082
|
const { baseline, legacy } = normalizeVaultSandboxBaseline(storedBaseline);
|
|
2083
|
+
const emptyProgress = { phase: "reconcile", processedPaths: 0, totalPaths: 0, remainingPaths: 0, committedGroups: 0 };
|
|
2084
|
+
const nothing = {
|
|
2085
|
+
rejectedPaths: [],
|
|
2086
|
+
binaryDriftPaths: [],
|
|
2087
|
+
changes: [],
|
|
2088
|
+
convergedChanges: [],
|
|
2089
|
+
appliedPaths: [],
|
|
2090
|
+
conflicts: [],
|
|
2091
|
+
operationId: options.operationId ?? null
|
|
2092
|
+
};
|
|
2003
2093
|
if (!baseline || baseline.schema !== VAULT_SANDBOX_BASELINE_SCHEMA || baseline.filesystemIncarnationId !== incarnation.id) {
|
|
2004
2094
|
return {
|
|
2095
|
+
...nothing,
|
|
2005
2096
|
succeeded: true,
|
|
2006
2097
|
complete: true,
|
|
2007
|
-
rejectedPaths: [],
|
|
2008
|
-
binaryDriftPaths: [],
|
|
2009
2098
|
refusedDeletionPaths: [],
|
|
2010
2099
|
pathsMissingFromBox: [],
|
|
2011
|
-
|
|
2012
|
-
changes: [],
|
|
2013
|
-
appliedPaths: [],
|
|
2014
|
-
conflicts: [],
|
|
2015
|
-
progress: {
|
|
2016
|
-
phase: "reconcile",
|
|
2017
|
-
processedPaths: 0,
|
|
2018
|
-
totalPaths: 0,
|
|
2019
|
-
remainingPaths: 0,
|
|
2020
|
-
committedGroups: 0
|
|
2021
|
-
},
|
|
2100
|
+
progress: emptyProgress,
|
|
2022
2101
|
baselineResetReason: baseline ? "filesystem_incarnation_changed" : "baseline_missing"
|
|
2023
2102
|
};
|
|
2024
2103
|
}
|
|
2104
|
+
options.onPhase?.("manifest");
|
|
2025
2105
|
const manifest = await readSandboxVaultManifest(config, box, workspaceId, options.sessionId);
|
|
2026
2106
|
if (!manifest.directoryPresent) {
|
|
2027
|
-
return blockedSandboxVaultSync(
|
|
2028
|
-
|
|
2029
|
-
|
|
2030
|
-
|
|
2031
|
-
|
|
2032
|
-
|
|
2107
|
+
return blockedSandboxVaultSync(config.root, manifest.rejectedPaths[0], "vault_directory_missing", Object.keys(baseline.files).length);
|
|
2108
|
+
}
|
|
2109
|
+
if (options.canonicalPathsOnly) {
|
|
2110
|
+
options.onPhase?.("canonical-heads");
|
|
2111
|
+
const unknown = await mapWithConcurrency([...manifest.entries.keys()].filter((path) => !baseline.files[path]), VAULT_IO_CONCURRENCY2, async (path) => {
|
|
2112
|
+
const head = await config.vault.readVaultFileState(workspaceId, path);
|
|
2113
|
+
return head.deleted || !head.revision;
|
|
2114
|
+
});
|
|
2115
|
+
if (unknown.some(Boolean)) throw new Error("The retained Vault mirror contains unclassified local files; preserve them for Vault recovery");
|
|
2033
2116
|
}
|
|
2034
|
-
const liveBaselinePaths = Object.entries(baseline.files).filter(([, file]) => !file.deleted).map(([path]) => path);
|
|
2117
|
+
const liveBaselinePaths = Object.entries(baseline.files).filter(([path, file]) => !file.deleted && !excluded(config, path)).map(([path]) => path);
|
|
2035
2118
|
const wouldDeletePaths = liveBaselinePaths.filter((path) => !manifest.entries.has(path)).sort();
|
|
2036
|
-
const
|
|
2037
|
-
|
|
2038
|
-
|
|
2039
|
-
|
|
2040
|
-
|
|
2041
|
-
|
|
2042
|
-
|
|
2043
|
-
|
|
2044
|
-
|
|
2045
|
-
|
|
2046
|
-
|
|
2047
|
-
|
|
2048
|
-
|
|
2119
|
+
const deletes = config.deletes ?? "rename-only";
|
|
2120
|
+
let refusedDeletionPaths;
|
|
2121
|
+
let deletionRefusal = "sandbox_deletion_denied";
|
|
2122
|
+
if (deletes === "rename-only") {
|
|
2123
|
+
const renameTargets = /* @__PURE__ */ new Map();
|
|
2124
|
+
for (const [path, entry] of manifest.entries) {
|
|
2125
|
+
const base = baseline.files[path];
|
|
2126
|
+
if (base && !base.deleted) continue;
|
|
2127
|
+
renameTargets.set(entry.contentHash, (renameTargets.get(entry.contentHash) ?? 0) + 1);
|
|
2128
|
+
}
|
|
2129
|
+
refusedDeletionPaths = wouldDeletePaths.filter((path) => {
|
|
2130
|
+
const hash = baseline.files[path]?.contentHash;
|
|
2131
|
+
const available = hash ? renameTargets.get(hash) ?? 0 : 0;
|
|
2132
|
+
if (available === 0) return true;
|
|
2133
|
+
renameTargets.set(hash, available - 1);
|
|
2134
|
+
return false;
|
|
2135
|
+
});
|
|
2136
|
+
} else {
|
|
2137
|
+
const assessment = assessVaultDeletionBatch({
|
|
2138
|
+
baselinePaths: liveBaselinePaths,
|
|
2139
|
+
proposedDeletions: wouldDeletePaths,
|
|
2140
|
+
manifestEmpty: manifest.entries.size === 0,
|
|
2141
|
+
policy: deletes.guard
|
|
2142
|
+
});
|
|
2143
|
+
refusedDeletionPaths = assessment.allowed ? [] : assessment.wouldDeletePaths;
|
|
2144
|
+
deletionRefusal = "mass_deletion_guard";
|
|
2145
|
+
if (!assessment.allowed) {
|
|
2146
|
+
console.error("[vault] refusing the deletes of a sandbox Vault scan that would remove most of the baseline", {
|
|
2147
|
+
workspaceId,
|
|
2148
|
+
sandboxId,
|
|
2149
|
+
reason: assessment.reason,
|
|
2150
|
+
wouldDelete: assessment.wouldDelete,
|
|
2151
|
+
baselineLive: assessment.baselineLive
|
|
2152
|
+
});
|
|
2153
|
+
}
|
|
2154
|
+
}
|
|
2155
|
+
const refusal = () => refusedDeletionPaths.length > 0 ? { blockedReason: deletionRefusal } : {};
|
|
2049
2156
|
if (baseline.progress?.operation === "hydrate") {
|
|
2050
2157
|
return {
|
|
2158
|
+
...nothing,
|
|
2051
2159
|
succeeded: manifest.rejectedPaths.length === 0,
|
|
2052
2160
|
complete: true,
|
|
2053
2161
|
rejectedPaths: manifest.rejectedPaths,
|
|
2054
|
-
binaryDriftPaths: [],
|
|
2055
2162
|
refusedDeletionPaths,
|
|
2056
2163
|
pathsMissingFromBox: wouldDeletePaths,
|
|
2057
|
-
|
|
2058
|
-
|
|
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" } : {}
|
|
2164
|
+
progress: { ...emptyProgress, totalPaths: manifest.entries.size },
|
|
2165
|
+
...refusal()
|
|
2069
2166
|
};
|
|
2070
2167
|
}
|
|
2168
|
+
options.onStage?.("reconcile_scan", scanStartedAt, { sandboxFiles: manifest.entries.size, baselineFiles: Object.keys(baseline.files).length });
|
|
2071
2169
|
const rejectedPaths = [...manifest.rejectedPaths];
|
|
2072
2170
|
const binaryDriftPaths = [];
|
|
2073
2171
|
const candidatePaths = /* @__PURE__ */ new Set();
|
|
@@ -2077,9 +2175,7 @@ async function reconcileSandboxVaultFilesToKv(config, box, workspaceId, sandboxI
|
|
|
2077
2175
|
if (entry.contentHash !== base.onDiskHash) binaryDriftPaths.push(path);
|
|
2078
2176
|
continue;
|
|
2079
2177
|
}
|
|
2080
|
-
if (!base || base.deleted || base.contentHash !== entry.contentHash)
|
|
2081
|
-
candidatePaths.add(path);
|
|
2082
|
-
}
|
|
2178
|
+
if (!base || base.deleted || base.contentHash !== entry.contentHash) candidatePaths.add(path);
|
|
2083
2179
|
}
|
|
2084
2180
|
for (const path of wouldDeletePaths) {
|
|
2085
2181
|
if (!refusedDeletionPaths.includes(path)) candidatePaths.add(path);
|
|
@@ -2090,12 +2186,8 @@ async function reconcileSandboxVaultFilesToKv(config, box, workspaceId, sandboxI
|
|
|
2090
2186
|
(path) => {
|
|
2091
2187
|
const base = baseline.files[path];
|
|
2092
2188
|
const entry = manifest.entries.get(path);
|
|
2093
|
-
if ((!base || base.deleted) && entry) {
|
|
2094
|
-
|
|
2095
|
-
}
|
|
2096
|
-
if (base && !base.deleted && !entry && base.contentHash) {
|
|
2097
|
-
return { path, kind: "delete", contentHash: base.contentHash };
|
|
2098
|
-
}
|
|
2189
|
+
if ((!base || base.deleted) && entry) return { path, kind: "put", contentHash: entry.contentHash };
|
|
2190
|
+
if (base && !base.deleted && !entry && base.contentHash) return { path, kind: "delete", contentHash: base.contentHash };
|
|
2099
2191
|
return { path, kind: null, contentHash: null };
|
|
2100
2192
|
},
|
|
2101
2193
|
[...candidatePathList].sort()
|
|
@@ -2103,90 +2195,45 @@ async function reconcileSandboxVaultFilesToKv(config, box, workspaceId, sandboxI
|
|
|
2103
2195
|
const candidateBudget = VAULT_SYNC_GROUP_SIZE * VAULT_SYNC_MAX_GROUPS;
|
|
2104
2196
|
const selectedCandidatePaths = [];
|
|
2105
2197
|
for (const unit of candidateUnits) {
|
|
2106
|
-
if (selectedCandidatePaths.length > 0 && selectedCandidatePaths.length + unit.length > candidateBudget)
|
|
2107
|
-
break;
|
|
2108
|
-
}
|
|
2198
|
+
if (selectedCandidatePaths.length > 0 && selectedCandidatePaths.length + unit.length > candidateBudget) break;
|
|
2109
2199
|
selectedCandidatePaths.push(...unit);
|
|
2110
2200
|
}
|
|
2201
|
+
options.onPhase?.("file-reads");
|
|
2111
2202
|
const snapshots = /* @__PURE__ */ new Map();
|
|
2112
|
-
await mapWithConcurrency(
|
|
2113
|
-
|
|
2114
|
-
|
|
2115
|
-
|
|
2116
|
-
|
|
2117
|
-
|
|
2118
|
-
|
|
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
|
-
}
|
|
2203
|
+
await mapWithConcurrency(selectedCandidatePaths.filter((path) => manifest.entries.has(path)), VAULT_IO_CONCURRENCY2, async (path) => {
|
|
2204
|
+
const entry = manifest.entries.get(path);
|
|
2205
|
+
if (!entry) throw new Error(`Sandbox Vault manifest entry is missing for ${path}`);
|
|
2206
|
+
try {
|
|
2207
|
+
snapshots.set(path, await readChangedSandboxFile(config, box, entry, options.sessionId));
|
|
2208
|
+
} catch (error) {
|
|
2209
|
+
rejectedPaths.push(sandboxVaultRejection(path, "read", "sandbox.read_failed", error));
|
|
2128
2210
|
}
|
|
2129
|
-
);
|
|
2211
|
+
});
|
|
2130
2212
|
const readableCandidates = selectedCandidatePaths.filter((path) => !manifest.entries.has(path) || snapshots.has(path));
|
|
2131
|
-
|
|
2132
|
-
|
|
2133
|
-
|
|
2134
|
-
|
|
2135
|
-
|
|
2136
|
-
|
|
2137
|
-
|
|
2138
|
-
const
|
|
2139
|
-
|
|
2140
|
-
|
|
2141
|
-
|
|
2142
|
-
|
|
2143
|
-
if (
|
|
2144
|
-
|
|
2213
|
+
options.onPhase?.("head-reads");
|
|
2214
|
+
const currentStates = new Map(await mapWithConcurrency(readableCandidates, VAULT_IO_CONCURRENCY2, async (path) => [path, await config.vault.readVaultFileState(workspaceId, path)]));
|
|
2215
|
+
options.onPhase?.("plan-changes");
|
|
2216
|
+
const plan = await planSandboxVaultChanges(config, workspaceId, baseline, readableCandidates, snapshots, currentStates);
|
|
2217
|
+
let authorizedChanges = plan.changes;
|
|
2218
|
+
if (deletes === "rename-only") {
|
|
2219
|
+
const pendingPutsByHash = /* @__PURE__ */ new Map();
|
|
2220
|
+
for (const change of plan.changes) {
|
|
2221
|
+
if (change.kind !== "put" || !change.contentHash) continue;
|
|
2222
|
+
pendingPutsByHash.set(change.contentHash, (pendingPutsByHash.get(change.contentHash) ?? 0) + 1);
|
|
2223
|
+
}
|
|
2224
|
+
authorizedChanges = plan.changes.filter((change) => {
|
|
2225
|
+
if (change.kind !== "delete") return true;
|
|
2226
|
+
const hash = change.contentHash;
|
|
2227
|
+
const available = hash ? pendingPutsByHash.get(hash) ?? 0 : 0;
|
|
2228
|
+
if (!hash || available === 0) {
|
|
2229
|
+
refusedDeletionPaths.push(change.path);
|
|
2230
|
+
return false;
|
|
2145
2231
|
}
|
|
2146
|
-
|
|
2147
|
-
|
|
2148
|
-
|
|
2149
|
-
|
|
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);
|
|
2232
|
+
pendingPutsByHash.set(hash, available - 1);
|
|
2233
|
+
return true;
|
|
2234
|
+
});
|
|
2235
|
+
refusedDeletionPaths.sort();
|
|
2177
2236
|
}
|
|
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
2237
|
const groups = groupSandboxVaultChanges(authorizedChanges, VAULT_SYNC_GROUP_SIZE);
|
|
2191
2238
|
const groupsToCommit = groups.slice(0, VAULT_SYNC_MAX_GROUPS);
|
|
2192
2239
|
const stagedChanges = groupsToCommit.flat().map((change) => ({
|
|
@@ -2197,9 +2244,10 @@ async function reconcileSandboxVaultFilesToKv(config, box, workspaceId, sandboxI
|
|
|
2197
2244
|
}));
|
|
2198
2245
|
const stagedFingerprint = await sha256Hex(JSON.stringify(stagedChanges));
|
|
2199
2246
|
const operationId = options.operationId ?? (stagedChanges.length > 0 ? `sandbox:${sandboxId}:${stagedFingerprint}` : null);
|
|
2200
|
-
|
|
2201
|
-
|
|
2202
|
-
|
|
2247
|
+
options.onPhase?.("staged");
|
|
2248
|
+
if (operationId && options.onStaged) await options.onStaged({ operationId, changes: stagedChanges });
|
|
2249
|
+
options.onPhase?.("mutate");
|
|
2250
|
+
const importStartedAt = Date.now();
|
|
2203
2251
|
const appliedPaths = [];
|
|
2204
2252
|
const appliedFiles = [];
|
|
2205
2253
|
const conflicts = [];
|
|
@@ -2214,7 +2262,7 @@ async function reconcileSandboxVaultFilesToKv(config, box, workspaceId, sandboxI
|
|
|
2214
2262
|
const result = await config.vault.commitVaultMutation({
|
|
2215
2263
|
workspaceId,
|
|
2216
2264
|
operationId: options.operationId ? `${options.operationId}:${fingerprint}` : `sandbox:${sandboxId}:${fingerprint}`,
|
|
2217
|
-
writer: { kind: "sandbox", id: sandboxId },
|
|
2265
|
+
writer: { kind: "sandbox", id: sandboxId, ...options.provenance ? { provenance: options.provenance } : {} },
|
|
2218
2266
|
changes: group.map(({ contentHash: _contentHash, ...change }) => change)
|
|
2219
2267
|
});
|
|
2220
2268
|
if (result.status === "applied") {
|
|
@@ -2224,44 +2272,21 @@ async function reconcileSandboxVaultFilesToKv(config, box, workspaceId, sandboxI
|
|
|
2224
2272
|
conflicts.push(...result.conflicts);
|
|
2225
2273
|
}
|
|
2226
2274
|
} 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
|
-
}
|
|
2275
|
+
for (const change of group) rejectedPaths.push(sandboxVaultRejection(change.path, "commit", "vault.commit_failed", error));
|
|
2235
2276
|
}
|
|
2236
2277
|
}
|
|
2237
|
-
|
|
2238
|
-
|
|
2239
|
-
|
|
2240
|
-
|
|
2241
|
-
|
|
2242
|
-
for (const file of appliedFiles) {
|
|
2278
|
+
const recorded = [...appliedFiles, ...plan.convergedFiles, ...plan.supersededFiles];
|
|
2279
|
+
const untrustedManifest = deletes !== "rename-only" && refusedDeletionPaths.length > 0;
|
|
2280
|
+
if (!untrustedManifest && (recorded.length > 0 || binaryDriftPaths.length > 0)) {
|
|
2281
|
+
const baselineFiles = legacy ? Object.fromEntries(Object.entries(baseline.files).map(([path, file]) => [path, file.deleted ? file : { ...file, revision: null }])) : { ...baseline.files };
|
|
2282
|
+
for (const file of recorded) {
|
|
2243
2283
|
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
|
-
};
|
|
2284
|
+
baselineFiles[file.path] = snapshot?.binary ? { revision: file.revision, contentHash: file.contentHash, deleted: file.deleted, binary: true, onDiskHash: snapshot.onDiskHash ?? null } : { revision: file.revision, contentHash: file.contentHash, deleted: file.deleted };
|
|
2255
2285
|
}
|
|
2256
2286
|
for (const path of binaryDriftPaths) {
|
|
2257
|
-
baselineFiles[path] = {
|
|
2258
|
-
revision: null,
|
|
2259
|
-
contentHash: null,
|
|
2260
|
-
deleted: false,
|
|
2261
|
-
binary: true,
|
|
2262
|
-
onDiskHash: null
|
|
2263
|
-
};
|
|
2287
|
+
baselineFiles[path] = { revision: null, contentHash: null, deleted: false, binary: true, onDiskHash: null };
|
|
2264
2288
|
}
|
|
2289
|
+
options.onPhase?.("baseline-write");
|
|
2265
2290
|
await config.vault.writeVaultSandboxBaseline({
|
|
2266
2291
|
schema: VAULT_SANDBOX_BASELINE_SCHEMA,
|
|
2267
2292
|
workspaceId,
|
|
@@ -2273,6 +2298,13 @@ async function reconcileSandboxVaultFilesToKv(config, box, workspaceId, sandboxI
|
|
|
2273
2298
|
}
|
|
2274
2299
|
const processedPaths = selectedCandidatePaths.length;
|
|
2275
2300
|
const remainingPaths = candidatePaths.size - processedPaths;
|
|
2301
|
+
options.onStage?.("reconcile_import", importStartedAt, {
|
|
2302
|
+
candidates: plan.changes.length,
|
|
2303
|
+
imported: appliedPaths.length,
|
|
2304
|
+
converged: plan.convergedChanges.length,
|
|
2305
|
+
failed: rejectedPaths.length,
|
|
2306
|
+
conflicts: conflicts.length
|
|
2307
|
+
});
|
|
2276
2308
|
return {
|
|
2277
2309
|
succeeded: rejectedPaths.length === 0,
|
|
2278
2310
|
complete: remainingPaths === 0,
|
|
@@ -2280,11 +2312,13 @@ async function reconcileSandboxVaultFilesToKv(config, box, workspaceId, sandboxI
|
|
|
2280
2312
|
binaryDriftPaths,
|
|
2281
2313
|
refusedDeletionPaths,
|
|
2282
2314
|
pathsMissingFromBox: wouldDeletePaths,
|
|
2283
|
-
...
|
|
2315
|
+
...refusal(),
|
|
2284
2316
|
operationId,
|
|
2285
2317
|
changes: stagedChanges,
|
|
2318
|
+
convergedChanges: plan.convergedChanges,
|
|
2286
2319
|
appliedPaths,
|
|
2287
2320
|
conflicts,
|
|
2321
|
+
supersededPaths: plan.supersededFiles.length,
|
|
2288
2322
|
progress: {
|
|
2289
2323
|
phase: "reconcile",
|
|
2290
2324
|
processedPaths,
|
|
@@ -2294,6 +2328,43 @@ async function reconcileSandboxVaultFilesToKv(config, box, workspaceId, sandboxI
|
|
|
2294
2328
|
}
|
|
2295
2329
|
};
|
|
2296
2330
|
}
|
|
2331
|
+
async function planSandboxVaultChanges(config, workspaceId, baseline, paths, snapshots, currentStates) {
|
|
2332
|
+
const changes = [];
|
|
2333
|
+
const convergedChanges = [];
|
|
2334
|
+
const convergedFiles = [];
|
|
2335
|
+
const supersededFiles = [];
|
|
2336
|
+
const superseded = async (path, current, contentHash) => {
|
|
2337
|
+
if (!current.revision) return false;
|
|
2338
|
+
const revision = await config.vault.findVaultRevisionWithContent(workspaceId, path, current.revision, contentHash);
|
|
2339
|
+
if (revision) supersededFiles.push({ path, revision, contentHash, deleted: false });
|
|
2340
|
+
return Boolean(revision);
|
|
2341
|
+
};
|
|
2342
|
+
for (const path of [...paths].sort()) {
|
|
2343
|
+
const snapshot = snapshots.get(path);
|
|
2344
|
+
const current = currentStates.get(path);
|
|
2345
|
+
if (!current) throw new Error(`Vault state missing for ${path}`);
|
|
2346
|
+
const base = baseline.files[path];
|
|
2347
|
+
if (snapshot) {
|
|
2348
|
+
if (base && !base.deleted && base.contentHash === snapshot.contentHash) continue;
|
|
2349
|
+
const expectedRevision = base ? base.revision : null;
|
|
2350
|
+
if (!current.deleted && current.revision && current.contentHash === snapshot.contentHash) {
|
|
2351
|
+
convergedChanges.push({ path, kind: "put", contentHash: snapshot.contentHash, expectedRevision });
|
|
2352
|
+
convergedFiles.push({ path, revision: current.revision, contentHash: current.contentHash, deleted: false });
|
|
2353
|
+
continue;
|
|
2354
|
+
}
|
|
2355
|
+
if ((!base || current.revision !== base.revision) && await superseded(path, current, snapshot.contentHash)) continue;
|
|
2356
|
+
changes.push({ path, kind: "put", content: snapshot.content, contentHash: snapshot.contentHash, expectedRevision });
|
|
2357
|
+
} else if (base && !base.deleted) {
|
|
2358
|
+
if (current.deleted) {
|
|
2359
|
+
convergedChanges.push({ path, kind: "delete", contentHash: null, expectedRevision: base.revision });
|
|
2360
|
+
convergedFiles.push({ path, revision: current.revision, contentHash: null, deleted: true });
|
|
2361
|
+
continue;
|
|
2362
|
+
}
|
|
2363
|
+
changes.push({ path, kind: "delete", contentHash: base.contentHash, expectedRevision: base.revision });
|
|
2364
|
+
}
|
|
2365
|
+
}
|
|
2366
|
+
return { changes, convergedChanges, convergedFiles, supersededFiles };
|
|
2367
|
+
}
|
|
2297
2368
|
async function probeSandboxVaultLoss(config, box, workspaceId) {
|
|
2298
2369
|
if (!box.id) return [];
|
|
2299
2370
|
const incarnation = readReadySandboxFilesystemIncarnation(box);
|