|
| 1 | +// Copyright (c) 2025 ObjectStack. Licensed under the Apache-2.0 license. |
| 2 | + |
| 3 | +import { describe, it, expect } from 'vitest'; |
| 4 | +import stack from '../objectstack.config'; |
| 5 | + |
| 6 | +/** |
| 7 | + * Skill reference guards — every capability a skill CLAIMS must resolve. |
| 8 | + * |
| 9 | + * The defect class: a skill's `tools: [...]` is a list of NAMES. `os |
| 10 | + * validate` and `build` check that each name is a well-formed snake_case |
| 11 | + * string and stop there — nothing resolves it against a tool that exists. |
| 12 | + * At runtime an unresolved name is dropped silently, so the model is handed |
| 13 | + * instructions ("call `search_knowledge` for the policy context") describing |
| 14 | + * a capability it does not have, and improvises. Every instance of this bug |
| 15 | + * shipped green: issue #493 counted eleven fictional tools across six |
| 16 | + * skills, #512 removed ten of them, and `search_knowledge` survived in |
| 17 | + * `customer_360` because nothing in CI could see it. |
| 18 | + * |
| 19 | + * Note the trap in "just define the missing tool": per `ToolSchema`, AI tool |
| 20 | + * metadata is a READ-ONLY PROJECTION for Studio discovery — it has no |
| 21 | + * `implementation` field and no framework executor loads it. A hand-authored |
| 22 | + * `defineTool` record would satisfy a naive existence check while remaining |
| 23 | + * exactly as unrunnable. So the resolvable universe below is deliberately |
| 24 | + * narrow: platform built-ins, and the `action_<name>` tools the runtime |
| 25 | + * materialises from Actions that opted in. |
| 26 | + * |
| 27 | + * Kept in its own file rather than appended to metadata-references.test.ts, |
| 28 | + * which guards UI metadata and is a busy merge surface. |
| 29 | + */ |
| 30 | + |
| 31 | +type AnyRec = Record<string, any>; |
| 32 | +const skills: AnyRec[] = (stack as any).skills ?? []; |
| 33 | +const actions: AnyRec[] = (stack as any).actions ?? []; |
| 34 | +const flows: AnyRec[] = (stack as any).flows ?? []; |
| 35 | +const toolMetadata: AnyRec[] = (stack as any).tools ?? []; |
| 36 | + |
| 37 | +const skillNames = new Set(skills.map((s) => s.name)); |
| 38 | +const flowNames = new Set(flows.map((f) => f.name)); |
| 39 | +const action = (name: string) => actions.find((a) => a.name === name); |
| 40 | + |
| 41 | +/** |
| 42 | + * Tools the AI runtime provides — the only names a skill may reference |
| 43 | + * without anything in this repo defining them. |
| 44 | + * |
| 45 | + * The runtime that serves them ships in the closed cloud package |
| 46 | + * (`@objectstack/service-ai`, ADR-0025 §2), so the open-edition |
| 47 | + * `node_modules` this repo installs cannot be read for the authoritative |
| 48 | + * list. What IS verifiable locally is the MCP bridge in |
| 49 | + * `@objectstack/mcp@16.1.0`, which registers the same registry's data + |
| 50 | + * metadata tools: `list_objects`, `describe_object`, `query_records`, |
| 51 | + * `get_record`, `aggregate_data`, `create_record`, `update_record`, |
| 52 | + * `delete_record`, `list_actions`, `run_action`, `validate_expression`. |
| 53 | + * |
| 54 | + * This list is the READ subset of that surface plus `visualize_data`. |
| 55 | + * Deliberately excluded: the write tools (`create_record` / |
| 56 | + * `update_record` / `delete_record`). HotCRM's state changes go through |
| 57 | + * Actions so that validation, sharing rules and audit match the UI path — |
| 58 | + * a skill reaching for raw record writes is a design regression, and this |
| 59 | + * list is where that gets caught. |
| 60 | + * |
| 61 | + * Adding a name here is a claim that the platform serves it. Make that |
| 62 | + * claim deliberately: an entry that is merely aspirational reintroduces |
| 63 | + * exactly the bug this file exists to prevent. |
| 64 | + */ |
| 65 | +const PLATFORM_TOOLS = new Set([ |
| 66 | + 'list_objects', |
| 67 | + 'describe_object', |
| 68 | + 'query_records', |
| 69 | + 'get_record', |
| 70 | + 'aggregate_data', |
| 71 | + // Charting tool served by the cloud AI runtime's analytics tier. Not in |
| 72 | + // the open-edition bundle (nothing AI-runtime is), verified against the |
| 73 | + // upstream reference-integrity rule when `revenue_forecasting` adopted it |
| 74 | + // in #512. |
| 75 | + 'visualize_data', |
| 76 | +]); |
| 77 | + |
| 78 | +/** `action_<name>` — the tool the runtime materialises from an Action (ADR-0011). */ |
| 79 | +const ACTION_TOOL_PREFIX = 'action_'; |
| 80 | + |
| 81 | +describe('skill tool references resolve', () => { |
| 82 | + it('every skill declares at least one tool', () => { |
| 83 | + expect(skills.length).toBeGreaterThan(0); |
| 84 | + for (const skill of skills) { |
| 85 | + expect(Array.isArray(skill.tools), `${skill.name}: tools must be an array`).toBe(true); |
| 86 | + expect(skill.tools.length, `${skill.name}: declares no tools`).toBeGreaterThan(0); |
| 87 | + } |
| 88 | + }); |
| 89 | + |
| 90 | + it('every tool name resolves to a platform built-in or a materialised Action tool', () => { |
| 91 | + const dangling: string[] = []; |
| 92 | + |
| 93 | + for (const skill of skills) { |
| 94 | + for (const tool of skill.tools as string[]) { |
| 95 | + // Trailing-wildcard subscriptions (`action_*`) match a family of |
| 96 | + // dynamically registered tools; resolve the prefix, not the name. |
| 97 | + if (tool.endsWith('*')) { |
| 98 | + const prefix = tool.slice(0, -1); |
| 99 | + const matches = |
| 100 | + [...PLATFORM_TOOLS].some((t) => t.startsWith(prefix)) || |
| 101 | + (prefix === ACTION_TOOL_PREFIX && actions.length > 0) || |
| 102 | + actions.some((a) => `${ACTION_TOOL_PREFIX}${a.name}`.startsWith(prefix)); |
| 103 | + if (!matches) dangling.push(`${skill.name} → ${tool} (wildcard matches nothing)`); |
| 104 | + continue; |
| 105 | + } |
| 106 | + |
| 107 | + if (PLATFORM_TOOLS.has(tool)) continue; |
| 108 | + |
| 109 | + if (tool.startsWith(ACTION_TOOL_PREFIX)) { |
| 110 | + const actionName = tool.slice(ACTION_TOOL_PREFIX.length); |
| 111 | + if (!action(actionName)) { |
| 112 | + dangling.push(`${skill.name} → ${tool} (no Action named "${actionName}")`); |
| 113 | + } |
| 114 | + continue; |
| 115 | + } |
| 116 | + |
| 117 | + dangling.push(`${skill.name} → ${tool}`); |
| 118 | + } |
| 119 | + } |
| 120 | + |
| 121 | + expect( |
| 122 | + dangling, |
| 123 | + `Skills reference tools that nothing serves:\n ${dangling.join('\n ')}\n` + |
| 124 | + 'Either drop the reference, or route the step through an Action with ' + |
| 125 | + '`ai: { exposed: true, description }`. Authoring `defineTool` metadata ' + |
| 126 | + 'does NOT make a tool runnable (ToolSchema is a read-only projection).', |
| 127 | + ).toEqual([]); |
| 128 | + }); |
| 129 | + |
| 130 | + it('every referenced Action is AI-exposed and has a headless path', () => { |
| 131 | + const referenced = new Set( |
| 132 | + skills |
| 133 | + .flatMap((s) => s.tools as string[]) |
| 134 | + .filter((t) => t.startsWith(ACTION_TOOL_PREFIX) && !t.endsWith('*')) |
| 135 | + .map((t) => t.slice(ACTION_TOOL_PREFIX.length)), |
| 136 | + ); |
| 137 | + |
| 138 | + expect(referenced.size, 'expected at least one Action-backed skill step').toBeGreaterThan(0); |
| 139 | + |
| 140 | + for (const name of referenced) { |
| 141 | + const a = action(name)!; |
| 142 | + |
| 143 | + // ADR-0011: opt-in, default off. Without `exposed` the runtime never |
| 144 | + // materialises the tool, so the reference is dead however real the |
| 145 | + // Action is — the exact regression #512's follow-up commit fixed. |
| 146 | + expect(a.ai?.exposed, `${name}: referenced by a skill but not \`ai.exposed\``).toBe(true); |
| 147 | + expect( |
| 148 | + a.ai?.description?.length ?? 0, |
| 149 | + `${name}: \`ai.exposed\` requires an LLM-facing description (≥40 chars)`, |
| 150 | + ).toBeGreaterThanOrEqual(40); |
| 151 | + |
| 152 | + // A modal Action collects its input from a person and has no headless |
| 153 | + // path, so no tool is generated even when it opts in. Only flow-typed |
| 154 | + // Actions with a resolvable target, or script-typed Actions with a |
| 155 | + // body, can actually run for an agent. |
| 156 | + if (a.type === 'flow') { |
| 157 | + expect(a.target, `${name}: flow-typed Action with no target`).toBeTruthy(); |
| 158 | + expect(flowNames, `${name}: target flow "${a.target}" is not defined`).toContain(a.target); |
| 159 | + } else if (a.type === 'script') { |
| 160 | + expect(a.body?.source, `${name}: script-typed Action with no body`).toBeTruthy(); |
| 161 | + } else { |
| 162 | + expect.fail( |
| 163 | + `${name}: type "${a.type}" has no headless path, so \`${ACTION_TOOL_PREFIX}${name}\` ` + |
| 164 | + 'is never materialised. Point the skill at the button instead of claiming to press it.', |
| 165 | + ); |
| 166 | + } |
| 167 | + } |
| 168 | + }); |
| 169 | + |
| 170 | + it('does not lean on AI tool metadata, which never executes', () => { |
| 171 | + // ToolSchema: "[READ-ONLY PROJECTION — not an execution entry point] |
| 172 | + // Authoring a tool as metadata does NOT make it runnable." Declaring |
| 173 | + // `tools` on the stack to satisfy a dangling reference would restore |
| 174 | + // green CI and none of the behaviour. |
| 175 | + expect( |
| 176 | + toolMetadata.map((t) => t.name), |
| 177 | + 'stack.tools is a Studio-discovery projection with no executor — a skill ' + |
| 178 | + 'cannot call these. Route the capability through an Action instead.', |
| 179 | + ).toEqual([]); |
| 180 | + }); |
| 181 | +}); |
| 182 | + |
| 183 | +describe('skill cross-references resolve', () => { |
| 184 | + it('every skill handed off to in instructions exists', () => { |
| 185 | + // `case_triage` used to hand off to `response_drafting`, a skill that was |
| 186 | + // never defined (issue #493); the real one is `email_drafting`. |
| 187 | + const dangling: string[] = []; |
| 188 | + |
| 189 | + for (const skill of skills) { |
| 190 | + const instructions: string = skill.instructions ?? ''; |
| 191 | + for (const [, referenced] of instructions.matchAll(/`([a-z][a-z0-9_]*)`\s+skill/g)) { |
| 192 | + if (!skillNames.has(referenced)) { |
| 193 | + dangling.push(`${skill.name} → \`${referenced}\` skill`); |
| 194 | + } |
| 195 | + } |
| 196 | + } |
| 197 | + |
| 198 | + expect(dangling, `Instructions hand off to skills that do not exist:\n ${dangling.join('\n ')}`).toEqual([]); |
| 199 | + }); |
| 200 | + |
| 201 | + it('every defined skill is registered on the stack', () => { |
| 202 | + // A skill left out of `src/skills/index.ts` is inert: it validates, it |
| 203 | + // typechecks, and the assistant never sees it. |
| 204 | + expect(skillNames.size, 'duplicate skill names on the stack').toBe(skills.length); |
| 205 | + for (const name of ['live_data', 'lead_qualification', 'email_drafting', 'revenue_forecasting', 'case_triage', 'customer_360']) { |
| 206 | + expect(skillNames, `skill "${name}" is not registered`).toContain(name); |
| 207 | + } |
| 208 | + }); |
| 209 | +}); |
0 commit comments