@mastra/platform-workspace 1.5.0-alpha.2 → 1.5.0-alpha.4

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/dist/index.js CHANGED
@@ -4,6 +4,7 @@ import { FileExistsError, FileNotFoundError, MastraFilesystem, MastraSandbox, Pr
4
4
  import { CommandExitError, Sandbox, TimeoutError } from "e2b";
5
5
  import { execFile } from "child_process";
6
6
  import { promisify } from "util";
7
+ import { createHash } from "crypto";
7
8
  //#region src/client.ts
8
9
  const DEFAULT_PROXY_URL = "https://workspaces.mastra.ai";
9
10
  /**
@@ -1940,6 +1941,31 @@ var PlatformSandbox = class PlatformSandbox extends MastraSandbox {
1940
1941
  }
1941
1942
  };
1942
1943
  //#endregion
1944
+ //#region ../../packages/_internals/workspace/dist/index.js
1945
+ /**
1946
+ * Setup completion marker shared by repo templates and their consumers.
1947
+ *
1948
+ * A repo template writes this file beside the checkout as its last build
1949
+ * step, so it exists only in images where every setup command succeeded. Its
1950
+ * content is a digest of the setup commands the image ran, letting a sandbox
1951
+ * booted from the image tell whether the setup it is about to run already
1952
+ * happened. Relative to the template's build cwd, which is also the runtime
1953
+ * working directory the repo was cloned into.
1954
+ */
1955
+ const SETUP_MARKER_PATH = ".mastra-sandbox/setup";
1956
+ /** Blank entries never become build steps, so they never count toward the digest either. */
1957
+ function normalizeSetupCommands(setupCommand) {
1958
+ return (setupCommand === void 0 ? [] : Array.isArray(setupCommand) ? setupCommand : [setupCommand]).filter((command) => command.trim() !== "");
1959
+ }
1960
+ /** The marker content for a setup command list: `sha256:<hex>` over the commands joined by newlines. */
1961
+ function setupMarkerContent(setupCommand) {
1962
+ return `sha256:${createHash("sha256").update(normalizeSetupCommands(setupCommand).join("\n")).digest("hex")}`;
1963
+ }
1964
+ /** Shell step that writes the marker relative to the cwd. `content` is a digest, so it is shell-safe. */
1965
+ function setupMarkerCommand(content) {
1966
+ return `mkdir -p "$(dirname "${SETUP_MARKER_PATH}")" && printf '%s' '${content}' > "${SETUP_MARKER_PATH}"`;
1967
+ }
1968
+ //#endregion
1943
1969
  //#region src/repo-template.ts
1944
1970
  const execFileAsync = promisify(execFile);
1945
1971
  const SHA_PATTERN = /^[0-9a-f]{7,40}$/i;
@@ -2022,6 +2048,7 @@ function createRepoTemplate(options) {
2022
2048
  if (workingDirectory) template = template.runCmd(`mkdir -p "${workingDirectory}"`).setWorkdir(workingDirectory);
2023
2049
  template = template.runCmd(`git ${auth}clone ${cloneUrl} "${repoDir}"`).runCmd(`git -C "${repoDir}" ${auth}fetch origin ${sha}`).runCmd(`git -C "${repoDir}" checkout ${sha}`);
2024
2050
  for (const command of setupCommands) template = template.runCmd(`cd "${repoDir}" && ${command}`);
2051
+ template = template.runCmd(setupMarkerCommand(setupMarkerContent(setupCommands)));
2025
2052
  return template.withFamily(family);
2026
2053
  };
2027
2054
  }
@@ -2079,7 +2106,50 @@ function redactSecrets(value) {
2079
2106
  function gitAuthFlag() {
2080
2107
  return `-c http.extraheader="AUTHORIZATION: basic $(printf 'x-access-token:%s' "$${BUILD_TOKEN_ENV}" | base64 -w0)"`;
2081
2108
  }
2082
- async function resolveDefaultBranchHead(cloneUrl, token, execute = execFileAsync) {
2109
+ /**
2110
+ * `owner/repo` for a github.com clone URL, else undefined. Only the public
2111
+ * host is API-resolvable: GitHub Enterprise and other forges keep the git
2112
+ * path below.
2113
+ */
2114
+ function parseGithubRepo(cloneUrl) {
2115
+ let url;
2116
+ try {
2117
+ url = new URL(cloneUrl);
2118
+ } catch {
2119
+ return;
2120
+ }
2121
+ if (url.hostname.toLowerCase() !== "github.com") return void 0;
2122
+ const [owner, repo, ...rest] = url.pathname.split("/").filter(Boolean);
2123
+ if (!owner || !repo || rest.length > 0) return void 0;
2124
+ return {
2125
+ owner,
2126
+ repo: repo.replace(/\.git$/i, "")
2127
+ };
2128
+ }
2129
+ /**
2130
+ * Resolve the default-branch head. github.com repositories go through the
2131
+ * REST API so resolution works wherever the host runs, including deployed
2132
+ * images without a git binary; other hosts shell out to `git ls-remote`.
2133
+ * Throws with a redaction-safe message when the head cannot be resolved.
2134
+ */
2135
+ async function resolveDefaultBranchHead(cloneUrl, token, execute = execFileAsync, fetchImpl = fetch) {
2136
+ const github = parseGithubRepo(cloneUrl);
2137
+ if (github) {
2138
+ const response = await fetchImpl(`https://api.github.com/repos/${github.owner}/${github.repo}/commits/HEAD`, {
2139
+ headers: {
2140
+ Accept: "application/vnd.github.sha",
2141
+ "X-GitHub-Api-Version": "2022-11-28",
2142
+ "User-Agent": "mastra-platform-workspace",
2143
+ ...token ? { Authorization: `Bearer ${token}` } : {}
2144
+ },
2145
+ signal: AbortSignal.timeout(1e4)
2146
+ }).catch((error) => {
2147
+ throw new Error(`GitHub head lookup failed: ${error instanceof Error ? error.message : String(error)}`);
2148
+ });
2149
+ if (!response.ok) throw new Error(`GitHub head lookup failed: ${response.status} ${response.statusText}`.trim());
2150
+ const sha = (await response.text()).trim();
2151
+ return SHA_PATTERN.test(sha) ? sha : void 0;
2152
+ }
2083
2153
  try {
2084
2154
  const env = {
2085
2155
  ...process.env,