Skip to content

fix(bots): refresh account balances once a bot is archived - #257

Closed
carlito-hummingbot wants to merge 3 commits into
hummingbot:mainfrom
carlito-hummingbot:fix/refresh-balances-after-archive
Closed

carlito-hummingbot wants to merge 3 commits into
hummingbot:mainfrom
carlito-hummingbot:fix/refresh-balances-after-archive

Conversation

@carlito-hummingbot

@carlito-hummingbot carlito-hummingbot commented Nov 15, 2026 •

Copy link
Copy Markdown

What this fixes

POST /portfolio/state serves an in-memory snapshot (accounts_service.get_accounts_state())
that is only re-read by update_account_state_loop, every
account_update_interval minutes (default 5), or when a caller passes
refresh: true. refresh defaults to false.

stop-and-archive-bot frees the margin the bot was holding but touches nothing
in that snapshot, so immediately after an archive the portfolio still reports
the archived bot's funds as committed — which is what a dashboard renders, and
it reads as "the archive didn't take".

Measured, on a live box

bitget_perpetual USDT, quad-threat's E4 bot deployed then archived through
POST /bot-orchestration/stop-and-archive-bot/{bot_name}:

                                 refresh=false      refresh=true
while the bot was running        50.46641187        50.67436934     <- margin committed
immediately after the archive    50.46641187        50.67436934     <- STALE, pre-fix
after the fix, post-archive      50.67436934        50.67436934

Before the fix the unrefreshed read kept the committed figure until the next
5-minute loop. After it, the cached row already agrees with the exchange.

The change

The route's background task is wrapped so that, once
stop_and_archive_bot(...) returns, it calls
accounts_service.update_account_state(skip_gateway=True).

Best-effort by design: a failed balance refresh logs a warning and does not turn
a completed archive into an error. skip_gateway=True keeps it to CEX balances,
matching what the portfolio route does for a fast query.

Verification

Run the way CI runs it (pytest -q -m "not integration"), in the repo's
hummingbot-api conda env, against the PR head:

test/test_stop_and_archive_bot_name.py            6 passed
full suite                                        974 passed, 1 skipped, 1 failed

The one failure is pre-existing on main, verified by running it in a
worktree of origin/main with the same image:

test/test_backtest_offloading.py::test_a_backtest_past_its_budget_is_terminated_and_reported
  PR head: 1 failed   |   origin/main: 1 failed

It is a timing-sensitive backtest-supervisor test and is unrelated to this
change (which touches only routers/bot_orchestration.py).

Correction to an earlier revision of this description: it said this repo had
no test suite. That was wrong — it does, and the first version of this PR broke
one of its tests (test_endpoint_schedules_the_background_task_with_the_path_name),
which is what CI caught. The design was changed rather than the test being
adjusted away: the archive task is scheduled exactly as before and the refresh is
a second, ordered background task.

Why a second task rather than a wrapper

The first attempt wrapped stop_and_archive_bot in a closure that archived and
then refreshed. That changed the identity of the scheduled task, so a test
guarding "the endpoint hands the orchestrator the path parameter, unmodified"
failed — for a reason that had nothing to do with what it was guarding.

Starlette runs background tasks in order, awaiting each, so the refresh is
scheduled after the archive instead. The archive call is byte-identical to
main's, and the refresh is a separate unit with its own test
(test_a_failed_balance_refresh_does_not_fail_the_archive).

/portfolio/state serves an in-memory snapshot that only the background loop
refreshes, every account_update_interval minutes (default 5). Archiving a bot
frees the margin it was holding but touched nothing in that snapshot, so for up
to a loop interval the portfolio still reported those funds as committed - and
since `refresh` defaults to false, that is the figure a dashboard shows.

Measured on a live box, bitget_perpetual USDT:

    while running:  refresh=false 50.46641187   refresh=true 50.67436934
    after archive:  refresh=false 50.67436934   refresh=true 50.67436934

Before this change the post-archive unrefreshed read reported 50.46641187.
@greptile-apps

greptile-apps Bot commented Nov 15, 2026 •

Copy link
Copy Markdown

RetriggerView in GreptileConfidence Score: 5/5

[Medium impact] The PR appears safe to merge, with no outstanding findings.

Summary

Refreshes cached CEX balances after a bot is archived.

  • Archived bots refresh exchange balances before portfolio readers use the cached snapshot.

The previous endpoint-test finding is fixed: the archive remains the first queued task, with its original arguments. Changes since the previous review only reformat imports and introduce no new issues.

Reviews (3) · Last reviewed commit: "style: apply the repo's pinned isort (5...." · Reviewed by Greptile

Comment thread routers/bot_orchestration.py Resolved
The first version wrapped stop_and_archive_bot in a closure. That changed the
identity of the scheduled task, so the test pinning that the endpoint hands the
orchestrator the path parameter unmodified failed for a reason unrelated to what
it guards.

Starlette awaits background tasks in order, so the refresh is its own task after
the archive instead: the archive call stays exactly as it was, and
test_endpoint_schedules_the_background_task_with_the_path_name keeps its
original assertions with the count adjusted to two.
pre-commit rewrote the imports in both files. Import formatting only.
@carlito-hummingbot

Copy link
Copy Markdown
Author

Superseded by #258. The post-archive balance refresh is in that PR together with #252 and #255.

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