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.

62.1 KB
Raw Blame
// @vitest-environment node
import type * as BusinessConst from '@lobechat/business-const';
import { TRPCError } from '@trpc/server';
import { beforeEach, describe, expect, it, vi } from 'vitest';

import type * as MessageModelModule from '@/database/models/message';
import { createContextInner } from '@/libs/trpc/lambda/context';

const mockServerDB = {
  transaction: vi.fn(async (fn: (trx: unknown) => Promise<unknown>) => fn({ trx: true })),
};
vi.mock('@/database/core/db-adaptor', () => ({
  getServerDB: vi.fn(function () {
    return mockServerDB;
  }),
}));

// Pin the cloud-only capability open so the visitor procedures under test are
// reachable: ENABLE_BUSINESS_FEATURES is false in OSS builds, and the
// shareChatProcedure middleware (`_helpers/agentShareFeatureGate.ts`) would
// otherwise reject everything with FORBIDDEN before reaching any of the
// behavior these tests exercise. The "gate itself" is covered separately by
// `_helpers/__tests__/agentShareFeatureGate.test.ts` and the dedicated
// "visitor capability" describe block below.
const mocks = vi.hoisted(() => ({
  businessConst: { ENABLE_BUSINESS_FEATURES: true },
}));
vi.mock('@lobechat/business-const', async () => {
  const actual = await vi.importActual<typeof BusinessConst>('@lobechat/business-const');
  return {
    ...actual,
    // `packages/utils/src/apiKey.ts` reads this dynamically (`import * as
    // businessConst`), pulled in transitively via the unmocked
    // `createContextInner` -> `ApiKeyModel` chain below. `actual` here
    // resolves to the cloud override, which omits this key entirely (see
    // that file's own doc comment), so vitest's mock-export validation has
    // no own property to find unless it is listed explicitly.
    API_KEY_PREFIX: (actual as Record<string, unknown>).API_KEY_PREFIX,
    // A getter (not a static spread) so per-test mutation of
    // `mocks.businessConst.ENABLE_BUSINESS_FEATURES` is observed by every
    // subsequent read, including inside the already-imported gate helper.
    get ENABLE_BUSINESS_FEATURES() {
      return mocks.businessConst.ENABLE_BUSINESS_FEATURES;
    },
  };
});

const mockGetFeatureFlagsState = vi.fn();
vi.mock('@/server/featureFlags', () => ({
  getServerFeatureFlagsStateFromRuntimeConfig: (...args: unknown[]) =>
    mockGetFeatureFlagsState(...args),
}));

const mockAccessCheck = vi.fn();
const mockIsRunStillAuthorized = vi.fn();
const mockLockScopedAgentRow = vi.fn();
const mockLockUploadAdmission = vi.fn();
vi.mock('@/database/models/agentShare', () => ({
  AgentShareModel: {
    findByShareIdWithAccessCheck: (...args: any[]) => mockAccessCheck(...args),
    isRunStillAuthorized: (...args: any[]) => mockIsRunStillAuthorized(...args),
    lockScopedAgentRow: (...args: any[]) => mockLockScopedAgentRow(...args),
    lockUploadAdmission: (...args: any[]) => mockLockUploadAdmission(...args),
  },
}));

const mockFindById = vi.fn();
const mockCountMessagesBySender = vi.fn();
const mockQueryBySender = vi.fn();
const mockIsRunningOperationAlive = vi.fn();
const TopicModelMock = vi.fn(function () {
  return {
    countMessagesBySender: mockCountMessagesBySender,
    findById: mockFindById,
    isRunningOperationAlive: mockIsRunningOperationAlive,
    queryBySender: mockQueryBySender,
  };
});
vi.mock('@/database/models/topic', () => ({
  TopicModel: TopicModelMock,
}));

const mockFindMessagePlugin = vi.fn();
const mockMessageQuery = vi.fn();
const mockMessageQueryForVisitor = vi.fn();
vi.mock('@/database/models/message', async (importOriginal) => {
  // Keep the real `sanitizeVisitorError` (rather than re-stubbing it) so the
  // startup-error regression below exercises the SAME projection shareChat
  // reuses in production — not a test-only stand-in that could silently
  // drift from it.
  const actual = await importOriginal<typeof MessageModelModule>();
  return {
    ...actual,
    MessageModel: vi.fn(function () {
      return {
        findMessagePlugin: mockFindMessagePlugin,
        query: mockMessageQuery,
        queryForVisitor: mockMessageQueryForVisitor,
      };
    }),
  };
});

vi.mock('@/database/models/user', () => ({
  UserModel: vi.fn(function () {
    return { getUserSettings: vi.fn().mockResolvedValue({}) };
  }),
}));

const mockDocumentFindById = vi.fn();
const DocumentModelMock = vi.fn(function () {
  return { findById: mockDocumentFindById };
});
vi.mock('@/database/models/document', () => ({
  DocumentModel: DocumentModelMock,
}));

const mockFileFindByIds = vi.fn();
const mockFileFindById = vi.fn();
const mockFileCreate = vi.fn();
const mockFileDeleteUnreferenced = vi.fn();
const mockFileCountAgentShareUsage = vi.fn();
const FileModelMock = vi.fn(function () {
  return {
    countAgentShareUsage: mockFileCountAgentShareUsage,
    create: mockFileCreate,
    deleteUnreferenced: mockFileDeleteUnreferenced,
    findById: mockFileFindById,
    findByIds: mockFileFindByIds,
  };
});
vi.mock('@/database/models/file', () => ({
  FileModel: FileModelMock,
}));

const mockReserveUpload = vi.fn();
vi.mock('@/server/services/fileUploadReservation', () => ({
  reserveUpload: (...args: any[]) => mockReserveUpload(...args),
}));

const mockUploadCountLiveUnderPrefix = vi.fn();
const FileUploadModelMock = vi.fn(function () {
  return { countLiveUsageUnderPrefix: mockUploadCountLiveUnderPrefix };
});
vi.mock('@/database/models/fileUpload', () => ({
  FileUploadModel: FileUploadModelMock,
}));

const mockUploadTouchActive = vi.fn();
const mockUploadFindLatest = vi.fn();
const mockUploadRelease = vi.fn();
const mockUploadReleaseBestEffort = vi.fn();
const mockUploadFindLatestForUpdate = vi.fn();
const mockUploadSettle = vi.fn();
const FileUploadServiceMock = vi.fn(function () {
  return {
    findLatest: mockUploadFindLatest,
    model: {
      findLatestByPathnameForUpdate: mockUploadFindLatestForUpdate,
      settle: mockUploadSettle,
    },
    release: mockUploadRelease,
    releaseBestEffort: mockUploadReleaseBestEffort,
    touchActive: mockUploadTouchActive,
  };
});
vi.mock('@/server/services/fileUpload', () => ({
  FileUploadService: FileUploadServiceMock,
}));

const mockCreatePreSignedUrl = vi.fn();
vi.mock('@/server/modules/S3', () => ({
  FileS3: vi.fn(function () {
    return { createPreSignedUrl: mockCreatePreSignedUrl };
  }),
}));

vi.mock('@/config/db', () => ({
  serverDBEnv: { REMOVE_GLOBAL_FILE: true },
}));

const mockExecAgent = vi.fn();
const mockInterruptTask = vi.fn();
const mockSetQueuedMessages = vi.fn();
const mockStopPendingApproval = vi.fn();
const AiAgentServiceMock = vi.fn(function () {
  return {
    execAgent: mockExecAgent,
    interruptTask: mockInterruptTask,
    setQueuedMessages: mockSetQueuedMessages,
    stopPendingApproval: mockStopPendingApproval,
  };
});
vi.mock('@/server/services/aiAgent', () => ({
  AiAgentService: AiAgentServiceMock,
}));

const mockGetFileAccessUrl = vi.fn();
const mockGetFileMetadata = vi.fn();
const mockDeleteStoredFile = vi.fn();
vi.mock('@/server/services/file', () => ({
  FileService: vi.fn(function () {
    return {
      deleteFile: mockDeleteStoredFile,
      getFileAccessUrl: mockGetFileAccessUrl,
      getFileMetadata: mockGetFileMetadata,
    };
  }),
}));

const mockSpendGate = vi.fn();
vi.mock('@/business/server/agent-share/spendGate', () => ({
  checkAgentShareSpendAllowance: (...args: any[]) => mockSpendGate(...args),
}));

const mockSignUserJWT = vi.fn();
vi.mock('@/libs/trpc/utils/internalJwt', () => ({
  signUserJWT: (...args: any[]) => mockSignUserJWT(...args),
}));

const mockResolveBySource = vi.fn();
vi.mock('@/business/server/agent-run/agentInterventionReview', async (importOriginal) => ({
  ...(await importOriginal<Record<string, unknown>>()),
  resolveAgentInterventionBySource: (...args: any[]) => mockResolveBySource(...args),
}));

const mockDispatchClaimed = vi.fn();
const mockBridgeLegacyResume = vi.fn();
vi.mock('../aiAgent', async (importOriginal) => ({
  ...(await importOriginal<Record<string, unknown>>()),
  bridgeLegacyResumeToSourceIntervention: (...args: any[]) => mockBridgeLegacyResume(...args),
  dispatchClaimedAgentIntervention: (...args: any[]) => mockDispatchClaimed(...args),
}));

const { shareChatRouter } = await import('../shareChat');

const VISITOR = 'visitor-1';
const OWNER = 'owner-1';

const share = {
  agentId: 'agt_share',
  ownerId: OWNER,
  shareConfig: {
    allowReadMemory: false,
    toolGrants: [],
    maxMessagesPerVisitor: 3,
  },
  shareId: 'share-1',
  visibility: 'link',
  workspaceId: null,
};

const visitorTopic = {
  agentId: share.agentId,
  id: 'tpc_visitor',
  metadata: { runningOperation: { operationId: 'op-1' } },
  senderId: VISITOR,
};

const createCaller = async () =>
  shareChatRouter.createCaller(await createContextInner({ userId: VISITOR }));

describe('shareChatRouter', () => {
  beforeEach(() => {
    vi.clearAllMocks();
    mocks.businessConst.ENABLE_BUSINESS_FEATURES = true;
    mockGetFeatureFlagsState.mockResolvedValue({ enableAgentShare: true });
    mockAccessCheck.mockResolvedValue(share);
    mockIsRunStillAuthorized.mockResolvedValue(true);
    mockLockScopedAgentRow.mockResolvedValue({ id: share.agentId, workspaceId: null });
    mockFindById.mockResolvedValue(visitorTopic);
    mockIsRunningOperationAlive.mockResolvedValue(true);
    mockCountMessagesBySender.mockResolvedValue(0);
    mockQueryBySender.mockResolvedValue([]);
    mockMessageQuery.mockResolvedValue([]);
    mockExecAgent.mockResolvedValue({ operationId: 'op-1', success: true });
    mockInterruptTask.mockResolvedValue({ operationId: 'op-1', success: true });
    mockSetQueuedMessages.mockResolvedValue({ success: true });
    mockSignUserJWT.mockResolvedValue('visitor-jwt');
    mockSpendGate.mockResolvedValue({ allowed: true });
    mockFileFindByIds.mockResolvedValue([]);
    mockFileFindById.mockResolvedValue(undefined);
    mockFileCreate.mockResolvedValue({ id: 'file-new' });
    mockFileDeleteUnreferenced.mockResolvedValue(undefined);
    mockReserveUpload.mockResolvedValue({ id: 'upload-1', size: 10 });
    mockCreatePreSignedUrl.mockResolvedValue('https://s3/put');
    mockUploadTouchActive.mockResolvedValue(undefined);
    mockUploadFindLatest.mockResolvedValue(undefined);
    mockUploadRelease.mockResolvedValue(true);
    mockUploadReleaseBestEffort.mockResolvedValue(undefined);
    mockUploadFindLatestForUpdate.mockResolvedValue(undefined);
    mockUploadSettle.mockResolvedValue(true);
    mockGetFileAccessUrl.mockResolvedValue('https://s3/get');
    mockGetFileMetadata.mockResolvedValue({ contentLength: 10 });
    mockDeleteStoredFile.mockResolvedValue(undefined);
    mockResolveBySource.mockResolvedValue({ handled: false });
    mockDispatchClaimed.mockResolvedValue({ status: 'resolving' });
    mockBridgeLegacyResume.mockResolvedValue(undefined);
    mockFindMessagePlugin.mockResolvedValue(undefined);
    mockStopPendingApproval.mockResolvedValue({ operationId: 'op-1', success: true });
  });

  describe('execAgent', () => {
    // The owner never uses the visitor chain, so this entry point demands the
    // same `link` visibility the per-step revalidation
    // (`AgentShareModel.isRunStillAuthorized`) demands — otherwise an owner
    // previewing their own private share could start a run its own step loop
    // would immediately abort.
    it('rejects a share that is not link-visible, even for the owner', async () => {
      mockAccessCheck.mockResolvedValue({ ...share, visibility: 'private' });
      const caller = await createCaller();

      await expect(caller.execAgent({ prompt: 'hi', shareId: 'share-1' })).rejects.toMatchObject({
        code: 'FORBIDDEN',
      });
      expect(mockExecAgent).not.toHaveBeenCalled();
    });

    // The spend gate runs before ANY row is created, so a rejected run leaves
    // no orphan topic / placeholder assistant message behind.
    it('rejects the run when the spend gate vetoes it, before any topic lookup', async () => {
      mockSpendGate.mockResolvedValue({ allowed: false });
      const caller = await createCaller();

      await expect(caller.execAgent({ prompt: 'hi', shareId: 'share-1' })).rejects.toMatchObject({
        code: 'TOO_MANY_REQUESTS',
        message: 'ShareSpendLimitExceeded',
      });
      expect(mockCountMessagesBySender).not.toHaveBeenCalled();
      expect(mockExecAgent).not.toHaveBeenCalled();
    });

    it('passes the creator, share and configured cap to the spend gate', async () => {
      mockAccessCheck.mockResolvedValue({
        ...share,
        shareConfig: { ...share.shareConfig, monthlySpendLimit: 25 },
      });
      const caller = await createCaller();

      await caller.execAgent({ prompt: 'hi', shareId: 'share-1' });

      expect(mockSpendGate).toHaveBeenCalledWith({
        agentId: share.agentId,
        monthlySpendLimit: 25,
        ownerUserId: OWNER,
        shareId: 'share-1',
        visitorUserId: VISITOR,
      });
    });

    it('refuses new and existing topics once the visitor has sent their message cap', async () => {
      mockCountMessagesBySender.mockResolvedValue(3);
      const caller = await createCaller();
      const refused = { code: 'TOO_MANY_REQUESTS', message: 'ShareMessageLimitExceeded' };

      await expect(caller.execAgent({ prompt: 'hi', shareId: 'share-1' })).rejects.toMatchObject(
        refused,
      );
      // The cap spans all the visitor's topics, so an existing topic is refused too.
      await expect(
        caller.execAgent({ prompt: 'hi', shareId: 'share-1', topicId: 'tpc_visitor' }),
      ).rejects.toMatchObject(refused);
      expect(mockCountMessagesBySender).toHaveBeenCalledWith({
        agentId: share.agentId,
        senderId: VISITOR,
      });
      expect(mockExecAgent).not.toHaveBeenCalled();
    });

    it('admits a run while the visitor is below the message cap', async () => {
      mockCountMessagesBySender.mockResolvedValue(2);
      const caller = await createCaller();

      await caller.execAgent({ prompt: 'hi', shareId: 'share-1' });

      expect(mockExecAgent).toHaveBeenCalled();
    });

    it("fails closed when the topic is not the visitor's own share topic", async () => {
      mockFindById.mockResolvedValue({ ...visitorTopic, senderId: 'someone-else' });
      const caller = await createCaller();

      await expect(
        caller.execAgent({ prompt: 'hi', shareId: 'share-1', topicId: 'tpc_visitor' }),
      ).rejects.toMatchObject({ code: 'NOT_FOUND' });
      expect(mockExecAgent).not.toHaveBeenCalled();
    });

    it('keeps the steer mark of a follow-up the visitor queued behind a running turn', async () => {
      const caller = await createCaller();

      await caller.execAgent({ prompt: 'follow up', shareId: 'share-1', steer: true });

      expect(mockExecAgent).toHaveBeenCalledWith(expect.objectContaining({ steer: true }));
    });

    it('dispatches a creator-scoped run carrying the share gate', async () => {
      const caller = await createCaller();

      await expect(caller.execAgent({ prompt: 'hi', shareId: 'share-1' })).resolves.toMatchObject({
        operationId: 'op-1',
      });

      // Service runs as the CREATOR — the share's owner, never the visitor.
      expect(AiAgentServiceMock).toHaveBeenCalledWith(expect.anything(), OWNER, expect.any(Object));
      expect(mockExecAgent).toHaveBeenCalledWith(
        expect.objectContaining({
          // Agent id comes from the share record, not client input.
          agentId: share.agentId,
          shareGate: expect.objectContaining({
            agentId: share.agentId,
            shareConfig: share.shareConfig,
            shareId: share.shareId,
            visitorUserId: VISITOR,
          }),
        }),
      );
    });

    describe('attachments', () => {
      const fileIds = ['file-a', 'file-b'];

      it("forwards fileIds to the run after checking each one's share provenance in the CREATOR scope", async () => {
        mockFileFindByIds.mockResolvedValue(fileIds.map((id) => ({ id })));
        const caller = await createCaller();

        await caller.execAgent({ fileIds, prompt: 'look', shareId: 'share-1' });

        expect(FileModelMock).toHaveBeenCalledWith(expect.anything(), OWNER, undefined);
        expect(mockFileFindByIds).toHaveBeenCalledWith(fileIds, {
          shareId: share.shareId,
          type: 'agentShare',
          visitorUserId: VISITOR,
        });
        expect(mockExecAgent).toHaveBeenCalledWith(expect.objectContaining({ fileIds }));
      });

      it('rejects with NOT_FOUND when the provenance-scoped lookup omits any id', async () => {
        mockFileFindByIds.mockResolvedValue([{ id: 'file-a' }]);
        const caller = await createCaller();

        await expect(
          caller.execAgent({ fileIds, prompt: 'look', shareId: 'share-1' }),
        ).rejects.toMatchObject({ code: 'NOT_FOUND', message: 'File not found' });
        expect(mockCountMessagesBySender).not.toHaveBeenCalled();
        expect(mockExecAgent).not.toHaveBeenCalled();
      });

      it('accepts duplicate ids as long as each distinct id is owned', async () => {
        mockFileFindByIds.mockResolvedValue([{ id: 'file-a' }]);
        const caller = await createCaller();

        await expect(
          caller.execAgent({ fileIds: ['file-a', 'file-a'], prompt: 'look', shareId: 'share-1' }),
        ).resolves.toMatchObject({ operationId: 'op-1' });
        expect(mockFileFindByIds).toHaveBeenCalledWith(['file-a'], {
          shareId: share.shareId,
          type: 'agentShare',
          visitorUserId: VISITOR,
        });
      });

      it('skips the ownership lookup entirely when no fileIds are sent', async () => {
        const caller = await createCaller();

        await caller.execAgent({ prompt: 'hi', shareId: 'share-1' });

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

      it('rejects more than SHARE_VISITOR_MAX_FILES_PER_TURN ids at the schema, before any lookup', async () => {
        const caller = await createCaller();
        const tooMany = Array.from({ length: 11 }, (_, i) => `file-${i}`);

        await expect(
          caller.execAgent({ fileIds: tooMany, prompt: 'look', shareId: 'share-1' }),
        ).rejects.toMatchObject({ code: 'BAD_REQUEST' });
        expect(FileModelMock).not.toHaveBeenCalled();
        expect(mockExecAgent).not.toHaveBeenCalled();
      });
    });

    // Regression for Codex P1 (`shareChat.ts` prompt schema): a
    // direct RPC caller (bypassing any client-side textarea limit) could
    // previously submit an HTTP-infrastructure-limit-sized `prompt`, which
    // `AiAgentService.execAgent` would persist verbatim into the CREATOR's
    // messages before any topic/turn cap even runs (those gate request
    // COUNT, not per-request SIZE). The schema now rejects an oversized
    // prompt before any row is touched.
    it('rejects an oversized prompt before any DB row is touched', async () => {
      const caller = await createCaller();
      const oversizedPrompt = 'a'.repeat(20_001);

      await expect(
        caller.execAgent({ prompt: oversizedPrompt, shareId: 'share-1' }),
      ).rejects.toMatchObject({ code: 'BAD_REQUEST' });
      expect(mockAccessCheck).not.toHaveBeenCalled();
      expect(mockExecAgent).not.toHaveBeenCalled();
    });

    it('accepts a prompt right at the size limit', async () => {
      const caller = await createCaller();
      const maxPrompt = 'a'.repeat(20_000);

      await expect(
        caller.execAgent({ prompt: maxPrompt, shareId: 'share-1' }),
      ).resolves.toMatchObject({ operationId: 'op-1' });
    });

    // Regression for Codex P2: a startup failure BEFORE Gateway
    // streaming begins (e.g. the queue/runtime backend returning a raw
    // diagnostic) must not reach the visitor verbatim — the run executes
    // under the CREATOR's identity, so `error.message` here can carry
    // provider/infra detail. `toVisitorSafeStartupError` must project it
    // through the same `sanitizeVisitorError` classification used elsewhere
    // in this branch, not echo it back raw.
    it('redacts a diagnostic startup failure instead of leaking it to the visitor', async () => {
      const diagnostic = new Error(
        'ECONNREFUSED connecting to internal-runtime-queue.prod.internal:6379 (provider=openai, apiKey=sk-***)',
      );
      mockExecAgent.mockRejectedValueOnce(diagnostic);
      const caller = await createCaller();

      const rejection = caller.execAgent({ prompt: 'hi', shareId: 'share-1' });
      await expect(rejection).rejects.toMatchObject({ code: 'INTERNAL_SERVER_ERROR' });
      await rejection.catch((error: any) => {
        expect(error.message).not.toContain('internal-runtime-queue');
        expect(error.message).not.toContain('openai');
        expect(error.message).not.toContain('sk-');
      });
    });

    // Regression for Codex P2 follow-up (`shareChat.ts:249`):
    // `AiAgentService.execAgent` RESOLVES (rather than throws) with
    // `{ success: false, error }` when `createOperation` itself fails to
    // start (see `aiAgent/index.ts`'s `execAgent` catch block) — a case the
    // surrounding try/catch above never sees because nothing was thrown.
    // Without a check on the resolved value, that raw `error` (and the
    // whole "started" shape) would flow straight back to the visitor and
    // the Gateway client would try to open a WebSocket for an operation
    // that never began.
    it('redacts a RESOLVED (not thrown) startup failure and never returns it as a live operation', async () => {
      const diagnostic =
        'QStash publish failed: 503 from internal-queue.prod.internal (token=shhh)';
      mockExecAgent.mockResolvedValueOnce({
        agentId: share.agentId,
        assistantMessageId: 'msg_assistant',
        autoStarted: false,
        createdAt: new Date().toISOString(),
        error: diagnostic,
        message: 'Agent operation failed to start',
        operationId: 'op-failed',
        status: 'error',
        success: false,
        timestamp: new Date().toISOString(),
        topicId: 'tpc_visitor',
        userMessageId: 'msg_user',
      });
      const caller = await createCaller();

      const rejection = caller.execAgent({ prompt: 'hi', shareId: 'share-1' });
      await expect(rejection).rejects.toMatchObject({ code: 'INTERNAL_SERVER_ERROR' });
      await rejection.catch((error: any) => {
        expect(error.message).not.toContain('internal-queue');
        expect(error.message).not.toContain('token=shhh');
      });
    });

    it('never sets interactiveStart for a new send, so concurrent visitor sends contend on the real runningOperation liveness instead of only the short reservation', async () => {
      // Regression for Codex P1 (`shareChat.ts:186`): `interactiveStart:
      // true` makes `TopicModel.tryReserveTaskCallback` skip its `runningOperation`
      // liveness check entirely (`ignoreRunningOperation`) and contend only on the
      // short-lived `taskCallbackReservation`, which is released right after the
      // FIRST operation is created — long before it finishes running. That let a
      // second concurrent visitor send for the same topic claim the topic-start
      // reservation too, create its own creator-credentialed operation, and
      // overwrite the topic's `runningOperation` marker, orphaning the first
      // operation beyond the reach of `interruptTask` / the revocation sweep. See
      // `topicStartReservation.shareVisitorConcurrency.race.test.ts` for the
      // real-Postgres proof of the underlying reservation mechanics this pins.
      const caller = await createCaller();

      await caller.execAgent({ prompt: 'hi', shareId: 'share-1' });

      expect(mockExecAgent).toHaveBeenCalledWith(
        expect.objectContaining({ interactiveStart: false }),
      );
    });
  });

  describe('intervention approval', () => {
    const shareVisitor = { agentId: share.agentId, ownerUserId: OWNER, topicId: 'tpc_visitor' };
    const resumeApproval = {
      decision: 'approved' as const,
      parentMessageId: 'msg-tool',
      toolCallId: 'call-1',
    };
    const source = {
      action: { scope: 'once' as const, type: 'approve_tool' as const },
      batchId: 'batch-1',
      operationId: 'op-parked',
      resolutionRequestId: '018fbd8e-7baf-7c6d-8000-000000000001',
      targets: [{ toolCallId: 'call-1', toolMessageId: 'msg-tool' }],
    };

    it("resumes the visitor's parked run with their answer and approval mode", async () => {
      // Answering the last allowed turn's approval must not be refused.
      mockCountMessagesBySender.mockResolvedValue(3);
      const caller = await createCaller();

      await caller.execAgent({
        parentMessageId: 'msg-tool',
        prompt: '',
        resumeApproval,
        shareId: 'share-1',
        topicId: 'tpc_visitor',
        userInterventionConfig: { approvalMode: 'allow-list', allowList: ['lobe-web-browsing'] },
      });

      expect(mockCountMessagesBySender).not.toHaveBeenCalled();
      expect(mockBridgeLegacyResume).toHaveBeenCalledWith(
        expect.objectContaining({ resumeApproval, shareVisitor }),
        expect.objectContaining({ shareGate: expect.any(Object), userId: VISITOR }),
      );
      expect(mockExecAgent).toHaveBeenCalledWith(
        expect.objectContaining({
          // The parked operation still holds the topic; the exactly-once
          // approval claim, not the liveness check, serializes resumes.
          interactiveStart: true,
          parentMessageId: 'msg-tool',
          resume: true,
          resumeApproval,
          shareGate: expect.objectContaining({ visitorUserId: VISITOR }),
          userInterventionConfig: {
            allowList: ['lobe-web-browsing'],
            approvalMode: 'allow-list',
          },
        }),
      );
    });

    it('returns the durable claim result instead of starting a legacy resume', async () => {
      mockBridgeLegacyResume.mockResolvedValue({ operationId: 'op-continued', success: true });
      const caller = await createCaller();

      await expect(
        caller.execAgent({
          parentMessageId: 'msg-tool',
          prompt: '',
          resumeApproval,
          shareId: 'share-1',
          topicId: 'tpc_visitor',
        }),
      ).resolves.toMatchObject({ operationId: 'op-continued' });
      expect(mockExecAgent).not.toHaveBeenCalled();
    });

    it('rejects a resume outside an existing visitor topic', async () => {
      const caller = await createCaller();

      await expect(
        caller.execAgent({
          parentMessageId: 'msg-tool',
          prompt: '',
          resumeApproval,
          shareId: 'share-1',
        }),
      ).rejects.toMatchObject({ code: 'BAD_REQUEST' });
      expect(mockExecAgent).not.toHaveBeenCalled();
    });

    it("resolves a source intervention as the visitor, scoped to the visitor's topic", async () => {
      mockResolveBySource.mockResolvedValue({
        claimId: 'claim-1',
        handled: true,
        ownerUserId: OWNER,
        state: 'claimed',
      });
      mockDispatchClaimed.mockResolvedValue({
        execution: { operationId: 'op-continued', success: true },
        status: 'resolving',
      });
      const caller = await createCaller();

      await expect(
        caller.resolveInterventionBySource({
          ...source,
          shareId: 'share-1',
          topicId: 'tpc_visitor',
        }),
      ).resolves.toMatchObject({ state: 'claimed', success: true });
      expect(mockResolveBySource).toHaveBeenCalledWith(
        expect.objectContaining({ ...source, actorUserId: VISITOR, shareVisitor }),
      );
      expect(mockDispatchClaimed).toHaveBeenCalledWith(
        expect.objectContaining({ claimId: 'claim-1' }),
        expect.objectContaining({
          shareGate: expect.objectContaining({ visitorUserId: VISITOR }),
          userId: VISITOR,
        }),
      );
    });

    it("refuses to remember an approval into the creator's allow list", async () => {
      const caller = await createCaller();

      await expect(
        caller.resolveInterventionBySource({
          ...source,
          action: { scope: 'remember', type: 'approve_tool' },
          shareId: 'share-1',
          topicId: 'tpc_visitor',
        }),
      ).rejects.toMatchObject({ code: 'FORBIDDEN' });
      expect(mockResolveBySource).not.toHaveBeenCalled();
    });

    it("fails closed on a topic that is not the visitor's own", async () => {
      mockFindById.mockResolvedValue({ ...visitorTopic, senderId: 'someone-else' });
      const caller = await createCaller();

      await expect(
        caller.resolveInterventionBySource({
          ...source,
          shareId: 'share-1',
          topicId: 'tpc_visitor',
        }),
      ).rejects.toMatchObject({ code: 'NOT_FOUND' });
      expect(mockResolveBySource).not.toHaveBeenCalled();
    });

    it('reports an unavailable durable store so the client falls back to a resume', async () => {
      const caller = await createCaller();

      await expect(
        caller.resolveInterventionBySource({
          ...source,
          shareId: 'share-1',
          topicId: 'tpc_visitor',
        }),
      ).resolves.toMatchObject({ status: 'unavailable', success: false });
      expect(mockDispatchClaimed).not.toHaveBeenCalled();
    });

    it('stops a parked run through the creator-scoped service without a durable store', async () => {
      const caller = await createCaller();
      const input = {
        batchId: 'batch-1',
        operationId: 'op-parked',
        toolMessageIds: ['msg-tool'],
        topicId: 'tpc_visitor',
      };

      await caller.stopPendingApproval({ ...input, shareId: 'share-1' });

      expect(mockStopPendingApproval).toHaveBeenCalledWith(input);
    });

    it('stops a durable batch through the source claim as the visitor', async () => {
      mockFindMessagePlugin.mockResolvedValue({
        intervention: { batchId: 'batch-1', operationId: 'op-parked' },
        toolCallId: 'call-1',
      });
      mockResolveBySource.mockResolvedValue({ handled: true, state: 'claimed' });
      const caller = await createCaller();

      await caller.stopPendingApproval({
        batchId: 'batch-1',
        operationId: 'op-parked',
        shareId: 'share-1',
        toolMessageIds: ['msg-tool'],
        topicId: 'tpc_visitor',
      });

      expect(mockResolveBySource).toHaveBeenCalledWith(
        expect.objectContaining({
          action: { scope: 'operation', type: 'stop' },
          actorUserId: VISITOR,
          shareVisitor,
          targets: [{ toolCallId: 'call-1', toolMessageId: 'msg-tool' }],
        }),
      );
      expect(mockDispatchClaimed).toHaveBeenCalled();
      expect(mockStopPendingApproval).not.toHaveBeenCalled();
    });
  });

  describe('createUploadUrl', () => {
    const prefix = `files/${OWNER}/agent-share/share-1/`;
    const MB = 1024 * 1024;

    beforeEach(() => {
      mockFileCountAgentShareUsage.mockResolvedValue(0);
      mockUploadCountLiveUnderPrefix.mockResolvedValue(0);
      // Run the share's `admit` hook the way the real reservation does: inside
      // the transaction, before anything is inserted.
      mockReserveUpload.mockImplementation(
        async (params: { admit?: (trx: unknown) => Promise<void>; size: number }) => {
          await params.admit?.({ trx: true });
          return { id: 'upload-1', size: params.size };
        },
      );
    });

    it("counts the share's settled files and live reservations inside the reservation transaction", async () => {
      mockFileCountAgentShareUsage.mockResolvedValue(500 * MB);
      mockUploadCountLiveUnderPrefix.mockResolvedValue(11 * MB);
      const caller = await createCaller();

      // 500 + 11 + 1 = 512 MB: exactly at the default cap is still admitted.
      await caller.createUploadUrl({ name: 'cat.png', shareId: 'share-1', size: 1 * MB });

      expect(mockFileCountAgentShareUsage).toHaveBeenCalledWith('share-1', { trx: true });
      expect(mockUploadCountLiveUnderPrefix).toHaveBeenCalledWith(prefix, { trx: true });
      expect(mockCreatePreSignedUrl).toHaveBeenCalled();
    });

    it('serializes the cap decision on the share BEFORE counting, inside the same transaction', async () => {
      // The counts alone are racy: nothing row-locks a sum over two tables, and
      // the reservation's own users-row lock is taken after `admit`. Two
      // concurrent visitors must therefore queue on the share lock first.
      const order: string[] = [];
      mockLockUploadAdmission.mockImplementation(async () => {
        order.push('lock');
      });
      mockFileCountAgentShareUsage.mockImplementation(async () => {
        order.push('settled');
        return 0;
      });
      mockUploadCountLiveUnderPrefix.mockImplementation(async () => {
        order.push('reserved');
        return 0;
      });
      const caller = await createCaller();

      await caller.createUploadUrl({ name: 'cat.png', shareId: 'share-1', size: 10 });

      expect(mockLockUploadAdmission).toHaveBeenCalledWith({ trx: true }, 'share-1');
      expect(order).toEqual(['lock', 'settled', 'reserved']);
    });

    it('rejects a zero-byte reservation even though it would add nothing to the cap sum', async () => {
      const caller = await createCaller();

      await expect(
        caller.createUploadUrl({ name: 'cat.png', shareId: 'share-1', size: 0 }),
      ).rejects.toMatchObject({ code: 'BAD_REQUEST' });
      expect(mockReserveUpload).not.toHaveBeenCalled();
    });

    it("refuses with a share_limit block once the share's upload space is used up", async () => {
      mockFileCountAgentShareUsage.mockResolvedValue(500 * MB);
      mockUploadCountLiveUnderPrefix.mockResolvedValue(11 * MB);
      const caller = await createCaller();

      await expect(
        caller.createUploadUrl({ name: 'cat.png', shareId: 'share-1', size: 2 * MB }),
      ).rejects.toMatchObject({ code: 'FORBIDDEN', message: 'storage_block:share_limit' });
      expect(mockCreatePreSignedUrl).not.toHaveBeenCalled();
    });

    it('honours a creator-configured cap and turns attachments off at 0 before any reservation', async () => {
      mockAccessCheck.mockResolvedValue({
        ...share,
        shareConfig: { ...share.shareConfig, maxFileStorage: 0 },
      });
      const caller = await createCaller();

      await expect(
        caller.createUploadUrl({ name: 'cat.png', shareId: 'share-1', size: 1 }),
      ).rejects.toMatchObject({ code: 'FORBIDDEN', message: 'storage_block:share_limit' });
      expect(mockReserveUpload).not.toHaveBeenCalled();
    });

    it('refuses even a request that clears the size pre-check when the cap is 0', async () => {
      // Guards the `maxFileStorage <= 0` branch on its own: a request whose size
      // slips past `size > maxFileStorage` (only possible at 0 with a size of 0,
      // which zod refuses, or via a future relaxation) must still be turned away.
      mockAccessCheck.mockResolvedValue({
        ...share,
        shareConfig: { ...share.shareConfig, maxFileStorage: -1 },
      });
      const caller = await createCaller();

      await expect(
        caller.createUploadUrl({ name: 'cat.png', shareId: 'share-1', size: 1 }),
      ).rejects.toMatchObject({ code: 'FORBIDDEN', message: 'storage_block:share_limit' });
      expect(mockReserveUpload).not.toHaveBeenCalled();
    });

    it("reserves the upload under the CREATOR's storage quota and returns a share-prefixed key", async () => {
      const caller = await createCaller();

      const result = await caller.createUploadUrl({
        name: 'cat.png',
        shareId: 'share-1',
        size: 10,
      });

      expect(result.pathname.startsWith(prefix)).toBe(true);
      expect(result.pathname.endsWith('/cat.png')).toBe(true);
      expect(result.url).toBe('https://s3/put');
      // The quota that pays is the one reserved: creator, never the visitor.
      expect(FileUploadModelMock).toHaveBeenCalledWith(expect.anything(), OWNER, undefined);
      expect(mockReserveUpload).toHaveBeenCalledWith(
        expect.objectContaining({ pathname: result.pathname, size: 10, userId: OWNER }),
      );
      expect(mockReserveUpload.mock.calls[0][0].workspaceId).toBeNull();
      expect(mockCreatePreSignedUrl).toHaveBeenCalledWith(result.pathname, 10);
    });

    it('keeps a visitor-supplied name from steering the key out of the share prefix', async () => {
      const caller = await createCaller();

      const result = await caller.createUploadUrl({
        name: '../../etc/passwd',
        shareId: 'share-1',
        size: 1,
      });

      expect(result.pathname.startsWith(prefix)).toBe(true);
      expect(result.pathname.endsWith('/.._.._etc_passwd')).toBe(true);
    });

    it("collapses the creator's storage_block reason to a visitor-safe code and never reaches S3", async () => {
      // The original reason describes the creator's billing state — a
      // stranger with the link must not learn it.
      mockReserveUpload.mockRejectedValue(
        new TRPCError({ code: 'FORBIDDEN', message: 'storage_block:subscription_past_due' }),
      );
      const caller = await createCaller();

      await expect(
        caller.createUploadUrl({ name: 'cat.png', shareId: 'share-1', size: 10 }),
      ).rejects.toMatchObject({ code: 'FORBIDDEN', message: 'storage_block:creator_quota' });
      expect(mockCreatePreSignedUrl).not.toHaveBeenCalled();
    });

    it('passes other reservation failures through untouched', async () => {
      mockReserveUpload.mockRejectedValue(
        new TRPCError({ code: 'CONFLICT', message: 'Upload pathname is already reserved' }),
      );
      const caller = await createCaller();

      await expect(
        caller.createUploadUrl({ name: 'cat.png', shareId: 'share-1', size: 10 }),
      ).rejects.toMatchObject({ code: 'CONFLICT', message: 'Upload pathname is already reserved' });
    });

    it('releases the reservation when minting the pre-signed URL fails', async () => {
      mockCreatePreSignedUrl.mockRejectedValue(new Error('s3 down'));
      const caller = await createCaller();

      await expect(
        caller.createUploadUrl({ name: 'cat.png', shareId: 'share-1', size: 10 }),
      ).rejects.toThrow('s3 down');
      expect(mockUploadReleaseBestEffort).toHaveBeenCalledWith(
        expect.stringMatching(new RegExp(`^${prefix}`)),
      );
    });

    it('rejects a file over SHARE_VISITOR_MAX_FILE_SIZE at the schema', async () => {
      const caller = await createCaller();

      await expect(
        caller.createUploadUrl({ name: 'big.bin', shareId: 'share-1', size: 33 * 1024 * 1024 }),
      ).rejects.toMatchObject({ code: 'BAD_REQUEST' });
      expect(mockReserveUpload).not.toHaveBeenCalled();
    });

    it('is refused on a private share like every other visitor procedure', async () => {
      mockAccessCheck.mockResolvedValue({ ...share, visibility: 'private' });
      const caller = await createCaller();

      await expect(
        caller.createUploadUrl({ name: 'cat.png', shareId: 'share-1', size: 10 }),
      ).rejects.toMatchObject({ code: 'FORBIDDEN' });
      expect(mockReserveUpload).not.toHaveBeenCalled();
    });
  });

  describe('createFile', () => {
    const pathname = `files/${OWNER}/agent-share/share-1/abc/cat.png`;
    const input = {
      fileType: 'image/png',
      metadata: { height: 2, ratio: 0.5, width: 1 },
      name: 'cat.png',
      pathname,
      shareId: 'share-1',
      size: 10,
    };

    beforeEach(() => {
      mockUploadTouchActive.mockResolvedValue({ id: 'upload-1', size: 10, status: 'active' });
      mockUploadFindLatestForUpdate.mockResolvedValue({
        id: 'upload-1',
        size: 10,
        status: 'active',
      });
    });

    it('writes a creator-owned row with visitor provenance and settles the session', async () => {
      const caller = await createCaller();

      const result = await caller.createFile(input);

      expect(result).toEqual({ id: 'file-new', url: 'https://s3/get' });
      expect(FileModelMock).toHaveBeenCalledWith(expect.anything(), OWNER, undefined);
      expect(mockFileCreate).toHaveBeenCalledWith(
        expect.objectContaining({
          fileType: 'image/png',
          metadata: expect.objectContaining({
            agentShare: { shareId: 'share-1', visitorUserId: VISITOR },
            dirname: `files/${OWNER}/agent-share/share-1/abc`,
            filename: 'cat.png',
            height: 2,
            path: pathname,
            width: 1,
          }),
          name: 'cat.png',
          size: 10,
          url: pathname,
        }),
        false,
        expect.anything(),
      );
      expect(mockFileCreate.mock.calls[0][0]).not.toHaveProperty('fileHash');
      expect(mockFileCreate.mock.calls[0][0]).not.toHaveProperty('source');
      expect(mockUploadSettle).toHaveBeenCalledWith('upload-1', 'file-new', expect.anything());
    });

    it('serializes settlement with Agent transfer and revalidates the share after the lock', async () => {
      const caller = await createCaller();

      await caller.createFile(input);

      expect(mockLockScopedAgentRow).toHaveBeenCalledWith({ trx: true }, share.agentId, {
        userId: OWNER,
        workspaceId: undefined,
      });
      expect(mockIsRunStillAuthorized).toHaveBeenCalledWith(
        { trx: true },
        { agentId: share.agentId, shareId: share.shareId },
      );
      expect(mockLockScopedAgentRow.mock.invocationCallOrder[0]).toBeLessThan(
        mockUploadFindLatestForUpdate.mock.invocationCallOrder[0],
      );
    });

    it('rejects settlement when hard revocation wins the Agent row lock', async () => {
      mockLockScopedAgentRow.mockResolvedValue(null);
      const caller = await createCaller();

      await expect(caller.createFile(input)).rejects.toMatchObject({ code: 'NOT_FOUND' });

      expect(mockFileCreate).not.toHaveBeenCalled();
      expect(mockUploadSettle).not.toHaveBeenCalled();
    });

    it('ignores a client-supplied hash instead of registering the object for dedup', async () => {
      const caller = await createCaller();

      await caller.createFile({ ...input, hash: 'h'.repeat(64) } as typeof input);

      expect(mockFileCreate.mock.calls[0][0]).not.toHaveProperty('fileHash');
      expect(mockFileCreate.mock.calls[0][1]).toBe(false);
    });

    it('refuses a pathname outside the share prefix (a creator-owned reservation)', async () => {
      const caller = await createCaller();

      await expect(
        caller.createFile({ ...input, pathname: `files/${OWNER}/2026/own.png` }),
      ).rejects.toMatchObject({ code: 'NOT_FOUND', message: 'Upload not found' });
      expect(mockUploadTouchActive).not.toHaveBeenCalled();
      expect(mockFileCreate).not.toHaveBeenCalled();
    });

    it("refuses another share's prefix even for the same creator", async () => {
      const caller = await createCaller();

      await expect(
        caller.createFile({ ...input, pathname: `files/${OWNER}/agent-share/share-2/abc/cat.png` }),
      ).rejects.toMatchObject({ code: 'NOT_FOUND' });
      expect(mockFileCreate).not.toHaveBeenCalled();
    });

    it('rejects when there is no active reservation (no legacy path for share uploads)', async () => {
      mockUploadTouchActive.mockResolvedValue(undefined);
      const caller = await createCaller();

      await expect(caller.createFile(input)).rejects.toMatchObject({ code: 'CONFLICT' });
      expect(mockFileCreate).not.toHaveBeenCalled();
    });

    it('releases the reservation when the stored object size does not match', async () => {
      mockGetFileMetadata.mockResolvedValue({ contentLength: 11 });
      const caller = await createCaller();

      await expect(caller.createFile(input)).rejects.toMatchObject({
        code: 'BAD_REQUEST',
        message: 'Uploaded file size mismatch',
      });
      expect(mockUploadReleaseBestEffort).toHaveBeenCalledWith(pathname);
      expect(mockFileCreate).not.toHaveBeenCalled();
    });

    it('drops unknown metadata keys at the schema so provenance can only be server-written', async () => {
      const caller = await createCaller();

      await caller.createFile({
        ...input,
        metadata: {
          ...input.metadata,
          agentShare: { shareId: 'share-1', visitorUserId: 'someone-else' },
        } as any,
      });

      expect(mockFileCreate.mock.calls[0][0].metadata.agentShare).toEqual({
        shareId: 'share-1',
        visitorUserId: VISITOR,
      });
    });
  });

  describe('abortUpload', () => {
    it('releases an active reservation under the share prefix', async () => {
      const pathname = `files/${OWNER}/agent-share/share-1/abc/cat.png`;
      mockUploadFindLatest.mockResolvedValue({ status: 'active' });
      const caller = await createCaller();

      await caller.abortUpload({ pathname, shareId: 'share-1' });

      expect(FileUploadServiceMock).toHaveBeenCalledWith(expect.anything(), OWNER, undefined);
      expect(mockUploadRelease).toHaveBeenCalledWith(pathname);
    });

    it("cannot release a reservation outside the share prefix (the creator's own upload)", async () => {
      const caller = await createCaller();

      await expect(
        caller.abortUpload({ pathname: `files/${OWNER}/2026/own.png`, shareId: 'share-1' }),
      ).rejects.toMatchObject({ code: 'NOT_FOUND' });
      expect(mockUploadRelease).not.toHaveBeenCalled();
    });
  });

  describe('removeFile', () => {
    it("deletes this visitor's own unsent share upload and the stored object", async () => {
      mockFileFindById.mockResolvedValue({ id: 'file-a' });
      mockFileDeleteUnreferenced.mockResolvedValue({
        id: 'file-a',
        url: 'files/x/cat.png',
      });
      const caller = await createCaller();

      await caller.removeFile({ fileId: 'file-a', shareId: 'share-1' });

      expect(FileModelMock).toHaveBeenCalledWith(expect.anything(), OWNER, undefined);
      expect(mockFileFindById).toHaveBeenCalledWith('file-a', {
        accessScope: {
          shareId: share.shareId,
          type: 'agentShare',
          visitorUserId: VISITOR,
        },
      });
      expect(mockFileDeleteUnreferenced).toHaveBeenCalledWith('file-a', {
        accessScope: {
          shareId: share.shareId,
          type: 'agentShare',
          visitorUserId: VISITOR,
        },
        removeGlobalFile: true,
      });
      expect(mockDeleteStoredFile).toHaveBeenCalledWith('files/x/cat.png');
    });

    it('leaves the stored object alone when the row is already referenced by a message', async () => {
      mockFileFindById.mockResolvedValue({ id: 'file-a' });
      mockFileDeleteUnreferenced.mockResolvedValue(undefined);
      const caller = await createCaller();

      await caller.removeFile({ fileId: 'file-a', shareId: 'share-1' });

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

    it('refuses a file outside the share and visitor provenance scope', async () => {
      const caller = await createCaller();

      await expect(
        caller.removeFile({ fileId: 'file-a', shareId: 'share-1' }),
      ).rejects.toMatchObject({ code: 'NOT_FOUND', message: 'File not found' });
      expect(mockFileDeleteUnreferenced).not.toHaveBeenCalled();
    });
  });

  describe('interruptTask', () => {
    it('interrupts a running operation that matches the topic ownership and running marker', async () => {
      const caller = await createCaller();

      await expect(
        caller.interruptTask({ operationId: 'op-1', shareId: 'share-1', topicId: 'tpc_visitor' }),
      ).resolves.toMatchObject({ operationId: 'op-1', success: true });

      // Service runs as the CREATOR — the run's operation/thread rows live there.
      expect(AiAgentServiceMock).toHaveBeenCalledWith(expect.anything(), OWNER, {
        includeShareVisitor: true,
      });
      expect(mockInterruptTask).toHaveBeenCalledWith({
        operationId: 'op-1',
        topicId: 'tpc_visitor',
      });
    });

    it("fails closed when the topic is not the visitor's own share topic", async () => {
      mockFindById.mockResolvedValue({ ...visitorTopic, senderId: 'someone-else' });
      const caller = await createCaller();

      await expect(
        caller.interruptTask({ operationId: 'op-1', shareId: 'share-1', topicId: 'tpc_visitor' }),
      ).rejects.toMatchObject({ code: 'NOT_FOUND' });
      expect(mockInterruptTask).not.toHaveBeenCalled();
    });

    it('rejects an operationId that does not match the topic’s current running operation', async () => {
      const caller = await createCaller();

      await expect(
        caller.interruptTask({
          operationId: 'op-someone-elses',
          shareId: 'share-1',
          topicId: 'tpc_visitor',
        }),
      ).rejects.toMatchObject({ code: 'NOT_FOUND' });
      expect(mockInterruptTask).not.toHaveBeenCalled();
    });

    it('rejects when the topic has no running operation at all', async () => {
      mockFindById.mockResolvedValue({ ...visitorTopic, metadata: {} });
      const caller = await createCaller();

      await expect(
        caller.interruptTask({ operationId: 'op-1', shareId: 'share-1', topicId: 'tpc_visitor' }),
      ).rejects.toMatchObject({ code: 'NOT_FOUND' });
      expect(mockInterruptTask).not.toHaveBeenCalled();
    });

    // Regression for Codex P2: same startup-failure redaction as
    // `execAgent` — `AiAgentService.interruptTask` also runs creator-scoped
    // and can throw a raw infra/provider diagnostic before any Gateway event
    // exists to sanitize.
    it('redacts a diagnostic interrupt failure instead of leaking it to the visitor', async () => {
      const diagnostic = new Error(
        'pg driver error: relation "operations_internal" does not exist',
      );
      mockInterruptTask.mockRejectedValueOnce(diagnostic);
      const caller = await createCaller();

      const rejection = caller.interruptTask({
        operationId: 'op-1',
        shareId: 'share-1',
        topicId: 'tpc_visitor',
      });
      await expect(rejection).rejects.toMatchObject({ code: 'INTERNAL_SERVER_ERROR' });
      await rejection.catch((error: any) => {
        expect(error.message).not.toContain('operations_internal');
        expect(error.message).not.toContain('pg driver');
      });
    });
  });

  describe('setQueuedMessages', () => {
    it('flags the running operation through the creator-scoped service', async () => {
      const caller = await createCaller();

      await expect(
        caller.setQueuedMessages({
          operationId: 'op-1',
          pending: true,
          shareId: 'share-1',
          topicId: 'tpc_visitor',
        }),
      ).resolves.toEqual({ success: true });

      expect(AiAgentServiceMock).toHaveBeenCalledWith(expect.anything(), OWNER, {
        includeShareVisitor: true,
      });
      expect(mockSetQueuedMessages).toHaveBeenCalledWith({ operationId: 'op-1', pending: true });
    });

    it('rejects an operationId that does not match the topic’s current running operation', async () => {
      const caller = await createCaller();

      await expect(
        caller.setQueuedMessages({
          operationId: 'op-someone-elses',
          pending: true,
          shareId: 'share-1',
          topicId: 'tpc_visitor',
        }),
      ).rejects.toMatchObject({ code: 'NOT_FOUND' });
      expect(mockSetQueuedMessages).not.toHaveBeenCalled();
    });

    it("fails closed when the topic is not the visitor's own share topic", async () => {
      mockFindById.mockResolvedValue({ ...visitorTopic, senderId: 'someone-else' });
      const caller = await createCaller();

      await expect(
        caller.setQueuedMessages({
          operationId: 'op-1',
          pending: false,
          shareId: 'share-1',
          topicId: 'tpc_visitor',
        }),
      ).rejects.toMatchObject({ code: 'NOT_FOUND' });
      expect(mockSetQueuedMessages).not.toHaveBeenCalled();
    });
  });

  describe('getTopics', () => {
    it("returns only the visitor's own topics via agentId + senderId scoping", async () => {
      const caller = await createCaller();
      await caller.getTopics({ shareId: 'share-1' });

      // Topic model is creator-scoped; the query narrows to this visitor's own
      // topics on this agent. `agent_shares` is 1:1 per agent, so `(agentId,
      // senderId)` identifies the share conversation without a share-instance
      // column on `topics`.
      expect(TopicModelMock).toHaveBeenCalledWith(expect.anything(), OWNER, undefined, undefined, {
        includeShareVisitor: true,
      });
      expect(mockQueryBySender).toHaveBeenCalledWith({
        agentId: share.agentId,
        senderId: VISITOR,
      });
    });

    it('does not tie the list page size to the live message cap', async () => {
      // The cap only gates sending. A creator lowering it must not hide the
      // visitor's older conversations — the visitor surface has no pagination
      // or deep links to recover them.
      mockAccessCheck.mockResolvedValue({
        ...share,
        shareConfig: { ...share.shareConfig, maxMessagesPerVisitor: 1 },
      });
      const caller = await createCaller();
      await caller.getTopics({ shareId: 'share-1' });

      expect(mockQueryBySender).toHaveBeenCalledWith({
        agentId: share.agentId,
        senderId: VISITOR,
      });
    });
  });

  describe('getMessages', () => {
    it('rejects a topic on a different agent of the same creator', async () => {
      mockFindById.mockResolvedValue({ ...visitorTopic, agentId: 'agt_other' });
      const caller = await createCaller();

      await expect(
        caller.getMessages({ shareId: 'share-1', topicId: 'tpc_visitor' }),
      ).rejects.toMatchObject({ code: 'NOT_FOUND' });
      expect(mockMessageQueryForVisitor).not.toHaveBeenCalled();
    });

    it('assembles Work summaries under the visitor’s own share scope', async () => {
      const caller = await createCaller();
      await caller.getMessages({
        includeFileWorks: true,
        shareId: 'share-1',
        topicId: 'tpc_visitor',
      });

      expect(mockMessageQueryForVisitor).toHaveBeenCalledWith(
        { includeFileWorks: true, topicId: 'tpc_visitor' },
        expect.objectContaining({
          // Pinned to share + topic + VISITOR: the ordinary scope would join
          // the creator's Works, which must never reach a visitor surface.
          workAccessScope: {
            shareId: 'share-1',
            topicId: 'tpc_visitor',
            type: 'agentShare',
            visitorUserId: VISITOR,
          },
        }),
      );
    });

    it('keeps the Work-free response for clients that do not opt in', async () => {
      // Pre-Works clients (rolling deploys, cached sessions, lagging desktop
      // builds) omit `includeFileWorks` and cannot open visitor Work cards, so
      // no share scope is supplied and queryForVisitor skips Work assembly.
      const caller = await createCaller();
      await caller.getMessages({ shareId: 'share-1', topicId: 'tpc_visitor' });

      expect(mockMessageQueryForVisitor).toHaveBeenCalledWith(
        { includeFileWorks: undefined, topicId: 'tpc_visitor' },
        expect.objectContaining({ workAccessScope: undefined }),
      );
    });

    it('uses the visitor-redacted read path, never the raw creator-scoped query()', async () => {
      // Regression: getMessages must call `queryForVisitor` (which strips the
      // creator's sender/spend fields), not `query()` — see message.ts
      // `toVisitorMessage` for what that redaction guards against.
      const caller = await createCaller();
      await caller.getMessages({ shareId: 'share-1', topicId: 'tpc_visitor' });

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

  describe('getDocument', () => {
    const document = {
      content: '# Notes',
      fileType: 'text/markdown',
      id: 'doc-1',
      metadata: { agentShare: { shareId: 'share-1' }, secret: 'creator-only' },
      title: 'Notes',
      updatedAt: new Date('2026-09-01T00:00:00.000Z'),
      userId: OWNER,
    };

    it('reads the document under the visitor’s share document scope', async () => {
      mockDocumentFindById.mockResolvedValue(document);
      const caller = await createCaller();

      await expect(
        caller.getDocument({ documentId: 'doc-1', shareId: 'share-1', topicId: 'tpc_visitor' }),
      ).resolves.toEqual({
        content: '# Notes',
        fileType: 'text/markdown',
        id: 'doc-1',
        title: 'Notes',
        updatedAt: document.updatedAt,
      });
      // Creator-scoped model + the share scope pinned to this visitor topic.
      expect(DocumentModelMock).toHaveBeenCalledWith(
        expect.anything(),
        OWNER,
        undefined,
        undefined,
        { shareId: 'share-1', topicId: 'tpc_visitor', type: 'agentShare', visitorUserId: VISITOR },
      );
      expect(mockDocumentFindById).toHaveBeenCalledWith('doc-1');
    });

    it('404s when the scoped lookup misses (creator or other-visitor document)', async () => {
      mockDocumentFindById.mockResolvedValue(undefined);
      const caller = await createCaller();

      await expect(
        caller.getDocument({ documentId: 'doc-1', shareId: 'share-1', topicId: 'tpc_visitor' }),
      ).rejects.toMatchObject({ code: 'NOT_FOUND' });
    });

    it('rejects a topic that is not the visitor’s own', async () => {
      mockFindById.mockResolvedValue({ ...visitorTopic, senderId: 'someone-else' });
      const caller = await createCaller();

      await expect(
        caller.getDocument({ documentId: 'doc-1', shareId: 'share-1', topicId: 'tpc_visitor' }),
      ).rejects.toMatchObject({ code: 'NOT_FOUND' });
      expect(mockDocumentFindById).not.toHaveBeenCalled();
    });
  });

  describe('issueGatewayUserToken', () => {
    it('signs the per-user hub token for the VISITOR, never the creator', async () => {
      const caller = await createCaller();

      await expect(caller.issueGatewayUserToken({ shareId: 'share-1' })).resolves.toEqual({
        token: 'visitor-jwt',
      });
      expect(mockSignUserJWT).toHaveBeenCalledWith(VISITOR);
      expect(mockAccessCheck).toHaveBeenCalledWith(expect.anything(), 'share-1', VISITOR);
    });

    // The v2 hub socket is per user, not per operation: the hub authorizes
    // each `subscribe` against the op's registered owner, so minting must not
    // depend on a topic marker or a live run (a visitor opens the socket
    // before their first send).
    it('does not require a topic or a running operation', async () => {
      mockFindById.mockResolvedValue(undefined);
      mockIsRunningOperationAlive.mockResolvedValue(false);
      const caller = await createCaller();

      await expect(caller.issueGatewayUserToken({ shareId: 'share-1' })).resolves.toEqual({
        token: 'visitor-jwt',
      });
      expect(mockFindById).not.toHaveBeenCalled();
      expect(mockIsRunningOperationAlive).not.toHaveBeenCalled();
    });

    it('rejects an unknown share without signing anything', async () => {
      mockAccessCheck.mockRejectedValue(
        new TRPCError({ code: 'NOT_FOUND', message: 'Share not found' }),
      );
      const caller = await createCaller();

      await expect(caller.issueGatewayUserToken({ shareId: 'missing' })).rejects.toMatchObject({
        code: 'NOT_FOUND',
      });
      expect(mockSignUserJWT).not.toHaveBeenCalled();
    });

    it('rejects a share that is not link-visible', async () => {
      mockAccessCheck.mockResolvedValue({ ...share, visibility: 'private' });
      const caller = await createCaller();

      await expect(caller.issueGatewayUserToken({ shareId: 'share-1' })).rejects.toMatchObject({
        code: 'FORBIDDEN',
      });
      expect(mockSignUserJWT).not.toHaveBeenCalled();
    });
  });

  describe('refreshGatewayToken', () => {
    it('signs the token for the VISITOR, never the creator', async () => {
      const caller = await createCaller();

      await expect(
        caller.refreshGatewayToken({ shareId: 'share-1', topicId: 'tpc_visitor' }),
      ).resolves.toEqual({ token: 'visitor-jwt' });
      expect(mockSignUserJWT).toHaveBeenCalledWith(VISITOR);
    });

    it('rejects when the topic has no running operation', async () => {
      mockFindById.mockResolvedValue({ ...visitorTopic, metadata: {} });
      const caller = await createCaller();

      await expect(
        caller.refreshGatewayToken({ shareId: 'share-1', topicId: 'tpc_visitor' }),
      ).rejects.toMatchObject({ code: 'NOT_FOUND' });
      expect(mockSignUserJWT).not.toHaveBeenCalled();
    });

    // The marker is cleared best-effort at finish, so a stale one must not
    // send the visitor's browser to reconnect to a finished run (it would
    // register the topic as "running" locally and freeze its message list).
    it('rejects when the marker points at a run that already ended', async () => {
      mockIsRunningOperationAlive.mockResolvedValue(false);
      const caller = await createCaller();

      await expect(
        caller.refreshGatewayToken({ shareId: 'share-1', topicId: 'tpc_visitor' }),
      ).rejects.toMatchObject({ code: 'NOT_FOUND' });
      expect(mockIsRunningOperationAlive).toHaveBeenCalledWith(
        expect.anything(),
        visitorTopic.metadata.runningOperation,
      );
      expect(mockSignUserJWT).not.toHaveBeenCalled();
    });
  });

  it('requires authentication', async () => {
    const caller = shareChatRouter.createCaller(await createContextInner());

    await expect(caller.getTopics({ shareId: 'share-1' })).rejects.toMatchObject({
      code: 'UNAUTHORIZED',
    });
  });

  describe('visitor capability', () => {
    it('rejects on a deployment without business features, before any share lookup', async () => {
      mocks.businessConst.ENABLE_BUSINESS_FEATURES = false;
      const caller = await createCaller();

      await expect(caller.getTopics({ shareId: 'share-1' })).rejects.toMatchObject({
        code: 'FORBIDDEN',
      });
      expect(mockAccessCheck).not.toHaveBeenCalled();
    });

    it.each([false, undefined])(
      'admits visitor procedures when the agent share flag is %s',
      async (enableAgentShare) => {
        mockGetFeatureFlagsState.mockResolvedValue({ enableAgentShare });
        mockMessageQueryForVisitor.mockResolvedValue([]);
        const caller = await createCaller();

        await expect(caller.getTopics({ shareId: 'share-1' })).resolves.toEqual([]);
        await expect(
          caller.getMessages({ shareId: 'share-1', topicId: 'tpc_visitor' }),
        ).resolves.toEqual([]);
        await expect(caller.execAgent({ prompt: 'hi', shareId: 'share-1' })).resolves.toMatchObject(
          {
            success: true,
          },
        );
        await expect(
          caller.interruptTask({ operationId: 'op-1', shareId: 'share-1', topicId: 'tpc_visitor' }),
        ).resolves.toMatchObject({ success: true });
        await expect(
          caller.refreshGatewayToken({ shareId: 'share-1', topicId: 'tpc_visitor' }),
        ).resolves.toEqual({ token: 'visitor-jwt' });
        await expect(caller.issueGatewayUserToken({ shareId: 'share-1' })).resolves.toEqual({
          token: 'visitor-jwt',
        });
        expect(mockGetFeatureFlagsState).not.toHaveBeenCalled();
      },
    );
  });
});