@myrialabs/clopen 0.3.12 → 0.3.14

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 (55) hide show
  1. package/backend/auth/session-invalidation.test.ts +45 -1
  2. package/backend/auth/session-invalidation.ts +35 -1
  3. package/backend/chat/stream-manager.ts +15 -0
  4. package/backend/database/migrations/037_repair_auth_audit_log_schema.ts +38 -0
  5. package/backend/database/migrations/index.ts +7 -0
  6. package/backend/engine/README.md +16 -1597
  7. package/backend/engine/adapters/claude/message-converter.ts +34 -7
  8. package/backend/engine/adapters/claude/models.ts +174 -2
  9. package/backend/engine/adapters/codex/stream.ts +54 -1
  10. package/backend/engine/adapters/copilot/stream.ts +79 -19
  11. package/backend/engine/adapters/opencode/stream.ts +23 -23
  12. package/backend/engine/adapters/qwen/stream.ts +83 -1
  13. package/backend/engine/docs/adapter-contract.md +183 -0
  14. package/backend/engine/docs/adding-an-engine.md +150 -0
  15. package/backend/engine/docs/database.md +123 -0
  16. package/backend/engine/docs/frontend-and-chat.md +176 -0
  17. package/backend/engine/docs/lessons-learned.md +715 -0
  18. package/backend/engine/docs/quick-reference.md +36 -0
  19. package/backend/engine/docs/system-tools.md +82 -0
  20. package/backend/engine/docs/websocket-routes.md +240 -0
  21. package/backend/engine/structured-helpers.ts +59 -0
  22. package/backend/files/file-archive.test.ts +131 -0
  23. package/backend/files/file-archive.ts +252 -0
  24. package/backend/files/file-operations.ts +39 -66
  25. package/backend/files/file-size-limit.test.ts +71 -15
  26. package/backend/files/file-size-limit.ts +36 -8
  27. package/backend/files/file-watcher.ts +117 -3
  28. package/backend/http/files-upload.ts +168 -0
  29. package/backend/index.ts +8 -0
  30. package/backend/mcp/servers/helper.ts +5 -1
  31. package/backend/projects/project-cleanup.ts +1 -1
  32. package/backend/terminal/pty-session-manager.ts +3 -0
  33. package/backend/terminal/stream-manager.ts +1 -1
  34. package/backend/utils/ws.ts +9 -0
  35. package/backend/ws/auth/users.ts +1 -1
  36. package/backend/ws/chat/stream.ts +46 -38
  37. package/backend/ws/files/path-access.test.ts +166 -0
  38. package/backend/ws/files/path-access.ts +56 -22
  39. package/backend/ws/files/read.ts +5 -5
  40. package/backend/ws/files/watch.ts +48 -19
  41. package/backend/ws/files/write.ts +53 -27
  42. package/backend/ws/git/commit-message.ts +12 -5
  43. package/backend/ws/terminal/session.ts +1 -0
  44. package/dist/assets/main-BqiSLc8F.css +1 -0
  45. package/dist/assets/{main-HIDO3ugS.js → main-C37-Y8f-.js} +230 -229
  46. package/dist/index.html +2 -2
  47. package/package.json +8 -7
  48. package/shared/types/git.ts +4 -2
  49. package/shared/types/stores/settings.ts +2 -0
  50. package/shared/types/unified/index.ts +6 -0
  51. package/shared/types/unified/stream.ts +15 -1
  52. package/shared/types/unified/tool.ts +41 -1
  53. package/shared/utils/ws-server.ts +6 -0
  54. package/backend/engine/adapters/copilot/session-fork.ts +0 -111
  55. package/dist/assets/main-C2JiGYnx.css +0 -1
@@ -1,12 +1,18 @@
1
1
  import { describe, expect, test, mock, beforeEach } from 'bun:test';
2
2
  import { invalidateUserSessions, getActiveSessionCountForUser } from './session-invalidation';
3
3
 
4
- // Mock the ws module
5
4
  const mockGetConnectionsForUser = mock(() => [] as any[]);
6
5
  const mockClearAuthForConnections = mock(() => 0);
7
6
  const mockGetProjectChatSessions = mock(() => new Map());
8
7
  const mockEmitUser = mock(() => {});
9
8
 
9
+ const mockKillSession = mock(() => {});
10
+ const mockGetAllSessions = mock(() => [] as Array<{
11
+ sessionId: string;
12
+ projectId?: string;
13
+ userId: string;
14
+ }>);
15
+
10
16
  mock.module('$backend/utils/ws', () => ({
11
17
  ws: {
12
18
  getConnectionsForUser: mockGetConnectionsForUser,
@@ -18,12 +24,21 @@ mock.module('$backend/utils/ws', () => ({
18
24
  }
19
25
  }));
20
26
 
27
+ mock.module('$backend/terminal/pty-session-manager', () => ({
28
+ ptySessionManager: {
29
+ getAllSessions: mockGetAllSessions,
30
+ killSession: mockKillSession
31
+ }
32
+ }));
33
+
21
34
  describe('invalidateUserSessions', () => {
22
35
  beforeEach(() => {
23
36
  mockGetConnectionsForUser.mockClear();
24
37
  mockClearAuthForConnections.mockClear();
25
38
  mockGetProjectChatSessions.mockClear();
26
39
  mockEmitUser.mockClear();
40
+ mockKillSession.mockClear();
41
+ mockGetAllSessions.mockClear();
27
42
  });
28
43
 
29
44
  test('clears auth on all connections for a user', () => {
@@ -46,6 +61,35 @@ describe('invalidateUserSessions', () => {
46
61
 
47
62
  expect(cleared).toBe(0);
48
63
  });
64
+
65
+ test('kills only PTY sessions owned by the revoked user in the project', () => {
66
+ mockGetConnectionsForUser.mockReturnValue(['conn1']);
67
+ mockClearAuthForConnections.mockReturnValue(1);
68
+ mockGetAllSessions.mockReturnValue([
69
+ { sessionId: 'alice-session', projectId: 'proj-1', userId: 'alice' },
70
+ { sessionId: 'bob-session', projectId: 'proj-1', userId: 'bob' },
71
+ { sessionId: 'bob-other', projectId: 'proj-2', userId: 'bob' }
72
+ ]);
73
+
74
+ invalidateUserSessions('bob', 'proj-1');
75
+
76
+ expect(mockKillSession).toHaveBeenCalledTimes(1);
77
+ expect(mockKillSession).toHaveBeenCalledWith('bob-session', 'SIGKILL');
78
+ });
79
+
80
+ test('kills PTY sessions when user has no websocket connections but project access was revoked', () => {
81
+ mockGetConnectionsForUser.mockReturnValue([]);
82
+ mockGetAllSessions.mockReturnValue([
83
+ { sessionId: 'bob-session', projectId: 'proj-1', userId: 'bob' },
84
+ { sessionId: 'alice-session', projectId: 'proj-1', userId: 'alice' }
85
+ ]);
86
+
87
+ const cleared = invalidateUserSessions('bob', 'proj-1');
88
+
89
+ expect(cleared).toBe(0);
90
+ expect(mockKillSession).toHaveBeenCalledTimes(1);
91
+ expect(mockKillSession).toHaveBeenCalledWith('bob-session', 'SIGKILL');
92
+ });
49
93
  });
50
94
 
51
95
  describe('getActiveSessionCountForUser', () => {
@@ -7,19 +7,28 @@
7
7
 
8
8
  import { ws } from '$backend/utils/ws';
9
9
  import { debug } from '$shared/utils/logger';
10
+ import { ptySessionManager } from '$backend/terminal/pty-session-manager';
10
11
 
11
12
  /**
12
13
  * Invalidate all active WebSocket sessions for a specific user.
13
14
  * This clears their in-memory auth state and triggers a force-logout
14
15
  * event so the frontend knows to disconnect and re-authenticate.
15
16
  *
17
+ * Also kills any PTY terminal sessions belonging to the specified
18
+ * project to prevent orphaned processes from continuing to run
19
+ * after access is revoked.
20
+ *
16
21
  * @param userId - The user whose sessions should be invalidated
22
+ * @param projectId - Optional project ID to scope PTY cleanup
17
23
  * @returns The number of connections that were cleared
18
24
  */
19
- export function invalidateUserSessions(userId: string): number {
25
+ export function invalidateUserSessions(userId: string, projectId?: string): number {
20
26
  const connections = ws.getConnectionsForUser(userId);
21
27
  if (connections.length === 0) {
22
28
  debug.log('auth', `No active sessions to invalidate for user ${userId}`);
29
+ if (projectId) {
30
+ killUserPtySessionsForProject(userId, projectId);
31
+ }
23
32
  return 0;
24
33
  }
25
34
 
@@ -29,9 +38,34 @@ export function invalidateUserSessions(userId: string): number {
29
38
  // Notify only this user's active connections to force logout.
30
39
  ws.emit.user(userId, 'auth:force-logout-user', { reason: 'Project access revoked' });
31
40
 
41
+ if (projectId) {
42
+ killUserPtySessionsForProject(userId, projectId);
43
+ }
44
+
32
45
  return cleared;
33
46
  }
34
47
 
48
+ /**
49
+ * Kill PTY terminal sessions owned by a specific user in a project.
50
+ * Prevents orphaned shell processes from continuing to run after
51
+ * project access is revoked, without affecting other users' sessions.
52
+ */
53
+ function killUserPtySessionsForProject(userId: string, projectId: string): void {
54
+ let killedCount = 0;
55
+
56
+ for (const session of ptySessionManager.getAllSessions()) {
57
+ if (session.projectId === projectId && session.userId === userId) {
58
+ debug.log('auth', `Killing PTY session ${session.sessionId} for user ${userId} (project ${projectId} access revoked)`);
59
+ ptySessionManager.killSession(session.sessionId, 'SIGKILL');
60
+ killedCount++;
61
+ }
62
+ }
63
+
64
+ if (killedCount > 0) {
65
+ debug.log('auth', `Killed ${killedCount} PTY session(s) for user ${userId} in project ${projectId}`);
66
+ }
67
+ }
68
+
35
69
  /**
36
70
  * Get the number of active WebSocket connections for a user.
37
71
  * Useful for logging and debugging.
@@ -24,6 +24,7 @@ import type {
24
24
  SystemInitEvent,
25
25
  RateLimitEvent,
26
26
  RateLimitType,
27
+ NotificationEvent,
27
28
  TokenUsage,
28
29
  StopReason,
29
30
  UserContentBlock,
@@ -731,6 +732,20 @@ class StreamManager extends EventEmitter {
731
732
  continue;
732
733
  }
733
734
 
735
+ // ── Notification (e.g. background-task completion) ─────
736
+ case 'notification': {
737
+ const note = output as NotificationEvent;
738
+ this.emitStreamEvent(streamState, 'notification', {
739
+ notification: {
740
+ type: note.level,
741
+ title: note.title,
742
+ message: note.message,
743
+ },
744
+ timestamp: new Date().toISOString()
745
+ });
746
+ continue; // transient — don't save to DB
747
+ }
748
+
734
749
  // ── Result ─────────────────────────────────────────────
735
750
  case 'result': {
736
751
  if (output.subtype === 'success') {
@@ -0,0 +1,38 @@
1
+ /**
2
+ * Migration 037: Repair auth_audit_log schema for early-applied databases
3
+ *
4
+ * Some databases applied an early version of migration 036 that did not
5
+ * include the actor_user_id column. The original CREATE statement uses
6
+ * IF NOT EXISTS, so a re-run won't alter the existing table. This
7
+ * migration adds the missing column when absent.
8
+ */
9
+
10
+ import type { DatabaseConnection } from '$shared/types/database/connection';
11
+
12
+ export const description = 'Repair auth_audit_log schema (add missing actor_user_id column)';
13
+
14
+ interface SqliteColumnInfo {
15
+ name: string;
16
+ }
17
+
18
+ export function up(db: DatabaseConnection): void {
19
+ const columns = db
20
+ .prepare(`PRAGMA table_info(auth_audit_log)`)
21
+ .all() as SqliteColumnInfo[];
22
+
23
+ if (columns.length === 0) {
24
+ // Table doesn't exist yet — migration 036 will create it with the
25
+ // correct schema. Nothing to repair.
26
+ return;
27
+ }
28
+
29
+ const hasActorUserId = columns.some((c) => c.name === 'actor_user_id');
30
+ if (!hasActorUserId) {
31
+ db.exec(`ALTER TABLE auth_audit_log ADD COLUMN actor_user_id TEXT;`);
32
+ db.exec(`CREATE INDEX IF NOT EXISTS idx_auth_audit_actor_user_id ON auth_audit_log(actor_user_id);`);
33
+ }
34
+ }
35
+
36
+ export function down(_db: DatabaseConnection): void {
37
+ // Non-destructive repair — no reversal needed.
38
+ }
@@ -35,6 +35,7 @@ import * as migration033 from './033_seed_codex_provider';
35
35
  import * as migration034 from './034_seed_qwen_provider';
36
36
  import * as migration035 from './035_add_owner_to_db_client_connections';
37
37
  import * as migration036 from './036_create_auth_audit_log';
38
+ import * as migration037 from './037_repair_auth_audit_log_schema';
38
39
 
39
40
  // Export all migrations in order
40
41
  export const migrations = [
@@ -253,6 +254,12 @@ export const migrations = [
253
254
  description: migration036.description,
254
255
  up: migration036.up,
255
256
  down: migration036.down
257
+ },
258
+ {
259
+ id: '037',
260
+ description: migration037.description,
261
+ up: migration037.up,
262
+ down: migration037.down
256
263
  }
257
264
  ];
258
265