@narumitw/pi-sync-daemon 0.6.1 → 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 (72) hide show
  1. package/CHANGELOG.md +15 -0
  2. package/README.md +3 -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/dist/resolver-host.js +1 -1
  71. package/docs/architecture.md +6 -0
  72. package/package.json +4 -4
@@ -1,5 +1,5 @@
1
1
  import { loadConfigForCheck, syncCheckConfigFingerprint } from "../settings/config.js";
2
- import { snapshotOptionsForContext } from "../snapshot/session-paths.js";
2
+ import { resolvedSnapshotOptionsForContext } from "../snapshot/session-paths.js";
3
3
  import { withLock } from "../state/lock.js";
4
4
  import { withStateDirectoryAccess } from "../state/state-directory.js";
5
5
  import { readStateForConfig } from "../state/sync-state-store.js";
@@ -197,6 +197,17 @@ export function createContinuousSync(loaders, clock = systemClock, publish) {
197
197
  if (!ctx.isIdle())
198
198
  throw new DOMException("Owning session became busy", "AbortError");
199
199
  };
200
+ const openWatch = async () => {
201
+ try {
202
+ const { sessionDir } = await resolvedSnapshotOptionsForContext(ctx, config, signal);
203
+ validate();
204
+ return await watchContinuous(config, changed, { sessionDir, signal });
205
+ }
206
+ catch {
207
+ validate();
208
+ return () => { };
209
+ }
210
+ };
200
211
  const stopWatch = () => {
201
212
  closeWatch();
202
213
  wake();
@@ -204,7 +215,7 @@ export function createContinuousSync(loaders, clock = systemClock, publish) {
204
215
  signal.addEventListener("abort", stopWatch, { once: true });
205
216
  let pollDue = 0;
206
217
  try {
207
- closeWatch = await watchContinuous(config, changed);
218
+ closeWatch = await openWatch();
208
219
  validate();
209
220
  while (!signal.aborted && ctx.isIdle()) {
210
221
  validate();
@@ -218,7 +229,7 @@ export function createContinuousSync(loaders, clock = systemClock, publish) {
218
229
  if (syncCheckConfigFingerprint(latest) !== syncCheckConfigFingerprint(config)) {
219
230
  closeWatch();
220
231
  config = latest;
221
- closeWatch = await watchContinuous(config, changed);
232
+ closeWatch = await openWatch();
222
233
  validate();
223
234
  pollDue = 0;
224
235
  }
@@ -324,7 +335,7 @@ export function createContinuousSync(loaders, clock = systemClock, publish) {
324
335
  pollDue = clock.now() + POLL_MS + Math.floor(clock.random() * 6_000);
325
336
  closeWatch();
326
337
  closeWatch = () => { };
327
- closeWatch = await watchContinuous(config, changed);
338
+ closeWatch = await openWatch();
328
339
  validate();
329
340
  }
330
341
  validate();
@@ -372,16 +383,15 @@ export function createContinuousSync(loaders, clock = systemClock, publish) {
372
383
  validate();
373
384
  const observed = await timings.measure("inspection and guards", () => guardedLock("continuous-check", async () => {
374
385
  validate();
375
- const inspection = await inspectionModule.inspectSync(config, snapshotOptionsForContext(ctx, config), signal, undefined, timings);
386
+ const snapshotOptions = await resolvedSnapshotOptionsForContext(ctx, config, signal);
387
+ validate();
388
+ const inspection = await inspectionModule.inspectSync(config, snapshotOptions, signal, undefined, timings);
376
389
  validate();
377
390
  let localIdentity = inspection.localIdentity;
378
391
  if (!localIdentity) {
379
392
  const snapshotModule = await loaders.snapshot();
380
393
  validate();
381
- const local = await snapshotModule.createSnapshot(config.snapshotIdentity, {
382
- ...snapshotOptionsForContext(ctx, config),
383
- signal,
384
- });
394
+ const local = await snapshotModule.createSnapshot(config.snapshotIdentity, snapshotOptions);
385
395
  validate();
386
396
  localIdentity = hashContinuous(fileHashMap(local));
387
397
  }
@@ -1,3 +1,6 @@
1
1
  import type { AnySyncConfig } from "../settings/settings-types.js";
2
2
  /** Hints only. The scheduler's periodic snapshot scan remains required. */
3
- export declare function watchContinuous(config: AnySyncConfig, changed: (settings: boolean) => void): Promise<() => void>;
3
+ export declare function watchContinuous(config: AnySyncConfig, changed: (settings: boolean) => void, options?: {
4
+ sessionDir?: string;
5
+ signal?: AbortSignal;
6
+ }): Promise<() => void>;
@@ -3,27 +3,55 @@ import fs from "node:fs/promises";
3
3
  import path from "node:path";
4
4
  import { getEngineRoot as getAgentDir } from "../host.js";
5
5
  import { isDeniedPath, toPosix } from "../paths.js";
6
+ import { sessionStorageRoot } from "../snapshot/snapshot-paths.js";
6
7
  import { compileSyncExclude } from "./exclude-policy.js";
8
+ import { compileSyncPolicyTraversal, configuredSnapshotPathMatcher, syncSessionsSelected } from "./sync-policy.js";
9
+ function relativeWithin(root, target) {
10
+ const relative = path.relative(root, target);
11
+ return relative === ".." || relative.startsWith(`..${path.sep}`) || path.isAbsolute(relative)
12
+ ? undefined
13
+ : toPosix(relative);
14
+ }
7
15
  /** Hints only. The scheduler's periodic snapshot scan remains required. */
8
- export async function watchContinuous(config, changed) {
16
+ export async function watchContinuous(config, changed, options = {}) {
9
17
  const root = getAgentDir();
10
18
  const watchers = [];
11
19
  const excluded = compileSyncExclude(config.exclude);
12
20
  const selected = config.include.map((item) => path.resolve(root, item));
21
+ const scoped = config.scope === "agent-directory";
22
+ const sessionRoot = sessionStorageRoot(root, options.sessionDir);
23
+ const managed = configuredSnapshotPathMatcher(config);
24
+ const traverse = compileSyncPolicyTraversal(config);
25
+ const scopedPath = (target) => {
26
+ const sessionPath = relativeWithin(sessionRoot, target);
27
+ if (sessionPath !== undefined)
28
+ return sessionPath ? `sessions/${sessionPath}` : "sessions";
29
+ const relative = relativeWithin(root, target);
30
+ return relative && !/^sessions(?:\/|$)/iu.test(relative) ? relative : undefined;
31
+ };
13
32
  const relevant = (target) => selected.some((item) => target === item || target.startsWith(`${item}${path.sep}`) || item.startsWith(`${target}${path.sep}`));
33
+ let closed = false;
14
34
  const add = (directory, recursive) => {
15
35
  try {
16
- const watcher = watch(directory, { recursive, persistent: false }, (_event, file) => {
36
+ const watcher = watch(directory, { recursive, persistent: false }, (event, file) => {
17
37
  // Unknown names can be our own private writes. Periodic scans cover them.
18
- if (!file)
38
+ if (!file || closed || options.signal?.aborted)
19
39
  return;
20
40
  const target = path.resolve(directory, String(file));
41
+ if (!relativeWithin(directory, target))
42
+ return;
21
43
  if (target === path.join(root, "pi-sync.json"))
22
44
  changed(true);
23
- else if (isDeniedPath(path.relative(root, target)))
24
- return;
25
- else if (!excluded(toPosix(path.relative(root, target))) && relevant(target))
45
+ else if (scoped) {
46
+ const relative = scopedPath(target);
47
+ if (relative && (managed(relative) || (event === "rename" && traverse(relative))))
48
+ changed(false);
49
+ }
50
+ else if (!isDeniedPath(path.relative(root, target)) &&
51
+ !excluded(toPosix(path.relative(root, target))) &&
52
+ relevant(target)) {
26
53
  changed(false);
54
+ }
27
55
  });
28
56
  watcher.on("error", () => watcher.close());
29
57
  watchers.push(watcher);
@@ -32,12 +60,59 @@ export async function watchContinuous(config, changed) {
32
60
  /* Periodic scans cover unavailable or unsupported watchers. */
33
61
  }
34
62
  };
35
- add(root, false);
36
- const directories = new Set();
63
+ const directories = new Set([root]);
64
+ const close = () => {
65
+ if (closed)
66
+ return;
67
+ closed = true;
68
+ for (const watcher of watchers)
69
+ watcher.close();
70
+ };
37
71
  try {
72
+ options.signal?.throwIfAborted();
73
+ add(root, false);
74
+ if (scoped) {
75
+ const pending = [root];
76
+ if (syncSessionsSelected(config) && traverse("sessions"))
77
+ pending.push(sessionRoot);
78
+ const scheduled = new Set(pending);
79
+ for (let index = 0; index < pending.length && index < 512; index++) {
80
+ const directory = pending[index];
81
+ if (!directory)
82
+ continue;
83
+ options.signal?.throwIfAborted();
84
+ try {
85
+ const stat = await fs.lstat(directory);
86
+ options.signal?.throwIfAborted();
87
+ if (!stat.isDirectory())
88
+ continue;
89
+ if (directory !== root)
90
+ add(directory, false);
91
+ const entries = await fs.readdir(directory, { withFileTypes: true });
92
+ options.signal?.throwIfAborted();
93
+ for (const entry of entries) {
94
+ if (pending.length >= 512)
95
+ break;
96
+ if (!entry.isDirectory())
97
+ continue;
98
+ const target = path.join(directory, entry.name);
99
+ const relative = scopedPath(target);
100
+ if (relative && traverse(relative) && !scheduled.has(target)) {
101
+ scheduled.add(target);
102
+ pending.push(target);
103
+ }
104
+ }
105
+ }
106
+ catch {
107
+ options.signal?.throwIfAborted();
108
+ }
109
+ }
110
+ return close;
111
+ }
38
112
  for (const target of selected) {
39
113
  try {
40
114
  const stat = await fs.lstat(target);
115
+ options.signal?.throwIfAborted();
41
116
  if (stat.isSymbolicLink())
42
117
  continue;
43
118
  const directory = stat.isDirectory() ? target : path.dirname(target);
@@ -47,19 +122,16 @@ export async function watchContinuous(config, changed) {
47
122
  add(directory, stat.isDirectory());
48
123
  }
49
124
  catch (error) {
125
+ options.signal?.throwIfAborted();
50
126
  if (error.code !== "ENOENT")
51
127
  continue;
52
128
  // Missing targets are found by the parent hint or the next periodic scan.
53
129
  }
54
130
  }
55
- return () => {
56
- for (const watcher of watchers)
57
- watcher.close();
58
- };
131
+ return close;
59
132
  }
60
133
  catch (error) {
61
- for (const watcher of watchers)
62
- watcher.close();
134
+ close();
63
135
  throw error;
64
136
  }
65
137
  }
@@ -3,3 +3,7 @@ export declare function normalizeSyncExclude(value: unknown): string[];
3
3
  export declare function sameSyncExclude(left: unknown, right: unknown): boolean;
4
4
  /** Compile at the operation boundary and reuse for every file/directory. */
5
5
  export declare function compileSyncExclude(value: unknown): (relativePath: string) => boolean;
6
+ export declare function compilePathRules(value: unknown): {
7
+ matches: (relativePath: string) => boolean;
8
+ mayMatchBelow: (directory: string) => boolean;
9
+ };
@@ -51,6 +51,9 @@ export function sameSyncExclude(left, right) {
51
51
  }
52
52
  /** Compile at the operation boundary and reuse for every file/directory. */
53
53
  export function compileSyncExclude(value) {
54
+ return compilePathRules(value).matches;
55
+ }
56
+ export function compilePathRules(value) {
54
57
  const matchers = normalizeSyncExclude(value).map((rule) =>
55
58
  // A trailing globstar also matches zero segments; ancestor matching owns its subtree.
56
59
  new Minimatch(rule.replace(/(?:\/\*\*)+$/u, ""), {
@@ -63,7 +66,7 @@ export function compileSyncExclude(value) {
63
66
  platform: "linux",
64
67
  optimizationLevel: 0,
65
68
  }));
66
- return (relativePath) => {
69
+ const matches = (relativePath) => {
67
70
  if (matchers.length === 0)
68
71
  return false;
69
72
  for (let candidate = relativePath; candidate;) {
@@ -76,4 +79,8 @@ export function compileSyncExclude(value) {
76
79
  }
77
80
  return false;
78
81
  };
82
+ return {
83
+ matches,
84
+ mayMatchBelow: (directory) => matchers.some((matcher) => matcher.match(directory, true)),
85
+ };
79
86
  }
@@ -1,8 +1,8 @@
1
- import { loadConfig } from "../settings/config.js";
1
+ import { configFromSettings, loadConfig } from "../settings/config.js";
2
2
  import { isMissingConfigError } from "../settings/config-errors.js";
3
3
  import { consumeLocalConfigMigrationNotice } from "../settings/config-file.js";
4
4
  import { readLocalConfigObject } from "../settings/settings-store.js";
5
- import { sessionDirFromContext } from "../snapshot/session-paths.js";
5
+ import { effectiveSessionRoot, sessionDirFromContext, snapshotOptionsForContext } from "../snapshot/session-paths.js";
6
6
  import { pendingTransactionEntries } from "../snapshot/snapshot-transaction-directory.js";
7
7
  import { stateDirectoryMigrationNotice, withStateDirectoryAccess } from "../state/state-directory.js";
8
8
  import { captureMutationOwner } from "./mutation-owner.js";
@@ -29,12 +29,16 @@ export async function initializeSync(ctx, signal, publish) {
29
29
  validate();
30
30
  const settings = await readLocalConfigObject();
31
31
  validate();
32
- const setup = settings?.activeSyncSetup;
32
+ const config = settings?.activeSyncSetup ? configFromSettings(settings) : undefined;
33
+ const sessionDir = config?.scope === "agent-directory"
34
+ ? await effectiveSessionRoot(ctx, config, signal)
35
+ : sessionDirFromContext(ctx);
36
+ validate();
33
37
  const current = ctx.sessionManager.getSessionFile?.();
34
38
  await recoverSnapshotTransactionsOnStartup({
35
- exclude: setup ? settings.syncSetups[setup]?.sync.exclude : undefined,
39
+ ...(config ? snapshotOptionsForContext(ctx, config) : {}),
36
40
  signal,
37
- sessionDir: sessionDirFromContext(ctx),
41
+ sessionDir,
38
42
  protectedTargets: [...(ctx.protectedSessionFiles ?? []), ...(current ? [current] : [])],
39
43
  validateMutation: validate,
40
44
  });
@@ -1,17 +1,17 @@
1
1
  import { createHash } from "node:crypto";
2
2
  import { regenerateSnapshotIdentity } from "../snapshot/snapshot.js";
3
- import { compileSyncExclude } from "./exclude-policy.js";
3
+ import { assertAgentDirectorySnapshotBound } from "../snapshot/snapshot-bounds.js";
4
4
  import { jsonEqual, parseSettingsDocument, renderSettingsMembers, } from "./json-document.js";
5
5
  import { normalizeLocalFields } from "./local-field-policy.js";
6
- import { snapshotSelectionExclude, validateSnapshotSelection } from "./sync-policy.js";
6
+ import { compileSyncPolicyExclude, configuredSnapshotPathMatcher, validateSnapshotSelection } from "./sync-policy.js";
7
7
  export { normalizeLocalFields } from "./local-field-policy.js";
8
8
  /** Shared transport contract: older snapshots cannot carry portable policy metadata. */
9
9
  export function validateSnapshotFieldPolicy(snapshot) {
10
- if (snapshot.version !== 1 && snapshot.version !== 2 && snapshot.version !== 3 && snapshot.version !== 4)
10
+ if (![1, 2, 3, 4, 5].includes(snapshot.version))
11
11
  throw new Error("Unsupported snapshot format.");
12
12
  if (snapshot.version === 1 && snapshot.localFields !== undefined)
13
13
  throw new Error("Portable field policy requires snapshot version 2.");
14
- if (snapshot.version === 2 || snapshot.version === 3 || snapshot.version === 4) {
14
+ if (snapshot.version !== 1) {
15
15
  if (snapshot.version === 2 && snapshot.localFields === undefined)
16
16
  throw new Error("Snapshot version 2 requires explicit localFields rules.");
17
17
  normalizeLocalFields(snapshot.localFields);
@@ -21,11 +21,21 @@ export function validateSnapshotFieldPolicy(snapshot) {
21
21
  export function validatePortableSnapshot(snapshot) {
22
22
  validateSnapshotFieldPolicy(snapshot);
23
23
  validateSnapshotSelection(snapshot);
24
- const excludedPath = compileSyncExclude(snapshotSelectionExclude(snapshot));
25
- if (snapshot.files.some((file) => excludedPath(file.path)))
26
- throw new Error("Portable snapshot retains excluded paths.");
24
+ const selection = snapshot.selection;
25
+ if (selection?.scope) {
26
+ const selected = configuredSnapshotPathMatcher(selection);
27
+ if (snapshot.files.some((file) => typeof file?.path !== "string" || !selected(file.path)))
28
+ throw new Error("Portable snapshot retains excluded or unsafe agent-directory paths.");
29
+ assertAgentDirectorySnapshotBound(snapshot.files);
30
+ }
31
+ else {
32
+ const excludedPath = compileSyncPolicyExclude(selection ?? {});
33
+ if (snapshot.files.some((file) => excludedPath(file.path)))
34
+ throw new Error("Portable snapshot retains excluded paths.");
35
+ }
27
36
  if (snapshot.version === 1 || !snapshot.localFields?.length)
28
37
  return;
38
+ requireCanonicalFieldSettingsPath(snapshot);
29
39
  try {
30
40
  for (const entry of snapshot.files) {
31
41
  if (typeof entry?.path !== "string" || entry.path.toLowerCase() !== "settings.json")
@@ -51,10 +61,17 @@ function file(content) {
51
61
  sha256: createHash("sha256").update(content).digest("hex"),
52
62
  };
53
63
  }
64
+ function requireCanonicalFieldSettingsPath(snapshot) {
65
+ if (snapshot.selection?.scope &&
66
+ snapshot.files.some(({ path }) => path !== "settings.json" && path.toLowerCase() === "settings.json"))
67
+ throw new Error("Directory scope requires the exact settings.json spelling when machine-local fields are configured.");
68
+ }
54
69
  export function portableSnapshot(snapshot, fields) {
55
70
  if (fields === undefined)
56
71
  return snapshot;
57
72
  const excluded = new Set(normalizeLocalFields(fields));
73
+ if (excluded.size > 0)
74
+ requireCanonicalFieldSettingsPath(snapshot);
58
75
  const files = snapshot.files.map((entry) => {
59
76
  if (entry.path !== "settings.json" || excluded.size === 0)
60
77
  return entry;
@@ -74,7 +91,7 @@ export function portableSnapshot(snapshot, fields) {
74
91
  });
75
92
  return regenerateSnapshotIdentity({
76
93
  ...snapshot,
77
- version: snapshot.version === 4 ? 4 : 2,
94
+ version: snapshot.version >= 4 ? snapshot.version : 2,
78
95
  localFields: [...excluded],
79
96
  files,
80
97
  });
@@ -82,6 +99,8 @@ export function portableSnapshot(snapshot, fields) {
82
99
  export function overlayLocalFields(portable, local, fields) {
83
100
  if (!fields?.length)
84
101
  return portable;
102
+ requireCanonicalFieldSettingsPath(portable);
103
+ requireCanonicalFieldSettingsPath(local);
85
104
  const target = portable.files.find((entry) => entry.path === "settings.json");
86
105
  const current = local.files.find((entry) => entry.path === "settings.json");
87
106
  if (!target && !current)