Skip to content

armgit / cindy

Public Imported from GitHub
0

Consider it done. The open-source AI agent that works out of the box · 想到,就能做到。开源、开箱即用的 AI Agent。

11.5 KB
Raw Blame
import { describe, expect, it, vi } from 'vitest';

import {
  assessRuntimeModelSwitchGate,
  applyWithVerifiedModelWindow,
  buildDeferredRuntimeSelectionProfile,
  nextDeferredModelWindowRetry,
  shouldSkipColdPiWindowRehydration,
} from '../runtimeModelSwitchGate';

const million = 1_000_000;
const twoHundredK = 200_000;

const base = {
  inTurn: false,
  isRemote: false,
  agentKind: 'claude-code' as const,
  runtimeRouteChanged: true,
  verifiedTargetWindow: million,
  verifiedCurrentWindow: million,
  contextTokensKnown: true,
  contextTokens: 450_000,
};

describe('assessRuntimeModelSwitchGate', () => {
  // Codex can compact with the previous model before sampling the smaller one.
  // Rebuilding here would discard the native history before it gets that chance.
  it.each([false, true])('leaves Codex shrink compaction to native (remote=%s)', (isRemote) => {
    expect(assessRuntimeModelSwitchGate({ ...base, agentKind: 'codex', isRemote,
      verifiedTargetWindow: twoHundredK })).toEqual({ skipRebuild: true, defer: false });
  });
  it.each([
    {
      name: 'same route idle → hot apply, no rebuild',
      input: { ...base, runtimeRouteChanged: false },
      want: { skipRebuild: true, defer: false },
    },
    {
      name: 'same route running → hot apply, no rebuild (does not interrupt turn)',
      input: { ...base, runtimeRouteChanged: false, inTurn: true },
      want: { skipRebuild: true, defer: false },
    },
    {
      name: 'same-or-larger verified window idle → hot apply',
      input: base,
      want: { skipRebuild: true, defer: false },
    },
    {
      name: 'same-or-larger verified window running → hot apply',
      input: { ...base, inTurn: true },
      want: { skipRebuild: true, defer: false },
    },
    {
      name: 'Cindy/unverified target window idle → fail-open hot apply',
      input: { ...base, verifiedTargetWindow: null },
      want: { skipRebuild: true, defer: false },
    },
    {
      name: 'Cindy/unverified target window running → fail-open hot apply',
      input: { ...base, verifiedTargetWindow: null, inTurn: true },
      want: { skipRebuild: true, defer: false },
    },
    {
      name: 'unknown current window → fail-open',
      input: { ...base, verifiedCurrentWindow: undefined },
      want: { skipRebuild: true, defer: false },
    },
    {
      name: 'unknown usage → fail-open',
      input: { ...base, contextTokensKnown: false, contextTokens: 0 },
      want: { skipRebuild: true, defer: false },
    },
    {
      name: 'zero/invalid target window → fail-open',
      input: { ...base, verifiedTargetWindow: 0 },
      want: { skipRebuild: true, defer: false },
    },
    {
      name: 'shrink below danger idle → hot apply, no rebuild',
      input: {
        ...base,
        verifiedTargetWindow: twoHundredK,
        contextTokens: 100_000,
      },
      want: { skipRebuild: true, defer: false },
    },
    {
      name: 'shrink danger idle → live rebuild',
      input: {
        ...base,
        verifiedTargetWindow: twoHundredK,
        contextTokens: 190_000,
      },
      want: { skipRebuild: false, defer: false },
    },
    {
      name: 'shrink overflow idle → live rebuild',
      input: {
        ...base,
        verifiedTargetWindow: twoHundredK,
        contextTokens: 220_000,
      },
      want: { skipRebuild: false, defer: false },
    },
    {
      name: 'shrink danger running → defer, keep selection',
      input: {
        ...base,
        inTurn: true,
        verifiedTargetWindow: twoHundredK,
        contextTokens: 190_000,
      },
      want: { skipRebuild: false, defer: true },
    },
    {
      name: 'remote running any route change → defer',
      input: { ...base, isRemote: true, inTurn: true },
      want: { skipRebuild: true, defer: true },
    },
    {
      name: 'remote running even when windows unknown → defer',
      input: {
        ...base,
        isRemote: true,
        inTurn: true,
        verifiedTargetWindow: null,
        contextTokensKnown: false,
      },
      want: { skipRebuild: true, defer: true },
    },
    {
      name: 'Pi running route change → defer',
      input: { ...base, agentKind: 'pi', inTurn: true },
      want: { skipRebuild: true, defer: true },
    },
    {
      name: 'Pi idle same window → hot apply',
      input: { ...base, agentKind: 'pi' },
      want: { skipRebuild: true, defer: false },
    },
    {
      name: 'Pi running but route unchanged → hot apply (effort-only)',
      input: {
        ...base,
        agentKind: 'pi',
        inTurn: true,
        runtimeRouteChanged: false,
      },
      want: { skipRebuild: true, defer: false },
    },
    {
      name: 'Codex running same window → hot apply',
      input: { ...base, agentKind: 'codex', inTurn: true },
      want: { skipRebuild: true, defer: false },
    },
    {
      name: 'remote idle shrink danger → reject (cannot rebuild remotely)',
      input: {
        ...base,
        isRemote: true,
        verifiedTargetWindow: twoHundredK,
        contextTokens: 190_000,
      },
      want: { skipRebuild: false, defer: false, reject: 'remote-shrink-rebuild' },
    },
    {
      name: 'remote idle unverified target → fail-open',
      input: { ...base, isRemote: true, verifiedTargetWindow: null },
      want: { skipRebuild: true, defer: false },
    },
    {
      name: 'local BYOM unverified + running → fail-open hot apply',
      input: {
        ...base,
        agentKind: 'claude-code',
        inTurn: true,
        verifiedTargetWindow: null,
        verifiedCurrentWindow: million,
      },
      want: { skipRebuild: true, defer: false },
    },
    {
      name: 'shrink overflow running → defer, keep selection',
      input: {
        ...base,
        inTurn: true,
        verifiedTargetWindow: twoHundredK,
        contextTokens: 220_000,
      },
      want: { skipRebuild: false, defer: true },
    },
    {
      name: 'Codex shrink danger running → native switch without destructive rebuild',
      input: {
        ...base,
        agentKind: 'codex',
        inTurn: true,
        verifiedTargetWindow: twoHundredK,
        contextTokens: 190_000,
      },
      want: { skipRebuild: true, defer: false },
    },
    {
      name: 'remote running same-route effort-only → still defer',
      input: { ...base, isRemote: true, inTurn: true, runtimeRouteChanged: false },
      want: { skipRebuild: true, defer: true },
    },
    {
      name: 'Pi unverified target running → defer (cannot live-verify)',
      input: {
        ...base,
        agentKind: 'pi',
        inTurn: true,
        verifiedTargetWindow: null,
      },
      want: { skipRebuild: true, defer: true },
    },
  ])('$name', ({ input, want }) => {
    expect(assessRuntimeModelSwitchGate(input)).toEqual(want);
  });
});

describe('shouldSkipColdPiWindowRehydration', () => {
  it.each([
    {
      name: 'live usage far below the target window → skip the 2~3s cold start',
      input: { contextTokens: 26_921, targetContextWindow: million },
      want: true,
    },
    {
      name: 'empty context → nothing to protect, skip',
      input: { contextTokens: 0, targetContextWindow: twoHundredK },
      want: true,
    },
    {
      name: 'warn band still hot-applies (no rebuild) → skip',
      input: { contextTokens: 150_000, targetContextWindow: twoHundredK },
      want: true,
    },
    {
      name: 'danger band can need the shrink handoff → verify',
      input: { contextTokens: 185_000, targetContextWindow: twoHundredK },
      want: false,
    },
    {
      name: 'overflow can need confirmation → verify',
      input: { contextTokens: 240_000, targetContextWindow: twoHundredK },
      want: false,
    },
    {
      name: 'missing live usage must not skip verification',
      input: { contextTokens: null, targetContextWindow: twoHundredK },
      want: false,
    },
    {
      name: 'unknown target window must not skip verification',
      input: { contextTokens: 26_921, targetContextWindow: null },
      want: false,
    },
    {
      name: 'non-positive target window is not a verified ceiling',
      input: { contextTokens: 26_921, targetContextWindow: 0 },
      want: false,
    },
  ])('$name', ({ input, want }) => {
    expect(shouldSkipColdPiWindowRehydration(input)).toBe(want);
  });
});

describe('buildDeferredRuntimeSelectionProfile', () => {
  it('keeps the clicked high + Fast on the pending profile', () => {
    expect(
      buildDeferredRuntimeSelectionProfile({
        agentKind: 'claude-code',
        model: 'claude-fable-5',
        providerId: 'cindy',
        atomicSelection: { effort: 'high', fastMode: true },
        currentFastMode: false,
      }),
    ).toEqual({
      agentKind: 'claude-code',
      model: 'claude-fable-5',
      providerId: 'cindy',
      effort: 'high',
      fastMode: true,
    });
  });

  it('no-rank model pending effort is null, not leftover high', () => {
    expect(
      buildDeferredRuntimeSelectionProfile({
        agentKind: 'claude-code',
        model: 'local-llama',
        providerId: 'custom:ollama',
        atomicSelection: { effort: null, fastMode: false },
        currentEffort: 'xhigh',
        currentFastMode: true,
      }).effort,
    ).toBeNull();
  });

  // Regression: scheduler Pi route refresh omitted atomicSelection and projected
  // null over an xhigh session. Both immediate and queued profiles use this
  // production builder; previous coverage only exercised explicit selections.
  // Unit tier: no native process or database is needed to reproduce the lost axis.
  it.each(['xhigh', null] as const)('route-only refresh preserves current effort %s', (currentEffort) => {
    expect(
      buildDeferredRuntimeSelectionProfile({
        agentKind: 'pi',
        model: 'reasoner',
        providerId: 'custom',
        currentEffort,
        currentFastMode: true,
      }),
    ).toMatchObject({ effort: currentEffort, fastMode: true });
  });
});

describe('nextDeferredModelWindowRetry', () => {
  it('idle settle with no extra confirmation is done', () => {
    expect(nextDeferredModelWindowRetry(false, undefined)).toEqual({ action: 'done' });
  });

  it('retries with the verified window instead of dropping the selection', () => {
    expect(nextDeferredModelWindowRetry(true, 200_000)).toEqual({
      action: 'retry',
      confirmedContextWindow: 200_000,
    });
  });

  it('cancels only when confirmation is required but no window was verified', () => {
    expect(nextDeferredModelWindowRetry(true, undefined)).toEqual({ action: 'cancel' });
    expect(nextDeferredModelWindowRetry(true, 0)).toEqual({ action: 'cancel' });
  });
});

describe('saved model selection window recovery', () => {
  it('retries with exactly the verified target window before reporting success', async () => {
    const apply = vi
      .fn()
      .mockResolvedValueOnce({
        contextWindowConfirmationRequired: 200_000,
        contextTokensForConfirmation: 400_000,
      })
      .mockResolvedValueOnce({ applied: true });
    await expect(applyWithVerifiedModelWindow(apply)).resolves.toEqual({ applied: true });
    expect(apply.mock.calls).toEqual([[], [200_000]]);
  });
  it('does not guess unknown windows or loop if the verified window changes', async () => {
    const unknown = vi.fn(async () => ({ contextTokensForConfirmation: 400_000 }));
    await expect(applyWithVerifiedModelWindow(unknown)).rejects.toThrow('could not be verified');
    expect(unknown).toHaveBeenCalledOnce();
    const changed = vi.fn(async () => ({ contextWindowConfirmationRequired: 200_000 }));
    await expect(applyWithVerifiedModelWindow(changed)).rejects.toThrow('changed during recovery');
    expect(changed).toHaveBeenCalledTimes(2);
  });
});