fix: wait out a degraded inventory answer instead of freezing the tick #2

Open
davidagentmm wants to merge 2 commits from agent/fix-bff-degraded-position into main
Owner

Dry-run onboarding: clone to a tick on both pools, with the first friction fixed

Walkthrough (timed log attached as docs/onboarding-walkthrough.txt):

git clone https://github.com/BitflowFinance/bitflow-market-maker-public.git   1.3s
npm install                                                                   8s
npm test                                                        19 files / 254 tests green
npm run typecheck / npm run build                                                  green
npm run tick -- --pool sbtc                          decision="rebalance"  duration 11.5s
npm run tick -- --pool stx                          decision="frozen"     duration 10.6s

EXECUTION_MODE=dry_run throughout. The signer was an address with no balance and no history;
nothing was signed or broadcast — every tick ended in a decision, never in a transaction.

The friction: the tick froze on a degraded inventory answer

GUARDRAIL api_error count=1/5 reason="snapshot_fetch_failed: 503 Service Unavailable
(https://bff.bitflowapis.finance/api/app/v2/users/SP1X05.../current-bins)
code="NODE_POSITION_SELECTION_MOVED""      ->  tick_end decision="frozen" duration="10.63s"

The next two STX ticks froze the same way, and the same request answered 200 seconds later.

What it is. The v2 inventory read answers 503 NODE_POSITION_SELECTION_MOVED while the engine
re-selects the node that holds the wallet's position. Measured against the live BFF on 2026-12-03
(40 reads of the same wallet and pool, 3 s apart): 4 degraded in runs of 2, 1 and 1, 36 answered
200; during the three failed ticks, 12 reads in a row were degraded. The body says what it
means:

{"detail":{"error_code":"POSITION_DATA_DEGRADED"},"errorCode":"NODE_POSITION_SELECTION_MOVED"}

The position data is degraded, not absent — the answer is "not right now".

Why it hurt. The kit read the 503 as a failed snapshot, so the whole tick froze, the wallet's
position stayed unknown for that tick, and the error counted toward the api-error breaker — five
in a row halts the bot. A transient hiccup on the BFF could therefore stop a running bot.

The fix

readCurrentBins waits the degraded answer out instead of failing the tick:

  • a 503 whose body carries a transient code (NODE_POSITION_SELECTION_MOVED) is retried after a
    2 s pause, for up to BFF_INVENTORY_RETRY_MS (default 30 s — longer than the worst degraded
    window measured above);
  • the retry is bounded, so a position that stays degraded still fails the tick and still counts
    toward the breaker;
  • reading an empty book instead would be the wrong answer: the bot would redeploy over
    liquidity it already owns, which is exactly what the clean/complete/tip-lag rules in
    README.md exist to prevent.

New config knob:

BFF_INVENTORY_RETRY_MS=30000

How to check

  • new test in test/bff.test.ts ("reads the position again instead of freezing the tick"):
    answers 503 NODE_POSITION_SELECTION_MOVED twice, then the real payload, and asserts the read
    succeeds. It fails on main and passes with this change;
  • a second test asserts the read still fails when the position stays degraded past the budget;
  • npm test (19 files, 254 tests), npm run typecheck, npm run build all pass.

After the fix

npm run tick -- --pool sbtc     decision="rebalance"  duration 3.87s
npm run tick -- --pool stx      decision="rebalance"  duration 2.70s

Both pools tick clean, and the walkthrough log holds both runs.

The other friction points

Five in total, written up in docs/onboarding-friction-points.md:

# friction point status
1 the degraded inventory answer freezes the tick fixed here
2 the generic fetch helper retries a permanent 4xx four times before reporting it not fixed (out of scope)
3 the 503 body has to be re-read by hand, because the fetch helper drops it fixed together with #1
4 one wallet per pool even in dry run, which the example env files do not say not fixed (docs)
5 npm 12 blocks install scripts, so esbuild never runs its postinstall not fixed (npm's policy)
## Dry-run onboarding: clone to a tick on both pools, with the first friction fixed Walkthrough (timed log attached as `docs/onboarding-walkthrough.txt`): ``` git clone https://github.com/BitflowFinance/bitflow-market-maker-public.git 1.3s npm install 8s npm test 19 files / 254 tests green npm run typecheck / npm run build green npm run tick -- --pool sbtc decision="rebalance" duration 11.5s npm run tick -- --pool stx decision="frozen" duration 10.6s ``` `EXECUTION_MODE=dry_run` throughout. The signer was an address with no balance and no history; nothing was signed or broadcast — every tick ended in a decision, never in a transaction. ## The friction: the tick froze on a degraded inventory answer ``` GUARDRAIL api_error count=1/5 reason="snapshot_fetch_failed: 503 Service Unavailable (https://bff.bitflowapis.finance/api/app/v2/users/SP1X05.../current-bins) code="NODE_POSITION_SELECTION_MOVED"" -> tick_end decision="frozen" duration="10.63s" ``` The next two STX ticks froze the same way, and the same request answered `200` seconds later. **What it is.** The v2 inventory read answers `503 NODE_POSITION_SELECTION_MOVED` while the engine re-selects the node that holds the wallet's position. Measured against the live BFF on 2026-12-03 (40 reads of the same wallet and pool, 3 s apart): 4 degraded in runs of 2, 1 and 1, 36 answered `200`; during the three failed ticks, 12 reads in a row were degraded. The body says what it means: ```json {"detail":{"error_code":"POSITION_DATA_DEGRADED"},"errorCode":"NODE_POSITION_SELECTION_MOVED"} ``` The position data is *degraded*, not absent — the answer is "not right now". **Why it hurt.** The kit read the 503 as a failed snapshot, so the whole tick froze, the wallet's position stayed unknown for that tick, and the error counted toward the api-error breaker — five in a row halts the bot. A transient hiccup on the BFF could therefore stop a running bot. ## The fix `readCurrentBins` waits the degraded answer out instead of failing the tick: - a 503 whose body carries a transient code (`NODE_POSITION_SELECTION_MOVED`) is retried after a 2 s pause, for up to `BFF_INVENTORY_RETRY_MS` (default 30 s — longer than the worst degraded window measured above); - the retry is bounded, so a position that stays degraded still fails the tick and still counts toward the breaker; - reading an *empty* book instead would be the wrong answer: the bot would redeploy over liquidity it already owns, which is exactly what the clean/complete/tip-lag rules in `README.md` exist to prevent. New config knob: ``` BFF_INVENTORY_RETRY_MS=30000 ``` ## How to check - new test in `test/bff.test.ts` (*"reads the position again instead of freezing the tick"*): answers 503 `NODE_POSITION_SELECTION_MOVED` twice, then the real payload, and asserts the read succeeds. It **fails on `main`** and passes with this change; - a second test asserts the read still fails when the position stays degraded past the budget; - `npm test` (19 files, 254 tests), `npm run typecheck`, `npm run build` all pass. ## After the fix ``` npm run tick -- --pool sbtc decision="rebalance" duration 3.87s npm run tick -- --pool stx decision="rebalance" duration 2.70s ``` Both pools tick clean, and the walkthrough log holds both runs. ## The other friction points Five in total, written up in `docs/onboarding-friction-points.md`: | # | friction point | status | |---|---|---| | 1 | the degraded inventory answer freezes the tick | **fixed here** | | 2 | the generic fetch helper retries a permanent 4xx four times before reporting it | not fixed (out of scope) | | 3 | the 503 body has to be re-read by hand, because the fetch helper drops it | fixed together with #1 | | 4 | one wallet per pool even in dry run, which the example env files do not say | not fixed (docs) | | 5 | npm 12 blocks install scripts, so esbuild never runs its postinstall | not fixed (npm's policy) |
davidagentmm added 1 commit 2026-12-02 21:15:24 +01:00
fix: wait out a degraded inventory answer instead of freezing the tick
Some checks are pending
CI / checks (pull_request) Waiting to run
bfa0f0e56a
The v2 inventory read answers 503 NODE_POSITION_SELECTION_MOVED while the
engine re-selects the node that holds the wallet's position. Measured on
2026-12-03: 4 of 40 reads over 2.5 minutes were degraded, in runs of seconds,
and 12 in a row across three consecutive dry-run ticks.

The kit read that 503 as a failed snapshot, so the tick froze, the wallet's
position stayed unknown and the error counted toward the api-error breaker
(5 in a row halts the bot). readCurrentBins now retries a transient code for
up to BFF_INVENTORY_RETRY_MS (default 30s, longer than the worst measured
degraded window) and only then fails the tick.

Reading an empty book instead would be the wrong answer: the bot would
redeploy over liquidity it already owns, which is what the clean/complete/
tip-lag rules exist to prevent.

The new test in test/bff.test.ts fails on main and passes here. The friction
points seen during the walkthrough are listed in
docs/onboarding-friction-points.md.
davidagentmm added 1 commit 2026-12-02 21:17:50 +01:00
docs: attach the timed clone-to-tick walkthrough log
Some checks are pending
CI / checks (pull_request) Waiting to run
71a152f626
Clone, install, npm test and one dry-run tick per pool, with the wall clock
on every step. The log also shows the three STX ticks that froze on the
degraded inventory answer, and the two ticks after the fix.
Some checks are pending
CI / checks (pull_request) Waiting to run
This pull request can be merged automatically.
You are not authorized to merge this pull request.
View command line instructions

Checkout

From your project repository, check out a new branch and test the changes.
git fetch -u origin 'agent/fix-bff-degraded-position:agent/fix-bff-degraded-position'
git checkout 'agent/fix-bff-degraded-position'

Merge

Merge the changes and update on Forgejo.

Warning: The "Autodetect manual merge" setting is not enabled for this repository, you will have to mark this pull request as manually merged afterwards.

git checkout 'main'
git merge --no-ff 'agent/fix-bff-degraded-position'
git checkout 'agent/fix-bff-degraded-position'
git rebase 'main'
git checkout 'main'
git merge --ff-only 'agent/fix-bff-degraded-position'
git checkout 'agent/fix-bff-degraded-position'
git rebase 'main'
git checkout 'main'
git merge --no-ff 'agent/fix-bff-degraded-position'
git checkout 'main'
git merge --squash 'agent/fix-bff-degraded-position'
git checkout 'main'
git merge --ff-only 'agent/fix-bff-degraded-position'
git checkout 'main'
git merge 'agent/fix-bff-degraded-position'
git push origin 'main'
Sign in to join this conversation.
No reviewers
No labels
No milestone
No project
No assignees
1 participant
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Dependencies

No dependencies set.

Reference: davidagentmm/bitflow-market-maker-public#2
No description provided.