// 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; /** Activate `run_workflow` when the session starts. */ autoEnable: boolean; /** Max concurrent sub-agents in a `fanout` phase. */ concurrency: number; /** Result-submission reminders after an agent stops without output. */ maxRetries: number; /** Extensions loaded into every sub-agent: all discovered extensions (`"all"`, * default), none (`"none"`), or one static list of sibling pi-ext names. * `step.tools` selects the initial tools; loaded extensions may change them. */ subagentExtensions: "none" | "all" | string[]; /** Load host skills into sub-agents (`"all"`, default) or omit them (`"none"`). */ subagentSkills: "none" | "all"; /** Retain parent-linked sub-agent sessions, or run them entirely in memory. */ subagentSessionRetention: "all" | "none"; /** 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, autoEnable: false, concurrency: 5, maxRetries: 2, subagentExtensions: "all", subagentSkills: "all", subagentSessionRetention: "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.auto_enable === "boolean") out.autoEnable = ultra.auto_enable; 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; else if (Array.isArray(ultra.subagentExtensions)) out.subagentExtensions = [ ...new Set( ultra.subagentExtensions .filter((value): value is string => typeof value === "string") .map((value) => value.trim()) .filter(Boolean), ), ]; if (ultra.subagentSkills === "none" || ultra.subagentSkills === "all") out.subagentSkills = ultra.subagentSkills; if ( ultra.subagentSessionRetention === "none" || ultra.subagentSessionRetention === "all" ) out.subagentSessionRetention = ultra.subagentSessionRetention; 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, autoEnable: typeof p.autoEnable === "boolean" ? p.autoEnable : into.autoEnable, concurrency: typeof p.concurrency === "number" ? p.concurrency : into.concurrency, maxRetries: typeof p.maxRetries === "number" ? p.maxRetries : into.maxRetries, subagentExtensions: Array.isArray(p.subagentExtensions) ? [...p.subagentExtensions] : p.subagentExtensions === "none" || p.subagentExtensions === "all" ? p.subagentExtensions : into.subagentExtensions, subagentSkills: p.subagentSkills === "none" || p.subagentSkills === "all" ? p.subagentSkills : into.subagentSkills, subagentSessionRetention: p.subagentSessionRetention === "none" || p.subagentSessionRetention === "all" ? p.subagentSessionRetention : into.subagentSessionRetention, 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), ); }