@narumitw/pi-sync-daemon 0.8.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.
Files changed (54) hide show
  1. package/CHANGELOG.md +10 -0
  2. package/dist/cli.js +1 -1
  3. package/dist/consent.d.ts +14 -2
  4. package/dist/consent.js +134 -16
  5. package/dist/engine/backends/git/git-backend.d.ts +1 -2
  6. package/dist/engine/backends/git/git-backend.js +61 -30
  7. package/dist/engine/commands/command-types.d.ts +2 -1
  8. package/dist/engine/host.d.ts +1 -0
  9. package/dist/engine/ignore/ignore-initialization.d.ts +3 -0
  10. package/dist/engine/ignore/ignore-initialization.js +93 -27
  11. package/dist/engine/ignore/ignore-settings.d.ts +16 -1
  12. package/dist/engine/ignore/ignore-settings.js +85 -14
  13. package/dist/engine/settings/config-file.js +1 -1
  14. package/dist/engine/settings/config.d.ts +3 -2
  15. package/dist/engine/settings/config.js +71 -28
  16. package/dist/engine/settings/settings-management.d.ts +3 -1
  17. package/dist/engine/settings/settings-management.js +8 -2
  18. package/dist/engine/settings/settings-types.d.ts +2 -0
  19. package/dist/engine/snapshot/session-paths.js +7 -2
  20. package/dist/engine/snapshot/snapshot-apply.d.ts +1 -1
  21. package/dist/engine/snapshot/snapshot-transaction-policy.d.ts +7 -2
  22. package/dist/engine/snapshot/snapshot-transaction-policy.js +37 -17
  23. package/dist/engine/snapshot/snapshot-transaction.js +55 -13
  24. package/dist/engine/snapshot/snapshot-types.d.ts +4 -0
  25. package/dist/engine/sync/agent-plan.js +1 -0
  26. package/dist/engine/sync/agent-sync.js +8 -1
  27. package/dist/engine/sync/continuous-sync.d.ts +3 -1
  28. package/dist/engine/sync/continuous-sync.js +32 -15
  29. package/dist/engine/sync/merge-acceptance.d.ts +8 -0
  30. package/dist/engine/sync/merge-acceptance.js +29 -0
  31. package/dist/engine/sync/merge-apply.js +7 -4
  32. package/dist/engine/sync/merge-journal.d.ts +6 -0
  33. package/dist/engine/sync/merge-journal.js +33 -14
  34. package/dist/engine/sync/merged-sync.js +31 -39
  35. package/dist/engine/sync/remote-snapshot.d.ts +2 -1
  36. package/dist/engine/sync/remote-snapshot.js +17 -2
  37. package/dist/engine/sync/session-push.js +20 -6
  38. package/dist/engine/sync/sync-mutations.js +71 -50
  39. package/dist/engine/sync/sync-policy.d.ts +5 -0
  40. package/dist/engine/sync/sync-policy.js +28 -3
  41. package/dist/engine/sync/sync-state.d.ts +2 -0
  42. package/dist/engine/sync/sync-state.js +3 -8
  43. package/dist/engine/sync/transfer-freshness.d.ts +31 -0
  44. package/dist/engine/sync/transfer-freshness.js +82 -0
  45. package/dist/engine-service.d.ts +1 -1
  46. package/dist/engine-service.js +124 -27
  47. package/dist/monitor.d.ts +4 -1
  48. package/dist/monitor.js +84 -32
  49. package/dist/server.d.ts +1 -0
  50. package/dist/server.js +46 -5
  51. package/dist/service-host.d.ts +1 -1
  52. package/dist/service-host.js +2 -1
  53. package/docs/architecture.md +13 -5
  54. package/package.json +1 -1
@@ -1,11 +1,26 @@
1
1
  import { type Execution } from "../../protocol.js";
2
2
  import type { PiSyncSettingsV3 } from "../settings/settings-types.js";
3
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">;
4
5
  export declare function newIgnoreFilename(before: PiSyncSettingsV3 | undefined, next: PiSyncSettingsV3): string | undefined;
5
6
  type ReviewedIgnoreSource = IgnoreSource & {
6
7
  pendingCreation?: string;
7
8
  };
8
- export declare function reviewNewIgnoreSource(root: string, filename: string, execution: Execution): Promise<ReviewedIgnoreSource>;
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
+ })>;
9
24
  export declare function prepareReviewedIgnoreSource(root: string, reviewed: ReviewedIgnoreSource, execution: Execution): Promise<{
10
25
  source: IgnoreSource;
11
26
  created: boolean;
@@ -1,15 +1,16 @@
1
1
  import { createHash, randomUUID } from "node:crypto";
2
+ import { lstatSync } from "node:fs";
2
3
  import fs from "node:fs/promises";
3
4
  import path from "node:path";
4
5
  import { requireHandover } from "../../consent.js";
5
6
  import { documentFingerprint } from "../../document.js";
6
- import { MAX_REVIEW_BYTES } from "../../protocol.js";
7
+ import { MAX_REVIEW_BYTES, record } from "../../protocol.js";
7
8
  import { createServiceContext } from "../../service-host.js";
8
9
  import { createSyncBackend } from "../backends/backend-factory.js";
9
10
  import { engineHost } from "../host.js";
10
11
  import { configFromSettings } from "../settings/config.js";
11
12
  import { peekLocalConfigDocument, updateLocalConfigDocument, } from "../settings/config-file.js";
12
- import { validateSettingsDocument } from "../settings/settings-validation.js";
13
+ import { validateConfigName, validateSettingsDocument } from "../settings/settings-validation.js";
13
14
  import { resolvedSnapshotOptionsForContext } from "../snapshot/session-paths.js";
14
15
  import { createSnapshot } from "../snapshot/snapshot.js";
15
16
  import { pendingTransactionEntries } from "../snapshot/snapshot-transaction-directory.js";
@@ -18,11 +19,32 @@ import { withLock } from "../state/lock.js";
18
19
  import { stateDir, withStateDirectoryAccess } from "../state/state-directory.js";
19
20
  import { requireNoMergeJournal } from "../sync/merge-journal.js";
20
21
  import { configuredSnapshotPathMatcher } from "../sync/sync-policy.js";
21
- import { safeTerminalText } from "../ui/terminal-text.js";
22
+ import { safeTerminalText, snapshotPathLabel } from "../ui/terminal-text.js";
22
23
  import { compileEffectiveIgnorePolicy } from "./effective-ignore-policy.js";
23
- import { prepareDefaultIgnoreSource } from "./ignore-initialization.js";
24
+ import { AutomaticIgnoreCreationUnavailable, checkAutomaticIgnoreCreation, prepareDefaultIgnoreSource, } from "./ignore-initialization.js";
24
25
  import { assertCurrentIgnoreMigrationOffer } from "./ignore-migration-prompt.js";
25
26
  import { compileIgnoreRules, DEFAULT_IGNORE_CREATION_RECORD, DEFAULT_IGNORE_FILENAME, DEFAULT_IGNORE_RULES, IgnoreCreationPendingError, ignoreSourceDigest, readIgnoreSource, validateIgnoreFilename, } from "./ignore-source.js";
27
+ export function runIgnorePolicyChange(operation, payload, execution) {
28
+ if (!record(payload) || typeof payload.setup !== "string")
29
+ throw new Error("Invalid ignore-file policy request.");
30
+ const { setup } = payload;
31
+ validateConfigName(setup, "sync setup");
32
+ if (operation === "ignore.migrate") {
33
+ const { offerToken } = payload;
34
+ if (Object.keys(payload).some((key) => key !== "setup" && key !== "offerToken") ||
35
+ (offerToken !== undefined && (typeof offerToken !== "string" || !/^[a-f0-9]{64}$/u.test(offerToken))))
36
+ throw new Error("Invalid ignore-file migration request.");
37
+ if (offerToken !== undefined && execution.facts()?.mode !== "tui")
38
+ throw new Error("Ignore migration offers require an attached TUI client.");
39
+ return migrateIgnoreSetup(execution, setup, offerToken);
40
+ }
41
+ const { filename, expectedFilename } = payload;
42
+ if (typeof filename !== "string" ||
43
+ typeof expectedFilename !== "string" ||
44
+ Object.keys(payload).some((key) => key !== "setup" && key !== "filename" && key !== "expectedFilename"))
45
+ throw new Error("Invalid ignore-file switch request.");
46
+ return switchIgnoreFile(execution, setup, filename, expectedFilename);
47
+ }
26
48
  function sourceToken(document) {
27
49
  return documentFingerprint(document ? { bytes: document.bytes.toString("base64"), identity: document.identity } : undefined);
28
50
  }
@@ -86,10 +108,47 @@ async function readSetupSource(root, filename, signal) {
86
108
  };
87
109
  }
88
110
  }
111
+ async function readPolicyReviewSource(root, filename, execution, allowCreation) {
112
+ const { signal } = execution;
113
+ execution.validate();
114
+ signal.throwIfAborted();
115
+ filename = validateIgnoreFilename(filename);
116
+ if (filename !== DEFAULT_IGNORE_FILENAME &&
117
+ lstatSync(path.resolve(root), { throwIfNoEntry: false })?.isDirectory() &&
118
+ !lstatSync(path.resolve(root, filename), { throwIfNoEntry: false })) {
119
+ throw manualCreationRequired(root, filename);
120
+ }
121
+ const source = await (allowCreation
122
+ ? readSetupSource(root, filename, signal)
123
+ : readIgnoreSource(root, filename, signal));
124
+ execution.validate();
125
+ if (source.present)
126
+ return source;
127
+ if (!allowCreation)
128
+ throw manualCreationRequired(root, filename);
129
+ await checkAutomaticIgnoreCreation(root, signal).catch((error) => {
130
+ execution.validate();
131
+ throw creationFailure(root, source, error);
132
+ });
133
+ execution.validate();
134
+ return source;
135
+ }
136
+ function manualCreationRequired(root, filename, cause) {
137
+ return new Error(`Ignore file does not exist: ${snapshotPathLabel(path.resolve(root, filename))}\n` +
138
+ `${cause ? `${cause.message}. ` : ""}Create a regular UTF-8 text file there manually with these starter rules, then retry the Pi Sync policy change:\n${DEFAULT_IGNORE_RULES}`, { cause });
139
+ }
140
+ function creationFailure(root, source, error) {
141
+ if (!(error instanceof AutomaticIgnoreCreationUnavailable))
142
+ return error;
143
+ if (!source.pendingCreation)
144
+ return manualCreationRequired(root, source.filename, error);
145
+ return new Error(`Interrupted creation of ${snapshotPathLabel(path.resolve(root, source.filename))}. ${error.message}. ` +
146
+ `Preserve the rule file and creation record ${snapshotPathLabel(path.resolve(root, DEFAULT_IGNORE_CREATION_RECORD))}. ` +
147
+ "Retry the reviewed Pi Sync policy change where safe automatic recovery is available.", { cause: error });
148
+ }
89
149
  export async function reviewNewIgnoreSource(root, filename, execution) {
90
150
  const ctx = requireInteractive(execution);
91
- const source = await readSetupSource(root, filename, execution.signal);
92
- execution.validate();
151
+ const source = await readPolicyReviewSource(root, filename, execution, true);
93
152
  compileIgnoreRules(source.text);
94
153
  const description = describeSource(source);
95
154
  if (!(await confirm(ctx, execution, "Review ignore-file policy", [
@@ -111,7 +170,10 @@ export async function prepareReviewedIgnoreSource(root, reviewed, execution) {
111
170
  stale();
112
171
  if (current.present)
113
172
  return { source: current, created: false };
114
- const prepared = await prepareDefaultIgnoreSource(root, execution.signal);
173
+ const prepared = await prepareDefaultIgnoreSource(root, execution.signal).catch((error) => {
174
+ execution.validate();
175
+ throw creationFailure(root, current, error);
176
+ });
115
177
  execution.validate();
116
178
  if (!prepared.source.present ||
117
179
  prepared.source.text !== reviewed.text ||
@@ -123,6 +185,10 @@ export async function switchIgnoreFile(execution, setupName, filename, expectedF
123
185
  const ctx = requireInteractive(execution);
124
186
  filename = validateIgnoreFilename(filename);
125
187
  expectedFilename = validateIgnoreFilename(expectedFilename);
188
+ if (expectedFilename !== filename) {
189
+ await readPolicyReviewSource(execution.root, filename, execution, false);
190
+ execution.validate();
191
+ }
126
192
  await requireHandover(execution);
127
193
  execution.validate();
128
194
  return withStateDirectoryAccess(() => withLock("sync", async () => {
@@ -139,10 +205,8 @@ export async function switchIgnoreFile(execution, setupName, filename, expectedF
139
205
  if ((await pendingTransactionEntries()).some((entry) => entry.isDirectory()))
140
206
  throw new Error("Resolve pending local recovery before changing the content policy.");
141
207
  await requireNoMergeJournal(configFromSettings(settings, setupName));
142
- const source = await readIgnoreSource(execution.root, filename, execution.signal);
208
+ const source = await readPolicyReviewSource(execution.root, filename, execution, false);
143
209
  execution.validate();
144
- if (!source.present)
145
- throw new Error(`Ignore file does not exist: ${safeTerminalText(path.join(execution.root, filename))}`);
146
210
  compileIgnoreRules(source.text);
147
211
  if (!(await confirm(ctx, execution, "Review changing the ignore file", [
148
212
  `Setup: ${safeTerminalText(setupName)}`,
@@ -162,6 +226,8 @@ export async function switchIgnoreFile(execution, setupName, filename, expectedF
162
226
  }
163
227
  export async function migrateIgnoreSetup(execution, setupName, offerToken) {
164
228
  const ctx = requireInteractive(execution);
229
+ await readPolicyReviewSource(execution.root, DEFAULT_IGNORE_FILENAME, execution, true);
230
+ execution.validate();
165
231
  await requireHandover(execution);
166
232
  execution.validate();
167
233
  return withStateDirectoryAccess(() => withLock("sync", async () => {
@@ -169,8 +235,13 @@ export async function migrateIgnoreSetup(execution, setupName, offerToken) {
169
235
  execution.validate();
170
236
  if (!document)
171
237
  throw new Error("No Pi Sync settings to migrate.");
172
- if (offerToken !== undefined)
173
- assertCurrentIgnoreMigrationOffer(document, execution.root, setupName, offerToken);
238
+ const guardSettings = settingsReviewGuard(document, execution);
239
+ const validateReview = (candidate) => {
240
+ guardSettings(candidate);
241
+ if (offerToken !== undefined)
242
+ assertCurrentIgnoreMigrationOffer(candidate, execution.root, setupName, offerToken);
243
+ };
244
+ validateReview(document);
174
245
  const before = validateSettingsDocument(document.parsed);
175
246
  const setup = Object.hasOwn(before.syncSetups, setupName) ? before.syncSetups[setupName] : undefined;
176
247
  if (!setup || ignoreFilename(setup))
@@ -188,7 +259,7 @@ export async function migrateIgnoreSetup(execution, setupName, offerToken) {
188
259
  next.syncSetups[setupName].sync = { ...common, scope: "agent-directory", sessions, pathPolicy: "ignore-file" };
189
260
  next.version = 8;
190
261
  validateSettingsDocument(next);
191
- const source = await readSetupSource(execution.root, DEFAULT_IGNORE_FILENAME, execution.signal);
262
+ const source = await readPolicyReviewSource(execution.root, DEFAULT_IGNORE_FILENAME, execution, true);
192
263
  execution.validate();
193
264
  const current = {
194
265
  ...configFromSettings(next, setupName),
@@ -242,7 +313,7 @@ export async function migrateIgnoreSetup(execution, setupName, offerToken) {
242
313
  : fresh)
243
314
  stale();
244
315
  };
245
- await updateLocalConfigDocument(next, () => next, validateSettingsDocument, execution.signal, settingsReviewGuard(document, execution), async () => {
316
+ await updateLocalConfigDocument(next, () => next, validateSettingsDocument, execution.signal, validateReview, async () => {
246
317
  verifyHead(await backend.readHead(execution.signal));
247
318
  execution.validate();
248
319
  if (!source.present && (await collect()).fingerprint !== review.fingerprint)
@@ -197,7 +197,7 @@ export function updateLocalConfigDocument(defaultValue, update, validate, signal
197
197
  signal?.throwIfAborted();
198
198
  if (document && JSON.stringify(document.parsed) === JSON.stringify(next))
199
199
  return next;
200
- if (beforePublish && document?.path !== localConfigPath()) {
200
+ if (beforePublish && document && document.path !== localConfigPath()) {
201
201
  throw new Error("This reviewed settings change requires pi-sync.json. No settings were saved; use the canonical filename and reopen the review.");
202
202
  }
203
203
  const review = reviewed
@@ -1,8 +1,8 @@
1
1
  import { type SyncSelectionConfig } from "../sync/sync-policy.js";
2
2
  import type { AnySyncConfig, PartialConfig, PiSyncSettingsV3, StorageConnectionSettings, SyncSetupSettings } from "./settings-types.js";
3
- export declare function loadConfig(setupName?: string): Promise<AnySyncConfig>;
3
+ export declare function loadConfig(setupName?: string, signal?: AbortSignal): Promise<AnySyncConfig>;
4
4
  /** A background freshness read must never trigger legacy-file publication. */
5
- export declare function loadConfigForCheck(): Promise<AnySyncConfig>;
5
+ export declare function loadConfigForCheck(setupName?: string, signal?: AbortSignal): Promise<AnySyncConfig>;
6
6
  export declare function loadSettingsConfig(setupName?: string): Promise<AnySyncConfig>;
7
7
  export declare function requireLegacyOperationConfig(config: AnySyncConfig): AnySyncConfig;
8
8
  export declare function resolveIgnoreConfig(settings: PiSyncSettingsV3, setupName?: string, signal?: AbortSignal): Promise<AnySyncConfig>;
@@ -16,6 +16,7 @@ export declare function syncConfigReviewIdentity(config: AnySyncConfig): string;
16
16
  export declare function syncConfigReviewFingerprint(config: AnySyncConfig): string;
17
17
  /** Internal freshness token, including credentials; never display or persist it. */
18
18
  export declare function syncCheckConfigFingerprint(config: AnySyncConfig): string;
19
+ export declare function syncSettingsConfigFingerprint(config: AnySyncConfig): string;
19
20
  export declare function sessionTokenWarnings(config: {
20
21
  endpoint?: string;
21
22
  sessionToken?: string;
@@ -1,22 +1,39 @@
1
1
  import { createHash } from "node:crypto";
2
+ import path from "node:path";
3
+ import { documentFingerprint } from "../../document.js";
2
4
  import { backendIdentityCoordinates } from "../backends/backend-identity.js";
3
5
  import { normalizeGitBranch, normalizeGitDirectory, normalizeGitRemote } from "../backends/git/git-config.js";
4
6
  import { normalizeWebDavPath, normalizeWebDavUrl } from "../backends/webdav/webdav-config.js";
5
- import { getEngineRoot } from "../host.js";
6
- import { loadEffectiveIgnorePolicy } from "../ignore/effective-ignore-policy.js";
7
+ import { engineHost, getEngineRoot } from "../host.js";
8
+ import { compileEffectiveIgnorePolicy } from "../ignore/effective-ignore-policy.js";
7
9
  import { DEFAULT_IGNORE_FILENAME, readIgnoreSource, validateIgnoreFilename } from "../ignore/ignore-source.js";
8
10
  import { normalizeSyncExclude } from "../sync/exclude-policy.js";
9
11
  import { normalizeLocalFields } from "../sync/local-field-policy.js";
10
- import { normalizeDirectoryRules, normalizeSyncInclude, syncDirectoryPolicyKey, syncSessionsSelected, } from "../sync/sync-policy.js";
12
+ import { localIgnorePolicyKey, normalizeDirectoryRules, normalizeSyncInclude, syncDirectoryPolicyKey, syncSessionsSelected, } from "../sync/sync-policy.js";
13
+ import { safeTerminalText } from "../ui/terminal-text.js";
11
14
  import { localConfigPath, readActiveLocalConfigDocumentForRepair, withLockedLocalConfigDocument, } from "./config-file.js";
12
- import { requireSettings } from "./settings-store.js";
15
+ import { readLocalConfigDocument, requireSettings } from "./settings-store.js";
13
16
  import { DEFAULT_ON_SWITCH, isCloudflareR2Endpoint, normalizeS3Bucket, normalizeS3Endpoint, normalizeStoragePath, optionalString, ownObject, requiredString, validateConfigName, validateSettingsDocument, } from "./settings-validation.js";
14
- export async function loadConfig(setupName) {
15
- return requireLegacyOperationConfig(configFromSettings(await requireSettings(), setupName));
17
+ export async function loadConfig(setupName, signal) {
18
+ signal?.throwIfAborted();
19
+ const document = await readLocalConfigDocument();
20
+ if (!document)
21
+ throw new Error(`Missing pi-sync settings. Open /sync and choose Set up sync, or create ${localConfigPath()}.`);
22
+ return operationalConfig(document, setupName, signal);
16
23
  }
17
24
  /** A background freshness read must never trigger legacy-file publication. */
18
- export async function loadConfigForCheck() {
19
- return requireLegacyOperationConfig(configFromSettings(await readSettingsForCheck()));
25
+ export async function loadConfigForCheck(setupName, signal) {
26
+ signal?.throwIfAborted();
27
+ return operationalConfig(await readActiveLocalConfigDocumentForRepair(), setupName, signal);
28
+ }
29
+ async function operationalConfig(document, setupName, signal) {
30
+ signal?.throwIfAborted();
31
+ const config = configFromSettings(settingsFromCheckDocument(document), setupName);
32
+ if (!config.reservedIgnoreNames)
33
+ return config;
34
+ if (engineHost().ignoreFileOperationsAvailable === false)
35
+ return requireLegacyOperationConfig(config);
36
+ return resolveOperationPolicy(config, setupName, signal, document);
20
37
  }
21
38
  export async function loadSettingsConfig(setupName) {
22
39
  return configFromSettings(await readSettingsForCheck(), setupName);
@@ -37,46 +54,62 @@ export function requireLegacyOperationConfig(config) {
37
54
  }
38
55
  export async function resolveIgnoreConfig(settings, setupName, signal) {
39
56
  signal?.throwIfAborted();
40
- const config = configFromSettings(validateSettingsDocument(settings), setupName);
41
- if (config.pathPolicy !== "ignore-file")
42
- return requireLegacyOperationConfig(config);
57
+ return resolveOperationPolicy(configFromSettings(validateSettingsDocument(settings), setupName), setupName, signal);
58
+ }
59
+ function settingsDocumentFingerprint(document) {
60
+ return documentFingerprint(document && { bytes: document.bytes.toString("base64"), identity: document.identity });
61
+ }
62
+ async function resolveOperationPolicy(config, setupName, signal, original) {
63
+ if (!config.reservedIgnoreNames)
64
+ return config;
43
65
  const root = getEngineRoot();
44
66
  const validateRoot = () => {
67
+ signal?.throwIfAborted();
45
68
  if (root !== getEngineRoot())
46
69
  throw new Error("The Pi agent directory changed; reopen the review before continuing.");
47
70
  };
71
+ const configDocumentFingerprint = original && settingsDocumentFingerprint(original);
48
72
  const settingsFingerprint = syncCheckConfigFingerprint(config);
49
73
  const validateSettings = (document) => {
50
- signal?.throwIfAborted();
51
74
  validateRoot();
52
- const current = configFromSettings(document, setupName);
53
- if (settingsFingerprint !== syncCheckConfigFingerprint(current)) {
75
+ if ((original &&
76
+ (document?.path !== original.path || settingsDocumentFingerprint(document) !== configDocumentFingerprint)) ||
77
+ settingsFingerprint !==
78
+ syncCheckConfigFingerprint(configFromSettings(settingsFromCheckDocument(document), setupName))) {
54
79
  throw new Error("Pi Sync settings changed; reopen the review before continuing.");
55
80
  }
56
- return current;
57
81
  };
58
- const ignorePolicy = await loadEffectiveIgnorePolicy(root, ignorePolicyInput(config), signal);
59
- signal?.throwIfAborted();
82
+ const source = config.pathPolicy === "ignore-file" ? await readIgnoreSource(root, config.ignoreFile, signal) : undefined;
60
83
  validateRoot();
84
+ if (original && source && !source.present) {
85
+ throw new Error(`Ignore file is missing: ${safeTerminalText(path.join(root, source.filename))}. Create it and reopen Pi Sync; starter rules are only a preview.`);
86
+ }
87
+ if (original && source)
88
+ validateSettings(await readActiveLocalConfigDocumentForRepair());
61
89
  return {
62
90
  ...config,
63
- ignorePolicy,
91
+ ...(configDocumentFingerprint ? { configDocumentFingerprint } : {}),
92
+ ...(source ? { ignorePolicy: compileEffectiveIgnorePolicy(source, ignorePolicyInput(config)) } : {}),
93
+ ...(source?.identity ? { ignoreSourceIdentity: source.identity } : {}),
64
94
  validateIgnorePolicy: async () => {
65
- signal?.throwIfAborted();
66
95
  validateRoot();
67
96
  await withLockedLocalConfigDocument(async (document, recheck) => {
68
- const current = validateSettings(settingsFromCheckDocument(document));
69
- const source = await readIgnoreSource(root, current.ignoreFile, signal);
70
- validateRoot();
71
- recheck();
72
- if (source.sourceDigest !== ignorePolicy.sourceDigest) {
73
- throw new Error("Pi Sync ignore policy changed; reopen the review before continuing.");
97
+ validateSettings(document);
98
+ if (source) {
99
+ const current = await readIgnoreSource(root, source.filename, signal);
100
+ validateRoot();
101
+ recheck();
102
+ if (current.sourceDigest !== source.sourceDigest || current.identity !== source.identity) {
103
+ throw new Error("Pi Sync ignore policy changed; reopen the review before continuing.");
104
+ }
105
+ if (!current.present) {
106
+ throw new Error(`Ignore file "${source.filename}" is missing; starter rules can only be used for a preview.`);
107
+ }
74
108
  }
75
- if (!source.present) {
76
- throw new Error(`Ignore file "${source.filename}" is missing; starter rules can only be used for a preview.`);
109
+ else {
110
+ recheck();
77
111
  }
78
112
  }, signal, true);
79
- signal?.throwIfAborted();
80
113
  validateRoot();
81
114
  },
82
115
  };
@@ -140,6 +173,7 @@ export function syncSetupReviewIdentity(setupName, setup, connectionName, connec
140
173
  return syncConfigReviewIdentity(resolveSyncConfig(setupName, setup, connectionName, connection, DEFAULT_ON_SWITCH, false, true));
141
174
  }
142
175
  export function syncConfigReviewIdentity(config) {
176
+ const localPolicy = localIgnorePolicyKey(config);
143
177
  const directoryPolicy = config.pathPolicy === "ignore-file" && !config.ignorePolicy
144
178
  ? JSON.stringify([config.pathPolicy, config.ignoreFile])
145
179
  : syncDirectoryPolicyKey(config);
@@ -162,6 +196,7 @@ export function syncConfigReviewIdentity(config) {
162
196
  : config.exclude === undefined
163
197
  ? []
164
198
  : ["exclude", normalizeSyncExclude(config.exclude).map((rule) => rule.toLowerCase())]),
199
+ ...(localPolicy ? [localPolicy] : []),
165
200
  ]);
166
201
  }
167
202
  export function syncConfigReviewFingerprint(config) {
@@ -173,6 +208,14 @@ export function syncCheckConfigFingerprint(config) {
173
208
  .update(JSON.stringify({ ...config, showStatus: undefined, debug: undefined }))
174
209
  .digest("hex");
175
210
  }
211
+ export function syncSettingsConfigFingerprint(config) {
212
+ return syncCheckConfigFingerprint({
213
+ ...config,
214
+ configDocumentFingerprint: undefined,
215
+ ignoreSourceIdentity: undefined,
216
+ ignorePolicy: undefined,
217
+ });
218
+ }
176
219
  function storageReviewFromConfig(config) {
177
220
  return {
178
221
  connectionName: config.connectionName,
@@ -20,5 +20,7 @@ export declare function updateSyncSetup(name: string, update: (setup: SyncSetupS
20
20
  upgradeExclude?: boolean;
21
21
  signal?: AbortSignal;
22
22
  }): Promise<void>;
23
- export declare function removeSyncSetup(name: string, signal?: AbortSignal): Promise<void>;
23
+ export declare function removeSyncSetup(name: string, signal?: AbortSignal): Promise<{
24
+ revertedToLegacyOnly: boolean;
25
+ }>;
24
26
  export declare function removeStorageConnection(name: string, signal?: AbortSignal): Promise<void>;
@@ -122,8 +122,10 @@ export async function updateSyncSetup(name, update, options = {}) {
122
122
  }
123
123
  export async function removeSyncSetup(name, signal) {
124
124
  validateConfigName(name, "sync setup");
125
- await updateLocalConfig((settings) => {
126
- if (!Object.hasOwn(settings.syncSetups, name))
125
+ let removedLastIgnoreSetup = false;
126
+ const saved = await updateLocalConfig((settings) => {
127
+ const setup = settings.syncSetups[name];
128
+ if (!Object.hasOwn(settings.syncSetups, name) || !setup)
127
129
  throw new Error(`Sync setup not found: ${name}`);
128
130
  const isCurrent = settings.activeSyncSetup === name;
129
131
  if (isCurrent && Object.keys(settings.syncSetups).length > 1) {
@@ -131,11 +133,15 @@ export async function removeSyncSetup(name, signal) {
131
133
  }
132
134
  const syncSetups = { ...settings.syncSetups };
133
135
  delete syncSetups[name];
136
+ removedLastIgnoreSetup =
137
+ setup.sync.pathPolicy === "ignore-file" &&
138
+ !Object.values(syncSetups).some((remaining) => remaining.sync.pathPolicy === "ignore-file");
134
139
  const next = { ...settings, syncSetups };
135
140
  if (isCurrent)
136
141
  delete next.activeSyncSetup;
137
142
  return next;
138
143
  }, signal);
144
+ return { revertedToLegacyOnly: removedLastIgnoreSetup && saved.activeSyncSetup !== undefined };
139
145
  }
140
146
  export async function removeStorageConnection(name, signal) {
141
147
  validateConfigName(name, "storage connection");
@@ -128,6 +128,8 @@ export interface SyncConfig<Backend extends ResolvedSyncBackend = ResolvedS3Back
128
128
  reservedIgnoreNames?: readonly string[];
129
129
  ignorePolicy?: EffectiveIgnorePolicy;
130
130
  validateIgnorePolicy?: SnapshotOptions["validateIgnorePolicy"];
131
+ ignoreSourceIdentity?: string;
132
+ configDocumentFingerprint?: string;
131
133
  exclude?: string[];
132
134
  reinclude?: string[];
133
135
  automatic: boolean;
@@ -1,6 +1,7 @@
1
1
  import fs from "node:fs/promises";
2
2
  import os from "node:os";
3
3
  import path from "node:path";
4
+ import { backendIdentityCoordinates } from "../backends/backend-identity.js";
4
5
  import { getEngineRoot as getAgentDir, } from "../host.js";
5
6
  import { configuredSnapshotPathMatcher } from "../sync/sync-policy.js";
6
7
  import { agentDirectorySessionRoot, reservedIgnoreTargetGuard } from "./snapshot-exclude-safety.js";
@@ -142,9 +143,13 @@ export function snapshotOptionsForContext(ctx, config) {
142
143
  return {
143
144
  include: config.include,
144
145
  ...(config.scope === undefined ? {} : { scope: config.scope }),
145
- ...(config.ignorePolicy
146
- ? { ignorePolicy: config.ignorePolicy, validateIgnorePolicy: config.validateIgnorePolicy }
146
+ ...(config.setupName === undefined ? {} : { setupName: config.setupName }),
147
+ ...(config.backend && (config.ignorePolicy || config.reservedIgnoreNames !== undefined)
148
+ ? { recoverySetupKey: JSON.stringify([backendIdentityCoordinates(config), config.localFields]) }
147
149
  : {}),
150
+ ...(config.ignorePolicy ? { ignorePolicy: config.ignorePolicy } : {}),
151
+ ...(config.ignoreSourceIdentity === undefined ? {} : { ignoreSourceIdentity: config.ignoreSourceIdentity }),
152
+ ...(config.validateIgnorePolicy === undefined ? {} : { validateIgnorePolicy: config.validateIgnorePolicy }),
148
153
  ...(config.exclude === undefined ? {} : { exclude: config.exclude }),
149
154
  ...(config.reinclude === undefined ? {} : { reinclude: config.reinclude }),
150
155
  ...(config.reservedIgnoreNames === undefined ? {} : { reservedIgnoreNames: config.reservedIgnoreNames }),
@@ -1,5 +1,5 @@
1
1
  import { type Snapshot, type SnapshotApplyPlan, type SnapshotOptions } from "./snapshot-types.js";
2
- export declare function applySnapshot(snapshot: Snapshot, protectedRelativePaths?: Set<string>, options?: Pick<SnapshotOptions, "include" | "exclude" | "scope" | "reinclude" | "ignorePolicy" | "reservedIgnoreNames" | "sessionDir" | "syncFiles" | "syncSessions" | "extraFiles" | "signal" | "validateMutation" | "validateIgnorePolicy" | "expectedFileHashes"> & {
2
+ export declare function applySnapshot(snapshot: Snapshot, protectedRelativePaths?: Set<string>, options?: Pick<SnapshotOptions, "include" | "exclude" | "scope" | "reinclude" | "ignorePolicy" | "reservedIgnoreNames" | "setupName" | "recoverySetupKey" | "ignoreSourceIdentity" | "sessionDir" | "syncFiles" | "syncSessions" | "extraFiles" | "signal" | "validateMutation" | "validateIgnorePolicy" | "validateApplyReview" | "expectedFileHashes"> & {
3
3
  reviewedLocal?: Pick<Snapshot, "files">;
4
4
  }): Promise<{
5
5
  [k: string]: string;
@@ -5,6 +5,10 @@ export declare const LEGACY_AGENT_DIRECTORY_JOURNAL_VERSION = 8;
5
5
  export declare const FIXED_WORKSPACE_JOURNAL_VERSION = 9;
6
6
  export declare const AGENT_DIRECTORY_JOURNAL_VERSION = 10;
7
7
  export declare const IGNORE_POLICY_JOURNAL_VERSION = 11;
8
+ export declare const BOUND_DIRECTORY_JOURNAL_VERSION = 12;
9
+ export declare const BOUND_INCLUDED_PATHS_JOURNAL_VERSION = 13;
10
+ type RecoveryPolicyBinding = Pick<SnapshotOptions, "setupName" | "ignoreSourceIdentity" | "recoverySetupKey">;
11
+ export declare function recoveryPolicyKey(options: SyncSelectionConfig & RecoveryPolicyBinding): string | undefined;
8
12
  export declare function isScopedJournal(journal: TransactionJournalPolicy): boolean;
9
13
  export declare function isRecoveryStageId(value: unknown): value is string;
10
14
  export declare function isScopedIdentity(value: unknown): value is string;
@@ -19,10 +23,11 @@ export interface TransactionJournalPolicy {
19
23
  exclude?: string[];
20
24
  selection?: SnapshotSelection;
21
25
  ignoreSource?: IgnoreSourceBinding;
26
+ localPolicyKey?: string;
22
27
  }
23
28
  export declare function ignoreSourceBinding(policy: Pick<EffectiveIgnorePolicy, "filename" | "sourceDigest" | "reservedNames">): IgnoreSourceBinding;
24
29
  type TransactionGuards = Pick<SnapshotOptions, "validateIgnorePolicy" | "signal" | "validateMutation">;
25
- export interface TransactionOptions extends SyncSelectionConfig, TransactionGuards {
30
+ export interface TransactionOptions extends SyncSelectionConfig, TransactionGuards, RecoveryPolicyBinding, Pick<SnapshotOptions, "validateApplyReview"> {
26
31
  sessionDir?: string;
27
32
  exclude?: readonly string[];
28
33
  protectedTargets?: readonly string[];
@@ -33,7 +38,7 @@ export declare class StaleIgnorePolicyError extends Error {
33
38
  }
34
39
  export declare function ignorePolicyValidator(options: SyncSelectionConfig & TransactionGuards): (() => Promise<void>) | undefined;
35
40
  export declare function validateJournalPolicy(journal: TransactionJournalPolicy): void;
36
- export declare function assertRecoveryPolicy(journal: TransactionJournalPolicy, options: SyncSelectionConfig): void;
41
+ export declare function assertRecoveryPolicy(journal: TransactionJournalPolicy, options: TransactionOptions): void;
37
42
  export interface TransactionEntry {
38
43
  target: string;
39
44
  afterTarget?: string;