forked from BasedHardware/omi
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathkernel.test.ts
More file actions
1329 lines (1145 loc) · 48.7 KB
/
Copy pathkernel.test.ts
File metadata and controls
1329 lines (1145 loc) · 48.7 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
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
// AgentRuntimeKernel behavioral tests — the run/attempt/binding state machine
// driven end to end against a real SQLite store and a fake RuntimeAdapter.
//
// Driver: injects node:sqlite's DatabaseSync via the store's `databaseFactory`
// seam, exactly as store.test.ts does. better-sqlite3 is rebuilt for Electron's
// ABI and cannot load under plain-node Vitest; both drivers satisfy the store's
// structural KernelDatabase interface, so this exercises the production path.
//
// Hermetic: no network, no sleeps, no ordering dependence between tests.
//
// SCOPE: the run / attempt / binding / delegation state machine and its guards.
// The coordinator surface — the context-packet security gates, the action-queue
// projection, and the intent-router decision order — lives in
// desktopCoordinator.test.ts. Do not read this file alone and conclude the
// context-packet gate is untested.
import { mkdtempSync, rmSync } from 'node:fs'
import { join } from 'node:path'
import { tmpdir } from 'node:os'
import { DatabaseSync } from 'node:sqlite'
import { afterEach, describe, expect, it } from 'vitest'
import { AgentRuntimeKernel } from './kernel'
import { AdapterRegistry } from './adapterRegistry'
import { SqliteAgentStore, type DatabaseFactory } from './store'
import type {
AdapterAttemptContext,
AdapterAttemptResult,
AdapterBindingHandle,
AdapterCapabilities,
AdapterEventSink,
CancelAttemptContext,
CancelDispatchResult,
OpenBindingInput,
ResumeBindingInput,
RuntimeAdapter
} from '../codingAgent/interface'
const nodeSqliteFactory = DatabaseSync as unknown as DatabaseFactory
const createdDirs: string[] = []
const openStores: SqliteAgentStore[] = []
const OWNER = 'owner-1'
afterEach(() => {
// Reset so native session ids are per-test deterministic (native-1, native-2, …)
// rather than depending on how many tests ran before.
nativeSessionCounter = 0
// Windows holds the SQLite file open until the handle is closed; rmSync would
// otherwise fail with EPERM.
for (const store of openStores.splice(0)) {
try {
store.close()
} catch {
// already closed
}
}
for (const dir of createdDirs.splice(0)) {
rmSync(dir, { recursive: true, force: true })
}
})
// === Fake adapter ============================================================
interface FakeAdapterOptions {
adapterId?: string
supportsNativeResume?: boolean
requiresPinnedWorker?: boolean
/** Resolve executeAttempt with this text (default: echo the prompt). */
reply?: string
/** Throw from executeAttempt instead of resolving. */
executeError?: Error
/** Throw from resumeBinding (drives the stale-binding path). */
resumeError?: Error
/** Called with the assembled prompt on every attempt. */
onPrompt?: (prompt: string) => void
/** Block executeAttempt until the returned resolver is called. */
gate?: { promise: Promise<void> }
}
interface FakeAdapter extends RuntimeAdapter {
readonly calls: {
openBinding: OpenBindingInput[]
resumeBinding: ResumeBindingInput[]
executeAttempt: AdapterAttemptContext[]
cancelAttempt: CancelAttemptContext[]
}
}
function capabilitiesFor(options: FakeAdapterOptions): AdapterCapabilities {
const supportsNativeResume = options.supportsNativeResume ?? true
return {
resumeFidelity: supportsNativeResume ? 'native' : 'none',
supportsNativeResume,
supportsCancellation: true,
acknowledgesCancellation: true,
requiresPinnedWorker: options.requiresPinnedWorker ?? false,
supportsModelSwitching: true,
supportsArtifactEmission: false,
supportsTools: true,
restartBehavior: supportsNativeResume
? 'native_bindings_survive'
: 'process_local_bindings_stale'
}
}
let nativeSessionCounter = 0
function fakeAdapter(options: FakeAdapterOptions = {}): FakeAdapter {
const adapterId = options.adapterId ?? 'test-adapter'
const calls: FakeAdapter['calls'] = {
openBinding: [],
resumeBinding: [],
executeAttempt: [],
cancelAttempt: []
}
let aborted = false
return {
adapterId,
capabilities: capabilitiesFor(options),
calls,
async start() {
/* no-op fake */
},
async stop() {
/* no-op fake */
},
async openBinding(input: OpenBindingInput): Promise<AdapterBindingHandle> {
calls.openBinding.push(input)
nativeSessionCounter += 1
return {
sessionId: input.sessionId,
adapterId,
// INV-AGENT: never echo the Omi sessionId back as the native id.
adapterNativeSessionId: `native-${nativeSessionCounter}`,
resumeFidelity: capabilitiesFor(options).resumeFidelity,
cwd: input.cwd,
model: input.model
}
},
async resumeBinding(input: ResumeBindingInput): Promise<AdapterBindingHandle> {
calls.resumeBinding.push(input)
if (options.resumeError) throw options.resumeError
return {
sessionId: input.sessionId,
adapterId,
adapterNativeSessionId: input.adapterNativeSessionId,
resumeFidelity: capabilitiesFor(options).resumeFidelity,
cwd: input.cwd,
model: input.model
}
},
async executeAttempt(
context: AdapterAttemptContext,
sink: AdapterEventSink,
signal: AbortSignal
): Promise<AdapterAttemptResult> {
calls.executeAttempt.push(context)
const promptText = context.prompt
.map((block) => (block.type === 'text' ? block.text : ''))
.join('')
options.onPrompt?.(promptText)
sink({ type: 'text_delta', text: 'streaming' })
if (options.gate) {
signal.addEventListener('abort', () => {
aborted = true
})
await options.gate.promise
if (aborted || signal.aborted) {
throw new Error('aborted')
}
}
if (options.executeError) throw options.executeError
return {
text: options.reply ?? `echo: ${promptText}`,
adapterSessionId: context.binding.adapterNativeSessionId,
terminalStatus: 'succeeded',
inputTokens: 10,
outputTokens: 20,
costUsd: 0.01
}
},
async cancelAttempt(context: CancelAttemptContext): Promise<CancelDispatchResult> {
calls.cancelAttempt.push(context)
return {
accepted: true,
dispatchAttempted: true,
adapterAcknowledged: true
}
}
}
}
// === Harness =================================================================
/** A fresh temp dir; reopen the same dir to simulate a process restart. */
function newStoreDir(): string {
const dir = mkdtempSync(join(tmpdir(), 'omi-kernel-'))
createdDirs.push(dir)
return dir
}
/**
* Open (or REOPEN) the store at a given dir. Reopening runs reconcileStartup —
* that is what makes a restart test a real restart rather than a fresh kernel
* over a live store object.
*/
function openStoreAt(dir: string): SqliteAgentStore {
const store = new SqliteAgentStore({
databaseFactory: nodeSqliteFactory,
databasePath: join(dir, 'omi-agentd.sqlite3')
})
openStores.push(store)
return store
}
function newStore(): SqliteAgentStore {
return openStoreAt(newStoreDir())
}
function newKernel(
adapter: FakeAdapter,
store: SqliteAgentStore = newStore()
): { kernel: AgentRuntimeKernel; store: SqliteAgentStore; adapter: FakeAdapter } {
const registry = new AdapterRegistry()
registry.register(adapter.adapterId, () => adapter, 2)
const kernel = new AgentRuntimeKernel({ store, registry, runtimeNodeId: 'node-a' })
return { kernel, store, adapter }
}
function runInput(overrides: Record<string, unknown> = {}) {
return {
ownerId: OWNER,
surfaceKind: 'main_chat',
externalRefKind: 'chat',
externalRefId: 'default',
defaultAdapterId: 'test-adapter',
adapterId: 'test-adapter',
clientId: 'client-1',
requestId: 'request-1',
prompt: 'hello there',
...overrides
} as Parameters<AgentRuntimeKernel['executeRun']>[0]
}
// === Tests ===================================================================
describe('AgentRuntimeKernel — executeRun', () => {
it('runs a turn end to end and persists the terminal state', async () => {
const { kernel, store } = newKernel(fakeAdapter())
const result = await kernel.executeRun(runInput())
expect(result.terminalStatus).toBe('succeeded')
expect(result.text).toContain('echo:')
expect(result.run.status).toBe('succeeded')
expect(result.attempt.status).toBe('succeeded')
expect(result.run.inputTokens).toBe(10)
expect(result.run.costUsd).toBeCloseTo(0.01)
// The adapter's native session id is recorded, and is never the Omi id.
const binding = store.getRow('SELECT * FROM adapter_bindings WHERE session_id = ?', [
result.session.sessionId
])
expect(binding.adapter_native_session_id).toMatch(/^native-/)
expect(binding.adapter_native_session_id).not.toBe(result.session.sessionId)
expect(result.adapterSessionId).not.toBe(result.session.sessionId)
})
it('records both turns of the conversation on the kernel transcript (INV-CHAT-1)', async () => {
const { kernel, store } = newKernel(fakeAdapter({ reply: 'the answer' }))
await kernel.executeRun(runInput({ prompt: 'the question' }))
const turns = store.allRows(
'SELECT role, content FROM conversation_turns ORDER BY created_at_ms ASC, rowid ASC'
)
expect(turns.map((turn) => turn.role)).toEqual(['user', 'assistant'])
expect(turns[0].content).toBe('the question')
expect(turns[1].content).toBe('the answer')
})
it('emits a subscriber event stream for the run lifecycle', async () => {
const { kernel } = newKernel(fakeAdapter())
const types: string[] = []
kernel.subscribe((event) => types.push(event.type))
await kernel.executeRun(runInput())
expect(types).toContain('run.queued')
expect(types).toContain('binding.created')
expect(types).toContain('attempt.started')
expect(types).toContain('run.running')
expect(types).toContain('message.delta')
expect(types).toContain('message.completed')
expect(types).toContain('run.succeeded')
})
it('fails the run and records the failure when the adapter throws', async () => {
const adapter = fakeAdapter({ executeError: new Error('adapter exploded') })
const { kernel } = newKernel(adapter)
const result = await kernel.executeRun(runInput({ maxAttempts: 1 }))
expect(result.terminalStatus).toBe('failed')
expect(result.run.status).toBe('failed')
expect(result.run.errorCode).toBe('adapter_execution_failed')
expect(result.run.errorMessage).toBeTruthy()
expect(adapter.calls.executeAttempt).toHaveLength(1)
})
it('fails the run when the adapter is not registered', async () => {
const { kernel } = newKernel(fakeAdapter())
const result = await kernel.executeRun(
runInput({ adapterId: 'test-adapter', defaultAdapterId: 'test-adapter' })
)
expect(result.terminalStatus).toBe('succeeded')
// A second session pinned to an adapter with no registered pool.
// (requestId must differ — the store enforces UNIQUE(client_id, request_id)
// as its run-idempotency key.)
const unregistered = await kernel.executeRun(
runInput({
requestId: 'request-2',
externalRefId: 'other',
adapterId: 'ghost-adapter',
defaultAdapterId: 'ghost-adapter'
})
)
expect(unregistered.terminalStatus).toBe('failed')
expect(unregistered.run.errorCode).toBe('adapter_not_registered')
})
it('cancels an in-flight run and dispatches the cancellation to the adapter', async () => {
let release!: () => void
const gate = {
promise: new Promise<void>((resolve) => {
release = resolve
})
}
const adapter = fakeAdapter({ gate })
const { kernel } = newKernel(adapter)
const running = kernel.executeRun(runInput({ maxAttempts: 1 }))
// Wait for the attempt to actually reach the adapter before cancelling.
await waitFor(() => adapter.calls.executeAttempt.length === 1)
const runId = adapter.calls.executeAttempt[0].runId
const cancelled = await kernel.cancelRun(runId, { ownerId: OWNER })
expect(cancelled.accepted).toBe(true)
expect(cancelled.dispatchAttempted).toBe(true)
expect(cancelled.adapterAcknowledged).toBe(true)
// The kernel supplies host identity on cancel; ownerId is authoritative.
expect(adapter.calls.cancelAttempt[0].ownerId).toBe(OWNER)
release()
const result = await running
expect(result.terminalStatus).toBe('cancelled')
expect(result.run.status).toBe('cancelled')
})
it('refuses to cancel an already-terminal run', async () => {
const { kernel } = newKernel(fakeAdapter())
const result = await kernel.executeRun(runInput())
const cancelled = await kernel.cancelRun(result.run.runId, { ownerId: OWNER })
expect(cancelled.accepted).toBe(false)
expect(cancelled.dispatchAttempted).toBe(false)
})
it('rejects a run whose session belongs to another owner', async () => {
const { kernel } = newKernel(fakeAdapter())
const first = await kernel.executeRun(runInput())
await expect(
kernel.executeRun(runInput({ sessionId: first.session.sessionId, ownerId: 'intruder' }))
).rejects.toThrow(/does not belong to owner/)
})
})
describe('AgentRuntimeKernel — bindings', () => {
it('resumes the existing binding on a second turn rather than opening a new one', async () => {
const adapter = fakeAdapter({ supportsNativeResume: true })
const { kernel } = newKernel(adapter)
const first = await kernel.executeRun(runInput({ requestId: 'r1' }))
const second = await kernel.executeRun(runInput({ requestId: 'r2' }))
expect(first.session.sessionId).toBe(second.session.sessionId)
expect(adapter.calls.openBinding).toHaveLength(1)
expect(adapter.calls.resumeBinding).toHaveLength(1)
expect(adapter.calls.resumeBinding[0].adapterNativeSessionId).toMatch(/^native-/)
})
// NOTE: this is a fresh-kernel test, NOT a restart test. It shares the SAME
// store object, so reconcileStartup() never runs. It proves the kernel keeps no
// in-memory binding cache — the binding row alone is enough to resume. The real
// restart (store closed and reopened, reconcileStartup fired) is the next test;
// do not let this one stand in for it.
it('resumes from the binding row alone, with no in-memory carryover', async () => {
const store = newStore()
const firstAdapter = fakeAdapter()
const first = newKernel(firstAdapter, store)
const seeded = await first.kernel.executeRun(runInput())
const nativeId = firstAdapter.calls.executeAttempt[0].binding.adapterNativeSessionId
const secondAdapter = fakeAdapter()
const second = newKernel(secondAdapter, store)
const resumed = await second.kernel.executeRun(runInput({ requestId: 'r2' }))
expect(resumed.session.sessionId).toBe(seeded.session.sessionId)
expect(secondAdapter.calls.openBinding).toHaveLength(0)
expect(secondAdapter.calls.resumeBinding).toHaveLength(1)
expect(secondAdapter.calls.resumeBinding[0].adapterNativeSessionId).toBe(nativeId)
expect(resumed.terminalStatus).toBe('succeeded')
})
it('resumes a natively-resumable binding across a real process restart', async () => {
// First process: run a turn, then CLOSE the store.
const dir = newStoreDir()
const firstAdapter = fakeAdapter({ supportsNativeResume: true })
const firstStore = openStoreAt(dir)
const seeded = await newKernel(firstAdapter, firstStore).kernel.executeRun(runInput())
const nativeId = firstAdapter.calls.executeAttempt[0].binding.adapterNativeSessionId
firstStore.close()
// Second process: reopen the SAME file. reconcileStartup() runs on open and
// NULLs adapter_instance_id on every binding — a native binding must still
// resume, because its native session id outlives the process.
const secondStore = openStoreAt(dir)
const secondAdapter = fakeAdapter({ supportsNativeResume: true })
const resumed = await newKernel(secondAdapter, secondStore).kernel.executeRun(
runInput({ requestId: 'r2' })
)
expect(resumed.session.sessionId).toBe(seeded.session.sessionId)
expect(secondAdapter.calls.resumeBinding).toHaveLength(1)
expect(secondAdapter.calls.resumeBinding[0].adapterNativeSessionId).toBe(nativeId)
expect(secondAdapter.calls.openBinding).toHaveLength(0)
expect(resumed.terminalStatus).toBe('succeeded')
})
it('never reuses a process-local binding across a real process restart', async () => {
// The dangerous case: a pinned-worker adapter whose session lives only inside
// the dead process. Its handle is worthless after a restart, and reusing it
// would hand the model a dead worker. reconcileStartup marks every
// resume_fidelity='none' binding stale precisely to stop that; without it,
// canUseProcessLocalBinding (which keys on adapterInstanceId === runtimeNodeId)
// would happily reuse the row.
const dir = newStoreDir()
const firstAdapter = fakeAdapter({ supportsNativeResume: false, requiresPinnedWorker: true })
const firstStore = openStoreAt(dir)
await newKernel(firstAdapter, firstStore).kernel.executeRun(runInput())
const beforeRestart = firstStore.getRow(
'SELECT status, resume_fidelity, adapter_instance_id FROM adapter_bindings'
)
expect(String(beforeRestart.status)).toBe('active')
expect(String(beforeRestart.resume_fidelity)).toBe('none')
expect(String(beforeRestart.adapter_instance_id)).toBe('node-a')
firstStore.close()
// Restart: reopening the file runs reconcileStartup.
const secondStore = openStoreAt(dir)
const staleAfterRestart = secondStore.getRow(
"SELECT status, adapter_instance_id FROM adapter_bindings WHERE resume_fidelity = 'none'"
)
expect(String(staleAfterRestart.status)).toBe('stale')
expect(staleAfterRestart.adapter_instance_id).toBeNull()
const secondAdapter = fakeAdapter({ supportsNativeResume: false, requiresPinnedWorker: true })
const result = await newKernel(secondAdapter, secondStore).kernel.executeRun(
runInput({ requestId: 'r2' })
)
// A brand new binding, never the dead process-local one.
expect(secondAdapter.calls.resumeBinding).toHaveLength(0)
expect(secondAdapter.calls.openBinding).toHaveLength(1)
expect(result.terminalStatus).toBe('succeeded')
expect(
Number(
secondStore.getRow('SELECT COUNT(*) AS c FROM adapter_bindings WHERE status = ?', [
'active'
]).c
)
).toBe(1)
})
it('marks the binding stale and opens a fresh one when resume fails', async () => {
const store = newStore()
await newKernel(fakeAdapter(), store).kernel.executeRun(runInput())
const resumeFails = fakeAdapter({ resumeError: new Error('native session gone') })
const { kernel } = newKernel(resumeFails, store)
const result = await kernel.executeRun(runInput({ requestId: 'r2', maxAttempts: 2 }))
expect(resumeFails.calls.resumeBinding).toHaveLength(1)
// Attempt 1 fails stale; attempt 2 opens a new binding and succeeds.
expect(resumeFails.calls.openBinding).toHaveLength(1)
expect(result.terminalStatus).toBe('succeeded')
const staleCount = store.getRow(
"SELECT COUNT(*) AS count FROM adapter_bindings WHERE status = 'stale'"
)
expect(Number(staleCount.count)).toBe(1)
})
it('opens a new binding instead of resuming when the cwd changed', async () => {
const store = newStore()
const adapter = fakeAdapter()
const { kernel } = newKernel(adapter, store)
await kernel.executeRun(runInput({ cwd: '/work/a' }))
await kernel.executeRun(runInput({ requestId: 'r2', cwd: '/work/b' }))
expect(adapter.calls.openBinding).toHaveLength(2)
expect(adapter.calls.resumeBinding).toHaveLength(0)
})
it('invalidates the active bindings of an owner', async () => {
const adapter = fakeAdapter()
const { kernel, store } = newKernel(adapter)
await kernel.executeRun(runInput())
const invalidated = kernel.invalidateBindings({ ownerId: OWNER, surfaceKind: 'main_chat' })
expect(invalidated.invalidatedBindingIds).toHaveLength(1)
// Assert the STATE CHANGE, not the returned array — that array is built by
// the SELECT that runs before the UPDATE, so it would still be populated if
// the UPDATE were deleted.
const binding = store.getRow('SELECT status FROM adapter_bindings WHERE binding_id = ?', [
invalidated.invalidatedBindingIds[0]
])
expect(String(binding.status)).toBe('invalid')
// And the next run must open a fresh binding rather than resume the dead one.
await kernel.executeRun(runInput({ requestId: 'r2' }))
expect(adapter.calls.openBinding).toHaveLength(2)
expect(adapter.calls.resumeBinding).toHaveLength(0)
})
})
describe('AgentRuntimeKernel — single-active-run enforcement (INV-AGENT)', () => {
// STORE-LEVEL guard, exercised while the kernel holds a live attempt: this
// calls store.insertAttempt directly, so it proves the DB authority index — not
// a kernel code path. The kernel-level equivalent is the binding-lock test
// below (two concurrent runs on one session). Duplicates store.test.ts's index
// coverage on purpose, under a live run.
it('store authority index refuses a second active attempt while a run is in flight', async () => {
let release!: () => void
const gate = {
promise: new Promise<void>((resolve) => {
release = resolve
})
}
const adapter = fakeAdapter({ gate })
const { kernel, store } = newKernel(adapter)
const running = kernel.executeRun(runInput({ maxAttempts: 1 }))
await waitFor(() => adapter.calls.executeAttempt.length === 1)
const runId = adapter.calls.executeAttempt[0].runId
// The store's partial-unique authority index is what backs this: a run may
// only ever hold one active attempt.
expect(() =>
store.insertAttempt({
runId,
attemptNo: 99,
status: 'running',
adapterId: 'test-adapter',
adapterInstanceId: 'node-a',
runtimeNodeId: 'node-a',
retryable: 0
})
).toThrow()
release()
await running
})
it('serializes two concurrent runs on one session behind the binding lock', async () => {
const adapter = fakeAdapter()
const { kernel } = newKernel(adapter)
const [a, b] = await Promise.all([
kernel.executeRun(runInput({ requestId: 'r1' })),
kernel.executeRun(runInput({ requestId: 'r2' }))
])
expect(a.session.sessionId).toBe(b.session.sessionId)
expect(a.terminalStatus).toBe('succeeded')
expect(b.terminalStatus).toBe('succeeded')
// Exactly one binding was opened for the session despite the race.
expect(adapter.calls.openBinding).toHaveLength(1)
})
})
describe('AgentRuntimeKernel — sendAgentMessage', () => {
it('continues an existing session by id', async () => {
const adapter = fakeAdapter()
const { kernel } = newKernel(adapter)
const first = await kernel.executeRun(runInput())
const followUp = await kernel.sendAgentMessage({
sessionId: first.session.sessionId,
ownerId: OWNER,
clientId: 'client-1',
requestId: 'request-2',
prompt: 'follow up'
})
expect(followUp.session.sessionId).toBe(first.session.sessionId)
expect(followUp.terminalStatus).toBe('succeeded')
expect(adapter.calls.executeAttempt).toHaveLength(2)
expect(adapter.calls.openBinding).toHaveLength(1)
})
})
describe('AgentRuntimeKernel — leaf-role guards (INV-AGENT)', () => {
it('lets a trusted user spawn a background agent', async () => {
const { kernel } = newKernel(fakeAdapter())
const spawned = await kernel.spawnBackgroundAgent({
ownerId: OWNER,
clientId: 'client-1',
requestId: 'request-bg',
prompt: 'do the thing',
adapterId: 'test-adapter',
defaultAdapterId: 'test-adapter',
trustedUserSpawn: true
})
expect(spawned.session.executionRole).toBe('leaf')
expect(spawned.run.status).toBe('queued')
})
it('surfaces a hung background spawn as a failed run in listSessions (silent-death regression)', async () => {
// The original silent death: a stalled ACP turn left the run pinned 'running'
// forever, invisible to list_agent_sessions — the ONE read the LLM status tool
// and the bar pill both project from — so a status query reported "no active
// agents". With the ACP no-progress watchdog now bounding the turn, the adapter
// rejects; the run must land 'failed' AND surface in the floating_bar
// projection listSessions returns, so the pill/status shows Failed, never a
// phantom stall. Here the fake adapter rejects with a no-progress-shaped error,
// standing in for the real watchdog firing.
const adapter = fakeAdapter({
executeError: new Error('test-adapter produced no progress for 300 seconds')
})
const { kernel, store } = newKernel(adapter)
const spawned = await kernel.spawnBackgroundAgent({
ownerId: OWNER,
clientId: 'client-1',
requestId: 'request-hung',
prompt: 'build a snake game',
adapterId: 'test-adapter',
defaultAdapterId: 'test-adapter',
trustedUserSpawn: true
})
// spawnBackgroundAgent is fire-and-forget; the failure is recorded async.
await waitFor(
() =>
String(
store.getRow('SELECT status FROM runs WHERE run_id = ?', [spawned.run.runId]).status
) === 'failed'
)
// The exact read list_agent_sessions performs (surfaceKind 'floating_bar').
const sessions = kernel.listSessions({ ownerId: OWNER, surfaceKind: 'floating_bar' })
const summary = sessions.find((s) => s.session.sessionId === spawned.session.sessionId)
expect(summary).toBeDefined()
const run = summary?.activeRun ?? summary?.latestRun
expect(run?.status).toBe('failed')
expect(run?.errorCode).toBe('adapter_execution_failed')
})
it('refuses an agent-originated spawn with no caller session', async () => {
const { kernel } = newKernel(fakeAdapter())
await expect(
kernel.spawnBackgroundAgent({
ownerId: OWNER,
clientId: 'client-1',
requestId: 'request-bg',
prompt: 'do the thing',
adapterId: 'test-adapter',
defaultAdapterId: 'test-adapter'
})
).rejects.toThrow(/requires a coordinator caller session/)
})
it('refuses a leaf worker spawning another background agent', async () => {
const { kernel } = newKernel(fakeAdapter())
const leaf = await kernel.spawnBackgroundAgent({
ownerId: OWNER,
clientId: 'client-1',
requestId: 'request-bg',
prompt: 'parent work',
adapterId: 'test-adapter',
defaultAdapterId: 'test-adapter',
trustedUserSpawn: true
})
await expect(
kernel.spawnBackgroundAgent({
ownerId: OWNER,
clientId: 'client-1',
requestId: 'request-bg-2',
prompt: 'child work',
adapterId: 'test-adapter',
defaultAdapterId: 'test-adapter',
callerSessionId: leaf.session.sessionId
})
).rejects.toThrow(/Leaf workers cannot create background agents/)
})
})
describe('AgentRuntimeKernel — surface sessions and transcript', () => {
it('resolves one canonical session per surface and reuses it', async () => {
const { kernel } = newKernel(fakeAdapter())
const surfaceRef = {
surfaceKind: 'main_chat',
externalRefKind: 'chat',
externalRefId: 'default'
}
const first = kernel.resolveSurfaceSession({ ownerId: OWNER, surfaceRef })
const again = kernel.resolveSurfaceSession({ ownerId: OWNER, surfaceRef })
expect(again.agentSessionId).toBe(first.agentSessionId)
expect(again.conversationId).toBe(first.conversationId)
const run = await kernel.executeRun(runInput())
expect(run.session.sessionId).toBe(first.agentSessionId)
})
it('records a surface turn and returns it in the main-chat tail', () => {
const { kernel } = newKernel(fakeAdapter())
kernel.recordSurfaceTurn({
ownerId: OWNER,
surfaceRef: {
surfaceKind: 'main_chat',
externalRefKind: 'chat',
externalRefId: 'default'
},
userText: 'ping',
assistantText: 'pong',
origin: 'typed'
})
const tail = kernel.getMainChatTurnTail(OWNER)
expect(tail.turns.map((turn) => turn.content)).toEqual(['ping', 'pong'])
})
it('clears owner state and invalidates its bindings', async () => {
const { kernel, store } = newKernel(fakeAdapter())
await kernel.executeRun(runInput())
const cleared = kernel.clearOwnerState(OWNER)
expect(cleared.invalidatedBindingIds.length).toBeGreaterThan(0)
// Assert the state change, not the returned array (which the pre-UPDATE
// SELECT populates). No binding of this owner may remain usable.
expect(
Number(
store.getRow('SELECT COUNT(*) AS c FROM adapter_bindings WHERE status = ?', ['active']).c
)
).toBe(0)
})
})
describe('AgentRuntimeKernel — startup reconciliation', () => {
// STORE-LEVEL: calls store.reconcileStartup() directly against rows the kernel
// wrote. The kernel-level consequences of a restart (bindings resumed vs. never
// reused) are the two "real process restart" tests above.
it('store reconcileStartup orphans a run and attempt left in flight by a crash', async () => {
let release!: () => void
const gate = {
promise: new Promise<void>((resolve) => {
release = resolve
})
}
const adapter = fakeAdapter({ gate })
const store = newStore()
const { kernel } = newKernel(adapter, store)
const running = kernel.executeRun(runInput({ maxAttempts: 1 }))
await waitFor(() => adapter.calls.executeAttempt.length === 1)
const runId = adapter.calls.executeAttempt[0].runId
// Simulate the process dying mid-attempt: the row stays 'running'.
const reconciled = store.reconcileStartup()
expect(reconciled.orphanedRunIds).toContain(runId)
expect(reconciled.orphanedAttemptIds).toHaveLength(1)
expect(store.getRow('SELECT status FROM runs WHERE run_id = ?', [runId]).status).toBe(
'orphaned'
)
release()
await running.catch(() => undefined)
})
})
describe('AgentRuntimeKernel — turn context assembly', () => {
it('injects the coordinator route and context packet into a main-chat prompt', async () => {
const prompts: string[] = []
const adapter = fakeAdapter({ onPrompt: (prompt) => prompts.push(prompt) })
const { kernel, store } = newKernel(adapter)
await kernel.executeRun(runInput({ prompt: 'what should I do next' }))
expect(prompts).toHaveLength(1)
const prompt = prompts[0]
expect(prompt).toContain('[Desktop Coordinator Route Context]')
expect(prompt).toContain('routeIntent=')
expect(prompt).toContain('# Context Packet')
expect(prompt).toContain('# User Message')
expect(prompt).toContain('what should I do next')
// The context packet is really persisted (the stub-free path), with its
// redacted preview, and the packet id in the prompt matches the row.
const packet = store.getRow('SELECT * FROM desktop_context_packets')
expect(prompt).toContain(String(packet.packet_id))
expect(String(packet.owner_id)).toBe(OWNER)
})
it('suppresses the coordinator route on an explicit agent-control-tool turn', async () => {
const prompts: string[] = []
const adapter = fakeAdapter({ onPrompt: (prompt) => prompts.push(prompt) })
const { kernel } = newKernel(adapter)
await kernel.executeRun(runInput({ prompt: 'call spawn_background_agent for me' }))
expect(prompts[0]).not.toContain('[Desktop Coordinator Route Context]')
expect(prompts[0]).not.toContain('# Context Packet')
})
it('gives a leaf worker the no-spawn execution boundary', async () => {
const prompts: string[] = []
const adapter = fakeAdapter({ onPrompt: (prompt) => prompts.push(prompt) })
const { kernel } = newKernel(adapter)
await kernel.spawnBackgroundAgent({
ownerId: OWNER,
clientId: 'client-1',
requestId: 'request-bg',
prompt: 'background work',
surfaceKind: 'background_agent',
externalRefKind: 'pill',
externalRefId: 'pill-1',
adapterId: 'test-adapter',
defaultAdapterId: 'test-adapter',
trustedUserSpawn: true
})
await waitFor(() => prompts.length === 1)
expect(prompts[0]).toContain('# Execution Boundary')
expect(prompts[0]).toContain('background agents cannot create more agents')
})
it('carries the prior transcript into the next turn', async () => {
const prompts: string[] = []
// A non-resumable adapter has no native history, so the kernel must inject
// the full transcript tail rather than only the undelivered delta.
const adapter = fakeAdapter({
supportsNativeResume: false,
requiresPinnedWorker: true,
onPrompt: (prompt) => prompts.push(prompt),
reply: 'first answer'
})
const { kernel } = newKernel(adapter)
await kernel.executeRun(runInput({ prompt: 'first question' }))
await kernel.executeRun(runInput({ requestId: 'r2', prompt: 'second question' }))
expect(prompts).toHaveLength(2)
expect(prompts[1]).toContain('<conversation_history>')
expect(prompts[1]).toContain('first question')
expect(prompts[1]).toContain('first answer')
})
})
describe('AgentRuntimeKernel — desktop action queue and intent routing', () => {
it('surfaces a failed run on the action queue', async () => {
const { kernel } = newKernel(fakeAdapter({ executeError: new Error('boom') }))
await kernel.executeRun(runInput({ maxAttempts: 1 }))
const queue = kernel.listDesktopActionQueue({ ownerId: OWNER })
expect(queue.some((item) => item.kind === 'failed_run')).toBe(true)
})
it('routes an ordinary utterance to a new run when nothing matches', () => {
const { kernel } = newKernel(fakeAdapter())
const route = kernel.routeDesktopIntent({
ownerId: OWNER,
utterance: 'hello',
surfaceKind: 'main_chat'
})
expect(route.intent).toBe('new_run')
})
it('routes an ambiguous external send to a dispatch', () => {
const { kernel } = newKernel(fakeAdapter())
const route = kernel.routeDesktopIntent({
ownerId: OWNER,
utterance: 'email this to the team',
surfaceKind: 'main_chat'
})
expect(route.intent).toBe('dispatch')
})
it('resumes a healthy related session', async () => {
const { kernel } = newKernel(fakeAdapter())
const seeded = await kernel.executeRun(runInput())
const route = kernel.routeDesktopIntent({
ownerId: OWNER,
utterance: 'and now the next step',
surfaceKind: 'main_chat'
})
expect(route.intent).toBe('resume')
expect(route.sessionId).toBe(seeded.session.sessionId)
})
it('answers a status question from local state', async () => {
const { kernel } = newKernel(fakeAdapter())
await kernel.executeRun(runInput({ surfaceKind: 'floating_bar', externalRefId: 'bar' }))
const route = kernel.routeDesktopIntent({
ownerId: OWNER,
utterance: "what's running",
surfaceKind: 'main_chat'
})
expect(route.intent).toBe('quick_answer')
})
it('hides a queue item behind an attention override', async () => {
const { kernel } = newKernel(fakeAdapter({ executeError: new Error('boom') }))
const failed = await kernel.executeRun(runInput({ maxAttempts: 1 }))
kernel.setDesktopAttentionOverride({
ownerId: OWNER,
subjectKind: 'run',
subjectId: failed.run.runId,
// A fixed timestamp: suppression keys on dismissedAtMs being non-null, so
// there is no reason to make this test depend on the wall clock.
dismissedAtMs: 1_000_000_000
})
const queue = kernel.listDesktopActionQueue({ ownerId: OWNER })
expect(queue.some((item) => item.subjectId === failed.run.runId)).toBe(false)
})
})
describe('AgentRuntimeKernel — execution role derived from the surface (INV-AGENT)', () => {
// REGRESSION: executionRoleForSurface had no production caller. macOS derives
// the role at its transport boundary; Windows is in-process and has no
// transport, so the kernel is the funnel. Before the fix, resolveSession
// defaulted an unset role to 'coordinator', so a run created directly on a LEAF
// surface became a coordinator session — and, because the role is persisted on
// the session row, kept coordinator spawn rights forever. That is a leaf escape.
it.each([
['background_agent', undefined],
['delegated_agent', undefined],
['floating_bar', 'pill']
])('derives leaf for surface %s/%s', async (surfaceKind, externalRefKind) => {
const { kernel } = newKernel(fakeAdapter())
const result = await kernel.executeRun(
runInput({
surfaceKind,
externalRefKind: externalRefKind ?? 'ref',
externalRefId: 'x-1'
})
)
expect(result.session.executionRole).toBe('leaf')
})
it('derives coordinator for an ordinary chat surface', async () => {
const { kernel } = newKernel(fakeAdapter())
const result = await kernel.executeRun(runInput())
expect(result.session.executionRole).toBe('coordinator')
const bar = await kernel.executeRun(
runInput({ requestId: 'r2', surfaceKind: 'floating_bar', externalRefKind: 'chat' })