@narumitw/pi-sync-daemon 0.8.0 → 0.9.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 +10 -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 +3 -0
- package/dist/engine/ignore/ignore-initialization.js +93 -27
- package/dist/engine/ignore/ignore-settings.d.ts +16 -1
- package/dist/engine/ignore/ignore-settings.js +85 -14
- 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-mutations.js +71 -50
- 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-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 +13 -5
- package/package.json +1 -1
package/CHANGELOG.md
CHANGED
|
@@ -1,5 +1,15 @@
|
|
|
1
1
|
# @narumitw/pi-sync-daemon
|
|
2
2
|
|
|
3
|
+
## 0.9.0
|
|
4
|
+
|
|
5
|
+
### Minor Changes
|
|
6
|
+
|
|
7
|
+
- 5dfa289: Resolve local ignore-file policies for reviewed setup, inspection, transfer, and recovery while keeping legacy policies usable in mixed settings. Keep rule files local and publish only the portable policy fingerprint in version 6 snapshots. Create a missing starter after confirmation on Linux, macOS, and Windows; if automatic creation is unavailable, show the exact path and instructions for manual creation. Preserve the existing settings writer and keep in-app rule saves disabled.
|
|
8
|
+
|
|
9
|
+
### Patch Changes
|
|
10
|
+
|
|
11
|
+
- 25777f7: Read Git history manifests in bounded batches to reduce subprocess launches. Keep the same history preview and validate all retained publications in the original order.
|
|
12
|
+
|
|
3
13
|
## 0.8.0
|
|
4
14
|
|
|
5
15
|
### Minor Changes
|
package/dist/cli.js
CHANGED
|
@@ -39,7 +39,7 @@ export async function main(args) {
|
|
|
39
39
|
log?.close(); /* Never follow a replaced diagnostic path. */
|
|
40
40
|
}
|
|
41
41
|
};
|
|
42
|
-
const service = createEngineService(paths.root);
|
|
42
|
+
const service = createEngineService(paths.root, true);
|
|
43
43
|
let server;
|
|
44
44
|
try {
|
|
45
45
|
server = await startServer(paths.root, service.execute, {
|
package/dist/consent.d.ts
CHANGED
|
@@ -7,6 +7,7 @@ interface Consent {
|
|
|
7
7
|
nonce: string;
|
|
8
8
|
setups: Record<string, {
|
|
9
9
|
fingerprint: string;
|
|
10
|
+
automaticFingerprint?: string;
|
|
10
11
|
model?: ClientFacts["model"];
|
|
11
12
|
sessionDir?: string;
|
|
12
13
|
cwd?: string;
|
|
@@ -17,10 +18,21 @@ export declare class ConsentPublicationError extends Error {
|
|
|
17
18
|
}
|
|
18
19
|
/** A different interactive binding cannot silently resume an older unattended grant after exit/restart. */
|
|
19
20
|
export declare function revokeChangedBindings(root: string, peers: readonly ClientFacts[]): void;
|
|
20
|
-
|
|
21
|
+
export declare function revokeAfterExitGrants(root: string): number;
|
|
21
22
|
export declare function revokeRetainedGrants(root: string): number;
|
|
22
23
|
export declare function readConsent(root: string): Consent | undefined;
|
|
23
24
|
export declare function consentFingerprint(root: string, config: AnySyncConfig): string;
|
|
25
|
+
export declare function automaticPolicyAuthorized(root: string, config: AnySyncConfig): boolean;
|
|
26
|
+
export declare function reviewAutomaticPolicy(execution: Execution, config: AnySyncConfig, expectedFingerprint: string): Promise<boolean>;
|
|
27
|
+
export declare function captureAutomaticAuthorization(execution: Execution, expectedFingerprint?: string): Promise<{
|
|
28
|
+
config: AnySyncConfig;
|
|
29
|
+
validate: (current?: AnySyncConfig) => void;
|
|
30
|
+
refresh: () => Promise<void>;
|
|
31
|
+
}>;
|
|
32
|
+
export declare function participantBinding(execution: Pick<Execution, "participants">): string;
|
|
24
33
|
export declare function requireHandover(execution: Execution): Promise<void>;
|
|
25
|
-
export declare function authorizeAfterExit(execution: Execution, payload: unknown): Promise<
|
|
34
|
+
export declare function authorizeAfterExit(execution: Execution, payload: unknown): Promise<{
|
|
35
|
+
config: AnySyncConfig;
|
|
36
|
+
afterExit: boolean;
|
|
37
|
+
} | undefined>;
|
|
26
38
|
export {};
|
package/dist/consent.js
CHANGED
|
@@ -3,6 +3,8 @@ import { closeSync, fstatSync, fsyncSync, lstatSync, mkdirSync, openSync, readFi
|
|
|
3
3
|
import path from "node:path";
|
|
4
4
|
import { syncCheckConfigFingerprint } from "./engine/settings/config.js";
|
|
5
5
|
import { stateDir } from "./engine/state/state-directory.js";
|
|
6
|
+
import { startupTransferEnabled } from "./engine/sync/agent-policy.js";
|
|
7
|
+
import { syncSessionsSelected } from "./engine/sync/sync-policy.js";
|
|
6
8
|
import { safeTerminalText } from "./engine/ui/terminal-text.js";
|
|
7
9
|
import { record } from "./protocol.js";
|
|
8
10
|
function file(root) {
|
|
@@ -82,7 +84,9 @@ export function revokeChangedBindings(root, peers) {
|
|
|
82
84
|
if (changed)
|
|
83
85
|
publishConsent(consent);
|
|
84
86
|
}
|
|
85
|
-
|
|
87
|
+
export function revokeAfterExitGrants(root) {
|
|
88
|
+
return revokeRetainedGrants(root);
|
|
89
|
+
}
|
|
86
90
|
export function revokeRetainedGrants(root) {
|
|
87
91
|
const consent = readConsent(root);
|
|
88
92
|
if (!consent)
|
|
@@ -129,6 +133,8 @@ export function readConsent(root) {
|
|
|
129
133
|
if (!record(entry) ||
|
|
130
134
|
typeof entry.fingerprint !== "string" ||
|
|
131
135
|
!/^[a-f0-9]{64}$/u.test(entry.fingerprint) ||
|
|
136
|
+
(entry.automaticFingerprint !== undefined &&
|
|
137
|
+
(typeof entry.automaticFingerprint !== "string" || !/^[a-f0-9]{64}$/u.test(entry.automaticFingerprint))) ||
|
|
132
138
|
typeof entry.afterExit !== "boolean" ||
|
|
133
139
|
(entry.cwd !== undefined &&
|
|
134
140
|
(typeof entry.cwd !== "string" || !path.isAbsolute(entry.cwd) || entry.cwd.includes("\0"))) ||
|
|
@@ -167,6 +173,119 @@ export function consentFingerprint(root, config) {
|
|
|
167
173
|
.update(syncCheckConfigFingerprint(config))
|
|
168
174
|
.digest("hex");
|
|
169
175
|
}
|
|
176
|
+
export function automaticPolicyAuthorized(root, config) {
|
|
177
|
+
if (config.reservedIgnoreNames === undefined)
|
|
178
|
+
return true;
|
|
179
|
+
const saved = readConsent(root)?.setups[config.setupName];
|
|
180
|
+
return saved?.automaticFingerprint !== undefined && saved.automaticFingerprint === consentFingerprint(root, config);
|
|
181
|
+
}
|
|
182
|
+
export async function reviewAutomaticPolicy(execution, config, expectedFingerprint) {
|
|
183
|
+
if (consentFingerprint(execution.root, config) !== expectedFingerprint)
|
|
184
|
+
throw new Error("Setup changed; reopen the automatic transfer review.");
|
|
185
|
+
if (config.reservedIgnoreNames === undefined || automaticPolicyAuthorized(execution.root, config))
|
|
186
|
+
return false;
|
|
187
|
+
const consent = readConsent(execution.root);
|
|
188
|
+
if (!consent)
|
|
189
|
+
return false;
|
|
190
|
+
const consentToken = JSON.stringify(consent);
|
|
191
|
+
const peers = participantBinding(execution);
|
|
192
|
+
const binding = JSON.stringify(execution.facts());
|
|
193
|
+
const { loadConfigForCheck } = await import("./engine/settings/config.js");
|
|
194
|
+
const fingerprint = syncCheckConfigFingerprint(config);
|
|
195
|
+
const validate = () => {
|
|
196
|
+
execution.validate();
|
|
197
|
+
execution.signal.throwIfAborted();
|
|
198
|
+
if (participantBinding(execution) !== peers ||
|
|
199
|
+
JSON.stringify(execution.facts()) !== binding ||
|
|
200
|
+
JSON.stringify(readConsent(execution.root)) !== consentToken)
|
|
201
|
+
throw new Error("Participants or consent changed during automatic transfer review.");
|
|
202
|
+
};
|
|
203
|
+
const current = await loadConfigForCheck(undefined, execution.signal);
|
|
204
|
+
validate();
|
|
205
|
+
if (syncCheckConfigFingerprint(current) !== fingerprint)
|
|
206
|
+
return false;
|
|
207
|
+
const modes = [
|
|
208
|
+
startupTransferEnabled(config) && "Automatic transfer at startup",
|
|
209
|
+
config.automatic &&
|
|
210
|
+
syncSessionsSelected(config) &&
|
|
211
|
+
"Push selected files on shutdown (including sessions when selected)",
|
|
212
|
+
config.continuous && config.agentMode && "Continuous sync and Autonomous model requests",
|
|
213
|
+
].filter(Boolean);
|
|
214
|
+
if (modes.length === 0)
|
|
215
|
+
throw new Error("No automatic transfer mode is enabled under the current local rules.");
|
|
216
|
+
const accepted = await execution.request("confirm", {
|
|
217
|
+
title: "Allow the enabled automatic sync modes for this setup?",
|
|
218
|
+
message: [
|
|
219
|
+
`Setup: ${safeTerminalText(config.setupName)}`,
|
|
220
|
+
`Destination: ${safeTerminalText(config.connectionName)} · ${safeTerminalText(config.storagePath)}`,
|
|
221
|
+
...(config.ignorePolicy
|
|
222
|
+
? [
|
|
223
|
+
`Local ignore file: ${safeTerminalText(config.ignorePolicy.filename)}`,
|
|
224
|
+
"Review this file before you approve its scope.",
|
|
225
|
+
]
|
|
226
|
+
: []),
|
|
227
|
+
`Enabled modes: ${modes.join("; ")}`,
|
|
228
|
+
"Their usual baseline, remote selection, and ownership checks still apply. This does not allow work after the last Pi exits. A settings or rule-file change requires a new review.",
|
|
229
|
+
].join("\n"),
|
|
230
|
+
});
|
|
231
|
+
validate();
|
|
232
|
+
if (accepted !== true)
|
|
233
|
+
return false;
|
|
234
|
+
const latest = await loadConfigForCheck(undefined, execution.signal);
|
|
235
|
+
await config.validateIgnorePolicy?.();
|
|
236
|
+
validate();
|
|
237
|
+
if (syncCheckConfigFingerprint(latest) !== fingerprint)
|
|
238
|
+
throw new Error("Setup changed during automatic transfer review.");
|
|
239
|
+
const saved = consent.setups[config.setupName];
|
|
240
|
+
const automaticFingerprint = consentFingerprint(execution.root, config);
|
|
241
|
+
publishConsent({
|
|
242
|
+
...consent,
|
|
243
|
+
setups: {
|
|
244
|
+
...consent.setups,
|
|
245
|
+
[config.setupName]: {
|
|
246
|
+
...saved,
|
|
247
|
+
fingerprint: saved?.fingerprint ?? automaticFingerprint,
|
|
248
|
+
afterExit: saved?.afterExit ?? false,
|
|
249
|
+
automaticFingerprint,
|
|
250
|
+
},
|
|
251
|
+
},
|
|
252
|
+
});
|
|
253
|
+
return true;
|
|
254
|
+
}
|
|
255
|
+
export async function captureAutomaticAuthorization(execution, expectedFingerprint) {
|
|
256
|
+
const consent = readConsent(execution.root);
|
|
257
|
+
if (!consent)
|
|
258
|
+
throw new Error("Automatic work requires reviewed daemon handover first.");
|
|
259
|
+
const consentToken = JSON.stringify(consent);
|
|
260
|
+
const peers = participantBinding(execution);
|
|
261
|
+
const { loadConfigForCheck } = await import("./engine/settings/config.js");
|
|
262
|
+
const validateParticipants = () => {
|
|
263
|
+
execution.validate();
|
|
264
|
+
if (participantBinding(execution) !== peers || JSON.stringify(readConsent(execution.root)) !== consentToken)
|
|
265
|
+
throw new Error("Daemon participants or consent changed before automatic work completed.");
|
|
266
|
+
};
|
|
267
|
+
validateParticipants();
|
|
268
|
+
const config = await loadConfigForCheck(undefined, execution.signal);
|
|
269
|
+
validateParticipants();
|
|
270
|
+
const fingerprint = syncCheckConfigFingerprint(config);
|
|
271
|
+
if (expectedFingerprint !== undefined ? fingerprint !== expectedFingerprint : config.reservedIgnoreNames !== undefined)
|
|
272
|
+
throw new Error("Settings changed before automatic work; use the reviewed foreground command.");
|
|
273
|
+
if (!automaticPolicyAuthorized(execution.root, config))
|
|
274
|
+
throw new Error("Automatic transfer requires separate approval for the current setup and policy. Run reviewed /sync first.");
|
|
275
|
+
const validate = (current) => {
|
|
276
|
+
validateParticipants();
|
|
277
|
+
if (current && syncCheckConfigFingerprint(current) !== fingerprint)
|
|
278
|
+
throw new Error("Settings changed before automatic work completed.");
|
|
279
|
+
};
|
|
280
|
+
const refresh = async () => {
|
|
281
|
+
validate();
|
|
282
|
+
validate(await loadConfigForCheck(undefined, execution.signal));
|
|
283
|
+
};
|
|
284
|
+
return { config, validate, refresh };
|
|
285
|
+
}
|
|
286
|
+
export function participantBinding(execution) {
|
|
287
|
+
return JSON.stringify(execution.participants().toSorted((left, right) => left.id.localeCompare(right.id)));
|
|
288
|
+
}
|
|
170
289
|
export async function requireHandover(execution) {
|
|
171
290
|
if (readConsent(execution.root))
|
|
172
291
|
return;
|
|
@@ -194,9 +313,12 @@ export async function authorizeAfterExit(execution, payload) {
|
|
|
194
313
|
if (!record(payload) || typeof payload.afterExit !== "boolean" || typeof payload.fingerprint !== "string")
|
|
195
314
|
throw new Error("Invalid daemon authorization.");
|
|
196
315
|
await requireHandover(execution);
|
|
197
|
-
const
|
|
198
|
-
const
|
|
316
|
+
const peers = participantBinding(execution);
|
|
317
|
+
const { loadConfigForCheck } = await import("./engine/settings/config.js");
|
|
318
|
+
const config = await loadConfigForCheck(undefined, execution.signal);
|
|
199
319
|
execution.validate();
|
|
320
|
+
if (participantBinding(execution) !== peers)
|
|
321
|
+
throw new Error("Participants changed during daemon authorization.");
|
|
200
322
|
const facts = execution.facts();
|
|
201
323
|
const binding = JSON.stringify({ model: facts?.model, sessionDir: facts?.sessionDir, cwd: facts?.cwd });
|
|
202
324
|
const fingerprint = consentFingerprint(execution.root, config);
|
|
@@ -222,22 +344,17 @@ export async function authorizeAfterExit(execution, payload) {
|
|
|
222
344
|
: `Selected content: ${paths(config.include)}`;
|
|
223
345
|
const accepted = await execution.request("confirm", {
|
|
224
346
|
title: payload.afterExit ? "Allow continuous sync after all Pi clients exit?" : "Revoke unattended sync?",
|
|
225
|
-
message: `Setup: ${safeTerminalText(config.setupName)}\n${selection}\nModel: ${safeTerminalText(facts?.model?.provider ?? "unavailable")}/${safeTerminalText(facts?.model?.id ?? "unavailable")}\n\n${payload.afterExit ?
|
|
347
|
+
message: `Setup: ${safeTerminalText(config.setupName)}\n${selection}\nModel: ${safeTerminalText(facts?.model?.provider ?? "unavailable")}/${safeTerminalText(facts?.model?.id ?? "unavailable")}\n\n${payload.afterExit ? `The daemon may transfer selected private content and send conflict evidence to this independently available provider after Pi exits. Existing budgets, exclusions, journals, and stale-evidence checks still apply. No parent conversation or tools are sent. Changing this setup or model invalidates this permission.${config.reservedIgnoreNames !== undefined ? " This also approves the enabled automatic modes for this setup while Pi clients are attached." : ""}` : "The daemon may remain alive, but no new transfer or model request may start with zero clients."}`,
|
|
226
348
|
});
|
|
227
349
|
execution.validate();
|
|
228
350
|
if (accepted !== true)
|
|
229
|
-
return
|
|
230
|
-
const latest = await
|
|
351
|
+
return undefined;
|
|
352
|
+
const latest = await loadConfigForCheck(undefined, execution.signal);
|
|
231
353
|
execution.validate();
|
|
232
|
-
|
|
233
|
-
|
|
234
|
-
|
|
235
|
-
|
|
236
|
-
signal: execution.signal,
|
|
237
|
-
validateMutation: () => execution.validate(),
|
|
238
|
-
})?.();
|
|
239
|
-
}
|
|
240
|
-
if (consentFingerprint(execution.root, latest) !== fingerprint ||
|
|
354
|
+
await config.validateIgnorePolicy?.();
|
|
355
|
+
execution.validate();
|
|
356
|
+
if (participantBinding(execution) !== peers ||
|
|
357
|
+
consentFingerprint(execution.root, latest) !== fingerprint ||
|
|
241
358
|
JSON.stringify({
|
|
242
359
|
model: execution.facts()?.model,
|
|
243
360
|
sessionDir: execution.facts()?.sessionDir,
|
|
@@ -258,8 +375,9 @@ export async function authorizeAfterExit(execution, payload) {
|
|
|
258
375
|
sessionDir: facts?.sessionDir,
|
|
259
376
|
cwd: facts?.cwd,
|
|
260
377
|
afterExit: payload.afterExit,
|
|
378
|
+
...(payload.afterExit && config.reservedIgnoreNames !== undefined ? { automaticFingerprint: fingerprint } : {}),
|
|
261
379
|
},
|
|
262
380
|
},
|
|
263
381
|
});
|
|
264
|
-
return
|
|
382
|
+
return { config, afterExit: payload.afterExit };
|
|
265
383
|
}
|
|
@@ -35,8 +35,7 @@ export declare class GitSyncBackend implements SyncBackend {
|
|
|
35
35
|
private resolveSnapshotReference;
|
|
36
36
|
private headForSha;
|
|
37
37
|
private fetchRemoteHead;
|
|
38
|
-
private
|
|
39
|
-
private showFile;
|
|
38
|
+
private tryReadHistoryManifestBatch;
|
|
40
39
|
private createCommit;
|
|
41
40
|
private ensureCache;
|
|
42
41
|
private initializeCache;
|
|
@@ -9,10 +9,12 @@ import { throwIfAborted } from "../../sync/signals.js";
|
|
|
9
9
|
import { syncErrorGuidance } from "../../sync/sync-error-guidance.js";
|
|
10
10
|
import { SyncBackendConflictError, SyncBackendPublicationOutcomeUnknownError, } from "../sync-backend.js";
|
|
11
11
|
import { normalizeGitBranch, normalizeGitDirectory, normalizeGitRemote, normalizeGitRemoteIdentity, validateGitNamespace, } from "./git-config.js";
|
|
12
|
-
import { GitCommandError, readGitBlobs, runGit } from "./git-runner.js";
|
|
12
|
+
import { GitCommandError, parseGitBlobBatch, readGitBlobs, runGit } from "./git-runner.js";
|
|
13
13
|
import { GIT_MANIFEST_VERSION, MAX_GIT_MANIFEST_BYTES, MAX_GIT_TREE_OUTPUT_BYTES, parseGitTree, prepareGitSnapshot, requireGitManifest, validateGitPublicationTree, validateGitSnapshot, } from "./git-storage.js";
|
|
14
14
|
const COMMAND_TIMEOUT_MS = 30_000;
|
|
15
15
|
const POST_COMMIT_TIMEOUT_MS = 45_000;
|
|
16
|
+
const HISTORY_MANIFEST_BATCH_SIZE = 8;
|
|
17
|
+
const MANIFEST_BATCH_FRAME_BYTES = 96;
|
|
16
18
|
const gitCacheMutationQueues = new Map();
|
|
17
19
|
export class GitSyncBackend {
|
|
18
20
|
config;
|
|
@@ -182,20 +184,28 @@ export class GitSyncBackend {
|
|
|
182
184
|
}
|
|
183
185
|
async listHistory(signal) {
|
|
184
186
|
const sha = await this.fetchRemoteHead(signal);
|
|
187
|
+
throwIfAborted(signal);
|
|
185
188
|
if (!sha)
|
|
186
189
|
return [];
|
|
187
190
|
const result = await this.git(["rev-list", "--first-parent", "--reverse", "--max-count=100", sha], { signal });
|
|
188
191
|
const commits = result.stdout.toString("utf8").trim().split("\n").filter(Boolean);
|
|
189
192
|
const entries = [];
|
|
190
|
-
|
|
191
|
-
|
|
192
|
-
|
|
193
|
-
|
|
194
|
-
|
|
195
|
-
|
|
196
|
-
|
|
197
|
-
|
|
198
|
-
|
|
193
|
+
throwIfAborted(signal);
|
|
194
|
+
for (let offset = 0; offset < commits.length; offset += HISTORY_MANIFEST_BATCH_SIZE) {
|
|
195
|
+
const group = commits.slice(offset, offset + HISTORY_MANIFEST_BATCH_SIZE);
|
|
196
|
+
const manifests = await this.tryReadHistoryManifestBatch(group, signal);
|
|
197
|
+
for (const [index, commit] of group.entries()) {
|
|
198
|
+
throwIfAborted(signal);
|
|
199
|
+
const { manifest } = await this.readPublication(commit, signal, manifests?.[index]);
|
|
200
|
+
throwIfAborted(signal);
|
|
201
|
+
entries.push({
|
|
202
|
+
snapshotRef: commit,
|
|
203
|
+
snapshotId: manifest.snapshotId,
|
|
204
|
+
createdAt: manifest.createdAt,
|
|
205
|
+
machine: manifest.machine,
|
|
206
|
+
syncSessions: manifest.syncSessions,
|
|
207
|
+
});
|
|
208
|
+
}
|
|
199
209
|
}
|
|
200
210
|
return entries;
|
|
201
211
|
}
|
|
@@ -314,27 +324,27 @@ export class GitSyncBackend {
|
|
|
314
324
|
}
|
|
315
325
|
}, signal);
|
|
316
326
|
}
|
|
317
|
-
async
|
|
318
|
-
requireCommitSha(commit);
|
|
319
|
-
const bytes = await this.showFile(commit, this.manifestPath(), signal, MAX_GIT_MANIFEST_BYTES);
|
|
320
|
-
let parsed;
|
|
327
|
+
async tryReadHistoryManifestBatch(commits, signal) {
|
|
321
328
|
try {
|
|
322
|
-
|
|
323
|
-
|
|
324
|
-
|
|
325
|
-
|
|
329
|
+
for (const commit of commits)
|
|
330
|
+
requireCommitSha(commit);
|
|
331
|
+
const manifestPath = this.manifestPath();
|
|
332
|
+
const maxContentBytes = commits.length * MAX_GIT_MANIFEST_BYTES;
|
|
333
|
+
const result = await this.git(["cat-file", "--batch"], {
|
|
334
|
+
input: `${commits.map((commit) => `${commit}:${manifestPath}`).join("\n")}\n`,
|
|
335
|
+
maxOutputBytes: maxContentBytes + commits.length * MANIFEST_BATCH_FRAME_BYTES,
|
|
336
|
+
signal,
|
|
337
|
+
});
|
|
338
|
+
throwIfAborted(signal);
|
|
339
|
+
if (result.stderr.length > 0)
|
|
340
|
+
return undefined;
|
|
341
|
+
const blobs = parseGitBlobBatch(result.stdout, commits.length, maxContentBytes);
|
|
342
|
+
return blobs.every((blob) => blob.length <= MAX_GIT_MANIFEST_BYTES) ? blobs : undefined;
|
|
326
343
|
}
|
|
327
|
-
|
|
328
|
-
|
|
329
|
-
|
|
330
|
-
parsed.snapshotVersion === IGNORE_SNAPSHOT_VERSION &&
|
|
331
|
-
!isUtf8(bytes)) {
|
|
332
|
-
throw new Error("Git publication manifest is not valid UTF-8.");
|
|
344
|
+
catch {
|
|
345
|
+
throwIfAborted(signal);
|
|
346
|
+
return undefined;
|
|
333
347
|
}
|
|
334
|
-
return requireGitManifest(parsed);
|
|
335
|
-
}
|
|
336
|
-
showFile(commit, filePath, signal, maxOutputBytes) {
|
|
337
|
-
return this.git(["show", `${commit}:${filePath}`], { signal, maxOutputBytes }).then((result) => result.stdout);
|
|
338
348
|
}
|
|
339
349
|
async createCommit(snapshot, files, manifest, parent, signal) {
|
|
340
350
|
const manifestBytes = Buffer.from(`${JSON.stringify(manifest)}\n`, "utf8");
|
|
@@ -514,8 +524,12 @@ export class GitSyncBackend {
|
|
|
514
524
|
filePath(filePath) {
|
|
515
525
|
return posixJoin(this.publicationPath(), "files", filePath);
|
|
516
526
|
}
|
|
517
|
-
async readPublication(commit, signal) {
|
|
518
|
-
|
|
527
|
+
async readPublication(commit, signal, manifestBytes) {
|
|
528
|
+
requireCommitSha(commit);
|
|
529
|
+
const bytes = manifestBytes ??
|
|
530
|
+
(await this.git(["show", `${commit}:${this.manifestPath()}`], { signal, maxOutputBytes: MAX_GIT_MANIFEST_BYTES }))
|
|
531
|
+
.stdout;
|
|
532
|
+
const manifest = parseGitManifest(bytes);
|
|
519
533
|
const entries = await this.readPublicationTree(commit, signal, manifest.snapshotVersion === IGNORE_SNAPSHOT_VERSION);
|
|
520
534
|
const payloadEntries = validateGitPublicationTree(entries, manifest, this.manifestPath(), (filePath) => this.filePath(filePath));
|
|
521
535
|
return { manifest, payloadEntries };
|
|
@@ -572,6 +586,23 @@ function gitDestination(config) {
|
|
|
572
586
|
}
|
|
573
587
|
return `${host} · ${config.destination.branch}:${config.destination.directory}`;
|
|
574
588
|
}
|
|
589
|
+
function parseGitManifest(bytes) {
|
|
590
|
+
let parsed;
|
|
591
|
+
try {
|
|
592
|
+
parsed = JSON.parse(bytes.toString("utf8"));
|
|
593
|
+
}
|
|
594
|
+
catch (error) {
|
|
595
|
+
throw new Error("Git publication manifest is malformed.", { cause: error });
|
|
596
|
+
}
|
|
597
|
+
if (parsed &&
|
|
598
|
+
typeof parsed === "object" &&
|
|
599
|
+
"snapshotVersion" in parsed &&
|
|
600
|
+
parsed.snapshotVersion === IGNORE_SNAPSHOT_VERSION &&
|
|
601
|
+
!isUtf8(bytes)) {
|
|
602
|
+
throw new Error("Git publication manifest is not valid UTF-8.");
|
|
603
|
+
}
|
|
604
|
+
return requireGitManifest(parsed);
|
|
605
|
+
}
|
|
575
606
|
function remoteHead(sha, manifest, identity) {
|
|
576
607
|
return {
|
|
577
608
|
snapshotRef: sha,
|
|
@@ -9,6 +9,7 @@ export interface CommandOptions {
|
|
|
9
9
|
reload: boolean;
|
|
10
10
|
auto: boolean;
|
|
11
11
|
setup?: string;
|
|
12
|
+
automaticFingerprint?: string;
|
|
12
13
|
signal?: AbortSignal;
|
|
13
14
|
onCommit?: () => void;
|
|
14
15
|
/** In-memory startup diagnostics only; never persisted or sent to a backend. */
|
|
@@ -18,7 +19,7 @@ export interface CommandOptions {
|
|
|
18
19
|
/** Internal cycle authorization, ownership and durable request reservation; never command arguments. */
|
|
19
20
|
background?: {
|
|
20
21
|
validate(config?: AnySyncConfig): void;
|
|
21
|
-
reserve(key: string, signal?: AbortSignal): Promise<void>;
|
|
22
|
+
reserve?(key: string, signal?: AbortSignal): Promise<void>;
|
|
22
23
|
};
|
|
23
24
|
/** Manager-only, separately confirmed retry; never parsed from command arguments. */
|
|
24
25
|
agentRetryRefusal?: boolean;
|
package/dist/engine/host.d.ts
CHANGED
|
@@ -55,6 +55,7 @@ export interface ConfigAuthority {
|
|
|
55
55
|
replace(document: import("./settings/config-file.js").LocalConfigDocument, value: Record<string, unknown>): Promise<void>;
|
|
56
56
|
}
|
|
57
57
|
export interface EngineHost {
|
|
58
|
+
ignoreFileOperationsAvailable?: boolean;
|
|
58
59
|
root(): string;
|
|
59
60
|
config?: ConfigAuthority;
|
|
60
61
|
allowConfigMigration?(): boolean;
|
|
@@ -1,4 +1,7 @@
|
|
|
1
1
|
import { type IgnoreSource } from "./ignore-source.js";
|
|
2
|
+
export declare class AutomaticIgnoreCreationUnavailable extends Error {
|
|
3
|
+
}
|
|
4
|
+
export declare function checkAutomaticIgnoreCreation(root: string, signal: AbortSignal): Promise<void>;
|
|
2
5
|
export declare function initializeDefaultIgnoreSource(root: string, signal?: AbortSignal): Promise<IgnoreSource>;
|
|
3
6
|
export declare function prepareDefaultIgnoreSource(root: string, signal?: AbortSignal): Promise<{
|
|
4
7
|
source: IgnoreSource;
|
|
@@ -6,7 +6,28 @@ const starter = Buffer.from(DEFAULT_IGNORE_RULES);
|
|
|
6
6
|
const record = Buffer.from(`pi-sync-ignore-creation-v1\n${DEFAULT_IGNORE_RULES}`);
|
|
7
7
|
const noFollow = (constants.O_NOFOLLOW ?? 0) | (constants.O_NONBLOCK ?? 0);
|
|
8
8
|
const openRead = constants.O_RDONLY | noFollow;
|
|
9
|
+
const openExisting = process.platform === "win32" ? constants.O_RDWR | noFollow : openRead;
|
|
9
10
|
const openExclusive = constants.O_RDWR | constants.O_CREAT | constants.O_EXCL | noFollow;
|
|
11
|
+
export class AutomaticIgnoreCreationUnavailable extends Error {
|
|
12
|
+
}
|
|
13
|
+
export async function checkAutomaticIgnoreCreation(root, signal) {
|
|
14
|
+
signal.throwIfAborted();
|
|
15
|
+
root = path.resolve(root);
|
|
16
|
+
const expected = await fs.lstat(root, { bigint: true });
|
|
17
|
+
signal.throwIfAborted();
|
|
18
|
+
assertRoot(root, expected);
|
|
19
|
+
await checkCreationAccess(root);
|
|
20
|
+
const { directory } = await openCreationDirectory(root, expected);
|
|
21
|
+
try {
|
|
22
|
+
signal.throwIfAborted();
|
|
23
|
+
await syncCreationDirectory(directory);
|
|
24
|
+
assertRoot(root, expected);
|
|
25
|
+
}
|
|
26
|
+
finally {
|
|
27
|
+
await directory?.close();
|
|
28
|
+
signal.throwIfAborted();
|
|
29
|
+
}
|
|
30
|
+
}
|
|
10
31
|
export async function initializeDefaultIgnoreSource(root, signal) {
|
|
11
32
|
return (await prepareDefaultIgnoreSource(root, signal)).source;
|
|
12
33
|
}
|
|
@@ -15,15 +36,10 @@ export async function prepareDefaultIgnoreSource(root, signal) {
|
|
|
15
36
|
root = path.resolve(root);
|
|
16
37
|
try {
|
|
17
38
|
const initialRoot = await fs.lstat(root, { bigint: true });
|
|
18
|
-
const
|
|
19
|
-
const current = lstatSync(root, { bigint: true });
|
|
20
|
-
if (!current.isDirectory() || !sameIdentity(initialRoot, current) || realpathSync(root) !== root) {
|
|
21
|
-
throw new Error("the Pi agent directory is not canonical or changed during initialization");
|
|
22
|
-
}
|
|
23
|
-
};
|
|
39
|
+
const verifyRoot = () => assertRoot(root, initialRoot);
|
|
24
40
|
const checkRoot = () => {
|
|
25
41
|
signal?.throwIfAborted();
|
|
26
|
-
|
|
42
|
+
verifyRoot();
|
|
27
43
|
};
|
|
28
44
|
checkRoot();
|
|
29
45
|
const priorRecord = statAt(path.join(root, DEFAULT_IGNORE_CREATION_RECORD));
|
|
@@ -33,16 +49,11 @@ export async function prepareDefaultIgnoreSource(root, signal) {
|
|
|
33
49
|
if (source.present)
|
|
34
50
|
return { source, created: false };
|
|
35
51
|
}
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
}
|
|
39
|
-
const directory = await fs.open(root, openRead | constants.O_DIRECTORY);
|
|
52
|
+
await checkCreationAccess(root);
|
|
53
|
+
const { directory, anchor } = await openCreationDirectory(root, initialRoot);
|
|
40
54
|
try {
|
|
41
|
-
if (!sameIdentity(initialRoot, await directory.stat({ bigint: true })))
|
|
42
|
-
changed("Pi agent directory");
|
|
43
|
-
const anchor = await directoryAnchor(directory, initialRoot);
|
|
44
55
|
checkRoot();
|
|
45
|
-
await directory
|
|
56
|
+
const flushDirectory = await syncCreationDirectory(directory);
|
|
46
57
|
checkRoot();
|
|
47
58
|
const markerPath = path.join(anchor, DEFAULT_IGNORE_CREATION_RECORD);
|
|
48
59
|
const target = path.join(anchor, DEFAULT_IGNORE_FILENAME);
|
|
@@ -50,36 +61,40 @@ export async function prepareDefaultIgnoreSource(root, signal) {
|
|
|
50
61
|
assertOwned(priorRecord, DEFAULT_IGNORE_CREATION_RECORD, true);
|
|
51
62
|
else if (statAt(target))
|
|
52
63
|
changed(DEFAULT_IGNORE_FILENAME);
|
|
53
|
-
const marker = await fs.open(markerPath, priorRecord ?
|
|
64
|
+
const marker = await fs.open(markerPath, priorRecord ? openExisting : openExclusive, 0o600).catch((error) => {
|
|
65
|
+
if (!priorRecord)
|
|
66
|
+
checkCreationError(error);
|
|
67
|
+
throw error;
|
|
68
|
+
});
|
|
54
69
|
try {
|
|
55
|
-
const markerStat = await flushAndVerify(marker, markerPath, path.join(root, DEFAULT_IGNORE_CREATION_RECORD), record, priorRecord ? checkRoot :
|
|
56
|
-
await
|
|
70
|
+
const markerStat = await flushAndVerify(marker, markerPath, path.join(root, DEFAULT_IGNORE_CREATION_RECORD), record, priorRecord ? checkRoot : verifyRoot, priorRecord, true);
|
|
71
|
+
await flushDirectory();
|
|
57
72
|
checkRoot();
|
|
58
73
|
assertCurrent(markerPath, markerStat);
|
|
59
74
|
const priorFile = priorRecord ? statAt(target) : undefined;
|
|
60
75
|
if (priorFile)
|
|
61
76
|
assertOwned(priorFile, DEFAULT_IGNORE_FILENAME, false);
|
|
62
|
-
const file = await fs.open(target, priorFile ?
|
|
77
|
+
const file = await fs.open(target, priorFile ? openExisting : openExclusive, 0o600).catch(async (cause) => {
|
|
63
78
|
if (!priorRecord && cause.code === "EEXIST") {
|
|
64
|
-
|
|
79
|
+
verifyRoot();
|
|
65
80
|
assertCurrent(markerPath, markerStat);
|
|
66
81
|
unlinkSync(markerPath);
|
|
67
|
-
await
|
|
82
|
+
await flushDirectory();
|
|
68
83
|
}
|
|
69
84
|
throw cause;
|
|
70
85
|
});
|
|
71
86
|
try {
|
|
72
|
-
const checkTargetRoot = priorFile ? checkRoot :
|
|
87
|
+
const checkTargetRoot = priorFile ? checkRoot : verifyRoot;
|
|
73
88
|
const fileStat = await flushAndVerify(file, target, path.join(root, DEFAULT_IGNORE_FILENAME), starter, () => {
|
|
74
89
|
checkTargetRoot();
|
|
75
90
|
assertCurrent(markerPath, markerStat);
|
|
76
91
|
}, priorFile);
|
|
77
|
-
await
|
|
92
|
+
await flushDirectory();
|
|
78
93
|
checkTargetRoot();
|
|
79
94
|
assertCurrent(target, fileStat);
|
|
80
95
|
assertCurrent(markerPath, markerStat);
|
|
81
96
|
unlinkSync(markerPath);
|
|
82
|
-
await
|
|
97
|
+
await flushDirectory();
|
|
83
98
|
checkRoot();
|
|
84
99
|
const source = await readIgnoreSource(root, DEFAULT_IGNORE_FILENAME, signal);
|
|
85
100
|
checkRoot();
|
|
@@ -97,12 +112,14 @@ export async function prepareDefaultIgnoreSource(root, signal) {
|
|
|
97
112
|
}
|
|
98
113
|
}
|
|
99
114
|
finally {
|
|
100
|
-
await directory
|
|
115
|
+
await directory?.close();
|
|
101
116
|
signal?.throwIfAborted();
|
|
102
117
|
}
|
|
103
118
|
}
|
|
104
119
|
catch (cause) {
|
|
105
120
|
signal?.throwIfAborted();
|
|
121
|
+
if (cause instanceof AutomaticIgnoreCreationUnavailable)
|
|
122
|
+
throw cause;
|
|
106
123
|
const code = cause.code;
|
|
107
124
|
const detail = code === "EEXIST"
|
|
108
125
|
? "a file or creation record appeared concurrently; review the current files and retry"
|
|
@@ -112,6 +129,56 @@ export async function prepareDefaultIgnoreSource(root, signal) {
|
|
|
112
129
|
throw new Error(`Ignore file "${DEFAULT_IGNORE_FILENAME}" initialization: ${detail}.`, { cause });
|
|
113
130
|
}
|
|
114
131
|
}
|
|
132
|
+
function checkCreationError(error) {
|
|
133
|
+
if (["EACCES", "EPERM", "EROFS", "ENOTSUP", "EOPNOTSUPP"].includes(error.code ?? "")) {
|
|
134
|
+
throw new AutomaticIgnoreCreationUnavailable("the filesystem does not permit automatic creation", { cause: error });
|
|
135
|
+
}
|
|
136
|
+
}
|
|
137
|
+
async function checkCreationAccess(root) {
|
|
138
|
+
try {
|
|
139
|
+
await fs.access(root, constants.W_OK);
|
|
140
|
+
}
|
|
141
|
+
catch (error) {
|
|
142
|
+
checkCreationError(error);
|
|
143
|
+
throw error;
|
|
144
|
+
}
|
|
145
|
+
}
|
|
146
|
+
function assertRoot(root, expected) {
|
|
147
|
+
const current = lstatSync(root, { bigint: true });
|
|
148
|
+
if (!current.isDirectory() || !sameIdentity(expected, current) || realpathSync(root) !== root) {
|
|
149
|
+
throw new Error("the Pi agent directory is not canonical or changed during initialization");
|
|
150
|
+
}
|
|
151
|
+
}
|
|
152
|
+
async function openCreationDirectory(root, expected) {
|
|
153
|
+
if (process.platform === "win32")
|
|
154
|
+
return { anchor: root };
|
|
155
|
+
const directory = await fs.open(root, openRead | (constants.O_DIRECTORY ?? 0));
|
|
156
|
+
try {
|
|
157
|
+
if (!sameIdentity(expected, await directory.stat({ bigint: true })))
|
|
158
|
+
changed("Pi agent directory");
|
|
159
|
+
return { directory, anchor: (await directoryAnchor(directory, expected)) ?? root };
|
|
160
|
+
}
|
|
161
|
+
catch (error) {
|
|
162
|
+
await directory.close();
|
|
163
|
+
throw error;
|
|
164
|
+
}
|
|
165
|
+
}
|
|
166
|
+
async function syncCreationDirectory(directory) {
|
|
167
|
+
if (!directory)
|
|
168
|
+
return async () => { };
|
|
169
|
+
try {
|
|
170
|
+
await directory.sync();
|
|
171
|
+
return () => directory.sync();
|
|
172
|
+
}
|
|
173
|
+
catch (error) {
|
|
174
|
+
if (["EBADF", "EINVAL", "ENOSYS", "ENOTSUP", "EOPNOTSUPP", "EPERM"].includes(error.code ?? "")) {
|
|
175
|
+
if (process.platform === "darwin")
|
|
176
|
+
return async () => { };
|
|
177
|
+
throw new AutomaticIgnoreCreationUnavailable("the filesystem does not support flushing the Pi agent directory");
|
|
178
|
+
}
|
|
179
|
+
throw error;
|
|
180
|
+
}
|
|
181
|
+
}
|
|
115
182
|
async function directoryAnchor(directory, expected) {
|
|
116
183
|
for (const base of ["/proc/self/fd", "/dev/fd"]) {
|
|
117
184
|
const candidate = `${base}/${directory.fd}`;
|
|
@@ -123,7 +190,6 @@ async function directoryAnchor(directory, expected) {
|
|
|
123
190
|
if (observed?.isDirectory() && sameIdentity(expected, observed))
|
|
124
191
|
return candidate;
|
|
125
192
|
}
|
|
126
|
-
throw new Error("directory-relative creation is unavailable; use an existing ignore file");
|
|
127
193
|
}
|
|
128
194
|
function statAt(target) {
|
|
129
195
|
return lstatSync(target, { bigint: true, throwIfNoEntry: false });
|
|
@@ -140,7 +206,7 @@ function assertOwned(stat, name, privateRecord) {
|
|
|
140
206
|
if (!stat.isFile() ||
|
|
141
207
|
stat.nlink !== 1n ||
|
|
142
208
|
(process.getuid && stat.uid !== BigInt(process.getuid())) ||
|
|
143
|
-
(privateRecord && (stat.mode & 63n) !== 0n)) {
|
|
209
|
+
(privateRecord && process.platform !== "win32" && (stat.mode & 63n) !== 0n)) {
|
|
144
210
|
throw new Error(`unsafe "${name}"; preserve it for manual recovery`);
|
|
145
211
|
}
|
|
146
212
|
}
|