@narumitw/pi-sync-daemon 0.7.0 → 0.9.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 +16 -2
- package/dist/cli.js +1 -1
- package/dist/consent.d.ts +14 -2
- package/dist/consent.js +147 -14
- package/dist/engine/backends/git/git-backend.d.ts +2 -3
- package/dist/engine/backends/git/git-backend.js +71 -25
- package/dist/engine/backends/git/git-storage.d.ts +1 -1
- package/dist/engine/backends/git/git-storage.js +30 -9
- package/dist/engine/backends/s3/s3-backend.js +8 -0
- package/dist/engine/backends/sync-backend.d.ts +1 -0
- package/dist/engine/backends/webdav/webdav-backend.js +8 -0
- package/dist/engine/commands/command-types.d.ts +2 -1
- package/dist/engine/host.d.ts +1 -0
- package/dist/engine/ignore/effective-ignore-policy.d.ts +10 -0
- package/dist/engine/ignore/effective-ignore-policy.js +33 -0
- package/dist/engine/ignore/ignore-edit.d.ts +13 -0
- package/dist/engine/ignore/ignore-edit.js +102 -0
- package/dist/engine/ignore/ignore-initialization.d.ts +9 -0
- package/dist/engine/ignore/ignore-initialization.js +252 -0
- package/dist/engine/ignore/ignore-migration-prompt.d.ts +7 -0
- package/dist/engine/ignore/ignore-migration-prompt.js +139 -0
- package/dist/engine/ignore/ignore-settings.d.ts +30 -0
- package/dist/engine/ignore/ignore-settings.js +393 -0
- package/dist/engine/ignore/ignore-source.d.ts +27 -0
- package/dist/engine/ignore/ignore-source.js +250 -0
- package/dist/engine/settings/config-file.d.ts +9 -2
- package/dist/engine/settings/config-file.js +124 -28
- package/dist/engine/settings/config.d.ts +6 -2
- package/dist/engine/settings/config.js +140 -11
- package/dist/engine/settings/settings-management.d.ts +5 -3
- package/dist/engine/settings/settings-management.js +24 -10
- package/dist/engine/settings/settings-types.d.ts +25 -2
- package/dist/engine/settings/settings-validation.js +23 -5
- package/dist/engine/snapshot/session-paths.d.ts +1 -1
- package/dist/engine/snapshot/session-paths.js +41 -13
- package/dist/engine/snapshot/snapshot-apply.d.ts +3 -3
- package/dist/engine/snapshot/snapshot-apply.js +23 -4
- package/dist/engine/snapshot/snapshot-codec.d.ts +1 -1
- package/dist/engine/snapshot/snapshot-codec.js +14 -5
- package/dist/engine/snapshot/snapshot-exclude-safety.d.ts +4 -2
- package/dist/engine/snapshot/snapshot-exclude-safety.js +76 -19
- package/dist/engine/snapshot/snapshot-transaction-images.d.ts +3 -1
- package/dist/engine/snapshot/snapshot-transaction-images.js +33 -3
- package/dist/engine/snapshot/snapshot-transaction-policy.d.ts +30 -2
- package/dist/engine/snapshot/snapshot-transaction-policy.js +135 -11
- package/dist/engine/snapshot/snapshot-transaction-verification.d.ts +7 -0
- package/dist/engine/snapshot/snapshot-transaction-verification.js +78 -0
- package/dist/engine/snapshot/snapshot-transaction.d.ts +1 -12
- package/dist/engine/snapshot/snapshot-transaction.js +129 -109
- package/dist/engine/snapshot/snapshot-types.d.ts +27 -1
- package/dist/engine/snapshot/snapshot-types.js +9 -1
- package/dist/engine/snapshot/snapshot.d.ts +6 -4
- package/dist/engine/snapshot/snapshot.js +91 -34
- package/dist/engine/state/state-types.d.ts +1 -0
- package/dist/engine/sync/agent-plan.js +1 -0
- package/dist/engine/sync/agent-sync.js +8 -1
- package/dist/engine/sync/continuous-sync.d.ts +3 -1
- package/dist/engine/sync/continuous-sync.js +50 -18
- package/dist/engine/sync/continuous-watch.js +4 -1
- package/dist/engine/sync/initialization.js +3 -2
- package/dist/engine/sync/local-fields.d.ts +3 -1
- package/dist/engine/sync/local-fields.js +62 -1
- package/dist/engine/sync/merge-acceptance.d.ts +8 -0
- package/dist/engine/sync/merge-acceptance.js +29 -0
- package/dist/engine/sync/merge-apply.js +42 -11
- package/dist/engine/sync/merge-journal.d.ts +24 -4
- package/dist/engine/sync/merge-journal.js +134 -8
- package/dist/engine/sync/merged-sync.d.ts +1 -1
- package/dist/engine/sync/merged-sync.js +128 -83
- package/dist/engine/sync/remote-snapshot.d.ts +11 -3
- package/dist/engine/sync/remote-snapshot.js +72 -18
- package/dist/engine/sync/session-push.js +20 -6
- package/dist/engine/sync/sync-inspection.js +7 -5
- package/dist/engine/sync/sync-mutations.d.ts +2 -1
- package/dist/engine/sync/sync-mutations.js +157 -73
- package/dist/engine/sync/sync-policy.d.ts +29 -8
- package/dist/engine/sync/sync-policy.js +152 -16
- package/dist/engine/sync/sync-queries.js +8 -1
- package/dist/engine/sync/sync-state.d.ts +4 -2
- package/dist/engine/sync/sync-state.js +15 -2
- package/dist/engine/sync/transfer-freshness.d.ts +31 -0
- package/dist/engine/sync/transfer-freshness.js +82 -0
- package/dist/engine-service.d.ts +1 -1
- package/dist/engine-service.js +146 -27
- package/dist/monitor.d.ts +4 -1
- package/dist/monitor.js +84 -32
- package/dist/server.d.ts +1 -0
- package/dist/server.js +46 -5
- package/dist/service-host.d.ts +1 -1
- package/dist/service-host.js +2 -1
- package/docs/architecture.md +30 -0
- package/package.json +2 -1
|
@@ -59,6 +59,9 @@ export class WebDavSyncBackend {
|
|
|
59
59
|
async publishSnapshot(snapshot, expected, options = {}) {
|
|
60
60
|
throwIfAborted(options.signal);
|
|
61
61
|
assertSnapshotIdentity(snapshot, this.config.destination.namespace);
|
|
62
|
+
if (options.validateIgnorePolicy)
|
|
63
|
+
await options.validateIgnorePolicy();
|
|
64
|
+
throwIfAborted(options.signal);
|
|
62
65
|
await this.ensureAtomicConditions(options.signal);
|
|
63
66
|
throwIfAborted(options.signal);
|
|
64
67
|
const client = new WebDavClient(this.config, options.signal);
|
|
@@ -67,6 +70,9 @@ export class WebDavSyncBackend {
|
|
|
67
70
|
throwIfAborted(options.signal);
|
|
68
71
|
const pointer = createLatestPointer(this.config.destination.namespace, snapshot, sha256(encoded));
|
|
69
72
|
const stagedPath = snapshotPath(this.config, snapshot.id);
|
|
73
|
+
if (options.validateIgnorePolicy)
|
|
74
|
+
await options.validateIgnorePolicy();
|
|
75
|
+
throwIfAborted(options.signal);
|
|
70
76
|
try {
|
|
71
77
|
await client.putBuffer(stagedPath, encoded, "application/gzip", { ifAbsent: true });
|
|
72
78
|
}
|
|
@@ -89,6 +95,8 @@ export class WebDavSyncBackend {
|
|
|
89
95
|
});
|
|
90
96
|
}
|
|
91
97
|
const condition = publicationCondition(currentObject, expected, this.identity);
|
|
98
|
+
if (options.validateIgnorePolicy)
|
|
99
|
+
await options.validateIgnorePolicy();
|
|
92
100
|
throwIfAborted(options.signal);
|
|
93
101
|
options.onCommit?.();
|
|
94
102
|
const commitClient = new WebDavClient(this.config, AbortSignal.timeout(this.postCommitTimeoutMs), this.postCommitTimeoutMs);
|
|
@@ -9,6 +9,7 @@ export interface CommandOptions {
|
|
|
9
9
|
reload: boolean;
|
|
10
10
|
auto: boolean;
|
|
11
11
|
setup?: string;
|
|
12
|
+
automaticFingerprint?: string;
|
|
12
13
|
signal?: AbortSignal;
|
|
13
14
|
onCommit?: () => void;
|
|
14
15
|
/** In-memory startup diagnostics only; never persisted or sent to a backend. */
|
|
@@ -18,7 +19,7 @@ export interface CommandOptions {
|
|
|
18
19
|
/** Internal cycle authorization, ownership and durable request reservation; never command arguments. */
|
|
19
20
|
background?: {
|
|
20
21
|
validate(config?: AnySyncConfig): void;
|
|
21
|
-
reserve(key: string, signal?: AbortSignal): Promise<void>;
|
|
22
|
+
reserve?(key: string, signal?: AbortSignal): Promise<void>;
|
|
22
23
|
};
|
|
23
24
|
/** Manager-only, separately confirmed retry; never parsed from command arguments. */
|
|
24
25
|
agentRetryRefusal?: boolean;
|
package/dist/engine/host.d.ts
CHANGED
|
@@ -55,6 +55,7 @@ export interface ConfigAuthority {
|
|
|
55
55
|
replace(document: import("./settings/config-file.js").LocalConfigDocument, value: Record<string, unknown>): Promise<void>;
|
|
56
56
|
}
|
|
57
57
|
export interface EngineHost {
|
|
58
|
+
ignoreFileOperationsAvailable?: boolean;
|
|
58
59
|
root(): string;
|
|
59
60
|
config?: ConfigAuthority;
|
|
60
61
|
allowConfigMigration?(): boolean;
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
import type { EffectiveIgnorePolicy } from "../snapshot/snapshot-types.js";
|
|
2
|
+
import { type IgnoreSource } from "./ignore-source.js";
|
|
3
|
+
export interface IgnorePolicyInput {
|
|
4
|
+
filename?: string;
|
|
5
|
+
sessions: boolean;
|
|
6
|
+
configuredFilenames: readonly string[];
|
|
7
|
+
}
|
|
8
|
+
export declare function loadEffectiveIgnorePolicy(root: string, input: IgnorePolicyInput, signal?: AbortSignal): Promise<EffectiveIgnorePolicy>;
|
|
9
|
+
export declare function compileEffectiveIgnorePolicy(source: IgnoreSource, input: IgnorePolicyInput): EffectiveIgnorePolicy;
|
|
10
|
+
export declare function requireUnchangedIgnorePolicy(root: string, reviewed: EffectiveIgnorePolicy, current: IgnorePolicyInput, signal?: AbortSignal): Promise<void>;
|
|
@@ -0,0 +1,33 @@
|
|
|
1
|
+
import { compileIgnoreRules, DEFAULT_IGNORE_FILENAME, ignorePolicyFingerprint, readIgnoreSource, validateIgnoreFilename, } from "./ignore-source.js";
|
|
2
|
+
function reservedNames(filename, configuredFilenames) {
|
|
3
|
+
return [
|
|
4
|
+
...new Set([DEFAULT_IGNORE_FILENAME, filename, ...configuredFilenames].map((name) => validateIgnoreFilename(name).toLowerCase())),
|
|
5
|
+
].sort();
|
|
6
|
+
}
|
|
7
|
+
export async function loadEffectiveIgnorePolicy(root, input, signal) {
|
|
8
|
+
const source = await readIgnoreSource(root, input.filename, signal);
|
|
9
|
+
signal?.throwIfAborted();
|
|
10
|
+
return compileEffectiveIgnorePolicy(source, input);
|
|
11
|
+
}
|
|
12
|
+
export function compileEffectiveIgnorePolicy(source, input) {
|
|
13
|
+
return {
|
|
14
|
+
filename: source.filename,
|
|
15
|
+
sourceDigest: source.sourceDigest,
|
|
16
|
+
fingerprint: ignorePolicyFingerprint(source.text, input.sessions),
|
|
17
|
+
sessions: input.sessions,
|
|
18
|
+
reservedNames: reservedNames(source.filename, input.configuredFilenames),
|
|
19
|
+
matches: compileIgnoreRules(source.text),
|
|
20
|
+
};
|
|
21
|
+
}
|
|
22
|
+
export async function requireUnchangedIgnorePolicy(root, reviewed, current, signal) {
|
|
23
|
+
const source = await readIgnoreSource(root, current.filename, signal);
|
|
24
|
+
signal?.throwIfAborted();
|
|
25
|
+
const currentNames = reservedNames(source.filename, current.configuredFilenames);
|
|
26
|
+
if (reviewed.filename !== source.filename ||
|
|
27
|
+
reviewed.sourceDigest !== source.sourceDigest ||
|
|
28
|
+
reviewed.sessions !== current.sessions ||
|
|
29
|
+
reviewed.reservedNames.length !== currentNames.length ||
|
|
30
|
+
reviewed.reservedNames.some((name, index) => name !== currentNames[index])) {
|
|
31
|
+
throw new Error("Pi Sync ignore policy changed; reopen the review before continuing.");
|
|
32
|
+
}
|
|
33
|
+
}
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
import { type Execution } from "../../protocol.js";
|
|
2
|
+
type IgnoreLocation = {
|
|
3
|
+
filename: string;
|
|
4
|
+
path: string;
|
|
5
|
+
sharingSetups: string[];
|
|
6
|
+
};
|
|
7
|
+
type IgnoreEdit = IgnoreLocation & {
|
|
8
|
+
text: string;
|
|
9
|
+
sourceDigest: string;
|
|
10
|
+
identity: string;
|
|
11
|
+
};
|
|
12
|
+
export declare function readIgnoreFileEdit(payload: unknown, execution: Execution): Promise<IgnoreLocation | IgnoreEdit>;
|
|
13
|
+
export {};
|
|
@@ -0,0 +1,102 @@
|
|
|
1
|
+
import { createHash } from "node:crypto";
|
|
2
|
+
import { lstatSync, statSync } from "node:fs";
|
|
3
|
+
import path from "node:path";
|
|
4
|
+
import { record } from "../../protocol.js";
|
|
5
|
+
import { getEngineRoot } from "../host.js";
|
|
6
|
+
import { withLockedLocalConfigDocument } from "../settings/config-file.js";
|
|
7
|
+
import { validateConfigName, validateSettingsDocument } from "../settings/settings-validation.js";
|
|
8
|
+
import { compileIgnoreRules, DEFAULT_IGNORE_CREATION_RECORD, DEFAULT_IGNORE_FILENAME, ignoreFileIdentity, readIgnoreSource, sameFile, } from "./ignore-source.js";
|
|
9
|
+
function locateIgnoreFile(document, root, setup) {
|
|
10
|
+
const settings = document && validateSettingsDocument(document.parsed);
|
|
11
|
+
const current = settings && Object.hasOwn(settings.syncSetups, setup) ? settings.syncSetups[setup] : undefined;
|
|
12
|
+
if (!current)
|
|
13
|
+
throw new Error("Sync setup no longer exists; reopen ignore-file settings.");
|
|
14
|
+
if (settings?.version !== 8 || current.sync.scope !== "agent-directory" || current.sync.pathPolicy !== "ignore-file")
|
|
15
|
+
throw new Error("This sync setup does not use an ignore file.");
|
|
16
|
+
const filename = current.sync.ignoreFile ?? DEFAULT_IGNORE_FILENAME;
|
|
17
|
+
return {
|
|
18
|
+
filename,
|
|
19
|
+
path: path.join(root, filename),
|
|
20
|
+
sharingSetups: Object.entries(settings.syncSetups)
|
|
21
|
+
.filter(([name, { sync }]) => name !== setup &&
|
|
22
|
+
sync.scope === "agent-directory" &&
|
|
23
|
+
sync.pathPolicy === "ignore-file" &&
|
|
24
|
+
(sync.ignoreFile ?? DEFAULT_IGNORE_FILENAME) === filename)
|
|
25
|
+
.map(([name]) => name)
|
|
26
|
+
.sort(),
|
|
27
|
+
};
|
|
28
|
+
}
|
|
29
|
+
function statAt(target) {
|
|
30
|
+
return lstatSync(target, { bigint: true, throwIfNoEntry: false });
|
|
31
|
+
}
|
|
32
|
+
function stale(target) {
|
|
33
|
+
throw new Error(`Ignore file changed or disappeared at "${target}"; reopen ignore-file settings.`);
|
|
34
|
+
}
|
|
35
|
+
function assertCurrent(target, expected) {
|
|
36
|
+
const current = statAt(target);
|
|
37
|
+
if (expected ? !current || !sameFile(expected, current) : current)
|
|
38
|
+
stale(target);
|
|
39
|
+
}
|
|
40
|
+
function validateTarget(execution, location) {
|
|
41
|
+
execution.validate();
|
|
42
|
+
const root = getEngineRoot();
|
|
43
|
+
if (location.filename === DEFAULT_IGNORE_FILENAME && statAt(path.join(root, DEFAULT_IGNORE_CREATION_RECORD))) {
|
|
44
|
+
throw new Error(`Ignore file "${location.path}" has an unfinished creation record; repair it before editing.`);
|
|
45
|
+
}
|
|
46
|
+
const target = statAt(location.path);
|
|
47
|
+
if (target &&
|
|
48
|
+
execution.liveClients().some(({ sessionFile }) => {
|
|
49
|
+
const session = sessionFile && statSync(sessionFile, { bigint: true, throwIfNoEntry: false });
|
|
50
|
+
return session && target.dev === session.dev && target.ino === session.ino;
|
|
51
|
+
}))
|
|
52
|
+
throw new Error(`Ignore file "${location.path}" is a protected live session.`);
|
|
53
|
+
return { root, target };
|
|
54
|
+
}
|
|
55
|
+
function editorDigest(setup, location, sourceDigest) {
|
|
56
|
+
return createHash("sha256")
|
|
57
|
+
.update(JSON.stringify(["pi-sync-ignore-editor-v1", setup, sourceDigest, location.sharingSetups]))
|
|
58
|
+
.digest("hex");
|
|
59
|
+
}
|
|
60
|
+
async function loadEditorSource(setup, location, execution) {
|
|
61
|
+
const { root, target: stat } = validateTarget(execution, location);
|
|
62
|
+
if (!stat)
|
|
63
|
+
return { location };
|
|
64
|
+
try {
|
|
65
|
+
const source = await readIgnoreSource(root, location.filename, execution.signal);
|
|
66
|
+
validateTarget(execution, location);
|
|
67
|
+
assertCurrent(location.path, stat);
|
|
68
|
+
if (!source.present || source.identity !== ignoreFileIdentity(stat))
|
|
69
|
+
stale(location.path);
|
|
70
|
+
compileIgnoreRules(source.text);
|
|
71
|
+
const { text, identity } = source;
|
|
72
|
+
return {
|
|
73
|
+
location,
|
|
74
|
+
existing: {
|
|
75
|
+
view: { ...location, text, sourceDigest: editorDigest(setup, location, source.sourceDigest), identity },
|
|
76
|
+
stat,
|
|
77
|
+
},
|
|
78
|
+
};
|
|
79
|
+
}
|
|
80
|
+
catch (cause) {
|
|
81
|
+
execution.validate();
|
|
82
|
+
throw new Error(`Cannot open ignore file "${location.path}": ${cause.message}`, { cause });
|
|
83
|
+
}
|
|
84
|
+
}
|
|
85
|
+
export async function readIgnoreFileEdit(payload, execution) {
|
|
86
|
+
if (!record(payload) || typeof payload.setup !== "string" || Object.keys(payload).some((key) => key !== "setup"))
|
|
87
|
+
throw new Error("Invalid ignore-file request.");
|
|
88
|
+
const { setup } = payload;
|
|
89
|
+
validateConfigName(setup, "sync setup");
|
|
90
|
+
const source = await withLockedLocalConfigDocument(async (document, recheck) => {
|
|
91
|
+
execution.validate();
|
|
92
|
+
const location = locateIgnoreFile(document, getEngineRoot(), setup);
|
|
93
|
+
const result = await loadEditorSource(setup, location, execution);
|
|
94
|
+
validateTarget(execution, location);
|
|
95
|
+
recheck();
|
|
96
|
+
assertCurrent(location.path, result.existing?.stat);
|
|
97
|
+
return result;
|
|
98
|
+
}, execution.signal);
|
|
99
|
+
validateTarget(execution, source.location);
|
|
100
|
+
assertCurrent(source.location.path, source.existing?.stat);
|
|
101
|
+
return source.existing?.view ?? source.location;
|
|
102
|
+
}
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
import { type IgnoreSource } from "./ignore-source.js";
|
|
2
|
+
export declare class AutomaticIgnoreCreationUnavailable extends Error {
|
|
3
|
+
}
|
|
4
|
+
export declare function checkAutomaticIgnoreCreation(root: string, signal: AbortSignal): Promise<void>;
|
|
5
|
+
export declare function initializeDefaultIgnoreSource(root: string, signal?: AbortSignal): Promise<IgnoreSource>;
|
|
6
|
+
export declare function prepareDefaultIgnoreSource(root: string, signal?: AbortSignal): Promise<{
|
|
7
|
+
source: IgnoreSource;
|
|
8
|
+
created: boolean;
|
|
9
|
+
}>;
|
|
@@ -0,0 +1,252 @@
|
|
|
1
|
+
import { constants, lstatSync, realpathSync, unlinkSync } from "node:fs";
|
|
2
|
+
import fs from "node:fs/promises";
|
|
3
|
+
import path from "node:path";
|
|
4
|
+
import { DEFAULT_IGNORE_CREATION_RECORD, DEFAULT_IGNORE_FILENAME, DEFAULT_IGNORE_RULES, ignoreFileIdentity, readIgnoreSource, sameFile, sameIdentity, } from "./ignore-source.js";
|
|
5
|
+
const starter = Buffer.from(DEFAULT_IGNORE_RULES);
|
|
6
|
+
const record = Buffer.from(`pi-sync-ignore-creation-v1\n${DEFAULT_IGNORE_RULES}`);
|
|
7
|
+
const noFollow = (constants.O_NOFOLLOW ?? 0) | (constants.O_NONBLOCK ?? 0);
|
|
8
|
+
const openRead = constants.O_RDONLY | noFollow;
|
|
9
|
+
const openExisting = process.platform === "win32" ? constants.O_RDWR | noFollow : openRead;
|
|
10
|
+
const openExclusive = constants.O_RDWR | constants.O_CREAT | constants.O_EXCL | noFollow;
|
|
11
|
+
export class AutomaticIgnoreCreationUnavailable extends Error {
|
|
12
|
+
}
|
|
13
|
+
export async function checkAutomaticIgnoreCreation(root, signal) {
|
|
14
|
+
signal.throwIfAborted();
|
|
15
|
+
root = path.resolve(root);
|
|
16
|
+
const expected = await fs.lstat(root, { bigint: true });
|
|
17
|
+
signal.throwIfAborted();
|
|
18
|
+
assertRoot(root, expected);
|
|
19
|
+
await checkCreationAccess(root);
|
|
20
|
+
const { directory } = await openCreationDirectory(root, expected);
|
|
21
|
+
try {
|
|
22
|
+
signal.throwIfAborted();
|
|
23
|
+
await syncCreationDirectory(directory);
|
|
24
|
+
assertRoot(root, expected);
|
|
25
|
+
}
|
|
26
|
+
finally {
|
|
27
|
+
await directory?.close();
|
|
28
|
+
signal.throwIfAborted();
|
|
29
|
+
}
|
|
30
|
+
}
|
|
31
|
+
export async function initializeDefaultIgnoreSource(root, signal) {
|
|
32
|
+
return (await prepareDefaultIgnoreSource(root, signal)).source;
|
|
33
|
+
}
|
|
34
|
+
export async function prepareDefaultIgnoreSource(root, signal) {
|
|
35
|
+
signal?.throwIfAborted();
|
|
36
|
+
root = path.resolve(root);
|
|
37
|
+
try {
|
|
38
|
+
const initialRoot = await fs.lstat(root, { bigint: true });
|
|
39
|
+
const verifyRoot = () => assertRoot(root, initialRoot);
|
|
40
|
+
const checkRoot = () => {
|
|
41
|
+
signal?.throwIfAborted();
|
|
42
|
+
verifyRoot();
|
|
43
|
+
};
|
|
44
|
+
checkRoot();
|
|
45
|
+
const priorRecord = statAt(path.join(root, DEFAULT_IGNORE_CREATION_RECORD));
|
|
46
|
+
if (!priorRecord) {
|
|
47
|
+
const source = await readIgnoreSource(root, DEFAULT_IGNORE_FILENAME, signal);
|
|
48
|
+
checkRoot();
|
|
49
|
+
if (source.present)
|
|
50
|
+
return { source, created: false };
|
|
51
|
+
}
|
|
52
|
+
await checkCreationAccess(root);
|
|
53
|
+
const { directory, anchor } = await openCreationDirectory(root, initialRoot);
|
|
54
|
+
try {
|
|
55
|
+
checkRoot();
|
|
56
|
+
const flushDirectory = await syncCreationDirectory(directory);
|
|
57
|
+
checkRoot();
|
|
58
|
+
const markerPath = path.join(anchor, DEFAULT_IGNORE_CREATION_RECORD);
|
|
59
|
+
const target = path.join(anchor, DEFAULT_IGNORE_FILENAME);
|
|
60
|
+
if (priorRecord)
|
|
61
|
+
assertOwned(priorRecord, DEFAULT_IGNORE_CREATION_RECORD, true);
|
|
62
|
+
else if (statAt(target))
|
|
63
|
+
changed(DEFAULT_IGNORE_FILENAME);
|
|
64
|
+
const marker = await fs.open(markerPath, priorRecord ? openExisting : openExclusive, 0o600).catch((error) => {
|
|
65
|
+
if (!priorRecord)
|
|
66
|
+
checkCreationError(error);
|
|
67
|
+
throw error;
|
|
68
|
+
});
|
|
69
|
+
try {
|
|
70
|
+
const markerStat = await flushAndVerify(marker, markerPath, path.join(root, DEFAULT_IGNORE_CREATION_RECORD), record, priorRecord ? checkRoot : verifyRoot, priorRecord, true);
|
|
71
|
+
await flushDirectory();
|
|
72
|
+
checkRoot();
|
|
73
|
+
assertCurrent(markerPath, markerStat);
|
|
74
|
+
const priorFile = priorRecord ? statAt(target) : undefined;
|
|
75
|
+
if (priorFile)
|
|
76
|
+
assertOwned(priorFile, DEFAULT_IGNORE_FILENAME, false);
|
|
77
|
+
const file = await fs.open(target, priorFile ? openExisting : openExclusive, 0o600).catch(async (cause) => {
|
|
78
|
+
if (!priorRecord && cause.code === "EEXIST") {
|
|
79
|
+
verifyRoot();
|
|
80
|
+
assertCurrent(markerPath, markerStat);
|
|
81
|
+
unlinkSync(markerPath);
|
|
82
|
+
await flushDirectory();
|
|
83
|
+
}
|
|
84
|
+
throw cause;
|
|
85
|
+
});
|
|
86
|
+
try {
|
|
87
|
+
const checkTargetRoot = priorFile ? checkRoot : verifyRoot;
|
|
88
|
+
const fileStat = await flushAndVerify(file, target, path.join(root, DEFAULT_IGNORE_FILENAME), starter, () => {
|
|
89
|
+
checkTargetRoot();
|
|
90
|
+
assertCurrent(markerPath, markerStat);
|
|
91
|
+
}, priorFile);
|
|
92
|
+
await flushDirectory();
|
|
93
|
+
checkTargetRoot();
|
|
94
|
+
assertCurrent(target, fileStat);
|
|
95
|
+
assertCurrent(markerPath, markerStat);
|
|
96
|
+
unlinkSync(markerPath);
|
|
97
|
+
await flushDirectory();
|
|
98
|
+
checkRoot();
|
|
99
|
+
const source = await readIgnoreSource(root, DEFAULT_IGNORE_FILENAME, signal);
|
|
100
|
+
checkRoot();
|
|
101
|
+
if (source.identity !== ignoreFileIdentity(fileStat) || source.text !== DEFAULT_IGNORE_RULES) {
|
|
102
|
+
changed(DEFAULT_IGNORE_FILENAME);
|
|
103
|
+
}
|
|
104
|
+
return { source, created: priorFile === undefined };
|
|
105
|
+
}
|
|
106
|
+
finally {
|
|
107
|
+
await file.close();
|
|
108
|
+
}
|
|
109
|
+
}
|
|
110
|
+
finally {
|
|
111
|
+
await marker.close();
|
|
112
|
+
}
|
|
113
|
+
}
|
|
114
|
+
finally {
|
|
115
|
+
await directory?.close();
|
|
116
|
+
signal?.throwIfAborted();
|
|
117
|
+
}
|
|
118
|
+
}
|
|
119
|
+
catch (cause) {
|
|
120
|
+
signal?.throwIfAborted();
|
|
121
|
+
if (cause instanceof AutomaticIgnoreCreationUnavailable)
|
|
122
|
+
throw cause;
|
|
123
|
+
const code = cause.code;
|
|
124
|
+
const detail = code === "EEXIST"
|
|
125
|
+
? "a file or creation record appeared concurrently; review the current files and retry"
|
|
126
|
+
: code
|
|
127
|
+
? "could not safely create or flush the file"
|
|
128
|
+
: cause.message;
|
|
129
|
+
throw new Error(`Ignore file "${DEFAULT_IGNORE_FILENAME}" initialization: ${detail}.`, { cause });
|
|
130
|
+
}
|
|
131
|
+
}
|
|
132
|
+
function checkCreationError(error) {
|
|
133
|
+
if (["EACCES", "EPERM", "EROFS", "ENOTSUP", "EOPNOTSUPP"].includes(error.code ?? "")) {
|
|
134
|
+
throw new AutomaticIgnoreCreationUnavailable("the filesystem does not permit automatic creation", { cause: error });
|
|
135
|
+
}
|
|
136
|
+
}
|
|
137
|
+
async function checkCreationAccess(root) {
|
|
138
|
+
try {
|
|
139
|
+
await fs.access(root, constants.W_OK);
|
|
140
|
+
}
|
|
141
|
+
catch (error) {
|
|
142
|
+
checkCreationError(error);
|
|
143
|
+
throw error;
|
|
144
|
+
}
|
|
145
|
+
}
|
|
146
|
+
function assertRoot(root, expected) {
|
|
147
|
+
const current = lstatSync(root, { bigint: true });
|
|
148
|
+
if (!current.isDirectory() || !sameIdentity(expected, current) || realpathSync(root) !== root) {
|
|
149
|
+
throw new Error("the Pi agent directory is not canonical or changed during initialization");
|
|
150
|
+
}
|
|
151
|
+
}
|
|
152
|
+
async function openCreationDirectory(root, expected) {
|
|
153
|
+
if (process.platform === "win32")
|
|
154
|
+
return { anchor: root };
|
|
155
|
+
const directory = await fs.open(root, openRead | (constants.O_DIRECTORY ?? 0));
|
|
156
|
+
try {
|
|
157
|
+
if (!sameIdentity(expected, await directory.stat({ bigint: true })))
|
|
158
|
+
changed("Pi agent directory");
|
|
159
|
+
return { directory, anchor: (await directoryAnchor(directory, expected)) ?? root };
|
|
160
|
+
}
|
|
161
|
+
catch (error) {
|
|
162
|
+
await directory.close();
|
|
163
|
+
throw error;
|
|
164
|
+
}
|
|
165
|
+
}
|
|
166
|
+
async function syncCreationDirectory(directory) {
|
|
167
|
+
if (!directory)
|
|
168
|
+
return async () => { };
|
|
169
|
+
try {
|
|
170
|
+
await directory.sync();
|
|
171
|
+
return () => directory.sync();
|
|
172
|
+
}
|
|
173
|
+
catch (error) {
|
|
174
|
+
if (["EBADF", "EINVAL", "ENOSYS", "ENOTSUP", "EOPNOTSUPP", "EPERM"].includes(error.code ?? "")) {
|
|
175
|
+
if (process.platform === "darwin")
|
|
176
|
+
return async () => { };
|
|
177
|
+
throw new AutomaticIgnoreCreationUnavailable("the filesystem does not support flushing the Pi agent directory");
|
|
178
|
+
}
|
|
179
|
+
throw error;
|
|
180
|
+
}
|
|
181
|
+
}
|
|
182
|
+
async function directoryAnchor(directory, expected) {
|
|
183
|
+
for (const base of ["/proc/self/fd", "/dev/fd"]) {
|
|
184
|
+
const candidate = `${base}/${directory.fd}`;
|
|
185
|
+
const observed = await fs.stat(`${candidate}/.`, { bigint: true }).catch((error) => {
|
|
186
|
+
if (["ENOENT", "ENOTDIR", "EACCES", "EPERM"].includes(error.code ?? ""))
|
|
187
|
+
return undefined;
|
|
188
|
+
throw error;
|
|
189
|
+
});
|
|
190
|
+
if (observed?.isDirectory() && sameIdentity(expected, observed))
|
|
191
|
+
return candidate;
|
|
192
|
+
}
|
|
193
|
+
}
|
|
194
|
+
function statAt(target) {
|
|
195
|
+
return lstatSync(target, { bigint: true, throwIfNoEntry: false });
|
|
196
|
+
}
|
|
197
|
+
function changed(name) {
|
|
198
|
+
throw new Error(`"${name}" changed during initialization; review the current files and retry`);
|
|
199
|
+
}
|
|
200
|
+
function assertCurrent(target, expected) {
|
|
201
|
+
const current = statAt(target);
|
|
202
|
+
if (!current || !sameFile(expected, current))
|
|
203
|
+
changed(path.basename(target));
|
|
204
|
+
}
|
|
205
|
+
function assertOwned(stat, name, privateRecord) {
|
|
206
|
+
if (!stat.isFile() ||
|
|
207
|
+
stat.nlink !== 1n ||
|
|
208
|
+
(process.getuid && stat.uid !== BigInt(process.getuid())) ||
|
|
209
|
+
(privateRecord && process.platform !== "win32" && (stat.mode & 63n) !== 0n)) {
|
|
210
|
+
throw new Error(`unsafe "${name}"; preserve it for manual recovery`);
|
|
211
|
+
}
|
|
212
|
+
}
|
|
213
|
+
async function flushAndVerify(handle, target, canonical, expected, checkRoot, prior, privateRecord = false) {
|
|
214
|
+
const name = path.basename(target);
|
|
215
|
+
const opened = await handle.stat({ bigint: true });
|
|
216
|
+
assertOwned(opened, name, privateRecord);
|
|
217
|
+
if (prior && !sameFile(prior, opened))
|
|
218
|
+
changed(name);
|
|
219
|
+
checkRoot();
|
|
220
|
+
if ((await fs.realpath(target)) !== canonical)
|
|
221
|
+
throw new Error(`use the exact on-disk filename "${name}"`);
|
|
222
|
+
checkRoot();
|
|
223
|
+
assertCurrent(target, opened);
|
|
224
|
+
if (!prior)
|
|
225
|
+
await handle.writeFile(expected);
|
|
226
|
+
const before = await handle.stat({ bigint: true });
|
|
227
|
+
assertOwned(before, name, privateRecord);
|
|
228
|
+
if (!(prior ? sameFile(opened, before) : sameIdentity(opened, before)))
|
|
229
|
+
changed(name);
|
|
230
|
+
checkRoot();
|
|
231
|
+
assertCurrent(target, before);
|
|
232
|
+
const observed = Buffer.alloc(expected.length + 1);
|
|
233
|
+
let count = 0;
|
|
234
|
+
while (count < observed.length) {
|
|
235
|
+
checkRoot();
|
|
236
|
+
const { bytesRead } = await handle.read(observed, count, observed.length - count, count);
|
|
237
|
+
if (bytesRead === 0)
|
|
238
|
+
break;
|
|
239
|
+
count += bytesRead;
|
|
240
|
+
}
|
|
241
|
+
if (count !== expected.length || !observed.subarray(0, count).equals(expected)) {
|
|
242
|
+
throw new Error(privateRecord
|
|
243
|
+
? `invalid creation record "${name}"; preserve it for manual recovery`
|
|
244
|
+
: `unfinished creation of "${name}"; move the incomplete file outside the Pi agent directory or restore the full starter rules, then retry confirmed setup or migration`);
|
|
245
|
+
}
|
|
246
|
+
await handle.sync();
|
|
247
|
+
if (!sameFile(before, await handle.stat({ bigint: true })))
|
|
248
|
+
changed(name);
|
|
249
|
+
checkRoot();
|
|
250
|
+
assertCurrent(target, before);
|
|
251
|
+
return before;
|
|
252
|
+
}
|
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
import { type Execution } from "../../protocol.js";
|
|
2
|
+
import { type LocalConfigDocument } from "../settings/config-file.js";
|
|
3
|
+
export declare function assertCurrentIgnoreMigrationOffer(document: LocalConfigDocument | undefined, root: string, setup: string, token: string): void;
|
|
4
|
+
export declare function readIgnoreMigrationOffer(payload: unknown, execution: Execution): Promise<{
|
|
5
|
+
token: string;
|
|
6
|
+
} | null>;
|
|
7
|
+
export declare function deferIgnoreMigrationOffer(payload: unknown, execution: Execution): Promise<true>;
|
|
@@ -0,0 +1,139 @@
|
|
|
1
|
+
import { createHash } from "node:crypto";
|
|
2
|
+
import { closeSync, constants, fstatSync, fsync, lstatSync, openSync, realpathSync } from "node:fs";
|
|
3
|
+
import path from "node:path";
|
|
4
|
+
import { promisify } from "node:util";
|
|
5
|
+
import { documentFingerprint } from "../../document.js";
|
|
6
|
+
import { record } from "../../protocol.js";
|
|
7
|
+
import { getEngineRoot } from "../host.js";
|
|
8
|
+
import { withLockedLocalConfigDocument } from "../settings/config-file.js";
|
|
9
|
+
import { validateConfigName, validateSettingsDocument } from "../settings/settings-validation.js";
|
|
10
|
+
import { sameFile, sameIdentity } from "./ignore-source.js";
|
|
11
|
+
const MIGRATION_VERSION = 1;
|
|
12
|
+
const flush = promisify(fsync);
|
|
13
|
+
function markerPath(root, setup) {
|
|
14
|
+
const key = createHash("sha256")
|
|
15
|
+
.update(JSON.stringify([setup, MIGRATION_VERSION]))
|
|
16
|
+
.digest("hex");
|
|
17
|
+
return path.join(root, `.pi-sync.json.ignore-migration-${key}.deferred`);
|
|
18
|
+
}
|
|
19
|
+
function markerAt(target) {
|
|
20
|
+
const marker = lstatSync(target, { bigint: true, throwIfNoEntry: false });
|
|
21
|
+
if (marker &&
|
|
22
|
+
(!marker.isFile() ||
|
|
23
|
+
marker.nlink !== 1n ||
|
|
24
|
+
marker.size !== 0n ||
|
|
25
|
+
(process.getuid && marker.uid !== BigInt(process.getuid())) ||
|
|
26
|
+
(process.platform !== "win32" && (marker.mode & 63n) !== 0n) ||
|
|
27
|
+
realpathSync(target) !== target))
|
|
28
|
+
throw new Error(`Unsafe ignore migration deferral marker at "${target}"; preserve it for inspection.`);
|
|
29
|
+
return marker;
|
|
30
|
+
}
|
|
31
|
+
function eligibleToken(document, setup) {
|
|
32
|
+
if (!document)
|
|
33
|
+
return null;
|
|
34
|
+
const settings = validateSettingsDocument(document.parsed);
|
|
35
|
+
if (settings.activeSyncSetup !== setup ||
|
|
36
|
+
!Object.hasOwn(settings.syncSetups, setup) ||
|
|
37
|
+
settings.syncSetups[setup].sync.pathPolicy === "ignore-file")
|
|
38
|
+
return null;
|
|
39
|
+
const fingerprint = documentFingerprint({ bytes: document.bytes.toString("base64"), identity: document.identity });
|
|
40
|
+
return createHash("sha256")
|
|
41
|
+
.update(JSON.stringify([MIGRATION_VERSION, document.path, fingerprint]))
|
|
42
|
+
.digest("hex");
|
|
43
|
+
}
|
|
44
|
+
function stale() {
|
|
45
|
+
throw new Error("Ignore migration offer is stale; reopen Pi Sync.");
|
|
46
|
+
}
|
|
47
|
+
export function assertCurrentIgnoreMigrationOffer(document, root, setup, token) {
|
|
48
|
+
if (eligibleToken(document, setup) !== token || markerAt(markerPath(root, setup)))
|
|
49
|
+
stale();
|
|
50
|
+
}
|
|
51
|
+
function validateClient(execution) {
|
|
52
|
+
execution.validate();
|
|
53
|
+
const facts = execution.facts();
|
|
54
|
+
if (!execution.clientId || facts?.id !== execution.clientId || facts.mode !== "tui" || !facts.hasUI)
|
|
55
|
+
throw new Error("Ignore migration offers require an attached TUI client.");
|
|
56
|
+
}
|
|
57
|
+
function readRequest(payload, execution, fields) {
|
|
58
|
+
validateClient(execution);
|
|
59
|
+
if (!record(payload) ||
|
|
60
|
+
typeof payload.setup !== "string" ||
|
|
61
|
+
Object.keys(payload).some((key) => !fields.includes(key)))
|
|
62
|
+
throw new Error("Invalid ignore migration offer request.");
|
|
63
|
+
validateConfigName(payload.setup, "sync setup");
|
|
64
|
+
return payload.setup;
|
|
65
|
+
}
|
|
66
|
+
export async function readIgnoreMigrationOffer(payload, execution) {
|
|
67
|
+
const setup = readRequest(payload, execution, ["setup"]);
|
|
68
|
+
const result = await withLockedLocalConfigDocument(async (document, recheck) => {
|
|
69
|
+
validateClient(execution);
|
|
70
|
+
const token = eligibleToken(document, setup);
|
|
71
|
+
const deferred = token && markerAt(markerPath(getEngineRoot(), setup));
|
|
72
|
+
recheck();
|
|
73
|
+
return token && !deferred ? { token } : null;
|
|
74
|
+
}, execution.signal, false, { securePermissions: false });
|
|
75
|
+
validateClient(execution);
|
|
76
|
+
return result;
|
|
77
|
+
}
|
|
78
|
+
export function deferIgnoreMigrationOffer(payload, execution) {
|
|
79
|
+
const setup = readRequest(payload, execution, ["setup", "token"]);
|
|
80
|
+
if (!record(payload) || typeof payload.token !== "string" || !/^[a-f0-9]{64}$/u.test(payload.token))
|
|
81
|
+
throw new Error("Invalid ignore migration offer token.");
|
|
82
|
+
const { token } = payload;
|
|
83
|
+
return withLockedLocalConfigDocument(async (document, recheck) => {
|
|
84
|
+
validateClient(execution);
|
|
85
|
+
const root = getEngineRoot();
|
|
86
|
+
const target = markerPath(root, setup);
|
|
87
|
+
assertCurrentIgnoreMigrationOffer(document, root, setup, token);
|
|
88
|
+
await createMarker(root, target, () => {
|
|
89
|
+
if (execution.liveClients().some((peer) => peer.sessionFile && path.resolve(peer.sessionFile) === target))
|
|
90
|
+
throw new Error("Cannot defer ignore migration at a protected live session path.");
|
|
91
|
+
validateClient(execution);
|
|
92
|
+
recheck();
|
|
93
|
+
});
|
|
94
|
+
return true;
|
|
95
|
+
}, execution.signal, true, { securePermissions: false });
|
|
96
|
+
}
|
|
97
|
+
async function createMarker(root, target, beforeCreate) {
|
|
98
|
+
const noFollow = constants.O_NOFOLLOW ?? 0;
|
|
99
|
+
const directory = process.platform === "win32" ? undefined : openSync(root, constants.O_RDONLY | constants.O_DIRECTORY | noFollow);
|
|
100
|
+
try {
|
|
101
|
+
beforeCreate();
|
|
102
|
+
if (directory !== undefined &&
|
|
103
|
+
!sameIdentity(lstatSync(getEngineRoot(), { bigint: true }), fstatSync(directory, { bigint: true })))
|
|
104
|
+
throw new Error("The Pi agent directory changed before recording ignore migration deferral.");
|
|
105
|
+
const marker = openMarker(target, noFollow);
|
|
106
|
+
try {
|
|
107
|
+
const installed = fstatSync(marker, { bigint: true });
|
|
108
|
+
try {
|
|
109
|
+
await flush(marker);
|
|
110
|
+
if (directory !== undefined)
|
|
111
|
+
await flush(directory);
|
|
112
|
+
}
|
|
113
|
+
catch (cause) {
|
|
114
|
+
throw new Error("Ignore migration was deferred but marker durability could not be verified.", { cause });
|
|
115
|
+
}
|
|
116
|
+
getEngineRoot();
|
|
117
|
+
const current = markerAt(target);
|
|
118
|
+
if (!current || !sameFile(installed, current))
|
|
119
|
+
throw new Error("Ignore migration deferral marker changed during publication.");
|
|
120
|
+
}
|
|
121
|
+
finally {
|
|
122
|
+
closeSync(marker);
|
|
123
|
+
}
|
|
124
|
+
}
|
|
125
|
+
finally {
|
|
126
|
+
if (directory !== undefined)
|
|
127
|
+
closeSync(directory);
|
|
128
|
+
}
|
|
129
|
+
}
|
|
130
|
+
function openMarker(target, noFollow) {
|
|
131
|
+
try {
|
|
132
|
+
return openSync(target, constants.O_WRONLY | constants.O_CREAT | constants.O_EXCL | noFollow, 0o600);
|
|
133
|
+
}
|
|
134
|
+
catch (error) {
|
|
135
|
+
if (error.code === "EEXIST")
|
|
136
|
+
stale();
|
|
137
|
+
throw error;
|
|
138
|
+
}
|
|
139
|
+
}
|
|
@@ -0,0 +1,30 @@
|
|
|
1
|
+
import { type Execution } from "../../protocol.js";
|
|
2
|
+
import type { PiSyncSettingsV3 } from "../settings/settings-types.js";
|
|
3
|
+
import { type IgnoreSource } from "./ignore-source.js";
|
|
4
|
+
export declare function runIgnorePolicyChange(operation: "ignore.migrate" | "ignore.switch", payload: unknown, execution: Execution): Promise<"cancelled" | "saved">;
|
|
5
|
+
export declare function newIgnoreFilename(before: PiSyncSettingsV3 | undefined, next: PiSyncSettingsV3): string | undefined;
|
|
6
|
+
type ReviewedIgnoreSource = IgnoreSource & {
|
|
7
|
+
pendingCreation?: string;
|
|
8
|
+
};
|
|
9
|
+
export declare function reviewNewIgnoreSource(root: string, filename: string, execution: Execution): Promise<({
|
|
10
|
+
filename: string;
|
|
11
|
+
text: string;
|
|
12
|
+
sourceDigest: string;
|
|
13
|
+
} & {
|
|
14
|
+
present: false;
|
|
15
|
+
identity?: undefined;
|
|
16
|
+
}) | ({
|
|
17
|
+
filename: string;
|
|
18
|
+
text: string;
|
|
19
|
+
sourceDigest: string;
|
|
20
|
+
} & {
|
|
21
|
+
present: true;
|
|
22
|
+
identity: string;
|
|
23
|
+
})>;
|
|
24
|
+
export declare function prepareReviewedIgnoreSource(root: string, reviewed: ReviewedIgnoreSource, execution: Execution): Promise<{
|
|
25
|
+
source: IgnoreSource;
|
|
26
|
+
created: boolean;
|
|
27
|
+
}>;
|
|
28
|
+
export declare function switchIgnoreFile(execution: Execution, setupName: string, filename: string, expectedFilename: string): Promise<"cancelled" | "saved">;
|
|
29
|
+
export declare function migrateIgnoreSetup(execution: Execution, setupName: string, offerToken?: string): Promise<"cancelled" | "saved">;
|
|
30
|
+
export {};
|