claude-code-rev-benchmark / scenarios /correctnessTools.ts
roseking's picture
Add professional benchmark suite
9f5cf6f verified
import type { ToolUseBlock } from '@anthropic-ai/sdk/resources/index.mjs'
import { getDefaultAppState } from '../../src/state/AppStateStore.js'
import { runTools } from '../../src/services/tools/toolOrchestration.js'
import type { CanUseToolFn } from '../../src/hooks/useCanUseTool.js'
import { enqueue, resetCommandQueue } from '../../src/utils/messageQueueManager.js'
import { processQueueIfReady } from '../../src/utils/queueProcessor.js'
import { createAssistantMessage } from '../../src/utils/messages.js'
import type { ToolUseContext } from '../../src/Tool.js'
import type { BenchmarkConfig, RunContext, Scenario, SingleExecution } from '../types.js'
async function runQueueCorrectness(
_context: RunContext,
config: BenchmarkConfig,
): Promise<SingleExecution[]> {
const runs: SingleExecution[] = []
for (let i = 0; i < config.iterations.queueCorrectness; i++) {
resetCommandQueue()
const executedBatches: string[][] = []
enqueue({ value: '/help', mode: 'prompt', priority: 'next' })
enqueue({ value: 'first normal', mode: 'prompt', priority: 'next' })
enqueue({ value: 'second normal', mode: 'prompt', priority: 'next' })
enqueue({ value: 'echo hi', mode: 'bash', priority: 'next' })
const startedAt = performance.now()
while (
processQueueIfReady({
executeInput: async commands => {
executedBatches.push(commands.map(command => String(command.value)))
},
}).processed
) {
// keep draining
}
const durationMs = performance.now() - startedAt
const slashBatch = executedBatches[0] ?? []
const normalBatch = executedBatches[1] ?? []
const bashBatch = executedBatches[2] ?? []
const ok =
slashBatch.length === 1 &&
slashBatch[0] === '/help' &&
normalBatch.length === 2 &&
bashBatch.length === 1
runs.push({
ok,
durationMs,
details: {
executedBatches,
},
error: ok ? undefined : 'queue processor did not preserve expected batching semantics',
})
}
resetCommandQueue()
return runs
}
function createMinimalToolUseContext(): ToolUseContext {
let appState = getDefaultAppState()
let inProgress = new Set<string>()
return {
options: {
commands: [],
debug: false,
mainLoopModel: 'claude-sonnet-4-5',
tools: [],
verbose: false,
thinkingConfig: {
type: 'disabled',
},
mcpClients: [],
mcpResources: {},
isNonInteractiveSession: true,
agentDefinitions: { activeAgents: [], allAgents: [] },
},
abortController: new AbortController(),
readFileState: {} as ToolUseContext['readFileState'],
getAppState: () => appState,
setAppState: updater => {
appState = updater(appState)
},
setInProgressToolUseIDs: updater => {
inProgress = updater(inProgress)
},
setResponseLength: _updater => {},
updateFileHistoryState: _updater => {},
updateAttributionState: _updater => {},
messages: [],
}
}
async function runToolPipeline(
_context: RunContext,
config: BenchmarkConfig,
): Promise<SingleExecution[]> {
const runs: SingleExecution[] = []
for (let i = 0; i < config.iterations.toolPipeline; i++) {
const toolUseContext = createMinimalToolUseContext()
const canUseTool: CanUseToolFn = async (_tool, input) => ({
behavior: 'allow',
updatedInput: input,
})
const toolUseMessages: ToolUseBlock[] = [
{
id: `tool-use-${i}-1`,
name: 'NonExistentToolA',
input: {},
type: 'tool_use',
},
{
id: `tool-use-${i}-2`,
name: 'NonExistentToolB',
input: {},
type: 'tool_use',
},
]
const assistantMessages = toolUseMessages.map(toolUse =>
createAssistantMessage({
content: [toolUse],
}),
)
const startedAt = performance.now()
let yieldedMessages = 0
for await (const update of runTools(
toolUseMessages,
assistantMessages,
canUseTool,
toolUseContext,
)) {
if (update.message) {
yieldedMessages++
}
}
const durationMs = performance.now() - startedAt
const ok = yieldedMessages >= toolUseMessages.length
runs.push({
ok,
durationMs,
details: {
yieldedMessages,
},
error: ok ? undefined : 'tool orchestration produced fewer messages than expected',
})
}
return runs
}
export function createCorrectnessAndToolScenarios(
config: BenchmarkConfig,
): Scenario[] {
return [
{
id: 'B07',
name: 'Slash and Queue Correctness',
category: 'correctness',
description:
'Validate slash command isolation, normal prompt batching, and bash single-item behavior.',
tags: ['queue', 'slash', 'correctness'],
run: context => runQueueCorrectness(context, config),
},
{
id: 'B08',
name: 'Tool Pipeline Execution',
category: 'tools',
description:
'Measure runTools orchestration overhead and ensure deterministic tool-result plumbing.',
tags: ['tools', 'orchestration', 'pipeline'],
run: context => runToolPipeline(context, config),
},
]
}