@narumitw/pi-sync 0.63.1 → 0.64.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 (127) hide show
  1. package/CHANGELOG.md +11 -0
  2. package/README.md +12 -5
  3. package/dist/chunks/{chunk-HLCML3OW.ts → chunk-4VKWHVUO.ts} +4 -3
  4. package/dist/chunks/chunk-4VKWHVUO.ts.map +7 -0
  5. package/dist/chunks/{chunk-27F5W7DX.ts → chunk-DAZZ5BEJ.ts} +3 -1
  6. package/dist/chunks/chunk-DAZZ5BEJ.ts.map +7 -0
  7. package/dist/chunks/{chunk-V76KCWHO.ts → chunk-F3VODLVQ.ts} +3 -3
  8. package/dist/chunks/{chunk-VQ47JBLF.ts → chunk-FI2F5JBS.ts} +2 -2
  9. package/dist/chunks/{chunk-LBNC5AOM.ts → chunk-NDEZ52SS.ts} +12 -12
  10. package/dist/chunks/{chunk-B5F3JLD3.ts → chunk-RW44KNFD.ts} +304 -70
  11. package/dist/chunks/chunk-RW44KNFD.ts.map +7 -0
  12. package/dist/chunks/{chunk-EXDWEFIJ.ts → chunk-TJ7YNITN.ts} +5 -5
  13. package/dist/chunks/{chunk-HSCAFOT5.ts → chunk-TZMVSTAM.ts} +5 -5
  14. package/dist/chunks/{chunk-SXCH4Q5O.ts → chunk-VPSC7BXM.ts} +3 -3
  15. package/dist/chunks/{chunk-POWOMJ7S.ts → chunk-YT6PDRMM.ts} +58 -31
  16. package/dist/chunks/chunk-YT6PDRMM.ts.map +7 -0
  17. package/dist/chunks/{command-handler-GMRRFWOL.ts → command-handler-N45GY7AV.ts} +39 -27
  18. package/dist/chunks/command-handler-N45GY7AV.ts.map +7 -0
  19. package/dist/chunks/{conflict-review-JWEA7DPQ.ts → conflict-review-5E4M44BA.ts} +2 -2
  20. package/dist/chunks/{file-selection-KID23GAD.ts → file-selection-DDQN4LVD.ts} +9 -9
  21. package/dist/chunks/{manager-result-dispatcher-GDB74IOP.ts → manager-result-dispatcher-FKW55L7I.ts} +5 -5
  22. package/dist/chunks/{manager-ui-T5J5WG7T.ts → manager-ui-EV23DPZQ.ts} +75 -57
  23. package/dist/chunks/manager-ui-EV23DPZQ.ts.map +7 -0
  24. package/dist/chunks/{merge-review-OQDXNTJU.ts → merge-review-6YSLSMGS.ts} +3 -2
  25. package/dist/chunks/ownership-recovery-UDDHSDSZ.ts +59 -0
  26. package/dist/chunks/ownership-recovery-UDDHSDSZ.ts.map +7 -0
  27. package/dist/chunks/{remote-selection-ui-GPITNEBF.ts → remote-selection-ui-N4NMWFT2.ts} +4 -4
  28. package/dist/chunks/{setup-switch-L54X5U5I.ts → setup-switch-HY7K3442.ts} +4 -4
  29. package/dist/chunks/{setup-wizard-LL6YMGUR.ts → setup-wizard-LEKDQ3HK.ts} +9 -9
  30. package/dist/chunks/{sync-inspection-SRU6HGGK.ts → sync-inspection-OS2LVOLV.ts} +5 -5
  31. package/dist/chunks/{sync-operations-UGSCONZ7.ts → sync-operations-D3MDJUQQ.ts} +2 -2
  32. package/dist/daemon/dist/cli.js +2 -0
  33. package/dist/daemon/dist/client-transport.js +66 -0
  34. package/dist/daemon/dist/client.js +116 -86
  35. package/dist/daemon/dist/consent.js +33 -11
  36. package/dist/daemon/dist/daemon-debug.js +33 -0
  37. package/dist/daemon/dist/daemon-recovery.js +75 -0
  38. package/dist/daemon/dist/engine/backends/git/git-backend.js +39 -12
  39. package/dist/daemon/dist/engine/backends/git/git-runner.js +6 -1
  40. package/dist/daemon/dist/engine/backends/git/git-storage.js +5 -9
  41. package/dist/daemon/dist/engine/backends/publication-encoding.js +19 -0
  42. package/dist/daemon/dist/engine/backends/s3/s3-backend.js +10 -12
  43. package/dist/daemon/dist/engine/backends/webdav/webdav-backend.js +15 -26
  44. package/dist/daemon/dist/engine/backends/webdav/webdav-client.js +10 -0
  45. package/dist/daemon/dist/engine/debug-log.js +134 -0
  46. package/dist/daemon/dist/engine/paths.js +7 -1
  47. package/dist/daemon/dist/engine/settings/config-file.js +62 -17
  48. package/dist/daemon/dist/engine/settings/config.js +49 -14
  49. package/dist/daemon/dist/engine/snapshot/snapshot-apply.js +30 -8
  50. package/dist/daemon/dist/engine/snapshot/snapshot-codec.js +3 -1
  51. package/dist/daemon/dist/engine/snapshot/snapshot-payload.js +32 -0
  52. package/dist/daemon/dist/engine/snapshot/snapshot-transaction-images.js +34 -0
  53. package/dist/daemon/dist/engine/snapshot/snapshot-transaction.js +50 -7
  54. package/dist/daemon/dist/engine/snapshot/snapshot.js +64 -25
  55. package/dist/daemon/dist/engine/state/lock.js +157 -17
  56. package/dist/daemon/dist/engine/sync/continuous-sync.js +35 -11
  57. package/dist/daemon/dist/engine/sync/initialization.js +2 -2
  58. package/dist/daemon/dist/engine/sync/local-fields.js +42 -19
  59. package/dist/daemon/dist/engine/sync/merged-sync.js +13 -10
  60. package/dist/daemon/dist/engine/sync/remote-snapshot.js +18 -3
  61. package/dist/daemon/dist/engine/sync/session-push.js +13 -4
  62. package/dist/daemon/dist/engine/sync/startup-bootstrap.js +105 -0
  63. package/dist/daemon/dist/engine/sync/startup-recovery-evidence.js +37 -0
  64. package/dist/daemon/dist/engine/sync/startup-timings.js +80 -8
  65. package/dist/daemon/dist/engine/sync/sync-error-guidance.js +6 -1
  66. package/dist/daemon/dist/engine/sync/sync-inspection.js +4 -7
  67. package/dist/daemon/dist/engine/sync/sync-local.js +3 -1
  68. package/dist/daemon/dist/engine/sync/sync-mutations.js +9 -3
  69. package/dist/daemon/dist/engine/sync/sync-policy.js +9 -1
  70. package/dist/daemon/dist/engine/sync/sync-state.js +12 -4
  71. package/dist/daemon/dist/engine-service.js +54 -10
  72. package/dist/daemon/dist/identity.js +36 -6
  73. package/dist/daemon/dist/index.js +2 -1
  74. package/dist/daemon/dist/monitor.js +20 -1
  75. package/dist/daemon/dist/paged-review.js +177 -0
  76. package/dist/daemon/dist/participant-ledger.js +149 -0
  77. package/dist/daemon/dist/protocol.js +4 -0
  78. package/dist/daemon/dist/server.js +268 -120
  79. package/dist/daemon/dist/service-host.js +29 -9
  80. package/dist/daemon/dist/state-wait-jobs.js +23 -0
  81. package/dist/daemon/dist/state-wait.js +34 -0
  82. package/dist/daemon/package.json +1 -1
  83. package/dist/index.ts +128 -48
  84. package/dist/index.ts.map +2 -2
  85. package/docs/continuous-sync-verification.md +2 -0
  86. package/docs/daemon-migration.md +30 -2
  87. package/docs/tui-contract.md +7 -1
  88. package/package.json +2 -2
  89. package/skills/configure-pi-sync/references/configuration.md +23 -13
  90. package/src/commands/command-execution.ts +11 -3
  91. package/src/commands/command-handler.ts +10 -3
  92. package/src/commands/command.ts +3 -2
  93. package/src/daemon/client-debug.ts +45 -0
  94. package/src/daemon/ownership-recovery.ts +50 -0
  95. package/src/daemon/pi-client.ts +288 -63
  96. package/src/daemon/startup-bootstrap-client.ts +26 -0
  97. package/src/settings/config.ts +1 -0
  98. package/src/sync/automatic-sync.ts +61 -18
  99. package/src/sync/startup-check.ts +13 -5
  100. package/src/sync-extension.ts +46 -8
  101. package/src/ui/manager-state.ts +29 -21
  102. package/src/ui/manager-ui.ts +16 -1
  103. package/src/ui/merge-review.ts +52 -16
  104. package/src/ui/settings-ui.ts +2 -2
  105. package/src/ui/sync-confirmation.ts +16 -19
  106. package/src/ui/sync-setups-ui.ts +15 -12
  107. package/dist/chunks/chunk-27F5W7DX.ts.map +0 -7
  108. package/dist/chunks/chunk-B5F3JLD3.ts.map +0 -7
  109. package/dist/chunks/chunk-HLCML3OW.ts.map +0 -7
  110. package/dist/chunks/chunk-POWOMJ7S.ts.map +0 -7
  111. package/dist/chunks/command-handler-GMRRFWOL.ts.map +0 -7
  112. package/dist/chunks/manager-ui-T5J5WG7T.ts.map +0 -7
  113. /package/dist/chunks/{chunk-V76KCWHO.ts.map → chunk-F3VODLVQ.ts.map} +0 -0
  114. /package/dist/chunks/{chunk-VQ47JBLF.ts.map → chunk-FI2F5JBS.ts.map} +0 -0
  115. /package/dist/chunks/{chunk-LBNC5AOM.ts.map → chunk-NDEZ52SS.ts.map} +0 -0
  116. /package/dist/chunks/{chunk-EXDWEFIJ.ts.map → chunk-TJ7YNITN.ts.map} +0 -0
  117. /package/dist/chunks/{chunk-HSCAFOT5.ts.map → chunk-TZMVSTAM.ts.map} +0 -0
  118. /package/dist/chunks/{chunk-SXCH4Q5O.ts.map → chunk-VPSC7BXM.ts.map} +0 -0
  119. /package/dist/chunks/{conflict-review-JWEA7DPQ.ts.map → conflict-review-5E4M44BA.ts.map} +0 -0
  120. /package/dist/chunks/{file-selection-KID23GAD.ts.map → file-selection-DDQN4LVD.ts.map} +0 -0
  121. /package/dist/chunks/{manager-result-dispatcher-GDB74IOP.ts.map → manager-result-dispatcher-FKW55L7I.ts.map} +0 -0
  122. /package/dist/chunks/{merge-review-OQDXNTJU.ts.map → merge-review-6YSLSMGS.ts.map} +0 -0
  123. /package/dist/chunks/{remote-selection-ui-GPITNEBF.ts.map → remote-selection-ui-N4NMWFT2.ts.map} +0 -0
  124. /package/dist/chunks/{setup-switch-L54X5U5I.ts.map → setup-switch-HY7K3442.ts.map} +0 -0
  125. /package/dist/chunks/{setup-wizard-LL6YMGUR.ts.map → setup-wizard-LEKDQ3HK.ts.map} +0 -0
  126. /package/dist/chunks/{sync-inspection-SRU6HGGK.ts.map → sync-inspection-OS2LVOLV.ts.map} +0 -0
  127. /package/dist/chunks/{sync-operations-UGSCONZ7.ts.map → sync-operations-D3MDJUQQ.ts.map} +0 -0
@@ -2,7 +2,7 @@ import { createSyncBackend } from "../backends/backend-factory.js";
2
2
  import { expectedRemoteHead, SyncBackendConflictError } from "../backends/sync-backend.js";
3
3
  import { loadConfig, syncCheckConfigFingerprint } from "../settings/config.js";
4
4
  import { effectiveSessionRoot, requireStableMergeSessionRoot, snapshotOptionsForContext, } from "../snapshot/session-paths.js";
5
- import { createSnapshot, filterSnapshotForConfigPolicy, mergeRemotePreservedFiles, regenerateSnapshotIdentity, scanSnapshot, } from "../snapshot/snapshot.js";
5
+ import { createSnapshot, filterSnapshotForConfigPolicy, mergeRemotePreservedFiles, observeSnapshot, regenerateSnapshotIdentity, scanSnapshot, snapshotWithObservedInventory, } from "../snapshot/snapshot.js";
6
6
  import { ignorePolicyValidator } from "../snapshot/snapshot-transaction-policy.js";
7
7
  import { hasCurrentMergeBaseline, pruneMergeBaselines, readMergeAncestors } from "../state/merge-baseline-store.js";
8
8
  import { readStateForConfig, statePathForConfig, syncStateFingerprint } from "../state/sync-state-store.js";
@@ -27,7 +27,7 @@ import { formatRemoteSelectionStatus, readSnapshotForHead, requireCompatibleRemo
27
27
  import { resolveSettingsConflicts } from "./settings-conflicts.js";
28
28
  import { createStartupTimings } from "./startup-timings.js";
29
29
  import { createSyncDecision } from "./sync-decision.js";
30
- import { backupLocal, captureMutationOwner, protectedSessionPaths } from "./sync-local.js";
30
+ import { backupSnapshot, captureMutationOwner, protectedSessionPaths } from "./sync-local.js";
31
31
  import { configuredSnapshotPathMatcher, inspectRemoteSelection, syncSessionsSelected } from "./sync-policy.js";
32
32
  import { fileHashMap, hasLocalChanges, hasRemoteChanges, sameHashes, syncPolicyChanged } from "./sync-state.js";
33
33
  import { TEXT_MERGE_WORK_LIMIT } from "./text-merge.js";
@@ -163,7 +163,7 @@ export async function mergeSync(ctx, options, factory = createSyncBackend, resol
163
163
  if (!(await hasCurrentMergeBaseline(config, local, state)))
164
164
  return false;
165
165
  await validate();
166
- const currentLocal = await timings.measure("transfer local snapshot", () => createSnapshot(config.snapshotIdentity, snapshotOptions));
166
+ const currentLocal = await timings.measure("transfer local snapshot", () => observeSnapshot(snapshotOptions));
167
167
  await validate();
168
168
  const currentState = await timings.measure("transfer state read", () => readStateForConfig(config));
169
169
  await validate();
@@ -362,7 +362,7 @@ export async function mergeSync(ctx, options, factory = createSyncBackend, resol
362
362
  throw new ContinuousError("stale", "Remote changed before baseline acceptance; retry from a fresh observation.");
363
363
  progress = await persistProgress();
364
364
  await validate();
365
- const currentLocal = await timings.measure("transfer local snapshot", () => createSnapshot(config.snapshotIdentity, snapshotOptions));
365
+ const currentLocal = await timings.measure("transfer local snapshot", () => observeSnapshot(snapshotOptions));
366
366
  await validate();
367
367
  if (!sameHashes(fileHashMap(currentLocal), fileHashMap(localRaw)) ||
368
368
  syncStateFingerprint(await timings.measure("transfer state read", () => readStateForConfig(config))) !==
@@ -411,8 +411,10 @@ export async function mergeSync(ctx, options, factory = createSyncBackend, resol
411
411
  return "cancelled";
412
412
  }
413
413
  await validate();
414
- const refreshed = await timings.measure("transfer local snapshot", () => createSnapshot(config.snapshotIdentity, snapshotOptions));
415
- if (!sameHashes(fileHashMap(refreshed), fileHashMap(localRaw)))
414
+ const refreshObservation = await timings.measure("transfer local snapshot", () => observeSnapshot(snapshotOptions));
415
+ validateOwner();
416
+ const refreshed = snapshotWithObservedInventory(localRaw, refreshObservation);
417
+ if (!refreshed)
416
418
  throw new ContinuousError("stale", "Local content changed during review; no merged transfer was performed.");
417
419
  if (syncStateFingerprint(await timings.measure("transfer state read", () => readStateForConfig(config))) !==
418
420
  syncStateFingerprint(state))
@@ -426,7 +428,7 @@ export async function mergeSync(ctx, options, factory = createSyncBackend, resol
426
428
  await validate();
427
429
  progress = await persistProgress();
428
430
  await validate();
429
- const backup = await timings.measure("transfer backup", () => backupLocal(config.snapshotIdentity, snapshotOptions, options.signal));
431
+ const backup = await timings.measure("transfer backup", () => backupSnapshot(regenerateSnapshotIdentity(refreshed), options.signal));
430
432
  await validateFresh();
431
433
  const journal = {
432
434
  ...mergeJournalPolicy(config),
@@ -446,9 +448,10 @@ export async function mergeSync(ctx, options, factory = createSyncBackend, resol
446
448
  // No backend call has begun: a stale candidate can be retired without ambiguous publication.
447
449
  const atCommitState = syncStateFingerprint(await timings.measure("transfer state read", () => readStateForConfig(config)));
448
450
  await validate();
449
- const atCommit = await timings.measure("transfer local snapshot", () => createSnapshot(config.snapshotIdentity, snapshotOptions));
451
+ const commitObservation = await timings.measure("transfer local snapshot", () => observeSnapshot(snapshotOptions));
450
452
  await validate();
451
- if (!sameHashes(fileHashMap(atCommit), fileHashMap(localRaw)) || atCommitState !== journal.stateIdentity) {
453
+ const atCommit = snapshotWithObservedInventory(refreshed, commitObservation);
454
+ if (!atCommit || atCommitState !== journal.stateIdentity) {
452
455
  await validateFresh();
453
456
  await clearMergeJournal(config);
454
457
  throw new ContinuousError("stale", "Local content or baseline changed before publication; candidate retired without transfer. Review a fresh sync.");
@@ -546,7 +549,7 @@ async function completeJournal(ctx, config, backend, journal, { validate, valida
546
549
  const readRetirementEvidence = async () => {
547
550
  const state = await timings.measure("transfer state read", () => readStateForConfig(config));
548
551
  await validateFresh();
549
- const local = await timings.measure("transfer local snapshot", () => createSnapshot(config.snapshotIdentity, snapshotOptions));
552
+ const local = await timings.measure("transfer local snapshot", () => observeSnapshot(snapshotOptions));
550
553
  validateMutation();
551
554
  const latest = await timings.measure("transfer remote head", () => backend.readHead(signal));
552
555
  validateMutation();
@@ -8,6 +8,9 @@ import { syncPolicyChanged } from "./sync-state.js";
8
8
  async function readValidatedSnapshotForHead(backend, head, signal) {
9
9
  const snapshot = await backend.readSnapshot(head.snapshotRef, signal);
10
10
  throwIfAborted(signal);
11
+ return validateSnapshotForHead(snapshot, head);
12
+ }
13
+ function validateSnapshotForHead(snapshot, head) {
11
14
  validatePortableSnapshot(snapshot);
12
15
  if (snapshot.id !== head.snapshotId) {
13
16
  throw new Error(`Remote head ${head.snapshotId} resolved to unexpected snapshot ${snapshot.id}.`);
@@ -105,10 +108,10 @@ function safeList(values) {
105
108
  return values.length > 0 ? values.map(safeTerminalText).join(", ") : "none";
106
109
  }
107
110
  export async function readRemoteSnapshot(backend, config, signal, options = {}) {
108
- const head = await backend.readHead(signal);
109
- if (!head)
111
+ const observed = await readCurrentSnapshot(backend, config.scope === "agent-directory", signal);
112
+ if (!observed)
110
113
  return { head: undefined, snapshot: undefined, selectionState: undefined };
111
- const rawSnapshot = await readValidatedSnapshotForHead(backend, head, signal);
114
+ const { head, snapshot: rawSnapshot } = observed;
112
115
  const selectionState = inspectRemoteSelection(config.include, rawSnapshot, config.exclude, config);
113
116
  if (!options.allowSelectionDifference && selectionState.kind === "different")
114
117
  throw selectionMismatch(config, selectionState);
@@ -117,3 +120,15 @@ export async function readRemoteSnapshot(backend, config, signal, options = {})
117
120
  : filterSnapshotForConfigPolicy(rawSnapshot, config, { sessionDir: options.sessionDir });
118
121
  return { head, snapshot, selectionState };
119
122
  }
123
+ async function readCurrentSnapshot(backend, singleObservation, signal) {
124
+ if (singleObservation && backend.readHeadAndSnapshot) {
125
+ const observed = await backend.readHeadAndSnapshot(signal);
126
+ throwIfAborted(signal);
127
+ if (observed)
128
+ validateSnapshotForHead(observed.snapshot, observed.head);
129
+ return observed;
130
+ }
131
+ const head = await backend.readHead(signal);
132
+ throwIfAborted(signal);
133
+ return head && { head, snapshot: await readValidatedSnapshotForHead(backend, head, signal) };
134
+ }
@@ -9,6 +9,7 @@ import { syncSessionsSelected } from "./sync-policy.js";
9
9
  import { hasLocalChanges, syncPolicyChanged } from "./sync-state.js";
10
10
  /** Existing legacy policy; service ownership/consent is established by its caller. */
11
11
  export async function pushSessionsOnShutdown(ctx, options, loaders = { operations: () => import("./sync-operations.js") }) {
12
+ const note = options.noteDebugOutcome;
12
13
  const validate = captureMutationOwner(ctx, options.signal);
13
14
  const config = await loadConfigForCheck(undefined, options.signal);
14
15
  validate();
@@ -25,23 +26,31 @@ export async function pushSessionsOnShutdown(ctx, options, loaders = { operation
25
26
  if (fingerprint !== syncCheckConfigFingerprint(latest))
26
27
  throw new Error("Settings changed before automatic session push.");
27
28
  };
28
- if (!config.automatic || !syncSessionsSelected(config))
29
+ if (!config.automatic || !syncSessionsSelected(config)) {
30
+ note?.("skipped");
29
31
  return;
32
+ }
30
33
  await withStateDirectoryAccess(() => withLock("auto-session-push", async () => {
31
34
  await refresh();
32
35
  const state = await readStateForConfig(config);
33
36
  await refresh();
34
- if ((config.scope || state.scope) && (!state.lastAppliedSnapshot || syncPolicyChanged(state, config)))
37
+ if ((config.scope || state.scope) && (!state.lastAppliedSnapshot || syncPolicyChanged(state, config))) {
38
+ note?.("blocked");
35
39
  return;
40
+ }
36
41
  const snapshotOptions = await resolvedSnapshotOptionsForContext(ctx, config, options.signal);
37
42
  await refresh();
38
43
  const local = await createSnapshot(config.snapshotIdentity, snapshotOptions);
39
44
  await refresh();
40
- if (!hasLocalChanges(local, state, config))
45
+ if (!hasLocalChanges(local, state, config)) {
46
+ note?.("skipped");
41
47
  return;
48
+ }
42
49
  const operations = await loaders.operations();
43
50
  await refresh();
44
- await operations.push(ctx, { ...options, auto: true, yes: true, silent: true, reload: false }, { config, state, local });
51
+ const result = await operations.push(ctx, { ...options, auto: true, yes: true, silent: true, reload: false }, { config, state, local });
45
52
  validate();
53
+ if (result === "cancelled")
54
+ note?.("cancelled");
46
55
  }));
47
56
  }
@@ -0,0 +1,105 @@
1
+ import { closeSync, constants, fstatSync, lstatSync, openSync, readSync } from "node:fs";
2
+ import path from "node:path";
3
+ import { readConsent } from "../../consent.js";
4
+ import { documentFingerprint } from "../../document.js";
5
+ import { MAX_SETTINGS_BYTES } from "../../protocol.js";
6
+ import { configFromSettings } from "../settings/config.js";
7
+ import { legacyLocalConfigPath, localConfigPath } from "../settings/config-file.js";
8
+ import { validateSettingsDocument } from "../settings/settings-validation.js";
9
+ import { stateDirectoryMigrationNotice } from "../state/state-directory.js";
10
+ import { statePathForConfig } from "../state/sync-state-store.js";
11
+ import { startupRecoveryEvidence } from "./startup-recovery-evidence.js";
12
+ function inspect(target) {
13
+ return lstatSync(target, { bigint: true, throwIfNoEntry: false });
14
+ }
15
+ function sameFile(before, now) {
16
+ return (now?.isFile() &&
17
+ now.dev === before.dev &&
18
+ now.ino === before.ino &&
19
+ now.size === before.size &&
20
+ now.mode === before.mode &&
21
+ now.ctimeNs === before.ctimeNs &&
22
+ now.mtimeNs === before.mtimeNs);
23
+ }
24
+ function readCanonicalSettings() {
25
+ const target = localConfigPath();
26
+ const mayNeedWrite = () => Boolean(inspect(legacyLocalConfigPath()) || inspect(`${target}.mutation-lock`));
27
+ if (mayNeedWrite())
28
+ return;
29
+ const before = inspect(target);
30
+ if (!before?.isFile() ||
31
+ before.size > MAX_SETTINGS_BYTES ||
32
+ (process.getuid && before.uid !== BigInt(process.getuid())) ||
33
+ (process.platform !== "win32" && (before.mode & 511n) !== 384n))
34
+ return;
35
+ const fd = openSync(target, constants.O_RDONLY | (constants.O_NOFOLLOW ?? 0) | (constants.O_NONBLOCK ?? 0));
36
+ try {
37
+ if (!sameFile(before, fstatSync(fd, { bigint: true })))
38
+ return;
39
+ const bytes = Buffer.alloc(Number(before.size) + 1);
40
+ let offset = 0;
41
+ while (offset < bytes.length) {
42
+ const count = readSync(fd, bytes, offset, bytes.length - offset, offset);
43
+ if (!count)
44
+ break;
45
+ offset += count;
46
+ }
47
+ if (offset !== Number(before.size) ||
48
+ !sameFile(before, fstatSync(fd, { bigint: true })) ||
49
+ !sameFile(before, inspect(target)) ||
50
+ mayNeedWrite())
51
+ return;
52
+ return {
53
+ bytes: bytes.subarray(0, offset),
54
+ fingerprint: documentFingerprint({
55
+ bytes: bytes.subarray(0, offset).toString("base64"),
56
+ identity: { dev: Number(before.dev), ino: Number(before.ino) },
57
+ }),
58
+ };
59
+ }
60
+ finally {
61
+ closeSync(fd);
62
+ }
63
+ }
64
+ export function captureStartupBootstrap(root, expected, signal, reviewOwnsLock) {
65
+ signal.throwIfAborted();
66
+ if (stateDirectoryMigrationNotice())
67
+ return;
68
+ const recovery = startupRecoveryEvidence(root, reviewOwnsLock);
69
+ if (recovery === undefined)
70
+ return;
71
+ const consent = readConsent(root);
72
+ if (!consent)
73
+ return;
74
+ const document = readCanonicalSettings();
75
+ if (document?.fingerprint !== expected)
76
+ return;
77
+ let parsed;
78
+ try {
79
+ parsed = JSON.parse(document.bytes.toString("utf8"));
80
+ }
81
+ catch {
82
+ return;
83
+ }
84
+ const settings = validateSettingsDocument(parsed);
85
+ if (!settings.activeSyncSetup)
86
+ return;
87
+ const config = configFromSettings(settings);
88
+ if (config.reservedIgnoreNames)
89
+ return;
90
+ const stateFile = statePathForConfig(config);
91
+ const noMergeJournal = () => {
92
+ const setupDirectory = inspect(path.dirname(stateFile));
93
+ return !setupDirectory || (setupDirectory.isDirectory() && !inspect(`${stateFile}.merge-journal.json`));
94
+ };
95
+ const consentToken = JSON.stringify(consent);
96
+ const validate = () => {
97
+ signal.throwIfAborted();
98
+ return (readCanonicalSettings()?.fingerprint === expected &&
99
+ !stateDirectoryMigrationNotice() &&
100
+ JSON.stringify(readConsent(root)) === consentToken &&
101
+ noMergeJournal() &&
102
+ startupRecoveryEvidence(root, reviewOwnsLock) === recovery);
103
+ };
104
+ return noMergeJournal() && startupRecoveryEvidence(root, reviewOwnsLock) === recovery ? validate : undefined;
105
+ }
@@ -0,0 +1,37 @@
1
+ import { lstatSync, opendirSync } from "node:fs";
2
+ import path from "node:path";
3
+ import { stateDir } from "../state/state-directory.js";
4
+ function stamp(info) {
5
+ return info && `${info.dev}:${info.ino}:${info.ctimeNs}:${info.mtimeNs}`;
6
+ }
7
+ function inspect(target) {
8
+ return lstatSync(target, { bigint: true, throwIfNoEntry: false });
9
+ }
10
+ export function startupRecoveryEvidence(root, reviewOwnsLock) {
11
+ const state = stateDir(root);
12
+ const transactions = path.join(state, "transactions");
13
+ const guard = path.join(root, ".pi-sync-state-migration.lock");
14
+ const lock = path.join(state, "lock");
15
+ const protectedState = () => state === stateDir(root) &&
16
+ !inspect(guard) &&
17
+ (reviewOwnsLock ? reviewOwnsLock() : !inspect(lock) && !inspect(`${lock}.guard`));
18
+ if (!protectedState())
19
+ return;
20
+ const parent = inspect(state);
21
+ const pending = inspect(transactions);
22
+ if (pending) {
23
+ if (!pending.isDirectory())
24
+ return;
25
+ const directory = opendirSync(transactions, { bufferSize: 1 });
26
+ try {
27
+ if (directory.readSync())
28
+ return;
29
+ }
30
+ finally {
31
+ directory.closeSync();
32
+ }
33
+ }
34
+ if (!protectedState() || stamp(parent) !== stamp(inspect(state)) || stamp(pending) !== stamp(inspect(transactions)))
35
+ return;
36
+ return JSON.stringify([state, stamp(parent), stamp(pending)]);
37
+ }
@@ -1,4 +1,68 @@
1
1
  import { performance } from "node:perf_hooks";
2
+ const startupStages = [
3
+ "daemon initialization",
4
+ "transaction recovery",
5
+ "settings read",
6
+ "config resolution",
7
+ "inspection import",
8
+ "inspection and guards",
9
+ "backend initialization",
10
+ "local snapshot",
11
+ "sync state read",
12
+ "remote head",
13
+ "operations import",
14
+ "automatic transfer",
15
+ "transfer authorization",
16
+ "transfer backend initialization",
17
+ "transfer journal read",
18
+ "transfer journal recovery",
19
+ "transfer state read",
20
+ "transfer local snapshot",
21
+ "transfer remote head",
22
+ "transfer snapshot download",
23
+ "transfer unchanged verification",
24
+ "transfer merge planning",
25
+ "transfer settings merge",
26
+ "transfer content merge",
27
+ "transfer preflight",
28
+ "transfer backup",
29
+ "transfer journal write",
30
+ "transfer remote publication",
31
+ "transfer local apply",
32
+ "transfer baseline write",
33
+ "transfer cleanup",
34
+ ];
35
+ const startupScopes = [
36
+ "local initialization",
37
+ "background startup",
38
+ "continuous startup",
39
+ "daemon inspection",
40
+ "daemon transfer",
41
+ ];
42
+ const outcomes = ["completed", "stopped"];
43
+ const unchangedLine = "transfer result: unchanged (fast path)";
44
+ const end = "(?![\\s\\S])";
45
+ const duration = "(?:0|[1-9][0-9]{0,15})\\.[0-9] ms";
46
+ const headingPattern = new RegExp(`^pi-sync debug: (?:${startupScopes.join("|")}) \\((?:${outcomes.join("|")})\\)${end}`, "u");
47
+ const stagePattern = new RegExp(`^(${startupStages.join("|")}): ${duration}(?: \\((?:[2-9]|[1-9][0-9]{1,15}) calls\\))?(?: \\(interrupted\\))?${end}`, "u");
48
+ const totalPattern = new RegExp(`^total: ${duration}${end}`, "u");
49
+ export function isStartupTimingReport(value) {
50
+ if (value.length > 4096)
51
+ return false;
52
+ const lines = value.split("\n");
53
+ if (!headingPattern.test(lines.shift() ?? "") || !totalPattern.test(lines.pop() ?? ""))
54
+ return false;
55
+ if (lines.at(-1) === unchangedLine)
56
+ lines.pop();
57
+ const seen = new Set();
58
+ for (const line of lines) {
59
+ const stage = stagePattern.exec(line)?.[1];
60
+ if (!stage || seen.has(stage))
61
+ return false;
62
+ seen.add(stage);
63
+ }
64
+ return true;
65
+ }
2
66
  /** Fixed labels and durations only: never retain errors, paths, settings, or payloads. */
3
67
  export function createStartupTimings(publishReport = (ctx, report) => ctx.ui.notify(report, "info")) {
4
68
  const started = performance.now();
@@ -8,6 +72,7 @@ export function createStartupTimings(publishReport = (ctx, report) => ctx.ui.not
8
72
  markUnchanged() {
9
73
  unchanged = true;
10
74
  },
75
+ wasUnchanged: () => unchanged,
11
76
  async measure(stage, run) {
12
77
  const start = performance.now();
13
78
  let completed = false;
@@ -25,16 +90,23 @@ export function createStartupTimings(publishReport = (ctx, report) => ctx.ui.not
25
90
  });
26
91
  }
27
92
  },
28
- publish(ctx, enabled, sessionSignal, scope, outcome = "completed") {
29
- if (!enabled || !ctx.hasUI || sessionSignal.aborted)
93
+ publish(ctx, enabled, sessionSignal, scope, outcome = "completed", observe) {
94
+ const display = enabled && ctx.hasUI && !sessionSignal.aborted;
95
+ if (!display && !observe)
30
96
  return;
97
+ const report = [
98
+ `pi-sync debug: ${scope} (${outcome})`,
99
+ ...Array.from(stages, ([stage, value]) => `${stage}: ${value.ms.toFixed(1)} ms${value.calls > 1 ? ` (${value.calls} calls)` : ""}${value.interrupted ? " (interrupted)" : ""}`),
100
+ ...(unchanged ? [unchangedLine] : []),
101
+ `total: ${(performance.now() - started).toFixed(1)} ms`,
102
+ ].join("\n");
103
+ try {
104
+ observe?.(report);
105
+ }
106
+ catch { }
31
107
  try {
32
- publishReport(ctx, [
33
- `pi-sync debug: ${scope} (${outcome})`,
34
- ...Array.from(stages, ([stage, value]) => `${stage}: ${value.ms.toFixed(1)} ms${value.calls > 1 ? ` (${value.calls} calls)` : ""}${value.interrupted ? " (interrupted)" : ""}`),
35
- ...(unchanged ? ["transfer result: unchanged (fast path)"] : []),
36
- `total: ${(performance.now() - started).toFixed(1)} ms`,
37
- ].join("\n"));
108
+ if (display)
109
+ publishReport(ctx, report);
38
110
  }
39
111
  catch {
40
112
  // Diagnostics must not change sync behavior if the UI is unavailable during teardown.
@@ -1,3 +1,4 @@
1
+ import { OWNERSHIP_CLEANUP_REQUIRED } from "../../protocol.js";
1
2
  import { errorMessage } from "./sync-errors.js";
2
3
  /** Add recovery advice without changing backend outcome semantics or printing error causes. */
3
4
  export function syncErrorGuidance(error) {
@@ -6,7 +7,11 @@ export function syncErrorGuidance(error) {
6
7
  const code = error?.code;
7
8
  const status = error?.status;
8
9
  let next;
9
- if (code === "ENOSPC") {
10
+ if (code === OWNERSHIP_CLEANUP_REQUIRED) {
11
+ next =
12
+ "Open /sync and choose Stop daemon for recovery, or run /sync daemon recover. Do not retry the transfer yet.";
13
+ }
14
+ else if (code === "ENOSPC") {
10
15
  next = "Free disk space for the private pi-sync settings/state directory, then retry.";
11
16
  }
12
17
  else if (code && ["EACCES", "EPERM", "EROFS"].includes(code)) {
@@ -1,18 +1,17 @@
1
- import { createHash } from "node:crypto";
2
1
  import { createSyncBackend } from "../backends/backend-factory.js";
3
- import { createSnapshot } from "../snapshot/snapshot.js";
2
+ import { observeSnapshot } from "../snapshot/snapshot.js";
4
3
  import { readStateForConfig, syncStateFingerprint } from "../state/sync-state-store.js";
5
4
  import { inspectHeadSelection } from "./remote-snapshot.js";
6
5
  import { throwIfAborted } from "./signals.js";
7
6
  import { createStartupTimings } from "./startup-timings.js";
8
7
  import { syncSelectionHasContent } from "./sync-policy.js";
9
- import { fileHashMap, hasLocalChanges, remoteChangedSinceState } from "./sync-state.js";
8
+ import { hasLocalChanges, rawSnapshotHashIdentity, remoteChangedSinceState } from "./sync-state.js";
10
9
  /** Advisory baseline-relative observation, never an authorization to transfer files. */
11
10
  export async function inspectSync(config, options, signal, factory = createSyncBackend, timings = createStartupTimings()) {
12
11
  throwIfAborted(signal);
13
12
  const backend = await timings.measure("backend initialization", () => factory(config));
14
13
  throwIfAborted(signal);
15
- const local = await timings.measure("local snapshot", () => createSnapshot(config.snapshotIdentity, { ...options, signal }));
14
+ const local = await timings.measure("local snapshot", () => observeSnapshot({ ...options, signal }, Boolean(config.localFields?.length)));
16
15
  throwIfAborted(signal);
17
16
  const state = await timings.measure("sync state read", () => readStateForConfig(config));
18
17
  throwIfAborted(signal);
@@ -21,9 +20,7 @@ export async function inspectSync(config, options, signal, factory = createSyncB
21
20
  const selectionState = head ? await inspectHeadSelection(backend, config, head, signal) : undefined;
22
21
  throwIfAborted(signal);
23
22
  const freshness = {
24
- localIdentity: createHash("sha256")
25
- .update(JSON.stringify(fileHashMap(local)))
26
- .digest("hex"),
23
+ localIdentity: rawSnapshotHashIdentity(local),
27
24
  };
28
25
  return {
29
26
  ...freshness,
@@ -19,7 +19,9 @@ export function protectedSessionPaths(ctx, sessionRoot = sessionDirFromContext(c
19
19
  export { captureMutationOwner } from "./mutation-owner.js";
20
20
  export async function backupLocal(profile, options = {}, signal) {
21
21
  throwIfAborted(signal);
22
- const snapshot = await createSnapshot(profile, { ...options, signal });
22
+ return backupSnapshot(await createSnapshot(profile, { ...options, signal }), signal);
23
+ }
24
+ export async function backupSnapshot(snapshot, signal) {
23
25
  throwIfAborted(signal);
24
26
  const backupDirectory = path.join(stateDir(), "backups");
25
27
  await fs.mkdir(backupDirectory, { recursive: true, mode: 0o700 });
@@ -422,6 +422,7 @@ export async function syncBoth(ctx, options, factory = createSyncBackend) {
422
422
  if (!options.auto && syncSelectionHasContent(config) && state.lastAppliedSnapshot)
423
423
  return mergeSync(ctx, options, factory);
424
424
  if (!syncSelectionHasContent(config)) {
425
+ options.noteDebugOutcome?.("skipped");
425
426
  if (!options.silent) {
426
427
  ctx.ui.notify(`Sync setup “${config.setupName}” includes no files. Choose included content in /sync Settings before syncing.`, "warning");
427
428
  }
@@ -512,7 +513,8 @@ export async function syncBoth(ctx, options, factory = createSyncBackend) {
512
513
  directMessage: "Remote settings match, but local and remote Pi sessions differ. Run /sync diff, then manually choose /sync pull or /sync push.",
513
514
  });
514
515
  }
515
- await pullInitialSource();
516
+ if ((await pullInitialSource()) === "cancelled")
517
+ options.noteDebugOutcome?.("cancelled");
516
518
  return;
517
519
  }
518
520
  await acceptMatchingState(remote);
@@ -531,16 +533,20 @@ export async function syncBoth(ctx, options, factory = createSyncBackend) {
531
533
  return;
532
534
  }
533
535
  if (remoteChanged) {
534
- await pullInitialSource();
536
+ if ((await pullInitialSource()) === "cancelled")
537
+ options.noteDebugOutcome?.("cancelled");
535
538
  return;
536
539
  }
537
540
  if (localChanged || !remote) {
538
- await push(ctx, options, { config, state, local, backend }, factory);
541
+ if ((await push(ctx, options, { config, state, local, backend }, factory)) === "cancelled")
542
+ options.noteDebugOutcome?.("cancelled");
539
543
  return;
540
544
  }
541
545
  if (shouldRefreshSyncedState(remote, head, state, config, (left, right) => backend.sameRevision(left, right))) {
542
546
  await acceptMatchingState(remote);
543
547
  }
548
+ else
549
+ options.noteDebugOutcome?.("skipped");
544
550
  if (!options.silent)
545
551
  ctx.ui.notify("pi-sync is already up to date.", "info");
546
552
  }
@@ -1,6 +1,6 @@
1
1
  import { createHash } from "node:crypto";
2
2
  import path from "node:path";
3
- import { isDeniedPath, toPosix } from "../paths.js";
3
+ import { DEBUG_LOG_FILENAME, isDebugLogPath, isDeniedPath, toPosix } from "../paths.js";
4
4
  import { IGNORE_SNAPSHOT_VERSION, } from "../snapshot/snapshot-types.js";
5
5
  import { compilePathRules, compileSyncExclude, normalizeSyncExclude, sameSyncExclude } from "./exclude-policy.js";
6
6
  export const BUILT_IN_SYNC_ROOTS = [
@@ -202,6 +202,14 @@ export function validateSnapshotSelection(snapshot) {
202
202
  if ((snapshot.version === IGNORE_SNAPSHOT_VERSION) !== (selection?.version === 4))
203
203
  throw new Error("Ignore-file policy requires snapshot version 6 and selection version 4.");
204
204
  }
205
+ export function assertSnapshotPathExcludesDebugLog(filePath) {
206
+ if (typeof filePath === "string" && isDebugLogPath(filePath))
207
+ throw new Error(`Snapshot contains the local-only ${DEBUG_LOG_FILENAME}. Review the old snapshot before retrying sync.`);
208
+ }
209
+ export function assertSnapshotExcludesDebugLog(snapshot) {
210
+ for (const file of snapshot.files)
211
+ assertSnapshotPathExcludesDebugLog(file?.path);
212
+ }
205
213
  export function requireLegacyTransferPolicy(snapshot) {
206
214
  if (snapshot.version === IGNORE_SNAPSHOT_VERSION || snapshot.selection?.version === 4)
207
215
  throw new Error("This setup cannot transfer an ignore-file snapshot without a reviewed local ignore policy.");
@@ -1,14 +1,17 @@
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
- import { portableSnapshot, sameLocalFields } from "./local-fields.js";
5
+ import { portableObservation, portableSnapshot, sameLocalFields } from "./local-fields.js";
5
6
  import { compareIgnorePolicy, configuredSnapshotPathMatcher, customIncludePathsByLower, includeFromSelectionConfig, localIgnorePolicyKey, normalizeSyncInclude, syncDirectoryPolicyKey, syncSessionsSelected, } from "./sync-policy.js";
6
7
  export function hasLocalChanges(local, state, config) {
7
- return ((Boolean(state.lastAppliedSnapshot) &&
8
+ if (state.lastAppliedSnapshot &&
8
9
  (state.scope !== config.scope ||
9
10
  !sameLocalFields(state.localFields, config.localFields) ||
10
- !sameSelectionRules(state, config))) ||
11
- !sameHashes(fileHashMap(portableSnapshot(local, config.localFields)), stateHashMapForConfig(state, config)));
11
+ !sameSelectionRules(state, config)))
12
+ return true;
13
+ const portable = "version" in local ? portableSnapshot(local, config.localFields) : portableObservation(local, config.localFields);
14
+ return !sameHashes(fileHashMap(portable), stateHashMapForConfig(state, config));
12
15
  }
13
16
  export function remoteChangedSinceState(head, state, config, sameRevision) {
14
17
  if (!head)
@@ -46,6 +49,11 @@ export function sameHashes(left, right) {
46
49
  export function fileHashMap(snapshot) {
47
50
  return Object.fromEntries(snapshot.files.map((file) => [file.path, file.sha256]));
48
51
  }
52
+ export function rawSnapshotHashIdentity(snapshot) {
53
+ return createHash("sha256")
54
+ .update(JSON.stringify(fileHashMap(snapshot)))
55
+ .digest("hex");
56
+ }
49
57
  function stateHashMapForConfig(state, config) {
50
58
  const includePaths = customIncludePathsByLower(includeFromSelectionConfig(config));
51
59
  const configured = configuredSnapshotPathMatcher(config);