All files / agent/src/config types.ts

100% Statements 340/340
100% Branches 0/0
100% Functions 0/0
100% Lines 340/340

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 3401x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x
/**
 * Agent Configuration Types
 *
 * Defines TypeScript types for agent configuration schema
 * Requirements: 1.1, 1.2
 */
 
export interface DockerConfig {
  baseImage: string;
  networkMode: 'none' | 'bridge';
  memoryLimit: string;
  cpuLimit: string;
  volumeMounts: VolumeMount[];
  registryAuth?: RegistryAuth;
}
 
// --- Enhanced configuration types ---
 
/** Rules for deciding which jobs this agent will accept */
export interface AcceptanceRules {
  /** Recipe names to accept (glob patterns). Default: ["*"] */
  allowedRecipes: string[];
  /** Recipe names to reject (glob patterns). Default: [] */
  blockedRecipes: string[];
  /** Repository patterns to accept (glob patterns). Default: ["*"] */
  allowedRepositories: string[];
  /** Maximum job duration in minutes. Default: 120 */
  maxJobDurationMinutes: number;
  /** Require Docker for all jobs. Default: false */
  requireDocker: boolean;
}
 
/** Quiet hours configuration for reduced resource usage during off-peak times */
export interface QuietHoursConfig {
  /** Cron-like schedule: "HH:MM-HH:MM" (24h format) */
  schedule: string;
  /** IANA timezone. Default: system timezone */
  timezone: string;
  /** Max concurrent jobs during quiet hours */
  maxConcurrentJobs: number;
  /** Max CPU percent during quiet hours */
  maxCpuPercent: number;
}
 
/** Resource governance limits for the agent */
export interface ResourceGovernance {
  /** Max CPU usage percent (0-100). Default: 80 */
  maxCpuPercent: number;
  /** Max memory usage percent (0-100). Default: 70 */
  maxMemoryPercent: number;
  /** Minimum disk space to keep free in GB. Default: 10 */
  reservedDiskGB: number;
  /** Quiet hours configuration */
  quietHours?: QuietHoursConfig;
}
 
/** Configuration for native (non-Docker) execution */
export interface NativeExecutionConfig {
  /** Enable native (non-Docker) execution. Default: false */
  enabled: boolean;
  /** macOS sandbox profile name */
  sandboxProfile?: string;
  /** Allowed filesystem paths for native builds */
  allowedPaths: string[];
  /** Run as separate user. Default: true */
  userIsolation: boolean;
}
 
/** Execution mode configuration */
export interface ExecutionConfig {
  /** Default execution mode: docker, native, or auto. Default: docker */
  defaultMode: 'docker' | 'native' | 'auto';
  /** Native execution settings */
  native: NativeExecutionConfig;
  /** Docker settings */
  docker: DockerConfig;
}
 
/** Build cache configuration */
export interface CacheConfig {
  /** Enable build caching. Default: true */
  enabled: boolean;
  /** Cache storage directory */
  directory: string;
  /** Maximum total cache size in GB. Default: 20 */
  maxSizeGB: number;
  /** Eviction strategy. Default: lru */
  strategy: 'lru' | 'fifo' | 'lfu';
  /** Named cache volumes (name -> host path) */
  volumes: Record<string, string>;
}
 
export interface VolumeMount {
  hostPath: string;
  containerPath: string;
  readOnly: boolean;
}
 
export interface RegistryAuth {
  username: string;
  password: string;
  registry: string;
}
 
export interface SecurityConfig {
  // Resource limits
  maxMemoryMB: number;
  maxCpuCores: number;
  maxDiskSpaceMB: number;
  maxProcesses: number;
  maxFileDescriptors: number;
 
  // Network security
  networkMode: 'none' | 'bridge';
  allowedOutboundPorts: number[];
  blockPrivateNetworks: boolean;
 
  // Filesystem security
  readOnlyRoot: boolean;
  allowedWritePaths: string[];
  tempDirSizeMB: number;
 
  // Container security
  dropAllCapabilities: boolean;
  allowedCapabilities: string[];
  noNewPrivileges: boolean;
  seccompProfile?: string;
  apparmorProfile?: string;
 
  // Execution limits
  maxExecutionTimeSeconds: number;
  killTimeoutSeconds: number;
 
  // Monitoring
  enableResourceMonitoring: boolean;
  enableSecurityAuditing: boolean;
  logSecurityEvents: boolean;
}
 
export interface S3Config {
  bucket: string;
  region: string;
  accessKeyId?: string;
  secretAccessKey?: string;
  endpoint?: string; // For S3-compatible storage
  pathPrefix?: string; // Prefix for all artifact paths
}
 
export interface StorageConfig {
  type: 'local' | 's3';
 
  // Local storage configuration
  localPath?: string; // Base path for local artifact storage
 
  // S3 configuration
  s3?: S3Config;
 
  // Cleanup settings
  cleanupAfterUpload: boolean; // Delete local files after successful upload
  retentionDays: number; // How long to keep artifacts
}
 
export interface AgentConfig {
  // Platform connection
  platformUrl: string;
  apiKey: string;
  agentId: string;
 
  // Agent settings
  name: string;
  maxConcurrentJobs: number;
  heartbeatInterval: number; // seconds
  tags: string[];
 
  // Docker configuration
  dockerConfig: DockerConfig;
 
  // Enhanced security configuration
  securityConfig: SecurityConfig;
 
  // Storage configuration (MVP.4.2.4)
  storageConfig: StorageConfig;
 
  // Resource limits
  maxMemoryUsage: number; // percentage (0-100)
  maxCpuUsage: number; // percentage (0-100)
  maxDiskUsage: number; // percentage (0-100)
 
  // Logging
  logLevel: 'debug' | 'info' | 'warn' | 'error';
  logRetentionDays: number;
 
  // Security
  allowedRepositories?: string[]; // Optional whitelist
  blockedRepositories?: string[]; // Optional blacklist
 
  // Timeouts
  jobTimeoutMinutes: number;
  connectionTimeoutSeconds: number;
 
  // Features
  enableMetrics: boolean;
  enableAutoUpdates: boolean;
 
  // Enhanced configuration sections
  acceptanceRules: AcceptanceRules;
  resourceGovernance: ResourceGovernance;
  executionConfig: ExecutionConfig;
  cacheConfig: CacheConfig;
}
 
export interface ConfigValidationError {
  field: string;
  message: string;
  value?: any;
}
 
export interface ConfigValidationResult {
  isValid: boolean;
  errors: ConfigValidationError[];
}
 
export interface EnvironmentOverrides {
  BUILDHIVE_PLATFORM_URL?: string;
  BUILDHIVE_API_KEY?: string;
  BUILDHIVE_AGENT_ID?: string;
  BUILDHIVE_AGENT_NAME?: string;
  BUILDHIVE_MAX_CONCURRENT_JOBS?: string;
  BUILDHIVE_HEARTBEAT_INTERVAL?: string;
  BUILDHIVE_TAGS?: string; // comma-separated
  BUILDHIVE_LOG_LEVEL?: string;
  BUILDHIVE_JOB_TIMEOUT_MINUTES?: string;
  BUILDHIVE_ENABLE_METRICS?: string;
  BUILDHIVE_ENABLE_AUTO_UPDATES?: string;
 
  // Resource governance overrides
  BUILDHIVE_MAX_CPU_PERCENT?: string;
  BUILDHIVE_MAX_MEMORY_PERCENT?: string;
  BUILDHIVE_RESERVED_DISK_GB?: string;
 
  // Acceptance rules overrides
  BUILDHIVE_ALLOWED_RECIPES?: string; // comma-separated glob patterns
  BUILDHIVE_BLOCKED_RECIPES?: string; // comma-separated glob patterns
  BUILDHIVE_ALLOWED_REPOSITORIES?: string; // comma-separated glob patterns
  BUILDHIVE_MAX_JOB_DURATION_MINUTES?: string;
  BUILDHIVE_REQUIRE_DOCKER?: string;
 
  // Execution config overrides
  BUILDHIVE_EXECUTION_MODE?: string;
 
  // Cache config overrides
  BUILDHIVE_CACHE_ENABLED?: string;
  BUILDHIVE_CACHE_DIRECTORY?: string;
  BUILDHIVE_CACHE_MAX_SIZE_GB?: string;
}
 
export const DEFAULT_CONFIG: Partial<AgentConfig> = {
  maxConcurrentJobs: 1,
  heartbeatInterval: 30,
  tags: [],
  dockerConfig: {
    baseImage: 'ubuntu:22.04',
    networkMode: 'none',
    memoryLimit: '2g',
    cpuLimit: '1.0',
    volumeMounts: []
  },
  securityConfig: {
    maxMemoryMB: 2048,
    maxCpuCores: 2,
    maxDiskSpaceMB: 10240,
    maxProcesses: 256,
    maxFileDescriptors: 1024,
    networkMode: 'none',
    allowedOutboundPorts: [],
    blockPrivateNetworks: true,
    readOnlyRoot: true,
    allowedWritePaths: ['/workspace', '/tmp/buildhive'],
    tempDirSizeMB: 512,
    dropAllCapabilities: true,
    allowedCapabilities: ['CHOWN', 'DAC_OVERRIDE', 'FOWNER', 'SETGID', 'SETUID'],
    noNewPrivileges: true,
    maxExecutionTimeSeconds: 3600,
    killTimeoutSeconds: 30,
    enableResourceMonitoring: true,
    enableSecurityAuditing: true,
    logSecurityEvents: true
  },
  storageConfig: {
    type: 'local',
    localPath: '/tmp/buildhive/artifacts',
    cleanupAfterUpload: true,
    retentionDays: 7
  },
  maxMemoryUsage: 80,
  maxCpuUsage: 80,
  maxDiskUsage: 90,
  logLevel: 'info',
  logRetentionDays: 7,
  jobTimeoutMinutes: 60,
  connectionTimeoutSeconds: 30,
  enableMetrics: true,
  enableAutoUpdates: true,
 
  // Enhanced configuration defaults
  acceptanceRules: {
    allowedRecipes: ['*'],
    blockedRecipes: [],
    allowedRepositories: ['*'],
    maxJobDurationMinutes: 120,
    requireDocker: false
  },
  resourceGovernance: {
    maxCpuPercent: 80,
    maxMemoryPercent: 70,
    reservedDiskGB: 10
  },
  executionConfig: {
    defaultMode: 'docker',
    native: {
      enabled: false,
      allowedPaths: ['/tmp/buildhive'],
      userIsolation: true
    },
    docker: {
      baseImage: 'ubuntu:22.04',
      networkMode: 'none',
      memoryLimit: '2g',
      cpuLimit: '1.0',
      volumeMounts: []
    }
  },
  cacheConfig: {
    enabled: true,
    directory: '/tmp/buildhive/cache',
    maxSizeGB: 20,
    strategy: 'lru',
    volumes: {}
  }
};