@myrialabs/clopen 0.3.12 → 0.3.14
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/backend/auth/session-invalidation.test.ts +45 -1
- package/backend/auth/session-invalidation.ts +35 -1
- package/backend/chat/stream-manager.ts +15 -0
- package/backend/database/migrations/037_repair_auth_audit_log_schema.ts +38 -0
- package/backend/database/migrations/index.ts +7 -0
- package/backend/engine/README.md +16 -1597
- package/backend/engine/adapters/claude/message-converter.ts +34 -7
- package/backend/engine/adapters/claude/models.ts +174 -2
- package/backend/engine/adapters/codex/stream.ts +54 -1
- package/backend/engine/adapters/copilot/stream.ts +79 -19
- package/backend/engine/adapters/opencode/stream.ts +23 -23
- package/backend/engine/adapters/qwen/stream.ts +83 -1
- package/backend/engine/docs/adapter-contract.md +183 -0
- package/backend/engine/docs/adding-an-engine.md +150 -0
- package/backend/engine/docs/database.md +123 -0
- package/backend/engine/docs/frontend-and-chat.md +176 -0
- package/backend/engine/docs/lessons-learned.md +715 -0
- package/backend/engine/docs/quick-reference.md +36 -0
- package/backend/engine/docs/system-tools.md +82 -0
- package/backend/engine/docs/websocket-routes.md +240 -0
- package/backend/engine/structured-helpers.ts +59 -0
- package/backend/files/file-archive.test.ts +131 -0
- package/backend/files/file-archive.ts +252 -0
- package/backend/files/file-operations.ts +39 -66
- package/backend/files/file-size-limit.test.ts +71 -15
- package/backend/files/file-size-limit.ts +36 -8
- package/backend/files/file-watcher.ts +117 -3
- package/backend/http/files-upload.ts +168 -0
- package/backend/index.ts +8 -0
- package/backend/mcp/servers/helper.ts +5 -1
- package/backend/projects/project-cleanup.ts +1 -1
- package/backend/terminal/pty-session-manager.ts +3 -0
- package/backend/terminal/stream-manager.ts +1 -1
- package/backend/utils/ws.ts +9 -0
- package/backend/ws/auth/users.ts +1 -1
- package/backend/ws/chat/stream.ts +46 -38
- package/backend/ws/files/path-access.test.ts +166 -0
- package/backend/ws/files/path-access.ts +56 -22
- package/backend/ws/files/read.ts +5 -5
- package/backend/ws/files/watch.ts +48 -19
- package/backend/ws/files/write.ts +53 -27
- package/backend/ws/git/commit-message.ts +12 -5
- package/backend/ws/terminal/session.ts +1 -0
- package/dist/assets/main-BqiSLc8F.css +1 -0
- package/dist/assets/{main-HIDO3ugS.js → main-C37-Y8f-.js} +230 -229
- package/dist/index.html +2 -2
- package/package.json +8 -7
- package/shared/types/git.ts +4 -2
- package/shared/types/stores/settings.ts +2 -0
- package/shared/types/unified/index.ts +6 -0
- package/shared/types/unified/stream.ts +15 -1
- package/shared/types/unified/tool.ts +41 -1
- package/shared/utils/ws-server.ts +6 -0
- package/backend/engine/adapters/copilot/session-fork.ts +0 -111
- package/dist/assets/main-C2JiGYnx.css +0 -1
|
@@ -0,0 +1,166 @@
|
|
|
1
|
+
import { describe, expect, test, mock, beforeEach, afterEach } from 'bun:test';
|
|
2
|
+
import { mkdir, symlink, writeFile, rm, realpath } from 'node:fs/promises';
|
|
3
|
+
import { join } from 'node:path';
|
|
4
|
+
import { tmpdir, platform } from 'node:os';
|
|
5
|
+
import { randomUUID } from 'node:crypto';
|
|
6
|
+
|
|
7
|
+
const isWindows = platform() === 'win32';
|
|
8
|
+
|
|
9
|
+
// Symlinks on Windows require admin privileges. Skip symlink-specific tests
|
|
10
|
+
// when running without elevated permissions.
|
|
11
|
+
async function canCreateSymlinks(): Promise<boolean> {
|
|
12
|
+
const testPath = join(tmpdir(), `symlink-test-${randomUUID()}`);
|
|
13
|
+
const targetPath = join(tmpdir(), `symlink-target-${randomUUID()}`);
|
|
14
|
+
try {
|
|
15
|
+
await mkdir(targetPath, { recursive: true });
|
|
16
|
+
await symlink(targetPath, testPath, 'dir');
|
|
17
|
+
await rm(testPath);
|
|
18
|
+
await rm(targetPath, { recursive: true });
|
|
19
|
+
return true;
|
|
20
|
+
} catch {
|
|
21
|
+
return false;
|
|
22
|
+
}
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
// Mock dependencies before importing the module under test
|
|
26
|
+
const mockGetAllForUser = mock(() => [] as Array<{ id: string; path: string }>);
|
|
27
|
+
const mockGetAll = mock(() => [] as Array<{ id: string; path: string }>);
|
|
28
|
+
const mockUserHasProject = mock(() => false);
|
|
29
|
+
const mockGetByPath = mock(() => null as { id: string; path: string } | null);
|
|
30
|
+
|
|
31
|
+
mock.module('../../database/queries/project-queries', () => ({
|
|
32
|
+
projectQueries: {
|
|
33
|
+
getAllForUser: mockGetAllForUser,
|
|
34
|
+
getAll: mockGetAll,
|
|
35
|
+
userHasProject: mockUserHasProject,
|
|
36
|
+
getByPath: mockGetByPath,
|
|
37
|
+
}
|
|
38
|
+
}));
|
|
39
|
+
|
|
40
|
+
const mockGetRole = mock((): 'admin' | 'member' => 'member');
|
|
41
|
+
const mockGetUserId = mock(() => 'user-1');
|
|
42
|
+
|
|
43
|
+
mock.module('$backend/utils/ws', () => ({
|
|
44
|
+
ws: {
|
|
45
|
+
getRole: mockGetRole,
|
|
46
|
+
getUserId: mockGetUserId,
|
|
47
|
+
}
|
|
48
|
+
}));
|
|
49
|
+
|
|
50
|
+
mock.module('../access', () => ({
|
|
51
|
+
requireProjectAccess: mock(() => ({ id: 'proj-1', path: '' })),
|
|
52
|
+
}));
|
|
53
|
+
|
|
54
|
+
// Import after mocking
|
|
55
|
+
const { requireFilePathAccess, requireSharedFilePathAccess } = await import('./path-access');
|
|
56
|
+
|
|
57
|
+
let testDir: string;
|
|
58
|
+
|
|
59
|
+
describe('path-access symlink resolution', () => {
|
|
60
|
+
beforeEach(async () => {
|
|
61
|
+
const rawTestDir = join(tmpdir(), `clopen-path-access-test-${randomUUID()}`);
|
|
62
|
+
await mkdir(rawTestDir, { recursive: true });
|
|
63
|
+
// Canonicalize so test expectations match what resolveRealPath returns.
|
|
64
|
+
// macOS tmpdir lives under /var → /private/var, which realpath follows.
|
|
65
|
+
testDir = await realpath(rawTestDir);
|
|
66
|
+
mockGetRole.mockImplementation(() => 'member');
|
|
67
|
+
mockGetUserId.mockImplementation(() => 'user-1');
|
|
68
|
+
});
|
|
69
|
+
|
|
70
|
+
afterEach(async () => {
|
|
71
|
+
await rm(testDir, { recursive: true, force: true });
|
|
72
|
+
});
|
|
73
|
+
|
|
74
|
+
test('blocks read through symlink to external directory', async () => {
|
|
75
|
+
if (!(await canCreateSymlinks())) {
|
|
76
|
+
console.log('SKIP: symlink creation requires admin on this platform');
|
|
77
|
+
return;
|
|
78
|
+
}
|
|
79
|
+
// Setup: project at testDir/project, symlink pointing to testDir/external
|
|
80
|
+
const projectPath = join(testDir, 'project');
|
|
81
|
+
const externalPath = join(testDir, 'external');
|
|
82
|
+
const symlinkPath = join(projectPath, 'escape');
|
|
83
|
+
|
|
84
|
+
await mkdir(projectPath, { recursive: true });
|
|
85
|
+
await mkdir(externalPath, { recursive: true });
|
|
86
|
+
await writeFile(join(externalPath, 'secret.txt'), 'confidential');
|
|
87
|
+
await symlink(externalPath, symlinkPath, 'dir');
|
|
88
|
+
|
|
89
|
+
mockGetAllForUser.mockReturnValue([{ id: 'proj-1', path: projectPath }]);
|
|
90
|
+
|
|
91
|
+
// Attempt to access file through symlink
|
|
92
|
+
const targetPath = join(symlinkPath, 'secret.txt');
|
|
93
|
+
await expect(requireFilePathAccess({} as any, targetPath)).rejects.toThrow('Access denied');
|
|
94
|
+
});
|
|
95
|
+
|
|
96
|
+
test('blocks write through symlink to external directory for non-existing leaf', async () => {
|
|
97
|
+
if (!(await canCreateSymlinks())) {
|
|
98
|
+
console.log('SKIP: symlink creation requires admin on this platform');
|
|
99
|
+
return;
|
|
100
|
+
}
|
|
101
|
+
// Setup: project at testDir/project, symlink pointing to testDir/external
|
|
102
|
+
const projectPath = join(testDir, 'project');
|
|
103
|
+
const externalPath = join(testDir, 'external');
|
|
104
|
+
const symlinkPath = join(projectPath, 'escape');
|
|
105
|
+
|
|
106
|
+
await mkdir(projectPath, { recursive: true });
|
|
107
|
+
await mkdir(externalPath, { recursive: true });
|
|
108
|
+
await symlink(externalPath, symlinkPath, 'dir');
|
|
109
|
+
|
|
110
|
+
mockGetAllForUser.mockReturnValue([{ id: 'proj-1', path: projectPath }]);
|
|
111
|
+
|
|
112
|
+
// Attempt to write to a non-existing file through symlink
|
|
113
|
+
// This is the key regression: realpath throws for non-existing paths,
|
|
114
|
+
// but resolveRealPath should still resolve the symlinked parent
|
|
115
|
+
const targetPath = join(symlinkPath, 'new-file.txt');
|
|
116
|
+
await expect(requireFilePathAccess({} as any, targetPath)).rejects.toThrow('Access denied');
|
|
117
|
+
});
|
|
118
|
+
|
|
119
|
+
test('blocks rename of ancestor path containing another user project', async () => {
|
|
120
|
+
// Setup: user-1 tries to rename /parent, but user-2 has project at /parent/child
|
|
121
|
+
const parentPath = join(testDir, 'parent');
|
|
122
|
+
const childProjectPath = join(parentPath, 'child-project');
|
|
123
|
+
|
|
124
|
+
await mkdir(childProjectPath, { recursive: true });
|
|
125
|
+
|
|
126
|
+
mockGetAll.mockReturnValue([{ id: 'proj-2', path: childProjectPath }]);
|
|
127
|
+
mockUserHasProject.mockReturnValue(false);
|
|
128
|
+
|
|
129
|
+
// user-1 attempts to rename the parent directory
|
|
130
|
+
await expect(requireSharedFilePathAccess({} as any, parentPath)).rejects.toThrow('Access denied');
|
|
131
|
+
});
|
|
132
|
+
|
|
133
|
+
test('allows access to files inside user own project', async () => {
|
|
134
|
+
const projectPath = join(testDir, 'project');
|
|
135
|
+
const filePath = join(projectPath, 'file.txt');
|
|
136
|
+
|
|
137
|
+
await mkdir(projectPath, { recursive: true });
|
|
138
|
+
await writeFile(filePath, 'content');
|
|
139
|
+
|
|
140
|
+
mockGetAllForUser.mockReturnValue([{ id: 'proj-1', path: projectPath }]);
|
|
141
|
+
|
|
142
|
+
const result = await requireFilePathAccess({} as any, filePath);
|
|
143
|
+
expect(result).toBe(filePath);
|
|
144
|
+
});
|
|
145
|
+
|
|
146
|
+
test('allows creating new files inside user own project', async () => {
|
|
147
|
+
const projectPath = join(testDir, 'project');
|
|
148
|
+
const newFilePath = join(projectPath, 'new-file.txt');
|
|
149
|
+
|
|
150
|
+
await mkdir(projectPath, { recursive: true });
|
|
151
|
+
|
|
152
|
+
mockGetAllForUser.mockReturnValue([{ id: 'proj-1', path: projectPath }]);
|
|
153
|
+
|
|
154
|
+
// new-file.txt doesn't exist yet, but should still be allowed
|
|
155
|
+
const result = await requireFilePathAccess({} as any, newFilePath);
|
|
156
|
+
expect(result).toBe(newFilePath);
|
|
157
|
+
});
|
|
158
|
+
|
|
159
|
+
test('admin bypasses all checks', async () => {
|
|
160
|
+
mockGetRole.mockImplementation(() => 'admin');
|
|
161
|
+
|
|
162
|
+
const anyPath = join(testDir, 'any', 'path', 'file.txt');
|
|
163
|
+
const result = await requireFilePathAccess({} as any, anyPath);
|
|
164
|
+
expect(result).toBe(anyPath);
|
|
165
|
+
});
|
|
166
|
+
});
|
|
@@ -1,4 +1,5 @@
|
|
|
1
|
-
import { isAbsolute, relative, resolve } from 'node:path';
|
|
1
|
+
import { basename, dirname, isAbsolute, join, relative, resolve } from 'node:path';
|
|
2
|
+
import { realpath } from 'node:fs/promises';
|
|
2
3
|
|
|
3
4
|
import { projectQueries } from '../../database/queries/project-queries';
|
|
4
5
|
import { ws } from '$backend/utils/ws';
|
|
@@ -6,9 +7,31 @@ import type { WSConnection } from '$shared/utils/ws-server';
|
|
|
6
7
|
import type { Project } from '$shared/types/database/schema';
|
|
7
8
|
import { requireProjectAccess } from '../access';
|
|
8
9
|
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
10
|
+
/**
|
|
11
|
+
* Resolve a path to its real (canonical) location, following symlinks.
|
|
12
|
+
* For non-existent paths, resolves the deepest existing ancestor and
|
|
13
|
+
* rejoins the unresolved tail so symlinked parent components are always
|
|
14
|
+
* followed even when the leaf doesn't exist yet.
|
|
15
|
+
*/
|
|
16
|
+
async function resolveRealPath(p: string): Promise<string> {
|
|
17
|
+
const abs = resolve(p);
|
|
18
|
+
try {
|
|
19
|
+
return await realpath(abs);
|
|
20
|
+
} catch {
|
|
21
|
+
const parent = dirname(abs);
|
|
22
|
+
if (parent === abs) return abs;
|
|
23
|
+
return join(await resolveRealPath(parent), basename(abs));
|
|
24
|
+
}
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
/**
|
|
28
|
+
* Check whether candidatePath is inside rootPath, resolving symlinks on both
|
|
29
|
+
* sides so that a symlink inside a project root cannot escape to an external
|
|
30
|
+
* target.
|
|
31
|
+
*/
|
|
32
|
+
async function isPathInside(rootPath: string, candidatePath: string): Promise<boolean> {
|
|
33
|
+
const root = await resolveRealPath(rootPath);
|
|
34
|
+
const candidate = await resolveRealPath(candidatePath);
|
|
12
35
|
const rel = relative(root, candidate);
|
|
13
36
|
return rel === '' || (!!rel && !rel.startsWith('..') && !isAbsolute(rel));
|
|
14
37
|
}
|
|
@@ -24,29 +47,40 @@ export function requireProjectPathAccess(conn: WSConnection, projectPath: string
|
|
|
24
47
|
return requireProjectAccess(conn, project.id);
|
|
25
48
|
}
|
|
26
49
|
|
|
27
|
-
export function requireFilePathAccess(conn: WSConnection, filePath: string): string {
|
|
28
|
-
|
|
29
|
-
|
|
50
|
+
export async function requireFilePathAccess(conn: WSConnection, filePath: string): Promise<string> {
|
|
51
|
+
return await requireFilePathAccessFor(filePath, ws.getRole(conn), ws.getUserId(conn));
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
/**
|
|
55
|
+
* Identity-based variant of {@link requireFilePathAccess} — usable from HTTP
|
|
56
|
+
* routes (which have no `WSConnection`) once the caller has resolved the user
|
|
57
|
+
* from a bearer token. Same access policy as the WS version.
|
|
58
|
+
*/
|
|
59
|
+
export async function requireFilePathAccessFor(filePath: string, role: string | null, userId: string | null): Promise<string> {
|
|
60
|
+
const normalizedPath = await resolveRealPath(filePath);
|
|
61
|
+
if (role === 'admin') {
|
|
30
62
|
return normalizedPath;
|
|
31
63
|
}
|
|
64
|
+
if (!userId) {
|
|
65
|
+
throw new Error('Access denied');
|
|
66
|
+
}
|
|
32
67
|
|
|
33
|
-
const userId = ws.getUserId(conn);
|
|
34
68
|
const projects = projectQueries.getAllForUser(userId);
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
69
|
+
for (const project of projects) {
|
|
70
|
+
if (await isPathInside(project.path, normalizedPath)) {
|
|
71
|
+
return normalizedPath;
|
|
72
|
+
}
|
|
39
73
|
}
|
|
40
74
|
|
|
41
|
-
|
|
75
|
+
throw new Error('Access denied');
|
|
42
76
|
}
|
|
43
77
|
|
|
44
78
|
// Picker-style guard: allow paths inside the user's accessible projects OR
|
|
45
79
|
// outside every registered project. Rejects only when the path lives inside
|
|
46
80
|
// another project the user cannot access. Used by FolderBrowser flows that
|
|
47
81
|
// operate around / before a project exists.
|
|
48
|
-
export function requireSharedFilePathAccess(conn: WSConnection, filePath: string): string {
|
|
49
|
-
const normalizedPath =
|
|
82
|
+
export async function requireSharedFilePathAccess(conn: WSConnection, filePath: string): Promise<string> {
|
|
83
|
+
const normalizedPath = await resolveRealPath(filePath);
|
|
50
84
|
if (ws.getRole(conn) === 'admin') {
|
|
51
85
|
return normalizedPath;
|
|
52
86
|
}
|
|
@@ -57,9 +91,9 @@ export function requireSharedFilePathAccess(conn: WSConnection, filePath: string
|
|
|
57
91
|
// Without this, renaming/deleting `/foo` would break a project rooted at
|
|
58
92
|
// `/foo/bar` even though `/foo` itself is not "inside" any project.
|
|
59
93
|
for (const project of allProjects) {
|
|
60
|
-
const projectRoot =
|
|
94
|
+
const projectRoot = await resolveRealPath(project.path);
|
|
61
95
|
if (projectRoot === normalizedPath) continue; // equality is handled below
|
|
62
|
-
if (!isPathInside(normalizedPath, project.path)) continue;
|
|
96
|
+
if (!(await isPathInside(normalizedPath, project.path))) continue;
|
|
63
97
|
if (!projectQueries.userHasProject(userId, project.id)) {
|
|
64
98
|
throw new Error('Access denied');
|
|
65
99
|
}
|
|
@@ -69,8 +103,8 @@ export function requireSharedFilePathAccess(conn: WSConnection, filePath: string
|
|
|
69
103
|
// roots resolve to the inner one, not whichever the DB returns first.
|
|
70
104
|
let containing: { id: string; path: string } | null = null;
|
|
71
105
|
for (const project of allProjects) {
|
|
72
|
-
if (!isPathInside(project.path, normalizedPath)) continue;
|
|
73
|
-
const projectRoot =
|
|
106
|
+
if (!(await isPathInside(project.path, normalizedPath))) continue;
|
|
107
|
+
const projectRoot = await resolveRealPath(project.path);
|
|
74
108
|
if (!containing || projectRoot.length > resolve(containing.path).length) {
|
|
75
109
|
containing = project;
|
|
76
110
|
}
|
|
@@ -87,13 +121,13 @@ export function requireSharedFilePathAccess(conn: WSConnection, filePath: string
|
|
|
87
121
|
return normalizedPath;
|
|
88
122
|
}
|
|
89
123
|
|
|
90
|
-
export function filterAccessibleExpandedPaths(rootPath: string, expandedPaths?: Set<string>): Set<string> | undefined {
|
|
124
|
+
export async function filterAccessibleExpandedPaths(rootPath: string, expandedPaths?: Set<string>): Promise<Set<string> | undefined> {
|
|
91
125
|
if (!expandedPaths) return undefined;
|
|
92
126
|
const filtered = new Set<string>();
|
|
93
127
|
|
|
94
128
|
for (const expandedPath of expandedPaths) {
|
|
95
|
-
if (isPathInside(rootPath, expandedPath)) {
|
|
96
|
-
filtered.add(
|
|
129
|
+
if (await isPathInside(rootPath, expandedPath)) {
|
|
130
|
+
filtered.add(await resolveRealPath(expandedPath));
|
|
97
131
|
}
|
|
98
132
|
}
|
|
99
133
|
|
package/backend/ws/files/read.ts
CHANGED
|
@@ -89,7 +89,7 @@ export const readHandler = createRouter()
|
|
|
89
89
|
.filter(Boolean);
|
|
90
90
|
expandedPaths = new Set(paths);
|
|
91
91
|
}
|
|
92
|
-
expandedPaths = filterAccessibleExpandedPaths(projectPath, expandedPaths);
|
|
92
|
+
expandedPaths = await filterAccessibleExpandedPaths(projectPath, expandedPaths);
|
|
93
93
|
|
|
94
94
|
const fileTree = await buildFileTree(projectPath, 3, 0, expandedPaths);
|
|
95
95
|
return fileTree as any;
|
|
@@ -132,7 +132,7 @@ export const readHandler = createRouter()
|
|
|
132
132
|
: (data.path === '.' || data.path === '' ? process.cwd() : data.path);
|
|
133
133
|
const guardedPath = data.path === 'drives'
|
|
134
134
|
? 'drives'
|
|
135
|
-
: requireSharedFilePathAccess(conn, requestedPath);
|
|
135
|
+
: await requireSharedFilePathAccess(conn, requestedPath);
|
|
136
136
|
const result = await handlePathBrowsing(guardedPath);
|
|
137
137
|
return result;
|
|
138
138
|
})
|
|
@@ -156,7 +156,7 @@ export const readHandler = createRouter()
|
|
|
156
156
|
)
|
|
157
157
|
)
|
|
158
158
|
}, async ({ data, conn }) => {
|
|
159
|
-
const dirPath = requireFilePathAccess(conn, data.dir_path);
|
|
159
|
+
const dirPath = await requireFilePathAccess(conn, data.dir_path);
|
|
160
160
|
const result = await listDirectoryContents(dirPath);
|
|
161
161
|
return result;
|
|
162
162
|
})
|
|
@@ -176,7 +176,7 @@ export const readHandler = createRouter()
|
|
|
176
176
|
error: t.Optional(t.String())
|
|
177
177
|
})
|
|
178
178
|
}, async ({ data, conn }) => {
|
|
179
|
-
const filePath = requireFilePathAccess(conn, data.file_path);
|
|
179
|
+
const filePath = await requireFilePathAccess(conn, data.file_path);
|
|
180
180
|
const result = await readFileContents(filePath);
|
|
181
181
|
return result;
|
|
182
182
|
})
|
|
@@ -221,7 +221,7 @@ export const readHandler = createRouter()
|
|
|
221
221
|
contentType: t.String()
|
|
222
222
|
})
|
|
223
223
|
}, async ({ data, conn }) => {
|
|
224
|
-
const path = requireFilePathAccess(conn, data.path);
|
|
224
|
+
const path = await requireFilePathAccess(conn, data.path);
|
|
225
225
|
|
|
226
226
|
// Read file as binary
|
|
227
227
|
const file = Bun.file(path);
|
|
@@ -15,6 +15,10 @@ import { ws } from '$backend/utils/ws';
|
|
|
15
15
|
import { projectQueries } from '../../database/queries/project-queries';
|
|
16
16
|
import { requireProjectAccess } from '../access';
|
|
17
17
|
|
|
18
|
+
// Connection ids that already have a disconnect cleanup registered, so we
|
|
19
|
+
// don't stack duplicate cleanups when a connection re-watches across switches.
|
|
20
|
+
const cleanupRegistered = new Set<string>();
|
|
21
|
+
|
|
18
22
|
export const watchHandler = createRouter()
|
|
19
23
|
// Start watching a project directory
|
|
20
24
|
.on('files:watch', {
|
|
@@ -30,19 +34,23 @@ export const watchHandler = createRouter()
|
|
|
30
34
|
const projectId = project.id;
|
|
31
35
|
|
|
32
36
|
try {
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
37
|
+
const connId = ws.getConnectionId(conn);
|
|
38
|
+
|
|
39
|
+
// Tear down the watcher automatically if the connection drops without
|
|
40
|
+
// sending an explicit unwatch (tab close, network loss).
|
|
41
|
+
if (connId && !cleanupRegistered.has(connId)) {
|
|
42
|
+
cleanupRegistered.add(connId);
|
|
43
|
+
ws.addCleanup(conn, () => {
|
|
44
|
+
cleanupRegistered.delete(connId);
|
|
45
|
+
fileWatcher.removeViewerFromAll(connId);
|
|
40
46
|
});
|
|
41
|
-
return;
|
|
42
47
|
}
|
|
43
48
|
|
|
44
|
-
//
|
|
45
|
-
|
|
49
|
+
// Register this connection as a viewer; starts the watcher if it is
|
|
50
|
+
// the first viewer, otherwise reuses the existing one.
|
|
51
|
+
const success = connId
|
|
52
|
+
? await fileWatcher.addViewer(connId, projectId, projectPath)
|
|
53
|
+
: await fileWatcher.startWatching(projectId, projectPath);
|
|
46
54
|
|
|
47
55
|
if (success) {
|
|
48
56
|
// Broadcast confirmation (frontend filters by projectId)
|
|
@@ -69,25 +77,46 @@ export const watchHandler = createRouter()
|
|
|
69
77
|
}
|
|
70
78
|
})
|
|
71
79
|
|
|
72
|
-
// Stop watching a project directory
|
|
80
|
+
// Stop watching a project directory.
|
|
81
|
+
// Resolves the target project from the explicit `projectPath` (the project
|
|
82
|
+
// being left) rather than the connection's current project context — by the
|
|
83
|
+
// time this fires the context has usually already advanced to the new
|
|
84
|
+
// project, so relying on it would unwatch the wrong one.
|
|
73
85
|
.on('files:unwatch', {
|
|
74
|
-
data: t.Object({
|
|
86
|
+
data: t.Object({
|
|
87
|
+
projectPath: t.Optional(t.String())
|
|
88
|
+
})
|
|
75
89
|
}, async ({ data, conn }) => {
|
|
76
|
-
|
|
90
|
+
let projectId: string;
|
|
91
|
+
if (data.projectPath) {
|
|
92
|
+
const project = projectQueries.getByPath(data.projectPath);
|
|
93
|
+
if (!project) return; // unknown path — nothing to unwatch
|
|
94
|
+
requireProjectAccess(conn, project.id);
|
|
95
|
+
projectId = project.id;
|
|
96
|
+
} else {
|
|
97
|
+
// Backwards-compatible fallback for callers that don't send a path.
|
|
98
|
+
projectId = ws.getProjectId(conn);
|
|
99
|
+
}
|
|
77
100
|
|
|
78
101
|
try {
|
|
79
|
-
const
|
|
102
|
+
const connId = ws.getConnectionId(conn);
|
|
103
|
+
if (connId) {
|
|
104
|
+
// Drops this viewer; watcher is stopped only when none remain.
|
|
105
|
+
fileWatcher.removeViewer(connId, projectId);
|
|
106
|
+
} else {
|
|
107
|
+
fileWatcher.stopWatching(projectId);
|
|
108
|
+
}
|
|
80
109
|
|
|
81
|
-
// Broadcast
|
|
110
|
+
// Broadcast the ACTUAL resulting state — other devices may still be
|
|
111
|
+
// viewing this project, in which case the watcher is still alive and
|
|
112
|
+
// they must not be told it stopped.
|
|
82
113
|
ws.emit.project(projectId, 'files:watching', {
|
|
83
114
|
projectId,
|
|
84
|
-
watching:
|
|
115
|
+
watching: fileWatcher.isWatching(projectId),
|
|
85
116
|
timestamp: Date.now()
|
|
86
117
|
});
|
|
87
118
|
|
|
88
|
-
|
|
89
|
-
debug.log('file', `Stopped watching project ${projectId}`);
|
|
90
|
-
}
|
|
119
|
+
debug.log('file', `Viewer left project ${projectId}`);
|
|
91
120
|
} catch (error) {
|
|
92
121
|
debug.error('file', 'Error stopping file watch:', error);
|
|
93
122
|
}
|
|
@@ -20,10 +20,9 @@ import {
|
|
|
20
20
|
createDirectoryOperation,
|
|
21
21
|
renameOperation,
|
|
22
22
|
duplicateOperation,
|
|
23
|
-
uploadFileOperation,
|
|
24
23
|
deleteOperation
|
|
25
24
|
} from '../../files/file-operations';
|
|
26
|
-
import {
|
|
25
|
+
import { createZipOperation, extractZipOperation } from '../../files/file-archive';
|
|
27
26
|
import { stat as fsStat, readdir as fsReaddir, rename as fsRename, access as fsAccess } from 'node:fs/promises';
|
|
28
27
|
import { requireFilePathAccess, requireSharedFilePathAccess } from './path-access';
|
|
29
28
|
|
|
@@ -32,7 +31,11 @@ export const writeHandler = createRouter()
|
|
|
32
31
|
.http('files:write-file', {
|
|
33
32
|
data: t.Object({
|
|
34
33
|
filePath: t.String(),
|
|
35
|
-
content: t.String()
|
|
34
|
+
content: t.String(),
|
|
35
|
+
// Optimistic concurrency token: the disk mtime the client's buffer is
|
|
36
|
+
// based on. When present and it no longer matches disk, the write is
|
|
37
|
+
// rejected instead of overwriting newer content. Omit to force-write.
|
|
38
|
+
baseModified: t.Optional(t.String())
|
|
36
39
|
}),
|
|
37
40
|
response: t.Object({
|
|
38
41
|
message: t.String(),
|
|
@@ -40,12 +43,12 @@ export const writeHandler = createRouter()
|
|
|
40
43
|
modified: t.String()
|
|
41
44
|
})
|
|
42
45
|
}, async ({ data, conn }) => {
|
|
43
|
-
const filePath = requireFilePathAccess(conn, data.filePath);
|
|
46
|
+
const filePath = await requireFilePathAccess(conn, data.filePath);
|
|
44
47
|
debug.log('file', 'Write file operation:', {
|
|
45
48
|
filePath,
|
|
46
49
|
contentLength: data.content.length
|
|
47
50
|
});
|
|
48
|
-
return await writeFileOperation(filePath, data.content);
|
|
51
|
+
return await writeFileOperation(filePath, data.content, data.baseModified);
|
|
49
52
|
})
|
|
50
53
|
|
|
51
54
|
// Create file operation
|
|
@@ -61,7 +64,7 @@ export const writeHandler = createRouter()
|
|
|
61
64
|
modified: t.String()
|
|
62
65
|
})
|
|
63
66
|
}, async ({ data, conn }) => {
|
|
64
|
-
const filePath = requireFilePathAccess(conn, data.filePath);
|
|
67
|
+
const filePath = await requireFilePathAccess(conn, data.filePath);
|
|
65
68
|
return await createFileOperation(filePath, data.content);
|
|
66
69
|
})
|
|
67
70
|
|
|
@@ -79,7 +82,7 @@ export const writeHandler = createRouter()
|
|
|
79
82
|
// Uses the shared guard so FolderBrowser can create candidate project
|
|
80
83
|
// folders outside any existing project, while still preventing writes
|
|
81
84
|
// inside another user's project.
|
|
82
|
-
const dirPath = requireSharedFilePathAccess(conn, data.dirPath);
|
|
85
|
+
const dirPath = await requireSharedFilePathAccess(conn, data.dirPath);
|
|
83
86
|
return await createDirectoryOperation(dirPath);
|
|
84
87
|
})
|
|
85
88
|
|
|
@@ -96,8 +99,8 @@ export const writeHandler = createRouter()
|
|
|
96
99
|
modified: t.String()
|
|
97
100
|
})
|
|
98
101
|
}, async ({ data, conn }) => {
|
|
99
|
-
const oldPath = requireFilePathAccess(conn, data.oldPath);
|
|
100
|
-
const newPath = requireFilePathAccess(conn, data.newPath);
|
|
102
|
+
const oldPath = await requireFilePathAccess(conn, data.oldPath);
|
|
103
|
+
const newPath = await requireFilePathAccess(conn, data.newPath);
|
|
101
104
|
return await renameOperation(oldPath, newPath);
|
|
102
105
|
})
|
|
103
106
|
|
|
@@ -115,21 +118,20 @@ export const writeHandler = createRouter()
|
|
|
115
118
|
modified: t.String()
|
|
116
119
|
})
|
|
117
120
|
}, async ({ data, conn }) => {
|
|
118
|
-
const sourcePath = requireFilePathAccess(conn, data.sourcePath);
|
|
119
|
-
const targetPath = requireFilePathAccess(conn, data.targetPath);
|
|
121
|
+
const sourcePath = await requireFilePathAccess(conn, data.sourcePath);
|
|
122
|
+
const targetPath = await requireFilePathAccess(conn, data.targetPath);
|
|
120
123
|
return await duplicateOperation(sourcePath, targetPath);
|
|
121
124
|
})
|
|
122
125
|
|
|
123
|
-
//
|
|
124
|
-
|
|
126
|
+
// Uploads moved to the HTTP route POST /api/files/upload — the WS path
|
|
127
|
+
// kept getting dropped by the Vite dev proxy with `write EPIPE` on
|
|
128
|
+
// sustained binary transfers (any chunk size). See backend/http/files-upload.ts.
|
|
129
|
+
|
|
130
|
+
// Zip operation — compress a set of files/directories into a single archive
|
|
131
|
+
.http('files:zip', {
|
|
125
132
|
data: t.Object({
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
name: t.String(),
|
|
129
|
-
type: t.String(),
|
|
130
|
-
size: t.Number(),
|
|
131
|
-
data: t.Uint8Array()
|
|
132
|
-
})
|
|
133
|
+
sourcePaths: t.Array(t.String()),
|
|
134
|
+
targetPath: t.String()
|
|
133
135
|
}),
|
|
134
136
|
response: t.Object({
|
|
135
137
|
message: t.String(),
|
|
@@ -138,9 +140,33 @@ export const writeHandler = createRouter()
|
|
|
138
140
|
modified: t.String()
|
|
139
141
|
})
|
|
140
142
|
}, async ({ data, conn }) => {
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
143
|
+
if (data.sourcePaths.length === 0) {
|
|
144
|
+
throw new Error('At least one source path is required');
|
|
145
|
+
}
|
|
146
|
+
const resolvedSources: string[] = [];
|
|
147
|
+
for (const p of data.sourcePaths) {
|
|
148
|
+
resolvedSources.push(await requireFilePathAccess(conn, p));
|
|
149
|
+
}
|
|
150
|
+
const resolvedTarget = await requireFilePathAccess(conn, data.targetPath);
|
|
151
|
+
return await createZipOperation(resolvedSources, resolvedTarget);
|
|
152
|
+
})
|
|
153
|
+
|
|
154
|
+
// Extract operation — extract a zip archive into a target directory
|
|
155
|
+
.http('files:extract', {
|
|
156
|
+
data: t.Object({
|
|
157
|
+
archivePath: t.String(),
|
|
158
|
+
targetDir: t.String()
|
|
159
|
+
}),
|
|
160
|
+
response: t.Object({
|
|
161
|
+
message: t.String(),
|
|
162
|
+
path: t.String(),
|
|
163
|
+
entries: t.Number(),
|
|
164
|
+
modified: t.String()
|
|
165
|
+
})
|
|
166
|
+
}, async ({ data, conn }) => {
|
|
167
|
+
const archivePath = await requireFilePathAccess(conn, data.archivePath);
|
|
168
|
+
const targetDir = await requireFilePathAccess(conn, data.targetDir);
|
|
169
|
+
return await extractZipOperation(archivePath, targetDir);
|
|
144
170
|
})
|
|
145
171
|
|
|
146
172
|
// Delete operation
|
|
@@ -154,7 +180,7 @@ export const writeHandler = createRouter()
|
|
|
154
180
|
path: t.String()
|
|
155
181
|
})
|
|
156
182
|
}, async ({ data, conn }) => {
|
|
157
|
-
const filePath = requireFilePathAccess(conn, data.filePath);
|
|
183
|
+
const filePath = await requireFilePathAccess(conn, data.filePath);
|
|
158
184
|
return await deleteOperation(filePath, data.force);
|
|
159
185
|
})
|
|
160
186
|
|
|
@@ -171,7 +197,7 @@ export const writeHandler = createRouter()
|
|
|
171
197
|
path: t.String()
|
|
172
198
|
})
|
|
173
199
|
}, async ({ data, conn }) => {
|
|
174
|
-
const dirPath = requireSharedFilePathAccess(conn, data.dirPath);
|
|
200
|
+
const dirPath = await requireSharedFilePathAccess(conn, data.dirPath);
|
|
175
201
|
|
|
176
202
|
const stats = await fsStat(dirPath);
|
|
177
203
|
if (!stats.isDirectory()) {
|
|
@@ -202,8 +228,8 @@ export const writeHandler = createRouter()
|
|
|
202
228
|
modified: t.String()
|
|
203
229
|
})
|
|
204
230
|
}, async ({ data, conn }) => {
|
|
205
|
-
const oldPath = requireSharedFilePathAccess(conn, data.oldPath);
|
|
206
|
-
const newPath = requireSharedFilePathAccess(conn, data.newPath);
|
|
231
|
+
const oldPath = await requireSharedFilePathAccess(conn, data.oldPath);
|
|
232
|
+
const newPath = await requireSharedFilePathAccess(conn, data.newPath);
|
|
207
233
|
|
|
208
234
|
let oldStats;
|
|
209
235
|
try {
|
|
@@ -13,8 +13,15 @@ import type { GeneratedCommitMessage } from '$shared/types/git';
|
|
|
13
13
|
import { debug } from '$shared/utils/logger';
|
|
14
14
|
import { requireProjectAccess } from '../access';
|
|
15
15
|
|
|
16
|
+
// Schema is shaped to satisfy OpenAI's strict structured-output mode (used by
|
|
17
|
+
// Codex via `outputSchema`): every object must declare `additionalProperties:
|
|
18
|
+
// false`, every property must appear in `required`, and optional fields are
|
|
19
|
+
// expressed as nullable type unions rather than being absent from `required`.
|
|
20
|
+
// Claude/OpenCode/Copilot/Qwen accept this same shape — strict is the lowest
|
|
21
|
+
// common denominator.
|
|
16
22
|
const COMMIT_MESSAGE_SCHEMA = {
|
|
17
23
|
type: 'object',
|
|
24
|
+
additionalProperties: false,
|
|
18
25
|
properties: {
|
|
19
26
|
type: {
|
|
20
27
|
type: 'string',
|
|
@@ -22,19 +29,19 @@ const COMMIT_MESSAGE_SCHEMA = {
|
|
|
22
29
|
description: 'The conventional commit type'
|
|
23
30
|
},
|
|
24
31
|
scope: {
|
|
25
|
-
type: 'string',
|
|
26
|
-
description: '
|
|
32
|
+
type: ['string', 'null'],
|
|
33
|
+
description: 'Scope of the change (e.g., component name, module). Null when no scope applies.'
|
|
27
34
|
},
|
|
28
35
|
subject: {
|
|
29
36
|
type: 'string',
|
|
30
37
|
description: 'Short imperative description, lowercase, no period, max 72 chars'
|
|
31
38
|
},
|
|
32
39
|
body: {
|
|
33
|
-
type: 'string',
|
|
34
|
-
description: '
|
|
40
|
+
type: ['string', 'null'],
|
|
41
|
+
description: 'Longer description explaining the why behind the change. Null for single-line commits.'
|
|
35
42
|
}
|
|
36
43
|
},
|
|
37
|
-
required: ['type', 'subject']
|
|
44
|
+
required: ['type', 'scope', 'subject', 'body']
|
|
38
45
|
};
|
|
39
46
|
|
|
40
47
|
export const commitMessageHandler = createRouter()
|