@mastra/docker 0.9.2 → 0.9.3-alpha.1

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.cjs CHANGED
@@ -27,6 +27,7 @@ let _mastra_core_workspace = require("@mastra/core/workspace");
27
27
  let dockerode = require("dockerode");
28
28
  dockerode = __toESM(dockerode, 1);
29
29
  let tar_stream = require("tar-stream");
30
+ let string_decoder = require("string_decoder");
30
31
  let path_posix = require("path/posix");
31
32
  path_posix = __toESM(path_posix, 1);
32
33
  let _grpc_grpc_js = require("@grpc/grpc-js");
@@ -114,6 +115,8 @@ ret=$?
114
115
  rm -f "$1" 2>/dev/null
115
116
  exit $ret
116
117
  `;
118
+ /** KILL_SCRIPT exit code when no PGID file was readable or it was empty. */
119
+ const NO_PGID_EXIT_CODE = 3;
117
120
  /**
118
121
  * Kill script: read the recorded PGID and SIGKILL the whole process group.
119
122
  * A negative PID targets the kernel-owned process group, so descendants that
@@ -129,11 +132,12 @@ i=0
129
132
  while [ ! -r "$f" ] && [ "$i" -lt 40 ]; do sleep 0.05; i=$((i + 1)); done
130
133
  # If the PGID was never recorded (file absent/unreadable after the wait, or
131
134
  # empty), we have no group to signal or verify — report failure rather than
132
- # falsely claiming the tree was terminated.
133
- [ -r "$f" ] || exit 1
135
+ # falsely claiming the tree was terminated. Exit code 3 marks this case so
136
+ # kill() can tell it apart from a failed termination (exit 1).
137
+ [ -r "$f" ] || exit 3
134
138
  pgid=$(cat "$f" 2>/dev/null)
135
139
  rm -f "$f" 2>/dev/null
136
- [ -n "$pgid" ] || exit 1
140
+ [ -n "$pgid" ] || exit 3
137
141
  kill -STOP -"$pgid" 2>/dev/null
138
142
  kill -KILL -"$pgid" 2>/dev/null
139
143
  # Fallback for images without setsid: the leader is not a group leader, so also
@@ -310,7 +314,13 @@ var DockerProcessHandle = class extends _mastra_core_workspace.ProcessHandle {
310
314
  await new Promise((resolve) => setTimeout(resolve, 10));
311
315
  killInfo = await killExec.inspect();
312
316
  }
313
- if (killInfo.ExitCode !== 0) throw new Error(`kill helper exited with code ${killInfo.ExitCode}`);
317
+ if (killInfo.ExitCode !== 0) {
318
+ if (killInfo.ExitCode === NO_PGID_EXIT_CODE) {
319
+ const selfInfo = await this._exec.inspect().catch(() => void 0);
320
+ if (selfInfo && !selfInfo.Running) return false;
321
+ }
322
+ throw new Error(`kill helper exited with code ${killInfo.ExitCode}`);
323
+ }
314
324
  this._killed = true;
315
325
  this._destroyStream();
316
326
  return true;
@@ -399,6 +409,8 @@ var DockerProcessManager = class extends _mastra_core_workspace.SandboxProcessMa
399
409
  handle._setExecStream(stream);
400
410
  const waitPromise = new Promise((resolve) => {
401
411
  const buffer = [];
412
+ const stdoutDecoder = new string_decoder.StringDecoder("utf8");
413
+ const stderrDecoder = new string_decoder.StringDecoder("utf8");
402
414
  stream.on("data", (chunk) => {
403
415
  buffer.push(chunk);
404
416
  let combined = Buffer.concat(buffer);
@@ -410,17 +422,32 @@ var DockerProcessManager = class extends _mastra_core_workspace.SandboxProcessMa
410
422
  buffer.push(combined);
411
423
  break;
412
424
  }
413
- const payload = combined.subarray(8, 8 + size).toString("utf-8");
414
- if (type === 1) handle.emitStdout(payload);
415
- else if (type === 2) handle.emitStderr(payload);
425
+ const payload = combined.subarray(8, 8 + size);
426
+ if (type === 1) {
427
+ const text = stdoutDecoder.write(payload);
428
+ if (text) handle.emitStdout(text);
429
+ } else if (type === 2) {
430
+ const text = stderrDecoder.write(payload);
431
+ if (text) handle.emitStderr(text);
432
+ }
416
433
  combined = combined.subarray(8 + size);
417
434
  }
418
435
  if (combined.length > 0 && buffer.length === 0) buffer.push(combined);
419
436
  });
437
+ let decodersFlushed = false;
438
+ const flushDecoders = () => {
439
+ if (decodersFlushed) return;
440
+ decodersFlushed = true;
441
+ const stdoutRest = stdoutDecoder.end();
442
+ if (stdoutRest) handle.emitStdout(stdoutRest);
443
+ const stderrRest = stderrDecoder.end();
444
+ if (stderrRest) handle.emitStderr(stderrRest);
445
+ };
420
446
  let settled = false;
421
447
  const settle = (exitCode, metadata = {}) => {
422
448
  if (settled) return;
423
449
  settled = true;
450
+ flushDecoders();
424
451
  handle._setExitCode(exitCode);
425
452
  resolve({
426
453
  success: exitCode === 0,
@@ -478,6 +505,7 @@ var DockerProcessManager = class extends _mastra_core_workspace.SandboxProcessMa
478
505
  settleTerminated();
479
506
  return;
480
507
  }
508
+ flushDecoders();
481
509
  settle(1, { stderr: handle.stderr || "Stream error" });
482
510
  });
483
511
  });