fix: improve log reliability and correlation
This commit is contained in:
1 parent
8eea1cb7a4
commit
8a7ca8a654
21 files changed
+698
-91
No files matched your search
+73
-26
@@ -1566,10 +1566,9 @@ class AgentLoop:
|
||||
try:
|
||||
await delivery.abort_stream()
|
||||
except Exception:
|
||||
logger.debug(
|
||||
logger.opt(exception=log_content).debug(
|
||||
"Could not close stream for cancelled session {}",
|
||||
session_key,
|
||||
exc_info=True,
|
||||
)
|
||||
# An explicit turn stop materializes partial context so
|
||||
# the next prompt can see completed tool results. Gateway
|
||||
@@ -1591,10 +1590,9 @@ class AgentLoop:
|
||||
key,
|
||||
)
|
||||
except Exception:
|
||||
logger.debug(
|
||||
logger.opt(exception=log_content).debug(
|
||||
"Could not restore checkpoint for cancelled session {}",
|
||||
session_key,
|
||||
exc_info=True,
|
||||
)
|
||||
raise
|
||||
except Exception as exc:
|
||||
@@ -1779,15 +1777,18 @@ class AgentLoop:
|
||||
|
||||
ctx.events = EventSink(track_output, ctx.events.accepts)
|
||||
|
||||
await self._run_turn_stage(ctx, "restore", self._restore_turn)
|
||||
await self._run_turn_stage(ctx, "compact", self._compact_session)
|
||||
if await self._run_turn_stage(ctx, "command", self._dispatch_command):
|
||||
with logger.contextualize(turn_id=ctx.turn_id, session_key=ctx.session_key):
|
||||
await self._run_turn_stage(ctx, "restore", self._restore_turn)
|
||||
await self._run_turn_stage(ctx, "compact", self._compact_session)
|
||||
if await self._run_turn_stage(ctx, "command", self._dispatch_command):
|
||||
self._log_turn_completion(ctx, outcome="command")
|
||||
return ctx.outbound
|
||||
await self._run_turn_stage(ctx, "build", self._build_turn)
|
||||
await self._run_turn_stage(ctx, "run", self._run_turn)
|
||||
await self._run_turn_stage(ctx, "save", self._persist_turn)
|
||||
await self._run_turn_stage(ctx, "respond", self._prepare_outbound)
|
||||
self._log_turn_completion(ctx)
|
||||
return ctx.outbound
|
||||
await self._run_turn_stage(ctx, "build", self._build_turn)
|
||||
await self._run_turn_stage(ctx, "run", self._run_turn)
|
||||
await self._run_turn_stage(ctx, "save", self._persist_turn)
|
||||
await self._run_turn_stage(ctx, "respond", self._prepare_outbound)
|
||||
return ctx.outbound
|
||||
|
||||
async def _run_turn_stage(
|
||||
self,
|
||||
@@ -1800,22 +1801,76 @@ class AgentLoop:
|
||||
result = await handler(ctx)
|
||||
except Exception:
|
||||
duration_ms = (time.perf_counter() - started_at) * 1000
|
||||
logger.debug(
|
||||
"[turn {}] Stage {} failed after {:.1f}ms",
|
||||
ctx.turn_id,
|
||||
log_content = not ctx.ephemeral and (
|
||||
ctx.session is None
|
||||
or (ctx.session.policy.persist and ctx.session.policy.log_content)
|
||||
)
|
||||
logger.opt(exception=log_content).bind(
|
||||
event="turn_stage",
|
||||
stage=name,
|
||||
outcome="error",
|
||||
duration_ms=round(duration_ms, 1),
|
||||
).error(
|
||||
"Stage {} failed after {:.1f}ms",
|
||||
name,
|
||||
duration_ms,
|
||||
)
|
||||
raise
|
||||
duration_ms = (time.perf_counter() - started_at) * 1000
|
||||
logger.debug(
|
||||
"[turn {}] Stage {} completed in {:.1f}ms",
|
||||
ctx.turn_id,
|
||||
logger.bind(
|
||||
event="turn_stage",
|
||||
stage=name,
|
||||
outcome="success",
|
||||
duration_ms=round(duration_ms, 1),
|
||||
).debug(
|
||||
"Stage {} completed in {:.1f}ms",
|
||||
name,
|
||||
duration_ms,
|
||||
)
|
||||
return result
|
||||
|
||||
def _log_turn_completion(self, ctx: TurnContext, *, outcome: str | None = None) -> None:
|
||||
duration_ms = ctx.turn_latency_ms
|
||||
if duration_ms is None:
|
||||
duration_ms = max(0, round((time.time() - ctx.turn_wall_started_at) * 1000))
|
||||
runtime = ctx.runtime
|
||||
provider = runtime.provider.provider_name if runtime is not None else None
|
||||
model = runtime.model if runtime is not None else None
|
||||
response_preview = "[content hidden]"
|
||||
if (
|
||||
ctx.kind is TurnKind.USER
|
||||
and ctx.session is not None
|
||||
and not ctx.ephemeral
|
||||
and ctx.session.policy.persist
|
||||
and ctx.session.policy.log_content
|
||||
and ctx.final_content is not None
|
||||
):
|
||||
response_preview = (
|
||||
f"{ctx.final_content[:120]}..."
|
||||
if len(ctx.final_content) > 120
|
||||
else ctx.final_content
|
||||
)
|
||||
final_outcome = outcome or ctx.stop_reason or "completed"
|
||||
logger.bind(
|
||||
event="turn_completed",
|
||||
outcome=final_outcome,
|
||||
duration_ms=duration_ms,
|
||||
provider=provider,
|
||||
model=model,
|
||||
channel=ctx.msg.channel,
|
||||
chat_id=ctx.msg.chat_id,
|
||||
).info(
|
||||
"Turn completed channel={} chat_id={} outcome={} duration_ms={} "
|
||||
"provider={} model={} response={}",
|
||||
ctx.msg.channel,
|
||||
ctx.msg.chat_id,
|
||||
final_outcome,
|
||||
duration_ms,
|
||||
provider or "-",
|
||||
model or "-",
|
||||
response_preview,
|
||||
)
|
||||
|
||||
def _assemble_outbound(
|
||||
self,
|
||||
msg: InboundMessage,
|
||||
@@ -1823,16 +1878,9 @@ class AgentLoop:
|
||||
stop_reason: str,
|
||||
streamed_content: bool,
|
||||
*,
|
||||
log_content: bool = True,
|
||||
turn_latency_ms: int | None = None,
|
||||
) -> OutboundMessage | None:
|
||||
"""Assemble the final outbound message from turn results."""
|
||||
if log_content:
|
||||
preview = final_content[:120] + "..." if len(final_content) > 120 else final_content
|
||||
logger.info("Response to {}:{}: {}", msg.channel, msg.sender_id, preview)
|
||||
else:
|
||||
logger.info("Response to {}:{}: [content hidden]", msg.channel, msg.sender_id)
|
||||
|
||||
event = None
|
||||
meta = dict(msg.metadata or {})
|
||||
if streamed_content and stop_reason not in {"error", "tool_error"}:
|
||||
@@ -2184,7 +2232,6 @@ class AgentLoop:
|
||||
cast(str, ctx.final_content),
|
||||
ctx.stop_reason,
|
||||
ctx.streamed_content,
|
||||
log_content=ctx.require_session().policy.log_content,
|
||||
turn_latency_ms=ctx.turn_latency_ms,
|
||||
)
|
||||
if ctx.ephemeral and ctx.outbound is not None:
|
||||
|
||||
Reference in new issue
Block a user