@myrialabs/clopen 0.4.3 → 0.4.5
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/README.md +2 -0
- package/backend/auth/permissions.ts +1 -0
- package/backend/database/migrations/043_relocate_engine_config_dirs.ts +71 -0
- package/backend/database/migrations/index.ts +7 -0
- package/backend/database/queries/mcp-server-queries.ts +14 -0
- package/backend/engine/README.md +12 -8
- package/backend/engine/adapters/claude/environment.ts +3 -3
- package/backend/engine/adapters/codex/credential.ts +12 -2
- package/backend/engine/adapters/codex/stream.ts +6 -0
- package/backend/engine/adapters/copilot/stream.ts +5 -1
- package/backend/engine/adapters/opencode/server.ts +36 -5
- package/backend/engine/adapters/qwen/environment.ts +7 -0
- package/backend/engine/adapters/qwen/session-fork.ts +13 -1
- package/backend/engine/docs/database.md +9 -6
- package/backend/engine/docs/lessons-learned.md +117 -16
- package/backend/git/git-service.ts +50 -1
- package/backend/mcp/external/parse.test.ts +190 -0
- package/backend/mcp/external/parse.ts +293 -0
- package/backend/mcp/external/probe.ts +55 -5
- package/backend/mcp/external/registry-client.ts +8 -1
- package/backend/mcp/index.ts +2 -0
- package/backend/snapshot/gitignore.ts +230 -7
- package/backend/snapshot/helpers.ts +34 -10
- package/backend/utils/paths.ts +16 -0
- package/backend/ws/engine/codex/accounts.ts +3 -0
- package/backend/ws/files/read.ts +10 -1
- package/backend/ws/git/branch-name.ts +20 -4
- package/backend/ws/git/branch.ts +66 -11
- package/backend/ws/git/commit-message.ts +18 -3
- package/backend/ws/git/commit.ts +50 -16
- package/backend/ws/git/conflict.ts +47 -5
- package/backend/ws/git/diff.ts +38 -4
- package/backend/ws/git/log.ts +22 -2
- package/backend/ws/git/remote.ts +82 -33
- package/backend/ws/git/staging.ts +39 -9
- package/backend/ws/git/status.ts +52 -0
- package/backend/ws/mcp/crud.ts +42 -2
- package/backend/ws/snapshot/timeline.ts +1 -1
- package/dist/assets/main-gNUEJAC5.css +1 -0
- package/dist/assets/{main-D-AcjotJ.js → main-zXKH_C5O.js} +318 -290
- package/dist/index.html +2 -2
- package/package.json +1 -1
- package/shared/types/git.ts +20 -0
- package/dist/assets/main-DJf71Zum.css +0 -1
|
@@ -22,6 +22,138 @@ const ALWAYS_EXCLUDE_DIRS = new Set([
|
|
|
22
22
|
'node_modules',
|
|
23
23
|
]);
|
|
24
24
|
|
|
25
|
+
/**
|
|
26
|
+
* Find paths of all nested git repos (directories with their own `.git`) at any
|
|
27
|
+
* depth under `rootPath`. Walks into nested repos to find deeply nested ones.
|
|
28
|
+
* Does NOT scan the repos — just returns their absolute paths.
|
|
29
|
+
*
|
|
30
|
+
* Used by callers that need to run git commands inside each nested repo
|
|
31
|
+
* (e.g. `git:status` aggregating changes from nested repos into the Changes
|
|
32
|
+
* tab of the Git panel).
|
|
33
|
+
*/
|
|
34
|
+
export async function findNestedRepoPaths(rootPath: string): Promise<string[]> {
|
|
35
|
+
const paths: string[] = [];
|
|
36
|
+
|
|
37
|
+
const walk = async (currentPath: string): Promise<void> => {
|
|
38
|
+
let entries;
|
|
39
|
+
try {
|
|
40
|
+
entries = await fs.readdir(currentPath, { withFileTypes: true });
|
|
41
|
+
} catch {
|
|
42
|
+
return;
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
for (const entry of entries) {
|
|
46
|
+
if (!entry.isDirectory()) continue;
|
|
47
|
+
if (ALWAYS_EXCLUDE_DIRS.has(entry.name)) continue;
|
|
48
|
+
|
|
49
|
+
const fullPath = path.join(currentPath, entry.name);
|
|
50
|
+
|
|
51
|
+
// Check if this directory is itself a git repo
|
|
52
|
+
try {
|
|
53
|
+
await fs.access(path.join(fullPath, '.git'));
|
|
54
|
+
paths.push(fullPath);
|
|
55
|
+
// Fall through to also walk into it for deeper nested repos
|
|
56
|
+
} catch {
|
|
57
|
+
// Not a git repo
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
await walk(fullPath);
|
|
61
|
+
}
|
|
62
|
+
};
|
|
63
|
+
|
|
64
|
+
await walk(rootPath);
|
|
65
|
+
return paths;
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
/**
|
|
69
|
+
* Read the outer repo's `.gitmodules` file and return the set of relative
|
|
70
|
+
* submodule paths. Returns an empty set when the file is missing or unreadable
|
|
71
|
+
* (e.g. the project isn't a git repo at all, or has no submodules).
|
|
72
|
+
*
|
|
73
|
+
* The format is INI-like:
|
|
74
|
+
* [submodule "vendor/foo"]
|
|
75
|
+
* path = vendor/foo
|
|
76
|
+
* url = git@example.com:vendor/foo.git
|
|
77
|
+
*
|
|
78
|
+
* The `path =` value is what we return — the directory inside the project root
|
|
79
|
+
* where the submodule is checked out. If a section omits `path =`, git falls
|
|
80
|
+
* back to the section name; we mirror that.
|
|
81
|
+
*/
|
|
82
|
+
export async function findSubmodulePaths(rootPath: string): Promise<Set<string>> {
|
|
83
|
+
const out = new Set<string>();
|
|
84
|
+
let raw: string;
|
|
85
|
+
try {
|
|
86
|
+
raw = await fs.readFile(path.join(rootPath, '.gitmodules'), 'utf8');
|
|
87
|
+
} catch {
|
|
88
|
+
return out; // file missing → no submodules
|
|
89
|
+
}
|
|
90
|
+
|
|
91
|
+
// Two-pass parse: collect per-section { name, path? } then resolve.
|
|
92
|
+
// Single-pass is simpler but needs careful handling of the implicit
|
|
93
|
+
// section-name fallback; the two-pass version reads more clearly.
|
|
94
|
+
const sections: Array<{ name: string; path?: string }> = [];
|
|
95
|
+
let current: { name: string; path?: string } | null = null;
|
|
96
|
+
for (const line of raw.split(/\r?\n/)) {
|
|
97
|
+
const trimmed = line.trim();
|
|
98
|
+
if (!trimmed || trimmed.startsWith('#')) continue;
|
|
99
|
+
|
|
100
|
+
const sectionMatch = trimmed.match(/^\[submodule\s+"([^"]+)"\]\s*$/);
|
|
101
|
+
if (sectionMatch) {
|
|
102
|
+
if (current) sections.push(current);
|
|
103
|
+
current = { name: sectionMatch[1] };
|
|
104
|
+
continue;
|
|
105
|
+
}
|
|
106
|
+
if (!current) continue;
|
|
107
|
+
|
|
108
|
+
const pathMatch = trimmed.match(/^path\s*=\s*(.+?)\s*$/);
|
|
109
|
+
if (pathMatch) current.path = pathMatch[1].replace(/\\/g, '/');
|
|
110
|
+
}
|
|
111
|
+
if (current) sections.push(current);
|
|
112
|
+
|
|
113
|
+
for (const s of sections) {
|
|
114
|
+
out.add(s.path ?? s.name);
|
|
115
|
+
}
|
|
116
|
+
return out;
|
|
117
|
+
}
|
|
118
|
+
|
|
119
|
+
/**
|
|
120
|
+
* Given a file path relative to the project root, find the deepest nested
|
|
121
|
+
* git repo that contains it. Returns the repo's absolute path and the file
|
|
122
|
+
* path relative to that repo, or `null` if the file lives in the outer repo.
|
|
123
|
+
*
|
|
124
|
+
* Used by git staging/discard handlers so that `git add` / `git restore` /
|
|
125
|
+
* `git rm` run inside the correct repo (the outer repo's `git` would skip
|
|
126
|
+
* or fail on files that are tracked by a nested repo).
|
|
127
|
+
*/
|
|
128
|
+
export async function findRepoForFile(
|
|
129
|
+
projectPath: string,
|
|
130
|
+
filePath: string
|
|
131
|
+
): Promise<{ repoPath: string; relativeFilePath: string } | null> {
|
|
132
|
+
const normalizedFilePath = filePath.replace(/\\/g, '/');
|
|
133
|
+
const nestedRepoPaths = await findNestedRepoPaths(projectPath);
|
|
134
|
+
|
|
135
|
+
let bestMatch: { repoPath: string; relativeFilePath: string } | null = null;
|
|
136
|
+
let bestRepoRelLen = -1;
|
|
137
|
+
|
|
138
|
+
for (const repoPath of nestedRepoPaths) {
|
|
139
|
+
const repoRel = path.relative(projectPath, repoPath).replace(/\\/g, '/');
|
|
140
|
+
// File is inside this repo if its path starts with `repoRel/`
|
|
141
|
+
if (normalizedFilePath === repoRel || normalizedFilePath.startsWith(repoRel + '/')) {
|
|
142
|
+
if (repoRel.length > bestRepoRelLen) {
|
|
143
|
+
bestMatch = {
|
|
144
|
+
repoPath,
|
|
145
|
+
relativeFilePath: normalizedFilePath === repoRel
|
|
146
|
+
? ''
|
|
147
|
+
: normalizedFilePath.substring(repoRel.length + 1)
|
|
148
|
+
};
|
|
149
|
+
bestRepoRelLen = repoRel.length;
|
|
150
|
+
}
|
|
151
|
+
}
|
|
152
|
+
}
|
|
153
|
+
|
|
154
|
+
return bestMatch;
|
|
155
|
+
}
|
|
156
|
+
|
|
25
157
|
/**
|
|
26
158
|
* Get list of snapshot-eligible files using git (preferred) or manual scan.
|
|
27
159
|
* Returns full absolute paths.
|
|
@@ -49,13 +181,39 @@ async function scanWithGit(dirPath: string): Promise<string[] | null> {
|
|
|
49
181
|
return null;
|
|
50
182
|
}
|
|
51
183
|
|
|
184
|
+
try {
|
|
185
|
+
// Scan the outer repo
|
|
186
|
+
const files = await scanSingleGitRepo(dirPath);
|
|
187
|
+
|
|
188
|
+
// Find and scan nested git repos — separate repositories living inside
|
|
189
|
+
// the project (e.g. a theme extracted into its own repo). The parent
|
|
190
|
+
// repo's `git ls-files --exclude-standard` skips these because git treats
|
|
191
|
+
// a nested repo as a single gitlink entry (or excludes it entirely when
|
|
192
|
+
// it's listed in the parent's .gitignore). Without this step, files
|
|
193
|
+
// inside nested repos are invisible to the snapshot system, so AI
|
|
194
|
+
// changes to them never appear in the checkpoint banner.
|
|
195
|
+
const nestedFiles = await findAndScanNestedRepos(dirPath);
|
|
196
|
+
|
|
197
|
+
const allFiles = [...files, ...nestedFiles];
|
|
198
|
+
debug.log('snapshot', `Git scan found ${allFiles.length} files (${files.length} outer, ${nestedFiles.length} nested)`);
|
|
199
|
+
return allFiles;
|
|
200
|
+
} catch (err) {
|
|
201
|
+
debug.warn('snapshot', 'git ls-files failed, falling back to manual scan:', err);
|
|
202
|
+
return null;
|
|
203
|
+
}
|
|
204
|
+
}
|
|
205
|
+
|
|
206
|
+
/**
|
|
207
|
+
* Run `git ls-files -co --exclude-standard` in a single git repo and return
|
|
208
|
+
* absolute paths. Used for both the outer repo and nested repos.
|
|
209
|
+
*/
|
|
210
|
+
async function scanSingleGitRepo(dirPath: string): Promise<string[]> {
|
|
52
211
|
try {
|
|
53
212
|
const result = await execGit(['ls-files', '-co', '--exclude-standard'], dirPath, 60_000);
|
|
54
|
-
if (result.exitCode !== 0) return
|
|
55
|
-
const output = result.stdout;
|
|
213
|
+
if (result.exitCode !== 0) return [];
|
|
56
214
|
|
|
57
215
|
const files: string[] = [];
|
|
58
|
-
for (const line of
|
|
216
|
+
for (const line of result.stdout.split('\n')) {
|
|
59
217
|
const relativePath = line.trim();
|
|
60
218
|
if (!relativePath) continue;
|
|
61
219
|
|
|
@@ -63,17 +221,70 @@ async function scanWithGit(dirPath: string): Promise<string[] | null> {
|
|
|
63
221
|
const firstSegment = relativePath.split('/')[0];
|
|
64
222
|
if (ALWAYS_EXCLUDE_DIRS.has(firstSegment)) continue;
|
|
65
223
|
|
|
224
|
+
// Skip directory entries — git emits nested repos as a single
|
|
225
|
+
// entry with a trailing slash (e.g. `theme/`). These are handled
|
|
226
|
+
// by findAndScanNestedRepos, not here.
|
|
227
|
+
if (relativePath.endsWith('/')) continue;
|
|
228
|
+
|
|
66
229
|
files.push(path.join(dirPath, relativePath));
|
|
67
230
|
}
|
|
68
|
-
|
|
69
|
-
debug.log('snapshot', `Git scan found ${files.length} files`);
|
|
70
231
|
return files;
|
|
71
232
|
} catch (err) {
|
|
72
|
-
debug.warn('snapshot',
|
|
73
|
-
return
|
|
233
|
+
debug.warn('snapshot', `git ls-files failed for ${dirPath}:`, err);
|
|
234
|
+
return [];
|
|
74
235
|
}
|
|
75
236
|
}
|
|
76
237
|
|
|
238
|
+
/**
|
|
239
|
+
* Walk the project tree to find nested git repos (directories with their own
|
|
240
|
+
* `.git`) and scan each one independently. This catches nested repos that are
|
|
241
|
+
* gitignored by the parent — they'd otherwise be invisible to the outer repo's
|
|
242
|
+
* `git ls-files --exclude-standard`.
|
|
243
|
+
*
|
|
244
|
+
* The walk respects ALWAYS_EXCLUDE_DIRS (skips `.git`, `node_modules`) but does
|
|
245
|
+
* NOT respect .gitignore, because the nested repo we're looking for may itself
|
|
246
|
+
* be gitignored by the parent. Once a nested repo is found, its own
|
|
247
|
+
* `git ls-files --exclude-standard` handles its .gitignore rules.
|
|
248
|
+
*/
|
|
249
|
+
async function findAndScanNestedRepos(rootPath: string): Promise<string[]> {
|
|
250
|
+
const files: string[] = [];
|
|
251
|
+
|
|
252
|
+
const walk = async (currentPath: string): Promise<void> => {
|
|
253
|
+
let entries;
|
|
254
|
+
try {
|
|
255
|
+
entries = await fs.readdir(currentPath, { withFileTypes: true });
|
|
256
|
+
} catch {
|
|
257
|
+
return;
|
|
258
|
+
}
|
|
259
|
+
|
|
260
|
+
for (const entry of entries) {
|
|
261
|
+
if (!entry.isDirectory()) continue;
|
|
262
|
+
if (ALWAYS_EXCLUDE_DIRS.has(entry.name)) continue;
|
|
263
|
+
|
|
264
|
+
const fullPath = path.join(currentPath, entry.name);
|
|
265
|
+
|
|
266
|
+
// Check if this directory is itself a git repo (`.git` can be a
|
|
267
|
+
// directory or a file — the latter for worktrees/submodules).
|
|
268
|
+
try {
|
|
269
|
+
await fs.access(path.join(fullPath, '.git'));
|
|
270
|
+
// Nested repo found — scan it independently, then continue
|
|
271
|
+
// walking into it to find even deeper nested repos.
|
|
272
|
+
const nestedFiles = await scanSingleGitRepo(fullPath);
|
|
273
|
+
files.push(...nestedFiles);
|
|
274
|
+
await walk(fullPath);
|
|
275
|
+
continue;
|
|
276
|
+
} catch {
|
|
277
|
+
// Not a git repo — recurse to find deeper nested repos
|
|
278
|
+
}
|
|
279
|
+
|
|
280
|
+
await walk(fullPath);
|
|
281
|
+
}
|
|
282
|
+
};
|
|
283
|
+
|
|
284
|
+
await walk(rootPath);
|
|
285
|
+
return files;
|
|
286
|
+
}
|
|
287
|
+
|
|
77
288
|
// ============================================================================
|
|
78
289
|
// Strategy 2: Manual scan with .gitignore parsing
|
|
79
290
|
// ============================================================================
|
|
@@ -282,6 +493,18 @@ async function scanWithGitignoreParsing(projectPath: string): Promise<string[]>
|
|
|
282
493
|
if (ALWAYS_EXCLUDE_DIRS.has(entry.name)) continue;
|
|
283
494
|
|
|
284
495
|
if (entry.isDirectory()) {
|
|
496
|
+
// Check if this is a nested git repo — scan it independently
|
|
497
|
+
// and bypass the .gitignore filter so files inside a
|
|
498
|
+
// gitignored nested repo are still tracked.
|
|
499
|
+
try {
|
|
500
|
+
await fs.access(path.join(fullPath, '.git'));
|
|
501
|
+
const nestedFiles = await scanSingleGitRepo(fullPath);
|
|
502
|
+
files.push(...nestedFiles);
|
|
503
|
+
continue;
|
|
504
|
+
} catch {
|
|
505
|
+
// Not a git repo — apply gitignore filter and recurse
|
|
506
|
+
}
|
|
507
|
+
|
|
285
508
|
if (!filter.isIgnored(relativePath, true)) {
|
|
286
509
|
await scan(fullPath);
|
|
287
510
|
}
|
|
@@ -1,6 +1,8 @@
|
|
|
1
1
|
import { snapshotQueries, messageQueries, checkpointQueries } from '../database/queries';
|
|
2
|
+
import { blobStore } from './blob-store';
|
|
2
3
|
import { debug } from '$shared/utils/logger';
|
|
3
4
|
import { loadMessage } from '$shared/utils/message-formatter';
|
|
5
|
+
import { calculateFileChangeStats } from '$shared/utils/diff-calculator';
|
|
4
6
|
import type { DatabaseMessage } from '$shared/types/database/schema';
|
|
5
7
|
import type { UnifiedMessage, UserMessage } from '$shared/types/unified';
|
|
6
8
|
|
|
@@ -320,27 +322,49 @@ export function isDescendant(
|
|
|
320
322
|
* The snapshot associated with the checkpoint message itself contains the stats
|
|
321
323
|
* (file changes the assistant made in response to this user message).
|
|
322
324
|
*/
|
|
323
|
-
export function getCheckpointFileStats(
|
|
325
|
+
export async function getCheckpointFileStats(
|
|
324
326
|
checkpointMsg: DatabaseMessage
|
|
325
|
-
): { filesChanged: number; insertions: number; deletions: number } {
|
|
327
|
+
): Promise<{ filesChanged: number; insertions: number; deletions: number }> {
|
|
326
328
|
const snapshot = snapshotQueries.getByMessageId(checkpointMsg.id);
|
|
327
329
|
if (!snapshot) {
|
|
328
330
|
return { filesChanged: 0, insertions: 0, deletions: 0 };
|
|
329
331
|
}
|
|
330
332
|
|
|
331
333
|
let filesChanged = snapshot.files_changed || 0;
|
|
332
|
-
|
|
333
|
-
|
|
334
|
-
|
|
335
|
-
//
|
|
336
|
-
|
|
334
|
+
let insertions = snapshot.insertions || 0;
|
|
335
|
+
let deletions = snapshot.deletions || 0;
|
|
336
|
+
|
|
337
|
+
// Fallback for snapshots captured before nested-repo files were tracked:
|
|
338
|
+
// `session_changes` may be non-empty (with nested-repo entries) but
|
|
339
|
+
// `files_changed`/`insertions`/`deletions` are 0 because the capture
|
|
340
|
+
// loop never saw those files. Recompute from the blob store so the
|
|
341
|
+
// Restore Checkpoint UI shows the real numbers retroactively.
|
|
342
|
+
if (snapshot.session_changes && (insertions === 0 || deletions === 0)) {
|
|
337
343
|
try {
|
|
338
|
-
const changes = JSON.parse(snapshot.session_changes as string);
|
|
344
|
+
const changes = JSON.parse(snapshot.session_changes as string) as Record<string, { oldHash: string; newHash: string }>;
|
|
339
345
|
const changeCount = Object.keys(changes).length;
|
|
340
346
|
if (changeCount > 0) {
|
|
341
|
-
|
|
347
|
+
const previousSnapshot: Record<string, Buffer> = {};
|
|
348
|
+
const currentSnapshot: Record<string, Buffer> = {};
|
|
349
|
+
let allBlobsOk = true;
|
|
350
|
+
for (const [filepath, entry] of Object.entries(changes)) {
|
|
351
|
+
if (entry.oldHash) {
|
|
352
|
+
try { previousSnapshot[filepath] = await blobStore.readBlob(entry.oldHash); }
|
|
353
|
+
catch { allBlobsOk = false; break; }
|
|
354
|
+
}
|
|
355
|
+
if (entry.newHash) {
|
|
356
|
+
try { currentSnapshot[filepath] = await blobStore.readBlob(entry.newHash); }
|
|
357
|
+
catch { allBlobsOk = false; break; }
|
|
358
|
+
}
|
|
359
|
+
}
|
|
360
|
+
if (allBlobsOk) {
|
|
361
|
+
const recomputed = calculateFileChangeStats(previousSnapshot, currentSnapshot);
|
|
362
|
+
filesChanged = recomputed.filesChanged;
|
|
363
|
+
insertions = recomputed.insertions;
|
|
364
|
+
deletions = recomputed.deletions;
|
|
365
|
+
}
|
|
342
366
|
}
|
|
343
|
-
} catch { /* use
|
|
367
|
+
} catch { /* use stored values */ }
|
|
344
368
|
}
|
|
345
369
|
|
|
346
370
|
return { filesChanged, insertions, deletions };
|
package/backend/utils/paths.ts
CHANGED
|
@@ -10,6 +10,22 @@ export function getClopenDir(): string {
|
|
|
10
10
|
return join(homedir(), process.env.NODE_ENV === 'development' ? '.clopen-dev' : '.clopen');
|
|
11
11
|
}
|
|
12
12
|
|
|
13
|
+
/**
|
|
14
|
+
* Returns the isolated per-engine config/home directory under
|
|
15
|
+
* `{clopenDir}/engine/{engine}/user/`.
|
|
16
|
+
*
|
|
17
|
+
* Every engine's runtime state (credentials, sessions, logs) is redirected
|
|
18
|
+
* here via that engine's home/config env var (CLAUDE_CONFIG_DIR, CODEX_HOME,
|
|
19
|
+
* COPILOT_HOME, QWEN_RUNTIME_DIR, XDG_* for OpenCode) so Clopen never mixes
|
|
20
|
+
* its data with the user's own global CLI usage (~/.codex, ~/.copilot, …).
|
|
21
|
+
*
|
|
22
|
+
* Grouped under an `engine/` parent so all AI-engine state sits in one place,
|
|
23
|
+
* separate from Clopen's other data dirs (snapshots, etc.).
|
|
24
|
+
*/
|
|
25
|
+
export function getEngineUserConfigDir(engine: string): string {
|
|
26
|
+
return join(getClopenDir(), 'engine', engine, 'user');
|
|
27
|
+
}
|
|
28
|
+
|
|
13
29
|
/**
|
|
14
30
|
* Resolve a path to an OS-native absolute path.
|
|
15
31
|
* On Windows: resolves relative paths, prepends drive letter, converts to backslashes.
|
|
@@ -46,6 +46,7 @@ import {
|
|
|
46
46
|
applyAccountAuth,
|
|
47
47
|
readAuthJson,
|
|
48
48
|
serializeCodexCredential,
|
|
49
|
+
getCodexHomeDir,
|
|
49
50
|
} from '../../../engine/adapters/codex/credential';
|
|
50
51
|
import { resolveBinaryWithRefresh } from '../../../utils/cli';
|
|
51
52
|
import { getCleanSpawnEnv } from '../../../utils/env';
|
|
@@ -324,6 +325,8 @@ export const codexAccountsHandler = createRouter()
|
|
|
324
325
|
// server. (`BROWSER=false` is honoured by most OS open helpers.)
|
|
325
326
|
const ptyEnv = getCleanSpawnEnv();
|
|
326
327
|
ptyEnv['BROWSER'] = 'false';
|
|
328
|
+
// Write auth.json into Clopen's isolated Codex home, not ~/.codex.
|
|
329
|
+
ptyEnv['CODEX_HOME'] = getCodexHomeDir();
|
|
327
330
|
|
|
328
331
|
let pty: ReturnType<typeof spawn>;
|
|
329
332
|
try {
|
package/backend/ws/files/read.ts
CHANGED
|
@@ -19,6 +19,7 @@ import {
|
|
|
19
19
|
} from '../../files/file-reading';
|
|
20
20
|
import { handlePathBrowsing } from '../../files/path-browsing';
|
|
21
21
|
import { gitService } from '../../git/git-service';
|
|
22
|
+
import { findRepoForFile } from '../../snapshot/gitignore';
|
|
22
23
|
import { projectQueries } from '../../database/queries/project-queries';
|
|
23
24
|
import { isAbsolute, relative } from 'node:path';
|
|
24
25
|
import { requireProjectAccess } from '../access';
|
|
@@ -207,7 +208,15 @@ export const readHandler = createRouter()
|
|
|
207
208
|
// Normalize path separators for git
|
|
208
209
|
relativePath = relativePath.replace(/\\/g, '/');
|
|
209
210
|
|
|
210
|
-
|
|
211
|
+
// Route to the nested repo that owns the file (if any) — the outer
|
|
212
|
+
// repo's `git show HEAD:file` returns nothing for files tracked
|
|
213
|
+
// inside a nested repo and gitignored by the parent, so the editor
|
|
214
|
+
// gutter diff would silently go empty for subrepo files without this.
|
|
215
|
+
const repo = await findRepoForFile(project.path, relativePath);
|
|
216
|
+
const cwd = repo?.repoPath ?? project.path;
|
|
217
|
+
const gitFilePath = repo?.relativeFilePath ?? relativePath;
|
|
218
|
+
|
|
219
|
+
const content = await gitService.getFileAtRef(cwd, ref, gitFilePath);
|
|
211
220
|
return { content, ref };
|
|
212
221
|
})
|
|
213
222
|
|
|
@@ -112,6 +112,20 @@ Rules:
|
|
|
112
112
|
- Do not include ticket IDs unless they appear in the diff`;
|
|
113
113
|
}
|
|
114
114
|
|
|
115
|
+
import path from 'node:path';
|
|
116
|
+
|
|
117
|
+
function resolveRepoCwd(projectPath: string, repoPath: string | undefined): string {
|
|
118
|
+
if (!repoPath) return projectPath;
|
|
119
|
+
const resolved = path.resolve(repoPath);
|
|
120
|
+
const projectRoot = path.resolve(projectPath);
|
|
121
|
+
const sep = path.sep;
|
|
122
|
+
if (resolved !== projectRoot && !resolved.startsWith(projectRoot + sep)) {
|
|
123
|
+
debug.warn('git', `Rejected nested repoPath outside project: ${resolved}`);
|
|
124
|
+
return projectPath;
|
|
125
|
+
}
|
|
126
|
+
return resolved;
|
|
127
|
+
}
|
|
128
|
+
|
|
115
129
|
export const branchNameHandler = createRouter()
|
|
116
130
|
.http('git:generate-branch-name', {
|
|
117
131
|
data: t.Object({
|
|
@@ -121,15 +135,17 @@ export const branchNameHandler = createRouter()
|
|
|
121
135
|
modelId: t.String(),
|
|
122
136
|
branchSeparator: t.Optional(t.String()),
|
|
123
137
|
maxWords: t.Optional(t.Number()),
|
|
124
|
-
customPrompt: t.Optional(t.String())
|
|
138
|
+
customPrompt: t.Optional(t.String()),
|
|
139
|
+
repoPath: t.Optional(t.String())
|
|
125
140
|
}),
|
|
126
141
|
response: t.Object({
|
|
127
142
|
branchName: t.String()
|
|
128
143
|
})
|
|
129
144
|
}, async ({ data, conn }) => {
|
|
130
145
|
const project = requireProjectAccess(conn, data.projectId);
|
|
146
|
+
const cwd = resolveRepoCwd(project.path, data.repoPath);
|
|
131
147
|
|
|
132
|
-
const diffResult = await execGit(['diff', '--cached'],
|
|
148
|
+
const diffResult = await execGit(['diff', '--cached'], cwd);
|
|
133
149
|
const rawDiff = diffResult.stdout;
|
|
134
150
|
|
|
135
151
|
if (!rawDiff.trim()) {
|
|
@@ -143,7 +159,7 @@ export const branchNameHandler = createRouter()
|
|
|
143
159
|
throw new Error(`Engine "${engineType}" does not support structured generation`);
|
|
144
160
|
}
|
|
145
161
|
|
|
146
|
-
const branchResult = await execGit(['branch', '--show-current'],
|
|
162
|
+
const branchResult = await execGit(['branch', '--show-current'], cwd);
|
|
147
163
|
const currentBranch = branchResult.stdout.trim();
|
|
148
164
|
const prefix = getBranchPrefix(currentBranch);
|
|
149
165
|
const separator = normalizeBranchSeparator(data.branchSeparator);
|
|
@@ -158,7 +174,7 @@ export const branchNameHandler = createRouter()
|
|
|
158
174
|
providerSlug: data.providerSlug,
|
|
159
175
|
modelId: data.modelId,
|
|
160
176
|
schema: BRANCH_NAME_SCHEMA,
|
|
161
|
-
projectPath:
|
|
177
|
+
projectPath: cwd
|
|
162
178
|
});
|
|
163
179
|
|
|
164
180
|
const description = sanitizeBranchDescription(result.description, data.maxWords ?? MAX_BRANCH_DESCRIPTION_WORDS);
|
package/backend/ws/git/branch.ts
CHANGED
|
@@ -3,9 +3,11 @@
|
|
|
3
3
|
*/
|
|
4
4
|
|
|
5
5
|
import { t } from 'elysia';
|
|
6
|
+
import path from 'node:path';
|
|
6
7
|
import { createRouter } from '$shared/utils/ws-server';
|
|
7
8
|
import { gitService } from '../../git/git-service';
|
|
8
9
|
import { requireProjectAccess } from '../access';
|
|
10
|
+
import { debug } from '$shared/utils/logger';
|
|
9
11
|
|
|
10
12
|
const BranchSchema = t.Object({
|
|
11
13
|
name: t.String(),
|
|
@@ -17,6 +19,42 @@ const BranchSchema = t.Object({
|
|
|
17
19
|
lastCommit: t.Optional(t.String())
|
|
18
20
|
});
|
|
19
21
|
|
|
22
|
+
/** Subset of GitNestedRepoInfo we expose to the client (mirrors shared type). */
|
|
23
|
+
const NestedRepoInfoSchema = t.Object({
|
|
24
|
+
path: t.String(),
|
|
25
|
+
relPath: t.String(),
|
|
26
|
+
isSubmodule: t.Boolean(),
|
|
27
|
+
info: t.Object({
|
|
28
|
+
current: t.String(),
|
|
29
|
+
local: t.Array(BranchSchema),
|
|
30
|
+
remote: t.Array(BranchSchema),
|
|
31
|
+
ahead: t.Number(),
|
|
32
|
+
behind: t.Number(),
|
|
33
|
+
detached: t.Optional(t.Boolean()),
|
|
34
|
+
operation: t.Optional(t.Union([t.String(), t.Null()]))
|
|
35
|
+
}),
|
|
36
|
+
error: t.Optional(t.String())
|
|
37
|
+
});
|
|
38
|
+
|
|
39
|
+
/**
|
|
40
|
+
* Resolve the working directory for a git operation. Defaults to the project
|
|
41
|
+
* root; when `repoPath` is provided it must already be inside the project
|
|
42
|
+
* (these paths come from the backend's own `getBranches` response, but we
|
|
43
|
+
* re-validate to defend against a tampered/mismatched client).
|
|
44
|
+
*/
|
|
45
|
+
function resolveRepoCwd(projectPath: string, repoPath: string | undefined): string {
|
|
46
|
+
if (!repoPath) return projectPath;
|
|
47
|
+
const resolved = path.resolve(repoPath);
|
|
48
|
+
const projectRoot = path.resolve(projectPath);
|
|
49
|
+
const sep = path.sep;
|
|
50
|
+
// Must equal project root or live strictly inside it — no `..` escape.
|
|
51
|
+
if (resolved !== projectRoot && !resolved.startsWith(projectRoot + sep)) {
|
|
52
|
+
debug.warn('git', `Rejected nested repoPath outside project: ${resolved}`);
|
|
53
|
+
return projectPath;
|
|
54
|
+
}
|
|
55
|
+
return resolved;
|
|
56
|
+
}
|
|
57
|
+
|
|
20
58
|
export const branchHandler = createRouter()
|
|
21
59
|
.http('git:branches', {
|
|
22
60
|
data: t.Object({
|
|
@@ -28,7 +66,10 @@ export const branchHandler = createRouter()
|
|
|
28
66
|
local: t.Array(BranchSchema),
|
|
29
67
|
remote: t.Array(BranchSchema),
|
|
30
68
|
ahead: t.Number(),
|
|
31
|
-
behind: t.Number()
|
|
69
|
+
behind: t.Number(),
|
|
70
|
+
detached: t.Optional(t.Boolean()),
|
|
71
|
+
operation: t.Optional(t.Union([t.String(), t.Null()])),
|
|
72
|
+
nested: t.Optional(t.Array(NestedRepoInfoSchema))
|
|
32
73
|
})
|
|
33
74
|
}, async ({ data, conn }) => {
|
|
34
75
|
const project = requireProjectAccess(conn, data.projectId);
|
|
@@ -39,36 +80,45 @@ export const branchHandler = createRouter()
|
|
|
39
80
|
data: t.Object({
|
|
40
81
|
projectId: t.String(),
|
|
41
82
|
name: t.String({ minLength: 1 }),
|
|
42
|
-
startPoint: t.Optional(t.String())
|
|
83
|
+
startPoint: t.Optional(t.String()),
|
|
84
|
+
/** Absolute path of a nested repo inside the project (e.g. "packages/web").
|
|
85
|
+
* Defaults to the outer repo when omitted. */
|
|
86
|
+
repoPath: t.Optional(t.String())
|
|
43
87
|
}),
|
|
44
88
|
response: t.Object({ ok: t.Boolean() })
|
|
45
89
|
}, async ({ data, conn }) => {
|
|
46
90
|
const project = requireProjectAccess(conn, data.projectId);
|
|
47
|
-
|
|
91
|
+
const cwd = resolveRepoCwd(project.path, data.repoPath);
|
|
92
|
+
await gitService.createBranch(cwd, data.name, data.startPoint);
|
|
48
93
|
return { ok: true };
|
|
49
94
|
})
|
|
50
95
|
|
|
51
96
|
.http('git:switch-branch', {
|
|
52
97
|
data: t.Object({
|
|
53
98
|
projectId: t.String(),
|
|
54
|
-
name: t.String()
|
|
99
|
+
name: t.String(),
|
|
100
|
+
/** Absolute path of a nested repo inside the project. */
|
|
101
|
+
repoPath: t.Optional(t.String())
|
|
55
102
|
}),
|
|
56
103
|
response: t.Object({ ok: t.Boolean() })
|
|
57
104
|
}, async ({ data, conn }) => {
|
|
58
105
|
const project = requireProjectAccess(conn, data.projectId);
|
|
59
|
-
|
|
106
|
+
const cwd = resolveRepoCwd(project.path, data.repoPath);
|
|
107
|
+
await gitService.switchBranch(cwd, data.name);
|
|
60
108
|
return { ok: true };
|
|
61
109
|
})
|
|
62
110
|
|
|
63
111
|
.http('git:checkout-commit', {
|
|
64
112
|
data: t.Object({
|
|
65
113
|
projectId: t.String(),
|
|
66
|
-
commitHash: t.String()
|
|
114
|
+
commitHash: t.String(),
|
|
115
|
+
repoPath: t.Optional(t.String())
|
|
67
116
|
}),
|
|
68
117
|
response: t.Object({ ok: t.Boolean() })
|
|
69
118
|
}, async ({ data, conn }) => {
|
|
70
119
|
const project = requireProjectAccess(conn, data.projectId);
|
|
71
|
-
|
|
120
|
+
const cwd = resolveRepoCwd(project.path, data.repoPath);
|
|
121
|
+
await gitService.checkoutCommit(cwd, data.commitHash);
|
|
72
122
|
return { ok: true };
|
|
73
123
|
})
|
|
74
124
|
|
|
@@ -76,12 +126,15 @@ export const branchHandler = createRouter()
|
|
|
76
126
|
data: t.Object({
|
|
77
127
|
projectId: t.String(),
|
|
78
128
|
name: t.String(),
|
|
79
|
-
force: t.Optional(t.Boolean())
|
|
129
|
+
force: t.Optional(t.Boolean()),
|
|
130
|
+
/** Absolute path of a nested repo inside the project. */
|
|
131
|
+
repoPath: t.Optional(t.String())
|
|
80
132
|
}),
|
|
81
133
|
response: t.Object({ ok: t.Boolean() })
|
|
82
134
|
}, async ({ data, conn }) => {
|
|
83
135
|
const project = requireProjectAccess(conn, data.projectId);
|
|
84
|
-
|
|
136
|
+
const cwd = resolveRepoCwd(project.path, data.repoPath);
|
|
137
|
+
await gitService.deleteBranch(cwd, data.name, data.force);
|
|
85
138
|
return { ok: true };
|
|
86
139
|
})
|
|
87
140
|
|
|
@@ -102,7 +155,8 @@ export const branchHandler = createRouter()
|
|
|
102
155
|
data: t.Object({
|
|
103
156
|
projectId: t.String(),
|
|
104
157
|
branchName: t.String(),
|
|
105
|
-
noFastForward: t.Optional(t.Boolean())
|
|
158
|
+
noFastForward: t.Optional(t.Boolean()),
|
|
159
|
+
repoPath: t.Optional(t.String())
|
|
106
160
|
}),
|
|
107
161
|
response: t.Object({
|
|
108
162
|
success: t.Boolean(),
|
|
@@ -110,5 +164,6 @@ export const branchHandler = createRouter()
|
|
|
110
164
|
})
|
|
111
165
|
}, async ({ data, conn }) => {
|
|
112
166
|
const project = requireProjectAccess(conn, data.projectId);
|
|
113
|
-
|
|
167
|
+
const cwd = resolveRepoCwd(project.path, data.repoPath);
|
|
168
|
+
return await gitService.mergeBranch(cwd, data.branchName, data.noFastForward ?? false);
|
|
114
169
|
});
|
|
@@ -43,6 +43,19 @@ const COMMIT_MESSAGE_SCHEMA = {
|
|
|
43
43
|
},
|
|
44
44
|
required: ['type', 'scope', 'subject', 'body']
|
|
45
45
|
};
|
|
46
|
+
import path from 'node:path';
|
|
47
|
+
|
|
48
|
+
function resolveRepoCwd(projectPath: string, repoPath: string | undefined): string {
|
|
49
|
+
if (!repoPath) return projectPath;
|
|
50
|
+
const resolved = path.resolve(repoPath);
|
|
51
|
+
const projectRoot = path.resolve(projectPath);
|
|
52
|
+
const sep = path.sep;
|
|
53
|
+
if (resolved !== projectRoot && !resolved.startsWith(projectRoot + sep)) {
|
|
54
|
+
debug.warn('git', `Rejected nested repoPath outside project: ${resolved}`);
|
|
55
|
+
return projectPath;
|
|
56
|
+
}
|
|
57
|
+
return resolved;
|
|
58
|
+
}
|
|
46
59
|
|
|
47
60
|
export const commitMessageHandler = createRouter()
|
|
48
61
|
.http('git:generate-commit-message', {
|
|
@@ -52,16 +65,18 @@ export const commitMessageHandler = createRouter()
|
|
|
52
65
|
providerSlug: t.String(),
|
|
53
66
|
modelId: t.String(),
|
|
54
67
|
format: t.Union([t.Literal('single-line'), t.Literal('multi-line')]),
|
|
55
|
-
customPrompt: t.Optional(t.String())
|
|
68
|
+
customPrompt: t.Optional(t.String()),
|
|
69
|
+
repoPath: t.Optional(t.String())
|
|
56
70
|
}),
|
|
57
71
|
response: t.Object({
|
|
58
72
|
message: t.String()
|
|
59
73
|
})
|
|
60
74
|
}, async ({ data, conn }) => {
|
|
61
75
|
const project = requireProjectAccess(conn, data.projectId);
|
|
76
|
+
const cwd = resolveRepoCwd(project.path, data.repoPath);
|
|
62
77
|
|
|
63
78
|
// Get raw staged diff text
|
|
64
|
-
const diffResult = await execGit(['diff', '--cached'],
|
|
79
|
+
const diffResult = await execGit(['diff', '--cached'], cwd);
|
|
65
80
|
const rawDiff = diffResult.stdout;
|
|
66
81
|
|
|
67
82
|
if (!rawDiff.trim()) {
|
|
@@ -102,7 +117,7 @@ ${rawDiff}`;
|
|
|
102
117
|
providerSlug: data.providerSlug,
|
|
103
118
|
modelId: data.modelId,
|
|
104
119
|
schema: COMMIT_MESSAGE_SCHEMA,
|
|
105
|
-
projectPath:
|
|
120
|
+
projectPath: cwd
|
|
106
121
|
});
|
|
107
122
|
|
|
108
123
|
// Format structured output into conventional commit string
|