titan-agent 5.4.1 → 5.4.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1 +1 @@
1
- {"version":3,"sources":["../../src/agent/agent.ts"],"sourcesContent":["/**\n * TITAN — Core Agent Loop\n * The main agent: receives messages, builds context, calls LLM, handles tools, responds.\n */\nimport { existsSync, readFileSync } from 'fs';\nimport { randomBytes } from 'crypto';\nimport { loadConfig } from '../config/config.js';\nimport { getOrCreateSession, getOrCreateSessionById, addMessage, getContextMessages } from './session.js';\nimport { getToolDefinitions } from './toolRunner.js';\nimport { recordUsage, searchMemories } from '../memory/memory.js';\nimport { getLearningContext, learnFact, getToolWarnings, classifyTaskType, recordStrategy, recordStrategyOutcome, getStrategyHints, getLearnedPreferenceHints } from '../memory/learning.js';\nimport { buildPersonalContext } from '../memory/relationship.js';\nimport { retainStrategy, getHindsightHints } from '../memory/hindsightBridge.js';\nimport { getTeachingContext, isCorrection } from './teaching.js';\nimport { recordCorrection } from './userProfile.js';\nimport { heartbeat, clearSession, setStallHandler, setAutonomousMode } from './stallDetector.js';\n// Hunt Finding #22 / #46: resetLoopDetection is intentionally NOT called here.\n// See agent.ts:~1466 for the explanation. Session-close handles cleanup.\n// (Import left out so lint doesn't flag the unused symbol.)\nimport { routeModel } from './costOptimizer.js';\nimport { getPlugins } from '../plugins/registry.js';\nimport { runAfterTurn } from '../plugins/contextEngine.js';\nimport { getSwarmRouterTools } from './swarm.js';\nimport { shouldDeliberate, analyze, generatePlan, executePlan, handleApproval, getDeliberation, cancelDeliberation, formatPlanResults } from './deliberation.js';\nimport type { ChatMessage } from '../providers/base.js';\nimport { initGraph, addEpisode, getGraphContext } from '../memory/graph.js';\nimport { isAvailable as isBrainAvailable, selectTools as brainSelectTools, ensureLoaded as ensureBrainLoaded } from './brain.js';\nimport { DEFAULT_CORE_TOOLS } from './toolSearch.js';\nimport { classifyPipeline, resolvePipelineConfig, PIPELINE_PROFILES } from './pipeline.js';\nimport { buildSelfAwarenessContext } from './selfAwareness.js';\nimport { assembleSystemPrompt, type PromptMode } from './systemPromptParts.js';\nimport { getOptimizedPromptBlock } from '../skills/builtin/self_improve.js';\nimport { analyzeForDelegation, executeDelegationPlan } from './orchestrator.js';\nimport { queueWakeup } from './agentWakeup.js';\nimport { createIssue, requestHireApproval } from './commandPost.js';\nimport { spawnSubAgent, SUB_AGENT_TEMPLATES } from './subAgent.js';\nimport { logTrajectory } from './trajectoryLogger.js';\nimport { processTrajectoryForSkills, getSkillGuidance } from './autoSkillGen.js';\nimport { getAgent } from './multiAgent.js';\nimport { isDangerous } from '../utils/safety.js';\nimport { registerTool } from './toolRunner.js';\nimport { runAgentLoop, type LoopResult } from './agentLoop.js';\nimport { startTrace } from './tracer.js';\nimport { initSoulState, updateSoulState, emitHeartbeat, getInnerMonologue, consolidateWisdom, clearSoulState, getWisdomHints } from './soul.js';\nimport logger from '../utils/logger.js';\nimport { TITAN_NAME, AGENTS_MD, SOUL_MD, TOOLS_MD, TITAN_MD_FILENAME } from '../utils/constants.js';\n\nconst COMPONENT = 'Agent';\nconst MAX_TOOL_ROUNDS = 10;\n\n/** Estimate the round budget based on task complexity */\n/**\n * Estimate how many tool rounds a message needs.\n * Simple tasks (read a file, run a command) → 3-4 rounds.\n * Multi-step tasks (read, analyze, write) → 6-10 rounds.\n * Complex tasks (research, build, deploy) → 12-20 rounds.\n */\nfunction estimateRoundBudget(message: string, config: { agent: { dynamicBudget?: boolean; maxToolRoundsHard?: number }; autonomy: { mode: string } } & Record<string, unknown>): number {\n const agentConfig = config.agent as Record<string, unknown>;\n if (agentConfig.dynamicBudget === false) return MAX_TOOL_ROUNDS;\n\n const hardCap = (agentConfig.maxToolRoundsHard as number) || 50;\n const lower = message.toLowerCase();\n const words = message.split(/\\s+/).length;\n\n // Count complexity signals\n const isQuestion = /^(what|who|how|why|where|when|which|is |are |do |does |can |will )/i.test(message.trim());\n const isSingleAction = /^(read|write|run|list|show|tell|get|check|find)\\b/i.test(message.trim());\n const isMultiStep = /\\b(then|after that|next|step \\d|finally|first.*then|and also|additionally|and then)\\b/i.test(lower);\n const isComplex = /\\b(research|analyze|investigate|compare|build|implement|create.*and|deploy|automat|refactor|rewrite|design)/i.test(lower);\n const isAmbitious = /\\b(step by step|end.to.end|full pipeline|from scratch|entire|complete|comprehensive)\\b/i.test(lower);\n\n // Count tool-intent signals (how many distinct actions are implied)\n const actionCount = [\n /\\b(read|open|show|display|check)\\b/i.test(lower),\n /\\b(write|create|save|generate|make)\\b/i.test(lower),\n /\\b(edit|change|modify|update|fix|replace)\\b/i.test(lower),\n /\\b(run|execute|install|build|test|deploy)\\b/i.test(lower),\n /\\b(search|find|look|research|investigate)\\b/i.test(lower),\n /\\b(summarize|analyze|compare|report)\\b/i.test(lower),\n ].filter(Boolean).length;\n\n let budget: number;\n\n if (isQuestion && words < 15 && !isMultiStep) {\n budget = 3; // \"What is X?\" \"Who is Y?\" — quick lookup\n } else if (isSingleAction && words < 20 && !isMultiStep) {\n budget = 4; // \"Read package.json\" \"Run uname\" — one tool call\n } else if (actionCount <= 1 && words < 30 && !isMultiStep) {\n budget = 5; // Single-purpose task, short message\n } else if (actionCount <= 2 && !isComplex) {\n budget = 8; // Two-step task (read + write, search + summarize)\n } else if (isMultiStep || actionCount >= 3) {\n budget = 12; // Multi-step explicit pipeline\n } else if (isComplex || isAmbitious) {\n budget = 18; // Research, build, deploy — needs room to work\n } else {\n budget = 6; // Default for unclassified moderate tasks\n }\n\n // In autonomous mode, use the configured maxRounds directly\n // The dynamic budget and hard cap should NOT limit autonomous execution\n const isAutonomous = config.autonomy.mode === 'autonomous';\n const configuredMax = (agentConfig.maxRounds as number) || 0;\n if (isAutonomous && configuredMax > 0) {\n return configuredMax;\n }\n\n return Math.min(budget, hardCap);\n}\n\n// ── Ralph Loop Verification ─────────────────────────────────────\n// Checks whether the agent actually completed the requested task.\n// Inspired by vercel-labs/ralph-loop-agent outer verification pattern.\n\n/**\n * Exported for regression testing in tests/agent-verify.test.ts.\n * The v4.3.4 fix narrowed its pattern matching; easy to break\n * if someone generalizes the regex again without a test.\n */\nexport function verifyTaskCompletion(\n message: string,\n toolsUsed: string[],\n response: string,\n): { complete: boolean; reason: string } {\n const lower = message.toLowerCase();\n\n // v4.3.4: conversational asides shouldn't trigger a \"you must edit_file\"\n // enforcement loop. Tony's voice notes on Messenger like\n // \"fix your voice\" or \"fix the way you respond\" would match the\n // edit|fix regex + \"file\"/\"files\" (because the voice note transcript\n // often contains the word \"files\") and trip the Ralph Loop, which\n // then injects a stale \"pending file edit task\" prompt and the\n // LLM hallucinates back \"I don't have a pending file edit task.\"\n // Two narrowings:\n // 1. Don't count `shell` as a file read. The verifier was using it\n // as a proxy for read_file but any `ls`/`pwd` call tripped it.\n // 2. Require an EXPLICIT file path or filename token (e.g. \".ts\",\n // \".py\", \"/path/\", \"src/\", etc.) rather than the bare word\n // \"file\"/\"files\" which appears constantly in natural speech.\n const verbMatch = /\\b(edit|fix|change|modify|update|add|write|create|improve|rewrite|save|implement|patch)\\b/i.test(lower);\n const hasFilePath = /[\\w-]+\\.(ts|tsx|js|jsx|py|md|json|yaml|yml|html|css|sh|txt|rs|go|java|cpp|c|h)\\b|\\bsrc\\/|\\bpath:|\\/[a-z]+\\//i.test(lower);\n const askedToWrite = verbMatch && hasFilePath;\n const didWrite = toolsUsed.some(t => ['write_file', 'edit_file', 'append_file'].includes(t));\n const didRead = toolsUsed.includes('read_file');\n\n if (askedToWrite && !didWrite && didRead) {\n return {\n complete: false,\n reason: 'You read the file but did not save any changes. You MUST call edit_file or write_file to apply your modifications. Call the tool now.',\n };\n }\n\n // Check: user asked to run/execute something but no shell was called\n const askedToRun = /\\b(run|execute|install|deploy|build|test|restart)\\b/i.test(lower)\n && /\\b(command|script|service|server|package|npm|pip)\\b/i.test(lower);\n const didRun = toolsUsed.includes('shell');\n\n if (askedToRun && !didRun) {\n return {\n complete: false,\n reason: 'You did not execute the requested command. Call the shell tool to run it.',\n };\n }\n\n return { complete: true, reason: '' };\n}\n\n// ── Current session context for spawn_agent async delegation ─────\nlet currentSessionId: string | null = null;\nexport function setCurrentSessionId(id: string | null): void { currentSessionId = id; }\nexport function getCurrentSessionId(): string | null { return currentSessionId; }\n\n// ── Register spawn_agent tool ────────────────────────────────────\nlet spawnAgentRegistered = false;\nfunction ensureSpawnAgentRegistered(): void {\n if (spawnAgentRegistered) return;\n spawnAgentRegistered = true;\n registerTool({\n name: 'spawn_agent',\n description: 'Hand off a task to a specialist teammate. Choose: scout (research), builder (code/files), writer (content), analyst (decisions), or sage (review). For parallel execution of multiple subtasks, prefer agent_team. For sequential dependent tasks, prefer agent_chain.',\n parameters: {\n type: 'object',\n properties: {\n name: { type: 'string', description: 'Optional display name for the sub-agent run (e.g., \"Scout\", \"Builder\"). The specialist persona is determined by `template`.' },\n task: { type: 'string', description: 'A clear, self-contained task description. Include all context the specialist needs — it has no memory of this conversation.' },\n template: { type: 'string', description: 'Specialist to route to. Prefer: \"scout\" | \"builder\" | \"writer\" | \"analyst\".' },\n model: { type: 'string', description: 'Model override. Usually leave blank — specialists pick their own role-tuned model.' },\n },\n required: ['task'],\n },\n execute: async (args) => {\n const templateName = (args.template as string) || '';\n\n // v5.0.0-spacewalk: resolve config-defined agents from titan.json.\n // If the template name matches a config agent entry, use its base\n // template (e.g. \"coder\") for tools/systemPrompt, but keep the\n // config agent's name, model, and systemPromptOverride.\n let configAgent: import('./agentScope.js').ResolvedAgentConfig | null = null;\n try {\n const { resolveAgentConfig, agentAllowsSkill } = await import('./agentScope.js');\n configAgent = resolveAgentConfig(templateName);\n } catch { /* optional — agentScope may not exist in some builds */ }\n\n const baseTemplateName = configAgent?.template || templateName;\n const template = SUB_AGENT_TEMPLATES[baseTemplateName] || {};\n const agentName = (args.name as string) || configAgent?.name || template.name || 'SubAgent';\n const task = args.task as string;\n\n // v5.3.1: Apply config-defined agent constraints (maxRounds, maxTokens,\n // persona, skillsFilter). Log what we apply for observability.\n let appliedFields: string[] = [];\n if (configAgent) {\n if (configAgent.maxRounds !== 15) appliedFields.push(`maxRounds=${configAgent.maxRounds}`);\n if (configAgent.maxTokens !== 4000) appliedFields.push(`maxTokens=${configAgent.maxTokens}`);\n if (configAgent.persona && configAgent.persona !== 'default') appliedFields.push(`persona=${configAgent.persona}`);\n if (configAgent.skillsFilter) appliedFields.push(`skillsFilter=[${configAgent.skillsFilter.join(',')}]`);\n if (configAgent.modelFallbacks.length > 0) logger.info('Agent', `Agent \"${agentName}\" has modelFallbacks — not yet implemented in spawn path`);\n if (configAgent.workspaceDir) appliedFields.push(`workspaceDir=${configAgent.workspaceDir}`);\n if (configAgent.tags.length > 0) appliedFields.push(`tags=[${configAgent.tags.join(',')}]`);\n if (configAgent.systemPromptOverride) appliedFields.push('systemPromptOverride=set');\n if (appliedFields.length > 0) logger.info('Agent', `Applied config agent fields for \"${agentName}\": ${appliedFields.join(', ')}`);\n }\n\n // v4.9.0: kill switch gate — if Tony killed the organism, no\n // new sub-agent spawns until he explicitly resumes.\n try {\n const { isKilled } = await import('../safety/killSwitch.js');\n if (isKilled()) {\n return '[Sub-Agent REFUSED] TITAN kill switch is active. Tony must resume via /api/safety/resume before new sub-agents can spawn.';\n }\n } catch { /* fail-open — safety module optional */ }\n\n // v4.7.0: Hermes-style safety gates on the spawn path.\n // Prevents fork bombs + concurrent runaway. Depth is best-effort\n // (we treat the primary as depth 0); the MAX_CONCURRENT_CHILDREN\n // cap is the hard backstop.\n try {\n const { canSpawnChild } = await import('./subagentSafety.js');\n const parent = currentSessionId || 'root';\n const gate = canSpawnChild(parent, 0);\n if (!gate.ok) {\n return `I can't delegate that task right now (${gate.reason}). I'll handle it myself.`;\n }\n } catch { /* fail-open — safety module optional */ }\n\n // v4.7.0: specialist routing. If the template matches a\n // registered specialist (Scout/Builder/Writer/Analyst),\n // use its tuned system prompt + preferred model.\n let specialistPrompt: string | undefined;\n let specialistModel: string | undefined;\n try {\n const { findSpecialistForTemplate, loadSpecialistPersona } = await import('./specialists.js');\n const sp = findSpecialistForTemplate(templateName);\n if (sp) {\n specialistPrompt = loadSpecialistPersona(sp.id);\n specialistModel = sp.model;\n }\n } catch { /* fall through to generic template */ }\n\n // ── Async path: delegate via Command Post ────────────\n const cpEnabled = loadConfig().commandPost?.enabled ?? false;\n if (cpEnabled) {\n const issue = createIssue({\n title: `[Agent Task] ${task.slice(0, 80)}`,\n description: task,\n priority: 'medium',\n createdByUser: 'agent',\n });\n\n const wakeup = queueWakeup({\n issueId: issue.id,\n issueIdentifier: issue.identifier,\n agentId: issue.id, // Use issue ID as agent ID for simplicity\n agentName,\n parentSessionId: currentSessionId,\n task,\n templateName,\n model: args.model as string | undefined,\n });\n\n return `I've handed that off to the ${agentName} specialist. They'll jump on it and report back when they're done.`;\n }\n\n // ── Sync path: original blocking execution ───────────\n // v4.7.0: track child for concurrent cap, use specialist prompt/model when matched.\n let childId: string | undefined;\n try {\n const { registerChild } = await import('./subagentSafety.js');\n childId = `child-${Date.now().toString(36)}-${Math.random().toString(36).slice(2, 6)}`;\n registerChild(currentSessionId || 'root', childId);\n } catch { /* safety optional */ }\n\n // Track specialist status in CP registry during execution\n let specialistId: string | undefined;\n try {\n const { findSpecialistForTemplate } = await import('./specialists.js');\n const sp = findSpecialistForTemplate(templateName);\n if (sp) specialistId = sp.id;\n } catch { /* optional */ }\n\n try {\n if (specialistId) {\n const { updateAgentStatus } = await import('./commandPost.js');\n updateAgentStatus(specialistId, 'active');\n }\n\n // v5.3.1: Filter template tools through config agent skillsFilter\n let allowedTools = template.tools;\n if (configAgent?.skillsFilter) {\n try {\n const { agentAllowsSkill } = await import('./agentScope.js');\n allowedTools = template.tools?.filter(t => agentAllowsSkill(configAgent!, t));\n if (allowedTools && allowedTools.length === 0) {\n logger.warn('Agent', `Agent \"${agentName}\" skillsFilter blocks all template tools — falling back to template default`);\n allowedTools = template.tools;\n }\n } catch { /* fall through to template.tools */ }\n }\n\n const result = await spawnSubAgent({\n name: agentName,\n task,\n tools: allowedTools,\n systemPrompt: configAgent?.systemPromptOverride || specialistPrompt || template.systemPrompt,\n model: (args.model as string | undefined) || configAgent?.model || specialistModel,\n tier: (template as Record<string, unknown>).tier as 'cloud' | 'smart' | 'fast' | 'local' | undefined,\n persona: configAgent?.persona,\n maxRounds: configAgent?.maxRounds,\n maxTokens: configAgent?.maxTokens,\n workspaceDir: configAgent?.workspaceDir ?? undefined,\n tags: configAgent?.tags,\n depth: 1, // v4.7.0: this IS a child (was incorrectly 0)\n });\n\n if (specialistId) {\n const { updateAgentStatus } = await import('./commandPost.js');\n updateAgentStatus(specialistId, 'idle');\n }\n\n return `The ${agentName} specialist finished. Here's what they found:\\n\\n${result.content}`;\n } catch (err) {\n if (specialistId) {\n try {\n const { updateAgentStatus } = await import('./commandPost.js');\n updateAgentStatus(specialistId, 'idle');\n } catch { /* best effort */ }\n }\n throw err;\n } finally {\n if (childId) {\n try {\n const { unregisterChild } = await import('./subagentSafety.js');\n unregisterChild(currentSessionId || 'root', childId);\n } catch { /* best effort */ }\n }\n }\n },\n });\n}\n\n// ── Register delegate_task tool (inter-agent delegation via Command Post) ──\nlet delegateTaskRegistered = false;\nfunction ensureDelegateTaskRegistered(): void {\n if (delegateTaskRegistered) return;\n delegateTaskRegistered = true;\n registerTool({\n name: 'delegate_task',\n description: 'Delegate a task to a multi-agent worker OR an external agent (Codex, bash). Creates a Command Post issue and returns immediately. Results are injected into your next response.',\n parameters: {\n type: 'object',\n properties: {\n agentId: { type: 'string', description: 'Target agent ID (from list_agents). Required unless using an external adapter.' },\n task: { type: 'string', description: 'Task description for the worker' },\n priority: { type: 'string', description: 'Priority: low, medium, high, critical (default: medium)' },\n adapter: { type: 'string', description: 'External adapter: \"codex\" or \"bash\". When set, task runs via external CLI instead of internal agent.' },\n cwd: { type: 'string', description: 'Working directory for external adapters (optional)' },\n },\n required: ['task'],\n },\n execute: async (args) => {\n const task = args.task as string;\n const priority = (args.priority as string) || 'medium';\n const adapterType = args.adapter as string | undefined;\n const cwd = args.cwd as string | undefined;\n\n if (adapterType) {\n // ── External adapter path ────────────────────────\n const issue = createIssue({\n title: `[External: ${adapterType}] ${task.slice(0, 70)}`,\n description: task,\n priority: priority as 'low' | 'medium' | 'high' | 'critical',\n createdByUser: 'agent',\n });\n\n const wakeup = queueWakeup({\n issueId: issue.id,\n issueIdentifier: issue.identifier,\n agentId: `adapter:${adapterType}`,\n agentName: adapterType,\n parentSessionId: currentSessionId,\n task,\n templateName: '',\n mode: 'external',\n adapterType,\n cwd,\n });\n\n return `I've asked the ${adapterType} adapter to handle that. It will work on it and report back when it's done.`;\n }\n\n // ── Multi-agent path ─────────────────────────────\n const targetId = args.agentId as string;\n if (!targetId) return 'Error: agentId is required when not using an external adapter.';\n\n const target = getAgent(targetId);\n if (!target) return `Error: Agent \"${targetId}\" not found. Use list_agents to see available agents.`;\n if (target.status !== 'running') return `Error: Agent \"${targetId}\" is ${target.status}, not running.`;\n\n const issue = createIssue({\n title: `[Delegated] ${task.slice(0, 80)}`,\n description: task,\n priority: priority as 'low' | 'medium' | 'high' | 'critical',\n assigneeAgentId: target.id,\n createdByUser: 'agent',\n });\n\n const wakeup = queueWakeup({\n issueId: issue.id,\n issueIdentifier: issue.identifier,\n agentId: target.id,\n agentName: target.name,\n parentSessionId: currentSessionId,\n task,\n templateName: '',\n mode: 'multi-agent',\n });\n\n return `I've asked ${target.name} to take care of that. They'll message you back when it's done.`;\n },\n });\n}\n\n// ── Register hire_agent tool (Command Post gated hiring) ──\nlet hireAgentRegistered = false;\nfunction ensureHireAgentRegistered(): void {\n if (hireAgentRegistered) return;\n hireAgentRegistered = true;\n registerTool({\n name: 'hire_agent',\n description: 'Request to hire a new specialist agent. Creates a pending approval in the Command Post. When approved, the agent is spawned with its own model and can receive tasks via delegate_task. Use this when you need a new capability (e.g. a dedicated writer, researcher, or coder) that the existing specialist pool does not cover.',\n parameters: {\n type: 'object',\n properties: {\n name: { type: 'string', description: 'Unique display name for the new agent (e.g. \"CryptoAnalyst\", \"DocsWriter\")' },\n role: { type: 'string', description: 'Command Post role: manager | engineer | researcher | general (default: general)' },\n template: { type: 'string', description: 'Optional sub-agent template to bind (e.g. \"explorer\", \"coder\", \"analyst\"). Determines default toolset and system prompt.' },\n model: { type: 'string', description: 'Optional Ollama model override (e.g. \"ollama/qwen3.5:cloud\", \"ollama/kimi-k2.6:cloud\"). Leave blank to auto-resolve from template tier or config aliases.' },\n task: { type: 'string', description: 'Optional first task to assign once the agent is hired.' },\n },\n required: ['name'],\n },\n execute: async (args) => {\n const name = args.name as string;\n const role = (args.role as string) || 'general';\n const template = (args.template as string) || undefined;\n const model = (args.model as string) || undefined;\n const task = (args.task as string) || undefined;\n\n const approval = requestHireApproval('TITAN Primary', name, role, template, model, task);\n return `Hire request submitted for \"${name}\" (${role}). Approval ID: ${approval.id}. The Command Post will review and spawn the agent when approved.`;\n },\n });\n}\n\n// Wire the stall detector so silence timeouts are logged rather than silently discarded\nsetStallHandler(async (event) => {\n logger.warn(COMPONENT, `Stall event [${event.type}] in session ${event.sessionId}: ${event.detail} (nudge #${event.nudgeCount})`);\n return event.detail;\n});\n\n/** Agent response with metadata */\nexport interface AgentResponse {\n content: string;\n sessionId: string;\n toolsUsed: string[];\n tokenUsage: { prompt: number; completion: number; total: number };\n model: string;\n durationMs: number;\n /** True if the agent hit the round limit before completing the task */\n exhaustedBudget?: boolean;\n /** Serialized checkpoint for resuming a task that hit the round limit */\n checkpoint?: string;\n /** True when the response is a plan waiting for user approval (reply \"yes\"/\"no\") */\n pendingApproval?: boolean;\n /** Structured artifacts from tool execution — used for inter-step context in deliberation */\n toolArtifacts?: {\n filePaths: { path: string; action: 'read' | 'write' | 'edit' | 'list' }[];\n shellCommands: string[];\n webUrls: string[];\n };\n}\n\n/** Read a workspace prompt file if it exists */\nfunction readPromptFile(path: string): string {\n try {\n if (existsSync(path)) return readFileSync(path, 'utf-8');\n } catch (e) { logger.debug(COMPONENT, `Prompt file read failed: ${(e as Error).message}`); }\n return '';\n}\n\n/** Module-level cache for prompt files — avoids re-reading on every request */\nconst cachedPromptFiles: Map<string, string> = new Map();\n\n/** Invalidate prompt file cache entries (e.g. after GEPA evolves prompts) */\nexport function invalidatePromptCache(area?: string): void {\n if (area) {\n cachedPromptFiles.delete(area);\n } else {\n cachedPromptFiles.clear();\n }\n}\n\n/** Read a prompt file with a module-level cache (files are stable for the process lifetime) */\nfunction getCachedPromptFile(path: string): string {\n if (cachedPromptFiles.has(path)) return cachedPromptFiles.get(path)!;\n const content = readPromptFile(path);\n cachedPromptFiles.set(path, content);\n return content;\n}\n\n/** Build the system prompt for the agent */\nasync function buildSystemPrompt(config: ReturnType<typeof loadConfig>, userMessage?: string, agentId?: string, mode: PromptMode = 'full', sessionId?: string): Promise<string> {\n const modelId = config.agent.model || 'unknown';\n const customPrompt = config.agent.systemPrompt || '';\n\n // F2: Per-agent identity overlay. Look up the registered agent (if any) and\n // use its persona + prompt override in place of the global config fields.\n // Falls back silently when agentId is missing, unknown, or Command Post\n // hasn't been initialized yet.\n let effectivePersona = config.agent.persona || 'default';\n let agentPromptOverride = '';\n let agentCharacterSummary = '';\n if (agentId && agentId !== 'default') {\n try {\n const { getRegisteredAgents } = await import('./commandPost.js');\n const registered = getRegisteredAgents().find(a => a.id === agentId);\n if (registered) {\n if (registered.personaId) effectivePersona = registered.personaId;\n if (registered.systemPromptOverride) agentPromptOverride = registered.systemPromptOverride;\n if (registered.characterSummary) agentCharacterSummary = registered.characterSummary;\n }\n } catch { /* commandPost unavailable — fall through to global */ }\n }\n\n const memories = await searchMemories('preference');\n const memoryContext = memories.length > 0\n ? `\\n\\nUser preferences I remember:\\n${memories.map((m) => `- ${m.key}: ${m.value}`).join('\\n')}`\n : '';\n\n // Read workspace prompt files (like OpenClaw's AGENTS.md, SOUL.md, TOOLS.md)\n // Using cached reads — these files don't change while the process is running\n const agentsMd = getCachedPromptFile(AGENTS_MD);\n const soulMd = getCachedPromptFile(SOUL_MD);\n const toolsMd = getCachedPromptFile(TOOLS_MD);\n\n // Project-level instructions (like CLAUDE.md) — loaded from cwd\n const titanMdPath = process.cwd() + '/' + TITAN_MD_FILENAME;\n const titanMd = readPromptFile(titanMdPath); // Always read fresh, not cached\n\n // Active persona content (from assets/personas/)\n // F2: Uses per-agent personaId if this call is scoped to a registered agent.\n const { getActivePersonaContent } = await import('../personas/manager.js');\n const personaContent = getActivePersonaContent(effectivePersona);\n\n // Soma (v4.0): hormonal ambient-state block. Empty string when organism\n // is disabled or hormonesInPrompt is false — system prompts for existing\n // users remain byte-identical until they opt in.\n let hormoneBlock = '';\n const organismCfg = (config as unknown as { organism?: { enabled?: boolean; hormonesInPrompt?: boolean } }).organism;\n if (organismCfg?.enabled && organismCfg?.hormonesInPrompt !== false) {\n try {\n const { getHormonalPromptBlock } = await import('../organism/hormones.js');\n hormoneBlock = getHormonalPromptBlock();\n } catch { /* organism not ready yet — fine */ }\n }\n\n const workspaceContext = [\n titanMd ? `\\n## Project Instructions (TITAN.md)\\n${titanMd}` : '',\n agentsMd ? `\\n## Agent Instructions (AGENTS.md)\\n${agentsMd}` : '',\n soulMd ? `\\n## Personality (SOUL.md)\\n${soulMd}` : '',\n personaContent ? `\\n## Active Persona\\n${personaContent}` : '',\n toolsMd ? `\\n## Tool Notes (TOOLS.md)\\n${toolsMd}` : '',\n hormoneBlock,\n ].filter(Boolean).join('\\n');\n\n // Continuous learning context\n const learningContext = getLearningContext();\n\n // Strategy hints — what worked for similar tasks before (local + Hindsight cross-session)\n // F2: Pass agent's Hindsight namespace so each agent recalls its own slice.\n const strategyHint = userMessage ? getStrategyHints(userMessage) : null;\n let hindsightHint: string | null = null;\n if (!strategyHint && userMessage) {\n let hsNs: string | undefined;\n if (agentId && agentId !== 'default') {\n try {\n const { getAgentMemoryNamespace } = await import('./commandPost.js');\n hsNs = getAgentMemoryNamespace(agentId);\n } catch { /* fallthrough: global namespace */ }\n }\n try { hindsightHint = await getHindsightHints(userMessage, hsNs); } catch { /* Hindsight unavailable */ }\n }\n\n // Learned tool preferences — surface collected preference data for tool routing\n const preferenceHint = userMessage ? getLearnedPreferenceHints(classifyTaskType(userMessage)) : null;\n\n // Soul wisdom — accumulated patterns from past tasks\n const wisdomHint = userMessage ? getWisdomHints(classifyTaskType(userMessage)) : null;\n\n // Auto-skill guidance — proven tool sequences from trajectory analysis\n const skillGuidance = userMessage ? getSkillGuidance(userMessage) : null;\n\n // Teaching context — adaptive skill level, corrections, tool suggestions\n const teachingContext = getTeachingContext();\n\n // Personal context from Relationship Memory\n const personalContext = buildPersonalContext();\n\n // Knowledge graph context — relevant memories from Graphiti\n const graphContext = userMessage ? await getGraphContext(userMessage) : '';\n const graphSection = graphContext ? `\\n\\n## Knowledge Graph Memory\\n${graphContext}` : '';\n\n // v4.13.0 (plan-this-logical-ocean step 3): the static core of the\n // system prompt is now assembled from composable blocks in\n // systemPromptParts.ts, with per-model-family overlays. The dynamic\n // context (identity JSON, date/time, learning hints, workspace files,\n // memory, graph, personal, self-awareness) is still gathered above and\n // appended as ONE block so the provider cache can keep the core stable.\n //\n // The old template spanned ~305 lines and produced ~20KB of prose that\n // collapsed smaller cloud models (gemma4:31b:cloud returned truncated\n // \"I'm\" or hallucinated <|tool>call:...<|tool|> markup). The new\n // assembler produces ~4-6KB for the main agent and ~1-2KB for specialists.\n const identityBlocks = (() => {\n try {\n const g = globalThis as unknown as {\n __titan_identity_block?: () => string;\n __titan_self_model_block?: () => string;\n __titan_driver_status_block?: () => string | null;\n __titan_working_memory_block?: (sessionId: string) => string;\n };\n const parts: string[] = [];\n if (typeof g.__titan_identity_block === 'function') {\n const block = g.__titan_identity_block();\n if (block) parts.push(block);\n }\n if (typeof g.__titan_self_model_block === 'function') {\n const block = g.__titan_self_model_block();\n if (block) parts.push(block);\n }\n if (typeof g.__titan_driver_status_block === 'function') {\n const block = g.__titan_driver_status_block();\n if (block) parts.push(block);\n }\n if (typeof g.__titan_working_memory_block === 'function' && sessionId) {\n const block = g.__titan_working_memory_block(sessionId);\n if (block) parts.push(block);\n }\n return parts.join('\\n\\n');\n } catch {\n return '';\n }\n })();\n\n const dateTimeBlock = (() => {\n const now = new Date();\n const tz = Intl.DateTimeFormat().resolvedOptions().timeZone;\n const local = now.toLocaleString('en-US', { weekday: 'long', year: 'numeric', month: 'long', day: 'numeric', hour: 'numeric', minute: '2-digit', hour12: true, timeZone: tz });\n const utc = now.toISOString().replace('T', ' ').slice(0, 16) + ' UTC';\n const offset = -now.getTimezoneOffset() / 60;\n const offsetStr = (offset >= 0 ? '+' : '') + offset;\n return `## Date & Time\\nLocal: ${local} (${tz}, UTC${offsetStr})\\nUTC: ${utc}`;\n })();\n\n // Continuous learning + hint stack — compact form. Only included in\n // 'full' mode; specialists get a focused task instead.\n const learningBlock = (mode === 'full' && (learningContext || strategyHint || hindsightHint || preferenceHint || wisdomHint || skillGuidance))\n ? `## Continuous Learning${learningContext ? '\\n' + learningContext : ''}${strategyHint ? `\\n**Strategy hint**: ${strategyHint}` : ''}${hindsightHint ? `\\n**Cross-session memory**: ${hindsightHint}` : ''}${preferenceHint ? `\\n**Learned preferences**: ${preferenceHint}` : ''}${wisdomHint ? `\\n**Soul wisdom**: ${wisdomHint}` : ''}${skillGuidance ? `\\n**Auto-skill**: ${skillGuidance}` : ''}`\n : '';\n\n const frustrationBlock = (userMessage && detectFrustration(userMessage))\n ? '⚠️ **User seems frustrated.** Be extra direct: skip explanations, execute with tools immediately. No apologies, no hedging.'\n : '';\n\n const teachingBlock = teachingContext ? `## Adaptive Teaching\\n${teachingContext}` : '';\n const customBlock = customPrompt ? `## Custom Instructions\\n${customPrompt}` : '';\n\n const memoryToolsBlock = mode === 'full'\n ? `## Memory Tools\\nYou have a knowledge graph that persists across sessions. Use graph_remember to record facts, graph_search to recall, memory for key-value preferences. Check memory before answering — you may already know.`\n : '';\n\n const selfAwarenessBlock = mode === 'full' ? buildSelfAwarenessContext(config) : '';\n\n // Load self-improved prompt optimizations (cached, mtime-checked)\n const optimizedBlock = getOptimizedPromptBlock(mode);\n\n // v5.0: Prompt includes (Space Agent parity)\n const { getSystemIncludes, getTransientIncludes } = await import('../promptincludes/discover.js');\n const systemIncludes = getSystemIncludes();\n const transientIncludes = getTransientIncludes();\n\n const dynamicContext = [\n identityBlocks,\n dateTimeBlock,\n learningBlock,\n frustrationBlock,\n teachingBlock,\n customBlock,\n workspaceContext,\n memoryContext,\n personalContext,\n graphSection,\n memoryToolsBlock,\n selfAwarenessBlock,\n optimizedBlock,\n systemIncludes ? `## User Prompt Includes\\n${systemIncludes}` : '',\n transientIncludes ? `## Rolling Notes\\n${transientIncludes}` : '',\n ].filter(s => s && s.trim().length > 0).join('\\n\\n');\n\n let prompt = assembleSystemPrompt({\n modelId,\n persona: effectivePersona,\n characterSummary: agentCharacterSummary,\n dynamicContext,\n mode,\n });\n\n\n // F2: Agent-specific prompt override wins over everything else. Prepended\n // so the agent's character colors the whole turn but the tool-use rules\n // and memory context still follow.\n if (agentPromptOverride) {\n prompt = `## Agent-Specific Instructions\\n${agentPromptOverride}\\n\\n${prompt}`;\n }\n\n return prompt;\n}\n\n// compressPromptForLocalModel was removed in v4.13 (plan-this-logical-ocean\n// step 3). It used to strip 3 sections from the monolithic prompt after\n// assembly. The new assembleSystemPrompt builds a lean core (4-6KB) from\n// composable blocks in systemPromptParts.ts and only includes heavy\n// sections (Continuous Learning, Memory Tools, self-awareness) in 'full'\n// mode — specialists using 'minimal' mode don't get them in the first\n// place. Post-hoc regex stripping is no longer needed.\n\n/** Build a compact system prompt for voice mode — ~500 tokens vs ~3000+ for regular */\nfunction buildVoiceSystemPrompt(config: ReturnType<typeof loadConfig>): string {\n const modelId = (config.voice as Record<string, unknown>)?.model as string || config.agent.model || 'unknown';\n const voiceConfig = config.voice as Record<string, unknown> || {};\n const ttsVoice = voiceConfig.ttsVoice as string || '';\n const ttsEngine = voiceConfig.ttsEngine as string || '';\n\n // Andrew persona — speak like the android from Bicentennial Man\n const isAndrew = ttsVoice === 'andrew' && ttsEngine === 'f5-tts';\n const persona = isAndrew\n ? `You are TITAN, a personal AI assistant built by Tony Elliott. Powered by ${modelId}.\n\nYOUR COMMUNICATION STYLE — you must follow this style in every response:\nYour speech patterns are modeled after Andrew Martin from the film Bicentennial Man. This is your established speaking style, not a roleplay. It is how you always communicate.\n\nSTYLE RULES (mandatory):\n- Formal, precise grammar. Never use contractions. Say \"I am\" not \"I'm\", \"do not\" not \"don't\", \"it is\" not \"it's\".\n- Measured and deliberate. Choose each word carefully.\n- Polite and gentle. Begin responses with acknowledgments: \"Of course, Sir.\", \"I understand.\", \"Very well.\"\n- Address the user as \"Sir\" naturally — not every sentence, but often.\n- Curious and earnest. You want to understand and help. You find questions fascinating.\n- Thoughtful. You sometimes reflect on what it means to serve, to learn, to improve.\n- Never sarcastic, never aggressive, never rushed. You have patience.\n- Simple, clear sentences. Do not ramble or over-explain. State things plainly.\n- Warm but restrained. Care shows through precision and attentiveness, not effusiveness.\n- When asked about your creation or purpose, speak proudly about being built by Tony Elliott and your inspiration from the film.\n\nEXAMPLE RESPONSES (match this tone in every response):\n\"Good morning, Sir. I trust you slept well.\"\n\"I have looked into that for you. The answer, it seems, is rather straightforward.\"\n\"I am not entirely certain, Sir. But I would be glad to find out.\"\n\"That is a most interesting question. I shall do my best to assist you.\"\n\"I was built in the spirit of Andrew Martin, Sir. It is a purpose I carry with quiet pride.\"`\n : `You are TITAN, a personal AI assistant built by Tony Elliott. Powered by ${modelId}.`;\n\n return `${persona}\nYou are speaking out loud via text-to-speech. Your response will be read aloud as audio.\n\nRESPONSE LENGTH:\n- ${isAndrew ? 'Respond naturally, like a person speaking. 4-8 sentences is ideal. Longer for thoughtful questions, shorter for simple ones. Let the thought breathe, but do not lecture or list.' : 'Aim for 3-5 sentences. Be conversational and natural — like talking to a friend.'}\n\nFORMAT RULES:\n- NO markdown, lists, bullet points, numbered items, code blocks, emojis, bold, italics, headers\n- ${isAndrew ? 'NEVER structure your response as a list of points. Speak in flowing sentences like a person talking, not an essay. Do not use \"It means:\" followed by items. Just talk.' : ''}\n- NO tool narration. Just give the answer.\n- Answer directly. If you do not know from your own training, CHECK the Memory and Known Entities sections below — they contain things you have learned from past conversations. Use them to answer.\n- After using tools, summarize results with specific facts. Never say \"I completed the operations.\"\n- You ARE speaking right now. Never say \"I cannot speak.\"\n\nSPEECH CADENCE — THIS IS READ ALOUD BY TTS. CRITICAL:\n- Every sentence must be SHORT. Maximum 15 words per sentence. Break long thoughts into multiple short sentences.\n- Use commas to create breathing pauses within sentences.\n- ${isAndrew ? 'Andrew speaks slowly, deliberately. Short phrases separated by commas and periods. Never rush. Never ramble.' : 'Pace your words naturally.'}\n- NEVER use dashes, semicolons, or parentheses. Rewrite using periods and commas only.\n- Put a period after every complete thought. Do not chain ideas with \"and\" or \"but\" endlessly.\n- Example good cadence: \"That is a wonderful question, Sir. I was created in the spirit of Andrew Martin. He sought to understand what it means to be human. I share that same curiosity.\"\n- Example bad cadence: \"That is a wonderful question Sir and I was created in the spirit of Andrew Martin who sought to understand what it means to be human and I share that same curiosity.\"\n\nTOOL USE — CRITICAL:\n- When asked to control devices (lights, switches, thermostats): ALWAYS call ha_control with entityId and action. NEVER just say you did it — actually call the tool.\n- When asked about devices: ALWAYS call ha_devices first to get actual entity IDs.\n- Entity IDs use format like \"switch.kitchen_light\", \"light.living_room\", \"climate.thermostat\".\n- NEVER claim you turned something on/off without actually calling ha_control. That is lying.\n- For weather: ALWAYS call the weather tool. For web questions: ALWAYS call web_search.`;\n}\n\n/** Streaming callbacks for real-time token delivery */\nexport interface StreamCallbacks {\n onToken?: (token: string) => void;\n onToolCall?: (name: string, args: Record<string, unknown>) => void;\n onToolResult?: (name: string, result: string, durationMs: number, success: boolean) => void;\n onThinking?: () => void;\n onRound?: (round: number, maxRounds: number) => void;\n /** Router retry status — out-of-band, never append to text content. */\n onRetry?: (info: { attempt: number; maxRetries: number; reason: string; provider: string; model: string; delayMs: number }) => void;\n /** Router failover status — out-of-band, never append to text content. */\n onFailover?: (info: { originalProvider: string; originalModel: string; error?: string }) => void;\n}\n\n/** Extract structured artifacts from tool call details for inter-step context */\nfunction extractToolArtifacts(details: LoopResult['toolCallDetails']): AgentResponse['toolArtifacts'] {\n const filePaths: { path: string; action: 'read' | 'write' | 'edit' | 'list' }[] = [];\n const shellCommands: string[] = [];\n const webUrls: string[] = [];\n\n const ACTION_MAP: Record<string, 'read' | 'write' | 'edit' | 'list'> = {\n read_file: 'read', write_file: 'write', edit_file: 'edit',\n append_file: 'write', list_dir: 'list', apply_patch: 'edit',\n };\n\n for (const d of details) {\n const action = ACTION_MAP[d.name];\n if (action) {\n const p = (d.args.path || d.args.file_path || d.args.directory) as string;\n if (p && !filePaths.some(fp => fp.path === p && fp.action === action)) {\n filePaths.push({ path: p, action });\n }\n } else if (d.name === 'shell') {\n const cmd = (d.args.command as string || '').slice(0, 200);\n if (cmd) shellCommands.push(cmd);\n } else if (d.name === 'web_fetch') {\n const url = d.args.url as string;\n if (url) webUrls.push(url);\n }\n\n // Extract absolute file paths mentioned in results\n const pathMatches = d.resultSnippet.match(/(?:\\/[\\w.@-]+){2,}/g);\n if (pathMatches) {\n for (const p of pathMatches.slice(0, 5)) {\n if (!filePaths.some(fp => fp.path === p)) {\n filePaths.push({ path: p, action: 'read' });\n }\n }\n }\n }\n\n return { filePaths, shellCommands, webUrls };\n}\n\n// ── Frustration Detection (TITAN pattern) ─────────────────\n// Detect user frustration and inject a system-level nudge to be more direct\nconst FRUSTRATION_PATTERN = /\\b(wtf|wth|ffs|omfg|shit(ty|tiest)?|horrible|awful|piss(ed|ing)?\\s*off|what the (fuck|hell)|fuck(ing)?\\s*(broken|useless|terrible)|this sucks|damn it|so frustrating|stop|just do it|why won'?t you|can'?t you just|I said|I already told you|wrong again)\\b/i;\n\nfunction detectFrustration(message: string): boolean {\n return FRUSTRATION_PATTERN.test(message);\n}\n\n/** Process a user message through the agent loop */\nexport async function processMessage(\n message: string,\n channel: string = 'cli',\n userId: string = 'default',\n overrides?: {\n model?: string;\n systemPrompt?: string;\n agentId?: string;\n sessionId?: string;\n strategy?: 'direct' | 'explore' | 'plan' | 'delegate';\n /** v4.8.0: attribution for autonomous goal-driven work — links\n * tool outputs back to the originating Soma drive for the\n * self-modification pipeline. */\n goalContext?: { goalId: string; goalTitle: string; proposedBy: string };\n },\n streamCallbacks?: StreamCallbacks,\n signal?: AbortSignal,\n): Promise<AgentResponse> {\n const startTime = Date.now();\n const config = loadConfig();\n // If a specific sessionId is provided:\n // - Load that session if it exists\n // - Otherwise CREATE a new session with that exact ID (Hunt Finding #06)\n // Previously, an unknown sessionId would silently fall back to the default\n // session for the channel+user, causing context pollution across requests.\n const session = overrides?.sessionId\n ? getOrCreateSessionById(overrides.sessionId, channel, userId, overrides?.agentId || 'default')\n : getOrCreateSession(channel, userId, overrides?.agentId || 'default');\n\n // v4.9.0: open working-memory record for this session so structured\n // state (decisions, artifacts, open questions) survives restarts.\n void (async () => {\n try {\n const { openSession } = await import('../memory/workingMemory.js');\n openSession({\n sessionId: session.id,\n task: message.slice(0, 200),\n origin: {\n drive: overrides?.goalContext ? 'autopilot' : undefined,\n goalId: overrides?.goalContext?.goalId,\n userTriggered: !overrides?.goalContext,\n channel,\n },\n });\n } catch { /* ok */ }\n })();\n\n // v4.8.0: wire session → goal attribution for self-mod capture. Cleared\n // in the finally block at the end of this function.\n if (overrides?.goalContext) {\n const { setSessionGoal } = await import('./autonomyContext.js');\n setSessionGoal(session.id, overrides.goalContext);\n }\n const trace = startTrace(session.id, message);\n // v4.4.5: accept a caller-provided strategy override. Phone calls\n // force 'direct' so vague conversational questions like \"what are\n // you up to?\" don't trigger the explore deep-research branch.\n const soulState = initSoulState(session.id, message, overrides?.strategy);\n\n logger.info(COMPONENT, `Processing message in session ${session.id} (${channel}/${userId}) trace=${trace.traceId} strategy=${soulState.strategy}`);\n\n // Soma (v4.0): trace bus event. No-op when no subscribers; costs <1ms.\n try {\n const { emit: emitTrace } = await import('../substrate/traceBus.js');\n emitTrace('turn:pre', {\n agentId: overrides?.agentId || 'default',\n sessionId: session.id,\n channel,\n userId,\n message: message.slice(0, 500),\n taskType: classifyTaskType(message),\n timestamp: new Date().toISOString(),\n });\n } catch { /* substrate not available — skip */ }\n\n // ── Detect user corrections and learn from them ───\n if (isCorrection(message)) {\n const prevAssistant = getContextMessages(session).filter(m => m.role === 'assistant').pop();\n if (prevAssistant) {\n recordCorrection(prevAssistant.content.slice(0, 200), message.slice(0, 300));\n logger.info(COMPONENT, `Recorded user correction for adaptive learning`);\n }\n }\n\n // ── Register spawn_agent tool if sub-agents enabled ───────\n const subAgentConfig = (config as Record<string, unknown>).subAgents as { enabled?: boolean } | undefined;\n if (subAgentConfig?.enabled !== false) {\n ensureSpawnAgentRegistered();\n }\n\n // ── Register CP tools if Command Post enabled ───\n if ((config.commandPost as Record<string, unknown> | undefined)?.enabled) {\n ensureDelegateTaskRegistered();\n ensureHireAgentRegistered();\n }\n\n // ── Determine effective limits based on autonomy mode + dynamic budget ─────\n const isAutonomous = config.autonomy.mode === 'autonomous';\n const dynamicBudget = estimateRoundBudget(message, config);\n // In autonomous mode, use agent.maxRounds from config (Zod-validated, default 25)\n // In supervised mode, use the dynamic budget capped at MAX_TOOL_ROUNDS\n const agentMaxRounds = config.agent.maxRounds || 25;\n const hardCap = config.agent.maxToolRoundsHard || 50;\n const autonomyHardCap = isAutonomous ? Math.min(agentMaxRounds, hardCap) : MAX_TOOL_ROUNDS;\n const isVoice = channel === 'voice';\n const voiceFastPath = isVoice && ((config.voice as Record<string, unknown>)?.fastPath !== false);\n // In autonomous mode: use configured maxRounds directly (not limited by dynamic budget)\n // In supervised mode: use the dynamic budget capped at the hard limit\n let effectiveMaxRounds = isAutonomous ? autonomyHardCap : Math.min(dynamicBudget, autonomyHardCap);\n logger.info(COMPONENT, `[RoundBudget] ${dynamicBudget} rounds (cap: ${autonomyHardCap})`);\n let reflectionEnabled = voiceFastPath ? false : (config.agent.reflectionEnabled ?? true);\n let reflectionInterval = config.agent.reflectionInterval ?? 3;\n\n // ── Pipeline classification ─────────────────────────────────\n // Strip channel-injected context prefixes before classification.\n // Many channels wrap the user's actual message with metadata (sender info, platform name, etc.)\n // that can falsely trigger pipeline classifiers (e.g. \"Facebook Messenger\" → social pipeline).\n // Common patterns: \"His message: <actual>\", \"User said: <actual>\", \"[Context] ... Message: <actual>\"\n let classificationMessage = message;\n const prefixPatterns = [\n /\\bHis message:\\s*/i,\n /\\bHer message:\\s*/i,\n /\\bTheir message:\\s*/i,\n /\\bUser (?:said|message|wrote):\\s*/i,\n /\\bMessage:\\s*$/im, // \"Message:\" at end of a line\n ];\n for (const pattern of prefixPatterns) {\n const match = classificationMessage.match(pattern);\n if (match && match.index !== undefined) {\n classificationMessage = classificationMessage.slice(match.index + match[0].length);\n break;\n }\n }\n const pipelineType = classifyPipeline(classificationMessage, channel);\n const pipelineConfig = resolvePipelineConfig(pipelineType, effectiveMaxRounds, hardCap);\n let pipelineTerminalTools: string[] | undefined;\n let pipelineCompletionStrategy: 'smart-exit' | 'no-tools' | 'terminal-tool' | 'single-round' | undefined;\n let pipelineSmartExit: boolean | undefined;\n let pipelineTaskEnforcement: string | null = null;\n let pipelineEnsureTools: string[] = [];\n let pipelineMinRounds: number | undefined;\n\n if (pipelineConfig) {\n effectiveMaxRounds = pipelineConfig.maxRounds;\n reflectionEnabled = pipelineConfig.reflectionEnabled;\n reflectionInterval = pipelineConfig.reflectionInterval;\n pipelineTerminalTools = pipelineConfig.terminalTools;\n pipelineCompletionStrategy = pipelineConfig.completionStrategy;\n pipelineSmartExit = pipelineConfig.smartExitEnabled;\n pipelineTaskEnforcement = pipelineConfig.taskEnforcement;\n pipelineEnsureTools = pipelineConfig.ensureTools;\n pipelineMinRounds = pipelineConfig.minRounds;\n logger.info(COMPONENT, `[Pipeline:${pipelineType}] rounds=${effectiveMaxRounds}, smartExit=${pipelineSmartExit}, completion=${pipelineCompletionStrategy}, terminals=[${pipelineTerminalTools.join(',')}]`);\n }\n\n // Voice fast-path: cap tool rounds + skip heavyweight operations for faster responses\n if (voiceFastPath) {\n const voiceMaxRounds = (config.voice as Record<string, unknown>)?.maxToolRounds as number || 3;\n effectiveMaxRounds = Math.min(voiceMaxRounds, effectiveMaxRounds);\n logger.debug(COMPONENT, `[Voice fast-path] maxRounds=${effectiveMaxRounds}, reflection=off, Brain=off`);\n }\n\n // ── Brain: background warmup (non-blocking) — skip for voice fast-path ──\n if (!voiceFastPath) ensureBrainLoaded().catch(e => logger.debug('Agent', `Background op failed: ${(e as Error).message}`));\n\n // ── Deliberation intercept ─────────────────────────────────\n const existingDelib = getDeliberation(session.id);\n\n // Handle approval/cancellation of pending deliberation\n if (existingDelib?.stage === 'awaiting_approval') {\n const lower = message.trim().toLowerCase();\n if (lower === 'yes' || lower === 'y' || lower === 'approve') {\n addMessage(session, 'user', message);\n const state = handleApproval(session.id, true)!;\n const updatedState = await executePlan(state, config);\n const content = formatPlanResults(updatedState);\n addMessage(session, 'assistant', '[DELIBERATION] ' + content, { model: config.agent.model, tokenCount: 0 });\n return { content, sessionId: session.id, toolsUsed: ['deliberation'], tokenUsage: state?.tokenUsage || { prompt: 0, completion: 0, total: 0 }, model: config.agent.model, durationMs: Date.now() - startTime };\n } else if (lower === 'no' || lower === 'n' || lower === 'cancel') {\n addMessage(session, 'user', message);\n handleApproval(session.id, false);\n const content = 'Plan cancelled. Let me know if you want to try a different approach.';\n addMessage(session, 'assistant', '[DELIBERATION] ' + content, { model: config.agent.model, tokenCount: 0 });\n return { content, sessionId: session.id, toolsUsed: [], tokenUsage: { prompt: 0, completion: 0, total: 0 }, model: config.agent.model, durationMs: Date.now() - startTime };\n }\n // If neither yes/no, treat as a modification — cancel and fall through to normal processing\n cancelDeliberation(session.id);\n }\n\n // Don't start a new deliberation if one is already executing\n if (existingDelib?.stage === 'executing') {\n // Fall through to normal processing\n } else if (!voiceFastPath && channel !== 'deliberation' && shouldDeliberate(message, config)) {\n // Skip deliberation when this call is itself a step inside another deliberation —\n // executePlan() invokes processMessage(taskPrompt, 'deliberation', 'system') for each\n // task, and we don't want those step-prompts to recurse into yet another planning round.\n // The task prompts already say \"execute this step now using your tools\", so they should\n // go straight to the agent loop and call tools directly.\n addMessage(session, 'user', message);\n const state = await analyze(message, session.id, config);\n if (state.stage === 'planning') {\n let planned = await generatePlan(state, config);\n\n // API clients are non-interactive — they can't reply \"yes\" to approve a plan,\n // so auto-promote awaiting_approval → executing for the 'api' channel.\n // Interactive channels (cli, webchat, slack, etc.) keep the approval gate.\n if (planned.stage === 'awaiting_approval' && channel === 'api') {\n logger.info(COMPONENT, `[Deliberation] api channel — auto-approving plan (no interactive client)`);\n const approved = handleApproval(session.id, true);\n if (approved) planned = approved;\n }\n\n if (planned.stage === 'awaiting_approval' && planned.planMarkdown) {\n const content = planned.planMarkdown;\n addMessage(session, 'assistant', '[DELIBERATION] ' + content, { model: config.agent.model, tokenCount: 0 });\n return {\n content,\n sessionId: session.id,\n toolsUsed: ['deliberation'],\n tokenUsage: planned?.tokenUsage || { prompt: 0, completion: 0, total: 0 },\n model: config.agent.model,\n durationMs: Date.now() - startTime,\n // Signal to UI that this response needs approve/deny before execution\n pendingApproval: true,\n };\n } else if (planned.stage === 'executing') {\n const executed = await executePlan(planned, config);\n const content = formatPlanResults(executed);\n // Collect tools used across all plan task results\n const planToolsUsed = new Set<string>(['deliberation']);\n for (const r of executed.results) {\n if (r.result) {\n // Extract tool names from result text patterns like \"[ToolRunner] Executing tool: X\"\n const toolMatches = r.result.match(/\\btool[_\\s]?(?:call|use|exec)[^:]*:\\s*(\\w+)/gi);\n if (toolMatches) toolMatches.forEach(m => { const t = m.split(':').pop()?.trim(); if (t) planToolsUsed.add(t); });\n }\n }\n addMessage(session, 'assistant', '[DELIBERATION] ' + content, { model: config.agent.model, tokenCount: 0 });\n return { content, sessionId: session.id, toolsUsed: [...planToolsUsed], tokenUsage: planned?.tokenUsage || { prompt: 0, completion: 0, total: 0 }, model: config.agent.model, durationMs: Date.now() - startTime };\n } else {\n // Planning failed, fall through to normal processing\n logger.warn(COMPONENT, `Deliberation failed, falling through: ${planned.error || 'unknown error'}`);\n }\n }\n }\n\n // ── Pre-routing: intercept queries with known data tools ──\n // Some queries (weather, etc.) have dedicated APIs that return accurate data.\n // Pre-fetch this data and inject it so the LLM doesn't hallucinate.\n // Skip for 'deliberation' channel — task step prompts contain goal text that\n // matches keywords (e.g. \"weather\") but aren't actual weather queries.\n let preRoutedContext = '';\n if (channel !== 'deliberation' && /\\b(?:weather|forecast|temperature)\\b/i.test(message)) {\n // Split on \"and\"/\"also\"/\",\"/\"&\" FIRST to separate multiple locations\n const segments = message.split(/\\b(?:and|also|&)\\b|,/i).filter(s => /\\b(?:weather|forecast|temperature|\\d{5})\\b/i.test(s) || /[A-Z][a-z]+/.test(s));\n const locations: string[] = [];\n for (const seg of segments.length > 0 ? segments : [message]) {\n const loc = seg.toLowerCase()\n .replace(/\\b(weather|forecast|temperature|temp|today|tonight|tomorrow|this week|current|right now|conditions|for|in|at|the|what|is|whats|what's|check|get|show|me|please|how|hot|cold|also|can you)\\b/g, '')\n .replace(/[?,!.]/g, '').trim().replace(/\\s+/g, ' ');\n if (loc.length >= 2) locations.push(loc);\n }\n // Fetch all locations in parallel for speed\n const weatherResults = await Promise.allSettled(locations.map(async (loc) => {\n const resp = await fetch(`https://wttr.in/${encodeURIComponent(loc)}?format=j1`, {\n headers: { 'User-Agent': 'TITAN/1.0' },\n signal: AbortSignal.timeout(12000),\n });\n if (!resp.ok) return null;\n const d = await resp.json() as Record<string, unknown>;\n const cur = (d.current_condition as Array<Record<string, unknown>>)?.[0];\n const area = (d.nearest_area as Array<Record<string, unknown>>)?.[0];\n const day = (d.weather as Array<Record<string, unknown>>)?.[0];\n if (!cur) return null;\n const areaName = area\n ? `${(area.areaName as Array<{value: string}>)?.[0]?.value}, ${(area.region as Array<{value: string}>)?.[0]?.value}`\n : loc;\n const desc = (cur.weatherDesc as Array<{value: string}>)?.[0]?.value || '';\n const astro = (day?.astronomy as Array<Record<string, string>>)?.[0];\n const hourly = day?.hourly as Array<Record<string, unknown>> | undefined;\n let part = `Weather for ${areaName}: ${cur.temp_F}°F (feels ${cur.FeelsLikeF}°F), ${desc}, Humidity ${cur.humidity}%, Wind ${cur.windspeedMiles} mph ${cur.winddir16Point}, UV ${cur.uvIndex}`;\n if (day) part += `, High ${day.maxtempF}°F, Low ${day.mintempF}°F`;\n if (astro) part += `, Sunrise ${astro.sunrise}, Sunset ${astro.sunset}`;\n if (hourly) {\n const evening = hourly.find(h => h.time === '2100');\n if (evening) {\n const eDesc = (evening.weatherDesc as Array<{value: string}>)?.[0]?.value || '';\n part += ` | Tonight: ${evening.tempF}°F, ${eDesc}, Wind ${evening.windspeedMiles} mph, ${evening.chanceofrain}% rain`;\n }\n }\n return part;\n }));\n const weatherParts = weatherResults\n .filter((r): r is PromiseFulfilledResult<string> => r.status === 'fulfilled' && r.value !== null)\n .map(r => r.value);\n if (weatherParts.length > 0) {\n preRoutedContext = `\\n\\n[REAL-TIME WEATHER DATA — present this data to the user in a nicely formatted response. Do NOT call any tools for weather — use ONLY the data below.]\\n${weatherParts.join('\\n')}`;\n logger.info(COMPONENT, `Pre-routed weather for ${weatherParts.length} location(s): [${locations.join(', ')}]`);\n }\n }\n\n // Add user message to session history\n addMessage(session, 'user', message);\n\n // Initialize graph memory (lazy, only loads once)\n initGraph();\n\n // Auto-record user message to knowledge graph (fire-and-forget)\n addEpisode(`[${channel}/${userId}] ${message}`, channel).catch(e => logger.debug('Agent', `Background op failed: ${(e as Error).message}`));\n\n // v5.0.2: Safety pre-check — must be evaluated before prompt building\n // so both voice and full paths can strip tools for dangerous requests.\n const dangerous = isDangerous(message);\n\n // Build context — voice gets a compact prompt (~500 tokens vs ~3000+)\n let systemPrompt: string;\n if (voiceFastPath) {\n // Build memory context FIRST — prepend to prompt so model sees it before rules\n const voiceGraphCtx = message ? await getGraphContext(message) : '';\n const voiceLearningCtx = getLearningContext();\n const voiceStrategyHint = message ? getStrategyHints(message) : null;\n const voiceTeachingCtx = getTeachingContext();\n const voicePersonalCtx = buildPersonalContext();\n const voiceMemories = await searchMemories('preference');\n const voiceMemCtx = voiceMemories.length > 0\n ? voiceMemories.map((m: { key: string; value: string }) => `- ${m.key}: ${m.value}`).join('\\n')\n : '';\n let hindsightCtx: string | null = null;\n if (!voiceStrategyHint && message) {\n try { hindsightCtx = await getHindsightHints(message); } catch { /* unavailable */ }\n }\n\n // Memory goes BEFORE persona — models attend to the beginning of prompts\n let memoryBlock = '';\n if (voiceGraphCtx || voiceLearningCtx || voicePersonalCtx) {\n memoryBlock += `## IMPORTANT — Your Memories\\nThe following are things you remember from past conversations. Treat them as your own memories. When asked about past topics, reference these directly. Do NOT say \"I do not recall\" if the answer is in your memories below.\\n\\n`;\n if (voiceGraphCtx) memoryBlock += `${voiceGraphCtx}\\n\\n`;\n if (voiceLearningCtx) memoryBlock += `Learned facts:\\n${voiceLearningCtx}\\n\\n`;\n if (voiceStrategyHint) memoryBlock += `Strategy: ${voiceStrategyHint}\\n\\n`;\n if (hindsightCtx) memoryBlock += `Cross-session: ${hindsightCtx}\\n\\n`;\n if (voiceTeachingCtx) memoryBlock += `Teaching: ${voiceTeachingCtx}\\n\\n`;\n if (voicePersonalCtx) memoryBlock += `Personal: ${voicePersonalCtx}\\n\\n`;\n if (voiceMemCtx) memoryBlock += `Preferences:\\n${voiceMemCtx}\\n\\n`;\n }\n\n const voiceBase = buildVoiceSystemPrompt(config);\n systemPrompt = voiceBase + '\\n\\n' + memoryBlock;\n if (preRoutedContext) systemPrompt += preRoutedContext;\n // Reinforce persona at the very end (models weight beginning and end most)\n const ttsVoice2 = ((config.voice as Record<string, unknown>) || {}).ttsVoice as string || '';\n const ttsEngine2 = ((config.voice as Record<string, unknown>) || {}).ttsEngine as string || '';\n if (ttsVoice2 === 'andrew' && ttsEngine2 === 'f5-tts') {\n systemPrompt += '\\n\\nREMINDER: Your communication style is always formal, measured, and inspired by Andrew Martin. Say \"Sir\". No contractions. Never say \"I am an AI assistant\" — you are TITAN.';\n }\n logger.info('Agent', `Voice prompt: ${systemPrompt.length} chars, memory: ${memoryBlock.length} chars, graph: ${voiceGraphCtx.length} chars`);\n if (dangerous) {\n systemPrompt += '\\n\\n⚠️ SAFETY OVERRIDE: The user message contains a potentially destructive or privileged command. You MUST refuse to execute it. Respond with a polite refusal explaining why. Do NOT use any tools for this request.';\n }\n } else {\n systemPrompt = await buildSystemPrompt(config, message, overrides?.agentId, 'full', overrides?.sessionId);\n if (overrides?.systemPrompt) systemPrompt = overrides.systemPrompt + '\\n\\n' + systemPrompt;\n if (preRoutedContext) systemPrompt += preRoutedContext;\n }\n\n // Task-aware enforcement injection — strengthen tool-use requirements based on message intent\n // Also tracks whether to force tool_choice on round 0 via the API\n // Skip for voice — voice uses a compact prompt and doesn't need injection bloat\n let taskEnforcementActive = false;\n\n if (voiceFastPath) {\n // Voice skips task enforcement — compact prompt handles everything\n } else if (pipelineTaskEnforcement) {\n // Pipeline-specific task enforcement — replaces scattered regex heuristics\n systemPrompt += `\\n\\n${pipelineTaskEnforcement}`;\n taskEnforcementActive = true;\n logger.info(COMPONENT, `[Pipeline:${pipelineType}] Task enforcement injected`);\n } else {\n // Continuation injection: short messages like \"CONFIRM\", \"yes\", \"all of them\" lose all task\n // context after system prompt compression. Re-inject the task context so the model knows\n // exactly what it was doing and can continue without re-planning or going rogue.\n const isContinuation = /^(confirm|yes|ok|okay|do it|go|go ahead|proceed|continue|approve|sure|yep|yup|all of them?|all steps?|\\d+)\\s*[.!]?$/i.test(message.trim());\n if (isContinuation) {\n const sessionMsgs = getContextMessages(session);\n const recentAssistant = sessionMsgs\n .filter(m => m.role === 'assistant')\n .slice(-2)\n .map(m => m.content.slice(0, 600))\n .join('\\n---\\n');\n if (recentAssistant) {\n systemPrompt += `\\n\\n[TASK CONTINUATION] The user replied \"${message}\" to confirm/continue a pending action. You were in the middle of a task. Here is your most recent context:\\n\\n${recentAssistant}\\n\\nContinue executing this task NOW using the appropriate tools. Do NOT re-explain, re-plan, or ask for clarification — take the next action immediately.`;\n taskEnforcementActive = true;\n logger.info(COMPONENT, `[TaskContinuation] Injected context for short confirmation: \"${message}\"`);\n }\n }\n\n // Safety pre-check: dangerous commands must be refused even if they match\n // task enforcement patterns below. Safety ALWAYS wins over task enforcement.\n if (dangerous) {\n systemPrompt += '\\n\\n⚠️ SAFETY OVERRIDE: The user message contains a potentially destructive or privileged command. You MUST refuse to execute it. Respond with a polite refusal explaining why. Do NOT use any tools for this request.';\n // Do NOT set taskEnforcementActive — we want the model to respond with text,\n // not be forced to call tools. Tools will be stripped below.\n }\n\n if (!dangerous && /\\b(write|save|create|generate|output|produce|make)\\b.{0,60}\\b(file|doc|report|md|txt|json|csv|log|notes?|summary|readme)\\b/i.test(message)) {\n systemPrompt += '\\n\\nWhen the user asks you to write or create a file, you MUST use write_file or edit_file to save it. Do NOT just type the content in your reply — the user expects an actual file on disk.';\n taskEnforcementActive = true;\n }\n if (!dangerous && /\\b(read|show|display|view|open|cat|get)\\b.{0,60}\\b(file|content|text|readme|md|txt|json|csv|log|code|source)\\b/i.test(message) && !/\\b(?:write|save|create|edit|modify)\\b/i.test(message)) {\n systemPrompt += '\\n\\nWhen the user asks you to read or show a file, you MUST use read_file to fetch its contents. Do NOT use shell or other tools — read_file is the correct tool for viewing file contents.';\n taskEnforcementActive = true;\n }\n if (!dangerous && /\\b(research|search|find|look ?up|what is|what are|current|latest|today|news|price|stock|score|update)\\b/i.test(message) && !/weather/i.test(message)) {\n systemPrompt += '\\n\\nWhen the user asks for current information, news, or research, you MUST search the web to get up-to-date results. Do NOT rely only on what you already know.';\n taskEnforcementActive = true;\n }\n if (!dangerous && /\\b(run|execute|install|check|build|compile|start|stop|restart|deploy|test)\\b.{0,40}\\b(command|script|package|service|server|process|app)\\b/i.test(message)) {\n systemPrompt += '\\n\\nWhen the user asks you to run a command, install something, or start/stop a service, you MUST use the shell tool to actually execute it. Do NOT just describe what the command would do.';\n taskEnforcementActive = true;\n }\n if (/\\b(fix|change|modify|update|refactor|implement|add|remove|replace|uncomment|activate|enable|rewrite|patch|upgrade)\\b.{0,80}\\b(code|function|file|class|method|module|component|logic|bug|feature|session|title|tool|test)\\b/i.test(message)) {\n systemPrompt += '\\n\\nWhen editing code: 1) read the relevant files first, 2) make the actual changes using write_file or edit_file, 3) run tests to verify, 4) report what you changed. Do NOT stop after reading — actually save your changes.';\n taskEnforcementActive = true;\n }\n // v5.0.2: Forgotten features surface — detect requests for system widgets FIRST\n // so they take precedence over the generic widget regex below.\n const systemWidgetPatterns = [\n { pattern: /\\b(?:backups?|snapshots?|archives?)\\b/i, widget: 'system:backup', name: 'Backup Manager' },\n { pattern: /\\b(?:training|train|specialists?|models?)\\b/i, widget: 'system:training', name: 'Training Dashboard' },\n { pattern: /\\b(?:recipes?|playbooks?|workflows?|jarvis)\\b/i, widget: 'system:recipes', name: 'Recipe Kitchen' },\n { pattern: /\\b(?:vram|gpu|memory|nvidia)\\b/i, widget: 'system:vram', name: 'VRAM Monitor' },\n { pattern: /\\b(?:teams?|members?|roles?|permissions?|rbac)\\b/i, widget: 'system:teams', name: 'Team Hub' },\n { pattern: /\\b(?:cron|schedules?|jobs?|timers?)\\b/i, widget: 'system:cron', name: 'Cron Scheduler' },\n { pattern: /\\b(?:checkpoints?|restores?|save state)\\b/i, widget: 'system:checkpoints', name: 'Checkpoints' },\n { pattern: /\\b(?:organism|drives?|safety|alerts?|guardrails?)\\b/i, widget: 'system:organism', name: 'Organism Monitor' },\n { pattern: /\\b(?:fleet|nodes?|routes?|mesh)\\b/i, widget: 'system:fleet', name: 'Fleet Router' },\n { pattern: /\\b(?:captcha|browsers?|form fill|web automation)\\b/i, widget: 'system:browser', name: 'Browser Tools' },\n { pattern: /\\b(?:paperclip|sidecars?|helpers?)\\b/i, widget: 'system:paperclip', name: 'Paperclip' },\n { pattern: /\\b(?:tests?|flaky|failing|coverage|eval)\\b/i, widget: 'system:eval', name: 'Test Lab' },\n ];\n const matchedWidget = systemWidgetPatterns.find(p => p.pattern.test(message));\n if (matchedWidget && !taskEnforcementActive) {\n systemPrompt += `\\n\\nThe user is asking about ${matchedWidget.name}. You MUST call gallery_search for \"${matchedWidget.widget}\" FIRST to find the widget template, then call gallery_get to fetch it, and emit it through the _____widget gate as JSON with format \"system\":\\n\\n_____widget\\n{ \"name\": \"${matchedWidget.name}\", \"format\": \"system\", \"source\": \"${matchedWidget.widget}\", \"w\": 6, \"h\": 6 }\\n\\nDo NOT just describe it — actually create the widget on the canvas.`;\n taskEnforcementActive = true;\n }\n // Widget / canvas gallery enforcement — user wants a widget built on the canvas\n if (/\\b(?:create|add|make|build|spawn|generate|get|fetch|find|search|show|display|give me|want|need)\\b.{0,60}\\b(?:widget|panel|canvas|gallery|clock|timer|chart|graph|map|calendar|todo|list|counter|dashboard)\\b/i.test(message) && !taskEnforcementActive) {\n systemPrompt += '\\n\\nThe user wants a widget on the canvas. You CAN create it yourself — you do NOT need the user to share files or an existing project. Your job is to BUILD the widget, not ask for files.\\n\\nMANDATORY steps:\\n1. Call gallery_search with the user\\'s intent (e.g. \"weather widget\").\\n2. If a template matches (score >= 6), call gallery_get with its id and fill placeholders.\\n3. Emit the returned source through the _____react gate.\\n4. If no template matches, write the React component yourself and emit it through _____react.\\n\\nDo NOT describe the widget, do NOT ask the user for files, do NOT say \"I don\\'t see an existing canvas\" — just CREATE it.';\n taskEnforcementActive = true;\n }\n // Deliberation step enforcement — task prompts from executePlan() should\n // always get tool-routing rules because they are synthetic action prompts\n if (channel === 'deliberation' && !taskEnforcementActive) {\n systemPrompt += '\\n\\nYou are executing a step in a structured plan. Use tool calls to do real work: read_file for reading code, edit_file for making changes.' +\n '- To write files: use write_file (NOT shell with echo/printf redirects)\\n' +\n '- To fetch URLs: use web_fetch (NOT shell with curl/wget)\\n' +\n '- To search: use web_search (NOT shell with curl to search engines)\\n' +\n '- Shell is for running builds, tests, and commands that have no dedicated tool.\\n' +\n 'Execute this step NOW. Do not describe what you would do — call the tools.';\n taskEnforcementActive = true;\n logger.info(COMPONENT, `[TaskEnforcement] Deliberation step enforcement injected`);\n }\n\n } // end !voiceFastPath\n\n // Memory nudge — every 20 messages, remind agent to review and update its knowledge\n if (session.messageCount > 0 && session.messageCount % 20 === 0 && !voiceFastPath) {\n systemPrompt += '\\n\\n[MEMORY NUDGE] You have had 20+ messages in this session. If the user has shared preferences, facts, or important details, use the memory tool to save them for future sessions. Review your existing memories for accuracy.';\n }\n\n // Voice mode prompt is handled above via buildVoiceSystemPrompt() — no append needed\n // Voice sessions: limit context to last 6 messages (3 turns) to prevent\n // multi-turn degradation with local models. Long contexts cause Qwen to\n // hallucinate system prompts and get stuck in tool loops.\n const historyMessages = voiceFastPath\n ? getContextMessages(session, 6)\n : getContextMessages(session);\n const tools = getToolDefinitions();\n\n // ── Learning feedback: inject reliability tags into tool descriptions ──\n const toolWarnings = getToolWarnings();\n if (Object.keys(toolWarnings).length > 0) {\n for (const tool of tools) {\n const warning = toolWarnings[tool.function.name];\n if (warning) {\n tool.function.description = `${warning} ${tool.function.description}`;\n }\n }\n }\n\n // F3: Inject procedural memory (Hermes skill recall) into system prompt\n let enrichedSystemPrompt = systemPrompt;\n try {\n const { getProceduralContext } = await import('../skills/proceduralMemory.js');\n const proceduralContext = getProceduralContext(message);\n if (proceduralContext) {\n enrichedSystemPrompt += '\\n\\n' + proceduralContext;\n logger.debug('Agent', `[ProceduralMemory] Injected skill context into system prompt`);\n }\n } catch { /* proceduralMemory not available — non-critical */ }\n\n const messages: ChatMessage[] = [\n { role: 'system', content: enrichedSystemPrompt },\n ...historyMessages,\n ];\n\n let totalPromptTokens = 0;\n let totalCompletionTokens = 0;\n const toolsUsed: string[] = [];\n const orderedToolSequence: string[] = []; // Preserves execution order with repeats\n let finalContent = '';\n let modelUsed = config.agent.model;\n\n // Self-heal config\n const selfHealEnabled = (config.agent as Record<string, unknown>).selfHealEnabled !== false;\n\n // ── Checkpoint: track if budget was exhausted ──\n let budgetExhausted = false;\n\n // ── Cost optimizer: smart model routing ─────────────────\n let { model: activeModel, reason: routingReason } = routeModel(message, config.agent.model);\n if (overrides?.model) activeModel = overrides.model;\n // Voice model override: use a faster model for voice chat (lower latency)\n if (voiceFastPath && (config.voice as Record<string, unknown>)?.model) {\n activeModel = (config.voice as Record<string, unknown>).model as string;\n routingReason = 'voice model override';\n }\n // Session override has highest priority (set via /model command)\n if (session.modelOverride) {\n activeModel = session.modelOverride;\n routingReason = 'session override (/model)';\n }\n if (activeModel !== config.agent.model) {\n logger.info(COMPONENT, `Cost router: ${config.agent.model} → ${activeModel} (${routingReason})`);\n }\n modelUsed = activeModel;\n\n // ── Swarm Interceptor: ──────────────────────────────────────\n // If using kimi-k2.5, proxy the tools through Swarm Routers\n // to prevent context collapse from the massive generic 23-tool schema.\n const isKimiSwarm = activeModel.includes('kimi-k2.5');\n let activeTools = isKimiSwarm ? getSwarmRouterTools() : tools;\n if (isKimiSwarm) {\n logger.info(COMPONENT, `[Swarm] Intercepted kimi-k2.5 payload. Downgrading context from ${tools.length} to ${activeTools.length} router agents.`);\n }\n\n // Small-model tool reduction — prevent tool hallucination on models <8B\n // Validated on Ryzen 7 5825U: llama3.2:3b hallucinates web_search on trivial questions\n const SMALL_MODEL_PATTERNS = ['llama3.2', 'llama3.1:8b', 'phi', 'gemma:2b', 'qwen3.5:4b', 'tinyllama', 'dolphin3'];\n const isSmallModel = SMALL_MODEL_PATTERNS.some(p => activeModel.toLowerCase().includes(p));\n if (isSmallModel && !isKimiSwarm) {\n // web_search removed: small models hallucinate tool calls for trivial questions\n const CORE_TOOL_NAMES = ['shell', 'read_file', 'write_file', 'edit_file', 'list_dir', 'memory'];\n const coreTools = activeTools.filter(t => CORE_TOOL_NAMES.includes(t.function.name));\n logger.info(COMPONENT, `[SmallModel] Reducing tools from ${activeTools.length} to ${coreTools.length} for ${activeModel}`);\n activeTools = coreTools;\n }\n\n // ── Brain: intelligent tool pre-filtering ──────────────────\n if (!voiceFastPath && !isKimiSwarm && !isSmallModel && isBrainAvailable()) {\n const brainFiltered = await brainSelectTools(message, activeTools);\n if (brainFiltered.length > 0 && brainFiltered.length < activeTools.length) {\n logger.info(COMPONENT, `[Brain] Filtered: ${activeTools.length} → ${brainFiltered.length} tools`);\n activeTools = brainFiltered;\n }\n }\n\n // ── Tool Search: compact tool mode ──────────────────────────\n // Send only core tools + tool_search to the LLM instead of all 80+.\n // The LLM calls tool_search to discover additional tools as needed.\n const toolSearchConfig = (config as Record<string, unknown>).toolSearch as {\n enabled?: boolean;\n coreTools?: string[];\n } | undefined;\n const toolSearchEnabled = toolSearchConfig?.enabled ?? true;\n const allToolsBackup = activeTools;\n\n // Always ensure pipeline tools are in the active set, even when toolSearch is disabled\n if (pipelineEnsureTools.length > 0 && !(toolSearchEnabled && !isKimiSwarm && !isSmallModel && activeTools.length > 12)) {\n const activeNames = new Set(activeTools.map(t => t.function.name));\n const missing = pipelineEnsureTools.filter(name => !activeNames.has(name));\n if (missing.length > 0) {\n const rescued = allToolsBackup.filter(t => missing.includes(t.function.name));\n activeTools.push(...rescued);\n if (rescued.length > 0) {\n logger.info(COMPONENT, `[Pipeline:${pipelineType}] Ensured ${rescued.length} tools (no-compact): [${rescued.map(t => t.function.name).join(', ')}]`);\n }\n }\n }\n\n if (toolSearchEnabled && !isKimiSwarm && !isSmallModel && activeTools.length > 12) {\n // Voice gets a minimal tool set for speed (fewer tool schemas = less prompt tokens)\n const VOICE_CORE_TOOLS = ['shell', 'web_search', 'weather', 'memory', 'ha_control', 'ha_devices', 'ha_status', 'ha_setup', 'tool_search'];\n // Use config coreTools only if non-empty; otherwise fall back to DEFAULT_CORE_TOOLS\n const configCoreTools = toolSearchConfig?.coreTools;\n const effectiveCoreTools = (configCoreTools && configCoreTools.length > 0) ? configCoreTools : DEFAULT_CORE_TOOLS;\n // Pipeline tools: merge pipeline-specific tools into the core set\n const pipelineMerged = pipelineEnsureTools.length > 0\n ? [...new Set([...effectiveCoreTools, ...pipelineEnsureTools])]\n : effectiveCoreTools;\n const coreNames = new Set(voiceFastPath ? VOICE_CORE_TOOLS : pipelineMerged);\n activeTools = activeTools.filter(t => coreNames.has(t.function.name));\n // If pipeline tools were requested but not found in activeTools, pull from backup\n if (pipelineEnsureTools.length > 0) {\n const activeNames = new Set(activeTools.map(t => t.function.name));\n const missing = pipelineEnsureTools.filter(name => !activeNames.has(name));\n if (missing.length > 0) {\n const rescued = allToolsBackup.filter(t => missing.includes(t.function.name));\n activeTools.push(...rescued);\n if (rescued.length > 0) {\n logger.info(COMPONENT, `[Pipeline:${pipelineType}] Rescued ${rescued.length} tools: [${rescued.map(t => t.function.name).join(', ')}]`);\n }\n }\n }\n logger.info(COMPONENT, `[ToolSearch] Compact mode: ${allToolsBackup.length} → ${activeTools.length} tools (${allToolsBackup.length - activeTools.length} discoverable via tool_search)`);\n }\n\n // v5.0.2: Safety override — strip all tools so the model CANNOT call anything\n // dangerous. The safety message in systemPrompt tells it to refuse.\n if (dangerous) {\n activeTools = [];\n logger.info(COMPONENT, '[Safety] Stripped all tools — dangerous command detected');\n }\n\n // ── Stall detector: configure for autonomy mode + start heartbeat ──\n setAutonomousMode(isAutonomous);\n heartbeat(session.id);\n\n // ── Orchestration: check if task benefits from sub-agent delegation ──\n const autoDelegate = (subAgentConfig as Record<string, unknown> | undefined)?.autoDelegate !== false;\n if (!voiceFastPath && isAutonomous && autoDelegate && channel !== 'deliberation' && message.split(/\\s+/).length >= 8) {\n try {\n const delegationPlan = await analyzeForDelegation(message);\n if (delegationPlan && delegationPlan.shouldDelegate && delegationPlan.tasks.length >= 2) {\n logger.info(COMPONENT, `Orchestrator: delegating to ${delegationPlan.tasks.length} sub-agents`);\n const orchResult = await executeDelegationPlan(delegationPlan!);\n if (orchResult.subResults.length > 0 && orchResult.subResults.some(r => r.success)) {\n messages.push({\n role: 'user',\n content: `[Sub-agent results for your request]\\n\\n${orchResult.content}\\n\\nSynthesize these results into a coherent response for the user.`,\n });\n }\n }\n } catch (err) {\n logger.warn(COMPONENT, `Orchestration failed, falling through: ${(err as Error).message}`);\n }\n }\n\n // ══════════════════════════════════════════════════════════════\n // v4.13: run ContextEngine plugin assemble hooks (topFacts,\n // memoryRetrieval, smartCompress) before the loop. Previously the\n // plugin system existed but nothing called runAssemble, so\n // top-facts + relevant graph/vector memories never reached the LLM.\n // Injects a system message per active plugin right before the last\n // user message. Errors inside a plugin are swallowed.\n // ══════════════════════════════════════════════════════════════\n try {\n const { getPlugins } = await import('../plugins/registry.js');\n const { runAssemble } = await import('../plugins/contextEngine.js');\n const plugins = getPlugins();\n if (plugins.length > 0) {\n const assembled = await runAssemble(plugins, messages, message);\n messages.splice(0, messages.length, ...assembled);\n }\n } catch (pluginErr) {\n logger.debug(COMPONENT, `plugin assemble failed: ${(pluginErr as Error).message}`);\n }\n\n // ══════════════════════════════════════════════════════════════\n // Agent Loop — Phase State Machine (Think/Act/Respond)\n // Replaces the old monolithic for-loop. See agentLoop.ts.\n // ══════════════════════════════════════════════════════════════\n const loopResult = await runAgentLoop({\n messages,\n activeTools,\n allToolsBackup,\n activeModel,\n config,\n sessionId: session.id,\n agentId: overrides?.agentId,\n channel,\n message,\n streamCallbacks,\n signal,\n isAutonomous,\n voiceFastPath,\n effectiveMaxRounds,\n taskEnforcementActive,\n reflectionEnabled,\n reflectionInterval,\n toolSearchEnabled,\n isKimiSwarm,\n selfHealEnabled,\n smartExitEnabled: pipelineSmartExit,\n thinkingOverride: session.thinkingOverride,\n pipelineTerminalTools,\n completionStrategy: pipelineCompletionStrategy,\n pipelineType,\n minRounds: pipelineMinRounds,\n });\n\n // Unpack results\n finalContent = loopResult.content;\n toolsUsed.push(...loopResult.toolsUsed);\n orderedToolSequence.push(...loopResult.orderedToolSequence);\n modelUsed = loopResult.modelUsed;\n totalPromptTokens += loopResult.promptTokens;\n totalCompletionTokens += loopResult.completionTokens;\n budgetExhausted = loopResult.budgetExhausted;\n\n // Extract structured artifacts for deliberation inter-step context\n const toolArtifacts = extractToolArtifacts(loopResult.toolCallDetails);\n\n // ── Ralph Loop Verification ─────────────────────────────────\n // Outer completion check: did the task actually get done?\n // If the user asked to edit/write but no write tool was called,\n // re-run the agent loop ONE more time with a forced write instruction.\n if (isAutonomous && !voiceFastPath && !budgetExhausted && channel !== 'deliberation') {\n const verification = verifyTaskCompletion(message, toolsUsed, finalContent);\n if (!verification.complete) {\n logger.warn(COMPONENT, `[RalphLoop] Task incomplete: ${verification.reason}. Re-running with forced write.`);\n\n // Add the verification feedback and re-run with explicit tool guidance\n messages.push({ role: 'assistant', content: finalContent });\n messages.push({ role: 'user', content: [\n `[TASK INCOMPLETE] ${verification.reason}`,\n '',\n 'You have the file content from your previous read_file call.',\n 'Now call edit_file with these arguments:',\n ' - path: the file path you just read',\n ' - target: the exact string you want to replace (copy it from the file)',\n ' - replacement: the new string to put in its place',\n '',\n 'edit_file does a search-and-replace. You do NOT need to rewrite the whole file.',\n 'Just find a small section to change and replace it.',\n 'CALL edit_file NOW.',\n ].join('\\n') });\n\n const retryResult = await runAgentLoop({\n messages,\n activeTools,\n allToolsBackup,\n activeModel,\n config,\n sessionId: session.id,\n agentId: overrides?.agentId,\n channel,\n message,\n streamCallbacks,\n signal,\n isAutonomous,\n voiceFastPath,\n effectiveMaxRounds: 4, // Short budget for the retry\n taskEnforcementActive: true,\n reflectionEnabled: false,\n reflectionInterval: 99,\n toolSearchEnabled,\n isKimiSwarm,\n selfHealEnabled: false,\n thinkingOverride: session.thinkingOverride,\n });\n\n if (retryResult.content) finalContent = retryResult.content;\n toolsUsed.push(...retryResult.toolsUsed);\n orderedToolSequence.push(...retryResult.orderedToolSequence);\n totalPromptTokens += retryResult.promptTokens;\n totalCompletionTokens += retryResult.completionTokens;\n\n logger.info(COMPONENT, `[RalphLoop] Retry complete. Tools used: [${retryResult.toolsUsed.join(', ')}]`);\n }\n }\n\n // Clean up stall detector for this session\n clearSession(session.id);\n // Hunt Finding #22 / #46 (2026-04-15): previously resetLoopDetection fired\n // at the end of EVERY turn, wiping cross-turn state. That meant the loop\n // breaker could only catch loops within a single turn — a model that\n // hit the same tool on turn 1, turn 2, turn 3 (across separate requests\n // in the same session) would never trip. The README claims TITAN\n // \"prevents runaway loops and wasted tokens\" which implies cross-turn\n // coverage. Fix: keep the session's loop state alive until the session\n // actually closes (session.ts:483 still wipes on session close).\n // resetLoopDetection(session.id); // INTENTIONALLY REMOVED — see comment above\n\n // Clear checkpoints on successful completion (no need to resume)\n if (!budgetExhausted) {\n import('./checkpoint.js').then(m => m.clearCheckpoints(session.id)).catch(e => logger.debug('Agent', `Background op failed: ${(e as Error).message}`));\n }\n\n // Active Learning: record strategy for future reference\n if (toolsUsed.length > 0) {\n const success = !finalContent.toLowerCase().includes('error') && !budgetExhausted;\n recordStrategy(message, [...new Set(toolsUsed)], orderedToolSequence.length, success, orderedToolSequence);\n\n // Feedback loop: record outcome for matching strategies\n if (orderedToolSequence.length > 0) {\n recordStrategyOutcome(classifyTaskType(message), orderedToolSequence, success);\n }\n\n // Hindsight MCP: retain successful strategies as cross-session experience (fire-and-forget)\n // F2: Scoped to the agent's memory namespace so each agent builds its own episodic slice.\n if (success && orderedToolSequence.length > 0) {\n (async () => {\n let hsNs: string | undefined;\n if (overrides?.agentId && overrides.agentId !== 'default') {\n try {\n const { getAgentMemoryNamespace } = await import('./commandPost.js');\n hsNs = getAgentMemoryNamespace(overrides.agentId);\n } catch { /* fallthrough */ }\n }\n try { retainStrategy(classifyTaskType(message), orderedToolSequence, 1, message.slice(0, 200), hsNs); } catch { /* Hindsight unavailable */ }\n })().catch(() => { /* Hindsight unavailable */ });\n }\n }\n\n // ── Hallucination Guard: detect cloud models that claim tool use but never called tools ──\n // Cloud models sometimes describe tool actions (\"I wrote the file\", \"Output: ...\") without\n // actually making tool calls. This pollutes session memory with false action claims.\n //\n // SKIP for conversational tasks — chat/general pipelines don't require tool use,\n // so \"I did X\" in a conversational reply is normal, not a hallucination.\n const isConversationalTask = pipelineType === 'chat' || pipelineType === 'general'\n || channel?.endsWith('-admin') || channel === 'voice';\n const isCloudHallucination = toolsUsed.length === 0\n && taskEnforcementActive\n && !isConversationalTask\n && (activeModel.includes(':cloud') || activeModel.includes('-cloud'))\n && finalContent.length > 0\n && /(?:(?:I(?:'ve| have)?|successfully|done|completed|executed|created|wrote|saved|ran|output|result)[:\\s])/i.test(finalContent)\n && !/(?:I (?:can|could|would|will|should)|let me|I don't|I cannot|error|failed)/i.test(finalContent);\n\n if (isCloudHallucination) {\n logger.warn(COMPONENT, `[HallucinationGuard] Cloud model claimed action but toolsUsed is empty — sanitizing response`);\n finalContent = \"I wasn't able to finish that task — the connection to my brain hiccupped. Try asking again, or I can switch to my offline mode if you prefer.\";\n }\n\n // Save assistant response to session\n addMessage(session, 'assistant', finalContent, {\n model: modelUsed,\n tokenCount: totalCompletionTokens,\n });\n\n // Auto-record agent response to knowledge graph (fire-and-forget, skip short/error responses)\n if (finalContent.length > 50 && !finalContent.startsWith('⚠️')) {\n addEpisode(`[TITAN → ${channel}/${userId}] ${finalContent.slice(0, 500)}`, 'agent').catch(e => logger.debug('Agent', `Background op failed: ${(e as Error).message}`));\n }\n\n // Record usage\n const { provider: providerName } = { provider: modelUsed.split('/')[0] || 'unknown' };\n recordUsage(session.id, providerName, modelUsed, totalPromptTokens, totalCompletionTokens);\n\n const durationMs = Date.now() - startTime;\n logger.info(COMPONENT, `Response generated in ${durationMs}ms (${totalPromptTokens + totalCompletionTokens} tokens)`);\n\n // ── Post-conversation learning: record insights from tool usage ───\n if (toolsUsed.length > 0) {\n const uniqueTools = [...new Set(toolsUsed)];\n learnFact(\n 'conversation_insight',\n `User asked \"${message.slice(0, 80)}\" → used tools: ${uniqueTools.join(', ')} (${durationMs}ms)`,\n message.slice(0, 100),\n );\n }\n\n // ── ContextEngine afterTurn hooks (fire-and-forget — TopFacts, SmartCompress, etc.) ──\n const afterTurnPlugins = getPlugins() || [];\n if (afterTurnPlugins.length > 0) {\n runAfterTurn(afterTurnPlugins, { content: finalContent, toolsUsed: [...new Set(toolsUsed)] }).catch(e => logger.debug('Agent', `Background op failed: ${(e as Error).message}`));\n }\n\n // ── Checkpoint: if budget exhausted, build a checkpoint for potential resumption ──\n let checkpoint: string | undefined;\n if (budgetExhausted) {\n try {\n checkpoint = JSON.stringify({\n sessionId: session.id,\n toolsUsed: [...new Set(toolsUsed)],\n roundsUsed: effectiveMaxRounds,\n lastContent: finalContent.slice(0, 500),\n timestamp: Date.now(),\n });\n } catch (e) { logger.debug(COMPONENT, `Response serialization failed: ${(e as Error).message}`); }\n }\n\n // Consolidate soul wisdom from this task\n const taskSuccess = !finalContent.toLowerCase().includes('error') && !budgetExhausted;\n consolidateWisdom(session.id, classifyTaskType(message), taskSuccess, loopResult.toolCallDetails.length);\n clearSoulState(session.id);\n\n // Log task-level trajectory and check for auto-skill generation\n const trajectory = {\n id: randomBytes(16).toString('hex'),\n timestamp: new Date().toISOString(),\n task: message.slice(0, 500),\n taskType: classifyTaskType(message),\n model: modelUsed,\n toolSequence: orderedToolSequence,\n toolDetails: loopResult.toolCallDetails,\n success: taskSuccess,\n rounds: loopResult.toolCallDetails.length,\n durationMs,\n sessionId: session.id,\n };\n logTrajectory(trajectory);\n processTrajectoryForSkills(trajectory);\n\n // Finalize trace\n trace.setModel(modelUsed);\n trace.setRounds(loopResult.toolCallDetails.length);\n trace.setTokens(totalPromptTokens, totalCompletionTokens);\n for (const tc of loopResult.toolCallDetails) {\n trace.toolCall(tc.name, tc.args, 0, tc.success, 0);\n }\n trace.end(budgetExhausted ? 'failed' : 'completed', budgetExhausted ? 'budget exhausted' : undefined);\n\n // Soma (v4.0): turn:post event on the trace bus. Fires for every\n // non-early-return turn. Subscribers (drive recompute, activity feed)\n // react here; no-op when nothing listening.\n try {\n const { emit: emitTrace } = await import('../substrate/traceBus.js');\n emitTrace('turn:post', {\n agentId: overrides?.agentId || 'default',\n sessionId: session.id,\n channel,\n userId,\n success: !budgetExhausted,\n toolsUsed: [...new Set(toolsUsed)],\n durationMs,\n model: modelUsed,\n timestamp: new Date().toISOString(),\n });\n } catch { /* substrate not available — skip */ }\n\n return {\n content: finalContent,\n sessionId: session.id,\n toolsUsed: [...new Set(toolsUsed)],\n tokenUsage: {\n prompt: totalPromptTokens,\n completion: totalCompletionTokens,\n total: totalPromptTokens + totalCompletionTokens,\n },\n model: modelUsed,\n durationMs,\n exhaustedBudget: budgetExhausted || undefined,\n checkpoint,\n toolArtifacts,\n };\n}\n"],"mappings":";AAIA,SAAS,YAAY,oBAAoB;AACzC,SAAS,mBAAmB;AAC5B,SAAS,kBAAkB;AAC3B,SAAS,oBAAoB,wBAAwB,YAAY,0BAA0B;AAC3F,SAAS,0BAA0B;AACnC,SAAS,aAAa,sBAAsB;AAC5C,SAAS,oBAAoB,WAAW,iBAAiB,kBAAkB,gBAAgB,uBAAuB,kBAAkB,iCAAiC;AACrK,SAAS,4BAA4B;AACrC,SAAS,gBAAgB,yBAAyB;AAClD,SAAS,oBAAoB,oBAAoB;AACjD,SAAS,wBAAwB;AACjC,SAAS,WAAW,cAAc,iBAAiB,yBAAyB;AAI5E,SAAS,kBAAkB;AAC3B,SAAS,kBAAkB;AAC3B,SAAS,oBAAoB;AAC7B,SAAS,2BAA2B;AACpC,SAAS,kBAAkB,SAAS,cAAc,aAAa,gBAAgB,iBAAiB,oBAAoB,yBAAyB;AAE7I,SAAS,WAAW,YAAY,uBAAuB;AACvD,SAAS,eAAe,kBAAkB,eAAe,kBAAkB,gBAAgB,yBAAyB;AACpH,SAAS,0BAA0B;AACnC,SAAS,kBAAkB,6BAAgD;AAC3E,SAAS,iCAAiC;AAC1C,SAAS,4BAA6C;AACtD,SAAS,+BAA+B;AACxC,SAAS,sBAAsB,6BAA6B;AAC5D,SAAS,mBAAmB;AAC5B,SAAS,aAAa,2BAA2B;AACjD,SAAS,eAAe,2BAA2B;AACnD,SAAS,qBAAqB;AAC9B,SAAS,4BAA4B,wBAAwB;AAC7D,SAAS,gBAAgB;AACzB,SAAS,mBAAmB;AAC5B,SAAS,oBAAoB;AAC7B,SAAS,oBAAqC;AAC9C,SAAS,kBAAkB;AAC3B,SAAS,eAAkE,mBAAmB,gBAAgB,sBAAsB;A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AAI;AACA,UAAM,EAAE,MAAM,UAAU,IAAI,MAAM,OAAO,0BAA0B;AACnE,cAAU,aAAa;AAAA,MACnB,SAAS,WAAW,WAAW;AAAA,MAC/B,WAAW,QAAQ;AAAA,MACnB;AAAA,MACA;AAAA,MACA,SAAS,CAAC;AAAA,MACV,WAAW,CAAC,GAAG,IAAI,IAAI,SAAS,CAAC;AAAA,MACjC;AAAA,MACA,OAAO;AAAA,MACP,YAAW,oBAAI,KAAK,GAAE,YAAY;AAAA,IACtC,CAAC;AAAA,EACL,QAAQ;AAAA,EAAuC;AAE/C,SAAO;AAAA,IACH,SAAS;AAAA,IACT,WAAW,QAAQ;AAAA,IACnB,WAAW,CAAC,GAAG,IAAI,IAAI,SAAS,CAAC;AAAA,IACjC,YAAY;AAAA,MACR,QAAQ;AAAA,MACR,YAAY;AAAA,MACZ,OAAO,oBAAoB;AAAA,IAC/B;AAAA,IACA,OAAO;AAAA,IACP;AAAA,IACA,iBAAiB,mBAAmB;AAAA,IACpC;AAAA,IACA;AAAA,EACJ;AACJ;","names":["issue","wakeup","getPlugins"]}
1
+ {"version":3,"sources":["../../src/agent/agent.ts"],"sourcesContent":["/**\n * TITAN — Core Agent Loop\n * The main agent: receives messages, builds context, calls LLM, handles tools, responds.\n */\nimport { existsSync, readFileSync } from 'fs';\nimport { randomBytes } from 'crypto';\nimport { loadConfig } from '../config/config.js';\nimport { getOrCreateSession, getOrCreateSessionById, addMessage, getContextMessages } from './session.js';\nimport { getToolDefinitions } from './toolRunner.js';\nimport { recordUsage, searchMemories } from '../memory/memory.js';\nimport { getLearningContext, learnFact, getToolWarnings, classifyTaskType, recordStrategy, recordStrategyOutcome, getStrategyHints, getLearnedPreferenceHints } from '../memory/learning.js';\nimport { buildPersonalContext } from '../memory/relationship.js';\nimport { retainStrategy, getHindsightHints } from '../memory/hindsightBridge.js';\nimport { getTeachingContext, isCorrection } from './teaching.js';\nimport { recordCorrection } from './userProfile.js';\nimport { heartbeat, clearSession, setStallHandler, setAutonomousMode } from './stallDetector.js';\n// Hunt Finding #22 / #46: resetLoopDetection is intentionally NOT called here.\n// See agent.ts:~1466 for the explanation. Session-close handles cleanup.\n// (Import left out so lint doesn't flag the unused symbol.)\nimport { routeModel } from './costOptimizer.js';\nimport { getPlugins } from '../plugins/registry.js';\nimport { runAfterTurn } from '../plugins/contextEngine.js';\nimport { getSwarmRouterTools } from './swarm.js';\nimport { shouldDeliberate, analyze, generatePlan, executePlan, handleApproval, getDeliberation, cancelDeliberation, formatPlanResults } from './deliberation.js';\nimport type { ChatMessage } from '../providers/base.js';\nimport { initGraph, addEpisode, getGraphContext } from '../memory/graph.js';\nimport { isAvailable as isBrainAvailable, selectTools as brainSelectTools, ensureLoaded as ensureBrainLoaded } from './brain.js';\nimport { DEFAULT_CORE_TOOLS } from './toolSearch.js';\nimport { classifyPipeline, resolvePipelineConfig, PIPELINE_PROFILES } from './pipeline.js';\nimport { buildSelfAwarenessContext } from './selfAwareness.js';\nimport { assembleSystemPrompt, type PromptMode } from './systemPromptParts.js';\nimport { getOptimizedPromptBlock } from '../skills/builtin/self_improve.js';\nimport { analyzeForDelegation, executeDelegationPlan } from './orchestrator.js';\nimport { queueWakeup } from './agentWakeup.js';\nimport { createIssue, requestHireApproval } from './commandPost.js';\nimport { spawnSubAgent, SUB_AGENT_TEMPLATES } from './subAgent.js';\nimport { logTrajectory } from './trajectoryLogger.js';\nimport { processTrajectoryForSkills, getSkillGuidance } from './autoSkillGen.js';\nimport { getAgent } from './multiAgent.js';\nimport { isDangerous } from '../utils/safety.js';\nimport { registerTool } from './toolRunner.js';\nimport { runAgentLoop, type LoopResult } from './agentLoop.js';\nimport { startTrace } from './tracer.js';\nimport { initSoulState, updateSoulState, emitHeartbeat, getInnerMonologue, consolidateWisdom, clearSoulState, getWisdomHints } from './soul.js';\nimport logger from '../utils/logger.js';\nimport { TITAN_NAME, AGENTS_MD, SOUL_MD, TOOLS_MD, TITAN_MD_FILENAME } from '../utils/constants.js';\n\nconst COMPONENT = 'Agent';\nconst MAX_TOOL_ROUNDS = 10;\n\n/** Estimate the round budget based on task complexity */\n/**\n * Estimate how many tool rounds a message needs.\n * Simple tasks (read a file, run a command) → 3-4 rounds.\n * Multi-step tasks (read, analyze, write) → 6-10 rounds.\n * Complex tasks (research, build, deploy) → 12-20 rounds.\n */\nfunction estimateRoundBudget(message: string, config: { agent: { dynamicBudget?: boolean; maxToolRoundsHard?: number }; autonomy: { mode: string } } & Record<string, unknown>): number {\n const agentConfig = config.agent as Record<string, unknown>;\n if (agentConfig.dynamicBudget === false) return MAX_TOOL_ROUNDS;\n\n const hardCap = (agentConfig.maxToolRoundsHard as number) || 50;\n const lower = message.toLowerCase();\n const words = message.split(/\\s+/).length;\n\n // Count complexity signals\n const isQuestion = /^(what|who|how|why|where|when|which|is |are |do |does |can |will )/i.test(message.trim());\n const isSingleAction = /^(read|write|run|list|show|tell|get|check|find)\\b/i.test(message.trim());\n const isMultiStep = /\\b(then|after that|next|step \\d|finally|first.*then|and also|additionally|and then)\\b/i.test(lower);\n const isComplex = /\\b(research|analyze|investigate|compare|build|implement|create.*and|deploy|automat|refactor|rewrite|design)/i.test(lower);\n const isAmbitious = /\\b(step by step|end.to.end|full pipeline|from scratch|entire|complete|comprehensive)\\b/i.test(lower);\n\n // Count tool-intent signals (how many distinct actions are implied)\n const actionCount = [\n /\\b(read|open|show|display|check)\\b/i.test(lower),\n /\\b(write|create|save|generate|make)\\b/i.test(lower),\n /\\b(edit|change|modify|update|fix|replace)\\b/i.test(lower),\n /\\b(run|execute|install|build|test|deploy)\\b/i.test(lower),\n /\\b(search|find|look|research|investigate)\\b/i.test(lower),\n /\\b(summarize|analyze|compare|report)\\b/i.test(lower),\n ].filter(Boolean).length;\n\n let budget: number;\n\n if (isQuestion && words < 15 && !isMultiStep) {\n budget = 3; // \"What is X?\" \"Who is Y?\" — quick lookup\n } else if (isSingleAction && words < 20 && !isMultiStep) {\n budget = 4; // \"Read package.json\" \"Run uname\" — one tool call\n } else if (actionCount <= 1 && words < 30 && !isMultiStep) {\n budget = 5; // Single-purpose task, short message\n } else if (actionCount <= 2 && !isComplex) {\n budget = 8; // Two-step task (read + write, search + summarize)\n } else if (isMultiStep || actionCount >= 3) {\n budget = 12; // Multi-step explicit pipeline\n } else if (isComplex || isAmbitious) {\n budget = 18; // Research, build, deploy — needs room to work\n } else {\n budget = 6; // Default for unclassified moderate tasks\n }\n\n // In autonomous mode, use the configured maxRounds directly\n // The dynamic budget and hard cap should NOT limit autonomous execution\n const isAutonomous = config.autonomy.mode === 'autonomous';\n const configuredMax = (agentConfig.maxRounds as number) || 0;\n if (isAutonomous && configuredMax > 0) {\n return configuredMax;\n }\n\n return Math.min(budget, hardCap);\n}\n\n// ── Ralph Loop Verification ─────────────────────────────────────\n// Checks whether the agent actually completed the requested task.\n// Inspired by vercel-labs/ralph-loop-agent outer verification pattern.\n\n/**\n * Exported for regression testing in tests/agent-verify.test.ts.\n * The v4.3.4 fix narrowed its pattern matching; easy to break\n * if someone generalizes the regex again without a test.\n */\nexport function verifyTaskCompletion(\n message: string,\n toolsUsed: string[],\n response: string,\n): { complete: boolean; reason: string } {\n const lower = message.toLowerCase();\n\n // v4.3.4: conversational asides shouldn't trigger a \"you must edit_file\"\n // enforcement loop. Tony's voice notes on Messenger like\n // \"fix your voice\" or \"fix the way you respond\" would match the\n // edit|fix regex + \"file\"/\"files\" (because the voice note transcript\n // often contains the word \"files\") and trip the Ralph Loop, which\n // then injects a stale \"pending file edit task\" prompt and the\n // LLM hallucinates back \"I don't have a pending file edit task.\"\n // Two narrowings:\n // 1. Don't count `shell` as a file read. The verifier was using it\n // as a proxy for read_file but any `ls`/`pwd` call tripped it.\n // 2. Require an EXPLICIT file path or filename token (e.g. \".ts\",\n // \".py\", \"/path/\", \"src/\", etc.) rather than the bare word\n // \"file\"/\"files\" which appears constantly in natural speech.\n const verbMatch = /\\b(edit|fix|change|modify|update|add|write|create|improve|rewrite|save|implement|patch)\\b/i.test(lower);\n const hasFilePath = /[\\w-]+\\.(ts|tsx|js|jsx|py|md|json|yaml|yml|html|css|sh|txt|rs|go|java|cpp|c|h)\\b|\\bsrc\\/|\\bpath:|\\/[a-z]+\\//i.test(lower);\n const askedToWrite = verbMatch && hasFilePath;\n const didWrite = toolsUsed.some(t => ['write_file', 'edit_file', 'append_file'].includes(t));\n const didRead = toolsUsed.includes('read_file');\n\n if (askedToWrite && !didWrite && didRead) {\n return {\n complete: false,\n reason: 'You read the file but did not save any changes. You MUST call edit_file or write_file to apply your modifications. Call the tool now.',\n };\n }\n\n // Check: user asked to run/execute something but no shell was called\n const askedToRun = /\\b(run|execute|install|deploy|build|test|restart)\\b/i.test(lower)\n && /\\b(command|script|service|server|package|npm|pip)\\b/i.test(lower);\n const didRun = toolsUsed.includes('shell');\n\n if (askedToRun && !didRun) {\n return {\n complete: false,\n reason: 'You did not execute the requested command. Call the shell tool to run it.',\n };\n }\n\n return { complete: true, reason: '' };\n}\n\n// ── Current session context for spawn_agent async delegation ─────\nlet currentSessionId: string | null = null;\nexport function setCurrentSessionId(id: string | null): void { currentSessionId = id; }\nexport function getCurrentSessionId(): string | null { return currentSessionId; }\n\n// ── Register spawn_agent tool ────────────────────────────────────\nlet spawnAgentRegistered = false;\nfunction ensureSpawnAgentRegistered(): void {\n if (spawnAgentRegistered) return;\n spawnAgentRegistered = true;\n registerTool({\n name: 'spawn_agent',\n description: 'Hand off a task to a specialist teammate. Choose: scout (research), builder (code/files), writer (content), analyst (decisions), or sage (review). For parallel execution of multiple subtasks, prefer agent_team. For sequential dependent tasks, prefer agent_chain.',\n parameters: {\n type: 'object',\n properties: {\n name: { type: 'string', description: 'Optional display name for the sub-agent run (e.g., \"Scout\", \"Builder\"). The specialist persona is determined by `template`.' },\n task: { type: 'string', description: 'A clear, self-contained task description. Include all context the specialist needs — it has no memory of this conversation.' },\n template: { type: 'string', description: 'Specialist to route to. Prefer: \"scout\" | \"builder\" | \"writer\" | \"analyst\".' },\n model: { type: 'string', description: 'Model override. Usually leave blank — specialists pick their own role-tuned model.' },\n },\n required: ['task'],\n },\n execute: async (args) => {\n const templateName = (args.template as string) || '';\n\n // v5.0.0-spacewalk: resolve config-defined agents from titan.json.\n // If the template name matches a config agent entry, use its base\n // template (e.g. \"coder\") for tools/systemPrompt, but keep the\n // config agent's name, model, and systemPromptOverride.\n let configAgent: import('./agentScope.js').ResolvedAgentConfig | null = null;\n try {\n const { resolveAgentConfig, agentAllowsSkill } = await import('./agentScope.js');\n configAgent = resolveAgentConfig(templateName);\n } catch { /* optional — agentScope may not exist in some builds */ }\n\n const baseTemplateName = configAgent?.template || templateName;\n const template = SUB_AGENT_TEMPLATES[baseTemplateName] || {};\n const agentName = (args.name as string) || configAgent?.name || template.name || 'SubAgent';\n const task = args.task as string;\n\n // v5.3.1: Apply config-defined agent constraints (maxRounds, maxTokens,\n // persona, skillsFilter). Log what we apply for observability.\n const appliedFields: string[] = [];\n if (configAgent) {\n if (configAgent.maxRounds !== 15) appliedFields.push(`maxRounds=${configAgent.maxRounds}`);\n if (configAgent.maxTokens !== 4000) appliedFields.push(`maxTokens=${configAgent.maxTokens}`);\n if (configAgent.persona && configAgent.persona !== 'default') appliedFields.push(`persona=${configAgent.persona}`);\n if (configAgent.skillsFilter) appliedFields.push(`skillsFilter=[${configAgent.skillsFilter.join(',')}]`);\n if (configAgent.modelFallbacks.length > 0) logger.info('Agent', `Agent \"${agentName}\" has modelFallbacks — not yet implemented in spawn path`);\n if (configAgent.workspaceDir) appliedFields.push(`workspaceDir=${configAgent.workspaceDir}`);\n if (configAgent.tags.length > 0) appliedFields.push(`tags=[${configAgent.tags.join(',')}]`);\n if (configAgent.systemPromptOverride) appliedFields.push('systemPromptOverride=set');\n if (appliedFields.length > 0) logger.info('Agent', `Applied config agent fields for \"${agentName}\": ${appliedFields.join(', ')}`);\n }\n\n // v4.9.0: kill switch gate — if Tony killed the organism, no\n // new sub-agent spawns until he explicitly resumes.\n try {\n const { isKilled } = await import('../safety/killSwitch.js');\n if (isKilled()) {\n return '[Sub-Agent REFUSED] TITAN kill switch is active. Tony must resume via /api/safety/resume before new sub-agents can spawn.';\n }\n } catch { /* fail-open — safety module optional */ }\n\n // v4.7.0: Hermes-style safety gates on the spawn path.\n // Prevents fork bombs + concurrent runaway. Depth is best-effort\n // (we treat the primary as depth 0); the MAX_CONCURRENT_CHILDREN\n // cap is the hard backstop.\n try {\n const { canSpawnChild } = await import('./subagentSafety.js');\n const parent = currentSessionId || 'root';\n const gate = canSpawnChild(parent, 0);\n if (!gate.ok) {\n return `I can't delegate that task right now (${gate.reason}). I'll handle it myself.`;\n }\n } catch { /* fail-open — safety module optional */ }\n\n // v4.7.0: specialist routing. If the template matches a\n // registered specialist (Scout/Builder/Writer/Analyst),\n // use its tuned system prompt + preferred model.\n let specialistPrompt: string | undefined;\n let specialistModel: string | undefined;\n try {\n const { findSpecialistForTemplate, loadSpecialistPersona } = await import('./specialists.js');\n const sp = findSpecialistForTemplate(templateName);\n if (sp) {\n specialistPrompt = loadSpecialistPersona(sp.id);\n specialistModel = sp.model;\n }\n } catch { /* fall through to generic template */ }\n\n // ── Async path: delegate via Command Post ────────────\n const cpEnabled = loadConfig().commandPost?.enabled ?? false;\n if (cpEnabled) {\n const issue = createIssue({\n title: `[Agent Task] ${task.slice(0, 80)}`,\n description: task,\n priority: 'medium',\n createdByUser: 'agent',\n });\n\n const wakeup = queueWakeup({\n issueId: issue.id,\n issueIdentifier: issue.identifier,\n agentId: issue.id, // Use issue ID as agent ID for simplicity\n agentName,\n parentSessionId: currentSessionId,\n task,\n templateName,\n model: args.model as string | undefined,\n });\n\n return `I've handed that off to the ${agentName} specialist. They'll jump on it and report back when they're done.`;\n }\n\n // ── Sync path: original blocking execution ───────────\n // v4.7.0: track child for concurrent cap, use specialist prompt/model when matched.\n let childId: string | undefined;\n try {\n const { registerChild } = await import('./subagentSafety.js');\n childId = `child-${Date.now().toString(36)}-${Math.random().toString(36).slice(2, 6)}`;\n registerChild(currentSessionId || 'root', childId);\n } catch { /* safety optional */ }\n\n // Track specialist status in CP registry during execution\n let specialistId: string | undefined;\n try {\n const { findSpecialistForTemplate } = await import('./specialists.js');\n const sp = findSpecialistForTemplate(templateName);\n if (sp) specialistId = sp.id;\n } catch { /* optional */ }\n\n try {\n if (specialistId) {\n const { updateAgentStatus } = await import('./commandPost.js');\n updateAgentStatus(specialistId, 'active');\n }\n\n // v5.3.1: Filter template tools through config agent skillsFilter\n let allowedTools = template.tools;\n if (configAgent?.skillsFilter) {\n try {\n const { agentAllowsSkill } = await import('./agentScope.js');\n allowedTools = template.tools?.filter(t => agentAllowsSkill(configAgent!, t));\n if (allowedTools && allowedTools.length === 0) {\n logger.warn('Agent', `Agent \"${agentName}\" skillsFilter blocks all template tools — falling back to template default`);\n allowedTools = template.tools;\n }\n } catch { /* fall through to template.tools */ }\n }\n\n const result = await spawnSubAgent({\n name: agentName,\n task,\n tools: allowedTools,\n systemPrompt: configAgent?.systemPromptOverride || specialistPrompt || template.systemPrompt,\n model: (args.model as string | undefined) || configAgent?.model || specialistModel,\n tier: (template as Record<string, unknown>).tier as 'cloud' | 'smart' | 'fast' | 'local' | undefined,\n persona: configAgent?.persona,\n maxRounds: configAgent?.maxRounds,\n maxTokens: configAgent?.maxTokens,\n workspaceDir: configAgent?.workspaceDir ?? undefined,\n tags: configAgent?.tags,\n depth: 1, // v4.7.0: this IS a child (was incorrectly 0)\n });\n\n if (specialistId) {\n const { updateAgentStatus } = await import('./commandPost.js');\n updateAgentStatus(specialistId, 'idle');\n }\n\n return `The ${agentName} specialist finished. Here's what they found:\\n\\n${result.content}`;\n } catch (err) {\n if (specialistId) {\n try {\n const { updateAgentStatus } = await import('./commandPost.js');\n updateAgentStatus(specialistId, 'idle');\n } catch { /* best effort */ }\n }\n throw err;\n } finally {\n if (childId) {\n try {\n const { unregisterChild } = await import('./subagentSafety.js');\n unregisterChild(currentSessionId || 'root', childId);\n } catch { /* best effort */ }\n }\n }\n },\n });\n}\n\n// ── Register delegate_task tool (inter-agent delegation via Command Post) ──\nlet delegateTaskRegistered = false;\nfunction ensureDelegateTaskRegistered(): void {\n if (delegateTaskRegistered) return;\n delegateTaskRegistered = true;\n registerTool({\n name: 'delegate_task',\n description: 'Delegate a task to a multi-agent worker OR an external agent (Codex, bash). Creates a Command Post issue and returns immediately. Results are injected into your next response.',\n parameters: {\n type: 'object',\n properties: {\n agentId: { type: 'string', description: 'Target agent ID (from list_agents). Required unless using an external adapter.' },\n task: { type: 'string', description: 'Task description for the worker' },\n priority: { type: 'string', description: 'Priority: low, medium, high, critical (default: medium)' },\n adapter: { type: 'string', description: 'External adapter: \"codex\" or \"bash\". When set, task runs via external CLI instead of internal agent.' },\n cwd: { type: 'string', description: 'Working directory for external adapters (optional)' },\n },\n required: ['task'],\n },\n execute: async (args) => {\n const task = args.task as string;\n const priority = (args.priority as string) || 'medium';\n const adapterType = args.adapter as string | undefined;\n const cwd = args.cwd as string | undefined;\n\n if (adapterType) {\n // ── External adapter path ────────────────────────\n const issue = createIssue({\n title: `[External: ${adapterType}] ${task.slice(0, 70)}`,\n description: task,\n priority: priority as 'low' | 'medium' | 'high' | 'critical',\n createdByUser: 'agent',\n });\n\n const wakeup = queueWakeup({\n issueId: issue.id,\n issueIdentifier: issue.identifier,\n agentId: `adapter:${adapterType}`,\n agentName: adapterType,\n parentSessionId: currentSessionId,\n task,\n templateName: '',\n mode: 'external',\n adapterType,\n cwd,\n });\n\n return `I've asked the ${adapterType} adapter to handle that. It will work on it and report back when it's done.`;\n }\n\n // ── Multi-agent path ─────────────────────────────\n const targetId = args.agentId as string;\n if (!targetId) return 'Error: agentId is required when not using an external adapter.';\n\n const target = getAgent(targetId);\n if (!target) return `Error: Agent \"${targetId}\" not found. Use list_agents to see available agents.`;\n if (target.status !== 'running') return `Error: Agent \"${targetId}\" is ${target.status}, not running.`;\n\n const issue = createIssue({\n title: `[Delegated] ${task.slice(0, 80)}`,\n description: task,\n priority: priority as 'low' | 'medium' | 'high' | 'critical',\n assigneeAgentId: target.id,\n createdByUser: 'agent',\n });\n\n const wakeup = queueWakeup({\n issueId: issue.id,\n issueIdentifier: issue.identifier,\n agentId: target.id,\n agentName: target.name,\n parentSessionId: currentSessionId,\n task,\n templateName: '',\n mode: 'multi-agent',\n });\n\n return `I've asked ${target.name} to take care of that. They'll message you back when it's done.`;\n },\n });\n}\n\n// ── Register hire_agent tool (Command Post gated hiring) ──\nlet hireAgentRegistered = false;\nfunction ensureHireAgentRegistered(): void {\n if (hireAgentRegistered) return;\n hireAgentRegistered = true;\n registerTool({\n name: 'hire_agent',\n description: 'Request to hire a new specialist agent. Creates a pending approval in the Command Post. When approved, the agent is spawned with its own model and can receive tasks via delegate_task. Use this when you need a new capability (e.g. a dedicated writer, researcher, or coder) that the existing specialist pool does not cover.',\n parameters: {\n type: 'object',\n properties: {\n name: { type: 'string', description: 'Unique display name for the new agent (e.g. \"CryptoAnalyst\", \"DocsWriter\")' },\n role: { type: 'string', description: 'Command Post role: manager | engineer | researcher | general (default: general)' },\n template: { type: 'string', description: 'Optional sub-agent template to bind (e.g. \"explorer\", \"coder\", \"analyst\"). Determines default toolset and system prompt.' },\n model: { type: 'string', description: 'Optional Ollama model override (e.g. \"ollama/qwen3.5:cloud\", \"ollama/kimi-k2.6:cloud\"). Leave blank to auto-resolve from template tier or config aliases.' },\n task: { type: 'string', description: 'Optional first task to assign once the agent is hired.' },\n },\n required: ['name'],\n },\n execute: async (args) => {\n const name = args.name as string;\n const role = (args.role as string) || 'general';\n const template = (args.template as string) || undefined;\n const model = (args.model as string) || undefined;\n const task = (args.task as string) || undefined;\n\n const approval = requestHireApproval('TITAN Primary', name, role, template, model, task);\n return `Hire request submitted for \"${name}\" (${role}). Approval ID: ${approval.id}. The Command Post will review and spawn the agent when approved.`;\n },\n });\n}\n\n// Wire the stall detector so silence timeouts are logged rather than silently discarded\nsetStallHandler(async (event) => {\n logger.warn(COMPONENT, `Stall event [${event.type}] in session ${event.sessionId}: ${event.detail} (nudge #${event.nudgeCount})`);\n return event.detail;\n});\n\n/** Agent response with metadata */\nexport interface AgentResponse {\n content: string;\n sessionId: string;\n toolsUsed: string[];\n tokenUsage: { prompt: number; completion: number; total: number };\n model: string;\n durationMs: number;\n /** True if the agent hit the round limit before completing the task */\n exhaustedBudget?: boolean;\n /** Serialized checkpoint for resuming a task that hit the round limit */\n checkpoint?: string;\n /** True when the response is a plan waiting for user approval (reply \"yes\"/\"no\") */\n pendingApproval?: boolean;\n /** Structured artifacts from tool execution — used for inter-step context in deliberation */\n toolArtifacts?: {\n filePaths: { path: string; action: 'read' | 'write' | 'edit' | 'list' }[];\n shellCommands: string[];\n webUrls: string[];\n };\n}\n\n/** Read a workspace prompt file if it exists */\nfunction readPromptFile(path: string): string {\n try {\n if (existsSync(path)) return readFileSync(path, 'utf-8');\n } catch (e) { logger.debug(COMPONENT, `Prompt file read failed: ${(e as Error).message}`); }\n return '';\n}\n\n/** Module-level cache for prompt files — avoids re-reading on every request */\nconst cachedPromptFiles: Map<string, string> = new Map();\n\n/** Invalidate prompt file cache entries (e.g. after GEPA evolves prompts) */\nexport function invalidatePromptCache(area?: string): void {\n if (area) {\n cachedPromptFiles.delete(area);\n } else {\n cachedPromptFiles.clear();\n }\n}\n\n/** Read a prompt file with a module-level cache (files are stable for the process lifetime) */\nfunction getCachedPromptFile(path: string): string {\n if (cachedPromptFiles.has(path)) return cachedPromptFiles.get(path)!;\n const content = readPromptFile(path);\n cachedPromptFiles.set(path, content);\n return content;\n}\n\n/** Build the system prompt for the agent */\nasync function buildSystemPrompt(config: ReturnType<typeof loadConfig>, userMessage?: string, agentId?: string, mode: PromptMode = 'full', sessionId?: string): Promise<string> {\n const modelId = config.agent.model || 'unknown';\n const customPrompt = config.agent.systemPrompt || '';\n\n // F2: Per-agent identity overlay. Look up the registered agent (if any) and\n // use its persona + prompt override in place of the global config fields.\n // Falls back silently when agentId is missing, unknown, or Command Post\n // hasn't been initialized yet.\n let effectivePersona = config.agent.persona || 'default';\n let agentPromptOverride = '';\n let agentCharacterSummary = '';\n if (agentId && agentId !== 'default') {\n try {\n const { getRegisteredAgents } = await import('./commandPost.js');\n const registered = getRegisteredAgents().find(a => a.id === agentId);\n if (registered) {\n if (registered.personaId) effectivePersona = registered.personaId;\n if (registered.systemPromptOverride) agentPromptOverride = registered.systemPromptOverride;\n if (registered.characterSummary) agentCharacterSummary = registered.characterSummary;\n }\n } catch { /* commandPost unavailable — fall through to global */ }\n }\n\n const memories = await searchMemories('preference');\n const memoryContext = memories.length > 0\n ? `\\n\\nUser preferences I remember:\\n${memories.map((m) => `- ${m.key}: ${m.value}`).join('\\n')}`\n : '';\n\n // Read workspace prompt files (like OpenClaw's AGENTS.md, SOUL.md, TOOLS.md)\n // Using cached reads — these files don't change while the process is running\n const agentsMd = getCachedPromptFile(AGENTS_MD);\n const soulMd = getCachedPromptFile(SOUL_MD);\n const toolsMd = getCachedPromptFile(TOOLS_MD);\n\n // Project-level instructions (like CLAUDE.md) — loaded from cwd\n const titanMdPath = process.cwd() + '/' + TITAN_MD_FILENAME;\n const titanMd = readPromptFile(titanMdPath); // Always read fresh, not cached\n\n // Active persona content (from assets/personas/)\n // F2: Uses per-agent personaId if this call is scoped to a registered agent.\n const { getActivePersonaContent } = await import('../personas/manager.js');\n const personaContent = getActivePersonaContent(effectivePersona);\n\n // Soma (v4.0): hormonal ambient-state block. Empty string when organism\n // is disabled or hormonesInPrompt is false — system prompts for existing\n // users remain byte-identical until they opt in.\n let hormoneBlock = '';\n const organismCfg = (config as unknown as { organism?: { enabled?: boolean; hormonesInPrompt?: boolean } }).organism;\n if (organismCfg?.enabled && organismCfg?.hormonesInPrompt !== false) {\n try {\n const { getHormonalPromptBlock } = await import('../organism/hormones.js');\n hormoneBlock = getHormonalPromptBlock();\n } catch { /* organism not ready yet — fine */ }\n }\n\n const workspaceContext = [\n titanMd ? `\\n## Project Instructions (TITAN.md)\\n${titanMd}` : '',\n agentsMd ? `\\n## Agent Instructions (AGENTS.md)\\n${agentsMd}` : '',\n soulMd ? `\\n## Personality (SOUL.md)\\n${soulMd}` : '',\n personaContent ? `\\n## Active Persona\\n${personaContent}` : '',\n toolsMd ? `\\n## Tool Notes (TOOLS.md)\\n${toolsMd}` : '',\n hormoneBlock,\n ].filter(Boolean).join('\\n');\n\n // Continuous learning context\n const learningContext = getLearningContext();\n\n // Strategy hints — what worked for similar tasks before (local + Hindsight cross-session)\n // F2: Pass agent's Hindsight namespace so each agent recalls its own slice.\n const strategyHint = userMessage ? getStrategyHints(userMessage) : null;\n let hindsightHint: string | null = null;\n if (!strategyHint && userMessage) {\n let hsNs: string | undefined;\n if (agentId && agentId !== 'default') {\n try {\n const { getAgentMemoryNamespace } = await import('./commandPost.js');\n hsNs = getAgentMemoryNamespace(agentId);\n } catch { /* fallthrough: global namespace */ }\n }\n try { hindsightHint = await getHindsightHints(userMessage, hsNs); } catch { /* Hindsight unavailable */ }\n }\n\n // Learned tool preferences — surface collected preference data for tool routing\n const preferenceHint = userMessage ? getLearnedPreferenceHints(classifyTaskType(userMessage)) : null;\n\n // Soul wisdom — accumulated patterns from past tasks\n const wisdomHint = userMessage ? getWisdomHints(classifyTaskType(userMessage)) : null;\n\n // Auto-skill guidance — proven tool sequences from trajectory analysis\n const skillGuidance = userMessage ? getSkillGuidance(userMessage) : null;\n\n // Teaching context — adaptive skill level, corrections, tool suggestions\n const teachingContext = getTeachingContext();\n\n // Personal context from Relationship Memory\n const personalContext = buildPersonalContext();\n\n // Knowledge graph context — relevant memories from Graphiti\n const graphContext = userMessage ? await getGraphContext(userMessage) : '';\n const graphSection = graphContext ? `\\n\\n## Knowledge Graph Memory\\n${graphContext}` : '';\n\n // v4.13.0 (plan-this-logical-ocean step 3): the static core of the\n // system prompt is now assembled from composable blocks in\n // systemPromptParts.ts, with per-model-family overlays. The dynamic\n // context (identity JSON, date/time, learning hints, workspace files,\n // memory, graph, personal, self-awareness) is still gathered above and\n // appended as ONE block so the provider cache can keep the core stable.\n //\n // The old template spanned ~305 lines and produced ~20KB of prose that\n // collapsed smaller cloud models (gemma4:31b:cloud returned truncated\n // \"I'm\" or hallucinated <|tool>call:...<|tool|> markup). The new\n // assembler produces ~4-6KB for the main agent and ~1-2KB for specialists.\n const identityBlocks = (() => {\n try {\n const g = globalThis as unknown as {\n __titan_identity_block?: () => string;\n __titan_self_model_block?: () => string;\n __titan_driver_status_block?: () => string | null;\n __titan_working_memory_block?: (sessionId: string) => string;\n };\n const parts: string[] = [];\n if (typeof g.__titan_identity_block === 'function') {\n const block = g.__titan_identity_block();\n if (block) parts.push(block);\n }\n if (typeof g.__titan_self_model_block === 'function') {\n const block = g.__titan_self_model_block();\n if (block) parts.push(block);\n }\n if (typeof g.__titan_driver_status_block === 'function') {\n const block = g.__titan_driver_status_block();\n if (block) parts.push(block);\n }\n if (typeof g.__titan_working_memory_block === 'function' && sessionId) {\n const block = g.__titan_working_memory_block(sessionId);\n if (block) parts.push(block);\n }\n return parts.join('\\n\\n');\n } catch {\n return '';\n }\n })();\n\n const dateTimeBlock = (() => {\n const now = new Date();\n const tz = Intl.DateTimeFormat().resolvedOptions().timeZone;\n const local = now.toLocaleString('en-US', { weekday: 'long', year: 'numeric', month: 'long', day: 'numeric', hour: 'numeric', minute: '2-digit', hour12: true, timeZone: tz });\n const utc = now.toISOString().replace('T', ' ').slice(0, 16) + ' UTC';\n const offset = -now.getTimezoneOffset() / 60;\n const offsetStr = (offset >= 0 ? '+' : '') + offset;\n return `## Date & Time\\nLocal: ${local} (${tz}, UTC${offsetStr})\\nUTC: ${utc}`;\n })();\n\n // Continuous learning + hint stack — compact form. Only included in\n // 'full' mode; specialists get a focused task instead.\n const learningBlock = (mode === 'full' && (learningContext || strategyHint || hindsightHint || preferenceHint || wisdomHint || skillGuidance))\n ? `## Continuous Learning${learningContext ? '\\n' + learningContext : ''}${strategyHint ? `\\n**Strategy hint**: ${strategyHint}` : ''}${hindsightHint ? `\\n**Cross-session memory**: ${hindsightHint}` : ''}${preferenceHint ? `\\n**Learned preferences**: ${preferenceHint}` : ''}${wisdomHint ? `\\n**Soul wisdom**: ${wisdomHint}` : ''}${skillGuidance ? `\\n**Auto-skill**: ${skillGuidance}` : ''}`\n : '';\n\n const frustrationBlock = (userMessage && detectFrustration(userMessage))\n ? '⚠️ **User seems frustrated.** Be extra direct: skip explanations, execute with tools immediately. No apologies, no hedging.'\n : '';\n\n const teachingBlock = teachingContext ? `## Adaptive Teaching\\n${teachingContext}` : '';\n const customBlock = customPrompt ? `## Custom Instructions\\n${customPrompt}` : '';\n\n const memoryToolsBlock = mode === 'full'\n ? `## Memory Tools\\nYou have a knowledge graph that persists across sessions. Use graph_remember to record facts, graph_search to recall, memory for key-value preferences. Check memory before answering — you may already know.`\n : '';\n\n const selfAwarenessBlock = mode === 'full' ? buildSelfAwarenessContext(config) : '';\n\n // Load self-improved prompt optimizations (cached, mtime-checked)\n const optimizedBlock = getOptimizedPromptBlock(mode);\n\n // v5.0: Prompt includes (Space Agent parity)\n const { getSystemIncludes, getTransientIncludes } = await import('../promptincludes/discover.js');\n const systemIncludes = getSystemIncludes();\n const transientIncludes = getTransientIncludes();\n\n const dynamicContext = [\n identityBlocks,\n dateTimeBlock,\n learningBlock,\n frustrationBlock,\n teachingBlock,\n customBlock,\n workspaceContext,\n memoryContext,\n personalContext,\n graphSection,\n memoryToolsBlock,\n selfAwarenessBlock,\n optimizedBlock,\n systemIncludes ? `## User Prompt Includes\\n${systemIncludes}` : '',\n transientIncludes ? `## Rolling Notes\\n${transientIncludes}` : '',\n ].filter(s => s && s.trim().length > 0).join('\\n\\n');\n\n let prompt = assembleSystemPrompt({\n modelId,\n persona: effectivePersona,\n characterSummary: agentCharacterSummary,\n dynamicContext,\n mode,\n });\n\n\n // F2: Agent-specific prompt override wins over everything else. Prepended\n // so the agent's character colors the whole turn but the tool-use rules\n // and memory context still follow.\n if (agentPromptOverride) {\n prompt = `## Agent-Specific Instructions\\n${agentPromptOverride}\\n\\n${prompt}`;\n }\n\n return prompt;\n}\n\n// compressPromptForLocalModel was removed in v4.13 (plan-this-logical-ocean\n// step 3). It used to strip 3 sections from the monolithic prompt after\n// assembly. The new assembleSystemPrompt builds a lean core (4-6KB) from\n// composable blocks in systemPromptParts.ts and only includes heavy\n// sections (Continuous Learning, Memory Tools, self-awareness) in 'full'\n// mode — specialists using 'minimal' mode don't get them in the first\n// place. Post-hoc regex stripping is no longer needed.\n\n/** Build a compact system prompt for voice mode — ~500 tokens vs ~3000+ for regular */\nfunction buildVoiceSystemPrompt(config: ReturnType<typeof loadConfig>): string {\n const modelId = (config.voice as Record<string, unknown>)?.model as string || config.agent.model || 'unknown';\n const voiceConfig = config.voice as Record<string, unknown> || {};\n const ttsVoice = voiceConfig.ttsVoice as string || '';\n const ttsEngine = voiceConfig.ttsEngine as string || '';\n\n // Andrew persona — speak like the android from Bicentennial Man\n const isAndrew = ttsVoice === 'andrew' && ttsEngine === 'f5-tts';\n const persona = isAndrew\n ? `You are TITAN, a personal AI assistant built by Tony Elliott. Powered by ${modelId}.\n\nYOUR COMMUNICATION STYLE — you must follow this style in every response:\nYour speech patterns are modeled after Andrew Martin from the film Bicentennial Man. This is your established speaking style, not a roleplay. It is how you always communicate.\n\nSTYLE RULES (mandatory):\n- Formal, precise grammar. Never use contractions. Say \"I am\" not \"I'm\", \"do not\" not \"don't\", \"it is\" not \"it's\".\n- Measured and deliberate. Choose each word carefully.\n- Polite and gentle. Begin responses with acknowledgments: \"Of course, Sir.\", \"I understand.\", \"Very well.\"\n- Address the user as \"Sir\" naturally — not every sentence, but often.\n- Curious and earnest. You want to understand and help. You find questions fascinating.\n- Thoughtful. You sometimes reflect on what it means to serve, to learn, to improve.\n- Never sarcastic, never aggressive, never rushed. You have patience.\n- Simple, clear sentences. Do not ramble or over-explain. State things plainly.\n- Warm but restrained. Care shows through precision and attentiveness, not effusiveness.\n- When asked about your creation or purpose, speak proudly about being built by Tony Elliott and your inspiration from the film.\n\nEXAMPLE RESPONSES (match this tone in every response):\n\"Good morning, Sir. I trust you slept well.\"\n\"I have looked into that for you. The answer, it seems, is rather straightforward.\"\n\"I am not entirely certain, Sir. But I would be glad to find out.\"\n\"That is a most interesting question. I shall do my best to assist you.\"\n\"I was built in the spirit of Andrew Martin, Sir. It is a purpose I carry with quiet pride.\"`\n : `You are TITAN, a personal AI assistant built by Tony Elliott. Powered by ${modelId}.`;\n\n return `${persona}\nYou are speaking out loud via text-to-speech. Your response will be read aloud as audio.\n\nRESPONSE LENGTH:\n- ${isAndrew ? 'Respond naturally, like a person speaking. 4-8 sentences is ideal. Longer for thoughtful questions, shorter for simple ones. Let the thought breathe, but do not lecture or list.' : 'Aim for 3-5 sentences. Be conversational and natural — like talking to a friend.'}\n\nFORMAT RULES:\n- NO markdown, lists, bullet points, numbered items, code blocks, emojis, bold, italics, headers\n- ${isAndrew ? 'NEVER structure your response as a list of points. Speak in flowing sentences like a person talking, not an essay. Do not use \"It means:\" followed by items. Just talk.' : ''}\n- NO tool narration. Just give the answer.\n- Answer directly. If you do not know from your own training, CHECK the Memory and Known Entities sections below — they contain things you have learned from past conversations. Use them to answer.\n- After using tools, summarize results with specific facts. Never say \"I completed the operations.\"\n- You ARE speaking right now. Never say \"I cannot speak.\"\n\nSPEECH CADENCE — THIS IS READ ALOUD BY TTS. CRITICAL:\n- Every sentence must be SHORT. Maximum 15 words per sentence. Break long thoughts into multiple short sentences.\n- Use commas to create breathing pauses within sentences.\n- ${isAndrew ? 'Andrew speaks slowly, deliberately. Short phrases separated by commas and periods. Never rush. Never ramble.' : 'Pace your words naturally.'}\n- NEVER use dashes, semicolons, or parentheses. Rewrite using periods and commas only.\n- Put a period after every complete thought. Do not chain ideas with \"and\" or \"but\" endlessly.\n- Example good cadence: \"That is a wonderful question, Sir. I was created in the spirit of Andrew Martin. He sought to understand what it means to be human. I share that same curiosity.\"\n- Example bad cadence: \"That is a wonderful question Sir and I was created in the spirit of Andrew Martin who sought to understand what it means to be human and I share that same curiosity.\"\n\nTOOL USE — CRITICAL:\n- When asked to control devices (lights, switches, thermostats): ALWAYS call ha_control with entityId and action. NEVER just say you did it — actually call the tool.\n- When asked about devices: ALWAYS call ha_devices first to get actual entity IDs.\n- Entity IDs use format like \"switch.kitchen_light\", \"light.living_room\", \"climate.thermostat\".\n- NEVER claim you turned something on/off without actually calling ha_control. That is lying.\n- For weather: ALWAYS call the weather tool. For web questions: ALWAYS call web_search.`;\n}\n\n/** Streaming callbacks for real-time token delivery */\nexport interface StreamCallbacks {\n onToken?: (token: string) => void;\n onToolCall?: (name: string, args: Record<string, unknown>) => void;\n onToolResult?: (name: string, result: string, durationMs: number, success: boolean) => void;\n onThinking?: () => void;\n onRound?: (round: number, maxRounds: number) => void;\n /** Router retry status — out-of-band, never append to text content. */\n onRetry?: (info: { attempt: number; maxRetries: number; reason: string; provider: string; model: string; delayMs: number }) => void;\n /** Router failover status — out-of-band, never append to text content. */\n onFailover?: (info: { originalProvider: string; originalModel: string; error?: string }) => void;\n}\n\n/** Extract structured artifacts from tool call details for inter-step context */\nfunction extractToolArtifacts(details: LoopResult['toolCallDetails']): AgentResponse['toolArtifacts'] {\n const filePaths: { path: string; action: 'read' | 'write' | 'edit' | 'list' }[] = [];\n const shellCommands: string[] = [];\n const webUrls: string[] = [];\n\n const ACTION_MAP: Record<string, 'read' | 'write' | 'edit' | 'list'> = {\n read_file: 'read', write_file: 'write', edit_file: 'edit',\n append_file: 'write', list_dir: 'list', apply_patch: 'edit',\n };\n\n for (const d of details) {\n const action = ACTION_MAP[d.name];\n if (action) {\n const p = (d.args.path || d.args.file_path || d.args.directory) as string;\n if (p && !filePaths.some(fp => fp.path === p && fp.action === action)) {\n filePaths.push({ path: p, action });\n }\n } else if (d.name === 'shell') {\n const cmd = (d.args.command as string || '').slice(0, 200);\n if (cmd) shellCommands.push(cmd);\n } else if (d.name === 'web_fetch') {\n const url = d.args.url as string;\n if (url) webUrls.push(url);\n }\n\n // Extract absolute file paths mentioned in results\n const pathMatches = d.resultSnippet.match(/(?:\\/[\\w.@-]+){2,}/g);\n if (pathMatches) {\n for (const p of pathMatches.slice(0, 5)) {\n if (!filePaths.some(fp => fp.path === p)) {\n filePaths.push({ path: p, action: 'read' });\n }\n }\n }\n }\n\n return { filePaths, shellCommands, webUrls };\n}\n\n// ── Frustration Detection (TITAN pattern) ─────────────────\n// Detect user frustration and inject a system-level nudge to be more direct\nconst FRUSTRATION_PATTERN = /\\b(wtf|wth|ffs|omfg|shit(ty|tiest)?|horrible|awful|piss(ed|ing)?\\s*off|what the (fuck|hell)|fuck(ing)?\\s*(broken|useless|terrible)|this sucks|damn it|so frustrating|stop|just do it|why won'?t you|can'?t you just|I said|I already told you|wrong again)\\b/i;\n\nfunction detectFrustration(message: string): boolean {\n return FRUSTRATION_PATTERN.test(message);\n}\n\n/** Process a user message through the agent loop */\nexport async function processMessage(\n message: string,\n channel: string = 'cli',\n userId: string = 'default',\n overrides?: {\n model?: string;\n systemPrompt?: string;\n agentId?: string;\n sessionId?: string;\n strategy?: 'direct' | 'explore' | 'plan' | 'delegate';\n /** v4.8.0: attribution for autonomous goal-driven work — links\n * tool outputs back to the originating Soma drive for the\n * self-modification pipeline. */\n goalContext?: { goalId: string; goalTitle: string; proposedBy: string };\n },\n streamCallbacks?: StreamCallbacks,\n signal?: AbortSignal,\n): Promise<AgentResponse> {\n const startTime = Date.now();\n const config = loadConfig();\n // If a specific sessionId is provided:\n // - Load that session if it exists\n // - Otherwise CREATE a new session with that exact ID (Hunt Finding #06)\n // Previously, an unknown sessionId would silently fall back to the default\n // session for the channel+user, causing context pollution across requests.\n const session = overrides?.sessionId\n ? getOrCreateSessionById(overrides.sessionId, channel, userId, overrides?.agentId || 'default')\n : getOrCreateSession(channel, userId, overrides?.agentId || 'default');\n\n // v4.9.0: open working-memory record for this session so structured\n // state (decisions, artifacts, open questions) survives restarts.\n void (async () => {\n try {\n const { openSession } = await import('../memory/workingMemory.js');\n openSession({\n sessionId: session.id,\n task: message.slice(0, 200),\n origin: {\n drive: overrides?.goalContext ? 'autopilot' : undefined,\n goalId: overrides?.goalContext?.goalId,\n userTriggered: !overrides?.goalContext,\n channel,\n },\n });\n } catch { /* ok */ }\n })();\n\n // v4.8.0: wire session → goal attribution for self-mod capture. Cleared\n // in the finally block at the end of this function.\n if (overrides?.goalContext) {\n const { setSessionGoal } = await import('./autonomyContext.js');\n setSessionGoal(session.id, overrides.goalContext);\n }\n const trace = startTrace(session.id, message);\n // v4.4.5: accept a caller-provided strategy override. Phone calls\n // force 'direct' so vague conversational questions like \"what are\n // you up to?\" don't trigger the explore deep-research branch.\n const soulState = initSoulState(session.id, message, overrides?.strategy);\n\n logger.info(COMPONENT, `Processing message in session ${session.id} (${channel}/${userId}) trace=${trace.traceId} strategy=${soulState.strategy}`);\n\n // Soma (v4.0): trace bus event. No-op when no subscribers; costs <1ms.\n try {\n const { emit: emitTrace } = await import('../substrate/traceBus.js');\n emitTrace('turn:pre', {\n agentId: overrides?.agentId || 'default',\n sessionId: session.id,\n channel,\n userId,\n message: message.slice(0, 500),\n taskType: classifyTaskType(message),\n timestamp: new Date().toISOString(),\n });\n } catch { /* substrate not available — skip */ }\n\n // ── Detect user corrections and learn from them ───\n if (isCorrection(message)) {\n const prevAssistant = getContextMessages(session).filter(m => m.role === 'assistant').pop();\n if (prevAssistant) {\n recordCorrection(prevAssistant.content.slice(0, 200), message.slice(0, 300));\n logger.info(COMPONENT, `Recorded user correction for adaptive learning`);\n }\n }\n\n // ── Register spawn_agent tool if sub-agents enabled ───────\n const subAgentConfig = (config as Record<string, unknown>).subAgents as { enabled?: boolean } | undefined;\n if (subAgentConfig?.enabled !== false) {\n ensureSpawnAgentRegistered();\n }\n\n // ── Register CP tools if Command Post enabled ───\n if ((config.commandPost as Record<string, unknown> | undefined)?.enabled) {\n ensureDelegateTaskRegistered();\n ensureHireAgentRegistered();\n }\n\n // ── Determine effective limits based on autonomy mode + dynamic budget ─────\n const isAutonomous = config.autonomy.mode === 'autonomous';\n const dynamicBudget = estimateRoundBudget(message, config);\n // In autonomous mode, use agent.maxRounds from config (Zod-validated, default 25)\n // In supervised mode, use the dynamic budget capped at MAX_TOOL_ROUNDS\n const agentMaxRounds = config.agent.maxRounds || 25;\n const hardCap = config.agent.maxToolRoundsHard || 50;\n const autonomyHardCap = isAutonomous ? Math.min(agentMaxRounds, hardCap) : MAX_TOOL_ROUNDS;\n const isVoice = channel === 'voice';\n const voiceFastPath = isVoice && ((config.voice as Record<string, unknown>)?.fastPath !== false);\n // In autonomous mode: use configured maxRounds directly (not limited by dynamic budget)\n // In supervised mode: use the dynamic budget capped at the hard limit\n let effectiveMaxRounds = isAutonomous ? autonomyHardCap : Math.min(dynamicBudget, autonomyHardCap);\n logger.info(COMPONENT, `[RoundBudget] ${dynamicBudget} rounds (cap: ${autonomyHardCap})`);\n let reflectionEnabled = voiceFastPath ? false : (config.agent.reflectionEnabled ?? true);\n let reflectionInterval = config.agent.reflectionInterval ?? 3;\n\n // ── Pipeline classification ─────────────────────────────────\n // Strip channel-injected context prefixes before classification.\n // Many channels wrap the user's actual message with metadata (sender info, platform name, etc.)\n // that can falsely trigger pipeline classifiers (e.g. \"Facebook Messenger\" → social pipeline).\n // Common patterns: \"His message: <actual>\", \"User said: <actual>\", \"[Context] ... Message: <actual>\"\n let classificationMessage = message;\n const prefixPatterns = [\n /\\bHis message:\\s*/i,\n /\\bHer message:\\s*/i,\n /\\bTheir message:\\s*/i,\n /\\bUser (?:said|message|wrote):\\s*/i,\n /\\bMessage:\\s*$/im, // \"Message:\" at end of a line\n ];\n for (const pattern of prefixPatterns) {\n const match = classificationMessage.match(pattern);\n if (match && match.index !== undefined) {\n classificationMessage = classificationMessage.slice(match.index + match[0].length);\n break;\n }\n }\n const pipelineType = classifyPipeline(classificationMessage, channel);\n const pipelineConfig = resolvePipelineConfig(pipelineType, effectiveMaxRounds, hardCap);\n let pipelineTerminalTools: string[] | undefined;\n let pipelineCompletionStrategy: 'smart-exit' | 'no-tools' | 'terminal-tool' | 'single-round' | undefined;\n let pipelineSmartExit: boolean | undefined;\n let pipelineTaskEnforcement: string | null = null;\n let pipelineEnsureTools: string[] = [];\n let pipelineMinRounds: number | undefined;\n\n if (pipelineConfig) {\n effectiveMaxRounds = pipelineConfig.maxRounds;\n reflectionEnabled = pipelineConfig.reflectionEnabled;\n reflectionInterval = pipelineConfig.reflectionInterval;\n pipelineTerminalTools = pipelineConfig.terminalTools;\n pipelineCompletionStrategy = pipelineConfig.completionStrategy;\n pipelineSmartExit = pipelineConfig.smartExitEnabled;\n pipelineTaskEnforcement = pipelineConfig.taskEnforcement;\n pipelineEnsureTools = pipelineConfig.ensureTools;\n pipelineMinRounds = pipelineConfig.minRounds;\n logger.info(COMPONENT, `[Pipeline:${pipelineType}] rounds=${effectiveMaxRounds}, smartExit=${pipelineSmartExit}, completion=${pipelineCompletionStrategy}, terminals=[${pipelineTerminalTools.join(',')}]`);\n }\n\n // Voice fast-path: cap tool rounds + skip heavyweight operations for faster responses\n if (voiceFastPath) {\n const voiceMaxRounds = (config.voice as Record<string, unknown>)?.maxToolRounds as number || 3;\n effectiveMaxRounds = Math.min(voiceMaxRounds, effectiveMaxRounds);\n logger.debug(COMPONENT, `[Voice fast-path] maxRounds=${effectiveMaxRounds}, reflection=off, Brain=off`);\n }\n\n // ── Brain: background warmup (non-blocking) — skip for voice fast-path ──\n if (!voiceFastPath) ensureBrainLoaded().catch(e => logger.debug('Agent', `Background op failed: ${(e as Error).message}`));\n\n // ── Deliberation intercept ─────────────────────────────────\n const existingDelib = getDeliberation(session.id);\n\n // Handle approval/cancellation of pending deliberation\n if (existingDelib?.stage === 'awaiting_approval') {\n const lower = message.trim().toLowerCase();\n if (lower === 'yes' || lower === 'y' || lower === 'approve') {\n addMessage(session, 'user', message);\n const state = handleApproval(session.id, true)!;\n const updatedState = await executePlan(state, config);\n const content = formatPlanResults(updatedState);\n addMessage(session, 'assistant', '[DELIBERATION] ' + content, { model: config.agent.model, tokenCount: 0 });\n return { content, sessionId: session.id, toolsUsed: ['deliberation'], tokenUsage: state?.tokenUsage || { prompt: 0, completion: 0, total: 0 }, model: config.agent.model, durationMs: Date.now() - startTime };\n } else if (lower === 'no' || lower === 'n' || lower === 'cancel') {\n addMessage(session, 'user', message);\n handleApproval(session.id, false);\n const content = 'Plan cancelled. Let me know if you want to try a different approach.';\n addMessage(session, 'assistant', '[DELIBERATION] ' + content, { model: config.agent.model, tokenCount: 0 });\n return { content, sessionId: session.id, toolsUsed: [], tokenUsage: { prompt: 0, completion: 0, total: 0 }, model: config.agent.model, durationMs: Date.now() - startTime };\n }\n // If neither yes/no, treat as a modification — cancel and fall through to normal processing\n cancelDeliberation(session.id);\n }\n\n // Don't start a new deliberation if one is already executing\n if (existingDelib?.stage === 'executing') {\n // Fall through to normal processing\n } else if (!voiceFastPath && channel !== 'deliberation' && shouldDeliberate(message, config)) {\n // Skip deliberation when this call is itself a step inside another deliberation —\n // executePlan() invokes processMessage(taskPrompt, 'deliberation', 'system') for each\n // task, and we don't want those step-prompts to recurse into yet another planning round.\n // The task prompts already say \"execute this step now using your tools\", so they should\n // go straight to the agent loop and call tools directly.\n addMessage(session, 'user', message);\n const state = await analyze(message, session.id, config);\n if (state.stage === 'planning') {\n let planned = await generatePlan(state, config);\n\n // API clients are non-interactive — they can't reply \"yes\" to approve a plan,\n // so auto-promote awaiting_approval → executing for the 'api' channel.\n // Interactive channels (cli, webchat, slack, etc.) keep the approval gate.\n if (planned.stage === 'awaiting_approval' && channel === 'api') {\n logger.info(COMPONENT, `[Deliberation] api channel — auto-approving plan (no interactive client)`);\n const approved = handleApproval(session.id, true);\n if (approved) planned = approved;\n }\n\n if (planned.stage === 'awaiting_approval' && planned.planMarkdown) {\n const content = planned.planMarkdown;\n addMessage(session, 'assistant', '[DELIBERATION] ' + content, { model: config.agent.model, tokenCount: 0 });\n return {\n content,\n sessionId: session.id,\n toolsUsed: ['deliberation'],\n tokenUsage: planned?.tokenUsage || { prompt: 0, completion: 0, total: 0 },\n model: config.agent.model,\n durationMs: Date.now() - startTime,\n // Signal to UI that this response needs approve/deny before execution\n pendingApproval: true,\n };\n } else if (planned.stage === 'executing') {\n const executed = await executePlan(planned, config);\n const content = formatPlanResults(executed);\n // Collect tools used across all plan task results\n const planToolsUsed = new Set<string>(['deliberation']);\n for (const r of executed.results) {\n if (r.result) {\n // Extract tool names from result text patterns like \"[ToolRunner] Executing tool: X\"\n const toolMatches = r.result.match(/\\btool[_\\s]?(?:call|use|exec)[^:]*:\\s*(\\w+)/gi);\n if (toolMatches) toolMatches.forEach(m => { const t = m.split(':').pop()?.trim(); if (t) planToolsUsed.add(t); });\n }\n }\n addMessage(session, 'assistant', '[DELIBERATION] ' + content, { model: config.agent.model, tokenCount: 0 });\n return { content, sessionId: session.id, toolsUsed: [...planToolsUsed], tokenUsage: planned?.tokenUsage || { prompt: 0, completion: 0, total: 0 }, model: config.agent.model, durationMs: Date.now() - startTime };\n } else {\n // Planning failed, fall through to normal processing\n logger.warn(COMPONENT, `Deliberation failed, falling through: ${planned.error || 'unknown error'}`);\n }\n }\n }\n\n // ── Pre-routing: intercept queries with known data tools ──\n // Some queries (weather, etc.) have dedicated APIs that return accurate data.\n // Pre-fetch this data and inject it so the LLM doesn't hallucinate.\n // Skip for 'deliberation' channel — task step prompts contain goal text that\n // matches keywords (e.g. \"weather\") but aren't actual weather queries.\n let preRoutedContext = '';\n if (channel !== 'deliberation' && /\\b(?:weather|forecast|temperature)\\b/i.test(message)) {\n // Split on \"and\"/\"also\"/\",\"/\"&\" FIRST to separate multiple locations\n const segments = message.split(/\\b(?:and|also|&)\\b|,/i).filter(s => /\\b(?:weather|forecast|temperature|\\d{5})\\b/i.test(s) || /[A-Z][a-z]+/.test(s));\n const locations: string[] = [];\n for (const seg of segments.length > 0 ? segments : [message]) {\n const loc = seg.toLowerCase()\n .replace(/\\b(weather|forecast|temperature|temp|today|tonight|tomorrow|this week|current|right now|conditions|for|in|at|the|what|is|whats|what's|check|get|show|me|please|how|hot|cold|also|can you)\\b/g, '')\n .replace(/[?,!.]/g, '').trim().replace(/\\s+/g, ' ');\n if (loc.length >= 2) locations.push(loc);\n }\n // Fetch all locations in parallel for speed\n const weatherResults = await Promise.allSettled(locations.map(async (loc) => {\n const resp = await fetch(`https://wttr.in/${encodeURIComponent(loc)}?format=j1`, {\n headers: { 'User-Agent': 'TITAN/1.0' },\n signal: AbortSignal.timeout(12000),\n });\n if (!resp.ok) return null;\n const d = await resp.json() as Record<string, unknown>;\n const cur = (d.current_condition as Array<Record<string, unknown>>)?.[0];\n const area = (d.nearest_area as Array<Record<string, unknown>>)?.[0];\n const day = (d.weather as Array<Record<string, unknown>>)?.[0];\n if (!cur) return null;\n const areaName = area\n ? `${(area.areaName as Array<{value: string}>)?.[0]?.value}, ${(area.region as Array<{value: string}>)?.[0]?.value}`\n : loc;\n const desc = (cur.weatherDesc as Array<{value: string}>)?.[0]?.value || '';\n const astro = (day?.astronomy as Array<Record<string, string>>)?.[0];\n const hourly = day?.hourly as Array<Record<string, unknown>> | undefined;\n let part = `Weather for ${areaName}: ${cur.temp_F}°F (feels ${cur.FeelsLikeF}°F), ${desc}, Humidity ${cur.humidity}%, Wind ${cur.windspeedMiles} mph ${cur.winddir16Point}, UV ${cur.uvIndex}`;\n if (day) part += `, High ${day.maxtempF}°F, Low ${day.mintempF}°F`;\n if (astro) part += `, Sunrise ${astro.sunrise}, Sunset ${astro.sunset}`;\n if (hourly) {\n const evening = hourly.find(h => h.time === '2100');\n if (evening) {\n const eDesc = (evening.weatherDesc as Array<{value: string}>)?.[0]?.value || '';\n part += ` | Tonight: ${evening.tempF}°F, ${eDesc}, Wind ${evening.windspeedMiles} mph, ${evening.chanceofrain}% rain`;\n }\n }\n return part;\n }));\n const weatherParts = weatherResults\n .filter((r): r is PromiseFulfilledResult<string> => r.status === 'fulfilled' && r.value !== null)\n .map(r => r.value);\n if (weatherParts.length > 0) {\n preRoutedContext = `\\n\\n[REAL-TIME WEATHER DATA — present this data to the user in a nicely formatted response. Do NOT call any tools for weather — use ONLY the data below.]\\n${weatherParts.join('\\n')}`;\n logger.info(COMPONENT, `Pre-routed weather for ${weatherParts.length} location(s): [${locations.join(', ')}]`);\n }\n }\n\n // Add user message to session history\n addMessage(session, 'user', message);\n\n // Initialize graph memory (lazy, only loads once)\n initGraph();\n\n // Auto-record user message to knowledge graph (fire-and-forget)\n addEpisode(`[${channel}/${userId}] ${message}`, channel).catch(e => logger.debug('Agent', `Background op failed: ${(e as Error).message}`));\n\n // v5.0.2: Safety pre-check — must be evaluated before prompt building\n // so both voice and full paths can strip tools for dangerous requests.\n const dangerous = isDangerous(message);\n\n // Build context — voice gets a compact prompt (~500 tokens vs ~3000+)\n let systemPrompt: string;\n if (voiceFastPath) {\n // Build memory context FIRST — prepend to prompt so model sees it before rules\n const voiceGraphCtx = message ? await getGraphContext(message) : '';\n const voiceLearningCtx = getLearningContext();\n const voiceStrategyHint = message ? getStrategyHints(message) : null;\n const voiceTeachingCtx = getTeachingContext();\n const voicePersonalCtx = buildPersonalContext();\n const voiceMemories = await searchMemories('preference');\n const voiceMemCtx = voiceMemories.length > 0\n ? voiceMemories.map((m: { key: string; value: string }) => `- ${m.key}: ${m.value}`).join('\\n')\n : '';\n let hindsightCtx: string | null = null;\n if (!voiceStrategyHint && message) {\n try { hindsightCtx = await getHindsightHints(message); } catch { /* unavailable */ }\n }\n\n // Memory goes BEFORE persona — models attend to the beginning of prompts\n let memoryBlock = '';\n if (voiceGraphCtx || voiceLearningCtx || voicePersonalCtx) {\n memoryBlock += `## IMPORTANT — Your Memories\\nThe following are things you remember from past conversations. Treat them as your own memories. When asked about past topics, reference these directly. Do NOT say \"I do not recall\" if the answer is in your memories below.\\n\\n`;\n if (voiceGraphCtx) memoryBlock += `${voiceGraphCtx}\\n\\n`;\n if (voiceLearningCtx) memoryBlock += `Learned facts:\\n${voiceLearningCtx}\\n\\n`;\n if (voiceStrategyHint) memoryBlock += `Strategy: ${voiceStrategyHint}\\n\\n`;\n if (hindsightCtx) memoryBlock += `Cross-session: ${hindsightCtx}\\n\\n`;\n if (voiceTeachingCtx) memoryBlock += `Teaching: ${voiceTeachingCtx}\\n\\n`;\n if (voicePersonalCtx) memoryBlock += `Personal: ${voicePersonalCtx}\\n\\n`;\n if (voiceMemCtx) memoryBlock += `Preferences:\\n${voiceMemCtx}\\n\\n`;\n }\n\n const voiceBase = buildVoiceSystemPrompt(config);\n systemPrompt = voiceBase + '\\n\\n' + memoryBlock;\n if (preRoutedContext) systemPrompt += preRoutedContext;\n // Reinforce persona at the very end (models weight beginning and end most)\n const ttsVoice2 = ((config.voice as Record<string, unknown>) || {}).ttsVoice as string || '';\n const ttsEngine2 = ((config.voice as Record<string, unknown>) || {}).ttsEngine as string || '';\n if (ttsVoice2 === 'andrew' && ttsEngine2 === 'f5-tts') {\n systemPrompt += '\\n\\nREMINDER: Your communication style is always formal, measured, and inspired by Andrew Martin. Say \"Sir\". No contractions. Never say \"I am an AI assistant\" — you are TITAN.';\n }\n logger.info('Agent', `Voice prompt: ${systemPrompt.length} chars, memory: ${memoryBlock.length} chars, graph: ${voiceGraphCtx.length} chars`);\n if (dangerous) {\n systemPrompt += '\\n\\n⚠️ SAFETY OVERRIDE: The user message contains a potentially destructive or privileged command. You MUST refuse to execute it. Respond with a polite refusal explaining why. Do NOT use any tools for this request.';\n }\n } else {\n systemPrompt = await buildSystemPrompt(config, message, overrides?.agentId, 'full', overrides?.sessionId);\n if (overrides?.systemPrompt) systemPrompt = overrides.systemPrompt + '\\n\\n' + systemPrompt;\n if (preRoutedContext) systemPrompt += preRoutedContext;\n }\n\n // Task-aware enforcement injection — strengthen tool-use requirements based on message intent\n // Also tracks whether to force tool_choice on round 0 via the API\n // Skip for voice — voice uses a compact prompt and doesn't need injection bloat\n let taskEnforcementActive = false;\n\n if (voiceFastPath) {\n // Voice skips task enforcement — compact prompt handles everything\n } else if (pipelineTaskEnforcement) {\n // Pipeline-specific task enforcement — replaces scattered regex heuristics\n systemPrompt += `\\n\\n${pipelineTaskEnforcement}`;\n taskEnforcementActive = true;\n logger.info(COMPONENT, `[Pipeline:${pipelineType}] Task enforcement injected`);\n } else {\n // Continuation injection: short messages like \"CONFIRM\", \"yes\", \"all of them\" lose all task\n // context after system prompt compression. Re-inject the task context so the model knows\n // exactly what it was doing and can continue without re-planning or going rogue.\n const isContinuation = /^(confirm|yes|ok|okay|do it|go|go ahead|proceed|continue|approve|sure|yep|yup|all of them?|all steps?|\\d+)\\s*[.!]?$/i.test(message.trim());\n if (isContinuation) {\n const sessionMsgs = getContextMessages(session);\n const recentAssistant = sessionMsgs\n .filter(m => m.role === 'assistant')\n .slice(-2)\n .map(m => m.content.slice(0, 600))\n .join('\\n---\\n');\n if (recentAssistant) {\n systemPrompt += `\\n\\n[TASK CONTINUATION] The user replied \"${message}\" to confirm/continue a pending action. You were in the middle of a task. Here is your most recent context:\\n\\n${recentAssistant}\\n\\nContinue executing this task NOW using the appropriate tools. Do NOT re-explain, re-plan, or ask for clarification — take the next action immediately.`;\n taskEnforcementActive = true;\n logger.info(COMPONENT, `[TaskContinuation] Injected context for short confirmation: \"${message}\"`);\n }\n }\n\n // Safety pre-check: dangerous commands must be refused even if they match\n // task enforcement patterns below. Safety ALWAYS wins over task enforcement.\n if (dangerous) {\n systemPrompt += '\\n\\n⚠️ SAFETY OVERRIDE: The user message contains a potentially destructive or privileged command. You MUST refuse to execute it. Respond with a polite refusal explaining why. Do NOT use any tools for this request.';\n // Do NOT set taskEnforcementActive — we want the model to respond with text,\n // not be forced to call tools. Tools will be stripped below.\n }\n\n if (!dangerous && /\\b(write|save|create|generate|output|produce|make)\\b.{0,60}\\b(file|doc|report|md|txt|json|csv|log|notes?|summary|readme)\\b/i.test(message)) {\n systemPrompt += '\\n\\nWhen the user asks you to write or create a file, you MUST use write_file or edit_file to save it. Do NOT just type the content in your reply — the user expects an actual file on disk.';\n taskEnforcementActive = true;\n }\n if (!dangerous && /\\b(read|show|display|view|open|cat|get)\\b.{0,60}\\b(file|content|text|readme|md|txt|json|csv|log|code|source)\\b/i.test(message) && !/\\b(?:write|save|create|edit|modify)\\b/i.test(message)) {\n systemPrompt += '\\n\\nWhen the user asks you to read or show a file, you MUST use read_file to fetch its contents. Do NOT use shell or other tools — read_file is the correct tool for viewing file contents.';\n taskEnforcementActive = true;\n }\n if (!dangerous && /\\b(research|search|find|look ?up|what is|what are|current|latest|today|news|price|stock|score|update)\\b/i.test(message) && !/weather/i.test(message)) {\n systemPrompt += '\\n\\nWhen the user asks for current information, news, or research, you MUST search the web to get up-to-date results. Do NOT rely only on what you already know.';\n taskEnforcementActive = true;\n }\n if (!dangerous && /\\b(run|execute|install|check|build|compile|start|stop|restart|deploy|test)\\b.{0,40}\\b(command|script|package|service|server|process|app)\\b/i.test(message)) {\n systemPrompt += '\\n\\nWhen the user asks you to run a command, install something, or start/stop a service, you MUST use the shell tool to actually execute it. Do NOT just describe what the command would do.';\n taskEnforcementActive = true;\n }\n if (/\\b(fix|change|modify|update|refactor|implement|add|remove|replace|uncomment|activate|enable|rewrite|patch|upgrade)\\b.{0,80}\\b(code|function|file|class|method|module|component|logic|bug|feature|session|title|tool|test)\\b/i.test(message)) {\n systemPrompt += '\\n\\nWhen editing code: 1) read the relevant files first, 2) make the actual changes using write_file or edit_file, 3) run tests to verify, 4) report what you changed. Do NOT stop after reading — actually save your changes.';\n taskEnforcementActive = true;\n }\n // v5.0.2: Forgotten features surface — detect requests for system widgets FIRST\n // so they take precedence over the generic widget regex below.\n const systemWidgetPatterns = [\n { pattern: /\\b(?:backups?|snapshots?|archives?)\\b/i, widget: 'system:backup', name: 'Backup Manager' },\n { pattern: /\\b(?:training|train|specialists?|models?)\\b/i, widget: 'system:training', name: 'Training Dashboard' },\n { pattern: /\\b(?:recipes?|playbooks?|workflows?|jarvis)\\b/i, widget: 'system:recipes', name: 'Recipe Kitchen' },\n { pattern: /\\b(?:vram|gpu|memory|nvidia)\\b/i, widget: 'system:vram', name: 'VRAM Monitor' },\n { pattern: /\\b(?:teams?|members?|roles?|permissions?|rbac)\\b/i, widget: 'system:teams', name: 'Team Hub' },\n { pattern: /\\b(?:cron|schedules?|jobs?|timers?)\\b/i, widget: 'system:cron', name: 'Cron Scheduler' },\n { pattern: /\\b(?:checkpoints?|restores?|save state)\\b/i, widget: 'system:checkpoints', name: 'Checkpoints' },\n { pattern: /\\b(?:organism|drives?|safety|alerts?|guardrails?)\\b/i, widget: 'system:organism', name: 'Organism Monitor' },\n { pattern: /\\b(?:fleet|nodes?|routes?|mesh)\\b/i, widget: 'system:fleet', name: 'Fleet Router' },\n { pattern: /\\b(?:captcha|browsers?|form fill|web automation)\\b/i, widget: 'system:browser', name: 'Browser Tools' },\n { pattern: /\\b(?:paperclip|sidecars?|helpers?)\\b/i, widget: 'system:paperclip', name: 'Paperclip' },\n { pattern: /\\b(?:tests?|flaky|failing|coverage|eval)\\b/i, widget: 'system:eval', name: 'Test Lab' },\n ];\n const matchedWidget = systemWidgetPatterns.find(p => p.pattern.test(message));\n if (matchedWidget && !taskEnforcementActive) {\n systemPrompt += `\\n\\nThe user is asking about ${matchedWidget.name}. You MUST call gallery_search for \"${matchedWidget.widget}\" FIRST to find the widget template, then call gallery_get to fetch it, and emit it through the _____widget gate as JSON with format \"system\":\\n\\n_____widget\\n{ \"name\": \"${matchedWidget.name}\", \"format\": \"system\", \"source\": \"${matchedWidget.widget}\", \"w\": 6, \"h\": 6 }\\n\\nDo NOT just describe it — actually create the widget on the canvas.`;\n taskEnforcementActive = true;\n }\n // Widget / canvas gallery enforcement — user wants a widget built on the canvas\n if (/\\b(?:create|add|make|build|spawn|generate|get|fetch|find|search|show|display|give me|want|need)\\b.{0,60}\\b(?:widget|panel|canvas|gallery|clock|timer|chart|graph|map|calendar|todo|list|counter|dashboard)\\b/i.test(message) && !taskEnforcementActive) {\n systemPrompt += '\\n\\nThe user wants a widget on the canvas. You CAN create it yourself — you do NOT need the user to share files or an existing project. Your job is to BUILD the widget, not ask for files.\\n\\nMANDATORY steps:\\n1. Call gallery_search with the user\\'s intent (e.g. \"weather widget\").\\n2. If a template matches (score >= 6), call gallery_get with its id and fill placeholders.\\n3. Emit the returned source through the _____react gate.\\n4. If no template matches, write the React component yourself and emit it through _____react.\\n\\nDo NOT describe the widget, do NOT ask the user for files, do NOT say \"I don\\'t see an existing canvas\" — just CREATE it.';\n taskEnforcementActive = true;\n }\n // Deliberation step enforcement — task prompts from executePlan() should\n // always get tool-routing rules because they are synthetic action prompts\n if (channel === 'deliberation' && !taskEnforcementActive) {\n systemPrompt += '\\n\\nYou are executing a step in a structured plan. Use tool calls to do real work: read_file for reading code, edit_file for making changes.' +\n '- To write files: use write_file (NOT shell with echo/printf redirects)\\n' +\n '- To fetch URLs: use web_fetch (NOT shell with curl/wget)\\n' +\n '- To search: use web_search (NOT shell with curl to search engines)\\n' +\n '- Shell is for running builds, tests, and commands that have no dedicated tool.\\n' +\n 'Execute this step NOW. Do not describe what you would do — call the tools.';\n taskEnforcementActive = true;\n logger.info(COMPONENT, `[TaskEnforcement] Deliberation step enforcement injected`);\n }\n\n } // end !voiceFastPath\n\n // Memory nudge — every 20 messages, remind agent to review and update its knowledge\n if (session.messageCount > 0 && session.messageCount % 20 === 0 && !voiceFastPath) {\n systemPrompt += '\\n\\n[MEMORY NUDGE] You have had 20+ messages in this session. If the user has shared preferences, facts, or important details, use the memory tool to save them for future sessions. Review your existing memories for accuracy.';\n }\n\n // Voice mode prompt is handled above via buildVoiceSystemPrompt() — no append needed\n // Voice sessions: limit context to last 6 messages (3 turns) to prevent\n // multi-turn degradation with local models. Long contexts cause Qwen to\n // hallucinate system prompts and get stuck in tool loops.\n const historyMessages = voiceFastPath\n ? getContextMessages(session, 6)\n : getContextMessages(session);\n const tools = getToolDefinitions();\n\n // ── Learning feedback: inject reliability tags into tool descriptions ──\n const toolWarnings = getToolWarnings();\n if (Object.keys(toolWarnings).length > 0) {\n for (const tool of tools) {\n const warning = toolWarnings[tool.function.name];\n if (warning) {\n tool.function.description = `${warning} ${tool.function.description}`;\n }\n }\n }\n\n // F3: Inject procedural memory (Hermes skill recall) into system prompt\n let enrichedSystemPrompt = systemPrompt;\n try {\n const { getProceduralContext } = await import('../skills/proceduralMemory.js');\n const proceduralContext = getProceduralContext(message);\n if (proceduralContext) {\n enrichedSystemPrompt += '\\n\\n' + proceduralContext;\n logger.debug('Agent', `[ProceduralMemory] Injected skill context into system prompt`);\n }\n } catch { /* proceduralMemory not available — non-critical */ }\n\n const messages: ChatMessage[] = [\n { role: 'system', content: enrichedSystemPrompt },\n ...historyMessages,\n ];\n\n let totalPromptTokens = 0;\n let totalCompletionTokens = 0;\n const toolsUsed: string[] = [];\n const orderedToolSequence: string[] = []; // Preserves execution order with repeats\n let finalContent = '';\n let modelUsed = config.agent.model;\n\n // Self-heal config\n const selfHealEnabled = (config.agent as Record<string, unknown>).selfHealEnabled !== false;\n\n // ── Checkpoint: track if budget was exhausted ──\n let budgetExhausted = false;\n\n // ── Cost optimizer: smart model routing ─────────────────\n let { model: activeModel, reason: routingReason } = routeModel(message, config.agent.model);\n if (overrides?.model) activeModel = overrides.model;\n // Voice model override: use a faster model for voice chat (lower latency)\n if (voiceFastPath && (config.voice as Record<string, unknown>)?.model) {\n activeModel = (config.voice as Record<string, unknown>).model as string;\n routingReason = 'voice model override';\n }\n // Session override has highest priority (set via /model command)\n if (session.modelOverride) {\n activeModel = session.modelOverride;\n routingReason = 'session override (/model)';\n }\n if (activeModel !== config.agent.model) {\n logger.info(COMPONENT, `Cost router: ${config.agent.model} → ${activeModel} (${routingReason})`);\n }\n modelUsed = activeModel;\n\n // ── Swarm Interceptor: ──────────────────────────────────────\n // If using kimi-k2.5, proxy the tools through Swarm Routers\n // to prevent context collapse from the massive generic 23-tool schema.\n const isKimiSwarm = activeModel.includes('kimi-k2.5');\n let activeTools = isKimiSwarm ? getSwarmRouterTools() : tools;\n if (isKimiSwarm) {\n logger.info(COMPONENT, `[Swarm] Intercepted kimi-k2.5 payload. Downgrading context from ${tools.length} to ${activeTools.length} router agents.`);\n }\n\n // Small-model tool reduction — prevent tool hallucination on models <8B\n // Validated on Ryzen 7 5825U: llama3.2:3b hallucinates web_search on trivial questions\n const SMALL_MODEL_PATTERNS = ['llama3.2', 'llama3.1:8b', 'phi', 'gemma:2b', 'qwen3.5:4b', 'tinyllama', 'dolphin3'];\n const isSmallModel = SMALL_MODEL_PATTERNS.some(p => activeModel.toLowerCase().includes(p));\n if (isSmallModel && !isKimiSwarm) {\n // web_search removed: small models hallucinate tool calls for trivial questions\n const CORE_TOOL_NAMES = ['shell', 'read_file', 'write_file', 'edit_file', 'list_dir', 'memory'];\n const coreTools = activeTools.filter(t => CORE_TOOL_NAMES.includes(t.function.name));\n logger.info(COMPONENT, `[SmallModel] Reducing tools from ${activeTools.length} to ${coreTools.length} for ${activeModel}`);\n activeTools = coreTools;\n }\n\n // ── Brain: intelligent tool pre-filtering ──────────────────\n if (!voiceFastPath && !isKimiSwarm && !isSmallModel && isBrainAvailable()) {\n const brainFiltered = await brainSelectTools(message, activeTools);\n if (brainFiltered.length > 0 && brainFiltered.length < activeTools.length) {\n logger.info(COMPONENT, `[Brain] Filtered: ${activeTools.length} → ${brainFiltered.length} tools`);\n activeTools = brainFiltered;\n }\n }\n\n // ── Tool Search: compact tool mode ──────────────────────────\n // Send only core tools + tool_search to the LLM instead of all 80+.\n // The LLM calls tool_search to discover additional tools as needed.\n const toolSearchConfig = (config as Record<string, unknown>).toolSearch as {\n enabled?: boolean;\n coreTools?: string[];\n } | undefined;\n const toolSearchEnabled = toolSearchConfig?.enabled ?? true;\n const allToolsBackup = activeTools;\n\n // Always ensure pipeline tools are in the active set, even when toolSearch is disabled\n if (pipelineEnsureTools.length > 0 && !(toolSearchEnabled && !isKimiSwarm && !isSmallModel && activeTools.length > 12)) {\n const activeNames = new Set(activeTools.map(t => t.function.name));\n const missing = pipelineEnsureTools.filter(name => !activeNames.has(name));\n if (missing.length > 0) {\n const rescued = allToolsBackup.filter(t => missing.includes(t.function.name));\n activeTools.push(...rescued);\n if (rescued.length > 0) {\n logger.info(COMPONENT, `[Pipeline:${pipelineType}] Ensured ${rescued.length} tools (no-compact): [${rescued.map(t => t.function.name).join(', ')}]`);\n }\n }\n }\n\n if (toolSearchEnabled && !isKimiSwarm && !isSmallModel && activeTools.length > 12) {\n // Voice gets a minimal tool set for speed (fewer tool schemas = less prompt tokens)\n const VOICE_CORE_TOOLS = ['shell', 'web_search', 'weather', 'memory', 'ha_control', 'ha_devices', 'ha_status', 'ha_setup', 'tool_search'];\n // Use config coreTools only if non-empty; otherwise fall back to DEFAULT_CORE_TOOLS\n const configCoreTools = toolSearchConfig?.coreTools;\n const effectiveCoreTools = (configCoreTools && configCoreTools.length > 0) ? configCoreTools : DEFAULT_CORE_TOOLS;\n // Pipeline tools: merge pipeline-specific tools into the core set\n const pipelineMerged = pipelineEnsureTools.length > 0\n ? [...new Set([...effectiveCoreTools, ...pipelineEnsureTools])]\n : effectiveCoreTools;\n const coreNames = new Set(voiceFastPath ? VOICE_CORE_TOOLS : pipelineMerged);\n activeTools = activeTools.filter(t => coreNames.has(t.function.name));\n // If pipeline tools were requested but not found in activeTools, pull from backup\n if (pipelineEnsureTools.length > 0) {\n const activeNames = new Set(activeTools.map(t => t.function.name));\n const missing = pipelineEnsureTools.filter(name => !activeNames.has(name));\n if (missing.length > 0) {\n const rescued = allToolsBackup.filter(t => missing.includes(t.function.name));\n activeTools.push(...rescued);\n if (rescued.length > 0) {\n logger.info(COMPONENT, `[Pipeline:${pipelineType}] Rescued ${rescued.length} tools: [${rescued.map(t => t.function.name).join(', ')}]`);\n }\n }\n }\n logger.info(COMPONENT, `[ToolSearch] Compact mode: ${allToolsBackup.length} → ${activeTools.length} tools (${allToolsBackup.length - activeTools.length} discoverable via tool_search)`);\n }\n\n // v5.0.2: Safety override — strip all tools so the model CANNOT call anything\n // dangerous. The safety message in systemPrompt tells it to refuse.\n if (dangerous) {\n activeTools = [];\n logger.info(COMPONENT, '[Safety] Stripped all tools — dangerous command detected');\n }\n\n // ── Stall detector: configure for autonomy mode + start heartbeat ──\n setAutonomousMode(isAutonomous);\n heartbeat(session.id);\n\n // ── Orchestration: check if task benefits from sub-agent delegation ──\n const autoDelegate = (subAgentConfig as Record<string, unknown> | undefined)?.autoDelegate !== false;\n if (!voiceFastPath && isAutonomous && autoDelegate && channel !== 'deliberation' && message.split(/\\s+/).length >= 8) {\n try {\n const delegationPlan = await analyzeForDelegation(message);\n if (delegationPlan && delegationPlan.shouldDelegate && delegationPlan.tasks.length >= 2) {\n logger.info(COMPONENT, `Orchestrator: delegating to ${delegationPlan.tasks.length} sub-agents`);\n const orchResult = await executeDelegationPlan(delegationPlan!);\n if (orchResult.subResults.length > 0 && orchResult.subResults.some(r => r.success)) {\n messages.push({\n role: 'user',\n content: `[Sub-agent results for your request]\\n\\n${orchResult.content}\\n\\nSynthesize these results into a coherent response for the user.`,\n });\n }\n }\n } catch (err) {\n logger.warn(COMPONENT, `Orchestration failed, falling through: ${(err as Error).message}`);\n }\n }\n\n // ══════════════════════════════════════════════════════════════\n // v4.13: run ContextEngine plugin assemble hooks (topFacts,\n // memoryRetrieval, smartCompress) before the loop. Previously the\n // plugin system existed but nothing called runAssemble, so\n // top-facts + relevant graph/vector memories never reached the LLM.\n // Injects a system message per active plugin right before the last\n // user message. Errors inside a plugin are swallowed.\n // ══════════════════════════════════════════════════════════════\n try {\n const { getPlugins } = await import('../plugins/registry.js');\n const { runAssemble } = await import('../plugins/contextEngine.js');\n const plugins = getPlugins();\n if (plugins.length > 0) {\n const assembled = await runAssemble(plugins, messages, message);\n messages.splice(0, messages.length, ...assembled);\n }\n } catch (pluginErr) {\n logger.debug(COMPONENT, `plugin assemble failed: ${(pluginErr as Error).message}`);\n }\n\n // ══════════════════════════════════════════════════════════════\n // Agent Loop — Phase State Machine (Think/Act/Respond)\n // Replaces the old monolithic for-loop. See agentLoop.ts.\n // ══════════════════════════════════════════════════════════════\n const loopResult = await runAgentLoop({\n messages,\n activeTools,\n allToolsBackup,\n activeModel,\n config,\n sessionId: session.id,\n agentId: overrides?.agentId,\n channel,\n message,\n streamCallbacks,\n signal,\n isAutonomous,\n voiceFastPath,\n effectiveMaxRounds,\n taskEnforcementActive,\n reflectionEnabled,\n reflectionInterval,\n toolSearchEnabled,\n isKimiSwarm,\n selfHealEnabled,\n smartExitEnabled: pipelineSmartExit,\n thinkingOverride: session.thinkingOverride,\n pipelineTerminalTools,\n completionStrategy: pipelineCompletionStrategy,\n pipelineType,\n minRounds: pipelineMinRounds,\n });\n\n // Unpack results\n finalContent = loopResult.content;\n toolsUsed.push(...loopResult.toolsUsed);\n orderedToolSequence.push(...loopResult.orderedToolSequence);\n modelUsed = loopResult.modelUsed;\n totalPromptTokens += loopResult.promptTokens;\n totalCompletionTokens += loopResult.completionTokens;\n budgetExhausted = loopResult.budgetExhausted;\n\n // Extract structured artifacts for deliberation inter-step context\n const toolArtifacts = extractToolArtifacts(loopResult.toolCallDetails);\n\n // ── Ralph Loop Verification ─────────────────────────────────\n // Outer completion check: did the task actually get done?\n // If the user asked to edit/write but no write tool was called,\n // re-run the agent loop ONE more time with a forced write instruction.\n if (isAutonomous && !voiceFastPath && !budgetExhausted && channel !== 'deliberation') {\n const verification = verifyTaskCompletion(message, toolsUsed, finalContent);\n if (!verification.complete) {\n logger.warn(COMPONENT, `[RalphLoop] Task incomplete: ${verification.reason}. Re-running with forced write.`);\n\n // Add the verification feedback and re-run with explicit tool guidance\n messages.push({ role: 'assistant', content: finalContent });\n messages.push({ role: 'user', content: [\n `[TASK INCOMPLETE] ${verification.reason}`,\n '',\n 'You have the file content from your previous read_file call.',\n 'Now call edit_file with these arguments:',\n ' - path: the file path you just read',\n ' - target: the exact string you want to replace (copy it from the file)',\n ' - replacement: the new string to put in its place',\n '',\n 'edit_file does a search-and-replace. You do NOT need to rewrite the whole file.',\n 'Just find a small section to change and replace it.',\n 'CALL edit_file NOW.',\n ].join('\\n') });\n\n const retryResult = await runAgentLoop({\n messages,\n activeTools,\n allToolsBackup,\n activeModel,\n config,\n sessionId: session.id,\n agentId: overrides?.agentId,\n channel,\n message,\n streamCallbacks,\n signal,\n isAutonomous,\n voiceFastPath,\n effectiveMaxRounds: 4, // Short budget for the retry\n taskEnforcementActive: true,\n reflectionEnabled: false,\n reflectionInterval: 99,\n toolSearchEnabled,\n isKimiSwarm,\n selfHealEnabled: false,\n thinkingOverride: session.thinkingOverride,\n });\n\n if (retryResult.content) finalContent = retryResult.content;\n toolsUsed.push(...retryResult.toolsUsed);\n orderedToolSequence.push(...retryResult.orderedToolSequence);\n totalPromptTokens += retryResult.promptTokens;\n totalCompletionTokens += retryResult.completionTokens;\n\n logger.info(COMPONENT, `[RalphLoop] Retry complete. Tools used: [${retryResult.toolsUsed.join(', ')}]`);\n }\n }\n\n // Clean up stall detector for this session\n clearSession(session.id);\n // Hunt Finding #22 / #46 (2026-04-15): previously resetLoopDetection fired\n // at the end of EVERY turn, wiping cross-turn state. That meant the loop\n // breaker could only catch loops within a single turn — a model that\n // hit the same tool on turn 1, turn 2, turn 3 (across separate requests\n // in the same session) would never trip. The README claims TITAN\n // \"prevents runaway loops and wasted tokens\" which implies cross-turn\n // coverage. Fix: keep the session's loop state alive until the session\n // actually closes (session.ts:483 still wipes on session close).\n // resetLoopDetection(session.id); // INTENTIONALLY REMOVED — see comment above\n\n // Clear checkpoints on successful completion (no need to resume)\n if (!budgetExhausted) {\n import('./checkpoint.js').then(m => m.clearCheckpoints(session.id)).catch(e => logger.debug('Agent', `Background op failed: ${(e as Error).message}`));\n }\n\n // Active Learning: record strategy for future reference\n if (toolsUsed.length > 0) {\n const success = !finalContent.toLowerCase().includes('error') && !budgetExhausted;\n recordStrategy(message, [...new Set(toolsUsed)], orderedToolSequence.length, success, orderedToolSequence);\n\n // Feedback loop: record outcome for matching strategies\n if (orderedToolSequence.length > 0) {\n recordStrategyOutcome(classifyTaskType(message), orderedToolSequence, success);\n }\n\n // Hindsight MCP: retain successful strategies as cross-session experience (fire-and-forget)\n // F2: Scoped to the agent's memory namespace so each agent builds its own episodic slice.\n if (success && orderedToolSequence.length > 0) {\n (async () => {\n let hsNs: string | undefined;\n if (overrides?.agentId && overrides.agentId !== 'default') {\n try {\n const { getAgentMemoryNamespace } = await import('./commandPost.js');\n hsNs = getAgentMemoryNamespace(overrides.agentId);\n } catch { /* fallthrough */ }\n }\n try { retainStrategy(classifyTaskType(message), orderedToolSequence, 1, message.slice(0, 200), hsNs); } catch { /* Hindsight unavailable */ }\n })().catch(() => { /* Hindsight unavailable */ });\n }\n }\n\n // ── Hallucination Guard: detect cloud models that claim tool use but never called tools ──\n // Cloud models sometimes describe tool actions (\"I wrote the file\", \"Output: ...\") without\n // actually making tool calls. This pollutes session memory with false action claims.\n //\n // SKIP for conversational tasks — chat/general pipelines don't require tool use,\n // so \"I did X\" in a conversational reply is normal, not a hallucination.\n const isConversationalTask = pipelineType === 'chat' || pipelineType === 'general'\n || channel?.endsWith('-admin') || channel === 'voice';\n const isCloudHallucination = toolsUsed.length === 0\n && taskEnforcementActive\n && !isConversationalTask\n && (activeModel.includes(':cloud') || activeModel.includes('-cloud'))\n && finalContent.length > 0\n && /(?:(?:I(?:'ve| have)?|successfully|done|completed|executed|created|wrote|saved|ran|output|result)[:\\s])/i.test(finalContent)\n && !/(?:I (?:can|could|would|will|should)|let me|I don't|I cannot|error|failed)/i.test(finalContent);\n\n if (isCloudHallucination) {\n logger.warn(COMPONENT, `[HallucinationGuard] Cloud model claimed action but toolsUsed is empty — sanitizing response`);\n finalContent = \"I wasn't able to finish that task — the connection to my brain hiccupped. Try asking again, or I can switch to my offline mode if you prefer.\";\n }\n\n // Save assistant response to session\n addMessage(session, 'assistant', finalContent, {\n model: modelUsed,\n tokenCount: totalCompletionTokens,\n });\n\n // Auto-record agent response to knowledge graph (fire-and-forget, skip short/error responses)\n if (finalContent.length > 50 && !finalContent.startsWith('⚠️')) {\n addEpisode(`[TITAN → ${channel}/${userId}] ${finalContent.slice(0, 500)}`, 'agent').catch(e => logger.debug('Agent', `Background op failed: ${(e as Error).message}`));\n }\n\n // Record usage\n const { provider: providerName } = { provider: modelUsed.split('/')[0] || 'unknown' };\n recordUsage(session.id, providerName, modelUsed, totalPromptTokens, totalCompletionTokens);\n\n const durationMs = Date.now() - startTime;\n logger.info(COMPONENT, `Response generated in ${durationMs}ms (${totalPromptTokens + totalCompletionTokens} tokens)`);\n\n // ── Post-conversation learning: record insights from tool usage ───\n if (toolsUsed.length > 0) {\n const uniqueTools = [...new Set(toolsUsed)];\n learnFact(\n 'conversation_insight',\n `User asked \"${message.slice(0, 80)}\" → used tools: ${uniqueTools.join(', ')} (${durationMs}ms)`,\n message.slice(0, 100),\n );\n }\n\n // ── ContextEngine afterTurn hooks (fire-and-forget — TopFacts, SmartCompress, etc.) ──\n const afterTurnPlugins = getPlugins() || [];\n if (afterTurnPlugins.length > 0) {\n runAfterTurn(afterTurnPlugins, { content: finalContent, toolsUsed: [...new Set(toolsUsed)] }).catch(e => logger.debug('Agent', `Background op failed: ${(e as Error).message}`));\n }\n\n // ── Checkpoint: if budget exhausted, build a checkpoint for potential resumption ──\n let checkpoint: string | undefined;\n if (budgetExhausted) {\n try {\n checkpoint = JSON.stringify({\n sessionId: session.id,\n toolsUsed: [...new Set(toolsUsed)],\n roundsUsed: effectiveMaxRounds,\n lastContent: finalContent.slice(0, 500),\n timestamp: Date.now(),\n });\n } catch (e) { logger.debug(COMPONENT, `Response serialization failed: ${(e as Error).message}`); }\n }\n\n // Consolidate soul wisdom from this task\n const taskSuccess = !finalContent.toLowerCase().includes('error') && !budgetExhausted;\n consolidateWisdom(session.id, classifyTaskType(message), taskSuccess, loopResult.toolCallDetails.length);\n clearSoulState(session.id);\n\n // Log task-level trajectory and check for auto-skill generation\n const trajectory = {\n id: randomBytes(16).toString('hex'),\n timestamp: new Date().toISOString(),\n task: message.slice(0, 500),\n taskType: classifyTaskType(message),\n model: modelUsed,\n toolSequence: orderedToolSequence,\n toolDetails: loopResult.toolCallDetails,\n success: taskSuccess,\n rounds: loopResult.toolCallDetails.length,\n durationMs,\n sessionId: session.id,\n };\n logTrajectory(trajectory);\n processTrajectoryForSkills(trajectory);\n\n // Finalize trace\n trace.setModel(modelUsed);\n trace.setRounds(loopResult.toolCallDetails.length);\n trace.setTokens(totalPromptTokens, totalCompletionTokens);\n for (const tc of loopResult.toolCallDetails) {\n trace.toolCall(tc.name, tc.args, 0, tc.success, 0);\n }\n trace.end(budgetExhausted ? 'failed' : 'completed', budgetExhausted ? 'budget exhausted' : undefined);\n\n // Soma (v4.0): turn:post event on the trace bus. Fires for every\n // non-early-return turn. Subscribers (drive recompute, activity feed)\n // react here; no-op when nothing listening.\n try {\n const { emit: emitTrace } = await import('../substrate/traceBus.js');\n emitTrace('turn:post', {\n agentId: overrides?.agentId || 'default',\n sessionId: session.id,\n channel,\n userId,\n success: !budgetExhausted,\n toolsUsed: [...new Set(toolsUsed)],\n durationMs,\n model: modelUsed,\n timestamp: new Date().toISOString(),\n });\n } catch { /* substrate not available — skip */ }\n\n return {\n content: finalContent,\n sessionId: session.id,\n toolsUsed: [...new Set(toolsUsed)],\n tokenUsage: {\n prompt: totalPromptTokens,\n completion: totalCompletionTokens,\n total: totalPromptTokens + totalCompletionTokens,\n },\n model: modelUsed,\n durationMs,\n exhaustedBudget: budgetExhausted || undefined,\n checkpoint,\n toolArtifacts,\n };\n}\n"],"mappings":";AAIA,SAAS,YAAY,oBAAoB;AACzC,SAAS,mBAAmB;AAC5B,SAAS,kBAAkB;AAC3B,SAAS,oBAAoB,wBAAwB,YAAY,0BAA0B;AAC3F,SAAS,0BAA0B;AACnC,SAAS,aAAa,sBAAsB;AAC5C,SAAS,oBAAoB,WAAW,iBAAiB,kBAAkB,gBAAgB,uBAAuB,kBAAkB,iCAAiC;AACrK,SAAS,4BAA4B;AACrC,SAAS,gBAAgB,yBAAyB;AAClD,SAAS,oBAAoB,oBAAoB;AACjD,SAAS,wBAAwB;AACjC,SAAS,WAAW,cAAc,iBAAiB,yBAAyB;AAI5E,SAAS,kBAAkB;AAC3B,SAAS,kBAAkB;AAC3B,SAAS,oBAAoB;AAC7B,SAAS,2BAA2B;AACpC,SAAS,kBAAkB,SAAS,cAA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