Skip to content

bug(company-os): native AutoBot-agent runs are never charged to a budget (cost forwarding unwired) #20883

Closed

Description

@mrveiss

Found in review of #20222.

Problem

Native AutoBot-agent runs are never charged to any budget: no cost event, no budget counter update, and no BudgetExhausted hard stop (#8215, #15859) for this adapter.

Evidence

  • autobot-backend/llc/adapters/autobot_agent_adapter.py:380 _forward_cost returns early when self._budget_session_factory is None, before any ingest.
  • autobot-backend/llc/adapters/autobot_agent_adapter.py:171,194 the factory is an optional constructor argument defaulting to None.
  • autobot-backend/llc/scheduler/heartbeat_scheduler.py:846 the only non-test construction is AutoBotAgentAdapter(agent_config=adapter_config): no factory passed.
  • grep -rn "budget_session_factory" autobot-backend --include=*.py | grep -v /tests/ returns only the adapter itself (lines 171, 179, 194, 380, 414); nothing outside it supplies a value.

Second defect, same code

autobot-backend/llc/adapters/autobot_agent_adapter.py:414-415 does async with self._budget_session_factory() as session: then BudgetService().ingest_cost_event(...) and never commits. Even with a factory wired, a successful ingest rolls back on exit unless the factory itself commits. PR #20222 makes the hard-stop path persist by committing inside the service before raising; the success path is not covered.

Impact

  • No spend is recorded for native AutoBot-agent runs.
  • Budget limits cannot stop these runs, so the M2.6 goal (every LLM call records a cost event; over-budget agents stop) is not met for this adapter.

Fix direction

Wire the factory at the construction site in heartbeat_scheduler.py using a committing session scope (autobot_shared/db_session.async_session_scope, commit-on-exit, rollback-on-error), so the success path persists as well as the hard-stop path. Reuse the hosting service's session maker per db_session.py guidance.

Acceptance criteria

  • A native run's tokens produce a cost event and a budget counter update (test through the real heartbeat construction path, not an injected factory).
  • A run over the limit hard-stops with BudgetExhausted, and its spend persists after the run (test reads the counter from a fresh session).
  • The adapter no longer silently skips forwarding when no factory is configured in production wiring (guard or test asserting the scheduler passes one).

Activity

  1. github-actions commented on Nov 13, 2026

    @github-actions
    Contributor

    Landed via the merge train (#21454, merge 87819661d3) via #21434

  2. mrveiss commented on Nov 13, 2026

    @mrveiss
    OwnerAuthor

    Landed with merge train #21454 (8781966), member PR #21434. Checked against origin/main at 8781966, by reading code and tests; tests were not run here.

    Criterion Status Evidence
    A native run's tokens produce a cost event and a budget counter update (test through the real heartbeat construction path) partly delivered; counter update needs a real database read Construction path wired: llc/scheduler/heartbeat_scheduler.py:843 passes budget_session_factory=budget_session_scope (llc/scheduler/usage_billing.py:46, a committing scope). test_native_agent_cost_charged_20883.py test_run_with_usage_produces_one_committed_cost_event_for_the_agents_budget runs _dispatch_autobot_agent and asserts one ingest_cost_event and one commit, but the BudgetService and session are fakes, so no counter is read.
    A run over the limit hard-stops with BudgetExhausted, and its spend persists (test reads the counter from a fresh session) partly delivered; persistence needs a real database read test_hard_stop_still_stops_the_run asserts BudgetExhausted propagates. Persistence of the hard-stop spend rests on the service committing before it raises (#20202); no test here reads a counter from a fresh session.
    The adapter no longer silently skips forwarding when no factory is configured in production wiring delivered by code, ticked now heartbeat_scheduler.py:843; guard test_scheduler_construction_site_passes_a_budget_session_factory asserts the kwarg is callable

    Also delivered and not in the criteria: a lost non-budget charge is logged at ERROR with traceback and does not fail the run (autobot_agent_adapter.py:426; test_a_lost_charge_is_logged_at_error_with_traceback_and_does_not_fail_the_run). It is only logged, not recorded anywhere a user can see; follow-up #21432.

    Still open: the first two criteria, which need a counter read from a real database. The issue stays open.

  3. added a commit that references this issue on Nov 21, 2026
  4. mrveiss commented on Nov 21, 2026

    @mrveiss
    OwnerAuthor

    Closure evidence on origin/main after #21955 (merge 52a2fe50e). All ACs are ticked.

    • AC1. llc/tests/test_native_agent_cost_persists_20883.py:116 drives the real _dispatch_autobot_agent construction path with the real BudgetService on SQLite. A run under its limit records a cost event and updates the counter.
    • AC2. Same file, :124. A run over its limit hard-stops with BudgetExhausted, and its spend persists when read from a fresh session.
    • AC3 was already ticked. The scheduler passes the factory at llc/scheduler/heartbeat_scheduler.py:844 (budget_session_factory=budget_session_scope), and that scope commits.
  5. mrveiss commented on Nov 21, 2026

    @mrveiss
    OwnerAuthor

    Host evidence, local install on main d8cbd91925 (includes #21955), 2026-11-21. Read-only. No native AutoBot-agent heartbeat run happened between the restart (21:20 Riga) and the check: 0 runs for every visible agent, and the backend log since then shows only router and watchdog startup for cost and budget. The charge path therefore had nothing to charge, and this is not host proof. The cost routes report lifetime totals only. Proof needs a native run on the install, then a check of its cost event.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    Projects

    No projects

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions