Skip to content

Commit e745f53

Browse files
committed
fix(voice-call): isolate replaced realtime bridges
1 parent 9dfc877 commit e745f53

2 files changed

Lines changed: 51 additions & 12 deletions

File tree

‎extensions/voice-call/src/webhook/realtime-handler.test.ts‎

Lines changed: 33 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1871,7 +1871,12 @@ describe("RealtimeCallHandler path routing", () => {
18711871

18721872
it("ignores late continuity reset and close from a replaced bridge", async () => {
18731873
const callbacks: RealtimeBridgeRequest[] = [];
1874-
const bridges = [makeBridge(), makeBridge()];
1874+
const oldCloseBridge = vi.fn();
1875+
const replacementCloseBridge = vi.fn();
1876+
const bridges = [
1877+
makeBridge({ close: oldCloseBridge }),
1878+
makeBridge({ close: replacementCloseBridge }),
1879+
];
18751880
const createBridge = vi.fn((request: RealtimeBridgeRequest) => {
18761881
callbacks.push(request);
18771882
const bridge = bridges[callbacks.length - 1];
@@ -1881,11 +1886,15 @@ describe("RealtimeCallHandler path routing", () => {
18811886
return bridge;
18821887
});
18831888
const processEvent = vi.fn();
1889+
const hangupCall = vi.fn(async () => {});
1890+
const sharedCallSid = "CA-continuity-shared";
1891+
const call = makeCallRecord(sharedCallSid);
18841892
const handler = makeHandler(undefined, {
18851893
manager: {
1886-
getCallByProviderCallId: vi.fn((providerCallId: string) => makeCallRecord(providerCallId)),
1894+
getCallByProviderCallId: vi.fn(() => call),
18871895
processEvent,
18881896
},
1897+
provider: { hangupCall },
18891898
realtimeProvider: makeRealtimeProvider(createBridge),
18901899
});
18911900
const oldServer = await startRealtimeServer(handler);
@@ -1897,7 +1906,7 @@ describe("RealtimeCallHandler path routing", () => {
18971906
oldWs.send(
18981907
JSON.stringify({
18991908
event: "start",
1900-
start: { streamSid: "MZ-continuity-old", callSid: "CA-continuity-old" },
1909+
start: { streamSid: "MZ-continuity-old", callSid: sharedCallSid },
19011910
}),
19021911
);
19031912
await waitForRealtimeTest(() => {
@@ -1912,7 +1921,7 @@ describe("RealtimeCallHandler path routing", () => {
19121921
event: "start",
19131922
start: {
19141923
streamSid: "MZ-continuity-replacement",
1915-
callSid: "CA-continuity-replacement",
1924+
callSid: sharedCallSid,
19161925
},
19171926
}),
19181927
);
@@ -1921,6 +1930,9 @@ describe("RealtimeCallHandler path routing", () => {
19211930
});
19221931

19231932
callbacks[1]?.onTranscript?.("user", "Fresh ", false);
1933+
callbacks[0]?.onTranscript?.("user", "stale partial", false);
1934+
callbacks[0]?.onTranscript?.("user", "stale final", true);
1935+
callbacks[0]?.onTranscript?.("assistant", "stale assistant", true);
19241936
callbacks[0]?.onEvent?.({
19251937
direction: "client",
19261938
type: "session.continuity.reset",
@@ -1929,7 +1941,18 @@ describe("RealtimeCallHandler path routing", () => {
19291941
direction: "client",
19301942
type: "session.continuity.reset",
19311943
});
1932-
callbacks[0]?.onClose?.("completed");
1944+
callbacks[0]?.onClose?.("error");
1945+
oldWs.terminate();
1946+
await waitForRealtimeTest(() => {
1947+
expect(oldCloseBridge).toHaveBeenCalledOnce();
1948+
});
1949+
expect(replacementCloseBridge).not.toHaveBeenCalled();
1950+
expect(hangupCall).not.toHaveBeenCalled();
1951+
expect(
1952+
processEvent.mock.calls
1953+
.map(([event]) => event as NormalizedEvent)
1954+
.filter((event) => event.type === "call.ended"),
1955+
).toHaveLength(0);
19331956
callbacks[1]?.onTranscript?.("user", "caller", true);
19341957

19351958
await waitForRealtimeTest(() => {
@@ -1940,6 +1963,11 @@ describe("RealtimeCallHandler path routing", () => {
19401963
.map((event) => (event.type === "call.speech" ? event.transcript : undefined)),
19411964
).toEqual(["Fresh caller"]);
19421965
});
1966+
expect(
1967+
processEvent.mock.calls
1968+
.map(([event]) => event as NormalizedEvent)
1969+
.filter((event) => event.type === "call.assistant-speech"),
1970+
).toHaveLength(0);
19431971
} finally {
19441972
if (
19451973
replacementWs.readyState !== WebSocket.CLOSED &&

‎extensions/voice-call/src/webhook/realtime-handler.ts‎

Lines changed: 18 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -339,7 +339,10 @@ export class RealtimeCallHandler {
339339
private readonly activeBridgesByCallId = new Map<string, ActiveRealtimeVoiceBridge>();
340340
private readonly activeTelephonyClosersByCallId = new Map<
341341
string,
342-
(reason: TelephonyCloseReason) => void
342+
{
343+
owner: ActiveRealtimeVoiceBridge;
344+
close: (reason: TelephonyCloseReason) => void;
345+
}
343346
>();
344347
private readonly partialUserTranscriptsByCallId = new Map<string, string>();
345348
private readonly rawPartialUserTranscriptsByCallId = new Map<string, string>();
@@ -797,6 +800,10 @@ export class RealtimeCallHandler {
797800
},
798801
},
799802
onTranscript: (role, text, isFinal) => {
803+
const owner = nativeConsultOwner.current;
804+
if (owner && !this.isActiveBridgeOwner(callId, owner)) {
805+
return;
806+
}
800807
const turnId = harness.ensureTurn();
801808
const eventType =
802809
role === "assistant"
@@ -979,7 +986,7 @@ export class RealtimeCallHandler {
979986
payload: { reason },
980987
final: true,
981988
});
982-
if (reason !== "error") {
989+
if (reason !== "error" || (owner && !ownsCallState)) {
983990
return;
984991
}
985992
emitCallEnd("error");
@@ -1017,8 +1024,9 @@ export class RealtimeCallHandler {
10171024
});
10181025
this.activeBridgesByCallId.set(callId, session);
10191026
this.activeBridgesByCallId.set(callSid, session);
1020-
this.activeTelephonyClosersByCallId.set(callId, closeTelephony);
1021-
this.activeTelephonyClosersByCallId.set(callSid, closeTelephony);
1027+
const telephonyCloser = { owner: session, close: closeTelephony };
1028+
this.activeTelephonyClosersByCallId.set(callId, telephonyCloser);
1029+
this.activeTelephonyClosersByCallId.set(callSid, telephonyCloser);
10221030
const sendAudioToSession = session.sendAudio.bind(session);
10231031
session.sendAudio = (audio) => {
10241032
if (speechDetector.accept({ rms: calculateMulawRms(audio), peak: 0 })) {
@@ -1059,8 +1067,11 @@ export class RealtimeCallHandler {
10591067

10601068
session.connect().catch((error: unknown) => {
10611069
console.error("[voice-call] Failed to connect realtime bridge:", error);
1070+
const ownsCallState = this.isActiveBridgeOwner(callId, session);
10621071
session.close();
1063-
emitCallEnd("error");
1072+
if (ownsCallState) {
1073+
emitCallEnd("error");
1074+
}
10641075
ws.close(1011, "Failed to connect");
10651076
});
10661077

@@ -1246,8 +1257,8 @@ export class RealtimeCallHandler {
12461257
reason: TelephonyCloseReason,
12471258
): void {
12481259
const closer = this.activeTelephonyClosersByCallId.get(callIdOrSid);
1249-
if (closer) {
1250-
closer(reason);
1260+
if (closer && closer.owner === bridge) {
1261+
closer.close(reason);
12511262
return;
12521263
}
12531264
bridge?.close();

0 commit comments

Comments
 (0)