@narumitw/pi-sync-daemon 0.0.0-stage → 0.2.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 +13 -0
- package/LICENSE +21 -0
- package/README.md +89 -2
- package/dist/cli.d.ts +2 -0
- package/dist/cli.js +128 -0
- package/dist/client.d.ts +55 -0
- package/dist/client.js +484 -0
- package/dist/consent.d.ts +24 -0
- package/dist/consent.js +205 -0
- package/dist/document.d.ts +10 -0
- package/dist/document.js +6 -0
- package/dist/endpoint.d.ts +8 -0
- package/dist/endpoint.js +91 -0
- package/dist/engine/backends/backend-factory.d.ts +6 -0
- package/dist/engine/backends/backend-factory.js +16 -0
- package/dist/engine/backends/backend-identity.d.ts +3 -0
- package/dist/engine/backends/backend-identity.js +38 -0
- package/dist/engine/backends/backend-types.d.ts +70 -0
- package/dist/engine/backends/backend-types.js +1 -0
- package/dist/engine/backends/git/git-backend.d.ts +55 -0
- package/dist/engine/backends/git/git-backend.js +700 -0
- package/dist/engine/backends/git/git-config.d.ts +5 -0
- package/dist/engine/backends/git/git-config.js +118 -0
- package/dist/engine/backends/git/git-runner.d.ts +25 -0
- package/dist/engine/backends/git/git-runner.js +232 -0
- package/dist/engine/backends/git/git-storage.d.ts +39 -0
- package/dist/engine/backends/git/git-storage.js +226 -0
- package/dist/engine/backends/s3/s3-backend.d.ts +32 -0
- package/dist/engine/backends/s3/s3-backend.js +436 -0
- package/dist/engine/backends/s3/s3-client.d.ts +36 -0
- package/dist/engine/backends/s3/s3-client.js +352 -0
- package/dist/engine/backends/s3/s3-conditions.d.ts +4 -0
- package/dist/engine/backends/s3/s3-conditions.js +66 -0
- package/dist/engine/backends/snapshot-pointer.d.ts +6 -0
- package/dist/engine/backends/snapshot-pointer.js +22 -0
- package/dist/engine/backends/sync-backend.d.ts +71 -0
- package/dist/engine/backends/sync-backend.js +23 -0
- package/dist/engine/backends/webdav/webdav-backend.d.ts +34 -0
- package/dist/engine/backends/webdav/webdav-backend.js +534 -0
- package/dist/engine/backends/webdav/webdav-client.d.ts +36 -0
- package/dist/engine/backends/webdav/webdav-client.js +326 -0
- package/dist/engine/backends/webdav/webdav-config.d.ts +5 -0
- package/dist/engine/backends/webdav/webdav-config.js +73 -0
- package/dist/engine/commands/command-types.d.ts +31 -0
- package/dist/engine/commands/command-types.js +1 -0
- package/dist/engine/host.d.ts +70 -0
- package/dist/engine/host.js +16 -0
- package/dist/engine/paths.d.ts +9 -0
- package/dist/engine/paths.js +71 -0
- package/dist/engine/settings/config-errors.d.ts +1 -0
- package/dist/engine/settings/config-errors.js +4 -0
- package/dist/engine/settings/config-file.d.ts +33 -0
- package/dist/engine/settings/config-file.js +630 -0
- package/dist/engine/settings/config.d.ts +24 -0
- package/dist/engine/settings/config.js +200 -0
- package/dist/engine/settings/settings-management.d.ts +24 -0
- package/dist/engine/settings/settings-management.js +191 -0
- package/dist/engine/settings/settings-store.d.ts +8 -0
- package/dist/engine/settings/settings-store.js +41 -0
- package/dist/engine/settings/settings-types.d.ts +134 -0
- package/dist/engine/settings/settings-types.js +1 -0
- package/dist/engine/settings/settings-validation.d.ts +15 -0
- package/dist/engine/settings/settings-validation.js +356 -0
- package/dist/engine/snapshot/session-paths.d.ts +12 -0
- package/dist/engine/snapshot/session-paths.js +91 -0
- package/dist/engine/snapshot/snapshot-apply.d.ts +22 -0
- package/dist/engine/snapshot/snapshot-apply.js +262 -0
- package/dist/engine/snapshot/snapshot-case-replacement.d.ts +15 -0
- package/dist/engine/snapshot/snapshot-case-replacement.js +82 -0
- package/dist/engine/snapshot/snapshot-codec.d.ts +6 -0
- package/dist/engine/snapshot/snapshot-codec.js +53 -0
- package/dist/engine/snapshot/snapshot-exclude-safety.d.ts +7 -0
- package/dist/engine/snapshot/snapshot-exclude-safety.js +47 -0
- package/dist/engine/snapshot/snapshot-mutation-queues.d.ts +9 -0
- package/dist/engine/snapshot/snapshot-mutation-queues.js +133 -0
- package/dist/engine/snapshot/snapshot-paths.d.ts +1 -0
- package/dist/engine/snapshot/snapshot-paths.js +11 -0
- package/dist/engine/snapshot/snapshot-root-transition.d.ts +25 -0
- package/dist/engine/snapshot/snapshot-root-transition.js +122 -0
- package/dist/engine/snapshot/snapshot-transaction-directory.d.ts +2 -0
- package/dist/engine/snapshot/snapshot-transaction-directory.js +16 -0
- package/dist/engine/snapshot/snapshot-transaction-plan.d.ts +12 -0
- package/dist/engine/snapshot/snapshot-transaction-plan.js +68 -0
- package/dist/engine/snapshot/snapshot-transaction.d.ts +15 -0
- package/dist/engine/snapshot/snapshot-transaction.js +787 -0
- package/dist/engine/snapshot/snapshot-types.d.ts +48 -0
- package/dist/engine/snapshot/snapshot-types.js +1 -0
- package/dist/engine/snapshot/snapshot.d.ts +30 -0
- package/dist/engine/snapshot/snapshot.js +364 -0
- package/dist/engine/state/json-file.d.ts +6 -0
- package/dist/engine/state/json-file.js +49 -0
- package/dist/engine/state/lock-policy.d.ts +2 -0
- package/dist/engine/state/lock-policy.js +2 -0
- package/dist/engine/state/lock.d.ts +20 -0
- package/dist/engine/state/lock.js +259 -0
- package/dist/engine/state/lockfile-fs.d.ts +13 -0
- package/dist/engine/state/lockfile-fs.js +13 -0
- package/dist/engine/state/merge-baseline-store.d.ts +11 -0
- package/dist/engine/state/merge-baseline-store.js +187 -0
- package/dist/engine/state/mutation-directory-sync.d.ts +5 -0
- package/dist/engine/state/mutation-directory-sync.js +34 -0
- package/dist/engine/state/operation-availability.d.ts +49 -0
- package/dist/engine/state/operation-availability.js +40 -0
- package/dist/engine/state/state-directory.d.ts +14 -0
- package/dist/engine/state/state-directory.js +225 -0
- package/dist/engine/state/state-types.d.ts +27 -0
- package/dist/engine/state/state-types.js +1 -0
- package/dist/engine/state/sync-state-store.d.ts +11 -0
- package/dist/engine/state/sync-state-store.js +54 -0
- package/dist/engine/sync/agent-candidate.d.ts +44 -0
- package/dist/engine/sync/agent-candidate.js +207 -0
- package/dist/engine/sync/agent-plan.d.ts +8 -0
- package/dist/engine/sync/agent-plan.js +84 -0
- package/dist/engine/sync/agent-policy.d.ts +9 -0
- package/dist/engine/sync/agent-policy.js +10 -0
- package/dist/engine/sync/agent-refusal-retry.d.ts +4 -0
- package/dist/engine/sync/agent-refusal-retry.js +39 -0
- package/dist/engine/sync/agent-request.d.ts +14 -0
- package/dist/engine/sync/agent-request.js +13 -0
- package/dist/engine/sync/agent-runner.d.ts +6 -0
- package/dist/engine/sync/agent-runner.js +183 -0
- package/dist/engine/sync/agent-store.d.ts +7 -0
- package/dist/engine/sync/agent-store.js +177 -0
- package/dist/engine/sync/agent-sync.d.ts +4 -0
- package/dist/engine/sync/agent-sync.js +132 -0
- package/dist/engine/sync/apply-review.d.ts +8 -0
- package/dist/engine/sync/apply-review.js +51 -0
- package/dist/engine/sync/conflict-artifact-ownership.d.ts +4 -0
- package/dist/engine/sync/conflict-artifact-ownership.js +53 -0
- package/dist/engine/sync/conflict-artifacts.d.ts +32 -0
- package/dist/engine/sync/conflict-artifacts.js +158 -0
- package/dist/engine/sync/conflict-groups.d.ts +9 -0
- package/dist/engine/sync/conflict-groups.js +76 -0
- package/dist/engine/sync/conflict-preview.d.ts +4 -0
- package/dist/engine/sync/conflict-preview.js +53 -0
- package/dist/engine/sync/conflict-resolution.d.ts +13 -0
- package/dist/engine/sync/conflict-resolution.js +53 -0
- package/dist/engine/sync/conflict-retention.d.ts +5 -0
- package/dist/engine/sync/conflict-retention.js +61 -0
- package/dist/engine/sync/conflict-review.d.ts +4 -0
- package/dist/engine/sync/conflict-review.js +73 -0
- package/dist/engine/sync/content-conflicts.d.ts +10 -0
- package/dist/engine/sync/content-conflicts.js +70 -0
- package/dist/engine/sync/continuous-errors.d.ts +14 -0
- package/dist/engine/sync/continuous-errors.js +66 -0
- package/dist/engine/sync/continuous-lease.d.ts +9 -0
- package/dist/engine/sync/continuous-lease.js +206 -0
- package/dist/engine/sync/continuous-record.d.ts +31 -0
- package/dist/engine/sync/continuous-record.js +199 -0
- package/dist/engine/sync/continuous-storage.d.ts +14 -0
- package/dist/engine/sync/continuous-storage.js +59 -0
- package/dist/engine/sync/continuous-sync.d.ts +15 -0
- package/dist/engine/sync/continuous-sync.js +496 -0
- package/dist/engine/sync/continuous-watch.d.ts +3 -0
- package/dist/engine/sync/continuous-watch.js +65 -0
- package/dist/engine/sync/exclude-policy.d.ts +5 -0
- package/dist/engine/sync/exclude-policy.js +79 -0
- package/dist/engine/sync/field-migration.d.ts +5 -0
- package/dist/engine/sync/field-migration.js +20 -0
- package/dist/engine/sync/file-merge-planner.d.ts +36 -0
- package/dist/engine/sync/file-merge-planner.js +123 -0
- package/dist/engine/sync/initialization.d.ts +12 -0
- package/dist/engine/sync/initialization.js +75 -0
- package/dist/engine/sync/json-document.d.ts +29 -0
- package/dist/engine/sync/json-document.js +172 -0
- package/dist/engine/sync/local-field-policy.d.ts +2 -0
- package/dist/engine/sync/local-field-policy.js +26 -0
- package/dist/engine/sync/local-fields.d.ts +9 -0
- package/dist/engine/sync/local-fields.js +116 -0
- package/dist/engine/sync/merge-apply.d.ts +5 -0
- package/dist/engine/sync/merge-apply.js +386 -0
- package/dist/engine/sync/merge-journal.d.ts +31 -0
- package/dist/engine/sync/merge-journal.js +179 -0
- package/dist/engine/sync/merge-transfer-error.d.ts +3 -0
- package/dist/engine/sync/merge-transfer-error.js +64 -0
- package/dist/engine/sync/merged-sync.d.ts +6 -0
- package/dist/engine/sync/merged-sync.js +618 -0
- package/dist/engine/sync/mutation-owner.d.ts +2 -0
- package/dist/engine/sync/mutation-owner.js +18 -0
- package/dist/engine/sync/package-merge.d.ts +3 -0
- package/dist/engine/sync/package-merge.js +82 -0
- package/dist/engine/sync/partial-progress.d.ts +15 -0
- package/dist/engine/sync/partial-progress.js +39 -0
- package/dist/engine/sync/remote-snapshot.d.ts +28 -0
- package/dist/engine/sync/remote-snapshot.js +62 -0
- package/dist/engine/sync/resolver-stream.d.ts +4 -0
- package/dist/engine/sync/resolver-stream.js +34 -0
- package/dist/engine/sync/session-merge.d.ts +3 -0
- package/dist/engine/sync/session-merge.js +178 -0
- package/dist/engine/sync/session-push.d.ts +5 -0
- package/dist/engine/sync/session-push.js +32 -0
- package/dist/engine/sync/settings-conflicts.d.ts +8 -0
- package/dist/engine/sync/settings-conflicts.js +56 -0
- package/dist/engine/sync/settings-merge.d.ts +13 -0
- package/dist/engine/sync/settings-merge.js +93 -0
- package/dist/engine/sync/signals.d.ts +2 -0
- package/dist/engine/sync/signals.js +8 -0
- package/dist/engine/sync/startup-timings.d.ts +11 -0
- package/dist/engine/sync/startup-timings.js +44 -0
- package/dist/engine/sync/sync-decision.d.ts +35 -0
- package/dist/engine/sync/sync-decision.js +66 -0
- package/dist/engine/sync/sync-error-guidance.d.ts +2 -0
- package/dist/engine/sync/sync-error-guidance.js +55 -0
- package/dist/engine/sync/sync-errors.d.ts +9 -0
- package/dist/engine/sync/sync-errors.js +18 -0
- package/dist/engine/sync/sync-inspection.d.ts +26 -0
- package/dist/engine/sync/sync-inspection.js +40 -0
- package/dist/engine/sync/sync-loaders.d.ts +7 -0
- package/dist/engine/sync/sync-loaders.js +8 -0
- package/dist/engine/sync/sync-local.d.ts +5 -0
- package/dist/engine/sync/sync-local.js +40 -0
- package/dist/engine/sync/sync-mutations.d.ts +30 -0
- package/dist/engine/sync/sync-mutations.js +589 -0
- package/dist/engine/sync/sync-operations.d.ts +2 -0
- package/dist/engine/sync/sync-operations.js +3 -0
- package/dist/engine/sync/sync-policy.d.ts +85 -0
- package/dist/engine/sync/sync-policy.js +356 -0
- package/dist/engine/sync/sync-queries.d.ts +7 -0
- package/dist/engine/sync/sync-queries.js +195 -0
- package/dist/engine/sync/sync-state.d.ts +31 -0
- package/dist/engine/sync/sync-state.js +92 -0
- package/dist/engine/sync/text-merge.d.ts +19 -0
- package/dist/engine/sync/text-merge.js +124 -0
- package/dist/engine/types.d.ts +5 -0
- package/dist/engine/types.js +2 -0
- package/dist/engine/ui/merge-review.d.ts +2 -0
- package/dist/engine/ui/merge-review.js +7 -0
- package/dist/engine/ui/merged-sync-summary.d.ts +25 -0
- package/dist/engine/ui/merged-sync-summary.js +86 -0
- package/dist/engine/ui/sync-format.d.ts +13 -0
- package/dist/engine/ui/sync-format.js +150 -0
- package/dist/engine/ui/sync-status.d.ts +9 -0
- package/dist/engine/ui/sync-status.js +30 -0
- package/dist/engine/ui/terminal-text.d.ts +3 -0
- package/dist/engine/ui/terminal-text.js +9 -0
- package/dist/engine/ui/transfer-changes.d.ts +12 -0
- package/dist/engine/ui/transfer-changes.js +24 -0
- package/dist/engine/ui/transfer-review.d.ts +3 -0
- package/dist/engine/ui/transfer-review.js +19 -0
- package/dist/engine-service.d.ts +11 -0
- package/dist/engine-service.js +315 -0
- package/dist/identity.d.ts +20 -0
- package/dist/identity.js +133 -0
- package/dist/index.d.ts +8 -0
- package/dist/index.js +4 -0
- package/dist/lifecycle-log.d.ts +7 -0
- package/dist/lifecycle-log.js +53 -0
- package/dist/lifecycle.d.ts +2 -0
- package/dist/lifecycle.js +21 -0
- package/dist/model-signature.d.ts +2 -0
- package/dist/model-signature.js +7 -0
- package/dist/monitor.d.ts +11 -0
- package/dist/monitor.js +245 -0
- package/dist/participant-admission.d.ts +5 -0
- package/dist/participant-admission.js +30 -0
- package/dist/protocol.d.ts +97 -0
- package/dist/protocol.js +90 -0
- package/dist/resolver-host.d.ts +11 -0
- package/dist/resolver-host.js +78 -0
- package/dist/server.d.ts +18 -0
- package/dist/server.js +724 -0
- package/dist/service-host.d.ts +7 -0
- package/dist/service-host.js +174 -0
- package/docs/architecture.md +133 -0
- package/package.json +54 -4
|
@@ -0,0 +1,589 @@
|
|
|
1
|
+
import { createSyncBackend } from "../backends/backend-factory.js";
|
|
2
|
+
import { expectedRemoteHead, } from "../backends/sync-backend.js";
|
|
3
|
+
import { loadConfig, syncCheckConfigFingerprint } from "../settings/config.js";
|
|
4
|
+
import { sessionDirForApply, snapshotOptionsForContext } from "../snapshot/session-paths.js";
|
|
5
|
+
import { createSnapshot, filterSnapshotForConfigPolicy, mergeRemotePreservedFiles, regenerateSnapshotIdentity, scanSnapshot, snapshotIncludesSessions, snapshotWithoutSessions, } from "../snapshot/snapshot.js";
|
|
6
|
+
import { applySnapshot } from "../snapshot/snapshot-apply.js";
|
|
7
|
+
import { pruneMergeBaselines } from "../state/merge-baseline-store.js";
|
|
8
|
+
import { readStateForConfig, syncStateFingerprint, writeStateForConfig } from "../state/sync-state-store.js";
|
|
9
|
+
import { countPreservedRemoteFiles, formatPullSummary, formatPushSummary, formatRollbackSummary, } from "../ui/sync-format.js";
|
|
10
|
+
import { setSyncStatus } from "../ui/sync-status.js";
|
|
11
|
+
import { confirmTransferReview } from "../ui/transfer-review.js";
|
|
12
|
+
import { startupTransferEnabled } from "./agent-policy.js";
|
|
13
|
+
import { inspectCaseCleanup, revalidateCaseCleanup } from "./apply-review.js";
|
|
14
|
+
import { confirmFieldMigration } from "./field-migration.js";
|
|
15
|
+
import { overlayLocalFields, portableSnapshot, sameLocalFields, validatePortableSnapshot } from "./local-fields.js";
|
|
16
|
+
import { readMergeJournal, requireNoMergeJournal, retireMergeJournal } from "./merge-journal.js";
|
|
17
|
+
import { mergeSync } from "./merged-sync.js";
|
|
18
|
+
import { readRemoteSnapshot, readSnapshotForHead, requireCompatibleRemoteSelection } from "./remote-snapshot.js";
|
|
19
|
+
import { throwIfAborted } from "./signals.js";
|
|
20
|
+
import { createSyncDecision } from "./sync-decision.js";
|
|
21
|
+
import { errorMessage } from "./sync-errors.js";
|
|
22
|
+
import { backupLocal, captureMutationOwner, protectedSessionPaths } from "./sync-local.js";
|
|
23
|
+
import { inspectRemoteSelection, syncSessionsSelected } from "./sync-policy.js";
|
|
24
|
+
import { canPullRemoteSessionsOnFirstSync, canPullRemoteSettingsOnFirstSync, fileHashMap, hasLocalChanges, hasRemoteChanges, remoteChangedSinceState, sameHashes, shouldRefreshSyncedState, snapshotHashesMatchState, snapshotsMatch, syncPolicyChanged, } from "./sync-state.js";
|
|
25
|
+
const VERSION = 1;
|
|
26
|
+
const POST_LOCAL_COMMIT_TIMEOUT_MS = 30_000;
|
|
27
|
+
/** Whole-direction acceptance must never forget withheld state without an explicit recovery choice. */
|
|
28
|
+
function requireResolvedDirectionalProgress(state, options, allowForce = true) {
|
|
29
|
+
if (state.unresolved?.length && (options.auto || !options.force || !allowForce))
|
|
30
|
+
throw new Error("Unresolved partial conflicts remain. Review /sync conflicts or explicitly choose push --force or pull --force; automatic transfer and rollback cannot accept unresolved groups.");
|
|
31
|
+
}
|
|
32
|
+
export class PublicationStatePersistenceError extends Error {
|
|
33
|
+
head;
|
|
34
|
+
backupPath;
|
|
35
|
+
constructor(head, cause, backupPath) {
|
|
36
|
+
super(`Remote publication ${head.snapshotId} is active, but local sync state could not be saved${backupPath ? `; local backup: ${backupPath}` : ""}: ${errorMessage(cause)}`, { cause });
|
|
37
|
+
this.name = "PublicationStatePersistenceError";
|
|
38
|
+
this.head = head;
|
|
39
|
+
this.backupPath = backupPath;
|
|
40
|
+
}
|
|
41
|
+
}
|
|
42
|
+
export class RollbackPublicationError extends Error {
|
|
43
|
+
backupPath;
|
|
44
|
+
constructor(backupPath, cause) {
|
|
45
|
+
super(`Rollback applied locally with backup ${backupPath}, but remote publication failed: ${errorMessage(cause)}`, {
|
|
46
|
+
cause,
|
|
47
|
+
});
|
|
48
|
+
this.name = "RollbackPublicationError";
|
|
49
|
+
this.backupPath = backupPath;
|
|
50
|
+
}
|
|
51
|
+
}
|
|
52
|
+
export async function push(ctx, options, input, factory = createSyncBackend) {
|
|
53
|
+
const validateMutation = captureMutationOwner(ctx, options.signal);
|
|
54
|
+
const validateCommittedOwner = captureMutationOwner(ctx);
|
|
55
|
+
const config = input?.config ?? (await loadConfig(options.setup));
|
|
56
|
+
throwIfAborted(options.signal);
|
|
57
|
+
await requireNoMergeJournal(config, options);
|
|
58
|
+
const state = input?.state ?? (await readStateForConfig(config));
|
|
59
|
+
validateMutation();
|
|
60
|
+
requireResolvedDirectionalProgress(state, options);
|
|
61
|
+
setSyncStatus(ctx, `pushing ${config.setupName}`, { activity: "pushing" });
|
|
62
|
+
const backend = input?.backend ?? (await factory(config));
|
|
63
|
+
throwIfAborted(options.signal);
|
|
64
|
+
const localRaw = input?.local ?? (await createSnapshot(config.snapshotIdentity, snapshotOptionsForContext(ctx, config)));
|
|
65
|
+
const local = portableSnapshot(localRaw, config.localFields);
|
|
66
|
+
throwIfAborted(options.signal);
|
|
67
|
+
let head = await backend.readHead(options.signal);
|
|
68
|
+
let remoteForUpload = await readRemoteSnapshotForUpload(backend, config, head, state, options.signal);
|
|
69
|
+
if (!options.force &&
|
|
70
|
+
!remoteForUpload &&
|
|
71
|
+
head?.selection &&
|
|
72
|
+
inspectRemoteSelection(config.include, { selection: head.selection, files: [] }, config.exclude).kind ===
|
|
73
|
+
"different") {
|
|
74
|
+
remoteForUpload = await readSnapshotForHead(backend, head, options.signal);
|
|
75
|
+
}
|
|
76
|
+
if (remoteForUpload && !options.force) {
|
|
77
|
+
requireCompatibleRemoteSelection(config, remoteForUpload);
|
|
78
|
+
}
|
|
79
|
+
if (remoteChangedSinceState(head, state, config, (left, right) => backend.sameRevision(left, right)) &&
|
|
80
|
+
!options.force) {
|
|
81
|
+
const remoteForConflict = remoteForUpload ? filterSnapshotForConfigPolicy(remoteForUpload, config) : undefined;
|
|
82
|
+
if (!remoteForConflict || !snapshotHashesMatchState(remoteForConflict, state, config)) {
|
|
83
|
+
throw createSyncDecision({
|
|
84
|
+
kind: head ? "remote-or-policy-changed" : "remote-empty",
|
|
85
|
+
config,
|
|
86
|
+
state,
|
|
87
|
+
local,
|
|
88
|
+
remote: remoteForConflict,
|
|
89
|
+
localChanged: hasLocalChanges(local, state, config),
|
|
90
|
+
remoteChanged: true,
|
|
91
|
+
directMessage: "Remote or sync policy changed since last sync. Run /sync pull first or /sync push --force.",
|
|
92
|
+
});
|
|
93
|
+
}
|
|
94
|
+
}
|
|
95
|
+
if (!remoteForUpload && head && (config.localFields !== undefined || !options.yes))
|
|
96
|
+
remoteForUpload = await readSnapshotForHead(backend, head, options.signal);
|
|
97
|
+
if (!(await confirmFieldMigration(ctx, config, state, remoteForUpload, options.force, options.signal))) {
|
|
98
|
+
setSyncStatus(ctx, undefined);
|
|
99
|
+
return "cancelled";
|
|
100
|
+
}
|
|
101
|
+
const configIdentity = syncCheckConfigFingerprint(config);
|
|
102
|
+
let upload = await snapshotForUpload(backend, config, local, head, remoteForUpload, options.signal);
|
|
103
|
+
if (!config.skipSecretScan) {
|
|
104
|
+
const secrets = scanSnapshot(local);
|
|
105
|
+
if (secrets.length > 0) {
|
|
106
|
+
throw new Error(`Refusing to push possible secrets:\n${secrets.map((s) => `- ${s}`).join("\n")}`);
|
|
107
|
+
}
|
|
108
|
+
}
|
|
109
|
+
if (!(await confirmPush(ctx, options, config, backend, local, upload, head, remoteForUpload))) {
|
|
110
|
+
return "cancelled";
|
|
111
|
+
}
|
|
112
|
+
if (options.force) {
|
|
113
|
+
const refreshedHead = await backend.readHead(options.signal);
|
|
114
|
+
if (!sameRemoteHead(backend, head, refreshedHead)) {
|
|
115
|
+
head = refreshedHead;
|
|
116
|
+
remoteForUpload = head ? await readSnapshotForHead(backend, head, options.signal) : undefined;
|
|
117
|
+
validateMutation();
|
|
118
|
+
if (!(await confirmFieldMigration(ctx, config, state, remoteForUpload, options.force, options.signal))) {
|
|
119
|
+
setSyncStatus(ctx, undefined);
|
|
120
|
+
return "cancelled";
|
|
121
|
+
}
|
|
122
|
+
validateMutation();
|
|
123
|
+
upload = await snapshotForUpload(backend, config, local, head, remoteForUpload, options.signal);
|
|
124
|
+
if (!(await confirmPush(ctx, options, config, backend, local, upload, head, remoteForUpload, "Remote changed during review. Push the refreshed plan?"))) {
|
|
125
|
+
return "cancelled";
|
|
126
|
+
}
|
|
127
|
+
}
|
|
128
|
+
}
|
|
129
|
+
validateMutation();
|
|
130
|
+
if (configIdentity !== syncCheckConfigFingerprint(await loadConfig(options.setup)))
|
|
131
|
+
throw new Error("Settings changed during push review.");
|
|
132
|
+
validateMutation();
|
|
133
|
+
if (!options.yes || config.localFields !== undefined || (await readMergeJournal(config))) {
|
|
134
|
+
const current = await createSnapshot(config.snapshotIdentity, snapshotOptionsForContext(ctx, config));
|
|
135
|
+
if (!sameHashes(fileHashMap(current), fileHashMap(localRaw)))
|
|
136
|
+
throw new Error("Local content changed during push review; retry from current bytes.");
|
|
137
|
+
}
|
|
138
|
+
const currentState = await readStateForConfig(config);
|
|
139
|
+
if (!options.yes && syncStateFingerprint(currentState) !== syncStateFingerprint(state))
|
|
140
|
+
throw new Error("Baseline changed during push review.");
|
|
141
|
+
requireResolvedDirectionalProgress(currentState, options);
|
|
142
|
+
validateMutation();
|
|
143
|
+
const result = await backend.publishSnapshot(upload, expectedRemoteHead(head), {
|
|
144
|
+
signal: options.signal,
|
|
145
|
+
onCommit: options.onCommit,
|
|
146
|
+
});
|
|
147
|
+
try {
|
|
148
|
+
await writeStateForConfig(config, {
|
|
149
|
+
version: VERSION,
|
|
150
|
+
profile: config.snapshotIdentity,
|
|
151
|
+
lastAppliedSnapshot: result.head.snapshotId,
|
|
152
|
+
lastRemoteRevision: result.head.revision,
|
|
153
|
+
lastFileHashes: fileHashMap(local),
|
|
154
|
+
include: [...config.include],
|
|
155
|
+
...(config.exclude === undefined ? {} : { exclude: [...config.exclude] }),
|
|
156
|
+
...(config.localFields !== undefined ? { localFields: config.localFields } : {}),
|
|
157
|
+
}, local, validateCommittedOwner);
|
|
158
|
+
}
|
|
159
|
+
catch (error) {
|
|
160
|
+
throw new PublicationStatePersistenceError(result.head, error);
|
|
161
|
+
}
|
|
162
|
+
await retireMergeJournal(config);
|
|
163
|
+
await pruneMergeBaselines(config, await readStateForConfig(config), validateCommittedOwner);
|
|
164
|
+
validateCommittedOwner();
|
|
165
|
+
if (options.signal?.aborted)
|
|
166
|
+
return;
|
|
167
|
+
setSyncStatus(ctx, undefined);
|
|
168
|
+
if (!options.silent) {
|
|
169
|
+
ctx.ui.notify([
|
|
170
|
+
`Pushed ${upload.files.length} files from sync setup “${config.setupName}” as ${result.head.snapshotId}.`,
|
|
171
|
+
...result.warnings,
|
|
172
|
+
]
|
|
173
|
+
.filter(Boolean)
|
|
174
|
+
.join("\n"), result.warnings.length > 0 ? "warning" : "info");
|
|
175
|
+
}
|
|
176
|
+
return "applied";
|
|
177
|
+
}
|
|
178
|
+
export async function pull(ctx, options, factory = createSyncBackend) {
|
|
179
|
+
const validateMutation = captureMutationOwner(ctx, options.signal);
|
|
180
|
+
const validateCommittedOwner = captureMutationOwner(ctx);
|
|
181
|
+
const config = await loadConfig(options.setup);
|
|
182
|
+
throwIfAborted(options.signal);
|
|
183
|
+
await requireNoMergeJournal(config, options);
|
|
184
|
+
setSyncStatus(ctx, `pulling ${config.setupName}`, { activity: "pulling" });
|
|
185
|
+
const backend = await factory(config);
|
|
186
|
+
const state = await readStateForConfig(config);
|
|
187
|
+
validateMutation();
|
|
188
|
+
requireResolvedDirectionalProgress(state, options);
|
|
189
|
+
const local = await createSnapshot(config.snapshotIdentity, snapshotOptionsForContext(ctx, config));
|
|
190
|
+
throwIfAborted(options.signal);
|
|
191
|
+
const { head, snapshot: remote } = await readRemoteSnapshot(backend, config, options.signal);
|
|
192
|
+
throwIfAborted(options.signal);
|
|
193
|
+
const localChanged = hasLocalChanges(local, state, config);
|
|
194
|
+
if (!remote) {
|
|
195
|
+
throw createSyncDecision({
|
|
196
|
+
kind: "remote-empty",
|
|
197
|
+
config,
|
|
198
|
+
state,
|
|
199
|
+
local,
|
|
200
|
+
localChanged,
|
|
201
|
+
remoteChanged: false,
|
|
202
|
+
directMessage: "Remote is empty. Run /sync push from a configured machine first.",
|
|
203
|
+
});
|
|
204
|
+
}
|
|
205
|
+
if (!sameLocalFields(config.localFields, remote.localFields)) {
|
|
206
|
+
setSyncStatus(ctx, undefined);
|
|
207
|
+
throw new Error("Pull requires the configured local-field policy to match the remote. To change the remote policy, review push --force from the authoritative machine first, then pull using matching rules; old history is not erased.");
|
|
208
|
+
}
|
|
209
|
+
if (!(await confirmFieldMigration(ctx, config, state, remote, options.force, options.signal))) {
|
|
210
|
+
setSyncStatus(ctx, undefined);
|
|
211
|
+
return "cancelled";
|
|
212
|
+
}
|
|
213
|
+
const configIdentity = syncCheckConfigFingerprint(config);
|
|
214
|
+
const logical = portableSnapshot(remote, config.localFields);
|
|
215
|
+
const physical = overlayLocalFields(logical, local, config.localFields);
|
|
216
|
+
const remoteChanged = hasRemoteChanges(remote, state, config, protectedSessionPaths(ctx));
|
|
217
|
+
if (localChanged && remoteChanged && state.lastAppliedSnapshot && !options.force) {
|
|
218
|
+
throw createSyncDecision({
|
|
219
|
+
kind: "both-changed",
|
|
220
|
+
config,
|
|
221
|
+
state,
|
|
222
|
+
local,
|
|
223
|
+
remote,
|
|
224
|
+
localChanged,
|
|
225
|
+
remoteChanged,
|
|
226
|
+
directMessage: "Both local and remote changed since last sync. Run /sync diff, then choose /sync pull --force or /sync push --force.",
|
|
227
|
+
});
|
|
228
|
+
}
|
|
229
|
+
const caseCleanup = options.yes ? [] : await inspectCaseCleanup(physical, options.signal);
|
|
230
|
+
validateMutation();
|
|
231
|
+
if (!options.yes &&
|
|
232
|
+
!(await confirmTransferReview(ctx, snapshotIncludesSessions(remote) ? "Pull pi settings and sessions?" : "Pull pi settings?", formatPullSummary(config, backend.destination, local, physical, protectedSessionPaths(ctx), caseCleanup.map((item) => item.path)), options.signal, validateMutation))) {
|
|
233
|
+
setSyncStatus(ctx, undefined);
|
|
234
|
+
ctx.ui.notify("Pull cancelled.", "info");
|
|
235
|
+
return "cancelled";
|
|
236
|
+
}
|
|
237
|
+
throwIfAborted(options.signal);
|
|
238
|
+
const recoveringMerge = Boolean(await readMergeJournal(config));
|
|
239
|
+
const requireFreshRecovery = async () => {
|
|
240
|
+
validateMutation();
|
|
241
|
+
const currentHead = await backend.readHead(options.signal);
|
|
242
|
+
validateMutation();
|
|
243
|
+
const current = await createSnapshot(config.snapshotIdentity, snapshotOptionsForContext(ctx, config));
|
|
244
|
+
validateMutation();
|
|
245
|
+
if (!sameHashes(fileHashMap(current), fileHashMap(local)) || !sameRemoteHead(backend, head, currentHead)) {
|
|
246
|
+
throw new Error("Local or remote content changed during recovery review; retry from a fresh diff.");
|
|
247
|
+
}
|
|
248
|
+
};
|
|
249
|
+
if (recoveringMerge)
|
|
250
|
+
await requireFreshRecovery();
|
|
251
|
+
if (configIdentity !== syncCheckConfigFingerprint(await loadConfig(options.setup)))
|
|
252
|
+
throw new Error("Settings changed during pull review.");
|
|
253
|
+
validateMutation();
|
|
254
|
+
const refreshed = await createSnapshot(config.snapshotIdentity, snapshotOptionsForContext(ctx, config));
|
|
255
|
+
if (!sameHashes(fileHashMap(local), fileHashMap(refreshed)))
|
|
256
|
+
throw new Error("Local bytes changed during pull review.");
|
|
257
|
+
if (!sameRemoteHead(backend, head, await backend.readHead(options.signal)))
|
|
258
|
+
throw new Error("Remote changed during pull review.");
|
|
259
|
+
validateMutation();
|
|
260
|
+
const backup = await backupLocal(config.snapshotIdentity, snapshotOptionsForContext(ctx, config), options.signal);
|
|
261
|
+
const applySessionDir = await sessionDirForApply(ctx, physical);
|
|
262
|
+
if (recoveringMerge)
|
|
263
|
+
await requireFreshRecovery();
|
|
264
|
+
const currentState = await readStateForConfig(config);
|
|
265
|
+
if (!options.yes && syncStateFingerprint(currentState) !== syncStateFingerprint(state))
|
|
266
|
+
throw new Error("Baseline changed during pull review.");
|
|
267
|
+
requireResolvedDirectionalProgress(currentState, options);
|
|
268
|
+
if (!options.yes)
|
|
269
|
+
await revalidateCaseCleanup(caseCleanup, physical, options.signal);
|
|
270
|
+
validateMutation();
|
|
271
|
+
options.onCommit?.();
|
|
272
|
+
const lastFileHashes = await applySnapshot(physical, protectedSessionPaths(ctx), {
|
|
273
|
+
include: config.include,
|
|
274
|
+
...(config.exclude === undefined ? {} : { exclude: [...config.exclude] }),
|
|
275
|
+
sessionDir: applySessionDir,
|
|
276
|
+
signal: options.signal,
|
|
277
|
+
validateMutation,
|
|
278
|
+
expectedFileHashes: config.localFields?.length
|
|
279
|
+
? { "settings.json": fileHashMap(local)["settings.json"] ?? null }
|
|
280
|
+
: undefined,
|
|
281
|
+
});
|
|
282
|
+
// Local installation is complete; user cancellation must not leave the baseline stale.
|
|
283
|
+
validateCommittedOwner();
|
|
284
|
+
await writeStateForConfig(config, {
|
|
285
|
+
version: VERSION,
|
|
286
|
+
profile: config.snapshotIdentity,
|
|
287
|
+
lastAppliedSnapshot: remote.id,
|
|
288
|
+
lastRemoteRevision: head?.revision,
|
|
289
|
+
lastFileHashes: config.localFields?.length
|
|
290
|
+
? { ...lastFileHashes, "settings.json": fileHashMap(logical)["settings.json"] }
|
|
291
|
+
: lastFileHashes,
|
|
292
|
+
include: [...config.include],
|
|
293
|
+
...(config.exclude === undefined ? {} : { exclude: [...config.exclude] }),
|
|
294
|
+
...(config.localFields !== undefined ? { localFields: config.localFields } : {}),
|
|
295
|
+
}, logical, validateCommittedOwner);
|
|
296
|
+
await retireMergeJournal(config);
|
|
297
|
+
await pruneMergeBaselines(config, await readStateForConfig(config), validateCommittedOwner);
|
|
298
|
+
validateCommittedOwner();
|
|
299
|
+
if (options.signal?.aborted)
|
|
300
|
+
return "applied";
|
|
301
|
+
setSyncStatus(ctx, undefined);
|
|
302
|
+
if (!options.silent) {
|
|
303
|
+
ctx.ui.notify(`Pulled ${remote.files.length} files from ${remote.id}. Backup: ${backup}`, "info");
|
|
304
|
+
}
|
|
305
|
+
else if (options.auto && syncSessionsSelected(config) && snapshotIncludesSessions(remote)) {
|
|
306
|
+
ctx.ui.notify("Pulled Pi sessions after startup selected the current session. Restart Pi or resume a pulled session to use newly synced conversations.", "warning");
|
|
307
|
+
}
|
|
308
|
+
if (options.reload)
|
|
309
|
+
await maybeReload(ctx, options.signal, validateMutation);
|
|
310
|
+
return "applied";
|
|
311
|
+
}
|
|
312
|
+
export async function syncBoth(ctx, options, factory = createSyncBackend) {
|
|
313
|
+
const validateMutation = captureMutationOwner(ctx, options.signal);
|
|
314
|
+
const config = await loadConfig(options.setup);
|
|
315
|
+
throwIfAborted(options.signal);
|
|
316
|
+
options.background?.validate(config);
|
|
317
|
+
if (options.agentRetryRefusal) {
|
|
318
|
+
if (!config.agentMode || options.auto || options.background || !ctx.hasUI)
|
|
319
|
+
throw new Error("Enable Autonomous sync and use the foreground manager to retry a cached refusal.");
|
|
320
|
+
const state = await readStateForConfig(config);
|
|
321
|
+
validateMutation();
|
|
322
|
+
const pending = await readMergeJournal(config);
|
|
323
|
+
validateMutation();
|
|
324
|
+
if (!config.include.length || !state.lastAppliedSnapshot || pending)
|
|
325
|
+
throw new Error("Retry requires an accepted baseline and no pending journal. Use Sync now or reviewed recovery.");
|
|
326
|
+
return mergeSync(ctx, options, factory);
|
|
327
|
+
}
|
|
328
|
+
if (config.include.length > 0 && options.auto && startupTransferEnabled(config))
|
|
329
|
+
return mergeSync(ctx, options, factory);
|
|
330
|
+
if (!options.auto && config.include.length > 0 && (await readMergeJournal(config)))
|
|
331
|
+
return mergeSync(ctx, options, factory);
|
|
332
|
+
const state = await readStateForConfig(config);
|
|
333
|
+
throwIfAborted(options.signal);
|
|
334
|
+
if (!options.auto && config.include.length > 0 && state.lastAppliedSnapshot)
|
|
335
|
+
return mergeSync(ctx, options, factory);
|
|
336
|
+
const backend = await factory(config);
|
|
337
|
+
const local = await createSnapshot(config.snapshotIdentity, snapshotOptionsForContext(ctx, config));
|
|
338
|
+
throwIfAborted(options.signal);
|
|
339
|
+
if (config.include.length === 0) {
|
|
340
|
+
if (!options.silent) {
|
|
341
|
+
ctx.ui.notify(`Sync setup “${config.setupName}” includes no files. Choose included content in /sync Settings before syncing.`, "warning");
|
|
342
|
+
}
|
|
343
|
+
return;
|
|
344
|
+
}
|
|
345
|
+
const { head, snapshot: remote } = await readRemoteSnapshot(backend, config, options.signal);
|
|
346
|
+
throwIfAborted(options.signal);
|
|
347
|
+
const localChanged = hasLocalChanges(local, state, config);
|
|
348
|
+
const remoteChanged = remote ? hasRemoteChanges(remote, state, config, protectedSessionPaths(ctx)) : false;
|
|
349
|
+
if (options.auto)
|
|
350
|
+
throw new Error("Automatic transfer is not authorized by the current settings.");
|
|
351
|
+
// Established baselines return through mergeSync above; only initial-source selection remains.
|
|
352
|
+
const portableLocal = portableSnapshot(local, config.localFields);
|
|
353
|
+
const acceptMatchingState = async (snapshot) => {
|
|
354
|
+
requireCompatibleRemoteSelection(config, snapshot);
|
|
355
|
+
const accepted = portableSnapshot(snapshot, config.localFields);
|
|
356
|
+
await writeStateForConfig(config, {
|
|
357
|
+
version: VERSION,
|
|
358
|
+
profile: config.snapshotIdentity,
|
|
359
|
+
lastAppliedSnapshot: snapshot.id,
|
|
360
|
+
lastRemoteRevision: head?.revision,
|
|
361
|
+
lastFileHashes: fileHashMap(accepted),
|
|
362
|
+
include: [...config.include],
|
|
363
|
+
...(config.exclude === undefined ? {} : { exclude: [...config.exclude] }),
|
|
364
|
+
...(config.localFields !== undefined ? { localFields: config.localFields } : {}),
|
|
365
|
+
}, accepted, validateMutation);
|
|
366
|
+
await pruneMergeBaselines(config, await readStateForConfig(config), validateMutation);
|
|
367
|
+
validateMutation();
|
|
368
|
+
};
|
|
369
|
+
if (remote && remote.files.length > 0 && local.files.length > 0) {
|
|
370
|
+
requireCompatibleRemoteSelection(config, remote);
|
|
371
|
+
const portableRemote = portableSnapshot(remote, config.localFields);
|
|
372
|
+
if (!canPullRemoteSettingsOnFirstSync(portableLocal, portableRemote)) {
|
|
373
|
+
throw createSyncDecision({
|
|
374
|
+
kind: "first-sync-settings-diverged",
|
|
375
|
+
config,
|
|
376
|
+
state,
|
|
377
|
+
local,
|
|
378
|
+
remote,
|
|
379
|
+
localChanged: true,
|
|
380
|
+
remoteChanged: true,
|
|
381
|
+
directMessage: "Remote settings exist and this machine has different local Pi settings. Run /sync diff, then manually choose /sync pull or /sync push.",
|
|
382
|
+
});
|
|
383
|
+
}
|
|
384
|
+
if (!sameHashes(fileHashMap(portableLocal), fileHashMap(portableRemote))) {
|
|
385
|
+
if (!canPullRemoteSessionsOnFirstSync(portableLocal, portableRemote)) {
|
|
386
|
+
throw createSyncDecision({
|
|
387
|
+
kind: "first-sync-sessions-diverged",
|
|
388
|
+
config,
|
|
389
|
+
state,
|
|
390
|
+
local,
|
|
391
|
+
remote,
|
|
392
|
+
localChanged: true,
|
|
393
|
+
remoteChanged: true,
|
|
394
|
+
directMessage: "Remote settings match, but local and remote Pi sessions differ. Run /sync diff, then manually choose /sync pull or /sync push.",
|
|
395
|
+
});
|
|
396
|
+
}
|
|
397
|
+
await pull(ctx, options, factory);
|
|
398
|
+
return;
|
|
399
|
+
}
|
|
400
|
+
await acceptMatchingState(remote);
|
|
401
|
+
if (!options.silent)
|
|
402
|
+
ctx.ui.notify("pi-sync state initialized; local settings already match remote.", "info");
|
|
403
|
+
return;
|
|
404
|
+
}
|
|
405
|
+
if (localChanged &&
|
|
406
|
+
remoteChanged &&
|
|
407
|
+
remote &&
|
|
408
|
+
snapshotsMatch(portableLocal, portableSnapshot(remote, remote.localFields))) {
|
|
409
|
+
requireCompatibleRemoteSelection(config, remote);
|
|
410
|
+
await acceptMatchingState(remote);
|
|
411
|
+
if (!options.silent)
|
|
412
|
+
ctx.ui.notify("pi-sync is already up to date.", "info");
|
|
413
|
+
return;
|
|
414
|
+
}
|
|
415
|
+
if (remoteChanged) {
|
|
416
|
+
await pull(ctx, options, factory);
|
|
417
|
+
return;
|
|
418
|
+
}
|
|
419
|
+
if (localChanged || !remote) {
|
|
420
|
+
await push(ctx, options, undefined, factory);
|
|
421
|
+
return;
|
|
422
|
+
}
|
|
423
|
+
if (shouldRefreshSyncedState(remote, head, state, config, (left, right) => backend.sameRevision(left, right))) {
|
|
424
|
+
await acceptMatchingState(remote);
|
|
425
|
+
}
|
|
426
|
+
if (!options.silent)
|
|
427
|
+
ctx.ui.notify("pi-sync is already up to date.", "info");
|
|
428
|
+
}
|
|
429
|
+
export async function rollback(ctx, options, factory = createSyncBackend, expectedSelection) {
|
|
430
|
+
const validateMutation = captureMutationOwner(ctx, options.signal);
|
|
431
|
+
const validateCommittedOwner = captureMutationOwner(ctx);
|
|
432
|
+
const target = options.args[0];
|
|
433
|
+
if (!target)
|
|
434
|
+
throw new Error("Usage: /sync rollback <snapshot-id> [--yes]");
|
|
435
|
+
const config = await loadConfig(options.setup);
|
|
436
|
+
throwIfAborted(options.signal);
|
|
437
|
+
const backend = await factory(config);
|
|
438
|
+
if (expectedSelection &&
|
|
439
|
+
(backend.identity !== expectedSelection.backendIdentity || config.setupName !== expectedSelection.setup)) {
|
|
440
|
+
throw new Error("Sync setup or storage location changed while history was open; reopen history and retry.");
|
|
441
|
+
}
|
|
442
|
+
// --force is only a rollback compatibility flag, not a reviewed merge-recovery direction.
|
|
443
|
+
await requireNoMergeJournal(config);
|
|
444
|
+
const state = await readStateForConfig(config);
|
|
445
|
+
validateMutation();
|
|
446
|
+
requireResolvedDirectionalProgress(state, options, false);
|
|
447
|
+
const decoded = await backend.readSnapshot(target, options.signal);
|
|
448
|
+
validatePortableSnapshot(decoded);
|
|
449
|
+
if (!sameLocalFields(decoded.localFields, config.localFields))
|
|
450
|
+
throw new Error("Rollback requires the historical snapshot local-field policy to match the configured rules. Review push --force from current local bytes to migrate policy first; choose matching history afterward.");
|
|
451
|
+
const selected = filterSnapshotForConfigPolicy(syncSessionsSelected(config) ? decoded : snapshotWithoutSessions(decoded), config);
|
|
452
|
+
const remote = portableSnapshot(regenerateSnapshotIdentity(selected), config.localFields);
|
|
453
|
+
const local = await createSnapshot(config.snapshotIdentity, snapshotOptionsForContext(ctx, config));
|
|
454
|
+
const expectedHead = await backend.readHead(options.signal);
|
|
455
|
+
const currentRemote = expectedHead ? await readSnapshotForHead(backend, expectedHead, options.signal) : undefined;
|
|
456
|
+
validateMutation();
|
|
457
|
+
if (!(await confirmFieldMigration(ctx, config, state, currentRemote, true, options.signal)))
|
|
458
|
+
return;
|
|
459
|
+
validateMutation();
|
|
460
|
+
const physical = overlayLocalFields(remote, local, config.localFields);
|
|
461
|
+
const caseCleanup = options.yes ? [] : await inspectCaseCleanup(physical, options.signal);
|
|
462
|
+
validateMutation();
|
|
463
|
+
if (!options.yes) {
|
|
464
|
+
const confirmed = await ctx.ui.confirm(snapshotIncludesSessions(remote) ? "Rollback pi settings and sessions?" : "Rollback pi settings?", formatRollbackSummary(config, backend.destination, local, physical, target, protectedSessionPaths(ctx), caseCleanup.map((item) => item.path)), { signal: options.signal });
|
|
465
|
+
validateMutation();
|
|
466
|
+
if (!confirmed) {
|
|
467
|
+
ctx.ui.notify("Rollback cancelled.", "info");
|
|
468
|
+
return;
|
|
469
|
+
}
|
|
470
|
+
}
|
|
471
|
+
validateMutation();
|
|
472
|
+
const currentLocal = await createSnapshot(config.snapshotIdentity, snapshotOptionsForContext(ctx, config));
|
|
473
|
+
if (!sameHashes(fileHashMap(currentLocal), fileHashMap(local)))
|
|
474
|
+
throw new Error("Local bytes changed during rollback review.");
|
|
475
|
+
if (syncCheckConfigFingerprint(config) !== syncCheckConfigFingerprint(await loadConfig(options.setup)))
|
|
476
|
+
throw new Error("Settings changed during rollback review.");
|
|
477
|
+
if (!sameRemoteHead(backend, expectedHead, await backend.readHead(options.signal)))
|
|
478
|
+
throw new Error("Remote changed during rollback review.");
|
|
479
|
+
validateMutation();
|
|
480
|
+
throwIfAborted(options.signal);
|
|
481
|
+
const backup = await backupLocal(config.snapshotIdentity, snapshotOptionsForContext(ctx, config), options.signal);
|
|
482
|
+
const applySessionDir = await sessionDirForApply(ctx, remote);
|
|
483
|
+
const currentState = await readStateForConfig(config);
|
|
484
|
+
if (!options.yes && syncStateFingerprint(currentState) !== syncStateFingerprint(state))
|
|
485
|
+
throw new Error("Baseline changed during rollback review.");
|
|
486
|
+
requireResolvedDirectionalProgress(currentState, options, false);
|
|
487
|
+
if (!options.yes)
|
|
488
|
+
await revalidateCaseCleanup(caseCleanup, physical, options.signal);
|
|
489
|
+
validateMutation();
|
|
490
|
+
options.onCommit?.();
|
|
491
|
+
const lastFileHashes = await applySnapshot(physical, protectedSessionPaths(ctx), {
|
|
492
|
+
include: config.include,
|
|
493
|
+
...(config.exclude === undefined ? {} : { exclude: [...config.exclude] }),
|
|
494
|
+
sessionDir: applySessionDir,
|
|
495
|
+
signal: options.signal,
|
|
496
|
+
validateMutation,
|
|
497
|
+
expectedFileHashes: config.localFields?.length
|
|
498
|
+
? { "settings.json": fileHashMap(local)["settings.json"] ?? null }
|
|
499
|
+
: undefined,
|
|
500
|
+
});
|
|
501
|
+
// The local transaction is retired; finish publication despite caller cancellation.
|
|
502
|
+
validateCommittedOwner();
|
|
503
|
+
let result;
|
|
504
|
+
try {
|
|
505
|
+
const completionSignal = AbortSignal.timeout(POST_LOCAL_COMMIT_TIMEOUT_MS);
|
|
506
|
+
const upload = await snapshotForUpload(backend, config, remote, expectedHead, currentRemote, completionSignal);
|
|
507
|
+
validateCommittedOwner();
|
|
508
|
+
result = await backend.publishSnapshot(upload, expectedRemoteHead(expectedHead), {
|
|
509
|
+
signal: completionSignal,
|
|
510
|
+
});
|
|
511
|
+
}
|
|
512
|
+
catch (error) {
|
|
513
|
+
throw new RollbackPublicationError(backup, error);
|
|
514
|
+
}
|
|
515
|
+
try {
|
|
516
|
+
await writeStateForConfig(config, {
|
|
517
|
+
version: VERSION,
|
|
518
|
+
profile: config.snapshotIdentity,
|
|
519
|
+
lastAppliedSnapshot: result.head.snapshotId,
|
|
520
|
+
lastRemoteRevision: result.head.revision,
|
|
521
|
+
lastFileHashes: config.localFields?.length
|
|
522
|
+
? { ...lastFileHashes, "settings.json": fileHashMap(remote)["settings.json"] }
|
|
523
|
+
: lastFileHashes,
|
|
524
|
+
include: [...config.include],
|
|
525
|
+
...(config.exclude === undefined ? {} : { exclude: [...config.exclude] }),
|
|
526
|
+
...(config.localFields !== undefined ? { localFields: config.localFields } : {}),
|
|
527
|
+
}, remote, validateCommittedOwner);
|
|
528
|
+
}
|
|
529
|
+
catch (error) {
|
|
530
|
+
throw new PublicationStatePersistenceError(result.head, error, backup);
|
|
531
|
+
}
|
|
532
|
+
await retireMergeJournal(config);
|
|
533
|
+
await pruneMergeBaselines(config, await readStateForConfig(config), validateCommittedOwner);
|
|
534
|
+
validateCommittedOwner();
|
|
535
|
+
if (options.signal?.aborted)
|
|
536
|
+
return;
|
|
537
|
+
ctx.ui.notify([
|
|
538
|
+
`Rolled back sync setup “${config.setupName}” to ${target}; latest: ${result.head.snapshotId}. Backup: ${backup}`,
|
|
539
|
+
...result.warnings,
|
|
540
|
+
]
|
|
541
|
+
.filter(Boolean)
|
|
542
|
+
.join("\n"), result.warnings.length > 0 ? "warning" : "info");
|
|
543
|
+
await maybeReload(ctx, options.signal, validateMutation);
|
|
544
|
+
}
|
|
545
|
+
async function maybeReload(ctx, signal, validateMutation = captureMutationOwner(ctx, signal)) {
|
|
546
|
+
if (signal?.aborted || typeof ctx.reload !== "function")
|
|
547
|
+
return;
|
|
548
|
+
validateMutation();
|
|
549
|
+
if (ctx.hasUI &&
|
|
550
|
+
(await ctx.ui.confirm("Reload Pi resources now?", "This reloads extensions, skills, prompts, themes, and context files."))) {
|
|
551
|
+
if (signal?.aborted)
|
|
552
|
+
return;
|
|
553
|
+
validateMutation();
|
|
554
|
+
await ctx.reload();
|
|
555
|
+
}
|
|
556
|
+
}
|
|
557
|
+
async function readRemoteSnapshotForUpload(backend, config, head, state, signal) {
|
|
558
|
+
if (!head ||
|
|
559
|
+
(head.snapshotId === state.lastAppliedSnapshot &&
|
|
560
|
+
!syncPolicyChanged(state, config) &&
|
|
561
|
+
(!state.lastRemoteRevision || backend.sameRevision(head.revision, state.lastRemoteRevision)))) {
|
|
562
|
+
return undefined;
|
|
563
|
+
}
|
|
564
|
+
return readSnapshotForHead(backend, head, signal);
|
|
565
|
+
}
|
|
566
|
+
async function snapshotForUpload(backend, config, local, head, remote, signal) {
|
|
567
|
+
if (!head)
|
|
568
|
+
return local;
|
|
569
|
+
const snapshot = remote ?? (await readSnapshotForHead(backend, head, signal));
|
|
570
|
+
return mergeRemotePreservedFiles(local, snapshot, config);
|
|
571
|
+
}
|
|
572
|
+
async function confirmPush(ctx, options, config, backend, local, upload, head, remote, title = snapshotIncludesSessions(upload) ? "Push pi settings and sessions?" : "Push pi settings?") {
|
|
573
|
+
throwIfAborted(options.signal);
|
|
574
|
+
if (options.yes)
|
|
575
|
+
return true;
|
|
576
|
+
const confirmed = await confirmTransferReview(ctx, title, formatPushSummary(config, backend.destination, upload, head, countPreservedRemoteFiles(local, upload), remote), options.signal, captureMutationOwner(ctx, options.signal));
|
|
577
|
+
throwIfAborted(options.signal);
|
|
578
|
+
if (confirmed)
|
|
579
|
+
return true;
|
|
580
|
+
setSyncStatus(ctx, undefined);
|
|
581
|
+
ctx.ui.notify("Push cancelled.", "info");
|
|
582
|
+
return false;
|
|
583
|
+
}
|
|
584
|
+
function sameRemoteHead(backend, left, right) {
|
|
585
|
+
if (!left || !right)
|
|
586
|
+
return left === right;
|
|
587
|
+
return backend.sameRevision(left.revision, right.revision);
|
|
588
|
+
}
|
|
589
|
+
export { backupLocal } from "./sync-local.js";
|
|
@@ -0,0 +1,3 @@
|
|
|
1
|
+
// First-use operation surface for the cached loader; implementations stay lazy.
|
|
2
|
+
export { backupLocal, PublicationStatePersistenceError, pull, push, RollbackPublicationError, rollback, syncBoth, } from "./sync-mutations.js";
|
|
3
|
+
export { diff, doctor, history, status } from "./sync-queries.js";
|