@narumitw/pi-sync-daemon 0.6.2 → 0.7.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 (71) hide show
  1. package/CHANGELOG.md +8 -0
  2. package/README.md +1 -1
  3. package/dist/consent.js +11 -1
  4. package/dist/engine/backends/git/git-storage.js +11 -6
  5. package/dist/engine/paths.js +1 -1
  6. package/dist/engine/settings/config-file.js +2 -0
  7. package/dist/engine/settings/config.d.ts +2 -4
  8. package/dist/engine/settings/config.js +22 -7
  9. package/dist/engine/settings/settings-management.d.ts +2 -2
  10. package/dist/engine/settings/settings-management.js +27 -33
  11. package/dist/engine/settings/settings-types.d.ts +18 -4
  12. package/dist/engine/settings/settings-validation.js +34 -24
  13. package/dist/engine/snapshot/session-paths.d.ts +5 -3
  14. package/dist/engine/snapshot/session-paths.js +55 -9
  15. package/dist/engine/snapshot/snapshot-apply.d.ts +6 -7
  16. package/dist/engine/snapshot/snapshot-apply.js +88 -21
  17. package/dist/engine/snapshot/snapshot-bounds.d.ts +4 -0
  18. package/dist/engine/snapshot/snapshot-bounds.js +20 -0
  19. package/dist/engine/snapshot/snapshot-codec.js +3 -4
  20. package/dist/engine/snapshot/snapshot-entry-projection.d.ts +10 -0
  21. package/dist/engine/snapshot/snapshot-entry-projection.js +78 -0
  22. package/dist/engine/snapshot/snapshot-exclude-safety.d.ts +7 -2
  23. package/dist/engine/snapshot/snapshot-exclude-safety.js +180 -22
  24. package/dist/engine/snapshot/snapshot-payload.d.ts +2 -0
  25. package/dist/engine/snapshot/snapshot-payload.js +10 -0
  26. package/dist/engine/snapshot/snapshot-reviewed-preimages.d.ts +2 -0
  27. package/dist/engine/snapshot/snapshot-reviewed-preimages.js +60 -0
  28. package/dist/engine/snapshot/snapshot-root-transition.d.ts +4 -0
  29. package/dist/engine/snapshot/snapshot-root-transition.js +16 -0
  30. package/dist/engine/snapshot/snapshot-transaction-images.d.ts +37 -0
  31. package/dist/engine/snapshot/snapshot-transaction-images.js +357 -0
  32. package/dist/engine/snapshot/snapshot-transaction-policy.d.ts +50 -0
  33. package/dist/engine/snapshot/snapshot-transaction-policy.js +172 -0
  34. package/dist/engine/snapshot/snapshot-transaction-stage.d.ts +32 -0
  35. package/dist/engine/snapshot/snapshot-transaction-stage.js +303 -0
  36. package/dist/engine/snapshot/snapshot-transaction.d.ts +2 -1
  37. package/dist/engine/snapshot/snapshot-transaction.js +379 -331
  38. package/dist/engine/snapshot/snapshot-types.d.ts +24 -1
  39. package/dist/engine/snapshot/snapshot.d.ts +19 -2
  40. package/dist/engine/snapshot/snapshot.js +283 -26
  41. package/dist/engine/state/merge-baseline-store.js +20 -9
  42. package/dist/engine/state/state-types.d.ts +2 -0
  43. package/dist/engine/sync/apply-review.d.ts +3 -3
  44. package/dist/engine/sync/apply-review.js +5 -4
  45. package/dist/engine/sync/continuous-sync.js +19 -9
  46. package/dist/engine/sync/continuous-watch.d.ts +4 -1
  47. package/dist/engine/sync/continuous-watch.js +86 -14
  48. package/dist/engine/sync/exclude-policy.d.ts +4 -0
  49. package/dist/engine/sync/exclude-policy.js +8 -1
  50. package/dist/engine/sync/initialization.js +9 -5
  51. package/dist/engine/sync/local-fields.js +27 -8
  52. package/dist/engine/sync/merge-apply.js +155 -40
  53. package/dist/engine/sync/merge-journal.js +13 -4
  54. package/dist/engine/sync/merged-sync.js +21 -18
  55. package/dist/engine/sync/partial-progress.js +5 -3
  56. package/dist/engine/sync/remote-snapshot.d.ts +1 -0
  57. package/dist/engine/sync/remote-snapshot.js +8 -5
  58. package/dist/engine/sync/session-push.js +7 -6
  59. package/dist/engine/sync/sync-decision.d.ts +4 -0
  60. package/dist/engine/sync/sync-decision.js +17 -2
  61. package/dist/engine/sync/sync-inspection.js +3 -3
  62. package/dist/engine/sync/sync-mutations.d.ts +8 -1
  63. package/dist/engine/sync/sync-mutations.js +153 -72
  64. package/dist/engine/sync/sync-policy.d.ts +20 -10
  65. package/dist/engine/sync/sync-policy.js +170 -16
  66. package/dist/engine/sync/sync-queries.js +41 -31
  67. package/dist/engine/sync/sync-state.d.ts +11 -0
  68. package/dist/engine/sync/sync-state.js +24 -5
  69. package/dist/engine/ui/sync-format.js +22 -7
  70. package/docs/architecture.md +6 -0
  71. package/package.json +1 -1
@@ -1,6 +1,6 @@
1
1
  import path from "node:path";
2
2
  import { isDeniedPath, toPosix } from "../paths.js";
3
- import { compileSyncExclude, normalizeSyncExclude, sameSyncExclude } from "./exclude-policy.js";
3
+ import { compilePathRules, compileSyncExclude, normalizeSyncExclude, sameSyncExclude } from "./exclude-policy.js";
4
4
  export const BUILT_IN_SYNC_ROOTS = [
5
5
  "settings.json",
6
6
  "keybindings.json",
@@ -101,8 +101,21 @@ function validateAgentRelativeInclude(value) {
101
101
  throw new Error(`Invalid pi-sync settings: use the canonical ${topLevel} root instead of a nested sync.include path.`);
102
102
  }
103
103
  }
104
+ export function normalizeDirectoryRules(value) {
105
+ return normalizeSyncExclude(Array.isArray(value)
106
+ ? value.map((rule) => (typeof rule === "string" ? rule.replace(/(?<!\/)\/(?= *$)/u, "") : rule))
107
+ : value);
108
+ }
104
109
  export function portableSnapshotSelection(value) {
105
110
  const selection = value;
111
+ if (selection && typeof selection === "object" && !Array.isArray(selection) && selection.version === 3) {
112
+ if (selection.scope !== "agent-directory" ||
113
+ !Array.isArray(selection.exclude) ||
114
+ !Array.isArray(selection.reinclude) ||
115
+ Object.keys(selection).some((key) => !["version", "scope", "include", "exclude", "reinclude"].includes(key)))
116
+ throw new Error("Invalid agent-directory snapshot policy.");
117
+ return directorySelection(selection.include, selection.exclude, selection.reinclude);
118
+ }
106
119
  if (!selection ||
107
120
  typeof selection !== "object" ||
108
121
  Array.isArray(selection) ||
@@ -124,7 +137,21 @@ export function snapshotSelectionInclude(snapshot) {
124
137
  export function snapshotSelectionExclude(snapshot) {
125
138
  return snapshot.selection === undefined ? undefined : portableSnapshotSelection(snapshot.selection).exclude;
126
139
  }
127
- export function selectionForSnapshot(include, exclude) {
140
+ function directorySelection(include, exclude, reinclude) {
141
+ const normalized = normalizeSyncInclude(include);
142
+ if (normalized.some((root) => root !== "sessions"))
143
+ throw new Error("Agent-directory snapshots only use include metadata for explicit session selection.");
144
+ return {
145
+ version: 3,
146
+ scope: "agent-directory",
147
+ include: normalized,
148
+ exclude: normalizeDirectoryRules(exclude),
149
+ reinclude: normalizeDirectoryRules(reinclude),
150
+ };
151
+ }
152
+ export function selectionForSnapshot(include, exclude, directory) {
153
+ if (directory?.scope === "agent-directory")
154
+ return directorySelection(include, exclude, directory.reinclude);
128
155
  const normalized = normalizeSyncInclude(include);
129
156
  return exclude === undefined
130
157
  ? { version: 1, include: normalized }
@@ -139,6 +166,8 @@ export function validateSnapshotSelection(snapshot) {
139
166
  const selection = snapshot.selection === undefined ? undefined : portableSnapshotSelection(snapshot.selection);
140
167
  if ((snapshot.version === 4) !== (selection?.version === 2))
141
168
  throw new Error("Exclude policy requires snapshot version 4 and selection version 2.");
169
+ if ((snapshot.version === 5) !== (selection?.version === 3))
170
+ throw new Error("Agent-directory policy requires snapshot version 5 and selection version 3.");
142
171
  }
143
172
  export function sameSyncInclude(left, right) {
144
173
  const normalizedLeft = normalizeSyncInclude(left);
@@ -157,21 +186,40 @@ export function compareSyncInclude(local, remote) {
157
186
  localOnly: localInclude.filter((item) => !remoteSet.has(item)),
158
187
  };
159
188
  }
160
- export function inspectRemoteSelection(localInclude, snapshot, localExclude) {
161
- const remoteInclude = snapshotSelectionInclude(snapshot);
162
- if (!remoteInclude)
189
+ export function syncDirectoryPolicyKey(config) {
190
+ if (config.scope !== "agent-directory")
191
+ return undefined;
192
+ return JSON.stringify([
193
+ config.scope,
194
+ includeFromSelectionConfig(config).includes("sessions"),
195
+ normalizeDirectoryRules(config.exclude).map((rule) => rule.toLowerCase()),
196
+ normalizeDirectoryRules(config.reinclude).map((rule) => rule.toLowerCase()),
197
+ ]);
198
+ }
199
+ export function inspectRemoteSelection(localInclude, snapshot, localExclude, directory) {
200
+ const selection = snapshot.selection === undefined ? undefined : portableSnapshotSelection(snapshot.selection);
201
+ if (!selection && !directory?.scope)
163
202
  return { kind: "legacy", discovered: discoverLegacySnapshotInclude(snapshot) };
164
- const exclude = snapshotSelectionExclude(snapshot);
203
+ const remoteInclude = selection?.include ?? discoverLegacySnapshotInclude(snapshot);
204
+ const exclude = selection?.exclude;
165
205
  const comparison = compareSyncInclude(localInclude, remoteInclude);
166
- const rules = exclude === undefined ? {} : { exclude };
167
- return comparison.same && sameSyncExclude(localExclude, exclude)
206
+ const rules = {
207
+ ...(exclude === undefined ? {} : { exclude }),
208
+ ...(selection?.scope ? { scope: selection.scope, reinclude: selection.reinclude } : {}),
209
+ };
210
+ const directoryKey = syncDirectoryPolicyKey({ include: localInclude, exclude: localExclude, ...directory });
211
+ const sameDirectoryPolicy = directoryKey === syncDirectoryPolicyKey({ include: remoteInclude, ...rules });
212
+ const sameExclusions = directoryKey !== undefined
213
+ ? sameSyncExclude(normalizeDirectoryRules(localExclude), exclude)
214
+ : sameSyncExclude(localExclude, exclude);
215
+ return comparison.same && sameExclusions && sameDirectoryPolicy
168
216
  ? { kind: "same", include: remoteInclude, ...rules }
169
217
  : {
170
218
  kind: "different",
171
219
  include: remoteInclude,
172
220
  ...comparison,
173
221
  ...rules,
174
- ...(!sameSyncExclude(localExclude, exclude) ? { excludeChanged: true } : {}),
222
+ ...(!sameExclusions ? { excludeChanged: true } : {}),
175
223
  };
176
224
  }
177
225
  export class RemoteSelectionMismatchError extends Error {
@@ -179,10 +227,10 @@ export class RemoteSelectionMismatchError extends Error {
179
227
  setupName;
180
228
  localInclude;
181
229
  remoteInclude;
182
- constructor(setupName, localInclude, remoteInclude, configIdentity = JSON.stringify([setupName, normalizeSyncInclude(localInclude)]), localExclude, remoteExclude) {
230
+ constructor(setupName, localInclude, remoteInclude, configIdentity = JSON.stringify([setupName, normalizeSyncInclude(localInclude)]), localExclude, remoteExclude, localDirectory, remoteDirectory) {
183
231
  const local = normalizeSyncInclude(localInclude);
184
232
  const remote = normalizeSyncInclude(remoteInclude);
185
- super(formatRemoteSelectionMismatch(setupName, local, remote, localExclude, remoteExclude));
233
+ super(formatRemoteSelectionMismatch(setupName, local, remote, localExclude, remoteExclude, localDirectory, remoteDirectory));
186
234
  this.name = "RemoteSelectionMismatchError";
187
235
  this.setupName = setupName;
188
236
  this.localInclude = local;
@@ -194,20 +242,28 @@ export class RemoteSelectionMismatchError extends Error {
194
242
  remoteInclude: [...remote],
195
243
  ...(localExclude === undefined ? {} : { localExclude: normalizeSyncExclude(localExclude) }),
196
244
  ...(remoteExclude === undefined ? {} : { remoteExclude: normalizeSyncExclude(remoteExclude) }),
245
+ ...(localDirectory?.scope ? { localDirectory: directoryRules(localDirectory) } : {}),
246
+ ...(remoteDirectory?.scope ? { remoteDirectory: directoryRules(remoteDirectory) } : {}),
197
247
  };
198
248
  }
199
249
  }
200
- export function remoteSelectionMismatch(config, remoteInclude, configIdentity, remoteExclude) {
201
- return new RemoteSelectionMismatchError(config.setupName, config.include, remoteInclude, configIdentity, config.exclude, remoteExclude);
250
+ function directoryRules(directory) {
251
+ return { scope: directory.scope, reinclude: normalizeDirectoryRules(directory.reinclude) };
252
+ }
253
+ export function remoteSelectionMismatch(config, remoteInclude, configIdentity, remoteExclude, remoteDirectory) {
254
+ return new RemoteSelectionMismatchError(config.setupName, config.include, remoteInclude, configIdentity, config.exclude, remoteExclude, config, remoteDirectory);
202
255
  }
203
- export function formatRemoteSelectionMismatch(setupName, localInclude, remoteInclude, localExclude, remoteExclude) {
256
+ export function formatRemoteSelectionMismatch(setupName, localInclude, remoteInclude, localExclude, remoteExclude, localDirectory, remoteDirectory) {
204
257
  const comparison = compareSyncInclude(localInclude, remoteInclude);
205
258
  const lines = [
206
259
  `Synced content differs for sync setup “${stripTerminalControls(setupName)}”.`,
207
260
  `Remote-only: ${comparison.remoteOnly.join(", ") || "none"}`,
208
261
  `This-device-only: ${comparison.localOnly.join(", ") || "none"}`,
209
262
  ];
210
- if (!sameSyncExclude(localExclude, remoteExclude)) {
263
+ if (localDirectory?.scope || remoteDirectory?.scope) {
264
+ lines.push(`Remote scope: ${remoteDirectory?.scope ?? "selected paths or legacy"}`, `This device scope: ${localDirectory?.scope ?? "selected paths"}`, `Remote excluded paths: ${normalizeSyncExclude(remoteExclude).join(", ") || "none"}`, `This device excluded paths: ${normalizeSyncExclude(localExclude).join(", ") || "none"}`, `Remote re-included paths: ${normalizeDirectoryRules(remoteDirectory?.reinclude).join(", ") || "none"}`, `This device re-included paths: ${normalizeDirectoryRules(localDirectory?.reinclude).join(", ") || "none"}`);
265
+ }
266
+ else if (!sameSyncExclude(localExclude, remoteExclude)) {
211
267
  lines.push(`Remote excluded paths: ${normalizeSyncExclude(remoteExclude).join(", ") || "none"}`, `This device excluded paths: ${normalizeSyncExclude(localExclude).join(", ") || "none"}`);
212
268
  }
213
269
  else if (comparison.remoteOnly.length === 0 && comparison.localOnly.length === 0) {
@@ -271,19 +327,117 @@ export function customIncludePathsByLower(value) {
271
327
  export function includeFromSelectionConfig(config) {
272
328
  if (config.include !== undefined)
273
329
  return normalizeSyncInclude(config.include);
330
+ if (config.scope === "agent-directory")
331
+ return [];
274
332
  return [
275
333
  ...normalizeSyncFiles(config.syncFiles),
276
334
  ...normalizeExtraFiles(config.extraFiles),
277
335
  ...(config.syncSessions ? ["sessions"] : []),
278
336
  ];
279
337
  }
338
+ export function syncSelectionHasContent(config) {
339
+ return config.scope === "agent-directory" || includeFromSelectionConfig(config).length > 0;
340
+ }
341
+ const DIRECTORY_SCOPE_LOCAL_ROOTS = new Set([
342
+ "auth.json",
343
+ "auth.json.lock",
344
+ "trust.json",
345
+ "trust.json.lock",
346
+ "settings.json.lock",
347
+ "models-store.json",
348
+ "models-store.json.lock",
349
+ "crashes.json",
350
+ "pi-debug.log",
351
+ ]);
352
+ function hasControlCharacter(value) {
353
+ for (let index = 0; index < value.length; index++) {
354
+ const code = value.charCodeAt(index);
355
+ if (code < 32 || (code >= 127 && code <= 159))
356
+ return true;
357
+ }
358
+ return false;
359
+ }
360
+ function isProtectedDirectoryPath(relativePath) {
361
+ if (Buffer.byteLength(relativePath, "utf8") > 4_096)
362
+ return true;
363
+ const segments = relativePath.split("/");
364
+ const root = segments[0]?.toLowerCase();
365
+ if (segments.some((segment) => !segment || /[. ]$/u.test(segment) || /^(?:con|prn|aux|nul|com[1-9¹²³]|lpt[1-9¹²³])(?:\.|$)/iu.test(segment)) ||
366
+ /[\\<>:"|?*]/u.test(relativePath) ||
367
+ hasControlCharacter(relativePath) ||
368
+ (root === "sessions" && segments[0] !== "sessions") ||
369
+ (root === "settings.json" && relativePath !== "settings.json") ||
370
+ (root !== undefined && DIRECTORY_SCOPE_LOCAL_ROOTS.has(root)))
371
+ return true;
372
+ for (let ancestor = relativePath; ancestor;) {
373
+ if (isDeniedPath(ancestor))
374
+ return true;
375
+ const slash = ancestor.lastIndexOf("/");
376
+ if (slash < 0)
377
+ break;
378
+ ancestor = ancestor.slice(0, slash);
379
+ }
380
+ return false;
381
+ }
382
+ function compileDirectoryPolicy(config) {
383
+ const excluded = compilePathRules(normalizeDirectoryRules(config.exclude));
384
+ 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
+ return {
388
+ excludes: (relativePath) => blocked(relativePath) || (excluded.matches(relativePath) && !restored.matches(relativePath)),
389
+ descends: (relativePath) => !blocked(relativePath) &&
390
+ (!excluded.matches(relativePath) || restored.matches(relativePath) || restored.mayMatchBelow(relativePath)),
391
+ };
392
+ }
393
+ export const MAX_AGENT_DIRECTORY_ENTRIES = 65_536;
394
+ export const MAX_AGENT_DIRECTORY_FILES = 16_384;
395
+ export function assertAgentDirectoryPathLayout(files) {
396
+ const prefixes = new Map();
397
+ let count = 0;
398
+ for (const file of files) {
399
+ if (++count > MAX_AGENT_DIRECTORY_FILES)
400
+ throw new Error("Agent-directory snapshot exceeds the file bound.");
401
+ for (let end = file.indexOf("/");; end = file.indexOf("/", end + 1)) {
402
+ const spelling = end === -1 ? file : file.slice(0, end);
403
+ const portable = spelling.normalize("NFC").toLowerCase();
404
+ const known = prefixes.get(portable);
405
+ if (known && (known.spelling !== spelling || known.file || end === -1))
406
+ throw new Error("Agent-directory snapshot has conflicting portable paths.");
407
+ if (!known) {
408
+ if (prefixes.size >= MAX_AGENT_DIRECTORY_ENTRIES)
409
+ throw new Error("Agent-directory snapshot exceeds the path bound.");
410
+ prefixes.set(portable, { spelling, file: end === -1 });
411
+ }
412
+ if (end === -1)
413
+ break;
414
+ }
415
+ }
416
+ }
417
+ export function compileSyncPolicyExclude(config) {
418
+ return config.scope === "agent-directory"
419
+ ? compileDirectoryPolicy(config).excludes
420
+ : compileSyncExclude(config.exclude);
421
+ }
422
+ export function compileSyncPolicyTraversal(config) {
423
+ if (config.scope === "agent-directory")
424
+ return compileDirectoryPolicy(config).descends;
425
+ const excludes = compileSyncExclude(config.exclude);
426
+ return (relativePath) => !excludes(relativePath);
427
+ }
280
428
  export function syncSessionsSelected(config) {
281
- return includeFromSelectionConfig(config).includes("sessions") && !compileSyncExclude(config.exclude)("sessions");
429
+ return includeFromSelectionConfig(config).includes("sessions") && compileSyncPolicyTraversal(config)("sessions");
282
430
  }
283
431
  export function isConfiguredSnapshotPath(relativePath, config, _legacyExtraFiles) {
284
432
  return configuredSnapshotPathMatcher(config)(relativePath);
285
433
  }
286
434
  export function configuredSnapshotPathMatcher(config) {
435
+ if (config.scope === "agent-directory") {
436
+ const excluded = compileSyncPolicyExclude(config);
437
+ return (relativePath) => !excluded(relativePath) &&
438
+ relativePath !== "sessions" &&
439
+ (!relativePath.startsWith("sessions/") || relativePath.endsWith(".jsonl"));
440
+ }
287
441
  const selection = syncIncludeSelection(includeFromSelectionConfig(config));
288
442
  const excluded = compileSyncExclude(config.exclude);
289
443
  return (relativePath) => {
@@ -1,6 +1,6 @@
1
1
  import { createSyncBackend } from "../backends/backend-factory.js";
2
2
  import { loadConfig, syncSessionsWarnings } from "../settings/config.js";
3
- import { snapshotOptionsForContext } from "../snapshot/session-paths.js";
3
+ import { resolvedSnapshotOptionsForContext } from "../snapshot/session-paths.js";
4
4
  import { createSnapshot, scanSnapshot } from "../snapshot/snapshot.js";
5
5
  import { inspectLock, isLockGuardHeld, isStaleLock, withLock } from "../state/lock.js";
6
6
  import { readStateForConfig } from "../state/sync-state-store.js";
@@ -14,12 +14,25 @@ import { errorMessage } from "./sync-errors.js";
14
14
  import { inspectSync } from "./sync-inspection.js";
15
15
  import { rollback } from "./sync-mutations.js";
16
16
  import { syncSessionsSelected } from "./sync-policy.js";
17
- const DEFAULT_PROFILE = "default";
17
+ function contentLines(config) {
18
+ return [
19
+ config.scope === "agent-directory"
20
+ ? "included content: eligible files in the Pi agent directory"
21
+ : `included content: ${config.include.map(safeTerminalText).join(", ") || "none"}`,
22
+ ...(config.exclude === undefined
23
+ ? []
24
+ : [`excluded paths: ${config.exclude.map(safeTerminalText).join(", ") || "none"}`]),
25
+ ...(config.reinclude === undefined
26
+ ? []
27
+ : [`re-included paths: ${config.reinclude.map(safeTerminalText).join(", ") || "none"}`]),
28
+ `sessions: ${syncSessionsSelected(config) ? "included" : "excluded"}`,
29
+ ];
30
+ }
18
31
  export async function status(ctx, options, factory = createSyncBackend) {
19
32
  const config = await loadConfig(options.setup);
20
33
  throwIfAborted(options.signal);
21
34
  setSyncStatus(ctx, `checking ${config.setupName}`, { activity: "checking" });
22
- const { head, selectionState, localChanged, remoteChanged, localFiles, destination, capability } = await inspectSync(config, snapshotOptionsForContext(ctx, config), options.signal, factory);
35
+ const { head, selectionState, localChanged, remoteChanged, localFiles, destination, capability } = await inspectSync(config, await resolvedSnapshotOptionsForContext(ctx, config, options.signal), options.signal, factory);
23
36
  throwIfAborted(options.signal);
24
37
  const continuous = config.continuous
25
38
  ? await (await import("./continuous-record.js")).continuousStatusLines(config)
@@ -33,11 +46,7 @@ export async function status(ctx, options, factory = createSyncBackend) {
33
46
  `storage connection: ${config.connectionName}`,
34
47
  `storage location: ${safeTerminalText(destination)}`,
35
48
  `publication safety: ${publicationCapabilityDescription(capability)}`,
36
- `included content: ${config.include.join(", ") || "none"}`,
37
- ...(config.exclude === undefined
38
- ? []
39
- : [`excluded paths: ${config.exclude.map(safeTerminalText).join(", ") || "none"}`]),
40
- `sessions: ${syncSessionsSelected(config) ? "included" : "excluded"}`,
49
+ ...contentLines(config),
41
50
  ...continuous,
42
51
  remoteText,
43
52
  formatRemoteSelectionStatus(selectionState),
@@ -52,10 +61,12 @@ export async function diff(ctx, options, factory = createSyncBackend) {
52
61
  throwIfAborted(options.signal);
53
62
  setSyncStatus(ctx, `checking ${config.setupName}`, { activity: "checking" });
54
63
  const backend = await factory(config);
55
- const local = portableSnapshot(await createSnapshot(config.snapshotIdentity, snapshotOptionsForContext(ctx, config)), config.localFields);
64
+ const snapshotOptions = await resolvedSnapshotOptionsForContext(ctx, config, options.signal);
65
+ const local = portableSnapshot(await createSnapshot(config.snapshotIdentity, snapshotOptions), config.localFields);
56
66
  throwIfAborted(options.signal);
57
67
  const { snapshot: remote, selectionState } = await readRemoteSnapshot(backend, config, options.signal, {
58
68
  allowSelectionDifference: true,
69
+ sessionDir: snapshotOptions.sessionDir,
59
70
  });
60
71
  throwIfAborted(options.signal);
61
72
  setSyncStatus(ctx, undefined);
@@ -64,11 +75,7 @@ export async function diff(ctx, options, factory = createSyncBackend) {
64
75
  `sync setup: ${config.setupName}`,
65
76
  `storage connection: ${config.connectionName}`,
66
77
  `storage location: ${safeTerminalText(backend.destination)}`,
67
- `included content: ${config.include.join(", ") || "none"}`,
68
- ...(config.exclude === undefined
69
- ? []
70
- : [`excluded paths: ${config.exclude.map(safeTerminalText).join(", ") || "none"}`]),
71
- `sessions: ${syncSessionsSelected(config) ? "included" : "excluded"}`,
78
+ ...contentLines(config),
72
79
  formatRemoteSelectionStatus(selectionState),
73
80
  ...warnings,
74
81
  ].join("\n");
@@ -82,19 +89,18 @@ export async function diff(ctx, options, factory = createSyncBackend) {
82
89
  export async function doctor(ctx, options, factory = createSyncBackend) {
83
90
  const messages = [];
84
91
  let level = "info";
85
- let snapshotOptions = {};
86
- let profile = DEFAULT_PROFILE;
92
+ let localConfig;
87
93
  let backend;
88
94
  let backendSummary = [];
89
95
  try {
90
96
  const config = await loadConfig(options.setup);
91
97
  throwIfAborted(options.signal);
98
+ localConfig = {
99
+ profile: config.snapshotIdentity,
100
+ snapshotOptions: await resolvedSnapshotOptionsForContext(ctx, config, options.signal),
101
+ };
92
102
  backend = await factory(config);
93
- profile = config.snapshotIdentity;
94
- snapshotOptions = snapshotOptionsForContext(ctx, config);
95
- messages.push(`config: ok (sync setup ${config.setupName})`, `included content: ${config.include.join(", ") || "none"}`, ...(config.exclude === undefined
96
- ? []
97
- : [`excluded paths: ${config.exclude.map(safeTerminalText).join(", ") || "none"}`]), `sessions: ${syncSessionsSelected(config) ? "included" : "excluded"}`);
103
+ messages.push(`config: ok (sync setup ${config.setupName})`, ...contentLines(config));
98
104
  backendSummary = [
99
105
  `storage location: ${safeTerminalText(backend.destination)}`,
100
106
  `publication safety: ${publicationCapabilityDescription(backend.capability)}`,
@@ -110,17 +116,21 @@ export async function doctor(ctx, options, factory = createSyncBackend) {
110
116
  level = "warning";
111
117
  messages.push(`config: ${errorMessage(error)}`);
112
118
  }
113
- const local = await createSnapshot(profile, snapshotOptions);
114
- throwIfAborted(options.signal);
115
- const secrets = scanSnapshot(local);
116
- if (secrets.length > 0) {
117
- level = "warning";
118
- messages.push("secret scan: possible secrets found:");
119
- messages.push(...secrets.map((secret) => `- ${secret}`));
120
- }
121
- else {
122
- messages.push(`secret scan: ok (${local.files.length} files checked)`);
119
+ if (localConfig) {
120
+ const local = await createSnapshot(localConfig.profile, localConfig.snapshotOptions);
121
+ throwIfAborted(options.signal);
122
+ const secrets = scanSnapshot(local);
123
+ if (secrets.length > 0) {
124
+ level = "warning";
125
+ messages.push("secret scan: possible secrets found:");
126
+ messages.push(...secrets.map((secret) => `- ${safeTerminalText(secret)}`));
127
+ }
128
+ else {
129
+ messages.push(`secret scan: ok (${local.files.length} files checked)`);
130
+ }
123
131
  }
132
+ else
133
+ messages.push("secret scan: skipped without a valid content policy");
124
134
  const lock = await inspectLock();
125
135
  throwIfAborted(options.signal);
126
136
  if (lock.status === "valid" && isStaleLock(lock.lock)) {
@@ -16,6 +16,17 @@ 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: {
20
+ include: string[];
21
+ exclude?: string[];
22
+ scope?: "agent-directory";
23
+ reinclude?: string[];
24
+ }): {
25
+ include: string[];
26
+ exclude?: string[] | undefined;
27
+ scope?: "agent-directory" | undefined;
28
+ reinclude?: string[] | undefined;
29
+ };
19
30
  export declare function settingsHashMap(snapshot: Snapshot): {
20
31
  [k: string]: string;
21
32
  };
@@ -2,10 +2,12 @@ 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, syncSessionsSelected, } from "./sync-policy.js";
5
+ import { configuredSnapshotPathMatcher, customIncludePathsByLower, includeFromSelectionConfig, normalizeSyncInclude, syncDirectoryPolicyKey, syncSessionsSelected, } from "./sync-policy.js";
6
6
  export function hasLocalChanges(local, state, config) {
7
7
  return ((Boolean(state.lastAppliedSnapshot) &&
8
- (!sameLocalFields(state.localFields, config.localFields) || !sameSyncExclude(state.exclude, config.exclude))) ||
8
+ (state.scope !== config.scope ||
9
+ !sameLocalFields(state.localFields, config.localFields) ||
10
+ !sameSelectionRules(state, config))) ||
9
11
  !sameHashes(fileHashMap(portableSnapshot(local, config.localFields)), stateHashMapForConfig(state, config)));
10
12
  }
11
13
  export function remoteChangedSinceState(head, state, config, sameRevision) {
@@ -41,7 +43,10 @@ function stateHashMapForConfig(state, config) {
41
43
  const configured = configuredSnapshotPathMatcher(config);
42
44
  return Object.fromEntries(Object.entries(state.lastFileHashes)
43
45
  .filter(([filePath]) => configured(filePath))
44
- .map(([filePath, hash]) => [canonicalSnapshotPathForConfig(filePath, includePaths), hash]));
46
+ .map(([filePath, hash]) => [
47
+ config.scope === "agent-directory" ? filePath : canonicalSnapshotPathForConfig(filePath, includePaths),
48
+ hash,
49
+ ]));
45
50
  }
46
51
  export function snapshotHashesMatchState(snapshot, state, config, ignoredPaths = new Set()) {
47
52
  return sameHashes(withoutHashPaths(fileHashMap(snapshot), ignoredPaths), withoutHashPaths(stateHashMapForConfig(state, config), ignoredPaths));
@@ -55,8 +60,9 @@ function withoutHashPaths(hashes, ignoredPaths) {
55
60
  return Object.fromEntries(Object.entries(hashes).filter(([filePath]) => !ignoredPaths.has(toPosix(filePath))));
56
61
  }
57
62
  export function syncPolicyChanged(state, config) {
58
- return (syncIncludeChanged(state, config) ||
59
- !sameSyncExclude(state.exclude, config.exclude) ||
63
+ return (state.scope !== config.scope ||
64
+ syncIncludeChanged(state, config) ||
65
+ !sameSelectionRules(state, config) ||
60
66
  !sameLocalFields(state.localFields, config.localFields));
61
67
  }
62
68
  export function shouldRefreshSyncedState(remote, head, state, config, sameRevision) {
@@ -64,6 +70,19 @@ export function shouldRefreshSyncedState(remote, head, state, config, sameRevisi
64
70
  Boolean(head && (!state.lastRemoteRevision || !sameRevision(head.revision, state.lastRemoteRevision))) ||
65
71
  syncPolicyChanged(state, config));
66
72
  }
73
+ export function acceptedSelection(config) {
74
+ return {
75
+ include: [...config.include],
76
+ ...(config.exclude === undefined ? {} : { exclude: [...config.exclude] }),
77
+ ...(config.scope === undefined ? {} : { scope: config.scope }),
78
+ ...(config.reinclude === undefined ? {} : { reinclude: [...config.reinclude] }),
79
+ };
80
+ }
81
+ function sameSelectionRules(state, config) {
82
+ return config.scope === "agent-directory" && state.scope === config.scope
83
+ ? syncDirectoryPolicyKey(state) === syncDirectoryPolicyKey(config)
84
+ : sameSyncExclude(state.exclude, config.exclude);
85
+ }
67
86
  function syncIncludeChanged(state, config) {
68
87
  const stored = state.include ? normalizeSyncInclude(state.include) : includeFromSelectionConfig(state);
69
88
  const current = includeFromSelectionConfig(config);
@@ -47,7 +47,7 @@ export function formatPushSummary(config, destination, upload, head, preservedRe
47
47
  ...excludedPolicyLines(config),
48
48
  head ? `Remote latest: ${safeTerminalText(head.snapshotId)}` : "Remote latest: empty",
49
49
  "Publication effect: the backend's active head will reference the new immutable snapshot. Removed paths leave the active snapshot; retained history is not erased.",
50
- ...(remote && inspectRemoteSelection(config.include, remote, config.exclude).kind === "different"
50
+ ...(remote && inspectRemoteSelection(config.include, remote, config.exclude, config).kind === "different"
51
51
  ? ["Included-content policy effect: replace the differing remote selection with this setup's local selection."]
52
52
  : []),
53
53
  formatPublicationPreview(remote, upload),
@@ -56,15 +56,30 @@ export function formatPushSummary(config, destination, upload, head, preservedRe
56
56
  : preservedRemoteFileCount > 0
57
57
  ? `Possible secrets in locally managed files were scanned before this prompt; ${preservedRemoteFileCount} preserved remote file(s) were not rescanned.`
58
58
  : "Possible secrets were scanned before this prompt.",
59
+ ...(config.scope === "agent-directory"
60
+ ? ["Warning: synced files may contain secrets or other sensitive data that the scanner cannot identify."]
61
+ : []),
59
62
  ].join("\n");
60
63
  }
61
64
  function excludedPolicyLines(config) {
62
- return config.exclude === undefined
63
- ? []
64
- : [
65
- `Excluded paths: ${config.exclude.map(safeTerminalText).join(", ") || "none"}`,
66
- "Excluded local files remain untouched; matching paths are omitted from new publications, not erased from history.",
67
- ];
65
+ const scoped = config.scope === "agent-directory";
66
+ return [
67
+ ...(scoped
68
+ ? [
69
+ "Scope: eligible files in the Pi agent directory.",
70
+ "Always local: Pi credentials (auth.json), project trust (trust.json), their lock files, and Pi Sync settings and service state.",
71
+ ]
72
+ : []),
73
+ ...(config.exclude === undefined && !scoped
74
+ ? []
75
+ : [`Excluded paths: ${(config.exclude ?? []).map(safeTerminalText).join(", ") || "none"}`]),
76
+ ...(scoped ? [`Re-included paths: ${(config.reinclude ?? []).map(safeTerminalText).join(", ") || "none"}`] : []),
77
+ ...(scoped || config.exclude !== undefined
78
+ ? [
79
+ "Excluded local files remain untouched; matching paths are omitted from new publications, not erased from history.",
80
+ ]
81
+ : []),
82
+ ];
68
83
  }
69
84
  export function formatApplyPreview(local, remote, protectedPaths = new Set(), additionalRemovals = []) {
70
85
  return formatDirectionalChanges(local, remote, {
@@ -189,6 +189,12 @@ Pi core's settings storage has separate existing-file and missing-file branches
189
189
 
190
190
  These guarantees cover cooperating registered clients. Unregistered old Pi versions, external editors, shell commands, and arbitrary extensions remain independent writers. Preserve conservative preimage checks, recovery evidence, and explicit upgrade preconditions.
191
191
 
192
+ ### Agent-directory recovery evidence
193
+
194
+ 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
+
196
+ 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
+
192
198
  ## Explicit Continuous retry
193
199
 
194
200
  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.6.2",
3
+ "version": "0.7.0",
4
4
  "description": "A local synchronization daemon with a versioned client API.",
5
5
  "type": "module",
6
6
  "license": "MIT",