const agentDefinition = {
id: "editor-best-of-n-max",
displayName: "Best-of-N Max Editor",
publisher: "codebuff",
version: "0.0.5",
model: "anthropic/claude-sonnet-4.5",
toolNames: [
"spawn_agents",
"str_replace",
"write_file",
"set_messages",
"set_output"
],
spawnableAgents: [
"codebuff/best-of-n-selector@0.0.14",
"codebuff/best-of-n-selector-opus@0.0.4",
"codebuff/best-of-n-selector-gemini@0.0.5",
"codebuff/editor-implementor@0.0.5",
"codebuff/editor-implementor-opus@0.0.4",
"codebuff/editor-implementor-gemini@0.0.5",
"codebuff/editor-implementor-gpt-5@0.0.5"
],
inputSchema: {
params: {
type: "object",
properties: {
n: {
type: "number",
description: "Number of parallel implementor agents to spawn. Defaults to 4. Use fewer for simple tasks and max of 10 for complex tasks."
}
}
}
},
includeMessageHistory: true,
outputMode: "structured_output",
spawnerPrompt: `Edits code by orchestrating multiple implementor agents to generate implementation proposals, selects the best one, and applies the changes. Do not specify an input prompt for this agent; it inherits the context of the entire conversation with the user. Make sure to read any files intended to be edited before spawning this agent as it cannot read files on its own.`,
systemPrompt: ``,
instructionsPrompt: ``,
stepPrompt: ``,
handleSteps: function* handleStepsMax({
agentState,
params,
logger
}) {
const n = Math.min(10, Math.max(1, params?.n ?? 4)), MAX_MODEL_PATTERN = [
"editor-implementor-opus",
"editor-implementor-opus",
"editor-implementor-gpt-5",
"editor-implementor-opus",
"editor-implementor-opus",
"editor-implementor-gpt-5",
"editor-implementor-opus",
"editor-implementor-opus",
"editor-implementor-opus",
"editor-implementor-opus"
], { messageHistory: initialMessageHistory } = agentState;
let userMessageIndex = initialMessageHistory.length;
while (userMessageIndex > 0)
if (initialMessageHistory[userMessageIndex - 1].role === "user")
userMessageIndex--;
else
break;
yield {
toolName: "set_messages",
input: {
messages: initialMessageHistory.slice(0, userMessageIndex)
},
includeToolCall: !1
};
const implementorAgents = MAX_MODEL_PATTERN.slice(0, n).map((agent_type) => ({
agent_type
})), { toolResult: implementorResults } = yield {
toolName: "spawn_agents",
input: {
agents: implementorAgents
},
includeToolCall: !1
}, spawnedImplementations = extractSpawnResults(implementorResults);
logger.info({ implementorResults, spawnedImplementations }, "spawnedImplementations");
const letters = "ABCDEFGHIJKLMNOPQRSTUVWXYZ", implementations = spawnedImplementations.map((result, index) => ({
id: letters[index],
content: "errorMessage" in result ? `Error: ${result.errorMessage}` : extractLastMessageText(result) ?? ""
})), { toolResult: selectorResult, agentState: selectorAgentState } = yield {
toolName: "spawn_agents",
input: {
agents: [
{
agent_type: "best-of-n-selector-opus",
params: { implementations }
}
]
},
includeToolCall: !1
}, selectorOutput = extractSpawnResults(selectorResult)[0];
if ("errorMessage" in selectorOutput) {
yield {
toolName: "set_output",
input: { error: selectorOutput.errorMessage }
};
return;
}
const { implementationId } = selectorOutput.value, chosenImplementation = implementations.find((implementation) => implementation.id === implementationId);
if (!chosenImplementation) {
yield {
toolName: "set_output",
input: { error: "Failed to find chosen implementation." }
};
return;
}
const numMessagesBeforeStepText = selectorAgentState.messageHistory.length, { agentState: postEditsAgentState } = yield {
type: "STEP_TEXT",
text: chosenImplementation.content
}, { messageHistory } = postEditsAgentState;
yield {
toolName: "set_output",
input: {
messages: messageHistory.slice(numMessagesBeforeStepText)
},
includeToolCall: !1
};
function extractSpawnResults(results) {
if (!results || results.length === 0)
return [];
const jsonResult = results.find((r) => r.type === "json");
if (!jsonResult?.value)
return [];
return (Array.isArray(jsonResult.value) ? jsonResult.value : [jsonResult.value]).map((result) => result?.value).filter(Boolean);
}
function extractLastMessageText(agentOutput) {
if (!agentOutput)
return null;
if (agentOutput.type === "lastMessage" && Array.isArray(agentOutput.value))
for (let i = agentOutput.value.length - 1;i >= 0; i--) {
const message = agentOutput.value[i];
if (message.role === "assistant" && Array.isArray(message.content)) {
for (const part of message.content)
if (part.type === "text" && typeof part.text === "string")
return part.text;
}
}
return null;
}
},
mcpServers: {},
inheritParentSystemPrompt: true
}