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.

8.2 KB
Raw Blame
import { GOAL_BATCH_RELEASE_TITLE, GOAL_MACHINE_GATE_TITLE } from '@lobechat/const/goal';
import { describe, expect, it } from 'vitest';

import {
  coordinatorGateKind,
  coordinatorGateReason,
  coordinatorReasonCopy,
  gateOptionLabelKey,
  localizedCoordinatorReason,
} from './coordinatorCopy';

const decision = (ids: string[]) => ({ options: ids.map((id) => ({ id, label: id })) }) as any;

describe('coordinatorGateKind', () => {
  it('recognizes the coordinator gate shapes and nothing else', () => {
    expect(coordinatorGateKind(decision(['retry', 'retire']))).toBe('recoverTask');
    expect(coordinatorGateKind(decision(['retry', 'fail']))).toBe('goalAcceptance');
    // The terminal acceptance gate also offers abandoning it.
    expect(coordinatorGateKind(decision(['retry', 'retire', 'fail']))).toBe('goalAcceptance');
    expect(coordinatorGateKind(decision(['option-1', 'assume', 'answer']))).toBe('clarifyGoal');
    expect(coordinatorGateKind(decision(['approve', 'reject']))).toBeUndefined();
    expect(coordinatorGateKind(undefined)).toBeUndefined();
  });

  it('tells a held batch release from a batch break', () => {
    expect(coordinatorGateKind(decision(['release', 'revise']))).toBe('batchRelease');
    expect(coordinatorGateKind(decision(['revise', 'new_class']))).toBe('batchPatternBreak');
    expect(gateOptionLabelKey({ id: 'release' }, 'batchRelease')).toBe(
      'goalProcess.gate.option.releaseBatch',
    );
    // A sent-back release is rewritten by the main Agent, not re-probed.
    expect(gateOptionLabelKey({ id: 'revise' }, 'batchRelease')).toBe(
      'goalProcess.gate.option.sendBackBatch',
    );
    expect(gateOptionLabelKey({ id: 'revise' }, 'batchPatternBreak')).toBe(
      'goalProcess.gate.option.reviseBatch',
    );
  });
});

describe('coordinatorNodeTitleKey', () => {
  it('maps the terminal acceptance task and coordinator gates to locale keys', async () => {
    const { coordinatorNodeTitleKey } = await import('./coordinatorCopy');
    const view = (node: any, dec?: any) => ({ decision: dec, humanTouches: [], node }) as any;

    expect(
      coordinatorNodeTitleKey(view({ kind: 'task', title: 'Complete full Goal acceptance' })),
    ).toBe('goalProcess.node.terminalAcceptance');
    expect(
      coordinatorNodeTitleKey(
        view({ kind: 'decision', title: 'x' }, decision(['retry', 'retire'])),
      ),
    ).toBe('goalProcess.gate.title.recoverTask');
    expect(
      coordinatorNodeTitleKey(view({ kind: 'decision', title: GOAL_BATCH_RELEASE_TITLE })),
    ).toBe('goalProcess.gate.title.batchRelease');
    expect(coordinatorNodeTitleKey(view({ kind: 'task', title: '普通任务' }))).toBeUndefined();
  });
});

describe('isGoalAcceptanceTask', () => {
  it('recognizes only the terminal Goal acceptance task', async () => {
    const { isGoalAcceptanceTask } = await import('./coordinatorCopy');
    const view = (node: any) => ({ humanTouches: [], node }) as any;

    expect(
      isGoalAcceptanceTask(view({ kind: 'task', title: 'Complete full Goal acceptance' })),
    ).toBe(true);
    expect(isGoalAcceptanceTask(view({ kind: 'task', title: 'R2 · 对比' }))).toBe(false);
    expect(
      isGoalAcceptanceTask(view({ kind: 'decision', title: 'Complete full Goal acceptance' })),
    ).toBe(false);
  });
});

describe('coordinatorGateReason', () => {
  it('strips the question template down to the reason', () => {
    expect(
      coordinatorGateReason(
        'Verification could not run (internal error); the delivery was not evaluated.. Retry or retire this task node?',
      ),
    ).toBe('Verification could not run (internal error); the delivery was not evaluated.');
    expect(
      coordinatorGateReason(
        'Goal-level acceptance did not pass. Retry Goal acceptance or fail this Goal?',
      ),
    ).toBe('Goal-level acceptance did not pass');
    expect(
      coordinatorGateReason(
        'Goal-level acceptance did not pass. Retry Goal acceptance, abandon it, or fail this Goal?',
      ),
    ).toBe('Goal-level acceptance did not pass');
  });

  it('returns non-template questions verbatim', () => {
    expect(coordinatorGateReason('Which dataset should the ablation use?')).toBe(
      'Which dataset should the ablation use?',
    );
  });
});

describe('coordinatorReasonCopy', () => {
  it('maps every known coordinator reason to a locale ref', () => {
    expect(
      coordinatorReasonCopy(
        'Verification could not run (internal error); the delivery was not evaluated.',
      ),
    ).toEqual({ key: 'goalProcess.gate.reason.verifyInternalError' });
    expect(coordinatorReasonCopy('Task T-932 did not pass verification')).toEqual({
      key: 'goalProcess.gate.reason.verifyFailed',
      params: { id: 'T-932' },
    });
    expect(coordinatorReasonCopy('Task attempt budget was exhausted')).toEqual({
      key: 'goalProcess.gate.reason.attemptBudgetExhausted',
    });
    expect(
      coordinatorReasonCopy('Goal cost budget was exhausted after an operation was abandoned'),
    ).toEqual({ key: 'goalProcess.gate.reason.costBudgetExhausted' });
    expect(coordinatorReasonCopy('Automatic recovery could not start the next attempt')).toEqual({
      key: 'goalProcess.gate.reason.recoveryFailed',
    });
    expect(
      coordinatorReasonCopy(
        'Automatic recovery could not start the next attempt: Task already has a running topic (tpc_1). Cancel it first or use --continue',
      ),
    ).toEqual({
      key: 'goalProcess.gate.reason.recoveryFailedWithError',
      params: {
        error: 'Task already has a running topic (tpc_1). Cancel it first or use --continue',
      },
    });
    expect(
      coordinatorReasonCopy('Task T-12 failed verification the same way 2 times in a row'),
    ).toEqual({
      key: 'goalProcess.gate.reason.verifyStalled',
      params: { count: '2', id: 'T-12' },
    });
  });

  it('wraps a bare runtime error type in a localized sentence', () => {
    expect(coordinatorReasonCopy('InvalidProviderAPIKey')).toEqual({
      key: 'goalProcess.gate.reason.runError',
      params: { code: 'InvalidProviderAPIKey' },
    });
    expect(coordinatorReasonCopy('Timeout')).toBeUndefined();
  });

  it('passes unknown reasons through as undefined so the raw text renders', () => {
    expect(coordinatorReasonCopy('some future reason')).toBeUndefined();
    expect(coordinatorReasonCopy(undefined)).toBeUndefined();
  });
});

describe('machine gate copy', () => {
  it('reads the machine gate off its title and gives its Retry the "fixed it" label', () => {
    expect(coordinatorGateKind(decision(['retry', 'retire']), GOAL_MACHINE_GATE_TITLE)).toBe(
      'fixSetup',
    );
    expect(gateOptionLabelKey({ id: 'retry' }, 'fixSetup')).toBe(
      'goalProcess.gate.option.fixedRetry',
    );
    expect(gateOptionLabelKey({ id: 'retry' }, 'recoverTask')).toBe(
      'goalProcess.gate.option.retry',
    );
    expect(gateOptionLabelKey({ id: 'option-1' })).toBeUndefined();
  });

  it('strips the machine gate question down to what broke', () => {
    expect(
      coordinatorGateReason(
        'Setup problem: Working directory does not exist: /tmp/x. Create /tmp/x on the device the agent runs on, or point the agent at a working directory that exists there. Fix it, then retry or retire this task node?',
      ),
    ).toBe(
      'Setup problem: Working directory does not exist: /tmp/x. Create /tmp/x on the device the agent runs on, or point the agent at a working directory that exists there',
    );
    expect(coordinatorReasonCopy('Task device stayed offline')).toEqual({
      key: 'goalProcess.gate.reason.deviceStayedOffline',
    });
  });
});

describe('localizedCoordinatorReason', () => {
  const t = (key: string, params?: Record<string, unknown>) =>
    `${key}${params?.count === undefined ? '' : `(${params.count})`}`;

  it('localizes every blocker of a compound batch reason', () => {
    expect(
      localizedCoordinatorReason(
        '1 unit(s) did not succeed; a human decision is still open in this batch',
        t,
      ),
    ).toBe('goalProcess.gate.reason.batchBroken(1); goalProcess.gate.reason.batchOpenDecision');
  });

  it('keeps a reason it does not recognize as stored', () => {
    expect(localizedCoordinatorReason('Something else went wrong', t)).toBe(
      'Something else went wrong',
    );
  });
});