Repository navigation
studio: settle the per-chat sampling tests on the store's own write barrier - #9352
danielhanchen wants to merge 1 commit into
Conversation
…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.
|
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 Same worktree, same
The 15 include all 12 the Windows job reports and all 9 the The diagnosis is the right shape too. The drains spent a fixed 18 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 The two files left alone, |
|
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 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. |
…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.
Main is red on
Frontend unit tests (Windows)and onFrontend 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:
setParamscaptures the edit, a 400ms debounce fires, and the write runs on a promise chain that ends inThat 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 withmodule.registerand therefore runs off-thread withexistsSyncprobes 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:
18 turns, then read the row back.
tickis 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:
TypeError: Cannot read properties of undefined (reading 'temperature')atthread-scoped-sampling-compat.test.ts:264, wherethreadRows.rows.get("E")came back undefined.threadRowsthat the next scenario has justreset(). That isB4casserting1.37 !== 0: 1.37 is the temperature an earlier test wrote, arriving after the fact.A1reporting 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
setImmediateturns pass between the debounce firing and the row being written, on the same checkout and the same machine: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: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.temperatureinwriteThreadScopedSettings) 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:The lint total is identical to
origin/mainat78a3587, 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.tsandtests/thread-scoped-pairing-default-refresh.test.tssettle withawait 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.