@@ -3,6 +3,7 @@ import { describe, it, expect, vi, beforeEach } from 'vitest';
33
44import type { MastraDBMessage } from '../../agent/message-list' ;
55import { MessageList } from '../../agent/message-list' ;
6+ import { TripWire } from '../../agent/trip-wire' ;
67import type { IMastraLogger } from '../../logger' ;
78import { ProcessorRunner } from '../../processors/runner' ;
89import type { ChunkType } from '../../stream' ;
@@ -870,6 +871,54 @@ describe('TokenLimiterProcessor', () => {
870871 ) . rejects . toThrow ( 'System messages alone exceed token limit' ) ;
871872 } ) ;
872873
874+ it ( 'should throw TripWire when no messages fit within the remaining token budget' , async ( ) => {
875+ const processor = new TokenLimiterProcessor ( { limit : 25 } ) ;
876+ const messageList = new MessageList ( ) ;
877+
878+ messageList . add (
879+ {
880+ id : 'user-1' ,
881+ role : 'user' ,
882+ content : { format : 2 , content : 'Hello' , parts : [ { type : 'text' , text : 'Hello' } ] } ,
883+ createdAt : new Date ( '2023-01-01T00:00:00Z' ) ,
884+ } ,
885+ 'input' ,
886+ ) ;
887+
888+ try {
889+ await processor . processInputStep ( {
890+ messageList,
891+ stepNumber : 1 ,
892+ model : createMockModel ( ) ,
893+ steps : [ ] ,
894+ systemMessages : [ ] ,
895+ state : { } ,
896+ retryCount : 0 ,
897+ abort : ( ( ) => {
898+ throw new Error ( 'aborted' ) ;
899+ } ) as any ,
900+ } ) ;
901+ expect . fail ( 'Expected TokenLimiterProcessor to throw a TripWire' ) ;
902+ } catch ( error ) {
903+ expect ( error ) . toBeInstanceOf ( TripWire ) ;
904+ expect ( error ) . toHaveProperty (
905+ 'message' ,
906+ 'TokenLimiterProcessor: No messages fit within the remaining token budget. Cannot send LLM a request with no messages.' ,
907+ ) ;
908+ expect ( ( error as TripWire ) . options ) . toEqual ( {
909+ retry : false ,
910+ metadata : {
911+ systemTokens : 0 ,
912+ limit : 25 ,
913+ remainingBudget : 1 ,
914+ messageCount : 1 ,
915+ } ,
916+ } ) ;
917+ }
918+
919+ expect ( messageList . get . all . db ( ) ) . toHaveLength ( 1 ) ;
920+ } ) ;
921+
873922 it ( 'should handle tool call messages in token counting' , async ( ) => {
874923 const processor = new TokenLimiterProcessor ( { limit : 100 } ) ;
875924
0 commit comments