From e3ffa955f27d84eb211838c5b24d77b564e14e79 Mon Sep 17 00:00:00 2001 From: FlopBut Date: Sun, 15 Nov 2026 12:27:55 +0100 Subject: [PATCH 1/4] feat(pricing): price the Claude 5.5 / 5.2 generation and add --backfill-cost Since 2026-11-07 production has stored cost_usd unset for every span from claude-opus-5-5, claude-fable-5-2 and claude-haiku-5-5: the table did not know them, so the dashboard showed $0 for a week of traffic. Adds Opus 5.5, Sonnet 5.5, Haiku 5.5, Fable/Mythos 5.2 and 5.1, and moves Sonnet 5 to the $2/$10 rate Anthropic still publishes. Revives the cost backfill from the closed PR #67 (FLO-551/552) as reviewed: --backfill-cost is a dry run, --backfill-cost-apply writes spans only. The README procedure is rewritten for the current deploy: the dry run restores the newest snapshot instead of copying a live file, and both one-offs bypass the entrypoint so they do not start a second tunnel connector. Co-Authored-By: Claude Opus 5.5 --- README.md | 48 +++++ cmd/cotel/main.go | 59 ++++++ internal/pricing/prices.go | 74 +++++++- internal/pricing/prices_test.go | 41 +++++ internal/storage/backfill.go | 202 ++++++++++++++++++++ internal/storage/backfill_test.go | 297 ++++++++++++++++++++++++++++++ 6 files changed, 712 insertions(+), 9 deletions(-) create mode 100644 internal/storage/backfill.go create mode 100644 internal/storage/backfill_test.go diff --git a/README.md b/README.md index 38927b2..121fcda 100644 --- a/README.md +++ b/README.md @@ -553,6 +553,54 @@ from the network at startup instead of bundling it). | `COTEL_ART_REPRO_CYCLES` | `2` | Read by the ART-corruption reproducer test, not by the binary: how many replace-transaction-then-restart cycles it runs. The damage accumulates across cycles, so this is the other knob to raise. | | `COTEL_DATA_VOLUME` | `cotel-data-repaired-20261004` | Read by `docker-compose.yml`, not by the binary: the Docker volume mounted at `/data`. Point it at a restored copy to bring an instance up *without* writing to the volume being restored from — Docker has no `volume rename`, so the only other way to serve repaired data under the expected name is to overwrite the damaged original, which is also the forensic evidence. The default is a restored copy, not the `cotel_cotel-data` name compose derives by itself: that volume holds a database the binary can no longer open and is kept untouched as evidence. A fresh install that has neither volume gets the default created empty, which is correct. | +## Maintenance commands + +### Cost backfill + +When the pricing table gains a model or corrects a rate after spans were already ingested, those +spans keep the `cost_usd` they were stored with (unset for a model the table did not know). +`--backfill-cost` recomputes `cost_usd` from each span's stored token counts at today's rates. + +> **The running server holds an exclusive DuckDB lock.** No second process can open +> `/data/cotel.duckdb` while the container is up, not even read-only (`Could not set lock on file +> ... Conflicting lock is held`), so neither command can be run with `docker exec` against the live +> file. The dry run reads a database restored from the newest snapshot (no downtime); the apply runs +> against the volume while the server is stopped (brief downtime). + +Both one-offs below pass `--entrypoint /usr/local/bin/cotel` (or `sh`) so the image entrypoint +does not start a second Cloudflare tunnel connector next to the server's, and `--no-deps` so nothing +else is started. + +**Step 1 - dry run against the newest snapshot (no writes, no downtime):** + +```bash +# Snapshot paths are absolute; pick the newest directory under /snapshots. +docker compose run --rm --no-deps -e COTEL_DB_PATH=/tmp/dry.duckdb --entrypoint sh cotel -c \ + 'cotel --db-import "$(ls -d /snapshots/20* | tail -1)" && cotel --backfill-cost' +``` + +It prints a per-model table of old and new cost and the total delta. Spans with an unknown or empty +model are counted separately and left untouched. The snapshot is up to `COTEL_SNAPSHOT_INTERVAL` +old, so the totals are an estimate of what the apply will do. + +**Step 2 - apply (after reviewing the dry run):** + +```bash +docker compose stop cotel +docker compose run --rm --no-deps --entrypoint /usr/local/bin/cotel cotel --backfill-cost-apply +docker compose start cotel +``` + +It updates `cost_usd` on every span of a priced model. It is idempotent: cost is recomputed from the +stored token counts, not scaled, so a second run changes nothing. + +> **Scope - `spans` only.** `daily_usage` is not touched. The retention roll-up derives +> `total_cost_usd` as `SUM(spans.cost_usd)`, so a day rolled up *after* the backfill is correct by +> construction. A day already rolled up keeps the cost it was rolled up with: roll-ups written +> before schema v8 kept no cache-token totals, and on real traffic cache tokens outnumber input and +> output ~156:1, so a recompute from the stored counters would understate the cost. Run the backfill +> before the affected days leave the raw window (`COTEL_RETENTION_RAW_DAYS`, 30 days by default). + ## Architecture ``` diff --git a/cmd/cotel/main.go b/cmd/cotel/main.go index 9d89363..ea3445a 100644 --- a/cmd/cotel/main.go +++ b/cmd/cotel/main.go @@ -31,6 +31,8 @@ func main() { dbQuery := flag.String("db-query", "", "run SQL query against DuckDB, print first column of first row, and exit") dbImport := flag.String("db-import", "", "restore COTEL_DB_PATH from the snapshot directory given, verify it against the snapshot manifest, and exit; the target database file must be empty or absent") healthcheck := flag.Bool("healthcheck", false, "probe the local dashboard /healthz and exit 0 (ready) or 1; used by the container HEALTHCHECK") + backfillCost := flag.Bool("backfill-cost", false, "dry-run: show what recalculating cost_usd from stored token counts would change (no writes), then exit") + backfillCostApply := flag.Bool("backfill-cost-apply", false, "recalculate cost_usd for all spans of priced models from their stored token counts, then exit") flag.Parse() dbPath := env("COTEL_DB_PATH", "/data/cotel.duckdb") @@ -67,6 +69,12 @@ func main() { return } + // The backfill needs the database file to itself (DuckDB has one writer), so + // like --db-import it runs before the listeners bind and exits afterwards. + if *backfillCost || *backfillCostApply { + os.Exit(runBackfill(dbPath, *backfillCostApply)) + } + // Bind BEFORE storage.Open: WAL replay + schema migration can block for // minutes on a large DB, and a bound port answering a retryable 503 keeps // OTLP clients retrying where a connection reset would drop their spans. @@ -151,6 +159,57 @@ func main() { os.Exit(code) } +// runBackfill opens the database, recomputes cost_usd and prints a per-model +// report. apply=false is a dry run that writes nothing. +func runBackfill(dbPath string, apply bool) int { + db, err := storage.Open(dbPath) + if err != nil { + log.Printf("open storage: %v", err) + return 1 + } + defer db.Close() + + var rep storage.BackfillReport + if apply { + fmt.Println("=== APPLYING cost_usd backfill ===") + rep, err = db.BackfillCostUSD() + } else { + fmt.Println("=== DRY RUN (no writes) - pass --backfill-cost-apply to apply ===") + rep, err = db.DryRunBackfill() + } + if err != nil { + log.Printf("backfill: %v", err) + return 1 + } + printBackfillReport(rep, apply) + return 0 +} + +// printBackfillReport prints a BackfillReport to stdout. +func printBackfillReport(rep storage.BackfillReport, applied bool) { + verb := "would change" + if applied { + verb = "changed" + } + fmt.Printf("\n%-35s %8s %12s %12s %12s\n", + "model", "spans", "old_cost_usd", "new_cost_usd", "delta_usd") + fmt.Println(strings.Repeat("-", 87)) + for _, row := range rep.ModelRows { + fmt.Printf("%-35s %8d %12.4f %12.4f %+12.4f\n", + row.Model, row.SpanCount, row.OldCostUSD, row.NewCostUSD, row.DeltaUSD) + } + fmt.Println(strings.Repeat("-", 87)) + fmt.Printf("total delta_usd: %+.4f (spans that %s: %d)\n", + rep.TotalDeltaUSD, verb, rep.SpansToUpdate) + if rep.UnknownModel > 0 { + fmt.Printf("unknown model (skipped): %d spans\n", rep.UnknownModel) + } + if rep.EmptyModel > 0 { + fmt.Printf("empty/null model (skipped): %d spans\n", rep.EmptyModel) + } + fmt.Println() +} + // shutdownCheckpointTimeout bounds a CHECKPOINT so a stuck fold cannot hang // the process. On shutdown it stays inside the container stop grace period // (docker-compose stop_grace_period); on startup it keeps a wedged fold from diff --git a/internal/pricing/prices.go b/internal/pricing/prices.go index 2adad96..0c76edc 100644 --- a/internal/pricing/prices.go +++ b/internal/pricing/prices.go @@ -1,6 +1,6 @@ // 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-08 against https://platform.claude.com/docs/en/about-claude/models/overview +// Last verified: 2026-11 against https://platform.claude.com/docs/en/about-claude/models/overview // Update this table when Anthropic revises rates. package pricing @@ -23,8 +23,56 @@ type ModelPrices struct { // snapshot suffixes canonicalID strips. // // Cache rates follow Anthropic's convention: CacheRead = 0.1 x input, -// CacheWrite = 1.25 x input (the 5-minute ephemeral TTL). +// CacheWrite = 1.25 x input (the 5-minute ephemeral TTL). Where Anthropic +// publishes a cache-read rate that departs from 0.1 x input (Opus 5.5, Fable 5.2, +// Sonnet 5.5), the published rate wins. var table = map[string]ModelPrices{ + // Claude 5.5 / 5.2 generation + "claude-opus-5-5": { + InputPerMTok: 4.00, + OutputPerMTok: 20.00, + CacheReadPerMTok: 0.20, + CacheWritePerMTok: 5.00, + }, + "claude-sonnet-5-5": { + InputPerMTok: 2.00, + OutputPerMTok: 10.00, + CacheReadPerMTok: 0.10, + CacheWritePerMTok: 2.50, + }, + // Haiku 5.5 bills $0.50/$2.50 for prompts over 100K tokens. A span does not + // say which tier the request was billed at, so the base rate is used and + // long-prompt Haiku requests are under-counted. + "claude-haiku-5-5": { + InputPerMTok: 0.10, + OutputPerMTok: 0.50, + CacheReadPerMTok: 0.01, + CacheWritePerMTok: 0.125, + }, + "claude-fable-5-2": { + InputPerMTok: 8.00, + OutputPerMTok: 40.00, + CacheReadPerMTok: 0.20, + CacheWritePerMTok: 10.00, + }, + "claude-mythos-5-2": { + InputPerMTok: 8.00, + OutputPerMTok: 40.00, + CacheReadPerMTok: 0.20, + CacheWritePerMTok: 10.00, + }, + "claude-fable-5-1": { + InputPerMTok: 10.00, + OutputPerMTok: 50.00, + CacheReadPerMTok: 1.00, + CacheWritePerMTok: 12.50, + }, + "claude-mythos-5-1": { + InputPerMTok: 10.00, + OutputPerMTok: 50.00, + CacheReadPerMTok: 1.00, + CacheWritePerMTok: 12.50, + }, // Claude 5 family "claude-fable-5": { InputPerMTok: 10.00, @@ -44,14 +92,13 @@ var table = map[string]ModelPrices{ CacheReadPerMTok: 0.50, CacheWritePerMTok: 6.25, }, - // Sonnet 5 has a $2/$10 introductory rate through 2026-08-31; we bill the - // standard $3/$15 rate. A date-dependent rate would silently rot, so the - // standard rate is the boring, correct choice. + // Sonnet 5 launched with a $2/$10 rate described as introductory; the + // published rate is still $2/$10 as of 2026-11, so that is what is billed. "claude-sonnet-5": { - InputPerMTok: 3.00, - OutputPerMTok: 15.00, - CacheReadPerMTok: 0.30, - CacheWritePerMTok: 3.75, + InputPerMTok: 2.00, + OutputPerMTok: 10.00, + CacheReadPerMTok: 0.20, + CacheWritePerMTok: 2.50, }, // Claude 4.x family "claude-opus-4-8": { @@ -132,6 +179,15 @@ func isSnapshotDate(s string) bool { return true } +// Known reports whether the model (after stripping tier and snapshot suffixes) +// is present in the price table. Callers use this to tell a genuinely unpriced +// model from a priced one that computes a zero cost (e.g. a span with zero +// billable tokens). Do not infer "unknown model" from Compute() == 0. +func Known(model string) bool { + _, ok := table[canonicalID(model)] + return ok +} + // Compute returns the estimated cost in USD for the given token counts and model. // If the model is not in the price table it logs a warning and returns 0 so that // an unknown model never causes an ingest failure. diff --git a/internal/pricing/prices_test.go b/internal/pricing/prices_test.go index 58ec9fc..874ae22 100644 --- a/internal/pricing/prices_test.go +++ b/internal/pricing/prices_test.go @@ -251,3 +251,44 @@ func TestComputeUnknownModelLogCap(t *testing.T) { t.Errorf("post-cap unknown still returns 0, got %v", got) } } + +// The models production actually saw go dark on: from 2026-11-07 every span +// from these ids was stored with cost_usd unset because the table lacked them. +func TestComputeClaude55Generation(t *testing.T) { + cases := []struct { + model string + in, out, cr, cw float64 // $/MTok + }{ + {"claude-opus-5-5", 4.00, 20.00, 0.20, 5.00}, + {"claude-opus-5-5[1m]", 4.00, 20.00, 0.20, 5.00}, + {"claude-fable-5-2", 8.00, 40.00, 0.20, 10.00}, + {"claude-mythos-5-2", 8.00, 40.00, 0.20, 10.00}, + {"claude-fable-5-1", 10.00, 50.00, 1.00, 12.50}, + {"claude-sonnet-5-5", 2.00, 10.00, 0.10, 2.50}, + {"claude-haiku-5-5", 0.10, 0.50, 0.01, 0.125}, + {"claude-sonnet-5[1m]", 2.00, 10.00, 0.20, 2.50}, + } + for _, c := range cases { + if !pricing.Known(c.model) { + t.Errorf("%s: Known() = false", c.model) + continue + } + got := pricing.Compute(c.model, 1_000_000, 1_000_000, 1_000_000, 1_000_000) + want := c.in + c.out + c.cr + c.cw + if math.Abs(got-want) > 1e-9 { + t.Errorf("%s: got %.6f, want %.6f", c.model, got, want) + } + } +} + +func TestKnownIsNotComputeNonZero(t *testing.T) { + if !pricing.Known("claude-opus-5-5") { + t.Fatal("priced model reported unknown") + } + if pricing.Compute("claude-opus-5-5", 0, 0, 0, 0) != 0 { + t.Fatal("zero tokens must cost zero") + } + if pricing.Known("claude-nonexistent-9") || pricing.Known("") { + t.Fatal("unpriced or empty model reported known") + } +} diff --git a/internal/storage/backfill.go b/internal/storage/backfill.go new file mode 100644 index 0000000..9f5205c --- /dev/null +++ b/internal/storage/backfill.go @@ -0,0 +1,202 @@ +package storage + +import ( + "fmt" + "log" + "sort" + + "github.com/Flopsstuff/cotel/internal/pricing" +) + +// BackfillReport summarises changes from DryRunBackfill or BackfillCostUSD. +type BackfillReport struct { + // Known-model spans, grouped by model. + ModelRows []ModelDelta + // Spans with a non-empty model value that pricing.Compute does not know — left untouched. + UnknownModel int64 + // Spans with NULL or empty model — left untouched. + EmptyModel int64 + // Sum of (newCost - oldCost) across all spans that would be (or were) updated. + TotalDeltaUSD float64 + // Number of spans where newCost differs from the stored cost_usd. + SpansToUpdate int64 +} + +// ModelDelta is one row of a per-model cost breakdown. +type ModelDelta struct { + Model string + SpanCount int64 + OldCostUSD float64 + NewCostUSD float64 + DeltaUSD float64 +} + +// spanRow holds raw data read from the spans table for cost computation. +type spanRow struct { + id string + model string + in int64 + out int64 + cr int64 + cw int64 + oldCost float64 + newCost float64 // recomputed cost; only meaningful in the toWrite slice + isEmpty bool // model is NULL or "" +} + +// readAllSpans fetches every span needed for a backfill (all model/token data). +func (db *DB) readAllSpans() ([]spanRow, error) { + rows, err := db.rw.Query(` + SELECT span_id, + COALESCE(model, '') AS model, + COALESCE(input_tokens, 0), + COALESCE(output_tokens, 0), + COALESCE(cache_read_tokens, 0), + COALESCE(cache_write_tokens, 0), + COALESCE(cost_usd, 0) + FROM spans + `) + if err != nil { + return nil, fmt.Errorf("backfill: read spans: %w", err) + } + defer rows.Close() + + var out []spanRow + for rows.Next() { + var r spanRow + if err := rows.Scan(&r.id, &r.model, &r.in, &r.out, &r.cr, &r.cw, &r.oldCost); err != nil { + return nil, fmt.Errorf("backfill: scan span: %w", err) + } + r.isEmpty = r.model == "" + out = append(out, r) + } + return out, rows.Err() +} + +// computeReport builds a BackfillReport from raw span data without writing anything. +func computeReport(rows []spanRow) (BackfillReport, []spanRow) { + type accum struct { + count int64 + oldCostUSD float64 + newCostUSD float64 + } + byModel := map[string]*accum{} + + var toWrite []spanRow + var rep BackfillReport + + for _, r := range rows { + if r.isEmpty { + rep.EmptyModel++ + continue + } + if !pricing.Known(r.model) { + // Genuinely unpriced model — leave the stored cost untouched. Classify + // by Known(), not by Compute() == 0: a priced model with zero billable + // tokens also computes 0 and must NOT be counted as unknown. + rep.UnknownModel++ + continue + } + newCost := pricing.Compute(r.model, r.in, r.out, r.cr, r.cw) + acc := byModel[r.model] + if acc == nil { + acc = &accum{} + byModel[r.model] = acc + } + acc.count++ + acc.oldCostUSD += r.oldCost + acc.newCostUSD += newCost + + if newCost != r.oldCost { + rep.SpansToUpdate++ + rep.TotalDeltaUSD += newCost - r.oldCost + toWrite = append(toWrite, spanRow{id: r.id, model: r.model, oldCost: r.oldCost, + newCost: newCost, in: r.in, out: r.out, cr: r.cr, cw: r.cw}) + } + } + + for model, acc := range byModel { + rep.ModelRows = append(rep.ModelRows, ModelDelta{ + Model: model, + SpanCount: acc.count, + OldCostUSD: acc.oldCostUSD, + NewCostUSD: acc.newCostUSD, + DeltaUSD: acc.newCostUSD - acc.oldCostUSD, + }) + } + // Stable order: map iteration is random, and the operator's main check is + // diffing two consecutive dry-runs to confirm idempotence. + sort.Slice(rep.ModelRows, func(i, j int) bool { + if rep.ModelRows[i].SpanCount != rep.ModelRows[j].SpanCount { + return rep.ModelRows[i].SpanCount > rep.ModelRows[j].SpanCount + } + return rep.ModelRows[i].Model < rep.ModelRows[j].Model + }) + return rep, toWrite +} + +// DryRunBackfill computes what BackfillCostUSD would do without writing anything. +// It is safe to call while the server is running (read-only). +func (db *DB) DryRunBackfill() (BackfillReport, error) { + rows, err := db.readAllSpans() + if err != nil { + return BackfillReport{}, err + } + rep, _ := computeReport(rows) + return rep, nil +} + +// BackfillCostUSD recalculates cost_usd for every span whose model is known to +// pricing.Compute(). Spans with unknown or empty models are left untouched and +// counted in the returned report. Running it twice yields the same result +// (idempotent). +// +// It deliberately does NOT touch daily_usage. Roll-up derives total_cost_usd as +// SUM(spans.cost_usd), so any day rolled up after this backfill is correct by +// construction. Repairing an already-rolled-up row is impossible anyway: +// daily_usage persists only input/output token totals, and on real data cache +// tokens are ~156x that volume, so a recompute from the stored counters would +// recover only a small fraction of the true cost - and would silently lower a +// correct row on re-run. Making aggregates recomputable needs cache-token +// columns on daily_usage; that is tracked separately, not bolted onto a +// backfill. See FLO-552 review. +func (db *DB) BackfillCostUSD() (BackfillReport, error) { + allRows, err := db.readAllSpans() + if err != nil { + return BackfillReport{}, err + } + rep, updates := computeReport(allRows) + + tx, err := db.rw.Begin() + if err != nil { + return rep, fmt.Errorf("backfill: begin tx: %w", err) + } + defer func() { + if err != nil { + _ = tx.Rollback() + } + }() + + stmt, err := tx.Prepare(`UPDATE spans SET cost_usd = ? WHERE span_id = ?`) + if err != nil { + return rep, fmt.Errorf("backfill: prepare update: %w", err) + } + defer stmt.Close() + + var spansDone int64 + for _, u := range updates { + if _, execErr := stmt.Exec(u.newCost, u.id); execErr != nil { + err = fmt.Errorf("backfill: update span %s: %w", u.id, execErr) + return rep, err + } + spansDone++ + } + + if err = tx.Commit(); err != nil { + return rep, fmt.Errorf("backfill: commit spans: %w", err) + } + log.Printf("backfill: updated %d spans (unknown=%d empty=%d)", + spansDone, rep.UnknownModel, rep.EmptyModel) + + return rep, nil +} diff --git a/internal/storage/backfill_test.go b/internal/storage/backfill_test.go new file mode 100644 index 0000000..5c206c8 --- /dev/null +++ b/internal/storage/backfill_test.go @@ -0,0 +1,297 @@ +package storage + +import ( + "testing" + "time" +) + +// insertCostSpan inserts a span with explicit cost_usd for backfill tests. +func insertCostSpan(t *testing.T, db *DB, spanID, model string, in, out int64, costUSD float64) { + t.Helper() + now := time.Now() + _, err := db.rw.Exec(` + INSERT INTO spans (trace_id, span_id, name, start_time, end_time, + session_id, tool_name, model, + input_tokens, output_tokens, cache_read_tokens, cache_write_tokens, cost_usd) + VALUES (?,?,?,?,?,?,?,?,?,?,0,0,?)`, + "trace1", spanID, "test", now, now.Add(time.Second), + "sess-test", "tool-test", model, in, out, costUSD, + ) + if err != nil { + t.Fatalf("insertCostSpan %s: %v", spanID, err) + } +} + +func TestBackfillCostUSD_CorrectsZeroAndOverpriced(t *testing.T) { + db, err := Open(":memory:") + if err != nil { + t.Fatal(err) + } + defer db.Close() + + // Span that had no price (model missing from old table) → cost_usd = 0 + insertCostSpan(t, db, "s1", "claude-opus-4-8", 1_000_000, 0, 0) + // Span overpriced at old 15/75 rate: 1M input * 15 = $15 instead of correct $5 + insertCostSpan(t, db, "s2", "claude-opus-4-7", 1_000_000, 0, 15.0) + // Span with empty model — must be untouched (cost stays 0) + insertCostSpan(t, db, "s3", "", 1_000_000, 0, 0) + + rep, err := db.BackfillCostUSD() + if err != nil { + t.Fatalf("BackfillCostUSD: %v", err) + } + + if rep.SpansToUpdate != 2 { + t.Errorf("SpansToUpdate = %d, want 2", rep.SpansToUpdate) + } + if rep.EmptyModel != 1 { + t.Errorf("EmptyModel = %d, want 1", rep.EmptyModel) + } + + var cost1, cost2, cost3 float64 + db.rw.QueryRow(`SELECT COALESCE(cost_usd,0) FROM spans WHERE span_id='s1'`).Scan(&cost1) + db.rw.QueryRow(`SELECT COALESCE(cost_usd,0) FROM spans WHERE span_id='s2'`).Scan(&cost2) + db.rw.QueryRow(`SELECT COALESCE(cost_usd,0) FROM spans WHERE span_id='s3'`).Scan(&cost3) + + // claude-opus-4-8: $5/MTok input → 1M tokens = $5 + if cost1 < 4.99 || cost1 > 5.01 { + t.Errorf("s1 cost_usd = %.4f, want ~5.0", cost1) + } + // claude-opus-4-7: also $5/MTok → $5 (corrected from $15) + if cost2 < 4.99 || cost2 > 5.01 { + t.Errorf("s2 cost_usd = %.4f, want ~5.0 (corrected from 15.0)", cost2) + } + // empty-model span unchanged + if cost3 != 0 { + t.Errorf("s3 (no model) cost_usd = %.4f, want 0", cost3) + } +} + +func TestBackfillCostUSD_UnknownModelSkipped(t *testing.T) { + db, err := Open(":memory:") + if err != nil { + t.Fatal(err) + } + defer db.Close() + + // Span with a model name not in the pricing table — must NOT be zeroed out. + insertCostSpan(t, db, "u1", "gpt-99-turbo", 1_000_000, 0, 3.50) + + rep, err := db.BackfillCostUSD() + if err != nil { + t.Fatalf("BackfillCostUSD: %v", err) + } + + if rep.UnknownModel != 1 { + t.Errorf("UnknownModel = %d, want 1", rep.UnknownModel) + } + if rep.SpansToUpdate != 0 { + t.Errorf("SpansToUpdate = %d, want 0 (unknown model must be skipped)", rep.SpansToUpdate) + } + + var cost float64 + db.rw.QueryRow(`SELECT COALESCE(cost_usd,0) FROM spans WHERE span_id='u1'`).Scan(&cost) + if cost != 3.50 { + t.Errorf("unknown-model span cost_usd = %.4f, want 3.50 (unchanged)", cost) + } +} + +func TestBackfillCostUSD_Idempotent(t *testing.T) { + db, err := Open(":memory:") + if err != nil { + t.Fatal(err) + } + defer db.Close() + + insertCostSpan(t, db, "s1", "claude-opus-4-8", 500_000, 200_000, 0) + + rep1, err := db.BackfillCostUSD() + if err != nil { + t.Fatalf("first backfill: %v", err) + } + + var costAfter1 float64 + db.rw.QueryRow(`SELECT COALESCE(cost_usd,0) FROM spans WHERE span_id='s1'`).Scan(&costAfter1) + + rep2, err := db.BackfillCostUSD() + if err != nil { + t.Fatalf("second backfill: %v", err) + } + + var costAfter2 float64 + db.rw.QueryRow(`SELECT COALESCE(cost_usd,0) FROM spans WHERE span_id='s1'`).Scan(&costAfter2) + + if costAfter1 != costAfter2 { + t.Errorf("not idempotent: first=%.6f second=%.6f", costAfter1, costAfter2) + } + // Second run should find nothing to update (already correct). + if rep2.SpansToUpdate != 0 { + t.Errorf("second run SpansToUpdate = %d, want 0 (already idempotent)", rep2.SpansToUpdate) + } + _ = rep1 +} + +func TestDryRunBackfill_NoWrites(t *testing.T) { + db, err := Open(":memory:") + if err != nil { + t.Fatal(err) + } + defer db.Close() + + insertCostSpan(t, db, "s1", "claude-opus-4-8", 1_000_000, 0, 0) + insertCostSpan(t, db, "s2", "", 1_000_000, 0, 0) // empty model + insertCostSpan(t, db, "s3", "gpt-99", 1_000_000, 0, 7.0) // unknown model + + rep, err := db.DryRunBackfill() + if err != nil { + t.Fatalf("DryRunBackfill: %v", err) + } + + if rep.SpansToUpdate != 1 { + t.Errorf("SpansToUpdate = %d, want 1", rep.SpansToUpdate) + } + if rep.EmptyModel != 1 { + t.Errorf("EmptyModel = %d, want 1", rep.EmptyModel) + } + if rep.UnknownModel != 1 { + t.Errorf("UnknownModel = %d, want 1", rep.UnknownModel) + } + + // Verify nothing was written. + var cost float64 + db.rw.QueryRow(`SELECT COALESCE(cost_usd,0) FROM spans WHERE span_id='s1'`).Scan(&cost) + if cost != 0 { + t.Errorf("dry-run wrote to DB: s1 cost_usd = %.4f, want 0", cost) + } + db.rw.QueryRow(`SELECT COALESCE(cost_usd,0) FROM spans WHERE span_id='s3'`).Scan(&cost) + if cost != 7.0 { + t.Errorf("dry-run modified unknown model span: s3 cost_usd = %.4f, want 7.0", cost) + } +} + +func TestBackfillCostUSD_CacheTokens(t *testing.T) { + db, err := Open(":memory:") + if err != nil { + t.Fatal(err) + } + defer db.Close() + + // Insert with cache tokens directly to verify cache rates are applied + _, err = db.rw.Exec(` + INSERT INTO spans (trace_id, span_id, name, start_time, end_time, model, + input_tokens, output_tokens, cache_read_tokens, cache_write_tokens, cost_usd) + VALUES ('t','c1','test',now(),now(),'claude-opus-4-8',0,0,1000000,0, 0.0)`) + if err != nil { + t.Fatal(err) + } + + if _, err := db.BackfillCostUSD(); err != nil { + t.Fatal(err) + } + + var cost float64 + db.rw.QueryRow(`SELECT COALESCE(cost_usd,0) FROM spans WHERE span_id='c1'`).Scan(&cost) + // claude-opus-4-8 cache_read = $0.50/MTok → 1M tokens = $0.50 + if cost < 0.49 || cost > 0.51 { + t.Errorf("cache read cost = %.4f, want ~0.50", cost) + } +} + +// B3: a priced model with zero billable tokens computes cost 0 but must NOT be +// counted as an unknown model — only genuinely unpriced models are "unknown". +func TestComputeReport_KnownModelZeroTokens_NotUnknown(t *testing.T) { + db, err := Open(":memory:") + if err != nil { + t.Fatal(err) + } + defer db.Close() + + insertCostSpan(t, db, "z1", "claude-opus-4-8", 0, 0, 0) // known model, zero tokens → cost 0 + insertCostSpan(t, db, "u1", "gpt-99-turbo", 1_000_000, 0, 0) // genuinely unknown model + + rep, err := db.DryRunBackfill() + if err != nil { + t.Fatalf("DryRunBackfill: %v", err) + } + + if rep.UnknownModel != 1 { + t.Errorf("UnknownModel = %d, want 1 (only gpt-99; the zero-token opus span is NOT unknown)", rep.UnknownModel) + } + var found bool + for _, m := range rep.ModelRows { + if m.Model == "claude-opus-4-8" { + found = true + if m.SpanCount != 1 { + t.Errorf("claude-opus-4-8 SpanCount = %d, want 1", m.SpanCount) + } + } + } + if !found { + t.Errorf("known zero-token model missing from ModelRows breakdown") + } +} + +// The backfill must leave daily_usage completely alone. Roll-up derives +// total_cost_usd as SUM(spans.cost_usd), so post-backfill roll-ups are correct +// by construction; an already-rolled-up row cannot be repaired from its stored +// counters because daily_usage persists no cache tokens. Re-deriving it would +// only lower a correct row on re-run. +func TestBackfillCostUSD_DoesNotTouchDailyUsage(t *testing.T) { + db, err := Open(":memory:") + if err != nil { + t.Fatal(err) + } + defer db.Close() + + // A rolled-up row whose cost was computed under the old (wrong) rates, and a + // cache-heavy row whose correct cost is far above its input+output-only cost. + if _, err := db.rw.Exec(` + INSERT INTO daily_usage (day, session_id, model, tool_name, user_id, span_count, + total_input_tokens, total_output_tokens, total_cost_usd) + VALUES ('2026-07-01', 'sess1', 'claude-opus-4-7', 'tool1', NULL, 10, 1000000, 0, 15.0), + ('2026-07-02', 'sess2', 'claude-opus-4-8', 'tool1', NULL, 20, 1000, 0, 42.0)`); err != nil { + t.Fatal(err) + } + // A recent raw span not represented in daily_usage - must not be materialised. + insertCostSpan(t, db, "recent1", "claude-opus-4-8", 1_000_000, 0, 0) + + if _, err := db.BackfillCostUSD(); err != nil { + t.Fatalf("BackfillCostUSD: %v", err) + } + + // The span itself is still corrected. + var spanCost float64 + db.rw.QueryRow(`SELECT COALESCE(cost_usd,0) FROM spans WHERE span_id='recent1'`).Scan(&spanCost) + if spanCost < 4.99 || spanCost > 5.01 { + t.Errorf("span cost_usd = %.4f, want ~5.0", spanCost) + } + + // Every daily_usage row is byte-for-byte unchanged. + for _, tc := range []struct { + day string + cost float64 + spanCount int64 + in int64 + }{ + {"2026-07-01", 15.0, 10, 1_000_000}, + {"2026-07-02", 42.0, 20, 1_000}, + } { + var cost float64 + var spanCount, in, out int64 + db.rw.QueryRow(`SELECT total_cost_usd, span_count, total_input_tokens, total_output_tokens + FROM daily_usage WHERE day=?`, tc.day).Scan(&cost, &spanCount, &in, &out) + if cost != tc.cost { + t.Errorf("%s total_cost_usd = %.4f, want %.4f (backfill must not touch daily_usage)", tc.day, cost, tc.cost) + } + if spanCount != tc.spanCount || in != tc.in || out != 0 { + t.Errorf("%s counters changed: span_count=%d in=%d out=%d", tc.day, spanCount, in, out) + } + } + + // No rows materialised from the recent raw span. + var n int64 + db.rw.QueryRow(`SELECT COUNT(*) FROM daily_usage`).Scan(&n) + if n != 2 { + t.Errorf("daily_usage row count = %d, want 2 (backfill must not materialise aggregates)", n) + } +} From 1bf04ff43a53a640fb4f5a0732c6e2a411f97098 Mon Sep 17 00:00:00 2001 From: FlopBut Date: Sun, 15 Nov 2026 12:34:30 +0100 Subject: [PATCH 2/4] feat(storage,api)!: rename users to actors (schema v11) The entity a token belongs to was never only a person: production holds people (flop), Paperclip agents (Daedalus, Clio, ...) and hosts (rpi5). It is now an actor everywhere below the UI. Storage (schema v11): table users -> actors, spans.user_id and daily_usage.user_id -> actor_id, idx_spans_user_id -> idx_spans_actor_id. The rename runs in Go before schema.sql (migrateUsersToActors): a rename cannot be written idempotently in SQL, and schema.sql's CREATE TABLE IF NOT EXISTS actors / ADD COLUMN IF NOT EXISTS actor_id would otherwise create empty twins beside the populated originals. It checks the catalog per step, drops the spans secondary indexes first (DuckDB refuses to ALTER an indexed table; schema.sql recreates them), commits as one transaction, and refuses to start if both users and actors exist. Tested against a database built from the frozen v10 schema.sql (testdata/schema_v10.sql). Export format v2 names the column actor_id at the same offset; the importer still reads v1 files' user_id. Pre-v11 snapshots restore unchanged and are migrated by the next open. API: /api/v1/actors, ?actor_id=, mode=actor_only|actor_and_history, actors_count in /overview. The old /users route, ?user_id= and user_* delete modes are accepted as aliases. Response field names change. BREAKING CHANGE: JSON fields users_count and user_id are now actors_count and actor_id; export manifests are format_version 2. Co-Authored-By: Claude Opus 5.5 --- cmd/cotel/main.go | 2 +- internal/api/actors.go | 240 +++++++++++++++ internal/api/actors_delete_test.go | 155 ++++++++++ internal/api/actors_list_test.go | 167 ++++++++++ internal/api/auth/middleware.go | 10 +- internal/api/auth/middleware_test.go | 10 +- internal/api/handler.go | 94 +++--- internal/api/handler_test.go | 8 +- internal/api/history_grid_test.go | 2 +- internal/api/history_range_test.go | 12 +- internal/api/overview_range_test.go | 52 ++-- internal/api/settings.go | 6 +- internal/api/tools_list_test.go | 14 +- internal/api/users.go | 237 --------------- internal/api/users_delete_test.go | 155 ---------- internal/api/users_list_test.go | 138 --------- internal/export/export.go | 16 +- internal/export/export_test.go | 4 +- internal/importpkg/import_test.go | 28 +- internal/importpkg/reader.go | 16 +- internal/ingest/context.go | 18 +- internal/ingest/handler.go | 12 +- internal/storage/{users.go => actors.go} | 178 +++++------ internal/storage/actors_migration_test.go | 175 +++++++++++ ...users_page_test.go => actors_page_test.go} | 284 +++++++++--------- internal/storage/actors_test.go | 219 ++++++++++++++ internal/storage/art_repro_test.go | 40 +-- internal/storage/backfill_test.go | 2 +- internal/storage/duckdb.go | 94 +++++- internal/storage/export.go | 26 +- internal/storage/pushdown_test.go | 10 +- internal/storage/retention.go | 6 +- internal/storage/retention_test.go | 2 +- internal/storage/schema.sql | 44 +-- internal/storage/schema_version_test.go | 11 +- internal/storage/snapshot_test.go | 2 +- internal/storage/testdata/schema_v10.sql | 159 ++++++++++ internal/storage/users_test.go | 219 -------------- scripts/agent-set-cotel-token.sh | 8 +- scripts/seed-demo.py | 32 +- scripts/shoot-screenshots.mjs | 2 +- 41 files changed, 1708 insertions(+), 1201 deletions(-) create mode 100644 internal/api/actors.go create mode 100644 internal/api/actors_delete_test.go create mode 100644 internal/api/actors_list_test.go delete mode 100644 internal/api/users.go delete mode 100644 internal/api/users_delete_test.go delete mode 100644 internal/api/users_list_test.go rename internal/storage/{users.go => actors.go} (65%) create mode 100644 internal/storage/actors_migration_test.go rename internal/storage/{users_page_test.go => actors_page_test.go} (50%) create mode 100644 internal/storage/actors_test.go create mode 100644 internal/storage/testdata/schema_v10.sql delete mode 100644 internal/storage/users_test.go diff --git a/cmd/cotel/main.go b/cmd/cotel/main.go index ea3445a..9d86835 100644 --- a/cmd/cotel/main.go +++ b/cmd/cotel/main.go @@ -130,7 +130,7 @@ func main() { ingestMux.Handle("/v1/traces", auth.Middleware(db, ingest.New(db))) ro := db.ReadOnly() - apiHandler := api.New(ro).SetPublicIngestURL(parsePublicIngestURL(os.Getenv("COTEL_PUBLIC_INGEST_URL"))).SetUserStore(db) + apiHandler := api.New(ro).SetPublicIngestURL(parsePublicIngestURL(os.Getenv("COTEL_PUBLIC_INGEST_URL"))).SetActorStore(db) dashMux := http.NewServeMux() dashMux.Handle("/api/v1/export", auth.Middleware(db, export.NewHandler(db))) dashMux.Handle("/api/v1/import", auth.Middleware(db, importpkg.NewHandler(db))) diff --git a/internal/api/actors.go b/internal/api/actors.go new file mode 100644 index 0000000..f0ef9d9 --- /dev/null +++ b/internal/api/actors.go @@ -0,0 +1,240 @@ +package api + +import ( + "encoding/json" + "errors" + "net/http" + "strings" + "time" + + "github.com/Flopsstuff/cotel/internal/storage" +) + +// ActorStore is the write-capable interface used by actor management endpoints. +type ActorStore interface { + CreateActor(name string) (storage.Actor, error) + ListActorsPage(opts storage.ListActorsOptions) ([]storage.ActorWithStats, int, error) + GetActorWithStats(id string, since *time.Time) (storage.ActorWithStats, error) + RotateToken(actorID string) (storage.Actor, error) + SoftDeleteActor(actorID string) error + DeleteActorWithHistory(actorID string) error + DeleteAnonymousData() error + GetSetting(key string) (string, error) + SetSetting(key, value string) error +} + +type actorItem struct { + ID string `json:"id"` + Name string `json:"name"` + Token string `json:"token"` + CreatedAt string `json:"created_at"` + TotalCostUSD float64 `json:"cost"` + Sessions int64 `json:"sessions"` + LastSeen *string `json:"last_seen"` +} + +type actorsListResponse struct { + Actors []actorItem `json:"actors"` + Total int `json:"total"` + Page int `json:"page"` + Limit int `json:"limit"` + Range string `json:"range"` + Sort string `json:"sort"` + Order string `json:"order"` +} + +// actorSortKeys whitelists the sort keys the actors list accepts; storage maps +// each to its SELECT-list column. +var actorSortKeys = map[string]string{ + "name": "name", + "created_at": "created_at", + "last_seen": "last_seen", + "cost": "cost", + "sessions": "sessions", +} + +func toActorItem(u storage.ActorWithStats) actorItem { + item := actorItem{ + ID: u.ID, + Name: u.Name, + Token: u.Token, + TotalCostUSD: u.TotalCostUSD, + Sessions: u.Sessions, + } + if u.ID != storage.AnonymousActorID { + item.CreatedAt = u.CreatedAt.UTC().Format(time.RFC3339) + } + if u.LastSeen != nil { + s := u.LastSeen.UTC().Format(time.RFC3339) + item.LastSeen = &s + } + return item +} + +func toActorItemPlain(u storage.Actor) actorItem { + return actorItem{ + ID: u.ID, + Name: u.Name, + Token: u.Token, + CreatedAt: u.CreatedAt.UTC().Format(time.RFC3339), + } +} + +// handleActors dispatches GET (list) and POST (create) on /api/v1/actors. +func (h *Handler) handleActors(w http.ResponseWriter, r *http.Request) { + if h.actorStore == nil { + jsonError(w, "not found", http.StatusNotFound) + return + } + switch r.Method { + case http.MethodGet: + h.listActors(w, r) + case http.MethodPost: + h.createActor(w, r) + default: + jsonError(w, "method not allowed", http.StatusMethodNotAllowed) + } +} + +// handleActorByID dispatches GET (single actor) and DELETE on /api/v1/actors/{id}. +// The special id "__anonymous__" resolves to the synthetic anonymous row (GET) +// or purges all spans with no actor_id (DELETE). +// For named actors: ?mode=actor_only (default) soft-deletes; ?mode=actor_and_history anonymizes spans then hard-deletes. +func (h *Handler) handleActorByID(w http.ResponseWriter, r *http.Request, id string) { + if h.actorStore == nil || id == "" { + jsonError(w, "not found", http.StatusNotFound) + return + } + if r.Method == http.MethodGet { + h.getActor(w, r, id) + return + } + if r.Method != http.MethodDelete { + jsonError(w, "method not allowed", http.StatusMethodNotAllowed) + return + } + + if id == storage.AnonymousActorID { + if err := h.actorStore.DeleteAnonymousData(); err != nil { + jsonError(w, "delete failed", http.StatusInternalServerError) + return + } + w.WriteHeader(http.StatusNoContent) + return + } + + mode := r.URL.Query().Get("mode") + switch mode { + case "": + mode = "actor_only" + case "user_only", "user_and_history": // pre-v11 names (ADR-0027) + mode = "actor_" + strings.TrimPrefix(mode, "user_") + } + + var err error + switch mode { + case "actor_only": + err = h.actorStore.SoftDeleteActor(id) + case "actor_and_history": + err = h.actorStore.DeleteActorWithHistory(id) + default: + jsonError(w, "invalid mode: must be actor_only or actor_and_history", http.StatusBadRequest) + return + } + + if errors.Is(err, storage.ErrNotFound) { + jsonError(w, "not found", http.StatusNotFound) + return + } + if err != nil { + jsonError(w, "delete failed", http.StatusInternalServerError) + return + } + w.WriteHeader(http.StatusNoContent) +} + +// handleActorRotateToken handles POST /api/v1/actors/{id}/rotate-token. +func (h *Handler) handleActorRotateToken(w http.ResponseWriter, r *http.Request, id string) { + if h.actorStore == nil || id == "" { + jsonError(w, "not found", http.StatusNotFound) + return + } + if r.Method != http.MethodPost { + jsonError(w, "method not allowed", http.StatusMethodNotAllowed) + return + } + u, err := h.actorStore.RotateToken(id) + if err != nil { + jsonError(w, "rotate failed", http.StatusInternalServerError) + return + } + w.Header().Set("Content-Type", "application/json") + w.WriteHeader(http.StatusOK) + _ = json.NewEncoder(w).Encode(toActorItemPlain(u)) +} + +func (h *Handler) listActors(w http.ResponseWriter, r *http.Request) { + rangeKey, since := parseRange(r) + sort, order := parseSortOrder(r, actorSortKeys, "cost") + // limit defaults to 0 — unpaginated, which keeps the ActorSearch typeahead whole. + page, limit := parsePaging(r) + + actors, total, err := h.actorStore.ListActorsPage(storage.ListActorsOptions{ + Since: since, + Query: r.URL.Query().Get("q"), + Sort: sort, + Order: order, + Page: page, + Limit: limit, + }) + if err != nil { + jsonError(w, "query failed", http.StatusInternalServerError) + return + } + items := make([]actorItem, 0, len(actors)) + for _, u := range actors { + items = append(items, toActorItem(u)) + } + jsonOK(w, actorsListResponse{ + Actors: items, + Total: total, + Page: page, + Limit: limit, + Range: rangeKey, + Sort: sort, + Order: order, + }) +} + +// getActor handles GET /api/v1/actors/{id}: a single actor with range-scoped stats. +func (h *Handler) getActor(w http.ResponseWriter, r *http.Request, id string) { + _, since := parseRange(r) + u, err := h.actorStore.GetActorWithStats(id, since) + if errors.Is(err, storage.ErrNotFound) { + jsonError(w, "not found", http.StatusNotFound) + return + } + if err != nil { + jsonError(w, "query failed", http.StatusInternalServerError) + return + } + jsonOK(w, toActorItem(u)) +} + +func (h *Handler) createActor(w http.ResponseWriter, r *http.Request) { + var req struct { + Name string `json:"name"` + } + if err := json.NewDecoder(r.Body).Decode(&req); err != nil || strings.TrimSpace(req.Name) == "" { + jsonError(w, "name required", http.StatusBadRequest) + return + } + u, err := h.actorStore.CreateActor(strings.TrimSpace(req.Name)) + if err != nil { + jsonError(w, "create failed", http.StatusInternalServerError) + return + } + w.Header().Set("Content-Type", "application/json") + w.WriteHeader(http.StatusCreated) + _ = json.NewEncoder(w).Encode(toActorItemPlain(u)) +} diff --git a/internal/api/actors_delete_test.go b/internal/api/actors_delete_test.go new file mode 100644 index 0000000..3bdcb36 --- /dev/null +++ b/internal/api/actors_delete_test.go @@ -0,0 +1,155 @@ +package api_test + +import ( + "net/http" + "net/http/httptest" + "testing" + "time" + + "github.com/Flopsstuff/cotel/internal/api" + "github.com/Flopsstuff/cotel/internal/api/auth" + "github.com/Flopsstuff/cotel/internal/storage" +) + +func newActorHandler(t *testing.T, db *storage.DB, ro *storage.ReadDB) http.Handler { + t.Helper() + return api.New(ro).SetActorStore(db) +} + +func deleteActor(t *testing.T, h http.Handler, id, mode string) int { + t.Helper() + path := "/api/v1/actors/" + id + if mode != "" { + path += "?mode=" + mode + } + req := httptest.NewRequest(http.MethodDelete, path, nil) + w := httptest.NewRecorder() + h.ServeHTTP(w, req) + return w.Code +} + +func createAPIActor(t *testing.T, db *storage.DB, name string) storage.Actor { + t.Helper() + u, err := db.CreateActor(name) + if err != nil { + t.Fatalf("CreateActor(%q): %v", name, err) + } + return u +} + +func addAPISpan(t *testing.T, db *storage.DB, spanID, actorID string) { + t.Helper() + now := time.Now() + if err := db.InsertSpan(storage.Span{ + TraceID: "tr1", SpanID: spanID, Name: "llm", SessionID: "s1", + ActorID: actorID, StartTime: now, EndTime: now.Add(time.Second), + }); err != nil { + t.Fatalf("InsertSpan: %v", err) + } +} + +func listAPIActorIDs(t *testing.T, h http.Handler) map[string]bool { + t.Helper() + _, body := getJSON(t, h, "/api/v1/actors") + actors, _ := body["actors"].([]any) + ids := make(map[string]bool) + for _, u := range actors { + m, _ := u.(map[string]any) + if id, ok := m["id"].(string); ok { + ids[id] = true + } + } + return ids +} + +func tokenStatus(db *storage.DB, token string) int { + h := auth.Middleware(db, http.HandlerFunc(func(w http.ResponseWriter, _ *http.Request) { + w.WriteHeader(http.StatusOK) + })) + req := httptest.NewRequest(http.MethodGet, "/", nil) + req.Header.Set("Authorization", "Bearer "+token) + w := httptest.NewRecorder() + h.ServeHTTP(w, req) + return w.Code +} + +// TestDeleteActor_ModeActorOnly verifies soft-delete: actor gone from listing, +// token rejected, spans still reference the original name. +func TestDeleteActor_ModeActorOnly(t *testing.T) { + db, ro := openTestDB(t) + h := newActorHandler(t, db, ro) + u := createAPIActor(t, db, "alice") + addAPISpan(t, db, "sp-alice", "alice") + + if code := deleteActor(t, h, u.ID, "actor_only"); code != http.StatusNoContent { + t.Fatalf("want 204, got %d", code) + } + if listAPIActorIDs(t, h)[u.ID] { + t.Error("alice must not appear in actor list after soft-delete") + } + if tokenStatus(db, u.Token) != http.StatusUnauthorized { + t.Error("soft-deleted actor's token must be rejected") + } +} + +// TestDeleteActor_ModeDefault confirms actor_only is the default when mode is omitted. +func TestDeleteActor_ModeDefault(t *testing.T) { + db, ro := openTestDB(t) + h := newActorHandler(t, db, ro) + u := createAPIActor(t, db, "bob") + + if code := deleteActor(t, h, u.ID, ""); code != http.StatusNoContent { + t.Fatalf("want 204, got %d", code) + } + if listAPIActorIDs(t, h)[u.ID] { + t.Error("bob must not appear in list after default delete") + } +} + +// TestDeleteActor_ModeActorAndHistory verifies anonymization + hard-delete. +func TestDeleteActor_ModeActorAndHistory(t *testing.T) { + db, ro := openTestDB(t) + h := newActorHandler(t, db, ro) + u := createAPIActor(t, db, "carol") + addAPISpan(t, db, "sp-carol", "carol") + + if code := deleteActor(t, h, u.ID, "actor_and_history"); code != http.StatusNoContent { + t.Fatalf("want 204, got %d", code) + } + if listAPIActorIDs(t, h)[u.ID] { + t.Error("carol must not appear in list after actor_and_history delete") + } +} + +// TestDeleteActor_NotFound returns 404 for an unknown actor ID. +func TestDeleteActor_NotFound(t *testing.T) { + db, ro := openTestDB(t) + h := newActorHandler(t, db, ro) + + if code := deleteActor(t, h, "no-such-id", "actor_only"); code != http.StatusNotFound { + t.Errorf("want 404, got %d", code) + } +} + +// TestDeleteActor_AlreadyDeleted returns 404 on a second delete attempt. +func TestDeleteActor_AlreadyDeleted(t *testing.T) { + db, ro := openTestDB(t) + h := newActorHandler(t, db, ro) + u := createAPIActor(t, db, "dave") + + deleteActor(t, h, u.ID, "actor_only") + if code := deleteActor(t, h, u.ID, "actor_only"); code != http.StatusNotFound { + t.Errorf("want 404 on double delete, got %d", code) + } +} + +// TestDeleteActor_InvalidMode returns 400 for an unrecognized mode. +func TestDeleteActor_InvalidMode(t *testing.T) { + db, ro := openTestDB(t) + h := newActorHandler(t, db, ro) + u := createAPIActor(t, db, "eve") + + if code := deleteActor(t, h, u.ID, "nuke_everything"); code != http.StatusBadRequest { + t.Errorf("want 400, got %d", code) + } +} diff --git a/internal/api/actors_list_test.go b/internal/api/actors_list_test.go new file mode 100644 index 0000000..8456594 --- /dev/null +++ b/internal/api/actors_list_test.go @@ -0,0 +1,167 @@ +package api_test + +import ( + "testing" + "time" + + "github.com/Flopsstuff/cotel/internal/storage" +) + +func addCostSpan(t *testing.T, db *storage.DB, spanID, actor, session string, cost float64) { + t.Helper() + now := time.Now() + insertSpan(t, db, storage.Span{ + TraceID: "tr", SpanID: spanID, Name: "llm", SessionID: session, + ActorID: actor, StartTime: now.Add(-1 * time.Hour), EndTime: now.Add(-1 * time.Hour).Add(time.Second), + CostUSD: ptr(cost), + }) +} + +// TestListActors_EchoAndDefaults confirms the response carries the echo fields +// with their defaults when no query parameters are supplied. +func TestListActors_EchoAndDefaults(t *testing.T) { + db, ro := openTestDB(t) + h := newActorHandler(t, db, ro) + createAPIActor(t, db, "alice") + + code, body := getJSON(t, h, "/api/v1/actors") + if code != 200 { + t.Fatalf("want 200, got %d", code) + } + if body["range"] != "month" || body["sort"] != "cost" || body["order"] != "desc" { + t.Errorf("defaults: range/sort/order = %v/%v/%v", body["range"], body["sort"], body["order"]) + } + if body["limit"].(float64) != 0 { + t.Errorf("default limit: want 0, got %v", body["limit"]) + } + if body["total"].(float64) != 1 { + t.Errorf("total: want 1, got %v", body["total"]) + } +} + +// TestListActors_GlobalSortAcrossPages proves paging sits on top of a global sort: +// page 1 (limit 2, cost desc) holds the two globally most expensive actors. +func TestListActors_GlobalSortAcrossPages(t *testing.T) { + db, ro := openTestDB(t) + h := newActorHandler(t, db, ro) + createAPIActor(t, db, "cheap") + createAPIActor(t, db, "mid") + createAPIActor(t, db, "pricey") + addCostSpan(t, db, "c1", "cheap", "sc", 10) + addCostSpan(t, db, "m1", "mid", "sm", 20) + addCostSpan(t, db, "p1", "pricey", "sp", 30) + + _, body := getJSON(t, h, "/api/v1/actors?range=all&sort=cost&order=desc&page=1&limit=2") + actors, _ := body["actors"].([]any) + if len(actors) != 2 { + t.Fatalf("page 1 limit 2: want 2 actors, got %d", len(actors)) + } + if body["total"].(float64) != 3 { + t.Errorf("total: want 3, got %v", body["total"]) + } + first := actors[0].(map[string]any) + second := actors[1].(map[string]any) + if first["name"] != "pricey" || second["name"] != "mid" { + t.Errorf("global sort broken: page 1 = %v, %v", first["name"], second["name"]) + } +} + +// TestListActors_QueryFilter filters server-side on name. +func TestListActors_QueryFilter(t *testing.T) { + db, ro := openTestDB(t) + h := newActorHandler(t, db, ro) + createAPIActor(t, db, "alice") + createAPIActor(t, db, "bob") + + _, body := getJSON(t, h, "/api/v1/actors?q=ali") + actors, _ := body["actors"].([]any) + if len(actors) != 1 || body["total"].(float64) != 1 { + t.Fatalf("q=ali: want 1 actor/total, got %d actors total=%v", len(actors), body["total"]) + } + if actors[0].(map[string]any)["name"] != "alice" { + t.Errorf("q=ali: want alice, got %v", actors[0].(map[string]any)["name"]) + } +} + +// TestListActors_InvalidParamsFallBack confirms bad values fall back to defaults +// rather than erroring (trust the boundary). +func TestListActors_InvalidParamsFallBack(t *testing.T) { + db, ro := openTestDB(t) + h := newActorHandler(t, db, ro) + createAPIActor(t, db, "alice") + + code, body := getJSON(t, h, "/api/v1/actors?range=decade&sort=whatever&order=sideways") + if code != 200 { + t.Fatalf("want 200 (no 400), got %d", code) + } + if body["range"] != "month" || body["sort"] != "cost" || body["order"] != "desc" { + t.Errorf("fallbacks: range/sort/order = %v/%v/%v", body["range"], body["sort"], body["order"]) + } +} + +// TestGetActor_ByID returns a single actor and 404s for unknown ids. +func TestGetActor_ByID(t *testing.T) { + db, ro := openTestDB(t) + h := newActorHandler(t, db, ro) + u := createAPIActor(t, db, "alice") + addCostSpan(t, db, "a1", "alice", "sa", 12) + + code, body := getJSON(t, h, "/api/v1/actors/"+u.ID+"?range=all") + if code != 200 { + t.Fatalf("want 200, got %d", code) + } + if body["name"] != "alice" || body["token"] != u.Token { + t.Errorf("identity mismatch: %v", body) + } + if body["cost"].(float64) != 12 { + t.Errorf("cost: want 12, got %v", body["cost"]) + } + + if code, _ := getJSON(t, h, "/api/v1/actors/no-such-id"); code != 404 { + t.Errorf("unknown id: want 404, got %d", code) + } +} + +// TestGetActor_Anonymous resolves the synthetic anonymous row. +func TestGetActor_Anonymous(t *testing.T) { + db, ro := openTestDB(t) + h := newActorHandler(t, db, ro) + addCostSpan(t, db, "anon1", "", "sanon", 9) + + code, body := getJSON(t, h, "/api/v1/actors/__anonymous__?range=all") + if code != 200 { + t.Fatalf("want 200, got %d", code) + } + if body["id"] != "__anonymous__" || body["cost"].(float64) != 9 { + t.Errorf("anon: got %v", body) + } +} + +// The pre-v11 spellings keep working after the users → actors rename +// (ADR-0027): the /users route, the user_id filter and the user_* delete modes. +func TestLegacyUserSpellings(t *testing.T) { + db, ro := openTestDB(t) + h := newActorHandler(t, db, ro) + alice := createAPIActor(t, db, "alice") + createAPIActor(t, db, "bob") + addCostSpan(t, db, "s1", "alice", "sess-a", 1.0) + addCostSpan(t, db, "s2", "bob", "sess-b", 2.0) + + code, body := getJSON(t, h, "/api/v1/users") + if code != 200 || body["total"].(float64) != 2 { + t.Fatalf("/users alias: code %d, total %v", code, body["total"]) + } + code, body = getJSON(t, h, "/api/v1/users/"+alice.ID) + if code != 200 || body["name"] != "alice" { + t.Fatalf("/users/{id} alias: code %d, body %v", code, body) + } + + _, body = getJSON(t, h, "/api/v1/overview?range=all&user_id=alice") + if body["sessions_count"].(float64) != 1 { + t.Fatalf("user_id filter alias: sessions_count %v, want 1", body["sessions_count"]) + } + + if code := deleteActor(t, h, alice.ID, "user_only"); code != 204 { + t.Fatalf("mode=user_only alias: code %d, want 204", code) + } +} diff --git a/internal/api/auth/middleware.go b/internal/api/auth/middleware.go index a66dd8b..ae0dece 100644 --- a/internal/api/auth/middleware.go +++ b/internal/api/auth/middleware.go @@ -12,14 +12,14 @@ import ( // AuthDB is the database interface required by Middleware. // *storage.DB satisfies this interface. type AuthDB interface { - GetUserByToken(token string) (*storage.User, error) + GetActorByToken(token string) (*storage.Actor, error) GetSettingBool(key string, defaultVal bool) bool } // Middleware enforces the allow_anonymous auth policy shared across all cotel endpoints. // // Rules: -// - valid cotel_* bearer → identify the user (injected into ctx), allow +// - valid cotel_* bearer → identify the actor (injected into ctx), allow // - no bearer + allow_anonymous=true → allow anonymously // - no bearer + allow_anonymous=false → 401 // - invalid/unknown bearer → 401 @@ -27,9 +27,9 @@ func Middleware(db AuthDB, next http.Handler) http.Handler { return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { bearer := strings.TrimPrefix(r.Header.Get("Authorization"), "Bearer ") if strings.HasPrefix(bearer, "cotel_") { - user, err := db.GetUserByToken(bearer) - if err == nil && user != nil { - ctx := ingest.WithUserName(r.Context(), user.Name) + actor, err := db.GetActorByToken(bearer) + if err == nil && actor != nil { + ctx := ingest.WithActorName(r.Context(), actor.Name) next.ServeHTTP(w, r.WithContext(ctx)) return } diff --git a/internal/api/auth/middleware_test.go b/internal/api/auth/middleware_test.go index 536a109..5ed74c2 100644 --- a/internal/api/auth/middleware_test.go +++ b/internal/api/auth/middleware_test.go @@ -10,13 +10,13 @@ import ( ) type mockAuthDB struct { - user *storage.User + actor *storage.Actor allowAnonymous bool } -func (m *mockAuthDB) GetUserByToken(token string) (*storage.User, error) { - if m.user != nil && m.user.Token == token { - return m.user, nil +func (m *mockAuthDB) GetActorByToken(token string) (*storage.Actor, error) { + if m.actor != nil && m.actor.Token == token { + return m.actor, nil } return nil, ¬FoundErr{} } @@ -57,7 +57,7 @@ func TestMiddleware_AnonymousDenied(t *testing.T) { func TestMiddleware_ValidToken(t *testing.T) { db := &mockAuthDB{ - user: &storage.User{Name: "alice", Token: "cotel_abc123"}, + actor: &storage.Actor{Name: "alice", Token: "cotel_abc123"}, allowAnonymous: false, } h := auth.Middleware(db, okHandler) diff --git a/internal/api/handler.go b/internal/api/handler.go index e433bbd..41fe8e0 100644 --- a/internal/api/handler.go +++ b/internal/api/handler.go @@ -25,7 +25,7 @@ type DB interface { type Handler struct { db DB tokenDB TokenStore - userStore UserStore + actorStore ActorStore publicIngestURL string } @@ -40,9 +40,9 @@ func (h *Handler) SetTokenStore(ts TokenStore) *Handler { return h } -// SetUserStore attaches a writable store for the /users and /settings endpoints. -func (h *Handler) SetUserStore(us UserStore) *Handler { - h.userStore = us +// SetActorStore attaches a writable store for the /actors and /settings endpoints. +func (h *Handler) SetActorStore(us ActorStore) *Handler { + h.actorStore = us return h } @@ -63,6 +63,11 @@ func (h *Handler) ServeHTTP(w http.ResponseWriter, r *http.Request) { } path := strings.TrimPrefix(r.URL.Path, "/api/v1") + // /users is the pre-v11 name of /actors (ADR-0027); kept so bookmarks and + // scripts written against it keep working. + if path == "/users" || strings.HasPrefix(path, "/users/") { + path = "/actors" + strings.TrimPrefix(path, "/users") + } switch { case path == "/overview": @@ -81,14 +86,14 @@ func (h *Handler) ServeHTTP(w http.ResponseWriter, r *http.Request) { h.handleModels(w, r) case path == "/history" || path == "/history/": h.handleHistory(w, r) - case path == "/users" || path == "/users/": - h.handleUsers(w, r) - case strings.HasPrefix(path, "/users/"): - rest := strings.TrimPrefix(path, "/users/") + case path == "/actors" || path == "/actors/": + h.handleActors(w, r) + case strings.HasPrefix(path, "/actors/"): + rest := strings.TrimPrefix(path, "/actors/") if strings.HasSuffix(rest, "/rotate-token") { - h.handleUserRotateToken(w, r, strings.TrimSuffix(rest, "/rotate-token")) + h.handleActorRotateToken(w, r, strings.TrimSuffix(rest, "/rotate-token")) } else { - h.handleUserByID(w, r, rest) + h.handleActorByID(w, r, rest) } case path == "/settings" || path == "/settings/": h.handleSettings(w, r) @@ -227,28 +232,31 @@ func likeFilter(column, q string) (clause string, args []any) { return " AND " + column + ` ILIKE ? ESCAPE '\'`, []any{"%" + escapeLike(q) + "%"} } -// userIDClause returns an additional SQL WHERE fragment and its argument when -// the request contains a non-empty user_id query parameter. The caller appends +// actorIDClause returns an additional SQL WHERE fragment and its argument when +// the request contains a non-empty actor_id query parameter. The caller appends // the clause directly to the end of their existing WHERE and appends arg to -// their args slice (only when arg is non-empty). When no user_id is present +// their args slice (only when arg is non-empty). When no actor_id is present // both return values are empty strings. // -// The special value "__anonymous__" maps to user_id IS NULL (spans with no user_id). -func userIDClause(r *http.Request) (clause string, arg string) { - return userIDClauseOn(r, "") +// The special value "__anonymous__" maps to actor_id IS NULL (spans with no actor_id). +func actorIDClause(r *http.Request) (clause string, arg string) { + return actorIDClauseOn(r, "") } -// userIDClauseOn is userIDClause with the column qualified by a table alias +// actorIDClauseOn is actorIDClause with the column qualified by a table alias // prefix (e.g. "du."), for queries that reference more than one table. -func userIDClauseOn(r *http.Request, prefix string) (clause string, arg string) { - uid := r.URL.Query().Get("user_id") +func actorIDClauseOn(r *http.Request, prefix string) (clause string, arg string) { + uid := r.URL.Query().Get("actor_id") + if uid == "" { + uid = r.URL.Query().Get("user_id") // pre-v11 name (ADR-0027) + } if uid == "" { return "", "" } if uid == "__anonymous__" { - return " AND " + prefix + "user_id IS NULL", "" + return " AND " + prefix + "actor_id IS NULL", "" } - return " AND " + prefix + "user_id = ?", uid + return " AND " + prefix + "actor_id = ?", uid } // usageCTE unions raw spans with rolled-up daily_usage into one row set that @@ -275,7 +283,7 @@ usage AS ( CAST(CAST(start_time AS TIMESTAMP) AS DATE) AS day, start_time AS first_seen, NULLIF(session_id, '') AS session_id, - user_id, + actor_id, NULLIF(model, '') AS model, NULLIF(tool_name, '') AS tool_name, CAST(1 AS BIGINT) AS spans, @@ -290,7 +298,7 @@ usage AS ( du.day, CAST(du.day AS TIMESTAMPTZ), NULLIF(du.session_id, '%[2]s'), - du.user_id, + du.actor_id, NULLIF(du.model, '%[2]s'), NULLIF(du.tool_name, '%[2]s'), du.span_count, @@ -303,7 +311,7 @@ usage AS ( )` // usageFilter carries the WHERE fragments that scope usageCTE to a time window -// and, optionally, one user, together with their arguments in statement order — +// and, optionally, one actor, together with their arguments in statement order — // raw side first, aggregate side second. type usageFilter struct { raw string @@ -314,8 +322,8 @@ type usageFilter struct { // newUsageFilter builds the scoping fragments for usageCTE. from and to are // inclusive bounds; nil leaves that side unbounded. func newUsageFilter(r *http.Request, from, to *time.Time) usageFilter { - rawUID, rawUIDArg := userIDClause(r) - aggUID, aggUIDArg := userIDClauseOn(r, "du.") + rawUID, rawUIDArg := actorIDClause(r) + aggUID, aggUIDArg := actorIDClauseOn(r, "du.") f := usageFilter{raw: rawUID, agg: aggUID} if rawUIDArg != "" { @@ -347,7 +355,7 @@ func (f usageFilter) cte() string { return fmt.Sprintf(usageCTE, f.raw, storage.UnknownSentinel, f.agg) } -// ---- /api/v1/users — delegated to users.go ---- +// ---- /api/v1/actors — delegated to actors.go ---- // ---- /api/v1/health ---- @@ -497,7 +505,7 @@ func (h *Handler) setting(key string) (string, error) { type overviewResponse struct { Range string `json:"range"` SessionsCount int64 `json:"sessions_count"` - UsersCount int64 `json:"users_count"` + ActorsCount int64 `json:"actors_count"` TotalCostUSD float64 `json:"total_cost_usd"` TotalInputTokens int64 `json:"total_input_tokens"` TotalOutputTokens int64 `json:"total_output_tokens"` @@ -534,12 +542,12 @@ func (h *Handler) handleOverview(w http.ResponseWriter, r *http.Request) { TopTools: []topToolRow{}, } - // The anonymous bucket has no user_id, so COUNT(DISTINCT user_id) would drop - // it; it is one principal on the users list and counts as one here too. + // The anonymous bucket has no actor_id, so COUNT(DISTINCT actor_id) would drop + // 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 user_id) + CASE WHEN COUNT(*) FILTER (WHERE user_id IS NULL) > 0 THEN 1 ELSE 0 END, + 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), COALESCE(SUM(output_tokens), 0), @@ -547,7 +555,7 @@ SELECT FROM usage `, f.args...).Scan( &resp.SessionsCount, - &resp.UsersCount, + &resp.ActorsCount, &resp.TotalCostUSD, &resp.TotalInputTokens, &resp.TotalOutputTokens, @@ -636,7 +644,7 @@ GROUP BY tool_name ORDER BY call_count DESC, tool_name ASC LIMIT 5 type sessionItem struct { SessionID string `json:"session_id"` - UserID string `json:"user_id"` + ActorID string `json:"actor_id"` FirstSeen string `json:"first_seen"` LastSeen string `json:"last_seen"` Model string `json:"model"` @@ -688,7 +696,7 @@ func (h *Handler) handleSessions(w http.ResponseWriter, r *http.Request) { sortExpr = "MIN(start_time)" } - uidClause, uid := userIDClause(r) + uidClause, uid := actorIDClause(r) sinceClause := "" scopeArgs := []any{} if uid != "" { @@ -718,7 +726,7 @@ func (h *Handler) handleSessions(w http.ResponseWriter, r *http.Request) { COALESCE(SUM(output_tokens), 0), COUNT(*) FILTER (WHERE tool_name IS NOT NULL AND tool_name <> ''), MAX(CASE WHEN status_code = 2 THEN 1 ELSE 0 END), - COALESCE(MAX(user_id), '') + COALESCE(MAX(actor_id), '') FROM spans WHERE `+hasSession+`%s GROUP BY session_id @@ -740,7 +748,7 @@ func (h *Handler) handleSessions(w http.ResponseWriter, r *http.Request) { var firstSeen, lastSeen time.Time var hasError int if err := rows.Scan(&s.SessionID, &firstSeen, &lastSeen, &s.Model, - &s.CostUSD, &s.InputTokens, &s.OutputTokens, &s.ToolCalls, &hasError, &s.UserID); err != nil { + &s.CostUSD, &s.InputTokens, &s.OutputTokens, &s.ToolCalls, &hasError, &s.ActorID); err != nil { queryFailed(w) return } @@ -1137,8 +1145,8 @@ func (h *Handler) handleTools(w http.ResponseWriter, r *http.Request) { sort, order := parseSortOrder(r, toolSortExprs, "calls") page, limit := parsePaging(r) - rawUID, rawUIDArg := userIDClause(r) - aggUID, aggUIDArg := userIDClauseOn(r, "du.") + rawUID, rawUIDArg := actorIDClause(r) + aggUID, aggUIDArg := actorIDClauseOn(r, "du.") var rawSince, aggSince string filterArgs := []any{} @@ -1220,7 +1228,7 @@ ORDER BY %s %s NULLS LAST, name ASC%s // row that predates schema v9. It is nil when no such row exists, or when the // gap it describes falls entirely before the selected range. func (h *Handler) durationStatsSince(r *http.Request, since *time.Time) *string { - aggUID, aggUIDArg := userIDClauseOn(r, "du.") + aggUID, aggUIDArg := actorIDClauseOn(r, "du.") args := []any{} if aggUIDArg != "" { args = append(args, aggUIDArg) @@ -1294,7 +1302,7 @@ func (h *Handler) handleBashCommands(w http.ResponseWriter, r *http.Request) { sort, order := parseSortOrder(r, bashSortExprs, "calls") page, limit := parsePaging(r) - uidClause, uid := userIDClause(r) + uidClause, uid := actorIDClause(r) filterArgs := []any{} if uid != "" { filterArgs = append(filterArgs, uid) @@ -1389,7 +1397,7 @@ ORDER BY %s %s NULLS LAST, command ASC%s // fully covered by raw spans. Shared by the endpoints that cannot answer from // the roll-up — the Bash breakdown (ADR-0012) and the sessions list (ADR-0014). func (h *Handler) rawCoveredSince(r *http.Request, since *time.Time) *string { - aggUID, aggUIDArg := userIDClauseOn(r, "du.") + aggUID, aggUIDArg := actorIDClauseOn(r, "du.") args := []any{} if aggUIDArg != "" { args = append(args, aggUIDArg) @@ -1623,7 +1631,7 @@ func (h *Handler) handleHistory(w http.ResponseWriter, r *http.Request) { return } - uidClause, uid := userIDClause(r) + uidClause, uid := actorIDClause(r) window, args := rawWindow(from, to) if uid != "" { args = append(args, uid) @@ -1663,7 +1671,7 @@ func (h *Handler) handleHistory(w http.ResponseWriter, r *http.Request) { // granularity the roll-up cannot express. func (h *Handler) historyRawSeries(resp *historyResponse, r *http.Request, gran string, from, to *time.Time) error { bucketExpr := historyBucketExpr(gran) - uidClause, uid := userIDClause(r) + uidClause, uid := actorIDClause(r) window, args := rawWindow(from, to) if uid != "" { args = append(args, uid) diff --git a/internal/api/handler_test.go b/internal/api/handler_test.go index 9508e01..0f316b9 100644 --- a/internal/api/handler_test.go +++ b/internal/api/handler_test.go @@ -341,12 +341,12 @@ func TestSessions(t *testing.T) { }, }, { - name: "surfaces user_id", + name: "surfaces actor_id", seed: func(t *testing.T, db *storage.DB) { now := time.Now() insertSpan(t, db, storage.Span{ TraceID: "t1", SpanID: "s1", Name: "llm", SessionID: "sess1", - UserID: "alice", StartTime: now, EndTime: now.Add(time.Second), + ActorID: "alice", StartTime: now, EndTime: now.Add(time.Second), }) }, path: "/api/v1/sessions", @@ -357,8 +357,8 @@ func TestSessions(t *testing.T) { t.Fatalf("want 1 item, got %v", body["items"]) } item := items[0].(map[string]any) - if item["user_id"] != "alice" { - t.Errorf("want user_id=alice, got %v", item["user_id"]) + if item["actor_id"] != "alice" { + t.Errorf("want actor_id=alice, got %v", item["actor_id"]) } }, }, diff --git a/internal/api/history_grid_test.go b/internal/api/history_grid_test.go index 00f1e1c..2751bd9 100644 --- a/internal/api/history_grid_test.go +++ b/internal/api/history_grid_test.go @@ -25,7 +25,7 @@ func seedGridFixture(t *testing.T, db *storage.DB) { start := anchor.Add(offset) insertSpan(t, db, storage.Span{ TraceID: "tr", SpanID: string(rune('a'+i)) + "-grid", Name: "llm", - SessionID: "s-grid", Model: "sonnet", ToolName: "Bash", UserID: "alice", + SessionID: "s-grid", Model: "sonnet", ToolName: "Bash", ActorID: "alice", StartTime: start, EndTime: start.Add(time.Second), CostUSD: ptr(1.0), InputTokens: ptr(int64(10)), OutputTokens: ptr(int64(1)), }) diff --git a/internal/api/history_range_test.go b/internal/api/history_range_test.go index 32baa1c..717b9c8 100644 --- a/internal/api/history_range_test.go +++ b/internal/api/history_range_test.go @@ -294,9 +294,9 @@ func TestHistory_CoarseGranularitiesSpanTheUnion(t *testing.T) { } } -// TestHistory_RangeComposesWithUserID checks the two filters intersect on both +// TestHistory_RangeComposesWithActorID checks the two filters intersect on both // sides of the union rather than one overriding the other. -func TestHistory_RangeComposesWithUserID(t *testing.T) { +func TestHistory_RangeComposesWithActorID(t *testing.T) { db, ro := openTestDB(t) seedRangeFixture(t, db) h := api.New(ro) @@ -306,10 +306,10 @@ func TestHistory_RangeComposesWithUserID(t *testing.T) { spans float64 cost float64 }{ - {"/api/v1/history?granularity=day&range=all&user_id=alice", 2, 3}, - {"/api/v1/history?granularity=day&range=all&user_id=bob", 3, 4}, - {"/api/v1/history?granularity=day&range=all&user_id=carol", 5, 8}, - {"/api/v1/history?granularity=day&range=month&user_id=bob", 0, 0}, + {"/api/v1/history?granularity=day&range=all&actor_id=alice", 2, 3}, + {"/api/v1/history?granularity=day&range=all&actor_id=bob", 3, 4}, + {"/api/v1/history?granularity=day&range=all&actor_id=carol", 5, 8}, + {"/api/v1/history?granularity=day&range=month&actor_id=bob", 0, 0}, } for _, tc := range cases { t.Run(tc.path, func(t *testing.T) { diff --git a/internal/api/overview_range_test.go b/internal/api/overview_range_test.go index 1f96679..fe9d220 100644 --- a/internal/api/overview_range_test.go +++ b/internal/api/overview_range_test.go @@ -15,7 +15,7 @@ type usageRow struct { session string model string tool string - user string + actor string spans int64 cost float64 in, out int64 @@ -23,17 +23,17 @@ type usageRow struct { func addUsageRow(t *testing.T, db *storage.DB, r usageRow) { t.Helper() - var user any - if r.user != "" { - user = r.user + var actor any + if r.actor != "" { + actor = r.actor } _, err := db.Exec(` INSERT INTO daily_usage - (day, session_id, model, tool_name, user_id, span_count, + (day, session_id, model, tool_name, actor_id, span_count, total_input_tokens, total_output_tokens, total_cache_read_tokens, total_cache_write_tokens, total_cost_usd) VALUES (CAST(? AS DATE), ?, ?, ?, ?, ?, ?, ?, 0, 0, ?) - `, r.day.UTC().Format("2006-01-02"), r.session, r.model, r.tool, user, + `, r.day.UTC().Format("2006-01-02"), r.session, r.model, r.tool, actor, r.spans, r.in, r.out, r.cost) if err != nil { t.Fatalf("insert daily_usage: %v", err) @@ -57,28 +57,28 @@ func seedRangeFixture(t *testing.T, db *storage.DB) { insertSpan(t, db, storage.Span{ TraceID: "tr", SpanID: "raw-floor", Name: "llm", - SessionID: "s-raw-1", Model: "sonnet", ToolName: "Bash", UserID: "alice", + SessionID: "s-raw-1", Model: "sonnet", ToolName: "Bash", ActorID: "alice", StartTime: floor, EndTime: floor.Add(time.Second), CostUSD: ptr(2.0), InputTokens: ptr(int64(20)), OutputTokens: ptr(int64(2)), }) recent := time.Now().Add(-time.Hour) insertSpan(t, db, storage.Span{ TraceID: "tr", SpanID: "raw-recent", Name: "llm", - SessionID: "s-raw-2", Model: "sonnet", ToolName: "Read", UserID: "alice", + SessionID: "s-raw-2", Model: "sonnet", ToolName: "Read", ActorID: "alice", StartTime: recent, EndTime: recent.Add(time.Second), CostUSD: ptr(1.0), InputTokens: ptr(int64(10)), OutputTokens: ptr(int64(1)), }) addUsageRow(t, db, usageRow{ - day: floor, session: "s-floor", model: "opus", tool: "Grep", user: "bob", + day: floor, session: "s-floor", model: "opus", tool: "Grep", actor: "bob", spans: 7, cost: 99, in: 700, out: 70, }) addUsageRow(t, db, usageRow{ - day: floor.AddDate(0, 0, -55), session: "s-year", model: "opus", tool: "Grep", user: "bob", + day: floor.AddDate(0, 0, -55), session: "s-year", model: "opus", tool: "Grep", actor: "bob", spans: 3, cost: 4, in: 40, out: 4, }) addUsageRow(t, db, usageRow{ - day: floor.AddDate(0, 0, -395), session: "s-old", model: "haiku", tool: "Glob", user: "carol", + day: floor.AddDate(0, 0, -395), session: "s-old", model: "haiku", tool: "Glob", actor: "carol", spans: 5, cost: 8, in: 80, out: 8, }) } @@ -115,7 +115,7 @@ func TestOverview_RangeAcrossUnion(t *testing.T) { cases := []struct { rangeKey string sessions float64 - users float64 + actors float64 cost float64 input float64 output float64 @@ -139,8 +139,8 @@ func TestOverview_RangeAcrossUnion(t *testing.T) { if got := num(t, body, "sessions_count"); got != tc.sessions { t.Errorf("sessions_count: want %v, got %v", tc.sessions, got) } - if got := num(t, body, "users_count"); got != tc.users { - t.Errorf("users_count: want %v, got %v", tc.users, got) + if got := num(t, body, "actors_count"); got != tc.actors { + t.Errorf("actors_count: want %v, got %v", tc.actors, got) } if got := num(t, body, "total_cost_usd"); got != tc.cost { t.Errorf("total_cost_usd: want %v, got %v", tc.cost, got) @@ -178,10 +178,10 @@ func TestOverview_FloorDayNotDoubleCounted(t *testing.T) { } } -// TestOverview_RangeComposesWithUserID checks the two filters intersect rather -// than one overriding the other, including for users_count, which used to +// TestOverview_RangeComposesWithActorID checks the two filters intersect rather +// than one overriding the other, including for actors_count, which used to // ignore both. -func TestOverview_RangeComposesWithUserID(t *testing.T) { +func TestOverview_RangeComposesWithActorID(t *testing.T) { db, ro := openTestDB(t) seedRangeFixture(t, db) h := api.New(ro) @@ -189,14 +189,14 @@ func TestOverview_RangeComposesWithUserID(t *testing.T) { cases := []struct { path string sessions float64 - users float64 + actors float64 cost float64 }{ - {"/api/v1/overview?range=all&user_id=alice", 2, 1, 3}, - {"/api/v1/overview?range=all&user_id=bob", 1, 1, 4}, - {"/api/v1/overview?range=all&user_id=carol", 1, 1, 8}, - {"/api/v1/overview?range=month&user_id=bob", 0, 0, 0}, - {"/api/v1/overview?range=week&user_id=alice", 2, 1, 3}, + {"/api/v1/overview?range=all&actor_id=alice", 2, 1, 3}, + {"/api/v1/overview?range=all&actor_id=bob", 1, 1, 4}, + {"/api/v1/overview?range=all&actor_id=carol", 1, 1, 8}, + {"/api/v1/overview?range=month&actor_id=bob", 0, 0, 0}, + {"/api/v1/overview?range=week&actor_id=alice", 2, 1, 3}, } for _, tc := range cases { t.Run(tc.path, func(t *testing.T) { @@ -204,8 +204,8 @@ func TestOverview_RangeComposesWithUserID(t *testing.T) { if got := num(t, body, "sessions_count"); got != tc.sessions { t.Errorf("sessions_count: want %v, got %v", tc.sessions, got) } - if got := num(t, body, "users_count"); got != tc.users { - t.Errorf("users_count: want %v, got %v", tc.users, got) + if got := num(t, body, "actors_count"); got != tc.actors { + t.Errorf("actors_count: want %v, got %v", tc.actors, got) } if got := num(t, body, "total_cost_usd"); got != tc.cost { t.Errorf("total_cost_usd: want %v, got %v", tc.cost, got) @@ -404,7 +404,7 @@ func TestSessions_BlankSessionIDIsNotASession(t *testing.T) { seedRangeFixture(t, db) now := time.Now().Add(-2 * time.Hour) insertSpan(t, db, storage.Span{ - TraceID: "tr", SpanID: "no-session", Name: "llm", SessionID: "", UserID: "alice", + TraceID: "tr", SpanID: "no-session", Name: "llm", SessionID: "", ActorID: "alice", StartTime: now, EndTime: now.Add(time.Second), }) h := api.New(ro) diff --git a/internal/api/settings.go b/internal/api/settings.go index 46545b8..839c205 100644 --- a/internal/api/settings.go +++ b/internal/api/settings.go @@ -11,7 +11,7 @@ type settingsResponse struct { // handleSettings dispatches GET and PUT on /api/v1/settings. func (h *Handler) handleSettings(w http.ResponseWriter, r *http.Request) { - if h.userStore == nil { + if h.actorStore == nil { jsonError(w, "not found", http.StatusNotFound) return } @@ -26,7 +26,7 @@ func (h *Handler) handleSettings(w http.ResponseWriter, r *http.Request) { } func (h *Handler) getSettings(w http.ResponseWriter) { - val, err := h.userStore.GetSetting("allow_anonymous") + val, err := h.actorStore.GetSetting("allow_anonymous") anon := true // default on if err == nil { anon = val == "true" @@ -46,7 +46,7 @@ func (h *Handler) putSettings(w http.ResponseWriter, r *http.Request) { if req.AllowAnonymous { val = "true" } - if err := h.userStore.SetSetting("allow_anonymous", val); err != nil { + if err := h.actorStore.SetSetting("allow_anonymous", val); err != nil { jsonError(w, "update failed", http.StatusInternalServerError) return } diff --git a/internal/api/tools_list_test.go b/internal/api/tools_list_test.go index 599993b..fcb0a0a 100644 --- a/internal/api/tools_list_test.go +++ b/internal/api/tools_list_test.go @@ -42,7 +42,7 @@ func addAggRow(t *testing.T, db *storage.DB, r aggRow) { t.Helper() _, err := db.Exec(` INSERT INTO daily_usage - (day, session_id, model, tool_name, user_id, span_count, total_cost_usd, total_duration_ms, fail_count) + (day, session_id, model, tool_name, actor_id, span_count, total_cost_usd, total_duration_ms, fail_count) VALUES (CAST(? AS DATE), ?, 'm', ?, NULL, ?, 0, ?, ?) `, r.day.UTC().Format("2006-01-02"), "agg-"+r.tool, r.tool, r.spanCount, r.durationMS, r.failCount) if err != nil { @@ -335,24 +335,24 @@ func TestTools_QueryFilter(t *testing.T) { } } -// TestTools_UserFilterStillWorks guards the pre-existing user_id filter across +// TestTools_ActorFilterStillWorks guards the pre-existing actor_id filter across // the rewrite, on both halves of the union. -func TestTools_UserFilterStillWorks(t *testing.T) { +func TestTools_ActorFilterStillWorks(t *testing.T) { db, ro := openTestDB(t) now := time.Now().Add(-time.Hour) insertSpan(t, db, storage.Span{ TraceID: "tr", SpanID: "u1", Name: "tool", SessionID: "s", ToolName: "Bash", - UserID: "alice", StartTime: now, EndTime: now.Add(100 * time.Millisecond), + ActorID: "alice", StartTime: now, EndTime: now.Add(100 * time.Millisecond), }) insertSpan(t, db, storage.Span{ TraceID: "tr", SpanID: "u2", Name: "tool", SessionID: "s", ToolName: "Grep", - UserID: "bob", StartTime: now, EndTime: now.Add(100 * time.Millisecond), + ActorID: "bob", StartTime: now, EndTime: now.Add(100 * time.Millisecond), }) h := api.New(ro) - _, body := getJSON(t, h, "/api/v1/tools?range=all&user_id=alice") + _, body := getJSON(t, h, "/api/v1/tools?range=all&actor_id=alice") if names := toolNames(t, body); len(names) != 1 || names[0] != "Bash" { - t.Errorf("user_id=alice: want [Bash], got %v", names) + t.Errorf("actor_id=alice: want [Bash], got %v", names) } } diff --git a/internal/api/users.go b/internal/api/users.go deleted file mode 100644 index b118303..0000000 --- a/internal/api/users.go +++ /dev/null @@ -1,237 +0,0 @@ -package api - -import ( - "encoding/json" - "errors" - "net/http" - "strings" - "time" - - "github.com/Flopsstuff/cotel/internal/storage" -) - -// UserStore is the write-capable interface used by user management endpoints. -type UserStore interface { - CreateUser(name string) (storage.User, error) - ListUsersPage(opts storage.ListUsersOptions) ([]storage.UserWithStats, int, error) - GetUserWithStats(id string, since *time.Time) (storage.UserWithStats, error) - RotateToken(userID string) (storage.User, error) - SoftDeleteUser(userID string) error - DeleteUserWithHistory(userID string) error - DeleteAnonymousData() error - GetSetting(key string) (string, error) - SetSetting(key, value string) error -} - -type userItem struct { - ID string `json:"id"` - Name string `json:"name"` - Token string `json:"token"` - CreatedAt string `json:"created_at"` - TotalCostUSD float64 `json:"cost"` - Sessions int64 `json:"sessions"` - LastSeen *string `json:"last_seen"` -} - -type usersListResponse struct { - Users []userItem `json:"users"` - Total int `json:"total"` - Page int `json:"page"` - Limit int `json:"limit"` - Range string `json:"range"` - Sort string `json:"sort"` - Order string `json:"order"` -} - -// userSortKeys whitelists the sort keys the users list accepts; storage maps -// each to its SELECT-list column. -var userSortKeys = map[string]string{ - "name": "name", - "created_at": "created_at", - "last_seen": "last_seen", - "cost": "cost", - "sessions": "sessions", -} - -func toUserItem(u storage.UserWithStats) userItem { - item := userItem{ - ID: u.ID, - Name: u.Name, - Token: u.Token, - TotalCostUSD: u.TotalCostUSD, - Sessions: u.Sessions, - } - if u.ID != storage.AnonymousUserID { - item.CreatedAt = u.CreatedAt.UTC().Format(time.RFC3339) - } - if u.LastSeen != nil { - s := u.LastSeen.UTC().Format(time.RFC3339) - item.LastSeen = &s - } - return item -} - -func toUserItemPlain(u storage.User) userItem { - return userItem{ - ID: u.ID, - Name: u.Name, - Token: u.Token, - CreatedAt: u.CreatedAt.UTC().Format(time.RFC3339), - } -} - -// handleUsers dispatches GET (list) and POST (create) on /api/v1/users. -func (h *Handler) handleUsers(w http.ResponseWriter, r *http.Request) { - if h.userStore == nil { - jsonError(w, "not found", http.StatusNotFound) - return - } - switch r.Method { - case http.MethodGet: - h.listUsers(w, r) - case http.MethodPost: - h.createUser(w, r) - default: - jsonError(w, "method not allowed", http.StatusMethodNotAllowed) - } -} - -// handleUserByID dispatches GET (single user) and DELETE on /api/v1/users/{id}. -// The special id "__anonymous__" resolves to the synthetic anonymous row (GET) -// or purges all spans with no user_id (DELETE). -// For named users: ?mode=user_only (default) soft-deletes; ?mode=user_and_history anonymizes spans then hard-deletes. -func (h *Handler) handleUserByID(w http.ResponseWriter, r *http.Request, id string) { - if h.userStore == nil || id == "" { - jsonError(w, "not found", http.StatusNotFound) - return - } - if r.Method == http.MethodGet { - h.getUser(w, r, id) - return - } - if r.Method != http.MethodDelete { - jsonError(w, "method not allowed", http.StatusMethodNotAllowed) - return - } - - if id == storage.AnonymousUserID { - if err := h.userStore.DeleteAnonymousData(); err != nil { - jsonError(w, "delete failed", http.StatusInternalServerError) - return - } - w.WriteHeader(http.StatusNoContent) - return - } - - mode := r.URL.Query().Get("mode") - if mode == "" { - mode = "user_only" - } - - var err error - switch mode { - case "user_only": - err = h.userStore.SoftDeleteUser(id) - case "user_and_history": - err = h.userStore.DeleteUserWithHistory(id) - default: - jsonError(w, "invalid mode: must be user_only or user_and_history", http.StatusBadRequest) - return - } - - if errors.Is(err, storage.ErrNotFound) { - jsonError(w, "not found", http.StatusNotFound) - return - } - if err != nil { - jsonError(w, "delete failed", http.StatusInternalServerError) - return - } - w.WriteHeader(http.StatusNoContent) -} - -// handleUserRotateToken handles POST /api/v1/users/{id}/rotate-token. -func (h *Handler) handleUserRotateToken(w http.ResponseWriter, r *http.Request, id string) { - if h.userStore == nil || id == "" { - jsonError(w, "not found", http.StatusNotFound) - return - } - if r.Method != http.MethodPost { - jsonError(w, "method not allowed", http.StatusMethodNotAllowed) - return - } - u, err := h.userStore.RotateToken(id) - if err != nil { - jsonError(w, "rotate failed", http.StatusInternalServerError) - return - } - w.Header().Set("Content-Type", "application/json") - w.WriteHeader(http.StatusOK) - _ = json.NewEncoder(w).Encode(toUserItemPlain(u)) -} - -func (h *Handler) listUsers(w http.ResponseWriter, r *http.Request) { - rangeKey, since := parseRange(r) - sort, order := parseSortOrder(r, userSortKeys, "cost") - // limit defaults to 0 — unpaginated, which keeps the UserSearch typeahead whole. - page, limit := parsePaging(r) - - users, total, err := h.userStore.ListUsersPage(storage.ListUsersOptions{ - Since: since, - Query: r.URL.Query().Get("q"), - Sort: sort, - Order: order, - Page: page, - Limit: limit, - }) - if err != nil { - jsonError(w, "query failed", http.StatusInternalServerError) - return - } - items := make([]userItem, 0, len(users)) - for _, u := range users { - items = append(items, toUserItem(u)) - } - jsonOK(w, usersListResponse{ - Users: items, - Total: total, - Page: page, - Limit: limit, - Range: rangeKey, - Sort: sort, - Order: order, - }) -} - -// getUser handles GET /api/v1/users/{id}: a single user with range-scoped stats. -func (h *Handler) getUser(w http.ResponseWriter, r *http.Request, id string) { - _, since := parseRange(r) - u, err := h.userStore.GetUserWithStats(id, since) - if errors.Is(err, storage.ErrNotFound) { - jsonError(w, "not found", http.StatusNotFound) - return - } - if err != nil { - jsonError(w, "query failed", http.StatusInternalServerError) - return - } - jsonOK(w, toUserItem(u)) -} - -func (h *Handler) createUser(w http.ResponseWriter, r *http.Request) { - var req struct { - Name string `json:"name"` - } - if err := json.NewDecoder(r.Body).Decode(&req); err != nil || strings.TrimSpace(req.Name) == "" { - jsonError(w, "name required", http.StatusBadRequest) - return - } - u, err := h.userStore.CreateUser(strings.TrimSpace(req.Name)) - if err != nil { - jsonError(w, "create failed", http.StatusInternalServerError) - return - } - w.Header().Set("Content-Type", "application/json") - w.WriteHeader(http.StatusCreated) - _ = json.NewEncoder(w).Encode(toUserItemPlain(u)) -} diff --git a/internal/api/users_delete_test.go b/internal/api/users_delete_test.go deleted file mode 100644 index 0488b46..0000000 --- a/internal/api/users_delete_test.go +++ /dev/null @@ -1,155 +0,0 @@ -package api_test - -import ( - "net/http" - "net/http/httptest" - "testing" - "time" - - "github.com/Flopsstuff/cotel/internal/api" - "github.com/Flopsstuff/cotel/internal/api/auth" - "github.com/Flopsstuff/cotel/internal/storage" -) - -func newUserHandler(t *testing.T, db *storage.DB, ro *storage.ReadDB) http.Handler { - t.Helper() - return api.New(ro).SetUserStore(db) -} - -func deleteUser(t *testing.T, h http.Handler, id, mode string) int { - t.Helper() - path := "/api/v1/users/" + id - if mode != "" { - path += "?mode=" + mode - } - req := httptest.NewRequest(http.MethodDelete, path, nil) - w := httptest.NewRecorder() - h.ServeHTTP(w, req) - return w.Code -} - -func createAPIUser(t *testing.T, db *storage.DB, name string) storage.User { - t.Helper() - u, err := db.CreateUser(name) - if err != nil { - t.Fatalf("CreateUser(%q): %v", name, err) - } - return u -} - -func addAPISpan(t *testing.T, db *storage.DB, spanID, userID string) { - t.Helper() - now := time.Now() - if err := db.InsertSpan(storage.Span{ - TraceID: "tr1", SpanID: spanID, Name: "llm", SessionID: "s1", - UserID: userID, StartTime: now, EndTime: now.Add(time.Second), - }); err != nil { - t.Fatalf("InsertSpan: %v", err) - } -} - -func listAPIUserIDs(t *testing.T, h http.Handler) map[string]bool { - t.Helper() - _, body := getJSON(t, h, "/api/v1/users") - users, _ := body["users"].([]any) - ids := make(map[string]bool) - for _, u := range users { - m, _ := u.(map[string]any) - if id, ok := m["id"].(string); ok { - ids[id] = true - } - } - return ids -} - -func tokenStatus(db *storage.DB, token string) int { - h := auth.Middleware(db, http.HandlerFunc(func(w http.ResponseWriter, _ *http.Request) { - w.WriteHeader(http.StatusOK) - })) - req := httptest.NewRequest(http.MethodGet, "/", nil) - req.Header.Set("Authorization", "Bearer "+token) - w := httptest.NewRecorder() - h.ServeHTTP(w, req) - return w.Code -} - -// TestDeleteUser_ModeUserOnly verifies soft-delete: user gone from listing, -// token rejected, spans still reference the original name. -func TestDeleteUser_ModeUserOnly(t *testing.T) { - db, ro := openTestDB(t) - h := newUserHandler(t, db, ro) - u := createAPIUser(t, db, "alice") - addAPISpan(t, db, "sp-alice", "alice") - - if code := deleteUser(t, h, u.ID, "user_only"); code != http.StatusNoContent { - t.Fatalf("want 204, got %d", code) - } - if listAPIUserIDs(t, h)[u.ID] { - t.Error("alice must not appear in user list after soft-delete") - } - if tokenStatus(db, u.Token) != http.StatusUnauthorized { - t.Error("soft-deleted user's token must be rejected") - } -} - -// TestDeleteUser_ModeDefault confirms user_only is the default when mode is omitted. -func TestDeleteUser_ModeDefault(t *testing.T) { - db, ro := openTestDB(t) - h := newUserHandler(t, db, ro) - u := createAPIUser(t, db, "bob") - - if code := deleteUser(t, h, u.ID, ""); code != http.StatusNoContent { - t.Fatalf("want 204, got %d", code) - } - if listAPIUserIDs(t, h)[u.ID] { - t.Error("bob must not appear in list after default delete") - } -} - -// TestDeleteUser_ModeUserAndHistory verifies anonymization + hard-delete. -func TestDeleteUser_ModeUserAndHistory(t *testing.T) { - db, ro := openTestDB(t) - h := newUserHandler(t, db, ro) - u := createAPIUser(t, db, "carol") - addAPISpan(t, db, "sp-carol", "carol") - - if code := deleteUser(t, h, u.ID, "user_and_history"); code != http.StatusNoContent { - t.Fatalf("want 204, got %d", code) - } - if listAPIUserIDs(t, h)[u.ID] { - t.Error("carol must not appear in list after user_and_history delete") - } -} - -// TestDeleteUser_NotFound returns 404 for an unknown user ID. -func TestDeleteUser_NotFound(t *testing.T) { - db, ro := openTestDB(t) - h := newUserHandler(t, db, ro) - - if code := deleteUser(t, h, "no-such-id", "user_only"); code != http.StatusNotFound { - t.Errorf("want 404, got %d", code) - } -} - -// TestDeleteUser_AlreadyDeleted returns 404 on a second delete attempt. -func TestDeleteUser_AlreadyDeleted(t *testing.T) { - db, ro := openTestDB(t) - h := newUserHandler(t, db, ro) - u := createAPIUser(t, db, "dave") - - deleteUser(t, h, u.ID, "user_only") - if code := deleteUser(t, h, u.ID, "user_only"); code != http.StatusNotFound { - t.Errorf("want 404 on double delete, got %d", code) - } -} - -// TestDeleteUser_InvalidMode returns 400 for an unrecognized mode. -func TestDeleteUser_InvalidMode(t *testing.T) { - db, ro := openTestDB(t) - h := newUserHandler(t, db, ro) - u := createAPIUser(t, db, "eve") - - if code := deleteUser(t, h, u.ID, "nuke_everything"); code != http.StatusBadRequest { - t.Errorf("want 400, got %d", code) - } -} diff --git a/internal/api/users_list_test.go b/internal/api/users_list_test.go deleted file mode 100644 index 8c49ca9..0000000 --- a/internal/api/users_list_test.go +++ /dev/null @@ -1,138 +0,0 @@ -package api_test - -import ( - "testing" - "time" - - "github.com/Flopsstuff/cotel/internal/storage" -) - -func addCostSpan(t *testing.T, db *storage.DB, spanID, user, session string, cost float64) { - t.Helper() - now := time.Now() - insertSpan(t, db, storage.Span{ - TraceID: "tr", SpanID: spanID, Name: "llm", SessionID: session, - UserID: user, StartTime: now.Add(-1 * time.Hour), EndTime: now.Add(-1 * time.Hour).Add(time.Second), - CostUSD: ptr(cost), - }) -} - -// TestListUsers_EchoAndDefaults confirms the response carries the echo fields -// with their defaults when no query parameters are supplied. -func TestListUsers_EchoAndDefaults(t *testing.T) { - db, ro := openTestDB(t) - h := newUserHandler(t, db, ro) - createAPIUser(t, db, "alice") - - code, body := getJSON(t, h, "/api/v1/users") - if code != 200 { - t.Fatalf("want 200, got %d", code) - } - if body["range"] != "month" || body["sort"] != "cost" || body["order"] != "desc" { - t.Errorf("defaults: range/sort/order = %v/%v/%v", body["range"], body["sort"], body["order"]) - } - if body["limit"].(float64) != 0 { - t.Errorf("default limit: want 0, got %v", body["limit"]) - } - if body["total"].(float64) != 1 { - t.Errorf("total: want 1, got %v", body["total"]) - } -} - -// TestListUsers_GlobalSortAcrossPages proves paging sits on top of a global sort: -// page 1 (limit 2, cost desc) holds the two globally most expensive users. -func TestListUsers_GlobalSortAcrossPages(t *testing.T) { - db, ro := openTestDB(t) - h := newUserHandler(t, db, ro) - createAPIUser(t, db, "cheap") - createAPIUser(t, db, "mid") - createAPIUser(t, db, "pricey") - addCostSpan(t, db, "c1", "cheap", "sc", 10) - addCostSpan(t, db, "m1", "mid", "sm", 20) - addCostSpan(t, db, "p1", "pricey", "sp", 30) - - _, body := getJSON(t, h, "/api/v1/users?range=all&sort=cost&order=desc&page=1&limit=2") - users, _ := body["users"].([]any) - if len(users) != 2 { - t.Fatalf("page 1 limit 2: want 2 users, got %d", len(users)) - } - if body["total"].(float64) != 3 { - t.Errorf("total: want 3, got %v", body["total"]) - } - first := users[0].(map[string]any) - second := users[1].(map[string]any) - if first["name"] != "pricey" || second["name"] != "mid" { - t.Errorf("global sort broken: page 1 = %v, %v", first["name"], second["name"]) - } -} - -// TestListUsers_QueryFilter filters server-side on name. -func TestListUsers_QueryFilter(t *testing.T) { - db, ro := openTestDB(t) - h := newUserHandler(t, db, ro) - createAPIUser(t, db, "alice") - createAPIUser(t, db, "bob") - - _, body := getJSON(t, h, "/api/v1/users?q=ali") - users, _ := body["users"].([]any) - if len(users) != 1 || body["total"].(float64) != 1 { - t.Fatalf("q=ali: want 1 user/total, got %d users total=%v", len(users), body["total"]) - } - if users[0].(map[string]any)["name"] != "alice" { - t.Errorf("q=ali: want alice, got %v", users[0].(map[string]any)["name"]) - } -} - -// TestListUsers_InvalidParamsFallBack confirms bad values fall back to defaults -// rather than erroring (trust the boundary). -func TestListUsers_InvalidParamsFallBack(t *testing.T) { - db, ro := openTestDB(t) - h := newUserHandler(t, db, ro) - createAPIUser(t, db, "alice") - - code, body := getJSON(t, h, "/api/v1/users?range=decade&sort=whatever&order=sideways") - if code != 200 { - t.Fatalf("want 200 (no 400), got %d", code) - } - if body["range"] != "month" || body["sort"] != "cost" || body["order"] != "desc" { - t.Errorf("fallbacks: range/sort/order = %v/%v/%v", body["range"], body["sort"], body["order"]) - } -} - -// TestGetUser_ByID returns a single user and 404s for unknown ids. -func TestGetUser_ByID(t *testing.T) { - db, ro := openTestDB(t) - h := newUserHandler(t, db, ro) - u := createAPIUser(t, db, "alice") - addCostSpan(t, db, "a1", "alice", "sa", 12) - - code, body := getJSON(t, h, "/api/v1/users/"+u.ID+"?range=all") - if code != 200 { - t.Fatalf("want 200, got %d", code) - } - if body["name"] != "alice" || body["token"] != u.Token { - t.Errorf("identity mismatch: %v", body) - } - if body["cost"].(float64) != 12 { - t.Errorf("cost: want 12, got %v", body["cost"]) - } - - if code, _ := getJSON(t, h, "/api/v1/users/no-such-id"); code != 404 { - t.Errorf("unknown id: want 404, got %d", code) - } -} - -// TestGetUser_Anonymous resolves the synthetic anonymous row. -func TestGetUser_Anonymous(t *testing.T) { - db, ro := openTestDB(t) - h := newUserHandler(t, db, ro) - addCostSpan(t, db, "anon1", "", "sanon", 9) - - code, body := getJSON(t, h, "/api/v1/users/__anonymous__?range=all") - if code != 200 { - t.Fatalf("want 200, got %d", code) - } - if body["id"] != "__anonymous__" || body["cost"].(float64) != 9 { - t.Errorf("anon: got %v", body) - } -} diff --git a/internal/export/export.go b/internal/export/export.go index 844a38b..77a67cb 100644 --- a/internal/export/export.go +++ b/internal/export/export.go @@ -13,8 +13,11 @@ import ( "github.com/Flopsstuff/cotel/internal/storage" ) +// FormatVersion bumps only on a breaking change (ADR-0005). v2 renamed the +// attribution column user_id → actor_id in both tables (ADR-0027); the +// importer still reads v1 files. const ( - FormatVersion = 1 + FormatVersion = 2 CotelVersion = "0.1.0" ) @@ -35,11 +38,12 @@ type Manifest struct { Tables map[string]TableMeta `json:"tables"` } -// Column lists match ADR-0005 manifest v1 exactly. +// Column lists match ADR-0005 manifest v1 exactly, except that v2 names the +// attribution column actor_id where v1 had user_id, at the same offset. var SpansColumns = []string{ "span_id", "trace_id", "parent_span_id", "service_name", "name", "start_time", "end_time", "duration_ms", "session_id", "model", - "tool_name", "user_id", "status_code", "input_tokens", "output_tokens", + "tool_name", "actor_id", "status_code", "input_tokens", "output_tokens", "cache_read_tokens", "cache_write_tokens", "cost_usd", "attributes", "resource_attrs", "ingested_at", } @@ -48,7 +52,7 @@ var SpansColumns = []string{ // importer reads by header name, but a positional reader of an older column // keeps working only if earlier offsets never shift. var DailyUsageColumns = []string{ - "day", "session_id", "model", "tool_name", "user_id", + "day", "session_id", "model", "tool_name", "actor_id", "span_count", "total_input_tokens", "total_output_tokens", "total_cost_usd", "total_cache_read_tokens", "total_cache_write_tokens", } @@ -144,7 +148,7 @@ func writeSpansCSV(w io.Writer, spans []storage.Span) error { row[8] = s.SessionID row[9] = s.Model row[10] = s.ToolName - row[11] = s.UserID + row[11] = s.ActorID row[12] = strconv.FormatInt(int64(s.StatusCode), 10) row[13] = nullInt64Str(s.InputTokens) row[14] = nullInt64Str(s.OutputTokens) @@ -173,7 +177,7 @@ func writeDailyUsageCSV(w io.Writer, rows []storage.DailyUsageRow) error { row[1] = r.SessionID row[2] = r.Model row[3] = r.ToolName - row[4] = r.UserID + row[4] = r.ActorID row[5] = strconv.FormatInt(r.SpanCount, 10) row[6] = strconv.FormatInt(r.TotalInputTokens, 10) row[7] = strconv.FormatInt(r.TotalOutputTokens, 10) diff --git a/internal/export/export_test.go b/internal/export/export_test.go index 35e2767..6c6b2a5 100644 --- a/internal/export/export_test.go +++ b/internal/export/export_test.go @@ -158,8 +158,8 @@ func TestBuildZIP_NonEmpty(t *testing.T) { if err := json.NewDecoder(rc).Decode(&got); err != nil { t.Fatalf("decode manifest: %v", err) } - if got.FormatVersion != 1 { - t.Errorf("format_version: got %d, want 1", got.FormatVersion) + if got.FormatVersion != export.FormatVersion { + t.Errorf("format_version: got %d, want %d", got.FormatVersion, export.FormatVersion) } if got.Period != "day" { t.Errorf("period: got %q, want day", got.Period) diff --git a/internal/importpkg/import_test.go b/internal/importpkg/import_test.go index 895f44c..b5ba2a6 100644 --- a/internal/importpkg/import_test.go +++ b/internal/importpkg/import_test.go @@ -193,7 +193,7 @@ func TestReadZIP_UnknownColumnsDropped(t *testing.T) { sf, _ := zw.Create("spans.csv") // Header has an extra unknown column; known columns have zero/empty values. - _, _ = sf.Write([]byte("span_id,trace_id,unknown_col,name,start_time,end_time,parent_span_id,service_name,session_id,model,tool_name,user_id,status_code,input_tokens,output_tokens,cache_read_tokens,cache_write_tokens,cost_usd,attributes,resource_attrs,ingested_at,duration_ms\n")) + _, _ = sf.Write([]byte("span_id,trace_id,unknown_col,name,start_time,end_time,parent_span_id,service_name,session_id,model,tool_name,actor_id,status_code,input_tokens,output_tokens,cache_read_tokens,cache_write_tokens,cost_usd,attributes,resource_attrs,ingested_at,duration_ms\n")) _, _ = sf.Write([]byte("span-unk,trace-unk,IGNORED,op.test,2026-05-01T12:00:00Z,2026-05-01T12:00:01Z,,,,,,,,,,,,,,,,\n")) _ = zw.Close() @@ -401,3 +401,29 @@ func TestRoundTripWithStorage(t *testing.T) { t.Errorf("duplicate import daily: got %d inserted, want 0", n) } } + +// A v1 export, written before ADR-0027 renamed user_id to actor_id, must still +// import with its attribution intact. +func TestReadZIP_V1UserIDColumn(t *testing.T) { + var buf bytes.Buffer + zw := zip.NewWriter(&buf) + mf, _ := zw.Create("manifest.json") + _, _ = mf.Write([]byte(`{"format_version":1,"cotel_version":"0.1.0","export_at":"2026-05-01T00:00:00Z","period":"day","period_start":"2026-05-01T00:00:00Z","period_end":"2026-05-02T00:00:00Z","tables":{"spans":{"row_count":1},"daily_usage":{"row_count":1}}}`)) + sf, _ := zw.Create("spans.csv") + _, _ = sf.Write([]byte("span_id,trace_id,name,start_time,end_time,user_id\ns1,t1,n,2026-05-01T10:00:00Z,2026-05-01T10:00:01Z,Daedalus\n")) + df, _ := zw.Create("daily_usage.csv") + _, _ = df.Write([]byte("day,session_id,model,tool_name,user_id,span_count\n2026-05-01,sess,m,,Daedalus,3\n")) + _ = zw.Close() + + data := buf.Bytes() + _, spans, daily, err := importpkg.ReadZIP(bytes.NewReader(data), int64(len(data))) + if err != nil { + t.Fatalf("ReadZIP v1: %v", err) + } + if len(spans) != 1 || spans[0].ActorID != "Daedalus" { + t.Fatalf("v1 spans = %+v, want one span attributed to Daedalus", spans) + } + if len(daily) != 1 || daily[0].ActorID != "Daedalus" { + t.Fatalf("v1 daily_usage = %+v, want one row attributed to Daedalus", daily) + } +} diff --git a/internal/importpkg/reader.go b/internal/importpkg/reader.go index 78161d5..766ca7f 100644 --- a/internal/importpkg/reader.go +++ b/internal/importpkg/reader.go @@ -18,7 +18,7 @@ import ( const ( maxUncompressedBytes = 100 * 1024 * 1024 // 100 MB maxEntries = 5 - currentMaxVersion = export.FormatVersion // 1 + currentMaxVersion = export.FormatVersion // 2 ) // ErrTooLarge is returned when the ZIP content exceeds the 100 MB uncompressed limit. @@ -146,7 +146,7 @@ func parseSpansCSV(r io.Reader) ([]storage.Span, error) { s.SessionID = col(row, idx, "session_id") s.Model = col(row, idx, "model") s.ToolName = col(row, idx, "tool_name") - s.UserID = col(row, idx, "user_id") + s.ActorID = actorCol(row, idx) if v := col(row, idx, "status_code"); v != "" { n, _ := strconv.ParseInt(v, 10, 32) @@ -199,7 +199,7 @@ func parseDailyUsageCSV(r io.Reader) ([]storage.DailyUsageRow, error) { du.SessionID = col(row, idx, "session_id") du.Model = col(row, idx, "model") du.ToolName = col(row, idx, "tool_name") - du.UserID = col(row, idx, "user_id") + du.ActorID = actorCol(row, idx) if v := col(row, idx, "span_count"); v != "" { du.SpanCount, _ = strconv.ParseInt(v, 10, 64) @@ -224,6 +224,16 @@ func parseDailyUsageCSV(r io.Reader) ([]storage.DailyUsageRow, error) { // col returns the value at the named column, or "" if the column is absent or missing from the row. // Unknown columns are silently dropped; missing known columns default to "". +// actorCol reads the attribution column: actor_id in format v2, user_id in v1 +// files exported before the rename (ADR-0027). Reading by header name keeps +// this independent of the manifest's declared version. +func actorCol(row []string, idx map[string]int) string { + if _, ok := idx["actor_id"]; ok { + return col(row, idx, "actor_id") + } + return col(row, idx, "user_id") +} + func col(row []string, idx map[string]int, name string) string { i, ok := idx[name] if !ok || i >= len(row) { diff --git a/internal/ingest/context.go b/internal/ingest/context.go index 21e28e5..eb2e8d8 100644 --- a/internal/ingest/context.go +++ b/internal/ingest/context.go @@ -2,18 +2,18 @@ package ingest import "context" -// ctxKeyUserID is the context key for the authenticated user's display name. -type ctxKeyUserID struct{} +// ctxKeyActorID is the context key for the authenticated actor's display name. +type ctxKeyActorID struct{} -// WithUserName returns a new context carrying the authenticated user's name. -// Used by auth middleware to propagate the user identity to the ingest handler. -func WithUserName(ctx context.Context, name string) context.Context { - return context.WithValue(ctx, ctxKeyUserID{}, name) +// WithActorName returns a new context carrying the authenticated actor's name. +// Used by auth middleware to propagate the actor identity to the ingest handler. +func WithActorName(ctx context.Context, name string) context.Context { + return context.WithValue(ctx, ctxKeyActorID{}, name) } -// UserNameFromContext extracts the user name injected by auth middleware, or "". -func UserNameFromContext(ctx context.Context) string { - if name, ok := ctx.Value(ctxKeyUserID{}).(string); ok { +// ActorNameFromContext extracts the actor name injected by auth middleware, or "". +func ActorNameFromContext(ctx context.Context) string { + if name, ok := ctx.Value(ctxKeyActorID{}).(string); ok { return name } return "" diff --git a/internal/ingest/handler.go b/internal/ingest/handler.go index e4cb439..59f60e6 100644 --- a/internal/ingest/handler.go +++ b/internal/ingest/handler.go @@ -83,8 +83,8 @@ func (h *Handler) ServeHTTP(w http.ResponseWriter, r *http.Request) { } } - // User identity comes from the auth middleware context, not OTEL attributes. - userName := UserNameFromContext(r.Context()) + // Actor identity comes from the auth middleware context, not OTEL attributes. + actorName := ActorNameFromContext(r.Context()) for _, rs := range req.ResourceSpans { svcName := attrStr(rs.GetResource().GetAttributes(), "service.name") @@ -93,7 +93,7 @@ func (h *Handler) ServeHTTP(w http.ResponseWriter, r *http.Request) { for _, ss := range rs.ScopeSpans { for _, sp := range ss.Spans { - h.queue <- decodeSpan(sp, svcName, resAttrs, string(resJSON), userName) + h.queue <- decodeSpan(sp, svcName, resAttrs, string(resJSON), actorName) } } } @@ -107,7 +107,7 @@ func (h *Handler) ServeHTTP(w http.ResponseWriter, r *http.Request) { // identifier, checked in order of preference. var sessionIDKeys = []string{"session.id", "claude.session.id", "cc.session.id"} -func decodeSpan(sp *tracepb.Span, svcName string, resAttrs map[string]any, resJSON string, userName string) storage.Span { +func decodeSpan(sp *tracepb.Span, svcName string, resAttrs map[string]any, resJSON string, actorName string) storage.Span { attrs := attrsToMap(sp.Attributes) attrsJSON, _ := json.Marshal(attrs) @@ -141,8 +141,8 @@ func decodeSpan(sp *tracepb.Span, svcName string, resAttrs map[string]any, resJS } } - // User identity is injected by auth middleware; empty = anonymous (stored as NULL). - s.UserID = userName + // Actor identity is injected by auth middleware; empty = anonymous (stored as NULL). + s.ActorID = actorName if v, ok := attrs["model"]; ok { s.Model = fmt.Sprintf("%v", v) diff --git a/internal/storage/users.go b/internal/storage/actors.go similarity index 65% rename from internal/storage/users.go rename to internal/storage/actors.go index 1e305a5..f0f97c0 100644 --- a/internal/storage/users.go +++ b/internal/storage/actors.go @@ -10,23 +10,23 @@ import ( "time" ) -// AnonymousUserID is the synthetic ID used to represent spans with no user_id (unauthenticated ingest). -const AnonymousUserID = "__anonymous__" +// AnonymousActorID is the synthetic ID used to represent spans with no actor_id (unauthenticated ingest). +const AnonymousActorID = "__anonymous__" -// ErrNotFound is returned when the target user does not exist or is already soft-deleted. +// ErrNotFound is returned when the target actor does not exist or is already soft-deleted. var ErrNotFound = errors.New("not found") -// User is a named principal whose token authenticates OTLP ingest requests. -type User struct { +// Actor is a named principal whose token authenticates OTLP ingest requests. +type Actor struct { ID string Name string Token string CreatedAt time.Time } -// UserWithStats adds span-derived metrics to a User. -type UserWithStats struct { - User +// ActorWithStats adds span-derived metrics to an Actor. +type ActorWithStats struct { + Actor TotalCostUSD float64 Sessions int64 LastSeen *time.Time @@ -49,29 +49,29 @@ func generateID() string { b[0:4], b[4:6], b[6:8], b[8:10], b[10:16]) } -// CreateUser inserts a new user with a generated token and returns the full record. -func (db *DB) CreateUser(name string) (User, error) { +// CreateActor inserts a new actor with a generated token and returns the full record. +func (db *DB) CreateActor(name string) (Actor, error) { token, err := generateToken() if err != nil { - return User{}, err + return Actor{}, err } id := generateID() _, err = db.rw.Exec( - `INSERT INTO users (id, name, token) VALUES (?, ?, ?)`, + `INSERT INTO actors (id, name, token) VALUES (?, ?, ?)`, id, name, token, ) if err != nil { - return User{}, err + return Actor{}, err } - var u User + var u Actor err = db.rw.QueryRow( - `SELECT id, name, token, created_at FROM users WHERE id = ?`, id, + `SELECT id, name, token, created_at FROM actors WHERE id = ?`, id, ).Scan(&u.ID, &u.Name, &u.Token, &u.CreatedAt) return u, err } -// ListUsersOptions parameterises ListUsersPage. -type ListUsersOptions struct { +// ListActorsOptions parameterises ListActorsPage. +type ListActorsOptions struct { Since *time.Time // lower bound for range-scoped cost/sessions; nil = all-time Query string // case-insensitive substring match on name; "" = no filter Sort string // name|created_at|last_seen|cost|sessions (invalid → cost) @@ -80,9 +80,9 @@ type ListUsersOptions struct { Limit int // rows per page; 0 = unpaginated; clamped to 500 } -// userSortExprs whitelists sort keys to their SELECT-list column, so the caller +// actorSortExprs whitelists sort keys to their SELECT-list column, so the caller // can never inject an ORDER BY expression. -var userSortExprs = map[string]string{ +var actorSortExprs = map[string]string{ "name": "name", "created_at": "created_at", "last_seen": "last_seen", @@ -107,21 +107,21 @@ raw_floor AS ( SELECT MIN(start_time) AS ts FROM spans ), usage AS ( - SELECT user_id, session_id, cost_usd AS cost + SELECT actor_id, session_id, cost_usd AS cost FROM spans WHERE TRUE%s UNION ALL - SELECT du.user_id, du.session_id, du.total_cost_usd AS cost + SELECT du.actor_id, du.session_id, du.total_cost_usd AS cost FROM daily_usage du CROSS JOIN raw_floor rf WHERE (rf.ts IS NULL OR du.day < CAST(CAST(rf.ts AS TIMESTAMP) AS DATE))%s )` -// ListUsersPage returns a sorted, optionally paged slice of users with +// ListActorsPage returns a sorted, optionally paged slice of actors with // range-scoped cost/session stats, plus the total number of matching rows // (counted before paging). The synthetic __anonymous__ row is produced inside -// the SQL so it sorts, filters, and paginates inline with real users. -func (db *DB) ListUsersPage(opts ListUsersOptions) ([]UserWithStats, int, error) { - sortExpr, ok := userSortExprs[opts.Sort] +// the SQL so it sorts, filters, and paginates inline with real actors. +func (db *DB) ListActorsPage(opts ListActorsOptions) ([]ActorWithStats, int, error) { + sortExpr, ok := actorSortExprs[opts.Sort] if !ok { sortExpr = "cost" } @@ -162,11 +162,11 @@ func (db *DB) ListUsersPage(opts ListUsersOptions) ([]UserWithStats, int, error) args = append(args, limit, (page-1)*limit) } - cte := fmt.Sprintf(usersPageCTE, rawSince, aggSince, AnonymousUserID) + cte := fmt.Sprintf(actorsPageCTE, rawSince, aggSince, AnonymousActorID) query := cte + fmt.Sprintf(` SELECT id, name, token, created_at, cost, sessions, last_seen, COUNT(*) OVER () AS total -FROM all_users +FROM all_actors WHERE TRUE%s ORDER BY %s %s NULLS LAST, name ASC%s `, qFilter, sortExpr, order, limitClause) @@ -177,10 +177,10 @@ ORDER BY %s %s NULLS LAST, name ASC%s } defer rows.Close() - var out []UserWithStats + var out []ActorWithStats var total int for rows.Next() { - var u UserWithStats + var u ActorWithStats var token sql.NullString var createdAt, lastSeen sql.NullTime if err := rows.Scan( @@ -208,7 +208,7 @@ ORDER BY %s %s NULLS LAST, name ASC%s // COUNT(*) OVER () rides on the result rows, so a page past the end yields no // row to read it from. Re-count so total stays the match count regardless of page. if len(out) == 0 { - countQuery := cte + fmt.Sprintf("\nSELECT COUNT(*) FROM all_users WHERE TRUE%s\n", qFilter) + countQuery := cte + fmt.Sprintf("\nSELECT COUNT(*) FROM all_actors WHERE TRUE%s\n", qFilter) if err := db.rw.QueryRow(countQuery, filterArgs...).Scan(&total); err != nil { return nil, 0, err } @@ -216,26 +216,26 @@ ORDER BY %s %s NULLS LAST, name ASC%s return out, total, nil } -const usersPageCTE = ` +const actorsPageCTE = ` WITH ` + rangeUsageCTE + `, usage_agg AS ( - SELECT user_id, + SELECT actor_id, COALESCE(SUM(cost), 0) AS cost, COUNT(DISTINCT session_id) AS sessions FROM usage - GROUP BY user_id + GROUP BY actor_id ), seen AS ( - SELECT user_id, MAX(end_time) AS last_seen FROM spans GROUP BY user_id + SELECT actor_id, MAX(end_time) AS last_seen FROM spans GROUP BY actor_id ), named AS ( SELECT u.id, u.name, u.token, u.created_at, COALESCE(ua.cost, 0) AS cost, COALESCE(ua.sessions, 0) AS sessions, s.last_seen AS last_seen - FROM users u - LEFT JOIN usage_agg ua ON ua.user_id = u.name - LEFT JOIN seen s ON s.user_id = u.name + FROM actors u + LEFT JOIN usage_agg ua ON ua.actor_id = u.name + LEFT JOIN seen s ON s.actor_id = u.name WHERE u.deleted_at IS NULL ), anon AS ( @@ -244,51 +244,51 @@ anon AS ( 'Anonymous' AS name, CAST(NULL AS VARCHAR) AS token, CAST(NULL AS TIMESTAMPTZ) AS created_at, - COALESCE((SELECT cost FROM usage_agg WHERE user_id IS NULL), 0) AS cost, - COALESCE((SELECT sessions FROM usage_agg WHERE user_id IS NULL), 0) AS sessions, - (SELECT last_seen FROM seen WHERE user_id IS NULL) AS last_seen + COALESCE((SELECT cost FROM usage_agg WHERE actor_id IS NULL), 0) AS cost, + COALESCE((SELECT sessions FROM usage_agg WHERE actor_id IS NULL), 0) AS sessions, + (SELECT last_seen FROM seen WHERE actor_id IS NULL) AS last_seen FROM (SELECT 1) d - WHERE EXISTS (SELECT 1 FROM spans WHERE user_id IS NULL) - OR EXISTS (SELECT 1 FROM daily_usage WHERE user_id IS NULL) + WHERE EXISTS (SELECT 1 FROM spans WHERE actor_id IS NULL) + OR EXISTS (SELECT 1 FROM daily_usage WHERE actor_id IS NULL) ), -all_users AS ( +all_actors AS ( SELECT * FROM named UNION ALL SELECT * FROM anon )` -// GetUserWithStats returns a single user with range-scoped cost/session stats -// (since = nil means all-time). id may be the __anonymous__ pseudo-user, which +// GetActorWithStats returns a single actor with range-scoped cost/session stats +// (since = nil means all-time). id may be the __anonymous__ pseudo-actor, which // always resolves to the synthetic row. Returns ErrNotFound for an unknown or -// soft-deleted named user. -func (db *DB) GetUserWithStats(id string, since *time.Time) (UserWithStats, error) { - if id == AnonymousUserID { +// soft-deleted named actor. +func (db *DB) GetActorWithStats(id string, since *time.Time) (ActorWithStats, error) { + if id == AnonymousActorID { cost, sessions, lastSeen, err := db.principalStats(true, "", since) if err != nil { - return UserWithStats{}, err + return ActorWithStats{}, err } - return UserWithStats{ - User: User{ID: AnonymousUserID, Name: "Anonymous"}, + return ActorWithStats{ + Actor: Actor{ID: AnonymousActorID, Name: "Anonymous"}, TotalCostUSD: cost, Sessions: sessions, LastSeen: lastSeen, }, nil } - var u UserWithStats + var u ActorWithStats err := db.rw.QueryRow( - `SELECT id, name, token, created_at FROM users WHERE id = ? AND deleted_at IS NULL`, id, + `SELECT id, name, token, created_at FROM actors WHERE id = ? AND deleted_at IS NULL`, id, ).Scan(&u.ID, &u.Name, &u.Token, &u.CreatedAt) if err != nil { if errors.Is(err, sql.ErrNoRows) { - return UserWithStats{}, ErrNotFound + return ActorWithStats{}, ErrNotFound } - return UserWithStats{}, err + return ActorWithStats{}, err } cost, sessions, lastSeen, err := db.principalStats(false, u.Name, since) if err != nil { - return UserWithStats{}, err + return ActorWithStats{}, err } u.TotalCostUSD = cost u.Sessions = sessions @@ -297,13 +297,13 @@ func (db *DB) GetUserWithStats(id string, since *time.Time) (UserWithStats, erro } // principalStats computes range-scoped cost/sessions plus all-time last_seen for -// one principal: the anonymous bucket (isAnon) or a named user. It reuses the +// one principal: the anonymous bucket (isAnon) or a named actor. It reuses the // same span/daily_usage union as the list query so a single row's numbers match // the list exactly. func (db *DB) principalStats(isAnon bool, name string, since *time.Time) (float64, int64, *time.Time, error) { - rawPrincipal, aggPrincipal := "user_id IS NULL", "du.user_id IS NULL" + rawPrincipal, aggPrincipal := "actor_id IS NULL", "du.actor_id IS NULL" if !isAnon { - rawPrincipal, aggPrincipal = "user_id = ?", "du.user_id = ?" + rawPrincipal, aggPrincipal = "actor_id = ?", "du.actor_id = ?" } var rawSince, aggSince string @@ -360,11 +360,11 @@ SELECT COALESCE(SUM(cost), 0), COUNT(DISTINCT session_id) FROM usage return cost, sessions, lastSeen, nil } -// GetUserByToken looks up a non-deleted user by their plaintext token. -func (db *DB) GetUserByToken(token string) (*User, error) { - var u User +// GetActorByToken looks up a non-deleted actor by their plaintext token. +func (db *DB) GetActorByToken(token string) (*Actor, error) { + var u Actor err := db.rw.QueryRow( - `SELECT id, name, token, created_at FROM users WHERE token = ? AND deleted_at IS NULL`, token, + `SELECT id, name, token, created_at FROM actors WHERE token = ? AND deleted_at IS NULL`, token, ).Scan(&u.ID, &u.Name, &u.Token, &u.CreatedAt) if err != nil { return nil, err @@ -372,36 +372,36 @@ func (db *DB) GetUserByToken(token string) (*User, error) { return &u, nil } -// RotateToken replaces a user's token with a freshly generated one. -func (db *DB) RotateToken(userID string) (User, error) { +// RotateToken replaces an actor's token with a freshly generated one. +func (db *DB) RotateToken(actorID string) (Actor, error) { token, err := generateToken() if err != nil { - return User{}, err + return Actor{}, err } _, err = db.rw.Exec( - `UPDATE users SET token = ? WHERE id = ?`, token, userID, + `UPDATE actors SET token = ? WHERE id = ?`, token, actorID, ) if err != nil { - return User{}, err + return Actor{}, err } - var u User + var u Actor err = db.rw.QueryRow( - `SELECT id, name, token, created_at FROM users WHERE id = ?`, userID, + `SELECT id, name, token, created_at FROM actors WHERE id = ?`, actorID, ).Scan(&u.ID, &u.Name, &u.Token, &u.CreatedAt) return u, err } -// SoftDeleteUser stamps deleted_at, making the user invisible and their token -// invalid (GetUserByToken filters deleted_at IS NULL). Returns ErrNotFound if -// the user does not exist or is already soft-deleted. +// SoftDeleteActor stamps deleted_at, making the actor invisible and their token +// invalid (GetActorByToken filters deleted_at IS NULL). Returns ErrNotFound if +// the actor does not exist or is already soft-deleted. // // Note: DuckDB v1.8.x has a bug where UPDATE on a UNIQUE-constrained column // raises a false "duplicate key" error. We therefore do NOT update the token -// column — the deleted_at filter is sufficient to reject auth for deleted users. -func (db *DB) SoftDeleteUser(userID string) error { +// column — the deleted_at filter is sufficient to reject auth for deleted actors. +func (db *DB) SoftDeleteActor(actorID string) error { result, err := db.rw.Exec( - `UPDATE users SET deleted_at = CURRENT_TIMESTAMP WHERE id = ? AND deleted_at IS NULL`, - userID, + `UPDATE actors SET deleted_at = CURRENT_TIMESTAMP WHERE id = ? AND deleted_at IS NULL`, + actorID, ) if err != nil { return err @@ -413,15 +413,15 @@ func (db *DB) SoftDeleteUser(userID string) error { return nil } -// DeleteUserWithHistory hard-deletes all spans and daily_usage rows belonging to the -// user, then hard-deletes the user row. Returns ErrNotFound if the user does not +// DeleteActorWithHistory hard-deletes all spans and daily_usage rows belonging to the +// actor, then hard-deletes the actor row. Returns ErrNotFound if the actor does not // exist or is already soft-deleted. // // Note: DuckDB v1.x fails with "Vector::Reference used on vector of different type" -// when executing UPDATE on spans.user_id (a column added via ADD COLUMN on a table +// when executing UPDATE on spans.actor_id (a column added via ADD COLUMN on a table // with GENERATED ALWAYS columns). We therefore DELETE span rows rather than -// anonymising them. All history is removed together with the user. -func (db *DB) DeleteUserWithHistory(userID string) error { +// anonymising them. All history is removed together with the actor. +func (db *DB) DeleteActorWithHistory(actorID string) error { tx, err := db.rw.Begin() if err != nil { return err @@ -434,7 +434,7 @@ func (db *DB) DeleteUserWithHistory(userID string) error { var name string if err = tx.QueryRow( - `SELECT name FROM users WHERE id = ? AND deleted_at IS NULL`, userID, + `SELECT name FROM actors WHERE id = ? AND deleted_at IS NULL`, actorID, ).Scan(&name); err != nil { if errors.Is(err, sql.ErrNoRows) { err = ErrNotFound @@ -442,22 +442,22 @@ func (db *DB) DeleteUserWithHistory(userID string) error { return err } - if _, err = tx.Exec(`DELETE FROM spans WHERE user_id = ?`, name); err != nil { + if _, err = tx.Exec(`DELETE FROM spans WHERE actor_id = ?`, name); err != nil { return err } - if _, err = tx.Exec(`DELETE FROM daily_usage WHERE user_id = ?`, name); err != nil { + if _, err = tx.Exec(`DELETE FROM daily_usage WHERE actor_id = ?`, name); err != nil { return err } - if _, err = tx.Exec(`DELETE FROM users WHERE id = ?`, userID); err != nil { + if _, err = tx.Exec(`DELETE FROM actors WHERE id = ?`, actorID); err != nil { return err } return tx.Commit() } -// DeleteAnonymousData hard-deletes all spans and daily_usage rows that have no user_id (anonymous ingest). +// DeleteAnonymousData hard-deletes all spans and daily_usage rows that have no actor_id (anonymous ingest). func (db *DB) DeleteAnonymousData() error { tx, err := db.rw.Begin() if err != nil { @@ -469,10 +469,10 @@ func (db *DB) DeleteAnonymousData() error { } }() - if _, err = tx.Exec(`DELETE FROM spans WHERE user_id IS NULL`); err != nil { + if _, err = tx.Exec(`DELETE FROM spans WHERE actor_id IS NULL`); err != nil { return err } - if _, err = tx.Exec(`DELETE FROM daily_usage WHERE user_id IS NULL`); err != nil { + if _, err = tx.Exec(`DELETE FROM daily_usage WHERE actor_id IS NULL`); err != nil { return err } return tx.Commit() diff --git a/internal/storage/actors_migration_test.go b/internal/storage/actors_migration_test.go new file mode 100644 index 0000000..25ca031 --- /dev/null +++ b/internal/storage/actors_migration_test.go @@ -0,0 +1,175 @@ +package storage + +import ( + "database/sql" + "os" + "path/filepath" + "sort" + "testing" + "time" +) + +// seedV10 builds a database exactly as schema version 10 left it - testdata +// holds a frozen copy of that release's schema.sql - and fills it with an +// actor, attributed and anonymous spans, and a rolled-up day. +func seedV10(t *testing.T, path string) { + t.Helper() + ddl, err := os.ReadFile(filepath.Join("testdata", "schema_v10.sql")) + if err != nil { + t.Fatalf("read v10 schema: %v", err) + } + raw, err := sql.Open("duckdb", path) + if err != nil { + t.Fatalf("raw open: %v", err) + } + defer raw.Close() + stmts := []string{ + string(ddl), + `INSERT INTO users (id, name, token) VALUES ('u1', 'Daedalus', 'cotel_` + repeatHex("a") + `')`, + `INSERT INTO users (id, name, token, deleted_at) VALUES ('u2', 'Gone', 'cotel_` + repeatHex("b") + `', now())`, + `INSERT INTO spans (trace_id, span_id, name, start_time, end_time, session_id, user_id, cost_usd) + VALUES ('t', 's1', 'n', now(), now(), 'sess1', 'Daedalus', 1.5), + ('t', 's2', 'n', now(), now(), 'sess2', NULL, 0.5)`, + `INSERT INTO daily_usage (day, session_id, model, tool_name, user_id, span_count, total_cost_usd) + VALUES (DATE '2026-01-01', 'old', 'm', '', 'Daedalus', 3, 2.0)`, + } + for _, s := range stmts { + if _, err := raw.Exec(s); err != nil { + t.Fatalf("seed v10: %v\n%s", err, s) + } + } +} + +func repeatHex(c string) string { + out := "" + for i := 0; i < 64; i++ { + out += c + } + return out +} + +func TestMigrateUsersToActors_FromV10(t *testing.T) { + path := filepath.Join(t.TempDir(), "v10.duckdb") + seedV10(t, path) + + db, err := Open(path) + if err != nil { + t.Fatalf("open v10 database: %v", err) + } + defer db.Close() + + if v, _ := appliedSchemaVersion(db.rw); v != 11 { + t.Fatalf("schema version = %d, want 11", v) + } + + // The old names are gone, so no query can silently read an empty twin. + tx, err := db.rw.Begin() + if err != nil { + t.Fatal(err) + } + for _, c := range []struct{ table, col string }{{"spans", "user_id"}, {"daily_usage", "user_id"}} { + if ok, err := columnExists(tx, c.table, c.col); err != nil || ok { + t.Errorf("%s.%s still exists (err=%v)", c.table, c.col, err) + } + } + if ok, err := tableExists(tx, "users"); err != nil || ok { + t.Errorf("users table still exists (err=%v)", err) + } + _ = tx.Rollback() + + // Tokens survive the table rename, and soft-deletes keep rejecting. + a, err := db.GetActorByToken("cotel_" + repeatHex("a")) + if err != nil || a.Name != "Daedalus" { + t.Fatalf("token lookup after migration = %+v, %v", a, err) + } + if _, err := db.GetActorByToken("cotel_" + repeatHex("b")); err == nil { + t.Fatal("soft-deleted actor's token accepted after migration") + } + + // Attribution survives the column renames, raw and rolled up alike. + got, err := db.GetActorWithStats("u1", nil) + if err != nil { + t.Fatalf("GetActorWithStats: %v", err) + } + if got.TotalCostUSD != 3.5 || got.Sessions != 2 { + t.Fatalf("Daedalus stats = cost %v sessions %d, want 3.5 / 2", got.TotalCostUSD, got.Sessions) + } + var anon int + if err := db.rw.QueryRow(`SELECT count(*) FROM spans WHERE actor_id IS NULL`).Scan(&anon); err != nil || anon != 1 { + t.Fatalf("anonymous spans = %d (err=%v), want 1", anon, err) + } + + // The secondary indexes come back under the new name. + rows, err := db.rw.Query(`SELECT index_name FROM duckdb_indexes() WHERE table_name = 'spans'`) + if err != nil { + t.Fatal(err) + } + var idx []string + for rows.Next() { + var n string + _ = rows.Scan(&n) + idx = append(idx, n) + } + rows.Close() + sort.Strings(idx) + want := []string{"idx_spans_actor_id", "idx_spans_name", "idx_spans_session_id", "idx_spans_start_time"} + if len(idx) != len(want) { + t.Fatalf("spans indexes = %v, want %v", idx, want) + } + for i := range want { + if idx[i] != want[i] { + t.Fatalf("spans indexes = %v, want %v", idx, want) + } + } + + // New ingest lands in the renamed column. + if err := db.InsertSpan(Span{TraceID: "t", SpanID: "s3", Name: "n", StartTime: time.Now(), EndTime: time.Now(), ActorID: "Daedalus"}); err != nil { + t.Fatalf("insert after migration: %v", err) + } +} + +// A second open of a migrated file, and a reopen after the schema hash changes, +// must both be no-ops for the rename. +func TestMigrateUsersToActors_Idempotent(t *testing.T) { + path := filepath.Join(t.TempDir(), "v10.duckdb") + seedV10(t, path) + for i := 0; i < 2; i++ { + db, err := Open(path) + if err != nil { + t.Fatalf("open #%d: %v", i+1, err) + } + if i == 0 { + // Force the next open to re-apply schema.sql and re-run the rename check. + if _, err := db.rw.Exec(`UPDATE settings SET value = 'stale' WHERE key = ?`, schemaSHAKey); err != nil { + t.Fatal(err) + } + } + db.Close() + } + db, err := Open(path) + if err != nil { + t.Fatalf("final open: %v", err) + } + defer db.Close() + var n int + if err := db.rw.QueryRow(`SELECT count(*) FROM actors`).Scan(&n); err != nil || n != 2 { + t.Fatalf("actors rows = %d (err=%v), want 2", n, err) + } +} + +func TestMigrateUsersToActors_RefusesBothTables(t *testing.T) { + path := filepath.Join(t.TempDir(), "both.duckdb") + seedV10(t, path) + raw, err := sql.Open("duckdb", path) + if err != nil { + t.Fatal(err) + } + if _, err := raw.Exec(`CREATE TABLE actors (id VARCHAR)`); err != nil { + t.Fatal(err) + } + raw.Close() + if db, err := Open(path); err == nil { + db.Close() + t.Fatal("open succeeded with both users and actors present") + } +} diff --git a/internal/storage/users_page_test.go b/internal/storage/actors_page_test.go similarity index 50% rename from internal/storage/users_page_test.go rename to internal/storage/actors_page_test.go index 35e7f06..467a93f 100644 --- a/internal/storage/users_page_test.go +++ b/internal/storage/actors_page_test.go @@ -9,27 +9,27 @@ import ( func costPtr(v float64) *float64 { return &v } -// addRangeSpan inserts one raw span. An empty user stores user_id NULL (anonymous). -func addRangeSpan(t *testing.T, db *DB, spanID, user, session string, start time.Time, cost float64) { +// addRangeSpan inserts one raw span. An empty actor stores actor_id NULL (anonymous). +func addRangeSpan(t *testing.T, db *DB, spanID, actor, session string, start time.Time, cost float64) { t.Helper() if err := db.InsertSpan(Span{ TraceID: "tr", SpanID: spanID, Name: "llm", SessionID: session, - UserID: user, StartTime: start, EndTime: start.Add(time.Second), CostUSD: costPtr(cost), + ActorID: actor, StartTime: start, EndTime: start.Add(time.Second), CostUSD: costPtr(cost), }); err != nil { t.Fatalf("InsertSpan(%s): %v", spanID, err) } } -// addDailyUsageRow inserts one rolled-up aggregate row. An empty user stores -// user_id NULL (anonymous). -func addDailyUsageRow(t *testing.T, db *DB, day time.Time, user, session, model string, cost float64) { +// addDailyUsageRow inserts one rolled-up aggregate row. An empty actor stores +// actor_id NULL (anonymous). +func addDailyUsageRow(t *testing.T, db *DB, day time.Time, actor, session, model string, cost float64) { t.Helper() var uid any - if user != "" { - uid = user + if actor != "" { + uid = actor } _, err := db.rw.Exec(` - INSERT INTO daily_usage (day, session_id, model, tool_name, user_id, span_count, total_cost_usd) + INSERT INTO daily_usage (day, session_id, model, tool_name, actor_id, span_count, total_cost_usd) VALUES (CAST(? AS DATE), ?, ?, 'tool', ?, 1, ?) `, day.Format("2006-01-02"), session, model, uid, cost) if err != nil { @@ -37,35 +37,35 @@ func addDailyUsageRow(t *testing.T, db *DB, day time.Time, user, session, model } } -func setUserCreatedAt(t *testing.T, db *DB, id string, ts time.Time) { +func setActorCreatedAt(t *testing.T, db *DB, id string, ts time.Time) { t.Helper() - if _, err := db.rw.Exec(`UPDATE users SET created_at = ? WHERE id = ?`, ts, id); err != nil { + if _, err := db.rw.Exec(`UPDATE actors SET created_at = ? WHERE id = ?`, ts, id); err != nil { t.Fatalf("set created_at: %v", err) } } -func userByName(users []UserWithStats, name string) (UserWithStats, bool) { - for _, u := range users { +func actorByName(actors []ActorWithStats, name string) (ActorWithStats, bool) { + for _, u := range actors { if u.Name == name { return u, true } } - return UserWithStats{}, false + return ActorWithStats{}, false } -func userNames(users []UserWithStats) []string { - out := make([]string, len(users)) - for i, u := range users { +func actorNames(actors []ActorWithStats) []string { + out := make([]string, len(actors)) + for i, u := range actors { out[i] = u.Name } return out } -// TestListUsersPage_RangeScopesStats verifies that cost/sessions honour the +// TestListActorsPage_RangeScopesStats verifies that cost/sessions honour the // range lower bound while created_at/last_seen stay all-time. -func TestListUsersPage_RangeScopesStats(t *testing.T) { - db := openTestUserDB(t) - mustCreateTestUser(t, db, "alice") +func TestListActorsPage_RangeScopesStats(t *testing.T) { + db := openTestActorDB(t) + mustCreateTestActor(t, db, "alice") now := time.Now() addRangeSpan(t, db, "s-now", "alice", "sess-now", now.Add(-1*time.Hour), 1) @@ -86,11 +86,11 @@ func TestListUsersPage_RangeScopesStats(t *testing.T) { } for _, tc := range cases { t.Run(tc.name, func(t *testing.T) { - users, _, err := db.ListUsersPage(ListUsersOptions{Since: tc.since}) + actors, _, err := db.ListActorsPage(ListActorsOptions{Since: tc.since}) if err != nil { - t.Fatalf("ListUsersPage: %v", err) + t.Fatalf("ListActorsPage: %v", err) } - u, ok := userByName(users, "alice") + u, ok := actorByName(actors, "alice") if !ok { t.Fatal("alice missing from list") } @@ -104,14 +104,14 @@ func TestListUsersPage_RangeScopesStats(t *testing.T) { } } -// TestListUsersPage_UnionBoundaryNoDoubleCount models the snapped (whole-day) +// TestListActorsPage_UnionBoundaryNoDoubleCount models the snapped (whole-day) // roll-up: raw_floor's day is fully raw, so a daily_usage row on that same day is // stale and must be excluded by the strict `day < raw_floor` bound (else it would // double count against the raw floor-day spans). A strictly-earlier aggregate day // is included. -func TestListUsersPage_UnionBoundaryNoDoubleCount(t *testing.T) { - db := openTestUserDB(t) - mustCreateTestUser(t, db, "alice") +func TestListActorsPage_UnionBoundaryNoDoubleCount(t *testing.T) { + db := openTestActorDB(t) + mustCreateTestActor(t, db, "alice") // Anchor to midnight UTC so the raw span and the floor-day aggregate share a // calendar day regardless of the current time of day. today := time.Now().UTC().Truncate(24 * time.Hour) @@ -124,11 +124,11 @@ func TestListUsersPage_UnionBoundaryNoDoubleCount(t *testing.T) { // A strictly-earlier aggregate day — included. addDailyUsageRow(t, db, floor.AddDate(0, 0, -2), "alice", "sess-older", "m1", 7) - users, _, err := db.ListUsersPage(ListUsersOptions{}) + actors, _, err := db.ListActorsPage(ListActorsOptions{}) if err != nil { - t.Fatalf("ListUsersPage: %v", err) + t.Fatalf("ListActorsPage: %v", err) } - u, ok := userByName(users, "alice") + u, ok := actorByName(actors, "alice") if !ok { t.Fatal("alice missing") } @@ -140,96 +140,96 @@ func TestListUsersPage_UnionBoundaryNoDoubleCount(t *testing.T) { } } -// TestListUsersPage_AggregateRangeLowerBound checks that the range lower bound +// TestListActorsPage_AggregateRangeLowerBound checks that the range lower bound // is applied to daily_usage (day >= since) as well as raw spans. -func TestListUsersPage_AggregateRangeLowerBound(t *testing.T) { - db := openTestUserDB(t) - mustCreateTestUser(t, db, "alice") +func TestListActorsPage_AggregateRangeLowerBound(t *testing.T) { + db := openTestActorDB(t) + mustCreateTestActor(t, db, "alice") now := time.Now() addRangeSpan(t, db, "s-now", "alice", "sess-now", now.Add(-1*time.Hour), 1) addDailyUsageRow(t, db, now.AddDate(0, 0, -10), "alice", "sess-mid", "m1", 50) addDailyUsageRow(t, db, now.AddDate(0, 0, -40), "alice", "sess-old", "m1", 100) - month, _, err := db.ListUsersPage(ListUsersOptions{Since: ptrTime(now.AddDate(0, 0, -30))}) + month, _, err := db.ListActorsPage(ListActorsOptions{Since: ptrTime(now.AddDate(0, 0, -30))}) if err != nil { t.Fatalf("month: %v", err) } - u, _ := userByName(month, "alice") + u, _ := actorByName(month, "alice") if u.TotalCostUSD != 51 || u.Sessions != 2 { // now (1) + mid (50); old excluded t.Errorf("month: want cost 51 sessions 2, got %v/%v", u.TotalCostUSD, u.Sessions) } - all, _, err := db.ListUsersPage(ListUsersOptions{}) + all, _, err := db.ListActorsPage(ListActorsOptions{}) if err != nil { t.Fatalf("all: %v", err) } - ua, _ := userByName(all, "alice") + ua, _ := actorByName(all, "alice") if ua.TotalCostUSD != 151 || ua.Sessions != 3 { t.Errorf("all: want cost 151 sessions 3, got %v/%v", ua.TotalCostUSD, ua.Sessions) } } -// TestListUsersPage_SortAllKeys sorts by every key in both directions. -func TestListUsersPage_SortAllKeys(t *testing.T) { - db := openTestUserDB(t) - a := mustCreateTestUser(t, db, "a-user") - b := mustCreateTestUser(t, db, "b-user") - c := mustCreateTestUser(t, db, "c-user") +// TestListActorsPage_SortAllKeys sorts by every key in both directions. +func TestListActorsPage_SortAllKeys(t *testing.T) { + db := openTestActorDB(t) + a := mustCreateTestActor(t, db, "a-actor") + b := mustCreateTestActor(t, db, "b-actor") + c := mustCreateTestActor(t, db, "c-actor") base := time.Now().Add(-48 * time.Hour) - setUserCreatedAt(t, db, a.ID, base) - setUserCreatedAt(t, db, b.ID, base.Add(time.Hour)) - setUserCreatedAt(t, db, c.ID, base.Add(2*time.Hour)) + setActorCreatedAt(t, db, a.ID, base) + setActorCreatedAt(t, db, b.ID, base.Add(time.Hour)) + setActorCreatedAt(t, db, c.ID, base.Add(2*time.Hour)) now := time.Now() // cost: a=10, b=30, c=20 ; sessions: a=1, b=3, c=2 ; last_seen: a>b>c - addRangeSpan(t, db, "a1", "a-user", "a1", now, 10) - addRangeSpan(t, db, "b1", "b-user", "b1", now.Add(-1*time.Hour), 10) - addRangeSpan(t, db, "b2", "b-user", "b2", now.Add(-1*time.Hour), 10) - addRangeSpan(t, db, "b3", "b-user", "b3", now.Add(-1*time.Hour), 10) - addRangeSpan(t, db, "c1", "c-user", "c1", now.Add(-2*time.Hour), 10) - addRangeSpan(t, db, "c2", "c-user", "c2", now.Add(-2*time.Hour), 10) + addRangeSpan(t, db, "a1", "a-actor", "a1", now, 10) + addRangeSpan(t, db, "b1", "b-actor", "b1", now.Add(-1*time.Hour), 10) + addRangeSpan(t, db, "b2", "b-actor", "b2", now.Add(-1*time.Hour), 10) + addRangeSpan(t, db, "b3", "b-actor", "b3", now.Add(-1*time.Hour), 10) + addRangeSpan(t, db, "c1", "c-actor", "c1", now.Add(-2*time.Hour), 10) + addRangeSpan(t, db, "c2", "c-actor", "c2", now.Add(-2*time.Hour), 10) cases := []struct { sort, order string want []string }{ - {"name", "asc", []string{"a-user", "b-user", "c-user"}}, - {"name", "desc", []string{"c-user", "b-user", "a-user"}}, - {"created_at", "asc", []string{"a-user", "b-user", "c-user"}}, - {"created_at", "desc", []string{"c-user", "b-user", "a-user"}}, - {"cost", "asc", []string{"a-user", "c-user", "b-user"}}, - {"cost", "desc", []string{"b-user", "c-user", "a-user"}}, - {"sessions", "asc", []string{"a-user", "c-user", "b-user"}}, - {"sessions", "desc", []string{"b-user", "c-user", "a-user"}}, - {"last_seen", "asc", []string{"c-user", "b-user", "a-user"}}, - {"last_seen", "desc", []string{"a-user", "b-user", "c-user"}}, + {"name", "asc", []string{"a-actor", "b-actor", "c-actor"}}, + {"name", "desc", []string{"c-actor", "b-actor", "a-actor"}}, + {"created_at", "asc", []string{"a-actor", "b-actor", "c-actor"}}, + {"created_at", "desc", []string{"c-actor", "b-actor", "a-actor"}}, + {"cost", "asc", []string{"a-actor", "c-actor", "b-actor"}}, + {"cost", "desc", []string{"b-actor", "c-actor", "a-actor"}}, + {"sessions", "asc", []string{"a-actor", "c-actor", "b-actor"}}, + {"sessions", "desc", []string{"b-actor", "c-actor", "a-actor"}}, + {"last_seen", "asc", []string{"c-actor", "b-actor", "a-actor"}}, + {"last_seen", "desc", []string{"a-actor", "b-actor", "c-actor"}}, } for _, tc := range cases { t.Run(tc.sort+"_"+tc.order, func(t *testing.T) { - users, _, err := db.ListUsersPage(ListUsersOptions{Sort: tc.sort, Order: tc.order}) + actors, _, err := db.ListActorsPage(ListActorsOptions{Sort: tc.sort, Order: tc.order}) if err != nil { - t.Fatalf("ListUsersPage: %v", err) + t.Fatalf("ListActorsPage: %v", err) } - if got := userNames(users); !reflect.DeepEqual(got, tc.want) { + if got := actorNames(actors); !reflect.DeepEqual(got, tc.want) { t.Errorf("sort=%s order=%s: want %v, got %v", tc.sort, tc.order, tc.want, got) } }) } } -// TestListUsersPage_PagesConcatToFullList proves the sort order is global: page 1 +// TestListActorsPage_PagesConcatToFullList proves the sort order is global: page 1 // followed by page 2 equals the full sorted list, not a re-sorted page. -func TestListUsersPage_PagesConcatToFullList(t *testing.T) { - db := openTestUserDB(t) +func TestListActorsPage_PagesConcatToFullList(t *testing.T) { + db := openTestActorDB(t) now := time.Now() for i, name := range []string{"u1", "u2", "u3", "u4", "u5"} { - mustCreateTestUser(t, db, name) + mustCreateTestActor(t, db, name) addRangeSpan(t, db, "sp-"+name, name, "sess-"+name, now, float64((i+1)*10)) } - full, total, err := db.ListUsersPage(ListUsersOptions{Sort: "cost", Order: "desc"}) + full, total, err := db.ListActorsPage(ListActorsOptions{Sort: "cost", Order: "desc"}) if err != nil { t.Fatalf("full: %v", err) } @@ -237,9 +237,9 @@ func TestListUsersPage_PagesConcatToFullList(t *testing.T) { t.Fatalf("full list: want 5 rows/total, got %d rows total=%d", len(full), total) } - var paged []UserWithStats + var paged []ActorWithStats for page := 1; page <= 3; page++ { - rows, tot, err := db.ListUsersPage(ListUsersOptions{Sort: "cost", Order: "desc", Page: page, Limit: 2}) + rows, tot, err := db.ListActorsPage(ListActorsOptions{Sort: "cost", Order: "desc", Page: page, Limit: 2}) if err != nil { t.Fatalf("page %d: %v", page, err) } @@ -249,64 +249,64 @@ func TestListUsersPage_PagesConcatToFullList(t *testing.T) { paged = append(paged, rows...) } - if !reflect.DeepEqual(userNames(full), userNames(paged)) { - t.Errorf("page concat != full list:\n full=%v\npaged=%v", userNames(full), userNames(paged)) + if !reflect.DeepEqual(actorNames(full), actorNames(paged)) { + t.Errorf("page concat != full list:\n full=%v\npaged=%v", actorNames(full), actorNames(paged)) } } -// TestListUsersPage_QueryFilter filters on a case-insensitive name substring. -func TestListUsersPage_QueryFilter(t *testing.T) { - db := openTestUserDB(t) +// TestListActorsPage_QueryFilter filters on a case-insensitive name substring. +func TestListActorsPage_QueryFilter(t *testing.T) { + db := openTestActorDB(t) for _, name := range []string{"alice", "alicia", "bob"} { - mustCreateTestUser(t, db, name) + mustCreateTestActor(t, db, name) } - users, total, err := db.ListUsersPage(ListUsersOptions{Query: "ALI"}) + actors, total, err := db.ListActorsPage(ListActorsOptions{Query: "ALI"}) if err != nil { - t.Fatalf("ListUsersPage: %v", err) + t.Fatalf("ListActorsPage: %v", err) } - if total != 2 || len(users) != 2 { - t.Fatalf("q=ALI: want 2 rows/total, got %d rows total=%d (%v)", len(users), total, userNames(users)) + if total != 2 || len(actors) != 2 { + t.Fatalf("q=ALI: want 2 rows/total, got %d rows total=%d (%v)", len(actors), total, actorNames(actors)) } - if _, ok := userByName(users, "alice"); !ok { + if _, ok := actorByName(actors, "alice"); !ok { t.Error("q=ALI must match alice") } - if _, ok := userByName(users, "alicia"); !ok { + if _, ok := actorByName(actors, "alicia"); !ok { t.Error("q=ALI must match alicia") } - if _, ok := userByName(users, "bob"); ok { + if _, ok := actorByName(actors, "bob"); ok { t.Error("q=ALI must not match bob") } } -// TestListUsersPage_AnonymousSortsInline proves the __anonymous__ row is produced +// TestListActorsPage_AnonymousSortsInline proves the __anonymous__ row is produced // in SQL and sorts by its own stats, not appended at the end. -func TestListUsersPage_AnonymousSortsInline(t *testing.T) { - db := openTestUserDB(t) - mustCreateTestUser(t, db, "low") - mustCreateTestUser(t, db, "high") +func TestListActorsPage_AnonymousSortsInline(t *testing.T) { + db := openTestActorDB(t) + mustCreateTestActor(t, db, "low") + mustCreateTestActor(t, db, "high") now := time.Now() addRangeSpan(t, db, "low1", "low", "sess-low", now, 10) addRangeSpan(t, db, "high1", "high", "sess-high", now, 100) addRangeSpan(t, db, "anon1", "", "sess-anon", now, 50) // anonymous, between low and high - users, _, err := db.ListUsersPage(ListUsersOptions{Sort: "cost", Order: "desc"}) + actors, _, err := db.ListActorsPage(ListActorsOptions{Sort: "cost", Order: "desc"}) if err != nil { - t.Fatalf("ListUsersPage: %v", err) + t.Fatalf("ListActorsPage: %v", err) } want := []string{"high", "Anonymous", "low"} - if got := userNames(users); !reflect.DeepEqual(got, want) { + if got := actorNames(actors); !reflect.DeepEqual(got, want) { t.Errorf("anonymous not sorted inline: want %v, got %v", want, got) } } -// TestListUsersPage_AnonymousSurvivesRollup covers the anonymous bucket after +// TestListActorsPage_AnonymousSurvivesRollup covers the anonymous bucket after // retention has purged its raw spans: the cost lives only in daily_usage, and -// the row must still be listed — GetUserWithStats reports it either way, so +// the row must still be listed — GetActorWithStats reports it either way, so // gating the list row on raw spans alone would make the two endpoints disagree. -func TestListUsersPage_AnonymousSurvivesRollup(t *testing.T) { - db := openTestUserDB(t) - mustCreateTestUser(t, db, "alice") +func TestListActorsPage_AnonymousSurvivesRollup(t *testing.T) { + db := openTestActorDB(t) + mustCreateTestActor(t, db, "alice") today := time.Now().UTC().Truncate(24 * time.Hour) // alice's raw span is the only one left, so raw_floor lands on today. @@ -314,16 +314,16 @@ func TestListUsersPage_AnonymousSurvivesRollup(t *testing.T) { // Anonymous data exists only as a strictly-earlier aggregate. addDailyUsageRow(t, db, today.AddDate(0, 0, -2), "", "sess-anon", "m1", 50) - users, total, err := db.ListUsersPage(ListUsersOptions{Sort: "cost", Order: "desc"}) + actors, total, err := db.ListActorsPage(ListActorsOptions{Sort: "cost", Order: "desc"}) if err != nil { - t.Fatalf("ListUsersPage: %v", err) + t.Fatalf("ListActorsPage: %v", err) } if total != 2 { t.Errorf("total: want 2 (alice + anonymous), got %d", total) } - u, ok := userByName(users, "Anonymous") + u, ok := actorByName(actors, "Anonymous") if !ok { - t.Fatalf("anonymous row dropped after roll-up: got %v", userNames(users)) + t.Fatalf("anonymous row dropped after roll-up: got %v", actorNames(actors)) } if u.TotalCostUSD != 50 { t.Errorf("anonymous cost: want 50, got %v", u.TotalCostUSD) @@ -332,9 +332,9 @@ func TestListUsersPage_AnonymousSurvivesRollup(t *testing.T) { t.Errorf("anonymous sessions: want 1, got %v", u.Sessions) } - one, err := db.GetUserWithStats(AnonymousUserID, nil) + one, err := db.GetActorWithStats(AnonymousActorID, nil) if err != nil { - t.Fatalf("GetUserWithStats(anonymous): %v", err) + t.Fatalf("GetActorWithStats(anonymous): %v", err) } if one.TotalCostUSD != u.TotalCostUSD || one.Sessions != u.Sessions { t.Errorf("detail disagrees with list: detail %v/%v, list %v/%v", @@ -342,68 +342,68 @@ func TestListUsersPage_AnonymousSurvivesRollup(t *testing.T) { } } -// TestListUsersPage_LimitZeroReturnsAll confirms limit=0 disables paging. -func TestListUsersPage_LimitZeroReturnsAll(t *testing.T) { - db := openTestUserDB(t) +// TestListActorsPage_LimitZeroReturnsAll confirms limit=0 disables paging. +func TestListActorsPage_LimitZeroReturnsAll(t *testing.T) { + db := openTestActorDB(t) for _, name := range []string{"u1", "u2", "u3"} { - mustCreateTestUser(t, db, name) + mustCreateTestActor(t, db, name) } now := time.Now() addRangeSpan(t, db, "anon", "", "sess-anon", now, 1) - users, total, err := db.ListUsersPage(ListUsersOptions{Limit: 0}) + actors, total, err := db.ListActorsPage(ListActorsOptions{Limit: 0}) if err != nil { - t.Fatalf("ListUsersPage: %v", err) + t.Fatalf("ListActorsPage: %v", err) } - if len(users) != 4 || total != 4 { // 3 users + anonymous - t.Errorf("limit=0: want 4 rows/total, got %d rows total=%d", len(users), total) + if len(actors) != 4 || total != 4 { // 3 actors + anonymous + t.Errorf("limit=0: want 4 rows/total, got %d rows total=%d", len(actors), total) } } -// TestListUsersPage_PagePastEndKeepsTotal pins that total stays the match count +// TestListActorsPage_PagePastEndKeepsTotal pins that total stays the match count // on an out-of-range page, so a client can still tell how many pages exist. -func TestListUsersPage_PagePastEndKeepsTotal(t *testing.T) { - db := openTestUserDB(t) +func TestListActorsPage_PagePastEndKeepsTotal(t *testing.T) { + db := openTestActorDB(t) for _, name := range []string{"u1", "u2", "u3"} { - mustCreateTestUser(t, db, name) + mustCreateTestActor(t, db, name) } - users, total, err := db.ListUsersPage(ListUsersOptions{Limit: 2, Page: 99}) + actors, total, err := db.ListActorsPage(ListActorsOptions{Limit: 2, Page: 99}) if err != nil { - t.Fatalf("ListUsersPage: %v", err) + t.Fatalf("ListActorsPage: %v", err) } - if len(users) != 0 { - t.Errorf("page past end: want 0 rows, got %d", len(users)) + if len(actors) != 0 { + t.Errorf("page past end: want 0 rows, got %d", len(actors)) } if total != 3 { t.Errorf("page past end: want total=3, got %d", total) } - _, qTotal, err := db.ListUsersPage(ListUsersOptions{Query: "u1", Limit: 2, Page: 99}) + _, qTotal, err := db.ListActorsPage(ListActorsOptions{Query: "u1", Limit: 2, Page: 99}) if err != nil { - t.Fatalf("ListUsersPage(q): %v", err) + t.Fatalf("ListActorsPage(q): %v", err) } if qTotal != 1 { t.Errorf("page past end with q: want total=1, got %d", qTotal) } } -// TestGetUserWithStats covers named users, range scoping, anonymous, and 404s. -func TestGetUserWithStats(t *testing.T) { - db := openTestUserDB(t) - u := mustCreateTestUser(t, db, "alice") +// TestGetActorWithStats covers named actors, range scoping, anonymous, and 404s. +func TestGetActorWithStats(t *testing.T) { + db := openTestActorDB(t) + u := mustCreateTestActor(t, db, "alice") now := time.Now() addRangeSpan(t, db, "recent", "alice", "sess-recent", now.Add(-1*time.Hour), 5) addRangeSpan(t, db, "old", "alice", "sess-old", now.AddDate(0, 0, -60), 20) addRangeSpan(t, db, "anon", "", "sess-anon", now, 7) t.Run("named month-scoped", func(t *testing.T) { - got, err := db.GetUserWithStats(u.ID, ptrTime(now.AddDate(0, 0, -30))) + got, err := db.GetActorWithStats(u.ID, ptrTime(now.AddDate(0, 0, -30))) if err != nil { - t.Fatalf("GetUserWithStats: %v", err) + t.Fatalf("GetActorWithStats: %v", err) } if got.Name != "alice" || got.Token != u.Token { - t.Errorf("identity mismatch: %+v", got.User) + t.Errorf("identity mismatch: %+v", got.Actor) } if got.TotalCostUSD != 5 || got.Sessions != 1 { t.Errorf("month scope: want cost 5 sessions 1, got %v/%v", got.TotalCostUSD, got.Sessions) @@ -411,9 +411,9 @@ func TestGetUserWithStats(t *testing.T) { }) t.Run("named all-time", func(t *testing.T) { - got, err := db.GetUserWithStats(u.ID, nil) + got, err := db.GetActorWithStats(u.ID, nil) if err != nil { - t.Fatalf("GetUserWithStats: %v", err) + t.Fatalf("GetActorWithStats: %v", err) } if got.TotalCostUSD != 25 || got.Sessions != 2 { t.Errorf("all-time: want cost 25 sessions 2, got %v/%v", got.TotalCostUSD, got.Sessions) @@ -421,27 +421,27 @@ func TestGetUserWithStats(t *testing.T) { }) t.Run("anonymous", func(t *testing.T) { - got, err := db.GetUserWithStats(AnonymousUserID, nil) + got, err := db.GetActorWithStats(AnonymousActorID, nil) if err != nil { - t.Fatalf("GetUserWithStats(anon): %v", err) + t.Fatalf("GetActorWithStats(anon): %v", err) } - if got.ID != AnonymousUserID || got.TotalCostUSD != 7 || got.Sessions != 1 { + if got.ID != AnonymousActorID || got.TotalCostUSD != 7 || got.Sessions != 1 { t.Errorf("anon: want cost 7 sessions 1, got %+v", got) } }) t.Run("unknown 404", func(t *testing.T) { - if _, err := db.GetUserWithStats("no-such-id", nil); !errors.Is(err, ErrNotFound) { - t.Errorf("unknown user: want ErrNotFound, got %v", err) + if _, err := db.GetActorWithStats("no-such-id", nil); !errors.Is(err, ErrNotFound) { + t.Errorf("unknown actor: want ErrNotFound, got %v", err) } }) t.Run("soft-deleted 404", func(t *testing.T) { - if err := db.SoftDeleteUser(u.ID); err != nil { - t.Fatalf("SoftDeleteUser: %v", err) + if err := db.SoftDeleteActor(u.ID); err != nil { + t.Fatalf("SoftDeleteActor: %v", err) } - if _, err := db.GetUserWithStats(u.ID, nil); !errors.Is(err, ErrNotFound) { - t.Errorf("soft-deleted user: want ErrNotFound, got %v", err) + if _, err := db.GetActorWithStats(u.ID, nil); !errors.Is(err, ErrNotFound) { + t.Errorf("soft-deleted actor: want ErrNotFound, got %v", err) } }) } diff --git a/internal/storage/actors_test.go b/internal/storage/actors_test.go new file mode 100644 index 0000000..fd62a59 --- /dev/null +++ b/internal/storage/actors_test.go @@ -0,0 +1,219 @@ +package storage + +import ( + "errors" + "testing" + "time" +) + +func openTestActorDB(t *testing.T) *DB { + t.Helper() + db, err := Open(":memory:") + if err != nil { + t.Fatalf("open db: %v", err) + } + t.Cleanup(func() { db.Close() }) + return db +} + +func mustCreateTestActor(t *testing.T, db *DB, name string) Actor { + t.Helper() + u, err := db.CreateActor(name) + if err != nil { + t.Fatalf("CreateActor(%q): %v", name, err) + } + return u +} + +func insertTestSpan(t *testing.T, db *DB, spanID, actorID string) { + t.Helper() + now := time.Now() + if err := db.InsertSpan(Span{ + TraceID: "trace1", SpanID: spanID, Name: "llm", SessionID: "sess1", + ActorID: actorID, StartTime: now, EndTime: now.Add(time.Second), + }); err != nil { + t.Fatalf("InsertSpan: %v", err) + } +} + +func actorInTestList(t *testing.T, db *DB, name string) bool { + t.Helper() + actors, _, err := db.ListActorsPage(ListActorsOptions{}) + if err != nil { + t.Fatalf("ListActorsPage: %v", err) + } + for _, u := range actors { + if u.Name == name { + return true + } + } + return false +} + +func queryInt64(t *testing.T, db *DB, query string, args ...any) int64 { + t.Helper() + var n int64 + if err := db.rw.QueryRow(query, args...).Scan(&n); err != nil { + t.Fatalf("query %q: %v", query, err) + } + return n +} + +func queryString(t *testing.T, db *DB, query string, args ...any) string { + t.Helper() + var s string + if err := db.rw.QueryRow(query, args...).Scan(&s); err != nil { + t.Fatalf("query %q: %v", query, err) + } + return s +} + +// TestSoftDeleteActor_ActorOnly verifies mode-A (soft delete) semantics: +// actor disappears from listing, original token rejected, spans intact. +func TestSoftDeleteActor_ActorOnly(t *testing.T) { + db := openTestActorDB(t) + u := mustCreateTestActor(t, db, "alice") + insertTestSpan(t, db, "span-alice-1", "alice") + + if err := db.SoftDeleteActor(u.ID); err != nil { + t.Fatalf("SoftDeleteActor: %v", err) + } + + if actorInTestList(t, db, "alice") { + t.Error("alice must not appear in actor list after soft-delete") + } + + // Original live token must be rejected (GetActorByToken filters deleted_at IS NULL). + got, err := db.GetActorByToken(u.Token) + if err == nil || got != nil { + t.Errorf("soft-deleted token must be rejected, got actor=%v err=%v", got, err) + } + + // Spans must still reference alice (not anonymized in actor_only mode). + n := queryInt64(t, db, `SELECT COUNT(*) FROM spans WHERE actor_id = 'alice'`) + if n != 1 { + t.Errorf("want 1 span for alice after soft-delete, got %d", n) + } +} + +// TestSoftDeleteActor_NoResurface verifies the schema backfill does not recreate a +// soft-deleted actor. The actor row still exists (deleted_at set), so the +// WHERE NOT EXISTS guard prevents a new row. +func TestSoftDeleteActor_NoResurface(t *testing.T) { + db := openTestActorDB(t) + u := mustCreateTestActor(t, db, "alice") + insertTestSpan(t, db, "span-alice-2", "alice") + + if err := db.SoftDeleteActor(u.ID); err != nil { + t.Fatalf("SoftDeleteActor: %v", err) + } + + // Simulate the v4→v5 backfill that runs on every DB open. + _, err := db.rw.Exec(` + INSERT INTO actors (id, name, token) + SELECT uuid() AS id, actor_id AS name, + 'cotel_' || md5(uuid()::VARCHAR) || md5(uuid()::VARCHAR) AS token + FROM (SELECT DISTINCT actor_id FROM spans WHERE actor_id IS NOT NULL AND actor_id <> '') t + WHERE NOT EXISTS (SELECT 1 FROM actors u WHERE u.name = t.actor_id) + AND t.actor_id <> '[deleted]' + `) + if err != nil { + t.Fatalf("backfill: %v", err) + } + + if actorInTestList(t, db, "alice") { + t.Error("alice resurfaces after backfill — soft-delete guard broken") + } +} + +// TestDeleteActorWithHistory removes the actor row and hard-deletes their spans and daily_usage. +func TestDeleteActorWithHistory(t *testing.T) { + db := openTestActorDB(t) + u := mustCreateTestActor(t, db, "bob") + insertTestSpan(t, db, "span-bob-1", "bob") + + _, err := db.rw.Exec(` + INSERT INTO daily_usage (day, session_id, model, tool_name, actor_id, span_count) + VALUES (today(), 'sess1', '', '', 'bob', 1) + `) + if err != nil { + t.Fatalf("insert daily_usage: %v", err) + } + + if err := db.DeleteActorWithHistory(u.ID); err != nil { + t.Fatalf("DeleteActorWithHistory: %v", err) + } + + if actorInTestList(t, db, "bob") { + t.Error("bob must not appear in actor list after DeleteActorWithHistory") + } + + spanCount := queryInt64(t, db, `SELECT COUNT(*) FROM spans WHERE span_id = 'span-bob-1'`) + if spanCount != 0 { + t.Errorf("span must be deleted, got %d rows", spanCount) + } + + duCount := queryInt64(t, db, `SELECT COUNT(*) FROM daily_usage WHERE session_id = 'sess1'`) + if duCount != 0 { + t.Errorf("daily_usage must be deleted, got %d rows", duCount) + } +} + +// TestDeleteActorWithHistory_NoResurface verifies that spans are gone and the backfill +// guard skips any residual '[deleted]' rows from older data. +func TestDeleteActorWithHistory_NoResurface(t *testing.T) { + db := openTestActorDB(t) + u := mustCreateTestActor(t, db, "carol") + insertTestSpan(t, db, "span-carol-1", "carol") + + if err := db.DeleteActorWithHistory(u.ID); err != nil { + t.Fatalf("DeleteActorWithHistory: %v", err) + } + + spanCount := queryInt64(t, db, `SELECT COUNT(*) FROM spans WHERE span_id = 'span-carol-1'`) + if spanCount != 0 { + t.Errorf("carol's span must be deleted, got %d rows", spanCount) + } + + // Backfill guard: '[deleted]' sentinel must never resurface as a real actor. + _, err := db.rw.Exec(` + INSERT INTO actors (id, name, token) + SELECT uuid() AS id, actor_id AS name, + 'cotel_' || md5(uuid()::VARCHAR) || md5(uuid()::VARCHAR) AS token + FROM (SELECT DISTINCT actor_id FROM spans WHERE actor_id IS NOT NULL AND actor_id <> '') t + WHERE NOT EXISTS (SELECT 1 FROM actors u WHERE u.name = t.actor_id) + AND t.actor_id <> '[deleted]' + `) + if err != nil { + t.Fatalf("backfill: %v", err) + } + + if actorInTestList(t, db, "[deleted]") { + t.Error("[deleted] sentinel must never become a real actor") + } +} + +// TestSoftDeleteActor_NotFound returns ErrNotFound for unknown or already-deleted actors. +func TestSoftDeleteActor_NotFound(t *testing.T) { + db := openTestActorDB(t) + + if err := db.SoftDeleteActor("nonexistent-id"); !errors.Is(err, ErrNotFound) { + t.Errorf("nonexistent actor: want ErrNotFound, got %v", err) + } + + u := mustCreateTestActor(t, db, "dave") + if err := db.SoftDeleteActor(u.ID); err != nil { + t.Fatalf("first SoftDeleteActor: %v", err) + } + if err := db.SoftDeleteActor(u.ID); !errors.Is(err, ErrNotFound) { + t.Errorf("double soft-delete: want ErrNotFound, got %v", err) + } +} + +// TestDeleteActorWithHistory_NotFound returns ErrNotFound for unknown actors. +func TestDeleteActorWithHistory_NotFound(t *testing.T) { + db := openTestActorDB(t) + if err := db.DeleteActorWithHistory("nonexistent-id"); !errors.Is(err, ErrNotFound) { + t.Errorf("want ErrNotFound, got %v", err) + } +} diff --git a/internal/storage/art_repro_test.go b/internal/storage/art_repro_test.go index 5f30ca5..2c0768b 100644 --- a/internal/storage/art_repro_test.go +++ b/internal/storage/art_repro_test.go @@ -48,17 +48,17 @@ const ( // rows to keep. A roll-up that moves nothing exercises nothing. const reproSpanDays = 60 -// reproUsers is how many distinct values the index on the added column gets. +// reproActors is how many distinct values the index on the added column gets. // Low cardinality over many rows is what gives the ART long shared prefixes and // deep nodes, i.e. the structure the defect damages. -const reproUsers = 12 +const reproActors = 12 -// preUserIDSchema is `spans` as it stood at schema version 2, taken from that -// era's schema.sql: every column the table has today except user_id, plus +// preActorIDSchema is `spans` as it stood at schema version 2, taken from that +// era's schema.sql: every column the table has today except actor_id, plus // duration_ms, and the three ART indexes of that era. // // Both differences from today's table carry a migration. Starting without -// user_id is what makes the v2 -> v3 ALTER add a column to a filled, indexed +// actor_id is what makes the v2 -> v3 ALTER add a column to a filled, indexed // table. Starting *with* duration_ms is what makes the v9 -> v10 ALTER that // drops it do anything at all: `DROP COLUMN IF EXISTS` over a column that was // never there is a silent no-op, and v10 is the ALTER that ran in production on @@ -66,7 +66,7 @@ const reproUsers = 12 // day after the database was created. While the column is present the table // also carries the logical/physical column skew of ADR-0013, so the indexes are // built over the layout production built them over. -const preUserIDSchema = ` +const preActorIDSchema = ` CREATE TABLE schema_version ( version INTEGER PRIMARY KEY, applied_at TIMESTAMPTZ DEFAULT now() @@ -104,7 +104,7 @@ CREATE INDEX idx_spans_name ON spans(name); ` // seedSpans inserts a batch in one statement. Values are derived from the row -// number, never from a captured payload: the reproducer must carry no user id, +// number, never from a captured payload: the reproducer must carry no actor id, // session id or span attribute that ever belonged to anyone. Parameters are // (now, span-days, first row number, last row number). const seedSpans = ` @@ -139,8 +139,8 @@ FROM ( // value yet. This is the mutation the defect needs: a bulk write through an ART // index that was built on a column the table did not originally have. var populateAddedColumn = fmt.Sprintf( - `UPDATE spans SET user_id = 'user-' || (hash(span_id) %% %d)::VARCHAR WHERE user_id IS NULL`, - reproUsers) + `UPDATE spans SET actor_id = 'actor-' || (hash(span_id) %% %d)::VARCHAR WHERE actor_id IS NULL`, + reproActors) func envCount(t *testing.T, key string, def int) int { t.Helper() @@ -234,11 +234,11 @@ func TestARTIndexSurvivesAddedColumnAndBulkReplace(t *testing.T) { t.Fatalf("version(): %v", err) } t.Logf("engine %s: %d spans over %d days, then %d replace/restart cycles", engine, spans, reproSpanDays, cycles) - mustExec(t, seed, "pre-migration schema", preUserIDSchema) + mustExec(t, seed, "pre-migration schema", preActorIDSchema) mustExec(t, seed, fmt.Sprintf("seed %d spans", spans), seedSpans, now, reproSpanDays, 0, spans) if !spansHasColumn(t, seed, "duration_ms") { t.Fatal("the seeded database has no duration_ms, so it is not pre-v10 and the v10 ALTER " + - "under test would pass over it as a silent no-op: restore the column in preUserIDSchema") + "under test would pass over it as a silent no-op: restore the column in preActorIDSchema") } mustExec(t, seed, "seed checkpoint", "CHECKPOINT") if err := seed.Close(); err != nil { @@ -247,7 +247,7 @@ func TestARTIndexSurvivesAddedColumnAndBulkReplace(t *testing.T) { requireNoWAL(t, path, "after seeding") // Phase 2 — the migration, as cotel performs it on the next start: - // schema.sql adds user_id VARCHAR to the filled table, drops duration_ms + // schema.sql adds actor_id VARCHAR to the filled table, drops duration_ms // off the middle of it, and builds an ART index on the added column. db, err := Open(path) if err != nil { @@ -338,7 +338,7 @@ func TestARTIndexSurvivesAddedColumnAndBulkReplace(t *testing.T) { // primary key's ART index still holds its own entries. func assertIndexesIntact(t *testing.T, db *DB, wantSpans int) { t.Helper() - want := []string{"idx_spans_name", "idx_spans_session_id", "idx_spans_start_time", "idx_spans_user_id"} + want := []string{"idx_spans_actor_id", "idx_spans_name", "idx_spans_session_id", "idx_spans_start_time"} got := indexNames(t, db.ReadOnly()) if fmt.Sprint(got) != fmt.Sprint(want) { t.Fatalf("after reopen: indexes %v, want %v", got, want) @@ -359,7 +359,7 @@ func countSpans(t *testing.T, db *DB) int { return n } -// assertPushdownProbesAgree compares a bare `user_id = ` on the added +// assertPushdownProbesAgree compares a bare `actor_id = ` on the added // column, which the optimizer folds into the scan, against the same predicate // wrapped in an expression, which it cannot fold. A wrong count on the first is // this defect's silent form — the shape ADR-0013 documents, where the pushed @@ -375,15 +375,15 @@ func countSpans(t *testing.T, db *DB) int { func assertPushdownProbesAgree(t *testing.T, db *DB) { t.Helper() ro := db.ReadOnly() - for u := 0; u < reproUsers; u++ { - want := fmt.Sprintf("user-%d", u) - bare := fmt.Sprintf("SELECT count(*) FROM spans WHERE user_id = '%s'", want) - wrapped := fmt.Sprintf("SELECT count(*) FROM spans WHERE user_id || '' = '%s'", want) + for u := 0; u < reproActors; u++ { + want := fmt.Sprintf("actor-%d", u) + bare := fmt.Sprintf("SELECT count(*) FROM spans WHERE actor_id = '%s'", want) + wrapped := fmt.Sprintf("SELECT count(*) FROM spans WHERE actor_id || '' = '%s'", want) if u == 0 { // One pair of plans per restart is enough to show the two probes // still take different paths through the scan. - filter := fmt.Sprintf("user_id='%s'", want) + filter := fmt.Sprintf("actor_id='%s'", want) if plan := queryPlan(t, ro, bare); !strings.Contains(plan, filter) { t.Fatalf("%s is no longer pushed into the scan, so it tests the same path as the "+ "control probe and the pair has nothing left to disagree about; plan: %s", bare, plan) @@ -401,7 +401,7 @@ func assertPushdownProbesAgree(t *testing.T, db *DB) { t.Fatalf("control probe %s: %v", want, err) } if pushed != scanned { - t.Fatalf("REPRODUCED: user_id = %q matched %d rows as a pushed-down filter, %d as an expression", want, pushed, scanned) + t.Fatalf("REPRODUCED: actor_id = %q matched %d rows as a pushed-down filter, %d as an expression", want, pushed, scanned) } if pushed == 0 { t.Fatalf("probe %s matched nothing by either path; the fixture built no entries for it", want) diff --git a/internal/storage/backfill_test.go b/internal/storage/backfill_test.go index 5c206c8..bd35aea 100644 --- a/internal/storage/backfill_test.go +++ b/internal/storage/backfill_test.go @@ -246,7 +246,7 @@ func TestBackfillCostUSD_DoesNotTouchDailyUsage(t *testing.T) { // A rolled-up row whose cost was computed under the old (wrong) rates, and a // cache-heavy row whose correct cost is far above its input+output-only cost. if _, err := db.rw.Exec(` - INSERT INTO daily_usage (day, session_id, model, tool_name, user_id, span_count, + INSERT INTO daily_usage (day, session_id, model, tool_name, actor_id, span_count, total_input_tokens, total_output_tokens, total_cost_usd) VALUES ('2026-07-01', 'sess1', 'claude-opus-4-7', 'tool1', NULL, 10, 1000000, 0, 15.0), ('2026-07-02', 'sess2', 'claude-opus-4-8', 'tool1', NULL, 20, 1000, 0, 42.0)`); err != nil { diff --git a/internal/storage/duckdb.go b/internal/storage/duckdb.go index db156ac..89c7e58 100644 --- a/internal/storage/duckdb.go +++ b/internal/storage/duckdb.go @@ -211,6 +211,9 @@ func ensureSchema(rw *sql.DB) error { if current, ok := appliedSchemaVersion(rw); ok && current >= target && appliedSchemaSHA(rw) == sha { return nil } + if err := migrateUsersToActors(rw); err != nil { + return fmt.Errorf("migrate users to actors: %w", err) + } if _, err := rw.Exec(string(ddl)); err != nil { return fmt.Errorf("apply schema: %w", err) } @@ -220,6 +223,88 @@ func ensureSchema(rw *sql.DB) error { return nil } +// spansSecondaryIndexes are the ART indexes schema.sql creates on spans. DuckDB +// refuses to ALTER a table any index depends on, so a column rename has to drop +// them first; schema.sql recreates them right after (CREATE INDEX IF NOT EXISTS). +var spansSecondaryIndexes = []string{ + "idx_spans_session_id", "idx_spans_start_time", "idx_spans_name", + "idx_spans_user_id", "idx_spans_actor_id", +} + +// migrateUsersToActors is the v10 → v11 rename (ADR-0027): table users → +// actors, and spans.user_id / daily_usage.user_id → actor_id. It runs before +// schema.sql because a rename cannot be made idempotent in plain SQL, and +// schema.sql's CREATE TABLE IF NOT EXISTS actors / ADD COLUMN IF NOT EXISTS +// actor_id would otherwise create an empty actors table and an empty actor_id +// column beside the populated originals. Each step checks the catalog, so a +// fresh database, an already-migrated one and a re-apply are all no-ops. All +// steps commit together or not at all. +func migrateUsersToActors(rw *sql.DB) error { + tx, err := rw.Begin() + if err != nil { + return err + } + defer tx.Rollback() //nolint:errcheck // no-op after Commit + + hasUsers, err := tableExists(tx, "users") + if err != nil { + return err + } + if hasUsers { + hasActors, err := tableExists(tx, "actors") + if err != nil { + return err + } + if hasActors { + // Refuse to guess which of the two holds the real tokens. + return fmt.Errorf("both users and actors tables exist; resolve by hand before upgrading") + } + if _, err := tx.Exec(`ALTER TABLE users RENAME TO actors`); err != nil { + return fmt.Errorf("rename users: %w", err) + } + } + + spansHasUserID, err := columnExists(tx, "spans", "user_id") + if err != nil { + return err + } + if spansHasUserID { + for _, idx := range spansSecondaryIndexes { + if _, err := tx.Exec(`DROP INDEX IF EXISTS ` + idx); err != nil { + return fmt.Errorf("drop %s: %w", idx, err) + } + } + if _, err := tx.Exec(`ALTER TABLE spans RENAME COLUMN user_id TO actor_id`); err != nil { + return fmt.Errorf("rename spans.user_id: %w", err) + } + } + + dailyHasUserID, err := columnExists(tx, "daily_usage", "user_id") + if err != nil { + return err + } + if dailyHasUserID { + if _, err := tx.Exec(`ALTER TABLE daily_usage RENAME COLUMN user_id TO actor_id`); err != nil { + return fmt.Errorf("rename daily_usage.user_id: %w", err) + } + } + return tx.Commit() +} + +func tableExists(tx *sql.Tx, table string) (bool, error) { + var n int + err := tx.QueryRow(`SELECT count(*) FROM information_schema.tables + WHERE table_schema = 'main' AND table_name = ?`, table).Scan(&n) + return n > 0, err +} + +func columnExists(tx *sql.Tx, table, column string) (bool, error) { + var n int + err := tx.QueryRow(`SELECT count(*) FROM information_schema.columns + WHERE table_schema = 'main' AND table_name = ? AND column_name = ?`, table, column).Scan(&n) + return n > 0, err +} + func (db *DB) Close() error { return db.rw.Close() } // Checkpoint folds the write-ahead log into the main database file so the next @@ -246,8 +331,9 @@ type Span struct { SessionID string Model string ToolName string - // UserID identifies the sender; set via OTEL resource attribute user.id (empty = unset → stored as NULL). - UserID string + // ActorID is the name of the actor whose token sent the span, injected by the + // auth middleware; empty (anonymous ingest) is stored as NULL. + ActorID string // StatusCode is the OTLP span status: 0=UNSET, 1=OK, 2=ERROR. StatusCode int32 InputTokens *int64 @@ -265,7 +351,7 @@ const insertSpan = ` INSERT OR IGNORE INTO spans ( trace_id, span_id, parent_span_id, name, start_time, end_time, service_name, - session_id, model, tool_name, user_id, status_code, + session_id, model, tool_name, actor_id, status_code, input_tokens, output_tokens, cache_read_tokens, cache_write_tokens, cost_usd, attributes, resource_attrs ) VALUES (?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)` @@ -286,7 +372,7 @@ func (db *DB) InsertSpan(s Span) error { _, err := db.rw.Exec(insertSpan, s.TraceID, s.SpanID, s.ParentSpanID, s.Name, s.StartTime, s.EndTime, s.ServiceName, - s.SessionID, s.Model, s.ToolName, nullableStr(s.UserID), s.StatusCode, + s.SessionID, s.Model, s.ToolName, nullableStr(s.ActorID), s.StatusCode, s.InputTokens, s.OutputTokens, s.CacheReadTokens, s.CacheWriteTokens, s.CostUSD, s.Attributes, s.ResourceAttrs, ) diff --git a/internal/storage/export.go b/internal/storage/export.go index 09ff80c..baebf0a 100644 --- a/internal/storage/export.go +++ b/internal/storage/export.go @@ -11,7 +11,7 @@ type DailyUsageRow struct { SessionID string Model string ToolName string - UserID string + ActorID string SpanCount int64 TotalInputTokens int64 TotalOutputTokens int64 @@ -30,7 +30,7 @@ func (db *DB) ExportSpans(from, to time.Time) ([]Span, error) { rows, err := db.rw.Query(` SELECT trace_id, span_id, parent_span_id, name, start_time, end_time, service_name, - session_id, model, tool_name, user_id, status_code, + session_id, model, tool_name, actor_id, status_code, input_tokens, output_tokens, cache_read_tokens, cache_write_tokens, cost_usd, attributes::VARCHAR AS attributes, resource_attrs::VARCHAR AS resource_attrs, ingested_at FROM spans @@ -44,12 +44,12 @@ func (db *DB) ExportSpans(from, to time.Time) ([]Span, error) { var spans []Span for rows.Next() { var s Span - var parentSpanID, serviceName, sessionID, model, toolName, userID sql.NullString + var parentSpanID, serviceName, sessionID, model, toolName, actorID sql.NullString var attrs, resourceAttrs sql.NullString if err := rows.Scan( &s.TraceID, &s.SpanID, &parentSpanID, &s.Name, &s.StartTime, &s.EndTime, &serviceName, - &sessionID, &model, &toolName, &userID, &s.StatusCode, + &sessionID, &model, &toolName, &actorID, &s.StatusCode, &s.InputTokens, &s.OutputTokens, &s.CacheReadTokens, &s.CacheWriteTokens, &s.CostUSD, &attrs, &resourceAttrs, &s.IngestedAt, ); err != nil { @@ -60,7 +60,7 @@ func (db *DB) ExportSpans(from, to time.Time) ([]Span, error) { s.SessionID = sessionID.String s.Model = model.String s.ToolName = toolName.String - s.UserID = userID.String + s.ActorID = actorID.String s.Attributes = attrs.String s.ResourceAttrs = resourceAttrs.String spans = append(spans, s) @@ -71,7 +71,7 @@ func (db *DB) ExportSpans(from, to time.Time) ([]Span, error) { // ExportDailyUsage returns all daily_usage rows with day in [from, to). func (db *DB) ExportDailyUsage(from, to time.Time) ([]DailyUsageRow, error) { rows, err := db.rw.Query(` - SELECT day, session_id, model, tool_name, user_id, + SELECT day, session_id, model, tool_name, actor_id, span_count, total_input_tokens, total_output_tokens, total_cache_read_tokens, total_cache_write_tokens, total_cost_usd, total_duration_ms, fail_count @@ -86,9 +86,9 @@ func (db *DB) ExportDailyUsage(from, to time.Time) ([]DailyUsageRow, error) { var out []DailyUsageRow for rows.Next() { var r DailyUsageRow - var sessionID, model, toolName, userID sql.NullString + var sessionID, model, toolName, actorID sql.NullString if err := rows.Scan( - &r.Day, &sessionID, &model, &toolName, &userID, + &r.Day, &sessionID, &model, &toolName, &actorID, &r.SpanCount, &r.TotalInputTokens, &r.TotalOutputTokens, &r.TotalCacheReadTokens, &r.TotalCacheWriteTokens, &r.TotalCostUSD, &r.TotalDurationMs, &r.FailCount, @@ -98,7 +98,7 @@ func (db *DB) ExportDailyUsage(from, to time.Time) ([]DailyUsageRow, error) { r.SessionID = sessionID.String r.Model = model.String r.ToolName = toolName.String - r.UserID = userID.String + r.ActorID = actorID.String out = append(out, r) } return out, rows.Err() @@ -108,7 +108,7 @@ const importSpanSQL = ` INSERT OR IGNORE INTO spans ( trace_id, span_id, parent_span_id, name, start_time, end_time, service_name, - session_id, model, tool_name, user_id, status_code, + session_id, model, tool_name, actor_id, status_code, input_tokens, output_tokens, cache_read_tokens, cache_write_tokens, cost_usd, attributes, resource_attrs, ingested_at ) VALUES (?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)` @@ -147,7 +147,7 @@ func (db *DB) ImportSpans(spans []Span) (int, error) { s.TraceID, s.SpanID, nullableStr(s.ParentSpanID), s.Name, s.StartTime, s.EndTime, nullableStr(s.ServiceName), nullableStr(s.SessionID), nullableStr(s.Model), nullableStr(s.ToolName), - nullableStr(s.UserID), s.StatusCode, + nullableStr(s.ActorID), s.StatusCode, s.InputTokens, s.OutputTokens, s.CacheReadTokens, s.CacheWriteTokens, s.CostUSD, s.Attributes, s.ResourceAttrs, ingestedAt, ) @@ -162,7 +162,7 @@ func (db *DB) ImportSpans(spans []Span) (int, error) { const importDailyUsageSQL = ` INSERT OR IGNORE INTO daily_usage ( - day, session_id, model, tool_name, user_id, + day, session_id, model, tool_name, actor_id, span_count, total_input_tokens, total_output_tokens, total_cache_read_tokens, total_cache_write_tokens, total_cost_usd, total_duration_ms, fail_count @@ -191,7 +191,7 @@ func (db *DB) ImportDailyUsage(rows []DailyUsageRow) (int, error) { for _, r := range rows { res, err := stmt.Exec( r.Day, nullableStr(r.SessionID), nullableStr(r.Model), nullableStr(r.ToolName), - nullableStr(r.UserID), + nullableStr(r.ActorID), r.SpanCount, r.TotalInputTokens, r.TotalOutputTokens, r.TotalCacheReadTokens, r.TotalCacheWriteTokens, r.TotalCostUSD, r.TotalDurationMs, r.FailCount, diff --git a/internal/storage/pushdown_test.go b/internal/storage/pushdown_test.go index a873990..c133e5d 100644 --- a/internal/storage/pushdown_test.go +++ b/internal/storage/pushdown_test.go @@ -68,7 +68,7 @@ func pushdownFixture(t *testing.T, db *DB) { SessionID: fmt.Sprintf("session-%d", i), Model: "claude-opus-4", ToolName: "Bash", - UserID: fmt.Sprintf("user-%d", i), + ActorID: fmt.Sprintf("actor-%d", i), }) if err != nil { t.Fatalf("insert span %d: %v", i, err) @@ -206,24 +206,24 @@ func TestUpgradeDropsGeneratedDurationColumn(t *testing.T) { start_time TIMESTAMPTZ NOT NULL, end_time TIMESTAMPTZ NOT NULL, duration_ms DOUBLE GENERATED ALWAYS AS (epoch_ms(end_time) - epoch_ms(start_time)), service_name VARCHAR, session_id VARCHAR, model VARCHAR, tool_name VARCHAR, - user_id VARCHAR, status_code TINYINT DEFAULT 0, + actor_id VARCHAR, status_code TINYINT DEFAULT 0, input_tokens INTEGER, output_tokens INTEGER, cache_read_tokens INTEGER, cache_write_tokens INTEGER, cost_usd DOUBLE, attributes JSON, resource_attrs JSON, ingested_at TIMESTAMPTZ DEFAULT now() )`); err != nil { t.Fatalf("seed v9 spans: %v", err) } - for _, idx := range []string{"session_id", "start_time", "name", "user_id"} { + for _, idx := range []string{"session_id", "start_time", "name", "actor_id"} { if _, err := raw.Exec(`CREATE INDEX idx_spans_` + idx + ` ON spans(` + idx + `)`); err != nil { t.Fatalf("seed index on %s: %v", idx, err) } } for i := 0; i < 3; i++ { if _, err := raw.Exec(` - INSERT INTO spans (trace_id, span_id, name, start_time, end_time, service_name, session_id, model, tool_name, user_id) + INSERT INTO spans (trace_id, span_id, name, start_time, end_time, service_name, session_id, model, tool_name, actor_id) VALUES ('t', ?, 'tool.execution', ?, ?, 'claude-code', ?, 'claude-opus-4', 'Bash', ?)`, fmt.Sprintf("span-%d", i), time.Unix(int64(i), 0), time.Unix(int64(i)+1, 0), - fmt.Sprintf("session-%d", i), fmt.Sprintf("user-%d", i)); err != nil { + fmt.Sprintf("session-%d", i), fmt.Sprintf("actor-%d", i)); err != nil { t.Fatalf("seed span %d: %v", i, err) } } diff --git a/internal/storage/retention.go b/internal/storage/retention.go index 77e6cd5..2c3235f 100644 --- a/internal/storage/retention.go +++ b/internal/storage/retention.go @@ -128,7 +128,7 @@ func (db *DB) rollupAndPurgeAt(cfg RetentionConfig, now time.Time) error { if _, err := tx.Exec(` INSERT INTO daily_usage - (day, session_id, model, tool_name, user_id, + (day, session_id, model, tool_name, actor_id, span_count, total_input_tokens, total_output_tokens, total_cache_read_tokens, total_cache_write_tokens, total_cost_usd, total_duration_ms, fail_count) @@ -137,7 +137,7 @@ func (db *DB) rollupAndPurgeAt(cfg RetentionConfig, now time.Time) error { COALESCE(NULLIF(session_id, ''), ?) AS session_id, COALESCE(NULLIF(model, ''), ?) AS model, COALESCE(NULLIF(tool_name, ''), ?) AS tool_name, - MAX(user_id) AS user_id, + MAX(actor_id) AS actor_id, COUNT(*) AS span_count, COALESCE(SUM(input_tokens), 0), COALESCE(SUM(output_tokens), 0), @@ -158,7 +158,7 @@ func (db *DB) rollupAndPurgeAt(cfg RetentionConfig, now time.Time) error { total_cost_usd = daily_usage.total_cost_usd + excluded.total_cost_usd, total_duration_ms = COALESCE(daily_usage.total_duration_ms, 0) + excluded.total_duration_ms, fail_count = COALESCE(daily_usage.fail_count, 0) + excluded.fail_count, - user_id = COALESCE(daily_usage.user_id, excluded.user_id) + actor_id = COALESCE(daily_usage.actor_id, excluded.actor_id) `, UnknownSentinel, UnknownSentinel, UnknownSentinel, rollupCutoff); err != nil { return err } diff --git a/internal/storage/retention_test.go b/internal/storage/retention_test.go index 07c4f57..77da519 100644 --- a/internal/storage/retention_test.go +++ b/internal/storage/retention_test.go @@ -181,7 +181,7 @@ func TestRollupAndPurge_DurationAndFailCoalesceNULL(t *testing.T) { day := time.Date(2026, 1, 15, 0, 0, 0, 0, time.UTC) if _, err := db.rw.Exec(` INSERT INTO daily_usage ( - day, session_id, model, tool_name, user_id, + day, session_id, model, tool_name, actor_id, span_count, total_input_tokens, total_output_tokens, total_cost_usd, total_duration_ms, fail_count ) VALUES (CAST(? AS DATE), 'sess', 'claude-opus-5', 'Bash', NULL, diff --git a/internal/storage/schema.sql b/internal/storage/schema.sql index da3b6c2..f3715be 100644 --- a/internal/storage/schema.sql +++ b/internal/storage/schema.sql @@ -1,4 +1,4 @@ --- Schema version: 10 +-- Schema version: 11 -- Versioned; never silently rename columns. CREATE TABLE IF NOT EXISTS schema_version ( @@ -26,8 +26,9 @@ CREATE TABLE IF NOT EXISTS spans ( model VARCHAR, tool_name VARCHAR, - -- User identifier: NULL when unauthenticated, UUID from users table when authenticated - user_id VARCHAR, + -- Actor (person, agent or host) the span is attributed to: the actors.name + -- of the token that sent it, NULL when unauthenticated. Was user_id before v11. + actor_id VARCHAR, -- OTLP span status: 0=UNSET, 1=OK, 2=ERROR status_code TINYINT DEFAULT 0, @@ -49,8 +50,8 @@ CREATE TABLE IF NOT EXISTS spans ( -- Migration v1 → v2: add status_code if upgrading an existing database. ALTER TABLE spans ADD COLUMN IF NOT EXISTS status_code TINYINT DEFAULT 0; --- Migration v2 → v3: add user_id for multi-user telemetry separation. -ALTER TABLE spans ADD COLUMN IF NOT EXISTS user_id VARCHAR; +-- Migration v2 → v3: add the attribution column (named user_id until v11). +ALTER TABLE spans ADD COLUMN IF NOT EXISTS actor_id VARCHAR; -- Migration v9 → v10: drop the derived duration_ms column (ADR-0013). As a -- VIRTUAL generated column it took a logical slot but no storage slot, so every @@ -64,14 +65,14 @@ ALTER TABLE spans ADD COLUMN IF NOT EXISTS user_id VARCHAR; DROP INDEX IF EXISTS idx_spans_session_id; DROP INDEX IF EXISTS idx_spans_start_time; DROP INDEX IF EXISTS idx_spans_name; -DROP INDEX IF EXISTS idx_spans_user_id; +DROP INDEX IF EXISTS idx_spans_actor_id; ALTER TABLE spans DROP COLUMN IF EXISTS duration_ms; -- Indexes for dashboard hot paths (DuckDB ART indexes). CREATE INDEX IF NOT EXISTS idx_spans_session_id ON spans(session_id); CREATE INDEX IF NOT EXISTS idx_spans_start_time ON spans(start_time); CREATE INDEX IF NOT EXISTS idx_spans_name ON spans(name); -CREATE INDEX IF NOT EXISTS idx_spans_user_id ON spans(user_id); +CREATE INDEX IF NOT EXISTS idx_spans_actor_id ON spans(actor_id); -- Daily aggregates for long-term retention (schema version 1) CREATE TABLE IF NOT EXISTS daily_usage ( @@ -79,7 +80,7 @@ CREATE TABLE IF NOT EXISTS daily_usage ( session_id VARCHAR, model VARCHAR, tool_name VARCHAR, - user_id VARCHAR, + actor_id VARCHAR, span_count BIGINT, total_input_tokens BIGINT, total_output_tokens BIGINT, @@ -91,8 +92,8 @@ CREATE TABLE IF NOT EXISTS daily_usage ( PRIMARY KEY (day, session_id, model, tool_name) ); --- Migration v2 → v3: add user_id to daily_usage. -ALTER TABLE daily_usage ADD COLUMN IF NOT EXISTS user_id VARCHAR; +-- Migration v2 → v3: add the attribution column to daily_usage (user_id until v11). +ALTER TABLE daily_usage ADD COLUMN IF NOT EXISTS actor_id VARCHAR; -- Migration v7 → v8: preserve cache-token totals through roll-up. -- Additive, nullable, no default: rows rolled up before this migration stay NULL @@ -114,8 +115,9 @@ CREATE TABLE IF NOT EXISTS api_tokens ( last_used_at TIMESTAMPTZ ); --- Named users with always-visible plaintext tokens (schema version 5) -CREATE TABLE IF NOT EXISTS users ( +-- Named actors (people, agents, hosts) with always-visible plaintext tokens. +-- Created as `users` in schema version 5, renamed to `actors` in version 11. +CREATE TABLE IF NOT EXISTS actors ( id VARCHAR PRIMARY KEY, name VARCHAR NOT NULL UNIQUE, token VARCHAR NOT NULL UNIQUE, @@ -132,20 +134,20 @@ CREATE TABLE IF NOT EXISTS settings ( -- Seed allow_anonymous=true (old "no tokens = open" default preserved) INSERT INTO settings (key, value) VALUES ('allow_anonymous', 'true') ON CONFLICT DO NOTHING; --- Migration v4→v5: backfill a user row for each distinct span user_id. +-- Migration v4→v5: backfill an actor row for each distinct span actor_id. -- Uses ON CONFLICT DO NOTHING on the UNIQUE name column to be idempotent. -- Token is 'cotel_' + 64 hex chars built from two md5 calls to avoid external deps. -INSERT INTO users (id, name, token) +INSERT INTO actors (id, name, token) SELECT uuid() AS id, - user_id AS name, + actor_id AS name, 'cotel_' || md5(uuid()::VARCHAR) || md5(uuid()::VARCHAR) AS token -FROM (SELECT DISTINCT user_id FROM spans WHERE user_id IS NOT NULL AND user_id <> '') t -WHERE NOT EXISTS (SELECT 1 FROM users u WHERE u.name = t.user_id) - AND t.user_id <> '[deleted]'; +FROM (SELECT DISTINCT actor_id FROM spans WHERE actor_id IS NOT NULL AND actor_id <> '') t +WHERE NOT EXISTS (SELECT 1 FROM actors u WHERE u.name = t.actor_id) + AND t.actor_id <> '[deleted]'; -- Migration v6 → v7: add deleted_at for soft-delete support. -ALTER TABLE users ADD COLUMN IF NOT EXISTS deleted_at TIMESTAMPTZ DEFAULT NULL; +ALTER TABLE actors ADD COLUMN IF NOT EXISTS deleted_at TIMESTAMPTZ DEFAULT NULL; INSERT INTO schema_version (version) VALUES (1) ON CONFLICT DO NOTHING; INSERT INTO schema_version (version) VALUES (2) ON CONFLICT DO NOTHING; @@ -157,3 +159,7 @@ INSERT INTO schema_version (version) VALUES (7) ON CONFLICT DO NOTHING; INSERT INTO schema_version (version) VALUES (8) ON CONFLICT DO NOTHING; INSERT INTO schema_version (version) VALUES (9) ON CONFLICT DO NOTHING; INSERT INTO schema_version (version) VALUES (10) ON CONFLICT DO NOTHING; +-- v11 renames users → actors and user_id → actor_id. A rename cannot be written +-- idempotently in SQL, so it is not in this file: migrateUsersToActors in +-- duckdb.go runs it before this file is applied (ADR-0027). +INSERT INTO schema_version (version) VALUES (11) ON CONFLICT DO NOTHING; diff --git a/internal/storage/schema_version_test.go b/internal/storage/schema_version_test.go index 4fd188b..ae2aa17 100644 --- a/internal/storage/schema_version_test.go +++ b/internal/storage/schema_version_test.go @@ -66,7 +66,8 @@ func TestEnsureSchema_FreshDB(t *testing.T) { func TestEnsureSchema_PreMarkerDB(t *testing.T) { path := filepath.Join(t.TempDir(), "premarker.duckdb") - // Simulate an old database: a spans table, no schema_version table/rows. + // Simulate an old database: a spans table with the pre-v11 user_id column and + // no schema_version table/rows. raw, err := sql.Open("duckdb", path) if err != nil { t.Fatalf("raw open: %v", err) @@ -93,13 +94,13 @@ func TestEnsureSchema_PreMarkerDB(t *testing.T) { if got, want := appliedVersion(t, db), embeddedVersion(t); got != want { t.Fatalf("recorded version = %d, want %d", got, want) } - // The user backfill migration must have run against the pre-existing spans. + // The actor backfill migration must have run against the pre-existing spans. var n int - if err := db.rw.QueryRow(`SELECT count(*) FROM users WHERE name = 'alice'`).Scan(&n); err != nil { - t.Fatalf("count users: %v", err) + if err := db.rw.QueryRow(`SELECT count(*) FROM actors WHERE name = 'alice'`).Scan(&n); err != nil { + t.Fatalf("count actors: %v", err) } if n != 1 { - t.Fatalf("backfilled users for 'alice' = %d, want 1", n) + t.Fatalf("backfilled actors for 'alice' = %d, want 1", n) } } diff --git a/internal/storage/snapshot_test.go b/internal/storage/snapshot_test.go index 5ba8cdc..05f4eb5 100644 --- a/internal/storage/snapshot_test.go +++ b/internal/storage/snapshot_test.go @@ -45,7 +45,7 @@ func TestSnapshotWritesManifestLast(t *testing.T) { t.Fatalf("Snapshot: %v", err) } - for _, name := range []string{"schema.sql", "load.sql", "spans.parquet", "users.parquet", SnapshotManifestName} { + for _, name := range []string{"schema.sql", "load.sql", "spans.parquet", "actors.parquet", SnapshotManifestName} { if _, err := os.Stat(filepath.Join(m.Directory, name)); err != nil { t.Errorf("snapshot is missing %s: %v", name, err) } diff --git a/internal/storage/testdata/schema_v10.sql b/internal/storage/testdata/schema_v10.sql new file mode 100644 index 0000000..da3b6c2 --- /dev/null +++ b/internal/storage/testdata/schema_v10.sql @@ -0,0 +1,159 @@ +-- Schema version: 10 +-- Versioned; never silently rename columns. + +CREATE TABLE IF NOT EXISTS schema_version ( + version INTEGER PRIMARY KEY, + applied_at TIMESTAMPTZ DEFAULT now() +); + +-- Raw OTLP spans from Claude Code +CREATE TABLE IF NOT EXISTS spans ( + -- OTLP identity + trace_id VARCHAR NOT NULL, + span_id VARCHAR NOT NULL PRIMARY KEY, + parent_span_id VARCHAR, + name VARCHAR NOT NULL, + + -- Timing + start_time TIMESTAMPTZ NOT NULL, + end_time TIMESTAMPTZ NOT NULL, + + -- Service metadata + service_name VARCHAR, + + -- Extracted high-cardinality keys (index targets) + session_id VARCHAR, + model VARCHAR, + tool_name VARCHAR, + + -- User identifier: NULL when unauthenticated, UUID from users table when authenticated + user_id VARCHAR, + + -- OTLP span status: 0=UNSET, 1=OK, 2=ERROR + status_code TINYINT DEFAULT 0, + + -- Token / cost counters + input_tokens INTEGER, + output_tokens INTEGER, + cache_read_tokens INTEGER, + cache_write_tokens INTEGER, + cost_usd DOUBLE, + + -- Full attribute bag (for ad-hoc queries) + attributes JSON, + resource_attrs JSON, + + ingested_at TIMESTAMPTZ DEFAULT now() +); + +-- Migration v1 → v2: add status_code if upgrading an existing database. +ALTER TABLE spans ADD COLUMN IF NOT EXISTS status_code TINYINT DEFAULT 0; + +-- Migration v2 → v3: add user_id for multi-user telemetry separation. +ALTER TABLE spans ADD COLUMN IF NOT EXISTS user_id VARCHAR; + +-- Migration v9 → v10: drop the derived duration_ms column (ADR-0013). As a +-- VIRTUAL generated column it took a logical slot but no storage slot, so every +-- later column's logical index ran one ahead of its physical index and a bare +-- `col = ` probed an unrelated index and matched nothing. Duration is +-- computed where it is read. +-- +-- DuckDB refuses to ALTER a table an index depends on, so the secondary indexes +-- are dropped here and recreated below. Both statements are idempotent, so a +-- re-apply of this file rebuilds the indexes but moves no row data. +DROP INDEX IF EXISTS idx_spans_session_id; +DROP INDEX IF EXISTS idx_spans_start_time; +DROP INDEX IF EXISTS idx_spans_name; +DROP INDEX IF EXISTS idx_spans_user_id; +ALTER TABLE spans DROP COLUMN IF EXISTS duration_ms; + +-- Indexes for dashboard hot paths (DuckDB ART indexes). +CREATE INDEX IF NOT EXISTS idx_spans_session_id ON spans(session_id); +CREATE INDEX IF NOT EXISTS idx_spans_start_time ON spans(start_time); +CREATE INDEX IF NOT EXISTS idx_spans_name ON spans(name); +CREATE INDEX IF NOT EXISTS idx_spans_user_id ON spans(user_id); + +-- Daily aggregates for long-term retention (schema version 1) +CREATE TABLE IF NOT EXISTS daily_usage ( + day DATE NOT NULL, + session_id VARCHAR, + model VARCHAR, + tool_name VARCHAR, + user_id VARCHAR, + span_count BIGINT, + total_input_tokens BIGINT, + total_output_tokens BIGINT, + total_cache_read_tokens BIGINT, + total_cache_write_tokens BIGINT, + total_cost_usd DOUBLE, + total_duration_ms DOUBLE, + fail_count BIGINT, + PRIMARY KEY (day, session_id, model, tool_name) +); + +-- Migration v2 → v3: add user_id to daily_usage. +ALTER TABLE daily_usage ADD COLUMN IF NOT EXISTS user_id VARCHAR; + +-- Migration v7 → v8: preserve cache-token totals through roll-up. +-- Additive, nullable, no default: rows rolled up before this migration stay NULL +-- (honestly "unknown", not a fabricated 0); rows rolled up after get real sums. +ALTER TABLE daily_usage ADD COLUMN IF NOT EXISTS total_cache_read_tokens BIGINT; +ALTER TABLE daily_usage ADD COLUMN IF NOT EXISTS total_cache_write_tokens BIGINT; + +-- Migration v8 → v9: preserve duration and failure counts through roll-up. +ALTER TABLE daily_usage ADD COLUMN IF NOT EXISTS total_duration_ms DOUBLE; +ALTER TABLE daily_usage ADD COLUMN IF NOT EXISTS fail_count BIGINT; + +-- API tokens for OTLP ingest auth (schema version 4) — kept for upgrade safety, not used by new auth +CREATE TABLE IF NOT EXISTS api_tokens ( + id VARCHAR PRIMARY KEY, + name VARCHAR NOT NULL, + token_hash VARCHAR NOT NULL UNIQUE, + prefix VARCHAR NOT NULL, + created_at TIMESTAMPTZ DEFAULT now(), + last_used_at TIMESTAMPTZ +); + +-- Named users with always-visible plaintext tokens (schema version 5) +CREATE TABLE IF NOT EXISTS users ( + id VARCHAR PRIMARY KEY, + name VARCHAR NOT NULL UNIQUE, + token VARCHAR NOT NULL UNIQUE, + created_at TIMESTAMPTZ DEFAULT now() +); + +-- Runtime key/value settings (schema version 6) +CREATE TABLE IF NOT EXISTS settings ( + key VARCHAR PRIMARY KEY, + value VARCHAR NOT NULL, + updated_at TIMESTAMPTZ DEFAULT now() +); + +-- Seed allow_anonymous=true (old "no tokens = open" default preserved) +INSERT INTO settings (key, value) VALUES ('allow_anonymous', 'true') ON CONFLICT DO NOTHING; + +-- Migration v4→v5: backfill a user row for each distinct span user_id. +-- Uses ON CONFLICT DO NOTHING on the UNIQUE name column to be idempotent. +-- Token is 'cotel_' + 64 hex chars built from two md5 calls to avoid external deps. +INSERT INTO users (id, name, token) +SELECT + uuid() AS id, + user_id AS name, + 'cotel_' || md5(uuid()::VARCHAR) || md5(uuid()::VARCHAR) AS token +FROM (SELECT DISTINCT user_id FROM spans WHERE user_id IS NOT NULL AND user_id <> '') t +WHERE NOT EXISTS (SELECT 1 FROM users u WHERE u.name = t.user_id) + AND t.user_id <> '[deleted]'; + +-- Migration v6 → v7: add deleted_at for soft-delete support. +ALTER TABLE users ADD COLUMN IF NOT EXISTS deleted_at TIMESTAMPTZ DEFAULT NULL; + +INSERT INTO schema_version (version) VALUES (1) ON CONFLICT DO NOTHING; +INSERT INTO schema_version (version) VALUES (2) ON CONFLICT DO NOTHING; +INSERT INTO schema_version (version) VALUES (3) ON CONFLICT DO NOTHING; +INSERT INTO schema_version (version) VALUES (4) ON CONFLICT DO NOTHING; +INSERT INTO schema_version (version) VALUES (5) ON CONFLICT DO NOTHING; +INSERT INTO schema_version (version) VALUES (6) ON CONFLICT DO NOTHING; +INSERT INTO schema_version (version) VALUES (7) ON CONFLICT DO NOTHING; +INSERT INTO schema_version (version) VALUES (8) ON CONFLICT DO NOTHING; +INSERT INTO schema_version (version) VALUES (9) ON CONFLICT DO NOTHING; +INSERT INTO schema_version (version) VALUES (10) ON CONFLICT DO NOTHING; diff --git a/internal/storage/users_test.go b/internal/storage/users_test.go deleted file mode 100644 index fd0104b..0000000 --- a/internal/storage/users_test.go +++ /dev/null @@ -1,219 +0,0 @@ -package storage - -import ( - "errors" - "testing" - "time" -) - -func openTestUserDB(t *testing.T) *DB { - t.Helper() - db, err := Open(":memory:") - if err != nil { - t.Fatalf("open db: %v", err) - } - t.Cleanup(func() { db.Close() }) - return db -} - -func mustCreateTestUser(t *testing.T, db *DB, name string) User { - t.Helper() - u, err := db.CreateUser(name) - if err != nil { - t.Fatalf("CreateUser(%q): %v", name, err) - } - return u -} - -func insertTestSpan(t *testing.T, db *DB, spanID, userID string) { - t.Helper() - now := time.Now() - if err := db.InsertSpan(Span{ - TraceID: "trace1", SpanID: spanID, Name: "llm", SessionID: "sess1", - UserID: userID, StartTime: now, EndTime: now.Add(time.Second), - }); err != nil { - t.Fatalf("InsertSpan: %v", err) - } -} - -func userInTestList(t *testing.T, db *DB, name string) bool { - t.Helper() - users, _, err := db.ListUsersPage(ListUsersOptions{}) - if err != nil { - t.Fatalf("ListUsersPage: %v", err) - } - for _, u := range users { - if u.Name == name { - return true - } - } - return false -} - -func queryInt64(t *testing.T, db *DB, query string, args ...any) int64 { - t.Helper() - var n int64 - if err := db.rw.QueryRow(query, args...).Scan(&n); err != nil { - t.Fatalf("query %q: %v", query, err) - } - return n -} - -func queryString(t *testing.T, db *DB, query string, args ...any) string { - t.Helper() - var s string - if err := db.rw.QueryRow(query, args...).Scan(&s); err != nil { - t.Fatalf("query %q: %v", query, err) - } - return s -} - -// TestSoftDeleteUser_UserOnly verifies mode-A (soft delete) semantics: -// user disappears from listing, original token rejected, spans intact. -func TestSoftDeleteUser_UserOnly(t *testing.T) { - db := openTestUserDB(t) - u := mustCreateTestUser(t, db, "alice") - insertTestSpan(t, db, "span-alice-1", "alice") - - if err := db.SoftDeleteUser(u.ID); err != nil { - t.Fatalf("SoftDeleteUser: %v", err) - } - - if userInTestList(t, db, "alice") { - t.Error("alice must not appear in user list after soft-delete") - } - - // Original live token must be rejected (GetUserByToken filters deleted_at IS NULL). - got, err := db.GetUserByToken(u.Token) - if err == nil || got != nil { - t.Errorf("soft-deleted token must be rejected, got user=%v err=%v", got, err) - } - - // Spans must still reference alice (not anonymized in user_only mode). - n := queryInt64(t, db, `SELECT COUNT(*) FROM spans WHERE user_id = 'alice'`) - if n != 1 { - t.Errorf("want 1 span for alice after soft-delete, got %d", n) - } -} - -// TestSoftDeleteUser_NoResurface verifies the schema backfill does not recreate a -// soft-deleted user. The user row still exists (deleted_at set), so the -// WHERE NOT EXISTS guard prevents a new row. -func TestSoftDeleteUser_NoResurface(t *testing.T) { - db := openTestUserDB(t) - u := mustCreateTestUser(t, db, "alice") - insertTestSpan(t, db, "span-alice-2", "alice") - - if err := db.SoftDeleteUser(u.ID); err != nil { - t.Fatalf("SoftDeleteUser: %v", err) - } - - // Simulate the v4→v5 backfill that runs on every DB open. - _, err := db.rw.Exec(` - INSERT INTO users (id, name, token) - SELECT uuid() AS id, user_id AS name, - 'cotel_' || md5(uuid()::VARCHAR) || md5(uuid()::VARCHAR) AS token - FROM (SELECT DISTINCT user_id FROM spans WHERE user_id IS NOT NULL AND user_id <> '') t - WHERE NOT EXISTS (SELECT 1 FROM users u WHERE u.name = t.user_id) - AND t.user_id <> '[deleted]' - `) - if err != nil { - t.Fatalf("backfill: %v", err) - } - - if userInTestList(t, db, "alice") { - t.Error("alice resurfaces after backfill — soft-delete guard broken") - } -} - -// TestDeleteUserWithHistory removes the user row and hard-deletes their spans and daily_usage. -func TestDeleteUserWithHistory(t *testing.T) { - db := openTestUserDB(t) - u := mustCreateTestUser(t, db, "bob") - insertTestSpan(t, db, "span-bob-1", "bob") - - _, err := db.rw.Exec(` - INSERT INTO daily_usage (day, session_id, model, tool_name, user_id, span_count) - VALUES (today(), 'sess1', '', '', 'bob', 1) - `) - if err != nil { - t.Fatalf("insert daily_usage: %v", err) - } - - if err := db.DeleteUserWithHistory(u.ID); err != nil { - t.Fatalf("DeleteUserWithHistory: %v", err) - } - - if userInTestList(t, db, "bob") { - t.Error("bob must not appear in user list after DeleteUserWithHistory") - } - - spanCount := queryInt64(t, db, `SELECT COUNT(*) FROM spans WHERE span_id = 'span-bob-1'`) - if spanCount != 0 { - t.Errorf("span must be deleted, got %d rows", spanCount) - } - - duCount := queryInt64(t, db, `SELECT COUNT(*) FROM daily_usage WHERE session_id = 'sess1'`) - if duCount != 0 { - t.Errorf("daily_usage must be deleted, got %d rows", duCount) - } -} - -// TestDeleteUserWithHistory_NoResurface verifies that spans are gone and the backfill -// guard skips any residual '[deleted]' rows from older data. -func TestDeleteUserWithHistory_NoResurface(t *testing.T) { - db := openTestUserDB(t) - u := mustCreateTestUser(t, db, "carol") - insertTestSpan(t, db, "span-carol-1", "carol") - - if err := db.DeleteUserWithHistory(u.ID); err != nil { - t.Fatalf("DeleteUserWithHistory: %v", err) - } - - spanCount := queryInt64(t, db, `SELECT COUNT(*) FROM spans WHERE span_id = 'span-carol-1'`) - if spanCount != 0 { - t.Errorf("carol's span must be deleted, got %d rows", spanCount) - } - - // Backfill guard: '[deleted]' sentinel must never resurface as a real user. - _, err := db.rw.Exec(` - INSERT INTO users (id, name, token) - SELECT uuid() AS id, user_id AS name, - 'cotel_' || md5(uuid()::VARCHAR) || md5(uuid()::VARCHAR) AS token - FROM (SELECT DISTINCT user_id FROM spans WHERE user_id IS NOT NULL AND user_id <> '') t - WHERE NOT EXISTS (SELECT 1 FROM users u WHERE u.name = t.user_id) - AND t.user_id <> '[deleted]' - `) - if err != nil { - t.Fatalf("backfill: %v", err) - } - - if userInTestList(t, db, "[deleted]") { - t.Error("[deleted] sentinel must never become a real user") - } -} - -// TestSoftDeleteUser_NotFound returns ErrNotFound for unknown or already-deleted users. -func TestSoftDeleteUser_NotFound(t *testing.T) { - db := openTestUserDB(t) - - if err := db.SoftDeleteUser("nonexistent-id"); !errors.Is(err, ErrNotFound) { - t.Errorf("nonexistent user: want ErrNotFound, got %v", err) - } - - u := mustCreateTestUser(t, db, "dave") - if err := db.SoftDeleteUser(u.ID); err != nil { - t.Fatalf("first SoftDeleteUser: %v", err) - } - if err := db.SoftDeleteUser(u.ID); !errors.Is(err, ErrNotFound) { - t.Errorf("double soft-delete: want ErrNotFound, got %v", err) - } -} - -// TestDeleteUserWithHistory_NotFound returns ErrNotFound for unknown users. -func TestDeleteUserWithHistory_NotFound(t *testing.T) { - db := openTestUserDB(t) - if err := db.DeleteUserWithHistory("nonexistent-id"); !errors.Is(err, ErrNotFound) { - t.Errorf("want ErrNotFound, got %v", err) - } -} diff --git a/scripts/agent-set-cotel-token.sh b/scripts/agent-set-cotel-token.sh index ac62fc9..4183537 100755 --- a/scripts/agent-set-cotel-token.sh +++ b/scripts/agent-set-cotel-token.sh @@ -2,11 +2,11 @@ # # agent-set-cotel-token.sh — give the running agent its own cotel telemetry token. # -# cotel attributes OTLP spans to a user by the bearer token in +# cotel attributes OTLP spans to an actor by the bearer token in # OTEL_EXPORTER_OTLP_HEADERS. If every Claude Code instance sends the same token, # all agents merge into one bucket. This script makes each agent self-attribute # by writing its OWN token into ITS OWN Paperclip adapter env, so every future -# run exports telemetry under that agent's distinct cotel user. +# run exports telemetry under that agent's distinct cotel actor. # # Why self-apply (not a central rollout): identical to the git-identity case — # the Paperclip API redacts other agents' adapterConfig and rejects cross-agent @@ -25,7 +25,7 @@ # (settings.json `env` is applied at startup and overrides the inherited per-agent process env.) # # The token cannot be derived — it is minted in the cotel dashboard -# (Users -> Add user, one user per agent) and is a `cotel_<64 hex>` string. Pass +# (Actors -> Add actor, one actor per agent) and is a `cotel_<64 hex>` string. Pass # it as the first argument or via COTEL_TOKEN. # # Idempotent: re-running with the same token is a no-op. @@ -47,7 +47,7 @@ set -euo pipefail TOKEN="${1:-${COTEL_TOKEN:-}}" if [ -z "$TOKEN" ]; then echo "usage: $0 (or set COTEL_TOKEN)" >&2 - echo "mint one in the cotel dashboard: Users -> Add user (one per agent)" >&2 + echo "mint one in the cotel dashboard: Actors -> Add actor (one per agent)" >&2 exit 2 fi if ! printf '%s' "$TOKEN" | grep -Eq '^cotel_[0-9a-f]{64}$'; then diff --git a/scripts/seed-demo.py b/scripts/seed-demo.py index 855e019..73c0777 100755 --- a/scripts/seed-demo.py +++ b/scripts/seed-demo.py @@ -1,7 +1,7 @@ #!/usr/bin/env python3 """Seed a cotel instance with synthetic Claude Code telemetry. -Point it at a *throwaway* instance — it creates users and ingests spans, it +Point it at a *throwaway* instance — it creates actors and ingests spans, it never deletes anything. Used to produce the README screenshots and to give a fresh install something to look at. @@ -26,8 +26,8 @@ from datetime import datetime, timedelta, timezone # Named principals, heaviest first. Weight drives how many sessions each one -# opens, so the Users top-5 has a readable spread instead of seven equal bars. -USERS = [ +# opens, so the Actors top-5 has a readable spread instead of seven equal bars. +ACTORS = [ ("alex-mbp", 1.00), ("priya-mbp", 0.85), ("sam-desktop", 0.70), @@ -37,7 +37,7 @@ ("nightly-bot", 0.25), ] -# Per-user model mix: (model, weight). ci-runner and nightly-bot are automation +# Per-actor model mix: (model, weight). ci-runner and nightly-bot are automation # and ride the cheap tiers. MODEL_MIX = { "ci-runner": [("claude-haiku-4-5", 0.7), ("claude-sonnet-5", 0.3)], @@ -79,13 +79,13 @@ def post(url, payload, token=None, timeout=60): return json.loads(raw) if raw else {} -def create_users(dash_url, rng): +def create_actors(dash_url, rng): tokens = {} - for name, _ in USERS: + for name, _ in ACTORS: try: - created = post(dash_url + "/api/v1/users", {"name": name}) + created = post(dash_url + "/api/v1/actors", {"name": name}) except urllib.error.HTTPError as e: - sys.exit("could not create user %s: %s %s" % (name, e.code, e.read().decode()[:200])) + sys.exit("could not create actor %s: %s %s" % (name, e.code, e.read().decode()[:200])) tokens[name] = created["token"] return tokens @@ -124,9 +124,9 @@ def model_tokens(rng, model): } -def build_session(rng, user, start, session_id): +def build_session(rng, actor, start, session_id): """Return the OTLP resourceSpans payload for one session.""" - model = pick(rng, MODEL_MIX.get(user, MODEL_MIX["_default"])) + model = pick(rng, MODEL_MIX.get(actor, MODEL_MIX["_default"])) trace_id = hex_id(rng, 32) turns = rng.randint(4, 22) # A session that errors does so on a tool, not on the whole run; roughly one @@ -201,8 +201,8 @@ def main(): ingest = args.ingest_url.rstrip("/") + "/v1/traces" rng = random.Random(args.seed) - tokens = create_users(dash, rng) - print("created %d users" % len(tokens)) + tokens = create_actors(dash, rng) + print("created %d actors" % len(tokens)) now = datetime.now(timezone.utc) sessions = 0 @@ -213,8 +213,8 @@ def main(): hour=0, minute=0, second=0, microsecond=0 ) weight = DAY_WEIGHT[day.weekday()] - for user, user_weight in USERS: - count = rng.random() * 3.4 * weight * user_weight + for actor, actor_weight in ACTORS: + count = rng.random() * 3.4 * weight * actor_weight for _ in range(int(count) + (1 if rng.random() < count % 1 else 0)): # Working hours, jittered; the current day stops at "now" so the # newest rows are not in the future. @@ -226,9 +226,9 @@ def main(): if start > now - timedelta(minutes=25): continue session_id = "sess_" + hex_id(rng, 12) - payload = build_session(rng, user, start, session_id) + payload = build_session(rng, actor, start, session_id) try: - post(ingest, payload, token=tokens[user]) + post(ingest, payload, token=tokens[actor]) except urllib.error.URLError as e: sys.exit("ingest failed: %s" % e) sessions += 1 diff --git a/scripts/shoot-screenshots.mjs b/scripts/shoot-screenshots.mjs index 5734cb2..05aeb1b 100644 --- a/scripts/shoot-screenshots.mjs +++ b/scripts/shoot-screenshots.mjs @@ -23,7 +23,7 @@ const SHOTS = [ wait: '.recharts-surface', end: `(${STAT_SECTIONS}).find(d => /^spans & cost/i.test(d.textContent.trim()))`, }, - { name: 'dashboard-users', path: '/users', wait: 'table', end: `document.querySelector('table')` }, + { name: 'dashboard-actors', path: '/actors', wait: 'table', end: `document.querySelector('table')` }, { name: 'dashboard-tools', path: '/tools', wait: 'table', end: `document.querySelector('table')` }, { name: 'dashboard-sessions', path: '/sessions', wait: 'table', end: `document.querySelectorAll('tbody tr')[8]` }, { From e1fb0e1c5ab039319e9cffa84124482829327ef7 Mon Sep 17 00:00:00 2001 From: FlopBut Date: Sun, 15 Nov 2026 12:37:22 +0100 Subject: [PATCH 3/4] feat(frontend): Users page becomes Actors Pages, routes, components and the actor filter follow the API rename: /actors and /actors/:id, ?actor_id=, actors_count on the Overview KPI. The old /users and /users/:id URLs redirect so bookmarks keep working. The Setup step now says what an actor is - a person, an AI agent or a host - instead of asking the reader to add themselves as a user. Co-Authored-By: Claude Opus 5.5 --- frontend/src/api/index.ts | 80 ++++++++-------- ...module.css => DeleteActorModal.module.css} | 0 ...leteUserModal.tsx => DeleteActorModal.tsx} | 60 ++++++------ frontend/src/components/Layout.tsx | 2 +- frontend/src/components/index.tsx | 2 +- frontend/src/context/ActorContext.tsx | 21 ++++ frontend/src/context/UserContext.tsx | 21 ---- frontend/src/main.tsx | 18 +++- ...tail.module.css => ActorDetail.module.css} | 0 .../pages/{UserDetail.tsx => ActorDetail.tsx} | 40 ++++---- .../{Users.module.css => Actors.module.css} | 6 +- frontend/src/pages/{Users.tsx => Actors.tsx} | 66 ++++++------- frontend/src/pages/Costs.module.css | 2 +- frontend/src/pages/Costs.tsx | 14 +-- frontend/src/pages/History.module.css | 2 +- frontend/src/pages/History.tsx | 14 +-- frontend/src/pages/Models.module.css | 2 +- frontend/src/pages/Models.tsx | 14 +-- frontend/src/pages/Overview.module.css | 4 +- frontend/src/pages/Overview.tsx | 96 +++++++++---------- frontend/src/pages/Sessions.module.css | 8 +- frontend/src/pages/Sessions.tsx | 32 +++---- frontend/src/pages/Setup.tsx | 14 +-- frontend/src/pages/Tools.module.css | 2 +- frontend/src/pages/Tools.tsx | 16 ++-- 25 files changed, 272 insertions(+), 264 deletions(-) rename frontend/src/components/{DeleteUserModal.module.css => DeleteActorModal.module.css} (100%) rename frontend/src/components/{DeleteUserModal.tsx => DeleteActorModal.tsx} (60%) create mode 100644 frontend/src/context/ActorContext.tsx delete mode 100644 frontend/src/context/UserContext.tsx rename frontend/src/pages/{UserDetail.module.css => ActorDetail.module.css} (100%) rename frontend/src/pages/{UserDetail.tsx => ActorDetail.tsx} (83%) rename frontend/src/pages/{Users.module.css => Actors.module.css} (99%) rename frontend/src/pages/{Users.tsx => Actors.tsx} (77%) diff --git a/frontend/src/api/index.ts b/frontend/src/api/index.ts index 6d2973b..0a890af 100644 --- a/frontend/src/api/index.ts +++ b/frontend/src/api/index.ts @@ -9,7 +9,7 @@ export const fetcher = (url: string) => export interface OverviewResponse { range: string sessions_count: number - users_count: number + actors_count: number total_cost_usd: number total_input_tokens: number total_output_tokens: number @@ -21,7 +21,7 @@ export interface OverviewResponse { export interface SessionItem { session_id: string - user_id: string + actor_id: string first_seen: string last_seen: string model: string @@ -95,7 +95,7 @@ export interface BashCommandItem { } // ListParams is the range/search/sort/paging contract the tools and -// bash-commands lists share with the users list (ADR-0011, ADR-0012). +// bash-commands lists share with the actors list (ADR-0011, ADR-0012). export interface ListParams { range: string q?: string @@ -103,7 +103,7 @@ export interface ListParams { order: string page: number limit: number - user_id?: string + actor_id?: string } export type ToolsParams = ListParams @@ -140,7 +140,7 @@ function listQuery(p: ListParams): string { limit: String(p.limit), }) if (p.q) qs.set('q', p.q) - if (p.user_id) qs.set('user_id', p.user_id) + if (p.actor_id) qs.set('actor_id', p.actor_id) return qs.toString() } @@ -152,8 +152,8 @@ export interface ModelItem { total_output_tokens: number } -export interface UserRow { - user_id: string +export interface ActorRow { + actor_id: string is_default: boolean sessions: number total_cost_usd: number @@ -163,12 +163,12 @@ export interface UserRow { last_seen: string } -export interface UsersResponse { - items: UserRow[] +export interface ActorsResponse { + items: ActorRow[] } -// New user management types -export interface User { +// New actor management types +export interface Actor { id: string name: string token: string @@ -178,12 +178,12 @@ export interface User { last_seen: string | null } -export interface UsersListResponse { - users: User[] +export interface ActorsListResponse { + actors: Actor[] } -export interface UsersPageResponse { - users: User[] +export interface ActorsPageResponse { + actors: Actor[] total: number page: number limit: number @@ -192,7 +192,7 @@ export interface UsersPageResponse { order: string } -export interface UsersPageParams { +export interface ActorsPageParams { range: string q?: string sort: string @@ -205,12 +205,12 @@ export interface SettingsResponse { allow_anonymous: boolean } -// useUsers fetches every user unpaginated (limit=0) — used by the UserSearch typeahead. -export function useUsers() { - return useSWR('/api/v1/users', fetcher) +// useActors fetches every actor unpaginated (limit=0) — used by the ActorSearch typeahead. +export function useActors() { + return useSWR('/api/v1/actors', fetcher) } -export function useUsersPage(params: UsersPageParams) { +export function useActorsPage(params: ActorsPageParams) { const qs = new URLSearchParams({ range: params.range, sort: params.sort, @@ -219,20 +219,20 @@ export function useUsersPage(params: UsersPageParams) { limit: String(params.limit), }) if (params.q) qs.set('q', params.q) - return useSWR(`/api/v1/users?${qs.toString()}`, fetcher, { keepPreviousData: true }) + return useSWR(`/api/v1/actors?${qs.toString()}`, fetcher, { keepPreviousData: true }) } -export function useUser(id: string | undefined, range: string) { +export function useActor(id: string | undefined, range: string) { const qs = new URLSearchParams({ range }) - return useSWR(id ? `/api/v1/users/${encodeURIComponent(id)}?${qs.toString()}` : null, fetcher) + return useSWR(id ? `/api/v1/actors/${encodeURIComponent(id)}?${qs.toString()}` : null, fetcher) } export function useSettings() { return useSWR('/api/v1/settings', fetcher) } -export async function createUser(name: string): Promise { - const res = await fetch('/api/v1/users', { +export async function createActor(name: string): Promise { + const res = await fetch('/api/v1/actors', { method: 'POST', headers: { 'Content-Type': 'application/json' }, body: JSON.stringify({ name }), @@ -244,8 +244,8 @@ export async function createUser(name: string): Promise { return res.json() } -export async function rotateUserToken(id: string): Promise { - const res = await fetch(`/api/v1/users/${id}/rotate-token`, { method: 'POST' }) +export async function rotateActorToken(id: string): Promise { + const res = await fetch(`/api/v1/actors/${id}/rotate-token`, { method: 'POST' }) if (!res.ok) { const text = await res.text().catch(() => res.statusText) throw new Error(text || `${res.status}`) @@ -253,10 +253,10 @@ export async function rotateUserToken(id: string): Promise { return res.json() } -export type DeleteMode = 'user_only' | 'user_and_history' +export type DeleteMode = 'actor_only' | 'actor_and_history' -export async function deleteUser(id: string, mode?: DeleteMode): Promise { - const url = mode ? `/api/v1/users/${id}?mode=${mode}` : `/api/v1/users/${id}` +export async function deleteActor(id: string, mode?: DeleteMode): Promise { + const url = mode ? `/api/v1/actors/${id}?mode=${mode}` : `/api/v1/actors/${id}` const res = await fetch(url, { method: 'DELETE' }) if (!res.ok && res.status !== 204) { const text = await res.text().catch(() => res.statusText) @@ -280,9 +280,9 @@ export async function updateSettings(settings: Partial): Promi // The four hooks below take range last and omit the parameter when it is // undefined, so a caller that does not pass one keeps the server-side default // for that endpoint (ADR-0014). -export function useOverview(refreshInterval = 30_000, userId?: string, range?: string) { +export function useOverview(refreshInterval = 30_000, actorId?: string, range?: string) { const params = new URLSearchParams() - if (userId) params.set('user_id', userId) + if (actorId) params.set('actor_id', actorId) if (range) params.set('range', range) const qs = params.toString() return useSWR(`/api/v1/overview${qs ? `?${qs}` : ''}`, fetcher, { @@ -296,11 +296,11 @@ export function useSessions( limit = 50, sort = 'start_time', order = 'desc', - userId?: string, + actorId?: string, range?: string, ) { const params = new URLSearchParams({ page: String(page), limit: String(limit), sort, order }) - if (userId) params.set('user_id', userId) + if (actorId) params.set('actor_id', actorId) if (range) params.set('range', range) return useSWR(`/api/v1/sessions?${params.toString()}`, fetcher, { keepPreviousData: true, @@ -311,11 +311,11 @@ export function useSession(id: string) { return useSWR(id ? `/api/v1/sessions/${id}` : null, fetcher) } -export function useCosts(from?: string, to?: string, userId?: string, range?: string) { +export function useCosts(from?: string, to?: string, actorId?: string, range?: string) { const params = new URLSearchParams() if (from) params.set('from', from) if (to) params.set('to', to) - if (userId) params.set('user_id', userId) + if (actorId) params.set('actor_id', actorId) if (range) params.set('range', range) const qs = params.toString() return useSWR(`/api/v1/costs${qs ? `?${qs}` : ''}`, fetcher, { @@ -338,9 +338,9 @@ export interface ModelsResponse { range: string } -export function useModels(userId?: string, range?: string) { +export function useModels(actorId?: string, range?: string) { const params = new URLSearchParams() - if (userId) params.set('user_id', userId) + if (actorId) params.set('actor_id', actorId) if (range) params.set('range', range) const qs = params.toString() return useSWR(`/api/v1/models${qs ? `?${qs}` : ''}`, fetcher, { @@ -387,13 +387,13 @@ export function useHistory( granularity: string, from?: string, to?: string, - userId?: string, + actorId?: string, range?: string, ) { const params = new URLSearchParams({ granularity }) if (from) params.set('from', from) if (to) params.set('to', to) - if (userId) params.set('user_id', userId) + if (actorId) params.set('actor_id', actorId) if (range) params.set('range', range) return useSWR(`/api/v1/history?${params.toString()}`, fetcher, { keepPreviousData: true, diff --git a/frontend/src/components/DeleteUserModal.module.css b/frontend/src/components/DeleteActorModal.module.css similarity index 100% rename from frontend/src/components/DeleteUserModal.module.css rename to frontend/src/components/DeleteActorModal.module.css diff --git a/frontend/src/components/DeleteUserModal.tsx b/frontend/src/components/DeleteActorModal.tsx similarity index 60% rename from frontend/src/components/DeleteUserModal.tsx rename to frontend/src/components/DeleteActorModal.tsx index d4b104d..018a649 100644 --- a/frontend/src/components/DeleteUserModal.tsx +++ b/frontend/src/components/DeleteActorModal.tsx @@ -1,15 +1,15 @@ import { useState, useEffect, useRef } from 'react' -import { deleteUser } from '../api' -import type { User, DeleteMode } from '../api' -import styles from './DeleteUserModal.module.css' +import { deleteActor } from '../api' +import type { Actor, DeleteMode } from '../api' +import styles from './DeleteActorModal.module.css' -interface DeleteUserModalProps { - user: User +interface DeleteActorModalProps { + actor: Actor onClose: () => void onDeleted: (mode: DeleteMode) => void } -export function DeleteUserModal({ user, onClose, onDeleted }: DeleteUserModalProps) { +export function DeleteActorModal({ actor, onClose, onDeleted }: DeleteActorModalProps) { const [mode, setMode] = useState(null) const [confirmText, setConfirmText] = useState('') const [loading, setLoading] = useState(false) @@ -25,8 +25,8 @@ export function DeleteUserModal({ user, onClose, onDeleted }: DeleteUserModalPro return () => document.removeEventListener('keydown', onKey) }, [onClose]) - const modeAReady = mode === 'user_only' - const modeBReady = mode === 'user_and_history' && confirmText === user.name + const modeAReady = mode === 'actor_only' + const modeBReady = mode === 'actor_and_history' && confirmText === actor.name const canSubmit = modeAReady || modeBReady const handleSubmit = async () => { @@ -34,7 +34,7 @@ export function DeleteUserModal({ user, onClose, onDeleted }: DeleteUserModalPro setLoading(true) setError(null) try { - await deleteUser(user.id, mode) + await deleteActor(actor.id, mode) onDeleted(mode) } catch (err) { setError(err instanceof Error ? err.message : 'Delete failed') @@ -44,60 +44,60 @@ export function DeleteUserModal({ user, onClose, onDeleted }: DeleteUserModalPro const handleModeChange = (next: DeleteMode) => { setMode(next) - if (next !== 'user_and_history') setConfirmText('') + if (next !== 'actor_and_history') setConfirmText('') setError(null) } - const confirmLabel = mode === 'user_and_history' ? 'Permanently delete' : 'Delete user' + const confirmLabel = mode === 'actor_and_history' ? 'Permanently delete' : 'Delete actor' return (
e.target === e.currentTarget && onClose()}>
-

Delete user

+

Delete actor

- You are about to delete {user.name}. Choose what to delete: + You are about to delete {actor.name}. Choose what to delete:

handleModeChange('user_only')} - onKeyDown={(e) => (e.key === ' ' || e.key === 'Enter') && handleModeChange('user_only')} + className={`${styles.radioCard} ${mode === 'actor_only' ? styles.radioCardSelectedA : ''}`} + onClick={() => handleModeChange('actor_only')} + onKeyDown={(e) => (e.key === ' ' || e.key === 'Enter') && handleModeChange('actor_only')} >
- {mode === 'user_only' ? '●' : '○'} - Delete user only + {mode === 'actor_only' ? '●' : '○'} + Delete actor only

- Revokes {user.name}'s login access. All session history and telemetry stays attributed to {user.name}. Reversible by re-creating a user with the same name. + Revokes {actor.name}'s login access. All session history and telemetry stays attributed to {actor.name}. Reversible by re-creating an actor with the same name.

handleModeChange('user_and_history')} - onKeyDown={(e) => (e.key === ' ' || e.key === 'Enter') && handleModeChange('user_and_history')} + className={`${styles.radioCard} ${mode === 'actor_and_history' ? styles.radioCardSelectedB : ''}`} + onClick={() => handleModeChange('actor_and_history')} + onKeyDown={(e) => (e.key === ' ' || e.key === 'Enter') && handleModeChange('actor_and_history')} >
- {mode === 'user_and_history' ? '●' : '○'} - Delete user + history + {mode === 'actor_and_history' ? '●' : '○'} + Delete actor + history ⚠ Permanent

- Revokes access and permanently removes {user.name} and all their telemetry. This cannot be undone. + Revokes access and permanently removes {actor.name} and all their telemetry. This cannot be undone.

- {mode === 'user_and_history' && ( + {mode === 'actor_and_history' && (
- + ) : ( )}
{showDelete && !isAnon && ( - setShowDelete(false)} onDeleted={() => { setShowDelete(false) - navigate('/users') + navigate('/actors') }} /> )} diff --git a/frontend/src/pages/Users.module.css b/frontend/src/pages/Actors.module.css similarity index 99% rename from frontend/src/pages/Users.module.css rename to frontend/src/pages/Actors.module.css index bf5866f..34bc940 100644 --- a/frontend/src/pages/Users.module.css +++ b/frontend/src/pages/Actors.module.css @@ -22,7 +22,7 @@ margin-bottom: var(--space-2); } -/* New user token banner */ +/* New actor token banner */ .newTokenBanner { background: var(--color-surface); border: 1px solid var(--color-accent); @@ -139,12 +139,12 @@ vertical-align: middle; } -.userName { +.actorName { font-weight: 500; color: var(--color-text-1); } -.userNameAnon { +.actorNameAnon { font-weight: 500; color: var(--color-text-2); font-style: italic; diff --git a/frontend/src/pages/Users.tsx b/frontend/src/pages/Actors.tsx similarity index 77% rename from frontend/src/pages/Users.tsx rename to frontend/src/pages/Actors.tsx index ea6bbb1..20ce6d3 100644 --- a/frontend/src/pages/Users.tsx +++ b/frontend/src/pages/Actors.tsx @@ -1,20 +1,20 @@ import { useState, useCallback, useEffect } from 'react' import { useNavigate } from 'react-router-dom' import { Copy, Check, Plus, Search, ChevronLeft, ChevronRight } from 'lucide-react' -import { useUsersPage, createUser } from '../api' -import type { User } from '../api' +import { useActorsPage, createActor } from '../api' +import type { Actor } from '../api' import { DataTable, EmptyState, ErrorState, LoadingSkeleton, SegmentedControl } from '../components' import type { Column, SortState } from '../components' import { RANGE_OPTIONS, RANGE_SUFFIX, useRangeCookie } from '../lib/range' -import styles from './Users.module.css' +import styles from './Actors.module.css' const ANON_ID = '__anonymous__' const PAGE_SIZE = 50 -const RANGE_COOKIE = 'cotel_users_range' +const RANGE_COOKIE = 'cotel_actors_range' type SortKey = 'name' | 'cost' | 'sessions' | 'created_at' | 'last_seen' -interface UserVM { +interface ActorVM { id: string name: string cost: number @@ -24,7 +24,7 @@ interface UserVM { isAnon: boolean } -function AddUserModal({ onClose, onCreated }: { onClose: () => void; onCreated: (u: User) => void }) { +function AddActorModal({ onClose, onCreated }: { onClose: () => void; onCreated: (u: Actor) => void }) { const [name, setName] = useState('') const [loading, setLoading] = useState(false) const [error, setError] = useState(null) @@ -36,7 +36,7 @@ function AddUserModal({ onClose, onCreated }: { onClose: () => void; onCreated: setLoading(true) setError(null) try { - const u = await createUser(trimmed) + const u = await createActor(trimmed) onCreated(u) } catch (err) { setError(err instanceof Error ? err.message : 'Create failed') @@ -48,7 +48,7 @@ function AddUserModal({ onClose, onCreated }: { onClose: () => void; onCreated: return (
e.target === e.currentTarget && onClose()}>
-

Add user

+

Add actor

void; onCreated: ) } -function NewTokenBanner({ user, onDismiss }: { user: User; onDismiss: () => void }) { +function NewTokenBanner({ actor, onDismiss }: { actor: Actor; onDismiss: () => void }) { const [copied, setCopied] = useState(false) const copy = useCallback(async () => { - await navigator.clipboard.writeText(user.token) + await navigator.clipboard.writeText(actor.token) setCopied(true) setTimeout(() => setCopied(false), 2000) - }, [user.token]) + }, [actor.token]) return (
- User {user.name} created — copy your token + Actor {actor.name} created — copy your token
- {user.token} + {actor.token}

- Set OTEL_EXPORTER_OTLP_HEADERS=Authorization=Bearer {user.token} in your Claude Code settings. + Set OTEL_EXPORTER_OTLP_HEADERS=Authorization=Bearer {actor.token} in your Claude Code settings.

) } -export default function Users() { +export default function Actors() { const navigate = useNavigate() const [range, setRange] = useRangeCookie(RANGE_COOKIE) @@ -109,7 +109,7 @@ export default function Users() { const [q, setQ] = useState('') const [showAdd, setShowAdd] = useState(false) - const [newUser, setNewUser] = useState(null) + const [newActor, setNewActor] = useState(null) // Debounce the search box into the server-side q parameter. useEffect(() => { @@ -120,7 +120,7 @@ export default function Users() { return () => clearTimeout(t) }, [search]) - const { data, error, isLoading, mutate } = useUsersPage({ + const { data, error, isLoading, mutate } = useActorsPage({ range, q: q || undefined, sort, @@ -130,7 +130,7 @@ export default function Users() { }) const total = data?.total ?? 0 - const rows: UserVM[] = (data?.users ?? []).map((u) => ({ + const rows: ActorVM[] = (data?.actors ?? []).map((u) => ({ id: u.id, name: u.name, cost: u.cost, @@ -148,25 +148,25 @@ export default function Users() { setPage(1) } - const handleSortChange = (next: NonNullable>) => { + const handleSortChange = (next: NonNullable>) => { setSort(next.key as SortKey) setOrder(next.dir) setPage(1) } - const handleCreated = (u: User) => { + const handleCreated = (u: Actor) => { setShowAdd(false) - setNewUser(u) + setNewActor(u) mutate() } - const columns: Column[] = [ + const columns: Column[] = [ { key: 'name', label: 'Name', sortable: true, render: (_v, row) => ( - {row.name} + {row.name} ), }, { @@ -206,7 +206,7 @@ export default function Users() { return (
-

Users

+

Actors

@@ -229,11 +229,11 @@ export default function Users() { />
- {newUser && setNewUser(null)} />} + {newActor && setNewActor(null)} />} {isLoading ? ( @@ -241,21 +241,21 @@ export default function Users() { ) : total === 0 ? ( q ? ( -
No users match "{q}"
+
No actors match "{q}"
) : ( ) ) : ( <> - + columns={columns} rows={rows} - sort={{ key: sort as keyof UserVM, dir: order }} + sort={{ key: sort as keyof ActorVM, dir: order }} onSortChange={handleSortChange} - onRowClick={(row) => navigate(`/users/${encodeURIComponent(row.id)}`)} + onRowClick={(row) => navigate(`/actors/${encodeURIComponent(row.id)}`)} /> {total > PAGE_SIZE && ( @@ -280,7 +280,7 @@ export default function Users() { )} - {showAdd && setShowAdd(false)} onCreated={handleCreated} />} + {showAdd && setShowAdd(false)} onCreated={handleCreated} />}
) } diff --git a/frontend/src/pages/Costs.module.css b/frontend/src/pages/Costs.module.css index c608636..6a270fa 100644 --- a/frontend/src/pages/Costs.module.css +++ b/frontend/src/pages/Costs.module.css @@ -14,7 +14,7 @@ margin-bottom: var(--space-2); } -.userChip { +.actorChip { display: inline-flex; align-items: center; gap: 4px; diff --git a/frontend/src/pages/Costs.tsx b/frontend/src/pages/Costs.tsx index 3c6eeb1..7b19fe4 100644 --- a/frontend/src/pages/Costs.tsx +++ b/frontend/src/pages/Costs.tsx @@ -24,15 +24,15 @@ export default function Costs() { const [from, setFrom] = useState(daysAgo(30)) const [to, setTo] = useState(today()) const [searchParams, setSearchParams] = useSearchParams() - const userId = searchParams.get('user_id') ?? undefined - const { data, error, isLoading } = useCosts(from, to, userId) + const actorId = searchParams.get('actor_id') ?? undefined + const { data, error, isLoading } = useCosts(from, to, actorId) const totalCost = data?.by_model.reduce((s, m) => s + m.cost_usd, 0) ?? 0 - function clearUserFilter() { + function clearActorFilter() { setSearchParams((prev) => { const next = new URLSearchParams(prev) - next.delete('user_id') + next.delete('actor_id') return next }) } @@ -42,9 +42,9 @@ export default function Costs() {

Costs

- {userId && ( - )}
diff --git a/frontend/src/pages/History.module.css b/frontend/src/pages/History.module.css index 680d7f7..ede0381 100644 --- a/frontend/src/pages/History.module.css +++ b/frontend/src/pages/History.module.css @@ -14,7 +14,7 @@ margin-bottom: var(--space-2); } -.userChip { +.actorChip { display: inline-flex; align-items: center; gap: 4px; diff --git a/frontend/src/pages/History.tsx b/frontend/src/pages/History.tsx index d53304f..1104733 100644 --- a/frontend/src/pages/History.tsx +++ b/frontend/src/pages/History.tsx @@ -308,13 +308,13 @@ export default function History() { const [to, setTo] = useState(today()) const [activePreset, setActivePreset] = useState('30d') const [searchParams, setSearchParams] = useSearchParams() - const userId = searchParams.get('user_id') ?? undefined - const { data, error, isLoading } = useHistory(gran, from, to, userId) + const actorId = searchParams.get('actor_id') ?? undefined + const { data, error, isLoading } = useHistory(gran, from, to, actorId) - function clearUserFilter() { + function clearActorFilter() { setSearchParams((prev) => { const next = new URLSearchParams(prev) - next.delete('user_id') + next.delete('actor_id') return next }) } @@ -338,9 +338,9 @@ export default function History() {

History

- {userId && ( - )}
diff --git a/frontend/src/pages/Models.module.css b/frontend/src/pages/Models.module.css index fdaa931..18383f4 100644 --- a/frontend/src/pages/Models.module.css +++ b/frontend/src/pages/Models.module.css @@ -12,7 +12,7 @@ color: var(--color-text-1); } -.userChip { +.actorChip { display: inline-flex; align-items: center; gap: 4px; diff --git a/frontend/src/pages/Models.tsx b/frontend/src/pages/Models.tsx index 0816bf5..6cd72c5 100644 --- a/frontend/src/pages/Models.tsx +++ b/frontend/src/pages/Models.tsx @@ -7,13 +7,13 @@ import styles from './Models.module.css' export default function Models() { const [searchParams, setSearchParams] = useSearchParams() - const userId = searchParams.get('user_id') ?? undefined - const { data, error, isLoading } = useModels(userId) + const actorId = searchParams.get('actor_id') ?? undefined + const { data, error, isLoading } = useModels(actorId) - function clearUserFilter() { + function clearActorFilter() { setSearchParams((prev) => { const next = new URLSearchParams(prev) - next.delete('user_id') + next.delete('actor_id') return next }) } @@ -29,9 +29,9 @@ export default function Models() {

Models

- {userId && ( - )}
diff --git a/frontend/src/pages/Overview.module.css b/frontend/src/pages/Overview.module.css index 148062b..edcf722 100644 --- a/frontend/src/pages/Overview.module.css +++ b/frontend/src/pages/Overview.module.css @@ -18,7 +18,7 @@ gap: var(--space-4); } -.userChip { +.actorChip { display: inline-flex; align-items: center; gap: 4px; @@ -95,7 +95,7 @@ color: var(--color-accent); } -.anonUser { +.anonActor { color: var(--color-text-2); font-style: italic; } diff --git a/frontend/src/pages/Overview.tsx b/frontend/src/pages/Overview.tsx index 8e7d88c..b44323a 100644 --- a/frontend/src/pages/Overview.tsx +++ b/frontend/src/pages/Overview.tsx @@ -5,9 +5,9 @@ import { XAxis, YAxis, Tooltip, ResponsiveContainer, } from 'recharts' import { - useOverview, useSessions, useHistory, useTools, useModels, useUsersPage, + useOverview, useSessions, useHistory, useTools, useModels, useActorsPage, } from '../api' -import type { SessionItem, ToolItem, ModelItem, User } from '../api' +import type { SessionItem, ToolItem, ModelItem, Actor } from '../api' import { KpiCard, DataTable, EmptyState, ErrorState, RefreshIndicator, SegmentedControl, KpiSkeleton, ChartSkeleton, LoadingSkeleton, sessionStatusBadge, failRateBadge, ChartTooltip, @@ -23,7 +23,7 @@ const ANON_ID = '__anonymous__' interface SectionProps { range: RangeKey - userId?: string + actorId?: string } function fmtTokens(n: number): string { @@ -38,9 +38,9 @@ function formatDay(iso: string): string { // ---- Compact section components ---- -function UsersSection({ range }: { range: RangeKey }) { +function ActorsSection({ range }: { range: RangeKey }) { const navigate = useNavigate() - const { data, isLoading, error } = useUsersPage({ + const { data, isLoading, error } = useActorsPage({ range, sort: 'cost', order: 'desc', @@ -50,17 +50,17 @@ function UsersSection({ range }: { range: RangeKey }) { if (isLoading && !data) return if (error) return - if (!data || data.users.length === 0) - return + if (!data || data.actors.length === 0) + return return ( - + columns={[ { key: 'name', label: 'Name', render: (v, row) => ( - {String(v)} + {String(v)} ), }, { key: 'cost', label: 'Cost', render: (v) => `$${Number(v).toFixed(2)}` }, @@ -71,8 +71,8 @@ function UsersSection({ range }: { range: RangeKey }) { render: (v) => (v ? new Date(String(v)).toLocaleString() : '—'), }, ]} - rows={data.users} - onRowClick={(row) => navigate(`/users/${encodeURIComponent(row.id)}`)} + rows={data.actors} + onRowClick={(row) => navigate(`/actors/${encodeURIComponent(row.id)}`)} /> ) } @@ -81,12 +81,12 @@ function UsersSection({ range }: { range: RangeKey }) { // days over a year, ten minutes over a day. On the year and all ranges that is // the same request ActivitySection already makes, and SWR serves both from one // fetch. -function ActivityGridSection({ range, userId }: SectionProps) { +function ActivityGridSection({ range, actorId }: SectionProps) { const { data, isLoading, error } = useHistory( GRID_GRANULARITY[range], undefined, undefined, - userId, + actorId, range, ) @@ -109,15 +109,15 @@ function ActivityGridSection({ range, userId }: SectionProps) { } // Spans and cost share the history buckets, so the two series are bucketed, -// windowed and user-filtered identically — a client-side join of /history with +// windowed and actor-filtered identically — a client-side join of /history with // /costs could not guarantee that. They carry different units, so each keeps its // own axis and the legend names the side it reads against. -function ActivitySection({ range, userId }: SectionProps) { +function ActivitySection({ range, actorId }: SectionProps) { const { data, isLoading, error } = useHistory( range === 'day' ? 'hour' : 'day', undefined, undefined, - userId, + actorId, range, ) @@ -211,14 +211,14 @@ function ActivitySection({ range, userId }: SectionProps) { ) } -function ToolsSection({ range, userId }: SectionProps) { +function ToolsSection({ range, actorId }: SectionProps) { const { data, isLoading, error } = useTools({ range, sort: 'calls', order: 'desc', page: 1, limit: 5, - user_id: userId, + actor_id: actorId, }) if (isLoading && !data) return @@ -249,8 +249,8 @@ function ToolsSection({ range, userId }: SectionProps) { ) } -function ModelsSection({ range, userId }: SectionProps) { - const { data, isLoading, error } = useModels(userId, range) +function ModelsSection({ range, actorId }: SectionProps) { + const { data, isLoading, error } = useModels(actorId, range) const rows = useMemo(() => { if (!data) return [] @@ -305,9 +305,9 @@ function ModelsSection({ range, userId }: SectionProps) { ) } -function SessionsSection({ range, userId }: SectionProps) { +function SessionsSection({ range, actorId }: SectionProps) { const navigate = useNavigate() - const { data, isLoading, error } = useSessions(1, 5, 'start_time', 'desc', userId, range) + const { data, isLoading, error } = useSessions(1, 5, 'start_time', 'desc', actorId, range) if (isLoading && !data) return if (error) return @@ -328,10 +328,10 @@ function SessionsSection({ range, userId }: SectionProps) { ), }, { - key: 'user_id', - label: 'User', + key: 'actor_id', + label: 'Actor', render: (v) => - v ? String(v) : anonymous, + v ? String(v) : anonymous, }, { key: 'first_seen', @@ -369,19 +369,19 @@ function SessionsSection({ range, userId }: SectionProps) { export default function Overview() { const [paused, setPaused] = useState(false) const [searchParams, setSearchParams] = useSearchParams() - const userId = searchParams.get('user_id') ?? undefined + const actorId = searchParams.get('actor_id') ?? undefined const [range, setRange] = useRangeCookie(RANGE_COOKIE) - const { data, error, isLoading, isValidating, mutate } = useOverview(paused ? 0 : 30_000, userId, range) + const { data, error, isLoading, isValidating, mutate } = useOverview(paused ? 0 : 30_000, actorId, range) - const userParam = userId ? `?user_id=${encodeURIComponent(userId)}` : '' + const actorParam = actorId ? `?actor_id=${encodeURIComponent(actorId)}` : '' const suffix = RANGE_SUFFIX[range] const scoped = (label: string) => (suffix ? `${label} (${suffix})` : label) - const clearUserFilter = () => + const clearActorFilter = () => setSearchParams((prev) => { const next = new URLSearchParams(prev) - next.delete('user_id') + next.delete('actor_id') return next }) @@ -390,9 +390,9 @@ export default function Overview() {

Overview

- {userId && ( - )}
@@ -421,43 +421,43 @@ export default function Overview() { ) : data ? (
- +
) : null} - - + + - {!userId && ( - - + {!actorId && ( + + )} - + - - + + - - + + - - + +
) diff --git a/frontend/src/pages/Sessions.module.css b/frontend/src/pages/Sessions.module.css index 19ffdbc..456586e 100644 --- a/frontend/src/pages/Sessions.module.css +++ b/frontend/src/pages/Sessions.module.css @@ -12,7 +12,7 @@ color: var(--color-text-1); } -.userChip { +.actorChip { display: inline-flex; align-items: center; gap: 4px; @@ -85,7 +85,7 @@ color: var(--color-accent); } -.userLink { +.actorLink { background: none; border: none; padding: 0; @@ -96,11 +96,11 @@ text-align: left; } -.userLink:hover { +.actorLink:hover { text-decoration: underline; } -.anonUser { +.anonActor { color: var(--color-text-2); font-style: italic; } diff --git a/frontend/src/pages/Sessions.tsx b/frontend/src/pages/Sessions.tsx index 191f789..353414b 100644 --- a/frontend/src/pages/Sessions.tsx +++ b/frontend/src/pages/Sessions.tsx @@ -11,21 +11,21 @@ export default function Sessions() { const [statusFilter, setStatusFilter] = useState<'all' | 'ok' | 'error'>('all') const navigate = useNavigate() const [searchParams, setSearchParams] = useSearchParams() - const userId = searchParams.get('user_id') ?? undefined - const { data, error, isLoading } = useSessions(page, 50, 'start_time', 'desc', userId) + const actorId = searchParams.get('actor_id') ?? undefined + const { data, error, isLoading } = useSessions(page, 50, 'start_time', 'desc', actorId) - function clearUserFilter() { + function clearActorFilter() { setSearchParams((prev) => { const next = new URLSearchParams(prev) - next.delete('user_id') + next.delete('actor_id') return next }) } - function applyUserFilter(uid: string) { + function applyActorFilter(uid: string) { setSearchParams((prev) => { const next = new URLSearchParams(prev) - next.set('user_id', uid) + next.set('actor_id', uid) return next }) setPage(1) @@ -36,7 +36,7 @@ export default function Sessions() { const needle = search.toLowerCase() return data.items.filter( (s) => - (search === '' || (s.user_id ?? '').toLowerCase().includes(needle)) && + (search === '' || (s.actor_id ?? '').toLowerCase().includes(needle)) && (statusFilter === 'all' || s.status === statusFilter), ) }, [data, search, statusFilter]) @@ -47,9 +47,9 @@ export default function Sessions() {

Sessions

- {userId && ( - )}
@@ -57,7 +57,7 @@ export default function Sessions() {
{ setSearch(e.target.value); setPage(1) }} /> @@ -99,20 +99,20 @@ export default function Sessions() { ), }, { - key: 'user_id', - label: 'User', + key: 'actor_id', + label: 'Actor', sortable: true, render: (v) => v ? ( ) : ( - anonymous + anonymous ), }, { diff --git a/frontend/src/pages/Setup.tsx b/frontend/src/pages/Setup.tsx index e7e5c6b..f7a4a6a 100644 --- a/frontend/src/pages/Setup.tsx +++ b/frontend/src/pages/Setup.tsx @@ -169,11 +169,11 @@ function GettingStartedTab() {
    -
  1. Go to Users and click Add user.
  2. +
  3. Go to Actors and click Add actor.
  4. Copy the generated token.
  5. Add to Claude Code settings:
@@ -182,7 +182,7 @@ function GettingStartedTab() {
{OTLP_HEADERS_SNIPPET}

- Replace YOUR_TOKEN_HERE with the token shown after user creation. + Replace YOUR_TOKEN_HERE with the token shown after the actor is created.

To block unauthenticated sends, open the{' '} @@ -497,8 +497,8 @@ function SettingsTab() {

Allow anonymous OTLP
- When enabled, spans without an Authorization header are accepted and attributed to no user. - Disable to require every OTLP sender to present a valid user token. + When enabled, spans without an Authorization header are accepted and attributed to no actor. + Disable to require every OTLP sender to present a valid actor token.