@narumitw/pi-sync-daemon 0.8.0 → 0.9.1
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 +18 -0
- package/dist/cli.js +1 -1
- package/dist/consent.d.ts +14 -2
- package/dist/consent.js +134 -16
- package/dist/engine/backends/git/git-backend.d.ts +1 -2
- package/dist/engine/backends/git/git-backend.js +61 -30
- package/dist/engine/commands/command-types.d.ts +2 -1
- package/dist/engine/host.d.ts +1 -0
- package/dist/engine/ignore/ignore-initialization.d.ts +8 -1
- package/dist/engine/ignore/ignore-initialization.js +142 -40
- package/dist/engine/ignore/ignore-settings.d.ts +16 -1
- package/dist/engine/ignore/ignore-settings.js +94 -26
- package/dist/engine/ignore/ignore-source.d.ts +1 -1
- package/dist/engine/ignore/ignore-source.js +1 -1
- package/dist/engine/settings/config-file.js +1 -1
- package/dist/engine/settings/config.d.ts +3 -2
- package/dist/engine/settings/config.js +71 -28
- package/dist/engine/settings/settings-management.d.ts +3 -1
- package/dist/engine/settings/settings-management.js +8 -2
- package/dist/engine/settings/settings-types.d.ts +2 -0
- package/dist/engine/snapshot/session-paths.js +7 -2
- package/dist/engine/snapshot/snapshot-apply.d.ts +1 -1
- package/dist/engine/snapshot/snapshot-transaction-policy.d.ts +7 -2
- package/dist/engine/snapshot/snapshot-transaction-policy.js +37 -17
- package/dist/engine/snapshot/snapshot-transaction.js +55 -13
- package/dist/engine/snapshot/snapshot-types.d.ts +4 -0
- package/dist/engine/sync/agent-plan.js +1 -0
- package/dist/engine/sync/agent-sync.js +8 -1
- package/dist/engine/sync/continuous-sync.d.ts +3 -1
- package/dist/engine/sync/continuous-sync.js +32 -15
- package/dist/engine/sync/merge-acceptance.d.ts +8 -0
- package/dist/engine/sync/merge-acceptance.js +29 -0
- package/dist/engine/sync/merge-apply.js +7 -4
- package/dist/engine/sync/merge-journal.d.ts +6 -0
- package/dist/engine/sync/merge-journal.js +33 -14
- package/dist/engine/sync/merged-sync.js +31 -39
- package/dist/engine/sync/remote-snapshot.d.ts +2 -1
- package/dist/engine/sync/remote-snapshot.js +17 -2
- package/dist/engine/sync/session-push.js +20 -6
- package/dist/engine/sync/sync-error-guidance.js +16 -3
- package/dist/engine/sync/sync-mutations.js +75 -54
- package/dist/engine/sync/sync-policy.d.ts +5 -0
- package/dist/engine/sync/sync-policy.js +28 -3
- package/dist/engine/sync/sync-state.d.ts +2 -0
- package/dist/engine/sync/sync-state.js +3 -8
- package/dist/engine/sync/transfer-freshness.d.ts +31 -0
- package/dist/engine/sync/transfer-freshness.js +82 -0
- 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 +1 -1
- package/dist/engine-service.js +124 -27
- package/dist/monitor.d.ts +4 -1
- package/dist/monitor.js +84 -32
- package/dist/server.d.ts +1 -0
- package/dist/server.js +46 -5
- package/dist/service-host.d.ts +1 -1
- package/dist/service-host.js +2 -1
- package/docs/architecture.md +14 -6
- package/package.json +1 -1
package/dist/monitor.js
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
import { randomUUID } from "node:crypto";
|
|
2
2
|
import path from "node:path";
|
|
3
|
-
import { ConsentPublicationError, consentFingerprint, readConsent, revokeChangedBindings } from "./consent.js";
|
|
4
|
-
import { loadConfigForCheck } from "./engine/settings/config.js";
|
|
3
|
+
import { automaticPolicyAuthorized, ConsentPublicationError, consentFingerprint, participantBinding, readConsent, revokeChangedBindings, } from "./consent.js";
|
|
4
|
+
import { loadConfigForCheck, syncCheckConfigFingerprint } from "./engine/settings/config.js";
|
|
5
5
|
import { createEngineLoaders } from "./engine/sync/sync-loaders.js";
|
|
6
6
|
import { createServiceContext } from "./service-host.js";
|
|
7
7
|
export function createMonitor(root, host) {
|
|
@@ -11,12 +11,19 @@ export function createMonitor(root, host) {
|
|
|
11
11
|
let closed = false;
|
|
12
12
|
let authorizationFailed = false;
|
|
13
13
|
let scheduled = false;
|
|
14
|
-
|
|
14
|
+
let settingsGeneration = 0;
|
|
15
|
+
let blockedParticipants;
|
|
16
|
+
const participatingSessions = () => JSON.stringify(server
|
|
17
|
+
?.participants()
|
|
18
|
+
.map(({ id, generation }) => `${id}:${generation}`)
|
|
19
|
+
.sort() ?? []);
|
|
20
|
+
const policyNeedsReview = () => blockedParticipants === participatingSessions();
|
|
21
|
+
let monitoredPolicy;
|
|
15
22
|
const retries = new Map();
|
|
23
|
+
const retryOwner = (peer) => JSON.stringify([peer.generation, peer.cwd, peer.sessionId, peer.sessionFile, peer.sessionDir, peer.model]);
|
|
16
24
|
let epoch;
|
|
17
25
|
const timingOwners = new WeakMap();
|
|
18
26
|
const loaders = createEngineLoaders();
|
|
19
|
-
const operations = loaders.operations;
|
|
20
27
|
let continuous;
|
|
21
28
|
function schedule() {
|
|
22
29
|
if (closed || scheduled || !server)
|
|
@@ -42,7 +49,7 @@ export function createMonitor(root, host) {
|
|
|
42
49
|
await current.promise;
|
|
43
50
|
}
|
|
44
51
|
function unattendedGrant(config) {
|
|
45
|
-
if (!config.continuous || !config.agentMode)
|
|
52
|
+
if (!config.continuous || !config.agentMode || !automaticPolicyAuthorized(root, config))
|
|
46
53
|
return undefined;
|
|
47
54
|
const saved = readConsent(root)?.setups[config.setupName];
|
|
48
55
|
return saved?.afterExit && saved.fingerprint === consentFingerprint(root, config) ? saved : undefined;
|
|
@@ -57,7 +64,7 @@ export function createMonitor(root, host) {
|
|
|
57
64
|
const config = await loadConfigForCheck();
|
|
58
65
|
if (closed || authorizationFailed)
|
|
59
66
|
return "pause";
|
|
60
|
-
return unattendedGrant(config) ? "keep" : "exit";
|
|
67
|
+
return !policyNeedsReview() && unattendedGrant(config) ? "keep" : "exit";
|
|
61
68
|
}
|
|
62
69
|
catch {
|
|
63
70
|
// Invalid evidence is not an implicit revocation or permission to discard a safety barrier.
|
|
@@ -68,14 +75,25 @@ export function createMonitor(root, host) {
|
|
|
68
75
|
const service = server;
|
|
69
76
|
if (!service)
|
|
70
77
|
return;
|
|
71
|
-
|
|
72
|
-
if (!available())
|
|
78
|
+
if (closed || authorizationFailed || policyNeedsReview() || task || !service.isIdle())
|
|
73
79
|
return;
|
|
74
|
-
const
|
|
75
|
-
|
|
80
|
+
const generation = settingsGeneration;
|
|
81
|
+
const participants = service.participantGeneration?.();
|
|
82
|
+
const participantsToken = participantBinding(service);
|
|
83
|
+
const consentToken = JSON.stringify(readConsent(root));
|
|
84
|
+
const sameAuthorization = () => !closed &&
|
|
85
|
+
!authorizationFailed &&
|
|
86
|
+
!policyNeedsReview() &&
|
|
87
|
+
server === service &&
|
|
88
|
+
generation === settingsGeneration &&
|
|
89
|
+
participants === service.participantGeneration?.() &&
|
|
90
|
+
participantsToken === participantBinding(service) &&
|
|
91
|
+
consentToken === JSON.stringify(readConsent(root));
|
|
92
|
+
const available = () => !task && service.isIdle() && sameAuthorization();
|
|
93
|
+
if (!consentToken || !available())
|
|
76
94
|
return;
|
|
77
95
|
const config = await loadConfigForCheck();
|
|
78
|
-
if (!available())
|
|
96
|
+
if (!available() || !automaticPolicyAuthorized(root, config))
|
|
79
97
|
return;
|
|
80
98
|
if (!config.continuous || !config.agentMode) {
|
|
81
99
|
retries.clear();
|
|
@@ -87,6 +105,9 @@ export function createMonitor(root, host) {
|
|
|
87
105
|
const peers = service.participants();
|
|
88
106
|
if (peers.some((peer) => !peer.idle || peer.ready !== true))
|
|
89
107
|
return;
|
|
108
|
+
const fingerprint = syncCheckConfigFingerprint(config);
|
|
109
|
+
if (config.reservedIgnoreNames !== undefined && monitoredPolicy !== undefined && monitoredPolicy !== fingerprint)
|
|
110
|
+
return;
|
|
90
111
|
const authorized = unattendedGrant(config);
|
|
91
112
|
const selected = peers.toSorted((a, b) => a.id.localeCompare(b.id))[0];
|
|
92
113
|
if (!selected && !authorized)
|
|
@@ -135,7 +156,9 @@ export function createMonitor(root, host) {
|
|
|
135
156
|
};
|
|
136
157
|
const execution = { ...original, facts: () => stableFacts };
|
|
137
158
|
const allowed = (currentConfig = config) => {
|
|
138
|
-
if (
|
|
159
|
+
if (controller.signal.aborted ||
|
|
160
|
+
!sameAuthorization() ||
|
|
161
|
+
syncCheckConfigFingerprint(currentConfig) !== fingerprint)
|
|
139
162
|
return false;
|
|
140
163
|
const current = service.participants();
|
|
141
164
|
if (current.length)
|
|
@@ -159,12 +182,11 @@ export function createMonitor(root, host) {
|
|
|
159
182
|
timingOwners.set(ctx, execution);
|
|
160
183
|
ctx.backgroundAuthorized = allowed;
|
|
161
184
|
ctx.isIdle = () => Boolean(service.isIdle() && allowed());
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
};
|
|
185
|
+
const refresh = async () => {
|
|
186
|
+
execution.validate();
|
|
187
|
+
if (!allowed(await loadConfigForCheck(undefined, controller.signal)))
|
|
188
|
+
throw new Error("Daemon automatic authorization changed.");
|
|
189
|
+
execution.validate();
|
|
168
190
|
};
|
|
169
191
|
const sessionSignal = epoch.controller.signal;
|
|
170
192
|
const debug = epoch.debug;
|
|
@@ -177,30 +199,47 @@ export function createMonitor(root, host) {
|
|
|
177
199
|
void host
|
|
178
200
|
.run(execution, async () => {
|
|
179
201
|
const { prepareResolverHost } = await import("./resolver-host.js");
|
|
202
|
+
await refresh();
|
|
180
203
|
const resolver = await prepareResolverHost(root, binding, controller.signal);
|
|
181
204
|
try {
|
|
182
|
-
|
|
183
|
-
|
|
205
|
+
await refresh();
|
|
206
|
+
if (config.reservedIgnoreNames !== undefined)
|
|
207
|
+
monitoredPolicy = fingerprint;
|
|
184
208
|
ctx.model = resolver.model;
|
|
185
209
|
ctx.modelUnavailableReason = resolver.unavailable;
|
|
186
|
-
ctx.modelRegistry =
|
|
210
|
+
ctx.modelRegistry = {
|
|
211
|
+
streamSimple: (model, context, options) => {
|
|
212
|
+
execution.validate();
|
|
213
|
+
if (!allowed())
|
|
214
|
+
throw new Error("Daemon authorization changed before model dispatch.");
|
|
215
|
+
return resolver.registry.streamSimple(model, context, options);
|
|
216
|
+
},
|
|
217
|
+
};
|
|
187
218
|
ctx.validateModel = async () => {
|
|
188
|
-
|
|
189
|
-
|
|
190
|
-
for (const peer of service.participants()) {
|
|
219
|
+
await refresh();
|
|
220
|
+
for (const peer of peers) {
|
|
191
221
|
const fresh = await execution.request("model-binding", { background: true }, peer.id);
|
|
192
222
|
if (JSON.stringify(fresh ?? undefined) !== JSON.stringify(binding))
|
|
193
223
|
throw new Error("Participating model changed before dispatch.");
|
|
194
|
-
|
|
195
|
-
throw new Error("Daemon authorization changed during model validation.");
|
|
224
|
+
await refresh();
|
|
196
225
|
}
|
|
197
226
|
};
|
|
198
227
|
const explicitRetry = service.participants().some((peer) => {
|
|
199
228
|
const retry = retries.get(peer.id);
|
|
200
|
-
return retry?.
|
|
229
|
+
return retry?.fingerprint === fingerprint && retry.owner === retryOwner(peer) && !retry.signal.aborted;
|
|
201
230
|
});
|
|
202
231
|
retries.clear();
|
|
203
|
-
activeContinuous.start(ctx, sessionSignal, explicitRetry ? config.setupName : false, debug)
|
|
232
|
+
activeContinuous.start(ctx, sessionSignal, explicitRetry ? config.setupName : false, debug, async (run) => {
|
|
233
|
+
await refresh();
|
|
234
|
+
const attempt = new AbortController();
|
|
235
|
+
const owner = service.backgroundExecution(AbortSignal.any([attempt.signal, controller.signal]));
|
|
236
|
+
try {
|
|
237
|
+
return await host.run({ ...owner, facts: execution.facts }, () => run(owner.committed));
|
|
238
|
+
}
|
|
239
|
+
finally {
|
|
240
|
+
attempt.abort();
|
|
241
|
+
}
|
|
242
|
+
});
|
|
204
243
|
await new Promise((resolve) => {
|
|
205
244
|
if (controller.signal.aborted)
|
|
206
245
|
resolve();
|
|
@@ -229,10 +268,23 @@ export function createMonitor(root, host) {
|
|
|
229
268
|
schedule,
|
|
230
269
|
lifetime,
|
|
231
270
|
blockAutomatic,
|
|
232
|
-
explicitSync(
|
|
271
|
+
explicitSync(config, execution) {
|
|
233
272
|
const peer = execution.facts();
|
|
234
|
-
if (
|
|
235
|
-
|
|
273
|
+
if (closed || !peer)
|
|
274
|
+
return;
|
|
275
|
+
const fingerprint = syncCheckConfigFingerprint(config);
|
|
276
|
+
retries.set(peer.id, { fingerprint, owner: retryOwner(peer), signal: execution.signal });
|
|
277
|
+
},
|
|
278
|
+
reviewedPolicy(config) {
|
|
279
|
+
blockedParticipants = undefined;
|
|
280
|
+
monitoredPolicy = config.reservedIgnoreNames === undefined ? undefined : syncCheckConfigFingerprint(config);
|
|
281
|
+
},
|
|
282
|
+
settingsCommitted(blockNewPolicy) {
|
|
283
|
+
settingsGeneration++;
|
|
284
|
+
retries.clear();
|
|
285
|
+
if (blockNewPolicy)
|
|
286
|
+
blockedParticipants = participatingSessions();
|
|
287
|
+
task?.controller.abort(new DOMException("Sync settings committed", "AbortError"));
|
|
236
288
|
},
|
|
237
289
|
async pause() {
|
|
238
290
|
transition = transition.then(stop);
|
|
@@ -244,7 +296,7 @@ export function createMonitor(root, host) {
|
|
|
244
296
|
if (server) {
|
|
245
297
|
const peers = server.participants();
|
|
246
298
|
for (const [id, retry] of retries)
|
|
247
|
-
if (retry.signal.aborted || !peers.some((peer) => peer.id === id && peer
|
|
299
|
+
if (retry.signal.aborted || !peers.some((peer) => peer.id === id && retryOwner(peer) === retry.owner))
|
|
248
300
|
retries.delete(id);
|
|
249
301
|
revokeChangedBindings(root, peers);
|
|
250
302
|
}
|
package/dist/server.d.ts
CHANGED
|
@@ -16,6 +16,7 @@ export declare function startServer(root: string, execute: Execute, hooks?: Serv
|
|
|
16
16
|
backgroundExecution: (signal: AbortSignal) => Execution;
|
|
17
17
|
readonly retainedEndpoint: string | undefined;
|
|
18
18
|
participants: () => ClientFacts[];
|
|
19
|
+
participantGeneration: () => number;
|
|
19
20
|
liveClients: () => ClientFacts[];
|
|
20
21
|
expireClients: () => Promise<void>;
|
|
21
22
|
checkLifetime: () => Promise<void>;
|
package/dist/server.js
CHANGED
|
@@ -51,6 +51,7 @@ export async function startServer(root, execute, hooks = {}) {
|
|
|
51
51
|
const pending = new Map();
|
|
52
52
|
const peersFile = path.join(paths.directory, "clients.json");
|
|
53
53
|
let sequence = 0;
|
|
54
|
+
let participantGeneration = 0;
|
|
54
55
|
let closed = false;
|
|
55
56
|
let stopRequested = false;
|
|
56
57
|
let automaticClosing = false;
|
|
@@ -62,6 +63,15 @@ export async function startServer(root, execute, hooks = {}) {
|
|
|
62
63
|
let closing;
|
|
63
64
|
let peerWrites = Promise.resolve();
|
|
64
65
|
let heartbeat;
|
|
66
|
+
function participantsChanged() {
|
|
67
|
+
participantGeneration++;
|
|
68
|
+
for (const job of [...jobs.values(), ...backgroundJobs]) {
|
|
69
|
+
if (job.participantGeneration !== undefined &&
|
|
70
|
+
!job.committed &&
|
|
71
|
+
(job.view.phase === "queued" || job.view.phase === "running"))
|
|
72
|
+
job.controller.abort(new DOMException("Daemon participants changed before automatic work completed.", "AbortError"));
|
|
73
|
+
}
|
|
74
|
+
}
|
|
65
75
|
function savePeers(candidate) {
|
|
66
76
|
idleExit?.changed();
|
|
67
77
|
const operation = peerWrites.then(() => writePeers(candidate));
|
|
@@ -168,6 +178,8 @@ export async function startServer(root, execute, hooks = {}) {
|
|
|
168
178
|
const assertOwner = () => {
|
|
169
179
|
if (automaticClosing && !compromised)
|
|
170
180
|
throw new DaemonStoppingError();
|
|
181
|
+
if (stopRequested)
|
|
182
|
+
throw new DaemonStoppingError();
|
|
171
183
|
if (closed || compromised)
|
|
172
184
|
throw new Error("Daemon ownership is no longer valid.");
|
|
173
185
|
assertRoot();
|
|
@@ -187,9 +199,12 @@ export async function startServer(root, execute, hooks = {}) {
|
|
|
187
199
|
return peer;
|
|
188
200
|
};
|
|
189
201
|
const liveClientFacts = () => [...participants.values()].filter((peer) => processExists(peer.facts.pid)).map((peer) => peer.facts);
|
|
202
|
+
const participates = (peer) => !peer.publishing && (peer.connected || peer.suspected);
|
|
190
203
|
const participantFacts = () => [...participants.values()]
|
|
191
|
-
.filter((peer) =>
|
|
192
|
-
.map((peer) => peer.closing || peer.suspected
|
|
204
|
+
.filter((peer) => participates(peer) && processExists(peer.facts.pid))
|
|
205
|
+
.map((peer) => peer.closing || peer.suspected || Date.now() - peer.seen > 5_000
|
|
206
|
+
? { ...(peer.resumeFacts ?? peer.facts), idle: false }
|
|
207
|
+
: peer.facts);
|
|
193
208
|
const contactState = (peer) => {
|
|
194
209
|
if (closed || stopRequested || compromised || peer.publishing || peer.closing || !processExists(peer.facts.pid))
|
|
195
210
|
return "inactive";
|
|
@@ -215,6 +230,8 @@ export async function startServer(root, execute, hooks = {}) {
|
|
|
215
230
|
}
|
|
216
231
|
assertOwner();
|
|
217
232
|
job.controller.signal.throwIfAborted();
|
|
233
|
+
if (job.participantGeneration !== undefined && job.participantGeneration !== participantGeneration)
|
|
234
|
+
throw new Error("Daemon participants changed before automatic work completed.");
|
|
218
235
|
if (job.client) {
|
|
219
236
|
const peer = requireParticipant(job.client);
|
|
220
237
|
if (peer.facts.generation !== job.generation)
|
|
@@ -348,6 +365,14 @@ export async function startServer(root, execute, hooks = {}) {
|
|
|
348
365
|
digest,
|
|
349
366
|
client,
|
|
350
367
|
generation: peer.facts.generation,
|
|
368
|
+
participantGeneration: [
|
|
369
|
+
"startup-transfer",
|
|
370
|
+
"shutdown",
|
|
371
|
+
"daemon.authorize",
|
|
372
|
+
"daemon.authorize-connected",
|
|
373
|
+
].includes(operation)
|
|
374
|
+
? participantGeneration
|
|
375
|
+
: undefined,
|
|
351
376
|
controller: new AbortController(),
|
|
352
377
|
committed: false,
|
|
353
378
|
promise: Promise.resolve(),
|
|
@@ -425,6 +450,7 @@ export async function startServer(root, execute, hooks = {}) {
|
|
|
425
450
|
recheck();
|
|
426
451
|
// Expose the new binding to the monitor, but only as a non-ready, busy suspect.
|
|
427
452
|
peer.resumeFacts = { ...candidate, ready: false };
|
|
453
|
+
participantsChanged();
|
|
428
454
|
// Never wait for jobs or monitor transitions inside admission: they can need the fence.
|
|
429
455
|
await Promise.race([
|
|
430
456
|
Promise.all([hooks.factsChanged?.(), ...[...jobs.values()].filter(running).map((job) => job.promise)]),
|
|
@@ -452,6 +478,7 @@ export async function startServer(root, execute, hooks = {}) {
|
|
|
452
478
|
peer.resumeFacts = undefined;
|
|
453
479
|
peer.suspected = false;
|
|
454
480
|
peer.connected = true;
|
|
481
|
+
participantsChanged();
|
|
455
482
|
hooks.idle?.();
|
|
456
483
|
return poll(peer, 0);
|
|
457
484
|
}, signal);
|
|
@@ -472,6 +499,7 @@ export async function startServer(root, execute, hooks = {}) {
|
|
|
472
499
|
if (closedClients.has(id))
|
|
473
500
|
return poll(peer, 0);
|
|
474
501
|
peer.closing = true;
|
|
502
|
+
participantsChanged();
|
|
475
503
|
invalidateExpiry(peer);
|
|
476
504
|
for (const waiting of [...pending.values()])
|
|
477
505
|
if (waiting.target === id && !waiting.callback.kind.startsWith("queue-"))
|
|
@@ -483,6 +511,7 @@ export async function startServer(root, execute, hooks = {}) {
|
|
|
483
511
|
job.controller.abort(new DOMException("Client detached", "AbortError"));
|
|
484
512
|
await Promise.allSettled(affected.map((job) => job.promise));
|
|
485
513
|
peer.connected = false;
|
|
514
|
+
participantsChanged();
|
|
486
515
|
// Orderly exit remains protected by PID, but is not an ambiguous active participant.
|
|
487
516
|
peer.suspected = retain && handoff?.kind !== "exit";
|
|
488
517
|
peer.closing = false;
|
|
@@ -532,6 +561,7 @@ export async function startServer(root, execute, hooks = {}) {
|
|
|
532
561
|
throw new Error("Client identity collision.");
|
|
533
562
|
if (old)
|
|
534
563
|
invalidateExpiry(old);
|
|
564
|
+
participantsChanged();
|
|
535
565
|
participants.set(params.id, {
|
|
536
566
|
facts: structuredClone(params),
|
|
537
567
|
reservations: old?.reservations,
|
|
@@ -557,6 +587,7 @@ export async function startServer(root, execute, hooks = {}) {
|
|
|
557
587
|
}
|
|
558
588
|
const attached = requireParticipant(params.id, true);
|
|
559
589
|
attached.publishing = false;
|
|
590
|
+
participantsChanged();
|
|
560
591
|
// A shutdown-confirmed handoff keeps protection across SDK invalidation.
|
|
561
592
|
// Publish the replacement first, then retire only the matching closed scope.
|
|
562
593
|
for (const [id, peer] of participants) {
|
|
@@ -607,6 +638,8 @@ export async function startServer(root, execute, hooks = {}) {
|
|
|
607
638
|
requireParticipant(params.facts.id);
|
|
608
639
|
peer.facts = structuredClone(params.facts);
|
|
609
640
|
const published = peer.facts;
|
|
641
|
+
if (JSON.stringify(old) !== JSON.stringify(published))
|
|
642
|
+
participantsChanged();
|
|
610
643
|
try {
|
|
611
644
|
await savePeers();
|
|
612
645
|
}
|
|
@@ -785,10 +818,12 @@ export async function startServer(root, execute, hooks = {}) {
|
|
|
785
818
|
if (closed)
|
|
786
819
|
return;
|
|
787
820
|
let changed = false;
|
|
821
|
+
let participationChanged = false;
|
|
788
822
|
for (const [id, peer] of participants) {
|
|
789
823
|
const exited = !processExists(peer.facts.pid);
|
|
790
824
|
if (!exited && (!peer.connected || Date.now() - peer.seen <= 5_000))
|
|
791
825
|
continue;
|
|
826
|
+
participationChanged ||= participates(peer);
|
|
792
827
|
peer.connected = false;
|
|
793
828
|
peer.suspected = !exited;
|
|
794
829
|
if (exited)
|
|
@@ -811,10 +846,12 @@ export async function startServer(root, execute, hooks = {}) {
|
|
|
811
846
|
}
|
|
812
847
|
// Contact expiry alone retains the live session and its uncertain grants.
|
|
813
848
|
}
|
|
814
|
-
if (
|
|
849
|
+
if (participationChanged)
|
|
850
|
+
participantsChanged();
|
|
851
|
+
if (changed)
|
|
815
852
|
await savePeers();
|
|
853
|
+
if (participationChanged)
|
|
816
854
|
await hooks.factsChanged?.();
|
|
817
|
-
}
|
|
818
855
|
}
|
|
819
856
|
const lifetime = async () => closed || compromised || peerWriteFailed ? "pause" : ((await hooks.lifetime?.()) ?? "pause");
|
|
820
857
|
if (hooks.lifetime) {
|
|
@@ -917,6 +954,7 @@ export async function startServer(root, execute, hooks = {}) {
|
|
|
917
954
|
const job = {
|
|
918
955
|
view: { id: randomUUID(), phase: "running" },
|
|
919
956
|
digest: "",
|
|
957
|
+
participantGeneration,
|
|
920
958
|
controller: new AbortController(),
|
|
921
959
|
committed: false,
|
|
922
960
|
promise: Promise.resolve(),
|
|
@@ -941,11 +979,14 @@ export async function startServer(root, execute, hooks = {}) {
|
|
|
941
979
|
return endpoint.retainedDirectory;
|
|
942
980
|
},
|
|
943
981
|
participants: participantFacts,
|
|
982
|
+
participantGeneration: () => participantGeneration,
|
|
944
983
|
liveClients: liveClientFacts,
|
|
945
984
|
expireClients,
|
|
946
985
|
// The heartbeat and deterministic lifecycle tests use the same evaluation boundary.
|
|
947
986
|
checkLifetime: () => idleExit?.tick() ?? Promise.resolve(),
|
|
948
|
-
isIdle: () => !closed &&
|
|
987
|
+
isIdle: () => !closed &&
|
|
988
|
+
!stopRequested &&
|
|
989
|
+
[...jobs.values()].every((job) => job.view.phase !== "queued" && job.view.phase !== "running"),
|
|
949
990
|
get closed() {
|
|
950
991
|
return closed;
|
|
951
992
|
},
|
package/dist/service-host.d.ts
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
import { type EngineContext } from "./engine/host.js";
|
|
2
2
|
import type { Execution } from "./protocol.js";
|
|
3
3
|
/** One service process owns one root; tests must use a separate process per root. */
|
|
4
|
-
export declare function installServiceHost(root: string): {
|
|
4
|
+
export declare function installServiceHost(root: string, ignoreFileOperationsAvailable?: boolean): {
|
|
5
5
|
run<T>(execution: Execution, action: () => Promise<T>): Promise<T>;
|
|
6
6
|
};
|
|
7
7
|
export declare function createServiceContext(execution: Execution): EngineContext;
|
package/dist/service-host.js
CHANGED
|
@@ -9,7 +9,7 @@ import { canonicalRootGuard, processExists } from "./identity.js";
|
|
|
9
9
|
import { withCleanup } from "./lifecycle.js";
|
|
10
10
|
const scope = new AsyncLocalStorage();
|
|
11
11
|
/** One service process owns one root; tests must use a separate process per root. */
|
|
12
|
-
export function installServiceHost(root) {
|
|
12
|
+
export function installServiceHost(root, ignoreFileOperationsAvailable) {
|
|
13
13
|
let poisoned = false;
|
|
14
14
|
const assertRoot = canonicalRootGuard(root);
|
|
15
15
|
function current() {
|
|
@@ -66,6 +66,7 @@ export function installServiceHost(root) {
|
|
|
66
66
|
});
|
|
67
67
|
}
|
|
68
68
|
configureEngineHost({
|
|
69
|
+
ignoreFileOperationsAvailable,
|
|
69
70
|
allowConfigMigration: () => Boolean(readConsent(root)),
|
|
70
71
|
root: () => {
|
|
71
72
|
assertRoot();
|
package/docs/architecture.md
CHANGED
|
@@ -33,20 +33,28 @@ flowchart LR
|
|
|
33
33
|
|
|
34
34
|
Read-only display state is not a second settings writer. A client proposes a validated new document with the exact source fingerprint; the authoritative writer checks the fingerprint under its settings lock. A stale editor must reopen rather than merge a stale approval automatically. Invalid existing JSON remains intact. Unknown fields remain in the proposed source clone.
|
|
35
35
|
|
|
36
|
-
|
|
36
|
+
Reviewed ignore-file switch and migration require an existing canonical `pi-sync.json`; they do not promote `pi-sync.local.json`. A new setup in an unconfigured root uses the existing private temporary-file-plus-rename settings writer. The shared lock orders cooperating writers, but a file or absence check followed by rename is not atomic against writers that ignore the lock. Neither settings installation nor the legacy settings workflow changes in this activation.
|
|
37
37
|
|
|
38
|
-
### Reviewed ignore policy changes (
|
|
38
|
+
### Reviewed ignore policy changes (activation candidate)
|
|
39
39
|
|
|
40
|
-
|
|
40
|
+
When the daemon advertises ignore-file support, `ignore.switch` and `ignore.migrate` use the reviewed settings writer. The reviewed `settings.cas` route may introduce one new ignore-file setup and accept validated unrelated edits in mixed documents. It cannot bypass migration or switching review for an existing policy. An optional first-open offer token must come from the active TUI setup and still match at publication; explicit Settings migration does not require an offer. Migration keeps the resolved session opt-in; the rules can still exclude sessions. After preparing the files, migration checks the remote head before and after comparing the local paths under both policies. It omits only the starter file created by that operation from the prior policy's paths. It then rechecks the rule source and settings before publication. Policy validation reads the rule source under the shared settings lock and rechecks the settings document before accepting the source digest. A late refusal does not save the new policy or start a transfer; a verified starter file or private settings backup already prepared can remain. The remote head, filesystem inventory, and settings publication are not one atomic transaction.
|
|
41
|
+
|
|
42
|
+
For a missing default on Linux, macOS, or Windows, create exclusively and retain an interruption record until the starter bytes are flushed and verified. Use a verified held directory where it can address child files. Otherwise verify the canonical directory and file identities around path-based operations. Those checks cannot make paths atomic against unrelated directory replacements. Windows does not flush the directory through Node; macOS only does so when supported by the filesystem. This fallback does not promise physical power-loss durability. If creation is unavailable, return the path, starter rules, and retry guidance; incomplete creation keeps its recovery evidence and does not commit settings. An existing valid source is reused. A preview of absent rules cannot authorize an operation.
|
|
41
43
|
|
|
42
44
|
Opening Settings can show an existing rule file path without reading or writing the rules. The daemon rejects `ignore.save`: Node does not provide an atomic rename conditional on the destination still matching the reviewed file. The daemon's locks do not extend to external editors. `ignore.read` checks reported live session files by device and inode so aliases cannot expose session text as rules.
|
|
43
45
|
|
|
44
46
|
### Local ignore migration deferral
|
|
45
47
|
|
|
46
|
-
`ignore.migration-offer` and `ignore.migration-defer` require a current attached TUI client. The read returns an opaque token only for the requested active legacy setup without a deferral. Opening or dismissing a prompt does not persist a choice. An explicit decline must present the same setup and token, which binds the active document's path, file identity, and bytes to the migration version. `ignore.capabilities` reports
|
|
48
|
+
`ignore.migration-offer` and `ignore.migration-defer` require a current attached TUI client. The read returns an opaque token only for the requested active legacy setup without a deferral. Opening or dismissing a prompt does not persist a choice. An explicit decline must present the same setup and token, which binds the active document's path, file identity, and bytes to the migration version. Reading `ignore.capabilities` only reports service support; it does not migrate or authorize anything. None of these operations pause the monitor, take the sync lock, contact a backend, or change settings or rules.
|
|
47
49
|
|
|
48
50
|
The local settings queue orders both operations. A read checks the active document under the cross-process config lock when settings or a writer lock are present. A decline always holds that lock. Before publishing a decline, the daemon rechecks the source and client and exclusively creates an empty private marker in the canonical root. This serializes cooperating writers; it is an optimistic check, not an atomic comparison against external editors or parent-directory replacement. Existing markers must be regular, private, single-link, and empty; unsafe paths are not overwritten. The marker name is denied by the snapshot path policy and stores only a hash of the setup name and migration version. Declining applies to that name on this device, including across settings edits and daemon restarts. Success waits for the file flush and, on POSIX, the root directory flush. Once created, the marker stays in place if its durability is uncertain; the operation reports the failure without modifying settings or removing the marker.
|
|
49
51
|
|
|
52
|
+
### Operational policy binding (activation candidate)
|
|
53
|
+
|
|
54
|
+
Version 8 settings can contain both ignore-file and legacy setups. Display projections can carry unresolved settings metadata; operations resolve the intended setup and usable rule source before inventory, transfer or recovery. Reserve the default and configured rule filenames across setups, including legacy operations within a mixed document. Hard path and live-session protection still apply; session opt-in is separate from the rules.
|
|
55
|
+
|
|
56
|
+
A version 6 snapshot carries portable intent (a versioned fingerprint of ordered rules and session opt-in), never the local filename, rule text, or local source evidence. Legacy readers refuse unsupported snapshots; upgrade peers before first publication. An explicit directional review is required when publishing a new policy over an incompatible head. There is no remote rule adoption or `--yes` bypass. Authorize foreground and existing opt-in automatic work separately from settings changes; setup, migration, or rule-file switching neither starts work nor carries forward stale authorization. Recheck settings, local source, remote revision, and mutation ownership after review and at the final apply or publication boundary. On uncertain publication, reconcile the existing attempt instead of starting a new one.
|
|
57
|
+
|
|
50
58
|
## Context inventory
|
|
51
59
|
|
|
52
60
|
| Former Pi dependency | Engine host boundary | Source |
|
|
@@ -56,7 +64,7 @@ The local settings queue orders both operations. A read checks the active docume
|
|
|
56
64
|
| `ctx.isIdle()` | Current participating-client facts, not a captured Pi context | `service-host.ts` |
|
|
57
65
|
| Protected sessions and missing-settings guard | All live client PIDs, including retired clients; separate from active participation | `server.ts`, `service-host.ts` |
|
|
58
66
|
| `ctx.ui.notify`, status | Bounded events | `server.ts`, `service-host.ts` |
|
|
59
|
-
| Confirm/select/exact merge review | Job-bound callbacks; revalidation after response | `service-host.ts`, `engine/ui/
|
|
67
|
+
| Confirm/select/exact merge review | Job-bound callbacks; revalidation after response; directional TUI review sends compact summary lines and the full path list as separate fields | `service-host.ts`, `engine/ui/transfer-review.ts` |
|
|
60
68
|
| Reload | Explicit client callback after the existing user confirmation | `engine/sync/sync-mutations.ts` |
|
|
61
69
|
| `ctx.model`, `modelRegistry.streamSimple` | Explicit selected model identity/signature; independent public SDK runtime | `model-signature.ts`, `resolver-host.ts` |
|
|
62
70
|
| `withFileMutationQueue` | Participating Pi queue grants plus daemon-local queue | `service-host.ts`, Pi adapter `daemon/apply-gate.ts` |
|
|
@@ -207,7 +215,7 @@ These guarantees cover cooperating registered clients. Unregistered old Pi versi
|
|
|
207
215
|
|
|
208
216
|
Version 10 journals use unique workspace slots. Scratch files get separate unique slots in private directories on the destination filesystem. Recovery reads only the names that the durable journal or scratch record identifies. A failed publication can leave unrecorded objects, but it cannot block a new slot or authorize deleting the old one. The synced path policy excludes these private objects. Older journals keep their exact paths and identity checks; an upgrade does not adopt unrecorded data.
|
|
209
217
|
|
|
210
|
-
|
|
218
|
+
Apply under an ignore-file or mixed legacy policy uses transaction journal version 12 for directory scope or version 13 for selected paths. The journal binds the setup, destination, local-field policy, and reserved filenames, plus exact local source evidence for ignore-file setups. Merge journals version 3, accepted baselines, and retained conflict evidence also bind the local policy. These are private records, not part of the portable selection. Pending recovery checks the recorded binding under mutation ownership. A journal without the required binding cannot authorize replay under an activated policy. Completed cleanup does not replay file changes. Preserve uncertain evidence; do not replay under different rules or treat a downgrade after v6 publication as rollback. See [ADR 0008](../../../docs/adr/0008-pi-sync-ignore-file.md) for the contract and activation gates.
|
|
211
219
|
|
|
212
220
|
A merge journal can be retired when its recorded commit is already the accepted baseline, even if the remote policy has changed. This does not apply files from the newer head. The prepared ignore-policy merge engine also supports an archive-only review for a pending journal whose original remote head and accepted baseline still match. Directional force cannot skip that recovery. The review can keep newer local edits and archive the journal without changing the backup, the baseline, or the remote. Before retiring an inactive journal without review, recovery checks the baseline and policy, takes a fresh local snapshot, and rechecks the remote head. After archive-only approval, it checks the baseline and policy before the final remote read and rechecks session ownership before archiving. The operation lock orders daemon work but cannot make these reads atomic with external editors or remote writers. If the evidence is uncertain, recovery leaves the journal in place.
|
|
213
221
|
|