@narumitw/pi-sync-daemon 0.7.0 → 0.8.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (74) hide show
  1. package/CHANGELOG.md +11 -0
  2. package/README.md +16 -2
  3. package/dist/consent.js +21 -6
  4. package/dist/engine/backends/git/git-backend.d.ts +1 -1
  5. package/dist/engine/backends/git/git-backend.js +17 -2
  6. package/dist/engine/backends/git/git-storage.d.ts +1 -1
  7. package/dist/engine/backends/git/git-storage.js +30 -9
  8. package/dist/engine/backends/s3/s3-backend.js +8 -0
  9. package/dist/engine/backends/sync-backend.d.ts +1 -0
  10. package/dist/engine/backends/webdav/webdav-backend.js +8 -0
  11. package/dist/engine/ignore/effective-ignore-policy.d.ts +10 -0
  12. package/dist/engine/ignore/effective-ignore-policy.js +33 -0
  13. package/dist/engine/ignore/ignore-edit.d.ts +13 -0
  14. package/dist/engine/ignore/ignore-edit.js +102 -0
  15. package/dist/engine/ignore/ignore-initialization.d.ts +6 -0
  16. package/dist/engine/ignore/ignore-initialization.js +186 -0
  17. package/dist/engine/ignore/ignore-migration-prompt.d.ts +7 -0
  18. package/dist/engine/ignore/ignore-migration-prompt.js +139 -0
  19. package/dist/engine/ignore/ignore-settings.d.ts +15 -0
  20. package/dist/engine/ignore/ignore-settings.js +322 -0
  21. package/dist/engine/ignore/ignore-source.d.ts +27 -0
  22. package/dist/engine/ignore/ignore-source.js +250 -0
  23. package/dist/engine/settings/config-file.d.ts +9 -2
  24. package/dist/engine/settings/config-file.js +124 -28
  25. package/dist/engine/settings/config.d.ts +3 -0
  26. package/dist/engine/settings/config.js +93 -7
  27. package/dist/engine/settings/settings-management.d.ts +2 -2
  28. package/dist/engine/settings/settings-management.js +16 -8
  29. package/dist/engine/settings/settings-types.d.ts +23 -2
  30. package/dist/engine/settings/settings-validation.js +23 -5
  31. package/dist/engine/snapshot/session-paths.d.ts +1 -1
  32. package/dist/engine/snapshot/session-paths.js +36 -13
  33. package/dist/engine/snapshot/snapshot-apply.d.ts +3 -3
  34. package/dist/engine/snapshot/snapshot-apply.js +23 -4
  35. package/dist/engine/snapshot/snapshot-codec.d.ts +1 -1
  36. package/dist/engine/snapshot/snapshot-codec.js +14 -5
  37. package/dist/engine/snapshot/snapshot-exclude-safety.d.ts +4 -2
  38. package/dist/engine/snapshot/snapshot-exclude-safety.js +76 -19
  39. package/dist/engine/snapshot/snapshot-transaction-images.d.ts +3 -1
  40. package/dist/engine/snapshot/snapshot-transaction-images.js +33 -3
  41. package/dist/engine/snapshot/snapshot-transaction-policy.d.ts +24 -1
  42. package/dist/engine/snapshot/snapshot-transaction-policy.js +106 -2
  43. package/dist/engine/snapshot/snapshot-transaction-verification.d.ts +7 -0
  44. package/dist/engine/snapshot/snapshot-transaction-verification.js +78 -0
  45. package/dist/engine/snapshot/snapshot-transaction.d.ts +1 -12
  46. package/dist/engine/snapshot/snapshot-transaction.js +86 -108
  47. package/dist/engine/snapshot/snapshot-types.d.ts +23 -1
  48. package/dist/engine/snapshot/snapshot-types.js +9 -1
  49. package/dist/engine/snapshot/snapshot.d.ts +6 -4
  50. package/dist/engine/snapshot/snapshot.js +91 -34
  51. package/dist/engine/state/state-types.d.ts +1 -0
  52. package/dist/engine/sync/continuous-sync.js +18 -3
  53. package/dist/engine/sync/continuous-watch.js +4 -1
  54. package/dist/engine/sync/initialization.js +3 -2
  55. package/dist/engine/sync/local-fields.d.ts +3 -1
  56. package/dist/engine/sync/local-fields.js +62 -1
  57. package/dist/engine/sync/merge-apply.js +35 -7
  58. package/dist/engine/sync/merge-journal.d.ts +18 -4
  59. package/dist/engine/sync/merge-journal.js +115 -8
  60. package/dist/engine/sync/merged-sync.d.ts +1 -1
  61. package/dist/engine/sync/merged-sync.js +103 -50
  62. package/dist/engine/sync/remote-snapshot.d.ts +9 -2
  63. package/dist/engine/sync/remote-snapshot.js +56 -17
  64. package/dist/engine/sync/sync-inspection.js +7 -5
  65. package/dist/engine/sync/sync-mutations.d.ts +2 -1
  66. package/dist/engine/sync/sync-mutations.js +110 -47
  67. package/dist/engine/sync/sync-policy.d.ts +24 -8
  68. package/dist/engine/sync/sync-policy.js +127 -16
  69. package/dist/engine/sync/sync-queries.js +8 -1
  70. package/dist/engine/sync/sync-state.d.ts +2 -2
  71. package/dist/engine/sync/sync-state.js +20 -2
  72. package/dist/engine-service.js +24 -2
  73. package/docs/architecture.md +22 -0
  74. package/package.json +2 -1
@@ -1,5 +1,6 @@
1
1
  import path from "node:path";
2
2
  import { isDeniedPath, toPosix } from "../paths.js";
3
+ import { IGNORE_SNAPSHOT_VERSION, } from "../snapshot/snapshot-types.js";
3
4
  import { compilePathRules, compileSyncExclude, normalizeSyncExclude, sameSyncExclude } from "./exclude-policy.js";
4
5
  export const BUILT_IN_SYNC_ROOTS = [
5
6
  "settings.json",
@@ -108,6 +109,22 @@ export function normalizeDirectoryRules(value) {
108
109
  }
109
110
  export function portableSnapshotSelection(value) {
110
111
  const selection = value;
112
+ if (selection && typeof selection === "object" && !Array.isArray(selection) && selection.version === 4) {
113
+ if (selection.scope !== "agent-directory" ||
114
+ typeof selection.sessions !== "boolean" ||
115
+ typeof selection.fingerprint !== "string" ||
116
+ selection.fingerprint.length !== 64 ||
117
+ !/^[0-9a-f]{64}$/u.test(selection.fingerprint) ||
118
+ Object.keys(selection).length !== 4 ||
119
+ !["version", "scope", "sessions", "fingerprint"].every((key) => Object.hasOwn(selection, key)))
120
+ throw new Error("Invalid ignore-file snapshot policy.");
121
+ return {
122
+ version: 4,
123
+ scope: "agent-directory",
124
+ fingerprint: selection.fingerprint,
125
+ sessions: selection.sessions,
126
+ };
127
+ }
111
128
  if (selection && typeof selection === "object" && !Array.isArray(selection) && selection.version === 3) {
112
129
  if (selection.scope !== "agent-directory" ||
113
130
  !Array.isArray(selection.exclude) ||
@@ -132,7 +149,10 @@ export function portableSnapshotSelection(value) {
132
149
  return { version: 2, include, exclude: normalizeSyncExclude(selection.exclude) };
133
150
  }
134
151
  export function snapshotSelectionInclude(snapshot) {
135
- return snapshot.selection === undefined ? undefined : portableSnapshotSelection(snapshot.selection).include;
152
+ if (snapshot.selection === undefined)
153
+ return undefined;
154
+ const selection = portableSnapshotSelection(snapshot.selection);
155
+ return selection.version === 4 ? (selection.sessions ? ["sessions"] : []) : selection.include;
136
156
  }
137
157
  export function snapshotSelectionExclude(snapshot) {
138
158
  return snapshot.selection === undefined ? undefined : portableSnapshotSelection(snapshot.selection).exclude;
@@ -150,6 +170,16 @@ function directorySelection(include, exclude, reinclude) {
150
170
  };
151
171
  }
152
172
  export function selectionForSnapshot(include, exclude, directory) {
173
+ if (directory?.ignorePolicy) {
174
+ if (directory.scope !== "agent-directory")
175
+ throw new Error("Ignore rules require agent-directory scope.");
176
+ return portableSnapshotSelection({
177
+ version: 4,
178
+ scope: directory.scope,
179
+ fingerprint: directory.ignorePolicy.fingerprint,
180
+ sessions: directory.ignorePolicy.sessions,
181
+ });
182
+ }
153
183
  if (directory?.scope === "agent-directory")
154
184
  return directorySelection(include, exclude, directory.reinclude);
155
185
  const normalized = normalizeSyncInclude(include);
@@ -168,6 +198,12 @@ export function validateSnapshotSelection(snapshot) {
168
198
  throw new Error("Exclude policy requires snapshot version 4 and selection version 2.");
169
199
  if ((snapshot.version === 5) !== (selection?.version === 3))
170
200
  throw new Error("Agent-directory policy requires snapshot version 5 and selection version 3.");
201
+ if ((snapshot.version === IGNORE_SNAPSHOT_VERSION) !== (selection?.version === 4))
202
+ throw new Error("Ignore-file policy requires snapshot version 6 and selection version 4.");
203
+ }
204
+ export function requireLegacyTransferPolicy(snapshot) {
205
+ if (snapshot.version === IGNORE_SNAPSHOT_VERSION || snapshot.selection?.version === 4)
206
+ throw new Error("This setup cannot transfer an ignore-file snapshot without a reviewed local ignore policy.");
171
207
  }
172
208
  export function sameSyncInclude(left, right) {
173
209
  const normalizedLeft = normalizeSyncInclude(left);
@@ -186,9 +222,39 @@ export function compareSyncInclude(local, remote) {
186
222
  localOnly: localInclude.filter((item) => !remoteSet.has(item)),
187
223
  };
188
224
  }
225
+ export function compareIgnorePolicy(local, remote) {
226
+ if (remote?.version !== 4)
227
+ return local ? "local-ignore-remote-legacy" : undefined;
228
+ if (!local)
229
+ return "local-legacy-remote-ignore";
230
+ return local.fingerprint === remote.fingerprint && local.sessions === remote.sessions ? "match" : "mismatch";
231
+ }
232
+ export function formatIgnorePolicyComparison(comparison) {
233
+ switch (comparison) {
234
+ case "match":
235
+ return "Remote ignore-file policy matches this device.";
236
+ case "mismatch":
237
+ return "Remote ignore-file policy differs. Edit local rules to match, or review publishing this device's policy.";
238
+ case "local-ignore-remote-legacy":
239
+ return "Remote uses a legacy content policy. Review publishing this device's ignore-file policy.";
240
+ case "local-legacy-remote-ignore":
241
+ return "Remote uses an ignore-file policy. Configure matching local rules before transferring.";
242
+ }
243
+ }
189
244
  export function syncDirectoryPolicyKey(config) {
190
245
  if (config.scope !== "agent-directory")
191
246
  return undefined;
247
+ const policy = config.ignorePolicy;
248
+ if (policy) {
249
+ return JSON.stringify([
250
+ "ignore-file",
251
+ policy.fingerprint,
252
+ policy.sessions,
253
+ policy.filename,
254
+ policy.sourceDigest,
255
+ policy.reservedNames,
256
+ ]);
257
+ }
192
258
  return JSON.stringify([
193
259
  config.scope,
194
260
  includeFromSelectionConfig(config).includes("sessions"),
@@ -198,6 +264,15 @@ export function syncDirectoryPolicyKey(config) {
198
264
  }
199
265
  export function inspectRemoteSelection(localInclude, snapshot, localExclude, directory) {
200
266
  const selection = snapshot.selection === undefined ? undefined : portableSnapshotSelection(snapshot.selection);
267
+ const ignorePolicyComparison = compareIgnorePolicy(directory?.ignorePolicy, selection);
268
+ if (ignorePolicyComparison !== undefined) {
269
+ const include = snapshotSelectionInclude(snapshot) ?? discoverLegacySnapshotInclude(snapshot);
270
+ const scope = selection?.scope;
271
+ if (ignorePolicyComparison === "match")
272
+ return { kind: "same", include, scope, ignorePolicyComparison };
273
+ const comparison = compareSyncInclude(directory?.ignorePolicy ? (directory.ignorePolicy.sessions ? ["sessions"] : []) : localInclude, include);
274
+ return { kind: "different", include, scope, ...comparison, ignorePolicyComparison };
275
+ }
201
276
  if (!selection && !directory?.scope)
202
277
  return { kind: "legacy", discovered: discoverLegacySnapshotInclude(snapshot) };
203
278
  const remoteInclude = selection?.include ?? discoverLegacySnapshotInclude(snapshot);
@@ -227,10 +302,12 @@ export class RemoteSelectionMismatchError extends Error {
227
302
  setupName;
228
303
  localInclude;
229
304
  remoteInclude;
230
- constructor(setupName, localInclude, remoteInclude, configIdentity = JSON.stringify([setupName, normalizeSyncInclude(localInclude)]), localExclude, remoteExclude, localDirectory, remoteDirectory) {
305
+ constructor(setupName, localInclude, remoteInclude, configIdentity = JSON.stringify([setupName, normalizeSyncInclude(localInclude)]), localExclude, remoteExclude, localDirectory, remoteDirectory, ignorePolicyComparison) {
231
306
  const local = normalizeSyncInclude(localInclude);
232
307
  const remote = normalizeSyncInclude(remoteInclude);
233
- super(formatRemoteSelectionMismatch(setupName, local, remote, localExclude, remoteExclude, localDirectory, remoteDirectory));
308
+ super(ignorePolicyComparison
309
+ ? formatIgnorePolicyComparison(ignorePolicyComparison)
310
+ : formatRemoteSelectionMismatch(setupName, local, remote, localExclude, remoteExclude, localDirectory, remoteDirectory));
234
311
  this.name = "RemoteSelectionMismatchError";
235
312
  this.setupName = setupName;
236
313
  this.localInclude = local;
@@ -240,10 +317,14 @@ export class RemoteSelectionMismatchError extends Error {
240
317
  configIdentity,
241
318
  localInclude: [...local],
242
319
  remoteInclude: [...remote],
243
- ...(localExclude === undefined ? {} : { localExclude: normalizeSyncExclude(localExclude) }),
244
- ...(remoteExclude === undefined ? {} : { remoteExclude: normalizeSyncExclude(remoteExclude) }),
245
- ...(localDirectory?.scope ? { localDirectory: directoryRules(localDirectory) } : {}),
246
- ...(remoteDirectory?.scope ? { remoteDirectory: directoryRules(remoteDirectory) } : {}),
320
+ ...(ignorePolicyComparison
321
+ ? { ignorePolicyComparison }
322
+ : {
323
+ ...(localExclude === undefined ? {} : { localExclude: normalizeSyncExclude(localExclude) }),
324
+ ...(remoteExclude === undefined ? {} : { remoteExclude: normalizeSyncExclude(remoteExclude) }),
325
+ ...(localDirectory?.scope ? { localDirectory: directoryRules(localDirectory) } : {}),
326
+ ...(remoteDirectory?.scope ? { remoteDirectory: directoryRules(remoteDirectory) } : {}),
327
+ }),
247
328
  };
248
329
  }
249
330
  }
@@ -251,7 +332,7 @@ function directoryRules(directory) {
251
332
  return { scope: directory.scope, reinclude: normalizeDirectoryRules(directory.reinclude) };
252
333
  }
253
334
  export function remoteSelectionMismatch(config, remoteInclude, configIdentity, remoteExclude, remoteDirectory) {
254
- return new RemoteSelectionMismatchError(config.setupName, config.include, remoteInclude, configIdentity, config.exclude, remoteExclude, config, remoteDirectory);
335
+ return new RemoteSelectionMismatchError(config.setupName, config.include, remoteInclude, configIdentity, config.exclude, remoteExclude, config, remoteDirectory, remoteDirectory?.ignorePolicyComparison);
255
336
  }
256
337
  export function formatRemoteSelectionMismatch(setupName, localInclude, remoteInclude, localExclude, remoteExclude, localDirectory, remoteDirectory) {
257
338
  const comparison = compareSyncInclude(localInclude, remoteInclude);
@@ -325,10 +406,12 @@ export function customIncludePathsByLower(value) {
325
406
  return new Map(syncIncludeSelection(value).custom.map((relativePath) => [relativePath.toLowerCase(), relativePath]));
326
407
  }
327
408
  export function includeFromSelectionConfig(config) {
409
+ if (config.ignorePolicy)
410
+ return config.ignorePolicy.sessions ? ["sessions"] : [];
328
411
  if (config.include !== undefined)
329
412
  return normalizeSyncInclude(config.include);
330
413
  if (config.scope === "agent-directory")
331
- return [];
414
+ return isIgnoreSnapshotSelection(config) && config.sessions ? ["sessions"] : [];
332
415
  return [
333
416
  ...normalizeSyncFiles(config.syncFiles),
334
417
  ...normalizeExtraFiles(config.extraFiles),
@@ -379,11 +462,37 @@ function isProtectedDirectoryPath(relativePath) {
379
462
  }
380
463
  return false;
381
464
  }
465
+ export function isIgnoreControlPath(relativePath, reservedNames) {
466
+ const first = (relativePath.split("/", 1)[0] ?? "").toLowerCase();
467
+ return first === ".syncignore" || reservedNames?.has(first) === true;
468
+ }
469
+ function isIgnoreSnapshotSelection(config) {
470
+ return config.version === 4 && config.fingerprint !== undefined;
471
+ }
472
+ function compileIgnoreControlPath(config) {
473
+ const local = config.ignorePolicy;
474
+ if (!local && !config.reservedIgnoreNames && !isIgnoreSnapshotSelection(config))
475
+ return undefined;
476
+ const names = new Set([...(local ? [local.filename, ...local.reservedNames] : []), ...(config.reservedIgnoreNames ?? [])].map((name) => name.toLowerCase()));
477
+ return (relativePath) => isIgnoreControlPath(relativePath, names);
478
+ }
382
479
  function compileDirectoryPolicy(config) {
480
+ const localIgnore = config.ignorePolicy;
481
+ const ignoreSelection = localIgnore !== undefined || isIgnoreSnapshotSelection(config);
482
+ const controlPath = compileIgnoreControlPath(config);
483
+ const sessions = includeFromSelectionConfig(config).includes("sessions");
484
+ const blocked = (relativePath) => isProtectedDirectoryPath(relativePath) ||
485
+ (ignoreSelection && !relativePath.isWellFormed()) ||
486
+ controlPath?.(relativePath) === true ||
487
+ (!sessions && /^sessions(?:\/|$)/iu.test(relativePath));
488
+ if (localIgnore) {
489
+ return {
490
+ excludes: (relativePath, directory = false) => blocked(relativePath) || localIgnore.matches(relativePath, directory),
491
+ descends: (relativePath) => !blocked(relativePath) && !localIgnore.matches(relativePath, true),
492
+ };
493
+ }
383
494
  const excluded = compilePathRules(normalizeDirectoryRules(config.exclude));
384
495
  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
496
  return {
388
497
  excludes: (relativePath) => blocked(relativePath) || (excluded.matches(relativePath) && !restored.matches(relativePath)),
389
498
  descends: (relativePath) => !blocked(relativePath) &&
@@ -415,14 +524,16 @@ export function assertAgentDirectoryPathLayout(files) {
415
524
  }
416
525
  }
417
526
  export function compileSyncPolicyExclude(config) {
418
- return config.scope === "agent-directory"
419
- ? compileDirectoryPolicy(config).excludes
420
- : compileSyncExclude(config.exclude);
527
+ if (config.scope === "agent-directory")
528
+ return compileDirectoryPolicy(config).excludes;
529
+ const excludes = compileSyncExclude(config.exclude);
530
+ const controlPath = compileIgnoreControlPath(config);
531
+ return controlPath ? (relativePath) => controlPath(toPosix(relativePath)) || excludes(relativePath) : excludes;
421
532
  }
422
533
  export function compileSyncPolicyTraversal(config) {
423
534
  if (config.scope === "agent-directory")
424
535
  return compileDirectoryPolicy(config).descends;
425
- const excludes = compileSyncExclude(config.exclude);
536
+ const excludes = compileSyncPolicyExclude(config);
426
537
  return (relativePath) => !excludes(relativePath);
427
538
  }
428
539
  export function syncSessionsSelected(config) {
@@ -439,7 +550,7 @@ export function configuredSnapshotPathMatcher(config) {
439
550
  (!relativePath.startsWith("sessions/") || relativePath.endsWith(".jsonl"));
440
551
  }
441
552
  const selection = syncIncludeSelection(includeFromSelectionConfig(config));
442
- const excluded = compileSyncExclude(config.exclude);
553
+ const excluded = compileSyncPolicyExclude(config);
443
554
  return (relativePath) => {
444
555
  const normalized = toPosix(relativePath);
445
556
  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
- ctx.ui.notify(`${header}\n\n${formatDiff(local, portableSnapshot(remote, remote.localFields))}`, level);
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
  }
@@ -16,13 +16,13 @@ export declare function snapshotHashesMatchState(snapshot: Snapshot, state: Sync
16
16
  export declare function snapshotsMatch(left: Snapshot, right: Snapshot): boolean;
17
17
  export declare function syncPolicyChanged(state: SyncState, config: SyncPolicyConfig): boolean;
18
18
  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: {
19
+ export declare function acceptedSelection(config: SyncPolicyConfig & {
20
20
  include: string[];
21
21
  exclude?: string[];
22
- scope?: "agent-directory";
23
22
  reinclude?: string[];
24
23
  }): {
25
24
  include: string[];
25
+ ignorePolicyKey?: string | undefined;
26
26
  exclude?: string[] | undefined;
27
27
  scope?: "agent-directory" | undefined;
28
28
  reinclude?: string[] | undefined;
@@ -1,8 +1,13 @@
1
+ import { createHash } from "node:crypto";
1
2
  import { toPosix } from "../paths.js";
2
3
  import { canonicalSnapshotPathForConfig, filterSnapshotForConfigPolicy, isSessionPath } from "../snapshot/snapshot.js";
3
4
  import { sameSyncExclude } from "./exclude-policy.js";
4
5
  import { portableSnapshot, sameLocalFields } from "./local-fields.js";
5
- import { configuredSnapshotPathMatcher, customIncludePathsByLower, includeFromSelectionConfig, normalizeSyncInclude, syncDirectoryPolicyKey, syncSessionsSelected, } from "./sync-policy.js";
6
+ import { compareIgnorePolicy, configuredSnapshotPathMatcher, customIncludePathsByLower, includeFromSelectionConfig, normalizeSyncInclude, syncDirectoryPolicyKey, syncSessionsSelected, } from "./sync-policy.js";
7
+ function ignorePolicyKey(config) {
8
+ const key = config.ignorePolicy && syncDirectoryPolicyKey(config);
9
+ return key === undefined ? undefined : createHash("sha256").update(key).digest("hex");
10
+ }
6
11
  export function hasLocalChanges(local, state, config) {
7
12
  return ((Boolean(state.lastAppliedSnapshot) &&
8
13
  (state.scope !== config.scope ||
@@ -13,6 +18,11 @@ export function hasLocalChanges(local, state, config) {
13
18
  export function remoteChangedSinceState(head, state, config, sameRevision) {
14
19
  if (!head)
15
20
  return Boolean(state.lastAppliedSnapshot);
21
+ if (head.selection) {
22
+ const comparison = compareIgnorePolicy(config.ignorePolicy, head.selection);
23
+ if (comparison && comparison !== "match")
24
+ return true;
25
+ }
16
26
  if (head.snapshotId !== state.lastAppliedSnapshot)
17
27
  return true;
18
28
  if (state.lastRemoteRevision && !sameRevision(head.revision, state.lastRemoteRevision))
@@ -24,6 +34,9 @@ export function remoteChangedSinceState(head, state, config, sameRevision) {
24
34
  head.syncSessions);
25
35
  }
26
36
  export function hasRemoteChanges(remote, state, config, ignoredPaths = new Set()) {
37
+ const comparison = compareIgnorePolicy(config.ignorePolicy, remote.selection);
38
+ if (comparison && comparison !== "match")
39
+ return true;
27
40
  if (remote.id === state.lastAppliedSnapshot && !syncPolicyChanged(state, config))
28
41
  return false;
29
42
  return !snapshotHashesMatchState(portableSnapshot(filterSnapshotForConfigPolicy(remote, config), remote.localFields), state, config, ignoredPaths);
@@ -71,14 +84,19 @@ export function shouldRefreshSyncedState(remote, head, state, config, sameRevisi
71
84
  syncPolicyChanged(state, config));
72
85
  }
73
86
  export function acceptedSelection(config) {
87
+ const key = ignorePolicyKey(config);
74
88
  return {
75
- include: [...config.include],
89
+ include: config.ignorePolicy ? includeFromSelectionConfig(config) : [...config.include],
90
+ ...(key === undefined ? {} : { ignorePolicyKey: key }),
76
91
  ...(config.exclude === undefined ? {} : { exclude: [...config.exclude] }),
77
92
  ...(config.scope === undefined ? {} : { scope: config.scope }),
78
93
  ...(config.reinclude === undefined ? {} : { reinclude: [...config.reinclude] }),
79
94
  };
80
95
  }
81
96
  function sameSelectionRules(state, config) {
97
+ const key = ignorePolicyKey(config);
98
+ if (key !== undefined || state.ignorePolicyKey !== undefined)
99
+ return key === state.ignorePolicyKey;
82
100
  return config.scope === "agent-directory" && state.scope === config.scope
83
101
  ? syncDirectoryPolicyKey(state) === syncDirectoryPolicyKey(config)
84
102
  : sameSyncExclude(state.exclude, config.exclude);
@@ -32,6 +32,21 @@ export function createEngineService(root) {
32
32
  const host = installServiceHost(root);
33
33
  const monitor = createMonitor(root, host);
34
34
  const execute = (operation, payload, execution) => host.run(execution, async () => {
35
+ if (operation === "ignore.capabilities") {
36
+ execution.validate();
37
+ return { available: false };
38
+ }
39
+ if (operation === "ignore.migration-offer" || operation === "ignore.migration-defer") {
40
+ execution.validate();
41
+ const { readIgnoreMigrationOffer, deferIgnoreMigrationOffer } = await import("./engine/ignore/ignore-migration-prompt.js");
42
+ return operation === "ignore.migration-offer"
43
+ ? readIgnoreMigrationOffer(payload, execution)
44
+ : deferIgnoreMigrationOffer(payload, execution);
45
+ }
46
+ if (operation === "ignore.save") {
47
+ execution.validate();
48
+ throw new Error("Daemon ignore-file saves are unavailable. Edit the existing rule file manually.");
49
+ }
35
50
  await monitor.pause();
36
51
  execution.validate();
37
52
  if (operation === "daemon.authorize")
@@ -58,6 +73,11 @@ export function createEngineService(root) {
58
73
  }
59
74
  if (operation === "monitor.pause")
60
75
  return;
76
+ if (operation === "ignore.read") {
77
+ const { readIgnoreFileEdit } = await import("./engine/ignore/ignore-edit.js");
78
+ execution.validate();
79
+ return readIgnoreFileEdit(payload, execution);
80
+ }
61
81
  if (operation === "settings.read" || operation === "settings.repair-read") {
62
82
  const { readLocalConfigDocument } = await import("./engine/settings/settings-store.js");
63
83
  const { readActiveLocalConfigDocumentForRepair } = await import("./engine/settings/config-file.js");
@@ -81,8 +101,10 @@ export function createEngineService(root) {
81
101
  const { updateLocalConfigDocument, peekLocalConfigDocument } = await import("./engine/settings/config-file.js");
82
102
  const { localConfigTemplate } = await import("./engine/settings/settings-store.js");
83
103
  const { validateSettingsDocument } = await import("./engine/settings/settings-validation.js");
84
- const next = payload.next;
85
- validateSettingsDocument(next);
104
+ const next = validateSettingsDocument(payload.next);
105
+ if (Object.values(next.syncSetups).some((setup) => setup.sync.pathPolicy === "ignore-file")) {
106
+ throw new Error("Ignore-file setup activation is not available in this Daemon build.");
107
+ }
86
108
  if (Buffer.byteLength(JSON.stringify(next)) > MAX_SETTINGS_BYTES)
87
109
  throw new Error("Settings exceed the editable local API byte limit; no fields were dropped.");
88
110
  const validateSource = (document) => {
@@ -33,6 +33,20 @@ flowchart LR
33
33
 
34
34
  Read-only display state is not a second settings writer. A client proposes a validated new document with the exact source fingerprint; the authoritative writer checks the fingerprint under its settings lock. A stale editor must reopen rather than merge a stale approval automatically. Invalid existing JSON remains intact. Unknown fields remain in the proposed source clone.
35
35
 
36
+ The prepared ignore-file switch and migration require an existing `pi-sync.json`. They do not promote `pi-sync.local.json` or install an absent settings file: Node does not provide an atomic no-replace rename, and hard-link publication is not portable to Termux. The legacy file and any competing canonical file stay untouched by these operations. The writer rechecks the active path, identity, and exact bytes before replacing the canonical document. The shared lock orders cooperating writers but does not make a file check and rename atomic against external editors. The existing legacy settings workflow is unchanged.
37
+
38
+ ### Reviewed ignore policy changes (not enabled)
39
+
40
+ The internal switch and migration flows use the same settings writer. Migration keeps the resolved session opt-in; the rules can still exclude sessions. After preparing the files, migration checks the remote head before and after comparing the local paths under both policies. It omits only the starter file created by that operation from the prior policy's paths. It then rechecks the rule source and settings before publication. Policy validation reads the rule source under the shared settings lock and rechecks the settings document before accepting the source digest. A late refusal does not save the new policy or start a transfer; a verified starter file or private settings backup already prepared can remain. The remote head, filesystem inventory, and settings publication are not one atomic transaction.
41
+
42
+ Opening Settings can show an existing rule file path without reading or writing the rules. The daemon rejects `ignore.save`: Node does not provide an atomic rename conditional on the destination still matching the reviewed file. The daemon's locks do not extend to external editors. `ignore.read` checks reported live session files by device and inode so aliases cannot expose session text as rules.
43
+
44
+ ### Local ignore migration deferral
45
+
46
+ `ignore.migration-offer` and `ignore.migration-defer` require a current attached TUI client. The read returns an opaque token only for the requested active legacy setup without a deferral. Opening or dismissing a prompt does not persist a choice. An explicit decline must present the same setup and token, which binds the active document's path, file identity, and bytes to the migration version. `ignore.capabilities` reports `{ available: false }`; it does not enable migration. None of these operations pause the monitor, take the sync lock, contact a backend, or change settings or rules.
47
+
48
+ The local settings queue orders both operations. A read checks the active document under the cross-process config lock when settings or a writer lock are present. A decline always holds that lock. Before publishing a decline, the daemon rechecks the source and client and exclusively creates an empty private marker in the canonical root. This serializes cooperating writers; it is an optimistic check, not an atomic comparison against external editors or parent-directory replacement. Existing markers must be regular, private, single-link, and empty; unsafe paths are not overwritten. The marker name is denied by the snapshot path policy and stores only a hash of the setup name and migration version. Declining applies to that name on this device, including across settings edits and daemon restarts. Success waits for the file flush and, on POSIX, the root directory flush. Once created, the marker stays in place if its durability is uncertain; the operation reports the failure without modifying settings or removing the marker.
49
+
36
50
  ## Context inventory
37
51
 
38
52
  | Former Pi dependency | Engine host boundary | Source |
@@ -193,8 +207,16 @@ These guarantees cover cooperating registered clients. Unregistered old Pi versi
193
207
 
194
208
  Version 10 journals use unique workspace slots. Scratch files get separate unique slots in private directories on the destination filesystem. Recovery reads only the names that the durable journal or scratch record identifies. A failed publication can leave unrecorded objects, but it cannot block a new slot or authorize deleting the old one. The synced path policy excludes these private objects. Older journals keep their exact paths and identity checks; an upgrade does not adopt unrecorded data.
195
209
 
210
+ The ignore-file apply engine uses version 11 with the same workspace layout. It records local source evidence separately from the portable policy digest. Pending recovery requires the same reviewed rule source and a fresh source check under mutation ownership. Completed cleanup still uses the recorded workspace identity and does not replay file changes. The ignore-file policy is not yet enabled through setup or transfer routing; see [ADR 0008](../../../docs/adr/0008-pi-sync-ignore-file.md) for the proposed activation contract.
211
+
212
+ A merge journal can be retired when its recorded commit is already the accepted baseline, even if the remote policy has changed. This does not apply files from the newer head. The prepared ignore-policy merge engine also supports an archive-only review for a pending journal whose original remote head and accepted baseline still match. Directional force cannot skip that recovery. The review can keep newer local edits and archive the journal without changing the backup, the baseline, or the remote. Before retiring an inactive journal without review, recovery checks the baseline and policy, takes a fresh local snapshot, and rechecks the remote head. After archive-only approval, it checks the baseline and policy before the final remote read and rechecks session ownership before archiving. The operation lock orders daemon work but cannot make these reads atomic with external editors or remote writers. If the evidence is uncertain, recovery leaves the journal in place.
213
+
214
+ On macOS, setting a modification time earlier than a file's creation time can also move its creation time back. Before recording an empty scratch file's identity, recovery sets and flushes the backup's timestamps on that file. It reapplies the same timestamps after copying the bytes, then verifies the recorded identity and content before installation.
215
+
196
216
  An identity that must survive edits uses the filesystem device, inode, and creation time. A change timestamp is not creation time: libuv's Unix stat fallback can report `ctime` as `birthtimeNs`, and Linux `statx` can report zero when birth time is absent. With equal positive timestamps on Linux or Android, the engine rechecks the object with synchronous `lstat` and looks for a same-device ancestor with a distinct birth time. These calls share libuv's synchronous `statx` path, which disables itself after falling back. If the engine cannot verify creation time, it stops before an action that needs durable identity. Atomic unlink and empty-directory removal do not need that proof. No identity check makes path-based operations atomic with respect to unrelated writers.
197
217
 
218
+ On Windows, NTFS can change a file's creation time when recovery renames it to a recently deleted name. An installed recovery vacancy can therefore match the recorded device and file ID when its exact restored bytes and original spelling also match. This exception applies only to the installed file. Recovery stages, removal ownership, and observations before rename retain their complete identity checks. Journal formats do not change, and a different file ID or changed content still blocks recovery.
219
+
198
220
  ## Explicit Continuous retry
199
221
 
200
222
  Validated `/sync sync` and manager **Sync now** requests record the requesting client's last target setup. Startup transfers, status reads, and separate refusal retries do not set this intent. Keep intents separate across clients, and retain them while another client is busy. Cancellation, session-generation replacement, disabled Continuous policy, and service shutdown cannot carry an old intent into a later activation.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@narumitw/pi-sync-daemon",
3
- "version": "0.7.0",
3
+ "version": "0.8.0",
4
4
  "description": "A local synchronization daemon with a versioned client API.",
5
5
  "type": "module",
6
6
  "license": "MIT",
@@ -39,6 +39,7 @@
39
39
  "@earendil-works/pi-coding-agent": "1.6.0",
40
40
  "@earendil-works/pi-tui": "1.6.0",
41
41
  "fast-xml-parser": "^5.12.1",
42
+ "ignore": "^7.0.16",
42
43
  "minimatch": "^11.0.9",
43
44
  "proper-lockfile": "^4.1.2",
44
45
  "semver": "^7.8.6"