sdk-python icon indicating copy to clipboard operation
sdk-python copied to clipboard

[BUG] TS tracer: concurrent tool calls leave one local tool trace unended and end the other at the wrong time

Open arronbailiss opened this issue 1 week ago • 1 comments

Human Overview

To be filled in by a human (AI-drafted issue, see team/AI_USAGE_POLICY.md).

Checks

  • [x] I have updated to the lastest minor and patch version of Strands
  • [x] I have checked the documentation and this is not expected behavior
  • [x] I have searched ./issues and there are no duplicates of my issue

SDK Language

TypeScript

Strands Version

main @ 794d37c6e (@strands-agents/sdk 0.0.1-development); also present at 7296caba7

Language Runtime Version

Node.js 24.15.0

Operating System

macOS 26.7.3

Installation Method

git clone

Steps to Reproduce

The tracer keeps a single currentTool local trace. With the concurrent tool executor (the default), two tool calls start before either ends: the first endToolCallSpan ends the second call's trace, and the first call's trace is never ended.

import { Agent, Model, tool, type ModelStreamEvent } from '@strands-agents/sdk'
import { z } from 'zod'

class TwoToolsModel extends Model {
  private _turn = 0
  getConfig() { return { modelId: 'two-tools' } }
  updateConfig() {}
  async *stream(): AsyncGenerator<ModelStreamEvent> {
    yield { type: 'modelMessageStartEvent', role: 'assistant' }
    if (this._turn++ === 0) {
      for (const [id, name] of [['t1', 'slow_a'], ['t2', 'slow_b']]) {
        yield { type: 'modelContentBlockStartEvent', start: { type: 'toolUseStart', toolUseId: id, name } }
        yield { type: 'modelContentBlockDeltaEvent', delta: { type: 'toolUseInputDelta', input: '{}' } }
        yield { type: 'modelContentBlockStopEvent' }
      }
      yield { type: 'modelMessageStopEvent', stopReason: 'toolUse' }
    } else {
      yield { type: 'modelContentBlockStartEvent' }
      yield { type: 'modelContentBlockDeltaEvent', delta: { type: 'textDelta', text: 'done' } }
      yield { type: 'modelContentBlockStopEvent' }
      yield { type: 'modelMessageStopEvent', stopReason: 'endTurn' }
    }
  }
}

const sleep = (ms: number) => new Promise((r) => setTimeout(r, ms))
const slowA = tool({ name: 'slow_a', description: 'a', inputSchema: z.object({}), callback: async () => { await sleep(50); return 'a' } })
const slowB = tool({ name: 'slow_b', description: 'b', inputSchema: z.object({}), callback: async () => { await sleep(100); return 'b' } })

const agent = new Agent({ model: new TwoToolsModel(), tools: [slowA, slowB], printer: false })
const result = await agent.invoke('go')
for (const trace of result.traces ?? []) {
  for (const child of trace.children) {
    if (child.name.startsWith('Tool:')) console.log(trace.name, '>', child.name, 'endTime =', child.endTime, 'duration =', child.duration)
  }
}

Expected Behavior

Both tool traces are ended, slow_a with a duration of about 50 ms and slow_b about 100 ms.

Actual Behavior

Cycle 1 > Tool: slow_a endTime = null duration = 0
Cycle 1 > Tool: slow_b endTime = 1795119458965 duration = 51

slow_a is never ended, and slow_b is ended when slow_a finished, so its duration is wrong too. The OTel spans are unaffected (each call passes its own Span); only the local traces in AgentResult.traces are.

Additional Context

At the pin 7296caba7 the same code is at tracer.ts:429-439 and 495-500.

arronbailiss avatar Nov 20 '26 17:11 arronbailiss

I'd like to pick this up. Reading the current tracer, the single currentTool still gets overwritten when calls overlap.

I'd associate each local trace with its tool call ID and end that specific trace, including on errors and when no OTel span is available. I'd cover both completion orders with controlled tool callbacks, then check the reported behavior through an offline Agent run. The OTel span behavior would stay unchanged.

Could a maintainer confirm this scope before I start a patch? I haven't run the reproducer yet.

Codex helped draft this comment and would assist with implementation and tests.

jjohnnystwsss avatar Nov 22 '26 11:11 jjohnnystwsss