|
| 1 | +import { AgentState } from '@codebuff/common/types/session-state' |
| 2 | +import { ProjectFileContext, FileTreeNode } from '@codebuff/common/util/file' |
| 3 | +import { describe, it, expect, beforeEach, afterEach } from 'bun:test' |
| 4 | + |
| 5 | +import { agentRegistry } from '../templates/agent-registry' |
| 6 | +import { collectParentInstructions } from '../templates/strings' |
| 7 | +import { AgentTemplate } from '../templates/types' |
| 8 | + |
| 9 | +// Helper to create a mock ProjectFileContext |
| 10 | +const createMockFileContext = ( |
| 11 | + agentTemplates: Record<string, string> |
| 12 | +): ProjectFileContext => ({ |
| 13 | + projectRoot: '/test', |
| 14 | + cwd: '/test', |
| 15 | + knowledgeFiles: {}, |
| 16 | + userKnowledgeFiles: {}, |
| 17 | + agentTemplates, |
| 18 | + fileTree: [ |
| 19 | + { |
| 20 | + type: 'directory', |
| 21 | + name: 'test', |
| 22 | + children: [], |
| 23 | + filePath: '/test', |
| 24 | + } as FileTreeNode, |
| 25 | + ], |
| 26 | + fileTokenScores: {}, |
| 27 | + gitChanges: { |
| 28 | + status: '', |
| 29 | + diff: '', |
| 30 | + diffCached: '', |
| 31 | + lastCommitMessages: '', |
| 32 | + }, |
| 33 | + changesSinceLastChat: {}, |
| 34 | + shellConfigFiles: {}, |
| 35 | + systemInfo: { |
| 36 | + platform: 'test', |
| 37 | + shell: 'test', |
| 38 | + nodeVersion: 'test', |
| 39 | + arch: 'test', |
| 40 | + homedir: '/test', |
| 41 | + cpus: 1, |
| 42 | + }, |
| 43 | +}) |
| 44 | + |
| 45 | +describe('Parent Instructions Injection', () => { |
| 46 | + beforeEach(() => { |
| 47 | + agentRegistry.reset() |
| 48 | + }) |
| 49 | + |
| 50 | + afterEach(() => { |
| 51 | + agentRegistry.reset() |
| 52 | + }) |
| 53 | + |
| 54 | + it('should inject parent instructions into userInputPrompt', async () => { |
| 55 | + // Mock file context with agent templates |
| 56 | + const fileContext = createMockFileContext({ |
| 57 | + '.agents/templates/knowledge-keeper.json': JSON.stringify({ |
| 58 | + version: '1.0.0', |
| 59 | + id: 'knowledge-keeper', |
| 60 | + name: 'Knowledge Keeper', |
| 61 | + purpose: 'Test agent', |
| 62 | + model: 'anthropic/claude-4-sonnet-20250522', |
| 63 | + outputMode: 'last_message', |
| 64 | + includeMessageHistory: false, |
| 65 | + toolNames: ['end_turn'], |
| 66 | + spawnableAgents: [], |
| 67 | + parentInstructions: { |
| 68 | + researcher: |
| 69 | + 'Spawn knowledge-keeper when you find documentation gaps.', |
| 70 | + file_picker: 'Spawn knowledge-keeper when you discover missing docs.', |
| 71 | + }, |
| 72 | + systemPrompt: 'You are a test agent.', |
| 73 | + userInputPrompt: 'Process the user request.', |
| 74 | + agentStepPrompt: 'Continue processing.', |
| 75 | + }), |
| 76 | + '.agents/templates/researcher.json': JSON.stringify({ |
| 77 | + version: '1.0.0', |
| 78 | + id: 'researcher', |
| 79 | + name: 'Researcher', |
| 80 | + purpose: 'Research agent', |
| 81 | + model: 'anthropic/claude-4-sonnet-20250522', |
| 82 | + outputMode: 'last_message', |
| 83 | + includeMessageHistory: false, |
| 84 | + toolNames: ['end_turn'], |
| 85 | + spawnableAgents: [], |
| 86 | + systemPrompt: 'You are a researcher.', |
| 87 | + userInputPrompt: 'Research the topic.', |
| 88 | + agentStepPrompt: 'Continue research.', |
| 89 | + }), |
| 90 | + }) |
| 91 | + |
| 92 | + // Initialize the registry |
| 93 | + await agentRegistry.initialize(fileContext) |
| 94 | + |
| 95 | + // Get the researcher template |
| 96 | + const researcherTemplate = agentRegistry.getTemplate( |
| 97 | + 'researcher' |
| 98 | + ) as AgentTemplate |
| 99 | + expect(researcherTemplate).toBeDefined() |
| 100 | + |
| 101 | + // Create mock agent state |
| 102 | + const agentState: AgentState = { |
| 103 | + agentId: 'test-agent', |
| 104 | + agentType: 'researcher', |
| 105 | + agentContext: {}, |
| 106 | + subagents: [], |
| 107 | + messageHistory: [], |
| 108 | + stepsRemaining: 10, |
| 109 | + report: {}, |
| 110 | + } |
| 111 | + |
| 112 | + // Test parent instructions collection directly |
| 113 | + const parentInstructions = await collectParentInstructions( |
| 114 | + 'researcher', |
| 115 | + agentRegistry |
| 116 | + ) |
| 117 | + |
| 118 | + // Verify that parent instructions are collected |
| 119 | + expect(parentInstructions).toHaveLength(1) |
| 120 | + expect(parentInstructions[0]).toBe( |
| 121 | + 'Spawn knowledge-keeper when you find documentation gaps.' |
| 122 | + ) |
| 123 | + }) |
| 124 | + |
| 125 | + it('should not inject parent instructions when none exist', async () => { |
| 126 | + // Mock file context with agent templates without parentInstructions |
| 127 | + const fileContext = createMockFileContext({ |
| 128 | + '.agents/templates/researcher.json': JSON.stringify({ |
| 129 | + version: '1.0.0', |
| 130 | + id: 'researcher', |
| 131 | + name: 'Researcher', |
| 132 | + purpose: 'Research agent', |
| 133 | + model: 'anthropic/claude-4-sonnet-20250522', |
| 134 | + outputMode: 'last_message', |
| 135 | + includeMessageHistory: false, |
| 136 | + toolNames: ['end_turn'], |
| 137 | + spawnableAgents: [], |
| 138 | + systemPrompt: 'You are a researcher.', |
| 139 | + userInputPrompt: 'Research the topic.', |
| 140 | + agentStepPrompt: 'Continue research.', |
| 141 | + }), |
| 142 | + }) |
| 143 | + |
| 144 | + // Initialize the registry |
| 145 | + await agentRegistry.initialize(fileContext) |
| 146 | + |
| 147 | + // Get the researcher template |
| 148 | + const researcherTemplate = agentRegistry.getTemplate( |
| 149 | + 'researcher' |
| 150 | + ) as AgentTemplate |
| 151 | + expect(researcherTemplate).toBeDefined() |
| 152 | + |
| 153 | + // Create mock agent state |
| 154 | + const agentState: AgentState = { |
| 155 | + agentId: 'test-agent', |
| 156 | + agentType: 'researcher', |
| 157 | + agentContext: {}, |
| 158 | + subagents: [], |
| 159 | + messageHistory: [], |
| 160 | + stepsRemaining: 10, |
| 161 | + report: {}, |
| 162 | + } |
| 163 | + |
| 164 | + // Test parent instructions collection directly |
| 165 | + const parentInstructions = await collectParentInstructions( |
| 166 | + 'researcher', |
| 167 | + agentRegistry |
| 168 | + ) |
| 169 | + |
| 170 | + // Verify that no parent instructions are collected |
| 171 | + expect(parentInstructions).toHaveLength(0) |
| 172 | + }) |
| 173 | + |
| 174 | + it('should handle multiple agents with instructions for the same target', async () => { |
| 175 | + // Mock file context with multiple agents having instructions for researcher |
| 176 | + const fileContext = createMockFileContext({ |
| 177 | + '.agents/templates/knowledge-keeper.json': JSON.stringify({ |
| 178 | + version: '1.0.0', |
| 179 | + id: 'knowledge-keeper', |
| 180 | + name: 'Knowledge Keeper', |
| 181 | + purpose: 'Test agent', |
| 182 | + model: 'anthropic/claude-4-sonnet-20250522', |
| 183 | + outputMode: 'last_message', |
| 184 | + includeMessageHistory: false, |
| 185 | + toolNames: ['end_turn'], |
| 186 | + spawnableAgents: [], |
| 187 | + parentInstructions: { |
| 188 | + researcher: 'First instruction for researcher.', |
| 189 | + }, |
| 190 | + systemPrompt: 'You are a test agent.', |
| 191 | + userInputPrompt: 'Process the user request.', |
| 192 | + agentStepPrompt: 'Continue processing.', |
| 193 | + }), |
| 194 | + '.agents/templates/planner.json': JSON.stringify({ |
| 195 | + version: '1.0.0', |
| 196 | + id: 'planner', |
| 197 | + name: 'Planner', |
| 198 | + purpose: 'Planning agent', |
| 199 | + model: 'anthropic/claude-4-sonnet-20250522', |
| 200 | + outputMode: 'last_message', |
| 201 | + includeMessageHistory: false, |
| 202 | + toolNames: ['end_turn'], |
| 203 | + spawnableAgents: [], |
| 204 | + parentInstructions: { |
| 205 | + researcher: 'Second instruction for researcher.', |
| 206 | + }, |
| 207 | + systemPrompt: 'You are a planner.', |
| 208 | + userInputPrompt: 'Plan the task.', |
| 209 | + agentStepPrompt: 'Continue planning.', |
| 210 | + }), |
| 211 | + '.agents/templates/researcher.json': JSON.stringify({ |
| 212 | + version: '1.0.0', |
| 213 | + id: 'researcher', |
| 214 | + name: 'Researcher', |
| 215 | + purpose: 'Research agent', |
| 216 | + model: 'anthropic/claude-4-sonnet-20250522', |
| 217 | + outputMode: 'last_message', |
| 218 | + includeMessageHistory: false, |
| 219 | + toolNames: ['end_turn'], |
| 220 | + spawnableAgents: [], |
| 221 | + systemPrompt: 'You are a researcher.', |
| 222 | + userInputPrompt: 'Research the topic.', |
| 223 | + agentStepPrompt: 'Continue research.', |
| 224 | + }), |
| 225 | + }) |
| 226 | + |
| 227 | + // Initialize the registry |
| 228 | + await agentRegistry.initialize(fileContext) |
| 229 | + |
| 230 | + // Get the researcher template |
| 231 | + const researcherTemplate = agentRegistry.getTemplate( |
| 232 | + 'researcher' |
| 233 | + ) as AgentTemplate |
| 234 | + expect(researcherTemplate).toBeDefined() |
| 235 | + |
| 236 | + // Create mock agent state |
| 237 | + const agentState: AgentState = { |
| 238 | + agentId: 'test-agent', |
| 239 | + agentType: 'researcher', |
| 240 | + agentContext: {}, |
| 241 | + subagents: [], |
| 242 | + messageHistory: [], |
| 243 | + stepsRemaining: 10, |
| 244 | + report: {}, |
| 245 | + } |
| 246 | + |
| 247 | + // Test parent instructions collection directly |
| 248 | + const parentInstructions = await collectParentInstructions( |
| 249 | + 'researcher', |
| 250 | + agentRegistry |
| 251 | + ) |
| 252 | + |
| 253 | + // Verify that both parent instructions are collected |
| 254 | + expect(parentInstructions).toHaveLength(2) |
| 255 | + expect(parentInstructions).toContain('First instruction for researcher.') |
| 256 | + expect(parentInstructions).toContain('Second instruction for researcher.') |
| 257 | + }) |
| 258 | +}) |
0 commit comments