autonomy phase 2

This commit is contained in:
serversdwn
2025-12-14 14:43:08 -05:00
parent 49f792f20c
commit 193bf814ec
12 changed files with 2258 additions and 4 deletions

View File

@@ -140,6 +140,55 @@ async def run_reason(req: ReasonRequest):
logger.warning(f"[EXECUTIVE] Planning failed: {e}")
executive_plan = None
# ----------------------------------------------------------------
# STAGE 0.8 — Autonomous Tool Invocation
# ----------------------------------------------------------------
tool_results = None
autonomous_enabled = os.getenv("ENABLE_AUTONOMOUS_TOOLS", "true").lower() == "true"
tool_confidence_threshold = float(os.getenv("AUTONOMOUS_TOOL_CONFIDENCE_THRESHOLD", "0.6"))
if autonomous_enabled and inner_result:
if VERBOSE_DEBUG:
logger.debug("[STAGE 0.8] Analyzing autonomous tool needs...")
try:
from autonomy.tools.decision_engine import ToolDecisionEngine
from autonomy.tools.orchestrator import ToolOrchestrator
# Analyze which tools to invoke
decision_engine = ToolDecisionEngine()
tool_decision = await decision_engine.analyze_tool_needs(
user_prompt=req.user_prompt,
monologue=inner_result,
context_state=context_state,
available_tools=["RAG", "WEB", "WEATHER", "CODEBRAIN"]
)
# Execute tools if confidence threshold met
if tool_decision["should_invoke_tools"] and tool_decision["confidence"] >= tool_confidence_threshold:
orchestrator = ToolOrchestrator(tool_timeout=30)
tool_results = await orchestrator.execute_tools(
tools_to_invoke=tool_decision["tools_to_invoke"],
context_state=context_state
)
# Format results for context injection
tool_context = orchestrator.format_results_for_context(tool_results)
context_state["autonomous_tool_results"] = tool_context
if VERBOSE_DEBUG:
summary = tool_results.get("execution_summary", {})
logger.debug(f"[STAGE 0.8] Tools executed: {summary.get('successful', [])} succeeded")
else:
if VERBOSE_DEBUG:
logger.debug(f"[STAGE 0.8] No tools invoked (confidence: {tool_decision.get('confidence', 0):.2f})")
except Exception as e:
logger.warning(f"[STAGE 0.8] Autonomous tool invocation failed: {e}")
if VERBOSE_DEBUG:
import traceback
traceback.print_exc()
# ----------------------------------------------------------------
# STAGE 1 — Intake summary
# ----------------------------------------------------------------
@@ -217,7 +266,7 @@ async def run_reason(req: ReasonRequest):
update_last_assistant_message(req.session_id, persona_answer)
# ----------------------------------------------------------------
# STAGE 6.5 — Self-state update
# STAGE 6.5 — Self-state update & Pattern Learning
# ----------------------------------------------------------------
try:
from autonomy.self.analyzer import analyze_and_update_state
@@ -230,6 +279,50 @@ async def run_reason(req: ReasonRequest):
except Exception as e:
logger.warning(f"[SELF_STATE] Update failed: {e}")
# Pattern learning
try:
from autonomy.learning.pattern_learner import get_pattern_learner
learner = get_pattern_learner()
await learner.learn_from_interaction(
user_prompt=req.user_prompt,
response=persona_answer,
monologue=inner_result or {},
context=context_state
)
except Exception as e:
logger.warning(f"[PATTERN_LEARNER] Learning failed: {e}")
# ----------------------------------------------------------------
# STAGE 7 — Proactive Monitoring & Suggestions
# ----------------------------------------------------------------
proactive_enabled = os.getenv("ENABLE_PROACTIVE_MONITORING", "true").lower() == "true"
proactive_min_priority = float(os.getenv("PROACTIVE_SUGGESTION_MIN_PRIORITY", "0.6"))
if proactive_enabled:
try:
from autonomy.proactive.monitor import get_proactive_monitor
from autonomy.self.state import load_self_state
monitor = get_proactive_monitor(min_priority=proactive_min_priority)
self_state = load_self_state()
suggestion = await monitor.analyze_session(
session_id=req.session_id,
context_state=context_state,
self_state=self_state
)
# Append suggestion to response if exists
if suggestion:
suggestion_text = monitor.format_suggestion(suggestion)
persona_answer += suggestion_text
if VERBOSE_DEBUG:
logger.debug(f"[STAGE 7] Proactive suggestion added: {suggestion['type']} (priority: {suggestion['priority']:.2f})")
except Exception as e:
logger.warning(f"[STAGE 7] Proactive monitoring failed: {e}")
if VERBOSE_DEBUG:
logger.debug(f"\n{'='*80}")
logger.debug(f"[PIPELINE COMPLETE] Session: {req.session_id}")