@narumitw/pi-sync 0.60.5 → 0.62.0
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/CHANGELOG.md +25 -0
- package/README.md +32 -23
- package/dist/chunks/{chunk-F64YWRXG.ts → chunk-3E3TR3JE.ts} +4 -4
- package/dist/chunks/{chunk-YVH5VJR4.ts → chunk-6PFZUEUG.ts} +138 -61
- package/dist/chunks/chunk-6PFZUEUG.ts.map +7 -0
- package/dist/chunks/{chunk-ZIL6PW5L.ts → chunk-6YKOHBSC.ts} +2 -2
- package/dist/chunks/{chunk-OA4NZLSL.ts → chunk-APZVJAEW.ts} +4 -3
- package/dist/chunks/{chunk-KPWZNMKM.ts → chunk-ECYCNZOE.ts} +1 -5
- package/dist/chunks/chunk-ECYCNZOE.ts.map +7 -0
- package/dist/chunks/chunk-GELYN4UC.ts +14 -0
- package/dist/chunks/chunk-GELYN4UC.ts.map +7 -0
- package/dist/chunks/{chunk-V2QIMC3U.ts → chunk-GSCZ4VXV.ts} +13 -8
- package/dist/chunks/chunk-GSCZ4VXV.ts.map +7 -0
- package/dist/chunks/{chunk-6W2SNZ5P.ts → chunk-IBXEM35O.ts} +8 -3
- package/dist/chunks/{chunk-6W2SNZ5P.ts.map → chunk-IBXEM35O.ts.map} +3 -3
- package/dist/chunks/{chunk-6KIWZ4AY.ts → chunk-INECVQNR.ts} +13 -12
- package/dist/chunks/chunk-INECVQNR.ts.map +7 -0
- package/dist/chunks/chunk-JREGWM2B.ts +139 -0
- package/dist/chunks/chunk-JREGWM2B.ts.map +7 -0
- package/dist/chunks/{chunk-277VHM2P.ts → chunk-MNYRU5U5.ts} +5 -2
- package/dist/chunks/{chunk-277VHM2P.ts.map → chunk-MNYRU5U5.ts.map} +2 -2
- package/dist/chunks/chunk-NHFGAZEC.ts +277 -0
- package/dist/chunks/chunk-NHFGAZEC.ts.map +7 -0
- package/dist/chunks/{chunk-EMRFVLZM.ts → chunk-NXWHPVXR.ts} +2 -2
- package/dist/chunks/{chunk-SMVKMYLK.ts → chunk-P4GI7LTT.ts} +2 -2
- package/dist/chunks/{chunk-MCUZFUHY.ts → chunk-PWESMTH6.ts} +10 -10
- package/dist/chunks/{chunk-V4AMI3VL.ts → chunk-R3SAMXB6.ts} +10 -2
- package/dist/chunks/chunk-R3SAMXB6.ts.map +7 -0
- package/dist/chunks/{chunk-RNIEVDTR.ts → chunk-T3LABNHF.ts} +2 -2
- package/dist/chunks/{chunk-S3R3W3KF.ts → chunk-UMPA5S7X.ts} +2 -2
- package/dist/chunks/{chunk-WP2M67XZ.ts → chunk-WBIOSK76.ts} +2 -2
- package/dist/chunks/{chunk-LAXTCSX4.ts → chunk-ZB3GNX3P.ts} +124 -139
- package/dist/chunks/chunk-ZB3GNX3P.ts.map +7 -0
- package/dist/chunks/{chunk-HEB6YT64.ts → chunk-ZO52JM4H.ts} +2 -2
- package/dist/chunks/{command-handler-TNPWFIPG.ts → command-handler-6ATREPG7.ts} +44 -40
- package/dist/chunks/command-handler-6ATREPG7.ts.map +7 -0
- package/dist/chunks/{conflict-review-ETCSYJTH.ts → conflict-review-MAOGTIAI.ts} +4 -4
- package/dist/chunks/{continuous-record-3XDXDOMV.ts → continuous-record-3SKJ6I3V.ts} +2 -2
- package/dist/chunks/{continuous-storage-HLKCHF6Z.ts → continuous-storage-3EU6I7Z3.ts} +2 -2
- package/dist/chunks/{file-selection-DD6X4J2K.ts → file-selection-QR2KZVGD.ts} +28 -15
- package/dist/chunks/file-selection-QR2KZVGD.ts.map +7 -0
- package/dist/chunks/manager-result-dispatcher-MC6EQOQY.ts +19 -0
- package/dist/chunks/{manager-ui-LFONXWQN.ts → manager-ui-AOFUNXAJ.ts} +648 -305
- package/dist/chunks/manager-ui-AOFUNXAJ.ts.map +7 -0
- package/dist/chunks/remote-selection-ui-4BTP2OJR.ts +18 -0
- package/dist/chunks/{setup-switch-SPZE7HIQ.ts → setup-switch-Y6AMFFNK.ts} +7 -7
- package/dist/chunks/{setup-wizard-5JYKAEIT.ts → setup-wizard-5G426MOZ.ts} +18 -16
- package/dist/chunks/setup-wizard-5G426MOZ.ts.map +7 -0
- package/dist/chunks/{snapshot-5HY3M2NL.ts → snapshot-ZUJJZCFW.ts} +2 -2
- package/dist/chunks/{snapshot-transaction-directory-OU2D7R7A.ts → snapshot-transaction-directory-F46JWRAU.ts} +2 -2
- package/dist/chunks/{state-directory-XXHU73Z3.ts → state-directory-CMAIF34J.ts} +2 -2
- package/dist/chunks/{sync-inspection-6VGR2UVZ.ts → sync-inspection-ITZ6Z4DE.ts} +7 -7
- package/dist/chunks/{sync-operations-SAKAZL6Z.ts → sync-operations-NMVKF7SE.ts} +13 -9
- package/dist/chunks/sync-operations-NMVKF7SE.ts.map +7 -0
- package/dist/chunks/{sync-state-2LMIXJ2C.ts → sync-state-QIT64TKV.ts} +2 -2
- package/dist/daemon/LICENSE +21 -0
- package/dist/daemon/dist/build-version.js +8 -0
- package/dist/daemon/dist/child-sdk.mjs +17 -0
- package/dist/daemon/dist/cli.js +169 -0
- package/dist/daemon/dist/client.js +909 -0
- package/dist/daemon/dist/consent.js +265 -0
- package/dist/daemon/dist/document.js +6 -0
- package/dist/daemon/dist/endpoint.js +91 -0
- package/dist/daemon/dist/engine/backends/backend-factory.js +16 -0
- package/dist/daemon/dist/engine/backends/backend-identity.js +38 -0
- package/dist/daemon/dist/engine/backends/backend-types.js +1 -0
- package/dist/daemon/dist/engine/backends/git/git-backend.js +715 -0
- package/dist/daemon/dist/engine/backends/git/git-config.js +118 -0
- package/dist/daemon/dist/engine/backends/git/git-runner.js +232 -0
- package/dist/daemon/dist/engine/backends/git/git-storage.js +252 -0
- package/dist/daemon/dist/engine/backends/s3/s3-backend.js +444 -0
- package/dist/daemon/dist/engine/backends/s3/s3-client.js +352 -0
- package/dist/daemon/dist/engine/backends/s3/s3-conditions.js +66 -0
- package/dist/daemon/dist/engine/backends/snapshot-pointer.js +22 -0
- package/dist/daemon/dist/engine/backends/sync-backend.js +23 -0
- package/dist/daemon/dist/engine/backends/webdav/webdav-backend.js +542 -0
- package/dist/daemon/dist/engine/backends/webdav/webdav-client.js +326 -0
- package/dist/daemon/dist/engine/backends/webdav/webdav-config.js +73 -0
- package/dist/daemon/dist/engine/commands/command-types.js +1 -0
- package/dist/daemon/dist/engine/host.js +16 -0
- package/dist/daemon/dist/engine/ignore/effective-ignore-policy.js +33 -0
- package/dist/daemon/dist/engine/ignore/ignore-edit.js +102 -0
- package/dist/daemon/dist/engine/ignore/ignore-initialization.js +186 -0
- package/dist/daemon/dist/engine/ignore/ignore-migration-prompt.js +139 -0
- package/dist/daemon/dist/engine/ignore/ignore-settings.js +322 -0
- package/dist/daemon/dist/engine/ignore/ignore-source.js +250 -0
- package/dist/daemon/dist/engine/paths.js +71 -0
- package/dist/daemon/dist/engine/settings/config-errors.js +4 -0
- package/dist/daemon/dist/engine/settings/config-file.js +736 -0
- package/dist/daemon/dist/engine/settings/config.js +301 -0
- package/dist/daemon/dist/engine/settings/settings-management.js +193 -0
- package/dist/daemon/dist/engine/settings/settings-store.js +41 -0
- package/dist/daemon/dist/engine/settings/settings-types.js +1 -0
- package/dist/daemon/dist/engine/settings/settings-validation.js +384 -0
- package/dist/daemon/dist/engine/snapshot/session-paths.js +160 -0
- package/dist/daemon/dist/engine/snapshot/snapshot-apply.js +348 -0
- package/dist/daemon/dist/engine/snapshot/snapshot-bounds.js +20 -0
- package/dist/daemon/dist/engine/snapshot/snapshot-case-replacement.js +82 -0
- package/dist/daemon/dist/engine/snapshot/snapshot-codec.js +61 -0
- package/dist/daemon/dist/engine/snapshot/snapshot-entry-projection.js +78 -0
- package/dist/daemon/dist/engine/snapshot/snapshot-exclude-safety.js +262 -0
- package/dist/daemon/dist/engine/snapshot/snapshot-mutation-queues.js +133 -0
- package/dist/daemon/dist/engine/snapshot/snapshot-paths.js +11 -0
- package/dist/daemon/dist/engine/snapshot/snapshot-payload.js +10 -0
- package/dist/daemon/dist/engine/snapshot/snapshot-reviewed-preimages.js +60 -0
- package/dist/daemon/dist/engine/snapshot/snapshot-root-transition.js +138 -0
- package/dist/daemon/dist/engine/snapshot/snapshot-transaction-directory.js +16 -0
- package/dist/daemon/dist/engine/snapshot/snapshot-transaction-images.js +387 -0
- package/dist/daemon/dist/engine/snapshot/snapshot-transaction-plan.js +68 -0
- package/dist/daemon/dist/engine/snapshot/snapshot-transaction-policy.js +276 -0
- package/dist/daemon/dist/engine/snapshot/snapshot-transaction-stage.js +303 -0
- package/dist/daemon/dist/engine/snapshot/snapshot-transaction-verification.js +78 -0
- package/dist/daemon/dist/engine/snapshot/snapshot-transaction.js +813 -0
- package/dist/daemon/dist/engine/snapshot/snapshot-types.js +9 -0
- package/dist/daemon/dist/engine/snapshot/snapshot.js +678 -0
- package/dist/daemon/dist/engine/state/json-file.js +49 -0
- package/dist/daemon/dist/engine/state/lock-policy.js +2 -0
- package/dist/daemon/dist/engine/state/lock.js +259 -0
- package/dist/daemon/dist/engine/state/lockfile-fs.js +13 -0
- package/dist/daemon/dist/engine/state/merge-baseline-store.js +198 -0
- package/dist/daemon/dist/engine/state/mutation-directory-sync.js +34 -0
- package/dist/daemon/dist/engine/state/operation-availability.js +40 -0
- package/dist/daemon/dist/engine/state/state-directory.js +227 -0
- package/dist/daemon/dist/engine/state/state-types.js +1 -0
- package/dist/daemon/dist/engine/state/sync-state-store.js +54 -0
- package/dist/daemon/dist/engine/sync/agent-candidate.js +207 -0
- package/dist/daemon/dist/engine/sync/agent-plan.js +84 -0
- package/dist/daemon/dist/engine/sync/agent-policy.js +10 -0
- package/dist/daemon/dist/engine/sync/agent-refusal-retry.js +39 -0
- package/dist/daemon/dist/engine/sync/agent-request.js +13 -0
- package/dist/daemon/dist/engine/sync/agent-runner.js +183 -0
- package/dist/daemon/dist/engine/sync/agent-store.js +177 -0
- package/dist/daemon/dist/engine/sync/agent-sync.js +132 -0
- package/dist/daemon/dist/engine/sync/apply-review.js +52 -0
- package/dist/daemon/dist/engine/sync/conflict-artifact-ownership.js +53 -0
- package/dist/daemon/dist/engine/sync/conflict-artifacts.js +158 -0
- package/dist/daemon/dist/engine/sync/conflict-groups.js +76 -0
- package/dist/daemon/dist/engine/sync/conflict-preview.js +53 -0
- package/dist/daemon/dist/engine/sync/conflict-resolution.js +53 -0
- package/dist/daemon/dist/engine/sync/conflict-retention.js +61 -0
- package/dist/daemon/dist/engine/sync/conflict-review.js +73 -0
- package/dist/daemon/dist/engine/sync/content-conflicts.js +70 -0
- package/dist/daemon/dist/engine/sync/continuous-errors.js +66 -0
- package/dist/daemon/dist/engine/sync/continuous-lease.js +206 -0
- package/dist/daemon/dist/engine/sync/continuous-record.js +199 -0
- package/dist/daemon/dist/engine/sync/continuous-storage.js +59 -0
- package/dist/daemon/dist/engine/sync/continuous-sync.js +521 -0
- package/dist/daemon/dist/engine/sync/continuous-watch.js +140 -0
- package/dist/daemon/dist/engine/sync/exclude-policy.js +86 -0
- package/dist/daemon/dist/engine/sync/field-migration.js +20 -0
- package/dist/daemon/dist/engine/sync/file-merge-planner.js +123 -0
- package/dist/daemon/dist/engine/sync/initialization.js +80 -0
- package/dist/daemon/dist/engine/sync/json-document.js +172 -0
- package/dist/daemon/dist/engine/sync/local-field-policy.js +26 -0
- package/dist/daemon/dist/engine/sync/local-fields.js +196 -0
- package/dist/daemon/dist/engine/sync/merge-apply.js +529 -0
- package/dist/daemon/dist/engine/sync/merge-journal.js +295 -0
- package/dist/daemon/dist/engine/sync/merge-transfer-error.js +64 -0
- package/dist/daemon/dist/engine/sync/merged-sync.js +674 -0
- package/dist/daemon/dist/engine/sync/mutation-owner.js +18 -0
- package/dist/daemon/dist/engine/sync/package-merge.js +82 -0
- package/dist/daemon/dist/engine/sync/partial-progress.js +41 -0
- package/dist/daemon/dist/engine/sync/remote-snapshot.js +104 -0
- package/dist/daemon/dist/engine/sync/resolver-stream.js +34 -0
- package/dist/daemon/dist/engine/sync/session-merge.js +178 -0
- package/dist/daemon/dist/engine/sync/session-push.js +33 -0
- package/dist/daemon/dist/engine/sync/settings-conflicts.js +56 -0
- package/dist/daemon/dist/engine/sync/settings-merge.js +93 -0
- package/dist/daemon/dist/engine/sync/signals.js +8 -0
- package/dist/daemon/dist/engine/sync/startup-timings.js +44 -0
- package/dist/daemon/dist/engine/sync/sync-decision.js +81 -0
- package/dist/daemon/dist/engine/sync/sync-error-guidance.js +55 -0
- package/dist/daemon/dist/engine/sync/sync-errors.js +18 -0
- package/dist/daemon/dist/engine/sync/sync-inspection.js +42 -0
- package/dist/daemon/dist/engine/sync/sync-loaders.js +8 -0
- package/dist/daemon/dist/engine/sync/sync-local.js +40 -0
- package/dist/daemon/dist/engine/sync/sync-mutations.js +733 -0
- package/dist/daemon/dist/engine/sync/sync-operations.js +3 -0
- package/dist/daemon/dist/engine/sync/sync-policy.js +621 -0
- package/dist/daemon/dist/engine/sync/sync-queries.js +212 -0
- package/dist/daemon/dist/engine/sync/sync-state.js +129 -0
- package/dist/daemon/dist/engine/sync/text-merge.js +124 -0
- package/dist/daemon/dist/engine/types.js +2 -0
- package/dist/daemon/dist/engine/ui/merge-review.js +7 -0
- package/dist/daemon/dist/engine/ui/merged-sync-summary.js +86 -0
- package/dist/daemon/dist/engine/ui/sync-format.js +165 -0
- package/dist/daemon/dist/engine/ui/sync-status.js +30 -0
- package/dist/daemon/dist/engine/ui/terminal-text.js +9 -0
- package/dist/daemon/dist/engine/ui/transfer-changes.js +24 -0
- package/dist/daemon/dist/engine/ui/transfer-review.js +19 -0
- package/dist/daemon/dist/engine-service.js +344 -0
- package/dist/daemon/dist/identity.js +241 -0
- package/dist/daemon/dist/idle-exit.js +77 -0
- package/dist/daemon/dist/index.js +4 -0
- package/dist/daemon/dist/lifecycle-log.js +53 -0
- package/dist/daemon/dist/lifecycle.js +21 -0
- package/dist/daemon/dist/model-signature.js +7 -0
- package/dist/daemon/dist/monitor.js +272 -0
- package/dist/daemon/dist/participant-admission.js +30 -0
- package/dist/daemon/dist/protocol.js +104 -0
- package/dist/daemon/dist/resolver-host.js +78 -0
- package/dist/daemon/dist/server.js +953 -0
- package/dist/daemon/dist/service-host.js +174 -0
- package/dist/daemon/dist/upgrade-repair.js +55 -0
- package/dist/daemon/package.json +3 -0
- package/dist/index.ts +32 -32
- package/dist/package.json +6 -0
- package/docs/daemon-migration.md +27 -16
- package/package.json +11 -5
- package/skills/configure-pi-sync/SKILL.md +38 -13
- package/skills/configure-pi-sync/references/configuration.md +61 -34
- package/src/commands/command-execution.ts +8 -2
- package/src/daemon/pi-client.ts +1 -0
- package/src/settings/config.ts +1 -0
- package/src/sync/sync-operations.ts +18 -10
- package/src/sync/sync-policy.ts +5 -0
- package/src/ui/agent-mode-setting.ts +9 -1
- package/src/ui/excluded-paths-ui.ts +157 -2
- package/src/ui/file-selection.ts +12 -7
- package/src/ui/ignore-file-ui.ts +271 -0
- package/src/ui/ignore-migration-prompt.ts +65 -0
- package/src/ui/manager-state.ts +39 -20
- package/src/ui/manager-ui.ts +38 -19
- package/src/ui/remote-selection-ui.ts +159 -33
- package/src/ui/settings-frame.ts +2 -8
- package/src/ui/settings-review.ts +26 -0
- package/src/ui/settings-ui.ts +327 -199
- package/src/ui/setup/directory-rules.ts +81 -0
- package/src/ui/setup/git-ui.ts +14 -24
- package/src/ui/setup/s3-ui.ts +13 -26
- package/src/ui/setup/setup-actions.ts +0 -4
- package/src/ui/setup/setup-prompts.ts +99 -17
- package/src/ui/setup/setup-switcher.ts +4 -2
- package/src/ui/setup/setup-wizard.ts +5 -4
- package/src/ui/setup/webdav-ui.ts +13 -27
- package/src/ui/storage-connections-ui.ts +22 -14
- package/src/ui/sync-attention.ts +8 -3
- package/src/ui/sync-format.ts +8 -1
- package/src/ui/sync-setups-ui.ts +47 -27
- package/dist/chunks/chunk-6KIWZ4AY.ts.map +0 -7
- package/dist/chunks/chunk-KPWZNMKM.ts.map +0 -7
- package/dist/chunks/chunk-LAXTCSX4.ts.map +0 -7
- package/dist/chunks/chunk-V2QIMC3U.ts.map +0 -7
- package/dist/chunks/chunk-V4AMI3VL.ts.map +0 -7
- package/dist/chunks/chunk-YVH5VJR4.ts.map +0 -7
- package/dist/chunks/command-handler-TNPWFIPG.ts.map +0 -7
- package/dist/chunks/file-selection-DD6X4J2K.ts.map +0 -7
- package/dist/chunks/manager-result-dispatcher-2GFNWHDM.ts +0 -18
- package/dist/chunks/manager-ui-LFONXWQN.ts.map +0 -7
- package/dist/chunks/remote-selection-ui-GFR6TDTX.ts +0 -17
- package/dist/chunks/setup-wizard-5JYKAEIT.ts.map +0 -7
- package/dist/chunks/sync-operations-SAKAZL6Z.ts.map +0 -7
- /package/dist/chunks/{chunk-F64YWRXG.ts.map → chunk-3E3TR3JE.ts.map} +0 -0
- /package/dist/chunks/{chunk-ZIL6PW5L.ts.map → chunk-6YKOHBSC.ts.map} +0 -0
- /package/dist/chunks/{chunk-OA4NZLSL.ts.map → chunk-APZVJAEW.ts.map} +0 -0
- /package/dist/chunks/{chunk-EMRFVLZM.ts.map → chunk-NXWHPVXR.ts.map} +0 -0
- /package/dist/chunks/{chunk-SMVKMYLK.ts.map → chunk-P4GI7LTT.ts.map} +0 -0
- /package/dist/chunks/{chunk-MCUZFUHY.ts.map → chunk-PWESMTH6.ts.map} +0 -0
- /package/dist/chunks/{chunk-RNIEVDTR.ts.map → chunk-T3LABNHF.ts.map} +0 -0
- /package/dist/chunks/{chunk-S3R3W3KF.ts.map → chunk-UMPA5S7X.ts.map} +0 -0
- /package/dist/chunks/{chunk-WP2M67XZ.ts.map → chunk-WBIOSK76.ts.map} +0 -0
- /package/dist/chunks/{chunk-HEB6YT64.ts.map → chunk-ZO52JM4H.ts.map} +0 -0
- /package/dist/chunks/{conflict-review-ETCSYJTH.ts.map → conflict-review-MAOGTIAI.ts.map} +0 -0
- /package/dist/chunks/{continuous-record-3XDXDOMV.ts.map → continuous-record-3SKJ6I3V.ts.map} +0 -0
- /package/dist/chunks/{continuous-storage-HLKCHF6Z.ts.map → continuous-storage-3EU6I7Z3.ts.map} +0 -0
- /package/dist/chunks/{manager-result-dispatcher-2GFNWHDM.ts.map → manager-result-dispatcher-MC6EQOQY.ts.map} +0 -0
- /package/dist/chunks/{remote-selection-ui-GFR6TDTX.ts.map → remote-selection-ui-4BTP2OJR.ts.map} +0 -0
- /package/dist/chunks/{setup-switch-SPZE7HIQ.ts.map → setup-switch-Y6AMFFNK.ts.map} +0 -0
- /package/dist/chunks/{snapshot-5HY3M2NL.ts.map → snapshot-ZUJJZCFW.ts.map} +0 -0
- /package/dist/chunks/{snapshot-transaction-directory-OU2D7R7A.ts.map → snapshot-transaction-directory-F46JWRAU.ts.map} +0 -0
- /package/dist/chunks/{state-directory-XXHU73Z3.ts.map → state-directory-CMAIF34J.ts.map} +0 -0
- /package/dist/chunks/{sync-inspection-6VGR2UVZ.ts.map → sync-inspection-ITZ6Z4DE.ts.map} +0 -0
- /package/dist/chunks/{sync-state-2LMIXJ2C.ts.map → sync-state-QIT64TKV.ts.map} +0 -0
|
@@ -0,0 +1,348 @@
|
|
|
1
|
+
import { createHash } from "node:crypto";
|
|
2
|
+
import fs from "node:fs/promises";
|
|
3
|
+
import path from "node:path";
|
|
4
|
+
import { assertWithinRoot, isDeniedPath, isPathInside, parentPaths, safeJoin, toPosix } from "../paths.js";
|
|
5
|
+
import { syncDirectory } from "../state/json-file.js";
|
|
6
|
+
import { stateDir } from "../state/state-directory.js";
|
|
7
|
+
import { compileSyncPolicyExclude, requireLegacyTransferPolicy, sameSnapshotSelection, selectionForSnapshot, } from "../sync/sync-policy.js";
|
|
8
|
+
import { agentDir } from "./session-paths.js";
|
|
9
|
+
import { agentDirectoryConfiguredSessionDir, agentDirectoryInventory, createSnapshot, isSessionFilePath, isSessionPath, sessionStorageRoot, snapshotIncludesSessions, snapshotTarget, } from "./snapshot.js";
|
|
10
|
+
import { assertAgentDirectorySnapshotBound } from "./snapshot-bounds.js";
|
|
11
|
+
import { assertProjectedAgentDirectoryBudget } from "./snapshot-entry-projection.js";
|
|
12
|
+
import { agentDirectoryPhysicalSessionPath, assertUnexcludedTargets } from "./snapshot-exclude-safety.js";
|
|
13
|
+
import { decodeBase64Strict } from "./snapshot-payload.js";
|
|
14
|
+
import { reviewedSnapshotPreimages } from "./snapshot-reviewed-preimages.js";
|
|
15
|
+
import { applySnapshotTransaction, recoverPendingSnapshotTransactions } from "./snapshot-transaction.js";
|
|
16
|
+
import { ignorePolicyValidator } from "./snapshot-transaction-policy.js";
|
|
17
|
+
import { IGNORE_SNAPSHOT_VERSION, } from "./snapshot-types.js";
|
|
18
|
+
function sha256(value) {
|
|
19
|
+
return createHash("sha256").update(value).digest("hex");
|
|
20
|
+
}
|
|
21
|
+
function fileHashMap(snapshot) {
|
|
22
|
+
return Object.fromEntries(snapshot.files.map((file) => [file.path, file.sha256]));
|
|
23
|
+
}
|
|
24
|
+
function assertSnapshotApplyPolicy(snapshot, options) {
|
|
25
|
+
if (snapshot.version !== IGNORE_SNAPSHOT_VERSION) {
|
|
26
|
+
requireLegacyTransferPolicy(snapshot);
|
|
27
|
+
if (options.ignorePolicy)
|
|
28
|
+
throw new Error("Project the snapshot into the reviewed local ignore-file policy before applying it.");
|
|
29
|
+
return;
|
|
30
|
+
}
|
|
31
|
+
if (options.scope !== "agent-directory" || !options.ignorePolicy)
|
|
32
|
+
requireLegacyTransferPolicy(snapshot);
|
|
33
|
+
if (!sameSnapshotSelection(snapshot.selection, selectionForSnapshot(undefined, undefined, options)))
|
|
34
|
+
throw new Error("Snapshot does not match the local ignore-file policy; review and project it before applying it.");
|
|
35
|
+
}
|
|
36
|
+
export async function applySnapshot(snapshot, protectedRelativePaths = new Set(), options = {}) {
|
|
37
|
+
assertSnapshotApplyPolicy(snapshot, options);
|
|
38
|
+
const validateIgnorePolicy = ignorePolicyValidator(options);
|
|
39
|
+
const root = agentDir();
|
|
40
|
+
if (snapshot.version === 5 && options.scope !== "agent-directory")
|
|
41
|
+
throw new Error("Agent-directory snapshot apply requires the local agent-directory policy.");
|
|
42
|
+
const sessionDir = options.scope === "agent-directory"
|
|
43
|
+
? sessionStorageRoot(root, options.sessionDir ?? (await agentDirectoryConfiguredSessionDir(options.signal, root, options)))
|
|
44
|
+
: options.sessionDir;
|
|
45
|
+
options.validateMutation?.();
|
|
46
|
+
options = { ...options, sessionDir };
|
|
47
|
+
const excluded = excludedApplyPath(root, options);
|
|
48
|
+
snapshot = { ...snapshot, files: snapshot.files.filter((file) => !excluded(file.path)) };
|
|
49
|
+
const transactionOptions = {
|
|
50
|
+
...options,
|
|
51
|
+
protectedTargets: [...protectedRelativePaths].map((relative) => snapshotTarget(root, relative, sessionDir)),
|
|
52
|
+
expectedPreimages: new Map(Object.entries(options.expectedFileHashes ?? {})
|
|
53
|
+
.filter(([relative]) => !excluded(relative))
|
|
54
|
+
.map(([relative, hash]) => [
|
|
55
|
+
snapshotTarget(root, relative, sessionDir),
|
|
56
|
+
hash === null ? "missing" : `file:${hash}`,
|
|
57
|
+
])),
|
|
58
|
+
};
|
|
59
|
+
options.validateMutation?.();
|
|
60
|
+
if (validateIgnorePolicy)
|
|
61
|
+
await validateIgnorePolicy();
|
|
62
|
+
await recoverPendingSnapshotTransactions(transactionOptions);
|
|
63
|
+
const current = await createSnapshot(snapshot.profile, {
|
|
64
|
+
...options,
|
|
65
|
+
...(options.scope === undefined && options.include === undefined
|
|
66
|
+
? { syncSessions: snapshotIncludesSessions(snapshot) }
|
|
67
|
+
: {}),
|
|
68
|
+
sessionDir,
|
|
69
|
+
});
|
|
70
|
+
const inventory = agentDirectoryInventory(current);
|
|
71
|
+
if (options.reviewedLocal && !sameReviewedFiles(current, options.reviewedLocal))
|
|
72
|
+
throw new Error("Reviewed local files changed before apply; reopen the transfer preview.");
|
|
73
|
+
if (options.scope === "agent-directory") {
|
|
74
|
+
assertAgentDirectorySnapshotBound([
|
|
75
|
+
...snapshot.files.filter(({ path }) => !protectedRelativePaths.has(path)),
|
|
76
|
+
...current.files.filter(({ path }) => protectedRelativePaths.has(path)),
|
|
77
|
+
]);
|
|
78
|
+
}
|
|
79
|
+
const plan = await addTopLevelCaseVariantDeletes(root, protectSnapshotApplyPlan(root, preflightSnapshotApply(root, snapshot, current, options), protectedRelativePaths, sessionDir), snapshot, options);
|
|
80
|
+
if (inventory) {
|
|
81
|
+
assertProjectedAgentDirectoryBudget(inventory, {
|
|
82
|
+
deletes: plan.deletes,
|
|
83
|
+
writes: [
|
|
84
|
+
...plan.writes.map(({ target, content }) => ({ target, size: content.length })),
|
|
85
|
+
{ target: stateDir(root) },
|
|
86
|
+
],
|
|
87
|
+
}, options);
|
|
88
|
+
await preflightSnapshotMutations(root, plan, sessionDir, options);
|
|
89
|
+
}
|
|
90
|
+
if (options.reviewedLocal || inventory) {
|
|
91
|
+
const images = reviewedSnapshotPreimages(root, sessionDir, current.files, plan, inventory);
|
|
92
|
+
const reviewed = options.reviewedLocal;
|
|
93
|
+
if (reviewed && inventory) {
|
|
94
|
+
const approved = reviewedSnapshotPreimages(root, sessionDir, reviewed.files, plan, agentDirectoryInventory(reviewed));
|
|
95
|
+
if ([...images].some(([target, image]) => image !== approved.get(target)))
|
|
96
|
+
throw new Error("Reviewed directory preimage changed before apply; reopen the transfer preview.");
|
|
97
|
+
}
|
|
98
|
+
for (const [target, image] of images) {
|
|
99
|
+
const existing = transactionOptions.expectedPreimages.get(target);
|
|
100
|
+
if (existing !== undefined && existing !== image)
|
|
101
|
+
throw new Error("Reviewed preimage evidence is inconsistent.");
|
|
102
|
+
transactionOptions.expectedPreimages.set(target, image);
|
|
103
|
+
}
|
|
104
|
+
}
|
|
105
|
+
if (!inventory)
|
|
106
|
+
await preflightSnapshotMutations(root, plan, sessionDir, options);
|
|
107
|
+
await applySnapshotTransaction(plan, transactionOptions);
|
|
108
|
+
return appliedFileHashMap(snapshot, current, protectedRelativePaths);
|
|
109
|
+
}
|
|
110
|
+
function sameReviewedFiles(scanned, reviewed) {
|
|
111
|
+
const hashes = new Map(scanned.files.map((file) => [file.path, file.sha256]));
|
|
112
|
+
return (scanned.files.length === reviewed.files.length &&
|
|
113
|
+
reviewed.files.every((file) => hashes.get(file.path) === file.sha256));
|
|
114
|
+
}
|
|
115
|
+
export function preflightSnapshotApply(root, snapshot, current, options = {}) {
|
|
116
|
+
assertSnapshotApplyPolicy(snapshot, options);
|
|
117
|
+
const seenPaths = new Set();
|
|
118
|
+
const remotePaths = new Set();
|
|
119
|
+
const writes = [];
|
|
120
|
+
const deletes = [];
|
|
121
|
+
const excluded = excludedApplyPath(root, options);
|
|
122
|
+
for (const file of snapshot.files) {
|
|
123
|
+
const normalized = toPosix(file.path);
|
|
124
|
+
if (options.scope === "agent-directory" && excluded(normalized))
|
|
125
|
+
continue;
|
|
126
|
+
if (!isSafeSnapshotPath(file.path)) {
|
|
127
|
+
throw new Error(`Unsafe path in snapshot: ${file.path}`);
|
|
128
|
+
}
|
|
129
|
+
if (isSessionPath(normalized) && !isSessionFilePath(normalized)) {
|
|
130
|
+
throw new Error(`Unsafe session path in snapshot: ${file.path}`);
|
|
131
|
+
}
|
|
132
|
+
if (excluded(normalized))
|
|
133
|
+
continue;
|
|
134
|
+
if (seenPaths.has(normalized))
|
|
135
|
+
throw new Error(`Duplicate path in snapshot: ${normalized}`);
|
|
136
|
+
seenPaths.add(normalized);
|
|
137
|
+
remotePaths.add(normalized);
|
|
138
|
+
const target = snapshotTarget(root, normalized, options.sessionDir);
|
|
139
|
+
const content = decodeBase64Strict(file.contentBase64, normalized);
|
|
140
|
+
if (sha256(content) !== file.sha256)
|
|
141
|
+
throw new Error(`Checksum mismatch in snapshot file: ${normalized}`);
|
|
142
|
+
writes.push({ target, content });
|
|
143
|
+
}
|
|
144
|
+
const deletePaths = new Set();
|
|
145
|
+
const inventory = options.scope === "agent-directory" ? agentDirectoryInventory(current) : undefined;
|
|
146
|
+
for (const { target } of writes) {
|
|
147
|
+
if (inventory?.entries.get(target) === "directory")
|
|
148
|
+
deletePaths.add(target);
|
|
149
|
+
}
|
|
150
|
+
for (const file of current.files) {
|
|
151
|
+
const normalized = toPosix(file.path);
|
|
152
|
+
if (excluded(normalized))
|
|
153
|
+
continue;
|
|
154
|
+
if (!remotePaths.has(normalized)) {
|
|
155
|
+
deletePaths.add(snapshotTarget(root, normalized, options.sessionDir));
|
|
156
|
+
}
|
|
157
|
+
for (const remotePath of parentPaths(normalized)) {
|
|
158
|
+
if (remotePaths.has(remotePath)) {
|
|
159
|
+
deletePaths.add(snapshotTarget(root, remotePath, options.sessionDir));
|
|
160
|
+
}
|
|
161
|
+
}
|
|
162
|
+
}
|
|
163
|
+
deletes.push(...deletePaths);
|
|
164
|
+
return { writes, deletes };
|
|
165
|
+
}
|
|
166
|
+
export function protectSnapshotApplyPlan(root, plan, protectedRelativePaths, sessionDir) {
|
|
167
|
+
if (protectedRelativePaths.size === 0)
|
|
168
|
+
return plan;
|
|
169
|
+
const protectedTargets = new Set([...protectedRelativePaths].map((relativePath) => snapshotTarget(root, relativePath, sessionDir)));
|
|
170
|
+
return {
|
|
171
|
+
writes: plan.writes.filter((item) => !protectedTargets.has(item.target)),
|
|
172
|
+
deletes: plan.deletes.filter((target) => !protectedTargets.has(target)),
|
|
173
|
+
};
|
|
174
|
+
}
|
|
175
|
+
export async function addTopLevelCaseVariantDeletes(root, plan, snapshot, options = {}) {
|
|
176
|
+
if (options.scope === "agent-directory" || snapshot.selection?.scope === "agent-directory")
|
|
177
|
+
return plan;
|
|
178
|
+
const excluded = compileSyncPolicyExclude(options);
|
|
179
|
+
const topLevelPaths = new Map();
|
|
180
|
+
for (const file of snapshot.files) {
|
|
181
|
+
const normalized = toPosix(file.path);
|
|
182
|
+
if (!normalized.includes("/") && isSafeSnapshotPath(file.path) && !excluded(normalized)) {
|
|
183
|
+
topLevelPaths.set(normalized.toLowerCase(), normalized);
|
|
184
|
+
}
|
|
185
|
+
}
|
|
186
|
+
if (topLevelPaths.size === 0)
|
|
187
|
+
return plan;
|
|
188
|
+
let entries;
|
|
189
|
+
try {
|
|
190
|
+
entries = await fs.readdir(root, { withFileTypes: true });
|
|
191
|
+
}
|
|
192
|
+
catch (error) {
|
|
193
|
+
if (error.code === "ENOENT")
|
|
194
|
+
return plan;
|
|
195
|
+
throw error;
|
|
196
|
+
}
|
|
197
|
+
const deletes = new Set(plan.deletes);
|
|
198
|
+
for (const entry of entries) {
|
|
199
|
+
const canonicalPath = topLevelPaths.get(entry.name.toLowerCase());
|
|
200
|
+
if (canonicalPath &&
|
|
201
|
+
entry.name !== canonicalPath &&
|
|
202
|
+
!excluded(entry.name) &&
|
|
203
|
+
(entry.isFile() || entry.isSymbolicLink())) {
|
|
204
|
+
deletes.add(safeJoin(root, entry.name));
|
|
205
|
+
}
|
|
206
|
+
}
|
|
207
|
+
return { ...plan, deletes: [...deletes] };
|
|
208
|
+
}
|
|
209
|
+
export function appliedFileHashMap(snapshot, current, protectedRelativePaths) {
|
|
210
|
+
const hashes = fileHashMap(snapshot);
|
|
211
|
+
if (protectedRelativePaths.size === 0)
|
|
212
|
+
return hashes;
|
|
213
|
+
const currentHashes = fileHashMap(current);
|
|
214
|
+
for (const relativePath of protectedRelativePaths) {
|
|
215
|
+
const normalized = toPosix(relativePath);
|
|
216
|
+
if (currentHashes[normalized]) {
|
|
217
|
+
hashes[normalized] = currentHashes[normalized];
|
|
218
|
+
}
|
|
219
|
+
else {
|
|
220
|
+
delete hashes[normalized];
|
|
221
|
+
}
|
|
222
|
+
}
|
|
223
|
+
return hashes;
|
|
224
|
+
}
|
|
225
|
+
function isSafeSnapshotPath(relativePath) {
|
|
226
|
+
if (relativePath.includes("\\"))
|
|
227
|
+
return false;
|
|
228
|
+
const normalized = toPosix(relativePath);
|
|
229
|
+
return (Boolean(normalized) &&
|
|
230
|
+
normalized !== "." &&
|
|
231
|
+
normalized !== ".." &&
|
|
232
|
+
!normalized.startsWith("../") &&
|
|
233
|
+
!path.posix.isAbsolute(normalized) &&
|
|
234
|
+
path.posix.normalize(normalized) === normalized &&
|
|
235
|
+
!isDeniedPath(normalized));
|
|
236
|
+
}
|
|
237
|
+
function excludedApplyPath(root, options) {
|
|
238
|
+
const excluded = compileSyncPolicyExclude(options);
|
|
239
|
+
if (options.scope !== "agent-directory")
|
|
240
|
+
return excluded;
|
|
241
|
+
const physicalSessionPath = agentDirectoryPhysicalSessionPath(root, options.sessionDir, options.ignorePolicy, options.reservedIgnoreNames);
|
|
242
|
+
return (relative) => excluded(relative) ||
|
|
243
|
+
(/^sessions(?:\/|$)/iu.test(relative) ? !isSessionFilePath(relative) : physicalSessionPath(relative));
|
|
244
|
+
}
|
|
245
|
+
export async function preflightSnapshotMutations(root, plan, sessionDir, options = {}) {
|
|
246
|
+
assertMutationCurrent(options);
|
|
247
|
+
await assertUnexcludedTargets(root, [...plan.deletes, ...plan.writes.map((item) => item.target)], {
|
|
248
|
+
...options,
|
|
249
|
+
sessionDir,
|
|
250
|
+
});
|
|
251
|
+
assertMutationCurrent(options);
|
|
252
|
+
const deletePaths = new Set(plan.deletes);
|
|
253
|
+
for (const target of plan.deletes) {
|
|
254
|
+
await assertNoSymlinkParents(rootForTarget(root, target, sessionDir), target, options);
|
|
255
|
+
assertMutationCurrent(options);
|
|
256
|
+
}
|
|
257
|
+
for (const item of plan.writes) {
|
|
258
|
+
await prepareSnapshotWrite(rootForTarget(root, item.target, sessionDir), item.target, deletePaths, options);
|
|
259
|
+
assertMutationCurrent(options);
|
|
260
|
+
}
|
|
261
|
+
}
|
|
262
|
+
function assertMutationCurrent(options) {
|
|
263
|
+
options.validateMutation?.();
|
|
264
|
+
options.signal?.throwIfAborted();
|
|
265
|
+
}
|
|
266
|
+
async function guardedStat(target, options) {
|
|
267
|
+
assertMutationCurrent(options);
|
|
268
|
+
let stat;
|
|
269
|
+
try {
|
|
270
|
+
stat = await fs.lstat(target);
|
|
271
|
+
}
|
|
272
|
+
catch (error) {
|
|
273
|
+
assertMutationCurrent(options);
|
|
274
|
+
if (error.code === "ENOENT")
|
|
275
|
+
return undefined;
|
|
276
|
+
throw error;
|
|
277
|
+
}
|
|
278
|
+
assertMutationCurrent(options);
|
|
279
|
+
return stat;
|
|
280
|
+
}
|
|
281
|
+
function rootForTarget(root, target, sessionDir) {
|
|
282
|
+
const sessionRoot = sessionDir ? sessionStorageRoot(root, sessionDir) : undefined;
|
|
283
|
+
if (sessionRoot && isPathInside(sessionRoot, target))
|
|
284
|
+
return sessionRoot;
|
|
285
|
+
return root;
|
|
286
|
+
}
|
|
287
|
+
async function prepareSnapshotWrite(root, target, deletePaths, options) {
|
|
288
|
+
const parentWillBeReplaced = await ensureSafeDirectory(root, path.dirname(target), deletePaths, options);
|
|
289
|
+
assertMutationCurrent(options);
|
|
290
|
+
if (parentWillBeReplaced)
|
|
291
|
+
return;
|
|
292
|
+
const stat = await guardedStat(target, options);
|
|
293
|
+
assertMutationCurrent(options);
|
|
294
|
+
if (stat?.isSymbolicLink())
|
|
295
|
+
throw new Error(`Refusing to overwrite symlink during snapshot apply: ${target}`);
|
|
296
|
+
if (stat?.isDirectory() && !deletePaths.has(target)) {
|
|
297
|
+
throw new Error(`Refusing to overwrite directory during snapshot apply: ${target}`);
|
|
298
|
+
}
|
|
299
|
+
}
|
|
300
|
+
async function ensureSafeDirectory(root, directory, deletePaths, options) {
|
|
301
|
+
assertMutationCurrent(options);
|
|
302
|
+
assertWithinRoot(root, directory);
|
|
303
|
+
const rootPath = path.resolve(root);
|
|
304
|
+
const relative = path.relative(rootPath, path.resolve(directory));
|
|
305
|
+
let current = rootPath;
|
|
306
|
+
for (const part of relative.split(path.sep).filter(Boolean)) {
|
|
307
|
+
current = path.join(current, part);
|
|
308
|
+
const stat = await guardedStat(current, options);
|
|
309
|
+
assertMutationCurrent(options);
|
|
310
|
+
if (stat) {
|
|
311
|
+
if (stat.isSymbolicLink())
|
|
312
|
+
throw new Error(`Refusing to follow symlink during snapshot apply: ${current}`);
|
|
313
|
+
if (!stat.isDirectory()) {
|
|
314
|
+
if (deletePaths.has(current))
|
|
315
|
+
return true;
|
|
316
|
+
throw new Error(`Snapshot path parent is not a directory: ${current}`);
|
|
317
|
+
}
|
|
318
|
+
}
|
|
319
|
+
else {
|
|
320
|
+
if (options.scope === "agent-directory")
|
|
321
|
+
return true;
|
|
322
|
+
await fs.mkdir(current);
|
|
323
|
+
assertMutationCurrent(options);
|
|
324
|
+
await syncDirectory(path.dirname(current));
|
|
325
|
+
assertMutationCurrent(options);
|
|
326
|
+
}
|
|
327
|
+
}
|
|
328
|
+
return false;
|
|
329
|
+
}
|
|
330
|
+
async function assertNoSymlinkParents(root, target, options) {
|
|
331
|
+
assertMutationCurrent(options);
|
|
332
|
+
assertWithinRoot(root, target);
|
|
333
|
+
const rootPath = path.resolve(root);
|
|
334
|
+
const relative = path.relative(rootPath, path.resolve(target));
|
|
335
|
+
let current = rootPath;
|
|
336
|
+
const parts = relative.split(path.sep).filter(Boolean);
|
|
337
|
+
for (const part of parts.slice(0, -1)) {
|
|
338
|
+
current = path.join(current, part);
|
|
339
|
+
const stat = await guardedStat(current, options);
|
|
340
|
+
assertMutationCurrent(options);
|
|
341
|
+
if (!stat)
|
|
342
|
+
return;
|
|
343
|
+
if (stat.isSymbolicLink())
|
|
344
|
+
throw new Error(`Refusing to follow symlink during snapshot apply: ${current}`);
|
|
345
|
+
if (!stat.isDirectory())
|
|
346
|
+
throw new Error(`Snapshot path parent is not a directory: ${current}`);
|
|
347
|
+
}
|
|
348
|
+
}
|
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
import { assertAgentDirectoryPathLayout } from "../sync/sync-policy.js";
|
|
2
|
+
import { snapshotBase64Size } from "./snapshot-payload.js";
|
|
3
|
+
export const MAX_AGENT_DIRECTORY_BYTES = 64 * 1024 * 1024;
|
|
4
|
+
const MAX_AGENT_DIRECTORY_SETTINGS_BYTES = 1024 * 1024;
|
|
5
|
+
export function assertAgentDirectorySettingsSize(bytes) {
|
|
6
|
+
if (bytes > MAX_AGENT_DIRECTORY_SETTINGS_BYTES)
|
|
7
|
+
throw new Error("Pi settings.json exceeds the 1 MiB session metadata byte bound.");
|
|
8
|
+
}
|
|
9
|
+
export function assertAgentDirectorySnapshotBound(files) {
|
|
10
|
+
assertAgentDirectoryPathLayout(files.map((file) => file.path));
|
|
11
|
+
let bytes = 0;
|
|
12
|
+
for (const file of files) {
|
|
13
|
+
const size = snapshotBase64Size(file.contentBase64, file.path);
|
|
14
|
+
if (file.path.toLowerCase() === "settings.json")
|
|
15
|
+
assertAgentDirectorySettingsSize(size);
|
|
16
|
+
bytes += size;
|
|
17
|
+
if (bytes > MAX_AGENT_DIRECTORY_BYTES)
|
|
18
|
+
throw new Error("Projected agent directory exceeds the 64 MiB scan byte bound.");
|
|
19
|
+
}
|
|
20
|
+
}
|
|
@@ -0,0 +1,82 @@
|
|
|
1
|
+
import fs from "node:fs/promises";
|
|
2
|
+
import path from "node:path";
|
|
3
|
+
/** Only the top-level file correction produced by addTopLevelCaseVariantDeletes is eligible. */
|
|
4
|
+
export function isCaseReplacement(root, before, after) {
|
|
5
|
+
return (before !== after &&
|
|
6
|
+
path.dirname(before) === root &&
|
|
7
|
+
path.dirname(after) === root &&
|
|
8
|
+
path.basename(before).toLowerCase() === path.basename(after).toLowerCase());
|
|
9
|
+
}
|
|
10
|
+
/** Absence is handled by the journal's removal/replacement evidence, never by naming evidence. */
|
|
11
|
+
export async function caseReplacementSpelling(before, after) {
|
|
12
|
+
const observations = await Promise.all([before, after].map(async (target) => {
|
|
13
|
+
try {
|
|
14
|
+
const stat = await fs.lstat(target);
|
|
15
|
+
if (!stat.isFile() || stat.nlink !== 1)
|
|
16
|
+
throw new Error("Unsafe case replacement target.");
|
|
17
|
+
return await fs.realpath(target);
|
|
18
|
+
}
|
|
19
|
+
catch (error) {
|
|
20
|
+
if (error.code === "ENOENT")
|
|
21
|
+
return undefined;
|
|
22
|
+
throw error;
|
|
23
|
+
}
|
|
24
|
+
}));
|
|
25
|
+
if (observations.every((value) => value === undefined))
|
|
26
|
+
return undefined;
|
|
27
|
+
if (!observations[0] || observations[0] !== observations[1])
|
|
28
|
+
throw new Error("Case replacement no longer identifies one physical file; preserve evidence for review.");
|
|
29
|
+
const names = (await fs.readdir(path.dirname(before))).filter((name) => name.toLowerCase() === path.basename(before).toLowerCase());
|
|
30
|
+
if (names.length !== 1 || (names[0] !== path.basename(before) && names[0] !== path.basename(after)))
|
|
31
|
+
throw new Error("Case replacement spelling changed; preserve evidence for review.");
|
|
32
|
+
return path.join(path.dirname(before), names[0]);
|
|
33
|
+
}
|
|
34
|
+
export async function coalesceCaseReplacements(root, plan) {
|
|
35
|
+
const replacements = new Map();
|
|
36
|
+
const deletesByCase = new Map();
|
|
37
|
+
const writeTargets = new Set(plan.writes.map((item) => item.target));
|
|
38
|
+
for (const target of new Set(plan.deletes)) {
|
|
39
|
+
if (path.dirname(target) !== root || writeTargets.has(target))
|
|
40
|
+
continue;
|
|
41
|
+
const key = path.basename(target).toLowerCase();
|
|
42
|
+
const group = deletesByCase.get(key) ?? [];
|
|
43
|
+
group.push(target);
|
|
44
|
+
deletesByCase.set(key, group);
|
|
45
|
+
}
|
|
46
|
+
for (const item of plan.writes) {
|
|
47
|
+
if (path.dirname(item.target) !== root)
|
|
48
|
+
continue;
|
|
49
|
+
const candidates = deletesByCase.get(path.basename(item.target).toLowerCase()) ?? [];
|
|
50
|
+
if (candidates.length !== 1)
|
|
51
|
+
continue;
|
|
52
|
+
const before = candidates[0];
|
|
53
|
+
if (!before || !isCaseReplacement(root, before, item.target))
|
|
54
|
+
continue;
|
|
55
|
+
// Different real paths on a case-sensitive filesystem remain independent targets.
|
|
56
|
+
let beforePath;
|
|
57
|
+
let afterPath;
|
|
58
|
+
try {
|
|
59
|
+
[beforePath, afterPath] = await Promise.all([fs.realpath(before), fs.realpath(item.target)]);
|
|
60
|
+
}
|
|
61
|
+
catch (error) {
|
|
62
|
+
if (error.code === "ENOENT")
|
|
63
|
+
continue;
|
|
64
|
+
throw error;
|
|
65
|
+
}
|
|
66
|
+
if (beforePath !== afterPath)
|
|
67
|
+
continue;
|
|
68
|
+
if (replacements.has(before))
|
|
69
|
+
throw new Error("Multiple writes alias a case replacement target.");
|
|
70
|
+
if ((await caseReplacementSpelling(before, item.target)) !== before)
|
|
71
|
+
throw new Error("Case replacement preimage spelling changed; review is required.");
|
|
72
|
+
replacements.set(before, item.target);
|
|
73
|
+
}
|
|
74
|
+
const beforeByAfter = new Map([...replacements].map(([before, after]) => [after, before]));
|
|
75
|
+
return {
|
|
76
|
+
replacements,
|
|
77
|
+
plan: {
|
|
78
|
+
deletes: plan.deletes,
|
|
79
|
+
writes: plan.writes.map((item) => ({ ...item, target: beforeByAfter.get(item.target) ?? item.target })),
|
|
80
|
+
},
|
|
81
|
+
};
|
|
82
|
+
}
|
|
@@ -0,0 +1,61 @@
|
|
|
1
|
+
import { isUtf8 } from "node:buffer";
|
|
2
|
+
import { Readable, Writable } from "node:stream";
|
|
3
|
+
import { pipeline } from "node:stream/promises";
|
|
4
|
+
import { promisify } from "node:util";
|
|
5
|
+
import { createGunzip, gzip } from "node:zlib";
|
|
6
|
+
import { throwIfAborted } from "../sync/signals.js";
|
|
7
|
+
import { snapshotSelectionInclude, validateSnapshotSelection } from "../sync/sync-policy.js";
|
|
8
|
+
import { IGNORE_SNAPSHOT_VERSION, isSupportedSnapshotVersion } from "./snapshot-types.js";
|
|
9
|
+
const VERSION = 1;
|
|
10
|
+
const MAX_DECOMPRESSED_SNAPSHOT_BYTES = 512 * 1024 * 1024;
|
|
11
|
+
const gzipAsync = promisify(gzip);
|
|
12
|
+
export async function encodeSnapshot(snapshot) {
|
|
13
|
+
if (!isSupportedSnapshotVersion(snapshot?.version))
|
|
14
|
+
throw new Error("Unsupported snapshot format.");
|
|
15
|
+
if (snapshot.version !== IGNORE_SNAPSHOT_VERSION)
|
|
16
|
+
snapshotSelectionInclude(snapshot);
|
|
17
|
+
validateSnapshotSelection(snapshot);
|
|
18
|
+
if (snapshot.version === 4 || snapshot.version === 5 || snapshot.version === IGNORE_SNAPSHOT_VERSION) {
|
|
19
|
+
const { validatePortableSnapshot } = await import("../sync/local-fields.js");
|
|
20
|
+
validatePortableSnapshot(snapshot);
|
|
21
|
+
}
|
|
22
|
+
return gzipAsync(Buffer.from(JSON.stringify(snapshot), "utf8"));
|
|
23
|
+
}
|
|
24
|
+
export async function decodeSnapshot(buffer, options = {}) {
|
|
25
|
+
throwIfAborted(options.signal);
|
|
26
|
+
const limit = options.maxOutputLength ?? MAX_DECOMPRESSED_SNAPSHOT_BYTES;
|
|
27
|
+
const chunks = [];
|
|
28
|
+
let total = 0;
|
|
29
|
+
const sink = new Writable({
|
|
30
|
+
write(chunk, _encoding, callback) {
|
|
31
|
+
total += chunk.byteLength;
|
|
32
|
+
if (total > limit) {
|
|
33
|
+
callback(new Error(`Decompressed snapshot exceeds the ${limit}-byte limit.`));
|
|
34
|
+
return;
|
|
35
|
+
}
|
|
36
|
+
chunks.push(Buffer.from(chunk));
|
|
37
|
+
callback();
|
|
38
|
+
},
|
|
39
|
+
});
|
|
40
|
+
await pipeline(Readable.from([buffer]), createGunzip(), sink, { signal: options.signal });
|
|
41
|
+
throwIfAborted(options.signal);
|
|
42
|
+
const payload = Buffer.concat(chunks, total);
|
|
43
|
+
const parsed = JSON.parse(payload.toString("utf8"));
|
|
44
|
+
if (!isSupportedSnapshotVersion(parsed?.version) || !Array.isArray(parsed.files)) {
|
|
45
|
+
throw new Error("Unsupported snapshot format.");
|
|
46
|
+
}
|
|
47
|
+
if (parsed.version === IGNORE_SNAPSHOT_VERSION && !isUtf8(payload))
|
|
48
|
+
throw new Error("Invalid snapshot UTF-8.");
|
|
49
|
+
validateSnapshotSelection(parsed);
|
|
50
|
+
if (parsed.version !== VERSION) {
|
|
51
|
+
const { normalizeLocalFields, validatePortableSnapshot } = await import("../sync/local-fields.js");
|
|
52
|
+
validatePortableSnapshot(parsed);
|
|
53
|
+
if (parsed.localFields !== undefined)
|
|
54
|
+
parsed.localFields = normalizeLocalFields(parsed.localFields);
|
|
55
|
+
}
|
|
56
|
+
else if (parsed.localFields !== undefined)
|
|
57
|
+
throw new Error("Portable field policy requires snapshot version 2.");
|
|
58
|
+
if (parsed.version !== IGNORE_SNAPSHOT_VERSION)
|
|
59
|
+
snapshotSelectionInclude(parsed);
|
|
60
|
+
return parsed;
|
|
61
|
+
}
|
|
@@ -0,0 +1,78 @@
|
|
|
1
|
+
import path from "node:path";
|
|
2
|
+
import { isPathInside, toPosix } from "../paths.js";
|
|
3
|
+
import { compileSyncPolicyTraversal, MAX_AGENT_DIRECTORY_ENTRIES, MAX_AGENT_DIRECTORY_FILES, } from "../sync/sync-policy.js";
|
|
4
|
+
import { scanAgentDirectoryInventory } from "./snapshot.js";
|
|
5
|
+
import { MAX_AGENT_DIRECTORY_BYTES } from "./snapshot-bounds.js";
|
|
6
|
+
export async function recheckAgentDirectoryBudget(root, plan, options, pendingStateEntry) {
|
|
7
|
+
const inventory = await scanAgentDirectoryInventory(root, options, pendingStateEntry);
|
|
8
|
+
options.signal?.throwIfAborted();
|
|
9
|
+
options.validateMutation?.();
|
|
10
|
+
assertProjectedAgentDirectoryBudget(inventory, { deletes: plan.deletes, writes: plan.writes.map(({ target, content }) => ({ target, size: content.length })) }, options);
|
|
11
|
+
}
|
|
12
|
+
export function assertProjectedAgentDirectoryBudget({ root, sessionRoot, scanSessions, entries, fileSizes, routingDirectories, walkedDirectories, }, plan, options) {
|
|
13
|
+
const removed = new Map(plan.deletes.map((target) => [target, true]));
|
|
14
|
+
const isRemoved = (target) => {
|
|
15
|
+
const known = removed.get(target);
|
|
16
|
+
if (known !== undefined)
|
|
17
|
+
return known;
|
|
18
|
+
const parent = path.dirname(target);
|
|
19
|
+
const value = parent !== target && isRemoved(parent);
|
|
20
|
+
removed.set(target, value);
|
|
21
|
+
return value;
|
|
22
|
+
};
|
|
23
|
+
let count = 0;
|
|
24
|
+
const files = new Map(plan.writes.flatMap(({ target, size }) => (size === undefined ? [] : [[target, size]])));
|
|
25
|
+
for (const target of entries.keys()) {
|
|
26
|
+
if (!isRemoved(target)) {
|
|
27
|
+
count++;
|
|
28
|
+
const size = fileSizes.get(target);
|
|
29
|
+
if (size !== undefined && !files.has(target))
|
|
30
|
+
files.set(target, size);
|
|
31
|
+
}
|
|
32
|
+
}
|
|
33
|
+
if (files.size > MAX_AGENT_DIRECTORY_FILES)
|
|
34
|
+
throw new Error("Projected agent directory exceeds the scan file bound.");
|
|
35
|
+
let bytes = 0;
|
|
36
|
+
for (const size of files.values())
|
|
37
|
+
bytes += size;
|
|
38
|
+
if (bytes > MAX_AGENT_DIRECTORY_BYTES)
|
|
39
|
+
throw new Error("Projected agent directory exceeds the 64 MiB scan byte bound.");
|
|
40
|
+
const introduced = new Set();
|
|
41
|
+
for (const { target } of plan.writes) {
|
|
42
|
+
const boundary = isPathInside(root, target) ? root : sessionRoot;
|
|
43
|
+
for (let entry = target; entry !== boundary; entry = path.dirname(entry)) {
|
|
44
|
+
if (introduced.has(entry) || (!isRemoved(entry) && (entries.has(entry) || routingDirectories.has(entry))))
|
|
45
|
+
break;
|
|
46
|
+
introduced.add(entry);
|
|
47
|
+
}
|
|
48
|
+
}
|
|
49
|
+
const canTraverse = compileSyncPolicyTraversal(options);
|
|
50
|
+
const walked = new Map([
|
|
51
|
+
[root, true],
|
|
52
|
+
[sessionRoot, scanSessions],
|
|
53
|
+
]);
|
|
54
|
+
const willWalk = (directory) => {
|
|
55
|
+
const known = walked.get(directory);
|
|
56
|
+
if (known !== undefined)
|
|
57
|
+
return known;
|
|
58
|
+
let value = false;
|
|
59
|
+
if (!introduced.has(directory))
|
|
60
|
+
value = walkedDirectories.has(directory) && !isRemoved(directory);
|
|
61
|
+
else if (isPathInside(sessionRoot, directory)) {
|
|
62
|
+
value =
|
|
63
|
+
canTraverse(`sessions/${toPosix(path.relative(sessionRoot, directory))}`) && willWalk(path.dirname(directory));
|
|
64
|
+
}
|
|
65
|
+
else if (isPathInside(root, directory)) {
|
|
66
|
+
const relative = toPosix(path.relative(root, directory));
|
|
67
|
+
value = !/^sessions(?:\/|$)/iu.test(relative) && canTraverse(relative) && willWalk(path.dirname(directory));
|
|
68
|
+
}
|
|
69
|
+
walked.set(directory, value);
|
|
70
|
+
return value;
|
|
71
|
+
};
|
|
72
|
+
for (const target of introduced) {
|
|
73
|
+
if (willWalk(path.dirname(target)))
|
|
74
|
+
count++;
|
|
75
|
+
}
|
|
76
|
+
if (count > MAX_AGENT_DIRECTORY_ENTRIES)
|
|
77
|
+
throw new Error("Projected agent directory exceeds the scan entry bound.");
|
|
78
|
+
}
|