|
| 1 | +import * as fs from 'fs'; |
| 2 | +import * as os from 'os'; |
| 3 | +import * as path from 'path'; |
| 4 | +import { |
| 5 | + agentRunTools, |
| 6 | + buildRegistry, |
| 7 | + parseAgentPrompt, |
| 8 | + resolveTask, |
| 9 | + type AgentPrompt, |
| 10 | + type AgentRegistry, |
| 11 | +} from '../agent-prompt-loader'; |
| 12 | +import { QueueStore } from '../queue'; |
| 13 | + |
| 14 | +function tmpDir(): string { |
| 15 | + return fs.mkdtempSync(path.join(os.tmpdir(), 'agent-loader-test-')); |
| 16 | +} |
| 17 | + |
| 18 | +function registryOf(prompts: AgentPrompt[]): AgentRegistry { |
| 19 | + return buildRegistry( |
| 20 | + prompts.map((p) => ({ ...p, flow: 'test-flow' })), |
| 21 | + 'test-flow', |
| 22 | + ); |
| 23 | +} |
| 24 | + |
| 25 | +describe('parseAgentPrompt', () => { |
| 26 | + const sample = `--- |
| 27 | +type: instrument-events |
| 28 | +model: claude-sonnet-4-6 # cheapest model that succeeds |
| 29 | +skills: [instrument-events] |
| 30 | +allowedTools: [Read, Edit, Grep, Glob, Bash] |
| 31 | +disallowedTools: [enqueue_task] |
| 32 | +dependsOn: [init] |
| 33 | +--- |
| 34 | +
|
| 35 | +## Goal |
| 36 | +Add at least one capture call. |
| 37 | +`; |
| 38 | + |
| 39 | + it('parses frontmatter scalars and inline arrays', () => { |
| 40 | + const p = parseAgentPrompt(sample, 'fallback'); |
| 41 | + expect(p.type).toBe('instrument-events'); |
| 42 | + expect(p.model).toBe('claude-sonnet-4-6'); |
| 43 | + expect(p.skills).toEqual(['instrument-events']); |
| 44 | + expect(p.allowedTools).toEqual(['Read', 'Edit', 'Grep', 'Glob', 'Bash']); |
| 45 | + expect(p.disallowedTools).toEqual(['enqueue_task']); |
| 46 | + expect(p.dependsOn).toEqual(['init']); |
| 47 | + }); |
| 48 | + |
| 49 | + it('strips inline comments and keeps the body', () => { |
| 50 | + const p = parseAgentPrompt(sample, 'fallback'); |
| 51 | + expect(p.model).not.toContain('#'); |
| 52 | + expect(p.body).toContain('## Goal'); |
| 53 | + expect(p.body).not.toContain('---'); |
| 54 | + }); |
| 55 | + |
| 56 | + it('falls back to the menu id when type is omitted', () => { |
| 57 | + const p = parseAgentPrompt('---\nmodel: x\n---\nbody', 'install'); |
| 58 | + expect(p.type).toBe('install'); |
| 59 | + }); |
| 60 | + |
| 61 | + it('parses the flow from frontmatter', () => { |
| 62 | + const p = parseAgentPrompt('---\nflow: audit\n---\nx', 'fix-events'); |
| 63 | + expect(p.flow).toBe('audit'); |
| 64 | + }); |
| 65 | + |
| 66 | + it('marks the seed from frontmatter; everything else is a task', () => { |
| 67 | + expect(parseAgentPrompt('---\nseed: true\n---\nplan', 'planner').seed).toBe( |
| 68 | + true, |
| 69 | + ); |
| 70 | + expect(parseAgentPrompt('---\nmodel: x\n---\nbody', 'install').seed).toBe( |
| 71 | + false, |
| 72 | + ); |
| 73 | + }); |
| 74 | + |
| 75 | + it('defaults missing array fields to empty and model to undefined', () => { |
| 76 | + const p = parseAgentPrompt('no frontmatter at all', 'stub'); |
| 77 | + expect(p.model).toBeUndefined(); |
| 78 | + expect(p.skills).toEqual([]); |
| 79 | + expect(p.dependsOn).toEqual([]); |
| 80 | + expect(p.body).toBe('no frontmatter at all'); |
| 81 | + }); |
| 82 | +}); |
| 83 | + |
| 84 | +describe('agentRunTools', () => { |
| 85 | + it('MCP-qualifies orchestrator tools and passes native tools through', () => { |
| 86 | + const p = parseAgentPrompt( |
| 87 | + '---\nallowedTools: [Read, read_handoffs]\ndisallowedTools: [enqueue_task, complete_task, Bash]\n---\nx', |
| 88 | + 't', |
| 89 | + ); |
| 90 | + const { allowedTools, disallowedTools } = agentRunTools(p); |
| 91 | + expect(allowedTools).toEqual([ |
| 92 | + 'Read', |
| 93 | + 'mcp__posthog-wizard__read_handoffs', |
| 94 | + ]); |
| 95 | + expect(disallowedTools).toEqual([ |
| 96 | + 'mcp__posthog-wizard__enqueue_task', |
| 97 | + 'mcp__posthog-wizard__complete_task', |
| 98 | + 'Bash', |
| 99 | + ]); |
| 100 | + }); |
| 101 | +}); |
| 102 | + |
| 103 | +describe('buildRegistry', () => { |
| 104 | + const prompt = (over: Partial<AgentPrompt>): AgentPrompt => ({ |
| 105 | + type: 'x', |
| 106 | + seed: false, |
| 107 | + skills: [], |
| 108 | + allowedTools: [], |
| 109 | + disallowedTools: [], |
| 110 | + dependsOn: [], |
| 111 | + body: 'b', |
| 112 | + ...over, |
| 113 | + }); |
| 114 | + |
| 115 | + it('scopes to one flow and keeps the seed out of the task types', () => { |
| 116 | + const registry = buildRegistry( |
| 117 | + [ |
| 118 | + prompt({ type: 'plan-audit', flow: 'audit', seed: true }), |
| 119 | + prompt({ type: 'fix-events', flow: 'audit' }), |
| 120 | + prompt({ type: 'install', flow: 'posthog-integration' }), |
| 121 | + prompt({ type: 'example' }), |
| 122 | + ], |
| 123 | + 'audit', |
| 124 | + ); |
| 125 | + expect(registry.types).toEqual(['fix-events']); |
| 126 | + expect(registry.seed?.type).toBe('plan-audit'); |
| 127 | + expect(registry.get('install')).toBeUndefined(); |
| 128 | + // A flowless prompt (e.g. the documentation example) joins no registry. |
| 129 | + expect(registry.get('example')).toBeUndefined(); |
| 130 | + }); |
| 131 | +}); |
| 132 | + |
| 133 | +describe('resolveTask', () => { |
| 134 | + let dir: string; |
| 135 | + let store: QueueStore; |
| 136 | + |
| 137 | + beforeEach(() => { |
| 138 | + dir = tmpDir(); |
| 139 | + store = new QueueStore(dir, 'run-1'); |
| 140 | + }); |
| 141 | + |
| 142 | + afterEach(() => { |
| 143 | + fs.rmSync(dir, { recursive: true, force: true }); |
| 144 | + }); |
| 145 | + |
| 146 | + const prompt: AgentPrompt = { |
| 147 | + type: 'capture', |
| 148 | + seed: false, |
| 149 | + model: 'claude-haiku-4-5-20251001', |
| 150 | + skills: ['instrument-events'], |
| 151 | + allowedTools: ['Read', 'Edit'], |
| 152 | + disallowedTools: ['enqueue_task'], |
| 153 | + dependsOn: ['plan-capture'], |
| 154 | + body: '## Goal\nInstrument the planned events.', |
| 155 | + }; |
| 156 | + |
| 157 | + it('throws when no prompt is registered for the type', () => { |
| 158 | + const registry = registryOf([]); |
| 159 | + const task = { type: 'capture', dependsOn: [] } as never; |
| 160 | + expect(() => resolveTask(registry, task, store)).toThrow(/capture/); |
| 161 | + }); |
| 162 | + |
| 163 | + it('resolves model, tools, and skills from the prompt', () => { |
| 164 | + const registry = registryOf([prompt]); |
| 165 | + const task = store.enqueue({ type: 'capture' }); |
| 166 | + const resolved = resolveTask(registry, task, store); |
| 167 | + expect(resolved.model).toBe('claude-haiku-4-5-20251001'); |
| 168 | + expect(resolved.skills).toEqual(['instrument-events']); |
| 169 | + expect(resolved.disallowedTools).toEqual([ |
| 170 | + 'mcp__posthog-wizard__enqueue_task', |
| 171 | + ]); |
| 172 | + }); |
| 173 | + |
| 174 | + it('prefers the enqueue model override over the prompt model', () => { |
| 175 | + const registry = registryOf([prompt]); |
| 176 | + const task = store.enqueue({ type: 'capture', model: 'override-x' }); |
| 177 | + expect(resolveTask(registry, task, store).model).toBe('override-x'); |
| 178 | + }); |
| 179 | + |
| 180 | + it("appends upstream dependencies' handoffs as context", () => { |
| 181 | + const registry = registryOf([prompt]); |
| 182 | + const dep = store.enqueue({ type: 'plan-capture' }); |
| 183 | + store.complete(dep.id, { |
| 184 | + goals: 'decide events', |
| 185 | + did: 'picked signup and purchase', |
| 186 | + forNextAgent: 'instrument those two', |
| 187 | + }); |
| 188 | + const task = store.enqueue({ |
| 189 | + type: 'capture', |
| 190 | + dependsOn: [dep.id], |
| 191 | + }); |
| 192 | + const resolved = resolveTask(registry, task, store); |
| 193 | + expect(resolved.prompt).toContain('Context from previous steps'); |
| 194 | + expect(resolved.prompt).toContain('picked signup and purchase'); |
| 195 | + expect(resolved.prompt).toContain('instrument those two'); |
| 196 | + }); |
| 197 | + |
| 198 | + it('omits the context section when there are no handoffs', () => { |
| 199 | + const registry = registryOf([prompt]); |
| 200 | + const task = store.enqueue({ type: 'capture' }); |
| 201 | + expect(resolveTask(registry, task, store).prompt).not.toContain( |
| 202 | + 'Context from previous steps', |
| 203 | + ); |
| 204 | + }); |
| 205 | +}); |
0 commit comments