Skip to content

fix: finalize circuit-broken agent runs through usage accounting#4178

Open
orange-dot wants to merge 9 commits intosuperplanehq:mainfrom
orange-dot:upstream/fix-agent-circuit-break-usage-finalization
Open

fix: finalize circuit-broken agent runs through usage accounting#4178
orange-dot wants to merge 9 commits intosuperplanehq:mainfrom
orange-dot:upstream/fix-agent-circuit-break-usage-finalization

Conversation

@orange-dot
Copy link
Copy Markdown

Summary

This fixes the circuit-break finalization path so agent runs record and publish usage through the same run-level accounting surface used by the normal completion path.

Why

Circuit-broken runs can terminate after real model usage has already accumulated. If that path does not finalize through the same run-level accounting source as normal completion, the recorded totals can drift from the actual run state.

What changed

  • read usage from agent_run.usage() in the web agent flow
  • use that run-level usage in the circuit-break finalization path
  • align the normal completion path to the same source for consistency
  • keep the supporting loop-guard and discovery-tool behavior changes in the same PR because they belong to the same runtime-correctness surface
  • add focused regression coverage for the circuit-break accounting path

Scope guardrails

  • no schema or API changes
  • no protocol or wire-format changes
  • limited to agent runtime finalization/accounting behavior and the directly related guard behavior needed to exercise it cleanly

Validation

  • isolated agents_test DB recreate plus agent migrations
  • docker compose -f docker-compose.dev.yml run --rm -e DB_NAME=agents_test agent uv run --group dev python -m pytest tests/test_agent_http.py tests/test_tool_loop_guard.py tests/test_list_integration_resources.py
  • docker compose -f docker-compose.dev.yml run --rm -e DB_NAME=agents_test agent uv run --group dev python -m pytest tests

Observed results:

  • focused regression suite passed: 12 passed
  • full agent suite passed: 172 passed
  • the signed-off single-commit snapshot was rerun as-is after metadata cleanup

Request for review

Would appreciate a close look at whether this keeps the accounting/finalization contract tight enough around circuit-broken runs without widening scope beyond the runtime paths that actually participate in usage publication.

Signed-off-by: Bojan Janjatović <bojan.janjatovic@mamut-studio.com>
@superplanehq-integration
Copy link
Copy Markdown

👋 Commands for maintainers:

  • /sp start - Start an ephemeral machine (takes ~30s)
  • /sp stop - Stop a running machine (auto-executed on pr close)

Signed-off-by: Bojan Janjatović <bojan.janjatovic@mamut-studio.com>
@orange-dot orange-dot force-pushed the upstream/fix-agent-circuit-break-usage-finalization branch from d37d65c to 3038dcc Compare April 16, 2026 13:49
@orange-dot orange-dot marked this pull request as draft April 16, 2026 17:52
Copy link
Copy Markdown
Contributor

@cursor cursor bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Cursor Bugbot has reviewed your changes and found 1 potential issue.

Fix All in Cursor

❌ Bugbot Autofix is OFF. To automatically fix reported issues with cloud agents, enable autofix in the Cursor dashboard.

Reviewed by Cursor Bugbot for commit 4d3be70. Configure here.

Comment thread agent/src/ai/web.py
Signed-off-by: Bojan Janjatović <bojan.janjatovic@mamut-studio.com>
Signed-off-by: Bojan Janjatović <bojan.janjatovic@mamut-studio.com>
@orange-dot orange-dot marked this pull request as ready for review April 16, 2026 19:39
@orange-dot
Copy link
Copy Markdown
Author

Observation from local follow-up on April 16,
2026:

The public Semaphore run failed on
TestSendEmailComponent/ running_send_email_in_a_canvas_flow with timed out waiting for execution of node Send Email.

I ran the same targeted test locally in an
isolated compose setup against:

  • PR head
    fb75cd0e710847523b406cf3dfe274e08eb645be
  • PR merge ref
    e734a2d5724d0b1ea9fd212692b7437c45df59ab
  • local main baseline
    62671f252985284d6d468e07a10b58d05fe5fd86

All three passed locally.

Not claiming the PR is clean from this alone,
only noting that I could not reproduce the
current Semaphore Send Email failure locally
on either the head or merge snapshot.

Signed-off-by: Bojan Janjatović <bojan.janjatovic@mamut-studio.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant