From b4ebc312b63f1fb0eebba96a79dd300e8a09c0f0 Mon Sep 17 00:00:00 2001 From: FlopBut Date: Sun, 15 Nov 2026 16:26:17 +0100 Subject: [PATCH 1/2] feat(ingest): sessions, models, tokens and cost for Codex and Grok CLI agents Paperclip's Codex (codex-app-server) and Grok (grok-cli) agents export spans that ingest stored with no session, model or tokens: neither harness uses Claude Code's attribute names, and both emit several spans per event. They never reached the Sessions page and cost $0. - internal/ingest/producers.go: one adapter per harness, chosen by service.name, run after the generic extraction and before cost is derived. Claude Code spans are untouched. - Codex: session from conversation.id, else thread.id when it is a UUID (elsewhere it is the OS thread number); usage from the turn span's codex.turn.token_usage.* with cached input split out; the per-request gen_ai.usage.* copies are not read (they sum to the turn); a tool call is a span with tool_name and call_id. - Grok: session from session_id, model from model_id; tokens only on session.process_conversation_turn, with cache_read_tokens taken out of its inclusive input_tokens; only tool.execution is a tool call. - Prices for gpt-5.6-sol ($4/$0.40/$20) and grok-4.7, grok-4.6 ($2/$0.50/$6), base rates, from third-party rate cards citing the providers. - Session counts (actors, overview, history) ignore an empty session_id, which was counted as one session. Already-stored spans are not rewritten: session_id is an indexed column. Grok still lands on Anonymous (it carries no paperclip.agent.id); Cursor exports nothing. Co-Authored-By: Claude Opus 5.5 --- CHANGELOG.md | 2 + internal/api/handler.go | 6 +- internal/ingest/handler.go | 2 + internal/ingest/producers.go | 137 ++++++++++++++++ internal/ingest/producers_test.go | 233 +++++++++++++++++++++++++++ internal/pricing/prices.go | 33 +++- internal/storage/actors.go | 4 +- internal/storage/actors_page_test.go | 32 ++++ 8 files changed, 441 insertions(+), 8 deletions(-) create mode 100644 internal/ingest/producers.go create mode 100644 internal/ingest/producers_test.go diff --git a/CHANGELOG.md b/CHANGELOG.md index f6ec803..9e43bbc 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -19,6 +19,8 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 - `docker compose build` tags its result `cotel-local:latest` instead of `ghcr.io/flopsstuff/cotel:latest`. Compose treats `image:` next to `build:` as the name to tag the local build with, so the published name was being reused for an artifact that never came from the registry: on the production host — where the deploy is `docker compose build --pull` followed by `up -d`, and `--pull` refreshes the Dockerfile's base images rather than the app image — `Image=ghcr.io/flopsstuff/cotel:latest` named a locally built binary, and built-here and pulled-from-registry were the same string. Three things follow from splitting them. You can read which artifact a host is running off the name. The thing the release pipeline publishes, and which `release.yml` moves only on an explicit dispatch, is no longer shadowed by every merge to `main`. And `docker pull ghcr.io/flopsstuff/cotel:latest` on the production host stops being able to swap the running service's binary at its next restart for content no deploy step validated — it now lands on a name nothing there runs. Production still *builds* its image, deliberately: a pull-only production would mean a merge to `main` no longer changes production, which is a separate decision from this cleanup and would need its own ADR. The operations pages name both images where the distinction matters — the recovery procedure's "match the CLI to the version linked into cotel" step asks `cotel-local:latest` on a host that builds its own image — and they also record that `ghcr.io/flopsstuff/cotel:latest` is now a multi-arch index whose `linux/arm64` entry runs natively on a Pi or Apple Silicon, so the documented `docker run --entrypoint /usr/local/bin/cotel … --db-query` and `--db-import` commands are honest on arm64 without a local build ([docs/operations/duckdb-recovery.md](docs/operations/duckdb-recovery.md), [docs/operations/duckdb-snapshots.md](docs/operations/duckdb-snapshots.md)) ### Fixed +- **Codex and Grok CLI agents get sessions, models, tokens and cost.** Paperclip's Codex (`codex-app-server`) and Grok (`grok-cli`) agents were exporting spans that ingest stored with no session, so they never reached the Sessions page, and with no tokens or model, so they cost $0. Neither harness uses Claude Code's attribute names, and both emit several spans per event. `internal/ingest/producers.go` adds an adapter per harness, chosen by `service.name` and applied only to those spans. Claude Code spans are untouched. For Codex, the session comes from `conversation.id`, or from `thread.id` when it is a UUID (on other spans it is the OS thread number). Usage comes from the turn span's `codex.turn.token_usage.*`, with cached input split out. The per-request `gen_ai.usage.*` copies are not read, since they sum to the turn. A tool call is a span with both `tool_name` and `call_id`. For Grok, the session comes from `session_id` and the model from `model_id`. Tokens are kept only on `session.process_conversation_turn`, with `cache_read_tokens` taken out of its inclusive `input_tokens`, because `sampling.stream_decode` repeats the output count. Only `tool.execution` counts as a tool call, not `tool.decision`. Prices are added for `gpt-5.6-sol` ($4 input / $0.40 cached / $20 output) and `grok-4.7` / `grok-4.6` ($2 / $0.50 / $6), at base rates, taken from third-party rate cards that cite the providers. Grok still lands on Anonymous: it does not carry `paperclip.agent.id` (ADR-0029). Cursor exports no telemetry at all. Already-stored spans are not rewritten, because `session_id` is an indexed column (see the ART incident). +- Actor and overview session counts no longer count a span stored without a session id as a session. An actor whose only spans were sessionless showed one session on Actors and none on Sessions. - Spans from the Claude 5.5 / 5.2 generation are priced. From 2026-11-07 every span from `claude-opus-5-5`, `claude-fable-5-2` and `claude-haiku-5-5` was stored with `cost_usd` unset because the price table did not know them, so the dashboard showed $0 for a week of real traffic. Adds Opus 5.5 ($4/$20), Sonnet 5.5 ($2/$10), Haiku 5.5 ($0.10/$0.50; its $0.50/$2.50 rate for prompts over 100K tokens is not modelled, since a span does not say which tier billed it), Fable and Mythos 5.2 ($8/$40) and 5.1 ($10/$50), with the published cache-read rates where they depart from 0.1 x input. Sonnet 5 moves from $3/$15 to the $2/$10 Anthropic still publishes. `pricing.Known` tells an unpriced model from a priced span that costs zero. Already-stored spans keep their unset cost until `--backfill-cost-apply` runs - The Deploy workflow stops asking the production host's GUI keychain for credentials it does not hold, and stops being able to hold the deploy queue indefinitely when something does hang. `docker compose build --pull` makes buildkit resolve the Dockerfile frontend and the base images, which makes the docker CLI consult the credential store named in the host's `~/.docker/config.json`. On the production runner that store is `desktop`, whose helper is tied to the logged-in GUI session: spawned from the runner's launchd session it blocks in `open(2)` and never returns, buildkit re-spawns it on every retry, and a single hung call also stalls image resolution for any other build on that host (measured: a hand build from an interactive shell, normally 2.6 s, sat 4 min behind one). Nothing in the run log said so, because the hang lands before the first layer and the step's last line is the frontend resolve. The same helper call from an interactive shell on the same host answers in milliseconds, so both the hosted-runner smoke build and a hand build on the deploy host passed throughout. The job now builds against a copy of the config carrying the host's contexts and CLI plugins but no credential store at all, which is faithful to what the host actually has: its `auths` map is empty, and registry auth, if it is ever needed there, belongs in an explicit `docker login` with a repository secret rather than in one person's keychain. The deploy job also gains `timeout-minutes: 20`. The first wedge ran for nine hours and, because `deploy-main` has `cancel-in-progress: false`, every push behind it sat `pending` with no job at all, which is indistinguishable from an idle queue: production kept serving a build five days old, the next runtime fix would silently not have shipped, and no run ever went red to say so. A hung build now fails loudly and releases the group instead. diff --git a/internal/api/handler.go b/internal/api/handler.go index 02f4b16..fb74914 100644 --- a/internal/api/handler.go +++ b/internal/api/handler.go @@ -551,7 +551,7 @@ func (h *Handler) handleOverview(w http.ResponseWriter, r *http.Request) { // it; it is one principal on the actors list and counts as one here too. if err := h.db.QueryRow(cte+` SELECT - COUNT(DISTINCT session_id), + COUNT(DISTINCT NULLIF(session_id, '')), COUNT(DISTINCT actor_id) + CASE WHEN COUNT(*) FILTER (WHERE actor_id IS NULL) > 0 THEN 1 ELSE 0 END, COALESCE(SUM(cost), 0), COALESCE(SUM(input_tokens), 0), @@ -1685,7 +1685,7 @@ func (h *Handler) historyRawSeries(resp *historyResponse, r *http.Request, gran brows, err := h.db.Query(fmt.Sprintf(` SELECT %s AS bucket, - COUNT(DISTINCT session_id) AS sessions, + COUNT(DISTINCT NULLIF(session_id, '')) AS sessions, COUNT(*) AS spans, COALESCE(SUM(cost_usd), 0) AS cost_usd, COALESCE(SUM(input_tokens), 0) AS input_tokens, @@ -1744,7 +1744,7 @@ func (h *Handler) historyUnionSeries(resp *historyResponse, r *http.Request, gra brows, err := h.db.Query(cte+fmt.Sprintf(` SELECT %s AS bucket, - COUNT(DISTINCT session_id) AS sessions, + COUNT(DISTINCT NULLIF(session_id, '')) AS sessions, CAST(SUM(spans) AS BIGINT) AS spans, COALESCE(SUM(cost), 0) AS cost_usd, COALESCE(SUM(input_tokens), 0) AS input_tokens, diff --git a/internal/ingest/handler.go b/internal/ingest/handler.go index 36bea80..5799da8 100644 --- a/internal/ingest/handler.go +++ b/internal/ingest/handler.go @@ -225,6 +225,8 @@ func decodeSpan(sp *tracepb.Span, svcName string, resAttrs map[string]any, resJS s.CostUSD = &f } + normalizeProducer(svcName, sp.Name, attrs, &s) + // Derive cost from token counts when the producer did not supply cost_usd. // Claude Code beta telemetry omits the field; we compute it ingest-time so // the stored row is always queryable without a per-query price JOIN. diff --git a/internal/ingest/producers.go b/internal/ingest/producers.go new file mode 100644 index 0000000..1cb8e21 --- /dev/null +++ b/internal/ingest/producers.go @@ -0,0 +1,137 @@ +package ingest + +import ( + "fmt" + "strings" + + "github.com/Flopsstuff/cotel/internal/storage" +) + +// Producer adapters map the span attributes of harnesses other than Claude Code +// onto the columns decodeSpan fills from Claude Code's names. Each one was +// written against what the harness actually exports through Paperclip, not +// against a spec: neither harness follows a published convention for sessions +// or usage, and both emit several spans per logical event, so the rules below +// also choose which span carries a count, to keep sums from double counting. +// +// They run after the generic extraction and before cost is derived, and only +// for spans whose service.name names the harness. Claude Code spans are never +// touched. + +// normalizeProducer applies the adapter for svcName, if there is one. +func normalizeProducer(svcName, spanName string, attrs map[string]any, s *storage.Span) { + svc := strings.ToLower(svcName) + switch { + case strings.HasPrefix(svc, "codex"): + normalizeCodex(attrs, s) + case strings.HasPrefix(svc, "grok"): + normalizeGrok(spanName, attrs, s) + } +} + +// Codex (service.name "codex-app-server", Rust tracing over OTLP). +// +// - Session: conversation.id on exec spans; on the turn span and the tool +// handler spans the same id travels as thread.id. thread.id is also the +// tracing layer's OS thread number (an integer) on every other span, so it +// counts only when it is a UUID string. +// - Usage: the turn span (session_task.turn) carries the turn's totals under +// codex.turn.token_usage.*, next to model. The per-request handle_responses +// spans repeat the same tokens as gen_ai.usage.* without a model; they sum +// to the turn total, so they are not read. input_tokens includes the cached +// part; non_cached_input_tokens is what bills at the input rate. +// - Tools: a tool invocation is the span with both tool_name and call_id +// (exec, exec_command). code_mode.broker.invoke_tool names the same nested +// call again without a call_id and is not counted. +func normalizeCodex(attrs map[string]any, s *storage.Span) { + if s.SessionID == "" { + if v := str(attrs["conversation.id"]); v != "" { + s.SessionID = v + } else if v, ok := attrs["thread.id"].(string); ok && isUUID(v) { + s.SessionID = v + } + } + + const p = "codex.turn.token_usage." + if _, ok := attrs[p+"input_tokens"]; ok { + total := toInt64(attrs[p+"input_tokens"]) + cached := toInt64(attrs[p+"cached_input_tokens"]) + in := total - cached + if v, ok := attrs[p+"non_cached_input_tokens"]; ok { + in = toInt64(v) + } + s.InputTokens = ptr(max(in, 0)) + s.CacheReadTokens = ptr(cached) + s.CacheWriteTokens = ptr(toInt64(attrs[p+"cache_write_input_tokens"])) + s.OutputTokens = ptr(toInt64(attrs[p+"output_tokens"])) + } + + if s.ToolName != "" && str(attrs["call_id"]) == "" { + s.ToolName = "" + } +} + +// Grok CLI (service.name "grok-cli"). +// +// - Session: session_id; model: model_id. +// - Usage: session.process_conversation_turn carries the turn's totals. +// sampling.stream_decode repeats output_tokens per sample and would double +// the turn's output, so token counts are kept on the turn span only. +// input_tokens includes cache_read_tokens (OpenAI-style accounting), so the +// cached part is taken out of the input column. +// - Tools: tool.execution is the call; tool.decision is the permission check +// before it and names the same tool. +func normalizeGrok(spanName string, attrs map[string]any, s *storage.Span) { + if s.SessionID == "" { + s.SessionID = str(attrs["session_id"]) + } + if s.Model == "" { + s.Model = str(attrs["model_id"]) + } + + if spanName == "session.process_conversation_turn" { + if s.InputTokens != nil && s.CacheReadTokens != nil { + s.InputTokens = ptr(max(*s.InputTokens-*s.CacheReadTokens, 0)) + } + } else { + s.InputTokens, s.OutputTokens = nil, nil + s.CacheReadTokens, s.CacheWriteTokens = nil, nil + } + + if spanName != "tool.execution" { + s.ToolName = "" + } +} + +func str(v any) string { + if v == nil { + return "" + } + if s, ok := v.(string); ok { + return s + } + return fmt.Sprintf("%v", v) +} + +func ptr(n int64) *int64 { return &n } + +// isUUID reports whether s has the 8-4-4-4-12 hex shape of a UUID. +func isUUID(s string) bool { + if len(s) != 36 { + return false + } + for i := 0; i < len(s); i++ { + c := s[i] + switch i { + case 8, 13, 18, 23: + if c != '-' { + return false + } + default: + if !(c >= '0' && c <= '9' || c >= 'a' && c <= 'f' || c >= 'A' && c <= 'F') { + return false + } + } + } + return true +} diff --git a/internal/ingest/producers_test.go b/internal/ingest/producers_test.go new file mode 100644 index 0000000..cc2408d --- /dev/null +++ b/internal/ingest/producers_test.go @@ -0,0 +1,233 @@ +package ingest_test + +import ( + "bytes" + "math" + "net/http" + "net/http/httptest" + "strings" + "testing" + + "github.com/Flopsstuff/cotel/internal/ingest" + "github.com/Flopsstuff/cotel/internal/storage" +) + +// The span shapes below are the ones Codex 0.160 (codex-app-server) and Grok CLI +// 1.0.77 export through Paperclip, trimmed to the attributes that matter; ids +// are synthetic. + +const ( + codexConv = "0000c0de-0000-7000-8000-000000000001" + grokSessID = "00006a0c-0000-7000-8000-000000000002" +) + +type kv struct { + key string + val string // already-encoded OTLP AnyValue JSON, e.g. {"stringValue":"x"} +} + +func s(v string) string { return `{"stringValue":"` + v + `"}` } +func i(v string) string { return `{"intValue":"` + v + `"}` } + +// ingestOne posts one span from service svc and returns what was stored. +func ingestOne(t *testing.T, svc, spanName string, attrs ...kv) storage.Span { + t.Helper() + parts := make([]string, 0, len(attrs)) + for _, a := range attrs { + parts = append(parts, `{"key":"`+a.key+`","value":`+a.val+`}`) + } + payload := `{"resourceSpans":[{"resource":{"attributes":[` + + `{"key":"service.name","value":{"stringValue":"` + svc + `"}}` + + `]},"scopeSpans":[{"spans":[{` + + `"traceId":"aabbccdd00000000aabbccdd00000000",` + + `"spanId":"aabbccdd00000000",` + + `"name":"` + spanName + `",` + + `"startTimeUnixNano":"1778327029000000000",` + + `"endTimeUnixNano":"1778327129000000000",` + + `"attributes":[` + strings.Join(parts, ",") + `]}]}]}]}` + + store := &memStore{} + h := ingest.New(store) + req := httptest.NewRequest(http.MethodPost, "/v1/traces", bytes.NewBufferString(payload)) + req.Header.Set("Content-Type", "application/json") + h.ServeHTTP(httptest.NewRecorder(), req) + h.Flush() + if len(store.spans) != 1 { + t.Fatalf("expected 1 span, got %d", len(store.spans)) + } + return store.spans[0] +} + +func wantTokens(t *testing.T, name string, got *int64, want int64) { + t.Helper() + if got == nil { + t.Errorf("%s: got nil, want %d", name, want) + return + } + if *got != want { + t.Errorf("%s: got %d, want %d", name, *got, want) + } +} + +func wantCost(t *testing.T, got *float64, want float64) { + t.Helper() + if got == nil { + t.Fatalf("cost_usd: got nil, want %.6f", want) + } + if math.Abs(*got-want) > 1e-9 { + t.Errorf("cost_usd: got %.9f, want %.9f", *got, want) + } +} + +// The Codex turn span is where a session gets its model, tokens and cost: the +// conversation id rides as thread.id, and input_tokens includes the cached part. +func TestCodexTurnSpan(t *testing.T) { + sp := ingestOne(t, "codex-app-server", "session_task.turn", + kv{"thread.id", s(codexConv)}, + kv{"model", s("gpt-5.6-sol")}, + kv{"codex.turn.token_usage.input_tokens", i("174228")}, + kv{"codex.turn.token_usage.cached_input_tokens", i("130176")}, + kv{"codex.turn.token_usage.non_cached_input_tokens", i("44052")}, + kv{"codex.turn.token_usage.cache_write_input_tokens", i("0")}, + kv{"codex.turn.token_usage.output_tokens", i("934")}, + kv{"codex.turn.token_usage.reasoning_output_tokens", i("82")}, + ) + if sp.SessionID != codexConv { + t.Errorf("session_id: got %q, want %q", sp.SessionID, codexConv) + } + if sp.Model != "gpt-5.6-sol" { + t.Errorf("model: got %q", sp.Model) + } + wantTokens(t, "input", sp.InputTokens, 44052) + wantTokens(t, "cache_read", sp.CacheReadTokens, 130176) + wantTokens(t, "cache_write", sp.CacheWriteTokens, 0) + wantTokens(t, "output", sp.OutputTokens, 934) + // gpt-5.6-sol: $4 in, $0.40 cached, $20 out per MTok. + wantCost(t, sp.CostUSD, (44052*4.0+130176*0.40+934*20.0)/1e6) +} + +// Without non_cached_input_tokens the input column is total minus cached. +func TestCodexTurnSpanDerivesNonCachedInput(t *testing.T) { + sp := ingestOne(t, "codex-app-server", "session_task.turn", + kv{"thread.id", s(codexConv)}, + kv{"model", s("gpt-5.6-sol")}, + kv{"codex.turn.token_usage.input_tokens", i("1000")}, + kv{"codex.turn.token_usage.cached_input_tokens", i("600")}, + kv{"codex.turn.token_usage.output_tokens", i("10")}, + ) + wantTokens(t, "input", sp.InputTokens, 400) + wantTokens(t, "cache_read", sp.CacheReadTokens, 600) +} + +// thread.id on most Codex spans is the tracing layer's OS thread number. +func TestCodexIntegerThreadIDIsNotASession(t *testing.T) { + sp := ingestOne(t, "codex-app-server", "fs.get_metadata", kv{"thread.id", i("3")}) + if sp.SessionID != "" { + t.Errorf("session_id: got %q, want empty", sp.SessionID) + } +} + +// handle_responses repeats the turn's usage per request as gen_ai.usage.*; the +// turn span already carries the sum, so these must not add to it. +func TestCodexPerRequestUsageIsNotCounted(t *testing.T) { + sp := ingestOne(t, "codex-app-server", "handle_responses", + kv{"gen_ai.usage.input_tokens", i("24325")}, + kv{"gen_ai.usage.output_tokens", i("196")}, + kv{"gen_ai.usage.cache_read.input_tokens", i("11264")}, + ) + if sp.InputTokens != nil || sp.OutputTokens != nil || sp.CacheReadTokens != nil { + t.Errorf("handle_responses must carry no tokens, got in=%v out=%v cr=%v", + sp.InputTokens, sp.OutputTokens, sp.CacheReadTokens) + } + if sp.CostUSD != nil { + t.Errorf("handle_responses must carry no cost, got %v", *sp.CostUSD) + } +} + +// A Codex tool call is the span with tool_name and call_id; the broker span that +// names the same nested call has no call_id and is not a second call. +func TestCodexToolSpans(t *testing.T) { + call := ingestOne(t, "codex-app-server", "exec_command", + kv{"thread.id", s(codexConv)}, + kv{"tool_name", s("exec_command")}, + kv{"call_id", s("exec-1")}, + ) + if call.ToolName != "exec_command" || call.SessionID != codexConv { + t.Errorf("exec_command: tool=%q session=%q", call.ToolName, call.SessionID) + } + + broker := ingestOne(t, "codex-app-server", "code_mode.broker.invoke_tool", + kv{"conversation.id", s(codexConv)}, + kv{"tool_name", s("exec_command")}, + kv{"thread.id", i("5")}, + ) + if broker.ToolName != "" { + t.Errorf("broker span must not count as a tool call, got %q", broker.ToolName) + } + if broker.SessionID != codexConv { + t.Errorf("broker session_id from conversation.id: got %q", broker.SessionID) + } +} + +// The Grok turn span carries session, model and the turn's usage; its +// input_tokens includes cache_read_tokens. +func TestGrokTurnSpan(t *testing.T) { + sp := ingestOne(t, "grok-cli", "session.process_conversation_turn", + kv{"session_id", s(grokSessID)}, + kv{"model_id", s("grok-4.7")}, + kv{"input_tokens", i("204311")}, + kv{"cache_read_tokens", i("171008")}, + kv{"output_tokens", i("1294")}, + kv{"last_sample.output_tokens", i("159")}, + ) + if sp.SessionID != grokSessID { + t.Errorf("session_id: got %q", sp.SessionID) + } + if sp.Model != "grok-4.7" { + t.Errorf("model: got %q", sp.Model) + } + wantTokens(t, "input", sp.InputTokens, 204311-171008) + wantTokens(t, "cache_read", sp.CacheReadTokens, 171008) + wantTokens(t, "output", sp.OutputTokens, 1294) + // grok-4.7: $2 in, $0.50 cached, $6 out per MTok. + wantCost(t, sp.CostUSD, ((204311-171008)*2.0+171008*0.50+1294*6.0)/1e6) +} + +// sampling.stream_decode repeats output_tokens per sample; counted on the turn. +func TestGrokSampleSpanCarriesNoTokens(t *testing.T) { + sp := ingestOne(t, "grok-cli", "sampling.stream_decode", kv{"output_tokens", i("317")}) + if sp.OutputTokens != nil { + t.Errorf("stream_decode must carry no tokens, got %d", *sp.OutputTokens) + } +} + +// tool.decision is the permission check; tool.execution is the call. +func TestGrokToolSpans(t *testing.T) { + exec := ingestOne(t, "grok-cli", "tool.execution", + kv{"session_id", s(grokSessID)}, kv{"tool_name", s("read_file")}) + if exec.ToolName != "read_file" || exec.SessionID != grokSessID { + t.Errorf("tool.execution: tool=%q session=%q", exec.ToolName, exec.SessionID) + } + decision := ingestOne(t, "grok-cli", "tool.decision", kv{"tool_name", s("read_file")}) + if decision.ToolName != "" { + t.Errorf("tool.decision must not count as a tool call, got %q", decision.ToolName) + } +} + +// The adapters key on service.name: a Claude Code span with the same attribute +// names is decoded exactly as before. +func TestClaudeCodeSpansAreNotAdapted(t *testing.T) { + sp := ingestOne(t, "claude-code", "claude_code.tool", + kv{"session_id", s(grokSessID)}, + kv{"model_id", s("grok-4.7")}, + kv{"tool_name", s("Read")}, + kv{"output_tokens", i("10")}, + ) + if sp.SessionID != "" || sp.Model != "" { + t.Errorf("claude-code: session=%q model=%q, want both empty", sp.SessionID, sp.Model) + } + if sp.ToolName != "Read" { + t.Errorf("claude-code tool: got %q", sp.ToolName) + } + wantTokens(t, "output", sp.OutputTokens, 10) +} diff --git a/internal/pricing/prices.go b/internal/pricing/prices.go index 0c76edc..f75e035 100644 --- a/internal/pricing/prices.go +++ b/internal/pricing/prices.go @@ -1,7 +1,9 @@ // Package pricing computes ingest-time cost from token counts. -// Prices are USD per million tokens sourced from Anthropic's published pricing page. -// Last verified: 2026-11 against https://platform.claude.com/docs/en/about-claude/models/overview -// Update this table when Anthropic revises rates. +// Prices are USD per million tokens. Claude rates come from Anthropic's published +// pricing page, last verified 2026-11 against +// https://platform.claude.com/docs/en/about-claude/models/overview; the OpenAI and +// xAI entries say where theirs came from. Update this table when a provider +// revises rates. package pricing import ( @@ -131,6 +133,31 @@ var table = map[string]ModelPrices{ CacheReadPerMTok: 0.10, CacheWritePerMTok: 1.25, }, + // Non-Anthropic harnesses Paperclip runs (Codex, Grok CLI). Neither + // provider charges for writing to its prompt cache, so a cache write bills + // as ordinary input. Both bill long prompts (Grok: 200K+ tokens, the whole + // request) at a higher tier that a span cannot be matched to, so the base + // rate is used, as for Haiku 5.5. Verified 2026-11-15 against third-party + // rate cards citing the providers (openai.com/api/pricing and + // docs.x.ai/docs/models were not reachable to confirm). + "gpt-5.6-sol": { + InputPerMTok: 4.00, + OutputPerMTok: 20.00, + CacheReadPerMTok: 0.40, + CacheWritePerMTok: 4.00, + }, + "grok-4.7": { + InputPerMTok: 2.00, + OutputPerMTok: 6.00, + CacheReadPerMTok: 0.50, + CacheWritePerMTok: 2.00, + }, + "grok-4.6": { + InputPerMTok: 2.00, + OutputPerMTok: 6.00, + CacheReadPerMTok: 0.50, + CacheWritePerMTok: 2.00, + }, // Legacy / alias entries "claude-3-5-sonnet": { InputPerMTok: 3.00, diff --git a/internal/storage/actors.go b/internal/storage/actors.go index 51ae0f7..57e2ee6 100644 --- a/internal/storage/actors.go +++ b/internal/storage/actors.go @@ -226,7 +226,7 @@ WITH ` + rangeUsageCTE + `, usage_agg AS ( SELECT actor_id, COALESCE(SUM(cost), 0) AS cost, - COUNT(DISTINCT session_id) AS sessions + COUNT(DISTINCT NULLIF(session_id, '')) AS sessions FROM usage GROUP BY actor_id ), @@ -343,7 +343,7 @@ usage AS ( FROM daily_usage du CROSS JOIN raw_floor rf WHERE %s AND (rf.ts IS NULL OR du.day < CAST(CAST(rf.ts AS TIMESTAMP) AS DATE))%s ) -SELECT COALESCE(SUM(cost), 0), COUNT(DISTINCT session_id) FROM usage +SELECT COALESCE(SUM(cost), 0), COUNT(DISTINCT NULLIF(session_id, '')) FROM usage `, rawPrincipal, rawSince, aggPrincipal, aggSince) var cost float64 diff --git a/internal/storage/actors_page_test.go b/internal/storage/actors_page_test.go index 467a93f..20c99e4 100644 --- a/internal/storage/actors_page_test.go +++ b/internal/storage/actors_page_test.go @@ -447,3 +447,35 @@ func TestGetActorWithStats(t *testing.T) { } func ptrTime(t time.Time) *time.Time { return &t } + +// A span stored without a session id (Codex and Grok emit most of theirs that +// way) is not a session: before this was counted, an actor whose only spans were +// sessionless showed one session on the Actors page and none on Sessions. +func TestActorSessionsIgnoreEmptySessionID(t *testing.T) { + db := openTestActorDB(t) + u := mustCreateTestActor(t, db, "codex") + now := time.Now() + addRangeSpan(t, db, "noise-1", "codex", "", now.Add(-time.Hour), 0) + addRangeSpan(t, db, "noise-2", "codex", "", now.Add(-time.Hour), 0) + + got, err := db.GetActorWithStats(u.ID, nil) + if err != nil { + t.Fatalf("GetActorWithStats: %v", err) + } + if got.Sessions != 0 { + t.Errorf("GetActorWithStats: want 0 sessions, got %d", got.Sessions) + } + + addRangeSpan(t, db, "turn", "codex", "conv-1", now.Add(-time.Hour), 1) + actors, _, err := db.ListActorsPage(ListActorsOptions{}) + if err != nil { + t.Fatalf("ListActorsPage: %v", err) + } + row, ok := actorByName(actors, "codex") + if !ok { + t.Fatalf("codex missing from %v", actorNames(actors)) + } + if row.Sessions != 1 { + t.Errorf("ListActorsPage: want 1 session, got %d", row.Sessions) + } +} From 17979178ba5c6bf3fdb6063a3cd72c25ef18508e Mon Sep 17 00:00:00 2001 From: FlopBut Date: Sun, 15 Nov 2026 19:31:16 +0100 Subject: [PATCH 2/2] fix(ingest): match paperclip.agent.id case-insensitively The dashboard lowercases the Paperclip agent id it stores, but the ingest lookup compared the id as the sender sent it, so an uppercase UUID left that agent's spans on Anonymous. Raised by CodeRabbit on #166. Co-Authored-By: Claude Opus 5.5 --- CHANGELOG.md | 1 + internal/ingest/handler.go | 3 +++ internal/ingest/handler_test.go | 2 ++ 3 files changed, 6 insertions(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index 9e43bbc..cdb20b0 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -19,6 +19,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 - `docker compose build` tags its result `cotel-local:latest` instead of `ghcr.io/flopsstuff/cotel:latest`. Compose treats `image:` next to `build:` as the name to tag the local build with, so the published name was being reused for an artifact that never came from the registry: on the production host — where the deploy is `docker compose build --pull` followed by `up -d`, and `--pull` refreshes the Dockerfile's base images rather than the app image — `Image=ghcr.io/flopsstuff/cotel:latest` named a locally built binary, and built-here and pulled-from-registry were the same string. Three things follow from splitting them. You can read which artifact a host is running off the name. The thing the release pipeline publishes, and which `release.yml` moves only on an explicit dispatch, is no longer shadowed by every merge to `main`. And `docker pull ghcr.io/flopsstuff/cotel:latest` on the production host stops being able to swap the running service's binary at its next restart for content no deploy step validated — it now lands on a name nothing there runs. Production still *builds* its image, deliberately: a pull-only production would mean a merge to `main` no longer changes production, which is a separate decision from this cleanup and would need its own ADR. The operations pages name both images where the distinction matters — the recovery procedure's "match the CLI to the version linked into cotel" step asks `cotel-local:latest` on a host that builds its own image — and they also record that `ghcr.io/flopsstuff/cotel:latest` is now a multi-arch index whose `linux/arm64` entry runs natively on a Pi or Apple Silicon, so the documented `docker run --entrypoint /usr/local/bin/cotel … --db-query` and `--db-import` commands are honest on arm64 without a local build ([docs/operations/duckdb-recovery.md](docs/operations/duckdb-recovery.md), [docs/operations/duckdb-snapshots.md](docs/operations/duckdb-snapshots.md)) ### Fixed +- A `paperclip.agent.id` sent in upper or mixed case now finds its actor. The dashboard stores the id lowercased and the ingest lookup compared it as sent, so such spans stayed on Anonymous. - **Codex and Grok CLI agents get sessions, models, tokens and cost.** Paperclip's Codex (`codex-app-server`) and Grok (`grok-cli`) agents were exporting spans that ingest stored with no session, so they never reached the Sessions page, and with no tokens or model, so they cost $0. Neither harness uses Claude Code's attribute names, and both emit several spans per event. `internal/ingest/producers.go` adds an adapter per harness, chosen by `service.name` and applied only to those spans. Claude Code spans are untouched. For Codex, the session comes from `conversation.id`, or from `thread.id` when it is a UUID (on other spans it is the OS thread number). Usage comes from the turn span's `codex.turn.token_usage.*`, with cached input split out. The per-request `gen_ai.usage.*` copies are not read, since they sum to the turn. A tool call is a span with both `tool_name` and `call_id`. For Grok, the session comes from `session_id` and the model from `model_id`. Tokens are kept only on `session.process_conversation_turn`, with `cache_read_tokens` taken out of its inclusive `input_tokens`, because `sampling.stream_decode` repeats the output count. Only `tool.execution` counts as a tool call, not `tool.decision`. Prices are added for `gpt-5.6-sol` ($4 input / $0.40 cached / $20 output) and `grok-4.7` / `grok-4.6` ($2 / $0.50 / $6), at base rates, taken from third-party rate cards that cite the providers. Grok still lands on Anonymous: it does not carry `paperclip.agent.id` (ADR-0029). Cursor exports no telemetry at all. Already-stored spans are not rewritten, because `session_id` is an indexed column (see the ART incident). - Actor and overview session counts no longer count a span stored without a session id as a session. An actor whose only spans were sessionless showed one session on Actors and none on Sessions. - Spans from the Claude 5.5 / 5.2 generation are priced. From 2026-11-07 every span from `claude-opus-5-5`, `claude-fable-5-2` and `claude-haiku-5-5` was stored with `cost_usd` unset because the price table did not know them, so the dashboard showed $0 for a week of real traffic. Adds Opus 5.5 ($4/$20), Sonnet 5.5 ($2/$10), Haiku 5.5 ($0.10/$0.50; its $0.50/$2.50 rate for prompts over 100K tokens is not modelled, since a span does not say which tier billed it), Fable and Mythos 5.2 ($8/$40) and 5.1 ($10/$50), with the published cache-read rates where they depart from 0.1 x input. Sonnet 5 moves from $3/$15 to the $2/$10 Anthropic still publishes. `pricing.Known` tells an unpriced model from a priced span that costs zero. Already-stored spans keep their unset cost until `--backfill-cost-apply` runs diff --git a/internal/ingest/handler.go b/internal/ingest/handler.go index 5799da8..8bce592 100644 --- a/internal/ingest/handler.go +++ b/internal/ingest/handler.go @@ -7,6 +7,7 @@ import ( "io" "log" "net/http" + "strings" "time" "google.golang.org/protobuf/encoding/protojson" @@ -136,6 +137,8 @@ func (h *Handler) agentResolver(enabled bool) func(agentID string) string { } seen := map[string]string{} return func(agentID string) string { + // The dashboard stores the id lowercased; match a sender's casing to it. + agentID = strings.ToLower(strings.TrimSpace(agentID)) if agentID == "" { return "" } diff --git a/internal/ingest/handler_test.go b/internal/ingest/handler_test.go index c910cc7..01f1a15 100644 --- a/internal/ingest/handler_test.go +++ b/internal/ingest/handler_test.go @@ -6,6 +6,7 @@ import ( "net/http" "net/http/httptest" "os" + "strings" "testing" "github.com/Flopsstuff/cotel/internal/ingest" @@ -385,6 +386,7 @@ func TestPaperclipAgentAttribution(t *testing.T) { {"ingest key, span attr only", true, "", "", daedalus, "Daedalus", 1}, {"resource attr beats span attr", true, "", wayland, daedalus, "Wayland", 1}, {"unknown agent stays anonymous", true, "", "11111111-1111-1111-1111-111111111111", "", "", 1}, + {"agent id matched case-insensitively", true, "", strings.ToUpper(wayland), "", "Wayland", 1}, {"actor token wins", true, "flop", wayland, "", "flop", 0}, {"no ingest key: attribute ignored", false, "", wayland, "", "", 0}, }