// ultra — settings (Task 9, offline-testable half). // // Reads the `ultra.*` block from Pi settings via runtime `settingsManager`. /** Resolved `ultra` configuration (design §Settings). */ export type ModelTierName = "small" | "medium" | "big"; export type ModelTiers = Partial>; export interface UltraSettings { /** When false, the tool + command refuse to run. */ enabled: boolean; /** Max concurrent sub-agents in a `fanout` phase. */ concurrency: number; /** Schema-validation retries per step. */ maxRetries: number; /** Per-step timeout when the step omits `timeoutMs`. */ defaultTimeoutMs: number; /** Load host extensions into sub-agents so their tools are grantable * (`"all"`, default) or keep sub-agents isolated to core tools (`"none"`). * The `step.tools` allowlist still gates which tools are actually active. */ subagentExtensions: "none" | "all"; /** Optional local aliases used by step.model: small|medium|big. */ modelTiers: ModelTiers; } /** Documented defaults (design §Settings table). */ export const ULTRA_DEFAULTS: UltraSettings = { enabled: true, concurrency: 5, maxRetries: 2, defaultTimeoutMs: 180000, subagentExtensions: "all", modelTiers: {}, }; function isPlainObject(v: unknown): v is Record { return v !== null && typeof v === "object" && !Array.isArray(v); } function pickModelTiers(raw: unknown): ModelTiers | undefined { if (!isPlainObject(raw)) return undefined; const out: ModelTiers = {}; for (const name of ["small", "medium", "big"] as const) { const value = raw[name]; if (typeof value === "string") out[name] = value; } return out; } /** * Extract the `ultra` block from a parsed settings object, keeping only known * keys with the correct primitive type. A missing/non-object block, wrong-typed * values, and unknown keys are dropped. */ export function pickUltra(raw: unknown): Partial { if (!isPlainObject(raw)) return {}; const ultra = raw.ultra; if (!isPlainObject(ultra)) return {}; const out: Partial = {}; if (typeof ultra.enabled === "boolean") out.enabled = ultra.enabled; if (typeof ultra.concurrency === "number") out.concurrency = ultra.concurrency; if (typeof ultra.maxRetries === "number") out.maxRetries = ultra.maxRetries; if (typeof ultra.defaultTimeoutMs === "number") out.defaultTimeoutMs = ultra.defaultTimeoutMs; if (ultra.subagentExtensions === "none" || ultra.subagentExtensions === "all") out.subagentExtensions = ultra.subagentExtensions; const modelTiers = pickModelTiers(ultra.modelTiers); if (modelTiers) out.modelTiers = modelTiers; return out; } /** Fold partial settings over the defaults; later partials win. Wrong-typed * fields are ignored (keep the prior value). */ export function mergeUltraSettings( ...partials: Partial[] ): UltraSettings { return partials.reduce( (into, p) => ({ enabled: typeof p.enabled === "boolean" ? p.enabled : into.enabled, concurrency: typeof p.concurrency === "number" ? p.concurrency : into.concurrency, maxRetries: typeof p.maxRetries === "number" ? p.maxRetries : into.maxRetries, defaultTimeoutMs: typeof p.defaultTimeoutMs === "number" ? p.defaultTimeoutMs : into.defaultTimeoutMs, subagentExtensions: p.subagentExtensions === "none" || p.subagentExtensions === "all" ? p.subagentExtensions : into.subagentExtensions, modelTiers: isPlainObject(p.modelTiers) ? p.modelTiers : into.modelTiers, }), { ...ULTRA_DEFAULTS }, ); } /** Settings source; `runtime` is the raw `settingsManager.getSettings()` object. */ export interface SettingsSources { runtime?: unknown; } /** Resolve effective settings: defaults < runtime. */ export function loadUltraSettings(sources: SettingsSources): UltraSettings { return mergeUltraSettings(pickUltra(sources.runtime)); }