@@ -30,6 +30,7 @@ import {
3030 type TurnItemId ,
3131} from "@t3tools/contracts" ;
3232import { modelSelectionCommandType , modelSelectionsEqual } from "@t3tools/shared/model" ;
33+ import * as Clock from "effect/Clock" ;
3334import * as Context from "effect/Context" ;
3435import * as DateTime from "effect/DateTime" ;
3536import * as Duration from "effect/Duration" ;
@@ -39,6 +40,9 @@ import * as Option from "effect/Option";
3940import * as Schema from "effect/Schema" ;
4041import * as Stream from "effect/Stream" ;
4142
43+ import * as ProviderRegistry from "../provider/ProviderRegistry.ts" ;
44+ import * as ServerSettings from "../serverSettings.ts" ;
45+ import { limitAccountSwitchSelection } from "./LimitAccountSwitch.ts" ;
4246import * as Orchestrator from "./Orchestrator.ts" ;
4347import { projectTurnItemForDetail } from "./WireProjection.ts" ;
4448import * as LegacyV1ThreadImporter from "./legacy/LegacyV1ThreadImporter.ts" ;
@@ -452,6 +456,9 @@ const make = Effect.gen(function* () {
452456 const orchestrator = yield * Orchestrator . OrchestratorV2 ;
453457 const layerScope = yield * Effect . scope ;
454458 const legacyImporter = yield * LegacyV1ThreadImporter . LegacyV1ThreadImporter ;
459+ // Optional, so harnesses that build this service without providers still work.
460+ const serverSettings = yield * Effect . serviceOption ( ServerSettings . ServerSettingsService ) ;
461+ const providerRegistry = yield * Effect . serviceOption ( ProviderRegistry . ProviderRegistry ) ;
455462
456463 const ensureLegacyTranscript = Effect . fn (
457464 "orchestrationV2.threadManagement.ensureLegacyTranscript" ,
@@ -518,8 +525,48 @@ const make = Effect.gen(function* () {
518525 Effect . andThen ( orchestrator . getThreadSnapshotWindow ( threadId , options ) ) ,
519526 ) ;
520527
528+ // Every way to start a turn (clients, agent tools, scheduled tasks) comes
529+ // through here, so this is where a used-up account hands over. Reading the
530+ // limits is I/O, which the orchestrator's decisions must not do.
531+ const withLimitAccountSwitch = Effect . fn ( "orchestrationV2.threadManagement.limitAccountSwitch" ) (
532+ function * ( command : Parameters < ThreadManagementServiceShape [ "dispatch" ] > [ 0 ] ) {
533+ if (
534+ command . type !== "message.dispatch" ||
535+ Option . isNone ( serverSettings ) ||
536+ Option . isNone ( providerRegistry )
537+ ) {
538+ return command ;
539+ }
540+ const enabled = yield * serverSettings . value . getSettings . pipe (
541+ Effect . map ( ( settings ) => settings . switchAccountAtUsageLimit ) ,
542+ Effect . orElseSucceed ( ( ) => false ) ,
543+ ) ;
544+ if ( ! enabled ) return command ;
545+ const thread = yield * orchestrator
546+ . getThreadShell ( command . threadId )
547+ . pipe ( Effect . orElseSucceed ( ( ) => null ) ) ;
548+ if ( thread === null ) return command ;
549+ const selection = limitAccountSwitchSelection ( {
550+ command,
551+ thread,
552+ providers : yield * providerRegistry . value . getProviders ,
553+ nowMs : yield * Clock . currentTimeMillis ,
554+ } ) ;
555+ if ( selection === null ) return command ;
556+ yield * Effect . logInfo ( "Starting the turn on another account: usage limit reached" , {
557+ threadId : command . threadId ,
558+ from : ( command . modelSelection ?? thread . modelSelection ) . instanceId ,
559+ to : selection . instanceId ,
560+ } ) ;
561+ return { ...command , modelSelection : selection } ;
562+ } ,
563+ ) ;
564+
521565 const dispatch : ThreadManagementServiceShape [ "dispatch" ] = ( command ) =>
522- ensureCommandTranscripts ( command ) . pipe ( Effect . andThen ( orchestrator . dispatch ( command ) ) ) ;
566+ ensureCommandTranscripts ( command ) . pipe (
567+ Effect . andThen ( withLimitAccountSwitch ( command ) ) ,
568+ Effect . flatMap ( ( routed ) => orchestrator . dispatch ( routed ) ) ,
569+ ) ;
523570
524571 const getProjectThread : ThreadManagementServiceShape [ "getProjectThread" ] = ( input ) =>
525572 getThreadProjection ( input . threadId ) . pipe (
0 commit comments