@mastra/platform-workspace 1.13.0 → 1.13.1-alpha.0

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.
@@ -22,6 +22,8 @@ export interface CommandsApi {
22
22
  stdin?: boolean;
23
23
  cwd?: string;
24
24
  envs?: Record<string, string>;
25
+ /** OS user to run the command as. Omitted when the provider has no user configured. */
26
+ user?: string;
25
27
  }): Promise<CommandsApiHandle>;
26
28
  connect(pid: number, opts?: CommandsApiStreamOptions): Promise<CommandsApiHandle>;
27
29
  /** Running commands only. */
@@ -68,6 +70,12 @@ export interface CommandsApiProcessManagerOptions {
68
70
  env?: Record<string, string | undefined>;
69
71
  /** Default timeout in milliseconds for commands that don't specify one. */
70
72
  defaultTimeout?: number;
73
+ /**
74
+ * OS user every command this manager starts runs as. Providers pass the user
75
+ * their sandbox is configured with; leaving it unset keeps the provider's own
76
+ * default user.
77
+ */
78
+ user?: string;
71
79
  }
72
80
  /**
73
81
  * A process manager over E2B's envd commands API. Providers supply the
@@ -75,7 +83,9 @@ export interface CommandsApiProcessManagerOptions {
75
83
  */
76
84
  export declare abstract class CommandsApiProcessManager<TSandbox extends MastraSandbox = MastraSandbox> extends SandboxProcessManager<TSandbox> {
77
85
  protected readonly defaultTimeout?: number;
78
- constructor({ defaultTimeout, ...options }?: CommandsApiProcessManagerOptions);
86
+ /** Read by {@link CommandsApiProcessHandle.kill}, which must run as the same user. */
87
+ readonly defaultUser?: string;
88
+ constructor({ defaultTimeout, user, ...options }?: CommandsApiProcessManagerOptions);
79
89
  /** Run `fn` against the provider's current commands handle. */
80
90
  abstract runCommands<T>(operation: CommandsApiOperation, fn: (commands: CommandsApi) => Promise<T>): Promise<T>;
81
91
  /** Wrap a started or attached command. Providers override this to supply their own handle. */
package/dist/index.cjs CHANGED
@@ -1172,7 +1172,8 @@ var CommandsApiProcessHandle = class extends _mastra_core_workspace.ProcessHandl
1172
1172
  await (await commands.run(killDescendants(this.inner.pid), {
1173
1173
  background: true,
1174
1174
  stdin: false,
1175
- timeoutMs: KILL_DESCENDANTS_TIMEOUT_MS
1175
+ timeoutMs: KILL_DESCENDANTS_TIMEOUT_MS,
1176
+ ...this.manager.defaultUser !== void 0 && { user: this.manager.defaultUser }
1176
1177
  })).wait();
1177
1178
  } catch {}
1178
1179
  return commands.kill(this.inner.pid);
@@ -1193,9 +1194,12 @@ var CommandsApiProcessHandle = class extends _mastra_core_workspace.ProcessHandl
1193
1194
  */
1194
1195
  var CommandsApiProcessManager = class extends _mastra_core_workspace.SandboxProcessManager {
1195
1196
  defaultTimeout;
1196
- constructor({ defaultTimeout, ...options } = {}) {
1197
+ /** Read by {@link CommandsApiProcessHandle.kill}, which must run as the same user. */
1198
+ defaultUser;
1199
+ constructor({ defaultTimeout, user, ...options } = {}) {
1197
1200
  super(options);
1198
1201
  this.defaultTimeout = defaultTimeout;
1202
+ this.defaultUser = user;
1199
1203
  }
1200
1204
  /** Wrap a started or attached command. Providers override this to supply their own handle. */
1201
1205
  createHandle(inner, stdinMode, options) {
@@ -1215,6 +1219,7 @@ var CommandsApiProcessManager = class extends _mastra_core_workspace.SandboxProc
1215
1219
  cwd: options.cwd ?? this.defaultCwd(),
1216
1220
  envs,
1217
1221
  timeoutMs: options.timeout ?? this.defaultTimeout,
1222
+ ...this.defaultUser !== void 0 && { user: this.defaultUser },
1218
1223
  onStdout: output.onStdout,
1219
1224
  onStderr: output.onStderr
1220
1225
  });
@@ -1804,6 +1809,29 @@ function shellQuote(arg) {
1804
1809
  if (/^[a-zA-Z0-9._\-/=:@]+$/.test(arg)) return arg;
1805
1810
  return `'${arg.replace(/'/g, `'\\''`)}'`;
1806
1811
  }
1812
+ /**
1813
+ * `command` run from `cwd` by a leading shell `cd`, with the transports left to
1814
+ * start in the working directory. The exec transports report a `cwd` that
1815
+ * doesn't exist as a broken connection (retried, then "transport failed"), so
1816
+ * changing directory in the shell turns it into an ordinary failed command.
1817
+ *
1818
+ * A relative `cwd` resolves under `workingDirectory`. An absolute one outside it
1819
+ * that doesn't exist falls back to the same path under `workingDirectory`: the
1820
+ * workspace file tools read absolute paths that way (`/repo` is
1821
+ * `<workingDirectory>/repo`), so agents pass command `cwd`s the same way.
1822
+ */
1823
+ function commandInCwd(command, cwd, workingDirectory) {
1824
+ if (!cwd) return command;
1825
+ const normalize = (dir) => path.posix.normalize(dir).replace(/(.)\/$/, "$1");
1826
+ const base = workingDirectory ? normalize(workingDirectory) : void 0;
1827
+ const target = normalize(base && !cwd.startsWith("/") ? path.posix.join(base, cwd) : cwd);
1828
+ if (target === base) return command;
1829
+ const candidates = [target];
1830
+ if (base && cwd.startsWith("/") && !target.startsWith(`${base}/`)) candidates.push(path.posix.join(base, target));
1831
+ const branches = candidates.map((dir, i) => `${i === 0 ? "if" : "elif"} [ -d ${shellQuote(dir)} ]; then cd -- ${shellQuote(dir)} || exit 1`);
1832
+ const notFound = shellQuote(`Working directory not found: ${target}${candidates.length > 1 ? ` (also tried ${candidates[1]})` : ""}`);
1833
+ return `${branches.join("; ")}; else echo ${notFound} >&2; exit 1; fi\n${command}`;
1834
+ }
1807
1835
  var PlatformProcessHandle = class extends _mastra_core_workspace.ProcessHandle {
1808
1836
  pid;
1809
1837
  resultPromise;
@@ -1865,6 +1893,10 @@ const withCommands = Symbol("withCommands");
1865
1893
  * process runs, `kill` stops it, and `list` reports what is running in the VM.
1866
1894
  */
1867
1895
  var PlatformCommandsProcessManager = class extends CommandsApiProcessManager {
1896
+ async spawn(command, options = {}) {
1897
+ const { cwd, ...rest } = options;
1898
+ return super.spawn(commandInCwd(command, cwd, this.sandbox.workingDirectory), rest);
1899
+ }
1868
1900
  runCommands(operation, fn) {
1869
1901
  return this.sandbox[withCommands](operation, fn);
1870
1902
  }
@@ -2624,17 +2656,19 @@ var PlatformSandbox = class PlatformSandbox extends _mastra_core_workspace.Mastr
2624
2656
  const fullCommand = buildCommand(command, args);
2625
2657
  const effectiveTimeout = options?.timeout ?? this._timeout;
2626
2658
  const sandboxEnv = this.getEnv();
2659
+ const { cwd, ...restOptions } = options ?? {};
2660
+ const execCommand = commandInCwd(fullCommand, cwd, this.workingDirectory);
2627
2661
  const execCallOptions = Object.keys(sandboxEnv).length > 0 ? {
2628
- ...options,
2662
+ ...restOptions,
2629
2663
  env: {
2630
2664
  ...sandboxEnv,
2631
2665
  ...options?.env
2632
2666
  }
2633
- } : options;
2667
+ } : restOptions;
2634
2668
  await this._awaitTransportReady();
2635
2669
  const instanceUrl = once ? void 0 : this._addressRegistry?.get(this._sandboxId);
2636
2670
  if (instanceUrl) {
2637
- const privateNet = await this._tryExecViaPrivateNetwork(instanceUrl, fullCommand, effectiveTimeout, execCallOptions);
2671
+ const privateNet = await this._tryExecViaPrivateNetwork(instanceUrl, execCommand, effectiveTimeout, execCallOptions);
2638
2672
  if (privateNet) {
2639
2673
  const privateExit = privateNet.exitCode ?? 124;
2640
2674
  return {
@@ -2648,7 +2682,7 @@ var PlatformSandbox = class PlatformSandbox extends _mastra_core_workspace.Mastr
2648
2682
  };
2649
2683
  }
2650
2684
  }
2651
- const result = await this._runDirectExec(fullCommand, effectiveTimeout, execCallOptions, once ? 1 : 2);
2685
+ const result = await this._runDirectExec(execCommand, effectiveTimeout, execCallOptions, once ? 1 : 2);
2652
2686
  const exitCode = result.exitCode ?? 124;
2653
2687
  return {
2654
2688
  success: exitCode === 0,