forked from BasedHardware/omi
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathworkerPool.ts
More file actions
353 lines (316 loc) · 11.4 KB
/
Copy pathworkerPool.ts
File metadata and controls
353 lines (316 loc) · 11.4 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
// Adapter worker pool — Windows port of the macOS agent runtime's worker-pool.ts
// (desktop/macos/agent/src/runtime/worker-pool.ts).
//
// A pool owns up to `maxWorkers` adapter instances and hands out exclusive,
// queued leases keyed on the binding. Pinned-worker adapters (Hermes/Codex,
// whose native session lives inside the adapter process) stay bound to one
// worker for the life of a binding; native-resumable adapters (Claude Code,
// OpenClaw) can float across workers. Pure concurrency logic — no kernel/store
// deps — so it unit-tests standalone with a fake adapter.
//
// macOS parity note: pi-mono is registered with the pi-mono-specific cap
// (`configuredPiMonoMaxWorkers`, default 2) — mirroring mac worker-pool.ts —
// while every other adapter keeps `configuredMaxWorkers` / DEFAULT_MAX_WORKERS.
import type { AdapterBindingHandle, RuntimeAdapter } from '../codingAgent/interface'
export const DEFAULT_MAX_WORKERS = 8
export const DEFAULT_PI_MONO_MAX_WORKERS = 2
export function configuredMaxWorkers(env = process.env): number {
const raw = env.OMI_AGENT_MAX_WORKERS
if (!raw) return DEFAULT_MAX_WORKERS
const parsed = Number.parseInt(raw, 10)
if (!Number.isFinite(parsed) || parsed < 1) return DEFAULT_MAX_WORKERS
return parsed
}
// pi-mono spawns one pi subprocess per pinned worker, so its pool is capped
// well below the generic default to bound concurrent subprocesses (mac parity).
export function configuredPiMonoMaxWorkers(env = process.env): number {
const raw = env.OMI_PI_MONO_MAX_WORKERS
if (!raw) return DEFAULT_PI_MONO_MAX_WORKERS
const parsed = Number.parseInt(raw, 10)
if (!Number.isFinite(parsed) || parsed < 1) return DEFAULT_PI_MONO_MAX_WORKERS
return parsed
}
export class AdapterWorker {
readonly workerId: string
readonly adapter: RuntimeAdapter
private activeAttemptId: string | null = null
private activeBindingId: string | null = null
private pinnedBindingId: string | null = null
constructor(workerId: string, adapter: RuntimeAdapter) {
this.workerId = workerId
this.adapter = adapter
}
get isBusy(): boolean {
return this.activeAttemptId !== null
}
canRun(binding?: AdapterBindingHandle): boolean {
if (this.isBusy) return false
if (!this.adapter.capabilities.requiresPinnedWorker) return true
if (!this.pinnedBindingId) return true
return Boolean(binding?.bindingId && binding.bindingId === this.pinnedBindingId)
}
hasActiveBinding(bindingId: string): boolean {
return this.activeBindingId === bindingId
}
hasPinnedBinding(bindingId: string): boolean {
return this.pinnedBindingId === bindingId
}
get idlePinnedBindingId(): string | null {
if (this.isBusy) return null
return this.pinnedBindingId
}
releaseIdlePinnedBinding(): string | null {
if (this.isBusy || !this.pinnedBindingId) {
return null
}
const bindingId = this.pinnedBindingId
this.pinnedBindingId = null
return bindingId
}
pinBinding(binding: AdapterBindingHandle): void {
if (!binding.bindingId) {
throw new Error('Pinned adapter workers require a bindingId')
}
if (this.pinnedBindingId && this.pinnedBindingId !== binding.bindingId) {
throw new Error(
`Worker ${this.workerId} is already pinned to binding ${this.pinnedBindingId}`
)
}
this.pinnedBindingId = binding.bindingId
}
replacePinnedBinding(replacesBindingId: string, binding: AdapterBindingHandle): void {
if (!binding.bindingId) {
throw new Error('Pinned adapter workers require a bindingId')
}
if (this.pinnedBindingId && this.pinnedBindingId !== replacesBindingId) {
throw new Error(
`Worker ${this.workerId} is pinned to binding ${this.pinnedBindingId}, not replacement source ${replacesBindingId}`
)
}
this.pinnedBindingId = binding.bindingId
}
async runExclusive<T>(
attemptId: string,
binding: AdapterBindingHandle | undefined,
work: () => Promise<T>
): Promise<T> {
if (this.activeAttemptId && this.activeAttemptId !== attemptId) {
throw new Error(`Worker ${this.workerId} already has active attempt ${this.activeAttemptId}`)
}
if (!this.activeAttemptId) {
this.reserve(attemptId, binding)
} else if (binding?.bindingId && this.activeBindingId !== binding.bindingId) {
throw new Error(
`Worker ${this.workerId} has active binding ${this.activeBindingId ?? '(none)'}`
)
}
try {
return await work()
} finally {
this.activeAttemptId = null
this.activeBindingId = null
}
}
reserve(attemptId: string, binding?: AdapterBindingHandle): void {
if (this.activeAttemptId) {
throw new Error(`Worker ${this.workerId} already has active attempt ${this.activeAttemptId}`)
}
this.activeAttemptId = attemptId
this.activeBindingId = binding?.bindingId ?? null
}
}
type AdapterFactory = () => RuntimeAdapter
type WorkerLeaseResolver = (worker: AdapterWorker) => void
interface WorkerLeaseOptions {
onIdlePinnedBindingEvicted?: (bindingId: string) => void
protectPinnedBindingAfterWork?: boolean
}
interface PendingWorkerLease {
binding?: AdapterBindingHandle
attemptId: string
options?: WorkerLeaseOptions
resolve: WorkerLeaseResolver
reject: (error: Error) => void
}
export class AdapterWorkerPool {
private readonly maxWorkers: number
private readonly adapterFactory: AdapterFactory
private readonly workers: AdapterWorker[] = []
private readonly waiters: PendingWorkerLease[] = []
private readonly protectedPinnedBindingIds = new Set<string>()
private nextWorkerId = 1
constructor(adapterFactory: AdapterFactory, maxWorkers = configuredMaxWorkers()) {
if (maxWorkers < 1) {
throw new Error('AdapterWorkerPool maxWorkers must be at least 1')
}
this.adapterFactory = adapterFactory
this.maxWorkers = maxWorkers
}
get size(): number {
return this.workers.length
}
get capacity(): number {
return this.maxWorkers
}
get requiresPinnedWorkers(): boolean {
return this.workers.some((worker) => worker.adapter.capabilities.requiresPinnedWorker)
}
releaseIdlePinnedBinding(): string | null {
for (const worker of this.workers) {
const idlePinnedBindingId = worker.idlePinnedBindingId
if (idlePinnedBindingId && this.protectedPinnedBindingIds.has(idlePinnedBindingId)) {
continue
}
const bindingId = worker.releaseIdlePinnedBinding()
if (bindingId) {
return bindingId
}
}
return null
}
protectPinnedBinding(bindingId: string | null | undefined): void {
if (bindingId) {
this.protectedPinnedBindingIds.add(bindingId)
}
}
unprotectPinnedBinding(bindingId: string | null | undefined): void {
if (bindingId) {
this.protectedPinnedBindingIds.delete(bindingId)
this.drainWaiters()
}
}
acquire(binding?: AdapterBindingHandle): AdapterWorker | null {
const bindingId = binding?.bindingId
if (bindingId) {
if (this.workers.some((worker) => worker.hasActiveBinding(bindingId))) {
return null
}
}
const pinnedIdle = bindingId
? this.workers.find((worker) => worker.canRun(binding) && worker.hasPinnedBinding(bindingId))
: undefined
const idle = pinnedIdle ?? this.workers.find((worker) => worker.canRun(binding))
if (idle) {
if (binding && idle.adapter.capabilities.requiresPinnedWorker) {
idle.pinBinding(binding)
}
return idle
}
if (this.workers.length >= this.maxWorkers) {
return null
}
const adapter = this.adapterFactory()
const worker = new AdapterWorker(`worker-${this.nextWorkerId++}`, adapter)
if (binding && adapter.capabilities.requiresPinnedWorker) {
worker.pinBinding(binding)
}
this.workers.push(worker)
return worker
}
async runExclusiveQueued<T>(
binding: AdapterBindingHandle | undefined,
attemptId: string,
work: (worker: AdapterWorker) => Promise<T>,
options?: WorkerLeaseOptions
): Promise<T> {
const worker = await this.acquireQueued(binding, attemptId, options)
let succeeded = false
try {
const result = await worker.runExclusive(attemptId, binding, () => work(worker))
succeeded = true
return result
} finally {
if (succeeded && options?.protectPinnedBindingAfterWork) {
this.protectPinnedBinding(worker.idlePinnedBindingId)
}
this.drainWaiters()
}
}
private acquireQueued(
binding: AdapterBindingHandle | undefined,
attemptId: string,
options?: WorkerLeaseOptions
): Promise<AdapterWorker> {
const worker =
this.acquire(binding) ?? this.acquireByEvictingIdlePinnedBinding(binding, options)
if (worker) {
worker.reserve(attemptId, binding)
return Promise.resolve(worker)
}
if (!this.canEventuallyAcquire(binding, options)) {
return Promise.reject(this.noCapacityError(binding))
}
return new Promise((resolve, reject) => {
this.waiters.push({ binding, attemptId, options, resolve, reject })
})
}
private drainWaiters(): void {
for (let i = 0; i < this.waiters.length; ) {
const waiter = this.waiters[i]!
let worker: AdapterWorker | null
try {
worker =
this.acquire(waiter.binding) ??
this.acquireByEvictingIdlePinnedBinding(waiter.binding, waiter.options)
} catch (error) {
this.waiters.splice(i, 1)
waiter.reject(error instanceof Error ? error : new Error(String(error)))
continue
}
if (!worker) {
i += 1
continue
}
this.waiters.splice(i, 1)
worker.reserve(waiter.attemptId, waiter.binding)
waiter.resolve(worker)
}
for (let i = 0; i < this.waiters.length; ) {
const waiter = this.waiters[i]!
if (this.canEventuallyAcquire(waiter.binding, waiter.options)) {
i += 1
continue
}
this.waiters.splice(i, 1)
waiter.reject(this.noCapacityError(waiter.binding))
}
}
private acquireByEvictingIdlePinnedBinding(
binding: AdapterBindingHandle | undefined,
options: WorkerLeaseOptions | undefined
): AdapterWorker | null {
if (binding || !options?.onIdlePinnedBindingEvicted) {
return null
}
for (const worker of this.workers) {
const evictedBindingId = worker.idlePinnedBindingId
if (!evictedBindingId) continue
if (this.protectedPinnedBindingIds.has(evictedBindingId)) continue
const releasedBindingId = worker.releaseIdlePinnedBinding()
if (releasedBindingId !== evictedBindingId) {
throw new Error(
`Worker ${worker.workerId} failed to release pinned binding ${evictedBindingId}`
)
}
options.onIdlePinnedBindingEvicted(evictedBindingId)
return worker
}
return null
}
private canEventuallyAcquire(
binding?: AdapterBindingHandle,
options?: WorkerLeaseOptions
): boolean {
if (this.workers.length < this.maxWorkers) return true
const bindingId = binding?.bindingId
return this.workers.some((worker) => {
if (!worker.adapter.capabilities.requiresPinnedWorker) return true
if (!bindingId && options?.onIdlePinnedBindingEvicted) return true
return Boolean(bindingId && worker.hasPinnedBinding(bindingId))
})
}
private noCapacityError(binding?: AdapterBindingHandle): Error {
const bindingLabel = binding?.bindingId ? `binding ${binding.bindingId}` : 'a new binding'
return new Error(`No adapter worker capacity available for ${bindingLabel}`)
}
}