@vellumai/assistant 0.12.4-dev.202609231620.a15f321 → 0.12.4-dev.202609231715.8da971c

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (27) hide show
  1. package/package.json +1 -1
  2. package/src/calls/__tests__/call-controller.test.ts +18 -0
  3. package/src/calls/__tests__/voice-control-protocol.test.ts +6 -1
  4. package/src/calls/__tests__/voice-leg-coordinator.test.ts +42 -0
  5. package/src/calls/__tests__/voice-session-bridge.test.ts +34 -3
  6. package/src/calls/__tests__/voice-triage-escalate.test.ts +69 -11
  7. package/src/calls/call-controller.ts +3 -1
  8. package/src/calls/voice-control-protocol.ts +9 -4
  9. package/src/calls/voice-escalation-judge.ts +1 -1
  10. package/src/calls/voice-leg-coordinator.ts +15 -4
  11. package/src/calls/voice-session-bridge.ts +28 -20
  12. package/src/calls/voice-triage-escalate.ts +61 -26
  13. package/src/cli/commands/oauth/index.help.ts +8 -2
  14. package/src/cli/commands/oauth/request.test.ts +32 -0
  15. package/src/config/__tests__/platform-identity.test.ts +59 -0
  16. package/src/config/platform-identity.ts +22 -5
  17. package/src/live-voice/__tests__/live-voice-triage-escalate.test.ts +37 -1
  18. package/src/live-voice/__tests__/live-voice-vad.test.ts +34 -2
  19. package/src/live-voice/live-voice-session.ts +10 -8
  20. package/src/notifications/__tests__/assistant-reply-producer.test.ts +13 -0
  21. package/src/oauth/AGENTS.md +2 -0
  22. package/src/oauth/byo-connection.test.ts +28 -0
  23. package/src/persistence/conversation-crud.ts +2 -0
  24. package/src/persistence/conversation-types.ts +6 -2
  25. package/src/runtime/routes/__tests__/oauth-proxy-routes.test.ts +61 -3
  26. package/src/runtime/routes/oauth-proxy-passthrough.test.ts +32 -0
  27. package/src/util/oauth-request-body.test.ts +5 -1
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@vellumai/assistant",
3
- "version": "0.12.4-dev.202609231620.a15f321",
3
+ "version": "0.12.4-dev.202609231715.8da971c",
4
4
  "license": "MIT",
5
5
  "type": "module",
6
6
  "exports": {
@@ -4355,6 +4355,24 @@ describe("call-controller", () => {
4355
4355
  controller.destroy();
4356
4356
  });
4357
4357
 
4358
+ test("a terminal verdict hands off without repeating the streamed bridge", async () => {
4359
+ const bridge = "Let me check that.";
4360
+ const { calls } = scriptLegs([
4361
+ [bridge, " [", "ESCALATE", "]"],
4362
+ ["The answer is 42."],
4363
+ ]);
4364
+ const { relay, controller } = setupController();
4365
+
4366
+ await controller.handleCallerUtterance("What is six times seven?");
4367
+
4368
+ expect(calls).toHaveLength(2);
4369
+ expect(calls[1].routingLeg).toBe("escalated");
4370
+ expect(calls[1].spokenEscalationBridge).toBe(bridge);
4371
+ expect(spokenText(relay)).toBe(`${bridge} The answer is 42.`);
4372
+ expect(relay.sentTokens.filter((t) => t.last)).toHaveLength(1);
4373
+ controller.destroy();
4374
+ });
4375
+
4358
4376
  test("a bare escalate verdict speaks the canned bridge, audio-only", async () => {
4359
4377
  const { calls } = scriptLegs([["[1]"], ["Forty-two."]]);
4360
4378
  const { session, relay, controller } = setupController();
@@ -17,10 +17,13 @@ import {
17
17
  describe("front-door verdict tokens", () => {
18
18
  test("token constants are the expected bracketed forms", () => {
19
19
  expect(HOLD_VERDICT_TOKEN).toBe("[0]");
20
- expect(ESCALATE_VERDICT_TOKEN).toBe("[1]");
20
+ expect(ESCALATE_VERDICT_TOKEN).toBe("[ESCALATE]");
21
21
  });
22
22
 
23
23
  test("stripInternalSpeechMarkers removes both tokens so they are never spoken", () => {
24
+ expect(stripInternalSpeechMarkers("[ESCALATE] Let me check.").trim()).toBe(
25
+ "Let me check.",
26
+ );
24
27
  expect(
25
28
  stripInternalSpeechMarkers("[1] Let me think about that.").trim(),
26
29
  ).toBe("Let me think about that.");
@@ -59,6 +62,7 @@ describe("isIncompleteControlMarkerTail", () => {
59
62
  "[-",
60
63
  "[-1",
61
64
  "[END_CAL",
65
+ "[ESCALAT",
62
66
  "[TASK:ST",
63
67
  "[ASK_GUARDIAN_APPRO",
64
68
  ]) {
@@ -70,6 +74,7 @@ describe("isIncompleteControlMarkerTail", () => {
70
74
  for (const tail of [
71
75
  "[-1]",
72
76
  "[END_CALL]",
77
+ "[ESCALATE]",
73
78
  TASK_STOP_MARKER,
74
79
  "[0] answer",
75
80
  "[-1] look here",
@@ -148,6 +148,48 @@ describe("createFrontDoorLegCoordinator", () => {
148
148
  expect(recorded.escalated.length).toBe(1);
149
149
  });
150
150
 
151
+ test("a terminal verdict hands off once with the already released speech", () => {
152
+ const { coordinator, recorded } = harness();
153
+ const bridge = "I will highlight the Rotate control on your screen.";
154
+ coordinator.push(bridge);
155
+ expect(recorded.answers).toEqual([bridge]);
156
+ coordinator.push(" [");
157
+ coordinator.push("ESCALATE]");
158
+ expect(coordinator.handedOff).toBe(false);
159
+
160
+ expect(coordinator.complete()).toBe(true);
161
+ expect(recorded.bridges).toEqual([
162
+ { spokenBridge: bridge, usesFallback: false, alreadyReleased: true },
163
+ ]);
164
+ expect(recorded.escalated).toEqual([escalatedLegFor(bridge)]);
165
+ expect(recorded.events).not.toContain("overrule");
166
+ coordinator.complete();
167
+ coordinator.push("late text");
168
+ expect(recorded.escalated).toHaveLength(1);
169
+ });
170
+
171
+ test("a cancelled answer with a terminal verdict never hands off", () => {
172
+ let live = true;
173
+ const { coordinator, recorded } = harness({ live: () => live });
174
+ coordinator.push("I will highlight it. [ESCALATE]");
175
+ live = false;
176
+ expect(coordinator.complete()).toBe(false);
177
+ expect(recorded.bridges).toEqual([]);
178
+ expect(recorded.escalated).toEqual([]);
179
+ });
180
+
181
+ test.each(["Select option [1]", "The source is listed as [1]"])(
182
+ "a numbered reference does not start another leg: %s",
183
+ (answer) => {
184
+ const { coordinator, recorded } = harness();
185
+ coordinator.push(answer);
186
+ expect(coordinator.complete()).toBe(false);
187
+ expect(recorded.answers).toEqual([answer]);
188
+ expect(recorded.bridges).toEqual([]);
189
+ expect(recorded.escalated).toEqual([]);
190
+ },
191
+ );
192
+
151
193
  test("a leg that stops mid-bridge hands off on completion with the canned bridge", () => {
152
194
  const { coordinator, recorded } = harness({ language: "es" });
153
195
  coordinator.push("[1]");
@@ -2485,6 +2485,18 @@ describe("cutFrontDoorContentAtVerdict", () => {
2485
2485
  expect(cut?.spokenText).toBe("");
2486
2486
  });
2487
2487
 
2488
+ test("a terminal verdict split across blocks preserves all released speech", () => {
2489
+ const bridge = "Let me check. I will highlight the Rotate control.";
2490
+ const cut = cutFrontDoorContentAtVerdict([
2491
+ { type: "text", text: `${bridge} [` },
2492
+ { type: "text", text: "ESCALATE] " },
2493
+ ]);
2494
+ expect(cut).toEqual({
2495
+ blocks: [{ type: "text", text: bridge }],
2496
+ spokenText: bridge,
2497
+ });
2498
+ });
2499
+
2488
2500
  test("stray verdict tokens inside an answer are stripped, not treated as escalation", () => {
2489
2501
  const cut = cutFrontDoorContentAtVerdict([
2490
2502
  { type: "text", text: "It is Tuesday [0] indeed." },
@@ -2511,7 +2523,7 @@ describe("front-door hub stream gate", () => {
2511
2523
  * `deltas` in order, then ends the leg with `finalEvent`.
2512
2524
  */
2513
2525
  function makeStreamingConversation(
2514
- deltas: string[],
2526
+ deltas: readonly string[],
2515
2527
  finalEvent:
2516
2528
  | "message_complete"
2517
2529
  | "generation_cancelled" = "message_complete",
@@ -2595,6 +2607,22 @@ describe("front-door hub stream gate", () => {
2595
2607
  expect(texts.join("")).toBe("It is Tuesday, and it is sunny.");
2596
2608
  });
2597
2609
 
2610
+ test("a terminal escalation never broadcasts marker fragments", async () => {
2611
+ makeStreamingConversation([
2612
+ "I will highlight it.",
2613
+ " [",
2614
+ "ESC",
2615
+ "ALATE",
2616
+ "]",
2617
+ ]);
2618
+
2619
+ const texts = await collectBroadcastText(() =>
2620
+ startVoiceTurn({ ...makeTurnOptions(), routingLeg: "front-door" }),
2621
+ );
2622
+
2623
+ expect(texts).toEqual(["I will highlight it.", " "]);
2624
+ });
2625
+
2598
2626
  test("an answer waits on the escalation judge before reaching the hub", async () => {
2599
2627
  let openGate!: () => void;
2600
2628
  judgeEscalationGate = new Promise<void>((resolve) => {
@@ -2618,9 +2646,12 @@ describe("front-door hub stream gate", () => {
2618
2646
  }
2619
2647
  });
2620
2648
 
2621
- test("an overruled answer never reaches the hub", async () => {
2649
+ test.each([
2650
+ { deltas: ["Yeah okay, ", "I'll do it."] },
2651
+ { deltas: ["[ASK_GUARDIAN:"] },
2652
+ ])("an overruled answer never reaches the hub: %j", async ({ deltas }) => {
2622
2653
  judgeEscalationVerdict = true;
2623
- makeStreamingConversation(["Yeah okay, ", "I'll do it."]);
2654
+ makeStreamingConversation(deltas);
2624
2655
 
2625
2656
  const texts = await collectBroadcastText(async () => {
2626
2657
  const handle = await startVoiceTurn({
@@ -393,13 +393,14 @@ describe("spokenBridgeText", () => {
393
393
  expect(spokenBridgeText(ESCALATE_VERDICT_TOKEN)).toBe("");
394
394
  });
395
395
 
396
- test("is empty when the output does not lead with the verdict (an answer)", () => {
397
- // A stray token later in an answer is not an escalation under the
398
- // verdict-first protocol.
396
+ test("is empty for an answer with no leading or terminal verdict", () => {
399
397
  expect(spokenBridgeText("It is Tuesday.")).toBe("");
400
- expect(spokenBridgeText(`Half an answer ${ESCALATE_VERDICT_TOKEN}`)).toBe(
401
- "",
402
- );
398
+ expect(spokenBridgeText("The token [1] is reserved.")).toBe("");
399
+ });
400
+
401
+ test("a terminal verdict preserves every sentence already released", () => {
402
+ const bridge = "Let me check. I will highlight the control.";
403
+ expect(spokenBridgeText(`${bridge} [ESCALATE]`)).toBe(bridge);
403
404
  });
404
405
  });
405
406
 
@@ -419,7 +420,7 @@ describe("needsFallbackBridge", () => {
419
420
 
420
421
  describe("classifyFrontDoorLeading", () => {
421
422
  test("pending while the stream could still become a verdict token", () => {
422
- for (const leading of ["", "[", "[1"]) {
423
+ for (const leading of ["", "[", "[1", "[E", "[ESCALATE"]) {
423
424
  expect(classifyFrontDoorLeading(leading, false)).toBe("pending");
424
425
  }
425
426
  expect(classifyFrontDoorLeading("[0", true)).toBe("pending");
@@ -435,6 +436,10 @@ describe("classifyFrontDoorLeading", () => {
435
436
  });
436
437
 
437
438
  test("escalate on the leading escalate token", () => {
439
+ expect(classifyFrontDoorLeading("[ESCALATE]", false)).toBe("escalate");
440
+ expect(classifyFrontDoorLeading("[ESCALATE] Let me check.", true)).toBe(
441
+ "escalate",
442
+ );
438
443
  expect(classifyFrontDoorLeading("[1]", false)).toBe("escalate");
439
444
  expect(classifyFrontDoorLeading("[1] Let me check.", true)).toBe(
440
445
  "escalate",
@@ -544,6 +549,38 @@ describe("createFrontDoorVerdictMachine", () => {
544
549
  expect(undecided.finish()).toEqual({ kind: "done" });
545
550
  });
546
551
 
552
+ test("a terminal verdict recovers at completion across every delta boundary", () => {
553
+ const bridge = "I will highlight the Rotate control on your screen.";
554
+ const text = `${bridge} [ESCALATE] \n`;
555
+ for (let split = 1; split < text.length; split++) {
556
+ const machine = createFrontDoorVerdictMachine(false);
557
+ expect(machine.push(text.slice(0, split)).kind).toBe("answer");
558
+ expect(machine.push(text.slice(split)).kind).toBe("answer");
559
+ expect(machine.finish()).toEqual({ kind: "terminal-escalate", bridge });
560
+ expect(machine.finish()).toEqual({ kind: "done" });
561
+ expect(machine.push("late")).toEqual({ kind: "done" });
562
+ }
563
+ });
564
+
565
+ test.each([
566
+ "Select option [1]",
567
+ "The source is listed as [1]",
568
+ "The source supports this statement. [1]",
569
+ "I will highlight it. [1]",
570
+ "The token [ESCALATE] is reserved.",
571
+ "I will highlight it. [ESCALATE",
572
+ "The token is `[ESCALATE]`",
573
+ "The token [1] is reserved.",
574
+ "I will highlight it. [1",
575
+ "I will highlight it.[1]",
576
+ "The token is `[1]`",
577
+ "I will highlight it. [0]",
578
+ ])("does not recover a nonterminal or incomplete verdict: %s", (text) => {
579
+ const machine = createFrontDoorVerdictMachine(false);
580
+ machine.push(text);
581
+ expect(machine.finish()).toEqual({ kind: "done" });
582
+ });
583
+
547
584
  test("hold is a step only when the leg was taught the token", () => {
548
585
  expect(kinds([HOLD_VERDICT_TOKEN], true)).toEqual(["hold"]);
549
586
  expect(kinds([`${HOLD_VERDICT_TOKEN} trailing`], true)).toEqual(["hold"]);
@@ -627,15 +664,36 @@ describe("createFrontDoorStreamGate", () => {
627
664
  expect(release(["[1] Let me check"], { finish: false })).toEqual([]);
628
665
  });
629
666
 
667
+ test("recovering a terminal verdict never releases the answer twice", () => {
668
+ const deltas = ["I will highlight it.", " [", "ESCALATE", "]"];
669
+ expect(release(deltas)).toEqual(["I will highlight it.", " "]);
670
+ });
671
+
672
+ test("terminal verdict fragments never escape at any delta boundary", () => {
673
+ const text = "I will highlight it. [ESCALATE]";
674
+ for (let split = 1; split < text.length; split++) {
675
+ expect(release([text.slice(0, split), text.slice(split)]).join("")).toBe(
676
+ "I will highlight it. ",
677
+ );
678
+ }
679
+ });
680
+
681
+ test("an incomplete marker is flushed only on normal completion", () => {
682
+ expect(release(["An opening bracket ["], { finish: false })).toEqual([
683
+ "An opening bracket ",
684
+ ]);
685
+ expect(release(["An opening bracket ["]).join("")).toBe(
686
+ "An opening bracket [",
687
+ );
688
+ });
689
+
630
690
  test("a hold verdict releases nothing", () => {
631
691
  expect(release([HOLD_VERDICT_TOKEN], { holdEnabled: true })).toEqual([]);
632
692
  });
633
693
 
634
- test("the hold token is ordinary text on a leg that was never taught it", () => {
635
- // A non-speculative leg's prompt has no hold branch, so its output must
636
- // not be swallowed by a token it could only have parroted.
694
+ test("the hold token is stripped without discarding a non-speculative answer", () => {
637
695
  expect(release([`${HOLD_VERDICT_TOKEN} is the index.`])).toEqual([
638
- `${HOLD_VERDICT_TOKEN} is the index.`,
696
+ " is the index.",
639
697
  ]);
640
698
  });
641
699
 
@@ -1158,7 +1158,9 @@ export class CallController {
1158
1158
  speakFixedPhrase(bridge.spokenBridge, bridge.language);
1159
1159
  return;
1160
1160
  }
1161
- fullResponseText += `${bridge.spokenBridge} `;
1161
+ if (!bridge.alreadyReleased) {
1162
+ fullResponseText += `${bridge.spokenBridge} `;
1163
+ }
1162
1164
  flushSafeText(fullResponseText, { force: true });
1163
1165
  // Force-synthesize the bridge now. On the synthesized-TTS path text is
1164
1166
  // held in pendingSynthText until a sentence boundary, so an
@@ -28,7 +28,12 @@ export const TASK_UPDATE_SILENT_MARKER = "[TASK_UPDATE:SILENT]";
28
28
  * markers they are swallowed before reaching TTS — never spoken aloud.
29
29
  */
30
30
  export const HOLD_VERDICT_TOKEN = "[0]";
31
- export const ESCALATE_VERDICT_TOKEN = "[1]";
31
+ export const ESCALATE_VERDICT_TOKEN = "[ESCALATE]";
32
+ const LEGACY_ESCALATE_VERDICT_TOKEN = "[1]";
33
+ export const ESCALATE_VERDICT_TOKENS = [
34
+ ESCALATE_VERDICT_TOKEN,
35
+ LEGACY_ESCALATE_VERDICT_TOKEN,
36
+ ] as const;
32
37
 
33
38
  /**
34
39
  * Room-minimize token, kept in the marker table so it is stripped from
@@ -93,7 +98,6 @@ const CALL_OPENING_ACK_MARKER_REGEX = /\[CALL_OPENING_ACK\]/g;
93
98
  const END_CALL_MARKER_REGEX = /\[END_CALL\]/g;
94
99
  const TASK_STOP_MARKER_REGEX = /\[TASK:STOP\]/g;
95
100
  const HOLD_VERDICT_TOKEN_REGEX = /\[0\]/g;
96
- const ESCALATE_VERDICT_TOKEN_REGEX = /\[1\]/g;
97
101
  const MINIMIZE_ROOM_MARKER_REGEX = /\[-1\]/g;
98
102
  const MUTE_MARKER_REGEX = /\[MUTE(?::\s*[^\]]*)?\]/g;
99
103
  const UPDATES_MARKER_REGEX = /\[UPDATES:\s*[^\]]*\]/g;
@@ -223,7 +227,8 @@ export function stripInternalSpeechMarkers(text: string): string {
223
227
  .replace(TASK_STOP_MARKER_REGEX, "")
224
228
  .replaceAll(TASK_UPDATE_SILENT_MARKER, "")
225
229
  .replace(HOLD_VERDICT_TOKEN_REGEX, "")
226
- .replace(ESCALATE_VERDICT_TOKEN_REGEX, "")
230
+ .replaceAll(ESCALATE_VERDICT_TOKEN, "")
231
+ .replaceAll(LEGACY_ESCALATE_VERDICT_TOKEN, "")
227
232
  .replace(MINIMIZE_ROOM_MARKER_REGEX, "")
228
233
  .replace(MUTE_MARKER_REGEX, "")
229
234
  .replace(UPDATES_MARKER_REGEX, "")
@@ -253,7 +258,7 @@ const CONTROL_MARKER_STRINGS = [
253
258
  TASK_STOP_MARKER,
254
259
  TASK_UPDATE_SILENT_MARKER,
255
260
  "[0]",
256
- "[1]",
261
+ ...ESCALATE_VERDICT_TOKENS,
257
262
  "[-1]",
258
263
  MUTE_MARKER,
259
264
  MUTE_MARKER_PREFIX,
@@ -58,7 +58,7 @@ export interface EscalationJudgement {
58
58
  }
59
59
 
60
60
  const ESCALATION_QUESTION =
61
- "The voice assistant answering right now has NO tools and no access to the user's accounts, files, messages, calendar, screen, apps, the web, or saved memories. Would a correct reply to what the caller just said require doing something it cannot do from here: taking an action (sending, scheduling, creating, editing, changing, opening, calling), looking up live or current information, or retrieving a personal fact that is not already stated in the recent conversation? Answer yes if the caller is asking for, or agreeing to, any such action, even vaguely or via a short confirmation of an earlier offer. Answer no for conversation, opinions, general knowledge, and questions answerable from the recent conversation.";
61
+ "The voice assistant answering right now has NO tools and no access to the user's accounts, files, messages, calendar, apps, the web, or saved memories. It can start or refresh screen sharing and inspect shared screen images through built-in call controls. Answer no when the caller only asks to share their screen, look at their screen, or describe what is visible; obtaining a screen view does not require escalation. Requests to click, type, edit, or draw/highlight on the screen still require escalation. Would a correct reply to what the caller just said require doing something it cannot do from here: taking an action (sending, scheduling, creating, editing, changing, opening, calling), looking up live or current information, or retrieving a personal fact that is not already stated in the recent conversation? Answer yes if the caller is asking for, or agreeing to, any such action, even vaguely or via a short confirmation of an earlier offer. Answer no for conversation, opinions, general knowledge, and questions answerable from the recent conversation.";
62
62
 
63
63
  function textOf(message: Message): string {
64
64
  return message.content
@@ -33,7 +33,10 @@ import {
33
33
  /** The phrase spoken across the hand-off, as {@link resolveSpokenEscalationBridge} settles it. */
34
34
  export type SpokenEscalationBridge = ReturnType<
35
35
  typeof resolveSpokenEscalationBridge
36
- >;
36
+ > & {
37
+ /** The answer path already emitted this text; only flush pending speech. */
38
+ alreadyReleased?: boolean;
39
+ };
37
40
 
38
41
  /** The leg that answers for real after the front-door leg handed off. */
39
42
  export interface EscalatedLeg {
@@ -169,8 +172,8 @@ export function createFrontDoorLegCoordinator(options: {
169
172
  };
170
173
 
171
174
  const handOff = (
172
- cappedBridge: string,
173
- opts: { overruled?: boolean } = {},
175
+ bridgeText: string,
176
+ opts: { overruled?: boolean; alreadyReleased?: boolean } = {},
174
177
  ): void => {
175
178
  if (handedOff || !host.isLive()) {
176
179
  return;
@@ -182,7 +185,13 @@ export function createFrontDoorLegCoordinator(options: {
182
185
  } else {
183
186
  host.abortLeg();
184
187
  }
185
- const bridge = resolveSpokenEscalationBridge(cappedBridge, host.language());
188
+ const bridge: SpokenEscalationBridge = opts.alreadyReleased
189
+ ? {
190
+ spokenBridge: bridgeText,
191
+ usesFallback: false,
192
+ alreadyReleased: true,
193
+ }
194
+ : resolveSpokenEscalationBridge(bridgeText, host.language());
186
195
  host.speakBridge(bridge);
187
196
  // The bridge is the turn's spoken acknowledgement: narration keeps its
188
197
  // minimum gap from it rather than following it back to back.
@@ -299,6 +308,8 @@ export function createFrontDoorLegCoordinator(options: {
299
308
  const step = verdict.finish();
300
309
  if (step.kind === "bridge") {
301
310
  handOff(step.bridge);
311
+ } else if (step.kind === "terminal-escalate") {
312
+ handOff(step.bridge, { alreadyReleased: true });
302
313
  }
303
314
  }
304
315
  return handedOff;
@@ -73,7 +73,7 @@ import {
73
73
  import {
74
74
  CALL_OPENING_MARKER,
75
75
  CALL_VERIFICATION_COMPLETE_MARKER,
76
- ESCALATE_VERDICT_TOKEN,
76
+ ESCALATE_VERDICT_TOKENS,
77
77
  HOLD_VERDICT_TOKEN,
78
78
  stripInternalSpeechMarkers,
79
79
  terminalControlMarkerLength,
@@ -87,6 +87,7 @@ import {
87
87
  ESCALATION_CONTINUATION_CONTENT,
88
88
  frontDoorCapabilityDigest,
89
89
  frontDoorDecisionRule,
90
+ leadingEscalationToken,
90
91
  spokenBridgeText,
91
92
  type VoiceRoutingLeg,
92
93
  } from "./voice-triage-escalate.js";
@@ -840,30 +841,32 @@ function trimOuterTextEdges(blocks: ContentBlock[]): ContentBlock[] {
840
841
  * `ESCALATE_VERDICT_TOKEN` reduces to a single text block holding the
841
842
  * capped bridge; empty spoken text means the caller heard only the canned
842
843
  * fallback bridge, which is audio-only and never a transcript row, so the
843
- * caller should delete the row. Stray verdict tokens elsewhere in an
844
- * answer were never spoken (the live gate strips them) and are stripped
845
- * from the persisted text to match.
844
+ * caller should delete the row. A terminal escalation keeps all speech
845
+ * already released before the verdict. Other stray verdict tokens were
846
+ * never spoken and are stripped from the persisted text to match.
846
847
  */
847
848
  export function cutFrontDoorContentAtVerdict(
848
849
  blocks: ContentBlock[],
849
850
  ): { blocks: ContentBlock[]; spokenText: string } | null {
850
851
  const joinedText = joinedTextOfBlocks(blocks);
851
- if (joinedText.trimStart().startsWith(ESCALATE_VERDICT_TOKEN)) {
852
- const spokenText = spokenBridgeText(joinedText);
852
+ const spokenText = spokenBridgeText(joinedText);
853
+ if (
854
+ spokenText.length > 0 ||
855
+ leadingEscalationToken(joinedText) !== undefined
856
+ ) {
853
857
  return {
854
858
  blocks: spokenText.length > 0 ? [{ type: "text", text: spokenText }] : [],
855
859
  spokenText,
856
860
  };
857
861
  }
858
862
  if (
859
- !joinedText.includes(ESCALATE_VERDICT_TOKEN) &&
863
+ !ESCALATE_VERDICT_TOKENS.some((token) => joinedText.includes(token)) &&
860
864
  !joinedText.includes(HOLD_VERDICT_TOKEN)
861
865
  ) {
862
866
  return null;
863
867
  }
864
868
  const kept = stripMarkersFromBlocks(blocks);
865
- const spokenText = joinedTextOfBlocks(kept).trim();
866
- return { blocks: kept, spokenText };
869
+ return { blocks: kept, spokenText: joinedTextOfBlocks(kept).trim() };
867
870
  }
868
871
 
869
872
  // ---------------------------------------------------------------------------
@@ -1811,6 +1814,20 @@ export async function startVoiceTurn(
1811
1814
  ? createFrontDoorStreamGate(opts.unifiedVerdict === true)
1812
1815
  : null;
1813
1816
 
1817
+ const broadcastFrontDoorText = (
1818
+ msg: Extract<AssistantEvent, { type: "assistant_text_delta" }>,
1819
+ ): void => {
1820
+ // Answer text waits for the judge, including text flushed at completion.
1821
+ if (
1822
+ frontDoorStreamGate?.answering &&
1823
+ !escalationJudgeSettled &&
1824
+ hubHold === null
1825
+ ) {
1826
+ hubHold = [];
1827
+ }
1828
+ emitHubEvent(msg);
1829
+ };
1830
+
1814
1831
  /**
1815
1832
  * Broadcast one agent-loop event to hub subscribers, holding a front-door
1816
1833
  * leg's control-plane text back at the boundary rather than emitting it and
@@ -1825,16 +1842,7 @@ export async function startVoiceTurn(
1825
1842
  }
1826
1843
  const released = frontDoorStreamGate.push(msg.text);
1827
1844
  if (released.length > 0) {
1828
- // Answer text while the escalation judge is out: hold it, and every
1829
- // leg event after it, until the verdict says the caller hears it.
1830
- if (
1831
- frontDoorStreamGate.answering &&
1832
- !escalationJudgeSettled &&
1833
- hubHold === null
1834
- ) {
1835
- hubHold = [];
1836
- }
1837
- emitHubEvent({ ...msg, text: released });
1845
+ broadcastFrontDoorText({ ...msg, text: released });
1838
1846
  }
1839
1847
  };
1840
1848
 
@@ -2150,7 +2158,7 @@ export async function startVoiceTurn(
2150
2158
  // never hands off, and correspondingly never flushes.
2151
2159
  const trailing = frontDoorStreamGate.finish();
2152
2160
  if (trailing.length > 0) {
2153
- broadcastMessage({
2161
+ broadcastFrontDoorText({
2154
2162
  type: "assistant_text_delta",
2155
2163
  text: trailing,
2156
2164
  ...(reservedAssistantRowId !== null