@narumitw/pi-sync-daemon 0.9.0 → 0.10.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 (102) hide show
  1. package/CHANGELOG.md +19 -0
  2. package/README.md +11 -1
  3. package/dist/cli.js +2 -0
  4. package/dist/client-transport.d.ts +14 -0
  5. package/dist/client-transport.js +66 -0
  6. package/dist/client.d.ts +15 -8
  7. package/dist/client.js +116 -86
  8. package/dist/consent.js +33 -11
  9. package/dist/daemon-debug.d.ts +3 -0
  10. package/dist/daemon-debug.js +33 -0
  11. package/dist/daemon-recovery.d.ts +4 -0
  12. package/dist/daemon-recovery.js +75 -0
  13. package/dist/engine/backends/git/git-backend.d.ts +8 -0
  14. package/dist/engine/backends/git/git-backend.js +39 -12
  15. package/dist/engine/backends/git/git-runner.js +6 -1
  16. package/dist/engine/backends/git/git-storage.js +5 -9
  17. package/dist/engine/backends/publication-encoding.d.ts +3 -0
  18. package/dist/engine/backends/publication-encoding.js +19 -0
  19. package/dist/engine/backends/s3/s3-backend.js +10 -12
  20. package/dist/engine/backends/sync-backend.d.ts +4 -0
  21. package/dist/engine/backends/webdav/webdav-backend.d.ts +0 -2
  22. package/dist/engine/backends/webdav/webdav-backend.js +15 -26
  23. package/dist/engine/backends/webdav/webdav-client.js +10 -0
  24. package/dist/engine/commands/command-types.d.ts +1 -0
  25. package/dist/engine/debug-log.d.ts +27 -0
  26. package/dist/engine/debug-log.js +134 -0
  27. package/dist/engine/ignore/ignore-initialization.d.ts +5 -1
  28. package/dist/engine/ignore/ignore-initialization.js +50 -14
  29. package/dist/engine/ignore/ignore-settings.js +11 -14
  30. package/dist/engine/ignore/ignore-source.d.ts +1 -1
  31. package/dist/engine/ignore/ignore-source.js +1 -1
  32. package/dist/engine/paths.d.ts +2 -0
  33. package/dist/engine/paths.js +7 -1
  34. package/dist/engine/settings/config-file.d.ts +10 -1
  35. package/dist/engine/settings/config-file.js +62 -17
  36. package/dist/engine/settings/config.d.ts +7 -0
  37. package/dist/engine/settings/config.js +49 -14
  38. package/dist/engine/snapshot/snapshot-apply.d.ts +2 -1
  39. package/dist/engine/snapshot/snapshot-apply.js +30 -8
  40. package/dist/engine/snapshot/snapshot-codec.js +3 -1
  41. package/dist/engine/snapshot/snapshot-payload.d.ts +4 -0
  42. package/dist/engine/snapshot/snapshot-payload.js +32 -0
  43. package/dist/engine/snapshot/snapshot-transaction-images.d.ts +2 -0
  44. package/dist/engine/snapshot/snapshot-transaction-images.js +34 -0
  45. package/dist/engine/snapshot/snapshot-transaction.d.ts +4 -1
  46. package/dist/engine/snapshot/snapshot-transaction.js +50 -7
  47. package/dist/engine/snapshot/snapshot-types.d.ts +10 -2
  48. package/dist/engine/snapshot/snapshot.d.ts +3 -1
  49. package/dist/engine/snapshot/snapshot.js +64 -25
  50. package/dist/engine/state/lock.d.ts +2 -1
  51. package/dist/engine/state/lock.js +157 -17
  52. package/dist/engine/sync/continuous-sync.d.ts +10 -1
  53. package/dist/engine/sync/continuous-sync.js +35 -11
  54. package/dist/engine/sync/initialization.d.ts +1 -1
  55. package/dist/engine/sync/initialization.js +2 -2
  56. package/dist/engine/sync/local-fields.d.ts +6 -1
  57. package/dist/engine/sync/local-fields.js +42 -19
  58. package/dist/engine/sync/merged-sync.js +13 -10
  59. package/dist/engine/sync/remote-snapshot.js +18 -3
  60. package/dist/engine/sync/session-push.js +13 -4
  61. package/dist/engine/sync/startup-bootstrap.d.ts +1 -0
  62. package/dist/engine/sync/startup-bootstrap.js +105 -0
  63. package/dist/engine/sync/startup-recovery-evidence.d.ts +1 -0
  64. package/dist/engine/sync/startup-recovery-evidence.js +37 -0
  65. package/dist/engine/sync/startup-timings.d.ts +9 -2
  66. package/dist/engine/sync/startup-timings.js +80 -8
  67. package/dist/engine/sync/sync-error-guidance.js +22 -4
  68. package/dist/engine/sync/sync-inspection.js +4 -7
  69. package/dist/engine/sync/sync-local.d.ts +2 -1
  70. package/dist/engine/sync/sync-local.js +3 -1
  71. package/dist/engine/sync/sync-mutations.js +13 -7
  72. package/dist/engine/sync/sync-policy.d.ts +2 -0
  73. package/dist/engine/sync/sync-policy.js +9 -1
  74. package/dist/engine/sync/sync-state.d.ts +8 -3
  75. package/dist/engine/sync/sync-state.js +12 -4
  76. package/dist/engine/ui/sync-format.d.ts +16 -5
  77. package/dist/engine/ui/sync-format.js +23 -55
  78. package/dist/engine/ui/transfer-review.d.ts +2 -1
  79. package/dist/engine/ui/transfer-review.js +3 -3
  80. package/dist/engine-service.d.ts +6 -0
  81. package/dist/engine-service.js +54 -10
  82. package/dist/identity.d.ts +5 -2
  83. package/dist/identity.js +36 -6
  84. package/dist/index.d.ts +2 -1
  85. package/dist/index.js +2 -1
  86. package/dist/monitor.js +20 -1
  87. package/dist/paged-review.d.ts +35 -0
  88. package/dist/paged-review.js +177 -0
  89. package/dist/participant-ledger.d.ts +16 -0
  90. package/dist/participant-ledger.js +149 -0
  91. package/dist/protocol.d.ts +15 -0
  92. package/dist/protocol.js +4 -0
  93. package/dist/server.d.ts +7 -0
  94. package/dist/server.js +268 -120
  95. package/dist/service-host.d.ts +2 -1
  96. package/dist/service-host.js +29 -9
  97. package/dist/state-wait-jobs.d.ts +8 -0
  98. package/dist/state-wait-jobs.js +23 -0
  99. package/dist/state-wait.d.ts +7 -0
  100. package/dist/state-wait.js +34 -0
  101. package/docs/architecture.md +23 -1
  102. package/package.json +1 -1
package/CHANGELOG.md CHANGED
@@ -1,5 +1,24 @@
1
1
  # @narumitw/pi-sync-daemon
2
2
 
3
+ ## 0.10.0
4
+
5
+ ### Minor Changes
6
+
7
+ - 316dde1: Append safe owner, timing, job, and Continuous sync diagnostics to the shared, local-only `pi-sync.log`. Use the agent directory's Debug mode setting at the next owner, job, or cycle boundary, including for already-authorized unattended work. Keep repeated unchanged polls quiet and leave the operational `lifecycle.log` unchanged.
8
+
9
+ ### Patch Changes
10
+
11
+ - 6224417: Recover a blocked daemon from the Pi Sync menu or `/sync daemon recover`. Stop the reviewed owner without restarting it or replaying a transfer, and clean up the owned operation lock when a release failure blocks further writes.
12
+ - bbcc9e9: Reduce sync work without changing transfer approvals or recovery formats. Pull and rollback avoid reinstalling identical files, inspection avoids building unused payloads, and local requests wait for state changes instead of repeatedly polling jobs. Reduce repeated setup reads, review downloads, and backend work while preserving fresh checks and existing peer compatibility.
13
+
14
+ ## 0.9.1
15
+
16
+ ### Patch Changes
17
+
18
+ - eb17fcc: Explain how to reduce sync scope when the agent-directory scan exceeds its limit. Replace outdated Check setup directions with current Diagnostics and storage settings paths.
19
+ - aa99878: Let Up and Down select sync confirmation actions immediately. Show directional counts and safety notes in the summary, with exact file paths available in View details instead of requiring a scroll through the entire file list.
20
+ - 8e32b4b: Exclude top-level `git/` package checkouts in new `.syncignore` starter files and list the starter rules in alphabetical order. Preserve existing rule files and recover interrupted creation with its original rules.
21
+
3
22
  ## 0.9.0
4
23
 
5
24
  ### Minor Changes
package/README.md CHANGED
@@ -66,6 +66,14 @@ Existing `pi-sync.json`, canonical `pi-sync/`, and legacy `.pisync/` remain in t
66
66
 
67
67
  Automatic, Autonomous, and Continuous defaults remain unchanged. Old permission for work while Pi is open does not permit work after the last client exits. `/sync daemon allow-after-exit` reviews that extra permission for the current setup and selected independently reproducible provider/model; `/sync daemon revoke-after-exit` revokes it. Setup changes invalidate the saved digest. Changes to an interactive client’s model, working directory, or session-directory binding revoke older unattended grants, including across restart. Print/JSON never launch a service; passive attachment to an existing service protects their sessions and pauses automatic work. Registered live PIDs keep the service alive, including passive, disconnected, publishing, and orderly retired clients. A valid explicit after-exit grant for the active setup keeps service lifetime under the existing fingerprint and binding rules; it does not expand transfer or model permission. Temporary provider or backend unavailability does not itself revoke that grant. Lifetime checks do not call a model, contact a provider or storage, or execute credential commands. There is no new setting, environment variable, or keep-alive flag.
68
68
 
69
+ ### Debug mode
70
+
71
+ The daemon reads the top-level `debug` boolean in `pi-sync.json` for its agent directory (`--root` in the CLI). It defaults to `false`. Use **/sync → Settings → Debug mode** in Pi or edit the settings file. The daemon applies the validated value at its next owner, job, or Continuous sync cycle boundary; no restart is needed.
72
+
73
+ When enabled, the owner appends selected lifecycle, startup timing, job, and Continuous sync records to the private `<agent-directory>/pi-sync.log`. This includes already-authorized work after Pi exits but grants no new permission. Repeated successful unchanged polls do not add records. Filter the JSON lines by `source` to isolate `pi-sync-daemon` records from `pi-sync` client records.
74
+
75
+ The file is local-only. Records omit raw errors, credentials, addresses, paths, synced contents, and model data. Logging is best effort, not a recovery journal. Turning it off leaves existing records in place; retention is manual. See the [Pi Sync Debug mode reference](https://github.com/narumiruna/pi-extensions/blob/main/packages/pi-sync/skills/configure-pi-sync/references/configuration.md#debug-mode) for cleanup and older snapshots.
76
+
69
77
  ## 🔒 Security and privacy
70
78
 
71
79
  IPC uses a private root-bound Unix socket on POSIX or a user/root-bound named pipe on Windows. Requests negotiate protocol, instance, root, and a private capability token. Do not share runtime identity files. The service has the same filesystem and network permissions as your user.
@@ -94,7 +102,9 @@ owner. See the [daemon upgrade guide](https://github.com/narumiruna/pi-extension
94
102
 
95
103
  CLI `status` reports `recordedPid` from a validated identity file and `pid` only after a verified handshake. A pending upgrade has a separate `pendingUpgradePid`. `running: null` means the handshake failed, with or without a pending upgrade. Unknown status proves neither exit nor compatibility. Use diagnostics from an already available compatible version; if you cannot identify the owner, stop and preserve the records. Status does not expose the IPC token.
96
104
 
97
- A verified handshake also reports the private lifecycle-log path. The log retains at most 8 KiB of timestamps and lifecycle phases, not exception text, credentials, model output, or sync content; older entries roll over. It is diagnostic evidence, not a recovery journal. Child stdio is detached and discarded.
105
+ Pi Sync can also review and stop the existing daemon from `/sync daemon recover`. The client library exposes `inspectDaemonRecovery(root)` and `stopDaemonForRecovery(target)` for that flow. Inspection does not start the daemon. A successful stop verifies identity release and process exit without starting a replacement. Keep recovery evidence and follow the [cleanup recovery guide](https://github.com/narumiruna/pi-extensions/blob/main/packages/pi-sync/docs/daemon-migration.md#recover-from-uncertain-daemon-cleanup) if the stop cannot be verified.
106
+
107
+ A verified handshake also reports the private operational `lifecycle.log` path. This log is separate from `pi-sync.log` and is not controlled by Debug mode. It retains at most 8 KiB of timestamps and lifecycle phases, not exception text, credentials, model output, or sync content; older entries roll over. It is diagnostic evidence, not a recovery journal. Child stdio is detached and discarded.
98
108
 
99
109
  The source-linked [architecture and safety contract](docs/architecture.md) defines ownership, resolver capability checks, write coordination, and the decision matrix. It distinguishes required checks from completed evidence.
100
110
 
package/dist/cli.js CHANGED
@@ -58,6 +58,7 @@ export async function main(args) {
58
58
  log = openLifecycleLog(paths.directory);
59
59
  }
60
60
  catch (error) {
61
+ server.recordBootOutcome("failed");
61
62
  await server.close();
62
63
  throw error;
63
64
  }
@@ -70,6 +71,7 @@ export async function main(args) {
70
71
  throw error;
71
72
  }
72
73
  note("ready");
74
+ server.recordBootOutcome("ready");
73
75
  let stopping;
74
76
  const stop = () => {
75
77
  stopping ??= server.close().catch(() => {
@@ -0,0 +1,14 @@
1
+ import type { Socket } from "node:net";
2
+ import { type RuntimePaths } from "./identity.js";
3
+ import { type Identity, type RemoteError } from "./protocol.js";
4
+ export declare class DaemonError extends Error {
5
+ readonly remote: RemoteError;
6
+ constructor(remote: RemoteError);
7
+ }
8
+ export declare function requestDaemon(paths: RuntimePaths, identity: Identity, method: string, params: unknown, signal?: AbortSignal, options?: {
9
+ onSocket?(socket: Socket): void;
10
+ reviewDetails?: 1;
11
+ }): Promise<unknown>;
12
+ export declare function hello(paths: RuntimePaths, identity: Identity, signal?: AbortSignal): Promise<{
13
+ needsUpgrade: boolean;
14
+ }>;
@@ -0,0 +1,66 @@
1
+ import { request as httpRequest } from "node:http";
2
+ import { lt, valid } from "semver";
3
+ import { DAEMON_BUILD_VERSION } from "./build-version.js";
4
+ import { daemonPaths, readJsonBody } from "./identity.js";
5
+ import { MAX_MESSAGE_BYTES, record } from "./protocol.js";
6
+ export class DaemonError extends Error {
7
+ remote;
8
+ constructor(remote) {
9
+ super(remote.message);
10
+ this.name = remote.name;
11
+ this.remote = remote;
12
+ }
13
+ }
14
+ export async function requestDaemon(paths, identity, method, params, signal, options) {
15
+ signal?.throwIfAborted();
16
+ const body = JSON.stringify({
17
+ version: 1,
18
+ root: paths.root,
19
+ instance: identity.instance,
20
+ token: identity.token,
21
+ method,
22
+ params,
23
+ ...(method === "attach" && options?.reviewDetails === 1 ? { reviewDetails: 1 } : {}),
24
+ });
25
+ if (Buffer.byteLength(body) > MAX_MESSAGE_BYTES)
26
+ throw new Error("Daemon request exceeds its byte limit.");
27
+ return new Promise((resolve, reject) => {
28
+ const request = httpRequest({
29
+ socketPath: daemonPaths(paths, identity.instance).endpoint,
30
+ path: "/v1",
31
+ method: "POST",
32
+ signal,
33
+ headers: { "content-type": "application/json", "content-length": Buffer.byteLength(body) },
34
+ }, (response) => {
35
+ void readJsonBody(response)
36
+ .then((value) => {
37
+ if (!record(value))
38
+ throw new Error("Invalid daemon response.");
39
+ if (record(value.error) && typeof value.error.message === "string")
40
+ throw new DaemonError(value.error);
41
+ if (response.statusCode !== 200 || !Object.hasOwn(value, "result"))
42
+ throw new Error("Invalid daemon response.");
43
+ resolve(value.result);
44
+ })
45
+ .catch(reject);
46
+ });
47
+ request.setTimeout(method === "attach" || method === "resume-contact" ? 0 : 30_000, () => request.destroy(new Error("Daemon request timed out.")));
48
+ request.once("error", reject);
49
+ if (options?.onSocket)
50
+ request.once("socket", options.onSocket);
51
+ request.end(body);
52
+ });
53
+ }
54
+ export async function hello(paths, identity, signal) {
55
+ const value = await requestDaemon(paths, identity, "hello", {}, signal);
56
+ if (!record(value) ||
57
+ value.version !== 1 ||
58
+ value.root !== paths.root ||
59
+ value.instance !== identity.instance ||
60
+ value.pid !== identity.pid)
61
+ throw new Error("Daemon handshake mismatch.");
62
+ const version = value.buildVersion;
63
+ if (Object.hasOwn(value, "buildVersion") && (typeof version !== "string" || valid(version) !== version))
64
+ throw new Error("Invalid daemon build version.");
65
+ return { needsUpgrade: typeof version !== "string" || lt(version, DAEMON_BUILD_VERSION) };
66
+ }
package/dist/client.d.ts CHANGED
@@ -1,10 +1,9 @@
1
- import { type RuntimePaths } from "./identity.js";
2
- import { type Callback, type ClientEvent, type ClientFacts, type ClientHandoff, type ClientContact as ContactState, type Identity, type RemoteError } from "./protocol.js";
3
- export declare class DaemonError extends Error {
4
- readonly remote: RemoteError;
5
- constructor(remote: RemoteError);
6
- }
7
- export declare function requestDaemon(paths: RuntimePaths, identity: Identity, method: string, params: unknown, signal?: AbortSignal): Promise<unknown>;
1
+ import { type RuntimePaths, type UpgradeOwner } from "./identity.js";
2
+ import { type MergeReviewPresenter } from "./paged-review.js";
3
+ import { type Callback, type ClientEvent, type ClientFacts, type ClientHandoff, type ClientContact as ContactState, type Identity } from "./protocol.js";
4
+ export { DaemonError, hello, requestDaemon } from "./client-transport.js";
5
+ export declare function checkUpgradeConsent(paths: RuntimePaths, assertRoot: () => void, signal?: AbortSignal): Promise<void>;
6
+ export declare function finishUpgrade(paths: RuntimePaths, owner: UpgradeOwner, assertRoot: () => void, signal?: AbortSignal): Promise<void>;
8
7
  export declare function acquireStartupLock(paths: RuntimePaths, signal?: AbortSignal): Promise<() => Promise<void>>;
9
8
  /** Explicit startup only: callers decide whether configuration or a user action authorizes it. */
10
9
  export declare function ensureDaemon(root: string, signal?: AbortSignal): Promise<{
@@ -30,6 +29,7 @@ export interface QueueReleaseDelivery {
30
29
  }
31
30
  export interface ConnectionOptions {
32
31
  callback(callback: Callback, signal: AbortSignal): Promise<unknown>;
32
+ mergeReview?: MergeReviewPresenter;
33
33
  event?(event: ClientEvent): void;
34
34
  error?(error: unknown): void;
35
35
  /** Each current background observation after admission, including repeated states. */
@@ -56,6 +56,10 @@ export declare class DaemonConnection {
56
56
  private resuming;
57
57
  private resumeUncertain;
58
58
  private pendingJobs;
59
+ private observesState;
60
+ private stateRevision?;
61
+ private terminalJobs;
62
+ private jobWaiters;
59
63
  private uncertain;
60
64
  private reportedError;
61
65
  private commits;
@@ -77,7 +81,8 @@ export declare class DaemonConnection {
77
81
  /** Resume this participant without replacing its callbacks, jobs, or polling. */
78
82
  resumeContact(facts: ClientFacts, signal?: AbortSignal): Promise<void>;
79
83
  private sameClient;
80
- run(operation: string, payload: unknown, signal?: AbortSignal, onCommit?: () => void): Promise<unknown>;
84
+ run(operation: string, payload: unknown, signal?: AbortSignal, onCommit?: () => void, onSubmit?: (id: string) => void): Promise<unknown>;
85
+ private markWorkUncertain;
81
86
  private publishContact;
82
87
  private consume;
83
88
  private reportRelease;
@@ -85,7 +90,9 @@ export declare class DaemonConnection {
85
90
  private performCallback;
86
91
  private wait;
87
92
  private reportError;
93
+ private observeState;
88
94
  private poll;
95
+ private pollUntilClosed;
89
96
  pause(): Promise<void>;
90
97
  dispose(handoff?: ClientHandoff): Promise<void>;
91
98
  }
package/dist/client.js CHANGED
@@ -1,76 +1,16 @@
1
1
  import { spawn } from "node:child_process";
2
2
  import { createHash, randomUUID } from "node:crypto";
3
- import { request as httpRequest } from "node:http";
4
3
  import { setTimeout as delay } from "node:timers/promises";
5
4
  import { fileURLToPath } from "node:url";
6
5
  import lockfile from "proper-lockfile";
7
- import { lt, valid } from "semver";
8
- import { DAEMON_BUILD_VERSION } from "./build-version.js";
9
- import { canonicalRootGuard, clearUpgradeOwner, daemonPaths, privateDirectory, processExists, processExited, readIdentity, readJsonBody, readProcessBirth, readUpgradeOwner, recordUpgradeOwner, runtimePaths, upgradeRepairStarted, } from "./identity.js";
6
+ import { DaemonError, hello, requestDaemon } from "./client-transport.js";
7
+ import { canonicalRootGuard, clearUpgradeOwner, daemonPaths, privateDirectory, processExists, processExited, readIdentity, readProcessBirth, readUpgradeOwner, recordUpgradeOwner, runtimePaths, upgradeRepairStarted, } from "./identity.js";
10
8
  import { withCleanup } from "./lifecycle.js";
11
- import { MAX_MESSAGE_BYTES, MAX_REVIEW_BYTES, operationPrefix, REVIEW_PAGE_CHARS, record, } from "./protocol.js";
12
- export class DaemonError extends Error {
13
- remote;
14
- constructor(remote) {
15
- super(remote.message);
16
- this.name = remote.name;
17
- this.remote = remote;
18
- }
19
- }
20
- export async function requestDaemon(paths, identity, method, params, signal) {
21
- signal?.throwIfAborted();
22
- const body = JSON.stringify({
23
- version: 1,
24
- root: paths.root,
25
- instance: identity.instance,
26
- token: identity.token,
27
- method,
28
- params,
29
- });
30
- if (Buffer.byteLength(body) > MAX_MESSAGE_BYTES)
31
- throw new Error("Daemon request exceeds its byte limit.");
32
- return new Promise((resolve, reject) => {
33
- const request = httpRequest({
34
- socketPath: daemonPaths(paths, identity.instance).endpoint,
35
- path: "/v1",
36
- method: "POST",
37
- signal,
38
- headers: { "content-type": "application/json", "content-length": Buffer.byteLength(body) },
39
- }, (response) => {
40
- void readJsonBody(response)
41
- .then((value) => {
42
- if (!record(value))
43
- throw new Error("Invalid daemon response.");
44
- if (record(value.error) && typeof value.error.message === "string")
45
- throw new DaemonError(value.error);
46
- if (response.statusCode !== 200 || !Object.hasOwn(value, "result"))
47
- throw new Error("Invalid daemon response.");
48
- resolve(value.result);
49
- })
50
- .catch(reject);
51
- });
52
- // Admission can wait behind a complete apply. The request owner's signal
53
- // cancels that wait; an idle timeout must not let admission escape the fence.
54
- request.setTimeout(method === "attach" || method === "resume-contact" ? 0 : 30_000, () => request.destroy(new Error("Daemon request timed out.")));
55
- request.once("error", reject);
56
- request.end(body);
57
- });
58
- }
59
- async function hello(paths, identity, signal) {
60
- const value = await requestDaemon(paths, identity, "hello", {}, signal);
61
- if (!record(value) ||
62
- value.version !== 1 ||
63
- value.root !== paths.root ||
64
- value.instance !== identity.instance ||
65
- value.pid !== identity.pid)
66
- throw new Error("Daemon handshake mismatch.");
67
- const version = value.buildVersion;
68
- if (Object.hasOwn(value, "buildVersion") && (typeof version !== "string" || valid(version) !== version))
69
- throw new Error("Invalid daemon build version.");
70
- // The old @narumitw/sync-daemon used the same wire protocol without a build version.
71
- return { needsUpgrade: typeof version !== "string" || lt(version, DAEMON_BUILD_VERSION) };
72
- }
73
- async function checkUpgradeConsent(paths, assertRoot, signal) {
9
+ import { presentPagedReview, reviewScope } from "./paged-review.js";
10
+ import { MAX_REVIEW_BYTES, OWNERSHIP_CLEANUP_REQUIRED, operationPrefix, REVIEW_PAGE_CHARS, record, UNKNOWN_JOB_RESULT, } from "./protocol.js";
11
+ import { STATE_WAIT_FAILURE_LIMIT, stateWaitJobs, stateWaitResult } from "./state-wait-jobs.js";
12
+ export { DaemonError, hello, requestDaemon } from "./client-transport.js";
13
+ export async function checkUpgradeConsent(paths, assertRoot, signal) {
74
14
  const { readConsent } = await import("./consent.js");
75
15
  const { withStateDirectoryGuardForRoot } = await import("./engine/state/state-directory.js");
76
16
  signal?.throwIfAborted();
@@ -80,15 +20,18 @@ async function checkUpgradeConsent(paths, assertRoot, signal) {
80
20
  // An older owner cannot report an in-memory revocation failure.
81
21
  const consent = readConsent(paths.root);
82
22
  if (consent && Object.values(consent.setups).some((setup) => setup.afterExit))
83
- throw new Error("Review or revoke unattended Pi Sync permission before restarting the older daemon.");
23
+ throw new Error("Review or revoke unattended Pi Sync permission before stopping or replacing the daemon.");
84
24
  });
85
25
  signal?.throwIfAborted();
86
26
  assertRoot();
87
27
  }
88
- async function finishUpgrade(paths, owner, assertRoot, signal) {
28
+ export async function finishUpgrade(paths, owner, assertRoot, signal) {
89
29
  await waitForProcessExit(owner, signal);
90
- if ((await readIdentity(paths))?.instance === owner.instance)
30
+ const current = await readIdentity(paths);
31
+ if (current?.instance === owner.instance)
91
32
  throw new Error("The daemon exited without releasing its identity.");
33
+ if (current && owner.purpose === "cleanup")
34
+ throw new Error("Daemon owner changed during recovery. Review the current owner before retrying.");
92
35
  await checkUpgradeConsent(paths, assertRoot, signal);
93
36
  await clearUpgradeOwner(paths);
94
37
  signal?.throwIfAborted();
@@ -160,6 +103,8 @@ export async function ensureDaemon(root, signal) {
160
103
  upgraded = true;
161
104
  continue;
162
105
  }
106
+ if (upgrade?.purpose === "cleanup")
107
+ throw Object.assign(new Error("Daemon recovery is incomplete. Use /sync daemon recover to finish the reviewed stop."), { code: OWNERSHIP_CLEANUP_REQUIRED });
163
108
  if (!existing)
164
109
  break;
165
110
  // A sample taken only after hello could describe an unrelated process reusing the PID.
@@ -347,6 +292,10 @@ export class DaemonConnection {
347
292
  resuming;
348
293
  resumeUncertain = false;
349
294
  pendingJobs = new Set();
295
+ observesState = false;
296
+ stateRevision;
297
+ terminalJobs = new Set();
298
+ jobWaiters = new Map();
350
299
  uncertain = false;
351
300
  reportedError = false;
352
301
  commits = new Map();
@@ -388,7 +337,7 @@ export class DaemonConnection {
388
337
  this.admitting = true;
389
338
  this.lifecycleRevision++;
390
339
  this.contactRevision++;
391
- const request = requestDaemon(this.paths, this.identity, "attach", this.facts, signal ? AbortSignal.any([signal, this.controller.signal]) : this.controller.signal);
340
+ const request = requestDaemon(this.paths, this.identity, "attach", this.facts, signal ? AbortSignal.any([signal, this.controller.signal]) : this.controller.signal, this.options.mergeReview ? { reviewDetails: 1 } : undefined);
392
341
  // Poll during attach: stopping a previous controller may need release answers
393
342
  // from this reattached participant before the attach response can complete.
394
343
  this.polling ??= this.poll();
@@ -397,6 +346,9 @@ export class DaemonConnection {
397
346
  const result = await request;
398
347
  admitted = true;
399
348
  this.publishContact(this.consume(result));
349
+ this.observesState = record(result) && result.stateWait === 1;
350
+ this.stateRevision = undefined;
351
+ this.terminalJobs.clear();
400
352
  this.attached = true;
401
353
  return this;
402
354
  }
@@ -519,7 +471,7 @@ export class DaemonConnection {
519
471
  sameClient(facts) {
520
472
  return facts.id === this.facts.id && facts.pid === this.facts.pid && facts.generation === this.facts.generation;
521
473
  }
522
- async run(operation, payload, signal, onCommit) {
474
+ async run(operation, payload, signal, onCommit, onSubmit) {
523
475
  if (this.closed ||
524
476
  this.uncertain ||
525
477
  this.contactResumeState !== "idle" ||
@@ -529,10 +481,13 @@ export class DaemonConnection {
529
481
  this.contactState === "inactive")
530
482
  throw new Error("Daemon client is unavailable; confirm contact and drain work before submitting.");
531
483
  signal?.throwIfAborted();
532
- const id = `${operationPrefix(this.identity)}${randomUUID()}`;
484
+ const { paths, identity } = this;
485
+ const id = `${operationPrefix(identity)}${randomUUID()}`;
486
+ const jobParams = { id, client: this.facts.id };
487
+ const transport = this.controller.signal;
533
488
  this.pendingJobs.add(id);
534
489
  const abort = () => {
535
- void requestDaemon(this.paths, this.identity, "cancel", { id, client: this.facts.id }, this.controller.signal).catch(() => { });
490
+ void requestDaemon(paths, identity, "cancel", jobParams, transport).catch(() => { });
536
491
  };
537
492
  let terminal = false;
538
493
  if (onCommit)
@@ -541,11 +496,37 @@ export class DaemonConnection {
541
496
  if (signal?.aborted)
542
497
  abort();
543
498
  return withCleanup(async () => {
544
- await requestDaemon(this.paths, this.identity, "submit", { id, client: this.facts.id, operation, payload }, this.controller.signal);
499
+ try {
500
+ onSubmit?.(id);
501
+ }
502
+ catch { }
503
+ await requestDaemon(paths, identity, "submit", { ...jobParams, operation, payload }, transport);
504
+ let terminalObserved = this.observesState;
505
+ if (terminalObserved && !this.terminalJobs.has(id)) {
506
+ const completion = Promise.withResolvers();
507
+ this.jobWaiters.set(id, completion);
508
+ const keepAlive = setInterval(() => { }, 30_000);
509
+ try {
510
+ terminalObserved = (await completion.promise) === "terminal";
511
+ }
512
+ finally {
513
+ this.jobWaiters.delete(id);
514
+ clearInterval(keepAlive);
515
+ }
516
+ }
545
517
  while (!this.closed) {
546
- const reply = await requestDaemon(this.paths, this.identity, "poll", { client: this.facts.id, cursor: this.cursor, job: id }, this.controller.signal);
547
- this.consume(reply);
518
+ const reply = await requestDaemon(paths, identity, "poll", { client: jobParams.client, cursor: this.cursor, job: id }, transport).catch((error) => {
519
+ if (error instanceof DaemonError && error.message === UNKNOWN_JOB_RESULT)
520
+ this.markWorkUncertain();
521
+ throw error;
522
+ });
548
523
  const job = record(reply) ? reply.job : undefined;
524
+ const missing = terminalObserved && (job?.id !== id || (job.phase !== "done" && job.phase !== "failed"));
525
+ if (missing)
526
+ this.markWorkUncertain();
527
+ this.consume(reply);
528
+ if (missing)
529
+ throw new Error(UNKNOWN_JOB_RESULT);
549
530
  if (job?.id === id && job.phase === "failed") {
550
531
  terminal = true;
551
532
  throw new DaemonError(job.error ?? { name: "Error", message: "Daemon operation failed." });
@@ -566,17 +547,22 @@ export class DaemonConnection {
566
547
  if (callback.job === id && !callback.kind.startsWith("queue-"))
567
548
  callback.controller.abort();
568
549
  try {
569
- await requestDaemon(this.paths, this.identity, "cancel", { id, client: this.facts.id });
550
+ await requestDaemon(paths, identity, "cancel", jobParams);
570
551
  }
571
552
  catch (error) {
572
- this.uncertain = true;
573
- for (const callback of this.callbacks.values())
574
- if (callback.kind !== "queue-release")
575
- callback.controller.abort();
553
+ this.markWorkUncertain();
576
554
  throw new Error("Operation outcome is uncertain; reload to reconnect and reconcile, never replay the write.", { cause: error });
577
555
  }
578
556
  }
579
- }).finally(() => this.pendingJobs.delete(id));
557
+ }).finally(() => {
558
+ this.pendingJobs.delete(id);
559
+ });
560
+ }
561
+ markWorkUncertain() {
562
+ this.uncertain = true;
563
+ for (const callback of this.callbacks.values())
564
+ if (callback.kind !== "queue-release")
565
+ callback.controller.abort();
580
566
  }
581
567
  publishContact(contact) {
582
568
  this.contactRevision++;
@@ -637,6 +623,7 @@ export class DaemonConnection {
637
623
  this.uncertain = true;
638
624
  const controller = new AbortController();
639
625
  const foreground = this.pendingJobs.has(callback.job);
626
+ const reviewGeneration = callback.review?.offer !== undefined ? this.facts.generation : undefined;
640
627
  this.callbacks.set(callback.id, {
641
628
  controller,
642
629
  kind: callback.kind,
@@ -665,7 +652,10 @@ export class DaemonConnection {
665
652
  await requestDaemon(this.paths, this.identity, "answer", {
666
653
  client: this.facts.id,
667
654
  id: callback.id,
668
- ...(failed ? { error: "Client callback cancelled or failed." } : { value }),
655
+ ...(reviewGeneration !== undefined ? { generation: reviewGeneration } : {}),
656
+ ...(failed || (reviewGeneration !== undefined && controller.signal.aborted)
657
+ ? { error: "Client callback cancelled or failed." }
658
+ : { value }),
669
659
  }, this.callbackIO.signal);
670
660
  }
671
661
  finally {
@@ -755,6 +745,11 @@ export class DaemonConnection {
755
745
  const reference = callback.review;
756
746
  if (!reference)
757
747
  return this.options.callback(callback, signal);
748
+ if (reference.offer !== undefined) {
749
+ if (!this.options.mergeReview)
750
+ throw new Error("Exact review offer requires an opted-in presenter.");
751
+ return presentPagedReview(reviewScope(this.identity, this.facts, callback), callback, AbortSignal.any([signal, this.callbackIO.signal]), this.options.mergeReview, (params, owned) => requestDaemon(this.paths, this.identity, "review-page", params, owned));
752
+ }
758
753
  if (callback.kind !== "merge-review" ||
759
754
  !record(callback.payload) ||
760
755
  !Number.isSafeInteger(reference.pages) ||
@@ -816,14 +811,44 @@ export class DaemonConnection {
816
811
  /* Error presentation must not own a task. */
817
812
  }
818
813
  }
814
+ async observeState() {
815
+ const jobs = stateWaitJobs(this.jobWaiters);
816
+ const reply = await requestDaemon(this.paths, this.identity, "state-wait", {
817
+ client: this.facts.id,
818
+ generation: this.facts.generation,
819
+ cursor: this.cursor,
820
+ revision: this.stateRevision,
821
+ jobs,
822
+ }, this.controller.signal, { onSocket: (socket) => socket.unref() });
823
+ return stateWaitResult(reply);
824
+ }
819
825
  async poll() {
826
+ await withCleanup(() => this.pollUntilClosed(), async () => {
827
+ for (const waiter of this.jobWaiters.values())
828
+ waiter.reject(this.controller.signal.reason ?? new Error("Daemon client closed."));
829
+ });
830
+ }
831
+ async pollUntilClosed() {
832
+ let failures = 0;
820
833
  while (!this.closed) {
821
834
  try {
822
835
  const revision = this.contactRevision;
823
- const reply = await requestDaemon(this.paths, this.identity, "poll", { client: this.facts.id, cursor: this.cursor }, this.controller.signal);
836
+ const state = this.observesState && !this.admitting ? await this.observeState() : undefined;
837
+ const reply = state?.reply ??
838
+ (await requestDaemon(this.paths, this.identity, "poll", { client: this.facts.id, cursor: this.cursor }, this.controller.signal));
824
839
  // An older poll must not undo typed update expiry or a successful resume.
825
840
  if (revision === this.contactRevision) {
841
+ if (state?.missing.some((id) => this.jobWaiters.has(id)))
842
+ this.markWorkUncertain();
826
843
  const contact = this.consume(reply);
844
+ if (state) {
845
+ this.stateRevision = state.version;
846
+ this.terminalJobs = new Set(state.finished);
847
+ for (const id of state.missing)
848
+ this.jobWaiters.get(id)?.reject(new Error(UNKNOWN_JOB_RESULT));
849
+ for (const id of state.finished)
850
+ this.jobWaiters.get(id)?.resolve("terminal");
851
+ }
827
852
  this.reportedError = false;
828
853
  if (revision === this.contactRevision &&
829
854
  this.attached &&
@@ -838,11 +863,16 @@ export class DaemonConnection {
838
863
  this.options.contact?.(contact);
839
864
  }
840
865
  }
841
- await this.wait(100);
866
+ failures = 0;
867
+ if (!state)
868
+ await this.wait(100);
842
869
  }
843
870
  catch (error) {
844
871
  if (this.closed)
845
872
  return;
873
+ if (++failures >= STATE_WAIT_FAILURE_LIMIT)
874
+ for (const waiter of this.jobWaiters.values())
875
+ waiter.resolve("poll");
846
876
  if (this.attached)
847
877
  this.reportError(error);
848
878
  try {
package/dist/consent.js CHANGED
@@ -182,11 +182,15 @@ export function automaticPolicyAuthorized(root, config) {
182
182
  export async function reviewAutomaticPolicy(execution, config, expectedFingerprint) {
183
183
  if (consentFingerprint(execution.root, config) !== expectedFingerprint)
184
184
  throw new Error("Setup changed; reopen the automatic transfer review.");
185
- if (config.reservedIgnoreNames === undefined || automaticPolicyAuthorized(execution.root, config))
185
+ if (config.reservedIgnoreNames === undefined || automaticPolicyAuthorized(execution.root, config)) {
186
+ execution.noteDebugOutcome?.("skipped");
186
187
  return false;
188
+ }
187
189
  const consent = readConsent(execution.root);
188
- if (!consent)
190
+ if (!consent) {
191
+ execution.noteDebugOutcome?.("blocked");
189
192
  return false;
193
+ }
190
194
  const consentToken = JSON.stringify(consent);
191
195
  const peers = participantBinding(execution);
192
196
  const binding = JSON.stringify(execution.facts());
@@ -202,8 +206,10 @@ export async function reviewAutomaticPolicy(execution, config, expectedFingerpri
202
206
  };
203
207
  const current = await loadConfigForCheck(undefined, execution.signal);
204
208
  validate();
205
- if (syncCheckConfigFingerprint(current) !== fingerprint)
209
+ if (syncCheckConfigFingerprint(current) !== fingerprint) {
210
+ execution.noteDebugOutcome?.("blocked");
206
211
  return false;
212
+ }
207
213
  const modes = [
208
214
  startupTransferEnabled(config) && "Automatic transfer at startup",
209
215
  config.automatic &&
@@ -229,8 +235,10 @@ export async function reviewAutomaticPolicy(execution, config, expectedFingerpri
229
235
  ].join("\n"),
230
236
  });
231
237
  validate();
232
- if (accepted !== true)
238
+ if (accepted !== true) {
239
+ execution.noteDebugOutcome?.("cancelled");
233
240
  return false;
241
+ }
234
242
  const latest = await loadConfigForCheck(undefined, execution.signal);
235
243
  await config.validateIgnorePolicy?.();
236
244
  validate();
@@ -254,28 +262,38 @@ export async function reviewAutomaticPolicy(execution, config, expectedFingerpri
254
262
  }
255
263
  export async function captureAutomaticAuthorization(execution, expectedFingerprint) {
256
264
  const consent = readConsent(execution.root);
257
- if (!consent)
265
+ if (!consent) {
266
+ execution.noteDebugOutcome?.("blocked");
258
267
  throw new Error("Automatic work requires reviewed daemon handover first.");
268
+ }
259
269
  const consentToken = JSON.stringify(consent);
260
270
  const peers = participantBinding(execution);
261
271
  const { loadConfigForCheck } = await import("./engine/settings/config.js");
262
272
  const validateParticipants = () => {
263
273
  execution.validate();
264
- if (participantBinding(execution) !== peers || JSON.stringify(readConsent(execution.root)) !== consentToken)
274
+ if (participantBinding(execution) !== peers || JSON.stringify(readConsent(execution.root)) !== consentToken) {
275
+ execution.noteDebugOutcome?.("blocked");
265
276
  throw new Error("Daemon participants or consent changed before automatic work completed.");
277
+ }
266
278
  };
267
279
  validateParticipants();
268
280
  const config = await loadConfigForCheck(undefined, execution.signal);
269
281
  validateParticipants();
270
282
  const fingerprint = syncCheckConfigFingerprint(config);
271
- if (expectedFingerprint !== undefined ? fingerprint !== expectedFingerprint : config.reservedIgnoreNames !== undefined)
283
+ if (expectedFingerprint !== undefined ? fingerprint !== expectedFingerprint : config.reservedIgnoreNames !== undefined) {
284
+ execution.noteDebugOutcome?.("blocked");
272
285
  throw new Error("Settings changed before automatic work; use the reviewed foreground command.");
273
- if (!automaticPolicyAuthorized(execution.root, config))
286
+ }
287
+ if (!automaticPolicyAuthorized(execution.root, config)) {
288
+ execution.noteDebugOutcome?.("blocked");
274
289
  throw new Error("Automatic transfer requires separate approval for the current setup and policy. Run reviewed /sync first.");
290
+ }
275
291
  const validate = (current) => {
276
292
  validateParticipants();
277
- if (current && syncCheckConfigFingerprint(current) !== fingerprint)
293
+ if (current && syncCheckConfigFingerprint(current) !== fingerprint) {
294
+ execution.noteDebugOutcome?.("blocked");
278
295
  throw new Error("Settings changed before automatic work completed.");
296
+ }
279
297
  };
280
298
  const refresh = async () => {
281
299
  validate();
@@ -294,8 +312,10 @@ export async function requireHandover(execution) {
294
312
  message: "Close or upgrade every older Pi process before continuing. An idle older Pi cannot be detected from an absent sync lock. The daemon will own transfers; Pi keeps presentation and live file queues. Autonomous resolution uses independently available auth.json/models.json configuration and the environment captured when the service starts, not Pi runtime accounts or providers. Credential commands run non-interactively from the canonical agent root. Review this independent provider/account configuration; restart the service after changing its environment. This does not authorize transfers or model requests after the last Pi exits.",
295
313
  });
296
314
  execution.validate();
297
- if (accepted !== true)
315
+ if (accepted !== true) {
316
+ execution.noteDebugOutcome?.("cancelled");
298
317
  throw new Error("Daemon handover was not approved; no transfer was started.");
318
+ }
299
319
  const { withLock } = await import("./engine/state/lock.js");
300
320
  await withLock("daemon-handover", async () => {
301
321
  execution.validate();
@@ -347,8 +367,10 @@ export async function authorizeAfterExit(execution, payload) {
347
367
  message: `Setup: ${safeTerminalText(config.setupName)}\n${selection}\nModel: ${safeTerminalText(facts?.model?.provider ?? "unavailable")}/${safeTerminalText(facts?.model?.id ?? "unavailable")}\n\n${payload.afterExit ? `The daemon may transfer selected private content and send conflict evidence to this independently available provider after Pi exits. Existing budgets, exclusions, journals, and stale-evidence checks still apply. No parent conversation or tools are sent. Changing this setup or model invalidates this permission.${config.reservedIgnoreNames !== undefined ? " This also approves the enabled automatic modes for this setup while Pi clients are attached." : ""}` : "The daemon may remain alive, but no new transfer or model request may start with zero clients."}`,
348
368
  });
349
369
  execution.validate();
350
- if (accepted !== true)
370
+ if (accepted !== true) {
371
+ execution.noteDebugOutcome?.("cancelled");
351
372
  return undefined;
373
+ }
352
374
  const latest = await loadConfigForCheck(undefined, execution.signal);
353
375
  execution.validate();
354
376
  await config.validateIgnorePolicy?.();