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.

47.2 KB
Raw Blame
// @vitest-environment node
import { eq } from 'drizzle-orm';
import { afterEach, beforeEach, describe, expect, it } from 'vitest';

import { matchesAgentInterventionContinuationProvenance } from '@/business/server/agent-run/agentInterventionIdentity';

import { getTestDB } from '../../core/getTestDB';
import { agentOperations, topics, users, workspaces } from '../../schemas';
import type { LobeChatDatabase } from '../../type';
import { AgentOperationModel } from '../agentOperation';

const serverDB: LobeChatDatabase = await getTestDB();

const userId = 'agent-operation-test-user-id';
const otherUserId = 'agent-operation-test-other-user';

beforeEach(async () => {
  await serverDB.delete(users);
  await serverDB.insert(users).values([{ id: userId }, { id: otherUserId }]);
});

afterEach(async () => {
  await serverDB.delete(agentOperations);
  await serverDB.delete(users);
});

describe('AgentOperationModel', () => {
  it('matches diagnostic source identity within both topic and owner', async () => {
    await serverDB.insert(topics).values({ id: 'diagnostic-topic', userId });
    const model = new AgentOperationModel(serverDB, userId);
    await model.recordStart({
      operationId: 'diagnostic-operation',
      topicId: 'diagnostic-topic',
      appContext: { sourceMessageId: 'server-incident-message' },
    });
    await model.recordStart({
      operationId: 'unrelated-operation',
      topicId: 'diagnostic-topic',
      appContext: { sourceMessageId: 'another-message' },
    });
    expect(
      (await model.findByTopicSourceMessage('diagnostic-topic', 'server-incident-message'))?.id,
    ).toBe('diagnostic-operation');
    expect(
      await model.findByTopicSourceMessage('different-topic', 'server-incident-message'),
    ).toBeUndefined();
    expect(
      await new AgentOperationModel(serverDB, otherUserId).findByTopicSourceMessage(
        'diagnostic-topic',
        'server-incident-message',
      ),
    ).toBeUndefined();
  });

  describe('recordStart', () => {
    it('inserts a row with status=running and the provided ids', async () => {
      const model = new AgentOperationModel(serverDB, userId);
      const operationId = 'op-start-1';

      await model.recordStart({
        appContext: { scope: 'chat', sourceMessageId: 'msg-1' },
        maxSteps: 20,
        model: 'gpt-4o',
        modelRuntimeConfig: { model: 'gpt-4o', provider: 'openai' },
        operationId,
        provider: 'openai',
        trigger: 'chat',
      });

      const row = await model.findById(operationId);
      expect(row).toMatchObject({
        appContext: { scope: 'chat', sourceMessageId: 'msg-1' },
        id: operationId,
        maxSteps: 20,
        model: 'gpt-4o',
        modelRuntimeConfig: { model: 'gpt-4o', provider: 'openai' },
        provider: 'openai',
        status: 'running',
        trigger: 'chat',
        userId,
      });
      expect(row?.startedAt).toBeInstanceOf(Date);
      expect(row?.completedAt).toBeNull();
    });

    it('persists the agent-signal marker into metadata so server tools can read it back', async () => {
      const model = new AgentOperationModel(serverDB, userId);
      const operationId = 'op-start-marker';
      // Server-side self-iteration tools resolve the review window / source id from
      // metadata.agentSignal (the trimmed appContext intentionally drops it). If
      // the marker is not persisted here, tools fall back to a 1970 window +
      // operationId source.
      const agentSignal = {
        agentId: 'agent_reviewed',
        kind: 'nightly-review',
        localDate: '2026-05-30',
        reviewWindowEnd: '2026-05-30T00:00:00.000Z',
        reviewWindowStart: '2026-05-29T00:00:00.000Z',
        sourceId: 'nightly-review:user:agent_reviewed:2026-05-30',
      };

      await model.recordStart({
        appContext: { scope: 'chat' },
        metadata: { agentSignal },
        operationId,
      });

      const row = await model.findById(operationId);
      expect(row?.metadata).toEqual({ agentSignal });
    });

    it('is idempotent on the primary key', async () => {
      const model = new AgentOperationModel(serverDB, userId);
      const operationId = 'op-start-2';

      await model.recordStart({ operationId });
      // Second call must not throw — primary-key conflict is swallowed.
      await model.recordStart({ operationId });

      const rows = await serverDB
        .select()
        .from(agentOperations)
        .where(eq(agentOperations.id, operationId));
      expect(rows).toHaveLength(1);
    });
  });

  describe('agent intervention dispatch recovery markers', () => {
    it('persists ready preparation and queue ACK without replacing provenance', async () => {
      const model = new AgentOperationModel(serverDB, userId);
      const operationId = 'op-intervention-marker';
      const provenance = {
        resolutionRequestId: 'request-intervention-marker',
        sourceOperationId: 'source-operation',
        sourceToolMessageIds: ['tool-message'],
      };
      await model.recordStart({
        metadata: { agentInterventionContinuation: provenance },
        operationId,
      });

      await expect(
        model.recordAgentInterventionPreparation(operationId, {
          deduplicationId: 'agent-intervention:op-intervention-marker:0',
          resolutionRequestId: provenance.resolutionRequestId,
          state: 'ready',
          stepIndex: 0,
        }),
      ).resolves.toBe(true);
      await expect(
        model.recordAgentInterventionDispatch(operationId, {
          deduplicationId: 'agent-intervention:op-intervention-marker:0',
          messageId: 'queue-message',
          resolutionRequestId: provenance.resolutionRequestId,
          scheduledAt: '2026-08-26T00:00:00.000Z',
          state: 'scheduled',
        }),
      ).resolves.toBe(true);

      const row = await model.findById(operationId);
      expect(
        matchesAgentInterventionContinuationProvenance(
          row?.metadata?.agentInterventionContinuation,
          provenance,
        ),
      ).toBe(true);
      expect(row?.metadata).toMatchObject({
        agentInterventionContinuation: provenance,
        agentInterventionDispatch: {
          deduplicationId: 'agent-intervention:op-intervention-marker:0',
          state: 'scheduled',
        },
        agentInterventionPreparation: {
          deduplicationId: 'agent-intervention:op-intervention-marker:0',
          state: 'ready',
          stepIndex: 0,
        },
      });
    });

    it('rejects a preparation marker from another request or owner', async () => {
      const model = new AgentOperationModel(serverDB, userId);
      const attacker = new AgentOperationModel(serverDB, otherUserId);
      const operationId = 'op-intervention-marker-authority';
      await model.recordStart({
        metadata: {
          agentInterventionContinuation: {
            resolutionRequestId: 'request-owner',
            sourceOperationId: 'source-operation',
            sourceToolMessageIds: ['tool-message'],
          },
        },
        operationId,
      });
      const marker = {
        deduplicationId: 'agent-intervention:op-intervention-marker-authority:0',
        resolutionRequestId: 'request-other',
        state: 'ready' as const,
        stepIndex: 0,
      };

      await expect(model.recordAgentInterventionPreparation(operationId, marker)).resolves.toBe(
        false,
      );
      await expect(
        attacker.recordAgentInterventionPreparation(operationId, {
          ...marker,
          resolutionRequestId: 'request-owner',
        }),
      ).resolves.toBe(false);
      expect((await model.findById(operationId))?.metadata).not.toHaveProperty(
        'agentInterventionPreparation',
      );
    });
  });

  describe('server-default relay invocation attestation', () => {
    it('records the first accepted invocation and reuses it for matching retries', async () => {
      const model = new AgentOperationModel(serverDB, userId);
      const operationId = 'op-server-default-relay';
      await model.recordStart({
        metadata: {
          agentType: 'trae',
          preserved: true,
          serverDefaultHeterogeneous: true,
        },
        model: 'gpt-5.4',
        operationId,
        provider: 'lobehub',
      });
      const first = {
        acceptedAt: '2026-09-01T00:00:00.000Z',
        agentType: 'trae',
        ingress: 'openai-responses' as const,
        model: 'gpt-5.4',
        operationId,
        provider: 'lobehub',
      };

      await expect(model.recordServerDefaultRelayInvocation(operationId, first)).resolves.toEqual(
        first,
      );
      await expect(
        model.recordServerDefaultRelayInvocation(operationId, {
          ...first,
          acceptedAt: '2026-09-01T00:01:00.000Z',
        }),
      ).resolves.toEqual(first);

      expect((await model.findById(operationId))?.metadata).toEqual({
        agentType: 'trae',
        preserved: true,
        serverDefaultHeterogeneous: true,
        serverDefaultRelayInvocation: first,
      });
    });

    it('rejects an unmarked, mismatched, terminal, or foreign operation', async () => {
      const model = new AgentOperationModel(serverDB, userId);
      const otherOwner = new AgentOperationModel(serverDB, otherUserId);
      const invocation = {
        acceptedAt: '2026-09-01T00:00:00.000Z',
        agentType: 'trae',
        ingress: 'openai-responses' as const,
        model: 'gpt-5.4',
        operationId: 'op-relay-authority',
        provider: 'lobehub',
      };

      await model.recordStart({
        metadata: { agentType: 'trae' },
        model: invocation.model,
        operationId: invocation.operationId,
        provider: invocation.provider,
      });
      await expect(
        model.recordServerDefaultRelayInvocation(invocation.operationId, invocation),
      ).resolves.toBeNull();

      await serverDB
        .update(agentOperations)
        .set({ metadata: { agentType: 'trae', serverDefaultHeterogeneous: true } })
        .where(eq(agentOperations.id, invocation.operationId));
      await expect(
        model.recordServerDefaultRelayInvocation(invocation.operationId, {
          ...invocation,
          model: 'gpt-5.5',
        }),
      ).resolves.toBeNull();
      await expect(
        model.recordServerDefaultRelayInvocation(invocation.operationId, {
          ...invocation,
          operationId: 'different-operation',
        }),
      ).resolves.toBeNull();
      await expect(
        otherOwner.recordServerDefaultRelayInvocation(invocation.operationId, invocation),
      ).resolves.toBeNull();

      await model.settleRunning(invocation.operationId, 'done');
      await expect(
        model.recordServerDefaultRelayInvocation(invocation.operationId, invocation),
      ).resolves.toBeNull();
      expect((await model.findById(invocation.operationId))?.metadata).not.toHaveProperty(
        'serverDefaultRelayInvocation',
      );
    });
  });

  describe('recordCompletion', () => {
    it('updates the row to a terminal status with aggregates and trace key', async () => {
      const model = new AgentOperationModel(serverDB, userId);
      const operationId = 'op-complete-1';

      const completedAt = new Date('2026-05-13T01:23:45.000Z');
      await model.recordStart({ operationId });
      await model.recordCompletion(operationId, {
        completedAt,
        completionReason: 'done',
        cost: { total: 0.123 },
        llmCalls: 4,
        processingTimeMs: 5432,
        status: 'done',
        stepCount: 7,
        toolCalls: 2,
        totalCost: 0.123,
        totalInputTokens: 1000,
        totalOutputTokens: 200,
        totalTokens: 1200,
        traceS3Key: 'agent-traces/agent-x/topic-x/op-complete-1.json',
        usage: { llm: { apiCalls: 4 } },
      });

      const row = await model.findById(operationId);
      expect(row).toMatchObject({
        completionReason: 'done',
        cost: { total: 0.123 },
        llmCalls: 4,
        processingTimeMs: 5432,
        status: 'done',
        stepCount: 7,
        toolCalls: 2,
        totalCost: 0.123,
        totalInputTokens: 1000,
        totalOutputTokens: 200,
        totalTokens: 1200,
        traceS3Key: 'agent-traces/agent-x/topic-x/op-complete-1.json',
      });
      expect(row?.completedAt?.toISOString()).toBe(completedAt.toISOString());
    });

    it('leaves completedAt null when not explicitly provided (e.g. waiting_for_human)', async () => {
      const model = new AgentOperationModel(serverDB, userId);
      const operationId = 'op-waiting';

      await model.recordStart({ operationId });
      await model.recordCompletion(operationId, {
        completionReason: 'waiting_for_human',
        status: 'waiting_for_human',
      });

      const row = await model.findById(operationId);
      expect(row?.status).toBe('waiting_for_human');
      expect(row?.completedAt).toBeNull();
    });

    it('writes error and interruption payloads on failure paths', async () => {
      const model = new AgentOperationModel(serverDB, userId);
      const operationId = 'op-complete-error';

      await model.recordStart({ operationId });
      await model.recordCompletion(operationId, {
        completedAt: new Date(),
        completionReason: 'error',
        error: { message: 'boom', type: 'AgentRuntimeError' },
        interruption: {
          canResume: false,
          interruptedAt: '2026-05-13T00:00:00.000Z',
          reason: 'rate_limited',
        },
        status: 'error',
      });

      const row = await model.findById(operationId);
      expect(row?.status).toBe('error');
      expect(row?.completionReason).toBe('error');
      expect(row?.error).toMatchObject({ message: 'boom', type: 'AgentRuntimeError' });
      expect(row?.interruption).toMatchObject({ canResume: false, reason: 'rate_limited' });
    });

    it('is a no-op when the start row was never written', async () => {
      const model = new AgentOperationModel(serverDB, userId);
      // No prior recordStart — recordCompletion must not throw and must not
      // create a phantom row.
      await model.recordCompletion('op-missing', { status: 'done', completionReason: 'done' });

      const row = await model.findById('op-missing');
      expect(row).toBeNull();
    });

    it('does not flip another user’s row when their operationId is known', async () => {
      const ownerModel = new AgentOperationModel(serverDB, userId);
      const attackerModel = new AgentOperationModel(serverDB, otherUserId);
      const operationId = 'op-cross-user';

      await ownerModel.recordStart({ operationId });
      await attackerModel.recordCompletion(operationId, {
        completedAt: new Date(),
        completionReason: 'error',
        error: { message: 'spoofed', type: 'AgentRuntimeError' },
        status: 'error',
      });

      // Owner's row must still read as running — the cross-user update is
      // filtered out by the userId scope in the WHERE clause.
      const row = await ownerModel.findById(operationId);
      expect(row?.status).toBe('running');
      expect(row?.error).toBeNull();
      // The attacker cannot read the row either.
      expect(await attackerModel.findById(operationId)).toBeNull();
    });
  });

  describe('mergeMetadata', () => {
    it('merges keys into existing metadata and is scoped to the owner', async () => {
      const model = new AgentOperationModel(serverDB, userId);
      const operationId = 'op-merge-metadata';
      await model.recordStart({ operationId });
      await serverDB
        .update(agentOperations)
        .set({ metadata: { existing: 1 } })
        .where(eq(agentOperations.id, operationId));

      expect(await model.mergeMetadata(operationId, { supersede: { kind: 'client_missed' } })).toBe(
        true,
      );
      expect((await model.findById(operationId))!.metadata).toEqual({
        existing: 1,
        supersede: { kind: 'client_missed' },
      });

      expect(
        await new AgentOperationModel(serverDB, otherUserId).mergeMetadata(operationId, {
          foreign: true,
        }),
      ).toBe(false);
      expect((await model.findById(operationId))!.metadata).not.toHaveProperty('foreign');
    });
  });

  describe('operation lease', () => {
    it('answers whether a run is still live on a given topic', async () => {
      // The ingest path falls back to this when a topic loses its
      // `runningOperation` marker: the row, not the marker, says whether the
      // producer behind a batch is still alive — and the topic pairing keeps
      // the answer from authorizing a write to someone else's topic.
      await serverDB.insert(topics).values([
        { id: 'live-topic', userId },
        { id: 'other-topic', userId },
      ]);
      const model = new AgentOperationModel(serverDB, userId);
      const operationId = 'op-live-on-topic';
      await model.recordStart({ operationId, topicId: 'live-topic' });

      expect(await model.isRunningOnTopic(operationId, 'live-topic')).toBe(true);
      // Bound to another topic, so it cannot vouch for a write to this one.
      expect(await model.isRunningOnTopic(operationId, 'other-topic')).toBe(false);
      // Another user's row is out of scope entirely.
      expect(
        await new AgentOperationModel(serverDB, otherUserId).isRunningOnTopic(
          operationId,
          'live-topic',
        ),
      ).toBe(false);

      await model.recordCompletion(operationId, { status: 'done' });
      expect(await model.isRunningOnTopic(operationId, 'live-topic')).toBe(false);
    });

    it('refreshes a running operation and only settles an expired lease', async () => {
      const model = new AgentOperationModel(serverDB, userId);
      const operationId = 'op-lease';
      await model.recordStart({ operationId });
      await serverDB
        .update(agentOperations)
        .set({ updatedAt: new Date('2026-01-01T00:00:00.000Z') })
        .where(eq(agentOperations.id, operationId));

      await model.touchRunning(operationId);
      const refreshed = await model.findById(operationId);
      expect(refreshed!.updatedAt.getTime()).toBeGreaterThan(
        new Date('2026-01-01T00:00:00.000Z').getTime(),
      );

      expect(await model.settleStaleRunning(operationId, new Date(Date.now() - 60_000))).toBe(
        false,
      );
      expect((await model.findById(operationId))?.status).toBe('running');

      await serverDB
        .update(agentOperations)
        .set({ updatedAt: new Date('2026-01-01T00:00:00.000Z') })
        .where(eq(agentOperations.id, operationId));
      expect(await model.settleStaleRunning(operationId, new Date(Date.now() - 60_000))).toBe(true);
      expect(await model.findById(operationId)).toMatchObject({
        completionReason: 'lease_expired',
        status: 'abandoned',
      });
    });

    it('persists the latest cost while reclaiming an expired lease', async () => {
      const model = new AgentOperationModel(serverDB, userId);
      const operationId = 'op-lease-cost';
      await model.recordStart({ operationId });
      await serverDB
        .update(agentOperations)
        .set({ updatedAt: new Date('2026-01-01T00:00:00.000Z') })
        .where(eq(agentOperations.id, operationId));

      expect(await model.settleStaleRunning(operationId, new Date(Date.now() - 60_000), 0.75)).toBe(
        true,
      );
      expect(await model.findById(operationId)).toMatchObject({
        status: 'abandoned',
        totalCost: 0.75,
      });
    });

    it('does not let a late completion overwrite a reclaimed operation', async () => {
      const model = new AgentOperationModel(serverDB, userId);
      const operationId = 'op-reclaimed-completion-race';
      await model.recordStart({ operationId });
      await serverDB
        .update(agentOperations)
        .set({ updatedAt: new Date('2026-01-01T00:00:00.000Z') })
        .where(eq(agentOperations.id, operationId));

      expect(await model.settleStaleRunning(operationId, new Date(Date.now() - 60_000))).toBe(true);
      expect(
        await model.recordCompletion(operationId, {
          completionReason: 'done',
          status: 'done',
        }),
      ).toBeUndefined();
      expect(await model.findById(operationId)).toMatchObject({
        completionReason: 'lease_expired',
        status: 'abandoned',
      });
    });

    it('lets the retiring caller persist terminal stats onto the abandoned row', async () => {
      // The stale-operation reaper claims with settleStaleRunning, then runs the
      // completion lifecycle, whose write must land (so hooks fire) without
      // moving the row out of `abandoned`.
      const model = new AgentOperationModel(serverDB, userId);
      const operationId = 'op-stale-retired-stats';
      await model.recordStart({ operationId });
      await serverDB
        .update(agentOperations)
        .set({ updatedAt: new Date('2026-01-01T00:00:00.000Z') })
        .where(eq(agentOperations.id, operationId));

      expect(await model.settleStaleRunning(operationId, new Date(Date.now() - 60_000))).toBe(true);
      expect(
        await model.recordCompletion(operationId, {
          completionReason: 'error',
          status: 'error',
        }),
      ).toBeUndefined();
      expect(
        await model.recordCompletion(operationId, {
          completionReason: 'lease_expired',
          status: 'abandoned',
          stepCount: 4,
        }),
      ).toBeDefined();
      expect(await model.findById(operationId)).toMatchObject({
        completionReason: 'lease_expired',
        status: 'abandoned',
        stepCount: 4,
      });
    });
  });

  describe('hetero ingest rejection marker', () => {
    const marker = {
      at: '2026-09-18T04:44:38.923Z',
      droppedEvents: 12,
      reason: 'stale-operation',
    } as const;

    it('keeps the first refusal, survives a terminal row, and stays owner-scoped', async () => {
      const model = new AgentOperationModel(serverDB, userId);
      const operationId = 'op-ingest-refused';
      await model.recordStart({ operationId });

      expect(await model.recordHeteroIngestRejection(operationId, marker)).toBe(true);
      // Later batches of the same run are refused too — the first refusal is the
      // one that explains where the output started disappearing.
      expect(
        await model.recordHeteroIngestRejection(operationId, {
          ...marker,
          at: '2026-09-18T04:46:00.000Z',
          droppedEvents: 3,
        }),
      ).toBe(false);
      expect((await model.findById(operationId))?.metadata).toMatchObject({
        heteroIngestRejection: marker,
      });

      // "The row is already terminal" is itself a refusal reason, so unlike the
      // lease writes this one must not be gated on status = running.
      const terminalId = 'op-ingest-refused-terminal';
      await model.recordStart({ operationId: terminalId });
      await model.settleRunning(terminalId, 'done');
      expect(await model.recordHeteroIngestRejection(terminalId, marker)).toBe(true);

      expect(
        await new AgentOperationModel(serverDB, otherUserId).recordHeteroIngestRejection(
          'op-ingest-refused-foreign',
          marker,
        ),
      ).toBe(false);
    });

    it('merges into existing metadata instead of replacing it', async () => {
      const model = new AgentOperationModel(serverDB, userId);
      const operationId = 'op-ingest-refused-merge';
      await model.recordStart({ operationId, metadata: { assistantMessageId: 'asst-1' } });

      await model.recordHeteroIngestRejection(operationId, marker);

      expect((await model.findById(operationId))?.metadata).toMatchObject({
        assistantMessageId: 'asst-1',
        heteroIngestRejection: marker,
      });
    });
  });

  describe('settleLive', () => {
    it('retires running and parked rows but never rewrites a terminal one', async () => {
      const model = new AgentOperationModel(serverDB, userId);
      await model.recordStart({ operationId: 'op-live-running' });
      await model.recordStart({ operationId: 'op-live-human' });
      await model.recordCompletion('op-live-human', {
        completionReason: 'waiting_for_human',
        status: 'waiting_for_human',
      });
      await model.recordStart({ operationId: 'op-live-async' });
      await model.recordCompletion('op-live-async', {
        completionReason: 'waiting_for_async_tool',
        status: 'waiting_for_async_tool',
      });
      await model.recordStart({ operationId: 'op-live-done' });
      await model.settleRunning('op-live-done', 'done');

      expect(await model.settleLive('op-live-running', 'error')).toBe(true);
      expect(await model.settleLive('op-live-human', 'error')).toBe(true);
      expect(await model.settleLive('op-live-async', 'error')).toBe(true);
      expect(await model.settleLive('op-live-done', 'error')).toBe(false);

      for (const id of ['op-live-running', 'op-live-human', 'op-live-async']) {
        expect(await model.findById(id)).toMatchObject({
          completionReason: 'error',
          status: 'error',
        });
      }
      expect((await model.findById('op-live-done'))?.status).toBe('done');
    });

    it('leaves parked rows alone through settleRunning', async () => {
      const model = new AgentOperationModel(serverDB, userId);
      await model.recordStart({ operationId: 'op-parked-kept' });
      await model.recordCompletion('op-parked-kept', {
        completionReason: 'waiting_for_human',
        status: 'waiting_for_human',
      });

      expect(await model.settleRunning('op-parked-kept', 'error')).toBe(false);
      expect((await model.findById('op-parked-kept'))?.status).toBe('waiting_for_human');
    });

    it("does not settle another user's row", async () => {
      await new AgentOperationModel(serverDB, userId).recordStart({
        operationId: 'op-live-foreign',
      });

      expect(
        await new AgentOperationModel(serverDB, otherUserId).settleLive('op-live-foreign', 'error'),
      ).toBe(false);
    });
  });

  describe('claimForAbandonment', () => {
    const park = async (
      model: AgentOperationModel,
      operationId: string,
      status: 'waiting_for_async_tool' | 'waiting_for_client' | 'waiting_for_human',
    ) => {
      await model.recordStart({ operationId });
      await model.recordCompletion(operationId, { completionReason: status, status });
    };

    it('claims every live status, including a client wait', async () => {
      const model = new AgentOperationModel(serverDB, userId);
      await model.recordStart({ operationId: 'op-claim-running' });
      await park(model, 'op-claim-human', 'waiting_for_human');
      await park(model, 'op-claim-async', 'waiting_for_async_tool');
      await park(model, 'op-claim-client', 'waiting_for_client');

      for (const id of [
        'op-claim-running',
        'op-claim-human',
        'op-claim-async',
        'op-claim-client',
      ]) {
        expect(await model.claimForAbandonment(id, 'inactivity_watchdog')).toBe(true);
        expect(await model.findById(id)).toMatchObject({
          completionReason: 'error',
          status: 'error',
        });
      }
    });

    it('stamps the claim provenance so a retry can tell it apart', async () => {
      const model = new AgentOperationModel(serverDB, userId);
      await park(model, 'op-claim-mark', 'waiting_for_client');

      await model.claimForAbandonment('op-claim-mark', 'inactivity_watchdog');

      expect((await model.findById('op-claim-mark'))?.metadata).toMatchObject({
        abandonment: { phase: 'claimed', reason: 'inactivity_watchdog' },
      });
    });

    it('never re-claims a row that already settled on its own', async () => {
      const model = new AgentOperationModel(serverDB, userId);
      await model.recordStart({ operationId: 'op-claim-done' });
      await model.settleRunning('op-claim-done', 'done');

      expect(await model.claimForAbandonment('op-claim-done', 'inactivity_watchdog')).toBe(false);
      expect(await model.findById('op-claim-done')).toMatchObject({ status: 'done' });
    });

    it("does not claim another user's parked client wait", async () => {
      const owner = new AgentOperationModel(serverDB, userId);
      await park(owner, 'op-claim-foreign', 'waiting_for_client');

      expect(
        await new AgentOperationModel(serverDB, otherUserId).claimForAbandonment(
          'op-claim-foreign',
          'inactivity_watchdog',
        ),
      ).toBe(false);
      expect((await owner.findById('op-claim-foreign'))?.status).toBe('waiting_for_client');
    });
  });

  describe('client wait resume claim', () => {
    const park = async (model: AgentOperationModel, operationId: string) => {
      await model.recordStart({ operationId });
      await model.recordCompletion(operationId, {
        completionReason: 'waiting_for_client',
        status: 'waiting_for_client',
      });
    };

    it('claims and reverts only within the caller workspace', async () => {
      await serverDB
        .insert(workspaces)
        .values({ id: 'ws-wait', name: 'ws-wait', primaryOwnerId: userId, slug: 'ws-wait' })
        .onConflictDoNothing();
      const inWorkspace = new AgentOperationModel(serverDB, userId, 'ws-wait');
      await park(inWorkspace, 'op-wait-ws');

      const personal = new AgentOperationModel(serverDB, userId);
      expect(await personal.tryResumeFromClientWait('op-wait-ws')).toBe(false);
      expect((await inWorkspace.findById('op-wait-ws'))?.status).toBe('waiting_for_client');

      expect(await inWorkspace.tryResumeFromClientWait('op-wait-ws')).toBe(true);
      expect(await personal.revertClientWaitResume('op-wait-ws')).toBe(false);
      expect((await inWorkspace.findById('op-wait-ws'))?.status).toBe('running');

      expect(await inWorkspace.revertClientWaitResume('op-wait-ws')).toBe(true);
      expect((await inWorkspace.findById('op-wait-ws'))?.status).toBe('waiting_for_client');

      await serverDB.delete(workspaces).where(eq(workspaces.id, 'ws-wait'));
    });

    it('lists only waits for the asking client providers before the limit', async () => {
      const model = new AgentOperationModel(serverDB, userId);
      await model.recordStart({ operationId: 'op-wait-mine', provider: 'lmstudio' });
      await model.recordCompletion('op-wait-mine', {
        completionReason: 'waiting_for_client',
        status: 'waiting_for_client',
      });
      for (const id of ['op-wait-other-1', 'op-wait-other-2']) {
        await model.recordStart({ operationId: id, provider: 'ollama' });
        await model.recordCompletion(id, {
          completionReason: 'waiting_for_client',
          status: 'waiting_for_client',
        });
      }

      const rows = await model.listWaitingForClient({ limit: 1, providers: ['lmstudio'] });

      expect(rows.map((row) => row.id)).toEqual(['op-wait-mine']);
    });

    it('resumes a personal wait once', async () => {
      const model = new AgentOperationModel(serverDB, userId);
      await park(model, 'op-wait-personal');

      expect(await model.tryResumeFromClientWait('op-wait-personal')).toBe(true);
      expect(await model.tryResumeFromClientWait('op-wait-personal')).toBe(false);
      expect(
        await new AgentOperationModel(serverDB, otherUserId).revertClientWaitResume(
          'op-wait-personal',
        ),
      ).toBe(false);
    });
  });

  describe('claimStaleRedrive', () => {
    const makeStale = async (operationId: string) =>
      serverDB
        .update(agentOperations)
        .set({ updatedAt: new Date('2026-01-01T00:00:00.000Z') })
        .where(eq(agentOperations.id, operationId));

    const staleBefore = () => new Date(Date.now() - 60_000);

    it('hands out increasing attempts and stops at the budget', async () => {
      const model = new AgentOperationModel(serverDB, userId);
      const operationId = 'op-redrive-budget';
      await model.recordStart({ operationId });

      await makeStale(operationId);
      expect(await model.claimStaleRedrive(operationId, staleBefore(), 2)).toBe(1);
      await makeStale(operationId);
      expect(await model.claimStaleRedrive(operationId, staleBefore(), 2)).toBe(2);
      await makeStale(operationId);
      // Budget spent — the caller falls back to abandoning the operation.
      expect(await model.claimStaleRedrive(operationId, staleBefore(), 2)).toBeNull();
    });

    it('re-arms the lease so the next sweep skips a recovering operation', async () => {
      const model = new AgentOperationModel(serverDB, userId);
      const operationId = 'op-redrive-rearm';
      await model.recordStart({ operationId });
      await makeStale(operationId);

      expect(await model.claimStaleRedrive(operationId, staleBefore(), 3)).toBe(1);
      // The claim itself bumped updatedAt, so the row is no longer a candidate.
      expect(await model.claimStaleRedrive(operationId, staleBefore(), 3)).toBeNull();
    });

    it('loses to a heartbeat that landed after the candidate was selected', async () => {
      const model = new AgentOperationModel(serverDB, userId);
      const operationId = 'op-redrive-heartbeat-race';
      await model.recordStart({ operationId });
      await makeStale(operationId);

      const selectedAt = staleBefore();
      await model.touchRunning(operationId);

      expect(await model.claimStaleRedrive(operationId, selectedAt, 3)).toBeNull();
      expect((await model.findById(operationId))?.status).toBe('running');
    });

    it('never claims an operation that already reached a terminal state', async () => {
      const model = new AgentOperationModel(serverDB, userId);
      const operationId = 'op-redrive-terminal';
      await model.recordStart({ operationId });
      await model.recordCompletion(operationId, { completionReason: 'done', status: 'done' });
      await makeStale(operationId);

      expect(await model.claimStaleRedrive(operationId, staleBefore(), 3)).toBeNull();
    });

    it('preserves unrelated metadata keys', async () => {
      const model = new AgentOperationModel(serverDB, userId);
      const operationId = 'op-redrive-metadata';
      await model.recordStart({ operationId, metadata: { keepMe: 'yes' } });
      await makeStale(operationId);

      await model.claimStaleRedrive(operationId, staleBefore(), 3);

      const row = await model.findById(operationId);
      expect(row?.metadata).toMatchObject({
        keepMe: 'yes',
        staleRedrive: { attempts: 1 },
      });
    });

    it('gives an attempt back so a failed publish costs no budget', async () => {
      const model = new AgentOperationModel(serverDB, userId);
      const operationId = 'op-redrive-release';
      await model.recordStart({ operationId });

      await makeStale(operationId);
      expect(await model.claimStaleRedrive(operationId, staleBefore(), 2)).toBe(1);
      expect(await model.releaseStaleRedrive(operationId, 1)).toBe(true);

      // Budget restored: the next claim hands out attempt 1 again.
      await makeStale(operationId);
      expect(await model.claimStaleRedrive(operationId, staleBefore(), 2)).toBe(1);
    });

    it('only ever undoes its own increment', async () => {
      const model = new AgentOperationModel(serverDB, userId);
      const operationId = 'op-redrive-release-race';
      await model.recordStart({ operationId });

      await makeStale(operationId);
      await model.claimStaleRedrive(operationId, staleBefore(), 3);
      await makeStale(operationId);
      expect(await model.claimStaleRedrive(operationId, staleBefore(), 3)).toBe(2);

      // A late release for attempt 1 must not walk the counter backwards past
      // the attempt another sweep has since claimed.
      expect(await model.releaseStaleRedrive(operationId, 1)).toBe(false);
      const row = await model.findById(operationId);
      expect(row?.metadata).toMatchObject({ staleRedrive: { attempts: 2 } });
    });

    it('scopes the release to the owning user', async () => {
      const operationId = 'op-redrive-release-ownership';
      const model = new AgentOperationModel(serverDB, userId);
      await model.recordStart({ operationId });
      await makeStale(operationId);
      await model.claimStaleRedrive(operationId, staleBefore(), 3);

      const intruder = new AgentOperationModel(serverDB, otherUserId);
      expect(await intruder.releaseStaleRedrive(operationId, 1)).toBe(false);
    });

    it('is scoped to the owning user', async () => {
      const operationId = 'op-redrive-ownership';
      await new AgentOperationModel(serverDB, userId).recordStart({ operationId });
      await makeStale(operationId);

      const intruder = new AgentOperationModel(serverDB, otherUserId);
      expect(await intruder.claimStaleRedrive(operationId, staleBefore(), 3)).toBeNull();
    });
  });

  describe('sumChildUsage', () => {
    const seedChild = async (
      model: AgentOperationModel,
      id: string,
      parentOperationId: string,
      usage: { llmCalls: number; toolCalls: number; totalCost: number; totalTokens: number },
    ) => {
      await model.recordStart({ operationId: id, parentOperationId });
      await model.recordCompletion(id, {
        completionReason: 'done',
        llmCalls: usage.llmCalls,
        status: 'done',
        toolCalls: usage.toolCalls,
        totalCost: usage.totalCost,
        totalInputTokens: usage.totalTokens,
        totalOutputTokens: 0,
        totalTokens: usage.totalTokens,
      });
    };

    it('sums every child of the parent', async () => {
      const model = new AgentOperationModel(serverDB, userId);
      await model.recordStart({ operationId: 'parent' });
      await seedChild(model, 'child-a', 'parent', {
        llmCalls: 2,
        toolCalls: 3,
        totalCost: 0.25,
        totalTokens: 1000,
      });
      await seedChild(model, 'child-b', 'parent', {
        llmCalls: 1,
        toolCalls: 4,
        totalCost: 0.75,
        totalTokens: 2000,
      });

      const rollup = await model.sumChildUsage('parent');

      expect(rollup).toEqual({
        llmCalls: 3,
        toolCalls: 7,
        totalCost: 1,
        totalInputTokens: 3000,
        totalOutputTokens: 0,
        totalTokens: 3000,
      });
    });

    // The whole reason this is a read-time SUM: the sub-agent completion bridge is
    // contractually re-deliverable, so an accumulation onto the parent row would
    // double-count. Re-deriving is exact however many times it runs.
    it('is idempotent — re-deriving does not accumulate', async () => {
      const model = new AgentOperationModel(serverDB, userId);
      await model.recordStart({ operationId: 'parent' });
      await seedChild(model, 'child-a', 'parent', {
        llmCalls: 1,
        toolCalls: 1,
        totalCost: 0.5,
        totalTokens: 1234,
      });

      const first = await model.sumChildUsage('parent');
      const second = await model.sumChildUsage('parent');

      expect(second).toEqual(first);
      expect(second.totalTokens).toBe(1234);
    });

    it('returns zeroes for an operation with no children', async () => {
      const model = new AgentOperationModel(serverDB, userId);
      await model.recordStart({ operationId: 'lonely' });

      expect(await model.sumChildUsage('lonely')).toEqual({
        llmCalls: 0,
        toolCalls: 0,
        totalCost: 0,
        totalInputTokens: 0,
        totalOutputTokens: 0,
        totalTokens: 0,
      });
    });

    it("does not sum another user's children", async () => {
      const model = new AgentOperationModel(serverDB, userId);
      const attacker = new AgentOperationModel(serverDB, otherUserId);
      await model.recordStart({ operationId: 'parent' });
      await seedChild(model, 'child-a', 'parent', {
        llmCalls: 1,
        toolCalls: 1,
        totalCost: 0.5,
        totalTokens: 1000,
      });

      expect(await attacker.sumChildUsage('parent')).toEqual({
        llmCalls: 0,
        toolCalls: 0,
        totalCost: 0,
        totalInputTokens: 0,
        totalOutputTokens: 0,
        totalTokens: 0,
      });
    });
  });

  describe('getMaxDurationSeconds', () => {
    it('returns the longest wall-clock duration, ignoring in-flight and other users', async () => {
      const model = new AgentOperationModel(serverDB, userId);

      await serverDB.insert(agentOperations).values([
        // 5 minutes
        {
          completedAt: new Date('2026-05-13T10:05:00.000Z'),
          id: 'op-dur-1',
          startedAt: new Date('2026-05-13T10:00:00.000Z'),
          status: 'done',
          userId,
        },
        // 1 hour — the longest
        {
          completedAt: new Date('2026-05-13T12:00:00.000Z'),
          id: 'op-dur-2',
          startedAt: new Date('2026-05-13T11:00:00.000Z'),
          status: 'done',
          userId,
        },
        // in-flight: no completedAt -> excluded
        {
          completedAt: null,
          id: 'op-dur-running',
          startedAt: new Date('2026-05-13T09:00:00.000Z'),
          status: 'running',
          userId,
        },
        // another user's much longer op -> excluded
        {
          completedAt: new Date('2026-05-13T20:00:00.000Z'),
          id: 'op-dur-other',
          startedAt: new Date('2026-05-13T10:00:00.000Z'),
          status: 'done',
          userId: otherUserId,
        },
      ]);

      const result = await model.getMaxDurationSeconds();
      expect(result).toBe(3600);
    });

    it('returns 0 when there are no completed operations', async () => {
      const model = new AgentOperationModel(serverDB, userId);

      await serverDB.insert(agentOperations).values({
        completedAt: null,
        id: 'op-dur-none',
        startedAt: new Date('2026-05-13T09:00:00.000Z'),
        status: 'running',
        userId,
      });

      const result = await model.getMaxDurationSeconds();
      expect(result).toBe(0);
    });
  });

  describe('listOperationTree', () => {
    it('returns the root op together with its direct children, owner-scoped', async () => {
      const model = new AgentOperationModel(serverDB, userId);

      await serverDB.insert(agentOperations).values([
        { id: 'root', status: 'done', userId },
        { id: 'child-a', parentOperationId: 'root', status: 'done', userId },
        { id: 'child-b', parentOperationId: 'root', status: 'done', userId },
        // Unrelated op (different parent) must not leak in.
        { id: 'stranger', parentOperationId: 'other-root', status: 'done', userId },
        // Another user's child of the same root must not leak in.
        { id: 'foreign-child', parentOperationId: 'root', status: 'done', userId: otherUserId },
      ]);

      const tree = await model.listOperationTree('root');
      expect(tree.map((op) => op.id).sort()).toEqual(['child-a', 'child-b', 'root']);
    });

    it('returns just the root when it has no children', async () => {
      const model = new AgentOperationModel(serverDB, userId);
      await serverDB.insert(agentOperations).values({ id: 'lonely', status: 'done', userId });

      const tree = await model.listOperationTree('lonely');
      expect(tree.map((op) => op.id)).toEqual(['lonely']);
    });
  });

  describe('findOwnOperationById', () => {
    beforeEach(async () => {
      await serverDB.insert(topics).values([
        { id: 'own-tpc', userId },
        // Agent-share visitor topic: creator's userId + a visitor senderId.
        { id: 'own-tpc-visitor', senderId: 'visitor-user-x', userId },
      ]);
      await serverDB.insert(agentOperations).values([
        { id: 'op-own', status: 'done', topicId: 'own-tpc', userId },
        { id: 'op-own-visitor', status: 'done', topicId: 'own-tpc-visitor', userId },
        { id: 'op-own-topicless', status: 'done', userId },
      ]);
    });

    it('hides an operation recorded inside an agent-share visitor topic', async () => {
      // Visitor runs execute under the creator's identity, so the row passes
      // ownership — the creator-facing trace lookup must still read it as absent.
      const model = new AgentOperationModel(serverDB, userId);

      expect(await model.findOwnOperationById('op-own-visitor')).toBeNull();
      // The runtime lookup keeps working: it drives the visitor's own run.
      expect(await model.findById('op-own-visitor')).toMatchObject({ id: 'op-own-visitor' });
    });

    it('returns the creator own operations, topic-bound or not', async () => {
      const model = new AgentOperationModel(serverDB, userId);

      expect(await model.findOwnOperationById('op-own')).toMatchObject({ id: 'op-own' });
      expect(await model.findOwnOperationById('op-own-topicless')).toMatchObject({
        id: 'op-own-topicless',
      });
    });

    it('does not cross the ownership boundary', async () => {
      const model = new AgentOperationModel(serverDB, otherUserId);

      expect(await model.findOwnOperationById('op-own')).toBeNull();
    });
  });

  describe('listByTopic', () => {
    beforeEach(async () => {
      await serverDB.insert(topics).values([
        { id: 'tpc-a', userId },
        { id: 'tpc-b', userId },
        { id: 'tpc-foreign', userId: otherUserId },
        // Agent-share visitor topic: creator's userId + a visitor senderId.
        { id: 'tpc-visitor', senderId: 'visitor-user-x', userId },
      ]);
    });

    it('excludes operations recorded inside an agent-share visitor topic', async () => {
      const model = new AgentOperationModel(serverDB, userId);

      // Visitor runs execute under the creator's identity, so their operation
      // rows pass the ownership filter — the trace panel must still not expose
      // a visitor conversation's trajectory.
      await serverDB
        .insert(agentOperations)
        .values([{ id: 'op-visitor', status: 'done', topicId: 'tpc-visitor', userId }]);

      const rows = await model.listByTopic('tpc-visitor');
      expect(rows).toEqual([]);
    });

    it('returns the topic operations newest first, owner-scoped', async () => {
      const model = new AgentOperationModel(serverDB, userId);

      await serverDB.insert(agentOperations).values([
        {
          createdAt: new Date('2026-01-01'),
          id: 'op-old',
          status: 'done',
          topicId: 'tpc-a',
          userId,
        },
        {
          createdAt: new Date('2026-01-03'),
          id: 'op-new',
          status: 'done',
          topicId: 'tpc-a',
          userId,
        },
        // Another topic's op must not leak in.
        { id: 'op-other-topic', status: 'done', topicId: 'tpc-b', userId },
        // Another user's op on the same topic must not leak in.
        { id: 'op-foreign', status: 'done', topicId: 'tpc-a', userId: otherUserId },
      ]);

      const rows = await model.listByTopic('tpc-a');
      expect(rows.map((row) => row.id)).toEqual(['op-new', 'op-old']);
    });

    it('reports whether a trace was recorded so callers can tell it apart from a failed fetch', async () => {
      const model = new AgentOperationModel(serverDB, userId);

      await serverDB.insert(agentOperations).values([
        {
          id: 'op-with-trace',
          status: 'done',
          topicId: 'tpc-a',
          traceS3Key: 'agent-traces/agt_x/tpc-a/op-with-trace.json.zst',
          userId,
        },
        { id: 'op-without-trace', status: 'done', topicId: 'tpc-a', userId },
      ]);

      const byId = Object.fromEntries(
        (await model.listByTopic('tpc-a')).map((row) => [row.id, row.traceS3Key]),
      );
      expect(byId['op-with-trace']).toBe('agent-traces/agt_x/tpc-a/op-with-trace.json.zst');
      expect(byId['op-without-trace']).toBeNull();
    });

    it('honours the limit', async () => {
      const model = new AgentOperationModel(serverDB, userId);

      await serverDB.insert(agentOperations).values(
        Array.from({ length: 5 }, (_, index) => ({
          createdAt: new Date(2026, 0, index + 1),
          id: `op-${index}`,
          status: 'done' as const,
          topicId: 'tpc-a',
          userId,
        })),
      );

      expect((await model.listByTopic('tpc-a', 2)).map((row) => row.id)).toEqual(['op-4', 'op-3']);
    });
  });
});