Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 23 additions & 0 deletions changelog.d/599.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
### Added
- **Every Model Reply Carries the Reason It Ended (`devops_cli.ai.client`, `devops_cli.ai.direct`)**:
- `LLMResponse.finish_reason` holds pydantic-ai's `FinishReason` (`stop`, `length`, `content_filter`, `tool_call` or `error`), read from the OpenAI-compatible `finish_reason`, Anthropic's `stop_reason` and Ollama's `done_reason` through three tables in `config/constants.py`. `max_tokens` and `model_context_window_exceeded` become `length`. An absent or unmapped value is `None`, meaning unknown, so truncation is never guessed. `to_model_response` passes the reason on and `from_model_response` reads it (#599).
- `ai.llm.dispatch` and `ai.llm.stream` set `gen_ai.response.finish_reasons` to the provider's reason, and leave it out when the reason is unknown (#599).
- **The Spend Ledger and Review Profiles Count Why Replies Ended (`devops_cli.ai.spend`, `devops review`)**:
- The spend ledger gains a `finish_reason` column. A ledger created before it gains the column in place and keeps its rows. Chat dispatches, streams and the pydantic-ai direct calls record the reason, and observers registered with `observe_llm_calls` receive it with each call (#599).
- Each stage in a review's `profile.json` counts its replies by reason (`finish_reasons`, with `unknown` when the provider gave none) and the replies cut at their token cap by serving backend (`truncated`). When any reply hit the cap, the profile's summary line says how many (#599).

### Changed
- **A Stream Ends Only on Its Provider's Final Frame (`devops ai chat`, `devops_cli.ai.client`)**:
- OpenAI-compatible and Anthropic streams are read with httpx2's `EventSource`, which splits lines on CR and LF alone, and Ollama streams are split on LF from the raw bytes. A delta holding a raw U+2028, which the old line reader split in two and dropped, now arrives intact. Each event, and each unterminated Ollama line, is limited to 1 MiB (`DEFAULT_AI_STREAM_MAX_EVENT_BYTES`), and the whole stream is still limited to 50 MiB. Chunks are still yielded as they arrive (#599).
- A stream is complete on a chunk with a finish reason or `[DONE]` (OpenAI-compatible), `message_stop` (Anthropic) or `done: true` (Ollama). A stream that ends before that, an error frame, and an event stream sent with another content type raise `AIClientError`, where each used to return its partial text as a whole reply. An error frame's message is masked and cut to 256 characters. A stream cut after it yielded output is not replayed on the next Ollama host (#599).
- A stream that fails after yielding output records its spend with finish reason `error`, then re-raises. One that fails before its first chunk records nothing, as a failed non-streamed call records nothing (#599).

### Fixed
- **Cut Replies Are No Longer Cached (`devops_cli.ai.client`)**:
- A reply that ended at its token cap (`length`), was filtered (`content_filter`) or failed (`error`) is returned but not stored in the response cache, so the next identical call reaches the provider instead of the cut reply for 7 days. Replies cached before this change expire as before, or go at once with `devops ai cache clear` (#599).
- **The Dispatch Span No Longer Says Every Reply Stopped (`devops_cli.ai.client`)**:
- `gen_ai.response.finish_reasons` was hard-coded to `["stop"]` on every `ai.llm.dispatch` span (#599).

### Removed
- **Line-Based Stream Helpers (`devops_cli.ai.client`)**:
- `_consume_streaming_lines`, `_read_response_lines`, `_extract_claude_stream_chunk`, `_extract_openai_stream_chunk`, `_extract_ollama_stream_chunk`, `_extract_ollama_stream_tuple` and `_extract_stream_chunk` are replaced by `_read_event_stream`, `_read_ndjson_stream` and one frame parser per provider in `devops_cli.ai.client.streaming`. The five that `devops_cli.ai.client` re-exported are removed from it, with no alias (#599).
5 changes: 5 additions & 0 deletions docs/SELF_IMPROVEMENT.md
Original file line number Diff line number Diff line change
Expand Up @@ -385,6 +385,11 @@ tokens made during each stage, the backends the gateway routed them to, and the
verified and reported finding counts. The profile's session ID is an attribute of the session's
`review.session` span, so a slow stage can be followed into its trace.

Each stage also counts its replies by the reason the provider gave for their end
(`finish_reasons`, with `unknown` when it gave none), and the replies cut at their token cap by
the backend that served them (`truncated`). When any reply hit the cap, the profile's summary line
says how many.

A single review is not a measurement: identical runs produce different numbers of candidate
findings, and verification time follows them. `devops review benchmark <targets> -n 3` reviews the
same files several times with the response cache bypassed and saves the medians under
Expand Down
2 changes: 1 addition & 1 deletion docs/TELEMETRY.md
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ points devops-cli at the cluster's collector.

LLM spans follow the OpenTelemetry GenAI semantic conventions, which live in `open-telemetry/semantic-conventions-genai` and have no tagged release. `src/devops_cli/telemetry/semconv_genai.json` pins them to commit `b31e9e8ea26ac1c086d3313d474e31d7c3f391ae`, resolved with weaver 0.26.1, and lists their current attribute keys, metric names and span types.

- **Inference spans**: `ai.llm.dispatch` and `ai.llm.stream` are CLIENT spans of the `gen_ai.inference.client` type. Both carry `gen_ai.operation.name` (`chat`), `gen_ai.provider.name` (`anthropic` for the `claude` provider, other provider ids unchanged) and `gen_ai.request.model`, plus `server.address` and `server.port` when one backend host served the request. Only `ai.llm.dispatch` carries the reply's usage (`gen_ai.usage.input_tokens`, `gen_ai.usage.output_tokens`), `gen_ai.response.model` and `gen_ai.response.finish_reasons`. `ai.llm.stream` carries `gen_ai.request.stream` and `gen_ai.response.time_to_first_chunk` in seconds.
- **Inference spans**: `ai.llm.dispatch` and `ai.llm.stream` are CLIENT spans of the `gen_ai.inference.client` type. Both carry `gen_ai.operation.name` (`chat`), `gen_ai.provider.name` (`anthropic` for the `claude` provider, other provider ids unchanged) and `gen_ai.request.model`, plus `server.address` and `server.port` when one backend host served the request. Only `ai.llm.dispatch` carries the reply's usage (`gen_ai.usage.input_tokens`, `gen_ai.usage.output_tokens`) and `gen_ai.response.model`. `ai.llm.stream` carries `gen_ai.request.stream` and `gen_ai.response.time_to_first_chunk` in seconds. Both carry `gen_ai.response.finish_reasons` with the reason the provider gave for the reply's end (`stop`, `length`, `content_filter` or `tool_call`), and leave it out when the provider gave none.
- **Wrapper spans**: `ai.llm.chat`, `ai.client.chat_structured` and `pydantic_ai.direct.*` are INTERNAL and set no operation, usage or response keys, so each request is counted once.
- **Private keys**: Anything the conventions do not define is written under `llm.*`, such as `llm.request.priority`, `llm.tokens_per_second` and `llm.usage.cost_usd`, never under `gen_ai.*`.
- **Check**: `tests/test_telemetry_semconv.py` reads every Python file under `src/devops_cli/`, comments and docstrings included. It fails on a quoted string starting with `gen_ai.` that is not a current attribute key or metric name in the snapshot, and on any f-string that builds one.
Expand Down
Loading
Loading