Skip to content

studio: settle the per-chat sampling tests on the store's own write barrier - #9352

Closed
danielhanchen wants to merge 1 commit into
mainfrom
fix-windows-frontend-tests
Closed

danielhanchen wants to merge 1 commit into
mainfrom
fix-windows-frontend-tests

Conversation

@danielhanchen

Copy link
Copy Markdown
Member

Main is red on Frontend unit tests (Windows) and on Frontend build + bundle sanity, and it blocks every open PR. Same bug in both, different severity.

What actually happens

A per-chat sampling edit is written like this: setParams captures the edit, a 400ms debounce fires, and the write runs on a promise chain that ends in

const { updateStoredChatThread } = await import("../utils/chat-history-storage");

That specifier has no extension, so it is resolved through a hook, and in the tests it is resolved through tests/thread-sampling-resolver.mjs, which is registered with module.register and therefore runs off-thread with existsSync probes behind it. The import is real work on a real event-loop turn, not a microtask.

The three drains that these tests run on did not wait for that. They gave it a fixed number of turns:

for (let round = 0; round < 3; round += 1) {
  tick(1000);
  for (let i = 0; i < 6; i += 1) {
    await new Promise((resolve) => setImmediate(resolve));
  }
}

18 turns, then read the row back. tick is a mocked clock, so it costs no real time at all: those 18 turns go by in microseconds on an otherwise empty loop, and nothing in them is tied to the import finishing.

When the import has not finished, two things follow, and both are in the CI logs:

  • the row is simply not there yet. That is TypeError: Cannot read properties of undefined (reading 'temperature') at thread-scoped-sampling-compat.test.ts:264, where threadRows.rows.get("E") came back undefined.
  • the write lands later, after the test has moved on, into the shared threadRows that the next scenario has just reset(). That is B4c asserting 1.37 !== 0: 1.37 is the temperature an earlier test wrote, arriving after the fact. A1 reporting both "owed 0.6, shows 1.37" and "owed 1.37, shows 0.6" in the same sweep is the same thing across 120 orderings.

Why it is red in CI and green locally

Node version. Both jobs pin node-version: '22'; I had been running 24.

I instrumented the write path to count how many setImmediate turns pass between the debounce firing and the row being written, on the same checkout and the same machine:

Node turns needed (3 runs) budget
24.14.0 1, 1, 1 18
22.22.0 2, 61, 34 18

Node 24 collapses that dynamic import to a single turn, so the budget is never close to being spent and the suite is green. On Node 22 it is variable and routinely well past 18.

Severity then tracks how fast the machine is, which is why the two jobs fail different subsets of the same set: ubuntu fails 9, Windows fails 12, and Node 22 on my box, unloaded, fails 15. It is one bug, not two, and it is not Windows-specific.

Before and after, Node 22, Linux

Full tests/**/*.test.ts, node-v22.22.0:

before   tests 4080   pass 4065   fail 15
after    tests 4080   pass 4080   fail 0

The 15 before are a superset of the 12 the Windows job fails and of the 9 the ubuntu job fails.

The change

awaitStartedThreadScopedSettingsWrites() on the store returns when the row writes it has already started have landed. It is not a flush: a debounce that has not fired is left alone, so a caller cannot use it to make a write happen earlier than the store would have made it. The three drains call it after their turns instead of assuming the turns were enough.

That removes the dependency on how long module resolution takes on the machine, rather than widening the window. Nothing is skipped, retried or given more time.

The tests still bite

To check the barrier is not hiding the behaviour it is supposed to observe, I put a one-line regression into the write path (delete settings.temperature in writeThreadScopedSettings) and ran the two files on Node 22 with the fix in place: 18 failures, including all of the ones this PR turns green. Reverted afterwards.

Counts

studio/frontend, Node 24 (the local default), before and after are the same because 24 was never affected:

npm test        tests 4080   pass 4080   fail 0
npm run typecheck   clean, exit 0
npm run lint    735 problems (577 errors, 158 warnings)

The lint total is identical to origin/main at 78a3587, which I ran in a separate clean worktree to compare. Those are pre-existing and none of them are in the files this PR touches.

One thing I did not change

tests/thread-scoped-commit-restores-defaults.test.ts and tests/thread-scoped-pairing-default-refresh.test.ts settle with await new Promise((resolve) => setTimeout(resolve, 900)) on the real clock. That is the same assumption in a different form, but it does have 500ms of real slack past the debounce and it is green on Node 22 on both jobs, so I left it alone rather than widen this PR.

…arrier

The row write ends in a dynamic import. The test drains gave it a fixed 18
event-loop turns and then read the row, which on Node 22 is not enough.
Await the writes the store has actually started instead of counting turns.
@danielhanchen

Copy link
Copy Markdown
Member Author

Reproduced the whole thing independently on Node 22 rather than taking it on report, and it holds up exactly.

I have Node 24.14.0 locally, which is why my earlier full-suite runs were green and why I first mistook this for a Windows problem. Every CI job that fails pins node-version: '22'.

Same worktree, same node_modules, Node 22.22.0:

tests pass fail
with the fix reverted, i.e. current main 4080 4065 15
with the fix 4080 4080 0

The 15 include all 12 the Windows job reports and all 9 the Frontend build + bundle sanity job reports, which settles that it is one bug rather than two, with a severity that tracks how slow the machine is.

The diagnosis is the right shape too. The drains spent a fixed 18 setImmediate turns and then read the row, but tick is a mocked clock, so those turns cost no real time and nothing in them was tied to the dynamic import() finishing. Turn counts of 1, 1, 1 on Node 24 against 2, 61, 34 on Node 22 is exactly why an 18-turn budget looked generous and was not.

Two things I want to note approvingly, since they are what makes this a fix rather than a widening. The barrier awaits writes already in flight rather than flushing, so it cannot make a write happen earlier than the store would have. And putting a delete settings.temperature regression into the write path still fails 18 tests with the barrier in place, which is the check that matters: a barrier that hid what it was waiting on would have gone green.

The two files left alone, thread-scoped-commit-restores-defaults.test.ts and thread-scoped-pairing-default-refresh.test.ts, are the same assumption in a different form. Agreed they are out of scope here given the 500ms of real slack, but they are the next thing to bite when someone changes that debounce.

@danielhanchen

Copy link
Copy Markdown
Member Author

Closing in favour of #9367, which takes this PR's approach.

You were right about the mechanism and I merged the weaker one first. #9332 fixed the same bug by waiting on the mocked timers plus a race against the module loader: it issues an import of its own each round and relies on the hooks thread serving requests in order, so a reply to a later request cannot arrive first. That works, and it is an assumption about node internals standing in for a claim about the store. If it ever stops holding it fails as a stale read, which is the exact failure mode the change existed to abolish.

Awaiting threadSettingsWriteChains has no such assumption behind it, so #9367 adds awaitStartedThreadScopedSettingsWrites as written here and hands it to the drain as a barrier, keeping #9332's exact timer counting and its exhaustion throw.

Mutation-tested on v22.23.2, the version setup-node 22 resolves to: with the barrier disabled and timer counting left in place, the simulation suite is 8 failed / 10 passed. The barrier is doing the work.

rhsCZ pushed a commit to rhsCZ/unsloth that referenced this pull request Aug 20, 2026
…loader (unslothai#9367)

unslothai#9332 fixed these suites by waiting on two observables: the mocked timers, which
it counts exactly, and the module loader, which it raced by issuing an import of
its own each round. The second one works and is still the wrong thing to assert.

The loader probe rests on an assumption about node internals that I recorded in
the comment and never verified: that the hooks thread serves requests in order
over one port, so a reply to a request issued after the store's cannot arrive
first. If that ever stops holding it fails as a stale read, which is precisely
the failure mode unslothai#9332 existed to abolish -- a wait that comes up short reports
"chat A minP: owed 0, shows 0.01" and sends the next reader into the store.

The store already tracks the thing worth waiting on. threadSettingsWriteChains
holds the live promise chain per thread, and awaitThreadScopedSettingsWrite
already awaits one of them by id. What a drain needs is all of the started ones,
without knowing which chats the store decided to write, so this adds
awaitStartedThreadScopedSettingsWrites.

It is deliberately not a flush. A debounce that has not fired yet is left alone,
so a caller cannot use it to make a write happen earlier than the store would
have; that would let a test pass against a store that never scheduled the write
at all. It is bounded at 20 passes rather than looping until the map empties, so
a write that keeps rescheduling itself surfaces as a failed assertion instead of
a hang. It repeats rather than awaiting one snapshot because a chain that settles
can leave a newer one behind it for the same chat.

drainMockedTimers now takes that as a caller-supplied `barrier` instead of
carrying its own loader probe. The timer counting is unchanged, and so is the
exhaustion throw, which is the property that keeps an under-drain from ever again
looking like a lost edit.

The approach is the one from unslothai#9352, which I opened against the same bug in
parallel and is closed in favour of this. unslothai#9332's mechanism landed first and
turned Windows green; this replaces the part of it that was a guess about the
runtime with a measurement of the subject.

Verified
------------------------------------------------------------------------
Both suites twice on v24.14.0 and twice on v22.23.2, the version setup-node 22
resolves to: compat 16 passed, simulation 18 passed, every run. Full frontend
suite 4080 passed, 0 failed.

Mutation-tested on v22.23.2, which is the only runtime where this is observable:
disabling the barrier and leaving the timer counting in place gives 8 failed, 10
passed. So the barrier is carrying the load, not decorating a wait that already
worked.
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