@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
|
@@ -0,0 +1,122 @@
|
|
|
1
|
+
import fs from "node:fs/promises";
|
|
2
|
+
import path from "node:path";
|
|
3
|
+
import { isPathInside } from "../paths.js";
|
|
4
|
+
import { syncDirectory } from "../state/json-file.js";
|
|
5
|
+
import { expandSessionDir } from "./session-paths.js";
|
|
6
|
+
import { sessionStorageRoot } from "./snapshot-paths.js";
|
|
7
|
+
import { fileImage } from "./snapshot-transaction-plan.js";
|
|
8
|
+
function validateOwner(guards) {
|
|
9
|
+
guards.validateMutation?.();
|
|
10
|
+
guards.signal?.throwIfAborted();
|
|
11
|
+
}
|
|
12
|
+
function rootFromSettings(root, bytes) {
|
|
13
|
+
let settings;
|
|
14
|
+
try {
|
|
15
|
+
settings = JSON.parse(new TextDecoder("utf-8", { fatal: true }).decode(bytes));
|
|
16
|
+
}
|
|
17
|
+
catch {
|
|
18
|
+
throw new Error("Invalid settings root-transition evidence.");
|
|
19
|
+
}
|
|
20
|
+
if (!settings || typeof settings !== "object" || Array.isArray(settings))
|
|
21
|
+
throw new Error("Invalid settings root-transition evidence.");
|
|
22
|
+
const value = settings.sessionDir;
|
|
23
|
+
if (value !== undefined && typeof value !== "string")
|
|
24
|
+
throw new Error("Invalid settings root-transition evidence.");
|
|
25
|
+
return path.resolve(sessionStorageRoot(root, value ? expandSessionDir(value) : undefined));
|
|
26
|
+
}
|
|
27
|
+
async function beforeRoot(directory, root, entry, guards) {
|
|
28
|
+
validateOwner(guards);
|
|
29
|
+
if (entry.kind === "missing" && entry.beforeImage === "missing")
|
|
30
|
+
return path.join(root, "sessions");
|
|
31
|
+
if (entry.kind !== "file")
|
|
32
|
+
throw new Error("Root transition requires regular settings preimage evidence.");
|
|
33
|
+
const backup = path.join(directory, "before", entry.backupName);
|
|
34
|
+
const stat = await fs.lstat(backup);
|
|
35
|
+
validateOwner(guards);
|
|
36
|
+
if (!stat.isFile())
|
|
37
|
+
throw new Error("Invalid settings root-transition backup.");
|
|
38
|
+
const bytes = await fs.readFile(backup);
|
|
39
|
+
validateOwner(guards);
|
|
40
|
+
if (fileImage(bytes) !== entry.beforeImage)
|
|
41
|
+
throw new Error("Changed settings root-transition backup.");
|
|
42
|
+
return rootFromSettings(root, bytes);
|
|
43
|
+
}
|
|
44
|
+
export async function prepareSessionRootTransition(directory, root, sessionRoot, entries, plan, guards = {}) {
|
|
45
|
+
const settingsTarget = path.join(root, "settings.json");
|
|
46
|
+
const entry = entries.find((item) => item.target === settingsTarget || item.afterTarget === settingsTarget);
|
|
47
|
+
const write = plan.writes.find((item) => item.target === settingsTarget || item.target === entry?.target);
|
|
48
|
+
if (!entry || !entries.some((item) => item !== entry && isPathInside(sessionRoot, item.target)))
|
|
49
|
+
return undefined;
|
|
50
|
+
const deleted = !write && plan.deletes.includes(entry.target) && entry.afterImage === "missing";
|
|
51
|
+
if (!write && !deleted)
|
|
52
|
+
return undefined;
|
|
53
|
+
const afterRoot = write ? rootFromSettings(root, write.content) : path.join(root, "sessions");
|
|
54
|
+
// An explicit context root is not a settings-driven transition.
|
|
55
|
+
if (afterRoot !== sessionRoot)
|
|
56
|
+
return undefined;
|
|
57
|
+
const previous = await beforeRoot(directory, root, entry, guards);
|
|
58
|
+
if (previous === sessionRoot)
|
|
59
|
+
return undefined;
|
|
60
|
+
if (write && fileImage(write.content) !== entry.afterImage)
|
|
61
|
+
throw new Error("Changed settings transition postimage.");
|
|
62
|
+
if (!write)
|
|
63
|
+
return { beforeRoot: previous, settingsAfterBase64: null };
|
|
64
|
+
validateOwner(guards);
|
|
65
|
+
const handle = await fs.open(path.join(directory, "settings-after"), "wx", 0o600);
|
|
66
|
+
try {
|
|
67
|
+
validateOwner(guards);
|
|
68
|
+
await handle.writeFile(write.content);
|
|
69
|
+
validateOwner(guards);
|
|
70
|
+
await handle.sync();
|
|
71
|
+
validateOwner(guards);
|
|
72
|
+
}
|
|
73
|
+
finally {
|
|
74
|
+
await handle.close();
|
|
75
|
+
}
|
|
76
|
+
await syncDirectory(directory);
|
|
77
|
+
validateOwner(guards);
|
|
78
|
+
return { beforeRoot: previous, settingsAfterFile: true };
|
|
79
|
+
}
|
|
80
|
+
export function validateSessionRootTransition(value) {
|
|
81
|
+
if (!value ||
|
|
82
|
+
typeof value.beforeRoot !== "string" ||
|
|
83
|
+
!path.isAbsolute(value.beforeRoot) ||
|
|
84
|
+
path.resolve(value.beforeRoot) !== value.beforeRoot ||
|
|
85
|
+
(value.settingsAfterFile !== undefined && value.settingsAfterFile !== true) ||
|
|
86
|
+
(value.settingsAfterFile === true
|
|
87
|
+
? value.settingsAfterBase64 !== undefined
|
|
88
|
+
: value.settingsAfterBase64 !== null &&
|
|
89
|
+
(typeof value.settingsAfterBase64 !== "string" ||
|
|
90
|
+
value.settingsAfterBase64.length > 32 * 1024 * 1024 ||
|
|
91
|
+
Buffer.from(value.settingsAfterBase64, "base64").toString("base64") !== value.settingsAfterBase64)))
|
|
92
|
+
throw new Error("Invalid session root-transition evidence.");
|
|
93
|
+
}
|
|
94
|
+
/** Authorization is bound to both settings images, never just an arbitrary pinned root. */
|
|
95
|
+
export async function resolveTransitionSessionRoot(directory, root, sessionRoot, transition, entries, currentRoot, guards = {}) {
|
|
96
|
+
const settingsTarget = path.join(root, "settings.json");
|
|
97
|
+
const entry = entries.find((item) => item.target === settingsTarget || item.afterTarget === settingsTarget);
|
|
98
|
+
if (!entry)
|
|
99
|
+
throw new Error("Missing settings root-transition evidence.");
|
|
100
|
+
let after;
|
|
101
|
+
if (transition.settingsAfterFile) {
|
|
102
|
+
const file = path.join(directory, "settings-after");
|
|
103
|
+
const stat = await fs.lstat(file);
|
|
104
|
+
validateOwner(guards);
|
|
105
|
+
if (!stat.isFile() ||
|
|
106
|
+
stat.isSymbolicLink() ||
|
|
107
|
+
stat.nlink !== 1 ||
|
|
108
|
+
(process.platform !== "win32" && (stat.mode & 0o077) !== 0))
|
|
109
|
+
throw new Error("Unsafe settings root-transition postimage.");
|
|
110
|
+
after = await fs.readFile(file);
|
|
111
|
+
validateOwner(guards);
|
|
112
|
+
}
|
|
113
|
+
else if (transition.settingsAfterBase64 !== null)
|
|
114
|
+
after = Buffer.from(transition.settingsAfterBase64, "base64");
|
|
115
|
+
if ((after ? fileImage(after) : "missing") !== entry.afterImage ||
|
|
116
|
+
(after ? rootFromSettings(root, after) : path.join(root, "sessions")) !== sessionRoot ||
|
|
117
|
+
(await beforeRoot(directory, root, entry, guards)) !== transition.beforeRoot)
|
|
118
|
+
throw new Error("Inconsistent settings root-transition evidence.");
|
|
119
|
+
if (currentRoot !== transition.beforeRoot && currentRoot !== sessionRoot)
|
|
120
|
+
throw new Error("Transaction session root is not owned by this context; preserve evidence for review.");
|
|
121
|
+
return sessionRoot;
|
|
122
|
+
}
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
import fs from "node:fs/promises";
|
|
2
|
+
import path from "node:path";
|
|
3
|
+
import { stateDir } from "../state/state-directory.js";
|
|
4
|
+
export function transactionRoot() {
|
|
5
|
+
return path.join(stateDir(), "transactions");
|
|
6
|
+
}
|
|
7
|
+
export async function pendingTransactionEntries() {
|
|
8
|
+
try {
|
|
9
|
+
return await fs.readdir(transactionRoot(), { withFileTypes: true });
|
|
10
|
+
}
|
|
11
|
+
catch (error) {
|
|
12
|
+
if (error.code === "ENOENT")
|
|
13
|
+
return [];
|
|
14
|
+
throw error;
|
|
15
|
+
}
|
|
16
|
+
}
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
import type { SnapshotApplyPlan } from "./snapshot-types.js";
|
|
2
|
+
export declare function fileImage(content: Buffer): string;
|
|
3
|
+
/** Work follows path depth and emitted evidence, not every target/write pair. */
|
|
4
|
+
export declare function indexTransactionPlan(plan: SnapshotApplyPlan, targets: readonly string[]): {
|
|
5
|
+
writeImages: Map<string, string>;
|
|
6
|
+
postFiles: Map<string, {
|
|
7
|
+
relative: string;
|
|
8
|
+
image: string;
|
|
9
|
+
}[]>;
|
|
10
|
+
deletes: string[];
|
|
11
|
+
deletedWrites: Set<string>;
|
|
12
|
+
};
|
|
@@ -0,0 +1,68 @@
|
|
|
1
|
+
import { createHash } from "node:crypto";
|
|
2
|
+
import path from "node:path";
|
|
3
|
+
import { isPathInside } from "../paths.js";
|
|
4
|
+
export function fileImage(content) {
|
|
5
|
+
return `file:${createHash("sha256").update(content).digest("hex")}`;
|
|
6
|
+
}
|
|
7
|
+
/** Work follows path depth and emitted evidence, not every target/write pair. */
|
|
8
|
+
export function indexTransactionPlan(plan, targets) {
|
|
9
|
+
const targetIndex = indexTargets(targets);
|
|
10
|
+
const writeImages = new Map();
|
|
11
|
+
const postFiles = new Map(targets.map((target) => [target, []]));
|
|
12
|
+
for (const write of plan.writes) {
|
|
13
|
+
const image = fileImage(write.content);
|
|
14
|
+
// Preserve the first exact write and the original descendant order, including duplicates.
|
|
15
|
+
if (!writeImages.has(write.target))
|
|
16
|
+
writeImages.set(write.target, image);
|
|
17
|
+
for (const ancestor of ancestorTargets(write.target, targetIndex, false)) {
|
|
18
|
+
if (ancestor !== write.target && isStrictDescendant(ancestor, write.target)) {
|
|
19
|
+
postFiles.get(ancestor)?.push({ relative: path.relative(ancestor, write.target), image });
|
|
20
|
+
}
|
|
21
|
+
}
|
|
22
|
+
}
|
|
23
|
+
const deleteIndex = indexTargets(plan.deletes);
|
|
24
|
+
const deletes = plan.deletes.filter((target) => {
|
|
25
|
+
for (const ancestor of ancestorTargets(target, deleteIndex, false)) {
|
|
26
|
+
if (ancestor !== target && isStrictDescendant(ancestor, target))
|
|
27
|
+
return false;
|
|
28
|
+
}
|
|
29
|
+
return true;
|
|
30
|
+
});
|
|
31
|
+
const deletedWrites = new Set();
|
|
32
|
+
const outerDeleteIndex = indexTargets(deletes);
|
|
33
|
+
for (const write of plan.writes) {
|
|
34
|
+
for (const ancestor of ancestorTargets(write.target, outerDeleteIndex)) {
|
|
35
|
+
if (ancestor === write.target || isPathInside(ancestor, write.target)) {
|
|
36
|
+
deletedWrites.add(write.target);
|
|
37
|
+
break;
|
|
38
|
+
}
|
|
39
|
+
}
|
|
40
|
+
}
|
|
41
|
+
return { writeImages, postFiles, deletes, deletedWrites };
|
|
42
|
+
}
|
|
43
|
+
function isStrictDescendant(parent, target) {
|
|
44
|
+
return path.relative(path.resolve(parent), path.resolve(target)) !== "" && isPathInside(parent, target);
|
|
45
|
+
}
|
|
46
|
+
function pathKey(target) {
|
|
47
|
+
const resolved = path.resolve(target);
|
|
48
|
+
return process.platform === "win32" ? resolved.toLowerCase() : resolved;
|
|
49
|
+
}
|
|
50
|
+
function indexTargets(targets) {
|
|
51
|
+
const index = new Map();
|
|
52
|
+
for (const target of new Set(targets)) {
|
|
53
|
+
const key = pathKey(target);
|
|
54
|
+
const rows = index.get(key) ?? [];
|
|
55
|
+
rows.push(target);
|
|
56
|
+
index.set(key, rows);
|
|
57
|
+
}
|
|
58
|
+
return index;
|
|
59
|
+
}
|
|
60
|
+
function* ancestorTargets(target, index, includeSelf = true) {
|
|
61
|
+
const resolved = path.resolve(target);
|
|
62
|
+
// Strict-descendant queries skip the same-address bucket, including native case aliases.
|
|
63
|
+
for (let ancestor = includeSelf ? resolved : path.dirname(resolved);; ancestor = path.dirname(ancestor)) {
|
|
64
|
+
yield* index.get(pathKey(ancestor)) ?? [];
|
|
65
|
+
if (path.dirname(ancestor) === ancestor)
|
|
66
|
+
return;
|
|
67
|
+
}
|
|
68
|
+
}
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
import type { SnapshotApplyPlan } from "./snapshot-types.js";
|
|
2
|
+
interface TransactionOptions {
|
|
3
|
+
sessionDir?: string;
|
|
4
|
+
exclude?: readonly string[];
|
|
5
|
+
signal?: AbortSignal;
|
|
6
|
+
validateMutation?: () => void;
|
|
7
|
+
protectedTargets?: readonly string[];
|
|
8
|
+
/** Fresh apply authorization only; recovery remains bound to durable journal images. */
|
|
9
|
+
expectedPreimages?: ReadonlyMap<string, string>;
|
|
10
|
+
resolveConfiguredSessionDir?: boolean;
|
|
11
|
+
}
|
|
12
|
+
export declare function applySnapshotTransaction(plan: SnapshotApplyPlan, options?: TransactionOptions): Promise<void>;
|
|
13
|
+
export declare function recoverSnapshotTransactionsOnStartup(options?: TransactionOptions): Promise<void>;
|
|
14
|
+
export declare function recoverPendingSnapshotTransactions(options?: TransactionOptions): Promise<void>;
|
|
15
|
+
export {};
|