@narumitw/pi-sync 0.64.0 → 0.66.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 +20 -0
- package/README.md +80 -11
- package/dist/chunks/{chunk-TJ7YNITN.ts → chunk-5YOLQKNZ.ts} +39 -3
- package/dist/chunks/chunk-5YOLQKNZ.ts.map +7 -0
- package/dist/chunks/{chunk-NDEZ52SS.ts → chunk-ASU3GZPW.ts} +11 -93
- package/dist/chunks/chunk-ASU3GZPW.ts.map +7 -0
- 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-BESRBKVO.ts} +3 -3
- package/dist/chunks/{chunk-TZMVSTAM.ts → chunk-BYVHABNR.ts} +4 -4
- package/dist/chunks/chunk-ECYCNZOE.ts +12 -0
- package/dist/chunks/chunk-ECYCNZOE.ts.map +7 -0
- package/dist/chunks/{chunk-RW44KNFD.ts → chunk-JQ5BAWW2.ts} +182 -12
- package/dist/chunks/chunk-JQ5BAWW2.ts.map +7 -0
- package/dist/chunks/{chunk-MANNB4WZ.ts → chunk-KNEUL4XN.ts} +1 -7
- package/dist/chunks/chunk-KNEUL4XN.ts.map +7 -0
- 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/chunk-Z5JMOXJM.ts +106 -0
- package/dist/chunks/chunk-Z5JMOXJM.ts.map +7 -0
- package/dist/chunks/{command-handler-N45GY7AV.ts → command-handler-44EFGPWH.ts} +54 -13
- package/dist/chunks/command-handler-44EFGPWH.ts.map +7 -0
- package/dist/chunks/{conflict-review-5E4M44BA.ts → conflict-review-J7L7S4AR.ts} +2 -2
- package/dist/chunks/{file-selection-DDQN4LVD.ts → file-selection-XWACUTAH.ts} +8 -8
- package/dist/chunks/ignore-file-ui-OHFISFVC.ts +21 -0
- package/dist/chunks/{manager-result-dispatcher-FKW55L7I.ts → manager-result-dispatcher-WXE2BAQO.ts} +4 -4
- package/dist/chunks/{manager-ui-EV23DPZQ.ts → manager-ui-CVZLMXJ5.ts} +90 -76
- package/dist/chunks/manager-ui-CVZLMXJ5.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-E4XZCQ7A.ts} +3 -3
- package/dist/chunks/{setup-switch-HY7K3442.ts → setup-switch-644T6KHG.ts} +3 -3
- package/dist/chunks/setup-switch-644T6KHG.ts.map +7 -0
- package/dist/chunks/{setup-wizard-LEKDQ3HK.ts → setup-wizard-IEW56AZE.ts} +10 -8
- package/dist/chunks/{setup-wizard-LEKDQ3HK.ts.map → setup-wizard-IEW56AZE.ts.map} +1 -1
- 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/consent.js +14 -2
- 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 +15 -4
- package/dist/daemon/dist/engine/backends/git/git-storage.js +15 -3
- 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/ignore/adoption-journal.js +143 -0
- package/dist/daemon/dist/engine/ignore/snapshot-ignore-rules.js +39 -0
- package/dist/daemon/dist/engine/snapshot/snapshot-apply.js +2 -2
- package/dist/daemon/dist/engine/snapshot/snapshot-codec.js +6 -6
- package/dist/daemon/dist/engine/snapshot/snapshot-types.js +5 -1
- package/dist/daemon/dist/engine/snapshot/snapshot.js +5 -2
- package/dist/daemon/dist/engine/sync/conflict-review.js +2 -0
- package/dist/daemon/dist/engine/sync/continuous-watch.js +3 -2
- package/dist/daemon/dist/engine/sync/directional-action.js +119 -0
- package/dist/daemon/dist/engine/sync/ignore-adoption-recovery.js +50 -0
- package/dist/daemon/dist/engine/sync/ignore-adoption.js +401 -0
- package/dist/daemon/dist/engine/sync/ignore-publication.js +23 -0
- package/dist/daemon/dist/engine/sync/initialization.js +12 -0
- package/dist/daemon/dist/engine/sync/local-fields.js +6 -3
- package/dist/daemon/dist/engine/sync/merge-journal.js +2 -2
- package/dist/daemon/dist/engine/sync/merged-sync.js +14 -3
- 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 +57 -3
- package/dist/daemon/dist/engine/sync/sync-mutations.js +375 -346
- package/dist/daemon/dist/engine/sync/sync-policy.js +5 -5
- package/dist/daemon/dist/engine/sync/sync-queries.js +3 -2
- package/dist/daemon/dist/engine/sync/sync-rollback.js +204 -0
- package/dist/daemon/dist/engine/sync/transfer-freshness.js +11 -5
- package/dist/daemon/dist/engine/ui/ignore-rule-preview.js +18 -0
- package/dist/daemon/dist/engine-service.js +69 -1
- 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/docs/tui-contract.md +3 -3
- package/package.json +2 -2
- package/skills/configure-pi-sync/references/configuration.md +10 -8
- 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/ignore-file-ui.ts +13 -5
- package/src/ui/manager-state.ts +17 -10
- package/src/ui/manager-ui.ts +30 -25
- package/src/ui/merge-review.ts +8 -9
- package/src/ui/remote-selection-ui.ts +43 -2
- package/src/ui/settings-ui.ts +30 -30
- package/src/ui/sync-confirmation.ts +7 -7
- package/dist/chunks/chunk-CKUGCBHP.ts.map +0 -7
- package/dist/chunks/chunk-MANNB4WZ.ts.map +0 -7
- package/dist/chunks/chunk-NDEZ52SS.ts.map +0 -7
- package/dist/chunks/chunk-RW44KNFD.ts.map +0 -7
- package/dist/chunks/chunk-TJ7YNITN.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-BESRBKVO.ts.map} +0 -0
- /package/dist/chunks/{chunk-TZMVSTAM.ts.map → chunk-BYVHABNR.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/{file-selection-DDQN4LVD.ts.map → file-selection-XWACUTAH.ts.map} +0 -0
- /package/dist/chunks/{manager-result-dispatcher-FKW55L7I.ts.map → ignore-file-ui-OHFISFVC.ts.map} +0 -0
- /package/dist/chunks/{merge-review-6YSLSMGS.ts.map → manager-result-dispatcher-WXE2BAQO.ts.map} +0 -0
- /package/dist/chunks/{remote-selection-ui-N4NMWFT2.ts.map → merge-review-AAC674C6.ts.map} +0 -0
- /package/dist/chunks/{setup-switch-HY7K3442.ts.map → remote-selection-ui-E4XZCQ7A.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,58 +1,52 @@
|
|
|
1
1
|
import { createSyncBackend } from "../backends/backend-factory.js";
|
|
2
|
-
import { expectedRemoteHead, } from "../backends/sync-backend.js";
|
|
3
|
-
import {
|
|
2
|
+
import { expectedRemoteHead, SyncBackendConflictError, } from "../backends/sync-backend.js";
|
|
3
|
+
import { loadAdoptionJournal, requireNoPendingAdoption } from "../ignore/adoption-journal.js";
|
|
4
|
+
import { loadConfig, loadConfigForCheck, syncCheckConfigFingerprint } from "../settings/config.js";
|
|
4
5
|
import { effectiveSessionRoot, requireStableMergeSessionRoot, resolvedSnapshotOptionsForContext, sessionDirForApply, } from "../snapshot/session-paths.js";
|
|
5
|
-
import { createSnapshot, filterSnapshotForConfigPolicy,
|
|
6
|
+
import { createSnapshot, filterSnapshotForConfigPolicy, scanSnapshot, snapshotIncludesSessions, } from "../snapshot/snapshot.js";
|
|
6
7
|
import { applySnapshot } from "../snapshot/snapshot-apply.js";
|
|
8
|
+
import { pendingTransactionEntries } from "../snapshot/snapshot-transaction-directory.js";
|
|
7
9
|
import { ignorePolicyValidator } from "../snapshot/snapshot-transaction-policy.js";
|
|
8
10
|
import { pruneMergeBaselines } from "../state/merge-baseline-store.js";
|
|
9
11
|
import { readStateForConfig, syncStateFingerprint, writeStateForConfig } from "../state/sync-state-store.js";
|
|
10
|
-
import {
|
|
12
|
+
import { formatRuleChanges } from "../ui/ignore-rule-preview.js";
|
|
13
|
+
import { countPreservedRemoteFiles, formatPullSummary, formatPushSummary, formatTransferReview, } from "../ui/sync-format.js";
|
|
11
14
|
import { setSyncStatus } from "../ui/sync-status.js";
|
|
12
15
|
import { confirmTransferReview } from "../ui/transfer-review.js";
|
|
13
16
|
import { startupTransferEnabled } from "./agent-policy.js";
|
|
14
17
|
import { inspectCaseCleanup, revalidateCaseCleanup } from "./apply-review.js";
|
|
18
|
+
import { checkpointDirectionalAction, foregroundDirectionalAction, keepProtectedRemoteSessions, localRestoreGuidance, notifyDirectionalSuccess, remoteRestoreGuidance, requireDirectionalPullBoundary, requireDirectionalSelection, validateDirectionalSettings, } from "./directional-action.js";
|
|
15
19
|
import { confirmFieldMigration } from "./field-migration.js";
|
|
20
|
+
import { adoptionSettingsFingerprint, adoptRemoteRules, canReviewAdoption } from "./ignore-adoption.js";
|
|
21
|
+
import { ignorePublicationNotice, withPublishedIgnoreRules } from "./ignore-publication.js";
|
|
16
22
|
import { overlayLocalFields, portableSnapshot, sameLocalFields, validatePortableSnapshot } from "./local-fields.js";
|
|
17
23
|
import { readMergeJournal, requireNoMergeJournal, retireMergeJournal } from "./merge-journal.js";
|
|
18
24
|
import { mergeSync } from "./merged-sync.js";
|
|
19
25
|
import { readRemoteSnapshot, readRemoteSnapshotForUpload, readSnapshotForHead, requireCompatibleRemoteSelection, sameRemoteHead, snapshotForUpload, } from "./remote-snapshot.js";
|
|
20
26
|
import { throwIfAborted } from "./signals.js";
|
|
21
27
|
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,
|
|
28
|
+
import { errorMessage, PublicationStatePersistenceError } from "./sync-errors.js";
|
|
29
|
+
import { backupDirectionalSnapshot, backupLocal, captureMutationOwner, LocalBackupVerificationError, maybeReload, protectedSessionPaths, } from "./sync-local.js";
|
|
30
|
+
import { formatIgnorePolicyComparison, inspectRemoteSelection, syncSelectionHasContent, syncSessionsSelected, } from "./sync-policy.js";
|
|
25
31
|
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";
|
|
32
|
+
import { checkLiveProtection, directionalApplyGuards, requireIgnoreBaselineReview, requireResolvedDirectionalProgress, validateDirectionalPublication, } from "./transfer-freshness.js";
|
|
27
33
|
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
|
-
}
|
|
34
|
+
export { PublicationStatePersistenceError, RollbackPublicationError } from "./sync-errors.js";
|
|
35
|
+
export { rollback } from "./sync-rollback.js";
|
|
49
36
|
export async function push(ctx, options, input, factory = createSyncBackend) {
|
|
37
|
+
requireNoPendingAdoption();
|
|
50
38
|
const validateOwner = captureMutationOwner(ctx, options.signal);
|
|
51
39
|
const validateCommittedOwner = captureMutationOwner(ctx);
|
|
52
|
-
const
|
|
40
|
+
const action = foregroundDirectionalAction(ctx, options);
|
|
41
|
+
if (action && input)
|
|
42
|
+
throw new Error("Single-action Push must load the bound foreground setup.");
|
|
43
|
+
const config = action
|
|
44
|
+
? await loadConfigForCheck(undefined, options.signal)
|
|
45
|
+
: (input?.config ?? (await loadConfig(options.setup, options.signal)));
|
|
53
46
|
const validateMutation = () => {
|
|
54
47
|
validateOwner();
|
|
55
48
|
options.background?.validate(config);
|
|
49
|
+
action?.validate(config);
|
|
56
50
|
};
|
|
57
51
|
validateMutation();
|
|
58
52
|
await requireNoMergeJournal(config, options);
|
|
@@ -69,29 +63,39 @@ export async function push(ctx, options, input, factory = createSyncBackend) {
|
|
|
69
63
|
}
|
|
70
64
|
setSyncStatus(ctx, `pushing ${config.setupName}`, { activity: "pushing" });
|
|
71
65
|
const backend = input?.backend ?? (await factory(config));
|
|
72
|
-
|
|
66
|
+
validateMutation();
|
|
73
67
|
const snapshotOptions = await resolvedSnapshotOptionsForContext(ctx, config, options.signal);
|
|
68
|
+
if (action)
|
|
69
|
+
snapshotOptions.sessionDir = await effectiveSessionRoot(ctx, config, options.signal);
|
|
74
70
|
validateMutation();
|
|
75
71
|
const validateIgnorePolicy = ignorePolicyValidator({ ...snapshotOptions, validateMutation });
|
|
76
72
|
const localRaw = input?.local ?? (await createSnapshot(config.snapshotIdentity, snapshotOptions));
|
|
77
73
|
const local = portableSnapshot(localRaw, config.localFields);
|
|
74
|
+
const reviewedProtectedPaths = action ? protectedSessionPaths(ctx, snapshotOptions.sessionDir) : new Set();
|
|
78
75
|
if (validateIgnorePolicy)
|
|
79
76
|
await validateIgnorePolicy();
|
|
80
77
|
throwIfAborted(options.signal);
|
|
81
78
|
let head = await backend.readHead(options.signal);
|
|
82
|
-
let remoteForUpload =
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
79
|
+
let remoteForUpload = action && head
|
|
80
|
+
? await readSnapshotForHead(backend, head, options.signal, config)
|
|
81
|
+
: await readRemoteSnapshotForUpload(backend, config, head, state, options);
|
|
82
|
+
validateMutation();
|
|
83
|
+
const policyOverwrite = Boolean(config.ignorePolicy &&
|
|
84
|
+
remoteForUpload &&
|
|
85
|
+
inspectRemoteSelection(config.include, remoteForUpload, config.exclude, config).kind === "different");
|
|
86
|
+
if (policyOverwrite && remoteForUpload && (options.auto || options.background || options.yes || !ctx.hasUI))
|
|
87
|
+
requireCompatibleRemoteSelection(config, remoteForUpload);
|
|
88
|
+
if (action && !policyOverwrite)
|
|
89
|
+
requireDirectionalSelection(config, state, localRaw, remoteForUpload, snapshotOptions.sessionDir);
|
|
90
|
+
else
|
|
91
|
+
requireIgnoreBaselineReview(ctx, config, state, options);
|
|
92
|
+
if (!action && remoteForUpload && !options.force && !policyOverwrite) {
|
|
91
93
|
requireCompatibleRemoteSelection(config, remoteForUpload);
|
|
92
94
|
}
|
|
93
|
-
if (
|
|
94
|
-
|
|
95
|
+
if (!action &&
|
|
96
|
+
remoteChangedSinceState(head, state, config, (left, right) => backend.sameRevision(left, right)) &&
|
|
97
|
+
!options.force &&
|
|
98
|
+
!policyOverwrite) {
|
|
95
99
|
const remoteForConflict = remoteForUpload
|
|
96
100
|
? filterSnapshotForConfigPolicy(remoteForUpload, config, snapshotOptions)
|
|
97
101
|
: undefined;
|
|
@@ -117,15 +121,35 @@ export async function push(ctx, options, input, factory = createSyncBackend) {
|
|
|
117
121
|
}
|
|
118
122
|
const configIdentity = syncCheckConfigFingerprint(config);
|
|
119
123
|
let upload = await snapshotForUpload(backend, config, local, head, remoteForUpload, snapshotOptions);
|
|
124
|
+
if (action) {
|
|
125
|
+
upload = keepProtectedRemoteSessions(config, upload, remoteForUpload, snapshotOptions.sessionDir, reviewedProtectedPaths);
|
|
126
|
+
validatePortableSnapshot(upload);
|
|
127
|
+
}
|
|
128
|
+
upload = await withPublishedIgnoreRules(upload, config, options.signal);
|
|
129
|
+
validateMutation();
|
|
120
130
|
if (!config.skipSecretScan) {
|
|
121
|
-
const secrets = scanSnapshot(local);
|
|
131
|
+
const secrets = scanSnapshot({ ...local, ...(upload.ignoreRules ? { ignoreRules: upload.ignoreRules } : {}) });
|
|
122
132
|
if (secrets.length > 0) {
|
|
123
133
|
throw new Error(`Refusing to push possible secrets:\n${secrets.map((s) => `- ${s}`).join("\n")}`);
|
|
124
134
|
}
|
|
125
135
|
}
|
|
126
|
-
if (!
|
|
136
|
+
if ((!action || policyOverwrite) &&
|
|
137
|
+
!(await confirmPush(ctx, options, config, backend, local, upload, head, remoteForUpload, validateMutation))) {
|
|
127
138
|
return "cancelled";
|
|
128
139
|
}
|
|
140
|
+
if (action) {
|
|
141
|
+
await checkpointDirectionalAction(options, {
|
|
142
|
+
direction: "push",
|
|
143
|
+
config,
|
|
144
|
+
state,
|
|
145
|
+
local: localRaw,
|
|
146
|
+
remote: remoteForUpload,
|
|
147
|
+
head,
|
|
148
|
+
sessionDir: snapshotOptions.sessionDir,
|
|
149
|
+
protectedPaths: reviewedProtectedPaths,
|
|
150
|
+
});
|
|
151
|
+
validateMutation();
|
|
152
|
+
}
|
|
129
153
|
if (options.force) {
|
|
130
154
|
const refreshedHead = await backend.readHead(options.signal);
|
|
131
155
|
validateMutation();
|
|
@@ -147,14 +171,17 @@ export async function push(ctx, options, input, factory = createSyncBackend) {
|
|
|
147
171
|
}
|
|
148
172
|
}
|
|
149
173
|
validateMutation();
|
|
150
|
-
if (
|
|
174
|
+
if (action)
|
|
175
|
+
await validateDirectionalSettings(options, config, validateMutation);
|
|
176
|
+
else if (configIdentity !== syncCheckConfigFingerprint(await loadConfig(options.setup)))
|
|
151
177
|
throw new Error("Settings changed during push review.");
|
|
152
178
|
validateMutation();
|
|
153
|
-
if (
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
179
|
+
if (!action &&
|
|
180
|
+
(config.scope === "agent-directory" ||
|
|
181
|
+
validateIgnorePolicy ||
|
|
182
|
+
!options.yes ||
|
|
183
|
+
config.localFields !== undefined ||
|
|
184
|
+
(await readMergeJournal(config)))) {
|
|
158
185
|
const current = await createSnapshot(config.snapshotIdentity, snapshotOptions);
|
|
159
186
|
if (!sameHashes(fileHashMap(current), fileHashMap(localRaw)))
|
|
160
187
|
throw new Error("Local content changed during push review; retry from current bytes.");
|
|
@@ -167,65 +194,131 @@ export async function push(ctx, options, input, factory = createSyncBackend) {
|
|
|
167
194
|
snapshotOptions.sessionDir !== (await effectiveSessionRoot(ctx, config, options.signal)))
|
|
168
195
|
throw new Error("Pi session storage changed during push review.");
|
|
169
196
|
validateMutation();
|
|
170
|
-
if (
|
|
197
|
+
if (action)
|
|
198
|
+
await validateDirectionalSettings(options, config, validateMutation);
|
|
199
|
+
else if (configIdentity !== syncCheckConfigFingerprint(await loadConfig(options.setup)))
|
|
171
200
|
throw new Error("Settings changed during push review.");
|
|
172
201
|
validateMutation();
|
|
173
|
-
const validatePublication = validateIgnorePolicy || options.background
|
|
174
|
-
? () =>
|
|
175
|
-
(
|
|
176
|
-
|
|
177
|
-
|
|
178
|
-
|
|
179
|
-
|
|
202
|
+
const validatePublication = validateIgnorePolicy || options.background || action
|
|
203
|
+
? async () => {
|
|
204
|
+
if (action && !sameRemoteHead(backend, head, await backend.readHead(options.signal)))
|
|
205
|
+
throw new Error("Remote changed before Push could publish.");
|
|
206
|
+
validateMutation();
|
|
207
|
+
await validateDirectionalPublication(ctx, config, snapshotOptions, state, fileHashMap(localRaw), action
|
|
208
|
+
? async () => {
|
|
209
|
+
await validateIgnorePolicy?.();
|
|
210
|
+
await validateDirectionalSettings(options, config, validateMutation);
|
|
211
|
+
checkLiveProtection(ctx, snapshotOptions.sessionDir, reviewedProtectedPaths);
|
|
212
|
+
}
|
|
213
|
+
: (validateIgnorePolicy ??
|
|
214
|
+
(async () => {
|
|
215
|
+
if (configIdentity !== syncCheckConfigFingerprint(await loadConfig(options.setup, options.signal)))
|
|
216
|
+
throw new Error("Settings changed before push publication.");
|
|
217
|
+
validateMutation();
|
|
218
|
+
})), "push", Boolean(action));
|
|
219
|
+
}
|
|
180
220
|
: undefined;
|
|
181
221
|
if (validateIgnorePolicy) {
|
|
182
222
|
validatePortableSnapshot(upload);
|
|
183
223
|
await validateIgnorePolicy();
|
|
184
224
|
}
|
|
185
|
-
|
|
186
|
-
|
|
187
|
-
|
|
188
|
-
|
|
189
|
-
|
|
225
|
+
if (action)
|
|
226
|
+
await validatePublication?.();
|
|
227
|
+
const accepted = action ? filterSnapshotForConfigPolicy(upload, config, snapshotOptions) : local;
|
|
228
|
+
const restore = action && head && remoteForUpload
|
|
229
|
+
? remoteRestoreGuidance(head, remoteForUpload, config, snapshotOptions.sessionDir)
|
|
230
|
+
: undefined;
|
|
231
|
+
let result;
|
|
232
|
+
let startedPublication = false;
|
|
233
|
+
try {
|
|
234
|
+
result = await backend.publishSnapshot(upload, expectedRemoteHead(head), {
|
|
235
|
+
...(restore ? { preservePreviousHead: true } : {}),
|
|
236
|
+
signal: options.signal,
|
|
237
|
+
onCommit: action
|
|
238
|
+
? () => {
|
|
239
|
+
options.onCommit?.();
|
|
240
|
+
startedPublication = true;
|
|
241
|
+
}
|
|
242
|
+
: options.onCommit,
|
|
243
|
+
...(validatePublication ? { validateIgnorePolicy: validatePublication } : {}),
|
|
244
|
+
});
|
|
245
|
+
}
|
|
246
|
+
catch (error) {
|
|
247
|
+
if (!action || (!startedPublication && options.signal?.aborted))
|
|
248
|
+
throw error;
|
|
249
|
+
const mightCommit = startedPublication && !(error instanceof SyncBackendConflictError && error.phase === "before-commit");
|
|
250
|
+
const phase = mightCommit ? "Push publication outcome is not verified." : "Push did not replace the remote head.";
|
|
251
|
+
throw new Error([phase, errorMessage(error), startedPublication ? restore : undefined].filter(Boolean).join(" "), {
|
|
252
|
+
cause: error,
|
|
253
|
+
});
|
|
254
|
+
}
|
|
190
255
|
try {
|
|
191
256
|
await writeStateForConfig(config, {
|
|
192
257
|
version: VERSION,
|
|
193
258
|
profile: config.snapshotIdentity,
|
|
194
259
|
lastAppliedSnapshot: result.head.snapshotId,
|
|
195
260
|
lastRemoteRevision: result.head.revision,
|
|
196
|
-
lastFileHashes: fileHashMap(
|
|
261
|
+
lastFileHashes: fileHashMap(accepted),
|
|
197
262
|
...acceptedSelection(config),
|
|
198
263
|
...(config.localFields !== undefined ? { localFields: config.localFields } : {}),
|
|
199
|
-
},
|
|
264
|
+
}, accepted, validateCommittedOwner);
|
|
200
265
|
}
|
|
201
266
|
catch (error) {
|
|
202
|
-
|
|
267
|
+
const persistence = new PublicationStatePersistenceError(result.head, error);
|
|
268
|
+
throw restore ? new Error(`${persistence.message} ${restore}`, { cause: persistence }) : persistence;
|
|
203
269
|
}
|
|
204
|
-
|
|
205
|
-
|
|
206
|
-
|
|
207
|
-
|
|
208
|
-
|
|
209
|
-
|
|
210
|
-
|
|
211
|
-
|
|
212
|
-
|
|
213
|
-
|
|
270
|
+
try {
|
|
271
|
+
if (!action)
|
|
272
|
+
await retireMergeJournal(config);
|
|
273
|
+
await pruneMergeBaselines(config, await readStateForConfig(config), validateCommittedOwner);
|
|
274
|
+
validateCommittedOwner();
|
|
275
|
+
}
|
|
276
|
+
catch (error) {
|
|
277
|
+
if (!action)
|
|
278
|
+
throw error;
|
|
279
|
+
throw new Error([
|
|
280
|
+
`Remote publication ${result.head.snapshotId} is active, but final bookkeeping failed: ${errorMessage(error)}`,
|
|
281
|
+
restore,
|
|
214
282
|
]
|
|
215
283
|
.filter(Boolean)
|
|
216
|
-
.join("
|
|
284
|
+
.join(" "), { cause: error });
|
|
217
285
|
}
|
|
286
|
+
if (!options.signal?.aborted)
|
|
287
|
+
setSyncStatus(ctx, undefined);
|
|
288
|
+
if (options.signal?.aborted && !action)
|
|
289
|
+
return;
|
|
290
|
+
notifyDirectionalSuccess(ctx, options, [
|
|
291
|
+
`Pushed ${upload.files.length} files from sync setup “${config.setupName}” as ${result.head.snapshotId}.`,
|
|
292
|
+
restore,
|
|
293
|
+
...result.warnings,
|
|
294
|
+
]
|
|
295
|
+
.filter(Boolean)
|
|
296
|
+
.join("\n"), result.warnings.length > 0);
|
|
218
297
|
return "applied";
|
|
219
298
|
}
|
|
220
299
|
export async function pull(ctx, options, factory = createSyncBackend, initial) {
|
|
221
|
-
const
|
|
300
|
+
const action = foregroundDirectionalAction(ctx, options);
|
|
301
|
+
if (action && initial)
|
|
302
|
+
throw new Error("Single-action Pull cannot be used for initial-source selection.");
|
|
303
|
+
const adoptionReview = !initial && canReviewAdoption(ctx, options);
|
|
304
|
+
const journal = adoptionReview ? await loadAdoptionJournal() : undefined;
|
|
305
|
+
if (!adoptionReview)
|
|
306
|
+
requireNoPendingAdoption();
|
|
307
|
+
const validateOwner = captureMutationOwner(ctx, options.signal);
|
|
222
308
|
const validateCommittedOwner = captureMutationOwner(ctx);
|
|
223
|
-
const config = await loadConfig(options.setup);
|
|
309
|
+
const config = action ? await loadConfigForCheck(undefined, options.signal) : await loadConfig(options.setup);
|
|
310
|
+
const validateMutation = () => {
|
|
311
|
+
validateOwner();
|
|
312
|
+
action?.validate(config);
|
|
313
|
+
};
|
|
224
314
|
validateMutation();
|
|
225
315
|
const configIdentity = syncCheckConfigFingerprint(config);
|
|
226
316
|
if (initial && initial.configIdentity !== configIdentity)
|
|
227
317
|
throw new Error("Settings changed before initial pull; run sync again.");
|
|
228
318
|
await requireNoMergeJournal(config, options);
|
|
319
|
+
if (action && !journal && (await pendingTransactionEntries()).length)
|
|
320
|
+
throw new Error("A pending local transaction must be reconciled before Pull; its evidence was retained.");
|
|
321
|
+
validateMutation();
|
|
229
322
|
setSyncStatus(ctx, `pulling ${config.setupName}`, { activity: "pulling" });
|
|
230
323
|
const backend = await factory(config);
|
|
231
324
|
const state = await readStateForConfig(config);
|
|
@@ -234,7 +327,29 @@ export async function pull(ctx, options, factory = createSyncBackend, initial) {
|
|
|
234
327
|
throw new Error("Baseline changed before initial pull; run sync again.");
|
|
235
328
|
requireResolvedDirectionalProgress(state, options);
|
|
236
329
|
const snapshotOptions = await resolvedSnapshotOptionsForContext(ctx, config, options.signal);
|
|
237
|
-
|
|
330
|
+
if (action)
|
|
331
|
+
snapshotOptions.sessionDir = await effectiveSessionRoot(ctx, config, options.signal);
|
|
332
|
+
validateMutation();
|
|
333
|
+
if (journal) {
|
|
334
|
+
if (journal.setupName !== config.setupName ||
|
|
335
|
+
journal.setupFingerprint !== adoptionSettingsFingerprint(config) ||
|
|
336
|
+
config.pathPolicy !== "ignore-file")
|
|
337
|
+
throw new Error("Pending rule adoption belongs to a different setup. Preserve its journal and use the originating setup.");
|
|
338
|
+
const remote = await readSnapshotForHead(backend, journal.head, options.signal, config);
|
|
339
|
+
validateMutation();
|
|
340
|
+
return adoptRemoteRules({
|
|
341
|
+
ctx,
|
|
342
|
+
options,
|
|
343
|
+
config,
|
|
344
|
+
backend,
|
|
345
|
+
state,
|
|
346
|
+
head: journal.head,
|
|
347
|
+
remote,
|
|
348
|
+
snapshotOptions,
|
|
349
|
+
validateMutation,
|
|
350
|
+
journal,
|
|
351
|
+
});
|
|
352
|
+
}
|
|
238
353
|
const validateIgnorePolicy = ignorePolicyValidator({ ...snapshotOptions, validateMutation });
|
|
239
354
|
if (initial && initial.sessionDir !== snapshotOptions.sessionDir)
|
|
240
355
|
throw new Error("Pi session storage changed before initial pull; run sync again.");
|
|
@@ -242,11 +357,45 @@ export async function pull(ctx, options, factory = createSyncBackend, initial) {
|
|
|
242
357
|
validateMutation();
|
|
243
358
|
if (initial && !sameHashes(initial.localHashes, fileHashMap(local)))
|
|
244
359
|
throw new Error("Local bytes changed before initial pull; run sync again.");
|
|
245
|
-
|
|
360
|
+
let preimageRemote;
|
|
361
|
+
let adoption;
|
|
362
|
+
const { head, snapshot: remote } = await readRemoteSnapshot(backend, config, options.signal, {
|
|
363
|
+
...snapshotOptions,
|
|
364
|
+
...(action || (adoptionReview && config.ignorePolicy)
|
|
365
|
+
? {
|
|
366
|
+
validateSelection: (raw) => {
|
|
367
|
+
const comparison = inspectRemoteSelection(config.include, raw, config.exclude, config).ignorePolicyComparison;
|
|
368
|
+
if (adoptionReview && comparison === "mismatch") {
|
|
369
|
+
adoption = raw;
|
|
370
|
+
}
|
|
371
|
+
else if (action) {
|
|
372
|
+
requireDirectionalSelection(config, state, local, raw, snapshotOptions.sessionDir);
|
|
373
|
+
preimageRemote = raw;
|
|
374
|
+
}
|
|
375
|
+
else {
|
|
376
|
+
requireCompatibleRemoteSelection(config, raw);
|
|
377
|
+
}
|
|
378
|
+
},
|
|
379
|
+
}
|
|
380
|
+
: {}),
|
|
381
|
+
});
|
|
246
382
|
validateMutation();
|
|
383
|
+
if (adoption && head)
|
|
384
|
+
return adoptRemoteRules({
|
|
385
|
+
ctx,
|
|
386
|
+
options,
|
|
387
|
+
config,
|
|
388
|
+
backend,
|
|
389
|
+
state,
|
|
390
|
+
head,
|
|
391
|
+
remote: adoption,
|
|
392
|
+
snapshotOptions,
|
|
393
|
+
validateMutation,
|
|
394
|
+
});
|
|
247
395
|
if (initial && !matchesInitialHead(initial, backend, head))
|
|
248
396
|
throw new Error("Remote changed before initial pull; run sync again.");
|
|
249
|
-
|
|
397
|
+
if (!action)
|
|
398
|
+
requireIgnoreBaselineReview(ctx, config, state, options);
|
|
250
399
|
const localChanged = hasLocalChanges(local, state, config);
|
|
251
400
|
if (!remote) {
|
|
252
401
|
throw createSyncDecision({
|
|
@@ -269,11 +418,11 @@ export async function pull(ctx, options, factory = createSyncBackend, initial) {
|
|
|
269
418
|
}
|
|
270
419
|
const logical = portableSnapshot(remote, config.localFields);
|
|
271
420
|
const physical = overlayLocalFields(logical, local, config.localFields);
|
|
272
|
-
if (config.scope === "agent-directory")
|
|
421
|
+
if (action || config.scope === "agent-directory")
|
|
273
422
|
requireStableMergeSessionRoot(local, physical, config.scope);
|
|
274
423
|
const reviewedProtectedPaths = protectedSessionPaths(ctx, snapshotOptions.sessionDir);
|
|
275
424
|
const remoteChanged = hasRemoteChanges(remote, state, config, reviewedProtectedPaths);
|
|
276
|
-
if (localChanged && remoteChanged && state.lastAppliedSnapshot && !options.force) {
|
|
425
|
+
if (!action && localChanged && remoteChanged && state.lastAppliedSnapshot && !options.force) {
|
|
277
426
|
throw createSyncDecision({
|
|
278
427
|
kind: "both-changed",
|
|
279
428
|
config,
|
|
@@ -285,14 +434,32 @@ export async function pull(ctx, options, factory = createSyncBackend, initial) {
|
|
|
285
434
|
directMessage: "Both local and remote changed since last sync. Run /sync diff, then choose /sync pull --force or /sync push --force.",
|
|
286
435
|
});
|
|
287
436
|
}
|
|
288
|
-
const caseCleanup = options.yes ? [] : await inspectCaseCleanup(physical, snapshotOptions);
|
|
437
|
+
const caseCleanup = action || options.yes ? [] : await inspectCaseCleanup(physical, snapshotOptions);
|
|
289
438
|
validateMutation();
|
|
290
|
-
if (!
|
|
439
|
+
if (!action &&
|
|
440
|
+
!options.yes &&
|
|
291
441
|
!(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
442
|
setSyncStatus(ctx, undefined);
|
|
293
443
|
ctx.ui.notify("Pull cancelled.", "info");
|
|
294
444
|
return "cancelled";
|
|
295
445
|
}
|
|
446
|
+
const validateActionBoundary = action
|
|
447
|
+
? () => requireDirectionalPullBoundary(ctx, options, config, config.scope ? physical : { ...physical, files: [...local.files, ...physical.files] }, snapshotOptions, reviewedProtectedPaths, validateMutation)
|
|
448
|
+
: undefined;
|
|
449
|
+
if (action) {
|
|
450
|
+
await checkpointDirectionalAction(options, {
|
|
451
|
+
direction: "pull",
|
|
452
|
+
config,
|
|
453
|
+
state,
|
|
454
|
+
local,
|
|
455
|
+
remote: preimageRemote,
|
|
456
|
+
head,
|
|
457
|
+
sessionDir: snapshotOptions.sessionDir,
|
|
458
|
+
protectedPaths: reviewedProtectedPaths,
|
|
459
|
+
});
|
|
460
|
+
validateMutation();
|
|
461
|
+
await validateActionBoundary?.();
|
|
462
|
+
}
|
|
296
463
|
throwIfAborted(options.signal);
|
|
297
464
|
const recoveringMerge = Boolean(await readMergeJournal(config));
|
|
298
465
|
const requireFreshRecovery = async () => {
|
|
@@ -307,7 +474,7 @@ export async function pull(ctx, options, factory = createSyncBackend, initial) {
|
|
|
307
474
|
};
|
|
308
475
|
if (recoveringMerge)
|
|
309
476
|
await requireFreshRecovery();
|
|
310
|
-
if (configIdentity !== syncCheckConfigFingerprint(await loadConfig(options.setup)))
|
|
477
|
+
if (!action && configIdentity !== syncCheckConfigFingerprint(await loadConfig(options.setup)))
|
|
311
478
|
throw new Error("Settings changed during pull review.");
|
|
312
479
|
validateMutation();
|
|
313
480
|
const refreshed = await createSnapshot(config.snapshotIdentity, snapshotOptions);
|
|
@@ -316,78 +483,116 @@ export async function pull(ctx, options, factory = createSyncBackend, initial) {
|
|
|
316
483
|
if (!sameRemoteHead(backend, head, await backend.readHead(options.signal)))
|
|
317
484
|
throw new Error("Remote changed during pull review.");
|
|
318
485
|
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
|
|
486
|
+
const restorePoint = action ? await backupDirectionalSnapshot(local, options.signal) : undefined;
|
|
487
|
+
const backup = restorePoint?.backupPath ?? (await backupLocal(config.snapshotIdentity, snapshotOptions, options.signal));
|
|
488
|
+
let mayHaveApplied = false;
|
|
489
|
+
let installed = false;
|
|
490
|
+
try {
|
|
491
|
+
const applySessionDir = action
|
|
492
|
+
? snapshotOptions.sessionDir
|
|
493
|
+
: await sessionDirForApply(ctx, physical, snapshotOptions);
|
|
494
|
+
if (recoveringMerge)
|
|
495
|
+
await requireFreshRecovery();
|
|
496
|
+
const currentState = await readStateForConfig(config);
|
|
497
|
+
validateMutation();
|
|
498
|
+
if ((validateIgnorePolicy || initial || !options.yes) &&
|
|
499
|
+
syncStateFingerprint(currentState) !== syncStateFingerprint(state))
|
|
500
|
+
throw new Error("Baseline changed during pull review.");
|
|
501
|
+
requireResolvedDirectionalProgress(currentState, options);
|
|
502
|
+
if (!action && !options.yes)
|
|
503
|
+
await revalidateCaseCleanup(caseCleanup, physical, snapshotOptions);
|
|
504
|
+
if (initial && !matchesInitialHead(initial, backend, await backend.readHead(options.signal)))
|
|
505
|
+
throw new Error("Remote changed during initial pull; run sync again.");
|
|
506
|
+
validateMutation();
|
|
507
|
+
if (config.scope && applySessionDir !== (await effectiveSessionRoot(ctx, config, options.signal)))
|
|
508
|
+
throw new Error("Pi session storage changed during pull review.");
|
|
509
|
+
validateMutation();
|
|
510
|
+
if (!action && configIdentity !== syncCheckConfigFingerprint(await loadConfig(options.setup)))
|
|
511
|
+
throw new Error("Settings changed during pull review.");
|
|
512
|
+
validateMutation();
|
|
513
|
+
if (validateIgnorePolicy) {
|
|
514
|
+
if (!sameRemoteHead(backend, head, await backend.readHead(options.signal)))
|
|
515
|
+
throw new Error("Remote changed during pull review.");
|
|
516
|
+
await validateIgnorePolicy();
|
|
517
|
+
}
|
|
518
|
+
const protectedPaths = protectedSessionPaths(ctx, snapshotOptions.sessionDir);
|
|
519
|
+
const applyGuards = directionalApplyGuards({
|
|
520
|
+
ctx,
|
|
521
|
+
config,
|
|
522
|
+
backend,
|
|
523
|
+
state,
|
|
524
|
+
sessionDir: snapshotOptions.sessionDir,
|
|
525
|
+
head,
|
|
526
|
+
protectedPaths: reviewedProtectedPaths,
|
|
527
|
+
signal: options.signal,
|
|
528
|
+
onCommit: action
|
|
529
|
+
? () => {
|
|
530
|
+
options.onCommit?.();
|
|
531
|
+
mayHaveApplied = true;
|
|
532
|
+
}
|
|
533
|
+
: options.onCommit,
|
|
534
|
+
validateMutation,
|
|
535
|
+
validateIgnorePolicy: action
|
|
536
|
+
? async () => {
|
|
537
|
+
await restorePoint?.validate();
|
|
538
|
+
await validateIgnorePolicy?.();
|
|
539
|
+
await validateActionBoundary?.();
|
|
540
|
+
}
|
|
541
|
+
: validateIgnorePolicy,
|
|
542
|
+
effectiveRoot: Boolean(action),
|
|
543
|
+
});
|
|
544
|
+
applyGuards.validateMutation();
|
|
545
|
+
if (!validateIgnorePolicy && !action)
|
|
546
|
+
options.onCommit?.();
|
|
547
|
+
const lastFileHashes = await applySnapshot(physical, protectedPaths, {
|
|
548
|
+
...snapshotOptions,
|
|
549
|
+
sessionDir: applySessionDir,
|
|
550
|
+
...applyGuards,
|
|
551
|
+
...(action || config.scope === "agent-directory" || validateIgnorePolicy ? { reviewedLocal: local } : {}),
|
|
552
|
+
expectedFileHashes: config.localFields?.length
|
|
553
|
+
? { "settings.json": fileHashMap(local)["settings.json"] ?? null }
|
|
554
|
+
: undefined,
|
|
555
|
+
});
|
|
556
|
+
// Local installation is complete; user cancellation must not leave the baseline stale.
|
|
557
|
+
installed = true;
|
|
558
|
+
validateCommittedOwner();
|
|
559
|
+
await writeStateForConfig(config, {
|
|
560
|
+
version: VERSION,
|
|
561
|
+
profile: config.snapshotIdentity,
|
|
562
|
+
lastAppliedSnapshot: remote.id,
|
|
563
|
+
lastRemoteRevision: head?.revision,
|
|
564
|
+
lastFileHashes: config.localFields?.length
|
|
565
|
+
? { ...lastFileHashes, "settings.json": fileHashMap(logical)["settings.json"] }
|
|
566
|
+
: lastFileHashes,
|
|
567
|
+
...acceptedSelection(config),
|
|
568
|
+
...(config.localFields !== undefined ? { localFields: config.localFields } : {}),
|
|
569
|
+
}, logical, validateCommittedOwner);
|
|
570
|
+
if (!action)
|
|
571
|
+
await retireMergeJournal(config);
|
|
572
|
+
await pruneMergeBaselines(config, await readStateForConfig(config), validateCommittedOwner);
|
|
573
|
+
validateCommittedOwner();
|
|
344
574
|
}
|
|
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)
|
|
575
|
+
catch (error) {
|
|
576
|
+
if (!action || (options.signal?.aborted && !mayHaveApplied))
|
|
577
|
+
throw error;
|
|
578
|
+
const phase = installed
|
|
579
|
+
? "Local snapshot was applied, but final bookkeeping failed."
|
|
580
|
+
: mayHaveApplied
|
|
581
|
+
? "Local apply did not finish; its outcome is not verified."
|
|
582
|
+
: "Pull stopped before applying local files.";
|
|
583
|
+
const message = `${phase} ${error instanceof LocalBackupVerificationError ? "" : `${localRestoreGuidance(backup)} `}${errorMessage(error)}`;
|
|
584
|
+
if (error instanceof AggregateError)
|
|
585
|
+
throw new AggregateError(error.errors, message, { cause: error });
|
|
586
|
+
throw new Error(message, { cause: error });
|
|
587
|
+
}
|
|
588
|
+
if (options.signal?.aborted && !action)
|
|
388
589
|
return "applied";
|
|
389
|
-
|
|
390
|
-
|
|
590
|
+
if (!options.signal?.aborted)
|
|
591
|
+
setSyncStatus(ctx, undefined);
|
|
592
|
+
if (action) {
|
|
593
|
+
notifyDirectionalSuccess(ctx, options, `Pulled snapshot ${remote.id}. ${localRestoreGuidance(backup)}`);
|
|
594
|
+
}
|
|
595
|
+
else if (!options.silent) {
|
|
391
596
|
ctx.ui.notify(`Pulled ${remote.files.length} files from ${remote.id}. Backup: ${backup}`, "info");
|
|
392
597
|
}
|
|
393
598
|
else if (options.auto && syncSessionsSelected(config) && snapshotIncludesSessions(remote)) {
|
|
@@ -398,6 +603,9 @@ export async function pull(ctx, options, factory = createSyncBackend, initial) {
|
|
|
398
603
|
return "applied";
|
|
399
604
|
}
|
|
400
605
|
export async function syncBoth(ctx, options, factory = createSyncBackend) {
|
|
606
|
+
requireNoPendingAdoption();
|
|
607
|
+
if (options.directionalAction)
|
|
608
|
+
throw new Error("Single-action transfer cannot authorize bidirectional Sync.");
|
|
401
609
|
const validateMutation = captureMutationOwner(ctx, options.signal);
|
|
402
610
|
const config = await loadConfig(options.setup);
|
|
403
611
|
throwIfAborted(options.signal);
|
|
@@ -550,192 +758,6 @@ export async function syncBoth(ctx, options, factory = createSyncBackend) {
|
|
|
550
758
|
if (!options.silent)
|
|
551
759
|
ctx.ui.notify("pi-sync is already up to date.", "info");
|
|
552
760
|
}
|
|
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
761
|
async function confirmPush(ctx, options, config, backend, local, upload, head, remote, validateMutation, title = snapshotIncludesSessions(upload) ? "Push pi settings and sessions?" : "Push pi settings?") {
|
|
740
762
|
validateMutation();
|
|
741
763
|
if (options.yes)
|
|
@@ -744,9 +766,16 @@ async function confirmPush(ctx, options, config, backend, local, upload, head, r
|
|
|
744
766
|
const comparison = config.ignorePolicy && remote
|
|
745
767
|
? inspectRemoteSelection(config.include, remote, config.exclude, config).ignorePolicyComparison
|
|
746
768
|
: undefined;
|
|
747
|
-
const
|
|
748
|
-
|
|
749
|
-
|
|
769
|
+
const notice = ignorePublicationNotice(upload);
|
|
770
|
+
const overwrite = comparison !== undefined && comparison !== "match";
|
|
771
|
+
const confirmed = await confirmTransferReview(ctx, overwrite ? "Overwrite remote policy and push?" : title, formatTransferReview(...(overwrite
|
|
772
|
+
? [
|
|
773
|
+
formatIgnorePolicyComparison(comparison),
|
|
774
|
+
"This replaces the remote policy with this device's rules. Remote rules are not adopted on this device.",
|
|
775
|
+
`Sessions opt-in: ${remote?.selection?.version === 4 ? (remote.selection.sessions ? "on" : "off") : "legacy"} → ${config.ignorePolicy?.sessions ? "on" : "off"}.`,
|
|
776
|
+
formatRuleChanges(remote?.ignoreRules?.text, upload.ignoreRules?.text),
|
|
777
|
+
]
|
|
778
|
+
: []), ...(notice ? [notice, ""] : []), summary), options.signal, validateMutation);
|
|
750
779
|
throwIfAborted(options.signal);
|
|
751
780
|
if (confirmed)
|
|
752
781
|
return true;
|