@myrialabs/clopen 0.4.34 → 0.4.35

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 (60) hide show
  1. package/backend/auth/permissions.ts +7 -0
  2. package/backend/containers/actions.ts +316 -0
  3. package/backend/containers/disk-usage.test.ts +68 -0
  4. package/backend/containers/disk-usage.ts +170 -0
  5. package/backend/containers/logs.ts +335 -0
  6. package/backend/containers/monitor.ts +410 -0
  7. package/backend/containers/parse.test.ts +599 -0
  8. package/backend/containers/parse.ts +905 -0
  9. package/backend/containers/port-index.test.ts +91 -0
  10. package/backend/containers/port-index.ts +198 -0
  11. package/backend/containers/prune-job.test.ts +148 -0
  12. package/backend/containers/prune-job.ts +129 -0
  13. package/backend/containers/pty-backend.ts +337 -0
  14. package/backend/containers/pty.ts +114 -0
  15. package/backend/containers/runtime.ts +218 -0
  16. package/backend/containers/scan.ts +293 -0
  17. package/backend/files/file-watcher.test.ts +105 -0
  18. package/backend/files/file-watcher.ts +45 -1
  19. package/backend/git/git-service.test.ts +33 -0
  20. package/backend/git/git-service.ts +7 -3
  21. package/backend/git/nested-repos.test.ts +292 -1
  22. package/backend/git/nested-repos.ts +330 -17
  23. package/backend/{ports → host}/runner.ts +19 -10
  24. package/backend/index.ts +11 -2
  25. package/backend/ports/attribute.ts +1 -1
  26. package/backend/ports/kill.ts +1 -1
  27. package/backend/ports/monitor.ts +1 -1
  28. package/backend/ports/processes.ts +1 -1
  29. package/backend/ports/scan-host.ts +77 -8
  30. package/backend/ports/scan.test.ts +1 -1
  31. package/backend/ports/scan.ts +1 -1
  32. package/backend/ports/ssh-lineage.ts +1 -1
  33. package/backend/preview/browser/browser-video-capture.ts +84 -31
  34. package/backend/ws/containers/actions.ts +156 -0
  35. package/backend/ws/containers/index.ts +43 -0
  36. package/backend/ws/containers/list.ts +146 -0
  37. package/backend/ws/containers/logs.ts +43 -0
  38. package/backend/ws/containers/tunnel.ts +101 -0
  39. package/backend/ws/git/log.ts +3 -1
  40. package/backend/ws/git/status.ts +37 -27
  41. package/backend/ws/index.ts +2 -0
  42. package/backend/ws/message-rate-limiter.test.ts +45 -0
  43. package/backend/ws/message-rate-limiter.ts +34 -3
  44. package/backend/ws/ssh/connections.ts +8 -0
  45. package/backend/ws/system/device-info.ts +99 -43
  46. package/dist/assets/{index-vW2RpdHu.js → index-B0iIhSO9.js} +1 -1
  47. package/dist/assets/{index-gGTMFUa8.js → index-BRJqss6F.js} +1 -1
  48. package/dist/assets/main-Buko4D0t.css +1 -0
  49. package/dist/assets/{main-ZCojSzvW.js → main-C_iGUZyL.js} +467 -449
  50. package/dist/index.html +2 -2
  51. package/dist/manifest.json +9 -2
  52. package/package.json +1 -1
  53. package/shared/types/containers/container.ts +366 -0
  54. package/shared/types/containers/index.ts +5 -0
  55. package/shared/types/host/index.ts +18 -0
  56. package/shared/types/ports/port.ts +47 -9
  57. package/shared/utils/logger.ts +2 -0
  58. package/shared/utils/ws-client.ts +67 -12
  59. package/shared/utils/ws-server.ts +64 -13
  60. package/dist/assets/main-CYCMEbXB.css +0 -1
package/dist/index.html CHANGED
@@ -58,8 +58,8 @@
58
58
  }
59
59
  })();
60
60
  </script>
61
- <script type="module" crossorigin src="/assets/main-ZCojSzvW.js"></script>
62
- <link rel="stylesheet" crossorigin href="/assets/main-CYCMEbXB.css">
61
+ <script type="module" crossorigin src="/assets/main-C_iGUZyL.js"></script>
62
+ <link rel="stylesheet" crossorigin href="/assets/main-Buko4D0t.css">
63
63
  </head>
64
64
  <body
65
65
  class="min-h-dvh bg-white text-slate-900 dark:bg-slate-950 dark:text-slate-100 transition-colors duration-200"
@@ -1,16 +1,23 @@
1
1
  {
2
+ "id": "/",
2
3
  "name": "Clopen",
3
4
  "short_name": "Clopen",
4
- "description": "All-in-one web workspace for Claude Code & OpenCode — chat, terminal, git, browser preview, checkpoints, and real-time collaboration",
5
+ "description": "All-in-one web workspace for AI coding agents — Claude Code, OpenCode, Codex, Copilot, Qwen Code, Pi, Cline, and Cursor — with chat, terminal, git, browser preview, database client, checkpoints, and real-time collaboration",
5
6
  "start_url": "/",
7
+ "scope": "/",
8
+ "lang": "en",
9
+ "dir": "ltr",
6
10
  "display": "standalone",
11
+ "orientation": "any",
12
+ "categories": ["developer", "productivity", "utilities"],
7
13
  "background_color": "#0e172b",
8
14
  "theme_color": "#0e172b",
9
15
  "icons": [
10
16
  {
11
17
  "src": "/favicon.svg",
12
18
  "sizes": "any",
13
- "type": "image/svg+xml"
19
+ "type": "image/svg+xml",
20
+ "purpose": "any"
14
21
  }
15
22
  ]
16
23
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@myrialabs/clopen",
3
- "version": "0.4.34",
3
+ "version": "0.4.35",
4
4
  "description": "All-in-one web workspace for AI coding agents — Claude Code, OpenCode, Codex, Copilot, Qwen Code, Pi, Cline, and Cursor — with chat, terminal, git, browser preview, database client, checkpoints, and real-time collaboration",
5
5
  "author": "Myria Labs",
6
6
  "license": "MIT",
@@ -0,0 +1,366 @@
1
+ /**
2
+ * Containers — the shape of "what is running on this host, and how to reach it".
3
+ *
4
+ * One model serves the machine Clopen runs on and any saved SSH host, so the
5
+ * same table, detail pane, log view and shell render either. Docker and Podman
6
+ * answer the same questions in slightly different words; the differences are
7
+ * flattened here rather than pushed into the UI, and anything a runtime cannot
8
+ * report is `null` with the reason carried in `limitations` — never a guess
9
+ * dressed as a fact.
10
+ */
11
+
12
+ import { LOCAL_HOST_ID, type HostId } from '../host';
13
+
14
+ /** The runtimes Clopen knows how to drive. Detected, never configured. */
15
+ export type ContainerRuntime = 'docker' | 'podman';
16
+
17
+ /**
18
+ * Lifecycle states, normalised across both runtimes. `unknown` is kept rather
19
+ * than folded into `exited`: a state Clopen does not recognise must not be
20
+ * rendered as one it does.
21
+ */
22
+ export type ContainerState =
23
+ | 'running'
24
+ | 'paused'
25
+ | 'restarting'
26
+ | 'created'
27
+ | 'exited'
28
+ | 'dead'
29
+ | 'removing'
30
+ | 'unknown';
31
+
32
+ /** A healthcheck verdict. `none` means the image declares no healthcheck. */
33
+ export type ContainerHealth = 'healthy' | 'unhealthy' | 'starting' | 'none';
34
+
35
+ /**
36
+ * One published port. `hostPort` is null when the port is exposed by the image
37
+ * but not published to the host — the container can be reached from its own
38
+ * network and nowhere else, which is worth showing rather than hiding.
39
+ */
40
+ export interface ContainerPortBinding {
41
+ /** The address on the host the port is bound to, e.g. `0.0.0.0`. */
42
+ hostAddress: string | null;
43
+ hostPort: number | null;
44
+ containerPort: number;
45
+ protocol: 'tcp' | 'udp';
46
+ }
47
+
48
+ /** One row in the container table. */
49
+ export interface ContainerEntry {
50
+ /** Stable across scans, so the UI can diff without rebuilding the table. */
51
+ key: string;
52
+ /** Full id, used for every action so a name collision cannot misfire. */
53
+ id: string;
54
+ /** Twelve characters of the id, which is how the runtime prints it. */
55
+ shortId: string;
56
+ name: string;
57
+ image: string;
58
+ state: ContainerState;
59
+ /** The runtime's own words, e.g. `Up 3 hours (healthy)`. */
60
+ statusText: string;
61
+ health: ContainerHealth;
62
+ /** ISO timestamp, or null when the runtime printed a date nothing can parse. */
63
+ createdAt: string | null;
64
+ /** ISO timestamp of the current run; null unless the container is running. */
65
+ startedAt: string | null;
66
+ ports: ContainerPortBinding[];
67
+ /** Set from the compose labels, so a stack reads as a stack. */
68
+ composeProject: string | null;
69
+ composeService: string | null;
70
+ /** The entrypoint command, as the runtime reports it. */
71
+ command: string | null;
72
+ /**
73
+ * False when this host would refuse the action anyway — a container the
74
+ * account cannot touch is shown, but not offered as something to stop.
75
+ */
76
+ canManage: boolean;
77
+ }
78
+
79
+ /** An image on the host. Read-only: building and pulling are out of scope. */
80
+ export interface ContainerImageEntry {
81
+ key: string;
82
+ id: string;
83
+ repository: string;
84
+ tag: string;
85
+ /** Human size exactly as the runtime printed it. */
86
+ size: string;
87
+ createdAt: string | null;
88
+ /** True for `<none>:<none>` layers left behind by a rebuild. */
89
+ dangling: boolean;
90
+ /** Names of containers currently using it, so deleting is never a surprise. */
91
+ usedBy: string[];
92
+ }
93
+
94
+ /** A network on the host. */
95
+ export interface ContainerNetworkEntry {
96
+ key: string;
97
+ id: string;
98
+ name: string;
99
+ driver: string;
100
+ scope: string;
101
+ internal: boolean;
102
+ createdAt: string | null;
103
+ usedBy: string[];
104
+ /**
105
+ * True for the networks the runtime creates itself — `bridge`, `host`,
106
+ * `none`, `podman`. They cannot be removed, and offering to would only
107
+ * produce an error the user cannot act on.
108
+ */
109
+ predefined: boolean;
110
+ }
111
+
112
+ /** A volume on the host. */
113
+ export interface ContainerVolumeEntry {
114
+ key: string;
115
+ name: string;
116
+ driver: string;
117
+ mountpoint: string | null;
118
+ createdAt: string | null;
119
+ usedBy: string[];
120
+ }
121
+
122
+ /**
123
+ * A capability this host could not deliver. Surfaced in the UI so an empty
124
+ * table reads as "this host cannot tell us" rather than "nothing is running".
125
+ */
126
+ export type ContainerLimitationCode =
127
+ | 'no-runtime'
128
+ | 'daemon-unreachable'
129
+ | 'permission-denied'
130
+ | 'images-unavailable'
131
+ | 'volumes-unavailable'
132
+ | 'networks-unavailable'
133
+ | 'no-start-times';
134
+
135
+ export interface ContainerLimitation {
136
+ code: ContainerLimitationCode;
137
+ message: string;
138
+ }
139
+
140
+ /** How the host answered when asked what runtime it has. */
141
+ export interface ContainerRuntimeInfo {
142
+ runtime: ContainerRuntime | null;
143
+ version: string | null;
144
+ /**
145
+ * Why there is no usable runtime. Split out because the three causes need
146
+ * three different things from the user, and "no containers" is the wrong
147
+ * answer to all of them.
148
+ */
149
+ problem: 'none' | 'not-installed' | 'daemon-unreachable' | 'permission-denied';
150
+ /** The runtime's own error, when it gave one. */
151
+ detail: string | null;
152
+ }
153
+
154
+ export interface ContainerScanResult {
155
+ hostId: HostId;
156
+ /** ISO timestamp of the scan that produced this. */
157
+ scannedAt: string;
158
+ runtime: ContainerRuntime | null;
159
+ runtimeVersion: string | null;
160
+ runtimeProblem: ContainerRuntimeInfo['problem'];
161
+ entries: ContainerEntry[];
162
+ images: ContainerImageEntry[];
163
+ volumes: ContainerVolumeEntry[];
164
+ networks: ContainerNetworkEntry[];
165
+ limitations: ContainerLimitation[];
166
+ /** Set when the scan failed outright; the lists are then empty. */
167
+ error: string | null;
168
+ }
169
+
170
+ /** What a row offers to do. All of these are admin-only on the server. */
171
+ export type ContainerAction =
172
+ | 'start'
173
+ | 'stop'
174
+ | 'restart'
175
+ | 'pause'
176
+ | 'unpause'
177
+ | 'remove'
178
+ /** `remove` on something still running: it is stopped first, by the runtime. */
179
+ | 'force-remove';
180
+
181
+ export interface ContainerActionResult {
182
+ ok: boolean;
183
+ error: string | null;
184
+ }
185
+
186
+ /** The four things a host holds that this feature can remove. */
187
+ export type ContainerResourceKind = 'container' | 'image' | 'volume' | 'network';
188
+
189
+ /**
190
+ * The three that are removed as themselves. A container is removed through its
191
+ * own lifecycle instead, because whether it is still running changes both the
192
+ * command and what the confirmation has to say.
193
+ */
194
+ export type RemovableResourceKind = Exclude<ContainerResourceKind, 'container'>;
195
+
196
+ /**
197
+ * What a clean-up sweep can reclaim.
198
+ *
199
+ * Split by what each one costs to get back rather than lumped into one button.
200
+ * Removing a stopped container is nothing; removing an unused image means
201
+ * pulling or building it again, which on a slow connection is an afternoon.
202
+ */
203
+ export type PruneKind =
204
+ | 'containers'
205
+ | 'dangling-images'
206
+ | 'images'
207
+ | 'volumes'
208
+ | 'networks'
209
+ | 'build-cache';
210
+
211
+ export interface PruneOutcome {
212
+ kind: PruneKind;
213
+ ok: boolean;
214
+ /** Entries the runtime listed as removed. Zero is a normal answer. */
215
+ removed: number;
216
+ /** Space the runtime says it freed, in its own words. Null if it did not say. */
217
+ reclaimed: string | null;
218
+ error: string | null;
219
+ }
220
+
221
+ /**
222
+ * A cleanup sweep, as the server remembers it.
223
+ *
224
+ * The sweep belongs to the host, not to the dialog that started it. Closing the
225
+ * dialog — or refreshing the browser, or opening the panel on another device —
226
+ * must not lose track of a `prune` that is still deleting, and must not invite
227
+ * a second one on top of it. So the server owns the job and the dialog attaches
228
+ * to whatever it finds.
229
+ */
230
+ export interface PruneJob {
231
+ hostId: string;
232
+ kinds: PruneKind[];
233
+ startedAt: string;
234
+ /** Null while the sweep is still running. */
235
+ finishedAt: string | null;
236
+ /** One line per kind, filled in when the sweep ends. */
237
+ outcomes: PruneOutcome[] | null;
238
+ }
239
+
240
+ /** One line of `system df`: how much of a resource is in use, and how much is not. */
241
+ export interface ContainerDiskUsageRow {
242
+ kind: 'images' | 'containers' | 'volumes' | 'build-cache';
243
+ total: number | null;
244
+ active: number | null;
245
+ /** Sizes stay as the runtime printed them — it is the authority on its own units. */
246
+ size: string;
247
+ reclaimable: string;
248
+ }
249
+
250
+ export interface ContainerDiskUsage {
251
+ rows: ContainerDiskUsageRow[];
252
+ error: string | null;
253
+ /**
254
+ * When this reading was taken, so the dialog can say how old it is.
255
+ *
256
+ * A reading is shown from cache rather than re-measured on every open,
257
+ * because `system df` is the one container command whose cost is set by how
258
+ * much disk the host holds rather than by how many containers it runs: the
259
+ * daemon walks every image layer and every volume to answer it. On a host
260
+ * with a few hundred volumes on a virtualised filesystem that is over a
261
+ * minute, and almost all of it is the volumes.
262
+ */
263
+ measuredAt: string | null;
264
+ }
265
+
266
+ /**
267
+ * How long each container command may run, and therefore how long a caller has
268
+ * to be prepared to wait for it.
269
+ *
270
+ * Both ends read these. The backend hands them to the runtime as the command
271
+ * budget; the frontend hands them to `ws.http` as the request budget, plus
272
+ * `TRANSPORT_GRACE_MS`. They have to come from one place, because a transport
273
+ * that gives up before the command does turns every slow-but-successful run
274
+ * into a reported failure — a prune that is still deleting, a container that is
275
+ * still stopping — and the caller then has no way to find out it worked.
276
+ */
277
+ export const CONTAINER_TIMEOUTS = {
278
+ /** `stop` waits out the container's grace period, which can be long. */
279
+ action: 120_000,
280
+ /** A prune walks and unlinks everything it removes. */
281
+ prune: 300_000,
282
+ /** One `ps`, `images`, `volume ls` or `network ls`. */
283
+ list: 20_000,
284
+ /** A full listing: the four above, plus detecting the runtime. */
285
+ scan: 90_000,
286
+ /** `stats` samples over a window before it prints. */
287
+ stats: 30_000,
288
+ inspect: 20_000
289
+ } as const;
290
+
291
+ /**
292
+ * The extra the transport allows on top of the command's own budget.
293
+ *
294
+ * Enough to cover the round trip and the runtime's own teardown, so the command
295
+ * is always the thing that gives up first and the caller always hears why.
296
+ */
297
+ export const TRANSPORT_GRACE_MS = 15_000;
298
+
299
+ /**
300
+ * How long a disk reading stays good before the dialog measures again.
301
+ *
302
+ * The figures only move when something is created or removed, and a prune
303
+ * invalidates them outright, so re-measuring on every open would spend a minute
304
+ * of the daemon's time to redraw numbers that had not changed.
305
+ */
306
+ export const DISK_USAGE_STALE_MS = 300_000;
307
+
308
+ /**
309
+ * How long the measurement itself may run before it is abandoned.
310
+ *
311
+ * Nothing waits on this — it is pushed when it lands — so the budget is here to
312
+ * stop a wedged daemon holding a slot forever, not to bound anyone's spinner.
313
+ */
314
+ export const DISK_USAGE_TIMEOUT_MS = 600_000;
315
+
316
+ /** A single sample of what one container is consuming, read on demand. */
317
+ export interface ContainerStats {
318
+ cpuPercent: number | null;
319
+ memoryUsage: string | null;
320
+ memoryPercent: number | null;
321
+ networkIO: string | null;
322
+ blockIO: string | null;
323
+ pids: number | null;
324
+ }
325
+
326
+ /** Everything `inspect` adds to a row, fetched only when a detail pane opens. */
327
+ export interface ContainerDetail {
328
+ id: string;
329
+ name: string;
330
+ image: string;
331
+ imageId: string | null;
332
+ command: string | null;
333
+ createdAt: string | null;
334
+ startedAt: string | null;
335
+ finishedAt: string | null;
336
+ state: ContainerState;
337
+ health: ContainerHealth;
338
+ exitCode: number | null;
339
+ restartCount: number | null;
340
+ restartPolicy: string | null;
341
+ pid: number | null;
342
+ workingDir: string | null;
343
+ user: string | null;
344
+ networks: Array<{ name: string; ipAddress: string | null }>;
345
+ mounts: Array<{ source: string; destination: string; readOnly: boolean; kind: string }>;
346
+ ports: ContainerPortBinding[];
347
+ env: string[];
348
+ labels: Array<{ key: string; value: string }>;
349
+ }
350
+
351
+ /** One push of container output while a log stream is being followed. */
352
+ export interface ContainerLogChunk {
353
+ streamId: string;
354
+ hostId: HostId;
355
+ containerId: string;
356
+ /** Newline-terminated text exactly as the container wrote it. */
357
+ data: string;
358
+ /** Set once when the stream ends, with the reason if it was not asked for. */
359
+ done?: boolean;
360
+ error?: string | null;
361
+ }
362
+
363
+ export const LOCAL_CONTAINER_HOST: HostId = LOCAL_HOST_ID;
364
+
365
+ /** Lines kept per stream. A chatty container must not grow without limit. */
366
+ export const CONTAINER_LOG_BUFFER_LINES = 2000;
@@ -0,0 +1,5 @@
1
+ /**
2
+ * Container manager types barrel export.
3
+ */
4
+
5
+ export * from './container';
@@ -0,0 +1,18 @@
1
+ /**
2
+ * The identity of a machine Clopen can act on.
3
+ *
4
+ * Ports, containers and everything host-scoped that follows them address a
5
+ * machine the same way: `local` is the machine Clopen itself runs on, and any
6
+ * other id is a saved SSH connection. `local` is not a special case — it is
7
+ * simply the host whose id happens to be `local`, which is what lets one store
8
+ * and one scan serve both the More Tools panel and the SSH Client tab.
9
+ */
10
+
11
+ export type HostId = string;
12
+
13
+ export const LOCAL_HOST_ID: HostId = 'local';
14
+
15
+ /** True when this id names the machine Clopen is running on. */
16
+ export function isLocalHost(hostId: HostId): boolean {
17
+ return hostId === LOCAL_HOST_ID;
18
+ }
@@ -8,6 +8,8 @@
8
8
  * than guessing.
9
9
  */
10
10
 
11
+ import { LOCAL_HOST_ID, type HostId } from '../host';
12
+
11
13
  export type PortProtocol = 'tcp' | 'udp';
12
14
 
13
15
  export type PortIpVersion = 'v4' | 'v6';
@@ -60,12 +62,13 @@ export interface PortProcess {
60
62
  export type PortOriginConfidence = 'certain' | 'guess';
61
63
 
62
64
  /**
63
- * The three tiers the UI groups by:
64
- * - `clopen` — a listener Clopen opened, registered by the feature that owns it
65
- * - `session` — a process descended from a Clopen terminal session
66
- * - `external` — everything else on the machine
65
+ * The four tiers the UI groups by:
66
+ * - `clopen` — a listener Clopen opened, registered by the feature that owns it
67
+ * - `session` — a process descended from a Clopen terminal session
68
+ * - `container` — a port a container runtime publishes on this host
69
+ * - `external` — everything else on the machine
67
70
  */
68
- export type PortOriginKind = 'clopen' | 'session' | 'external';
71
+ export type PortOriginKind = 'clopen' | 'session' | 'container' | 'external';
69
72
 
70
73
  /** Which Clopen feature owns a `clopen` port, so it can be stopped properly. */
71
74
  export type PortOwnerFeature =
@@ -94,6 +97,29 @@ export interface PortOrigin {
94
97
  projectId?: string;
95
98
  /** Set when the lineage ends at an sshd session rather than a Clopen one. */
96
99
  remoteSessionUser?: string;
100
+ /** Set when `kind` is `container` — the container publishing this port. */
101
+ containerId?: string;
102
+ }
103
+
104
+ /**
105
+ * The container publishing a port, when one does.
106
+ *
107
+ * A published port is a fact the runtime states outright, so this is never a
108
+ * guess. It is also the only owner worth naming: the listening process is
109
+ * `docker-proxy` or the daemon itself, and with the userland proxy disabled
110
+ * there is no listening socket at all — the port is reached through a firewall
111
+ * rule. Either way the thing to act on is the container.
112
+ */
113
+ export interface PortContainerRef {
114
+ runtime: 'docker' | 'podman';
115
+ /** Full container id, so an action can address it unambiguously. */
116
+ id: string;
117
+ /** Twelve-character id, which is how the runtime's own output reads. */
118
+ shortId: string;
119
+ name: string;
120
+ image: string;
121
+ /** The port inside the container this host port is mapped to. */
122
+ containerPort: number;
97
123
  }
98
124
 
99
125
  /** A peer currently connected to a listening port. */
@@ -109,7 +135,8 @@ export type PortKillBlockedReason =
109
135
  | 'is-clopen-itself'
110
136
  | 'unknown-pid'
111
137
  | 'not-permitted'
112
- | 'system-process';
138
+ | 'system-process'
139
+ | 'managed-by-container';
113
140
 
114
141
  /** One row in the table: a port, its owner, and what is connected to it. */
115
142
  export interface PortEntry {
@@ -138,6 +165,13 @@ export interface PortEntry {
138
165
  * reachable from the public internet.
139
166
  */
140
167
  publicUrl: string | null;
168
+ /**
169
+ * Set when a container runtime publishes this port. Like `publicUrl` this
170
+ * describes how the port is reached rather than replacing whoever holds it,
171
+ * and it is what lets the row point at the container instead of offering to
172
+ * signal a proxy process that would only come back.
173
+ */
174
+ container: PortContainerRef | null;
141
175
  canKill: boolean;
142
176
  killBlockedReason: PortKillBlockedReason | null;
143
177
  }
@@ -158,10 +192,14 @@ export interface PortLimitation {
158
192
  message: string;
159
193
  }
160
194
 
161
- /** `local` is the machine running Clopen; any other id is an SSH connection. */
162
- export type PortHostId = string;
195
+ /**
196
+ * `local` is the machine running Clopen; any other id is an SSH connection.
197
+ * Both names point at the same value: host identity is shared by every
198
+ * host-scoped feature, and ports keep their own spelling only for readability.
199
+ */
200
+ export type PortHostId = HostId;
163
201
 
164
- export const LOCAL_PORT_HOST: PortHostId = 'local';
202
+ export const LOCAL_PORT_HOST: PortHostId = LOCAL_HOST_ID;
165
203
 
166
204
  export interface PortScanResult {
167
205
  hostId: PortHostId;
@@ -48,6 +48,8 @@ export type LogLabel =
48
48
  | 'db-client'
49
49
  | 'ssh'
50
50
  | 'ports'
51
+ | 'containers'
52
+ | 'host'
51
53
 
52
54
  // User
53
55
  | 'user'
@@ -278,6 +278,23 @@ export class WSClient<TAPI extends { client: any; server: any }> {
278
278
  /** Prevents a heal-reconnect stampede when many requests time out at once. */
279
279
  private healingReconnect = false;
280
280
 
281
+ /**
282
+ * When the socket last delivered ANYTHING from the server.
283
+ *
284
+ * This is the evidence that separates the two failures a stalled request can
285
+ * mean, which the recovery path used to conflate. A socket that is still
286
+ * handing us other traffic is demonstrably alive, so a request that has not
287
+ * come back is slow or was lost in transit — re-sending it is the whole fix.
288
+ * A socket that has gone completely silent is the black-holed case reconnect
289
+ * exists for.
290
+ *
291
+ * Treating both as "the connection is dead" is what turned one slow read into
292
+ * a full teardown, and a teardown re-runs every panel's reconnect handler at
293
+ * once — a burst far more expensive than the request that triggered it, and
294
+ * one that made the next timeout more likely rather than less.
295
+ */
296
+ private lastInboundAt = 0;
297
+
281
298
  /** Current context (synced with server) */
282
299
  private context: {
283
300
  userId: string | null;
@@ -376,8 +393,11 @@ export class WSClient<TAPI extends { client: any; server: any }> {
376
393
  }
377
394
 
378
395
  // A fresh socket is live again — clear the heal guard so future
379
- // dead-connection recoveries can fire.
396
+ // dead-connection recoveries can fire, and count the open itself as
397
+ // proof of life so a quiet-but-healthy connection is never mistaken
398
+ // for a black-holed one on its first request.
380
399
  this.healingReconnect = false;
400
+ this.lastInboundAt = Date.now();
381
401
 
382
402
  // Flush queued fire-and-forget messages AFTER context + room
383
403
  // re-joins are synced. Drain into a local array first so a send
@@ -407,6 +427,10 @@ export class WSClient<TAPI extends { client: any; server: any }> {
407
427
  };
408
428
 
409
429
  this.ws.onmessage = (event) => {
430
+ // Proof of life for the recovery path, recorded before anything can
431
+ // throw: what matters is that a frame ARRIVED, not that we managed
432
+ // to handle it.
433
+ this.lastInboundAt = Date.now();
410
434
  try {
411
435
  if (event.data instanceof ArrayBuffer) {
412
436
  // Binary message
@@ -889,7 +913,7 @@ export class WSClient<TAPI extends { client: any; server: any }> {
889
913
  // Reads can be safely re-sent; mutations can only be re-sent if they were
890
914
  // never delivered (tracked via `entry.sent`).
891
915
  const idempotent = isIdempotentHttpAction(actionStr);
892
- // Up to this many automatic heal-reconnect retries for idempotent reads.
916
+ // Up to this many automatic recovery attempts for idempotent reads.
893
917
  const MAX_HEAL_ATTEMPTS = 2;
894
918
 
895
919
  return new Promise((resolve, reject) => {
@@ -920,23 +944,54 @@ export class WSClient<TAPI extends { client: any; server: any }> {
920
944
  }
921
945
  };
922
946
 
923
- // Timeout handler — for idempotent reads, try to heal a dead-but-open
924
- // socket and retry instead of failing outright.
947
+ // Timeout handler — for idempotent reads, recover and retry instead of
948
+ // failing outright.
949
+ //
950
+ // The recovery is now proportionate to the evidence, which it was not
951
+ // before: ANY stalled read forced a full reconnect, so one slow answer
952
+ // from a busy server tore down a connection that was working perfectly
953
+ // and made all fifteen `onWsReconnect` subscribers refetch at once. On a
954
+ // loaded instance that burst is what caused the NEXT timeout, and the
955
+ // loop sustained itself — panels re-showing their skeletons every few
956
+ // seconds for as long as the instance stayed busy.
957
+ //
958
+ // So the socket is only rebuilt when it has actually stopped speaking.
959
+ // While other traffic is still arriving the connection is known-good and
960
+ // re-sending the one request costs a single frame.
925
961
  const onTimeout = () => {
926
962
  if (idempotent && entry.attempts < MAX_HEAL_ATTEMPTS) {
927
963
  entry.attempts++;
928
- debug.warn(
929
- 'websocket',
930
- `Request stalled, healing connection and retrying: ${actionStr} (attempt ${entry.attempts})`
931
- );
932
964
  entry.arm();
933
- // If the socket still claims to be open, it is likely black-holed
934
- // — force a reconnect (guarded against a stampede). The onopen
935
- // resend loop will re-send this request on the fresh socket.
936
- if (this.isSocketOpen() && !this.healingReconnect) {
965
+
966
+ // Silent for a whole timeout window means nothing at all is
967
+ // coming through — a stream, an event, another response — which
968
+ // is the black-hole signature reconnect exists for. Anything
969
+ // less is a connection that works and a request that did not.
970
+ const silentFor = Date.now() - this.lastInboundAt;
971
+ const socketIsBlackHoled = this.isSocketOpen() && silentFor >= timeout;
972
+
973
+ if (socketIsBlackHoled && !this.healingReconnect) {
974
+ debug.warn(
975
+ 'websocket',
976
+ `Socket silent for ${silentFor}ms — reconnecting and retrying: ${actionStr} (attempt ${entry.attempts})`
977
+ );
937
978
  this.healingReconnect = true;
979
+ // The onopen resend loop re-sends this request on the fresh
980
+ // socket, so nothing extra is needed here.
938
981
  this.reconnect();
982
+ return;
983
+ }
984
+
985
+ debug.warn(
986
+ 'websocket',
987
+ `Request stalled but connection is live — re-sending: ${actionStr} (attempt ${entry.attempts})`
988
+ );
989
+ if (this.isSocketOpen()) {
990
+ entry.sent = true;
991
+ this.sendRaw(entry.action, entry.payload);
939
992
  }
993
+ // A socket that is not open needs no action: the request stays in
994
+ // `pendingHttpRequests` and the onopen resend loop delivers it.
940
995
  return;
941
996
  }
942
997
  entry.cleanup();