fabro/lib/components/fabro-agent/src
Bryan Helmkamp 2b6fcdbb80
Bill subagent spend to stages and make live usage loss-proof
Stage billing previously counted only the parent session's own LLM
calls. SubAgentResult carried no usage, so when a stage completed, the
authoritative billing replaced the live event-accumulated total with a
main-session-only number and every child session's spend vanished. On a
real run this collapsed a ~$175 total to ~$10.

Fix, part 1 — child spend reaches the parent's totals:
- SubAgentSupervisor keeps a spend ledger (SessionSpend). The child
  runner records last_input_usage/last_input_cost after every
  generation, including failed turns.
- The parent session drains the ledger into its input totals before
  publishing them, so AgentApiBackend::record_input_usage bills the
  whole session tree. Nested subagents compose: grandchildren drain
  into the child, whose runner records into the parent.

Fix, part 2 — the live number rides the same data:
- Each session emits a monotonic lifetime counter of its own spend
  (session_spend on AssistantMessage, session_billing on agent.message
  events, spec'd in fabro-api.yaml).
- The projection keeps the latest counter per session at the run level
  and attributes only the delta to the stage a message belongs to, so
  dropped or replayed events can no longer lose or double-count spend,
  and sessions reused across stages (shared threads) bill each stage
  for exactly its own turns. Legacy events without the counter fall
  back to per-message summing.

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
2026-08-06 18:27:56 -04:00
..
profiles Merge pull request #694 from fabro-sh/feat/reusable-subagent-sessions 2026-08-01 10:21:41 -04:00
agent_profile.rs fix(agent): harden compaction reasoning budgets 2026-07-24 21:58:23 -04:00
apply_patch.lark refactor: organize crates into three layers 2026-07-23 17:59:34 -04:00
apply_patch.rs refactor: organize crates into three layers 2026-07-23 17:59:34 -04:00
cli.rs refactor: simplify subagent session reuse 2026-08-01 10:09:07 -04:00
compaction.rs refactor(model): rename Kimi provider to Moonshot 2026-07-31 09:00:52 -04:00
config.rs feat(agent): add Claude 5 profile 2026-07-25 13:15:57 -04:00
context_window.rs fix(agent): harden Kimi profile tool contracts 2026-07-24 22:05:48 -04:00
docker_sandbox.rs refactor: organize crates into three layers 2026-07-23 17:59:34 -04:00
error.rs fix(agent): harden compaction failure handling 2026-07-24 20:57:57 -04:00
event.rs refactor: organize crates into three layers 2026-07-23 17:59:34 -04:00
file_tracker.rs fix(agent): harden Kimi profile tool contracts 2026-07-24 22:05:48 -04:00
history.rs refactor(agent): simplify task reminder staging and test fixtures 2026-08-04 14:20:32 -04:00
lib.rs Bill subagent spend to stages and make live usage loss-proof 2026-08-06 18:27:56 -04:00
local_sandbox.rs refactor: organize crates into three layers 2026-07-23 17:59:34 -04:00
loop_detection.rs refactor: organize crates into three layers 2026-07-23 17:59:34 -04:00
mcp_integration.rs refactor: organize crates into three layers 2026-07-23 17:59:34 -04:00
memory.rs feat(agent): add Claude 5 profile 2026-07-25 13:15:57 -04:00
native_tool.rs refactor(agent): give Claude 5 subagent tools fabro canonical names 2026-07-26 07:57:42 -04:00
question_tools.rs refactor(agent): share one normalizer between the question tools 2026-07-26 08:04:24 -04:00
sandbox.rs Add context-sourced command stdin 2026-07-29 10:25:51 -04:00
session.rs Bill subagent spend to stages and make live usage loss-proof 2026-08-06 18:27:56 -04:00
skills.rs feat(agent): add Claude 5 profile 2026-07-25 13:15:57 -04:00
subagent.rs Bill subagent spend to stages and make live usage loss-proof 2026-08-06 18:27:56 -04:00
task_reminder.rs refactor: organize crates into three layers 2026-07-23 17:59:34 -04:00
test_support.rs refactor(agent): simplify task reminder staging and test fixtures 2026-08-04 14:20:32 -04:00
todo_runtime.rs fix(agent): correct background-agent notification delivery 2026-07-25 15:15:39 -04:00
todo_tools.rs feat(agent): add Claude 5 profile 2026-07-25 13:15:57 -04:00
tool_execution.rs fix(agent): remove the read-before-write guard 2026-07-25 14:53:38 -04:00
tool_permissions.rs refactor(agent): model built-in tools as an enum with per-profile vocabularies 2026-07-24 20:04:11 -04:00
tool_registry.rs fix(agent): simplify GPT-5.6 tool routing 2026-07-25 11:49:40 -04:00
tools.rs Simplify stdin_source plumbing after review 2026-07-29 11:14:48 -04:00
truncation.rs fix(agent): harden Kimi profile tool contracts 2026-07-24 22:05:48 -04:00
types.rs Bill subagent spend to stages and make live usage loss-proof 2026-08-06 18:27:56 -04:00