@myrialabs/clopen 0.4.39 → 0.4.41
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/.env.example +1 -1
- package/backend/artifacts/detect.ts +15 -3
- package/backend/artifacts/generate.ts +2 -2
- package/backend/artifacts/index.ts +12 -4
- package/backend/artifacts/markers.ts +24 -1
- package/backend/artifacts/matrix.ts +47 -3
- package/backend/artifacts/sync.test.ts +3 -3
- package/backend/artifacts/sync.ts +8 -20
- package/backend/artifacts/types.ts +9 -3
- package/backend/auth/permissions.ts +29 -11
- package/backend/auth/session-cleanup.ts +10 -1
- package/backend/auth/tokens.ts +9 -1
- package/backend/chat/stream-manager.ts +42 -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/079_merge_commands_into_skills.ts +222 -0
- package/backend/database/migrations/080_create_file_shares.ts +111 -0
- package/backend/database/migrations/081_create_push_subscriptions.ts +32 -0
- package/backend/database/migrations/index.ts +28 -0
- package/backend/database/queries/file-share-queries.ts +163 -0
- package/backend/database/queries/index.ts +12 -4
- package/backend/database/queries/profile-queries.ts +5 -3
- package/backend/database/queries/push-subscription-queries.test.ts +90 -0
- package/backend/database/queries/push-subscription-queries.ts +99 -0
- package/backend/database/queries/skill-queries.ts +96 -18
- 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/engine/adapters/claude/stream.ts +2 -1
- package/backend/engine/adapters/cline/stream.ts +3 -3
- package/backend/engine/adapters/codex/stream.ts +3 -3
- package/backend/engine/adapters/copilot/stream.ts +3 -3
- package/backend/engine/adapters/cursor/stream.ts +25 -3
- package/backend/engine/adapters/opencode/stream.ts +4 -6
- package/backend/engine/adapters/pi/stream.ts +2 -2
- package/backend/engine/adapters/qwen/stream.ts +2 -2
- package/backend/engine/artifact-sync.ts +26 -20
- package/backend/engine/docs/artifacts.md +81 -22
- package/backend/engine/docs/quick-reference.md +2 -1
- package/backend/engine/resolve-model.test.ts +108 -0
- package/backend/engine/resolve-model.ts +53 -10
- package/backend/engine/structured-helpers.ts +22 -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/files/file-shares.ts +369 -0
- package/backend/files/path-browsing.ts +27 -21
- package/backend/http/files-shared.test.ts +505 -0
- package/backend/http/files-shared.ts +351 -0
- package/backend/http/push.ts +150 -0
- package/backend/index.ts +8 -0
- package/backend/instructions/index.ts +1 -1
- package/backend/instructions/sync.ts +30 -6
- 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/memory/compose.ts +2 -2
- package/backend/memory/consolidate.ts +2 -2
- package/backend/memory/extract/episodic.ts +2 -2
- package/backend/memory/judge.ts +2 -2
- package/backend/profiles/service.ts +7 -6
- package/backend/push/sender.ts +172 -0
- package/backend/push/tags.ts +25 -0
- package/backend/push/vapid-keys.ts +77 -0
- package/backend/push/webpush-crypto.test.ts +200 -0
- package/backend/push/webpush-crypto.ts +231 -0
- package/backend/skills/detect.ts +28 -0
- package/backend/skills/index.ts +22 -4
- package/backend/skills/invoke.test.ts +114 -0
- package/backend/skills/invoke.ts +222 -0
- package/backend/skills/service.ts +194 -44
- package/backend/skills/spec.ts +35 -0
- package/backend/skills/sync.ts +85 -55
- package/backend/subagents/detect.ts +2 -3
- package/backend/subagents/sync.ts +15 -8
- package/backend/work/pull-requests.ts +2 -2
- 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/chat/stream.ts +100 -8
- package/backend/ws/db-client/env.ts +142 -0
- package/backend/ws/db-client/index.ts +2 -0
- package/backend/ws/files/index.ts +4 -0
- package/backend/ws/files/share.ts +95 -0
- package/backend/ws/git/branch-name.ts +2 -2
- package/backend/ws/git/commit-message.ts +2 -2
- package/backend/ws/index.ts +3 -4
- package/backend/ws/profiles/crud.ts +2 -3
- 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/skills/catalog.ts +5 -1
- package/backend/ws/skills/crud.ts +128 -22
- package/backend/ws/skills/index.ts +4 -1
- package/backend/ws/system/device-info.ts +307 -76
- package/backend/ws/user/crud.ts +12 -3
- 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-CeNKKSDR.js} +1 -1
- package/dist/assets/{index-BrF6nqld.js → index-DW89566K.js} +1 -1
- package/dist/assets/main-Cy5DXWPT.js +1207 -0
- package/dist/assets/main-DJKj_4By.css +1 -0
- package/dist/index.html +7 -2
- package/dist/sw.js +112 -0
- package/package.json +1 -1
- package/shared/constants/engine-tools.ts +31 -6
- package/shared/constants/notification-messages.ts +142 -0
- 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/unified/stream.ts +7 -0
- package/shared/types/worktree-branching.ts +315 -0
- package/shared/utils/logger.ts +1 -0
- package/backend/commands/detect.ts +0 -25
- package/backend/commands/index.ts +0 -9
- package/backend/commands/service.ts +0 -178
- package/backend/commands/store.ts +0 -45
- package/backend/commands/sync.ts +0 -94
- package/backend/database/queries/command-queries.ts +0 -82
- package/backend/ws/commands/crud.ts +0 -157
- package/backend/ws/commands/index.ts +0 -8
- package/dist/assets/main-Bd3gb79F.css +0 -1
- package/dist/assets/main-Bo1hD7mt.js +0 -1191
|
@@ -0,0 +1,118 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Tests for the branch name.
|
|
3
|
+
*
|
|
4
|
+
* The name is load-bearing rather than cosmetic: no provider here lets a client
|
|
5
|
+
* set metadata on a branch, so when a crash lands between "the provider created
|
|
6
|
+
* it" and "the row was written", the name is the ENTIRE evidence tying the leak
|
|
7
|
+
* back to Clopen. These cases are the ones that break that tie.
|
|
8
|
+
*/
|
|
9
|
+
|
|
10
|
+
import { describe, it, expect } from 'bun:test';
|
|
11
|
+
import {
|
|
12
|
+
branchDatabaseNameFor,
|
|
13
|
+
branchNameFor,
|
|
14
|
+
isClopenBranchName,
|
|
15
|
+
isClopenDatabaseName,
|
|
16
|
+
slugifyForBranch
|
|
17
|
+
} from './naming';
|
|
18
|
+
|
|
19
|
+
describe('slugifyForBranch', () => {
|
|
20
|
+
it('reduces to a character class every provider accepts', () => {
|
|
21
|
+
expect(slugifyForBranch('Fix Login!! (urgent)')).toBe('fix-login-urgent');
|
|
22
|
+
expect(slugifyForBranch(' spaced out ')).toBe('spaced-out');
|
|
23
|
+
expect(slugifyForBranch('under_score-and-dash')).toBe('under-score-and-dash');
|
|
24
|
+
});
|
|
25
|
+
|
|
26
|
+
it('strips accents rather than dropping the words carrying them', () => {
|
|
27
|
+
expect(slugifyForBranch('café münchen')).toBe('cafe-munchen');
|
|
28
|
+
});
|
|
29
|
+
|
|
30
|
+
it('never leaves a leading or trailing dash', () => {
|
|
31
|
+
// A trailing dash survives the length cap unless it is trimmed AFTER the
|
|
32
|
+
// slice, which is the ordering this asserts.
|
|
33
|
+
expect(slugifyForBranch('a'.repeat(39) + ' tail', 40)).not.toMatch(/-$/);
|
|
34
|
+
expect(slugifyForBranch('---edge---')).toBe('edge');
|
|
35
|
+
});
|
|
36
|
+
|
|
37
|
+
it('answers empty for input with nothing sluggable in it', () => {
|
|
38
|
+
expect(slugifyForBranch('!!!')).toBe('');
|
|
39
|
+
expect(slugifyForBranch('')).toBe('');
|
|
40
|
+
});
|
|
41
|
+
});
|
|
42
|
+
|
|
43
|
+
describe('branchNameFor', () => {
|
|
44
|
+
it('is deterministic for the same project and worktree', () => {
|
|
45
|
+
const input = { projectName: 'Acme Web', worktreeSlug: 'fix-login' };
|
|
46
|
+
expect(branchNameFor(input)).toBe(branchNameFor(input));
|
|
47
|
+
expect(branchNameFor(input)).toBe('clopen/acme-web/fix-login');
|
|
48
|
+
});
|
|
49
|
+
|
|
50
|
+
it('keeps the prefix even when both parts slug away to nothing', () => {
|
|
51
|
+
// Without the fallbacks this would be `clopen//`, which matches the prefix
|
|
52
|
+
// test but tells the orphan sweep nothing and collides with every other
|
|
53
|
+
// unsluggable project.
|
|
54
|
+
expect(branchNameFor({ projectName: '!!!', worktreeSlug: '???' })).toBe(
|
|
55
|
+
'clopen/project/worktree'
|
|
56
|
+
);
|
|
57
|
+
});
|
|
58
|
+
|
|
59
|
+
it('separates two worktrees that share a display name', () => {
|
|
60
|
+
// The SLUG is used rather than the name for exactly this reason:
|
|
61
|
+
// `uniqueWorktreeSlug` already guarantees uniqueness within a project, and
|
|
62
|
+
// using the display name would reintroduce the collision it solves.
|
|
63
|
+
const first = branchNameFor({ projectName: 'Acme', worktreeSlug: 'fix-login' });
|
|
64
|
+
const second = branchNameFor({ projectName: 'Acme', worktreeSlug: 'fix-login-2' });
|
|
65
|
+
expect(first).not.toBe(second);
|
|
66
|
+
});
|
|
67
|
+
});
|
|
68
|
+
|
|
69
|
+
describe('isClopenBranchName', () => {
|
|
70
|
+
it('matches what we create', () => {
|
|
71
|
+
expect(isClopenBranchName(branchNameFor({ projectName: 'Acme', worktreeSlug: 'x' }))).toBe(true);
|
|
72
|
+
});
|
|
73
|
+
|
|
74
|
+
it('does not match a branch that merely mentions the word', () => {
|
|
75
|
+
// The sweep OFFERS these for deletion, so a false positive puts someone
|
|
76
|
+
// else's branch in front of a Delete button.
|
|
77
|
+
expect(isClopenBranchName('main')).toBe(false);
|
|
78
|
+
expect(isClopenBranchName('clopen')).toBe(false);
|
|
79
|
+
expect(isClopenBranchName('my-clopen/thing')).toBe(false);
|
|
80
|
+
expect(isClopenBranchName('preview/pr-12')).toBe(false);
|
|
81
|
+
});
|
|
82
|
+
});
|
|
83
|
+
|
|
84
|
+
describe('branchDatabaseNameFor', () => {
|
|
85
|
+
it('produces a plain SQL identifier, not a branch name', () => {
|
|
86
|
+
// This one is interpolated into DDL and capped at 63 bytes by Postgres,
|
|
87
|
+
// so the slashes a Neon branch name carries would be a syntax error.
|
|
88
|
+
const name = branchDatabaseNameFor({ projectName: 'Acme Shop', worktreeSlug: 'fix-login' });
|
|
89
|
+
expect(name).toBe('clopen_acme_shop_fix_login');
|
|
90
|
+
expect(name).toMatch(/^[a-z0-9_]+$/);
|
|
91
|
+
});
|
|
92
|
+
|
|
93
|
+
it('stays inside the identifier limit, prefix intact', () => {
|
|
94
|
+
// The prefix surviving the cap is what makes a leaked database findable:
|
|
95
|
+
// `isClopenDatabaseName` is the whole of the evidence.
|
|
96
|
+
const name = branchDatabaseNameFor({
|
|
97
|
+
projectName: 'A'.repeat(80),
|
|
98
|
+
worktreeSlug: 'B'.repeat(80)
|
|
99
|
+
});
|
|
100
|
+
expect(name.length).toBeLessThanOrEqual(63);
|
|
101
|
+
expect(isClopenDatabaseName(name)).toBe(true);
|
|
102
|
+
});
|
|
103
|
+
|
|
104
|
+
it('keeps two worktrees of one project apart', () => {
|
|
105
|
+
const first = branchDatabaseNameFor({ projectName: 'Acme', worktreeSlug: 'a' });
|
|
106
|
+
const second = branchDatabaseNameFor({ projectName: 'Acme', worktreeSlug: 'b' });
|
|
107
|
+
expect(first).not.toBe(second);
|
|
108
|
+
});
|
|
109
|
+
});
|
|
110
|
+
|
|
111
|
+
describe('isClopenDatabaseName', () => {
|
|
112
|
+
it('matches what we create and nothing that merely resembles it', () => {
|
|
113
|
+
expect(isClopenDatabaseName('clopen_acme_fix_login')).toBe(true);
|
|
114
|
+
expect(isClopenDatabaseName('clopen')).toBe(false);
|
|
115
|
+
expect(isClopenDatabaseName('myclopen_thing')).toBe(false);
|
|
116
|
+
expect(isClopenDatabaseName('postgres')).toBe(false);
|
|
117
|
+
});
|
|
118
|
+
});
|
|
@@ -0,0 +1,97 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* What a worktree's branch is called at the provider.
|
|
3
|
+
*
|
|
4
|
+
* This is not cosmetic. The name is the ONLY thing that can tie a leaked branch
|
|
5
|
+
* back to Clopen, because no provider here lets a client set arbitrary metadata
|
|
6
|
+
* on a branch — Neon's `creation_source` is read-only, and there is no tag or
|
|
7
|
+
* label field. So when a crash lands between "the provider created it" and "the
|
|
8
|
+
* row was written", the name is the whole of the evidence.
|
|
9
|
+
*
|
|
10
|
+
* Hence: deterministic, prefixed, and slugged down to a character class every
|
|
11
|
+
* provider accepts. Slashes are kept because Neon uses them itself (its GitHub
|
|
12
|
+
* integration creates `preview/pr-123-…`), and they make the prefix read as a
|
|
13
|
+
* namespace rather than as a word someone might have chosen.
|
|
14
|
+
*/
|
|
15
|
+
|
|
16
|
+
import { BRANCH_NAME_PREFIX } from '$shared/types/worktree-branching';
|
|
17
|
+
|
|
18
|
+
/**
|
|
19
|
+
* A provider-safe slug.
|
|
20
|
+
*
|
|
21
|
+
* Reduced to `[a-z0-9-]`, which is a strict subset of what Neon and Turso
|
|
22
|
+
* accept, rather than encoding either one's rules. The same trade `start-work`
|
|
23
|
+
* makes for git ref names, for the same reason: a subset that always works
|
|
24
|
+
* beats a rule set that has to be kept in step with two vendors.
|
|
25
|
+
*/
|
|
26
|
+
export function slugifyForBranch(text: string, maxLength = 40): string {
|
|
27
|
+
return text
|
|
28
|
+
.toLowerCase()
|
|
29
|
+
.normalize('NFKD')
|
|
30
|
+
// Drop the combining marks NFKD just separated out, BEFORE the character
|
|
31
|
+
// class runs. Without this `ü` decomposes to `u` + a diaeresis, the class
|
|
32
|
+
// turns that mark into a space, and "münchen" slugs to `mu-nchen` — a name
|
|
33
|
+
// that splits a word in half and, worse, is not the name anyone would
|
|
34
|
+
// search the provider's console for.
|
|
35
|
+
.replace(/[\u0300-\u036f]/g, '')
|
|
36
|
+
.replace(/[^\w\s-]/g, ' ')
|
|
37
|
+
.trim()
|
|
38
|
+
.split(/[\s_-]+/)
|
|
39
|
+
.filter(Boolean)
|
|
40
|
+
.join('-')
|
|
41
|
+
.slice(0, maxLength)
|
|
42
|
+
.replace(/^-+|-+$/g, '');
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
/**
|
|
46
|
+
* The branch name for one worktree.
|
|
47
|
+
*
|
|
48
|
+
* The worktree SLUG is used rather than its name, and that is deliberate: a
|
|
49
|
+
* slug is already unique within its project (`uniqueWorktreeSlug` guarantees
|
|
50
|
+
* it), so two worktrees called "fix login" cannot collide here either. Using
|
|
51
|
+
* the display name would reintroduce the collision the slug exists to solve.
|
|
52
|
+
*/
|
|
53
|
+
export function branchNameFor(input: { projectName: string; worktreeSlug: string }): string {
|
|
54
|
+
const project = slugifyForBranch(input.projectName, 30) || 'project';
|
|
55
|
+
const worktree = slugifyForBranch(input.worktreeSlug, 40) || 'worktree';
|
|
56
|
+
return `${BRANCH_NAME_PREFIX}/${project}/${worktree}`;
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
/**
|
|
60
|
+
* The DATABASE name for a worktree's copy.
|
|
61
|
+
*
|
|
62
|
+
* A different function from `branchNameFor` because it answers to a different
|
|
63
|
+
* rule set. A Neon branch name may contain slashes and is only ever read by
|
|
64
|
+
* humans; a database name is an SQL identifier, capped at 63 bytes by Postgres,
|
|
65
|
+
* and is about to be interpolated into DDL. So: underscores rather than
|
|
66
|
+
* slashes, `[a-z0-9_]` only, and length enforced here rather than discovered as
|
|
67
|
+
* a truncation by the server.
|
|
68
|
+
*
|
|
69
|
+
* The prefix survives the cap. It is what `isClopenDatabaseName` matches, and
|
|
70
|
+
* therefore the only thing that lets the orphan sweep recognise a database a
|
|
71
|
+
* crash left behind.
|
|
72
|
+
*/
|
|
73
|
+
export function branchDatabaseNameFor(input: {
|
|
74
|
+
projectName: string;
|
|
75
|
+
worktreeSlug: string;
|
|
76
|
+
}): string {
|
|
77
|
+
const project = slugifyForBranch(input.projectName, 20).replace(/-/g, '_') || 'project';
|
|
78
|
+
const worktree = slugifyForBranch(input.worktreeSlug, 24).replace(/-/g, '_') || 'worktree';
|
|
79
|
+
return `${BRANCH_NAME_PREFIX}_${project}_${worktree}`.slice(0, 63).replace(/_+$/, '');
|
|
80
|
+
}
|
|
81
|
+
|
|
82
|
+
/** Whether a database on the server looks like one Clopen made. */
|
|
83
|
+
export function isClopenDatabaseName(name: string): boolean {
|
|
84
|
+
return name.startsWith(`${BRANCH_NAME_PREFIX}_`);
|
|
85
|
+
}
|
|
86
|
+
|
|
87
|
+
/**
|
|
88
|
+
* Whether a branch at the provider looks like one of ours.
|
|
89
|
+
*
|
|
90
|
+
* Used only to decide what the orphan sweep is allowed to OFFER for deletion —
|
|
91
|
+
* never to delete anything automatically. A user who happens to name a branch
|
|
92
|
+
* `clopen/...` by hand would otherwise see it proposed for deletion, which is
|
|
93
|
+
* why the sweep reports and the user decides.
|
|
94
|
+
*/
|
|
95
|
+
export function isClopenBranchName(name: string): boolean {
|
|
96
|
+
return name.startsWith(`${BRANCH_NAME_PREFIX}/`);
|
|
97
|
+
}
|
|
@@ -0,0 +1,229 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Tests for the worktree-branch projection.
|
|
3
|
+
*
|
|
4
|
+
* The cases here are the ones that destroy something when they are wrong:
|
|
5
|
+
*
|
|
6
|
+
* - One account must own a connection per LIVE worktree branch, and an orphan
|
|
7
|
+
* must own none. An orphan's remote database may well still exist — that is
|
|
8
|
+
* what makes it an orphan — but its worktree is gone, and leaving a
|
|
9
|
+
* connection behind clutters every admin's list with rows nobody is using.
|
|
10
|
+
* - Neon accounts carry BOTH `database` and `worktree-branching`, so the two
|
|
11
|
+
* projectors write to the same table for the same account. Migration 077
|
|
12
|
+
* widened the projection key with `target_id` precisely so they can; if
|
|
13
|
+
* either released by capability alone it would tear down the other's rows.
|
|
14
|
+
* - Releasing must DELETE. Nothing here is ever adopted — a host the provider
|
|
15
|
+
* minted seconds ago cannot have been typed in by anyone — so there is no
|
|
16
|
+
* snapshot to restore and a "hand it back" path would leave a husk.
|
|
17
|
+
*/
|
|
18
|
+
|
|
19
|
+
import { describe, it, expect, beforeAll, beforeEach, afterAll } from 'bun:test';
|
|
20
|
+
|
|
21
|
+
import { initializeDatabase, closeDatabase } from '$backend/database';
|
|
22
|
+
import {
|
|
23
|
+
dbClientConnectionQueries,
|
|
24
|
+
integrationAccountQueries,
|
|
25
|
+
integrationProjectionQueries,
|
|
26
|
+
projectQueries,
|
|
27
|
+
worktreeBranchQueries,
|
|
28
|
+
worktreeQueries
|
|
29
|
+
} from '$backend/database/queries';
|
|
30
|
+
import { integrationAccounts } from '$backend/integrations/accounts';
|
|
31
|
+
import { reproject } from '$backend/integrations';
|
|
32
|
+
import type { BranchConnection } from '$shared/types/worktree-branching';
|
|
33
|
+
import './index';
|
|
34
|
+
|
|
35
|
+
const CONNECTION: BranchConnection = {
|
|
36
|
+
uri: 'postgresql://owner:pw@ep-one.eu-central-1.aws.neon.tech/neondb?sslmode=require',
|
|
37
|
+
host: 'ep-one.eu-central-1.aws.neon.tech',
|
|
38
|
+
port: 5432,
|
|
39
|
+
username: 'neondb_owner',
|
|
40
|
+
password: 'pw',
|
|
41
|
+
database: 'neondb',
|
|
42
|
+
sslMode: 'require'
|
|
43
|
+
};
|
|
44
|
+
|
|
45
|
+
let projectId: string;
|
|
46
|
+
|
|
47
|
+
function connect(label?: string) {
|
|
48
|
+
return integrationAccounts.connect({
|
|
49
|
+
provider: 'neon',
|
|
50
|
+
label,
|
|
51
|
+
credentials: { apiKey: 'napi_test' }
|
|
52
|
+
});
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
function makeWorktree(name: string) {
|
|
56
|
+
return worktreeQueries.create({
|
|
57
|
+
project_id: projectId,
|
|
58
|
+
name,
|
|
59
|
+
slug: name.toLowerCase().replace(/\s+/g, '-'),
|
|
60
|
+
path: `/tmp/clopen-test/${name}`,
|
|
61
|
+
clone_mode: 'copy',
|
|
62
|
+
base_tree: {}
|
|
63
|
+
});
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
function cutBranch(
|
|
67
|
+
accountId: string,
|
|
68
|
+
worktreeId: string,
|
|
69
|
+
branchRef: string,
|
|
70
|
+
connection: BranchConnection = CONNECTION
|
|
71
|
+
) {
|
|
72
|
+
return worktreeBranchQueries.create({
|
|
73
|
+
worktreeId,
|
|
74
|
+
projectId,
|
|
75
|
+
sourceKind: 'account',
|
|
76
|
+
sourceId: accountId,
|
|
77
|
+
parentRef: 'winter-frost-12345678',
|
|
78
|
+
branchRef,
|
|
79
|
+
branchName: `clopen/acme/${branchRef}`,
|
|
80
|
+
connection,
|
|
81
|
+
envVar: 'DATABASE_URL',
|
|
82
|
+
envFile: '.env',
|
|
83
|
+
strategy: 'native',
|
|
84
|
+
notice: null
|
|
85
|
+
});
|
|
86
|
+
}
|
|
87
|
+
|
|
88
|
+
function clearFixtures(): void {
|
|
89
|
+
for (const account of integrationAccountQueries.getAll()) {
|
|
90
|
+
integrationAccountQueries.remove(account.id);
|
|
91
|
+
}
|
|
92
|
+
for (const connection of dbClientConnectionQueries.list()) {
|
|
93
|
+
dbClientConnectionQueries.delete(connection.id);
|
|
94
|
+
}
|
|
95
|
+
for (const worktree of worktreeQueries.getByProjectId(projectId)) {
|
|
96
|
+
worktreeQueries.delete(worktree.id);
|
|
97
|
+
}
|
|
98
|
+
}
|
|
99
|
+
|
|
100
|
+
beforeAll(async () => {
|
|
101
|
+
await initializeDatabase();
|
|
102
|
+
const now = new Date().toISOString();
|
|
103
|
+
projectId = projectQueries.create({
|
|
104
|
+
name: 'Branch Fixture',
|
|
105
|
+
path: '/tmp/clopen-test/branch-fixture',
|
|
106
|
+
created_at: now,
|
|
107
|
+
last_opened_at: now
|
|
108
|
+
}).id;
|
|
109
|
+
});
|
|
110
|
+
|
|
111
|
+
beforeEach(() => {
|
|
112
|
+
clearFixtures();
|
|
113
|
+
});
|
|
114
|
+
|
|
115
|
+
afterAll(() => {
|
|
116
|
+
clearFixtures();
|
|
117
|
+
projectQueries.deleteProject(projectId);
|
|
118
|
+
closeDatabase();
|
|
119
|
+
});
|
|
120
|
+
|
|
121
|
+
describe('worktree branch projection', () => {
|
|
122
|
+
it('projects one connection per live branch', () => {
|
|
123
|
+
const account = connect();
|
|
124
|
+
cutBranch(account.id, makeWorktree('fix login').id, 'br-one');
|
|
125
|
+
cutBranch(account.id, makeWorktree('fix signup').id, 'br-two');
|
|
126
|
+
|
|
127
|
+
reproject(account.id);
|
|
128
|
+
|
|
129
|
+
expect(dbClientConnectionQueries.list()).toHaveLength(2);
|
|
130
|
+
expect(integrationProjectionQueries.getForAccount(account.id)).toHaveLength(2);
|
|
131
|
+
});
|
|
132
|
+
|
|
133
|
+
it('carries the endpoint and the password onto the row', () => {
|
|
134
|
+
const account = connect();
|
|
135
|
+
cutBranch(account.id, makeWorktree('fix login').id, 'br-one');
|
|
136
|
+
reproject(account.id);
|
|
137
|
+
|
|
138
|
+
const connection = dbClientConnectionQueries.list()[0];
|
|
139
|
+
expect(connection.host).toBe(CONNECTION.host);
|
|
140
|
+
expect(connection.port).toBe(5432);
|
|
141
|
+
expect(connection.username).toBe('neondb_owner');
|
|
142
|
+
expect(connection.database).toBe('neondb');
|
|
143
|
+
expect(connection.sslMode).toBe('require');
|
|
144
|
+
// The password comes from the sealed blob on the branch row, which is the
|
|
145
|
+
// only place it exists — the provider generated it and nobody typed it.
|
|
146
|
+
expect(connection.password).toBe('pw');
|
|
147
|
+
});
|
|
148
|
+
|
|
149
|
+
it('names the connection after the WORKTREE, not the branch', () => {
|
|
150
|
+
// The connection list is global, so "fix login (branch)" is the question
|
|
151
|
+
// someone actually has — `clopen/acme/br-one` is not.
|
|
152
|
+
const account = connect();
|
|
153
|
+
cutBranch(account.id, makeWorktree('fix login').id, 'br-one');
|
|
154
|
+
reproject(account.id);
|
|
155
|
+
|
|
156
|
+
expect(dbClientConnectionQueries.list()[0].name).toBe('fix login (branch)');
|
|
157
|
+
});
|
|
158
|
+
|
|
159
|
+
it('re-projecting an existing branch does not create a second row', () => {
|
|
160
|
+
const account = connect();
|
|
161
|
+
cutBranch(account.id, makeWorktree('fix login').id, 'br-one');
|
|
162
|
+
|
|
163
|
+
reproject(account.id);
|
|
164
|
+
const first = dbClientConnectionQueries.list()[0].id;
|
|
165
|
+
reproject(account.id);
|
|
166
|
+
|
|
167
|
+
const connections = dbClientConnectionQueries.list();
|
|
168
|
+
expect(connections).toHaveLength(1);
|
|
169
|
+
expect(connections[0].id).toBe(first);
|
|
170
|
+
});
|
|
171
|
+
|
|
172
|
+
it('releases exactly the branch that was orphaned, and keeps the other', () => {
|
|
173
|
+
const account = connect();
|
|
174
|
+
const kept = cutBranch(account.id, makeWorktree('fix login').id, 'br-one');
|
|
175
|
+
const lost = cutBranch(account.id, makeWorktree('fix signup').id, 'br-two');
|
|
176
|
+
reproject(account.id);
|
|
177
|
+
expect(dbClientConnectionQueries.list()).toHaveLength(2);
|
|
178
|
+
|
|
179
|
+
// What `detach` does when the remote delete fails: the row survives so the
|
|
180
|
+
// leak can be reported, and it stops being live.
|
|
181
|
+
worktreeBranchQueries.markOrphaned(lost.id, 'Neon is unreachable');
|
|
182
|
+
reproject(account.id);
|
|
183
|
+
|
|
184
|
+
const connections = dbClientConnectionQueries.list();
|
|
185
|
+
expect(connections).toHaveLength(1);
|
|
186
|
+
expect(connections[0].id).toBe(worktreeBranchQueries.getById(kept.id)!.connection_id!);
|
|
187
|
+
});
|
|
188
|
+
|
|
189
|
+
it('deletes rather than hands back — nothing here is ever adopted', () => {
|
|
190
|
+
const account = connect();
|
|
191
|
+
const branch = cutBranch(account.id, makeWorktree('fix login').id, 'br-one');
|
|
192
|
+
reproject(account.id);
|
|
193
|
+
const connectionId = worktreeBranchQueries.getById(branch.id)!.connection_id!;
|
|
194
|
+
|
|
195
|
+
worktreeBranchQueries.remove(branch.id);
|
|
196
|
+
reproject(account.id);
|
|
197
|
+
|
|
198
|
+
expect(dbClientConnectionQueries.get(connectionId)).toBeNull();
|
|
199
|
+
expect(integrationProjectionQueries.getForAccount(account.id)).toHaveLength(0);
|
|
200
|
+
});
|
|
201
|
+
|
|
202
|
+
it('projects nothing when the sealed connection cannot be read', () => {
|
|
203
|
+
// What a database restored without its key looks like. The branch stays
|
|
204
|
+
// listed and stays deletable; only the projection is skipped, which is the
|
|
205
|
+
// same degradation every other read path in the secrets layer takes.
|
|
206
|
+
const account = connect();
|
|
207
|
+
const worktree = makeWorktree('fix login');
|
|
208
|
+
const branch = cutBranch(account.id, worktree.id, 'br-one');
|
|
209
|
+
|
|
210
|
+
// `connectionOf` reads the opened column, so an unreadable blob is the
|
|
211
|
+
// same shape as one that never existed.
|
|
212
|
+
worktreeBranchQueries.setConnectionId(branch.id, null);
|
|
213
|
+
const row = worktreeBranchQueries.getById(branch.id)!;
|
|
214
|
+
expect(worktreeBranchQueries.connectionOf({ ...row, connection_json: null })).toBeNull();
|
|
215
|
+
});
|
|
216
|
+
|
|
217
|
+
it('lets one account own rows under both capabilities at once', () => {
|
|
218
|
+
// Neon declares `database` AND `worktree-branching`. Migration 077's
|
|
219
|
+
// `target_id` in the projection key is what allows it; without it the
|
|
220
|
+
// second projector's write would collide with the first's row.
|
|
221
|
+
const account = connect();
|
|
222
|
+
cutBranch(account.id, makeWorktree('fix login').id, 'br-one');
|
|
223
|
+
reproject(account.id);
|
|
224
|
+
|
|
225
|
+
const projections = integrationProjectionQueries.getForAccount(account.id);
|
|
226
|
+
expect(projections.every((entry) => entry.target_kind === 'db_client_connection')).toBe(true);
|
|
227
|
+
expect(projections.some((entry) => entry.capability === 'worktree-branching')).toBe(true);
|
|
228
|
+
});
|
|
229
|
+
});
|
|
@@ -0,0 +1,139 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The `worktree-branching` projector: an account owns one
|
|
3
|
+
* `db_client_connections` row per live worktree branch.
|
|
4
|
+
*
|
|
5
|
+
* Without this a branch would be a connection string in a dotenv file and
|
|
6
|
+
* nothing more — the user could see that their agent had run migrations and
|
|
7
|
+
* have no way to look at what those migrations did. With it, the branch is a
|
|
8
|
+
* connection like any other: the schema tree, the ER diagram and the query
|
|
9
|
+
* console read it without knowing it is derived, which is the whole point of
|
|
10
|
+
* keeping this layer off the path every connection already flows through.
|
|
11
|
+
*
|
|
12
|
+
* DELIBERATELY NOT A `integration_db_links` ROW, even though a link is the same
|
|
13
|
+
* shape. A link is user-managed: it appears in the link dialog and it has an
|
|
14
|
+
* Unlink button. Unlinking a worktree's branch would tear the connection out
|
|
15
|
+
* from under a live worktree while leaving the branch itself running, so the
|
|
16
|
+
* two are kept as separate projectors of separate tables — and because
|
|
17
|
+
* migration 077 widened the projection key with `target_id`, one account can
|
|
18
|
+
* own rows under both capabilities without either releasing the other's.
|
|
19
|
+
*
|
|
20
|
+
* NOTHING IS EVER ADOPTED here, which is the one way this projector is simpler
|
|
21
|
+
* than DB Client's. Adoption exists so a connection someone typed by hand
|
|
22
|
+
* before connecting an account ends up as one row rather than two. A branch
|
|
23
|
+
* that was created seconds ago, on a host the provider just minted, cannot have
|
|
24
|
+
* been typed in by anyone — so a row is always created and always deleted, and
|
|
25
|
+
* there is no snapshot to restore.
|
|
26
|
+
*/
|
|
27
|
+
|
|
28
|
+
import {
|
|
29
|
+
dbClientConnectionQueries,
|
|
30
|
+
worktreeBranchQueries,
|
|
31
|
+
worktreeQueries,
|
|
32
|
+
type WorktreeBranchRow
|
|
33
|
+
} from '$backend/database/queries';
|
|
34
|
+
import type { Projector, ProjectionContext, ProjectionResult } from '$backend/integrations';
|
|
35
|
+
import { getBranchProvider } from './registry';
|
|
36
|
+
import { debug } from '$shared/utils/logger';
|
|
37
|
+
|
|
38
|
+
/**
|
|
39
|
+
* What the connection is called in DB Client's list.
|
|
40
|
+
*
|
|
41
|
+
* The WORKTREE's name, not the branch's: the list is global, and "fix login
|
|
42
|
+
* (branch)" is the question someone actually has. The provider's own noun is
|
|
43
|
+
* used so a Turso row does not call a seeded database a branch.
|
|
44
|
+
*/
|
|
45
|
+
function connectionNameFor(row: WorktreeBranchRow, noun: string): string {
|
|
46
|
+
const worktree = row.worktree_id ? worktreeQueries.getById(row.worktree_id) : null;
|
|
47
|
+
return `${worktree?.name ?? row.branch_name} (${noun})`;
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
/**
|
|
51
|
+
* The branches that should own a connection right now.
|
|
52
|
+
*
|
|
53
|
+
* ACTIVE and still attached to a worktree. An orphan is deliberately excluded:
|
|
54
|
+
* its remote database may well still exist — that is what makes it an orphan —
|
|
55
|
+
* but the worktree it belonged to is gone, so leaving a connection behind would
|
|
56
|
+
* clutter every admin's list with rows pointing at databases nobody is using.
|
|
57
|
+
* The orphan list is where those live, and it offers a delete rather than a
|
|
58
|
+
* connection.
|
|
59
|
+
*/
|
|
60
|
+
function liveBranchesOf(accountId: string): WorktreeBranchRow[] {
|
|
61
|
+
return worktreeBranchQueries
|
|
62
|
+
.getForAccount(accountId)
|
|
63
|
+
.filter((row) => row.status === 'active' && row.worktree_id !== null);
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
export const worktreeBranchProjector: Projector = {
|
|
67
|
+
capability: 'worktree-branching',
|
|
68
|
+
targetKind: 'db_client_connection',
|
|
69
|
+
|
|
70
|
+
project(context: ProjectionContext): ProjectionResult[] {
|
|
71
|
+
const noun = getBranchProvider(context.account.provider)?.info().noun ?? 'branch';
|
|
72
|
+
const results: ProjectionResult[] = [];
|
|
73
|
+
|
|
74
|
+
for (const row of liveBranchesOf(context.account.id)) {
|
|
75
|
+
const connection = worktreeBranchQueries.connectionOf(row);
|
|
76
|
+
if (!connection) {
|
|
77
|
+
// The sealed blob could not be opened — a key that changed, or a
|
|
78
|
+
// restore from a backup without it. The branch stays listed and stays
|
|
79
|
+
// deletable; it simply projects nothing, which is the same
|
|
80
|
+
// degradation every other read path in the secrets layer takes.
|
|
81
|
+
debug.warn('worktree', `Branch ${row.id} has no readable connection — nothing to project`);
|
|
82
|
+
continue;
|
|
83
|
+
}
|
|
84
|
+
|
|
85
|
+
const fields = {
|
|
86
|
+
driver: 'postgres' as const,
|
|
87
|
+
host: connection.host,
|
|
88
|
+
port: connection.port,
|
|
89
|
+
username: connection.username,
|
|
90
|
+
database: connection.database,
|
|
91
|
+
sslMode: connection.sslMode,
|
|
92
|
+
options: { worktreeBranch: { branchId: row.id, worktreeId: row.worktree_id } }
|
|
93
|
+
};
|
|
94
|
+
|
|
95
|
+
const existing = row.connection_id ? dbClientConnectionQueries.get(row.connection_id) : null;
|
|
96
|
+
|
|
97
|
+
if (!existing) {
|
|
98
|
+
const created = dbClientConnectionQueries.create({
|
|
99
|
+
name: connectionNameFor(row, noun),
|
|
100
|
+
password: connection.password,
|
|
101
|
+
...fields
|
|
102
|
+
});
|
|
103
|
+
worktreeBranchQueries.setConnectionId(row.id, created.id);
|
|
104
|
+
results.push({
|
|
105
|
+
targetKind: 'db_client_connection',
|
|
106
|
+
targetId: created.id,
|
|
107
|
+
adopted: false,
|
|
108
|
+
restore: null
|
|
109
|
+
});
|
|
110
|
+
continue;
|
|
111
|
+
}
|
|
112
|
+
|
|
113
|
+
dbClientConnectionQueries.update(existing.id, {
|
|
114
|
+
name: connectionNameFor(row, noun),
|
|
115
|
+
...fields
|
|
116
|
+
});
|
|
117
|
+
// Through `replacePassword`, not the patch: a patch reads an empty
|
|
118
|
+
// password as "not re-typed", which is right for a form and wrong for a
|
|
119
|
+
// derived row that must equal what it was derived from.
|
|
120
|
+
dbClientConnectionQueries.replacePassword(existing.id, connection.password);
|
|
121
|
+
|
|
122
|
+
results.push({
|
|
123
|
+
targetKind: 'db_client_connection',
|
|
124
|
+
targetId: existing.id,
|
|
125
|
+
adopted: false,
|
|
126
|
+
restore: null
|
|
127
|
+
});
|
|
128
|
+
}
|
|
129
|
+
|
|
130
|
+
return results;
|
|
131
|
+
},
|
|
132
|
+
|
|
133
|
+
release(_context: ProjectionContext, targetId: string): void {
|
|
134
|
+
// Always ours, never adopted — so always a delete. The branch row keeps
|
|
135
|
+
// its `connection_id` pointing at a row that no longer exists, which the
|
|
136
|
+
// next projection treats as "never projected" and recreates.
|
|
137
|
+
dbClientConnectionQueries.delete(targetId);
|
|
138
|
+
}
|
|
139
|
+
};
|