forked from BasedHardware/omi
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpiMono.test.ts
More file actions
1159 lines (1015 loc) · 40.6 KB
/
Copy pathpiMono.test.ts
File metadata and controls
1159 lines (1015 loc) · 40.6 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
// Ported from desktop/macos/agent/tests/pi-mono-adapter.test.ts, adapted to the
// Windows adapter: the constructor takes an options object (piPath /
// extensionPath / nodeBin), the subprocess spawns `nodeBin [piPath, ...args]`
// under ELECTRON_RUN_AS_NODE, and the RuntimeAdapter forwards only the narrow
// AdapterStreamEvent set to the kernel sink. All subprocess machinery is mocked;
// no real pi CLI is spawned.
import { EventEmitter } from 'node:events'
import { PassThrough } from 'node:stream'
import { existsSync, readFileSync, writeFileSync } from 'node:fs'
import { dirname, join } from 'node:path'
import { fileURLToPath } from 'node:url'
import { spawn } from 'child_process'
import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest'
import {
PiMonoAdapter,
PiMonoRuntimeAdapter,
HarnessFeature,
resolveBundledPi,
piCliCandidates,
resolveBundledExtension,
piExtensionCandidates
} from './piMono'
import type { AdapterAttemptContext, AdapterStreamEvent } from './interface'
import { AdapterWorkerPool } from '../agentKernel/workerPool'
vi.mock('child_process', async () => {
const actual = await vi.importActual<typeof import('child_process')>('child_process')
return {
...actual,
spawn: vi.fn()
}
})
type FakeProc = EventEmitter & {
stdin: PassThrough
stdout: PassThrough
stderr: PassThrough
kill: ReturnType<typeof vi.fn>
pid: number
}
let currentProc: FakeProc | null = null
function newFakeProc(): FakeProc {
// kill is a no-op (does NOT emit exit) so restart tests can spawn cleanly —
// matches the macOS test harness.
return Object.assign(new EventEmitter(), {
stdin: new PassThrough(),
stdout: new PassThrough(),
stderr: new PassThrough(),
kill: vi.fn(),
pid: 4242
}) as FakeProc
}
// Typed view of the adapter's private surface so tests can drive RPC handlers
// and inspect internal state without `any` (lint forbids explicit any).
interface PiInternals {
sendCommand: (cmd: Record<string, unknown>) => void
handleTurnEnd: (event: Record<string, unknown>) => void
handleToolStart: (event: Record<string, unknown>) => void
handleToolEnd: (event: Record<string, unknown>) => void
contextFilePath: string
sessions: Map<string, unknown>
process: EventEmitter | null
activePromptGeneration: number
pendingRequests: Map<number, unknown>
eventHandler: ((event: unknown) => void) | null
}
function internals(adapter: PiMonoAdapter): PiInternals {
return adapter as unknown as PiInternals
}
function createAdapter(
configOverrides: {
authToken?: string
omiApiBaseUrl?: string
onRestart?: (reason: string) => void
} = {}
): { adapter: PiMonoAdapter; events: Array<Record<string, unknown>> } {
const adapter = new PiMonoAdapter(
{ authToken: 'test-token', ...configOverrides },
{ piPath: '/fake/pi', extensionPath: '/fake/ext.ts', nodeBin: '/fake/node' }
)
const events: Array<Record<string, unknown>> = []
internals(adapter).sendCommand = vi.fn()
return { adapter, events }
}
function seedSessions(adapter: PiMonoAdapter, ...sessionIds: string[]): void {
const sessions = internals(adapter).sessions
for (const sessionId of sessionIds) {
sessions.set(sessionId, { cwd: '/tmp' })
}
}
type AttemptContextOverrides = Omit<Partial<AdapterAttemptContext>, 'binding'> & {
binding?: Partial<AdapterAttemptContext['binding']>
}
function makeAttemptContext(overrides: AttemptContextOverrides = {}): AdapterAttemptContext {
const attemptId = overrides.attemptId ?? 'att_runtime'
const sessionId = overrides.sessionId ?? 'ses_runtime'
const adapterNativeSessionId = overrides.binding?.adapterNativeSessionId ?? 'session-1'
return {
sessionId,
ownerId: overrides.ownerId ?? 'owner-runtime',
requestId: overrides.requestId ?? 'request-runtime',
clientId: overrides.clientId ?? 'client-runtime',
runId: overrides.runId ?? 'run_runtime',
attemptId,
binding: {
bindingId: 'bind-runtime',
sessionId,
adapterId: 'pi-mono',
adapterNativeSessionId,
resumeFidelity: 'none',
cwd: '/tmp',
...overrides.binding
},
prompt: overrides.prompt ?? [{ type: 'text', text: 'hello' }],
tools: overrides.tools,
mode: overrides.mode ?? 'act',
metadata: overrides.metadata
}
}
function makeTurnEndEvent(text: string, totalCost = 1.25): Record<string, unknown> {
return {
type: 'turn_end',
message: {
role: 'assistant',
content: [{ type: 'text', text }],
usage: {
input: 11,
output: 7,
cacheRead: 3,
cacheWrite: 2,
totalTokens: 23,
cost: { input: 0.1, output: 0.2, cacheRead: 0.3, cacheWrite: 0.4, total: totalCost }
}
}
}
}
function makeErrorTurnEndEvent(errorMessage: string): Record<string, unknown> {
return {
type: 'turn_end',
message: { role: 'assistant', errorMessage, content: [] }
}
}
beforeEach(() => {
currentProc = null
vi.mocked(spawn).mockReset()
vi.mocked(spawn).mockImplementation(() => {
currentProc = newFakeProc()
return currentProc as never
})
})
afterEach(() => {
vi.restoreAllMocks()
})
describe('PiMonoAdapter prompt correlation', () => {
it('writes the active runtime attempt context before prompt execution', async () => {
const { adapter } = createAdapter()
seedSessions(adapter, 'session-1')
const runtime = new PiMonoRuntimeAdapter(adapter)
const execution = runtime.executeAttempt(
makeAttemptContext({ metadata: { protocolVersion: 2, disableSwiftBackedTools: true } }),
() => {},
new AbortController().signal
)
const relayContext = JSON.parse(readFileSync(internals(adapter).contextFilePath, 'utf8'))
expect(relayContext).toMatchObject({
adapterId: 'pi-mono',
protocolVersion: 2,
requestId: 'request-runtime',
clientId: 'client-runtime',
sessionId: 'ses_runtime',
runId: 'run_runtime',
attemptId: 'att_runtime',
adapterSessionId: 'session-1',
disableSwiftBackedTools: true
})
internals(adapter).handleTurnEnd(makeTurnEndEvent('done'))
await expect(execution).resolves.toMatchObject({ terminalStatus: 'succeeded' })
expect(existsSync(internals(adapter).contextFilePath)).toBe(false)
})
it('carries the host tool-relay pipe/token into the per-turn context file', async () => {
const register = vi.fn((sessionId: string) => ({
pipePath: '\\\\.\\pipe\\omi-tool-relay-test',
token: `tok-${sessionId}`
}))
const adapter = new PiMonoAdapter(
{ authToken: 'test-token', registerToolRelay: register },
{ piPath: '/fake/pi', extensionPath: '/fake/ext.ts', nodeBin: '/fake/node' }
)
internals(adapter).sendCommand = vi.fn()
seedSessions(adapter, 'session-1')
const runtime = new PiMonoRuntimeAdapter(adapter)
const execution = runtime.executeAttempt(
makeAttemptContext({ sessionId: 'ses_wire' }),
() => {},
new AbortController().signal
)
const ctx = JSON.parse(readFileSync(internals(adapter).contextFilePath, 'utf8'))
// Token is keyed to the KERNEL session id (host-derived authority), not the
// adapter-native session id.
expect(register).toHaveBeenCalledWith('ses_wire')
expect(ctx.bridgePipe).toBe('\\\\.\\pipe\\omi-tool-relay-test')
expect(ctx.bridgeToken).toBe('tok-ses_wire')
internals(adapter).handleTurnEnd(makeTurnEndEvent('done'))
await expect(execution).resolves.toMatchObject({ terminalStatus: 'succeeded' })
})
it('omits the bridge target when no relay is registered (graceful)', async () => {
const { adapter } = createAdapter() // no registerToolRelay callback
seedSessions(adapter, 'session-1')
const runtime = new PiMonoRuntimeAdapter(adapter)
const execution = runtime.executeAttempt(
makeAttemptContext(),
() => {},
new AbortController().signal
)
const ctx = JSON.parse(readFileSync(internals(adapter).contextFilePath, 'utf8'))
expect(ctx.bridgePipe).toBeUndefined()
expect(ctx.bridgeToken).toBeUndefined()
internals(adapter).handleTurnEnd(makeTurnEndEvent('done'))
await expect(execution).resolves.toMatchObject({ terminalStatus: 'succeeded' })
})
it('removes the runtime attempt context after adapter errors', async () => {
const { adapter } = createAdapter()
internals(adapter).sendCommand = vi.fn(() => {
throw new Error('adapter send failed')
})
seedSessions(adapter, 'session-1')
const runtime = new PiMonoRuntimeAdapter(adapter)
await expect(
runtime.executeAttempt(
makeAttemptContext({ attemptId: 'att_error' }),
() => {},
new AbortController().signal
)
).rejects.toThrow('adapter send failed')
expect(existsSync(internals(adapter).contextFilePath)).toBe(false)
})
it('removes the runtime attempt context after abort (cancelled result)', async () => {
const { adapter } = createAdapter()
seedSessions(adapter, 'session-1')
const runtime = new PiMonoRuntimeAdapter(adapter)
const controller = new AbortController()
const execution = runtime.executeAttempt(
makeAttemptContext({ attemptId: 'att_abort' }),
() => {},
controller.signal
)
expect(JSON.parse(readFileSync(internals(adapter).contextFilePath, 'utf8')).attemptId).toBe(
'att_abort'
)
controller.abort()
await expect(execution).resolves.toMatchObject({ terminalStatus: 'cancelled' })
expect(existsSync(internals(adapter).contextFilePath)).toBe(false)
})
it('rejects a concurrent attempt without clearing the active attempt context', async () => {
const { adapter } = createAdapter()
seedSessions(adapter, 'session-1', 'session-2')
const runtime = new PiMonoRuntimeAdapter(adapter)
const first = runtime.executeAttempt(
makeAttemptContext({
attemptId: 'att_first',
binding: { adapterNativeSessionId: 'session-1' }
}),
() => {},
new AbortController().signal
)
expect(JSON.parse(readFileSync(internals(adapter).contextFilePath, 'utf8')).attemptId).toBe(
'att_first'
)
const second = runtime.executeAttempt(
makeAttemptContext({
attemptId: 'att_second',
binding: { adapterNativeSessionId: 'session-2' }
}),
() => {},
new AbortController().signal
)
await expect(second).rejects.toThrow('pi-mono prompt already in flight')
expect(JSON.parse(readFileSync(internals(adapter).contextFilePath, 'utf8')).attemptId).toBe(
'att_first'
)
internals(adapter).handleTurnEnd(makeTurnEndEvent('first done'))
await expect(first).resolves.toMatchObject({ terminalStatus: 'succeeded' })
expect(existsSync(internals(adapter).contextFilePath)).toBe(false)
})
it('removes invalid relay context for the completed attempt', () => {
const { adapter } = createAdapter()
writeFileSync(internals(adapter).contextFilePath, '{invalid json')
adapter.clearRelayContextForAttempt('att_invalid')
expect(existsSync(internals(adapter).contextFilePath)).toBe(false)
})
// M1 — ported from desktop/macos/agent/tests/pi-mono-adapter.test.ts:246-286.
// A runtime attempt writes a relay context; after it completes, a DIRECT
// sendPrompt (no 7th relayContext arg) must not carry a stale context — the
// writeRelayContext(undefined) path clears it (piMono.ts:816-829). A stale
// file is seeded first so the assertion actually exercises the clear.
it('clears stale relay context when direct prompt execution has no runtime context', async () => {
const { adapter } = createAdapter()
seedSessions(adapter, 'session-1')
const runtime = new PiMonoRuntimeAdapter(adapter)
const execution = runtime.executeAttempt(
makeAttemptContext({ binding: { adapterNativeSessionId: 'session-1' } }),
() => {},
new AbortController().signal
)
expect(existsSync(internals(adapter).contextFilePath)).toBe(true)
internals(adapter).handleTurnEnd(makeTurnEndEvent('done'))
await expect(execution).resolves.toMatchObject({ terminalStatus: 'succeeded' })
// Simulate a leftover context from a prior run so the direct-prompt clear is
// observable rather than trivially-already-absent.
writeFileSync(
internals(adapter).contextFilePath,
JSON.stringify({ adapterId: 'pi-mono', attemptId: 'att_stale' })
)
expect(existsSync(internals(adapter).contextFilePath)).toBe(true)
const directPrompt = adapter.sendPrompt(
'session-1',
[{ type: 'text', text: 'direct' }],
[],
'act',
() => {},
async () => ''
)
expect(existsSync(internals(adapter).contextFilePath)).toBe(false)
internals(adapter).handleTurnEnd(makeTurnEndEvent('direct done'))
await expect(directPrompt).resolves.toMatchObject({ text: 'direct done' })
})
it('rejects a second prompt while one is in flight and never emits a result event', async () => {
const { adapter, events } = createAdapter()
seedSessions(adapter, 'session-1', 'session-2')
const firstPrompt = adapter.sendPrompt(
'session-1',
[{ type: 'text', text: 'first' }],
[],
'act',
(event) => events.push(event as Record<string, unknown>),
async () => ''
)
await expect(
adapter.sendPrompt(
'session-2',
[{ type: 'text', text: 'second' }],
[],
'act',
(event) => events.push(event as Record<string, unknown>),
async () => ''
)
).rejects.toThrow('pi-mono prompt already in flight')
internals(adapter).handleTurnEnd(makeTurnEndEvent('first response', 2.5))
await expect(firstPrompt).resolves.toMatchObject({
text: 'first response',
sessionId: 'session-1',
costUsd: 2.5,
inputTokens: 11,
outputTokens: 7,
cacheReadTokens: 3,
cacheWriteTokens: 2
})
expect(events.some((event) => event.type === 'result')).toBe(false)
})
it('rejects turn_end errors instead of resolving success', async () => {
const { adapter, events } = createAdapter()
seedSessions(adapter, 'session-1')
const prompt = adapter.sendPrompt(
'session-1',
[{ type: 'text', text: 'fail' }],
[],
'act',
(event) => events.push(event as Record<string, unknown>),
async () => ''
)
internals(adapter).handleTurnEnd(makeErrorTurnEndEvent('adapter failed'))
await expect(prompt).rejects.toThrow('adapter failed')
expect(events).toContainEqual(
expect.objectContaining({
type: 'error',
message: 'adapter failed',
adapterSessionId: 'session-1'
})
)
})
it('does not report success after a required agent-control operation fails', async () => {
const { adapter, events } = createAdapter()
seedSessions(adapter, 'session-1')
const prompt = adapter.sendPrompt(
'session-1',
[{ type: 'text', text: 'create a child' }],
[],
'act',
(event) => events.push(event as Record<string, unknown>),
async () => ''
)
internals(adapter).handleToolEnd({
toolName: 'spawn_agent',
toolCallId: 'tool-spawn',
result: {
content: [
{
type: 'text',
text: JSON.stringify({
ok: false,
error: {
code: 'missing_request_context',
message: 'missing active Omi request context'
}
})
}
]
}
})
internals(adapter).handleTurnEnd(
makeTurnEndEvent('I could not create the child, but I am done.')
)
await expect(prompt).rejects.toThrow('Required spawn_agent operation failed')
expect(events).toContainEqual(
expect.objectContaining({
type: 'error',
message: expect.stringContaining('missing active Omi request context')
})
)
})
it('allows a successful required-control retry to complete the parent turn', async () => {
const { adapter } = createAdapter()
seedSessions(adapter, 'session-1')
const prompt = adapter.sendPrompt(
'session-1',
[{ type: 'text', text: 'create a child' }],
[],
'act',
() => {},
async () => ''
)
internals(adapter).handleToolEnd({
toolName: 'spawn_agent',
toolCallId: 'tool-spawn-1',
result: {
content: [
{
type: 'text',
text: JSON.stringify({ ok: false, error: { message: 'temporary failure' } })
}
]
}
})
internals(adapter).handleToolEnd({
toolName: 'spawn_agent',
toolCallId: 'tool-spawn-2',
result: { content: [{ type: 'text', text: JSON.stringify({ ok: true }) }] }
})
internals(adapter).handleTurnEnd(makeTurnEndEvent('child created'))
await expect(prompt).resolves.toMatchObject({ text: 'child created' })
})
it('does not let an unrelated control success erase a failed obligation', async () => {
const { adapter } = createAdapter()
seedSessions(adapter, 'session-1')
const prompt = adapter.sendPrompt(
'session-1',
[{ type: 'text', text: 'create both children' }],
[],
'act',
() => {},
async () => ''
)
internals(adapter).handleToolStart({
toolName: 'spawn_agent',
toolCallId: 'tool-child-a',
args: { objective: 'child A' }
})
internals(adapter).handleToolEnd({
toolName: 'spawn_agent',
toolCallId: 'tool-child-a',
result: {
content: [
{ type: 'text', text: JSON.stringify({ ok: false, error: { message: 'failed A' } }) }
]
}
})
internals(adapter).handleToolStart({
toolName: 'spawn_agent',
toolCallId: 'tool-child-b',
args: { objective: 'child B' }
})
internals(adapter).handleToolEnd({
toolName: 'spawn_agent',
toolCallId: 'tool-child-b',
result: { content: [{ type: 'text', text: JSON.stringify({ ok: true }) }] }
})
internals(adapter).handleTurnEnd(makeTurnEndEvent('child B created'))
await expect(prompt).rejects.toThrow('failed A')
})
it('resolves abort before turn_end and drops the late completion', async () => {
const { adapter, events } = createAdapter()
seedSessions(adapter, 'session-1')
const prompt = adapter.sendPrompt(
'session-1',
[{ type: 'text', text: 'abort me' }],
[],
'act',
(event) => events.push(event as Record<string, unknown>),
async () => ''
)
adapter.abort('session-1')
await expect(prompt).resolves.toMatchObject({
text: '',
sessionId: 'session-1',
costUsd: 0,
inputTokens: 0,
outputTokens: 0
})
internals(adapter).handleTurnEnd(makeTurnEndEvent('late response'))
expect(events).toEqual([])
expect(internals(adapter).activePromptGeneration).toBe(0)
})
it('drops stray turn_end events when no prompt is in flight', () => {
const { adapter, events } = createAdapter()
internals(adapter).eventHandler = (event) => events.push(event as Record<string, unknown>)
internals(adapter).handleTurnEnd(makeTurnEndEvent('orphaned response'))
expect(events).toEqual([])
expect(internals(adapter).pendingRequests.size).toBe(0)
})
})
describe('PiMonoAdapter subprocess death', () => {
it('rejects the pending prompt and clears relay context when the subprocess exits', async () => {
const { adapter } = createAdapter()
await adapter.start()
seedSessions(adapter, 'session-1')
const runtime = new PiMonoRuntimeAdapter(adapter)
const execution = runtime.executeAttempt(
makeAttemptContext({ attemptId: 'att_exit' }),
() => {},
new AbortController().signal
)
expect(JSON.parse(readFileSync(internals(adapter).contextFilePath, 'utf8')).attemptId).toBe(
'att_exit'
)
currentProc!.emit('exit', 7)
await expect(execution).rejects.toThrow('pi-mono process exited (code 7)')
expect(existsSync(internals(adapter).contextFilePath)).toBe(false)
})
})
describe('PiMonoAdapter restart lifecycle', () => {
it('restarts the subprocess and notifies observers after a system-prompt change', async () => {
const onRestart = vi.fn()
const { adapter } = createAdapter({ onRestart })
await adapter.start()
await expect(adapter.setSystemPrompt('new prompt')).resolves.toBe(true)
expect(onRestart).toHaveBeenCalledWith('systemPrompt')
expect(spawn).toHaveBeenCalledTimes(2)
await adapter.stop()
})
})
describe('PiMonoAdapter spawn shape (behavioral, Windows)', () => {
it('spawns Electron-as-Node with the pi cli.js as argv[0] and the rpc flags', async () => {
const adapter = new PiMonoAdapter(
{ authToken: 'test-token' },
{ piPath: '/fake/pi.js', extensionPath: '/fake/ext.ts', nodeBin: '/fake/node' }
)
await adapter.start()
expect(spawn).toHaveBeenCalledOnce()
const [cmd, args, options] = vi.mocked(spawn).mock.calls[0] as [
string,
string[],
{ env: Record<string, string>; shell?: boolean; windowsHide?: boolean }
]
// Windows deviation from macOS: spawn nodeBin, not the cli directly.
expect(cmd).toBe('/fake/node')
expect(args[0]).toBe('/fake/pi.js')
expect(args).toEqual(
expect.arrayContaining([
'--mode',
'rpc',
'-e',
'/fake/ext.ts',
'--provider',
'omi',
'--model',
'omi-sonnet'
])
)
expect(args).not.toContain('--no-extensions')
expect(options.shell).toBe(false)
expect(options.windowsHide).toBe(true)
await adapter.stop()
})
it('runs the child as plain Node and scrubs credentials in the env', async () => {
const adapter = new PiMonoAdapter(
{ authToken: 'firebase-id-token-xyz', omiApiBaseUrl: 'https://api.example/v2' },
{ piPath: '/fake/pi.js', extensionPath: '/fake/ext.ts', nodeBin: '/fake/node' }
)
await adapter.start()
const [, , options] = vi.mocked(spawn).mock.calls[0] as [
string,
string[],
{ env: Record<string, string> }
]
expect(options.env.ELECTRON_RUN_AS_NODE).toBe('1')
// Raw token, NOT "Bearer <token>" (pi prepends the scheme itself).
expect(options.env.OMI_API_KEY).toBe('firebase-id-token-xyz')
expect(options.env.OMI_API_BASE_URL).toBe('https://api.example/v2')
expect(options.env.OMI_ADAPTER_ID).toBe('pi-mono')
expect(options.env.OMI_EXECUTION_ROLE).toBe('coordinator')
// Serviceable product-tool projection rides the (role-static) spawn env and
// includes the reference serviceable executor so the extension can filter its
// advertisement to it. The per-turn pipe/token do NOT ride the env.
expect(options.env.OMI_SERVICEABLE_PRODUCT_TOOLS).toContain('capture_screen')
expect(options.env.OMI_BRIDGE_PIPE).toBeUndefined()
expect(options.env.OMI_BRIDGE_TOKEN).toBeUndefined()
// Upstream provider secret must never reach the extension.
expect(options.env.ANTHROPIC_API_KEY).toBeUndefined()
await adapter.stop()
})
it('refuses to start without an auth token', async () => {
const adapter = new PiMonoAdapter(
{},
{ piPath: '/fake/pi.js', extensionPath: '/fake/ext.ts', nodeBin: '/fake/node' }
)
await expect(adapter.start()).rejects.toThrow('requires config.authToken')
expect(spawn).not.toHaveBeenCalled()
})
it('injects the complete OMI_BYOK_* set from config.byokEnv at spawn', async () => {
const adapter = new PiMonoAdapter(
{
authToken: 'firebase-id-token-xyz',
byokEnv: {
OMI_BYOK_OPENAI: 'sk-openai',
OMI_BYOK_ANTHROPIC: 'sk-ant',
OMI_BYOK_GEMINI: 'gm-key',
OMI_BYOK_DEEPGRAM: 'dg-key'
}
},
{ piPath: '/fake/pi.js', extensionPath: '/fake/ext.ts', nodeBin: '/fake/node' }
)
await adapter.start()
const [, , options] = vi.mocked(spawn).mock.calls[0] as [
string,
string[],
{ env: Record<string, string> }
]
expect(options.env.OMI_BYOK_OPENAI).toBe('sk-openai')
expect(options.env.OMI_BYOK_ANTHROPIC).toBe('sk-ant')
expect(options.env.OMI_BYOK_GEMINI).toBe('gm-key')
expect(options.env.OMI_BYOK_DEEPGRAM).toBe('dg-key')
// Managed OMI_API_KEY is always the Firebase token, never a BYOK key.
expect(options.env.OMI_API_KEY).toBe('firebase-id-token-xyz')
await adapter.stop()
})
it('scrubs inherited OMI_BYOK_* and injects nothing when config.byokEnv is absent', async () => {
// A stale OMI_BYOK_* leaking from the parent env must never reach the child.
process.env.OMI_BYOK_OPENAI = 'stale-inherited'
try {
const adapter = new PiMonoAdapter(
{ authToken: 'firebase-id-token-xyz' },
{ piPath: '/fake/pi.js', extensionPath: '/fake/ext.ts', nodeBin: '/fake/node' }
)
await adapter.start()
const [, , options] = vi.mocked(spawn).mock.calls[0] as [
string,
string[],
{ env: Record<string, string> }
]
expect(options.env.OMI_BYOK_OPENAI).toBeUndefined()
await adapter.stop()
} finally {
delete process.env.OMI_BYOK_OPENAI
}
})
})
describe('PiMonoAdapter image channel (no bytes leak into text)', () => {
// Security property: screenshot pixels ride pi's separate cmd.images RPC and
// are NEVER concatenated into the text `message`. This mirrors the throw-gate
// in agentKernel/desktopContextPacket.ts (assertNoScreenshotBytes) which
// rejects any text snippet that looks like base64 image bytes.
const BASE64_BYTES_RE = /^[A-Za-z0-9+/]{400,}={0,2}$/
const DATA_URL_RE = /^data:image\//i
it('routes image bytes to cmd.images and keeps the text message clean', async () => {
const { adapter } = createAdapter()
seedSessions(adapter, 'session-1')
let captured: Record<string, unknown> | undefined
internals(adapter).sendCommand = (cmd) => {
captured = cmd
}
const imageBytes = 'A'.repeat(600) // >400 chars — trips the throw-gate pattern
const prompt = adapter.sendPrompt(
'session-1',
[
{ type: 'text', text: 'what is on my screen?' },
{ type: 'image', data: imageBytes, mimeType: 'image/jpeg' }
],
[],
'act',
() => {},
async () => ''
)
expect(captured).toBeDefined()
expect(captured!.type).toBe('prompt')
// The text channel contains ONLY the text block.
expect(captured!.message).toBe('what is on my screen?')
expect(String(captured!.message)).not.toContain(imageBytes)
// The image rides the separate images RPC field.
expect(captured!.images).toEqual([{ type: 'image', data: imageBytes, mimeType: 'image/jpeg' }])
// The text message would survive the desktopContextPacket throw-gate.
expect(DATA_URL_RE.test(String(captured!.message))).toBe(false)
expect(BASE64_BYTES_RE.test(String(captured!.message))).toBe(false)
internals(adapter).handleTurnEnd(makeTurnEndEvent('ok'))
await prompt
})
it('omits cmd.images entirely for a text-only prompt', async () => {
const { adapter } = createAdapter()
seedSessions(adapter, 'session-1')
let captured: Record<string, unknown> | undefined
internals(adapter).sendCommand = (cmd) => {
captured = cmd
}
const prompt = adapter.sendPrompt(
'session-1',
[{ type: 'text', text: 'plain text' }],
[],
'act',
() => {},
async () => ''
)
expect(captured!.message).toBe('plain text')
expect('images' in captured!).toBe(false)
internals(adapter).handleTurnEnd(makeTurnEndEvent('ok'))
await prompt
})
})
describe('PiMonoRuntimeAdapter sink event forwarding', () => {
it('forwards canonical stream events but drops tool_use and error from the narrow sink', async () => {
const { adapter } = createAdapter()
seedSessions(adapter, 'session-1')
const runtime = new PiMonoRuntimeAdapter(adapter)
const sinkEvents: AdapterStreamEvent[] = []
const execution = runtime.executeAttempt(
makeAttemptContext(),
(event) => sinkEvents.push(event),
new AbortController().signal
)
// text_delta and tool_activity are canonical stream events → forwarded.
internals(adapter).handleToolStart({
toolName: 'read',
toolCallId: 'call-1',
args: { path: '/tmp/x' }
})
// toolcall_end emits a `tool_use` harness event → must NOT reach the sink.
;(
adapter as unknown as { handleMessageUpdate: (e: Record<string, unknown>) => void }
).handleMessageUpdate({
type: 'message_update',
assistantMessageEvent: {
type: 'toolcall_end',
toolCall: { id: 'call-1', name: 'read', arguments: { path: '/tmp/x' } }
}
})
;(
adapter as unknown as { handleMessageUpdate: (e: Record<string, unknown>) => void }
).handleMessageUpdate({
type: 'message_update',
assistantMessageEvent: { type: 'text_delta', delta: 'hello' }
})
internals(adapter).handleTurnEnd(makeTurnEndEvent('done'))
await expect(execution).resolves.toMatchObject({ terminalStatus: 'succeeded' })
const types = sinkEvents.map((e) => e.type)
expect(types).toContain('tool_activity')
expect(types).toContain('text_delta')
expect(types).not.toContain('tool_use')
expect(types).not.toContain('error')
})
it('rejects executeAttempt on a turn_end error without surfacing an error event to the sink', async () => {
const { adapter } = createAdapter()
seedSessions(adapter, 'session-1')
const runtime = new PiMonoRuntimeAdapter(adapter)
const sinkEvents: AdapterStreamEvent[] = []
const execution = runtime.executeAttempt(
makeAttemptContext(),
(event) => sinkEvents.push(event),
new AbortController().signal
)
internals(adapter).handleTurnEnd(makeErrorTurnEndEvent('boom'))
await expect(execution).rejects.toThrow('boom')
expect(sinkEvents.map((e) => e.type)).not.toContain('error')
})
})
describe('PiMonoRuntimeAdapter binding + capabilities', () => {
it('opens a binding whose native session id differs from the Omi session id', async () => {
const { adapter } = createAdapter()
const runtime = new PiMonoRuntimeAdapter(adapter)
const binding = await runtime.openBinding({ sessionId: 'omi-ses', cwd: '/work' })
expect(binding.adapterId).toBe('pi-mono')
expect(binding.sessionId).toBe('omi-ses')
expect(binding.adapterNativeSessionId).not.toBe('omi-ses')
expect(binding.adapterNativeSessionId).toBeTruthy()
expect(binding.resumeFidelity).toBe('none')
await adapter.stop()
})
it('exposes managed-cloud, worker-pinned, non-resumable capabilities', () => {
const { adapter } = createAdapter()
const runtime = new PiMonoRuntimeAdapter(adapter)
expect(runtime.adapterId).toBe('pi-mono')
expect(runtime.capabilities).toMatchObject({
resumeFidelity: 'none',
supportsNativeResume: false,
supportsCancellation: true,
acknowledgesCancellation: false,
requiresPinnedWorker: true,
supportsModelSwitching: true,
supportsArtifactEmission: false,
supportsTools: true,
restartBehavior: 'process_local_bindings_stale'
})
})
it('acknowledges cancellation dispatch and marks the attempt cancelled', async () => {
const { adapter } = createAdapter()
seedSessions(adapter, 'session-1')
const runtime = new PiMonoRuntimeAdapter(adapter)
const result = await runtime.cancelAttempt({
sessionId: 'ses',
attemptId: 'att-1',
binding: makeAttemptContext().binding
})
expect(result).toMatchObject({
accepted: true,
dispatchAttempted: true,
adapterAcknowledged: false
})
})
it('does not advertise native session resume as a feature', () => {
const { adapter } = createAdapter()
expect(adapter.supportsFeature(HarnessFeature.SESSION_RESUME)).toBe(false)
expect(adapter.supportsFeature(HarnessFeature.BIDIRECTIONAL_RPC)).toBe(true)
expect(adapter.supportsFeature(HarnessFeature.COST_TRACKING)).toBe(true)
})
})
// Regression guard for the "(void 0) is not a function" adapter-construction crash:
// resolveBundledPi() used `import.meta.resolve`, which esbuild compiles to `(void 0)`
// in the CJS main bundle, throwing before any adapter catch could fire. The prior
// tests never caught it because they always inject an explicit `piPath`, so the
// real resolver was never exercised — and under vitest `import.meta.resolve` works
// anyway. These tests exercise the resolver directly and ban the unusable API.
describe('resolveBundledPi', () => {
it('resolves a real, existing dist/cli.js on the filesystem (no import.meta.resolve)', () => {
const cli = resolveBundledPi()
expect(existsSync(cli)).toBe(true)
expect(cli.replace(/\\/g, '/')).toMatch(
/node_modules\/@earendil-works\/pi-coding-agent\/dist\/cli\.js$/
)
})
it('constructing the adapter without a piPath override does not throw', () => {
// This is the exact path that crashed at runtime: no options.piPath and no
// PI_MONO_PATH → the constructor calls resolveBundledPi().
const prev = process.env.PI_MONO_PATH
delete process.env.PI_MONO_PATH
try {
expect(() => new PiMonoAdapter({ authToken: 'test-token' })).not.toThrow()
} finally {
if (prev !== undefined) process.env.PI_MONO_PATH = prev
}
})
it('lists the asar-unpacked packaged path first, then walks up for dev', () => {
const packaged = piCliCandidates('/anything', '/app/resources')
expect(packaged[0].replace(/\\/g, '/')).toBe(
'/app/resources/app.asar.unpacked/node_modules/@earendil-works/pi-coding-agent/dist/cli.js'
)
// With no resourcesPath (outside Electron) it walks up from moduleDir toward
// the hoisted node_modules — one candidate per ancestor directory.
const dev = piCliCandidates('/a/b/c', undefined)
expect(dev.every((p) => p.replace(/\\/g, '/').endsWith('/cli.js'))).toBe(true)
expect(dev.map((p) => p.replace(/\\/g, '/'))).toContain(
'/a/b/c/node_modules/@earendil-works/pi-coding-agent/dist/cli.js'
)
expect(dev.map((p) => p.replace(/\\/g, '/'))).toContain(
'/a/node_modules/@earendil-works/pi-coding-agent/dist/cli.js'
)
})
it('source code never uses import.meta.resolve (breaks in the CJS main bundle)', () => {
const src = readFileSync(join(dirname(fileURLToPath(import.meta.url)), 'piMono.ts'), 'utf8')
// Strip comments so this catches real usage, not the doc-comment that explains
// why the API is banned.
const code = src.replace(/\/\*[\s\S]*?\*\//g, '').replace(/(^|[^:])\/\/[^\n]*/g, '$1')