@1agh/maude 0.27.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.
Files changed (27) hide show
  1. package/cli/commands/design-link.test.mjs +46 -0
  2. package/cli/commands/doctor.mjs +110 -5
  3. package/cli/commands/doctor.test.mjs +52 -0
  4. package/cli/lib/design-link.mjs +38 -8
  5. package/package.json +8 -8
  6. package/plugins/design/dev-server/activity.ts +256 -0
  7. package/plugins/design/dev-server/ai-banner.tsx +4 -0
  8. package/plugins/design/dev-server/artboard-activity-overlay.tsx +67 -0
  9. package/plugins/design/dev-server/canvas-comment-mount.tsx +164 -9
  10. package/plugins/design/dev-server/canvas-lib.tsx +62 -15
  11. package/plugins/design/dev-server/config.schema.json +2 -1
  12. package/plugins/design/dev-server/dist/client.bundle.js +18 -18
  13. package/plugins/design/dev-server/dist/comment-mount.js +82 -4
  14. package/plugins/design/dev-server/inspect.ts +31 -1
  15. package/plugins/design/dev-server/participants-chrome.tsx +86 -1
  16. package/plugins/design/dev-server/server.ts +10 -1
  17. package/plugins/design/dev-server/sync/index.ts +24 -19
  18. package/plugins/design/dev-server/test/activity.test.ts +195 -0
  19. package/plugins/design/dev-server/test/artboard-activity-overlay.test.tsx +56 -0
  20. package/plugins/design/dev-server/test/canvas-hmr-runtime.test.tsx +114 -0
  21. package/plugins/design/dev-server/test/sync-runtime.test.ts +34 -6
  22. package/plugins/design/dev-server/test/use-agent-presence.test.tsx +114 -0
  23. package/plugins/design/dev-server/test/use-canvas-activity.test.tsx +206 -0
  24. package/plugins/design/dev-server/use-agent-presence.tsx +244 -0
  25. package/plugins/design/dev-server/use-canvas-activity.tsx +252 -0
  26. package/plugins/design/dev-server/ws.ts +21 -2
  27. package/plugins/design/templates/_shell.html +108 -3
@@ -0,0 +1,252 @@
1
+ /**
2
+ * @file use-canvas-activity.tsx — Phase 13 / DDR-029 activity context.
3
+ * @scope plugins/design/dev-server/use-canvas-activity.tsx
4
+ * @purpose Iframe-runtime React context fed by the server's `activity` WS
5
+ * messages. `DCArtboard` reads it to render the "agent works here"
6
+ * overlay on the artboards being edited right now.
7
+ *
8
+ * The bridge: the canvas-shell harness (`templates/_shell.html`) owns the WS
9
+ * connection and re-dispatches every `{ type:'activity', … }` message as a
10
+ * `maude:activity` CustomEvent on `document` (the same pattern used for
11
+ * `maude:meta-refreshed`). This provider subscribes to that event — no second
12
+ * socket, and trivially testable (dispatch the event in a test). In-memory only:
13
+ * activity is ephemeral, never persisted.
14
+ */
15
+
16
+ import {
17
+ type ReactNode,
18
+ createContext,
19
+ useContext,
20
+ useEffect,
21
+ useMemo,
22
+ useRef,
23
+ useState,
24
+ } from 'react';
25
+
26
+ /** Cross-fade window after a file flips `idle` before its overlay is removed. */
27
+ export const ACTIVITY_FADE_MS = 200;
28
+
29
+ /** Wire shape of a server `activity` message (snake_case, matches activity.ts). */
30
+ export interface ActivityMessage {
31
+ type?: 'activity';
32
+ file: string;
33
+ status: 'active' | 'idle';
34
+ artboard_ids?: string[] | null;
35
+ ts: string;
36
+ }
37
+
38
+ interface ActivityEntry {
39
+ status: 'active' | 'idle';
40
+ artboardIds: string[] | null;
41
+ ts: string;
42
+ }
43
+
44
+ type ActivityMap = Record<string, ActivityEntry>;
45
+
46
+ /** What `useCanvasActivity()` returns for a given canvas file. */
47
+ export interface CanvasActivity {
48
+ /** An entry exists (file is active OR within the post-idle fade window). */
49
+ present: boolean;
50
+ /** The file is currently being edited (drives the pulse). */
51
+ active: boolean;
52
+ /** Scoped artboard ids, or null = file-level (every artboard lights up). */
53
+ artboardIds: string[] | null;
54
+ /** Basename of the canvas file, for the badge label. */
55
+ fileLabel: string;
56
+ }
57
+
58
+ interface ActivityContextValue {
59
+ map: ActivityMap;
60
+ /** This canvas's design-root-relative key (server-message keyspace). */
61
+ currentKey: string;
62
+ normalizeKey: (file: string) => string;
63
+ }
64
+
65
+ const ActivityContext = createContext<ActivityContextValue | null>(null);
66
+
67
+ // ---------------------------------------------------------------------------
68
+ // Pure helpers — exported for unit tests.
69
+
70
+ /**
71
+ * Normalize a canvas path to the server's activity keyspace: design-root-
72
+ * relative, slash-normalized, no leading slash. Accepts the canonical form
73
+ * (`ui/Foo.tsx`) or the designRel-prefixed form (`.design/ui/Foo.tsx`) that
74
+ * `mountCanvas` passes — stripping `designRel` when known.
75
+ */
76
+ export function activityKey(file: string, designRel?: string): string {
77
+ let s = (file ?? '').replace(/\\/g, '/').replace(/^\/+/, '');
78
+ const dr = (designRel ?? '').replace(/^\.\//, '').replace(/^\/+|\/+$/g, '');
79
+ if (dr && s.startsWith(`${dr}/`)) s = s.slice(dr.length + 1);
80
+ return s;
81
+ }
82
+
83
+ /** Pure reducer: fold one activity message into the map. */
84
+ export function applyActivityChange(prev: ActivityMap, change: ActivityMessage): ActivityMap {
85
+ if (!change || typeof change.file !== 'string' || !change.file) return prev;
86
+ return {
87
+ ...prev,
88
+ [change.file]: {
89
+ status: change.status === 'active' ? 'active' : 'idle',
90
+ artboardIds: Array.isArray(change.artboard_ids) ? change.artboard_ids : null,
91
+ ts: typeof change.ts === 'string' ? change.ts : '',
92
+ },
93
+ };
94
+ }
95
+
96
+ /** True when an artboard id is in scope for a change (null scope = all). */
97
+ export function matchesArtboard(artboardIds: string[] | null, id: string): boolean {
98
+ return artboardIds === null || artboardIds.includes(id);
99
+ }
100
+
101
+ function basename(key: string): string {
102
+ const i = key.lastIndexOf('/');
103
+ return i >= 0 ? key.slice(i + 1) : key;
104
+ }
105
+
106
+ function readDesignRel(explicit?: string): string | undefined {
107
+ if (explicit) return explicit;
108
+ if (typeof window !== 'undefined') {
109
+ const w = window as unknown as { __canvas_design_rel__?: string };
110
+ if (typeof w.__canvas_design_rel__ === 'string') return w.__canvas_design_rel__;
111
+ }
112
+ return undefined;
113
+ }
114
+
115
+ /**
116
+ * Seed the map from `window.__maude_activity_seed__` — the WS-open snapshot the
117
+ * shell stashes (raw server `activity.state`, keyed design-root-relative with a
118
+ * camelCase `artboardIds`). Load-bearing after the HMR reload a canvas edit
119
+ * triggers: the snapshot can land before React mounts, so the dispatched event
120
+ * is missed and only this synchronous read recovers the in-flight overlay.
121
+ */
122
+ function readActivitySeed(): ActivityMap {
123
+ if (typeof window === 'undefined') return {};
124
+ const w = window as unknown as {
125
+ __maude_activity_seed__?: Record<
126
+ string,
127
+ { status?: 'active' | 'idle'; ts?: string; artboardIds?: string[] | null }
128
+ >;
129
+ };
130
+ const seed = w.__maude_activity_seed__;
131
+ if (!seed || typeof seed !== 'object') return {};
132
+ const out: ActivityMap = {};
133
+ for (const file of Object.keys(seed)) {
134
+ const e = seed[file];
135
+ if (!e || e.status !== 'active') continue; // only resurrect active overlays
136
+ out[file] = {
137
+ status: 'active',
138
+ artboardIds: Array.isArray(e.artboardIds) ? e.artboardIds : null,
139
+ ts: typeof e.ts === 'string' ? e.ts : '',
140
+ };
141
+ }
142
+ return out;
143
+ }
144
+
145
+ // ---------------------------------------------------------------------------
146
+
147
+ export interface CanvasActivityProviderProps {
148
+ /** This canvas's file (designRel-prefixed or design-root-relative). */
149
+ file?: string;
150
+ /** designRel for key normalization; defaults to `window.__canvas_design_rel__`. */
151
+ designRel?: string;
152
+ /** Seed (snapshot / tests). Keyed by design-root-relative path. */
153
+ initialState?: ActivityMap;
154
+ children: ReactNode;
155
+ }
156
+
157
+ export function CanvasActivityProvider({
158
+ file,
159
+ designRel,
160
+ initialState,
161
+ children,
162
+ }: CanvasActivityProviderProps) {
163
+ const dr = readDesignRel(designRel);
164
+ const currentKey = useMemo(() => {
165
+ if (file) return activityKey(file, dr);
166
+ // No explicit file (the canvas-lib mount path) → read the design-root-
167
+ // relative canvas path the shell stamps on window. It already matches the
168
+ // server's activity keyspace, so no normalization is needed.
169
+ if (typeof window !== 'undefined') {
170
+ const w = window as unknown as { __canvas_rel__?: string };
171
+ if (typeof w.__canvas_rel__ === 'string' && w.__canvas_rel__) return w.__canvas_rel__;
172
+ }
173
+ return '';
174
+ }, [file, dr]);
175
+ const [map, setMap] = useState<ActivityMap>(() => initialState ?? readActivitySeed());
176
+ // Per-file removal timers for the post-idle cross-fade.
177
+ const fadeTimers = useRef<Map<string, ReturnType<typeof setTimeout>>>(new Map());
178
+
179
+ useEffect(() => {
180
+ if (typeof document === 'undefined') return;
181
+ const timers = fadeTimers.current;
182
+
183
+ function onActivity(ev: Event) {
184
+ const detail = (ev as CustomEvent<ActivityMessage>).detail;
185
+ if (!detail || typeof detail.file !== 'string') return;
186
+ setMap((prev) => applyActivityChange(prev, detail));
187
+
188
+ const pending = timers.get(detail.file);
189
+ if (pending) {
190
+ clearTimeout(pending);
191
+ timers.delete(detail.file);
192
+ }
193
+ if (detail.status === 'idle') {
194
+ // Keep the (now-idle) entry around briefly so the overlay cross-fades
195
+ // out instead of snapping off, then drop it.
196
+ const ts = detail.ts;
197
+ const t = setTimeout(() => {
198
+ timers.delete(detail.file);
199
+ setMap((prev) => {
200
+ const entry = prev[detail.file];
201
+ // Only remove if it's still the same idle entry (not re-activated).
202
+ if (!entry || entry.status !== 'idle' || entry.ts !== ts) return prev;
203
+ const { [detail.file]: _drop, ...rest } = prev;
204
+ return rest;
205
+ });
206
+ }, ACTIVITY_FADE_MS);
207
+ timers.set(detail.file, t);
208
+ }
209
+ }
210
+
211
+ document.addEventListener('maude:activity', onActivity as EventListener);
212
+ return () => {
213
+ document.removeEventListener('maude:activity', onActivity as EventListener);
214
+ for (const t of timers.values()) clearTimeout(t);
215
+ timers.clear();
216
+ };
217
+ }, []);
218
+
219
+ const value = useMemo<ActivityContextValue>(
220
+ () => ({ map, currentKey, normalizeKey: (f: string) => activityKey(f, dr) }),
221
+ [map, currentKey, dr]
222
+ );
223
+
224
+ return <ActivityContext.Provider value={value}>{children}</ActivityContext.Provider>;
225
+ }
226
+
227
+ const EMPTY: CanvasActivity = {
228
+ present: false,
229
+ active: false,
230
+ artboardIds: null,
231
+ fileLabel: '',
232
+ };
233
+
234
+ /**
235
+ * Activity state for a canvas. With no argument, returns the current canvas's
236
+ * activity (the provider knows its own file); pass a `file` to query another.
237
+ * Returns an inert value outside the provider (legacy / specimen mounts), so the
238
+ * overlay simply never renders there.
239
+ */
240
+ export function useCanvasActivity(file?: string): CanvasActivity {
241
+ const ctx = useContext(ActivityContext);
242
+ if (!ctx) return EMPTY;
243
+ const key = file != null ? ctx.normalizeKey(file) : ctx.currentKey;
244
+ if (!key) return EMPTY;
245
+ const entry = ctx.map[key];
246
+ return {
247
+ present: !!entry,
248
+ active: entry?.status === 'active',
249
+ artboardIds: entry?.artboardIds ?? null,
250
+ fileLabel: basename(key),
251
+ };
252
+ }
@@ -3,6 +3,7 @@
3
3
 
4
4
  import type { ServerWebSocket, WebSocketHandler } from 'bun';
5
5
 
6
+ import type { Activity } from './activity.ts';
6
7
  import type { Api } from './api.ts';
7
8
  import type { Collab, RoomConn } from './collab/index.ts';
8
9
  import type { Context } from './context.ts';
@@ -76,7 +77,13 @@ export interface Ws {
76
77
  clientCount(): number;
77
78
  }
78
79
 
79
- export function createWs(ctx: Context, api: Api, inspect: Inspect, collab: Collab): Ws {
80
+ export function createWs(
81
+ ctx: Context,
82
+ api: Api,
83
+ inspect: Inspect,
84
+ collab: Collab,
85
+ activity: Activity
86
+ ): Ws {
80
87
  const clients = new Set<ServerWebSocket<WsData>>();
81
88
 
82
89
  function send(ws: ServerWebSocket<WsData>, payload: unknown) {
@@ -141,6 +148,14 @@ export function createWs(ctx: Context, api: Api, inspect: Inspect, collab: Colla
141
148
  // Uses broadcastHmr so the segregated canvas origin's HMR-only sockets get it.
142
149
  createHmrBroadcaster(ctx, (msg) => broadcastHmr(msg));
143
150
 
151
+ // Phase 13 / DDR-029 — canvas activity overlay. activity.ts emits
152
+ // `activity:change` per file as edits land + go idle. The overlay renders
153
+ // INSIDE the canvas iframe, which (with the default origin split, DDR-054)
154
+ // holds a `canvas-hmr` socket — so this MUST use broadcastHmr, not broadcast,
155
+ // to reach it. The payload is just a canvas path + status (no secrets); it's
156
+ // the same exposure class the iframe already gets from `canvas-hmr` file paths.
157
+ ctx.bus.on('activity:change', (payload) => broadcastHmr({ type: 'activity', ...payload }));
158
+
144
159
  // Bind a connection to its room. Stored per-socket so close() can find the
145
160
  // right room to disconnect from. Multiplexed via ws.data.id.
146
161
  const collabConns = new Map<string, { roomSlug: string; conn: RoomConn }>();
@@ -175,7 +190,11 @@ export function createWs(ctx: Context, api: Api, inspect: Inspect, collab: Colla
175
190
  return;
176
191
  }
177
192
  clients.add(ws);
178
- send(ws, { type: 'snapshot', state: inspect.state });
193
+ // Snapshot carries the inspector state AND the activity map so a client
194
+ // opening mid-edit seeds its overlay state (Phase 13). Inspector-origin
195
+ // sockets only — `canvas-hmr` sockets get no snapshot by design and rely
196
+ // on live `activity` broadcasts.
197
+ send(ws, { type: 'snapshot', state: inspect.state, activity: activity.state });
179
198
  },
180
199
  async close(ws) {
181
200
  if (ws.data.kind === 'collab') {
@@ -110,6 +110,10 @@
110
110
  /* HUDs + banners */
111
111
  .dc-ai-banner,
112
112
  .dc-undo-hud,
113
+ /* Activity overlay — live "agent works here" chrome (Phase 13 / DDR-029) */
114
+ .dc-activity-rim,
115
+ /* HMR "holding last good" toast (Phase 13.1 / DDR-077) */
116
+ .dc-hmr-holding,
113
117
  /* Artboard title/label button (editor affordance, not design content) */
114
118
  .dc-artboard-label,
115
119
  /* Generic opt-in hooks for any future floating overlay */
@@ -162,6 +166,52 @@
162
166
  const tokensRel = stripDesignPrefix(params.get('tokens') || '');
163
167
  const componentsRel = stripDesignPrefix(params.get('components') || '');
164
168
  const layoutRel = stripDesignPrefix(params.get('layout') || '');
169
+
170
+ // Phase 13.1 / DDR-077 — error-resilient HMR while an agent edits. Hoisted
171
+ // here so the HMR client (below) can soft-reload the canvas module instead
172
+ // of location.reload()-ing into a half-saved (broken) build.
173
+ const canvasUrl = canvasRel ? ('/' + designRel + '/' + canvasRel) : null;
174
+ const myFileKey = canvasRel ? (designRel + '/' + canvasRel) : null;
175
+ // True while an `ai-activity` entry is live for THIS canvas (an agent is
176
+ // running /design:edit|new on it). Mirrors ai-banner's gate: parent relays
177
+ // ai-activity via postMessage when embedded; the own-WS path (standalone /
178
+ // same-origin inspector socket) is handled in connectHmr below.
179
+ let agentEditing = false;
180
+ window.addEventListener('message', function (e) {
181
+ // DDR-078 security follow-up: only the trusted embedding parent relays
182
+ // ai-activity. Reject canvas self-posts (which could flip the HMR gate)
183
+ // and any non-parent source. Standalone gets it via its own WS below.
184
+ if (e.source !== window.parent || window.parent === window) return;
185
+ const m = e && e.data;
186
+ if (!m || typeof m !== 'object' || m.dgn !== 'ai-activity') return;
187
+ if (myFileKey && m.file === myFileKey) agentEditing = !!m.entry;
188
+ });
189
+
190
+ // Soft reload — re-import the canvas module (cache-busted) and swap it in
191
+ // via the runtime, holding the last good render on a build/import error
192
+ // (no white flash). Only used when an agent is editing; manual edits keep
193
+ // the plain location.reload() below.
194
+ function softReload(version) {
195
+ if (!canvasUrl) { location.reload(); return; }
196
+ const rt = window.__maudeCanvasRuntime;
197
+ if (!rt || !rt.remount) { location.reload(); return; }
198
+ import(canvasUrl + '?v=' + (version || Date.now()))
199
+ .then(function (mod) {
200
+ if (mod && typeof mod.default === 'function') {
201
+ rt.remount(mod.default);
202
+ if (rt.setHolding) rt.setHolding(false);
203
+ } else {
204
+ // No usable default export (rare) — fall back to a hard reload.
205
+ location.reload();
206
+ }
207
+ })
208
+ .catch(function (err) {
209
+ // Build/transpile/import error → keep the current render, surface a
210
+ // non-destructive "holding" toast. Next good build swaps it in.
211
+ if (rt.setHolding) rt.setHolding(true, (err && (err.message || String(err))) || 'build error');
212
+ console.warn('[canvas-shell] soft-reload held last good (build error):', err);
213
+ });
214
+ }
165
215
  // Phase 6.5 — exporters pass ?hide-chrome=1 to flip the export-mode
166
216
  // stylesheet from `media="not all"` to `media="all"`, hiding the
167
217
  // dev-server overlays during capture. See `<style id="canvas-hide-chrome">`.
@@ -192,7 +242,55 @@
192
242
  ws.addEventListener('message', (ev) => {
193
243
  try {
194
244
  const msg = JSON.parse(ev.data);
195
- if (!msg || msg.type !== 'canvas-hmr') return;
245
+ if (!msg) return;
246
+ // Phase 13.1 / DDR-077 — own-WS path for the agent-active gate (this
247
+ // socket is the inspector channel in standalone / same-origin mode;
248
+ // embedded canvas-hmr sockets get ai-activity via the parent's
249
+ // postMessage relay handled above).
250
+ if (msg.type === 'ai-activity') {
251
+ if (myFileKey && msg.file === myFileKey) agentEditing = !!msg.entry;
252
+ return;
253
+ }
254
+ // Phase 13 / DDR-029 — canvas activity overlay. The server pushes
255
+ // `{ type:'activity', file, status, artboard_ids, ts }` as edits land
256
+ // + go idle; the injected runtime (use-canvas-activity.tsx) listens
257
+ // for the `maude:activity` document event. Same-document bridge — no
258
+ // second socket. The `snapshot` seed re-plays the activity map for a
259
+ // tab that opens mid-edit (inspector-origin only).
260
+ if (msg.type === 'activity') {
261
+ // Maintain a running activity seed (shape matches the snapshot's
262
+ // `activity.state`: camelCase artboardIds). Load-bearing for the
263
+ // Phase 13.1 soft-reload: a soft remount resets the activity
264
+ // provider WITHOUT a page reload (so no fresh snapshot), and the
265
+ // provider re-seeds from this map — without it the overlay would
266
+ // vanish on every agent .tsx save.
267
+ if (!window.__maude_activity_seed__) window.__maude_activity_seed__ = {};
268
+ if (msg.status === 'active') {
269
+ window.__maude_activity_seed__[msg.file] = {
270
+ status: 'active', artboardIds: msg.artboard_ids || null, ts: msg.ts,
271
+ };
272
+ } else {
273
+ delete window.__maude_activity_seed__[msg.file];
274
+ }
275
+ document.dispatchEvent(new CustomEvent('maude:activity', { detail: msg }));
276
+ return;
277
+ }
278
+ if (msg.type === 'snapshot' && msg.activity && typeof msg.activity === 'object') {
279
+ // Stash so the provider can seed on mount even if it isn't listening
280
+ // yet (the snapshot can arrive before React mounts — a real race
281
+ // after the HMR reload that a canvas edit triggers). Also dispatch
282
+ // for any provider that IS already mounted (same-origin live case).
283
+ window.__maude_activity_seed__ = msg.activity;
284
+ for (const file in msg.activity) {
285
+ const e = msg.activity[file];
286
+ if (!e) continue;
287
+ document.dispatchEvent(new CustomEvent('maude:activity', {
288
+ detail: { type: 'activity', file: file, status: e.status, artboard_ids: e.artboardIds || null, ts: e.ts },
289
+ }));
290
+ }
291
+ return;
292
+ }
293
+ if (msg.type !== 'canvas-hmr') return;
196
294
  if (msg.mode === 'css') {
197
295
  const v = msg.version || Date.now();
198
296
  // Match by exact filename when we have one; otherwise refresh all.
@@ -222,7 +320,14 @@
222
320
  } else if (msg.mode === 'module' || msg.mode === 'hard') {
223
321
  // Only reload when the change touches *this* canvas (or `_lib`).
224
322
  if (msg.mode === 'hard' || !msg.file || (canvasRel && msg.file === canvasRel)) {
225
- location.reload();
323
+ // Phase 13.1 / DDR-077 — when an agent is live-editing, soft-
324
+ // reload (hold last good on a broken intermediate). Manual edits
325
+ // (or no runtime yet) keep the plain, immediate hard reload.
326
+ if (agentEditing && window.__maudeCanvasRuntime && window.__maudeCanvasRuntime.remount) {
327
+ softReload(msg.version);
328
+ } else {
329
+ location.reload();
330
+ }
226
331
  }
227
332
  } else if (msg.mode === 'meta') {
228
333
  // Phase 8 — canvas-meta sidecar changed. Filter to our own canvas:
@@ -276,7 +381,7 @@
276
381
  document.head.appendChild(layoutLink);
277
382
  }
278
383
 
279
- const canvasUrl = '/' + designRel + '/' + canvasRel;
384
+ // canvasUrl is hoisted above (Phase 13.1) so the HMR soft-reload can use it.
280
385
  // Phase 4 T5 — read the canvas's sibling .meta.json and stash it on
281
386
  // window so DesignCanvas can seed `layout` + `viewport` synchronously.
282
387
  // The fetch runs in parallel with imports; failure is non-fatal (the