pushwork 2.0.0-a.sub.1 → 2.0.0-preview

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 (251) hide show
  1. package/dist/branches.d.ts +19 -0
  2. package/dist/branches.d.ts.map +1 -0
  3. package/dist/branches.js +111 -0
  4. package/dist/branches.js.map +1 -0
  5. package/dist/cli.d.ts +1 -1
  6. package/dist/cli.d.ts.map +1 -1
  7. package/dist/cli.js +238 -272
  8. package/dist/cli.js.map +1 -1
  9. package/dist/config.d.ts +17 -0
  10. package/dist/config.d.ts.map +1 -0
  11. package/dist/config.js +84 -0
  12. package/dist/config.js.map +1 -0
  13. package/dist/fs-tree.d.ts +6 -0
  14. package/dist/fs-tree.d.ts.map +1 -0
  15. package/dist/fs-tree.js +99 -0
  16. package/dist/fs-tree.js.map +1 -0
  17. package/dist/ignore.d.ts +6 -0
  18. package/dist/ignore.d.ts.map +1 -0
  19. package/dist/ignore.js +74 -0
  20. package/dist/ignore.js.map +1 -0
  21. package/dist/index.d.ts +8 -4
  22. package/dist/index.d.ts.map +1 -1
  23. package/dist/index.js +34 -4
  24. package/dist/index.js.map +1 -1
  25. package/dist/log.d.ts +3 -0
  26. package/dist/log.d.ts.map +1 -0
  27. package/dist/log.js +14 -0
  28. package/dist/log.js.map +1 -0
  29. package/dist/pushwork.d.ts +115 -0
  30. package/dist/pushwork.d.ts.map +1 -0
  31. package/dist/pushwork.js +918 -0
  32. package/dist/pushwork.js.map +1 -0
  33. package/dist/repo.d.ts +14 -0
  34. package/dist/repo.d.ts.map +1 -0
  35. package/dist/repo.js +60 -0
  36. package/dist/repo.js.map +1 -0
  37. package/dist/shapes/custom.d.ts +3 -0
  38. package/dist/shapes/custom.d.ts.map +1 -0
  39. package/dist/shapes/custom.js +57 -0
  40. package/dist/shapes/custom.js.map +1 -0
  41. package/dist/shapes/file.d.ts +20 -0
  42. package/dist/shapes/file.d.ts.map +1 -0
  43. package/dist/shapes/file.js +140 -0
  44. package/dist/shapes/file.js.map +1 -0
  45. package/dist/shapes/index.d.ts +10 -0
  46. package/dist/shapes/index.d.ts.map +1 -0
  47. package/dist/shapes/index.js +35 -0
  48. package/dist/shapes/index.js.map +1 -0
  49. package/dist/shapes/patchwork-folder.d.ts +3 -0
  50. package/dist/shapes/patchwork-folder.d.ts.map +1 -0
  51. package/dist/shapes/patchwork-folder.js +160 -0
  52. package/dist/shapes/patchwork-folder.js.map +1 -0
  53. package/dist/shapes/types.d.ts +37 -0
  54. package/dist/shapes/types.d.ts.map +1 -0
  55. package/dist/shapes/types.js +52 -0
  56. package/dist/shapes/types.js.map +1 -0
  57. package/dist/shapes/vfs.d.ts +3 -0
  58. package/dist/shapes/vfs.d.ts.map +1 -0
  59. package/dist/shapes/vfs.js +88 -0
  60. package/dist/shapes/vfs.js.map +1 -0
  61. package/dist/stash.d.ts +23 -0
  62. package/dist/stash.d.ts.map +1 -0
  63. package/dist/stash.js +118 -0
  64. package/dist/stash.js.map +1 -0
  65. package/flake.lock +128 -0
  66. package/flake.nix +66 -0
  67. package/package.json +15 -48
  68. package/patches/@automerge__automerge-repo@2.6.0-subduction.15.patch +26 -0
  69. package/pnpm-workspace.yaml +5 -0
  70. package/src/branches.ts +93 -0
  71. package/src/cli.ts +258 -408
  72. package/src/config.ts +64 -0
  73. package/src/fs-tree.ts +70 -0
  74. package/src/ignore.ts +33 -0
  75. package/src/index.ts +38 -4
  76. package/src/log.ts +8 -0
  77. package/src/pushwork.ts +1055 -0
  78. package/src/repo.ts +76 -0
  79. package/src/shapes/custom.ts +29 -0
  80. package/src/shapes/file.ts +115 -0
  81. package/src/shapes/index.ts +19 -0
  82. package/src/shapes/patchwork-folder.ts +156 -0
  83. package/src/shapes/types.ts +79 -0
  84. package/src/shapes/vfs.ts +93 -0
  85. package/src/stash.ts +106 -0
  86. package/test/integration/branches.test.ts +389 -0
  87. package/test/integration/pushwork.test.ts +547 -0
  88. package/test/setup.ts +29 -0
  89. package/test/unit/doc-shape.test.ts +612 -0
  90. package/tsconfig.json +2 -3
  91. package/vitest.config.ts +14 -0
  92. package/ARCHITECTURE-ACCORDING-TO-CLAUDE.md +0 -248
  93. package/CLAUDE.md +0 -141
  94. package/README.md +0 -221
  95. package/babel.config.js +0 -5
  96. package/dist/cli/commands.d.ts +0 -71
  97. package/dist/cli/commands.d.ts.map +0 -1
  98. package/dist/cli/commands.js +0 -794
  99. package/dist/cli/commands.js.map +0 -1
  100. package/dist/cli/index.d.ts +0 -2
  101. package/dist/cli/index.d.ts.map +0 -1
  102. package/dist/cli/index.js +0 -19
  103. package/dist/cli/index.js.map +0 -1
  104. package/dist/commands.d.ts +0 -61
  105. package/dist/commands.d.ts.map +0 -1
  106. package/dist/commands.js +0 -861
  107. package/dist/commands.js.map +0 -1
  108. package/dist/config/index.d.ts +0 -71
  109. package/dist/config/index.d.ts.map +0 -1
  110. package/dist/config/index.js +0 -314
  111. package/dist/config/index.js.map +0 -1
  112. package/dist/core/change-detection.d.ts +0 -80
  113. package/dist/core/change-detection.d.ts.map +0 -1
  114. package/dist/core/change-detection.js +0 -523
  115. package/dist/core/change-detection.js.map +0 -1
  116. package/dist/core/config.d.ts +0 -81
  117. package/dist/core/config.d.ts.map +0 -1
  118. package/dist/core/config.js +0 -258
  119. package/dist/core/config.js.map +0 -1
  120. package/dist/core/index.d.ts +0 -6
  121. package/dist/core/index.d.ts.map +0 -1
  122. package/dist/core/index.js +0 -6
  123. package/dist/core/index.js.map +0 -1
  124. package/dist/core/move-detection.d.ts +0 -34
  125. package/dist/core/move-detection.d.ts.map +0 -1
  126. package/dist/core/move-detection.js +0 -121
  127. package/dist/core/move-detection.js.map +0 -1
  128. package/dist/core/snapshot.d.ts +0 -105
  129. package/dist/core/snapshot.d.ts.map +0 -1
  130. package/dist/core/snapshot.js +0 -217
  131. package/dist/core/snapshot.js.map +0 -1
  132. package/dist/core/sync-engine.d.ts +0 -157
  133. package/dist/core/sync-engine.d.ts.map +0 -1
  134. package/dist/core/sync-engine.js +0 -1379
  135. package/dist/core/sync-engine.js.map +0 -1
  136. package/dist/types/config.d.ts +0 -99
  137. package/dist/types/config.d.ts.map +0 -1
  138. package/dist/types/config.js +0 -5
  139. package/dist/types/config.js.map +0 -1
  140. package/dist/types/documents.d.ts +0 -88
  141. package/dist/types/documents.d.ts.map +0 -1
  142. package/dist/types/documents.js +0 -20
  143. package/dist/types/documents.js.map +0 -1
  144. package/dist/types/index.d.ts +0 -4
  145. package/dist/types/index.d.ts.map +0 -1
  146. package/dist/types/index.js +0 -4
  147. package/dist/types/index.js.map +0 -1
  148. package/dist/types/snapshot.d.ts +0 -64
  149. package/dist/types/snapshot.d.ts.map +0 -1
  150. package/dist/types/snapshot.js +0 -2
  151. package/dist/types/snapshot.js.map +0 -1
  152. package/dist/utils/content-similarity.d.ts +0 -53
  153. package/dist/utils/content-similarity.d.ts.map +0 -1
  154. package/dist/utils/content-similarity.js +0 -155
  155. package/dist/utils/content-similarity.js.map +0 -1
  156. package/dist/utils/content.d.ts +0 -10
  157. package/dist/utils/content.d.ts.map +0 -1
  158. package/dist/utils/content.js +0 -31
  159. package/dist/utils/content.js.map +0 -1
  160. package/dist/utils/directory.d.ts +0 -24
  161. package/dist/utils/directory.d.ts.map +0 -1
  162. package/dist/utils/directory.js +0 -52
  163. package/dist/utils/directory.js.map +0 -1
  164. package/dist/utils/fs.d.ts +0 -74
  165. package/dist/utils/fs.d.ts.map +0 -1
  166. package/dist/utils/fs.js +0 -248
  167. package/dist/utils/fs.js.map +0 -1
  168. package/dist/utils/index.d.ts +0 -5
  169. package/dist/utils/index.d.ts.map +0 -1
  170. package/dist/utils/index.js +0 -5
  171. package/dist/utils/index.js.map +0 -1
  172. package/dist/utils/mime-types.d.ts +0 -13
  173. package/dist/utils/mime-types.d.ts.map +0 -1
  174. package/dist/utils/mime-types.js +0 -209
  175. package/dist/utils/mime-types.js.map +0 -1
  176. package/dist/utils/network-sync.d.ts +0 -36
  177. package/dist/utils/network-sync.d.ts.map +0 -1
  178. package/dist/utils/network-sync.js +0 -250
  179. package/dist/utils/network-sync.js.map +0 -1
  180. package/dist/utils/node-polyfills.d.ts +0 -9
  181. package/dist/utils/node-polyfills.d.ts.map +0 -1
  182. package/dist/utils/node-polyfills.js +0 -9
  183. package/dist/utils/node-polyfills.js.map +0 -1
  184. package/dist/utils/output.d.ts +0 -129
  185. package/dist/utils/output.d.ts.map +0 -1
  186. package/dist/utils/output.js +0 -368
  187. package/dist/utils/output.js.map +0 -1
  188. package/dist/utils/repo-factory.d.ts +0 -13
  189. package/dist/utils/repo-factory.d.ts.map +0 -1
  190. package/dist/utils/repo-factory.js +0 -46
  191. package/dist/utils/repo-factory.js.map +0 -1
  192. package/dist/utils/string-similarity.d.ts +0 -14
  193. package/dist/utils/string-similarity.d.ts.map +0 -1
  194. package/dist/utils/string-similarity.js +0 -39
  195. package/dist/utils/string-similarity.js.map +0 -1
  196. package/dist/utils/text-diff.d.ts +0 -37
  197. package/dist/utils/text-diff.d.ts.map +0 -1
  198. package/dist/utils/text-diff.js +0 -93
  199. package/dist/utils/text-diff.js.map +0 -1
  200. package/dist/utils/trace.d.ts +0 -19
  201. package/dist/utils/trace.d.ts.map +0 -1
  202. package/dist/utils/trace.js +0 -63
  203. package/dist/utils/trace.js.map +0 -1
  204. package/src/commands.ts +0 -1134
  205. package/src/core/change-detection.ts +0 -712
  206. package/src/core/config.ts +0 -313
  207. package/src/core/index.ts +0 -5
  208. package/src/core/move-detection.ts +0 -169
  209. package/src/core/snapshot.ts +0 -275
  210. package/src/core/sync-engine.ts +0 -1795
  211. package/src/types/config.ts +0 -111
  212. package/src/types/documents.ts +0 -91
  213. package/src/types/index.ts +0 -3
  214. package/src/types/snapshot.ts +0 -67
  215. package/src/utils/content.ts +0 -34
  216. package/src/utils/directory.ts +0 -73
  217. package/src/utils/fs.ts +0 -297
  218. package/src/utils/index.ts +0 -4
  219. package/src/utils/mime-types.ts +0 -244
  220. package/src/utils/network-sync.ts +0 -319
  221. package/src/utils/node-polyfills.ts +0 -8
  222. package/src/utils/output.ts +0 -450
  223. package/src/utils/repo-factory.ts +0 -73
  224. package/src/utils/string-similarity.ts +0 -54
  225. package/src/utils/text-diff.ts +0 -101
  226. package/src/utils/trace.ts +0 -70
  227. package/test/integration/README.md +0 -328
  228. package/test/integration/clone-test.sh +0 -310
  229. package/test/integration/conflict-resolution-test.sh +0 -309
  230. package/test/integration/debug-both-nested.sh +0 -74
  231. package/test/integration/debug-concurrent-nested.sh +0 -87
  232. package/test/integration/debug-nested.sh +0 -73
  233. package/test/integration/deletion-behavior-test.sh +0 -487
  234. package/test/integration/deletion-sync-test-simple.sh +0 -193
  235. package/test/integration/deletion-sync-test.sh +0 -297
  236. package/test/integration/exclude-patterns.test.ts +0 -144
  237. package/test/integration/full-integration-test.sh +0 -363
  238. package/test/integration/fuzzer.test.ts +0 -818
  239. package/test/integration/in-memory-sync.test.ts +0 -830
  240. package/test/integration/init-sync.test.ts +0 -89
  241. package/test/integration/manual-sync-test.sh +0 -84
  242. package/test/integration/sync-deletion.test.ts +0 -280
  243. package/test/integration/sync-flow.test.ts +0 -291
  244. package/test/jest.setup.ts +0 -34
  245. package/test/run-tests.sh +0 -225
  246. package/test/unit/deletion-behavior.test.ts +0 -249
  247. package/test/unit/enhanced-mime-detection.test.ts +0 -244
  248. package/test/unit/snapshot.test.ts +0 -404
  249. package/test/unit/sync-convergence.test.ts +0 -298
  250. package/test/unit/sync-timing.test.ts +0 -134
  251. package/test/unit/utils.test.ts +0 -366
@@ -1,244 +0,0 @@
1
- import * as mimeTypes from "mime-types";
2
-
3
- /**
4
- * Custom MIME type definitions for developer files
5
- * Based on patchwork-cli's approach
6
- */
7
- const CUSTOM_MIME_TYPES: Record<string, string> = {
8
- // TypeScript files - override the incorrect video/mp2t detection
9
- ".ts": "text/typescript",
10
- ".tsx": "text/tsx",
11
-
12
- // Config file formats
13
- ".json": "application/json",
14
- ".yaml": "text/yaml",
15
- ".yml": "text/yaml",
16
- ".toml": "application/toml",
17
- ".ini": "text/plain",
18
- ".conf": "text/plain",
19
- ".config": "text/plain",
20
-
21
- // Vue.js single file components
22
- ".vue": "text/vue",
23
-
24
- // Modern CSS preprocessors
25
- ".scss": "text/scss",
26
- ".sass": "text/sass",
27
- ".less": "text/less",
28
- ".styl": "text/stylus",
29
-
30
- // Modern JavaScript variants
31
- ".mjs": "application/javascript",
32
- ".cjs": "application/javascript",
33
-
34
- // React JSX
35
- ".jsx": "text/jsx",
36
-
37
- // Svelte components
38
- ".svelte": "text/svelte",
39
-
40
- // Web assembly
41
- ".wasm": "application/wasm",
42
-
43
- // Other common dev files
44
- ".d.ts": "text/typescript",
45
- ".map": "application/json", // Source maps
46
- ".env": "text/plain",
47
- ".gitignore": "text/plain",
48
- ".gitattributes": "text/plain",
49
- ".editorconfig": "text/plain",
50
- ".prettierrc": "application/json",
51
- ".eslintrc": "application/json",
52
- ".babelrc": "application/json",
53
-
54
- // Documentation formats
55
- ".mdx": "text/markdown",
56
- ".rst": "text/x-rst",
57
-
58
- // Docker files
59
- Dockerfile: "text/plain",
60
- ".dockerignore": "text/plain",
61
-
62
- // Package manager files
63
- "package.json": "application/json",
64
- "package-lock.json": "application/json",
65
- "yarn.lock": "text/plain",
66
- "pnpm-lock.yaml": "text/yaml",
67
- "composer.json": "application/json",
68
- Pipfile: "text/plain",
69
- "requirements.txt": "text/plain",
70
-
71
- // Build tool configs
72
- "webpack.config.js": "application/javascript",
73
- "vite.config.js": "application/javascript",
74
- "rollup.config.js": "application/javascript",
75
- "tsconfig.json": "application/json",
76
- "jsconfig.json": "application/json",
77
- };
78
-
79
- /**
80
- * File extensions that should always be treated as text
81
- * regardless of MIME type detection
82
- */
83
- const FORCE_TEXT_EXTENSIONS = new Set([
84
- ".ts",
85
- ".tsx",
86
- ".jsx",
87
- ".vue",
88
- ".svelte",
89
- ".scss",
90
- ".sass",
91
- ".less",
92
- ".styl",
93
- ".env",
94
- ".gitignore",
95
- ".gitattributes",
96
- ".editorconfig",
97
- ".d.ts",
98
- ".map",
99
- ".mdx",
100
- ".rst",
101
- ".toml",
102
- ".ini",
103
- ".conf",
104
- ".config",
105
- ".lock",
106
- ]);
107
-
108
- /**
109
- * Get enhanced MIME type for file with custom dev file support
110
- */
111
- export function getEnhancedMimeType(filePath: string): string {
112
- const normalized = normalizePathSeparators(filePath);
113
- const filename = normalized.split("/").pop() || "";
114
- const extension = getFileExtension(normalized);
115
-
116
- // Check custom definitions first (by extension)
117
- if (extension && CUSTOM_MIME_TYPES[extension]) {
118
- return CUSTOM_MIME_TYPES[extension];
119
- }
120
-
121
- // Check custom definitions by full filename
122
- if (CUSTOM_MIME_TYPES[filename]) {
123
- return CUSTOM_MIME_TYPES[filename];
124
- }
125
-
126
- // Fall back to standard mime-types library
127
- const standardMime = mimeTypes.lookup(normalized);
128
- if (standardMime) {
129
- return standardMime;
130
- }
131
-
132
- // Final fallback
133
- return "application/octet-stream";
134
- }
135
-
136
- /**
137
- * Check if file extension should be forced to text type
138
- */
139
- export function shouldForceAsText(filePath: string): boolean {
140
- const extension = getFileExtension(filePath);
141
- return extension ? FORCE_TEXT_EXTENSIONS.has(extension) : false;
142
- }
143
-
144
- /**
145
- * Get file extension including the dot (internal helper)
146
- */
147
- function getFileExtension(filePath: string): string {
148
- const match = filePath.match(/\.[^.]*$/);
149
- return match ? match[0].toLowerCase() : "";
150
- }
151
-
152
- /**
153
- * Normalize path separators to forward slashes for cross-platform consistency
154
- */
155
- function normalizePathSeparators(p: string): string {
156
- return p.replace(/\\/g, "/");
157
- }
158
-
159
- /**
160
- * Enhanced text file detection with developer file support
161
- */
162
- export async function isEnhancedTextFile(filePath: string): Promise<boolean> {
163
- // Force certain extensions to be treated as text
164
- if (shouldForceAsText(filePath)) {
165
- return true;
166
- }
167
-
168
- // Check MIME type
169
- const mimeType = getEnhancedMimeType(filePath);
170
- if (isTextMimeType(mimeType)) {
171
- return true;
172
- }
173
-
174
- // If it's a known binary type (but not the generic fallback), don't fall back to content detection
175
- if (isBinaryMimeType(mimeType) && mimeType !== "application/octet-stream") {
176
- return false;
177
- }
178
-
179
- // For generic octet-stream or unknown types, use content-based detection
180
- return isTextByContent(filePath);
181
- }
182
-
183
- /**
184
- * Check if MIME type indicates text content
185
- */
186
- function isTextMimeType(mimeType: string): boolean {
187
- return (
188
- mimeType.startsWith("text/") ||
189
- mimeType === "application/json" ||
190
- mimeType === "application/xml" ||
191
- mimeType === "application/javascript" ||
192
- mimeType === "application/typescript" ||
193
- mimeType === "application/toml" ||
194
- mimeType.includes("javascript") ||
195
- mimeType.includes("typescript") ||
196
- mimeType.includes("json") ||
197
- mimeType.includes("xml")
198
- );
199
- }
200
-
201
- /**
202
- * Check if MIME type indicates binary content
203
- */
204
- function isBinaryMimeType(mimeType: string): boolean {
205
- return (
206
- mimeType.startsWith("image/") ||
207
- mimeType.startsWith("video/") ||
208
- mimeType.startsWith("audio/") ||
209
- mimeType.startsWith("font/") ||
210
- mimeType === "application/zip" ||
211
- mimeType === "application/pdf" ||
212
- mimeType === "application/octet-stream" ||
213
- mimeType === "application/wasm" ||
214
- mimeType.includes("binary")
215
- );
216
- }
217
-
218
- /**
219
- * Content-based text detection (fallback method)
220
- */
221
- async function isTextByContent(filePath: string): Promise<boolean> {
222
- try {
223
- const fs = await import("fs/promises");
224
-
225
- // Sample first 8KB to detect binary content
226
- const handle = await fs.open(filePath, "r");
227
- const stats = await handle.stat();
228
- const sampleSize = Math.min(8192, stats.size);
229
-
230
- if (sampleSize === 0) {
231
- await handle.close();
232
- return true; // Empty file is text
233
- }
234
-
235
- const buffer = Buffer.alloc(sampleSize);
236
- await handle.read(buffer, 0, sampleSize, 0);
237
- await handle.close();
238
-
239
- // Check for null bytes which indicate binary content
240
- return !buffer.includes(0);
241
- } catch {
242
- return false;
243
- }
244
- }
@@ -1,319 +0,0 @@
1
- import {
2
- DocHandle,
3
- Repo,
4
- AutomergeUrl,
5
- } from "@automerge/automerge-repo";
6
- import { out } from "./output.js";
7
- import { DirectoryDocument } from "../types/index.js";
8
- import { getPlainUrl } from "./directory.js";
9
-
10
- const isDebug = !!process.env.DEBUG;
11
- function debug(...args: any[]) {
12
- if (isDebug) console.error("[pushwork:sync]", ...args);
13
- }
14
-
15
- /**
16
- * Wait for bidirectional sync to stabilize.
17
- * This function waits until document heads stop changing, indicating that
18
- * both outgoing and incoming sync has completed.
19
- *
20
- * With Subduction, sync is handled automatically by the transport layer.
21
- * We poll heads until they stabilize to confirm sync completion.
22
- *
23
- * @param repo - The Automerge repository
24
- * @param rootDirectoryUrl - The root directory URL to start traversal from
25
- * @param options - Configuration options
26
- */
27
- export async function waitForBidirectionalSync(
28
- repo: Repo,
29
- rootDirectoryUrl: AutomergeUrl | undefined,
30
- options: {
31
- timeoutMs?: number;
32
- pollIntervalMs?: number;
33
- stableChecksRequired?: number;
34
- handles?: DocHandle<unknown>[];
35
- } = {},
36
- ): Promise<void> {
37
- const {
38
- timeoutMs = 10000,
39
- pollIntervalMs = 100,
40
- stableChecksRequired = 3,
41
- handles,
42
- } = options;
43
-
44
- if (!rootDirectoryUrl) {
45
- return;
46
- }
47
-
48
- const startTime = Date.now();
49
- let lastSeenHeads = new Map<string, string>();
50
- let stableCount = 0;
51
- let pollCount = 0;
52
- let dynamicTimeoutMs = timeoutMs;
53
-
54
- debug(`waitForBidirectionalSync: starting (timeout=${timeoutMs}ms, stableChecks=${stableChecksRequired}${handles ? `, tracking ${handles.length} handles` : ', full tree scan'})`);
55
-
56
- while (Date.now() - startTime < dynamicTimeoutMs) {
57
- pollCount++;
58
- // Get current heads: use provided handles if available, otherwise full tree scan
59
- const currentHeads = handles
60
- ? getHandleHeads(handles)
61
- : await getAllDocumentHeads(repo, rootDirectoryUrl);
62
-
63
- // After first scan: scale timeout to tree size and reset the clock.
64
- // The first scan is just establishing a baseline — its duration
65
- // shouldn't count against the stability-wait timeout.
66
- if (pollCount === 1) {
67
- const scanDuration = Date.now() - startTime;
68
- dynamicTimeoutMs = Math.max(timeoutMs, 5000 + currentHeads.size * 50) + scanDuration;
69
- debug(`waitForBidirectionalSync: first scan took ${scanDuration}ms, timeout now ${dynamicTimeoutMs}ms for ${currentHeads.size} docs`);
70
- }
71
-
72
- // Check if heads are stable (no changes since last check)
73
- const isStable = headsMapEqual(lastSeenHeads, currentHeads);
74
-
75
- if (isStable) {
76
- stableCount++;
77
- debug(`waitForBidirectionalSync: stable check ${stableCount}/${stableChecksRequired} (${currentHeads.size} docs, poll #${pollCount})`);
78
- if (stableCount >= stableChecksRequired) {
79
- const elapsed = Date.now() - startTime;
80
- debug(`waitForBidirectionalSync: converged in ${elapsed}ms after ${pollCount} polls (${currentHeads.size} docs)`);
81
- out.taskLine(`Bidirectional sync converged (${currentHeads.size} docs, ${elapsed}ms)`);
82
- return; // Converged!
83
- }
84
- } else {
85
- // Find which docs changed
86
- if (lastSeenHeads.size > 0) {
87
- const changedDocs: string[] = [];
88
- for (const [url, heads] of currentHeads) {
89
- if (lastSeenHeads.get(url) !== heads) {
90
- changedDocs.push(url);
91
- }
92
- }
93
- const newDocs = currentHeads.size - lastSeenHeads.size;
94
- if (newDocs > 0) {
95
- debug(`waitForBidirectionalSync: ${newDocs} new docs discovered, ${changedDocs.length} docs changed heads (poll #${pollCount})`);
96
- } else if (changedDocs.length > 0) {
97
- debug(`waitForBidirectionalSync: ${changedDocs.length} docs changed heads: ${changedDocs.slice(0, 5).join(", ")}${changedDocs.length > 5 ? ` ...and ${changedDocs.length - 5} more` : ""} (poll #${pollCount})`);
98
- }
99
- } else {
100
- debug(`waitForBidirectionalSync: initial scan found ${currentHeads.size} docs (poll #${pollCount})`);
101
- }
102
- if (stableCount > 0) {
103
- debug(`waitForBidirectionalSync: heads changed after ${stableCount} stable checks, resetting`);
104
- }
105
- stableCount = 0;
106
- lastSeenHeads = currentHeads;
107
- }
108
-
109
- await new Promise((r) => setTimeout(r, pollIntervalMs));
110
- }
111
-
112
- // Timeout - but don't throw, just log a warning
113
- // The sync may still work, we just couldn't confirm stability
114
- const elapsed = Date.now() - startTime;
115
- debug(`waitForBidirectionalSync: timed out after ${elapsed}ms (${pollCount} polls, ${lastSeenHeads.size} docs tracked, reached ${stableCount}/${stableChecksRequired} stable checks)`);
116
- out.taskLine(`Bidirectional sync timed out after ${(elapsed / 1000).toFixed(1)}s - document heads were still changing after ${pollCount} checks across ${lastSeenHeads.size} docs (reached ${stableCount}/${stableChecksRequired} stability checks). This may mean another peer is actively editing, or the sync server is slow to relay changes. The sync will continue but some remote changes may not be reflected yet.`, true);
117
- }
118
-
119
- /**
120
- * Get heads from a pre-collected set of handles (cheap, synchronous reads).
121
- * Used for post-push stabilization where we already know which documents changed.
122
- */
123
- function getHandleHeads(
124
- handles: DocHandle<unknown>[],
125
- ): Map<string, string> {
126
- const heads = new Map<string, string>();
127
- for (const handle of handles) {
128
- heads.set(getPlainUrl(handle.url), JSON.stringify(handle.heads()));
129
- }
130
- return heads;
131
- }
132
-
133
- /**
134
- * Get all document heads in the directory hierarchy.
135
- * Returns a map of document URL -> serialized heads.
136
- * Uses plain URLs (without heads) to ensure we see current document state.
137
- */
138
- async function getAllDocumentHeads(
139
- repo: Repo,
140
- rootDirectoryUrl: AutomergeUrl,
141
- ): Promise<Map<string, string>> {
142
- const heads = new Map<string, string>();
143
- // Pass URL as-is; collectHeadsRecursive will strip heads
144
- await collectHeadsRecursive(repo, rootDirectoryUrl, heads);
145
- return heads;
146
- }
147
-
148
- /**
149
- * Recursively collect document heads from the directory hierarchy.
150
- * Uses getPlainUrl to strip heads and always see the CURRENT state of documents.
151
- */
152
- async function collectHeadsRecursive(
153
- repo: Repo,
154
- directoryUrl: AutomergeUrl,
155
- heads: Map<string, string>,
156
- ): Promise<void> {
157
- try {
158
- const plainUrl = getPlainUrl(directoryUrl);
159
- const handle = await repo.find<DirectoryDocument>(plainUrl);
160
- const doc = await handle.doc();
161
-
162
- // Record this directory's heads (use plain URL as key for consistency)
163
- heads.set(plainUrl, JSON.stringify(handle.heads()));
164
-
165
- if (!doc || !doc.docs) {
166
- return;
167
- }
168
-
169
- // Process all entries in the directory concurrently
170
- await Promise.all(doc.docs.map(async (entry: { type: string; url: AutomergeUrl; name: string }) => {
171
- if (entry.type === "folder") {
172
- // Recurse into subdirectory (entry.url may have stale heads)
173
- await collectHeadsRecursive(repo, entry.url, heads);
174
- } else if (entry.type === "file") {
175
- // Get file document heads (strip heads from entry.url)
176
- try {
177
- const fileUrl = getPlainUrl(entry.url);
178
- const fileHandle = await repo.find(fileUrl);
179
- heads.set(fileUrl, JSON.stringify(fileHandle.heads()));
180
- } catch {
181
- // File document may not exist yet
182
- }
183
- }
184
- }));
185
- } catch {
186
- // Directory may not exist yet
187
- }
188
- }
189
-
190
- /**
191
- * Compare two heads maps for equality.
192
- */
193
- function headsMapEqual(
194
- a: Map<string, string>,
195
- b: Map<string, string>,
196
- ): boolean {
197
- if (a.size !== b.size) {
198
- return false;
199
- }
200
- for (const [key, value] of a) {
201
- if (b.get(key) !== value) {
202
- return false;
203
- }
204
- }
205
- return true;
206
- }
207
-
208
- /**
209
- * Result of waitForSync — lists which handles failed to sync.
210
- */
211
- export interface SyncWaitResult {
212
- failed: DocHandle<unknown>[];
213
- }
214
-
215
- /** Maximum documents to sync concurrently to avoid flooding the server */
216
- const SYNC_BATCH_SIZE = 10;
217
-
218
- /**
219
- * Wait for documents to sync by polling head stability.
220
- *
221
- * With Subduction, sync is automatic via the transport layer.
222
- * We wait for each handle's heads to stabilize (stop changing),
223
- * which indicates the sync layer has finished processing.
224
- *
225
- * Processes handles in batches to avoid overwhelming the system.
226
- */
227
- export async function waitForSync(
228
- handlesToWaitOn: DocHandle<unknown>[],
229
- timeoutMs: number = 60000,
230
- ): Promise<SyncWaitResult> {
231
- const startTime = Date.now();
232
-
233
- if (handlesToWaitOn.length === 0) {
234
- debug("waitForSync: no documents to sync");
235
- return { failed: [] };
236
- }
237
-
238
- debug(`waitForSync: waiting for ${handlesToWaitOn.length} documents (timeout=${timeoutMs}ms, batchSize=${SYNC_BATCH_SIZE})`);
239
- out.taskLine(`Uploading: waiting for ${handlesToWaitOn.length} documents to sync`);
240
-
241
- // Process in batches to avoid flooding the server
242
- const failed: DocHandle<unknown>[] = [];
243
- let synced = 0;
244
-
245
- for (let i = 0; i < handlesToWaitOn.length; i += SYNC_BATCH_SIZE) {
246
- const batch = handlesToWaitOn.slice(i, i + SYNC_BATCH_SIZE);
247
- const batchNum = Math.floor(i / SYNC_BATCH_SIZE) + 1;
248
- const totalBatches = Math.ceil(handlesToWaitOn.length / SYNC_BATCH_SIZE);
249
-
250
- if (totalBatches > 1) {
251
- debug(`waitForSync: batch ${batchNum}/${totalBatches} (${batch.length} docs)`);
252
- out.update(`Uploading batch ${batchNum}/${totalBatches} (${synced}/${handlesToWaitOn.length} done)`);
253
- }
254
-
255
- const results = await Promise.allSettled(
256
- batch.map(handle => waitForHandleHeadStability(handle, timeoutMs, startTime))
257
- );
258
-
259
- for (const result of results) {
260
- if (result.status === "rejected") {
261
- failed.push(result.reason as DocHandle<unknown>);
262
- } else {
263
- synced++;
264
- }
265
- }
266
- }
267
-
268
- const elapsed = Date.now() - startTime;
269
- if (failed.length > 0) {
270
- debug(`waitForSync: ${failed.length} documents failed after ${elapsed}ms`);
271
- out.taskLine(`Upload: ${synced} synced, ${failed.length} failed after ${(elapsed / 1000).toFixed(1)}s`, true);
272
- } else {
273
- debug(`waitForSync: all ${handlesToWaitOn.length} documents synced in ${elapsed}ms`);
274
- out.taskLine(`All ${handlesToWaitOn.length} documents uploaded to server (${(elapsed / 1000).toFixed(1)}s)`);
275
- }
276
-
277
- return { failed };
278
- }
279
-
280
- /**
281
- * Wait for a single document handle's heads to stabilize.
282
- * Polls the handle's heads and waits until they remain unchanged
283
- * for several consecutive checks, indicating sync completion.
284
- *
285
- * Resolves with the handle on success, rejects with the handle on timeout.
286
- */
287
- function waitForHandleHeadStability(
288
- handle: DocHandle<unknown>,
289
- timeoutMs: number,
290
- startTime: number,
291
- ): Promise<DocHandle<unknown>> {
292
- return new Promise<DocHandle<unknown>>((resolve, reject) => {
293
- let lastHeads = JSON.stringify(handle.heads());
294
- let stableCount = 0;
295
- const stableRequired = 3;
296
-
297
- const pollInterval = setInterval(() => {
298
- const currentHeads = JSON.stringify(handle.heads());
299
- if (currentHeads === lastHeads) {
300
- stableCount++;
301
- if (stableCount >= stableRequired) {
302
- clearInterval(pollInterval);
303
- clearTimeout(timeout);
304
- debug(`waitForSync: ${handle.url}... converged in ${Date.now() - startTime}ms`);
305
- resolve(handle);
306
- }
307
- } else {
308
- stableCount = 0;
309
- lastHeads = currentHeads;
310
- }
311
- }, 100);
312
-
313
- const timeout = setTimeout(() => {
314
- clearInterval(pollInterval);
315
- debug(`waitForSync: ${handle.url}... timed out after ${timeoutMs}ms`);
316
- reject(handle);
317
- }, timeoutMs);
318
- });
319
- }
@@ -1,8 +0,0 @@
1
- /**
2
- * Polyfills for browser APIs required by @automerge/automerge-subduction.
3
- * Must be imported before any subduction code.
4
- *
5
- * The Subduction WASM module uses IndexedDB for key persistence
6
- * (via WebCryptoSigner). In Node.js we provide a fake-indexeddb polyfill.
7
- */
8
- import "fake-indexeddb/auto";