@narumitw/pi-sync 0.24.0 → 0.28.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/src/snapshot.ts CHANGED
@@ -5,20 +5,25 @@ import os from "node:os";
5
5
  import path from "node:path";
6
6
  import { agentDir, configuredSessionDir } from "./config.js";
7
7
  import { isDeniedPath, posixJoin, safeJoin, toPosix } from "./paths.js";
8
+
8
9
  export { isDeniedPath } from "./paths.js";
9
- import type {
10
- Snapshot,
11
- SnapshotFile,
12
- SnapshotOptions,
13
- SyncConfig,
14
- } from "./types.js";
10
+
11
+ import {
12
+ canonicalSnapshotPathForConfig,
13
+ DEFAULT_SYNC_FILES,
14
+ extraFilePathsByLower,
15
+ isConfiguredSnapshotPath,
16
+ isPreservableUnmanagedSnapshotPath,
17
+ normalizeExtraFiles,
18
+ normalizeSyncFiles,
19
+ } from "./sync-policy.js";
20
+ import type { Snapshot, SnapshotFile, SnapshotOptions, SyncConfig } from "./types.js";
21
+
22
+ export { canonicalSnapshotPathForConfig, isConfiguredSnapshotPath } from "./sync-policy.js";
15
23
 
16
24
  const VERSION = 1;
17
- const TOP_LEVEL_FILES = new Set(["settings.json", "keybindings.json", "models.json", "AGENTS.md", "APPEND_SYSTEM.md"]);
18
- const TOP_LEVEL_DIRS = new Set(["skills", "prompts", "themes", "extensions"]);
19
- const TOP_LEVEL_FILE_PATHS = new Map([...TOP_LEVEL_FILES].map((name) => [name.toLowerCase(), name]));
20
- const TOP_LEVEL_FILE_NAMES = new Set(TOP_LEVEL_FILE_PATHS.keys());
21
- const RESERVED_TOP_LEVEL_NAMES = new Set([...TOP_LEVEL_DIRS, "sessions"]);
25
+ const TOP_LEVEL_FILES: readonly string[] = DEFAULT_SYNC_FILES.filter((name) => name.includes("."));
26
+ const TOP_LEVEL_DIRS = new Set<string>(DEFAULT_SYNC_FILES.filter((name) => !name.includes(".")));
22
27
  const SECRET_PATTERNS = [
23
28
  /AWS_SECRET_ACCESS_KEY\s*[=:]\s*['"]?[A-Za-z0-9/+]{35,}/i,
24
29
  /(ANTHROPIC|OPENAI|GEMINI|GOOGLE|FIRECRAWL|GITHUB|CLOUDFLARE|R2|S3)_[A-Z0-9_]*(KEY|TOKEN|SECRET)\s*[=:]\s*['"]?[^\s'"]{12,}/i,
@@ -33,27 +38,6 @@ function expandHome(value: string) {
33
38
  return value;
34
39
  }
35
40
 
36
- export function normalizeExtraFiles(value: unknown) {
37
- if (!Array.isArray(value)) return [];
38
- const seen = new Set<string>();
39
- return value
40
- .filter((item): item is string => typeof item === "string")
41
- .map((item) => item.trim())
42
- .filter((item) => {
43
- const lower = item.toLowerCase();
44
- if (
45
- item === "" || item === "." || item === ".." || item.includes("/") || item.includes("\\") ||
46
- TOP_LEVEL_FILE_NAMES.has(lower) || isDeniedPath(item) || RESERVED_TOP_LEVEL_NAMES.has(lower) || seen.has(lower)
47
- ) return false;
48
- seen.add(lower);
49
- return true;
50
- });
51
- }
52
-
53
- export function extraFilePathsByLower(value: unknown) {
54
- return new Map(normalizeExtraFiles(value).map((fileName) => [fileName.toLowerCase(), fileName]));
55
- }
56
-
57
41
  function selectTopLevelFileEntry(entries: Dirent[], fileName: string) {
58
42
  const exact = entries.find((entry) => entry.isFile() && entry.name === fileName);
59
43
  if (exact) return exact;
@@ -70,15 +54,24 @@ function sha256(value: Buffer) {
70
54
  function isSafeSnapshotPath(relativePath: string) {
71
55
  if (relativePath.includes("\\")) return false;
72
56
  const normalized = toPosix(relativePath);
73
- return Boolean(normalized) && normalized !== "." && normalized !== ".." && !normalized.startsWith("../") &&
74
- !path.posix.isAbsolute(normalized) && path.posix.normalize(normalized) === normalized && !isDeniedPath(normalized);
57
+ return (
58
+ Boolean(normalized) &&
59
+ normalized !== "." &&
60
+ normalized !== ".." &&
61
+ !normalized.startsWith("../") &&
62
+ !path.posix.isAbsolute(normalized) &&
63
+ path.posix.normalize(normalized) === normalized &&
64
+ !isDeniedPath(normalized)
65
+ );
75
66
  }
76
67
 
77
68
  function snapshotsMatch(left: Snapshot, right: Snapshot) {
78
69
  const leftHashes = new Map(left.files.map((file) => [file.path, file.sha256]));
79
70
  const rightHashes = new Map(right.files.map((file) => [file.path, file.sha256]));
80
- return leftHashes.size === rightHashes.size &&
81
- [...leftHashes].every(([filePath, hash]) => rightHashes.get(filePath) === hash);
71
+ return (
72
+ leftHashes.size === rightHashes.size &&
73
+ [...leftHashes].every(([filePath, hash]) => rightHashes.get(filePath) === hash)
74
+ );
82
75
  }
83
76
 
84
77
  function snapshotId() {
@@ -91,6 +84,7 @@ export async function createSnapshot(
91
84
  ): Promise<Snapshot> {
92
85
  const syncSessions = Boolean(options.syncSessions);
93
86
  const files = await collectFiles(agentDir(), {
87
+ syncFiles: options.syncFiles,
94
88
  syncSessions,
95
89
  sessionDir: options.sessionDir ?? (await configuredSessionDir()),
96
90
  extraFiles: options.extraFiles,
@@ -112,12 +106,14 @@ export async function collectFiles(
112
106
  ): Promise<SnapshotFile[]> {
113
107
  const results: SnapshotFile[] = [];
114
108
  const entries = await fs.readdir(root, { withFileTypes: true });
109
+ const selectedFiles = new Set<string>(normalizeSyncFiles(options.syncFiles));
115
110
  for (const entry of entries) {
116
- if (entry.isDirectory() && TOP_LEVEL_DIRS.has(entry.name)) {
111
+ if (entry.isDirectory() && TOP_LEVEL_DIRS.has(entry.name) && selectedFiles.has(entry.name)) {
117
112
  await collectDirectory(results, root, entry.name);
118
113
  }
119
114
  }
120
115
  for (const fileName of TOP_LEVEL_FILES) {
116
+ if (!selectedFiles.has(fileName)) continue;
121
117
  const entry = selectTopLevelFileEntry(entries, fileName);
122
118
  if (entry) await addFile(results, root, entry.name, fileName);
123
119
  }
@@ -168,7 +164,11 @@ async function addFile(
168
164
  if (!isSafeSnapshotPath(snapshotPath)) return;
169
165
  const absolutePath = safeJoin(root, relativePath);
170
166
  const content = await fs.readFile(absolutePath);
171
- results.push({ path: snapshotPath, contentBase64: content.toString("base64"), sha256: sha256(content) });
167
+ results.push({
168
+ path: snapshotPath,
169
+ contentBase64: content.toString("base64"),
170
+ sha256: sha256(content),
171
+ });
172
172
  }
173
173
 
174
174
  export function isSessionPath(relativePath: string) {
@@ -181,11 +181,18 @@ export function isSessionFilePath(relativePath: string) {
181
181
  }
182
182
 
183
183
  export function sessionStorageRoot(root: string, configuredSessionDir?: string) {
184
- return configuredSessionDir ? path.resolve(expandHome(configuredSessionDir)) : path.resolve(root, "sessions");
184
+ return configuredSessionDir
185
+ ? path.resolve(expandHome(configuredSessionDir))
186
+ : path.resolve(root, "sessions");
185
187
  }
186
188
 
187
- export function sessionSnapshotPathFromAbsolute(sessionFile: string, configuredSessionDir?: string) {
188
- const relativePath = toPosix(path.relative(sessionStorageRoot(agentDir(), configuredSessionDir), sessionFile));
189
+ export function sessionSnapshotPathFromAbsolute(
190
+ sessionFile: string,
191
+ configuredSessionDir?: string,
192
+ ) {
193
+ const relativePath = toPosix(
194
+ path.relative(sessionStorageRoot(agentDir(), configuredSessionDir), sessionFile),
195
+ );
189
196
  if (!relativePath || relativePath.startsWith("../") || path.posix.isAbsolute(relativePath)) {
190
197
  return undefined;
191
198
  }
@@ -195,23 +202,21 @@ export function sessionSnapshotPathFromAbsolute(sessionFile: string, configuredS
195
202
 
196
203
  export function snapshotTarget(root: string, relativePath: string, configuredSessionDir?: string) {
197
204
  if (isSessionPath(relativePath)) {
198
- return safeJoin(sessionStorageRoot(root, configuredSessionDir), relativePath.slice("sessions/".length));
205
+ return safeJoin(
206
+ sessionStorageRoot(root, configuredSessionDir),
207
+ relativePath.slice("sessions/".length),
208
+ );
199
209
  }
200
210
  return safeJoin(root, relativePath);
201
211
  }
202
212
 
203
- function isPathInside(parent: string, child: string) {
204
- const relativePath = path.relative(parent, child);
205
- return relativePath === "" || (!relativePath.startsWith("..") && !path.isAbsolute(relativePath));
206
- }
207
-
208
213
  export function snapshotIncludesSessions(snapshot: Snapshot) {
209
214
  return snapshot.syncSessions === true || snapshot.files.some((file) => isSessionPath(file.path));
210
215
  }
211
216
 
212
217
  export function filterSnapshotForConfigPolicy(
213
218
  snapshot: Snapshot,
214
- config: Pick<SyncConfig, "syncSessions" | "extraFiles">,
219
+ config: Pick<SyncConfig, "syncFiles" | "syncSessions" | "extraFiles">,
215
220
  options: { regenerateId?: boolean } = {},
216
221
  ) {
217
222
  const extraFilePaths = extraFilePathsByLower(config.extraFiles);
@@ -230,23 +235,9 @@ export function filterSnapshotForConfigPolicy(
230
235
  };
231
236
  }
232
237
 
233
- export function isConfiguredSnapshotPath(
234
- relativePath: string,
235
- config: Pick<SyncConfig, "syncSessions">,
236
- extraFiles: Set<string>,
237
- ) {
238
- const normalized = toPosix(relativePath);
239
- if (isSessionPath(normalized)) return config.syncSessions;
240
- if (!normalized.includes("/")) {
241
- const lower = normalized.toLowerCase();
242
- return TOP_LEVEL_FILE_NAMES.has(lower) || extraFiles.has(lower);
243
- }
244
- return TOP_LEVEL_DIRS.has(normalized.slice(0, normalized.indexOf("/")));
245
- }
246
-
247
238
  function canonicalizeSnapshotFilesForConfig(
248
239
  files: SnapshotFile[],
249
- config: Pick<SyncConfig, "syncSessions">,
240
+ config: Pick<SyncConfig, "syncFiles" | "syncSessions">,
250
241
  extraFiles: Set<string>,
251
242
  extraFilePaths: Map<string, string>,
252
243
  ) {
@@ -257,14 +248,17 @@ function canonicalizeSnapshotFilesForConfig(
257
248
  >();
258
249
  for (const file of files) {
259
250
  const normalized = toPosix(file.path);
260
- if (!isSafeSnapshotPath(file.path) || !isConfiguredSnapshotPath(normalized, config, extraFiles)) {
251
+ if (
252
+ !isSafeSnapshotPath(file.path) ||
253
+ !isConfiguredSnapshotPath(normalized, config, extraFiles)
254
+ ) {
261
255
  continue;
262
256
  }
263
- const topLevelPath = canonicalTopLevelFilePath(normalized, extraFilePaths);
264
- if (!topLevelPath) {
257
+ if (normalized.includes("/")) {
265
258
  configuredFiles.push(normalized === file.path ? file : { ...file, path: normalized });
266
259
  continue;
267
260
  }
261
+ const topLevelPath = canonicalSnapshotPathForConfig(normalized, extraFilePaths);
268
262
  const candidate = {
269
263
  exact: normalized === topLevelPath,
270
264
  file: { ...file, path: topLevelPath },
@@ -281,17 +275,6 @@ function canonicalizeSnapshotFilesForConfig(
281
275
  ].sort((left, right) => left.path.localeCompare(right.path));
282
276
  }
283
277
 
284
- function canonicalTopLevelFilePath(relativePath: string, extraFilePaths: Map<string, string>) {
285
- const normalized = toPosix(relativePath);
286
- if (normalized.includes("/")) return undefined;
287
- const lower = normalized.toLowerCase();
288
- return TOP_LEVEL_FILE_PATHS.get(lower) ?? extraFilePaths.get(lower);
289
- }
290
-
291
- export function canonicalSnapshotPathForConfig(relativePath: string, extraFilePaths: Map<string, string>) {
292
- return canonicalTopLevelFilePath(relativePath, extraFilePaths) ?? toPosix(relativePath);
293
- }
294
-
295
278
  function isPreferredExtraCandidate(
296
279
  left: { exact: boolean; originalPath: string },
297
280
  right: { exact: boolean; originalPath: string },
@@ -332,38 +315,36 @@ export function scanSnapshot(snapshot: Snapshot) {
332
315
  export function mergeRemotePreservedFiles(
333
316
  local: Snapshot,
334
317
  remote: Snapshot,
335
- config: Pick<SyncConfig, "syncSessions" | "extraFiles">,
318
+ config: Pick<SyncConfig, "syncFiles" | "syncSessions" | "extraFiles">,
336
319
  ) {
337
- const withSessions = config.syncSessions ? local : mergeRemoteSessionFiles(local, remote);
338
- const paths = new Set(withSessions.files.map((file) => file.path));
339
- const pathNames = new Set(withSessions.files.map((file) => file.path.toLowerCase()));
340
- const extraFileNames = new Set(normalizeExtraFiles(config.extraFiles).map((file) => file.toLowerCase()));
341
- const seenRemoteExtraNames = new Set<string>();
342
- const remoteExtras = remote.files.filter((file) => {
320
+ const localPathNames = new Set(local.files.map((file) => file.path.toLowerCase()));
321
+ const configuredExtraFiles = new Set(
322
+ normalizeExtraFiles(config.extraFiles).map((file) => file.toLowerCase()),
323
+ );
324
+ const preservedPathNames = new Set<string>();
325
+ const preserved = remote.files.filter((file) => {
343
326
  const normalized = toPosix(file.path);
344
327
  const lower = normalized.toLowerCase();
345
328
  if (
346
- paths.has(normalized) ||
347
- pathNames.has(lower) ||
329
+ localPathNames.has(lower) ||
330
+ preservedPathNames.has(lower) ||
348
331
  !isSafeSnapshotPath(file.path) ||
349
- normalized.includes("/") ||
350
- TOP_LEVEL_FILE_NAMES.has(lower) ||
351
- RESERVED_TOP_LEVEL_NAMES.has(lower) ||
352
- extraFileNames.has(lower) ||
353
- seenRemoteExtraNames.has(lower)
332
+ isConfiguredSnapshotPath(normalized, config, configuredExtraFiles) ||
333
+ !isPreservableUnmanagedSnapshotPath(normalized)
354
334
  ) {
355
335
  return false;
356
336
  }
357
- seenRemoteExtraNames.add(lower);
337
+ preservedPathNames.add(lower);
358
338
  return true;
359
339
  });
360
- if (remoteExtras.length === 0) return withSessions;
340
+ if (preserved.length === 0) return local;
361
341
  return {
362
- ...withSessions,
342
+ ...local,
363
343
  id: snapshotId(),
364
344
  createdAt: new Date().toISOString(),
365
345
  machine: os.hostname(),
366
- files: [...withSessions.files, ...remoteExtras].sort((left, right) =>
346
+ syncSessions: snapshotIncludesSessions(local) || snapshotIncludesSessions(remote),
347
+ files: [...local.files, ...preserved].sort((left, right) =>
367
348
  left.path.localeCompare(right.path),
368
349
  ),
369
350
  };
@@ -0,0 +1,129 @@
1
+ import path from "node:path";
2
+ import { isDeniedPath, toPosix } from "./paths.js";
3
+
4
+ export const DEFAULT_SYNC_FILES = [
5
+ "settings.json",
6
+ "keybindings.json",
7
+ "models.json",
8
+ "AGENTS.md",
9
+ "APPEND_SYSTEM.md",
10
+ "skills",
11
+ "prompts",
12
+ "themes",
13
+ "extensions",
14
+ ] as const;
15
+
16
+ export type BuiltInSyncFile = (typeof DEFAULT_SYNC_FILES)[number];
17
+
18
+ const BUILT_IN_BY_LOWER = new Map<string, BuiltInSyncFile>(
19
+ DEFAULT_SYNC_FILES.map((fileName) => [fileName.toLowerCase(), fileName]),
20
+ );
21
+ const TOP_LEVEL_FILE_PATHS = new Map<string, string>(
22
+ DEFAULT_SYNC_FILES.filter((fileName) => fileName.includes(".")).map((fileName) => [
23
+ fileName.toLowerCase(),
24
+ fileName,
25
+ ]),
26
+ );
27
+ const TOP_LEVEL_FILE_NAMES = new Set<string>(TOP_LEVEL_FILE_PATHS.keys());
28
+ const TOP_LEVEL_DIRS = new Set<string>(
29
+ DEFAULT_SYNC_FILES.filter((fileName) => !fileName.includes(".")),
30
+ );
31
+ const RESERVED_TOP_LEVEL_NAMES = new Set<string>([...TOP_LEVEL_DIRS, "sessions"]);
32
+
33
+ export function normalizeSyncFiles(value: unknown): BuiltInSyncFile[] {
34
+ if (value === undefined) return [...DEFAULT_SYNC_FILES];
35
+ if (!Array.isArray(value)) throw new Error("syncFiles must be an array of built-in file names.");
36
+
37
+ const result: BuiltInSyncFile[] = [];
38
+ const seen = new Set<string>();
39
+ for (const item of value) {
40
+ if (typeof item !== "string") throw new Error("syncFiles items must be strings.");
41
+ const canonical = BUILT_IN_BY_LOWER.get(item.trim().toLowerCase());
42
+ if (!canonical) throw new Error(`Unknown syncFiles item: ${item}`);
43
+ const lower = canonical.toLowerCase();
44
+ if (seen.has(lower)) continue;
45
+ seen.add(lower);
46
+ result.push(canonical);
47
+ }
48
+ return result;
49
+ }
50
+
51
+ export function normalizeExtraFiles(value: unknown) {
52
+ if (!Array.isArray(value)) return [];
53
+ const seen = new Set<string>();
54
+ return value
55
+ .filter((item): item is string => typeof item === "string")
56
+ .map((item) => item.trim())
57
+ .filter((item) => {
58
+ const lower = item.toLowerCase();
59
+ if (
60
+ item === "" ||
61
+ item === "." ||
62
+ item === ".." ||
63
+ item.includes("/") ||
64
+ item.includes("\\") ||
65
+ TOP_LEVEL_FILE_NAMES.has(lower) ||
66
+ isDeniedPath(item) ||
67
+ RESERVED_TOP_LEVEL_NAMES.has(lower) ||
68
+ seen.has(lower)
69
+ ) {
70
+ return false;
71
+ }
72
+ seen.add(lower);
73
+ return true;
74
+ });
75
+ }
76
+
77
+ export function extraFilePathsByLower(value: unknown) {
78
+ return new Map(normalizeExtraFiles(value).map((fileName) => [fileName.toLowerCase(), fileName]));
79
+ }
80
+
81
+ export function selectedSyncFileSet(value: unknown) {
82
+ return new Set(normalizeSyncFiles(value));
83
+ }
84
+
85
+ export function isConfiguredSnapshotPath(
86
+ relativePath: string,
87
+ config: { syncFiles?: unknown; syncSessions: boolean },
88
+ extraFiles: Set<string>,
89
+ ) {
90
+ const normalized = toPosix(relativePath);
91
+ if (normalized.startsWith("sessions/")) return config.syncSessions;
92
+ const selected = selectedSyncFileSet(config.syncFiles);
93
+ if (!normalized.includes("/")) {
94
+ const lower = normalized.toLowerCase();
95
+ const builtIn = BUILT_IN_BY_LOWER.get(lower);
96
+ return builtIn ? selected.has(builtIn) && !TOP_LEVEL_DIRS.has(builtIn) : extraFiles.has(lower);
97
+ }
98
+ const topLevel = normalized.slice(0, normalized.indexOf("/"));
99
+ return selected.has(topLevel as BuiltInSyncFile) && TOP_LEVEL_DIRS.has(topLevel);
100
+ }
101
+
102
+ export function canonicalSnapshotPathForConfig(
103
+ relativePath: string,
104
+ extraFilePaths: Map<string, string>,
105
+ ) {
106
+ const normalized = toPosix(relativePath);
107
+ if (normalized.includes("/")) return normalized;
108
+ const lower = normalized.toLowerCase();
109
+ return TOP_LEVEL_FILE_PATHS.get(lower) ?? extraFilePaths.get(lower) ?? normalized;
110
+ }
111
+
112
+ export function isPreservableUnmanagedSnapshotPath(relativePath: string) {
113
+ const normalized = toPosix(relativePath);
114
+ if (!normalized || isDeniedPath(normalized)) return false;
115
+ if (normalized.startsWith("sessions/")) return normalized.endsWith(".jsonl");
116
+ if (!normalized.includes("/")) {
117
+ const lower = normalized.toLowerCase();
118
+ return TOP_LEVEL_FILE_NAMES.has(lower) || !RESERVED_TOP_LEVEL_NAMES.has(lower);
119
+ }
120
+ return TOP_LEVEL_DIRS.has(normalized.slice(0, normalized.indexOf("/")));
121
+ }
122
+
123
+ export function isSafeExtraFileName(fileName: string) {
124
+ return normalizeExtraFiles([fileName]).length === 1;
125
+ }
126
+
127
+ export function isBuiltInTopLevelFile(fileName: string) {
128
+ return TOP_LEVEL_FILE_NAMES.has(path.posix.basename(fileName).toLowerCase());
129
+ }
@@ -0,0 +1,167 @@
1
+ import { toPosix } from "./paths.js";
2
+ import {
3
+ canonicalSnapshotPathForConfig,
4
+ filterSnapshotForConfigPolicy,
5
+ isConfiguredSnapshotPath,
6
+ isSessionPath,
7
+ } from "./snapshot.js";
8
+ import { extraFilePathsByLower, normalizeExtraFiles, normalizeSyncFiles } from "./sync-policy.js";
9
+ import type { LatestPointer, RemoteObject, Snapshot, SyncConfig, SyncState } from "./types.js";
10
+
11
+ export function hasLocalChanges(local: Snapshot, state: SyncState, config: SyncConfig) {
12
+ return !sameHashes(fileHashMap(local), stateHashMapForConfig(state, config));
13
+ }
14
+
15
+ export function remoteChangedSinceState(
16
+ latest: RemoteObject<LatestPointer>,
17
+ state: SyncState,
18
+ config: SyncConfig,
19
+ ) {
20
+ if (latest.missing) return Boolean(state.lastAppliedSnapshot);
21
+ if (latest.value?.snapshot !== state.lastAppliedSnapshot) return true;
22
+ if (syncFilesChanged(state, config) || extraFilesChanged(state, config)) return true;
23
+ return config.syncSessions && state.syncSessions !== true && latest.value?.syncSessions === true;
24
+ }
25
+
26
+ export function hasRemoteChanges(
27
+ remote: Snapshot,
28
+ state: SyncState,
29
+ config: SyncConfig,
30
+ ignoredPaths = new Set<string>(),
31
+ ) {
32
+ if (remote.id === state.lastAppliedSnapshot && !syncPolicyChanged(state, config)) return false;
33
+ return !snapshotHashesMatchState(
34
+ filterSnapshotForConfigPolicy(remote, config),
35
+ state,
36
+ config,
37
+ ignoredPaths,
38
+ );
39
+ }
40
+
41
+ export function sameHashes(left: Record<string, string>, right: Record<string, string>) {
42
+ const keys = new Set([...Object.keys(left), ...Object.keys(right)]);
43
+ for (const key of keys) {
44
+ if (left[key] !== right[key]) return false;
45
+ }
46
+ return true;
47
+ }
48
+
49
+ export function fileHashMap(snapshot: Snapshot) {
50
+ return Object.fromEntries(snapshot.files.map((file) => [file.path, file.sha256]));
51
+ }
52
+
53
+ function stateHashMapForConfig(
54
+ state: SyncState,
55
+ config: Pick<SyncConfig, "syncFiles" | "syncSessions" | "extraFiles">,
56
+ ) {
57
+ const extraFilePaths = extraFilePathsByLower(config.extraFiles);
58
+ const extraFiles = new Set(extraFilePaths.keys());
59
+ return Object.fromEntries(
60
+ Object.entries(state.lastFileHashes)
61
+ .filter(([filePath]) => isConfiguredSnapshotPath(filePath, config, extraFiles))
62
+ .map(([filePath, hash]) => [canonicalSnapshotPathForConfig(filePath, extraFilePaths), hash]),
63
+ );
64
+ }
65
+
66
+ export function snapshotHashesMatchState(
67
+ snapshot: Snapshot,
68
+ state: SyncState,
69
+ config: Pick<SyncConfig, "syncFiles" | "syncSessions" | "extraFiles">,
70
+ ignoredPaths = new Set<string>(),
71
+ ) {
72
+ return sameHashes(
73
+ withoutHashPaths(fileHashMap(snapshot), ignoredPaths),
74
+ withoutHashPaths(stateHashMapForConfig(state, config), ignoredPaths),
75
+ );
76
+ }
77
+
78
+ export function snapshotsMatch(left: Snapshot, right: Snapshot) {
79
+ return (
80
+ left.syncSessions === right.syncSessions && sameHashes(fileHashMap(left), fileHashMap(right))
81
+ );
82
+ }
83
+
84
+ function withoutHashPaths(hashes: Record<string, string>, ignoredPaths: Set<string>) {
85
+ if (ignoredPaths.size === 0) return hashes;
86
+ return Object.fromEntries(
87
+ Object.entries(hashes).filter(([filePath]) => !ignoredPaths.has(toPosix(filePath))),
88
+ );
89
+ }
90
+
91
+ export function syncPolicyChanged(
92
+ state: SyncState,
93
+ config: Pick<SyncConfig, "syncFiles" | "syncSessions" | "extraFiles">,
94
+ ) {
95
+ return (
96
+ syncFilesChanged(state, config) ||
97
+ (state.syncSessions ?? false) !== config.syncSessions ||
98
+ extraFilesChanged(state, config)
99
+ );
100
+ }
101
+
102
+ export function shouldRefreshSyncedState(
103
+ remote: Snapshot,
104
+ state: SyncState,
105
+ config: Pick<SyncConfig, "syncFiles" | "syncSessions" | "extraFiles">,
106
+ ) {
107
+ return remote.id !== state.lastAppliedSnapshot || syncPolicyChanged(state, config);
108
+ }
109
+
110
+ function syncFilesChanged(state: SyncState, config: Pick<SyncConfig, "syncFiles">) {
111
+ return !sameStringSet(normalizeSyncFiles(state.syncFiles), normalizeSyncFiles(config.syncFiles));
112
+ }
113
+
114
+ function extraFilesChanged(state: SyncState, config: Pick<SyncConfig, "extraFiles">) {
115
+ return !sameStringSet(
116
+ normalizeExtraFiles(state.extraFiles),
117
+ normalizeExtraFiles(config.extraFiles),
118
+ );
119
+ }
120
+
121
+ function sameStringSet(left: string[], right: string[]) {
122
+ const leftSet = new Set(left);
123
+ const rightSet = new Set(right);
124
+ if (leftSet.size !== rightSet.size) return false;
125
+ return [...leftSet].every((item) => rightSet.has(item));
126
+ }
127
+
128
+ export function settingsHashMap(snapshot: Snapshot) {
129
+ return Object.fromEntries(
130
+ snapshot.files
131
+ .filter((file) => !isSessionPath(file.path))
132
+ .map((file) => [file.path, file.sha256]),
133
+ );
134
+ }
135
+
136
+ export function sessionHashMap(snapshot: Snapshot) {
137
+ return Object.fromEntries(
138
+ snapshot.files
139
+ .filter((file) => isSessionPath(file.path))
140
+ .map((file) => [file.path, file.sha256]),
141
+ );
142
+ }
143
+
144
+ export function settingsHashMapFromState(state: SyncState) {
145
+ return Object.fromEntries(
146
+ Object.entries(state.lastFileHashes).filter(([filePath]) => !isSessionPath(filePath)),
147
+ );
148
+ }
149
+
150
+ export function settingsHashesMatchState(remote: Snapshot, state: SyncState) {
151
+ return sameHashes(settingsHashMap(remote), settingsHashMapFromState(state));
152
+ }
153
+
154
+ export function canPullRemoteSettingsOnFirstSync(local: Snapshot, remote: Snapshot) {
155
+ const remoteSettings = settingsHashMap(remote);
156
+ return Object.entries(settingsHashMap(local)).every(
157
+ ([filePath, hash]) => remoteSettings[filePath] === hash,
158
+ );
159
+ }
160
+
161
+ export function canPullRemoteSessionsOnFirstSync(local: Snapshot, remote: Snapshot) {
162
+ const localSessions = sessionHashMap(local);
163
+ const remoteSessions = sessionHashMap(remote);
164
+ return Object.entries(localSessions).every(
165
+ ([filePath, hash]) => remoteSessions[filePath] === hash,
166
+ );
167
+ }