@narumitw/pi-sync-daemon 0.6.1 → 0.7.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 +15 -0
- package/README.md +3 -1
- package/dist/consent.js +11 -1
- package/dist/engine/backends/git/git-storage.js +11 -6
- package/dist/engine/paths.js +1 -1
- package/dist/engine/settings/config-file.js +2 -0
- package/dist/engine/settings/config.d.ts +2 -4
- package/dist/engine/settings/config.js +22 -7
- package/dist/engine/settings/settings-management.d.ts +2 -2
- package/dist/engine/settings/settings-management.js +27 -33
- package/dist/engine/settings/settings-types.d.ts +18 -4
- package/dist/engine/settings/settings-validation.js +34 -24
- package/dist/engine/snapshot/session-paths.d.ts +5 -3
- package/dist/engine/snapshot/session-paths.js +55 -9
- package/dist/engine/snapshot/snapshot-apply.d.ts +6 -7
- package/dist/engine/snapshot/snapshot-apply.js +88 -21
- package/dist/engine/snapshot/snapshot-bounds.d.ts +4 -0
- package/dist/engine/snapshot/snapshot-bounds.js +20 -0
- package/dist/engine/snapshot/snapshot-codec.js +3 -4
- package/dist/engine/snapshot/snapshot-entry-projection.d.ts +10 -0
- package/dist/engine/snapshot/snapshot-entry-projection.js +78 -0
- package/dist/engine/snapshot/snapshot-exclude-safety.d.ts +7 -2
- package/dist/engine/snapshot/snapshot-exclude-safety.js +180 -22
- package/dist/engine/snapshot/snapshot-payload.d.ts +2 -0
- package/dist/engine/snapshot/snapshot-payload.js +10 -0
- package/dist/engine/snapshot/snapshot-reviewed-preimages.d.ts +2 -0
- package/dist/engine/snapshot/snapshot-reviewed-preimages.js +60 -0
- package/dist/engine/snapshot/snapshot-root-transition.d.ts +4 -0
- package/dist/engine/snapshot/snapshot-root-transition.js +16 -0
- package/dist/engine/snapshot/snapshot-transaction-images.d.ts +37 -0
- package/dist/engine/snapshot/snapshot-transaction-images.js +357 -0
- package/dist/engine/snapshot/snapshot-transaction-policy.d.ts +50 -0
- package/dist/engine/snapshot/snapshot-transaction-policy.js +172 -0
- package/dist/engine/snapshot/snapshot-transaction-stage.d.ts +32 -0
- package/dist/engine/snapshot/snapshot-transaction-stage.js +303 -0
- package/dist/engine/snapshot/snapshot-transaction.d.ts +2 -1
- package/dist/engine/snapshot/snapshot-transaction.js +379 -331
- package/dist/engine/snapshot/snapshot-types.d.ts +24 -1
- package/dist/engine/snapshot/snapshot.d.ts +19 -2
- package/dist/engine/snapshot/snapshot.js +283 -26
- package/dist/engine/state/merge-baseline-store.js +20 -9
- package/dist/engine/state/state-types.d.ts +2 -0
- package/dist/engine/sync/apply-review.d.ts +3 -3
- package/dist/engine/sync/apply-review.js +5 -4
- package/dist/engine/sync/continuous-sync.js +19 -9
- package/dist/engine/sync/continuous-watch.d.ts +4 -1
- package/dist/engine/sync/continuous-watch.js +86 -14
- package/dist/engine/sync/exclude-policy.d.ts +4 -0
- package/dist/engine/sync/exclude-policy.js +8 -1
- package/dist/engine/sync/initialization.js +9 -5
- package/dist/engine/sync/local-fields.js +27 -8
- package/dist/engine/sync/merge-apply.js +155 -40
- package/dist/engine/sync/merge-journal.js +13 -4
- package/dist/engine/sync/merged-sync.js +21 -18
- package/dist/engine/sync/partial-progress.js +5 -3
- package/dist/engine/sync/remote-snapshot.d.ts +1 -0
- package/dist/engine/sync/remote-snapshot.js +8 -5
- package/dist/engine/sync/session-push.js +7 -6
- package/dist/engine/sync/sync-decision.d.ts +4 -0
- package/dist/engine/sync/sync-decision.js +17 -2
- package/dist/engine/sync/sync-inspection.js +3 -3
- package/dist/engine/sync/sync-mutations.d.ts +8 -1
- package/dist/engine/sync/sync-mutations.js +153 -72
- package/dist/engine/sync/sync-policy.d.ts +20 -10
- package/dist/engine/sync/sync-policy.js +170 -16
- package/dist/engine/sync/sync-queries.js +41 -31
- package/dist/engine/sync/sync-state.d.ts +11 -0
- package/dist/engine/sync/sync-state.js +24 -5
- package/dist/engine/ui/sync-format.js +22 -7
- package/dist/resolver-host.js +1 -1
- package/docs/architecture.md +6 -0
- package/package.json +4 -4
|
@@ -0,0 +1,303 @@
|
|
|
1
|
+
import { createHash, randomUUID } from "node:crypto";
|
|
2
|
+
import fs from "node:fs/promises";
|
|
3
|
+
import path from "node:path";
|
|
4
|
+
import { syncDirectory, writeJson } from "../state/json-file.js";
|
|
5
|
+
import { matchesScopedFilePrefix, ownedScopedSubset, readTransactionJournal, removeScopedImage, scopedIdentity, scopedPathIdentity, scopedTransactionImage, stableScopedIdentity, } from "./snapshot-transaction-images.js";
|
|
6
|
+
import { isRecoveryStageId, isScopedIdentity } from "./snapshot-transaction-policy.js";
|
|
7
|
+
function validate(options) {
|
|
8
|
+
options.validateMutation?.();
|
|
9
|
+
options.signal?.throwIfAborted();
|
|
10
|
+
}
|
|
11
|
+
function unrecognizedStage() {
|
|
12
|
+
throw new Error("Unrecognized recovery staging image; preserve evidence for review.");
|
|
13
|
+
}
|
|
14
|
+
export function recoveryStagePath(directory, entry) {
|
|
15
|
+
const token = createHash("sha256").update(directory).update("\\0").update(entry.backupName).digest("hex");
|
|
16
|
+
return path.join(path.dirname(entry.target), `.pi-sync.json.${token}.${entry.kind === "file" ? "restore" : "restore-tree"}`);
|
|
17
|
+
}
|
|
18
|
+
export async function prepareLegacyRecoveryStage(directory, entry, options, persist) {
|
|
19
|
+
const temporary = recoveryStagePath(directory, entry);
|
|
20
|
+
if (!entry.recoveryStaged) {
|
|
21
|
+
try {
|
|
22
|
+
await fs.lstat(temporary);
|
|
23
|
+
throw new Error("Unowned recovery staging path; preserve evidence for review.");
|
|
24
|
+
}
|
|
25
|
+
catch (error) {
|
|
26
|
+
if (error.code !== "ENOENT")
|
|
27
|
+
throw error;
|
|
28
|
+
}
|
|
29
|
+
validate(options);
|
|
30
|
+
entry.recoveryStaged = true;
|
|
31
|
+
await persist();
|
|
32
|
+
validate(options);
|
|
33
|
+
}
|
|
34
|
+
return temporary;
|
|
35
|
+
}
|
|
36
|
+
export async function assertNoUnclaimedLegacyStage(directory, entry, options) {
|
|
37
|
+
if (entry.recoveryStaged)
|
|
38
|
+
return;
|
|
39
|
+
validate(options);
|
|
40
|
+
try {
|
|
41
|
+
await fs.lstat(recoveryStagePath(directory, entry));
|
|
42
|
+
}
|
|
43
|
+
catch (error) {
|
|
44
|
+
validate(options);
|
|
45
|
+
const code = error.code;
|
|
46
|
+
if (code === "ENOENT" || code === "ENOTDIR")
|
|
47
|
+
return;
|
|
48
|
+
throw error;
|
|
49
|
+
}
|
|
50
|
+
unrecognizedStage();
|
|
51
|
+
}
|
|
52
|
+
export async function cleanScopedLegacyStage(directory, entry, options) {
|
|
53
|
+
const temporary = recoveryStagePath(directory, entry);
|
|
54
|
+
const staged = await scopedTransactionImage(temporary, options);
|
|
55
|
+
if (staged.kind === "missing")
|
|
56
|
+
return;
|
|
57
|
+
if (staged.kind !== entry.kind || staged.value !== entry.beforeImage)
|
|
58
|
+
unrecognizedStage();
|
|
59
|
+
await removeScopedImage(temporary, staged, options);
|
|
60
|
+
await syncDirectory(path.dirname(temporary));
|
|
61
|
+
}
|
|
62
|
+
function workspacePath(directory, entry, slot = entry.recoveryWorkspaceId) {
|
|
63
|
+
const base = `${recoveryStagePath(directory, entry)}.workspace`;
|
|
64
|
+
return slot ? `${base}.${slot}` : base;
|
|
65
|
+
}
|
|
66
|
+
function scratchPath(workspace, slot) {
|
|
67
|
+
return `${workspace}.scratch.${slot}`;
|
|
68
|
+
}
|
|
69
|
+
async function existingDirectory(workspace, identity, options, requireStable = false) {
|
|
70
|
+
validate(options);
|
|
71
|
+
try {
|
|
72
|
+
const stat = await fs.lstat(workspace, { bigint: true });
|
|
73
|
+
validate(options);
|
|
74
|
+
if (!stat.isDirectory() || scopedIdentity(stat) !== identity)
|
|
75
|
+
unrecognizedStage();
|
|
76
|
+
if (requireStable)
|
|
77
|
+
stableScopedIdentity(workspace, stat);
|
|
78
|
+
return true;
|
|
79
|
+
}
|
|
80
|
+
catch (error) {
|
|
81
|
+
validate(options);
|
|
82
|
+
const code = error.code;
|
|
83
|
+
if (code === "ENOENT" || code === "ENOTDIR")
|
|
84
|
+
return false;
|
|
85
|
+
throw error;
|
|
86
|
+
}
|
|
87
|
+
}
|
|
88
|
+
function fileKey(relative) {
|
|
89
|
+
return createHash("sha256").update(relative).digest("hex");
|
|
90
|
+
}
|
|
91
|
+
function findFile(node, key, relative = "") {
|
|
92
|
+
if (node.kind === "file" && fileKey(relative) === key)
|
|
93
|
+
return [relative, node];
|
|
94
|
+
if (node.kind !== "directory")
|
|
95
|
+
return;
|
|
96
|
+
for (const [name, child] of node.children) {
|
|
97
|
+
const found = findFile(child, key, path.join(relative, name));
|
|
98
|
+
if (found)
|
|
99
|
+
return found;
|
|
100
|
+
}
|
|
101
|
+
}
|
|
102
|
+
const SCRATCH_EVIDENCE_BYTES = 1024;
|
|
103
|
+
function parseScratchEvidence(content) {
|
|
104
|
+
if (content === undefined)
|
|
105
|
+
return;
|
|
106
|
+
const saved = JSON.parse(content);
|
|
107
|
+
if (!saved ||
|
|
108
|
+
!isScopedIdentity(saved.identity) ||
|
|
109
|
+
typeof saved.file !== "string" ||
|
|
110
|
+
!/^[0-9a-f]{64}$/u.test(saved.file))
|
|
111
|
+
unrecognizedStage();
|
|
112
|
+
const { identity, file, slot, container } = saved;
|
|
113
|
+
if (slot === undefined && container === undefined)
|
|
114
|
+
return { identity, file };
|
|
115
|
+
if (!isRecoveryStageId(slot) || !isScopedIdentity(container))
|
|
116
|
+
unrecognizedStage();
|
|
117
|
+
return { identity, file, slot, container };
|
|
118
|
+
}
|
|
119
|
+
async function createPrivateStageDirectory(directory, options) {
|
|
120
|
+
validate(options);
|
|
121
|
+
await fs.mkdir(directory, { mode: 0o700 });
|
|
122
|
+
validate(options);
|
|
123
|
+
const stat = await fs.lstat(directory, { bigint: true });
|
|
124
|
+
validate(options);
|
|
125
|
+
if (!stat.isDirectory())
|
|
126
|
+
unrecognizedStage();
|
|
127
|
+
return stableScopedIdentity(directory, stat);
|
|
128
|
+
}
|
|
129
|
+
async function removeScratchContainer(container, identity, options) {
|
|
130
|
+
if (!(await existingDirectory(container, identity, options)))
|
|
131
|
+
return;
|
|
132
|
+
validate(options);
|
|
133
|
+
await fs.rmdir(container);
|
|
134
|
+
await syncDirectory(path.dirname(container));
|
|
135
|
+
}
|
|
136
|
+
export async function inspectScopedRecoveryStage(directory, entry, options, requireStableWorkspace = false) {
|
|
137
|
+
if (!entry.recoveryWorkspace)
|
|
138
|
+
return;
|
|
139
|
+
const workspace = workspacePath(directory, entry);
|
|
140
|
+
if (!(await existingDirectory(workspace, entry.recoveryWorkspace, options)))
|
|
141
|
+
return;
|
|
142
|
+
const backup = path.join(directory, "before", entry.backupName);
|
|
143
|
+
const source = await scopedTransactionImage(backup, options);
|
|
144
|
+
if (source.kind !== entry.kind || source.value !== entry.beforeImage)
|
|
145
|
+
unrecognizedStage();
|
|
146
|
+
const payload = path.join(workspace, "image");
|
|
147
|
+
const evidence = path.join(directory, `scratch-${entry.backupName}.json`);
|
|
148
|
+
const saved = parseScratchEvidence(await readTransactionJournal(evidence, options, SCRATCH_EVIDENCE_BYTES));
|
|
149
|
+
const container = saved?.slot ? scratchPath(workspace, saved.slot) : undefined;
|
|
150
|
+
const hasContainer = container && saved?.container && (await existingDirectory(container, saved.container, options));
|
|
151
|
+
if (hasContainer) {
|
|
152
|
+
for await (const child of await fs.opendir(container)) {
|
|
153
|
+
validate(options);
|
|
154
|
+
if (child.name !== "bytes")
|
|
155
|
+
unrecognizedStage();
|
|
156
|
+
}
|
|
157
|
+
}
|
|
158
|
+
const scratch = container ? path.join(container, "bytes") : path.join(workspace, "scratch");
|
|
159
|
+
for await (const child of await fs.opendir(workspace)) {
|
|
160
|
+
validate(options);
|
|
161
|
+
if (child.name !== "image" && (child.name !== "scratch" || saved?.slot))
|
|
162
|
+
unrecognizedStage();
|
|
163
|
+
}
|
|
164
|
+
const image = await scopedTransactionImage(payload, options);
|
|
165
|
+
if (image.kind !== "missing" && !ownedScopedSubset(image, source))
|
|
166
|
+
unrecognizedStage();
|
|
167
|
+
const partial = !container || hasContainer ? await scopedTransactionImage(scratch, options) : { kind: "missing", value: "missing" };
|
|
168
|
+
if (partial.kind !== "missing") {
|
|
169
|
+
if (partial.kind !== "file" ||
|
|
170
|
+
!saved ||
|
|
171
|
+
scopedIdentity(partial.stat) !== saved.identity ||
|
|
172
|
+
findFile(image, saved.file))
|
|
173
|
+
unrecognizedStage();
|
|
174
|
+
const expected = findFile(source, saved.file);
|
|
175
|
+
if (!expected || !(await matchesScopedFilePrefix(path.join(backup, expected[0]), expected[1], partial, options)))
|
|
176
|
+
unrecognizedStage();
|
|
177
|
+
}
|
|
178
|
+
if (!(await existingDirectory(workspace, entry.recoveryWorkspace, options, requireStableWorkspace)))
|
|
179
|
+
unrecognizedStage();
|
|
180
|
+
return {
|
|
181
|
+
workspace,
|
|
182
|
+
backup,
|
|
183
|
+
source,
|
|
184
|
+
payload,
|
|
185
|
+
image,
|
|
186
|
+
scratch,
|
|
187
|
+
partial,
|
|
188
|
+
evidence,
|
|
189
|
+
container,
|
|
190
|
+
containerIdentity: saved?.container,
|
|
191
|
+
};
|
|
192
|
+
}
|
|
193
|
+
async function clearScratch(stage, options) {
|
|
194
|
+
if (stage.container && stage.containerIdentity) {
|
|
195
|
+
if (!(await existingDirectory(stage.container, stage.containerIdentity, options))) {
|
|
196
|
+
if (stage.partial.kind !== "missing")
|
|
197
|
+
unrecognizedStage();
|
|
198
|
+
return;
|
|
199
|
+
}
|
|
200
|
+
await removeScopedImage(stage.scratch, stage.partial, options);
|
|
201
|
+
await removeScratchContainer(stage.container, stage.containerIdentity, options);
|
|
202
|
+
}
|
|
203
|
+
else {
|
|
204
|
+
await removeScopedImage(stage.scratch, stage.partial, options);
|
|
205
|
+
}
|
|
206
|
+
}
|
|
207
|
+
export async function buildScopedRecoveryStage(directory, entry, options, persist) {
|
|
208
|
+
if (!entry.recoveryWorkspace) {
|
|
209
|
+
const slot = randomUUID();
|
|
210
|
+
const workspace = workspacePath(directory, entry, slot);
|
|
211
|
+
const identity = await createPrivateStageDirectory(workspace, options);
|
|
212
|
+
await syncDirectory(path.dirname(workspace));
|
|
213
|
+
validate(options);
|
|
214
|
+
entry.recoveryWorkspace = identity;
|
|
215
|
+
entry.recoveryWorkspaceId = slot;
|
|
216
|
+
await persist();
|
|
217
|
+
}
|
|
218
|
+
const stage = (await inspectScopedRecoveryStage(directory, entry, options, true)) ?? unrecognizedStage();
|
|
219
|
+
const { workspace } = stage;
|
|
220
|
+
const workspaceIdentity = entry.recoveryWorkspace;
|
|
221
|
+
await clearScratch(stage, options);
|
|
222
|
+
async function copy(source, existing, relative) {
|
|
223
|
+
if (!workspaceIdentity || !(await existingDirectory(workspace, workspaceIdentity, options)))
|
|
224
|
+
unrecognizedStage();
|
|
225
|
+
const destination = path.join(stage.payload, relative);
|
|
226
|
+
if (source.kind === "file") {
|
|
227
|
+
if (existing && existing.kind !== "missing")
|
|
228
|
+
return;
|
|
229
|
+
let identity;
|
|
230
|
+
const slot = randomUUID();
|
|
231
|
+
const container = scratchPath(workspace, slot);
|
|
232
|
+
const containerIdentity = await createPrivateStageDirectory(container, options);
|
|
233
|
+
const scratch = path.join(container, "bytes");
|
|
234
|
+
const copied = await scopedTransactionImage(path.join(stage.backup, relative), options, scratch, undefined, async (handle) => {
|
|
235
|
+
const stat = await handle.stat({ bigint: true });
|
|
236
|
+
validate(options);
|
|
237
|
+
if (!stat.isFile() || stat.nlink !== 1n || stat.size !== 0n)
|
|
238
|
+
unrecognizedStage();
|
|
239
|
+
identity = stableScopedIdentity(scratch, stat);
|
|
240
|
+
if ((await scopedPathIdentity(scratch, options)) !== identity)
|
|
241
|
+
unrecognizedStage();
|
|
242
|
+
if (!(await existingDirectory(container, containerIdentity, options)))
|
|
243
|
+
unrecognizedStage();
|
|
244
|
+
await syncDirectory(container);
|
|
245
|
+
await syncDirectory(path.dirname(container));
|
|
246
|
+
if (!(await existingDirectory(workspace, workspaceIdentity, options)))
|
|
247
|
+
unrecognizedStage();
|
|
248
|
+
validate(options);
|
|
249
|
+
await writeJson(stage.evidence, { identity, file: fileKey(relative), slot, container: containerIdentity }, { maxBytes: SCRATCH_EVIDENCE_BYTES });
|
|
250
|
+
});
|
|
251
|
+
if (copied.value !== source.value)
|
|
252
|
+
unrecognizedStage();
|
|
253
|
+
const ready = await scopedTransactionImage(scratch, options);
|
|
254
|
+
if (ready.kind !== "file" ||
|
|
255
|
+
ready.value !== source.value ||
|
|
256
|
+
scopedIdentity(ready.stat) !== identity ||
|
|
257
|
+
(await scopedTransactionImage(destination, options)).kind !== "missing" ||
|
|
258
|
+
!(await existingDirectory(workspace, workspaceIdentity, options)) ||
|
|
259
|
+
!(await existingDirectory(container, containerIdentity, options)))
|
|
260
|
+
unrecognizedStage();
|
|
261
|
+
if ((await scopedPathIdentity(scratch, options)) !== identity)
|
|
262
|
+
unrecognizedStage();
|
|
263
|
+
validate(options);
|
|
264
|
+
await fs.rename(scratch, destination);
|
|
265
|
+
await syncDirectory(path.dirname(destination));
|
|
266
|
+
await removeScratchContainer(container, containerIdentity, options);
|
|
267
|
+
}
|
|
268
|
+
else if (source.kind === "directory") {
|
|
269
|
+
if (!existing || existing.kind === "missing") {
|
|
270
|
+
await fs.mkdir(destination, { mode: 0o700 });
|
|
271
|
+
await syncDirectory(path.dirname(destination));
|
|
272
|
+
}
|
|
273
|
+
const children = new Map(existing?.kind === "directory" ? existing.children : []);
|
|
274
|
+
for (const [name, child] of source.children)
|
|
275
|
+
await copy(child, children.get(name), path.join(relative, name));
|
|
276
|
+
validate(options);
|
|
277
|
+
await fs.chmod(destination, Number(source.stat.mode & 511n));
|
|
278
|
+
validate(options);
|
|
279
|
+
await fs.utimes(destination, source.stat.atime, source.stat.mtime);
|
|
280
|
+
await syncDirectory(destination);
|
|
281
|
+
}
|
|
282
|
+
}
|
|
283
|
+
await copy(stage.source, stage.image, "");
|
|
284
|
+
}
|
|
285
|
+
export async function cleanScopedWorkspace(directory, entry, options) {
|
|
286
|
+
const stage = await inspectScopedRecoveryStage(directory, entry, options);
|
|
287
|
+
if (!stage)
|
|
288
|
+
return;
|
|
289
|
+
const identity = entry.recoveryWorkspace;
|
|
290
|
+
if (!identity)
|
|
291
|
+
unrecognizedStage();
|
|
292
|
+
if ((stage.image.kind !== "missing" ||
|
|
293
|
+
(stage.partial.kind !== "missing" && path.dirname(stage.scratch) === stage.workspace)) &&
|
|
294
|
+
!(await existingDirectory(stage.workspace, identity, options, true)))
|
|
295
|
+
unrecognizedStage();
|
|
296
|
+
await clearScratch(stage, options);
|
|
297
|
+
await removeScopedImage(stage.payload, stage.image, options);
|
|
298
|
+
if (!(await existingDirectory(stage.workspace, identity, options)))
|
|
299
|
+
unrecognizedStage();
|
|
300
|
+
validate(options);
|
|
301
|
+
await fs.rmdir(stage.workspace);
|
|
302
|
+
await syncDirectory(path.dirname(stage.workspace));
|
|
303
|
+
}
|
|
@@ -1,5 +1,6 @@
|
|
|
1
|
+
import { type SyncSelectionConfig } from "../sync/sync-policy.js";
|
|
1
2
|
import type { SnapshotApplyPlan } from "./snapshot-types.js";
|
|
2
|
-
interface TransactionOptions {
|
|
3
|
+
interface TransactionOptions extends SyncSelectionConfig {
|
|
3
4
|
sessionDir?: string;
|
|
4
5
|
exclude?: readonly string[];
|
|
5
6
|
signal?: AbortSignal;
|