@narumitw/pi-sync 0.61.0 → 0.63.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 +46 -25
- package/dist/chunks/{chunk-277VHM2P.ts → chunk-27F5W7DX.ts} +7 -3
- package/dist/chunks/chunk-27F5W7DX.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-CKUGCBHP.ts +114 -0
- package/dist/chunks/chunk-CKUGCBHP.ts.map +7 -0
- package/dist/chunks/chunk-EJDDRS6O.ts +17 -0
- package/dist/chunks/chunk-EJDDRS6O.ts.map +7 -0
- package/dist/chunks/{chunk-JVFCVPPD.ts → chunk-EXDWEFIJ.ts} +87 -147
- package/dist/chunks/chunk-EXDWEFIJ.ts.map +7 -0
- package/dist/chunks/{chunk-4SR3ZYHA.ts → chunk-GELYN4UC.ts} +2 -2
- package/dist/chunks/{chunk-2ZGV5KNC.ts → chunk-HSCAFOT5.ts} +15 -12
- package/dist/chunks/chunk-HSCAFOT5.ts.map +7 -0
- package/dist/chunks/{chunk-YQSFJLJ3.ts → chunk-IBXEM35O.ts} +4 -3
- package/dist/chunks/{chunk-2JHAN3ZO.ts → chunk-JREGWM2B.ts} +3 -3
- package/dist/chunks/chunk-MANNB4WZ.ts +36 -0
- package/dist/chunks/chunk-MANNB4WZ.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-ZVGRZD4C.ts → chunk-R3SAMXB6.ts} +2 -2
- package/dist/chunks/{chunk-4KTJLJSJ.ts → chunk-S6FUUWV5.ts} +148 -21
- package/dist/chunks/chunk-S6FUUWV5.ts.map +7 -0
- package/dist/chunks/{chunk-PJMM75W5.ts → chunk-SXCH4Q5O.ts} +41 -23
- package/dist/chunks/chunk-SXCH4Q5O.ts.map +7 -0
- package/dist/chunks/{chunk-RNIEVDTR.ts → chunk-T3LABNHF.ts} +2 -2
- package/dist/chunks/{chunk-E3YLRLJM.ts → chunk-UGS6RYDZ.ts} +14 -12
- package/dist/chunks/chunk-UGS6RYDZ.ts.map +7 -0
- package/dist/chunks/{chunk-S3R3W3KF.ts → chunk-UMPA5S7X.ts} +2 -2
- package/dist/chunks/{chunk-EMVZL4PH.ts → chunk-V76KCWHO.ts} +7 -5
- package/dist/chunks/{chunk-EMVZL4PH.ts.map → chunk-V76KCWHO.ts.map} +1 -1
- package/dist/chunks/{chunk-S7F4FOAS.ts → chunk-VQ47JBLF.ts} +5 -5
- package/dist/chunks/{chunk-WP2M67XZ.ts → chunk-WBIOSK76.ts} +2 -2
- package/dist/chunks/chunk-WFBHWYI6.ts +18 -0
- package/dist/chunks/chunk-WFBHWYI6.ts.map +7 -0
- package/dist/chunks/{chunk-HEB6YT64.ts → chunk-ZO52JM4H.ts} +2 -2
- package/dist/chunks/{command-handler-K4XJOGYC.ts → command-handler-VINMQ7ZM.ts} +26 -43
- package/dist/chunks/command-handler-VINMQ7ZM.ts.map +7 -0
- package/dist/chunks/{conflict-review-IGKY2TOJ.ts → conflict-review-PCDFRIVB.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-MGGVOKM7.ts → file-selection-KID23GAD.ts} +21 -14
- package/dist/chunks/file-selection-KID23GAD.ts.map +7 -0
- package/dist/chunks/manager-result-dispatcher-GDB74IOP.ts +20 -0
- package/dist/chunks/{manager-ui-KYQ5AZJS.ts → manager-ui-YHDECYKI.ts} +454 -167
- package/dist/chunks/manager-ui-YHDECYKI.ts.map +7 -0
- package/dist/chunks/remote-selection-ui-GPITNEBF.ts +19 -0
- package/dist/chunks/{setup-switch-D6K6PBPC.ts → setup-switch-L54X5U5I.ts} +7 -7
- package/dist/chunks/{setup-wizard-4DYUDHJR.ts → setup-wizard-YO4MCPUF.ts} +15 -11
- package/dist/chunks/{setup-wizard-4DYUDHJR.ts.map → setup-wizard-YO4MCPUF.ts.map} +1 -1
- 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-J3CRBORL.ts → sync-inspection-H2WX5SUT.ts} +5 -5
- package/dist/chunks/{sync-operations-BVWYYKH3.ts → sync-operations-2MZAYHL2.ts} +13 -9
- package/dist/chunks/sync-operations-2MZAYHL2.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 +383 -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 +746 -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 +252 -0
- package/dist/daemon/dist/engine/ignore/ignore-migration-prompt.js +139 -0
- package/dist/daemon/dist/engine/ignore/ignore-settings.js +393 -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 +344 -0
- package/dist/daemon/dist/engine/settings/settings-management.js +199 -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 +165 -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 +296 -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 +855 -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 +85 -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 +139 -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 +538 -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-acceptance.js +29 -0
- package/dist/daemon/dist/engine/sync/merge-apply.js +532 -0
- package/dist/daemon/dist/engine/sync/merge-journal.js +314 -0
- package/dist/daemon/dist/engine/sync/merge-transfer-error.js +64 -0
- package/dist/daemon/dist/engine/sync/merged-sync.js +666 -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 +119 -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 +47 -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 +754 -0
- package/dist/daemon/dist/engine/sync/sync-operations.js +3 -0
- package/dist/daemon/dist/engine/sync/sync-policy.js +646 -0
- package/dist/daemon/dist/engine/sync/sync-queries.js +212 -0
- package/dist/daemon/dist/engine/sync/sync-state.js +124 -0
- package/dist/daemon/dist/engine/sync/text-merge.js +124 -0
- package/dist/daemon/dist/engine/sync/transfer-freshness.js +82 -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 +441 -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 +324 -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 +994 -0
- package/dist/daemon/dist/service-host.js +175 -0
- package/dist/daemon/dist/upgrade-repair.js +55 -0
- package/dist/daemon/package.json +3 -0
- package/dist/index.ts +40 -37
- package/dist/index.ts.map +4 -4
- package/dist/package.json +6 -0
- package/docs/daemon-migration.md +27 -16
- package/package.json +9 -3
- package/skills/configure-pi-sync/SKILL.md +24 -46
- package/skills/configure-pi-sync/references/configuration.md +62 -22
- package/src/commands/command-execution.ts +13 -9
- package/src/commands/command-types.ts +1 -0
- package/src/daemon/pi-client.ts +4 -2
- package/src/settings/config.ts +2 -0
- package/src/sync/automatic-sync.ts +9 -2
- package/src/sync/setup-switch.ts +38 -15
- package/src/sync/startup-check.ts +7 -4
- package/src/sync/sync-operations.ts +18 -10
- package/src/ui/excluded-paths-ui.ts +6 -3
- package/src/ui/file-selection.ts +7 -2
- package/src/ui/ignore-file-guidance.ts +7 -0
- package/src/ui/ignore-file-ui.ts +97 -0
- package/src/ui/ignore-migration-prompt.ts +65 -0
- package/src/ui/manager-state.ts +40 -14
- package/src/ui/manager-ui.ts +41 -19
- package/src/ui/remote-selection-ui.ts +88 -28
- package/src/ui/settings-review.ts +26 -0
- package/src/ui/settings-ui.ts +378 -224
- package/src/ui/setup/setup-actions.ts +16 -4
- package/src/ui/setup/setup-prompts.ts +55 -9
- package/src/ui/setup/setup-switcher.ts +24 -11
- package/src/ui/storage-connections-ui.ts +50 -20
- package/src/ui/sync-setups-ui.ts +65 -30
- package/dist/chunks/chunk-277VHM2P.ts.map +0 -7
- package/dist/chunks/chunk-2ZGV5KNC.ts.map +0 -7
- package/dist/chunks/chunk-4KTJLJSJ.ts.map +0 -7
- package/dist/chunks/chunk-E3YLRLJM.ts.map +0 -7
- package/dist/chunks/chunk-ECYCNZOE.ts +0 -12
- package/dist/chunks/chunk-ECYCNZOE.ts.map +0 -7
- package/dist/chunks/chunk-JVFCVPPD.ts.map +0 -7
- package/dist/chunks/chunk-PJMM75W5.ts.map +0 -7
- package/dist/chunks/command-handler-K4XJOGYC.ts.map +0 -7
- package/dist/chunks/file-selection-MGGVOKM7.ts.map +0 -7
- package/dist/chunks/manager-result-dispatcher-4VJVDEUD.ts +0 -19
- package/dist/chunks/manager-ui-KYQ5AZJS.ts.map +0 -7
- package/dist/chunks/remote-selection-ui-W3KOZM3W.ts +0 -18
- package/dist/chunks/sync-operations-BVWYYKH3.ts.map +0 -7
- /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-4SR3ZYHA.ts.map → chunk-GELYN4UC.ts.map} +0 -0
- /package/dist/chunks/{chunk-YQSFJLJ3.ts.map → chunk-IBXEM35O.ts.map} +0 -0
- /package/dist/chunks/{chunk-2JHAN3ZO.ts.map → chunk-JREGWM2B.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-ZVGRZD4C.ts.map → chunk-R3SAMXB6.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-S7F4FOAS.ts.map → chunk-VQ47JBLF.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-IGKY2TOJ.ts.map → conflict-review-PCDFRIVB.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-4VJVDEUD.ts.map → manager-result-dispatcher-GDB74IOP.ts.map} +0 -0
- /package/dist/chunks/{remote-selection-ui-W3KOZM3W.ts.map → remote-selection-ui-GPITNEBF.ts.map} +0 -0
- /package/dist/chunks/{setup-switch-D6K6PBPC.ts.map → setup-switch-L54X5U5I.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-J3CRBORL.ts.map → sync-inspection-H2WX5SUT.ts.map} +0 -0
- /package/dist/chunks/{sync-state-2LMIXJ2C.ts.map → sync-state-QIT64TKV.ts.map} +0 -0
|
@@ -0,0 +1,383 @@
|
|
|
1
|
+
import { createHmac, randomBytes } from "node:crypto";
|
|
2
|
+
import { closeSync, fstatSync, fsyncSync, lstatSync, mkdirSync, openSync, readFileSync, renameSync, rmSync, writeFileSync, } from "node:fs";
|
|
3
|
+
import path from "node:path";
|
|
4
|
+
import { syncCheckConfigFingerprint } from "./engine/settings/config.js";
|
|
5
|
+
import { stateDir } from "./engine/state/state-directory.js";
|
|
6
|
+
import { startupTransferEnabled } from "./engine/sync/agent-policy.js";
|
|
7
|
+
import { syncSessionsSelected } from "./engine/sync/sync-policy.js";
|
|
8
|
+
import { safeTerminalText } from "./engine/ui/terminal-text.js";
|
|
9
|
+
import { record } from "./protocol.js";
|
|
10
|
+
function file(root) {
|
|
11
|
+
return path.join(stateDir(root), "daemon.json");
|
|
12
|
+
}
|
|
13
|
+
export class ConsentPublicationError extends Error {
|
|
14
|
+
}
|
|
15
|
+
const FORMATTED_CONSENT_BYTES = 65_536;
|
|
16
|
+
// Each legacy grant already uses at least 64 bytes for its fingerprint. Reserving one byte per
|
|
17
|
+
// grant covers all legacy writes, including compact files, when `true` becomes `false`.
|
|
18
|
+
const MAX_CONSENT_BYTES = FORMATTED_CONSENT_BYTES + FORMATTED_CONSENT_BYTES / 64;
|
|
19
|
+
function compactConsentWithRevocationReserve(value) {
|
|
20
|
+
const compact = JSON.stringify(value);
|
|
21
|
+
const grantCount = Object.values(value.setups).filter((setup) => setup.afterExit).length;
|
|
22
|
+
if (Buffer.byteLength(compact) + grantCount > MAX_CONSENT_BYTES)
|
|
23
|
+
throw new Error("Daemon consent exceeds its storage bound; existing evidence retained.");
|
|
24
|
+
return compact;
|
|
25
|
+
}
|
|
26
|
+
function syncConsentDirectory(target) {
|
|
27
|
+
if (process.platform === "win32")
|
|
28
|
+
return;
|
|
29
|
+
const directory = openSync(path.dirname(target), "r");
|
|
30
|
+
try {
|
|
31
|
+
fsyncSync(directory);
|
|
32
|
+
}
|
|
33
|
+
finally {
|
|
34
|
+
closeSync(directory);
|
|
35
|
+
}
|
|
36
|
+
}
|
|
37
|
+
/** A small synchronous publication keeps approval, metadata revocation, and rename in one event-loop turn. */
|
|
38
|
+
function publishConsent(value) {
|
|
39
|
+
try {
|
|
40
|
+
const target = file(value.root);
|
|
41
|
+
const compact = compactConsentWithRevocationReserve(value);
|
|
42
|
+
const formatted = `${JSON.stringify(value, null, "\t")}\n`;
|
|
43
|
+
const content = Buffer.byteLength(formatted) <= FORMATTED_CONSENT_BYTES ? formatted : compact;
|
|
44
|
+
mkdirSync(path.dirname(target), { recursive: true, mode: 0o700 });
|
|
45
|
+
const temporary = `${target}.${process.pid}.${randomBytes(16).toString("hex")}.tmp`;
|
|
46
|
+
const handle = openSync(temporary, "wx", 0o600);
|
|
47
|
+
try {
|
|
48
|
+
try {
|
|
49
|
+
writeFileSync(handle, content);
|
|
50
|
+
fsyncSync(handle);
|
|
51
|
+
}
|
|
52
|
+
finally {
|
|
53
|
+
closeSync(handle);
|
|
54
|
+
}
|
|
55
|
+
renameSync(temporary, target);
|
|
56
|
+
syncConsentDirectory(target);
|
|
57
|
+
}
|
|
58
|
+
finally {
|
|
59
|
+
rmSync(temporary, { force: true });
|
|
60
|
+
}
|
|
61
|
+
}
|
|
62
|
+
catch (error) {
|
|
63
|
+
throw new ConsentPublicationError("Daemon consent publication failed; preserve the retained document.", {
|
|
64
|
+
cause: error,
|
|
65
|
+
});
|
|
66
|
+
}
|
|
67
|
+
}
|
|
68
|
+
/** A different interactive binding cannot silently resume an older unattended grant after exit/restart. */
|
|
69
|
+
export function revokeChangedBindings(root, peers) {
|
|
70
|
+
const consent = readConsent(root);
|
|
71
|
+
if (!consent)
|
|
72
|
+
return;
|
|
73
|
+
const interactive = peers.filter((peer) => peer.hasUI);
|
|
74
|
+
let changed = false;
|
|
75
|
+
for (const saved of Object.values(consent.setups)) {
|
|
76
|
+
if (saved.afterExit &&
|
|
77
|
+
interactive.some((peer) => JSON.stringify(peer.model) !== JSON.stringify(saved.model) ||
|
|
78
|
+
peer.sessionDir !== saved.sessionDir ||
|
|
79
|
+
peer.cwd !== saved.cwd)) {
|
|
80
|
+
saved.afterExit = false;
|
|
81
|
+
changed = true;
|
|
82
|
+
}
|
|
83
|
+
}
|
|
84
|
+
if (changed)
|
|
85
|
+
publishConsent(consent);
|
|
86
|
+
}
|
|
87
|
+
export function revokeAfterExitGrants(root) {
|
|
88
|
+
return revokeRetainedGrants(root);
|
|
89
|
+
}
|
|
90
|
+
export function revokeRetainedGrants(root) {
|
|
91
|
+
const consent = readConsent(root);
|
|
92
|
+
if (!consent)
|
|
93
|
+
return 0;
|
|
94
|
+
let revoked = 0;
|
|
95
|
+
for (const saved of Object.values(consent.setups)) {
|
|
96
|
+
if (!saved.afterExit)
|
|
97
|
+
continue;
|
|
98
|
+
saved.afterExit = false;
|
|
99
|
+
revoked += 1;
|
|
100
|
+
}
|
|
101
|
+
if (revoked)
|
|
102
|
+
publishConsent(consent);
|
|
103
|
+
// A prior attempt may have renamed the synced file but failed to sync the directory.
|
|
104
|
+
else
|
|
105
|
+
syncConsentDirectory(file(root));
|
|
106
|
+
return revoked;
|
|
107
|
+
}
|
|
108
|
+
export function readConsent(root) {
|
|
109
|
+
const target = file(root);
|
|
110
|
+
let handle;
|
|
111
|
+
try {
|
|
112
|
+
const before = lstatSync(target);
|
|
113
|
+
if (!before.isFile() ||
|
|
114
|
+
before.isSymbolicLink() ||
|
|
115
|
+
before.size > MAX_CONSENT_BYTES ||
|
|
116
|
+
(process.getuid && before.uid !== process.getuid()) ||
|
|
117
|
+
(process.platform !== "win32" && before.mode & 0o077))
|
|
118
|
+
throw new Error("Unsafe daemon consent document; evidence retained.");
|
|
119
|
+
handle = openSync(target, "r");
|
|
120
|
+
const opened = fstatSync(handle);
|
|
121
|
+
if (opened.dev !== before.dev || opened.ino !== before.ino)
|
|
122
|
+
throw new Error("Daemon consent changed while opening.");
|
|
123
|
+
const value = JSON.parse(readFileSync(handle, "utf8"));
|
|
124
|
+
if (!record(value) ||
|
|
125
|
+
value.version !== 1 ||
|
|
126
|
+
value.root !== root ||
|
|
127
|
+
value.handover !== true ||
|
|
128
|
+
typeof value.nonce !== "string" ||
|
|
129
|
+
!/^[a-f0-9]{64}$/u.test(value.nonce) ||
|
|
130
|
+
!record(value.setups))
|
|
131
|
+
throw new Error("Invalid daemon consent; evidence retained.");
|
|
132
|
+
for (const entry of Object.values(value.setups)) {
|
|
133
|
+
if (!record(entry) ||
|
|
134
|
+
typeof entry.fingerprint !== "string" ||
|
|
135
|
+
!/^[a-f0-9]{64}$/u.test(entry.fingerprint) ||
|
|
136
|
+
(entry.automaticFingerprint !== undefined &&
|
|
137
|
+
(typeof entry.automaticFingerprint !== "string" || !/^[a-f0-9]{64}$/u.test(entry.automaticFingerprint))) ||
|
|
138
|
+
typeof entry.afterExit !== "boolean" ||
|
|
139
|
+
(entry.cwd !== undefined &&
|
|
140
|
+
(typeof entry.cwd !== "string" || !path.isAbsolute(entry.cwd) || entry.cwd.includes("\0"))) ||
|
|
141
|
+
(entry.sessionDir !== undefined &&
|
|
142
|
+
(typeof entry.sessionDir !== "string" || !path.isAbsolute(entry.sessionDir))) ||
|
|
143
|
+
(entry.model !== undefined &&
|
|
144
|
+
(!record(entry.model) ||
|
|
145
|
+
typeof entry.model.provider !== "string" ||
|
|
146
|
+
typeof entry.model.id !== "string" ||
|
|
147
|
+
typeof entry.model.signature !== "string" ||
|
|
148
|
+
!/^[a-f0-9]{64}$/u.test(entry.model.signature) ||
|
|
149
|
+
!["stored", "environment", "models_json_key", "models_json_command"].includes(String(entry.model.authSource)))))
|
|
150
|
+
throw new Error("Invalid daemon setup consent.");
|
|
151
|
+
}
|
|
152
|
+
const consent = value;
|
|
153
|
+
compactConsentWithRevocationReserve(consent);
|
|
154
|
+
return consent;
|
|
155
|
+
}
|
|
156
|
+
catch (error) {
|
|
157
|
+
if (error.code === "ENOENT")
|
|
158
|
+
return undefined;
|
|
159
|
+
throw error;
|
|
160
|
+
}
|
|
161
|
+
finally {
|
|
162
|
+
if (handle !== undefined)
|
|
163
|
+
closeSync(handle);
|
|
164
|
+
}
|
|
165
|
+
}
|
|
166
|
+
export function consentFingerprint(root, config) {
|
|
167
|
+
const consent = readConsent(root);
|
|
168
|
+
if (!consent)
|
|
169
|
+
throw new Error("Review daemon handover first.");
|
|
170
|
+
// Keep the original digest domain across package renames; never persist the internal freshness token.
|
|
171
|
+
return createHmac("sha256", consent.nonce)
|
|
172
|
+
.update("sync-daemon-consent/v1")
|
|
173
|
+
.update(syncCheckConfigFingerprint(config))
|
|
174
|
+
.digest("hex");
|
|
175
|
+
}
|
|
176
|
+
export function automaticPolicyAuthorized(root, config) {
|
|
177
|
+
if (config.reservedIgnoreNames === undefined)
|
|
178
|
+
return true;
|
|
179
|
+
const saved = readConsent(root)?.setups[config.setupName];
|
|
180
|
+
return saved?.automaticFingerprint !== undefined && saved.automaticFingerprint === consentFingerprint(root, config);
|
|
181
|
+
}
|
|
182
|
+
export async function reviewAutomaticPolicy(execution, config, expectedFingerprint) {
|
|
183
|
+
if (consentFingerprint(execution.root, config) !== expectedFingerprint)
|
|
184
|
+
throw new Error("Setup changed; reopen the automatic transfer review.");
|
|
185
|
+
if (config.reservedIgnoreNames === undefined || automaticPolicyAuthorized(execution.root, config))
|
|
186
|
+
return false;
|
|
187
|
+
const consent = readConsent(execution.root);
|
|
188
|
+
if (!consent)
|
|
189
|
+
return false;
|
|
190
|
+
const consentToken = JSON.stringify(consent);
|
|
191
|
+
const peers = participantBinding(execution);
|
|
192
|
+
const binding = JSON.stringify(execution.facts());
|
|
193
|
+
const { loadConfigForCheck } = await import("./engine/settings/config.js");
|
|
194
|
+
const fingerprint = syncCheckConfigFingerprint(config);
|
|
195
|
+
const validate = () => {
|
|
196
|
+
execution.validate();
|
|
197
|
+
execution.signal.throwIfAborted();
|
|
198
|
+
if (participantBinding(execution) !== peers ||
|
|
199
|
+
JSON.stringify(execution.facts()) !== binding ||
|
|
200
|
+
JSON.stringify(readConsent(execution.root)) !== consentToken)
|
|
201
|
+
throw new Error("Participants or consent changed during automatic transfer review.");
|
|
202
|
+
};
|
|
203
|
+
const current = await loadConfigForCheck(undefined, execution.signal);
|
|
204
|
+
validate();
|
|
205
|
+
if (syncCheckConfigFingerprint(current) !== fingerprint)
|
|
206
|
+
return false;
|
|
207
|
+
const modes = [
|
|
208
|
+
startupTransferEnabled(config) && "Automatic transfer at startup",
|
|
209
|
+
config.automatic &&
|
|
210
|
+
syncSessionsSelected(config) &&
|
|
211
|
+
"Push selected files on shutdown (including sessions when selected)",
|
|
212
|
+
config.continuous && config.agentMode && "Continuous sync and Autonomous model requests",
|
|
213
|
+
].filter(Boolean);
|
|
214
|
+
if (modes.length === 0)
|
|
215
|
+
throw new Error("No automatic transfer mode is enabled under the current local rules.");
|
|
216
|
+
const accepted = await execution.request("confirm", {
|
|
217
|
+
title: "Allow the enabled automatic sync modes for this setup?",
|
|
218
|
+
message: [
|
|
219
|
+
`Setup: ${safeTerminalText(config.setupName)}`,
|
|
220
|
+
`Destination: ${safeTerminalText(config.connectionName)} · ${safeTerminalText(config.storagePath)}`,
|
|
221
|
+
...(config.ignorePolicy
|
|
222
|
+
? [
|
|
223
|
+
`Local ignore file: ${safeTerminalText(config.ignorePolicy.filename)}`,
|
|
224
|
+
"Review this file before you approve its scope.",
|
|
225
|
+
]
|
|
226
|
+
: []),
|
|
227
|
+
`Enabled modes: ${modes.join("; ")}`,
|
|
228
|
+
"Their usual baseline, remote selection, and ownership checks still apply. This does not allow work after the last Pi exits. A settings or rule-file change requires a new review.",
|
|
229
|
+
].join("\n"),
|
|
230
|
+
});
|
|
231
|
+
validate();
|
|
232
|
+
if (accepted !== true)
|
|
233
|
+
return false;
|
|
234
|
+
const latest = await loadConfigForCheck(undefined, execution.signal);
|
|
235
|
+
await config.validateIgnorePolicy?.();
|
|
236
|
+
validate();
|
|
237
|
+
if (syncCheckConfigFingerprint(latest) !== fingerprint)
|
|
238
|
+
throw new Error("Setup changed during automatic transfer review.");
|
|
239
|
+
const saved = consent.setups[config.setupName];
|
|
240
|
+
const automaticFingerprint = consentFingerprint(execution.root, config);
|
|
241
|
+
publishConsent({
|
|
242
|
+
...consent,
|
|
243
|
+
setups: {
|
|
244
|
+
...consent.setups,
|
|
245
|
+
[config.setupName]: {
|
|
246
|
+
...saved,
|
|
247
|
+
fingerprint: saved?.fingerprint ?? automaticFingerprint,
|
|
248
|
+
afterExit: saved?.afterExit ?? false,
|
|
249
|
+
automaticFingerprint,
|
|
250
|
+
},
|
|
251
|
+
},
|
|
252
|
+
});
|
|
253
|
+
return true;
|
|
254
|
+
}
|
|
255
|
+
export async function captureAutomaticAuthorization(execution, expectedFingerprint) {
|
|
256
|
+
const consent = readConsent(execution.root);
|
|
257
|
+
if (!consent)
|
|
258
|
+
throw new Error("Automatic work requires reviewed daemon handover first.");
|
|
259
|
+
const consentToken = JSON.stringify(consent);
|
|
260
|
+
const peers = participantBinding(execution);
|
|
261
|
+
const { loadConfigForCheck } = await import("./engine/settings/config.js");
|
|
262
|
+
const validateParticipants = () => {
|
|
263
|
+
execution.validate();
|
|
264
|
+
if (participantBinding(execution) !== peers || JSON.stringify(readConsent(execution.root)) !== consentToken)
|
|
265
|
+
throw new Error("Daemon participants or consent changed before automatic work completed.");
|
|
266
|
+
};
|
|
267
|
+
validateParticipants();
|
|
268
|
+
const config = await loadConfigForCheck(undefined, execution.signal);
|
|
269
|
+
validateParticipants();
|
|
270
|
+
const fingerprint = syncCheckConfigFingerprint(config);
|
|
271
|
+
if (expectedFingerprint !== undefined ? fingerprint !== expectedFingerprint : config.reservedIgnoreNames !== undefined)
|
|
272
|
+
throw new Error("Settings changed before automatic work; use the reviewed foreground command.");
|
|
273
|
+
if (!automaticPolicyAuthorized(execution.root, config))
|
|
274
|
+
throw new Error("Automatic transfer requires separate approval for the current setup and policy. Run reviewed /sync first.");
|
|
275
|
+
const validate = (current) => {
|
|
276
|
+
validateParticipants();
|
|
277
|
+
if (current && syncCheckConfigFingerprint(current) !== fingerprint)
|
|
278
|
+
throw new Error("Settings changed before automatic work completed.");
|
|
279
|
+
};
|
|
280
|
+
const refresh = async () => {
|
|
281
|
+
validate();
|
|
282
|
+
validate(await loadConfigForCheck(undefined, execution.signal));
|
|
283
|
+
};
|
|
284
|
+
return { config, validate, refresh };
|
|
285
|
+
}
|
|
286
|
+
export function participantBinding(execution) {
|
|
287
|
+
return JSON.stringify(execution.participants().toSorted((left, right) => left.id.localeCompare(right.id)));
|
|
288
|
+
}
|
|
289
|
+
export async function requireHandover(execution) {
|
|
290
|
+
if (readConsent(execution.root))
|
|
291
|
+
return;
|
|
292
|
+
const accepted = await execution.request("confirm", {
|
|
293
|
+
title: "Adopt pi-sync-daemon as the synchronization owner?",
|
|
294
|
+
message: "Close or upgrade every older Pi process before continuing. An idle older Pi cannot be detected from an absent sync lock. The daemon will own transfers; Pi keeps presentation and live file queues. Autonomous resolution uses independently available auth.json/models.json configuration and the environment captured when the service starts, not Pi runtime accounts or providers. Credential commands run non-interactively from the canonical agent root. Review this independent provider/account configuration; restart the service after changing its environment. This does not authorize transfers or model requests after the last Pi exits.",
|
|
295
|
+
});
|
|
296
|
+
execution.validate();
|
|
297
|
+
if (accepted !== true)
|
|
298
|
+
throw new Error("Daemon handover was not approved; no transfer was started.");
|
|
299
|
+
const { withLock } = await import("./engine/state/lock.js");
|
|
300
|
+
await withLock("daemon-handover", async () => {
|
|
301
|
+
execution.validate();
|
|
302
|
+
if (!readConsent(execution.root))
|
|
303
|
+
publishConsent({
|
|
304
|
+
version: 1,
|
|
305
|
+
root: execution.root,
|
|
306
|
+
handover: true,
|
|
307
|
+
nonce: randomBytes(32).toString("hex"),
|
|
308
|
+
setups: {},
|
|
309
|
+
});
|
|
310
|
+
});
|
|
311
|
+
}
|
|
312
|
+
export async function authorizeAfterExit(execution, payload) {
|
|
313
|
+
if (!record(payload) || typeof payload.afterExit !== "boolean" || typeof payload.fingerprint !== "string")
|
|
314
|
+
throw new Error("Invalid daemon authorization.");
|
|
315
|
+
await requireHandover(execution);
|
|
316
|
+
const peers = participantBinding(execution);
|
|
317
|
+
const { loadConfigForCheck } = await import("./engine/settings/config.js");
|
|
318
|
+
const config = await loadConfigForCheck(undefined, execution.signal);
|
|
319
|
+
execution.validate();
|
|
320
|
+
if (participantBinding(execution) !== peers)
|
|
321
|
+
throw new Error("Participants changed during daemon authorization.");
|
|
322
|
+
const facts = execution.facts();
|
|
323
|
+
const binding = JSON.stringify({ model: facts?.model, sessionDir: facts?.sessionDir, cwd: facts?.cwd });
|
|
324
|
+
const fingerprint = consentFingerprint(execution.root, config);
|
|
325
|
+
const policyGeneration = JSON.stringify(readConsent(execution.root));
|
|
326
|
+
if (fingerprint !== payload.fingerprint)
|
|
327
|
+
throw new Error("Setup changed; reopen the daemon authorization review.");
|
|
328
|
+
if (payload.afterExit && (!config.continuous || !config.agentMode))
|
|
329
|
+
throw new Error("Enable the reviewed Autonomous and Continuous policies first.");
|
|
330
|
+
const paths = (rules) => rules?.map(safeTerminalText).join(", ") || "none";
|
|
331
|
+
const selection = config.ignorePolicy
|
|
332
|
+
? [
|
|
333
|
+
"Selected content: eligible files in the Pi agent directory",
|
|
334
|
+
`Local ignore file: ${safeTerminalText(config.ignorePolicy.filename)}`,
|
|
335
|
+
`Session opt-in: ${config.ignorePolicy.sessions ? "On — ignore rules still apply" : "Off"}`,
|
|
336
|
+
].join("\n")
|
|
337
|
+
: config.scope === "agent-directory"
|
|
338
|
+
? [
|
|
339
|
+
"Selected content: all eligible files in the Pi agent directory",
|
|
340
|
+
`Excluded paths: ${paths(config.exclude)}`,
|
|
341
|
+
`Re-included paths: ${paths(config.reinclude)}`,
|
|
342
|
+
`Session opt-in: ${config.include.includes("sessions") ? "On — path rules still apply" : "Off"}`,
|
|
343
|
+
].join("\n")
|
|
344
|
+
: `Selected content: ${paths(config.include)}`;
|
|
345
|
+
const accepted = await execution.request("confirm", {
|
|
346
|
+
title: payload.afterExit ? "Allow continuous sync after all Pi clients exit?" : "Revoke unattended sync?",
|
|
347
|
+
message: `Setup: ${safeTerminalText(config.setupName)}\n${selection}\nModel: ${safeTerminalText(facts?.model?.provider ?? "unavailable")}/${safeTerminalText(facts?.model?.id ?? "unavailable")}\n\n${payload.afterExit ? `The daemon may transfer selected private content and send conflict evidence to this independently available provider after Pi exits. Existing budgets, exclusions, journals, and stale-evidence checks still apply. No parent conversation or tools are sent. Changing this setup or model invalidates this permission.${config.reservedIgnoreNames !== undefined ? " This also approves the enabled automatic modes for this setup while Pi clients are attached." : ""}` : "The daemon may remain alive, but no new transfer or model request may start with zero clients."}`,
|
|
348
|
+
});
|
|
349
|
+
execution.validate();
|
|
350
|
+
if (accepted !== true)
|
|
351
|
+
return undefined;
|
|
352
|
+
const latest = await loadConfigForCheck(undefined, execution.signal);
|
|
353
|
+
execution.validate();
|
|
354
|
+
await config.validateIgnorePolicy?.();
|
|
355
|
+
execution.validate();
|
|
356
|
+
if (participantBinding(execution) !== peers ||
|
|
357
|
+
consentFingerprint(execution.root, latest) !== fingerprint ||
|
|
358
|
+
JSON.stringify({
|
|
359
|
+
model: execution.facts()?.model,
|
|
360
|
+
sessionDir: execution.facts()?.sessionDir,
|
|
361
|
+
cwd: execution.facts()?.cwd,
|
|
362
|
+
}) !== binding)
|
|
363
|
+
throw new Error("Setup or model changed during daemon authorization.");
|
|
364
|
+
const consent = readConsent(execution.root);
|
|
365
|
+
if (!consent || JSON.stringify(consent) !== policyGeneration)
|
|
366
|
+
throw new Error("Daemon authorization changed; reopen its review.");
|
|
367
|
+
publishConsent({
|
|
368
|
+
...consent,
|
|
369
|
+
setups: {
|
|
370
|
+
...consent.setups,
|
|
371
|
+
[config.setupName]: {
|
|
372
|
+
...consent.setups[config.setupName],
|
|
373
|
+
fingerprint,
|
|
374
|
+
model: facts?.model,
|
|
375
|
+
sessionDir: facts?.sessionDir,
|
|
376
|
+
cwd: facts?.cwd,
|
|
377
|
+
afterExit: payload.afterExit,
|
|
378
|
+
...(payload.afterExit && config.reservedIgnoreNames !== undefined ? { automaticFingerprint: fingerprint } : {}),
|
|
379
|
+
},
|
|
380
|
+
},
|
|
381
|
+
});
|
|
382
|
+
return { config, afterExit: payload.afterExit };
|
|
383
|
+
}
|
|
@@ -0,0 +1,91 @@
|
|
|
1
|
+
import fs from "node:fs/promises";
|
|
2
|
+
import path from "node:path";
|
|
3
|
+
import { privateDirectory } from "./identity.js";
|
|
4
|
+
/** Node/libuv unlinks its original socket pathname, even if another file replaced it. */
|
|
5
|
+
export function endpointOwner(server, paths) {
|
|
6
|
+
let created = false;
|
|
7
|
+
let retired = false;
|
|
8
|
+
let retained;
|
|
9
|
+
let directoryIdentity;
|
|
10
|
+
let socketIdentity;
|
|
11
|
+
async function retire() {
|
|
12
|
+
if (retired || !created || !paths.endpointDirectory)
|
|
13
|
+
return;
|
|
14
|
+
const namespace = path.dirname(paths.directory);
|
|
15
|
+
await privateDirectory(namespace);
|
|
16
|
+
const container = await fs.mkdtemp(path.join(namespace, `${path.basename(paths.directory).slice(0, 16)}-retained-`));
|
|
17
|
+
try {
|
|
18
|
+
// Move the entry itself, including any replacement symlink, without following it.
|
|
19
|
+
// Do this only after all jobs and release acknowledgements have drained.
|
|
20
|
+
await fs.rename(paths.endpointDirectory, path.join(container, "entry"));
|
|
21
|
+
retained = container;
|
|
22
|
+
retired = true;
|
|
23
|
+
}
|
|
24
|
+
catch (error) {
|
|
25
|
+
await fs.rmdir(container).catch(() => { });
|
|
26
|
+
if (error.code === "ENOENT") {
|
|
27
|
+
retired = true;
|
|
28
|
+
return;
|
|
29
|
+
}
|
|
30
|
+
// Closing here would let Node delete an unknown replacement. Keep the owner
|
|
31
|
+
// alive and permit an explicit stop retry after runtime permissions are fixed.
|
|
32
|
+
throw new Error("Cannot retire the IPC namespace safely. Restore runtime directory permissions, then stop again; evidence retained.", { cause: error });
|
|
33
|
+
}
|
|
34
|
+
}
|
|
35
|
+
async function removeKnownRetiredSocket() {
|
|
36
|
+
if (!retained || !directoryIdentity)
|
|
37
|
+
return;
|
|
38
|
+
const entry = path.join(retained, "entry");
|
|
39
|
+
const directory = await fs.lstat(entry).catch(() => undefined);
|
|
40
|
+
if (!directory?.isDirectory() ||
|
|
41
|
+
directory.isSymbolicLink() ||
|
|
42
|
+
directory.dev !== directoryIdentity.dev ||
|
|
43
|
+
directory.ino !== directoryIdentity.ino)
|
|
44
|
+
return;
|
|
45
|
+
const target = path.join(entry, "api.sock");
|
|
46
|
+
const socket = await fs.lstat(target).catch(() => undefined);
|
|
47
|
+
if (socket?.isSocket() && socketIdentity && socket.dev === socketIdentity.dev && socket.ino === socketIdentity.ino)
|
|
48
|
+
await fs.unlink(target);
|
|
49
|
+
// Never recursively remove unknown entries or follow a replacement ancestor.
|
|
50
|
+
try {
|
|
51
|
+
await fs.rmdir(entry);
|
|
52
|
+
await fs.rmdir(retained);
|
|
53
|
+
retained = undefined;
|
|
54
|
+
}
|
|
55
|
+
catch (error) {
|
|
56
|
+
if (!["ENOTEMPTY", "EEXIST"].includes(String(error.code)))
|
|
57
|
+
throw error;
|
|
58
|
+
}
|
|
59
|
+
}
|
|
60
|
+
return {
|
|
61
|
+
async listen() {
|
|
62
|
+
if (paths.endpointDirectory) {
|
|
63
|
+
await fs.mkdir(paths.endpointDirectory, { mode: 0o700 });
|
|
64
|
+
created = true;
|
|
65
|
+
directoryIdentity = await fs.lstat(paths.endpointDirectory);
|
|
66
|
+
}
|
|
67
|
+
await new Promise((resolve, reject) => {
|
|
68
|
+
server.once("error", reject);
|
|
69
|
+
server.listen(paths.endpoint, resolve);
|
|
70
|
+
});
|
|
71
|
+
if (paths.endpointDirectory) {
|
|
72
|
+
const socket = await fs.lstat(paths.endpoint);
|
|
73
|
+
if (!socket.isSocket() || socket.isSymbolicLink())
|
|
74
|
+
throw new Error("IPC endpoint changed during bind; evidence retained.");
|
|
75
|
+
socketIdentity = socket;
|
|
76
|
+
// The exclusive 0700 parent is the access boundary. A pathname chmod after
|
|
77
|
+
// bind could instead follow an externally substituted symlink.
|
|
78
|
+
}
|
|
79
|
+
},
|
|
80
|
+
async close() {
|
|
81
|
+
await retire();
|
|
82
|
+
const closed = new Promise((resolve) => server.close(() => resolve()));
|
|
83
|
+
server.closeAllConnections();
|
|
84
|
+
await closed;
|
|
85
|
+
await removeKnownRetiredSocket();
|
|
86
|
+
},
|
|
87
|
+
get retainedDirectory() {
|
|
88
|
+
return retained;
|
|
89
|
+
},
|
|
90
|
+
};
|
|
91
|
+
}
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
export const createSyncBackend = async (config) => {
|
|
2
|
+
switch (config.backend.type) {
|
|
3
|
+
case "s3": {
|
|
4
|
+
const { S3SyncBackend } = await import("./s3/s3-backend.js");
|
|
5
|
+
return new S3SyncBackend(config.backend);
|
|
6
|
+
}
|
|
7
|
+
case "webdav": {
|
|
8
|
+
const { WebDavSyncBackend } = await import("./webdav/webdav-backend.js");
|
|
9
|
+
return new WebDavSyncBackend(config.backend);
|
|
10
|
+
}
|
|
11
|
+
case "git": {
|
|
12
|
+
const { GitSyncBackend } = await import("./git/git-backend.js");
|
|
13
|
+
return new GitSyncBackend(config.backend);
|
|
14
|
+
}
|
|
15
|
+
}
|
|
16
|
+
};
|
|
@@ -0,0 +1,38 @@
|
|
|
1
|
+
import { normalizeGitRemoteIdentity } from "./git/git-config.js";
|
|
2
|
+
import { normalizeWebDavIdentityUrl } from "./webdav/webdav-config.js";
|
|
3
|
+
export function backendIdentityCoordinates(config) {
|
|
4
|
+
switch (config.backend.type) {
|
|
5
|
+
case "s3":
|
|
6
|
+
return JSON.stringify([
|
|
7
|
+
"s3",
|
|
8
|
+
normalizeEndpointIdentity(config.backend.profile.endpoint),
|
|
9
|
+
config.backend.destination.bucket,
|
|
10
|
+
config.storagePath,
|
|
11
|
+
]);
|
|
12
|
+
case "git":
|
|
13
|
+
return JSON.stringify([
|
|
14
|
+
"git",
|
|
15
|
+
normalizeGitRemoteIdentity(config.backend.profile.remote),
|
|
16
|
+
config.backend.destination.branch,
|
|
17
|
+
config.storagePath,
|
|
18
|
+
]);
|
|
19
|
+
case "webdav":
|
|
20
|
+
return JSON.stringify([
|
|
21
|
+
"webdav",
|
|
22
|
+
normalizeWebDavIdentityUrl(config.backend.profile.url),
|
|
23
|
+
config.backend.profile.username,
|
|
24
|
+
config.storagePath,
|
|
25
|
+
]);
|
|
26
|
+
}
|
|
27
|
+
}
|
|
28
|
+
export function normalizeEndpointIdentity(endpoint) {
|
|
29
|
+
try {
|
|
30
|
+
const url = new URL(endpoint.trim());
|
|
31
|
+
url.hostname = url.hostname.toLowerCase();
|
|
32
|
+
url.pathname = url.pathname.replace(/\/+$/gu, "");
|
|
33
|
+
return url.toString().replace(/\/$/u, "");
|
|
34
|
+
}
|
|
35
|
+
catch {
|
|
36
|
+
return endpoint.trim();
|
|
37
|
+
}
|
|
38
|
+
}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|