Files
FusionAGI/fusionagi/reflection/loop.py
Devin AI 445865e429
Some checks failed
Tests / test (3.10) (pull_request) Failing after 40s
Tests / test (3.11) (pull_request) Failing after 39s
Tests / test (3.12) (pull_request) Successful in 49s
Tests / lint (pull_request) Successful in 35s
Tests / docker (pull_request) Successful in 2m27s
fix: deep GPU integration, fix all ruff/mypy issues, add .dockerignore
- Integrate GPU scoring inline into reasoning/multi_path.py (auto-uses GPU when available)
- Integrate GPU deduplication into multi_agent/consensus_engine.py
- Add semantic_search() method to memory/semantic_graph.py with GPU acceleration
- Integrate GPU training into self_improvement/training.py AutoTrainer
- Fix all 758 ruff lint issues (whitespace, import sorting, unused imports, ambiguous vars, undefined names)
- Fix all 40 mypy type errors across the codebase (no-any-return, union-attr, arg-type, etc.)
- Fix deprecated ruff config keys (select/ignore -> [tool.ruff.lint])
- Add .dockerignore to exclude .venv/, tests/, docs/ from Docker builds
- Add type hints and docstrings to verification/outcome.py
- Fix E402 import ordering in witness_agent.py
- Fix F821 undefined names in vector_pgvector.py and native.py
- Fix E741 ambiguous variable names in reflective.py and recommender.py

All 276 tests pass. 0 ruff errors. 0 mypy errors.

Co-Authored-By: Nakamoto, S <defi@defi-oracle.io>
2026-04-28 05:48:37 +00:00

82 lines
2.8 KiB
Python

"""Post-task reflection: run Critic and write lessons/heuristics to reflective memory."""
from typing import Any, Callable, Protocol
from fusionagi._logger import logger
from fusionagi.schemas.messages import AgentMessage, AgentMessageEnvelope
class CriticLike(Protocol):
"""Protocol for critic agent: must have identity and handle_message."""
identity: str
def handle_message(self, envelope: AgentMessageEnvelope) -> AgentMessageEnvelope | None:
...
class ReflectiveMemoryLike(Protocol):
"""Protocol for reflective memory: must have add_lesson and set_heuristic."""
def add_lesson(self, lesson: dict[str, Any]) -> None:
...
def set_heuristic(self, key: str, value: Any) -> None:
...
ReflectionCallback = Callable[[str, dict[str, Any]], None]
"""Callback (event_type, payload) -> None. Emits 'reflection_done' with task_id and evaluation."""
def run_reflection(
critic_agent: CriticLike,
task_id: str,
outcome: str,
trace: list[dict[str, Any]],
plan: dict[str, Any] | None,
reflective_memory: ReflectiveMemoryLike | None,
orchestrator_callback: ReflectionCallback | None = None,
) -> dict[str, Any] | None:
"""
Trigger reflection: send evaluate_request to Critic, then write evaluation
to reflective memory (lessons, heuristics). Optionally notify orchestrator
via orchestrator_callback(event_type, payload); e.g. "reflection_done" with task_id and evaluation.
Returns evaluation dict or None.
"""
envelope = AgentMessageEnvelope(
message=AgentMessage(
sender="orchestrator",
recipient=critic_agent.identity,
intent="evaluate_request",
payload={
"outcome": outcome,
"trace": trace,
"plan": plan,
},
),
task_id=task_id,
)
response = critic_agent.handle_message(envelope)
if not response or response.message.intent != "evaluation_ready":
return None
evaluation: dict[str, Any] = response.message.payload.get("evaluation", {}) # type: ignore[assignment]
if reflective_memory:
reflective_memory.add_lesson({
"task_id": task_id,
"outcome": outcome,
"evaluation": evaluation,
})
suggestions = evaluation.get("suggestions", [])
for i, s in enumerate(suggestions[:5]):
reflective_memory.set_heuristic(f"suggestion_{task_id}_{i}", s)
if orchestrator_callback:
try:
orchestrator_callback("reflection_done", {"task_id": task_id, "evaluation": evaluation})
except Exception:
logger.exception(
"Orchestrator callback failed (reflection_done); callback is best-effort",
extra={"task_id": task_id},
)
return evaluation