@narumitw/pi-sync-daemon 0.7.0 → 0.9.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 +21 -0
- package/README.md +16 -2
- package/dist/cli.js +1 -1
- package/dist/consent.d.ts +14 -2
- package/dist/consent.js +147 -14
- package/dist/engine/backends/git/git-backend.d.ts +2 -3
- package/dist/engine/backends/git/git-backend.js +71 -25
- package/dist/engine/backends/git/git-storage.d.ts +1 -1
- package/dist/engine/backends/git/git-storage.js +30 -9
- 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/commands/command-types.d.ts +2 -1
- package/dist/engine/host.d.ts +1 -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 +9 -0
- package/dist/engine/ignore/ignore-initialization.js +252 -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 +30 -0
- package/dist/engine/ignore/ignore-settings.js +393 -0
- package/dist/engine/ignore/ignore-source.d.ts +27 -0
- package/dist/engine/ignore/ignore-source.js +250 -0
- package/dist/engine/settings/config-file.d.ts +9 -2
- package/dist/engine/settings/config-file.js +124 -28
- package/dist/engine/settings/config.d.ts +6 -2
- package/dist/engine/settings/config.js +140 -11
- package/dist/engine/settings/settings-management.d.ts +5 -3
- package/dist/engine/settings/settings-management.js +24 -10
- package/dist/engine/settings/settings-types.d.ts +25 -2
- package/dist/engine/settings/settings-validation.js +23 -5
- package/dist/engine/snapshot/session-paths.d.ts +1 -1
- package/dist/engine/snapshot/session-paths.js +41 -13
- package/dist/engine/snapshot/snapshot-apply.d.ts +3 -3
- package/dist/engine/snapshot/snapshot-apply.js +23 -4
- package/dist/engine/snapshot/snapshot-codec.d.ts +1 -1
- package/dist/engine/snapshot/snapshot-codec.js +14 -5
- package/dist/engine/snapshot/snapshot-exclude-safety.d.ts +4 -2
- package/dist/engine/snapshot/snapshot-exclude-safety.js +76 -19
- package/dist/engine/snapshot/snapshot-transaction-images.d.ts +3 -1
- package/dist/engine/snapshot/snapshot-transaction-images.js +33 -3
- package/dist/engine/snapshot/snapshot-transaction-policy.d.ts +30 -2
- package/dist/engine/snapshot/snapshot-transaction-policy.js +135 -11
- 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 -12
- package/dist/engine/snapshot/snapshot-transaction.js +129 -109
- package/dist/engine/snapshot/snapshot-types.d.ts +27 -1
- package/dist/engine/snapshot/snapshot-types.js +9 -1
- package/dist/engine/snapshot/snapshot.d.ts +6 -4
- package/dist/engine/snapshot/snapshot.js +91 -34
- package/dist/engine/state/state-types.d.ts +1 -0
- package/dist/engine/sync/agent-plan.js +1 -0
- package/dist/engine/sync/agent-sync.js +8 -1
- package/dist/engine/sync/continuous-sync.d.ts +3 -1
- package/dist/engine/sync/continuous-sync.js +50 -18
- package/dist/engine/sync/continuous-watch.js +4 -1
- package/dist/engine/sync/initialization.js +3 -2
- package/dist/engine/sync/local-fields.d.ts +3 -1
- package/dist/engine/sync/local-fields.js +62 -1
- package/dist/engine/sync/merge-acceptance.d.ts +8 -0
- package/dist/engine/sync/merge-acceptance.js +29 -0
- package/dist/engine/sync/merge-apply.js +42 -11
- package/dist/engine/sync/merge-journal.d.ts +24 -4
- package/dist/engine/sync/merge-journal.js +134 -8
- package/dist/engine/sync/merged-sync.d.ts +1 -1
- package/dist/engine/sync/merged-sync.js +128 -83
- package/dist/engine/sync/remote-snapshot.d.ts +11 -3
- package/dist/engine/sync/remote-snapshot.js +72 -18
- package/dist/engine/sync/session-push.js +20 -6
- package/dist/engine/sync/sync-inspection.js +7 -5
- package/dist/engine/sync/sync-mutations.d.ts +2 -1
- package/dist/engine/sync/sync-mutations.js +157 -73
- package/dist/engine/sync/sync-policy.d.ts +29 -8
- package/dist/engine/sync/sync-policy.js +152 -16
- package/dist/engine/sync/sync-queries.js +8 -1
- package/dist/engine/sync/sync-state.d.ts +4 -2
- package/dist/engine/sync/sync-state.js +15 -2
- package/dist/engine/sync/transfer-freshness.d.ts +31 -0
- package/dist/engine/sync/transfer-freshness.js +82 -0
- package/dist/engine-service.d.ts +1 -1
- package/dist/engine-service.js +146 -27
- package/dist/monitor.d.ts +4 -1
- package/dist/monitor.js +84 -32
- package/dist/server.d.ts +1 -0
- package/dist/server.js +46 -5
- package/dist/service-host.d.ts +1 -1
- package/dist/service-host.js +2 -1
- package/docs/architecture.md +30 -0
- package/package.json +2 -1
|
@@ -1,5 +1,7 @@
|
|
|
1
|
+
import { createHash } from "node:crypto";
|
|
1
2
|
import path from "node:path";
|
|
2
3
|
import { isDeniedPath, toPosix } from "../paths.js";
|
|
4
|
+
import { IGNORE_SNAPSHOT_VERSION, } from "../snapshot/snapshot-types.js";
|
|
3
5
|
import { compilePathRules, compileSyncExclude, normalizeSyncExclude, sameSyncExclude } from "./exclude-policy.js";
|
|
4
6
|
export const BUILT_IN_SYNC_ROOTS = [
|
|
5
7
|
"settings.json",
|
|
@@ -108,6 +110,22 @@ export function normalizeDirectoryRules(value) {
|
|
|
108
110
|
}
|
|
109
111
|
export function portableSnapshotSelection(value) {
|
|
110
112
|
const selection = value;
|
|
113
|
+
if (selection && typeof selection === "object" && !Array.isArray(selection) && selection.version === 4) {
|
|
114
|
+
if (selection.scope !== "agent-directory" ||
|
|
115
|
+
typeof selection.sessions !== "boolean" ||
|
|
116
|
+
typeof selection.fingerprint !== "string" ||
|
|
117
|
+
selection.fingerprint.length !== 64 ||
|
|
118
|
+
!/^[0-9a-f]{64}$/u.test(selection.fingerprint) ||
|
|
119
|
+
Object.keys(selection).length !== 4 ||
|
|
120
|
+
!["version", "scope", "sessions", "fingerprint"].every((key) => Object.hasOwn(selection, key)))
|
|
121
|
+
throw new Error("Invalid ignore-file snapshot policy.");
|
|
122
|
+
return {
|
|
123
|
+
version: 4,
|
|
124
|
+
scope: "agent-directory",
|
|
125
|
+
fingerprint: selection.fingerprint,
|
|
126
|
+
sessions: selection.sessions,
|
|
127
|
+
};
|
|
128
|
+
}
|
|
111
129
|
if (selection && typeof selection === "object" && !Array.isArray(selection) && selection.version === 3) {
|
|
112
130
|
if (selection.scope !== "agent-directory" ||
|
|
113
131
|
!Array.isArray(selection.exclude) ||
|
|
@@ -132,7 +150,10 @@ export function portableSnapshotSelection(value) {
|
|
|
132
150
|
return { version: 2, include, exclude: normalizeSyncExclude(selection.exclude) };
|
|
133
151
|
}
|
|
134
152
|
export function snapshotSelectionInclude(snapshot) {
|
|
135
|
-
|
|
153
|
+
if (snapshot.selection === undefined)
|
|
154
|
+
return undefined;
|
|
155
|
+
const selection = portableSnapshotSelection(snapshot.selection);
|
|
156
|
+
return selection.version === 4 ? (selection.sessions ? ["sessions"] : []) : selection.include;
|
|
136
157
|
}
|
|
137
158
|
export function snapshotSelectionExclude(snapshot) {
|
|
138
159
|
return snapshot.selection === undefined ? undefined : portableSnapshotSelection(snapshot.selection).exclude;
|
|
@@ -150,6 +171,16 @@ function directorySelection(include, exclude, reinclude) {
|
|
|
150
171
|
};
|
|
151
172
|
}
|
|
152
173
|
export function selectionForSnapshot(include, exclude, directory) {
|
|
174
|
+
if (directory?.ignorePolicy) {
|
|
175
|
+
if (directory.scope !== "agent-directory")
|
|
176
|
+
throw new Error("Ignore rules require agent-directory scope.");
|
|
177
|
+
return portableSnapshotSelection({
|
|
178
|
+
version: 4,
|
|
179
|
+
scope: directory.scope,
|
|
180
|
+
fingerprint: directory.ignorePolicy.fingerprint,
|
|
181
|
+
sessions: directory.ignorePolicy.sessions,
|
|
182
|
+
});
|
|
183
|
+
}
|
|
153
184
|
if (directory?.scope === "agent-directory")
|
|
154
185
|
return directorySelection(include, exclude, directory.reinclude);
|
|
155
186
|
const normalized = normalizeSyncInclude(include);
|
|
@@ -168,6 +199,12 @@ export function validateSnapshotSelection(snapshot) {
|
|
|
168
199
|
throw new Error("Exclude policy requires snapshot version 4 and selection version 2.");
|
|
169
200
|
if ((snapshot.version === 5) !== (selection?.version === 3))
|
|
170
201
|
throw new Error("Agent-directory policy requires snapshot version 5 and selection version 3.");
|
|
202
|
+
if ((snapshot.version === IGNORE_SNAPSHOT_VERSION) !== (selection?.version === 4))
|
|
203
|
+
throw new Error("Ignore-file policy requires snapshot version 6 and selection version 4.");
|
|
204
|
+
}
|
|
205
|
+
export function requireLegacyTransferPolicy(snapshot) {
|
|
206
|
+
if (snapshot.version === IGNORE_SNAPSHOT_VERSION || snapshot.selection?.version === 4)
|
|
207
|
+
throw new Error("This setup cannot transfer an ignore-file snapshot without a reviewed local ignore policy.");
|
|
171
208
|
}
|
|
172
209
|
export function sameSyncInclude(left, right) {
|
|
173
210
|
const normalizedLeft = normalizeSyncInclude(left);
|
|
@@ -186,9 +223,39 @@ export function compareSyncInclude(local, remote) {
|
|
|
186
223
|
localOnly: localInclude.filter((item) => !remoteSet.has(item)),
|
|
187
224
|
};
|
|
188
225
|
}
|
|
226
|
+
export function compareIgnorePolicy(local, remote) {
|
|
227
|
+
if (remote?.version !== 4)
|
|
228
|
+
return local ? "local-ignore-remote-legacy" : undefined;
|
|
229
|
+
if (!local)
|
|
230
|
+
return "local-legacy-remote-ignore";
|
|
231
|
+
return local.fingerprint === remote.fingerprint && local.sessions === remote.sessions ? "match" : "mismatch";
|
|
232
|
+
}
|
|
233
|
+
export function formatIgnorePolicyComparison(comparison) {
|
|
234
|
+
switch (comparison) {
|
|
235
|
+
case "match":
|
|
236
|
+
return "Remote ignore-file policy matches this device.";
|
|
237
|
+
case "mismatch":
|
|
238
|
+
return "Remote ignore-file policy differs. Edit local rules to match, or review publishing this device's policy.";
|
|
239
|
+
case "local-ignore-remote-legacy":
|
|
240
|
+
return "Remote uses a legacy content policy. Review publishing this device's ignore-file policy.";
|
|
241
|
+
case "local-legacy-remote-ignore":
|
|
242
|
+
return "Remote uses an ignore-file policy. Configure matching local rules before transferring.";
|
|
243
|
+
}
|
|
244
|
+
}
|
|
189
245
|
export function syncDirectoryPolicyKey(config) {
|
|
190
246
|
if (config.scope !== "agent-directory")
|
|
191
247
|
return undefined;
|
|
248
|
+
const policy = config.ignorePolicy;
|
|
249
|
+
if (policy) {
|
|
250
|
+
return JSON.stringify([
|
|
251
|
+
"ignore-file",
|
|
252
|
+
policy.fingerprint,
|
|
253
|
+
policy.sessions,
|
|
254
|
+
policy.filename,
|
|
255
|
+
policy.sourceDigest,
|
|
256
|
+
policy.reservedNames,
|
|
257
|
+
]);
|
|
258
|
+
}
|
|
192
259
|
return JSON.stringify([
|
|
193
260
|
config.scope,
|
|
194
261
|
includeFromSelectionConfig(config).includes("sessions"),
|
|
@@ -196,8 +263,34 @@ export function syncDirectoryPolicyKey(config) {
|
|
|
196
263
|
normalizeDirectoryRules(config.reinclude).map((rule) => rule.toLowerCase()),
|
|
197
264
|
]);
|
|
198
265
|
}
|
|
266
|
+
export function localIgnorePolicyKey(config) {
|
|
267
|
+
if (!config.ignorePolicy && config.reservedIgnoreNames === undefined)
|
|
268
|
+
return undefined;
|
|
269
|
+
const reserved = reservedIgnoreSnapshotNames(config);
|
|
270
|
+
return createHash("sha256")
|
|
271
|
+
.update(JSON.stringify([
|
|
272
|
+
config.setupName,
|
|
273
|
+
config.recoverySetupKey,
|
|
274
|
+
syncDirectoryPolicyKey(config) ?? [
|
|
275
|
+
includeFromSelectionConfig(config),
|
|
276
|
+
normalizeSyncExclude(config.exclude).map((rule) => rule.toLowerCase()),
|
|
277
|
+
],
|
|
278
|
+
config.ignoreSourceIdentity,
|
|
279
|
+
[...(reserved ?? [])].sort(),
|
|
280
|
+
]))
|
|
281
|
+
.digest("hex");
|
|
282
|
+
}
|
|
199
283
|
export function inspectRemoteSelection(localInclude, snapshot, localExclude, directory) {
|
|
200
284
|
const selection = snapshot.selection === undefined ? undefined : portableSnapshotSelection(snapshot.selection);
|
|
285
|
+
const ignorePolicyComparison = compareIgnorePolicy(directory?.ignorePolicy, selection);
|
|
286
|
+
if (ignorePolicyComparison !== undefined) {
|
|
287
|
+
const include = snapshotSelectionInclude(snapshot) ?? discoverLegacySnapshotInclude(snapshot);
|
|
288
|
+
const scope = selection?.scope;
|
|
289
|
+
if (ignorePolicyComparison === "match")
|
|
290
|
+
return { kind: "same", include, scope, ignorePolicyComparison };
|
|
291
|
+
const comparison = compareSyncInclude(directory?.ignorePolicy ? (directory.ignorePolicy.sessions ? ["sessions"] : []) : localInclude, include);
|
|
292
|
+
return { kind: "different", include, scope, ...comparison, ignorePolicyComparison };
|
|
293
|
+
}
|
|
201
294
|
if (!selection && !directory?.scope)
|
|
202
295
|
return { kind: "legacy", discovered: discoverLegacySnapshotInclude(snapshot) };
|
|
203
296
|
const remoteInclude = selection?.include ?? discoverLegacySnapshotInclude(snapshot);
|
|
@@ -227,10 +320,12 @@ export class RemoteSelectionMismatchError extends Error {
|
|
|
227
320
|
setupName;
|
|
228
321
|
localInclude;
|
|
229
322
|
remoteInclude;
|
|
230
|
-
constructor(setupName, localInclude, remoteInclude, configIdentity = JSON.stringify([setupName, normalizeSyncInclude(localInclude)]), localExclude, remoteExclude, localDirectory, remoteDirectory) {
|
|
323
|
+
constructor(setupName, localInclude, remoteInclude, configIdentity = JSON.stringify([setupName, normalizeSyncInclude(localInclude)]), localExclude, remoteExclude, localDirectory, remoteDirectory, ignorePolicyComparison) {
|
|
231
324
|
const local = normalizeSyncInclude(localInclude);
|
|
232
325
|
const remote = normalizeSyncInclude(remoteInclude);
|
|
233
|
-
super(
|
|
326
|
+
super(ignorePolicyComparison
|
|
327
|
+
? formatIgnorePolicyComparison(ignorePolicyComparison)
|
|
328
|
+
: formatRemoteSelectionMismatch(setupName, local, remote, localExclude, remoteExclude, localDirectory, remoteDirectory));
|
|
234
329
|
this.name = "RemoteSelectionMismatchError";
|
|
235
330
|
this.setupName = setupName;
|
|
236
331
|
this.localInclude = local;
|
|
@@ -240,10 +335,14 @@ export class RemoteSelectionMismatchError extends Error {
|
|
|
240
335
|
configIdentity,
|
|
241
336
|
localInclude: [...local],
|
|
242
337
|
remoteInclude: [...remote],
|
|
243
|
-
...(
|
|
244
|
-
|
|
245
|
-
|
|
246
|
-
|
|
338
|
+
...(ignorePolicyComparison
|
|
339
|
+
? { ignorePolicyComparison }
|
|
340
|
+
: {
|
|
341
|
+
...(localExclude === undefined ? {} : { localExclude: normalizeSyncExclude(localExclude) }),
|
|
342
|
+
...(remoteExclude === undefined ? {} : { remoteExclude: normalizeSyncExclude(remoteExclude) }),
|
|
343
|
+
...(localDirectory?.scope ? { localDirectory: directoryRules(localDirectory) } : {}),
|
|
344
|
+
...(remoteDirectory?.scope ? { remoteDirectory: directoryRules(remoteDirectory) } : {}),
|
|
345
|
+
}),
|
|
247
346
|
};
|
|
248
347
|
}
|
|
249
348
|
}
|
|
@@ -251,7 +350,7 @@ function directoryRules(directory) {
|
|
|
251
350
|
return { scope: directory.scope, reinclude: normalizeDirectoryRules(directory.reinclude) };
|
|
252
351
|
}
|
|
253
352
|
export function remoteSelectionMismatch(config, remoteInclude, configIdentity, remoteExclude, remoteDirectory) {
|
|
254
|
-
return new RemoteSelectionMismatchError(config.setupName, config.include, remoteInclude, configIdentity, config.exclude, remoteExclude, config, remoteDirectory);
|
|
353
|
+
return new RemoteSelectionMismatchError(config.setupName, config.include, remoteInclude, configIdentity, config.exclude, remoteExclude, config, remoteDirectory, remoteDirectory?.ignorePolicyComparison);
|
|
255
354
|
}
|
|
256
355
|
export function formatRemoteSelectionMismatch(setupName, localInclude, remoteInclude, localExclude, remoteExclude, localDirectory, remoteDirectory) {
|
|
257
356
|
const comparison = compareSyncInclude(localInclude, remoteInclude);
|
|
@@ -325,10 +424,12 @@ export function customIncludePathsByLower(value) {
|
|
|
325
424
|
return new Map(syncIncludeSelection(value).custom.map((relativePath) => [relativePath.toLowerCase(), relativePath]));
|
|
326
425
|
}
|
|
327
426
|
export function includeFromSelectionConfig(config) {
|
|
427
|
+
if (config.ignorePolicy)
|
|
428
|
+
return config.ignorePolicy.sessions ? ["sessions"] : [];
|
|
328
429
|
if (config.include !== undefined)
|
|
329
430
|
return normalizeSyncInclude(config.include);
|
|
330
431
|
if (config.scope === "agent-directory")
|
|
331
|
-
return [];
|
|
432
|
+
return isIgnoreSnapshotSelection(config) && config.sessions ? ["sessions"] : [];
|
|
332
433
|
return [
|
|
333
434
|
...normalizeSyncFiles(config.syncFiles),
|
|
334
435
|
...normalizeExtraFiles(config.extraFiles),
|
|
@@ -379,11 +480,44 @@ function isProtectedDirectoryPath(relativePath) {
|
|
|
379
480
|
}
|
|
380
481
|
return false;
|
|
381
482
|
}
|
|
483
|
+
export function isIgnoreControlPath(relativePath, reservedNames) {
|
|
484
|
+
const first = (relativePath.split("/", 1)[0] ?? "").toLowerCase();
|
|
485
|
+
return first === ".syncignore" || reservedNames?.has(first) === true;
|
|
486
|
+
}
|
|
487
|
+
function isIgnoreSnapshotSelection(config) {
|
|
488
|
+
return config.version === 4 && config.fingerprint !== undefined;
|
|
489
|
+
}
|
|
490
|
+
function reservedIgnoreSnapshotNames(config) {
|
|
491
|
+
const local = config.ignorePolicy;
|
|
492
|
+
if (!local && !config.reservedIgnoreNames && !isIgnoreSnapshotSelection(config))
|
|
493
|
+
return undefined;
|
|
494
|
+
return new Set([
|
|
495
|
+
".syncignore",
|
|
496
|
+
...(local ? [local.filename, ...local.reservedNames] : []),
|
|
497
|
+
...(config.reservedIgnoreNames ?? []),
|
|
498
|
+
].map((name) => name.toLowerCase()));
|
|
499
|
+
}
|
|
500
|
+
function compileIgnoreControlPath(config) {
|
|
501
|
+
const names = reservedIgnoreSnapshotNames(config);
|
|
502
|
+
return names ? (relativePath) => isIgnoreControlPath(relativePath, names) : undefined;
|
|
503
|
+
}
|
|
382
504
|
function compileDirectoryPolicy(config) {
|
|
505
|
+
const localIgnore = config.ignorePolicy;
|
|
506
|
+
const ignoreSelection = localIgnore !== undefined || isIgnoreSnapshotSelection(config);
|
|
507
|
+
const controlPath = compileIgnoreControlPath(config);
|
|
508
|
+
const sessions = includeFromSelectionConfig(config).includes("sessions");
|
|
509
|
+
const blocked = (relativePath) => isProtectedDirectoryPath(relativePath) ||
|
|
510
|
+
(ignoreSelection && !relativePath.isWellFormed()) ||
|
|
511
|
+
controlPath?.(relativePath) === true ||
|
|
512
|
+
(!sessions && /^sessions(?:\/|$)/iu.test(relativePath));
|
|
513
|
+
if (localIgnore) {
|
|
514
|
+
return {
|
|
515
|
+
excludes: (relativePath, directory = false) => blocked(relativePath) || localIgnore.matches(relativePath, directory),
|
|
516
|
+
descends: (relativePath) => !blocked(relativePath) && !localIgnore.matches(relativePath, true),
|
|
517
|
+
};
|
|
518
|
+
}
|
|
383
519
|
const excluded = compilePathRules(normalizeDirectoryRules(config.exclude));
|
|
384
520
|
const restored = compilePathRules(normalizeDirectoryRules(config.reinclude));
|
|
385
|
-
const sessions = includeFromSelectionConfig(config).includes("sessions");
|
|
386
|
-
const blocked = (relativePath) => isProtectedDirectoryPath(relativePath) || (!sessions && /^sessions(?:\/|$)/iu.test(relativePath));
|
|
387
521
|
return {
|
|
388
522
|
excludes: (relativePath) => blocked(relativePath) || (excluded.matches(relativePath) && !restored.matches(relativePath)),
|
|
389
523
|
descends: (relativePath) => !blocked(relativePath) &&
|
|
@@ -415,14 +549,16 @@ export function assertAgentDirectoryPathLayout(files) {
|
|
|
415
549
|
}
|
|
416
550
|
}
|
|
417
551
|
export function compileSyncPolicyExclude(config) {
|
|
418
|
-
|
|
419
|
-
|
|
420
|
-
|
|
552
|
+
if (config.scope === "agent-directory")
|
|
553
|
+
return compileDirectoryPolicy(config).excludes;
|
|
554
|
+
const excludes = compileSyncExclude(config.exclude);
|
|
555
|
+
const controlPath = compileIgnoreControlPath(config);
|
|
556
|
+
return controlPath ? (relativePath) => controlPath(toPosix(relativePath)) || excludes(relativePath) : excludes;
|
|
421
557
|
}
|
|
422
558
|
export function compileSyncPolicyTraversal(config) {
|
|
423
559
|
if (config.scope === "agent-directory")
|
|
424
560
|
return compileDirectoryPolicy(config).descends;
|
|
425
|
-
const excludes =
|
|
561
|
+
const excludes = compileSyncPolicyExclude(config);
|
|
426
562
|
return (relativePath) => !excludes(relativePath);
|
|
427
563
|
}
|
|
428
564
|
export function syncSessionsSelected(config) {
|
|
@@ -439,7 +575,7 @@ export function configuredSnapshotPathMatcher(config) {
|
|
|
439
575
|
(!relativePath.startsWith("sessions/") || relativePath.endsWith(".jsonl"));
|
|
440
576
|
}
|
|
441
577
|
const selection = syncIncludeSelection(includeFromSelectionConfig(config));
|
|
442
|
-
const excluded =
|
|
578
|
+
const excluded = compileSyncPolicyExclude(config);
|
|
443
579
|
return (relativePath) => {
|
|
444
580
|
const normalized = toPosix(relativePath);
|
|
445
581
|
if (excluded(normalized))
|
|
@@ -19,6 +19,7 @@ function contentLines(config) {
|
|
|
19
19
|
config.scope === "agent-directory"
|
|
20
20
|
? "included content: eligible files in the Pi agent directory"
|
|
21
21
|
: `included content: ${config.include.map(safeTerminalText).join(", ") || "none"}`,
|
|
22
|
+
...(config.ignorePolicy ? ["path policy: local ignore file (rule text is not published)"] : []),
|
|
22
23
|
...(config.exclude === undefined
|
|
23
24
|
? []
|
|
24
25
|
: [`excluded paths: ${config.exclude.map(safeTerminalText).join(", ") || "none"}`]),
|
|
@@ -80,11 +81,16 @@ export async function diff(ctx, options, factory = createSyncBackend) {
|
|
|
80
81
|
...warnings,
|
|
81
82
|
].join("\n");
|
|
82
83
|
const level = warnings.length > 0 || selectionState?.kind === "different" ? "warning" : "info";
|
|
84
|
+
if (selectionState?.ignorePolicyComparison === "local-legacy-remote-ignore") {
|
|
85
|
+
ctx.ui.notify(`${header}\n\nFile comparison requires a configured local ignore-file policy.`, level);
|
|
86
|
+
return;
|
|
87
|
+
}
|
|
83
88
|
if (!remote) {
|
|
84
89
|
ctx.ui.notify(`${header}\n\n${formatSnapshotOnlyDiff("Remote is empty. Local push would upload", local)}`, level);
|
|
85
90
|
return;
|
|
86
91
|
}
|
|
87
|
-
|
|
92
|
+
const projection = config.ignorePolicy ? "Remote paths shown under this device's ignore rules.\n\n" : "";
|
|
93
|
+
ctx.ui.notify(`${header}\n\n${projection}${formatDiff(local, portableSnapshot(remote, remote.localFields))}`, level);
|
|
88
94
|
}
|
|
89
95
|
export async function doctor(ctx, options, factory = createSyncBackend) {
|
|
90
96
|
const messages = [];
|
|
@@ -195,6 +201,7 @@ export async function history(ctx, options, factory = createSyncBackend) {
|
|
|
195
201
|
await withLock("rollback", () => rollback(ctx, { ...options, args: [snapshot.snapshotRef], yes: false }, factory, {
|
|
196
202
|
backendIdentity: backend.identity,
|
|
197
203
|
setup: config.setupName,
|
|
204
|
+
snapshotId: snapshot.snapshotId,
|
|
198
205
|
}));
|
|
199
206
|
return;
|
|
200
207
|
}
|
|
@@ -4,6 +4,8 @@ import type { SyncState } from "../state/state-types.js";
|
|
|
4
4
|
import { type SyncSelectionConfig } from "./sync-policy.js";
|
|
5
5
|
type SyncPolicyConfig = SyncSelectionConfig & {
|
|
6
6
|
localFields?: string[];
|
|
7
|
+
setupName?: string;
|
|
8
|
+
ignoreSourceIdentity?: string;
|
|
7
9
|
};
|
|
8
10
|
export declare function hasLocalChanges(local: Snapshot, state: SyncState, config: SyncPolicyConfig): boolean;
|
|
9
11
|
export declare function remoteChangedSinceState(head: RemoteHead | undefined, state: SyncState, config: SyncPolicyConfig, sameRevision: (left: string, right: string) => boolean): boolean;
|
|
@@ -16,13 +18,13 @@ export declare function snapshotHashesMatchState(snapshot: Snapshot, state: Sync
|
|
|
16
18
|
export declare function snapshotsMatch(left: Snapshot, right: Snapshot): boolean;
|
|
17
19
|
export declare function syncPolicyChanged(state: SyncState, config: SyncPolicyConfig): boolean;
|
|
18
20
|
export declare function shouldRefreshSyncedState(remote: Snapshot, head: RemoteHead | undefined, state: SyncState, config: SyncPolicyConfig, sameRevision: (left: string, right: string) => boolean): boolean;
|
|
19
|
-
export declare function acceptedSelection(config: {
|
|
21
|
+
export declare function acceptedSelection(config: SyncPolicyConfig & {
|
|
20
22
|
include: string[];
|
|
21
23
|
exclude?: string[];
|
|
22
|
-
scope?: "agent-directory";
|
|
23
24
|
reinclude?: string[];
|
|
24
25
|
}): {
|
|
25
26
|
include: string[];
|
|
27
|
+
ignorePolicyKey?: string | undefined;
|
|
26
28
|
exclude?: string[] | undefined;
|
|
27
29
|
scope?: "agent-directory" | undefined;
|
|
28
30
|
reinclude?: string[] | undefined;
|
|
@@ -2,7 +2,7 @@ import { toPosix } from "../paths.js";
|
|
|
2
2
|
import { canonicalSnapshotPathForConfig, filterSnapshotForConfigPolicy, isSessionPath } from "../snapshot/snapshot.js";
|
|
3
3
|
import { sameSyncExclude } from "./exclude-policy.js";
|
|
4
4
|
import { portableSnapshot, sameLocalFields } from "./local-fields.js";
|
|
5
|
-
import { configuredSnapshotPathMatcher, customIncludePathsByLower, includeFromSelectionConfig, normalizeSyncInclude, syncDirectoryPolicyKey, syncSessionsSelected, } from "./sync-policy.js";
|
|
5
|
+
import { compareIgnorePolicy, configuredSnapshotPathMatcher, customIncludePathsByLower, includeFromSelectionConfig, localIgnorePolicyKey, normalizeSyncInclude, syncDirectoryPolicyKey, syncSessionsSelected, } from "./sync-policy.js";
|
|
6
6
|
export function hasLocalChanges(local, state, config) {
|
|
7
7
|
return ((Boolean(state.lastAppliedSnapshot) &&
|
|
8
8
|
(state.scope !== config.scope ||
|
|
@@ -13,6 +13,11 @@ export function hasLocalChanges(local, state, config) {
|
|
|
13
13
|
export function remoteChangedSinceState(head, state, config, sameRevision) {
|
|
14
14
|
if (!head)
|
|
15
15
|
return Boolean(state.lastAppliedSnapshot);
|
|
16
|
+
if (head.selection) {
|
|
17
|
+
const comparison = compareIgnorePolicy(config.ignorePolicy, head.selection);
|
|
18
|
+
if (comparison && comparison !== "match")
|
|
19
|
+
return true;
|
|
20
|
+
}
|
|
16
21
|
if (head.snapshotId !== state.lastAppliedSnapshot)
|
|
17
22
|
return true;
|
|
18
23
|
if (state.lastRemoteRevision && !sameRevision(head.revision, state.lastRemoteRevision))
|
|
@@ -24,6 +29,9 @@ export function remoteChangedSinceState(head, state, config, sameRevision) {
|
|
|
24
29
|
head.syncSessions);
|
|
25
30
|
}
|
|
26
31
|
export function hasRemoteChanges(remote, state, config, ignoredPaths = new Set()) {
|
|
32
|
+
const comparison = compareIgnorePolicy(config.ignorePolicy, remote.selection);
|
|
33
|
+
if (comparison && comparison !== "match")
|
|
34
|
+
return true;
|
|
27
35
|
if (remote.id === state.lastAppliedSnapshot && !syncPolicyChanged(state, config))
|
|
28
36
|
return false;
|
|
29
37
|
return !snapshotHashesMatchState(portableSnapshot(filterSnapshotForConfigPolicy(remote, config), remote.localFields), state, config, ignoredPaths);
|
|
@@ -71,14 +79,19 @@ export function shouldRefreshSyncedState(remote, head, state, config, sameRevisi
|
|
|
71
79
|
syncPolicyChanged(state, config));
|
|
72
80
|
}
|
|
73
81
|
export function acceptedSelection(config) {
|
|
82
|
+
const key = localIgnorePolicyKey(config);
|
|
74
83
|
return {
|
|
75
|
-
include: [...config.include],
|
|
84
|
+
include: config.ignorePolicy ? includeFromSelectionConfig(config) : [...config.include],
|
|
85
|
+
...(key === undefined ? {} : { ignorePolicyKey: key }),
|
|
76
86
|
...(config.exclude === undefined ? {} : { exclude: [...config.exclude] }),
|
|
77
87
|
...(config.scope === undefined ? {} : { scope: config.scope }),
|
|
78
88
|
...(config.reinclude === undefined ? {} : { reinclude: [...config.reinclude] }),
|
|
79
89
|
};
|
|
80
90
|
}
|
|
81
91
|
function sameSelectionRules(state, config) {
|
|
92
|
+
const key = localIgnorePolicyKey(config);
|
|
93
|
+
if (key !== undefined || state.ignorePolicyKey !== undefined)
|
|
94
|
+
return key === state.ignorePolicyKey;
|
|
82
95
|
return config.scope === "agent-directory" && state.scope === config.scope
|
|
83
96
|
? syncDirectoryPolicyKey(state) === syncDirectoryPolicyKey(config)
|
|
84
97
|
: sameSyncExclude(state.exclude, config.exclude);
|
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
import type { RemoteHead, SyncBackend } from "../backends/sync-backend.js";
|
|
2
|
+
import type { CommandOptions } from "../commands/command-types.js";
|
|
3
|
+
import type { EngineContext } from "../host.js";
|
|
4
|
+
import type { AnySyncConfig } from "../settings/settings-types.js";
|
|
5
|
+
import type { Snapshot, SnapshotOptions } from "../snapshot/snapshot-types.js";
|
|
6
|
+
import type { SyncState } from "../state/state-types.js";
|
|
7
|
+
export declare function requireResolvedDirectionalProgress(state: SyncState, options: CommandOptions, allowForce?: boolean): void;
|
|
8
|
+
export declare function requireIgnoreBaselineReview(ctx: EngineContext, config: AnySyncConfig, state: SyncState, options: CommandOptions): void;
|
|
9
|
+
export declare function validateDirectionalPublication(ctx: EngineContext, config: AnySyncConfig, options: SnapshotOptions, state: SyncState, expectedHashes: Record<string, string>, validatePolicy: () => Promise<void>, direction: "push" | "rollback"): Promise<void>;
|
|
10
|
+
export declare function checkLiveProtection(ctx: EngineContext, sessionDir: string | undefined, reviewed: ReadonlySet<string>): void;
|
|
11
|
+
export declare function directionalApplyGuards({ ctx, config, backend, state, sessionDir, head, protectedPaths, signal, validateMutation, validateIgnorePolicy, onCommit, }: {
|
|
12
|
+
ctx: EngineContext;
|
|
13
|
+
config: AnySyncConfig;
|
|
14
|
+
backend: SyncBackend;
|
|
15
|
+
state: SyncState;
|
|
16
|
+
sessionDir?: string;
|
|
17
|
+
head?: RemoteHead;
|
|
18
|
+
protectedPaths: ReadonlySet<string>;
|
|
19
|
+
signal?: AbortSignal;
|
|
20
|
+
validateMutation: () => void;
|
|
21
|
+
validateIgnorePolicy?: () => Promise<void>;
|
|
22
|
+
onCommit?: () => void;
|
|
23
|
+
}): {
|
|
24
|
+
validateMutation: () => void;
|
|
25
|
+
validateApplyReview?: undefined;
|
|
26
|
+
} | {
|
|
27
|
+
validateMutation: () => void;
|
|
28
|
+
validateApplyReview: () => Promise<void>;
|
|
29
|
+
};
|
|
30
|
+
export declare function validateMergeApplyReview(backend: SyncBackend, config: AnySyncConfig, head: RemoteHead, stateIdentity: string, validateFresh: () => Promise<void>, signal?: AbortSignal): Promise<void>;
|
|
31
|
+
export declare function validateMergedPublication(ctx: EngineContext, config: AnySyncConfig, options: SnapshotOptions, before: Snapshot, after: Snapshot, stateIdentity: string, validateFresh: () => Promise<void>): Promise<void>;
|
|
@@ -0,0 +1,82 @@
|
|
|
1
|
+
import { resolvedSnapshotOptionsForContext } from "../snapshot/session-paths.js";
|
|
2
|
+
import { createSnapshot } from "../snapshot/snapshot.js";
|
|
3
|
+
import { StaleIgnorePolicyError } from "../snapshot/snapshot-transaction-policy.js";
|
|
4
|
+
import { readStateForConfig, syncStateFingerprint } from "../state/sync-state-store.js";
|
|
5
|
+
import { ContinuousError } from "./continuous-errors.js";
|
|
6
|
+
import { preflightMergedTargets } from "./merge-apply.js";
|
|
7
|
+
import { sameRemoteHead } from "./remote-snapshot.js";
|
|
8
|
+
import { protectedSessionPaths } from "./sync-local.js";
|
|
9
|
+
import { fileHashMap, sameHashes, syncPolicyChanged } from "./sync-state.js";
|
|
10
|
+
export function requireResolvedDirectionalProgress(state, options, allowForce = true) {
|
|
11
|
+
if (state.unresolved?.length && (options.auto || !options.force || !allowForce))
|
|
12
|
+
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.");
|
|
13
|
+
}
|
|
14
|
+
export function requireIgnoreBaselineReview(ctx, config, state, options) {
|
|
15
|
+
if ((config.ignorePolicy || config.reservedIgnoreNames !== undefined || state.ignorePolicyKey !== undefined) &&
|
|
16
|
+
state.lastAppliedSnapshot &&
|
|
17
|
+
syncPolicyChanged(state, config) &&
|
|
18
|
+
(options.auto || options.yes || !ctx.hasUI)) {
|
|
19
|
+
throw new Error("Local ignore-file policy or source changed since the last accepted baseline. Review a foreground directional transfer.");
|
|
20
|
+
}
|
|
21
|
+
}
|
|
22
|
+
export async function validateDirectionalPublication(ctx, config, options, state, expectedHashes, validatePolicy, direction) {
|
|
23
|
+
const current = await createSnapshot(config.snapshotIdentity, options);
|
|
24
|
+
if (!sameHashes(fileHashMap(current), expectedHashes))
|
|
25
|
+
throw new Error(`Local bytes changed at the ${direction} publication boundary.`);
|
|
26
|
+
if (syncStateFingerprint(await readStateForConfig(config)) !== syncStateFingerprint(state))
|
|
27
|
+
throw new Error(`Baseline changed at the ${direction} publication boundary.`);
|
|
28
|
+
if ((config.scope === "agent-directory" || direction === "push") &&
|
|
29
|
+
options.sessionDir !== (await resolvedSnapshotOptionsForContext(ctx, config, options.signal)).sessionDir)
|
|
30
|
+
throw new Error(`Pi session storage changed at the ${direction} publication boundary.`);
|
|
31
|
+
await validatePolicy();
|
|
32
|
+
}
|
|
33
|
+
export function checkLiveProtection(ctx, sessionDir, reviewed) {
|
|
34
|
+
const current = protectedSessionPaths(ctx, sessionDir);
|
|
35
|
+
if (current.size !== reviewed.size || [...current].some((item) => !reviewed.has(item)))
|
|
36
|
+
throw new StaleIgnorePolicyError("Live session protection changed during review; reopen the review.");
|
|
37
|
+
}
|
|
38
|
+
export function directionalApplyGuards({ ctx, config, backend, state, sessionDir, head, protectedPaths, signal, validateMutation, validateIgnorePolicy, onCommit, }) {
|
|
39
|
+
if (!validateIgnorePolicy)
|
|
40
|
+
return { validateMutation };
|
|
41
|
+
const validateOwner = () => {
|
|
42
|
+
validateMutation();
|
|
43
|
+
checkLiveProtection(ctx, sessionDir, protectedPaths);
|
|
44
|
+
};
|
|
45
|
+
let committed = false;
|
|
46
|
+
return {
|
|
47
|
+
validateMutation: validateOwner,
|
|
48
|
+
validateApplyReview: async () => {
|
|
49
|
+
if (!sameRemoteHead(backend, head, await backend.readHead(signal)))
|
|
50
|
+
throw new StaleIgnorePolicyError("Remote changed before local apply; reopen the review.");
|
|
51
|
+
if (syncStateFingerprint(await readStateForConfig(config)) !== syncStateFingerprint(state))
|
|
52
|
+
throw new StaleIgnorePolicyError("Baseline changed before local apply; reopen the review.");
|
|
53
|
+
if (sessionDir !== (await resolvedSnapshotOptionsForContext(ctx, config, signal)).sessionDir)
|
|
54
|
+
throw new StaleIgnorePolicyError("Pi session storage changed before local apply; reopen the review.");
|
|
55
|
+
await validateIgnorePolicy();
|
|
56
|
+
validateOwner();
|
|
57
|
+
if (!committed) {
|
|
58
|
+
onCommit?.();
|
|
59
|
+
committed = true;
|
|
60
|
+
}
|
|
61
|
+
},
|
|
62
|
+
};
|
|
63
|
+
}
|
|
64
|
+
export async function validateMergeApplyReview(backend, config, head, stateIdentity, validateFresh, signal) {
|
|
65
|
+
if (!sameRemoteHead(backend, head, await backend.readHead(signal)))
|
|
66
|
+
throw new Error("Remote head changed before merged apply; journal and backup retained.");
|
|
67
|
+
if (syncStateFingerprint(await readStateForConfig(config)) !== stateIdentity)
|
|
68
|
+
throw new Error("Sync baseline changed before merged apply; journal and backup retained.");
|
|
69
|
+
await validateFresh();
|
|
70
|
+
}
|
|
71
|
+
export async function validateMergedPublication(ctx, config, options, before, after, stateIdentity, validateFresh) {
|
|
72
|
+
await validateFresh();
|
|
73
|
+
const current = await createSnapshot(config.snapshotIdentity, options);
|
|
74
|
+
const state = await readStateForConfig(config);
|
|
75
|
+
if (!sameHashes(fileHashMap(current), fileHashMap(before)) || syncStateFingerprint(state) !== stateIdentity)
|
|
76
|
+
throw new ContinuousError("stale", "Local content or baseline changed at publication; journal and backup retained.");
|
|
77
|
+
await preflightMergedTargets(current, after, options, [
|
|
78
|
+
...(ctx.protectedSessionFiles ?? []),
|
|
79
|
+
...[ctx.sessionManager.getSessionFile?.()].filter((file) => Boolean(file)),
|
|
80
|
+
]);
|
|
81
|
+
await validateFresh();
|
|
82
|
+
}
|
package/dist/engine-service.d.ts
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import { type Execute } from "./protocol.js";
|
|
2
2
|
/** Engine dispatch is loaded only by the service, never by the public IPC client. */
|
|
3
|
-
export declare function createEngineService(root: string): {
|
|
3
|
+
export declare function createEngineService(root: string, ignoreFileSettingsAvailable?: boolean): {
|
|
4
4
|
execute: Execute;
|
|
5
5
|
bind: (value: Awaited<ReturnType<typeof import("./server.js").startServer>>) => void;
|
|
6
6
|
hooks: {
|