Skip to content

Soul-driven AI agent with permission-hardened tools, token budgets, and multi-channel access. Runs 24/7 from CLI, Telegram or More.

2.8 KB
Raw Blame
import { cpus, totalmem, freemem } from 'node:os';
import { logger } from '../utils/logger.js';
import type { ResourceUsage } from '../types/agent.js';

const MB = 1024 * 1024;
const MIN_FREE_RAM_MB = 1024;
/**
 * Sub-agents run inside the main process, so every concurrent agent multiplies
 * heap pressure on the SAME V8 isolate (the per-agent RAM figure below is a
 * scheduling heuristic, not real isolation). A broad analysis task spawning
 * several concurrent sub-agents was a direct path to a shared-heap OOM.
 */
export const MAX_CONCURRENT_SUB_AGENTS = 3;

export class ResourceManager {
  private maxConcurrent: number;
  private userOverride: number | null = null;

  constructor() {
    this.maxConcurrent = this.calculateMaxConcurrent();
    logger.info(
      { cpuCores: cpus().length, maxConcurrent: this.maxConcurrent, totalRAM: `${Math.round(totalmem() / MB)}MB` },
      'ResourceManager initialized',
    );
  }

  private calculateMaxConcurrent(): number {
    const cpuCount = cpus().length;
    const availableMB = freemem() / MB;
    const totalMB = totalmem() / MB;

    // Shared-heap reality check first: concurrent sub-agents all allocate on
    // the main process's V8 heap, so no RAM-based formula can justify more
    // than a small pool.
    const cpuBasedMax = Math.max(1, cpuCount - 1);
    const ramBasedMax = Math.max(1, Math.floor((availableMB - MIN_FREE_RAM_MB) / 512));
    const systemMax = Math.max(1, Math.floor((totalMB / 2) / 512));

    let max = Math.min(cpuBasedMax, ramBasedMax, systemMax, MAX_CONCURRENT_SUB_AGENTS);

    if (max < 1) max = 1;
    if (availableMB < MIN_FREE_RAM_MB * 2) max = 1;

    return max;
  }

  getMaxConcurrent(): number {
    return this.userOverride ?? this.maxConcurrent;
  }

  setMaxConcurrent(n: number): void {
    this.userOverride = Math.max(1, n);
    logger.info({ max: this.userOverride }, 'User override set for max concurrent sub-agents');
  }

  clearOverride(): void {
    this.userOverride = null;
    logger.info('User override cleared, using auto-detected max concurrent');
  }

  canSpawn(): boolean {
    this.maxConcurrent = this.calculateMaxConcurrent();
    const effective = this.userOverride ?? this.maxConcurrent;
    return effective > 0;
  }

  getResourceUsage(activeAgents: number, queuedAgents: number, tokenBudgetRemaining: number): ResourceUsage {
    this.maxConcurrent = this.calculateMaxConcurrent();
    return {
      cpuCores: cpus().length,
      maxConcurrentAgents: this.userOverride ?? this.maxConcurrent,
      activeAgents,
      queuedAgents,
      systemMemoryMB: Math.round(totalmem() / MB),
      availableMemoryMB: Math.round(freemem() / MB),
      tokenBudgetRemaining,
    };
  }

  refresh(): void {
    this.maxConcurrent = this.calculateMaxConcurrent();
    logger.debug({ maxConcurrent: this.maxConcurrent }, 'Resource limits refreshed');
  }
}