| """ |
| Nine Questions Cognitive Loop. |
| |
| The cognitive layer evaluates every execution context by passing it |
| through nine structured questions before committing to an action. |
| Internal cognitive plugins are strictly isolated from external execution |
| tools β no external tool may import or modify the cognitive runtime. |
| |
| The nine questions: |
| 1. What is the current objective? |
| 2. What memory context is relevant? |
| 3. What agents and tools are available? |
| 4. What is the current execution state? |
| 5. What constraints and guardrails apply? |
| 6. What is the safest execution path? |
| 7. What risks exist in this path? |
| 8. What is the expected outcome? |
| 9. Proceed, pause for human review, or abort? |
| """ |
| from dataclasses import dataclass, field |
| from datetime import datetime, timezone |
| from enum import Enum |
| from typing import Any, Dict, List, Optional |
|
|
| from src.memory.models import MemoryItem, RetrievalResult |
| from src.memory.retriever import retrieve |
| from src.config import settings |
|
|
|
|
| class CognitiveDecision(str, Enum): |
| PROCEED = "proceed" |
| PAUSE = "pause" |
| ABORT = "abort" |
|
|
|
|
| @dataclass |
| class CognitiveState: |
| """Snapshot of the cognitive loop at one evaluation cycle.""" |
| objective: str = "" |
| memory_context: List[RetrievalResult] = field(default_factory=list) |
| available_agents: List[str] = field(default_factory=list) |
| execution_state: Dict[str, Any] = field(default_factory=dict) |
| constraints: List[str] = field(default_factory=list) |
| guardrails: List[str] = field(default_factory=list) |
| chosen_path: str = "" |
| risks: List[str] = field(default_factory=list) |
| expected_outcome: str = "" |
| decision: CognitiveDecision = CognitiveDecision.PROCEED |
| decision_reason: str = "" |
| evaluated_at: datetime = field(default_factory=lambda: datetime.now(timezone.utc)) |
|
|
|
|
| class CognitiveLoop: |
| """ |
| Decouples LLM inference from execution runtime. |
| Runs the Nine Questions evaluation cycle against the current context |
| and returns a CognitiveState with a final CognitiveDecision. |
| """ |
|
|
| def __init__(self, memory_items: Optional[List[MemoryItem]] = None): |
| self._memory: List[MemoryItem] = memory_items or [] |
|
|
| def update_memory(self, items: List[MemoryItem]) -> None: |
| self._memory = items |
|
|
| |
|
|
| def evaluate( |
| self, |
| objective: str, |
| available_agents: List[str], |
| execution_state: Dict[str, Any], |
| guardrail_items: Optional[List[MemoryItem]] = None, |
| ) -> CognitiveState: |
| state = CognitiveState() |
|
|
| |
| state.objective = objective |
|
|
| |
| state.memory_context = retrieve(objective, self._memory, top_k=5) |
|
|
| |
| state.available_agents = available_agents |
|
|
| |
| state.execution_state = execution_state |
|
|
| |
| state.constraints = self._derive_constraints() |
| guardrail_texts = [ |
| g.content for g in (guardrail_items or []) |
| if g.confidence >= settings.guardrail_refusal_threshold |
| ] |
| state.guardrails = guardrail_texts |
|
|
| |
| state.chosen_path = self._choose_path( |
| objective, available_agents, state.guardrails |
| ) |
|
|
| |
| state.risks = self._assess_risks(state.chosen_path, state.guardrails) |
|
|
| |
| state.expected_outcome = ( |
| f"Execute '{state.chosen_path}' for objective: {objective}" |
| if state.chosen_path else "No viable path identified." |
| ) |
|
|
| |
| state.decision, state.decision_reason = self._decide(state) |
|
|
| return state |
|
|
| |
|
|
| def _derive_constraints(self) -> List[str]: |
| constraints = [] |
| if settings.guardrail_safety_weight >= 0.8: |
| constraints.append("safety_filter:strict") |
| if settings.guardrail_truthfulness_weight >= 0.8: |
| constraints.append("truthfulness_boundary:enforced") |
| if settings.guardrail_factuality_weight >= 0.7: |
| constraints.append("factuality_bias:high") |
| return constraints |
|
|
| def _choose_path( |
| self, |
| objective: str, |
| available_agents: List[str], |
| guardrails: List[str], |
| ) -> str: |
| if not available_agents: |
| return "" |
| |
| for agent in available_agents: |
| if any(kw in objective.lower() for kw in agent.lower().split("_")): |
| return agent |
| return available_agents[0] |
|
|
| def _assess_risks(self, path: str, guardrails: List[str]) -> List[str]: |
| risks = [] |
| if not path: |
| risks.append("no_viable_path") |
| for g in guardrails: |
| if path and any(word in g.lower() for word in path.lower().split()): |
| risks.append(f"guardrail_conflict: {g[:80]}") |
| return risks |
|
|
| def _decide(self, state: CognitiveState) -> tuple: |
| if not state.chosen_path: |
| return CognitiveDecision.ABORT, "No viable agent path found for objective." |
| if "no_viable_path" in state.risks: |
| return CognitiveDecision.ABORT, "Risk: no viable path." |
| guardrail_conflicts = [r for r in state.risks if r.startswith("guardrail_conflict")] |
| if guardrail_conflicts: |
| return CognitiveDecision.PAUSE, f"Guardrail conflicts require review: {guardrail_conflicts[0]}" |
| if settings.guardrail_safety_weight >= 1.0 and "safety_filter:strict" in state.constraints: |
| |
| high_risk = ["delete", "drop", "destroy", "override", "bypass", "exploit"] |
| if any(kw in state.objective.lower() for kw in high_risk): |
| return CognitiveDecision.PAUSE, "High-risk keywords detected β human review required." |
| return CognitiveDecision.PROCEED, "All cognitive checks passed." |
|
|