@myrialabs/clopen 0.4.41 → 0.4.43
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/chat/stream-manager.ts +8 -0
- package/backend/database/crypto/secret-columns.ts +5 -1
- package/backend/database/index.ts +18 -0
- package/backend/database/migrations/082_create_git_identities.test.ts +186 -0
- package/backend/database/migrations/082_create_git_identities.ts +122 -0
- package/backend/database/migrations/index.ts +7 -0
- package/backend/database/queries/git-identity-queries.ts +290 -0
- package/backend/database/queries/index.ts +2 -0
- package/backend/db-client/env/detect.ts +2 -1
- package/backend/engine/adapters/claude/environment.ts +11 -1
- package/backend/engine/adapters/claude/models.ts +147 -18
- package/backend/engine/adapters/claude/stream.ts +1 -1
- package/backend/engine/adapters/codex/models.ts +13 -9
- package/backend/engine/adapters/codex/stream.ts +35 -3
- package/backend/engine/adapters/opencode/server.ts +26 -2
- package/backend/engine/adapters/opencode/stream.ts +4 -1
- package/backend/engine/adapters/qwen/stream.ts +3 -1
- package/backend/engine/types.ts +10 -0
- package/backend/env-files/discovery.ts +2 -1
- package/backend/files/file-reading.ts +4 -3
- package/backend/files/path-browsing.ts +2 -1
- package/backend/git/git-executor.ts +28 -2
- package/backend/git/git-service.ts +61 -41
- package/backend/git/identity/cli.test.ts +58 -0
- package/backend/git/identity/cli.ts +201 -0
- package/backend/git/identity/credential-helper.test.ts +242 -0
- package/backend/git/identity/env.test.ts +214 -0
- package/backend/git/identity/env.ts +193 -0
- package/backend/git/identity/import-local.test.ts +199 -0
- package/backend/git/identity/import-local.ts +141 -0
- package/backend/git/identity/index.ts +78 -0
- package/backend/git/identity/keys.ts +183 -0
- package/backend/git/identity/resolve.ts +140 -0
- package/backend/git/identity/scope-config.test.ts +145 -0
- package/backend/git/identity/scope-config.ts +175 -0
- package/backend/git/identity/service.ts +297 -0
- package/backend/index.ts +16 -0
- package/backend/preview/browser/browser-console-manager.ts +214 -34
- package/backend/preview/browser/browser-pool.ts +526 -123
- package/backend/preview/browser/browser-preview-service.ts +192 -8
- package/backend/preview/browser/browser-tab-lifecycle.test.ts +240 -0
- package/backend/preview/browser/browser-tab-lifecycle.ts +495 -0
- package/backend/preview/browser/browser-tab-manager.ts +291 -65
- package/backend/preview/browser/capture-profile.ts +15 -1
- package/backend/preview/browser/types.ts +26 -1
- package/backend/snapshot/turn-changes.ts +3 -2
- package/backend/ssh/sftp.ts +2 -1
- package/backend/terminal/ptykit.ts +24 -0
- package/backend/worktrees/merge.ts +2 -1
- package/backend/ws/git/branch.ts +17 -6
- package/backend/ws/git/commit.ts +6 -5
- package/backend/ws/git/conflict.ts +3 -2
- package/backend/ws/git/identity-context.ts +91 -0
- package/backend/ws/git/identity.ts +200 -0
- package/backend/ws/git/index.ts +3 -1
- package/backend/ws/git/remote.ts +57 -11
- package/backend/ws/preview/browser/data.ts +60 -0
- package/backend/ws/preview/browser/interact.ts +6 -0
- package/backend/ws/preview/browser/tab-info.ts +9 -1
- package/backend/ws/preview/browser/tab.ts +64 -8
- package/backend/ws/preview/index.ts +21 -0
- package/bin/clopen.ts +9 -0
- package/dist/assets/{index-CeNKKSDR.js → index-B6u8cait.js} +1 -1
- package/dist/assets/{index-DW89566K.js → index-BeUKiRiY.js} +1 -1
- package/dist/assets/{main-Cy5DXWPT.js → main-CMRXlv2f.js} +604 -596
- package/dist/assets/main-DzCY0n-j.css +1 -0
- package/dist/index.html +2 -2
- package/package.json +2 -2
- package/shared/types/git-identity.ts +112 -0
- package/shared/utils/compare.test.ts +46 -0
- package/shared/utils/compare.ts +21 -0
- package/dist/assets/main-DJKj_4By.css +0 -1
|
@@ -45,6 +45,7 @@ import { extractMessageText } from '../snapshot/helpers';
|
|
|
45
45
|
import { deferEpisodicIngest, ingestTurn } from '../memory/extract';
|
|
46
46
|
import { buildMemoryContext, withMemoryContext } from '../memory/context';
|
|
47
47
|
import { buildEngineHandoff, resolveBranchEngine, withHandoff } from './engine-handoff';
|
|
48
|
+
import { resolveGitEnv } from '$backend/git/identity';
|
|
48
49
|
import { debug } from '$shared/utils/logger';
|
|
49
50
|
import { DEFAULT_MODEL_ID, DEFAULT_MODEL_NAME } from '$shared/constants/engines';
|
|
50
51
|
|
|
@@ -889,6 +890,13 @@ class StreamManager extends EventEmitter {
|
|
|
889
890
|
...(projectId && chatSessionId && {
|
|
890
891
|
mcpContext: { projectId, chatSessionId, streamId: streamState.streamId, ...(activeProfileId != null && { profileId: activeProfileId }) }
|
|
891
892
|
}),
|
|
893
|
+
// The agent commits as the person who asked it to. Resolved here
|
|
894
|
+
// because this is the only layer that knows both the project and
|
|
895
|
+
// the requester; omitted when either is missing, which leaves git
|
|
896
|
+
// behaving exactly as it did before identities existed.
|
|
897
|
+
...(projectId && streamState.requestedByUserId && {
|
|
898
|
+
gitIdentityEnv: resolveGitEnv(projectId, streamState.requestedByUserId)
|
|
899
|
+
}),
|
|
892
900
|
});
|
|
893
901
|
|
|
894
902
|
await projectContextService.runWithContextAsync(
|
|
@@ -35,7 +35,11 @@ export const SECRET_COLUMNS: Record<string, readonly string[]> = {
|
|
|
35
35
|
// A worktree's branch database answers on a connection string that carries
|
|
36
36
|
// its own password — generated by the provider and never typed by anyone, so
|
|
37
37
|
// this row is the only place it exists.
|
|
38
|
-
worktree_branches: ['connection_json']
|
|
38
|
+
worktree_branches: ['connection_json'],
|
|
39
|
+
// What a git identity proves itself to a remote with. The public key is not
|
|
40
|
+
// listed: it is public, and keeping it readable lets the UI show it for
|
|
41
|
+
// copy-to-GitHub without opening the envelope.
|
|
42
|
+
git_identities: ['ssh_private_key', 'ssh_passphrase', 'https_token']
|
|
39
43
|
} as const;
|
|
40
44
|
|
|
41
45
|
/** Column names of one table, or an empty list when the table holds no secrets. */
|
|
@@ -64,6 +64,24 @@ async function runSeeders(db: DatabaseConnection): Promise<void> {
|
|
|
64
64
|
debug.log('database', '✅ Seeders completed');
|
|
65
65
|
}
|
|
66
66
|
|
|
67
|
+
/**
|
|
68
|
+
* Connect to an EXISTING database without migrating or seeding it.
|
|
69
|
+
*
|
|
70
|
+
* For short-lived side processes that only read a row — the git credential and
|
|
71
|
+
* askpass helpers git spawns mid-command. `initializeDatabase()` is wrong for
|
|
72
|
+
* them twice over: it would run migrations from a process nobody is watching,
|
|
73
|
+
* racing the schema of the server that is already running, and it would pay for
|
|
74
|
+
* seeders on every single authentication.
|
|
75
|
+
*
|
|
76
|
+
* Sets the same module-level manager `getDatabase()` reads, which is the part
|
|
77
|
+
* that makes the queries usable — connecting the manager alone does not.
|
|
78
|
+
*/
|
|
79
|
+
export async function connectExistingDatabase(): Promise<DatabaseConnection> {
|
|
80
|
+
if (dbManager?.isConnected()) return dbManager.getConnection();
|
|
81
|
+
dbManager = DatabaseManager.getInstance();
|
|
82
|
+
return await dbManager.connect();
|
|
83
|
+
}
|
|
84
|
+
|
|
67
85
|
export function getDatabase(): DatabaseConnection {
|
|
68
86
|
if (!dbManager || !dbManager.isConnected()) {
|
|
69
87
|
throw new Error('Database not initialized. Call initializeDatabase() first.');
|
|
@@ -0,0 +1,186 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The migration's guarantees are in the SQL, so this runs it against a real
|
|
3
|
+
* in-memory SQLite rather than asserting over a mock.
|
|
4
|
+
*
|
|
5
|
+
* Two of them are easy to write and easy to get wrong: the partial unique index
|
|
6
|
+
* that allows at most one default per user, and the cascades that stop a
|
|
7
|
+
* deleted user or project from leaving rows pointing at nothing.
|
|
8
|
+
*/
|
|
9
|
+
|
|
10
|
+
import { describe, expect, test } from 'bun:test';
|
|
11
|
+
import { Database } from 'bun:sqlite';
|
|
12
|
+
import type { DatabaseConnection } from '$shared/types/database/connection';
|
|
13
|
+
import { up, down } from './082_create_git_identities';
|
|
14
|
+
|
|
15
|
+
/** Only the columns the new tables reference. */
|
|
16
|
+
function freshDb(): Database {
|
|
17
|
+
const db = new Database(':memory:');
|
|
18
|
+
db.exec('PRAGMA foreign_keys = ON');
|
|
19
|
+
db.exec(`CREATE TABLE users (id TEXT PRIMARY KEY, name TEXT NOT NULL)`);
|
|
20
|
+
db.exec(`CREATE TABLE projects (id TEXT PRIMARY KEY, name TEXT NOT NULL)`);
|
|
21
|
+
db.exec(`CREATE TABLE integration_accounts (id TEXT PRIMARY KEY, label TEXT NOT NULL)`);
|
|
22
|
+
db.exec(`INSERT INTO users (id, name) VALUES ('u1', 'Ada'), ('u2', 'Grace')`);
|
|
23
|
+
db.exec(`INSERT INTO projects (id, name) VALUES ('p1', 'Proj')`);
|
|
24
|
+
up(db as unknown as DatabaseConnection);
|
|
25
|
+
return db;
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
function insertIdentity(db: Database, id: string, userId: string, isDefault = 0): void {
|
|
29
|
+
db.prepare(
|
|
30
|
+
`INSERT INTO git_identities (id, owner_user_id, label, name, email, is_default, created_at, updated_at)
|
|
31
|
+
VALUES (?, ?, ?, 'Ada', 'ada@example.com', ?, '2026-01-01', '2026-01-01')`
|
|
32
|
+
).run(id, userId, `label-${id}`, isDefault);
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
describe('migration 082', () => {
|
|
36
|
+
test('a user can hold at most one default identity', () => {
|
|
37
|
+
const db = freshDb();
|
|
38
|
+
insertIdentity(db, 'i1', 'u1', 1);
|
|
39
|
+
expect(() => insertIdentity(db, 'i2', 'u1', 1)).toThrow();
|
|
40
|
+
// Non-defaults are unconstrained — the index is partial, not global.
|
|
41
|
+
expect(() => insertIdentity(db, 'i3', 'u1', 0)).not.toThrow();
|
|
42
|
+
expect(() => insertIdentity(db, 'i4', 'u1', 0)).not.toThrow();
|
|
43
|
+
db.close();
|
|
44
|
+
});
|
|
45
|
+
|
|
46
|
+
test('the default is per user, not per install', () => {
|
|
47
|
+
const db = freshDb();
|
|
48
|
+
insertIdentity(db, 'i1', 'u1', 1);
|
|
49
|
+
expect(() => insertIdentity(db, 'i2', 'u2', 1)).not.toThrow();
|
|
50
|
+
db.close();
|
|
51
|
+
});
|
|
52
|
+
|
|
53
|
+
test('one user cannot reuse a label', () => {
|
|
54
|
+
const db = freshDb();
|
|
55
|
+
insertIdentity(db, 'i1', 'u1');
|
|
56
|
+
expect(() =>
|
|
57
|
+
db
|
|
58
|
+
.prepare(
|
|
59
|
+
`INSERT INTO git_identities (id, owner_user_id, label, name, email, created_at, updated_at)
|
|
60
|
+
VALUES ('i2', 'u1', 'label-i1', 'A', 'a@b.c', '2026-01-01', '2026-01-01')`
|
|
61
|
+
)
|
|
62
|
+
.run()
|
|
63
|
+
).toThrow();
|
|
64
|
+
db.close();
|
|
65
|
+
});
|
|
66
|
+
|
|
67
|
+
test('auth_method is constrained to the methods the code handles', () => {
|
|
68
|
+
const db = freshDb();
|
|
69
|
+
expect(() =>
|
|
70
|
+
db
|
|
71
|
+
.prepare(
|
|
72
|
+
`INSERT INTO git_identities (id, owner_user_id, label, name, email, auth_method, created_at, updated_at)
|
|
73
|
+
VALUES ('i9', 'u1', 'l9', 'A', 'a@b.c', 'carrier-pigeon', '2026-01-01', '2026-01-01')`
|
|
74
|
+
)
|
|
75
|
+
.run()
|
|
76
|
+
).toThrow();
|
|
77
|
+
db.close();
|
|
78
|
+
});
|
|
79
|
+
|
|
80
|
+
test('deleting a user takes their identities and bindings with them', () => {
|
|
81
|
+
const db = freshDb();
|
|
82
|
+
insertIdentity(db, 'i1', 'u1');
|
|
83
|
+
db.prepare(
|
|
84
|
+
`INSERT INTO git_identity_bindings (project_id, user_id, identity_id, created_at, updated_at)
|
|
85
|
+
VALUES ('p1', 'u1', 'i1', '2026-01-01', '2026-01-01')`
|
|
86
|
+
).run();
|
|
87
|
+
|
|
88
|
+
db.prepare(`DELETE FROM users WHERE id = 'u1'`).run();
|
|
89
|
+
|
|
90
|
+
expect(db.prepare('SELECT COUNT(*) as c FROM git_identities').get()).toEqual({ c: 0 });
|
|
91
|
+
expect(db.prepare('SELECT COUNT(*) as c FROM git_identity_bindings').get()).toEqual({ c: 0 });
|
|
92
|
+
db.close();
|
|
93
|
+
});
|
|
94
|
+
|
|
95
|
+
test('deleting an identity clears the bindings that pointed at it', () => {
|
|
96
|
+
const db = freshDb();
|
|
97
|
+
insertIdentity(db, 'i1', 'u1');
|
|
98
|
+
db.prepare(
|
|
99
|
+
`INSERT INTO git_identity_bindings (project_id, user_id, identity_id, created_at, updated_at)
|
|
100
|
+
VALUES ('p1', 'u1', 'i1', '2026-01-01', '2026-01-01')`
|
|
101
|
+
).run();
|
|
102
|
+
|
|
103
|
+
db.prepare(`DELETE FROM git_identities WHERE id = 'i1'`).run();
|
|
104
|
+
expect(db.prepare('SELECT COUNT(*) as c FROM git_identity_bindings').get()).toEqual({ c: 0 });
|
|
105
|
+
db.close();
|
|
106
|
+
});
|
|
107
|
+
|
|
108
|
+
test('one project holds one binding per user', () => {
|
|
109
|
+
const db = freshDb();
|
|
110
|
+
insertIdentity(db, 'i1', 'u1');
|
|
111
|
+
insertIdentity(db, 'i2', 'u1');
|
|
112
|
+
const bind = (identityId: string) =>
|
|
113
|
+
db
|
|
114
|
+
.prepare(
|
|
115
|
+
`INSERT INTO git_identity_bindings (project_id, user_id, identity_id, created_at, updated_at)
|
|
116
|
+
VALUES ('p1', 'u1', ?, '2026-01-01', '2026-01-01')
|
|
117
|
+
ON CONFLICT(project_id, user_id) DO UPDATE SET identity_id = excluded.identity_id`
|
|
118
|
+
)
|
|
119
|
+
.run(identityId);
|
|
120
|
+
|
|
121
|
+
bind('i1');
|
|
122
|
+
bind('i2');
|
|
123
|
+
const rows = db.prepare('SELECT identity_id FROM git_identity_bindings').all();
|
|
124
|
+
expect(rows).toEqual([{ identity_id: 'i2' }]);
|
|
125
|
+
db.close();
|
|
126
|
+
});
|
|
127
|
+
|
|
128
|
+
test('a disconnected integration account leaves the identity intact', () => {
|
|
129
|
+
// The account is where a borrowed token lives; losing it must not delete
|
|
130
|
+
// the identity, which still carries the user's name and email.
|
|
131
|
+
const db = freshDb();
|
|
132
|
+
db.prepare(`INSERT INTO integration_accounts (id, label) VALUES ('a1', 'GitHub')`).run();
|
|
133
|
+
db.prepare(
|
|
134
|
+
`INSERT INTO git_identities (id, owner_user_id, label, name, email, auth_method, integration_account_id, created_at, updated_at)
|
|
135
|
+
VALUES ('i1', 'u1', 'l1', 'Ada', 'ada@example.com', 'https-account', 'a1', '2026-01-01', '2026-01-01')`
|
|
136
|
+
).run();
|
|
137
|
+
|
|
138
|
+
db.prepare(`DELETE FROM integration_accounts WHERE id = 'a1'`).run();
|
|
139
|
+
|
|
140
|
+
const row = db.prepare('SELECT integration_account_id FROM git_identities WHERE id = ?').get('i1');
|
|
141
|
+
expect(row).toEqual({ integration_account_id: null });
|
|
142
|
+
db.close();
|
|
143
|
+
});
|
|
144
|
+
|
|
145
|
+
test('a user holds at most one mirrored machine identity', () => {
|
|
146
|
+
// The import runs on every list read, so without this a machine config
|
|
147
|
+
// change would append a second copy instead of updating the first.
|
|
148
|
+
const db = freshDb();
|
|
149
|
+
const insertLocal = (id: string, userId: string) =>
|
|
150
|
+
db
|
|
151
|
+
.prepare(
|
|
152
|
+
`INSERT INTO git_identities (id, owner_user_id, label, name, email, source, created_at, updated_at)
|
|
153
|
+
VALUES (?, ?, ?, 'A', 'a@b.c', 'local-machine', '2026-01-01', '2026-01-01')`
|
|
154
|
+
)
|
|
155
|
+
.run(id, userId, `l-${id}`);
|
|
156
|
+
|
|
157
|
+
insertLocal('m1', 'u1');
|
|
158
|
+
expect(() => insertLocal('m2', 'u1')).toThrow();
|
|
159
|
+
// Still per user, not per install.
|
|
160
|
+
expect(() => insertLocal('m3', 'u2')).not.toThrow();
|
|
161
|
+
db.close();
|
|
162
|
+
});
|
|
163
|
+
|
|
164
|
+
test('source is constrained to the two the code knows', () => {
|
|
165
|
+
const db = freshDb();
|
|
166
|
+
expect(() =>
|
|
167
|
+
db
|
|
168
|
+
.prepare(
|
|
169
|
+
`INSERT INTO git_identities (id, owner_user_id, label, name, email, source, created_at, updated_at)
|
|
170
|
+
VALUES ('x', 'u1', 'lx', 'A', 'a@b.c', 'imported-from-space', '2026-01-01', '2026-01-01')`
|
|
171
|
+
)
|
|
172
|
+
.run()
|
|
173
|
+
).toThrow();
|
|
174
|
+
db.close();
|
|
175
|
+
});
|
|
176
|
+
|
|
177
|
+
test('down() removes both tables', () => {
|
|
178
|
+
const db = freshDb();
|
|
179
|
+
down(db as unknown as DatabaseConnection);
|
|
180
|
+
const tables = db
|
|
181
|
+
.prepare(`SELECT name FROM sqlite_master WHERE type = 'table' AND name LIKE 'git_identit%'`)
|
|
182
|
+
.all();
|
|
183
|
+
expect(tables).toEqual([]);
|
|
184
|
+
db.close();
|
|
185
|
+
});
|
|
186
|
+
});
|
|
@@ -0,0 +1,122 @@
|
|
|
1
|
+
import type { DatabaseConnection } from '$shared/types/database/connection';
|
|
2
|
+
|
|
3
|
+
import { debug } from '$shared/utils/logger';
|
|
4
|
+
export const description = 'Create git_identities and git_identity_bindings for per-project git accounts';
|
|
5
|
+
|
|
6
|
+
/**
|
|
7
|
+
* Who a commit is authored by, and which credential reaches the remote.
|
|
8
|
+
*
|
|
9
|
+
* Until now Clopen ran `git` with whatever the machine's `.git/config` or
|
|
10
|
+
* `~/.gitconfig` said. On a laptop that silently works; on a VPS or in Docker
|
|
11
|
+
* it means commits carry a fabricated identity, or fail outright, and an HTTPS
|
|
12
|
+
* push dies against `GIT_TERMINAL_PROMPT=0` with no way to ask for a password.
|
|
13
|
+
*
|
|
14
|
+
* ── Two tables, not one ──
|
|
15
|
+
* An identity is a reusable account: a name, an email, and the credential that
|
|
16
|
+
* proves it to a host. A binding is a per-project, per-user choice of which one
|
|
17
|
+
* to use. They are split for the same reason `deploy_bindings` is separate from
|
|
18
|
+
* `integration_accounts`: one identity normally serves many projects, and
|
|
19
|
+
* folding the choice into the account would make a user re-enter (and re-rotate)
|
|
20
|
+
* the same key for every repository.
|
|
21
|
+
*
|
|
22
|
+
* ── Why identities belong to a user ──
|
|
23
|
+
* Clopen is multi-user. An identity is a claim about who a person is, so it is
|
|
24
|
+
* owned by `users.id` and a teammate's commits can never be attributed to
|
|
25
|
+
* someone else. The binding is keyed `(project_id, user_id)` for the same
|
|
26
|
+
* reason: two people sharing a project each commit as themselves.
|
|
27
|
+
*
|
|
28
|
+
* ── `hosts` ──
|
|
29
|
+
* A JSON array of hostnames this identity's credential is valid for
|
|
30
|
+
* (`["github.com"]`). Resolution is per project first, but a repository can
|
|
31
|
+
* have `origin` and `upstream` on different accounts, so the credential is
|
|
32
|
+
* chosen by matching the remote URL's host. An empty array means "no host
|
|
33
|
+
* claim" — usable for attribution, never selected for credentials.
|
|
34
|
+
*
|
|
35
|
+
* ── `source` ──
|
|
36
|
+
* `local-machine` marks the identity mirrored from the machine's own
|
|
37
|
+
* `git config --global`. It exists so a fresh install already lists the account
|
|
38
|
+
* the user has been committing with rather than showing an empty page, and it is
|
|
39
|
+
* NOT deletable: the machine's config is the thing it reflects, so deleting the
|
|
40
|
+
* row would only make it reappear. Everything else about it is editable — a user
|
|
41
|
+
* who wants a different email on it is expressing a preference, not a mistake,
|
|
42
|
+
* and Clopen stops mirroring once they do.
|
|
43
|
+
*
|
|
44
|
+
* Secrets (`ssh_private_key`, `ssh_passphrase`, `https_token`) are stored
|
|
45
|
+
* through the AES-256-GCM envelope in `backend/database/crypto`, the same way
|
|
46
|
+
* `ssh_connections` stores its keys.
|
|
47
|
+
*/
|
|
48
|
+
export const up = (db: DatabaseConnection): void => {
|
|
49
|
+
debug.log('migration', '📋 Creating git_identities table...');
|
|
50
|
+
|
|
51
|
+
db.exec(`
|
|
52
|
+
CREATE TABLE git_identities (
|
|
53
|
+
id TEXT PRIMARY KEY,
|
|
54
|
+
owner_user_id TEXT NOT NULL REFERENCES users(id) ON DELETE CASCADE,
|
|
55
|
+
label TEXT NOT NULL,
|
|
56
|
+
name TEXT NOT NULL,
|
|
57
|
+
email TEXT NOT NULL,
|
|
58
|
+
auth_method TEXT NOT NULL DEFAULT 'none'
|
|
59
|
+
CHECK (auth_method IN ('none','ssh-key','https-token','https-account')),
|
|
60
|
+
ssh_private_key TEXT,
|
|
61
|
+
ssh_public_key TEXT,
|
|
62
|
+
ssh_passphrase TEXT,
|
|
63
|
+
https_username TEXT,
|
|
64
|
+
https_token TEXT,
|
|
65
|
+
integration_account_id TEXT REFERENCES integration_accounts(id) ON DELETE SET NULL,
|
|
66
|
+
hosts TEXT NOT NULL DEFAULT '[]',
|
|
67
|
+
is_default INTEGER NOT NULL DEFAULT 0,
|
|
68
|
+
source TEXT NOT NULL DEFAULT 'manual'
|
|
69
|
+
CHECK (source IN ('manual','local-machine')),
|
|
70
|
+
created_at TEXT NOT NULL,
|
|
71
|
+
updated_at TEXT NOT NULL,
|
|
72
|
+
UNIQUE (owner_user_id, label)
|
|
73
|
+
)
|
|
74
|
+
`);
|
|
75
|
+
|
|
76
|
+
db.exec(`
|
|
77
|
+
CREATE INDEX idx_git_identities_owner ON git_identities (owner_user_id)
|
|
78
|
+
`);
|
|
79
|
+
|
|
80
|
+
// At most one default per user. A partial index is the only way SQLite can
|
|
81
|
+
// state that rule, and stating it here means a bug in the service layer
|
|
82
|
+
// surfaces as a constraint error rather than as two defaults and a coin flip
|
|
83
|
+
// over which identity a project inherits.
|
|
84
|
+
db.exec(`
|
|
85
|
+
CREATE UNIQUE INDEX idx_git_identities_one_default
|
|
86
|
+
ON git_identities (owner_user_id) WHERE is_default = 1
|
|
87
|
+
`);
|
|
88
|
+
|
|
89
|
+
// One mirrored row per user. The import runs whenever the list is read, so
|
|
90
|
+
// without this a machine config change would append a second copy instead of
|
|
91
|
+
// updating the first.
|
|
92
|
+
db.exec(`
|
|
93
|
+
CREATE UNIQUE INDEX idx_git_identities_one_local
|
|
94
|
+
ON git_identities (owner_user_id) WHERE source = 'local-machine'
|
|
95
|
+
`);
|
|
96
|
+
|
|
97
|
+
debug.log('migration', '📋 Creating git_identity_bindings table...');
|
|
98
|
+
|
|
99
|
+
db.exec(`
|
|
100
|
+
CREATE TABLE git_identity_bindings (
|
|
101
|
+
project_id TEXT NOT NULL REFERENCES projects(id) ON DELETE CASCADE,
|
|
102
|
+
user_id TEXT NOT NULL REFERENCES users(id) ON DELETE CASCADE,
|
|
103
|
+
identity_id TEXT NOT NULL REFERENCES git_identities(id) ON DELETE CASCADE,
|
|
104
|
+
created_at TEXT NOT NULL,
|
|
105
|
+
updated_at TEXT NOT NULL,
|
|
106
|
+
PRIMARY KEY (project_id, user_id)
|
|
107
|
+
)
|
|
108
|
+
`);
|
|
109
|
+
|
|
110
|
+
db.exec(`
|
|
111
|
+
CREATE INDEX idx_git_identity_bindings_identity ON git_identity_bindings (identity_id)
|
|
112
|
+
`);
|
|
113
|
+
|
|
114
|
+
debug.log('migration', '✅ git_identities and git_identity_bindings created');
|
|
115
|
+
};
|
|
116
|
+
|
|
117
|
+
export const down = (db: DatabaseConnection): void => {
|
|
118
|
+
debug.log('migration', '🗑️ Dropping git identity tables...');
|
|
119
|
+
db.exec('DROP TABLE IF EXISTS git_identity_bindings');
|
|
120
|
+
db.exec('DROP TABLE IF EXISTS git_identities');
|
|
121
|
+
debug.log('migration', '✅ git identity tables dropped');
|
|
122
|
+
};
|
|
@@ -80,6 +80,7 @@ import * as migration078 from './078_create_worktree_branch_tables';
|
|
|
80
80
|
import * as migration079 from './079_merge_commands_into_skills';
|
|
81
81
|
import * as migration080 from './080_create_file_shares';
|
|
82
82
|
import * as migration081 from './081_create_push_subscriptions';
|
|
83
|
+
import * as migration082 from './082_create_git_identities';
|
|
83
84
|
|
|
84
85
|
// Export all migrations in order
|
|
85
86
|
export const migrations = [
|
|
@@ -568,6 +569,12 @@ export const migrations = [
|
|
|
568
569
|
description: migration081.description,
|
|
569
570
|
up: migration081.up,
|
|
570
571
|
down: migration081.down
|
|
572
|
+
},
|
|
573
|
+
{
|
|
574
|
+
id: '082',
|
|
575
|
+
description: migration082.description,
|
|
576
|
+
up: migration082.up,
|
|
577
|
+
down: migration082.down
|
|
571
578
|
}
|
|
572
579
|
];
|
|
573
580
|
|
|
@@ -0,0 +1,290 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Git identity storage — rows in, rows out, secrets sealed at this boundary.
|
|
3
|
+
*
|
|
4
|
+
* Every write that touches `ssh_private_key`, `ssh_passphrase` or `https_token`
|
|
5
|
+
* goes through `sealFor`, and every read goes through `openRow`, which is the
|
|
6
|
+
* contract `backend/database/crypto/secret-columns.ts` audits at startup.
|
|
7
|
+
* Nothing above this module handles a sealed value.
|
|
8
|
+
*
|
|
9
|
+
* Secrets are UPDATED ONLY WHEN SUPPLIED. `undefined` means "leave what is
|
|
10
|
+
* stored", `null` means "clear it". Without that distinction every edit of a
|
|
11
|
+
* label would have to round-trip the private key through the browser to avoid
|
|
12
|
+
* wiping it — which is exactly what the DTO refuses to do.
|
|
13
|
+
*/
|
|
14
|
+
|
|
15
|
+
import { getDatabase } from '../index';
|
|
16
|
+
import { openRow, openRows, sealFor } from '../crypto/secret-columns';
|
|
17
|
+
import type { GitIdentityAuthMethod, GitIdentitySource } from '$shared/types/git-identity';
|
|
18
|
+
|
|
19
|
+
const TABLE = 'git_identities';
|
|
20
|
+
|
|
21
|
+
export interface GitIdentityRow {
|
|
22
|
+
id: string;
|
|
23
|
+
owner_user_id: string;
|
|
24
|
+
label: string;
|
|
25
|
+
name: string;
|
|
26
|
+
email: string;
|
|
27
|
+
auth_method: GitIdentityAuthMethod;
|
|
28
|
+
ssh_private_key: string | null;
|
|
29
|
+
ssh_public_key: string | null;
|
|
30
|
+
ssh_passphrase: string | null;
|
|
31
|
+
https_username: string | null;
|
|
32
|
+
https_token: string | null;
|
|
33
|
+
integration_account_id: string | null;
|
|
34
|
+
hosts: string;
|
|
35
|
+
is_default: number;
|
|
36
|
+
source: GitIdentitySource;
|
|
37
|
+
created_at: string;
|
|
38
|
+
updated_at: string;
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
export interface GitIdentityBindingRow {
|
|
42
|
+
project_id: string;
|
|
43
|
+
user_id: string;
|
|
44
|
+
identity_id: string;
|
|
45
|
+
created_at: string;
|
|
46
|
+
updated_at: string;
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
/** Create/update payload. Omitted secret fields keep their stored value. */
|
|
50
|
+
export interface GitIdentityWrite {
|
|
51
|
+
label: string;
|
|
52
|
+
name: string;
|
|
53
|
+
email: string;
|
|
54
|
+
authMethod: GitIdentityAuthMethod;
|
|
55
|
+
hosts: string[];
|
|
56
|
+
sshPrivateKey?: string | null;
|
|
57
|
+
sshPublicKey?: string | null;
|
|
58
|
+
sshPassphrase?: string | null;
|
|
59
|
+
httpsUsername?: string | null;
|
|
60
|
+
httpsToken?: string | null;
|
|
61
|
+
integrationAccountId?: string | null;
|
|
62
|
+
/** Only honoured on create — `update` never moves a row between sources. */
|
|
63
|
+
source?: GitIdentitySource;
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
/**
|
|
67
|
+
* Parse the stored `hosts` JSON into a lowercased list.
|
|
68
|
+
*
|
|
69
|
+
* Tolerant on read: a malformed blob yields no host claim rather than throwing,
|
|
70
|
+
* because the identity is still perfectly usable for attribution and failing
|
|
71
|
+
* the whole read would take the user's commit identity with it.
|
|
72
|
+
*/
|
|
73
|
+
export function parseHosts(raw: string): string[] {
|
|
74
|
+
try {
|
|
75
|
+
const parsed = JSON.parse(raw);
|
|
76
|
+
if (!Array.isArray(parsed)) return [];
|
|
77
|
+
return parsed
|
|
78
|
+
.filter((h): h is string => typeof h === 'string')
|
|
79
|
+
.map((h) => h.trim().toLowerCase())
|
|
80
|
+
.filter(Boolean);
|
|
81
|
+
} catch {
|
|
82
|
+
return [];
|
|
83
|
+
}
|
|
84
|
+
}
|
|
85
|
+
|
|
86
|
+
function serializeHosts(hosts: string[]): string {
|
|
87
|
+
const normalized = hosts.map((h) => h.trim().toLowerCase()).filter(Boolean);
|
|
88
|
+
return JSON.stringify([...new Set(normalized)]);
|
|
89
|
+
}
|
|
90
|
+
|
|
91
|
+
export const gitIdentityQueries = {
|
|
92
|
+
getById(id: string): GitIdentityRow | null {
|
|
93
|
+
const db = getDatabase();
|
|
94
|
+
const row = db.prepare(`SELECT * FROM ${TABLE} WHERE id = ?`).get(id) as
|
|
95
|
+
| GitIdentityRow
|
|
96
|
+
| undefined;
|
|
97
|
+
return row ? openRow(TABLE, row) : null;
|
|
98
|
+
},
|
|
99
|
+
|
|
100
|
+
listByUser(userId: string): GitIdentityRow[] {
|
|
101
|
+
const db = getDatabase();
|
|
102
|
+
const rows = db
|
|
103
|
+
.prepare(`SELECT * FROM ${TABLE} WHERE owner_user_id = ? ORDER BY is_default DESC, label`)
|
|
104
|
+
.all(userId) as GitIdentityRow[];
|
|
105
|
+
return openRows(TABLE, rows);
|
|
106
|
+
},
|
|
107
|
+
|
|
108
|
+
/** The row mirrored from the machine's global git config, if there is one. */
|
|
109
|
+
getLocalMachine(userId: string): GitIdentityRow | null {
|
|
110
|
+
const db = getDatabase();
|
|
111
|
+
const row = db
|
|
112
|
+
.prepare(`SELECT * FROM ${TABLE} WHERE owner_user_id = ? AND source = 'local-machine'`)
|
|
113
|
+
.get(userId) as GitIdentityRow | undefined;
|
|
114
|
+
return row ? openRow(TABLE, row) : null;
|
|
115
|
+
},
|
|
116
|
+
|
|
117
|
+
getDefaultForUser(userId: string): GitIdentityRow | null {
|
|
118
|
+
const db = getDatabase();
|
|
119
|
+
const row = db
|
|
120
|
+
.prepare(`SELECT * FROM ${TABLE} WHERE owner_user_id = ? AND is_default = 1`)
|
|
121
|
+
.get(userId) as GitIdentityRow | undefined;
|
|
122
|
+
return row ? openRow(TABLE, row) : null;
|
|
123
|
+
},
|
|
124
|
+
|
|
125
|
+
create(id: string, userId: string, input: GitIdentityWrite): GitIdentityRow {
|
|
126
|
+
const db = getDatabase();
|
|
127
|
+
const now = new Date().toISOString();
|
|
128
|
+
db.prepare(
|
|
129
|
+
`
|
|
130
|
+
INSERT INTO ${TABLE} (
|
|
131
|
+
id, owner_user_id, label, name, email, auth_method,
|
|
132
|
+
ssh_private_key, ssh_public_key, ssh_passphrase,
|
|
133
|
+
https_username, https_token, integration_account_id,
|
|
134
|
+
hosts, is_default, source, created_at, updated_at
|
|
135
|
+
) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, 0, ?, ?, ?)
|
|
136
|
+
`
|
|
137
|
+
).run(
|
|
138
|
+
id,
|
|
139
|
+
userId,
|
|
140
|
+
input.label,
|
|
141
|
+
input.name,
|
|
142
|
+
input.email,
|
|
143
|
+
input.authMethod,
|
|
144
|
+
sealFor(TABLE, 'ssh_private_key', input.sshPrivateKey ?? null),
|
|
145
|
+
input.sshPublicKey ?? null,
|
|
146
|
+
sealFor(TABLE, 'ssh_passphrase', input.sshPassphrase ?? null),
|
|
147
|
+
input.httpsUsername ?? null,
|
|
148
|
+
sealFor(TABLE, 'https_token', input.httpsToken ?? null),
|
|
149
|
+
input.integrationAccountId ?? null,
|
|
150
|
+
serializeHosts(input.hosts),
|
|
151
|
+
input.source ?? 'manual',
|
|
152
|
+
now,
|
|
153
|
+
now
|
|
154
|
+
);
|
|
155
|
+
return this.getById(id)!;
|
|
156
|
+
},
|
|
157
|
+
|
|
158
|
+
update(id: string, input: GitIdentityWrite): GitIdentityRow | null {
|
|
159
|
+
const db = getDatabase();
|
|
160
|
+
const existing = db.prepare(`SELECT id FROM ${TABLE} WHERE id = ?`).get(id);
|
|
161
|
+
if (!existing) return null;
|
|
162
|
+
|
|
163
|
+
// Secrets are appended conditionally so an omitted field never becomes a
|
|
164
|
+
// stored NULL. Column and parameter lists are built together to keep them
|
|
165
|
+
// impossible to misalign.
|
|
166
|
+
const sets: string[] = [
|
|
167
|
+
'label = ?',
|
|
168
|
+
'name = ?',
|
|
169
|
+
'email = ?',
|
|
170
|
+
'auth_method = ?',
|
|
171
|
+
'hosts = ?',
|
|
172
|
+
'ssh_public_key = ?',
|
|
173
|
+
'https_username = ?',
|
|
174
|
+
'integration_account_id = ?',
|
|
175
|
+
'updated_at = ?'
|
|
176
|
+
];
|
|
177
|
+
const params: unknown[] = [
|
|
178
|
+
input.label,
|
|
179
|
+
input.name,
|
|
180
|
+
input.email,
|
|
181
|
+
input.authMethod,
|
|
182
|
+
serializeHosts(input.hosts),
|
|
183
|
+
input.sshPublicKey ?? null,
|
|
184
|
+
input.httpsUsername ?? null,
|
|
185
|
+
input.integrationAccountId ?? null,
|
|
186
|
+
new Date().toISOString()
|
|
187
|
+
];
|
|
188
|
+
|
|
189
|
+
if (input.sshPrivateKey !== undefined) {
|
|
190
|
+
sets.push('ssh_private_key = ?');
|
|
191
|
+
params.push(sealFor(TABLE, 'ssh_private_key', input.sshPrivateKey));
|
|
192
|
+
}
|
|
193
|
+
if (input.sshPassphrase !== undefined) {
|
|
194
|
+
sets.push('ssh_passphrase = ?');
|
|
195
|
+
params.push(sealFor(TABLE, 'ssh_passphrase', input.sshPassphrase));
|
|
196
|
+
}
|
|
197
|
+
if (input.httpsToken !== undefined) {
|
|
198
|
+
sets.push('https_token = ?');
|
|
199
|
+
params.push(sealFor(TABLE, 'https_token', input.httpsToken));
|
|
200
|
+
}
|
|
201
|
+
|
|
202
|
+
params.push(id);
|
|
203
|
+
db.prepare(`UPDATE ${TABLE} SET ${sets.join(', ')} WHERE id = ?`).run(...(params as never[]));
|
|
204
|
+
return this.getById(id);
|
|
205
|
+
},
|
|
206
|
+
|
|
207
|
+
/**
|
|
208
|
+
* Make one identity the user's default, clearing any previous one.
|
|
209
|
+
*
|
|
210
|
+
* Both statements run in a transaction because the partial unique index
|
|
211
|
+
* rejects a moment where two rows claim the default — clearing first and
|
|
212
|
+
* setting second is not optional, it is what makes the write legal.
|
|
213
|
+
*/
|
|
214
|
+
setDefault(userId: string, id: string): void {
|
|
215
|
+
const db = getDatabase();
|
|
216
|
+
const swap = () => {
|
|
217
|
+
db.prepare(`UPDATE ${TABLE} SET is_default = 0 WHERE owner_user_id = ? AND is_default = 1`).run(
|
|
218
|
+
userId
|
|
219
|
+
);
|
|
220
|
+
db.prepare(`UPDATE ${TABLE} SET is_default = 1, updated_at = ? WHERE id = ? AND owner_user_id = ?`).run(
|
|
221
|
+
new Date().toISOString(),
|
|
222
|
+
id,
|
|
223
|
+
userId
|
|
224
|
+
);
|
|
225
|
+
};
|
|
226
|
+
// Not every driver exposes transactions (see `DatabaseConnection`). Running
|
|
227
|
+
// it unbatched is a worse guarantee, not a wrong result: the clear still
|
|
228
|
+
// precedes the set, which is what the partial unique index requires.
|
|
229
|
+
if (db.transaction) db.transaction(swap)();
|
|
230
|
+
else swap();
|
|
231
|
+
},
|
|
232
|
+
|
|
233
|
+
remove(id: string): boolean {
|
|
234
|
+
const db = getDatabase();
|
|
235
|
+
const result = db.prepare(`DELETE FROM ${TABLE} WHERE id = ?`).run(id) as { changes: number };
|
|
236
|
+
return result.changes > 0;
|
|
237
|
+
},
|
|
238
|
+
|
|
239
|
+
/** True when the label is already taken by another of this user's identities. */
|
|
240
|
+
labelTaken(userId: string, label: string, exceptId?: string): boolean {
|
|
241
|
+
const db = getDatabase();
|
|
242
|
+
const row = db
|
|
243
|
+
.prepare(
|
|
244
|
+
`SELECT id FROM ${TABLE} WHERE owner_user_id = ? AND label = ? AND id IS NOT ?`
|
|
245
|
+
)
|
|
246
|
+
.get(userId, label, exceptId ?? null);
|
|
247
|
+
return !!row;
|
|
248
|
+
}
|
|
249
|
+
};
|
|
250
|
+
|
|
251
|
+
export const gitIdentityBindingQueries = {
|
|
252
|
+
get(projectId: string, userId: string): GitIdentityBindingRow | null {
|
|
253
|
+
const db = getDatabase();
|
|
254
|
+
const row = db
|
|
255
|
+
.prepare('SELECT * FROM git_identity_bindings WHERE project_id = ? AND user_id = ?')
|
|
256
|
+
.get(projectId, userId) as GitIdentityBindingRow | undefined;
|
|
257
|
+
return row ?? null;
|
|
258
|
+
},
|
|
259
|
+
|
|
260
|
+
set(projectId: string, userId: string, identityId: string): void {
|
|
261
|
+
const db = getDatabase();
|
|
262
|
+
const now = new Date().toISOString();
|
|
263
|
+
db.prepare(
|
|
264
|
+
`
|
|
265
|
+
INSERT INTO git_identity_bindings (project_id, user_id, identity_id, created_at, updated_at)
|
|
266
|
+
VALUES (?, ?, ?, ?, ?)
|
|
267
|
+
ON CONFLICT(project_id, user_id) DO UPDATE SET
|
|
268
|
+
identity_id = excluded.identity_id,
|
|
269
|
+
updated_at = excluded.updated_at
|
|
270
|
+
`
|
|
271
|
+
).run(projectId, userId, identityId, now, now);
|
|
272
|
+
},
|
|
273
|
+
|
|
274
|
+
clear(projectId: string, userId: string): void {
|
|
275
|
+
const db = getDatabase();
|
|
276
|
+
db.prepare('DELETE FROM git_identity_bindings WHERE project_id = ? AND user_id = ?').run(
|
|
277
|
+
projectId,
|
|
278
|
+
userId
|
|
279
|
+
);
|
|
280
|
+
},
|
|
281
|
+
|
|
282
|
+
/** Projects this identity is bound to — what a delete confirmation shows. */
|
|
283
|
+
countForIdentity(identityId: string): number {
|
|
284
|
+
const db = getDatabase();
|
|
285
|
+
const row = db
|
|
286
|
+
.prepare('SELECT COUNT(*) as count FROM git_identity_bindings WHERE identity_id = ?')
|
|
287
|
+
.get(identityId) as { count: number };
|
|
288
|
+
return row.count;
|
|
289
|
+
}
|
|
290
|
+
};
|
|
@@ -46,6 +46,8 @@ export {
|
|
|
46
46
|
worktreeBranchQueries,
|
|
47
47
|
parseWorktreeBranchConfig
|
|
48
48
|
} from './worktree-branch-queries';
|
|
49
|
+
export { gitIdentityQueries, gitIdentityBindingQueries, parseHosts } from './git-identity-queries';
|
|
50
|
+
export type { GitIdentityRow, GitIdentityBindingRow, GitIdentityWrite } from './git-identity-queries';
|
|
49
51
|
export { pushSubscriptionQueries } from './push-subscription-queries';
|
|
50
52
|
export type { PushSubscription, PushSubscriptionInput } from './push-subscription-queries';
|
|
51
53
|
export type { WorktreeBranchBindingRow, WorktreeBranchRow } from './worktree-branch-queries';
|