@narumitw/pi-sync-daemon 0.7.0 → 0.8.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 (74) hide show
  1. package/CHANGELOG.md +11 -0
  2. package/README.md +16 -2
  3. package/dist/consent.js +21 -6
  4. package/dist/engine/backends/git/git-backend.d.ts +1 -1
  5. package/dist/engine/backends/git/git-backend.js +17 -2
  6. package/dist/engine/backends/git/git-storage.d.ts +1 -1
  7. package/dist/engine/backends/git/git-storage.js +30 -9
  8. package/dist/engine/backends/s3/s3-backend.js +8 -0
  9. package/dist/engine/backends/sync-backend.d.ts +1 -0
  10. package/dist/engine/backends/webdav/webdav-backend.js +8 -0
  11. package/dist/engine/ignore/effective-ignore-policy.d.ts +10 -0
  12. package/dist/engine/ignore/effective-ignore-policy.js +33 -0
  13. package/dist/engine/ignore/ignore-edit.d.ts +13 -0
  14. package/dist/engine/ignore/ignore-edit.js +102 -0
  15. package/dist/engine/ignore/ignore-initialization.d.ts +6 -0
  16. package/dist/engine/ignore/ignore-initialization.js +186 -0
  17. package/dist/engine/ignore/ignore-migration-prompt.d.ts +7 -0
  18. package/dist/engine/ignore/ignore-migration-prompt.js +139 -0
  19. package/dist/engine/ignore/ignore-settings.d.ts +15 -0
  20. package/dist/engine/ignore/ignore-settings.js +322 -0
  21. package/dist/engine/ignore/ignore-source.d.ts +27 -0
  22. package/dist/engine/ignore/ignore-source.js +250 -0
  23. package/dist/engine/settings/config-file.d.ts +9 -2
  24. package/dist/engine/settings/config-file.js +124 -28
  25. package/dist/engine/settings/config.d.ts +3 -0
  26. package/dist/engine/settings/config.js +93 -7
  27. package/dist/engine/settings/settings-management.d.ts +2 -2
  28. package/dist/engine/settings/settings-management.js +16 -8
  29. package/dist/engine/settings/settings-types.d.ts +23 -2
  30. package/dist/engine/settings/settings-validation.js +23 -5
  31. package/dist/engine/snapshot/session-paths.d.ts +1 -1
  32. package/dist/engine/snapshot/session-paths.js +36 -13
  33. package/dist/engine/snapshot/snapshot-apply.d.ts +3 -3
  34. package/dist/engine/snapshot/snapshot-apply.js +23 -4
  35. package/dist/engine/snapshot/snapshot-codec.d.ts +1 -1
  36. package/dist/engine/snapshot/snapshot-codec.js +14 -5
  37. package/dist/engine/snapshot/snapshot-exclude-safety.d.ts +4 -2
  38. package/dist/engine/snapshot/snapshot-exclude-safety.js +76 -19
  39. package/dist/engine/snapshot/snapshot-transaction-images.d.ts +3 -1
  40. package/dist/engine/snapshot/snapshot-transaction-images.js +33 -3
  41. package/dist/engine/snapshot/snapshot-transaction-policy.d.ts +24 -1
  42. package/dist/engine/snapshot/snapshot-transaction-policy.js +106 -2
  43. package/dist/engine/snapshot/snapshot-transaction-verification.d.ts +7 -0
  44. package/dist/engine/snapshot/snapshot-transaction-verification.js +78 -0
  45. package/dist/engine/snapshot/snapshot-transaction.d.ts +1 -12
  46. package/dist/engine/snapshot/snapshot-transaction.js +86 -108
  47. package/dist/engine/snapshot/snapshot-types.d.ts +23 -1
  48. package/dist/engine/snapshot/snapshot-types.js +9 -1
  49. package/dist/engine/snapshot/snapshot.d.ts +6 -4
  50. package/dist/engine/snapshot/snapshot.js +91 -34
  51. package/dist/engine/state/state-types.d.ts +1 -0
  52. package/dist/engine/sync/continuous-sync.js +18 -3
  53. package/dist/engine/sync/continuous-watch.js +4 -1
  54. package/dist/engine/sync/initialization.js +3 -2
  55. package/dist/engine/sync/local-fields.d.ts +3 -1
  56. package/dist/engine/sync/local-fields.js +62 -1
  57. package/dist/engine/sync/merge-apply.js +35 -7
  58. package/dist/engine/sync/merge-journal.d.ts +18 -4
  59. package/dist/engine/sync/merge-journal.js +115 -8
  60. package/dist/engine/sync/merged-sync.d.ts +1 -1
  61. package/dist/engine/sync/merged-sync.js +103 -50
  62. package/dist/engine/sync/remote-snapshot.d.ts +9 -2
  63. package/dist/engine/sync/remote-snapshot.js +56 -17
  64. package/dist/engine/sync/sync-inspection.js +7 -5
  65. package/dist/engine/sync/sync-mutations.d.ts +2 -1
  66. package/dist/engine/sync/sync-mutations.js +110 -47
  67. package/dist/engine/sync/sync-policy.d.ts +24 -8
  68. package/dist/engine/sync/sync-policy.js +127 -16
  69. package/dist/engine/sync/sync-queries.js +8 -1
  70. package/dist/engine/sync/sync-state.d.ts +2 -2
  71. package/dist/engine/sync/sync-state.js +20 -2
  72. package/dist/engine-service.js +24 -2
  73. package/docs/architecture.md +22 -0
  74. package/package.json +2 -1
package/CHANGELOG.md CHANGED
@@ -1,5 +1,16 @@
1
1
  # @narumitw/pi-sync-daemon
2
2
 
3
+ ## 0.8.0
4
+
5
+ ### Minor Changes
6
+
7
+ - d27317a: Prepare Pi Sync's local ignore-file policy, including recoverable default-file creation, settings review, and versioned snapshot compatibility checks. Existing setups are not migrated automatically. The new setup flow and first-open migration prompt remain disabled. Settings can show an existing rule file for manual editing; the daemon does not replace it. Settings shows the active settings path in RPC and explains which setup actions are unavailable while ignore-file transfers are disabled.
8
+
9
+ ### Patch Changes
10
+
11
+ - 59ea738: Fix snapshot recovery on macOS when restoring files with older modification times. Recovery still verifies the staging file's identity and contents before restoring the backup.
12
+ - 4073de5: Fix Windows recovery when NTFS changes a restored file's creation time during rename. Keep exact content, file ID, spelling, and pre-rename ownership checks, and preserve existing recovery journal formats. Make the daemon build use the Node compiler entrypoint instead of spawning a Windows command script.
13
+
3
14
  ## 0.7.0
4
15
 
5
16
  ### Minor Changes
package/README.md CHANGED
@@ -16,7 +16,19 @@
16
16
 
17
17
  ## 📦 Install
18
18
 
19
- The Pi client declares this service as a normal dependency; do not add it to `pi.extensions` or load it with `pi -e`. The npm package is `@narumitw/pi-sync-daemon`; the executable is `pi-sync-daemon`. For the previous package and executable names, see the [rename instructions](https://github.com/narumiruna/pi-extensions/blob/main/packages/pi-sync/docs/daemon-migration.md#ownership-and-upgrade).
19
+ Use Node.js 22.19.0 or later. The [Pi Sync npm package](https://www.npmjs.com/package/@narumitw/pi-sync) includes its own daemon build, so you do not need to install this standalone package to use Pi Sync. To install the standalone CLI:
20
+
21
+ ```sh
22
+ npm install --global @narumitw/pi-sync-daemon
23
+ ```
24
+
25
+ To try the standalone CLI without installing it globally:
26
+
27
+ ```sh
28
+ npm exec --package=@narumitw/pi-sync-daemon -- pi-sync-daemon --help
29
+ ```
30
+
31
+ This package is not an extension; do not add it to `pi.extensions` or load it with `pi -e`. See the [daemon CLI guide](https://github.com/narumiruna/pi-extensions/blob/main/packages/pi-sync/docs/daemon-migration.md#daemon-cli) for the previous executable name and for running the copy bundled with Pi Sync.
20
32
 
21
33
  For a local checkout, run from the repository root:
22
34
 
@@ -25,7 +37,7 @@ npm install
25
37
  npm --workspace @narumitw/pi-sync-daemon run build
26
38
  ```
27
39
 
28
- The build emits JavaScript, declarations, and an executable under `dist/`. A global npm installation is not required for the Pi client to locate its dependency. Registry installation remains blocked until publication rights and the dependency-first release are approved.
40
+ The build emits JavaScript, declarations, and the executable `dist/cli.js`.
29
41
 
30
42
  ## 🚀 Quick start
31
43
 
@@ -74,6 +86,8 @@ Close or upgrade every old Pi process before reviewed handover. An absent operat
74
86
 
75
87
  Automatic exit first stops new automatic work, cancels cancellable work, and drains writes, queue-release acknowledgements, persistence, and owned callbacks before releasing ownership. It adds no final sync or model request. Uncertain consent publication, failed revocation, or cleanup failure keeps the owner paused with evidence retained; automatic restart must not restore an older grant. Repair persistence and use the existing revocation/repair path before explicit restart. The next eligible interactive client or explicit command can restart through singleton startup only after the old owner's exit is proven; print/JSON never launch a replacement.
76
88
 
89
+ The standalone package is versioned and released independently. Updating it does not change the daemon bundled in an installed Pi Sync package. Update Pi Sync to get a newer bundled build.
90
+
77
91
  Stop the service and verify owner exit before an in-place rebuild or rollback. Preserve pending
78
92
  journals and backups. Rebuilding without changing the package version does not replace the running
79
93
  owner. See the [daemon upgrade guide](https://github.com/narumiruna/pi-extensions/blob/main/packages/pi-sync/docs/daemon-migration.md#ownership-and-upgrade) for package updates and manual recovery. Do not downgrade over pending/newer recovery evidence or restore old settings over newer bytes. Expired or old-instance operation IDs must reconcile existing journals rather than replay a possible commit. The bounded per-instance result ledger requires an explicit drained restart when full.
package/dist/consent.js CHANGED
@@ -196,6 +196,7 @@ export async function authorizeAfterExit(execution, payload) {
196
196
  await requireHandover(execution);
197
197
  const { loadConfig } = await import("./engine/settings/config.js");
198
198
  const config = await loadConfig();
199
+ execution.validate();
199
200
  const facts = execution.facts();
200
201
  const binding = JSON.stringify({ model: facts?.model, sessionDir: facts?.sessionDir, cwd: facts?.cwd });
201
202
  const fingerprint = consentFingerprint(execution.root, config);
@@ -205,14 +206,20 @@ export async function authorizeAfterExit(execution, payload) {
205
206
  if (payload.afterExit && (!config.continuous || !config.agentMode))
206
207
  throw new Error("Enable the reviewed Autonomous and Continuous policies first.");
207
208
  const paths = (rules) => rules?.map(safeTerminalText).join(", ") || "none";
208
- const selection = config.scope === "agent-directory"
209
+ const selection = config.ignorePolicy
209
210
  ? [
210
- "Selected content: all eligible files in the Pi agent directory",
211
- `Excluded paths: ${paths(config.exclude)}`,
212
- `Re-included paths: ${paths(config.reinclude)}`,
213
- `Session opt-in: ${config.include.includes("sessions") ? "On — path rules still apply" : "Off"}`,
211
+ "Selected content: eligible files in the Pi agent directory",
212
+ `Local ignore file: ${safeTerminalText(config.ignorePolicy.filename)}`,
213
+ `Session opt-in: ${config.ignorePolicy.sessions ? "On — ignore rules still apply" : "Off"}`,
214
214
  ].join("\n")
215
- : `Selected content: ${paths(config.include)}`;
215
+ : config.scope === "agent-directory"
216
+ ? [
217
+ "Selected content: all eligible files in the Pi agent directory",
218
+ `Excluded paths: ${paths(config.exclude)}`,
219
+ `Re-included paths: ${paths(config.reinclude)}`,
220
+ `Session opt-in: ${config.include.includes("sessions") ? "On — path rules still apply" : "Off"}`,
221
+ ].join("\n")
222
+ : `Selected content: ${paths(config.include)}`;
216
223
  const accepted = await execution.request("confirm", {
217
224
  title: payload.afterExit ? "Allow continuous sync after all Pi clients exit?" : "Revoke unattended sync?",
218
225
  message: `Setup: ${safeTerminalText(config.setupName)}\n${selection}\nModel: ${safeTerminalText(facts?.model?.provider ?? "unavailable")}/${safeTerminalText(facts?.model?.id ?? "unavailable")}\n\n${payload.afterExit ? "The daemon may transfer selected private content and send conflict evidence to this independently available provider after Pi exits. Existing budgets, exclusions, journals, and stale-evidence checks still apply. No parent conversation or tools are sent. Changing this setup or model invalidates this permission." : "The daemon may remain alive, but no new transfer or model request may start with zero clients."}`,
@@ -222,6 +229,14 @@ export async function authorizeAfterExit(execution, payload) {
222
229
  return false;
223
230
  const latest = await loadConfig();
224
231
  execution.validate();
232
+ if (config.ignorePolicy) {
233
+ const { ignorePolicyValidator } = await import("./engine/snapshot/snapshot-transaction-policy.js");
234
+ await ignorePolicyValidator({
235
+ ...config,
236
+ signal: execution.signal,
237
+ validateMutation: () => execution.validate(),
238
+ })?.();
239
+ }
225
240
  if (consentFingerprint(execution.root, latest) !== fingerprint ||
226
241
  JSON.stringify({
227
242
  model: execution.facts()?.model,
@@ -1,4 +1,4 @@
1
- import type { Snapshot } from "../../snapshot/snapshot-types.js";
1
+ import { type Snapshot } from "../../snapshot/snapshot-types.js";
2
2
  import type { ResolvedGitBackend } from "../backend-types.js";
3
3
  import { type BackendDiagnostic, type ExpectedRemoteHead, type PublishSnapshotOptions, type PublishSnapshotResult, type RemoteHead, type RemoteHistoryEntry, type SyncBackend } from "../sync-backend.js";
4
4
  export interface GitBackendOptions {
@@ -1,7 +1,9 @@
1
+ import { isUtf8 } from "node:buffer";
1
2
  import { createHash, randomUUID } from "node:crypto";
2
3
  import fs from "node:fs/promises";
3
4
  import path from "node:path";
4
5
  import { posixJoin } from "../../paths.js";
6
+ import { IGNORE_SNAPSHOT_VERSION } from "../../snapshot/snapshot-types.js";
5
7
  import { stateDir } from "../../state/state-directory.js";
6
8
  import { throwIfAborted } from "../../sync/signals.js";
7
9
  import { syncErrorGuidance } from "../../sync/sync-error-guidance.js";
@@ -136,6 +138,10 @@ export class GitSyncBackend {
136
138
  throw this.redactedError(error);
137
139
  }
138
140
  throwIfAborted(options.signal);
141
+ if (options.validateIgnorePolicy) {
142
+ await options.validateIgnorePolicy();
143
+ throwIfAborted(options.signal);
144
+ }
139
145
  options.onCommit?.();
140
146
  const ref = this.remoteRef();
141
147
  const lease = `--force-with-lease=${ref}:${observed ?? ""}`;
@@ -318,6 +324,13 @@ export class GitSyncBackend {
318
324
  catch (error) {
319
325
  throw new Error("Git publication manifest is malformed.", { cause: error });
320
326
  }
327
+ if (parsed &&
328
+ typeof parsed === "object" &&
329
+ "snapshotVersion" in parsed &&
330
+ parsed.snapshotVersion === IGNORE_SNAPSHOT_VERSION &&
331
+ !isUtf8(bytes)) {
332
+ throw new Error("Git publication manifest is not valid UTF-8.");
333
+ }
321
334
  return requireGitManifest(parsed);
322
335
  }
323
336
  showFile(commit, filePath, signal, maxOutputBytes) {
@@ -503,15 +516,17 @@ export class GitSyncBackend {
503
516
  }
504
517
  async readPublication(commit, signal) {
505
518
  const manifest = await this.readManifest(commit, signal);
506
- const entries = await this.readPublicationTree(commit, signal);
519
+ const entries = await this.readPublicationTree(commit, signal, manifest.snapshotVersion === IGNORE_SNAPSHOT_VERSION);
507
520
  const payloadEntries = validateGitPublicationTree(entries, manifest, this.manifestPath(), (filePath) => this.filePath(filePath));
508
521
  return { manifest, payloadEntries };
509
522
  }
510
- async readPublicationTree(commit, signal) {
523
+ async readPublicationTree(commit, signal, strictUtf8 = false) {
511
524
  const result = await this.git(["ls-tree", "-r", "-z", commit], {
512
525
  signal,
513
526
  maxOutputBytes: MAX_GIT_TREE_OUTPUT_BYTES,
514
527
  });
528
+ if (strictUtf8 && !isUtf8(result.stdout))
529
+ throw new Error("Git snapshot tree paths must be valid UTF-8.");
515
530
  return parseGitTree(result.stdout);
516
531
  }
517
532
  redactedError(error) {
@@ -1,4 +1,4 @@
1
- import type { Snapshot, SnapshotSelection } from "../../snapshot/snapshot-types.js";
1
+ import { type Snapshot, type SnapshotSelection } from "../../snapshot/snapshot-types.js";
2
2
  export declare const GIT_MANIFEST_VERSION = 2;
3
3
  export declare const MAX_GIT_MANIFEST_BYTES: number;
4
4
  export declare const MAX_GIT_TREE_OUTPUT_BYTES: number;
@@ -1,5 +1,7 @@
1
1
  import { createHash } from "node:crypto";
2
- import { sameLocalFields, validatePortableSnapshot, validateSnapshotFieldPolicy } from "../../sync/local-fields.js";
2
+ import { assertAgentDirectorySettingsSize, MAX_AGENT_DIRECTORY_BYTES } from "../../snapshot/snapshot-bounds.js";
3
+ import { IGNORE_SNAPSHOT_VERSION, isSupportedSnapshotVersion, } from "../../snapshot/snapshot-types.js";
4
+ import { sameLocalFields, validateIgnoreSnapshotIdentity, validateIgnoreSnapshotSessionFlag, validatePortableSnapshot, validateSnapshotFieldPolicy, } from "../../sync/local-fields.js";
3
5
  import { assertAgentDirectoryPathLayout, configuredSnapshotPathMatcher, portableSnapshotSelection, sameSnapshotSelection, snapshotSelectionInclude, validateSnapshotSelection, } from "../../sync/sync-policy.js";
4
6
  export const GIT_MANIFEST_VERSION = 2;
5
7
  export const MAX_GIT_MANIFEST_BYTES = 1024 * 1024;
@@ -18,11 +20,7 @@ export function requireGitManifest(value) {
18
20
  throw new Error("Git publication uses the unsupported pre-release gzip format; recreate this pi-sync-owned test branch.");
19
21
  }
20
22
  if (manifest.version !== GIT_MANIFEST_VERSION ||
21
- (manifest.snapshotVersion !== 1 &&
22
- manifest.snapshotVersion !== 2 &&
23
- manifest.snapshotVersion !== 3 &&
24
- manifest.snapshotVersion !== 4 &&
25
- manifest.snapshotVersion !== 5) ||
23
+ !isSupportedSnapshotVersion(manifest.snapshotVersion) ||
26
24
  typeof manifest.snapshotId !== "string" ||
27
25
  manifest.snapshotId.length > 512 ||
28
26
  !/^[A-Za-z0-9._-]+$/u.test(manifest.snapshotId) ||
@@ -58,8 +56,20 @@ export function requireGitManifest(value) {
58
56
  validateSnapshotFieldPolicy({ version: manifest.snapshotVersion, localFields: manifest.localFields });
59
57
  const selection = manifest.selection === undefined ? undefined : portableSnapshotSelection(manifest.selection);
60
58
  validateSnapshotSelection({ version: manifest.snapshotVersion, selection });
59
+ const ignoreSnapshot = selection?.version === 4;
60
+ if (ignoreSnapshot) {
61
+ validateIgnoreSnapshotIdentity({
62
+ id: manifest.snapshotId,
63
+ createdAt: manifest.createdAt,
64
+ machine: manifest.machine,
65
+ profile: manifest.profile,
66
+ });
67
+ validateIgnoreSnapshotSessionFlag(selection, manifest.snapshotSyncSessions);
68
+ }
61
69
  const selected = selection?.scope ? configuredSnapshotPathMatcher(selection) : undefined;
70
+ const byteLimit = ignoreSnapshot ? MAX_AGENT_DIRECTORY_BYTES : MAX_GIT_SNAPSHOT_BYTES;
62
71
  let total = 0;
72
+ let hasSessionFiles = false;
63
73
  const paths = new Set();
64
74
  for (const rawFile of manifest.files) {
65
75
  if (!rawFile || typeof rawFile !== "object" || Array.isArray(rawFile)) {
@@ -70,6 +80,7 @@ export function requireGitManifest(value) {
70
80
  !isSafeSnapshotPath(file.path) ||
71
81
  (selected !== undefined && !selected(file.path)) ||
72
82
  typeof file.sha256 !== "string" ||
83
+ (ignoreSnapshot && file.sha256.length !== 64) ||
73
84
  !/^[0-9a-f]{64}$/u.test(file.sha256) ||
74
85
  typeof file.size !== "number" ||
75
86
  !isGitPayloadSizeAllowed(file.size) ||
@@ -77,11 +88,20 @@ export function requireGitManifest(value) {
77
88
  throw new Error("Git publication manifest file is malformed.");
78
89
  }
79
90
  total += file.size;
80
- if (!Number.isSafeInteger(total) || total > MAX_GIT_SNAPSHOT_BYTES) {
81
- throw new Error(`Git snapshot content exceeds the ${MAX_GIT_SNAPSHOT_BYTES}-byte limit.`);
91
+ if (!Number.isSafeInteger(total) || total > byteLimit) {
92
+ throw new Error(`Git snapshot content exceeds the ${byteLimit}-byte limit.`);
93
+ }
94
+ if (ignoreSnapshot) {
95
+ if (file.path === "settings.json")
96
+ assertAgentDirectorySettingsSize(file.size);
97
+ if (file.path.startsWith("sessions/"))
98
+ hasSessionFiles = true;
82
99
  }
83
100
  paths.add(file.path);
84
101
  }
102
+ if (ignoreSnapshot && manifest.syncSessions !== (manifest.snapshotSyncSessions === true || hasSessionFiles)) {
103
+ throw new Error("Invalid Git ignore snapshot session metadata.");
104
+ }
85
105
  if (selected)
86
106
  assertAgentDirectoryPathLayout(paths);
87
107
  else
@@ -109,7 +129,8 @@ export function validateGitSnapshot(snapshot, manifest, namespace) {
109
129
  }
110
130
  }
111
131
  export function prepareGitSnapshot(snapshot, namespace) {
112
- snapshotSelectionInclude(snapshot);
132
+ if (snapshot.version !== IGNORE_SNAPSHOT_VERSION)
133
+ snapshotSelectionInclude(snapshot);
113
134
  validatePortableSnapshot(snapshot);
114
135
  if (typeof snapshot.id !== "string" ||
115
136
  !snapshot.id ||
@@ -75,6 +75,9 @@ export class S3SyncBackend {
75
75
  async publishSnapshot(snapshot, expected, options = {}) {
76
76
  throwIfAborted(options.signal);
77
77
  assertSnapshotIdentity(snapshot, this.config.destination.namespace);
78
+ if (options.validateIgnorePolicy)
79
+ await options.validateIgnorePolicy();
80
+ throwIfAborted(options.signal);
78
81
  await verifyS3Conditions(this.config, options.signal);
79
82
  throwIfAborted(options.signal);
80
83
  const stagedKey = snapshotKey(this.config, snapshot.id);
@@ -82,6 +85,9 @@ export class S3SyncBackend {
82
85
  throwIfAborted(options.signal);
83
86
  const pointer = pointerFor(this.config, snapshot, sha256(encoded));
84
87
  const cancellableClient = new S3Client(this.config, options.signal);
88
+ if (options.validateIgnorePolicy)
89
+ await options.validateIgnorePolicy();
90
+ throwIfAborted(options.signal);
85
91
  try {
86
92
  await cancellableClient.putBuffer(stagedKey, encoded, "application/gzip", {
87
93
  ifAbsent: true,
@@ -109,6 +115,8 @@ export class S3SyncBackend {
109
115
  });
110
116
  }
111
117
  const condition = currentObject.missing ? { ifAbsent: true } : { ifMatch: requireS3Etag(currentObject.etag) };
118
+ if (options.validateIgnorePolicy)
119
+ await options.validateIgnorePolicy();
112
120
  throwIfAborted(options.signal);
113
121
  options.onCommit?.();
114
122
  // The active-head PUT is the publication boundary. Do not bind it or its
@@ -27,6 +27,7 @@ export type ExpectedRemoteHead = {
27
27
  };
28
28
  export interface PublishSnapshotOptions {
29
29
  signal?: AbortSignal;
30
+ validateIgnorePolicy?: () => Promise<void>;
30
31
  /** Called at the backend's active-head commit boundary. */
31
32
  onCommit?: () => void;
32
33
  }
@@ -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);
@@ -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,6 @@
1
+ import { type IgnoreSource } from "./ignore-source.js";
2
+ export declare function initializeDefaultIgnoreSource(root: string, signal?: AbortSignal): Promise<IgnoreSource>;
3
+ export declare function prepareDefaultIgnoreSource(root: string, signal?: AbortSignal): Promise<{
4
+ source: IgnoreSource;
5
+ created: boolean;
6
+ }>;