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.

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

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

import type * as AcceptanceServiceModule from '../acceptanceService';
import { reviewGoalDelivery } from '../goalReview';

const mocks = vi.hoisted(() => ({
  goal: vi.fn(),
  run: vi.fn(),
  metadata: vi.fn(),
  acceptance: vi.fn(),
  rounds: vi.fn(),
  predict: vi.fn(),
  reviewModel: vi.fn(),
}));
vi.mock('../goalReviewModelConfig', () => ({ resolveGoalReviewModelConfig: mocks.reviewModel }));
vi.mock('@/database/models/verifyEvidence', () => ({
  VerifyEvidenceModel: vi.fn(function () {
    return { listByCheckResult: vi.fn().mockResolvedValue([]) };
  }),
}));
vi.mock('@/database/models/goal', () => ({
  GoalModel: vi.fn(function () {
    return { findByGraphTask: mocks.goal };
  }),
}));
vi.mock('@/database/models/verifyRun', () => ({
  VerifyRunModel: vi.fn(function () {
    return { findByOperation: mocks.run, setMetadata: mocks.metadata };
  }),
}));
vi.mock('../acceptanceService', async (original) => ({
  ...(await original<typeof AcceptanceServiceModule>()),
  AcceptanceService: vi.fn(function () {
    return {
      acceptanceModel: { findById: mocks.acceptance },
      loadRounds: mocks.rounds,
    };
  }),
}));
vi.mock('../reviewPredictor', () => ({
  GATE_REVIEW_MAX_VISUALS: 12,
  REVIEW_PREDICT_CONCURRENCY: 4,
  VerifyReviewPredictorService: vi.fn(function () {
    return { predict: mocks.predict };
  }),
}));
const db = {} as LobeChatDatabase;
const check = {
  id: 'c1',
  title: 'Document contents',
  required: true,
  verifierType: 'agent',
  verifierConfig: {},
  index: 0,
};
const result = {
  id: 'result1',
  checkItemId: 'c1',
  verifyRunId: 'r1',
  status: 'passed',
  verdict: 'passed',
  required: true,
};

beforeEach(() => {
  vi.clearAllMocks();
  mocks.reviewModel.mockResolvedValue({
    model: 'configured-model',
    provider: 'configured-provider',
  });
  mocks.goal.mockResolvedValue({ id: 'goal1' });
  mocks.run.mockResolvedValue({
    id: 'r1',
    acceptanceId: 'a1',
    metadata: { taskDrivenAt: 'claimed', maxRepairRounds: 2 },
  });
  mocks.acceptance.mockResolvedValue({ id: 'a1', requirement: 'Deliver an editable document' });
  mocks.rounds.mockResolvedValue({
    runs: [{ id: 'r1', roundIndex: 1, plan: [check] }],
    results: [result],
  });
  mocks.predict.mockResolvedValue({ id: 'p1', status: 'judged', action: 'accept' });
});

describe('Goal automatic Acceptance review', () => {
  it('does not automatically review ordinary tasks', async () => {
    mocks.goal.mockResolvedValue(null);
    expect(await reviewGoalDelivery(db, 'u1', 't1', 'op1')).toBeUndefined();
    expect(mocks.predict).not.toHaveBeenCalled();
  });

  it('records an AI acceptance without writing a human decision', async () => {
    expect(await reviewGoalDelivery(db, 'u1', 't1', 'op1')).toMatchObject({
      status: 'passed',
      predictionIds: ['p1'],
    });
    expect(mocks.metadata).toHaveBeenCalledWith('r1', {
      taskDrivenAt: 'claimed',
      maxRepairRounds: 2,
      goalReview: { status: 'passed', predictionIds: ['p1'], feedback: '' },
    });
    expect(mocks.predict).toHaveBeenCalledWith(
      expect.objectContaining({
        checkResultId: 'result1',
        includeTextEvidence: true,
        // The gate must not judge on the shadow lane's three-frame sample.
        maxVisuals: 12,
      }),
    );
  });

  /**
   * Regression: an "I cannot decide this from evidence" verdict was folded into
   * `rejected`, which told the builder to fix nothing and sent the Task round
   * again against the same unprovable criterion until the attempt budget ran out.
   */
  it('keeps an undecidable criterion out of the rejected bucket', async () => {
    mocks.predict.mockResolvedValue({
      id: 'p1',
      status: 'judged',
      action: 'unjudgeable',
      comment: 'The check asks the reviewer to rerun the scripts; a reader cannot do that.',
    });
    expect(await reviewGoalDelivery(db, 'u1', 't1', 'op1')).toMatchObject({
      status: 'unjudgeable',
      feedback:
        'Document contents: The check asks the reviewer to rerun the scripts; a reader cannot do that.',
    });
  });

  it('still reports a rejection when one check is short and another is undecidable', async () => {
    const second = { ...check, id: 'c2', index: 1, title: 'Chart rendered' };
    mocks.rounds.mockResolvedValue({
      runs: [{ id: 'r1', roundIndex: 1, plan: [check, second] }],
      results: [result, { ...result, id: 'result2', checkItemId: 'c2' }],
    });
    mocks.predict.mockImplementation(async ({ checkResultId }: { checkResultId: string }) =>
      checkResultId === 'result1'
        ? { id: 'p1', status: 'judged', action: 'unjudgeable', comment: 'Needs execution.' }
        : { id: 'p2', status: 'judged', action: 'reject', comment: 'The chart is blank.' },
    );
    // A genuinely short check makes another attempt worth paying for, so the
    // blocking outcome wins regardless of which check settled first.
    expect(await reviewGoalDelivery(db, 'u1', 't1', 'op1')).toMatchObject({ status: 'rejected' });
  });

  it('turns a rejection proposal into actionable feedback even when Verify passed', async () => {
    mocks.predict.mockResolvedValue({
      id: 'p1',
      status: 'judged',
      action: 'reject',
      comment: 'The exported file is missing the table.',
    });
    expect(await reviewGoalDelivery(db, 'u1', 't1', 'op1')).toMatchObject({
      status: 'rejected',
      feedback: 'Document contents: The exported file is missing the table.',
    });
  });

  it('names the rejected checks of the reviewed round so they can be repaired in it', async () => {
    mocks.predict.mockResolvedValue({
      id: 'p1',
      status: 'judged',
      action: 'reject',
      comment: 'Each chapter needs a second screenshot.',
    });
    expect(await reviewGoalDelivery(db, 'u1', 't1', 'op1')).toMatchObject({
      rejectedChecks: [{ checkItemId: 'c1', feedback: 'Each chapter needs a second screenshot.' }],
    });
  });

  it('does not offer an earlier round’s result for an in-round repair', async () => {
    mocks.run.mockResolvedValue({ id: 'r2', acceptanceId: 'a1', metadata: {} });
    mocks.rounds.mockResolvedValue({
      runs: [
        { id: 'r1', roundIndex: 1, plan: [check] },
        { id: 'r2', roundIndex: 2, plan: [{ ...check, id: 'c2' }] },
      ],
      results: [result, { ...result, id: 'result2', checkItemId: 'c2', verifyRunId: 'r2' }],
    });
    mocks.predict.mockImplementation(async ({ checkResultId }: { checkResultId: string }) =>
      checkResultId === 'result1'
        ? { id: 'p1', status: 'judged', action: 'reject', comment: 'Old evidence is short.' }
        : { id: 'p2', status: 'judged', action: 'accept' },
    );
    const review = await reviewGoalDelivery(db, 'u1', 't1', 'op1');
    expect(review?.status).toBe('rejected');
    expect(review?.rejectedChecks).toBeUndefined();
  });

  it('reviews supplementary checks across rounds, not only the bound run', async () => {
    mocks.run.mockResolvedValue({ id: 'r2', acceptanceId: 'a1', metadata: {} });
    mocks.rounds.mockResolvedValue({
      runs: [
        { id: 'r1', roundIndex: 1, plan: [check] },
        { id: 'r2', roundIndex: 2, plan: [{ ...check, id: 'c2' }] },
      ],
      results: [
        result,
        { ...result, id: 'result2', checkItemId: 'c2', verifyRunId: 'r2', verdict: 'uncertain' },
      ],
    });
    mocks.predict
      .mockResolvedValueOnce({ id: 'p1', status: 'judged', action: 'accept' })
      .mockResolvedValueOnce({
        id: 'p2',
        status: 'judged',
        action: 'reject',
        comment: 'Supply evidence.',
      });
    expect(await reviewGoalDelivery(db, 'u1', 't1', 'op1')).toMatchObject({
      status: 'rejected',
      predictionIds: ['p1', 'p2'],
    });
  });

  it('ignores a result filed under an id that no round ever planned', async () => {
    mocks.run.mockResolvedValue({ id: 'r2', acceptanceId: 'a1', metadata: {} });
    mocks.rounds.mockResolvedValue({
      runs: [
        { id: 'r1', roundIndex: 1, plan: [check] },
        { id: 'r2', roundIndex: 2, plan: [check] },
      ],
      results: [
        { ...result, id: 'orphan', checkItemId: 'pglite-classification', verifyRunId: 'r1' },
        { ...result, id: 'result2', verifyRunId: 'r2' },
      ],
    });
    mocks.predict.mockImplementation(async ({ checkResultId }: { checkResultId: string }) =>
      checkResultId === 'orphan'
        ? { id: 'p-orphan', status: 'judged', action: 'reject', comment: 'Stale evidence.' }
        : { id: 'p1', status: 'judged', action: 'accept' },
    );

    expect(await reviewGoalDelivery(db, 'u1', 't1', 'op1')).toMatchObject({
      status: 'passed',
      predictionIds: ['p1'],
    });
    expect(mocks.predict).toHaveBeenCalledTimes(1);
  });

  it('ignores an off-plan result whose id collides with a planned sourceCriterionId', async () => {
    mocks.rounds.mockResolvedValue({
      runs: [{ id: 'r1', roundIndex: 1, plan: [{ ...check, sourceCriterionId: 'criterion-1' }] }],
      results: [result, { ...result, id: 'orphan', checkItemId: 'criterion-1' }],
    });
    mocks.predict.mockImplementation(async ({ checkResultId }: { checkResultId: string }) =>
      checkResultId === 'orphan'
        ? { id: 'p-orphan', status: 'judged', action: 'reject', comment: 'Stale evidence.' }
        : { id: 'p1', status: 'judged', action: 'accept' },
    );

    expect(await reviewGoalDelivery(db, 'u1', 't1', 'op1')).toMatchObject({
      status: 'passed',
      predictionIds: ['p1'],
    });
    expect(mocks.predict).toHaveBeenCalledWith(
      expect.objectContaining({ checkResultId: 'result1' }),
    );
    expect(mocks.predict).not.toHaveBeenCalledWith(
      expect.objectContaining({ checkResultId: 'orphan' }),
    );
  });

  it('does not fall back to a required orphan when every planned check is optional', async () => {
    mocks.rounds.mockResolvedValue({
      runs: [{ id: 'r1', roundIndex: 1, plan: [{ ...check, required: false }] }],
      results: [
        { ...result, required: false },
        { ...result, id: 'orphan', checkItemId: 'pglite-classification' },
      ],
    });

    expect(await reviewGoalDelivery(db, 'u1', 't1', 'op1')).toMatchObject({
      status: 'errored',
      feedback: expect.stringContaining('Goal Acceptance has no required checks'),
    });
    expect(mocks.predict).not.toHaveBeenCalled();
  });

  it('sends missing evidence back and does not mistake skipped review for approval', async () => {
    mocks.predict.mockResolvedValue({ id: 'p1', status: 'skipped', statusReason: 'no evidence' });
    expect(await reviewGoalDelivery(db, 'u1', 't1', 'op1')).toMatchObject({ status: 'rejected' });
  });

  it('uses the resolved reviewer instead of requiring the Google pin', async () => {
    await reviewGoalDelivery(db, 'u1', 't1', 'op1');
    expect(mocks.predict.mock.calls[0][0].modelConfig).toEqual({
      model: 'configured-model',
      provider: 'configured-provider',
    });
  });

  it('holds with configuration guidance when no reviewer is available', async () => {
    mocks.reviewModel.mockResolvedValue(undefined);
    const review = await reviewGoalDelivery(db, 'u1', 't1', 'op1');
    expect(review?.status).toBe('errored');
    expect(review?.feedback).toContain('Configure an available model');
    expect(mocks.predict).not.toHaveBeenCalled();
  });

  it('does not require a model to honor an existing human approval', async () => {
    mocks.rounds.mockResolvedValue({
      runs: [{ id: 'r1', roundIndex: 1, plan: [check] }],
      results: [{ ...result, userDecision: 'accepted' }],
    });
    mocks.reviewModel.mockResolvedValue(undefined);
    expect((await reviewGoalDelivery(db, 'u1', 't1', 'op1'))?.status).toBe('passed');
    expect(mocks.reviewModel).not.toHaveBeenCalled();
  });

  it('holds on a reviewer error instead of approving or requesting a product fix', async () => {
    mocks.predict.mockResolvedValue({
      id: 'p1',
      status: 'errored',
      statusReason: 'provider unavailable',
    });
    expect(await reviewGoalDelivery(db, 'u1', 't1', 'op1')).toMatchObject({ status: 'errored' });
    // Retried once before giving up.
    expect(mocks.predict).toHaveBeenCalledTimes(2);
  });

  /**
   * Regression: every exception in here produced one canned sentence telling the
   * reader to configure a review model. A round that was never linked to its
   * Acceptance therefore escalated to a person as a model-configuration problem,
   * hiding the only fact that could have unblocked it.
   */
  it('carries the real reason when the review cannot even start', async () => {
    mocks.run.mockResolvedValue({ id: 'r1', acceptanceId: null, metadata: {} });

    const review = await reviewGoalDelivery(db, 'u1', 't1', 'op1');

    expect(review?.status).toBe('errored');
    expect(review?.feedback).toContain('Goal delivery has no Acceptance');
    expect(review?.feedback).not.toContain('Configure an available model');
  });

  /**
   * Regression: an unconfirmed draft left in the round chain — abandoned, or one a
   * CLI-driven verification was appended past — contributed result-less required
   * checks to the union, each rejected as missing evidence, so a delivery that
   * passed every confirmed check was sent back anyway.
   */
  it('judges only confirmed rounds, not a draft left in the chain', async () => {
    mocks.rounds.mockResolvedValue({
      runs: [
        {
          id: 'draft',
          plan: [{ ...check, id: 'draft-only', title: 'Never confirmed' }],
          planConfirmedAt: null,
          roundIndex: 1,
          status: 'planned',
          userDecision: null,
        },
        { id: 'r1', plan: [check], planConfirmedAt: new Date(), roundIndex: 2, status: 'passed' },
      ],
      results: [result],
    });

    expect(await reviewGoalDelivery(db, 'u1', 't1', 'op1')).toMatchObject({ status: 'passed' });
    expect(mocks.predict).toHaveBeenCalledTimes(1);
  });

  /**
   * A concurrent attempt opened its round while this delivery was being judged.
   * Task rounds confirm their plan when they open, so the empty newer round
   * planned every check and flagged this round's evidence as carried forward —
   * a passing delivery was sent back for "current evidence" it had just filed.
   */
  it('ignores a round opened after the one under review', async () => {
    mocks.rounds.mockResolvedValue({
      runs: [
        { id: 'r1', plan: [check], planConfirmedAt: new Date(), roundIndex: 1, status: 'passed' },
        { id: 'r2', plan: [check], planConfirmedAt: new Date(), roundIndex: 2, status: 'running' },
      ],
      results: [result],
    });

    expect(await reviewGoalDelivery(db, 'u1', 't1', 'op1')).toMatchObject({ status: 'passed' });
    expect(mocks.predict).toHaveBeenCalledTimes(1);
  });

  /**
   * The feedback is persisted on the run and quoted into the escalation, so an
   * unexpected backend failure must not carry SQL, identifiers or provider
   * diagnostics out of the server log.
   */
  it('keeps an unexpected backend failure out of the feedback', async () => {
    mocks.rounds.mockRejectedValue(
      new Error('select "verify_runs"."id" from ... — connection terminated'),
    );

    const review = await reviewGoalDelivery(db, 'u1', 't1', 'op1');

    expect(review?.status).toBe('errored');
    expect(review?.feedback).toContain('internal error');
    expect(review?.feedback).not.toContain('verify_runs');
  });

  /**
   * Regression: a review that could not run on one check was retried by
   * rerunning the whole review. Every other check was re-asked and its opinion
   * upserted over the first one, so a nondeterministic second pass could turn a
   * rejection into an acceptance and let the delivery complete.
   */
  it('retries only the check whose review could not run, keeping the other verdicts', async () => {
    const second = { ...check, id: 'c2', index: 1, title: 'Chart rendered' };
    mocks.rounds.mockResolvedValue({
      runs: [{ id: 'r1', roundIndex: 1, plan: [check, second] }],
      results: [result, { ...result, id: 'result2', checkItemId: 'c2' }],
    });
    let firstCheckAttempts = 0;
    mocks.predict.mockImplementation(async ({ checkResultId }: { checkResultId: string }) => {
      if (checkResultId === 'result2')
        return { id: 'p2', status: 'judged', action: 'reject', comment: 'The chart is blank.' };
      firstCheckAttempts += 1;
      return firstCheckAttempts === 1
        ? { id: 'p1', status: 'errored', statusReason: 'ECONNRESET' }
        : { id: 'p1', status: 'judged', action: 'accept' };
    });

    expect(await reviewGoalDelivery(db, 'u1', 't1', 'op1')).toMatchObject({ status: 'rejected' });
    const calls = mocks.predict.mock.calls.map(([params]) => params.checkResultId);
    expect(calls.filter((id) => id === 'result1')).toHaveLength(2);
    expect(calls.filter((id) => id === 'result2')).toHaveLength(1);
  });

  it('skips a check a dispute panel waived instead of re-judging it', async () => {
    mocks.rounds.mockResolvedValue({
      runs: [{ id: 'r1', roundIndex: 1, plan: [check] }],
      results: [
        {
          ...result,
          metadata: {
            dispute: {
              arbitration: { decidedAt: 'now', outcome: 'upheld', votes: [] },
              filedAt: 'now',
              kind: 'invalid_criterion',
              reason: 'Out of scope',
            },
          },
        },
      ],
    });

    expect(await reviewGoalDelivery(db, 'u1', 't1', 'op1')).toMatchObject({ status: 'passed' });
    expect(mocks.predict).not.toHaveBeenCalled();
  });

  /**
   * Regression: `reviewChecks` accepts a per-check decision in any lifecycle
   * state and writes `userDecision: 'rejected'` without clearing the dispute
   * metadata, so a waiver and a rejection can coexist. The waiver shortcut ran
   * first and dropped the rejection, letting the Goal review pass a delivery the
   * user had explicitly sent back.
   */
  it('lets an explicit rejection outrank a waived check', async () => {
    mocks.rounds.mockResolvedValue({
      runs: [{ id: 'r1', roundIndex: 1, plan: [check] }],
      results: [
        {
          ...result,
          userDecision: 'rejected',
          userDecisionDetail: { comment: 'The table in section 2 is still empty.' },
          metadata: {
            dispute: {
              arbitration: { decidedAt: 'now', outcome: 'upheld', votes: [] },
              filedAt: 'now',
              kind: 'invalid_criterion',
              reason: 'Out of scope',
            },
          },
        },
      ],
    });

    expect(await reviewGoalDelivery(db, 'u1', 't1', 'op1')).toMatchObject({
      status: 'rejected',
      feedback: expect.stringContaining('The table in section 2 is still empty.'),
    });
    expect(mocks.predict).not.toHaveBeenCalled();
  });

  it("hands the builder's missed-evidence rebuttal to the reviewer", async () => {
    mocks.rounds.mockResolvedValue({
      runs: [{ id: 'r1', roundIndex: 1, plan: [check] }],
      results: [
        {
          ...result,
          metadata: {
            dispute: {
              filedAt: 'now',
              kind: 'missed_evidence',
              reason: 'The second screenshot shows the saved document.',
            },
          },
        },
      ],
    });

    await reviewGoalDelivery(db, 'u1', 't1', 'op1');
    expect(mocks.predict).toHaveBeenCalledWith(
      expect.objectContaining({
        builderRebuttal: 'The second screenshot shows the saved document.',
      }),
    );
  });
});

vi.mock('@/server/services/task', () => ({ TaskService: vi.fn() }));