Skip to content

Self-hosted, Self-Improving, Federated Everything App — dev management, agent orchestration, persistent mind, digital twin, second brain, and creative pipeline from a single dashboard over Tailscale to port the user into the OS

18.9 KB
Raw Blame
/**
 * The per-app provider/model pin reaching the SPAWN, for every task type (#4783).
 *
 * `taskTypeOverrides[<taskType>].providerId` / `.model` used to reach the agent
 * only for `layered-intelligence`, because only its `buildTaskInput` hook read
 * the field and `applyProviderModelPins` applied the hook's return last. Every
 * other type merged `taskMetadata` off the same record and then took its provider
 * from the global Schedule pin, so a pin set on the Automation tab silently never
 * ran. These tests assert the generated task's metadata, not the plumbing.
 *
 * Isolated file so the mocked leaf graph (taskSchedule / taskPromptService /
 * providers) can't leak into the shared cosTaskGenerator suite.
 */

import { describe, it, expect, beforeEach, vi } from 'vitest';
import { __resetReviewerIdentityCache } from './reviewerIdentity.js';
import { buildClaimWorkTask, buildJiraTicketTask } from './cosTaskClaimWork.js';
import { emitOnDemandEmpty } from './cosTaskGenerator.js';
import { cosEvents } from './cosEvents.js';

vi.mock('./taskPromptService.js', () => ({
  getTaskPrompt: vi.fn(async () => 'Improve {appName} at {repoPath}\n{issueAuthorFilter}\n{issueExcludeLabels}\nReviewers: {reviewers}'),
  getStagePrompt: vi.fn(async () => 'Improve {appName} at {repoPath}\n{issueAuthorFilter}\n{issueExcludeLabels}\nReviewers: {reviewers}'),
}));

const getCodeReviewDefaultsMock = vi.fn(async () => ({ reviewers: ['codex'], usernames: [], optionalReviewers: [] }));
vi.mock('./codeReview.js', async original => ({
  ...await original(), getCodeReviewDefaults: (...args) => getCodeReviewDefaultsMock(...args),
}));
const identityLookup = vi.fn(async () => ({}));
vi.mock('./github.js', async original => ({ ...await original(), execGh: (...args) => identityLookup(...args) }));
const targetMock = vi.fn(async () => ({ tracker: 'github', target: { forge: 'github', apiHost: 'github.example.test' } }));
vi.mock('../lib/workTracker.js', async original => ({ ...await original(),
  resolveAppForgeTarget: (...args) => targetMock(...args),
  resolveAppWorkTracker: vi.fn(async () => ({ resolved: 'github', source: 'test' })),
}));
const recordPreflightOutcome = vi.fn(async () => {});
const finishPreflightCard = vi.fn(async () => {});
vi.mock('./preflightTaskCard.js', () => ({
  recordPreflightOutcome: (...args) => recordPreflightOutcome(...args),
  finishPreflightCard: (...args) => finishPreflightCard(...args),
  preflightReporter: () => vi.fn(async () => {}),
}));

const getTaskIntervalMock = vi.fn(async () => ({ type: 'weekly', taskMetadata: {} }));
vi.mock('./taskSchedule.js', () => ({
  INTERVAL_TYPES: { PERPETUAL: 'perpetual', WEEKLY: 'weekly' },
  getTaskInterval: vi.fn((...args) => getTaskIntervalMock(...args)),
  stripManagedAgentOptionsFromOverride: vi.fn((_type, meta) => meta),
  recordExecution: vi.fn(async () => {}),
  parkPerpetual: vi.fn(async () => {}),
  getPerpetualDrainState: vi.fn(async () => ({ signature: null, dispatchCount: 0 })),
  recordPerpetualDispatch: vi.fn(async () => 1),
  recordPerpetualStall: vi.fn(async () => {}),
}));

vi.mock('./appActivity.js', async (importActual) => ({
  ...(await importActual()),
  updateAppActivity: vi.fn(async () => {}),
}));

const getAppTaskTypeOverridesMock = vi.fn(async () => ({}));
vi.mock('./apps.js', async (importActual) => ({
  ...(await importActual()),
  getAppTaskTypeOverrides: vi.fn((...args) => getAppTaskTypeOverridesMock(...args)),
}));

// The harness boundary the pin has to answer to: `api` providers return text with
// no file-writing tools, so an agent task can never run on one.
const PROVIDER_TYPES = { 'claude-cli': 'cli', 'opencode-tui': 'tui', ollama: 'api' };
vi.mock('./providers.js', async (importActual) => ({
  ...(await importActual()),
  getProviderById: vi.fn(async (id) => (PROVIDER_TYPES[id] ? { id, type: PROVIDER_TYPES[id] } : null)),
}));

vi.mock('./taskLearning.js', async (importActual) => ({
  ...(await importActual()),
  getTaskTypeConfidence: vi.fn(async () => ({ autoApprove: true, tier: 'high', reason: 'test' })),
}));

vi.mock('../lib/gitRemote.js', async (importActual) => ({
  ...(await importActual()),
  readOriginRemoteUrl: vi.fn(async () => null),
}));

vi.mock('./perpetualWork.js', async importOriginal => ({
  ...(await importOriginal()),
  detectActionableWork: vi.fn(async () => ({ actionable: true, count: 3, signature: '42,43,44' })),
}));

import { detectActionableWork } from './perpetualWork.js';
import { recordPerpetualStall } from './taskSchedule.js';
import { prepareManagedAppImprovementTask } from './cosTaskGenerator.js';
import { resolveClaimWorkMetadata } from './cosTaskClaimWork.js';

const APP = { id: 'app-1', name: 'Example App', repoPath: '/tmp/example-repo' };
const STATE = { config: { confidenceAutoApproval: { enabled: false }, idleReviewPriority: 'MEDIUM' } };

// `ux` has no buildTaskInput hook — exactly the class of task type the pin used to
// be inert for.
const generate = async (taskType = 'ux') => {
  const prepared = await prepareManagedAppImprovementTask(taskType, APP, STATE, { skipPreconditions: true });
  return prepared.task;
};

describe('per-app provider/model pin on a task type with no buildTaskInput hook', () => {
  beforeEach(() => {
    vi.clearAllMocks();
    getTaskIntervalMock.mockResolvedValue({ type: 'weekly', taskMetadata: {} });
    getAppTaskTypeOverridesMock.mockResolvedValue({});
  });

  it('spawns on the app pin, not the global Schedule pin', async () => {
    getTaskIntervalMock.mockResolvedValue({ type: 'weekly', taskMetadata: {}, providerId: 'opencode-tui', model: 'qwen' });
    getAppTaskTypeOverridesMock.mockResolvedValue({ ux: { enabled: true, providerId: 'claude-cli', model: 'opus' } });

    const task = await generate();
    expect(task.metadata.provider).toBe('claude-cli');
    expect(task.metadata.providerId).toBe('claude-cli');
    expect(task.metadata.model).toBe('opus');
  });

  it('still takes the Schedule pin when the app pins nothing', async () => {
    getTaskIntervalMock.mockResolvedValue({ type: 'weekly', taskMetadata: {}, providerId: 'opencode-tui', model: 'qwen' });

    const task = await generate();
    expect(task.metadata.provider).toBe('opencode-tui');
    expect(task.metadata.model).toBe('qwen');
  });

  it('does not wedge on an api-typed app pin — it falls back to the Schedule pin', async () => {
    getTaskIntervalMock.mockResolvedValue({ type: 'weekly', taskMetadata: {}, providerId: 'claude-cli', model: 'opus' });
    getAppTaskTypeOverridesMock.mockResolvedValue({ ux: { enabled: true, providerId: 'ollama', model: 'llama-3' } });

    const task = await generate();
    expect(task.metadata.provider).toBe('claude-cli');
    expect(task.metadata.model).toBe('opus');
  });

  it('leaves an api-only Schedule pin alone when the app pins an api provider too', async () => {
    // Nothing reachable has a harness. Rerouting to some other provider the user
    // never chose would be worse than letting agentProviderResolution report its
    // permanent, actionable error — so the Schedule pin stands.
    getTaskIntervalMock.mockResolvedValue({ type: 'weekly', taskMetadata: {}, providerId: 'ollama' });
    getAppTaskTypeOverridesMock.mockResolvedValue({ ux: { enabled: true, providerId: 'ollama' } });

    const task = await generate();
    expect(task.metadata.provider).toBe('ollama');
  });

  // A model is provider-scoped. Overriding only the PROVIDER must not leave the
  // Schedule pin's model behind: agentProviderResolution honors an explicit
  // metadata.model as a CLI pass-through, so the leak ships the wrong CLI a model
  // it cannot run and fails on every retry until the task blocks.
  it('drops the Schedule pin model when the app pins a different provider', async () => {
    getTaskIntervalMock.mockResolvedValue({ type: 'weekly', taskMetadata: {}, providerId: 'opencode-tui', model: 'qwen-a' });
    getAppTaskTypeOverridesMock.mockResolvedValue({ ux: { enabled: true, providerId: 'claude-cli' } });

    const task = await generate();
    expect(task.metadata.provider).toBe('claude-cli');
    expect(task.metadata.model).toBeUndefined();
  });

  it('honors an app model pinned without an app provider', async () => {
    getTaskIntervalMock.mockResolvedValue({ type: 'weekly', taskMetadata: {}, providerId: 'claude-cli', model: 'opus' });
    getAppTaskTypeOverridesMock.mockResolvedValue({ ux: { enabled: true, model: 'sonnet' } });

    const task = await generate();
    expect(task.metadata.provider).toBe('claude-cli');
    expect(task.metadata.model).toBe('sonnet');
  });
});


describe('pipeline stage provider precedence', () => {
  it('preserves the stage local route over broader schedule and app pins', async () => {
    getTaskIntervalMock.mockResolvedValue({ type: 'weekly', providerId: 'opencode-tui', model: 'broad', effort: 'high', taskMetadata: {
      pipeline: { stages: [{ name: 'Local stage', promptKey: 'ux', providerId: 'claude-cli', model: 'local-model', effort: 'low' }] },
    } });
    getAppTaskTypeOverridesMock.mockResolvedValue({ ux: { providerId: 'opencode-tui', model: 'app-model' } });
    const task = await generate();
    expect(task.metadata).toMatchObject({ provider: 'claude-cli', providerId: 'claude-cli', model: 'local-model', effort: 'low' });
    const { task: overridden } = await prepareManagedAppImprovementTask('ux', APP, STATE, {
      skipPreconditions: true, providerOverride: { provider: 'opencode-tui', model: 'explicit-model', effort: 'high' },
    });
    expect(overridden.metadata).toMatchObject({ provider: 'opencode-tui', model: 'explicit-model', effort: 'high' });
  });
});

// Regression: maintainer inspection and batch execution must resolve the same
// claim-issue settings, rather than consulting the separate claim-work defaults.
it('prepares a scheduled perpetual swarm with matching filters and per-app execution settings', async () => {
  getTaskIntervalMock.mockImplementation(async taskType => taskType === 'claim-issue' ? {
    type: 'on-demand', perpetual: true, autoStart: false, providerId: 'opencode-tui', model: 'global-model', effort: 'low',
    taskMetadata: { swarmCount: 6, issueAuthorFilter: 'self', reviewers: ['codex'], useWorktree: false, openPR: false },
  } : { taskMetadata: { swarmCount: 0, issueAuthorFilter: 'any' } });
  getAppTaskTypeOverridesMock.mockResolvedValue({
    'claim-issue': { providerId: 'claude-cli', model: 'app-model',
      taskMetadata: { swarmCount: 3, issueAuthorFilter: 'collaborators', issueExcludeLabels: ['hold'], reviewers: ['opencode'] } },
    'claim-work': { taskMetadata: { issueAuthorFilter: 'owner' } },
  });
  const preview = await resolveClaimWorkMetadata(APP, 'claim-issue');
  const prepared = await prepareManagedAppImprovementTask('claim-issue', APP, STATE);
  expect(prepared.task.metadata).toMatchObject({ ...preview.metadata, analysisType: 'claim-issue',
    claimFlow: true, perpetual: true, provider: 'claude-cli', model: 'app-model', effort: 'low' });
  expect(prepared.task.metadata.claimTarget).toBeUndefined();
  expect(prepared.task.description).toContain('--swarm=3');
  expect(prepared.task.description).toContain('collaborators');
  expect(prepared.task.description).toContain('hold');
  expect(prepared.task.description).toContain('opencode');
  expect(prepared.task.description).toContain('model:');
  expect(prepared.task.description).toContain('effort:');
  expect(prepared.pendingPerpetualDispatch).toMatchObject({ taskType: 'claim-issue', appId: APP.id });
});

// A skipped prepare hands back WHY through its `skip` field — the one shape of
// "nothing", instead of a bare null plus a module-level side channel (#9182).
describe('prepareManagedAppImprovementTask skip result', () => {
  const transient = { actionable: false, transient: true, cli: 'gh', reason: 'gh-list-failed' };
  const prepareClaim = (appId) => prepareManagedAppImprovementTask('claim-issue', { ...APP, id: appId }, STATE);

  beforeEach(() => {
    vi.clearAllMocks();
    getTaskIntervalMock.mockResolvedValue({ type: 'on-demand', perpetual: true, taskMetadata: {} });
    getAppTaskTypeOverridesMock.mockResolvedValue({});
  });

  it('carries the transient detector verdict on the skip and leaves the streak below the stall threshold quiet', async () => {
    vi.mocked(detectActionableWork).mockResolvedValue({ ...transient, remedy: 'Allow gh outbound.', detail: 'bad fd' });
    const prepared = await prepareClaim('app-skip-verdict');
    expect(prepared).toEqual({
      task: null,
      pendingPerpetualDispatch: null,
      skip: { gate: 'perpetual-work', reason: 'gh-list-failed', cli: 'gh', remedy: 'Allow gh outbound.', detail: 'bad fd' },
    });
    expect(recordPerpetualStall).not.toHaveBeenCalled();
  });

  it('persists a stall on the 3rd CONSECUTIVE transient probe, and an actionable probe in between restarts the count', async () => {
    const appId = 'app-skip-streak';
    vi.mocked(detectActionableWork).mockResolvedValue(transient);
    await prepareClaim(appId);
    await prepareClaim(appId);
    expect(recordPerpetualStall).not.toHaveBeenCalled();

    // Recovery: the probe reports work again, which must clear the streak.
    vi.mocked(detectActionableWork).mockResolvedValue({ actionable: true, count: 1, signature: null });
    expect((await prepareClaim(appId)).skip).toBeNull();
    recordPerpetualStall.mockClear();

    vi.mocked(detectActionableWork).mockResolvedValue(transient);
    await prepareClaim(appId);
    await prepareClaim(appId);
    expect(recordPerpetualStall).not.toHaveBeenCalled();
    await prepareClaim(appId);
    expect(recordPerpetualStall).toHaveBeenCalledWith('claim-issue', appId, expect.objectContaining({ consecutive: 3, cli: 'gh' }));
  });

  it('names the gate for an install-wide target request that has no managed app to run on', async () => {
    const prepared = await prepareManagedAppImprovementTask('model-comparison-refresh', APP, STATE);
    expect(prepared).toMatchObject({ task: null, pendingPerpetualDispatch: null, skip: { gate: 'install-wide-target', reason: null } });
  });
});


describe('claim dispatch reviewer identity preflight (#13522)', () => {
  beforeEach(() => {
    vi.clearAllMocks();
    __resetReviewerIdentityCache();
    identityLookup.mockReset().mockResolvedValue({});
    targetMock.mockResolvedValue({ tracker: 'github', target: { forge: 'github', apiHost: 'github.example.test' } });
    getTaskIntervalMock.mockResolvedValue({ type: 'weekly', taskMetadata: {} });
    getAppTaskTypeOverridesMock.mockResolvedValue({});
    getCodeReviewDefaultsMock.mockResolvedValue({ reviewers: ['codex'], usernames: ['missing-reviewer'], optionalReviewers: [] });
  });

  const prepare = () => prepareManagedAppImprovementTask('claim-issue', APP, STATE, { skipPreconditions: true });
  const missing = () => { throw Object.assign(new Error('gh: Not Found (HTTP 404)'), { ghStderr: 'gh: Not Found (HTTP 404)' }); };

  it('blocks the scheduled prompt and records the exact default entry; repeated dispatch reuses the identity cache', async () => {
    identityLookup.mockImplementation(missing);
    const prepared = await prepare();
    expect(prepared.task).toBeNull();
    expect(prepared.skip).toMatchObject({ gate: 'reviewer-identity', detail: { reviewers: ['missing-reviewer'] } });
    expect(prepared.skip.reason).toContain('@missing-reviewer');
    expect(prepared.skip.reason).toContain('Settings → Code Reviewers (usernames)');
    expect(recordPreflightOutcome).toHaveBeenCalledWith(expect.objectContaining({ outcome: 'failed', reason: 'REVIEWER_IDENTITY_NOT_FOUND', note: prepared.skip.reason }));
    await prepare();
    expect(identityLookup).toHaveBeenCalledTimes(1);
    expect(identityLookup).toHaveBeenCalledWith(['api', '--hostname', 'github.example.test', 'users/missing-reviewer'], 8000);
    const emitted = vi.fn();
    cosEvents.on('schedule:on-demand-empty', emitted);
    try {
      await emitOnDemandEmpty({ taskScheduleMod: { getPerpetualParkInfo: async () => null }, request: { id: 'request-1', taskType: 'claim-issue' }, targetApp: APP, taskConfig: { perpetual: false }, skip: prepared.skip });
      expect(emitted).toHaveBeenCalledWith(expect.objectContaining({ outcome: 'transient', reason: prepared.skip.reason, forge: { cli: 'reviewer identity', remedy: prepared.skip.reason } }));
    } finally {
      cosEvents.off('schedule:on-demand-empty', emitted);
    }
  });

  it('blocks both manual claim builders before returning a prompt and names a task override entry', async () => {
    identityLookup.mockImplementation(missing);
    getTaskIntervalMock.mockResolvedValue({ type: 'weekly', taskMetadata: { usernames: ['missing-reviewer'] } });
    await expect(buildClaimWorkTask(APP)).rejects.toMatchObject({ code: 'REVIEWER_IDENTITY_NOT_FOUND', message: expect.stringContaining('Tasks → Schedule → claim-work → Reviewers (taskMetadata.usernames)') });
    await expect(buildJiraTicketTask(APP, 'TEST-123')).rejects.toMatchObject({ code: 'REVIEWER_IDENTITY_NOT_FOUND' });
    expect(identityLookup).toHaveBeenCalledTimes(1);
  });

  it('names the per-app settings entry when an app override supplied the missing reviewer', async () => {
    identityLookup.mockImplementation(missing);
    getAppTaskTypeOverridesMock.mockResolvedValue({ 'claim-issue': { taskMetadata: { usernames: ['missing-reviewer'] } } });
    const prepared = await prepare();
    expect(prepared.skip.reason).toContain('Apps → Automation → claim-issue → Reviewers (taskMetadata.usernames)');
  });

  it('preserves healthy reviewer roster and prompt metadata', async () => {
    const { task, skip } = await prepare();
    expect(skip).toBeNull();
    expect(task.description).toContain('Reviewers: codex,@missing-reviewer');
    expect(task.metadata.usernames).toEqual(['missing-reviewer']);
    expect(task.description).not.toContain('Reviewer identity preflight');
    expect(recordPreflightOutcome).not.toHaveBeenCalled();
  });

  it('does not classify authentication failure as a missing identity', async () => {
    identityLookup.mockRejectedValue(new Error('HTTP 401: Bad credentials'));
    const { task, skip } = await prepare();
    expect(skip).toBeNull();
    expect(task.description).toContain('@missing-reviewer');
    expect(recordPreflightOutcome).not.toHaveBeenCalled();
  });

  it('reports a verified-missing optional entry without blocking or rewriting its roster', async () => {
    identityLookup.mockImplementation(missing);
    getCodeReviewDefaultsMock.mockResolvedValue({ reviewers: ['codex'], usernames: ['missing-reviewer'], optionalReviewers: ['@missing-reviewer'] });
    const { task, skip } = await prepare();
    expect(skip).toBeNull();
    expect(task.description).toContain('@missing-reviewer~opt');
    expect(task.description).toContain('Reviewer identity preflight');
    expect(task.description).toContain('Report this configuration fault in the run summary');
    expect(task.metadata.optionalReviewers).toEqual(['@missing-reviewer']);
  });

  it('does not send GitHub identity probes to a GitLab target', async () => {
    targetMock.mockResolvedValue({ tracker: 'gitlab', target: { forge: 'gitlab', apiHost: null } });
    const { task } = await prepare();
    expect(task.description).toContain('@missing-reviewer');
    expect(identityLookup).not.toHaveBeenCalled();
  });
});