|
| 1 | +'use strict' |
| 2 | + |
| 3 | +const assert = require('node:assert/strict') |
| 4 | +const guard = require('../startup-guard') |
| 5 | + |
| 6 | +const { SCENARIO, VARIANT } = process.env |
| 7 | +const OPERATIONS = Number(process.env.OPERATIONS) |
| 8 | +const LOCAL_LIFECYCLE_LOOKAHEAD = 4 |
| 9 | + |
| 10 | +const SCENARIOS = { |
| 11 | + compact: { |
| 12 | + turns: 12, |
| 13 | + toolsPerTurn: 4, |
| 14 | + noiseChunks: 0, |
| 15 | + delayedResults: false, |
| 16 | + }, |
| 17 | + 'delayed-noisy': { |
| 18 | + turns: 8, |
| 19 | + toolsPerTurn: 6, |
| 20 | + noiseChunks: 12, |
| 21 | + delayedResults: true, |
| 22 | + }, |
| 23 | +} |
| 24 | + |
| 25 | +function pushNoise (chunks, count, label) { |
| 26 | + for (let idx = 0; idx < count; idx++) { |
| 27 | + chunks.push({ |
| 28 | + type: 'assistant', |
| 29 | + session_id: 'session-1', |
| 30 | + parent_tool_use_id: null, |
| 31 | + message: { |
| 32 | + id: `${label}-noise-${idx}`, |
| 33 | + model: 'claude-sonnet-4-6', |
| 34 | + content: [{ type: 'text', text: `noise ${label}-${idx}` }], |
| 35 | + usage: { input_tokens: 1, output_tokens: 1 }, |
| 36 | + }, |
| 37 | + }) |
| 38 | + } |
| 39 | +} |
| 40 | + |
| 41 | +function pushToolLifecycleChunks (chunks, toolUse, noiseChunks) { |
| 42 | + chunks.push({ type: 'system', subtype: 'task_started', tool_use_id: toolUse.id }) |
| 43 | + |
| 44 | + if (toolUse.name === 'Agent') { |
| 45 | + chunks.push({ |
| 46 | + type: 'assistant', |
| 47 | + session_id: 'session-1', |
| 48 | + parent_tool_use_id: toolUse.id, |
| 49 | + message: { |
| 50 | + id: `subagent-${toolUse.id}`, |
| 51 | + model: 'claude-sonnet-4-6', |
| 52 | + content: [{ |
| 53 | + type: 'tool_use', |
| 54 | + id: `nested-${toolUse.id}`, |
| 55 | + name: 'mcp__local__fetch_weather', |
| 56 | + input: toolUse.input, |
| 57 | + }], |
| 58 | + usage: { input_tokens: 5, output_tokens: 3 }, |
| 59 | + }, |
| 60 | + }) |
| 61 | + } |
| 62 | + |
| 63 | + pushNoise(chunks, noiseChunks, `${toolUse.id}-after-start`) |
| 64 | + chunks.push({ |
| 65 | + type: 'user', |
| 66 | + session_id: 'session-1', |
| 67 | + parent_tool_use_id: null, |
| 68 | + message: { |
| 69 | + role: 'user', |
| 70 | + content: [{ |
| 71 | + type: 'tool_result', |
| 72 | + tool_use_id: toolUse.id, |
| 73 | + content: [{ type: 'text', text: `result ${toolUse.turn}-${toolUse.tool}` }], |
| 74 | + }], |
| 75 | + }, |
| 76 | + }) |
| 77 | +} |
| 78 | + |
| 79 | +function buildFixture (scenario) { |
| 80 | + const chunks = [] |
| 81 | + const hookTools = new Map() |
| 82 | + const toolUses = [] |
| 83 | + |
| 84 | + for (let turn = 0; turn < scenario.turns; turn++) { |
| 85 | + const messageId = `msg-${turn}` |
| 86 | + const pendingToolUses = [] |
| 87 | + |
| 88 | + chunks.push({ |
| 89 | + type: 'assistant', |
| 90 | + session_id: 'session-1', |
| 91 | + parent_tool_use_id: null, |
| 92 | + message: { |
| 93 | + id: messageId, |
| 94 | + model: 'claude-sonnet-4-6', |
| 95 | + content: [{ type: 'text', text: `turn ${turn}` }], |
| 96 | + usage: { input_tokens: 10, output_tokens: 5 }, |
| 97 | + }, |
| 98 | + }) |
| 99 | + |
| 100 | + for (let tool = 0; tool < scenario.toolsPerTurn; tool++) { |
| 101 | + const id = `tool-${turn}-${tool}` |
| 102 | + const name = tool % 3 === 0 ? 'Agent' : 'mcp__local__fetch_weather' |
| 103 | + const input = name === 'Agent' |
| 104 | + ? { description: `subagent ${turn}-${tool}`, prompt: `fetch ${turn}-${tool}` } |
| 105 | + : { location: tool % 2 ? 'CA' : 'NY', units: 'fahrenheit' } |
| 106 | + |
| 107 | + chunks.push({ |
| 108 | + type: 'assistant', |
| 109 | + session_id: 'session-1', |
| 110 | + parent_tool_use_id: null, |
| 111 | + message: { |
| 112 | + id: messageId, |
| 113 | + model: 'claude-sonnet-4-6', |
| 114 | + content: [{ type: 'tool_use', id, name, input }], |
| 115 | + usage: { input_tokens: 10, output_tokens: 5 }, |
| 116 | + }, |
| 117 | + }) |
| 118 | + const scanStartIndex = chunks.length |
| 119 | + pushNoise(chunks, scenario.delayedResults ? scenario.noiseChunks : 0, `${id}-after-use`) |
| 120 | + |
| 121 | + const hookTool = { |
| 122 | + id, |
| 123 | + name, |
| 124 | + input, |
| 125 | + output: { content: `result ${turn}-${tool}` }, |
| 126 | + } |
| 127 | + hookTools.set(id, hookTool) |
| 128 | + const toolUse = { id, name, input, scanStartIndex, turn, tool } |
| 129 | + toolUses.push(toolUse) |
| 130 | + |
| 131 | + if (scenario.delayedResults) { |
| 132 | + pendingToolUses.push(toolUse) |
| 133 | + } else { |
| 134 | + pushToolLifecycleChunks(chunks, toolUse, scenario.noiseChunks) |
| 135 | + } |
| 136 | + } |
| 137 | + |
| 138 | + if (scenario.delayedResults) { |
| 139 | + pushNoise(chunks, scenario.noiseChunks, `turn-${turn}-before-results`) |
| 140 | + for (const toolUse of pendingToolUses) { |
| 141 | + pushToolLifecycleChunks(chunks, toolUse, scenario.noiseChunks) |
| 142 | + } |
| 143 | + } |
| 144 | + } |
| 145 | + |
| 146 | + return { chunks, hookTools, toolUses } |
| 147 | +} |
| 148 | + |
| 149 | +function buildStreamIndex (chunks) { |
| 150 | + const lifecycleByToolId = new Map() |
| 151 | + |
| 152 | + for (let idx = 0; idx < chunks.length; idx++) { |
| 153 | + const chunk = chunks[idx] |
| 154 | + if (chunk.type === 'system' && chunk.subtype === 'task_started') { |
| 155 | + const lifecycle = lifecycleByToolId.get(chunk.tool_use_id) || {} |
| 156 | + lifecycle.taskStartedIndex = idx |
| 157 | + lifecycleByToolId.set(chunk.tool_use_id, lifecycle) |
| 158 | + } else if (chunk.type === 'user') { |
| 159 | + const content = chunk.message.content |
| 160 | + for (const block of content) { |
| 161 | + if (block.type === 'tool_result') { |
| 162 | + const lifecycle = lifecycleByToolId.get(block.tool_use_id) || {} |
| 163 | + lifecycle.toolResultIndex = idx |
| 164 | + lifecycleByToolId.set(block.tool_use_id, lifecycle) |
| 165 | + } |
| 166 | + } |
| 167 | + } |
| 168 | + } |
| 169 | + |
| 170 | + return lifecycleByToolId |
| 171 | +} |
| 172 | + |
| 173 | +function scanLocalLifecycle (chunks, startIndex, toolUseId, lifecycle) { |
| 174 | + lifecycle.taskStartedIndex = undefined |
| 175 | + lifecycle.toolResultIndex = undefined |
| 176 | + |
| 177 | + const scanEnd = Math.min(chunks.length, startIndex + LOCAL_LIFECYCLE_LOOKAHEAD) |
| 178 | + |
| 179 | + for (let idx = startIndex; idx < scanEnd; idx++) { |
| 180 | + const chunk = chunks[idx] |
| 181 | + if (chunk.type === 'system' && chunk.subtype === 'task_started' && chunk.tool_use_id === toolUseId) { |
| 182 | + lifecycle.taskStartedIndex = idx |
| 183 | + } else if (chunk.type === 'user') { |
| 184 | + const content = chunk.message.content |
| 185 | + for (const block of content) { |
| 186 | + if (block.type === 'tool_result' && block.tool_use_id === toolUseId) { |
| 187 | + lifecycle.toolResultIndex = idx |
| 188 | + return lifecycle |
| 189 | + } |
| 190 | + } |
| 191 | + } |
| 192 | + } |
| 193 | + |
| 194 | + return lifecycle |
| 195 | +} |
| 196 | + |
| 197 | +function createStreamLookup (chunks) { |
| 198 | + let streamIndex |
| 199 | + const localLifecycle = {} |
| 200 | + |
| 201 | + return function getLifecycle (startIndex, toolUseId) { |
| 202 | + if (streamIndex) return streamIndex.get(toolUseId) || {} |
| 203 | + |
| 204 | + scanLocalLifecycle(chunks, startIndex, toolUseId, localLifecycle) |
| 205 | + if (localLifecycle.toolResultIndex !== undefined) return localLifecycle |
| 206 | + |
| 207 | + streamIndex = streamIndex || buildStreamIndex(chunks) |
| 208 | + const indexedLifecycle = streamIndex.get(toolUseId) |
| 209 | + return { |
| 210 | + taskStartedIndex: localLifecycle.taskStartedIndex ?? indexedLifecycle?.taskStartedIndex, |
| 211 | + toolResultIndex: indexedLifecycle?.toolResultIndex, |
| 212 | + } |
| 213 | + } |
| 214 | +} |
| 215 | + |
| 216 | +function findTaskStartedScan (chunks, startIndex, toolUseId) { |
| 217 | + for (let idx = startIndex; idx < chunks.length; idx++) { |
| 218 | + const chunk = chunks[idx] |
| 219 | + if (chunk.type === 'system' && chunk.subtype === 'task_started' && chunk.tool_use_id === toolUseId) return idx |
| 220 | + } |
| 221 | +} |
| 222 | + |
| 223 | +function findToolResultScan (chunks, startIndex, toolUseId) { |
| 224 | + for (let idx = startIndex; idx < chunks.length; idx++) { |
| 225 | + const chunk = chunks[idx] |
| 226 | + if (chunk.type !== 'user') continue |
| 227 | + const content = chunk.message.content |
| 228 | + for (const block of content) { |
| 229 | + if (block.type === 'tool_result' && block.tool_use_id === toolUseId) return idx |
| 230 | + } |
| 231 | + } |
| 232 | +} |
| 233 | + |
| 234 | +function runStreamScan (chunks, toolUses) { |
| 235 | + let sink = 0 |
| 236 | + for (const toolUse of toolUses) { |
| 237 | + const taskStartedIndex = findTaskStartedScan(chunks, toolUse.scanStartIndex, toolUse.id) |
| 238 | + const resultIndex = findToolResultScan(chunks, toolUse.scanStartIndex, toolUse.id) |
| 239 | + sink += toolUse.name.length + taskStartedIndex + resultIndex |
| 240 | + } |
| 241 | + return sink |
| 242 | +} |
| 243 | + |
| 244 | +function runHookIndexed (hookTools, getLifecycle, toolUses) { |
| 245 | + let sink = 0 |
| 246 | + for (const toolUse of toolUses) { |
| 247 | + const tool = hookTools.get(toolUse.id) |
| 248 | + const lifecycle = getLifecycle(toolUse.scanStartIndex, toolUse.id) |
| 249 | + const taskStartedIndex = lifecycle.taskStartedIndex |
| 250 | + const resultIndex = lifecycle.toolResultIndex |
| 251 | + sink += tool.name.length + taskStartedIndex + resultIndex |
| 252 | + } |
| 253 | + return sink |
| 254 | +} |
| 255 | + |
| 256 | +const scenario = SCENARIOS[SCENARIO] |
| 257 | +if (scenario === undefined) throw new Error(`Unknown SCENARIO: ${SCENARIO}`) |
| 258 | + |
| 259 | +const { chunks, hookTools, toolUses } = buildFixture(scenario) |
| 260 | +const expected = runStreamScan(chunks, toolUses) |
| 261 | +assert.equal(runHookIndexed(hookTools, createStreamLookup(chunks), toolUses), expected) |
| 262 | + |
| 263 | +let sink = 0 |
| 264 | +guard.loopStart() |
| 265 | +if (VARIANT === 'stream-scan') { |
| 266 | + for (let iteration = 0; iteration < OPERATIONS; iteration++) { |
| 267 | + sink += runStreamScan(chunks, toolUses) |
| 268 | + } |
| 269 | +} else if (VARIANT === 'hook-indexed') { |
| 270 | + for (let iteration = 0; iteration < OPERATIONS; iteration++) { |
| 271 | + sink += runHookIndexed(hookTools, createStreamLookup(chunks), toolUses) |
| 272 | + } |
| 273 | +} else { |
| 274 | + throw new Error(`Unknown VARIANT: ${VARIANT}`) |
| 275 | +} |
| 276 | +guard.done() |
| 277 | + |
| 278 | +assert.ok(sink > 0) |
0 commit comments