tywrap 0.1.1 → 0.2.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (63) hide show
  1. package/README.md +32 -5
  2. package/dist/cli.js +70 -3
  3. package/dist/cli.js.map +1 -1
  4. package/dist/config/index.d.ts.map +1 -1
  5. package/dist/config/index.js +47 -11
  6. package/dist/config/index.js.map +1 -1
  7. package/dist/core/generator.d.ts.map +1 -1
  8. package/dist/core/generator.js +6 -2
  9. package/dist/core/generator.js.map +1 -1
  10. package/dist/core/mapper.d.ts.map +1 -1
  11. package/dist/core/mapper.js +16 -4
  12. package/dist/core/mapper.js.map +1 -1
  13. package/dist/index.d.ts +2 -2
  14. package/dist/index.d.ts.map +1 -1
  15. package/dist/index.js +2 -2
  16. package/dist/index.js.map +1 -1
  17. package/dist/runtime/bridge-core.d.ts +64 -0
  18. package/dist/runtime/bridge-core.d.ts.map +1 -0
  19. package/dist/runtime/bridge-core.js +344 -0
  20. package/dist/runtime/bridge-core.js.map +1 -0
  21. package/dist/runtime/node.d.ts +161 -15
  22. package/dist/runtime/node.d.ts.map +1 -1
  23. package/dist/runtime/node.js +542 -218
  24. package/dist/runtime/node.js.map +1 -1
  25. package/dist/runtime/optimized-node.d.ts +19 -128
  26. package/dist/runtime/optimized-node.d.ts.map +1 -1
  27. package/dist/runtime/optimized-node.js +19 -627
  28. package/dist/runtime/optimized-node.js.map +1 -1
  29. package/dist/runtime/timed-out-request-tracker.d.ts +20 -0
  30. package/dist/runtime/timed-out-request-tracker.d.ts.map +1 -0
  31. package/dist/runtime/timed-out-request-tracker.js +48 -0
  32. package/dist/runtime/timed-out-request-tracker.js.map +1 -0
  33. package/dist/types/index.d.ts +3 -0
  34. package/dist/types/index.d.ts.map +1 -1
  35. package/dist/tywrap.d.ts +17 -4
  36. package/dist/tywrap.d.ts.map +1 -1
  37. package/dist/tywrap.js +68 -35
  38. package/dist/tywrap.js.map +1 -1
  39. package/dist/utils/codec.d.ts +22 -0
  40. package/dist/utils/codec.d.ts.map +1 -1
  41. package/dist/utils/codec.js +245 -55
  42. package/dist/utils/codec.js.map +1 -1
  43. package/dist/utils/logger.d.ts.map +1 -1
  44. package/dist/utils/logger.js +1 -0
  45. package/dist/utils/logger.js.map +1 -1
  46. package/dist/utils/python.d.ts.map +1 -1
  47. package/dist/utils/python.js.map +1 -1
  48. package/package.json +11 -2
  49. package/runtime/python_bridge.py +450 -69
  50. package/src/cli.ts +75 -3
  51. package/src/config/index.ts +58 -15
  52. package/src/core/generator.ts +6 -2
  53. package/src/core/mapper.ts +16 -4
  54. package/src/index.ts +2 -1
  55. package/src/runtime/bridge-core.ts +454 -0
  56. package/src/runtime/node.ts +725 -253
  57. package/src/runtime/optimized-node.ts +19 -844
  58. package/src/runtime/timed-out-request-tracker.ts +58 -0
  59. package/src/types/index.ts +3 -0
  60. package/src/tywrap.ts +91 -36
  61. package/src/utils/codec.ts +299 -82
  62. package/src/utils/logger.ts +1 -0
  63. package/src/utils/python.ts +0 -1
@@ -1,846 +1,21 @@
1
1
  /**
2
- * Optimized Node.js Runtime Bridge with Connection Pooling and Memory Management
3
- * High-performance Python subprocess management for production workloads
2
+ * @deprecated Import from './node.js' instead.
3
+ *
4
+ * OptimizedNodeBridge has been unified with NodeBridge. The NodeBridge class
5
+ * now supports both single-process mode (default) and multi-process pooling
6
+ * via the minProcesses/maxProcesses options.
7
+ *
8
+ * Migration:
9
+ * ```typescript
10
+ * // Before
11
+ * import { OptimizedNodeBridge, ProcessPoolOptions } from './optimized-node.js';
12
+ * const bridge = new OptimizedNodeBridge({ minProcesses: 2, maxProcesses: 4 });
13
+ *
14
+ * // After
15
+ * import { NodeBridge, NodeBridgeOptions } from './node.js';
16
+ * const bridge = new NodeBridge({ minProcesses: 2, maxProcesses: 4 });
17
+ * ```
18
+ *
19
+ * This file is maintained for backward compatibility only.
4
20
  */
5
-
6
- import { delimiter, isAbsolute, join, resolve } from 'node:path';
7
- import { fileURLToPath } from 'node:url';
8
- import type { ChildProcess } from 'child_process';
9
- import { EventEmitter } from 'events';
10
-
11
- import { globalCache } from '../utils/cache.js';
12
- import { decodeValueAsync } from '../utils/codec.js';
13
- import { getDefaultPythonPath } from '../utils/python.js';
14
- import { getVenvBinDir, getVenvPythonExe } from '../utils/runtime.js';
15
-
16
- import { RuntimeBridge } from './base.js';
17
- import { BridgeExecutionError, BridgeProtocolError } from './errors.js';
18
- import { TYWRAP_PROTOCOL } from './protocol.js';
19
- import { getComponentLogger } from '../utils/logger.js';
20
-
21
- const log = getComponentLogger('OptimizedBridge');
22
-
23
- interface RpcRequest {
24
- id: number;
25
- protocol: string;
26
- method: 'call' | 'instantiate' | 'call_method' | 'dispose_instance';
27
- params: unknown;
28
- }
29
-
30
- interface RpcResponse<T = unknown> {
31
- id: number;
32
- protocol: string;
33
- result?: T;
34
- error?: { type: string; message: string; traceback?: string };
35
- }
36
-
37
- interface ProcessPoolOptions {
38
- minProcesses?: number;
39
- maxProcesses?: number;
40
- maxIdleTime?: number; // ms to keep idle processes alive
41
- maxRequestsPerProcess?: number; // restart process after N requests
42
- pythonPath?: string;
43
- scriptPath?: string;
44
- virtualEnv?: string | undefined;
45
- cwd?: string;
46
- timeoutMs?: number;
47
- enableJsonFallback?: boolean;
48
- env?: Record<string, string | undefined>;
49
- warmupCommands?: Array<{ method: string; params: unknown }>; // Commands to warm up processes
50
- }
51
-
52
- interface WorkerProcess {
53
- process: ChildProcess;
54
- id: string;
55
- requestCount: number;
56
- lastUsed: number;
57
- busy: boolean;
58
- buffer: string;
59
- pendingRequests: Map<
60
- number,
61
- {
62
- resolve: (value: unknown) => void;
63
- reject: (error: unknown) => void;
64
- timer?: NodeJS.Timeout;
65
- startTime: number;
66
- }
67
- >;
68
- stats: {
69
- totalRequests: number;
70
- totalTime: number;
71
- averageTime: number;
72
- errorCount: number;
73
- };
74
- }
75
-
76
- interface OptimizedBridgeStats {
77
- totalRequests: number;
78
- totalTime: number;
79
- cacheHits: number;
80
- poolHits: number;
81
- poolMisses: number;
82
- processSpawns: number;
83
- processDeaths: number;
84
- memoryPeak: number;
85
- averageTime: number;
86
- cacheHitRate: number;
87
- }
88
-
89
- interface OptimizedBridgeStatsSnapshot extends OptimizedBridgeStats {
90
- poolSize: number;
91
- busyWorkers: number;
92
- memoryUsage: NodeJS.MemoryUsage;
93
- workerStats: Array<{
94
- id: string;
95
- requestCount: number;
96
- averageTime: number;
97
- errorCount: number;
98
- busy: boolean;
99
- pendingRequests: number;
100
- }>;
101
- }
102
-
103
- function resolveDefaultScriptPath(): string {
104
- try {
105
- return fileURLToPath(new URL('../../runtime/python_bridge.py', import.meta.url));
106
- } catch {
107
- return 'runtime/python_bridge.py';
108
- }
109
- }
110
-
111
- function resolveVirtualEnv(
112
- virtualEnv: string,
113
- cwd: string
114
- ): {
115
- venvPath: string;
116
- binDir: string;
117
- pythonPath: string;
118
- } {
119
- const venvPath = resolve(cwd, virtualEnv);
120
- const binDir = join(venvPath, getVenvBinDir());
121
- const pythonPath = join(binDir, getVenvPythonExe());
122
- return { venvPath, binDir, pythonPath };
123
- }
124
-
125
- export class OptimizedNodeBridge extends RuntimeBridge {
126
- private processPool: WorkerProcess[] = [];
127
- private roundRobinIndex = 0;
128
- private nextId = 1;
129
- private cleanupTimer?: NodeJS.Timeout;
130
- private options: Required<ProcessPoolOptions>;
131
- private emitter = new EventEmitter();
132
- private disposed = false;
133
-
134
- // Performance monitoring
135
- private stats: OptimizedBridgeStats = {
136
- totalRequests: 0,
137
- totalTime: 0,
138
- cacheHits: 0,
139
- poolHits: 0,
140
- poolMisses: 0,
141
- processSpawns: 0,
142
- processDeaths: 0,
143
- memoryPeak: 0,
144
- averageTime: 0,
145
- cacheHitRate: 0,
146
- };
147
-
148
- constructor(options: ProcessPoolOptions = {}) {
149
- super();
150
- const cwd = options.cwd ?? process.cwd();
151
- const virtualEnv = options.virtualEnv ? resolve(cwd, options.virtualEnv) : '';
152
- const venv = virtualEnv ? resolveVirtualEnv(virtualEnv, cwd) : undefined;
153
- const scriptPath = options.scriptPath ?? resolveDefaultScriptPath();
154
- const resolvedScriptPath = isAbsolute(scriptPath) ? scriptPath : resolve(cwd, scriptPath);
155
- this.options = {
156
- minProcesses: options.minProcesses ?? 2,
157
- maxProcesses: options.maxProcesses ?? 8,
158
- maxIdleTime: options.maxIdleTime ?? 300000, // 5 minutes
159
- maxRequestsPerProcess: options.maxRequestsPerProcess ?? 1000,
160
- pythonPath: options.pythonPath ?? venv?.pythonPath ?? getDefaultPythonPath(),
161
- scriptPath: resolvedScriptPath,
162
- virtualEnv,
163
- cwd,
164
- timeoutMs: options.timeoutMs ?? 30000,
165
- enableJsonFallback: options.enableJsonFallback ?? false,
166
- env: options.env ?? {},
167
- warmupCommands: options.warmupCommands ?? [],
168
- };
169
-
170
- // Start with minimum processes
171
- this.startCleanupScheduler();
172
- }
173
-
174
- async init(): Promise<void> {
175
- if (this.disposed) {
176
- throw new Error('Bridge has been disposed');
177
- }
178
-
179
- // Ensure minimum processes are available
180
- while (this.processPool.length < this.options.minProcesses) {
181
- await this.spawnProcess();
182
- }
183
-
184
- // Warm up processes if configured
185
- if (this.options.warmupCommands.length > 0) {
186
- await this.warmupProcesses();
187
- }
188
- }
189
-
190
- async call<T = unknown>(
191
- module: string,
192
- functionName: string,
193
- args: unknown[],
194
- kwargs?: Record<string, unknown>
195
- ): Promise<T> {
196
- const startTime = performance.now();
197
-
198
- // Try cache first for pure functions
199
- const cacheKey = globalCache.generateKey('runtime_call', module, functionName, args, kwargs);
200
- const cached = await globalCache.get<T>(cacheKey);
201
- if (cached !== null) {
202
- this.stats.cacheHits++;
203
- // Runtime cache HIT for ${module}.${functionName}
204
- return cached;
205
- }
206
-
207
- try {
208
- const result = await this.executeRequest<T>({
209
- method: 'call',
210
- params: { module, functionName, args, kwargs },
211
- });
212
-
213
- const duration = performance.now() - startTime;
214
-
215
- // Cache result for pure functions (simple heuristic)
216
- if (this.isPureFunctionCandidate(functionName, args)) {
217
- await globalCache.set(cacheKey, result, {
218
- computeTime: duration,
219
- dependencies: [module],
220
- });
221
- }
222
-
223
- this.updateStats(duration);
224
- return result;
225
- } catch (error) {
226
- this.updateStats(performance.now() - startTime, true);
227
- throw error;
228
- }
229
- }
230
-
231
- async instantiate<T = unknown>(
232
- module: string,
233
- className: string,
234
- args: unknown[],
235
- kwargs?: Record<string, unknown>
236
- ): Promise<T> {
237
- const startTime = performance.now();
238
-
239
- try {
240
- const result = await this.executeRequest<T>({
241
- method: 'instantiate',
242
- params: { module, className, args, kwargs },
243
- });
244
-
245
- this.updateStats(performance.now() - startTime);
246
- return result;
247
- } catch (error) {
248
- this.updateStats(performance.now() - startTime, true);
249
- throw error;
250
- }
251
- }
252
-
253
- async callMethod<T = unknown>(
254
- handle: string,
255
- methodName: string,
256
- args: unknown[],
257
- kwargs?: Record<string, unknown>
258
- ): Promise<T> {
259
- const startTime = performance.now();
260
-
261
- try {
262
- const result = await this.executeRequest<T>({
263
- method: 'call_method',
264
- params: { handle, methodName, args, kwargs },
265
- });
266
-
267
- this.updateStats(performance.now() - startTime);
268
- return result;
269
- } catch (error) {
270
- this.updateStats(performance.now() - startTime, true);
271
- throw error;
272
- }
273
- }
274
-
275
- async disposeInstance(handle: string): Promise<void> {
276
- await this.executeRequest<void>({
277
- method: 'dispose_instance',
278
- params: { handle },
279
- });
280
- }
281
-
282
- /**
283
- * Execute request with intelligent process selection
284
- */
285
- private async executeRequest<T>(payload: Omit<RpcRequest, 'id' | 'protocol'>): Promise<T> {
286
- let worker = this.selectOptimalWorker();
287
-
288
- // Spawn new process if none available and under limit
289
- if (!worker && this.processPool.length < this.options.maxProcesses) {
290
- try {
291
- worker = await this.spawnProcess();
292
- this.stats.poolMisses++;
293
- } catch (error) {
294
- throw new Error(`Failed to spawn worker process: ${error}`);
295
- }
296
- }
297
-
298
- // Wait for worker if all are busy
299
- worker ??= await this.waitForAvailableWorker();
300
-
301
- this.stats.poolHits++;
302
- return this.sendToWorker<T>(worker, payload);
303
- }
304
-
305
- /**
306
- * Select optimal worker based on load and performance
307
- */
308
- private selectOptimalWorker(): WorkerProcess | null {
309
- const availableWorkers = this.processPool.filter(w => !w.busy && w.process.connected);
310
-
311
- if (availableWorkers.length === 0) {
312
- return null;
313
- }
314
-
315
- // Simple round-robin for now, could be enhanced with load-based selection
316
- const worker = availableWorkers[this.roundRobinIndex % availableWorkers.length];
317
- this.roundRobinIndex = (this.roundRobinIndex + 1) % availableWorkers.length;
318
-
319
- return worker ?? null;
320
- }
321
-
322
- /**
323
- * Wait for any worker to become available
324
- */
325
- private async waitForAvailableWorker(timeoutMs: number = 5000): Promise<WorkerProcess> {
326
- return new Promise((resolvePromise, reject) => {
327
- const timeout = setTimeout(() => {
328
- reject(new Error('Timeout waiting for available worker'));
329
- }, timeoutMs);
330
-
331
- const checkWorker = (): void => {
332
- const worker = this.selectOptimalWorker();
333
- if (worker) {
334
- clearTimeout(timeout);
335
- resolvePromise(worker);
336
- } else {
337
- // Check again in 10ms
338
- setTimeout(checkWorker, 10);
339
- }
340
- };
341
-
342
- checkWorker();
343
- });
344
- }
345
-
346
- /**
347
- * Send request to specific worker
348
- */
349
- private async sendToWorker<T>(
350
- worker: WorkerProcess,
351
- payload: Omit<RpcRequest, 'id' | 'protocol'>
352
- ): Promise<T> {
353
- const id = this.nextId++;
354
- const message: RpcRequest = { id, protocol: TYWRAP_PROTOCOL, ...payload } as RpcRequest;
355
- const text = `${JSON.stringify(message)}\n`;
356
-
357
- worker.busy = true;
358
- worker.requestCount++;
359
- worker.lastUsed = Date.now();
360
-
361
- return new Promise<T>((resolvePromise, reject) => {
362
- const startTime = performance.now();
363
-
364
- const timer = setTimeout(() => {
365
- worker.pendingRequests.delete(id);
366
- worker.busy = false;
367
- reject(new Error(`Request ${id} timed out after ${this.options.timeoutMs}ms`));
368
- }, this.options.timeoutMs);
369
-
370
- worker.pendingRequests.set(id, {
371
- resolve: (value: unknown) => {
372
- const duration = performance.now() - startTime;
373
- worker.stats.totalTime += duration;
374
- worker.stats.totalRequests++;
375
- worker.stats.averageTime = worker.stats.totalTime / worker.stats.totalRequests;
376
- worker.busy = false;
377
- resolvePromise(value as T);
378
- },
379
- reject: (error: unknown) => {
380
- worker.stats.errorCount++;
381
- worker.busy = false;
382
- reject(error);
383
- },
384
- timer,
385
- startTime,
386
- });
387
-
388
- if (!worker.process.stdin?.writable) {
389
- worker.pendingRequests.delete(id);
390
- worker.busy = false;
391
- clearTimeout(timer);
392
- reject(new Error('Worker process stdin not writable'));
393
- return;
394
- }
395
-
396
- try {
397
- worker.process.stdin.write(text);
398
- } catch (error) {
399
- worker.pendingRequests.delete(id);
400
- worker.busy = false;
401
- clearTimeout(timer);
402
- reject(error);
403
- }
404
- });
405
- }
406
-
407
- /**
408
- * Spawn new worker process with optimizations
409
- */
410
- private async spawnProcess(): Promise<WorkerProcess> {
411
- const { spawn } = await import('child_process');
412
-
413
- const workerId = `worker_${Date.now()}_${Math.random().toString(36).substr(2, 9)}`;
414
-
415
- const env: NodeJS.ProcessEnv = {
416
- ...process.env,
417
- ...this.options.env,
418
- PYTHONUNBUFFERED: '1', // Ensure immediate output
419
- PYTHONDONTWRITEBYTECODE: '1', // Skip .pyc files for faster startup
420
- };
421
- if (!env.PYTHONUTF8) {
422
- env.PYTHONUTF8 = '1';
423
- }
424
- if (!env.PYTHONIOENCODING) {
425
- env.PYTHONIOENCODING = 'UTF-8';
426
- }
427
- if (this.options.virtualEnv) {
428
- const venv = resolveVirtualEnv(this.options.virtualEnv, this.options.cwd);
429
- env.VIRTUAL_ENV = venv.venvPath;
430
- env.PATH = `${venv.binDir}${delimiter}${env.PATH ?? ''}`;
431
- }
432
-
433
- if (this.options.enableJsonFallback && !env.TYWRAP_CODEC_FALLBACK) {
434
- env.TYWRAP_CODEC_FALLBACK = 'json';
435
- }
436
-
437
- const childProcess = spawn(this.options.pythonPath, [this.options.scriptPath], {
438
- cwd: this.options.cwd,
439
- stdio: ['pipe', 'pipe', 'pipe'],
440
- env,
441
- });
442
-
443
- const worker: WorkerProcess = {
444
- process: childProcess,
445
- id: workerId,
446
- requestCount: 0,
447
- lastUsed: Date.now(),
448
- busy: false,
449
- buffer: '',
450
- pendingRequests: new Map(),
451
- stats: {
452
- totalRequests: 0,
453
- totalTime: 0,
454
- averageTime: 0,
455
- errorCount: 0,
456
- },
457
- };
458
-
459
- // Setup process event handlers
460
- this.setupProcessHandlers(worker);
461
-
462
- this.processPool.push(worker);
463
- this.stats.processSpawns++;
464
-
465
- // Spawned Python worker process ${workerId} (pool size: ${this.processPool.length})
466
-
467
- return worker;
468
- }
469
-
470
- /**
471
- * Setup event handlers for worker process
472
- */
473
- private setupProcessHandlers(worker: WorkerProcess): void {
474
- const childProcess = worker.process;
475
-
476
- // Handle stdout data with efficient buffering
477
- childProcess.stdout?.on('data', (chunk: Buffer) => {
478
- worker.buffer += chunk.toString();
479
-
480
- let idx: number;
481
- while ((idx = worker.buffer.indexOf('\n')) !== -1) {
482
- const line = worker.buffer.slice(0, idx).trim();
483
- worker.buffer = worker.buffer.slice(idx + 1);
484
-
485
- if (!line) {
486
- continue;
487
- }
488
-
489
- this.handleWorkerResponse(worker, line).catch(error => {
490
- log.error('Error handling worker response', {
491
- workerId: worker.id,
492
- error: String(error),
493
- });
494
- });
495
- }
496
- });
497
-
498
- // Handle stderr for debugging
499
- childProcess.stderr?.on('data', (chunk: Buffer) => {
500
- const errorText = chunk.toString().trim();
501
- if (errorText) {
502
- log.warn('Worker stderr', { workerId: worker.id, output: errorText });
503
- }
504
- });
505
-
506
- // Handle process exit
507
- childProcess.on('exit', code => {
508
- log.warn('Worker exited', { workerId: worker.id, code });
509
- this.handleWorkerExit(worker, code);
510
- });
511
-
512
- // Handle process errors
513
- childProcess.on('error', error => {
514
- log.error('Worker error', { workerId: worker.id, error: String(error) });
515
- this.handleWorkerExit(worker, -1);
516
- });
517
- }
518
-
519
- /**
520
- * Handle response from worker process
521
- */
522
- private async handleWorkerResponse(worker: WorkerProcess, line: string): Promise<void> {
523
- try {
524
- const msg = JSON.parse(line) as RpcResponse;
525
- if (msg.protocol !== TYWRAP_PROTOCOL) {
526
- this.handleProtocolError(
527
- worker,
528
- line,
529
- new BridgeProtocolError(
530
- `Invalid protocol. Expected ${TYWRAP_PROTOCOL} but received ${String(msg.protocol)}`
531
- )
532
- );
533
- return;
534
- }
535
- if (typeof msg.id !== 'number') {
536
- this.handleProtocolError(worker, line, new BridgeProtocolError('Invalid response id'));
537
- return;
538
- }
539
- const pending = worker.pendingRequests.get(msg.id);
540
-
541
- if (!pending) {
542
- this.handleProtocolError(
543
- worker,
544
- line,
545
- new BridgeProtocolError(`Unexpected response id ${msg.id}`)
546
- );
547
- return;
548
- }
549
-
550
- worker.pendingRequests.delete(msg.id);
551
-
552
- if (pending.timer) {
553
- clearTimeout(pending.timer);
554
- }
555
-
556
- if (msg.error) {
557
- pending.reject(this.errorFrom(msg.error));
558
- } else {
559
- try {
560
- const decoded = await decodeValueAsync(msg.result);
561
- pending.resolve(decoded);
562
- } catch (decodeError) {
563
- pending.reject(new BridgeProtocolError(`Failed to decode response: ${decodeError}`));
564
- }
565
- }
566
- } catch (parseError) {
567
- this.handleProtocolError(worker, line, parseError);
568
- }
569
- }
570
-
571
- /**
572
- * Handle worker process exit
573
- */
574
- private handleWorkerExit(worker: WorkerProcess, code: number | null): void {
575
- // Reject all pending requests
576
- for (const [, pending] of worker.pendingRequests) {
577
- if (pending.timer) {
578
- clearTimeout(pending.timer);
579
- }
580
- pending.reject(new Error(`Worker process exited with code ${code}`));
581
- }
582
-
583
- worker.pendingRequests.clear();
584
-
585
- // Remove from pool
586
- const index = this.processPool.indexOf(worker);
587
- if (index >= 0) {
588
- this.processPool.splice(index, 1);
589
- this.stats.processDeaths++;
590
- }
591
-
592
- // Spawn replacement if needed and not disposing
593
- if (!this.disposed && this.processPool.length < this.options.minProcesses) {
594
- this.spawnProcess().catch(error => {
595
- log.error('Failed to spawn replacement worker', { error: String(error) });
596
- });
597
- }
598
- }
599
-
600
- /**
601
- * Warm up processes with configured commands
602
- */
603
- private async warmupProcesses(): Promise<void> {
604
- const warmupPromises = this.processPool.map(async worker => {
605
- for (const cmd of this.options.warmupCommands) {
606
- try {
607
- await this.sendToWorker(worker, {
608
- method: cmd.method as 'call' | 'instantiate' | 'call_method' | 'dispose_instance',
609
- params: cmd.params,
610
- });
611
- } catch (error) {
612
- log.warn('Warmup command failed', { workerId: worker.id, error: String(error) });
613
- }
614
- }
615
- });
616
-
617
- await Promise.all(warmupPromises);
618
- // Warmed up ${this.processPool.length} worker processes
619
- }
620
-
621
- /**
622
- * Heuristic to determine if function result should be cached
623
- */
624
- private isPureFunctionCandidate(functionName: string, args: unknown[]): boolean {
625
- // Simple heuristics - could be made more sophisticated
626
- const pureFunctionPatterns = [
627
- /^(get|fetch|read|load|find|search|query|select)_/i,
628
- /^(compute|calculate|process|transform|convert)_/i,
629
- /^(encode|decode|serialize|deserialize)_/i,
630
- ];
631
-
632
- const impureFunctionPatterns = [
633
- /^(set|save|write|update|insert|delete|create|modify)_/i,
634
- /^(send|post|put|patch)_/i,
635
- /random|uuid|timestamp|now|current/i,
636
- ];
637
-
638
- // Don't cache if function name suggests mutation
639
- if (impureFunctionPatterns.some(pattern => pattern.test(functionName))) {
640
- return false;
641
- }
642
-
643
- // Cache if function name suggests pure computation
644
- if (pureFunctionPatterns.some(pattern => pattern.test(functionName))) {
645
- return true;
646
- }
647
-
648
- // Don't cache if args contain mutable objects (very basic check)
649
- const hasComplexArgs = args.some(
650
- arg => arg !== null && typeof arg === 'object' && !(arg instanceof Date)
651
- );
652
-
653
- return !hasComplexArgs && args.length <= 3; // Cache simple calls with few args
654
- }
655
-
656
- /**
657
- * Update performance statistics
658
- */
659
- private updateStats(duration: number, _error: boolean = false): void {
660
- this.stats.totalRequests++;
661
- this.stats.totalTime += duration;
662
-
663
- const currentMemory = process.memoryUsage().heapUsed;
664
- if (currentMemory > this.stats.memoryPeak) {
665
- this.stats.memoryPeak = currentMemory;
666
- }
667
- }
668
-
669
- /**
670
- * Get performance statistics
671
- */
672
- getStats(): OptimizedBridgeStatsSnapshot {
673
- const avgTime =
674
- this.stats.totalRequests > 0 ? this.stats.totalTime / this.stats.totalRequests : 0;
675
- const hitRate =
676
- this.stats.totalRequests > 0 ? this.stats.cacheHits / this.stats.totalRequests : 0;
677
-
678
- return {
679
- ...this.stats,
680
- averageTime: avgTime,
681
- cacheHitRate: hitRate,
682
- poolSize: this.processPool.length,
683
- busyWorkers: this.processPool.filter(w => w.busy).length,
684
- memoryUsage: process.memoryUsage(),
685
- workerStats: this.processPool.map(w => ({
686
- id: w.id,
687
- requestCount: w.requestCount,
688
- averageTime: w.stats.averageTime,
689
- errorCount: w.stats.errorCount,
690
- busy: w.busy,
691
- pendingRequests: w.pendingRequests.size,
692
- })),
693
- };
694
- }
695
-
696
- /**
697
- * Cleanup idle processes
698
- */
699
- private async cleanup(): Promise<void> {
700
- const now = Date.now();
701
- const idleWorkers = this.processPool.filter(
702
- w =>
703
- !w.busy &&
704
- now - w.lastUsed > this.options.maxIdleTime &&
705
- this.processPool.length > this.options.minProcesses
706
- );
707
-
708
- for (const worker of idleWorkers) {
709
- await this.terminateWorker(worker);
710
- }
711
-
712
- // Restart workers that have handled too many requests
713
- const overusedWorkers = this.processPool.filter(
714
- w => !w.busy && w.requestCount >= this.options.maxRequestsPerProcess
715
- );
716
-
717
- for (const worker of overusedWorkers) {
718
- await this.terminateWorker(worker);
719
- if (this.processPool.length < this.options.minProcesses) {
720
- await this.spawnProcess();
721
- }
722
- }
723
- }
724
-
725
- /**
726
- * Gracefully terminate a worker
727
- */
728
- private async terminateWorker(worker: WorkerProcess): Promise<void> {
729
- const index = this.processPool.indexOf(worker);
730
- if (index >= 0) {
731
- this.processPool.splice(index, 1);
732
- }
733
-
734
- // Reject pending requests
735
- for (const [, pending] of worker.pendingRequests) {
736
- if (pending.timer) {
737
- clearTimeout(pending.timer);
738
- }
739
- pending.reject(new Error('Worker process terminated'));
740
- }
741
-
742
- // Graceful termination
743
- try {
744
- if (worker.process.connected) {
745
- worker.process.kill('SIGTERM');
746
-
747
- // Force kill if not terminated in 5 seconds
748
- setTimeout(() => {
749
- if (!worker.process.killed) {
750
- worker.process.kill('SIGKILL');
751
- }
752
- }, 5000);
753
- }
754
- } catch (error) {
755
- log.warn('Error terminating worker', { workerId: worker.id, error: String(error) });
756
- }
757
-
758
- // Terminated worker ${worker.id}
759
- }
760
-
761
- /**
762
- * Start cleanup scheduler
763
- */
764
- private startCleanupScheduler(): void {
765
- this.cleanupTimer = setInterval(async () => {
766
- try {
767
- await this.cleanup();
768
- } catch (error) {
769
- log.error('Cleanup error', { error: String(error) });
770
- }
771
- }, 60000); // Cleanup every minute
772
- }
773
-
774
- /**
775
- * Dispose all resources
776
- */
777
- async dispose(): Promise<void> {
778
- if (this.disposed) {
779
- return;
780
- }
781
-
782
- this.disposed = true;
783
-
784
- if (this.cleanupTimer) {
785
- clearInterval(this.cleanupTimer);
786
- this.cleanupTimer = undefined;
787
- }
788
-
789
- // Terminate all workers
790
- const terminationPromises = this.processPool.map(worker => this.terminateWorker(worker));
791
- await Promise.all(terminationPromises);
792
-
793
- this.processPool.length = 0;
794
- this.emitter.removeAllListeners();
795
-
796
- // Disposed optimized Node.js bridge
797
- }
798
-
799
- private errorFrom(err: { type: string; message: string; traceback?: string }): Error {
800
- const e = new BridgeExecutionError(`${err.type}: ${err.message}`);
801
- e.traceback = err.traceback;
802
- return e;
803
- }
804
-
805
- private handleProtocolError(worker: WorkerProcess, line: string, error: unknown): void {
806
- const snippet = line.length > 500 ? `${line.slice(0, 500)}…` : line;
807
- const detail = error instanceof Error ? error.message : String(error);
808
- const hint =
809
- 'Ensure your Python code does not print to stdout and that the bridge outputs only JSON lines.';
810
- const msg = `Protocol error from Python bridge. ${detail}\n${hint}\nOffending line: ${snippet}`;
811
- const bridgeError = new BridgeProtocolError(msg);
812
-
813
- for (const [, pending] of worker.pendingRequests) {
814
- if (pending.timer) {
815
- clearTimeout(pending.timer);
816
- }
817
- pending.reject(bridgeError);
818
- }
819
- worker.pendingRequests.clear();
820
-
821
- try {
822
- if (worker.process.connected) {
823
- worker.process.kill('SIGTERM');
824
- }
825
- } catch (killError) {
826
- log.warn('Error terminating worker after protocol error', {
827
- workerId: worker.id,
828
- error: String(killError),
829
- });
830
- }
831
-
832
- const index = this.processPool.indexOf(worker);
833
- if (index >= 0) {
834
- this.processPool.splice(index, 1);
835
- this.stats.processDeaths++;
836
- }
837
-
838
- if (!this.disposed && this.processPool.length < this.options.minProcesses) {
839
- this.spawnProcess().catch(spawnError => {
840
- log.error('Failed to spawn replacement worker after protocol error', {
841
- error: String(spawnError),
842
- });
843
- });
844
- }
845
- }
846
- }
21
+ export { NodeBridge as OptimizedNodeBridge, type NodeBridgeOptions as ProcessPoolOptions } from './node.js';