@vellumai/assistant 0.11.9-dev.202609080045.119baa5 → 0.11.9-dev.202609080319.eb4f1b0
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.
- package/package.json +1 -1
- package/src/calls/__tests__/voice-session-bridge.test.ts +166 -0
- package/src/calls/voice-session-bridge.ts +91 -0
- package/src/daemon/__tests__/turn-actor.test.ts +68 -0
- package/src/daemon/conversation-messaging.ts +3 -2
- package/src/daemon/conversation-surfaces.ts +3 -8
- package/src/daemon/conversation.ts +36 -6
- package/src/daemon/turn-actor.ts +46 -0
- package/src/live-voice/__tests__/live-voice-session-guardian.test.ts +127 -0
- package/src/live-voice/live-voice-connection.ts +14 -1
- package/src/live-voice/live-voice-session-manager.ts +10 -1
- package/src/live-voice/live-voice-session.ts +119 -28
- package/src/runtime/http-server.ts +23 -0
package/package.json
CHANGED
|
@@ -310,6 +310,7 @@ interface FakeTurnState {
|
|
|
310
310
|
assistantId: string | undefined;
|
|
311
311
|
callSessionId: string | undefined;
|
|
312
312
|
trustContext: unknown;
|
|
313
|
+
actorPrincipalId: string | undefined;
|
|
313
314
|
commandIntent: unknown;
|
|
314
315
|
turnChannelContext: unknown;
|
|
315
316
|
turnInterfaceContext: unknown;
|
|
@@ -332,6 +333,8 @@ function wireTurnState(
|
|
|
332
333
|
const conv = fake as FakeConversation & {
|
|
333
334
|
assistantId?: string;
|
|
334
335
|
trustContext?: unknown;
|
|
336
|
+
currentTurnSourceActorPrincipalId?: string;
|
|
337
|
+
currentTurnActorStampGeneration?: number;
|
|
335
338
|
commandIntent?: unknown;
|
|
336
339
|
channelCapabilities?: unknown;
|
|
337
340
|
voiceCallControlPrompt?: string;
|
|
@@ -341,6 +344,22 @@ function wireTurnState(
|
|
|
341
344
|
conv.assistantId = initial.assistantId;
|
|
342
345
|
conv.callSessionId = initial.callSessionId;
|
|
343
346
|
conv.trustContext = initial.trustContext;
|
|
347
|
+
// The real Conversation counts every write to the actor stamp behind an
|
|
348
|
+
// accessor (see `currentTurnActorStampGeneration`), which is what lets a
|
|
349
|
+
// turn tell its own stamp from a concurrent writer's. The fake counts them
|
|
350
|
+
// the same way, so the bridge's check is exercised here rather than being
|
|
351
|
+
// trivially true against a plain property.
|
|
352
|
+
let actorPrincipal = initial.actorPrincipalId;
|
|
353
|
+
conv.currentTurnActorStampGeneration = 0;
|
|
354
|
+
Object.defineProperty(conv, "currentTurnSourceActorPrincipalId", {
|
|
355
|
+
configurable: true,
|
|
356
|
+
get: () => actorPrincipal,
|
|
357
|
+
set: (value: string | undefined) => {
|
|
358
|
+
actorPrincipal = value;
|
|
359
|
+
conv.currentTurnActorStampGeneration =
|
|
360
|
+
(conv.currentTurnActorStampGeneration ?? 0) + 1;
|
|
361
|
+
},
|
|
362
|
+
});
|
|
344
363
|
conv.commandIntent = initial.commandIntent;
|
|
345
364
|
conv.channelCapabilities = initial.channelCapabilities;
|
|
346
365
|
conv.voiceCallControlPrompt = initial.voiceCallControlPrompt;
|
|
@@ -374,6 +393,7 @@ function wireTurnState(
|
|
|
374
393
|
assistantId: conv.assistantId,
|
|
375
394
|
callSessionId: conv.callSessionId,
|
|
376
395
|
trustContext: conv.trustContext,
|
|
396
|
+
actorPrincipalId: conv.currentTurnSourceActorPrincipalId,
|
|
377
397
|
commandIntent: conv.commandIntent,
|
|
378
398
|
turnChannelContext,
|
|
379
399
|
turnInterfaceContext,
|
|
@@ -393,6 +413,7 @@ function makeWinnerState(): FakeTurnState {
|
|
|
393
413
|
assistantId: "assistant-winner",
|
|
394
414
|
callSessionId: "session-winner",
|
|
395
415
|
trustContext: { sourceChannel: "imessage", trustClass: "trusted_contact" },
|
|
416
|
+
actorPrincipalId: "principal-winner",
|
|
396
417
|
commandIntent: undefined,
|
|
397
418
|
turnChannelContext: {
|
|
398
419
|
userMessageChannel: "imessage",
|
|
@@ -1489,6 +1510,103 @@ describe("startVoiceTurn queued-message drain race", () => {
|
|
|
1489
1510
|
});
|
|
1490
1511
|
});
|
|
1491
1512
|
|
|
1513
|
+
describe("startVoiceTurn actor principal", () => {
|
|
1514
|
+
// The turn's actor is what host proxies match connected desktop clients
|
|
1515
|
+
// against (`pickSameUserAutoResolve`). A turn that carries none matches no
|
|
1516
|
+
// client, so every computer-use call it makes is refused however healthy
|
|
1517
|
+
// the connected client is.
|
|
1518
|
+
|
|
1519
|
+
test("stamps the caller's actor for the duration of the turn", async () => {
|
|
1520
|
+
const statesAtPersist: FakeTurnState[] = [];
|
|
1521
|
+
const fake = makeFakeConversation({
|
|
1522
|
+
processing: false,
|
|
1523
|
+
onPersist: () => {
|
|
1524
|
+
statesAtPersist.push(readState());
|
|
1525
|
+
},
|
|
1526
|
+
});
|
|
1527
|
+
const readState = wireTurnState(fake.conversation, {});
|
|
1528
|
+
fakeConversation = fake.conversation;
|
|
1529
|
+
|
|
1530
|
+
await startVoiceTurn({
|
|
1531
|
+
...makeTurnOptions(undefined, "conv-actor-stamp"),
|
|
1532
|
+
actorPrincipalId: "principal-guardian",
|
|
1533
|
+
});
|
|
1534
|
+
|
|
1535
|
+
expect(statesAtPersist.length).toBe(1);
|
|
1536
|
+
expect(statesAtPersist[0]!.actorPrincipalId).toBe("principal-guardian");
|
|
1537
|
+
});
|
|
1538
|
+
|
|
1539
|
+
/**
|
|
1540
|
+
* A phone caller is whoever dialled in. Resolving them to a desktop client
|
|
1541
|
+
* would hand an inbound caller the owner's machine, so the telephony path
|
|
1542
|
+
* passes no actor and the turn must not invent one.
|
|
1543
|
+
*/
|
|
1544
|
+
test("a turn with no caller actor leaves the conversation without one", async () => {
|
|
1545
|
+
const statesAtPersist: FakeTurnState[] = [];
|
|
1546
|
+
const fake = makeFakeConversation({
|
|
1547
|
+
processing: false,
|
|
1548
|
+
onPersist: () => {
|
|
1549
|
+
statesAtPersist.push(readState());
|
|
1550
|
+
},
|
|
1551
|
+
});
|
|
1552
|
+
const readState = wireTurnState(fake.conversation, {});
|
|
1553
|
+
fakeConversation = fake.conversation;
|
|
1554
|
+
|
|
1555
|
+
await startVoiceTurn(makeTurnOptions(undefined, "conv-actor-absent"));
|
|
1556
|
+
|
|
1557
|
+
expect(statesAtPersist.length).toBe(1);
|
|
1558
|
+
expect(statesAtPersist[0]!.actorPrincipalId).toBeUndefined();
|
|
1559
|
+
});
|
|
1560
|
+
|
|
1561
|
+
/** The stamp is the turn's, so it goes when the turn does. */
|
|
1562
|
+
test("releases the actor when the turn ends", async () => {
|
|
1563
|
+
const fake = makeFakeConversation({ processing: false });
|
|
1564
|
+
const readState = wireTurnState(fake.conversation, {});
|
|
1565
|
+
fakeConversation = fake.conversation;
|
|
1566
|
+
|
|
1567
|
+
const handle = await startVoiceTurn({
|
|
1568
|
+
...makeTurnOptions(undefined, "conv-actor-release"),
|
|
1569
|
+
actorPrincipalId: "principal-guardian",
|
|
1570
|
+
});
|
|
1571
|
+
handle.abort();
|
|
1572
|
+
await flushMicrotasks();
|
|
1573
|
+
|
|
1574
|
+
expect(readState().actorPrincipalId).toBeUndefined();
|
|
1575
|
+
});
|
|
1576
|
+
|
|
1577
|
+
/**
|
|
1578
|
+
* The release is not unconditional. `runAgentLoopImpl` gives up the
|
|
1579
|
+
* processing claim before the turn-boundary commit is awaited, so a retry
|
|
1580
|
+
* can take the conversation and stamp its own actor while this turn is
|
|
1581
|
+
* still unwinding. The retry route installs no auth-context fallback, so a
|
|
1582
|
+
* clear on the way out would leave it with no actor at all and its
|
|
1583
|
+
* host-proxy calls refused.
|
|
1584
|
+
*/
|
|
1585
|
+
test("does not clear an actor another turn stamped while this one unwound", async () => {
|
|
1586
|
+
let conv: { currentTurnSourceActorPrincipalId?: string } | null = null;
|
|
1587
|
+
const fake = makeFakeConversation({
|
|
1588
|
+
processing: false,
|
|
1589
|
+
runAgentLoop: async () => {
|
|
1590
|
+
conv!.currentTurnSourceActorPrincipalId = "principal-retry";
|
|
1591
|
+
},
|
|
1592
|
+
});
|
|
1593
|
+
conv = fake.conversation as {
|
|
1594
|
+
currentTurnSourceActorPrincipalId?: string;
|
|
1595
|
+
};
|
|
1596
|
+
const readState = wireTurnState(fake.conversation, {});
|
|
1597
|
+
fakeConversation = fake.conversation;
|
|
1598
|
+
|
|
1599
|
+
const handle = await startVoiceTurn({
|
|
1600
|
+
...makeTurnOptions(undefined, "conv-actor-release-guard"),
|
|
1601
|
+
actorPrincipalId: "principal-guardian",
|
|
1602
|
+
});
|
|
1603
|
+
handle.abort();
|
|
1604
|
+
await flushMicrotasks();
|
|
1605
|
+
|
|
1606
|
+
expect(readState().actorPrincipalId).toBe("principal-retry");
|
|
1607
|
+
});
|
|
1608
|
+
});
|
|
1609
|
+
|
|
1492
1610
|
describe("startVoiceTurn race-loss state restore", () => {
|
|
1493
1611
|
// A busy persist means a concurrent turn (the lock winner) is running with
|
|
1494
1612
|
// per-turn state it installed. Every race-loss path must put the winner's
|
|
@@ -1547,6 +1665,54 @@ describe("startVoiceTurn race-loss state restore", () => {
|
|
|
1547
1665
|
expect(retryState.voiceCallControlPrompt).toContain("voice_call_control");
|
|
1548
1666
|
});
|
|
1549
1667
|
|
|
1668
|
+
/**
|
|
1669
|
+
* The winner here is an ordinary message turn, which stamps the actor
|
|
1670
|
+
* directly (`conversation-routes`, `conversation-process`) rather than
|
|
1671
|
+
* through this bridge. It shares the guardian, so it writes the identical
|
|
1672
|
+
* string this turn did and the field cannot say which of them wrote it.
|
|
1673
|
+
* Reverting on that would hand the winner a principal from an earlier turn,
|
|
1674
|
+
* and automatic host-client resolution would then go looking for that other
|
|
1675
|
+
* principal's desktop.
|
|
1676
|
+
*/
|
|
1677
|
+
test("an ordinary turn's actor stamp is not reverted by a losing voice turn", async () => {
|
|
1678
|
+
const statesDuringWait: FakeTurnState[] = [];
|
|
1679
|
+
const fake = makeFakeConversation({
|
|
1680
|
+
processing: false,
|
|
1681
|
+
waitForIdle: async () => {
|
|
1682
|
+
statesDuringWait.push(readState());
|
|
1683
|
+
fake.setProcessingFlag(false);
|
|
1684
|
+
return true;
|
|
1685
|
+
},
|
|
1686
|
+
onPersist: (attempt) => {
|
|
1687
|
+
if (attempt === 1) {
|
|
1688
|
+
// The winner takes the conversation and stamps the same guardian,
|
|
1689
|
+
// the way an ordinary turn does: a direct write, counted but
|
|
1690
|
+
// otherwise invisible to this bridge.
|
|
1691
|
+
(
|
|
1692
|
+
fake.conversation as { currentTurnSourceActorPrincipalId?: string }
|
|
1693
|
+
).currentTurnSourceActorPrincipalId = "principal-guardian";
|
|
1694
|
+
fake.setProcessingFlag(true);
|
|
1695
|
+
throw new Error("Conversation is already processing a message");
|
|
1696
|
+
}
|
|
1697
|
+
},
|
|
1698
|
+
});
|
|
1699
|
+
// What an earlier turn left resident, and what a value-based revert would
|
|
1700
|
+
// wrongly put back.
|
|
1701
|
+
const readState = wireTurnState(fake.conversation, {
|
|
1702
|
+
actorPrincipalId: "principal-other",
|
|
1703
|
+
});
|
|
1704
|
+
fakeConversation = fake.conversation;
|
|
1705
|
+
|
|
1706
|
+
await startVoiceTurn({
|
|
1707
|
+
...makeTurnOptions(undefined, "conv-race-ordinary-winner"),
|
|
1708
|
+
callSessionId: "session-voice-loser",
|
|
1709
|
+
actorPrincipalId: "principal-guardian",
|
|
1710
|
+
});
|
|
1711
|
+
|
|
1712
|
+
expect(statesDuringWait.length).toBe(1);
|
|
1713
|
+
expect(statesDuringWait[0]!.actorPrincipalId).toBe("principal-guardian");
|
|
1714
|
+
});
|
|
1715
|
+
|
|
1550
1716
|
test("a busy persist whose retry wait exhausts the budget leaves the winner's values in place", async () => {
|
|
1551
1717
|
const winnerState = makeWinnerState();
|
|
1552
1718
|
const fake = makeFakeConversation({
|
|
@@ -350,6 +350,37 @@ export interface VoiceTurnOptions {
|
|
|
350
350
|
assistantId?: string;
|
|
351
351
|
/** Guardian trust context for the caller. */
|
|
352
352
|
trustContext?: TrustContext;
|
|
353
|
+
/**
|
|
354
|
+
* The actor principal this turn runs as, for host-proxy same-user binding.
|
|
355
|
+
*
|
|
356
|
+
* Host proxies resolve a target client by matching the turn's actor against
|
|
357
|
+
* the actor each client registered its SSE subscription under
|
|
358
|
+
* (`pickSameUserAutoResolve`). A turn with no actor matches nothing, so
|
|
359
|
+
* every `computer_use_*` / `host_bash` / `host_file` call it makes is
|
|
360
|
+
* refused however healthy the connected client is.
|
|
361
|
+
*
|
|
362
|
+
* Set only by the local live-voice path, whose upgrade the gateway pins to
|
|
363
|
+
* the bound guardian. A phone call leaves it unset: the caller is whoever
|
|
364
|
+
* dialled in, and an inbound caller must never resolve to a client on the
|
|
365
|
+
* owner's machine.
|
|
366
|
+
*/
|
|
367
|
+
actorPrincipalId?: string;
|
|
368
|
+
/**
|
|
369
|
+
* Whether this turn resolved its own actor and found none, so the
|
|
370
|
+
* conversation's resting identity must not stand in for it.
|
|
371
|
+
*
|
|
372
|
+
* A live-voice turn whose guardian read failed knows the resting principal
|
|
373
|
+
* and knows it cannot vouch for it: the gateway may have admitted a
|
|
374
|
+
* guardian the daemon has not caught up with. Without this the host-proxy
|
|
375
|
+
* chain walks on to `currentTurnAuthContext` and `authContext`, which an
|
|
376
|
+
* ordinary text turn leaves populated, and hands the turn the previous
|
|
377
|
+
* occupant's desktop.
|
|
378
|
+
*
|
|
379
|
+
* A phone call sets neither this nor an actor: it never resolved one, and
|
|
380
|
+
* the resting identity is the machine owner's, which is the answer that
|
|
381
|
+
* path has always used.
|
|
382
|
+
*/
|
|
383
|
+
actorFallbackSuppressed?: boolean;
|
|
353
384
|
/** Whether this is an inbound call (no outbound task). */
|
|
354
385
|
isInbound: boolean;
|
|
355
386
|
/** The outbound call task, if any. */
|
|
@@ -1057,6 +1088,7 @@ export async function startVoiceTurn(
|
|
|
1057
1088
|
pendingVoiceApprovals.clear();
|
|
1058
1089
|
conversation.setChannelCapabilities(null);
|
|
1059
1090
|
conversation.setTrustContext(null);
|
|
1091
|
+
releaseActorStamp(undefined);
|
|
1060
1092
|
conversation.setCommandIntent(null);
|
|
1061
1093
|
conversation.setAssistantId("self");
|
|
1062
1094
|
conversation.setVoiceCallControlPrompt(null);
|
|
@@ -1157,10 +1189,56 @@ export async function startVoiceTurn(
|
|
|
1157
1189
|
// The exact values this turn installs, computed once: `restoreTurnState`
|
|
1158
1190
|
// recognizes by identity whether a field still holds THIS turn's value —
|
|
1159
1191
|
// a field a concurrent winner overwrote is the winner's to keep.
|
|
1192
|
+
/**
|
|
1193
|
+
* The actor-stamp generation this turn's own install left behind, or null
|
|
1194
|
+
* before it has installed one.
|
|
1195
|
+
*
|
|
1196
|
+
* `restoreTurnState` reverts the other per-turn values by identity, which
|
|
1197
|
+
* separates a concurrent winner's from this turn's only because they are
|
|
1198
|
+
* objects. The actor principal is a string and two turns for the same
|
|
1199
|
+
* guardian write the identical one, so the field cannot say who wrote it.
|
|
1200
|
+
* The conversation counts every write to it (see
|
|
1201
|
+
* `currentTurnActorStampGeneration`), including the direct ones ordinary
|
|
1202
|
+
* message turns make in `conversation-routes` and `conversation-process`,
|
|
1203
|
+
* so a count that has not moved is the proof this turn's stamp is still
|
|
1204
|
+
* the one standing.
|
|
1205
|
+
*/
|
|
1206
|
+
let installedActorStampGeneration: number | null = null;
|
|
1207
|
+
/**
|
|
1208
|
+
* Leave `next` behind as the actor stamp, but only while this turn's own
|
|
1209
|
+
* stamp is still the one standing.
|
|
1210
|
+
*
|
|
1211
|
+
* The release on the way out and the revert on a race loss both come
|
|
1212
|
+
* through here, because they are the same question asked twice: is the
|
|
1213
|
+
* stamp on the conversation still mine to take back? `runAgentLoopImpl`
|
|
1214
|
+
* gives up the processing claim before the turn-boundary commit is
|
|
1215
|
+
* awaited, so a retry can take the conversation and stamp its own actor
|
|
1216
|
+
* while this turn is still unwinding. Clearing then would strip an actor
|
|
1217
|
+
* the retry route installs no auth-context fallback for, and its
|
|
1218
|
+
* host-proxy calls would be refused, which is the failure the stamp exists
|
|
1219
|
+
* to prevent.
|
|
1220
|
+
*/
|
|
1221
|
+
const releaseActorStamp = (
|
|
1222
|
+
next: string | undefined,
|
|
1223
|
+
nextSuppressed = false,
|
|
1224
|
+
): void => {
|
|
1225
|
+
if (
|
|
1226
|
+
installedActorStampGeneration === null ||
|
|
1227
|
+
conversation.currentTurnActorStampGeneration !==
|
|
1228
|
+
installedActorStampGeneration
|
|
1229
|
+
) {
|
|
1230
|
+
return;
|
|
1231
|
+
}
|
|
1232
|
+
conversation.currentTurnSourceActorPrincipalId = next;
|
|
1233
|
+
conversation.currentTurnActorFallbackSuppressed = nextSuppressed;
|
|
1234
|
+
installedActorStampGeneration = null;
|
|
1235
|
+
};
|
|
1160
1236
|
const voiceTurnValues = {
|
|
1161
1237
|
assistantId: opts.assistantId ?? DAEMON_INTERNAL_ASSISTANT_ID,
|
|
1162
1238
|
callSessionId: voiceSessionId,
|
|
1163
1239
|
trustContext: opts.trustContext ?? null,
|
|
1240
|
+
actorPrincipalId: opts.actorPrincipalId ?? null,
|
|
1241
|
+
actorFallbackSuppressed: opts.actorFallbackSuppressed === true,
|
|
1164
1242
|
turnChannelContext,
|
|
1165
1243
|
turnInterfaceContext,
|
|
1166
1244
|
// Resolved from the channel, with no voice-specific override.
|
|
@@ -1185,6 +1263,12 @@ export async function startVoiceTurn(
|
|
|
1185
1263
|
conversation.setAssistantId(voiceTurnValues.assistantId);
|
|
1186
1264
|
conversation.callSessionId = voiceTurnValues.callSessionId;
|
|
1187
1265
|
conversation.setTrustContext(voiceTurnValues.trustContext);
|
|
1266
|
+
conversation.currentTurnSourceActorPrincipalId =
|
|
1267
|
+
voiceTurnValues.actorPrincipalId ?? undefined;
|
|
1268
|
+
conversation.currentTurnActorFallbackSuppressed =
|
|
1269
|
+
voiceTurnValues.actorFallbackSuppressed;
|
|
1270
|
+
installedActorStampGeneration =
|
|
1271
|
+
conversation.currentTurnActorStampGeneration;
|
|
1188
1272
|
conversation.setCommandIntent(null);
|
|
1189
1273
|
conversation.setTurnChannelContext(voiceTurnValues.turnChannelContext);
|
|
1190
1274
|
conversation.setTurnInterfaceContext?.(
|
|
@@ -1204,6 +1288,8 @@ export async function startVoiceTurn(
|
|
|
1204
1288
|
assistantId: conversation.assistantId,
|
|
1205
1289
|
callSessionId: conversation.callSessionId,
|
|
1206
1290
|
trustContext: conversation.trustContext,
|
|
1291
|
+
actorPrincipalId: conversation.currentTurnSourceActorPrincipalId,
|
|
1292
|
+
actorFallbackSuppressed: conversation.currentTurnActorFallbackSuppressed,
|
|
1207
1293
|
commandIntent: conversation.commandIntent,
|
|
1208
1294
|
turnChannelContext: conversation.getTurnChannelContext?.() ?? null,
|
|
1209
1295
|
turnInterfaceContext: conversation.getTurnInterfaceContext?.() ?? null,
|
|
@@ -1243,6 +1329,11 @@ export async function startVoiceTurn(
|
|
|
1243
1329
|
) {
|
|
1244
1330
|
conversation.setTrustContext(snap.trustContext ?? null);
|
|
1245
1331
|
}
|
|
1332
|
+
// Through the same guard the release uses: see `releaseActorStamp`.
|
|
1333
|
+
releaseActorStamp(
|
|
1334
|
+
snap.actorPrincipalId ?? undefined,
|
|
1335
|
+
snap.actorFallbackSuppressed,
|
|
1336
|
+
);
|
|
1246
1337
|
if ((conversation.commandIntent ?? null) === null) {
|
|
1247
1338
|
conversation.setCommandIntent(snap.commandIntent ?? null);
|
|
1248
1339
|
}
|
|
@@ -0,0 +1,68 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Who a turn runs as for host-proxy routing, and when the walk to the
|
|
3
|
+
* conversation's resting identity is allowed.
|
|
4
|
+
*/
|
|
5
|
+
import { describe, expect, test } from "bun:test";
|
|
6
|
+
|
|
7
|
+
import { turnActorPrincipalId } from "../turn-actor.js";
|
|
8
|
+
|
|
9
|
+
describe("turn actor principal", () => {
|
|
10
|
+
test("the turn's own actor wins", () => {
|
|
11
|
+
expect(
|
|
12
|
+
turnActorPrincipalId({
|
|
13
|
+
currentTurnSourceActorPrincipalId: "principal-turn",
|
|
14
|
+
currentTurnAuthContext: { actorPrincipalId: "principal-turn-auth" },
|
|
15
|
+
authContext: { actorPrincipalId: "principal-resting" },
|
|
16
|
+
}),
|
|
17
|
+
).toBe("principal-turn");
|
|
18
|
+
});
|
|
19
|
+
|
|
20
|
+
/** A `/v1/messages` turn sets only the turn-scoped auth context. */
|
|
21
|
+
test("falls back to the turn's auth context", () => {
|
|
22
|
+
expect(
|
|
23
|
+
turnActorPrincipalId({
|
|
24
|
+
currentTurnAuthContext: { actorPrincipalId: "principal-turn-auth" },
|
|
25
|
+
authContext: { actorPrincipalId: "principal-resting" },
|
|
26
|
+
}),
|
|
27
|
+
).toBe("principal-turn-auth");
|
|
28
|
+
});
|
|
29
|
+
|
|
30
|
+
test("falls back to the conversation's resting actor", () => {
|
|
31
|
+
expect(
|
|
32
|
+
turnActorPrincipalId({
|
|
33
|
+
authContext: { actorPrincipalId: "principal-resting" },
|
|
34
|
+
}),
|
|
35
|
+
).toBe("principal-resting");
|
|
36
|
+
});
|
|
37
|
+
|
|
38
|
+
test("answers nobody when nothing names an actor", () => {
|
|
39
|
+
expect(turnActorPrincipalId({})).toBeUndefined();
|
|
40
|
+
});
|
|
41
|
+
|
|
42
|
+
/**
|
|
43
|
+
* The case the suppression exists for. An ordinary text turn leaves both
|
|
44
|
+
* fallback fields populated, so a live-voice turn that resolved its own
|
|
45
|
+
* actor and found none would otherwise inherit that turn's principal and,
|
|
46
|
+
* through it, that user's connected desktop.
|
|
47
|
+
*/
|
|
48
|
+
test("a turn that found no actor of its own inherits none", () => {
|
|
49
|
+
expect(
|
|
50
|
+
turnActorPrincipalId({
|
|
51
|
+
currentTurnActorFallbackSuppressed: true,
|
|
52
|
+
currentTurnAuthContext: { actorPrincipalId: "principal-text-turn" },
|
|
53
|
+
authContext: { actorPrincipalId: "principal-resting" },
|
|
54
|
+
}),
|
|
55
|
+
).toBeUndefined();
|
|
56
|
+
});
|
|
57
|
+
|
|
58
|
+
/** Suppression stops the walk; it does not discard an actor the turn has. */
|
|
59
|
+
test("a suppressed turn still keeps its own actor", () => {
|
|
60
|
+
expect(
|
|
61
|
+
turnActorPrincipalId({
|
|
62
|
+
currentTurnActorFallbackSuppressed: true,
|
|
63
|
+
currentTurnSourceActorPrincipalId: "principal-turn",
|
|
64
|
+
authContext: { actorPrincipalId: "principal-resting" },
|
|
65
|
+
}),
|
|
66
|
+
).toBe("principal-turn");
|
|
67
|
+
});
|
|
68
|
+
});
|
|
@@ -234,6 +234,8 @@ export interface MessagingConversationContext {
|
|
|
234
234
|
authContext?: AuthContext;
|
|
235
235
|
currentTurnAuthContext?: AuthContext;
|
|
236
236
|
currentTurnSourceActorPrincipalId?: string;
|
|
237
|
+
/** See {@link turnActorPrincipalId}. */
|
|
238
|
+
currentTurnActorFallbackSuppressed?: boolean;
|
|
237
239
|
/**
|
|
238
240
|
* OS surface reported by the connected client, re-applied from transport
|
|
239
241
|
* metadata on every inbound message.
|
|
@@ -1521,8 +1523,7 @@ export async function persistQueuedMessageBody(
|
|
|
1521
1523
|
|
|
1522
1524
|
// Same list enrichMessageWithSourcePaths sees, so history reload rebuilds
|
|
1523
1525
|
// an identical annotation block (prefix-cache parity).
|
|
1524
|
-
const attachmentStoredPaths =
|
|
1525
|
-
extractAttachmentStoredPaths(sentAttachments);
|
|
1526
|
+
const attachmentStoredPaths = extractAttachmentStoredPaths(sentAttachments);
|
|
1526
1527
|
if (attachmentStoredPaths) {
|
|
1527
1528
|
updateMessageMetadata(persistedUserMessage.id, { attachmentStoredPaths });
|
|
1528
1529
|
}
|
|
@@ -89,6 +89,7 @@ import { INTERACTIVE_SURFACE_TYPES } from "./message-protocol.js";
|
|
|
89
89
|
import { isRowVisibleToUntrustedActor } from "./message-provenance.js";
|
|
90
90
|
import type { TrustContext } from "./trust-context-types.js";
|
|
91
91
|
import { restingTrust } from "./trust-context-types.js";
|
|
92
|
+
import { turnActorPrincipalId } from "./turn-actor.js";
|
|
92
93
|
export {
|
|
93
94
|
buildSurfaceShowPair,
|
|
94
95
|
type CurrentTurnSurface,
|
|
@@ -2998,10 +2999,7 @@ export async function surfaceProxyResolver(
|
|
|
2998
2999
|
// validate at the tool-resolution layer for the same reason. The proxy
|
|
2999
3000
|
// re-checks same-user (single authoritative gate); using the shared
|
|
3000
3001
|
// helper keeps log payload and error wording identical at both layers.
|
|
3001
|
-
const sourceActorPrincipalId =
|
|
3002
|
-
ctx.currentTurnSourceActorPrincipalId ??
|
|
3003
|
-
ctx.currentTurnAuthContext?.actorPrincipalId ??
|
|
3004
|
-
ctx.authContext?.actorPrincipalId;
|
|
3002
|
+
const sourceActorPrincipalId = turnActorPrincipalId(ctx);
|
|
3005
3003
|
const target = resolveHostCuTarget({
|
|
3006
3004
|
toolName,
|
|
3007
3005
|
targetClientId,
|
|
@@ -3076,10 +3074,7 @@ export async function surfaceProxyResolver(
|
|
|
3076
3074
|
? input.target_client_id
|
|
3077
3075
|
: undefined;
|
|
3078
3076
|
|
|
3079
|
-
const sourceActorPrincipalId =
|
|
3080
|
-
ctx.currentTurnSourceActorPrincipalId ??
|
|
3081
|
-
ctx.currentTurnAuthContext?.actorPrincipalId ??
|
|
3082
|
-
ctx.authContext?.actorPrincipalId;
|
|
3077
|
+
const sourceActorPrincipalId = turnActorPrincipalId(ctx);
|
|
3083
3078
|
if (targetClientId != null) {
|
|
3084
3079
|
const client = assistantEventHub.getClientById(targetClientId);
|
|
3085
3080
|
if (!client) {
|
|
@@ -284,6 +284,7 @@ import {
|
|
|
284
284
|
isPersonalMemoryAllowed,
|
|
285
285
|
} from "./trust-context.js";
|
|
286
286
|
import type { TrustContext } from "./trust-context-types.js";
|
|
287
|
+
import { turnActorPrincipalId } from "./turn-actor.js";
|
|
287
288
|
|
|
288
289
|
export interface ConversationConstructorOptions {
|
|
289
290
|
maxTokens?: number;
|
|
@@ -577,7 +578,40 @@ export class Conversation {
|
|
|
577
578
|
/** @internal */ currentTurnRequestOrigin?: string;
|
|
578
579
|
/** @internal */ authContext?: AuthContext;
|
|
579
580
|
/** @internal */ currentTurnAuthContext?: AuthContext;
|
|
580
|
-
/**
|
|
581
|
+
/**
|
|
582
|
+
* Whether this turn resolved its own actor and found none, which is not the
|
|
583
|
+
* same as a turn that never looked. See {@link turnActorPrincipalId}.
|
|
584
|
+
*
|
|
585
|
+
* @internal
|
|
586
|
+
*/
|
|
587
|
+
currentTurnActorFallbackSuppressed = false;
|
|
588
|
+
/** @internal */ private _currentTurnSourceActorPrincipalId?: string;
|
|
589
|
+
/**
|
|
590
|
+
* How many times the actor stamp has been written on this conversation.
|
|
591
|
+
*
|
|
592
|
+
* A turn that stamped the actor and later needs to know whether its own
|
|
593
|
+
* stamp is still the one standing cannot ask the value: two turns for the
|
|
594
|
+
* same guardian write the identical string, so the field cannot say who
|
|
595
|
+
* wrote it. Every write moves this counter, whoever makes it, so a reader
|
|
596
|
+
* that remembers the count at its own write can tell "still mine" from
|
|
597
|
+
* "someone stamped after me" without every writer having to cooperate.
|
|
598
|
+
*
|
|
599
|
+
* Behind the accessor below rather than bumped at the call sites, because
|
|
600
|
+
* the writers are spread across the routes and the turn pipeline and a
|
|
601
|
+
* counter they had to remember to move is one they would eventually not.
|
|
602
|
+
*
|
|
603
|
+
* @internal
|
|
604
|
+
*/
|
|
605
|
+
currentTurnActorStampGeneration = 0;
|
|
606
|
+
/** @internal */
|
|
607
|
+
get currentTurnSourceActorPrincipalId(): string | undefined {
|
|
608
|
+
return this._currentTurnSourceActorPrincipalId;
|
|
609
|
+
}
|
|
610
|
+
/** @internal */
|
|
611
|
+
set currentTurnSourceActorPrincipalId(value: string | undefined) {
|
|
612
|
+
this._currentTurnSourceActorPrincipalId = value;
|
|
613
|
+
this.currentTurnActorStampGeneration += 1;
|
|
614
|
+
}
|
|
581
615
|
/** @internal */ loadedHistoryTrustClass?: TrustClass;
|
|
582
616
|
/** @internal */ loadedHistoryPersonalMemoryAllowed?: boolean;
|
|
583
617
|
/** @internal */ loadedHistoryStale = false;
|
|
@@ -3082,11 +3116,7 @@ export class Conversation {
|
|
|
3082
3116
|
* correctly. Returns `undefined` when no actor identity is known.
|
|
3083
3117
|
*/
|
|
3084
3118
|
getTurnActorPrincipalId(): string | undefined {
|
|
3085
|
-
return (
|
|
3086
|
-
this.currentTurnSourceActorPrincipalId ??
|
|
3087
|
-
this.currentTurnAuthContext?.actorPrincipalId ??
|
|
3088
|
-
this.authContext?.actorPrincipalId
|
|
3089
|
-
);
|
|
3119
|
+
return turnActorPrincipalId(this);
|
|
3090
3120
|
}
|
|
3091
3121
|
|
|
3092
3122
|
setVoiceCallControlPrompt(prompt: string | null): void {
|
|
@@ -0,0 +1,46 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Who the current turn runs as, for host-proxy routing.
|
|
3
|
+
*
|
|
4
|
+
* One definition, used by `Conversation.getTurnActorPrincipalId` and by the
|
|
5
|
+
* surface resolver, because the answer decides whose desktop a tool may reach
|
|
6
|
+
* and two copies of that decision are two chances to disagree.
|
|
7
|
+
*/
|
|
8
|
+
|
|
9
|
+
/** The turn-scoped identity fields the answer is read from. */
|
|
10
|
+
export interface TurnActorSource {
|
|
11
|
+
currentTurnSourceActorPrincipalId?: string;
|
|
12
|
+
currentTurnAuthContext?: { actorPrincipalId?: string };
|
|
13
|
+
authContext?: { actorPrincipalId?: string };
|
|
14
|
+
/** See {@link turnActorPrincipalId}. */
|
|
15
|
+
currentTurnActorFallbackSuppressed?: boolean;
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
/**
|
|
19
|
+
* The actor principal that owns the current turn, or `undefined` when no
|
|
20
|
+
* identity is known.
|
|
21
|
+
*
|
|
22
|
+
* The turn's own actor wins, then the turn's auth context, then the
|
|
23
|
+
* conversation's resting one: a `/v1/messages` turn sets only the first two,
|
|
24
|
+
* and the resting context is what an ordinary text turn leaves behind.
|
|
25
|
+
*
|
|
26
|
+
* **`currentTurnActorFallbackSuppressed` stops that walk at the first step.**
|
|
27
|
+
* It marks a turn that resolved its own actor and found none, which is not the
|
|
28
|
+
* same as a turn that never looked. A live-voice turn whose guardian read
|
|
29
|
+
* failed is in exactly that position: it knows the conversation's resting
|
|
30
|
+
* identity and knows it cannot vouch for it, because the gateway may have
|
|
31
|
+
* admitted a guardian the daemon has not caught up with. Walking on would hand
|
|
32
|
+
* that turn the previous occupant's principal and, through it, their connected
|
|
33
|
+
* desktop.
|
|
34
|
+
*/
|
|
35
|
+
export function turnActorPrincipalId(
|
|
36
|
+
source: TurnActorSource,
|
|
37
|
+
): string | undefined {
|
|
38
|
+
if (source.currentTurnActorFallbackSuppressed) {
|
|
39
|
+
return source.currentTurnSourceActorPrincipalId;
|
|
40
|
+
}
|
|
41
|
+
return (
|
|
42
|
+
source.currentTurnSourceActorPrincipalId ??
|
|
43
|
+
source.currentTurnAuthContext?.actorPrincipalId ??
|
|
44
|
+
source.authContext?.actorPrincipalId
|
|
45
|
+
);
|
|
46
|
+
}
|
|
@@ -0,0 +1,127 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The guardian a live-voice turn runs as.
|
|
3
|
+
*
|
|
4
|
+
* The gateway admits the socket against its own binding and hands the
|
|
5
|
+
* principal down, so nothing here resolves one: a second reading taken later
|
|
6
|
+
* in the daemon is how an admitted session came to run as a guardian the
|
|
7
|
+
* gateway never let in.
|
|
8
|
+
*/
|
|
9
|
+
import { beforeEach, describe, expect, mock, test } from "bun:test";
|
|
10
|
+
|
|
11
|
+
const realLocalActorIdentityModule = {
|
|
12
|
+
...(await import("../../runtime/local-actor-identity.js")),
|
|
13
|
+
};
|
|
14
|
+
|
|
15
|
+
/** Calls the module under test made, with the options it passed. */
|
|
16
|
+
let lookups: { forceRefresh?: boolean }[] = [];
|
|
17
|
+
/** What the next lookup answers, or throws when set to an Error. */
|
|
18
|
+
let lookupResult: string | undefined | Error = undefined;
|
|
19
|
+
|
|
20
|
+
const realLocalPrincipalTrustModule = {
|
|
21
|
+
...(await import("../../runtime/local-principal-trust.js")),
|
|
22
|
+
};
|
|
23
|
+
|
|
24
|
+
/** Principals the trust resolver was asked about. */
|
|
25
|
+
let trustLookups: string[] = [];
|
|
26
|
+
|
|
27
|
+
mock.module("../../runtime/local-principal-trust.js", () => ({
|
|
28
|
+
...realLocalPrincipalTrustModule,
|
|
29
|
+
resolveLocalPrincipalTrustContext: async (input: {
|
|
30
|
+
actorPrincipalId: string;
|
|
31
|
+
sourceChannel: string;
|
|
32
|
+
}) => {
|
|
33
|
+
trustLookups.push(input.actorPrincipalId);
|
|
34
|
+
return {
|
|
35
|
+
sourceChannel: input.sourceChannel,
|
|
36
|
+
trustClass: "guardian",
|
|
37
|
+
requesterExternalUserId: input.actorPrincipalId,
|
|
38
|
+
};
|
|
39
|
+
},
|
|
40
|
+
}));
|
|
41
|
+
|
|
42
|
+
mock.module("../../runtime/local-actor-identity.js", () => ({
|
|
43
|
+
...realLocalActorIdentityModule,
|
|
44
|
+
findLocalGuardianPrincipalId: async (options?: {
|
|
45
|
+
forceRefresh?: boolean;
|
|
46
|
+
}) => {
|
|
47
|
+
lookups.push({ forceRefresh: options?.forceRefresh });
|
|
48
|
+
if (lookupResult instanceof Error) {
|
|
49
|
+
throw lookupResult;
|
|
50
|
+
}
|
|
51
|
+
return lookupResult;
|
|
52
|
+
},
|
|
53
|
+
}));
|
|
54
|
+
|
|
55
|
+
const { resolveLocalLiveVoiceIdentity } =
|
|
56
|
+
await import("../live-voice-session.js");
|
|
57
|
+
|
|
58
|
+
beforeEach(() => {
|
|
59
|
+
lookups = [];
|
|
60
|
+
trustLookups = [];
|
|
61
|
+
lookupResult = undefined;
|
|
62
|
+
});
|
|
63
|
+
|
|
64
|
+
/**
|
|
65
|
+
* What a turn is stamped with, given what the gateway admitted.
|
|
66
|
+
*
|
|
67
|
+
* The two answers fail apart on purpose. A socket the gateway admitted
|
|
68
|
+
* without naming a guardian still has a cached binding to say what the turn
|
|
69
|
+
* may DO, and nothing to say whose desktop it may reach.
|
|
70
|
+
*/
|
|
71
|
+
describe("live voice turn identity", () => {
|
|
72
|
+
test("stamps the guardian the gateway admitted", async () => {
|
|
73
|
+
lookupResult = "principal-cached";
|
|
74
|
+
|
|
75
|
+
const identity = await resolveLocalLiveVoiceIdentity("conv-1", {
|
|
76
|
+
principalId: "principal-admitted",
|
|
77
|
+
});
|
|
78
|
+
|
|
79
|
+
expect(identity.actorPrincipalId).toBe("principal-admitted");
|
|
80
|
+
expect(identity.trustContext).toBeDefined();
|
|
81
|
+
// The gateway's answer, never a second read behind its back. Resolving
|
|
82
|
+
// one here is what let an admitted session run as a different guardian.
|
|
83
|
+
expect(lookups.length).toBe(0);
|
|
84
|
+
expect(trustLookups).toEqual(["principal-admitted"]);
|
|
85
|
+
});
|
|
86
|
+
|
|
87
|
+
/**
|
|
88
|
+
* The whole point of failing closed. Stamping the cached principal here
|
|
89
|
+
* risks reaching another user's desktop; stamping nothing costs this
|
|
90
|
+
* session its host proxies and nothing else.
|
|
91
|
+
*/
|
|
92
|
+
test("leaves the actor unset when the gateway named no guardian", async () => {
|
|
93
|
+
lookupResult = "principal-cached";
|
|
94
|
+
|
|
95
|
+
const identity = await resolveLocalLiveVoiceIdentity("conv-2", {
|
|
96
|
+
principalId: undefined,
|
|
97
|
+
});
|
|
98
|
+
|
|
99
|
+
expect(identity.actorPrincipalId).toBeUndefined();
|
|
100
|
+
});
|
|
101
|
+
|
|
102
|
+
/**
|
|
103
|
+
* The call still runs in that state: trust is answered from the cached
|
|
104
|
+
* binding, so the turn keeps the capabilities it always had and only the
|
|
105
|
+
* host proxies go quiet.
|
|
106
|
+
*/
|
|
107
|
+
test("still answers trust when the actor is left unset", async () => {
|
|
108
|
+
lookupResult = "principal-cached";
|
|
109
|
+
|
|
110
|
+
const identity = await resolveLocalLiveVoiceIdentity("conv-3", {
|
|
111
|
+
principalId: undefined,
|
|
112
|
+
});
|
|
113
|
+
|
|
114
|
+
expect(identity.trustContext?.trustClass).toBe("guardian");
|
|
115
|
+
expect(trustLookups).toEqual(["principal-cached"]);
|
|
116
|
+
});
|
|
117
|
+
|
|
118
|
+
/** The background continuation asks nothing and uses only the trust answer. */
|
|
119
|
+
test("a caller with no session read is answered from the cache", async () => {
|
|
120
|
+
lookupResult = "principal-cached";
|
|
121
|
+
|
|
122
|
+
const identity = await resolveLocalLiveVoiceIdentity("conv-4");
|
|
123
|
+
|
|
124
|
+
expect(identity.actorPrincipalId).toBe("principal-cached");
|
|
125
|
+
expect(identity.trustContext?.trustClass).toBe("guardian");
|
|
126
|
+
});
|
|
127
|
+
});
|
|
@@ -81,11 +81,18 @@ export interface LiveVoiceConnection {
|
|
|
81
81
|
export function createLiveVoiceConnection(options: {
|
|
82
82
|
send: LiveVoiceFrameSender;
|
|
83
83
|
close?: () => void;
|
|
84
|
+
/**
|
|
85
|
+
* The guardian the gateway admitted this socket for. Held for the life of
|
|
86
|
+
* the socket rather than re-read per session, because it is a fact about
|
|
87
|
+
* the admission and cannot change without a new socket.
|
|
88
|
+
*/
|
|
89
|
+
guardianPrincipalId?: string;
|
|
84
90
|
}): LiveVoiceConnection {
|
|
85
91
|
return new LiveVoiceConnectionImpl(
|
|
86
92
|
options.send,
|
|
87
93
|
getLiveVoiceSessionManager(),
|
|
88
94
|
options.close,
|
|
95
|
+
options.guardianPrincipalId,
|
|
89
96
|
);
|
|
90
97
|
}
|
|
91
98
|
|
|
@@ -103,6 +110,7 @@ class LiveVoiceConnectionImpl implements LiveVoiceConnection {
|
|
|
103
110
|
private readonly send: LiveVoiceFrameSender,
|
|
104
111
|
private readonly manager: LiveVoiceSessionManager,
|
|
105
112
|
private readonly closeTransport?: () => void,
|
|
113
|
+
private readonly guardianPrincipalId?: string,
|
|
106
114
|
) {}
|
|
107
115
|
|
|
108
116
|
get sessionId(): string | undefined {
|
|
@@ -204,7 +212,12 @@ class LiveVoiceConnectionImpl implements LiveVoiceConnection {
|
|
|
204
212
|
? { closeTransport: this.closeTransport }
|
|
205
213
|
: {}),
|
|
206
214
|
},
|
|
207
|
-
{
|
|
215
|
+
{
|
|
216
|
+
signal: pending.signal,
|
|
217
|
+
...(this.guardianPrincipalId
|
|
218
|
+
? { guardianPrincipalId: this.guardianPrincipalId }
|
|
219
|
+
: {}),
|
|
220
|
+
},
|
|
208
221
|
);
|
|
209
222
|
} finally {
|
|
210
223
|
if (this.pendingStart === pending) {
|
|
@@ -69,6 +69,12 @@ export interface LiveVoiceServerFrameSink {
|
|
|
69
69
|
export interface LiveVoiceSessionFactoryContext {
|
|
70
70
|
sessionId: string;
|
|
71
71
|
startFrame: LiveVoiceClientStartFrame;
|
|
72
|
+
/**
|
|
73
|
+
* The guardian the gateway admitted this session's socket for, when it
|
|
74
|
+
* named one. The session's turns run as this principal; absent means they
|
|
75
|
+
* run with no actor and their host-proxy calls are refused.
|
|
76
|
+
*/
|
|
77
|
+
guardianPrincipalId?: string;
|
|
72
78
|
sendFrame(frame: LiveVoiceServerFramePayload): Promise<LiveVoiceServerFrame>;
|
|
73
79
|
/**
|
|
74
80
|
* Releases this session's manager slot after a failure that happens once
|
|
@@ -261,7 +267,7 @@ export class LiveVoiceSessionManager {
|
|
|
261
267
|
async startSession(
|
|
262
268
|
startFrame: LiveVoiceClientStartFrame,
|
|
263
269
|
sink: LiveVoiceServerFrameSink,
|
|
264
|
-
options: { signal?: AbortSignal } = {},
|
|
270
|
+
options: { signal?: AbortSignal; guardianPrincipalId?: string } = {},
|
|
265
271
|
): Promise<LiveVoiceStartSessionResult> {
|
|
266
272
|
const signal = options.signal;
|
|
267
273
|
if (isAborted(signal)) {
|
|
@@ -321,6 +327,9 @@ export class LiveVoiceSessionManager {
|
|
|
321
327
|
const context: LiveVoiceSessionFactoryContext = {
|
|
322
328
|
sessionId,
|
|
323
329
|
startFrame,
|
|
330
|
+
...(options.guardianPrincipalId
|
|
331
|
+
? { guardianPrincipalId: options.guardianPrincipalId }
|
|
332
|
+
: {}),
|
|
324
333
|
sendFrame: async (payload) => {
|
|
325
334
|
// `ready` carries the conversation the session actually landed in,
|
|
326
335
|
// which is the only source for one it minted itself. Read in passing
|
|
@@ -281,6 +281,19 @@ export type LiveVoiceStreamingTranscriberResolver = (
|
|
|
281
281
|
export type LiveVoiceCredentialReadinessResolver =
|
|
282
282
|
() => Promise<LiveVoiceCredentialReadiness>;
|
|
283
283
|
|
|
284
|
+
/**
|
|
285
|
+
* What a session's own guardian read settled on, which is not the same as no
|
|
286
|
+
* read having happened.
|
|
287
|
+
*
|
|
288
|
+
* Present with an undefined `principalId` means the session asked past the
|
|
289
|
+
* cache and got no answer; the turn then runs with no actor rather than with
|
|
290
|
+
* the cached one. Absent means nothing asked, which is the background
|
|
291
|
+
* continuation, and only its trust answer is used.
|
|
292
|
+
*/
|
|
293
|
+
export interface LiveVoiceSessionGuardian {
|
|
294
|
+
principalId: string | undefined;
|
|
295
|
+
}
|
|
296
|
+
|
|
284
297
|
export type LiveVoiceTurnStarter = (
|
|
285
298
|
options: VoiceTurnOptions,
|
|
286
299
|
) => Promise<VoiceTurnHandle>;
|
|
@@ -6975,7 +6988,9 @@ export function createLiveVoiceSession(
|
|
|
6975
6988
|
options.resolveCredentialReadiness === undefined
|
|
6976
6989
|
? defaultResolveLiveVoiceCredentialReadiness
|
|
6977
6990
|
: options.resolveCredentialReadiness,
|
|
6978
|
-
startVoiceTurn:
|
|
6991
|
+
startVoiceTurn:
|
|
6992
|
+
options.startVoiceTurn ??
|
|
6993
|
+
makeDefaultStartVoiceTurn(context.guardianPrincipalId),
|
|
6979
6994
|
streamTtsAudio:
|
|
6980
6995
|
options.streamTtsAudio === undefined
|
|
6981
6996
|
? defaultStreamLiveVoiceTtsAudio
|
|
@@ -7022,7 +7037,7 @@ export async function defaultSpawnBackgroundContinuation(args: {
|
|
|
7022
7037
|
// trust the foreground turn ran under and pass it explicitly (resolution
|
|
7023
7038
|
// itself stays fail-closed: on a miss the continuation runs as `unknown`,
|
|
7024
7039
|
// exactly as an unstamped turn does).
|
|
7025
|
-
const trustContext = await
|
|
7040
|
+
const { trustContext } = await resolveLocalLiveVoiceIdentity(
|
|
7026
7041
|
args.parentConversationId,
|
|
7027
7042
|
);
|
|
7028
7043
|
// getOrCreateConversation (not a raw registry read): it rebuilds a stale
|
|
@@ -7132,8 +7147,34 @@ async function defaultResolveLiveVoiceCredentialReadiness(): Promise<LiveVoiceCr
|
|
|
7132
7147
|
return resolveLiveVoiceCredentialReadiness();
|
|
7133
7148
|
}
|
|
7134
7149
|
|
|
7150
|
+
/**
|
|
7151
|
+
* The default turn starter, bound to the guardian this session was admitted
|
|
7152
|
+
* for.
|
|
7153
|
+
*
|
|
7154
|
+
* **The gateway's principal is authoritative and nothing here re-resolves
|
|
7155
|
+
* it.** The admission decision is made against the gateway's own binding and
|
|
7156
|
+
* the principal travels down on the upstream dial. The daemon is reached
|
|
7157
|
+
* through a service token, so every socket arrives as the same caller and any
|
|
7158
|
+
* identity worked out here would be a separate, later reading of a binding
|
|
7159
|
+
* that can change in between, leaving a session admitted for one guardian
|
|
7160
|
+
* running as another.
|
|
7161
|
+
*
|
|
7162
|
+
* Absent when the gateway named nobody, and the session's turns then run with
|
|
7163
|
+
* no actor.
|
|
7164
|
+
*
|
|
7165
|
+
* Exported for its tests, which are the only way to reach this: every caller
|
|
7166
|
+
* of `createLiveVoiceSession` in the suite injects its own `startVoiceTurn`,
|
|
7167
|
+
* so this starter is never driven there.
|
|
7168
|
+
*/
|
|
7169
|
+
export function makeDefaultStartVoiceTurn(
|
|
7170
|
+
guardianPrincipalId?: string,
|
|
7171
|
+
): LiveVoiceTurnStarter {
|
|
7172
|
+
return (options) => defaultStartVoiceTurn(options, guardianPrincipalId);
|
|
7173
|
+
}
|
|
7174
|
+
|
|
7135
7175
|
async function defaultStartVoiceTurn(
|
|
7136
7176
|
options: VoiceTurnOptions,
|
|
7177
|
+
guardianPrincipalId: string | undefined,
|
|
7137
7178
|
): Promise<VoiceTurnHandle> {
|
|
7138
7179
|
// On the first turn of a brand-new chat the client's conversation id has no
|
|
7139
7180
|
// persisted `conversations` row yet — the live-voice session adopts the id
|
|
@@ -7143,8 +7184,8 @@ async function defaultStartVoiceTurn(
|
|
|
7143
7184
|
// exists (idempotent) before persisting. Lives in the production wiring, not
|
|
7144
7185
|
// the session state machine, so session unit tests stay DB-free.
|
|
7145
7186
|
// Native: this is the local live-voice session, which adopts a conversation
|
|
7146
|
-
// id the app supplied. Its
|
|
7147
|
-
// `
|
|
7187
|
+
// id the app supplied. Its guardian identity resolves through
|
|
7188
|
+
// `resolveLocalLiveVoiceIdentity`, and a phone call reaches the assistant
|
|
7148
7189
|
// through the telephony path rather than here.
|
|
7149
7190
|
const createdConversation = ensureConversationExists(
|
|
7150
7191
|
options.conversationId,
|
|
@@ -7160,18 +7201,21 @@ async function defaultStartVoiceTurn(
|
|
|
7160
7201
|
options.conversationId,
|
|
7161
7202
|
);
|
|
7162
7203
|
}
|
|
7163
|
-
// Stamp the turn with the guardian's trust context
|
|
7164
|
-
//
|
|
7165
|
-
// session only exists for the guardian's own
|
|
7166
|
-
// gateway pins the `/v1/live-voice` upgrade to the
|
|
7167
|
-
// live-voice ingress bypasses the send-message
|
|
7168
|
-
//
|
|
7169
|
-
//
|
|
7170
|
-
//
|
|
7204
|
+
// Stamp the turn with the guardian's identity: the trust context that says
|
|
7205
|
+
// what it may do, and the actor principal that says whose machine it may
|
|
7206
|
+
// reach. A local live-voice session only exists for the guardian's own
|
|
7207
|
+
// authenticated client (the gateway pins the `/v1/live-voice` upgrade to the
|
|
7208
|
+
// bound guardian), but the live-voice ingress bypasses the send-message
|
|
7209
|
+
// route, which is where both are normally established. Without the trust
|
|
7210
|
+
// stamp the turn runs fail-closed as `unknown` and every sensitive tool is
|
|
7211
|
+
// denied; without the actor stamp it matches no connected client and every
|
|
7212
|
+
// host-proxy call is refused. Resolution stays fail-closed on both: a
|
|
7213
|
+
// gateway miss or missing binding leaves them unset, never a blind grant.
|
|
7171
7214
|
const trustStartedAt = performance.now();
|
|
7172
|
-
const trustContext =
|
|
7173
|
-
options.conversationId,
|
|
7174
|
-
|
|
7215
|
+
const { actorPrincipalId, trustContext } =
|
|
7216
|
+
await resolveLocalLiveVoiceIdentity(options.conversationId, {
|
|
7217
|
+
principalId: guardianPrincipalId,
|
|
7218
|
+
});
|
|
7175
7219
|
const trustMs = Math.round(performance.now() - trustStartedAt);
|
|
7176
7220
|
const { startVoiceTurn } = await import("../calls/voice-session-bridge.js");
|
|
7177
7221
|
log.info(
|
|
@@ -7185,35 +7229,82 @@ async function defaultStartVoiceTurn(
|
|
|
7185
7229
|
);
|
|
7186
7230
|
return startVoiceTurn({
|
|
7187
7231
|
...options,
|
|
7232
|
+
...(actorPrincipalId
|
|
7233
|
+
? { actorPrincipalId }
|
|
7234
|
+
: // The session asked and got no answer, so the conversation's resting
|
|
7235
|
+
// identity must not stand in: it may name the guardian this one was
|
|
7236
|
+
// rebound from, and the host proxies would follow it to that user's
|
|
7237
|
+
// desktop.
|
|
7238
|
+
{ actorFallbackSuppressed: true }),
|
|
7188
7239
|
...(trustContext ? { trustContext } : {}),
|
|
7189
7240
|
});
|
|
7190
7241
|
}
|
|
7191
7242
|
|
|
7192
7243
|
/**
|
|
7193
|
-
*
|
|
7194
|
-
*
|
|
7195
|
-
*
|
|
7196
|
-
*
|
|
7244
|
+
* Who a live-voice turn runs as: the local guardian's actor principal, and the
|
|
7245
|
+
* {@link TrustContext} resolved for it.
|
|
7246
|
+
*
|
|
7247
|
+
* The two answer different questions and are withheld under different
|
|
7248
|
+
* conditions, so a turn can carry one without the other.
|
|
7249
|
+
*
|
|
7250
|
+
* The principal says whose machine a tool may reach, and the host proxies
|
|
7251
|
+
* match connected clients against it. The trust class is a policy answer about
|
|
7252
|
+
* that principal, saying what the turn may do once it has one. A binding that
|
|
7253
|
+
* resolves to something other than `guardian` still names the actor the turn
|
|
7254
|
+
* belongs to: withholding the principal there would refuse the owner access to
|
|
7255
|
+
* their own machine over a question about what they are allowed to do with it.
|
|
7256
|
+
*
|
|
7257
|
+
* **The common partial answer is trust without an actor**, and it is the one
|
|
7258
|
+
* to recognise in an incident. A session whose own guardian read settled
|
|
7259
|
+
* nothing still resolves trust from the cached binding, because what a turn
|
|
7260
|
+
* may do is a question about the machine's owner and the cache answers it; the
|
|
7261
|
+
* actor is withheld because the cache cannot say whether the gateway admitted
|
|
7262
|
+
* that guardian or one it was rebound from. Such a turn talks and acts
|
|
7263
|
+
* normally and only its host-proxy calls are refused, which reads as computer
|
|
7264
|
+
* use being unavailable rather than as an untrusted turn.
|
|
7265
|
+
*
|
|
7266
|
+
* Both are empty only when no vellum guardian binding exists at all, or the
|
|
7267
|
+
* gateway answers nothing to either read, and the turn then runs exactly as an
|
|
7268
|
+
* unstamped one does.
|
|
7197
7269
|
*/
|
|
7198
|
-
async function
|
|
7270
|
+
export async function resolveLocalLiveVoiceIdentity(
|
|
7199
7271
|
conversationId: string,
|
|
7200
|
-
|
|
7272
|
+
session?: LiveVoiceSessionGuardian,
|
|
7273
|
+
): Promise<{
|
|
7274
|
+
actorPrincipalId?: string;
|
|
7275
|
+
trustContext?: TrustContext;
|
|
7276
|
+
}> {
|
|
7201
7277
|
const { findLocalGuardianPrincipalId } =
|
|
7202
7278
|
await import("../runtime/local-actor-identity.js");
|
|
7203
7279
|
const { resolveLocalPrincipalTrustContext } =
|
|
7204
7280
|
await import("../runtime/local-principal-trust.js");
|
|
7205
|
-
|
|
7206
|
-
|
|
7207
|
-
|
|
7281
|
+
// Trust is a question about the machine's own owner, so the cached binding
|
|
7282
|
+
// answers it whenever the session's read settled nothing. The actor is a
|
|
7283
|
+
// question about whose desktop a tool may reach, and only a read the
|
|
7284
|
+
// gateway's admission was reconciled with answers that one.
|
|
7285
|
+
const trustPrincipalId =
|
|
7286
|
+
session?.principalId ?? (await findLocalGuardianPrincipalId());
|
|
7287
|
+
if (!trustPrincipalId) {
|
|
7288
|
+
return {};
|
|
7208
7289
|
}
|
|
7209
7290
|
const trustContext = await resolveLocalPrincipalTrustContext({
|
|
7210
|
-
actorPrincipalId:
|
|
7291
|
+
actorPrincipalId: trustPrincipalId,
|
|
7211
7292
|
sourceChannel: "vellum",
|
|
7212
7293
|
conversationExternalId: conversationId,
|
|
7213
7294
|
});
|
|
7214
|
-
//
|
|
7215
|
-
//
|
|
7216
|
-
|
|
7295
|
+
// **Unset beats stale.** A session whose read settled nothing knows the
|
|
7296
|
+
// cached binding, but not whether the gateway admitted that guardian or one
|
|
7297
|
+
// it was just rebound from. Stamping the cached answer risks the turn
|
|
7298
|
+
// reaching another user's desktop; stamping nothing costs this session its
|
|
7299
|
+
// host proxies, which is where a voice session carrying no actor already
|
|
7300
|
+
// sits, and leaves the call itself untouched either way.
|
|
7301
|
+
const actorPrincipalId = session ? session.principalId : trustPrincipalId;
|
|
7302
|
+
return {
|
|
7303
|
+
...(actorPrincipalId ? { actorPrincipalId } : {}),
|
|
7304
|
+
// Only stamp a positive guardian resolution; the resolver's own
|
|
7305
|
+
// fail-closed `unknown` carries no more information than no stamp.
|
|
7306
|
+
...(trustContext.trustClass === "guardian" ? { trustContext } : {}),
|
|
7307
|
+
};
|
|
7217
7308
|
}
|
|
7218
7309
|
|
|
7219
7310
|
async function defaultStreamLiveVoiceTtsAudio(
|
|
@@ -177,6 +177,19 @@ interface SttStreamWebSocketData {
|
|
|
177
177
|
*/
|
|
178
178
|
interface LiveVoiceWebSocketData {
|
|
179
179
|
wsType: "live-voice";
|
|
180
|
+
/**
|
|
181
|
+
* The guardian the gateway admitted this socket for, when it named one.
|
|
182
|
+
*
|
|
183
|
+
* The runtime cannot work this out for itself: the gateway dials with a
|
|
184
|
+
* service token, so every socket arrives as the same caller, and any
|
|
185
|
+
* identity resolved here would be a second reading of a binding that can
|
|
186
|
+
* change between the admission and the read. Absent when the gateway
|
|
187
|
+
* admitted nobody, which the session reads as a turn with no actor.
|
|
188
|
+
*
|
|
189
|
+
* Trusted because it arrives on this dial, which only the gateway can make
|
|
190
|
+
* (see {@link verifyGatewayServiceToken}), and never from a client header.
|
|
191
|
+
*/
|
|
192
|
+
guardianPrincipalId?: string;
|
|
180
193
|
}
|
|
181
194
|
|
|
182
195
|
/**
|
|
@@ -352,6 +365,11 @@ export class RuntimeHttpServer {
|
|
|
352
365
|
send: (frame) => {
|
|
353
366
|
ws.send(JSON.stringify(frame));
|
|
354
367
|
},
|
|
368
|
+
...(liveVoiceWs.data.guardianPrincipalId
|
|
369
|
+
? {
|
|
370
|
+
guardianPrincipalId: liveVoiceWs.data.guardianPrincipalId,
|
|
371
|
+
}
|
|
372
|
+
: {}),
|
|
355
373
|
// Lets the daemon hang up on a client that stopped answering.
|
|
356
374
|
// A normal close (not a retryable one) so the client ends the
|
|
357
375
|
// call rather than reconnecting into a session that is gone.
|
|
@@ -1052,9 +1070,14 @@ export class RuntimeHttpServer {
|
|
|
1052
1070
|
return tokenError;
|
|
1053
1071
|
}
|
|
1054
1072
|
|
|
1073
|
+
const guardianPrincipalId =
|
|
1074
|
+
new URL(req.url).searchParams.get("guardianPrincipalId")?.trim() ||
|
|
1075
|
+
undefined;
|
|
1076
|
+
|
|
1055
1077
|
const upgraded = server.upgrade(req, {
|
|
1056
1078
|
data: {
|
|
1057
1079
|
wsType: "live-voice",
|
|
1080
|
+
...(guardianPrincipalId ? { guardianPrincipalId } : {}),
|
|
1058
1081
|
} satisfies LiveVoiceWebSocketData,
|
|
1059
1082
|
});
|
|
1060
1083
|
if (!upgraded) {
|