Skip to content

feat(coderd/x/chatd): open a chat_turn span per prompt - #28972

Closed
jscottmiller wants to merge 1 commit into
scott/chatd-stages/4-provider-attemptsfrom
scott/chatd-stages/5-turn-span
Closed

jscottmiller wants to merge 1 commit into
scott/chatd-stages/4-provider-attemptsfrom
scott/chatd-stages/5-turn-span

Conversation

@jscottmiller

@jscottmiller jscottmiller commented Sep 4, 2026 •

Copy link
Copy Markdown
Contributor

Superseded by #29771. Closed in favor of the reduced stack #29773.

Each prompt runs inside a chat_turn root span owned by the runner (runnerTurnSpan); the generation step, prepare, MCP connect, commit, thinking, tool call, compaction, retry backoff, and queue wait stages are parented under it, and Stages / StageModel are passed to GenerateAssistant, activating the PR 3 stages. chat_turn spans are standalone roots: the turn runs on a worker, often on another replica, and no trace context is persisted.

Anchoring: the span starts at the event that triggered the turn, the later of the last user prompt and a pending compaction request, so the reconstructed acquisition stage falls inside the turn and a /compact turn does not claim idle time. Anchors are monotonic per runner; an older trigger is clamped to the previous anchor and counted as stage_anomalies_total{reason="stale_anchor"}.

Lifecycle:

  • Ensure opens the span on the first generation task and returns a turnToken; token-taking methods no-op once the turn has been replaced. The next Ensure closes an invalidated turn first, so an interrupted turn cannot absorb the prompt promoted after it.
  • Invalidate(token, outcome, err) records how the turn stopped: interrupted (task context done), abandoned (expected exit such as a fence mismatch), error otherwise. Retryable task errors do not invalidate. The span ends with the error and carries turn_outcome; a turn still open at runner teardown is abandoned.
  • Complete marks the turn finished from inside the last step; Settle closes the span after that step's stage ends. When the finishing transition promotes a queued message, Settle opens the next turn anchored at the queue time (FinishTurnResult.PromotedQueuedAt) and records its queue_wait. Server.PromoteQueued records a standalone queue_wait for API-driven promotions.
  • Ensure resolves the chat's organization name (Server.organizationName, one lookup per organization, cached for the server's life) and puts it on the turn context with the chat kind. It is a span attribute on every stage span, not a metric label.
  • The compaction stage is labeled with the model that runs the summary: the override's StageModel when configured, else the chat model's.
  • generation_step no longer carries generation_attempt; the attempt is only known on commit and provider_attempt.
  • Detached background work (inflightContext) strips the span context and runs in background scope, so title, summary, and status label generation are their own trace roots. Summary and status label generation carry the chat kind; title generation runs without a chat and carries none.

ARCHITECTURE.md gains a "Lifecycle tracing" section.

🤖 This PR was generated by Coder Agents on behalf of @jscottmiller.

@jscottmiller
jscottmiller force-pushed the scott/chatd-stages/5-turn-span branch from 02c8b6d to a2ad5b2 Compare September 9, 2026 23:19
@jscottmiller jscottmiller added the experimental Changes that might not necessarily be merged, until its approved to proceed with. label Sep 11, 2026
@jscottmiller
jscottmiller force-pushed the scott/chatd-stages/5-turn-span branch from a2ad5b2 to 3363b52 Compare September 11, 2026 02:26
@jscottmiller
jscottmiller force-pushed the scott/chatd-stages/5-turn-span branch from 3363b52 to 926fabc Compare September 11, 2026 19:17
@jscottmiller jscottmiller removed the experimental Changes that might not necessarily be merged, until its approved to proceed with. label Sep 14, 2026
@jscottmiller
jscottmiller force-pushed the scott/chatd-stages/5-turn-span branch from 926fabc to 8b43bfd Compare September 14, 2026 20:16
@jscottmiller
jscottmiller force-pushed the scott/chatd-stages/5-turn-span branch from 8b43bfd to bf128ab Compare September 14, 2026 22:06
@jscottmiller
jscottmiller force-pushed the scott/chatd-stages/5-turn-span branch from bf128ab to 5f5ba82 Compare September 15, 2026 21:39
@jscottmiller
jscottmiller force-pushed the scott/chatd-stages/5-turn-span branch from 5f5ba82 to 58caa7a Compare September 16, 2026 03:12
@jscottmiller
jscottmiller force-pushed the scott/chatd-stages/5-turn-span branch from 58caa7a to 5660227 Compare September 17, 2026 16:55
@jscottmiller
jscottmiller force-pushed the scott/chatd-stages/5-turn-span branch 2 times, most recently from 1407ab3 to fa51203 Compare September 17, 2026 21:26
@jscottmiller
jscottmiller force-pushed the scott/chatd-stages/5-turn-span branch from fa51203 to 5586c22 Compare September 21, 2026 00:09
@jscottmiller
jscottmiller force-pushed the scott/chatd-stages/5-turn-span branch from 5586c22 to 6e676b0 Compare September 21, 2026 22:52
@linear-code

linear-code Bot commented Sep 22, 2026

Copy link
Copy Markdown

CODAGT-1102

@jscottmiller
jscottmiller force-pushed the scott/chatd-stages/5-turn-span branch from 6e676b0 to e1e09ff Compare September 23, 2026 01:37
The runner owns a runnerTurnSpan that opens a chat_turn root span on
the first generation task of a prompt, anchored at the trigger message,
and hands every task a turn token so a task that outlives its turn
cannot finish the turn that replaced it. A turn closes in two steps:
the finishing transition calls Complete from inside the step, and
Settle closes the span after the step's stage has ended, opening the
next turn from a promoted queued message when there is one. The
generation step, prepare, mcp_connect, commit, compaction, thinking,
tool_call, retry_backoff, acquisition, and queue_wait stages hang off
the turn, and inflight work is detached from it as background scope.
@jscottmiller
jscottmiller force-pushed the scott/chatd-stages/5-turn-span branch from e1e09ff to c39b2af Compare September 23, 2026 02:15
@github-actions github-actions Bot locked and limited conversation to collaborators Sep 23, 2026
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant