Skip to content

🤯 LobeHub - Super-Agents on Your Team. Add super-agents to the chats your team already uses.They take on work by themselves and get better every week.

30.3 KB
Raw Blame
import { eq } from 'drizzle-orm';
import { beforeEach, describe, expect, it, vi } from 'vitest';

import { AgentEvalRunModel, AgentEvalRunTopicModel } from '@/database/models/agentEval';
import { ThreadModel } from '@/database/models/thread';
import { agentEvalEnvironments, agentEvalTestCases, messages, topics } from '@/database/schemas';
import { AgentEvalMemoryService } from '@/server/services/agentEvalMemory';
import { AgentEvalRunService } from '@/server/services/agentEvalRun';
import type * as AgentEvalRunWorkflowModule from '@/server/workflows/agentEvalRun';
import { AgentEvalRunWorkflow } from '@/server/workflows/agentEvalRun';

import { cleanupDB, serverDB, setupEvalChain, userId } from './_setup';

const mockExecAgent = vi.fn();

vi.mock('@/server/services/aiAgent', () => ({
  AiAgentService: vi.fn().mockImplementation(function () {
    return {
      execAgent: mockExecAgent,
    };
  }),
}));

vi.mock('@/server/workflows/agentEvalRun', async (importOriginal) => {
  const actual = (await importOriginal()) as typeof AgentEvalRunWorkflowModule;

  return {
    ...actual,
    AgentEvalRunWorkflow: {
      ...actual.AgentEvalRunWorkflow,
      triggerFinalizeRun: vi.fn().mockResolvedValue({}),
      triggerResumeAgentTrajectory: vi.fn().mockResolvedValue({}),
      triggerResumeThreadTrajectory: vi.fn().mockResolvedValue({}),
    },
  };
});

vi.mock('@/envs/app', () => ({
  appEnv: { APP_URL: 'https://test.example.com' },
}));

vi.mock('@/server/services/agentRuntime/AgentRuntimeService', () => ({
  AgentRuntimeService: vi.fn().mockImplementation(function () {
    return {
      interruptOperation: vi.fn().mockResolvedValue(true),
    };
  }),
}));

const markTopicTimeout = async (params: { runId: string; testCaseId: string; topicId: string }) => {
  const runTopicModel = new AgentEvalRunTopicModel(serverDB, userId);

  await runTopicModel.updateByRunAndTopic(params.runId, params.topicId, {
    evalResult: { completionReason: 'timeout', duration: 1000, rubricScores: [] },
    passed: false,
    score: 0,
    status: 'timeout',
  });

  return runTopicModel.findByRunAndTestCase(params.runId, params.testCaseId);
};

beforeEach(async () => {
  await cleanupDB();
  mockExecAgent.mockReset();
  vi.mocked(AgentEvalRunWorkflow.triggerResumeAgentTrajectory).mockReset();
  vi.mocked(AgentEvalRunWorkflow.triggerResumeThreadTrajectory).mockReset();
});

const resumedTrajectoryInput = (
  run: { id: string },
  testCase: { id: string },
  topic: { id: string },
): Parameters<AgentEvalRunService['executeResumedTrajectory']>[0] => ({
  appContext: { topicId: topic.id },
  parentMessageId: 'parent-message-id',
  runId: run.id,
  testCaseId: testCase.id,
  topicId: topic.id,
  userId,
});

describe('AgentEvalRunService', () => {
  describe('canResumeTrajectory', () => {
    it.each(['running', 'failed', 'aborted'] as const)(
      'should allow timeout trajectory resume when run status is %s',
      async (status) => {
        const { run, testCase, topic } = await setupEvalChain({ totalCases: 1 });
        await new AgentEvalMemoryService(serverDB, userId).prepare({
          fixture: {},
          runId: run.id,
          testCaseId: testCase.id,
          topicId: topic.id,
        });

        await new AgentEvalRunModel(serverDB, userId).update(run.id, { status });
        await markTopicTimeout({ runId: run.id, testCaseId: testCase.id, topicId: topic.id });

        await expect(
          new AgentEvalRunService(serverDB, userId).canResumeTrajectory({
            runId: run.id,
            testCaseId: testCase.id,
          }),
        ).resolves.toEqual({ canResume: true });
      },
    );

    it('should reject completed runs', async () => {
      const { run, testCase, topic } = await setupEvalChain({ totalCases: 1 });

      await new AgentEvalRunModel(serverDB, userId).update(run.id, { status: 'completed' });
      await markTopicTimeout({ runId: run.id, testCaseId: testCase.id, topicId: topic.id });

      await expect(
        new AgentEvalRunService(serverDB, userId).canResumeTrajectory({
          runId: run.id,
          testCaseId: testCase.id,
        }),
      ).resolves.toEqual({
        canResume: false,
        reason: 'Trajectory is not resumable',
      });
    });

    it('should reject trajectories that are neither timeout nor error', async () => {
      const { run, testCase } = await setupEvalChain({ totalCases: 1 });

      await new AgentEvalRunModel(serverDB, userId).update(run.id, { status: 'failed' });

      await expect(
        new AgentEvalRunService(serverDB, userId).canResumeTrajectory({
          runId: run.id,
          testCaseId: testCase.id,
        }),
      ).resolves.toEqual({
        canResume: false,
        reason: 'Trajectory is not resumable',
      });
    });

    it('should require threadId for pass@k resume', async () => {
      const { run, testCase, topic } = await setupEvalChain({ totalCases: 1 });

      await new AgentEvalRunModel(serverDB, userId).update(run.id, {
        config: { k: 2 },
        status: 'failed',
      });
      await markTopicTimeout({ runId: run.id, testCaseId: testCase.id, topicId: topic.id });

      await expect(
        new AgentEvalRunService(serverDB, userId).canResumeTrajectory({
          runId: run.id,
          testCaseId: testCase.id,
        }),
      ).resolves.toEqual({
        canResume: false,
        reason: 'Invalid resume target',
      });
    });

    it('should reject threads outside the target eval trajectory', async () => {
      const { run, testCase, topic } = await setupEvalChain({ totalCases: 1 });

      await new AgentEvalRunModel(serverDB, userId).update(run.id, {
        config: { k: 2 },
        status: 'failed',
      });
      await markTopicTimeout({ runId: run.id, testCaseId: testCase.id, topicId: topic.id });

      const [{ id: otherTopicId }] = await serverDB
        .insert(topics)
        .values({ mode: 'test', title: 'Other Eval Topic', trigger: 'eval', userId })
        .returning({ id: topics.id });
      const otherThread = await new ThreadModel(serverDB, userId).create({
        topicId: otherTopicId,
        type: 'eval',
      });

      await expect(
        new AgentEvalRunService(serverDB, userId).canResumeTrajectory({
          runId: run.id,
          testCaseId: testCase.id,
          threadId: otherThread!.id,
        }),
      ).resolves.toEqual({
        canResume: false,
        reason: 'Invalid resume target',
      });
    });

    it('should allow pass@k resume when the case passed but a thread errored', async () => {
      const { run, testCase, topic } = await setupEvalChain({ totalCases: 1 });

      await new AgentEvalRunModel(serverDB, userId).update(run.id, {
        config: { k: 2 },
        status: 'failed',
      });

      const threadModel = new ThreadModel(serverDB, userId);
      const errorThread = await threadModel.create({ topicId: topic.id, type: 'eval' });
      const passedThread = await threadModel.create({ topicId: topic.id, type: 'eval' });
      await new AgentEvalMemoryService(serverDB, userId).prepare({
        fixture: {},
        runId: run.id,
        testCaseId: testCase.id,
        topicId: topic.id,
        threadId: errorThread!.id,
      });

      await new AgentEvalRunTopicModel(serverDB, userId).updateByRunAndTopic(run.id, topic.id, {
        evalResult: {
          passAllK: false,
          passAtK: true,
          threads: [
            { status: 'error', threadId: errorThread!.id },
            { passed: true, status: 'passed', threadId: passedThread!.id },
          ],
        },
        passed: true,
        score: 1,
        status: 'passed',
      });

      await expect(
        new AgentEvalRunService(serverDB, userId).canResumeTrajectory({
          runId: run.id,
          testCaseId: testCase.id,
          threadId: errorThread!.id,
        }),
      ).resolves.toEqual({ canResume: true });
    });

    it('should enforce maxSteps using the target thread history for pass@k resume', async () => {
      const { run, testCase, topic } = await setupEvalChain({ totalCases: 1 });

      await new AgentEvalRunModel(serverDB, userId).update(run.id, {
        config: { k: 2, maxSteps: 5 },
        status: 'failed',
      });

      const threadModel = new ThreadModel(serverDB, userId);
      const errorThread = await threadModel.create({ topicId: topic.id, type: 'eval' });
      const passedThread = await threadModel.create({ topicId: topic.id, type: 'eval' });
      await new AgentEvalMemoryService(serverDB, userId).prepare({
        fixture: {},
        runId: run.id,
        testCaseId: testCase.id,
        topicId: topic.id,
        threadId: errorThread!.id,
      });

      await new AgentEvalRunTopicModel(serverDB, userId).updateByRunAndTopic(run.id, topic.id, {
        evalResult: {
          passAllK: false,
          passAtK: true,
          steps: 3,
          threads: [
            { status: 'error', threadId: errorThread!.id },
            { passed: true, status: 'passed', threadId: passedThread!.id },
          ],
        },
        passed: true,
        score: 1,
        status: 'passed',
      });

      await threadModel.update(errorThread!.id, {
        metadata: {
          completedAt: new Date('2026-03-30T00:00:00.000Z').toISOString(),
          status: 'error',
          steps: 5,
          testCaseId: testCase.id,
        },
      } as any);

      await expect(
        new AgentEvalRunService(serverDB, userId).canResumeTrajectory({
          runId: run.id,
          testCaseId: testCase.id,
          threadId: errorThread!.id,
        }),
      ).resolves.toEqual({
        canResume: false,
        reason: 'Resume limit reached',
      });
    });
  });

  describe.each([false, true])('Memory resume preflight (threaded=%s)', (threaded) => {
    it.each(['unknown', 'changed'] as const)(
      'reports %s fixture before scheduling or changing history',
      async (kind) => {
        const { run, testCase, topic } = await setupEvalChain({ totalCases: 1 });
        await new AgentEvalRunModel(serverDB, userId).update(run.id, {
          config: { k: threaded ? 2 : 1 },
          status: 'failed',
        });
        const thread = threaded
          ? await new ThreadModel(serverDB, userId).create({ topicId: topic.id, type: 'eval' })
          : undefined;
        const target = { runId: run.id, testCaseId: testCase.id, threadId: thread?.id };
        if (kind === 'changed') {
          await new AgentEvalMemoryService(serverDB, userId).prepare({
            ...target,
            topicId: topic.id,
            fixture: {},
          });
          const environment = { memory: { seededAt: '2024-05-01T12:00:00.000Z' } };
          if (threaded) {
            const [shared] = await serverDB
              .insert(agentEvalEnvironments)
              .values({
                config: environment,
                name: 'Changed shared fixture',
                userId,
              })
              .returning();
            await serverDB
              .update(agentEvalTestCases)
              .set({ environmentId: shared.id })
              .where(eq(agentEvalTestCases.id, testCase.id));
          } else {
            await serverDB
              .update(agentEvalTestCases)
              .set({
                content: { ...testCase.content, environment },
              })
              .where(eq(agentEvalTestCases.id, testCase.id));
          }
        }
        const runTopicModel = new AgentEvalRunTopicModel(serverDB, userId);
        await runTopicModel.updateByRunAndTopic(run.id, topic.id, {
          status: 'timeout',
          evalResult: {
            error: 'original evidence',
            ...(thread && { threads: [{ status: 'timeout', threadId: thread.id }] }),
          },
        });
        const [message] = await serverDB
          .insert(messages)
          .values({
            role: 'assistant',
            content: 'partial history',
            topicId: topic.id,
            userId,
          })
          .returning();
        const before = await runTopicModel.findByRunAndTestCase(run.id, testCase.id);
        const beforeMetadata = (await serverDB.select().from(topics))[0].metadata;
        const service = new AgentEvalRunService(serverDB, userId);
        const check = await service.canResumeTrajectory(target);
        expect(check).toMatchObject({
          canResume: false,
          reason: expect.stringContaining(
            kind === 'unknown' ? 'rerun this case' : 'Cannot change Memory fixture',
          ),
        });
        expect(await service.getResumableCases(run.id)).toMatchObject([
          { testCaseId: testCase.id, threadId: thread?.id, canResume: false, reason: check.reason },
        ]);
        await expect(service.resumeTrajectory(target)).rejects.toThrow(check.reason);
        expect(AgentEvalRunWorkflow.triggerResumeAgentTrajectory).not.toHaveBeenCalled();
        expect(AgentEvalRunWorkflow.triggerResumeThreadTrajectory).not.toHaveBeenCalled();
        expect(await runTopicModel.findByRunAndTestCase(run.id, testCase.id)).toEqual(before);
        expect((await serverDB.select().from(topics))[0].metadata).toEqual(beforeMetadata);
        expect(await serverDB.select().from(messages)).toContainEqual(message);
      },
    );
  });

  describe('getResumableCases', () => {
    it('should include pass@k cases that have resumable threads', async () => {
      const { run, testCase, topic } = await setupEvalChain({ totalCases: 1 });

      await new AgentEvalRunModel(serverDB, userId).update(run.id, {
        config: { k: 2 },
        status: 'failed',
      });

      const threadModel = new ThreadModel(serverDB, userId);
      const errorThread = await threadModel.create({ topicId: topic.id, type: 'eval' });
      const passedThread = await threadModel.create({ topicId: topic.id, type: 'eval' });
      await new AgentEvalMemoryService(serverDB, userId).prepare({
        fixture: {},
        runId: run.id,
        testCaseId: testCase.id,
        topicId: topic.id,
        threadId: errorThread!.id,
      });

      await new AgentEvalRunTopicModel(serverDB, userId).updateByRunAndTopic(run.id, topic.id, {
        evalResult: {
          passAllK: false,
          passAtK: true,
          threads: [
            { status: 'error', threadId: errorThread!.id },
            { passed: true, status: 'passed', threadId: passedThread!.id },
          ],
        },
        passed: true,
        score: 1,
        status: 'passed',
      });

      await expect(
        new AgentEvalRunService(serverDB, userId).getResumableCases(run.id),
      ).resolves.toEqual([
        {
          canResume: true,
          caseStatus: 'passed',
          input: testCase.content.input,
          reason: undefined,
          resumeStatus: 'error',
          sortOrder: testCase.sortOrder,
          testCaseId: testCase.id,
          threadId: errorThread!.id,
        },
      ]);
    });
  });

  describe('resumeTrajectory', () => {
    it('should only trigger workflow for a timed-out pass@1 trajectory without mutating state', async () => {
      const { run, testCase, topic } = await setupEvalChain({ totalCases: 1 });
      await new AgentEvalMemoryService(serverDB, userId).prepare({
        fixture: {},
        runId: run.id,
        testCaseId: testCase.id,
        topicId: topic.id,
      });

      await serverDB
        .update(agentEvalTestCases)
        .set({ metadata: { caseId: 'case-42' } })
        .where(eq(agentEvalTestCases.id, testCase.id));

      const [userMessage] = await serverDB
        .insert(messages)
        .values({
          content: 'What is 6*7?',
          role: 'user',
          topicId: topic.id,
          userId,
        })
        .returning({ id: messages.id });

      await serverDB.insert(messages).values({
        content: '...',
        parentId: userMessage.id,
        role: 'assistant',
        topicId: topic.id,
        userId,
      });

      const runModel = new AgentEvalRunModel(serverDB, userId);
      const runTopicModel = new AgentEvalRunTopicModel(serverDB, userId);

      await runModel.update(run.id, {
        metrics: {
          averageScore: 0,
          completedCases: 1,
          failedCases: 0,
          passRate: 0,
          passedCases: 0,
          timeoutCases: 1,
          totalCases: 1,
        },
        status: 'failed',
      });
      await markTopicTimeout({ runId: run.id, testCaseId: testCase.id, topicId: topic.id });

      const service = new AgentEvalRunService(serverDB, userId);
      const result = await service.resumeTrajectory({ runId: run.id, testCaseId: testCase.id });

      expect(result).toEqual({
        mode: 'single',
        runId: run.id,
        testCaseId: testCase.id,
        threadId: undefined,
        topicId: topic.id,
        triggered: true,
      });

      expect(AgentEvalRunWorkflow.triggerResumeAgentTrajectory).toHaveBeenCalledWith(
        expect.objectContaining({
          appContext: { topicId: topic.id },
          caseId: 'case-42',
          envPrompt: undefined,
          maxSteps: undefined,
          parentMessageId: userMessage.id,
          runId: run.id,
          testCaseId: testCase.id,
          topicId: topic.id,
          userId,
        }),
      );
      expect(mockExecAgent).not.toHaveBeenCalled();

      const refreshedRun = await runModel.findById(run.id);
      expect(refreshedRun?.status).toBe('failed');
      expect(refreshedRun?.metrics).toMatchObject({
        completedCases: 1,
        timeoutCases: 1,
      });

      const refreshedRunTopic = await runTopicModel.findByRunAndTestCase(run.id, testCase.id);
      expect(refreshedRunTopic?.status).toBe('timeout');
      expect(refreshedRunTopic?.evalResult).toEqual({
        completionReason: 'timeout',
        duration: 1000,
        rubricScores: [],
      });
    });

    it('should only trigger workflow for the target pass@k thread without mutating thread state', async () => {
      const { run, testCase, topic } = await setupEvalChain({ totalCases: 1 });

      await new AgentEvalRunModel(serverDB, userId).update(run.id, {
        config: { k: 2 },
        status: 'failed',
      });
      await markTopicTimeout({ runId: run.id, testCaseId: testCase.id, topicId: topic.id });

      const threadModel = new ThreadModel(serverDB, userId);
      const thread = await threadModel.create({ topicId: topic.id, type: 'eval' });
      const otherThread = await threadModel.create({ topicId: topic.id, type: 'eval' });
      await new AgentEvalMemoryService(serverDB, userId).prepare({
        fixture: {},
        runId: run.id,
        testCaseId: testCase.id,
        topicId: topic.id,
        threadId: thread!.id,
      });

      await new AgentEvalRunTopicModel(serverDB, userId).updateByRunAndTopic(run.id, topic.id, {
        evalResult: {
          passAllK: false,
          passAtK: true,
          threads: [
            { status: 'timeout', threadId: thread!.id },
            { passed: true, status: 'passed', threadId: otherThread!.id },
          ],
        },
        passed: true,
        score: 1,
        status: 'passed',
      });

      await threadModel.update(thread!.id, {
        metadata: {
          completedAt: new Date('2026-03-30T00:00:00.000Z').toISOString(),
          operationId: 'op-target-old',
          passed: false,
          score: 0,
          testCaseId: testCase.id,
        },
      } as any);
      await threadModel.update(otherThread!.id, {
        metadata: {
          completedAt: new Date('2026-03-30T00:00:01.000Z').toISOString(),
          operationId: 'op-other-thread',
          passed: true,
          score: 1,
          testCaseId: testCase.id,
        },
      } as any);

      const [threadUserMessage] = await serverDB
        .insert(messages)
        .values({
          content: 'thread user prompt',
          role: 'user',
          threadId: thread!.id,
          topicId: topic.id,
          userId,
        })
        .returning({ id: messages.id });

      await serverDB.insert(messages).values({
        content: '...',
        parentId: threadUserMessage.id,
        role: 'assistant',
        threadId: thread!.id,
        topicId: topic.id,
        userId,
      });

      const service = new AgentEvalRunService(serverDB, userId);
      const result = await service.resumeTrajectory({
        runId: run.id,
        testCaseId: testCase.id,
        threadId: thread!.id,
      });

      expect(result).toEqual({
        mode: 'thread',
        runId: run.id,
        testCaseId: testCase.id,
        threadId: thread!.id,
        topicId: topic.id,
        triggered: true,
      });

      expect(AgentEvalRunWorkflow.triggerResumeThreadTrajectory).toHaveBeenCalledWith(
        expect.objectContaining({
          appContext: { threadId: thread!.id, topicId: topic.id },
          envPrompt: undefined,
          maxSteps: undefined,
          parentMessageId: threadUserMessage.id,
          runId: run.id,
          testCaseId: testCase.id,
          threadId: thread!.id,
          topicId: topic.id,
          userId,
        }),
      );
      expect(mockExecAgent).not.toHaveBeenCalled();

      expect((await threadModel.findById(thread!.id))?.metadata).toMatchObject({
        operationId: 'op-target-old',
        testCaseId: testCase.id,
      });
      expect((await threadModel.findById(otherThread!.id))?.metadata).toMatchObject({
        operationId: 'op-other-thread',
        passed: true,
        score: 1,
        testCaseId: testCase.id,
      });
    });
  });

  describe('executeResumedTrajectory', () => {
    it('should claim a timeout trajectory, switch run back to running, and store operationId', async () => {
      const { run, testCase, topic } = await setupEvalChain({ totalCases: 1 });
      await new AgentEvalMemoryService(serverDB, userId).prepare({
        fixture: {},
        runId: run.id,
        testCaseId: testCase.id,
        topicId: topic.id,
      });

      await new AgentEvalRunModel(serverDB, userId).update(run.id, { status: 'failed' });
      await markTopicTimeout({ runId: run.id, testCaseId: testCase.id, topicId: topic.id });

      mockExecAgent.mockResolvedValue({ operationId: 'op-resume-1' });

      const service = new AgentEvalRunService(serverDB, userId);
      const result = await service.executeResumedTrajectory(
        resumedTrajectoryInput(run, testCase, topic),
      );

      expect(result).toEqual({ status: 'started', topicId: topic.id });
      expect(mockExecAgent).toHaveBeenCalledWith(
        expect.objectContaining({
          appContext: { suppressSignal: true, topicId: topic.id },
          parentMessageId: 'parent-message-id',
          prompt: '',
          resume: true,
        }),
      );

      const refreshedRun = await new AgentEvalRunModel(serverDB, userId).findById(run.id);
      expect(refreshedRun?.status).toBe('running');
      expect(refreshedRun?.startedAt).toBeTruthy();

      const refreshedRunTopic = await new AgentEvalRunTopicModel(
        serverDB,
        userId,
      ).findByRunAndTestCase(run.id, testCase.id);
      expect(refreshedRunTopic?.status).toBe('running');
      expect(refreshedRunTopic?.evalResult).toEqual({
        executionId: expect.any(String),
        operationId: 'op-resume-1',
      });
    });

    it('does not double-count LLM calls when the completion hook runs locally', async () => {
      const { run, testCase, topic } = await setupEvalChain({ totalCases: 1 });
      await new AgentEvalMemoryService(serverDB, userId).prepare({
        fixture: {},
        runId: run.id,
        testCaseId: testCase.id,
        topicId: topic.id,
      });
      const runTopicModel = new AgentEvalRunTopicModel(serverDB, userId);

      await new AgentEvalRunModel(serverDB, userId).update(run.id, { status: 'failed' });
      await runTopicModel.updateByRunAndTopic(run.id, topic.id, {
        evalResult: {
          completionReason: 'timeout',
          duration: 1000,
          llmCalls: 3,
          rubricScores: [],
        },
        passed: false,
        score: 0,
        status: 'timeout',
      });

      let capturedHooks: any[] = [];
      mockExecAgent.mockImplementation(async (params: any) => {
        capturedHooks = params.hooks ?? [];
        return { operationId: 'op-resume-2' };
      });

      await new AgentEvalRunService(serverDB, userId).executeResumedTrajectory(
        resumedTrajectoryInput(run, testCase, topic),
      );

      // The operation is seeded with the previous total, so the completion
      // event is already cumulative and must not have `prevLlmCalls` added again.
      expect(mockExecAgent).toHaveBeenCalledWith(expect.objectContaining({ initialLlmCalls: 3 }));

      await capturedHooks[0].handler({
        llmCalls: 7,
        reason: 'done',
        status: 'done',
        steps: 9,
      });

      const refreshed = await runTopicModel.findByRunAndTestCase(run.id, testCase.id);
      expect((refreshed?.evalResult as any)?.llmCalls).toBe(7);
    });

    it('keeps prior timeout evidence when the original Memory fixture was not recorded', async () => {
      const { run, testCase, topic } = await setupEvalChain({ totalCases: 1 });
      const runModel = new AgentEvalRunModel(serverDB, userId);
      const runTopicModel = new AgentEvalRunTopicModel(serverDB, userId);
      await runModel.update(run.id, { status: 'failed' });
      const before = await markTopicTimeout({
        runId: run.id,
        testCaseId: testCase.id,
        topicId: topic.id,
      });
      await expect(
        new AgentEvalRunService(serverDB, userId).executeResumedTrajectory({
          appContext: { topicId: topic.id },
          parentMessageId: 'parent-message-id',
          runId: run.id,
          testCaseId: testCase.id,
          topicId: topic.id,
          userId,
        }),
      ).resolves.toMatchObject({
        status: 'cancelled',
        reason: expect.stringContaining('rerun this case instead of resuming'),
      });
      expect(await runTopicModel.findByRunAndTestCase(run.id, testCase.id)).toEqual(before);
      expect((await runModel.findById(run.id))?.status).toBe('failed');
      expect(mockExecAgent).not.toHaveBeenCalled();
    });

    it('should cancel when the trajectory is not resumable', async () => {
      const { run, testCase, topic } = await setupEvalChain({ totalCases: 1 });

      await new AgentEvalRunModel(serverDB, userId).update(run.id, { status: 'failed' });

      const result = await new AgentEvalRunService(serverDB, userId).executeResumedTrajectory(
        resumedTrajectoryInput(run, testCase, topic),
      );

      expect(result).toEqual({
        reason: 'Trajectory is not resumable',
        status: 'cancelled',
        topicId: topic.id,
      });
      expect(mockExecAgent).not.toHaveBeenCalled();
    });
  });

  describe('executeResumedThreadTrajectory', () => {
    it('should claim only the target timeout thread and store operationId on that thread', async () => {
      const { run, testCase, topic } = await setupEvalChain({ totalCases: 1 });

      await new AgentEvalRunModel(serverDB, userId).update(run.id, {
        config: { k: 2 },
        status: 'failed',
      });
      await markTopicTimeout({ runId: run.id, testCaseId: testCase.id, topicId: topic.id });

      const threadModel = new ThreadModel(serverDB, userId);
      const thread = await threadModel.create({ topicId: topic.id, type: 'eval' });
      const otherThread = await threadModel.create({ topicId: topic.id, type: 'eval' });

      await new AgentEvalRunTopicModel(serverDB, userId).updateByRunAndTopic(run.id, topic.id, {
        evalResult: {
          passAllK: false,
          passAtK: true,
          threads: [
            { status: 'timeout', threadId: thread!.id },
            { passed: true, status: 'passed', threadId: otherThread!.id },
          ],
        },
        passed: true,
        score: 1,
        status: 'passed',
      });

      await threadModel.update(thread!.id, {
        metadata: {
          completedAt: new Date('2026-03-30T00:00:00.000Z').toISOString(),
          cost: 1.2,
          llmCalls: 3,
          operationId: 'op-old-target',
          passed: false,
          score: 0,
          status: 'timeout',
          steps: 7,
          testCaseId: testCase.id,
          tokens: 123,
          toolCalls: 2,
        },
      } as any);
      await threadModel.update(otherThread!.id, {
        metadata: {
          completedAt: new Date('2026-03-30T00:00:01.000Z').toISOString(),
          operationId: 'op-other-thread',
          passed: true,
          score: 1,
          testCaseId: testCase.id,
        },
      } as any);

      await new AgentEvalMemoryService(serverDB, userId).prepare({
        fixture: {},
        runId: run.id,
        testCaseId: testCase.id,
        threadId: thread!.id,
        topicId: topic.id,
      });

      mockExecAgent.mockResolvedValue({ operationId: 'op-thread-resume-1' });

      const result = await new AgentEvalRunService(serverDB, userId).executeResumedThreadTrajectory(
        {
          appContext: { threadId: thread!.id, topicId: topic.id },
          parentMessageId: 'parent-message-id',
          runId: run.id,
          testCaseId: testCase.id,
          threadId: thread!.id,
          topicId: topic.id,
          userId,
        },
      );

      expect(result).toEqual({ status: 'started', threadId: thread!.id, topicId: topic.id });
      expect(mockExecAgent).toHaveBeenCalledWith(
        expect.objectContaining({
          appContext: { suppressSignal: true, threadId: thread!.id, topicId: topic.id },
          initialLlmCalls: 3,
          initialStepCount: 7,
          parentMessageId: 'parent-message-id',
          prompt: '',
          resume: true,
        }),
      );

      const updatedThread = await threadModel.findById(thread!.id);
      expect(updatedThread?.metadata).toEqual({
        executionId: expect.any(String),
        startedAt: expect.any(String),
        status: 'running',
        operationId: 'op-thread-resume-1',
        testCaseId: testCase.id,
      });

      const untouchedThread = await threadModel.findById(otherThread!.id);
      expect(untouchedThread?.metadata).toMatchObject({
        operationId: 'op-other-thread',
        passed: true,
        score: 1,
        testCaseId: testCase.id,
      });

      const refreshedRunTopic = await new AgentEvalRunTopicModel(
        serverDB,
        userId,
      ).findByRunAndTestCase(run.id, testCase.id);
      expect(refreshedRunTopic?.status).toBe('running');
      expect(refreshedRunTopic?.evalResult).toEqual({
        executionId: updatedThread?.metadata?.executionId,
        threads: [
          {
            executionId: updatedThread?.metadata?.executionId,
            status: 'running',
            threadId: thread!.id,
          },
          { passed: true, status: 'passed', threadId: otherThread!.id },
        ],
      });
    });
  });
});