@narumitw/pi-sync-daemon 0.6.2 → 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 +8 -0
- package/README.md +1 -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/docs/architecture.md +6 -0
- package/package.json +1 -1
|
@@ -1,47 +1,205 @@
|
|
|
1
|
+
import { lstatSync } from "node:fs";
|
|
1
2
|
import fs from "node:fs/promises";
|
|
2
3
|
import path from "node:path";
|
|
3
4
|
import { isPathInside, toPosix } from "../paths.js";
|
|
4
|
-
import {
|
|
5
|
+
import { mergePathIdentity } from "../sync/file-merge-planner.js";
|
|
6
|
+
import { compileSyncPolicyExclude, compileSyncPolicyTraversal, configuredSnapshotPathMatcher, } from "../sync/sync-policy.js";
|
|
5
7
|
import { sessionStorageRoot } from "./snapshot-paths.js";
|
|
8
|
+
function directoryStat(absolute) {
|
|
9
|
+
try {
|
|
10
|
+
return lstatSync(absolute, { throwIfNoEntry: false });
|
|
11
|
+
}
|
|
12
|
+
catch (error) {
|
|
13
|
+
if (error.code !== "ENOTDIR")
|
|
14
|
+
throw error;
|
|
15
|
+
return undefined;
|
|
16
|
+
}
|
|
17
|
+
}
|
|
18
|
+
function physicalRelative(root, target) {
|
|
19
|
+
root = path.resolve(root);
|
|
20
|
+
target = path.resolve(target);
|
|
21
|
+
if (target === root)
|
|
22
|
+
return "";
|
|
23
|
+
const prefix = root.endsWith(path.sep) ? root : `${root}${path.sep}`;
|
|
24
|
+
if (target.startsWith(prefix))
|
|
25
|
+
return target.slice(prefix.length);
|
|
26
|
+
const rootDepth = root.slice(path.parse(root).root.length).split(path.sep).filter(Boolean).length;
|
|
27
|
+
const targetVolume = path.parse(target).root;
|
|
28
|
+
const parts = target.slice(targetVolume.length).split(path.sep).filter(Boolean);
|
|
29
|
+
if (parts.length < rootDepth)
|
|
30
|
+
return undefined;
|
|
31
|
+
const ancestor = path.join(targetVolume, ...parts.slice(0, rootDepth));
|
|
32
|
+
return sameDirectoryPrefix(root, ancestor) ? parts.slice(rootDepth).join(path.sep) : undefined;
|
|
33
|
+
}
|
|
34
|
+
function sameDirectoryPrefix(left, right) {
|
|
35
|
+
if (left === right)
|
|
36
|
+
return true;
|
|
37
|
+
const samePortableName = mergePathIdentity(left) === mergePathIdentity(right);
|
|
38
|
+
if (!samePortableName && process.platform !== "win32")
|
|
39
|
+
return false;
|
|
40
|
+
const boundary = directoryStat(left);
|
|
41
|
+
const candidate = directoryStat(right);
|
|
42
|
+
if (boundary || candidate)
|
|
43
|
+
return Boolean(boundary?.isDirectory() &&
|
|
44
|
+
candidate?.isDirectory() &&
|
|
45
|
+
boundary.dev === candidate.dev &&
|
|
46
|
+
boundary.ino === candidate.ino);
|
|
47
|
+
return (samePortableName && path.dirname(left) !== left && sameDirectoryPrefix(path.dirname(left), path.dirname(right)));
|
|
48
|
+
}
|
|
49
|
+
export function agentDirectoryAnchor(root, boundary) {
|
|
50
|
+
let anchor = path.dirname(path.resolve(root));
|
|
51
|
+
while (!isPathInside(anchor, boundary)) {
|
|
52
|
+
const parent = path.dirname(anchor);
|
|
53
|
+
if (parent === anchor)
|
|
54
|
+
return path.parse(boundary).root;
|
|
55
|
+
anchor = parent;
|
|
56
|
+
}
|
|
57
|
+
return anchor;
|
|
58
|
+
}
|
|
59
|
+
export function agentDirectorySessionRoot(root, sessionDir) {
|
|
60
|
+
const sessionRoot = sessionStorageRoot(root, sessionDir);
|
|
61
|
+
if (physicalRelative(sessionRoot, root) !== undefined)
|
|
62
|
+
throw new Error("Pi session storage overlaps the agent directory root.");
|
|
63
|
+
const relative = physicalRelative(root, sessionRoot);
|
|
64
|
+
if (relative !== undefined) {
|
|
65
|
+
const canTraverse = compileSyncPolicyTraversal({ scope: "agent-directory", include: ["sessions"] });
|
|
66
|
+
if (!canTraverse(toPosix(relative)))
|
|
67
|
+
throw new Error("Pi session storage overlaps protected agent files.");
|
|
68
|
+
}
|
|
69
|
+
return sessionRoot;
|
|
70
|
+
}
|
|
71
|
+
export function agentDirectoryPhysicalSessionPath(root, sessionDir) {
|
|
72
|
+
const sessionRoot = agentDirectorySessionRoot(root, sessionDir);
|
|
73
|
+
const nested = physicalRelative(root, sessionRoot) !== undefined;
|
|
74
|
+
return (relativePath) => nested && physicalRelative(sessionRoot, path.resolve(root, relativePath)) !== undefined;
|
|
75
|
+
}
|
|
6
76
|
/** Metadata-only preflight: an ancestor replacement must not capture/remove excluded descendants. */
|
|
7
|
-
export async function assertUnexcludedTargets(root, targets, options) {
|
|
8
|
-
|
|
77
|
+
export async function assertUnexcludedTargets(root, targets, options, { recursive = true } = {}) {
|
|
78
|
+
const wholeDirectory = options.scope === "agent-directory";
|
|
79
|
+
if (!wholeDirectory && (options.exclude === undefined || (Array.isArray(options.exclude) && !options.exclude.length)))
|
|
9
80
|
return;
|
|
10
|
-
const excluded =
|
|
11
|
-
const sessionRoot =
|
|
81
|
+
const excluded = compileSyncPolicyExclude(options);
|
|
82
|
+
const sessionRoot = wholeDirectory
|
|
83
|
+
? agentDirectorySessionRoot(root, options.sessionDir)
|
|
84
|
+
: sessionStorageRoot(root, options.sessionDir);
|
|
85
|
+
const configured = configuredSnapshotPathMatcher(options);
|
|
12
86
|
let visited = 0;
|
|
13
87
|
const validate = () => {
|
|
14
88
|
options.signal?.throwIfAborted();
|
|
15
89
|
options.validateMutation?.();
|
|
16
90
|
};
|
|
17
|
-
|
|
91
|
+
const reserveVisit = () => {
|
|
18
92
|
validate();
|
|
19
93
|
if (++visited > 16_384)
|
|
20
94
|
throw new Error("Exclude safety preflight exceeds its path bound; review a smaller transfer.");
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
95
|
+
};
|
|
96
|
+
const refuse = () => {
|
|
97
|
+
throw new Error("Transfer would replace an excluded path or directory descendant; no replacement is authorized.");
|
|
98
|
+
};
|
|
99
|
+
async function visit(target) {
|
|
100
|
+
validate();
|
|
101
|
+
let sessionPath;
|
|
102
|
+
if (wholeDirectory) {
|
|
103
|
+
if (physicalRelative(target, sessionRoot) !== undefined)
|
|
104
|
+
refuse();
|
|
105
|
+
const sessionRelative = physicalRelative(sessionRoot, target);
|
|
106
|
+
if (sessionRelative !== undefined) {
|
|
107
|
+
sessionPath = `sessions/${toPosix(sessionRelative)}`;
|
|
108
|
+
if (excluded(sessionPath))
|
|
109
|
+
refuse();
|
|
110
|
+
}
|
|
111
|
+
else {
|
|
112
|
+
const relative = physicalRelative(root, target);
|
|
113
|
+
if (relative === undefined || /^sessions(?:\/|$)/iu.test(toPosix(relative)) || excluded(toPosix(relative)))
|
|
114
|
+
refuse();
|
|
115
|
+
}
|
|
116
|
+
}
|
|
117
|
+
else {
|
|
118
|
+
const candidates = [];
|
|
119
|
+
if (isPathInside(root, target))
|
|
120
|
+
candidates.push(toPosix(path.relative(root, target)));
|
|
121
|
+
if (isPathInside(sessionRoot, target))
|
|
122
|
+
candidates.push(`sessions/${toPosix(path.relative(sessionRoot, target))}`);
|
|
123
|
+
if (candidates.some(excluded))
|
|
124
|
+
refuse();
|
|
125
|
+
}
|
|
126
|
+
let stat;
|
|
28
127
|
try {
|
|
29
|
-
|
|
30
|
-
validate();
|
|
31
|
-
if (!stat.isDirectory() || stat.isSymbolicLink())
|
|
32
|
-
return;
|
|
33
|
-
const names = await fs.readdir(target);
|
|
34
|
-
validate();
|
|
35
|
-
for (const name of names)
|
|
36
|
-
await visit(path.join(target, name));
|
|
128
|
+
stat = await fs.lstat(target);
|
|
37
129
|
}
|
|
38
130
|
catch (error) {
|
|
39
131
|
validate();
|
|
40
132
|
const code = error.code;
|
|
41
133
|
if (code !== "ENOENT" && code !== "ENOTDIR")
|
|
42
134
|
throw error;
|
|
135
|
+
return;
|
|
43
136
|
}
|
|
137
|
+
validate();
|
|
138
|
+
if (wholeDirectory && !stat.isDirectory() && (!stat.isFile() || stat.nlink !== 1)) {
|
|
139
|
+
throw new Error("Refusing an unsafe symlink, hard link, or special file during snapshot mutation.");
|
|
140
|
+
}
|
|
141
|
+
if (sessionPath && stat.isFile() && !configured(sessionPath))
|
|
142
|
+
refuse();
|
|
143
|
+
if (!recursive || !stat.isDirectory())
|
|
144
|
+
return;
|
|
145
|
+
const children = [];
|
|
146
|
+
for await (const entry of await fs.opendir(target)) {
|
|
147
|
+
reserveVisit();
|
|
148
|
+
children.push(entry.name);
|
|
149
|
+
}
|
|
150
|
+
async function recheckDirectory() {
|
|
151
|
+
if (!wholeDirectory)
|
|
152
|
+
return;
|
|
153
|
+
const current = await fs.lstat(target);
|
|
154
|
+
validate();
|
|
155
|
+
if (!current.isDirectory() ||
|
|
156
|
+
current.dev !== stat.dev ||
|
|
157
|
+
current.ino !== stat.ino ||
|
|
158
|
+
current.ctimeMs !== stat.ctimeMs)
|
|
159
|
+
refuse();
|
|
160
|
+
}
|
|
161
|
+
await recheckDirectory();
|
|
162
|
+
for (const child of children)
|
|
163
|
+
await visit(path.join(target, child));
|
|
164
|
+
await recheckDirectory();
|
|
44
165
|
}
|
|
45
|
-
|
|
166
|
+
async function assertParents(target, includeTarget = false) {
|
|
167
|
+
let parent = agentDirectoryAnchor(root, target);
|
|
168
|
+
const parts = path.relative(parent, target).split(path.sep);
|
|
169
|
+
if (!includeTarget)
|
|
170
|
+
parts.pop();
|
|
171
|
+
for (const part of parts) {
|
|
172
|
+
parent = path.join(parent, part);
|
|
173
|
+
validate();
|
|
174
|
+
try {
|
|
175
|
+
const stat = await fs.lstat(parent);
|
|
176
|
+
validate();
|
|
177
|
+
if (stat.isSymbolicLink())
|
|
178
|
+
throw new Error("Refusing to follow symlink during snapshot mutation.");
|
|
179
|
+
if (!stat.isDirectory()) {
|
|
180
|
+
if (includeTarget)
|
|
181
|
+
throw new Error("Pi session storage is not a directory.");
|
|
182
|
+
break;
|
|
183
|
+
}
|
|
184
|
+
}
|
|
185
|
+
catch (error) {
|
|
186
|
+
validate();
|
|
187
|
+
const code = error.code;
|
|
188
|
+
if (code !== "ENOENT" && code !== "ENOTDIR")
|
|
189
|
+
throw error;
|
|
190
|
+
break;
|
|
191
|
+
}
|
|
192
|
+
}
|
|
193
|
+
}
|
|
194
|
+
validate();
|
|
195
|
+
if (wholeDirectory && options.sessionDir)
|
|
196
|
+
await assertParents(sessionRoot, true);
|
|
197
|
+
for (const target of new Set(targets)) {
|
|
198
|
+
reserveVisit();
|
|
199
|
+
if (wholeDirectory &&
|
|
200
|
+
(physicalRelative(root, target) !== undefined || physicalRelative(sessionRoot, target) !== undefined)) {
|
|
201
|
+
await assertParents(target);
|
|
202
|
+
}
|
|
46
203
|
await visit(target);
|
|
204
|
+
}
|
|
47
205
|
}
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
export function snapshotBase64Size(value, filePath) {
|
|
2
|
+
if (typeof value !== "string" || value.length % 4 !== 0 || !/^[A-Za-z0-9+/]*={0,2}(?![\s\S])/u.test(value))
|
|
3
|
+
throw new Error(`Invalid base64 content in snapshot file: ${filePath}`);
|
|
4
|
+
const padding = value.endsWith("==") ? 2 : value.endsWith("=") ? 1 : 0;
|
|
5
|
+
return (value.length / 4) * 3 - padding;
|
|
6
|
+
}
|
|
7
|
+
export function decodeBase64Strict(value, filePath) {
|
|
8
|
+
snapshotBase64Size(value, filePath);
|
|
9
|
+
return Buffer.from(value, "base64");
|
|
10
|
+
}
|
|
@@ -0,0 +1,2 @@
|
|
|
1
|
+
import type { AgentDirectoryInventory, SnapshotApplyPlan, SnapshotFile } from "./snapshot-types.js";
|
|
2
|
+
export declare function reviewedSnapshotPreimages(root: string, sessionDir: string | undefined, reviewed: readonly SnapshotFile[], plan: SnapshotApplyPlan, inventory?: AgentDirectoryInventory): Map<string, string>;
|
|
@@ -0,0 +1,60 @@
|
|
|
1
|
+
import { createHash } from "node:crypto";
|
|
2
|
+
import path from "node:path";
|
|
3
|
+
import { snapshotTarget } from "./snapshot.js";
|
|
4
|
+
function image(entry) {
|
|
5
|
+
if (entry === "unreviewed" || (typeof entry !== "string" && entry.unreviewed))
|
|
6
|
+
throw new Error("Reviewed directory contains an unreviewed entry; no replacement is authorized.");
|
|
7
|
+
if (typeof entry === "string")
|
|
8
|
+
return entry;
|
|
9
|
+
const rows = [...entry.entries.keys()]
|
|
10
|
+
.sort()
|
|
11
|
+
.map((name) => [name, image(entry.entries.get(name))]);
|
|
12
|
+
return `directory:${createHash("sha256").update(JSON.stringify(rows)).digest("hex")}`;
|
|
13
|
+
}
|
|
14
|
+
export function reviewedSnapshotPreimages(root, sessionDir, reviewed, plan, inventory) {
|
|
15
|
+
const images = new Map();
|
|
16
|
+
function insert(target, entry) {
|
|
17
|
+
const existing = images.get(target);
|
|
18
|
+
if (existing) {
|
|
19
|
+
if (typeof entry === "string" || typeof existing === "string")
|
|
20
|
+
throw new Error("Reviewed paths conflict before apply.");
|
|
21
|
+
return;
|
|
22
|
+
}
|
|
23
|
+
images.set(target, entry);
|
|
24
|
+
for (let parent = path.dirname(target); parent !== target; parent = path.dirname(target)) {
|
|
25
|
+
const existing = images.get(parent);
|
|
26
|
+
if (typeof existing === "string")
|
|
27
|
+
throw new Error("Reviewed paths conflict before apply.");
|
|
28
|
+
const directory = existing ?? { entries: new Map() };
|
|
29
|
+
directory.entries.set(path.basename(target), entry);
|
|
30
|
+
if (existing)
|
|
31
|
+
break;
|
|
32
|
+
images.set(parent, directory);
|
|
33
|
+
target = parent;
|
|
34
|
+
entry = directory;
|
|
35
|
+
}
|
|
36
|
+
}
|
|
37
|
+
for (const file of reviewed)
|
|
38
|
+
insert(snapshotTarget(root, file.path, sessionDir), `file:${file.sha256}`);
|
|
39
|
+
if (inventory) {
|
|
40
|
+
for (const [target, kind] of inventory.entries) {
|
|
41
|
+
if (kind === "directory")
|
|
42
|
+
insert(target, { entries: new Map() });
|
|
43
|
+
}
|
|
44
|
+
for (const [target, kind] of inventory.entries) {
|
|
45
|
+
if (kind !== "unreviewed")
|
|
46
|
+
continue;
|
|
47
|
+
const entry = images.get(target);
|
|
48
|
+
if (entry && typeof entry !== "string")
|
|
49
|
+
entry.unreviewed = true;
|
|
50
|
+
const parent = images.get(path.dirname(target));
|
|
51
|
+
if (parent && typeof parent !== "string")
|
|
52
|
+
parent.entries.set(path.basename(target), "unreviewed");
|
|
53
|
+
}
|
|
54
|
+
}
|
|
55
|
+
const targets = new Set([...plan.deletes, ...plan.writes.map(({ target }) => target)]);
|
|
56
|
+
return new Map([...targets].map((target) => {
|
|
57
|
+
const entry = inventory?.entries.get(target) === "unreviewed" ? "unreviewed" : images.get(target);
|
|
58
|
+
return [target, entry === undefined ? "missing" : image(entry)];
|
|
59
|
+
}));
|
|
60
|
+
}
|
|
@@ -18,6 +18,10 @@ interface SettingsEntry {
|
|
|
18
18
|
beforeImage?: string;
|
|
19
19
|
afterImage?: string;
|
|
20
20
|
}
|
|
21
|
+
export declare function stableAgentDirectorySettingsImage(root: string, plan: SnapshotApplyPlan, guards: TransitionGuards): Promise<{
|
|
22
|
+
target: string;
|
|
23
|
+
image: string;
|
|
24
|
+
} | undefined>;
|
|
21
25
|
export declare function prepareSessionRootTransition(directory: string, root: string, sessionRoot: string, entries: SettingsEntry[], plan: SnapshotApplyPlan, guards?: TransitionGuards): Promise<SessionRootTransition | undefined>;
|
|
22
26
|
export declare function validateSessionRootTransition(value: SessionRootTransition): void;
|
|
23
27
|
/** Authorization is bound to both settings images, never just an arbitrary pinned root. */
|
|
@@ -2,7 +2,9 @@ import fs from "node:fs/promises";
|
|
|
2
2
|
import path from "node:path";
|
|
3
3
|
import { isPathInside } from "../paths.js";
|
|
4
4
|
import { syncDirectory } from "../state/json-file.js";
|
|
5
|
+
import { mergePathIdentity } from "../sync/file-merge-planner.js";
|
|
5
6
|
import { expandSessionDir } from "./session-paths.js";
|
|
7
|
+
import { agentDirectorySessionDirFromSettings, readAgentDirectorySettings } from "./snapshot.js";
|
|
6
8
|
import { sessionStorageRoot } from "./snapshot-paths.js";
|
|
7
9
|
import { fileImage } from "./snapshot-transaction-plan.js";
|
|
8
10
|
function validateOwner(guards) {
|
|
@@ -41,6 +43,20 @@ async function beforeRoot(directory, root, entry, guards) {
|
|
|
41
43
|
throw new Error("Changed settings root-transition backup.");
|
|
42
44
|
return rootFromSettings(root, bytes);
|
|
43
45
|
}
|
|
46
|
+
export async function stableAgentDirectorySettingsImage(root, plan, guards) {
|
|
47
|
+
const settingsTarget = mergePathIdentity(path.join(root, "settings.json"));
|
|
48
|
+
const write = plan.writes.find(({ target }) => mergePathIdentity(target) === settingsTarget);
|
|
49
|
+
const target = write?.target ?? plan.deletes.find((target) => mergePathIdentity(target) === settingsTarget);
|
|
50
|
+
if (!target)
|
|
51
|
+
return;
|
|
52
|
+
const beforeBytes = await readAgentDirectorySettings(guards.signal, root);
|
|
53
|
+
validateOwner(guards);
|
|
54
|
+
const before = beforeBytes === undefined ? undefined : agentDirectorySessionDirFromSettings(beforeBytes);
|
|
55
|
+
const after = write ? agentDirectorySessionDirFromSettings(write.content) : undefined;
|
|
56
|
+
if (sessionStorageRoot(root, before) !== sessionStorageRoot(root, after))
|
|
57
|
+
throw new Error("Agent-directory transfers cannot change Pi session storage; keep the local sessionDir setting.");
|
|
58
|
+
return { target, image: beforeBytes === undefined ? "missing" : fileImage(beforeBytes) };
|
|
59
|
+
}
|
|
44
60
|
export async function prepareSessionRootTransition(directory, root, sessionRoot, entries, plan, guards = {}) {
|
|
45
61
|
const settingsTarget = path.join(root, "settings.json");
|
|
46
62
|
const entry = entries.find((item) => item.target === settingsTarget || item.afterTarget === settingsTarget);
|
|
@@ -0,0 +1,37 @@
|
|
|
1
|
+
import { type BigIntStats } from "node:fs";
|
|
2
|
+
import { type FileHandle } from "node:fs/promises";
|
|
3
|
+
import { type TransactionEntry } from "./snapshot-transaction-policy.js";
|
|
4
|
+
export declare const MAX_TRANSACTION_JOURNAL_BYTES: number;
|
|
5
|
+
interface Guards {
|
|
6
|
+
signal?: AbortSignal;
|
|
7
|
+
validateMutation?: () => void;
|
|
8
|
+
}
|
|
9
|
+
interface ScopedDirectoryImage {
|
|
10
|
+
kind: "directory";
|
|
11
|
+
value: string;
|
|
12
|
+
stat: BigIntStats;
|
|
13
|
+
children: [string, ScopedNode][];
|
|
14
|
+
}
|
|
15
|
+
type ScopedNode = {
|
|
16
|
+
kind: "file";
|
|
17
|
+
value: string;
|
|
18
|
+
stat: BigIntStats;
|
|
19
|
+
} | ScopedDirectoryImage;
|
|
20
|
+
export type ScopedImage = {
|
|
21
|
+
kind: "missing";
|
|
22
|
+
value: "missing";
|
|
23
|
+
} | ScopedNode;
|
|
24
|
+
export declare function scopedPathIdentity(target: string, guards: Guards): Promise<string>;
|
|
25
|
+
export declare function scopedIdentity(stat: BigIntStats): string;
|
|
26
|
+
export declare function stableScopedIdentity(target: string, stat: BigIntStats): string;
|
|
27
|
+
export declare function readTransactionJournal(file: string, guards: Guards, maxBytes?: number): Promise<string | undefined>;
|
|
28
|
+
export declare function scopedTransactionImage(target: string, guards: Guards, copyTo?: string, checkPath?: (source: string) => Promise<void>, beforeCopyFile?: (handle: FileHandle) => Promise<void>): Promise<ScopedImage>;
|
|
29
|
+
export declare function ownedScopedSubset(image: ScopedImage, expected: ScopedImage): boolean;
|
|
30
|
+
export declare function matchesScopedFilePrefix(source: string, original: ScopedImage, partial: ScopedImage, guards: Guards): Promise<boolean>;
|
|
31
|
+
export declare function scopedDescendant(image: ScopedImage, relative: string): ScopedImage | undefined;
|
|
32
|
+
export declare function removeScopedImage(target: string, image: ScopedImage, guards: Guards): Promise<void>;
|
|
33
|
+
export declare function legacyImage(target: string, durable?: boolean): Promise<string>;
|
|
34
|
+
type PostFiles = NonNullable<TransactionEntry["postFiles"]>;
|
|
35
|
+
export declare function ownedScopedPostTree(image: ScopedImage, files: PostFiles, requireComplete: boolean): boolean;
|
|
36
|
+
export declare function ownedLegacyPostTree(target: string, files: PostFiles, requireComplete: boolean): Promise<boolean>;
|
|
37
|
+
export {};
|