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.

fix(api): a timed-out agent run ends as failed (#512)

Pasqual Tronconebcab65dd6577
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
new file mode 100644
index 00000000..e9813ea9
--- /dev/null
+++ b/apps/api/src/modules/agents/core/__tests__/integration/run-timeout.test.ts
@@ -0,0 +1,67 @@
+import { describe, it, expect, beforeEach, afterEach } from 'bun:test';
+import { db, agentRun } from '@repo/db';
+import { eq } from 'drizzle-orm';
+import { authedApi } from '#tests/helpers/app';
+import { signUpTestUser } from '#tests/helpers/auth';
+import { resetDb } from '#tests/helpers/db';
+import { clearLimits, setLimits } from '#tests/helpers/limits';
+import { createAgent } from '#tests/helpers/agents';
+import { createCredential } from '#tests/helpers/integrations';
+import { claimDueRuns, enqueueAgentRun } from '../../run-queue';
+import { processAgentRuns } from '../../run-poller';
+
+// 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.
+
+describe('agent run timeout', () => {
+ let endpoint: ReturnType<typeof Bun.serve>;
+
+ beforeEach(async () => {
+ await resetDb();
+ endpoint = Bun.serve({ port: 0, fetch: () => new Promise<Response>(() => {}) });
+ });
+
+ afterEach(async () => {
+ clearLimits();
+ await endpoint.stop(true);
+ });
+
+ it('ends a run that exhausts its time as failed and does not retry it', async () => {
+ const owner = await signUpTestUser({ name: 'Owner' });
+ const asOwner = authedApi(owner.cookie);
+ await asOwner.projects.post({ key: 'MKT', name: 'Marketing' });
+ const view = await asOwner.projects({ projectKey: 'MKT' }).get();
+ const credentialId = await createCredential(asOwner, 'MKT', {
+ integrationKey: 'openai-compatible',
+ credential: { apiKey: 'sk-test', baseUrl: `http://localhost:${endpoint.port}/v1` },
+ });
+ const agent = (
+ await createAgent(asOwner, 'MKT', {
+ name: 'Bot',
+ username: 'bot',
+ kind: 'internal',
+ modelCredentialId: credentialId,
+ model: 'any-model',
+ })
+ ).data!.agent;
+ const issue = (
+ await asOwner
+ .projects({ projectKey: 'MKT' })
+ .issues.post({ columnId: view.data!.columns[0].id, title: 'Task' })
+ ).data!;
+ await enqueueAgentRun({
+ agentId: agent.id,
+ projectId: agent.projects[0].id,
+ issueId: issue.id,
+ sourceActivityId: null,
+ prompt: 'do it',
+ });
+ setLimits({ maxRunSeconds: 1 });
+
+ await processAgentRuns();
+
+ const [row] = await db.select().from(agentRun).where(eq(agentRun.issueId, issue.id));
+ expect(row).toMatchObject({ status: 'failed', attempts: 1, lastError: 'Timed out after 1s' });
+ expect((await claimDueRuns()).find((r) => r.id === row.id)).toBeUndefined();
+ });
+});
diff --git a/apps/api/src/modules/agents/core/run-poller.ts b/apps/api/src/modules/agents/core/run-poller.ts
index 58a028f9..5e78d4ba 100644
--- a/apps/api/src/modules/agents/core/run-poller.ts
+++ b/apps/api/src/modules/agents/core/run-poller.ts
@@ -47,6 +47,7 @@ async function processRun(run: ClaimedRun): Promise<void> {
// The issue's timeline entries are written here, where the agent's work actually
// starts and ends. A failure that will be retried is not the end of the run, so only
// the last attempt logs one.
+ const timeoutMs = runTimeoutMs(maxRunSeconds);
try {
await agentRunStarted(run);
const result = await runAgent(run.agentId, run.projectId, framePrompt(run), {
@@ -55,8 +56,15 @@ async function processRun(run: ClaimedRun): Promise<void> {
issueId: run.issueId,
scheduleId: run.scheduleId,
contextPreamble: runModePreamble(run.trigger) + peopleContext(run),
- abortSignal: AbortSignal.timeout(runTimeoutMs(maxRunSeconds)),
+ abortSignal: AbortSignal.timeout(timeoutMs),
});
+ if (result.aborted) {
+ // Mastra resolves generate() on abort, so the partial text would read as a success.
+ // Not retried: hitting the time ceiling is the workload, not a transient error.
+ await recordAgentRunFinished(run, 'failed');
+ await markRunFailed(run.id, `Timed out after ${Math.round(timeoutMs / 1000)}s`);
+ return;
+ }
await recordAgentRunFinished(run, 'success');
await markRunSuccess(run.id, result.text, result.usage);
} catch (error) {
diff --git a/apps/api/src/modules/agents/core/runtime/index.ts b/apps/api/src/modules/agents/core/runtime/index.ts
index 6f033892..115048e9 100644
--- a/apps/api/src/modules/agents/core/runtime/index.ts
+++ b/apps/api/src/modules/agents/core/runtime/index.ts
@@ -152,19 +152,32 @@ async function buildAgent(
// thread: threadId identifies the conversation (a new one is created when omitted)
// and the caller (callerUserId) owns it. The thread id used is returned so the
// caller can continue the conversation; it is null when memory is off.
+//
+// `aborted` is true when opts.abortSignal fired: Mastra then resolves with the partial
+// text instead of rejecting, so only this tells it from a normal finish.
export async function runAgent(
agentId: number,
projectId: number,
prompt: string,
opts: RunOpts,
-): Promise<{ text: string; threadId: string | null; usage: ContextUsage | null }> {
+): Promise<{
+ text: string;
+ threadId: string | null;
+ usage: ContextUsage | null;
+ aborted: boolean;
+}> {
const { agent, row, options, threadId } = await prepareRun(agentId, projectId, prompt, opts);
const result = await agent.generate(prompt, { ...options, abortSignal: opts.abortSignal });
const usage = contextOf(result.usage);
// A chat thread keeps one number, replaced by each answer. An autonomous run keeps
// the counts of that run instead, on its own row, so its caller stores them.
if (threadId && isChatThreadId(threadId)) await recordContextUsage(threadId, row.id, usage);
- return { text: (result.text ?? '').trim(), threadId, usage };
+ return {
+ text: (result.text ?? '').trim(),
+ threadId,
+ usage,
+ aborted: opts.abortSignal?.aborted ?? false,
+ };
}
// What the model reported about the last call of an answer, as the pair the chat keeps.