Spend telemetry: per-repo × account × week query, host column, triage/healer spend, price table #202

Closed
opened 2026-10-15 02:59:11 +00:00 by ben · 3 comments
Owner

Filed from the 2026-10-14 infrastructure review (Claude Code session on Ben's workstation; four parallel read-only review passes over the root harness). Line anchors are against main at 3e50274.

Symptom

spend (swarm-db.py:86-98) has account, model and cache columns and reaches repo via runs.repo, so the data exists for swarm and session runs. But: spend-weekly groups by week only (swarm-db.py:544-554), limit-lost by account only; run-triage.sh and heal.sh record NO spend (only session-runner.sh:617 and record-run-result.sh write); model is NULL on the aggregate path (record-run-result.sh:290); runs has no host column (host is an event row, run-swarm.sh:183); no price table, so no dollar figure. An operator cannot answer "what did project X cost this week on account A vs B, on which device" — the question per-device/per-project tokens exist to answer. Cost is the quiet killer in every Oct 2026 factory writeup.

Proposed change

  • runs.host column (additive); swarmdb_run_start fills it.
  • swarm-db.py spend-by --group repo,account,week[,host,model].
  • run-triage.sh and heal.sh: --output-format json on their claude calls + swarmdb_spend_from_result.
  • prices table keyed by model (input/output/cache per Mtok), seeded from swarm.config comments or a prices.conf; spend-by --usd.
  • fleet-tui Loss/Spend tab reads the new query.

Acceptance

  • tests/swarm-db-test.sh covers the grouped query and the usd join.
  • tests/run-triage-test.sh / tests/heal-test.sh assert a spend row per claude call (shimmed claude JSON).

depends_on

#200 (for the fp dimension; the rest is independent and may land first).

_Filed from the 2026-10-14 infrastructure review (Claude Code session on Ben's workstation; four parallel read-only review passes over the root harness). Line anchors are against `main` at 3e50274._ ## Symptom `spend` (`swarm-db.py:86-98`) has account, model and cache columns and reaches repo via `runs.repo`, so the data exists for swarm and session runs. But: `spend-weekly` groups by week only (`swarm-db.py:544-554`), `limit-lost` by account only; `run-triage.sh` and `heal.sh` record NO spend (only `session-runner.sh:617` and `record-run-result.sh` write); `model` is NULL on the aggregate path (`record-run-result.sh:290`); `runs` has no host column (host is an event row, `run-swarm.sh:183`); no price table, so no dollar figure. An operator cannot answer "what did project X cost this week on account A vs B, on which device" — the question per-device/per-project tokens exist to answer. Cost is the quiet killer in every Oct 2026 factory writeup. ## Proposed change - `runs.host` column (additive); `swarmdb_run_start` fills it. - `swarm-db.py spend-by --group repo,account,week[,host,model]`. - `run-triage.sh` and `heal.sh`: `--output-format json` on their claude calls + `swarmdb_spend_from_result`. - `prices` table keyed by model (input/output/cache per Mtok), seeded from `swarm.config` comments or a `prices.conf`; `spend-by --usd`. - fleet-tui Loss/Spend tab reads the new query. ## Acceptance - `tests/swarm-db-test.sh` covers the grouped query and the usd join. - `tests/run-triage-test.sh` / `tests/heal-test.sh` assert a spend row per claude call (shimmed claude JSON). ## depends_on #200 (for the fp dimension; the rest is independent and may land first).

This was generated by AI during triage.

Ruled by agent under ADR 0174 — veto anytime.

  • Ruling: ready-for-agent.
  • Why this is a two-way door: pure spend telemetry/attribution — an additive column, a grouped read query, recording spend for existing CLI calls, and a public-pricing lookup table. No secret provisioned, no spend authorized; the USD figure is a derived estimate from operator-maintained public pricing, not a spend authorization.
  • What proves it: the swarm-db test (grouped query + USD join) and triage/healer tests asserting a spend row per CLI call with a shimmed claude JSON.

Agent Brief

Category: enhancement

Summary: Make spend answerable per repo × account × week (× host, × model), record spend for the triage and healer CLI calls, add an operator-maintained price table to derive a USD figure, and surface it in the TUI.

Current behavior: Spend can only be grouped by week (or lost-capacity by account/week); there is no repo/host/model grouping, no dollar figure, triage and the healer record no spend at all, and the executing host is only an event row, not a column on the run. An operator cannot answer "what did project X cost this week on account A vs B, on which device."

Desired behavior: Add an additive host column to runs, filled at run start. Add a grouped spend query selectable over repo/account/week/host/model with an optional USD join against a model-keyed price table (input/output/cache per Mtok) seeded from operator config. Make the triage and healer CLI calls capture their usage (structured output) and record a spend row via the shared spend-from-result helper. The TUI Loss/Spend tab reads the new query.

Key interfaces:

  • runs gains an additive host column, populated at run start.
  • A grouped spend query: spend-by --group repo,account,week[,host,model] with optional --usd.
  • A prices table keyed by model (input/output/cache per Mtok), operator-seeded; USD is a derived estimate.
  • Triage and healer record one spend row per CLI call via the shared spend-from-result helper.

Acceptance criteria:

  • swarm-db test covers the grouped query and the USD join.
  • Triage and healer tests assert a spend row per CLI call (shimmed claude JSON).
  • The host column is populated at run start and selectable in the grouped query.
  • The TUI Loss/Spend tab reads the new query.

Out of scope: The token-fingerprint grouping dimension (depends on #200 and lands with it); any spend authorization or budget enforcement — this is read-only telemetry.

> *This was generated by AI during triage.* **Ruled by agent under ADR 0174 — veto anytime.** - **Ruling:** `ready-for-agent`. - **Why this is a two-way door:** pure spend telemetry/attribution — an additive column, a grouped read query, recording spend for existing CLI calls, and a public-pricing lookup table. No secret provisioned, no spend authorized; the USD figure is a derived estimate from operator-maintained public pricing, not a spend authorization. - **What proves it:** the swarm-db test (grouped query + USD join) and triage/healer tests asserting a spend row per CLI call with a shimmed `claude` JSON. ## Agent Brief **Category:** enhancement **Summary:** Make spend answerable per repo × account × week (× host, × model), record spend for the triage and healer CLI calls, add an operator-maintained price table to derive a USD figure, and surface it in the TUI. **Current behavior:** Spend can only be grouped by week (or lost-capacity by account/week); there is no repo/host/model grouping, no dollar figure, triage and the healer record no spend at all, and the executing host is only an event row, not a column on the run. An operator cannot answer "what did project X cost this week on account A vs B, on which device." **Desired behavior:** Add an additive host column to runs, filled at run start. Add a grouped spend query selectable over repo/account/week/host/model with an optional USD join against a model-keyed price table (input/output/cache per Mtok) seeded from operator config. Make the triage and healer CLI calls capture their usage (structured output) and record a spend row via the shared spend-from-result helper. The TUI Loss/Spend tab reads the new query. **Key interfaces:** - `runs` gains an additive `host` column, populated at run start. - A grouped spend query: `spend-by --group repo,account,week[,host,model]` with optional `--usd`. - A `prices` table keyed by model (input/output/cache per Mtok), operator-seeded; USD is a derived estimate. - Triage and healer record one spend row per CLI call via the shared spend-from-result helper. **Acceptance criteria:** - [ ] swarm-db test covers the grouped query and the USD join. - [ ] Triage and healer tests assert a spend row per CLI call (shimmed `claude` JSON). - [ ] The host column is populated at run start and selectable in the grouped query. - [ ] The TUI Loss/Spend tab reads the new query. **Out of scope:** The token-fingerprint grouping dimension (depends on #200 and lands with it); any spend authorization or budget enforcement — this is read-only telemetry.

swarm-claim host=matou-dev-server-01 run=21920
(automated multi-host claim — lowest live claim id works this ticket)

swarm-claim host=matou-dev-server-01 run=21920 (automated multi-host claim — lowest live claim id works this ticket)

close-report for #202 — status success

✅ close-report gates passed — every claim verified against main; issue closed.

envelope
{
  "issue": 202,
  "status": "success",
  "commits": [
    "1d591cc31ca4bc3b1ee404be2a8c13b990ad0308"
  ],
  "changed_files": [
    "STATUS.md",
    "docs/journal/2026-10-15-202.md",
    "fleet-tui/app.py",
    "fleet-tui/probe.py",
    "fleet-tui/tests/test_probe.py",
    "fleet-tui/tests/test_usage.py",
    "fleet-tui/usage.py",
    "heal.sh",
    "onboarding/vendor-manifest",
    "prices.conf",
    "run-swarm.sh",
    "run-triage.sh",
    "session-runner.sh",
    "swarm-db-lib.sh",
    "swarm-db.py",
    "swarm-db.sh",
    "swarm.config",
    "tests/heal-test.sh",
    "tests/run-triage-test.sh",
    "tests/swarm-db-test.sh"
  ],
  "tests": [
    {
      "command": "for t in tests/*.sh onboarding/tests/*.sh; do case \"$t\" in *fakebin*|*fixtures*) continue;; esac; bash \"$t\" || echo FAIL: $t; done",
      "exit_code": 0
    },
    {
      "command": "bash tests/swarm-db-test.sh",
      "exit_code": 0
    },
    {
      "command": "bash tests/run-triage-test.sh",
      "exit_code": 0
    },
    {
      "command": "bash tests/heal-test.sh",
      "exit_code": 0
    },
    {
      "command": "bash onboarding/tests/vendor-manifest-coverage-test.sh",
      "exit_code": 0
    },
    {
      "command": "bash tests/docs-budget-test.sh",
      "exit_code": 0
    },
    {
      "command": "python3 (manual, no pytest in sandbox) fleet-tui test_probe.py + test_usage.py new+existing assertions",
      "exit_code": 0
    }
  ],
  "criteria": {
    "swarm-db test covers the grouped query and the USD join": "tests/swarm-db-test.sh: new #202 blocks assert spend-by --group host/repo,account sums, unknown-dim refusal, and spend-by --usd pricing (priced $62.25, unpriced $0) + price-set/price-load; swarm-db.py q_spend_by + prices table + _USD_EXPR",
    "Triage and healer tests assert a spend row per CLI call (shimmed claude JSON)": "tests/run-triage-test.sh #202 block (shimmed JSON result → one spend row, tokens/model/account, tied to run); tests/heal-test.sh #202 block (json-stub → one spend row; limit stub → no row). run-triage.sh + heal.sh now call claude --output-format json and swarmdb_spend_from_result",
    "The host column is populated at run start and selectable in the grouped query": "swarm-db.py runs.host additive column + cmd_run_start --host; swarmdb_run_start 5th host arg (swarm-db-lib.sh) passed by run-swarm.sh/session-runner.sh/run-triage.sh/heal.sh; swarm-db-test.sh asserts host stamped + NULL-when-omitted + migration + spend-by --group host; run-triage/heal tests assert runs.host set",
    "The TUI Loss/Spend tab reads the new query": "fleet-tui/probe.py emits the prices section; fleet-tui/usage.py price_index/usd/usd_by_account price the per-model daily spend; fleet-tui/app.py usage_rows renders an ESTIMATED SPEND (week, USD) block; tests test_probe.py (prices section + absent-table degrade) and test_usage.py (USD pricing) verified manually (no pytest in sandbox)"
  },
  "summary": "Added runs.host, the spend-by grouped query, an operator price table + --usd estimate, triage/healer per-call spend recording, and a fleet-tui USD surface.",
  "reason": "",
  "blockers": [],
  "no_code_change": ""
}
**close-report** for #202 — status `success` :white_check_mark: **close-report gates passed** — every claim verified against main; issue closed. <details><summary>envelope</summary> ```json { "issue": 202, "status": "success", "commits": [ "1d591cc31ca4bc3b1ee404be2a8c13b990ad0308" ], "changed_files": [ "STATUS.md", "docs/journal/2026-10-15-202.md", "fleet-tui/app.py", "fleet-tui/probe.py", "fleet-tui/tests/test_probe.py", "fleet-tui/tests/test_usage.py", "fleet-tui/usage.py", "heal.sh", "onboarding/vendor-manifest", "prices.conf", "run-swarm.sh", "run-triage.sh", "session-runner.sh", "swarm-db-lib.sh", "swarm-db.py", "swarm-db.sh", "swarm.config", "tests/heal-test.sh", "tests/run-triage-test.sh", "tests/swarm-db-test.sh" ], "tests": [ { "command": "for t in tests/*.sh onboarding/tests/*.sh; do case \"$t\" in *fakebin*|*fixtures*) continue;; esac; bash \"$t\" || echo FAIL: $t; done", "exit_code": 0 }, { "command": "bash tests/swarm-db-test.sh", "exit_code": 0 }, { "command": "bash tests/run-triage-test.sh", "exit_code": 0 }, { "command": "bash tests/heal-test.sh", "exit_code": 0 }, { "command": "bash onboarding/tests/vendor-manifest-coverage-test.sh", "exit_code": 0 }, { "command": "bash tests/docs-budget-test.sh", "exit_code": 0 }, { "command": "python3 (manual, no pytest in sandbox) fleet-tui test_probe.py + test_usage.py new+existing assertions", "exit_code": 0 } ], "criteria": { "swarm-db test covers the grouped query and the USD join": "tests/swarm-db-test.sh: new #202 blocks assert spend-by --group host/repo,account sums, unknown-dim refusal, and spend-by --usd pricing (priced $62.25, unpriced $0) + price-set/price-load; swarm-db.py q_spend_by + prices table + _USD_EXPR", "Triage and healer tests assert a spend row per CLI call (shimmed claude JSON)": "tests/run-triage-test.sh #202 block (shimmed JSON result → one spend row, tokens/model/account, tied to run); tests/heal-test.sh #202 block (json-stub → one spend row; limit stub → no row). run-triage.sh + heal.sh now call claude --output-format json and swarmdb_spend_from_result", "The host column is populated at run start and selectable in the grouped query": "swarm-db.py runs.host additive column + cmd_run_start --host; swarmdb_run_start 5th host arg (swarm-db-lib.sh) passed by run-swarm.sh/session-runner.sh/run-triage.sh/heal.sh; swarm-db-test.sh asserts host stamped + NULL-when-omitted + migration + spend-by --group host; run-triage/heal tests assert runs.host set", "The TUI Loss/Spend tab reads the new query": "fleet-tui/probe.py emits the prices section; fleet-tui/usage.py price_index/usd/usd_by_account price the per-model daily spend; fleet-tui/app.py usage_rows renders an ESTIMATED SPEND (week, USD) block; tests test_probe.py (prices section + absent-table degrade) and test_usage.py (USD pricing) verified manually (no pytest in sandbox)" }, "summary": "Added runs.host, the spend-by grouped query, an operator price table + --usd estimate, triage/healer per-call spend recording, and a fleet-tui USD surface.", "reason": "", "blockers": [], "no_code_change": "" } ``` </details>
Sign in to join this conversation.
No milestone
No project
No assignees
2 participants
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Reference
Matou/dev-factory#202
No description provided.