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。

22.5 KB
Raw Blame
import fs from 'node:fs';
import os from 'node:os';
import path from 'node:path';
import { snapshotManagedSkillGrants } from '../shared/managed-skill-policy.js';
import { describe, expect, it } from 'vitest';

import type { CapabilityRoutingPolicy } from '../../types/capability-routing.js';
import {
  buildCodexBotSkillConfigOverrides,
  buildCodexBotMcpConfigOverrides,
  buildCodexCapabilityConfigOverrides,
  buildCodexCapabilitySkillConfigOverrides,
  buildCodexSessionCapabilityRoutingPolicy,
  mergeCodexSkillConfigOverrides,
  requiresCodexCapabilitySkillDiscovery,
} from './capability-routing.js';

describe('Bot Skill config', () => {
  it('keeps physical grants frozen when a catalog alias is repointed before reload', () => {
    const root = fs.mkdtempSync(path.join(os.tmpdir(), 'codex-bot-skill-grants-'));
    try {
      const old = path.join(root, 'approved');
      const replacement = path.join(root, 'replacement');
      const alias = path.join(root, 'discovery');
      for (const dir of [old, replacement]) {
        fs.mkdirSync(dir);
        fs.writeFileSync(path.join(dir, 'SKILL.md'), '# fixture');
      }
      fs.symlinkSync(old, alias, process.platform === 'win32' ? 'junction' : 'dir');
      const policy = { mode: 'allowlist' as const, configured: ['learn'],
        catalog: [{ name: 'learn', path: path.join(alias, 'SKILL.md') }] };
      const grants = snapshotManagedSkillGrants(policy)!;
      fs.unlinkSync(alias);
      fs.symlinkSync(replacement, alias, process.platform === 'win32' ? 'junction' : 'dir');
      const paths = [old, replacement].map(dir => path.join(dir, 'SKILL.md'));
      expect(buildCodexBotSkillConfigOverrides(policy, { grants,
        skills: paths.map(source => ({ path: source, enabled: true })),
      })['skills.config']).toEqual(expect.arrayContaining([
        { path: paths[0], enabled: true }, { path: paths[1], enabled: false },
        { path: policy.catalog[0]!.path, enabled: false },
      ]));
      // A scoped native discovery error wins over a previously granted path.
      expect(buildCodexBotSkillConfigOverrides(policy, { grants,
        skills: [{ path: paths[0]!, enabled: false }],
      })['skills.config']).toEqual(expect.arrayContaining([{ path: paths[0], enabled: false }]));
    } finally { fs.rmSync(root, { recursive: true, force: true }); }
  });

  it('maps a Bot allowlist to native per-thread Codex Skill state', () => {
    expect(buildCodexBotSkillConfigOverrides({
      mode: 'allowlist',
      configured: ['release'],
      catalog: [
        { name: 'release-notes', runtimeCommandName: 'release', path: '/skills/release/SKILL.md' },
        { name: 'incident-response', path: '/skills/incident/SKILL.md' },
      ],
    })).toEqual({
      'skills.config': [
        { path: '/skills/incident/SKILL.md', enabled: false },
        { path: '/skills/release/SKILL.md', enabled: true },
      ],
    });
  });

  it('keeps the stricter disabled state when Bot and host policies overlap', () => {
    expect(mergeCodexSkillConfigOverrides(
      { 'skills.config': [{ path: '/skills/release/SKILL.md', enabled: false }] },
      { 'skills.config': [{ path: '/skills/release/SKILL.md', enabled: true }] },
    )).toEqual({
      'skills.config': [{ path: '/skills/release/SKILL.md', enabled: false }],
    });
  });

  it('mounts Bot-owned Skills while disabling ambient Skills under legacy inherit', () => {
    expect(buildCodexBotSkillConfigOverrides({
      mode: 'inherit',
      configured: [],
      catalog: [{ name: 'global-release', path: '/skills/global-release/SKILL.md' }],
      ownSkills: [
        { name: 'weekly-report', path: '/bots/a/skills/weekly-report' },
        { name: 'duplicate', path: '/bots/a/skills/weekly-report' },
      ],
    })).toEqual({
      'skills.config': [
        { path: '/bots/a/skills/weekly-report', enabled: true },
        { path: '/skills/global-release/SKILL.md', enabled: false },
      ],
    });
  });

  it('does not let an ambient allowlist disable a Bot-owned Skill at the same path', () => {
    expect(buildCodexBotSkillConfigOverrides({
      mode: 'allowlist',
      configured: [],
      catalog: [{ name: 'ambient-copy', path: '/bots/a/skills/weekly-report' }],
      ownSkills: [{ name: 'weekly-report', path: '/bots/a/skills/weekly-report' }],
    })).toEqual({
      'skills.config': [
        { path: '/bots/a/skills/weekly-report', enabled: true },
      ],
    });
  });

  it('uses the SKILL.md file path for a Bot-owned Skill when provided', () => {
    expect(buildCodexBotSkillConfigOverrides({
      mode: 'allowlist',
      configured: [],
      catalog: [],
      ownSkills: [{
        name: 'weekly-report',
        path: '/bots/a/skills/weekly-report',
        filePath: '/bots/a/skills/weekly-report/SKILL.md',
      }],
    })).toEqual({
      'skills.config': [
        { path: '/bots/a/skills/weekly-report/SKILL.md', enabled: true },
      ],
    });
  });
});

describe('Bot MCP config', () => {
  it('keeps shared plugin discovery and explicitly selected MCP servers', () => {
    expect(buildCodexBotMcpConfigOverrides({
      mode: 'allowlist',
      configured: ['custom-a'],
      catalog: [
        { name: 'cindy_memory', source: 'builtin' },
        { name: 'cindy_helper', source: 'builtin' },
        { name: 'cindy', source: 'builtin' },
        { name: 'cindy_group_history', source: 'builtin' },
        { name: 'custom-a', source: 'custom' },
        { name: 'custom.with.dot', source: 'custom' },
      ],
    }, new Set(['cindy_memory', 'cindy_helper', 'cindy', 'cindy_group_history', 'cindy_orca', 'custom-a', 'custom.with.dot']))).toEqual({
      'features.apps': false,
      'mcp_servers.cindy_group_history.enabled': false,
      'mcp_servers.cindy_orca.enabled': false,
      'mcp_servers."custom.with.dot".enabled': false,
    });
  });

  it('does not manufacture invalid disabled-only transport entries', () => {
    expect(buildCodexBotMcpConfigOverrides({ mode: 'allowlist', configured: [], catalog: [{ name: 'cindy_contacts', source: 'builtin' }] }, new Set())).toEqual({ 'features.apps': false });
  });

  it('keeps an explicitly configured builtin capability server enabled', () => {
    // The docs toolset mounts cindy_docs through the same explicit allowlist
    // as custom MCPs; it must not be pinned off like ambient builtins.
    expect(buildCodexBotMcpConfigOverrides({
      mode: 'allowlist',
      configured: ['cindy_docs'],
      catalog: [
        { name: 'cindy_memory', source: 'builtin' },
        { name: 'cindy_helper', source: 'builtin' },
        { name: 'cindy_docs', source: 'builtin' },
        { name: 'cindy_scheduler', source: 'builtin' },
      ],
    }, new Set(['cindy_memory', 'cindy_helper', 'cindy_docs', 'cindy_scheduler', 'cindy', 'cindy_group_history', 'cindy_orca', 'cindy_docs']))).toEqual({
      'features.apps': false,
      'mcp_servers.cindy_scheduler.enabled': false,
      'mcp_servers.cindy_group_history.enabled': false,
      'mcp_servers.cindy_orca.enabled': false,
    });
  });

  // P1 regression: only real transports enter this set; a colliding custom name
  // must not escape the restriction. Existing native-only cases had no transport.
  it('disables an unselected transport named codex_apps', () => {
    expect(buildCodexBotMcpConfigOverrides({
      mode: 'allowlist', configured: [],
      catalog: [{ name: 'codex_apps', source: 'custom' }, { name: 'other_service', source: 'custom' }],
    }, new Set(['codex_apps', 'other_service']))).toEqual({
      'features.apps': false,
      'mcp_servers.codex_apps.enabled': false,
      'mcp_servers.other_service.enabled': false,
    });
  });

  // Regression: native Apps has no transport and escaped explicit Bot restrictions.
  // Unit tier: inspect actual thread config; never synthesize or enable a transport/account.
  it.each([true, false])('preserves the native Apps selection (allowed=%s)', (allowed) => {
    expect(buildCodexBotMcpConfigOverrides({
      mode: 'allowlist', configured: allowed ? ['codex_apps'] : [],
      catalog: [{ name: 'codex_apps', source: 'harness', available: true }],
    }, new Set())).toEqual(allowed ? {} : { 'features.apps': false });
    expect(buildCodexBotMcpConfigOverrides(undefined, new Set())).toEqual({});
    expect(buildCodexBotMcpConfigOverrides({ mode: 'inherit', configured: [], catalog: [] }, new Set())).toEqual({});
  });
});

describe('buildCodexSessionCapabilityRoutingPolicy', () => {
  const compatibilityRoute = {
    capabilityId: 'computer-use',
    source: {
      kind: 'harness-plugin',
      harness: 'codex',
      surface: 'skill',
      id: 'computer-use:computer-use',
      artifactId: 'computer-use',
      containerId: 'computer-use@openai-bundled',
    },
    invocation: 'disabled',
  } as const;
  const localReplacementRoute = {
    capabilityId: 'computer-use',
    source: {
      kind: 'harness-plugin',
      harness: 'codex',
      surface: 'plugin',
      id: 'computer-use@openai-bundled',
    },
    invocation: 'disabled',
    replacement: { kind: 'cindy-host', id: 'cindy_computer' },
  } as const;
  const pluginReplacementRoute = {
    capabilityId: 'example',
    source: {
      kind: 'harness-plugin',
      harness: 'codex',
      surface: 'plugin',
      id: 'example@personal',
    },
    invocation: 'disabled',
    replacement: { kind: 'cindy-plugin', id: 'example' },
  } as const;
  const policy = {
    overrides: [
      compatibilityRoute,
      localReplacementRoute,
      pluginReplacementRoute,
    ],
  } as const satisfies CapabilityRoutingPolicy;

  it('preserves local-host replacement arbitration for local Codex', () => {
    expect(buildCodexSessionCapabilityRoutingPolicy(policy, {
      cindyHostReplacementsAvailable: true,
    })).toBe(policy);
  });

  it('removes only unavailable Cindy-host replacements for remote Codex', () => {
    expect(buildCodexSessionCapabilityRoutingPolicy(policy, {
      cindyHostReplacementsAvailable: false,
    })).toEqual({
      overrides: [compatibilityRoute, pluginReplacementRoute],
    });
  });
});

describe('buildCodexCapabilityConfigOverrides', () => {
  it('can fail closed a host MCP server for one thread', () => {
    const policy = {
      overrides: [{
        capabilityId: 'browser-use',
        source: {
          kind: 'harness-plugin' as const,
          harness: 'codex',
          surface: 'mcp' as const,
          id: 'node_repl',
        },
        invocation: 'disabled' as const,
      }],
    };

    expect(buildCodexCapabilityConfigOverrides(policy)).toEqual({
      'mcp_servers.node_repl.enabled': false,
    });
  });

  it('disables the selected Codex plugin with a per-thread config override', () => {
    const policy = {
      overrides: [
        {
          capabilityId: 'computer-use',
          source: {
            kind: 'harness-plugin',
            harness: 'codex',
            surface: 'plugin',
            id: 'computer-use@openai-bundled',
          },
          invocation: 'disabled',
          replacement: {
            kind: 'cindy-host',
            id: 'cindy_computer',
          },
        },
      ],
    } as const satisfies CapabilityRoutingPolicy;

    expect(buildCodexCapabilityConfigOverrides(policy)).toEqual({
      'plugins."computer-use@openai-bundled".enabled': false,
    });
  });

  it('does not widen unsupported or unrelated directives', () => {
    const policy = {
      overrides: [
        {
          capabilityId: 'feishu',
          source: {
            kind: 'harness-plugin',
            harness: 'codex',
            surface: 'mcp',
            id: 'cindy-routed-feishu-delegate',
            artifactId: 'feishu-delegate',
            containerId: 'feishu-delegate@personal',
          },
          invocation: 'explicit-only',
        },
        {
          capabilityId: 'computer-use',
          source: {
            kind: 'harness-plugin',
            harness: 'claude-code',
            surface: 'plugin',
            id: 'computer-use',
          },
          invocation: 'disabled',
        },
        {
          capabilityId: 'computer-use',
          source: {
            kind: 'harness-builtin',
            harness: 'codex',
            surface: 'tool',
            id: 'computer',
          },
          invocation: 'disabled',
        },
        {
          capabilityId: 'computer-use',
          source: {
            kind: 'harness-plugin',
            harness: 'codex',
            surface: 'plugin',
            id: 'computer-use@openai-bundled',
          },
          invocation: 'auto',
        },
      ],
    } as const satisfies CapabilityRoutingPolicy;

    expect(buildCodexCapabilityConfigOverrides(policy)).toEqual({
      'plugins."feishu-delegate@personal".mcp_servers.feishu-delegate.enabled': false,
      'plugins."feishu-delegate@personal".mcp_servers.cindy-routed-feishu-delegate.default_tools_approval_mode':
        'prompt',
    });
  });

  it('quotes plugin ids as safe TOML path segments', () => {
    const policy = {
      overrides: [
        {
          capabilityId: 'example',
          source: {
            kind: 'harness-plugin',
            harness: 'codex',
            surface: 'plugin',
            id: 'plugin\\"quoted',
          },
          invocation: 'disabled',
        },
      ],
    } as const satisfies CapabilityRoutingPolicy;

    expect(buildCodexCapabilityConfigOverrides(policy)).toEqual({
      'plugins."plugin\\\\\\"quoted".enabled': false,
    });
  });

  it('fails closed for explicit-only plugins when the Codex home has no isolated overlay', () => {
    const policy = {
      overrides: [
        {
          capabilityId: 'feishu',
          source: {
            kind: 'harness-plugin',
            harness: 'codex',
            surface: 'skill',
            id: 'feishu-delegate:message-feishu-coworkers',
            artifactId: 'message-feishu-coworkers',
            containerId: 'feishu-delegate@personal',
          },
          invocation: 'explicit-only',
        },
        {
          capabilityId: 'feishu',
          source: {
            kind: 'harness-plugin',
            harness: 'codex',
            surface: 'mcp',
            id: 'cindy-routed-feishu-delegate',
            artifactId: 'feishu-delegate',
            containerId: 'feishu-delegate@personal',
          },
          invocation: 'explicit-only',
        },
        {
          capabilityId: 'computer-use',
          source: {
            kind: 'harness-plugin',
            harness: 'codex',
            surface: 'plugin',
            id: 'computer-use@openai-bundled',
          },
          invocation: 'disabled',
        },
      ],
    } as const satisfies CapabilityRoutingPolicy;

    expect(
      buildCodexCapabilityConfigOverrides(policy, {
        isolatedPluginOverlays: false,
      }),
    ).toEqual({
      'plugins."feishu-delegate@personal".enabled': false,
      'plugins."computer-use@openai-bundled".enabled': false,
    });
  });

  it('fails closed when a remote explicit-only route has no owning plugin id', () => {
    const policy = {
      overrides: [
        {
          capabilityId: 'feishu',
          source: {
            kind: 'harness-plugin',
            harness: 'codex',
            surface: 'skill',
            id: 'feishu-delegate:message-feishu-coworkers',
          },
          invocation: 'explicit-only',
        },
      ],
    } as const satisfies CapabilityRoutingPolicy;

    expect(() =>
      buildCodexCapabilityConfigOverrides(policy, {
        isolatedPluginOverlays: false,
      }),
    ).toThrowError(/source\.containerId is required/);
  });
});

describe('buildCodexCapabilitySkillConfigOverrides', () => {
  const disabledPluginPolicy = {
    overrides: [
      {
        capabilityId: 'computer-use',
        source: {
          kind: 'harness-plugin',
          harness: 'codex',
          surface: 'plugin',
          id: 'computer-use@openai-bundled',
        },
        invocation: 'disabled',
      },
    ],
  } as const satisfies CapabilityRoutingPolicy;

  const disabledSkillPolicy = {
    overrides: [
      {
        capabilityId: 'computer-use',
        source: {
          kind: 'harness-plugin',
          harness: 'codex',
          surface: 'skill',
          id: 'computer-use:computer-use',
          artifactId: 'computer-use',
          containerId: 'computer-use@openai-bundled',
        },
        invocation: 'disabled',
      },
    ],
  } as const satisfies CapabilityRoutingPolicy;

  it('disables matching plugin Skills and preserves existing disabled Skills', () => {
    expect(requiresCodexCapabilitySkillDiscovery(disabledPluginPolicy)).toBe(true);
    expect(buildCodexCapabilitySkillConfigOverrides(disabledPluginPolicy, [
      {
        path: '/Users/dash/.codex/plugins/cache/openai-bundled/computer-use/1.0.0/skills/computer-use/SKILL.md',
        enabled: true,
      },
      {
        path: '/Users/dash/.codex/skills/already-disabled/SKILL.md',
        enabled: false,
      },
      {
        path: '/Users/dash/.codex/plugins/cache/openai-bundled/browser/1.0.0/skills/browser/SKILL.md',
        enabled: true,
      },
    ])).toEqual({
      'skills.config': [
        {
          path: '/Users/dash/.codex/plugins/cache/openai-bundled/computer-use/1.0.0/skills/computer-use/SKILL.md',
          enabled: false,
        },
        {
          path: '/Users/dash/.codex/skills/already-disabled/SKILL.md',
          enabled: false,
        },
      ],
    });
  });

  it('matches remote Windows plugin cache paths without disabling namesakes', () => {
    expect(buildCodexCapabilitySkillConfigOverrides(disabledPluginPolicy, [
      {
        path: 'C:\\Users\\dash\\.codex\\plugins\\cache\\openai-bundled\\computer-use\\1.0.0\\skills\\computer-use\\SKILL.md',
        enabled: true,
      },
      {
        path: 'C:\\Users\\dash\\.codex\\skills\\computer-use\\SKILL.md',
        enabled: true,
      },
    ])).toEqual({
      'skills.config': [{
        path: 'C:\\Users\\dash\\.codex\\plugins\\cache\\openai-bundled\\computer-use\\1.0.0\\skills\\computer-use\\SKILL.md',
        enabled: false,
      }],
    });
  });

  it('matches bundled marketplace snapshots without disabling namesakes', () => {
    const macSnapshotPath =
      '/Users/dash/.codex/.tmp/bundled-marketplaces/openai-bundled/plugins/computer-use/skills/computer-use/SKILL.md';
    const windowsSnapshotPath =
      'C:\\Users\\dash\\.codex\\.tmp\\bundled-marketplaces\\openai-bundled.staging-123\\plugins\\computer-use\\skills\\computer-use\\SKILL.md';
    expect(buildCodexCapabilitySkillConfigOverrides(disabledSkillPolicy, [
      { path: macSnapshotPath, enabled: true },
      { path: windowsSnapshotPath, enabled: true },
      {
        path: '/Users/dash/.codex/skills/computer-use/SKILL.md',
        enabled: true,
      },
    ])).toEqual({
      'skills.config': [
        { path: macSnapshotPath, enabled: false },
        { path: windowsSnapshotPath, enabled: false },
      ],
    });
  });

  it('disables only the selected incompatible Skill without disabling its plugin', () => {
    expect(requiresCodexCapabilitySkillDiscovery(disabledSkillPolicy)).toBe(true);
    expect(buildCodexCapabilityConfigOverrides(disabledSkillPolicy)).toEqual({});
    expect(buildCodexCapabilitySkillConfigOverrides(disabledSkillPolicy, [
      {
        path: '/Users/dash/.codex/plugins/cache/openai-bundled/computer-use/1.0.0/skills/computer-use/SKILL.md',
        enabled: true,
      },
      {
        path: '/Users/dash/.codex/plugins/cache/openai-bundled/computer-use/1.0.0/skills/setup/SKILL.md',
        enabled: true,
      },
      {
        path: '/Users/dash/.codex/skills/computer-use/SKILL.md',
        enabled: true,
      },
    ])).toEqual({
      'skills.config': [{
        path: '/Users/dash/.codex/plugins/cache/openai-bundled/computer-use/1.0.0/skills/computer-use/SKILL.md',
        enabled: false,
      }],
    });
  });

  it('does not replace the base skills config when the plugin contributes no Skills', () => {
    expect(buildCodexCapabilitySkillConfigOverrides(disabledPluginPolicy, [{
      path: '/Users/dash/.codex/skills/already-disabled/SKILL.md',
      enabled: false,
    }])).toEqual({});
  });

  it('fails closed when a disabled plugin id has no marketplace provenance', () => {
    const invalidPolicy = {
      overrides: [{
        capabilityId: 'example',
        source: {
          kind: 'harness-plugin',
          harness: 'codex',
          surface: 'plugin',
          id: 'plugin-without-marketplace',
        },
        invocation: 'disabled',
      }],
    } as const satisfies CapabilityRoutingPolicy;

    expect(() => buildCodexCapabilitySkillConfigOverrides(invalidPolicy, []))
      .toThrowError(/expected plugin id in <name>@<marketplace> form/);
  });

  it('identifies a missing plugin source id in the error', () => {
    const invalidPolicy = {
      overrides: [{
        capabilityId: 'example',
        source: {
          kind: 'harness-plugin',
          harness: 'codex',
          surface: 'plugin',
          id: '',
        },
        invocation: 'disabled',
      }],
    } as const satisfies CapabilityRoutingPolicy;

    expect(() => buildCodexCapabilitySkillConfigOverrides(invalidPolicy, []))
      .toThrowError(/source\.id is required/);
  });

  it('fails closed when a disabled Skill lacks precise plugin provenance', () => {
    const noContainer = {
      overrides: [{
        capabilityId: 'example',
        source: {
          kind: 'harness-plugin',
          harness: 'codex',
          surface: 'skill',
          id: 'example:skill',
          artifactId: 'skill',
        },
        invocation: 'disabled',
      }],
    } as const satisfies CapabilityRoutingPolicy;
    const noArtifact = {
      overrides: [{
        capabilityId: 'example',
        source: {
          kind: 'harness-plugin',
          harness: 'codex',
          surface: 'skill',
          id: 'example:skill',
          containerId: 'example@marketplace',
        },
        invocation: 'disabled',
      }],
    } as const satisfies CapabilityRoutingPolicy;

    expect(() => buildCodexCapabilitySkillConfigOverrides(noContainer, []))
      .toThrowError(/source\.containerId is required/);
    expect(() => buildCodexCapabilitySkillConfigOverrides(noArtifact, []))
      .toThrowError(/source\.artifactId is required/);
  });

  it('skips discovery for unrelated routing policies', () => {
    const unrelatedPolicy = {
      overrides: [{
        capabilityId: 'computer-use',
        source: {
          kind: 'harness-plugin',
          harness: 'claude-code',
          surface: 'plugin',
          id: 'computer-use@openai-bundled',
        },
        invocation: 'disabled',
      }],
    } as const satisfies CapabilityRoutingPolicy;

    expect(requiresCodexCapabilitySkillDiscovery(unrelatedPolicy)).toBe(false);
    expect(buildCodexCapabilitySkillConfigOverrides(unrelatedPolicy, []))
      .toEqual({});
  });
});