From Flat Logs to Execution Trees: Debugging Modern AI Agents A developer detailed a method for reconstructing execution trees from flat event logs in AI agent debugging, addressing challenges like out-of-order events and incomplete spans. The approach uses stable trace and span identity, explicit assembly state, and diagnostics for anomalies such as duplicate starts or ends. An agent trace is usually written as a sequence of events because append-only data is simple to produce: span started span started span ended span started span ended span ended Developers do not want to debug that sequence directly. They want to see the causal structure: research agent ├─ search web ├─ query database ├─ call finance api │ ├─ attempt 1 timeout │ └─ attempt 2 ok └─ summarize results The event stream is optimized for writing. The execution tree is optimized for understanding. Building a reliable tree requires more than sorting by timestamp: events may arrive out of order, siblings may run concurrently, spans may be incomplete, and retries may fail while the parent operation still succeeds. Every event needs stable trace and span identity. Start events establish parentage; end events establish outcome and duration. type SpanKind = 'run' | 'model' | 'tool' | 'retrieval' | 'decision' | 'fallback'; type TraceEvent = | { event: 'span started'; traceId: string; spanId: string; parentSpanId: string | null; name: string; kind: SpanKind; timestampMs: number; } | { event: 'span ended'; traceId: string; spanId: string; timestampMs: number; status: 'ok' | 'error' | 'cancelled'; errorCategory?: string; metadata?: Record