@myrialabs/clopen 0.4.35 → 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 (82) hide show
  1. package/backend/artifacts/sync.test.ts +63 -0
  2. package/backend/artifacts/sync.ts +56 -3
  3. package/backend/database/migrations/070_create_worktrees_table.ts +67 -0
  4. package/backend/database/migrations/index.ts +7 -0
  5. package/backend/database/queries/index.ts +1 -0
  6. package/backend/database/queries/session-queries.ts +50 -11
  7. package/backend/database/queries/worktree-queries.ts +148 -0
  8. package/backend/engine/adapters/opencode/server.ts +389 -83
  9. package/backend/engine/adapters/opencode/stream.ts +14 -14
  10. package/backend/engine/hot-reload.ts +25 -10
  11. package/backend/files/file-watcher.ts +35 -9
  12. package/backend/host/metrics.test.ts +110 -0
  13. package/backend/host/metrics.ts +235 -0
  14. package/backend/index.ts +5 -2
  15. package/backend/mcp/internal/servers/browser-automation/context.ts +16 -2
  16. package/backend/ports/attribute.test.ts +3 -2
  17. package/backend/ports/attribute.ts +5 -44
  18. package/backend/preview/browser/browser-pool.ts +81 -0
  19. package/backend/preview/browser/browser-preview-service.ts +142 -32
  20. package/backend/preview/browser/browser-tab-manager.ts +350 -113
  21. package/backend/preview/browser/types.ts +9 -0
  22. package/backend/projects/project-cleanup.integration.test.ts +1 -1
  23. package/backend/projects/project-cleanup.ts +7 -1
  24. package/backend/projects/shell-ownership.ts +65 -0
  25. package/backend/snapshot/snapshot-service.ts +6 -2
  26. package/backend/terminal/ptykit.ts +35 -6
  27. package/backend/utils/ws.ts +21 -1
  28. package/backend/worktrees/clone.ts +222 -0
  29. package/backend/worktrees/index.ts +29 -0
  30. package/backend/worktrees/merge.test.ts +93 -0
  31. package/backend/worktrees/merge.ts +232 -0
  32. package/backend/worktrees/paths.test.ts +60 -0
  33. package/backend/worktrees/paths.ts +60 -0
  34. package/backend/worktrees/resolve.ts +67 -0
  35. package/backend/worktrees/service.ts +315 -0
  36. package/backend/worktrees/tree.ts +59 -0
  37. package/backend/ws/access.ts +18 -0
  38. package/backend/ws/artifacts/generate.ts +2 -2
  39. package/backend/ws/chat/stream.ts +9 -2
  40. package/backend/ws/files/panel-state-scope.test.ts +102 -0
  41. package/backend/ws/files/path-access.test.ts +93 -1
  42. package/backend/ws/files/path-access.ts +54 -5
  43. package/backend/ws/files/read.ts +15 -10
  44. package/backend/ws/files/search.ts +7 -7
  45. package/backend/ws/files/state.ts +70 -8
  46. package/backend/ws/files/watch.ts +16 -14
  47. package/backend/ws/git/branch-name.ts +3 -3
  48. package/backend/ws/git/branch.ts +15 -15
  49. package/backend/ws/git/commit-message.ts +3 -3
  50. package/backend/ws/git/commit.ts +17 -17
  51. package/backend/ws/git/conflict.ts +10 -10
  52. package/backend/ws/git/diff.ts +9 -9
  53. package/backend/ws/git/ignored.ts +9 -9
  54. package/backend/ws/git/log.ts +3 -3
  55. package/backend/ws/git/remote.ts +43 -43
  56. package/backend/ws/git/staging.ts +16 -16
  57. package/backend/ws/git/status.ts +13 -13
  58. package/backend/ws/index.ts +4 -0
  59. package/backend/ws/preview/access.ts +11 -3
  60. package/backend/ws/projects/index.ts +5 -1
  61. package/backend/ws/projects/info.test.ts +54 -0
  62. package/backend/ws/projects/info.ts +398 -0
  63. package/backend/ws/sessions/crud.ts +21 -6
  64. package/backend/ws/snapshot/restore.ts +11 -14
  65. package/backend/ws/system/device-info.ts +62 -144
  66. package/backend/ws/worktrees/crud.ts +187 -0
  67. package/backend/ws/worktrees/index.ts +33 -0
  68. package/backend/ws/worktrees/transfer.ts +88 -0
  69. package/dist/assets/{index-B0iIhSO9.js → index-Bg-SUCFg.js} +1 -1
  70. package/dist/assets/{index-BRJqss6F.js → index-Cz4YpHVi.js} +1 -1
  71. package/dist/assets/main-9G7IpQpK.css +1 -0
  72. package/dist/assets/main-BofFsH8E.js +1085 -0
  73. package/dist/index.html +2 -2
  74. package/package.json +1 -1
  75. package/shared/types/database/schema.ts +26 -0
  76. package/shared/utils/logger.ts +1 -0
  77. package/shared/utils/workspace-scope.test.ts +80 -0
  78. package/shared/utils/workspace-scope.ts +57 -0
  79. package/shared/utils/ws-client.ts +25 -5
  80. package/shared/utils/ws-server.ts +19 -3
  81. package/dist/assets/main-Buko4D0t.css +0 -1
  82. package/dist/assets/main-C_iGUZyL.js +0 -1078
package/dist/index.html CHANGED
@@ -58,8 +58,8 @@
58
58
  }
59
59
  })();
60
60
  </script>
61
- <script type="module" crossorigin src="/assets/main-C_iGUZyL.js"></script>
62
- <link rel="stylesheet" crossorigin href="/assets/main-Buko4D0t.css">
61
+ <script type="module" crossorigin src="/assets/main-BofFsH8E.js"></script>
62
+ <link rel="stylesheet" crossorigin href="/assets/main-9G7IpQpK.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"
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@myrialabs/clopen",
3
- "version": "0.4.35",
3
+ "version": "0.4.36",
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",
@@ -14,6 +14,31 @@ export interface Project {
14
14
  default_profile_id?: number | null; // Shared per-project default Profile (null = none)
15
15
  }
16
16
 
17
+ /** Isolation status of a worktree copy. */
18
+ export type WorktreeStatus = 'active' | 'applied' | 'archived';
19
+
20
+ /** How the worktree directory was materialised from the main tree. */
21
+ export type WorktreeCloneMode = 'reflink' | 'copy';
22
+
23
+ /**
24
+ * An isolated copy of a project that sessions can run in. `base_tree` is the
25
+ * merge base — a JSON TreeMap of the main tree at clone time.
26
+ */
27
+ export interface Worktree {
28
+ id: string;
29
+ project_id: string;
30
+ name: string;
31
+ slug: string;
32
+ path: string;
33
+ status: WorktreeStatus;
34
+ clone_mode: WorktreeCloneMode;
35
+ base_tree: string;
36
+ created_by?: string | null;
37
+ created_at: string;
38
+ last_opened_at?: string | null;
39
+ last_applied_at?: string | null;
40
+ }
41
+
17
42
  export interface ChatSession {
18
43
  // ── Identity ──
19
44
  id: string;
@@ -31,6 +56,7 @@ export interface ChatSession {
31
56
  account_name?: string; // Display name of the selected account
32
57
  profile_id?: number | null; // Active Profile bundle for this session (null = use project default / none)
33
58
  reasoning_effort?: string | null; // Reasoning/thinking level token (native per engine; null = engine default)
59
+ worktree_id?: string | null; // Isolated worktree this session runs in (null = main project tree)
34
60
 
35
61
  // ── HEAD state (re-derived when HEAD changes: undo/redo/restore/branch) ──
36
62
  head_message_id?: string; // Git-like HEAD pointer to current branch tip
@@ -10,6 +10,7 @@ export type LogLabel =
10
10
  | 'chat'
11
11
  | 'checkpoint'
12
12
  | 'snapshot'
13
+ | 'worktree'
13
14
 
14
15
  // Preview
15
16
  | 'preview'
@@ -0,0 +1,80 @@
1
+ import { describe, expect, test } from 'bun:test';
2
+ import { isScopeOfProject, makeScopeKey, parseScopeKey, scopeProjectId, scopeSlug } from './workspace-scope';
3
+
4
+ const PROJECT = '0f8c2a1e-1111-4a2b-9c3d-000000000001';
5
+ const WORKTREE = '5b7d3c9f-2222-4e6a-8b1c-000000000002';
6
+
7
+ describe('makeScopeKey', () => {
8
+ test('is the project id itself for the main tree', () => {
9
+ expect(makeScopeKey(PROJECT, null)).toBe(PROJECT);
10
+ expect(makeScopeKey(PROJECT)).toBe(PROJECT);
11
+ expect(makeScopeKey(PROJECT, '')).toBe(PROJECT);
12
+ });
13
+
14
+ test('joins project and worktree for a worktree', () => {
15
+ expect(makeScopeKey(PROJECT, WORKTREE)).toBe(`${PROJECT}~${WORKTREE}`);
16
+ });
17
+ });
18
+
19
+ describe('parseScopeKey', () => {
20
+ test('reads back a main-tree key', () => {
21
+ expect(parseScopeKey(PROJECT)).toEqual({ projectId: PROJECT, worktreeId: null });
22
+ });
23
+
24
+ test('reads back a worktree key', () => {
25
+ expect(parseScopeKey(makeScopeKey(PROJECT, WORKTREE))).toEqual({
26
+ projectId: PROJECT,
27
+ worktreeId: WORKTREE
28
+ });
29
+ });
30
+
31
+ test('treats a trailing separator as the main tree', () => {
32
+ expect(parseScopeKey(`${PROJECT}~`)).toEqual({ projectId: PROJECT, worktreeId: null });
33
+ });
34
+ });
35
+
36
+ describe('scopeProjectId', () => {
37
+ test('is what every access check runs against', () => {
38
+ expect(scopeProjectId(makeScopeKey(PROJECT, WORKTREE))).toBe(PROJECT);
39
+ expect(scopeProjectId(PROJECT)).toBe(PROJECT);
40
+ });
41
+ });
42
+
43
+ describe('isScopeOfProject', () => {
44
+ test('matches the project and each of its worktrees', () => {
45
+ expect(isScopeOfProject(PROJECT, PROJECT)).toBe(true);
46
+ expect(isScopeOfProject(makeScopeKey(PROJECT, WORKTREE), PROJECT)).toBe(true);
47
+ });
48
+
49
+ test('rejects another project', () => {
50
+ const other = '9999aaaa-3333-4c4d-8e5f-000000000003';
51
+ expect(isScopeOfProject(makeScopeKey(other, WORKTREE), PROJECT)).toBe(false);
52
+ });
53
+
54
+ test('rejects a project id that merely shares a prefix', () => {
55
+ expect(isScopeOfProject(`${PROJECT}-extra`, PROJECT)).toBe(false);
56
+ });
57
+ });
58
+
59
+ describe('scopeSlug', () => {
60
+ test('distinguishes a worktree from its project', () => {
61
+ expect(scopeSlug(PROJECT)).not.toBe(scopeSlug(makeScopeKey(PROJECT, WORKTREE)));
62
+ });
63
+
64
+ test('distinguishes two worktrees of the same project', () => {
65
+ const other = 'aaaa1111-4444-4f7b-9d2e-000000000004';
66
+ expect(scopeSlug(makeScopeKey(PROJECT, WORKTREE))).not.toBe(
67
+ scopeSlug(makeScopeKey(PROJECT, other))
68
+ );
69
+ });
70
+
71
+ test('is stable and alphanumeric so it survives id sanitisation', () => {
72
+ const slug = scopeSlug(makeScopeKey(PROJECT, WORKTREE));
73
+ expect(slug).toBe(scopeSlug(makeScopeKey(PROJECT, WORKTREE)));
74
+ expect(slug).toMatch(/^[a-zA-Z0-9]+$/);
75
+ });
76
+
77
+ test('keeps the main-tree token short and project-derived', () => {
78
+ expect(scopeSlug(PROJECT)).toBe('0f8c2a1e');
79
+ });
80
+ });
@@ -0,0 +1,57 @@
1
+ /**
2
+ * Workspace scope keys.
3
+ *
4
+ * Terminal sessions and preview tabs are keyed per workspace, not per project:
5
+ * a worktree exists so its work stays separate, and sharing those keys with the
6
+ * main tree would leak one task's shells and tabs into another's.
7
+ *
8
+ * Access is always decided on the project id, so the key stays parseable back
9
+ * to it rather than becoming an opaque identifier of its own.
10
+ */
11
+
12
+ /** Separator that cannot appear in a UUID, so parsing is unambiguous. */
13
+ const SCOPE_SEPARATOR = '~';
14
+
15
+ /** Key for a workspace: the project itself, or one of its worktrees. */
16
+ export function makeScopeKey(projectId: string, worktreeId?: string | null): string {
17
+ return worktreeId ? `${projectId}${SCOPE_SEPARATOR}${worktreeId}` : projectId;
18
+ }
19
+
20
+ export function parseScopeKey(scopeKey: string): { projectId: string; worktreeId: string | null } {
21
+ const index = scopeKey.indexOf(SCOPE_SEPARATOR);
22
+ if (index === -1) return { projectId: scopeKey, worktreeId: null };
23
+
24
+ return {
25
+ projectId: scopeKey.slice(0, index),
26
+ worktreeId: scopeKey.slice(index + SCOPE_SEPARATOR.length) || null
27
+ };
28
+ }
29
+
30
+ /** The project a scope key belongs to — what every access check runs against. */
31
+ export function scopeProjectId(scopeKey: string): string {
32
+ return parseScopeKey(scopeKey).projectId;
33
+ }
34
+
35
+ /** Whether `scopeKey` is any workspace of `projectId` (the project or a worktree). */
36
+ export function isScopeOfProject(scopeKey: string, projectId: string): boolean {
37
+ return parseScopeKey(scopeKey).projectId === projectId;
38
+ }
39
+
40
+ /**
41
+ * Short id-safe token that distinguishes one workspace from another.
42
+ *
43
+ * Ids generated per workspace (terminal sessions, preview tabs) are numbered
44
+ * from 1 inside their own scope, so two scopes of the same project mint the
45
+ * same id. Embedding this token is what keeps those numbers from colliding —
46
+ * a collision is not cosmetic: PtyKit refuses a session id held by another
47
+ * namespace, and the preview client matches events on tab id alone.
48
+ */
49
+ export function scopeSlug(scopeKey: string): string {
50
+ const { projectId, worktreeId } = parseScopeKey(scopeKey);
51
+ const alphanumeric = (value: string) => value.replace(/[^a-zA-Z0-9]/g, '');
52
+
53
+ const head = alphanumeric(projectId).slice(0, 8);
54
+ if (!worktreeId) return head;
55
+ // `w` keeps the token alphanumeric, so it survives id sanitisation.
56
+ return `${head}w${alphanumeric(worktreeId).slice(0, 6)}`;
57
+ }
@@ -299,9 +299,11 @@ export class WSClient<TAPI extends { client: any; server: any }> {
299
299
  private context: {
300
300
  userId: string | null;
301
301
  projectId: string | null;
302
+ worktreeId: string | null;
302
303
  } = {
303
304
  userId: null,
304
- projectId: null
305
+ projectId: null,
306
+ worktreeId: null
305
307
  };
306
308
 
307
309
  /** Session token for re-authentication on reconnection */
@@ -775,6 +777,8 @@ export class WSClient<TAPI extends { client: any; server: any }> {
775
777
  if (this.context.projectId === projectId) return;
776
778
 
777
779
  this.context.projectId = projectId;
780
+ // A worktree belongs to one project, so it never survives the switch.
781
+ this.context.worktreeId = null;
778
782
  debug.log('websocket', 'Context: project set to', projectId);
779
783
 
780
784
  if (this.isConnected) {
@@ -782,10 +786,25 @@ export class WSClient<TAPI extends { client: any; server: any }> {
782
786
  }
783
787
  }
784
788
 
789
+ /**
790
+ * Set the worktree this client is viewing (null = the main project tree).
791
+ * Server-side scoping of terminals and preview tabs reads this.
792
+ */
793
+ async setWorktree(worktreeId: string | null): Promise<void> {
794
+ if (this.context.worktreeId === worktreeId) return;
795
+
796
+ this.context.worktreeId = worktreeId;
797
+ debug.log('websocket', 'Context: worktree set to', worktreeId);
798
+
799
+ if (this.isConnected) {
800
+ await this.syncContext();
801
+ }
802
+ }
803
+
785
804
  /**
786
805
  * Get current context
787
806
  */
788
- getContext(): { userId: string | null; projectId: string | null } {
807
+ getContext(): { userId: string | null; projectId: string | null; worktreeId: string | null } {
789
808
  return { ...this.context };
790
809
  }
791
810
 
@@ -862,15 +881,16 @@ export class WSClient<TAPI extends { client: any; server: any }> {
862
881
  // Register response listener
863
882
  unsubResponse = this.on('ws:set-context:response' as any, handleResponse);
864
883
 
865
- // Send context sync request (only projectId — userId is set server-side by auth)
884
+ // Send context sync request (userId is set server-side by auth)
866
885
  this.emit('ws:set-context' as any, {
867
886
  requestId,
868
887
  data: {
869
- projectId: this.context.projectId
888
+ projectId: this.context.projectId,
889
+ worktreeId: this.context.worktreeId
870
890
  }
871
891
  } as any);
872
892
 
873
- debug.log('websocket', 'Context sync sent: projectId=', this.context.projectId);
893
+ debug.log('websocket', 'Context sync sent: projectId=', this.context.projectId, 'worktreeId=', this.context.worktreeId);
874
894
  });
875
895
  }
876
896
 
@@ -308,11 +308,13 @@ export class WSRouter<
308
308
  this.httpRoutes.set('ws:set-context', {
309
309
  action: 'ws:set-context',
310
310
  dataSchema: t.Object({
311
- projectId: t.Optional(t.Union([t.String(), t.Null()]))
311
+ projectId: t.Optional(t.Union([t.String(), t.Null()])),
312
+ worktreeId: t.Optional(t.Union([t.String(), t.Null()]))
312
313
  }),
313
314
  responseSchema: t.Object({
314
315
  userId: t.Union([t.String(), t.Null()]),
315
- projectId: t.Union([t.String(), t.Null()])
316
+ projectId: t.Union([t.String(), t.Null()]),
317
+ worktreeId: t.Union([t.String(), t.Null()])
316
318
  }),
317
319
  handler: async ({ conn, data }) => {
318
320
  // Import ws server to update context
@@ -338,11 +340,25 @@ export class WSRouter<
338
340
  wsServer.setProject(conn, data.projectId);
339
341
  }
340
342
 
343
+ // Which tree this connection is viewing. Validated against the
344
+ // project so a client cannot point itself at another one's worktree.
345
+ if (data.worktreeId !== undefined) {
346
+ let worktreeId = data.worktreeId;
347
+ if (worktreeId !== null) {
348
+ const { worktreeQueries } = await import('$backend/database/queries');
349
+ const activeProjectId = wsServer.getConnectionState(conn)?.projectId ?? null;
350
+ const worktree = worktreeQueries.getById(worktreeId);
351
+ if (!worktree || worktree.project_id !== activeProjectId) worktreeId = null;
352
+ }
353
+ wsServer.setWorktree(conn, worktreeId);
354
+ }
355
+
341
356
  // Read back from connectionState (single source of truth)
342
357
  const state = wsServer.getConnectionState(conn);
343
358
  return {
344
359
  userId: state?.userId ?? null,
345
- projectId: state?.projectId ?? null
360
+ projectId: state?.projectId ?? null,
361
+ worktreeId: state?.worktreeId ?? null
346
362
  };
347
363
  }
348
364
  });