@narumitw/pi-sync 0.58.0 → 0.59.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 +16 -0
- package/README.md +12 -6
- package/dist/chunks/{agent-candidate-U2NU3GUX.ts → agent-candidate-ZHN7FTZT.ts} +9 -8
- package/dist/chunks/{agent-plan-3YXDIJXO.ts → agent-plan-ILFEUJ2E.ts} +12 -11
- package/dist/chunks/{agent-plan-3YXDIJXO.ts.map → agent-plan-ILFEUJ2E.ts.map} +1 -1
- package/dist/chunks/{agent-refusal-retry-476YGNU5.ts → agent-refusal-retry-HRMHCX2A.ts} +9 -8
- package/dist/chunks/{agent-refusal-retry-476YGNU5.ts.map → agent-refusal-retry-HRMHCX2A.ts.map} +1 -1
- package/dist/chunks/{agent-store-7GINKTS7.ts → agent-store-CCKN4DPR.ts} +3 -3
- package/dist/chunks/{agent-sync-2R4GG2MF.ts → agent-sync-EGA2I5W7.ts} +4 -4
- package/dist/chunks/{chunk-CQD2TJHE.ts → chunk-25QCU3LY.ts} +4 -4
- package/dist/chunks/{chunk-OJARL2TG.ts → chunk-2UKUKIBI.ts} +4 -4
- package/dist/chunks/{chunk-MW6KUQYA.ts → chunk-2ZZJXITS.ts} +40 -10
- package/dist/chunks/chunk-2ZZJXITS.ts.map +7 -0
- package/dist/chunks/chunk-3JTZF7Z5.ts +77 -0
- package/dist/chunks/chunk-3JTZF7Z5.ts.map +7 -0
- package/dist/chunks/{chunk-DN4IA7MR.ts → chunk-4NVHHDN2.ts} +2 -2
- package/dist/chunks/{chunk-AKMV47YC.ts → chunk-6OFMB6KK.ts} +2 -2
- package/dist/chunks/{chunk-45COPAK7.ts → chunk-6XU5HZAN.ts} +6 -6
- package/dist/chunks/{chunk-45COPAK7.ts.map → chunk-6XU5HZAN.ts.map} +2 -2
- package/dist/chunks/{chunk-Y4UVXOLQ.ts → chunk-6YGRZSCN.ts} +15 -10
- package/dist/chunks/chunk-6YGRZSCN.ts.map +7 -0
- package/dist/chunks/{chunk-XEODG2PE.ts → chunk-76HID27T.ts} +2 -2
- package/dist/chunks/{chunk-7A35ZAVO.ts → chunk-7WYF2KUO.ts} +174 -122
- package/dist/chunks/chunk-7WYF2KUO.ts.map +7 -0
- package/dist/chunks/{chunk-MMQIEDZ6.ts → chunk-APDNHB2T.ts} +78 -35
- package/dist/chunks/chunk-APDNHB2T.ts.map +7 -0
- package/dist/chunks/{chunk-U6GXGY5G.ts → chunk-DTPELCPW.ts} +4 -4
- package/dist/chunks/{chunk-TZC7GDJE.ts → chunk-ILXMZ2HU.ts} +4 -4
- package/dist/chunks/{chunk-CBLTWXAU.ts → chunk-LA3HQMFG.ts} +2 -1
- package/dist/chunks/{chunk-CBLTWXAU.ts.map → chunk-LA3HQMFG.ts.map} +2 -2
- package/dist/chunks/{chunk-ESKOPQVB.ts → chunk-MKC3PXCN.ts} +53 -32
- package/dist/chunks/chunk-MKC3PXCN.ts.map +7 -0
- package/dist/chunks/{chunk-MG66XM5U.ts → chunk-NGW4JMBF.ts} +63 -19
- package/dist/chunks/chunk-NGW4JMBF.ts.map +7 -0
- package/dist/chunks/{chunk-TIMWN2RH.ts → chunk-OQBE2OMB.ts} +13 -14
- package/dist/chunks/chunk-OQBE2OMB.ts.map +7 -0
- package/dist/chunks/{chunk-MKSMJPVA.ts → chunk-P3YD4ONV.ts} +22 -6
- package/dist/chunks/chunk-P3YD4ONV.ts.map +7 -0
- package/dist/chunks/{chunk-JYILGR3K.ts → chunk-PQ75RPUP.ts} +3 -3
- package/dist/chunks/{chunk-FLHCOJCD.ts → chunk-QQVK2XFC.ts} +13 -6
- package/dist/chunks/chunk-QQVK2XFC.ts.map +7 -0
- package/dist/chunks/{chunk-WQMYV4DW.ts → chunk-RDOX3VH5.ts} +5 -5
- package/dist/chunks/{chunk-6O3EDBRL.ts → chunk-S7D6XWHI.ts} +2 -2
- package/dist/chunks/{chunk-7ZIYU5QS.ts → chunk-SHPNS7FM.ts} +7 -7
- package/dist/chunks/{chunk-7ZIYU5QS.ts.map → chunk-SHPNS7FM.ts.map} +2 -2
- package/dist/chunks/{chunk-GNYGNHFF.ts → chunk-T2TYBLXK.ts} +2 -2
- package/dist/chunks/{chunk-6LD5KX55.ts → chunk-UZSDS4HF.ts} +13 -6
- package/dist/chunks/chunk-UZSDS4HF.ts.map +7 -0
- package/dist/chunks/{chunk-5TSCBBPP.ts → chunk-VPODHKOX.ts} +3 -3
- package/dist/chunks/{chunk-BEST45H2.ts → chunk-WEKGW7HV.ts} +17 -8
- package/dist/chunks/chunk-WEKGW7HV.ts.map +7 -0
- package/dist/chunks/{chunk-KYTT7KV4.ts → chunk-WUHRVSWI.ts} +87 -37
- package/dist/chunks/chunk-WUHRVSWI.ts.map +7 -0
- package/dist/chunks/{chunk-OVBAW7KV.ts → chunk-XZ3ZTHKR.ts} +19 -9
- package/dist/chunks/chunk-XZ3ZTHKR.ts.map +7 -0
- package/dist/chunks/{chunk-S5OCYVPS.ts → chunk-YEULK7MK.ts} +3 -3
- package/dist/chunks/{command-handler-KCB6MCWS.ts → command-handler-SDGKIQH2.ts} +17 -14
- package/dist/chunks/command-handler-SDGKIQH2.ts.map +7 -0
- package/dist/chunks/{config-HYG7G5EM.ts → config-J5VZUY4J.ts} +4 -3
- package/dist/chunks/{conflict-artifacts-J6WSDCWP.ts → conflict-artifacts-MYSH3L2J.ts} +10 -9
- package/dist/chunks/{conflict-review-GXA6Y7KY.ts → conflict-review-J6ZSPNWL.ts} +21 -20
- package/dist/chunks/{conflict-review-GXA6Y7KY.ts.map → conflict-review-J6ZSPNWL.ts.map} +1 -1
- package/dist/chunks/{continuous-record-EPHGR2DU.ts → continuous-record-OM64J27R.ts} +4 -4
- package/dist/chunks/{continuous-sync-FAFNLEUW.ts → continuous-sync-BH5SWIRV.ts} +22 -17
- package/dist/chunks/continuous-sync-BH5SWIRV.ts.map +7 -0
- package/dist/chunks/{file-selection-IELTXSGZ.ts → file-selection-7HEVPO4T.ts} +11 -8
- package/dist/chunks/file-selection-7HEVPO4T.ts.map +7 -0
- package/dist/chunks/{git-backend-KHD36W27.ts → git-backend-6CKI6CUG.ts} +12 -15
- package/dist/chunks/{git-backend-KHD36W27.ts.map → git-backend-6CKI6CUG.ts.map} +2 -2
- package/dist/chunks/{local-fields-JEKAPZKD.ts → local-fields-SR7PH66U.ts} +6 -5
- package/dist/chunks/{manager-result-dispatcher-XCYPW7RC.ts → manager-result-dispatcher-CXUP5WDP.ts} +15 -14
- package/dist/chunks/{manager-ui-FMZXKPX2.ts → manager-ui-65ZETN6F.ts} +168 -51
- package/dist/chunks/manager-ui-65ZETN6F.ts.map +7 -0
- package/dist/chunks/{merge-baseline-store-65GKUMXI.ts → merge-baseline-store-4Y2X7P2D.ts} +4 -3
- package/dist/chunks/{remote-selection-ui-MYM2YCPF.ts → remote-selection-ui-W55BQDJQ.ts} +14 -13
- package/dist/chunks/{s3-backend-L24UZ6IK.ts → s3-backend-FDK7JSDQ.ts} +8 -7
- package/dist/chunks/{s3-backend-L24UZ6IK.ts.map → s3-backend-FDK7JSDQ.ts.map} +1 -1
- package/dist/chunks/{setup-switch-KLGESLQX.ts → setup-switch-AB27ADDI.ts} +5 -4
- package/dist/chunks/{setup-wizard-ZQAK35BP.ts → setup-wizard-5FILKFLQ.ts} +6 -5
- package/dist/chunks/{setup-wizard-ZQAK35BP.ts.map → setup-wizard-5FILKFLQ.ts.map} +1 -1
- package/dist/chunks/{snapshot-RGNDYT6A.ts → snapshot-G3W2GOGB.ts} +5 -4
- package/dist/chunks/{snapshot-transaction-26BQO36S.ts → snapshot-transaction-SGRUWT73.ts} +6 -5
- package/dist/chunks/{sync-inspection-MOMLGUTD.ts → sync-inspection-MDYHZHT7.ts} +10 -9
- package/dist/chunks/{sync-operations-45APGOL3.ts → sync-operations-ZAUP65FH.ts} +40 -29
- package/dist/chunks/sync-operations-ZAUP65FH.ts.map +7 -0
- package/dist/chunks/{sync-state-7L4NT7W2.ts → sync-state-H4TFZZ7S.ts} +7 -6
- package/dist/chunks/{webdav-backend-3ZCACILP.ts → webdav-backend-UNMIAXM5.ts} +7 -6
- package/dist/chunks/{webdav-backend-3ZCACILP.ts.map → webdav-backend-UNMIAXM5.ts.map} +1 -1
- package/dist/index.ts +26 -19
- package/dist/index.ts.map +2 -2
- package/docs/exclude-policy-audit.md +54 -0
- package/docs/exclude-review-ledger.md +36 -0
- package/docs/tui-contract.md +1 -1
- package/package.json +3 -2
- package/skills/configure-pi-sync/SKILL.md +4 -3
- package/skills/configure-pi-sync/references/configuration.md +72 -13
- package/src/backends/git/git-storage.ts +12 -11
- package/src/commands/command-execution.ts +2 -2
- package/src/commands/command-handler.ts +1 -0
- package/src/settings/config.ts +9 -3
- package/src/settings/settings-management.ts +4 -3
- package/src/settings/settings-types.ts +4 -1
- package/src/settings/settings-validation.ts +8 -4
- package/src/snapshot/session-paths.ts +1 -0
- package/src/snapshot/snapshot-apply.ts +23 -7
- package/src/snapshot/snapshot-codec.ts +12 -3
- package/src/snapshot/snapshot-exclude-safety.ts +44 -0
- package/src/snapshot/snapshot-transaction.ts +12 -0
- package/src/snapshot/snapshot-types.ts +4 -4
- package/src/snapshot/snapshot.ts +71 -30
- package/src/state/merge-baseline-store.ts +9 -1
- package/src/state/state-types.ts +1 -0
- package/src/sync/automatic-sync.ts +7 -2
- package/src/sync/continuous-watch.ts +4 -2
- package/src/sync/exclude-policy.ts +84 -0
- package/src/sync/local-fields.ts +15 -4
- package/src/sync/merge-apply.ts +6 -1
- package/src/sync/merge-journal.ts +12 -3
- package/src/sync/merged-sync.ts +17 -12
- package/src/sync/partial-progress.ts +1 -0
- package/src/sync/remote-snapshot.ts +7 -8
- package/src/sync/sync-decision.ts +11 -1
- package/src/sync/sync-inspection.ts +3 -1
- package/src/sync/sync-mutations.ts +11 -4
- package/src/sync/sync-policy.ts +101 -37
- package/src/sync/sync-queries.ts +13 -3
- package/src/sync/sync-state.ts +16 -10
- package/src/ui/excluded-paths-ui.ts +106 -0
- package/src/ui/file-selection.ts +6 -3
- package/src/ui/manager-state.ts +11 -5
- package/src/ui/remote-selection-ui.ts +73 -10
- package/src/ui/settings-ui.ts +20 -5
- package/src/ui/setup/setup-actions.ts +1 -1
- package/src/ui/setup/setup-switcher.ts +3 -2
- package/src/ui/storage-connections-ui.ts +2 -2
- package/src/ui/sync-attention.ts +46 -10
- package/src/ui/sync-format.ts +13 -1
- package/dist/chunks/chunk-6LD5KX55.ts.map +0 -7
- package/dist/chunks/chunk-7A35ZAVO.ts.map +0 -7
- package/dist/chunks/chunk-BEST45H2.ts.map +0 -7
- package/dist/chunks/chunk-ESKOPQVB.ts.map +0 -7
- package/dist/chunks/chunk-FLHCOJCD.ts.map +0 -7
- package/dist/chunks/chunk-KYTT7KV4.ts.map +0 -7
- package/dist/chunks/chunk-MG66XM5U.ts.map +0 -7
- package/dist/chunks/chunk-MKSMJPVA.ts.map +0 -7
- package/dist/chunks/chunk-MMQIEDZ6.ts.map +0 -7
- package/dist/chunks/chunk-MW6KUQYA.ts.map +0 -7
- package/dist/chunks/chunk-OVBAW7KV.ts.map +0 -7
- package/dist/chunks/chunk-TIMWN2RH.ts.map +0 -7
- package/dist/chunks/chunk-Y4UVXOLQ.ts.map +0 -7
- package/dist/chunks/command-handler-KCB6MCWS.ts.map +0 -7
- package/dist/chunks/continuous-sync-FAFNLEUW.ts.map +0 -7
- package/dist/chunks/file-selection-IELTXSGZ.ts.map +0 -7
- package/dist/chunks/manager-ui-FMZXKPX2.ts.map +0 -7
- package/dist/chunks/sync-operations-45APGOL3.ts.map +0 -7
- /package/dist/chunks/{agent-candidate-U2NU3GUX.ts.map → agent-candidate-ZHN7FTZT.ts.map} +0 -0
- /package/dist/chunks/{agent-store-7GINKTS7.ts.map → agent-store-CCKN4DPR.ts.map} +0 -0
- /package/dist/chunks/{agent-sync-2R4GG2MF.ts.map → agent-sync-EGA2I5W7.ts.map} +0 -0
- /package/dist/chunks/{chunk-CQD2TJHE.ts.map → chunk-25QCU3LY.ts.map} +0 -0
- /package/dist/chunks/{chunk-OJARL2TG.ts.map → chunk-2UKUKIBI.ts.map} +0 -0
- /package/dist/chunks/{chunk-DN4IA7MR.ts.map → chunk-4NVHHDN2.ts.map} +0 -0
- /package/dist/chunks/{chunk-AKMV47YC.ts.map → chunk-6OFMB6KK.ts.map} +0 -0
- /package/dist/chunks/{chunk-XEODG2PE.ts.map → chunk-76HID27T.ts.map} +0 -0
- /package/dist/chunks/{chunk-U6GXGY5G.ts.map → chunk-DTPELCPW.ts.map} +0 -0
- /package/dist/chunks/{chunk-TZC7GDJE.ts.map → chunk-ILXMZ2HU.ts.map} +0 -0
- /package/dist/chunks/{chunk-JYILGR3K.ts.map → chunk-PQ75RPUP.ts.map} +0 -0
- /package/dist/chunks/{chunk-WQMYV4DW.ts.map → chunk-RDOX3VH5.ts.map} +0 -0
- /package/dist/chunks/{chunk-6O3EDBRL.ts.map → chunk-S7D6XWHI.ts.map} +0 -0
- /package/dist/chunks/{chunk-GNYGNHFF.ts.map → chunk-T2TYBLXK.ts.map} +0 -0
- /package/dist/chunks/{chunk-5TSCBBPP.ts.map → chunk-VPODHKOX.ts.map} +0 -0
- /package/dist/chunks/{chunk-S5OCYVPS.ts.map → chunk-YEULK7MK.ts.map} +0 -0
- /package/dist/chunks/{config-HYG7G5EM.ts.map → config-J5VZUY4J.ts.map} +0 -0
- /package/dist/chunks/{conflict-artifacts-J6WSDCWP.ts.map → conflict-artifacts-MYSH3L2J.ts.map} +0 -0
- /package/dist/chunks/{continuous-record-EPHGR2DU.ts.map → continuous-record-OM64J27R.ts.map} +0 -0
- /package/dist/chunks/{local-fields-JEKAPZKD.ts.map → local-fields-SR7PH66U.ts.map} +0 -0
- /package/dist/chunks/{manager-result-dispatcher-XCYPW7RC.ts.map → manager-result-dispatcher-CXUP5WDP.ts.map} +0 -0
- /package/dist/chunks/{merge-baseline-store-65GKUMXI.ts.map → merge-baseline-store-4Y2X7P2D.ts.map} +0 -0
- /package/dist/chunks/{remote-selection-ui-MYM2YCPF.ts.map → remote-selection-ui-W55BQDJQ.ts.map} +0 -0
- /package/dist/chunks/{setup-switch-KLGESLQX.ts.map → setup-switch-AB27ADDI.ts.map} +0 -0
- /package/dist/chunks/{snapshot-RGNDYT6A.ts.map → snapshot-G3W2GOGB.ts.map} +0 -0
- /package/dist/chunks/{snapshot-transaction-26BQO36S.ts.map → snapshot-transaction-SGRUWT73.ts.map} +0 -0
- /package/dist/chunks/{sync-inspection-MOMLGUTD.ts.map → sync-inspection-MDYHZHT7.ts.map} +0 -0
- /package/dist/chunks/{sync-state-7L4NT7W2.ts.map → sync-state-H4TFZZ7S.ts.map} +0 -0
|
@@ -6,7 +6,7 @@ import {
|
|
|
6
6
|
} from "./chunk-5SFMBY5P.ts";
|
|
7
7
|
import {
|
|
8
8
|
withLock
|
|
9
|
-
} from "./chunk-
|
|
9
|
+
} from "./chunk-S7D6XWHI.ts";
|
|
10
10
|
import {
|
|
11
11
|
mergePathIdentity
|
|
12
12
|
} from "./chunk-XJHQPB3E.ts";
|
|
@@ -22,17 +22,23 @@ import {
|
|
|
22
22
|
configuredSessionDir,
|
|
23
23
|
expandSessionDir,
|
|
24
24
|
sessionStorageRoot
|
|
25
|
-
} from "./chunk-
|
|
25
|
+
} from "./chunk-LA3HQMFG.ts";
|
|
26
|
+
import {
|
|
27
|
+
compileSyncExclude,
|
|
28
|
+
normalizeSyncExclude,
|
|
29
|
+
sameSyncExclude
|
|
30
|
+
} from "./chunk-3JTZF7Z5.ts";
|
|
26
31
|
import {
|
|
27
32
|
assertWithinRoot,
|
|
28
33
|
isDeniedPath,
|
|
29
|
-
isPathInside
|
|
34
|
+
isPathInside,
|
|
35
|
+
toPosix
|
|
30
36
|
} from "./chunk-FHE2DCA2.ts";
|
|
31
37
|
|
|
32
38
|
// src/snapshot/snapshot-transaction.ts
|
|
33
39
|
import { createHash as createHash2, randomUUID } from "node:crypto";
|
|
34
|
-
import
|
|
35
|
-
import
|
|
40
|
+
import fs5 from "node:fs/promises";
|
|
41
|
+
import path7 from "node:path";
|
|
36
42
|
|
|
37
43
|
// src/state/mutation-directory-sync.ts
|
|
38
44
|
import path from "node:path";
|
|
@@ -131,9 +137,46 @@ async function coalesceCaseReplacements(root, plan) {
|
|
|
131
137
|
};
|
|
132
138
|
}
|
|
133
139
|
|
|
134
|
-
// src/snapshot/snapshot-
|
|
140
|
+
// src/snapshot/snapshot-exclude-safety.ts
|
|
135
141
|
import fs2 from "node:fs/promises";
|
|
136
142
|
import path3 from "node:path";
|
|
143
|
+
async function assertUnexcludedTargets(root, targets, options) {
|
|
144
|
+
if (!options.exclude?.length) return;
|
|
145
|
+
const excluded = compileSyncExclude(options.exclude);
|
|
146
|
+
const sessionRoot = sessionStorageRoot(root, options.sessionDir);
|
|
147
|
+
let visited = 0;
|
|
148
|
+
const validate = () => {
|
|
149
|
+
options.signal?.throwIfAborted();
|
|
150
|
+
options.validateMutation?.();
|
|
151
|
+
};
|
|
152
|
+
async function visit(target) {
|
|
153
|
+
validate();
|
|
154
|
+
if (++visited > 16384)
|
|
155
|
+
throw new Error("Exclude safety preflight exceeds its path bound; review a smaller transfer.");
|
|
156
|
+
const candidates = [];
|
|
157
|
+
if (isPathInside(root, target)) candidates.push(toPosix(path3.relative(root, target)));
|
|
158
|
+
if (isPathInside(sessionRoot, target)) candidates.push(`sessions/${toPosix(path3.relative(sessionRoot, target))}`);
|
|
159
|
+
if (candidates.some(excluded))
|
|
160
|
+
throw new Error("Transfer would replace an excluded path or directory descendant; no replacement is authorized.");
|
|
161
|
+
try {
|
|
162
|
+
const stat = await fs2.lstat(target);
|
|
163
|
+
validate();
|
|
164
|
+
if (!stat.isDirectory() || stat.isSymbolicLink()) return;
|
|
165
|
+
const names = await fs2.readdir(target);
|
|
166
|
+
validate();
|
|
167
|
+
for (const name of names) await visit(path3.join(target, name));
|
|
168
|
+
} catch (error) {
|
|
169
|
+
validate();
|
|
170
|
+
const code = error.code;
|
|
171
|
+
if (code !== "ENOENT" && code !== "ENOTDIR") throw error;
|
|
172
|
+
}
|
|
173
|
+
}
|
|
174
|
+
for (const target of new Set(targets)) await visit(target);
|
|
175
|
+
}
|
|
176
|
+
|
|
177
|
+
// src/snapshot/snapshot-mutation-queues.ts
|
|
178
|
+
import fs3 from "node:fs/promises";
|
|
179
|
+
import path4 from "node:path";
|
|
137
180
|
import { withFileMutationQueue } from "@earendil-works/pi-coding-agent";
|
|
138
181
|
function assertCurrent(options) {
|
|
139
182
|
options.validateMutation?.();
|
|
@@ -146,7 +189,7 @@ function isMissing(error) {
|
|
|
146
189
|
async function targetMissing(target, options) {
|
|
147
190
|
assertCurrent(options);
|
|
148
191
|
try {
|
|
149
|
-
await
|
|
192
|
+
await fs3.lstat(target);
|
|
150
193
|
assertCurrent(options);
|
|
151
194
|
return false;
|
|
152
195
|
} catch (error) {
|
|
@@ -158,31 +201,31 @@ async function targetMissing(target, options) {
|
|
|
158
201
|
async function queueKey(target, options) {
|
|
159
202
|
assertCurrent(options);
|
|
160
203
|
try {
|
|
161
|
-
const key = await
|
|
204
|
+
const key = await fs3.realpath(target);
|
|
162
205
|
assertCurrent(options);
|
|
163
206
|
return key;
|
|
164
207
|
} catch (error) {
|
|
165
208
|
assertCurrent(options);
|
|
166
209
|
if (!isMissing(error)) throw error;
|
|
167
|
-
return
|
|
210
|
+
return path4.resolve(target);
|
|
168
211
|
}
|
|
169
212
|
}
|
|
170
213
|
async function assertCanonicalParent(target, options) {
|
|
171
|
-
const parent =
|
|
214
|
+
const parent = path4.dirname(path4.resolve(target));
|
|
172
215
|
const suffix = [];
|
|
173
|
-
for (let current = parent; ; current =
|
|
216
|
+
for (let current = parent; ; current = path4.dirname(current)) {
|
|
174
217
|
assertCurrent(options);
|
|
175
218
|
let canonical;
|
|
176
219
|
try {
|
|
177
|
-
canonical = await
|
|
220
|
+
canonical = await fs3.realpath(current);
|
|
178
221
|
} catch (error) {
|
|
179
222
|
assertCurrent(options);
|
|
180
|
-
if (!isMissing(error) ||
|
|
181
|
-
suffix.unshift(
|
|
223
|
+
if (!isMissing(error) || path4.dirname(current) === current) throw error;
|
|
224
|
+
suffix.unshift(path4.basename(current));
|
|
182
225
|
continue;
|
|
183
226
|
}
|
|
184
227
|
assertCurrent(options);
|
|
185
|
-
if (
|
|
228
|
+
if (path4.join(canonical, ...suffix) !== parent)
|
|
186
229
|
throw new Error(
|
|
187
230
|
"Snapshot mutation through an aliased parent is unsafe; use the canonical agent/session path. Transaction evidence retained for review."
|
|
188
231
|
);
|
|
@@ -228,7 +271,7 @@ async function withMissingTargetQueues(targets, heldKeys, options, action) {
|
|
|
228
271
|
throw new Error("Snapshot target changed before destination queue reservation; evidence retained.");
|
|
229
272
|
}
|
|
230
273
|
};
|
|
231
|
-
const keys = unique.map((target) =>
|
|
274
|
+
const keys = unique.map((target) => path4.resolve(target)).filter((key) => !heldKeys.has(key)).sort();
|
|
232
275
|
await requireAbsent();
|
|
233
276
|
const combined = /* @__PURE__ */ new Set([...heldKeys, ...keys]);
|
|
234
277
|
async function acquire(index) {
|
|
@@ -243,12 +286,12 @@ async function withMissingTargetQueues(targets, heldKeys, options, action) {
|
|
|
243
286
|
}
|
|
244
287
|
|
|
245
288
|
// src/snapshot/snapshot-root-transition.ts
|
|
246
|
-
import
|
|
247
|
-
import
|
|
289
|
+
import fs4 from "node:fs/promises";
|
|
290
|
+
import path6 from "node:path";
|
|
248
291
|
|
|
249
292
|
// src/snapshot/snapshot-transaction-plan.ts
|
|
250
293
|
import { createHash } from "node:crypto";
|
|
251
|
-
import
|
|
294
|
+
import path5 from "node:path";
|
|
252
295
|
function fileImage(content) {
|
|
253
296
|
return `file:${createHash("sha256").update(content).digest("hex")}`;
|
|
254
297
|
}
|
|
@@ -261,7 +304,7 @@ function indexTransactionPlan(plan, targets) {
|
|
|
261
304
|
if (!writeImages.has(write.target)) writeImages.set(write.target, image2);
|
|
262
305
|
for (const ancestor of ancestorTargets(write.target, targetIndex, false)) {
|
|
263
306
|
if (ancestor !== write.target && isStrictDescendant(ancestor, write.target)) {
|
|
264
|
-
postFiles.get(ancestor)?.push({ relative:
|
|
307
|
+
postFiles.get(ancestor)?.push({ relative: path5.relative(ancestor, write.target), image: image2 });
|
|
265
308
|
}
|
|
266
309
|
}
|
|
267
310
|
}
|
|
@@ -285,10 +328,10 @@ function indexTransactionPlan(plan, targets) {
|
|
|
285
328
|
return { writeImages, postFiles, deletes, deletedWrites };
|
|
286
329
|
}
|
|
287
330
|
function isStrictDescendant(parent, target) {
|
|
288
|
-
return
|
|
331
|
+
return path5.relative(path5.resolve(parent), path5.resolve(target)) !== "" && isPathInside(parent, target);
|
|
289
332
|
}
|
|
290
333
|
function pathKey(target) {
|
|
291
|
-
const resolved =
|
|
334
|
+
const resolved = path5.resolve(target);
|
|
292
335
|
return process.platform === "win32" ? resolved.toLowerCase() : resolved;
|
|
293
336
|
}
|
|
294
337
|
function indexTargets(targets) {
|
|
@@ -302,10 +345,10 @@ function indexTargets(targets) {
|
|
|
302
345
|
return index;
|
|
303
346
|
}
|
|
304
347
|
function* ancestorTargets(target, index, includeSelf = true) {
|
|
305
|
-
const resolved =
|
|
306
|
-
for (let ancestor = includeSelf ? resolved :
|
|
348
|
+
const resolved = path5.resolve(target);
|
|
349
|
+
for (let ancestor = includeSelf ? resolved : path5.dirname(resolved); ; ancestor = path5.dirname(ancestor)) {
|
|
307
350
|
yield* index.get(pathKey(ancestor)) ?? [];
|
|
308
|
-
if (
|
|
351
|
+
if (path5.dirname(ancestor) === ancestor) return;
|
|
309
352
|
}
|
|
310
353
|
}
|
|
311
354
|
|
|
@@ -325,36 +368,36 @@ function rootFromSettings(root, bytes) {
|
|
|
325
368
|
throw new Error("Invalid settings root-transition evidence.");
|
|
326
369
|
const value = settings.sessionDir;
|
|
327
370
|
if (value !== void 0 && typeof value !== "string") throw new Error("Invalid settings root-transition evidence.");
|
|
328
|
-
return
|
|
371
|
+
return path6.resolve(sessionStorageRoot(root, value ? expandSessionDir(value) : void 0));
|
|
329
372
|
}
|
|
330
373
|
async function beforeRoot(directory, root, entry, guards) {
|
|
331
374
|
validateOwner(guards);
|
|
332
|
-
if (entry.kind === "missing" && entry.beforeImage === "missing") return
|
|
375
|
+
if (entry.kind === "missing" && entry.beforeImage === "missing") return path6.join(root, "sessions");
|
|
333
376
|
if (entry.kind !== "file") throw new Error("Root transition requires regular settings preimage evidence.");
|
|
334
|
-
const backup =
|
|
335
|
-
const stat = await
|
|
377
|
+
const backup = path6.join(directory, "before", entry.backupName);
|
|
378
|
+
const stat = await fs4.lstat(backup);
|
|
336
379
|
validateOwner(guards);
|
|
337
380
|
if (!stat.isFile()) throw new Error("Invalid settings root-transition backup.");
|
|
338
|
-
const bytes = await
|
|
381
|
+
const bytes = await fs4.readFile(backup);
|
|
339
382
|
validateOwner(guards);
|
|
340
383
|
if (fileImage(bytes) !== entry.beforeImage) throw new Error("Changed settings root-transition backup.");
|
|
341
384
|
return rootFromSettings(root, bytes);
|
|
342
385
|
}
|
|
343
386
|
async function prepareSessionRootTransition(directory, root, sessionRoot, entries, plan, guards = {}) {
|
|
344
|
-
const settingsTarget =
|
|
387
|
+
const settingsTarget = path6.join(root, "settings.json");
|
|
345
388
|
const entry = entries.find((item) => item.target === settingsTarget || item.afterTarget === settingsTarget);
|
|
346
389
|
const write = plan.writes.find((item) => item.target === settingsTarget || item.target === entry?.target);
|
|
347
390
|
if (!entry || !entries.some((item) => item !== entry && isPathInside(sessionRoot, item.target))) return void 0;
|
|
348
391
|
const deleted = !write && plan.deletes.includes(entry.target) && entry.afterImage === "missing";
|
|
349
392
|
if (!write && !deleted) return void 0;
|
|
350
|
-
const afterRoot = write ? rootFromSettings(root, write.content) :
|
|
393
|
+
const afterRoot = write ? rootFromSettings(root, write.content) : path6.join(root, "sessions");
|
|
351
394
|
if (afterRoot !== sessionRoot) return void 0;
|
|
352
395
|
const previous = await beforeRoot(directory, root, entry, guards);
|
|
353
396
|
if (previous === sessionRoot) return void 0;
|
|
354
397
|
if (write && fileImage(write.content) !== entry.afterImage) throw new Error("Changed settings transition postimage.");
|
|
355
398
|
if (!write) return { beforeRoot: previous, settingsAfterBase64: null };
|
|
356
399
|
validateOwner(guards);
|
|
357
|
-
const handle = await
|
|
400
|
+
const handle = await fs4.open(path6.join(directory, "settings-after"), "wx", 384);
|
|
358
401
|
try {
|
|
359
402
|
validateOwner(guards);
|
|
360
403
|
await handle.writeFile(write.content);
|
|
@@ -369,25 +412,25 @@ async function prepareSessionRootTransition(directory, root, sessionRoot, entrie
|
|
|
369
412
|
return { beforeRoot: previous, settingsAfterFile: true };
|
|
370
413
|
}
|
|
371
414
|
function validateSessionRootTransition(value) {
|
|
372
|
-
if (!value || typeof value.beforeRoot !== "string" || !
|
|
415
|
+
if (!value || typeof value.beforeRoot !== "string" || !path6.isAbsolute(value.beforeRoot) || path6.resolve(value.beforeRoot) !== value.beforeRoot || value.settingsAfterFile !== void 0 && value.settingsAfterFile !== true || (value.settingsAfterFile === true ? value.settingsAfterBase64 !== void 0 : value.settingsAfterBase64 !== null && (typeof value.settingsAfterBase64 !== "string" || value.settingsAfterBase64.length > 32 * 1024 * 1024 || Buffer.from(value.settingsAfterBase64, "base64").toString("base64") !== value.settingsAfterBase64)))
|
|
373
416
|
throw new Error("Invalid session root-transition evidence.");
|
|
374
417
|
}
|
|
375
418
|
async function resolveTransitionSessionRoot(directory, root, sessionRoot, transition, entries, currentRoot, guards = {}) {
|
|
376
|
-
const settingsTarget =
|
|
419
|
+
const settingsTarget = path6.join(root, "settings.json");
|
|
377
420
|
const entry = entries.find((item) => item.target === settingsTarget || item.afterTarget === settingsTarget);
|
|
378
421
|
if (!entry) throw new Error("Missing settings root-transition evidence.");
|
|
379
422
|
let after;
|
|
380
423
|
if (transition.settingsAfterFile) {
|
|
381
|
-
const file =
|
|
382
|
-
const stat = await
|
|
424
|
+
const file = path6.join(directory, "settings-after");
|
|
425
|
+
const stat = await fs4.lstat(file);
|
|
383
426
|
validateOwner(guards);
|
|
384
427
|
if (!stat.isFile() || stat.isSymbolicLink() || stat.nlink !== 1 || process.platform !== "win32" && (stat.mode & 63) !== 0)
|
|
385
428
|
throw new Error("Unsafe settings root-transition postimage.");
|
|
386
|
-
after = await
|
|
429
|
+
after = await fs4.readFile(file);
|
|
387
430
|
validateOwner(guards);
|
|
388
431
|
} else if (transition.settingsAfterBase64 !== null)
|
|
389
432
|
after = Buffer.from(transition.settingsAfterBase64, "base64");
|
|
390
|
-
if ((after ? fileImage(after) : "missing") !== entry.afterImage || (after ? rootFromSettings(root, after) :
|
|
433
|
+
if ((after ? fileImage(after) : "missing") !== entry.afterImage || (after ? rootFromSettings(root, after) : path6.join(root, "sessions")) !== sessionRoot || await beforeRoot(directory, root, entry, guards) !== transition.beforeRoot)
|
|
391
434
|
throw new Error("Inconsistent settings root-transition evidence.");
|
|
392
435
|
if (currentRoot !== transition.beforeRoot && currentRoot !== sessionRoot)
|
|
393
436
|
throw new Error("Transaction session root is not owned by this context; preserve evidence for review.");
|
|
@@ -399,13 +442,14 @@ var JOURNAL_VERSION = 4;
|
|
|
399
442
|
async function applySnapshotTransaction(plan, options = {}) {
|
|
400
443
|
options.validateMutation?.();
|
|
401
444
|
await recoverPendingSnapshotTransactions(options);
|
|
402
|
-
const coalesced = await coalesceCaseReplacements(
|
|
445
|
+
const coalesced = await coalesceCaseReplacements(path7.resolve(agentDir()), plan);
|
|
403
446
|
options.validateMutation?.();
|
|
404
447
|
options.signal?.throwIfAborted();
|
|
405
448
|
const targets = [.../* @__PURE__ */ new Set([...coalesced.plan.deletes, ...coalesced.plan.writes.map((item) => item.target)])].sort();
|
|
406
449
|
if (targets.length > 16384)
|
|
407
450
|
throw new Error("Snapshot transaction exceeds its target bound; review a smaller transfer.");
|
|
408
451
|
return withTargetQueues(targets, options, async (heldKeys) => {
|
|
452
|
+
await assertUnexcludedTargets(agentDir(), targets, options);
|
|
409
453
|
options.signal?.throwIfAborted();
|
|
410
454
|
if (targets.some(
|
|
411
455
|
(target) => options.protectedTargets?.some(
|
|
@@ -429,7 +473,7 @@ async function applySnapshotTransaction(plan, options = {}) {
|
|
|
429
473
|
for (const [target, expected] of options.expectedPreimages ?? []) {
|
|
430
474
|
if (originalTargets.has(target)) continue;
|
|
431
475
|
const variants = coalesced.plan.deletes.filter(
|
|
432
|
-
(candidate) => candidate !== target &&
|
|
476
|
+
(candidate) => candidate !== target && path7.dirname(candidate) === path7.dirname(target) && mergePathIdentity(candidate) === mergePathIdentity(target)
|
|
433
477
|
);
|
|
434
478
|
if (variants.length > 1) throw new Error("Ambiguous reviewed file preimage spellings; review required.");
|
|
435
479
|
const original = variants[0];
|
|
@@ -470,7 +514,7 @@ async function applySnapshotTransaction(plan, options = {}) {
|
|
|
470
514
|
const entry = entriesByTarget.get(target);
|
|
471
515
|
if (!entry) throw new Error("Unowned transaction target.");
|
|
472
516
|
await removeOwnedTarget(transaction.directory, entry, transaction.journal, options);
|
|
473
|
-
await syncDirectory(
|
|
517
|
+
await syncDirectory(path7.dirname(target));
|
|
474
518
|
}
|
|
475
519
|
await withMissingTargetQueues(
|
|
476
520
|
transaction.journal.entries.filter((entry) => entry.removalPending).flatMap((entry) => [entry.target, ...entry.afterTarget ? [entry.afterTarget] : []]),
|
|
@@ -494,19 +538,19 @@ async function applySnapshotTransaction(plan, options = {}) {
|
|
|
494
538
|
await verifyTarget(transaction.directory, entry, transaction.journal, deletedByThisCall);
|
|
495
539
|
options.validateMutation?.();
|
|
496
540
|
options.signal?.throwIfAborted();
|
|
497
|
-
await
|
|
541
|
+
await fs5.mkdir(path7.dirname(item.target), { recursive: true });
|
|
498
542
|
options.validateMutation?.();
|
|
499
543
|
options.signal?.throwIfAborted();
|
|
500
|
-
const temp =
|
|
544
|
+
const temp = path7.join(path7.dirname(item.target), `.pi-sync.json.${randomUUID()}.apply`);
|
|
501
545
|
try {
|
|
502
546
|
let mode = 384;
|
|
503
547
|
try {
|
|
504
|
-
const stat = await
|
|
548
|
+
const stat = await fs5.lstat(item.target);
|
|
505
549
|
if (stat.isFile()) mode = stat.mode & 511;
|
|
506
550
|
} catch (error) {
|
|
507
551
|
if (error.code !== "ENOENT") throw error;
|
|
508
552
|
}
|
|
509
|
-
const handle = await
|
|
553
|
+
const handle = await fs5.open(temp, "wx", mode);
|
|
510
554
|
try {
|
|
511
555
|
await handle.writeFile(item.content);
|
|
512
556
|
await handle.sync();
|
|
@@ -516,10 +560,10 @@ async function applySnapshotTransaction(plan, options = {}) {
|
|
|
516
560
|
await verifyTarget(transaction.directory, entry, transaction.journal, deletedByThisCall);
|
|
517
561
|
options.signal?.throwIfAborted();
|
|
518
562
|
options.validateMutation?.();
|
|
519
|
-
await
|
|
520
|
-
await syncDirectory(
|
|
563
|
+
await fs5.rename(temp, item.target);
|
|
564
|
+
await syncDirectory(path7.dirname(item.target));
|
|
521
565
|
} finally {
|
|
522
|
-
await
|
|
566
|
+
await fs5.rm(temp, { force: true });
|
|
523
567
|
}
|
|
524
568
|
}
|
|
525
569
|
options.validateMutation?.();
|
|
@@ -568,13 +612,13 @@ async function recoverSnapshotTransactionsOnStartup(options = {}) {
|
|
|
568
612
|
async function recoverPendingSnapshotTransactions(options = {}) {
|
|
569
613
|
for (const entry of (await pendingTransactionEntries()).sort((a, b) => a.name.localeCompare(b.name))) {
|
|
570
614
|
if (!entry.isDirectory()) continue;
|
|
571
|
-
const directory =
|
|
615
|
+
const directory = path7.join(transactionRoot(), entry.name);
|
|
572
616
|
let journal;
|
|
573
617
|
try {
|
|
574
|
-
const file =
|
|
575
|
-
const stat = await
|
|
618
|
+
const file = path7.join(directory, "journal.json");
|
|
619
|
+
const stat = await fs5.lstat(file);
|
|
576
620
|
if (!stat.isFile() || stat.size > 32 * 1024 * 1024) throw new Error("Invalid journal file.");
|
|
577
|
-
journal = JSON.parse(await
|
|
621
|
+
journal = JSON.parse(await fs5.readFile(file, "utf8"));
|
|
578
622
|
} catch (error) {
|
|
579
623
|
if (error.code === "ENOENT") continue;
|
|
580
624
|
throw new Error("Cannot recover malformed pi-sync transaction; preserve private evidence for review.");
|
|
@@ -593,13 +637,15 @@ async function recoverPendingSnapshotTransactions(options = {}) {
|
|
|
593
637
|
journal.sessionRoot,
|
|
594
638
|
journal.sessionRootTransition,
|
|
595
639
|
journal.entries,
|
|
596
|
-
|
|
640
|
+
path7.resolve(sessionStorageRoot(journal.root, sessionDir)),
|
|
597
641
|
options
|
|
598
642
|
);
|
|
599
643
|
options.validateMutation?.();
|
|
600
644
|
options.signal?.throwIfAborted();
|
|
601
645
|
}
|
|
602
646
|
validateJournal(directory, journal, sessionDir);
|
|
647
|
+
if (!journal.completed && !sameSyncExclude(journal.exclude, options.exclude))
|
|
648
|
+
throw new Error("Pending transaction has a different exclude policy; preserve its backup for reviewed recovery.");
|
|
603
649
|
if (journal.completed) {
|
|
604
650
|
await removeTransaction(directory);
|
|
605
651
|
continue;
|
|
@@ -613,12 +659,12 @@ async function recoverPendingSnapshotTransactions(options = {}) {
|
|
|
613
659
|
}
|
|
614
660
|
async function prepareTransaction(plan, targets, options, replacements = /* @__PURE__ */ new Map()) {
|
|
615
661
|
const { sessionDir } = options;
|
|
616
|
-
const root =
|
|
617
|
-
const sessionRoot = sessionDir ?
|
|
662
|
+
const root = path7.resolve(agentDir());
|
|
663
|
+
const sessionRoot = sessionDir ? path7.resolve(sessionStorageRoot(root, sessionDir)) : void 0;
|
|
618
664
|
const indexed = indexTransactionPlan(plan, targets);
|
|
619
|
-
const directory =
|
|
620
|
-
const backupDirectory =
|
|
621
|
-
await
|
|
665
|
+
const directory = path7.join(transactionRoot(), randomUUID());
|
|
666
|
+
const backupDirectory = path7.join(directory, "before");
|
|
667
|
+
await fs5.mkdir(backupDirectory, { recursive: true, mode: 448 });
|
|
622
668
|
const entries = [];
|
|
623
669
|
for (const [index, target] of targets.entries()) {
|
|
624
670
|
options.signal?.throwIfAborted();
|
|
@@ -627,15 +673,15 @@ async function prepareTransaction(plan, targets, options, replacements = /* @__P
|
|
|
627
673
|
await assertSafeParents(root, sessionRoot, target);
|
|
628
674
|
const entry = { target, backupName: `${index}`, kind: "missing" };
|
|
629
675
|
if (replacements.has(target)) entry.afterTarget = replacements.get(target);
|
|
630
|
-
const backup =
|
|
676
|
+
const backup = path7.join(backupDirectory, entry.backupName);
|
|
631
677
|
try {
|
|
632
|
-
const stat = await
|
|
678
|
+
const stat = await fs5.lstat(target);
|
|
633
679
|
if (stat.isSymbolicLink()) {
|
|
634
680
|
entry.kind = "symlink";
|
|
635
|
-
entry.linkTarget = await
|
|
681
|
+
entry.linkTarget = await fs5.readlink(target);
|
|
636
682
|
} else if (stat.isDirectory()) {
|
|
637
683
|
entry.kind = "directory";
|
|
638
|
-
await
|
|
684
|
+
await fs5.cp(target, backup, {
|
|
639
685
|
recursive: true,
|
|
640
686
|
dereference: false,
|
|
641
687
|
verbatimSymlinks: true,
|
|
@@ -648,8 +694,8 @@ async function prepareTransaction(plan, targets, options, replacements = /* @__P
|
|
|
648
694
|
});
|
|
649
695
|
} else if (stat.isFile()) {
|
|
650
696
|
entry.kind = "file";
|
|
651
|
-
await
|
|
652
|
-
await
|
|
697
|
+
await fs5.copyFile(target, backup);
|
|
698
|
+
await fs5.chmod(backup, stat.mode);
|
|
653
699
|
} else throw new Error("Unsupported existing snapshot target.");
|
|
654
700
|
} catch (error) {
|
|
655
701
|
const code = error.code;
|
|
@@ -672,7 +718,7 @@ async function prepareTransaction(plan, targets, options, replacements = /* @__P
|
|
|
672
718
|
entries.push(entry);
|
|
673
719
|
}
|
|
674
720
|
await syncDirectory(backupDirectory);
|
|
675
|
-
const effectiveRoot = sessionRoot ??
|
|
721
|
+
const effectiveRoot = sessionRoot ?? path7.join(root, "sessions");
|
|
676
722
|
const sessionRootTransition = await prepareSessionRootTransition(
|
|
677
723
|
directory,
|
|
678
724
|
root,
|
|
@@ -684,16 +730,17 @@ async function prepareTransaction(plan, targets, options, replacements = /* @__P
|
|
|
684
730
|
const journal = {
|
|
685
731
|
version: sessionRootTransition ? 7 : replacements.size ? 5 : JOURNAL_VERSION,
|
|
686
732
|
root,
|
|
733
|
+
...options.exclude === void 0 ? {} : { exclude: normalizeSyncExclude(options.exclude) },
|
|
687
734
|
sessionRoot: sessionRootTransition ? effectiveRoot : sessionRoot,
|
|
688
735
|
sessionRootTransition,
|
|
689
736
|
entries
|
|
690
737
|
};
|
|
691
738
|
options.signal?.throwIfAborted();
|
|
692
739
|
options.validateMutation?.();
|
|
693
|
-
await writeJson(
|
|
740
|
+
await writeJson(path7.join(directory, "journal.json"), journal, { maxBytes: 32 * 1024 * 1024 });
|
|
694
741
|
await syncDirectory(transactionRoot());
|
|
695
742
|
await syncDirectory(stateDir());
|
|
696
|
-
await syncDirectory(
|
|
743
|
+
await syncDirectory(path7.dirname(stateDir()));
|
|
697
744
|
return { directory, journal, deletes: indexed.deletes, deletedWrites: indexed.deletedWrites };
|
|
698
745
|
}
|
|
699
746
|
async function restoreTransaction(directory, journal, options, heldKeys) {
|
|
@@ -735,7 +782,7 @@ async function restoreQueuedTransaction(directory, journal, options, heldKeys) {
|
|
|
735
782
|
await assertSafeParents(journal.root, journal.sessionRoot, entry.target);
|
|
736
783
|
options.validateMutation?.();
|
|
737
784
|
options.signal?.throwIfAborted();
|
|
738
|
-
await
|
|
785
|
+
await fs5.rm(recoveryStagePath(directory, entry), { recursive: true, force: true });
|
|
739
786
|
options.validateMutation?.();
|
|
740
787
|
options.signal?.throwIfAborted();
|
|
741
788
|
}
|
|
@@ -749,9 +796,9 @@ async function restoreQueuedTransaction(directory, journal, options, heldKeys) {
|
|
|
749
796
|
if (entry.kind === "file") {
|
|
750
797
|
const temporary2 = await prepareRecoveryStage(directory, journal, entry, options);
|
|
751
798
|
try {
|
|
752
|
-
await
|
|
753
|
-
await
|
|
754
|
-
const handle = await
|
|
799
|
+
await fs5.mkdir(path7.dirname(entry.target), { recursive: true });
|
|
800
|
+
await fs5.copyFile(path7.join(directory, "before", entry.backupName), temporary2);
|
|
801
|
+
const handle = await fs5.open(temporary2, process.platform === "win32" ? "r+" : "r");
|
|
755
802
|
try {
|
|
756
803
|
await handle.sync();
|
|
757
804
|
} finally {
|
|
@@ -780,12 +827,12 @@ async function restoreQueuedTransaction(directory, journal, options, heldKeys) {
|
|
|
780
827
|
options.validateMutation?.();
|
|
781
828
|
options.signal?.throwIfAborted();
|
|
782
829
|
if (atInstall === before && await spellingMatches()) return;
|
|
783
|
-
await
|
|
784
|
-
await syncDirectory(
|
|
830
|
+
await fs5.rename(temporary2, entry.target);
|
|
831
|
+
await syncDirectory(path7.dirname(entry.target));
|
|
785
832
|
}
|
|
786
833
|
);
|
|
787
834
|
} finally {
|
|
788
|
-
await
|
|
835
|
+
await fs5.rm(temporary2, { force: true });
|
|
789
836
|
}
|
|
790
837
|
continue;
|
|
791
838
|
}
|
|
@@ -795,10 +842,10 @@ async function restoreQueuedTransaction(directory, journal, options, heldKeys) {
|
|
|
795
842
|
}
|
|
796
843
|
const temporary = await prepareRecoveryStage(directory, journal, entry, options);
|
|
797
844
|
try {
|
|
798
|
-
await
|
|
845
|
+
await fs5.mkdir(path7.dirname(entry.target), { recursive: true });
|
|
799
846
|
await assertSafeParents(journal.root, journal.sessionRoot, entry.target);
|
|
800
847
|
if (entry.kind === "directory") {
|
|
801
|
-
await
|
|
848
|
+
await fs5.cp(path7.join(directory, "before", entry.backupName), temporary, {
|
|
802
849
|
recursive: true,
|
|
803
850
|
dereference: false,
|
|
804
851
|
verbatimSymlinks: true,
|
|
@@ -809,7 +856,7 @@ async function restoreQueuedTransaction(directory, journal, options, heldKeys) {
|
|
|
809
856
|
return true;
|
|
810
857
|
}
|
|
811
858
|
});
|
|
812
|
-
} else if (entry.linkTarget !== void 0) await
|
|
859
|
+
} else if (entry.linkTarget !== void 0) await fs5.symlink(entry.linkTarget, temporary);
|
|
813
860
|
if (await image(temporary, true) !== before)
|
|
814
861
|
throw new Error("Staged recovery image differs from the backup; evidence retained.");
|
|
815
862
|
options.validateMutation?.();
|
|
@@ -825,11 +872,11 @@ async function restoreQueuedTransaction(directory, journal, options, heldKeys) {
|
|
|
825
872
|
options.validateMutation?.();
|
|
826
873
|
options.signal?.throwIfAborted();
|
|
827
874
|
if (atInstall === before) return;
|
|
828
|
-
await
|
|
829
|
-
await syncDirectory(
|
|
875
|
+
await fs5.rename(temporary, entry.target);
|
|
876
|
+
await syncDirectory(path7.dirname(entry.target));
|
|
830
877
|
});
|
|
831
878
|
} finally {
|
|
832
|
-
await
|
|
879
|
+
await fs5.rm(temporary, { recursive: true, force: true });
|
|
833
880
|
}
|
|
834
881
|
}
|
|
835
882
|
await syncMutationParents(
|
|
@@ -863,6 +910,7 @@ async function verifyTarget(directory, entry, journal, deletedByThisCall = false
|
|
|
863
910
|
);
|
|
864
911
|
}
|
|
865
912
|
async function removeOwnedTarget(directory, entry, journal, options, restoringBefore) {
|
|
913
|
+
await assertUnexcludedTargets(journal.root, [entry.target], options);
|
|
866
914
|
const current = await verifyTarget(directory, entry, journal);
|
|
867
915
|
options.validateMutation?.();
|
|
868
916
|
options.signal?.throwIfAborted();
|
|
@@ -874,13 +922,13 @@ async function removeOwnedTarget(directory, entry, journal, options, restoringBe
|
|
|
874
922
|
affected.replacementStarted = false;
|
|
875
923
|
}
|
|
876
924
|
}
|
|
877
|
-
await writeJson(
|
|
925
|
+
await writeJson(path7.join(directory, "journal.json"), journal, { maxBytes: 32 * 1024 * 1024 });
|
|
878
926
|
const verified = await verifyTarget(directory, entry, journal);
|
|
879
927
|
options.validateMutation?.();
|
|
880
928
|
options.signal?.throwIfAborted();
|
|
881
929
|
if (restoringBefore !== void 0 && verified === restoringBefore) return false;
|
|
882
|
-
await
|
|
883
|
-
await syncDirectory(
|
|
930
|
+
await fs5.rm(entry.target, { recursive: true, force: true });
|
|
931
|
+
await syncDirectory(path7.dirname(entry.target));
|
|
884
932
|
return true;
|
|
885
933
|
}
|
|
886
934
|
function upgradeRemovalEvidence(journal) {
|
|
@@ -898,7 +946,7 @@ async function retireRemovalIntent(directory, journal, entries, options) {
|
|
|
898
946
|
entry.removalPending = false;
|
|
899
947
|
entry.replacementStarted = true;
|
|
900
948
|
}
|
|
901
|
-
await writeJson(
|
|
949
|
+
await writeJson(path7.join(directory, "journal.json"), journal, { maxBytes: 32 * 1024 * 1024 });
|
|
902
950
|
options.validateMutation?.();
|
|
903
951
|
options.signal?.throwIfAborted();
|
|
904
952
|
}
|
|
@@ -913,8 +961,8 @@ async function retireRestorationIntent(directory, journal, entry, options) {
|
|
|
913
961
|
}
|
|
914
962
|
function recoveryStagePath(directory, entry) {
|
|
915
963
|
const token = createHash2("sha256").update(directory).update("\\0").update(entry.backupName).digest("hex");
|
|
916
|
-
return
|
|
917
|
-
|
|
964
|
+
return path7.join(
|
|
965
|
+
path7.dirname(entry.target),
|
|
918
966
|
`.pi-sync.json.${token}.${entry.kind === "file" ? "restore" : "restore-tree"}`
|
|
919
967
|
);
|
|
920
968
|
}
|
|
@@ -922,7 +970,7 @@ async function prepareRecoveryStage(directory, journal, entry, options) {
|
|
|
922
970
|
const temporary = recoveryStagePath(directory, entry);
|
|
923
971
|
if (!entry.recoveryStaged) {
|
|
924
972
|
try {
|
|
925
|
-
await
|
|
973
|
+
await fs5.lstat(temporary);
|
|
926
974
|
throw new Error("Unowned recovery staging path; preserve evidence for review.");
|
|
927
975
|
} catch (error) {
|
|
928
976
|
if (error.code !== "ENOENT") throw error;
|
|
@@ -930,7 +978,7 @@ async function prepareRecoveryStage(directory, journal, entry, options) {
|
|
|
930
978
|
options.validateMutation?.();
|
|
931
979
|
options.signal?.throwIfAborted();
|
|
932
980
|
entry.recoveryStaged = true;
|
|
933
|
-
await writeJson(
|
|
981
|
+
await writeJson(path7.join(directory, "journal.json"), journal, { maxBytes: 32 * 1024 * 1024 });
|
|
934
982
|
options.validateMutation?.();
|
|
935
983
|
options.signal?.throwIfAborted();
|
|
936
984
|
}
|
|
@@ -939,24 +987,24 @@ async function prepareRecoveryStage(directory, journal, entry, options) {
|
|
|
939
987
|
async function beforeImage(directory, entry) {
|
|
940
988
|
if (entry.kind === "missing") return "missing";
|
|
941
989
|
if (entry.kind === "symlink") return `symlink:${entry.linkTarget}`;
|
|
942
|
-
const value = await image(
|
|
990
|
+
const value = await image(path7.join(directory, "before", entry.backupName));
|
|
943
991
|
if (value === "missing") throw new Error("Transaction backup is missing; preserve evidence for review.");
|
|
944
992
|
return value;
|
|
945
993
|
}
|
|
946
994
|
async function image(target, durable = false) {
|
|
947
995
|
let stat;
|
|
948
996
|
try {
|
|
949
|
-
stat = await
|
|
997
|
+
stat = await fs5.lstat(target);
|
|
950
998
|
} catch (error) {
|
|
951
999
|
const code = error.code;
|
|
952
1000
|
if (code === "ENOENT" || code === "ENOTDIR") return "missing";
|
|
953
1001
|
throw error;
|
|
954
1002
|
}
|
|
955
|
-
if (stat.isSymbolicLink()) return `symlink:${await
|
|
1003
|
+
if (stat.isSymbolicLink()) return `symlink:${await fs5.readlink(target)}`;
|
|
956
1004
|
if (stat.isFile()) {
|
|
957
|
-
const content = await
|
|
1005
|
+
const content = await fs5.readFile(target);
|
|
958
1006
|
if (durable) {
|
|
959
|
-
const handle = await
|
|
1007
|
+
const handle = await fs5.open(target, process.platform === "win32" ? "r+" : "r");
|
|
960
1008
|
try {
|
|
961
1009
|
await handle.sync();
|
|
962
1010
|
} finally {
|
|
@@ -967,8 +1015,8 @@ async function image(target, durable = false) {
|
|
|
967
1015
|
}
|
|
968
1016
|
if (!stat.isDirectory()) throw new Error("Unsupported transaction image.");
|
|
969
1017
|
const entries = [];
|
|
970
|
-
for (const name of (await
|
|
971
|
-
entries.push([name, await image(
|
|
1018
|
+
for (const name of (await fs5.readdir(target)).sort())
|
|
1019
|
+
entries.push([name, await image(path7.join(target, name), durable)]);
|
|
972
1020
|
if (durable) await syncDirectory(target);
|
|
973
1021
|
return `directory:${createHash2("sha256").update(JSON.stringify(entries)).digest("hex")}`;
|
|
974
1022
|
}
|
|
@@ -977,22 +1025,22 @@ async function ownedPostTree(target, files, requireComplete) {
|
|
|
977
1025
|
const expected = new Map(files.map((file) => [mergePathIdentity(file.relative), file.image]));
|
|
978
1026
|
const prefixes = /* @__PURE__ */ new Set();
|
|
979
1027
|
for (const relative of expected.keys()) {
|
|
980
|
-
for (let parent =
|
|
1028
|
+
for (let parent = path7.dirname(relative); parent !== "." && parent !== path7.dirname(parent); parent = path7.dirname(parent))
|
|
981
1029
|
prefixes.add(parent);
|
|
982
1030
|
}
|
|
983
1031
|
const seen = /* @__PURE__ */ new Set();
|
|
984
1032
|
async function visit(directory) {
|
|
985
1033
|
let stat;
|
|
986
1034
|
try {
|
|
987
|
-
stat = await
|
|
1035
|
+
stat = await fs5.lstat(directory);
|
|
988
1036
|
} catch {
|
|
989
1037
|
return false;
|
|
990
1038
|
}
|
|
991
1039
|
if (!stat.isDirectory() || stat.isSymbolicLink()) return false;
|
|
992
|
-
for (const name of await
|
|
993
|
-
const child =
|
|
994
|
-
const relative = mergePathIdentity(
|
|
995
|
-
const stat2 = await
|
|
1040
|
+
for (const name of await fs5.readdir(directory)) {
|
|
1041
|
+
const child = path7.join(directory, name);
|
|
1042
|
+
const relative = mergePathIdentity(path7.relative(target, child));
|
|
1043
|
+
const stat2 = await fs5.lstat(child);
|
|
996
1044
|
if (stat2.isDirectory() && !stat2.isSymbolicLink()) {
|
|
997
1045
|
if (!prefixes.has(relative) || !await visit(child)) return false;
|
|
998
1046
|
} else {
|
|
@@ -1007,25 +1055,28 @@ async function ownedPostTree(target, files, requireComplete) {
|
|
|
1007
1055
|
function validateJournal(directory, journal, sessionDir) {
|
|
1008
1056
|
if (![1, 2, 3, 4, 5, 6, 7].includes(journal.version) || !Array.isArray(journal.entries) || journal.entries.length > 16384)
|
|
1009
1057
|
throw new Error("Unsupported pi-sync transaction journal; preserve evidence for review.");
|
|
1058
|
+
if (journal.exclude !== void 0 && !Array.isArray(journal.exclude))
|
|
1059
|
+
throw new Error("Invalid transaction exclude policy; preserve evidence.");
|
|
1060
|
+
normalizeSyncExclude(journal.exclude);
|
|
1010
1061
|
if (journal.completed !== void 0 && (![6, 7].includes(journal.version) || journal.completed !== true))
|
|
1011
1062
|
throw new Error("Invalid transaction completion evidence.");
|
|
1012
|
-
const root =
|
|
1013
|
-
if (typeof journal.root !== "string" ||
|
|
1063
|
+
const root = path7.resolve(agentDir());
|
|
1064
|
+
if (typeof journal.root !== "string" || path7.resolve(journal.root) !== root)
|
|
1014
1065
|
throw new Error("Transaction root no longer matches the Pi agent directory.");
|
|
1015
1066
|
if (journal.sessionRootTransition !== void 0) {
|
|
1016
1067
|
if (journal.version !== 7 || typeof journal.sessionRoot !== "string")
|
|
1017
1068
|
throw new Error("Invalid session root-transition version.");
|
|
1018
1069
|
validateSessionRootTransition(journal.sessionRootTransition);
|
|
1019
1070
|
} else if (journal.version === 7) throw new Error("Missing session root-transition evidence.");
|
|
1020
|
-
const trustedSessionRoot = sessionDir ?
|
|
1021
|
-
if (journal.sessionRoot !== void 0 && (typeof journal.sessionRoot !== "string" || !
|
|
1071
|
+
const trustedSessionRoot = sessionDir ? path7.resolve(sessionStorageRoot(root, sessionDir)) : path7.join(root, "sessions");
|
|
1072
|
+
if (journal.sessionRoot !== void 0 && (typeof journal.sessionRoot !== "string" || !path7.isAbsolute(journal.sessionRoot) || path7.resolve(journal.sessionRoot) !== journal.sessionRoot || !journal.completed && transactionHasSessionTargets(journal) && journal.sessionRoot !== trustedSessionRoot))
|
|
1022
1073
|
throw new Error("Transaction session root is not owned by this context; preserve evidence for review.");
|
|
1023
1074
|
for (const entry of journal.entries) {
|
|
1024
|
-
if (!entry || typeof entry.target !== "string" || !
|
|
1075
|
+
if (!entry || typeof entry.target !== "string" || !path7.isAbsolute(entry.target) || path7.resolve(entry.target) !== entry.target || typeof entry.backupName !== "string" || !/^\d+$/u.test(entry.backupName) || !["missing", "file", "directory", "symlink"].includes(entry.kind))
|
|
1025
1076
|
throw new Error("Invalid pi-sync transaction entry.");
|
|
1026
|
-
assertWithinRoot(directory,
|
|
1077
|
+
assertWithinRoot(directory, path7.join(directory, "before", entry.backupName));
|
|
1027
1078
|
assertAllowedTarget(root, journal.sessionRoot, entry.target);
|
|
1028
|
-
if (entry.afterTarget !== void 0 && (![5, 6, 7].includes(journal.version) || typeof entry.afterTarget !== "string" || !isCaseReplacement(root, entry.target, entry.afterTarget) ||
|
|
1079
|
+
if (entry.afterTarget !== void 0 && (![5, 6, 7].includes(journal.version) || typeof entry.afterTarget !== "string" || !isCaseReplacement(root, entry.target, entry.afterTarget) || path7.resolve(entry.afterTarget) !== entry.afterTarget || entry.kind !== "file"))
|
|
1029
1080
|
throw new Error("Invalid transaction case replacement evidence.");
|
|
1030
1081
|
if (entry.afterTarget) assertAllowedTarget(root, journal.sessionRoot, entry.afterTarget);
|
|
1031
1082
|
if (journal.version >= 2 && ((entry.beforeImage === void 0 ? !journal.completed : typeof entry.beforeImage !== "string") || (entry.afterImage === void 0 ? !journal.completed : typeof entry.afterImage !== "string") || (entry.postFiles === void 0 ? !journal.completed : !Array.isArray(entry.postFiles))))
|
|
@@ -1039,29 +1090,29 @@ function validateJournal(directory, journal, sessionDir) {
|
|
|
1039
1090
|
if (entry.removalPending && entry.replacementStarted)
|
|
1040
1091
|
throw new Error("Conflicting transaction replacement evidence.");
|
|
1041
1092
|
for (const file of entry.postFiles ?? []) {
|
|
1042
|
-
if (typeof file.relative !== "string" || typeof file.image !== "string" || !isStrictlyInside(entry.target,
|
|
1093
|
+
if (typeof file.relative !== "string" || typeof file.image !== "string" || !isStrictlyInside(entry.target, path7.resolve(entry.target, file.relative)))
|
|
1043
1094
|
throw new Error("Invalid transaction subtree evidence.");
|
|
1044
1095
|
}
|
|
1045
1096
|
}
|
|
1046
1097
|
}
|
|
1047
1098
|
function transactionHasSessionTargets(journal) {
|
|
1048
|
-
const root =
|
|
1099
|
+
const root = path7.resolve(agentDir());
|
|
1049
1100
|
return Array.isArray(journal.entries) && journal.entries.some(
|
|
1050
|
-
(entry) => typeof entry?.target === "string" && (!isPathInside(root, entry.target) || isPathInside(
|
|
1101
|
+
(entry) => typeof entry?.target === "string" && (!isPathInside(root, entry.target) || isPathInside(path7.join(root, "sessions"), entry.target) || typeof journal.sessionRoot === "string" && isPathInside(journal.sessionRoot, entry.target))
|
|
1051
1102
|
);
|
|
1052
1103
|
}
|
|
1053
1104
|
function isStrictlyInside(root, target) {
|
|
1054
|
-
return
|
|
1105
|
+
return path7.relative(path7.resolve(root), path7.resolve(target)) !== "" && isPathInside(root, target);
|
|
1055
1106
|
}
|
|
1056
1107
|
function assertAllowedTarget(root, sessionRoot, target) {
|
|
1057
|
-
const resolved =
|
|
1108
|
+
const resolved = path7.resolve(target);
|
|
1058
1109
|
if (isStrictlyInside(root, resolved)) {
|
|
1059
|
-
if (isDeniedPath(
|
|
1110
|
+
if (isDeniedPath(path7.relative(root, resolved))) throw new Error("Transaction targets denied private storage.");
|
|
1060
1111
|
assertWithinRoot(root, resolved);
|
|
1061
1112
|
return;
|
|
1062
1113
|
}
|
|
1063
1114
|
if (sessionRoot && isStrictlyInside(sessionRoot, resolved)) {
|
|
1064
|
-
if (isDeniedPath(`sessions/${
|
|
1115
|
+
if (isDeniedPath(`sessions/${path7.relative(sessionRoot, resolved)}`))
|
|
1065
1116
|
throw new Error("Transaction targets denied session storage.");
|
|
1066
1117
|
assertWithinRoot(sessionRoot, resolved);
|
|
1067
1118
|
return;
|
|
@@ -1071,10 +1122,10 @@ function assertAllowedTarget(root, sessionRoot, target) {
|
|
|
1071
1122
|
async function assertSafeParents(root, sessionRoot, target) {
|
|
1072
1123
|
assertAllowedTarget(root, sessionRoot, target);
|
|
1073
1124
|
const boundary = isPathInside(root, target) ? root : sessionRoot;
|
|
1074
|
-
for (let parent =
|
|
1075
|
-
if (
|
|
1125
|
+
for (let parent = path7.dirname(target); boundary && mergePathIdentity(parent) !== mergePathIdentity(boundary); parent = path7.dirname(parent)) {
|
|
1126
|
+
if (path7.dirname(parent) === parent) throw new Error("Transaction parent did not reach its owned boundary.");
|
|
1076
1127
|
try {
|
|
1077
|
-
const stat = await
|
|
1128
|
+
const stat = await fs5.lstat(parent);
|
|
1078
1129
|
if (!stat.isDirectory() && !stat.isFile() || stat.isSymbolicLink())
|
|
1079
1130
|
throw new Error("Unsafe transaction parent.");
|
|
1080
1131
|
} catch (error) {
|
|
@@ -1085,7 +1136,7 @@ async function assertSafeParents(root, sessionRoot, target) {
|
|
|
1085
1136
|
}
|
|
1086
1137
|
async function completeTransaction(directory, journal) {
|
|
1087
1138
|
await writeJson(
|
|
1088
|
-
|
|
1139
|
+
path7.join(directory, "journal.json"),
|
|
1089
1140
|
{ ...journal, version: journal.sessionRootTransition ? 7 : 6, completed: true },
|
|
1090
1141
|
{
|
|
1091
1142
|
maxBytes: 32 * 1024 * 1024
|
|
@@ -1094,16 +1145,17 @@ async function completeTransaction(directory, journal) {
|
|
|
1094
1145
|
await removeTransaction(directory);
|
|
1095
1146
|
}
|
|
1096
1147
|
async function removeTransaction(directory) {
|
|
1097
|
-
await
|
|
1148
|
+
await fs5.rm(directory, { recursive: true, force: true });
|
|
1098
1149
|
await syncDirectory(transactionRoot()).catch(() => {
|
|
1099
1150
|
});
|
|
1100
1151
|
}
|
|
1101
1152
|
|
|
1102
1153
|
export {
|
|
1103
1154
|
syncMutationParents,
|
|
1155
|
+
assertUnexcludedTargets,
|
|
1104
1156
|
assertCanonicalMutationTargets,
|
|
1105
1157
|
applySnapshotTransaction,
|
|
1106
1158
|
recoverSnapshotTransactionsOnStartup,
|
|
1107
1159
|
recoverPendingSnapshotTransactions
|
|
1108
1160
|
};
|
|
1109
|
-
//# sourceMappingURL=chunk-
|
|
1161
|
+
//# sourceMappingURL=chunk-7WYF2KUO.ts.map
|