@myrialabs/clopen 0.4.34 → 0.4.36

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 (125) hide show
  1. package/backend/artifacts/sync.test.ts +63 -0
  2. package/backend/artifacts/sync.ts +56 -3
  3. package/backend/auth/permissions.ts +7 -0
  4. package/backend/containers/actions.ts +316 -0
  5. package/backend/containers/disk-usage.test.ts +68 -0
  6. package/backend/containers/disk-usage.ts +170 -0
  7. package/backend/containers/logs.ts +335 -0
  8. package/backend/containers/monitor.ts +410 -0
  9. package/backend/containers/parse.test.ts +599 -0
  10. package/backend/containers/parse.ts +905 -0
  11. package/backend/containers/port-index.test.ts +91 -0
  12. package/backend/containers/port-index.ts +198 -0
  13. package/backend/containers/prune-job.test.ts +148 -0
  14. package/backend/containers/prune-job.ts +129 -0
  15. package/backend/containers/pty-backend.ts +337 -0
  16. package/backend/containers/pty.ts +114 -0
  17. package/backend/containers/runtime.ts +218 -0
  18. package/backend/containers/scan.ts +293 -0
  19. package/backend/database/migrations/070_create_worktrees_table.ts +67 -0
  20. package/backend/database/migrations/index.ts +7 -0
  21. package/backend/database/queries/index.ts +1 -0
  22. package/backend/database/queries/session-queries.ts +50 -11
  23. package/backend/database/queries/worktree-queries.ts +148 -0
  24. package/backend/engine/adapters/opencode/server.ts +389 -83
  25. package/backend/engine/adapters/opencode/stream.ts +14 -14
  26. package/backend/engine/hot-reload.ts +25 -10
  27. package/backend/files/file-watcher.test.ts +105 -0
  28. package/backend/files/file-watcher.ts +80 -10
  29. package/backend/git/git-service.test.ts +33 -0
  30. package/backend/git/git-service.ts +7 -3
  31. package/backend/git/nested-repos.test.ts +292 -1
  32. package/backend/git/nested-repos.ts +330 -17
  33. package/backend/host/metrics.test.ts +110 -0
  34. package/backend/host/metrics.ts +235 -0
  35. package/backend/{ports → host}/runner.ts +19 -10
  36. package/backend/index.ts +16 -4
  37. package/backend/mcp/internal/servers/browser-automation/context.ts +16 -2
  38. package/backend/ports/attribute.test.ts +3 -2
  39. package/backend/ports/attribute.ts +6 -45
  40. package/backend/ports/kill.ts +1 -1
  41. package/backend/ports/monitor.ts +1 -1
  42. package/backend/ports/processes.ts +1 -1
  43. package/backend/ports/scan-host.ts +77 -8
  44. package/backend/ports/scan.test.ts +1 -1
  45. package/backend/ports/scan.ts +1 -1
  46. package/backend/ports/ssh-lineage.ts +1 -1
  47. package/backend/preview/browser/browser-pool.ts +81 -0
  48. package/backend/preview/browser/browser-preview-service.ts +142 -32
  49. package/backend/preview/browser/browser-tab-manager.ts +350 -113
  50. package/backend/preview/browser/browser-video-capture.ts +84 -31
  51. package/backend/preview/browser/types.ts +9 -0
  52. package/backend/projects/project-cleanup.integration.test.ts +1 -1
  53. package/backend/projects/project-cleanup.ts +7 -1
  54. package/backend/projects/shell-ownership.ts +65 -0
  55. package/backend/snapshot/snapshot-service.ts +6 -2
  56. package/backend/terminal/ptykit.ts +35 -6
  57. package/backend/utils/ws.ts +21 -1
  58. package/backend/worktrees/clone.ts +222 -0
  59. package/backend/worktrees/index.ts +29 -0
  60. package/backend/worktrees/merge.test.ts +93 -0
  61. package/backend/worktrees/merge.ts +232 -0
  62. package/backend/worktrees/paths.test.ts +60 -0
  63. package/backend/worktrees/paths.ts +60 -0
  64. package/backend/worktrees/resolve.ts +67 -0
  65. package/backend/worktrees/service.ts +315 -0
  66. package/backend/worktrees/tree.ts +59 -0
  67. package/backend/ws/access.ts +18 -0
  68. package/backend/ws/artifacts/generate.ts +2 -2
  69. package/backend/ws/chat/stream.ts +9 -2
  70. package/backend/ws/containers/actions.ts +156 -0
  71. package/backend/ws/containers/index.ts +43 -0
  72. package/backend/ws/containers/list.ts +146 -0
  73. package/backend/ws/containers/logs.ts +43 -0
  74. package/backend/ws/containers/tunnel.ts +101 -0
  75. package/backend/ws/files/panel-state-scope.test.ts +102 -0
  76. package/backend/ws/files/path-access.test.ts +93 -1
  77. package/backend/ws/files/path-access.ts +54 -5
  78. package/backend/ws/files/read.ts +15 -10
  79. package/backend/ws/files/search.ts +7 -7
  80. package/backend/ws/files/state.ts +70 -8
  81. package/backend/ws/files/watch.ts +16 -14
  82. package/backend/ws/git/branch-name.ts +3 -3
  83. package/backend/ws/git/branch.ts +15 -15
  84. package/backend/ws/git/commit-message.ts +3 -3
  85. package/backend/ws/git/commit.ts +17 -17
  86. package/backend/ws/git/conflict.ts +10 -10
  87. package/backend/ws/git/diff.ts +9 -9
  88. package/backend/ws/git/ignored.ts +9 -9
  89. package/backend/ws/git/log.ts +6 -4
  90. package/backend/ws/git/remote.ts +43 -43
  91. package/backend/ws/git/staging.ts +16 -16
  92. package/backend/ws/git/status.ts +47 -37
  93. package/backend/ws/index.ts +6 -0
  94. package/backend/ws/message-rate-limiter.test.ts +45 -0
  95. package/backend/ws/message-rate-limiter.ts +34 -3
  96. package/backend/ws/preview/access.ts +11 -3
  97. package/backend/ws/projects/index.ts +5 -1
  98. package/backend/ws/projects/info.test.ts +54 -0
  99. package/backend/ws/projects/info.ts +398 -0
  100. package/backend/ws/sessions/crud.ts +21 -6
  101. package/backend/ws/snapshot/restore.ts +11 -14
  102. package/backend/ws/ssh/connections.ts +8 -0
  103. package/backend/ws/system/device-info.ts +70 -96
  104. package/backend/ws/worktrees/crud.ts +187 -0
  105. package/backend/ws/worktrees/index.ts +33 -0
  106. package/backend/ws/worktrees/transfer.ts +88 -0
  107. package/dist/assets/{index-vW2RpdHu.js → index-Bg-SUCFg.js} +1 -1
  108. package/dist/assets/{index-gGTMFUa8.js → index-Cz4YpHVi.js} +1 -1
  109. package/dist/assets/main-9G7IpQpK.css +1 -0
  110. package/dist/assets/main-BofFsH8E.js +1085 -0
  111. package/dist/index.html +2 -2
  112. package/dist/manifest.json +9 -2
  113. package/package.json +1 -1
  114. package/shared/types/containers/container.ts +366 -0
  115. package/shared/types/containers/index.ts +5 -0
  116. package/shared/types/database/schema.ts +26 -0
  117. package/shared/types/host/index.ts +18 -0
  118. package/shared/types/ports/port.ts +47 -9
  119. package/shared/utils/logger.ts +3 -0
  120. package/shared/utils/workspace-scope.test.ts +80 -0
  121. package/shared/utils/workspace-scope.ts +57 -0
  122. package/shared/utils/ws-client.ts +92 -17
  123. package/shared/utils/ws-server.ts +83 -16
  124. package/dist/assets/main-CYCMEbXB.css +0 -1
  125. package/dist/assets/main-ZCojSzvW.js +0 -1060
@@ -3,7 +3,15 @@ import { mkdtemp, rm, writeFile, mkdir } from 'node:fs/promises';
3
3
  import { tmpdir } from 'node:os';
4
4
  import path from 'node:path';
5
5
  import { execGit } from './git-executor';
6
- import { findNestedRepoPaths, findRepoForFile } from './nested-repos';
6
+ import {
7
+ beginWatchedDiscovery,
8
+ clearNestedRepoCache,
9
+ endWatchedDiscovery,
10
+ findNestedRepoPaths,
11
+ findRepoForFile,
12
+ invalidateRepoSet,
13
+ notifyPathChanged
14
+ } from './nested-repos';
7
15
 
8
16
  /**
9
17
  * Sub-repo detection used to walk the whole tree and report every directory
@@ -53,6 +61,10 @@ async function detected(): Promise<string[]> {
53
61
  beforeEach(async () => {
54
62
  root = await mkdtemp(path.join(tmpdir(), 'clopen-nested-'));
55
63
  await initRepo(root);
64
+ // The walk is cached for seconds; these tests rebuild a project's layout in
65
+ // milliseconds. Without this a fixture would be asserted against the
66
+ // previous test's tree whenever mkdtemp hands back a reused path.
67
+ clearNestedRepoCache();
56
68
  });
57
69
 
58
70
  afterEach(async () => {
@@ -206,6 +218,285 @@ describe('findNestedRepoPaths', () => {
206
218
  });
207
219
  });
208
220
 
221
+ describe('walk cache', () => {
222
+ // The walk is the expensive half of discovery (readdir over the whole tree
223
+ // plus a `git check-ignore` spawn per level), and the Git panel fires it
224
+ // several times a second: staging triggers `git:status` and `git:branches`,
225
+ // and the `.git` watcher fires `git:changed` right behind them. On Windows
226
+ // each of those spawns is slow enough that the panel visibly froze.
227
+
228
+ test('reuses the walk instead of re-scanning the tree', async () => {
229
+ expect(await detected()).toEqual([]);
230
+
231
+ // Created after the walk was cached, so it must not be visible yet:
232
+ // this is what proves the second call did not re-scan.
233
+ await initRepo(path.join(root, 'packages/widget'));
234
+ expect(await detected()).toEqual([]);
235
+
236
+ clearNestedRepoCache();
237
+ expect(await detected()).toEqual(['packages/widget']);
238
+ });
239
+
240
+ test('still reports a cached provisional repo the moment it gains work', async () => {
241
+ // The reason only the walk is cached. A repo inside an ignored tree is
242
+ // held back until it has something to show; if the filtered list were
243
+ // cached too, the sub-repo an agent just wrote into would stay missing
244
+ // from the Changes tab for the rest of the TTL.
245
+ await writeFile(path.join(root, '.gitignore'), 'themes/\n');
246
+ await initRepo(path.join(root, 'themes/mytheme'));
247
+
248
+ expect(await detected()).toEqual([]);
249
+
250
+ await addLocalWork(path.join(root, 'themes/mytheme'));
251
+ expect(await detected()).toEqual(['themes/mytheme']);
252
+ });
253
+
254
+ test('callers arriving during a walk share it and get their own array', async () => {
255
+ await initRepo(path.join(root, 'packages/widget'));
256
+
257
+ const [first, second] = await Promise.all([
258
+ findNestedRepoPaths(root),
259
+ findNestedRepoPaths(root)
260
+ ]);
261
+
262
+ expect(second).toEqual(first);
263
+ // Separate arrays — `git:status` mutates the list it is handed, and it
264
+ // runs concurrently with `git:branches` on the same root.
265
+ expect(second).not.toBe(first);
266
+ });
267
+
268
+ test('scopes clearing to one root', async () => {
269
+ const other = await mkdtemp(path.join(tmpdir(), 'clopen-nested-other-'));
270
+ try {
271
+ await initRepo(other);
272
+ expect(await detected()).toEqual([]);
273
+ await findNestedRepoPaths(other);
274
+
275
+ await initRepo(path.join(root, 'packages/widget'));
276
+ await initRepo(path.join(other, 'packages/widget'));
277
+
278
+ clearNestedRepoCache(root);
279
+ expect(await detected()).toEqual(['packages/widget']);
280
+ expect(await findNestedRepoPaths(other)).toEqual([]);
281
+ } finally {
282
+ await rm(other, { recursive: true, force: true });
283
+ }
284
+ });
285
+ });
286
+
287
+ describe('watcher-driven discovery', () => {
288
+ // Discovery used to be re-derived on every `git:status` and `git:branches`:
289
+ // an `fs.readdir` over the whole tree, a `git check-ignore` spawn per repo
290
+ // per depth level, then a `git status` spawn per repo inside an ignored
291
+ // tree. The panel fires that three or four times a second while staging,
292
+ // and on Windows every spawn pays for Defender.
293
+ //
294
+ // The file watcher already knows when any of it changes, so it — not a
295
+ // clock — decides when an answer is stale. These tests pin the contract
296
+ // both ways: a watched root reuses answers, and every signal that could
297
+ // change one puts it back.
298
+
299
+ /** Count git subcommands spawned while `run` executes. */
300
+ async function countSpawns<T>(run: () => Promise<T>): Promise<{ result: T; spawns: string[] }> {
301
+ const spawns: string[] = [];
302
+ const realSpawn = Bun.spawn;
303
+ // argv is [git, -c, safe.directory=…, <subcommand>, …]
304
+ // @ts-expect-error test instrumentation
305
+ Bun.spawn = (cmd: string[], options: unknown) => {
306
+ if (Array.isArray(cmd) && cmd[3]) spawns.push(cmd[3]);
307
+ // @ts-expect-error passthrough
308
+ return realSpawn(cmd, options);
309
+ };
310
+ try {
311
+ return { result: await run(), spawns };
312
+ } finally {
313
+ Bun.spawn = realSpawn;
314
+ }
315
+ }
316
+
317
+ test('a watched root answers a repeat call without touching git at all', async () => {
318
+ // The WordPress shape the detection policy was written for: every plugin
319
+ // is its own repo inside a tree the project ignores, so every one of them
320
+ // needs a `git status` to prove it has something to show. Unwatched, that
321
+ // is a spawn per plugin on every single call.
322
+ await writeFile(path.join(root, '.gitignore'), 'wp-content/plugins/\n');
323
+ for (const name of ['alpha', 'beta', 'gamma']) {
324
+ const plugin = path.join(root, 'wp-content/plugins', name);
325
+ await initRepo(plugin);
326
+ await addLocalWork(plugin);
327
+ }
328
+
329
+ beginWatchedDiscovery(root);
330
+ expect(await detected()).toEqual([
331
+ 'wp-content/plugins/alpha',
332
+ 'wp-content/plugins/beta',
333
+ 'wp-content/plugins/gamma'
334
+ ]);
335
+
336
+ const { result, spawns } = await countSpawns(() => detected());
337
+ expect(result).toEqual([
338
+ 'wp-content/plugins/alpha',
339
+ 'wp-content/plugins/beta',
340
+ 'wp-content/plugins/gamma'
341
+ ]);
342
+ expect(spawns).toEqual([]);
343
+ });
344
+
345
+ test('an unwatched root keeps probing live, so nothing can hide behind a guess', async () => {
346
+ // No watcher means no signal, and a cached "nothing to show" would hide a
347
+ // sub-repo the user is working in — the worst failure this module has.
348
+ await writeFile(path.join(root, '.gitignore'), 'themes/\n');
349
+ await initRepo(path.join(root, 'themes/mytheme'));
350
+ await addLocalWork(path.join(root, 'themes/mytheme'));
351
+
352
+ expect(await detected()).toEqual(['themes/mytheme']);
353
+
354
+ const { spawns } = await countSpawns(() => detected());
355
+ expect(spawns).toContain('status');
356
+ });
357
+
358
+ test('a write into a pristine sub-repo puts it back on the list', async () => {
359
+ // The transition the containment matching exists for. While `mytheme` is
360
+ // pristine it is deliberately unreported, so the watcher has never
361
+ // resolved a git dir for it and cannot name it. The only place it is
362
+ // known is the verdict cache holding the `false` that is about to be
363
+ // wrong — which is why invalidation matches by containment.
364
+ await writeFile(path.join(root, '.gitignore'), 'themes/\n');
365
+ await initRepo(path.join(root, 'themes/mytheme'));
366
+
367
+ beginWatchedDiscovery(root);
368
+ expect(await detected()).toEqual([]);
369
+
370
+ const edited = path.join(root, 'themes/mytheme/style.css');
371
+ await writeFile(edited, 'body { color: red }\n');
372
+ notifyPathChanged(root, edited);
373
+
374
+ expect(await detected()).toEqual(['themes/mytheme']);
375
+ });
376
+
377
+ test('a sibling sub-repo keeps its verdict when one of them is written to', async () => {
378
+ await writeFile(path.join(root, '.gitignore'), 'themes/\n');
379
+ for (const name of ['alpha', 'beta']) {
380
+ const theme = path.join(root, 'themes', name);
381
+ await initRepo(theme);
382
+ await addLocalWork(theme);
383
+ }
384
+
385
+ beginWatchedDiscovery(root);
386
+ expect(await detected()).toEqual(['themes/alpha', 'themes/beta']);
387
+
388
+ const edited = path.join(root, 'themes/alpha/style.css');
389
+ await writeFile(edited, 'body {}\n');
390
+ notifyPathChanged(root, edited);
391
+
392
+ // Only alpha is re-probed; beta answers from its cached verdict.
393
+ const { spawns } = await countSpawns(() => detected());
394
+ expect(spawns.filter((cmd) => cmd === 'status')).toHaveLength(1);
395
+ });
396
+
397
+ test('a per-repo exclude file moves the boundary like .gitignore does', async () => {
398
+ // `.git/info/exclude` is obeyed by `git check-ignore` exactly like
399
+ // `.gitignore`, so it decides whether a sub-repo has to prove local work.
400
+ // The watcher deliberately filters it out of the panel's git-state
401
+ // events — the panel has nothing to show for it — so discovery has to
402
+ // recognise it by name rather than ride along with those.
403
+ await initRepo(path.join(root, 'themes/mytheme'));
404
+
405
+ beginWatchedDiscovery(root);
406
+ expect(await detected()).toEqual(['themes/mytheme']);
407
+
408
+ await mkdir(path.join(root, '.git/info'), { recursive: true });
409
+ const exclude = path.join(root, '.git/info/exclude');
410
+ await writeFile(exclude, 'themes/\n');
411
+ notifyPathChanged(root, exclude);
412
+
413
+ // Now inside an ignored tree and pristine, so it drops off the list.
414
+ expect(await detected()).toEqual([]);
415
+ });
416
+
417
+ test('a .git appearing is itself a repo-set change', async () => {
418
+ beginWatchedDiscovery(root);
419
+ expect(await detected()).toEqual([]);
420
+
421
+ const widget = path.join(root, 'packages/widget');
422
+ await initRepo(widget);
423
+ notifyPathChanged(root, path.join(widget, '.git'));
424
+
425
+ expect(await detected()).toEqual(['packages/widget']);
426
+ });
427
+
428
+ test('an ordinary write is not treated as a repo-set change', async () => {
429
+ // The counterweight to the rules above: if every path invalidated the
430
+ // walk, the cache would do nothing at all during a build.
431
+ beginWatchedDiscovery(root);
432
+ expect(await detected()).toEqual([]);
433
+
434
+ await initRepo(path.join(root, 'packages/widget'));
435
+ const ordinary = path.join(root, 'src/app.ts');
436
+ await mkdir(path.join(root, 'src'), { recursive: true });
437
+ await writeFile(ordinary, 'export {}\n');
438
+ notifyPathChanged(root, ordinary);
439
+
440
+ expect(await detected()).toEqual([]);
441
+ });
442
+
443
+ test('ignores signals for a root nobody is watching', async () => {
444
+ await initRepo(path.join(root, 'packages/widget'));
445
+ // Never claimed, so there is no cached answer a signal could correct and
446
+ // nothing here should pretend otherwise.
447
+ notifyPathChanged(root, path.join(root, '.gitignore'));
448
+ expect(await detected()).toEqual(['packages/widget']);
449
+ });
450
+
451
+ test('two watchers on one root: the first to leave does not revoke the claim', async () => {
452
+ // Two projects can be opened at the same directory. Releasing on the
453
+ // first close would put discovery back on the clock while a perfectly
454
+ // good signal source was still running.
455
+ await writeFile(path.join(root, '.gitignore'), 'themes/\n');
456
+ await initRepo(path.join(root, 'themes/mytheme'));
457
+ await addLocalWork(path.join(root, 'themes/mytheme'));
458
+
459
+ beginWatchedDiscovery(root);
460
+ beginWatchedDiscovery(root);
461
+ expect(await detected()).toEqual(['themes/mytheme']);
462
+
463
+ endWatchedDiscovery(root);
464
+ const stillWatched = await countSpawns(() => detected());
465
+ expect(stillWatched.spawns).toEqual([]);
466
+
467
+ endWatchedDiscovery(root);
468
+ const released = await countSpawns(() => detected());
469
+ expect(released.spawns).toContain('status');
470
+ });
471
+
472
+ test('invalidating the repo set re-walks the tree', async () => {
473
+ beginWatchedDiscovery(root);
474
+ expect(await detected()).toEqual([]);
475
+
476
+ await initRepo(path.join(root, 'packages/widget'));
477
+ expect(await detected()).toEqual([]);
478
+
479
+ invalidateRepoSet(root);
480
+ expect(await detected()).toEqual(['packages/widget']);
481
+ });
482
+
483
+ test('losing the watcher drops every answer it was backing', async () => {
484
+ // Nothing will invalidate from here on, so a verdict kept across this
485
+ // boundary would never be corrected.
486
+ await writeFile(path.join(root, '.gitignore'), 'themes/\n');
487
+ await initRepo(path.join(root, 'themes/mytheme'));
488
+ await addLocalWork(path.join(root, 'themes/mytheme'));
489
+
490
+ beginWatchedDiscovery(root);
491
+ expect(await detected()).toEqual(['themes/mytheme']);
492
+
493
+ endWatchedDiscovery(root);
494
+ const { spawns } = await countSpawns(() => detected());
495
+ expect(spawns).toContain('check-ignore');
496
+ expect(spawns).toContain('status');
497
+ });
498
+ });
499
+
209
500
  describe('findRepoForFile', () => {
210
501
  test('routes a file to the deepest owning repo', async () => {
211
502
  await initRepo(path.join(root, 'packages/widget'));