@myrialabs/clopen 0.4.39 → 0.4.40
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.
- package/backend/auth/permissions.ts +23 -0
- package/backend/database/crypto/secret-columns.ts +5 -1
- package/backend/database/migrations/078_create_worktree_branch_tables.ts +158 -0
- package/backend/database/migrations/index.ts +7 -0
- package/backend/database/queries/index.ts +6 -0
- package/backend/database/queries/worktree-branch-queries.ts +331 -0
- package/backend/db-client/cloning.test.ts +161 -0
- package/backend/db-client/cloning.ts +672 -0
- package/backend/db-client/connection-url.test.ts +117 -0
- package/backend/db-client/connection-url.ts +126 -0
- package/backend/db-client/drivers/mongodb.ts +2 -14
- package/backend/db-client/drivers/mysql.ts +2 -8
- package/backend/db-client/drivers/postgres.ts +10 -13
- package/backend/db-client/drivers/redis.ts +2 -6
- package/backend/db-client/env/detect.ts +438 -0
- package/backend/db-client/env/diff.ts +94 -0
- package/backend/db-client/env/env.test.ts +574 -0
- package/backend/db-client/env/index.ts +15 -0
- package/backend/db-client/env/naming.ts +202 -0
- package/backend/db-client/env/service.ts +487 -0
- package/backend/db-client/integrations/index.ts +7 -0
- package/backend/db-client/integrations/links.ts +41 -10
- package/backend/db-client/providers/neon/adapter.ts +522 -0
- package/backend/db-client/providers/neon/api-types.ts +182 -0
- package/backend/db-client/providers/neon/client.ts +298 -0
- package/backend/db-client/providers/neon/endpoints.test.ts +201 -0
- package/backend/db-client/providers/neon/endpoints.ts +314 -0
- package/backend/db-client/providers/neon/list.test.ts +181 -0
- package/backend/db-client/providers/supabase/adapter.ts +25 -2
- package/backend/db-client/providers/types.ts +57 -0
- package/backend/env-files/discovery.ts +48 -0
- package/backend/env-files/edit.ts +213 -0
- package/backend/env-files/env-files.test.ts +515 -0
- package/backend/env-files/git.ts +27 -0
- package/backend/env-files/index.ts +44 -0
- package/backend/env-files/keys.ts +69 -0
- package/backend/env-files/parse.ts +157 -0
- package/backend/env-files/write.ts +229 -0
- package/backend/integrations/health.ts +18 -4
- package/backend/integrations/projections/types.ts +9 -2
- package/backend/integrations/registry/index.ts +3 -0
- package/backend/integrations/registry/providers/context7.ts +1 -0
- package/backend/integrations/registry/providers/firecrawl.ts +1 -0
- package/backend/integrations/registry/providers/github.ts +1 -0
- package/backend/integrations/registry/providers/neon.ts +55 -0
- package/backend/integrations/registry/providers/supabase.ts +1 -0
- package/backend/integrations/registry/providers/vercel.ts +1 -0
- package/backend/integrations/registry/types.ts +2 -0
- package/backend/work/start-work.ts +4 -0
- package/backend/worktrees/branching/engine.ts +557 -0
- package/backend/worktrees/branching/index.ts +56 -0
- package/backend/worktrees/branching/naming.test.ts +118 -0
- package/backend/worktrees/branching/naming.ts +97 -0
- package/backend/worktrees/branching/projector.test.ts +229 -0
- package/backend/worktrees/branching/projector.ts +139 -0
- package/backend/worktrees/branching/providers/neon.ts +220 -0
- package/backend/worktrees/branching/registry.ts +27 -0
- package/backend/worktrees/branching/service.ts +620 -0
- package/backend/worktrees/branching/suggest.test.ts +154 -0
- package/backend/worktrees/branching/suggest.ts +202 -0
- package/backend/worktrees/branching/types.ts +88 -0
- package/backend/worktrees/index.ts +7 -0
- package/backend/worktrees/service.ts +48 -1
- package/backend/ws/db-client/env.ts +142 -0
- package/backend/ws/db-client/index.ts +2 -0
- package/backend/ws/projects/index.ts +6 -1
- package/backend/ws/projects/info.ts +70 -6
- package/backend/ws/projects/overview.test.ts +108 -0
- package/backend/ws/projects/overview.ts +300 -0
- package/backend/ws/system/device-info.ts +307 -76
- package/backend/ws/worktrees/branching.ts +212 -0
- package/backend/ws/worktrees/crud.ts +33 -7
- package/backend/ws/worktrees/index.ts +14 -0
- package/dist/assets/{index-C8Gi-b6U.js → index-D4igjF6e.js} +1 -1
- package/dist/assets/{index-BrF6nqld.js → index-DONNwiXm.js} +1 -1
- package/dist/assets/main-BrfXH1bL.js +1205 -0
- package/dist/assets/main-CT1PygsW.css +1 -0
- package/dist/index.html +2 -2
- package/package.json +1 -1
- package/shared/constants/tool-icons.ts +6 -2
- package/shared/types/db-client/env.ts +192 -0
- package/shared/types/db-client/index.ts +1 -0
- package/shared/types/db-client/integrations.ts +30 -0
- package/shared/types/integrations.ts +9 -0
- package/shared/types/worktree-branching.ts +315 -0
- package/shared/utils/logger.ts +1 -0
- package/dist/assets/main-Bd3gb79F.css +0 -1
- package/dist/assets/main-Bo1hD7mt.js +0 -1191
|
@@ -182,6 +182,29 @@ export const ADMIN_ONLY_ROUTES = new Set([
|
|
|
182
182
|
// anyone who can use the connection can look.
|
|
183
183
|
'db-client:supabase-apply-migration',
|
|
184
184
|
'db-client:supabase-write-types',
|
|
185
|
+
// Worktree database branching. Pointing a project at a parent database
|
|
186
|
+
// commits an account's quota to it, and deleting a branch destroys a real
|
|
187
|
+
// database — both spend a credential that belongs to the install rather than
|
|
188
|
+
// to the caller, the same reason `db-client:link` is gated.
|
|
189
|
+
//
|
|
190
|
+
// `worktrees:branching-state` and `worktrees:branch-rewrite-env` are
|
|
191
|
+
// deliberately NOT listed: a member has to be able to see whether their own
|
|
192
|
+
// worktree got a branch, and to retry the dotenv write when it was refused.
|
|
193
|
+
//
|
|
194
|
+
// NEITHER ARE `worktrees:branch-parents` AND `worktrees:branching-save`, and
|
|
195
|
+
// that is a change rather than an oversight. They used to be here because a
|
|
196
|
+
// binding could only point at an integration account, and committing the
|
|
197
|
+
// install's quota is an admin decision. A binding can now point at a DB
|
|
198
|
+
// Client connection instead — a database the member themselves saved — and
|
|
199
|
+
// refusing that would make "give this worktree its own database" an
|
|
200
|
+
// admin-only feature for no reason that survives being stated. The account
|
|
201
|
+
// half keeps the old rule: `listBranchSources` shows accounts to admins
|
|
202
|
+
// only, and `requireSource` refuses anything the caller cannot see.
|
|
203
|
+
'worktrees:branching-clear',
|
|
204
|
+
'worktrees:branch-orphans',
|
|
205
|
+
'worktrees:branch-delete',
|
|
206
|
+
'worktrees:branch-forget',
|
|
207
|
+
'worktrees:branch-delete-remote',
|
|
185
208
|
// Memory Graph — the graph is instance-global and is injected into every
|
|
186
209
|
// future turn on every engine, so editing it changes what every agent is told.
|
|
187
210
|
// Mutations only: the read surface (memory:graph / :node / :search / :stats /
|
|
@@ -31,7 +31,11 @@ export const SECRET_COLUMNS: Record<string, readonly string[]> = {
|
|
|
31
31
|
// The secret a linked remote database needs, which is NOT always the same as
|
|
32
32
|
// the account's credential — a Supabase Management API token cannot read
|
|
33
33
|
// back the Postgres password, so the password lives here and nowhere else.
|
|
34
|
-
integration_db_links: ['secrets']
|
|
34
|
+
integration_db_links: ['secrets'],
|
|
35
|
+
// A worktree's branch database answers on a connection string that carries
|
|
36
|
+
// its own password — generated by the provider and never typed by anyone, so
|
|
37
|
+
// this row is the only place it exists.
|
|
38
|
+
worktree_branches: ['connection_json']
|
|
35
39
|
} as const;
|
|
36
40
|
|
|
37
41
|
/** Column names of one table, or an empty list when the table holds no secrets. */
|
|
@@ -0,0 +1,158 @@
|
|
|
1
|
+
import type { DatabaseConnection } from '$shared/types/database/connection';
|
|
2
|
+
import { debug } from '$shared/utils/logger';
|
|
3
|
+
|
|
4
|
+
export const description = 'Create worktree_branch_bindings and worktree_branches for database branching';
|
|
5
|
+
|
|
6
|
+
/**
|
|
7
|
+
* Worktree database branching — the binding, and the branches it produced.
|
|
8
|
+
*
|
|
9
|
+
* Two tables, for the same reason the Issues surface has two: one records the
|
|
10
|
+
* decision ("give this project's worktrees their own database"), the other
|
|
11
|
+
* records what that decision actually created and therefore what has to be
|
|
12
|
+
* cleaned up.
|
|
13
|
+
*
|
|
14
|
+
* A SOURCE IS NOT ALWAYS AN ACCOUNT, which is why both tables carry
|
|
15
|
+
* `source_kind` and two nullable parent columns instead of a single
|
|
16
|
+
* `account_id`. Neon cuts a branch through its API and needs an integration
|
|
17
|
+
* account; a Postgres running on the user's laptop needs nothing but the DB
|
|
18
|
+
* Client connection that already reaches it. Tying the feature to accounts made
|
|
19
|
+
* it a Neon feature by accident — a database with no vendor behind it had no
|
|
20
|
+
* way in at all.
|
|
21
|
+
*/
|
|
22
|
+
export const up = (db: DatabaseConnection): void => {
|
|
23
|
+
debug.log('migration', 'Creating worktree_branch_bindings table...');
|
|
24
|
+
|
|
25
|
+
// ONE BINDING PER PROJECT, which is why `project_id` is the whole primary
|
|
26
|
+
// key rather than half of it as in `work_bindings` and `deploy_bindings`.
|
|
27
|
+
//
|
|
28
|
+
// Those two are keyed per (project, account) because a project genuinely can
|
|
29
|
+
// track issues in two places or deploy to two targets. This one cannot: the
|
|
30
|
+
// binding's entire output is a single environment variable inside a single
|
|
31
|
+
// worktree, and two bindings would both claim it. Making the schema hold a
|
|
32
|
+
// state the feature cannot express would only produce a silent last-writer.
|
|
33
|
+
//
|
|
34
|
+
// `parent_name` is cached alongside the ref for the reason `deploy_bindings`
|
|
35
|
+
// caches `display_name`: a Neon project id is `winter-frost-12345678` and
|
|
36
|
+
// answers no question anyone asked.
|
|
37
|
+
//
|
|
38
|
+
// Exactly ONE of `account_id` and `connection_id` is set, and `source_kind`
|
|
39
|
+
// says which. Both cascade: a disconnected account or a deleted connection
|
|
40
|
+
// leaves a binding that points at nothing, and a binding that points at
|
|
41
|
+
// nothing would fail on every worktree create with a message about a thing
|
|
42
|
+
// the user already removed.
|
|
43
|
+
db.exec(`
|
|
44
|
+
CREATE TABLE IF NOT EXISTS worktree_branch_bindings (
|
|
45
|
+
project_id TEXT PRIMARY KEY REFERENCES projects(id) ON DELETE CASCADE,
|
|
46
|
+
source_kind TEXT NOT NULL DEFAULT 'account',
|
|
47
|
+
account_id TEXT REFERENCES integration_accounts(id) ON DELETE CASCADE,
|
|
48
|
+
connection_id TEXT REFERENCES db_client_connections(id) ON DELETE CASCADE,
|
|
49
|
+
parent_ref TEXT NOT NULL,
|
|
50
|
+
parent_name TEXT NOT NULL,
|
|
51
|
+
config_json TEXT NOT NULL DEFAULT '{}',
|
|
52
|
+
created_at TEXT NOT NULL,
|
|
53
|
+
updated_at TEXT NOT NULL
|
|
54
|
+
)
|
|
55
|
+
`);
|
|
56
|
+
|
|
57
|
+
db.exec(`
|
|
58
|
+
CREATE INDEX IF NOT EXISTS idx_worktree_branch_bindings_account
|
|
59
|
+
ON worktree_branch_bindings(account_id)
|
|
60
|
+
`);
|
|
61
|
+
|
|
62
|
+
db.exec(`
|
|
63
|
+
CREATE INDEX IF NOT EXISTS idx_worktree_branch_bindings_connection
|
|
64
|
+
ON worktree_branch_bindings(connection_id)
|
|
65
|
+
`);
|
|
66
|
+
|
|
67
|
+
debug.log('migration', 'Creating worktree_branches table...');
|
|
68
|
+
|
|
69
|
+
// `worktree_id` is NULLABLE and ON DELETE SET NULL — deliberately NOT a
|
|
70
|
+
// cascade, and this is the load-bearing decision in the whole table.
|
|
71
|
+
//
|
|
72
|
+
// Deleting a worktree deletes its branch at the provider. When that call
|
|
73
|
+
// FAILS — an expired token, a provider outage, a branch the account can no
|
|
74
|
+
// longer reach — the remote branch is still there, costing money and holding
|
|
75
|
+
// data. A cascade would delete the only record of it in the same breath, so
|
|
76
|
+
// the leak would be both silent and unrecoverable: nothing left would know
|
|
77
|
+
// the branch had ever belonged to anything.
|
|
78
|
+
//
|
|
79
|
+
// So the row survives its worktree. `worktree_id IS NULL` is precisely the
|
|
80
|
+
// definition of an orphan, and the orphan list is built from it.
|
|
81
|
+
//
|
|
82
|
+
// `connection_json` holds the branch's own connection string INCLUDING the
|
|
83
|
+
// password, so it is sealed — see `SECRET_COLUMNS`. It lives here rather than
|
|
84
|
+
// only on the projected connection because the projection must be
|
|
85
|
+
// rebuildable from the row alone; a release followed by a re-project would
|
|
86
|
+
// otherwise lose the password and leave a connection that cannot log in.
|
|
87
|
+
// `source_connection_id` deliberately carries NO foreign key, unlike its
|
|
88
|
+
// twin on the binding. The same reasoning as `worktree_id` above: this row
|
|
89
|
+
// is the only record of a real database that exists right now, and deleting
|
|
90
|
+
// the DB Client connection it was copied from must not erase the evidence of
|
|
91
|
+
// the copy. The orphan list is what the user has left to clean it up with.
|
|
92
|
+
db.exec(`
|
|
93
|
+
CREATE TABLE IF NOT EXISTS worktree_branches (
|
|
94
|
+
id TEXT PRIMARY KEY,
|
|
95
|
+
worktree_id TEXT REFERENCES worktrees(id) ON DELETE SET NULL,
|
|
96
|
+
project_id TEXT NOT NULL REFERENCES projects(id) ON DELETE CASCADE,
|
|
97
|
+
source_kind TEXT NOT NULL DEFAULT 'account',
|
|
98
|
+
account_id TEXT REFERENCES integration_accounts(id) ON DELETE CASCADE,
|
|
99
|
+
source_connection_id TEXT,
|
|
100
|
+
parent_ref TEXT NOT NULL,
|
|
101
|
+
branch_ref TEXT NOT NULL,
|
|
102
|
+
branch_name TEXT NOT NULL,
|
|
103
|
+
connection_json TEXT,
|
|
104
|
+
connection_id TEXT,
|
|
105
|
+
env_var TEXT NOT NULL,
|
|
106
|
+
env_file TEXT NOT NULL,
|
|
107
|
+
env_status TEXT NOT NULL DEFAULT 'failed',
|
|
108
|
+
env_detail TEXT,
|
|
109
|
+
status TEXT NOT NULL DEFAULT 'active',
|
|
110
|
+
error TEXT,
|
|
111
|
+
strategy TEXT,
|
|
112
|
+
notice TEXT,
|
|
113
|
+
created_at TEXT NOT NULL,
|
|
114
|
+
updated_at TEXT NOT NULL
|
|
115
|
+
)
|
|
116
|
+
`);
|
|
117
|
+
|
|
118
|
+
db.exec(`
|
|
119
|
+
CREATE INDEX IF NOT EXISTS idx_worktree_branches_worktree
|
|
120
|
+
ON worktree_branches(worktree_id)
|
|
121
|
+
`);
|
|
122
|
+
|
|
123
|
+
db.exec(`
|
|
124
|
+
CREATE INDEX IF NOT EXISTS idx_worktree_branches_account
|
|
125
|
+
ON worktree_branches(account_id)
|
|
126
|
+
`);
|
|
127
|
+
|
|
128
|
+
// Identity is (source, parent, branch), and the source is whichever of the
|
|
129
|
+
// two columns is set. A table constraint cannot express that — SQLite treats
|
|
130
|
+
// every NULL as distinct, so `UNIQUE (account_id, …)` would stop enforcing
|
|
131
|
+
// anything the moment the source was a connection.
|
|
132
|
+
db.exec(`
|
|
133
|
+
CREATE UNIQUE INDEX IF NOT EXISTS idx_worktree_branches_identity
|
|
134
|
+
ON worktree_branches(
|
|
135
|
+
source_kind,
|
|
136
|
+
COALESCE(account_id, source_connection_id, ''),
|
|
137
|
+
parent_ref,
|
|
138
|
+
branch_ref
|
|
139
|
+
)
|
|
140
|
+
`);
|
|
141
|
+
|
|
142
|
+
debug.log('migration', 'Worktree branching tables created');
|
|
143
|
+
};
|
|
144
|
+
|
|
145
|
+
export const down = (db: DatabaseConnection): void => {
|
|
146
|
+
debug.log('migration', 'Dropping worktree branching tables...');
|
|
147
|
+
db.exec('DROP INDEX IF EXISTS idx_worktree_branches_identity');
|
|
148
|
+
db.exec('DROP INDEX IF EXISTS idx_worktree_branches_account');
|
|
149
|
+
db.exec('DROP INDEX IF EXISTS idx_worktree_branches_worktree');
|
|
150
|
+
db.exec('DROP TABLE IF EXISTS worktree_branches');
|
|
151
|
+
db.exec('DROP INDEX IF EXISTS idx_worktree_branch_bindings_connection');
|
|
152
|
+
db.exec('DROP INDEX IF EXISTS idx_worktree_branch_bindings_account');
|
|
153
|
+
db.exec('DROP TABLE IF EXISTS worktree_branch_bindings');
|
|
154
|
+
// The remote branches themselves are deliberately left alone. A rollback is
|
|
155
|
+
// a schema operation, and destroying someone's databases from one would be
|
|
156
|
+
// the single worst thing this migration could do.
|
|
157
|
+
debug.log('migration', 'Worktree branching tables dropped — remote branches were left untouched');
|
|
158
|
+
};
|
|
@@ -76,6 +76,7 @@ import * as migration074 from './074_create_work_tables';
|
|
|
76
76
|
import * as migration075 from './075_create_deploy_bindings';
|
|
77
77
|
import * as migration076 from './076_remove_memory_code_graph';
|
|
78
78
|
import * as migration077 from './077_create_integration_db_links';
|
|
79
|
+
import * as migration078 from './078_create_worktree_branch_tables';
|
|
79
80
|
|
|
80
81
|
// Export all migrations in order
|
|
81
82
|
export const migrations = [
|
|
@@ -540,6 +541,12 @@ export const migrations = [
|
|
|
540
541
|
description: migration077.description,
|
|
541
542
|
up: migration077.up,
|
|
542
543
|
down: migration077.down
|
|
544
|
+
},
|
|
545
|
+
{
|
|
546
|
+
id: '078',
|
|
547
|
+
description: migration078.description,
|
|
548
|
+
up: migration078.up,
|
|
549
|
+
down: migration078.down
|
|
543
550
|
}
|
|
544
551
|
];
|
|
545
552
|
|
|
@@ -41,3 +41,9 @@ export { workBindingQueries, workLinkQueries, parseBindingConfig } from './work-
|
|
|
41
41
|
export type { WorkBindingRow, WorkLinkRow } from './work-queries';
|
|
42
42
|
export { deployBindingQueries, parseDeployConfig } from './deploy-queries';
|
|
43
43
|
export type { DeployBindingRow } from './deploy-queries';
|
|
44
|
+
export {
|
|
45
|
+
worktreeBranchBindingQueries,
|
|
46
|
+
worktreeBranchQueries,
|
|
47
|
+
parseWorktreeBranchConfig
|
|
48
|
+
} from './worktree-branch-queries';
|
|
49
|
+
export type { WorktreeBranchBindingRow, WorktreeBranchRow } from './worktree-branch-queries';
|
|
@@ -0,0 +1,331 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Worktree database branching — the binding, and the branches it produced.
|
|
3
|
+
*
|
|
4
|
+
* The binding table holds no secret: a parent ref and four display settings.
|
|
5
|
+
* The branch table holds a whole connection string, password included, so
|
|
6
|
+
* `connection_json` is sealed at rest by the crypto layer — callers here hand in
|
|
7
|
+
* and receive a plain object and never see an envelope.
|
|
8
|
+
*
|
|
9
|
+
* A blob the active key cannot open reads as `null`, which every caller treats
|
|
10
|
+
* exactly as it treats a branch whose connection was never resolved: the branch
|
|
11
|
+
* is still listed, still deletable at the provider, and simply projects no
|
|
12
|
+
* connection. Throwing instead would take down the worktree list itself.
|
|
13
|
+
*/
|
|
14
|
+
|
|
15
|
+
import { getDatabase } from '../index';
|
|
16
|
+
import { openRow, openRows, sealFor } from '../crypto';
|
|
17
|
+
import type {
|
|
18
|
+
BranchConnection,
|
|
19
|
+
BranchEnvStatus,
|
|
20
|
+
BranchSourceKind,
|
|
21
|
+
WorktreeBranchConfig
|
|
22
|
+
} from '$shared/types/worktree-branching';
|
|
23
|
+
import { DEFAULT_WORKTREE_BRANCH_CONFIG } from '$shared/types/worktree-branching';
|
|
24
|
+
|
|
25
|
+
const BRANCH_TABLE = 'worktree_branches';
|
|
26
|
+
|
|
27
|
+
export interface WorktreeBranchBindingRow {
|
|
28
|
+
project_id: string;
|
|
29
|
+
source_kind: BranchSourceKind;
|
|
30
|
+
/** Set for an account source, null for a connection one. */
|
|
31
|
+
account_id: string | null;
|
|
32
|
+
/** Set for a connection source, null for an account one. */
|
|
33
|
+
connection_id: string | null;
|
|
34
|
+
parent_ref: string;
|
|
35
|
+
parent_name: string;
|
|
36
|
+
config_json: string;
|
|
37
|
+
created_at: string;
|
|
38
|
+
updated_at: string;
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
export interface WorktreeBranchRow {
|
|
42
|
+
id: string;
|
|
43
|
+
/** Null once the worktree is gone. That is what makes the row an orphan. */
|
|
44
|
+
worktree_id: string | null;
|
|
45
|
+
project_id: string;
|
|
46
|
+
source_kind: BranchSourceKind;
|
|
47
|
+
account_id: string | null;
|
|
48
|
+
/** The DB Client connection copied FROM. No FK — see migration 078. */
|
|
49
|
+
source_connection_id: string | null;
|
|
50
|
+
parent_ref: string;
|
|
51
|
+
branch_ref: string;
|
|
52
|
+
branch_name: string;
|
|
53
|
+
/** Sealed on disk, plain JSON string here. Null when the key could not open it. */
|
|
54
|
+
connection_json: string | null;
|
|
55
|
+
connection_id: string | null;
|
|
56
|
+
env_var: string;
|
|
57
|
+
env_file: string;
|
|
58
|
+
env_status: BranchEnvStatus;
|
|
59
|
+
env_detail: string | null;
|
|
60
|
+
status: 'active' | 'orphaned';
|
|
61
|
+
error: string | null;
|
|
62
|
+
/** Which mechanism made it, for the sentence the manager shows. */
|
|
63
|
+
strategy: string | null;
|
|
64
|
+
/** What that mechanism did not carry, or null. */
|
|
65
|
+
notice: string | null;
|
|
66
|
+
created_at: string;
|
|
67
|
+
updated_at: string;
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
/** The account or connection a row belongs to, whichever it is. */
|
|
71
|
+
export function sourceIdOf(row: { account_id: string | null; connection_id?: string | null; source_connection_id?: string | null }): string {
|
|
72
|
+
return row.account_id ?? row.source_connection_id ?? row.connection_id ?? '';
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
/**
|
|
76
|
+
* Parse the stored config over the defaults.
|
|
77
|
+
*
|
|
78
|
+
* Merged rather than replaced, for the reason `parseDeployConfig` merges: a
|
|
79
|
+
* config written before an option existed must not read back with that option
|
|
80
|
+
* `undefined`. Here that would be worse than a wrong filter — an `envVar` of
|
|
81
|
+
* `undefined` writes the literal string "undefined=" into someone's dotenv file.
|
|
82
|
+
*/
|
|
83
|
+
export function parseWorktreeBranchConfig(raw: string | null): WorktreeBranchConfig {
|
|
84
|
+
if (!raw) return { ...DEFAULT_WORKTREE_BRANCH_CONFIG };
|
|
85
|
+
try {
|
|
86
|
+
const parsed = JSON.parse(raw) as Partial<WorktreeBranchConfig> | null;
|
|
87
|
+
return { ...DEFAULT_WORKTREE_BRANCH_CONFIG, ...(parsed ?? {}) };
|
|
88
|
+
} catch {
|
|
89
|
+
return { ...DEFAULT_WORKTREE_BRANCH_CONFIG };
|
|
90
|
+
}
|
|
91
|
+
}
|
|
92
|
+
|
|
93
|
+
export const worktreeBranchBindingQueries = {
|
|
94
|
+
get(projectId: string): WorktreeBranchBindingRow | null {
|
|
95
|
+
const db = getDatabase();
|
|
96
|
+
return db.prepare(
|
|
97
|
+
`SELECT * FROM worktree_branch_bindings WHERE project_id = ?`
|
|
98
|
+
).get(projectId) as WorktreeBranchBindingRow | null;
|
|
99
|
+
},
|
|
100
|
+
|
|
101
|
+
getForAccount(accountId: string): WorktreeBranchBindingRow[] {
|
|
102
|
+
const db = getDatabase();
|
|
103
|
+
return db.prepare(
|
|
104
|
+
`SELECT * FROM worktree_branch_bindings WHERE account_id = ?`
|
|
105
|
+
).all(accountId) as WorktreeBranchBindingRow[];
|
|
106
|
+
},
|
|
107
|
+
|
|
108
|
+
upsert(input: {
|
|
109
|
+
projectId: string;
|
|
110
|
+
sourceKind: BranchSourceKind;
|
|
111
|
+
sourceId: string;
|
|
112
|
+
parentRef: string;
|
|
113
|
+
parentName: string;
|
|
114
|
+
config: WorktreeBranchConfig;
|
|
115
|
+
}): WorktreeBranchBindingRow {
|
|
116
|
+
const db = getDatabase();
|
|
117
|
+
const now = new Date().toISOString();
|
|
118
|
+
const existing = this.get(input.projectId);
|
|
119
|
+
const accountId = input.sourceKind === 'account' ? input.sourceId : null;
|
|
120
|
+
const connectionId = input.sourceKind === 'connection' ? input.sourceId : null;
|
|
121
|
+
|
|
122
|
+
db.prepare(`
|
|
123
|
+
INSERT INTO worktree_branch_bindings (
|
|
124
|
+
project_id, source_kind, account_id, connection_id,
|
|
125
|
+
parent_ref, parent_name, config_json, created_at, updated_at
|
|
126
|
+
) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?)
|
|
127
|
+
ON CONFLICT (project_id) DO UPDATE SET
|
|
128
|
+
source_kind = excluded.source_kind,
|
|
129
|
+
account_id = excluded.account_id,
|
|
130
|
+
connection_id = excluded.connection_id,
|
|
131
|
+
parent_ref = excluded.parent_ref,
|
|
132
|
+
parent_name = excluded.parent_name,
|
|
133
|
+
config_json = excluded.config_json,
|
|
134
|
+
updated_at = excluded.updated_at
|
|
135
|
+
`).run(
|
|
136
|
+
input.projectId,
|
|
137
|
+
input.sourceKind,
|
|
138
|
+
accountId,
|
|
139
|
+
connectionId,
|
|
140
|
+
input.parentRef,
|
|
141
|
+
input.parentName,
|
|
142
|
+
JSON.stringify(input.config),
|
|
143
|
+
existing?.created_at ?? now,
|
|
144
|
+
now
|
|
145
|
+
);
|
|
146
|
+
|
|
147
|
+
return this.get(input.projectId)!;
|
|
148
|
+
},
|
|
149
|
+
|
|
150
|
+
remove(projectId: string): void {
|
|
151
|
+
const db = getDatabase();
|
|
152
|
+
db.prepare(`DELETE FROM worktree_branch_bindings WHERE project_id = ?`).run(projectId);
|
|
153
|
+
}
|
|
154
|
+
};
|
|
155
|
+
|
|
156
|
+
function openBranch<T extends WorktreeBranchRow | null>(row: T): T {
|
|
157
|
+
return (row ? openRow(BRANCH_TABLE, row) : row) as T;
|
|
158
|
+
}
|
|
159
|
+
|
|
160
|
+
export const worktreeBranchQueries = {
|
|
161
|
+
getById(id: string): WorktreeBranchRow | null {
|
|
162
|
+
const db = getDatabase();
|
|
163
|
+
return openBranch(
|
|
164
|
+
db.prepare(`SELECT * FROM worktree_branches WHERE id = ?`).get(id) as WorktreeBranchRow | null
|
|
165
|
+
);
|
|
166
|
+
},
|
|
167
|
+
|
|
168
|
+
/** The branch attached to one worktree, or null when it has none. */
|
|
169
|
+
getForWorktree(worktreeId: string): WorktreeBranchRow | null {
|
|
170
|
+
const db = getDatabase();
|
|
171
|
+
return openBranch(
|
|
172
|
+
db.prepare(
|
|
173
|
+
`SELECT * FROM worktree_branches WHERE worktree_id = ? AND status = 'active'`
|
|
174
|
+
).get(worktreeId) as WorktreeBranchRow | null
|
|
175
|
+
);
|
|
176
|
+
},
|
|
177
|
+
|
|
178
|
+
getForAccount(accountId: string): WorktreeBranchRow[] {
|
|
179
|
+
const db = getDatabase();
|
|
180
|
+
return openRows(
|
|
181
|
+
BRANCH_TABLE,
|
|
182
|
+
db.prepare(
|
|
183
|
+
`SELECT * FROM worktree_branches WHERE account_id = ? ORDER BY created_at DESC`
|
|
184
|
+
).all(accountId) as WorktreeBranchRow[]
|
|
185
|
+
);
|
|
186
|
+
},
|
|
187
|
+
|
|
188
|
+
getForProject(projectId: string): WorktreeBranchRow[] {
|
|
189
|
+
const db = getDatabase();
|
|
190
|
+
return openRows(
|
|
191
|
+
BRANCH_TABLE,
|
|
192
|
+
db.prepare(
|
|
193
|
+
`SELECT * FROM worktree_branches WHERE project_id = ? ORDER BY created_at DESC`
|
|
194
|
+
).all(projectId) as WorktreeBranchRow[]
|
|
195
|
+
);
|
|
196
|
+
},
|
|
197
|
+
|
|
198
|
+
/**
|
|
199
|
+
* Every branch whose worktree is gone, or whose remote delete failed.
|
|
200
|
+
*
|
|
201
|
+
* The two are one query because they are one problem: a branch that still
|
|
202
|
+
* exists at the provider and is no longer attached to anything. Reporting
|
|
203
|
+
* them separately would ask the user to understand how each one got that way
|
|
204
|
+
* before they could act on either.
|
|
205
|
+
*/
|
|
206
|
+
orphans(): WorktreeBranchRow[] {
|
|
207
|
+
const db = getDatabase();
|
|
208
|
+
return openRows(
|
|
209
|
+
BRANCH_TABLE,
|
|
210
|
+
db.prepare(`
|
|
211
|
+
SELECT * FROM worktree_branches
|
|
212
|
+
WHERE worktree_id IS NULL OR status = 'orphaned'
|
|
213
|
+
ORDER BY created_at DESC
|
|
214
|
+
`).all() as WorktreeBranchRow[]
|
|
215
|
+
);
|
|
216
|
+
},
|
|
217
|
+
|
|
218
|
+
connectionOf(row: WorktreeBranchRow): BranchConnection | null {
|
|
219
|
+
if (!row.connection_json) return null;
|
|
220
|
+
try {
|
|
221
|
+
const parsed = JSON.parse(row.connection_json) as BranchConnection | null;
|
|
222
|
+
return parsed && typeof parsed.uri === 'string' && parsed.uri ? parsed : null;
|
|
223
|
+
} catch {
|
|
224
|
+
return null;
|
|
225
|
+
}
|
|
226
|
+
},
|
|
227
|
+
|
|
228
|
+
create(input: {
|
|
229
|
+
worktreeId: string;
|
|
230
|
+
projectId: string;
|
|
231
|
+
sourceKind: BranchSourceKind;
|
|
232
|
+
sourceId: string;
|
|
233
|
+
parentRef: string;
|
|
234
|
+
branchRef: string;
|
|
235
|
+
branchName: string;
|
|
236
|
+
connection: BranchConnection;
|
|
237
|
+
envVar: string;
|
|
238
|
+
envFile: string;
|
|
239
|
+
strategy: string | null;
|
|
240
|
+
notice: string | null;
|
|
241
|
+
}): WorktreeBranchRow {
|
|
242
|
+
const db = getDatabase();
|
|
243
|
+
const id = crypto.randomUUID();
|
|
244
|
+
const now = new Date().toISOString();
|
|
245
|
+
|
|
246
|
+
db.prepare(`
|
|
247
|
+
INSERT INTO worktree_branches (
|
|
248
|
+
id, worktree_id, project_id, source_kind, account_id, source_connection_id,
|
|
249
|
+
parent_ref, branch_ref, branch_name,
|
|
250
|
+
connection_json, connection_id, env_var, env_file, env_status, env_detail,
|
|
251
|
+
status, error, strategy, notice, created_at, updated_at
|
|
252
|
+
) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, NULL, ?, ?, 'failed', NULL, 'active', NULL, ?, ?, ?, ?)
|
|
253
|
+
`).run(
|
|
254
|
+
id,
|
|
255
|
+
input.worktreeId,
|
|
256
|
+
input.projectId,
|
|
257
|
+
input.sourceKind,
|
|
258
|
+
input.sourceKind === 'account' ? input.sourceId : null,
|
|
259
|
+
input.sourceKind === 'connection' ? input.sourceId : null,
|
|
260
|
+
input.parentRef,
|
|
261
|
+
input.branchRef,
|
|
262
|
+
input.branchName,
|
|
263
|
+
sealFor(BRANCH_TABLE, 'connection_json', JSON.stringify(input.connection)),
|
|
264
|
+
input.envVar,
|
|
265
|
+
input.envFile,
|
|
266
|
+
input.strategy,
|
|
267
|
+
input.notice,
|
|
268
|
+
now,
|
|
269
|
+
now
|
|
270
|
+
);
|
|
271
|
+
|
|
272
|
+
return this.getById(id)!;
|
|
273
|
+
},
|
|
274
|
+
|
|
275
|
+
/** Every branch copied from one DB Client connection. */
|
|
276
|
+
getForSourceConnection(connectionId: string): WorktreeBranchRow[] {
|
|
277
|
+
const db = getDatabase();
|
|
278
|
+
return openRows(
|
|
279
|
+
BRANCH_TABLE,
|
|
280
|
+
db.prepare(
|
|
281
|
+
`SELECT * FROM worktree_branches WHERE source_connection_id = ? ORDER BY created_at DESC`
|
|
282
|
+
).all(connectionId) as WorktreeBranchRow[]
|
|
283
|
+
);
|
|
284
|
+
},
|
|
285
|
+
|
|
286
|
+
/**
|
|
287
|
+
* Record what happened when the connection string was written.
|
|
288
|
+
*
|
|
289
|
+
* Separate from `create` on purpose, and the ORDER is the reason. The row is
|
|
290
|
+
* written BEFORE the dotenv file is touched, so a crash in between leaves a
|
|
291
|
+
* branch that is tracked and deletable rather than one nothing knows about.
|
|
292
|
+
* That means the row exists for a moment with no env result — and the
|
|
293
|
+
* column's default of `failed` is the honest reading of that moment, because
|
|
294
|
+
* a process that died there genuinely did not write anything.
|
|
295
|
+
*/
|
|
296
|
+
setEnvResult(id: string, status: BranchEnvStatus, detail: string | null): void {
|
|
297
|
+
const db = getDatabase();
|
|
298
|
+
db.prepare(
|
|
299
|
+
`UPDATE worktree_branches SET env_status = ?, env_detail = ?, updated_at = ? WHERE id = ?`
|
|
300
|
+
).run(status, detail, new Date().toISOString(), id);
|
|
301
|
+
},
|
|
302
|
+
|
|
303
|
+
/** Remember which DB Client connection this branch projected. */
|
|
304
|
+
setConnectionId(id: string, connectionId: string | null): void {
|
|
305
|
+
const db = getDatabase();
|
|
306
|
+
db.prepare(
|
|
307
|
+
`UPDATE worktree_branches SET connection_id = ?, updated_at = ? WHERE id = ?`
|
|
308
|
+
).run(connectionId, new Date().toISOString(), id);
|
|
309
|
+
},
|
|
310
|
+
|
|
311
|
+
/**
|
|
312
|
+
* Mark a branch as leaked.
|
|
313
|
+
*
|
|
314
|
+
* Called when the remote delete failed. The row deliberately keeps its
|
|
315
|
+
* connection and its refs: they are exactly what a later retry needs, and
|
|
316
|
+
* clearing them would turn a reportable orphan into an unreachable one.
|
|
317
|
+
*/
|
|
318
|
+
markOrphaned(id: string, error: string): void {
|
|
319
|
+
const db = getDatabase();
|
|
320
|
+
db.prepare(`
|
|
321
|
+
UPDATE worktree_branches
|
|
322
|
+
SET status = 'orphaned', error = ?, worktree_id = NULL, updated_at = ?
|
|
323
|
+
WHERE id = ?
|
|
324
|
+
`).run(error, new Date().toISOString(), id);
|
|
325
|
+
},
|
|
326
|
+
|
|
327
|
+
remove(id: string): void {
|
|
328
|
+
const db = getDatabase();
|
|
329
|
+
db.prepare(`DELETE FROM worktree_branches WHERE id = ?`).run(id);
|
|
330
|
+
}
|
|
331
|
+
};
|