Repository navigation
feat: record tier and conversation per turn; osa metrics cost-report (E8.4) - #694
Conversation
…8.4); tested with an old-schema migration
…ver keeps the conversation (E8.4); tested through the routing app
… analysis (E8.4); tested on real rows and an old-schema copy
…; a --since with an offset is converted to UTC (#678); tested on real rows
…, falls back to immutable only on unable-to-open, shows failed requests, reads the default path without creating it (#678); tested on real WAL files
…, UTC offsets, expired analysis sessions (#678)
…cost-per-conversation # Conflicts: # CHANGELOG.md
…handler's plus a middleware stub (#687); tested through the middleware and router
…cost-per-conversation # Conflicts: # CHANGELOG.md
Review round 1 outcomes
Merged |
…cost-per-conversation # Conflicts: # CHANGELOG.md # docs/community-widget.md
…n the reader disconnects first (#687); tested through the ASGI app
… created and the note repeats; a skipped -wal gets its own note; symlinks are judged beside the real file (#678); tested on real files
… through the middleware row (#678); tested on the real route
Review round 2 outcomes
Merged |
…since Rich wraps it on CI's 80-column terminal
Summary
The backend half of E8.4: record which tier and which conversation every chat turn belongs to, and report cost per conversation, quick against analysis. Split from the widget work in #693 because the two together passed the 1500 line limit; they are independent.
request_loggainstier(quick,analysis, or NULL for a request that is not routed) andconversation_id, with an index onconversation_id. The model was already recorded per row. Existing databases are migrated on startup (_MIGRATION_COLUMNS, and the index is created after the migration so an old table does not fail on it).ChatSession.conversation_iddefaults to the session's id. The new session a handover opens inherits the old session's, so the quick row (<request id>:quick), the analysis row and every later turn on the new session,/chat/resumeincluded, share one id. Every_log_streaming_metricscall in the chat stream passestierandconversation_id; a handover's own row is the analysis run's, and an error before the analysis session starts is recorded as quick, since the assistant in hand is the quick model. A streamed request now leaves one row, the handler's: the metrics middleware no longer adds its stub row (no model) beside it (Streamed chat requests log a stub request_log row under the same request_id as the handler's row #687).get_conversation_costs(conn, community_id, since)insrc/metrics/queries.py: per-conversation quick and analysis cost, turns by tier and totals; stub rows (model NULL) are ignored; rows with no conversation id are listed by model. It reads a database from before the new columns existed, since the report does not migrate.osa metrics cost-report --community nemar --since 7d [--db PATH] [--json]: a table of conversation (short id), start, turns quick/analysis, quick cost, analysis cost, total, then totals by tier, and the rows without a conversation id by model. The database is opened read-only (SQLitemode=ro, falling back toimmutable=1for a copy with no WAL files beside it).docs/community-widget.md) andCHANGELOG.mdupdated.Refs #678 (not closing it: the one-week report itself needs a week of use; the follow-up is noted on the issue).
Tests and commands
tests/test_metrics/test_db.py: the migration on an old-schema database (columns added, old rows kept, index created, idempotent)tests/test_api/test_model_routing.py(metered_client): a handover writes a:quickrow and an analysis row sharingconversation_idwith tiers quick/analysis and the two models; a follow-up on the new session stays in the conversation; a/chat/resumerow keeps the analysis session'sconversation_idand tier; a plain quick turn writes one quick row; a community that does not route has a NULL tier; no row with tokens has a NULL modeltests/test_metrics/test_conversation_cost.py: built with reallog_requestcalls and realestimate_cost; stub rows ignored, old rows listed by model, old-schema database readtests/test_cli/test_metrics_cli.py:metrics cost-reportthrough typer'sCliRunner(table,--json,--since, read-only, a copy with no WAL files, old schema, empty window, missing and invalid files)uv run pytest tests/test_metrics tests/test_api/test_model_routing.py tests/test_api/test_streaming_metrics.py tests/test_agents/test_model_routing.py tests/test_frontend tests/test_cli -q: 699 passed, 2 skippeduv run pytest tests/test_api tests/test_core -q: 3288 passed, 11 skippeduv run ruff check . && uv run ruff format --check .: cleanNot done here: running the report against a copy of dev's metrics database. The pull needs ssh through the
hallujump host, which this environment could not authenticate to (no identities in the agent). The command to run, and the follow-up, are on #678.Closes #687