@myrialabs/clopen 0.4.35 → 0.4.36
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/artifacts/sync.test.ts +63 -0
- package/backend/artifacts/sync.ts +56 -3
- package/backend/database/migrations/070_create_worktrees_table.ts +67 -0
- package/backend/database/migrations/index.ts +7 -0
- package/backend/database/queries/index.ts +1 -0
- package/backend/database/queries/session-queries.ts +50 -11
- package/backend/database/queries/worktree-queries.ts +148 -0
- package/backend/engine/adapters/opencode/server.ts +389 -83
- package/backend/engine/adapters/opencode/stream.ts +14 -14
- package/backend/engine/hot-reload.ts +25 -10
- package/backend/files/file-watcher.ts +35 -9
- package/backend/host/metrics.test.ts +110 -0
- package/backend/host/metrics.ts +235 -0
- package/backend/index.ts +5 -2
- package/backend/mcp/internal/servers/browser-automation/context.ts +16 -2
- package/backend/ports/attribute.test.ts +3 -2
- package/backend/ports/attribute.ts +5 -44
- package/backend/preview/browser/browser-pool.ts +81 -0
- package/backend/preview/browser/browser-preview-service.ts +142 -32
- package/backend/preview/browser/browser-tab-manager.ts +350 -113
- package/backend/preview/browser/types.ts +9 -0
- package/backend/projects/project-cleanup.integration.test.ts +1 -1
- package/backend/projects/project-cleanup.ts +7 -1
- package/backend/projects/shell-ownership.ts +65 -0
- package/backend/snapshot/snapshot-service.ts +6 -2
- package/backend/terminal/ptykit.ts +35 -6
- package/backend/utils/ws.ts +21 -1
- package/backend/worktrees/clone.ts +222 -0
- package/backend/worktrees/index.ts +29 -0
- package/backend/worktrees/merge.test.ts +93 -0
- package/backend/worktrees/merge.ts +232 -0
- package/backend/worktrees/paths.test.ts +60 -0
- package/backend/worktrees/paths.ts +60 -0
- package/backend/worktrees/resolve.ts +67 -0
- package/backend/worktrees/service.ts +315 -0
- package/backend/worktrees/tree.ts +59 -0
- package/backend/ws/access.ts +18 -0
- package/backend/ws/artifacts/generate.ts +2 -2
- package/backend/ws/chat/stream.ts +9 -2
- package/backend/ws/files/panel-state-scope.test.ts +102 -0
- package/backend/ws/files/path-access.test.ts +93 -1
- package/backend/ws/files/path-access.ts +54 -5
- package/backend/ws/files/read.ts +15 -10
- package/backend/ws/files/search.ts +7 -7
- package/backend/ws/files/state.ts +70 -8
- package/backend/ws/files/watch.ts +16 -14
- package/backend/ws/git/branch-name.ts +3 -3
- package/backend/ws/git/branch.ts +15 -15
- package/backend/ws/git/commit-message.ts +3 -3
- package/backend/ws/git/commit.ts +17 -17
- package/backend/ws/git/conflict.ts +10 -10
- package/backend/ws/git/diff.ts +9 -9
- package/backend/ws/git/ignored.ts +9 -9
- package/backend/ws/git/log.ts +3 -3
- package/backend/ws/git/remote.ts +43 -43
- package/backend/ws/git/staging.ts +16 -16
- package/backend/ws/git/status.ts +13 -13
- package/backend/ws/index.ts +4 -0
- package/backend/ws/preview/access.ts +11 -3
- package/backend/ws/projects/index.ts +5 -1
- package/backend/ws/projects/info.test.ts +54 -0
- package/backend/ws/projects/info.ts +398 -0
- package/backend/ws/sessions/crud.ts +21 -6
- package/backend/ws/snapshot/restore.ts +11 -14
- package/backend/ws/system/device-info.ts +62 -144
- package/backend/ws/worktrees/crud.ts +187 -0
- package/backend/ws/worktrees/index.ts +33 -0
- package/backend/ws/worktrees/transfer.ts +88 -0
- package/dist/assets/{index-B0iIhSO9.js → index-Bg-SUCFg.js} +1 -1
- package/dist/assets/{index-BRJqss6F.js → index-Cz4YpHVi.js} +1 -1
- package/dist/assets/main-9G7IpQpK.css +1 -0
- package/dist/assets/main-BofFsH8E.js +1085 -0
- package/dist/index.html +2 -2
- package/package.json +1 -1
- package/shared/types/database/schema.ts +26 -0
- package/shared/utils/logger.ts +1 -0
- package/shared/utils/workspace-scope.test.ts +80 -0
- package/shared/utils/workspace-scope.ts +57 -0
- package/shared/utils/ws-client.ts +25 -5
- package/shared/utils/ws-server.ts +19 -3
- package/dist/assets/main-Buko4D0t.css +0 -1
- package/dist/assets/main-C_iGUZyL.js +0 -1078
|
@@ -0,0 +1,232 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Three-way merge between a worktree and the main tree.
|
|
3
|
+
*
|
|
4
|
+
* The merge base is the tree recorded when the worktree was cloned, so a file
|
|
5
|
+
* is only in play when the source side actually changed it. When the target
|
|
6
|
+
* side changed the same file the entry is a conflict — resolvable line-by-line
|
|
7
|
+
* through `git merge-file` when both sides are text, and whole-file otherwise.
|
|
8
|
+
*
|
|
9
|
+
* `source` and `target` are direction-agnostic: applying runs worktree → main,
|
|
10
|
+
* syncing runs main → worktree, and both use this same planner.
|
|
11
|
+
*/
|
|
12
|
+
|
|
13
|
+
import fs from 'fs/promises';
|
|
14
|
+
import os from 'os';
|
|
15
|
+
import path from 'path';
|
|
16
|
+
import type { TreeMap } from '../snapshot/blob-store';
|
|
17
|
+
import { execGit } from '../git/git-executor';
|
|
18
|
+
import { resolveBinary } from '../utils/cli';
|
|
19
|
+
import { debug } from '$shared/utils/logger';
|
|
20
|
+
import { readBlobText } from './tree';
|
|
21
|
+
|
|
22
|
+
export type MergeEntryStatus = 'added' | 'modified' | 'deleted';
|
|
23
|
+
|
|
24
|
+
/** What to do with one file: take the source side, keep the target, or merge both. */
|
|
25
|
+
export type MergeResolution = 'source' | 'target' | 'merge';
|
|
26
|
+
|
|
27
|
+
export interface MergeEntry {
|
|
28
|
+
path: string;
|
|
29
|
+
status: MergeEntryStatus;
|
|
30
|
+
baseHash: string | null;
|
|
31
|
+
sourceHash: string | null;
|
|
32
|
+
targetHash: string | null;
|
|
33
|
+
/** Target also moved away from the base, so taking the source would discard work. */
|
|
34
|
+
conflict: boolean;
|
|
35
|
+
/** A clean line-level merge of both sides is available. */
|
|
36
|
+
autoMergeable: boolean;
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
export interface MergePlan {
|
|
40
|
+
entries: MergeEntry[];
|
|
41
|
+
conflicts: MergeEntry[];
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
/**
|
|
45
|
+
* Decide which files transfer from source to target.
|
|
46
|
+
*
|
|
47
|
+
* Pure: the three trees are all it reads, which is what makes the interesting
|
|
48
|
+
* cases (both sides identical, both sides deleted, target-only edits) testable
|
|
49
|
+
* without touching a disk.
|
|
50
|
+
*/
|
|
51
|
+
export function planMerge(base: TreeMap, source: TreeMap, target: TreeMap): MergePlan {
|
|
52
|
+
const paths = new Set<string>([
|
|
53
|
+
...Object.keys(base),
|
|
54
|
+
...Object.keys(source),
|
|
55
|
+
...Object.keys(target)
|
|
56
|
+
]);
|
|
57
|
+
|
|
58
|
+
const entries: MergeEntry[] = [];
|
|
59
|
+
|
|
60
|
+
for (const filePath of [...paths].sort()) {
|
|
61
|
+
const baseHash = base[filePath] ?? null;
|
|
62
|
+
const sourceHash = source[filePath] ?? null;
|
|
63
|
+
const targetHash = target[filePath] ?? null;
|
|
64
|
+
|
|
65
|
+
// The source side never touched this file — nothing to carry over.
|
|
66
|
+
if (sourceHash === baseHash) continue;
|
|
67
|
+
|
|
68
|
+
// Both sides landed on the same content; the transfer is already done.
|
|
69
|
+
if (sourceHash === targetHash) continue;
|
|
70
|
+
|
|
71
|
+
const status: MergeEntryStatus =
|
|
72
|
+
sourceHash === null ? 'deleted' : baseHash === null ? 'added' : 'modified';
|
|
73
|
+
|
|
74
|
+
entries.push({
|
|
75
|
+
path: filePath,
|
|
76
|
+
status,
|
|
77
|
+
baseHash,
|
|
78
|
+
sourceHash,
|
|
79
|
+
targetHash,
|
|
80
|
+
conflict: targetHash !== baseHash,
|
|
81
|
+
autoMergeable: false
|
|
82
|
+
});
|
|
83
|
+
}
|
|
84
|
+
|
|
85
|
+
return { entries, conflicts: entries.filter((entry) => entry.conflict) };
|
|
86
|
+
}
|
|
87
|
+
|
|
88
|
+
/**
|
|
89
|
+
* Mark conflicts that `git merge-file` can resolve on its own.
|
|
90
|
+
* Runs before the user is asked anything, so the dialog only shows the files
|
|
91
|
+
* that genuinely need a decision.
|
|
92
|
+
*/
|
|
93
|
+
export async function markAutoMergeable(
|
|
94
|
+
plan: MergePlan,
|
|
95
|
+
sourceRoot: string,
|
|
96
|
+
targetRoot: string
|
|
97
|
+
): Promise<MergePlan> {
|
|
98
|
+
if (!resolveBinary('git')) return plan;
|
|
99
|
+
|
|
100
|
+
for (const entry of plan.conflicts) {
|
|
101
|
+
// A deletion on either side has no text to merge.
|
|
102
|
+
if (entry.status === 'deleted' || entry.targetHash === null) continue;
|
|
103
|
+
const merged = await mergeFileContents(entry, sourceRoot, targetRoot);
|
|
104
|
+
entry.autoMergeable = merged !== null;
|
|
105
|
+
}
|
|
106
|
+
|
|
107
|
+
return plan;
|
|
108
|
+
}
|
|
109
|
+
|
|
110
|
+
/**
|
|
111
|
+
* Three-way merge one file's text. Returns null when it cannot be merged
|
|
112
|
+
* cleanly — conflicting edits, a missing base blob, or binary content.
|
|
113
|
+
*/
|
|
114
|
+
export async function mergeFileContents(
|
|
115
|
+
entry: MergeEntry,
|
|
116
|
+
sourceRoot: string,
|
|
117
|
+
targetRoot: string
|
|
118
|
+
): Promise<string | null> {
|
|
119
|
+
if (entry.baseHash === null || entry.sourceHash === null || entry.targetHash === null) {
|
|
120
|
+
return null;
|
|
121
|
+
}
|
|
122
|
+
|
|
123
|
+
const baseText = await readBlobText(entry.baseHash);
|
|
124
|
+
if (baseText === null) return null;
|
|
125
|
+
|
|
126
|
+
const sourceText = await readText(path.join(sourceRoot, entry.path));
|
|
127
|
+
const targetText = await readText(path.join(targetRoot, entry.path));
|
|
128
|
+
if (sourceText === null || targetText === null) return null;
|
|
129
|
+
|
|
130
|
+
const scratch = await fs.mkdtemp(path.join(os.tmpdir(), 'clopen-merge-'));
|
|
131
|
+
try {
|
|
132
|
+
const currentFile = path.join(scratch, 'current');
|
|
133
|
+
const baseFile = path.join(scratch, 'base');
|
|
134
|
+
const otherFile = path.join(scratch, 'other');
|
|
135
|
+
|
|
136
|
+
await Promise.all([
|
|
137
|
+
fs.writeFile(currentFile, targetText),
|
|
138
|
+
fs.writeFile(baseFile, baseText),
|
|
139
|
+
fs.writeFile(otherFile, sourceText)
|
|
140
|
+
]);
|
|
141
|
+
|
|
142
|
+
// `-p` prints the result instead of rewriting `current`; a non-zero exit
|
|
143
|
+
// is the conflict count, not a failure to run.
|
|
144
|
+
const result = await execGit(
|
|
145
|
+
['merge-file', '-p', currentFile, baseFile, otherFile],
|
|
146
|
+
scratch,
|
|
147
|
+
{ okExitCodes: [1] }
|
|
148
|
+
);
|
|
149
|
+
|
|
150
|
+
return result.exitCode === 0 ? result.stdout : null;
|
|
151
|
+
} catch (error) {
|
|
152
|
+
debug.warn('worktree', `merge-file failed for ${entry.path}: ${error}`);
|
|
153
|
+
return null;
|
|
154
|
+
} finally {
|
|
155
|
+
await fs.rm(scratch, { recursive: true, force: true }).catch(() => {});
|
|
156
|
+
}
|
|
157
|
+
}
|
|
158
|
+
|
|
159
|
+
/** Read a file as text, or null when it is missing or contains a NUL byte. */
|
|
160
|
+
async function readText(filePath: string): Promise<string | null> {
|
|
161
|
+
try {
|
|
162
|
+
const buffer = await fs.readFile(filePath);
|
|
163
|
+
if (buffer.includes(0)) return null;
|
|
164
|
+
return buffer.toString('utf-8');
|
|
165
|
+
} catch {
|
|
166
|
+
return null;
|
|
167
|
+
}
|
|
168
|
+
}
|
|
169
|
+
|
|
170
|
+
export interface MergeApplyResult {
|
|
171
|
+
written: string[];
|
|
172
|
+
deleted: string[];
|
|
173
|
+
skipped: string[];
|
|
174
|
+
failed: string[];
|
|
175
|
+
}
|
|
176
|
+
|
|
177
|
+
/**
|
|
178
|
+
* Carry the planned changes into `targetRoot`.
|
|
179
|
+
*
|
|
180
|
+
* `resolutions` only has to cover conflicts; clean entries default to taking
|
|
181
|
+
* the source side, which is the whole point of the transfer.
|
|
182
|
+
*/
|
|
183
|
+
export async function applyMergePlan(
|
|
184
|
+
plan: MergePlan,
|
|
185
|
+
sourceRoot: string,
|
|
186
|
+
targetRoot: string,
|
|
187
|
+
resolutions: Record<string, MergeResolution> = {}
|
|
188
|
+
): Promise<MergeApplyResult> {
|
|
189
|
+
const result: MergeApplyResult = { written: [], deleted: [], skipped: [], failed: [] };
|
|
190
|
+
|
|
191
|
+
for (const entry of plan.entries) {
|
|
192
|
+
const resolution: MergeResolution = entry.conflict
|
|
193
|
+
? resolutions[entry.path] ?? 'target'
|
|
194
|
+
: 'source';
|
|
195
|
+
|
|
196
|
+
if (resolution === 'target') {
|
|
197
|
+
result.skipped.push(entry.path);
|
|
198
|
+
continue;
|
|
199
|
+
}
|
|
200
|
+
|
|
201
|
+
const targetPath = path.join(targetRoot, entry.path);
|
|
202
|
+
|
|
203
|
+
try {
|
|
204
|
+
if (resolution === 'merge') {
|
|
205
|
+
const merged = await mergeFileContents(entry, sourceRoot, targetRoot);
|
|
206
|
+
if (merged === null) {
|
|
207
|
+
result.failed.push(entry.path);
|
|
208
|
+
continue;
|
|
209
|
+
}
|
|
210
|
+
await fs.mkdir(path.dirname(targetPath), { recursive: true });
|
|
211
|
+
await fs.writeFile(targetPath, merged);
|
|
212
|
+
result.written.push(entry.path);
|
|
213
|
+
continue;
|
|
214
|
+
}
|
|
215
|
+
|
|
216
|
+
if (entry.status === 'deleted') {
|
|
217
|
+
await fs.rm(targetPath, { force: true });
|
|
218
|
+
result.deleted.push(entry.path);
|
|
219
|
+
continue;
|
|
220
|
+
}
|
|
221
|
+
|
|
222
|
+
await fs.mkdir(path.dirname(targetPath), { recursive: true });
|
|
223
|
+
await fs.copyFile(path.join(sourceRoot, entry.path), targetPath);
|
|
224
|
+
result.written.push(entry.path);
|
|
225
|
+
} catch (error) {
|
|
226
|
+
debug.warn('worktree', `Failed to transfer ${entry.path}: ${error}`);
|
|
227
|
+
result.failed.push(entry.path);
|
|
228
|
+
}
|
|
229
|
+
}
|
|
230
|
+
|
|
231
|
+
return result;
|
|
232
|
+
}
|
|
@@ -0,0 +1,60 @@
|
|
|
1
|
+
import { describe, expect, test } from 'bun:test';
|
|
2
|
+
import path from 'path';
|
|
3
|
+
import { isPathInside, slugifyWorktreeName, uniqueWorktreeSlug } from './paths';
|
|
4
|
+
|
|
5
|
+
describe('slugifyWorktreeName', () => {
|
|
6
|
+
test('lowercases and hyphenates', () => {
|
|
7
|
+
expect(slugifyWorktreeName('Refactor Auth')).toBe('refactor-auth');
|
|
8
|
+
});
|
|
9
|
+
|
|
10
|
+
test('strips characters that cannot be a directory name', () => {
|
|
11
|
+
expect(slugifyWorktreeName('fix/../etc passwd')).toBe('fix-etc-passwd');
|
|
12
|
+
});
|
|
13
|
+
|
|
14
|
+
test('trims leading and trailing separators', () => {
|
|
15
|
+
expect(slugifyWorktreeName(' --hello-- ')).toBe('hello');
|
|
16
|
+
});
|
|
17
|
+
|
|
18
|
+
test('returns empty when nothing usable survives', () => {
|
|
19
|
+
expect(slugifyWorktreeName('///')).toBe('');
|
|
20
|
+
});
|
|
21
|
+
|
|
22
|
+
test('caps the length', () => {
|
|
23
|
+
expect(slugifyWorktreeName('a'.repeat(200)).length).toBe(48);
|
|
24
|
+
});
|
|
25
|
+
});
|
|
26
|
+
|
|
27
|
+
describe('uniqueWorktreeSlug', () => {
|
|
28
|
+
test('uses the plain slug when free', () => {
|
|
29
|
+
expect(uniqueWorktreeSlug('Feature X', new Set())).toBe('feature-x');
|
|
30
|
+
});
|
|
31
|
+
|
|
32
|
+
test('suffixes on collision', () => {
|
|
33
|
+
expect(uniqueWorktreeSlug('Feature X', new Set(['feature-x']))).toBe('feature-x-2');
|
|
34
|
+
expect(uniqueWorktreeSlug('Feature X', new Set(['feature-x', 'feature-x-2']))).toBe('feature-x-3');
|
|
35
|
+
});
|
|
36
|
+
|
|
37
|
+
test('falls back to a default stem for unusable names', () => {
|
|
38
|
+
expect(uniqueWorktreeSlug('///', new Set())).toBe('worktree');
|
|
39
|
+
});
|
|
40
|
+
});
|
|
41
|
+
|
|
42
|
+
describe('isPathInside', () => {
|
|
43
|
+
const root = path.join('/tmp', 'clopen-root');
|
|
44
|
+
|
|
45
|
+
test('accepts the root itself', () => {
|
|
46
|
+
expect(isPathInside(root, root)).toBe(true);
|
|
47
|
+
});
|
|
48
|
+
|
|
49
|
+
test('accepts a nested path', () => {
|
|
50
|
+
expect(isPathInside(root, path.join(root, 'src', 'index.ts'))).toBe(true);
|
|
51
|
+
});
|
|
52
|
+
|
|
53
|
+
test('rejects an escape', () => {
|
|
54
|
+
expect(isPathInside(root, path.join(root, '..', 'elsewhere'))).toBe(false);
|
|
55
|
+
});
|
|
56
|
+
|
|
57
|
+
test('rejects a sibling with a shared prefix', () => {
|
|
58
|
+
expect(isPathInside(root, `${root}-other`)).toBe(false);
|
|
59
|
+
});
|
|
60
|
+
});
|
|
@@ -0,0 +1,60 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Worktree location and naming.
|
|
3
|
+
*
|
|
4
|
+
* Worktrees live outside the project directory. Inside it they would be picked
|
|
5
|
+
* up as nested repos by the Git panel, walked by the file watcher, and shown to
|
|
6
|
+
* the agent as part of its own tree.
|
|
7
|
+
*/
|
|
8
|
+
|
|
9
|
+
import path from 'path';
|
|
10
|
+
import { getClopenDir } from '../utils/paths';
|
|
11
|
+
|
|
12
|
+
/** Root that holds every worktree of every project. */
|
|
13
|
+
export function getWorktreesRootDir(): string {
|
|
14
|
+
return path.join(getClopenDir(), 'worktrees');
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
/** Directory that holds one project's worktrees. */
|
|
18
|
+
export function getProjectWorktreesDir(projectId: string): string {
|
|
19
|
+
return path.join(getWorktreesRootDir(), projectId);
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
/** Absolute path of a single worktree. */
|
|
23
|
+
export function getWorktreePath(projectId: string, slug: string): string {
|
|
24
|
+
return path.join(getProjectWorktreesDir(projectId), slug);
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
/**
|
|
28
|
+
* Turn a display name into a directory-safe slug.
|
|
29
|
+
* Returns an empty string when nothing usable survives — callers substitute one.
|
|
30
|
+
*/
|
|
31
|
+
export function slugifyWorktreeName(name: string): string {
|
|
32
|
+
return name
|
|
33
|
+
.toLowerCase()
|
|
34
|
+
.normalize('NFKD')
|
|
35
|
+
.replace(/[^a-z0-9]+/g, '-')
|
|
36
|
+
.replace(/^-+|-+$/g, '')
|
|
37
|
+
.slice(0, 48);
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
/**
|
|
41
|
+
* Pick a slug not already used by this project.
|
|
42
|
+
* `taken` is the set of existing slugs; a numeric suffix is added on collision.
|
|
43
|
+
*/
|
|
44
|
+
export function uniqueWorktreeSlug(name: string, taken: Set<string>): string {
|
|
45
|
+
const base = slugifyWorktreeName(name) || 'worktree';
|
|
46
|
+
if (!taken.has(base)) return base;
|
|
47
|
+
|
|
48
|
+
for (let suffix = 2; suffix < 1000; suffix++) {
|
|
49
|
+
const candidate = `${base}-${suffix}`;
|
|
50
|
+
if (!taken.has(candidate)) return candidate;
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
return `${base}-${Date.now()}`;
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
/** Whether `candidate` sits inside `root` (or is `root` itself). */
|
|
57
|
+
export function isPathInside(root: string, candidate: string): boolean {
|
|
58
|
+
const relative = path.relative(path.resolve(root), path.resolve(candidate));
|
|
59
|
+
return relative === '' || (!relative.startsWith('..') && !path.isAbsolute(relative));
|
|
60
|
+
}
|
|
@@ -0,0 +1,67 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Resolving the working root of a chat session.
|
|
3
|
+
*
|
|
4
|
+
* Anything that runs on behalf of a session — the engine, snapshots, file
|
|
5
|
+
* reads — must use this rather than the path the client sent. The client's
|
|
6
|
+
* value is a view preference; the session's worktree is the fact, and letting
|
|
7
|
+
* the two disagree is how an isolated task ends up writing into the main tree.
|
|
8
|
+
*/
|
|
9
|
+
|
|
10
|
+
import { existsSync } from 'fs';
|
|
11
|
+
import { projectQueries, sessionQueries, worktreeQueries } from '../database/queries';
|
|
12
|
+
import { debug } from '$shared/utils/logger';
|
|
13
|
+
|
|
14
|
+
export interface SessionRoot {
|
|
15
|
+
path: string;
|
|
16
|
+
projectId: string;
|
|
17
|
+
worktreeId: string | null;
|
|
18
|
+
worktreeName: string | null;
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
/**
|
|
22
|
+
* Root directory a worktree points at, or the project path when `worktreeId` is
|
|
23
|
+
* null. Falls back to the project when the worktree directory has vanished —
|
|
24
|
+
* a session must stay usable even if its worktree was deleted outside Clopen.
|
|
25
|
+
*/
|
|
26
|
+
export function resolveWorktreeRoot(projectId: string, worktreeId: string | null): SessionRoot {
|
|
27
|
+
const project = projectQueries.getById(projectId);
|
|
28
|
+
const projectPath = project?.path ?? '';
|
|
29
|
+
|
|
30
|
+
if (!worktreeId) {
|
|
31
|
+
return { path: projectPath, projectId, worktreeId: null, worktreeName: null };
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
const worktree = worktreeQueries.getById(worktreeId);
|
|
35
|
+
if (!worktree || worktree.project_id !== projectId) {
|
|
36
|
+
return { path: projectPath, projectId, worktreeId: null, worktreeName: null };
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
if (!existsSync(worktree.path)) {
|
|
40
|
+
debug.warn('worktree', `Worktree directory missing, using main tree: ${worktree.path}`);
|
|
41
|
+
return { path: projectPath, projectId, worktreeId: null, worktreeName: null };
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
return {
|
|
45
|
+
path: worktree.path,
|
|
46
|
+
projectId,
|
|
47
|
+
worktreeId: worktree.id,
|
|
48
|
+
worktreeName: worktree.name
|
|
49
|
+
};
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
/** Working root for a session, or null when the session is unknown. */
|
|
53
|
+
export function resolveSessionRoot(sessionId: string): SessionRoot | null {
|
|
54
|
+
const session = sessionQueries.getById(sessionId);
|
|
55
|
+
if (!session) return null;
|
|
56
|
+
return resolveWorktreeRoot(session.project_id, session.worktree_id ?? null);
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
/**
|
|
60
|
+
* Working root for a session, preferring the session's own binding and falling
|
|
61
|
+
* back to the caller-supplied path when the session cannot be resolved.
|
|
62
|
+
*/
|
|
63
|
+
export function resolveSessionPath(sessionId: string | undefined, fallbackPath: string): string {
|
|
64
|
+
if (!sessionId) return fallbackPath;
|
|
65
|
+
const root = resolveSessionRoot(sessionId);
|
|
66
|
+
return root?.path || fallbackPath;
|
|
67
|
+
}
|