Skip to content

⭐️⭐️⭐️ Open-source, self-hosted alternative to Linear and Plane. Project management and issue tracking where teams and AI agents work side by side to plan and ship products.

test(api): flush agent traces before the next test resets the database (#558)

Pasqual Tronconec6941b2fab71
diff --git a/apps/api/AGENTS.md b/apps/api/AGENTS.md
index 9d5e1e8b..e2dbdacd 100644
--- a/apps/api/AGENTS.md
+++ b/apps/api/AGENTS.md
@@ -445,6 +445,11 @@ directly). Helpers in `src/__tests__/helpers/`: `api` (anonymous client), `authe
- The first `signUpTestUser` in a test is `god` (fresh DB per `resetDb`). To act as a
plain user, create the god user first and act as the second.
- Don't hardcode ids or the "-42" sequence — read them from the create response.
+- A test that runs an internal agent against a model leaves its spans in the trace
+ exporter, which writes them in batches up to 5 s later. By then the next test has reset
+ the database and reused the ids, so the late spans count as that test's runs. Flush them
+ before the test ends, in `afterEach` or right after the run:
+ `await getMastra().observability.flush()`.
## Rules
diff --git a/apps/api/src/modules/agents/core/__tests__/integration/ai-agents.test.ts b/apps/api/src/modules/agents/core/__tests__/integration/ai-agents.test.ts
index c6707153..bfc1464d 100644
--- a/apps/api/src/modules/agents/core/__tests__/integration/ai-agents.test.ts
+++ b/apps/api/src/modules/agents/core/__tests__/integration/ai-agents.test.ts
@@ -9,6 +9,7 @@ import { resetDb } from '#tests/helpers/db';
import { createAgent, projectIdOf } from '#tests/helpers/agents';
import { createCredential } from '#tests/helpers/integrations';
import { untaggedRoutes } from '#tests/helpers/mcp';
+import { getMastra } from '../../runtime/observability';
// AI agents owned by a team. Each agent is backed by a hidden bot user, owns a
// better-auth API key, and is a member of the projects it is attached to, acting under
@@ -360,6 +361,7 @@ describe('ai agents', () => {
expect(endpoint.bodies.at(-1)).not.toHaveProperty('reasoning_effort');
} finally {
endpoint.stop();
+ await getMastra().observability.flush();
}
});
diff --git a/apps/api/src/modules/agents/core/__tests__/integration/run-step-limit.test.ts b/apps/api/src/modules/agents/core/__tests__/integration/run-step-limit.test.ts
index 9ea951e5..ccb12725 100644
--- a/apps/api/src/modules/agents/core/__tests__/integration/run-step-limit.test.ts
+++ b/apps/api/src/modules/agents/core/__tests__/integration/run-step-limit.test.ts
@@ -7,6 +7,7 @@ import { createCredential } from '#tests/helpers/integrations';
import { claimDueRuns, enqueueAgentRun } from '../../run-queue';
import { processAgentRuns } from '../../run-poller';
import { STEP_LIMIT_MESSAGE, streamAgent, type AgentRunEvent } from '../../runtime';
+import { getMastra } from '../../runtime/observability';
// An OpenAI-compatible model that asks for a tool on every call, so the run ends only
// because it reaches maxSteps; with `answers` set it replies once and stops instead.
@@ -65,6 +66,7 @@ describe('agent run step limit', () => {
afterEach(async () => {
await endpoint.stop(true);
+ await getMastra().observability.flush();
});
async function setup() {
diff --git a/apps/api/src/modules/agents/core/__tests__/integration/run-timeout.test.ts b/apps/api/src/modules/agents/core/__tests__/integration/run-timeout.test.ts
index e9813ea9..3945c5e7 100644
--- a/apps/api/src/modules/agents/core/__tests__/integration/run-timeout.test.ts
+++ b/apps/api/src/modules/agents/core/__tests__/integration/run-timeout.test.ts
@@ -9,6 +9,7 @@ import { createAgent } from '#tests/helpers/agents';
import { createCredential } from '#tests/helpers/integrations';
import { claimDueRuns, enqueueAgentRun } from '../../run-queue';
import { processAgentRuns } from '../../run-poller';
+import { getMastra } from '../../runtime/observability';
// A run that outlasts the team's maxRunSeconds. The model endpoint accepts the request
// and never answers, so the run ends only because its abort signal fires.
@@ -24,6 +25,7 @@ describe('agent run timeout', () => {
afterEach(async () => {
clearLimits();
await endpoint.stop(true);
+ await getMastra().observability.flush();
});
it('ends a run that exhausts its time as failed and does not retry it', async () => {