@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,227 @@
|
|
|
1
|
+
import { lstatSync } from "node:fs";
|
|
2
|
+
import fs from "node:fs/promises";
|
|
3
|
+
import path from "node:path";
|
|
4
|
+
import lockfile from "proper-lockfile";
|
|
5
|
+
import { getEngineRoot as getAgentDir } from "../host.js";
|
|
6
|
+
import { LOCK_GUARD_STALE_MS } from "./lock-policy.js";
|
|
7
|
+
import { LOCKFILE_FS_ADAPTER } from "./lockfile-fs.js";
|
|
8
|
+
const CANONICAL_DIRECTORY_NAME = "pi-sync";
|
|
9
|
+
const LEGACY_DIRECTORY_NAME = ".pisync";
|
|
10
|
+
const MIGRATION_LOCK_NAME = ".pi-sync-state-migration.lock";
|
|
11
|
+
const MIGRATION_LOCK_STALE_MS = 30_000;
|
|
12
|
+
const MIGRATION_LOCK_UPDATE_MS = 10_000;
|
|
13
|
+
const sharedStateDirectoryGuards = new Map();
|
|
14
|
+
export function stateDir(root = getAgentDir()) {
|
|
15
|
+
const roots = inspectStateRoots(root);
|
|
16
|
+
return roots.legacy ? legacyStateDir(root) : canonicalStateDir(root);
|
|
17
|
+
}
|
|
18
|
+
export function legacyStateDir(root = getAgentDir()) {
|
|
19
|
+
return path.join(root, LEGACY_DIRECTORY_NAME);
|
|
20
|
+
}
|
|
21
|
+
export function stateDirectoryMigrationNotice() {
|
|
22
|
+
const roots = inspectStateRoots();
|
|
23
|
+
if (!roots.legacy)
|
|
24
|
+
return undefined;
|
|
25
|
+
return "Legacy pi-sync state is still stored in .pisync. Close other Pi sessions, then run /sync migrate-state to move it to pi-sync/.";
|
|
26
|
+
}
|
|
27
|
+
export async function withStateDirectoryAccess(fn) {
|
|
28
|
+
const roots = inspectStateRoots();
|
|
29
|
+
if (!roots.legacy)
|
|
30
|
+
return fn();
|
|
31
|
+
return runWithStateDirectoryGuard(await acquireSharedStateDirectoryGuard(), fn);
|
|
32
|
+
}
|
|
33
|
+
export async function migrateLegacyStateDirectory() {
|
|
34
|
+
const initial = inspectStateRoots();
|
|
35
|
+
if (initial.canonical || !initial.legacy)
|
|
36
|
+
return { status: "ready" };
|
|
37
|
+
let guard;
|
|
38
|
+
try {
|
|
39
|
+
guard = await acquireStateDirectoryGuard();
|
|
40
|
+
}
|
|
41
|
+
catch (error) {
|
|
42
|
+
if (!isLockHeldError(error))
|
|
43
|
+
throw error;
|
|
44
|
+
return {
|
|
45
|
+
status: "deferred",
|
|
46
|
+
message: "pi-sync state migration was deferred because another state user is active. Close other Pi sessions and retry.",
|
|
47
|
+
};
|
|
48
|
+
}
|
|
49
|
+
return runWithStateDirectoryGuard(guard, async () => {
|
|
50
|
+
const roots = inspectStateRoots();
|
|
51
|
+
if (roots.canonical || !roots.legacy)
|
|
52
|
+
return { status: "ready" };
|
|
53
|
+
const legacyLock = path.join(legacyStateDir(), "lock");
|
|
54
|
+
const legacyGuardHeld = await lockfile.check(legacyLock, {
|
|
55
|
+
fs: LOCKFILE_FS_ADAPTER,
|
|
56
|
+
lockfilePath: `${legacyLock}.guard`,
|
|
57
|
+
realpath: false,
|
|
58
|
+
stale: LOCK_GUARD_STALE_MS,
|
|
59
|
+
});
|
|
60
|
+
// Idle continuous owners do not retain the exclusive migration guard.
|
|
61
|
+
// A live renewable lease still prevents moving their private state root.
|
|
62
|
+
const schedulerHeld = await lockfile.check(path.join(legacyStateDir(), "continuous", "scheduler"), {
|
|
63
|
+
fs: LOCKFILE_FS_ADAPTER,
|
|
64
|
+
realpath: false,
|
|
65
|
+
stale: LOCK_GUARD_STALE_MS,
|
|
66
|
+
});
|
|
67
|
+
if (legacyGuardHeld || schedulerHeld || (await pathExists(legacyLock))) {
|
|
68
|
+
return {
|
|
69
|
+
status: "deferred",
|
|
70
|
+
message: "pi-sync state migration was deferred because the legacy directory is busy. Close other Pi sessions, clear any confirmed stale sync lock, and retry.",
|
|
71
|
+
};
|
|
72
|
+
}
|
|
73
|
+
guard.throwIfCompromised();
|
|
74
|
+
await fs.rename(legacyStateDir(), canonicalStateDir());
|
|
75
|
+
guard.throwIfCompromised();
|
|
76
|
+
inspectStateRoots();
|
|
77
|
+
return {
|
|
78
|
+
status: "migrated",
|
|
79
|
+
message: `Migrated pi-sync state from ${legacyStateDir()} to ${canonicalStateDir()}.`,
|
|
80
|
+
};
|
|
81
|
+
});
|
|
82
|
+
}
|
|
83
|
+
async function acquireSharedStateDirectoryGuard() {
|
|
84
|
+
const key = migrationLockPath();
|
|
85
|
+
for (;;) {
|
|
86
|
+
let shared = sharedStateDirectoryGuards.get(key);
|
|
87
|
+
if (shared?.closing) {
|
|
88
|
+
await shared.closing;
|
|
89
|
+
continue;
|
|
90
|
+
}
|
|
91
|
+
if (!shared) {
|
|
92
|
+
shared = { guard: acquireStateDirectoryGuard(), users: 0 };
|
|
93
|
+
sharedStateDirectoryGuards.set(key, shared);
|
|
94
|
+
}
|
|
95
|
+
shared.users += 1;
|
|
96
|
+
let guard;
|
|
97
|
+
try {
|
|
98
|
+
guard = await shared.guard;
|
|
99
|
+
}
|
|
100
|
+
catch (error) {
|
|
101
|
+
if (sharedStateDirectoryGuards.get(key) === shared) {
|
|
102
|
+
sharedStateDirectoryGuards.delete(key);
|
|
103
|
+
}
|
|
104
|
+
throw error;
|
|
105
|
+
}
|
|
106
|
+
let released = false;
|
|
107
|
+
return {
|
|
108
|
+
throwIfCompromised: guard.throwIfCompromised,
|
|
109
|
+
release: async () => {
|
|
110
|
+
if (released)
|
|
111
|
+
return;
|
|
112
|
+
released = true;
|
|
113
|
+
shared.users -= 1;
|
|
114
|
+
if (shared.users > 0)
|
|
115
|
+
return;
|
|
116
|
+
const closing = guard.release();
|
|
117
|
+
shared.closing = closing;
|
|
118
|
+
try {
|
|
119
|
+
await closing;
|
|
120
|
+
}
|
|
121
|
+
finally {
|
|
122
|
+
if (sharedStateDirectoryGuards.get(key) === shared) {
|
|
123
|
+
sharedStateDirectoryGuards.delete(key);
|
|
124
|
+
}
|
|
125
|
+
}
|
|
126
|
+
},
|
|
127
|
+
};
|
|
128
|
+
}
|
|
129
|
+
}
|
|
130
|
+
/** Protect path selection and a synchronous access outside the engine host. */
|
|
131
|
+
export async function withStateDirectoryGuardForRoot(root, access) {
|
|
132
|
+
return runWithStateDirectoryGuard(await acquireStateDirectoryGuard(root), async () => access());
|
|
133
|
+
}
|
|
134
|
+
async function acquireStateDirectoryGuard(root = getAgentDir()) {
|
|
135
|
+
let compromisedError;
|
|
136
|
+
const release = await lockfile.lock(root, {
|
|
137
|
+
fs: LOCKFILE_FS_ADAPTER,
|
|
138
|
+
lockfilePath: migrationLockPath(root),
|
|
139
|
+
realpath: false,
|
|
140
|
+
stale: MIGRATION_LOCK_STALE_MS,
|
|
141
|
+
update: MIGRATION_LOCK_UPDATE_MS,
|
|
142
|
+
retries: { retries: 20, minTimeout: 10, maxTimeout: 50 },
|
|
143
|
+
onCompromised: (error) => {
|
|
144
|
+
compromisedError = error;
|
|
145
|
+
},
|
|
146
|
+
});
|
|
147
|
+
return {
|
|
148
|
+
release,
|
|
149
|
+
throwIfCompromised: () => {
|
|
150
|
+
if (compromisedError)
|
|
151
|
+
throw compromisedError;
|
|
152
|
+
},
|
|
153
|
+
};
|
|
154
|
+
}
|
|
155
|
+
async function runWithStateDirectoryGuard(guard, fn) {
|
|
156
|
+
let result;
|
|
157
|
+
let failed = false;
|
|
158
|
+
let failure;
|
|
159
|
+
try {
|
|
160
|
+
guard.throwIfCompromised();
|
|
161
|
+
result = await fn();
|
|
162
|
+
guard.throwIfCompromised();
|
|
163
|
+
}
|
|
164
|
+
catch (error) {
|
|
165
|
+
failed = true;
|
|
166
|
+
failure = error;
|
|
167
|
+
}
|
|
168
|
+
try {
|
|
169
|
+
await guard.release();
|
|
170
|
+
}
|
|
171
|
+
catch (error) {
|
|
172
|
+
if (!failed) {
|
|
173
|
+
failed = true;
|
|
174
|
+
failure = error;
|
|
175
|
+
}
|
|
176
|
+
}
|
|
177
|
+
if (failed)
|
|
178
|
+
throw failure;
|
|
179
|
+
return result;
|
|
180
|
+
}
|
|
181
|
+
function isLockHeldError(error) {
|
|
182
|
+
return error.code === "ELOCKED";
|
|
183
|
+
}
|
|
184
|
+
function canonicalStateDir(root = getAgentDir()) {
|
|
185
|
+
return path.join(root, CANONICAL_DIRECTORY_NAME);
|
|
186
|
+
}
|
|
187
|
+
function migrationLockPath(root = getAgentDir()) {
|
|
188
|
+
return path.join(root, MIGRATION_LOCK_NAME);
|
|
189
|
+
}
|
|
190
|
+
function inspectStateRoots(root = getAgentDir()) {
|
|
191
|
+
const canonicalPath = canonicalStateDir(root);
|
|
192
|
+
const legacyPath = legacyStateDir(root);
|
|
193
|
+
const canonical = isDirectoryRoot(canonicalPath, "canonical");
|
|
194
|
+
const legacy = isDirectoryRoot(legacyPath, "legacy");
|
|
195
|
+
if (canonical && legacy) {
|
|
196
|
+
throw new Error(`Both legacy ${legacyPath} and canonical ${canonicalPath} pi-sync state directories exist. Close all Pi sessions and reconcile the directories manually; pi-sync will not merge or delete either directory.`);
|
|
197
|
+
}
|
|
198
|
+
return { canonical, legacy };
|
|
199
|
+
}
|
|
200
|
+
function isDirectoryRoot(directory, label) {
|
|
201
|
+
try {
|
|
202
|
+
const entry = lstatSync(directory);
|
|
203
|
+
if (entry.isSymbolicLink()) {
|
|
204
|
+
throw new Error(`Refusing to use ${label} pi-sync state directory symbolic link: ${directory}`);
|
|
205
|
+
}
|
|
206
|
+
if (!entry.isDirectory()) {
|
|
207
|
+
throw new Error(`${label} pi-sync state path is not a directory: ${directory}`);
|
|
208
|
+
}
|
|
209
|
+
return true;
|
|
210
|
+
}
|
|
211
|
+
catch (error) {
|
|
212
|
+
if (error.code === "ENOENT")
|
|
213
|
+
return false;
|
|
214
|
+
throw error;
|
|
215
|
+
}
|
|
216
|
+
}
|
|
217
|
+
async function pathExists(filePath) {
|
|
218
|
+
try {
|
|
219
|
+
await fs.lstat(filePath);
|
|
220
|
+
return true;
|
|
221
|
+
}
|
|
222
|
+
catch (error) {
|
|
223
|
+
if (error.code === "ENOENT")
|
|
224
|
+
return false;
|
|
225
|
+
throw error;
|
|
226
|
+
}
|
|
227
|
+
}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|
|
@@ -0,0 +1,54 @@
|
|
|
1
|
+
import { createHash } from "node:crypto";
|
|
2
|
+
import fs from "node:fs/promises";
|
|
3
|
+
import path from "node:path";
|
|
4
|
+
import { backendIdentityCoordinates } from "../backends/backend-identity.js";
|
|
5
|
+
import { safeName } from "../paths.js";
|
|
6
|
+
import { readJsonIfExists, writeJson } from "./json-file.js";
|
|
7
|
+
import { stateDir } from "./state-directory.js";
|
|
8
|
+
const STATE_VERSION = 2;
|
|
9
|
+
/** Bounded observation token; do not retain the full baseline in passive UI state. */
|
|
10
|
+
export function syncStateFingerprint(state) {
|
|
11
|
+
return createHash("sha256").update(JSON.stringify(state)).digest("hex");
|
|
12
|
+
}
|
|
13
|
+
export async function readState(profile) {
|
|
14
|
+
return ((await readJsonIfExists(statePath(profile))) ?? {
|
|
15
|
+
version: STATE_VERSION,
|
|
16
|
+
profile,
|
|
17
|
+
lastFileHashes: {},
|
|
18
|
+
});
|
|
19
|
+
}
|
|
20
|
+
export async function writeState(profile, state) {
|
|
21
|
+
await writeJson(statePath(profile), state);
|
|
22
|
+
}
|
|
23
|
+
export async function readStateForConfig(config) {
|
|
24
|
+
return ((await readJsonIfExists(statePathForConfig(config))) ?? {
|
|
25
|
+
version: STATE_VERSION,
|
|
26
|
+
profile: config.snapshotIdentity,
|
|
27
|
+
lastFileHashes: {},
|
|
28
|
+
});
|
|
29
|
+
}
|
|
30
|
+
export async function writeStateForConfig(config, state, snapshot, validate = () => { }) {
|
|
31
|
+
validate();
|
|
32
|
+
if (snapshot) {
|
|
33
|
+
const { stageMergeBaseline } = await import("./merge-baseline-store.js");
|
|
34
|
+
validate();
|
|
35
|
+
await stageMergeBaseline(config, snapshot, state);
|
|
36
|
+
validate();
|
|
37
|
+
}
|
|
38
|
+
await writeJson(statePathForConfig(config), state);
|
|
39
|
+
validate();
|
|
40
|
+
}
|
|
41
|
+
export function statePathForConfig(config) {
|
|
42
|
+
const identity = backendIdentityCoordinates(config);
|
|
43
|
+
const hash = createHash("sha256").update(identity).digest("hex").slice(0, 16);
|
|
44
|
+
return path.join(stateDir(), "setups", `${config.backend.type}-${hash}.state.json`);
|
|
45
|
+
}
|
|
46
|
+
function statePath(profile) {
|
|
47
|
+
return path.join(stateDir(), `${safeName(profile)}.state.json`);
|
|
48
|
+
}
|
|
49
|
+
export function lockPath() {
|
|
50
|
+
return path.join(stateDir(), "lock");
|
|
51
|
+
}
|
|
52
|
+
export async function ensureStateDir() {
|
|
53
|
+
await fs.mkdir(stateDir(), { recursive: true });
|
|
54
|
+
}
|
|
@@ -0,0 +1,207 @@
|
|
|
1
|
+
import { createHash } from "node:crypto";
|
|
2
|
+
import { scanSnapshot } from "../snapshot/snapshot.js";
|
|
3
|
+
import { AGENT_INPUT_BYTES, AGENT_MAX_UNITS, AGENT_OUTPUT_BYTES } from "./agent-policy.js";
|
|
4
|
+
import { snapshotFile } from "./content-conflicts.js";
|
|
5
|
+
import { parseJsonObjectDocument, parseSettingsDocument } from "./json-document.js";
|
|
6
|
+
import { COUPLED_FIELDS, mergeSettingsJson } from "./settings-merge.js";
|
|
7
|
+
import { edits, isMergeTextPath, lines, text } from "./text-merge.js";
|
|
8
|
+
export const digest = (value) => createHash("sha256").update(JSON.stringify(value)).digest("hex");
|
|
9
|
+
function overlaps(a, b) {
|
|
10
|
+
return a.start === a.end || b.start === b.end
|
|
11
|
+
? a.start <= b.end && b.start <= a.end
|
|
12
|
+
: a.start < b.end && b.start < a.end;
|
|
13
|
+
}
|
|
14
|
+
/** Only overlapping edit regions are model-writable; all other bytes are assembled by code. */
|
|
15
|
+
function textParts(filePath, baseText, localText, remoteText) {
|
|
16
|
+
const base = lines(baseText);
|
|
17
|
+
const changes = [
|
|
18
|
+
...edits(base, lines(localText)).map((hunk) => ({ ...hunk, side: "local" })),
|
|
19
|
+
...edits(base, lines(remoteText)).map((hunk) => ({ ...hunk, side: "remote" })),
|
|
20
|
+
].sort((a, b) => a.start - b.start || a.end - b.end);
|
|
21
|
+
const clusters = [];
|
|
22
|
+
for (const change of changes) {
|
|
23
|
+
const last = clusters.at(-1);
|
|
24
|
+
if (last?.some((item) => overlaps(item, change)))
|
|
25
|
+
last.push(change);
|
|
26
|
+
else
|
|
27
|
+
clusters.push([change]);
|
|
28
|
+
}
|
|
29
|
+
const result = [];
|
|
30
|
+
let cursor = 0;
|
|
31
|
+
for (const cluster of clusters) {
|
|
32
|
+
const start = Math.min(...cluster.map((item) => item.start));
|
|
33
|
+
const end = Math.max(...cluster.map((item) => item.end));
|
|
34
|
+
result.push(base.slice(cursor, start).join(""));
|
|
35
|
+
const image = (side) => {
|
|
36
|
+
let position = start;
|
|
37
|
+
const output = [];
|
|
38
|
+
for (const hunk of cluster.filter((item) => item.side === side)) {
|
|
39
|
+
output.push(...base.slice(position, hunk.start), ...hunk.lines);
|
|
40
|
+
position = hunk.end;
|
|
41
|
+
}
|
|
42
|
+
return [...output, ...base.slice(position, end)].join("");
|
|
43
|
+
};
|
|
44
|
+
const local = image("local");
|
|
45
|
+
const remote = image("remote");
|
|
46
|
+
const original = base.slice(start, end).join("");
|
|
47
|
+
if (local === remote || local === original || remote === original)
|
|
48
|
+
result.push(local === original ? remote : local);
|
|
49
|
+
else
|
|
50
|
+
result.push({ id: "", path: filePath, kind: "text", base: original, local, remote });
|
|
51
|
+
cursor = end;
|
|
52
|
+
}
|
|
53
|
+
result.push(base.slice(cursor).join(""));
|
|
54
|
+
return result;
|
|
55
|
+
}
|
|
56
|
+
export function buildAgentEvidence(plan, local, remote, ancestors) {
|
|
57
|
+
const evidence = { units: [], files: [], unsupported: [] };
|
|
58
|
+
if (plan.kind !== "planned")
|
|
59
|
+
return evidence;
|
|
60
|
+
if (plan.conflicts.length > AGENT_MAX_UNITS)
|
|
61
|
+
throw new Error("Autonomous sync paused: too many conflict paths for bounded analysis.");
|
|
62
|
+
for (const conflict of plan.conflicts) {
|
|
63
|
+
try {
|
|
64
|
+
if (conflict.reason !== "both-changed")
|
|
65
|
+
throw new Error("unsupported conflict");
|
|
66
|
+
const versions = [ancestors, local.files, remote.files].map((files) => {
|
|
67
|
+
const file = files.find((item) => item.path === conflict.path);
|
|
68
|
+
if (!file)
|
|
69
|
+
throw new Error("missing ancestor/version");
|
|
70
|
+
const bytes = Buffer.from(file.contentBase64, "base64");
|
|
71
|
+
if (bytes.length > 8192)
|
|
72
|
+
throw new Error("file bound");
|
|
73
|
+
return text(bytes);
|
|
74
|
+
});
|
|
75
|
+
const [base, left, right] = versions;
|
|
76
|
+
const file = { path: conflict.path, base, local: left, remote: right };
|
|
77
|
+
let units;
|
|
78
|
+
if (conflict.path === "settings.json") {
|
|
79
|
+
const documents = versions.map((value) => parseSettingsDocument(Buffer.from(value)));
|
|
80
|
+
const merge = mergeSettingsJson(...versions.map((value) => Buffer.from(value)), undefined, true);
|
|
81
|
+
if (merge.kind === "review" && merge.reason !== "field-conflict")
|
|
82
|
+
throw new Error("unsupported settings");
|
|
83
|
+
const fields = new Set(merge.kind === "review" ? merge.fields : []);
|
|
84
|
+
const groups = COUPLED_FIELDS.map((group) => [...group]);
|
|
85
|
+
const coupled = new Set(groups.flat());
|
|
86
|
+
groups.push(...[...fields].filter((key) => !coupled.has(key)).map((key) => [key]));
|
|
87
|
+
file.fields = [];
|
|
88
|
+
units = groups
|
|
89
|
+
.filter((group) => group.some((key) => fields.has(key)))
|
|
90
|
+
.map((group) => {
|
|
91
|
+
const values = documents.map((document) => JSON.stringify(Object.fromEntries(group.flatMap((key) => {
|
|
92
|
+
const node = document.root.members.get(key)?.node;
|
|
93
|
+
return node ? [[key, node.value]] : [];
|
|
94
|
+
}))));
|
|
95
|
+
const unit = {
|
|
96
|
+
id: "",
|
|
97
|
+
path: file.path,
|
|
98
|
+
kind: "settings",
|
|
99
|
+
base: values[0] ?? "{}",
|
|
100
|
+
local: values[1] ?? "{}",
|
|
101
|
+
remote: values[2] ?? "{}",
|
|
102
|
+
};
|
|
103
|
+
file.fields?.push({ id: String(evidence.units.length + file.fields.length), key: group[0] ?? "" });
|
|
104
|
+
return unit;
|
|
105
|
+
});
|
|
106
|
+
}
|
|
107
|
+
else {
|
|
108
|
+
if (!isMergeTextPath(conflict.path))
|
|
109
|
+
throw new Error("unsupported path");
|
|
110
|
+
file.parts = textParts(file.path, base, left, right);
|
|
111
|
+
units = file.parts.filter((part) => typeof part !== "string");
|
|
112
|
+
}
|
|
113
|
+
for (const unit of units) {
|
|
114
|
+
unit.id = String(evidence.units.length);
|
|
115
|
+
evidence.units.push(unit);
|
|
116
|
+
}
|
|
117
|
+
evidence.files.push(file);
|
|
118
|
+
}
|
|
119
|
+
catch {
|
|
120
|
+
evidence.unsupported.push(conflict.path);
|
|
121
|
+
}
|
|
122
|
+
}
|
|
123
|
+
if (evidence.units.length > AGENT_MAX_UNITS || Buffer.byteLength(JSON.stringify(evidence.units)) > AGENT_INPUT_BYTES)
|
|
124
|
+
throw new Error("Autonomous sync paused: conflict evidence exceeds its input bound.");
|
|
125
|
+
// Scan only the actual disclosure image; skipSecretScan never authorizes model disclosure.
|
|
126
|
+
if (scanSnapshot({
|
|
127
|
+
...local,
|
|
128
|
+
files: evidence.units.flatMap((unit) => [unit.path, unit.base, unit.local, unit.remote].map((value) => snapshotFile(unit.path, Buffer.from(value)))),
|
|
129
|
+
}).length)
|
|
130
|
+
throw new Error("Autonomous sync paused: possible secrets in model evidence; nothing was disclosed.");
|
|
131
|
+
return evidence;
|
|
132
|
+
}
|
|
133
|
+
export function parseAgentCandidate(raw, evidence) {
|
|
134
|
+
if (Buffer.byteLength(raw) > AGENT_OUTPUT_BYTES)
|
|
135
|
+
throw new Error("Agent output exceeds its bound.");
|
|
136
|
+
const parsed = parseJsonObjectDocument(Buffer.from(raw)).root.value;
|
|
137
|
+
if (Object.keys(parsed).sort().join(",") !== "results,version" ||
|
|
138
|
+
parsed.version !== 1 ||
|
|
139
|
+
!Array.isArray(parsed.results) ||
|
|
140
|
+
parsed.results.length !== evidence.units.length)
|
|
141
|
+
throw new Error("Invalid agent candidate contract.");
|
|
142
|
+
const seen = new Set();
|
|
143
|
+
for (const result of parsed.results) {
|
|
144
|
+
if (!result || typeof result !== "object" || Array.isArray(result))
|
|
145
|
+
throw new Error("Invalid agent result.");
|
|
146
|
+
const row = result;
|
|
147
|
+
const unit = evidence.units.find((unit) => unit.id === row.id);
|
|
148
|
+
if (!unit || seen.has(unit.id))
|
|
149
|
+
throw new Error("Unowned or duplicate agent unit.");
|
|
150
|
+
seen.add(unit.id);
|
|
151
|
+
const keys = Object.keys(row).sort().join(",");
|
|
152
|
+
if (keys === "id,refuse" && row.refuse === true)
|
|
153
|
+
continue;
|
|
154
|
+
if (unit.kind === "settings" && keys === "id,source" && (row.source === "local" || row.source === "remote"))
|
|
155
|
+
continue;
|
|
156
|
+
if (unit.kind === "text" && keys === "content,id" && typeof row.content === "string" && row.content.length > 0) {
|
|
157
|
+
text(Buffer.from(row.content));
|
|
158
|
+
if (/^(?:<{7}|={7}|>{7}|\|{7})/mu.test(row.content))
|
|
159
|
+
throw new Error("Unresolved conflict markers.");
|
|
160
|
+
continue;
|
|
161
|
+
}
|
|
162
|
+
throw new Error("Unsupported agent action.");
|
|
163
|
+
}
|
|
164
|
+
return parsed;
|
|
165
|
+
}
|
|
166
|
+
export function applyAgentCandidate(plan, evidence, candidate) {
|
|
167
|
+
// Persisted candidates cross a trust boundary too.
|
|
168
|
+
parseAgentCandidate(JSON.stringify(candidate), evidence);
|
|
169
|
+
if (plan.kind !== "planned")
|
|
170
|
+
throw new Error("Unverified merge plan.");
|
|
171
|
+
const results = new Map(candidate.results.map((row) => [row.id, row]));
|
|
172
|
+
const replacements = new Map();
|
|
173
|
+
for (const file of evidence.files) {
|
|
174
|
+
const units = evidence.units.filter((unit) => unit.path === file.path);
|
|
175
|
+
if (units.some((unit) => results.get(unit.id) && "refuse" in (results.get(unit.id) ?? {})))
|
|
176
|
+
continue;
|
|
177
|
+
let bytes;
|
|
178
|
+
if (file.parts) {
|
|
179
|
+
bytes = Buffer.from(file.parts
|
|
180
|
+
.map((part) => (typeof part === "string" ? part : results.get(part.id).content))
|
|
181
|
+
.join(""));
|
|
182
|
+
}
|
|
183
|
+
else {
|
|
184
|
+
const selections = new Map((file.fields ?? []).map(({ id, key }) => [key, results.get(id).source]));
|
|
185
|
+
const merged = mergeSettingsJson(Buffer.from(file.base), Buffer.from(file.local), Buffer.from(file.remote), selections, true);
|
|
186
|
+
if (merged.kind !== "merged")
|
|
187
|
+
throw new Error("Agent settings units remain unresolved.");
|
|
188
|
+
// The existing renderer/parser retains atomic values, unknown members and exact numeric policy.
|
|
189
|
+
parseSettingsDocument(merged.content);
|
|
190
|
+
for (const { key } of file.fields ?? []) {
|
|
191
|
+
if (!selections.has(key))
|
|
192
|
+
throw new Error("Missing atomic settings selection.");
|
|
193
|
+
}
|
|
194
|
+
bytes = merged.content;
|
|
195
|
+
}
|
|
196
|
+
if (bytes.length > AGENT_OUTPUT_BYTES)
|
|
197
|
+
throw new Error("Agent file exceeds output bound.");
|
|
198
|
+
replacements.set(file.path, snapshotFile(file.path, bytes));
|
|
199
|
+
}
|
|
200
|
+
const decisions = plan.decisions.map((decision) => {
|
|
201
|
+
const file = replacements.get(decision.path);
|
|
202
|
+
return file && decision.kind === "conflict"
|
|
203
|
+
? { kind: "accepted", path: file.path, source: "merged", file }
|
|
204
|
+
: decision;
|
|
205
|
+
});
|
|
206
|
+
return { kind: "planned", decisions, conflicts: decisions.filter((item) => item.kind === "conflict") };
|
|
207
|
+
}
|
|
@@ -0,0 +1,85 @@
|
|
|
1
|
+
import { syncConfigReviewIdentity } from "../settings/config.js";
|
|
2
|
+
import { createSnapshot } from "../snapshot/snapshot.js";
|
|
3
|
+
import { readMergeAncestors } from "../state/merge-baseline-store.js";
|
|
4
|
+
import { readStateForConfig, syncStateFingerprint } from "../state/sync-state-store.js";
|
|
5
|
+
import { applyAgentCandidate, buildAgentEvidence, digest } from "./agent-candidate.js";
|
|
6
|
+
import { AgentResolutionRequired } from "./agent-request.js";
|
|
7
|
+
import { ContinuousError } from "./continuous-errors.js";
|
|
8
|
+
import { fileHashMap, sameHashes } from "./sync-state.js";
|
|
9
|
+
export async function resolveAgentPlan(ctx, options, config, backend, state, local, localRaw, remote, head, plan, snapshotOptions, validate) {
|
|
10
|
+
if (plan.kind !== "planned" || !plan.conflicts.length) {
|
|
11
|
+
if (options.agentRetryRefusal)
|
|
12
|
+
throw new ContinuousError("unsafe", "No eligible cached refusal for the current evidence. Use Sync now.");
|
|
13
|
+
return plan;
|
|
14
|
+
}
|
|
15
|
+
if (!(options.background && ctx.backgroundAuthorized?.(config)) &&
|
|
16
|
+
(!ctx.hasUI || (ctx.mode !== "tui" && ctx.mode !== "rpc")))
|
|
17
|
+
throw new Error("Autonomous sync paused: TUI/RPC is required.");
|
|
18
|
+
const ancestors = (await readMergeAncestors(config, state)) ?? [];
|
|
19
|
+
await validate();
|
|
20
|
+
let evidence;
|
|
21
|
+
try {
|
|
22
|
+
evidence = buildAgentEvidence(plan, local, remote, ancestors);
|
|
23
|
+
}
|
|
24
|
+
catch {
|
|
25
|
+
throw new ContinuousError("unsafe", "Autonomous sync paused: unsafe or unsupported conflict evidence.");
|
|
26
|
+
}
|
|
27
|
+
if (!config.partialSync && evidence.unsupported.length)
|
|
28
|
+
throw new ContinuousError("unsafe", "Autonomous sync paused: unsupported conflicts prevent an all-or-nothing transfer.");
|
|
29
|
+
if (!evidence.files.length) {
|
|
30
|
+
if (options.agentRetryRefusal)
|
|
31
|
+
throw new ContinuousError("unsafe", "No eligible cached refusal for the current evidence. Use Sync now.");
|
|
32
|
+
if (config.partialSync)
|
|
33
|
+
return plan;
|
|
34
|
+
throw new ContinuousError("unsafe", "Autonomous sync paused: unsupported conflict or missing verified ancestor; evidence is unchanged.");
|
|
35
|
+
}
|
|
36
|
+
if (!evidence.units.length) {
|
|
37
|
+
if (options.agentRetryRefusal)
|
|
38
|
+
throw new ContinuousError("unsafe", "No eligible cached refusal for the current evidence. Use Sync now.");
|
|
39
|
+
return applyAgentCandidate(plan, evidence, { version: 1, results: [] });
|
|
40
|
+
}
|
|
41
|
+
if (!ctx.model)
|
|
42
|
+
throw new ContinuousError("model", ctx.modelUnavailableReason ?? "Autonomous sync paused: select an available model first.");
|
|
43
|
+
const model = digest(ctx.model);
|
|
44
|
+
const key = digest([
|
|
45
|
+
1,
|
|
46
|
+
backend.identity,
|
|
47
|
+
syncConfigReviewIdentity(config),
|
|
48
|
+
...(config.configDocumentFingerprint ? [config.configDocumentFingerprint] : []),
|
|
49
|
+
syncStateFingerprint(state),
|
|
50
|
+
head.revision,
|
|
51
|
+
fileHashMap(localRaw),
|
|
52
|
+
evidence,
|
|
53
|
+
model,
|
|
54
|
+
]);
|
|
55
|
+
if (options.agentAcceptance?.key === key) {
|
|
56
|
+
try {
|
|
57
|
+
return applyAgentCandidate(plan, evidence, options.agentAcceptance.candidate);
|
|
58
|
+
}
|
|
59
|
+
catch {
|
|
60
|
+
throw new ContinuousError("unsafe", "Autonomous sync paused: candidate refused or unsupported conflict remains.");
|
|
61
|
+
}
|
|
62
|
+
}
|
|
63
|
+
const validateEvidence = async (signal = options.signal) => {
|
|
64
|
+
const check = async () => {
|
|
65
|
+
signal?.throwIfAborted();
|
|
66
|
+
await validate();
|
|
67
|
+
signal?.throwIfAborted();
|
|
68
|
+
};
|
|
69
|
+
await check();
|
|
70
|
+
if (digest(ctx.model) !== model)
|
|
71
|
+
throw new Error("Autonomous sync paused: selected model changed.");
|
|
72
|
+
const current = await createSnapshot(config.snapshotIdentity, { ...snapshotOptions, signal });
|
|
73
|
+
await check();
|
|
74
|
+
const currentState = await readStateForConfig(config);
|
|
75
|
+
await check();
|
|
76
|
+
const currentHead = await backend.readHead(signal);
|
|
77
|
+
await check();
|
|
78
|
+
if (!sameHashes(fileHashMap(current), fileHashMap(localRaw)) ||
|
|
79
|
+
syncStateFingerprint(currentState) !== syncStateFingerprint(state) ||
|
|
80
|
+
!currentHead ||
|
|
81
|
+
!backend.sameRevision(head.revision, currentHead.revision))
|
|
82
|
+
throw new ContinuousError("stale", "Autonomous sync paused: conflict evidence changed; retry a fresh sync.");
|
|
83
|
+
};
|
|
84
|
+
throw new AgentResolutionRequired(key, evidence, validateEvidence);
|
|
85
|
+
}
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
/** One policy for startup scheduling and commit-time authorization. */
|
|
2
|
+
export function startupTransferEnabled(config) {
|
|
3
|
+
return Boolean(config.automaticTransfer || config.agentMode);
|
|
4
|
+
}
|
|
5
|
+
export const AGENT_TIMEOUT_MS = 20_000;
|
|
6
|
+
export const AGENT_INPUT_BYTES = 24 * 1024;
|
|
7
|
+
export const AGENT_OUTPUT_BYTES = 16 * 1024;
|
|
8
|
+
export const AGENT_MAX_TOKENS = 8192;
|
|
9
|
+
export const AGENT_MAX_UNITS = 16;
|
|
10
|
+
export const AGENT_MAX_ATTEMPTS = 2;
|
|
@@ -0,0 +1,39 @@
|
|
|
1
|
+
import { loadConfig } from "../settings/config.js";
|
|
2
|
+
import { confirmMergeReview } from "../ui/merge-review.js";
|
|
3
|
+
import { safeTerminalText } from "../ui/terminal-text.js";
|
|
4
|
+
import { captureMutationOwner } from "./sync-local.js";
|
|
5
|
+
/** Review uses fresh request validation; no private text or provider error enters the summary. */
|
|
6
|
+
export async function confirmAgentRefusalRetry(ctx, setup, evidence, validate, signal) {
|
|
7
|
+
const owner = captureMutationOwner(ctx, signal);
|
|
8
|
+
owner();
|
|
9
|
+
const config = await loadConfig(setup);
|
|
10
|
+
owner();
|
|
11
|
+
await validate();
|
|
12
|
+
owner();
|
|
13
|
+
const summary = [
|
|
14
|
+
`Setup: ${safeTerminalText(config.setupName)}`,
|
|
15
|
+
`Storage connection: ${safeTerminalText(config.connectionName)}`,
|
|
16
|
+
`Storage path: ${safeTerminalText(config.storagePath)}`,
|
|
17
|
+
...(config.backend.type === "s3" ? [`Bucket: ${safeTerminalText(config.backend.destination.bucket)}`] : []),
|
|
18
|
+
...(config.backend.type === "git" ? [`Branch: ${safeTerminalText(config.backend.destination.branch)}`] : []),
|
|
19
|
+
`Selected model: ${safeTerminalText(ctx.model?.provider ?? "unknown")}/${safeTerminalText(ctx.model?.id ?? "unknown")}`,
|
|
20
|
+
`Conflict paths: ${[...new Set(evidence.units.map((unit) => unit.path))].map(safeTerminalText).join(", ")}`,
|
|
21
|
+
"Send this conflict evidence again despite the cached refusal? This permits one fresh provider request for the reviewed candidate, may incur costs, and can replace selected local and remote content if the result passes validation. Semantic correctness is not guaranteed.",
|
|
22
|
+
"Ordinary Sync now continues to reuse cached results. Automatic request accounting is not cleared or reset. The previous refusal is retained privately. No resources reload automatically. Cancellation cannot recall submitted content, provider charges, or committed writes.",
|
|
23
|
+
].join("\n\n");
|
|
24
|
+
const confirmed = await confirmMergeReview(ctx, "Retry refused model result?", summary, signal, () => {
|
|
25
|
+
try {
|
|
26
|
+
owner();
|
|
27
|
+
return true;
|
|
28
|
+
}
|
|
29
|
+
catch {
|
|
30
|
+
return false;
|
|
31
|
+
}
|
|
32
|
+
}, "Send one fresh request", summary.split("\n"));
|
|
33
|
+
owner();
|
|
34
|
+
if (!confirmed)
|
|
35
|
+
return false;
|
|
36
|
+
await validate();
|
|
37
|
+
owner();
|
|
38
|
+
return true;
|
|
39
|
+
}
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
/** Internal control flow: the operation lock must unwind before any provider call. */
|
|
2
|
+
export class AgentResolutionRequired extends Error {
|
|
3
|
+
key;
|
|
4
|
+
evidence;
|
|
5
|
+
validate;
|
|
6
|
+
constructor(key, evidence, validate) {
|
|
7
|
+
super("Agent resolution requires an unlocked inference phase.");
|
|
8
|
+
this.key = key;
|
|
9
|
+
this.evidence = evidence;
|
|
10
|
+
this.validate = validate;
|
|
11
|
+
this.name = "AgentResolutionRequired";
|
|
12
|
+
}
|
|
13
|
+
}
|