Skip to content

studiobench: give the action bar selftest's two clocks one origin - #9578

Merged
danielhanchen merged 2 commits into
mainfrom
fix-action-bar-wait-clock-origin
Aug 23, 2026
Merged

danielhanchen merged 2 commits into
mainfrom
fix-action-bar-wait-clock-origin

Conversation

@danielhanchen

Copy link
Copy Markdown
Member

Selftests, lint and the single-file build is red on main at 83539fa (run 32637092740):

tests/studio/studiobench/scene/selftest/test_studiobench_action_bar_wait.py::test_a_control_that_arrives_late_is_waited_for_rather_than_reported_missing
AssertionError: {'ran': True, 'waitedMs': 307.9, 'openMs': 0.1, 'closeMs': 0, ...}
assert 307.9 >= (400 * 0.8)

The test is mine, from #9297.

What was wrong

The two numbers in that assertion were measured from different origins.

HARNESS_JS armed the mount deadline at top level, const started = Date.now(), so the shimmed bar appeared 400 ms after the harness SCRIPT LOADED. waitedMs comes from waitForActionButton in scene/dom.js, whose clock starts when the action is INVOKED. Between those two instants sit two readFileSync calls, the dom.js eval, two new Function compiles, and V8 lazily compiling the action body on its first call.

So waitedMs was LATE_MOUNT_MS minus that setup cost, and the setup cost is whatever else the machine happens to be doing. On an idle machine it is a few milliseconds and the assertion passes. On the runner it cost about 92 ms and it did not. The * 0.8 was absorbing an offset that is not constant, not allowing for timer jitter, so it did not have a value that was correct for both machines.

Note what the failure payload says: ran: True, items: 2, openMs and closeMs both present. The action waited for the late bar and drove it correctly. Only the number describing the wait was measured against the wrong zero.

The fix

Arm the mount deadline from the action's own first look at the DOM, read off the same performance.now() dom.js reads, rather than at script load.

The ordering is what makes this safe rather than merely closer. waitForActionButton stamps started, calls hoverLastAssistantMessage (which only queries document), and then reads a control. That read is the first call into the shim's mounted(), so the deadline starts level with the action's clock or a hair after it, never before. waitedMs therefore cannot undershoot LATE_MOUNT_MS by any elapsed time, and a slow machine can only push the sample that ends the wait LATER, never earlier.

That lets the bound get tighter rather than looser: >= LATE_MOUNT_MS - 1 instead of >= 0.8 * LATE_MOUNT_MS. The 1 ms is dom.js rounding waitedMs to a tenth, and it does not scale with LATE_MOUNT_MS. The upper bound waitedMs < ACTION_BAR_WAIT_MS is unchanged, and the assertion still says what it existed to say, that the bar was absent when the wait began rather than there all along. It remains a liveness test.

No change to scene/actions.py or scene/dom.js. The shipped code was never at fault; the harness was measuring it wrong.

Measurements

40 runs per condition. Loaded means pinned with taskset to a single core against 48 spinners, so setup time is inflated the way the runner inflated it.

condition min p50 max test result
before, idle 403.2 404.4 405.7 40/40 pass
before, loaded 210.1 267.6 419.0 16/40 pass, 24 fail
after, idle 403.2 404.4 404.8 40/40 pass
after, loaded 400.3 409.2 417.4 40/40 pass

The loaded failures reproduce the CI signature exactly, including ran: True and items: 2 on every one of them. Pushed further, to 96 spinners on one core, the fixed test still reports min 400.5 and 0 failures.

test_the_wait_is_bounded_and_a_control_that_never_appears_still_reports_not_run is sound and is left alone. Both of its numbers are on the action's own clock: the loop runs while elapsed is under 300 ms, so waitedMs >= 300 structurally. Measured at min 300.3 across 40 runs under the same load, against its bound of 250.

Full job test set on this branch: python -m pytest tests/studio/studiobench -q gives 654 passed.

One thing I did not change, recorded here because I saw it. test_a_settled_reply_costs_the_action_nothing asserts waitedMs < 50 and does genuinely measure elapsed wall time, so at 96 spinners on one core it goes over on 1 or 2 runs in 40 as dom.js's methods are JIT compiled inside the measured window. That is identical before and after this change, it needs a load far past the level that reproduces the actual CI failure, and it is a different defect, so it is out of scope here.

Repo tests (CPU) is also red on main right now for an unrelated stripSearchImageTokens is not defined, which another change is handling.

The mount deadline in HARNESS_JS started at script load, but the waitedMs it
was compared against starts inside waitForActionButton, which runs after two
readFileSync calls, the dom.js eval, two Function compiles and V8's lazy
compile of the action body on first call. waitedMs was therefore
LATE_MOUNT_MS minus that setup cost, and the setup cost is whatever else the
machine is doing. The 0.8 in the assertion was absorbing that offset rather
than allowing for timer jitter.

Arm the deadline from the action's own first look at the DOM instead, off the
same performance.now() dom.js reads. waitForActionButton stamps its clock,
hovers, then reads a control, and that read is the first call into the shim,
so the deadline starts level with the action's clock or a hair after it and
never before. waitedMs can no longer undershoot LATE_MOUNT_MS, and a loaded
machine can only push the sample that ends the wait later.

The bound tightens from 0.8 * LATE_MOUNT_MS to LATE_MOUNT_MS - 1, the 1 ms
being dom.js rounding waitedMs to a tenth. It stays a liveness assertion and
keeps its upper bound of ACTION_BAR_WAIT_MS.

Measured over 40 runs each, pinned to one core against 48 spinners so setup
is inflated the way the runner inflated it: before, 24 of 40 fail with
waitedMs from 210 to 300 while ran, items and the open and close timings are
correct every time; after, 40 of 40 pass with waitedMs from 400.3 to 417.4.
Idle, before and after both sit at 403 to 405.
@danielhanchen
danielhanchen merged commit 3340780 into main Aug 23, 2026
26 of 27 checks passed
@danielhanchen
danielhanchen deleted the fix-action-bar-wait-clock-origin branch August 23, 2026 14:22
danielhanchen added a commit to danielhanchen/unsloth-staging-2 that referenced this pull request Aug 23, 2026
…its text (unslothai#9591)

* Read the settings route's contract off its parse tree, not its text

`Repo tests (CPU)` has been red on main since unslothai#9492 (1f475d7) on
test_the_backfill_fills_in_fields_rather_than_skipping_known_keys. The rule the
test guards did not change. unslothai#9492 added a third name to the
`model_dump(exclude = {...})` set in `update_openai_auto_switch_override`, the
formatter then split the set one name per line, and

    assert '"remove", "fill_absent_fields"' in route

no longer matched, because `route` is the raw source text of
studio/backend/routes/settings.py and the two names now have a newline and
sixteen spaces between them. Both names are still excluded. Nothing about the
write-mode-is-not-a-saved-field rule moved.

This is the third failure of this shape on main in as many days (unslothai#9578, unslothai#9579),
so pinning a new literal only moves the next one into the future. A test that
has to be re-edited whenever a set literal is reflowed is not testing the thing
it names.

The frontend greps in this file have no parser to hand. The backend does. Three
helpers read the rule out of settings.py's AST instead:

- `_model_dump_exclusions` returns the names the one `model_dump(exclude = ...)`
  in a given function leaves out. Exactly one such call, or the answer would be
  a union and a name dropped from the call that matters could hide behind
  another.
- `_annotated_field` returns the annotation and literal default of a field on a
  pydantic model, so "declared as a bool defaulting to False" is asserted as
  that rather than as a substring.
- `_forwarded_keywords` returns the keyword arguments passed as
  `name = payload.name`, which is the part the storage layer depends on.

Each raises rather than returning empty when it cannot find what it was pointed
at, so a renamed handler or field reddens here instead of passing vacuously.

Verified by mutation against the repaired test, one at a time on settings.py:

| Mutation                                          | Result |
|---------------------------------------------------|--------|
| unmodified main                                    | pass   |
| `"fill_absent_fields"` dropped from the exclude set| fail   |
| the exclude set reflowed onto one line, reordered  | pass   |
| the payload field defaulted to True                | fail   |
| `fill_absent_fields` no longer forwarded to the db | fail   |
| the route handler renamed                          | fail   |
| the payload field renamed                          | fail   |

The reflow is the case that caused this outage and is now the only one that
stays green; every mutation that actually breaks the contract still reddens.

Local: `tests/studio/test_model_picker_contracts.py` 187 passed, was 186 passed
1 failed. No production code changed.

* [pre-commit.ci] auto fixes from pre-commit.com hooks

for more information, see https://pre-commit.ci

---------

Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>
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