Repository navigation
Description
Summary
mecatl_ttft_seconds (time-to-first-token — the headline user-felt-latency metric) was absent for an entire live multi-turn, tool-using run (no series at all in /metrics; an OTel histogram only materializes after its first observation). The instrument is wired correctly end to end — the bug is that TTFT detection only fires on two display-delta chunk kinds, which the actual provider/model path doesn't always emit.
Root cause (confirmed)
TTFT is anchored by noteContent() in engine/agent/loop.go:1455-1471, which sets timing.ttftMs on the first content chunk. It is called from only two branches of the stream switch:
case port.ChunkText:(loop.go:1495-1496)case port.ChunkReasoning:(loop.go:1499-1503)
It is deliberately not called for ChunkReasoningItem (the encrypted reasoning replay blob, loop.go:1505-1510), ChunkToolCall (loop.go:1516), ChunkPhase, ChunkUsage, or ChunkDone.
The value flows correctly when set: timing.ttftMs → EvTurnEnd.TTFTMs (loop.go:1064) → telemetry records it under a if p.TTFTMs > 0 guard (internal/adapter/telemetry/metrics.go:429-431). A 0 is treated as "not measured" and skipped (engine/session/event.go:425-430).
So on the live path:
- Pure tool-call turns (the bulk of an agentic run) emit only
ChunkToolCall→noteContentnever fires →TTFTMs = 0→ skipped (correct by current design, but it's most turns). - Reasoning delivered as the encrypted replay item (
ChunkReasoningItem, common on OpenRouter / OpenAI Responses paths) → not a display delta →noteContentnever fires.
Net: across the run, noteContent essentially never fires, TTFTMs is 0 on every EvTurnEnd, the >0 guard skips all observations, and the histogram never materializes. The Clock is provably non-nil (turn_duration, which uses the same e.deps.Clock != nil check at loop.go:997, did record), so this is isolated to the chunk-kind coupling.
A test currently encodes the gap: engine/agent/latency_test.go:233-236 asserts a tool-call-first turn yields TTFTMs == 0.
Suggested fix
Anchor TTFT on the first observable output of any kind — add ChunkToolCall (a tool call is user-perceived progress and dominates agentic turns) and ChunkReasoningItem to the noteContent set — while keeping the inter-token gap measurement restricted to genuine streaming deltas (text/reasoning) so smoothness stats stay meaningful. Update the doc-comments (engine/session/event.go:425-441, engine/agent/loop.go:1452-1471) and flip the latency_test.go:233-236 assertion.
Acceptance
- A tool-call-first turn and a reasoning-replay-blob turn both record a non-zero TTFT.
mecatl_ttft_secondsappears for a normal agentic run.- Inter-token gap stats remain delta-only.
Found via live perf monitoring; related observability gap: the latency-quantile issue (histograms degrade to +Inf-only on the Prometheus surface).
Activity
fix(agent): anchor TTFT on first observable output, not only display …