@mastra/factory 0.0.0-fix-thread-subscription-multi-instance-redis-streams-20260730204150
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 +695 -0
- package/LICENSE.md +30 -0
- package/README.md +27 -0
- package/dist/auth.d.ts +181 -0
- package/dist/auth.d.ts.map +1 -0
- package/dist/auth.js +505 -0
- package/dist/auth.js.map +1 -0
- package/dist/capabilities/connection.d.ts +9 -0
- package/dist/capabilities/connection.d.ts.map +1 -0
- package/dist/capabilities/connection.js +1 -0
- package/dist/capabilities/intake.d.ts +140 -0
- package/dist/capabilities/intake.d.ts.map +1 -0
- package/dist/capabilities/intake.js +1 -0
- package/dist/capabilities/version-control.d.ts +245 -0
- package/dist/capabilities/version-control.d.ts.map +1 -0
- package/dist/capabilities/version-control.js +1 -0
- package/dist/factory.d.ts +156 -0
- package/dist/factory.d.ts.map +1 -0
- package/dist/factory.js +483 -0
- package/dist/factory.js.map +1 -0
- package/dist/index.d.ts +14 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +7 -0
- package/dist/integrations/base.d.ts +210 -0
- package/dist/integrations/base.d.ts.map +1 -0
- package/dist/integrations/base.js +1 -0
- package/dist/integrations/github/config.d.ts +60 -0
- package/dist/integrations/github/config.d.ts.map +1 -0
- package/dist/integrations/github/config.js +42 -0
- package/dist/integrations/github/config.js.map +1 -0
- package/dist/integrations/github/factory-session.d.ts +12 -0
- package/dist/integrations/github/factory-session.d.ts.map +1 -0
- package/dist/integrations/github/factory-session.js +37 -0
- package/dist/integrations/github/factory-session.js.map +1 -0
- package/dist/integrations/github/integration.d.ts +191 -0
- package/dist/integrations/github/integration.d.ts.map +1 -0
- package/dist/integrations/github/integration.js +1077 -0
- package/dist/integrations/github/integration.js.map +1 -0
- package/dist/integrations/github/issue-triage.d.ts +27 -0
- package/dist/integrations/github/issue-triage.d.ts.map +1 -0
- package/dist/integrations/github/issue-triage.js +82 -0
- package/dist/integrations/github/issue-triage.js.map +1 -0
- package/dist/integrations/github/pat.d.ts +38 -0
- package/dist/integrations/github/pat.d.ts.map +1 -0
- package/dist/integrations/github/pat.js +59 -0
- package/dist/integrations/github/pat.js.map +1 -0
- package/dist/integrations/github/provenance.d.ts +27 -0
- package/dist/integrations/github/provenance.d.ts.map +1 -0
- package/dist/integrations/github/provenance.js +75 -0
- package/dist/integrations/github/provenance.js.map +1 -0
- package/dist/integrations/github/routes.d.ts +69 -0
- package/dist/integrations/github/routes.d.ts.map +1 -0
- package/dist/integrations/github/routes.js +1324 -0
- package/dist/integrations/github/routes.js.map +1 -0
- package/dist/integrations/github/rules.d.ts +78 -0
- package/dist/integrations/github/rules.d.ts.map +1 -0
- package/dist/integrations/github/rules.js +362 -0
- package/dist/integrations/github/rules.js.map +1 -0
- package/dist/integrations/github/sandbox-release.d.ts +38 -0
- package/dist/integrations/github/sandbox-release.d.ts.map +1 -0
- package/dist/integrations/github/sandbox-release.js +74 -0
- package/dist/integrations/github/sandbox-release.js.map +1 -0
- package/dist/integrations/github/sandbox.d.ts +281 -0
- package/dist/integrations/github/sandbox.d.ts.map +1 -0
- package/dist/integrations/github/sandbox.js +547 -0
- package/dist/integrations/github/sandbox.js.map +1 -0
- package/dist/integrations/github/session-subscriptions.d.ts +26 -0
- package/dist/integrations/github/session-subscriptions.d.ts.map +1 -0
- package/dist/integrations/github/session-subscriptions.js +192 -0
- package/dist/integrations/github/session-subscriptions.js.map +1 -0
- package/dist/integrations/github/subscriptions.d.ts +54 -0
- package/dist/integrations/github/subscriptions.d.ts.map +1 -0
- package/dist/integrations/github/subscriptions.js +62 -0
- package/dist/integrations/github/subscriptions.js.map +1 -0
- package/dist/integrations/github/token-refresh.d.ts +12 -0
- package/dist/integrations/github/token-refresh.d.ts.map +1 -0
- package/dist/integrations/github/token-refresh.js +24 -0
- package/dist/integrations/github/token-refresh.js.map +1 -0
- package/dist/integrations/github/webhook.d.ts +87 -0
- package/dist/integrations/github/webhook.d.ts.map +1 -0
- package/dist/integrations/github/webhook.js +397 -0
- package/dist/integrations/github/webhook.js.map +1 -0
- package/dist/integrations/linear/agent-tools.d.ts +34 -0
- package/dist/integrations/linear/agent-tools.d.ts.map +1 -0
- package/dist/integrations/linear/agent-tools.js +85 -0
- package/dist/integrations/linear/agent-tools.js.map +1 -0
- package/dist/integrations/linear/integration.d.ts +233 -0
- package/dist/integrations/linear/integration.d.ts.map +1 -0
- package/dist/integrations/linear/integration.js +672 -0
- package/dist/integrations/linear/integration.js.map +1 -0
- package/dist/integrations/linear/routes.d.ts +61 -0
- package/dist/integrations/linear/routes.d.ts.map +1 -0
- package/dist/integrations/linear/routes.js +239 -0
- package/dist/integrations/linear/routes.js.map +1 -0
- package/dist/integrations/linear/rules.d.ts +42 -0
- package/dist/integrations/linear/rules.d.ts.map +1 -0
- package/dist/integrations/linear/rules.js +133 -0
- package/dist/integrations/linear/rules.js.map +1 -0
- package/dist/integrations/linear/storage.d.ts +65 -0
- package/dist/integrations/linear/storage.d.ts.map +1 -0
- package/dist/integrations/linear/storage.js +1 -0
- package/dist/integrations/platform/api-client.d.ts +26 -0
- package/dist/integrations/platform/api-client.d.ts.map +1 -0
- package/dist/integrations/platform/api-client.js +156 -0
- package/dist/integrations/platform/api-client.js.map +1 -0
- package/dist/integrations/platform/github/event-worker.d.ts +49 -0
- package/dist/integrations/platform/github/event-worker.d.ts.map +1 -0
- package/dist/integrations/platform/github/event-worker.js +348 -0
- package/dist/integrations/platform/github/event-worker.js.map +1 -0
- package/dist/integrations/platform/github/integration.d.ts +50 -0
- package/dist/integrations/platform/github/integration.d.ts.map +1 -0
- package/dist/integrations/platform/github/integration.js +961 -0
- package/dist/integrations/platform/github/integration.js.map +1 -0
- package/dist/integrations/platform/linear/integration.d.ts +46 -0
- package/dist/integrations/platform/linear/integration.d.ts.map +1 -0
- package/dist/integrations/platform/linear/integration.js +483 -0
- package/dist/integrations/platform/linear/integration.js.map +1 -0
- package/dist/integrations/workos/integration.d.ts +39 -0
- package/dist/integrations/workos/integration.d.ts.map +1 -0
- package/dist/integrations/workos/integration.js +91 -0
- package/dist/integrations/workos/integration.js.map +1 -0
- package/dist/routes/config.d.ts +235 -0
- package/dist/routes/config.d.ts.map +1 -0
- package/dist/routes/config.js +972 -0
- package/dist/routes/config.js.map +1 -0
- package/dist/routes/custom-provider-source.d.ts +52 -0
- package/dist/routes/custom-provider-source.d.ts.map +1 -0
- package/dist/routes/custom-provider-source.js +124 -0
- package/dist/routes/custom-provider-source.js.map +1 -0
- package/dist/routes/fs.d.ts +151 -0
- package/dist/routes/fs.d.ts.map +1 -0
- package/dist/routes/fs.js +708 -0
- package/dist/routes/fs.js.map +1 -0
- package/dist/routes/intake.d.ts +23 -0
- package/dist/routes/intake.d.ts.map +1 -0
- package/dist/routes/intake.js +179 -0
- package/dist/routes/intake.js.map +1 -0
- package/dist/routes/oauth.d.ts +43 -0
- package/dist/routes/oauth.d.ts.map +1 -0
- package/dist/routes/oauth.js +463 -0
- package/dist/routes/oauth.js.map +1 -0
- package/dist/routes/projects.d.ts +19 -0
- package/dist/routes/projects.d.ts.map +1 -0
- package/dist/routes/projects.js +461 -0
- package/dist/routes/projects.js.map +1 -0
- package/dist/routes/provider-credentials.d.ts +81 -0
- package/dist/routes/provider-credentials.d.ts.map +1 -0
- package/dist/routes/provider-credentials.js +86 -0
- package/dist/routes/provider-credentials.js.map +1 -0
- package/dist/routes/route.d.ts +48 -0
- package/dist/routes/route.d.ts.map +1 -0
- package/dist/routes/route.js +15 -0
- package/dist/routes/route.js.map +1 -0
- package/dist/routes/skills.d.ts +30 -0
- package/dist/routes/skills.d.ts.map +1 -0
- package/dist/routes/skills.js +168 -0
- package/dist/routes/skills.js.map +1 -0
- package/dist/routes/surface.d.ts +91 -0
- package/dist/routes/surface.d.ts.map +1 -0
- package/dist/routes/surface.js +260 -0
- package/dist/routes/surface.js.map +1 -0
- package/dist/routes/tenant-credentials.d.ts +74 -0
- package/dist/routes/tenant-credentials.d.ts.map +1 -0
- package/dist/routes/tenant-credentials.js +156 -0
- package/dist/routes/tenant-credentials.js.map +1 -0
- package/dist/routes/work-items.d.ts +40 -0
- package/dist/routes/work-items.d.ts.map +1 -0
- package/dist/routes/work-items.js +695 -0
- package/dist/routes/work-items.js.map +1 -0
- package/dist/rules/binding-context.d.ts +5 -0
- package/dist/rules/binding-context.d.ts.map +1 -0
- package/dist/rules/binding-context.js +28 -0
- package/dist/rules/binding-context.js.map +1 -0
- package/dist/rules/defaults.d.ts +9 -0
- package/dist/rules/defaults.d.ts.map +1 -0
- package/dist/rules/defaults.js +260 -0
- package/dist/rules/defaults.js.map +1 -0
- package/dist/rules/dispatcher.d.ts +42 -0
- package/dist/rules/dispatcher.d.ts.map +1 -0
- package/dist/rules/dispatcher.js +518 -0
- package/dist/rules/dispatcher.js.map +1 -0
- package/dist/rules/index.d.ts +7 -0
- package/dist/rules/index.d.ts.map +1 -0
- package/dist/rules/index.js +5 -0
- package/dist/rules/processor.d.ts +54 -0
- package/dist/rules/processor.d.ts.map +1 -0
- package/dist/rules/processor.js +378 -0
- package/dist/rules/processor.js.map +1 -0
- package/dist/rules/resolve.d.ts +17 -0
- package/dist/rules/resolve.d.ts.map +1 -0
- package/dist/rules/resolve.js +30 -0
- package/dist/rules/resolve.js.map +1 -0
- package/dist/rules/start-coordinator.d.ts +60 -0
- package/dist/rules/start-coordinator.d.ts.map +1 -0
- package/dist/rules/start-coordinator.js +194 -0
- package/dist/rules/start-coordinator.js.map +1 -0
- package/dist/rules/tools.d.ts +10 -0
- package/dist/rules/tools.d.ts.map +1 -0
- package/dist/rules/tools.js +58 -0
- package/dist/rules/tools.js.map +1 -0
- package/dist/rules/transition-service.d.ts +49 -0
- package/dist/rules/transition-service.d.ts.map +1 -0
- package/dist/rules/transition-service.js +232 -0
- package/dist/rules/transition-service.js.map +1 -0
- package/dist/rules/types.d.ts +234 -0
- package/dist/rules/types.d.ts.map +1 -0
- package/dist/rules/types.js +38 -0
- package/dist/rules/types.js.map +1 -0
- package/dist/rules/validation.d.ts +11 -0
- package/dist/rules/validation.d.ts.map +1 -0
- package/dist/rules/validation.js +308 -0
- package/dist/rules/validation.js.map +1 -0
- package/dist/sandbox/fleet.d.ts +222 -0
- package/dist/sandbox/fleet.d.ts.map +1 -0
- package/dist/sandbox/fleet.js +322 -0
- package/dist/sandbox/fleet.js.map +1 -0
- package/dist/sandbox/reattach.d.ts +3 -0
- package/dist/sandbox/reattach.d.ts.map +1 -0
- package/dist/sandbox/reattach.js +16 -0
- package/dist/sandbox/reattach.js.map +1 -0
- package/dist/server-error.d.ts +12 -0
- package/dist/server-error.d.ts.map +1 -0
- package/dist/server-error.js +16 -0
- package/dist/server-error.js.map +1 -0
- package/dist/skills/service.d.ts +48 -0
- package/dist/skills/service.d.ts.map +1 -0
- package/dist/skills/service.js +40 -0
- package/dist/skills/service.js.map +1 -0
- package/dist/spa-static.d.ts +33 -0
- package/dist/spa-static.d.ts.map +1 -0
- package/dist/spa-static.js +104 -0
- package/dist/spa-static.js.map +1 -0
- package/dist/state-signing.d.ts +59 -0
- package/dist/state-signing.d.ts.map +1 -0
- package/dist/state-signing.js +83 -0
- package/dist/state-signing.js.map +1 -0
- package/dist/storage/domains/audit/agent-audit.d.ts +27 -0
- package/dist/storage/domains/audit/agent-audit.d.ts.map +1 -0
- package/dist/storage/domains/audit/agent-audit.js +66 -0
- package/dist/storage/domains/audit/agent-audit.js.map +1 -0
- package/dist/storage/domains/audit/base.d.ts +140 -0
- package/dist/storage/domains/audit/base.d.ts.map +1 -0
- package/dist/storage/domains/audit/base.js +184 -0
- package/dist/storage/domains/audit/base.js.map +1 -0
- package/dist/storage/domains/audit/domain.d.ts +69 -0
- package/dist/storage/domains/audit/domain.d.ts.map +1 -0
- package/dist/storage/domains/audit/domain.js +167 -0
- package/dist/storage/domains/audit/domain.js.map +1 -0
- package/dist/storage/domains/channel-identity/base.d.ts +96 -0
- package/dist/storage/domains/channel-identity/base.d.ts.map +1 -0
- package/dist/storage/domains/channel-identity/base.js +149 -0
- package/dist/storage/domains/channel-identity/base.js.map +1 -0
- package/dist/storage/domains/credentials/base.d.ts +134 -0
- package/dist/storage/domains/credentials/base.d.ts.map +1 -0
- package/dist/storage/domains/credentials/base.js +273 -0
- package/dist/storage/domains/credentials/base.js.map +1 -0
- package/dist/storage/domains/custom-providers/base.d.ts +57 -0
- package/dist/storage/domains/custom-providers/base.d.ts.map +1 -0
- package/dist/storage/domains/custom-providers/base.js +145 -0
- package/dist/storage/domains/custom-providers/base.js.map +1 -0
- package/dist/storage/domains/intake/base.d.ts +38 -0
- package/dist/storage/domains/intake/base.d.ts.map +1 -0
- package/dist/storage/domains/intake/base.js +80 -0
- package/dist/storage/domains/intake/base.js.map +1 -0
- package/dist/storage/domains/integrations/base.d.ts +126 -0
- package/dist/storage/domains/integrations/base.d.ts.map +1 -0
- package/dist/storage/domains/integrations/base.js +317 -0
- package/dist/storage/domains/integrations/base.js.map +1 -0
- package/dist/storage/domains/memory-settings/base.d.ts +62 -0
- package/dist/storage/domains/memory-settings/base.d.ts.map +1 -0
- package/dist/storage/domains/memory-settings/base.js +126 -0
- package/dist/storage/domains/memory-settings/base.js.map +1 -0
- package/dist/storage/domains/model-packs/base.d.ts +49 -0
- package/dist/storage/domains/model-packs/base.d.ts.map +1 -0
- package/dist/storage/domains/model-packs/base.js +96 -0
- package/dist/storage/domains/model-packs/base.js.map +1 -0
- package/dist/storage/domains/projects/base.d.ts +58 -0
- package/dist/storage/domains/projects/base.d.ts.map +1 -0
- package/dist/storage/domains/projects/base.js +111 -0
- package/dist/storage/domains/projects/base.js.map +1 -0
- package/dist/storage/domains/queue-health/base.d.ts +52 -0
- package/dist/storage/domains/queue-health/base.d.ts.map +1 -0
- package/dist/storage/domains/queue-health/base.js +122 -0
- package/dist/storage/domains/queue-health/base.js.map +1 -0
- package/dist/storage/domains/source-control/base.d.ts +358 -0
- package/dist/storage/domains/source-control/base.d.ts.map +1 -0
- package/dist/storage/domains/source-control/base.js +908 -0
- package/dist/storage/domains/source-control/base.js.map +1 -0
- package/dist/storage/domains/source-control/inmemory.d.ts +175 -0
- package/dist/storage/domains/source-control/inmemory.d.ts.map +1 -0
- package/dist/storage/domains/source-control/inmemory.js +377 -0
- package/dist/storage/domains/source-control/inmemory.js.map +1 -0
- package/dist/storage/domains/work-items/base.d.ts +412 -0
- package/dist/storage/domains/work-items/base.d.ts.map +1 -0
- package/dist/storage/domains/work-items/base.js +1309 -0
- package/dist/storage/domains/work-items/base.js.map +1 -0
- package/dist/storage/domains/work-items/metrics.d.ts +101 -0
- package/dist/storage/domains/work-items/metrics.d.ts.map +1 -0
- package/dist/storage/domains/work-items/metrics.js +221 -0
- package/dist/storage/domains/work-items/metrics.js.map +1 -0
- 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 +24 -0
- package/dist/workspace.d.ts.map +1 -0
- package/dist/workspace.js +286 -0
- package/dist/workspace.js.map +1 -0
- package/factory-skills/configure-factory-rules/SKILL.md +67 -0
- package/factory-skills/factory-plan/SKILL.md +61 -0
- package/factory-skills/factory-review/SKILL.md +155 -0
- package/factory-skills/factory-triage/SKILL.md +77 -0
- package/package.json +81 -0
|
@@ -0,0 +1,708 @@
|
|
|
1
|
+
import { registerApiRoute } from "@mastra/core/server";
|
|
2
|
+
import { isAbsolute, join, posix, resolve, sep } from "path";
|
|
3
|
+
import { lstat, open, readdir, realpath, stat } from "fs/promises";
|
|
4
|
+
import { homedir } from "os";
|
|
5
|
+
import { SandboxFilesystem } from "@mastra/code-sdk/agents/sandbox-filesystem";
|
|
6
|
+
import { detectProject, getResourceIdOverride } from "@mastra/code-sdk/utils/project";
|
|
7
|
+
//#region src/routes/fs.ts
|
|
8
|
+
const MAX_TEXT_FILE_BYTES = 512 * 1024;
|
|
9
|
+
const MAX_DIFF_BYTES = 512 * 1024;
|
|
10
|
+
const WORKSPACE_NUMSTAT_SCRIPT = `
|
|
11
|
+
set -e
|
|
12
|
+
workdir=$1
|
|
13
|
+
index_file=$(mktemp)
|
|
14
|
+
untracked_file=$(mktemp)
|
|
15
|
+
object_dir=$(mktemp -d)
|
|
16
|
+
trap 'rm -f "$index_file" "$untracked_file"; rm -rf "$object_dir"' EXIT
|
|
17
|
+
rm -f "$index_file"
|
|
18
|
+
git -C "$workdir" diff --numstat -z --find-renames --no-ext-diff --no-textconv HEAD
|
|
19
|
+
git -C "$workdir" ls-files --others --exclude-standard -z >"$untracked_file"
|
|
20
|
+
git_dir=$(git -C "$workdir" rev-parse --absolute-git-dir)
|
|
21
|
+
export GIT_INDEX_FILE="$index_file"
|
|
22
|
+
export GIT_OBJECT_DIRECTORY="$object_dir"
|
|
23
|
+
export GIT_ALTERNATE_OBJECT_DIRECTORIES="$git_dir/objects"
|
|
24
|
+
git -C "$workdir" read-tree --empty
|
|
25
|
+
git -C "$workdir" update-index --add -z --stdin <"$untracked_file"
|
|
26
|
+
empty_tree=$(git -C "$workdir" hash-object -t tree /dev/null)
|
|
27
|
+
git -C "$workdir" diff --cached --numstat -z --no-ext-diff --no-textconv "$empty_tree"
|
|
28
|
+
`;
|
|
29
|
+
const BOUNDED_GIT_DIFF_SCRIPT = `
|
|
30
|
+
allow_exit_one=$1
|
|
31
|
+
shift
|
|
32
|
+
status_file=$(mktemp)
|
|
33
|
+
stderr_file=$(mktemp)
|
|
34
|
+
trap 'rm -f "$status_file" "$stderr_file"' EXIT
|
|
35
|
+
(
|
|
36
|
+
git "$@" 2>"$stderr_file"
|
|
37
|
+
printf '%s' "$?" >"$status_file"
|
|
38
|
+
) | head -c 524289
|
|
39
|
+
status=$(cat "$status_file")
|
|
40
|
+
case "$status" in
|
|
41
|
+
0|141) exit 0 ;;
|
|
42
|
+
1) [ "$allow_exit_one" = "1" ] && exit 0 ;;
|
|
43
|
+
esac
|
|
44
|
+
cat "$stderr_file" >&2
|
|
45
|
+
exit "\${status:-1}"
|
|
46
|
+
`;
|
|
47
|
+
const TEXT_DECODER = new TextDecoder("utf-8", { fatal: true });
|
|
48
|
+
const APPROVED_RENDERED_ROOTS = /* @__PURE__ */ new Set([".artifacts"]);
|
|
49
|
+
/** Erase a route handler's path-parameterized context to a plain `Context`. */
|
|
50
|
+
function loose(c) {
|
|
51
|
+
return c;
|
|
52
|
+
}
|
|
53
|
+
/** Resolve the browsable root, defaulting to the user's home directory. */
|
|
54
|
+
function resolveFsRoot(root) {
|
|
55
|
+
return resolve(root && root.trim() ? root : homedir());
|
|
56
|
+
}
|
|
57
|
+
/** True when `candidate` is `root` or nested under it. */
|
|
58
|
+
function isWithinRoot(candidate, root) {
|
|
59
|
+
if (candidate === root) return true;
|
|
60
|
+
const rootWithSep = root.endsWith(sep) ? root : root + sep;
|
|
61
|
+
return candidate.startsWith(rootWithSep);
|
|
62
|
+
}
|
|
63
|
+
async function realOrResolved(path) {
|
|
64
|
+
try {
|
|
65
|
+
return await realpath(path);
|
|
66
|
+
} catch {
|
|
67
|
+
return path;
|
|
68
|
+
}
|
|
69
|
+
}
|
|
70
|
+
/**
|
|
71
|
+
* Resolve a path's real location (following symlinks) and confirm it stays
|
|
72
|
+
* within `root`. Returns the real path when confined, or `null` when it escapes
|
|
73
|
+
* the root or does not exist. Used so a symlink inside the root that points
|
|
74
|
+
* outside it cannot be browsed or selected.
|
|
75
|
+
*/
|
|
76
|
+
async function realPathWithinRoot(candidate, root) {
|
|
77
|
+
try {
|
|
78
|
+
const real = await realpath(candidate);
|
|
79
|
+
return isWithinRoot(real, root) ? real : null;
|
|
80
|
+
} catch {
|
|
81
|
+
return null;
|
|
82
|
+
}
|
|
83
|
+
}
|
|
84
|
+
function assertRelativePath(path, label) {
|
|
85
|
+
const trimmed = path.trim();
|
|
86
|
+
if (!trimmed) throw new Error(`Missing required query param: ${label}`);
|
|
87
|
+
if (isAbsolute(trimmed)) throw new Error(`${label} must be relative`);
|
|
88
|
+
if (trimmed.split(/[\\/]+/).includes("..")) throw new Error(`${label} escapes workspace`);
|
|
89
|
+
const normalized = resolve("/", trimmed).slice(1);
|
|
90
|
+
if (!normalized || normalized === ".." || normalized.startsWith(`..${sep}`)) throw new Error(`${label} escapes workspace`);
|
|
91
|
+
return normalized;
|
|
92
|
+
}
|
|
93
|
+
function assertApprovedRenderedRoot(renderedRoot) {
|
|
94
|
+
const safeRoot = assertRelativePath(renderedRoot, "root");
|
|
95
|
+
if (!APPROVED_RENDERED_ROOTS.has(safeRoot)) throw new Error("Root is not approved for rendered workspace access");
|
|
96
|
+
return safeRoot;
|
|
97
|
+
}
|
|
98
|
+
async function confinedWorkspacePath(root, workspacePath) {
|
|
99
|
+
const resolvedRoot = await realOrResolved(resolveFsRoot(root));
|
|
100
|
+
const workspace = await realPathWithinRoot(isAbsolute(workspacePath) ? resolve(workspacePath) : resolve(resolvedRoot, workspacePath), resolvedRoot);
|
|
101
|
+
if (!workspace) throw new Error("Path is outside the browsable root");
|
|
102
|
+
return {
|
|
103
|
+
resolvedRoot,
|
|
104
|
+
workspace
|
|
105
|
+
};
|
|
106
|
+
}
|
|
107
|
+
async function confinedWorkspaceRelativePath(root, workspacePath, relativePath) {
|
|
108
|
+
const safeRelativePath = assertRelativePath(relativePath, "path");
|
|
109
|
+
const { workspace } = await confinedWorkspacePath(root, workspacePath);
|
|
110
|
+
const candidate = resolve(workspace, safeRelativePath);
|
|
111
|
+
if (!isWithinRoot(candidate, workspace)) throw new Error("Path escapes workspace");
|
|
112
|
+
const confinedPath = await realPathWithinRoot(candidate, workspace);
|
|
113
|
+
if (!confinedPath) throw new Error("Path is outside the workspace");
|
|
114
|
+
return {
|
|
115
|
+
workspace,
|
|
116
|
+
path: confinedPath,
|
|
117
|
+
relativePath: safeRelativePath
|
|
118
|
+
};
|
|
119
|
+
}
|
|
120
|
+
/**
|
|
121
|
+
* List the directories inside `requestedPath`, confined to `root`. An absent or
|
|
122
|
+
* out-of-root path is clamped to the root, so the worst a malicious client can
|
|
123
|
+
* do is browse within the allowed root.
|
|
124
|
+
*/
|
|
125
|
+
async function listDirectory(root, requestedPath) {
|
|
126
|
+
const resolvedRoot = await realOrResolved(resolveFsRoot(root));
|
|
127
|
+
let target = resolvedRoot;
|
|
128
|
+
if (requestedPath && requestedPath.trim()) target = await realPathWithinRoot(isAbsolute(requestedPath) ? resolve(requestedPath) : resolve(resolvedRoot, requestedPath), resolvedRoot) ?? resolvedRoot;
|
|
129
|
+
try {
|
|
130
|
+
if (!(await stat(target)).isDirectory()) target = resolvedRoot;
|
|
131
|
+
} catch {
|
|
132
|
+
target = resolvedRoot;
|
|
133
|
+
}
|
|
134
|
+
const dirents = await readdir(target, { withFileTypes: true });
|
|
135
|
+
const entries = [];
|
|
136
|
+
for (const dirent of dirents) {
|
|
137
|
+
if (dirent.name.startsWith(".")) continue;
|
|
138
|
+
const entryPath = join(target, dirent.name);
|
|
139
|
+
let isDir = dirent.isDirectory();
|
|
140
|
+
if (dirent.isSymbolicLink()) {
|
|
141
|
+
const real = await realPathWithinRoot(entryPath, resolvedRoot);
|
|
142
|
+
isDir = real ? (await stat(real).catch(() => null))?.isDirectory() === true : false;
|
|
143
|
+
}
|
|
144
|
+
if (isDir) entries.push({
|
|
145
|
+
name: dirent.name,
|
|
146
|
+
path: entryPath
|
|
147
|
+
});
|
|
148
|
+
}
|
|
149
|
+
entries.sort((a, b) => a.name.localeCompare(b.name));
|
|
150
|
+
const parent = target === resolvedRoot ? null : resolve(target, "..");
|
|
151
|
+
return {
|
|
152
|
+
root: resolvedRoot,
|
|
153
|
+
path: target,
|
|
154
|
+
parent,
|
|
155
|
+
entries
|
|
156
|
+
};
|
|
157
|
+
}
|
|
158
|
+
async function listRenderedEntries(rootPath, currentPath = rootPath) {
|
|
159
|
+
const dirents = await readdir(currentPath, { withFileTypes: true });
|
|
160
|
+
const entries = [];
|
|
161
|
+
for (const dirent of dirents) {
|
|
162
|
+
const entryPath = join(currentPath, dirent.name);
|
|
163
|
+
const info = await lstat(entryPath);
|
|
164
|
+
const relativePath = entryPath.slice(rootPath.length + 1);
|
|
165
|
+
if (info.isDirectory()) {
|
|
166
|
+
entries.push({
|
|
167
|
+
name: dirent.name,
|
|
168
|
+
path: relativePath,
|
|
169
|
+
type: "directory",
|
|
170
|
+
size: info.size,
|
|
171
|
+
updatedAt: info.mtime.toISOString()
|
|
172
|
+
});
|
|
173
|
+
entries.push(...await listRenderedEntries(rootPath, entryPath));
|
|
174
|
+
continue;
|
|
175
|
+
}
|
|
176
|
+
if (info.isFile()) entries.push({
|
|
177
|
+
name: dirent.name,
|
|
178
|
+
path: relativePath,
|
|
179
|
+
type: "file",
|
|
180
|
+
size: info.size,
|
|
181
|
+
updatedAt: info.mtime.toISOString()
|
|
182
|
+
});
|
|
183
|
+
}
|
|
184
|
+
return entries.sort((a, b) => a.path.localeCompare(b.path));
|
|
185
|
+
}
|
|
186
|
+
async function listWorkspaceRenderedPath(root, workspacePath, renderedRoot) {
|
|
187
|
+
const safeRoot = assertApprovedRenderedRoot(renderedRoot);
|
|
188
|
+
const { workspace } = await confinedWorkspacePath(root, workspacePath);
|
|
189
|
+
const renderedPath = resolve(workspace, safeRoot);
|
|
190
|
+
if (!isWithinRoot(renderedPath, workspace)) throw new Error("Root escapes workspace");
|
|
191
|
+
const confinedRootPath = await realPathWithinRoot(renderedPath, workspace);
|
|
192
|
+
if (!confinedRootPath) return {
|
|
193
|
+
workspacePath: workspace,
|
|
194
|
+
root: safeRoot,
|
|
195
|
+
rootPath: renderedPath,
|
|
196
|
+
entries: []
|
|
197
|
+
};
|
|
198
|
+
if (!(await stat(confinedRootPath)).isDirectory()) return {
|
|
199
|
+
workspacePath: workspace,
|
|
200
|
+
root: safeRoot,
|
|
201
|
+
rootPath: confinedRootPath,
|
|
202
|
+
entries: []
|
|
203
|
+
};
|
|
204
|
+
return {
|
|
205
|
+
workspacePath: workspace,
|
|
206
|
+
root: safeRoot,
|
|
207
|
+
rootPath: confinedRootPath,
|
|
208
|
+
entries: await listRenderedEntries(confinedRootPath)
|
|
209
|
+
};
|
|
210
|
+
}
|
|
211
|
+
async function readWorkspaceFile(root, workspacePath, path) {
|
|
212
|
+
assertApprovedRenderedRoot(assertRelativePath(path, "path").split("/")[0] ?? "");
|
|
213
|
+
const { workspace, path: confinedPath, relativePath } = await confinedWorkspaceRelativePath(root, workspacePath, path);
|
|
214
|
+
const info = await lstat(confinedPath);
|
|
215
|
+
if (info.isDirectory()) throw new Error("Path is a directory");
|
|
216
|
+
if (!info.isFile()) throw new Error("Unsupported file type");
|
|
217
|
+
const bytesToRead = Math.min(info.size, MAX_TEXT_FILE_BYTES);
|
|
218
|
+
const contentBuffer = Buffer.alloc(bytesToRead);
|
|
219
|
+
const handle = await open(confinedPath, "r");
|
|
220
|
+
try {
|
|
221
|
+
await handle.read(contentBuffer, 0, bytesToRead, 0);
|
|
222
|
+
} finally {
|
|
223
|
+
await handle.close();
|
|
224
|
+
}
|
|
225
|
+
try {
|
|
226
|
+
const content = TEXT_DECODER.decode(contentBuffer);
|
|
227
|
+
return {
|
|
228
|
+
workspacePath: workspace,
|
|
229
|
+
path: relativePath,
|
|
230
|
+
name: relativePath.split("/").pop() ?? relativePath,
|
|
231
|
+
size: info.size,
|
|
232
|
+
updatedAt: info.mtime.toISOString(),
|
|
233
|
+
contentType: "text",
|
|
234
|
+
content,
|
|
235
|
+
truncated: info.size > MAX_TEXT_FILE_BYTES
|
|
236
|
+
};
|
|
237
|
+
} catch {
|
|
238
|
+
return {
|
|
239
|
+
workspacePath: workspace,
|
|
240
|
+
path: relativePath,
|
|
241
|
+
name: relativePath.split("/").pop() ?? relativePath,
|
|
242
|
+
size: info.size,
|
|
243
|
+
updatedAt: info.mtime.toISOString(),
|
|
244
|
+
contentType: "unsupported"
|
|
245
|
+
};
|
|
246
|
+
}
|
|
247
|
+
}
|
|
248
|
+
async function listArtifacts(root, workspacePath) {
|
|
249
|
+
const listing = await listWorkspaceRenderedPath(root, workspacePath, ".artifacts");
|
|
250
|
+
return {
|
|
251
|
+
rootPath: listing.workspacePath,
|
|
252
|
+
artifactsPath: listing.rootPath,
|
|
253
|
+
entries: listing.entries
|
|
254
|
+
};
|
|
255
|
+
}
|
|
256
|
+
/**
|
|
257
|
+
* Resolve a `workspacePath` query param as a Factory session id. Returns the
|
|
258
|
+
* session when one exists and the caller owns it, `null` when no session
|
|
259
|
+
* matches (the caller should fall back to local-path handling), and throws
|
|
260
|
+
* when a session exists but belongs to another tenant.
|
|
261
|
+
*/
|
|
262
|
+
async function resolveAuthorizedSession(c, deps, workspacePath) {
|
|
263
|
+
if (!deps) return null;
|
|
264
|
+
const session = await deps.sessions.getBySessionId(workspacePath);
|
|
265
|
+
if (!session) return null;
|
|
266
|
+
if (deps.auth.enabled()) {
|
|
267
|
+
await deps.auth.ensureUser(c);
|
|
268
|
+
const tenant = deps.auth.tenant(c);
|
|
269
|
+
if (!tenant || tenant.orgId !== session.orgId || tenant.userId !== session.userId) throw new Error("Session is not available to the current user");
|
|
270
|
+
}
|
|
271
|
+
return session;
|
|
272
|
+
}
|
|
273
|
+
/**
|
|
274
|
+
* Reattach to the session's sandbox and wrap its workdir in a
|
|
275
|
+
* `SandboxFilesystem`. Returns `null` when the session has no provisioned
|
|
276
|
+
* sandbox yet (nothing materialized → nothing to list), or when the sandbox
|
|
277
|
+
* can no longer be reattached (e.g. torn down by the provider's idle GC).
|
|
278
|
+
* This is a passive read path, so it never re-provisions: the session's
|
|
279
|
+
* filesystem is preserved in its provider checkpoint and comes back the next
|
|
280
|
+
* time the workspace is actually opened (e.g. by sending a message).
|
|
281
|
+
*/
|
|
282
|
+
async function sessionSandbox(fleet, session) {
|
|
283
|
+
if (!fleet.enabled || !session.sandboxId || !session.sandboxWorkdir) return null;
|
|
284
|
+
let sandbox;
|
|
285
|
+
try {
|
|
286
|
+
sandbox = await fleet.reattachSandbox(session.sandboxId, { workingDirectory: session.sandboxWorkdir });
|
|
287
|
+
} catch {
|
|
288
|
+
return null;
|
|
289
|
+
}
|
|
290
|
+
return {
|
|
291
|
+
sandbox,
|
|
292
|
+
filesystem: new SandboxFilesystem({
|
|
293
|
+
sandbox,
|
|
294
|
+
workdir: session.sandboxWorkdir
|
|
295
|
+
}),
|
|
296
|
+
workdir: session.sandboxWorkdir
|
|
297
|
+
};
|
|
298
|
+
}
|
|
299
|
+
/** List an approved rendered root inside a Factory session's sandbox workdir. */
|
|
300
|
+
async function listSessionRenderedPath(fleet, session, renderedRoot) {
|
|
301
|
+
const safeRoot = assertApprovedRenderedRoot(renderedRoot);
|
|
302
|
+
const rootPath = posix.join(session.sandboxWorkdir ?? "", safeRoot);
|
|
303
|
+
const empty = {
|
|
304
|
+
workspacePath: session.sessionId,
|
|
305
|
+
root: safeRoot,
|
|
306
|
+
rootPath,
|
|
307
|
+
entries: []
|
|
308
|
+
};
|
|
309
|
+
const handle = await sessionSandbox(fleet, session);
|
|
310
|
+
if (!handle) return empty;
|
|
311
|
+
const quotedRoot = `'${rootPath.replace(/'/g, `'\\''`)}'`;
|
|
312
|
+
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 });
|
|
313
|
+
if (result.exitCode !== 0) return empty;
|
|
314
|
+
const entries = [];
|
|
315
|
+
for (const line of result.stdout.split("\n")) {
|
|
316
|
+
if (!line) continue;
|
|
317
|
+
const [type, sizeStr, mtimeStr, ...pathParts] = line.split(" ");
|
|
318
|
+
const fullPath = pathParts.join(" ");
|
|
319
|
+
if (!fullPath || !fullPath.startsWith(`${rootPath}/`)) continue;
|
|
320
|
+
const relativePath = fullPath.slice(rootPath.length + 1);
|
|
321
|
+
entries.push({
|
|
322
|
+
name: posix.basename(relativePath),
|
|
323
|
+
path: relativePath,
|
|
324
|
+
type: type === "d" ? "directory" : "file",
|
|
325
|
+
size: type === "d" ? 0 : Number(sizeStr) || 0,
|
|
326
|
+
updatedAt: (/* @__PURE__ */ new Date((Number(mtimeStr) || 0) * 1e3)).toISOString()
|
|
327
|
+
});
|
|
328
|
+
}
|
|
329
|
+
entries.sort((a, b) => a.path.localeCompare(b.path));
|
|
330
|
+
return {
|
|
331
|
+
workspacePath: session.sessionId,
|
|
332
|
+
root: safeRoot,
|
|
333
|
+
rootPath,
|
|
334
|
+
entries
|
|
335
|
+
};
|
|
336
|
+
}
|
|
337
|
+
/** Read a file under an approved rendered root inside a session's sandbox. */
|
|
338
|
+
async function readSessionWorkspaceFile(fleet, session, path) {
|
|
339
|
+
const safePath = assertRelativePath(path, "path");
|
|
340
|
+
assertApprovedRenderedRoot(safePath.split("/")[0] ?? "");
|
|
341
|
+
const handle = await sessionSandbox(fleet, session);
|
|
342
|
+
if (!handle) throw new Error("Session workspace is not available");
|
|
343
|
+
const { filesystem } = handle;
|
|
344
|
+
const info = await filesystem.stat(safePath);
|
|
345
|
+
if (info.type === "directory") throw new Error("Path is a directory");
|
|
346
|
+
const buffer = await filesystem.readFile(safePath);
|
|
347
|
+
const truncated = buffer.length > MAX_TEXT_FILE_BYTES;
|
|
348
|
+
const base = {
|
|
349
|
+
workspacePath: session.sessionId,
|
|
350
|
+
path: safePath,
|
|
351
|
+
name: posix.basename(safePath),
|
|
352
|
+
size: buffer.length,
|
|
353
|
+
updatedAt: info.modifiedAt.toISOString()
|
|
354
|
+
};
|
|
355
|
+
try {
|
|
356
|
+
const content = TEXT_DECODER.decode(truncated ? buffer.subarray(0, MAX_TEXT_FILE_BYTES) : buffer);
|
|
357
|
+
return {
|
|
358
|
+
...base,
|
|
359
|
+
contentType: "text",
|
|
360
|
+
content,
|
|
361
|
+
truncated
|
|
362
|
+
};
|
|
363
|
+
} catch {
|
|
364
|
+
return {
|
|
365
|
+
...base,
|
|
366
|
+
contentType: "unsupported"
|
|
367
|
+
};
|
|
368
|
+
}
|
|
369
|
+
}
|
|
370
|
+
function changeStatus(code) {
|
|
371
|
+
if (code === "??") return "untracked";
|
|
372
|
+
if (code.includes("U") || code === "AA" || code === "DD") return "conflicted";
|
|
373
|
+
if (code.includes("R")) return "renamed";
|
|
374
|
+
if (code.includes("C")) return "copied";
|
|
375
|
+
if (code.includes("D")) return "deleted";
|
|
376
|
+
if (code.includes("A")) return "added";
|
|
377
|
+
return "modified";
|
|
378
|
+
}
|
|
379
|
+
function parseWorkspaceChanges(output) {
|
|
380
|
+
const records = output.split("\0");
|
|
381
|
+
const changes = [];
|
|
382
|
+
for (let index = 0; index < records.length; index += 1) {
|
|
383
|
+
const record = records[index];
|
|
384
|
+
if (!record || record.length < 4) continue;
|
|
385
|
+
const code = record.slice(0, 2);
|
|
386
|
+
const path = record.slice(3);
|
|
387
|
+
const status = changeStatus(code);
|
|
388
|
+
if (status === "renamed" || status === "copied") {
|
|
389
|
+
const previousPath = records[index + 1];
|
|
390
|
+
if (previousPath) index += 1;
|
|
391
|
+
changes.push({
|
|
392
|
+
path,
|
|
393
|
+
previousPath: previousPath || void 0,
|
|
394
|
+
status
|
|
395
|
+
});
|
|
396
|
+
continue;
|
|
397
|
+
}
|
|
398
|
+
changes.push({
|
|
399
|
+
path,
|
|
400
|
+
status
|
|
401
|
+
});
|
|
402
|
+
}
|
|
403
|
+
return changes.toSorted((a, b) => a.path.localeCompare(b.path));
|
|
404
|
+
}
|
|
405
|
+
function parseWorkspaceChangeStats(output) {
|
|
406
|
+
const records = output.split("\0");
|
|
407
|
+
const stats = /* @__PURE__ */ new Map();
|
|
408
|
+
for (let index = 0; index < records.length; index += 1) {
|
|
409
|
+
const record = records[index];
|
|
410
|
+
if (!record) continue;
|
|
411
|
+
const firstTab = record.indexOf(" ");
|
|
412
|
+
const secondTab = record.indexOf(" ", firstTab + 1);
|
|
413
|
+
if (firstTab < 0 || secondTab < 0) continue;
|
|
414
|
+
const additionsText = record.slice(0, firstTab);
|
|
415
|
+
const deletionsText = record.slice(firstTab + 1, secondTab);
|
|
416
|
+
let path = record.slice(secondTab + 1);
|
|
417
|
+
if (!path) {
|
|
418
|
+
const renamedPath = records[index + 2];
|
|
419
|
+
if (!renamedPath) continue;
|
|
420
|
+
path = renamedPath;
|
|
421
|
+
index += 2;
|
|
422
|
+
}
|
|
423
|
+
if (path.startsWith("./")) path = path.slice(2);
|
|
424
|
+
if (additionsText === "-" || deletionsText === "-") {
|
|
425
|
+
stats.set(path, { binary: true });
|
|
426
|
+
continue;
|
|
427
|
+
}
|
|
428
|
+
const additions = Number(additionsText);
|
|
429
|
+
const deletions = Number(deletionsText);
|
|
430
|
+
if (Number.isFinite(additions) && Number.isFinite(deletions)) stats.set(path, {
|
|
431
|
+
additions,
|
|
432
|
+
deletions
|
|
433
|
+
});
|
|
434
|
+
}
|
|
435
|
+
return stats;
|
|
436
|
+
}
|
|
437
|
+
function unavailableWorkspaceChanges(workspacePath) {
|
|
438
|
+
return {
|
|
439
|
+
workspacePath,
|
|
440
|
+
available: false,
|
|
441
|
+
changes: []
|
|
442
|
+
};
|
|
443
|
+
}
|
|
444
|
+
async function listSessionWorkspaceChanges(fleet, session) {
|
|
445
|
+
const handle = await sessionSandbox(fleet, session);
|
|
446
|
+
if (!handle) return unavailableWorkspaceChanges(session.sessionId);
|
|
447
|
+
const [statusResult, statsResult] = await Promise.all([handle.sandbox.executeCommand("git", [
|
|
448
|
+
"-C",
|
|
449
|
+
handle.workdir,
|
|
450
|
+
"status",
|
|
451
|
+
"--porcelain=v1",
|
|
452
|
+
"-z",
|
|
453
|
+
"--untracked-files=all"
|
|
454
|
+
], { timeout: 3e4 }), handle.sandbox.executeCommand("sh", [
|
|
455
|
+
"-c",
|
|
456
|
+
WORKSPACE_NUMSTAT_SCRIPT,
|
|
457
|
+
"mastracode-numstat",
|
|
458
|
+
handle.workdir
|
|
459
|
+
], { timeout: 3e4 })]);
|
|
460
|
+
if (statusResult.exitCode !== 0) return unavailableWorkspaceChanges(session.sessionId);
|
|
461
|
+
const changes = parseWorkspaceChanges(statusResult.stdout);
|
|
462
|
+
if (statsResult.exitCode !== 0) return {
|
|
463
|
+
workspacePath: session.sessionId,
|
|
464
|
+
available: true,
|
|
465
|
+
changes
|
|
466
|
+
};
|
|
467
|
+
const stats = parseWorkspaceChangeStats(statsResult.stdout);
|
|
468
|
+
let additions = 0;
|
|
469
|
+
let deletions = 0;
|
|
470
|
+
const changesWithStats = changes.map((change) => {
|
|
471
|
+
const changeStats = stats.get(change.path);
|
|
472
|
+
additions += changeStats?.additions ?? 0;
|
|
473
|
+
deletions += changeStats?.deletions ?? 0;
|
|
474
|
+
return {
|
|
475
|
+
...change,
|
|
476
|
+
...changeStats
|
|
477
|
+
};
|
|
478
|
+
});
|
|
479
|
+
return {
|
|
480
|
+
workspacePath: session.sessionId,
|
|
481
|
+
available: true,
|
|
482
|
+
changes: changesWithStats,
|
|
483
|
+
additions,
|
|
484
|
+
deletions
|
|
485
|
+
};
|
|
486
|
+
}
|
|
487
|
+
async function executeBoundedGitDiff(sandbox, args, allowExitOne = false) {
|
|
488
|
+
return sandbox.executeCommand("sh", [
|
|
489
|
+
"-c",
|
|
490
|
+
BOUNDED_GIT_DIFF_SCRIPT,
|
|
491
|
+
"mastracode-diff",
|
|
492
|
+
allowExitOne ? "1" : "0",
|
|
493
|
+
...args
|
|
494
|
+
], { timeout: 3e4 });
|
|
495
|
+
}
|
|
496
|
+
function truncatePatch(patchBuffer) {
|
|
497
|
+
const patch = patchBuffer.subarray(0, MAX_DIFF_BYTES).toString("utf8");
|
|
498
|
+
const lastNewline = patch.lastIndexOf("\n");
|
|
499
|
+
if (lastNewline >= 0) return patch.slice(0, lastNewline + 1);
|
|
500
|
+
return patch.replace(/\uFFFD+$/, "");
|
|
501
|
+
}
|
|
502
|
+
async function readSessionWorkspaceDiff(fleet, session, path, previousPath) {
|
|
503
|
+
const safePath = assertRelativePath(path, "path");
|
|
504
|
+
const safePreviousPath = previousPath ? assertRelativePath(previousPath, "previousPath") : void 0;
|
|
505
|
+
const handle = await sessionSandbox(fleet, session);
|
|
506
|
+
if (!handle) throw new Error("Session workspace is not available");
|
|
507
|
+
const pathspecs = safePreviousPath ? [safePreviousPath, safePath] : [safePath];
|
|
508
|
+
let result = await executeBoundedGitDiff(handle.sandbox, [
|
|
509
|
+
"--literal-pathspecs",
|
|
510
|
+
"-C",
|
|
511
|
+
handle.workdir,
|
|
512
|
+
"diff",
|
|
513
|
+
"--find-renames",
|
|
514
|
+
"--no-ext-diff",
|
|
515
|
+
"--no-color",
|
|
516
|
+
"--unified=3",
|
|
517
|
+
"HEAD",
|
|
518
|
+
"--",
|
|
519
|
+
...pathspecs
|
|
520
|
+
]);
|
|
521
|
+
if (result.exitCode !== 0) throw new Error(result.stderr || "Unable to read workspace diff");
|
|
522
|
+
if (!result.stdout) {
|
|
523
|
+
const untracked = await handle.sandbox.executeCommand("git", [
|
|
524
|
+
"--literal-pathspecs",
|
|
525
|
+
"-C",
|
|
526
|
+
handle.workdir,
|
|
527
|
+
"ls-files",
|
|
528
|
+
"--others",
|
|
529
|
+
"--exclude-standard",
|
|
530
|
+
"--",
|
|
531
|
+
safePath
|
|
532
|
+
], { timeout: 3e4 });
|
|
533
|
+
if (untracked.exitCode === 0 && untracked.stdout.trim()) {
|
|
534
|
+
result = await executeBoundedGitDiff(handle.sandbox, [
|
|
535
|
+
"-C",
|
|
536
|
+
handle.workdir,
|
|
537
|
+
"diff",
|
|
538
|
+
"--no-index",
|
|
539
|
+
"--no-ext-diff",
|
|
540
|
+
"--no-color",
|
|
541
|
+
"--unified=3",
|
|
542
|
+
"--",
|
|
543
|
+
"/dev/null",
|
|
544
|
+
safePath
|
|
545
|
+
], true);
|
|
546
|
+
if (result.exitCode !== 0 && result.exitCode !== 1) throw new Error(result.stderr || "Unable to read workspace diff");
|
|
547
|
+
}
|
|
548
|
+
}
|
|
549
|
+
const patchBuffer = Buffer.from(result.stdout);
|
|
550
|
+
const truncated = patchBuffer.length > MAX_DIFF_BYTES;
|
|
551
|
+
return {
|
|
552
|
+
workspacePath: session.sessionId,
|
|
553
|
+
path: safePath,
|
|
554
|
+
patch: truncated ? truncatePatch(patchBuffer) : result.stdout,
|
|
555
|
+
truncated
|
|
556
|
+
};
|
|
557
|
+
}
|
|
558
|
+
/**
|
|
559
|
+
* Resolve a project path to the same resourceId the TUI uses. Mirrors
|
|
560
|
+
* `createMastraCode`: detect the project, then apply any resourceId override
|
|
561
|
+
* (MASTRA_RESOURCE_ID env var or `.mastracode/database.json`). This is the
|
|
562
|
+
* shared continuity point — start in the TUI, continue on the web, same path
|
|
563
|
+
* → same resourceId → same session.
|
|
564
|
+
*/
|
|
565
|
+
function resolveCodebase(projectPath) {
|
|
566
|
+
const info = detectProject(projectPath);
|
|
567
|
+
return {
|
|
568
|
+
resourceId: getResourceIdOverride(info.rootPath) ?? info.resourceId,
|
|
569
|
+
name: info.name,
|
|
570
|
+
rootPath: info.rootPath,
|
|
571
|
+
gitUrl: info.gitUrl,
|
|
572
|
+
gitBranch: info.gitBranch
|
|
573
|
+
};
|
|
574
|
+
}
|
|
575
|
+
/**
|
|
576
|
+
* Build the web filesystem routes as Mastra `apiRoutes`:
|
|
577
|
+
* - `GET /web/fs/list?path=...` — browse directories (confined to root)
|
|
578
|
+
* - `GET /web/codebase/resolve?path=...` — TUI-compatible codebase resourceId
|
|
579
|
+
*/
|
|
580
|
+
function buildFsRoutes(options = {}) {
|
|
581
|
+
const root = resolveFsRoot(options.root);
|
|
582
|
+
const sessionFs = options.sessionFs;
|
|
583
|
+
return [
|
|
584
|
+
registerApiRoute("/web/fs/list", {
|
|
585
|
+
method: "GET",
|
|
586
|
+
requiresAuth: false,
|
|
587
|
+
handler: async (c) => {
|
|
588
|
+
const path = c.req.query("path");
|
|
589
|
+
try {
|
|
590
|
+
const listing = await listDirectory(root, path);
|
|
591
|
+
return c.json(listing);
|
|
592
|
+
} catch (error) {
|
|
593
|
+
const message = error instanceof Error ? error.message : String(error);
|
|
594
|
+
return c.json({ error: message }, 500);
|
|
595
|
+
}
|
|
596
|
+
}
|
|
597
|
+
}),
|
|
598
|
+
registerApiRoute("/web/artifacts/list", {
|
|
599
|
+
method: "GET",
|
|
600
|
+
requiresAuth: false,
|
|
601
|
+
handler: async (c) => {
|
|
602
|
+
const path = c.req.query("path");
|
|
603
|
+
if (!path) return c.json({ error: "Missing required query param: path" }, 400);
|
|
604
|
+
try {
|
|
605
|
+
return c.json(await listArtifacts(root, path));
|
|
606
|
+
} catch (error) {
|
|
607
|
+
const message = error instanceof Error ? error.message : String(error);
|
|
608
|
+
const status = message === "Path is outside the browsable root" ? 403 : 500;
|
|
609
|
+
return c.json({ error: message }, status);
|
|
610
|
+
}
|
|
611
|
+
}
|
|
612
|
+
}),
|
|
613
|
+
registerApiRoute("/web/workspace/rendered/list", {
|
|
614
|
+
method: "GET",
|
|
615
|
+
requiresAuth: false,
|
|
616
|
+
handler: async (c) => {
|
|
617
|
+
const workspacePath = c.req.query("workspacePath");
|
|
618
|
+
const renderedRoot = c.req.query("root");
|
|
619
|
+
if (!workspacePath) return c.json({ error: "Missing required query param: workspacePath" }, 400);
|
|
620
|
+
if (!renderedRoot) return c.json({ error: "Missing required query param: root" }, 400);
|
|
621
|
+
try {
|
|
622
|
+
const session = await resolveAuthorizedSession(loose(c), sessionFs, workspacePath);
|
|
623
|
+
if (session && sessionFs) return c.json(await listSessionRenderedPath(sessionFs.fleet, session, renderedRoot));
|
|
624
|
+
return c.json(await listWorkspaceRenderedPath(root, workspacePath, renderedRoot));
|
|
625
|
+
} catch (error) {
|
|
626
|
+
const message = error instanceof Error ? error.message : String(error);
|
|
627
|
+
const status = message.includes("outside") || message.includes("relative") || message.includes("escapes") || message.includes("not approved") || message.includes("not available") ? 403 : 500;
|
|
628
|
+
return c.json({ error: message }, status);
|
|
629
|
+
}
|
|
630
|
+
}
|
|
631
|
+
}),
|
|
632
|
+
registerApiRoute("/web/workspace/changes", {
|
|
633
|
+
method: "GET",
|
|
634
|
+
requiresAuth: false,
|
|
635
|
+
handler: async (c) => {
|
|
636
|
+
const workspacePath = c.req.query("workspacePath");
|
|
637
|
+
if (!workspacePath) return c.json({ error: "Missing required query param: workspacePath" }, 400);
|
|
638
|
+
try {
|
|
639
|
+
const session = await resolveAuthorizedSession(loose(c), sessionFs, workspacePath);
|
|
640
|
+
if (!session || !sessionFs) return c.json(unavailableWorkspaceChanges(workspacePath));
|
|
641
|
+
return c.json(await listSessionWorkspaceChanges(sessionFs.fleet, session));
|
|
642
|
+
} catch (error) {
|
|
643
|
+
const message = error instanceof Error ? error.message : String(error);
|
|
644
|
+
return c.json({ error: message }, message.includes("not available") ? 403 : 500);
|
|
645
|
+
}
|
|
646
|
+
}
|
|
647
|
+
}),
|
|
648
|
+
registerApiRoute("/web/workspace/changes/diff", {
|
|
649
|
+
method: "GET",
|
|
650
|
+
requiresAuth: false,
|
|
651
|
+
handler: async (c) => {
|
|
652
|
+
const workspacePath = c.req.query("workspacePath");
|
|
653
|
+
const path = c.req.query("path");
|
|
654
|
+
const previousPath = c.req.query("previousPath");
|
|
655
|
+
if (!workspacePath) return c.json({ error: "Missing required query param: workspacePath" }, 400);
|
|
656
|
+
if (!path) return c.json({ error: "Missing required query param: path" }, 400);
|
|
657
|
+
try {
|
|
658
|
+
const session = await resolveAuthorizedSession(loose(c), sessionFs, workspacePath);
|
|
659
|
+
if (!session || !sessionFs) return c.json({ error: "Session workspace is not available" }, 403);
|
|
660
|
+
return c.json(await readSessionWorkspaceDiff(sessionFs.fleet, session, path, previousPath));
|
|
661
|
+
} catch (error) {
|
|
662
|
+
const message = error instanceof Error ? error.message : String(error);
|
|
663
|
+
const status = message.includes("relative") || message.includes("escapes") || message.includes("not available") ? 403 : 500;
|
|
664
|
+
return c.json({ error: message }, status);
|
|
665
|
+
}
|
|
666
|
+
}
|
|
667
|
+
}),
|
|
668
|
+
registerApiRoute("/web/workspace/file", {
|
|
669
|
+
method: "GET",
|
|
670
|
+
requiresAuth: false,
|
|
671
|
+
handler: async (c) => {
|
|
672
|
+
const workspacePath = c.req.query("workspacePath");
|
|
673
|
+
const path = c.req.query("path");
|
|
674
|
+
if (!workspacePath) return c.json({ error: "Missing required query param: workspacePath" }, 400);
|
|
675
|
+
if (!path) return c.json({ error: "Missing required query param: path" }, 400);
|
|
676
|
+
try {
|
|
677
|
+
const session = await resolveAuthorizedSession(loose(c), sessionFs, workspacePath);
|
|
678
|
+
if (session && sessionFs) return c.json(await readSessionWorkspaceFile(sessionFs.fleet, session, path));
|
|
679
|
+
return c.json(await readWorkspaceFile(root, workspacePath, path));
|
|
680
|
+
} catch (error) {
|
|
681
|
+
const message = error instanceof Error ? error.message : String(error);
|
|
682
|
+
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;
|
|
683
|
+
return c.json({ error: message }, status);
|
|
684
|
+
}
|
|
685
|
+
}
|
|
686
|
+
}),
|
|
687
|
+
registerApiRoute("/web/codebase/resolve", {
|
|
688
|
+
method: "GET",
|
|
689
|
+
requiresAuth: false,
|
|
690
|
+
handler: async (c) => {
|
|
691
|
+
const path = c.req.query("path");
|
|
692
|
+
if (!path) return c.json({ error: "Missing required query param: path" }, 400);
|
|
693
|
+
const confined = await realPathWithinRoot(isAbsolute(path) ? resolve(path) : resolve(root, path), root);
|
|
694
|
+
if (!confined) return c.json({ error: "Path is outside the browsable root" }, 403);
|
|
695
|
+
try {
|
|
696
|
+
return c.json(resolveCodebase(confined));
|
|
697
|
+
} catch (error) {
|
|
698
|
+
const message = error instanceof Error ? error.message : String(error);
|
|
699
|
+
return c.json({ error: message }, 500);
|
|
700
|
+
}
|
|
701
|
+
}
|
|
702
|
+
})
|
|
703
|
+
];
|
|
704
|
+
}
|
|
705
|
+
//#endregion
|
|
706
|
+
export { buildFsRoutes, listArtifacts, listDirectory, listSessionRenderedPath, listSessionWorkspaceChanges, listWorkspaceRenderedPath, parseWorkspaceChangeStats, parseWorkspaceChanges, readSessionWorkspaceDiff, readSessionWorkspaceFile, readWorkspaceFile, resolveCodebase, resolveFsRoot };
|
|
707
|
+
|
|
708
|
+
//# sourceMappingURL=fs.js.map
|