Skip to content

[BUG] Agent: "total_turns" log always reads 1 in chat (per-request counter, not session total) #727

Description

@edenreich

Summary

The total_turns field in the agent completion logs always reads 1 in normal back-and-forth chat.
It is populated from the per-request AgentContext.Turns (which resets on every user message)
instead of the session-cumulative counter, so it never reflects the conversation's real turn count.

Steps to Reproduce

  1. infer chat.
  2. Send a message and wait for the reply; send a second message and wait for the reply (two simple
    turns, no tool calls).
  3. Inspect the day's log:
    grep total_turns ~/.infer/logs/app-$(date +%F).log - every completed run logs "total_turns":1,
    including the second (and every later) turn.

Expected Behavior

A field named total_turns should reflect the cumulative number of model turns in the session (e.g. 2
after two conversational turns) - or be renamed so it is clearly a per-request iteration count. The
session already tracks the cumulative value via sessionTurns.

Actual Behavior

Every completed run logs total_turns:1 for simple chat. The value only exceeds 1 within a single run
that loops through tools; it resets on each new user message, so it never accumulates across the
conversation.

Root Cause

  • AgentContext.Turns starts at 0 per agent run (internal/agent/agent_event_driven.go:77) and is
    incremented once per LLM streaming iteration in startStreaming (internal/agent/agent_streaming.go:20).
    A fresh EventDrivenAgent / AgentContext is built for every RunWithStream (i.e. every user
    message), so Turns is per-request and resets each turn - as documented at
    internal/agent/agent.go:41-47.
  • The completion logs label this per-request value total_turns:
    internal/agent/states/completing.go:48 and :80, plus internal/agent/agent_event_driven.go:257
    ("agent reached Idle state - turn complete").
  • The session-cumulative counter that would match "total" - AgentServiceImpl.sessionTurns
    (internal/agent/agent.go:48), incremented alongside Turns at agent_streaming.go:21 - is already
    maintained (it drives reminder cadence via injectDueReminders, agent_utils.go:854) but is never
    surfaced in these logs.

Proposed Fix

Either (observability-only, no behavioural change):

  • Log the session-cumulative count under total_turns (or add a distinct session_turns field sourced
    from sessionTurns) at the completion / reached-Idle log sites, and/or
  • Rename the existing per-request field to something unambiguous (e.g. run_turns / iterations) so it
    is not read as a session total.

Turn limiting is unaffected - MaxTurns continues to gate the per-request Turns.

Environment

inference-gateway/cli, main. Surfaced while reviewing agent logs after #722 / #725 cleared unrelated
log noise.

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't workingreleased

    Type

    Fields

    No fields configured for Bug.

    Projects

    Status
    Done

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions