import { existsSync, readFileSync, writeFileSync } from 'node:fs'; import { getWorkspaceDir } from './init-command.js'; import { ensureUserDataLayout, getUserDataDiagnosticsConfigFile, } from './user-data.js'; export const DIAGNOSTICS_LEVELS = ['errors', 'telemetry', 'full'] as const; export type DiagnosticsLevel = typeof DIAGNOSTICS_LEVELS[number]; export interface DiagnosticsConfig { level: DiagnosticsLevel; allow_upload: boolean; capture?: DiagnosticsCaptureConfig; } export interface DiagnosticsCaptureConfig { actions?: boolean; behavior_io?: boolean; perception_io?: boolean; max_record_bytes?: number; } const TOP_LEVEL_KEYS = new Set(['level', 'allow_upload', 'capture']); export function defaultDiagnosticsConfig(): DiagnosticsConfig { return { level: 'errors', allow_upload: false, }; } function record(value: unknown): Record | null { return value && typeof value === 'object' && !Array.isArray(value) ? value as Record : null; } export function parseDiagnosticsConfig(value: unknown): DiagnosticsConfig { const root = record(value); if (!root) throw new Error('user-data/diagnostics.json must contain a JSON object.'); const unknownKeys = Object.keys(root).filter((key) => !TOP_LEVEL_KEYS.has(key)); if (unknownKeys.length > 0) { throw new Error(`Unknown user-data/diagnostics.json field: ${unknownKeys.join(', ')}`); } if (!(DIAGNOSTICS_LEVELS as readonly unknown[]).includes(root.level)) { throw new Error('user-data/diagnostics.json level must be errors, telemetry, or full.'); } if (typeof root.allow_upload !== 'boolean') { throw new Error('user-data/diagnostics.json allow_upload must be a boolean.'); } let capture: DiagnosticsCaptureConfig | undefined; if (root.capture !== undefined) { const value = record(root.capture); if (!value) throw new Error('diagnostics.capture must be an object.'); if (Object.keys(value).some(key => !['actions', 'behavior_io', 'perception_io', 'max_record_bytes'].includes(key))) { throw new Error('Unknown diagnostics.capture field.'); } for (const key of ['actions', 'behavior_io', 'perception_io']) { if (value[key] !== undefined && typeof value[key] !== 'boolean') throw new Error(`diagnostics.capture.${key} must be boolean.`); } if (value.max_record_bytes !== undefined && (!Number.isInteger(value.max_record_bytes) || Number(value.max_record_bytes) < 4096 || Number(value.max_record_bytes) > 4 * 1024 * 1024)) { throw new Error('diagnostics.capture.max_record_bytes must be 4096..4194304.'); } capture = value as DiagnosticsCaptureConfig; } return { level: root.level as DiagnosticsLevel, allow_upload: root.allow_upload, ...(capture ? { capture } : {}), }; } export function writeDiagnosticsConfig( config: DiagnosticsConfig, workspaceDir = getWorkspaceDir(), ): DiagnosticsConfig { ensureUserDataLayout(workspaceDir); const normalized = parseDiagnosticsConfig(config); writeFileSync( getUserDataDiagnosticsConfigFile(workspaceDir), `${JSON.stringify(normalized, null, 2)}\n`, 'utf8', ); return normalized; } export function readDiagnosticsConfig(workspaceDir = getWorkspaceDir()): DiagnosticsConfig { ensureUserDataLayout(workspaceDir); const path = getUserDataDiagnosticsConfigFile(workspaceDir); if (!existsSync(path)) return writeDiagnosticsConfig(defaultDiagnosticsConfig(), workspaceDir); try { return parseDiagnosticsConfig(JSON.parse(readFileSync(path, 'utf8'))); } catch (error) { if (error instanceof SyntaxError) { throw new Error(`Invalid JSON in ${path}: ${error.message}`); } throw error; } }