From 4a3a9407e49d08f7ce6dc91058d298eca6943858 Mon Sep 17 00:00:00 2001 From: chengyongru Date: Sat, 9 May 2026 17:00:27 +0800 Subject: [PATCH] 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 --- nanobot/agent/loop.py | 10 +++++++--- 1 file changed, 7 insertions(+), 3 deletions(-) diff --git a/nanobot/agent/loop.py b/nanobot/agent/loop.py index ee7f1484d..0a4d09d49 100644 --- a/nanobot/agent/loop.py +++ b/nanobot/agent/loop.py @@ -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) @@ -1212,9 +1213,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, @@ -1253,7 +1255,8 @@ class AgentLoop: ) ) logger.debug( - "State {} took {:.1f}ms -> event {}", + "[turn {}] State {} took {:.1f}ms -> event {}", + ctx.turn_id, ctx.state.name, duration, event, @@ -1262,7 +1265,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