@myrialabs/clopen 0.4.37 → 0.4.39

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 (193) hide show
  1. package/README.md +2 -2
  2. package/backend/auth/permissions.ts +39 -0
  3. package/backend/bootstrap.ts +16 -0
  4. package/backend/chat/stream-manager.ts +1 -1
  5. package/backend/database/crypto/envelope.test.ts +93 -0
  6. package/backend/database/crypto/envelope.ts +127 -0
  7. package/backend/database/crypto/index.ts +22 -0
  8. package/backend/database/crypto/master-key.ts +129 -0
  9. package/backend/database/crypto/secret-columns.ts +151 -0
  10. package/backend/database/migrations/071_create_notes_tables.ts +90 -0
  11. package/backend/database/migrations/072_encrypt_stored_secrets.ts +107 -0
  12. package/backend/database/migrations/073_create_integration_tables.ts +119 -0
  13. package/backend/database/migrations/074_create_work_tables.ts +94 -0
  14. package/backend/database/migrations/075_create_deploy_bindings.ts +54 -0
  15. package/backend/database/migrations/076_remove_memory_code_graph.ts +229 -0
  16. package/backend/database/migrations/077_create_integration_db_links.ts +150 -0
  17. package/backend/database/migrations/index.ts +49 -0
  18. package/backend/database/queries/db-client-connection-queries.ts +42 -12
  19. package/backend/database/queries/deploy-queries.ts +124 -0
  20. package/backend/database/queries/engine-queries.ts +23 -10
  21. package/backend/database/queries/graph-queries.ts +291 -247
  22. package/backend/database/queries/index.ts +10 -1
  23. package/backend/database/queries/integration-db-link-queries.ts +168 -0
  24. package/backend/database/queries/integration-queries.ts +325 -0
  25. package/backend/database/queries/mcp-server-queries.ts +38 -11
  26. package/backend/database/queries/note-queries.test.ts +190 -0
  27. package/backend/database/queries/note-queries.ts +269 -0
  28. package/backend/database/queries/profile-queries.ts +1 -1
  29. package/backend/database/queries/ssh-connection-queries.ts +19 -8
  30. package/backend/database/queries/work-queries.ts +210 -0
  31. package/backend/db-client/integrations/index.ts +37 -0
  32. package/backend/db-client/integrations/links.ts +533 -0
  33. package/backend/db-client/integrations/projector.test.ts +280 -0
  34. package/backend/db-client/integrations/projector.ts +256 -0
  35. package/backend/db-client/integrations/supabase-service.ts +266 -0
  36. package/backend/db-client/providers/registry.ts +27 -0
  37. package/backend/db-client/providers/supabase/adapter.ts +500 -0
  38. package/backend/db-client/providers/supabase/api-types.ts +92 -0
  39. package/backend/db-client/providers/supabase/apply-migration.ts +104 -0
  40. package/backend/db-client/providers/supabase/client.ts +259 -0
  41. package/backend/db-client/providers/supabase/features.ts +502 -0
  42. package/backend/db-client/providers/supabase/local.test.ts +78 -0
  43. package/backend/db-client/providers/supabase/local.ts +137 -0
  44. package/backend/db-client/providers/types.ts +137 -0
  45. package/backend/deployments/bindings.test.ts +43 -0
  46. package/backend/deployments/bindings.ts +216 -0
  47. package/backend/deployments/index.ts +53 -0
  48. package/backend/deployments/log-streams.ts +214 -0
  49. package/backend/deployments/prompts.ts +70 -0
  50. package/backend/deployments/providers/vercel/adapter.ts +757 -0
  51. package/backend/deployments/providers/vercel/build-logs.test.ts +80 -0
  52. package/backend/deployments/providers/vercel/build-logs.ts +211 -0
  53. package/backend/deployments/providers/vercel/client.ts +203 -0
  54. package/backend/deployments/providers/vercel/list.test.ts +624 -0
  55. package/backend/deployments/providers/vercel/normalise.test.ts +115 -0
  56. package/backend/deployments/providers/vercel/normalise.ts +113 -0
  57. package/backend/deployments/providers/vercel/types.ts +104 -0
  58. package/backend/deployments/registry.ts +27 -0
  59. package/backend/deployments/service.ts +458 -0
  60. package/backend/deployments/types.ts +278 -0
  61. package/backend/engine/docs/artifacts.md +22 -9
  62. package/backend/files/os-clipboard.test.ts +209 -0
  63. package/backend/files/os-clipboard.ts +567 -0
  64. package/backend/http/integration-hooks.ts +42 -0
  65. package/backend/http/notes-images.test.ts +228 -0
  66. package/backend/http/notes-images.ts +196 -0
  67. package/backend/index.ts +13 -0
  68. package/backend/integrations/accounts.ts +229 -0
  69. package/backend/integrations/health.ts +146 -0
  70. package/backend/integrations/index.ts +36 -0
  71. package/backend/integrations/projections/index.ts +0 -0
  72. package/backend/integrations/projections/mcp-projector.test.ts +204 -0
  73. package/backend/integrations/projections/mcp-projector.ts +163 -0
  74. package/backend/integrations/projections/types.ts +63 -0
  75. package/backend/integrations/registry/define.ts +38 -0
  76. package/backend/integrations/registry/index.ts +60 -0
  77. package/backend/integrations/registry/providers/context7.ts +44 -0
  78. package/backend/integrations/registry/providers/firecrawl.ts +41 -0
  79. package/backend/integrations/registry/providers/github.ts +78 -0
  80. package/backend/integrations/registry/providers/supabase.ts +61 -0
  81. package/backend/integrations/registry/providers/vercel.ts +46 -0
  82. package/backend/integrations/registry/types.ts +96 -0
  83. package/backend/integrations/webhooks/gateway.ts +160 -0
  84. package/backend/integrations/webhooks/verify.ts +65 -0
  85. package/backend/mcp/README.md +25 -3
  86. package/backend/mcp/external/config.ts +35 -1
  87. package/backend/mcp/index.ts +1 -1
  88. package/backend/mcp/internal/config.ts +1 -1
  89. package/backend/mcp/internal/servers/memory-graph/description.ts +3 -3
  90. package/backend/mcp/internal/servers/memory-graph/runner.ts +4 -9
  91. package/backend/mcp/internal/servers/memory-graph/schema.ts +0 -4
  92. package/backend/memory/README.md +57 -34
  93. package/backend/memory/compose.test.ts +3 -3
  94. package/backend/memory/compose.ts +6 -9
  95. package/backend/memory/config.ts +0 -5
  96. package/backend/memory/consolidate.ts +1 -2
  97. package/backend/memory/context.test.ts +8 -54
  98. package/backend/memory/context.ts +19 -25
  99. package/backend/memory/extract/episodic.ts +10 -12
  100. package/backend/memory/extract/index.ts +23 -52
  101. package/backend/memory/extract/scheduler.ts +3 -6
  102. package/backend/memory/index.ts +6 -5
  103. package/backend/memory/invalidate.ts +36 -48
  104. package/backend/memory/layout.ts +319 -130
  105. package/backend/memory/retention.ts +12 -42
  106. package/backend/memory/retrieval.eval.test.ts +2 -2
  107. package/backend/memory/retrieval.test.ts +228 -183
  108. package/backend/memory/retrieval.ts +22 -21
  109. package/backend/memory/revise.test.ts +62 -116
  110. package/backend/memory/revise.ts +0 -1
  111. package/backend/memory/view.ts +8 -5
  112. package/backend/notes/access.test.ts +82 -0
  113. package/backend/notes/access.ts +27 -0
  114. package/backend/notes/storage.test.ts +77 -0
  115. package/backend/notes/storage.ts +114 -0
  116. package/backend/permissions/service.ts +2 -2
  117. package/backend/profiles/service.ts +1 -1
  118. package/backend/projects/project-cleanup.ts +6 -0
  119. package/backend/snapshot/helpers.ts +35 -41
  120. package/backend/snapshot/snapshot-service.ts +90 -0
  121. package/backend/snapshot/turn-changes.test.ts +163 -0
  122. package/backend/snapshot/turn-changes.ts +131 -0
  123. package/backend/work/bindings.ts +124 -0
  124. package/backend/work/index.ts +42 -0
  125. package/backend/work/prompts.ts +150 -0
  126. package/backend/work/providers/github/actions-logs.ts +180 -0
  127. package/backend/work/providers/github/adapter.test.ts +88 -0
  128. package/backend/work/providers/github/adapter.ts +1255 -0
  129. package/backend/work/providers/github/client.ts +280 -0
  130. package/backend/work/providers/github/list-range.test.ts +158 -0
  131. package/backend/work/providers/github/token-kind.test.ts +42 -0
  132. package/backend/work/pull-requests.ts +242 -0
  133. package/backend/work/registry.ts +27 -0
  134. package/backend/work/service.ts +436 -0
  135. package/backend/work/start-work.test.ts +71 -0
  136. package/backend/work/start-work.ts +232 -0
  137. package/backend/work/types.ts +180 -0
  138. package/backend/ws/db-client/accounts.ts +225 -0
  139. package/backend/ws/db-client/connections.ts +34 -3
  140. package/backend/ws/db-client/context.ts +37 -0
  141. package/backend/ws/db-client/index.ts +20 -1
  142. package/backend/ws/db-client/supabase.ts +188 -0
  143. package/backend/ws/deployments/actions.ts +360 -0
  144. package/backend/ws/deployments/context.ts +43 -0
  145. package/backend/ws/deployments/crud.ts +152 -0
  146. package/backend/ws/deployments/index.ts +15 -0
  147. package/backend/ws/deployments/schemas.ts +183 -0
  148. package/backend/ws/files/clipboard.ts +78 -0
  149. package/backend/ws/files/index.ts +5 -0
  150. package/backend/ws/git/diff-budget.ts +70 -11
  151. package/backend/ws/index.ts +16 -2
  152. package/backend/ws/integrations/crud.ts +219 -0
  153. package/backend/ws/integrations/index.ts +22 -0
  154. package/backend/ws/mcp/crud.ts +25 -1
  155. package/backend/ws/memory/crud.ts +4 -16
  156. package/backend/ws/notes/index.ts +278 -0
  157. package/backend/ws/profiles/crud.ts +1 -1
  158. package/backend/ws/snapshot/changes.ts +279 -0
  159. package/backend/ws/snapshot/index.ts +4 -3
  160. package/backend/ws/work/actions.ts +206 -0
  161. package/backend/ws/work/context.ts +51 -0
  162. package/backend/ws/work/crud.ts +347 -0
  163. package/backend/ws/work/index.ts +17 -0
  164. package/backend/ws/work/pull-requests.ts +177 -0
  165. package/backend/ws/work/schemas.ts +226 -0
  166. package/dist/assets/{index-sJmS_CCm.js → index-BrF6nqld.js} +1 -1
  167. package/dist/assets/{index-Bik9TCBi.js → index-C8Gi-b6U.js} +1 -1
  168. package/dist/assets/main-Bd3gb79F.css +1 -0
  169. package/dist/assets/main-Bo1hD7mt.js +1191 -0
  170. package/dist/index.html +2 -2
  171. package/package.json +1 -1
  172. package/shared/constants/engine-tools.ts +1 -1
  173. package/shared/constants/tool-icons.ts +42 -0
  174. package/shared/types/database/schema.ts +41 -0
  175. package/shared/types/db-client/connection.ts +6 -0
  176. package/shared/types/db-client/index.ts +2 -0
  177. package/shared/types/db-client/integrations.ts +174 -0
  178. package/shared/types/db-client/supabase.ts +131 -0
  179. package/shared/types/deployments.ts +382 -0
  180. package/shared/types/integrations.ts +140 -0
  181. package/shared/types/memory.ts +34 -51
  182. package/shared/types/stores/settings.ts +8 -0
  183. package/shared/types/ui/components.ts +1 -1
  184. package/shared/types/work.ts +426 -0
  185. package/shared/utils/diff-calculator.test.ts +89 -0
  186. package/shared/utils/diff-calculator.ts +30 -65
  187. package/shared/utils/logger.ts +4 -0
  188. package/backend/memory/extract/languages.ts +0 -176
  189. package/backend/memory/extract/structural.test.ts +0 -215
  190. package/backend/memory/extract/structural.ts +0 -364
  191. package/backend/ws/snapshot/content.ts +0 -85
  192. package/dist/assets/main-DLqolBI2.css +0 -1
  193. package/dist/assets/main-Df1I3hnw.js +0 -1096
@@ -0,0 +1,90 @@
1
+ import type { DatabaseConnection } from '$shared/types/database/connection';
2
+ import { debug } from '$shared/utils/logger';
3
+
4
+ export const description = 'Create note collections, notes and note images';
5
+
6
+ export const up = (db: DatabaseConnection): void => {
7
+ debug.log('migration', 'Creating notes tables...');
8
+
9
+ // Scope lives here and only here. A note inherits who may read it from its
10
+ // collection, so there is no second copy of the scope that could disagree
11
+ // with this one — and no note that is reachable through two different
12
+ // permission rules.
13
+ db.exec(`
14
+ CREATE TABLE IF NOT EXISTS note_collections (
15
+ id TEXT PRIMARY KEY,
16
+ name TEXT NOT NULL,
17
+ scope TEXT NOT NULL CHECK (scope IN ('project', 'global')),
18
+ project_id TEXT REFERENCES projects(id) ON DELETE CASCADE,
19
+ created_by TEXT,
20
+ created_at TEXT NOT NULL,
21
+ updated_at TEXT NOT NULL,
22
+ CHECK (
23
+ (scope = 'project' AND project_id IS NOT NULL) OR
24
+ (scope = 'global' AND project_id IS NULL)
25
+ )
26
+ )
27
+ `);
28
+
29
+ db.exec(`
30
+ CREATE INDEX IF NOT EXISTS idx_note_collections_project ON note_collections(project_id)
31
+ `);
32
+
33
+ db.exec(`
34
+ CREATE INDEX IF NOT EXISTS idx_note_collections_scope ON note_collections(scope)
35
+ `);
36
+
37
+ db.exec(`
38
+ CREATE TABLE IF NOT EXISTS notes (
39
+ id TEXT PRIMARY KEY,
40
+ collection_id TEXT NOT NULL REFERENCES note_collections(id) ON DELETE CASCADE,
41
+ title TEXT,
42
+ content TEXT NOT NULL DEFAULT '',
43
+ created_by TEXT,
44
+ created_at TEXT NOT NULL,
45
+ updated_at TEXT NOT NULL
46
+ )
47
+ `);
48
+
49
+ db.exec(`
50
+ CREATE INDEX IF NOT EXISTS idx_notes_collection ON notes(collection_id)
51
+ `);
52
+
53
+ db.exec(`
54
+ CREATE INDEX IF NOT EXISTS idx_notes_updated ON notes(updated_at DESC)
55
+ `);
56
+
57
+ // `storage_path` is relative to the data dir with forward slashes
58
+ // (`notes/<collectionId>/<noteId>/<uuid>-<name>`), never absolute — see
59
+ // `backend/notes/storage.ts` for why.
60
+ db.exec(`
61
+ CREATE TABLE IF NOT EXISTS note_images (
62
+ id TEXT PRIMARY KEY,
63
+ note_id TEXT NOT NULL REFERENCES notes(id) ON DELETE CASCADE,
64
+ file_name TEXT NOT NULL,
65
+ mime_type TEXT NOT NULL,
66
+ size INTEGER NOT NULL,
67
+ storage_path TEXT NOT NULL,
68
+ created_at TEXT NOT NULL
69
+ )
70
+ `);
71
+
72
+ db.exec(`
73
+ CREATE INDEX IF NOT EXISTS idx_note_images_note ON note_images(note_id)
74
+ `);
75
+
76
+ debug.log('migration', 'Notes tables created');
77
+ };
78
+
79
+ export const down = (db: DatabaseConnection): void => {
80
+ debug.log('migration', 'Dropping notes tables...');
81
+ db.exec('DROP INDEX IF EXISTS idx_note_images_note');
82
+ db.exec('DROP TABLE IF EXISTS note_images');
83
+ db.exec('DROP INDEX IF EXISTS idx_notes_updated');
84
+ db.exec('DROP INDEX IF EXISTS idx_notes_collection');
85
+ db.exec('DROP TABLE IF EXISTS notes');
86
+ db.exec('DROP INDEX IF EXISTS idx_note_collections_scope');
87
+ db.exec('DROP INDEX IF EXISTS idx_note_collections_project');
88
+ db.exec('DROP TABLE IF EXISTS note_collections');
89
+ debug.log('migration', 'Notes tables dropped');
90
+ };
@@ -0,0 +1,107 @@
1
+ import type { DatabaseConnection } from '$shared/types/database/connection';
2
+ import { isSealed, open, seal, SECRET_COLUMNS } from '../crypto';
3
+ import { debug } from '$shared/utils/logger';
4
+
5
+ export const description = 'Re-encrypt every stored secret with the AES-256-GCM envelope';
6
+
7
+ /**
8
+ * Seal the secrets that were written before the crypto layer existed.
9
+ *
10
+ * THIS MUST USE RAW SQL. The query modules now seal on write and open on read,
11
+ * so routing this through them would encrypt an already-encrypted value on the
12
+ * way in and hand back plaintext on the way out — the migration would appear to
13
+ * do nothing while corrupting every row it touched.
14
+ *
15
+ * Two properties of the envelope make the pass safe to interrupt and safe to
16
+ * repeat: an unprefixed value is pre-migration plaintext and gets sealed, and a
17
+ * value that already carries the prefix is left exactly as it is. A half-applied
18
+ * migration therefore reads correctly, and a re-run cannot double-encrypt.
19
+ *
20
+ * `integration_accounts` is deliberately absent: migration 073 creates it, and
21
+ * everything written to it is sealed from the first insert.
22
+ */
23
+ const TABLES: Record<string, readonly string[]> = {
24
+ mcp_servers: SECRET_COLUMNS.mcp_servers!,
25
+ db_client_connections: SECRET_COLUMNS.db_client_connections!,
26
+ ssh_connections: SECRET_COLUMNS.ssh_connections!,
27
+ engine_accounts: SECRET_COLUMNS.engine_accounts!
28
+ };
29
+
30
+ function existingTables(db: DatabaseConnection): Set<string> {
31
+ const rows = db.prepare(`SELECT name FROM sqlite_master WHERE type = 'table'`).all() as { name: string }[];
32
+ return new Set(rows.map((row) => row.name));
33
+ }
34
+
35
+ /**
36
+ * Walk one column and rewrite each value through `transform`.
37
+ *
38
+ * Targeted by `rowid` rather than a primary key: the four tables use three
39
+ * different key names and types, and `rowid` is identical on all of them.
40
+ */
41
+ function rewriteColumn(
42
+ db: DatabaseConnection,
43
+ table: string,
44
+ column: string,
45
+ transform: (value: string) => string | null
46
+ ): number {
47
+ const rows = db.prepare(
48
+ `SELECT rowid AS rid, ${column} AS value FROM ${table} WHERE ${column} IS NOT NULL AND ${column} != ''`
49
+ ).all() as { rid: number; value: unknown }[];
50
+
51
+ const update = db.prepare(`UPDATE ${table} SET ${column} = ? WHERE rowid = ?`);
52
+ let changed = 0;
53
+
54
+ for (const row of rows) {
55
+ if (typeof row.value !== 'string') continue;
56
+ const next = transform(row.value);
57
+ if (next === null || next === row.value) continue;
58
+ update.run(next, row.rid);
59
+ changed += 1;
60
+ }
61
+
62
+ return changed;
63
+ }
64
+
65
+ export const up = (db: DatabaseConnection): void => {
66
+ debug.log('migration', 'Sealing stored secrets...');
67
+
68
+ const present = existingTables(db);
69
+ let total = 0;
70
+
71
+ for (const [table, columns] of Object.entries(TABLES)) {
72
+ if (!present.has(table)) continue;
73
+ for (const column of columns) {
74
+ total += rewriteColumn(db, table, column, (value) => (isSealed(value) ? null : seal(value)));
75
+ }
76
+ }
77
+
78
+ debug.log('migration', `Sealed ${total} secret value(s)`);
79
+ };
80
+
81
+ /**
82
+ * Unseal everything back to plaintext.
83
+ *
84
+ * A rollback that left ciphertext behind would be data loss the moment the key
85
+ * file went away, so `down()` has to be a real decryption pass rather than a
86
+ * no-op. A value the active key cannot open is left untouched: overwriting it
87
+ * with null would destroy the only copy, and leaving the envelope in place at
88
+ * least keeps it recoverable once the right key comes back.
89
+ */
90
+ export const down = (db: DatabaseConnection): void => {
91
+ debug.log('migration', 'Unsealing stored secrets...');
92
+
93
+ const present = existingTables(db);
94
+ let total = 0;
95
+
96
+ for (const [table, columns] of Object.entries(TABLES)) {
97
+ if (!present.has(table)) continue;
98
+ for (const column of columns) {
99
+ total += rewriteColumn(db, table, column, (value) => {
100
+ if (!isSealed(value)) return null;
101
+ return open(value);
102
+ });
103
+ }
104
+ }
105
+
106
+ debug.log('migration', `Unsealed ${total} secret value(s)`);
107
+ };
@@ -0,0 +1,119 @@
1
+ import type { DatabaseConnection } from '$shared/types/database/connection';
2
+ import { debug } from '$shared/utils/logger';
3
+
4
+ export const description = 'Create integration_accounts, integration_projections and integration_webhook_deliveries';
5
+
6
+ export const up = (db: DatabaseConnection): void => {
7
+ debug.log('migration', 'Creating integration_accounts table...');
8
+
9
+ // One row per connected third-party account, and the ONLY place that
10
+ // account's credential lives. Everything that talks to the service reads
11
+ // through here instead of keeping its own copy.
12
+ //
13
+ // `(provider, label)` is the identity: two projects pointing at different
14
+ // orgs of the same service are two accounts with two labels, not one row
15
+ // scoped twice. `project_id` is a BINDING on top of that identity — null
16
+ // means the account is available to every project.
17
+ //
18
+ // `credentials` is sealed at rest by the query layer (see
19
+ // backend/database/crypto). It is a JSON object because the shape differs
20
+ // per provider, and the whole blob is sealed rather than field-by-field.
21
+ db.exec(`
22
+ CREATE TABLE IF NOT EXISTS integration_accounts (
23
+ id TEXT PRIMARY KEY,
24
+ provider TEXT NOT NULL,
25
+ label TEXT NOT NULL,
26
+ project_id TEXT REFERENCES projects(id) ON DELETE CASCADE,
27
+ auth_method TEXT NOT NULL,
28
+ credentials TEXT NOT NULL DEFAULT '{}',
29
+ capabilities TEXT NOT NULL DEFAULT '[]',
30
+ status TEXT NOT NULL DEFAULT 'unknown'
31
+ CHECK (status IN ('unknown','ok','needs_auth','needs_config','error')),
32
+ status_detail TEXT,
33
+ checked_at TEXT,
34
+ is_enabled INTEGER NOT NULL DEFAULT 1,
35
+ created_at TEXT NOT NULL,
36
+ updated_at TEXT NOT NULL,
37
+ UNIQUE (provider, label)
38
+ )
39
+ `);
40
+
41
+ db.exec(`
42
+ CREATE INDEX IF NOT EXISTS idx_integration_accounts_provider
43
+ ON integration_accounts(provider)
44
+ `);
45
+
46
+ db.exec(`
47
+ CREATE INDEX IF NOT EXISTS idx_integration_accounts_project
48
+ ON integration_accounts(project_id)
49
+ `);
50
+
51
+ debug.log('migration', 'Creating integration_projections table...');
52
+
53
+ // A projection is a row on another surface that was DERIVED from an account
54
+ // — an `mcp_servers` row for the agent-tools capability, a
55
+ // `db_client_connections` row for a database one. This table is the link,
56
+ // and it is what makes re-projection and cleanup possible without any
57
+ // surface having to know accounts exist.
58
+ //
59
+ // `adopted` is the whole reason this is a table rather than a naming
60
+ // convention: when a projection lands on a slug the user had already
61
+ // installed by hand, we take ownership of the EXISTING row instead of
62
+ // creating a sibling, and mark it adopted so disconnecting releases it
63
+ // rather than deleting what the user built.
64
+ //
65
+ // `restore_json` is what makes that release honest. Adopting a row
66
+ // overwrites the credential the user had configured on it; without a
67
+ // snapshot, "we left your row intact" would mean leaving behind a row
68
+ // pointing at a credential we just deleted.
69
+ db.exec(`
70
+ CREATE TABLE IF NOT EXISTS integration_projections (
71
+ account_id TEXT NOT NULL REFERENCES integration_accounts(id) ON DELETE CASCADE,
72
+ capability TEXT NOT NULL,
73
+ target_kind TEXT NOT NULL,
74
+ target_id TEXT NOT NULL,
75
+ adopted INTEGER NOT NULL DEFAULT 0,
76
+ restore_json TEXT,
77
+ created_at TEXT NOT NULL,
78
+ PRIMARY KEY (account_id, capability, target_kind)
79
+ )
80
+ `);
81
+
82
+ db.exec(`
83
+ CREATE INDEX IF NOT EXISTS idx_integration_projections_target
84
+ ON integration_projections(target_kind, target_id)
85
+ `);
86
+
87
+ debug.log('migration', 'Creating integration_webhook_deliveries table...');
88
+
89
+ // Replay rejection. The UNIQUE key is the check: an INSERT that violates it
90
+ // IS the duplicate detection, atomically. A read-then-write would let two
91
+ // concurrent deliveries of the same event both pass the read.
92
+ db.exec(`
93
+ CREATE TABLE IF NOT EXISTS integration_webhook_deliveries (
94
+ provider TEXT NOT NULL,
95
+ delivery_id TEXT NOT NULL,
96
+ received_at TEXT NOT NULL,
97
+ PRIMARY KEY (provider, delivery_id)
98
+ )
99
+ `);
100
+
101
+ db.exec(`
102
+ CREATE INDEX IF NOT EXISTS idx_integration_webhook_received
103
+ ON integration_webhook_deliveries(received_at)
104
+ `);
105
+
106
+ debug.log('migration', 'integration tables created');
107
+ };
108
+
109
+ export const down = (db: DatabaseConnection): void => {
110
+ debug.log('migration', 'Dropping integration tables...');
111
+ db.exec('DROP INDEX IF EXISTS idx_integration_webhook_received');
112
+ db.exec('DROP TABLE IF EXISTS integration_webhook_deliveries');
113
+ db.exec('DROP INDEX IF EXISTS idx_integration_projections_target');
114
+ db.exec('DROP TABLE IF EXISTS integration_projections');
115
+ db.exec('DROP INDEX IF EXISTS idx_integration_accounts_project');
116
+ db.exec('DROP INDEX IF EXISTS idx_integration_accounts_provider');
117
+ db.exec('DROP TABLE IF EXISTS integration_accounts');
118
+ debug.log('migration', 'integration tables dropped');
119
+ };
@@ -0,0 +1,94 @@
1
+ import type { DatabaseConnection } from '$shared/types/database/connection';
2
+ import { debug } from '$shared/utils/logger';
3
+
4
+ export const description = 'Create work_bindings and work_links for the Issues & PRs surface';
5
+
6
+ export const up = (db: DatabaseConnection): void => {
7
+ debug.log('migration', 'Creating work_bindings table...');
8
+
9
+ // Which remote project a Clopen project is looking at, through one account.
10
+ //
11
+ // This belongs to the SURFACE, not to the account layer: an account is
12
+ // instance-wide (one GitHub token), while "which repo is this project" is
13
+ // per project. Two projects sharing one token is the normal case, and it is
14
+ // exactly the case a column on `integration_accounts` could not express.
15
+ //
16
+ // `locator` is provider-specific and opaque here — `owner/repo` for GitHub,
17
+ // a team key for a tracker. `detected` records that nobody chose it; it was
18
+ // read off the git remote. Keeping that flag is what lets the UI say "from
19
+ // your origin remote" instead of presenting a guess as a decision.
20
+ //
21
+ // `config_json` holds the per-binding behaviour: which state to move an item
22
+ // to on start-work and on PR open, and the branch template. It is NOT a
23
+ // secret and is stored in plain JSON.
24
+ db.exec(`
25
+ CREATE TABLE IF NOT EXISTS work_bindings (
26
+ project_id TEXT NOT NULL REFERENCES projects(id) ON DELETE CASCADE,
27
+ account_id TEXT NOT NULL REFERENCES integration_accounts(id) ON DELETE CASCADE,
28
+ locator TEXT NOT NULL,
29
+ detected INTEGER NOT NULL DEFAULT 0,
30
+ default_branch TEXT,
31
+ config_json TEXT NOT NULL DEFAULT '{}',
32
+ created_at TEXT NOT NULL,
33
+ updated_at TEXT NOT NULL,
34
+ PRIMARY KEY (project_id, account_id)
35
+ )
36
+ `);
37
+
38
+ db.exec(`
39
+ CREATE INDEX IF NOT EXISTS idx_work_bindings_account
40
+ ON work_bindings(account_id)
41
+ `);
42
+
43
+ debug.log('migration', 'Creating work_links table...');
44
+
45
+ // The link between a remote work item and the local work started for it.
46
+ //
47
+ // This is the ONLY thing about a work item that is persisted. Titles, bodies
48
+ // and comments are never copied into the database: they go stale, they turn
49
+ // a read-only surface into a cache that has to be invalidated, and the
50
+ // provider is always the truthful answer. What cannot be re-derived is which
51
+ // worktree and session someone opened for issue 123 — so that, and only
52
+ // that, is stored.
53
+ //
54
+ // Both foreign keys are ON DELETE SET NULL rather than CASCADE: deleting the
55
+ // worktree after applying its changes must not erase the record that this
56
+ // branch belongs to that issue, because the PR composer reads it to prefill
57
+ // "Closes #123" long after the worktree is gone.
58
+ db.exec(`
59
+ CREATE TABLE IF NOT EXISTS work_links (
60
+ id TEXT PRIMARY KEY,
61
+ project_id TEXT NOT NULL REFERENCES projects(id) ON DELETE CASCADE,
62
+ account_id TEXT NOT NULL REFERENCES integration_accounts(id) ON DELETE CASCADE,
63
+ item_kind TEXT NOT NULL CHECK (item_kind IN ('issue','pull-request')),
64
+ item_identifier TEXT NOT NULL,
65
+ item_title TEXT,
66
+ item_url TEXT,
67
+ worktree_id TEXT REFERENCES worktrees(id) ON DELETE SET NULL,
68
+ session_id TEXT REFERENCES chat_sessions(id) ON DELETE SET NULL,
69
+ branch TEXT,
70
+ created_at TEXT NOT NULL,
71
+ UNIQUE (project_id, account_id, item_kind, item_identifier)
72
+ )
73
+ `);
74
+
75
+ db.exec(`
76
+ CREATE INDEX IF NOT EXISTS idx_work_links_project
77
+ ON work_links(project_id)
78
+ `);
79
+
80
+ // The PR composer's lookup is "which item does this branch belong to", so
81
+ // that is the index it gets.
82
+ db.exec(`
83
+ CREATE INDEX IF NOT EXISTS idx_work_links_branch
84
+ ON work_links(project_id, branch)
85
+ `);
86
+
87
+ debug.log('migration', 'Work tables created');
88
+ };
89
+
90
+ export const down = (db: DatabaseConnection): void => {
91
+ debug.log('migration', 'Dropping work tables...');
92
+ db.exec('DROP TABLE IF EXISTS work_links');
93
+ db.exec('DROP TABLE IF EXISTS work_bindings');
94
+ };
@@ -0,0 +1,54 @@
1
+ import type { DatabaseConnection } from '$shared/types/database/connection';
2
+ import { debug } from '$shared/utils/logger';
3
+
4
+ export const description = 'Create deploy_bindings for the Deployments surface';
5
+
6
+ export const up = (db: DatabaseConnection): void => {
7
+ debug.log('migration', 'Creating deploy_bindings table...');
8
+
9
+ // Which remote project a Clopen project deploys to, through one account.
10
+ //
11
+ // Belongs to the SURFACE, not to the account layer, for the same reason
12
+ // `work_bindings` does: an account is one Vercel token, while "which Vercel
13
+ // project is this" is per project, and two projects sharing one token is the
14
+ // normal case.
15
+ //
16
+ // This is a sibling of `work_bindings` rather than a generalisation of it.
17
+ // The columns differ where it matters: `display_name` exists because a
18
+ // deployment locator is an opaque `prj_8Qc…` that means nothing to a reader,
19
+ // unlike an `owner/repo` that describes itself, and `team_id` exists because
20
+ // one token reaches several teams and the team is a property of which
21
+ // project you picked — not of the credential.
22
+ //
23
+ // Nothing about a deployment itself is stored. The provider is the truthful
24
+ // answer for state, logs and URLs, all three go stale within seconds, and
25
+ // there is no local artifact (no worktree, no branch) that would survive the
26
+ // remote record — so unlike the Issues surface there is not even a link
27
+ // table here.
28
+ db.exec(`
29
+ CREATE TABLE IF NOT EXISTS deploy_bindings (
30
+ project_id TEXT NOT NULL REFERENCES projects(id) ON DELETE CASCADE,
31
+ account_id TEXT NOT NULL REFERENCES integration_accounts(id) ON DELETE CASCADE,
32
+ locator TEXT NOT NULL,
33
+ display_name TEXT NOT NULL DEFAULT '',
34
+ team_id TEXT,
35
+ detected INTEGER NOT NULL DEFAULT 0,
36
+ config_json TEXT NOT NULL DEFAULT '{}',
37
+ created_at TEXT NOT NULL,
38
+ updated_at TEXT NOT NULL,
39
+ PRIMARY KEY (project_id, account_id)
40
+ )
41
+ `);
42
+
43
+ db.exec(`
44
+ CREATE INDEX IF NOT EXISTS idx_deploy_bindings_account
45
+ ON deploy_bindings(account_id)
46
+ `);
47
+
48
+ debug.log('migration', 'Deploy bindings table created');
49
+ };
50
+
51
+ export const down = (db: DatabaseConnection): void => {
52
+ debug.log('migration', 'Dropping deploy_bindings table...');
53
+ db.exec('DROP TABLE IF EXISTS deploy_bindings');
54
+ };