@@ -15,7 +15,9 @@ import type { AnswerCallInput, HangupCallInput, NormalizedEvent } from "../types
1515import type { CallManagerContext } from "./context.js" ;
1616import { processEvent } from "./events.js" ;
1717import { speakInitialMessage } from "./outbound.js" ;
18+ import { MAX_CALL_REPLAY_KEYS } from "./replay-keys.js" ;
1819
20+ const MANAGER_REPLAY_KEY_LIMIT = 10_000 ;
1921const logSpy = vi . hoisted ( ( ) => {
2022 const logEntries : string [ ] = [ ] ;
2123 return {
@@ -93,7 +95,7 @@ function createContext(overrides: Partial<CallManagerContext> = {}): CallManager
9395 activeCalls : new Map ( ) ,
9496 providerCallIdMap : new Map ( ) ,
9597 processedEventIds : new Set ( ) ,
96- rejectedProviderCallIds : new Set ( ) ,
98+ rejectedProviderCallIds : new Map ( ) ,
9799 provider : null ,
98100 config : VoiceCallConfigSchema . parse ( {
99101 enabled : true ,
@@ -771,6 +773,91 @@ describe("processEvent (functional)", () => {
771773 expect ( replayResult ) . toEqual ( { kind : "ignored" } ) ;
772774 } ) ;
773775
776+ it ( "bounds committed replay keys in both manager and persisted call owners" , ( ) => {
777+ const now = Date . now ( ) ;
778+ const managerKeys = Array . from (
779+ { length : MANAGER_REPLAY_KEY_LIMIT } ,
780+ ( _ , index ) => `manager-${ index } ` ,
781+ ) ;
782+ const callKeys = Array . from ( { length : MAX_CALL_REPLAY_KEYS } , ( _ , index ) => `call-${ index } ` ) ;
783+ const ctx = createContext ( { processedEventIds : new Set ( managerKeys ) } ) ;
784+ ctx . activeCalls . set ( "call-bounded" , {
785+ callId : "call-bounded" ,
786+ providerCallId : "provider-bounded" ,
787+ provider : "plivo" ,
788+ direction : "outbound" ,
789+ state : "active" ,
790+ from : "+15550000000" ,
791+ to : "+15550000001" ,
792+ startedAt : now ,
793+ transcript : [ ] ,
794+ processedEventIds : callKeys ,
795+ metadata : { } ,
796+ } ) ;
797+ ctx . providerCallIdMap . set ( "provider-bounded" , "call-bounded" ) ;
798+
799+ const result = processEvent ( ctx , {
800+ id : "evt-bounded-new" ,
801+ type : "call.dtmf" ,
802+ callId : "call-bounded" ,
803+ providerCallId : "provider-bounded" ,
804+ timestamp : now + 1 ,
805+ digits : "1" ,
806+ } ) ;
807+
808+ const call = ctx . activeCalls . get ( "call-bounded" ) ;
809+ expect ( result ) . toEqual ( { kind : "processed" } ) ;
810+ expect ( ctx . processedEventIds . size ) . toBe ( MANAGER_REPLAY_KEY_LIMIT ) ;
811+ expect ( ctx . processedEventIds . has ( "manager-0" ) ) . toBe ( false ) ;
812+ expect ( ctx . processedEventIds . has ( "evt-bounded-new" ) ) . toBe ( true ) ;
813+ expect ( call ?. processedEventIds ) . toHaveLength ( MAX_CALL_REPLAY_KEYS ) ;
814+ expect ( call ?. processedEventIds [ 0 ] ) . toBe ( "call-1" ) ;
815+ expect ( call ?. processedEventIds . at ( - 1 ) ) . toBe ( "evt-bounded-new" ) ;
816+ expect (
817+ processEvent ( ctx , {
818+ id : "evt-bounded-new" ,
819+ type : "call.dtmf" ,
820+ callId : "call-bounded" ,
821+ providerCallId : "provider-bounded" ,
822+ timestamp : now + 2 ,
823+ digits : "1" ,
824+ } ) ,
825+ ) . toEqual ( { kind : "ignored" } ) ;
826+ } ) ;
827+
828+ it ( "bounds rejected provider calls while retaining hangup-once behavior" , ( ) => {
829+ const rejectedProviderCallIds = new Map < string , symbol > (
830+ Array . from (
831+ { length : MANAGER_REPLAY_KEY_LIMIT } ,
832+ ( _ , index ) => [ `provider-${ index } ` , Symbol ( `provider-${ index } ` ) ] as const ,
833+ ) ,
834+ ) ;
835+ const { ctx, hangupCalls } = createRejectingInboundContext ( ) ;
836+ ctx . rejectedProviderCallIds = rejectedProviderCallIds ;
837+
838+ processEvent (
839+ ctx ,
840+ createInboundInitiatedEvent ( {
841+ id : "evt-rejected-new" ,
842+ providerCallId : "provider-new" ,
843+ from : "+15552222222" ,
844+ } ) ,
845+ ) ;
846+ processEvent (
847+ ctx ,
848+ createInboundInitiatedEvent ( {
849+ id : "evt-rejected-new-replay" ,
850+ providerCallId : "provider-new" ,
851+ from : "+15552222222" ,
852+ } ) ,
853+ ) ;
854+
855+ expect ( ctx . rejectedProviderCallIds . size ) . toBe ( MANAGER_REPLAY_KEY_LIMIT ) ;
856+ expect ( ctx . rejectedProviderCallIds . has ( "provider-0" ) ) . toBe ( false ) ;
857+ expect ( ctx . rejectedProviderCallIds . has ( "provider-new" ) ) . toBe ( true ) ;
858+ expect ( hangupCalls ) . toHaveLength ( 1 ) ;
859+ } ) ;
860+
774861 it ( "keeps retryable call.error events replayable" , ( ) => {
775862 const now = Date . now ( ) ;
776863 const ctx = createContext ( ) ;
0 commit comments