refactor(loop): add turn_id for trace correlation

- TurnContext now carries a turn_id (session_key:time_ns)
- All state transition debug logs include [turn_id] prefix
- RuntimeError messages also include turn_id for observability
This commit is contained in:
chengyongru 2026-05-09 17:00:27 +08:00 committed by Xubin Ren
parent 5327f5e1a0
commit 728d837e4e

View File

@ -209,6 +209,7 @@ class TurnContext:
msg: InboundMessage
session_key: str
state: TurnState
turn_id: str
session: Session | None = None
history: list[dict[str, Any]] = field(default_factory=list)
@ -1267,9 +1268,10 @@ class AgentLoop:
key = session_key or msg.session_key
ctx = TurnContext(
msg=msg,
session=self.sessions.get_or_create(key),
session=None,
session_key=key,
state=TurnState.RESTORE,
turn_id=f"{key}:{time.time_ns()}",
on_progress=on_progress,
on_stream=on_stream,
on_stream_end=on_stream_end,
@ -1308,7 +1310,8 @@ class AgentLoop:
)
)
logger.debug(
"State {} took {:.1f}ms -> event {}",
"[turn {}] State {} took {:.1f}ms -> event {}",
ctx.turn_id,
ctx.state.name,
duration,
event,
@ -1317,7 +1320,8 @@ class AgentLoop:
next_state = self._TRANSITIONS.get((ctx.state, event))
if next_state is None:
raise RuntimeError(
f"No transition from {ctx.state} on event {event!r}"
f"[turn {ctx.turn_id}] No transition from {ctx.state} "
f"on event {event!r}"
)
ctx.state = next_state