@mastra/factory 0.2.2-alpha.0 → 0.2.2-alpha.2
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/CHANGELOG.md +36 -0
- package/dist/auth.d.ts +2 -2
- package/dist/auth.d.ts.map +1 -1
- package/dist/auth.js +401 -314
- package/dist/auth.js.map +1 -1
- package/dist/capabilities/connection.js +1 -1
- package/dist/capabilities/intake.js +1 -1
- package/dist/capabilities/version-control.js +1 -1
- package/dist/factory.d.ts.map +1 -1
- package/dist/factory.js +441 -39455
- package/dist/factory.js.map +1 -1
- package/dist/index.js +2 -39466
- package/dist/integrations/base.js +1 -1
- package/dist/integrations/github/config.js +36 -22
- package/dist/integrations/github/config.js.map +1 -1
- package/dist/integrations/github/factory-session.js +33 -37
- package/dist/integrations/github/factory-session.js.map +1 -1
- package/dist/integrations/github/integration.js +1037 -3624
- package/dist/integrations/github/integration.js.map +1 -1
- package/dist/integrations/github/issue-triage.js +71 -71
- package/dist/integrations/github/issue-triage.js.map +1 -1
- package/dist/integrations/github/pat.js +47 -36
- package/dist/integrations/github/pat.js.map +1 -1
- package/dist/integrations/github/provenance.js +72 -101
- package/dist/integrations/github/provenance.js.map +1 -1
- package/dist/integrations/github/routes.d.ts.map +1 -1
- package/dist/integrations/github/routes.js +1272 -1833
- package/dist/integrations/github/routes.js.map +1 -1
- package/dist/integrations/github/rules.js +203 -471
- package/dist/integrations/github/rules.js.map +1 -1
- package/dist/integrations/github/sandbox.d.ts +2 -0
- package/dist/integrations/github/sandbox.d.ts.map +1 -1
- package/dist/integrations/github/sandbox.js +401 -326
- package/dist/integrations/github/sandbox.js.map +1 -1
- package/dist/integrations/github/session-subscriptions.js +165 -223
- package/dist/integrations/github/session-subscriptions.js.map +1 -1
- package/dist/integrations/github/subscriptions.js +43 -51
- package/dist/integrations/github/subscriptions.js.map +1 -1
- package/dist/integrations/github/token-refresh.js +15 -18
- package/dist/integrations/github/token-refresh.js.map +1 -1
- package/dist/integrations/github/webhook.js +359 -343
- package/dist/integrations/github/webhook.js.map +1 -1
- package/dist/integrations/linear/agent-tools.js +77 -100
- package/dist/integrations/linear/agent-tools.js.map +1 -1
- package/dist/integrations/linear/integration.js +611 -1309
- package/dist/integrations/linear/integration.js.map +1 -1
- package/dist/integrations/linear/routes.js +227 -230
- package/dist/integrations/linear/routes.js.map +1 -1
- package/dist/integrations/linear/rules.js +126 -346
- package/dist/integrations/linear/rules.js.map +1 -1
- package/dist/integrations/linear/storage.js +1 -1
- package/dist/integrations/platform/api-client.js +131 -135
- package/dist/integrations/platform/api-client.js.map +1 -1
- package/dist/integrations/platform/github/event-worker.js +275 -573
- package/dist/integrations/platform/github/event-worker.js.map +1 -1
- package/dist/integrations/platform/github/integration.d.ts.map +1 -1
- package/dist/integrations/platform/github/integration.js +845 -3942
- package/dist/integrations/platform/github/integration.js.map +1 -1
- package/dist/integrations/platform/linear/integration.js +457 -1309
- package/dist/integrations/platform/linear/integration.js.map +1 -1
- package/dist/integrations/workos/integration.js +80 -85
- package/dist/integrations/workos/integration.js.map +1 -1
- package/dist/routes/config.d.ts.map +1 -1
- package/dist/routes/config.js +933 -915
- package/dist/routes/config.js.map +1 -1
- package/dist/routes/custom-provider-source.js +110 -93
- package/dist/routes/custom-provider-source.js.map +1 -1
- package/dist/routes/fs.js +401 -349
- package/dist/routes/fs.js.map +1 -1
- package/dist/routes/intake.js +165 -165
- package/dist/routes/intake.js.map +1 -1
- package/dist/routes/oauth.js +447 -25166
- package/dist/routes/oauth.js.map +1 -1
- package/dist/routes/projects.js +437 -376
- package/dist/routes/projects.js.map +1 -1
- package/dist/routes/provider-credentials.js +76 -58
- package/dist/routes/provider-credentials.js.map +1 -1
- package/dist/routes/route.js +12 -8
- package/dist/routes/route.js.map +1 -1
- package/dist/routes/skills.js +160 -173
- package/dist/routes/skills.js.map +1 -1
- package/dist/routes/surface.js +250 -28618
- package/dist/routes/surface.js.map +1 -1
- package/dist/routes/tenant-credentials.js +141 -166
- package/dist/routes/tenant-credentials.js.map +1 -1
- package/dist/routes/work-items.js +654 -862
- package/dist/routes/work-items.js.map +1 -1
- package/dist/rules/binding-context.js +23 -35
- package/dist/rules/binding-context.js.map +1 -1
- package/dist/rules/defaults.js +200 -309
- package/dist/rules/defaults.js.map +1 -1
- package/dist/rules/dispatcher.d.ts.map +1 -1
- package/dist/rules/dispatcher.js +438 -677
- package/dist/rules/dispatcher.js.map +1 -1
- package/dist/rules/index.js +5 -599
- package/dist/rules/processor.js +344 -596
- package/dist/rules/processor.js.map +1 -1
- package/dist/rules/resolve.js +21 -18
- package/dist/rules/resolve.js.map +1 -1
- package/dist/rules/start-coordinator.js +168 -157
- package/dist/rules/start-coordinator.js.map +1 -1
- package/dist/rules/tools.js +50 -85
- package/dist/rules/tools.js.map +1 -1
- package/dist/rules/transition-service.js +183 -454
- package/dist/rules/transition-service.js.map +1 -1
- package/dist/rules/types.js +33 -29
- package/dist/rules/types.js.map +1 -1
- package/dist/rules/validation.js +266 -281
- package/dist/rules/validation.js.map +1 -1
- package/dist/sandbox/fleet.js +272 -250
- package/dist/sandbox/fleet.js.map +1 -1
- package/dist/sandbox/reattach.js +13 -6
- package/dist/sandbox/reattach.js.map +1 -1
- package/dist/server-error.js +12 -16
- package/dist/server-error.js.map +1 -1
- package/dist/skills/service.js +30 -35
- package/dist/skills/service.js.map +1 -1
- package/dist/spa-static.d.ts.map +1 -1
- package/dist/spa-static.js +91 -64
- package/dist/spa-static.js.map +1 -1
- package/dist/state-signing.js +73 -44
- package/dist/state-signing.js.map +1 -1
- package/dist/storage/domains/audit/agent-audit.js +60 -59
- package/dist/storage/domains/audit/agent-audit.js.map +1 -1
- package/dist/storage/domains/audit/base.js +169 -112
- package/dist/storage/domains/audit/base.js.map +1 -1
- package/dist/storage/domains/audit/domain.js +155 -153
- package/dist/storage/domains/audit/domain.js.map +1 -1
- package/dist/storage/domains/credentials/base.js +259 -243
- package/dist/storage/domains/credentials/base.js.map +1 -1
- package/dist/storage/domains/custom-providers/base.js +138 -143
- package/dist/storage/domains/custom-providers/base.js.map +1 -1
- package/dist/storage/domains/intake/base.js +75 -57
- package/dist/storage/domains/intake/base.js.map +1 -1
- package/dist/storage/domains/integrations/base.js +310 -245
- package/dist/storage/domains/integrations/base.js.map +1 -1
- package/dist/storage/domains/memory-settings/base.js +117 -102
- package/dist/storage/domains/memory-settings/base.js.map +1 -1
- package/dist/storage/domains/model-packs/base.js +89 -88
- package/dist/storage/domains/model-packs/base.js.map +1 -1
- package/dist/storage/domains/projects/base.js +97 -92
- package/dist/storage/domains/projects/base.js.map +1 -1
- package/dist/storage/domains/queue-health/base.js +111 -76
- package/dist/storage/domains/queue-health/base.js.map +1 -1
- package/dist/storage/domains/source-control/base.d.ts +9 -0
- package/dist/storage/domains/source-control/base.d.ts.map +1 -1
- package/dist/storage/domains/source-control/base.js +777 -702
- package/dist/storage/domains/source-control/base.js.map +1 -1
- package/dist/storage/domains/source-control/inmemory.d.ts +4 -0
- package/dist/storage/domains/source-control/inmemory.d.ts.map +1 -1
- package/dist/storage/domains/source-control/inmemory.js +322 -344
- package/dist/storage/domains/source-control/inmemory.js.map +1 -1
- package/dist/storage/domains/work-items/base.d.ts.map +1 -1
- package/dist/storage/domains/work-items/base.js +1266 -1132
- package/dist/storage/domains/work-items/base.js.map +1 -1
- package/dist/storage/domains/work-items/metrics.js +202 -184
- package/dist/storage/domains/work-items/metrics.js.map +1 -1
- package/dist/timing.d.ts +15 -0
- package/dist/timing.d.ts.map +1 -0
- package/dist/timing.js +34 -0
- package/dist/timing.js.map +1 -0
- package/dist/workspace.d.ts +2 -3
- package/dist/workspace.d.ts.map +1 -1
- package/dist/workspace.js +236 -438
- package/dist/workspace.js.map +1 -1
- package/factory-skills/factory-review/SKILL.md +9 -2
- package/package.json +9 -9
- package/dist/capabilities/connection.js.map +0 -1
- package/dist/capabilities/intake.js.map +0 -1
- package/dist/capabilities/version-control.js.map +0 -1
- package/dist/index.js.map +0 -1
- package/dist/integrations/base.js.map +0 -1
- package/dist/integrations/linear/storage.js.map +0 -1
- package/dist/rules/index.js.map +0 -1
package/dist/routes/fs.js
CHANGED
|
@@ -1,394 +1,446 @@
|
|
|
1
|
-
|
|
1
|
+
import { registerApiRoute } from "@mastra/core/server";
|
|
2
|
+
import { isAbsolute, join, posix, resolve, sep } from "path";
|
|
2
3
|
import { lstat, open, readdir, realpath, stat } from "fs/promises";
|
|
3
4
|
import { homedir } from "os";
|
|
4
|
-
import { isAbsolute, join, posix as posixPath, resolve, sep } from "path";
|
|
5
5
|
import { SandboxFilesystem } from "@mastra/code-sdk/agents/sandbox-filesystem";
|
|
6
6
|
import { detectProject, getResourceIdOverride } from "@mastra/code-sdk/utils/project";
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
7
|
+
//#region src/routes/fs.ts
|
|
8
|
+
const MAX_TEXT_FILE_BYTES = 512 * 1024;
|
|
9
|
+
const TEXT_DECODER = new TextDecoder("utf-8", { fatal: true });
|
|
10
|
+
const APPROVED_RENDERED_ROOTS = /* @__PURE__ */ new Set([".artifacts"]);
|
|
11
|
+
/** Erase a route handler's path-parameterized context to a plain `Context`. */
|
|
11
12
|
function loose(c) {
|
|
12
|
-
|
|
13
|
+
return c;
|
|
13
14
|
}
|
|
15
|
+
/** Resolve the browsable root, defaulting to the user's home directory. */
|
|
14
16
|
function resolveFsRoot(root) {
|
|
15
|
-
|
|
17
|
+
return resolve(root && root.trim() ? root : homedir());
|
|
16
18
|
}
|
|
19
|
+
/** True when `candidate` is `root` or nested under it. */
|
|
17
20
|
function isWithinRoot(candidate, root) {
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
+
if (candidate === root) return true;
|
|
22
|
+
const rootWithSep = root.endsWith(sep) ? root : root + sep;
|
|
23
|
+
return candidate.startsWith(rootWithSep);
|
|
21
24
|
}
|
|
22
25
|
async function realOrResolved(path) {
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
26
|
+
try {
|
|
27
|
+
return await realpath(path);
|
|
28
|
+
} catch {
|
|
29
|
+
return path;
|
|
30
|
+
}
|
|
28
31
|
}
|
|
32
|
+
/**
|
|
33
|
+
* Resolve a path's real location (following symlinks) and confirm it stays
|
|
34
|
+
* within `root`. Returns the real path when confined, or `null` when it escapes
|
|
35
|
+
* the root or does not exist. Used so a symlink inside the root that points
|
|
36
|
+
* outside it cannot be browsed or selected.
|
|
37
|
+
*/
|
|
29
38
|
async function realPathWithinRoot(candidate, root) {
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
39
|
+
try {
|
|
40
|
+
const real = await realpath(candidate);
|
|
41
|
+
return isWithinRoot(real, root) ? real : null;
|
|
42
|
+
} catch {
|
|
43
|
+
return null;
|
|
44
|
+
}
|
|
36
45
|
}
|
|
37
46
|
function assertRelativePath(path, label) {
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
return normalized;
|
|
47
|
+
const trimmed = path.trim();
|
|
48
|
+
if (!trimmed) throw new Error(`Missing required query param: ${label}`);
|
|
49
|
+
if (isAbsolute(trimmed)) throw new Error(`${label} must be relative`);
|
|
50
|
+
if (trimmed.split(/[\\/]+/).includes("..")) throw new Error(`${label} escapes workspace`);
|
|
51
|
+
const normalized = resolve("/", trimmed).slice(1);
|
|
52
|
+
if (!normalized || normalized === ".." || normalized.startsWith(`..${sep}`)) throw new Error(`${label} escapes workspace`);
|
|
53
|
+
return normalized;
|
|
46
54
|
}
|
|
47
55
|
function assertApprovedRenderedRoot(renderedRoot) {
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
56
|
+
const safeRoot = assertRelativePath(renderedRoot, "root");
|
|
57
|
+
if (!APPROVED_RENDERED_ROOTS.has(safeRoot)) throw new Error("Root is not approved for rendered workspace access");
|
|
58
|
+
return safeRoot;
|
|
51
59
|
}
|
|
52
60
|
async function confinedWorkspacePath(root, workspacePath) {
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
61
|
+
const resolvedRoot = await realOrResolved(resolveFsRoot(root));
|
|
62
|
+
const workspace = await realPathWithinRoot(isAbsolute(workspacePath) ? resolve(workspacePath) : resolve(resolvedRoot, workspacePath), resolvedRoot);
|
|
63
|
+
if (!workspace) throw new Error("Path is outside the browsable root");
|
|
64
|
+
return {
|
|
65
|
+
resolvedRoot,
|
|
66
|
+
workspace
|
|
67
|
+
};
|
|
58
68
|
}
|
|
59
69
|
async function confinedWorkspaceRelativePath(root, workspacePath, relativePath) {
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
70
|
+
const safeRelativePath = assertRelativePath(relativePath, "path");
|
|
71
|
+
const { workspace } = await confinedWorkspacePath(root, workspacePath);
|
|
72
|
+
const candidate = resolve(workspace, safeRelativePath);
|
|
73
|
+
if (!isWithinRoot(candidate, workspace)) throw new Error("Path escapes workspace");
|
|
74
|
+
const confinedPath = await realPathWithinRoot(candidate, workspace);
|
|
75
|
+
if (!confinedPath) throw new Error("Path is outside the workspace");
|
|
76
|
+
return {
|
|
77
|
+
workspace,
|
|
78
|
+
path: confinedPath,
|
|
79
|
+
relativePath: safeRelativePath
|
|
80
|
+
};
|
|
67
81
|
}
|
|
82
|
+
/**
|
|
83
|
+
* List the directories inside `requestedPath`, confined to `root`. An absent or
|
|
84
|
+
* out-of-root path is clamped to the root, so the worst a malicious client can
|
|
85
|
+
* do is browse within the allowed root.
|
|
86
|
+
*/
|
|
68
87
|
async function listDirectory(root, requestedPath) {
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
88
|
+
const resolvedRoot = await realOrResolved(resolveFsRoot(root));
|
|
89
|
+
let target = resolvedRoot;
|
|
90
|
+
if (requestedPath && requestedPath.trim()) target = await realPathWithinRoot(isAbsolute(requestedPath) ? resolve(requestedPath) : resolve(resolvedRoot, requestedPath), resolvedRoot) ?? resolvedRoot;
|
|
91
|
+
try {
|
|
92
|
+
if (!(await stat(target)).isDirectory()) target = resolvedRoot;
|
|
93
|
+
} catch {
|
|
94
|
+
target = resolvedRoot;
|
|
95
|
+
}
|
|
96
|
+
const dirents = await readdir(target, { withFileTypes: true });
|
|
97
|
+
const entries = [];
|
|
98
|
+
for (const dirent of dirents) {
|
|
99
|
+
if (dirent.name.startsWith(".")) continue;
|
|
100
|
+
const entryPath = join(target, dirent.name);
|
|
101
|
+
let isDir = dirent.isDirectory();
|
|
102
|
+
if (dirent.isSymbolicLink()) {
|
|
103
|
+
const real = await realPathWithinRoot(entryPath, resolvedRoot);
|
|
104
|
+
isDir = real ? (await stat(real).catch(() => null))?.isDirectory() === true : false;
|
|
105
|
+
}
|
|
106
|
+
if (isDir) entries.push({
|
|
107
|
+
name: dirent.name,
|
|
108
|
+
path: entryPath
|
|
109
|
+
});
|
|
110
|
+
}
|
|
111
|
+
entries.sort((a, b) => a.name.localeCompare(b.name));
|
|
112
|
+
const parent = target === resolvedRoot ? null : resolve(target, "..");
|
|
113
|
+
return {
|
|
114
|
+
root: resolvedRoot,
|
|
115
|
+
path: target,
|
|
116
|
+
parent,
|
|
117
|
+
entries
|
|
118
|
+
};
|
|
96
119
|
}
|
|
97
120
|
async function listRenderedEntries(rootPath, currentPath = rootPath) {
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
}
|
|
125
|
-
return entries.sort((a, b) => a.path.localeCompare(b.path));
|
|
121
|
+
const dirents = await readdir(currentPath, { withFileTypes: true });
|
|
122
|
+
const entries = [];
|
|
123
|
+
for (const dirent of dirents) {
|
|
124
|
+
const entryPath = join(currentPath, dirent.name);
|
|
125
|
+
const info = await lstat(entryPath);
|
|
126
|
+
const relativePath = entryPath.slice(rootPath.length + 1);
|
|
127
|
+
if (info.isDirectory()) {
|
|
128
|
+
entries.push({
|
|
129
|
+
name: dirent.name,
|
|
130
|
+
path: relativePath,
|
|
131
|
+
type: "directory",
|
|
132
|
+
size: info.size,
|
|
133
|
+
updatedAt: info.mtime.toISOString()
|
|
134
|
+
});
|
|
135
|
+
entries.push(...await listRenderedEntries(rootPath, entryPath));
|
|
136
|
+
continue;
|
|
137
|
+
}
|
|
138
|
+
if (info.isFile()) entries.push({
|
|
139
|
+
name: dirent.name,
|
|
140
|
+
path: relativePath,
|
|
141
|
+
type: "file",
|
|
142
|
+
size: info.size,
|
|
143
|
+
updatedAt: info.mtime.toISOString()
|
|
144
|
+
});
|
|
145
|
+
}
|
|
146
|
+
return entries.sort((a, b) => a.path.localeCompare(b.path));
|
|
126
147
|
}
|
|
127
148
|
async function listWorkspaceRenderedPath(root, workspacePath, renderedRoot) {
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
149
|
+
const safeRoot = assertApprovedRenderedRoot(renderedRoot);
|
|
150
|
+
const { workspace } = await confinedWorkspacePath(root, workspacePath);
|
|
151
|
+
const renderedPath = resolve(workspace, safeRoot);
|
|
152
|
+
if (!isWithinRoot(renderedPath, workspace)) throw new Error("Root escapes workspace");
|
|
153
|
+
const confinedRootPath = await realPathWithinRoot(renderedPath, workspace);
|
|
154
|
+
if (!confinedRootPath) return {
|
|
155
|
+
workspacePath: workspace,
|
|
156
|
+
root: safeRoot,
|
|
157
|
+
rootPath: renderedPath,
|
|
158
|
+
entries: []
|
|
159
|
+
};
|
|
160
|
+
if (!(await stat(confinedRootPath)).isDirectory()) return {
|
|
161
|
+
workspacePath: workspace,
|
|
162
|
+
root: safeRoot,
|
|
163
|
+
rootPath: confinedRootPath,
|
|
164
|
+
entries: []
|
|
165
|
+
};
|
|
166
|
+
return {
|
|
167
|
+
workspacePath: workspace,
|
|
168
|
+
root: safeRoot,
|
|
169
|
+
rootPath: confinedRootPath,
|
|
170
|
+
entries: await listRenderedEntries(confinedRootPath)
|
|
171
|
+
};
|
|
142
172
|
}
|
|
143
173
|
async function readWorkspaceFile(root, workspacePath, path) {
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
|
|
178
|
-
|
|
179
|
-
name: relativePath.split("/").pop() ?? relativePath,
|
|
180
|
-
size: info.size,
|
|
181
|
-
updatedAt: info.mtime.toISOString(),
|
|
182
|
-
contentType: "unsupported"
|
|
183
|
-
};
|
|
184
|
-
}
|
|
174
|
+
assertApprovedRenderedRoot(assertRelativePath(path, "path").split("/")[0] ?? "");
|
|
175
|
+
const { workspace, path: confinedPath, relativePath } = await confinedWorkspaceRelativePath(root, workspacePath, path);
|
|
176
|
+
const info = await lstat(confinedPath);
|
|
177
|
+
if (info.isDirectory()) throw new Error("Path is a directory");
|
|
178
|
+
if (!info.isFile()) throw new Error("Unsupported file type");
|
|
179
|
+
const bytesToRead = Math.min(info.size, MAX_TEXT_FILE_BYTES);
|
|
180
|
+
const contentBuffer = Buffer.alloc(bytesToRead);
|
|
181
|
+
const handle = await open(confinedPath, "r");
|
|
182
|
+
try {
|
|
183
|
+
await handle.read(contentBuffer, 0, bytesToRead, 0);
|
|
184
|
+
} finally {
|
|
185
|
+
await handle.close();
|
|
186
|
+
}
|
|
187
|
+
try {
|
|
188
|
+
const content = TEXT_DECODER.decode(contentBuffer);
|
|
189
|
+
return {
|
|
190
|
+
workspacePath: workspace,
|
|
191
|
+
path: relativePath,
|
|
192
|
+
name: relativePath.split("/").pop() ?? relativePath,
|
|
193
|
+
size: info.size,
|
|
194
|
+
updatedAt: info.mtime.toISOString(),
|
|
195
|
+
contentType: "text",
|
|
196
|
+
content,
|
|
197
|
+
truncated: info.size > MAX_TEXT_FILE_BYTES
|
|
198
|
+
};
|
|
199
|
+
} catch {
|
|
200
|
+
return {
|
|
201
|
+
workspacePath: workspace,
|
|
202
|
+
path: relativePath,
|
|
203
|
+
name: relativePath.split("/").pop() ?? relativePath,
|
|
204
|
+
size: info.size,
|
|
205
|
+
updatedAt: info.mtime.toISOString(),
|
|
206
|
+
contentType: "unsupported"
|
|
207
|
+
};
|
|
208
|
+
}
|
|
185
209
|
}
|
|
186
210
|
async function listArtifacts(root, workspacePath) {
|
|
187
|
-
|
|
188
|
-
|
|
189
|
-
|
|
190
|
-
|
|
191
|
-
|
|
192
|
-
|
|
211
|
+
const listing = await listWorkspaceRenderedPath(root, workspacePath, ".artifacts");
|
|
212
|
+
return {
|
|
213
|
+
rootPath: listing.workspacePath,
|
|
214
|
+
artifactsPath: listing.rootPath,
|
|
215
|
+
entries: listing.entries
|
|
216
|
+
};
|
|
193
217
|
}
|
|
218
|
+
/**
|
|
219
|
+
* Resolve a `workspacePath` query param as a Factory session id. Returns the
|
|
220
|
+
* session when one exists and the caller owns it, `null` when no session
|
|
221
|
+
* matches (the caller should fall back to local-path handling), and throws
|
|
222
|
+
* when a session exists but belongs to another tenant.
|
|
223
|
+
*/
|
|
194
224
|
async function resolveAuthorizedSession(c, deps, workspacePath) {
|
|
195
|
-
|
|
196
|
-
|
|
197
|
-
|
|
198
|
-
|
|
199
|
-
|
|
200
|
-
|
|
201
|
-
|
|
202
|
-
|
|
203
|
-
|
|
204
|
-
}
|
|
205
|
-
return session;
|
|
225
|
+
if (!deps) return null;
|
|
226
|
+
const session = await deps.sessions.getBySessionId(workspacePath);
|
|
227
|
+
if (!session) return null;
|
|
228
|
+
if (deps.auth.enabled()) {
|
|
229
|
+
await deps.auth.ensureUser(c);
|
|
230
|
+
const tenant = deps.auth.tenant(c);
|
|
231
|
+
if (!tenant || tenant.orgId !== session.orgId || tenant.userId !== session.userId) throw new Error("Session is not available to the current user");
|
|
232
|
+
}
|
|
233
|
+
return session;
|
|
206
234
|
}
|
|
235
|
+
/**
|
|
236
|
+
* Reattach to the session's sandbox and wrap its workdir in a
|
|
237
|
+
* `SandboxFilesystem`. Returns `null` when the session has no provisioned
|
|
238
|
+
* sandbox yet (nothing materialized → nothing to list), or when the sandbox
|
|
239
|
+
* can no longer be reattached (e.g. torn down by the provider's idle GC).
|
|
240
|
+
* This is a passive read path, so it never re-provisions: the session's
|
|
241
|
+
* filesystem is preserved in its provider checkpoint and comes back the next
|
|
242
|
+
* time the workspace is actually opened (e.g. by sending a message).
|
|
243
|
+
*/
|
|
207
244
|
async function sessionSandbox(fleet, session) {
|
|
208
|
-
|
|
209
|
-
|
|
210
|
-
|
|
211
|
-
|
|
212
|
-
|
|
213
|
-
|
|
214
|
-
|
|
215
|
-
|
|
216
|
-
|
|
217
|
-
|
|
218
|
-
|
|
219
|
-
|
|
245
|
+
if (!fleet.enabled || !session.sandboxId || !session.sandboxWorkdir) return null;
|
|
246
|
+
let sandbox;
|
|
247
|
+
try {
|
|
248
|
+
sandbox = await fleet.reattachSandbox(session.sandboxId, { workingDirectory: session.sandboxWorkdir });
|
|
249
|
+
} catch {
|
|
250
|
+
return null;
|
|
251
|
+
}
|
|
252
|
+
return {
|
|
253
|
+
sandbox,
|
|
254
|
+
filesystem: new SandboxFilesystem({
|
|
255
|
+
sandbox,
|
|
256
|
+
workdir: session.sandboxWorkdir
|
|
257
|
+
}),
|
|
258
|
+
workdir: session.sandboxWorkdir
|
|
259
|
+
};
|
|
220
260
|
}
|
|
261
|
+
/** List an approved rendered root inside a Factory session's sandbox workdir. */
|
|
221
262
|
async function listSessionRenderedPath(fleet, session, renderedRoot) {
|
|
222
|
-
|
|
223
|
-
|
|
224
|
-
|
|
225
|
-
|
|
226
|
-
|
|
227
|
-
|
|
228
|
-
|
|
229
|
-
|
|
230
|
-
|
|
231
|
-
|
|
232
|
-
|
|
233
|
-
|
|
234
|
-
|
|
235
|
-
|
|
236
|
-
|
|
237
|
-
|
|
238
|
-
|
|
239
|
-
|
|
240
|
-
|
|
241
|
-
|
|
242
|
-
|
|
243
|
-
|
|
244
|
-
|
|
245
|
-
|
|
246
|
-
|
|
247
|
-
|
|
248
|
-
|
|
249
|
-
|
|
250
|
-
|
|
251
|
-
|
|
252
|
-
|
|
253
|
-
|
|
263
|
+
const safeRoot = assertApprovedRenderedRoot(renderedRoot);
|
|
264
|
+
const rootPath = posix.join(session.sandboxWorkdir ?? "", safeRoot);
|
|
265
|
+
const empty = {
|
|
266
|
+
workspacePath: session.sessionId,
|
|
267
|
+
root: safeRoot,
|
|
268
|
+
rootPath,
|
|
269
|
+
entries: []
|
|
270
|
+
};
|
|
271
|
+
const handle = await sessionSandbox(fleet, session);
|
|
272
|
+
if (!handle) return empty;
|
|
273
|
+
const quotedRoot = `'${rootPath.replace(/'/g, `'\\''`)}'`;
|
|
274
|
+
const result = await handle.sandbox.executeCommand("sh", ["-c", `test -d ${quotedRoot} && find ${quotedRoot} -mindepth 1 -printf '%y\\t%s\\t%T@\\t%p\\n' 2>/dev/null || true`], { timeout: 3e4 });
|
|
275
|
+
if (result.exitCode !== 0) return empty;
|
|
276
|
+
const entries = [];
|
|
277
|
+
for (const line of result.stdout.split("\n")) {
|
|
278
|
+
if (!line) continue;
|
|
279
|
+
const [type, sizeStr, mtimeStr, ...pathParts] = line.split(" ");
|
|
280
|
+
const fullPath = pathParts.join(" ");
|
|
281
|
+
if (!fullPath || !fullPath.startsWith(`${rootPath}/`)) continue;
|
|
282
|
+
const relativePath = fullPath.slice(rootPath.length + 1);
|
|
283
|
+
entries.push({
|
|
284
|
+
name: posix.basename(relativePath),
|
|
285
|
+
path: relativePath,
|
|
286
|
+
type: type === "d" ? "directory" : "file",
|
|
287
|
+
size: type === "d" ? 0 : Number(sizeStr) || 0,
|
|
288
|
+
updatedAt: (/* @__PURE__ */ new Date((Number(mtimeStr) || 0) * 1e3)).toISOString()
|
|
289
|
+
});
|
|
290
|
+
}
|
|
291
|
+
entries.sort((a, b) => a.path.localeCompare(b.path));
|
|
292
|
+
return {
|
|
293
|
+
workspacePath: session.sessionId,
|
|
294
|
+
root: safeRoot,
|
|
295
|
+
rootPath,
|
|
296
|
+
entries
|
|
297
|
+
};
|
|
254
298
|
}
|
|
299
|
+
/** Read a file under an approved rendered root inside a session's sandbox. */
|
|
255
300
|
async function readSessionWorkspaceFile(fleet, session, path) {
|
|
256
|
-
|
|
257
|
-
|
|
258
|
-
|
|
259
|
-
|
|
260
|
-
|
|
261
|
-
|
|
262
|
-
|
|
263
|
-
|
|
264
|
-
|
|
265
|
-
|
|
266
|
-
|
|
267
|
-
|
|
268
|
-
|
|
269
|
-
|
|
270
|
-
|
|
271
|
-
|
|
272
|
-
|
|
273
|
-
|
|
274
|
-
|
|
275
|
-
|
|
276
|
-
|
|
277
|
-
|
|
301
|
+
const safePath = assertRelativePath(path, "path");
|
|
302
|
+
assertApprovedRenderedRoot(safePath.split("/")[0] ?? "");
|
|
303
|
+
const handle = await sessionSandbox(fleet, session);
|
|
304
|
+
if (!handle) throw new Error("Session workspace is not available");
|
|
305
|
+
const { filesystem } = handle;
|
|
306
|
+
const info = await filesystem.stat(safePath);
|
|
307
|
+
if (info.type === "directory") throw new Error("Path is a directory");
|
|
308
|
+
const buffer = await filesystem.readFile(safePath);
|
|
309
|
+
const truncated = buffer.length > MAX_TEXT_FILE_BYTES;
|
|
310
|
+
const base = {
|
|
311
|
+
workspacePath: session.sessionId,
|
|
312
|
+
path: safePath,
|
|
313
|
+
name: posix.basename(safePath),
|
|
314
|
+
size: buffer.length,
|
|
315
|
+
updatedAt: info.modifiedAt.toISOString()
|
|
316
|
+
};
|
|
317
|
+
try {
|
|
318
|
+
const content = TEXT_DECODER.decode(truncated ? buffer.subarray(0, MAX_TEXT_FILE_BYTES) : buffer);
|
|
319
|
+
return {
|
|
320
|
+
...base,
|
|
321
|
+
contentType: "text",
|
|
322
|
+
content,
|
|
323
|
+
truncated
|
|
324
|
+
};
|
|
325
|
+
} catch {
|
|
326
|
+
return {
|
|
327
|
+
...base,
|
|
328
|
+
contentType: "unsupported"
|
|
329
|
+
};
|
|
330
|
+
}
|
|
278
331
|
}
|
|
332
|
+
/**
|
|
333
|
+
* Resolve a project path to the same resourceId the TUI uses. Mirrors
|
|
334
|
+
* `createMastraCode`: detect the project, then apply any resourceId override
|
|
335
|
+
* (MASTRA_RESOURCE_ID env var or `.mastracode/database.json`). This is the
|
|
336
|
+
* shared continuity point — start in the TUI, continue on the web, same path
|
|
337
|
+
* → same resourceId → same session.
|
|
338
|
+
*/
|
|
279
339
|
function resolveCodebase(projectPath) {
|
|
280
|
-
|
|
281
|
-
|
|
282
|
-
|
|
283
|
-
|
|
284
|
-
|
|
285
|
-
|
|
286
|
-
|
|
287
|
-
|
|
288
|
-
};
|
|
340
|
+
const info = detectProject(projectPath);
|
|
341
|
+
return {
|
|
342
|
+
resourceId: getResourceIdOverride(info.rootPath) ?? info.resourceId,
|
|
343
|
+
name: info.name,
|
|
344
|
+
rootPath: info.rootPath,
|
|
345
|
+
gitUrl: info.gitUrl,
|
|
346
|
+
gitBranch: info.gitBranch
|
|
347
|
+
};
|
|
289
348
|
}
|
|
349
|
+
/**
|
|
350
|
+
* Build the web filesystem routes as Mastra `apiRoutes`:
|
|
351
|
+
* - `GET /web/fs/list?path=...` — browse directories (confined to root)
|
|
352
|
+
* - `GET /web/codebase/resolve?path=...` — TUI-compatible codebase resourceId
|
|
353
|
+
*/
|
|
290
354
|
function buildFsRoutes(options = {}) {
|
|
291
|
-
|
|
292
|
-
|
|
293
|
-
|
|
294
|
-
|
|
295
|
-
|
|
296
|
-
|
|
297
|
-
|
|
298
|
-
|
|
299
|
-
|
|
300
|
-
|
|
301
|
-
|
|
302
|
-
|
|
303
|
-
|
|
304
|
-
|
|
305
|
-
|
|
306
|
-
|
|
307
|
-
|
|
308
|
-
|
|
309
|
-
|
|
310
|
-
|
|
311
|
-
|
|
312
|
-
|
|
313
|
-
|
|
314
|
-
|
|
315
|
-
|
|
316
|
-
|
|
317
|
-
|
|
318
|
-
|
|
319
|
-
|
|
320
|
-
|
|
321
|
-
|
|
322
|
-
|
|
323
|
-
|
|
324
|
-
|
|
325
|
-
|
|
326
|
-
|
|
327
|
-
|
|
328
|
-
|
|
329
|
-
|
|
330
|
-
|
|
331
|
-
|
|
332
|
-
|
|
333
|
-
|
|
334
|
-
|
|
335
|
-
|
|
336
|
-
|
|
337
|
-
|
|
338
|
-
|
|
339
|
-
|
|
340
|
-
|
|
341
|
-
|
|
342
|
-
|
|
343
|
-
|
|
344
|
-
|
|
345
|
-
|
|
346
|
-
|
|
347
|
-
|
|
348
|
-
|
|
349
|
-
|
|
350
|
-
|
|
351
|
-
|
|
352
|
-
|
|
353
|
-
|
|
354
|
-
|
|
355
|
-
|
|
356
|
-
|
|
357
|
-
|
|
358
|
-
|
|
359
|
-
|
|
360
|
-
|
|
361
|
-
|
|
362
|
-
|
|
363
|
-
|
|
364
|
-
|
|
365
|
-
|
|
366
|
-
|
|
367
|
-
|
|
368
|
-
|
|
369
|
-
|
|
370
|
-
|
|
371
|
-
|
|
372
|
-
|
|
373
|
-
|
|
374
|
-
|
|
375
|
-
|
|
376
|
-
|
|
377
|
-
|
|
378
|
-
}
|
|
379
|
-
}
|
|
380
|
-
})
|
|
381
|
-
];
|
|
355
|
+
const root = resolveFsRoot(options.root);
|
|
356
|
+
const sessionFs = options.sessionFs;
|
|
357
|
+
return [
|
|
358
|
+
registerApiRoute("/web/fs/list", {
|
|
359
|
+
method: "GET",
|
|
360
|
+
requiresAuth: false,
|
|
361
|
+
handler: async (c) => {
|
|
362
|
+
const path = c.req.query("path");
|
|
363
|
+
try {
|
|
364
|
+
const listing = await listDirectory(root, path);
|
|
365
|
+
return c.json(listing);
|
|
366
|
+
} catch (error) {
|
|
367
|
+
const message = error instanceof Error ? error.message : String(error);
|
|
368
|
+
return c.json({ error: message }, 500);
|
|
369
|
+
}
|
|
370
|
+
}
|
|
371
|
+
}),
|
|
372
|
+
registerApiRoute("/web/artifacts/list", {
|
|
373
|
+
method: "GET",
|
|
374
|
+
requiresAuth: false,
|
|
375
|
+
handler: async (c) => {
|
|
376
|
+
const path = c.req.query("path");
|
|
377
|
+
if (!path) return c.json({ error: "Missing required query param: path" }, 400);
|
|
378
|
+
try {
|
|
379
|
+
return c.json(await listArtifacts(root, path));
|
|
380
|
+
} catch (error) {
|
|
381
|
+
const message = error instanceof Error ? error.message : String(error);
|
|
382
|
+
const status = message === "Path is outside the browsable root" ? 403 : 500;
|
|
383
|
+
return c.json({ error: message }, status);
|
|
384
|
+
}
|
|
385
|
+
}
|
|
386
|
+
}),
|
|
387
|
+
registerApiRoute("/web/workspace/rendered/list", {
|
|
388
|
+
method: "GET",
|
|
389
|
+
requiresAuth: false,
|
|
390
|
+
handler: async (c) => {
|
|
391
|
+
const workspacePath = c.req.query("workspacePath");
|
|
392
|
+
const renderedRoot = c.req.query("root");
|
|
393
|
+
if (!workspacePath) return c.json({ error: "Missing required query param: workspacePath" }, 400);
|
|
394
|
+
if (!renderedRoot) return c.json({ error: "Missing required query param: root" }, 400);
|
|
395
|
+
try {
|
|
396
|
+
const session = await resolveAuthorizedSession(loose(c), sessionFs, workspacePath);
|
|
397
|
+
if (session && sessionFs) return c.json(await listSessionRenderedPath(sessionFs.fleet, session, renderedRoot));
|
|
398
|
+
return c.json(await listWorkspaceRenderedPath(root, workspacePath, renderedRoot));
|
|
399
|
+
} catch (error) {
|
|
400
|
+
const message = error instanceof Error ? error.message : String(error);
|
|
401
|
+
const status = message.includes("outside") || message.includes("relative") || message.includes("escapes") || message.includes("not approved") || message.includes("not available") ? 403 : 500;
|
|
402
|
+
return c.json({ error: message }, status);
|
|
403
|
+
}
|
|
404
|
+
}
|
|
405
|
+
}),
|
|
406
|
+
registerApiRoute("/web/workspace/file", {
|
|
407
|
+
method: "GET",
|
|
408
|
+
requiresAuth: false,
|
|
409
|
+
handler: async (c) => {
|
|
410
|
+
const workspacePath = c.req.query("workspacePath");
|
|
411
|
+
const path = c.req.query("path");
|
|
412
|
+
if (!workspacePath) return c.json({ error: "Missing required query param: workspacePath" }, 400);
|
|
413
|
+
if (!path) return c.json({ error: "Missing required query param: path" }, 400);
|
|
414
|
+
try {
|
|
415
|
+
const session = await resolveAuthorizedSession(loose(c), sessionFs, workspacePath);
|
|
416
|
+
if (session && sessionFs) return c.json(await readSessionWorkspaceFile(sessionFs.fleet, session, path));
|
|
417
|
+
return c.json(await readWorkspaceFile(root, workspacePath, path));
|
|
418
|
+
} catch (error) {
|
|
419
|
+
const message = error instanceof Error ? error.message : String(error);
|
|
420
|
+
const status = message.includes("outside") || message.includes("relative") || message.includes("escapes") || message.includes("not approved") || message.includes("not available") ? 403 : message.includes("directory") ? 400 : message.includes("not found") ? 404 : 500;
|
|
421
|
+
return c.json({ error: message }, status);
|
|
422
|
+
}
|
|
423
|
+
}
|
|
424
|
+
}),
|
|
425
|
+
registerApiRoute("/web/codebase/resolve", {
|
|
426
|
+
method: "GET",
|
|
427
|
+
requiresAuth: false,
|
|
428
|
+
handler: async (c) => {
|
|
429
|
+
const path = c.req.query("path");
|
|
430
|
+
if (!path) return c.json({ error: "Missing required query param: path" }, 400);
|
|
431
|
+
const confined = await realPathWithinRoot(isAbsolute(path) ? resolve(path) : resolve(root, path), root);
|
|
432
|
+
if (!confined) return c.json({ error: "Path is outside the browsable root" }, 403);
|
|
433
|
+
try {
|
|
434
|
+
return c.json(resolveCodebase(confined));
|
|
435
|
+
} catch (error) {
|
|
436
|
+
const message = error instanceof Error ? error.message : String(error);
|
|
437
|
+
return c.json({ error: message }, 500);
|
|
438
|
+
}
|
|
439
|
+
}
|
|
440
|
+
})
|
|
441
|
+
];
|
|
382
442
|
}
|
|
383
|
-
|
|
384
|
-
|
|
385
|
-
|
|
386
|
-
listDirectory,
|
|
387
|
-
listSessionRenderedPath,
|
|
388
|
-
listWorkspaceRenderedPath,
|
|
389
|
-
readSessionWorkspaceFile,
|
|
390
|
-
readWorkspaceFile,
|
|
391
|
-
resolveCodebase,
|
|
392
|
-
resolveFsRoot
|
|
393
|
-
};
|
|
443
|
+
//#endregion
|
|
444
|
+
export { buildFsRoutes, listArtifacts, listDirectory, listSessionRenderedPath, listWorkspaceRenderedPath, readSessionWorkspaceFile, readWorkspaceFile, resolveCodebase, resolveFsRoot };
|
|
445
|
+
|
|
394
446
|
//# sourceMappingURL=fs.js.map
|