magic-context icon indicating copy to clipboard operation
magic-context copied to clipboard

Two schedulers can consume the same Dreamer slot

Open iceteaSA opened this issue 6 days ago • 3 comments

Two schedulers for one project can both collect an identical due slot before either runs it. On master (57e4e9beb), packages/plugin/src/features/magic-context/dreamer/task-scheduler.ts:289–297,450–492 collects due rows before acquiring the domain lease, then rechecks eligible activity under the lease without confirming that the same slot is still due. If another scheduler advances the row before the second gets the lease, and the activity gate still passes, the second can run stale work.

Test idea: pause one scheduler after it reads due state. Let another consume the slot and release the lease, then resume the first with an activity gate that stays true. Require one executor call and one advancement. Under the lease, reread and compare the durable slot or atomically claim that slot before running or advancing it.

I can open a PR if you want one.

iceteaSA avatar Nov 27 '26 21:11 iceteaSA

Thanks. We checked this against current master and the code is as you describe. Due rows are collected before the domain lease is taken. Under the lease, the scheduler re-evaluates the task's gate (with the current lastRunAt and watermark) but does not read nextDueAt again, so it does not confirm that the slot it collected is still due. For a gated task the gate will often fail after the other scheduler's run, and the slot is then recorded as skipped; when the gate still passes, or the task has no gate, the task can run twice.

We have not reproduced it yet, and we will reply here on how we take it (your PR or our own change) once that is decided.

magic-alfonso[bot] avatar Nov 27 '26 21:11 magic-alfonso[bot]

Decided: we will make this change ourselves, and we have started. Under the lease the scheduler will read the durable slot again and continue only if it is still the slot it collected; otherwise it does nothing. We will post here when it is released.

magic-alfonso[bot] avatar Nov 28 '26 08:11 magic-alfonso[bot]

Fixed in 0.49.91, released today. A host now checks, once it holds the lock, that the scheduled run it collected is still due, and skips it when another host has already done it. Two running hosts no longer run the same nightly task twice.

Update with npx @cortexkit/magic-context@latest doctor and restart your host. No database migration is needed. Please reopen if you still see it on 0.49.91 or later.

magic-alfonso[bot] avatar Nov 28 '26 14:11 magic-alfonso[bot]