@narumitw/pi-sync 0.60.1 → 0.60.4

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 (216) hide show
  1. package/CHANGELOG.md +37 -0
  2. package/README.md +9 -5
  3. package/dist/chunks/{chunk-7AC2RX4M.ts → chunk-277VHM2P.ts} +2 -2
  4. package/dist/chunks/chunk-277VHM2P.ts.map +7 -0
  5. package/dist/chunks/{chunk-UBE6XWOM.ts → chunk-6W2SNZ5P.ts} +2 -2
  6. package/dist/chunks/chunk-6W2SNZ5P.ts.map +7 -0
  7. package/dist/chunks/{chunk-JZKCOM62.ts → chunk-EMRFVLZM.ts} +2 -2
  8. package/dist/chunks/chunk-EMRFVLZM.ts.map +7 -0
  9. package/dist/chunks/{chunk-NPT2G4FU.ts → chunk-HEB6YT64.ts} +2 -2
  10. package/dist/chunks/chunk-HEB6YT64.ts.map +7 -0
  11. package/dist/chunks/{chunk-J5COV5TY.ts → chunk-HLCML3OW.ts} +2 -2
  12. package/dist/chunks/{chunk-J5COV5TY.ts.map → chunk-HLCML3OW.ts.map} +1 -1
  13. package/dist/chunks/{chunk-UZUUUY7C.ts → chunk-IGDI5CIH.ts} +4 -4
  14. package/dist/chunks/{chunk-ZMNVNECV.ts → chunk-KPWZNMKM.ts} +2 -2
  15. package/dist/chunks/chunk-KPWZNMKM.ts.map +7 -0
  16. package/dist/chunks/{chunk-YWEYRRQI.ts → chunk-LAXTCSX4.ts} +11 -11
  17. package/dist/chunks/chunk-LAXTCSX4.ts.map +7 -0
  18. package/dist/chunks/{chunk-BGGKHHO4.ts → chunk-MCUZFUHY.ts} +7 -7
  19. package/dist/chunks/{chunk-M7RHV3H6.ts → chunk-OA4NZLSL.ts} +2 -2
  20. package/dist/chunks/chunk-OA4NZLSL.ts.map +7 -0
  21. package/dist/chunks/{chunk-ENFHHB5O.ts → chunk-OVCP2SOB.ts} +42 -6
  22. package/dist/chunks/chunk-OVCP2SOB.ts.map +7 -0
  23. package/dist/chunks/{chunk-6LHIRS5R.ts → chunk-RJZIKZI3.ts} +6 -6
  24. package/dist/chunks/chunk-RJZIKZI3.ts.map +7 -0
  25. package/dist/chunks/{chunk-CEMFCWIU.ts → chunk-RNIEVDTR.ts} +2 -2
  26. package/dist/chunks/chunk-RNIEVDTR.ts.map +7 -0
  27. package/dist/chunks/{chunk-PDBTD2K6.ts → chunk-S3R3W3KF.ts} +2 -2
  28. package/dist/chunks/chunk-S3R3W3KF.ts.map +7 -0
  29. package/dist/chunks/{chunk-4RNCZZY7.ts → chunk-SE7HHBKH.ts} +15 -11
  30. package/dist/chunks/{chunk-4RNCZZY7.ts.map → chunk-SE7HHBKH.ts.map} +2 -2
  31. package/dist/chunks/{chunk-CZNBKIM5.ts → chunk-SMVKMYLK.ts} +2 -2
  32. package/dist/chunks/chunk-SMVKMYLK.ts.map +7 -0
  33. package/dist/chunks/{chunk-JKBUYCPC.ts → chunk-V4AMI3VL.ts} +2 -2
  34. package/dist/chunks/chunk-V4AMI3VL.ts.map +7 -0
  35. package/dist/chunks/{chunk-BWWTOQOW.ts → chunk-VQRDL3SY.ts} +310 -90
  36. package/dist/chunks/chunk-VQRDL3SY.ts.map +7 -0
  37. package/dist/chunks/{chunk-VZCWYXRS.ts → chunk-WP2M67XZ.ts} +2 -2
  38. package/dist/chunks/chunk-WP2M67XZ.ts.map +7 -0
  39. package/dist/chunks/{chunk-HNKLBM3U.ts → chunk-ZIL6PW5L.ts} +2 -2
  40. package/dist/chunks/chunk-ZIL6PW5L.ts.map +7 -0
  41. package/dist/chunks/{command-handler-FO4XBBHF.ts → command-handler-TMH4US44.ts} +23 -21
  42. package/dist/chunks/command-handler-TMH4US44.ts.map +7 -0
  43. package/dist/chunks/{conflict-review-XBSME2ER.ts → conflict-review-QGUCODDH.ts} +5 -5
  44. package/dist/chunks/{continuous-record-ZHUXVO4E.ts → continuous-record-3XDXDOMV.ts} +2 -2
  45. package/dist/chunks/continuous-record-3XDXDOMV.ts.map +7 -0
  46. package/dist/chunks/{continuous-storage-SQBAXACF.ts → continuous-storage-HLKCHF6Z.ts} +2 -2
  47. package/dist/chunks/continuous-storage-HLKCHF6Z.ts.map +7 -0
  48. package/dist/chunks/{file-selection-S6CNNNQI.ts → file-selection-DD6X4J2K.ts} +6 -6
  49. package/dist/chunks/manager-result-dispatcher-YVVD44QD.ts +18 -0
  50. package/dist/chunks/{manager-ui-AY5ZU6B4.ts → manager-ui-Q54OHOPR.ts} +26 -26
  51. package/dist/chunks/manager-ui-Q54OHOPR.ts.map +7 -0
  52. package/dist/chunks/remote-selection-ui-ODSUHX5N.ts +17 -0
  53. package/dist/chunks/{setup-switch-M3BAFGJM.ts → setup-switch-SPZE7HIQ.ts} +8 -8
  54. package/dist/chunks/{setup-wizard-NFAWP4KF.ts → setup-wizard-5JYKAEIT.ts} +11 -11
  55. package/dist/chunks/{snapshot-D4SFDP5J.ts → snapshot-5HY3M2NL.ts} +2 -2
  56. package/dist/chunks/snapshot-5HY3M2NL.ts.map +7 -0
  57. package/dist/chunks/{snapshot-transaction-directory-TNKBQ3UQ.ts → snapshot-transaction-directory-OU2D7R7A.ts} +2 -2
  58. package/dist/chunks/snapshot-transaction-directory-OU2D7R7A.ts.map +7 -0
  59. package/dist/chunks/{state-directory-JZSZVVPX.ts → state-directory-XXHU73Z3.ts} +2 -2
  60. package/dist/chunks/{sync-inspection-BJ4KPPNO.ts → sync-inspection-ECUTWISE.ts} +6 -6
  61. package/dist/chunks/sync-inspection-ECUTWISE.ts.map +7 -0
  62. package/dist/chunks/{sync-operations-TLWER2TT.ts → sync-operations-REXODWZB.ts} +6 -6
  63. package/dist/chunks/{sync-operations-TLWER2TT.ts.map → sync-operations-REXODWZB.ts.map} +2 -2
  64. package/dist/chunks/{sync-state-FVCJJ7MY.ts → sync-state-2LMIXJ2C.ts} +2 -2
  65. package/dist/chunks/sync-state-2LMIXJ2C.ts.map +7 -0
  66. package/dist/index.ts +74 -55
  67. package/dist/index.ts.map +3 -3
  68. package/docs/agent-mode-verification.md +1 -1
  69. package/docs/agent-quality-verification.md +1 -1
  70. package/docs/continuous-sync-verification.md +1 -1
  71. package/docs/daemon-migration.md +90 -10
  72. package/docs/tui-contract.md +7 -1
  73. package/package.json +3 -3
  74. package/skills/configure-pi-sync/references/configuration.md +9 -4
  75. package/src/backends/backend-factory.ts +2 -2
  76. package/src/backends/backend-identity.ts +2 -2
  77. package/src/backends/backend-types.ts +1 -1
  78. package/src/backends/git/git-backend.ts +2 -2
  79. package/src/backends/git/git-config.ts +2 -2
  80. package/src/backends/git/git-runner.ts +2 -2
  81. package/src/backends/git/git-storage.ts +2 -2
  82. package/src/backends/s3/s3-backend.ts +2 -2
  83. package/src/backends/s3/s3-client.ts +6 -2
  84. package/src/backends/s3/s3-conditions.ts +2 -2
  85. package/src/backends/snapshot-pointer.ts +2 -2
  86. package/src/backends/sync-backend.ts +2 -2
  87. package/src/backends/webdav/webdav-backend.ts +2 -2
  88. package/src/backends/webdav/webdav-client.ts +2 -2
  89. package/src/backends/webdav/webdav-config.ts +2 -2
  90. package/src/commands/command-execution.ts +2 -2
  91. package/src/commands/command-handler.ts +2 -0
  92. package/src/commands/command.ts +1 -1
  93. package/src/daemon/apply-gate.ts +1 -1
  94. package/src/daemon/pi-client.ts +335 -95
  95. package/src/daemon/recovery.ts +1 -1
  96. package/src/paths.ts +2 -2
  97. package/src/settings/config-errors.ts +2 -2
  98. package/src/settings/config-file.ts +2 -2
  99. package/src/settings/config.ts +2 -2
  100. package/src/settings/settings-management.ts +2 -2
  101. package/src/settings/settings-store.ts +2 -2
  102. package/src/settings/settings-types.ts +1 -1
  103. package/src/settings/settings-validation.ts +2 -2
  104. package/src/snapshot/session-paths.ts +2 -2
  105. package/src/snapshot/snapshot-apply.ts +2 -2
  106. package/src/snapshot/snapshot-case-replacement.ts +2 -2
  107. package/src/snapshot/snapshot-codec.ts +2 -2
  108. package/src/snapshot/snapshot-exclude-safety.ts +2 -2
  109. package/src/snapshot/snapshot-mutation-queues.ts +2 -2
  110. package/src/snapshot/snapshot-paths.ts +2 -2
  111. package/src/snapshot/snapshot-root-transition.ts +2 -2
  112. package/src/snapshot/snapshot-transaction-directory.ts +2 -2
  113. package/src/snapshot/snapshot-transaction-plan.ts +2 -2
  114. package/src/snapshot/snapshot-transaction.ts +2 -2
  115. package/src/snapshot/snapshot-types.ts +1 -1
  116. package/src/snapshot/snapshot.ts +2 -2
  117. package/src/state/json-file.ts +2 -2
  118. package/src/state/lock-policy.ts +2 -2
  119. package/src/state/lock.ts +2 -2
  120. package/src/state/lockfile-fs.ts +2 -2
  121. package/src/state/merge-baseline-store.ts +2 -2
  122. package/src/state/mutation-directory-sync.ts +2 -2
  123. package/src/state/operation-availability.ts +2 -2
  124. package/src/state/state-directory.ts +2 -2
  125. package/src/state/state-types.ts +1 -1
  126. package/src/state/sync-state-store.ts +2 -2
  127. package/src/sync/agent-candidate.ts +2 -2
  128. package/src/sync/agent-plan.ts +2 -2
  129. package/src/sync/agent-policy.ts +2 -2
  130. package/src/sync/agent-refusal-retry.ts +2 -2
  131. package/src/sync/agent-request.ts +2 -2
  132. package/src/sync/agent-runner.ts +2 -2
  133. package/src/sync/agent-store.ts +2 -2
  134. package/src/sync/agent-sync.ts +2 -2
  135. package/src/sync/apply-review.ts +2 -2
  136. package/src/sync/conflict-artifact-ownership.ts +2 -2
  137. package/src/sync/conflict-artifacts.ts +2 -2
  138. package/src/sync/conflict-groups.ts +2 -2
  139. package/src/sync/conflict-preview.ts +2 -2
  140. package/src/sync/conflict-resolution.ts +2 -2
  141. package/src/sync/conflict-retention.ts +2 -2
  142. package/src/sync/content-conflicts.ts +2 -2
  143. package/src/sync/continuous-errors.ts +2 -2
  144. package/src/sync/continuous-lease.ts +2 -2
  145. package/src/sync/continuous-record.ts +2 -2
  146. package/src/sync/continuous-storage.ts +2 -2
  147. package/src/sync/continuous-sync.ts +2 -2
  148. package/src/sync/continuous-watch.ts +2 -2
  149. package/src/sync/exclude-policy.ts +2 -2
  150. package/src/sync/field-migration.ts +2 -2
  151. package/src/sync/file-merge-planner.ts +6 -2
  152. package/src/sync/json-document.ts +2 -2
  153. package/src/sync/local-field-policy.ts +2 -2
  154. package/src/sync/local-fields.ts +2 -2
  155. package/src/sync/merge-apply.ts +2 -2
  156. package/src/sync/merge-journal.ts +2 -2
  157. package/src/sync/merge-transfer-error.ts +2 -2
  158. package/src/sync/merged-sync.ts +2 -2
  159. package/src/sync/mutation-owner.ts +2 -2
  160. package/src/sync/package-merge.ts +2 -2
  161. package/src/sync/partial-progress.ts +2 -2
  162. package/src/sync/remote-snapshot.ts +2 -2
  163. package/src/sync/session-merge.ts +2 -2
  164. package/src/sync/settings-conflicts.ts +2 -2
  165. package/src/sync/settings-merge.ts +2 -2
  166. package/src/sync/signals.ts +2 -2
  167. package/src/sync/startup-timings.ts +2 -2
  168. package/src/sync/sync-decision.ts +2 -2
  169. package/src/sync/sync-error-guidance.ts +2 -2
  170. package/src/sync/sync-errors.ts +2 -2
  171. package/src/sync/sync-inspection.ts +2 -2
  172. package/src/sync/sync-local.ts +6 -2
  173. package/src/sync/sync-mutations.ts +2 -2
  174. package/src/sync/sync-operations.ts +1 -1
  175. package/src/sync/sync-policy.ts +2 -2
  176. package/src/sync/sync-queries.ts +2 -2
  177. package/src/sync/sync-state.ts +2 -2
  178. package/src/sync/text-merge.ts +2 -2
  179. package/src/sync-extension.ts +62 -41
  180. package/src/ui/sync-status.ts +47 -4
  181. package/dist/chunks/chunk-6LHIRS5R.ts.map +0 -7
  182. package/dist/chunks/chunk-7AC2RX4M.ts.map +0 -7
  183. package/dist/chunks/chunk-BWWTOQOW.ts.map +0 -7
  184. package/dist/chunks/chunk-CEMFCWIU.ts.map +0 -7
  185. package/dist/chunks/chunk-CZNBKIM5.ts.map +0 -7
  186. package/dist/chunks/chunk-ENFHHB5O.ts.map +0 -7
  187. package/dist/chunks/chunk-HNKLBM3U.ts.map +0 -7
  188. package/dist/chunks/chunk-JKBUYCPC.ts.map +0 -7
  189. package/dist/chunks/chunk-JZKCOM62.ts.map +0 -7
  190. package/dist/chunks/chunk-M7RHV3H6.ts.map +0 -7
  191. package/dist/chunks/chunk-NPT2G4FU.ts.map +0 -7
  192. package/dist/chunks/chunk-PDBTD2K6.ts.map +0 -7
  193. package/dist/chunks/chunk-UBE6XWOM.ts.map +0 -7
  194. package/dist/chunks/chunk-VZCWYXRS.ts.map +0 -7
  195. package/dist/chunks/chunk-YWEYRRQI.ts.map +0 -7
  196. package/dist/chunks/chunk-ZMNVNECV.ts.map +0 -7
  197. package/dist/chunks/command-handler-FO4XBBHF.ts.map +0 -7
  198. package/dist/chunks/continuous-record-ZHUXVO4E.ts.map +0 -7
  199. package/dist/chunks/continuous-storage-SQBAXACF.ts.map +0 -7
  200. package/dist/chunks/manager-result-dispatcher-I2AMCCE7.ts +0 -18
  201. package/dist/chunks/manager-ui-AY5ZU6B4.ts.map +0 -7
  202. package/dist/chunks/remote-selection-ui-GZHPDQ4L.ts +0 -17
  203. package/dist/chunks/snapshot-D4SFDP5J.ts.map +0 -7
  204. package/dist/chunks/snapshot-transaction-directory-TNKBQ3UQ.ts.map +0 -7
  205. package/dist/chunks/sync-inspection-BJ4KPPNO.ts.map +0 -7
  206. package/dist/chunks/sync-state-FVCJJ7MY.ts.map +0 -7
  207. package/docs/settings.md +0 -90
  208. /package/dist/chunks/{chunk-UZUUUY7C.ts.map → chunk-IGDI5CIH.ts.map} +0 -0
  209. /package/dist/chunks/{chunk-BGGKHHO4.ts.map → chunk-MCUZFUHY.ts.map} +0 -0
  210. /package/dist/chunks/{conflict-review-XBSME2ER.ts.map → conflict-review-QGUCODDH.ts.map} +0 -0
  211. /package/dist/chunks/{file-selection-S6CNNNQI.ts.map → file-selection-DD6X4J2K.ts.map} +0 -0
  212. /package/dist/chunks/{manager-result-dispatcher-I2AMCCE7.ts.map → manager-result-dispatcher-YVVD44QD.ts.map} +0 -0
  213. /package/dist/chunks/{remote-selection-ui-GZHPDQ4L.ts.map → remote-selection-ui-ODSUHX5N.ts.map} +0 -0
  214. /package/dist/chunks/{setup-switch-M3BAFGJM.ts.map → setup-switch-SPZE7HIQ.ts.map} +0 -0
  215. /package/dist/chunks/{setup-wizard-NFAWP4KF.ts.map → setup-wizard-5JYKAEIT.ts.map} +0 -0
  216. /package/dist/chunks/{state-directory-JZSZVVPX.ts.map → state-directory-XXHU73Z3.ts.map} +0 -0
@@ -1,17 +1,19 @@
1
1
  // @generated by scripts/build-runtime.mjs; do not edit.
2
2
  // @ts-nocheck -- generated JavaScript uses a .ts extension for Pi's Jiti loader.
3
3
  import {
4
- setSyncStatus
5
- } from "./chunk-ENFHHB5O.ts";
4
+ setSyncStatus,
5
+ showSyncConnectionStatus
6
+ } from "./chunk-OVCP2SOB.ts";
6
7
  import {
7
- SyncDecisionRequiredError
8
- } from "./chunk-HNKLBM3U.ts";
8
+ SyncDecisionRequiredError,
9
+ errorMessage
10
+ } from "./chunk-ZIL6PW5L.ts";
9
11
  import {
10
12
  safeTerminalText
11
13
  } from "./chunk-2JNFEZ3V.ts";
12
14
  import {
13
15
  RemoteSelectionMismatchError
14
- } from "./chunk-JKBUYCPC.ts";
16
+ } from "./chunk-V4AMI3VL.ts";
15
17
 
16
18
  // src/daemon/pi-client.ts
17
19
  import { AsyncLocalStorage } from "node:async_hooks";
@@ -27,18 +29,21 @@ import {
27
29
  ensureDaemon,
28
30
  findDaemon as findDaemon2,
29
31
  MAX_SETTINGS_BYTES,
30
- modelSignature
31
- } from "@narumitw/sync-daemon";
32
- import { configureEngineHost } from "@narumitw/sync-daemon/engine/host";
33
- import { peekLocalConfigDocument } from "@narumitw/sync-daemon/engine/settings/config-file";
34
- import { validateSettingsDocument } from "@narumitw/sync-daemon/engine/settings/settings-validation";
35
- import { sessionDirFromContext } from "@narumitw/sync-daemon/engine/snapshot/session-paths";
32
+ modelSignature,
33
+ waitForDaemonExit
34
+ } from "@narumitw/pi-sync-daemon";
35
+ import { configureEngineHost } from "@narumitw/pi-sync-daemon/engine/host";
36
+ import {
37
+ peekLocalConfigDocument
38
+ } from "@narumitw/pi-sync-daemon/engine/settings/config-file";
39
+ import { validateSettingsDocument } from "@narumitw/pi-sync-daemon/engine/settings/settings-validation";
40
+ import { sessionDirFromContext } from "@narumitw/pi-sync-daemon/engine/snapshot/session-paths";
36
41
 
37
42
  // src/daemon/apply-gate.ts
38
43
  import { randomUUID } from "node:crypto";
39
44
  import path from "node:path";
40
45
  import { withFileMutationQueue } from "@earendil-works/pi-coding-agent";
41
- import { withTargetQueues } from "@narumitw/sync-daemon/engine/snapshot/snapshot-mutation-queues";
46
+ import { withTargetQueues } from "@narumitw/pi-sync-daemon/engine/snapshot/snapshot-mutation-queues";
42
47
  var PiApplyGate = class {
43
48
  constructor(validate) {
44
49
  this.validate = validate;
@@ -162,7 +167,7 @@ var PiApplyGate = class {
162
167
  };
163
168
 
164
169
  // src/daemon/recovery.ts
165
- import { DaemonConnection, findDaemon } from "@narumitw/sync-daemon";
170
+ import { DaemonConnection, findDaemon } from "@narumitw/pi-sync-daemon";
166
171
  var recoveries = /* @__PURE__ */ new Set();
167
172
  function alive(pid) {
168
173
  try {
@@ -246,13 +251,13 @@ function currentPiDaemonClient() {
246
251
  return current();
247
252
  }
248
253
  function current() {
249
- const client = scope.getStore();
254
+ const client = scope.getStore()?.client;
250
255
  if (!client) throw new Error("Pi synchronization requires a current session-bound daemon client.");
251
256
  client.validate();
252
257
  return client;
253
258
  }
254
259
  function canonicalPath(value) {
255
- if (value === void 0) return void 0;
260
+ if (value === void 0 || value === "") return void 0;
256
261
  if (!path2.isAbsolute(value)) throw new Error("Pi session paths must be absolute.");
257
262
  const parts = [];
258
263
  let cursor = path2.resolve(value);
@@ -316,10 +321,10 @@ var authority = {
316
321
  };
317
322
  function installPiDaemonHost() {
318
323
  configureEngineHost({
319
- root: () => scope.getStore()?.root ?? path2.resolve(getAgentDir()),
324
+ root: () => scope.getStore()?.client.root ?? path2.resolve(getAgentDir()),
320
325
  config: authority,
321
326
  mutate: async () => {
322
- throw new Error("File mutation belongs to sync-daemon; no in-process fallback is permitted.");
327
+ throw new Error("File mutation belongs to pi-sync-daemon; no in-process fallback is permitted.");
323
328
  },
324
329
  review: async () => {
325
330
  throw new Error("Engine reviews must arrive through the daemon API.");
@@ -337,8 +342,8 @@ function piDaemonClient(ctx) {
337
342
  client.validate();
338
343
  return client;
339
344
  }
340
- function withPiDaemonClient(ctx, action) {
341
- const client = piDaemonClient(ctx);
345
+ function withPiDaemonClient(ctx, action, reload) {
346
+ const owner = { client: piDaemonClient(ctx), reload };
342
347
  const ui = new Proxy(ctx.ui, {
343
348
  get(target, property) {
344
349
  if (property !== "custom") return Reflect.get(target, property, target);
@@ -352,14 +357,14 @@ function withPiDaemonClient(ctx, action) {
352
357
  if (typeof value !== "function") return value;
353
358
  const saved = methods.get(key);
354
359
  if (saved?.source === value) return saved.bound;
355
- const bound = (...values) => scope.run(client, () => Reflect.apply(value, original, values));
360
+ const bound = (...values) => scope.run(owner, () => Reflect.apply(value, original, values));
356
361
  methods.set(key, { source: value, bound });
357
362
  return bound;
358
363
  }
359
364
  });
360
365
  };
361
366
  const invoke = (...provided) => {
362
- const component = scope.run(client, () => factory(...provided));
367
+ const component = scope.run(owner, () => factory(...provided));
363
368
  return component instanceof Promise ? component.then(wrap) : wrap(component);
364
369
  };
365
370
  return target.custom(
@@ -374,11 +379,14 @@ function withPiDaemonClient(ctx, action) {
374
379
  return key === "ui" ? ui : Reflect.get(target, key, target);
375
380
  }
376
381
  });
377
- return scope.run(client, () => action(owned));
382
+ return scope.run(owner, () => action(owned));
378
383
  }
379
384
  async function remoteOperation(ctx, operation, options) {
380
385
  const client = piDaemonClient(ctx);
381
386
  const { signal, onCommit, args, setup } = options;
387
+ const owner = scope.getStore();
388
+ const reload = options.reload && owner?.client === client ? owner.reload : void 0;
389
+ reload?.signal.throwIfAborted();
382
390
  const payload = {
383
391
  args,
384
392
  ...setup === void 0 ? {} : { setup },
@@ -386,10 +394,13 @@ async function remoteOperation(ctx, operation, options) {
386
394
  force: options.force,
387
395
  stale: options.stale,
388
396
  silent: options.silent,
389
- reload: options.reload,
397
+ reload: reload !== void 0,
390
398
  ...options.agentRetryRefusal ? { agentRetryRefusal: true } : {}
391
399
  };
392
- return client.run(operation, payload, signal, ctx, onCommit);
400
+ return client.run(operation, payload, signal, ctx, onCommit, reload);
401
+ }
402
+ function remoteErrorNamed(error, name) {
403
+ return error instanceof DaemonError && error.remote.name === name;
393
404
  }
394
405
  var PiDaemonClient = class {
395
406
  constructor(ctx, signal, publish) {
@@ -408,21 +419,32 @@ var PiDaemonClient = class {
408
419
  signal;
409
420
  publish;
410
421
  connection;
422
+ protectionOnly = false;
411
423
  partialConnection;
412
424
  work = Promise.resolve();
413
425
  closing = false;
414
426
  paused = false;
415
427
  pausing;
416
428
  connecting;
429
+ contactRepair;
430
+ contactAttempt;
431
+ contactFactsUnpublished = false;
432
+ clearContactStatus;
417
433
  disposed = false;
418
434
  disposing;
419
435
  uiContext;
420
- reloadRequested = false;
436
+ reloadRequest;
437
+ pendingReload;
421
438
  busy = /* @__PURE__ */ new Set();
422
439
  initialized = false;
423
440
  startupDebug = false;
424
- id = randomUUID2();
425
- generation = randomUUID2();
441
+ participantIdentity = { id: randomUUID2(), generation: randomUUID2() };
442
+ get id() {
443
+ return this.participantIdentity.id;
444
+ }
445
+ get generation() {
446
+ return this.participantIdentity.generation;
447
+ }
426
448
  root;
427
449
  rawRoot;
428
450
  gate;
@@ -445,7 +467,7 @@ var PiDaemonClient = class {
445
467
  return void 0;
446
468
  return { provider: model.provider, id: model.id, signature: modelSignature(model), authSource: source };
447
469
  }
448
- facts() {
470
+ facts(protectionOnly = this.protectionOnly) {
449
471
  const manager = this.ctx.sessionManager;
450
472
  return {
451
473
  id: this.id,
@@ -453,14 +475,14 @@ var PiDaemonClient = class {
453
475
  pid: process.pid,
454
476
  cwd: this.ctx.cwd,
455
477
  mode: this.ctx.mode,
456
- hasUI: this.ctx.hasUI,
457
- idle: !this.paused && !this.busy.size && this.ctx.isIdle(),
458
- ready: this.initialized,
478
+ hasUI: !protectionOnly && this.ctx.hasUI,
479
+ idle: !protectionOnly && !this.paused && !this.busy.size && this.ctx.isIdle(),
480
+ ready: !protectionOnly && this.initialized,
459
481
  startupDebug: this.startupDebug,
460
482
  sessionFile: canonicalPath(manager.getSessionFile?.()),
461
483
  sessionId: manager.getSessionId?.(),
462
484
  sessionDir: canonicalPath(sessionDirFromContext(this.ctx)),
463
- model: this.modelBinding()
485
+ model: protectionOnly ? void 0 : this.modelBinding()
464
486
  };
465
487
  }
466
488
  async configured() {
@@ -474,6 +496,7 @@ var PiDaemonClient = class {
474
496
  async pauseForSessionChange() {
475
497
  this.validate();
476
498
  this.paused = true;
499
+ this.stopContactRepair(true);
477
500
  this.gate.cancelPending();
478
501
  this.pausing ??= (async () => {
479
502
  const connection = this.connection ?? this.partialConnection ?? await this.connecting?.catch(() => void 0);
@@ -484,70 +507,246 @@ var PiDaemonClient = class {
484
507
  })();
485
508
  return this.pausing;
486
509
  }
487
- async attachExisting() {
510
+ async attachExisting({ allowUpgrade = false } = {}) {
488
511
  this.validate();
489
512
  const address = await findDaemon2(this.root);
490
513
  this.validate();
491
- if (address) await this.connect(address);
514
+ if (!address) return false;
515
+ const upgrade = allowUpgrade && address.needsUpgrade && this.ctx.hasUI && ["tui", "rpc"].includes(this.ctx.mode);
516
+ let upgradeFailure;
517
+ if (upgrade) {
518
+ try {
519
+ await this.connect();
520
+ this.validate();
521
+ return true;
522
+ } catch (error) {
523
+ this.validate();
524
+ if (this.paused) throw error;
525
+ upgradeFailure = safeTerminalText(errorMessage(error));
526
+ }
527
+ }
528
+ try {
529
+ await this.connect(address);
530
+ } catch (error) {
531
+ this.validate();
532
+ if (remoteErrorNamed(error, "DaemonStoppingError")) return false;
533
+ throw error;
534
+ } finally {
535
+ if (upgradeFailure !== void 0) {
536
+ this.validate();
537
+ this.ctx.ui.notify(`pi-sync daemon upgrade failed: ${upgradeFailure}`, "error");
538
+ }
539
+ }
492
540
  this.validate();
493
- return Boolean(address);
541
+ return !this.protectionOnly;
494
542
  }
495
543
  async finishReattachment() {
496
544
  this.validate();
497
545
  if (this.connection) {
498
546
  await recoverDetachedClients({ paths: this.connection.paths, identity: this.connection.identity });
499
547
  this.validate();
500
- await this.connection.attach();
548
+ await this.connection.attach(this.signal);
501
549
  this.validate();
502
550
  }
503
551
  }
504
- async connect(existing) {
505
- this.validate();
506
- if (this.connection) return this.connection;
552
+ async connect(existing, requestSignal) {
553
+ const startupSignal = requestSignal ? AbortSignal.any([this.signal, requestSignal]) : this.signal;
554
+ const validateStartup = () => {
555
+ this.validate();
556
+ startupSignal.throwIfAborted();
557
+ };
558
+ validateStartup();
559
+ if (this.connection) {
560
+ if (!existing && this.protectionOnly)
561
+ throw new Error(
562
+ "This session is only protecting its files on an older Pi Sync daemon. Use /reload to upgrade."
563
+ );
564
+ return this.connection;
565
+ }
507
566
  this.connecting ??= (async () => {
567
+ if (this.partialConnection) {
568
+ await this.partialConnection.dispose();
569
+ await this.gate.releaseAfterDrain();
570
+ validateStartup();
571
+ this.partialConnection = void 0;
572
+ this.participantIdentity = { id: randomUUID2(), generation: randomUUID2() };
573
+ }
508
574
  if (!existing) await fs.mkdir(this.root, { recursive: true, mode: 448 });
509
- this.validate();
510
- const daemon = existing ?? await ensureDaemon(this.root, this.signal);
511
- this.validate();
512
- if (daemon.paths.root !== this.root) throw new Error("Daemon root changed before attach.");
513
- const connection = new DaemonConnection2(daemon.paths, daemon.identity, this.facts(), {
514
- callback: (callback, signal) => this.callback(callback, signal),
515
- event: (event) => {
516
- if (event.kind === "commit") return;
517
- if (this.signal.aborted || this.disposed || this.closing || clients.get(this.manager) !== this) return;
518
- const payload = event.payload;
519
- if (event.kind === "notify" && typeof payload?.message === "string")
520
- this.uiContext.ui.notify(safeTerminalText(payload.message), payload.level ?? "info");
521
- if (event.kind === "timings" && typeof event.payload?.report === "string") {
522
- this.publish?.(this.ctx, safeTerminalText(event.payload.report));
575
+ validateStartup();
576
+ let retried = false;
577
+ for (; ; ) {
578
+ validateStartup();
579
+ const daemon = existing ?? await ensureDaemon(this.root, startupSignal);
580
+ validateStartup();
581
+ if (daemon.paths.root !== this.root) throw new Error("Daemon root changed before attach.");
582
+ const protectionOnly = daemon.needsUpgrade === true;
583
+ const connection2 = new DaemonConnection2(daemon.paths, daemon.identity, this.facts(protectionOnly), {
584
+ callback: (callback, signal) => this.callback(callback, signal, protectionOnly),
585
+ event: (event) => {
586
+ if (event.kind === "commit") return;
587
+ if (this.signal.aborted || this.disposed || this.closing || clients.get(this.manager) !== this) return;
588
+ const payload = event.payload;
589
+ if (event.kind === "notify" && typeof payload?.message === "string")
590
+ this.uiContext.ui.notify(safeTerminalText(payload.message), payload.level ?? "info");
591
+ if (event.kind === "timings" && typeof payload?.report === "string") {
592
+ this.publish?.(this.ctx, payload.report.split("\n").map(safeTerminalText).join("\n"));
593
+ }
594
+ if (event.kind === "status")
595
+ setSyncStatus(
596
+ this.uiContext,
597
+ typeof payload?.value === "string" ? safeTerminalText(payload.value) : void 0,
598
+ payload.presentation
599
+ );
600
+ },
601
+ contact: (state) => this.observeContact(connection2, state),
602
+ error: () => {
603
+ if (this.ownsContactUI()) this.showContactStatus("sync service unavailable", "blocked");
523
604
  }
524
- if (event.kind === "status")
525
- setSyncStatus(
526
- this.uiContext,
527
- typeof payload?.value === "string" ? safeTerminalText(payload.value) : void 0,
528
- payload.presentation
529
- );
530
- },
531
- error: () => {
532
- if (!this.signal.aborted && !this.disposed && !this.closing && clients.get(this.manager) === this)
533
- setSyncStatus(this.ctx, "sync service unavailable", { activity: "blocked", reason: "repair" });
605
+ });
606
+ this.partialConnection = connection2;
607
+ try {
608
+ await connection2.attach(startupSignal);
609
+ } catch (error) {
610
+ if (!(error instanceof DaemonError) || error.remote.name !== "DaemonStoppingError") throw error;
611
+ await connection2.dispose().catch(() => {
612
+ });
613
+ this.partialConnection = void 0;
614
+ validateStartup();
615
+ if (existing || retried) throw error;
616
+ retried = true;
617
+ await waitForDaemonExit(daemon.identity, startupSignal);
618
+ validateStartup();
619
+ continue;
534
620
  }
535
- });
536
- this.partialConnection = connection;
537
- await connection.attach();
538
- this.validate();
539
- await recoverDetachedClients(daemon);
540
- if (this.signal.aborted || this.disposed || this.closing) {
541
- await connection.dispose();
542
621
  this.validate();
622
+ await recoverDetachedClients(daemon);
623
+ if (this.signal.aborted || this.disposed || this.closing) {
624
+ await connection2.dispose();
625
+ this.validate();
626
+ }
627
+ this.validate();
628
+ this.connection = connection2;
629
+ this.protectionOnly = protectionOnly;
630
+ this.partialConnection = void 0;
631
+ return connection2;
543
632
  }
544
- this.connection = connection;
545
- return connection;
546
633
  })().catch((error) => {
547
634
  this.connecting = void 0;
548
635
  throw error;
549
636
  });
550
- return this.connecting;
637
+ const connection = await this.connecting;
638
+ validateStartup();
639
+ if (!existing && this.protectionOnly)
640
+ throw new Error("This session is only protecting its files on an older Pi Sync daemon. Use /reload to upgrade.");
641
+ return connection;
642
+ }
643
+ ownsContactUI() {
644
+ return !this.signal.aborted && !this.disposed && !this.closing && !this.paused && clients.get(this.manager) === this;
645
+ }
646
+ showContactStatus(text, activity) {
647
+ const previous = this.clearContactStatus;
648
+ this.clearContactStatus = showSyncConnectionStatus(this.ctx, text, {
649
+ activity,
650
+ reason: activity === "retry" ? "temp" : "repair"
651
+ });
652
+ previous?.();
653
+ }
654
+ stopContactRepair(redraw) {
655
+ this.contactRepair?.controller.abort(new DOMException("Pi session is changing", "AbortError"));
656
+ this.clearContactStatus?.(redraw);
657
+ this.clearContactStatus = void 0;
658
+ }
659
+ observeContact(connection, state) {
660
+ if (connection !== this.connection || !this.ownsContactUI()) return;
661
+ if (state === "active") {
662
+ if (this.contactRepair || this.contactFactsUnpublished || connection.contactResumeState === "uncertain") return;
663
+ this.clearRestoredContact();
664
+ } else if (state === "expired" && connection.facts.hasUI && !this.contactAttempt && !this.contactRepair) {
665
+ if (this.gate.size || connection.hasPendingWork) return;
666
+ void this.resumeContact(connection).catch(() => {
667
+ });
668
+ }
669
+ }
670
+ clearRestoredContact() {
671
+ this.contactAttempt = void 0;
672
+ this.contactFactsUnpublished = false;
673
+ this.clearContactStatus?.();
674
+ this.clearContactStatus = void 0;
675
+ }
676
+ async resumeContact(connection) {
677
+ this.validate();
678
+ if (this.paused || this.connection !== connection) throw new Error("Pi Sync is paused for a session change.");
679
+ if (this.contactRepair) return this.contactRepair.done;
680
+ if (connection.contactState === "active" && connection.contactResumeState !== "uncertain") return;
681
+ if (this.gate.size || connection.hasPendingWork || connection.contactResumeState === "uncertain") {
682
+ throw new Error(
683
+ "Pi Sync cannot reconnect while previous work is still running or uncertain. Check sync recovery before retrying."
684
+ );
685
+ }
686
+ const earlier = this.contactAttempt;
687
+ if (connection.contactState !== "expired" || earlier) {
688
+ throw new Error("Pi Sync could not restore contact. Review pending work before using /reload.", {
689
+ cause: earlier?.error
690
+ });
691
+ }
692
+ const controller = new AbortController();
693
+ const signal = AbortSignal.any([this.signal, controller.signal]);
694
+ const attempt2 = {};
695
+ this.contactAttempt = attempt2;
696
+ const done = Promise.resolve().then(async () => {
697
+ try {
698
+ this.validate();
699
+ signal.throwIfAborted();
700
+ this.showContactStatus("sync reconnecting", "retry");
701
+ await connection.resumeContact({ ...this.facts(), idle: false, ready: false }, signal);
702
+ this.validate();
703
+ signal.throwIfAborted();
704
+ this.contactFactsUnpublished = true;
705
+ this.contactAttempt = void 0;
706
+ await connection.update(this.facts());
707
+ this.validate();
708
+ signal.throwIfAborted();
709
+ this.confirmContact(connection);
710
+ } catch (error) {
711
+ attempt2.error = error;
712
+ if (this.ownsContactUI() && !signal.aborted)
713
+ this.showContactStatus("sync reconnect blocked; check /sync daemon status", "blocked");
714
+ throw error;
715
+ } finally {
716
+ if (this.contactRepair?.controller === controller) this.contactRepair = void 0;
717
+ }
718
+ });
719
+ this.contactRepair = { controller, done };
720
+ return done;
721
+ }
722
+ async publishFacts(connection) {
723
+ if (this.contactRepair) await this.contactRepair.done;
724
+ this.validate();
725
+ if (this.paused) throw new Error("Pi Sync is paused for a session change.");
726
+ if (connection.contactResumeState === "uncertain")
727
+ throw new Error("Pi Sync cannot verify the previous reconnect. Review pending work before using /reload.");
728
+ try {
729
+ await connection.update(this.facts());
730
+ } catch (error) {
731
+ if (!connection.facts.hasUI || !remoteErrorNamed(error, "ClientContactExpiredError")) throw error;
732
+ await this.resumeContact(connection);
733
+ this.validate();
734
+ await connection.update(this.facts());
735
+ }
736
+ this.validate();
737
+ this.confirmContact(connection);
738
+ }
739
+ confirmContact(connection) {
740
+ if (connection.contactState === "expired" || connection.contactState === "inactive")
741
+ throw new Error("Pi Sync did not confirm active participation in the daemon.");
742
+ this.clearRestoredContact();
743
+ }
744
+ async verifyConnection(signal) {
745
+ const connection = await this.connect(void 0, signal);
746
+ signal?.throwIfAborted();
747
+ await this.publishFacts(connection);
748
+ signal?.throwIfAborted();
749
+ return connection;
551
750
  }
552
751
  async readSettings(repair = false) {
553
752
  this.validate();
@@ -561,8 +760,8 @@ var PiDaemonClient = class {
561
760
  }
562
761
  return decode(await this.run(repair ? "settings.repair-read" : "settings.read", {}));
563
762
  }
564
- run(operation, payload, signal, ctx = this.ctx, onCommit) {
565
- const result = this.work.then(() => this.runCurrent(operation, payload, signal, ctx, onCommit));
763
+ run(operation, payload, signal, ctx = this.ctx, onCommit, reload) {
764
+ const result = this.work.then(() => this.runCurrent(operation, payload, signal, ctx, onCommit, reload));
566
765
  this.work = result.then(
567
766
  () => {
568
767
  },
@@ -571,30 +770,42 @@ var PiDaemonClient = class {
571
770
  );
572
771
  return result;
573
772
  }
574
- async runCurrent(operation, payload, signal, ctx = this.ctx, onCommit) {
773
+ async runCurrent(operation, payload, signal, ctx = this.ctx, onCommit, reload) {
575
774
  this.validate();
576
775
  signal?.throwIfAborted();
776
+ reload?.signal.throwIfAborted();
777
+ if (this.pendingReload) throw new Error("Pi resources are about to reload.");
577
778
  if (this.paused && operation !== "shutdown")
578
779
  throw new Error("Synchronization is paused for a session change; if it was cancelled, use /reload to reattach.");
579
- const connection = await this.connect();
780
+ const connection = await this.connect(void 0, signal);
580
781
  if (this.paused && operation === "shutdown") {
581
782
  Object.assign(connection.facts, { ...this.facts(), idle: false, ready: false });
582
- await connection.attach();
783
+ await connection.attach(signal ? AbortSignal.any([signal, this.signal]) : this.signal);
583
784
  this.validate();
584
785
  }
585
786
  this.validate();
586
787
  signal?.throwIfAborted();
587
- await connection.update(this.facts());
788
+ if (this.paused)
789
+ await connection.update(this.facts());
790
+ else await this.publishFacts(connection);
588
791
  this.validate();
589
792
  signal?.throwIfAborted();
590
793
  this.uiContext = ctx;
794
+ const request = reload ? { received: false } : void 0;
795
+ this.reloadRequest = request;
591
796
  try {
592
- return await connection.run(
797
+ const result = await connection.run(
593
798
  operation,
594
799
  payload,
595
800
  signal ? AbortSignal.any([signal, this.signal]) : this.signal,
596
801
  onCommit
597
802
  );
803
+ if (request?.received && reload && !this.paused && !reload.signal.aborted && !signal?.aborted) {
804
+ this.validate();
805
+ this.pendingReload = reload;
806
+ reload.abort(new DOMException("Pi resource reload requested", "AbortError"));
807
+ }
808
+ return result;
598
809
  } catch (error) {
599
810
  if (error instanceof DaemonError) {
600
811
  const prototype = error.name === "RemoteSelectionMismatchError" ? RemoteSelectionMismatchError.prototype : error.name === "SyncDecisionRequiredError" ? SyncDecisionRequiredError.prototype : void 0;
@@ -608,12 +819,17 @@ var PiDaemonClient = class {
608
819
  throw error;
609
820
  } finally {
610
821
  this.uiContext = this.ctx;
611
- if (this.reloadRequested && !this.signal.aborted && !this.disposed && !this.closing && clients.get(this.manager) === this) {
612
- this.reloadRequested = false;
613
- if ("reload" in ctx && typeof ctx.reload === "function") await ctx.reload();
614
- }
822
+ this.reloadRequest = void 0;
615
823
  }
616
824
  }
825
+ canReload(owner) {
826
+ if (this.pendingReload !== owner || this.paused) return false;
827
+ this.validate();
828
+ return true;
829
+ }
830
+ releaseReload(owner) {
831
+ if (this.pendingReload === owner) this.pendingReload = void 0;
832
+ }
617
833
  async markReady(debug) {
618
834
  this.validate();
619
835
  this.startupDebug = debug;
@@ -621,18 +837,18 @@ var PiDaemonClient = class {
621
837
  await this.update();
622
838
  this.validate();
623
839
  }
624
- async setBusy(reason, busy) {
840
+ async setBusy(reason, busy, publish = true) {
625
841
  this.validate();
626
842
  if (busy) this.busy.add(reason);
627
843
  else this.busy.delete(reason);
628
- await this.update();
844
+ if (publish) await this.update();
629
845
  this.validate();
630
846
  }
631
847
  async update() {
632
848
  if (this.connection && !this.paused && !this.disposed && !this.signal.aborted)
633
- await this.connection.update(this.facts());
849
+ await this.publishFacts(this.connection);
634
850
  }
635
- async callback(callback, signal) {
851
+ async callback(callback, signal, protectionOnly) {
636
852
  const payload = callback.payload;
637
853
  if (callback.kind === "queue-release") {
638
854
  if (typeof payload?.reservation !== "string") throw new Error("Invalid queue release.");
@@ -643,6 +859,7 @@ var PiDaemonClient = class {
643
859
  signal.throwIfAborted();
644
860
  if (this.paused) throw new Error("Pi session is changing; no new work is authorized.");
645
861
  if (callback.kind === "queue-acquire") {
862
+ if (this.pendingReload) throw new Error("Pi resources are about to reload.");
646
863
  if (payload.background && !this.facts().idle) throw new Error("Pi is busy; background apply was not granted.");
647
864
  if (payload.generation !== this.generation || typeof payload.reservation !== "string" || !Array.isArray(payload.targets))
648
865
  throw new Error("Stale file-queue request.");
@@ -651,6 +868,7 @@ var PiDaemonClient = class {
651
868
  const ctx = this.uiContext;
652
869
  const combined = AbortSignal.any([signal, this.signal]);
653
870
  if (callback.kind === "model-binding") {
871
+ if (protectionOnly) return null;
654
872
  const facts = this.facts();
655
873
  const connection = this.connection ?? this.partialConnection;
656
874
  if (connection && JSON.stringify(connection.facts.model) !== JSON.stringify(facts.model)) {
@@ -660,7 +878,8 @@ var PiDaemonClient = class {
660
878
  return payload.background && !facts.idle ? null : facts.model ?? null;
661
879
  }
662
880
  if (callback.kind === "reload") {
663
- this.reloadRequested = true;
881
+ if (!this.reloadRequest) throw new Error("No active Pi command can reload resources.");
882
+ this.reloadRequest.received = true;
664
883
  return true;
665
884
  }
666
885
  if (!ctx.hasUI || !["tui", "rpc"].includes(ctx.mode)) throw new Error("This client cannot present safety reviews.");
@@ -704,6 +923,7 @@ var PiDaemonClient = class {
704
923
  async dispose(handoff) {
705
924
  if (this.disposing) return this.disposing;
706
925
  this.closing = true;
926
+ this.stopContactRepair(false);
707
927
  this.disposing = (async () => {
708
928
  const connection = this.connection ?? this.partialConnection ?? await this.connecting?.catch(() => void 0);
709
929
  let confirmed = false;
@@ -734,4 +954,4 @@ export {
734
954
  withPiDaemonClient,
735
955
  remoteOperation
736
956
  };
737
- //# sourceMappingURL=chunk-BWWTOQOW.ts.map
957
+ //# sourceMappingURL=chunk-VQRDL3SY.ts.map