Skip to content

Open-source Grok Bot alternative with a virtual machine that bots can use

fix(live): mask secrets in the bot's spoken lines as they are said

AdityaUmalebbf0e0eb75b2
diff --git a/packages/phone-protocol/src/live-call.ts b/packages/phone-protocol/src/live-call.ts
index 377f525be4..02d0dea101 100644
--- a/packages/phone-protocol/src/live-call.ts
+++ b/packages/phone-protocol/src/live-call.ts
@@ -85,23 +85,29 @@ const MAX_SPOKEN_LINES = 2_000;
* line per stretch of speech, for the call record. A delta without timing
* continues the current line. */
export class SpokenLines {
- private lines: Array<{ at: number; fragments: string[]; endMs: number }> = [];
+ private lines: Array<{ at: number; text: string; endMs: number }> = [];
+ private readonly scrub: (text: string) => string;
+
+ /** `scrub` masks each line as it grows, while anything it masks is still known to it. */
+ constructor(scrub: (text: string) => string = (text) => text) {
+ this.scrub = scrub;
+ }
add(text: string, at: number, startMs: number, endMs: number): void {
if (!text) return;
const timed = Number.isFinite(startMs) && Number.isFinite(endMs);
const last = this.lines.at(-1);
if (last && (!timed || startMs <= last.endMs + SPOKEN_LINE_GAP_MS)) {
- last.fragments.push(text);
+ last.text = this.scrub(last.text + text);
if (timed) last.endMs = Math.max(last.endMs, endMs);
return;
}
if (this.lines.length >= MAX_SPOKEN_LINES) return;
- this.lines.push({ at, fragments: [text], endMs: timed ? endMs : Number.NEGATIVE_INFINITY });
+ this.lines.push({ at, text: this.scrub(text), endMs: timed ? endMs : Number.NEGATIVE_INFINITY });
}
list(): LiveSpokenLine[] {
- return this.lines.map((line) => ({ at: line.at, text: joinFragments(line.fragments) })).filter((line) => line.text);
+ return this.lines.map((line) => ({ at: line.at, text: joinFragments([line.text]) })).filter((line) => line.text);
}
}
diff --git a/server/live-call-controller.test.ts b/server/live-call-controller.test.ts
index 5d39a848b8..441dda5724 100644
--- a/server/live-call-controller.test.ts
+++ b/server/live-call-controller.test.ts
@@ -9,6 +9,7 @@ import { LIVE_CUSTOM_REFUSAL } from "./live-access.ts";
import { LIVE_NEEDS_AZURE, LIVE_NEEDS_KEY, LiveSessionError, type LiveEndpoint } from "./live-call.ts";
import { FishRelays, FISH_AGENT_VOICE } from "./live-fish.ts";
import type { LiveElevenLabsSession, LiveFishSessionToken, LiveGeminiSession } from "../shared/wire.ts";
+import { addHeldSecretScrubber, HELD_SECRET_MASK } from "./redact.ts";
import type { RequestAuth } from "./request-auth.ts";
import type { Message, StoreChange } from "./store.ts";
@@ -1712,6 +1713,23 @@ describe("LiveCallController record", () => {
expect(JSON.stringify(record)).not.toContain("thanks");
});
+ it("masks a held code the bot read aloud when it is said, so it stays masked after the code is forgotten", async () => {
+ const code = "Q7XK-24LM";
+ const forget = addHeldSecretScrubber((text) => text.replaceAll(code, HELD_SECRET_MASK));
+ const t = setup({ recordCall: vi.fn() });
+ try {
+ await t.start();
+ t.socket().receive({ type: "session.output_transcript.delta", delta: "Your code is Q7XK", start_ms: 1_000, end_ms: 1_500 });
+ t.socket().receive({ type: "session.output_transcript.delta", delta: "-24LM.", start_ms: 1_500, end_ms: 2_000 });
+ } finally {
+ forget();
+ }
+ t.socket().drop();
+ const record = recorded(t)[0]!.record;
+ expect(record.spoken).toEqual([{ at: expect.any(Number), text: `Your code is ${HELD_SECRET_MASK}.` }]);
+ expect(JSON.stringify(record)).not.toContain("24LM");
+ });
+
it("counts wall-clock seconds when OpenAI sent none", async () => {
const t = setup({ recordCall: vi.fn() });
await t.start();
diff --git a/server/live-call-controller.ts b/server/live-call-controller.ts
index e8e68a3252..78990554f3 100644
--- a/server/live-call-controller.ts
+++ b/server/live-call-controller.ts
@@ -10,6 +10,7 @@ import type { LiveCallRecord, LiveCallState, LiveClient, LiveElevenLabsTransport
import { FISH_AGENT_VOICE } from "./live-fish.ts";
import { LIVE_NEEDS_KEY, liveAuthHeaders, liveCallSummaryLine, liveProviderName, LiveSessionError, liveVoice, spokenHangUp, type LiveEndpoint } from "./live-call.ts";
import { LIVE_CUSTOM_REFUSAL } from "./live-access.ts";
+import { redactSecretsInText } from "./redact.ts";
import type { RequestAuth } from "./request-auth.ts";
import type { Message, StoreChange } from "./store.ts";
@@ -296,7 +297,7 @@ export class LiveCallController {
socket: null,
attached: false,
transcript: new LiveTranscript(),
- spoken: new SpokenLines(),
+ spoken: new SpokenLines(redactSecretsInText),
heardThroughMs: 0,
lastDecision: null,
eventCounter: 0,
diff --git a/shared/live-call.test.ts b/shared/live-call.test.ts
index 3002efdda0..8d1fd3e6be 100644
--- a/shared/live-call.test.ts
+++ b/shared/live-call.test.ts
@@ -25,6 +25,16 @@ describe("SpokenLines", () => {
lines.add(" ", 10, 20_000, 20_100);
expect(lines.list()).toEqual([{ at: 2, text: "Sure, on it." }, { at: 9, text: "Done." }]);
});
+
+ it("scrubs each line as it grows, so a word split across deltas is masked and stays masked", () => {
+ let known = true;
+ const lines = new SpokenLines((text) => (known ? text.replaceAll("4417", "****") : text));
+ lines.add("The code is 44", 1, 0, 300);
+ lines.add("17.", 2, 300, 600);
+ known = false;
+ lines.add(" Anything else?", 3, 600, 900);
+ expect(lines.list()).toEqual([{ at: 1, text: "The code is ****. Anything else?" }]);
+ });
});
describe("LiveTranscript", () => {