Skip to content

fix(core): record cancelled attempts that race stream open - #3681

Merged
bobleer merged 2 commits into
openBitFun:mainfrom
bobleer:bob/fix-cancellation-ledger-flake
Nov 2, 2026
Merged

bobleer merged 2 commits into
openBitFun:mainfrom
bobleer:bob/fix-cancellation-ledger-flake

Conversation

@bobleer

@bobleer bobleer commented Nov 2, 2026

Copy link
Copy Markdown
Collaborator

Summary

Fixes the intermittent Linux CI failure of agentic::execution::round_executor::tests::factory_backed_cancellation_releases_stream_without_cooldown_and_observes_revocation (timeout waiting for 2 ledger records; seen on #3668 and #3492). The cause is a production bug, not test isolation: a cancelled model attempt could skip its token-usage ledger record.

Fixes #

Type and Areas

Type: bug fix (CI flake root cause)

Areas: Rust core (openbitfun-core round executor / token usage ledger)

Motivation / Impact

Root cause. RoundExecutor awaits the provider send in tokio::select! { cancel, send_future }. Cancellation in that select records a Cancelled attempt, and so does cancellation during stream processing. But if both branches are ready at the same poll, select! picks one at random. When it picks send_future, execution reaches the "check cancellation before stream processing" branch, which returned Cancelled without calling record_attempt_usage. The provider had already accepted that request, so it was billable, and its ledger record was lost.

The test cancels once the fixture server reports opened_streams == 1, which is set after the response head is written. On a fast machine the round usually reaches stream processing first. Under the load of the 3147-test CI run, the round task often isn't polled between the head arriving and the cancel. That leaves a 50/50 select! race, and losing it leaves one record instead of two. The 5 s wait then times out. Raising the timeout would not help: the record is never written.

I ruled out the other suspects. The ledger writer is a per-call tokio::spawn on the test's own runtime, with no global queue, debounce, or batch timer. The writes are serialized by per-path locks and flock. The test uses its own TokenUsageService in a temp dir.

Fix. The post-open cancellation branch now records the attempt as Cancelled with unknown usage, the same as the mid-send and mid-stream branches. Users now get a ledger entry for every cancelled request that reached the provider.

Verification

  • New regression test cancellation_racing_stream_open_still_records_the_sent_attempt: it delays the response head, pauses polling until the head is buffered, cancels, then resumes (16 iterations). Without the fix it fails (instrumentation confirmed it took the post-open path and left no record). With the fix it passes.
  • Both tests looped 48 times under 8-way parallel load: 48/48 pass.
  • cargo test --locked -p openbitfun-core --features product-full --lib round_executor: 68 passed
  • cargo test --locked -p openbitfun-core --features product-full --lib token_usage: 12 passed
  • cargo test --locked -p openbitfun-core --features product-full --lib: 3141 passed, 0 failed (macOS)
  • pnpm run fmt:rs

Remote scenarios: not exercised. The change is local to the core round executor and is the same for every host.

Reviewer Notes

No persisted-shape change. The record uses the existing ModelRequestOutcome::Cancelled attempt shape already written by the other cancellation paths.

Checklist

  • This PR is focused and does not include secrets, temporary prompts, generated scratch files, or unrelated artifacts.
  • Relevant verification is recorded above, or skipped checks are explained.
  • User-facing strings, docs, and locales are updated where applicable. (none needed)

A model attempt cancelled after the provider returned response headers
but before stream processing started returned Cancelled without writing
its ledger record. tokio::select! picks randomly when both the send and
cancel branches are ready, so under CI load the factory-backed
cancellation test intermittently found only one of two records.

Record the attempt as Cancelled on the post-open path, matching the
mid-send and mid-stream cancellation paths, and add a regression test
that pauses polling until the response head is buffered.
@bobleer
bobleer merged commit e580fe3 into openBitFun:main Nov 2, 2026
13 checks passed
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