@1agh/maude 2.0.3 → 2.0.6

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 (242) hide show
  1. package/README.md +6 -5
  2. package/apps/hub/src/project-transactions/kernel.mjs +10 -1
  3. package/apps/studio/access/filters.ts +12 -5
  4. package/apps/studio/access/known-canvases.ts +24 -1
  5. package/apps/studio/access/refused-inputs.ts +89 -0
  6. package/apps/studio/access/request.ts +0 -1
  7. package/apps/studio/acp/adapter-stderr.ts +43 -13
  8. package/apps/studio/acp/background-tasks.ts +155 -0
  9. package/apps/studio/acp/bridge.ts +44 -5
  10. package/apps/studio/acp/connected-fact.ts +15 -4
  11. package/apps/studio/acp/index.ts +14 -5
  12. package/apps/studio/actions/agents.ts +10 -0
  13. package/apps/studio/actions/defs/app.ts +12 -0
  14. package/apps/studio/actions/defs/view.ts +12 -0
  15. package/apps/studio/actions.manifest.json +25 -1
  16. package/apps/studio/actions.parity.json +13 -1
  17. package/apps/studio/annotations/ui/element-node.tsx +139 -54
  18. package/apps/studio/annotations/ui/lod.tsx +1 -1
  19. package/apps/studio/annotations/ui/text-style.ts +1 -1
  20. package/apps/studio/annotations-layer.tsx +0 -12
  21. package/apps/studio/api.ts +73 -24
  22. package/apps/studio/artboard-live-gate.tsx +267 -0
  23. package/apps/studio/artboard-live.ts +336 -0
  24. package/apps/studio/artboard-webgl.ts +668 -0
  25. package/apps/studio/bin/_agent-browser-bin.mjs +8 -0
  26. package/apps/studio/bin/_agent-browser-safe.mjs +2 -1
  27. package/apps/studio/bin/_canvas-reasons.mjs +34 -0
  28. package/apps/studio/bin/_ds-check-live.mjs +37 -0
  29. package/apps/studio/bin/_ds-check.mjs +4 -1
  30. package/apps/studio/bin/_ds-prove-judge.mjs +167 -0
  31. package/apps/studio/bin/_ds-prove-playwright.mjs +183 -105
  32. package/apps/studio/bin/_import-asset.mjs +2 -1
  33. package/apps/studio/bin/_perf-probe.mjs +2 -1
  34. package/apps/studio/bin/_png-playwright.mjs +10 -1
  35. package/apps/studio/bin/_pw-launch.mjs +14 -0
  36. package/apps/studio/bin/_studio-verb.mjs +24 -3
  37. package/apps/studio/bin/_thumb-blank.mjs +70 -0
  38. package/apps/studio/bin/_thumb-cover.mjs +35 -0
  39. package/apps/studio/bin/_thumbs-playwright.mjs +93 -13
  40. package/apps/studio/bin/ds-check.sh +5 -1
  41. package/apps/studio/bin/perf.sh +11 -1
  42. package/apps/studio/bin/server-up.sh +7 -2
  43. package/apps/studio/bin/smoke.sh +19 -0
  44. package/apps/studio/boot-progress.ts +31 -0
  45. package/apps/studio/canvas-build-sandbox.ts +275 -42
  46. package/apps/studio/canvas-build-worker.ts +10 -0
  47. package/apps/studio/canvas-create.ts +24 -0
  48. package/apps/studio/canvas-lib.tsx +131 -12
  49. package/apps/studio/canvas-module-cache.ts +93 -0
  50. package/apps/studio/canvas-source/errors.ts +39 -0
  51. package/apps/studio/canvas-source/waiting.ts +290 -0
  52. package/apps/studio/client/actions/shell-key-handlers.js +5 -0
  53. package/apps/studio/client/app.jsx +19 -9
  54. package/apps/studio/client/canvas-url.js +13 -0
  55. package/apps/studio/client/github.js +23 -3
  56. package/apps/studio/client/home/HomeAccounts.jsx +262 -0
  57. package/apps/studio/client/home/Shelves.jsx +4 -3
  58. package/apps/studio/client/home/home-accounts-model.js +121 -0
  59. package/apps/studio/client/home/home-api.js +36 -19
  60. package/apps/studio/client/home/home-app.jsx +141 -40
  61. package/apps/studio/client/home/home-model.js +172 -6
  62. package/apps/studio/client/home/home.css +169 -5
  63. package/apps/studio/client/home/shell-mode.js +8 -1
  64. package/apps/studio/client/hooks/use-git-actions.jsx +2 -1
  65. package/apps/studio/client/hooks/use-project-data.jsx +2 -0
  66. package/apps/studio/client/hooks/use-shell-core.jsx +3 -0
  67. package/apps/studio/client/hooks/use-tabs.jsx +64 -35
  68. package/apps/studio/client/index-loader.ts +23 -3
  69. package/apps/studio/client/panels/ChatPanel.jsx +49 -57
  70. package/apps/studio/client/panels/CreateProject.jsx +19 -5
  71. package/apps/studio/client/panels/DiffView.jsx +314 -248
  72. package/apps/studio/client/panels/TeamProjects.jsx +119 -69
  73. package/apps/studio/client/panels/acp-runtime.js +25 -0
  74. package/apps/studio/client/panels/caps-cache.js +46 -0
  75. package/apps/studio/client/panels/chat-finish.js +66 -0
  76. package/apps/studio/client/shell/frame-cap.js +53 -0
  77. package/apps/studio/client/shell/viewport.jsx +45 -8
  78. package/apps/studio/client/styles/_index.css +2 -0
  79. package/apps/studio/client/tree/tree.jsx +48 -21
  80. package/apps/studio/client/v2/access/access-client.js +2 -0
  81. package/apps/studio/client/v2/access/access-host.jsx +12 -1
  82. package/apps/studio/client/v2/access/trashed-banner.jsx +6 -3
  83. package/apps/studio/client/v2/ai/Advanced.jsx +12 -0
  84. package/apps/studio/client/v2/ai/BackgroundStrip.jsx +120 -0
  85. package/apps/studio/client/v2/ai/Composer.jsx +27 -10
  86. package/apps/studio/client/v2/ai/StoppedDivider.jsx +2 -1
  87. package/apps/studio/client/v2/ai/ai-run.css +87 -3
  88. package/apps/studio/client/v2/ai/background-model.js +113 -0
  89. package/apps/studio/client/v2/ai/needs-you-store.js +2 -1
  90. package/apps/studio/client/v2/ai/run-api.js +7 -3
  91. package/apps/studio/client/v2/ai/runs-store.js +2 -1
  92. package/apps/studio/client/v2/annotations/vote-store.js +11 -5
  93. package/apps/studio/client/v2/assets/asset-state-lane.js +6 -3
  94. package/apps/studio/client/v2/assets/assets-bus.js +10 -8
  95. package/apps/studio/client/v2/assets/browse-view.jsx +2 -0
  96. package/apps/studio/client/v2/assets/ds-library-view.jsx +47 -0
  97. package/apps/studio/client/v2/assets/spotlight-model.js +75 -0
  98. package/apps/studio/client/v2/assets/spotlight.css +153 -0
  99. package/apps/studio/client/v2/assets/spotlight.jsx +199 -0
  100. package/apps/studio/client/v2/assets/tile-menu.jsx +3 -1
  101. package/apps/studio/client/v2/assets/tile.jsx +3 -0
  102. package/apps/studio/client/v2/assets/use-assets.js +2 -1
  103. package/apps/studio/client/v2/canvases/canvases-panel.jsx +13 -2
  104. package/apps/studio/client/v2/canvases/covers.js +11 -5
  105. package/apps/studio/client/v2/canvases/folder-ask.js +139 -0
  106. package/apps/studio/client/v2/canvases/folder-dialogs.jsx +150 -0
  107. package/apps/studio/client/v2/canvases/trash-canvas.js +10 -2
  108. package/apps/studio/client/v2/canvases/trash-queue.js +152 -0
  109. package/apps/studio/client/v2/canvases/use-canvas-index.js +42 -18
  110. package/apps/studio/client/v2/canvases/use-pins.js +2 -1
  111. package/apps/studio/client/v2/code/canvas-error-card.jsx +70 -5
  112. package/apps/studio/client/v2/code/canvas-imports-card.jsx +15 -14
  113. package/apps/studio/client/v2/code/canvas-imports-words.js +44 -0
  114. package/apps/studio/client/v2/code/canvas-waiting-card.jsx +128 -0
  115. package/apps/studio/client/v2/code/canvas-waiting.js +164 -0
  116. package/apps/studio/client/v2/code/code-bus.js +5 -0
  117. package/apps/studio/client/v2/code/code.css +67 -0
  118. package/apps/studio/client/v2/code/use-canvas-source.js +19 -4
  119. package/apps/studio/client/v2/create/NameDialog.jsx +4 -2
  120. package/apps/studio/client/v2/create/name-dialog.css +10 -0
  121. package/apps/studio/client/v2/ds/advanced-host.jsx +11 -23
  122. package/apps/studio/client/v2/ds/ds-refresh.js +161 -0
  123. package/apps/studio/client/v2/ds/library-host.jsx +45 -45
  124. package/apps/studio/client/v2/ds/make-host.jsx +13 -12
  125. package/apps/studio/client/v2/ds/off-system-host.jsx +18 -16
  126. package/apps/studio/client/v2/ds/review-host.jsx +7 -1
  127. package/apps/studio/client/v2/ds/truth-host.jsx +15 -14
  128. package/apps/studio/client/v2/ds/upgrade-host.jsx +26 -10
  129. package/apps/studio/client/v2/empty/arriving.jsx +29 -12
  130. package/apps/studio/client/v2/empty/empty-host.jsx +8 -2
  131. package/apps/studio/client/v2/empty/not-local.jsx +31 -1
  132. package/apps/studio/client/v2/empty/use-empty-canvas.js +10 -1
  133. package/apps/studio/client/v2/export/last-finished.js +2 -1
  134. package/apps/studio/client/v2/history/branch-actions.js +13 -9
  135. package/apps/studio/client/v2/history/git-advanced.jsx +8 -1
  136. package/apps/studio/client/v2/history/git-model.js +21 -5
  137. package/apps/studio/client/v2/history/git-parts.jsx +76 -43
  138. package/apps/studio/client/v2/history/history.css +6 -0
  139. package/apps/studio/client/v2/left-panel/assets-panel.jsx +87 -10
  140. package/apps/studio/client/v2/left-panel/use-trash.js +2 -1
  141. package/apps/studio/client/v2/onboarding/AdvancedSheets.jsx +100 -33
  142. package/apps/studio/client/v2/onboarding/HubSheet.jsx +51 -83
  143. package/apps/studio/client/v2/onboarding/onboarding-model.js +10 -6
  144. package/apps/studio/client/v2/presence/faces.jsx +42 -14
  145. package/apps/studio/client/v2/present/order-notes-artboards.js +2 -1
  146. package/apps/studio/client/v2/search/search-recent.js +3 -1
  147. package/apps/studio/client/v2/search/search-remote.js +6 -4
  148. package/apps/studio/client/v2/settings/connections-model.js +1 -1
  149. package/apps/studio/client/v2/shell/account-card.jsx +10 -4
  150. package/apps/studio/client/v2/shell/canvas-crash-guard.js +137 -0
  151. package/apps/studio/client/v2/shell/github-signin-host.jsx +42 -0
  152. package/apps/studio/client/v2/shell/menu-runner.js +12 -0
  153. package/apps/studio/client/v2/shell/occlusion-insets.js +14 -2
  154. package/apps/studio/client/v2/shell/project-account-card.jsx +203 -0
  155. package/apps/studio/client/v2/shell/project-account-model.js +182 -0
  156. package/apps/studio/client/v2/shell/recovery-store.js +2 -1
  157. package/apps/studio/client/v2/shell/served-routes.js +120 -0
  158. package/apps/studio/client/v2/shell/share-cluster-host.jsx +139 -11
  159. package/apps/studio/client/v2/shell/share-cluster.css +58 -0
  160. package/apps/studio/client/v2/shell/share-cluster.jsx +26 -7
  161. package/apps/studio/client/v2/shell/shell-activity.js +19 -0
  162. package/apps/studio/client/v2/shell/team-switch.js +94 -0
  163. package/apps/studio/client/v2/shell/too-heavy-card.jsx +38 -0
  164. package/apps/studio/client/v2/shell/use-account.js +8 -1
  165. package/apps/studio/client/v2/shell/use-occluded-insets.js +3 -0
  166. package/apps/studio/client/v2/sync-states/conflict-card.jsx +7 -2
  167. package/apps/studio/client/v2/undo/use-undo-projection.jsx +3 -1
  168. package/apps/studio/cloud/endpoints.ts +23 -1
  169. package/apps/studio/collab/persistence.ts +9 -1
  170. package/apps/studio/collab/registry.ts +32 -0
  171. package/apps/studio/collab/room.ts +8 -0
  172. package/apps/studio/config-reload-wire.ts +53 -0
  173. package/apps/studio/dist/client.bundle.js +1098 -1102
  174. package/apps/studio/dist/styles.css +1 -1
  175. package/apps/studio/ds/board/specimen.tsx +12 -1
  176. package/apps/studio/ds/canvas-cli.ts +58 -2
  177. package/apps/studio/ds/check.ts +26 -1
  178. package/apps/studio/ds/cli.ts +7 -3
  179. package/apps/studio/ds/convert.ts +34 -6
  180. package/apps/studio/ds/ds-verb.ts +13 -0
  181. package/apps/studio/ds/post-build.ts +38 -0
  182. package/apps/studio/ds/upgrade-apply.ts +27 -5
  183. package/apps/studio/ds/upgrade-cli.ts +22 -2
  184. package/apps/studio/ds/upgrade-prove.ts +198 -15
  185. package/apps/studio/ds/upgrade-stage.ts +4 -1
  186. package/apps/studio/editing/minimap-context.tsx +3 -3
  187. package/apps/studio/editing/page-activity.ts +251 -0
  188. package/apps/studio/editing/rotate-handle.tsx +9 -5
  189. package/apps/studio/editing/sticky-name.tsx +4 -4
  190. package/apps/studio/history/restore-artboard-action.ts +40 -0
  191. package/apps/studio/history/trash.ts +6 -0
  192. package/apps/studio/home/accounts.ts +87 -0
  193. package/apps/studio/home/hub.ts +77 -175
  194. package/apps/studio/home/index-reader.ts +24 -2
  195. package/apps/studio/home/routes.ts +66 -8
  196. package/apps/studio/http.ts +131 -68
  197. package/apps/studio/index-flight.ts +40 -0
  198. package/apps/studio/managed-projects.ts +52 -24
  199. package/apps/studio/migrate/hub.ts +7 -1
  200. package/apps/studio/routes/canvas-source.ts +44 -2
  201. package/apps/studio/routes/ds-convert.ts +18 -0
  202. package/apps/studio/routes/history.ts +28 -5
  203. package/apps/studio/routes/hub-signin.ts +115 -0
  204. package/apps/studio/routes/index.ts +4 -0
  205. package/apps/studio/routes/table.ts +7 -0
  206. package/apps/studio/routes/trash.ts +19 -1
  207. package/apps/studio/server.ts +19 -10
  208. package/apps/studio/sync/accepted-cold-start.ts +54 -16
  209. package/apps/studio/sync/accepted-link.ts +9 -4
  210. package/apps/studio/sync/asset-push.ts +146 -23
  211. package/apps/studio/sync/asset-state.ts +3 -2
  212. package/apps/studio/sync/code-module-consent.ts +74 -0
  213. package/apps/studio/sync/file-ledger.ts +35 -0
  214. package/apps/studio/sync/file-membership.ts +28 -3
  215. package/apps/studio/sync/file-plane.ts +483 -86
  216. package/apps/studio/sync/file-pull.ts +304 -33
  217. package/apps/studio/sync/hub-browser-signin.ts +377 -0
  218. package/apps/studio/sync/hub-link.ts +1 -2
  219. package/apps/studio/sync/hub-text-undo.ts +30 -10
  220. package/apps/studio/sync/index.ts +381 -161
  221. package/apps/studio/sync/journal-client.ts +94 -5
  222. package/apps/studio/sync/pending-deletes.ts +100 -0
  223. package/apps/studio/sync/projection.ts +121 -14
  224. package/apps/studio/sync/pull-pacer.ts +160 -0
  225. package/apps/studio/sync/pull-priority.ts +119 -0
  226. package/apps/studio/sync/seed-progress.ts +58 -3
  227. package/apps/studio/sync/status.ts +3 -1
  228. package/apps/studio/sync/transaction-client.ts +71 -6
  229. package/apps/studio/sync/workspace-signin.ts +6 -256
  230. package/apps/studio/sync/writer-registry.ts +5 -1
  231. package/apps/studio/thumbs/service.ts +12 -2
  232. package/apps/studio/thumbs/validate.ts +7 -0
  233. package/apps/studio/ui-prefs-migrate.ts +4 -0
  234. package/apps/studio/video-comp.tsx +23 -0
  235. package/apps/studio/video-links-overlay.tsx +3 -2
  236. package/apps/studio/whats-new.json +126 -0
  237. package/cli/lib/bash-guard.mjs +13 -1
  238. package/cli/lib/design-help.gen.mjs +1 -1
  239. package/cli/lib/design-hook.mjs +20 -0
  240. package/cli/lib/studio-locate.mjs +35 -10
  241. package/cli/lib/workspace-plan.mjs +14 -0
  242. package/package.json +8 -8
@@ -44,7 +44,17 @@
44
44
  // construction), so a converged project costs one manifest fetch per poll.
45
45
 
46
46
  import { createHash } from 'node:crypto';
47
- import { existsSync, mkdirSync, readFileSync, renameSync, statSync, writeFileSync } from 'node:fs';
47
+ import {
48
+ closeSync,
49
+ existsSync,
50
+ mkdirSync,
51
+ openSync,
52
+ readSync,
53
+ renameSync,
54
+ rmSync,
55
+ statSync,
56
+ writeSync,
57
+ } from 'node:fs';
48
58
  import path from 'node:path';
49
59
 
50
60
  import {
@@ -98,6 +108,133 @@ export interface FilePullResult {
98
108
  /** F6 — the pass stopped on the aggregate byte budget rather than the count
99
109
  * cap. The remainder is the next pass's work. */
100
110
  budgetExhausted?: true;
111
+ /** How many offered files this pass wanted to move (before the count cap,
112
+ * without the held ones). `wanted > 0 && pulled.length === 0` is a pass that
113
+ * made no progress — what the gate backs off on (2.0.5). */
114
+ wanted?: number;
115
+ /** Files whose landing failed before and that wait out a backoff instead of
116
+ * being asked for again every pass — each with the reason, in words. */
117
+ held?: { rel: string; reason: string; attempts: number }[];
118
+ /** The manifest could not be read at all (hub unreachable or refusing). */
119
+ unreachable?: true;
120
+ }
121
+
122
+ /** First retry after a failed landing; doubles per failure up to the cap. */
123
+ const FAIL_BACKOFF_BASE_MS = 60_000;
124
+ const FAIL_BACKOFF_MAX_MS = 60 * 60_000;
125
+
126
+ /**
127
+ * What this runtime remembers about files whose landing failed (2.0.5, rule b).
128
+ *
129
+ * The legacy lane has no ledger, so without this a file that cannot land — a
130
+ * write the disk refuses, a hub that answers 500 for it — was asked for on
131
+ * EVERY pass, at the head of the batch, forever. Keyed by (path, hash): a new
132
+ * version of the file is a new offer and is tried at once.
133
+ */
134
+ export interface PullFailureMemory {
135
+ noteFailed(rel: string, sha256: string, reason: string, now: number): void;
136
+ noteLanded(rel: string): void;
137
+ /** The hold on this exact offer, while it is still waiting out its backoff. */
138
+ heldNow(rel: string, sha256: string, now: number): { reason: string; attempts: number } | null;
139
+ }
140
+
141
+ export function createPullFailureMemory(
142
+ opts: { baseMs?: number; maxMs?: number } = {}
143
+ ): PullFailureMemory {
144
+ const base = opts.baseMs ?? FAIL_BACKOFF_BASE_MS;
145
+ const max = opts.maxMs ?? FAIL_BACKOFF_MAX_MS;
146
+ const rows = new Map<
147
+ string,
148
+ { sha256: string; attempts: number; nextAt: number; reason: string }
149
+ >();
150
+ return {
151
+ noteFailed(rel, sha256, reason, now) {
152
+ const prev = rows.get(rel);
153
+ const attempts = prev && prev.sha256 === sha256 ? prev.attempts + 1 : 1;
154
+ const delay = Math.min(max, base * 2 ** (attempts - 1));
155
+ rows.delete(rel);
156
+ rows.set(rel, { sha256, attempts, nextAt: now + delay, reason });
157
+ // Bounded: a hostile manifest cannot grow this without limit.
158
+ if (rows.size > 20_000) {
159
+ const oldest = rows.keys().next().value;
160
+ if (oldest !== undefined) rows.delete(oldest);
161
+ }
162
+ },
163
+ noteLanded(rel) {
164
+ rows.delete(rel);
165
+ },
166
+ heldNow(rel, sha256, now) {
167
+ const r = rows.get(rel);
168
+ if (!r) return null;
169
+ if (r.sha256 !== sha256) {
170
+ rows.delete(rel); // a new version of the file is a new offer
171
+ return null;
172
+ }
173
+ return now < r.nextAt ? { reason: r.reason, attempts: r.attempts } : null;
174
+ },
175
+ };
176
+ }
177
+
178
+ /**
179
+ * One legacy pass at a time, and a pass that made no progress backs off
180
+ * (2.0.5, rule c).
181
+ *
182
+ * The 20 s poll used to start a new `pullFiles` on every tick, whatever the
183
+ * last one was still doing. Against a slow hub one pass of 200 files outlives
184
+ * many ticks, and every new pass chose the SAME 200 (the in-flight ones were
185
+ * not on disk yet): N passes downloading the same bodies at once, each holding
186
+ * them in memory. `run` answers null when it did not start a pass.
187
+ */
188
+ export function createLegacyPullGate(
189
+ opts: { now?: () => number; baseMs?: number; maxMs?: number } = {}
190
+ ) {
191
+ const now = opts.now ?? Date.now;
192
+ const base = opts.baseMs ?? 20_000;
193
+ const max = opts.maxMs ?? 10 * 60_000;
194
+ let running = false;
195
+ let streak = 0;
196
+ let notBefore = 0;
197
+ return {
198
+ async run(pass: () => Promise<FilePullResult>): Promise<FilePullResult | null> {
199
+ if (running || now() < notBefore) return null;
200
+ running = true;
201
+ try {
202
+ const r = await pass();
203
+ const stalled = r.unreachable === true || ((r.wanted ?? 0) > 0 && r.pulled.length === 0);
204
+ if (stalled) {
205
+ streak += 1;
206
+ notBefore = now() + Math.min(max, base * 2 ** (streak - 1));
207
+ } else {
208
+ streak = 0;
209
+ notBefore = 0;
210
+ }
211
+ return r;
212
+ } finally {
213
+ running = false;
214
+ }
215
+ },
216
+ /** ms until the next pass may start (0 = now). */
217
+ waitMs(): number {
218
+ return Math.max(0, notBefore - now());
219
+ },
220
+ };
221
+ }
222
+
223
+ /** SHA-256 of a file read in fixed chunks — never the whole file in memory. */
224
+ export function hashFileSync(abs: string): string {
225
+ const h = createHash('sha256');
226
+ const fd = openSync(abs, 'r');
227
+ try {
228
+ const buf = Buffer.allocUnsafe(1024 * 1024);
229
+ for (;;) {
230
+ const n = readSync(fd, buf, 0, buf.length, null);
231
+ if (n <= 0) break;
232
+ h.update(n === buf.length ? buf : buf.subarray(0, n));
233
+ }
234
+ } finally {
235
+ closeSync(fd);
236
+ }
237
+ return h.digest('hex');
101
238
  }
102
239
 
103
240
  /**
@@ -160,6 +297,9 @@ export interface PullFilesOptions {
160
297
  now?: () => number;
161
298
  /** F6 — override the aggregate per-pass byte ceiling (tests). */
162
299
  maxPassBytes?: number;
300
+ /** Kept by the caller across passes: a failed landing waits out a backoff
301
+ * rather than being asked for again on the next pass (rule b). */
302
+ failures?: PullFailureMemory;
163
303
  }
164
304
 
165
305
  /**
@@ -177,7 +317,12 @@ export async function pullFiles(opts: PullFilesOptions): Promise<FilePullResult>
177
317
  const out: FilePullResult = { pulled: [], skipped: 0, conflicts: [], dropped: [], failed: [] };
178
318
 
179
319
  const manifest = await fetchFileManifest(hubUrl, opts.token(), fetchImpl);
180
- if (manifest === null) return out;
320
+ if (manifest === null) {
321
+ out.unreachable = true;
322
+ return out;
323
+ }
324
+ const failures = opts.failures;
325
+ const held: NonNullable<FilePullResult['held']> = [];
181
326
 
182
327
  // Decide the whole pass from the manifest first, then move bytes: a local
183
328
  // win costs no fetch at all, and the loud cap counts only real transfers.
@@ -186,6 +331,13 @@ export async function pullFiles(opts: PullFilesOptions): Promise<FilePullResult>
186
331
  hasFile: (r: string) => existsSync(path.join(designRoot, r)),
187
332
  };
188
333
  const wanted: { entry: RemoteFileEntry; abs: string; conflict: FilePullConflict | null }[] = [];
334
+ /** A failed landing waiting out its backoff is reported, not re-fetched. */
335
+ const isHeld = (rel: string, entry: RemoteFileEntry): boolean => {
336
+ const h = failures?.heldNow(rel, entry.sha256, now());
337
+ if (!h) return false;
338
+ held.push({ rel, reason: h.reason, attempts: h.attempts });
339
+ return true;
340
+ };
189
341
 
190
342
  for (const entry of manifest) {
191
343
  const rel = entry.path;
@@ -231,28 +383,40 @@ export async function pullFiles(opts: PullFilesOptions): Promise<FilePullResult>
231
383
  drop(out, log, rel, 'a non-file sits at this path locally');
232
384
  continue;
233
385
  }
234
- let localSha: string;
235
- try {
236
- localSha = createHash('sha256').update(readFileSync(abs)).digest('hex');
237
- } catch (err) {
238
- out.failed.push({ rel, reason: `unreadable local copy: ${(err as Error).message}` });
239
- continue;
386
+ // LWW by mtime. The local file wins ties: overwriting a person's disk
387
+ // needs the remote to be STRICTLY newer. Decided before any byte is read:
388
+ // a local win costs no hash at all (every pass used to read every local
389
+ // file whole — gigabytes for an asset-heavy project, on each poll).
390
+ const hubNewer = entry.mtimeMs > st.mtimeMs;
391
+ let same: boolean;
392
+ if (st.size !== entry.size) {
393
+ same = false; // different lengths cannot be equal bytes
394
+ } else {
395
+ try {
396
+ same = hashFileSync(abs) === entry.sha256;
397
+ } catch (err) {
398
+ out.failed.push({ rel, reason: `unreadable local copy: ${(err as Error).message}` });
399
+ continue;
400
+ }
240
401
  }
241
- if (localSha === entry.sha256) {
402
+ if (same) {
242
403
  out.skipped += 1;
404
+ failures?.noteLanded(rel);
243
405
  continue;
244
406
  }
245
- // LWW by mtime. The local file wins ties: overwriting a person's disk
246
- // needs the remote to be STRICTLY newer.
247
- if (!(entry.mtimeMs > st.mtimeMs)) {
407
+ if (!hubNewer) {
248
408
  out.conflicts.push({ rel, winner: 'local' });
249
409
  continue;
250
410
  }
411
+ if (isHeld(rel, entry)) continue;
251
412
  wanted.push({ entry, abs, conflict: { rel, winner: 'hub' } });
252
413
  continue;
253
414
  }
415
+ if (isHeld(rel, entry)) continue;
254
416
  wanted.push({ entry, abs, conflict: null });
255
417
  }
418
+ out.wanted = wanted.length;
419
+ if (held.length > 0) out.held = held;
256
420
 
257
421
  const batch = wanted.slice(0, MAX_FILES_PER_PASS);
258
422
  if (batch.length < wanted.length) {
@@ -272,6 +436,12 @@ export async function pullFiles(opts: PullFilesOptions): Promise<FilePullResult>
272
436
  ...(opts.maxPassBytes !== undefined ? { maxBytes: opts.maxPassBytes } : {}),
273
437
  });
274
438
 
439
+ /** A failure that is the file's, not the pass's: remembered, so it waits. */
440
+ const fail = (entry: RemoteFileEntry, reason: string): void => {
441
+ out.failed.push({ rel: entry.path, reason });
442
+ failures?.noteFailed(entry.path, entry.sha256, reason, now());
443
+ };
444
+
275
445
  for (const { entry, abs, conflict } of batch) {
276
446
  const rel = entry.path;
277
447
  if (budget.exhausted()) break;
@@ -282,6 +452,16 @@ export async function pullFiles(opts: PullFilesOptions): Promise<FilePullResult>
282
452
  out.budgetExhausted = true;
283
453
  break;
284
454
  }
455
+ // Staged beside the target and renamed, so a reader never sees a
456
+ // half-written file. Created (with its folders) before the request: a
457
+ // folder that cannot be made costs no transfer.
458
+ const tmpAbs = `${abs}.part`;
459
+ try {
460
+ mkdirSync(path.dirname(abs), { recursive: true });
461
+ } catch (err) {
462
+ fail(entry, `could not create the folder for it here: ${(err as Error).message}`);
463
+ continue;
464
+ }
285
465
  try {
286
466
  const res = await fetchImpl(
287
467
  `${base}/_project-file/${rel.split('/').map(encodeURIComponent).join('/')}`,
@@ -291,29 +471,51 @@ export async function pullFiles(opts: PullFilesOptions): Promise<FilePullResult>
291
471
  }
292
472
  );
293
473
  if (!res.ok) {
294
- out.failed.push({ rel, reason: `HTTP ${res.status}` });
474
+ fail(entry, `the project's server answered HTTP ${res.status}`);
295
475
  continue;
296
476
  }
297
- const body = new Uint8Array(await res.arrayBuffer());
298
- if (body.byteLength > MAX_PULL_BYTES) {
299
- out.failed.push({ rel, reason: `implausible size (${body.byteLength} B)` });
477
+ // STREAMED to the staging file (2.0.5, rule d): one chunk in memory at a
478
+ // time, hashed and counted as it goes. Holding the body whole, times 200,
479
+ // times the passes that overlapped, was the field's 2–4 GB.
480
+ const limit = Math.min(MAX_PULL_BYTES, entry.size + budget.remaining());
481
+ const got = await streamToFile(res, tmpAbs, limit);
482
+ if (!got.ok) {
483
+ discard(tmpAbs);
484
+ if (got.over) {
485
+ if (got.size > MAX_PULL_BYTES) {
486
+ fail(entry, `implausible size (more than ${MAX_PULL_BYTES} B)`);
487
+ continue;
488
+ }
489
+ // Charge the overrun: the manifest's size was a hint, the wire is the
490
+ // fact. A body larger than declared must not slip past the ceiling.
491
+ out.failed.push({
492
+ rel,
493
+ reason: `pass byte budget reached (declared ${entry.size} B, sent more than ${limit} B)`,
494
+ });
495
+ out.budgetExhausted = true;
496
+ break;
497
+ }
498
+ fail(entry, got.reason);
300
499
  continue;
301
500
  }
302
- // Charge the overrun: the manifest's size was a hint, the wire is the
303
- // fact. A body larger than declared must not slip past the ceiling.
304
- if (body.byteLength > entry.size && !budget.take(body.byteLength - entry.size)) {
501
+ if (got.size > entry.size && !budget.take(got.size - entry.size)) {
502
+ discard(tmpAbs);
305
503
  out.failed.push({
306
504
  rel,
307
- reason: `pass byte budget reached (declared ${entry.size} B, sent ${body.byteLength} B)`,
505
+ reason: `pass byte budget reached (declared ${entry.size} B, sent ${got.size} B)`,
308
506
  });
309
507
  out.budgetExhausted = true;
310
508
  break;
311
509
  }
312
510
  // The manifest named a hash; the bytes must BE that hash. A mismatch is
313
- // usually a write racing the poll — a free retry, never a landing.
314
- const gotSha = createHash('sha256').update(body).digest('hex');
315
- if (gotSha !== entry.sha256) {
316
- out.failed.push({ rel, reason: 'content hash mismatch (racing a write?)' });
511
+ // usually a write racing the poll — a retry after a short wait, never a
512
+ // landing.
513
+ if (got.sha256 !== entry.sha256) {
514
+ discard(tmpAbs);
515
+ fail(
516
+ entry,
517
+ 'content hash mismatch — the bytes did not match what the project listed (it may be changing)'
518
+ );
317
519
  continue;
318
520
  }
319
521
  if (conflict) {
@@ -326,34 +528,103 @@ export async function pullFiles(opts: PullFilesOptions): Promise<FilePullResult>
326
528
  if (trashedTo === null) {
327
529
  // Could not park the loser ⇒ the overwrite is refused. Never
328
530
  // silent loss — the local copy stays, the conflict is reported.
531
+ discard(tmpAbs);
329
532
  out.conflicts.push({ rel, winner: 'local' });
330
533
  continue;
331
534
  }
332
535
  conflict.trashedTo = trashedTo;
333
536
  out.conflicts.push(conflict);
334
537
  }
335
- mkdirSync(path.dirname(abs), { recursive: true });
336
- // Write beside the target and rename, so a reader never sees a
337
- // half-written file.
338
- const tmpAbs = `${abs}.part`;
339
- writeFileSync(tmpAbs, body);
340
538
  renameSync(tmpAbs, abs);
539
+ // RULE (a): pulled means ON DISK WITH THAT HASH — checked, not assumed.
540
+ // A rename that "succeeded" into a place nothing can read back is a
541
+ // failure the person must hear about, not a delivery.
542
+ let landedSha: string | null = null;
543
+ try {
544
+ landedSha = statSync(abs).size === got.size ? hashFileSync(abs) : null;
545
+ } catch {
546
+ landedSha = null;
547
+ }
548
+ if (landedSha !== entry.sha256) {
549
+ fail(entry, 'it was written but could not be read back from this disk');
550
+ continue;
551
+ }
552
+ failures?.noteLanded(rel);
341
553
  out.pulled.push(rel);
342
554
  } catch (err) {
343
- out.failed.push({ rel, reason: (err as Error).message });
555
+ discard(tmpAbs);
556
+ fail(entry, (err as Error).message);
344
557
  }
345
558
  }
346
559
 
347
- if (out.pulled.length > 0 || out.conflicts.length > 0) {
560
+ if (out.pulled.length > 0 || out.conflicts.length > 0 || out.failed.length > 0) {
348
561
  log.log(
349
562
  `[sync/files] pulled ${out.pulled.length} project file(s) down (${out.skipped} already here, ${out.conflicts.length} conflict(s), ${out.failed.length} failed${
350
- out.budgetExhausted ? ', pass byte budget reached — resuming next pass' : ''
351
- }).`
563
+ held.length > 0 ? `, ${held.length} waiting to retry after a failure` : ''
564
+ }${out.budgetExhausted ? ', pass byte budget reached — resuming next pass' : ''}).`
352
565
  );
353
566
  }
354
567
  return out;
355
568
  }
356
569
 
570
+ /** Remove a staging file, never throwing (its folder may be the problem). */
571
+ function discard(abs: string): void {
572
+ try {
573
+ rmSync(abs, { force: true });
574
+ } catch {
575
+ /* nothing to clean */
576
+ }
577
+ }
578
+
579
+ /**
580
+ * Stream a response body into `abs`, hashing as it goes. Stops (and reports
581
+ * `over`) the moment the body passes `limit` bytes, so neither memory nor the
582
+ * disk ever holds more than the pass allowed.
583
+ */
584
+ async function streamToFile(
585
+ res: Response,
586
+ abs: string,
587
+ limit: number
588
+ ): Promise<
589
+ | { ok: true; size: number; sha256: string }
590
+ | { ok: false; over: true; size: number }
591
+ | { ok: false; over?: false; reason: string }
592
+ > {
593
+ const h = createHash('sha256');
594
+ let size = 0;
595
+ let fd: number | null = null;
596
+ try {
597
+ fd = openSync(abs, 'w');
598
+ if (res.body) {
599
+ const reader = res.body.getReader();
600
+ for (;;) {
601
+ const { done, value } = await reader.read();
602
+ if (done) break;
603
+ if (!value || value.byteLength === 0) continue;
604
+ size += value.byteLength;
605
+ if (size > limit) {
606
+ await reader.cancel().catch(() => {});
607
+ return { ok: false, over: true, size };
608
+ }
609
+ h.update(value);
610
+ let off = 0;
611
+ while (off < value.byteLength) off += writeSync(fd, value, off, value.byteLength - off);
612
+ }
613
+ }
614
+ return { ok: true, size, sha256: h.digest('hex') };
615
+ } catch (err) {
616
+ return { ok: false, reason: `the download did not finish: ${(err as Error).message}` };
617
+ } finally {
618
+ if (fd !== null) {
619
+ try {
620
+ closeSync(fd);
621
+ } catch {
622
+ /* already closed */
623
+ }
624
+ }
625
+ }
626
+ }
627
+
357
628
  /** One warn per (reason-class) flood is the caller's job; here every drop is
358
629
  * one line — drops are refusals of UNTRUSTED input and deserve a trace. */
359
630
  function drop(