Skip to content

fix(aimux): judge a header report by its instant in the report arm - #2150

Merged
yyhhyyyyyy merged 5 commits into
mainfrom
hui/aimux-report-cutoff
Nov 25, 2026
Merged

yyhhyyyyyy merged 5 commits into
mainfrom
hui/aimux-report-cutoff

Conversation

@yyhhyyyyyy

@yyhhyyyyyy yyhhyyyyyy commented Nov 24, 2026 •

Copy link
Copy Markdown
Collaborator

Refs #2117

Follow-up to #2143, which bounded the waits before the stream starts and judged a response-header report by the instant the transport produced it. The review of that change asked for the reported instant to be compared with the header cutoff on every completion path, so a report produced around the timer boundary is classified the same way regardless of poll order (#2143 (review)).

The boundary: the header allowance is a timer, and a timer fires on its next millisecond tick. For up to one tick after the allowance, the report arm could be polled while the deadline arm was still pending, so a report stamped after the allowance was accepted there and started a fresh stream window from its instant, while the same report read after the tick ended the wait as network naming missing headers. Which class a report got therefore depended on the poll order inside a sub-millisecond window.

The other completion paths are judged the same way. The two cancel arms outrank the report and do not read it. The deadline arm compares the report with the allowance. The report arm compares it too, and also covers the transport closing without a report, which has no instant to compare. The exchange arm is the one path that can run after the report arm found the receiver empty: a provider that returns at the headers (compatible, Bedrock, and xAI through the compatible provider) completes in the poll that publishes the report, and a provider that reads one event before returning can complete there as well when that event is already buffered. While the headers are still awaited, the exchange arm consumes a queued report and compares its instant with the allowance before it returns, so a late 200 does not start a stream and a fully buffered late non-2xx reply does not keep its status; an exchange failure with no queued report is returned unchanged. The waits after the headers, for the stream to start and between stream parts, have no producer-reported instant and keep the deadline-first order: an expired allowance still outranks a part that is ready at the same moment.

The report arm now compares the reported instant with the allowance and ends the wait as a network failure with the reason no response headers arrived within 300 seconds of sending the request, the kind and reason the deadline arm already used for a late report. The exchange arm makes the same comparison for a report it finds queued when it completes. The select order is unchanged, no timeout or constant is added, and a report inside the allowance keeps its behaviour, a non-2xx reply included. A non-2xx report written after the allowance ends the wait as missing headers whichever arm reads it, as the deadline arm already did when it read such a report; driver::tests::a_late_error_report_fails_as_a_network_failure writes a 401 at the boundary and pins that failure, its absent status included, and driver::tests::a_late_error_report_with_a_finished_body_fails_as_missing_headers pins a 401 whose body has already ended, where the exchange completes in the same poll. The model failure document now states both sides of the rule: headers written inside the allowance are on time when the driver reads them later, and headers written after the allowance are late when the driver reads them before the timer fires.

Known limitation: the instant a report carries is the instant the driver polls the completion of the fetch, not the instant the response headers reached the transport. ResponseWatch records it in the poll that returns from inner.fetch, and the FetchResponse it hands back carries no arrival instant from the connection below it. A report written inside the allowance and read by a later poll is still on time, because the instant it carries is inside the allowance. For a reply whose headers arrive inside the allowance, the wait ends as a network failure naming missing headers only when the task is not polled from that arrival until the allowance has passed, so no report is written inside it: the report then carries an instant after the allowance, and polling the exchange before the other arms changes nothing, because the instant it records is later than the allowance too. Closing that gap needs the arrival instant recorded by the transport, which is an upstream aimux change this change does not make.

On the old code the boundary is an assertion failure, not a timing artifact. driver::tests::a_header_report_written_after_its_allowance_is_late moves a paused clock half a tick off the grid, writes the report 0.2 ms after the allowance, and polls the task before the tick that ends it: the wait does not end as missing headers, and it must. The exchange arm is pinned from both sides: driver::tests::a_compatible_exchange_completing_after_its_allowance_is_late returns an OpenaiCompatible 200 in the poll that publishes a report written 0.2 ms after the allowance, without a second exchange poll, and driver::tests::a_late_error_report_with_a_finished_body_fails_as_missing_headers does the same for a 401 whose error body is already at EOF; both fail by assertion on the previous code. driver::tests::an_exchange_failure_without_a_report_keeps_its_error releases a connection failure after the allowance with no report and pins that its own kind and reason are returned. driver::tests::a_compatible_exchange_completing_at_the_allowance_starts pins a report written exactly at the allowance starting the reply.

Verification:

  • cargo test -p dim-agent-aimux --lib runs the driver suite, including the cases below, and the stream deadlines.
  • driver::tests::a_header_report_written_after_its_allowance_is_late covers a report written 0.2 ms after the allowance and read before the timer fires: the wait fails as network with the transport kind and the missing-headers reason; it fails by assertion without this change.
  • driver::tests::a_header_report_written_after_its_allowance_is_late_after_the_tick covers the same report read after the timer fires: the same failure, so both orders agree on its class.
  • driver::tests::a_header_report_written_at_the_allowance_stays_on_time covers a report written exactly at the allowance: the reply starts and delivers the text of its first event.
  • driver::tests::a_late_error_report_fails_as_a_network_failure covers a 401 written 0.2 ms after the allowance and read before the timer fires: the wait fails as network with the transport kind, the missing-headers reason and no status, the failure the deadline arm gives the same report.
  • driver::tests::a_compatible_exchange_completing_after_its_allowance_is_late covers an OpenaiCompatible 200 whose exchange completes in the poll that publishes a report written 0.2 ms after the allowance: no stream starts, and the wait fails as network with the transport kind and the missing-headers reason.
  • driver::tests::a_late_error_report_with_a_finished_body_fails_as_missing_headers covers a 401 whose error body is already at EOF, so the exchange fails in the poll that publishes its late report: the wait fails as missing headers with no status instead of keeping the HTTP class.
  • driver::tests::an_exchange_failure_without_a_report_keeps_its_error covers a connection failure released after the allowance with no report: the request fails with that failure's own kind and reason, not with missing headers.
  • driver::tests::a_compatible_exchange_completing_at_the_allowance_starts covers an OpenaiCompatible exchange that completes exactly at the allowance in one poll: the reply starts and delivers the text of its first event.
  • cargo test -p dim-agent-aimux --test agent_aimux_flow runs the model flow suite, including the response-header idle case.
  • cargo test -p dim-agent-capabilities --test capabilities_flow idle_timeout covers the in-place retry before any committed output and the turn ending after it.
  • cargo fmt --all -- --check --config error_on_line_overflow=true, cargo clippy --workspace --all-targets -- -D warnings and cargo run -p xtask -- check pass.

…ts allowance

The header allowance is a timer, so a report written after the allowance can still be read before the timer fires: the report arm accepts it and starts a fresh window from it, while the same report read after the tick ends the wait as missing headers. A virtual-time test writes the report 0.2 ms after the allowance with the clock half a tick off the grid, and a second one reads the same report after the tick; both must end the wait as missing headers. A third writes the report exactly at the allowance and must still start the reply.

The first fails on the current driver by assertion; the other two pin the orders the fix must keep.
The header allowance is a timer, and it fires on its next millisecond tick, so the report arm could read a report stamped after the allowance while its timer was still pending: the arm accepted the report and started a fresh stream window from it, while the same report read after the tick ended the wait as missing headers. The report arm now compares the allowed instant with the report and ends the wait as missing headers, so a report's class no longer depends on which arm reads it first. The select order is unchanged: the deadline still runs before the report and the exchange, and a part that is ready with an expired deadline still loses to it.

The model failure document states the two sides of the rule: a header written inside the allowance is on time when it is read later, and a header written after the allowance is late when it is read before the timer fires.
A report written after the header allowance must be classified the same way whichever arm reads it, and a non-2xx status must not change that class. This case writes a 401 at the boundary instant and asserts the missing-headers network failure the deadline arm gives the same report, with no status carried.

The classification comes from the previous commit, so this case pins it rather than reproducing an old failure.

@eric8810 eric8810 left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

[P2] Also check the header report when the exchange completes in the same poll (crates/dim-agent-aimux/src/driver.rs, result = &mut exchange). select! can poll the report receiver while it is still empty, then poll exchange; that poll can publish ResponseReport and return Ready without yielding. The exchange arm then returns success or a provider error without checking the now-available timestamp. The pinned OpenAI-compatible provider returns its StreamResult at the headers, so a late 200 in the same sub-millisecond window can still start a stream. A fully buffered late error response can similarly keep its HTTP class, unlike the same response with a pending body. It is the stream completion itself that lacks a timestamp, not the header report already queued by that completion. While still awaiting headers, consume and check any queued report before returning the exchange result, preserving genuine failures with no report. Please add an OpenaiCompatible late-200 case that returns without a second exchange poll; the current tests deliberately make the exchange pending after publishing the report and therefore only cover the other two arms.

The timestamp comparison in the two report-reading arms is consistent, including equality at the cutoff. Static review at 134b9f4d; Linux CI was in progress.

@zerob13 zerob13 left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

I confirmed the remaining gap in the exchange completion arm at 134b9f4d. The receiver can be empty when polled, then the exchange can publish the header report and finish in the same poll. That arm still returns the stream or provider error without judging the queued receipt timestamp. Please handle the queued report there and cover a compatible-provider exchange that completes in the same poll, as the existing review requests. The hosted CI gate passed. Static review only.

The exchange can publish the header report and finish in the same poll, after the report arm was polled while the channel was still empty: the compatible provider returns its stream at the headers, and a non-2xx body that has already ended lets the exchange fail in that poll. Pin a late report in both cases, an exchange failure with no report keeping its own classification, and a report written exactly at the allowance still starting the reply.
The exchange can publish the header report and finish in the same poll, after the report arm saw an empty channel, so a late report could start a stream or keep the HTTP status of a non-2xx reply. While the headers are still awaited, consume a queued report in the exchange arm and judge it by the instant the transport reported it, with the comparison the deadline and report arms use; an exchange failure with no report is returned unchanged. The select order is unchanged.
@yyhhyyyyyy

Copy link
Copy Markdown
Collaborator Author

Fixed in 5e40aa4. While the driver is still awaiting headers, the exchange arm now consumes a queued response report before it returns and compares the report's instant with the header allowance, using the same comparison as the deadline and report arms. A report written after the allowance ends the wait as a network failure naming missing headers, and an exchange failure with no queued report is returned unchanged. The select order is unchanged and no timer or constant is added.

The two late-report cases are pinned by driver::tests::a_compatible_exchange_completing_after_its_allowance_is_late, which returns an OpenaiCompatible 200 without a second exchange poll, and driver::tests::a_late_error_report_with_a_finished_body_fails_as_missing_headers, which pins a 401 whose error body is already at EOF so the exchange completes in the same poll. Both fail by assertion on the previous code. driver::tests::an_exchange_failure_without_a_report_keeps_its_error pins a connection failure with no report keeping its own kind and reason, and driver::tests::a_compatible_exchange_completing_at_the_allowance_starts pins a report written exactly at the allowance still starting the reply.

@yyhhyyyyyy

Copy link
Copy Markdown
Collaborator Author

Fixed in 5e40aa4, in the same change: while the driver still awaits headers, the exchange arm now consumes a queued report and compares its instant with the header allowance before it returns, so an exchange that publishes the report and completes in the same poll no longer ignores it. driver::tests::a_compatible_exchange_completing_after_its_allowance_is_late covers a compatible exchange that completes in that poll, and driver::tests::an_exchange_failure_without_a_report_keeps_its_error pins that a failure with no report keeps its own error. The select order is unchanged.

@eric8810 eric8810 left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Re-reviewed 5e40aa45. The exchange-completion arm now checks the queued header timestamp before returning, so a late compatible 200 and a late 401 whose body is already complete both report missing headers. A report exactly at the deadline still starts the compatible stream, and a transport failure that produced no report keeps its original error. This resolves my previous request. I verified all four new cases and the existing report/deadline-arm regressions in the Linux job; ci-ok passed.

@zerob13 zerob13 left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

The exchange-completion arm now consumes a queued header report and applies the same deadline as the other driver paths. The hosted Linux tests cover same-poll success and error, the exact deadline, and the existing cancellation/retry paths. Run 45535211463 passed with Aimux 0.7.0 on the #2154 merge base; the intervening changes on main do not touch the Aimux driver, its tests, or dependency lock. This approval covers the reviewed head, not a fresh full CI run on current main.

@yyhhyyyyyy
yyhhyyyyyy merged commit a7f4e87 into main Nov 25, 2026
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.

3 participants