@narumitw/pi-sync-daemon 0.0.0-stage → 0.2.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.
- package/CHANGELOG.md +13 -0
- package/LICENSE +21 -0
- package/README.md +89 -2
- package/dist/cli.d.ts +2 -0
- package/dist/cli.js +128 -0
- package/dist/client.d.ts +55 -0
- package/dist/client.js +484 -0
- package/dist/consent.d.ts +24 -0
- package/dist/consent.js +205 -0
- package/dist/document.d.ts +10 -0
- package/dist/document.js +6 -0
- package/dist/endpoint.d.ts +8 -0
- package/dist/endpoint.js +91 -0
- package/dist/engine/backends/backend-factory.d.ts +6 -0
- package/dist/engine/backends/backend-factory.js +16 -0
- package/dist/engine/backends/backend-identity.d.ts +3 -0
- package/dist/engine/backends/backend-identity.js +38 -0
- package/dist/engine/backends/backend-types.d.ts +70 -0
- package/dist/engine/backends/backend-types.js +1 -0
- package/dist/engine/backends/git/git-backend.d.ts +55 -0
- package/dist/engine/backends/git/git-backend.js +700 -0
- package/dist/engine/backends/git/git-config.d.ts +5 -0
- package/dist/engine/backends/git/git-config.js +118 -0
- package/dist/engine/backends/git/git-runner.d.ts +25 -0
- package/dist/engine/backends/git/git-runner.js +232 -0
- package/dist/engine/backends/git/git-storage.d.ts +39 -0
- package/dist/engine/backends/git/git-storage.js +226 -0
- package/dist/engine/backends/s3/s3-backend.d.ts +32 -0
- package/dist/engine/backends/s3/s3-backend.js +436 -0
- package/dist/engine/backends/s3/s3-client.d.ts +36 -0
- package/dist/engine/backends/s3/s3-client.js +352 -0
- package/dist/engine/backends/s3/s3-conditions.d.ts +4 -0
- package/dist/engine/backends/s3/s3-conditions.js +66 -0
- package/dist/engine/backends/snapshot-pointer.d.ts +6 -0
- package/dist/engine/backends/snapshot-pointer.js +22 -0
- package/dist/engine/backends/sync-backend.d.ts +71 -0
- package/dist/engine/backends/sync-backend.js +23 -0
- package/dist/engine/backends/webdav/webdav-backend.d.ts +34 -0
- package/dist/engine/backends/webdav/webdav-backend.js +534 -0
- package/dist/engine/backends/webdav/webdav-client.d.ts +36 -0
- package/dist/engine/backends/webdav/webdav-client.js +326 -0
- package/dist/engine/backends/webdav/webdav-config.d.ts +5 -0
- package/dist/engine/backends/webdav/webdav-config.js +73 -0
- package/dist/engine/commands/command-types.d.ts +31 -0
- package/dist/engine/commands/command-types.js +1 -0
- package/dist/engine/host.d.ts +70 -0
- package/dist/engine/host.js +16 -0
- package/dist/engine/paths.d.ts +9 -0
- package/dist/engine/paths.js +71 -0
- package/dist/engine/settings/config-errors.d.ts +1 -0
- package/dist/engine/settings/config-errors.js +4 -0
- package/dist/engine/settings/config-file.d.ts +33 -0
- package/dist/engine/settings/config-file.js +630 -0
- package/dist/engine/settings/config.d.ts +24 -0
- package/dist/engine/settings/config.js +200 -0
- package/dist/engine/settings/settings-management.d.ts +24 -0
- package/dist/engine/settings/settings-management.js +191 -0
- package/dist/engine/settings/settings-store.d.ts +8 -0
- package/dist/engine/settings/settings-store.js +41 -0
- package/dist/engine/settings/settings-types.d.ts +134 -0
- package/dist/engine/settings/settings-types.js +1 -0
- package/dist/engine/settings/settings-validation.d.ts +15 -0
- package/dist/engine/settings/settings-validation.js +356 -0
- package/dist/engine/snapshot/session-paths.d.ts +12 -0
- package/dist/engine/snapshot/session-paths.js +91 -0
- package/dist/engine/snapshot/snapshot-apply.d.ts +22 -0
- package/dist/engine/snapshot/snapshot-apply.js +262 -0
- package/dist/engine/snapshot/snapshot-case-replacement.d.ts +15 -0
- package/dist/engine/snapshot/snapshot-case-replacement.js +82 -0
- package/dist/engine/snapshot/snapshot-codec.d.ts +6 -0
- package/dist/engine/snapshot/snapshot-codec.js +53 -0
- package/dist/engine/snapshot/snapshot-exclude-safety.d.ts +7 -0
- package/dist/engine/snapshot/snapshot-exclude-safety.js +47 -0
- package/dist/engine/snapshot/snapshot-mutation-queues.d.ts +9 -0
- package/dist/engine/snapshot/snapshot-mutation-queues.js +133 -0
- package/dist/engine/snapshot/snapshot-paths.d.ts +1 -0
- package/dist/engine/snapshot/snapshot-paths.js +11 -0
- package/dist/engine/snapshot/snapshot-root-transition.d.ts +25 -0
- package/dist/engine/snapshot/snapshot-root-transition.js +122 -0
- package/dist/engine/snapshot/snapshot-transaction-directory.d.ts +2 -0
- package/dist/engine/snapshot/snapshot-transaction-directory.js +16 -0
- package/dist/engine/snapshot/snapshot-transaction-plan.d.ts +12 -0
- package/dist/engine/snapshot/snapshot-transaction-plan.js +68 -0
- package/dist/engine/snapshot/snapshot-transaction.d.ts +15 -0
- package/dist/engine/snapshot/snapshot-transaction.js +787 -0
- package/dist/engine/snapshot/snapshot-types.d.ts +48 -0
- package/dist/engine/snapshot/snapshot-types.js +1 -0
- package/dist/engine/snapshot/snapshot.d.ts +30 -0
- package/dist/engine/snapshot/snapshot.js +364 -0
- package/dist/engine/state/json-file.d.ts +6 -0
- package/dist/engine/state/json-file.js +49 -0
- package/dist/engine/state/lock-policy.d.ts +2 -0
- package/dist/engine/state/lock-policy.js +2 -0
- package/dist/engine/state/lock.d.ts +20 -0
- package/dist/engine/state/lock.js +259 -0
- package/dist/engine/state/lockfile-fs.d.ts +13 -0
- package/dist/engine/state/lockfile-fs.js +13 -0
- package/dist/engine/state/merge-baseline-store.d.ts +11 -0
- package/dist/engine/state/merge-baseline-store.js +187 -0
- package/dist/engine/state/mutation-directory-sync.d.ts +5 -0
- package/dist/engine/state/mutation-directory-sync.js +34 -0
- package/dist/engine/state/operation-availability.d.ts +49 -0
- package/dist/engine/state/operation-availability.js +40 -0
- package/dist/engine/state/state-directory.d.ts +14 -0
- package/dist/engine/state/state-directory.js +225 -0
- package/dist/engine/state/state-types.d.ts +27 -0
- package/dist/engine/state/state-types.js +1 -0
- package/dist/engine/state/sync-state-store.d.ts +11 -0
- package/dist/engine/state/sync-state-store.js +54 -0
- package/dist/engine/sync/agent-candidate.d.ts +44 -0
- package/dist/engine/sync/agent-candidate.js +207 -0
- package/dist/engine/sync/agent-plan.d.ts +8 -0
- package/dist/engine/sync/agent-plan.js +84 -0
- package/dist/engine/sync/agent-policy.d.ts +9 -0
- package/dist/engine/sync/agent-policy.js +10 -0
- package/dist/engine/sync/agent-refusal-retry.d.ts +4 -0
- package/dist/engine/sync/agent-refusal-retry.js +39 -0
- package/dist/engine/sync/agent-request.d.ts +14 -0
- package/dist/engine/sync/agent-request.js +13 -0
- package/dist/engine/sync/agent-runner.d.ts +6 -0
- package/dist/engine/sync/agent-runner.js +183 -0
- package/dist/engine/sync/agent-store.d.ts +7 -0
- package/dist/engine/sync/agent-store.js +177 -0
- package/dist/engine/sync/agent-sync.d.ts +4 -0
- package/dist/engine/sync/agent-sync.js +132 -0
- package/dist/engine/sync/apply-review.d.ts +8 -0
- package/dist/engine/sync/apply-review.js +51 -0
- package/dist/engine/sync/conflict-artifact-ownership.d.ts +4 -0
- package/dist/engine/sync/conflict-artifact-ownership.js +53 -0
- package/dist/engine/sync/conflict-artifacts.d.ts +32 -0
- package/dist/engine/sync/conflict-artifacts.js +158 -0
- package/dist/engine/sync/conflict-groups.d.ts +9 -0
- package/dist/engine/sync/conflict-groups.js +76 -0
- package/dist/engine/sync/conflict-preview.d.ts +4 -0
- package/dist/engine/sync/conflict-preview.js +53 -0
- package/dist/engine/sync/conflict-resolution.d.ts +13 -0
- package/dist/engine/sync/conflict-resolution.js +53 -0
- package/dist/engine/sync/conflict-retention.d.ts +5 -0
- package/dist/engine/sync/conflict-retention.js +61 -0
- package/dist/engine/sync/conflict-review.d.ts +4 -0
- package/dist/engine/sync/conflict-review.js +73 -0
- package/dist/engine/sync/content-conflicts.d.ts +10 -0
- package/dist/engine/sync/content-conflicts.js +70 -0
- package/dist/engine/sync/continuous-errors.d.ts +14 -0
- package/dist/engine/sync/continuous-errors.js +66 -0
- package/dist/engine/sync/continuous-lease.d.ts +9 -0
- package/dist/engine/sync/continuous-lease.js +206 -0
- package/dist/engine/sync/continuous-record.d.ts +31 -0
- package/dist/engine/sync/continuous-record.js +199 -0
- package/dist/engine/sync/continuous-storage.d.ts +14 -0
- package/dist/engine/sync/continuous-storage.js +59 -0
- package/dist/engine/sync/continuous-sync.d.ts +15 -0
- package/dist/engine/sync/continuous-sync.js +496 -0
- package/dist/engine/sync/continuous-watch.d.ts +3 -0
- package/dist/engine/sync/continuous-watch.js +65 -0
- package/dist/engine/sync/exclude-policy.d.ts +5 -0
- package/dist/engine/sync/exclude-policy.js +79 -0
- package/dist/engine/sync/field-migration.d.ts +5 -0
- package/dist/engine/sync/field-migration.js +20 -0
- package/dist/engine/sync/file-merge-planner.d.ts +36 -0
- package/dist/engine/sync/file-merge-planner.js +123 -0
- package/dist/engine/sync/initialization.d.ts +12 -0
- package/dist/engine/sync/initialization.js +75 -0
- package/dist/engine/sync/json-document.d.ts +29 -0
- package/dist/engine/sync/json-document.js +172 -0
- package/dist/engine/sync/local-field-policy.d.ts +2 -0
- package/dist/engine/sync/local-field-policy.js +26 -0
- package/dist/engine/sync/local-fields.d.ts +9 -0
- package/dist/engine/sync/local-fields.js +116 -0
- package/dist/engine/sync/merge-apply.d.ts +5 -0
- package/dist/engine/sync/merge-apply.js +386 -0
- package/dist/engine/sync/merge-journal.d.ts +31 -0
- package/dist/engine/sync/merge-journal.js +179 -0
- package/dist/engine/sync/merge-transfer-error.d.ts +3 -0
- package/dist/engine/sync/merge-transfer-error.js +64 -0
- package/dist/engine/sync/merged-sync.d.ts +6 -0
- package/dist/engine/sync/merged-sync.js +618 -0
- package/dist/engine/sync/mutation-owner.d.ts +2 -0
- package/dist/engine/sync/mutation-owner.js +18 -0
- package/dist/engine/sync/package-merge.d.ts +3 -0
- package/dist/engine/sync/package-merge.js +82 -0
- package/dist/engine/sync/partial-progress.d.ts +15 -0
- package/dist/engine/sync/partial-progress.js +39 -0
- package/dist/engine/sync/remote-snapshot.d.ts +28 -0
- package/dist/engine/sync/remote-snapshot.js +62 -0
- package/dist/engine/sync/resolver-stream.d.ts +4 -0
- package/dist/engine/sync/resolver-stream.js +34 -0
- package/dist/engine/sync/session-merge.d.ts +3 -0
- package/dist/engine/sync/session-merge.js +178 -0
- package/dist/engine/sync/session-push.d.ts +5 -0
- package/dist/engine/sync/session-push.js +32 -0
- package/dist/engine/sync/settings-conflicts.d.ts +8 -0
- package/dist/engine/sync/settings-conflicts.js +56 -0
- package/dist/engine/sync/settings-merge.d.ts +13 -0
- package/dist/engine/sync/settings-merge.js +93 -0
- package/dist/engine/sync/signals.d.ts +2 -0
- package/dist/engine/sync/signals.js +8 -0
- package/dist/engine/sync/startup-timings.d.ts +11 -0
- package/dist/engine/sync/startup-timings.js +44 -0
- package/dist/engine/sync/sync-decision.d.ts +35 -0
- package/dist/engine/sync/sync-decision.js +66 -0
- package/dist/engine/sync/sync-error-guidance.d.ts +2 -0
- package/dist/engine/sync/sync-error-guidance.js +55 -0
- package/dist/engine/sync/sync-errors.d.ts +9 -0
- package/dist/engine/sync/sync-errors.js +18 -0
- package/dist/engine/sync/sync-inspection.d.ts +26 -0
- package/dist/engine/sync/sync-inspection.js +40 -0
- package/dist/engine/sync/sync-loaders.d.ts +7 -0
- package/dist/engine/sync/sync-loaders.js +8 -0
- package/dist/engine/sync/sync-local.d.ts +5 -0
- package/dist/engine/sync/sync-local.js +40 -0
- package/dist/engine/sync/sync-mutations.d.ts +30 -0
- package/dist/engine/sync/sync-mutations.js +589 -0
- package/dist/engine/sync/sync-operations.d.ts +2 -0
- package/dist/engine/sync/sync-operations.js +3 -0
- package/dist/engine/sync/sync-policy.d.ts +85 -0
- package/dist/engine/sync/sync-policy.js +356 -0
- package/dist/engine/sync/sync-queries.d.ts +7 -0
- package/dist/engine/sync/sync-queries.js +195 -0
- package/dist/engine/sync/sync-state.d.ts +31 -0
- package/dist/engine/sync/sync-state.js +92 -0
- package/dist/engine/sync/text-merge.d.ts +19 -0
- package/dist/engine/sync/text-merge.js +124 -0
- package/dist/engine/types.d.ts +5 -0
- package/dist/engine/types.js +2 -0
- package/dist/engine/ui/merge-review.d.ts +2 -0
- package/dist/engine/ui/merge-review.js +7 -0
- package/dist/engine/ui/merged-sync-summary.d.ts +25 -0
- package/dist/engine/ui/merged-sync-summary.js +86 -0
- package/dist/engine/ui/sync-format.d.ts +13 -0
- package/dist/engine/ui/sync-format.js +150 -0
- package/dist/engine/ui/sync-status.d.ts +9 -0
- package/dist/engine/ui/sync-status.js +30 -0
- package/dist/engine/ui/terminal-text.d.ts +3 -0
- package/dist/engine/ui/terminal-text.js +9 -0
- package/dist/engine/ui/transfer-changes.d.ts +12 -0
- package/dist/engine/ui/transfer-changes.js +24 -0
- package/dist/engine/ui/transfer-review.d.ts +3 -0
- package/dist/engine/ui/transfer-review.js +19 -0
- package/dist/engine-service.d.ts +11 -0
- package/dist/engine-service.js +315 -0
- package/dist/identity.d.ts +20 -0
- package/dist/identity.js +133 -0
- package/dist/index.d.ts +8 -0
- package/dist/index.js +4 -0
- package/dist/lifecycle-log.d.ts +7 -0
- package/dist/lifecycle-log.js +53 -0
- package/dist/lifecycle.d.ts +2 -0
- package/dist/lifecycle.js +21 -0
- package/dist/model-signature.d.ts +2 -0
- package/dist/model-signature.js +7 -0
- package/dist/monitor.d.ts +11 -0
- package/dist/monitor.js +245 -0
- package/dist/participant-admission.d.ts +5 -0
- package/dist/participant-admission.js +30 -0
- package/dist/protocol.d.ts +97 -0
- package/dist/protocol.js +90 -0
- package/dist/resolver-host.d.ts +11 -0
- package/dist/resolver-host.js +78 -0
- package/dist/server.d.ts +18 -0
- package/dist/server.js +724 -0
- package/dist/service-host.d.ts +7 -0
- package/dist/service-host.js +174 -0
- package/docs/architecture.md +133 -0
- package/package.json +54 -4
package/dist/client.js
ADDED
|
@@ -0,0 +1,484 @@
|
|
|
1
|
+
import { spawn } from "node:child_process";
|
|
2
|
+
import { createHash, randomUUID } from "node:crypto";
|
|
3
|
+
import { request as httpRequest } from "node:http";
|
|
4
|
+
import { fileURLToPath } from "node:url";
|
|
5
|
+
import lockfile from "proper-lockfile";
|
|
6
|
+
import { daemonPaths, privateDirectory, processExists, readIdentity, readJsonBody, runtimePaths, } from "./identity.js";
|
|
7
|
+
import { withCleanup } from "./lifecycle.js";
|
|
8
|
+
import { MAX_MESSAGE_BYTES, MAX_REVIEW_BYTES, operationPrefix, REVIEW_PAGE_CHARS, record, } from "./protocol.js";
|
|
9
|
+
export class DaemonError extends Error {
|
|
10
|
+
remote;
|
|
11
|
+
constructor(remote) {
|
|
12
|
+
super(remote.message);
|
|
13
|
+
this.name = remote.name;
|
|
14
|
+
this.remote = remote;
|
|
15
|
+
}
|
|
16
|
+
}
|
|
17
|
+
export async function requestDaemon(paths, identity, method, params, signal) {
|
|
18
|
+
signal?.throwIfAborted();
|
|
19
|
+
const body = JSON.stringify({
|
|
20
|
+
version: 1,
|
|
21
|
+
root: paths.root,
|
|
22
|
+
instance: identity.instance,
|
|
23
|
+
token: identity.token,
|
|
24
|
+
method,
|
|
25
|
+
params,
|
|
26
|
+
});
|
|
27
|
+
if (Buffer.byteLength(body) > MAX_MESSAGE_BYTES)
|
|
28
|
+
throw new Error("Daemon request exceeds its byte limit.");
|
|
29
|
+
return new Promise((resolve, reject) => {
|
|
30
|
+
const request = httpRequest({
|
|
31
|
+
socketPath: daemonPaths(paths, identity.instance).endpoint,
|
|
32
|
+
path: "/v1",
|
|
33
|
+
method: "POST",
|
|
34
|
+
signal,
|
|
35
|
+
headers: { "content-type": "application/json", "content-length": Buffer.byteLength(body) },
|
|
36
|
+
}, (response) => {
|
|
37
|
+
void readJsonBody(response)
|
|
38
|
+
.then((value) => {
|
|
39
|
+
if (!record(value))
|
|
40
|
+
throw new Error("Invalid daemon response.");
|
|
41
|
+
if (record(value.error) && typeof value.error.message === "string")
|
|
42
|
+
throw new DaemonError(value.error);
|
|
43
|
+
if (response.statusCode !== 200 || !Object.hasOwn(value, "result"))
|
|
44
|
+
throw new Error("Invalid daemon response.");
|
|
45
|
+
resolve(value.result);
|
|
46
|
+
})
|
|
47
|
+
.catch(reject);
|
|
48
|
+
});
|
|
49
|
+
// Attach can wait behind a complete apply. Its connection-owned signal/dispose
|
|
50
|
+
// cancels that wait; an idle timeout must not let startup escape the admission fence.
|
|
51
|
+
request.setTimeout(method === "attach" ? 0 : 30_000, () => request.destroy(new Error("Daemon request timed out.")));
|
|
52
|
+
request.once("error", reject);
|
|
53
|
+
request.end(body);
|
|
54
|
+
});
|
|
55
|
+
}
|
|
56
|
+
async function hello(paths, identity) {
|
|
57
|
+
const value = await requestDaemon(paths, identity, "hello", {});
|
|
58
|
+
if (!record(value) ||
|
|
59
|
+
value.version !== 1 ||
|
|
60
|
+
value.root !== paths.root ||
|
|
61
|
+
value.instance !== identity.instance ||
|
|
62
|
+
value.pid !== identity.pid)
|
|
63
|
+
throw new Error("Daemon handshake mismatch.");
|
|
64
|
+
}
|
|
65
|
+
/** Explicit startup only: callers decide whether configuration or a user action authorizes it. */
|
|
66
|
+
export async function ensureDaemon(root, signal) {
|
|
67
|
+
const paths = await runtimePaths(root);
|
|
68
|
+
signal?.throwIfAborted();
|
|
69
|
+
await privateDirectory(paths.directory);
|
|
70
|
+
signal?.throwIfAborted();
|
|
71
|
+
const release = await lockfile.lock(paths.directory, {
|
|
72
|
+
realpath: false,
|
|
73
|
+
lockfilePath: paths.startup,
|
|
74
|
+
stale: 30_000,
|
|
75
|
+
update: 5_000,
|
|
76
|
+
retries: { retries: 100, factor: 1, minTimeout: 50, maxTimeout: 50 },
|
|
77
|
+
});
|
|
78
|
+
try {
|
|
79
|
+
signal?.throwIfAborted();
|
|
80
|
+
const existing = await readIdentity(paths);
|
|
81
|
+
if (existing) {
|
|
82
|
+
try {
|
|
83
|
+
await hello(paths, existing);
|
|
84
|
+
signal?.throwIfAborted();
|
|
85
|
+
return { paths: daemonPaths(paths, existing.instance), identity: existing };
|
|
86
|
+
}
|
|
87
|
+
catch (error) {
|
|
88
|
+
if (signal?.aborted)
|
|
89
|
+
throw signal.reason;
|
|
90
|
+
if (processExists(existing.pid))
|
|
91
|
+
throw new Error("A daemon owner is alive but unavailable; use diagnostics instead of starting a second owner.", { cause: error });
|
|
92
|
+
}
|
|
93
|
+
}
|
|
94
|
+
const executable = process.versions.bun ? "node" : process.execPath;
|
|
95
|
+
const child = spawn(executable, [fileURLToPath(new URL("./cli.js", import.meta.url)), "serve", "--root", paths.root], {
|
|
96
|
+
detached: true,
|
|
97
|
+
stdio: ["ignore", "ignore", "ignore", "ipc"],
|
|
98
|
+
windowsHide: true,
|
|
99
|
+
});
|
|
100
|
+
const ready = await new Promise((resolve, reject) => {
|
|
101
|
+
let timer;
|
|
102
|
+
const abort = () => {
|
|
103
|
+
cleanup();
|
|
104
|
+
reject(signal?.reason);
|
|
105
|
+
};
|
|
106
|
+
const cleanup = () => {
|
|
107
|
+
if (timer)
|
|
108
|
+
clearTimeout(timer);
|
|
109
|
+
signal?.removeEventListener("abort", abort);
|
|
110
|
+
child.off("message", message);
|
|
111
|
+
child.off("error", failed);
|
|
112
|
+
child.off("exit", exited);
|
|
113
|
+
};
|
|
114
|
+
const failed = (error) => {
|
|
115
|
+
cleanup();
|
|
116
|
+
reject(error);
|
|
117
|
+
};
|
|
118
|
+
const exited = (code) => failed(new Error(`Daemon startup exited (${code}); private state was retained.`));
|
|
119
|
+
const message = (value) => {
|
|
120
|
+
if (!record(value))
|
|
121
|
+
return;
|
|
122
|
+
if (value.type === "booting") {
|
|
123
|
+
timer = setTimeout(() => failed(new Error("Daemon readiness timed out.")), 15_000);
|
|
124
|
+
timer.unref?.();
|
|
125
|
+
}
|
|
126
|
+
if (value.type === "error")
|
|
127
|
+
failed(new Error(typeof value.message === "string" ? value.message : "Daemon startup failed."));
|
|
128
|
+
if (value.type === "ready" && record(value.identity)) {
|
|
129
|
+
cleanup();
|
|
130
|
+
resolve(value.identity);
|
|
131
|
+
}
|
|
132
|
+
};
|
|
133
|
+
signal?.addEventListener("abort", abort, { once: true });
|
|
134
|
+
child.on("message", message);
|
|
135
|
+
child.once("error", failed);
|
|
136
|
+
child.once("exit", exited);
|
|
137
|
+
if (signal?.aborted)
|
|
138
|
+
abort();
|
|
139
|
+
}).finally(() => {
|
|
140
|
+
if (child.connected)
|
|
141
|
+
child.disconnect();
|
|
142
|
+
child.unref();
|
|
143
|
+
});
|
|
144
|
+
// Never kill a published shared daemon on caller cancellation.
|
|
145
|
+
await hello(paths, ready);
|
|
146
|
+
signal?.throwIfAborted();
|
|
147
|
+
return { paths: daemonPaths(paths, ready.instance), identity: ready };
|
|
148
|
+
}
|
|
149
|
+
finally {
|
|
150
|
+
await release();
|
|
151
|
+
}
|
|
152
|
+
}
|
|
153
|
+
/** Inspect an existing compatible owner without starting one. */
|
|
154
|
+
export async function findDaemon(root) {
|
|
155
|
+
let paths;
|
|
156
|
+
try {
|
|
157
|
+
paths = await runtimePaths(root);
|
|
158
|
+
}
|
|
159
|
+
catch (error) {
|
|
160
|
+
if (error.code === "ENOENT")
|
|
161
|
+
return undefined;
|
|
162
|
+
throw error;
|
|
163
|
+
}
|
|
164
|
+
const identity = await readIdentity(paths);
|
|
165
|
+
if (!identity || !processExists(identity.pid))
|
|
166
|
+
return undefined;
|
|
167
|
+
await hello(paths, identity);
|
|
168
|
+
return { paths: daemonPaths(paths, identity.instance), identity };
|
|
169
|
+
}
|
|
170
|
+
/** Gracefully stop an existing compatible owner without creating a new service. */
|
|
171
|
+
export async function stopDaemon(root) {
|
|
172
|
+
const paths = await runtimePaths(root);
|
|
173
|
+
const identity = await readIdentity(paths);
|
|
174
|
+
if (!identity)
|
|
175
|
+
return false;
|
|
176
|
+
// The authenticated stop response is itself instance/root/version bound, even
|
|
177
|
+
// when a changed agent root makes ordinary health checks fail closed.
|
|
178
|
+
await requestDaemon(paths, identity, "stop", {});
|
|
179
|
+
const deadline = Date.now() + 30_000;
|
|
180
|
+
for (;;) {
|
|
181
|
+
const current = await readIdentity(paths);
|
|
182
|
+
if (!current || current.instance !== identity.instance)
|
|
183
|
+
return true;
|
|
184
|
+
if (Date.now() >= deadline)
|
|
185
|
+
throw new Error("Daemon did not drain; no replacement was attempted.");
|
|
186
|
+
await new Promise((resolve) => setTimeout(resolve, 25));
|
|
187
|
+
}
|
|
188
|
+
}
|
|
189
|
+
export class DaemonConnection {
|
|
190
|
+
paths;
|
|
191
|
+
identity;
|
|
192
|
+
facts;
|
|
193
|
+
options;
|
|
194
|
+
cursor = 0;
|
|
195
|
+
controller = new AbortController();
|
|
196
|
+
callbacks = new Map();
|
|
197
|
+
callbackIO = new AbortController();
|
|
198
|
+
callbackTasks = new Set();
|
|
199
|
+
updates = Promise.resolve();
|
|
200
|
+
attached = false;
|
|
201
|
+
uncertain = false;
|
|
202
|
+
reportedError = false;
|
|
203
|
+
commits = new Map();
|
|
204
|
+
completedCallbacks = new Set();
|
|
205
|
+
disposing;
|
|
206
|
+
polling;
|
|
207
|
+
closed = false;
|
|
208
|
+
constructor(paths, identity, facts, options) {
|
|
209
|
+
this.paths = paths;
|
|
210
|
+
this.identity = identity;
|
|
211
|
+
this.facts = facts;
|
|
212
|
+
this.options = options;
|
|
213
|
+
}
|
|
214
|
+
async attach() {
|
|
215
|
+
if (this.closed || this.uncertain)
|
|
216
|
+
throw new Error("Daemon client is unavailable; reconnect after draining work.");
|
|
217
|
+
const request = requestDaemon(this.paths, this.identity, "attach", this.facts, this.controller.signal);
|
|
218
|
+
// Poll during attach: stopping a previous controller may need release answers
|
|
219
|
+
// from this reattached participant before the attach response can complete.
|
|
220
|
+
this.polling ??= this.poll();
|
|
221
|
+
try {
|
|
222
|
+
const result = await request;
|
|
223
|
+
this.consume(result);
|
|
224
|
+
this.attached = true;
|
|
225
|
+
return this;
|
|
226
|
+
}
|
|
227
|
+
catch (error) {
|
|
228
|
+
return withCleanup(async () => {
|
|
229
|
+
throw error;
|
|
230
|
+
}, () => this.dispose());
|
|
231
|
+
}
|
|
232
|
+
}
|
|
233
|
+
update(facts) {
|
|
234
|
+
const update = this.updates.then(async () => {
|
|
235
|
+
if (this.closed || this.uncertain)
|
|
236
|
+
throw new Error("Daemon client is unavailable; reconnect after draining work.");
|
|
237
|
+
const result = await requestDaemon(this.paths, this.identity, "update", { facts }, this.controller.signal);
|
|
238
|
+
if (this.closed)
|
|
239
|
+
throw new Error("Daemon client closed during update.");
|
|
240
|
+
Object.assign(this.facts, facts);
|
|
241
|
+
this.consume(result);
|
|
242
|
+
});
|
|
243
|
+
this.updates = update.catch(() => { });
|
|
244
|
+
return update;
|
|
245
|
+
}
|
|
246
|
+
async run(operation, payload, signal, onCommit) {
|
|
247
|
+
if (this.closed || this.uncertain)
|
|
248
|
+
throw new Error("Daemon client is unavailable; reconnect after draining work.");
|
|
249
|
+
signal?.throwIfAborted();
|
|
250
|
+
const id = `${operationPrefix(this.identity)}${randomUUID()}`;
|
|
251
|
+
const abort = () => {
|
|
252
|
+
void requestDaemon(this.paths, this.identity, "cancel", { id, client: this.facts.id }, this.controller.signal).catch(() => { });
|
|
253
|
+
};
|
|
254
|
+
let terminal = false;
|
|
255
|
+
if (onCommit)
|
|
256
|
+
this.commits.set(id, { notify: onCommit, signal });
|
|
257
|
+
signal?.addEventListener("abort", abort, { once: true });
|
|
258
|
+
if (signal?.aborted)
|
|
259
|
+
abort();
|
|
260
|
+
return withCleanup(async () => {
|
|
261
|
+
await requestDaemon(this.paths, this.identity, "submit", { id, client: this.facts.id, operation, payload }, this.controller.signal);
|
|
262
|
+
while (!this.closed) {
|
|
263
|
+
const reply = await requestDaemon(this.paths, this.identity, "poll", { client: this.facts.id, cursor: this.cursor, job: id }, this.controller.signal);
|
|
264
|
+
this.consume(reply);
|
|
265
|
+
const job = record(reply) ? reply.job : undefined;
|
|
266
|
+
if (job?.phase === "failed") {
|
|
267
|
+
terminal = true;
|
|
268
|
+
throw new DaemonError(job.error ?? { name: "Error", message: "Daemon operation failed." });
|
|
269
|
+
}
|
|
270
|
+
if (job?.phase === "done") {
|
|
271
|
+
terminal = true;
|
|
272
|
+
signal?.throwIfAborted();
|
|
273
|
+
return job.result;
|
|
274
|
+
}
|
|
275
|
+
await this.wait(25, true);
|
|
276
|
+
}
|
|
277
|
+
throw new Error("Daemon client closed before operation completed.");
|
|
278
|
+
}, async () => {
|
|
279
|
+
signal?.removeEventListener("abort", abort);
|
|
280
|
+
this.commits.delete(id);
|
|
281
|
+
if (!terminal && !this.closed) {
|
|
282
|
+
for (const callback of this.callbacks.values())
|
|
283
|
+
if (callback.job === id && !callback.kind.startsWith("queue-"))
|
|
284
|
+
callback.controller.abort();
|
|
285
|
+
try {
|
|
286
|
+
await requestDaemon(this.paths, this.identity, "cancel", { id, client: this.facts.id });
|
|
287
|
+
}
|
|
288
|
+
catch (error) {
|
|
289
|
+
this.uncertain = true;
|
|
290
|
+
for (const callback of this.callbacks.values())
|
|
291
|
+
if (callback.kind !== "queue-release")
|
|
292
|
+
callback.controller.abort();
|
|
293
|
+
throw new Error("Operation outcome is uncertain; reload to reconnect and reconcile, never replay the write.", { cause: error });
|
|
294
|
+
}
|
|
295
|
+
}
|
|
296
|
+
});
|
|
297
|
+
}
|
|
298
|
+
consume(reply) {
|
|
299
|
+
if (!record(reply) ||
|
|
300
|
+
typeof reply.cursor !== "number" ||
|
|
301
|
+
!Number.isSafeInteger(reply.cursor) ||
|
|
302
|
+
reply.cursor < 0 ||
|
|
303
|
+
!Array.isArray(reply.callbacks) ||
|
|
304
|
+
!Array.isArray(reply.events))
|
|
305
|
+
throw new Error("Invalid daemon poll response.");
|
|
306
|
+
if (reply.cursor < this.cursor)
|
|
307
|
+
return;
|
|
308
|
+
for (const event of reply.events)
|
|
309
|
+
if (event.id > this.cursor) {
|
|
310
|
+
if (event.kind === "commit" && record(event.payload) && typeof event.payload.job === "string") {
|
|
311
|
+
const current = this.commits.get(event.payload.job);
|
|
312
|
+
this.commits.delete(event.payload.job);
|
|
313
|
+
if (current && !current.signal?.aborted)
|
|
314
|
+
current.notify();
|
|
315
|
+
}
|
|
316
|
+
this.options.event?.(event);
|
|
317
|
+
}
|
|
318
|
+
this.cursor = Math.max(this.cursor, reply.cursor);
|
|
319
|
+
const present = new Set(reply.callbacks.map((callback) => callback.id));
|
|
320
|
+
for (const [id, { controller }] of this.callbacks)
|
|
321
|
+
if (!present.has(id))
|
|
322
|
+
controller.abort(new DOMException("Callback is no longer current", "AbortError"));
|
|
323
|
+
for (const callback of reply.callbacks) {
|
|
324
|
+
if (this.callbacks.has(callback.id) || this.completedCallbacks.has(callback.id))
|
|
325
|
+
continue;
|
|
326
|
+
if (this.completedCallbacks.size >= 4096)
|
|
327
|
+
this.uncertain = true;
|
|
328
|
+
const controller = new AbortController();
|
|
329
|
+
this.callbacks.set(callback.id, { controller, kind: callback.kind, job: callback.job });
|
|
330
|
+
const task = (async () => {
|
|
331
|
+
let value;
|
|
332
|
+
let failed = false;
|
|
333
|
+
try {
|
|
334
|
+
if (this.uncertain && callback.kind !== "queue-release")
|
|
335
|
+
throw new Error("Client is draining uncertain work.");
|
|
336
|
+
value = await this.performCallback(callback, controller.signal);
|
|
337
|
+
}
|
|
338
|
+
catch {
|
|
339
|
+
failed = true;
|
|
340
|
+
}
|
|
341
|
+
await requestDaemon(this.paths, this.identity, "answer", {
|
|
342
|
+
client: this.facts.id,
|
|
343
|
+
id: callback.id,
|
|
344
|
+
...(failed ? { error: "Client callback cancelled or failed." } : { value }),
|
|
345
|
+
}, this.callbackIO.signal);
|
|
346
|
+
})()
|
|
347
|
+
.catch((error) => {
|
|
348
|
+
if (!this.closed && !controller.signal.aborted)
|
|
349
|
+
this.reportError(error);
|
|
350
|
+
})
|
|
351
|
+
.finally(() => {
|
|
352
|
+
if (this.completedCallbacks.size < 4096)
|
|
353
|
+
this.completedCallbacks.add(callback.id);
|
|
354
|
+
this.callbacks.delete(callback.id);
|
|
355
|
+
this.callbackTasks.delete(task);
|
|
356
|
+
});
|
|
357
|
+
this.callbackTasks.add(task);
|
|
358
|
+
}
|
|
359
|
+
}
|
|
360
|
+
async performCallback(callback, signal) {
|
|
361
|
+
const reference = callback.review;
|
|
362
|
+
if (!reference)
|
|
363
|
+
return this.options.callback(callback, signal);
|
|
364
|
+
if (callback.kind !== "merge-review" ||
|
|
365
|
+
!record(callback.payload) ||
|
|
366
|
+
!Number.isSafeInteger(reference.pages) ||
|
|
367
|
+
reference.pages < 1 ||
|
|
368
|
+
reference.pages > Math.ceil(MAX_REVIEW_BYTES / REVIEW_PAGE_CHARS) ||
|
|
369
|
+
!Number.isSafeInteger(reference.bytes) ||
|
|
370
|
+
reference.bytes > MAX_REVIEW_BYTES ||
|
|
371
|
+
reference.bytes < 1 ||
|
|
372
|
+
!/^[a-f0-9]{64}$/u.test(reference.sha256))
|
|
373
|
+
throw new Error("Invalid exact-review reference.");
|
|
374
|
+
const ownedSignal = AbortSignal.any([signal, this.callbackIO.signal]);
|
|
375
|
+
const pages = [];
|
|
376
|
+
for (let index = 0; index < reference.pages; index++) {
|
|
377
|
+
ownedSignal.throwIfAborted();
|
|
378
|
+
const page = await requestDaemon(this.paths, this.identity, "review-page", { client: this.facts.id, id: callback.id, index }, ownedSignal);
|
|
379
|
+
ownedSignal.throwIfAborted();
|
|
380
|
+
if (!record(page) ||
|
|
381
|
+
page.id !== callback.id ||
|
|
382
|
+
page.index !== index ||
|
|
383
|
+
typeof page.content !== "string" ||
|
|
384
|
+
page.content.length > REVIEW_PAGE_CHARS)
|
|
385
|
+
throw new Error("Invalid exact-review page.");
|
|
386
|
+
pages.push(page.content);
|
|
387
|
+
}
|
|
388
|
+
const content = pages.join("");
|
|
389
|
+
if (Buffer.byteLength(content) !== reference.bytes ||
|
|
390
|
+
createHash("sha256").update(content).digest("hex") !== reference.sha256)
|
|
391
|
+
throw new Error("Exact-review contents changed; approval was refused.");
|
|
392
|
+
ownedSignal.throwIfAborted();
|
|
393
|
+
return this.options.callback({ ...callback, review: undefined, payload: { ...callback.payload, content } }, signal);
|
|
394
|
+
}
|
|
395
|
+
async wait(ms, keepAlive = false) {
|
|
396
|
+
const signal = this.controller.signal;
|
|
397
|
+
signal.throwIfAborted();
|
|
398
|
+
await new Promise((resolve, reject) => {
|
|
399
|
+
const timer = setTimeout(done, ms);
|
|
400
|
+
if (!keepAlive)
|
|
401
|
+
timer.unref?.();
|
|
402
|
+
const abort = () => {
|
|
403
|
+
clearTimeout(timer);
|
|
404
|
+
signal.removeEventListener("abort", abort);
|
|
405
|
+
reject(signal.reason);
|
|
406
|
+
};
|
|
407
|
+
function done() {
|
|
408
|
+
signal.removeEventListener("abort", abort);
|
|
409
|
+
resolve();
|
|
410
|
+
}
|
|
411
|
+
signal.addEventListener("abort", abort, { once: true });
|
|
412
|
+
});
|
|
413
|
+
}
|
|
414
|
+
reportError(error) {
|
|
415
|
+
if (this.reportedError)
|
|
416
|
+
return;
|
|
417
|
+
this.reportedError = true;
|
|
418
|
+
try {
|
|
419
|
+
this.options.error?.(error);
|
|
420
|
+
}
|
|
421
|
+
catch {
|
|
422
|
+
/* Error presentation must not own a task. */
|
|
423
|
+
}
|
|
424
|
+
}
|
|
425
|
+
async poll() {
|
|
426
|
+
while (!this.closed) {
|
|
427
|
+
try {
|
|
428
|
+
const reply = await requestDaemon(this.paths, this.identity, "poll", { client: this.facts.id, cursor: this.cursor }, this.controller.signal);
|
|
429
|
+
this.consume(reply);
|
|
430
|
+
this.reportedError = false;
|
|
431
|
+
await this.wait(100);
|
|
432
|
+
}
|
|
433
|
+
catch (error) {
|
|
434
|
+
if (this.closed)
|
|
435
|
+
return;
|
|
436
|
+
if (this.attached)
|
|
437
|
+
this.reportError(error);
|
|
438
|
+
try {
|
|
439
|
+
await this.wait(100);
|
|
440
|
+
}
|
|
441
|
+
catch {
|
|
442
|
+
return;
|
|
443
|
+
}
|
|
444
|
+
}
|
|
445
|
+
}
|
|
446
|
+
}
|
|
447
|
+
async pause() {
|
|
448
|
+
if (this.closed)
|
|
449
|
+
throw new Error("Daemon client is closed.");
|
|
450
|
+
const reply = await requestDaemon(this.paths, this.identity, "pause", { client: this.facts.id });
|
|
451
|
+
this.consume(reply);
|
|
452
|
+
}
|
|
453
|
+
async dispose(handoff) {
|
|
454
|
+
if (this.disposing)
|
|
455
|
+
return this.disposing;
|
|
456
|
+
if (this.closed)
|
|
457
|
+
return;
|
|
458
|
+
this.disposing = (async () => {
|
|
459
|
+
this.polling ??= this.poll();
|
|
460
|
+
// Cancel UI before drain, but keep polling for acknowledged queue-release callbacks.
|
|
461
|
+
for (const { controller, kind } of this.callbacks.values())
|
|
462
|
+
if (!kind.startsWith("queue-"))
|
|
463
|
+
controller.abort();
|
|
464
|
+
try {
|
|
465
|
+
const reply = await requestDaemon(this.paths, this.identity, handoff ? "handoff" : "detach", {
|
|
466
|
+
client: this.facts.id,
|
|
467
|
+
generation: this.facts.generation,
|
|
468
|
+
pid: this.facts.pid,
|
|
469
|
+
handoff,
|
|
470
|
+
});
|
|
471
|
+
this.consume(reply);
|
|
472
|
+
}
|
|
473
|
+
finally {
|
|
474
|
+
this.closed = true;
|
|
475
|
+
this.controller.abort(new DOMException("Client disposed", "AbortError"));
|
|
476
|
+
this.callbackIO.abort(new DOMException("Client callback transport disposed", "AbortError"));
|
|
477
|
+
for (const { controller } of this.callbacks.values())
|
|
478
|
+
controller.abort();
|
|
479
|
+
await Promise.allSettled([this.polling, this.updates, ...this.callbackTasks]);
|
|
480
|
+
}
|
|
481
|
+
})();
|
|
482
|
+
return this.disposing;
|
|
483
|
+
}
|
|
484
|
+
}
|
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
import type { AnySyncConfig } from "./engine/settings/settings-types.js";
|
|
2
|
+
import { type ClientFacts, type Execution } from "./protocol.js";
|
|
3
|
+
interface Consent {
|
|
4
|
+
version: 1;
|
|
5
|
+
root: string;
|
|
6
|
+
handover: true;
|
|
7
|
+
nonce: string;
|
|
8
|
+
setups: Record<string, {
|
|
9
|
+
fingerprint: string;
|
|
10
|
+
model?: ClientFacts["model"];
|
|
11
|
+
sessionDir?: string;
|
|
12
|
+
cwd?: string;
|
|
13
|
+
afterExit: boolean;
|
|
14
|
+
}>;
|
|
15
|
+
}
|
|
16
|
+
export declare class ConsentPublicationError extends Error {
|
|
17
|
+
}
|
|
18
|
+
/** A different interactive binding cannot silently resume an older unattended grant after exit/restart. */
|
|
19
|
+
export declare function revokeChangedBindings(root: string, peers: readonly ClientFacts[]): void;
|
|
20
|
+
export declare function readConsent(root: string): Consent | undefined;
|
|
21
|
+
export declare function consentFingerprint(root: string, config: AnySyncConfig): string;
|
|
22
|
+
export declare function requireHandover(execution: Execution): Promise<void>;
|
|
23
|
+
export declare function authorizeAfterExit(execution: Execution, payload: unknown): Promise<boolean>;
|
|
24
|
+
export {};
|