forked from BasedHardware/omi
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathworker-pool.ts
More file actions
424 lines (383 loc) · 13.8 KB
/
Copy pathworker-pool.ts
File metadata and controls
424 lines (383 loc) · 13.8 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
import type {
AdapterBindingHandle,
RuntimeAdapter,
} from "../adapters/interface.js";
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;
}
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;
private unhealthy = false;
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 || this.unhealthy) 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 || this.unhealthy) return null;
return this.pinnedBindingId;
}
get pinnedBindingIdForRecovery(): string | null {
return this.pinnedBindingId;
}
markUnhealthy(): void {
this.unhealthy = true;
}
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;
recycleWorkerOnError?: boolean;
shouldRecycleWorkerOnError?: (error: unknown) => boolean;
onWorkerBindingInvalidated?: (bindingId: string | null) => void;
onWorkerRecycled?: (
bindingId: string | null,
outcome: { stopSucceeded: boolean; bindingInvalidationSucceeded: boolean }
) => void;
}
export class AdapterWorkerRecycledError extends Error {
readonly bindingId: string | null;
readonly stopSucceeded: boolean;
readonly bindingInvalidationSucceeded: boolean;
readonly originalError: unknown;
constructor(
originalError: unknown,
bindingId: string | null,
stopSucceeded: boolean,
bindingInvalidationSucceeded: boolean
) {
super(originalError instanceof Error ? originalError.message : String(originalError));
this.name = "AdapterWorkerRecycledError";
this.bindingId = bindingId;
this.stopSucceeded = stopSucceeded;
this.bindingInvalidationSucceeded = bindingInvalidationSucceeded;
this.originalError = originalError;
}
}
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;
} catch (error) {
if (
!options?.recycleWorkerOnError
|| options.shouldRecycleWorkerOnError?.(error) === false
) throw error;
const bindingId = worker.pinnedBindingIdForRecovery;
// Poison synchronously before the finally block drains waiters. Otherwise
// a queued lease can reacquire this process-local worker after its throw.
worker.markUnhealthy();
const index = this.workers.indexOf(worker);
if (index >= 0) this.workers.splice(index, 1);
if (bindingId) this.protectedPinnedBindingIds.delete(bindingId);
this.rejectWaitersForRecycledBinding(bindingId);
let bindingInvalidationSucceeded = true;
try {
options.onWorkerBindingInvalidated?.(bindingId);
} catch {
bindingInvalidationSucceeded = false;
// Keep this bounded: persistence exceptions may contain database paths.
console.error("[agent-runtime] worker recovery binding invalidation failed");
}
let stopSucceeded = true;
try {
await worker.adapter.stop();
} catch {
stopSucceeded = false;
}
try {
options.onWorkerRecycled?.(bindingId, {
stopSucceeded,
bindingInvalidationSucceeded,
});
} catch {
// Telemetry must not prevent deterministic worker cleanup. The thrown
// recovery error still carries both bounded lifecycle outcomes.
console.error("[agent-runtime] worker recovery event persistence failed");
}
throw new AdapterWorkerRecycledError(
error,
bindingId,
stopSucceeded,
bindingInvalidationSucceeded
);
} finally {
if (succeeded && options?.protectPinnedBindingAfterWork) {
this.protectPinnedBinding(worker.idlePinnedBindingId);
}
this.drainWaiters();
}
}
private rejectWaitersForRecycledBinding(bindingId: string | null): void {
if (!bindingId) return;
for (let i = this.waiters.length - 1; i >= 0; i -= 1) {
const waiter = this.waiters[i]!;
if (waiter.binding?.bindingId !== bindingId) continue;
this.waiters.splice(i, 1);
const error = new Error(`Adapter binding was recycled: ${bindingId}`);
error.name = "StaleAdapterBindingError";
waiter.reject(error);
}
}
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}`);
}
}