( workerInfo: WorkerInfo | undefined, msg: Record<string, unknown> )
| 617 | } |
| 618 | |
| 619 | function handleBrokerMessage( |
| 620 | workerInfo: WorkerInfo | undefined, |
| 621 | msg: Record<string, unknown> |
| 622 | ): void { |
| 623 | if (!workerInfo) return |
| 624 | const brokerId = msg.brokerId as number |
| 625 | const executionId = msg.executionId as number |
| 626 | const brokerName = msg.brokerName as string |
| 627 | const argsJson = msg.argsJson as string | undefined |
| 628 | |
| 629 | const sendResponse = (payload: Record<string, unknown>) => { |
| 630 | try { |
| 631 | workerInfo.process.send({ type: 'brokerResponse', brokerId, ...payload }) |
| 632 | } catch (err) { |
| 633 | logger.error('Failed to send broker response to worker', { |
| 634 | err, |
| 635 | brokerId, |
| 636 | brokerName, |
| 637 | workerId: workerInfo.id, |
| 638 | }) |
| 639 | } |
| 640 | } |
| 641 | |
| 642 | const logReject = (reason: string, extra?: Record<string, unknown>) => { |
| 643 | logger.warn('Sandbox broker call rejected', { |
| 644 | reason, |
| 645 | brokerName, |
| 646 | executionId, |
| 647 | workerId: workerInfo.id, |
| 648 | ...extra, |
| 649 | }) |
| 650 | } |
| 651 | |
| 652 | const pending = workerInfo.pendingExecutions.get(executionId) |
| 653 | if (!pending) { |
| 654 | sendResponse({ error: 'Execution no longer active' }) |
| 655 | return |
| 656 | } |
| 657 | |
| 658 | if (pending.cancelled) { |
| 659 | sendResponse({ error: 'Execution cancelled' }) |
| 660 | return |
| 661 | } |
| 662 | |
| 663 | if (argsJson && argsJson.length > MAX_BROKER_ARGS_JSON_CHARS) { |
| 664 | logReject('args_too_large', { argsJsonLength: argsJson.length }) |
| 665 | sendResponse({ |
| 666 | error: `Broker args exceed maximum size (${MAX_BROKER_ARGS_JSON_CHARS} chars)`, |
| 667 | }) |
| 668 | return |
| 669 | } |
| 670 | |
| 671 | pending.brokerCallCount++ |
| 672 | if (pending.brokerCallCount > MAX_BROKERS_PER_EXECUTION) { |
| 673 | logReject('rate_limit', { brokerCallCount: pending.brokerCallCount }) |
| 674 | sendResponse({ |
| 675 | error: `Broker call limit exceeded (${MAX_BROKERS_PER_EXECUTION} per execution)`, |
| 676 | }) |
no test coverage detected