@narumitw/pi-sync-daemon 0.0.0-stage → 0.2.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 +13 -0
- package/LICENSE +21 -0
- package/README.md +89 -2
- package/dist/cli.d.ts +2 -0
- package/dist/cli.js +128 -0
- package/dist/client.d.ts +55 -0
- package/dist/client.js +484 -0
- package/dist/consent.d.ts +24 -0
- package/dist/consent.js +205 -0
- package/dist/document.d.ts +10 -0
- package/dist/document.js +6 -0
- package/dist/endpoint.d.ts +8 -0
- package/dist/endpoint.js +91 -0
- package/dist/engine/backends/backend-factory.d.ts +6 -0
- package/dist/engine/backends/backend-factory.js +16 -0
- package/dist/engine/backends/backend-identity.d.ts +3 -0
- package/dist/engine/backends/backend-identity.js +38 -0
- package/dist/engine/backends/backend-types.d.ts +70 -0
- package/dist/engine/backends/backend-types.js +1 -0
- package/dist/engine/backends/git/git-backend.d.ts +55 -0
- package/dist/engine/backends/git/git-backend.js +700 -0
- package/dist/engine/backends/git/git-config.d.ts +5 -0
- package/dist/engine/backends/git/git-config.js +118 -0
- package/dist/engine/backends/git/git-runner.d.ts +25 -0
- package/dist/engine/backends/git/git-runner.js +232 -0
- package/dist/engine/backends/git/git-storage.d.ts +39 -0
- package/dist/engine/backends/git/git-storage.js +226 -0
- package/dist/engine/backends/s3/s3-backend.d.ts +32 -0
- package/dist/engine/backends/s3/s3-backend.js +436 -0
- package/dist/engine/backends/s3/s3-client.d.ts +36 -0
- package/dist/engine/backends/s3/s3-client.js +352 -0
- package/dist/engine/backends/s3/s3-conditions.d.ts +4 -0
- package/dist/engine/backends/s3/s3-conditions.js +66 -0
- package/dist/engine/backends/snapshot-pointer.d.ts +6 -0
- package/dist/engine/backends/snapshot-pointer.js +22 -0
- package/dist/engine/backends/sync-backend.d.ts +71 -0
- package/dist/engine/backends/sync-backend.js +23 -0
- package/dist/engine/backends/webdav/webdav-backend.d.ts +34 -0
- package/dist/engine/backends/webdav/webdav-backend.js +534 -0
- package/dist/engine/backends/webdav/webdav-client.d.ts +36 -0
- package/dist/engine/backends/webdav/webdav-client.js +326 -0
- package/dist/engine/backends/webdav/webdav-config.d.ts +5 -0
- package/dist/engine/backends/webdav/webdav-config.js +73 -0
- package/dist/engine/commands/command-types.d.ts +31 -0
- package/dist/engine/commands/command-types.js +1 -0
- package/dist/engine/host.d.ts +70 -0
- package/dist/engine/host.js +16 -0
- package/dist/engine/paths.d.ts +9 -0
- package/dist/engine/paths.js +71 -0
- package/dist/engine/settings/config-errors.d.ts +1 -0
- package/dist/engine/settings/config-errors.js +4 -0
- package/dist/engine/settings/config-file.d.ts +33 -0
- package/dist/engine/settings/config-file.js +630 -0
- package/dist/engine/settings/config.d.ts +24 -0
- package/dist/engine/settings/config.js +200 -0
- package/dist/engine/settings/settings-management.d.ts +24 -0
- package/dist/engine/settings/settings-management.js +191 -0
- package/dist/engine/settings/settings-store.d.ts +8 -0
- package/dist/engine/settings/settings-store.js +41 -0
- package/dist/engine/settings/settings-types.d.ts +134 -0
- package/dist/engine/settings/settings-types.js +1 -0
- package/dist/engine/settings/settings-validation.d.ts +15 -0
- package/dist/engine/settings/settings-validation.js +356 -0
- package/dist/engine/snapshot/session-paths.d.ts +12 -0
- package/dist/engine/snapshot/session-paths.js +91 -0
- package/dist/engine/snapshot/snapshot-apply.d.ts +22 -0
- package/dist/engine/snapshot/snapshot-apply.js +262 -0
- package/dist/engine/snapshot/snapshot-case-replacement.d.ts +15 -0
- package/dist/engine/snapshot/snapshot-case-replacement.js +82 -0
- package/dist/engine/snapshot/snapshot-codec.d.ts +6 -0
- package/dist/engine/snapshot/snapshot-codec.js +53 -0
- package/dist/engine/snapshot/snapshot-exclude-safety.d.ts +7 -0
- package/dist/engine/snapshot/snapshot-exclude-safety.js +47 -0
- package/dist/engine/snapshot/snapshot-mutation-queues.d.ts +9 -0
- package/dist/engine/snapshot/snapshot-mutation-queues.js +133 -0
- package/dist/engine/snapshot/snapshot-paths.d.ts +1 -0
- package/dist/engine/snapshot/snapshot-paths.js +11 -0
- package/dist/engine/snapshot/snapshot-root-transition.d.ts +25 -0
- package/dist/engine/snapshot/snapshot-root-transition.js +122 -0
- package/dist/engine/snapshot/snapshot-transaction-directory.d.ts +2 -0
- package/dist/engine/snapshot/snapshot-transaction-directory.js +16 -0
- package/dist/engine/snapshot/snapshot-transaction-plan.d.ts +12 -0
- package/dist/engine/snapshot/snapshot-transaction-plan.js +68 -0
- package/dist/engine/snapshot/snapshot-transaction.d.ts +15 -0
- package/dist/engine/snapshot/snapshot-transaction.js +787 -0
- package/dist/engine/snapshot/snapshot-types.d.ts +48 -0
- package/dist/engine/snapshot/snapshot-types.js +1 -0
- package/dist/engine/snapshot/snapshot.d.ts +30 -0
- package/dist/engine/snapshot/snapshot.js +364 -0
- package/dist/engine/state/json-file.d.ts +6 -0
- package/dist/engine/state/json-file.js +49 -0
- package/dist/engine/state/lock-policy.d.ts +2 -0
- package/dist/engine/state/lock-policy.js +2 -0
- package/dist/engine/state/lock.d.ts +20 -0
- package/dist/engine/state/lock.js +259 -0
- package/dist/engine/state/lockfile-fs.d.ts +13 -0
- package/dist/engine/state/lockfile-fs.js +13 -0
- package/dist/engine/state/merge-baseline-store.d.ts +11 -0
- package/dist/engine/state/merge-baseline-store.js +187 -0
- package/dist/engine/state/mutation-directory-sync.d.ts +5 -0
- package/dist/engine/state/mutation-directory-sync.js +34 -0
- package/dist/engine/state/operation-availability.d.ts +49 -0
- package/dist/engine/state/operation-availability.js +40 -0
- package/dist/engine/state/state-directory.d.ts +14 -0
- package/dist/engine/state/state-directory.js +225 -0
- package/dist/engine/state/state-types.d.ts +27 -0
- package/dist/engine/state/state-types.js +1 -0
- package/dist/engine/state/sync-state-store.d.ts +11 -0
- package/dist/engine/state/sync-state-store.js +54 -0
- package/dist/engine/sync/agent-candidate.d.ts +44 -0
- package/dist/engine/sync/agent-candidate.js +207 -0
- package/dist/engine/sync/agent-plan.d.ts +8 -0
- package/dist/engine/sync/agent-plan.js +84 -0
- package/dist/engine/sync/agent-policy.d.ts +9 -0
- package/dist/engine/sync/agent-policy.js +10 -0
- package/dist/engine/sync/agent-refusal-retry.d.ts +4 -0
- package/dist/engine/sync/agent-refusal-retry.js +39 -0
- package/dist/engine/sync/agent-request.d.ts +14 -0
- package/dist/engine/sync/agent-request.js +13 -0
- package/dist/engine/sync/agent-runner.d.ts +6 -0
- package/dist/engine/sync/agent-runner.js +183 -0
- package/dist/engine/sync/agent-store.d.ts +7 -0
- package/dist/engine/sync/agent-store.js +177 -0
- package/dist/engine/sync/agent-sync.d.ts +4 -0
- package/dist/engine/sync/agent-sync.js +132 -0
- package/dist/engine/sync/apply-review.d.ts +8 -0
- package/dist/engine/sync/apply-review.js +51 -0
- package/dist/engine/sync/conflict-artifact-ownership.d.ts +4 -0
- package/dist/engine/sync/conflict-artifact-ownership.js +53 -0
- package/dist/engine/sync/conflict-artifacts.d.ts +32 -0
- package/dist/engine/sync/conflict-artifacts.js +158 -0
- package/dist/engine/sync/conflict-groups.d.ts +9 -0
- package/dist/engine/sync/conflict-groups.js +76 -0
- package/dist/engine/sync/conflict-preview.d.ts +4 -0
- package/dist/engine/sync/conflict-preview.js +53 -0
- package/dist/engine/sync/conflict-resolution.d.ts +13 -0
- package/dist/engine/sync/conflict-resolution.js +53 -0
- package/dist/engine/sync/conflict-retention.d.ts +5 -0
- package/dist/engine/sync/conflict-retention.js +61 -0
- package/dist/engine/sync/conflict-review.d.ts +4 -0
- package/dist/engine/sync/conflict-review.js +73 -0
- package/dist/engine/sync/content-conflicts.d.ts +10 -0
- package/dist/engine/sync/content-conflicts.js +70 -0
- package/dist/engine/sync/continuous-errors.d.ts +14 -0
- package/dist/engine/sync/continuous-errors.js +66 -0
- package/dist/engine/sync/continuous-lease.d.ts +9 -0
- package/dist/engine/sync/continuous-lease.js +206 -0
- package/dist/engine/sync/continuous-record.d.ts +31 -0
- package/dist/engine/sync/continuous-record.js +199 -0
- package/dist/engine/sync/continuous-storage.d.ts +14 -0
- package/dist/engine/sync/continuous-storage.js +59 -0
- package/dist/engine/sync/continuous-sync.d.ts +15 -0
- package/dist/engine/sync/continuous-sync.js +496 -0
- package/dist/engine/sync/continuous-watch.d.ts +3 -0
- package/dist/engine/sync/continuous-watch.js +65 -0
- package/dist/engine/sync/exclude-policy.d.ts +5 -0
- package/dist/engine/sync/exclude-policy.js +79 -0
- package/dist/engine/sync/field-migration.d.ts +5 -0
- package/dist/engine/sync/field-migration.js +20 -0
- package/dist/engine/sync/file-merge-planner.d.ts +36 -0
- package/dist/engine/sync/file-merge-planner.js +123 -0
- package/dist/engine/sync/initialization.d.ts +12 -0
- package/dist/engine/sync/initialization.js +75 -0
- package/dist/engine/sync/json-document.d.ts +29 -0
- package/dist/engine/sync/json-document.js +172 -0
- package/dist/engine/sync/local-field-policy.d.ts +2 -0
- package/dist/engine/sync/local-field-policy.js +26 -0
- package/dist/engine/sync/local-fields.d.ts +9 -0
- package/dist/engine/sync/local-fields.js +116 -0
- package/dist/engine/sync/merge-apply.d.ts +5 -0
- package/dist/engine/sync/merge-apply.js +386 -0
- package/dist/engine/sync/merge-journal.d.ts +31 -0
- package/dist/engine/sync/merge-journal.js +179 -0
- package/dist/engine/sync/merge-transfer-error.d.ts +3 -0
- package/dist/engine/sync/merge-transfer-error.js +64 -0
- package/dist/engine/sync/merged-sync.d.ts +6 -0
- package/dist/engine/sync/merged-sync.js +618 -0
- package/dist/engine/sync/mutation-owner.d.ts +2 -0
- package/dist/engine/sync/mutation-owner.js +18 -0
- package/dist/engine/sync/package-merge.d.ts +3 -0
- package/dist/engine/sync/package-merge.js +82 -0
- package/dist/engine/sync/partial-progress.d.ts +15 -0
- package/dist/engine/sync/partial-progress.js +39 -0
- package/dist/engine/sync/remote-snapshot.d.ts +28 -0
- package/dist/engine/sync/remote-snapshot.js +62 -0
- package/dist/engine/sync/resolver-stream.d.ts +4 -0
- package/dist/engine/sync/resolver-stream.js +34 -0
- package/dist/engine/sync/session-merge.d.ts +3 -0
- package/dist/engine/sync/session-merge.js +178 -0
- package/dist/engine/sync/session-push.d.ts +5 -0
- package/dist/engine/sync/session-push.js +32 -0
- package/dist/engine/sync/settings-conflicts.d.ts +8 -0
- package/dist/engine/sync/settings-conflicts.js +56 -0
- package/dist/engine/sync/settings-merge.d.ts +13 -0
- package/dist/engine/sync/settings-merge.js +93 -0
- package/dist/engine/sync/signals.d.ts +2 -0
- package/dist/engine/sync/signals.js +8 -0
- package/dist/engine/sync/startup-timings.d.ts +11 -0
- package/dist/engine/sync/startup-timings.js +44 -0
- package/dist/engine/sync/sync-decision.d.ts +35 -0
- package/dist/engine/sync/sync-decision.js +66 -0
- package/dist/engine/sync/sync-error-guidance.d.ts +2 -0
- package/dist/engine/sync/sync-error-guidance.js +55 -0
- package/dist/engine/sync/sync-errors.d.ts +9 -0
- package/dist/engine/sync/sync-errors.js +18 -0
- package/dist/engine/sync/sync-inspection.d.ts +26 -0
- package/dist/engine/sync/sync-inspection.js +40 -0
- package/dist/engine/sync/sync-loaders.d.ts +7 -0
- package/dist/engine/sync/sync-loaders.js +8 -0
- package/dist/engine/sync/sync-local.d.ts +5 -0
- package/dist/engine/sync/sync-local.js +40 -0
- package/dist/engine/sync/sync-mutations.d.ts +30 -0
- package/dist/engine/sync/sync-mutations.js +589 -0
- package/dist/engine/sync/sync-operations.d.ts +2 -0
- package/dist/engine/sync/sync-operations.js +3 -0
- package/dist/engine/sync/sync-policy.d.ts +85 -0
- package/dist/engine/sync/sync-policy.js +356 -0
- package/dist/engine/sync/sync-queries.d.ts +7 -0
- package/dist/engine/sync/sync-queries.js +195 -0
- package/dist/engine/sync/sync-state.d.ts +31 -0
- package/dist/engine/sync/sync-state.js +92 -0
- package/dist/engine/sync/text-merge.d.ts +19 -0
- package/dist/engine/sync/text-merge.js +124 -0
- package/dist/engine/types.d.ts +5 -0
- package/dist/engine/types.js +2 -0
- package/dist/engine/ui/merge-review.d.ts +2 -0
- package/dist/engine/ui/merge-review.js +7 -0
- package/dist/engine/ui/merged-sync-summary.d.ts +25 -0
- package/dist/engine/ui/merged-sync-summary.js +86 -0
- package/dist/engine/ui/sync-format.d.ts +13 -0
- package/dist/engine/ui/sync-format.js +150 -0
- package/dist/engine/ui/sync-status.d.ts +9 -0
- package/dist/engine/ui/sync-status.js +30 -0
- package/dist/engine/ui/terminal-text.d.ts +3 -0
- package/dist/engine/ui/terminal-text.js +9 -0
- package/dist/engine/ui/transfer-changes.d.ts +12 -0
- package/dist/engine/ui/transfer-changes.js +24 -0
- package/dist/engine/ui/transfer-review.d.ts +3 -0
- package/dist/engine/ui/transfer-review.js +19 -0
- package/dist/engine-service.d.ts +11 -0
- package/dist/engine-service.js +315 -0
- package/dist/identity.d.ts +20 -0
- package/dist/identity.js +133 -0
- package/dist/index.d.ts +8 -0
- package/dist/index.js +4 -0
- package/dist/lifecycle-log.d.ts +7 -0
- package/dist/lifecycle-log.js +53 -0
- package/dist/lifecycle.d.ts +2 -0
- package/dist/lifecycle.js +21 -0
- package/dist/model-signature.d.ts +2 -0
- package/dist/model-signature.js +7 -0
- package/dist/monitor.d.ts +11 -0
- package/dist/monitor.js +245 -0
- package/dist/participant-admission.d.ts +5 -0
- package/dist/participant-admission.js +30 -0
- package/dist/protocol.d.ts +97 -0
- package/dist/protocol.js +90 -0
- package/dist/resolver-host.d.ts +11 -0
- package/dist/resolver-host.js +78 -0
- package/dist/server.d.ts +18 -0
- package/dist/server.js +724 -0
- package/dist/service-host.d.ts +7 -0
- package/dist/service-host.js +174 -0
- package/docs/architecture.md +133 -0
- package/package.json +54 -4
package/dist/consent.js
ADDED
|
@@ -0,0 +1,205 @@
|
|
|
1
|
+
import { createHmac, randomBytes } from "node:crypto";
|
|
2
|
+
import { closeSync, fstatSync, fsyncSync, lstatSync, mkdirSync, openSync, readFileSync, renameSync, rmSync, writeFileSync, } from "node:fs";
|
|
3
|
+
import path from "node:path";
|
|
4
|
+
import { syncCheckConfigFingerprint } from "./engine/settings/config.js";
|
|
5
|
+
import { stateDir } from "./engine/state/state-directory.js";
|
|
6
|
+
import { record } from "./protocol.js";
|
|
7
|
+
function file() {
|
|
8
|
+
return path.join(stateDir(), "daemon.json");
|
|
9
|
+
}
|
|
10
|
+
export class ConsentPublicationError extends Error {
|
|
11
|
+
}
|
|
12
|
+
/** A small synchronous publication keeps approval, metadata revocation, and rename in one event-loop turn. */
|
|
13
|
+
function publishConsent(value) {
|
|
14
|
+
try {
|
|
15
|
+
const target = file();
|
|
16
|
+
const content = `${JSON.stringify(value, null, "\t")}\n`;
|
|
17
|
+
if (Buffer.byteLength(content) > 65_536)
|
|
18
|
+
throw new Error("Daemon consent exceeds its storage bound; existing evidence retained.");
|
|
19
|
+
mkdirSync(path.dirname(target), { recursive: true, mode: 0o700 });
|
|
20
|
+
const temporary = `${target}.${process.pid}.${randomBytes(16).toString("hex")}.tmp`;
|
|
21
|
+
const handle = openSync(temporary, "wx", 0o600);
|
|
22
|
+
try {
|
|
23
|
+
try {
|
|
24
|
+
writeFileSync(handle, content);
|
|
25
|
+
fsyncSync(handle);
|
|
26
|
+
}
|
|
27
|
+
finally {
|
|
28
|
+
closeSync(handle);
|
|
29
|
+
}
|
|
30
|
+
renameSync(temporary, target);
|
|
31
|
+
if (process.platform !== "win32") {
|
|
32
|
+
const directory = openSync(path.dirname(target), "r");
|
|
33
|
+
try {
|
|
34
|
+
fsyncSync(directory);
|
|
35
|
+
}
|
|
36
|
+
finally {
|
|
37
|
+
closeSync(directory);
|
|
38
|
+
}
|
|
39
|
+
}
|
|
40
|
+
}
|
|
41
|
+
finally {
|
|
42
|
+
rmSync(temporary, { force: true });
|
|
43
|
+
}
|
|
44
|
+
}
|
|
45
|
+
catch (error) {
|
|
46
|
+
throw new ConsentPublicationError("Daemon consent publication failed; preserve the retained document.", {
|
|
47
|
+
cause: error,
|
|
48
|
+
});
|
|
49
|
+
}
|
|
50
|
+
}
|
|
51
|
+
/** A different interactive binding cannot silently resume an older unattended grant after exit/restart. */
|
|
52
|
+
export function revokeChangedBindings(root, peers) {
|
|
53
|
+
const consent = readConsent(root);
|
|
54
|
+
if (!consent)
|
|
55
|
+
return;
|
|
56
|
+
const interactive = peers.filter((peer) => peer.hasUI);
|
|
57
|
+
let changed = false;
|
|
58
|
+
for (const saved of Object.values(consent.setups)) {
|
|
59
|
+
if (saved.afterExit &&
|
|
60
|
+
interactive.some((peer) => JSON.stringify(peer.model) !== JSON.stringify(saved.model) ||
|
|
61
|
+
peer.sessionDir !== saved.sessionDir ||
|
|
62
|
+
peer.cwd !== saved.cwd)) {
|
|
63
|
+
saved.afterExit = false;
|
|
64
|
+
changed = true;
|
|
65
|
+
}
|
|
66
|
+
}
|
|
67
|
+
if (changed)
|
|
68
|
+
publishConsent(consent);
|
|
69
|
+
}
|
|
70
|
+
export function readConsent(root) {
|
|
71
|
+
const target = file();
|
|
72
|
+
let handle;
|
|
73
|
+
try {
|
|
74
|
+
const before = lstatSync(target);
|
|
75
|
+
if (!before.isFile() ||
|
|
76
|
+
before.isSymbolicLink() ||
|
|
77
|
+
before.size > 65_536 ||
|
|
78
|
+
(process.getuid && before.uid !== process.getuid()) ||
|
|
79
|
+
(process.platform !== "win32" && before.mode & 0o077))
|
|
80
|
+
throw new Error("Unsafe daemon consent document; evidence retained.");
|
|
81
|
+
handle = openSync(target, "r");
|
|
82
|
+
const opened = fstatSync(handle);
|
|
83
|
+
if (opened.dev !== before.dev || opened.ino !== before.ino)
|
|
84
|
+
throw new Error("Daemon consent changed while opening.");
|
|
85
|
+
const value = JSON.parse(readFileSync(handle, "utf8"));
|
|
86
|
+
if (!record(value) ||
|
|
87
|
+
value.version !== 1 ||
|
|
88
|
+
value.root !== root ||
|
|
89
|
+
value.handover !== true ||
|
|
90
|
+
typeof value.nonce !== "string" ||
|
|
91
|
+
!/^[a-f0-9]{64}$/u.test(value.nonce) ||
|
|
92
|
+
!record(value.setups))
|
|
93
|
+
throw new Error("Invalid daemon consent; evidence retained.");
|
|
94
|
+
for (const entry of Object.values(value.setups)) {
|
|
95
|
+
if (!record(entry) ||
|
|
96
|
+
typeof entry.fingerprint !== "string" ||
|
|
97
|
+
!/^[a-f0-9]{64}$/u.test(entry.fingerprint) ||
|
|
98
|
+
typeof entry.afterExit !== "boolean" ||
|
|
99
|
+
(entry.cwd !== undefined &&
|
|
100
|
+
(typeof entry.cwd !== "string" || !path.isAbsolute(entry.cwd) || entry.cwd.includes("\0"))) ||
|
|
101
|
+
(entry.sessionDir !== undefined &&
|
|
102
|
+
(typeof entry.sessionDir !== "string" || !path.isAbsolute(entry.sessionDir))) ||
|
|
103
|
+
(entry.model !== undefined &&
|
|
104
|
+
(!record(entry.model) ||
|
|
105
|
+
typeof entry.model.provider !== "string" ||
|
|
106
|
+
typeof entry.model.id !== "string" ||
|
|
107
|
+
typeof entry.model.signature !== "string" ||
|
|
108
|
+
!/^[a-f0-9]{64}$/u.test(entry.model.signature) ||
|
|
109
|
+
!["stored", "environment", "models_json_key", "models_json_command"].includes(String(entry.model.authSource)))))
|
|
110
|
+
throw new Error("Invalid daemon setup consent.");
|
|
111
|
+
}
|
|
112
|
+
return value;
|
|
113
|
+
}
|
|
114
|
+
catch (error) {
|
|
115
|
+
if (error.code === "ENOENT")
|
|
116
|
+
return undefined;
|
|
117
|
+
throw error;
|
|
118
|
+
}
|
|
119
|
+
finally {
|
|
120
|
+
if (handle !== undefined)
|
|
121
|
+
closeSync(handle);
|
|
122
|
+
}
|
|
123
|
+
}
|
|
124
|
+
export function consentFingerprint(root, config) {
|
|
125
|
+
const consent = readConsent(root);
|
|
126
|
+
if (!consent)
|
|
127
|
+
throw new Error("Review daemon handover first.");
|
|
128
|
+
// Keep the original digest domain across package renames; never persist the internal freshness token.
|
|
129
|
+
return createHmac("sha256", consent.nonce)
|
|
130
|
+
.update("sync-daemon-consent/v1")
|
|
131
|
+
.update(syncCheckConfigFingerprint(config))
|
|
132
|
+
.digest("hex");
|
|
133
|
+
}
|
|
134
|
+
export async function requireHandover(execution) {
|
|
135
|
+
if (readConsent(execution.root))
|
|
136
|
+
return;
|
|
137
|
+
const accepted = await execution.request("confirm", {
|
|
138
|
+
title: "Adopt pi-sync-daemon as the synchronization owner?",
|
|
139
|
+
message: "Close or upgrade every older Pi process before continuing. An idle older Pi cannot be detected from an absent sync lock. The daemon will own transfers; Pi keeps presentation and live file queues. Autonomous resolution uses independently available auth.json/models.json configuration and the environment captured when the service starts, not Pi runtime accounts or providers. Credential commands run non-interactively from the canonical agent root. Review this independent provider/account configuration; restart the service after changing its environment. This does not authorize transfers or model requests after the last Pi exits.",
|
|
140
|
+
});
|
|
141
|
+
execution.validate();
|
|
142
|
+
if (accepted !== true)
|
|
143
|
+
throw new Error("Daemon handover was not approved; no transfer was started.");
|
|
144
|
+
const { withLock } = await import("./engine/state/lock.js");
|
|
145
|
+
await withLock("daemon-handover", async () => {
|
|
146
|
+
execution.validate();
|
|
147
|
+
if (!readConsent(execution.root))
|
|
148
|
+
publishConsent({
|
|
149
|
+
version: 1,
|
|
150
|
+
root: execution.root,
|
|
151
|
+
handover: true,
|
|
152
|
+
nonce: randomBytes(32).toString("hex"),
|
|
153
|
+
setups: {},
|
|
154
|
+
});
|
|
155
|
+
});
|
|
156
|
+
}
|
|
157
|
+
export async function authorizeAfterExit(execution, payload) {
|
|
158
|
+
if (!record(payload) || typeof payload.afterExit !== "boolean" || typeof payload.fingerprint !== "string")
|
|
159
|
+
throw new Error("Invalid daemon authorization.");
|
|
160
|
+
await requireHandover(execution);
|
|
161
|
+
const { loadConfig } = await import("./engine/settings/config.js");
|
|
162
|
+
const config = await loadConfig();
|
|
163
|
+
const facts = execution.facts();
|
|
164
|
+
const binding = JSON.stringify({ model: facts?.model, sessionDir: facts?.sessionDir, cwd: facts?.cwd });
|
|
165
|
+
const fingerprint = consentFingerprint(execution.root, config);
|
|
166
|
+
const policyGeneration = JSON.stringify(readConsent(execution.root));
|
|
167
|
+
if (fingerprint !== payload.fingerprint)
|
|
168
|
+
throw new Error("Setup changed; reopen the daemon authorization review.");
|
|
169
|
+
if (payload.afterExit && (!config.continuous || !config.agentMode))
|
|
170
|
+
throw new Error("Enable the reviewed Autonomous and Continuous policies first.");
|
|
171
|
+
const accepted = await execution.request("confirm", {
|
|
172
|
+
title: payload.afterExit ? "Allow continuous sync after all Pi clients exit?" : "Revoke unattended sync?",
|
|
173
|
+
message: `Setup: ${config.setupName}\nSelected content: ${config.include.join(", ")}\nModel: ${facts?.model?.provider ?? "unavailable"}/${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." : "The daemon may remain alive, but no new transfer or model request may start with zero clients."}`,
|
|
174
|
+
});
|
|
175
|
+
execution.validate();
|
|
176
|
+
if (accepted !== true)
|
|
177
|
+
return false;
|
|
178
|
+
const latest = await loadConfig();
|
|
179
|
+
execution.validate();
|
|
180
|
+
if (consentFingerprint(execution.root, latest) !== fingerprint ||
|
|
181
|
+
JSON.stringify({
|
|
182
|
+
model: execution.facts()?.model,
|
|
183
|
+
sessionDir: execution.facts()?.sessionDir,
|
|
184
|
+
cwd: execution.facts()?.cwd,
|
|
185
|
+
}) !== binding)
|
|
186
|
+
throw new Error("Setup or model changed during daemon authorization.");
|
|
187
|
+
const consent = readConsent(execution.root);
|
|
188
|
+
if (!consent || JSON.stringify(consent) !== policyGeneration)
|
|
189
|
+
throw new Error("Daemon authorization changed; reopen its review.");
|
|
190
|
+
publishConsent({
|
|
191
|
+
...consent,
|
|
192
|
+
setups: {
|
|
193
|
+
...consent.setups,
|
|
194
|
+
[config.setupName]: {
|
|
195
|
+
...consent.setups[config.setupName],
|
|
196
|
+
fingerprint,
|
|
197
|
+
model: facts?.model,
|
|
198
|
+
sessionDir: facts?.sessionDir,
|
|
199
|
+
cwd: facts?.cwd,
|
|
200
|
+
afterExit: payload.afterExit,
|
|
201
|
+
},
|
|
202
|
+
},
|
|
203
|
+
});
|
|
204
|
+
return true;
|
|
205
|
+
}
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
export interface DocumentView {
|
|
2
|
+
path: string;
|
|
3
|
+
bytes: string;
|
|
4
|
+
identity: {
|
|
5
|
+
dev: number;
|
|
6
|
+
ino: number;
|
|
7
|
+
};
|
|
8
|
+
parsed: Record<string, unknown>;
|
|
9
|
+
}
|
|
10
|
+
export declare function documentFingerprint(document: Pick<DocumentView, "bytes" | "identity"> | undefined): string | null;
|
package/dist/document.js
ADDED
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
import type { Server } from "node:http";
|
|
2
|
+
import { type DaemonPaths } from "./identity.js";
|
|
3
|
+
/** Node/libuv unlinks its original socket pathname, even if another file replaced it. */
|
|
4
|
+
export declare function endpointOwner(server: Server, paths: DaemonPaths): {
|
|
5
|
+
listen(): Promise<void>;
|
|
6
|
+
close(): Promise<void>;
|
|
7
|
+
readonly retainedDirectory: string | undefined;
|
|
8
|
+
};
|
package/dist/endpoint.js
ADDED
|
@@ -0,0 +1,91 @@
|
|
|
1
|
+
import fs from "node:fs/promises";
|
|
2
|
+
import path from "node:path";
|
|
3
|
+
import { privateDirectory } from "./identity.js";
|
|
4
|
+
/** Node/libuv unlinks its original socket pathname, even if another file replaced it. */
|
|
5
|
+
export function endpointOwner(server, paths) {
|
|
6
|
+
let created = false;
|
|
7
|
+
let retired = false;
|
|
8
|
+
let retained;
|
|
9
|
+
let directoryIdentity;
|
|
10
|
+
let socketIdentity;
|
|
11
|
+
async function retire() {
|
|
12
|
+
if (retired || !created || !paths.endpointDirectory)
|
|
13
|
+
return;
|
|
14
|
+
const namespace = path.dirname(paths.directory);
|
|
15
|
+
await privateDirectory(namespace);
|
|
16
|
+
const container = await fs.mkdtemp(path.join(namespace, `${path.basename(paths.directory).slice(0, 16)}-retained-`));
|
|
17
|
+
try {
|
|
18
|
+
// Move the entry itself, including any replacement symlink, without following it.
|
|
19
|
+
// Do this only after all jobs and release acknowledgements have drained.
|
|
20
|
+
await fs.rename(paths.endpointDirectory, path.join(container, "entry"));
|
|
21
|
+
retained = container;
|
|
22
|
+
retired = true;
|
|
23
|
+
}
|
|
24
|
+
catch (error) {
|
|
25
|
+
await fs.rmdir(container).catch(() => { });
|
|
26
|
+
if (error.code === "ENOENT") {
|
|
27
|
+
retired = true;
|
|
28
|
+
return;
|
|
29
|
+
}
|
|
30
|
+
// Closing here would let Node delete an unknown replacement. Keep the owner
|
|
31
|
+
// alive and permit an explicit stop retry after runtime permissions are fixed.
|
|
32
|
+
throw new Error("Cannot retire the IPC namespace safely. Restore runtime directory permissions, then stop again; evidence retained.", { cause: error });
|
|
33
|
+
}
|
|
34
|
+
}
|
|
35
|
+
async function removeKnownRetiredSocket() {
|
|
36
|
+
if (!retained || !directoryIdentity)
|
|
37
|
+
return;
|
|
38
|
+
const entry = path.join(retained, "entry");
|
|
39
|
+
const directory = await fs.lstat(entry).catch(() => undefined);
|
|
40
|
+
if (!directory?.isDirectory() ||
|
|
41
|
+
directory.isSymbolicLink() ||
|
|
42
|
+
directory.dev !== directoryIdentity.dev ||
|
|
43
|
+
directory.ino !== directoryIdentity.ino)
|
|
44
|
+
return;
|
|
45
|
+
const target = path.join(entry, "api.sock");
|
|
46
|
+
const socket = await fs.lstat(target).catch(() => undefined);
|
|
47
|
+
if (socket?.isSocket() && socketIdentity && socket.dev === socketIdentity.dev && socket.ino === socketIdentity.ino)
|
|
48
|
+
await fs.unlink(target);
|
|
49
|
+
// Never recursively remove unknown entries or follow a replacement ancestor.
|
|
50
|
+
try {
|
|
51
|
+
await fs.rmdir(entry);
|
|
52
|
+
await fs.rmdir(retained);
|
|
53
|
+
retained = undefined;
|
|
54
|
+
}
|
|
55
|
+
catch (error) {
|
|
56
|
+
if (!["ENOTEMPTY", "EEXIST"].includes(String(error.code)))
|
|
57
|
+
throw error;
|
|
58
|
+
}
|
|
59
|
+
}
|
|
60
|
+
return {
|
|
61
|
+
async listen() {
|
|
62
|
+
if (paths.endpointDirectory) {
|
|
63
|
+
await fs.mkdir(paths.endpointDirectory, { mode: 0o700 });
|
|
64
|
+
created = true;
|
|
65
|
+
directoryIdentity = await fs.lstat(paths.endpointDirectory);
|
|
66
|
+
}
|
|
67
|
+
await new Promise((resolve, reject) => {
|
|
68
|
+
server.once("error", reject);
|
|
69
|
+
server.listen(paths.endpoint, resolve);
|
|
70
|
+
});
|
|
71
|
+
if (paths.endpointDirectory) {
|
|
72
|
+
const socket = await fs.lstat(paths.endpoint);
|
|
73
|
+
if (!socket.isSocket() || socket.isSymbolicLink())
|
|
74
|
+
throw new Error("IPC endpoint changed during bind; evidence retained.");
|
|
75
|
+
socketIdentity = socket;
|
|
76
|
+
// The exclusive 0700 parent is the access boundary. A pathname chmod after
|
|
77
|
+
// bind could instead follow an externally substituted symlink.
|
|
78
|
+
}
|
|
79
|
+
},
|
|
80
|
+
async close() {
|
|
81
|
+
await retire();
|
|
82
|
+
const closed = new Promise((resolve) => server.close(() => resolve()));
|
|
83
|
+
server.closeAllConnections();
|
|
84
|
+
await closed;
|
|
85
|
+
await removeKnownRetiredSocket();
|
|
86
|
+
},
|
|
87
|
+
get retainedDirectory() {
|
|
88
|
+
return retained;
|
|
89
|
+
},
|
|
90
|
+
};
|
|
91
|
+
}
|
|
@@ -0,0 +1,6 @@
|
|
|
1
|
+
import type { AnySyncConfig } from "../settings/settings-types.js";
|
|
2
|
+
import type { SyncBackend } from "./sync-backend.js";
|
|
3
|
+
export type SyncBackendFactory = {
|
|
4
|
+
bivarianceHack(config: AnySyncConfig): SyncBackend | Promise<SyncBackend>;
|
|
5
|
+
}["bivarianceHack"];
|
|
6
|
+
export declare const createSyncBackend: SyncBackendFactory;
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
export const createSyncBackend = async (config) => {
|
|
2
|
+
switch (config.backend.type) {
|
|
3
|
+
case "s3": {
|
|
4
|
+
const { S3SyncBackend } = await import("./s3/s3-backend.js");
|
|
5
|
+
return new S3SyncBackend(config.backend);
|
|
6
|
+
}
|
|
7
|
+
case "webdav": {
|
|
8
|
+
const { WebDavSyncBackend } = await import("./webdav/webdav-backend.js");
|
|
9
|
+
return new WebDavSyncBackend(config.backend);
|
|
10
|
+
}
|
|
11
|
+
case "git": {
|
|
12
|
+
const { GitSyncBackend } = await import("./git/git-backend.js");
|
|
13
|
+
return new GitSyncBackend(config.backend);
|
|
14
|
+
}
|
|
15
|
+
}
|
|
16
|
+
};
|
|
@@ -0,0 +1,38 @@
|
|
|
1
|
+
import { normalizeGitRemoteIdentity } from "./git/git-config.js";
|
|
2
|
+
import { normalizeWebDavIdentityUrl } from "./webdav/webdav-config.js";
|
|
3
|
+
export function backendIdentityCoordinates(config) {
|
|
4
|
+
switch (config.backend.type) {
|
|
5
|
+
case "s3":
|
|
6
|
+
return JSON.stringify([
|
|
7
|
+
"s3",
|
|
8
|
+
normalizeEndpointIdentity(config.backend.profile.endpoint),
|
|
9
|
+
config.backend.destination.bucket,
|
|
10
|
+
config.storagePath,
|
|
11
|
+
]);
|
|
12
|
+
case "git":
|
|
13
|
+
return JSON.stringify([
|
|
14
|
+
"git",
|
|
15
|
+
normalizeGitRemoteIdentity(config.backend.profile.remote),
|
|
16
|
+
config.backend.destination.branch,
|
|
17
|
+
config.storagePath,
|
|
18
|
+
]);
|
|
19
|
+
case "webdav":
|
|
20
|
+
return JSON.stringify([
|
|
21
|
+
"webdav",
|
|
22
|
+
normalizeWebDavIdentityUrl(config.backend.profile.url),
|
|
23
|
+
config.backend.profile.username,
|
|
24
|
+
config.storagePath,
|
|
25
|
+
]);
|
|
26
|
+
}
|
|
27
|
+
}
|
|
28
|
+
export function normalizeEndpointIdentity(endpoint) {
|
|
29
|
+
try {
|
|
30
|
+
const url = new URL(endpoint.trim());
|
|
31
|
+
url.hostname = url.hostname.toLowerCase();
|
|
32
|
+
url.pathname = url.pathname.replace(/\/+$/gu, "");
|
|
33
|
+
return url.toString().replace(/\/$/u, "");
|
|
34
|
+
}
|
|
35
|
+
catch {
|
|
36
|
+
return endpoint.trim();
|
|
37
|
+
}
|
|
38
|
+
}
|
|
@@ -0,0 +1,70 @@
|
|
|
1
|
+
import type { SnapshotSelection } from "../snapshot/snapshot-types.js";
|
|
2
|
+
/** Backend-only resolved S3 connection fields. */
|
|
3
|
+
export interface ResolvedS3StorageProfile {
|
|
4
|
+
kind: "r2" | "s3-compatible";
|
|
5
|
+
endpoint: string;
|
|
6
|
+
region: string;
|
|
7
|
+
accessKeyId: string;
|
|
8
|
+
secretAccessKey: string;
|
|
9
|
+
sessionToken?: string;
|
|
10
|
+
}
|
|
11
|
+
/** Backend-only coordinates. `prefix` is the complete reviewed v3 storage path. */
|
|
12
|
+
export interface ResolvedS3Destination {
|
|
13
|
+
bucket: string;
|
|
14
|
+
prefix: string;
|
|
15
|
+
/** Snapshot/wire identity; never derived from the local setup name. */
|
|
16
|
+
namespace: string;
|
|
17
|
+
}
|
|
18
|
+
export interface ResolvedS3Backend {
|
|
19
|
+
type: "s3";
|
|
20
|
+
profile: ResolvedS3StorageProfile;
|
|
21
|
+
destination: ResolvedS3Destination;
|
|
22
|
+
}
|
|
23
|
+
export interface ResolvedWebDavStorageProfile {
|
|
24
|
+
kind: "webdav";
|
|
25
|
+
url: string;
|
|
26
|
+
username: string;
|
|
27
|
+
password: string;
|
|
28
|
+
}
|
|
29
|
+
/** Backend-only coordinates. `path` is the complete reviewed v3 storage path. */
|
|
30
|
+
export interface ResolvedWebDavDestination {
|
|
31
|
+
path: string;
|
|
32
|
+
namespace: string;
|
|
33
|
+
}
|
|
34
|
+
export interface ResolvedWebDavBackend {
|
|
35
|
+
type: "webdav";
|
|
36
|
+
profile: ResolvedWebDavStorageProfile;
|
|
37
|
+
destination: ResolvedWebDavDestination;
|
|
38
|
+
}
|
|
39
|
+
export interface ResolvedGitStorageProfile {
|
|
40
|
+
kind: "git";
|
|
41
|
+
remote: string;
|
|
42
|
+
}
|
|
43
|
+
/** Backend-only coordinates. `directory` is the complete reviewed v3 storage path. */
|
|
44
|
+
export interface ResolvedGitDestination {
|
|
45
|
+
branch: string;
|
|
46
|
+
directory: string;
|
|
47
|
+
namespace: string;
|
|
48
|
+
}
|
|
49
|
+
export interface ResolvedGitBackend {
|
|
50
|
+
type: "git";
|
|
51
|
+
profile: ResolvedGitStorageProfile;
|
|
52
|
+
destination: ResolvedGitDestination;
|
|
53
|
+
}
|
|
54
|
+
export type ResolvedSyncBackend = ResolvedS3Backend | ResolvedWebDavBackend | ResolvedGitBackend;
|
|
55
|
+
export interface LatestPointer {
|
|
56
|
+
version: number;
|
|
57
|
+
profile: string;
|
|
58
|
+
snapshot: string;
|
|
59
|
+
sha256: string;
|
|
60
|
+
createdAt: string;
|
|
61
|
+
machine: string;
|
|
62
|
+
syncSessions?: boolean;
|
|
63
|
+
/** Lightweight projection; the immutable snapshot remains authoritative. */
|
|
64
|
+
selection?: SnapshotSelection;
|
|
65
|
+
}
|
|
66
|
+
export interface RemoteObject<T> {
|
|
67
|
+
value?: T;
|
|
68
|
+
etag?: string;
|
|
69
|
+
missing: boolean;
|
|
70
|
+
}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|
|
@@ -0,0 +1,55 @@
|
|
|
1
|
+
import type { Snapshot } from "../../snapshot/snapshot-types.js";
|
|
2
|
+
import type { ResolvedGitBackend } from "../backend-types.js";
|
|
3
|
+
import { type BackendDiagnostic, type ExpectedRemoteHead, type PublishSnapshotOptions, type PublishSnapshotResult, type RemoteHead, type RemoteHistoryEntry, type SyncBackend } from "../sync-backend.js";
|
|
4
|
+
export interface GitBackendOptions {
|
|
5
|
+
cacheRoot?: string;
|
|
6
|
+
allowLocalRemotes?: boolean;
|
|
7
|
+
commandTimeoutMs?: number;
|
|
8
|
+
postCommitTimeoutMs?: number;
|
|
9
|
+
/** Deterministic fault injection used only by the local backend test suite. */
|
|
10
|
+
afterPushForTest?: () => void | Promise<void>;
|
|
11
|
+
afterLsRemoteForTest?: () => void | Promise<void>;
|
|
12
|
+
afterPayloadWriteForTest?: () => void | Promise<void>;
|
|
13
|
+
}
|
|
14
|
+
export declare class GitSyncBackend implements SyncBackend {
|
|
15
|
+
private readonly config;
|
|
16
|
+
readonly identity: string;
|
|
17
|
+
readonly destination: string;
|
|
18
|
+
readonly capability: "lease-protected";
|
|
19
|
+
private readonly cacheRoot;
|
|
20
|
+
private readonly cacheDir;
|
|
21
|
+
private readonly allowLocalRemotes;
|
|
22
|
+
private readonly commandTimeoutMs;
|
|
23
|
+
private readonly postCommitTimeoutMs;
|
|
24
|
+
private readonly afterPushForTest?;
|
|
25
|
+
private readonly afterLsRemoteForTest?;
|
|
26
|
+
private readonly afterPayloadWriteForTest?;
|
|
27
|
+
private cacheReady?;
|
|
28
|
+
constructor(config: ResolvedGitBackend, options?: GitBackendOptions);
|
|
29
|
+
sameRevision(left: string, right: string): boolean;
|
|
30
|
+
readHead(signal?: AbortSignal): Promise<RemoteHead | undefined>;
|
|
31
|
+
readSnapshot(reference: string, signal?: AbortSignal): Promise<Snapshot>;
|
|
32
|
+
publishSnapshot(snapshot: Snapshot, expected: ExpectedRemoteHead, options?: PublishSnapshotOptions): Promise<PublishSnapshotResult>;
|
|
33
|
+
listHistory(signal?: AbortSignal): Promise<RemoteHistoryEntry[]>;
|
|
34
|
+
diagnose(signal?: AbortSignal): Promise<BackendDiagnostic[]>;
|
|
35
|
+
private resolveSnapshotReference;
|
|
36
|
+
private headForSha;
|
|
37
|
+
private fetchRemoteHead;
|
|
38
|
+
private readManifest;
|
|
39
|
+
private showFile;
|
|
40
|
+
private createCommit;
|
|
41
|
+
private ensureCache;
|
|
42
|
+
private initializeCache;
|
|
43
|
+
private cacheUsesSha1;
|
|
44
|
+
private git;
|
|
45
|
+
private remoteRef;
|
|
46
|
+
private publicationPath;
|
|
47
|
+
private manifestPath;
|
|
48
|
+
private filePath;
|
|
49
|
+
private readPublication;
|
|
50
|
+
private readPublicationTree;
|
|
51
|
+
private redactedError;
|
|
52
|
+
private safeError;
|
|
53
|
+
}
|
|
54
|
+
export declare function gitBackendIdentity(config: ResolvedGitBackend): string;
|
|
55
|
+
export declare function isSupportedGitVersion(value: string): boolean;
|