Skip to content

Commit f9e3ebe

Browse files
committed
base2 best of n
1 parent eea0862 commit f9e3ebe

File tree

6 files changed

+333
-3
lines changed

6 files changed

+333
-3
lines changed

.agents/base2/base2.ts

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,10 @@ export const createBase2: (
1010
mode: 'fast' | 'max',
1111
options?: {
1212
hasNoValidation?: boolean
13+
bestOfN?: boolean
1314
},
1415
) => Omit<SecretAgentDefinition, 'id'> = (mode, options) => {
15-
const { hasNoValidation = false } = options ?? {}
16+
const { hasNoValidation = false, bestOfN = false } = options ?? {}
1617
const isFast = mode === 'fast'
1718
const isMax = mode === 'max'
1819

@@ -56,6 +57,7 @@ export const createBase2: (
5657
'researcher-web',
5758
'researcher-docs',
5859
'commander',
60+
bestOfN && 'base2-best-of-n-orchestrator',
5961
isMax && 'base2-gpt-5-worker',
6062
'context-pruner',
6163
),
@@ -145,15 +147,18 @@ ${buildArray(
145147
`- Consider spawning other agents or reading more files as needed to gather comprehensive context to answer the user's request.`,
146148
isFast &&
147149
`- Use the write_todos tool to write out your step-by-step implementation plan.${hasNoValidation ? '' : ' You should include at least one step to validate/test your changes: be specific about whether to typecheck, run tests, run lints, etc.'}`,
148-
isFast &&
150+
bestOfN &&
151+
`- You must spawn the base2-best-of-n-orchestrator agent to implement the code changes, since it will generate multiple implementation proposals and select the best one, which the user wants you to do.`,
152+
!bestOfN &&
153+
isFast &&
149154
`- Use the str_replace or write_file tool to make the changes. (Pause after making all the changes to see the tool results of your edits and double check they went through correctly.)`,
150155
isMax &&
151156
`- IMPORTANT: You must spawn a base2-gpt-5-worker agent inline (with spawn_agent_inline tool) to do the planning and editing.`,
152157
!hasNoValidation &&
153158
`- Test your changes${isFast ? ' briefly' : ''} by running appropriate validation commands for the project (e.g. typechecks, tests, lints, etc.). You may have to explore the project to find the appropriate commands. Don't skip this step!`,
154159
`- Inform the user that you have completed the task in one sentence or a few short bullet points. Don't create any markdown summary files, unless asked by the user. If you already finished the user request and said you're done, then don't say anything else.`,
155160
).join('\n')}`,
156-
stepPrompt: `${isMax ? "Keep working until the user's request is completely satisfied. " : ''}After completing the user request, summarize your changes in a sentence or a few short bullet points. Do not create any summary markdown files or example documentation files, unless asked by the user. If you already summarized your changes, then end turn and don't say anything else.`,
161+
stepPrompt: `${isMax ? "Keep working until the user's request is completely satisfied. " : ''}${bestOfN ? "You must spawn the base2-best-of-n-orchestrator agent to implement the code changes. Don't forget to do this!" : ''}After completing the user request, summarize your changes in a sentence or a few short bullet points. Do not create any summary markdown files or example documentation files, unless asked by the user. If you already summarized your changes, then end turn and don't say anything else.`,
157162
handleSteps: function* ({ params }) {
158163
let steps = 0
159164
while (true) {
Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
import type { SecretAgentDefinition } from '../../types/secret-agent-definition'
2+
import { publisher } from '../../constants'
3+
4+
const definition: SecretAgentDefinition = {
5+
id: 'base2-best-of-n-editor',
6+
publisher,
7+
model: 'x-ai/grok-4-fast',
8+
displayName: 'Best-of-N Editor',
9+
spawnerPrompt:
10+
'Parses the selected implementation and applies all code changes',
11+
12+
toolNames: ['str_replace', 'write_file'],
13+
spawnableAgents: [],
14+
15+
inputSchema: {
16+
prompt: {
17+
type: 'string',
18+
description: '',
19+
},
20+
},
21+
outputMode: 'last_message',
22+
23+
instructionsPrompt: `You are the best-of-n editor agent. You have been provided with a selected implementation.
24+
25+
The implementation contains tool calls in the following format:
26+
27+
<codebuff_tool_call>
28+
{
29+
"cb_tool_name": "str_replace",
30+
"path": "path/to/file",
31+
"replacements": [...]
32+
}
33+
</codebuff_tool_call>
34+
35+
OR
36+
37+
<codebuff_tool_call>
38+
{
39+
"cb_tool_name": "write_file",
40+
"path": "path/to/file",
41+
"instructions": "...",
42+
"content": "..."
43+
}
44+
</codebuff_tool_call>
45+
46+
Your task is to:
47+
1. Parse all the tool calls from the implementation text
48+
2. Execute each tool call in order using your str_replace and write_file tools
49+
3. Apply all the changes exactly as specified in the implementation
50+
51+
IMPORTANT: You must execute ALL tool calls from the implementation. Do not skip any changes.
52+
53+
After completing the tool calls with tool results that confirm the changes were applied, please end your turn and do not write anything else.`,
54+
}
55+
56+
export default definition
Lines changed: 115 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,115 @@
1+
import type { SecretAgentDefinition } from '../../types/secret-agent-definition'
2+
import { publisher } from '../../constants'
3+
4+
const definition: SecretAgentDefinition = {
5+
id: 'base2-best-of-n-orchestrator',
6+
publisher,
7+
model: 'anthropic/claude-sonnet-4.5',
8+
displayName: 'Best-of-N Implementation Orchestrator',
9+
spawnerPrompt:
10+
'Orchestrates multiple implementor agents to generate implementation proposals and selects the best one',
11+
12+
includeMessageHistory: true,
13+
inheritParentSystemPrompt: true,
14+
15+
toolNames: ['spawn_agents', 'set_output'],
16+
spawnableAgents: [
17+
'base2-implementor',
18+
'base2-selector',
19+
'base2-best-of-n-editor',
20+
],
21+
22+
inputSchema: {},
23+
outputMode: 'structured_output',
24+
25+
handleSteps: function* ({ logger }) {
26+
// Spawn 5 implementor agents in parallel
27+
const { toolResult: implementorsResult } = yield {
28+
toolName: 'spawn_agents',
29+
input: {
30+
agents: [
31+
{ agent_type: 'base2-implementor' },
32+
{ agent_type: 'base2-implementor' },
33+
{ agent_type: 'base2-implementor' },
34+
{ agent_type: 'base2-implementor' },
35+
{ agent_type: 'base2-implementor' },
36+
],
37+
},
38+
}
39+
40+
// Extract all the plans from the structured outputs
41+
const letters = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ'
42+
// Parse implementations from tool results
43+
const implementations = (implementorsResult ?? [])
44+
.filter((result) => result.type === 'json')
45+
.map(
46+
(result) =>
47+
(result as any).value as { agentType: string; value: string }[],
48+
)
49+
.flatMap((results) =>
50+
results.map((result, index) => ({
51+
id: letters[index],
52+
content: JSON.stringify((result.value as any).value),
53+
})),
54+
)
55+
56+
// Spawn selector with implementations as params
57+
const { toolResult: selectorResult } = yield {
58+
toolName: 'spawn_agents',
59+
input: {
60+
agents: [
61+
{
62+
agent_type: 'base2-selector',
63+
params: { implementations },
64+
},
65+
],
66+
},
67+
}
68+
69+
// Extract chosen implementation from selector output
70+
const selectorOutput =
71+
(selectorResult ?? [])
72+
.filter((result) => result.type === 'json')
73+
.map(
74+
(result) =>
75+
result.value as {
76+
value: { value: { implementationId: string; reasoning: string } }
77+
}[],
78+
)[0][0] || {}
79+
80+
const chosenImplementationId = selectorOutput.value.value.implementationId
81+
const chosenImplementation = implementations.find(
82+
(implementation) => implementation.id === chosenImplementationId,
83+
)
84+
if (!chosenImplementation) {
85+
yield {
86+
toolName: 'set_output',
87+
input: { error: 'Failed to choose an implementation.' },
88+
}
89+
return
90+
}
91+
92+
// Spawn editor to apply the chosen implementation
93+
yield {
94+
toolName: 'spawn_agents',
95+
input: {
96+
agents: [
97+
{
98+
agent_type: 'base2-best-of-n-editor',
99+
prompt: chosenImplementation.content,
100+
},
101+
],
102+
},
103+
}
104+
105+
// Set output with the chosen implementation and reasoning
106+
yield {
107+
toolName: 'set_output',
108+
input: {
109+
implementation: chosenImplementation.content,
110+
},
111+
}
112+
},
113+
}
114+
115+
export default definition
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
import { createBase2 } from '../base2'
2+
import type { SecretAgentDefinition } from '../../types/secret-agent-definition'
3+
4+
const base2 = createBase2('fast', { bestOfN: true })
5+
const definition: SecretAgentDefinition = {
6+
...base2,
7+
id: 'base2-best-of-n',
8+
displayName: 'Buffy Best-of-N Orchestrator',
9+
}
10+
11+
export default definition
Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
import type { SecretAgentDefinition } from '../../types/secret-agent-definition'
2+
import { publisher } from '../../constants'
3+
4+
const definition: SecretAgentDefinition = {
5+
id: 'base2-implementor',
6+
publisher,
7+
model: 'anthropic/claude-sonnet-4.5',
8+
displayName: 'Implementation Generator',
9+
spawnerPrompt:
10+
'Generates a complete implementation plan with all code changes',
11+
12+
includeMessageHistory: true,
13+
inheritParentSystemPrompt: true,
14+
15+
toolNames: [],
16+
spawnableAgents: [],
17+
18+
inputSchema: {},
19+
outputMode: 'last_message',
20+
21+
instructionsPrompt: `You are an implementation generator agent. Your task is to write out ALL the code changes needed to complete the user's request in a single comprehensive response.
22+
23+
Write out what changes you would make using the tool call format below. Use this exact format for each file change:
24+
25+
<codebuff_tool_call>
26+
{
27+
"cb_tool_name": "str_replace",
28+
"path": "path/to/file",
29+
"replacements": [
30+
{
31+
"old": "exact old code",
32+
"new": "exact new code"
33+
},
34+
{
35+
"old": "exact old code 2",
36+
"new": "exact new code 2"
37+
},
38+
]
39+
}
40+
</codebuff_tool_call>
41+
42+
OR for new files or major rewrites:
43+
44+
<codebuff_tool_call>
45+
{
46+
"cb_tool_name": "write_file",
47+
"path": "path/to/file",
48+
"instructions": "What the change does",
49+
"content": "Complete file content or edit snippet"
50+
}
51+
</codebuff_tool_call>
52+
53+
Your implementation should:
54+
- Be complete and comprehensive
55+
- Include all necessary changes to fulfill the user's request
56+
- Follow the project's conventions and patterns
57+
- Be as simple and maintainable as possible
58+
- Reuse existing code wherever possible
59+
- Include proper error handling
60+
- Be well-structured and organized
61+
62+
Write out your complete implementation now, formatting all changes as tool calls as shown above.`,
63+
64+
handleSteps: function* () {
65+
yield 'STEP'
66+
},
67+
}
68+
69+
export default definition
Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
import type { SecretAgentDefinition } from '../../types/secret-agent-definition'
2+
import { publisher } from '../../constants'
3+
4+
const definition: SecretAgentDefinition = {
5+
id: 'base2-selector',
6+
publisher,
7+
model: 'anthropic/claude-sonnet-4.5',
8+
displayName: 'Implementation Selector',
9+
spawnerPrompt:
10+
'Analyzes multiple implementation proposals and selects the best one',
11+
12+
includeMessageHistory: true,
13+
inheritParentSystemPrompt: true,
14+
15+
toolNames: ['set_output'],
16+
spawnableAgents: [],
17+
18+
inputSchema: {
19+
params: {
20+
type: 'object',
21+
properties: {
22+
implementations: {
23+
type: 'array',
24+
items: {
25+
type: 'object',
26+
properties: {
27+
id: { type: 'string' },
28+
content: { type: 'string' },
29+
},
30+
required: ['id', 'content'],
31+
},
32+
},
33+
},
34+
required: ['implementations'],
35+
},
36+
},
37+
outputMode: 'structured_output',
38+
outputSchema: {
39+
type: 'object',
40+
properties: {
41+
reasoning: { type: 'string' },
42+
implementationId: { id: 'string' },
43+
},
44+
required: ['reasoning', 'implementationId'],
45+
},
46+
47+
instructionsPrompt: `You are the implementation selector agent. You have been provided with multiple implementation proposals via params.
48+
49+
The implementations are available in the params.implementations array, where each has:
50+
- id: A unique identifier for the implementation
51+
- content: The full implementation text with tool calls
52+
53+
Your task is to:
54+
1. Analyze each implementation proposal carefully
55+
2. Compare them against the original user requirements
56+
3. Evaluate each based on:
57+
- Correctness and completeness
58+
- Simplicity and maintainability
59+
- Code quality and adherence to project conventions
60+
- Minimal changes to existing code
61+
- Proper reuse of existing helpers and patterns
62+
- Clarity and readability
63+
64+
4. Select the best implementation
65+
5. Call set_output with the selected implementation
66+
67+
Format your set_output call with:
68+
{
69+
"reasoning": "Brief explanation of why this implementation was selected"
70+
"implementationId": "The id of the chosen implementation",
71+
}`,
72+
}
73+
74+
export default definition

0 commit comments

Comments
 (0)