55 type OrchestrationV2Command ,
66} from "@t3tools/contracts" ;
77import { modelSelectionsEqual } from "@t3tools/shared/model" ;
8+ import * as NodeSqliteClient from "@t3tools/shared/nodeSqliteClient" ;
89import * as DateTime from "effect/DateTime" ;
910import * as Effect from "effect/Effect" ;
1011import * as Layer from "effect/Layer" ;
@@ -207,12 +208,16 @@ const makeSweep = Effect.gen(function* () {
207208 const now = yield * DateTime . now ;
208209 // A thread scheduled to resume continues early once claude-swap moves its login.
209210 const loginMayMove = preferences . autoResumeLimitedThreads && preferences . claudeSwapUsageEnabled ;
210- const candidates = yield * projections . getLimitRecoveryCandidates ( {
211- now,
212- autoResume : preferences . autoResumeLimitedThreads ,
213- snooze : preferences . snoozeLimitedThreads ,
214- beforeReset : fallbackEnabled || loginMayMove ,
215- } ) ;
211+ // Both scans run every scheduler tick and read every thread. On the writer they
212+ // run on the event loop, and a cold page cache made one take seconds.
213+ const candidates = yield * projections
214+ . getLimitRecoveryCandidates ( {
215+ now,
216+ autoResume : preferences . autoResumeLimitedThreads ,
217+ snooze : preferences . snoozeLimitedThreads ,
218+ beforeReset : fallbackEnabled || loginMayMove ,
219+ } )
220+ . pipe ( NodeSqliteClient . readOnly ) ;
216221 const nowMs = DateTime . toEpochMillis ( now ) ;
217222 for ( const thread of candidates ) {
218223 // The fallback is the last resort, so it wins over waiting for the reset.
@@ -235,7 +240,9 @@ const makeSweep = Effect.gen(function* () {
235240 yield * dispatch ( thread , command ) ;
236241 }
237242 // Runs even with the fallback turned off, so threads it already moved still come back.
238- const returns = yield * projections . getLimitFallbackReturnCandidates ( ) ;
243+ const returns = yield * projections
244+ . getLimitFallbackReturnCandidates ( )
245+ . pipe ( NodeSqliteClient . readOnly ) ;
239246 if ( returns . length === 0 || ! ( yield * usageLimitSources . claudeSwapHasRoom ) ) return ;
240247 for ( const thread of returns ) {
241248 const command = limitFallbackReturnCommand ( thread ) ;
0 commit comments