@mastra/server-adapters-test-suite 0.2.1 → 0.2.2-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.
package/dist/index.js CHANGED
@@ -17,7 +17,7 @@ import { createScorer } from "@mastra/core/evals";
17
17
  import { LogLevel } from "@mastra/core/logger";
18
18
  import { MockMemory } from "@mastra/core/memory";
19
19
  import { SpanType } from "@mastra/core/observability";
20
- import { InMemoryStore } from "@mastra/core/storage";
20
+ import { EntityType, InMemoryStore } from "@mastra/core/storage";
21
21
  import { UnknownToolProviderError } from "@mastra/core/tool-provider";
22
22
  import { createTool, isValidationError } from "@mastra/core/tools";
23
23
  import { MastraVector } from "@mastra/core/vector";
@@ -25,7 +25,7 @@ import { CompositeVoice } from "@mastra/core/voice";
25
25
  import { createStep, createWorkflow } from "@mastra/core/workflows";
26
26
  import { LocalFilesystem, Workspace } from "@mastra/core/workspace";
27
27
  import { MCPClient, MCPServer, defineEvent } from "@mastra/mcp";
28
- import { AGENT_STREAM_TOPIC, createDurableAgent, createDurableAgentStream, createEventedAgent } from "@mastra/core/agent/durable";
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
30
  import http from "http";
31
31
  import { MCPServerBase } from "@mastra/core/mcp";
@@ -3152,6 +3152,34 @@ function deferred() {
3152
3152
  resolve
3153
3153
  };
3154
3154
  }
3155
+ async function persistReplayOwner(mastra, runId, agentId) {
3156
+ const workflows = await mastra.getStorage().getStore("workflows");
3157
+ const input = {
3158
+ agentId,
3159
+ status: "success",
3160
+ output: {},
3161
+ payload: {},
3162
+ startedAt: Date.now(),
3163
+ endedAt: Date.now()
3164
+ };
3165
+ await workflows.persistWorkflowSnapshot({
3166
+ workflowName: DurableStepIds.AGENTIC_LOOP,
3167
+ runId,
3168
+ snapshot: {
3169
+ runId,
3170
+ status: "success",
3171
+ value: {},
3172
+ context: { input },
3173
+ activePaths: [],
3174
+ activeStepsPath: {},
3175
+ serializedStepGraph: [],
3176
+ suspendedPaths: {},
3177
+ resumeLabels: {},
3178
+ waitingPaths: {},
3179
+ timestamp: Date.now()
3180
+ }
3181
+ });
3182
+ }
3155
3183
  async function collectPublicFrames(response, onFrame) {
3156
3184
  const frames = [];
3157
3185
  const reader = response.body.pipeThrough(new TextDecoderStream()).getReader();
@@ -3357,6 +3385,15 @@ function createDurableDisconnectTestSuite(config) {
3357
3385
  } finally {
3358
3386
  await reference.cleanup();
3359
3387
  }
3388
+ const observe = () => fetch(`${server.baseUrl}/api/agents/agent/observe`, {
3389
+ method: "POST",
3390
+ headers: { "content-type": "application/json" },
3391
+ body: JSON.stringify({ runId })
3392
+ });
3393
+ expect((await observe()).status).toBe(404);
3394
+ await persistReplayOwner(mastra, runId, "another-agent");
3395
+ expect((await observe()).status).toBe(403);
3396
+ await persistReplayOwner(mastra, runId, agent.id);
3360
3397
  for (const offset of [
3361
3398
  0,
3362
3399
  1,
@@ -3374,6 +3411,16 @@ function createDurableDisconnectTestSuite(config) {
3374
3411
  expect(await collectPublicFrames(response)).toEqual(expected);
3375
3412
  }
3376
3413
  expect(expected).toHaveLength(terminal === "error" ? 1 : 2);
3414
+ expect(expected.map((frame) => frame.index)).toEqual(terminal === "error" ? [2] : [void 0, 2]);
3415
+ const pastEnd = await fetch(`${server.baseUrl}/api/agents/agent/observe`, {
3416
+ method: "POST",
3417
+ headers: { "content-type": "application/json" },
3418
+ body: JSON.stringify({
3419
+ runId,
3420
+ offset: 3
3421
+ })
3422
+ });
3423
+ expect(await collectPublicFrames(pastEnd)).toEqual([]);
3377
3424
  if (config.observeWithClient) expect(await config.observeWithClient(server.baseUrl, runId)).toEqual(expected);
3378
3425
  });
3379
3426
  it("preserves finish metadata after a model error chunk (COR-1498)", async () => {
@@ -3391,6 +3438,7 @@ function createDurableDisconnectTestSuite(config) {
3391
3438
  });
3392
3439
  server = await config.startServer(mastra, () => {});
3393
3440
  const runId = `observe-model-error-${kind}`;
3441
+ await persistReplayOwner(mastra, runId, agent.id);
3394
3442
  const usage = {
3395
3443
  inputTokens: 1,
3396
3444
  outputTokens: 2,
@@ -3490,6 +3538,7 @@ function createDurableDisconnectTestSuite(config) {
3490
3538
  });
3491
3539
  server = await config.startServer(mastra, () => {});
3492
3540
  const runId = `observe-usage-${kind}-${explicit}`;
3541
+ await persistReplayOwner(mastra, runId, agent.id);
3493
3542
  const usage = {
3494
3543
  inputTokens: 1,
3495
3544
  outputTokens: 2,
@@ -3579,6 +3628,7 @@ function createDurableDisconnectTestSuite(config) {
3579
3628
  });
3580
3629
  server = await config.startServer(mastra, () => {});
3581
3630
  const runId = `observe-reject-${kind}`;
3631
+ await persistReplayOwner(mastra, runId, agent.id);
3582
3632
  const prefix = {
3583
3633
  type: "text-delta",
3584
3634
  runId,
@@ -3608,7 +3658,10 @@ function createDurableDisconnectTestSuite(config) {
3608
3658
  });
3609
3659
  expect(response.status).toBe(200);
3610
3660
  const wire = await response.text();
3611
- expect(wire).toContain(JSON.stringify(prefix));
3661
+ expect(wire).toContain(JSON.stringify({
3662
+ ...prefix,
3663
+ index: 0
3664
+ }));
3612
3665
  expect(wire).not.toContain("[DONE]");
3613
3666
  expect(wire).not.toContain("\"type\":\"error\"");
3614
3667
  });
@@ -3725,7 +3778,311 @@ function createDurableDisconnectTestSuite(config) {
3725
3778
  await original;
3726
3779
  }
3727
3780
  });
3781
+ it("resumes a multi-step tool loop after the last frame index received from /stream (COR-1506)", async () => {
3782
+ fixture = await config.setupModel();
3783
+ const tool = createTool({
3784
+ id: "lookup",
3785
+ description: "Look up a value",
3786
+ inputSchema: z.object({ n: z.number() }),
3787
+ execute: async ({ n }) => ({ value: n * 10 })
3788
+ });
3789
+ const agent = (kind === "durable" ? createDurableAgent : createEventedAgent)({
3790
+ agent: new Agent({
3791
+ id: "resume-agent",
3792
+ name: "Resume agent",
3793
+ instructions: "Look up values.",
3794
+ model: fixture.model,
3795
+ tools: { lookup: tool }
3796
+ }),
3797
+ cleanupTimeoutMs: 6e4
3798
+ });
3799
+ mastra = new Mastra$1({
3800
+ agents: { agent },
3801
+ storage: new InMemoryStore(),
3802
+ logger: false
3803
+ });
3804
+ server = await config.startServer(mastra, () => {});
3805
+ fixture.addResponse(0, {
3806
+ content: "First",
3807
+ toolCalls: [{
3808
+ id: "call-1",
3809
+ name: "lookup",
3810
+ arguments: { n: 1 }
3811
+ }]
3812
+ });
3813
+ fixture.addResponse(1, {
3814
+ content: "Second",
3815
+ toolCalls: [{
3816
+ id: "call-2",
3817
+ name: "lookup",
3818
+ arguments: { n: 2 }
3819
+ }]
3820
+ });
3821
+ fixture.addResponse(2, { content: "Done: 10 and 20." });
3822
+ fixture.addResponse(3, { content: "Again: 30." });
3823
+ const runId = `observe-resume-${kind}`;
3824
+ const post = async (route, body) => {
3825
+ const response = await fetch(`${server.baseUrl}/api/agents/agent/${route}`, {
3826
+ method: "POST",
3827
+ headers: { "content-type": "application/json" },
3828
+ body: JSON.stringify(body)
3829
+ });
3830
+ expect(response.status).toBe(200);
3831
+ return response;
3832
+ };
3833
+ const frames = await collectPublicFrames(await post("stream", {
3834
+ messages: "Look up 1 then 2",
3835
+ runId,
3836
+ maxSteps: 5
3837
+ }));
3838
+ expect(frames.filter((frame) => frame.type === "step-start")).toHaveLength(3);
3839
+ expect(frames.at(-1)).toMatchObject({ type: "finish" });
3840
+ expect(frames.every((frame) => typeof frame.index === "number")).toBe(true);
3841
+ expect(frames.at(-1).index).toBeGreaterThan(frames.length - 1);
3842
+ expect(await collectPublicFrames(await post("observe", {
3843
+ runId,
3844
+ offset: 0
3845
+ }))).toEqual(frames);
3846
+ const resumeAt = (cut) => cut === 0 ? 0 : frames[cut - 1].index + 1;
3847
+ const mismatches = [];
3848
+ for (let cut = 0; cut <= frames.length; cut++) {
3849
+ const resumed = await collectPublicFrames(await post("observe", {
3850
+ runId,
3851
+ offset: resumeAt(cut)
3852
+ }));
3853
+ const combined = [...frames.slice(0, cut), ...resumed];
3854
+ if (JSON.stringify(combined) !== JSON.stringify(frames)) mismatches.push(`cut ${cut}/${frames.length}: ${combined.length - frames.length} extra`);
3855
+ }
3856
+ expect(mismatches).toEqual([]);
3857
+ const cuts = [
3858
+ 0,
3859
+ Math.floor(frames.length / 2),
3860
+ frames.length - 1,
3861
+ frames.length
3862
+ ];
3863
+ if (config.observeWithClient) for (const cut of cuts) expect([...frames.slice(0, cut), ...await config.observeWithClient(server.baseUrl, runId, resumeAt(cut))]).toEqual(frames);
3864
+ if (config.streamWithClient) {
3865
+ const clientRunId = `${runId}-client`;
3866
+ const clientFrames = await config.streamWithClient(server.baseUrl, {
3867
+ messages: "Say it again",
3868
+ runId: clientRunId
3869
+ });
3870
+ expect(clientFrames.at(-1)).toMatchObject({ type: "finish" });
3871
+ expect(clientFrames.some((frame) => frame.type === "error")).toBe(false);
3872
+ expect(clientFrames.every((frame) => typeof frame.index === "number")).toBe(true);
3873
+ const mid = Math.floor(clientFrames.length / 2);
3874
+ expect([...clientFrames.slice(0, mid), ...await config.observeWithClient(server.baseUrl, clientRunId, clientFrames[mid - 1].index + 1)]).toEqual(clientFrames);
3875
+ }
3876
+ });
3877
+ it("keeps numbering and resuming by index across a tool-approval suspend (COR-1506)", async () => {
3878
+ fixture = await config.setupModel();
3879
+ const storage = new InMemoryStore();
3880
+ const authorize = createTool({
3881
+ id: "authorize",
3882
+ description: "Authorize the lookup",
3883
+ inputSchema: z.object({}),
3884
+ requireApproval: true,
3885
+ execute: async () => ({ approved: true })
3886
+ });
3887
+ const lookup = createTool({
3888
+ id: "lookup",
3889
+ description: "Look up a value",
3890
+ inputSchema: z.object({ n: z.number() }),
3891
+ execute: async ({ n }) => ({ value: n * 10 })
3892
+ });
3893
+ const agent = (kind === "durable" ? createDurableAgent : createEventedAgent)({
3894
+ agent: new Agent({
3895
+ id: "suspend-agent",
3896
+ name: "Suspend agent",
3897
+ instructions: "Authorize, then look up.",
3898
+ model: fixture.model,
3899
+ memory: new MockMemory({ storage }),
3900
+ tools: {
3901
+ authorize,
3902
+ lookup
3903
+ }
3904
+ }),
3905
+ cleanupTimeoutMs: 6e4
3906
+ });
3907
+ mastra = new Mastra$1({
3908
+ agents: { agent },
3909
+ storage,
3910
+ logger: false
3911
+ });
3912
+ server = await config.startServer(mastra, () => {});
3913
+ fixture.addResponse(0, {
3914
+ content: "Asking",
3915
+ toolCalls: [{
3916
+ id: "authorize-1",
3917
+ name: "authorize",
3918
+ arguments: {}
3919
+ }]
3920
+ });
3921
+ fixture.addResponse(1, {
3922
+ content: "Looking",
3923
+ toolCalls: [{
3924
+ id: "call-1",
3925
+ name: "lookup",
3926
+ arguments: { n: 1 }
3927
+ }]
3928
+ });
3929
+ fixture.addResponse(2, { content: "Done: 10." });
3930
+ const runId = `observe-suspend-${kind}`;
3931
+ const memory = {
3932
+ thread: runId,
3933
+ resource: "suspend-user"
3934
+ };
3935
+ const post = async (route, body) => {
3936
+ const response = await fetch(`${server.baseUrl}/api/agents/agent/${route}`, {
3937
+ method: "POST",
3938
+ headers: { "content-type": "application/json" },
3939
+ body: JSON.stringify(body)
3940
+ });
3941
+ expect(response.status).toBe(200);
3942
+ return response;
3943
+ };
3944
+ const suspended = [];
3945
+ const dropped = new AbortController();
3946
+ const streaming = await fetch(`${server.baseUrl}/api/agents/agent/stream`, {
3947
+ method: "POST",
3948
+ headers: { "content-type": "application/json" },
3949
+ signal: dropped.signal,
3950
+ body: JSON.stringify({
3951
+ messages: "Authorize then look up 1",
3952
+ runId,
3953
+ memory,
3954
+ maxSteps: 5
3955
+ })
3956
+ });
3957
+ expect(streaming.status).toBe(200);
3958
+ await collectPublicFrames(streaming, (frame) => {
3959
+ suspended.push(frame);
3960
+ if (frame.type === "tool-call-approval") dropped.abort();
3961
+ }).catch((error) => {
3962
+ if (error.name !== "AbortError") throw error;
3963
+ });
3964
+ expect(suspended.at(-1)).toMatchObject({ type: "tool-call-approval" });
3965
+ expect(suspended.every((frame) => typeof frame.index === "number")).toBe(true);
3966
+ const mid = Math.floor(suspended.length / 2);
3967
+ const parkedObservers = [suspended.length, mid].map(async (cut) => ({
3968
+ cut,
3969
+ frames: await collectPublicFrames(await post("observe", {
3970
+ runId,
3971
+ offset: suspended[cut - 1].index + 1
3972
+ }))
3973
+ }));
3974
+ await new Promise((resolve) => setTimeout(resolve, 200));
3975
+ const approved = await collectPublicFrames(await post("approve-tool-call", {
3976
+ runId,
3977
+ toolCallId: "authorize-1",
3978
+ memory,
3979
+ maxSteps: 5
3980
+ }));
3981
+ expect(approved.at(-1)).toMatchObject({ type: "finish" });
3982
+ expect(approved.every((frame) => typeof frame.index === "number")).toBe(true);
3983
+ expect(approved[0].index).toBeGreaterThan(suspended.at(-1).index);
3984
+ const frames = [...suspended, ...approved];
3985
+ expect(frames.map((frame) => frame.index)).toEqual([...frames.map((frame) => frame.index)].sort((a, b) => a - b));
3986
+ for (const { cut, frames: resumed } of await Promise.all(parkedObservers)) expect([...suspended.slice(0, cut), ...resumed]).toEqual(frames);
3987
+ expect(await collectPublicFrames(await post("observe", {
3988
+ runId,
3989
+ offset: 0
3990
+ }))).toEqual(frames);
3991
+ const mismatches = [];
3992
+ for (let cut = 0; cut <= frames.length; cut++) {
3993
+ const offset = cut === 0 ? 0 : frames[cut - 1].index + 1;
3994
+ const combined = [...frames.slice(0, cut), ...await collectPublicFrames(await post("observe", {
3995
+ runId,
3996
+ offset
3997
+ }))];
3998
+ if (JSON.stringify(combined) !== JSON.stringify(frames)) mismatches.push(`cut ${cut}/${frames.length}: ${combined.length - frames.length} extra`);
3999
+ }
4000
+ expect(mismatches).toEqual([]);
4001
+ }, 3e4);
3728
4002
  }
4003
+ if (kind === "plain") it("numbers every /observe frame with the index its log entry holds (COR-1506)", async () => {
4004
+ fixture = await config.setupModel();
4005
+ const tool = createTool({
4006
+ id: "lookup",
4007
+ description: "Look up a value",
4008
+ inputSchema: z.object({ n: z.number() }),
4009
+ execute: async ({ n }) => ({ value: n * 10 })
4010
+ });
4011
+ const agent = new Agent({
4012
+ id: "plain-resume-agent",
4013
+ name: "Plain resume agent",
4014
+ instructions: "Look up values.",
4015
+ model: fixture.model,
4016
+ tools: { lookup: tool }
4017
+ });
4018
+ mastra = new Mastra$1({
4019
+ agents: { agent },
4020
+ storage: new InMemoryStore(),
4021
+ logger: false
4022
+ });
4023
+ server = await config.startServer(mastra, () => {});
4024
+ fixture.addResponse(0, {
4025
+ content: "First",
4026
+ toolCalls: [{
4027
+ id: "call-1",
4028
+ name: "lookup",
4029
+ arguments: { n: 1 }
4030
+ }]
4031
+ });
4032
+ fixture.addResponse(1, {
4033
+ content: "Second",
4034
+ toolCalls: [{
4035
+ id: "call-2",
4036
+ name: "lookup",
4037
+ arguments: { n: 2 }
4038
+ }]
4039
+ });
4040
+ fixture.addResponse(2, { content: "Done: 10 and 20." });
4041
+ const runId = "observe-resume-plain";
4042
+ const post = async (route, body) => {
4043
+ const response = await fetch(`${server.baseUrl}/api/agents/agent/${route}`, {
4044
+ method: "POST",
4045
+ headers: { "content-type": "application/json" },
4046
+ body: JSON.stringify(body)
4047
+ });
4048
+ expect(response.status).toBe(200);
4049
+ return response;
4050
+ };
4051
+ const streamed = await collectPublicFrames(await post("stream", {
4052
+ messages: "Look up 1 then 2",
4053
+ runId,
4054
+ maxSteps: 5
4055
+ }));
4056
+ expect(streamed.filter((frame) => frame.type === "step-start")).toHaveLength(3);
4057
+ const pubsub = agent.__getStreamBus().pubsub;
4058
+ const topic = AGENT_STREAM_TOPIC(runId);
4059
+ await expect.poll(async () => (await pubsub.getHistory(topic)).some((event) => event.type === "finish"), { timeout: 5e3 }).toBe(true);
4060
+ const history = await pubsub.getHistory(topic);
4061
+ const frames = await collectPublicFrames(await post("observe", { runId }));
4062
+ expect(frames.map((frame) => frame.type)).toEqual(streamed.map((frame) => frame.type));
4063
+ expect(frames.every((frame) => typeof frame.index === "number")).toBe(true);
4064
+ for (const frame of frames) {
4065
+ const entry = history.find((event) => event.index === frame.index);
4066
+ expect(entry, `log entry ${frame.index}`).toBeDefined();
4067
+ if (entry.type === "chunk") expect(entry.data.type).toBe(frame.type);
4068
+ else expect(entry.type).toBe(frame.type);
4069
+ }
4070
+ expect(frames.every((frame, i) => i === 0 || frame.index > frames[i - 1].index)).toBe(true);
4071
+ const resumeAt = (cut) => cut === 0 ? 0 : frames[cut - 1].index + 1;
4072
+ const mismatches = [];
4073
+ for (let cut = 0; cut <= frames.length; cut++) {
4074
+ const resumed = await collectPublicFrames(await post("observe", {
4075
+ runId,
4076
+ offset: resumeAt(cut)
4077
+ }));
4078
+ if (JSON.stringify([...frames.slice(0, cut), ...resumed]) !== JSON.stringify(frames)) mismatches.push(`cut ${cut}/${frames.length}`);
4079
+ }
4080
+ expect(mismatches).toEqual([]);
4081
+ if (config.observeWithClient) {
4082
+ const mid = Math.floor(frames.length / 2);
4083
+ expect([...frames.slice(0, mid), ...await config.observeWithClient(server.baseUrl, runId, resumeAt(mid))]).toEqual(frames);
4084
+ }
4085
+ }, 3e4);
3729
4086
  for (const route of [
3730
4087
  "stream",
3731
4088
  "stream-alias",
@@ -3735,15 +4092,18 @@ function createDurableDisconnectTestSuite(config) {
3735
4092
  "resume-stream-until-idle",
3736
4093
  "approve-tool-call",
3737
4094
  "decline-tool-call"
3738
- ]) for (const explicitAbort of [
4095
+ ]) for (const { durable, explicitAbort } of (kind === "plain" ? [void 0, false] : [void 0]).flatMap((durable) => [
3739
4096
  false,
3740
4097
  true,
3741
4098
  "run",
3742
4099
  "threadless"
3743
- ]) {
4100
+ ].map((explicitAbort) => ({
4101
+ durable,
4102
+ explicitAbort
4103
+ })))) {
3744
4104
  if (typeof explicitAbort === "string" && (kind === "plain" || !["stream", "stream-alias"].includes(route))) continue;
3745
4105
  const threadless = explicitAbort === "threadless";
3746
- it(`${route}: disconnect ${explicitAbort ? `then explicit ${explicitAbort === true ? "thread" : explicitAbort} abort stops execution` : "preserves durable execution and replay"}`, async () => {
4106
+ it(`${route}${durable === false ? " (durable:false)" : ""}: disconnect ${explicitAbort ? `then explicit ${explicitAbort === true ? "thread" : explicitAbort} abort stops execution` : "preserves durable execution and replay"}`, async () => {
3747
4107
  fixture = await config.setupModel();
3748
4108
  const entered = deferred();
3749
4109
  const gate = deferred();
@@ -3792,6 +4152,7 @@ function createDurableDisconnectTestSuite(config) {
3792
4152
  const inner = new Agent({
3793
4153
  id: "disconnect-agent",
3794
4154
  name: "Disconnect agent",
4155
+ durable,
3795
4156
  instructions: "Execute three steps.",
3796
4157
  model: fixture.model,
3797
4158
  ...threadless ? {} : { memory },
@@ -3889,7 +4250,7 @@ function createDurableDisconnectTestSuite(config) {
3889
4250
  client.abort();
3890
4251
  await disconnected.promise;
3891
4252
  await request;
3892
- if (kind !== "plain" && explicitAbort) {
4253
+ if (durable !== false && explicitAbort) {
3893
4254
  const runScoped = typeof explicitAbort === "string";
3894
4255
  if (runScoped) {
3895
4256
  const denied = await fetch(`${server.baseUrl}/api/agents/otherAgent/runs/${runId}/abort`, { method: "POST" });
@@ -3909,10 +4270,10 @@ function createDurableDisconnectTestSuite(config) {
3909
4270
  expect(response.status).toBe(200);
3910
4271
  expect(result).toEqual({ aborted: true });
3911
4272
  }
3912
- const completes = kind !== "plain" && !explicitAbort;
4273
+ const completes = durable !== false && !explicitAbort;
3913
4274
  if (!completes) await vi.waitFor(() => expect(toolSignal?.aborted).toBe(true));
3914
4275
  else expect(toolSignal?.aborted).toBe(false);
3915
- const observing = kind === "plain" ? void 0 : fetch(`${base}/observe`, {
4276
+ const observing = durable === false ? void 0 : fetch(`${base}/observe`, {
3916
4277
  method: "POST",
3917
4278
  headers: { "content-type": "application/json" },
3918
4279
  body: JSON.stringify({ runId })
@@ -4222,6 +4583,73 @@ function createMCPRouteTestSuite(config) {
4222
4583
  });
4223
4584
  });
4224
4585
  }
4586
+ // src/legacy-sse-fixture.ts
4587
+ const TRUSTED_REQUEST_CONTEXT = Symbol.for("mastra.mcp.trustedRequestContext");
4588
+ function identityOf(auth) {
4589
+ const authInfo = auth;
4590
+ const trusted = authInfo?.[TRUSTED_REQUEST_CONTEXT];
4591
+ const user = trusted?.["mastra__user"];
4592
+ return {
4593
+ clientId: authInfo?.clientId ?? null,
4594
+ userId: user?.id ?? null,
4595
+ permissions: trusted?.["mastra__userPermissions"] ?? null
4596
+ };
4597
+ }
4598
+ /**
4599
+ * Stands in for an `@mastra/mcp` 1.x server, the only kind that serves the
4600
+ * standalone SSE transport. Instead of streaming, it answers the SSE request
4601
+ * with the caller identity the adapter handed it.
4602
+ */
4603
+ var LegacySSEFixture = class extends MCPServerBase {
4604
+ constructor() {
4605
+ super({
4606
+ id: "legacy-sse-fixture",
4607
+ name: "Legacy SSE fixture",
4608
+ version: "1.0.0",
4609
+ tools: {}
4610
+ });
4611
+ }
4612
+ async startSSE({ req, res }) {
4613
+ res.writeHead(200, { "Content-Type": "application/json" });
4614
+ res.end(JSON.stringify(identityOf(req.auth)));
4615
+ }
4616
+ async startHonoSSE({ authInfo }) {
4617
+ return Response.json(identityOf(authInfo));
4618
+ }
4619
+ convertTools() {
4620
+ return {};
4621
+ }
4622
+ async startStdio() {}
4623
+ async startHTTP() {}
4624
+ async close() {}
4625
+ getServerInfo() {
4626
+ return {
4627
+ id: this.id,
4628
+ name: this.name,
4629
+ version_detail: {
4630
+ version: this.version,
4631
+ release_date: this.releaseDate,
4632
+ is_latest: this.isLatest
4633
+ }
4634
+ };
4635
+ }
4636
+ getServerDetail() {
4637
+ return this.getServerInfo();
4638
+ }
4639
+ getToolListInfo() {
4640
+ return { tools: [] };
4641
+ }
4642
+ getToolInfo() {}
4643
+ async executeTool() {
4644
+ return {};
4645
+ }
4646
+ async readResource() {
4647
+ return { contents: [] };
4648
+ }
4649
+ async listResources() {
4650
+ return { resources: [] };
4651
+ }
4652
+ };
4225
4653
  // src/native-mcp-fixture.ts
4226
4654
  const unavailable = (feature) => async () => {
4227
4655
  throw new Error(`${feature} is not available on a 2026-07-28 server`);
@@ -4712,6 +5140,119 @@ function createMCPTransportTestSuite(config) {
4712
5140
  });
4713
5141
  });
4714
5142
  });
5143
+ describe("HTTP Transport with server auth", () => {
5144
+ const whoamiTool = createTool({
5145
+ id: "whoami",
5146
+ description: "Reports the caller identity the tool sees",
5147
+ inputSchema: z.object({}),
5148
+ execute: async (_input, context) => {
5149
+ const requestContext = context?.requestContext;
5150
+ return {
5151
+ userId: (requestContext?.get("user"))?.id ?? null,
5152
+ permissions: requestContext?.get("mastra__userPermissions") ?? null
5153
+ };
5154
+ }
5155
+ });
5156
+ let authedHttpServer;
5157
+ let authedPort;
5158
+ let authedMcpServer;
5159
+ let authedClient;
5160
+ const legacySseServer = new LegacySSEFixture();
5161
+ const users = {
5162
+ "reader-token": { id: "user-reader" },
5163
+ "blocked-token": { id: "user-blocked" }
5164
+ };
5165
+ beforeAll(async () => {
5166
+ authedMcpServer = new MCPServer({
5167
+ name: "authed",
5168
+ version: "1.0.0",
5169
+ tools: { whoami: whoamiTool }
5170
+ });
5171
+ const authedMastra = new Mastra$1({
5172
+ mcpServers: {
5173
+ authed: authedMcpServer,
5174
+ legacySse: legacySseServer
5175
+ },
5176
+ server: {
5177
+ auth: {
5178
+ authenticateToken: async (token) => users[token] ?? null,
5179
+ authorize: async (_path, _method, user) => user.id !== "user-blocked"
5180
+ },
5181
+ rbac: {
5182
+ getPermissions: async () => ["observability:read"],
5183
+ getRoles: async () => ["reader"]
5184
+ }
5185
+ }
5186
+ });
5187
+ const setup = await createServer(authedMastra);
5188
+ authedHttpServer = setup.server;
5189
+ authedPort = setup.port;
5190
+ authedClient = new MCPClient({ servers: { authed: {
5191
+ url: new URL(`http://localhost:${authedPort}/api/mcp/${authedMcpServer.id}/mcp`),
5192
+ requestInit: { headers: { Authorization: "Bearer reader-token" } }
5193
+ } } });
5194
+ }, 3e4);
5195
+ afterAll(async () => {
5196
+ await authedClient?.disconnect();
5197
+ authedHttpServer?.close();
5198
+ await authedMcpServer?.close();
5199
+ }, 3e4);
5200
+ it("gives MCP tools the user and permissions server auth resolved", async () => {
5201
+ const whoami = (await authedClient.listTools())["authed_whoami"];
5202
+ if (!whoami?.execute) throw new Error("Expected authed_whoami tool");
5203
+ const result = await whoami.execute({}, {});
5204
+ expectTextToolResult(result, {
5205
+ userId: "user-reader",
5206
+ permissions: ["observability:read"]
5207
+ });
5208
+ });
5209
+ it("rejects MCP requests without credentials", async () => {
5210
+ const response = await fetch(`http://localhost:${authedPort}/api/mcp/${authedMcpServer.id}/mcp`, {
5211
+ method: "POST",
5212
+ headers: {
5213
+ "Content-Type": "application/json",
5214
+ Accept: "application/json, text/event-stream"
5215
+ },
5216
+ body: JSON.stringify({
5217
+ jsonrpc: "2.0",
5218
+ id: 1,
5219
+ method: "tools/list",
5220
+ params: {}
5221
+ })
5222
+ });
5223
+ expect(response.status).toBe(401);
5224
+ });
5225
+ it("refuses MCP requests server auth does not authorize", async () => {
5226
+ const response = await fetch(`http://localhost:${authedPort}/api/mcp/${authedMcpServer.id}/mcp`, {
5227
+ method: "POST",
5228
+ headers: {
5229
+ "Content-Type": "application/json",
5230
+ Accept: "application/json, text/event-stream",
5231
+ Authorization: "Bearer blocked-token"
5232
+ },
5233
+ body: JSON.stringify({
5234
+ jsonrpc: "2.0",
5235
+ id: 1,
5236
+ method: "tools/list",
5237
+ params: {}
5238
+ })
5239
+ });
5240
+ expect(response.status).toBe(403);
5241
+ });
5242
+ it("hands the SSE transport the user and permissions server auth resolved", async () => {
5243
+ const response = await fetch(`http://localhost:${authedPort}/api/mcp/${legacySseServer.id}/sse`, { headers: { Authorization: "Bearer reader-token" } });
5244
+ expect(response.status).toBe(200);
5245
+ expect(await response.json()).toEqual({
5246
+ clientId: "user-reader",
5247
+ userId: "user-reader",
5248
+ permissions: ["observability:read"]
5249
+ });
5250
+ });
5251
+ it("refuses SSE requests server auth does not authorize", async () => {
5252
+ const response = await fetch(`http://localhost:${authedPort}/api/mcp/${legacySseServer.id}/sse`, { headers: { Authorization: "Bearer blocked-token" } });
5253
+ expect(response.status).toBe(403);
5254
+ });
5255
+ });
4715
5256
  describe("HTTP Transport client disconnect", () => {
4716
5257
  it.each([{
4717
5258
  name: "before any response bytes are sent",
@@ -5722,6 +6263,478 @@ function createKnowledgeRoutesTestSuite(config) {
5722
6263
  });
5723
6264
  });
5724
6265
  }
5725
- export { consumeSSEStream, createBodyLimitTestSuite, createCustomRouteFGATestSuite, createDefaultTestContext, createDurableDisconnectTestSuite, createHttpLoggingTestSuite, createKnowledgeRoutesTestSuite, createMCPRouteTestSuite, createMCPTransportTestSuite, createMultipartTestSuite, createRouteAdapterTestSuite, createStreamWithSensitiveData, createStreamWithUnserializableChunk, createStructuredWakeTestSuite, expectSerializedStreamChunks };
6266
+ // src/observability-signal-write-test-suite.ts
6267
+ /** Shared HTTP contract for feedback/score writes, backed by real in-memory storage. */
6268
+ function createObservabilitySignalWriteTestSuite({ startServer }) {
6269
+ describe.each(["feedback", "scores"])("%s HTTP writes", (collection) => {
6270
+ let server;
6271
+ let store;
6272
+ let trustedScope;
6273
+ let allowAccess;
6274
+ let authorLookupFails;
6275
+ const isFeedback = collection === "feedback";
6276
+ const field = isFeedback ? "feedback" : "score";
6277
+ const idField = isFeedback ? "feedbackId" : "scoreId";
6278
+ const payload = isFeedback ? {
6279
+ feedbackType: "thumbs",
6280
+ value: 1
6281
+ } : {
6282
+ scorerId: "accuracy",
6283
+ score: .9
6284
+ };
6285
+ const path = `/api/observability/${collection}`;
6286
+ beforeEach(async () => {
6287
+ trustedScope = {};
6288
+ allowAccess = true;
6289
+ authorLookupFails = false;
6290
+ const storage = new InMemoryStore();
6291
+ store = await storage.getStore("observability");
6292
+ server = await startServer(new Mastra({
6293
+ storage,
6294
+ logger: false,
6295
+ server: { auth: {
6296
+ authenticateToken: async () => ({}),
6297
+ getCurrentUser: async () => null,
6298
+ getUser: async (id) => {
6299
+ if (authorLookupFails) throw new Error("Directory unavailable");
6300
+ return id === "author-1" ? {
6301
+ id,
6302
+ name: "Alice",
6303
+ email: "alice@example.com",
6304
+ secret: "private"
6305
+ } : null;
6306
+ },
6307
+ authorize: async (_path, _method, _user, c) => {
6308
+ for (const [key, value] of Object.entries(trustedScope)) c.get("requestContext").set(key, value);
6309
+ return allowAccess;
6310
+ }
6311
+ } }
6312
+ }));
6313
+ });
6314
+ afterEach(async () => {
6315
+ await server?.close();
6316
+ });
6317
+ const post = (record, contextMode) => server.request(path, {
6318
+ method: "POST",
6319
+ headers: {
6320
+ "Content-Type": "application/json",
6321
+ authorization: "Bearer test-token"
6322
+ },
6323
+ body: JSON.stringify({
6324
+ [field]: record,
6325
+ contextMode
6326
+ })
6327
+ });
6328
+ const list = async () => {
6329
+ const response = await server.request(path, { headers: { authorization: "Bearer test-token" } });
6330
+ expect(response.status).toBe(200);
6331
+ return (await response.json())[collection];
6332
+ };
6333
+ describe("single-record reads", () => {
6334
+ it("returns the persisted record with its identity and timestamp", async () => {
6335
+ const id = "single-record";
6336
+ const timestamp = "2026-11-05T12:00:00.000Z";
6337
+ const identity = {
6338
+ traceId: "trace-123",
6339
+ timestamp: /* @__PURE__ */ new Date(timestamp)
6340
+ };
6341
+ if (isFeedback) await store.createFeedback({ feedback: {
6342
+ ...identity,
6343
+ feedbackId: id,
6344
+ feedbackType: "thumbs",
6345
+ value: 1
6346
+ } });
6347
+ else await store.createScore({ score: {
6348
+ ...identity,
6349
+ scoreId: id,
6350
+ scorerId: "accuracy",
6351
+ score: .9
6352
+ } });
6353
+ const response = await server.request(`${path}/${id}`, { headers: { authorization: "Bearer test-token" } });
6354
+ expect(response.status).toBe(200);
6355
+ expect(await response.json()).toMatchObject({ [field]: {
6356
+ ...payload,
6357
+ [idField]: id,
6358
+ traceId: "trace-123",
6359
+ timestamp
6360
+ } });
6361
+ });
6362
+ it("returns null for a missing record", async () => {
6363
+ const response = await server.request(`${path}/missing`, { headers: { authorization: "Bearer test-token" } });
6364
+ expect(response.status).toBe(200);
6365
+ expect(await response.json()).toEqual({ [field]: null });
6366
+ });
6367
+ it("denies reads when authorization rejects access", async () => {
6368
+ allowAccess = false;
6369
+ const response = await server.request(`${path}/private-record`, { headers: { authorization: "Bearer test-token" } });
6370
+ expect(response.status).toBe(403);
6371
+ });
6372
+ });
6373
+ if (isFeedback) describe("single-record feedback enrichment and compatibility", () => {
6374
+ it.each([
6375
+ "resolved",
6376
+ "missing",
6377
+ "failed",
6378
+ "anonymous"
6379
+ ])("returns feedback with %s author lookup consistently with the list", async (scenario) => {
6380
+ authorLookupFails = scenario === "failed";
6381
+ await store.createFeedback({ feedback: {
6382
+ feedbackId: "with-author",
6383
+ traceId: "trace-123",
6384
+ timestamp: /* @__PURE__ */ new Date("2026-11-05T12:00:00.000Z"),
6385
+ feedbackType: "thumbs",
6386
+ value: 1,
6387
+ feedbackUserId: scenario === "anonymous" ? null : scenario === "missing" ? "deleted-user" : "author-1"
6388
+ } });
6389
+ const response = await server.request(`${path}/with-author`, { headers: { authorization: "Bearer test-token" } });
6390
+ expect(response.status).toBe(200);
6391
+ const { feedback } = await response.json();
6392
+ expect(feedback).toEqual((await list())[0]);
6393
+ expect(feedback.feedbackId).toBe("with-author");
6394
+ if (scenario === "resolved") expect(feedback.author).toEqual({
6395
+ id: "author-1",
6396
+ name: "Alice",
6397
+ email: "alice@example.com"
6398
+ });
6399
+ else expect(feedback).not.toHaveProperty("author");
6400
+ });
6401
+ it("returns 501 when an older Core store lacks single-feedback reads", async () => {
6402
+ Object.defineProperty(store, "getFeedbackById", {
6403
+ value: void 0,
6404
+ configurable: true
6405
+ });
6406
+ const response = await server.request(`${path}/existing-id`, { headers: { authorization: "Bearer test-token" } });
6407
+ expect(response.status).toBe(501);
6408
+ expect(await response.json()).toMatchObject({ error: expect.stringContaining("single-feedback reads") });
6409
+ });
6410
+ });
6411
+ it.each([
6412
+ void 0,
6413
+ null,
6414
+ "",
6415
+ " ",
6416
+ " \n"
6417
+ ])("rejects traceId %j without persisting a record (OBS-788)", async (traceId) => {
6418
+ const create = vi.spyOn(store, isFeedback ? "createFeedback" : "createScore");
6419
+ for (const spanId of [void 0, "span-without-trace"]) {
6420
+ const response = await post({
6421
+ ...payload,
6422
+ traceId,
6423
+ spanId
6424
+ });
6425
+ expect(response.status, await response.clone().text()).toBe(400);
6426
+ }
6427
+ expect(create).not.toHaveBeenCalled();
6428
+ expect(await list()).toEqual([]);
6429
+ });
6430
+ it.each([void 0, "span-123"])("persists a trace with spanId %j and preserves the write contract", async (spanId) => {
6431
+ const before = Date.now();
6432
+ for (const id of [void 0, "caller-id"]) {
6433
+ const response = await post({
6434
+ ...payload,
6435
+ traceId: "trace-123",
6436
+ spanId,
6437
+ [idField]: id
6438
+ });
6439
+ expect(response.status, await response.clone().text()).toBe(200);
6440
+ expect(await response.json()).toMatchObject({
6441
+ success: true,
6442
+ data: {
6443
+ traceId: "trace-123",
6444
+ spanId: spanId ?? null,
6445
+ timestamp: expect.any(String)
6446
+ }
6447
+ });
6448
+ }
6449
+ const rows = await list();
6450
+ expect(rows).toHaveLength(2);
6451
+ expect(new Set(rows.map((row) => row[idField])).size).toBe(2);
6452
+ expect(rows.some((row) => row[idField] === "caller-id")).toBe(true);
6453
+ for (const row of rows) {
6454
+ expect(row).toMatchObject({
6455
+ ...payload,
6456
+ traceId: "trace-123"
6457
+ });
6458
+ expect(row.spanId ?? null).toBe(spanId ?? null);
6459
+ expect(row[idField]).toEqual(expect.any(String));
6460
+ expect(String(row[idField]).length).toBeGreaterThan(0);
6461
+ expect(new Date(String(row.timestamp)).getTime()).toBeGreaterThanOrEqual(before);
6462
+ expect(new Date(String(row.timestamp)).getTime()).toBeLessThanOrEqual(Date.now());
6463
+ }
6464
+ });
6465
+ const writeSpan = async (spanId, parentSpanId = null, overrides = {}) => {
6466
+ await store.createSpan({ span: {
6467
+ traceId: "trace-123",
6468
+ spanId,
6469
+ parentSpanId,
6470
+ name: spanId,
6471
+ spanType: SpanType.GENERIC,
6472
+ isEvent: false,
6473
+ startedAt: /* @__PURE__ */ new Date("2026-01-01T00:00:00Z"),
6474
+ endedAt: /* @__PURE__ */ new Date("2026-01-01T00:00:01Z"),
6475
+ entityType: EntityType.AGENT,
6476
+ entityId: spanId,
6477
+ userId: "span-user",
6478
+ tags: parentSpanId ? ["child-tag"] : ["root-tag"],
6479
+ metadata: {
6480
+ inherited: true,
6481
+ override: "stored",
6482
+ userId: "metadata-user"
6483
+ },
6484
+ ...overrides
6485
+ } });
6486
+ };
6487
+ it.each([void 0, "provided"])("uses supplied context without lookup in mode %j", async (contextMode) => {
6488
+ const lookup = vi.spyOn(store, "getTrace");
6489
+ const spanLookup = vi.spyOn(store, "getSpan");
6490
+ const timestamp = "2026-01-01T01:00:00.123456+01:00";
6491
+ const response = await post({
6492
+ ...payload,
6493
+ traceId: "absent",
6494
+ spanId: "absent",
6495
+ timestamp,
6496
+ [idField]: "stable-id",
6497
+ userId: "span-user",
6498
+ tags: ["provided"]
6499
+ }, contextMode);
6500
+ expect(response.status, await response.clone().text()).toBe(200);
6501
+ expect(await response.json()).toEqual({
6502
+ success: true,
6503
+ data: {
6504
+ [idField]: "stable-id",
6505
+ traceId: "absent",
6506
+ spanId: "absent",
6507
+ timestamp: "2026-01-01T00:00:00.123Z"
6508
+ }
6509
+ });
6510
+ expect(lookup).not.toHaveBeenCalled();
6511
+ expect(spanLookup).not.toHaveBeenCalled();
6512
+ const [row] = await list();
6513
+ expect(row).toMatchObject({
6514
+ userId: "span-user",
6515
+ tags: ["provided"],
6516
+ timestamp: "2026-01-01T00:00:00.123Z"
6517
+ });
6518
+ expect(row).not.toHaveProperty("contextMode");
6519
+ expect(row).not.toHaveProperty("rootEntityId");
6520
+ if (isFeedback) expect(row.feedbackUserId).toBeNull();
6521
+ });
6522
+ it("rejects unsupported modes and invalid timestamps before writing", async () => {
6523
+ for (const contextMode of [
6524
+ "automatic",
6525
+ null,
6526
+ false
6527
+ ]) expect((await post({
6528
+ ...payload,
6529
+ traceId: "trace-123"
6530
+ }, contextMode)).status).toBe(400);
6531
+ for (const timestamp of [
6532
+ "invalid",
6533
+ null,
6534
+ 123
6535
+ ]) expect((await post({
6536
+ ...payload,
6537
+ traceId: "trace-123",
6538
+ timestamp
6539
+ })).status).toBe(400);
6540
+ expect(await list()).toEqual([]);
6541
+ });
6542
+ it("requires the root before accepting a resolved annotation, even if the target exists", async () => {
6543
+ await writeSpan("child", "missing-root");
6544
+ for (const spanId of [
6545
+ void 0,
6546
+ "child",
6547
+ "missing-target"
6548
+ ]) {
6549
+ const response = await post({
6550
+ ...payload,
6551
+ traceId: "trace-123",
6552
+ spanId,
6553
+ tags: ["supplied"]
6554
+ }, "resolve");
6555
+ expect(response.status).toBe(404);
6556
+ expect(await response.json()).toMatchObject({ code: "TRACE_NOT_FOUND" });
6557
+ }
6558
+ await writeSpan("root");
6559
+ const response = await post({
6560
+ ...payload,
6561
+ traceId: "trace-123",
6562
+ spanId: "missing-target"
6563
+ }, "resolve");
6564
+ expect(response.status).toBe(404);
6565
+ expect(await response.json()).toMatchObject({ code: "SPAN_NOT_FOUND" });
6566
+ expect(await list()).toEqual([]);
6567
+ });
6568
+ it.each([void 0, "child"])("resolves root tags and target context for span %j", async (spanId) => {
6569
+ await writeSpan("root");
6570
+ await writeSpan("child", "root");
6571
+ const response = await post({
6572
+ ...payload,
6573
+ traceId: "trace-123",
6574
+ spanId,
6575
+ userId: "spoofed",
6576
+ tags: ["spoofed"],
6577
+ entityId: "spoofed",
6578
+ feedbackUserId: "reviewer",
6579
+ metadata: { override: "request" }
6580
+ }, "resolve");
6581
+ expect(response.status, await response.clone().text()).toBe(200);
6582
+ const [row] = await list();
6583
+ expect(row).toMatchObject({
6584
+ userId: "span-user",
6585
+ tags: ["root-tag"],
6586
+ entityId: spanId ?? "root",
6587
+ rootEntityId: "root",
6588
+ metadata: {
6589
+ inherited: true,
6590
+ override: "request",
6591
+ userId: "metadata-user"
6592
+ }
6593
+ });
6594
+ if (spanId) expect(row.parentEntityId).toBe("root");
6595
+ if (isFeedback) expect(row.feedbackUserId).toBe("reviewer");
6596
+ expect(row).not.toHaveProperty("contextMode");
6597
+ });
6598
+ it.each([
6599
+ void 0,
6600
+ "older-root",
6601
+ "child"
6602
+ ])("uses the target ancestor root, or latest root without a target, for span %j", async (spanId) => {
6603
+ await writeSpan("older-root", null, { tags: ["older"] });
6604
+ await writeSpan("parent", "older-root");
6605
+ await writeSpan("child", "parent");
6606
+ await writeSpan("newer-root", null, {
6607
+ endedAt: /* @__PURE__ */ new Date("2026-01-01T00:00:02Z"),
6608
+ tags: ["newer"]
6609
+ });
6610
+ const response = await post({
6611
+ ...payload,
6612
+ traceId: "trace-123",
6613
+ spanId
6614
+ }, "resolve");
6615
+ expect(response.status, await response.clone().text()).toBe(200);
6616
+ const [row] = await list();
6617
+ expect(row).toMatchObject({
6618
+ rootEntityId: spanId ? "older-root" : "newer-root",
6619
+ tags: spanId ? ["older"] : ["newer"],
6620
+ entityId: spanId ?? "newer-root"
6621
+ });
6622
+ if (spanId === "child") expect(row.parentEntityId).toBe("parent");
6623
+ });
6624
+ it.each(["missing", "cycle"])("rejects a target with a %s ancestor chain", async (chain) => {
6625
+ await writeSpan("unrelated-root");
6626
+ await writeSpan("child", "parent");
6627
+ await writeSpan("parent", chain === "cycle" ? "child" : "missing-root");
6628
+ const response = await post({
6629
+ ...payload,
6630
+ traceId: "trace-123",
6631
+ spanId: "child"
6632
+ }, "resolve");
6633
+ expect(response.status).toBe(404);
6634
+ expect(await response.json()).toMatchObject({ code: "TRACE_NOT_FOUND" });
6635
+ expect(await list()).toEqual([]);
6636
+ });
6637
+ it("enforces trusted organization scope and authenticated feedback attribution", async () => {
6638
+ trustedScope = {
6639
+ organizationId: "trusted-org",
6640
+ mastra__user: { id: "authenticated-reviewer" }
6641
+ };
6642
+ const response = await post({
6643
+ ...payload,
6644
+ traceId: "absent",
6645
+ organizationId: "spoofed-org",
6646
+ feedbackUserId: "spoofed-author",
6647
+ userId: "span-user"
6648
+ }, "provided");
6649
+ expect(response.status, await response.clone().text()).toBe(200);
6650
+ const [row] = await list();
6651
+ expect(row).toMatchObject({
6652
+ organizationId: "trusted-org",
6653
+ userId: "span-user"
6654
+ });
6655
+ if (isFeedback) expect(row.feedbackUserId).toBe("authenticated-reviewer");
6656
+ await writeSpan("root");
6657
+ const resolved = await post({
6658
+ ...payload,
6659
+ traceId: "trace-123",
6660
+ organizationId: "trusted-org"
6661
+ }, "resolve");
6662
+ expect(resolved.status).toBe(404);
6663
+ expect(await resolved.json()).toMatchObject({ code: "TRACE_NOT_FOUND" });
6664
+ });
6665
+ it.each([{
6666
+ field: "organizationId",
6667
+ contextKey: "organizationId"
6668
+ }, {
6669
+ field: "resourceId",
6670
+ contextKey: "mastra__resourceId"
6671
+ }])("does not let a caller override trusted $field during resolution", async ({ field, contextKey }) => {
6672
+ trustedScope = { [contextKey]: "trusted" };
6673
+ await writeSpan("trusted-root", null, { [field]: "trusted" });
6674
+ await writeSpan("foreign-root", null, { [field]: "foreign" });
6675
+ const rejected = await post({
6676
+ ...payload,
6677
+ traceId: "trace-123",
6678
+ spanId: "foreign-root",
6679
+ [field]: "foreign"
6680
+ }, "resolve");
6681
+ expect(rejected.status).toBe(404);
6682
+ expect(await rejected.json()).toMatchObject({ code: "SPAN_NOT_FOUND" });
6683
+ expect(await list()).toEqual([]);
6684
+ const accepted = await post({
6685
+ ...payload,
6686
+ traceId: "trace-123",
6687
+ spanId: "trusted-root",
6688
+ [field]: "foreign"
6689
+ }, "resolve");
6690
+ expect(accepted.status, await accepted.clone().text()).toBe(200);
6691
+ expect(await list()).toEqual([expect.objectContaining({
6692
+ [field]: "trusted",
6693
+ entityId: "trusted-root",
6694
+ rootEntityId: "trusted-root"
6695
+ })]);
6696
+ });
6697
+ it("allows self-hosted resolution without configured tenant scope", async () => {
6698
+ await writeSpan("root", null, {
6699
+ organizationId: "stored-org",
6700
+ resourceId: "stored-resource"
6701
+ });
6702
+ const response = await post({
6703
+ ...payload,
6704
+ traceId: "trace-123",
6705
+ organizationId: "caller-org",
6706
+ resourceId: "caller-resource"
6707
+ }, "resolve");
6708
+ expect(response.status, await response.clone().text()).toBe(200);
6709
+ expect(await list()).toEqual([expect.objectContaining({
6710
+ organizationId: "stored-org",
6711
+ resourceId: "stored-resource",
6712
+ rootEntityId: "root"
6713
+ })]);
6714
+ });
6715
+ it("still reads historical feedback and scores with no trace", async () => {
6716
+ const timestamp = /* @__PURE__ */ new Date();
6717
+ if (isFeedback) await store.createFeedback({ feedback: {
6718
+ feedbackId: "historical",
6719
+ timestamp,
6720
+ traceId: null,
6721
+ feedbackType: "thumbs",
6722
+ value: 1
6723
+ } });
6724
+ else await store.createScore({ score: {
6725
+ scoreId: "historical",
6726
+ timestamp,
6727
+ traceId: null,
6728
+ scorerId: "accuracy",
6729
+ score: .9
6730
+ } });
6731
+ expect(await list()).toEqual([expect.objectContaining({
6732
+ [idField]: "historical",
6733
+ traceId: null
6734
+ })]);
6735
+ });
6736
+ });
6737
+ }
6738
+ export { consumeSSEStream, createBodyLimitTestSuite, createCustomRouteFGATestSuite, createDefaultTestContext, createDurableDisconnectTestSuite, createHttpLoggingTestSuite, createKnowledgeRoutesTestSuite, createMCPRouteTestSuite, createMCPTransportTestSuite, createMultipartTestSuite, createObservabilitySignalWriteTestSuite, createRouteAdapterTestSuite, createStreamWithSensitiveData, createStreamWithUnserializableChunk, createStructuredWakeTestSuite, expectSerializedStreamChunks };
5726
6739
 
5727
6740
  //# sourceMappingURL=index.js.map