@narumitw/pi-sync 0.63.1 → 0.65.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 +21 -0
- package/README.md +86 -10
- package/dist/chunks/{chunk-CKUGCBHP.ts → chunk-AYGMIMCV.ts} +2 -2
- package/dist/chunks/chunk-AYGMIMCV.ts.map +7 -0
- package/dist/chunks/{chunk-27F5W7DX.ts → chunk-DAZZ5BEJ.ts} +3 -1
- package/dist/chunks/chunk-DAZZ5BEJ.ts.map +7 -0
- package/dist/chunks/{chunk-VQ47JBLF.ts → chunk-FI2F5JBS.ts} +2 -2
- package/dist/chunks/{chunk-V76KCWHO.ts → chunk-IOM6E5FW.ts} +4 -4
- package/dist/chunks/{chunk-B5F3JLD3.ts → chunk-JQ5BAWW2.ts} +482 -78
- package/dist/chunks/chunk-JQ5BAWW2.ts.map +7 -0
- package/dist/chunks/{chunk-LBNC5AOM.ts → chunk-OBVJTKVY.ts} +12 -12
- package/dist/chunks/{chunk-EXDWEFIJ.ts → chunk-OWEOZZZE.ts} +6 -6
- package/dist/chunks/{chunk-SXCH4Q5O.ts → chunk-SZJOUVCL.ts} +3 -3
- package/dist/chunks/{chunk-HSCAFOT5.ts → chunk-TZMVSTAM.ts} +5 -5
- package/dist/chunks/{chunk-POWOMJ7S.ts → chunk-WJQ4TGN5.ts} +58 -31
- package/dist/chunks/chunk-WJQ4TGN5.ts.map +7 -0
- package/dist/chunks/{chunk-HLCML3OW.ts → chunk-XUZV4OGI.ts} +5 -3
- package/dist/chunks/chunk-XUZV4OGI.ts.map +7 -0
- package/dist/chunks/{command-handler-GMRRFWOL.ts → command-handler-YZ4DXQ7N.ts} +81 -30
- package/dist/chunks/command-handler-YZ4DXQ7N.ts.map +7 -0
- package/dist/chunks/{conflict-review-JWEA7DPQ.ts → conflict-review-J7L7S4AR.ts} +2 -2
- package/dist/chunks/{file-selection-KID23GAD.ts → file-selection-DDQN4LVD.ts} +9 -9
- package/dist/chunks/{manager-result-dispatcher-GDB74IOP.ts → manager-result-dispatcher-XB75YLEG.ts} +5 -5
- package/dist/chunks/{manager-ui-T5J5WG7T.ts → manager-ui-RGA26Z6G.ts} +103 -83
- package/dist/chunks/manager-ui-RGA26Z6G.ts.map +7 -0
- package/dist/chunks/{merge-review-OQDXNTJU.ts → merge-review-AAC674C6.ts} +3 -2
- package/dist/chunks/ownership-recovery-UDDHSDSZ.ts +59 -0
- package/dist/chunks/ownership-recovery-UDDHSDSZ.ts.map +7 -0
- package/dist/chunks/{remote-selection-ui-GPITNEBF.ts → remote-selection-ui-WVMET5RI.ts} +4 -4
- package/dist/chunks/{setup-switch-L54X5U5I.ts → setup-switch-644T6KHG.ts} +4 -4
- package/dist/chunks/{setup-wizard-LL6YMGUR.ts → setup-wizard-YP2KF2WQ.ts} +9 -9
- package/dist/chunks/{sync-inspection-SRU6HGGK.ts → sync-inspection-NBBTSMKR.ts} +5 -5
- package/dist/chunks/{sync-operations-UGSCONZ7.ts → sync-operations-AIEM2KL2.ts} +2 -2
- package/dist/daemon/dist/cli.js +2 -0
- package/dist/daemon/dist/client-transport.js +66 -0
- package/dist/daemon/dist/client.js +222 -354
- package/dist/daemon/dist/consent.js +33 -11
- package/dist/daemon/dist/daemon-debug.js +33 -0
- package/dist/daemon/dist/daemon-recovery.js +158 -0
- package/dist/daemon/dist/daemon-startup.js +257 -0
- package/dist/daemon/dist/engine/backends/git/git-backend.js +49 -13
- package/dist/daemon/dist/engine/backends/git/git-runner.js +6 -1
- package/dist/daemon/dist/engine/backends/git/git-storage.js +5 -9
- package/dist/daemon/dist/engine/backends/publication-encoding.js +19 -0
- package/dist/daemon/dist/engine/backends/s3/s3-backend.js +30 -21
- package/dist/daemon/dist/engine/backends/sync-backend.js +16 -0
- package/dist/daemon/dist/engine/backends/webdav/webdav-backend.js +34 -32
- package/dist/daemon/dist/engine/backends/webdav/webdav-client.js +10 -0
- package/dist/daemon/dist/engine/debug-log.js +134 -0
- package/dist/daemon/dist/engine/paths.js +7 -1
- package/dist/daemon/dist/engine/settings/config-file.js +62 -17
- package/dist/daemon/dist/engine/settings/config.js +49 -14
- package/dist/daemon/dist/engine/snapshot/snapshot-apply.js +30 -8
- package/dist/daemon/dist/engine/snapshot/snapshot-codec.js +3 -1
- package/dist/daemon/dist/engine/snapshot/snapshot-payload.js +32 -0
- package/dist/daemon/dist/engine/snapshot/snapshot-transaction-images.js +34 -0
- package/dist/daemon/dist/engine/snapshot/snapshot-transaction.js +50 -7
- package/dist/daemon/dist/engine/snapshot/snapshot.js +64 -25
- package/dist/daemon/dist/engine/state/lock.js +157 -17
- package/dist/daemon/dist/engine/sync/continuous-sync.js +35 -11
- package/dist/daemon/dist/engine/sync/directional-action.js +124 -0
- package/dist/daemon/dist/engine/sync/initialization.js +2 -2
- package/dist/daemon/dist/engine/sync/local-fields.js +42 -19
- package/dist/daemon/dist/engine/sync/merged-sync.js +13 -10
- package/dist/daemon/dist/engine/sync/remote-snapshot.js +21 -4
- package/dist/daemon/dist/engine/sync/session-push.js +13 -4
- package/dist/daemon/dist/engine/sync/startup-bootstrap.js +105 -0
- package/dist/daemon/dist/engine/sync/startup-recovery-evidence.js +37 -0
- package/dist/daemon/dist/engine/sync/startup-timings.js +80 -8
- package/dist/daemon/dist/engine/sync/sync-error-guidance.js +6 -1
- package/dist/daemon/dist/engine/sync/sync-errors.js +20 -0
- package/dist/daemon/dist/engine/sync/sync-inspection.js +4 -7
- package/dist/daemon/dist/engine/sync/sync-local.js +59 -4
- package/dist/daemon/dist/engine/sync/sync-mutations.js +309 -337
- package/dist/daemon/dist/engine/sync/sync-policy.js +9 -1
- package/dist/daemon/dist/engine/sync/sync-queries.js +2 -1
- package/dist/daemon/dist/engine/sync/sync-rollback.js +198 -0
- package/dist/daemon/dist/engine/sync/sync-state.js +12 -4
- package/dist/daemon/dist/engine/sync/transfer-freshness.js +11 -5
- package/dist/daemon/dist/engine-service.js +122 -10
- package/dist/daemon/dist/foreground-queue-outcome.js +9 -0
- package/dist/daemon/dist/identity.js +36 -6
- package/dist/daemon/dist/index.js +2 -1
- package/dist/daemon/dist/monitor.js +21 -1
- package/dist/daemon/dist/paged-review.js +177 -0
- package/dist/daemon/dist/participant-ledger.js +149 -0
- package/dist/daemon/dist/protocol.js +4 -0
- package/dist/daemon/dist/server.js +340 -127
- package/dist/daemon/dist/service-host.js +83 -15
- package/dist/daemon/dist/state-wait-jobs.js +23 -0
- package/dist/daemon/dist/state-wait.js +34 -0
- package/dist/daemon/package.json +1 -1
- package/dist/index.ts +128 -48
- package/dist/index.ts.map +2 -2
- package/docs/continuous-sync-verification.md +2 -0
- package/docs/daemon-migration.md +31 -6
- package/docs/tui-contract.md +7 -1
- package/package.json +2 -2
- package/skills/configure-pi-sync/references/configuration.md +23 -13
- package/src/commands/command-execution.ts +67 -5
- package/src/commands/command-handler.ts +12 -3
- package/src/commands/command.ts +4 -2
- package/src/daemon/client-debug.ts +45 -0
- package/src/daemon/directional-recovery.ts +103 -0
- package/src/daemon/ownership-recovery.ts +50 -0
- package/src/daemon/pi-client.ts +399 -71
- package/src/daemon/startup-bootstrap-client.ts +26 -0
- package/src/settings/config.ts +1 -0
- package/src/sync/automatic-sync.ts +61 -18
- package/src/sync/startup-check.ts +13 -5
- package/src/sync-extension.ts +46 -8
- package/src/ui/cancellable-operation.ts +5 -2
- package/src/ui/manager-state.ts +31 -23
- package/src/ui/manager-ui.ts +45 -25
- package/src/ui/merge-review.ts +53 -18
- package/src/ui/settings-ui.ts +2 -2
- package/src/ui/sync-confirmation.ts +14 -17
- package/src/ui/sync-setups-ui.ts +15 -12
- package/dist/chunks/chunk-27F5W7DX.ts.map +0 -7
- package/dist/chunks/chunk-B5F3JLD3.ts.map +0 -7
- package/dist/chunks/chunk-CKUGCBHP.ts.map +0 -7
- package/dist/chunks/chunk-HLCML3OW.ts.map +0 -7
- package/dist/chunks/chunk-POWOMJ7S.ts.map +0 -7
- package/dist/chunks/command-handler-GMRRFWOL.ts.map +0 -7
- package/dist/chunks/manager-ui-T5J5WG7T.ts.map +0 -7
- /package/dist/chunks/{chunk-VQ47JBLF.ts.map → chunk-FI2F5JBS.ts.map} +0 -0
- /package/dist/chunks/{chunk-V76KCWHO.ts.map → chunk-IOM6E5FW.ts.map} +0 -0
- /package/dist/chunks/{chunk-LBNC5AOM.ts.map → chunk-OBVJTKVY.ts.map} +0 -0
- /package/dist/chunks/{chunk-EXDWEFIJ.ts.map → chunk-OWEOZZZE.ts.map} +0 -0
- /package/dist/chunks/{chunk-SXCH4Q5O.ts.map → chunk-SZJOUVCL.ts.map} +0 -0
- /package/dist/chunks/{chunk-HSCAFOT5.ts.map → chunk-TZMVSTAM.ts.map} +0 -0
- /package/dist/chunks/{conflict-review-JWEA7DPQ.ts.map → conflict-review-J7L7S4AR.ts.map} +0 -0
- /package/dist/chunks/{file-selection-KID23GAD.ts.map → file-selection-DDQN4LVD.ts.map} +0 -0
- /package/dist/chunks/{manager-result-dispatcher-GDB74IOP.ts.map → manager-result-dispatcher-XB75YLEG.ts.map} +0 -0
- /package/dist/chunks/{merge-review-OQDXNTJU.ts.map → merge-review-AAC674C6.ts.map} +0 -0
- /package/dist/chunks/{remote-selection-ui-GPITNEBF.ts.map → remote-selection-ui-WVMET5RI.ts.map} +0 -0
- /package/dist/chunks/{setup-switch-L54X5U5I.ts.map → setup-switch-644T6KHG.ts.map} +0 -0
- /package/dist/chunks/{setup-wizard-LL6YMGUR.ts.map → setup-wizard-YP2KF2WQ.ts.map} +0 -0
- /package/dist/chunks/{sync-inspection-SRU6HGGK.ts.map → sync-inspection-NBBTSMKR.ts.map} +0 -0
- /package/dist/chunks/{sync-operations-UGSCONZ7.ts.map → sync-operations-AIEM2KL2.ts.map} +0 -0
|
@@ -0,0 +1,105 @@
|
|
|
1
|
+
import { closeSync, constants, fstatSync, lstatSync, openSync, readSync } from "node:fs";
|
|
2
|
+
import path from "node:path";
|
|
3
|
+
import { readConsent } from "../../consent.js";
|
|
4
|
+
import { documentFingerprint } from "../../document.js";
|
|
5
|
+
import { MAX_SETTINGS_BYTES } from "../../protocol.js";
|
|
6
|
+
import { configFromSettings } from "../settings/config.js";
|
|
7
|
+
import { legacyLocalConfigPath, localConfigPath } from "../settings/config-file.js";
|
|
8
|
+
import { validateSettingsDocument } from "../settings/settings-validation.js";
|
|
9
|
+
import { stateDirectoryMigrationNotice } from "../state/state-directory.js";
|
|
10
|
+
import { statePathForConfig } from "../state/sync-state-store.js";
|
|
11
|
+
import { startupRecoveryEvidence } from "./startup-recovery-evidence.js";
|
|
12
|
+
function inspect(target) {
|
|
13
|
+
return lstatSync(target, { bigint: true, throwIfNoEntry: false });
|
|
14
|
+
}
|
|
15
|
+
function sameFile(before, now) {
|
|
16
|
+
return (now?.isFile() &&
|
|
17
|
+
now.dev === before.dev &&
|
|
18
|
+
now.ino === before.ino &&
|
|
19
|
+
now.size === before.size &&
|
|
20
|
+
now.mode === before.mode &&
|
|
21
|
+
now.ctimeNs === before.ctimeNs &&
|
|
22
|
+
now.mtimeNs === before.mtimeNs);
|
|
23
|
+
}
|
|
24
|
+
function readCanonicalSettings() {
|
|
25
|
+
const target = localConfigPath();
|
|
26
|
+
const mayNeedWrite = () => Boolean(inspect(legacyLocalConfigPath()) || inspect(`${target}.mutation-lock`));
|
|
27
|
+
if (mayNeedWrite())
|
|
28
|
+
return;
|
|
29
|
+
const before = inspect(target);
|
|
30
|
+
if (!before?.isFile() ||
|
|
31
|
+
before.size > MAX_SETTINGS_BYTES ||
|
|
32
|
+
(process.getuid && before.uid !== BigInt(process.getuid())) ||
|
|
33
|
+
(process.platform !== "win32" && (before.mode & 511n) !== 384n))
|
|
34
|
+
return;
|
|
35
|
+
const fd = openSync(target, constants.O_RDONLY | (constants.O_NOFOLLOW ?? 0) | (constants.O_NONBLOCK ?? 0));
|
|
36
|
+
try {
|
|
37
|
+
if (!sameFile(before, fstatSync(fd, { bigint: true })))
|
|
38
|
+
return;
|
|
39
|
+
const bytes = Buffer.alloc(Number(before.size) + 1);
|
|
40
|
+
let offset = 0;
|
|
41
|
+
while (offset < bytes.length) {
|
|
42
|
+
const count = readSync(fd, bytes, offset, bytes.length - offset, offset);
|
|
43
|
+
if (!count)
|
|
44
|
+
break;
|
|
45
|
+
offset += count;
|
|
46
|
+
}
|
|
47
|
+
if (offset !== Number(before.size) ||
|
|
48
|
+
!sameFile(before, fstatSync(fd, { bigint: true })) ||
|
|
49
|
+
!sameFile(before, inspect(target)) ||
|
|
50
|
+
mayNeedWrite())
|
|
51
|
+
return;
|
|
52
|
+
return {
|
|
53
|
+
bytes: bytes.subarray(0, offset),
|
|
54
|
+
fingerprint: documentFingerprint({
|
|
55
|
+
bytes: bytes.subarray(0, offset).toString("base64"),
|
|
56
|
+
identity: { dev: Number(before.dev), ino: Number(before.ino) },
|
|
57
|
+
}),
|
|
58
|
+
};
|
|
59
|
+
}
|
|
60
|
+
finally {
|
|
61
|
+
closeSync(fd);
|
|
62
|
+
}
|
|
63
|
+
}
|
|
64
|
+
export function captureStartupBootstrap(root, expected, signal, reviewOwnsLock) {
|
|
65
|
+
signal.throwIfAborted();
|
|
66
|
+
if (stateDirectoryMigrationNotice())
|
|
67
|
+
return;
|
|
68
|
+
const recovery = startupRecoveryEvidence(root, reviewOwnsLock);
|
|
69
|
+
if (recovery === undefined)
|
|
70
|
+
return;
|
|
71
|
+
const consent = readConsent(root);
|
|
72
|
+
if (!consent)
|
|
73
|
+
return;
|
|
74
|
+
const document = readCanonicalSettings();
|
|
75
|
+
if (document?.fingerprint !== expected)
|
|
76
|
+
return;
|
|
77
|
+
let parsed;
|
|
78
|
+
try {
|
|
79
|
+
parsed = JSON.parse(document.bytes.toString("utf8"));
|
|
80
|
+
}
|
|
81
|
+
catch {
|
|
82
|
+
return;
|
|
83
|
+
}
|
|
84
|
+
const settings = validateSettingsDocument(parsed);
|
|
85
|
+
if (!settings.activeSyncSetup)
|
|
86
|
+
return;
|
|
87
|
+
const config = configFromSettings(settings);
|
|
88
|
+
if (config.reservedIgnoreNames)
|
|
89
|
+
return;
|
|
90
|
+
const stateFile = statePathForConfig(config);
|
|
91
|
+
const noMergeJournal = () => {
|
|
92
|
+
const setupDirectory = inspect(path.dirname(stateFile));
|
|
93
|
+
return !setupDirectory || (setupDirectory.isDirectory() && !inspect(`${stateFile}.merge-journal.json`));
|
|
94
|
+
};
|
|
95
|
+
const consentToken = JSON.stringify(consent);
|
|
96
|
+
const validate = () => {
|
|
97
|
+
signal.throwIfAborted();
|
|
98
|
+
return (readCanonicalSettings()?.fingerprint === expected &&
|
|
99
|
+
!stateDirectoryMigrationNotice() &&
|
|
100
|
+
JSON.stringify(readConsent(root)) === consentToken &&
|
|
101
|
+
noMergeJournal() &&
|
|
102
|
+
startupRecoveryEvidence(root, reviewOwnsLock) === recovery);
|
|
103
|
+
};
|
|
104
|
+
return noMergeJournal() && startupRecoveryEvidence(root, reviewOwnsLock) === recovery ? validate : undefined;
|
|
105
|
+
}
|
|
@@ -0,0 +1,37 @@
|
|
|
1
|
+
import { lstatSync, opendirSync } from "node:fs";
|
|
2
|
+
import path from "node:path";
|
|
3
|
+
import { stateDir } from "../state/state-directory.js";
|
|
4
|
+
function stamp(info) {
|
|
5
|
+
return info && `${info.dev}:${info.ino}:${info.ctimeNs}:${info.mtimeNs}`;
|
|
6
|
+
}
|
|
7
|
+
function inspect(target) {
|
|
8
|
+
return lstatSync(target, { bigint: true, throwIfNoEntry: false });
|
|
9
|
+
}
|
|
10
|
+
export function startupRecoveryEvidence(root, reviewOwnsLock) {
|
|
11
|
+
const state = stateDir(root);
|
|
12
|
+
const transactions = path.join(state, "transactions");
|
|
13
|
+
const guard = path.join(root, ".pi-sync-state-migration.lock");
|
|
14
|
+
const lock = path.join(state, "lock");
|
|
15
|
+
const protectedState = () => state === stateDir(root) &&
|
|
16
|
+
!inspect(guard) &&
|
|
17
|
+
(reviewOwnsLock ? reviewOwnsLock() : !inspect(lock) && !inspect(`${lock}.guard`));
|
|
18
|
+
if (!protectedState())
|
|
19
|
+
return;
|
|
20
|
+
const parent = inspect(state);
|
|
21
|
+
const pending = inspect(transactions);
|
|
22
|
+
if (pending) {
|
|
23
|
+
if (!pending.isDirectory())
|
|
24
|
+
return;
|
|
25
|
+
const directory = opendirSync(transactions, { bufferSize: 1 });
|
|
26
|
+
try {
|
|
27
|
+
if (directory.readSync())
|
|
28
|
+
return;
|
|
29
|
+
}
|
|
30
|
+
finally {
|
|
31
|
+
directory.closeSync();
|
|
32
|
+
}
|
|
33
|
+
}
|
|
34
|
+
if (!protectedState() || stamp(parent) !== stamp(inspect(state)) || stamp(pending) !== stamp(inspect(transactions)))
|
|
35
|
+
return;
|
|
36
|
+
return JSON.stringify([state, stamp(parent), stamp(pending)]);
|
|
37
|
+
}
|
|
@@ -1,4 +1,68 @@
|
|
|
1
1
|
import { performance } from "node:perf_hooks";
|
|
2
|
+
const startupStages = [
|
|
3
|
+
"daemon initialization",
|
|
4
|
+
"transaction recovery",
|
|
5
|
+
"settings read",
|
|
6
|
+
"config resolution",
|
|
7
|
+
"inspection import",
|
|
8
|
+
"inspection and guards",
|
|
9
|
+
"backend initialization",
|
|
10
|
+
"local snapshot",
|
|
11
|
+
"sync state read",
|
|
12
|
+
"remote head",
|
|
13
|
+
"operations import",
|
|
14
|
+
"automatic transfer",
|
|
15
|
+
"transfer authorization",
|
|
16
|
+
"transfer backend initialization",
|
|
17
|
+
"transfer journal read",
|
|
18
|
+
"transfer journal recovery",
|
|
19
|
+
"transfer state read",
|
|
20
|
+
"transfer local snapshot",
|
|
21
|
+
"transfer remote head",
|
|
22
|
+
"transfer snapshot download",
|
|
23
|
+
"transfer unchanged verification",
|
|
24
|
+
"transfer merge planning",
|
|
25
|
+
"transfer settings merge",
|
|
26
|
+
"transfer content merge",
|
|
27
|
+
"transfer preflight",
|
|
28
|
+
"transfer backup",
|
|
29
|
+
"transfer journal write",
|
|
30
|
+
"transfer remote publication",
|
|
31
|
+
"transfer local apply",
|
|
32
|
+
"transfer baseline write",
|
|
33
|
+
"transfer cleanup",
|
|
34
|
+
];
|
|
35
|
+
const startupScopes = [
|
|
36
|
+
"local initialization",
|
|
37
|
+
"background startup",
|
|
38
|
+
"continuous startup",
|
|
39
|
+
"daemon inspection",
|
|
40
|
+
"daemon transfer",
|
|
41
|
+
];
|
|
42
|
+
const outcomes = ["completed", "stopped"];
|
|
43
|
+
const unchangedLine = "transfer result: unchanged (fast path)";
|
|
44
|
+
const end = "(?![\\s\\S])";
|
|
45
|
+
const duration = "(?:0|[1-9][0-9]{0,15})\\.[0-9] ms";
|
|
46
|
+
const headingPattern = new RegExp(`^pi-sync debug: (?:${startupScopes.join("|")}) \\((?:${outcomes.join("|")})\\)${end}`, "u");
|
|
47
|
+
const stagePattern = new RegExp(`^(${startupStages.join("|")}): ${duration}(?: \\((?:[2-9]|[1-9][0-9]{1,15}) calls\\))?(?: \\(interrupted\\))?${end}`, "u");
|
|
48
|
+
const totalPattern = new RegExp(`^total: ${duration}${end}`, "u");
|
|
49
|
+
export function isStartupTimingReport(value) {
|
|
50
|
+
if (value.length > 4096)
|
|
51
|
+
return false;
|
|
52
|
+
const lines = value.split("\n");
|
|
53
|
+
if (!headingPattern.test(lines.shift() ?? "") || !totalPattern.test(lines.pop() ?? ""))
|
|
54
|
+
return false;
|
|
55
|
+
if (lines.at(-1) === unchangedLine)
|
|
56
|
+
lines.pop();
|
|
57
|
+
const seen = new Set();
|
|
58
|
+
for (const line of lines) {
|
|
59
|
+
const stage = stagePattern.exec(line)?.[1];
|
|
60
|
+
if (!stage || seen.has(stage))
|
|
61
|
+
return false;
|
|
62
|
+
seen.add(stage);
|
|
63
|
+
}
|
|
64
|
+
return true;
|
|
65
|
+
}
|
|
2
66
|
/** Fixed labels and durations only: never retain errors, paths, settings, or payloads. */
|
|
3
67
|
export function createStartupTimings(publishReport = (ctx, report) => ctx.ui.notify(report, "info")) {
|
|
4
68
|
const started = performance.now();
|
|
@@ -8,6 +72,7 @@ export function createStartupTimings(publishReport = (ctx, report) => ctx.ui.not
|
|
|
8
72
|
markUnchanged() {
|
|
9
73
|
unchanged = true;
|
|
10
74
|
},
|
|
75
|
+
wasUnchanged: () => unchanged,
|
|
11
76
|
async measure(stage, run) {
|
|
12
77
|
const start = performance.now();
|
|
13
78
|
let completed = false;
|
|
@@ -25,16 +90,23 @@ export function createStartupTimings(publishReport = (ctx, report) => ctx.ui.not
|
|
|
25
90
|
});
|
|
26
91
|
}
|
|
27
92
|
},
|
|
28
|
-
publish(ctx, enabled, sessionSignal, scope, outcome = "completed") {
|
|
29
|
-
|
|
93
|
+
publish(ctx, enabled, sessionSignal, scope, outcome = "completed", observe) {
|
|
94
|
+
const display = enabled && ctx.hasUI && !sessionSignal.aborted;
|
|
95
|
+
if (!display && !observe)
|
|
30
96
|
return;
|
|
97
|
+
const report = [
|
|
98
|
+
`pi-sync debug: ${scope} (${outcome})`,
|
|
99
|
+
...Array.from(stages, ([stage, value]) => `${stage}: ${value.ms.toFixed(1)} ms${value.calls > 1 ? ` (${value.calls} calls)` : ""}${value.interrupted ? " (interrupted)" : ""}`),
|
|
100
|
+
...(unchanged ? [unchangedLine] : []),
|
|
101
|
+
`total: ${(performance.now() - started).toFixed(1)} ms`,
|
|
102
|
+
].join("\n");
|
|
103
|
+
try {
|
|
104
|
+
observe?.(report);
|
|
105
|
+
}
|
|
106
|
+
catch { }
|
|
31
107
|
try {
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
...Array.from(stages, ([stage, value]) => `${stage}: ${value.ms.toFixed(1)} ms${value.calls > 1 ? ` (${value.calls} calls)` : ""}${value.interrupted ? " (interrupted)" : ""}`),
|
|
35
|
-
...(unchanged ? ["transfer result: unchanged (fast path)"] : []),
|
|
36
|
-
`total: ${(performance.now() - started).toFixed(1)} ms`,
|
|
37
|
-
].join("\n"));
|
|
108
|
+
if (display)
|
|
109
|
+
publishReport(ctx, report);
|
|
38
110
|
}
|
|
39
111
|
catch {
|
|
40
112
|
// Diagnostics must not change sync behavior if the UI is unavailable during teardown.
|
|
@@ -1,3 +1,4 @@
|
|
|
1
|
+
import { OWNERSHIP_CLEANUP_REQUIRED } from "../../protocol.js";
|
|
1
2
|
import { errorMessage } from "./sync-errors.js";
|
|
2
3
|
/** Add recovery advice without changing backend outcome semantics or printing error causes. */
|
|
3
4
|
export function syncErrorGuidance(error) {
|
|
@@ -6,7 +7,11 @@ export function syncErrorGuidance(error) {
|
|
|
6
7
|
const code = error?.code;
|
|
7
8
|
const status = error?.status;
|
|
8
9
|
let next;
|
|
9
|
-
if (code ===
|
|
10
|
+
if (code === OWNERSHIP_CLEANUP_REQUIRED) {
|
|
11
|
+
next =
|
|
12
|
+
"Open /sync and choose Stop daemon for recovery, or run /sync daemon recover. Do not retry the transfer yet.";
|
|
13
|
+
}
|
|
14
|
+
else if (code === "ENOSPC") {
|
|
10
15
|
next = "Free disk space for the private pi-sync settings/state directory, then retry.";
|
|
11
16
|
}
|
|
12
17
|
else if (code && ["EACCES", "EPERM", "EROFS"].includes(code)) {
|
|
@@ -1,3 +1,23 @@
|
|
|
1
|
+
export class PublicationStatePersistenceError extends Error {
|
|
2
|
+
head;
|
|
3
|
+
backupPath;
|
|
4
|
+
constructor(head, cause, backupPath) {
|
|
5
|
+
super(`Remote publication ${head.snapshotId} is active, but local sync state could not be saved${backupPath ? `; local backup: ${backupPath}` : ""}: ${errorMessage(cause)}`, { cause });
|
|
6
|
+
this.name = "PublicationStatePersistenceError";
|
|
7
|
+
this.head = head;
|
|
8
|
+
this.backupPath = backupPath;
|
|
9
|
+
}
|
|
10
|
+
}
|
|
11
|
+
export class RollbackPublicationError extends Error {
|
|
12
|
+
backupPath;
|
|
13
|
+
constructor(backupPath, cause) {
|
|
14
|
+
super(`Rollback applied locally with backup ${backupPath}, but remote publication failed: ${errorMessage(cause)}`, {
|
|
15
|
+
cause,
|
|
16
|
+
});
|
|
17
|
+
this.name = "RollbackPublicationError";
|
|
18
|
+
this.backupPath = backupPath;
|
|
19
|
+
}
|
|
20
|
+
}
|
|
1
21
|
export class SetupPullRequiresUiError extends Error {
|
|
2
22
|
}
|
|
3
23
|
export class SyncDecisionRequiredError extends Error {
|
|
@@ -1,18 +1,17 @@
|
|
|
1
|
-
import { createHash } from "node:crypto";
|
|
2
1
|
import { createSyncBackend } from "../backends/backend-factory.js";
|
|
3
|
-
import {
|
|
2
|
+
import { observeSnapshot } from "../snapshot/snapshot.js";
|
|
4
3
|
import { readStateForConfig, syncStateFingerprint } from "../state/sync-state-store.js";
|
|
5
4
|
import { inspectHeadSelection } from "./remote-snapshot.js";
|
|
6
5
|
import { throwIfAborted } from "./signals.js";
|
|
7
6
|
import { createStartupTimings } from "./startup-timings.js";
|
|
8
7
|
import { syncSelectionHasContent } from "./sync-policy.js";
|
|
9
|
-
import {
|
|
8
|
+
import { hasLocalChanges, rawSnapshotHashIdentity, remoteChangedSinceState } from "./sync-state.js";
|
|
10
9
|
/** Advisory baseline-relative observation, never an authorization to transfer files. */
|
|
11
10
|
export async function inspectSync(config, options, signal, factory = createSyncBackend, timings = createStartupTimings()) {
|
|
12
11
|
throwIfAborted(signal);
|
|
13
12
|
const backend = await timings.measure("backend initialization", () => factory(config));
|
|
14
13
|
throwIfAborted(signal);
|
|
15
|
-
const local = await timings.measure("local snapshot", () =>
|
|
14
|
+
const local = await timings.measure("local snapshot", () => observeSnapshot({ ...options, signal }, Boolean(config.localFields?.length)));
|
|
16
15
|
throwIfAborted(signal);
|
|
17
16
|
const state = await timings.measure("sync state read", () => readStateForConfig(config));
|
|
18
17
|
throwIfAborted(signal);
|
|
@@ -21,9 +20,7 @@ export async function inspectSync(config, options, signal, factory = createSyncB
|
|
|
21
20
|
const selectionState = head ? await inspectHeadSelection(backend, config, head, signal) : undefined;
|
|
22
21
|
throwIfAborted(signal);
|
|
23
22
|
const freshness = {
|
|
24
|
-
localIdentity:
|
|
25
|
-
.update(JSON.stringify(fileHashMap(local)))
|
|
26
|
-
.digest("hex"),
|
|
23
|
+
localIdentity: rawSnapshotHashIdentity(local),
|
|
27
24
|
};
|
|
28
25
|
return {
|
|
29
26
|
...freshness,
|
|
@@ -1,3 +1,4 @@
|
|
|
1
|
+
import { constants } from "node:fs";
|
|
1
2
|
import fs from "node:fs/promises";
|
|
2
3
|
import path from "node:path";
|
|
3
4
|
import { sessionDirFromContext } from "../snapshot/session-paths.js";
|
|
@@ -16,13 +17,63 @@ export function protectedSessionPaths(ctx, sessionRoot = sessionDirFromContext(c
|
|
|
16
17
|
return relative ? [relative] : [];
|
|
17
18
|
}));
|
|
18
19
|
}
|
|
19
|
-
|
|
20
|
+
import { captureMutationOwner } from "./mutation-owner.js";
|
|
21
|
+
export { captureMutationOwner };
|
|
22
|
+
export async function maybeReload(ctx, signal, validateMutation = captureMutationOwner(ctx, signal)) {
|
|
23
|
+
if (signal?.aborted || typeof ctx.reload !== "function")
|
|
24
|
+
return;
|
|
25
|
+
validateMutation();
|
|
26
|
+
if (ctx.hasUI &&
|
|
27
|
+
(await ctx.ui.confirm("Reload Pi resources now?", "This reloads extensions, skills, prompts, themes, and context files."))) {
|
|
28
|
+
if (signal?.aborted)
|
|
29
|
+
return;
|
|
30
|
+
validateMutation();
|
|
31
|
+
await ctx.reload();
|
|
32
|
+
}
|
|
33
|
+
}
|
|
20
34
|
export async function backupLocal(profile, options = {}, signal) {
|
|
21
35
|
throwIfAborted(signal);
|
|
22
|
-
|
|
36
|
+
return backupSnapshot(await createSnapshot(profile, { ...options, signal }), signal);
|
|
37
|
+
}
|
|
38
|
+
export async function backupSnapshot(snapshot, signal) {
|
|
39
|
+
return (await writeSnapshotBackup(snapshot, signal)).backupPath;
|
|
40
|
+
}
|
|
41
|
+
export class LocalBackupVerificationError extends Error {
|
|
42
|
+
}
|
|
43
|
+
export async function backupDirectionalSnapshot(snapshot, signal) {
|
|
44
|
+
const { backupPath, encoded } = await writeSnapshotBackup(snapshot, signal, true);
|
|
45
|
+
const validate = async () => {
|
|
46
|
+
throwIfAborted(signal);
|
|
47
|
+
try {
|
|
48
|
+
if (stateDir() !== path.dirname(path.dirname(backupPath)) ||
|
|
49
|
+
!(await fs.lstat(path.dirname(backupPath))).isDirectory())
|
|
50
|
+
throw new Error("Local backup directory changed before Pull.");
|
|
51
|
+
const handle = await fs.open(backupPath, constants.O_RDONLY | constants.O_NOFOLLOW | constants.O_NONBLOCK);
|
|
52
|
+
try {
|
|
53
|
+
const stat = await handle.stat();
|
|
54
|
+
if (!stat.isFile() || stat.size !== encoded.length || !(await handle.readFile()).equals(encoded))
|
|
55
|
+
throw new Error("Local backup no longer matches the exact pre-Pull bytes.");
|
|
56
|
+
}
|
|
57
|
+
finally {
|
|
58
|
+
await handle.close();
|
|
59
|
+
}
|
|
60
|
+
}
|
|
61
|
+
catch (error) {
|
|
62
|
+
throwIfAborted(signal);
|
|
63
|
+
throw new LocalBackupVerificationError(`Local backup could not be verified at ${backupPath}.`, { cause: error });
|
|
64
|
+
}
|
|
65
|
+
throwIfAborted(signal);
|
|
66
|
+
};
|
|
67
|
+
await validate();
|
|
68
|
+
return { backupPath, validate };
|
|
69
|
+
}
|
|
70
|
+
async function writeSnapshotBackup(snapshot, signal, durableParent = false) {
|
|
23
71
|
throwIfAborted(signal);
|
|
24
|
-
const
|
|
72
|
+
const root = stateDir();
|
|
73
|
+
const backupDirectory = path.join(root, "backups");
|
|
25
74
|
await fs.mkdir(backupDirectory, { recursive: true, mode: 0o700 });
|
|
75
|
+
if (durableParent && !(await fs.lstat(backupDirectory)).isDirectory())
|
|
76
|
+
throw new Error("Refusing to write to a linked local backup directory.");
|
|
26
77
|
throwIfAborted(signal);
|
|
27
78
|
const backupPath = path.join(backupDirectory, `${snapshot.id}.json.gz`);
|
|
28
79
|
const encoded = await encodeSnapshot(snapshot);
|
|
@@ -36,5 +87,9 @@ export async function backupLocal(profile, options = {}, signal) {
|
|
|
36
87
|
await handle.close();
|
|
37
88
|
}
|
|
38
89
|
await syncDirectory(backupDirectory);
|
|
39
|
-
|
|
90
|
+
if (durableParent) {
|
|
91
|
+
await syncDirectory(root);
|
|
92
|
+
await syncDirectory(path.dirname(root));
|
|
93
|
+
}
|
|
94
|
+
return { backupPath, encoded };
|
|
40
95
|
}
|