@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.
- package/CHANGELOG.md +19 -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/ignore/ignore-initialization.d.ts +5 -1
- package/dist/engine/ignore/ignore-initialization.js +50 -14
- package/dist/engine/ignore/ignore-settings.js +11 -14
- package/dist/engine/ignore/ignore-source.d.ts +1 -1
- package/dist/engine/ignore/ignore-source.js +1 -1
- 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 +22 -4
- 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 +13 -7
- 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/ui/sync-format.d.ts +16 -5
- package/dist/engine/ui/sync-format.js +23 -55
- package/dist/engine/ui/transfer-review.d.ts +2 -1
- package/dist/engine/ui/transfer-review.js +3 -3
- 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 +23 -1
- package/package.json +1 -1
package/dist/monitor.js
CHANGED
|
@@ -23,6 +23,7 @@ export function createMonitor(root, host) {
|
|
|
23
23
|
const retryOwner = (peer) => JSON.stringify([peer.generation, peer.cwd, peer.sessionId, peer.sessionFile, peer.sessionDir, peer.model]);
|
|
24
24
|
let epoch;
|
|
25
25
|
const timingOwners = new WeakMap();
|
|
26
|
+
const cycleOutcomes = new Map();
|
|
26
27
|
const loaders = createEngineLoaders();
|
|
27
28
|
let continuous;
|
|
28
29
|
function schedule() {
|
|
@@ -116,7 +117,25 @@ export function createMonitor(root, host) {
|
|
|
116
117
|
const { createContinuousSync } = await import("./engine/sync/continuous-sync.js");
|
|
117
118
|
if (!available())
|
|
118
119
|
return;
|
|
119
|
-
continuous = createContinuousSync(loaders, undefined, (ctx, report) => timingOwners.get(ctx)?.emit("timings", { report })
|
|
120
|
+
continuous = createContinuousSync(loaders, undefined, (ctx, report) => timingOwners.get(ctx)?.emit("timings", { report }), {
|
|
121
|
+
timing: (ctx, report) => timingOwners.get(ctx)?.debug?.({ event: "startup-timings", report }),
|
|
122
|
+
cycle(ctx, setup, outcome) {
|
|
123
|
+
const debug = timingOwners.get(ctx)?.debug;
|
|
124
|
+
if (!debug)
|
|
125
|
+
return;
|
|
126
|
+
const key = `${outcome.outcome}:${outcome.reason ?? ""}`;
|
|
127
|
+
if (outcome.outcome !== "completed" && cycleOutcomes.get(setup) === key)
|
|
128
|
+
return;
|
|
129
|
+
cycleOutcomes.delete(setup);
|
|
130
|
+
cycleOutcomes.set(setup, key);
|
|
131
|
+
if (cycleOutcomes.size > 64) {
|
|
132
|
+
const oldest = cycleOutcomes.keys().next().value;
|
|
133
|
+
if (oldest !== undefined)
|
|
134
|
+
cycleOutcomes.delete(oldest);
|
|
135
|
+
}
|
|
136
|
+
debug({ event: "continuous-cycle", ...outcome });
|
|
137
|
+
},
|
|
138
|
+
});
|
|
120
139
|
// Re-enter every eligibility branch after importing the full engine graph.
|
|
121
140
|
return start();
|
|
122
141
|
}
|
|
@@ -0,0 +1,35 @@
|
|
|
1
|
+
import { type Callback, type ClientFacts, type Identity } from "./protocol.js";
|
|
2
|
+
interface ReviewScope {
|
|
3
|
+
instance: string;
|
|
4
|
+
client: string;
|
|
5
|
+
generation: string;
|
|
6
|
+
id: string;
|
|
7
|
+
job: string;
|
|
8
|
+
}
|
|
9
|
+
export declare function reviewScope(identity: Pick<Identity, "instance">, facts: Pick<ClientFacts, "id" | "generation">, callback: Pick<Callback, "id" | "job">): ReviewScope;
|
|
10
|
+
export declare function createPagedReviewOffer(scope: ReviewScope, data: unknown, content: string, bytes: number): {
|
|
11
|
+
summary: Readonly<{
|
|
12
|
+
title: string;
|
|
13
|
+
confirmationLabel: string | undefined;
|
|
14
|
+
summaryLines: readonly string[];
|
|
15
|
+
}>;
|
|
16
|
+
reference: Readonly<{
|
|
17
|
+
pages: number;
|
|
18
|
+
bytes: number;
|
|
19
|
+
sha256: string;
|
|
20
|
+
offer: string;
|
|
21
|
+
}>;
|
|
22
|
+
generation: string;
|
|
23
|
+
page(params: Record<string, unknown>): {
|
|
24
|
+
id: string;
|
|
25
|
+
index: number;
|
|
26
|
+
offer: string;
|
|
27
|
+
content: string;
|
|
28
|
+
};
|
|
29
|
+
approved(value: unknown): boolean;
|
|
30
|
+
} | undefined;
|
|
31
|
+
export type MergeReviewPresenter = (callback: Callback, loadDetails: (signal?: AbortSignal) => Promise<string>, signal: AbortSignal) => Promise<boolean>;
|
|
32
|
+
export declare function presentPagedReview(scope: ReviewScope, callback: Callback, signal: AbortSignal, present: MergeReviewPresenter, requestPage: (params: Record<string, unknown>, signal: AbortSignal) => Promise<unknown>): Promise<false | {
|
|
33
|
+
approvedReview: string;
|
|
34
|
+
}>;
|
|
35
|
+
export {};
|
|
@@ -0,0 +1,177 @@
|
|
|
1
|
+
import { createHash } from "node:crypto";
|
|
2
|
+
import { MAX_REVIEW_BYTES, REVIEW_PAGE_CHARS, record, } from "./protocol.js";
|
|
3
|
+
const SUMMARY_ROWS = 12;
|
|
4
|
+
const SUMMARY_ROW_CHARS = 240;
|
|
5
|
+
const HASH = /^[a-f0-9]{64}$/u;
|
|
6
|
+
export function reviewScope(identity, facts, callback) {
|
|
7
|
+
return {
|
|
8
|
+
instance: identity.instance,
|
|
9
|
+
client: facts.id,
|
|
10
|
+
generation: facts.generation,
|
|
11
|
+
id: callback.id,
|
|
12
|
+
job: callback.job,
|
|
13
|
+
};
|
|
14
|
+
}
|
|
15
|
+
function commitment(scope, summary, body) {
|
|
16
|
+
return createHash("sha256")
|
|
17
|
+
.update(JSON.stringify([
|
|
18
|
+
"pi-sync-merge-review-v1",
|
|
19
|
+
scope.instance,
|
|
20
|
+
scope.client,
|
|
21
|
+
scope.generation,
|
|
22
|
+
scope.id,
|
|
23
|
+
scope.job,
|
|
24
|
+
summary.title,
|
|
25
|
+
summary.confirmationLabel ?? null,
|
|
26
|
+
summary.summaryLines,
|
|
27
|
+
body.pages,
|
|
28
|
+
body.bytes,
|
|
29
|
+
body.sha256,
|
|
30
|
+
]))
|
|
31
|
+
.digest("hex");
|
|
32
|
+
}
|
|
33
|
+
function exactHash(content) {
|
|
34
|
+
return createHash("sha256").update(content, "utf16le").digest("hex");
|
|
35
|
+
}
|
|
36
|
+
function presentation(value) {
|
|
37
|
+
return (record(value) &&
|
|
38
|
+
typeof value.title === "string" &&
|
|
39
|
+
value.title.length > 0 &&
|
|
40
|
+
value.title.length <= 512 &&
|
|
41
|
+
(value.confirmationLabel === undefined ||
|
|
42
|
+
(typeof value.confirmationLabel === "string" && value.confirmationLabel.length <= 256)));
|
|
43
|
+
}
|
|
44
|
+
export function createPagedReviewOffer(scope, data, content, bytes) {
|
|
45
|
+
if (!presentation(data))
|
|
46
|
+
return undefined;
|
|
47
|
+
const supplied = data.summaryLines;
|
|
48
|
+
const candidates = Array.isArray(supplied) && supplied.length > 0
|
|
49
|
+
? supplied.slice(0, SUMMARY_ROWS)
|
|
50
|
+
: content.slice(0, SUMMARY_ROWS * SUMMARY_ROW_CHARS).split("\n", SUMMARY_ROWS);
|
|
51
|
+
const summaryLines = Object.freeze(candidates
|
|
52
|
+
.filter((line) => typeof line === "string")
|
|
53
|
+
.map((line) => line.slice(0, SUMMARY_ROW_CHARS)));
|
|
54
|
+
const summary = Object.freeze({ title: data.title, confirmationLabel: data.confirmationLabel, summaryLines });
|
|
55
|
+
const body = { pages: Math.ceil(content.length / REVIEW_PAGE_CHARS), bytes, sha256: exactHash(content) };
|
|
56
|
+
const reference = Object.freeze({ ...body, offer: commitment(scope, summary, body) });
|
|
57
|
+
let nextPage = 0;
|
|
58
|
+
return {
|
|
59
|
+
summary,
|
|
60
|
+
reference,
|
|
61
|
+
generation: scope.generation,
|
|
62
|
+
page(params) {
|
|
63
|
+
const index = params.index;
|
|
64
|
+
if (params.client !== scope.client ||
|
|
65
|
+
params.generation !== scope.generation ||
|
|
66
|
+
params.id !== scope.id ||
|
|
67
|
+
params.offer !== reference.offer ||
|
|
68
|
+
typeof index !== "number" ||
|
|
69
|
+
!Number.isSafeInteger(index) ||
|
|
70
|
+
index < 0 ||
|
|
71
|
+
index > nextPage ||
|
|
72
|
+
index >= reference.pages)
|
|
73
|
+
throw new Error("Stale, foreign, or invalid review page.");
|
|
74
|
+
if (index === nextPage)
|
|
75
|
+
nextPage++;
|
|
76
|
+
const start = index * REVIEW_PAGE_CHARS;
|
|
77
|
+
return { id: scope.id, index, offer: reference.offer, content: content.slice(start, start + REVIEW_PAGE_CHARS) };
|
|
78
|
+
},
|
|
79
|
+
approved(value) {
|
|
80
|
+
if (!record(value) || value.approvedReview !== reference.offer || nextPage !== reference.pages)
|
|
81
|
+
throw new Error("Exact review approval did not match the complete current offer.");
|
|
82
|
+
return true;
|
|
83
|
+
},
|
|
84
|
+
};
|
|
85
|
+
}
|
|
86
|
+
function verifyPagedReview(scope, payload, reference) {
|
|
87
|
+
if (!presentation(payload) ||
|
|
88
|
+
payload.content !== undefined ||
|
|
89
|
+
!Array.isArray(payload.summaryLines) ||
|
|
90
|
+
payload.summaryLines.length > SUMMARY_ROWS ||
|
|
91
|
+
!payload.summaryLines.every((line) => typeof line === "string" && line.length <= SUMMARY_ROW_CHARS) ||
|
|
92
|
+
!record(reference) ||
|
|
93
|
+
typeof reference.pages !== "number" ||
|
|
94
|
+
!Number.isSafeInteger(reference.pages) ||
|
|
95
|
+
reference.pages < 1 ||
|
|
96
|
+
reference.pages > Math.ceil(MAX_REVIEW_BYTES / REVIEW_PAGE_CHARS) ||
|
|
97
|
+
typeof reference.bytes !== "number" ||
|
|
98
|
+
!Number.isSafeInteger(reference.bytes) ||
|
|
99
|
+
reference.bytes <= REVIEW_PAGE_CHARS ||
|
|
100
|
+
reference.bytes > MAX_REVIEW_BYTES ||
|
|
101
|
+
typeof reference.sha256 !== "string" ||
|
|
102
|
+
!HASH.test(reference.sha256) ||
|
|
103
|
+
typeof reference.offer !== "string" ||
|
|
104
|
+
!HASH.test(reference.offer))
|
|
105
|
+
throw new Error("Invalid exact-review offer.");
|
|
106
|
+
const summary = Object.freeze({
|
|
107
|
+
title: payload.title,
|
|
108
|
+
confirmationLabel: payload.confirmationLabel,
|
|
109
|
+
summaryLines: Object.freeze([...payload.summaryLines]),
|
|
110
|
+
});
|
|
111
|
+
const verified = Object.freeze({
|
|
112
|
+
pages: reference.pages,
|
|
113
|
+
bytes: reference.bytes,
|
|
114
|
+
sha256: reference.sha256,
|
|
115
|
+
offer: reference.offer,
|
|
116
|
+
});
|
|
117
|
+
if (commitment(scope, summary, verified) !== verified.offer)
|
|
118
|
+
throw new Error("Exact-review offer changed; approval was refused.");
|
|
119
|
+
return { summary, reference: verified };
|
|
120
|
+
}
|
|
121
|
+
async function loadPagedReview(scope, reference, signal, pageRequest) {
|
|
122
|
+
const pages = [];
|
|
123
|
+
const { client, generation, id } = scope;
|
|
124
|
+
for (let index = 0; index < reference.pages; index++) {
|
|
125
|
+
signal.throwIfAborted();
|
|
126
|
+
const page = await pageRequest({ client, generation, id, offer: reference.offer, index });
|
|
127
|
+
signal.throwIfAborted();
|
|
128
|
+
if (!record(page) ||
|
|
129
|
+
page.id !== id ||
|
|
130
|
+
page.index !== index ||
|
|
131
|
+
page.offer !== reference.offer ||
|
|
132
|
+
typeof page.content !== "string" ||
|
|
133
|
+
page.content.length < 1 ||
|
|
134
|
+
page.content.length > REVIEW_PAGE_CHARS ||
|
|
135
|
+
(index < reference.pages - 1 && page.content.length !== REVIEW_PAGE_CHARS))
|
|
136
|
+
throw new Error("Invalid exact-review page.");
|
|
137
|
+
pages.push(page.content);
|
|
138
|
+
}
|
|
139
|
+
const content = pages.join("");
|
|
140
|
+
if (Buffer.byteLength(content) !== reference.bytes || exactHash(content) !== reference.sha256)
|
|
141
|
+
throw new Error("Exact-review contents changed; approval was refused.");
|
|
142
|
+
return content;
|
|
143
|
+
}
|
|
144
|
+
export async function presentPagedReview(scope, callback, signal, present, requestPage) {
|
|
145
|
+
if (callback.kind !== "merge-review")
|
|
146
|
+
throw new Error("Invalid exact-review callback.");
|
|
147
|
+
const { summary, reference } = verifyPagedReview(scope, callback.payload, callback.review);
|
|
148
|
+
const lifetime = new AbortController();
|
|
149
|
+
const owned = AbortSignal.any([signal, lifetime.signal]);
|
|
150
|
+
let loading;
|
|
151
|
+
let verified = false;
|
|
152
|
+
try {
|
|
153
|
+
owned.throwIfAborted();
|
|
154
|
+
const approved = await present({ ...callback, payload: summary, review: reference }, (requestSignal) => {
|
|
155
|
+
const pageSignal = requestSignal ? AbortSignal.any([owned, requestSignal]) : owned;
|
|
156
|
+
if (!loading) {
|
|
157
|
+
loading = loadPagedReview(scope, reference, pageSignal, (params) => requestPage(params, pageSignal)).then((content) => {
|
|
158
|
+
pageSignal.throwIfAborted();
|
|
159
|
+
verified = true;
|
|
160
|
+
return content;
|
|
161
|
+
});
|
|
162
|
+
void loading.catch(() => { });
|
|
163
|
+
}
|
|
164
|
+
return loading;
|
|
165
|
+
}, owned);
|
|
166
|
+
owned.throwIfAborted();
|
|
167
|
+
if (approved === false)
|
|
168
|
+
return false;
|
|
169
|
+
if (approved !== true || !verified)
|
|
170
|
+
throw new Error("Exact review details were not verified before approval.");
|
|
171
|
+
return { approvedReview: reference.offer };
|
|
172
|
+
}
|
|
173
|
+
finally {
|
|
174
|
+
lifetime.abort();
|
|
175
|
+
await loading?.catch(() => { });
|
|
176
|
+
}
|
|
177
|
+
}
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
import { type RuntimePaths } from "./identity.js";
|
|
2
|
+
import { type ClientFacts, type ClientHandoff } from "./protocol.js";
|
|
3
|
+
interface LedgerPeer {
|
|
4
|
+
facts: ClientFacts;
|
|
5
|
+
handoff?: ClientHandoff;
|
|
6
|
+
}
|
|
7
|
+
export declare function createParticipantLedger<T extends LedgerPeer>(paths: RuntimePaths, participants: () => Iterable<T>, onFailure: () => void): {
|
|
8
|
+
readonly failed: boolean;
|
|
9
|
+
drain: () => Promise<void>;
|
|
10
|
+
save(candidate: {
|
|
11
|
+
peer: T;
|
|
12
|
+
facts: ClientFacts;
|
|
13
|
+
} | undefined, allowNoop: boolean): Promise<void>;
|
|
14
|
+
restore(): Promise<LedgerPeer[]>;
|
|
15
|
+
};
|
|
16
|
+
export {};
|
|
@@ -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[];
|