@narumitw/pi-sync 0.64.0 → 0.65.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 +10 -0
- package/README.md +76 -7
- package/dist/chunks/{chunk-CKUGCBHP.ts → chunk-AYGMIMCV.ts} +2 -2
- package/dist/chunks/chunk-AYGMIMCV.ts.map +7 -0
- package/dist/chunks/{chunk-F3VODLVQ.ts → chunk-IOM6E5FW.ts} +3 -3
- package/dist/chunks/{chunk-RW44KNFD.ts → chunk-JQ5BAWW2.ts} +182 -12
- package/dist/chunks/chunk-JQ5BAWW2.ts.map +7 -0
- package/dist/chunks/{chunk-NDEZ52SS.ts → chunk-OBVJTKVY.ts} +5 -5
- package/dist/chunks/{chunk-TJ7YNITN.ts → chunk-OWEOZZZE.ts} +2 -2
- package/dist/chunks/{chunk-VPSC7BXM.ts → chunk-SZJOUVCL.ts} +2 -2
- package/dist/chunks/{chunk-YT6PDRMM.ts → chunk-WJQ4TGN5.ts} +17 -17
- package/dist/chunks/chunk-WJQ4TGN5.ts.map +7 -0
- package/dist/chunks/{chunk-4VKWHVUO.ts → chunk-XUZV4OGI.ts} +3 -2
- package/dist/chunks/{chunk-4VKWHVUO.ts.map → chunk-XUZV4OGI.ts.map} +2 -2
- package/dist/chunks/{command-handler-N45GY7AV.ts → command-handler-YZ4DXQ7N.ts} +49 -10
- package/dist/chunks/command-handler-YZ4DXQ7N.ts.map +7 -0
- package/dist/chunks/{conflict-review-5E4M44BA.ts → conflict-review-J7L7S4AR.ts} +2 -2
- package/dist/chunks/{manager-result-dispatcher-FKW55L7I.ts → manager-result-dispatcher-XB75YLEG.ts} +4 -4
- package/dist/chunks/{manager-ui-EV23DPZQ.ts → manager-ui-RGA26Z6G.ts} +40 -38
- package/dist/chunks/manager-ui-RGA26Z6G.ts.map +7 -0
- package/dist/chunks/{merge-review-6YSLSMGS.ts → merge-review-AAC674C6.ts} +3 -3
- package/dist/chunks/{remote-selection-ui-N4NMWFT2.ts → remote-selection-ui-WVMET5RI.ts} +3 -3
- package/dist/chunks/{setup-switch-HY7K3442.ts → setup-switch-644T6KHG.ts} +3 -3
- package/dist/chunks/{setup-wizard-LEKDQ3HK.ts → setup-wizard-YP2KF2WQ.ts} +5 -5
- package/dist/chunks/{sync-inspection-OS2LVOLV.ts → sync-inspection-NBBTSMKR.ts} +2 -2
- package/dist/chunks/{sync-operations-D3MDJUQQ.ts → sync-operations-AIEM2KL2.ts} +2 -2
- package/dist/daemon/dist/client.js +125 -287
- package/dist/daemon/dist/daemon-recovery.js +85 -2
- package/dist/daemon/dist/daemon-startup.js +257 -0
- package/dist/daemon/dist/engine/backends/git/git-backend.js +10 -1
- package/dist/daemon/dist/engine/backends/s3/s3-backend.js +20 -9
- package/dist/daemon/dist/engine/backends/sync-backend.js +16 -0
- package/dist/daemon/dist/engine/backends/webdav/webdav-backend.js +19 -6
- package/dist/daemon/dist/engine/sync/directional-action.js +124 -0
- package/dist/daemon/dist/engine/sync/remote-snapshot.js +3 -1
- package/dist/daemon/dist/engine/sync/sync-errors.js +20 -0
- package/dist/daemon/dist/engine/sync/sync-local.js +56 -3
- package/dist/daemon/dist/engine/sync/sync-mutations.js +300 -334
- package/dist/daemon/dist/engine/sync/sync-queries.js +2 -1
- package/dist/daemon/dist/engine/sync/sync-rollback.js +198 -0
- package/dist/daemon/dist/engine/sync/transfer-freshness.js +11 -5
- package/dist/daemon/dist/engine-service.js +68 -0
- package/dist/daemon/dist/foreground-queue-outcome.js +9 -0
- package/dist/daemon/dist/monitor.js +1 -0
- package/dist/daemon/dist/server.js +76 -11
- package/dist/daemon/dist/service-host.js +56 -8
- package/dist/daemon/package.json +1 -1
- package/dist/index.ts +6 -6
- package/docs/daemon-migration.md +3 -6
- package/package.json +2 -2
- package/src/commands/command-execution.ts +56 -2
- package/src/commands/command-handler.ts +2 -0
- package/src/commands/command.ts +2 -1
- package/src/daemon/directional-recovery.ts +103 -0
- package/src/daemon/pi-client.ts +112 -9
- package/src/ui/cancellable-operation.ts +5 -2
- package/src/ui/manager-state.ts +2 -2
- package/src/ui/manager-ui.ts +29 -24
- package/src/ui/merge-review.ts +8 -9
- package/src/ui/sync-confirmation.ts +7 -7
- package/dist/chunks/chunk-CKUGCBHP.ts.map +0 -7
- package/dist/chunks/chunk-RW44KNFD.ts.map +0 -7
- package/dist/chunks/chunk-YT6PDRMM.ts.map +0 -7
- package/dist/chunks/command-handler-N45GY7AV.ts.map +0 -7
- package/dist/chunks/manager-ui-EV23DPZQ.ts.map +0 -7
- /package/dist/chunks/{chunk-F3VODLVQ.ts.map → chunk-IOM6E5FW.ts.map} +0 -0
- /package/dist/chunks/{chunk-NDEZ52SS.ts.map → chunk-OBVJTKVY.ts.map} +0 -0
- /package/dist/chunks/{chunk-TJ7YNITN.ts.map → chunk-OWEOZZZE.ts.map} +0 -0
- /package/dist/chunks/{chunk-VPSC7BXM.ts.map → chunk-SZJOUVCL.ts.map} +0 -0
- /package/dist/chunks/{conflict-review-5E4M44BA.ts.map → conflict-review-J7L7S4AR.ts.map} +0 -0
- /package/dist/chunks/{manager-result-dispatcher-FKW55L7I.ts.map → manager-result-dispatcher-XB75YLEG.ts.map} +0 -0
- /package/dist/chunks/{merge-review-6YSLSMGS.ts.map → merge-review-AAC674C6.ts.map} +0 -0
- /package/dist/chunks/{remote-selection-ui-N4NMWFT2.ts.map → remote-selection-ui-WVMET5RI.ts.map} +0 -0
- /package/dist/chunks/{setup-switch-HY7K3442.ts.map → setup-switch-644T6KHG.ts.map} +0 -0
- /package/dist/chunks/{setup-wizard-LEKDQ3HK.ts.map → setup-wizard-YP2KF2WQ.ts.map} +0 -0
- /package/dist/chunks/{sync-inspection-OS2LVOLV.ts.map → sync-inspection-NBBTSMKR.ts.map} +0 -0
- /package/dist/chunks/{sync-operations-D3MDJUQQ.ts.map → sync-operations-AIEM2KL2.ts.map} +0 -0
|
@@ -1,17 +1,19 @@
|
|
|
1
1
|
import { createSyncBackend } from "../backends/backend-factory.js";
|
|
2
|
-
import { expectedRemoteHead, } from "../backends/sync-backend.js";
|
|
3
|
-
import { loadConfig, syncCheckConfigFingerprint } from "../settings/config.js";
|
|
2
|
+
import { expectedRemoteHead, SyncBackendConflictError, } from "../backends/sync-backend.js";
|
|
3
|
+
import { loadConfig, loadConfigForCheck, syncCheckConfigFingerprint } from "../settings/config.js";
|
|
4
4
|
import { effectiveSessionRoot, requireStableMergeSessionRoot, resolvedSnapshotOptionsForContext, sessionDirForApply, } from "../snapshot/session-paths.js";
|
|
5
|
-
import { createSnapshot, filterSnapshotForConfigPolicy,
|
|
5
|
+
import { createSnapshot, filterSnapshotForConfigPolicy, scanSnapshot, snapshotIncludesSessions, } from "../snapshot/snapshot.js";
|
|
6
6
|
import { applySnapshot } from "../snapshot/snapshot-apply.js";
|
|
7
|
+
import { pendingTransactionEntries } from "../snapshot/snapshot-transaction-directory.js";
|
|
7
8
|
import { ignorePolicyValidator } from "../snapshot/snapshot-transaction-policy.js";
|
|
8
9
|
import { pruneMergeBaselines } from "../state/merge-baseline-store.js";
|
|
9
10
|
import { readStateForConfig, syncStateFingerprint, writeStateForConfig } from "../state/sync-state-store.js";
|
|
10
|
-
import { countPreservedRemoteFiles,
|
|
11
|
+
import { countPreservedRemoteFiles, formatPullSummary, formatPushSummary, formatTransferReview, } from "../ui/sync-format.js";
|
|
11
12
|
import { setSyncStatus } from "../ui/sync-status.js";
|
|
12
13
|
import { confirmTransferReview } from "../ui/transfer-review.js";
|
|
13
14
|
import { startupTransferEnabled } from "./agent-policy.js";
|
|
14
15
|
import { inspectCaseCleanup, revalidateCaseCleanup } from "./apply-review.js";
|
|
16
|
+
import { checkpointDirectionalAction, foregroundDirectionalAction, keepProtectedRemoteSessions, localRestoreGuidance, notifyDirectionalSuccess, remoteRestoreGuidance, requireDirectionalPullBoundary, requireDirectionalSelection, validateDirectionalSettings, } from "./directional-action.js";
|
|
15
17
|
import { confirmFieldMigration } from "./field-migration.js";
|
|
16
18
|
import { overlayLocalFields, portableSnapshot, sameLocalFields, validatePortableSnapshot } from "./local-fields.js";
|
|
17
19
|
import { readMergeJournal, requireNoMergeJournal, retireMergeJournal } from "./merge-journal.js";
|
|
@@ -19,40 +21,27 @@ import { mergeSync } from "./merged-sync.js";
|
|
|
19
21
|
import { readRemoteSnapshot, readRemoteSnapshotForUpload, readSnapshotForHead, requireCompatibleRemoteSelection, sameRemoteHead, snapshotForUpload, } from "./remote-snapshot.js";
|
|
20
22
|
import { throwIfAborted } from "./signals.js";
|
|
21
23
|
import { createSyncDecision } from "./sync-decision.js";
|
|
22
|
-
import { errorMessage } from "./sync-errors.js";
|
|
23
|
-
import { backupLocal, captureMutationOwner, protectedSessionPaths } from "./sync-local.js";
|
|
24
|
-
import { formatIgnorePolicyComparison, inspectRemoteSelection,
|
|
24
|
+
import { errorMessage, PublicationStatePersistenceError } from "./sync-errors.js";
|
|
25
|
+
import { backupDirectionalSnapshot, backupLocal, captureMutationOwner, LocalBackupVerificationError, maybeReload, protectedSessionPaths, } from "./sync-local.js";
|
|
26
|
+
import { formatIgnorePolicyComparison, inspectRemoteSelection, syncSelectionHasContent, syncSessionsSelected, } from "./sync-policy.js";
|
|
25
27
|
import { acceptedSelection, canPullRemoteSessionsOnFirstSync, canPullRemoteSettingsOnFirstSync, fileHashMap, hasLocalChanges, hasRemoteChanges, remoteChangedSinceState, sameHashes, shouldRefreshSyncedState, snapshotHashesMatchState, snapshotsMatch, syncPolicyChanged, } from "./sync-state.js";
|
|
26
|
-
import { directionalApplyGuards, requireIgnoreBaselineReview, requireResolvedDirectionalProgress, validateDirectionalPublication, } from "./transfer-freshness.js";
|
|
28
|
+
import { checkLiveProtection, directionalApplyGuards, requireIgnoreBaselineReview, requireResolvedDirectionalProgress, validateDirectionalPublication, } from "./transfer-freshness.js";
|
|
27
29
|
const VERSION = 1;
|
|
28
|
-
|
|
29
|
-
export
|
|
30
|
-
head;
|
|
31
|
-
backupPath;
|
|
32
|
-
constructor(head, cause, backupPath) {
|
|
33
|
-
super(`Remote publication ${head.snapshotId} is active, but local sync state could not be saved${backupPath ? `; local backup: ${backupPath}` : ""}: ${errorMessage(cause)}`, { cause });
|
|
34
|
-
this.name = "PublicationStatePersistenceError";
|
|
35
|
-
this.head = head;
|
|
36
|
-
this.backupPath = backupPath;
|
|
37
|
-
}
|
|
38
|
-
}
|
|
39
|
-
export class RollbackPublicationError extends Error {
|
|
40
|
-
backupPath;
|
|
41
|
-
constructor(backupPath, cause) {
|
|
42
|
-
super(`Rollback applied locally with backup ${backupPath}, but remote publication failed: ${errorMessage(cause)}`, {
|
|
43
|
-
cause,
|
|
44
|
-
});
|
|
45
|
-
this.name = "RollbackPublicationError";
|
|
46
|
-
this.backupPath = backupPath;
|
|
47
|
-
}
|
|
48
|
-
}
|
|
30
|
+
export { PublicationStatePersistenceError, RollbackPublicationError } from "./sync-errors.js";
|
|
31
|
+
export { rollback } from "./sync-rollback.js";
|
|
49
32
|
export async function push(ctx, options, input, factory = createSyncBackend) {
|
|
50
33
|
const validateOwner = captureMutationOwner(ctx, options.signal);
|
|
51
34
|
const validateCommittedOwner = captureMutationOwner(ctx);
|
|
52
|
-
const
|
|
35
|
+
const action = foregroundDirectionalAction(ctx, options);
|
|
36
|
+
if (action && input)
|
|
37
|
+
throw new Error("Single-action Push must load the bound foreground setup.");
|
|
38
|
+
const config = action
|
|
39
|
+
? await loadConfigForCheck(undefined, options.signal)
|
|
40
|
+
: (input?.config ?? (await loadConfig(options.setup, options.signal)));
|
|
53
41
|
const validateMutation = () => {
|
|
54
42
|
validateOwner();
|
|
55
43
|
options.background?.validate(config);
|
|
44
|
+
action?.validate(config);
|
|
56
45
|
};
|
|
57
46
|
validateMutation();
|
|
58
47
|
await requireNoMergeJournal(config, options);
|
|
@@ -69,28 +58,37 @@ export async function push(ctx, options, input, factory = createSyncBackend) {
|
|
|
69
58
|
}
|
|
70
59
|
setSyncStatus(ctx, `pushing ${config.setupName}`, { activity: "pushing" });
|
|
71
60
|
const backend = input?.backend ?? (await factory(config));
|
|
72
|
-
|
|
61
|
+
validateMutation();
|
|
73
62
|
const snapshotOptions = await resolvedSnapshotOptionsForContext(ctx, config, options.signal);
|
|
63
|
+
if (action)
|
|
64
|
+
snapshotOptions.sessionDir = await effectiveSessionRoot(ctx, config, options.signal);
|
|
74
65
|
validateMutation();
|
|
75
66
|
const validateIgnorePolicy = ignorePolicyValidator({ ...snapshotOptions, validateMutation });
|
|
76
67
|
const localRaw = input?.local ?? (await createSnapshot(config.snapshotIdentity, snapshotOptions));
|
|
77
68
|
const local = portableSnapshot(localRaw, config.localFields);
|
|
69
|
+
const reviewedProtectedPaths = action ? protectedSessionPaths(ctx, snapshotOptions.sessionDir) : new Set();
|
|
78
70
|
if (validateIgnorePolicy)
|
|
79
71
|
await validateIgnorePolicy();
|
|
80
72
|
throwIfAborted(options.signal);
|
|
81
73
|
let head = await backend.readHead(options.signal);
|
|
82
|
-
let remoteForUpload =
|
|
74
|
+
let remoteForUpload = action && head
|
|
75
|
+
? await readSnapshotForHead(backend, head, options.signal, config)
|
|
76
|
+
: await readRemoteSnapshotForUpload(backend, config, head, state, options);
|
|
83
77
|
validateMutation();
|
|
84
78
|
if (config.ignorePolicy && remoteForUpload && options.force && (options.auto || options.yes || !ctx.hasUI)) {
|
|
85
79
|
const comparison = inspectRemoteSelection(config.include, remoteForUpload, config.exclude, config);
|
|
86
80
|
if (comparison.kind === "different")
|
|
87
81
|
requireCompatibleRemoteSelection(config, remoteForUpload);
|
|
88
82
|
}
|
|
89
|
-
|
|
90
|
-
|
|
83
|
+
if (action)
|
|
84
|
+
requireDirectionalSelection(config, state, localRaw, remoteForUpload, snapshotOptions.sessionDir);
|
|
85
|
+
else
|
|
86
|
+
requireIgnoreBaselineReview(ctx, config, state, options);
|
|
87
|
+
if (!action && remoteForUpload && !options.force) {
|
|
91
88
|
requireCompatibleRemoteSelection(config, remoteForUpload);
|
|
92
89
|
}
|
|
93
|
-
if (
|
|
90
|
+
if (!action &&
|
|
91
|
+
remoteChangedSinceState(head, state, config, (left, right) => backend.sameRevision(left, right)) &&
|
|
94
92
|
!options.force) {
|
|
95
93
|
const remoteForConflict = remoteForUpload
|
|
96
94
|
? filterSnapshotForConfigPolicy(remoteForUpload, config, snapshotOptions)
|
|
@@ -117,13 +115,30 @@ export async function push(ctx, options, input, factory = createSyncBackend) {
|
|
|
117
115
|
}
|
|
118
116
|
const configIdentity = syncCheckConfigFingerprint(config);
|
|
119
117
|
let upload = await snapshotForUpload(backend, config, local, head, remoteForUpload, snapshotOptions);
|
|
118
|
+
if (action) {
|
|
119
|
+
upload = keepProtectedRemoteSessions(config, upload, remoteForUpload, snapshotOptions.sessionDir, reviewedProtectedPaths);
|
|
120
|
+
validatePortableSnapshot(upload);
|
|
121
|
+
}
|
|
120
122
|
if (!config.skipSecretScan) {
|
|
121
123
|
const secrets = scanSnapshot(local);
|
|
122
124
|
if (secrets.length > 0) {
|
|
123
125
|
throw new Error(`Refusing to push possible secrets:\n${secrets.map((s) => `- ${s}`).join("\n")}`);
|
|
124
126
|
}
|
|
125
127
|
}
|
|
126
|
-
if (
|
|
128
|
+
if (action) {
|
|
129
|
+
await checkpointDirectionalAction(options, {
|
|
130
|
+
direction: "push",
|
|
131
|
+
config,
|
|
132
|
+
state,
|
|
133
|
+
local: localRaw,
|
|
134
|
+
remote: remoteForUpload,
|
|
135
|
+
head,
|
|
136
|
+
sessionDir: snapshotOptions.sessionDir,
|
|
137
|
+
protectedPaths: reviewedProtectedPaths,
|
|
138
|
+
});
|
|
139
|
+
validateMutation();
|
|
140
|
+
}
|
|
141
|
+
else if (!(await confirmPush(ctx, options, config, backend, local, upload, head, remoteForUpload, validateMutation))) {
|
|
127
142
|
return "cancelled";
|
|
128
143
|
}
|
|
129
144
|
if (options.force) {
|
|
@@ -147,14 +162,17 @@ export async function push(ctx, options, input, factory = createSyncBackend) {
|
|
|
147
162
|
}
|
|
148
163
|
}
|
|
149
164
|
validateMutation();
|
|
150
|
-
if (
|
|
165
|
+
if (action)
|
|
166
|
+
await validateDirectionalSettings(options, config, validateMutation);
|
|
167
|
+
else if (configIdentity !== syncCheckConfigFingerprint(await loadConfig(options.setup)))
|
|
151
168
|
throw new Error("Settings changed during push review.");
|
|
152
169
|
validateMutation();
|
|
153
|
-
if (
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
170
|
+
if (!action &&
|
|
171
|
+
(config.scope === "agent-directory" ||
|
|
172
|
+
validateIgnorePolicy ||
|
|
173
|
+
!options.yes ||
|
|
174
|
+
config.localFields !== undefined ||
|
|
175
|
+
(await readMergeJournal(config)))) {
|
|
158
176
|
const current = await createSnapshot(config.snapshotIdentity, snapshotOptions);
|
|
159
177
|
if (!sameHashes(fileHashMap(current), fileHashMap(localRaw)))
|
|
160
178
|
throw new Error("Local content changed during push review; retry from current bytes.");
|
|
@@ -167,65 +185,127 @@ export async function push(ctx, options, input, factory = createSyncBackend) {
|
|
|
167
185
|
snapshotOptions.sessionDir !== (await effectiveSessionRoot(ctx, config, options.signal)))
|
|
168
186
|
throw new Error("Pi session storage changed during push review.");
|
|
169
187
|
validateMutation();
|
|
170
|
-
if (
|
|
188
|
+
if (action)
|
|
189
|
+
await validateDirectionalSettings(options, config, validateMutation);
|
|
190
|
+
else if (configIdentity !== syncCheckConfigFingerprint(await loadConfig(options.setup)))
|
|
171
191
|
throw new Error("Settings changed during push review.");
|
|
172
192
|
validateMutation();
|
|
173
|
-
const validatePublication = validateIgnorePolicy || options.background
|
|
174
|
-
? () =>
|
|
175
|
-
(
|
|
176
|
-
|
|
177
|
-
|
|
178
|
-
|
|
179
|
-
|
|
193
|
+
const validatePublication = validateIgnorePolicy || options.background || action
|
|
194
|
+
? async () => {
|
|
195
|
+
if (action && !sameRemoteHead(backend, head, await backend.readHead(options.signal)))
|
|
196
|
+
throw new Error("Remote changed before Push could publish.");
|
|
197
|
+
validateMutation();
|
|
198
|
+
await validateDirectionalPublication(ctx, config, snapshotOptions, state, fileHashMap(localRaw), action
|
|
199
|
+
? async () => {
|
|
200
|
+
await validateIgnorePolicy?.();
|
|
201
|
+
await validateDirectionalSettings(options, config, validateMutation);
|
|
202
|
+
checkLiveProtection(ctx, snapshotOptions.sessionDir, reviewedProtectedPaths);
|
|
203
|
+
}
|
|
204
|
+
: (validateIgnorePolicy ??
|
|
205
|
+
(async () => {
|
|
206
|
+
if (configIdentity !== syncCheckConfigFingerprint(await loadConfig(options.setup, options.signal)))
|
|
207
|
+
throw new Error("Settings changed before push publication.");
|
|
208
|
+
validateMutation();
|
|
209
|
+
})), "push", Boolean(action));
|
|
210
|
+
}
|
|
180
211
|
: undefined;
|
|
181
212
|
if (validateIgnorePolicy) {
|
|
182
213
|
validatePortableSnapshot(upload);
|
|
183
214
|
await validateIgnorePolicy();
|
|
184
215
|
}
|
|
185
|
-
|
|
186
|
-
|
|
187
|
-
|
|
188
|
-
|
|
189
|
-
|
|
216
|
+
if (action)
|
|
217
|
+
await validatePublication?.();
|
|
218
|
+
const accepted = action ? filterSnapshotForConfigPolicy(upload, config, snapshotOptions) : local;
|
|
219
|
+
const restore = action && head && remoteForUpload
|
|
220
|
+
? remoteRestoreGuidance(head, remoteForUpload, config, snapshotOptions.sessionDir)
|
|
221
|
+
: undefined;
|
|
222
|
+
let result;
|
|
223
|
+
let startedPublication = false;
|
|
224
|
+
try {
|
|
225
|
+
result = await backend.publishSnapshot(upload, expectedRemoteHead(head), {
|
|
226
|
+
...(restore ? { preservePreviousHead: true } : {}),
|
|
227
|
+
signal: options.signal,
|
|
228
|
+
onCommit: action
|
|
229
|
+
? () => {
|
|
230
|
+
options.onCommit?.();
|
|
231
|
+
startedPublication = true;
|
|
232
|
+
}
|
|
233
|
+
: options.onCommit,
|
|
234
|
+
...(validatePublication ? { validateIgnorePolicy: validatePublication } : {}),
|
|
235
|
+
});
|
|
236
|
+
}
|
|
237
|
+
catch (error) {
|
|
238
|
+
if (!action || (!startedPublication && options.signal?.aborted))
|
|
239
|
+
throw error;
|
|
240
|
+
const mightCommit = startedPublication && !(error instanceof SyncBackendConflictError && error.phase === "before-commit");
|
|
241
|
+
const phase = mightCommit ? "Push publication outcome is not verified." : "Push did not replace the remote head.";
|
|
242
|
+
throw new Error([phase, errorMessage(error), startedPublication ? restore : undefined].filter(Boolean).join(" "), {
|
|
243
|
+
cause: error,
|
|
244
|
+
});
|
|
245
|
+
}
|
|
190
246
|
try {
|
|
191
247
|
await writeStateForConfig(config, {
|
|
192
248
|
version: VERSION,
|
|
193
249
|
profile: config.snapshotIdentity,
|
|
194
250
|
lastAppliedSnapshot: result.head.snapshotId,
|
|
195
251
|
lastRemoteRevision: result.head.revision,
|
|
196
|
-
lastFileHashes: fileHashMap(
|
|
252
|
+
lastFileHashes: fileHashMap(accepted),
|
|
197
253
|
...acceptedSelection(config),
|
|
198
254
|
...(config.localFields !== undefined ? { localFields: config.localFields } : {}),
|
|
199
|
-
},
|
|
255
|
+
}, accepted, validateCommittedOwner);
|
|
200
256
|
}
|
|
201
257
|
catch (error) {
|
|
202
|
-
|
|
258
|
+
const persistence = new PublicationStatePersistenceError(result.head, error);
|
|
259
|
+
throw restore ? new Error(`${persistence.message} ${restore}`, { cause: persistence }) : persistence;
|
|
203
260
|
}
|
|
204
|
-
|
|
205
|
-
|
|
206
|
-
|
|
207
|
-
|
|
208
|
-
|
|
209
|
-
|
|
210
|
-
|
|
211
|
-
|
|
212
|
-
|
|
213
|
-
|
|
261
|
+
try {
|
|
262
|
+
if (!action)
|
|
263
|
+
await retireMergeJournal(config);
|
|
264
|
+
await pruneMergeBaselines(config, await readStateForConfig(config), validateCommittedOwner);
|
|
265
|
+
validateCommittedOwner();
|
|
266
|
+
}
|
|
267
|
+
catch (error) {
|
|
268
|
+
if (!action)
|
|
269
|
+
throw error;
|
|
270
|
+
throw new Error([
|
|
271
|
+
`Remote publication ${result.head.snapshotId} is active, but final bookkeeping failed: ${errorMessage(error)}`,
|
|
272
|
+
restore,
|
|
214
273
|
]
|
|
215
274
|
.filter(Boolean)
|
|
216
|
-
.join("
|
|
275
|
+
.join(" "), { cause: error });
|
|
217
276
|
}
|
|
277
|
+
if (!options.signal?.aborted)
|
|
278
|
+
setSyncStatus(ctx, undefined);
|
|
279
|
+
if (options.signal?.aborted && !action)
|
|
280
|
+
return;
|
|
281
|
+
notifyDirectionalSuccess(ctx, options, [
|
|
282
|
+
`Pushed ${upload.files.length} files from sync setup “${config.setupName}” as ${result.head.snapshotId}.`,
|
|
283
|
+
restore,
|
|
284
|
+
...result.warnings,
|
|
285
|
+
]
|
|
286
|
+
.filter(Boolean)
|
|
287
|
+
.join("\n"), result.warnings.length > 0);
|
|
218
288
|
return "applied";
|
|
219
289
|
}
|
|
220
290
|
export async function pull(ctx, options, factory = createSyncBackend, initial) {
|
|
221
|
-
const
|
|
291
|
+
const action = foregroundDirectionalAction(ctx, options);
|
|
292
|
+
if (action && initial)
|
|
293
|
+
throw new Error("Single-action Pull cannot be used for initial-source selection.");
|
|
294
|
+
const validateOwner = captureMutationOwner(ctx, options.signal);
|
|
222
295
|
const validateCommittedOwner = captureMutationOwner(ctx);
|
|
223
|
-
const config = await loadConfig(options.setup);
|
|
296
|
+
const config = action ? await loadConfigForCheck(undefined, options.signal) : await loadConfig(options.setup);
|
|
297
|
+
const validateMutation = () => {
|
|
298
|
+
validateOwner();
|
|
299
|
+
action?.validate(config);
|
|
300
|
+
};
|
|
224
301
|
validateMutation();
|
|
225
302
|
const configIdentity = syncCheckConfigFingerprint(config);
|
|
226
303
|
if (initial && initial.configIdentity !== configIdentity)
|
|
227
304
|
throw new Error("Settings changed before initial pull; run sync again.");
|
|
228
305
|
await requireNoMergeJournal(config, options);
|
|
306
|
+
if (action && (await pendingTransactionEntries()).length)
|
|
307
|
+
throw new Error("A pending local transaction must be reconciled before Pull; its evidence was retained.");
|
|
308
|
+
validateMutation();
|
|
229
309
|
setSyncStatus(ctx, `pulling ${config.setupName}`, { activity: "pulling" });
|
|
230
310
|
const backend = await factory(config);
|
|
231
311
|
const state = await readStateForConfig(config);
|
|
@@ -234,6 +314,8 @@ export async function pull(ctx, options, factory = createSyncBackend, initial) {
|
|
|
234
314
|
throw new Error("Baseline changed before initial pull; run sync again.");
|
|
235
315
|
requireResolvedDirectionalProgress(state, options);
|
|
236
316
|
const snapshotOptions = await resolvedSnapshotOptionsForContext(ctx, config, options.signal);
|
|
317
|
+
if (action)
|
|
318
|
+
snapshotOptions.sessionDir = await effectiveSessionRoot(ctx, config, options.signal);
|
|
237
319
|
validateMutation();
|
|
238
320
|
const validateIgnorePolicy = ignorePolicyValidator({ ...snapshotOptions, validateMutation });
|
|
239
321
|
if (initial && initial.sessionDir !== snapshotOptions.sessionDir)
|
|
@@ -242,11 +324,23 @@ export async function pull(ctx, options, factory = createSyncBackend, initial) {
|
|
|
242
324
|
validateMutation();
|
|
243
325
|
if (initial && !sameHashes(initial.localHashes, fileHashMap(local)))
|
|
244
326
|
throw new Error("Local bytes changed before initial pull; run sync again.");
|
|
245
|
-
|
|
327
|
+
let preimageRemote;
|
|
328
|
+
const { head, snapshot: remote } = await readRemoteSnapshot(backend, config, options.signal, {
|
|
329
|
+
...snapshotOptions,
|
|
330
|
+
...(action
|
|
331
|
+
? {
|
|
332
|
+
validateSelection: (raw) => {
|
|
333
|
+
requireDirectionalSelection(config, state, local, raw, snapshotOptions.sessionDir);
|
|
334
|
+
preimageRemote = raw;
|
|
335
|
+
},
|
|
336
|
+
}
|
|
337
|
+
: {}),
|
|
338
|
+
});
|
|
246
339
|
validateMutation();
|
|
247
340
|
if (initial && !matchesInitialHead(initial, backend, head))
|
|
248
341
|
throw new Error("Remote changed before initial pull; run sync again.");
|
|
249
|
-
|
|
342
|
+
if (!action)
|
|
343
|
+
requireIgnoreBaselineReview(ctx, config, state, options);
|
|
250
344
|
const localChanged = hasLocalChanges(local, state, config);
|
|
251
345
|
if (!remote) {
|
|
252
346
|
throw createSyncDecision({
|
|
@@ -269,11 +363,11 @@ export async function pull(ctx, options, factory = createSyncBackend, initial) {
|
|
|
269
363
|
}
|
|
270
364
|
const logical = portableSnapshot(remote, config.localFields);
|
|
271
365
|
const physical = overlayLocalFields(logical, local, config.localFields);
|
|
272
|
-
if (config.scope === "agent-directory")
|
|
366
|
+
if (action || config.scope === "agent-directory")
|
|
273
367
|
requireStableMergeSessionRoot(local, physical, config.scope);
|
|
274
368
|
const reviewedProtectedPaths = protectedSessionPaths(ctx, snapshotOptions.sessionDir);
|
|
275
369
|
const remoteChanged = hasRemoteChanges(remote, state, config, reviewedProtectedPaths);
|
|
276
|
-
if (localChanged && remoteChanged && state.lastAppliedSnapshot && !options.force) {
|
|
370
|
+
if (!action && localChanged && remoteChanged && state.lastAppliedSnapshot && !options.force) {
|
|
277
371
|
throw createSyncDecision({
|
|
278
372
|
kind: "both-changed",
|
|
279
373
|
config,
|
|
@@ -285,14 +379,32 @@ export async function pull(ctx, options, factory = createSyncBackend, initial) {
|
|
|
285
379
|
directMessage: "Both local and remote changed since last sync. Run /sync diff, then choose /sync pull --force or /sync push --force.",
|
|
286
380
|
});
|
|
287
381
|
}
|
|
288
|
-
const caseCleanup = options.yes ? [] : await inspectCaseCleanup(physical, snapshotOptions);
|
|
382
|
+
const caseCleanup = action || options.yes ? [] : await inspectCaseCleanup(physical, snapshotOptions);
|
|
289
383
|
validateMutation();
|
|
290
|
-
if (!
|
|
384
|
+
if (!action &&
|
|
385
|
+
!options.yes &&
|
|
291
386
|
!(await confirmTransferReview(ctx, snapshotIncludesSessions(remote) ? "Pull pi settings and sessions?" : "Pull pi settings?", formatPullSummary(config, backend.destination, local, physical, reviewedProtectedPaths, caseCleanup.map((item) => item.path)), options.signal, validateMutation))) {
|
|
292
387
|
setSyncStatus(ctx, undefined);
|
|
293
388
|
ctx.ui.notify("Pull cancelled.", "info");
|
|
294
389
|
return "cancelled";
|
|
295
390
|
}
|
|
391
|
+
const validateActionBoundary = action
|
|
392
|
+
? () => requireDirectionalPullBoundary(ctx, options, config, config.scope ? physical : { ...physical, files: [...local.files, ...physical.files] }, snapshotOptions, reviewedProtectedPaths, validateMutation)
|
|
393
|
+
: undefined;
|
|
394
|
+
if (action) {
|
|
395
|
+
await checkpointDirectionalAction(options, {
|
|
396
|
+
direction: "pull",
|
|
397
|
+
config,
|
|
398
|
+
state,
|
|
399
|
+
local,
|
|
400
|
+
remote: preimageRemote,
|
|
401
|
+
head,
|
|
402
|
+
sessionDir: snapshotOptions.sessionDir,
|
|
403
|
+
protectedPaths: reviewedProtectedPaths,
|
|
404
|
+
});
|
|
405
|
+
validateMutation();
|
|
406
|
+
await validateActionBoundary?.();
|
|
407
|
+
}
|
|
296
408
|
throwIfAborted(options.signal);
|
|
297
409
|
const recoveringMerge = Boolean(await readMergeJournal(config));
|
|
298
410
|
const requireFreshRecovery = async () => {
|
|
@@ -307,7 +419,7 @@ export async function pull(ctx, options, factory = createSyncBackend, initial) {
|
|
|
307
419
|
};
|
|
308
420
|
if (recoveringMerge)
|
|
309
421
|
await requireFreshRecovery();
|
|
310
|
-
if (configIdentity !== syncCheckConfigFingerprint(await loadConfig(options.setup)))
|
|
422
|
+
if (!action && configIdentity !== syncCheckConfigFingerprint(await loadConfig(options.setup)))
|
|
311
423
|
throw new Error("Settings changed during pull review.");
|
|
312
424
|
validateMutation();
|
|
313
425
|
const refreshed = await createSnapshot(config.snapshotIdentity, snapshotOptions);
|
|
@@ -316,78 +428,116 @@ export async function pull(ctx, options, factory = createSyncBackend, initial) {
|
|
|
316
428
|
if (!sameRemoteHead(backend, head, await backend.readHead(options.signal)))
|
|
317
429
|
throw new Error("Remote changed during pull review.");
|
|
318
430
|
validateMutation();
|
|
319
|
-
const
|
|
320
|
-
const
|
|
321
|
-
|
|
322
|
-
|
|
323
|
-
|
|
324
|
-
|
|
325
|
-
|
|
326
|
-
|
|
327
|
-
|
|
328
|
-
|
|
329
|
-
|
|
330
|
-
|
|
331
|
-
|
|
332
|
-
|
|
333
|
-
|
|
334
|
-
|
|
335
|
-
|
|
336
|
-
|
|
337
|
-
|
|
338
|
-
|
|
339
|
-
|
|
340
|
-
|
|
341
|
-
|
|
342
|
-
|
|
343
|
-
await
|
|
431
|
+
const restorePoint = action ? await backupDirectionalSnapshot(local, options.signal) : undefined;
|
|
432
|
+
const backup = restorePoint?.backupPath ?? (await backupLocal(config.snapshotIdentity, snapshotOptions, options.signal));
|
|
433
|
+
let mayHaveApplied = false;
|
|
434
|
+
let installed = false;
|
|
435
|
+
try {
|
|
436
|
+
const applySessionDir = action
|
|
437
|
+
? snapshotOptions.sessionDir
|
|
438
|
+
: await sessionDirForApply(ctx, physical, snapshotOptions);
|
|
439
|
+
if (recoveringMerge)
|
|
440
|
+
await requireFreshRecovery();
|
|
441
|
+
const currentState = await readStateForConfig(config);
|
|
442
|
+
validateMutation();
|
|
443
|
+
if ((validateIgnorePolicy || initial || !options.yes) &&
|
|
444
|
+
syncStateFingerprint(currentState) !== syncStateFingerprint(state))
|
|
445
|
+
throw new Error("Baseline changed during pull review.");
|
|
446
|
+
requireResolvedDirectionalProgress(currentState, options);
|
|
447
|
+
if (!action && !options.yes)
|
|
448
|
+
await revalidateCaseCleanup(caseCleanup, physical, snapshotOptions);
|
|
449
|
+
if (initial && !matchesInitialHead(initial, backend, await backend.readHead(options.signal)))
|
|
450
|
+
throw new Error("Remote changed during initial pull; run sync again.");
|
|
451
|
+
validateMutation();
|
|
452
|
+
if (config.scope && applySessionDir !== (await effectiveSessionRoot(ctx, config, options.signal)))
|
|
453
|
+
throw new Error("Pi session storage changed during pull review.");
|
|
454
|
+
validateMutation();
|
|
455
|
+
if (!action && configIdentity !== syncCheckConfigFingerprint(await loadConfig(options.setup)))
|
|
456
|
+
throw new Error("Settings changed during pull review.");
|
|
457
|
+
validateMutation();
|
|
458
|
+
if (validateIgnorePolicy) {
|
|
459
|
+
if (!sameRemoteHead(backend, head, await backend.readHead(options.signal)))
|
|
460
|
+
throw new Error("Remote changed during pull review.");
|
|
461
|
+
await validateIgnorePolicy();
|
|
462
|
+
}
|
|
463
|
+
const protectedPaths = protectedSessionPaths(ctx, snapshotOptions.sessionDir);
|
|
464
|
+
const applyGuards = directionalApplyGuards({
|
|
465
|
+
ctx,
|
|
466
|
+
config,
|
|
467
|
+
backend,
|
|
468
|
+
state,
|
|
469
|
+
sessionDir: snapshotOptions.sessionDir,
|
|
470
|
+
head,
|
|
471
|
+
protectedPaths: reviewedProtectedPaths,
|
|
472
|
+
signal: options.signal,
|
|
473
|
+
onCommit: action
|
|
474
|
+
? () => {
|
|
475
|
+
options.onCommit?.();
|
|
476
|
+
mayHaveApplied = true;
|
|
477
|
+
}
|
|
478
|
+
: options.onCommit,
|
|
479
|
+
validateMutation,
|
|
480
|
+
validateIgnorePolicy: action
|
|
481
|
+
? async () => {
|
|
482
|
+
await restorePoint?.validate();
|
|
483
|
+
await validateIgnorePolicy?.();
|
|
484
|
+
await validateActionBoundary?.();
|
|
485
|
+
}
|
|
486
|
+
: validateIgnorePolicy,
|
|
487
|
+
effectiveRoot: Boolean(action),
|
|
488
|
+
});
|
|
489
|
+
applyGuards.validateMutation();
|
|
490
|
+
if (!validateIgnorePolicy && !action)
|
|
491
|
+
options.onCommit?.();
|
|
492
|
+
const lastFileHashes = await applySnapshot(physical, protectedPaths, {
|
|
493
|
+
...snapshotOptions,
|
|
494
|
+
sessionDir: applySessionDir,
|
|
495
|
+
...applyGuards,
|
|
496
|
+
...(action || config.scope === "agent-directory" || validateIgnorePolicy ? { reviewedLocal: local } : {}),
|
|
497
|
+
expectedFileHashes: config.localFields?.length
|
|
498
|
+
? { "settings.json": fileHashMap(local)["settings.json"] ?? null }
|
|
499
|
+
: undefined,
|
|
500
|
+
});
|
|
501
|
+
// Local installation is complete; user cancellation must not leave the baseline stale.
|
|
502
|
+
installed = true;
|
|
503
|
+
validateCommittedOwner();
|
|
504
|
+
await writeStateForConfig(config, {
|
|
505
|
+
version: VERSION,
|
|
506
|
+
profile: config.snapshotIdentity,
|
|
507
|
+
lastAppliedSnapshot: remote.id,
|
|
508
|
+
lastRemoteRevision: head?.revision,
|
|
509
|
+
lastFileHashes: config.localFields?.length
|
|
510
|
+
? { ...lastFileHashes, "settings.json": fileHashMap(logical)["settings.json"] }
|
|
511
|
+
: lastFileHashes,
|
|
512
|
+
...acceptedSelection(config),
|
|
513
|
+
...(config.localFields !== undefined ? { localFields: config.localFields } : {}),
|
|
514
|
+
}, logical, validateCommittedOwner);
|
|
515
|
+
if (!action)
|
|
516
|
+
await retireMergeJournal(config);
|
|
517
|
+
await pruneMergeBaselines(config, await readStateForConfig(config), validateCommittedOwner);
|
|
518
|
+
validateCommittedOwner();
|
|
344
519
|
}
|
|
345
|
-
|
|
346
|
-
|
|
347
|
-
|
|
348
|
-
|
|
349
|
-
|
|
350
|
-
|
|
351
|
-
|
|
352
|
-
|
|
353
|
-
|
|
354
|
-
|
|
355
|
-
|
|
356
|
-
|
|
357
|
-
|
|
358
|
-
|
|
359
|
-
applyGuards.validateMutation();
|
|
360
|
-
if (!validateIgnorePolicy)
|
|
361
|
-
options.onCommit?.();
|
|
362
|
-
const lastFileHashes = await applySnapshot(physical, protectedPaths, {
|
|
363
|
-
...snapshotOptions,
|
|
364
|
-
sessionDir: applySessionDir,
|
|
365
|
-
...applyGuards,
|
|
366
|
-
...(config.scope === "agent-directory" || validateIgnorePolicy ? { reviewedLocal: local } : {}),
|
|
367
|
-
expectedFileHashes: config.localFields?.length
|
|
368
|
-
? { "settings.json": fileHashMap(local)["settings.json"] ?? null }
|
|
369
|
-
: undefined,
|
|
370
|
-
});
|
|
371
|
-
// Local installation is complete; user cancellation must not leave the baseline stale.
|
|
372
|
-
validateCommittedOwner();
|
|
373
|
-
await writeStateForConfig(config, {
|
|
374
|
-
version: VERSION,
|
|
375
|
-
profile: config.snapshotIdentity,
|
|
376
|
-
lastAppliedSnapshot: remote.id,
|
|
377
|
-
lastRemoteRevision: head?.revision,
|
|
378
|
-
lastFileHashes: config.localFields?.length
|
|
379
|
-
? { ...lastFileHashes, "settings.json": fileHashMap(logical)["settings.json"] }
|
|
380
|
-
: lastFileHashes,
|
|
381
|
-
...acceptedSelection(config),
|
|
382
|
-
...(config.localFields !== undefined ? { localFields: config.localFields } : {}),
|
|
383
|
-
}, logical, validateCommittedOwner);
|
|
384
|
-
await retireMergeJournal(config);
|
|
385
|
-
await pruneMergeBaselines(config, await readStateForConfig(config), validateCommittedOwner);
|
|
386
|
-
validateCommittedOwner();
|
|
387
|
-
if (options.signal?.aborted)
|
|
520
|
+
catch (error) {
|
|
521
|
+
if (!action || (options.signal?.aborted && !mayHaveApplied))
|
|
522
|
+
throw error;
|
|
523
|
+
const phase = installed
|
|
524
|
+
? "Local snapshot was applied, but final bookkeeping failed."
|
|
525
|
+
: mayHaveApplied
|
|
526
|
+
? "Local apply did not finish; its outcome is not verified."
|
|
527
|
+
: "Pull stopped before applying local files.";
|
|
528
|
+
const message = `${phase} ${error instanceof LocalBackupVerificationError ? "" : `${localRestoreGuidance(backup)} `}${errorMessage(error)}`;
|
|
529
|
+
if (error instanceof AggregateError)
|
|
530
|
+
throw new AggregateError(error.errors, message, { cause: error });
|
|
531
|
+
throw new Error(message, { cause: error });
|
|
532
|
+
}
|
|
533
|
+
if (options.signal?.aborted && !action)
|
|
388
534
|
return "applied";
|
|
389
|
-
|
|
390
|
-
|
|
535
|
+
if (!options.signal?.aborted)
|
|
536
|
+
setSyncStatus(ctx, undefined);
|
|
537
|
+
if (action) {
|
|
538
|
+
notifyDirectionalSuccess(ctx, options, `Pulled snapshot ${remote.id}. ${localRestoreGuidance(backup)}`);
|
|
539
|
+
}
|
|
540
|
+
else if (!options.silent) {
|
|
391
541
|
ctx.ui.notify(`Pulled ${remote.files.length} files from ${remote.id}. Backup: ${backup}`, "info");
|
|
392
542
|
}
|
|
393
543
|
else if (options.auto && syncSessionsSelected(config) && snapshotIncludesSessions(remote)) {
|
|
@@ -398,6 +548,8 @@ export async function pull(ctx, options, factory = createSyncBackend, initial) {
|
|
|
398
548
|
return "applied";
|
|
399
549
|
}
|
|
400
550
|
export async function syncBoth(ctx, options, factory = createSyncBackend) {
|
|
551
|
+
if (options.directionalAction)
|
|
552
|
+
throw new Error("Single-action transfer cannot authorize bidirectional Sync.");
|
|
401
553
|
const validateMutation = captureMutationOwner(ctx, options.signal);
|
|
402
554
|
const config = await loadConfig(options.setup);
|
|
403
555
|
throwIfAborted(options.signal);
|
|
@@ -550,192 +702,6 @@ export async function syncBoth(ctx, options, factory = createSyncBackend) {
|
|
|
550
702
|
if (!options.silent)
|
|
551
703
|
ctx.ui.notify("pi-sync is already up to date.", "info");
|
|
552
704
|
}
|
|
553
|
-
export async function rollback(ctx, options, factory = createSyncBackend, expectedSelection) {
|
|
554
|
-
const validateMutation = captureMutationOwner(ctx, options.signal);
|
|
555
|
-
const validateCommittedOwner = captureMutationOwner(ctx);
|
|
556
|
-
const target = options.args[0];
|
|
557
|
-
if (!target)
|
|
558
|
-
throw new Error("Usage: /sync rollback <snapshot-id> [--yes]");
|
|
559
|
-
const config = await loadConfig(options.setup);
|
|
560
|
-
validateMutation();
|
|
561
|
-
if (config.ignorePolicy && (options.yes || options.auto || !ctx.hasUI))
|
|
562
|
-
throw new Error("Ignore-file rollback requires a separate foreground review; --yes cannot confirm it.");
|
|
563
|
-
const backend = await factory(config);
|
|
564
|
-
if (expectedSelection &&
|
|
565
|
-
(backend.identity !== expectedSelection.backendIdentity || config.setupName !== expectedSelection.setup)) {
|
|
566
|
-
throw new Error("Sync setup or storage location changed while history was open; reopen history and retry.");
|
|
567
|
-
}
|
|
568
|
-
// --force is only a rollback compatibility flag, not a reviewed merge-recovery direction.
|
|
569
|
-
await requireNoMergeJournal(config);
|
|
570
|
-
const state = await readStateForConfig(config);
|
|
571
|
-
validateMutation();
|
|
572
|
-
requireIgnoreBaselineReview(ctx, config, state, options);
|
|
573
|
-
requireResolvedDirectionalProgress(state, options, false);
|
|
574
|
-
const decoded = await backend.readSnapshot(target, options.signal);
|
|
575
|
-
validateMutation();
|
|
576
|
-
validatePortableSnapshot(decoded);
|
|
577
|
-
if (!config.ignorePolicy)
|
|
578
|
-
requireLegacyTransferPolicy(decoded);
|
|
579
|
-
if (config.ignorePolicy && expectedSelection?.snapshotId && decoded.id !== expectedSelection.snapshotId)
|
|
580
|
-
throw new Error("Historical snapshot changed while history was open; reopen history and retry.");
|
|
581
|
-
if (!sameLocalFields(decoded.localFields, config.localFields))
|
|
582
|
-
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.");
|
|
583
|
-
const snapshotOptions = await resolvedSnapshotOptionsForContext(ctx, config, options.signal);
|
|
584
|
-
validateMutation();
|
|
585
|
-
const validateIgnorePolicy = ignorePolicyValidator({ ...snapshotOptions, validateMutation });
|
|
586
|
-
const selected = filterSnapshotForConfigPolicy(config.ignorePolicy || syncSessionsSelected(config) ? decoded : snapshotWithoutSessions(decoded), config, snapshotOptions);
|
|
587
|
-
const remote = portableSnapshot(regenerateSnapshotIdentity(selected), config.localFields);
|
|
588
|
-
const local = await createSnapshot(config.snapshotIdentity, snapshotOptions);
|
|
589
|
-
const expectedHead = await backend.readHead(options.signal);
|
|
590
|
-
const currentRemote = expectedHead
|
|
591
|
-
? await readSnapshotForHead(backend, expectedHead, options.signal, config)
|
|
592
|
-
: undefined;
|
|
593
|
-
validateMutation();
|
|
594
|
-
if (config.ignorePolicy && currentRemote)
|
|
595
|
-
requireCompatibleRemoteSelection(config, currentRemote);
|
|
596
|
-
if (!(await confirmFieldMigration(ctx, config, state, currentRemote, true, options.signal)))
|
|
597
|
-
return;
|
|
598
|
-
validateMutation();
|
|
599
|
-
const physical = overlayLocalFields(remote, local, config.localFields);
|
|
600
|
-
if (config.scope === "agent-directory")
|
|
601
|
-
requireStableMergeSessionRoot(local, physical, config.scope);
|
|
602
|
-
const upload = config.ignorePolicy
|
|
603
|
-
? remote
|
|
604
|
-
: await snapshotForUpload(backend, config, remote, expectedHead, currentRemote, snapshotOptions);
|
|
605
|
-
if (config.scope === "agent-directory")
|
|
606
|
-
validatePortableSnapshot(upload);
|
|
607
|
-
const caseCleanup = options.yes ? [] : await inspectCaseCleanup(physical, snapshotOptions);
|
|
608
|
-
validateMutation();
|
|
609
|
-
const reviewedProtectedPaths = protectedSessionPaths(ctx, snapshotOptions.sessionDir);
|
|
610
|
-
if (!options.yes) {
|
|
611
|
-
const title = snapshotIncludesSessions(remote) ? "Rollback pi settings and sessions?" : "Rollback pi settings?";
|
|
612
|
-
const summary = formatRollbackSummary(config, backend.destination, local, physical, target, reviewedProtectedPaths, caseCleanup.map((item) => item.path));
|
|
613
|
-
const confirmed = config.ignorePolicy
|
|
614
|
-
? await confirmTransferReview(ctx, title, formatTransferReview("Historical files are projected through this device's current ignore rules. Historical rules are not adopted. Retained history is not erased.", "", summary, "", "Remote publication:", formatPublicationPreview(currentRemote, upload)), options.signal, validateMutation)
|
|
615
|
-
: await ctx.ui.confirm(title, summary.content, { signal: options.signal });
|
|
616
|
-
validateMutation();
|
|
617
|
-
if (!confirmed) {
|
|
618
|
-
ctx.ui.notify("Rollback cancelled.", "info");
|
|
619
|
-
return;
|
|
620
|
-
}
|
|
621
|
-
}
|
|
622
|
-
validateMutation();
|
|
623
|
-
const currentLocal = await createSnapshot(config.snapshotIdentity, snapshotOptions);
|
|
624
|
-
if (!sameHashes(fileHashMap(currentLocal), fileHashMap(local)))
|
|
625
|
-
throw new Error("Local bytes changed during rollback review.");
|
|
626
|
-
if (syncCheckConfigFingerprint(config) !== syncCheckConfigFingerprint(await loadConfig(options.setup)))
|
|
627
|
-
throw new Error("Settings changed during rollback review.");
|
|
628
|
-
if (!sameRemoteHead(backend, expectedHead, await backend.readHead(options.signal)))
|
|
629
|
-
throw new Error("Remote changed during rollback review.");
|
|
630
|
-
validateMutation();
|
|
631
|
-
throwIfAborted(options.signal);
|
|
632
|
-
const backup = await backupLocal(config.snapshotIdentity, snapshotOptions, options.signal);
|
|
633
|
-
const applySessionDir = await sessionDirForApply(ctx, config.scope ? physical : remote, snapshotOptions);
|
|
634
|
-
const currentState = await readStateForConfig(config);
|
|
635
|
-
if ((validateIgnorePolicy || !options.yes) && syncStateFingerprint(currentState) !== syncStateFingerprint(state))
|
|
636
|
-
throw new Error("Baseline changed during rollback review.");
|
|
637
|
-
requireResolvedDirectionalProgress(currentState, options, false);
|
|
638
|
-
if (!options.yes)
|
|
639
|
-
await revalidateCaseCleanup(caseCleanup, physical, snapshotOptions);
|
|
640
|
-
if (config.scope && applySessionDir !== (await effectiveSessionRoot(ctx, config, options.signal)))
|
|
641
|
-
throw new Error("Pi session storage changed during rollback review.");
|
|
642
|
-
validateMutation();
|
|
643
|
-
if (syncCheckConfigFingerprint(config) !== syncCheckConfigFingerprint(await loadConfig(options.setup)))
|
|
644
|
-
throw new Error("Settings changed during rollback review.");
|
|
645
|
-
validateMutation();
|
|
646
|
-
if (validateIgnorePolicy) {
|
|
647
|
-
if (!sameRemoteHead(backend, expectedHead, await backend.readHead(options.signal)))
|
|
648
|
-
throw new Error("Remote changed during rollback review.");
|
|
649
|
-
await validateIgnorePolicy();
|
|
650
|
-
}
|
|
651
|
-
const protectedPaths = protectedSessionPaths(ctx, snapshotOptions.sessionDir);
|
|
652
|
-
const applyGuards = directionalApplyGuards({
|
|
653
|
-
ctx,
|
|
654
|
-
config,
|
|
655
|
-
backend,
|
|
656
|
-
state,
|
|
657
|
-
sessionDir: snapshotOptions.sessionDir,
|
|
658
|
-
head: expectedHead,
|
|
659
|
-
protectedPaths: reviewedProtectedPaths,
|
|
660
|
-
signal: options.signal,
|
|
661
|
-
onCommit: options.onCommit,
|
|
662
|
-
validateMutation,
|
|
663
|
-
validateIgnorePolicy,
|
|
664
|
-
});
|
|
665
|
-
applyGuards.validateMutation();
|
|
666
|
-
if (!validateIgnorePolicy)
|
|
667
|
-
options.onCommit?.();
|
|
668
|
-
const lastFileHashes = await applySnapshot(physical, protectedPaths, {
|
|
669
|
-
...snapshotOptions,
|
|
670
|
-
sessionDir: applySessionDir,
|
|
671
|
-
...applyGuards,
|
|
672
|
-
...(config.scope === "agent-directory" || validateIgnorePolicy ? { reviewedLocal: local } : {}),
|
|
673
|
-
expectedFileHashes: config.localFields?.length
|
|
674
|
-
? { "settings.json": fileHashMap(local)["settings.json"] ?? null }
|
|
675
|
-
: undefined,
|
|
676
|
-
});
|
|
677
|
-
// The local transaction is retired; finish publication despite caller cancellation.
|
|
678
|
-
validateCommittedOwner();
|
|
679
|
-
let result;
|
|
680
|
-
try {
|
|
681
|
-
const signal = AbortSignal.timeout(POST_LOCAL_COMMIT_TIMEOUT_MS);
|
|
682
|
-
const validatePolicy = ignorePolicyValidator({
|
|
683
|
-
...snapshotOptions,
|
|
684
|
-
signal,
|
|
685
|
-
validateMutation: validateCommittedOwner,
|
|
686
|
-
});
|
|
687
|
-
const validatePublication = validatePolicy
|
|
688
|
-
? () => validateDirectionalPublication(ctx, config, { ...snapshotOptions, sessionDir: applySessionDir, signal }, state, lastFileHashes, validatePolicy, "rollback")
|
|
689
|
-
: undefined;
|
|
690
|
-
result = await backend.publishSnapshot(upload, expectedRemoteHead(expectedHead), {
|
|
691
|
-
signal,
|
|
692
|
-
...(validatePublication ? { validateIgnorePolicy: validatePublication } : {}),
|
|
693
|
-
});
|
|
694
|
-
}
|
|
695
|
-
catch (error) {
|
|
696
|
-
throw new RollbackPublicationError(backup, error);
|
|
697
|
-
}
|
|
698
|
-
try {
|
|
699
|
-
await writeStateForConfig(config, {
|
|
700
|
-
version: VERSION,
|
|
701
|
-
profile: config.snapshotIdentity,
|
|
702
|
-
lastAppliedSnapshot: result.head.snapshotId,
|
|
703
|
-
lastRemoteRevision: result.head.revision,
|
|
704
|
-
lastFileHashes: config.localFields?.length
|
|
705
|
-
? { ...lastFileHashes, "settings.json": fileHashMap(remote)["settings.json"] }
|
|
706
|
-
: lastFileHashes,
|
|
707
|
-
...acceptedSelection(config),
|
|
708
|
-
...(config.localFields !== undefined ? { localFields: config.localFields } : {}),
|
|
709
|
-
}, remote, validateCommittedOwner);
|
|
710
|
-
}
|
|
711
|
-
catch (error) {
|
|
712
|
-
throw new PublicationStatePersistenceError(result.head, error, backup);
|
|
713
|
-
}
|
|
714
|
-
await retireMergeJournal(config);
|
|
715
|
-
await pruneMergeBaselines(config, await readStateForConfig(config), validateCommittedOwner);
|
|
716
|
-
validateCommittedOwner();
|
|
717
|
-
if (options.signal?.aborted)
|
|
718
|
-
return;
|
|
719
|
-
ctx.ui.notify([
|
|
720
|
-
`Rolled back sync setup “${config.setupName}” to ${target}; latest: ${result.head.snapshotId}. Backup: ${backup}`,
|
|
721
|
-
...result.warnings,
|
|
722
|
-
]
|
|
723
|
-
.filter(Boolean)
|
|
724
|
-
.join("\n"), result.warnings.length > 0 ? "warning" : "info");
|
|
725
|
-
await maybeReload(ctx, options.signal, validateMutation);
|
|
726
|
-
}
|
|
727
|
-
async function maybeReload(ctx, signal, validateMutation = captureMutationOwner(ctx, signal)) {
|
|
728
|
-
if (signal?.aborted || typeof ctx.reload !== "function")
|
|
729
|
-
return;
|
|
730
|
-
validateMutation();
|
|
731
|
-
if (ctx.hasUI &&
|
|
732
|
-
(await ctx.ui.confirm("Reload Pi resources now?", "This reloads extensions, skills, prompts, themes, and context files."))) {
|
|
733
|
-
if (signal?.aborted)
|
|
734
|
-
return;
|
|
735
|
-
validateMutation();
|
|
736
|
-
await ctx.reload();
|
|
737
|
-
}
|
|
738
|
-
}
|
|
739
705
|
async function confirmPush(ctx, options, config, backend, local, upload, head, remote, validateMutation, title = snapshotIncludesSessions(upload) ? "Push pi settings and sessions?" : "Push pi settings?") {
|
|
740
706
|
validateMutation();
|
|
741
707
|
if (options.yes)
|