@narumitw/pi-sync-daemon 0.2.0 → 0.4.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 +4 -2
- package/dist/cli.js +7 -8
- package/dist/client.d.ts +24 -2
- package/dist/client.js +235 -31
- package/dist/engine-service.d.ts +3 -0
- package/dist/engine-service.js +8 -1
- package/dist/idle-exit.d.ts +17 -0
- package/dist/idle-exit.js +77 -0
- package/dist/index.d.ts +2 -2
- package/dist/index.js +1 -1
- package/dist/monitor.d.ts +3 -0
- package/dist/monitor.js +35 -8
- package/dist/protocol.d.ts +19 -0
- package/dist/protocol.js +14 -0
- package/dist/server.d.ts +6 -0
- package/dist/server.js +235 -16
- package/docs/architecture.md +51 -2
- package/package.json +1 -1
|
@@ -0,0 +1,77 @@
|
|
|
1
|
+
export const IDLE_EXIT_GRACE_MS = 30_000;
|
|
2
|
+
/** The server heartbeat owns timing; this controller owns only one guarded deadline and evaluation. */
|
|
3
|
+
export function createIdleExit(hooks) {
|
|
4
|
+
let generation = 0;
|
|
5
|
+
let stopped = false;
|
|
6
|
+
let failed = false;
|
|
7
|
+
let emptySince;
|
|
8
|
+
let running;
|
|
9
|
+
const changed = () => {
|
|
10
|
+
generation++;
|
|
11
|
+
if (hooks.live())
|
|
12
|
+
emptySince = undefined;
|
|
13
|
+
};
|
|
14
|
+
async function evaluate() {
|
|
15
|
+
if (stopped || failed)
|
|
16
|
+
return;
|
|
17
|
+
if (hooks.live()) {
|
|
18
|
+
emptySince = undefined;
|
|
19
|
+
return;
|
|
20
|
+
}
|
|
21
|
+
const epoch = generation;
|
|
22
|
+
const current = () => !stopped && !failed && generation === epoch && !hooks.live();
|
|
23
|
+
let quiesced = false;
|
|
24
|
+
try {
|
|
25
|
+
const policy = await hooks.policy();
|
|
26
|
+
if (!current())
|
|
27
|
+
return;
|
|
28
|
+
if (policy !== "exit") {
|
|
29
|
+
emptySince = undefined;
|
|
30
|
+
return;
|
|
31
|
+
}
|
|
32
|
+
emptySince ??= Date.now();
|
|
33
|
+
if (Date.now() - emptySince < IDLE_EXIT_GRACE_MS)
|
|
34
|
+
return;
|
|
35
|
+
quiesced = true;
|
|
36
|
+
await hooks.quiesce();
|
|
37
|
+
if (!current())
|
|
38
|
+
return;
|
|
39
|
+
const latest = await hooks.policy();
|
|
40
|
+
if (!current())
|
|
41
|
+
return;
|
|
42
|
+
if (latest !== "exit") {
|
|
43
|
+
emptySince = undefined;
|
|
44
|
+
return;
|
|
45
|
+
}
|
|
46
|
+
if (await hooks.close(current))
|
|
47
|
+
stopped = true;
|
|
48
|
+
else if (hooks.live())
|
|
49
|
+
emptySince = undefined;
|
|
50
|
+
}
|
|
51
|
+
catch {
|
|
52
|
+
// A failed drain must not be cleared by an implicit restart or another heartbeat.
|
|
53
|
+
failed = true;
|
|
54
|
+
hooks.failed();
|
|
55
|
+
}
|
|
56
|
+
finally {
|
|
57
|
+
if (quiesced && !stopped && !failed)
|
|
58
|
+
hooks.resume();
|
|
59
|
+
}
|
|
60
|
+
}
|
|
61
|
+
return {
|
|
62
|
+
changed,
|
|
63
|
+
tick() {
|
|
64
|
+
if (!running) {
|
|
65
|
+
running = evaluate().finally(() => {
|
|
66
|
+
running = undefined;
|
|
67
|
+
});
|
|
68
|
+
}
|
|
69
|
+
return running;
|
|
70
|
+
},
|
|
71
|
+
async dispose() {
|
|
72
|
+
stopped = true;
|
|
73
|
+
generation++;
|
|
74
|
+
await running;
|
|
75
|
+
},
|
|
76
|
+
};
|
|
77
|
+
}
|
package/dist/index.d.ts
CHANGED
|
@@ -1,8 +1,8 @@
|
|
|
1
1
|
export type { ConnectionOptions } from "./client.js";
|
|
2
|
-
export { DaemonConnection, DaemonError, ensureDaemon, findDaemon, requestDaemon, stopDaemon } from "./client.js";
|
|
2
|
+
export { DaemonConnection, DaemonError, ensureDaemon, findDaemon, requestDaemon, stopDaemon, waitForDaemonExit, } from "./client.js";
|
|
3
3
|
export type { DocumentView } from "./document.js";
|
|
4
4
|
export { documentFingerprint } from "./document.js";
|
|
5
5
|
export type { EngineContext, EngineHost, EngineSession, ReviewRequest } from "./engine/host.js";
|
|
6
6
|
export { modelSignature } from "./model-signature.js";
|
|
7
|
-
export type { Callback, ClientEvent, ClientFacts, ClientHandoff, Identity, JobView, RemoteError } from "./protocol.js";
|
|
7
|
+
export type { Callback, ClientContact as ContactState, ClientEvent, ClientFacts, ClientHandoff, Identity, JobView, RemoteError, } from "./protocol.js";
|
|
8
8
|
export { MAX_SETTINGS_BYTES, PROTOCOL_VERSION } from "./protocol.js";
|
package/dist/index.js
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
export { DaemonConnection, DaemonError, ensureDaemon, findDaemon, requestDaemon, stopDaemon } from "./client.js";
|
|
1
|
+
export { DaemonConnection, DaemonError, ensureDaemon, findDaemon, requestDaemon, stopDaemon, waitForDaemonExit, } from "./client.js";
|
|
2
2
|
export { documentFingerprint } from "./document.js";
|
|
3
3
|
export { modelSignature } from "./model-signature.js";
|
|
4
4
|
export { MAX_SETTINGS_BYTES, PROTOCOL_VERSION } from "./protocol.js";
|
package/dist/monitor.d.ts
CHANGED
|
@@ -1,9 +1,12 @@
|
|
|
1
|
+
import type { IdleExitPolicy } from "./idle-exit.js";
|
|
1
2
|
import type { Execution } from "./protocol.js";
|
|
2
3
|
import type { startServer } from "./server.js";
|
|
3
4
|
import type { installServiceHost } from "./service-host.js";
|
|
4
5
|
export declare function createMonitor(root: string, host: ReturnType<typeof installServiceHost>): {
|
|
5
6
|
bind(value: Awaited<ReturnType<typeof startServer>>): void;
|
|
6
7
|
schedule: () => void;
|
|
8
|
+
lifetime: () => Promise<IdleExitPolicy>;
|
|
9
|
+
blockAutomatic: () => void;
|
|
7
10
|
explicitSync(setup: string, execution: Execution): void;
|
|
8
11
|
pause(): Promise<void>;
|
|
9
12
|
factsChanged(): Promise<void>;
|
package/dist/monitor.js
CHANGED
|
@@ -27,6 +27,11 @@ export function createMonitor(root, host) {
|
|
|
27
27
|
transition = transition.then(start).catch(() => { });
|
|
28
28
|
});
|
|
29
29
|
}
|
|
30
|
+
function blockAutomatic() {
|
|
31
|
+
authorizationFailed = true;
|
|
32
|
+
retries.clear();
|
|
33
|
+
task?.controller.abort(new DOMException("Daemon safety barrier", "AbortError"));
|
|
34
|
+
}
|
|
30
35
|
async function stop() {
|
|
31
36
|
const current = task;
|
|
32
37
|
if (!current)
|
|
@@ -36,6 +41,29 @@ export function createMonitor(root, host) {
|
|
|
36
41
|
await current.continuous.stop();
|
|
37
42
|
await current.promise;
|
|
38
43
|
}
|
|
44
|
+
function unattendedGrant(config) {
|
|
45
|
+
if (!config.continuous || !config.agentMode)
|
|
46
|
+
return undefined;
|
|
47
|
+
const saved = readConsent(root)?.setups[config.setupName];
|
|
48
|
+
return saved?.afterExit && saved.fingerprint === consentFingerprint(root, config) ? saved : undefined;
|
|
49
|
+
}
|
|
50
|
+
async function lifetime() {
|
|
51
|
+
if (closed || authorizationFailed)
|
|
52
|
+
return "pause";
|
|
53
|
+
try {
|
|
54
|
+
const consent = readConsent(root);
|
|
55
|
+
if (!consent || !Object.values(consent.setups).some((saved) => saved.afterExit))
|
|
56
|
+
return "exit";
|
|
57
|
+
const config = await loadConfigForCheck();
|
|
58
|
+
if (closed || authorizationFailed)
|
|
59
|
+
return "pause";
|
|
60
|
+
return unattendedGrant(config) ? "keep" : "exit";
|
|
61
|
+
}
|
|
62
|
+
catch {
|
|
63
|
+
// Invalid evidence is not an implicit revocation or permission to discard a safety barrier.
|
|
64
|
+
return "pause";
|
|
65
|
+
}
|
|
66
|
+
}
|
|
39
67
|
async function start() {
|
|
40
68
|
const service = server;
|
|
41
69
|
if (!service)
|
|
@@ -56,13 +84,12 @@ export function createMonitor(root, host) {
|
|
|
56
84
|
const { pendingTransactionEntries } = await import("./engine/snapshot/snapshot-transaction-directory.js");
|
|
57
85
|
if ((await pendingTransactionEntries()).some((entry) => entry.isDirectory()) || !available())
|
|
58
86
|
return;
|
|
59
|
-
const fingerprint = consentFingerprint(root, config);
|
|
60
87
|
const peers = service.participants();
|
|
61
88
|
if (peers.some((peer) => !peer.idle || peer.ready !== true))
|
|
62
89
|
return;
|
|
63
|
-
const authorized =
|
|
90
|
+
const authorized = unattendedGrant(config);
|
|
64
91
|
const selected = peers.toSorted((a, b) => a.id.localeCompare(b.id))[0];
|
|
65
|
-
if (!selected &&
|
|
92
|
+
if (!selected && !authorized)
|
|
66
93
|
return;
|
|
67
94
|
if (!continuous) {
|
|
68
95
|
const { createContinuousSync } = await import("./engine/sync/continuous-sync.js");
|
|
@@ -117,9 +144,8 @@ export function createMonitor(root, host) {
|
|
|
117
144
|
JSON.stringify(peer.model) === JSON.stringify(binding) &&
|
|
118
145
|
peer.sessionDir === sessionDir &&
|
|
119
146
|
peer.cwd === cwd);
|
|
120
|
-
const saved =
|
|
121
|
-
return Boolean(saved
|
|
122
|
-
saved.fingerprint === consentFingerprint(root, currentConfig) &&
|
|
147
|
+
const saved = unattendedGrant(currentConfig);
|
|
148
|
+
return Boolean(saved &&
|
|
123
149
|
JSON.stringify(saved.model) === JSON.stringify(binding) &&
|
|
124
150
|
saved.sessionDir === sessionDir &&
|
|
125
151
|
(saved.cwd ?? root) === cwd);
|
|
@@ -201,6 +227,8 @@ export function createMonitor(root, host) {
|
|
|
201
227
|
schedule();
|
|
202
228
|
},
|
|
203
229
|
schedule,
|
|
230
|
+
lifetime,
|
|
231
|
+
blockAutomatic,
|
|
204
232
|
explicitSync(setup, execution) {
|
|
205
233
|
const peer = execution.facts();
|
|
206
234
|
if (!closed && peer)
|
|
@@ -222,8 +250,7 @@ export function createMonitor(root, host) {
|
|
|
222
250
|
}
|
|
223
251
|
}
|
|
224
252
|
catch (error) {
|
|
225
|
-
|
|
226
|
-
retries.clear();
|
|
253
|
+
blockAutomatic();
|
|
227
254
|
// Recovery barriers must not prevent passive clients from registering protection.
|
|
228
255
|
if (error instanceof ConsentPublicationError)
|
|
229
256
|
failure = error;
|
package/dist/protocol.d.ts
CHANGED
|
@@ -1,5 +1,15 @@
|
|
|
1
1
|
import type { Api, Model } from "@earendil-works/pi-ai";
|
|
2
2
|
export declare const PROTOCOL_VERSION = 1;
|
|
3
|
+
/** Authenticated idle shutdown after work is drained; callers may wait for this owner to exit. */
|
|
4
|
+
export declare class DaemonStoppingError extends Error {
|
|
5
|
+
name: string;
|
|
6
|
+
constructor();
|
|
7
|
+
}
|
|
8
|
+
/** This owner expired this client's heartbeat; a normal facts update cannot restore participation. */
|
|
9
|
+
export declare class ClientContactExpiredError extends Error {
|
|
10
|
+
name: string;
|
|
11
|
+
constructor();
|
|
12
|
+
}
|
|
3
13
|
export declare const MAX_MESSAGE_BYTES: number;
|
|
4
14
|
export declare const REVIEW_PAGE_CHARS: number;
|
|
5
15
|
export declare const MAX_REVIEW_BYTES: number;
|
|
@@ -74,6 +84,15 @@ export interface JobView {
|
|
|
74
84
|
result?: unknown;
|
|
75
85
|
error?: RemoteError;
|
|
76
86
|
}
|
|
87
|
+
export type ClientContact = "active" | "expired" | "inactive";
|
|
88
|
+
export interface PollReply {
|
|
89
|
+
cursor: number;
|
|
90
|
+
callbacks: Callback[];
|
|
91
|
+
events: ClientEvent[];
|
|
92
|
+
job?: JobView;
|
|
93
|
+
/** Absent on owners without protocol-v1 contact resume support. */
|
|
94
|
+
contact?: ClientContact;
|
|
95
|
+
}
|
|
77
96
|
export interface Execution {
|
|
78
97
|
readonly root: string;
|
|
79
98
|
readonly clientId: string | undefined;
|
package/dist/protocol.js
CHANGED
|
@@ -1,6 +1,20 @@
|
|
|
1
1
|
import { createHash } from "node:crypto";
|
|
2
2
|
import path from "node:path";
|
|
3
3
|
export const PROTOCOL_VERSION = 1;
|
|
4
|
+
/** Authenticated idle shutdown after work is drained; callers may wait for this owner to exit. */
|
|
5
|
+
export class DaemonStoppingError extends Error {
|
|
6
|
+
name = "DaemonStoppingError";
|
|
7
|
+
constructor() {
|
|
8
|
+
super("Daemon is closing after its idle grace period; wait for owner exit before reconnecting.");
|
|
9
|
+
}
|
|
10
|
+
}
|
|
11
|
+
/** This owner expired this client's heartbeat; a normal facts update cannot restore participation. */
|
|
12
|
+
export class ClientContactExpiredError extends Error {
|
|
13
|
+
name = "ClientContactExpiredError";
|
|
14
|
+
constructor() {
|
|
15
|
+
super("Client contact expired; resume the same session before publishing facts.");
|
|
16
|
+
}
|
|
17
|
+
}
|
|
4
18
|
export const MAX_MESSAGE_BYTES = 4 * 1024 * 1024;
|
|
5
19
|
export const REVIEW_PAGE_CHARS = 64 * 1024;
|
|
6
20
|
export const MAX_REVIEW_BYTES = 16 * 1024 * 1024;
|
package/dist/server.d.ts
CHANGED
|
@@ -1,8 +1,13 @@
|
|
|
1
|
+
import { type IdleExitPolicy } from "./idle-exit.js";
|
|
1
2
|
import { type ClientFacts, type Execute, type Execution, type Identity } from "./protocol.js";
|
|
2
3
|
export interface ServerHooks {
|
|
3
4
|
factsChanged?(): Promise<void>;
|
|
4
5
|
idle?(): void;
|
|
5
6
|
close?(): Promise<void>;
|
|
7
|
+
lifetime?(): Promise<IdleExitPolicy>;
|
|
8
|
+
quiesce?(): Promise<void>;
|
|
9
|
+
stopped?(): void;
|
|
10
|
+
failure?(): void;
|
|
6
11
|
}
|
|
7
12
|
export declare function startServer(root: string, execute: Execute, hooks?: ServerHooks): Promise<{
|
|
8
13
|
identity: Identity;
|
|
@@ -13,6 +18,7 @@ export declare function startServer(root: string, execute: Execute, hooks?: Serv
|
|
|
13
18
|
participants: () => ClientFacts[];
|
|
14
19
|
liveClients: () => ClientFacts[];
|
|
15
20
|
expireClients: () => Promise<void>;
|
|
21
|
+
checkLifetime: () => Promise<void>;
|
|
16
22
|
isIdle: () => boolean;
|
|
17
23
|
readonly closed: boolean;
|
|
18
24
|
}>;
|