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.

15.0 KB
Raw Blame
// @vitest-environment node
import { REVIEW_PREDICT_PROMPT_VERSION } from '@lobechat/prompts';
import sharp from 'sharp';
import { beforeEach, describe, expect, it, vi } from 'vitest';

import type { LobeChatDatabase } from '@/database/type';

import { GATE_REVIEW_MAX_VISUALS, VerifyReviewPredictorService } from '../reviewPredictor';

const mocks = vi.hoisted(() => ({
  result: vi.fn(),
  evidence: vi.fn(),
  existing: vi.fn(),
  upsert: vi.fn(),
  generate: vi.fn(),
  document: vi.fn(),
  file: vi.fn(),
  content: vi.fn(),
  url: vi.fn(),
  bytes: vi.fn(),
}));
vi.mock('@/database/models/verifyCheckResult', () => ({
  VerifyCheckResultModel: vi.fn(function () {
    return { findById: mocks.result };
  }),
}));
vi.mock('@/database/models/verifyEvidence', () => ({
  VerifyEvidenceModel: vi.fn(function () {
    return { listByCheckResult: mocks.evidence };
  }),
}));
vi.mock('@/database/models/verifyReviewPrediction', () => ({
  VerifyReviewPredictionModel: vi.fn(function () {
    return {
      findAdjudicated: mocks.existing,
      upsert: mocks.upsert,
    };
  }),
}));
vi.mock('@/database/models/document', () => ({
  DocumentModel: vi.fn(function () {
    return { findById: mocks.document };
  }),
}));
vi.mock('@/database/models/file', () => ({
  FileModel: vi.fn(function () {
    return { findById: mocks.file };
  }),
}));
vi.mock('@/server/services/file', () => ({
  FileService: vi.fn(function () {
    return {
      getFileAccessUrl: mocks.url,
      getFileByteArray: mocks.bytes,
      getFileContent: mocks.content,
    };
  }),
}));
vi.mock('@/server/services/aiGeneration', () => ({
  AiGenerationService: vi.fn(function () {
    return { generateObject: mocks.generate };
  }),
}));

beforeEach(() => {
  Object.values(mocks).forEach((mock) => mock.mockReset());
  mocks.result.mockResolvedValue({ id: 'r1', checkItemTitle: 'Table total', verdict: 'passed' });
  mocks.evidence.mockResolvedValue([{ id: 'e1', type: 'text', content: 'SUM(A1:A3) = 42' }]);
  mocks.generate.mockResolvedValue({
    action: 'reject',
    comment: 'Expected 43, got 42',
    regions: [],
  });
  mocks.upsert.mockImplementation(async (row) => ({ id: 'p1', ...row }));
});
const params = {
  checkResultId: 'r1',
  modelConfig: { model: 'review-model', provider: 'review-provider' },
};
const review = () => new VerifyReviewPredictorService({} as LobeChatDatabase, 'u1');

describe('Goal review evidence', () => {
  it('uses original text and records generation provenance', async () => {
    expect(await review().predict({ ...params, includeTextEvidence: true })).toMatchObject({
      action: 'reject',
      status: 'judged',
    });
    expect(JSON.stringify(mocks.generate.mock.calls[0][0].messages)).toContain('SUM(A1:A3) = 42');
    expect(mocks.generate.mock.calls[0][1].tracing).toMatchObject({
      promptVersion: REVIEW_PREDICT_PROMPT_VERSION,
      scenario: expect.any(String),
      schemaName: expect.any(String),
    });
    expect(mocks.upsert).toHaveBeenCalledWith(
      expect.objectContaining({
        model: 'review-model',
        provider: 'review-provider',
        promptVersion: REVIEW_PREDICT_PROMPT_VERSION,
      }),
    );
  });
  it('preserves the ordinary visual-only review behavior', async () => {
    expect(await review().predict(params)).toMatchObject({ status: 'skipped' });
    expect(mocks.generate).not.toHaveBeenCalled();
  });
  it('reads attached text files rather than treating paths as proof', async () => {
    mocks.evidence.mockResolvedValue([{ id: 'e1', type: 'text', fileId: 'f1' }]);
    mocks.file.mockResolvedValue({ id: 'f1', size: 10, url: 'evidence-key' });
    mocks.content.mockResolvedValue('Actual command output: 42');
    await review().predict({ ...params, includeTextEvidence: true });
    expect(JSON.stringify(mocks.generate.mock.calls[0][0].messages)).toContain(
      'Actual command output: 42',
    );
  });
  /**
   * Regression: the budget was spent in capture order, so an oversized row
   * consumed it and every later row was dropped without a trace. A Goal
   * delivery lost its own summary that way and was rejected for "missing
   * evidence" it had attached.
   */
  it('keeps a later small evidence row when an earlier attachment is oversized', async () => {
    mocks.evidence.mockResolvedValue([
      { content: 'x'.repeat(70_000), id: 'e1', type: 'text' },
      { content: 'RERUN: all five commands exited 0', id: 'e2', type: 'text' },
    ]);
    await review().predict({ ...params, includeTextEvidence: true });
    expect(JSON.stringify(mocks.generate.mock.calls[0][0].messages)).toContain(
      'RERUN: all five commands exited 0',
    );
  });

  /**
   * Regression: the caption a builder wrote on a text row was dropped, leaving
   * the reviewer to infer what a raw payload was from its bytes. Visual rows had
   * always carried their description.
   */
  it('shows the caption the builder wrote on each text row', async () => {
    mocks.evidence.mockResolvedValue([
      {
        content: 'exit=0',
        description: 'rerun log, byte-identical to round 1',
        id: 'e1',
        type: 'text',
      },
    ]);
    await review().predict({ ...params, includeTextEvidence: true });
    expect(JSON.stringify(mocks.generate.mock.calls[0][0].messages)).toContain(
      'rerun log, byte-identical to round 1',
    );
  });

  /**
   * Regression: frames past the cap were dropped silently, so the model judged a
   * five-frame check on three frames believing that was all of it — and its
   * rejection cited evidence that had been withheld from the request.
   */
  it('tells the reviewer which frames the request held back, and records it', async () => {
    mocks.evidence.mockResolvedValue([
      ...['s1', 's2', 's3', 's4'].map((id) => ({ fileId: id, id, type: 'screenshot' })),
      { content: 'exit=0', id: 't1', type: 'text' },
    ]);
    mocks.file.mockImplementation(async (id: string) => ({ id, size: 10, url: `key-${id}` }));
    mocks.url.mockImplementation(async ({ id }: { id: string }) => `https://x/${id}`);

    const prediction = await review().predict({ ...params, includeTextEvidence: true });
    const payload = JSON.stringify(mocks.generate.mock.calls[0][0].messages);
    expect(payload).toContain('Withheld from this request');
    expect(payload).toContain('1 more frame(s)');
    // The caveat rides on the judged row: "rejected blind" and "rejected having
    // seen everything" are otherwise the same verdict in the agreement stats.
    expect(prediction?.statusReason).toContain('1 more frame(s)');
  });

  /**
   * Regression: the Goal review attached only the first three frames. A check
   * carrying four header shots, a close recording and a tooltip shot came back
   * "unjudgeable — open the hidden LocalFile screenshot, GIF and tooltip", and a
   * delivery whose verifiers had all passed was parked on a person.
   */
  it('attaches every frame of an ordinary check when the review gates the Task', async () => {
    const ids = ['s1', 's2', 's3', 's4', 's5', 'g1', 's6'];
    mocks.evidence.mockResolvedValue(
      ids.map((id) => ({ fileId: id, id, type: id.startsWith('g') ? 'gif' : 'screenshot' })),
    );
    mocks.file.mockImplementation(async (id: string) => ({ id, size: 10, url: `key-${id}` }));
    mocks.url.mockImplementation(async ({ id }: { id: string }) => `https://x/${id}`);

    const prediction = await review().predict({
      ...params,
      includeTextEvidence: true,
      maxVisuals: GATE_REVIEW_MAX_VISUALS,
    });
    const payload = JSON.stringify(mocks.generate.mock.calls[0][0].messages);
    for (const id of ids) expect(payload).toContain(`https://x/${id}`);
    expect(prediction?.statusReason).toBeUndefined();
  });

  /**
   * Regression: frames went to the provider as links it had to download. A
   * provider that could not reach the storage behind them (a bucket on
   * localhost or a private network) failed every review of the check with
   * "Error while downloading file", and a Goal Task spent its whole attempt
   * budget re-delivering into a review that could never open its screenshot.
   */
  it('sends a stored frame inline instead of a link the provider must fetch', async () => {
    mocks.evidence.mockResolvedValue([{ fileId: 'f1', id: 's1', type: 'screenshot' }]);
    mocks.file.mockResolvedValue({ fileType: 'image/png', id: 'f1', size: 4, url: 'key-f1' });
    mocks.bytes.mockResolvedValue(new Uint8Array([0x89, 0x50, 0x4e, 0x47]));

    await review().predict(params);
    const payload = JSON.stringify(mocks.generate.mock.calls[0][0].messages);
    expect(payload).toContain('data:image/png;base64,iVBORw==');
    expect(mocks.url).not.toHaveBeenCalled();
  });

  /**
   * Regression: the inline ceiling was per frame only, so a gate review carrying
   * twelve large frames could build a request body far past provider limits.
   */
  it('keeps regions drawn on a GIF frame sheet off the original animation', async () => {
    mocks.evidence.mockResolvedValue([
      { fileId: 'f-gif', id: 'g1', type: 'gif' },
      { fileId: 'f-png', id: 's1', type: 'screenshot' },
    ]);
    const frames = await Promise.all(
      ['#ff0000', '#0000ff'].map((background) =>
        sharp({ create: { background, channels: 3, height: 8, width: 8 } })
          .png()
          .toBuffer(),
      ),
    );
    const gif = await sharp(frames, { join: { animated: true } })
      .gif()
      .toBuffer();
    const png = new Uint8Array([0x89, 0x50, 0x4e, 0x47]);
    mocks.file.mockImplementation(async (id: string) =>
      id === 'f-gif'
        ? { fileType: 'image/gif', id, size: gif.length, url: 'key-gif' }
        : { fileType: 'image/png', id, size: png.length, url: 'key-png' },
    );
    mocks.bytes.mockImplementation(async (url: string) =>
      url === 'key-gif' ? new Uint8Array(gif) : png,
    );
    const box = { height: 0.2, width: 0.2, x: 0.1, y: 0.1 };
    mocks.generate.mockResolvedValue({
      action: 'reject',
      comment: 'Spinner in frame 3',
      regions: [
        { ...box, comment: 'on the sheet', imageIndex: 0 },
        { ...box, comment: 'on the screenshot', imageIndex: 1 },
      ],
    });

    await review().predict(params);

    const payload = JSON.stringify(mocks.generate.mock.calls[0][0].messages);
    expect(payload).not.toContain('data:image/gif');
    expect(payload).toContain('frames in playback order');
    expect(mocks.upsert).toHaveBeenCalledWith(
      expect.objectContaining({
        annotations: [{ comment: 'on the screenshot', evidenceId: 's1', rect: box }],
        comment: 'Spinner in frame 3',
      }),
    );
  });

  it('still sends a GIF as a sheet after the inline budget is spent', async () => {
    const big = 5 * 1024 * 1024;
    const frames = await Promise.all(
      ['#ff0000', '#0000ff'].map((background) =>
        sharp({ create: { background, channels: 3, height: 8, width: 8 } })
          .png()
          .toBuffer(),
      ),
    );
    const gif = await sharp(frames, { join: { animated: true } })
      .gif()
      .toBuffer();
    // Three 5 MiB screenshots spend the whole 15 MiB budget before the GIF.
    mocks.evidence.mockResolvedValue([
      ...['s1', 's2', 's3'].map((id) => ({ fileId: id, id, type: 'screenshot' })),
      { fileId: 'f-gif', id: 'g1', type: 'gif' },
    ]);
    mocks.file.mockImplementation(async (id: string) =>
      id === 'f-gif'
        ? { fileType: 'image/gif', id, size: gif.length, url: 'key-gif' }
        : { fileType: 'image/png', id, size: big, url: `key-${id}` },
    );
    mocks.bytes.mockImplementation(async (url: string) =>
      url === 'key-gif' ? new Uint8Array(gif) : new Uint8Array([0x89, 0x50, 0x4e, 0x47]),
    );
    mocks.url.mockImplementation(async ({ id }: { id: string }) => `https://x/${id}`);

    await review().predict({ ...params, maxVisuals: GATE_REVIEW_MAX_VISUALS });

    const payload = JSON.stringify(mocks.generate.mock.calls[0][0].messages);
    // The sheet is budgeted first, so the last screenshot is the one linked.
    expect(payload).toContain('https://x/s3');
    expect(payload).not.toContain('https://x/f-gif');
    expect(payload).toContain('frames in playback order');
  });

  it('links the frames past the per-request inline budget', async () => {
    const size = 4 * 1024 * 1024;
    const ids = ['s1', 's2', 's3', 's4', 's5'];
    mocks.evidence.mockResolvedValue(ids.map((id) => ({ fileId: id, id, type: 'screenshot' })));
    mocks.file.mockImplementation(async (id: string) => ({
      fileType: 'image/png',
      id,
      size,
      url: `key-${id}`,
    }));
    mocks.bytes.mockResolvedValue(new Uint8Array([0x89, 0x50, 0x4e, 0x47]));
    mocks.url.mockImplementation(async ({ id }: { id: string }) => `https://x/${id}`);

    await review().predict({ ...params, maxVisuals: GATE_REVIEW_MAX_VISUALS });
    // 15 MiB budget: three 4 MiB frames inline, the fourth and fifth are linked.
    expect(mocks.bytes).toHaveBeenCalledTimes(3);
    const payload = JSON.stringify(mocks.generate.mock.calls[0][0].messages);
    expect(payload).not.toContain('https://x/s3');
    expect(payload).toContain('https://x/s4');
    expect(payload).toContain('https://x/s5');
  });

  it('links a frame too large to send inline', async () => {
    mocks.evidence.mockResolvedValue([{ fileId: 'f1', id: 's1', type: 'screenshot' }]);
    mocks.file.mockResolvedValue({
      fileType: 'image/png',
      id: 'f1',
      size: 50 * 1024 * 1024,
      url: 'key-f1',
    });
    mocks.url.mockResolvedValue('https://x/f1');

    await review().predict(params);
    expect(JSON.stringify(mocks.generate.mock.calls[0][0].messages)).toContain('https://x/f1');
    expect(mocks.bytes).not.toHaveBeenCalled();
  });

  it('leaves no caveat on a check whose evidence was fully shown', async () => {
    const prediction = await review().predict({ ...params, includeTextEvidence: true });
    expect(prediction?.statusReason).toBeUndefined();
  });

  it.each([
    [new Error('Connection timed out'), 'Connection timed out'],
    [{ message: 'Rate limit exceeded' }, 'Rate limit exceeded'],
    [{ error: { message: 'Invalid API key' }, errorType: 'ProviderBizError' }, 'Invalid API key'],
    ['Service unavailable', 'Service unavailable'],
    [
      { errorType: 'InvalidProviderAPIKey', request: { apiKey: 'private-key' } },
      'Review model review-provider/review-model could not run (InvalidProviderAPIKey). Check the provider configuration and retry the review.',
    ],
    [
      { unexpected: true },
      'Review model review-provider/review-model could not run. Check the provider configuration and retry the review.',
    ],
  ])('preserves an actionable reason for provider failure %#', async (error, reason) => {
    mocks.generate.mockRejectedValue(error);
    const prediction = await review().predict({ ...params, includeTextEvidence: true });
    expect(prediction).toMatchObject({ status: 'errored', statusReason: reason });
    expect(prediction?.statusReason).not.toContain('[object Object]');
    expect(prediction?.statusReason).not.toContain('private-key');
  });
  it('never turns malformed model output into approval', async () => {
    mocks.generate.mockResolvedValue({ action: 'maybe' });
    expect(await review().predict({ ...params, includeTextEvidence: true })).toMatchObject({
      status: 'errored',
    });
  });
});