@@ -33,7 +33,7 @@ import * as Stream from "effect/Stream";
3333
3434import * as TestProviderHost from "@t3tools/provider-testing/TestProviderHost" ;
3535import * as McpProviderSessions from "@t3tools/provider-core/server/McpProviderSessions" ;
36- import type { MuseItem } from "./protocol.ts" ;
36+ import type { MuseItem , MuseSubscriptionUsage } from "./protocol.ts" ;
3737import type { MuseSdkHost } from "./sdk.ts" ;
3838import * as IdAllocator from "@t3tools/provider-core/server/IdAllocator" ;
3939import * as ProviderAdapter from "@t3tools/provider-core/server/ProviderAdapter" ;
@@ -173,6 +173,11 @@ const makeFakeMuse = Effect.fnUntraced(function* (idPrefix = "native") {
173173 Effect . sync ( ( ) => {
174174 notify ( { jsonrpc : "2.0" , method, params : { sessionId, ...params } } ) ;
175175 } ) ,
176+ /** A notification about the host or account rather than a session, as `usage/changed` is. */
177+ emitHostWide : ( method : string , params : Record < string , unknown > ) =>
178+ Effect . sync ( ( ) => {
179+ notify ( { jsonrpc : "2.0" , method, params } ) ;
180+ } ) ,
176181 takeCall : Effect . fnUntraced ( function * ( method : string ) {
177182 while ( true ) {
178183 const call = yield * Queue . take ( requests ) ;
@@ -339,6 +344,20 @@ const startConversation = Effect.fnUntraced(function* (
339344 return { nativeId : call . commandId ! , providerTurn : event . providerTurn } ;
340345} ) ;
341346
347+ // `usage/changed` as Muse 1.4.3 sent it after a reply (2026-10-08), with a placeholder tier.
348+ const SUBSCRIPTION_USAGE = {
349+ window : { usedPercent : 0 , windowDurationMins : 300 , resetsAtMs : 1791465557000 } ,
350+ weekly : { usedPercent : 5 , resetsAtMs : 1791763200000 } ,
351+ tier : "tier-1" ,
352+ observedAtMs : 1791447762610 ,
353+ } satisfies MuseSubscriptionUsage ;
354+ // How Muse ends a turn Meta refuses for quota (from a simulated Meta 429).
355+ const QUOTA_MESSAGE =
356+ "API error 429: Subscription quota exhausted. Your usage window resets soon. (rate_limit_error)" ;
357+ // How Muse 1.4.6 ended a turn at a real quota stop (2026-10-13), relayed by a credential proxy.
358+ const RELAYED_QUOTA_MESSAGE =
359+ "API error 429: All credentials for model muse-wonder-1.0-contributor are cooling down (last error: rate_limit_exceeded: Subscription quota exhausted. Your usage window resets at 2026-10-13T10:45:52Z.) (after 10 provider attempts)" ;
360+
342361const approval = ( turnId : string ) => ( {
343362 approvalId : "approval-1" ,
344363 turnId,
@@ -574,6 +593,129 @@ describe("MuseAdapterV2", () => {
574593 } ) . pipe ( Effect . scoped , Effect . provide ( testLayer ) ) ,
575594 ) ;
576595
596+ it . effect ( "stops at a Meta usage limit until every exhausted window resets" , ( ) =>
597+ Effect . gen ( function * ( ) {
598+ const fake = yield * makeFakeMuse ( ) ;
599+ const harness = yield * makeHarness ( fake ) ;
600+ // Meta's report after the last reply that went through: the session window ran out.
601+ yield * fake . emitHostWide ( "usage/changed" , {
602+ ...SUBSCRIPTION_USAGE ,
603+ window : { ...SUBSCRIPTION_USAGE . window , usedPercent : 103 } ,
604+ } ) ;
605+ const { nativeId } = yield * startConversation ( harness , fake ) ;
606+ yield * fake . emit ( "turn/completed" , {
607+ turnId : nativeId ,
608+ terminal : "failed" ,
609+ reason : QUOTA_MESSAGE ,
610+ error : { kind : "modelError" , message : QUOTA_MESSAGE , retryable : false } ,
611+ } ) ;
612+ const terminal = yield * harness . takeEvent ( "turn.terminal" ) ;
613+ assert . strictEqual ( terminal . status , "failed" ) ;
614+ assert . strictEqual ( terminal . threadDisposition , "reusable" ) ;
615+ assert . strictEqual ( terminal . failure ?. class , "usage_limit" ) ;
616+ // The exhausted session window's reset, not the weekly one.
617+ assert . strictEqual ( terminal . failure ?. resetAt , "2026-10-08T13:19:17.000Z" ) ;
618+ assert . isTrue (
619+ harness . allEvents . some (
620+ ( event ) =>
621+ event . type === "turn_item.updated" &&
622+ event . turnItem . type === "error" &&
623+ event . turnItem . title === "Usage limit reached" ,
624+ ) ,
625+ ) ;
626+
627+ const next = yield * startConversation ( harness , fake , 2 ) ;
628+ yield * fake . emit ( "turn/completed" , {
629+ turnId : next . nativeId ,
630+ terminal : "failed" ,
631+ error : { message : "API error 429: Rate limit exceeded. (rate_limit_error)" } ,
632+ } ) ;
633+ const throttled = yield * harness . takeEvent ( "turn.terminal" ) ;
634+ assert . strictEqual ( throttled . failure ?. class , "provider_error" ) ;
635+ } ) . pipe ( Effect . scoped , Effect . provide ( testLayer ) ) ,
636+ ) ;
637+
638+ it . effect ( "limits a quota stop without a reset time when no report shows a used-up window" , ( ) =>
639+ Effect . gen ( function * ( ) {
640+ const fake = yield * makeFakeMuse ( ) ;
641+ const harness = yield * makeHarness ( fake ) ;
642+ // The last report still had room in both windows, so it cannot say when Meta will allow more.
643+ yield * fake . emitHostWide ( "usage/changed" , SUBSCRIPTION_USAGE ) ;
644+ const { nativeId } = yield * startConversation ( harness , fake ) ;
645+ yield * fake . emit ( "turn/completed" , {
646+ turnId : nativeId ,
647+ terminal : "failed" ,
648+ error : { message : QUOTA_MESSAGE } ,
649+ } ) ;
650+ const terminal = yield * harness . takeEvent ( "turn.terminal" ) ;
651+ assert . strictEqual ( terminal . failure ?. class , "usage_limit" ) ;
652+ assert . isUndefined ( terminal . failure ?. resetAt ) ;
653+ } ) . pipe ( Effect . scoped , Effect . provide ( testLayer ) ) ,
654+ ) ;
655+
656+ it . effect ( "resumes a quota stop at the reset the refusal names" , ( ) =>
657+ Effect . gen ( function * ( ) {
658+ const fake = yield * makeFakeMuse ( ) ;
659+ const harness = yield * makeHarness ( fake ) ;
660+ // A fresh host has no report yet; the refusal itself says when the window resets.
661+ const { nativeId } = yield * startConversation ( harness , fake ) ;
662+ yield * fake . emit ( "turn/completed" , {
663+ turnId : nativeId ,
664+ terminal : "failed" ,
665+ reason : RELAYED_QUOTA_MESSAGE ,
666+ error : { kind : "modelError" , message : RELAYED_QUOTA_MESSAGE , retryable : true } ,
667+ } ) ;
668+ const terminal = yield * harness . takeEvent ( "turn.terminal" ) ;
669+ assert . strictEqual ( terminal . failure ?. class , "usage_limit" ) ;
670+ assert . strictEqual ( terminal . failure ?. resetAt , "2026-10-13T10:45:52.000Z" ) ;
671+ } ) . pipe ( Effect . scoped , Effect . provide ( testLayer ) ) ,
672+ ) ;
673+
674+ it . effect ( "limits a stop Muse types as quota at the reset it reports" , ( ) =>
675+ Effect . gen ( function * ( ) {
676+ const fake = yield * makeFakeMuse ( ) ;
677+ const harness = yield * makeHarness ( fake ) ;
678+ const { nativeId } = yield * startConversation ( harness , fake ) ;
679+ // The typed quota decides even when the text names no quota.
680+ yield * fake . emit ( "turn/completed" , {
681+ turnId : nativeId ,
682+ terminal : "failed" ,
683+ error : {
684+ kind : "modelError" ,
685+ message : "Model request refused." ,
686+ quota : { limitKind : "tierLimit" , resetsAtMs : SUBSCRIPTION_USAGE . window . resetsAtMs } ,
687+ retryable : true ,
688+ } ,
689+ } ) ;
690+ const terminal = yield * harness . takeEvent ( "turn.terminal" ) ;
691+ assert . strictEqual ( terminal . failure ?. class , "usage_limit" ) ;
692+ assert . strictEqual ( terminal . failure ?. resetAt , "2026-10-08T13:19:17.000Z" ) ;
693+ } ) . pipe ( Effect . scoped , Effect . provide ( testLayer ) ) ,
694+ ) ;
695+
696+ it . effect ( "skips an unreadable usage report without ending the session" , ( ) =>
697+ Effect . gen ( function * ( ) {
698+ const fake = yield * makeFakeMuse ( ) ;
699+ const harness = yield * makeHarness ( fake ) ;
700+ yield * fake . emitHostWide ( "usage/changed" , { window : "not usage" } ) ;
701+ // A reset no Date can hold would throw once formatted, so that report is skipped too.
702+ yield * fake . emitHostWide ( "usage/changed" , {
703+ ...SUBSCRIPTION_USAGE ,
704+ window : { ...SUBSCRIPTION_USAGE . window , usedPercent : 103 , resetsAtMs : 1e16 } ,
705+ } ) ;
706+ const { nativeId } = yield * startConversation ( harness , fake ) ;
707+ yield * fake . emit ( "turn/completed" , {
708+ turnId : nativeId ,
709+ terminal : "failed" ,
710+ error : { message : QUOTA_MESSAGE } ,
711+ } ) ;
712+ const terminal = yield * harness . takeEvent ( "turn.terminal" ) ;
713+ assert . strictEqual ( terminal . failure ?. class , "usage_limit" ) ;
714+ assert . isUndefined ( terminal . failure ?. resetAt ) ;
715+ assert . strictEqual ( fake . closeCount ( ) , 0 ) ;
716+ } ) . pipe ( Effect . scoped , Effect . provide ( testLayer ) ) ,
717+ ) ;
718+
577719 it . effect ( "moves a resumed session on another model back to the catalog default" , ( ) =>
578720 Effect . gen ( function * ( ) {
579721 const fake = yield * makeFakeMuse ( ) ;
0 commit comments