@mastra/deployer 1.83.0-alpha.2 → 1.83.0-alpha.3

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.
@@ -3,7 +3,7 @@ name: mastra-deployer
3
3
  description: Documentation for @mastra/deployer. Use when working with @mastra/deployer APIs, configuration, or implementation.
4
4
  metadata:
5
5
  package: "@mastra/deployer"
6
- version: "1.83.0-alpha.2"
6
+ version: "1.83.0-alpha.3"
7
7
  ---
8
8
 
9
9
  ## When to use
@@ -1,5 +1,5 @@
1
1
  {
2
- "version": "1.83.0-alpha.2",
2
+ "version": "1.83.0-alpha.3",
3
3
  "package": "@mastra/deployer",
4
4
  "exports": {
5
5
  "Deployer": {
@@ -149,7 +149,15 @@ To add your own endpoints, see [Custom API Routes](https://mastra.ai/docs/server
149
149
 
150
150
  By default, the generated server handles `SIGINT` and `SIGTERM`. It stops accepting connections, waits up to [`server.drainTimeout`](https://mastra.ai/reference/configuration) for active requests and streams, then runs `mastra.shutdown()`. Shutdown gives in-flight workflow runs (including durable agent runs) the same drain window to finish before workers and pub/sub subscriptions are torn down. A durable agent run still executing after the window stops and releases its claim on the run, so the next boot can recover it right away. The drain timeout defaults to 5 seconds. A second signal terminates the process immediately. See [`server.handleShutdownSignals`](https://mastra.ai/reference/configuration) if you need to manage signals yourself.
151
151
 
152
- Increase `drainTimeout` when your hosting platform's termination grace period can accommodate longer turns. The HTTP drain and the workflow drain run one after the other, so keep enough time for both plus shutdown cleanup.
152
+ Shutdown then hands back the agent thread runs the process still holds:
153
+
154
+ - A run still executing is aborted, so it starts no further steps. Clients watching the thread from other processes see it end, and its partial response isn't saved.
155
+ - A run waiting on a tool approval or suspension stays suspended and can be resumed from another process. A tool call from the same step that's still executing is aborted first, like a run still executing.
156
+ - The thread lease is released right away, so another process can start a run on the thread without waiting for the lease to expire. Release is best-effort: if an aborted run doesn't stop in time (for example, a tool call that ignores the abort), or the lease backend doesn't answer or fails the release, the lease is kept until it expires.
157
+
158
+ Handing runs back can take up to about 1.5 seconds past the drain timeout.
159
+
160
+ Increase `drainTimeout` when your hosting platform's termination grace period can accommodate longer turns. The HTTP drain and the workflow drain run one after the other, so keep enough time for both, plus about 1.5 seconds to hand back thread runs, plus shutdown cleanup.
153
161
 
154
162
  If you call `mastra.shutdown()` yourself, pass `drainTimeout` to control how long it waits for in-flight workflow runs:
155
163
 
@@ -167,7 +175,7 @@ export const mastra = new Mastra({
167
175
  })
168
176
  ```
169
177
 
170
- A plain `agent.stream()` call can't resume after its server process exits. If a stream ends without its expected terminal event, treat the turn as interrupted and let the client retry or reconcile it. Use [durable agents](https://mastra.ai/docs/harness/durable-agents) when turns must survive process replacement. [Crash recovery](https://mastra.ai/docs/harness/durable-agents) requires shared persistent run storage and idempotent tools. Replaying missed events after a restart also requires a shared persistent cache such as Redis because the default event cache is in-memory. Recovering from multiple replicas requires a storage adapter that supports run fencing or a PubSub backend whose lease every replica shares. See [multi-instance deployments](https://mastra.ai/docs/harness/durable-agents).
178
+ A plain `agent.stream()` call can't resume after its server process exits. If a stream ends without its expected terminal event, treat the turn as interrupted and let the client retry or reconcile it. Use [durable agents](https://mastra.ai/docs/harness/durable-agents) when turns must survive process replacement. [Crash recovery](https://mastra.ai/docs/harness/durable-agents) requires shared persistent run storage and [idempotent tools](https://mastra.ai/docs/harness/durable-agents). Replaying missed events after a restart also requires a shared persistent cache such as Redis because the default event cache is in-memory. Recovering from multiple replicas requires a storage adapter that supports run fencing or a PubSub backend whose lease every replica shares. See [multi-instance deployments](https://mastra.ai/docs/harness/durable-agents).
171
179
 
172
180
  ## Troubleshooting
173
181
 
@@ -3614,6 +3614,27 @@ const BODY_METHODS = /* @__PURE__ */ new Set([
3614
3614
  "PATCH",
3615
3615
  "DELETE"
3616
3616
  ]);
3617
+ /**
3618
+ * Cancels a response body the runtime never started reading once the request aborts. @hono/node-server skips the body
3619
+ * of a request that closed while the route handler ran without cancelling it. A body the runtime already reads is
3620
+ * locked, and the runtime cancels that one itself on disconnect.
3621
+ *
3622
+ * `body` is only read after the abort: reading it earlier turns off @hono/node-server's fast path for string and byte
3623
+ * bodies, which is what sets `Content-Length` and the default content type.
3624
+ */
3625
+ function cancelUnreadBodyOnAbort(response, signal) {
3626
+ const cancel = () => {
3627
+ let body;
3628
+ try {
3629
+ body = response.body;
3630
+ } catch {
3631
+ return;
3632
+ }
3633
+ if (body && !body.locked) body.cancel("request aborted").catch(() => {});
3634
+ };
3635
+ if (signal.aborted) cancel();
3636
+ else signal.addEventListener("abort", cancel, { once: true });
3637
+ }
3617
3638
  var MastraServer = class extends _mastra_server_server_adapter.MastraServer {
3618
3639
  createContextMiddleware() {
3619
3640
  return async (c, next) => {
@@ -3675,12 +3696,16 @@ var MastraServer = class extends _mastra_server_server_adapter.MastraServer {
3675
3696
  } else res.header("Content-Type", "text/plain");
3676
3697
  res.header("Transfer-Encoding", "chunked");
3677
3698
  return (0, hono_streaming.stream)(res, async (stream) => {
3678
- if (streamFormat === "sse" && route.sseFlushOnConnect) await stream.write(": connected\n\n");
3679
3699
  const reader = (result instanceof ReadableStream ? result : result.fullStream).getReader();
3680
3700
  stream.onAbort(() => {
3681
3701
  reader.cancel("request aborted").catch(() => {});
3682
3702
  });
3703
+ const signal = res.req.raw.signal;
3704
+ const abortStream = () => stream.abort();
3705
+ if (signal.aborted) abortStream();
3706
+ else signal.addEventListener("abort", abortStream, { once: true });
3683
3707
  try {
3708
+ if (streamFormat === "sse" && route.sseFlushOnConnect) await stream.write(": connected\n\n");
3684
3709
  while (true) {
3685
3710
  const { done, value } = await reader.read();
3686
3711
  if (done) break;
@@ -3710,6 +3735,7 @@ var MastraServer = class extends _mastra_server_server_adapter.MastraServer {
3710
3735
  stack: error.stack
3711
3736
  } : error });
3712
3737
  } finally {
3738
+ signal.removeEventListener("abort", abortStream);
3713
3739
  await stream.close();
3714
3740
  }
3715
3741
  }, async (err) => {
@@ -3785,8 +3811,11 @@ var MastraServer = class extends _mastra_server_server_adapter.MastraServer {
3785
3811
  }
3786
3812
  if (route.responseType === "json") return response.json(result, 200);
3787
3813
  else if (route.responseType === "stream") return this.stream(route, response, result);
3788
- else if (route.responseType === "datastream-response") return result;
3789
- else if (route.responseType === "mcp-http") {
3814
+ else if (route.responseType === "datastream-response") {
3815
+ const fetchResponse = result;
3816
+ cancelUnreadBodyOnAbort(fetchResponse, response.req.raw.signal);
3817
+ return fetchResponse;
3818
+ } else if (route.responseType === "mcp-http") {
3790
3819
  const { server, httpPath, mcpOptions: routeMcpOptions } = result;
3791
3820
  const { req, res } = toReqRes(response.req.raw);
3792
3821
  const { setRequestAuth, ...options } = {
@@ -4859,8 +4888,9 @@ const DEFAULT_CORS_ALLOW_HEADERS = [
4859
4888
  const DEFAULT_CORS_EXPOSE_HEADERS = ["Content-Length", "X-Requested-With"];
4860
4889
  function getCorsConfig(serverCors, credentialsDefault) {
4861
4890
  const userCors = serverCors && typeof serverCors === "object" ? serverCors : void 0;
4862
- const origin = userCors && "origin" in userCors && userCors.origin ? userCors.origin : credentialsDefault ? (requestOrigin) => requestOrigin || void 0 : "*";
4863
- const credentials = userCors && "credentials" in userCors ? userCors.credentials : credentialsDefault;
4891
+ const hasExplicitOrigin = !!(userCors && "origin" in userCors && userCors.origin);
4892
+ const origin = hasExplicitOrigin ? userCors.origin : credentialsDefault ? (requestOrigin) => requestOrigin || void 0 : "*";
4893
+ const credentials = userCors && "credentials" in userCors ? userCors.credentials : credentialsDefault && hasExplicitOrigin;
4864
4894
  return {
4865
4895
  origin,
4866
4896
  allowMethods: DEFAULT_CORS_ALLOW_METHODS,
@@ -5171,7 +5201,7 @@ async function createNodeServer(mastra, options = { tools: {} }) {
5171
5201
  import("@mastra/core/telemetry").then(({ syncUsageTelemetry }) => syncUsageTelemetry(mastra)).catch(() => {});
5172
5202
  import("@mastra/core/telemetry").then(({ syncFeatureUsageTelemetry }) => syncFeatureUsageTelemetry(mastra)).catch(() => {});
5173
5203
  if (serverOptions?.handleShutdownSignals !== false) {
5174
- const CORE_SHUTDOWN_TIMEOUT_MS = 5e3;
5204
+ const CORE_SHUTDOWN_TIMEOUT_MS = 6500;
5175
5205
  const timedOut = Symbol("shutdown-timeout");
5176
5206
  const race = async (work, ms) => {
5177
5207
  let timeout;