// ultra — settings (Task 9, offline-testable half). // // Reads the `ultra.*` block from Pi's global and project settings. /** 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; /** 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"; /** Load host skills into sub-agents (`"all"`, default) or omit them (`"none"`). */ subagentSkills: "none" | "all"; /** Optional local aliases used by step.model: small|medium|big. */ modelTiers: ModelTiers; /** Fast model used to turn raw tool args into board action phrases; null disables it. */ actionSummarizerModel: string | null; } /** Documented defaults (design §Settings table). */ export const ULTRA_DEFAULTS: UltraSettings = { enabled: true, concurrency: 5, maxRetries: 2, subagentExtensions: "all", subagentSkills: "all", modelTiers: {}, actionSummarizerModel: null, }; 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 (ultra.subagentExtensions === "none" || ultra.subagentExtensions === "all") out.subagentExtensions = ultra.subagentExtensions; if (ultra.subagentSkills === "none" || ultra.subagentSkills === "all") out.subagentSkills = ultra.subagentSkills; const modelTiers = pickModelTiers(ultra.modelTiers); if (modelTiers) out.modelTiers = modelTiers; if (ultra.actionSummarizerModel === null) out.actionSummarizerModel = null; else if ( typeof ultra.actionSummarizerModel === "string" && ultra.actionSummarizerModel.trim() ) out.actionSummarizerModel = ultra.actionSummarizerModel.trim(); 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, subagentExtensions: p.subagentExtensions === "none" || p.subagentExtensions === "all" ? p.subagentExtensions : into.subagentExtensions, subagentSkills: p.subagentSkills === "none" || p.subagentSkills === "all" ? p.subagentSkills : into.subagentSkills, modelTiers: isPlainObject(p.modelTiers) ? { ...into.modelTiers, ...p.modelTiers } : into.modelTiers, actionSummarizerModel: p.actionSummarizerModel === null || typeof p.actionSummarizerModel === "string" ? p.actionSummarizerModel : into.actionSummarizerModel, }), { ...ULTRA_DEFAULTS }, ); } export interface SettingsSources { global?: unknown; project?: unknown; } /** Resolve effective settings: defaults < global < project. */ export function loadUltraSettings(sources: SettingsSources): UltraSettings { return mergeUltraSettings( pickUltra(sources.global), pickUltra(sources.project), ); }