@1agh/maude 1.6.12 → 1.6.13

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.
@@ -0,0 +1,288 @@
1
+ // The desktop park — an idle desktop lets its cloud cell sleep.
2
+ //
3
+ // WHY THE CLIENT PARKS, NOT THE CELL. `@cloudflare/containers` renews a cell's
4
+ // activity timer on every proxied request and socket message, and an open
5
+ // proxied socket blocks expiry outright. So a desktop left open holds its cell
6
+ // awake forever: 10.55 instance-hours on the first night after v1.6.12, with
7
+ // nobody working. Forcing the cell to stop under an open socket was rejected
8
+ // (the hub flushes before it closes sockets — sleep is lossless today only
9
+ // because it has always happened with none attached). So the desktop goes
10
+ // quiet on its own: it closes its sockets and its poll, and the cell sleeps
11
+ // through its ordinary `sleepAfter`.
12
+ //
13
+ // This file is the POLICY only — a pure reducer with no timers, no I/O and no
14
+ // clock of its own, in the shape of `apps/render/idle-policy.mjs`. The runtime
15
+ // (`sync/index.ts`) feeds it events and carries out the effects it returns.
16
+ //
17
+ // NEVER PARK BLIND. The decision to park needs a positive answer from the
18
+ // cell's own `/_cell/state` probe first: an old cell (404, or a proxied page)
19
+ // makes the phase `unsupported` for the rest of the session, which is today's
20
+ // behaviour exactly.
21
+
22
+ import { isRuntimeStateRel } from './file-membership.ts';
23
+
24
+ /** Idle this long — no local edit, no studio UI activity — before parking. */
25
+ export const PARK_AFTER_MS = 20 * 60_000;
26
+ /** How often a parked desktop asks the cell whether anything changed. */
27
+ export const PARK_PROBE_MS = 60_000;
28
+ /** The ceiling the probe backs off to while the probe itself keeps failing. */
29
+ export const PARK_PROBE_MAX_MS = 10 * 60_000;
30
+ /**
31
+ * The backstop for a missed change signal: a parked desktop resyncs at least
32
+ * this often even when every probe says nothing changed. A miss costs latency,
33
+ * never data, and this bounds the latency.
34
+ */
35
+ export const PARK_MAX_MS = 6 * 60 * 60_000;
36
+
37
+ /**
38
+ * - `off` — this runtime never parks (a self-hosted hub, a cell's own
39
+ * studio, a hub with no file plane).
40
+ * - `active` — connected as today.
41
+ * - `parked` — sockets and polls closed; probing the cell.
42
+ * - `unsupported` — the cell could not answer the probe contract; never park
43
+ * again this session.
44
+ */
45
+ export type ParkPhase = 'off' | 'active' | 'parked' | 'unsupported';
46
+
47
+ export interface ParkState {
48
+ phase: ParkPhase;
49
+ /** The later of the last local edit, the last UI activity and the last unpark. */
50
+ lastActivityAt: number;
51
+ /** Outbound work in flight — a park is never allowed over it. */
52
+ pending: boolean;
53
+ /** Local clock, when the current park began. */
54
+ parkedAt: number | null;
55
+ /**
56
+ * The cell's own clock (minute-floored) at the probe that allowed this park.
57
+ * A `changedAt` at or after it means somebody else changed something.
58
+ */
59
+ parkMark: number | null;
60
+ probeInFlight: boolean;
61
+ /** When the next probe is due (local clock). */
62
+ nextProbeAt: number | null;
63
+ /** Consecutive failed probes, for the backoff. */
64
+ probeFailures: number;
65
+ }
66
+
67
+ /** What `GET /_cell/state` said, already parsed by the caller. */
68
+ export type ProbeResult =
69
+ | { kind: 'state'; state: 'asleep' | 'running'; changedAt: number | null; at: number | null }
70
+ /** 404, a non-JSON body, a JSON body outside the contract: an old cell. */
71
+ | { kind: 'unsupported' }
72
+ /** No answer at all — offline, DNS, a 5xx. Says nothing about the contract. */
73
+ | { kind: 'network-error' };
74
+
75
+ export type ParkEvent =
76
+ | { type: 'tick'; now: number }
77
+ | { type: 'localEdit'; now: number }
78
+ | { type: 'uiActivity'; now: number }
79
+ | { type: 'pendingChanged'; pending: boolean; now: number }
80
+ | { type: 'probeResult'; result: ProbeResult; now: number };
81
+
82
+ export type ParkEffect = 'park' | 'unpark' | 'probe';
83
+
84
+ /** The thresholds, injectable so a runtime test does not wait 20 minutes. */
85
+ export interface ParkTimings {
86
+ afterMs: number;
87
+ probeMs: number;
88
+ probeMaxMs: number;
89
+ maxParkedMs: number;
90
+ }
91
+
92
+ export const PARK_TIMINGS: ParkTimings = {
93
+ afterMs: PARK_AFTER_MS,
94
+ probeMs: PARK_PROBE_MS,
95
+ probeMaxMs: PARK_PROBE_MAX_MS,
96
+ maxParkedMs: PARK_MAX_MS,
97
+ };
98
+
99
+ export interface ParkStep {
100
+ state: ParkState;
101
+ effects: ParkEffect[];
102
+ }
103
+
104
+ export function initialParkState(opts: { now: number; eligible: boolean }): ParkState {
105
+ return {
106
+ phase: opts.eligible ? 'active' : 'off',
107
+ lastActivityAt: opts.now,
108
+ pending: false,
109
+ parkedAt: null,
110
+ parkMark: null,
111
+ probeInFlight: false,
112
+ nextProbeAt: null,
113
+ probeFailures: 0,
114
+ };
115
+ }
116
+
117
+ /**
118
+ * True when a watcher event on `rel` can be somebody's work: anything outside
119
+ * the runtime-state taxonomy. The ledger's own file (`_state/file-ledger/`),
120
+ * `_canvas-state/`, `_history/` and friends are the studio talking to itself,
121
+ * and must neither trigger a plane pass nor count as presence.
122
+ */
123
+ export function isLocalWorkRel(rel: string): boolean {
124
+ const norm = rel.split('\\').join('/');
125
+ return norm.length > 0 && !isRuntimeStateRel(norm);
126
+ }
127
+
128
+ function unpark(state: ParkState, now: number): ParkStep {
129
+ return {
130
+ state: {
131
+ ...state,
132
+ phase: 'active',
133
+ lastActivityAt: now,
134
+ // A probe still out when the person came back is abandoned by the
135
+ // runtime and never reports; left set, it would block every later park
136
+ // this session (attacker review F2).
137
+ probeInFlight: false,
138
+ parkedAt: null,
139
+ parkMark: null,
140
+ nextProbeAt: null,
141
+ probeFailures: 0,
142
+ },
143
+ effects: ['unpark'],
144
+ };
145
+ }
146
+
147
+ function backoff(failures: number, t: ParkTimings): number {
148
+ return Math.min(t.probeMaxMs, t.probeMs * 2 ** Math.max(0, failures - 1));
149
+ }
150
+
151
+ export function parkReducer(
152
+ state: ParkState,
153
+ event: ParkEvent,
154
+ t: ParkTimings = PARK_TIMINGS
155
+ ): ParkStep {
156
+ if (state.phase === 'off') return { state, effects: [] };
157
+ const now = event.now;
158
+
159
+ switch (event.type) {
160
+ case 'localEdit':
161
+ case 'uiActivity': {
162
+ if (state.phase === 'parked') return unpark(state, now);
163
+ return {
164
+ state: { ...state, lastActivityAt: Math.max(state.lastActivityAt, now) },
165
+ effects: [],
166
+ };
167
+ }
168
+
169
+ case 'pendingChanged': {
170
+ // Work that appears while parked is somebody's work — a doc update with
171
+ // a local origin, a file the plane must push. It goes now.
172
+ if (state.phase === 'parked' && event.pending) {
173
+ return unpark({ ...state, pending: true }, now);
174
+ }
175
+ return { state: { ...state, pending: event.pending }, effects: [] };
176
+ }
177
+
178
+ case 'tick': {
179
+ if (state.phase === 'unsupported') return { state, effects: [] };
180
+ if (state.phase === 'parked') {
181
+ if (state.parkedAt !== null && now - state.parkedAt >= t.maxParkedMs) {
182
+ return unpark(state, now);
183
+ }
184
+ if (state.probeInFlight || (state.nextProbeAt !== null && now < state.nextProbeAt)) {
185
+ return { state, effects: [] };
186
+ }
187
+ return { state: { ...state, probeInFlight: true }, effects: ['probe'] };
188
+ }
189
+ // active
190
+ if (state.pending || state.probeInFlight) return { state, effects: [] };
191
+ if (now - state.lastActivityAt < t.afterMs) return { state, effects: [] };
192
+ if (state.nextProbeAt !== null && now < state.nextProbeAt) return { state, effects: [] };
193
+ // Idle long enough. Ask the cell before parking — never park blind.
194
+ return { state: { ...state, probeInFlight: true }, effects: ['probe'] };
195
+ }
196
+
197
+ case 'probeResult': {
198
+ const result = event.result;
199
+ const settled: ParkState = { ...state, probeInFlight: false };
200
+
201
+ if (result.kind === 'unsupported') {
202
+ const next: ParkState = { ...settled, phase: 'unsupported', nextProbeAt: null };
203
+ // An old cell under a parked desktop: go back to today's behaviour.
204
+ return state.phase === 'parked'
205
+ ? { state: { ...next, parkedAt: null, parkMark: null }, effects: ['unpark'] }
206
+ : { state: next, effects: [] };
207
+ }
208
+
209
+ if (result.kind === 'network-error') {
210
+ const failures = settled.probeFailures + 1;
211
+ // Parked: stay parked and ask again later. Active: do not park on a
212
+ // probe that never answered — try again on the same cadence.
213
+ return {
214
+ state: { ...settled, probeFailures: failures, nextProbeAt: now + backoff(failures, t) },
215
+ effects: [],
216
+ };
217
+ }
218
+
219
+ const ok: ParkState = { ...settled, probeFailures: 0, nextProbeAt: now + t.probeMs };
220
+
221
+ if (state.phase === 'parked') {
222
+ // Somebody else changed something since this park began: the cell is
223
+ // running by definition, and the change has to arrive.
224
+ const mark = state.parkMark;
225
+ if (result.changedAt !== null && mark !== null && result.changedAt >= mark) {
226
+ return unpark(ok, now);
227
+ }
228
+ return { state: ok, effects: [] };
229
+ }
230
+
231
+ if (state.phase !== 'active') return { state: ok, effects: [] };
232
+ // Re-check: an edit or new work may have landed while the probe was out.
233
+ if (ok.pending || now - ok.lastActivityAt < t.afterMs) {
234
+ return { state: { ...ok, nextProbeAt: null }, effects: [] };
235
+ }
236
+ return {
237
+ state: {
238
+ ...ok,
239
+ phase: 'parked',
240
+ parkedAt: now,
241
+ // The cell's clock when possible; the local one floored the same way
242
+ // when the cell did not say. Equal minutes unpark — a same-minute
243
+ // change cannot be told apart, and unparking is the safe direction.
244
+ // A mark from the future (a buggy or hostile hub) would hide every
245
+ // later change; it is not believed beyond a day ahead (F6).
246
+ parkMark:
247
+ result.at !== null && result.at <= now + 86_400_000
248
+ ? result.at
249
+ : Math.floor(now / 60_000) * 60_000,
250
+ },
251
+ effects: ['park'],
252
+ };
253
+ }
254
+ }
255
+ }
256
+
257
+ /**
258
+ * Parse a `/_cell/state` answer. Anything outside the contract is
259
+ * `unsupported` — an old cell proxies `/_cell/state` to its hub, which answers
260
+ * a 404 or a page, and a desktop must read that as "this cell cannot tell me
261
+ * it is asleep", never as "asleep".
262
+ */
263
+ export function parseCellState(
264
+ status: number,
265
+ contentType: string | null,
266
+ body: string
267
+ ): ProbeResult {
268
+ // A 5xx, a timeout, a rate limit or an edge challenge says nothing about the
269
+ // contract — only that this probe did not get through. Reading one as
270
+ // "unsupported" would switch parking off for the session (F6).
271
+ if (status >= 500 || status === 408 || status === 429 || status === 403) {
272
+ return { kind: 'network-error' };
273
+ }
274
+ if (status !== 200) return { kind: 'unsupported' };
275
+ if (!contentType || !/^application\/json\b/i.test(contentType)) return { kind: 'unsupported' };
276
+ let parsed: unknown;
277
+ try {
278
+ parsed = JSON.parse(body);
279
+ } catch {
280
+ return { kind: 'unsupported' };
281
+ }
282
+ if (!parsed || typeof parsed !== 'object') return { kind: 'unsupported' };
283
+ const p = parsed as { state?: unknown; changedAt?: unknown; at?: unknown };
284
+ if (p.state !== 'asleep' && p.state !== 'running') return { kind: 'unsupported' };
285
+ const num = (v: unknown): number | null =>
286
+ typeof v === 'number' && Number.isFinite(v) && v >= 0 ? v : null;
287
+ return { kind: 'state', state: p.state, changedAt: num(p.changedAt), at: num(p.at) };
288
+ }
@@ -50,3 +50,15 @@ export function parsePoke(payload: unknown): Poke | null {
50
50
  if (typeof head !== 'number' || !Number.isInteger(head) || head < 0) return null;
51
51
  return frame.documents === true ? { head, documents: true } : { head };
52
52
  }
53
+
54
+ /**
55
+ * The hub's keep-alive frame (`files-ctl.mjs` `CTL_KEEPALIVE_FRAME`). It exists
56
+ * only so an idle socket is not silent; the receiver ignores it — it is neither
57
+ * a poke nor a malformed frame. Exact match: anything longer or different is
58
+ * still judged by `parsePoke`.
59
+ */
60
+ export const CTL_KEEPALIVE_FRAME = '{"t":"ka"}';
61
+
62
+ export function isKeepalive(payload: unknown): boolean {
63
+ return payload === CTL_KEEPALIVE_FRAME;
64
+ }
@@ -26,6 +26,8 @@ export type SyncPhase =
26
26
  | 'refused'
27
27
  | 'offline'
28
28
  | 'nothing-syncable'
29
+ /** The desktop closed its link on purpose so the cloud project can sleep. */
30
+ | 'parked'
29
31
  /** The link works, but part of the project did not get through. */
30
32
  | 'attention';
31
33
 
@@ -85,6 +87,8 @@ export interface SyncStatusLike extends Partial<SyncStatusSnapshot> {
85
87
  accepted?: unknown;
86
88
  /** An AI action open or held (plan T16) — validated below. */
87
89
  aiAction?: unknown;
90
+ /** The desktop park (`sync/park.ts`) — validated below. */
91
+ parked?: unknown;
88
92
  }
89
93
 
90
94
  /** Hub-supplied text that reaches a UI. Bounded, never markup. */
@@ -456,6 +460,29 @@ export function syncPresentation(
456
460
  if (sourceAttention) return sourceAttention;
457
461
  }
458
462
 
463
+ // PARKED IS A CHOICE, NOT AN OUTAGE. The desktop closed its own link so the
464
+ // cloud project can sleep (`sync/park.ts`); calling that "offline" or
465
+ // "stalled" would be the exact lie DDR-214 exists to end. It ranks below
466
+ // refusals and source conflicts (sticky, they need a person) and above the
467
+ // connection states it deliberately produced.
468
+ const parkedSince = (status.parked as { since?: unknown } | undefined)?.since;
469
+ if (typeof parkedSince === 'number' && Number.isFinite(parkedSince) && parkedSince > 0) {
470
+ // Something waiting on a person (a held change, a blocked file) is still
471
+ // the headline while parked — "Nothing to do" over it would be a lie
472
+ // (attacker review F1). Work that retries on its own never parks at all
473
+ // (`hasSelfRetryingWork`).
474
+ const waiting = attention(false);
475
+ if (waiting) return waiting;
476
+ return {
477
+ phase: 'parked',
478
+ online: false,
479
+ label: 'asleep',
480
+ title: 'Cloud project asleep. Wakes on your next change.',
481
+ next: 'Nothing to do — edit or click anywhere and it reconnects.',
482
+ names: [],
483
+ };
484
+ }
485
+
459
486
  // Otherwise an unreachable hub outranks every count. Whatever the documents
460
487
  // last said, nothing is moving — and "72 synced" over a dead socket is the
461
488
  // exact shape of lie this module exists to stop.
@@ -672,3 +699,21 @@ function readAiAction(raw: unknown): { state: 'open' | 'held'; canvases: number
672
699
  const canvases = Array.isArray(r.canvases) ? Math.min(r.canvases.length, 10_000) : 0;
673
700
  return { state: r.state, canvases };
674
701
  }
702
+
703
+ /**
704
+ * Work that finishes on its own if the link stays up — files or media still
705
+ * moving or failed-and-retrying, a save waiting for the project's answer, an
706
+ * AI edit in progress. The desktop park waits for it (`sync/park.ts`): closing
707
+ * the link over it would stop the retry and leave teammates without the work.
708
+ * Held/blocked items wait on a PERSON, not on time, so they do not block a
709
+ * park (they would keep a cell awake forever); `syncPresentation` still shows
710
+ * them over the parked headline.
711
+ */
712
+ export function hasSelfRetryingWork(status: SyncStatusLike | null | undefined): boolean {
713
+ if (!status) return false;
714
+ const lanes = readLanes(status);
715
+ if (lanes.moving || lanes.failedFiles > 0 || lanes.failedMedia > 0) return true;
716
+ if (readAcceptedPending(status.accepted)) return true;
717
+ const ai = status.aiAction as { state?: unknown } | undefined;
718
+ return ai?.state === 'open';
719
+ }
@@ -96,6 +96,12 @@ export interface SyncStatusPayload extends SyncStatusSnapshot {
96
96
  appliedRevision?: number;
97
97
  /** Plan T16 — an AI action open or held (unfinished). Absent when none. */
98
98
  aiAction?: { state: 'open' | 'held'; label: string; canvases: string[]; since: number };
99
+ /**
100
+ * The desktop parked itself so its cloud cell can sleep (`sync/park.ts`).
101
+ * Deliberate, not a fault: sockets are closed on purpose and the next local
102
+ * change or click brings them back. Absent when not parked.
103
+ */
104
+ parked?: { since: number };
99
105
  }
100
106
 
101
107
  export interface AcceptedSaveStatus {
@@ -254,6 +260,8 @@ export interface SyncStatusStore {
254
260
  noteAppliedRevision(revision: number): void;
255
261
  /** Plan T16 — the AI action stage (null clears it). */
256
262
  updateAiAction(next: SyncStatusPayload['aiAction'] | null): void;
263
+ /** The desktop park (null clears it). */
264
+ updateParked(next: SyncStatusPayload['parked'] | null): void;
257
265
  /** Record a consent-class notice (A7) + persist + broadcast. Idempotent by
258
266
  * `id` — the notice sites fire once per boot, and a repeat is a no-op
259
267
  * rather than a duplicate row. */
@@ -295,6 +303,7 @@ export function createSyncStatusStore(opts: SyncStatusStoreOptions): SyncStatusS
295
303
  let files: FilePlaneStatus | undefined;
296
304
  let accepted: AcceptedSaveStatus | undefined;
297
305
  let aiAction: SyncStatusPayload['aiAction'] | undefined;
306
+ let parked: SyncStatusPayload['parked'] | undefined;
298
307
  const bootAt = now();
299
308
  const coldOpen: { canvasesMs?: number; filesMs?: number } = {};
300
309
  let appliedRevision: number | undefined;
@@ -323,6 +332,7 @@ export function createSyncStatusStore(opts: SyncStatusStoreOptions): SyncStatusS
323
332
  ...(files ? { files } : {}),
324
333
  ...(accepted ? { accepted } : {}),
325
334
  ...(aiAction ? { aiAction } : {}),
335
+ ...(parked ? { parked } : {}),
326
336
  ...(coldOpen.canvasesMs !== undefined || coldOpen.filesMs !== undefined
327
337
  ? { coldOpen: { ...coldOpen } }
328
338
  : {}),
@@ -420,6 +430,10 @@ export function createSyncStatusStore(opts: SyncStatusStoreOptions): SyncStatusS
420
430
  aiAction = next ?? undefined;
421
431
  flush(true);
422
432
  },
433
+ updateParked(next) {
434
+ parked = next ?? undefined;
435
+ flush(true);
436
+ },
423
437
  updateAccepted(next) {
424
438
  const was = accepted;
425
439
  accepted = next;
@@ -1,6 +1,15 @@
1
1
  {
2
2
  "$schema": "./whats-new.schema.json",
3
3
  "entries": [
4
+ {
5
+ "id": "idle-desktop-lets-cloud-sleep",
6
+ "version": "1.6.13",
7
+ "date": "2026-10-05",
8
+ "kind": "improvement",
9
+ "title": "An idle desktop lets your cloud project sleep",
10
+ "summary": "After 20 minutes with no edits or input, the desktop closes its cloud connection and the status bar reads “asleep”. Your next change or click reconnects, and anything teammates changed meanwhile arrives.",
11
+ "surface": "design-ui"
12
+ },
4
13
  {
5
14
  "id": "cloud-loading-states-and-faster-tree",
6
15
  "version": "1.6.5",
package/apps/studio/ws.ts CHANGED
@@ -449,6 +449,10 @@ export function createWs(
449
449
  else if (msg.type === 'tabs' && Array.isArray(msg.tabs)) inspect.setOpenTabs(msg.tabs);
450
450
  else if (msg.type === 'select' && msg.selection) inspect.setSelected(msg.selection);
451
451
  else if (msg.type === 'clear-select') inspect.setSelected(null);
452
+ // A person is at the studio (pointer/keyboard, throttled client-side).
453
+ // Feeds the desktop park (`sync/park.ts`): presence keeps the cloud
454
+ // link up and wakes a parked one. Carries nothing; acts on nothing.
455
+ else if (msg.type === 'presence') ctx.bus.emit('ui:active');
452
456
  else if (msg.type === 'comments-add' && msg.payload) {
453
457
  // In a cell the author is the vouched member, never a claim in the
454
458
  // frame and never the cell's own git identity (the machine).
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@1agh/maude",
3
- "version": "1.6.12",
3
+ "version": "1.6.13",
4
4
  "description": "Marketplace of Claude Code plugins by Michal Dovrtěl: `design` (canvas-first design iteration) + `flow` (generic agentic workflow loop with .ai second brain). Ships the `maude` CLI (with `mdcc` legacy alias) to scaffold workspace, run the design dev server, and manage configs.",
5
5
  "type": "module",
6
6
  "engines": {
@@ -58,13 +58,13 @@
58
58
  "test:e2e:desktop:parity:cloud": "pnpm --filter @maude/desktop-e2e e2e:parity:cloud"
59
59
  },
60
60
  "optionalDependencies": {
61
- "@1agh/maude-darwin-arm64": "1.6.12",
62
- "@1agh/maude-darwin-x64": "1.6.12",
63
- "@1agh/maude-linux-arm64": "1.6.12",
64
- "@1agh/maude-linux-arm64-musl": "1.6.12",
65
- "@1agh/maude-linux-x64": "1.6.12",
66
- "@1agh/maude-linux-x64-musl": "1.6.12",
67
- "@1agh/maude-win32-x64": "1.6.12"
61
+ "@1agh/maude-darwin-arm64": "1.6.13",
62
+ "@1agh/maude-darwin-x64": "1.6.13",
63
+ "@1agh/maude-linux-arm64": "1.6.13",
64
+ "@1agh/maude-linux-arm64-musl": "1.6.13",
65
+ "@1agh/maude-linux-x64": "1.6.13",
66
+ "@1agh/maude-linux-x64-musl": "1.6.13",
67
+ "@1agh/maude-win32-x64": "1.6.13"
68
68
  },
69
69
  "files": [
70
70
  "cli",