@narumitw/pi-sync-daemon 0.6.2 → 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 (97) hide show
  1. package/CHANGELOG.md +19 -0
  2. package/README.md +17 -3
  3. package/dist/consent.js +26 -1
  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 +39 -13
  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/paths.js +1 -1
  24. package/dist/engine/settings/config-file.d.ts +9 -2
  25. package/dist/engine/settings/config-file.js +126 -28
  26. package/dist/engine/settings/config.d.ts +5 -4
  27. package/dist/engine/settings/config.js +114 -13
  28. package/dist/engine/settings/settings-management.js +34 -32
  29. package/dist/engine/settings/settings-types.d.ts +40 -5
  30. package/dist/engine/settings/settings-validation.js +52 -24
  31. package/dist/engine/snapshot/session-paths.d.ts +6 -4
  32. package/dist/engine/snapshot/session-paths.js +84 -15
  33. package/dist/engine/snapshot/snapshot-apply.d.ts +7 -8
  34. package/dist/engine/snapshot/snapshot-apply.js +107 -21
  35. package/dist/engine/snapshot/snapshot-bounds.d.ts +4 -0
  36. package/dist/engine/snapshot/snapshot-bounds.js +20 -0
  37. package/dist/engine/snapshot/snapshot-codec.d.ts +1 -1
  38. package/dist/engine/snapshot/snapshot-codec.js +15 -7
  39. package/dist/engine/snapshot/snapshot-entry-projection.d.ts +10 -0
  40. package/dist/engine/snapshot/snapshot-entry-projection.js +78 -0
  41. package/dist/engine/snapshot/snapshot-exclude-safety.d.ts +9 -2
  42. package/dist/engine/snapshot/snapshot-exclude-safety.js +239 -24
  43. package/dist/engine/snapshot/snapshot-payload.d.ts +2 -0
  44. package/dist/engine/snapshot/snapshot-payload.js +10 -0
  45. package/dist/engine/snapshot/snapshot-reviewed-preimages.d.ts +2 -0
  46. package/dist/engine/snapshot/snapshot-reviewed-preimages.js +60 -0
  47. package/dist/engine/snapshot/snapshot-root-transition.d.ts +4 -0
  48. package/dist/engine/snapshot/snapshot-root-transition.js +16 -0
  49. package/dist/engine/snapshot/snapshot-transaction-images.d.ts +39 -0
  50. package/dist/engine/snapshot/snapshot-transaction-images.js +387 -0
  51. package/dist/engine/snapshot/snapshot-transaction-policy.d.ts +73 -0
  52. package/dist/engine/snapshot/snapshot-transaction-policy.js +276 -0
  53. package/dist/engine/snapshot/snapshot-transaction-stage.d.ts +32 -0
  54. package/dist/engine/snapshot/snapshot-transaction-stage.js +303 -0
  55. package/dist/engine/snapshot/snapshot-transaction-verification.d.ts +7 -0
  56. package/dist/engine/snapshot/snapshot-transaction-verification.js +78 -0
  57. package/dist/engine/snapshot/snapshot-transaction.d.ts +1 -11
  58. package/dist/engine/snapshot/snapshot-transaction.js +390 -364
  59. package/dist/engine/snapshot/snapshot-types.d.ts +47 -2
  60. package/dist/engine/snapshot/snapshot-types.js +9 -1
  61. package/dist/engine/snapshot/snapshot.d.ts +22 -3
  62. package/dist/engine/snapshot/snapshot.js +357 -43
  63. package/dist/engine/state/merge-baseline-store.js +20 -9
  64. package/dist/engine/state/state-types.d.ts +3 -0
  65. package/dist/engine/sync/apply-review.d.ts +3 -3
  66. package/dist/engine/sync/apply-review.js +5 -4
  67. package/dist/engine/sync/continuous-sync.js +37 -12
  68. package/dist/engine/sync/continuous-watch.d.ts +4 -1
  69. package/dist/engine/sync/continuous-watch.js +90 -15
  70. package/dist/engine/sync/exclude-policy.d.ts +4 -0
  71. package/dist/engine/sync/exclude-policy.js +8 -1
  72. package/dist/engine/sync/initialization.js +9 -4
  73. package/dist/engine/sync/local-fields.d.ts +3 -1
  74. package/dist/engine/sync/local-fields.js +88 -8
  75. package/dist/engine/sync/merge-apply.js +190 -47
  76. package/dist/engine/sync/merge-journal.d.ts +18 -4
  77. package/dist/engine/sync/merge-journal.js +126 -10
  78. package/dist/engine/sync/merged-sync.d.ts +1 -1
  79. package/dist/engine/sync/merged-sync.js +124 -68
  80. package/dist/engine/sync/partial-progress.js +5 -3
  81. package/dist/engine/sync/remote-snapshot.d.ts +10 -2
  82. package/dist/engine/sync/remote-snapshot.js +59 -17
  83. package/dist/engine/sync/session-push.js +7 -6
  84. package/dist/engine/sync/sync-decision.d.ts +4 -0
  85. package/dist/engine/sync/sync-decision.js +17 -2
  86. package/dist/engine/sync/sync-inspection.js +8 -6
  87. package/dist/engine/sync/sync-mutations.d.ts +10 -2
  88. package/dist/engine/sync/sync-mutations.js +245 -101
  89. package/dist/engine/sync/sync-policy.d.ts +36 -10
  90. package/dist/engine/sync/sync-policy.js +285 -20
  91. package/dist/engine/sync/sync-queries.js +49 -32
  92. package/dist/engine/sync/sync-state.d.ts +11 -0
  93. package/dist/engine/sync/sync-state.js +42 -5
  94. package/dist/engine/ui/sync-format.js +22 -7
  95. package/dist/engine-service.js +24 -2
  96. package/docs/architecture.md +28 -0
  97. package/package.json +2 -1
package/CHANGELOG.md CHANGED
@@ -1,5 +1,24 @@
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
+
14
+ ## 0.7.0
15
+
16
+ ### Minor Changes
17
+
18
+ - 4d02a81: Default new Pi Sync setups to the Pi agent directory, with editable exclusions for sessions, managed binaries, and npm installations. Directory scope can restore selected exceptions to exclusions, but Pi credentials and project trust stay local. Existing selected-path setups keep their rules.
19
+
20
+ Recheck the directory scan limits before deletions and writes. If a recovery staging attempt stops before ownership is saved, retry in a fresh path and preserve the unverified files.
21
+
3
22
  ## 0.6.2
4
23
 
5
24
  ### Patch 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
 
@@ -50,7 +62,7 @@ Pi starts the service only after configured interactive synchronization is neede
50
62
 
51
63
  ## ⚙️ Settings
52
64
 
53
- Existing `pi-sync.json`, canonical `pi-sync/`, and legacy `.pisync/` remain in the selected agent directory. There is no second backend or credential configuration format. The daemon is the authoritative Pi Sync settings writer; stale reviews must reopen rather than overwrite a newer document.
65
+ Existing `pi-sync.json`, canonical `pi-sync/`, and legacy `.pisync/` remain in the selected agent directory. There is no second backend or credential configuration format. The daemon is the authoritative Pi Sync settings writer; stale reviews must reopen rather than overwrite a newer document. Settings version 7 and snapshot version 5 support [agent-directory scope](https://github.com/narumiruna/pi-extensions/blob/main/packages/pi-sync/skills/configure-pi-sync/references/configuration.md#agent-directory-scope). Older clients must refuse these formats, and existing selected-path setups do not change scope automatically.
54
66
 
55
67
  Automatic, Autonomous, and Continuous defaults remain unchanged. Old permission for work while Pi is open does not permit work after the last client exits. `/sync daemon allow-after-exit` reviews that extra permission for the current setup and selected independently reproducible provider/model; `/sync daemon revoke-after-exit` revokes it. Setup changes invalidate the saved digest. Changes to an interactive client’s model, working directory, or session-directory binding revoke older unattended grants, including across restart. Print/JSON never launch a service; passive attachment to an existing service protects their sessions and pauses automatic work. Registered live PIDs keep the service alive, including passive, disconnected, publishing, and orderly retired clients. A valid explicit after-exit grant for the active setup keeps service lifetime under the existing fingerprint and binding rules; it does not expand transfer or model permission. Temporary provider or backend unavailability does not itself revoke that grant. Lifetime checks do not call a model, contact a provider or storage, or execute credential commands. There is no new setting, environment variable, or keep-alive flag.
56
68
 
@@ -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
@@ -3,6 +3,7 @@ import { closeSync, fstatSync, fsyncSync, lstatSync, mkdirSync, openSync, readFi
3
3
  import path from "node:path";
4
4
  import { syncCheckConfigFingerprint } from "./engine/settings/config.js";
5
5
  import { stateDir } from "./engine/state/state-directory.js";
6
+ import { safeTerminalText } from "./engine/ui/terminal-text.js";
6
7
  import { record } from "./protocol.js";
7
8
  function file(root) {
8
9
  return path.join(stateDir(root), "daemon.json");
@@ -195,6 +196,7 @@ export async function authorizeAfterExit(execution, payload) {
195
196
  await requireHandover(execution);
196
197
  const { loadConfig } = await import("./engine/settings/config.js");
197
198
  const config = await loadConfig();
199
+ execution.validate();
198
200
  const facts = execution.facts();
199
201
  const binding = JSON.stringify({ model: facts?.model, sessionDir: facts?.sessionDir, cwd: facts?.cwd });
200
202
  const fingerprint = consentFingerprint(execution.root, config);
@@ -203,15 +205,38 @@ export async function authorizeAfterExit(execution, payload) {
203
205
  throw new Error("Setup changed; reopen the daemon authorization review.");
204
206
  if (payload.afterExit && (!config.continuous || !config.agentMode))
205
207
  throw new Error("Enable the reviewed Autonomous and Continuous policies first.");
208
+ const paths = (rules) => rules?.map(safeTerminalText).join(", ") || "none";
209
+ const selection = config.ignorePolicy
210
+ ? [
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
+ ].join("\n")
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)}`;
206
223
  const accepted = await execution.request("confirm", {
207
224
  title: payload.afterExit ? "Allow continuous sync after all Pi clients exit?" : "Revoke unattended sync?",
208
- message: `Setup: ${config.setupName}\nSelected content: ${config.include.join(", ")}\nModel: ${facts?.model?.provider ?? "unavailable"}/${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."}`,
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."}`,
209
226
  });
210
227
  execution.validate();
211
228
  if (accepted !== true)
212
229
  return false;
213
230
  const latest = await loadConfig();
214
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
+ }
215
240
  if (consentFingerprint(execution.root, latest) !== fingerprint ||
216
241
  JSON.stringify({
217
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,6 +1,8 @@
1
1
  import { createHash } from "node:crypto";
2
- import { sameLocalFields, validatePortableSnapshot, validateSnapshotFieldPolicy } from "../../sync/local-fields.js";
3
- import { portableSnapshotSelection, sameSnapshotSelection, snapshotSelectionInclude, validateSnapshotSelection, } from "../../sync/sync-policy.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";
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;
6
8
  export const MAX_GIT_TREE_OUTPUT_BYTES = 16 * 1024 * 1024;
@@ -18,10 +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) ||
23
+ !isSupportedSnapshotVersion(manifest.snapshotVersion) ||
25
24
  typeof manifest.snapshotId !== "string" ||
26
25
  manifest.snapshotId.length > 512 ||
27
26
  !/^[A-Za-z0-9._-]+$/u.test(manifest.snapshotId) ||
@@ -55,10 +54,22 @@ export function requireGitManifest(value) {
55
54
  throw new Error("Git publication manifest is malformed.");
56
55
  }
57
56
  validateSnapshotFieldPolicy({ version: manifest.snapshotVersion, localFields: manifest.localFields });
58
- if (manifest.selection !== undefined)
59
- portableSnapshotSelection(manifest.selection);
60
- validateSnapshotSelection({ version: manifest.snapshotVersion, selection: manifest.selection });
57
+ const selection = manifest.selection === undefined ? undefined : portableSnapshotSelection(manifest.selection);
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
+ }
69
+ const selected = selection?.scope ? configuredSnapshotPathMatcher(selection) : undefined;
70
+ const byteLimit = ignoreSnapshot ? MAX_AGENT_DIRECTORY_BYTES : MAX_GIT_SNAPSHOT_BYTES;
61
71
  let total = 0;
72
+ let hasSessionFiles = false;
62
73
  const paths = new Set();
63
74
  for (const rawFile of manifest.files) {
64
75
  if (!rawFile || typeof rawFile !== "object" || Array.isArray(rawFile)) {
@@ -67,7 +78,9 @@ export function requireGitManifest(value) {
67
78
  const file = rawFile;
68
79
  if (!hasExactKeys(file, ["path", "sha256", "size"]) ||
69
80
  !isSafeSnapshotPath(file.path) ||
81
+ (selected !== undefined && !selected(file.path)) ||
70
82
  typeof file.sha256 !== "string" ||
83
+ (ignoreSnapshot && file.sha256.length !== 64) ||
71
84
  !/^[0-9a-f]{64}$/u.test(file.sha256) ||
72
85
  typeof file.size !== "number" ||
73
86
  !isGitPayloadSizeAllowed(file.size) ||
@@ -75,12 +88,24 @@ export function requireGitManifest(value) {
75
88
  throw new Error("Git publication manifest file is malformed.");
76
89
  }
77
90
  total += file.size;
78
- if (!Number.isSafeInteger(total) || total > MAX_GIT_SNAPSHOT_BYTES) {
79
- 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;
80
99
  }
81
100
  paths.add(file.path);
82
101
  }
83
- assertNoPathConflicts([...paths]);
102
+ if (ignoreSnapshot && manifest.syncSessions !== (manifest.snapshotSyncSessions === true || hasSessionFiles)) {
103
+ throw new Error("Invalid Git ignore snapshot session metadata.");
104
+ }
105
+ if (selected)
106
+ assertAgentDirectoryPathLayout(paths);
107
+ else
108
+ assertNoPathConflicts([...paths]);
84
109
  return manifest;
85
110
  }
86
111
  export function validateGitSnapshot(snapshot, manifest, namespace) {
@@ -104,7 +129,8 @@ export function validateGitSnapshot(snapshot, manifest, namespace) {
104
129
  }
105
130
  }
106
131
  export function prepareGitSnapshot(snapshot, namespace) {
107
- snapshotSelectionInclude(snapshot);
132
+ if (snapshot.version !== IGNORE_SNAPSHOT_VERSION)
133
+ snapshotSelectionInclude(snapshot);
108
134
  validatePortableSnapshot(snapshot);
109
135
  if (typeof snapshot.id !== "string" ||
110
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
+ }>;