import { existsSync, readFileSync } from "node:fs"; import { join } from "node:path"; import { resolvePolicyRoots, type PolicyRootTable } from "../../lib/policy-roots.ts"; import { clampDelegateDepth, MAX_DELEGATE_DEPTH } from "../helpers.ts"; import { DEFAULT_RUN_HISTORY_KEEP, normalizeRunHistoryKeep } from "../run-namespace.js"; import { DEFAULT_WATCHDOG_SETTING, WATCHDOG_SETTINGS, normalizeWatchdogSetting } from "../drift-watchdog.js"; import { DEFAULT_WATCHDOG_SYSTEM1_MODE, type WatchdogSystem1Mode } from "../system1-runtime.ts"; import type { SubagentRole } from "../types.ts"; export const THINKING_LEVELS = ["off", "minimal", "low", "medium", "high", "xhigh"] as const; const VALID_THINKING_LEVELS = new Set(THINKING_LEVELS); export function normalizeThinkingLevel(value: string | undefined | null): { level: string; warning?: string } { if (value == null || String(value).trim() === "") return { level: "off" }; const level = String(value).trim().toLowerCase(); if (VALID_THINKING_LEVELS.has(level)) return { level }; return { level: "off", warning: `thinking "${value}" is not a valid level (off|minimal|low|medium|high|xhigh) — using off` }; } export interface AgentTeamOverrides { language: string; personaModels: Record; personaModelLists: Record; personaThinking: Record; personaSubagents: Record>; personaDelegateDepth: Record; rulesDirs: string[]; docsPaths: string[]; policyRoots?: PolicyRootTable; reconSearchTimeoutMs: number | null; budgetOverrides: { maxDispatches?: number | null; maxResearch?: number | null; wallMs?: number | null; agentTurnMs?: number | null; recycleRuns?: number | null }; watchdogSetting: string; watchdogSystem1Mode: WatchdogSystem1Mode; watchdogJudgeModel: string | null; runHistoryKeep: number | null; pollPanel: string | null; warnings: string[]; } export const DEFAULT_OVERRIDES: AgentTeamOverrides = { language: "English", personaModels: {}, personaModelLists: {}, personaThinking: {}, personaSubagents: {}, personaDelegateDepth: {}, rulesDirs: [], docsPaths: [], reconSearchTimeoutMs: 120_000, budgetOverrides: {}, watchdogSetting: DEFAULT_WATCHDOG_SETTING, watchdogSystem1Mode: DEFAULT_WATCHDOG_SYSTEM1_MODE, watchdogJudgeModel: null, runHistoryKeep: DEFAULT_RUN_HISTORY_KEEP, pollPanel: null, warnings: [], }; function freshOverrides(): AgentTeamOverrides { return { ...DEFAULT_OVERRIDES, personaModels: {}, personaModelLists: {}, personaThinking: {}, personaSubagents: {}, personaDelegateDepth: {}, rulesDirs: [], docsPaths: [], budgetOverrides: {}, pollPanel: null, warnings: [] }; } function parseSubagentOverride(value: string): { model: string; tools?: string; thinking?: string } | null { const trimmed = value.trim(); if (!trimmed) return null; const match = trimmed.match(/^([^\s,]+)(?:\s*,\s*(.*))?$/); if (!match) return null; const result: { model: string; tools?: string; thinking?: string } = { model: match[1] }; const rest = match[2] ?? ""; if (!rest) return result; for (const chunk of rest.split(/\s*,\s*(?=[A-Za-z][\w-]*\s*=)/)) { const pair = chunk.match(/^([A-Za-z][\w-]*)\s*=\s*(.+)$/); if (!pair) continue; const key = pair[1].toLowerCase(); const item = pair[2].trim(); if (key === "tools" && item) result.tools = item; else if (key === "thinking" && item) result.thinking = item; } return result; } function resolveOverridesPolicy(cwd: string, overrides: AgentTeamOverrides): AgentTeamOverrides { overrides.policyRoots = resolvePolicyRoots(cwd, overrides); for (const diagnostic of overrides.policyRoots.diagnostics) { overrides.warnings.push(`policy roots: ${diagnostic.message} — check rules/docs paths relative to the checkout; grants do not authorize writes or remote capture`); } return overrides; } export function parseAgentTeamOverrides(cwd: string): AgentTeamOverrides { const file = join(cwd, ".ai", "agent-fleet-overrides.md"); if (!existsSync(file)) return resolveOverridesPolicy(cwd, freshOverrides()); let raw: string; try { raw = readFileSync(file, "utf-8"); } catch { return resolveOverridesPolicy(cwd, freshOverrides()); } const result = freshOverrides(); let inSection = false; for (const rawLine of raw.split("\n")) { const line = rawLine.replace(/\r$/, ""); const heading = line.match(/^##\s+(.+?)\s*$/); if (heading) { const name = heading[1].trim().toLowerCase(); inSection = name === "agent-hub" || name === "agent-team"; continue; } if (!inSection) continue; const pair = line.match(/^\s*([a-zA-Z][\w.-]*)\s*:\s*(.+?)\s*$/); if (!pair) continue; const key = pair[1].toLowerCase(); const value = pair[2].trim(); if (key === "language" && value) result.language = value; if (key === "rules" && value) result.rulesDirs = value.split(",").map(s => s.trim()).filter(Boolean); if (key === "docs" && value) result.docsPaths = value.split(",").map(s => s.trim()).filter(Boolean); if (key === "run-history-keep" && value) { const keep = normalizeRunHistoryKeep(value); if (keep === undefined) result.warnings.push(`run-history-keep "${value}" is not a positive integer or "off" — using the default (${DEFAULT_RUN_HISTORY_KEEP})`); else result.runHistoryKeep = keep; } if (key === "research-keep" && value) { result.warnings.push("research-keep is removed; live research helpers disappear when they finish. Delete this key."); } if (key === "recon-search-timeout-s" && value) { if (value.toLowerCase() === "off") result.reconSearchTimeoutMs = null; else if (/^\d+$/.test(value) && Number(value) >= 1 && Number(value) <= 3600) result.reconSearchTimeoutMs = Number(value) * 1000; else result.warnings.push(`recon-search-timeout-s "${value}" is not an integer from 1 to 3600 or "off" — using the default (120)`); } if (key === "mode" && value) result.warnings.push(`mode "${value}" is ignored — execution modes were removed; budgets follow task tier. Remove this key.`); if (key === "watchdog" && value) { const setting = normalizeWatchdogSetting(value); if (setting) result.watchdogSetting = setting; else result.warnings.push(`watchdog "${value}" is not one of ${WATCHDOG_SETTINGS.join("|")} — using the default (${DEFAULT_WATCHDOG_SETTING})`); } if (key === "watchdog-judge-model" && value) result.watchdogJudgeModel = value; if (key === "poll-panel" && value) result.pollPanel = value; if (["max-dispatches-per-turn", "max-research-per-turn", "turn-wall-time-s"].includes(key)) result.warnings.push(`${key} is ignored: continuation quotas were removed; usage remains visible and watchdog supervises progress.`); const budgetKeys: Record = { "agent-turn-timeout-s": { field: "agentTurnMs", scaleMs: true }, "session-recycle-runs": { field: "recycleRuns", scaleMs: false }, }; if (budgetKeys[key] && value) { const { field, scaleMs } = budgetKeys[key]; if (value.toLowerCase() === "off") result.budgetOverrides[field] = null; else if (/^\d+$/.test(value) && Number(value) >= 1) result.budgetOverrides[field] = Number(value) * (scaleMs ? 1000 : 1); else result.warnings.push(`${key} "${value}" is not a positive integer or "off" — using the task-tier default`); } const slug = "[a-z0-9]+(?:-[a-z0-9]+)*"; const model = key.match(new RegExp(`^model\\.(${slug})$`)); if (model && value) result.personaModels[model[1]] = value; const models = key.match(new RegExp(`^models\\.(${slug})$`)); if (models && value) result.personaModelLists[models[1]] = value.split(",").map(s => s.trim()).filter(Boolean); const thinking = key.match(new RegExp(`^thinking\\.(${slug})$`)); if (thinking && value) { const level = value.toLowerCase(); if (VALID_THINKING_LEVELS.has(level)) result.personaThinking[thinking[1]] = level; else result.warnings.push(`thinking.${thinking[1]} "${value}" is not a valid level (off|minimal|low|medium|high|xhigh) — ignored`); } const subagent = key.match(new RegExp(`^subagents\\.(${slug})\\.(${slug})$`)); if (subagent && value) { const parsed = parseSubagentOverride(value); if (parsed) { let thinking: string | undefined; if (parsed.thinking) { const normalized = normalizeThinkingLevel(parsed.thinking); if (normalized.warning) result.warnings.push(`subagents.${subagent[1]}.${subagent[2]} ${normalized.warning}`); else thinking = normalized.level; } (result.personaSubagents[subagent[1]] ||= {})[subagent[2]] = { model: parsed.model, ...(parsed.tools ? { tools: parsed.tools } : {}), ...(thinking ? { thinking } : {}), }; } } const depth = key.match(new RegExp(`^delegate-depth\\.(${slug})$`)); if (depth && value) { const count = Number(value); if (Number.isInteger(count) && count >= 0) { result.personaDelegateDepth[depth[1]] = clampDelegateDepth(count); if (count > MAX_DELEGATE_DEPTH) result.warnings.push(`delegate-depth.${depth[1]} ${count} exceeds the maximum (${MAX_DELEGATE_DEPTH}) — clamped to ${MAX_DELEGATE_DEPTH}`); } } } return resolveOverridesPolicy(cwd, result); }