@narumitw/pi-sync-daemon 0.6.2 → 0.8.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 +19 -0
- package/README.md +17 -3
- package/dist/consent.js +26 -1
- package/dist/engine/backends/git/git-backend.d.ts +1 -1
- package/dist/engine/backends/git/git-backend.js +17 -2
- package/dist/engine/backends/git/git-storage.d.ts +1 -1
- package/dist/engine/backends/git/git-storage.js +39 -13
- package/dist/engine/backends/s3/s3-backend.js +8 -0
- package/dist/engine/backends/sync-backend.d.ts +1 -0
- package/dist/engine/backends/webdav/webdav-backend.js +8 -0
- package/dist/engine/ignore/effective-ignore-policy.d.ts +10 -0
- package/dist/engine/ignore/effective-ignore-policy.js +33 -0
- package/dist/engine/ignore/ignore-edit.d.ts +13 -0
- package/dist/engine/ignore/ignore-edit.js +102 -0
- package/dist/engine/ignore/ignore-initialization.d.ts +6 -0
- package/dist/engine/ignore/ignore-initialization.js +186 -0
- package/dist/engine/ignore/ignore-migration-prompt.d.ts +7 -0
- package/dist/engine/ignore/ignore-migration-prompt.js +139 -0
- package/dist/engine/ignore/ignore-settings.d.ts +15 -0
- package/dist/engine/ignore/ignore-settings.js +322 -0
- package/dist/engine/ignore/ignore-source.d.ts +27 -0
- package/dist/engine/ignore/ignore-source.js +250 -0
- package/dist/engine/paths.js +1 -1
- package/dist/engine/settings/config-file.d.ts +9 -2
- package/dist/engine/settings/config-file.js +126 -28
- package/dist/engine/settings/config.d.ts +5 -4
- package/dist/engine/settings/config.js +114 -13
- package/dist/engine/settings/settings-management.js +34 -32
- package/dist/engine/settings/settings-types.d.ts +40 -5
- package/dist/engine/settings/settings-validation.js +52 -24
- package/dist/engine/snapshot/session-paths.d.ts +6 -4
- package/dist/engine/snapshot/session-paths.js +84 -15
- package/dist/engine/snapshot/snapshot-apply.d.ts +7 -8
- package/dist/engine/snapshot/snapshot-apply.js +107 -21
- package/dist/engine/snapshot/snapshot-bounds.d.ts +4 -0
- package/dist/engine/snapshot/snapshot-bounds.js +20 -0
- package/dist/engine/snapshot/snapshot-codec.d.ts +1 -1
- package/dist/engine/snapshot/snapshot-codec.js +15 -7
- package/dist/engine/snapshot/snapshot-entry-projection.d.ts +10 -0
- package/dist/engine/snapshot/snapshot-entry-projection.js +78 -0
- package/dist/engine/snapshot/snapshot-exclude-safety.d.ts +9 -2
- package/dist/engine/snapshot/snapshot-exclude-safety.js +239 -24
- package/dist/engine/snapshot/snapshot-payload.d.ts +2 -0
- package/dist/engine/snapshot/snapshot-payload.js +10 -0
- package/dist/engine/snapshot/snapshot-reviewed-preimages.d.ts +2 -0
- package/dist/engine/snapshot/snapshot-reviewed-preimages.js +60 -0
- package/dist/engine/snapshot/snapshot-root-transition.d.ts +4 -0
- package/dist/engine/snapshot/snapshot-root-transition.js +16 -0
- package/dist/engine/snapshot/snapshot-transaction-images.d.ts +39 -0
- package/dist/engine/snapshot/snapshot-transaction-images.js +387 -0
- package/dist/engine/snapshot/snapshot-transaction-policy.d.ts +73 -0
- package/dist/engine/snapshot/snapshot-transaction-policy.js +276 -0
- package/dist/engine/snapshot/snapshot-transaction-stage.d.ts +32 -0
- package/dist/engine/snapshot/snapshot-transaction-stage.js +303 -0
- package/dist/engine/snapshot/snapshot-transaction-verification.d.ts +7 -0
- package/dist/engine/snapshot/snapshot-transaction-verification.js +78 -0
- package/dist/engine/snapshot/snapshot-transaction.d.ts +1 -11
- package/dist/engine/snapshot/snapshot-transaction.js +390 -364
- package/dist/engine/snapshot/snapshot-types.d.ts +47 -2
- package/dist/engine/snapshot/snapshot-types.js +9 -1
- package/dist/engine/snapshot/snapshot.d.ts +22 -3
- package/dist/engine/snapshot/snapshot.js +357 -43
- package/dist/engine/state/merge-baseline-store.js +20 -9
- package/dist/engine/state/state-types.d.ts +3 -0
- package/dist/engine/sync/apply-review.d.ts +3 -3
- package/dist/engine/sync/apply-review.js +5 -4
- package/dist/engine/sync/continuous-sync.js +37 -12
- package/dist/engine/sync/continuous-watch.d.ts +4 -1
- package/dist/engine/sync/continuous-watch.js +90 -15
- package/dist/engine/sync/exclude-policy.d.ts +4 -0
- package/dist/engine/sync/exclude-policy.js +8 -1
- package/dist/engine/sync/initialization.js +9 -4
- package/dist/engine/sync/local-fields.d.ts +3 -1
- package/dist/engine/sync/local-fields.js +88 -8
- package/dist/engine/sync/merge-apply.js +190 -47
- package/dist/engine/sync/merge-journal.d.ts +18 -4
- package/dist/engine/sync/merge-journal.js +126 -10
- package/dist/engine/sync/merged-sync.d.ts +1 -1
- package/dist/engine/sync/merged-sync.js +124 -68
- package/dist/engine/sync/partial-progress.js +5 -3
- package/dist/engine/sync/remote-snapshot.d.ts +10 -2
- package/dist/engine/sync/remote-snapshot.js +59 -17
- package/dist/engine/sync/session-push.js +7 -6
- package/dist/engine/sync/sync-decision.d.ts +4 -0
- package/dist/engine/sync/sync-decision.js +17 -2
- package/dist/engine/sync/sync-inspection.js +8 -6
- package/dist/engine/sync/sync-mutations.d.ts +10 -2
- package/dist/engine/sync/sync-mutations.js +245 -101
- package/dist/engine/sync/sync-policy.d.ts +36 -10
- package/dist/engine/sync/sync-policy.js +285 -20
- package/dist/engine/sync/sync-queries.js +49 -32
- package/dist/engine/sync/sync-state.d.ts +11 -0
- package/dist/engine/sync/sync-state.js +42 -5
- package/dist/engine/ui/sync-format.js +22 -7
- package/dist/engine-service.js +24 -2
- package/docs/architecture.md +28 -0
- package/package.json +2 -1
|
@@ -1,12 +1,13 @@
|
|
|
1
1
|
import { createSyncBackend } from "../backends/backend-factory.js";
|
|
2
2
|
import { expectedRemoteHead, } from "../backends/sync-backend.js";
|
|
3
3
|
import { loadConfig, syncCheckConfigFingerprint } from "../settings/config.js";
|
|
4
|
-
import { sessionDirForApply,
|
|
4
|
+
import { effectiveSessionRoot, requireStableMergeSessionRoot, resolvedSnapshotOptionsForContext, sessionDirForApply, } from "../snapshot/session-paths.js";
|
|
5
5
|
import { createSnapshot, filterSnapshotForConfigPolicy, mergeRemotePreservedFiles, regenerateSnapshotIdentity, scanSnapshot, snapshotIncludesSessions, snapshotWithoutSessions, } from "../snapshot/snapshot.js";
|
|
6
6
|
import { applySnapshot } from "../snapshot/snapshot-apply.js";
|
|
7
|
+
import { ignorePolicyValidator } from "../snapshot/snapshot-transaction-policy.js";
|
|
7
8
|
import { pruneMergeBaselines } from "../state/merge-baseline-store.js";
|
|
8
9
|
import { readStateForConfig, syncStateFingerprint, writeStateForConfig } from "../state/sync-state-store.js";
|
|
9
|
-
import { countPreservedRemoteFiles, formatPullSummary, formatPushSummary, formatRollbackSummary, } from "../ui/sync-format.js";
|
|
10
|
+
import { countPreservedRemoteFiles, formatPublicationPreview, formatPullSummary, formatPushSummary, formatRollbackSummary, } from "../ui/sync-format.js";
|
|
10
11
|
import { setSyncStatus } from "../ui/sync-status.js";
|
|
11
12
|
import { confirmTransferReview } from "../ui/transfer-review.js";
|
|
12
13
|
import { startupTransferEnabled } from "./agent-policy.js";
|
|
@@ -15,13 +16,13 @@ import { confirmFieldMigration } from "./field-migration.js";
|
|
|
15
16
|
import { overlayLocalFields, portableSnapshot, sameLocalFields, validatePortableSnapshot } from "./local-fields.js";
|
|
16
17
|
import { readMergeJournal, requireNoMergeJournal, retireMergeJournal } from "./merge-journal.js";
|
|
17
18
|
import { mergeSync } from "./merged-sync.js";
|
|
18
|
-
import { readRemoteSnapshot, readSnapshotForHead, requireCompatibleRemoteSelection } from "./remote-snapshot.js";
|
|
19
|
+
import { readRemoteSnapshot, readRemoteSnapshotForUpload, readSnapshotForHead, requireCompatibleRemoteSelection, sameRemoteHead, } from "./remote-snapshot.js";
|
|
19
20
|
import { throwIfAborted } from "./signals.js";
|
|
20
21
|
import { createSyncDecision } from "./sync-decision.js";
|
|
21
22
|
import { errorMessage } from "./sync-errors.js";
|
|
22
23
|
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";
|
|
24
|
+
import { formatIgnorePolicyComparison, inspectRemoteSelection, requireLegacyTransferPolicy, syncSelectionHasContent, syncSessionsSelected, } from "./sync-policy.js";
|
|
25
|
+
import { acceptedSelection, canPullRemoteSessionsOnFirstSync, canPullRemoteSettingsOnFirstSync, fileHashMap, hasLocalChanges, hasRemoteChanges, remoteChangedSinceState, sameHashes, shouldRefreshSyncedState, snapshotHashesMatchState, snapshotsMatch, syncPolicyChanged, } from "./sync-state.js";
|
|
25
26
|
const VERSION = 1;
|
|
26
27
|
const POST_LOCAL_COMMIT_TIMEOUT_MS = 30_000;
|
|
27
28
|
/** Whole-direction acceptance must never forget withheld state without an explicit recovery choice. */
|
|
@@ -29,6 +30,14 @@ function requireResolvedDirectionalProgress(state, options, allowForce = true) {
|
|
|
29
30
|
if (state.unresolved?.length && (options.auto || !options.force || !allowForce))
|
|
30
31
|
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
|
}
|
|
33
|
+
function requireIgnoreBaselineReview(ctx, config, state, options) {
|
|
34
|
+
if (config.ignorePolicy &&
|
|
35
|
+
state.lastAppliedSnapshot &&
|
|
36
|
+
syncPolicyChanged(state, config) &&
|
|
37
|
+
(options.auto || options.yes || !ctx.hasUI)) {
|
|
38
|
+
throw new Error("Local ignore-file policy or source changed since the last accepted baseline. Review a foreground directional transfer.");
|
|
39
|
+
}
|
|
40
|
+
}
|
|
32
41
|
export class PublicationStatePersistenceError extends Error {
|
|
33
42
|
head;
|
|
34
43
|
backupPath;
|
|
@@ -58,27 +67,37 @@ export async function push(ctx, options, input, factory = createSyncBackend) {
|
|
|
58
67
|
const state = input?.state ?? (await readStateForConfig(config));
|
|
59
68
|
validateMutation();
|
|
60
69
|
requireResolvedDirectionalProgress(state, options);
|
|
70
|
+
if (options.auto &&
|
|
71
|
+
(config.scope === "agent-directory" || state.scope !== undefined) &&
|
|
72
|
+
(options.force || !state.lastAppliedSnapshot || syncPolicyChanged(state, config))) {
|
|
73
|
+
throw new Error("Automatic push requires an accepted baseline with matching content policy.");
|
|
74
|
+
}
|
|
61
75
|
setSyncStatus(ctx, `pushing ${config.setupName}`, { activity: "pushing" });
|
|
62
76
|
const backend = input?.backend ?? (await factory(config));
|
|
63
77
|
throwIfAborted(options.signal);
|
|
64
|
-
const
|
|
78
|
+
const snapshotOptions = await resolvedSnapshotOptionsForContext(ctx, config, options.signal);
|
|
79
|
+
validateMutation();
|
|
80
|
+
const validateIgnorePolicy = ignorePolicyValidator({ ...snapshotOptions, validateMutation });
|
|
81
|
+
const localRaw = input?.local ?? (await createSnapshot(config.snapshotIdentity, snapshotOptions));
|
|
65
82
|
const local = portableSnapshot(localRaw, config.localFields);
|
|
66
83
|
throwIfAborted(options.signal);
|
|
67
84
|
let head = await backend.readHead(options.signal);
|
|
68
|
-
let remoteForUpload = await readRemoteSnapshotForUpload(backend, config, head, state, options
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
remoteForUpload = await readSnapshotForHead(backend, head, options.signal);
|
|
85
|
+
let remoteForUpload = await readRemoteSnapshotForUpload(backend, config, head, state, options);
|
|
86
|
+
validateMutation();
|
|
87
|
+
if (config.ignorePolicy && remoteForUpload && options.force && (options.auto || options.yes || !ctx.hasUI)) {
|
|
88
|
+
const comparison = inspectRemoteSelection(config.include, remoteForUpload, config.exclude, config);
|
|
89
|
+
if (comparison.kind === "different")
|
|
90
|
+
requireCompatibleRemoteSelection(config, remoteForUpload);
|
|
75
91
|
}
|
|
92
|
+
requireIgnoreBaselineReview(ctx, config, state, options);
|
|
76
93
|
if (remoteForUpload && !options.force) {
|
|
77
94
|
requireCompatibleRemoteSelection(config, remoteForUpload);
|
|
78
95
|
}
|
|
79
96
|
if (remoteChangedSinceState(head, state, config, (left, right) => backend.sameRevision(left, right)) &&
|
|
80
97
|
!options.force) {
|
|
81
|
-
const remoteForConflict = remoteForUpload
|
|
98
|
+
const remoteForConflict = remoteForUpload
|
|
99
|
+
? filterSnapshotForConfigPolicy(remoteForUpload, config, snapshotOptions)
|
|
100
|
+
: undefined;
|
|
82
101
|
if (!remoteForConflict || !snapshotHashesMatchState(remoteForConflict, state, config)) {
|
|
83
102
|
throw createSyncDecision({
|
|
84
103
|
kind: head ? "remote-or-policy-changed" : "remote-empty",
|
|
@@ -93,35 +112,39 @@ export async function push(ctx, options, input, factory = createSyncBackend) {
|
|
|
93
112
|
}
|
|
94
113
|
}
|
|
95
114
|
if (!remoteForUpload && head && (config.localFields !== undefined || !options.yes))
|
|
96
|
-
remoteForUpload = await readSnapshotForHead(backend, head, options.signal);
|
|
115
|
+
remoteForUpload = await readSnapshotForHead(backend, head, options.signal, config);
|
|
116
|
+
validateMutation();
|
|
97
117
|
if (!(await confirmFieldMigration(ctx, config, state, remoteForUpload, options.force, options.signal))) {
|
|
98
118
|
setSyncStatus(ctx, undefined);
|
|
99
119
|
return "cancelled";
|
|
100
120
|
}
|
|
101
121
|
const configIdentity = syncCheckConfigFingerprint(config);
|
|
102
|
-
let upload = await snapshotForUpload(backend, config, local, head, remoteForUpload,
|
|
122
|
+
let upload = await snapshotForUpload(backend, config, local, head, remoteForUpload, snapshotOptions);
|
|
103
123
|
if (!config.skipSecretScan) {
|
|
104
124
|
const secrets = scanSnapshot(local);
|
|
105
125
|
if (secrets.length > 0) {
|
|
106
126
|
throw new Error(`Refusing to push possible secrets:\n${secrets.map((s) => `- ${s}`).join("\n")}`);
|
|
107
127
|
}
|
|
108
128
|
}
|
|
109
|
-
if (!(await confirmPush(ctx, options, config, backend, local, upload, head, remoteForUpload))) {
|
|
129
|
+
if (!(await confirmPush(ctx, options, config, backend, local, upload, head, remoteForUpload, validateMutation))) {
|
|
110
130
|
return "cancelled";
|
|
111
131
|
}
|
|
112
132
|
if (options.force) {
|
|
113
133
|
const refreshedHead = await backend.readHead(options.signal);
|
|
134
|
+
validateMutation();
|
|
114
135
|
if (!sameRemoteHead(backend, head, refreshedHead)) {
|
|
136
|
+
if (config.ignorePolicy)
|
|
137
|
+
throw new Error("Remote changed during ignore-file push review; reopen the review.");
|
|
115
138
|
head = refreshedHead;
|
|
116
|
-
remoteForUpload = head ? await readSnapshotForHead(backend, head, options.signal) : undefined;
|
|
139
|
+
remoteForUpload = head ? await readSnapshotForHead(backend, head, options.signal, config) : undefined;
|
|
117
140
|
validateMutation();
|
|
118
141
|
if (!(await confirmFieldMigration(ctx, config, state, remoteForUpload, options.force, options.signal))) {
|
|
119
142
|
setSyncStatus(ctx, undefined);
|
|
120
143
|
return "cancelled";
|
|
121
144
|
}
|
|
122
145
|
validateMutation();
|
|
123
|
-
upload = await snapshotForUpload(backend, config, local, head, remoteForUpload,
|
|
124
|
-
if (!(await confirmPush(ctx, options, config, backend, local, upload, head, remoteForUpload, "Remote changed during review. Push the refreshed plan?"))) {
|
|
146
|
+
upload = await snapshotForUpload(backend, config, local, head, remoteForUpload, snapshotOptions);
|
|
147
|
+
if (!(await confirmPush(ctx, options, config, backend, local, upload, head, remoteForUpload, validateMutation, "Remote changed during review. Push the refreshed plan?"))) {
|
|
125
148
|
return "cancelled";
|
|
126
149
|
}
|
|
127
150
|
}
|
|
@@ -130,19 +153,33 @@ export async function push(ctx, options, input, factory = createSyncBackend) {
|
|
|
130
153
|
if (configIdentity !== syncCheckConfigFingerprint(await loadConfig(options.setup)))
|
|
131
154
|
throw new Error("Settings changed during push review.");
|
|
132
155
|
validateMutation();
|
|
133
|
-
if (
|
|
134
|
-
|
|
156
|
+
if (config.scope === "agent-directory" ||
|
|
157
|
+
!options.yes ||
|
|
158
|
+
config.localFields !== undefined ||
|
|
159
|
+
(await readMergeJournal(config))) {
|
|
160
|
+
const current = await createSnapshot(config.snapshotIdentity, snapshotOptions);
|
|
135
161
|
if (!sameHashes(fileHashMap(current), fileHashMap(localRaw)))
|
|
136
162
|
throw new Error("Local content changed during push review; retry from current bytes.");
|
|
137
163
|
}
|
|
138
164
|
const currentState = await readStateForConfig(config);
|
|
139
|
-
if (!options.yes && syncStateFingerprint(currentState) !== syncStateFingerprint(state))
|
|
165
|
+
if ((config.ignorePolicy || !options.yes) && syncStateFingerprint(currentState) !== syncStateFingerprint(state))
|
|
140
166
|
throw new Error("Baseline changed during push review.");
|
|
141
167
|
requireResolvedDirectionalProgress(currentState, options);
|
|
168
|
+
if (config.scope === "agent-directory" &&
|
|
169
|
+
snapshotOptions.sessionDir !== (await effectiveSessionRoot(ctx, config, options.signal)))
|
|
170
|
+
throw new Error("Pi session storage changed during push review.");
|
|
171
|
+
validateMutation();
|
|
172
|
+
if (configIdentity !== syncCheckConfigFingerprint(await loadConfig(options.setup)))
|
|
173
|
+
throw new Error("Settings changed during push review.");
|
|
142
174
|
validateMutation();
|
|
175
|
+
if (validateIgnorePolicy) {
|
|
176
|
+
validatePortableSnapshot(upload);
|
|
177
|
+
await validateIgnorePolicy();
|
|
178
|
+
}
|
|
143
179
|
const result = await backend.publishSnapshot(upload, expectedRemoteHead(head), {
|
|
144
180
|
signal: options.signal,
|
|
145
181
|
onCommit: options.onCommit,
|
|
182
|
+
...(validateIgnorePolicy ? { validateIgnorePolicy } : {}),
|
|
146
183
|
});
|
|
147
184
|
try {
|
|
148
185
|
await writeStateForConfig(config, {
|
|
@@ -151,8 +188,7 @@ export async function push(ctx, options, input, factory = createSyncBackend) {
|
|
|
151
188
|
lastAppliedSnapshot: result.head.snapshotId,
|
|
152
189
|
lastRemoteRevision: result.head.revision,
|
|
153
190
|
lastFileHashes: fileHashMap(local),
|
|
154
|
-
|
|
155
|
-
...(config.exclude === undefined ? {} : { exclude: [...config.exclude] }),
|
|
191
|
+
...acceptedSelection(config),
|
|
156
192
|
...(config.localFields !== undefined ? { localFields: config.localFields } : {}),
|
|
157
193
|
}, local, validateCommittedOwner);
|
|
158
194
|
}
|
|
@@ -175,21 +211,36 @@ export async function push(ctx, options, input, factory = createSyncBackend) {
|
|
|
175
211
|
}
|
|
176
212
|
return "applied";
|
|
177
213
|
}
|
|
178
|
-
export async function pull(ctx, options, factory = createSyncBackend) {
|
|
214
|
+
export async function pull(ctx, options, factory = createSyncBackend, initial) {
|
|
179
215
|
const validateMutation = captureMutationOwner(ctx, options.signal);
|
|
180
216
|
const validateCommittedOwner = captureMutationOwner(ctx);
|
|
181
217
|
const config = await loadConfig(options.setup);
|
|
182
|
-
|
|
218
|
+
validateMutation();
|
|
219
|
+
const configIdentity = syncCheckConfigFingerprint(config);
|
|
220
|
+
if (initial && initial.configIdentity !== configIdentity)
|
|
221
|
+
throw new Error("Settings changed before initial pull; run sync again.");
|
|
183
222
|
await requireNoMergeJournal(config, options);
|
|
184
223
|
setSyncStatus(ctx, `pulling ${config.setupName}`, { activity: "pulling" });
|
|
185
224
|
const backend = await factory(config);
|
|
186
225
|
const state = await readStateForConfig(config);
|
|
187
226
|
validateMutation();
|
|
227
|
+
if (initial && initial.stateIdentity !== syncStateFingerprint(state))
|
|
228
|
+
throw new Error("Baseline changed before initial pull; run sync again.");
|
|
188
229
|
requireResolvedDirectionalProgress(state, options);
|
|
189
|
-
const
|
|
190
|
-
|
|
191
|
-
const
|
|
192
|
-
|
|
230
|
+
const snapshotOptions = await resolvedSnapshotOptionsForContext(ctx, config, options.signal);
|
|
231
|
+
validateMutation();
|
|
232
|
+
const validateIgnorePolicy = ignorePolicyValidator({ ...snapshotOptions, validateMutation });
|
|
233
|
+
if (initial && initial.sessionDir !== snapshotOptions.sessionDir)
|
|
234
|
+
throw new Error("Pi session storage changed before initial pull; run sync again.");
|
|
235
|
+
const local = await createSnapshot(config.snapshotIdentity, snapshotOptions);
|
|
236
|
+
validateMutation();
|
|
237
|
+
if (initial && !sameHashes(initial.localHashes, fileHashMap(local)))
|
|
238
|
+
throw new Error("Local bytes changed before initial pull; run sync again.");
|
|
239
|
+
const { head, snapshot: remote } = await readRemoteSnapshot(backend, config, options.signal, snapshotOptions);
|
|
240
|
+
validateMutation();
|
|
241
|
+
if (initial && !matchesInitialHead(initial, backend, head))
|
|
242
|
+
throw new Error("Remote changed before initial pull; run sync again.");
|
|
243
|
+
requireIgnoreBaselineReview(ctx, config, state, options);
|
|
193
244
|
const localChanged = hasLocalChanges(local, state, config);
|
|
194
245
|
if (!remote) {
|
|
195
246
|
throw createSyncDecision({
|
|
@@ -210,10 +261,12 @@ export async function pull(ctx, options, factory = createSyncBackend) {
|
|
|
210
261
|
setSyncStatus(ctx, undefined);
|
|
211
262
|
return "cancelled";
|
|
212
263
|
}
|
|
213
|
-
const configIdentity = syncCheckConfigFingerprint(config);
|
|
214
264
|
const logical = portableSnapshot(remote, config.localFields);
|
|
215
265
|
const physical = overlayLocalFields(logical, local, config.localFields);
|
|
216
|
-
|
|
266
|
+
if (config.scope === "agent-directory")
|
|
267
|
+
requireStableMergeSessionRoot(local, physical, config.scope);
|
|
268
|
+
const reviewedProtectedPaths = protectedSessionPaths(ctx, snapshotOptions.sessionDir);
|
|
269
|
+
const remoteChanged = hasRemoteChanges(remote, state, config, reviewedProtectedPaths);
|
|
217
270
|
if (localChanged && remoteChanged && state.lastAppliedSnapshot && !options.force) {
|
|
218
271
|
throw createSyncDecision({
|
|
219
272
|
kind: "both-changed",
|
|
@@ -226,10 +279,10 @@ export async function pull(ctx, options, factory = createSyncBackend) {
|
|
|
226
279
|
directMessage: "Both local and remote changed since last sync. Run /sync diff, then choose /sync pull --force or /sync push --force.",
|
|
227
280
|
});
|
|
228
281
|
}
|
|
229
|
-
const caseCleanup = options.yes ? [] : await inspectCaseCleanup(physical,
|
|
282
|
+
const caseCleanup = options.yes ? [] : await inspectCaseCleanup(physical, snapshotOptions);
|
|
230
283
|
validateMutation();
|
|
231
284
|
if (!options.yes &&
|
|
232
|
-
!(await confirmTransferReview(ctx, snapshotIncludesSessions(remote) ? "Pull pi settings and sessions?" : "Pull pi settings?", formatPullSummary(config, backend.destination, local, physical,
|
|
285
|
+
!(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))) {
|
|
233
286
|
setSyncStatus(ctx, undefined);
|
|
234
287
|
ctx.ui.notify("Pull cancelled.", "info");
|
|
235
288
|
return "cancelled";
|
|
@@ -240,7 +293,7 @@ export async function pull(ctx, options, factory = createSyncBackend) {
|
|
|
240
293
|
validateMutation();
|
|
241
294
|
const currentHead = await backend.readHead(options.signal);
|
|
242
295
|
validateMutation();
|
|
243
|
-
const current = await createSnapshot(config.snapshotIdentity,
|
|
296
|
+
const current = await createSnapshot(config.snapshotIdentity, snapshotOptions);
|
|
244
297
|
validateMutation();
|
|
245
298
|
if (!sameHashes(fileHashMap(current), fileHashMap(local)) || !sameRemoteHead(backend, head, currentHead)) {
|
|
246
299
|
throw new Error("Local or remote content changed during recovery review; retry from a fresh diff.");
|
|
@@ -251,30 +304,47 @@ export async function pull(ctx, options, factory = createSyncBackend) {
|
|
|
251
304
|
if (configIdentity !== syncCheckConfigFingerprint(await loadConfig(options.setup)))
|
|
252
305
|
throw new Error("Settings changed during pull review.");
|
|
253
306
|
validateMutation();
|
|
254
|
-
const refreshed = await createSnapshot(config.snapshotIdentity,
|
|
307
|
+
const refreshed = await createSnapshot(config.snapshotIdentity, snapshotOptions);
|
|
255
308
|
if (!sameHashes(fileHashMap(local), fileHashMap(refreshed)))
|
|
256
309
|
throw new Error("Local bytes changed during pull review.");
|
|
257
310
|
if (!sameRemoteHead(backend, head, await backend.readHead(options.signal)))
|
|
258
311
|
throw new Error("Remote changed during pull review.");
|
|
259
312
|
validateMutation();
|
|
260
|
-
const backup = await backupLocal(config.snapshotIdentity,
|
|
261
|
-
const applySessionDir = await sessionDirForApply(ctx, physical);
|
|
313
|
+
const backup = await backupLocal(config.snapshotIdentity, snapshotOptions, options.signal);
|
|
314
|
+
const applySessionDir = await sessionDirForApply(ctx, physical, snapshotOptions);
|
|
262
315
|
if (recoveringMerge)
|
|
263
316
|
await requireFreshRecovery();
|
|
264
317
|
const currentState = await readStateForConfig(config);
|
|
265
|
-
|
|
318
|
+
validateMutation();
|
|
319
|
+
if ((config.ignorePolicy || initial || !options.yes) &&
|
|
320
|
+
syncStateFingerprint(currentState) !== syncStateFingerprint(state))
|
|
266
321
|
throw new Error("Baseline changed during pull review.");
|
|
267
322
|
requireResolvedDirectionalProgress(currentState, options);
|
|
268
323
|
if (!options.yes)
|
|
269
|
-
await revalidateCaseCleanup(caseCleanup, physical,
|
|
324
|
+
await revalidateCaseCleanup(caseCleanup, physical, snapshotOptions);
|
|
325
|
+
if (initial && !matchesInitialHead(initial, backend, await backend.readHead(options.signal)))
|
|
326
|
+
throw new Error("Remote changed during initial pull; run sync again.");
|
|
327
|
+
validateMutation();
|
|
328
|
+
if (config.scope && applySessionDir !== (await effectiveSessionRoot(ctx, config, options.signal)))
|
|
329
|
+
throw new Error("Pi session storage changed during pull review.");
|
|
330
|
+
validateMutation();
|
|
331
|
+
if (configIdentity !== syncCheckConfigFingerprint(await loadConfig(options.setup)))
|
|
332
|
+
throw new Error("Settings changed during pull review.");
|
|
270
333
|
validateMutation();
|
|
334
|
+
if (validateIgnorePolicy) {
|
|
335
|
+
if (!sameRemoteHead(backend, head, await backend.readHead(options.signal)))
|
|
336
|
+
throw new Error("Remote changed during pull review.");
|
|
337
|
+
await validateIgnorePolicy();
|
|
338
|
+
}
|
|
339
|
+
const protectedPaths = protectedSessionPaths(ctx, snapshotOptions.sessionDir);
|
|
340
|
+
if (validateIgnorePolicy && !samePaths(protectedPaths, reviewedProtectedPaths))
|
|
341
|
+
throw new Error("Live session protection changed during pull review; reopen the review.");
|
|
271
342
|
options.onCommit?.();
|
|
272
|
-
const lastFileHashes = await applySnapshot(physical,
|
|
273
|
-
|
|
274
|
-
...(config.exclude === undefined ? {} : { exclude: [...config.exclude] }),
|
|
343
|
+
const lastFileHashes = await applySnapshot(physical, protectedPaths, {
|
|
344
|
+
...snapshotOptions,
|
|
275
345
|
sessionDir: applySessionDir,
|
|
276
|
-
signal: options.signal,
|
|
277
346
|
validateMutation,
|
|
347
|
+
...(config.scope === "agent-directory" ? { reviewedLocal: local } : {}),
|
|
278
348
|
expectedFileHashes: config.localFields?.length
|
|
279
349
|
? { "settings.json": fileHashMap(local)["settings.json"] ?? null }
|
|
280
350
|
: undefined,
|
|
@@ -289,8 +359,7 @@ export async function pull(ctx, options, factory = createSyncBackend) {
|
|
|
289
359
|
lastFileHashes: config.localFields?.length
|
|
290
360
|
? { ...lastFileHashes, "settings.json": fileHashMap(logical)["settings.json"] }
|
|
291
361
|
: lastFileHashes,
|
|
292
|
-
|
|
293
|
-
...(config.exclude === undefined ? {} : { exclude: [...config.exclude] }),
|
|
362
|
+
...acceptedSelection(config),
|
|
294
363
|
...(config.localFields !== undefined ? { localFields: config.localFields } : {}),
|
|
295
364
|
}, logical, validateCommittedOwner);
|
|
296
365
|
await retireMergeJournal(config);
|
|
@@ -321,37 +390,68 @@ export async function syncBoth(ctx, options, factory = createSyncBackend) {
|
|
|
321
390
|
validateMutation();
|
|
322
391
|
const pending = await readMergeJournal(config);
|
|
323
392
|
validateMutation();
|
|
324
|
-
if (!config
|
|
393
|
+
if (!syncSelectionHasContent(config) || !state.lastAppliedSnapshot || pending)
|
|
325
394
|
throw new Error("Retry requires an accepted baseline and no pending journal. Use Sync now or reviewed recovery.");
|
|
326
395
|
return mergeSync(ctx, options, factory);
|
|
327
396
|
}
|
|
328
|
-
if (config
|
|
397
|
+
if (syncSelectionHasContent(config) && options.auto && startupTransferEnabled(config))
|
|
329
398
|
return mergeSync(ctx, options, factory);
|
|
330
|
-
if (!options.auto && config
|
|
399
|
+
if (!options.auto && syncSelectionHasContent(config) && (await readMergeJournal(config)))
|
|
331
400
|
return mergeSync(ctx, options, factory);
|
|
332
401
|
const state = await readStateForConfig(config);
|
|
333
402
|
throwIfAborted(options.signal);
|
|
334
|
-
if (!options.auto && config
|
|
403
|
+
if (!options.auto && syncSelectionHasContent(config) && state.lastAppliedSnapshot)
|
|
335
404
|
return mergeSync(ctx, options, factory);
|
|
336
|
-
|
|
337
|
-
const local = await createSnapshot(config.snapshotIdentity, snapshotOptionsForContext(ctx, config));
|
|
338
|
-
throwIfAborted(options.signal);
|
|
339
|
-
if (config.include.length === 0) {
|
|
405
|
+
if (!syncSelectionHasContent(config)) {
|
|
340
406
|
if (!options.silent) {
|
|
341
407
|
ctx.ui.notify(`Sync setup “${config.setupName}” includes no files. Choose included content in /sync Settings before syncing.`, "warning");
|
|
342
408
|
}
|
|
343
409
|
return;
|
|
344
410
|
}
|
|
345
|
-
const
|
|
411
|
+
const backend = await factory(config);
|
|
412
|
+
const snapshotOptions = await resolvedSnapshotOptionsForContext(ctx, config, options.signal);
|
|
413
|
+
validateMutation();
|
|
414
|
+
const local = await createSnapshot(config.snapshotIdentity, snapshotOptions);
|
|
415
|
+
throwIfAborted(options.signal);
|
|
416
|
+
const { head, snapshot: remote } = await readRemoteSnapshot(backend, config, options.signal, snapshotOptions);
|
|
346
417
|
throwIfAborted(options.signal);
|
|
347
418
|
const localChanged = hasLocalChanges(local, state, config);
|
|
348
|
-
const remoteChanged = remote
|
|
419
|
+
const remoteChanged = remote
|
|
420
|
+
? hasRemoteChanges(remote, state, config, protectedSessionPaths(ctx, snapshotOptions.sessionDir))
|
|
421
|
+
: false;
|
|
349
422
|
if (options.auto)
|
|
350
423
|
throw new Error("Automatic transfer is not authorized by the current settings.");
|
|
351
424
|
// Established baselines return through mergeSync above; only initial-source selection remains.
|
|
352
425
|
const portableLocal = portableSnapshot(local, config.localFields);
|
|
426
|
+
const configIdentity = syncCheckConfigFingerprint(config);
|
|
427
|
+
const pullInitialSource = () => {
|
|
428
|
+
validateMutation();
|
|
429
|
+
return pull(ctx, options, factory, {
|
|
430
|
+
configIdentity,
|
|
431
|
+
stateIdentity: syncStateFingerprint(state),
|
|
432
|
+
sessionDir: snapshotOptions.sessionDir,
|
|
433
|
+
localHashes: fileHashMap(local),
|
|
434
|
+
head,
|
|
435
|
+
});
|
|
436
|
+
};
|
|
353
437
|
const acceptMatchingState = async (snapshot) => {
|
|
354
438
|
requireCompatibleRemoteSelection(config, snapshot);
|
|
439
|
+
if (config.scope === "agent-directory") {
|
|
440
|
+
const current = await createSnapshot(config.snapshotIdentity, snapshotOptions);
|
|
441
|
+
const currentState = await readStateForConfig(config);
|
|
442
|
+
const currentHead = await backend.readHead(options.signal);
|
|
443
|
+
if (!sameHashes(fileHashMap(current), fileHashMap(local)) ||
|
|
444
|
+
syncStateFingerprint(currentState) !== syncStateFingerprint(state) ||
|
|
445
|
+
!sameRemoteHead(backend, head, currentHead))
|
|
446
|
+
throw new Error("Initial source changed before baseline acceptance; run sync again.");
|
|
447
|
+
if (snapshotOptions.sessionDir !== (await effectiveSessionRoot(ctx, config, options.signal)) ||
|
|
448
|
+
configIdentity !== syncCheckConfigFingerprint(await loadConfig(options.setup)))
|
|
449
|
+
throw new Error("Settings or Pi session storage changed before baseline acceptance; run sync again.");
|
|
450
|
+
validateMutation();
|
|
451
|
+
const validateIgnorePolicy = ignorePolicyValidator({ ...snapshotOptions, validateMutation });
|
|
452
|
+
if (validateIgnorePolicy)
|
|
453
|
+
await validateIgnorePolicy();
|
|
454
|
+
}
|
|
355
455
|
const accepted = portableSnapshot(snapshot, config.localFields);
|
|
356
456
|
await writeStateForConfig(config, {
|
|
357
457
|
version: VERSION,
|
|
@@ -359,8 +459,7 @@ export async function syncBoth(ctx, options, factory = createSyncBackend) {
|
|
|
359
459
|
lastAppliedSnapshot: snapshot.id,
|
|
360
460
|
lastRemoteRevision: head?.revision,
|
|
361
461
|
lastFileHashes: fileHashMap(accepted),
|
|
362
|
-
|
|
363
|
-
...(config.exclude === undefined ? {} : { exclude: [...config.exclude] }),
|
|
462
|
+
...acceptedSelection(config),
|
|
364
463
|
...(config.localFields !== undefined ? { localFields: config.localFields } : {}),
|
|
365
464
|
}, accepted, validateMutation);
|
|
366
465
|
await pruneMergeBaselines(config, await readStateForConfig(config), validateMutation);
|
|
@@ -394,7 +493,7 @@ export async function syncBoth(ctx, options, factory = createSyncBackend) {
|
|
|
394
493
|
directMessage: "Remote settings match, but local and remote Pi sessions differ. Run /sync diff, then manually choose /sync pull or /sync push.",
|
|
395
494
|
});
|
|
396
495
|
}
|
|
397
|
-
await
|
|
496
|
+
await pullInitialSource();
|
|
398
497
|
return;
|
|
399
498
|
}
|
|
400
499
|
await acceptMatchingState(remote);
|
|
@@ -413,11 +512,11 @@ export async function syncBoth(ctx, options, factory = createSyncBackend) {
|
|
|
413
512
|
return;
|
|
414
513
|
}
|
|
415
514
|
if (remoteChanged) {
|
|
416
|
-
await
|
|
515
|
+
await pullInitialSource();
|
|
417
516
|
return;
|
|
418
517
|
}
|
|
419
518
|
if (localChanged || !remote) {
|
|
420
|
-
await push(ctx, options,
|
|
519
|
+
await push(ctx, options, { config, state, local, backend }, factory);
|
|
421
520
|
return;
|
|
422
521
|
}
|
|
423
522
|
if (shouldRefreshSyncedState(remote, head, state, config, (left, right) => backend.sameRevision(left, right))) {
|
|
@@ -433,7 +532,9 @@ export async function rollback(ctx, options, factory = createSyncBackend, expect
|
|
|
433
532
|
if (!target)
|
|
434
533
|
throw new Error("Usage: /sync rollback <snapshot-id> [--yes]");
|
|
435
534
|
const config = await loadConfig(options.setup);
|
|
436
|
-
|
|
535
|
+
validateMutation();
|
|
536
|
+
if (config.ignorePolicy && (options.yes || options.auto || !ctx.hasUI))
|
|
537
|
+
throw new Error("Ignore-file rollback requires a separate foreground review; --yes cannot confirm it.");
|
|
437
538
|
const backend = await factory(config);
|
|
438
539
|
if (expectedSelection &&
|
|
439
540
|
(backend.identity !== expectedSelection.backendIdentity || config.setupName !== expectedSelection.setup)) {
|
|
@@ -445,23 +546,47 @@ export async function rollback(ctx, options, factory = createSyncBackend, expect
|
|
|
445
546
|
validateMutation();
|
|
446
547
|
requireResolvedDirectionalProgress(state, options, false);
|
|
447
548
|
const decoded = await backend.readSnapshot(target, options.signal);
|
|
549
|
+
validateMutation();
|
|
448
550
|
validatePortableSnapshot(decoded);
|
|
551
|
+
if (!config.ignorePolicy)
|
|
552
|
+
requireLegacyTransferPolicy(decoded);
|
|
553
|
+
if (config.ignorePolicy && expectedSelection?.snapshotId && decoded.id !== expectedSelection.snapshotId)
|
|
554
|
+
throw new Error("Historical snapshot changed while history was open; reopen history and retry.");
|
|
449
555
|
if (!sameLocalFields(decoded.localFields, config.localFields))
|
|
450
556
|
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
|
|
557
|
+
const snapshotOptions = await resolvedSnapshotOptionsForContext(ctx, config, options.signal);
|
|
558
|
+
validateMutation();
|
|
559
|
+
const validateIgnorePolicy = ignorePolicyValidator({ ...snapshotOptions, validateMutation });
|
|
560
|
+
const selected = filterSnapshotForConfigPolicy(config.ignorePolicy || syncSessionsSelected(config) ? decoded : snapshotWithoutSessions(decoded), config, snapshotOptions);
|
|
452
561
|
const remote = portableSnapshot(regenerateSnapshotIdentity(selected), config.localFields);
|
|
453
|
-
const local = await createSnapshot(config.snapshotIdentity,
|
|
562
|
+
const local = await createSnapshot(config.snapshotIdentity, snapshotOptions);
|
|
454
563
|
const expectedHead = await backend.readHead(options.signal);
|
|
455
|
-
const currentRemote = expectedHead
|
|
564
|
+
const currentRemote = expectedHead
|
|
565
|
+
? await readSnapshotForHead(backend, expectedHead, options.signal, config)
|
|
566
|
+
: undefined;
|
|
456
567
|
validateMutation();
|
|
568
|
+
if (config.ignorePolicy && currentRemote)
|
|
569
|
+
requireCompatibleRemoteSelection(config, currentRemote);
|
|
457
570
|
if (!(await confirmFieldMigration(ctx, config, state, currentRemote, true, options.signal)))
|
|
458
571
|
return;
|
|
459
572
|
validateMutation();
|
|
460
573
|
const physical = overlayLocalFields(remote, local, config.localFields);
|
|
461
|
-
|
|
574
|
+
if (config.scope === "agent-directory")
|
|
575
|
+
requireStableMergeSessionRoot(local, physical, config.scope);
|
|
576
|
+
const upload = config.ignorePolicy
|
|
577
|
+
? remote
|
|
578
|
+
: await snapshotForUpload(backend, config, remote, expectedHead, currentRemote, snapshotOptions);
|
|
579
|
+
if (config.scope === "agent-directory")
|
|
580
|
+
validatePortableSnapshot(upload);
|
|
581
|
+
const caseCleanup = options.yes ? [] : await inspectCaseCleanup(physical, snapshotOptions);
|
|
462
582
|
validateMutation();
|
|
583
|
+
const reviewedProtectedPaths = protectedSessionPaths(ctx, snapshotOptions.sessionDir);
|
|
463
584
|
if (!options.yes) {
|
|
464
|
-
const
|
|
585
|
+
const title = snapshotIncludesSessions(remote) ? "Rollback pi settings and sessions?" : "Rollback pi settings?";
|
|
586
|
+
const summary = formatRollbackSummary(config, backend.destination, local, physical, target, reviewedProtectedPaths, caseCleanup.map((item) => item.path));
|
|
587
|
+
const confirmed = config.ignorePolicy
|
|
588
|
+
? await confirmTransferReview(ctx, title, `Historical files are projected through this device's current ignore rules. Historical rules are not adopted. Retained history is not erased.\n\n${summary}\n\nRemote publication:\n${formatPublicationPreview(currentRemote, upload)}`, options.signal, validateMutation)
|
|
589
|
+
: await ctx.ui.confirm(title, summary, { signal: options.signal });
|
|
465
590
|
validateMutation();
|
|
466
591
|
if (!confirmed) {
|
|
467
592
|
ctx.ui.notify("Rollback cancelled.", "info");
|
|
@@ -469,7 +594,7 @@ export async function rollback(ctx, options, factory = createSyncBackend, expect
|
|
|
469
594
|
}
|
|
470
595
|
}
|
|
471
596
|
validateMutation();
|
|
472
|
-
const currentLocal = await createSnapshot(config.snapshotIdentity,
|
|
597
|
+
const currentLocal = await createSnapshot(config.snapshotIdentity, snapshotOptions);
|
|
473
598
|
if (!sameHashes(fileHashMap(currentLocal), fileHashMap(local)))
|
|
474
599
|
throw new Error("Local bytes changed during rollback review.");
|
|
475
600
|
if (syncCheckConfigFingerprint(config) !== syncCheckConfigFingerprint(await loadConfig(options.setup)))
|
|
@@ -478,22 +603,34 @@ export async function rollback(ctx, options, factory = createSyncBackend, expect
|
|
|
478
603
|
throw new Error("Remote changed during rollback review.");
|
|
479
604
|
validateMutation();
|
|
480
605
|
throwIfAborted(options.signal);
|
|
481
|
-
const backup = await backupLocal(config.snapshotIdentity,
|
|
482
|
-
const applySessionDir = await sessionDirForApply(ctx, remote);
|
|
606
|
+
const backup = await backupLocal(config.snapshotIdentity, snapshotOptions, options.signal);
|
|
607
|
+
const applySessionDir = await sessionDirForApply(ctx, config.scope ? physical : remote, snapshotOptions);
|
|
483
608
|
const currentState = await readStateForConfig(config);
|
|
484
609
|
if (!options.yes && syncStateFingerprint(currentState) !== syncStateFingerprint(state))
|
|
485
610
|
throw new Error("Baseline changed during rollback review.");
|
|
486
611
|
requireResolvedDirectionalProgress(currentState, options, false);
|
|
487
612
|
if (!options.yes)
|
|
488
|
-
await revalidateCaseCleanup(caseCleanup, physical,
|
|
613
|
+
await revalidateCaseCleanup(caseCleanup, physical, snapshotOptions);
|
|
614
|
+
if (config.scope && applySessionDir !== (await effectiveSessionRoot(ctx, config, options.signal)))
|
|
615
|
+
throw new Error("Pi session storage changed during rollback review.");
|
|
489
616
|
validateMutation();
|
|
617
|
+
if (syncCheckConfigFingerprint(config) !== syncCheckConfigFingerprint(await loadConfig(options.setup)))
|
|
618
|
+
throw new Error("Settings changed during rollback review.");
|
|
619
|
+
validateMutation();
|
|
620
|
+
if (validateIgnorePolicy) {
|
|
621
|
+
if (!sameRemoteHead(backend, expectedHead, await backend.readHead(options.signal)))
|
|
622
|
+
throw new Error("Remote changed during rollback review.");
|
|
623
|
+
await validateIgnorePolicy();
|
|
624
|
+
}
|
|
625
|
+
const protectedPaths = protectedSessionPaths(ctx, snapshotOptions.sessionDir);
|
|
626
|
+
if (validateIgnorePolicy && !samePaths(protectedPaths, reviewedProtectedPaths))
|
|
627
|
+
throw new Error("Live session protection changed during rollback review; reopen the review.");
|
|
490
628
|
options.onCommit?.();
|
|
491
|
-
const lastFileHashes = await applySnapshot(physical,
|
|
492
|
-
|
|
493
|
-
...(config.exclude === undefined ? {} : { exclude: [...config.exclude] }),
|
|
629
|
+
const lastFileHashes = await applySnapshot(physical, protectedPaths, {
|
|
630
|
+
...snapshotOptions,
|
|
494
631
|
sessionDir: applySessionDir,
|
|
495
|
-
signal: options.signal,
|
|
496
632
|
validateMutation,
|
|
633
|
+
...(config.scope === "agent-directory" ? { reviewedLocal: local } : {}),
|
|
497
634
|
expectedFileHashes: config.localFields?.length
|
|
498
635
|
? { "settings.json": fileHashMap(local)["settings.json"] ?? null }
|
|
499
636
|
: undefined,
|
|
@@ -502,11 +639,15 @@ export async function rollback(ctx, options, factory = createSyncBackend, expect
|
|
|
502
639
|
validateCommittedOwner();
|
|
503
640
|
let result;
|
|
504
641
|
try {
|
|
505
|
-
const
|
|
506
|
-
const
|
|
507
|
-
|
|
642
|
+
const signal = AbortSignal.timeout(POST_LOCAL_COMMIT_TIMEOUT_MS);
|
|
643
|
+
const validateIgnorePolicy = ignorePolicyValidator({
|
|
644
|
+
...snapshotOptions,
|
|
645
|
+
signal,
|
|
646
|
+
validateMutation: validateCommittedOwner,
|
|
647
|
+
});
|
|
508
648
|
result = await backend.publishSnapshot(upload, expectedRemoteHead(expectedHead), {
|
|
509
|
-
signal
|
|
649
|
+
signal,
|
|
650
|
+
...(validateIgnorePolicy ? { validateIgnorePolicy } : {}),
|
|
510
651
|
});
|
|
511
652
|
}
|
|
512
653
|
catch (error) {
|
|
@@ -521,8 +662,7 @@ export async function rollback(ctx, options, factory = createSyncBackend, expect
|
|
|
521
662
|
lastFileHashes: config.localFields?.length
|
|
522
663
|
? { ...lastFileHashes, "settings.json": fileHashMap(remote)["settings.json"] }
|
|
523
664
|
: lastFileHashes,
|
|
524
|
-
|
|
525
|
-
...(config.exclude === undefined ? {} : { exclude: [...config.exclude] }),
|
|
665
|
+
...acceptedSelection(config),
|
|
526
666
|
...(config.localFields !== undefined ? { localFields: config.localFields } : {}),
|
|
527
667
|
}, remote, validateCommittedOwner);
|
|
528
668
|
}
|
|
@@ -554,26 +694,29 @@ async function maybeReload(ctx, signal, validateMutation = captureMutationOwner(
|
|
|
554
694
|
await ctx.reload();
|
|
555
695
|
}
|
|
556
696
|
}
|
|
557
|
-
async function
|
|
558
|
-
if (
|
|
559
|
-
(
|
|
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) {
|
|
697
|
+
async function snapshotForUpload(backend, config, local, head, remote, options) {
|
|
698
|
+
if (config.ignorePolicy)
|
|
699
|
+
return filterSnapshotForConfigPolicy(local, config, { ...options, regenerateId: true });
|
|
567
700
|
if (!head)
|
|
568
701
|
return local;
|
|
569
|
-
const snapshot = remote ?? (await readSnapshotForHead(backend, head, signal));
|
|
570
|
-
|
|
702
|
+
const snapshot = remote ?? (await readSnapshotForHead(backend, head, options.signal, config));
|
|
703
|
+
const remoteScoped = snapshot.selection?.version === 3;
|
|
704
|
+
if (remoteScoped !== (config.scope === "agent-directory")) {
|
|
705
|
+
throw new Error("Converting an existing remote between included paths and agent-directory scope is not supported. Use a separate storage location.");
|
|
706
|
+
}
|
|
707
|
+
return mergeRemotePreservedFiles(local, snapshot, config, options);
|
|
571
708
|
}
|
|
572
|
-
async function confirmPush(ctx, options, config, backend, local, upload, head, remote, title = snapshotIncludesSessions(upload) ? "Push pi settings and sessions?" : "Push pi settings?") {
|
|
573
|
-
|
|
709
|
+
async function confirmPush(ctx, options, config, backend, local, upload, head, remote, validateMutation, title = snapshotIncludesSessions(upload) ? "Push pi settings and sessions?" : "Push pi settings?") {
|
|
710
|
+
validateMutation();
|
|
574
711
|
if (options.yes)
|
|
575
712
|
return true;
|
|
576
|
-
const
|
|
713
|
+
const summary = formatPushSummary(config, backend.destination, upload, head, countPreservedRemoteFiles(local, upload), remote);
|
|
714
|
+
const comparison = config.ignorePolicy && remote
|
|
715
|
+
? inspectRemoteSelection(config.include, remote, config.exclude, config).ignorePolicyComparison
|
|
716
|
+
: undefined;
|
|
717
|
+
const confirmed = await confirmTransferReview(ctx, title, comparison && comparison !== "match"
|
|
718
|
+
? `${formatIgnorePolicyComparison(comparison)}\nOnly files selected by this device's rules are published. Remote rules are not adopted.\n\n${summary}`
|
|
719
|
+
: summary, options.signal, validateMutation);
|
|
577
720
|
throwIfAborted(options.signal);
|
|
578
721
|
if (confirmed)
|
|
579
722
|
return true;
|
|
@@ -581,9 +724,10 @@ async function confirmPush(ctx, options, config, backend, local, upload, head, r
|
|
|
581
724
|
ctx.ui.notify("Push cancelled.", "info");
|
|
582
725
|
return false;
|
|
583
726
|
}
|
|
584
|
-
function
|
|
585
|
-
|
|
586
|
-
|
|
587
|
-
|
|
727
|
+
function matchesInitialHead(initial, backend, current) {
|
|
728
|
+
return sameRemoteHead(backend, initial.head, current);
|
|
729
|
+
}
|
|
730
|
+
function samePaths(left, right) {
|
|
731
|
+
return left.size === right.size && [...left].every((item) => right.has(item));
|
|
588
732
|
}
|
|
589
733
|
export { backupLocal } from "./sync-local.js";
|