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.

17.6 KB
Raw Blame
// @vitest-environment node
import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest';

import { type IStreamEventManager } from '@/server/modules/AgentRuntime/types';
import { CompletionLifecycle } from '@/server/services/agentRuntime/CompletionLifecycle';

import { HeterogeneousAgentService, HeterogeneousPersistenceHandler } from '..';
import { __resetOperationStatesForTesting } from '../HeterogeneousPersistenceHandler';

const createSilentStreamManager = (): IStreamEventManager =>
  ({
    publishStreamEvent: vi.fn(async () => 'ok'),
  }) as unknown as IStreamEventManager;

const createPersistenceHandler = (
  findById: ReturnType<typeof vi.fn>,
  updateMetadata: ReturnType<typeof vi.fn>,
) =>
  new HeterogeneousPersistenceHandler({
    messageModel: {
      findById: vi.fn(async () => null),
      getLatestSpineMessageId: vi.fn(async () => null),
      listToolCallIndexByTopic: vi.fn(async () => []),
      update: vi.fn(async () => ({ success: true })),
    } as any,
    threadModel: {} as any,
    topicModel: { findById, updateMetadata } as any,
  });

const createFinishTopicModel = (
  findById: ReturnType<typeof vi.fn>,
  updateMetadata: ReturnType<typeof vi.fn>,
) => ({
  findById,
  settleRunningStatus: vi.fn(async () => undefined),
  takeRunningOperation: vi.fn(async () => ({ isRoot: true, operation: {} })),
  updateMetadata,
});

describe('HeterogeneousAgentService — phase 2c session id persistence + resume', () => {
  beforeEach(() => {
    __resetOperationStatesForTesting();
    vi.spyOn(CompletionLifecycle.prototype, 'completeOperation').mockResolvedValue(undefined);
  });
  afterEach(() => {
    __resetOperationStatesForTesting();
    vi.restoreAllMocks();
  });

  describe('heteroFinish persists sessionId via TopicModel.updateMetadata', () => {
    it('writes the CLI session id to topic.metadata.heteroSessionId', async () => {
      const updateMetadata = vi.fn(async () => undefined);
      const findById = vi.fn(async () => ({
        agentId: null,
        id: 'topic-1',
        metadata: {
          runningOperation: {
            assistantMessageId: 'asst-1',
            operationId: 'op-1',
          },
        },
      }));

      // Real handler so we exercise the persistSessionId path end-to-end
      const handler = createPersistenceHandler(findById, updateMetadata);

      // Seed in-memory state by ingesting one event so finish has something to drain
      await handler.ingest({
        events: [
          {
            data: { chunkType: 'text', content: 'hi' },
            operationId: 'op-1',
            stepIndex: 0,
            timestamp: 1,
            type: 'stream_chunk',
          },
        ],
        operationId: 'op-1',
        topicId: 'topic-1',
      });

      const service = new HeterogeneousAgentService({} as any, 'user-1', {
        persistenceHandler: handler,
        streamEventManager: createSilentStreamManager(),
        topicModel: createFinishTopicModel(findById, updateMetadata) as any,
      });

      await service.heteroFinish({
        agentType: 'claude-code',
        operationId: 'op-1',
        result: 'success',
        sessionId: 'cc-session-fresh',
        topicId: 'topic-1',
      });

      expect(updateMetadata).toHaveBeenCalledWith('topic-1', {
        heteroSessionId: 'cc-session-fresh',
      });
    });

    it('skips the metadata write when no sessionId is provided on success', async () => {
      const updateMetadata = vi.fn(async () => undefined);
      const findById = vi.fn(async () => ({
        agentId: null,
        id: 'topic-2',
        metadata: {
          runningOperation: {
            assistantMessageId: 'asst-2',
            operationId: 'op-2',
          },
        },
      }));

      const handler = createPersistenceHandler(findById, updateMetadata);
      await handler.ingest({
        events: [
          {
            data: {},
            operationId: 'op-2',
            stepIndex: 0,
            timestamp: 1,
            type: 'agent_runtime_end',
          },
        ],
        operationId: 'op-2',
        topicId: 'topic-2',
      });

      const service = new HeterogeneousAgentService({} as any, 'user-1', {
        persistenceHandler: handler,
        streamEventManager: createSilentStreamManager(),
        topicModel: createFinishTopicModel(findById, updateMetadata) as any,
      });

      await service.heteroFinish({
        agentType: 'claude-code',
        operationId: 'op-2',
        result: 'success',
        // no sessionId — CLI run aborted before init landed, or codex without resume
        topicId: 'topic-2',
      });

      expect(updateMetadata).not.toHaveBeenCalled();
    });

    it('clears stale heteroSessionId when result=error and no sessionId (sandbox recycled)', async () => {
      const updateMetadata = vi.fn(async () => undefined);
      const findById = vi.fn(async () => ({
        agentId: null,
        id: 'topic-stale',
        metadata: {
          heteroSessionId: 'cc-dead-session',
          runningOperation: { assistantMessageId: 'asst-s', operationId: 'op-stale' },
        },
      }));

      const handler = createPersistenceHandler(findById, updateMetadata);

      await handler.ingest({
        events: [
          {
            data: { chunkType: 'text', content: '' },
            operationId: 'op-stale',
            stepIndex: 0,
            timestamp: 1,
            type: 'stream_chunk',
          },
        ],
        operationId: 'op-stale',
        topicId: 'topic-stale',
      });

      const service = new HeterogeneousAgentService({} as any, 'user-1', {
        persistenceHandler: handler,
        streamEventManager: createSilentStreamManager(),
        topicModel: createFinishTopicModel(findById, updateMetadata) as any,
      });

      // Simulate: sandbox was recycled, CC exited before emitting system.init
      // so `sessionId` is undefined.
      await service.heteroFinish({
        agentType: 'claude-code',
        operationId: 'op-stale',
        result: 'error',
        resumeSessionInvalidated: true,
        // no sessionId — CC never initialized (resume failed)
        topicId: 'topic-stale',
      });

      // Must clear the stale session id so the next turn starts fresh
      expect(updateMetadata).toHaveBeenCalledWith('topic-stale', { heteroSessionId: undefined });
    });

    /**
     * @example Codex `already has an active writer` preserves the requested session token.
     */
    it('preserves heteroSessionId for a transient pre-init resume failure', async () => {
      // ROOT CAUSE:
      //
      // If a concurrent Codex writer rejected thread/resume, no new session id
      // was emitted. The old implementation treated every such error as a dead
      // session and cleared the valid token, forcing the next turn to start empty.
      //
      // Before: error + no sessionId always wrote heteroSessionId=undefined.
      // After: only an explicit resumeSessionInvalidated signal may clear it.
      const updateMetadata = vi.fn(async () => undefined);
      const findById = vi.fn(async () => ({
        id: 'topic-busy',
        metadata: {
          heteroSessionId: 'codex-thread-valid',
          runningOperation: { assistantMessageId: 'asst-busy', operationId: 'op-busy' },
        },
      }));
      const handler = createPersistenceHandler(findById, updateMetadata);
      await handler.ingest({
        events: [
          {
            data: { chunkType: 'text', content: '' },
            operationId: 'op-busy',
            stepIndex: 0,
            timestamp: 1,
            type: 'stream_chunk',
          },
        ],
        operationId: 'op-busy',
        topicId: 'topic-busy',
      });
      const service = new HeterogeneousAgentService({} as any, 'user-1', {
        persistenceHandler: handler,
        streamEventManager: createSilentStreamManager(),
        topicModel: {
          findById,
          settleRunningOperation: vi.fn(async () => ({ status: 'settled' as const })),
          updateMetadata,
        } as any,
      });

      await service.heteroFinish({
        agentType: 'codex',
        error: { message: 'already has an active writer', type: 'AgentRuntimeError' },
        operationId: 'op-busy',
        result: 'error',
        topicId: 'topic-busy',
      });

      expect(updateMetadata).not.toHaveBeenCalledWith('topic-busy', {
        heteroSessionId: undefined,
      });
    });

    it('persists sessionId even when result=error (so the next run can still resume context)', async () => {
      const updateMetadata = vi.fn(async () => undefined);
      const findById = vi.fn(async () => ({
        agentId: null,
        id: 'topic-3',
        metadata: {
          runningOperation: {
            assistantMessageId: 'asst-3',
            operationId: 'op-3',
          },
        },
      }));

      const handler = createPersistenceHandler(findById, updateMetadata);
      await handler.ingest({
        events: [
          {
            data: {},
            operationId: 'op-3',
            stepIndex: 0,
            timestamp: 1,
            type: 'agent_runtime_init',
          },
        ],
        operationId: 'op-3',
        topicId: 'topic-3',
      });

      const service = new HeterogeneousAgentService({} as any, 'user-1', {
        persistenceHandler: handler,
        streamEventManager: createSilentStreamManager(),
        topicModel: createFinishTopicModel(findById, updateMetadata) as any,
      });

      await service.heteroFinish({
        agentType: 'claude-code',
        error: { message: 'rate limited', type: 'AgentRuntimeError' },
        operationId: 'op-3',
        result: 'error',
        sessionId: 'cc-session-partial',
        topicId: 'topic-3',
      });

      expect(updateMetadata).toHaveBeenCalledWith('topic-3', {
        heteroSessionId: 'cc-session-partial',
      });
    });

    it('topic.metadata.runningOperation is preserved (updateMetadata merges, does not replace)', async () => {
      // This contract is enforced by `TopicModel.updateMetadata` itself
      // (verified in packages/database tests). We just assert the service
      // calls it with a partial — not the full metadata object.
      const updateMetadata = vi.fn(async () => undefined);
      const findById = vi.fn(async () => ({
        agentId: null,
        id: 'topic-4',
        metadata: {
          runningOperation: {
            assistantMessageId: 'asst-4',
            operationId: 'op-4',
          },
          workingDirectory: '/Users/dev/project',
        },
      }));

      const handler = createPersistenceHandler(findById, updateMetadata);
      await handler.ingest({
        events: [
          {
            data: {},
            operationId: 'op-4',
            stepIndex: 0,
            timestamp: 1,
            type: 'agent_runtime_init',
          },
        ],
        operationId: 'op-4',
        topicId: 'topic-4',
      });

      const service = new HeterogeneousAgentService({} as any, 'user-1', {
        persistenceHandler: handler,
        streamEventManager: createSilentStreamManager(),
        topicModel: createFinishTopicModel(findById, updateMetadata) as any,
      });

      await service.heteroFinish({
        agentType: 'claude-code',
        operationId: 'op-4',
        result: 'success',
        sessionId: 'cc-session-resume-target',
        topicId: 'topic-4',
      });

      // Critical: passes only the field we want to update; other peers
      // (runningOperation, workingDirectory) are untouched in the patch
      // and TopicModel.updateMetadata's merge semantics preserve them.
      expect(updateMetadata).toHaveBeenCalledTimes(1);
      const call = updateMetadata.mock.calls[0] as unknown as [string, Record<string, unknown>];
      const patch = call[1];
      expect(patch).toEqual({ heteroSessionId: 'cc-session-resume-target' });
      expect(patch).not.toHaveProperty('runningOperation');
      expect(patch).not.toHaveProperty('workingDirectory');
    });

    it('updateMetadata failure does not poison heteroFinish (terminal event still publishes)', async () => {
      const updateMetadata = vi.fn(async () => {
        throw new Error('connection lost');
      });
      const findById = vi.fn(async () => ({
        agentId: null,
        id: 'topic-5',
        metadata: {
          runningOperation: {
            assistantMessageId: 'asst-5',
            operationId: 'op-5',
          },
        },
      }));

      const handler = createPersistenceHandler(findById, updateMetadata);
      await handler.ingest({
        events: [
          {
            data: {},
            operationId: 'op-5',
            stepIndex: 0,
            timestamp: 1,
            type: 'agent_runtime_init',
          },
        ],
        operationId: 'op-5',
        topicId: 'topic-5',
      });

      const stream = createSilentStreamManager();
      const service = new HeterogeneousAgentService({} as any, 'user-1', {
        persistenceHandler: handler,
        streamEventManager: stream,
        topicModel: createFinishTopicModel(findById, updateMetadata) as any,
      });

      // Should not throw — sessionId persistence is best-effort
      await expect(
        service.heteroFinish({
          agentType: 'claude-code',
          operationId: 'op-5',
          result: 'success',
          sessionId: 'cc-session-x',
          topicId: 'topic-5',
        }),
      ).resolves.not.toThrow();

      // Terminal agent_runtime_end still published
      expect(stream.publishStreamEvent).toHaveBeenCalled();
    });
  });

  describe('eager session-id persistence on stream_start (survives watchdog abandon)', () => {
    it('persists heteroSessionId as soon as stream_start reports it, without waiting for heteroFinish', async () => {
      const updateMetadata = vi.fn(async () => undefined);
      const findById = vi.fn(async () => ({
        agentId: null,
        id: 'topic-abandon',
        metadata: {
          runningOperation: { assistantMessageId: 'asst-a', operationId: 'op-abandon' },
        },
      }));

      const handler = createPersistenceHandler(findById, updateMetadata);

      // Only a stream_start reporting the CC session id — NO heteroFinish. This
      // is the inactivity-watchdog path: the run starts, emits its session id,
      // then gets abandoned by AbandonOperationService (which never calls finish).
      await handler.ingest({
        events: [
          {
            data: { sessionId: 'cc-live-session' },
            operationId: 'op-abandon',
            stepIndex: 0,
            timestamp: 1,
            type: 'stream_start',
          },
        ],
        operationId: 'op-abandon',
        topicId: 'topic-abandon',
      });

      // The resume token is already on topic.metadata — the next turn can resume.
      expect(updateMetadata).toHaveBeenCalledWith('topic-abandon', {
        heteroSessionId: 'cc-live-session',
      });
    });

    it('does not re-write when stream_start repeats the same session id', async () => {
      const updateMetadata = vi.fn(async () => undefined);
      const findById = vi.fn(async () => ({
        agentId: null,
        id: 'topic-dedupe',
        metadata: { runningOperation: { assistantMessageId: 'asst-d', operationId: 'op-dedupe' } },
      }));

      const handler = createPersistenceHandler(findById, updateMetadata);

      await handler.ingest({
        events: [
          {
            data: { sessionId: 'cc-same' },
            operationId: 'op-dedupe',
            stepIndex: 0,
            timestamp: 1,
            type: 'stream_start',
          },
          {
            data: { sessionId: 'cc-same' },
            operationId: 'op-dedupe',
            stepIndex: 1,
            timestamp: 2,
            type: 'stream_start',
          },
        ],
        operationId: 'op-dedupe',
        topicId: 'topic-dedupe',
      });

      expect(updateMetadata).toHaveBeenCalledTimes(1);
    });
  });

  describe('getHeterogeneousResumeSessionId', () => {
    const buildService = (findByIdImpl: (id: string) => Promise<any>) => {
      const findById = vi.fn(findByIdImpl);
      const service = new HeterogeneousAgentService({} as any, 'user-1', {
        persistenceHandler: {
          finish: vi.fn(async () => {}),
          ingest: vi.fn(async () => {}),
        } as unknown as HeterogeneousPersistenceHandler,
        streamEventManager: createSilentStreamManager(),
        topicModel: { findById } as any,
      });
      return { findById, service };
    };

    it('returns the persisted heteroSessionId for the topic', async () => {
      const { findById, service } = buildService(async (id) => ({
        agentId: null,
        id,
        metadata: {
          heteroSessionId: 'cc-session-aaaa',
          runningOperation: { assistantMessageId: 'asst', operationId: 'op' },
        },
      }));

      const sessionId = await service.getHeterogeneousResumeSessionId('topic-resume');
      expect(sessionId).toBe('cc-session-aaaa');
      expect(findById).toHaveBeenCalledWith('topic-resume');
    });

    it('returns undefined when no prior run persisted a session id', async () => {
      const { service } = buildService(async (id) => ({
        agentId: null,
        id,
        metadata: {
          runningOperation: { assistantMessageId: 'asst', operationId: 'op' },
          // no heteroSessionId
        },
      }));
      expect(await service.getHeterogeneousResumeSessionId('topic-no-session')).toBeUndefined();
    });

    it('returns undefined for an unknown topic', async () => {
      const { service } = buildService(async () => null);
      expect(await service.getHeterogeneousResumeSessionId('topic-missing')).toBeUndefined();
    });

    it('returns undefined when topic has no metadata', async () => {
      const { service } = buildService(async (id) => ({ agentId: null, id, metadata: null }));
      expect(await service.getHeterogeneousResumeSessionId('topic-bare')).toBeUndefined();
    });
  });
});