Skip to content

Self-hosted, Self-Improving, Federated Everything App — dev management, agent orchestration, persistent mind, digital twin, second brain, and creative pipeline from a single dashboard over Tailscale to port the user into the OS

7.7 KB
Raw Blame
import { persistentMindMaintainerSchema } from '../lib/persistentMindMaintainer.js';
/**
 * CoS Status, Config, and Lifecycle Routes
 */

import { Router } from 'express';
import { getConfig } from '../services/cosState.js';
import { getStatus, pause, resume, start, stop, updateConfig } from '../services/cos.js';
import { getAllDomainUsageToday } from '../services/domainUsage.js';
import { reinitialize as reinitializeEmbeddings } from '../services/memoryEmbeddings.js';
import { asyncHandler } from '../lib/errorHandler.js';
import { validateRequest } from '../lib/validation.js';
import { z } from 'zod';
import { DOMAIN_IDS, DOMAIN_MODES } from '../lib/domainAutonomy.js';
import { AVATAR_VARIANT_PATTERN, RIGGED_VARIANT_PREFIX } from '../lib/avatarVariants.js';
// Single source of truth for the avatar-style vocabulary (#6253); the client
// picker re-exports the same leaf.
import { AVATAR_STYLE_IDS } from '../lib/avatarStyles.js';
import { BUDGET_LIMIT_FIELDS } from '../lib/domainBudgets.js';
import { persistentMindCapabilitiesSchema } from '../lib/persistentMindCapabilities.js';
import { persistentMindProfileSchema } from '../lib/persistentMindProfile.js';
import { persistentMindThinkingPresetsSchema } from '../lib/persistentMindThinkingPresets.js';
import { persistentMindPromptSchema } from '../lib/persistentMindPrompt.js';
import { persistentMindPlaybookSchema } from '../lib/persistentMindPlaybook.js';

const router = Router();

const pauseSchema = z.object({
  reason: z.string().optional(),
});

export const cosConfigSchema = z.object({
  goalsFile: z.string().optional(),
  healthCheckIntervalMs: z.number().int().min(1000).optional(),
  maxConcurrentAgents: z.number().int().min(1).optional(),
  maxConcurrentAgentsPerProject: z.number().int().min(1).optional(),
  maxProcessMemoryMb: z.number().int().min(128).optional(),
  maxTotalProcesses: z.number().int().min(1).optional(),
  mcpServers: z.array(z.object({
    name: z.string(),
    command: z.string(),
    args: z.array(z.string()).optional()
  })).optional(),
  autoStart: z.boolean().optional(),
  selfImprovementEnabled: z.boolean().optional(),
  appImprovementEnabled: z.boolean().optional(),
  improvementEnabled: z.boolean().optional(),
  // Built-in styles plus selectable rigged records (`rigged-<modelId>`, #5894).
  // The record half reuses the avatar variant charset (no slashes, no dots —
  // the same traversal guard `server/routes/avatar.js` enforces), so unknown
  // spellings still 400 here instead of persisting a style nothing can render.
  avatarStyle: z.union([
    z.enum(AVATAR_STYLE_IDS),
    z.string().startsWith(RIGGED_VARIANT_PREFIX).refine(
      (value) => AVATAR_VARIANT_PATTERN.test(value.slice(RIGGED_VARIANT_PREFIX.length)),
    ),
  ]).optional(),
  dynamicAvatar: z.boolean().optional(),
  alwaysOn: z.boolean().optional(),
  appReviewCooldownMs: z.number().int().min(0).optional(),
  idleReviewEnabled: z.boolean().optional(),
  idleReviewPriority: z.enum(['LOW', 'MEDIUM', 'HIGH', 'CRITICAL']).optional(),
  // Accept retired fields from cached/older clients, then discard them in the
  // transform below so they cannot be re-persisted.
  comprehensiveAppImprovement: z.boolean().optional(),
  immediateExecution: z.boolean().optional(),
  proactiveMode: z.boolean().optional(),
  autonomousJobsEnabled: z.boolean().optional(),
  autonomyLevel: z.enum(['standby', 'assistant', 'manager', 'yolo']).optional(),
  autoApproveInvestigations: z.boolean().optional(),
  // A durable reasoning route for the persistent mind. It is separate from
  // `alwaysOn`: saving or enabling this profile never starts background work.
  persistentMindMaintainer: persistentMindMaintainerSchema.optional(),
  persistentMindProfile: persistentMindProfileSchema.optional(),
  // Saved alternates one message may borrow for a single turn. Storing them
  // changes nothing about the route the mind wakes on by default.
  persistentMindThinkingPresets: persistentMindThinkingPresetsSchema.optional(),
  persistentMindPrompt: persistentMindPromptSchema.optional(),
  persistentMindPlaybook: persistentMindPlaybookSchema.optional(),
  // Separate opt-in action grant: an enabled reasoning profile does not imply
  // authority to create and execute agent tasks.
  persistentMindCapabilities: persistentMindCapabilitiesSchema.optional(),
  // Per-domain autonomy guardrails (#711): partial map of domainId → mode.
  // Partial is fine — updateConfig() merges it over the stored map.
  domainAutonomy: z.object(
    Object.fromEntries(DOMAIN_IDS.map((id) => [id, z.enum(DOMAIN_MODES).optional()]))
  ).strict().optional(),
  // Per-domain daily budgets (#711): partial map of domainId → { cap → value }.
  // Each cap is a non-negative integer or null (clear). Partial is fine —
  // updateConfig() field-merges it over the stored map. 0/null = unlimited.
  domainBudgets: z.object(
    Object.fromEntries(DOMAIN_IDS.map((id) => [
      id,
      z.object(
        Object.fromEntries(BUDGET_LIMIT_FIELDS.map((f) => [f, z.number().int().min(0).nullable().optional()]))
      ).strict().optional()
    ]))
  ).strict().optional(),
  rehabilitationGracePeriodDays: z.number().int().min(1).optional(),
  completedAgentRetentionMs: z.number().int().min(0).optional(),
  embeddingProviderId: z.string().optional(),
  embeddingModel: z.string().optional(),
  autoFixThresholds: z.object({
    maxLinesChanged: z.number().int().min(1).optional(),
    allowedCategories: z.array(z.string()).optional()
  }).optional(),
  // Safety-kind override (#2440): which outward-facing/irreversible kinds always
  // require human approval, orthogonal to confidence. updateConfig() replaces the
  // whole object, so the client sends the full slice.
  safetyKindApproval: z.object({
    enabled: z.boolean().optional(),
    alwaysApproveKinds: z.array(z.string()).optional()
  }).strict().optional()
}).strict().transform((config) => {
  const supportedConfig = { ...config };
  delete supportedConfig.autonomyLevel;
  delete supportedConfig.comprehensiveAppImprovement;
  delete supportedConfig.immediateExecution;
  delete supportedConfig.proactiveMode;
  return supportedConfig;
});

// GET /api/cos - Get CoS status
router.get('/', asyncHandler(async (req, res) => {
  const status = await getStatus();
  res.json(status);
}));

// POST /api/cos/start - Start CoS daemon
router.post('/start', asyncHandler(async (req, res) => {
  const result = await start();
  res.json(result);
}));

// POST /api/cos/stop - Stop CoS daemon
router.post('/stop', asyncHandler(async (req, res) => {
  const result = await stop();
  res.json(result);
}));

// POST /api/cos/pause - Pause CoS daemon (stays running but skips evaluations)
router.post('/pause', asyncHandler(async (req, res) => {
  const { reason } = validateRequest(pauseSchema, req.body);
  const result = await pause(reason);
  res.json(result);
}));

// POST /api/cos/resume - Resume CoS daemon from pause
router.post('/resume', asyncHandler(async (req, res) => {
  const result = await resume();
  res.json(result);
}));

// GET /api/cos/config - Get configuration
router.get('/config', asyncHandler(async (req, res) => {
  const config = await getConfig();
  res.json(config);
}));

// GET /api/cos/budget-usage - Today's per-domain autonomy usage (for the
// Domain Budgets panel). Reflects the rolling daily ledger that budgets gate on.
router.get('/budget-usage', asyncHandler(async (req, res) => {
  const usage = await getAllDomainUsageToday();
  res.json(usage);
}));

// PUT /api/cos/config - Update configuration
router.put('/config', asyncHandler(async (req, res) => {
  const validated = validateRequest(cosConfigSchema, req.body);
  const config = await updateConfig(validated);
  if (validated.embeddingProviderId !== undefined || validated.embeddingModel !== undefined) {
    reinitializeEmbeddings();
  }
  res.json(config);
}));

export default router;