forked from BasedHardware/omi
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathworkerPool.test.ts
More file actions
188 lines (166 loc) · 5.77 KB
/
Copy pathworkerPool.test.ts
File metadata and controls
188 lines (166 loc) · 5.77 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
// Tests for AdapterWorkerPool (macOS worker-pool.ts): exclusive+queued leases,
// binding-level serialization, capacity queueing, no-capacity rejection for
// pinned adapters, and pinned-worker reuse across sequential attempts.
import { describe, expect, it } from 'vitest'
import {
AdapterWorkerPool,
DEFAULT_PI_MONO_MAX_WORKERS,
configuredPiMonoMaxWorkers
} from './workerPool'
import type {
AdapterBindingHandle,
AdapterCapabilities,
RuntimeAdapter
} from '../codingAgent/interface'
function capabilities(requiresPinnedWorker: boolean): AdapterCapabilities {
return {
resumeFidelity: requiresPinnedWorker ? 'none' : 'native',
supportsNativeResume: !requiresPinnedWorker,
supportsCancellation: true,
acknowledgesCancellation: false,
requiresPinnedWorker,
supportsModelSwitching: true,
supportsArtifactEmission: false,
supportsTools: true,
restartBehavior: requiresPinnedWorker
? 'process_local_bindings_stale'
: 'native_bindings_survive'
}
}
function fakeAdapter(opts: { requiresPinnedWorker?: boolean } = {}): RuntimeAdapter {
return {
adapterId: 'test-adapter',
capabilities: capabilities(opts.requiresPinnedWorker ?? false),
async start() {
/* no-op fake */
},
async stop() {
/* no-op fake */
},
async openBinding(input) {
return {
sessionId: input.sessionId,
adapterId: 'test-adapter',
adapterNativeSessionId: 'native',
resumeFidelity: 'native',
cwd: input.cwd
}
},
async resumeBinding(input) {
return {
sessionId: input.sessionId,
adapterId: 'test-adapter',
adapterNativeSessionId: input.adapterNativeSessionId,
resumeFidelity: 'native',
cwd: input.cwd
}
},
async executeAttempt(context) {
return {
text: 'ok',
adapterSessionId: context.binding.adapterNativeSessionId,
terminalStatus: 'succeeded'
}
},
async cancelAttempt() {
return { accepted: true, dispatchAttempted: true, adapterAcknowledged: false }
}
}
}
function binding(bindingId: string): AdapterBindingHandle {
return {
bindingId,
sessionId: 'ses',
adapterId: 'test-adapter',
adapterNativeSessionId: `native-${bindingId}`,
resumeFidelity: 'native',
cwd: '/tmp'
}
}
function gate(): { promise: Promise<void>; open: () => void } {
let open!: () => void
const promise = new Promise<void>((resolve) => {
open = resolve
})
return { promise, open }
}
const flush = () => new Promise((resolve) => setImmediate(resolve))
describe('AdapterWorkerPool', () => {
it('serializes concurrent leases on the same binding', async () => {
const pool = new AdapterWorkerPool(() => fakeAdapter(), 4)
const order: string[] = []
const g = gate()
const b = binding('b1')
const first = pool.runExclusiveQueued(b, 'a1', async () => {
order.push('first-start')
await g.promise
order.push('first-end')
})
await flush()
const second = pool.runExclusiveQueued(b, 'a2', async () => {
order.push('second-start')
})
await flush()
expect(order).toEqual(['first-start']) // second is queued behind the active binding
g.open()
await Promise.all([first, second])
expect(order).toEqual(['first-start', 'first-end', 'second-start'])
})
it('queues a second lease when the single worker is busy, then runs it', async () => {
const pool = new AdapterWorkerPool(() => fakeAdapter(), 1)
const order: string[] = []
const g = gate()
const first = pool.runExclusiveQueued(binding('b1'), 'a1', async () => {
order.push('first-start')
await g.promise
order.push('first-end')
})
await flush()
const second = pool.runExclusiveQueued(binding('b2'), 'a2', async () => {
order.push('second-start')
})
await flush()
expect(order).toEqual(['first-start'])
expect(pool.size).toBe(1)
g.open()
await Promise.all([first, second])
expect(order).toEqual(['first-start', 'first-end', 'second-start'])
expect(pool.size).toBe(1)
})
it('rejects a new-binding lease when a pinned worker pool is at capacity', async () => {
const pool = new AdapterWorkerPool(() => fakeAdapter({ requiresPinnedWorker: true }), 1)
const g = gate()
const first = pool.runExclusiveQueued(binding('b1'), 'a1', async () => {
await g.promise
})
await flush()
await expect(pool.runExclusiveQueued(binding('b2'), 'a2', async () => {})).rejects.toThrow(
/No adapter worker capacity/
)
g.open()
await first
})
it('reuses a pinned worker for sequential attempts on the same binding', async () => {
const pool = new AdapterWorkerPool(() => fakeAdapter({ requiresPinnedWorker: true }), 1)
await pool.runExclusiveQueued(binding('b1'), 'a1', async () => {})
expect(pool.size).toBe(1)
await pool.runExclusiveQueued(binding('b1'), 'a2', async () => {})
expect(pool.size).toBe(1)
})
})
describe('configuredPiMonoMaxWorkers', () => {
it('defaults to DEFAULT_PI_MONO_MAX_WORKERS (2) when unset', () => {
expect(DEFAULT_PI_MONO_MAX_WORKERS).toBe(2)
expect(configuredPiMonoMaxWorkers({})).toBe(2)
})
it('honors a valid OMI_PI_MONO_MAX_WORKERS override', () => {
expect(configuredPiMonoMaxWorkers({ OMI_PI_MONO_MAX_WORKERS: '5' })).toBe(5)
expect(configuredPiMonoMaxWorkers({ OMI_PI_MONO_MAX_WORKERS: '1' })).toBe(1)
})
it('falls back to the default for non-numeric or out-of-range values', () => {
expect(configuredPiMonoMaxWorkers({ OMI_PI_MONO_MAX_WORKERS: 'nope' })).toBe(2)
expect(configuredPiMonoMaxWorkers({ OMI_PI_MONO_MAX_WORKERS: '0' })).toBe(2)
expect(configuredPiMonoMaxWorkers({ OMI_PI_MONO_MAX_WORKERS: '-3' })).toBe(2)
expect(configuredPiMonoMaxWorkers({ OMI_PI_MONO_MAX_WORKERS: '' })).toBe(2)
})
})