@narumitw/pi-sync-daemon 0.7.0 → 0.9.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (93) hide show
  1. package/CHANGELOG.md +21 -0
  2. package/README.md +16 -2
  3. package/dist/cli.js +1 -1
  4. package/dist/consent.d.ts +14 -2
  5. package/dist/consent.js +147 -14
  6. package/dist/engine/backends/git/git-backend.d.ts +2 -3
  7. package/dist/engine/backends/git/git-backend.js +71 -25
  8. package/dist/engine/backends/git/git-storage.d.ts +1 -1
  9. package/dist/engine/backends/git/git-storage.js +30 -9
  10. package/dist/engine/backends/s3/s3-backend.js +8 -0
  11. package/dist/engine/backends/sync-backend.d.ts +1 -0
  12. package/dist/engine/backends/webdav/webdav-backend.js +8 -0
  13. package/dist/engine/commands/command-types.d.ts +2 -1
  14. package/dist/engine/host.d.ts +1 -0
  15. package/dist/engine/ignore/effective-ignore-policy.d.ts +10 -0
  16. package/dist/engine/ignore/effective-ignore-policy.js +33 -0
  17. package/dist/engine/ignore/ignore-edit.d.ts +13 -0
  18. package/dist/engine/ignore/ignore-edit.js +102 -0
  19. package/dist/engine/ignore/ignore-initialization.d.ts +9 -0
  20. package/dist/engine/ignore/ignore-initialization.js +252 -0
  21. package/dist/engine/ignore/ignore-migration-prompt.d.ts +7 -0
  22. package/dist/engine/ignore/ignore-migration-prompt.js +139 -0
  23. package/dist/engine/ignore/ignore-settings.d.ts +30 -0
  24. package/dist/engine/ignore/ignore-settings.js +393 -0
  25. package/dist/engine/ignore/ignore-source.d.ts +27 -0
  26. package/dist/engine/ignore/ignore-source.js +250 -0
  27. package/dist/engine/settings/config-file.d.ts +9 -2
  28. package/dist/engine/settings/config-file.js +124 -28
  29. package/dist/engine/settings/config.d.ts +6 -2
  30. package/dist/engine/settings/config.js +140 -11
  31. package/dist/engine/settings/settings-management.d.ts +5 -3
  32. package/dist/engine/settings/settings-management.js +24 -10
  33. package/dist/engine/settings/settings-types.d.ts +25 -2
  34. package/dist/engine/settings/settings-validation.js +23 -5
  35. package/dist/engine/snapshot/session-paths.d.ts +1 -1
  36. package/dist/engine/snapshot/session-paths.js +41 -13
  37. package/dist/engine/snapshot/snapshot-apply.d.ts +3 -3
  38. package/dist/engine/snapshot/snapshot-apply.js +23 -4
  39. package/dist/engine/snapshot/snapshot-codec.d.ts +1 -1
  40. package/dist/engine/snapshot/snapshot-codec.js +14 -5
  41. package/dist/engine/snapshot/snapshot-exclude-safety.d.ts +4 -2
  42. package/dist/engine/snapshot/snapshot-exclude-safety.js +76 -19
  43. package/dist/engine/snapshot/snapshot-transaction-images.d.ts +3 -1
  44. package/dist/engine/snapshot/snapshot-transaction-images.js +33 -3
  45. package/dist/engine/snapshot/snapshot-transaction-policy.d.ts +30 -2
  46. package/dist/engine/snapshot/snapshot-transaction-policy.js +135 -11
  47. package/dist/engine/snapshot/snapshot-transaction-verification.d.ts +7 -0
  48. package/dist/engine/snapshot/snapshot-transaction-verification.js +78 -0
  49. package/dist/engine/snapshot/snapshot-transaction.d.ts +1 -12
  50. package/dist/engine/snapshot/snapshot-transaction.js +129 -109
  51. package/dist/engine/snapshot/snapshot-types.d.ts +27 -1
  52. package/dist/engine/snapshot/snapshot-types.js +9 -1
  53. package/dist/engine/snapshot/snapshot.d.ts +6 -4
  54. package/dist/engine/snapshot/snapshot.js +91 -34
  55. package/dist/engine/state/state-types.d.ts +1 -0
  56. package/dist/engine/sync/agent-plan.js +1 -0
  57. package/dist/engine/sync/agent-sync.js +8 -1
  58. package/dist/engine/sync/continuous-sync.d.ts +3 -1
  59. package/dist/engine/sync/continuous-sync.js +50 -18
  60. package/dist/engine/sync/continuous-watch.js +4 -1
  61. package/dist/engine/sync/initialization.js +3 -2
  62. package/dist/engine/sync/local-fields.d.ts +3 -1
  63. package/dist/engine/sync/local-fields.js +62 -1
  64. package/dist/engine/sync/merge-acceptance.d.ts +8 -0
  65. package/dist/engine/sync/merge-acceptance.js +29 -0
  66. package/dist/engine/sync/merge-apply.js +42 -11
  67. package/dist/engine/sync/merge-journal.d.ts +24 -4
  68. package/dist/engine/sync/merge-journal.js +134 -8
  69. package/dist/engine/sync/merged-sync.d.ts +1 -1
  70. package/dist/engine/sync/merged-sync.js +128 -83
  71. package/dist/engine/sync/remote-snapshot.d.ts +11 -3
  72. package/dist/engine/sync/remote-snapshot.js +72 -18
  73. package/dist/engine/sync/session-push.js +20 -6
  74. package/dist/engine/sync/sync-inspection.js +7 -5
  75. package/dist/engine/sync/sync-mutations.d.ts +2 -1
  76. package/dist/engine/sync/sync-mutations.js +157 -73
  77. package/dist/engine/sync/sync-policy.d.ts +29 -8
  78. package/dist/engine/sync/sync-policy.js +152 -16
  79. package/dist/engine/sync/sync-queries.js +8 -1
  80. package/dist/engine/sync/sync-state.d.ts +4 -2
  81. package/dist/engine/sync/sync-state.js +15 -2
  82. package/dist/engine/sync/transfer-freshness.d.ts +31 -0
  83. package/dist/engine/sync/transfer-freshness.js +82 -0
  84. package/dist/engine-service.d.ts +1 -1
  85. package/dist/engine-service.js +146 -27
  86. package/dist/monitor.d.ts +4 -1
  87. package/dist/monitor.js +84 -32
  88. package/dist/server.d.ts +1 -0
  89. package/dist/server.js +46 -5
  90. package/dist/service-host.d.ts +1 -1
  91. package/dist/service-host.js +2 -1
  92. package/docs/architecture.md +30 -0
  93. package/package.json +2 -1
package/CHANGELOG.md CHANGED
@@ -1,5 +1,26 @@
1
1
  # @narumitw/pi-sync-daemon
2
2
 
3
+ ## 0.9.0
4
+
5
+ ### Minor Changes
6
+
7
+ - 5dfa289: Resolve local ignore-file policies for reviewed setup, inspection, transfer, and recovery while keeping legacy policies usable in mixed settings. Keep rule files local and publish only the portable policy fingerprint in version 6 snapshots. Create a missing starter after confirmation on Linux, macOS, and Windows; if automatic creation is unavailable, show the exact path and instructions for manual creation. Preserve the existing settings writer and keep in-app rule saves disabled.
8
+
9
+ ### Patch Changes
10
+
11
+ - 25777f7: Read Git history manifests in bounded batches to reduce subprocess launches. Keep the same history preview and validate all retained publications in the original order.
12
+
13
+ ## 0.8.0
14
+
15
+ ### Minor Changes
16
+
17
+ - 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.
18
+
19
+ ### Patch Changes
20
+
21
+ - 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.
22
+ - 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.
23
+
3
24
  ## 0.7.0
4
25
 
5
26
  ### 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/cli.js CHANGED
@@ -39,7 +39,7 @@ export async function main(args) {
39
39
  log?.close(); /* Never follow a replaced diagnostic path. */
40
40
  }
41
41
  };
42
- const service = createEngineService(paths.root);
42
+ const service = createEngineService(paths.root, true);
43
43
  let server;
44
44
  try {
45
45
  server = await startServer(paths.root, service.execute, {
package/dist/consent.d.ts CHANGED
@@ -7,6 +7,7 @@ interface Consent {
7
7
  nonce: string;
8
8
  setups: Record<string, {
9
9
  fingerprint: string;
10
+ automaticFingerprint?: string;
10
11
  model?: ClientFacts["model"];
11
12
  sessionDir?: string;
12
13
  cwd?: string;
@@ -17,10 +18,21 @@ export declare class ConsentPublicationError extends Error {
17
18
  }
18
19
  /** A different interactive binding cannot silently resume an older unattended grant after exit/restart. */
19
20
  export declare function revokeChangedBindings(root: string, peers: readonly ClientFacts[]): void;
20
- /** Explicit offline repair only. The caller must exclude daemon owners and state migration. */
21
+ export declare function revokeAfterExitGrants(root: string): number;
21
22
  export declare function revokeRetainedGrants(root: string): number;
22
23
  export declare function readConsent(root: string): Consent | undefined;
23
24
  export declare function consentFingerprint(root: string, config: AnySyncConfig): string;
25
+ export declare function automaticPolicyAuthorized(root: string, config: AnySyncConfig): boolean;
26
+ export declare function reviewAutomaticPolicy(execution: Execution, config: AnySyncConfig, expectedFingerprint: string): Promise<boolean>;
27
+ export declare function captureAutomaticAuthorization(execution: Execution, expectedFingerprint?: string): Promise<{
28
+ config: AnySyncConfig;
29
+ validate: (current?: AnySyncConfig) => void;
30
+ refresh: () => Promise<void>;
31
+ }>;
32
+ export declare function participantBinding(execution: Pick<Execution, "participants">): string;
24
33
  export declare function requireHandover(execution: Execution): Promise<void>;
25
- export declare function authorizeAfterExit(execution: Execution, payload: unknown): Promise<boolean>;
34
+ export declare function authorizeAfterExit(execution: Execution, payload: unknown): Promise<{
35
+ config: AnySyncConfig;
36
+ afterExit: boolean;
37
+ } | undefined>;
26
38
  export {};
package/dist/consent.js CHANGED
@@ -3,6 +3,8 @@ 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 { startupTransferEnabled } from "./engine/sync/agent-policy.js";
7
+ import { syncSessionsSelected } from "./engine/sync/sync-policy.js";
6
8
  import { safeTerminalText } from "./engine/ui/terminal-text.js";
7
9
  import { record } from "./protocol.js";
8
10
  function file(root) {
@@ -82,7 +84,9 @@ export function revokeChangedBindings(root, peers) {
82
84
  if (changed)
83
85
  publishConsent(consent);
84
86
  }
85
- /** Explicit offline repair only. The caller must exclude daemon owners and state migration. */
87
+ export function revokeAfterExitGrants(root) {
88
+ return revokeRetainedGrants(root);
89
+ }
86
90
  export function revokeRetainedGrants(root) {
87
91
  const consent = readConsent(root);
88
92
  if (!consent)
@@ -129,6 +133,8 @@ export function readConsent(root) {
129
133
  if (!record(entry) ||
130
134
  typeof entry.fingerprint !== "string" ||
131
135
  !/^[a-f0-9]{64}$/u.test(entry.fingerprint) ||
136
+ (entry.automaticFingerprint !== undefined &&
137
+ (typeof entry.automaticFingerprint !== "string" || !/^[a-f0-9]{64}$/u.test(entry.automaticFingerprint))) ||
132
138
  typeof entry.afterExit !== "boolean" ||
133
139
  (entry.cwd !== undefined &&
134
140
  (typeof entry.cwd !== "string" || !path.isAbsolute(entry.cwd) || entry.cwd.includes("\0"))) ||
@@ -167,6 +173,119 @@ export function consentFingerprint(root, config) {
167
173
  .update(syncCheckConfigFingerprint(config))
168
174
  .digest("hex");
169
175
  }
176
+ export function automaticPolicyAuthorized(root, config) {
177
+ if (config.reservedIgnoreNames === undefined)
178
+ return true;
179
+ const saved = readConsent(root)?.setups[config.setupName];
180
+ return saved?.automaticFingerprint !== undefined && saved.automaticFingerprint === consentFingerprint(root, config);
181
+ }
182
+ export async function reviewAutomaticPolicy(execution, config, expectedFingerprint) {
183
+ if (consentFingerprint(execution.root, config) !== expectedFingerprint)
184
+ throw new Error("Setup changed; reopen the automatic transfer review.");
185
+ if (config.reservedIgnoreNames === undefined || automaticPolicyAuthorized(execution.root, config))
186
+ return false;
187
+ const consent = readConsent(execution.root);
188
+ if (!consent)
189
+ return false;
190
+ const consentToken = JSON.stringify(consent);
191
+ const peers = participantBinding(execution);
192
+ const binding = JSON.stringify(execution.facts());
193
+ const { loadConfigForCheck } = await import("./engine/settings/config.js");
194
+ const fingerprint = syncCheckConfigFingerprint(config);
195
+ const validate = () => {
196
+ execution.validate();
197
+ execution.signal.throwIfAborted();
198
+ if (participantBinding(execution) !== peers ||
199
+ JSON.stringify(execution.facts()) !== binding ||
200
+ JSON.stringify(readConsent(execution.root)) !== consentToken)
201
+ throw new Error("Participants or consent changed during automatic transfer review.");
202
+ };
203
+ const current = await loadConfigForCheck(undefined, execution.signal);
204
+ validate();
205
+ if (syncCheckConfigFingerprint(current) !== fingerprint)
206
+ return false;
207
+ const modes = [
208
+ startupTransferEnabled(config) && "Automatic transfer at startup",
209
+ config.automatic &&
210
+ syncSessionsSelected(config) &&
211
+ "Push selected files on shutdown (including sessions when selected)",
212
+ config.continuous && config.agentMode && "Continuous sync and Autonomous model requests",
213
+ ].filter(Boolean);
214
+ if (modes.length === 0)
215
+ throw new Error("No automatic transfer mode is enabled under the current local rules.");
216
+ const accepted = await execution.request("confirm", {
217
+ title: "Allow the enabled automatic sync modes for this setup?",
218
+ message: [
219
+ `Setup: ${safeTerminalText(config.setupName)}`,
220
+ `Destination: ${safeTerminalText(config.connectionName)} · ${safeTerminalText(config.storagePath)}`,
221
+ ...(config.ignorePolicy
222
+ ? [
223
+ `Local ignore file: ${safeTerminalText(config.ignorePolicy.filename)}`,
224
+ "Review this file before you approve its scope.",
225
+ ]
226
+ : []),
227
+ `Enabled modes: ${modes.join("; ")}`,
228
+ "Their usual baseline, remote selection, and ownership checks still apply. This does not allow work after the last Pi exits. A settings or rule-file change requires a new review.",
229
+ ].join("\n"),
230
+ });
231
+ validate();
232
+ if (accepted !== true)
233
+ return false;
234
+ const latest = await loadConfigForCheck(undefined, execution.signal);
235
+ await config.validateIgnorePolicy?.();
236
+ validate();
237
+ if (syncCheckConfigFingerprint(latest) !== fingerprint)
238
+ throw new Error("Setup changed during automatic transfer review.");
239
+ const saved = consent.setups[config.setupName];
240
+ const automaticFingerprint = consentFingerprint(execution.root, config);
241
+ publishConsent({
242
+ ...consent,
243
+ setups: {
244
+ ...consent.setups,
245
+ [config.setupName]: {
246
+ ...saved,
247
+ fingerprint: saved?.fingerprint ?? automaticFingerprint,
248
+ afterExit: saved?.afterExit ?? false,
249
+ automaticFingerprint,
250
+ },
251
+ },
252
+ });
253
+ return true;
254
+ }
255
+ export async function captureAutomaticAuthorization(execution, expectedFingerprint) {
256
+ const consent = readConsent(execution.root);
257
+ if (!consent)
258
+ throw new Error("Automatic work requires reviewed daemon handover first.");
259
+ const consentToken = JSON.stringify(consent);
260
+ const peers = participantBinding(execution);
261
+ const { loadConfigForCheck } = await import("./engine/settings/config.js");
262
+ const validateParticipants = () => {
263
+ execution.validate();
264
+ if (participantBinding(execution) !== peers || JSON.stringify(readConsent(execution.root)) !== consentToken)
265
+ throw new Error("Daemon participants or consent changed before automatic work completed.");
266
+ };
267
+ validateParticipants();
268
+ const config = await loadConfigForCheck(undefined, execution.signal);
269
+ validateParticipants();
270
+ const fingerprint = syncCheckConfigFingerprint(config);
271
+ if (expectedFingerprint !== undefined ? fingerprint !== expectedFingerprint : config.reservedIgnoreNames !== undefined)
272
+ throw new Error("Settings changed before automatic work; use the reviewed foreground command.");
273
+ if (!automaticPolicyAuthorized(execution.root, config))
274
+ throw new Error("Automatic transfer requires separate approval for the current setup and policy. Run reviewed /sync first.");
275
+ const validate = (current) => {
276
+ validateParticipants();
277
+ if (current && syncCheckConfigFingerprint(current) !== fingerprint)
278
+ throw new Error("Settings changed before automatic work completed.");
279
+ };
280
+ const refresh = async () => {
281
+ validate();
282
+ validate(await loadConfigForCheck(undefined, execution.signal));
283
+ };
284
+ return { config, validate, refresh };
285
+ }
286
+ export function participantBinding(execution) {
287
+ return JSON.stringify(execution.participants().toSorted((left, right) => left.id.localeCompare(right.id)));
288
+ }
170
289
  export async function requireHandover(execution) {
171
290
  if (readConsent(execution.root))
172
291
  return;
@@ -194,8 +313,12 @@ export async function authorizeAfterExit(execution, payload) {
194
313
  if (!record(payload) || typeof payload.afterExit !== "boolean" || typeof payload.fingerprint !== "string")
195
314
  throw new Error("Invalid daemon authorization.");
196
315
  await requireHandover(execution);
197
- const { loadConfig } = await import("./engine/settings/config.js");
198
- const config = await loadConfig();
316
+ const peers = participantBinding(execution);
317
+ const { loadConfigForCheck } = await import("./engine/settings/config.js");
318
+ const config = await loadConfigForCheck(undefined, execution.signal);
319
+ execution.validate();
320
+ if (participantBinding(execution) !== peers)
321
+ throw new Error("Participants changed during daemon authorization.");
199
322
  const facts = execution.facts();
200
323
  const binding = JSON.stringify({ model: facts?.model, sessionDir: facts?.sessionDir, cwd: facts?.cwd });
201
324
  const fingerprint = consentFingerprint(execution.root, config);
@@ -205,24 +328,33 @@ export async function authorizeAfterExit(execution, payload) {
205
328
  if (payload.afterExit && (!config.continuous || !config.agentMode))
206
329
  throw new Error("Enable the reviewed Autonomous and Continuous policies first.");
207
330
  const paths = (rules) => rules?.map(safeTerminalText).join(", ") || "none";
208
- const selection = config.scope === "agent-directory"
331
+ const selection = config.ignorePolicy
209
332
  ? [
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"}`,
333
+ "Selected content: eligible files in the Pi agent directory",
334
+ `Local ignore file: ${safeTerminalText(config.ignorePolicy.filename)}`,
335
+ `Session opt-in: ${config.ignorePolicy.sessions ? "On — ignore rules still apply" : "Off"}`,
214
336
  ].join("\n")
215
- : `Selected content: ${paths(config.include)}`;
337
+ : config.scope === "agent-directory"
338
+ ? [
339
+ "Selected content: all eligible files in the Pi agent directory",
340
+ `Excluded paths: ${paths(config.exclude)}`,
341
+ `Re-included paths: ${paths(config.reinclude)}`,
342
+ `Session opt-in: ${config.include.includes("sessions") ? "On — path rules still apply" : "Off"}`,
343
+ ].join("\n")
344
+ : `Selected content: ${paths(config.include)}`;
216
345
  const accepted = await execution.request("confirm", {
217
346
  title: payload.afterExit ? "Allow continuous sync after all Pi clients exit?" : "Revoke unattended sync?",
218
- 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."}`,
347
+ 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.${config.reservedIgnoreNames !== undefined ? " This also approves the enabled automatic modes for this setup while Pi clients are attached." : ""}` : "The daemon may remain alive, but no new transfer or model request may start with zero clients."}`,
219
348
  });
220
349
  execution.validate();
221
350
  if (accepted !== true)
222
- return false;
223
- const latest = await loadConfig();
351
+ return undefined;
352
+ const latest = await loadConfigForCheck(undefined, execution.signal);
353
+ execution.validate();
354
+ await config.validateIgnorePolicy?.();
224
355
  execution.validate();
225
- if (consentFingerprint(execution.root, latest) !== fingerprint ||
356
+ if (participantBinding(execution) !== peers ||
357
+ consentFingerprint(execution.root, latest) !== fingerprint ||
226
358
  JSON.stringify({
227
359
  model: execution.facts()?.model,
228
360
  sessionDir: execution.facts()?.sessionDir,
@@ -243,8 +375,9 @@ export async function authorizeAfterExit(execution, payload) {
243
375
  sessionDir: facts?.sessionDir,
244
376
  cwd: facts?.cwd,
245
377
  afterExit: payload.afterExit,
378
+ ...(payload.afterExit && config.reservedIgnoreNames !== undefined ? { automaticFingerprint: fingerprint } : {}),
246
379
  },
247
380
  },
248
381
  });
249
- return true;
382
+ return { config, afterExit: payload.afterExit };
250
383
  }
@@ -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 {
@@ -35,8 +35,7 @@ export declare class GitSyncBackend implements SyncBackend {
35
35
  private resolveSnapshotReference;
36
36
  private headForSha;
37
37
  private fetchRemoteHead;
38
- private readManifest;
39
- private showFile;
38
+ private tryReadHistoryManifestBatch;
40
39
  private createCommit;
41
40
  private ensureCache;
42
41
  private initializeCache;
@@ -1,16 +1,20 @@
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";
8
10
  import { SyncBackendConflictError, SyncBackendPublicationOutcomeUnknownError, } from "../sync-backend.js";
9
11
  import { normalizeGitBranch, normalizeGitDirectory, normalizeGitRemote, normalizeGitRemoteIdentity, validateGitNamespace, } from "./git-config.js";
10
- import { GitCommandError, readGitBlobs, runGit } from "./git-runner.js";
12
+ import { GitCommandError, parseGitBlobBatch, readGitBlobs, runGit } from "./git-runner.js";
11
13
  import { GIT_MANIFEST_VERSION, MAX_GIT_MANIFEST_BYTES, MAX_GIT_TREE_OUTPUT_BYTES, parseGitTree, prepareGitSnapshot, requireGitManifest, validateGitPublicationTree, validateGitSnapshot, } from "./git-storage.js";
12
14
  const COMMAND_TIMEOUT_MS = 30_000;
13
15
  const POST_COMMIT_TIMEOUT_MS = 45_000;
16
+ const HISTORY_MANIFEST_BATCH_SIZE = 8;
17
+ const MANIFEST_BATCH_FRAME_BYTES = 96;
14
18
  const gitCacheMutationQueues = new Map();
15
19
  export class GitSyncBackend {
16
20
  config;
@@ -136,6 +140,10 @@ export class GitSyncBackend {
136
140
  throw this.redactedError(error);
137
141
  }
138
142
  throwIfAborted(options.signal);
143
+ if (options.validateIgnorePolicy) {
144
+ await options.validateIgnorePolicy();
145
+ throwIfAborted(options.signal);
146
+ }
139
147
  options.onCommit?.();
140
148
  const ref = this.remoteRef();
141
149
  const lease = `--force-with-lease=${ref}:${observed ?? ""}`;
@@ -176,20 +184,28 @@ export class GitSyncBackend {
176
184
  }
177
185
  async listHistory(signal) {
178
186
  const sha = await this.fetchRemoteHead(signal);
187
+ throwIfAborted(signal);
179
188
  if (!sha)
180
189
  return [];
181
190
  const result = await this.git(["rev-list", "--first-parent", "--reverse", "--max-count=100", sha], { signal });
182
191
  const commits = result.stdout.toString("utf8").trim().split("\n").filter(Boolean);
183
192
  const entries = [];
184
- for (const commit of commits) {
185
- const { manifest } = await this.readPublication(commit, signal);
186
- entries.push({
187
- snapshotRef: commit,
188
- snapshotId: manifest.snapshotId,
189
- createdAt: manifest.createdAt,
190
- machine: manifest.machine,
191
- syncSessions: manifest.syncSessions,
192
- });
193
+ throwIfAborted(signal);
194
+ for (let offset = 0; offset < commits.length; offset += HISTORY_MANIFEST_BATCH_SIZE) {
195
+ const group = commits.slice(offset, offset + HISTORY_MANIFEST_BATCH_SIZE);
196
+ const manifests = await this.tryReadHistoryManifestBatch(group, signal);
197
+ for (const [index, commit] of group.entries()) {
198
+ throwIfAborted(signal);
199
+ const { manifest } = await this.readPublication(commit, signal, manifests?.[index]);
200
+ throwIfAborted(signal);
201
+ entries.push({
202
+ snapshotRef: commit,
203
+ snapshotId: manifest.snapshotId,
204
+ createdAt: manifest.createdAt,
205
+ machine: manifest.machine,
206
+ syncSessions: manifest.syncSessions,
207
+ });
208
+ }
193
209
  }
194
210
  return entries;
195
211
  }
@@ -308,20 +324,27 @@ export class GitSyncBackend {
308
324
  }
309
325
  }, signal);
310
326
  }
311
- async readManifest(commit, signal) {
312
- requireCommitSha(commit);
313
- const bytes = await this.showFile(commit, this.manifestPath(), signal, MAX_GIT_MANIFEST_BYTES);
314
- let parsed;
327
+ async tryReadHistoryManifestBatch(commits, signal) {
315
328
  try {
316
- parsed = JSON.parse(bytes.toString("utf8"));
329
+ for (const commit of commits)
330
+ requireCommitSha(commit);
331
+ const manifestPath = this.manifestPath();
332
+ const maxContentBytes = commits.length * MAX_GIT_MANIFEST_BYTES;
333
+ const result = await this.git(["cat-file", "--batch"], {
334
+ input: `${commits.map((commit) => `${commit}:${manifestPath}`).join("\n")}\n`,
335
+ maxOutputBytes: maxContentBytes + commits.length * MANIFEST_BATCH_FRAME_BYTES,
336
+ signal,
337
+ });
338
+ throwIfAborted(signal);
339
+ if (result.stderr.length > 0)
340
+ return undefined;
341
+ const blobs = parseGitBlobBatch(result.stdout, commits.length, maxContentBytes);
342
+ return blobs.every((blob) => blob.length <= MAX_GIT_MANIFEST_BYTES) ? blobs : undefined;
317
343
  }
318
- catch (error) {
319
- throw new Error("Git publication manifest is malformed.", { cause: error });
344
+ catch {
345
+ throwIfAborted(signal);
346
+ return undefined;
320
347
  }
321
- return requireGitManifest(parsed);
322
- }
323
- showFile(commit, filePath, signal, maxOutputBytes) {
324
- return this.git(["show", `${commit}:${filePath}`], { signal, maxOutputBytes }).then((result) => result.stdout);
325
348
  }
326
349
  async createCommit(snapshot, files, manifest, parent, signal) {
327
350
  const manifestBytes = Buffer.from(`${JSON.stringify(manifest)}\n`, "utf8");
@@ -501,17 +524,23 @@ export class GitSyncBackend {
501
524
  filePath(filePath) {
502
525
  return posixJoin(this.publicationPath(), "files", filePath);
503
526
  }
504
- async readPublication(commit, signal) {
505
- const manifest = await this.readManifest(commit, signal);
506
- const entries = await this.readPublicationTree(commit, signal);
527
+ async readPublication(commit, signal, manifestBytes) {
528
+ requireCommitSha(commit);
529
+ const bytes = manifestBytes ??
530
+ (await this.git(["show", `${commit}:${this.manifestPath()}`], { signal, maxOutputBytes: MAX_GIT_MANIFEST_BYTES }))
531
+ .stdout;
532
+ const manifest = parseGitManifest(bytes);
533
+ const entries = await this.readPublicationTree(commit, signal, manifest.snapshotVersion === IGNORE_SNAPSHOT_VERSION);
507
534
  const payloadEntries = validateGitPublicationTree(entries, manifest, this.manifestPath(), (filePath) => this.filePath(filePath));
508
535
  return { manifest, payloadEntries };
509
536
  }
510
- async readPublicationTree(commit, signal) {
537
+ async readPublicationTree(commit, signal, strictUtf8 = false) {
511
538
  const result = await this.git(["ls-tree", "-r", "-z", commit], {
512
539
  signal,
513
540
  maxOutputBytes: MAX_GIT_TREE_OUTPUT_BYTES,
514
541
  });
542
+ if (strictUtf8 && !isUtf8(result.stdout))
543
+ throw new Error("Git snapshot tree paths must be valid UTF-8.");
515
544
  return parseGitTree(result.stdout);
516
545
  }
517
546
  redactedError(error) {
@@ -557,6 +586,23 @@ function gitDestination(config) {
557
586
  }
558
587
  return `${host} · ${config.destination.branch}:${config.destination.directory}`;
559
588
  }
589
+ function parseGitManifest(bytes) {
590
+ let parsed;
591
+ try {
592
+ parsed = JSON.parse(bytes.toString("utf8"));
593
+ }
594
+ catch (error) {
595
+ throw new Error("Git publication manifest is malformed.", { cause: error });
596
+ }
597
+ if (parsed &&
598
+ typeof parsed === "object" &&
599
+ "snapshotVersion" in parsed &&
600
+ parsed.snapshotVersion === IGNORE_SNAPSHOT_VERSION &&
601
+ !isUtf8(bytes)) {
602
+ throw new Error("Git publication manifest is not valid UTF-8.");
603
+ }
604
+ return requireGitManifest(parsed);
605
+ }
560
606
  function remoteHead(sha, manifest, identity) {
561
607
  return {
562
608
  snapshotRef: sha,
@@ -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
  }