@1agh/maude 0.60.7 → 1.0.3

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 (145) hide show
  1. package/apps/studio/acp/index.ts +1 -0
  2. package/apps/studio/ai-banner.tsx +1 -0
  3. package/apps/studio/annotations-context-toolbar.tsx +3 -1
  4. package/apps/studio/annotations-layer.tsx +33 -16
  5. package/apps/studio/api.ts +108 -18
  6. package/apps/studio/artboard-guides-overlay.tsx +5 -1
  7. package/apps/studio/assets-s3.ts +6 -1
  8. package/apps/studio/bin/_import-figma.mjs +8 -3
  9. package/apps/studio/build.ts +1 -1
  10. package/apps/studio/canvas-artifacts.ts +21 -0
  11. package/apps/studio/canvas-build.ts +14 -9
  12. package/apps/studio/canvas-comment-mount.tsx +27 -30
  13. package/apps/studio/canvas-icons.tsx +1 -1
  14. package/apps/studio/canvas-lib.tsx +94 -5
  15. package/apps/studio/canvas-list-watch.ts +14 -1
  16. package/apps/studio/canvas-shell.tsx +8 -0
  17. package/apps/studio/client/app.jsx +224 -45
  18. package/apps/studio/client/panels/GitPanel.jsx +121 -13
  19. package/apps/studio/client/panels/SettingsPanel.jsx +1 -1
  20. package/apps/studio/client/panels/SyncConsentDialog.jsx +182 -0
  21. package/apps/studio/client/panels/SyncPanel.jsx +595 -1
  22. package/apps/studio/client/styles/3-shell-maude.css +38 -0
  23. package/apps/studio/clip-ops.ts +8 -1
  24. package/apps/studio/cloud/endpoints.ts +265 -3
  25. package/apps/studio/collab/origins.ts +3 -1
  26. package/apps/studio/comments-overlay.tsx +5 -0
  27. package/apps/studio/config.schema.json +24 -0
  28. package/apps/studio/context-menu.tsx +40 -27
  29. package/apps/studio/context.ts +57 -8
  30. package/apps/studio/cursors-overlay.tsx +25 -13
  31. package/apps/studio/dist/client.bundle.js +686 -686
  32. package/apps/studio/dist/comment-mount.js +2 -2
  33. package/apps/studio/dist/styles.css +1 -1
  34. package/apps/studio/exporters/jobs.ts +77 -15
  35. package/apps/studio/exporters/remote.ts +201 -0
  36. package/apps/studio/exporters/video-encode-lib.ts +10 -4
  37. package/apps/studio/figma/to-strokes.ts +11 -9
  38. package/apps/studio/gifenc.d.ts +51 -0
  39. package/apps/studio/git/log-format.ts +88 -0
  40. package/apps/studio/git/safe-rel.ts +96 -0
  41. package/apps/studio/git/service.ts +46 -27
  42. package/apps/studio/hmr-broadcast.ts +10 -0
  43. package/apps/studio/http.ts +384 -6
  44. package/apps/studio/participants-chrome.tsx +1 -0
  45. package/apps/studio/photo-store.ts +7 -0
  46. package/apps/studio/react-augment.d.ts +17 -0
  47. package/apps/studio/runtime-bundle.ts +6 -1
  48. package/apps/studio/server.ts +43 -8
  49. package/apps/studio/sync/agent.ts +70 -82
  50. package/apps/studio/sync/asset-push.ts +28 -71
  51. package/apps/studio/sync/autocommit.ts +106 -5
  52. package/apps/studio/sync/cell-file-events.ts +117 -0
  53. package/apps/studio/sync/cell-pairing.ts +20 -5
  54. package/apps/studio/sync/cell-write-nudge.ts +244 -0
  55. package/apps/studio/sync/codec.ts +155 -3
  56. package/apps/studio/sync/cold-start-apply.ts +211 -0
  57. package/apps/studio/sync/ctl-heal.ts +253 -0
  58. package/apps/studio/sync/ctl-provider.ts +217 -0
  59. package/apps/studio/sync/decide-file.ts +335 -0
  60. package/apps/studio/sync/file-ledger.ts +581 -0
  61. package/apps/studio/sync/file-membership.ts +32 -0
  62. package/apps/studio/sync/file-plane.ts +1400 -0
  63. package/apps/studio/sync/file-pull.ts +41 -4
  64. package/apps/studio/sync/hub-link.ts +16 -1
  65. package/apps/studio/sync/hub-listing.ts +46 -0
  66. package/apps/studio/sync/hubs-config.ts +16 -0
  67. package/apps/studio/sync/index.ts +915 -308
  68. package/apps/studio/sync/journal-client.ts +200 -0
  69. package/apps/studio/sync/migrate-seed.ts +99 -67
  70. package/apps/studio/sync/poke.ts +50 -0
  71. package/apps/studio/sync/projection.ts +13 -0
  72. package/apps/studio/sync/pull-budget.ts +86 -0
  73. package/apps/studio/sync/settings.ts +110 -0
  74. package/apps/studio/sync/status.ts +68 -0
  75. package/apps/studio/sync/trash.ts +243 -0
  76. package/apps/studio/sync/untrusted.ts +30 -10
  77. package/apps/studio/test/_helpers.ts +8 -0
  78. package/apps/studio/test/canvas-build.test.ts +63 -0
  79. package/apps/studio/test/canvas-list-watch.test.ts +17 -0
  80. package/apps/studio/test/canvas-move-api.test.ts +31 -0
  81. package/apps/studio/test/canvas-origin-gate.test.ts +12 -0
  82. package/apps/studio/test/canvas-shell-build-error.test.ts +49 -0
  83. package/apps/studio/test/cloud-history-hardening.test.ts +165 -0
  84. package/apps/studio/test/cloud-history-posture.test.ts +230 -0
  85. package/apps/studio/test/cloud-session-role.test.ts +30 -0
  86. package/apps/studio/test/cloud-shell-surfaces.test.ts +39 -0
  87. package/apps/studio/test/cold-start-apply.test.ts +303 -0
  88. package/apps/studio/test/collab-stress.test.ts +9 -1
  89. package/apps/studio/test/export-lane.test.ts +245 -0
  90. package/apps/studio/test/fixtures/video-comp-fixture.tsx +1 -1
  91. package/apps/studio/test/git-log-format.test.ts +95 -0
  92. package/apps/studio/test/git-safe-rel.test.ts +132 -0
  93. package/apps/studio/test/hmr-broadcast.test.ts +26 -0
  94. package/apps/studio/test/peer-selection-follows-camera.test.tsx +131 -0
  95. package/apps/studio/test/shared-doc-cell-pairing.test.ts +5 -2
  96. package/apps/studio/test/sync-agent.test.ts +78 -0
  97. package/apps/studio/test/sync-asset-push.test.ts +71 -108
  98. package/apps/studio/test/sync-autocommit.test.ts +80 -0
  99. package/apps/studio/test/sync-cell-write-nudge.test.ts +346 -0
  100. package/apps/studio/test/sync-ctl-channel.test.ts +508 -0
  101. package/apps/studio/test/sync-decide-file.test.ts +420 -0
  102. package/apps/studio/test/sync-file-ledger.test.ts +334 -0
  103. package/apps/studio/test/sync-file-membership.test.ts +17 -1
  104. package/apps/studio/test/sync-file-plane.test.ts +976 -0
  105. package/apps/studio/test/sync-hub-listing.test.ts +46 -0
  106. package/apps/studio/test/sync-meta-codec.test.ts +76 -0
  107. package/apps/studio/test/sync-move-retirement.test.ts +231 -0
  108. package/apps/studio/test/sync-panel-surface.test.ts +20 -0
  109. package/apps/studio/test/sync-path-pull.test.ts +67 -1
  110. package/apps/studio/test/sync-pull-budget.test.ts +169 -0
  111. package/apps/studio/test/sync-seed-defers-to-hub.test.ts +83 -0
  112. package/apps/studio/test/sync-settings-routes.test.ts +195 -0
  113. package/apps/studio/test/sync-settings.test.ts +151 -0
  114. package/apps/studio/test/sync-status.test.ts +69 -0
  115. package/apps/studio/test/sync-trash.test.ts +132 -0
  116. package/apps/studio/test/workspace-containment.test.ts +45 -9
  117. package/apps/studio/tsconfig.json +9 -10
  118. package/apps/studio/use-annotation-resize.tsx +14 -3
  119. package/apps/studio/use-collab.tsx +3 -1
  120. package/apps/studio/whats-new.json +90 -0
  121. package/apps/studio/workspace-mode.ts +110 -62
  122. package/apps/studio/ws.ts +22 -1
  123. package/cli/bin/claude-design-server.mjs +19 -0
  124. package/cli/commands/design.mjs +25 -6
  125. package/cli/commands/hub-workspace.mjs +243 -22
  126. package/cli/commands/hub-workspace.test.mjs +171 -0
  127. package/cli/commands/hub.mjs +71 -1
  128. package/cli/lib/design-link.mjs +186 -1
  129. package/cli/lib/design-ownership.mjs +330 -0
  130. package/cli/lib/design-ownership.test.mjs +329 -0
  131. package/cli/lib/hubs-config.mjs +21 -0
  132. package/cli/lib/hubs-config.test.mjs +47 -1
  133. package/cli/lib/workspace-plan.mjs +298 -5
  134. package/cli/lib/workspace-plan.test.mjs +215 -1
  135. package/package.json +10 -10
  136. package/plugins/design/templates/_shell.html +43 -2
  137. package/plugins/design/templates/design-system-inspiration/SUB-AGENT-PROMPTS.md +1 -1
  138. package/plugins/design/templates/design-system-inspiration/core/preview/_motion-readme.md.tpl +1 -1
  139. package/apps/studio/server.mjs +0 -1312
  140. package/apps/studio/sync/asset-pull.ts +0 -210
  141. package/apps/studio/sync/asset-push-worker.ts +0 -84
  142. package/apps/studio/sync/asset-sweep.ts +0 -262
  143. package/apps/studio/test/sync-asset-pull.test.ts +0 -161
  144. package/apps/studio/test/sync-asset-push-worker.test.ts +0 -183
  145. package/apps/studio/test/sync-asset-sweep.test.ts +0 -243
@@ -37,6 +37,15 @@ const git: GitRunner = (args, { cwd }) =>
37
37
  child.stderr.on('data', (d) => {
38
38
  stderr += d;
39
39
  });
40
+ // SPAWN FAILURE IS A RESULT, NOT A CRASH — mirroring the production runner
41
+ // (`git/service.ts`, which resolves 127 on 'error'). Without a listener the
42
+ // 'error' event is unhandled, and autocommit is DEBOUNCED: a commit queued
43
+ // by a finished test can fire after its temp repo is gone, so the late
44
+ // spawn reports `ENOENT ... posix_spawn 'git'` with no test to attribute it
45
+ // to. bun then prints "Unhandled error between tests" and exits 1 on a run
46
+ // with 844 pass / 0 fail — which is exactly how the required sync-lane gate
47
+ // came back red on a suite that had nothing wrong with it.
48
+ child.on('error', (e) => resolve({ code: 127, stdout, stderr: String(e) }));
40
49
  child.on('close', (code) => resolve({ code: code ?? 1, stdout, stderr }));
41
50
  });
42
51
 
@@ -455,3 +464,74 @@ describe('a contended lock must not lose the queue or the process (Phase 27 D2)'
455
464
  }
456
465
  }, 30_000);
457
466
  });
467
+
468
+ describe('a vanished path must not wedge the agent forever', () => {
469
+ // THE BUG: `git add -- <path that is gone and was never tracked>` exits 128
470
+ // with "did not match any files". The batch failed, re-queued ITSELF
471
+ // INCLUDING that path, and failed again identically — so the first canvas
472
+ // anybody deleted stopped the cell committing ANYTHING for the life of the
473
+ // process, while /health went on answering 200.
474
+ //
475
+ // Observed on a local cell: five commits, then twenty-plus identical
476
+ // `git add failed` lines and thirty canvases sitting untracked.
477
+ //
478
+ // Paths are noted from a read taken up to debounceMs earlier, for files whose
479
+ // lifetime this process does not own. A path disappearing mid-window is
480
+ // ROUTINE, so it has to be a case rather than an error.
481
+
482
+ const GONE = '.design/ui/Deleted.tsx';
483
+
484
+ test('a never-tracked path that vanished is dropped, and the rest still commits', async () => {
485
+ const auto = createAutoCommit({ repoRoot: repo, run: git, debounceMs: 5, log: silent() });
486
+ write(REL, 'export default () => "edited";\n');
487
+ auto.note(REL, ALICE);
488
+ // A sibling that was created and deleted inside the quiescence window.
489
+ auto.note(GONE, ALICE);
490
+ await auto.flush();
491
+
492
+ expect(await log('%s')).toEqual(['design: update Screen', 'seed']);
493
+ auto.stop();
494
+ });
495
+
496
+ test('and the agent is NOT wedged — the next edit commits normally', async () => {
497
+ const auto = createAutoCommit({ repoRoot: repo, run: git, debounceMs: 5, log: silent() });
498
+ write(REL, 'v1\n');
499
+ auto.note(REL, ALICE);
500
+ auto.note(GONE, ALICE);
501
+ await auto.flush();
502
+
503
+ write(REL, 'v2\n');
504
+ auto.note(REL, BOB);
505
+ await auto.flush();
506
+
507
+ // Two commits after the seed. Before the fix the first flush poisoned
508
+ // `touched` and every later flush failed on the same missing pathspec.
509
+ expect((await log('%s')).length).toBe(3);
510
+ auto.stop();
511
+ });
512
+
513
+ test('a TRACKED path that vanished is committed as a deletion, not dropped', async () => {
514
+ // The other kind of gone. Dropping this one would let the checkout and its
515
+ // history diverge permanently: the file is gone from disk and git would go
516
+ // on believing it exists.
517
+ const auto = createAutoCommit({ repoRoot: repo, run: git, debounceMs: 5, log: silent() });
518
+ rmSync(path.join(repo, REL));
519
+ auto.note(REL, ALICE);
520
+ await auto.flush();
521
+
522
+ const tracked = (await git(['ls-files'], { cwd: repo })).stdout;
523
+ expect(tracked).not.toContain('Screen.tsx');
524
+ expect((await log('%s')).length).toBe(2);
525
+ auto.stop();
526
+ });
527
+
528
+ test('a batch of nothing but vanished untracked paths is a no-op, not a failure', async () => {
529
+ const auto = createAutoCommit({ repoRoot: repo, run: git, debounceMs: 5, log: silent() });
530
+ auto.note(GONE, ALICE);
531
+ auto.note('.design/ui/AlsoGone.tsx', ALICE);
532
+ await auto.flush();
533
+
534
+ expect(await log('%s')).toEqual(['seed']);
535
+ auto.stop();
536
+ });
537
+ });
@@ -0,0 +1,346 @@
1
+ // The nudge — the cell child's half of the doorbell (DDR-226 §4, Increment 3).
2
+ //
3
+ // This exists because Increment 3 shipped `POST /api/journal/report` with no
4
+ // caller, and nothing noticed for a release: the receiver's own tests were
5
+ // green, the route answered correctly, and the only symptom was that
6
+ // cloud → desktop took up to fifteen minutes while desktop → cloud took
7
+ // seconds. So the last describe block here is a REACHABILITY tripwire — it
8
+ // asserts the wiring exists, not just that the module is correct in isolation.
9
+ //
10
+ // What is pinned:
11
+ //
12
+ // 1. **It carries paths and nothing else.** A nudge that could state a hash
13
+ // would be a nudge that could lie about content.
14
+ // 2. **The filter.** Runtime state (`_state/`, `_history/`, …) and
15
+ // malformed paths never reach the wire — otherwise a single canvas save
16
+ // would nudge about a dozen per-machine files nobody syncs.
17
+ // 3. **The mute.** A path the healer just announced is a fact the hub stated;
18
+ // we do not bill it for hearing it back.
19
+ // 4. **Failure is latency, never loss.** A hub that refuses the nudge must
20
+ // not throw, must not retry forever, and must say so once.
21
+
22
+ import { describe, expect, test } from 'bun:test';
23
+
24
+ import { createCellWriteNudge } from '../sync/cell-write-nudge.ts';
25
+
26
+ interface Call {
27
+ url: string;
28
+ body: { paths?: unknown };
29
+ auth: string | null;
30
+ }
31
+
32
+ /** A fetch double that records calls and answers with whatever `ok` says. */
33
+ function recorder(ok: (n: number) => boolean = () => true) {
34
+ const calls: Call[] = [];
35
+ const fetchImpl = (async (url: unknown, init: unknown) => {
36
+ const i = init as { headers?: Record<string, string>; body?: string };
37
+ calls.push({
38
+ url: String(url),
39
+ body: JSON.parse(i?.body ?? '{}'),
40
+ auth: i?.headers?.authorization ?? null,
41
+ });
42
+ const good = ok(calls.length);
43
+ return {
44
+ ok: good,
45
+ status: good ? 200 : 503,
46
+ json: async () => ({ noted: 0 }),
47
+ } as unknown as Response;
48
+ }) as unknown as typeof fetch;
49
+ return { calls, fetchImpl };
50
+ }
51
+
52
+ /** The `paths` array a recorded call carried — the only field a nudge may state. */
53
+ const pathsOf = (call: Call | undefined) => (call?.body.paths ?? []) as string[];
54
+
55
+ function nudger(over: Record<string, unknown> = {}) {
56
+ const { calls, fetchImpl } = (over.rec as ReturnType<typeof recorder>) ?? recorder();
57
+ return {
58
+ calls,
59
+ n: createCellWriteNudge({
60
+ hubUrl: 'http://127.0.0.1:4599',
61
+ token: 'tok',
62
+ fetchImpl,
63
+ coalesceMs: 0,
64
+ log: { warn() {}, error() {}, log() {} },
65
+ ...over,
66
+ }),
67
+ };
68
+ }
69
+
70
+ describe('the wire — paths only, to the loopback hub, with the bearer', () => {
71
+ test('a noted path becomes one POST naming exactly it', async () => {
72
+ const { n, calls } = nudger();
73
+ n.note('assets/photo.png');
74
+ await n.flush();
75
+
76
+ expect(calls).toHaveLength(1);
77
+ expect(calls[0]?.url).toBe('http://127.0.0.1:4599/api/journal/report');
78
+ expect(calls[0]?.auth).toBe('Bearer tok');
79
+ expect(calls[0]?.body).toEqual({ paths: ['assets/photo.png'] });
80
+ });
81
+
82
+ test('the body has NO field through which content could be stated', async () => {
83
+ const { n, calls } = nudger();
84
+ n.note('system/ds/brand.css');
85
+ await n.flush();
86
+
87
+ // The hub re-stats and re-hashes its own disk for every path it is handed.
88
+ // If this body ever grows a `sha256`/`size`/`deleted`, that property is
89
+ // gone and a supervised-but-untrusted process can speak about bytes.
90
+ expect(Object.keys(calls[0]?.body ?? {})).toEqual(['paths']);
91
+ });
92
+
93
+ test('a trailing slash on the hub URL does not produce a double slash', async () => {
94
+ const { n, calls } = nudger({ hubUrl: 'http://127.0.0.1:4599/' });
95
+ n.note('a.css');
96
+ await n.flush();
97
+ expect(calls[0]?.url).toBe('http://127.0.0.1:4599/api/journal/report');
98
+ });
99
+
100
+ test('a burst coalesces into one request, deduped', async () => {
101
+ const { n, calls } = nudger();
102
+ n.note('a.css');
103
+ n.note('b.css');
104
+ n.note('a.css');
105
+ await n.flush();
106
+
107
+ expect(calls).toHaveLength(1);
108
+ expect(pathsOf(calls[0]).sort()).toEqual(['a.css', 'b.css']);
109
+ });
110
+
111
+ test('more than the route ceiling splits into several requests', async () => {
112
+ const { n, calls } = nudger();
113
+ for (let i = 0; i < 70; i++) n.note(`assets/f${i}.png`);
114
+ await n.flush();
115
+
116
+ expect(calls).toHaveLength(2);
117
+ expect(pathsOf(calls[0]).length).toBe(64);
118
+ expect(pathsOf(calls[1]).length).toBe(6);
119
+ expect(n.named()).toBe(70);
120
+ });
121
+
122
+ test('nothing pending sends nothing', async () => {
123
+ const { n, calls } = nudger();
124
+ await n.flush();
125
+ expect(calls).toHaveLength(0);
126
+ expect(n.sent()).toBe(0);
127
+ });
128
+ });
129
+
130
+ describe('the filter — the obvious noise never reaches the wire', () => {
131
+ const dropped = [
132
+ '_state/file-ledger/hub.json',
133
+ '_history/ui-home/1234.tsx',
134
+ '_canvas-state/home.view.json',
135
+ '_chat/c-abc.session.json',
136
+ '_server.json',
137
+ '_active.json',
138
+ '_sync.json',
139
+ '_untrusted/INDEX.json',
140
+ '_trash/old.tsx',
141
+ 'ui/_server.json',
142
+ ];
143
+
144
+ for (const rel of dropped) {
145
+ test(`runtime state is not sync's business: ${rel}`, async () => {
146
+ const { n, calls } = nudger();
147
+ n.note(rel);
148
+ await n.flush();
149
+ expect(calls).toHaveLength(0);
150
+ });
151
+ }
152
+
153
+ const malformed = ['', '/abs/path.css', '../escape.css', 'a\\b.css', 'node_modules/x/i.js'];
154
+
155
+ for (const rel of malformed) {
156
+ test(`refused by shape: ${JSON.stringify(rel)}`, async () => {
157
+ const { n, calls } = nudger();
158
+ n.note(rel);
159
+ await n.flush();
160
+ expect(calls).toHaveLength(0);
161
+ });
162
+ }
163
+
164
+ test('a real design file gets through', async () => {
165
+ const { n, calls } = nudger();
166
+ n.note('system/smoke/preview/logo.svg');
167
+ await n.flush();
168
+ expect(calls).toHaveLength(1);
169
+ });
170
+
171
+ test('separators are judged, not rewritten', async () => {
172
+ // Every `fs:any` producer already emits forward slashes, so a backslash
173
+ // reaching here is not a Windows separator to be repaired — on POSIX it is
174
+ // a legal character in one filename, and rewriting it would nudge about a
175
+ // DIFFERENT file. The classifier refuses it, same as the hub would.
176
+ const { n, calls } = nudger();
177
+ n.note('system\\smoke\\brand.css');
178
+ await n.flush();
179
+ expect(calls).toHaveLength(0);
180
+ });
181
+
182
+ test('the client filter is a noise gate, NOT the membership decision', async () => {
183
+ // `ui/home.tsx` is canvas-owned — it travels as a CRDT doc, not on the file
184
+ // plane. We still nudge: the hub's classifier is the single membership
185
+ // oracle (DDR-226 §1) and answers `null` for it. Deciding membership here
186
+ // too would be a second oracle, and second oracles drift.
187
+ const { n, calls } = nudger();
188
+ n.note('ui/home.tsx');
189
+ await n.flush();
190
+ expect(calls).toHaveLength(1);
191
+ });
192
+ });
193
+
194
+ describe('the mute — we do not bill the hub for hearing its own facts back', () => {
195
+ test('a muted path is dropped once, then nudges normally', async () => {
196
+ const { n, calls } = nudger();
197
+
198
+ n.mute('assets/pulled.png');
199
+ n.note('assets/pulled.png'); // the healer's own fs:any echo
200
+ await n.flush();
201
+ expect(calls).toHaveLength(0);
202
+ expect(n.muted()).toBe(1);
203
+
204
+ // The mute is consumed, not sticky: a genuine local edit to a file we just
205
+ // pulled must still reach the hub.
206
+ n.note('assets/pulled.png');
207
+ await n.flush();
208
+ expect(calls).toHaveLength(1);
209
+ });
210
+
211
+ test('a mute expires', async () => {
212
+ let clock = 1_000;
213
+ const { n, calls } = nudger({ muteMs: 500, now: () => clock });
214
+
215
+ n.mute('assets/x.png');
216
+ clock += 600;
217
+ n.note('assets/x.png');
218
+ await n.flush();
219
+
220
+ expect(calls).toHaveLength(1);
221
+ expect(n.muted()).toBe(0);
222
+ });
223
+
224
+ test('muting one path does not mute another', async () => {
225
+ const { n, calls } = nudger();
226
+ n.mute('a.css');
227
+ n.note('a.css');
228
+ n.note('b.css');
229
+ await n.flush();
230
+ expect(calls[0]?.body.paths).toEqual(['b.css']);
231
+ });
232
+ });
233
+
234
+ describe('failure is latency, never loss', () => {
235
+ test('an unreachable hub does not throw and does not lose the process', async () => {
236
+ const throwing = (async () => {
237
+ throw new Error('ECONNREFUSED');
238
+ }) as unknown as typeof fetch;
239
+ const n = createCellWriteNudge({
240
+ hubUrl: 'http://127.0.0.1:4599',
241
+ token: 'tok',
242
+ fetchImpl: throwing,
243
+ coalesceMs: 0,
244
+ log: { warn() {}, error() {}, log() {} },
245
+ });
246
+
247
+ n.note('a.css');
248
+ await n.flush(); // must resolve, not reject
249
+ expect(n.failures()).toBe(1);
250
+ expect(n.sent()).toBe(0);
251
+ });
252
+
253
+ test('a refused batch is dropped, not retried forever — walk-import is the backstop', async () => {
254
+ const rec = recorder(() => false);
255
+ const { n, calls } = nudger({ rec });
256
+
257
+ n.note('a.css');
258
+ await n.flush();
259
+ await n.flush();
260
+ await n.flush();
261
+
262
+ // One attempt per note. Re-queueing would turn a hub restart into an
263
+ // unbounded storm against a rate-limited route, and buy nothing the
264
+ // reconciler does not already guarantee.
265
+ expect(calls).toHaveLength(1);
266
+ });
267
+
268
+ test('it says so once, not once per path', async () => {
269
+ const warnings: string[] = [];
270
+ const rec = recorder(() => false);
271
+ const { n } = nudger({
272
+ rec,
273
+ log: { warn: (m: string) => warnings.push(m), error() {}, log() {} },
274
+ });
275
+
276
+ n.note('a.css');
277
+ await n.flush();
278
+ n.note('b.css');
279
+ await n.flush();
280
+
281
+ expect(warnings).toHaveLength(1);
282
+ expect(warnings[0]).toMatch(/walk-import/);
283
+ });
284
+
285
+ test('recovery clears the failure count and re-arms the warning', async () => {
286
+ const warnings: string[] = [];
287
+ let healthy = false;
288
+ const rec = recorder(() => healthy);
289
+ const { n } = nudger({
290
+ rec,
291
+ log: { warn: (m: string) => warnings.push(m), error() {}, log() {} },
292
+ });
293
+
294
+ n.note('a.css');
295
+ await n.flush();
296
+ expect(n.failures()).toBe(1);
297
+
298
+ healthy = true;
299
+ n.note('b.css');
300
+ await n.flush();
301
+ expect(n.failures()).toBe(0);
302
+
303
+ healthy = false;
304
+ n.note('c.css');
305
+ await n.flush();
306
+ expect(warnings).toHaveLength(2); // a NEW outage is worth a NEW line
307
+ });
308
+
309
+ test('stop() silences it — a torn-down child does not keep talking', async () => {
310
+ const { n, calls } = nudger();
311
+ n.stop();
312
+ n.note('a.css');
313
+ n.mute('b.css');
314
+ await n.flush();
315
+ expect(calls).toHaveLength(0);
316
+ });
317
+ });
318
+
319
+ describe('REACHABILITY — the bug this module was written to close', () => {
320
+ test('the route has a caller in the shipped runtime', async () => {
321
+ // Increment 3 shipped `POST /api/journal/report` and nothing that calls it.
322
+ // Every unit test passed. The hub answered correctly. The only symptom was
323
+ // that one sync direction was 300× slower than the other, which reads to a
324
+ // user as "cloud → desktop is broken".
325
+ //
326
+ // Grep, not import: the point is that the WIRING exists in the file that
327
+ // boots the cell child, which no amount of testing this module in isolation
328
+ // can establish.
329
+ const wiring = await Bun.file(new URL('../sync/cell-file-events.ts', import.meta.url)).text();
330
+
331
+ expect(wiring).toContain('createCellWriteNudge');
332
+ // Subscribed to the bus event both synthetic-write sources converge on.
333
+ expect(wiring).toMatch(/bus\.on\(\s*'fs:any'/);
334
+ // And the healer mutes what it announces, so the echo is not billed.
335
+ expect(wiring).toContain('nudge.mute');
336
+ });
337
+
338
+ test('the sender names the exact route the hub receiver serves', async () => {
339
+ const hubJournal = await Bun.file(new URL('../../hub/src/journal.mjs', import.meta.url)).text();
340
+ const sender = await Bun.file(new URL('../sync/cell-write-nudge.ts', import.meta.url)).text();
341
+
342
+ // The hub's constant is the authority; this pins the twin.
343
+ expect(hubJournal).toContain("JOURNAL_REPORT_PATH = '/api/journal/report'");
344
+ expect(sender).toContain('/api/journal/report');
345
+ });
346
+ });