Log failed chat turns as chat entries

A turn whose agent raised wrote no user_logs row, so it only surfaced as
the agent's system error row. Every finished turn now writes its chat row,
at level error with the error when it failed, and linked to its trace; the
system row for the same traced activity is no longer listed twice.
This commit is contained in:
arc53-machine committed 2026-09-24 00:08:53 +01:00
1 parent 3156ee37a1
commit f2d92bf8df
4 files changed
+203 -39

No files matched your search

+13
View File
@@ -943,6 +943,16 @@ class GetUserLogs(Resource):
"WHERE e.activity_id = s.activity_id "
"AND e.level = 'error'))"
)
# A failed chat turn now writes its own chat row (level
# ``error``) linked to its trace; the agent's error row for
# the same activity would list the failure twice. Rows from
# before execution traces, or with tracing off, have no such
# trace and still show here.
chat_failure_dedupe = (
"NOT EXISTS (SELECT 1 FROM request_traces t "
"WHERE t.activity_id = s.activity_id "
"AND t.source IN ('stream', 'answer', 'v1'))"
)
if api_key_id:
# The owner-scoped lookup gates access, so the
# chat/webhook/system branches match on the agent
@@ -978,6 +988,7 @@ class GetUserLogs(Resource):
"s.level = 'error'",
"COALESCE(s.endpoint, '') NOT IN ('webhook', 'schedule')",
stack_agent_match,
chat_failure_dedupe,
]
# Owner-gated agent match: drop the user clause so a
# shared agent's runs (stamped with the caller's
@@ -1003,6 +1014,7 @@ class GetUserLogs(Resource):
"s.user_id = :user_id",
"s.level = 'error'",
"COALESCE(s.endpoint, '') NOT IN ('webhook', 'schedule')",
chat_failure_dedupe,
]
# Terminal statuses only (worker writes ``success`` /
# ``failed`` / ``timeout`` / ``skipped``; ``completed``
@@ -1233,6 +1245,7 @@ class GetUserLogs(Resource):
"attachments": payload.get("attachments"),
"request_id": payload.get("request_id"),
"message_id": payload.get("message_id"),
"error": payload.get("error"),
}
)
elif m["event_type"] in ("system", "webhook"):