Skip to content

studiobench: a window's stream-cost integrity is about one response - #9609

Merged
danielhanchen merged 5 commits into
mainfrom
p130-streamcost-decoder
Aug 25, 2026
Merged

danielhanchen merged 5 commits into
mainfrom
p130-streamcost-decoder

Conversation

@danielhanchen

Copy link
Copy Markdown
Member

StreamCostInstrument.close refuses a window whose opening buffer held half a frame AND
which then counted that frame. That pair is the thing that actually makes the denominator
wrong: characters delivered before the window opened, counted inside it. The two halves
were read at two different scopes. pending lives on the per-decoder state; carriedFlushes
was page-global on S, so any decoder could move the counter that was supposed to be about
that one buffer.

send_turn follows stop_generation in all three shipped schedules, and stop_generation
exists to cut a socket mid-frame. So the abort's orphaned half frame was routinely paired
with a carried flush produced by the NEW response's own split, and a window that had
delivered every one of its characters was refused for a buffer that never flushed.

Measured against the real instrument in Chromium rather than argued: a two-chunk reply
after an abort delivers all 13 of its characters inside the window and is refused, while
the same traffic with no abort in front of it is accepted. A refused window is dropped
outright, and reads go ragged exactly when the renderer is jammed, so the loss falls on the
most expensive windows. That is the flattering direction on cost per character.

What changed

carriedFlushes moves onto the decoder's own state beside pending, and
wireIntegrity(id) answers about a NAMED decoder whose id open records and close passes
back.

Named rather than compared at the two ends, and that distinction is the whole fix. A
same-id-at-both-ends check gets the important case wrong: the response pending at the open
can complete its carried frame INSIDE the window and then be replaced as the active decoder
before the close, and an id comparison would see two different ids, discard exactly the
carry it is looking for, and accept a window whose denominator really is wrong. Asking
about the named decoder answers regardless of who is active now.

Two details that would otherwise bite later. The sentinel active is built by the same
constructor as a real decoder state rather than by hand, or a window opening before the
first stream reads undefined where it wants zero. And the id map holds integers and no
TextDecoder reference, so it cannot defeat the WeakMap scoping the previous fix
introduced; it is bounded on the same discipline as MAX_PENDING_CHARS.

No payload key changes. decoder_id is an argument between streamcost.py and
streamcost.js and never reaches a row, so INTERFACES.md has nothing to add and the
reasoning lives in the instrument.

Tests

Three new, and the second and third are the ones that matter:

  • the fragmented response after an abort is scoreable
  • the counter is shown still to MOVE, asked of the decoder that owns the split, so the
    first test cannot pass against an instrument that has simply stopped counting carried
    flushes, which would take the genuine straddle refusal down with it
  • a frame that really did straddle the window open still refuses its window, with another
    decoder active at the close, which is the case the rejected id-comparison fix would break

Mutation: put the counter back on a page-global and the first fails with
assert 1 == 0 on wire_carried_frames_counted_in_window.

Four existing exact-dict assertions gain the new key, on the same tolerance idiom they
already use for carried_flushes beside it.

pytest tests/studio/studiobench -q: 1301 passed, up from 1298. ruff clean.

Follow-up to #9439, where this was accepted as a real residual on the fix made in that PR
and deliberately left open.

`StreamCostInstrument.close` refuses a window whose opening buffer held half a frame AND
which then counted that frame. That pair is the thing that actually makes the denominator
wrong: characters delivered before the window opened, counted inside it. The two halves
were read at two different scopes. `pending` lives on the per-decoder state; `carriedFlushes`
was page-global on `S`, so any decoder could move the counter that was supposed to be about
that one buffer.

`send_turn` follows `stop_generation` in all three shipped schedules, and `stop_generation`
exists to cut a socket mid-frame. So the abort's orphaned half frame was routinely paired
with a carried flush produced by the NEW response's own split, and a window that had
delivered every one of its characters was refused for a buffer that never flushed.

Measured against the real instrument in Chromium rather than argued: a two-chunk reply
after an abort delivers all 13 of its characters inside the window and is refused, while
the same traffic with no abort in front of it is accepted. A refused window is dropped
outright, and reads go ragged exactly when the renderer is jammed, so the loss falls on the
most expensive windows. That is the flattering direction on cost per character.

## What changed

`carriedFlushes` moves onto the decoder's own state beside `pending`, and
`wireIntegrity(id)` answers about a NAMED decoder whose id `open` records and `close` passes
back.

Named rather than compared at the two ends, and that distinction is the whole fix. A
same-id-at-both-ends check gets the important case wrong: the response pending at the open
can complete its carried frame INSIDE the window and then be replaced as the active decoder
before the close, and an id comparison would see two different ids, discard exactly the
carry it is looking for, and accept a window whose denominator really is wrong. Asking
about the named decoder answers regardless of who is active now.

Two details that would otherwise bite later. The sentinel `active` is built by the same
constructor as a real decoder state rather than by hand, or a window opening before the
first stream reads `undefined` where it wants zero. And the id map holds integers and no
`TextDecoder` reference, so it cannot defeat the WeakMap scoping the previous fix
introduced; it is bounded on the same discipline as `MAX_PENDING_CHARS`.

No payload key changes. `decoder_id` is an argument between `streamcost.py` and
`streamcost.js` and never reaches a row, so `INTERFACES.md` has nothing to add and the
reasoning lives in the instrument.

## Tests

Three new, and the second and third are the ones that matter:

- the fragmented response after an abort is scoreable
- the counter is shown still to MOVE, asked of the decoder that owns the split, so the
  first test cannot pass against an instrument that has simply stopped counting carried
  flushes, which would take the genuine straddle refusal down with it
- a frame that really did straddle the window open still refuses its window, with another
  decoder active at the close, which is the case the rejected id-comparison fix would break

Mutation: put the counter back on a page-global and the first fails with
`assert 1 == 0` on `wire_carried_frames_counted_in_window`.

Four existing exact-dict assertions gain the new key, on the same tolerance idiom they
already use for `carried_flushes` beside it.

`pytest tests/studio/studiobench -q`: 1301 passed, up from 1298. `ruff` clean.

Follow-up to #9439, where this was accepted as a real residual on the fix made in that PR
and deliberately left open.

@chatgpt-codex-connector chatgpt-codex-connector Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: fe690726d0

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

Comment on lines 605 to +607
pending_chars: active.pending.length + heldMarkerChars(),
// WHICH decoder the two numbers above are about, so the close can ask about the same one.
decoder_id: active.id,

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

P2 Badge Preserve the owner of a held marker fragment

When a new decoder has emitted only a partial data: marker at window open, pending_chars includes that decoder's markerHold, but decoder_id still names the previously active decoder (or the sentinel). If the new decoder completes its frame inside the window, its carried counter increments, while close() queries the unrelated ID and observes a zero delta; with no residual or parse failure, the window is incorrectly marked scoreable even though its denominator includes a frame that began before the window. Return integrity ownership for the held marker as well, or otherwise track every pending owner's carried count.

Useful? React with 👍 / 👎.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

Reproduced the mechanism exactly as described and it does not reach the consequence, so no change here.

Driven in chromium: decoder A completes a frame, the response ends, decoder B's first chunk is dat. At the open pending_chars=3 and decoder_id=2, which is A, so B's carried flush inside the window is invisible and the window closes scoreable. That part is right.

The window is scoreable correctly, though. markerTail is at most four characters of the literal data: and setMarkerTail is only ever reached on the branch where pending is empty, so a held fragment carries ZERO denominator characters. In that run reply_chars_delta came out 5 for hello, every character of it delivered after the open. The refusal exists for a buffer whose characters were delivered before the window and counted inside it, and that case is a real pending half frame, which lives on the decoder that is active by construction, is named by decoder_id, and still fires (test_a_window_that_opens_on_a_half_delivered_frame_is_not_scoreable).

Widening ownership to the marker holder would refuse that window instead. Pinned in f2a27da as test_a_marker_fragment_held_at_the_open_does_not_cost_the_window_its_reading, which goes red under exactly that change (decoder_id: (markerHold || active).id) while the other 24 stay green.

@danielhanchen

Copy link
Copy Markdown
Member Author

@codex review

1 similar comment
@danielhanchen

Copy link
Copy Markdown
Member Author

@codex review

@chatgpt-codex-connector

Copy link
Copy Markdown

Codex Review: Didn't find any major issues. Keep them coming!

Reviewed commit: f2a27da396

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

@danielhanchen
danielhanchen merged commit b7f8698 into main Aug 25, 2026
29 checks passed
@danielhanchen
danielhanchen deleted the p130-streamcost-decoder branch August 25, 2026 00:49
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