// Home23 — PM2 Process Configuration (auto-generated)
// Do not edit manually — regenerated by: node cli/home23.js agent create
//
// Agents: jerry, forrest

const fs = require('fs');
const path = require('path');
const yaml = require('js-yaml');

const HOME23 = __dirname;
const ENGINE = path.join(HOME23, 'engine');
const ENGINE_KILL_TIMEOUT_MS = 210000;
const PM2_INHERITANCE_BLOCKLIST = ['cron_restart', 'watch', 'HOME23_AGENT', 'INSTANCE_ID', 'DASHBOARD_PORT', 'COSMO_DASHBOARD_PORT', 'REALTIME_PORT', 'MCP_HTTP_PORT', 'COSMO_RUNTIME_DIR', 'COSMO_WORKSPACE_PATH'];
for (const key of PM2_INHERITANCE_BLOCKLIST) delete process.env[key];

function loadYaml(filePath) {
  if (!fs.existsSync(filePath)) return {};
  return yaml.load(fs.readFileSync(filePath, 'utf8')) || {};
}

const homeConfig = loadYaml(path.join(HOME23, 'config', 'home.yaml'));
const secrets = loadYaml(path.join(HOME23, 'config', 'secrets.yaml'));
const ollamaLocalUrl = homeConfig.providers?.['ollama-local']?.baseUrl || 'http://127.0.0.1:11434';
const screenlogicConfig = homeConfig.screenlogic || {};
const screenlogicVenvPython = path.join(HOME23, 'runtime', 'screenlogic-venv', 'bin', 'python');
const screenlogicPython = screenlogicConfig.python || (fs.existsSync(screenlogicVenvPython) ? screenlogicVenvPython : 'python3');

// Cosmo23 OAuth encryption key — read from secrets.yaml (init generates it)
const cosmo23EncryptionKey = secrets.cosmo23?.encryptionKey || '';
if (!cosmo23EncryptionKey) {
  console.warn('[ecosystem] Warning: cosmo23 encryption key not found in secrets.yaml. Run "home23 init" to generate.');
}
const cosmo23DbUrl = 'file:' + path.join(HOME23, 'cosmo23', '.cosmo23-config', 'database.db');

const commonEnv = {
  NODE_ENV: 'production',
  COSMO_CONFIG_PATH: path.join(HOME23, 'configs', 'base-engine.yaml'),
  EMBEDDING_BASE_URL: `${ollamaLocalUrl}/v1`,
  LOCAL_LLM_BASE_URL: `${ollamaLocalUrl}/v1`,
  OLLAMA_CLOUD_API_KEY: secrets.providers?.['ollama-cloud']?.apiKey || '',
  MINIMAX_API_KEY: secrets.providers?.minimax?.apiKey || '',
  ANTHROPIC_AUTH_TOKEN: secrets.providers?.anthropic?.apiKey || '',
  OPENAI_CODEX_AUTH_TOKEN: secrets.providers?.['openai-codex']?.apiKey || '',
  OPENAI_API_KEY: secrets.providers?.openai?.apiKey || '',
  XAI_API_KEY: secrets.providers?.xai?.apiKey || '',
};

module.exports = {
  apps: [

    // ── jerry ──
    {
      name: 'home23-jerry',
      script: 'src/index.js',
      cwd: ENGINE,
      filter_env: ['HOME23_AGENT', 'INSTANCE_ID', 'DASHBOARD_PORT', 'COSMO_DASHBOARD_PORT', 'REALTIME_PORT', 'MCP_HTTP_PORT', 'COSMO_RUNTIME_DIR', 'COSMO_WORKSPACE_PATH'],
      node_args: '--expose-gc --max-old-space-size=4096',
      max_memory_restart: '5G',
      kill_timeout: ENGINE_KILL_TIMEOUT_MS,
      autorestart: true, watch: false, merge_logs: true,
      out_file: path.join(HOME23, 'instances', 'jerry', 'logs') + '/engine-out.log',
      error_file: path.join(HOME23, 'instances', 'jerry', 'logs') + '/engine-err.log',
      env: { ...commonEnv, HOME23_AGENT: 'jerry', COSMO_RUNTIME_DIR: path.join(HOME23, 'instances', 'jerry', 'brain'), COSMO_WORKSPACE_PATH: path.join(HOME23, 'instances', 'jerry', 'workspace'), DASHBOARD_PORT: '5002', COSMO_DASHBOARD_PORT: '5002', REALTIME_PORT: '5001', MCP_HTTP_PORT: '5003', INSTANCE_ID: 'home23-jerry' },
    },
    {
      name: 'home23-jerry-dash',
      script: 'src/dashboard/server.js',
      cwd: ENGINE,
      filter_env: ['HOME23_AGENT', 'INSTANCE_ID', 'DASHBOARD_PORT', 'COSMO_DASHBOARD_PORT', 'REALTIME_PORT', 'MCP_HTTP_PORT', 'COSMO_RUNTIME_DIR', 'COSMO_WORKSPACE_PATH'],
      node_args: '--max-old-space-size=2048',
      max_memory_restart: '3G',
      autorestart: true, watch: false, merge_logs: true,
      out_file: path.join(HOME23, 'instances', 'jerry', 'logs') + '/dashboard-out.log',
      error_file: path.join(HOME23, 'instances', 'jerry', 'logs') + '/dashboard-err.log',
      env: { ...commonEnv, HOME23_AGENT: 'jerry', COSMO_RUNTIME_DIR: path.join(HOME23, 'instances', 'jerry', 'brain'), COSMO_WORKSPACE_PATH: path.join(HOME23, 'instances', 'jerry', 'workspace'), DASHBOARD_PORT: '5002', COSMO_DASHBOARD_PORT: '5002', REALTIME_PORT: '5001', MCP_HTTP_PORT: '5003', INSTANCE_ID: 'home23-jerry' },
    },
    {
      name: 'home23-jerry-harness',
      script: 'dist/home.js',
      cwd: HOME23,
      filter_env: ['cron_restart', 'HOME23_AGENT', 'INSTANCE_ID', 'DASHBOARD_PORT', 'COSMO_DASHBOARD_PORT', 'REALTIME_PORT', 'MCP_HTTP_PORT', 'COSMO_RUNTIME_DIR', 'COSMO_WORKSPACE_PATH'],
      node_args: '--expose-gc --max-old-space-size=4096 --cpu-prof --cpu-prof-dir=' + path.join(HOME23, 'instances', 'jerry', 'logs') + ' --cpu-prof-interval=1000 --heap-prof --heap-prof-dir=' + path.join(HOME23, 'instances', 'jerry', 'logs'),
      max_memory_restart: '5G',
      autorestart: true, watch: false, merge_logs: true,
      out_file: path.join(HOME23, 'instances', 'jerry', 'logs') + '/harness-out.log',
      error_file: path.join(HOME23, 'instances', 'jerry', 'logs') + '/harness-err.log',
      env: { HOME23_AGENT: 'jerry', OLLAMA_CLOUD_API_KEY: commonEnv.OLLAMA_CLOUD_API_KEY, MINIMAX_API_KEY: commonEnv.MINIMAX_API_KEY, ANTHROPIC_AUTH_TOKEN: commonEnv.ANTHROPIC_AUTH_TOKEN, OPENAI_CODEX_AUTH_TOKEN: commonEnv.OPENAI_CODEX_AUTH_TOKEN, OPENAI_API_KEY: commonEnv.OPENAI_API_KEY, XAI_API_KEY: commonEnv.XAI_API_KEY },
    },

    // ── forrest ──
    {
      name: 'home23-forrest',
      script: 'src/index.js',
      cwd: ENGINE,
      filter_env: ['HOME23_AGENT', 'INSTANCE_ID', 'DASHBOARD_PORT', 'COSMO_DASHBOARD_PORT', 'REALTIME_PORT', 'MCP_HTTP_PORT', 'COSMO_RUNTIME_DIR', 'COSMO_WORKSPACE_PATH'],
      node_args: '--expose-gc --max-old-space-size=4096',
      max_memory_restart: '5G',
      kill_timeout: ENGINE_KILL_TIMEOUT_MS,
      autorestart: true, watch: false, merge_logs: true,
      out_file: path.join(HOME23, 'instances', 'forrest', 'logs') + '/engine-out.log',
      error_file: path.join(HOME23, 'instances', 'forrest', 'logs') + '/engine-err.log',
      env: { ...commonEnv, HOME23_AGENT: 'forrest', COSMO_RUNTIME_DIR: path.join(HOME23, 'instances', 'forrest', 'brain'), COSMO_WORKSPACE_PATH: path.join(HOME23, 'instances', 'forrest', 'workspace'), DASHBOARD_PORT: '5012', COSMO_DASHBOARD_PORT: '5012', REALTIME_PORT: '5011', MCP_HTTP_PORT: '5013', INSTANCE_ID: 'home23-forrest' },
    },
    {
      name: 'home23-forrest-dash',
      script: 'src/dashboard/server.js',
      cwd: ENGINE,
      filter_env: ['HOME23_AGENT', 'INSTANCE_ID', 'DASHBOARD_PORT', 'COSMO_DASHBOARD_PORT', 'REALTIME_PORT', 'MCP_HTTP_PORT', 'COSMO_RUNTIME_DIR', 'COSMO_WORKSPACE_PATH'],
      node_args: '--max-old-space-size=2048',
      max_memory_restart: '3G',
      autorestart: true, watch: false, merge_logs: true,
      out_file: path.join(HOME23, 'instances', 'forrest', 'logs') + '/dashboard-out.log',
      error_file: path.join(HOME23, 'instances', 'forrest', 'logs') + '/dashboard-err.log',
      env: { ...commonEnv, HOME23_AGENT: 'forrest', COSMO_RUNTIME_DIR: path.join(HOME23, 'instances', 'forrest', 'brain'), COSMO_WORKSPACE_PATH: path.join(HOME23, 'instances', 'forrest', 'workspace'), DASHBOARD_PORT: '5012', COSMO_DASHBOARD_PORT: '5012', REALTIME_PORT: '5011', MCP_HTTP_PORT: '5013', INSTANCE_ID: 'home23-forrest' },
    },
    {
      name: 'home23-forrest-harness',
      script: 'dist/home.js',
      cwd: HOME23,
      filter_env: ['cron_restart', 'HOME23_AGENT', 'INSTANCE_ID', 'DASHBOARD_PORT', 'COSMO_DASHBOARD_PORT', 'REALTIME_PORT', 'MCP_HTTP_PORT', 'COSMO_RUNTIME_DIR', 'COSMO_WORKSPACE_PATH'],
      node_args: '--expose-gc --max-old-space-size=4096 --cpu-prof --cpu-prof-dir=' + path.join(HOME23, 'instances', 'forrest', 'logs') + ' --cpu-prof-interval=1000 --heap-prof --heap-prof-dir=' + path.join(HOME23, 'instances', 'forrest', 'logs'),
      max_memory_restart: '5G',
      autorestart: true, watch: false, merge_logs: true,
      out_file: path.join(HOME23, 'instances', 'forrest', 'logs') + '/harness-out.log',
      error_file: path.join(HOME23, 'instances', 'forrest', 'logs') + '/harness-err.log',
      env: { HOME23_AGENT: 'forrest', OLLAMA_CLOUD_API_KEY: commonEnv.OLLAMA_CLOUD_API_KEY, MINIMAX_API_KEY: commonEnv.MINIMAX_API_KEY, ANTHROPIC_AUTH_TOKEN: commonEnv.ANTHROPIC_AUTH_TOKEN, OPENAI_CODEX_AUTH_TOKEN: commonEnv.OPENAI_CODEX_AUTH_TOKEN, OPENAI_API_KEY: commonEnv.OPENAI_API_KEY, XAI_API_KEY: commonEnv.XAI_API_KEY },
    },

    // ── pm2 watchdog (shared) ──
    {
      name: 'home23-watchdog',
      script: path.join(HOME23, 'scripts', 'home23-pm2-watchdog-daemon.cjs'),
      cwd: HOME23,
      filter_env: ['cron_restart', 'watch', 'HOME23_AGENT', 'INSTANCE_ID', 'DASHBOARD_PORT', 'COSMO_DASHBOARD_PORT', 'REALTIME_PORT', 'MCP_HTTP_PORT', 'COSMO_RUNTIME_DIR', 'COSMO_WORKSPACE_PATH'],
      cron_restart: '0 0 31 2 *',
      autorestart: true, watch: false, merge_logs: true,
      out_file: path.join(HOME23, 'logs', 'pm2-watchdog-out.log'),
      error_file: path.join(HOME23, 'logs', 'pm2-watchdog-err.log'),
      env: { HOME23_WATCHDOG_INTERVAL_MS: '60000' },
    },

    // ── chrome-cdp (shared) ──
    {
      name: 'home23-chrome-cdp',
      script: path.join(HOME23, 'scripts', 'chrome-cdp.sh'),
      interpreter: 'none',
      filter_env: ['cron_restart', 'HOME23_AGENT', 'INSTANCE_ID', 'DASHBOARD_PORT', 'COSMO_DASHBOARD_PORT', 'REALTIME_PORT', 'MCP_HTTP_PORT', 'COSMO_RUNTIME_DIR', 'COSMO_WORKSPACE_PATH'],
      autorestart: true, watch: false, merge_logs: true,
      out_file: path.join(HOME23, 'logs', 'chrome-cdp-out.log'),
      error_file: path.join(HOME23, 'logs', 'chrome-cdp-err.log'),
      env: { CDP_PORT: '9222' },
    },

    // ── evobrew (shared) ──
    {
      name: 'home23-evobrew',
      script: 'server/server.js',
      cwd: path.join(HOME23, 'evobrew'),
      autorestart: true, watch: false, merge_logs: true,
      out_file: path.join(HOME23, 'logs', 'evobrew-out.log'),
      error_file: path.join(HOME23, 'logs', 'evobrew-err.log'),
      env: {
        ...commonEnv,
        ANTHROPIC_API_KEY: secrets.providers?.anthropic?.apiKey || commonEnv.ANTHROPIC_AUTH_TOKEN || '',
        COSMO_ADMIN_MODE: 'true',
        PORT: String(homeConfig.evobrew?.port || 3405),
        EVOBREW_CONFIG_DIR: path.join(HOME23, 'evobrew'),
        HOME23_MANAGED: 'true',
        NODE_ENV: 'production',
      },
    },

    // ── screenlogic (shared personal tile bridge) ──
    {
      name: 'home23-screenlogic',
      script: path.join(HOME23, 'scripts', 'screenlogic_bridge.py'),
      interpreter: screenlogicPython,
      cwd: HOME23,
      autorestart: true, watch: false, merge_logs: true,
      out_file: path.join(HOME23, 'logs', 'screenlogic-out.log'),
      error_file: path.join(HOME23, 'logs', 'screenlogic-err.log'),
      env: {
        SCREENLOGIC_ENABLED: String(screenlogicConfig.enabled !== false),
        SCREENLOGIC_HOST: String(screenlogicConfig.host || ''),
        SCREENLOGIC_ADAPTER_PORT: String(screenlogicConfig.adapterPort || 80),
        SCREENLOGIC_BRIDGE_HOST: String(screenlogicConfig.bridgeHost || '127.0.0.1'),
        SCREENLOGIC_BRIDGE_PORT: String(screenlogicConfig.bridgePort || 5023),
        SCREENLOGIC_POLL_SECONDS: String(screenlogicConfig.pollSeconds || 60),
        SCREENLOGIC_DISCOVERY: String(screenlogicConfig.discovery !== false),
      },
    },

    // ── cosmo23 (shared) ──
    {
      name: 'home23-cosmo23',
      script: 'server/index.js',
      cwd: path.join(HOME23, 'cosmo23'),
      autorestart: true, watch: false, merge_logs: true,
      out_file: path.join(HOME23, 'logs', 'cosmo23-out.log'),
      error_file: path.join(HOME23, 'logs', 'cosmo23-err.log'),
      env: {
        ...commonEnv,
        COSMO23_PORT: String(homeConfig.cosmo23?.ports?.app || 43210),
        COSMO23_WS_PORT: String(homeConfig.cosmo23?.ports?.websocket || 43240),
        COSMO23_DASHBOARD_PORT: String(homeConfig.cosmo23?.ports?.dashboard || 43244),
        COSMO23_MCP_HTTP_PORT: String(homeConfig.cosmo23?.ports?.mcp || 43247),
        COSMO23_CONFIG_DIR: path.join(HOME23, 'cosmo23', '.cosmo23-config'),
        DATABASE_URL: cosmo23DbUrl,
        ENCRYPTION_KEY: cosmo23EncryptionKey,
        COSMO_RUNTIME_DIR: path.join(HOME23, 'cosmo23', 'runs'),
        COSMO_REFERENCE_RUNS_PATHS: homeConfig.cosmo23?.source ? homeConfig.cosmo23.source + '/runs' : '',
        HOME23_MANAGED: 'true',
        HOME23_DASHBOARD_PORT: '5002',
        NODE_ENV: 'production',
      },
    },
  ],
};
