@mastra/server-adapters-test-suite 0.3.0-alpha.3 → 0.3.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
@@ -27,12 +27,16 @@ import { LocalFilesystem, Workspace } from "@mastra/core/workspace";
27
27
  import { MCPClient, MCPServer, defineEvent } from "@mastra/mcp";
28
28
  import { AGENT_STREAM_TOPIC, DurableStepIds, createDurableAgent, createDurableAgentStream, createEventedAgent } from "@mastra/core/agent/durable";
29
29
  import { Mastra as Mastra$1 } from "@mastra/core/mastra";
30
- import http from "http";
30
+ import http, { createServer, request } from "http";
31
31
  import { MCPServerBase } from "@mastra/core/mcp";
32
32
  import { RequestContext } from "@mastra/core/request-context";
33
33
  import { standardSchemaToJSONSchema } from "@mastra/core/schema";
34
34
  import { z as z$2 } from "zod/v4";
35
35
  import { Knowledge } from "@mastra/core/knowledge";
36
+ import { once } from "events";
37
+ import { DefaultObservabilityInstance, MastraPlatformExporter, MastraStorageExporter, Observability } from "@mastra/observability";
38
+ import { setFlagsFromString } from "v8";
39
+ import { runInNewContext } from "vm";
36
40
  // src/route-test-utils.ts
37
41
  /**
38
42
  * Normalizes a route path to ensure consistent formatting.
@@ -59,6 +63,17 @@ function normalizeRoutePath(path) {
59
63
  * Provide valid schema values for routes whose fields have cross-field constraints.
60
64
  */
61
65
  function getRouteSpecificSchemaDefaults(route) {
66
+ if (route.path === "/observability/feedback/:feedbackId" && route.method === "PUT") return { body: {
67
+ contextMode: "provided",
68
+ feedback: {
69
+ feedbackId: "test-feedback-replacement",
70
+ traceId: "test-trace",
71
+ timestamp: "2026-01-01T00:00:00.000Z",
72
+ feedbackSource: "user",
73
+ feedbackType: "comment",
74
+ value: "replacement feedback"
75
+ }
76
+ } };
62
77
  if (route.path === "/observability/traces/query/values") return { body: {
63
78
  predicateScope: "trace",
64
79
  path: "entityName"
@@ -147,7 +162,7 @@ function generateValidDataFromSchema(schema, fieldName) {
147
162
  return def.defaultValue();
148
163
  }
149
164
  if (typeName === "ZodString") {
150
- if ((def.checks ?? []).some((check) => {
165
+ if (def.format === "datetime" || (def.checks ?? []).some((check) => {
151
166
  const checkDef = check._zod?.def ?? check.def ?? check;
152
167
  return check.kind === "datetime" || check.format === "datetime" || checkDef.format === "datetime";
153
168
  })) {
@@ -328,7 +343,7 @@ function getDefaultValidPathParams(route) {
328
343
  if (route.path.includes(":scorerId") && route.path.includes("/stored/scorers/")) params.scorerId = "test-stored-scorer";
329
344
  else if (route.path.includes(":scorerId")) params.scorerId = "test-scorer";
330
345
  if (route.path.includes(":scoreId")) params.scoreId = "test-score";
331
- if (route.path.includes(":feedbackId")) params.feedbackId = "test-feedback";
346
+ if (route.path.includes(":feedbackId")) params.feedbackId = route.method === "PUT" ? "test-feedback-replacement" : "test-feedback";
332
347
  if (route.path.includes(":traceId")) params.traceId = "test-trace";
333
348
  if (route.path.includes(":runId")) params.runId = "test-run";
334
349
  if (route.path.includes(":stepId")) params.stepId = "test-step";
@@ -6685,6 +6700,172 @@ function createObservabilitySignalWriteTestSuite({ startServer }) {
6685
6700
  expect(response.status).toBe(200);
6686
6701
  return (await response.json())[collection];
6687
6702
  };
6703
+ if (collection === "feedback") {
6704
+ const original = {
6705
+ feedbackId: "edit",
6706
+ traceId: "trace-123",
6707
+ timestamp: "2026-11-11T12:00:00.123Z",
6708
+ feedbackType: "thumbs",
6709
+ value: 1
6710
+ };
6711
+ const put = (feedback, contextMode) => server.request(`${path}/edit`, {
6712
+ method: "PUT",
6713
+ headers: {
6714
+ "Content-Type": "application/json",
6715
+ authorization: "Bearer test-token"
6716
+ },
6717
+ body: JSON.stringify({
6718
+ feedback,
6719
+ contextMode
6720
+ })
6721
+ });
6722
+ it.each([
6723
+ { feedbackId: "other" },
6724
+ { timestamp: void 0 },
6725
+ { traceId: " " }
6726
+ ])("rejects malformed replacements: %j", async (change) => {
6727
+ expect((await put({
6728
+ ...original,
6729
+ ...change
6730
+ })).status).toBe(400);
6731
+ expect(await list()).toHaveLength(0);
6732
+ });
6733
+ it.each([{ traceId: "different" }, { timestamp: "2026-11-12T12:00:00.123Z" }])("rejects changing feedback identity: %j", async (change) => {
6734
+ await post(original);
6735
+ expect((await put({
6736
+ ...original,
6737
+ ...change
6738
+ })).status).toBe(409);
6739
+ expect(await list()).toEqual([expect.objectContaining({
6740
+ value: 1,
6741
+ traceId: original.traceId,
6742
+ timestamp: original.timestamp
6743
+ })]);
6744
+ });
6745
+ it("returns the stored timestamp on a duplicate POST so the next PUT succeeds", async () => {
6746
+ expect((await post(original)).status).toBe(200);
6747
+ const retry = await post({
6748
+ ...original,
6749
+ timestamp: void 0,
6750
+ value: -1
6751
+ });
6752
+ expect(retry.status).toBe(200);
6753
+ const { data } = await retry.json();
6754
+ expect(data).toMatchObject({
6755
+ feedbackId: original.feedbackId,
6756
+ timestamp: original.timestamp
6757
+ });
6758
+ expect((await store.getFeedbackById(original.feedbackId))?.value).toBe(1);
6759
+ expect((await put({
6760
+ ...original,
6761
+ ...data,
6762
+ value: -1
6763
+ })).status).toBe(200);
6764
+ expect((await store.getFeedbackById(original.feedbackId))?.value).toBe(-1);
6765
+ });
6766
+ it("does not disclose another tenant's feedback identity on a duplicate POST", async () => {
6767
+ await post({
6768
+ ...original,
6769
+ organizationId: "other"
6770
+ });
6771
+ trustedScope.organizationId = "trusted";
6772
+ expect((await post({
6773
+ ...original,
6774
+ organizationId: "other"
6775
+ })).status).toBe(404);
6776
+ expect((await store.getFeedbackById(original.feedbackId))?.value).toBe(1);
6777
+ });
6778
+ it("returns 404 instead of overwriting feedback from another tenant", async () => {
6779
+ await post({
6780
+ ...original,
6781
+ organizationId: "other"
6782
+ });
6783
+ trustedScope.organizationId = "trusted";
6784
+ expect((await put({
6785
+ ...original,
6786
+ organizationId: "other",
6787
+ value: -1
6788
+ })).status).toBe(404);
6789
+ expect((await store.getFeedbackById(original.feedbackId))?.value).toBe(1);
6790
+ });
6791
+ it("uses the authenticated feedback author and clears omitted replacement fields", async () => {
6792
+ await post({
6793
+ ...original,
6794
+ comment: "old",
6795
+ feedbackUserId: "old-author"
6796
+ });
6797
+ trustedScope.mastra__user = { id: "reviewer" };
6798
+ expect((await put({
6799
+ ...original,
6800
+ value: -1,
6801
+ feedbackUserId: "spoofed"
6802
+ })).status).toBe(200);
6803
+ const saved = await store.getFeedbackById(original.feedbackId);
6804
+ expect(saved).toMatchObject({
6805
+ value: -1,
6806
+ feedbackUserId: "reviewer"
6807
+ });
6808
+ expect(saved?.comment ?? null).toBeNull();
6809
+ });
6810
+ it("returns 501 with an older storage package and keeps existing feedback", async () => {
6811
+ await post(original);
6812
+ Object.defineProperty(store, "replaceFeedback", { value: void 0 });
6813
+ expect((await put({
6814
+ ...original,
6815
+ value: -1
6816
+ })).status).toBe(501);
6817
+ expect((await store.getFeedbackById(original.feedbackId))?.value).toBe(1);
6818
+ });
6819
+ it("allows replacement before create and requires the local anchor in resolve mode", async () => {
6820
+ expect((await put({
6821
+ ...original,
6822
+ value: -1
6823
+ }, "resolve")).status).toBe(404);
6824
+ expect((await put({
6825
+ ...original,
6826
+ value: -1
6827
+ }, "provided")).status).toBe(200);
6828
+ expect((await post(original)).status).toBe(200);
6829
+ expect((await store.getFeedbackById(original.feedbackId))?.value).toBe(-1);
6830
+ });
6831
+ it("replaces feedback without allowing a delayed create to undo the edit", async () => {
6832
+ const original = {
6833
+ feedbackId: "replace-me",
6834
+ traceId: "trace-123",
6835
+ timestamp: "2026-11-11T12:00:00.123Z",
6836
+ feedbackType: "comment",
6837
+ value: "original",
6838
+ comment: "old comment"
6839
+ };
6840
+ expect((await post(original)).status).toBe(200);
6841
+ const response = await server.request("/api/observability/feedback/replace-me", {
6842
+ method: "PUT",
6843
+ headers: {
6844
+ "Content-Type": "application/json",
6845
+ authorization: "Bearer test-token"
6846
+ },
6847
+ body: JSON.stringify({ feedback: {
6848
+ ...original,
6849
+ value: "edited",
6850
+ comment: null
6851
+ } })
6852
+ });
6853
+ expect(response.status, await response.clone().text()).toBe(200);
6854
+ expect(await response.json()).toMatchObject({
6855
+ success: true,
6856
+ data: {
6857
+ feedbackId: "replace-me",
6858
+ timestamp: original.timestamp
6859
+ }
6860
+ });
6861
+ expect((await post(original)).status).toBe(200);
6862
+ expect(await list()).toEqual([expect.objectContaining({
6863
+ feedbackId: "replace-me",
6864
+ value: "edited",
6865
+ comment: null
6866
+ })]);
6867
+ });
6868
+ }
6688
6869
  describe("single-record reads", () => {
6689
6870
  it("returns the persisted record with its identity and timestamp", async () => {
6690
6871
  const id = "single-record";
@@ -7089,7 +7270,547 @@ function createObservabilitySignalWriteTestSuite({ startServer }) {
7089
7270
  })]);
7090
7271
  });
7091
7272
  });
7273
+ describe.each(["feedback", "scores"])("%s HTTP export delivery", (collection) => {
7274
+ it.each(["flush", "shutdown"])("returns HTTP success before a custom exporter completes and drains delivery on %s", async (lifecycle) => {
7275
+ const accepted = [];
7276
+ const flushed = [];
7277
+ let release;
7278
+ const gate = new Promise((resolve) => {
7279
+ release = resolve;
7280
+ });
7281
+ const receiver = createServer(async (req, res) => {
7282
+ const chunks = [];
7283
+ for await (const chunk of req) chunks.push(Buffer.from(chunk));
7284
+ const body = JSON.parse(Buffer.concat(chunks).toString());
7285
+ if (req.url === "/accept") {
7286
+ accepted.push(body);
7287
+ await gate;
7288
+ } else flushed.push(...body);
7289
+ res.writeHead(204).end();
7290
+ });
7291
+ receiver.listen(0, "127.0.0.1");
7292
+ await once(receiver, "listening");
7293
+ const address = receiver.address();
7294
+ if (!address || typeof address === "string") throw new Error("Expected TCP listener");
7295
+ const endpoint = `http://127.0.0.1:${address.port}`;
7296
+ const buffered = [];
7297
+ const deliver = async (event) => {
7298
+ await fetch(`${endpoint}/accept`, {
7299
+ method: "POST",
7300
+ body: JSON.stringify(event)
7301
+ });
7302
+ buffered.push(event);
7303
+ };
7304
+ const flush = async () => {
7305
+ if (buffered.length) await fetch(`${endpoint}/flush`, {
7306
+ method: "POST",
7307
+ body: JSON.stringify(buffered.splice(0))
7308
+ });
7309
+ };
7310
+ const delegate = new DefaultObservabilityInstance({
7311
+ name: "custom",
7312
+ serviceName: "slow-custom-export",
7313
+ exporters: [{
7314
+ name: "http-buffer",
7315
+ exportTracingEvent: async () => {},
7316
+ onScoreEvent: deliver,
7317
+ onFeedbackEvent: deliver,
7318
+ flush,
7319
+ shutdown: flush
7320
+ }]
7321
+ });
7322
+ const custom = {
7323
+ getConfig: () => delegate.getConfig(),
7324
+ getExporters: () => delegate.getExporters(),
7325
+ getSpanOutputProcessors: () => delegate.getSpanOutputProcessors(),
7326
+ getLogger: () => delegate.getLogger(),
7327
+ getBridge: () => delegate.getBridge(),
7328
+ startSpan: (options) => delegate.startSpan(options),
7329
+ rebuildSpan: (cached) => delegate.rebuildSpan(cached),
7330
+ __setLogger: (logger) => delegate.__setLogger(logger),
7331
+ flush: () => delegate.flush(),
7332
+ shutdown: () => delegate.shutdown()
7333
+ };
7334
+ const observability = new Observability({});
7335
+ observability.registerInstance("custom", custom, true);
7336
+ const storage = new InMemoryStore();
7337
+ const server = await startServer(new Mastra({
7338
+ storage,
7339
+ observability,
7340
+ logger: false
7341
+ }));
7342
+ const isFeedback = collection === "feedback";
7343
+ const field = isFeedback ? "feedback" : "score";
7344
+ const idField = isFeedback ? "feedbackId" : "scoreId";
7345
+ try {
7346
+ const response = await server.request(`/api/observability/${collection}`, {
7347
+ method: "POST",
7348
+ headers: { "Content-Type": "application/json" },
7349
+ signal: AbortSignal.timeout(2e3),
7350
+ body: JSON.stringify({
7351
+ contextMode: "provided",
7352
+ [field]: {
7353
+ [idField]: "slow-export",
7354
+ traceId: "local-trace",
7355
+ ...isFeedback ? {
7356
+ feedbackType: "comment",
7357
+ value: "useful"
7358
+ } : {
7359
+ scorerId: "quality",
7360
+ score: 1
7361
+ }
7362
+ }
7363
+ })
7364
+ });
7365
+ expect(response.status, await response.clone().text()).toBe(200);
7366
+ await expect.poll(() => accepted.length).toBe(1);
7367
+ const store = await storage.getStore("observability");
7368
+ const rows = isFeedback ? (await store.listFeedback({})).feedback : (await store.listScores({})).scores;
7369
+ expect(rows).toEqual([expect.objectContaining({ [idField]: "slow-export" })]);
7370
+ let completed = false;
7371
+ const draining = observability[lifecycle]().then(() => {
7372
+ completed = true;
7373
+ });
7374
+ await new Promise((resolve) => setTimeout(resolve, 25));
7375
+ expect(completed).toBe(false);
7376
+ expect(flushed).toHaveLength(0);
7377
+ release();
7378
+ await draining;
7379
+ expect(flushed).toEqual(accepted);
7380
+ expect(flushed).toHaveLength(1);
7381
+ } finally {
7382
+ release();
7383
+ await observability.shutdown();
7384
+ await server.close();
7385
+ receiver.closeAllConnections();
7386
+ await new Promise((resolve) => receiver.close(() => resolve()));
7387
+ }
7388
+ });
7389
+ it.each([
7390
+ {
7391
+ contextMode: "provided",
7392
+ authenticated: true,
7393
+ emptyContext: false
7394
+ },
7395
+ {
7396
+ contextMode: "resolve",
7397
+ authenticated: true,
7398
+ emptyContext: false
7399
+ },
7400
+ {
7401
+ contextMode: "provided",
7402
+ authenticated: false,
7403
+ emptyContext: false
7404
+ },
7405
+ {
7406
+ contextMode: "provided",
7407
+ authenticated: false,
7408
+ emptyContext: true
7409
+ }
7410
+ ].flatMap((testCase) => (collection === "feedback" ? ["POST", "PUT"] : ["POST"]).map((method) => ({
7411
+ ...testCase,
7412
+ method
7413
+ }))))("persists and exports the same $method $contextMode record (authenticated: $authenticated, empty context: $emptyContext) without a second storage write", async ({ contextMode, authenticated, emptyContext, method }) => {
7414
+ const received = [];
7415
+ const receiver = createServer(async (req, res) => {
7416
+ const chunks = [];
7417
+ for await (const chunk of req) chunks.push(Buffer.from(chunk));
7418
+ received.push({
7419
+ path: req.url,
7420
+ method: req.method,
7421
+ body: JSON.parse(Buffer.concat(chunks).toString())
7422
+ });
7423
+ res.writeHead(204).end();
7424
+ });
7425
+ receiver.listen(0, "127.0.0.1");
7426
+ await once(receiver, "listening");
7427
+ const address = receiver.address();
7428
+ if (!address || typeof address === "string") throw new Error("Expected TCP listener");
7429
+ const storage = new InMemoryStore();
7430
+ const store = await storage.getStore("observability");
7431
+ const isFeedback = collection === "feedback";
7432
+ const field = isFeedback ? "feedback" : "score";
7433
+ const idField = isFeedback ? "feedbackId" : "scoreId";
7434
+ const batch = vi.spyOn(store, isFeedback ? "batchCreateFeedback" : "batchCreateScores");
7435
+ const observability = new Observability({ configs: { default: {
7436
+ serviceName: "http-create-test",
7437
+ exporters: [new MastraStorageExporter(), new MastraPlatformExporter({
7438
+ endpoint: `http://127.0.0.1:${address.port}`,
7439
+ accessToken: "test",
7440
+ resolveFromEnv: false
7441
+ })]
7442
+ } } });
7443
+ const server = await startServer(new Mastra({
7444
+ storage,
7445
+ observability,
7446
+ logger: false,
7447
+ server: { auth: {
7448
+ authenticateToken: async () => ({}),
7449
+ authorize: async (_path, _method, _user, c) => {
7450
+ if (!emptyContext) c.get("requestContext").set("organizationId", "trusted-org");
7451
+ if (authenticated) c.get("requestContext").set("mastra__user", { id: "reviewer" });
7452
+ return true;
7453
+ }
7454
+ } }
7455
+ }));
7456
+ try {
7457
+ if (contextMode === "resolve") await store.createSpan({ span: {
7458
+ traceId: "http-trace",
7459
+ spanId: "root",
7460
+ parentSpanId: null,
7461
+ name: "root",
7462
+ spanType: SpanType.AGENT_RUN,
7463
+ isEvent: false,
7464
+ startedAt: /* @__PURE__ */ new Date(),
7465
+ endedAt: /* @__PURE__ */ new Date(),
7466
+ organizationId: "trusted-org",
7467
+ userId: "span-user",
7468
+ tags: ["stored-tag"],
7469
+ metadata: { inherited: true }
7470
+ } });
7471
+ const timestamp = "2026-11-11T10:00:00.123Z";
7472
+ const response = await server.request(`/api/observability/${collection}${method === "PUT" ? "/http-record" : ""}`, {
7473
+ method,
7474
+ headers: {
7475
+ "Content-Type": "application/json",
7476
+ authorization: "Bearer test"
7477
+ },
7478
+ body: JSON.stringify({
7479
+ contextMode,
7480
+ [field]: {
7481
+ [idField]: "http-record",
7482
+ timestamp,
7483
+ traceId: "http-trace",
7484
+ ...!emptyContext ? {
7485
+ organizationId: "untrusted-org",
7486
+ userId: "span-user",
7487
+ tags: ["provided-tag"]
7488
+ } : {},
7489
+ metadata: {
7490
+ explicit: true,
7491
+ userId: "metadata-user"
7492
+ },
7493
+ ...isFeedback ? {
7494
+ feedbackType: "comment",
7495
+ value: "actionable",
7496
+ feedbackUserId: authenticated ? "spoofed" : null,
7497
+ reviewStatus: "reviewed"
7498
+ } : {
7499
+ scorerId: "quality",
7500
+ score: .9
7501
+ }
7502
+ }
7503
+ })
7504
+ });
7505
+ expect(response.status, await response.clone().text()).toBe(200);
7506
+ expect(await response.json()).toMatchObject({
7507
+ success: true,
7508
+ data: {
7509
+ [idField]: "http-record",
7510
+ timestamp
7511
+ }
7512
+ });
7513
+ if (isFeedback && method === "POST") {
7514
+ const retry = await server.request("/api/observability/feedback", {
7515
+ method: "POST",
7516
+ headers: {
7517
+ "Content-Type": "application/json",
7518
+ authorization: "Bearer test"
7519
+ },
7520
+ body: JSON.stringify({
7521
+ contextMode: "provided",
7522
+ feedback: {
7523
+ feedbackId: "http-record",
7524
+ traceId: "http-trace",
7525
+ feedbackType: "comment",
7526
+ value: "discarded"
7527
+ }
7528
+ })
7529
+ });
7530
+ expect(retry.status).toBe(200);
7531
+ expect(await retry.json()).toMatchObject({ data: {
7532
+ feedbackId: "http-record",
7533
+ timestamp
7534
+ } });
7535
+ }
7536
+ await observability.shutdown();
7537
+ const rows = isFeedback ? (await store.listFeedback({})).feedback : (await store.listScores({})).scores;
7538
+ expect(rows).toHaveLength(1);
7539
+ expect(batch).not.toHaveBeenCalled();
7540
+ const expectedContext = emptyContext ? {} : {
7541
+ organizationId: "trusted-org",
7542
+ userId: "span-user",
7543
+ tags: [contextMode === "resolve" ? "stored-tag" : "provided-tag"]
7544
+ };
7545
+ expect(rows[0]).toMatchObject({
7546
+ [idField]: "http-record",
7547
+ timestamp: /* @__PURE__ */ new Date(timestamp),
7548
+ ...expectedContext
7549
+ });
7550
+ const expectedPath = method === "PUT" ? "/api/observability/feedback/http-record" : `/ai/${collection}/publish`;
7551
+ const writes = received.filter((item) => item.path === expectedPath);
7552
+ expect(writes).toHaveLength(1);
7553
+ expect(writes[0].method).toBe(method);
7554
+ if (method === "POST") {
7555
+ expect(writes[0].body[collection]).toHaveLength(isFeedback ? 2 : 1);
7556
+ if (isFeedback) expect(writes[0].body.feedback[1]).toEqual(writes[0].body.feedback[0]);
7557
+ }
7558
+ const exported = method === "PUT" ? writes[0].body.feedback : writes[0].body[collection][0];
7559
+ expect(exported).toMatchObject({
7560
+ [idField]: "http-record",
7561
+ timestamp,
7562
+ traceId: "http-trace",
7563
+ correlationContext: expectedContext,
7564
+ metadata: { explicit: true }
7565
+ });
7566
+ if (emptyContext) expect(exported.correlationContext).toEqual({});
7567
+ if (contextMode === "resolve") expect(exported.metadata).toMatchObject({ inherited: true });
7568
+ if (isFeedback) {
7569
+ expect(rows[0]).toMatchObject({
7570
+ feedbackUserId: authenticated ? "reviewer" : null,
7571
+ reviewStatus: "reviewed"
7572
+ });
7573
+ expect(exported).toMatchObject({
7574
+ feedbackUserId: authenticated ? "reviewer" : null,
7575
+ reviewStatus: "reviewed"
7576
+ });
7577
+ expect(exported).not.toHaveProperty("userId");
7578
+ }
7579
+ } finally {
7580
+ await observability.shutdown();
7581
+ await server.close();
7582
+ receiver.closeAllConnections();
7583
+ await new Promise((resolve) => receiver.close(() => resolve()));
7584
+ }
7585
+ });
7586
+ });
7587
+ }
7588
+ // src/custom-route-disconnect-test-suite.ts
7589
+ /**
7590
+ * OpenAI-compatible chat completions server that streams text deltas until the
7591
+ * caller closes the connection, like a long model turn.
7592
+ */
7593
+ async function startFakeLLM() {
7594
+ let resolveStreaming;
7595
+ let resolveAborted;
7596
+ const streaming = new Promise((resolve) => {
7597
+ resolveStreaming = resolve;
7598
+ });
7599
+ const aborted = new Promise((resolve) => {
7600
+ resolveAborted = resolve;
7601
+ });
7602
+ const server = createServer((req, res) => {
7603
+ req.resume();
7604
+ req.on("end", () => {
7605
+ res.writeHead(200, { "content-type": "text/event-stream" });
7606
+ const timer = setInterval(() => {
7607
+ res.write(`data: ${JSON.stringify({
7608
+ id: "chatcmpl-1",
7609
+ object: "chat.completion.chunk",
7610
+ created: 0,
7611
+ model: "gpt-4o-mini",
7612
+ choices: [{
7613
+ index: 0,
7614
+ delta: { content: "tick " },
7615
+ finish_reason: null
7616
+ }]
7617
+ })}\n\n`);
7618
+ }, 25);
7619
+ res.on("close", () => {
7620
+ clearInterval(timer);
7621
+ if (!res.writableFinished) resolveAborted();
7622
+ });
7623
+ resolveStreaming();
7624
+ });
7625
+ });
7626
+ server.listen(0, "127.0.0.1");
7627
+ await once(server, "listening");
7628
+ const { port } = server.address();
7629
+ return {
7630
+ url: `http://127.0.0.1:${port}/v1`,
7631
+ streaming,
7632
+ aborted,
7633
+ close: async () => {
7634
+ server.closeAllConnections();
7635
+ await new Promise((resolve) => server.close(() => resolve()));
7636
+ }
7637
+ };
7638
+ }
7639
+ /** Rejects with a named error if `promise` does not settle within `ms`. */
7640
+ function withDeadline(promise, ms, what) {
7641
+ let timer;
7642
+ const deadline = new Promise((_, reject) => {
7643
+ timer = setTimeout(() => reject(/* @__PURE__ */ new Error(`Timed out after ${ms}ms waiting for ${what}`)), ms);
7644
+ });
7645
+ return Promise.race([promise, deadline]).finally(() => clearTimeout(timer));
7646
+ }
7647
+ let gcFn;
7648
+ /**
7649
+ * Forces full garbage collections, the way a long-lived server process
7650
+ * eventually does while a response is streaming. Uses `--expose-gc` when the
7651
+ * process already has it, and otherwise enables it at runtime.
7652
+ */
7653
+ async function collectGarbage() {
7654
+ if (!gcFn) {
7655
+ const exposed = globalThis.gc;
7656
+ if (typeof exposed === "function") gcFn = exposed;
7657
+ else {
7658
+ setFlagsFromString("--expose-gc");
7659
+ gcFn = runInNewContext("gc");
7660
+ }
7661
+ }
7662
+ for (let i = 0; i < 5; i++) {
7663
+ gcFn();
7664
+ await new Promise((resolve) => setTimeout(resolve, 10));
7665
+ }
7666
+ }
7667
+ /**
7668
+ * A custom route that streams an agent run must stop that run, and its model
7669
+ * request, when the client disconnects. Adapters re-dispatch custom routes
7670
+ * through an inner `Request` whose signal follows the client connection.
7671
+ */
7672
+ function createCustomRouteDisconnectTestSuite(config) {
7673
+ const { suiteName = "Custom route client disconnect", startServer } = config;
7674
+ describe(suiteName, () => {
7675
+ const cleanups = [];
7676
+ afterEach(async () => {
7677
+ for (const cleanup of cleanups.splice(0).reverse()) await cleanup();
7678
+ });
7679
+ it("stops the agent run and its model request when the client disconnects mid-stream after a GC (#29154)", async () => {
7680
+ const llm = await startFakeLLM();
7681
+ cleanups.push(llm.close);
7682
+ const agent = new Agent({
7683
+ id: "chat-agent",
7684
+ name: "chat-agent",
7685
+ instructions: "You are a test agent.",
7686
+ model: {
7687
+ id: "openai/gpt-4o-mini",
7688
+ url: llm.url,
7689
+ apiKey: "test-key"
7690
+ }
7691
+ });
7692
+ const mastra = new Mastra$1({
7693
+ logger: false,
7694
+ agents: { agent },
7695
+ server: { apiRoutes: [registerApiRoute("/chat", {
7696
+ method: "POST",
7697
+ requiresAuth: false,
7698
+ handler: async (c) => {
7699
+ const result = await c.get("mastra").getAgent("agent").stream("hi", { abortSignal: c.req.raw.signal });
7700
+ return new Response(result.textStream.pipeThrough(new TextEncoderStream()), { headers: { "content-type": "text/plain; charset=utf-8" } });
7701
+ }
7702
+ })] }
7703
+ });
7704
+ const server = await startServer(mastra);
7705
+ cleanups.push(server.close);
7706
+ let clientRequest;
7707
+ const firstChunk = new Promise((resolve, reject) => {
7708
+ clientRequest = request(`${server.baseUrl}/chat`, {
7709
+ method: "POST",
7710
+ headers: { "content-type": "application/json" }
7711
+ }, (res) => {
7712
+ res.on("error", () => {});
7713
+ if (res.statusCode !== 200) {
7714
+ res.resume();
7715
+ reject(/* @__PURE__ */ new Error(`Custom route responded with status ${res.statusCode}`));
7716
+ return;
7717
+ }
7718
+ res.once("data", () => resolve(res));
7719
+ res.once("end", () => reject(/* @__PURE__ */ new Error("Custom route response ended before its first chunk")));
7720
+ });
7721
+ clientRequest.on("error", reject);
7722
+ clientRequest.end(JSON.stringify({}));
7723
+ });
7724
+ cleanups.push(async () => {
7725
+ clientRequest?.destroy();
7726
+ });
7727
+ const response = await withDeadline(firstChunk, 5e3, "the first response chunk");
7728
+ expect(response.statusCode).toBe(200);
7729
+ await withDeadline(llm.streaming, 5e3, "the model request to start streaming");
7730
+ await collectGarbage();
7731
+ clientRequest.on("error", () => {});
7732
+ clientRequest.destroy();
7733
+ const outcome = await Promise.race([llm.aborted.then(() => "model request aborted"), new Promise((resolve) => setTimeout(() => resolve("model request still running"), 3e3))]);
7734
+ expect(outcome).toBe("model request aborted");
7735
+ }, 2e4);
7736
+ it.each([{
7737
+ outcome: "completes",
7738
+ failsMidStream: false
7739
+ }, {
7740
+ outcome: "fails mid-stream",
7741
+ failsMidStream: true
7742
+ }])("releases the request of a custom route once its streamed response $outcome (#29154)", async ({ failsMidStream }) => {
7743
+ const finalized = /* @__PURE__ */ new Set();
7744
+ const registry = new FinalizationRegistry((name) => finalized.add(name));
7745
+ let sourceErrored = false;
7746
+ const mastra = new Mastra$1({
7747
+ logger: false,
7748
+ server: { apiRoutes: [registerApiRoute("/report", {
7749
+ method: "POST",
7750
+ requiresAuth: false,
7751
+ handler: async (c) => {
7752
+ registry.register(c.req.raw, "request");
7753
+ registry.register(c.req.raw.signal, "request signal");
7754
+ const chunks = failsMidStream ? ["part-1 "] : [
7755
+ "part-1 ",
7756
+ "part-2 ",
7757
+ "part-3"
7758
+ ];
7759
+ const body = new ReadableStream({ pull(controller) {
7760
+ const chunk = chunks.shift();
7761
+ if (chunk !== void 0) controller.enqueue(chunk);
7762
+ else if (failsMidStream) {
7763
+ sourceErrored = true;
7764
+ controller.error(/* @__PURE__ */ new Error("report generation failed"));
7765
+ } else controller.close();
7766
+ } });
7767
+ return new Response(body.pipeThrough(new TextEncoderStream()), {
7768
+ status: 201,
7769
+ headers: {
7770
+ "content-type": "text/plain; charset=utf-8",
7771
+ "x-report-id": "r-1"
7772
+ }
7773
+ });
7774
+ }
7775
+ })] }
7776
+ });
7777
+ const server = await startServer(mastra);
7778
+ cleanups.push(server.close);
7779
+ const response = await withDeadline(new Promise((resolve, reject) => {
7780
+ const req = request(`${server.baseUrl}/report`, {
7781
+ method: "POST",
7782
+ agent: false,
7783
+ headers: {
7784
+ "content-type": "application/json",
7785
+ connection: "close"
7786
+ }
7787
+ }, (res) => {
7788
+ let text = "";
7789
+ res.setEncoding("utf8");
7790
+ res.on("data", (chunk) => text += chunk);
7791
+ res.on("error", () => {});
7792
+ res.on("close", () => resolve({
7793
+ status: res.statusCode,
7794
+ text,
7795
+ complete: res.complete
7796
+ }));
7797
+ });
7798
+ req.on("error", reject);
7799
+ req.end(JSON.stringify({}));
7800
+ }), 5e3, "the custom route response to close");
7801
+ if (failsMidStream) {
7802
+ expect(sourceErrored).toBe(true);
7803
+ expect(response.text).not.toContain("part-2");
7804
+ } else expect(response).toEqual({
7805
+ status: 201,
7806
+ text: "part-1 part-2 part-3",
7807
+ complete: true
7808
+ });
7809
+ for (let round = 0; round < 20 && finalized.size < 2; round++) await collectGarbage();
7810
+ expect([...finalized].sort()).toEqual(["request", "request signal"]);
7811
+ }, 2e4);
7812
+ });
7092
7813
  }
7093
- export { consumeSSEStream, createBodyLimitTestSuite, createCustomRouteFGATestSuite, createDefaultTestContext, createDurableDisconnectTestSuite, createHttpLoggingTestSuite, createKnowledgeRoutesTestSuite, createMCPRouteTestSuite, createMCPTransportTestSuite, createMultipartTestSuite, createObservabilitySignalWriteTestSuite, createRouteAdapterTestSuite, createStreamDisconnectTestSuite, createStreamWithSensitiveData, createStreamWithUnserializableChunk, createStructuredWakeTestSuite, expectSerializedStreamChunks };
7814
+ export { consumeSSEStream, createBodyLimitTestSuite, createCustomRouteDisconnectTestSuite, createCustomRouteFGATestSuite, createDefaultTestContext, createDurableDisconnectTestSuite, createHttpLoggingTestSuite, createKnowledgeRoutesTestSuite, createMCPRouteTestSuite, createMCPTransportTestSuite, createMultipartTestSuite, createObservabilitySignalWriteTestSuite, createRouteAdapterTestSuite, createStreamDisconnectTestSuite, createStreamWithSensitiveData, createStreamWithUnserializableChunk, createStructuredWakeTestSuite, expectSerializedStreamChunks };
7094
7815
 
7095
7816
  //# sourceMappingURL=index.js.map