|
| 1 | +// Copyright (c) 2026 ObjectStack. Licensed under the Apache-2.0 license. |
| 2 | + |
| 3 | +import { describe, it, expect } from 'vitest'; |
| 4 | +import { |
| 5 | + validateAiSurfaceAffinity, |
| 6 | + AI_SKILL_SURFACE_MISMATCH, |
| 7 | +} from './validate-ai-surface-affinity.js'; |
| 8 | + |
| 9 | +describe('validate-ai-surface-affinity', () => { |
| 10 | + // ── The false-positive floor, read per branch (#3806 lesson) ────────────── |
| 11 | + // `examples/` declares zero agents/skills, so it proves nothing about this |
| 12 | + // rule. The floor is instead the real shipped corpus the issue measured: |
| 13 | + // HotCRM's two agents × six skills, none of which declares a `surface` |
| 14 | + // (raw config — no Zod defaults applied). Every binding must pass. |
| 15 | + it('is clean on the HotCRM-shaped corpus (no surface fields anywhere)', () => { |
| 16 | + const stack = { |
| 17 | + agents: [ |
| 18 | + { |
| 19 | + name: 'sales_copilot', |
| 20 | + skills: [ |
| 21 | + 'live_data', |
| 22 | + 'lead_qualification', |
| 23 | + 'email_drafting', |
| 24 | + 'revenue_forecasting', |
| 25 | + 'customer_360', |
| 26 | + ], |
| 27 | + }, |
| 28 | + { |
| 29 | + name: 'service_copilot', |
| 30 | + skills: ['case_triage', 'customer_360', 'email_drafting'], |
| 31 | + }, |
| 32 | + ], |
| 33 | + skills: [ |
| 34 | + { name: 'live_data', tools: ['describe_object'] }, |
| 35 | + { name: 'lead_qualification', tools: ['analyze_lead'] }, |
| 36 | + { name: 'email_drafting', tools: ['generate_email'] }, |
| 37 | + { name: 'revenue_forecasting', tools: ['forecast_revenue'] }, |
| 38 | + { name: 'customer_360', tools: ['search_knowledge'] }, |
| 39 | + { name: 'case_triage', tools: ['triage_case'] }, |
| 40 | + ], |
| 41 | + }; |
| 42 | + expect(validateAiSurfaceAffinity(stack)).toEqual([]); |
| 43 | + }); |
| 44 | + |
| 45 | + it("flags an 'ask' agent referencing a 'build' skill", () => { |
| 46 | + const stack = { |
| 47 | + agents: [{ name: 'helper', surface: 'ask', skills: ['metadata_authoring'] }], |
| 48 | + skills: [{ name: 'metadata_authoring', surface: 'build', tools: [] }], |
| 49 | + }; |
| 50 | + const findings = validateAiSurfaceAffinity(stack); |
| 51 | + expect(findings).toHaveLength(1); |
| 52 | + expect(findings[0]).toMatchObject({ |
| 53 | + severity: 'error', |
| 54 | + rule: AI_SKILL_SURFACE_MISMATCH, |
| 55 | + where: 'agent "helper" · skills', |
| 56 | + path: 'agents[0].skills[0]', |
| 57 | + }); |
| 58 | + expect(findings[0].message).toContain("'ask'"); |
| 59 | + expect(findings[0].message).toContain("'build'"); |
| 60 | + }); |
| 61 | + |
| 62 | + it("flags a 'build' agent referencing an 'ask' skill (symmetric)", () => { |
| 63 | + const stack = { |
| 64 | + agents: [{ name: 'builder', surface: 'build', skills: ['data_explorer'] }], |
| 65 | + skills: [{ name: 'data_explorer', surface: 'ask', tools: [] }], |
| 66 | + }; |
| 67 | + const findings = validateAiSurfaceAffinity(stack); |
| 68 | + expect(findings).toHaveLength(1); |
| 69 | + expect(findings[0].path).toBe('agents[0].skills[0]'); |
| 70 | + }); |
| 71 | + |
| 72 | + it("accepts surface: 'both' on either agent surface", () => { |
| 73 | + const stack = { |
| 74 | + agents: [ |
| 75 | + { name: 'asker', surface: 'ask', skills: ['schema_tools'] }, |
| 76 | + { name: 'builder', surface: 'build', skills: ['schema_tools'] }, |
| 77 | + ], |
| 78 | + skills: [{ name: 'schema_tools', surface: 'both', tools: [] }], |
| 79 | + }; |
| 80 | + expect(validateAiSurfaceAffinity(stack)).toEqual([]); |
| 81 | + }); |
| 82 | + |
| 83 | + it("defaults an absent surface to 'ask' on BOTH sides (mirrors the runtime)", () => { |
| 84 | + // Agent without surface (→ 'ask') referencing a build skill: must flag. |
| 85 | + const stack = { |
| 86 | + agents: [{ name: 'helper', skills: ['metadata_authoring'] }], |
| 87 | + skills: [{ name: 'metadata_authoring', surface: 'build', tools: [] }], |
| 88 | + }; |
| 89 | + expect(validateAiSurfaceAffinity(stack)).toHaveLength(1); |
| 90 | + |
| 91 | + // Skill without surface (→ 'ask') referenced by a build agent: must flag. |
| 92 | + const inverse = { |
| 93 | + agents: [{ name: 'builder', surface: 'build', skills: ['plain'] }], |
| 94 | + skills: [{ name: 'plain', tools: [] }], |
| 95 | + }; |
| 96 | + expect(validateAiSurfaceAffinity(inverse)).toHaveLength(1); |
| 97 | + }); |
| 98 | + |
| 99 | + it('skips references that do not resolve in-stack (kernel/runtime-registered skills)', () => { |
| 100 | + // `ask`/`build` kernel skills (schema_reader, data_explorer, …) register at |
| 101 | + // boot and are statically invisible — resolving them against `stack.skills` |
| 102 | + // alone is #3820 D0/D2 territory, not this rule's. |
| 103 | + const stack = { |
| 104 | + agents: [{ name: 'helper', surface: 'ask', skills: ['schema_reader', 'data_explorer'] }], |
| 105 | + skills: [], |
| 106 | + }; |
| 107 | + expect(validateAiSurfaceAffinity(stack)).toEqual([]); |
| 108 | + }); |
| 109 | + |
| 110 | + it('reports every mismatched binding, with stable paths', () => { |
| 111 | + const stack = { |
| 112 | + agents: [ |
| 113 | + { |
| 114 | + name: 'helper', |
| 115 | + surface: 'ask', |
| 116 | + skills: ['ok_skill', 'authoring', 'unresolved', 'design'], |
| 117 | + }, |
| 118 | + ], |
| 119 | + skills: [ |
| 120 | + { name: 'ok_skill', surface: 'ask', tools: [] }, |
| 121 | + { name: 'authoring', surface: 'build', tools: [] }, |
| 122 | + { name: 'design', surface: 'build', tools: [] }, |
| 123 | + ], |
| 124 | + }; |
| 125 | + const findings = validateAiSurfaceAffinity(stack); |
| 126 | + expect(findings.map((f) => f.path)).toEqual(['agents[0].skills[1]', 'agents[0].skills[3]']); |
| 127 | + }); |
| 128 | + |
| 129 | + it('tolerates junk shapes without throwing', () => { |
| 130 | + expect(validateAiSurfaceAffinity({})).toEqual([]); |
| 131 | + expect(validateAiSurfaceAffinity({ agents: 'nope', skills: 42 } as never)).toEqual([]); |
| 132 | + expect( |
| 133 | + validateAiSurfaceAffinity({ |
| 134 | + agents: [null, { name: 'x', skills: [null, 7, 'y'] }], |
| 135 | + skills: [null, { surface: 'build' }, { name: 'y', surface: 'build' }], |
| 136 | + } as never), |
| 137 | + ).toHaveLength(1); |
| 138 | + }); |
| 139 | +}); |
0 commit comments