forked from BasedHardware/omi
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsurface-session.test.ts
More file actions
510 lines (466 loc) · 17.1 KB
/
Copy pathsurface-session.test.ts
File metadata and controls
510 lines (466 loc) · 17.1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
import { describe, it, expect, beforeEach, afterEach } from "vitest";
import { mkdtempSync, rmSync } from "node:fs";
import { join } from "node:path";
import { tmpdir } from "node:os";
import { SqliteAgentStore } from "../src/runtime/sqlite-store.js";
import { AgentRuntimeKernel } from "../src/runtime/kernel.js";
import { AdapterRegistry } from "../src/runtime/adapter-registry.js";
import { importLegacyMainChatSessions, resolveSurfaceSession } from "../src/runtime/surface-session.js";
import { listJournalTurns, recordJournalTurn } from "../src/runtime/conversation-journal.js";
function newStore(): SqliteAgentStore {
const dir = mkdtempSync(join(tmpdir(), "omi-surface-session-"));
return new SqliteAgentStore({ stateDir: dir, reconcileOnOpen: false });
}
describe("surface_conversations", () => {
let store: SqliteAgentStore;
let stateDir: string;
beforeEach(() => {
const dir = mkdtempSync(join(tmpdir(), "omi-surface-session-"));
stateDir = dir;
store = new SqliteAgentStore({ stateDir: dir, reconcileOnOpen: false });
});
afterEach(() => {
store.close();
rmSync(stateDir, { recursive: true, force: true });
});
it("resolveSurfaceSession creates and reuses the same agent session", () => {
const first = resolveSurfaceSession(
store,
{
ownerId: "owner-a",
surfaceRef: {
surfaceKind: "main_chat",
externalRefKind: "chat",
externalRefId: "default",
},
defaultAdapterId: "acp",
},
() => 1,
);
const second = resolveSurfaceSession(
store,
{
ownerId: "owner-a",
surfaceRef: {
surfaceKind: "main_chat",
externalRefKind: "chat",
externalRefId: "default",
},
defaultAdapterId: "acp",
},
() => 2,
);
expect(second.agentSessionId).toBe(first.agentSessionId);
expect(second.conversationId).toBe(first.conversationId);
expect(store.allRows("SELECT * FROM surface_conversations")).toHaveLength(1);
});
it("pins an explicit creation profile atomically and ignores later selections", () => {
const surfaceRef = {
surfaceKind: "main_chat",
externalRefKind: "chat",
externalRefId: "directed-provider",
};
const created = resolveSurfaceSession(store, {
ownerId: "owner-a",
surfaceRef,
defaultAdapterId: "acp",
modelProfile: "created-model",
defaultCwd: "/tmp/created",
}, () => 1);
const existing = resolveSurfaceSession(store, {
ownerId: "owner-a",
surfaceRef,
defaultAdapterId: "pi-mono",
modelProfile: "must-not-apply",
defaultCwd: "/tmp/must-not-apply",
}, () => 2);
expect(existing.agentSessionId).toBe(created.agentSessionId);
expect(store.getRow(
`SELECT adapter_id, model_profile, working_directory
FROM session_execution_profiles WHERE session_id = ? AND generation = 1`,
[created.agentSessionId],
)).toEqual({
adapter_id: "acp",
model_profile: "created-model",
working_directory: "/tmp/created",
});
});
it("isolates surfaces per owner", () => {
const ownerA = resolveSurfaceSession(
store,
{
ownerId: "owner-a",
surfaceRef: {
surfaceKind: "main_chat",
externalRefKind: "chat",
externalRefId: "default",
},
},
() => 1,
);
const ownerB = resolveSurfaceSession(
store,
{
ownerId: "owner-b",
surfaceRef: {
surfaceKind: "main_chat",
externalRefKind: "chat",
externalRefId: "default",
},
},
() => 1,
);
expect(ownerA.agentSessionId).not.toBe(ownerB.agentSessionId);
});
it("shares one canonical session and conversation when realtime voice resolves before main chat", () => {
const voice = resolveSurfaceSession(store, {
ownerId: "owner-a",
surfaceRef: { surfaceKind: "realtime_voice", externalRefKind: "chat", externalRefId: "default" },
defaultAdapterId: "acp",
}, () => 1);
const main = resolveSurfaceSession(store, {
ownerId: "owner-a",
surfaceRef: { surfaceKind: "main_chat", externalRefKind: "chat", externalRefId: "default" },
defaultAdapterId: "pi-mono",
}, () => 2);
expect(main).toEqual(voice);
expect(store.allRows(
`SELECT surface_kind, conversation_id, agent_session_id
FROM surface_conversations ORDER BY surface_kind ASC`,
)).toEqual([
{ surface_kind: "main_chat", conversation_id: voice.conversationId, agent_session_id: voice.agentSessionId },
{ surface_kind: "realtime_voice", conversation_id: voice.conversationId, agent_session_id: voice.agentSessionId },
]);
});
it("shares main, floating, and realtime aliases while preserving task/workstream boundaries", () => {
const main = resolveSurfaceSession(store, {
ownerId: "owner-a",
surfaceRef: { surfaceKind: "main_chat", externalRefKind: "chat", externalRefId: "default" },
}, () => 1);
const floating = resolveSurfaceSession(store, {
ownerId: "owner-a",
surfaceRef: { surfaceKind: "floating_chat", externalRefKind: "chat", externalRefId: "default" },
}, () => 2);
const voice = resolveSurfaceSession(store, {
ownerId: "owner-a",
surfaceRef: { surfaceKind: "realtime_voice", externalRefKind: "chat", externalRefId: "default" },
}, () => 3);
const task = resolveSurfaceSession(store, {
ownerId: "owner-a",
surfaceRef: { surfaceKind: "task_chat", externalRefKind: "task", externalRefId: "default" },
}, () => 4);
const workstream = resolveSurfaceSession(store, {
ownerId: "owner-a",
surfaceRef: { surfaceKind: "workstream", externalRefKind: "workstream", externalRefId: "default" },
}, () => 5);
expect(floating).toEqual(main);
expect(voice).toEqual(main);
expect(task.conversationId).not.toBe(main.conversationId);
expect(workstream.conversationId).not.toBe(main.conversationId);
expect(task.agentSessionId).not.toBe(main.agentSessionId);
expect(workstream.agentSessionId).not.toBe(main.agentSessionId);
recordJournalTurn(store, {
ownerId: "owner-a",
conversationId: voice.conversationId,
turnId: "voice-visible-everywhere",
role: "user",
surfaceKind: "realtime_voice",
origin: "realtime_voice",
status: "completed",
content: "Shared voice turn",
contentBlocks: [{ type: "text", id: "voice:text", text: "Shared voice turn" }],
createdAtMs: 10,
});
expect(listJournalTurns(store, {
ownerId: "owner-a",
conversationId: main.conversationId,
}).turns).toEqual([expect.objectContaining({ turnId: "voice-visible-everywhere" })]);
expect(listJournalTurns(store, {
ownerId: "owner-a",
conversationId: task.conversationId,
}).turns).toEqual([]);
});
it("links orphan sessions by external ref instead of violating uniqueness", () => {
const orphan = store.insertSession({
ownerId: "owner-a",
surfaceKind: "main_chat",
externalRefKind: "chat",
externalRefId: "default",
defaultAdapterId: "acp",
});
const resolved = resolveSurfaceSession(
store,
{
ownerId: "owner-a",
surfaceRef: {
surfaceKind: "main_chat",
externalRefKind: "chat",
externalRefId: "default",
},
},
() => 5,
);
expect(resolved.agentSessionId).toBe(orphan.sessionId);
expect(store.allRows("SELECT * FROM sessions")).toHaveLength(1);
expect(store.allRows("SELECT * FROM surface_conversations")).toHaveLength(1);
});
it("importLegacyMainChatSessions links surface when session already exists by external ref", () => {
const existing = store.insertSession({
ownerId: "owner-a",
surfaceKind: "main_chat",
externalRefKind: "chat",
externalRefId: "default",
defaultAdapterId: "acp",
});
const imported = importLegacyMainChatSessions(
store,
{
ownerId: "owner-a",
entries: [{ chatId: "default", agentSessionId: "ses_legacy_other" }],
},
() => 1,
);
expect(imported).toEqual({
acceptedEntries: [{ chatId: "default", agentSessionId: "ses_legacy_other" }],
importedCount: 1,
});
const row = store.getRow(
`SELECT conversation_id, agent_session_id FROM surface_conversations
WHERE owner_id = ? AND external_ref_id = ?`,
["owner-a", "default"],
);
expect(String(row.agent_session_id)).toBe(existing.sessionId);
expect(store.allRows("SELECT * FROM sessions")).toHaveLength(1);
});
it("imports legacy main-chat UserDefaults sessions once", () => {
const imported = importLegacyMainChatSessions(
store,
{
ownerId: "owner-a",
entries: [{ chatId: "default", agentSessionId: "ses_legacy_1" }],
},
() => 1,
);
expect(imported).toEqual({
acceptedEntries: [{ chatId: "default", agentSessionId: "ses_legacy_1" }],
importedCount: 1,
});
expect(importLegacyMainChatSessions(
store,
{
ownerId: "owner-a",
entries: [{ chatId: "default", agentSessionId: "ses_legacy_1" }],
},
() => 2,
)).toEqual({
acceptedEntries: [{ chatId: "default", agentSessionId: "ses_legacy_1" }],
importedCount: 0,
});
const resolved = resolveSurfaceSession(
store,
{
ownerId: "owner-a",
surfaceRef: {
surfaceKind: "main_chat",
externalRefKind: "chat",
externalRefId: "default",
},
},
() => 2,
);
expect(resolved.agentSessionId).toBe("ses_legacy_1");
});
it("rejects an invalid legacy alias batch atomically instead of acknowledging partial import", () => {
expect(() => importLegacyMainChatSessions(
store,
{
ownerId: "owner-a",
entries: [
{ chatId: "default", agentSessionId: "ses_valid" },
{ chatId: "other", agentSessionId: " " },
],
},
() => 1,
)).toThrow("invalid_legacy_main_chat_session_entry");
expect(store.allRows("SELECT * FROM surface_conversations")).toEqual([]);
expect(store.allRows("SELECT * FROM sessions")).toEqual([]);
});
it("does not rewrite an imported session profile during surface resolution", () => {
importLegacyMainChatSessions(
store,
{
ownerId: "owner-a",
entries: [{ chatId: "default", agentSessionId: "ses_legacy_1" }],
},
() => 1,
);
const resolved = resolveSurfaceSession(
store,
{
ownerId: "owner-a",
surfaceRef: {
surfaceKind: "main_chat",
externalRefKind: "chat",
externalRefId: "default",
},
defaultAdapterId: "pi-mono",
},
() => 2,
);
expect(resolved.agentSessionId).toBe("ses_legacy_1");
const session = store.getRow(
"SELECT default_adapter_id, provider_boundary FROM sessions WHERE session_id = ?",
[resolved.agentSessionId],
);
expect(String(session.default_adapter_id)).toBe("acp");
expect(String(session.provider_boundary)).toBe("local_user:acp");
});
it("does not rewrite a main-chat provider boundary after execution starts", () => {
const resolved = resolveSurfaceSession(
store,
{
ownerId: "owner-a",
surfaceRef: {
surfaceKind: "main_chat",
externalRefKind: "chat",
externalRefId: "default",
},
defaultAdapterId: "acp",
},
() => 1,
);
store.insertRun({
sessionId: resolved.agentSessionId,
clientId: "client-a",
requestId: "request-a",
status: "queued",
mode: "ask",
inputJson: "{}",
});
resolveSurfaceSession(
store,
{
ownerId: "owner-a",
surfaceRef: {
surfaceKind: "main_chat",
externalRefKind: "chat",
externalRefId: "default",
},
defaultAdapterId: "pi-mono",
},
() => 2,
);
const session = store.getRow(
"SELECT default_adapter_id, provider_boundary FROM sessions WHERE session_id = ?",
[resolved.agentSessionId],
);
expect(String(session.default_adapter_id)).toBe("acp");
expect(String(session.provider_boundary)).toBe("local_user:acp");
});
it("imports legacy sessions with distinct conversationId from agentSessionId", () => {
importLegacyMainChatSessions(
store,
{
ownerId: "owner-a",
entries: [{ chatId: "default", agentSessionId: "ses_legacy_1" }],
},
() => 1,
);
const row = store.getRow(
`SELECT conversation_id, agent_session_id FROM surface_conversations
WHERE owner_id = ? AND external_ref_id = ?`,
["owner-a", "default"],
);
expect(String(row.agent_session_id)).toBe("ses_legacy_1");
expect(String(row.conversation_id)).not.toBe("ses_legacy_1");
expect(String(row.conversation_id)).toMatch(/^conv_/);
});
it("owner B does not reuse owner A conversation for the same surface", () => {
const surfaceRef = {
surfaceKind: "main_chat",
externalRefKind: "chat",
externalRefId: "default",
};
const ownerA = resolveSurfaceSession(store, { ownerId: "owner-a", surfaceRef }, () => 1);
const ownerB = resolveSurfaceSession(store, { ownerId: "owner-b", surfaceRef }, () => 2);
expect(ownerA.conversationId).not.toBe(ownerB.conversationId);
expect(ownerA.agentSessionId).not.toBe(ownerB.agentSessionId);
});
it("clearOwnerState invalidates active bindings without deleting surface rows", () => {
const registry = new AdapterRegistry();
const kernel = new AgentRuntimeKernel({ store, registry });
const resolved = kernel.resolveSurfaceSession({
ownerId: "owner-a",
surfaceRef: {
surfaceKind: "service",
externalRefKind: "service",
externalRefId: "gmail_reader",
},
defaultAdapterId: "acp",
});
store.insertAdapterBinding({
sessionId: resolved.agentSessionId,
adapterId: "acp",
bindingGeneration: 1,
resumeFidelity: "native",
status: "active",
});
const cleared = kernel.clearOwnerState("owner-a");
expect(cleared.invalidatedBindingIds).toHaveLength(1);
expect(store.getRow("SELECT status FROM adapter_bindings").status).toBe("invalid");
expect(store.allRows("SELECT * FROM surface_conversations")).toHaveLength(1);
});
it("samples chat-first capability once for main Chat and keeps every other surface off", () => {
const kernel = new AgentRuntimeKernel({ store, registry: new AdapterRegistry() });
const mainSurface = { surfaceKind: "main_chat", externalRefKind: "chat", externalRefId: "chat-first" };
const main = kernel.resolveSurfaceSession({
ownerId: "owner-a",
surfaceRef: mainSurface,
defaultAdapterId: "acp",
chatFirstCapability: { chatFirstUi: true, controlGeneration: 7 },
});
const mainSnapshot = kernel.contextSnapshot(main.agentSessionId, "owner-a", "main_chat");
expect(mainSnapshot.capabilities.chatFirstUi).toBe(true);
expect(mainSnapshot.capabilities.chatFirstControlGeneration).toBe(7);
const floating = kernel.resolveSurfaceSession({
ownerId: "owner-a",
surfaceRef: { surfaceKind: "floating_chat", externalRefKind: "chat", externalRefId: "chat-first" },
defaultAdapterId: "acp",
chatFirstCapability: { chatFirstUi: true, controlGeneration: 7 },
});
const floatingSnapshot = kernel.contextSnapshot(floating.agentSessionId, "owner-a", "floating_chat");
expect(floatingSnapshot.capabilities.chatFirstUi).toBe(false);
expect(floatingSnapshot.capabilities.chatFirstControlGeneration).toBeNull();
});
it("does not let an early capability-less lookup consume the server-derived main-chat sample", () => {
const kernel = new AgentRuntimeKernel({ store, registry: new AdapterRegistry() });
const surfaceRef = { surfaceKind: "main_chat", externalRefKind: "chat", externalRefId: "pending-sample" };
const resolved = kernel.resolveSurfaceSession({ ownerId: "owner-a", surfaceRef, defaultAdapterId: "acp" });
expect(kernel.contextSnapshot(resolved.agentSessionId, "owner-a", "main_chat").capabilities.chatFirstUi).toBe(false);
kernel.resolveSurfaceSession({
ownerId: "owner-a",
surfaceRef,
defaultAdapterId: "acp",
chatFirstCapability: { chatFirstUi: true, controlGeneration: 7 },
});
const sampled = kernel.contextSnapshot(resolved.agentSessionId, "owner-a", "main_chat");
expect(sampled.capabilities.chatFirstUi).toBe(true);
expect(sampled.capabilities.chatFirstControlGeneration).toBe(7);
});
it("keeps an explicit main-chat capability sample immutable", () => {
const kernel = new AgentRuntimeKernel({ store, registry: new AdapterRegistry() });
const surfaceRef = { surfaceKind: "main_chat", externalRefKind: "chat", externalRefId: "sampled-off" };
kernel.resolveSurfaceSession({
ownerId: "owner-a",
surfaceRef,
defaultAdapterId: "acp",
chatFirstCapability: { chatFirstUi: false, controlGeneration: 7 },
});
expect(() => kernel.resolveSurfaceSession({
ownerId: "owner-a",
surfaceRef,
defaultAdapterId: "acp",
chatFirstCapability: { chatFirstUi: true, controlGeneration: 7 },
})).toThrow(/immutable/);
});
});