@narumitw/pi-sync-daemon 0.9.1 → 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.
- package/CHANGELOG.md +11 -0
- package/README.md +11 -1
- package/dist/cli.js +2 -0
- package/dist/client-transport.d.ts +14 -0
- package/dist/client-transport.js +66 -0
- package/dist/client.d.ts +15 -8
- package/dist/client.js +116 -86
- package/dist/consent.js +33 -11
- package/dist/daemon-debug.d.ts +3 -0
- package/dist/daemon-debug.js +33 -0
- package/dist/daemon-recovery.d.ts +4 -0
- package/dist/daemon-recovery.js +75 -0
- package/dist/engine/backends/git/git-backend.d.ts +8 -0
- package/dist/engine/backends/git/git-backend.js +39 -12
- package/dist/engine/backends/git/git-runner.js +6 -1
- package/dist/engine/backends/git/git-storage.js +5 -9
- package/dist/engine/backends/publication-encoding.d.ts +3 -0
- package/dist/engine/backends/publication-encoding.js +19 -0
- package/dist/engine/backends/s3/s3-backend.js +10 -12
- package/dist/engine/backends/sync-backend.d.ts +4 -0
- package/dist/engine/backends/webdav/webdav-backend.d.ts +0 -2
- package/dist/engine/backends/webdav/webdav-backend.js +15 -26
- package/dist/engine/backends/webdav/webdav-client.js +10 -0
- package/dist/engine/commands/command-types.d.ts +1 -0
- package/dist/engine/debug-log.d.ts +27 -0
- package/dist/engine/debug-log.js +134 -0
- package/dist/engine/paths.d.ts +2 -0
- package/dist/engine/paths.js +7 -1
- package/dist/engine/settings/config-file.d.ts +10 -1
- package/dist/engine/settings/config-file.js +62 -17
- package/dist/engine/settings/config.d.ts +7 -0
- package/dist/engine/settings/config.js +49 -14
- package/dist/engine/snapshot/snapshot-apply.d.ts +2 -1
- package/dist/engine/snapshot/snapshot-apply.js +30 -8
- package/dist/engine/snapshot/snapshot-codec.js +3 -1
- package/dist/engine/snapshot/snapshot-payload.d.ts +4 -0
- package/dist/engine/snapshot/snapshot-payload.js +32 -0
- package/dist/engine/snapshot/snapshot-transaction-images.d.ts +2 -0
- package/dist/engine/snapshot/snapshot-transaction-images.js +34 -0
- package/dist/engine/snapshot/snapshot-transaction.d.ts +4 -1
- package/dist/engine/snapshot/snapshot-transaction.js +50 -7
- package/dist/engine/snapshot/snapshot-types.d.ts +10 -2
- package/dist/engine/snapshot/snapshot.d.ts +3 -1
- package/dist/engine/snapshot/snapshot.js +64 -25
- package/dist/engine/state/lock.d.ts +2 -1
- package/dist/engine/state/lock.js +157 -17
- package/dist/engine/sync/continuous-sync.d.ts +10 -1
- package/dist/engine/sync/continuous-sync.js +35 -11
- package/dist/engine/sync/initialization.d.ts +1 -1
- package/dist/engine/sync/initialization.js +2 -2
- package/dist/engine/sync/local-fields.d.ts +6 -1
- package/dist/engine/sync/local-fields.js +42 -19
- package/dist/engine/sync/merged-sync.js +13 -10
- package/dist/engine/sync/remote-snapshot.js +18 -3
- package/dist/engine/sync/session-push.js +13 -4
- package/dist/engine/sync/startup-bootstrap.d.ts +1 -0
- package/dist/engine/sync/startup-bootstrap.js +105 -0
- package/dist/engine/sync/startup-recovery-evidence.d.ts +1 -0
- package/dist/engine/sync/startup-recovery-evidence.js +37 -0
- package/dist/engine/sync/startup-timings.d.ts +9 -2
- package/dist/engine/sync/startup-timings.js +80 -8
- package/dist/engine/sync/sync-error-guidance.js +6 -1
- package/dist/engine/sync/sync-inspection.js +4 -7
- package/dist/engine/sync/sync-local.d.ts +2 -1
- package/dist/engine/sync/sync-local.js +3 -1
- package/dist/engine/sync/sync-mutations.js +9 -3
- package/dist/engine/sync/sync-policy.d.ts +2 -0
- package/dist/engine/sync/sync-policy.js +9 -1
- package/dist/engine/sync/sync-state.d.ts +8 -3
- package/dist/engine/sync/sync-state.js +12 -4
- package/dist/engine-service.d.ts +6 -0
- package/dist/engine-service.js +54 -10
- package/dist/identity.d.ts +5 -2
- package/dist/identity.js +36 -6
- package/dist/index.d.ts +2 -1
- package/dist/index.js +2 -1
- package/dist/monitor.js +20 -1
- package/dist/paged-review.d.ts +35 -0
- package/dist/paged-review.js +177 -0
- package/dist/participant-ledger.d.ts +16 -0
- package/dist/participant-ledger.js +149 -0
- package/dist/protocol.d.ts +15 -0
- package/dist/protocol.js +4 -0
- package/dist/server.d.ts +7 -0
- package/dist/server.js +268 -120
- package/dist/service-host.d.ts +2 -1
- package/dist/service-host.js +29 -9
- package/dist/state-wait-jobs.d.ts +8 -0
- package/dist/state-wait-jobs.js +23 -0
- package/dist/state-wait.d.ts +7 -0
- package/dist/state-wait.js +34 -0
- package/docs/architecture.md +22 -0
- package/package.json +1 -1
|
@@ -0,0 +1,149 @@
|
|
|
1
|
+
var __addDisposableResource = (this && this.__addDisposableResource) || function (env, value, async) {
|
|
2
|
+
if (value !== null && value !== void 0) {
|
|
3
|
+
if (typeof value !== "object" && typeof value !== "function") throw new TypeError("Object expected.");
|
|
4
|
+
var dispose, inner;
|
|
5
|
+
if (async) {
|
|
6
|
+
if (!Symbol.asyncDispose) throw new TypeError("Symbol.asyncDispose is not defined.");
|
|
7
|
+
dispose = value[Symbol.asyncDispose];
|
|
8
|
+
}
|
|
9
|
+
if (dispose === void 0) {
|
|
10
|
+
if (!Symbol.dispose) throw new TypeError("Symbol.dispose is not defined.");
|
|
11
|
+
dispose = value[Symbol.dispose];
|
|
12
|
+
if (async) inner = dispose;
|
|
13
|
+
}
|
|
14
|
+
if (typeof dispose !== "function") throw new TypeError("Object not disposable.");
|
|
15
|
+
if (inner) dispose = function() { try { inner.call(this); } catch (e) { return Promise.reject(e); } };
|
|
16
|
+
env.stack.push({ value: value, dispose: dispose, async: async });
|
|
17
|
+
}
|
|
18
|
+
else if (async) {
|
|
19
|
+
env.stack.push({ async: true });
|
|
20
|
+
}
|
|
21
|
+
return value;
|
|
22
|
+
};
|
|
23
|
+
var __disposeResources = (this && this.__disposeResources) || (function (SuppressedError) {
|
|
24
|
+
return function (env) {
|
|
25
|
+
function fail(e) {
|
|
26
|
+
env.error = env.hasError ? new SuppressedError(e, env.error, "An error was suppressed during disposal.") : e;
|
|
27
|
+
env.hasError = true;
|
|
28
|
+
}
|
|
29
|
+
var r, s = 0;
|
|
30
|
+
function next() {
|
|
31
|
+
while (r = env.stack.pop()) {
|
|
32
|
+
try {
|
|
33
|
+
if (!r.async && s === 1) return s = 0, env.stack.push(r), Promise.resolve().then(next);
|
|
34
|
+
if (r.dispose) {
|
|
35
|
+
var result = r.dispose.call(r.value);
|
|
36
|
+
if (r.async) return s |= 2, Promise.resolve(result).then(next, function(e) { fail(e); return next(); });
|
|
37
|
+
}
|
|
38
|
+
else s |= 1;
|
|
39
|
+
}
|
|
40
|
+
catch (e) {
|
|
41
|
+
fail(e);
|
|
42
|
+
}
|
|
43
|
+
}
|
|
44
|
+
if (s === 1) return env.hasError ? Promise.reject(env.error) : Promise.resolve();
|
|
45
|
+
if (env.hasError) throw env.error;
|
|
46
|
+
}
|
|
47
|
+
return next();
|
|
48
|
+
};
|
|
49
|
+
})(typeof SuppressedError === "function" ? SuppressedError : function (error, suppressed, message) {
|
|
50
|
+
var e = new Error(message);
|
|
51
|
+
return e.name = "SuppressedError", e.error = error, e.suppressed = suppressed, e;
|
|
52
|
+
});
|
|
53
|
+
import { randomUUID } from "node:crypto";
|
|
54
|
+
import fs from "node:fs/promises";
|
|
55
|
+
import path from "node:path";
|
|
56
|
+
import { processExists } from "./identity.js";
|
|
57
|
+
import { MAX_MESSAGE_BYTES, record, validateFacts, validateHandoff, } from "./protocol.js";
|
|
58
|
+
export function createParticipantLedger(paths, participants, onFailure) {
|
|
59
|
+
const file = path.join(paths.directory, "clients.json");
|
|
60
|
+
let writes = Promise.resolve();
|
|
61
|
+
let published;
|
|
62
|
+
let failed = false;
|
|
63
|
+
async function write(candidate, allowNoop) {
|
|
64
|
+
const bytes = JSON.stringify({
|
|
65
|
+
version: 1,
|
|
66
|
+
root: paths.root,
|
|
67
|
+
clients: [...participants()]
|
|
68
|
+
.filter((peer) => processExists(peer.facts.pid))
|
|
69
|
+
.map((peer) => ({ ...(candidate?.peer === peer ? candidate.facts : peer.facts), handoff: peer.handoff })),
|
|
70
|
+
});
|
|
71
|
+
if (allowNoop && !failed && bytes === published)
|
|
72
|
+
return;
|
|
73
|
+
const temporary = path.join(paths.directory, `.${randomUUID()}.clients`);
|
|
74
|
+
const handle = await fs.open(temporary, "wx", 0o600);
|
|
75
|
+
try {
|
|
76
|
+
await handle.writeFile(bytes);
|
|
77
|
+
await handle.sync();
|
|
78
|
+
await handle.close();
|
|
79
|
+
await fs.rename(temporary, file);
|
|
80
|
+
published = bytes;
|
|
81
|
+
}
|
|
82
|
+
finally {
|
|
83
|
+
await handle.close().catch(() => { });
|
|
84
|
+
await fs.rm(temporary, { force: true });
|
|
85
|
+
}
|
|
86
|
+
}
|
|
87
|
+
return {
|
|
88
|
+
get failed() {
|
|
89
|
+
return failed;
|
|
90
|
+
},
|
|
91
|
+
drain: () => writes,
|
|
92
|
+
save(candidate, allowNoop) {
|
|
93
|
+
const operation = writes.then(() => write(candidate, allowNoop));
|
|
94
|
+
writes = operation.catch(() => {
|
|
95
|
+
failed = true;
|
|
96
|
+
onFailure();
|
|
97
|
+
});
|
|
98
|
+
return operation;
|
|
99
|
+
},
|
|
100
|
+
async restore() {
|
|
101
|
+
try {
|
|
102
|
+
const env_1 = { stack: [], error: void 0, hasError: false };
|
|
103
|
+
try {
|
|
104
|
+
const before = await fs.lstat(file);
|
|
105
|
+
if (!before.isFile() ||
|
|
106
|
+
before.isSymbolicLink() ||
|
|
107
|
+
before.size > MAX_MESSAGE_BYTES ||
|
|
108
|
+
(process.getuid && before.uid !== process.getuid()) ||
|
|
109
|
+
(process.platform !== "win32" && before.mode & 0o077))
|
|
110
|
+
throw new Error("Unsafe daemon client record.");
|
|
111
|
+
const handle = __addDisposableResource(env_1, await fs.open(file, "r"), true);
|
|
112
|
+
const opened = await handle.stat();
|
|
113
|
+
if (opened.dev !== before.dev || opened.ino !== before.ino)
|
|
114
|
+
throw new Error("Daemon client record changed while opening.");
|
|
115
|
+
const data = JSON.parse(await handle.readFile("utf8"));
|
|
116
|
+
if (!record(data) ||
|
|
117
|
+
data.version !== 1 ||
|
|
118
|
+
data.root !== paths.root ||
|
|
119
|
+
!Array.isArray(data.clients) ||
|
|
120
|
+
data.clients.length > 64)
|
|
121
|
+
throw new Error("Invalid daemon client record; evidence retained.");
|
|
122
|
+
return data.clients.map((stored) => {
|
|
123
|
+
if (!record(stored))
|
|
124
|
+
throw new Error("Invalid stored participant.");
|
|
125
|
+
const { handoff, ...facts } = stored;
|
|
126
|
+
validateFacts(facts);
|
|
127
|
+
if (handoff !== undefined)
|
|
128
|
+
validateHandoff(handoff);
|
|
129
|
+
return { facts, handoff };
|
|
130
|
+
});
|
|
131
|
+
}
|
|
132
|
+
catch (e_1) {
|
|
133
|
+
env_1.error = e_1;
|
|
134
|
+
env_1.hasError = true;
|
|
135
|
+
}
|
|
136
|
+
finally {
|
|
137
|
+
const result_1 = __disposeResources(env_1);
|
|
138
|
+
if (result_1)
|
|
139
|
+
await result_1;
|
|
140
|
+
}
|
|
141
|
+
}
|
|
142
|
+
catch (error) {
|
|
143
|
+
if (error.code === "ENOENT")
|
|
144
|
+
return [];
|
|
145
|
+
throw error;
|
|
146
|
+
}
|
|
147
|
+
},
|
|
148
|
+
};
|
|
149
|
+
}
|
package/dist/protocol.d.ts
CHANGED
|
@@ -1,5 +1,7 @@
|
|
|
1
1
|
import type { Api, Model } from "@earendil-works/pi-ai";
|
|
2
|
+
import type { DaemonDiagnosticSink } from "./daemon-debug.js";
|
|
2
3
|
export declare const PROTOCOL_VERSION = 1;
|
|
4
|
+
export declare const OWNERSHIP_CLEANUP_REQUIRED = "DAEMON_OWNERSHIP_CLEANUP";
|
|
3
5
|
/** Authenticated idle shutdown after work is drained; callers may wait for this owner to exit. */
|
|
4
6
|
export declare class DaemonStoppingError extends Error {
|
|
5
7
|
name: string;
|
|
@@ -56,6 +58,7 @@ export interface Request {
|
|
|
56
58
|
token: string;
|
|
57
59
|
method: string;
|
|
58
60
|
params: unknown;
|
|
61
|
+
reviewDetails?: 1;
|
|
59
62
|
}
|
|
60
63
|
export interface Callback {
|
|
61
64
|
id: string;
|
|
@@ -66,6 +69,7 @@ export interface Callback {
|
|
|
66
69
|
pages: number;
|
|
67
70
|
bytes: number;
|
|
68
71
|
sha256: string;
|
|
72
|
+
offer?: string;
|
|
69
73
|
};
|
|
70
74
|
}
|
|
71
75
|
export interface ClientEvent {
|
|
@@ -85,6 +89,14 @@ export interface JobView {
|
|
|
85
89
|
error?: RemoteError;
|
|
86
90
|
}
|
|
87
91
|
export type ClientContact = "active" | "expired" | "inactive";
|
|
92
|
+
export declare const DAEMON_JOB_CAPACITY = 64;
|
|
93
|
+
export declare const UNKNOWN_JOB_RESULT = "Operation is unknown or its result expired; reconcile journals instead of replaying.";
|
|
94
|
+
export declare const STATE_WAIT_HEARTBEAT_MS = 1000;
|
|
95
|
+
export interface StateWaitReply extends PollReply {
|
|
96
|
+
stateRevision: string;
|
|
97
|
+
finished: string[];
|
|
98
|
+
missing: string[];
|
|
99
|
+
}
|
|
88
100
|
export interface PollReply {
|
|
89
101
|
cursor: number;
|
|
90
102
|
callbacks: Callback[];
|
|
@@ -92,6 +104,7 @@ export interface PollReply {
|
|
|
92
104
|
job?: JobView;
|
|
93
105
|
/** Absent on owners without protocol-v1 contact resume support. */
|
|
94
106
|
contact?: ClientContact;
|
|
107
|
+
stateWait?: 1;
|
|
95
108
|
}
|
|
96
109
|
export interface Execution {
|
|
97
110
|
readonly root: string;
|
|
@@ -106,6 +119,8 @@ export interface Execution {
|
|
|
106
119
|
validate(): void;
|
|
107
120
|
request(kind: string, payload: unknown, clientId?: string, signal?: AbortSignal): Promise<unknown>;
|
|
108
121
|
emit(kind: ClientEvent["kind"], payload: unknown, clientId?: string): void;
|
|
122
|
+
debug?: DaemonDiagnosticSink;
|
|
123
|
+
noteDebugOutcome?(outcome: "blocked" | "cancelled" | "skipped"): void;
|
|
109
124
|
committed(): void;
|
|
110
125
|
}
|
|
111
126
|
export type Execute = (operation: string, payload: unknown, execution: Execution) => Promise<unknown>;
|
package/dist/protocol.js
CHANGED
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
import { createHash } from "node:crypto";
|
|
2
2
|
import path from "node:path";
|
|
3
3
|
export const PROTOCOL_VERSION = 1;
|
|
4
|
+
export const OWNERSHIP_CLEANUP_REQUIRED = "DAEMON_OWNERSHIP_CLEANUP";
|
|
4
5
|
/** Authenticated idle shutdown after work is drained; callers may wait for this owner to exit. */
|
|
5
6
|
export class DaemonStoppingError extends Error {
|
|
6
7
|
name = "DaemonStoppingError";
|
|
@@ -27,6 +28,9 @@ export function validateHandoff(value) {
|
|
|
27
28
|
["file", "cwd"].some((key) => typeof value[key] === "string" && !path.isAbsolute(value[key])))
|
|
28
29
|
throw new Error("Invalid client handoff.");
|
|
29
30
|
}
|
|
31
|
+
export const DAEMON_JOB_CAPACITY = 64;
|
|
32
|
+
export const UNKNOWN_JOB_RESULT = "Operation is unknown or its result expired; reconcile journals instead of replaying.";
|
|
33
|
+
export const STATE_WAIT_HEARTBEAT_MS = 1_000;
|
|
30
34
|
export function record(value) {
|
|
31
35
|
return typeof value === "object" && value !== null && !Array.isArray(value);
|
|
32
36
|
}
|
package/dist/server.d.ts
CHANGED
|
@@ -1,6 +1,11 @@
|
|
|
1
|
+
import type { DaemonDiagnosticSink } from "./daemon-debug.js";
|
|
1
2
|
import { type IdleExitPolicy } from "./idle-exit.js";
|
|
2
3
|
import { type ClientFacts, type Execute, type Execution, type Identity } from "./protocol.js";
|
|
3
4
|
export interface ServerHooks {
|
|
5
|
+
startup?: {
|
|
6
|
+
captureReviewLock(): (() => boolean) | undefined;
|
|
7
|
+
probe(fingerprint: string, signal: AbortSignal, reviewLock?: () => boolean): Promise<(() => boolean) | undefined>;
|
|
8
|
+
};
|
|
4
9
|
factsChanged?(): Promise<void>;
|
|
5
10
|
idle?(): void;
|
|
6
11
|
close?(): Promise<void>;
|
|
@@ -8,11 +13,13 @@ export interface ServerHooks {
|
|
|
8
13
|
quiesce?(): Promise<void>;
|
|
9
14
|
stopped?(): void;
|
|
10
15
|
failure?(): void;
|
|
16
|
+
diagnostic?: DaemonDiagnosticSink;
|
|
11
17
|
}
|
|
12
18
|
export declare function startServer(root: string, execute: Execute, hooks?: ServerHooks): Promise<{
|
|
13
19
|
identity: Identity;
|
|
14
20
|
paths: import("./identity.js").DaemonPaths;
|
|
15
21
|
close: () => Promise<void>;
|
|
22
|
+
recordBootOutcome: (outcome: "ready" | "failed") => void | undefined;
|
|
16
23
|
backgroundExecution: (signal: AbortSignal) => Execution;
|
|
17
24
|
readonly retainedEndpoint: string | undefined;
|
|
18
25
|
participants: () => ClientFacts[];
|