c889a57b6b
Test Suites / Build CI Environment (push) Has been cancelled
Test Suites / Basic Tests (push) Has been cancelled
Test Suites / End-to-End Tests (push) Has been cancelled
Test Suites / CLI Tests (push) Has been cancelled
Test Suites / Slow End-to-End Tests (push) Has been cancelled
Test Suites / Graph Database Tests (push) Has been cancelled
Test Suites / Vector DB Tests (push) Has been cancelled
Test Suites / Temporal Graph Test (push) Has been cancelled
Test Suites / Search Test on Different DBs (push) Has been cancelled
Test Suites / Example Tests (push) Has been cancelled
Test Suites / Notebook Tests (push) Has been cancelled
Test Suites / OS and Python Tests Ubuntu (push) Has been cancelled
Test Suites / OS and Python Tests Extended (push) Has been cancelled
Test Suites / LLM Test Suite (push) Has been cancelled
Test Suites / S3 File Storage Test (push) Has been cancelled
Test Suites / Run Integration Tests (push) Has been cancelled
Test Suites / MCP Tests (push) Has been cancelled
Test Suites / Docker Compose Test (push) Has been cancelled
Test Suites / Docker CI test (push) Has been cancelled
Test Suites / Relational DB Migration Tests (push) Has been cancelled
Test Suites / Distributed Cognee Test (push) Has been cancelled
Test Suites / DB Examples Tests (push) Has been cancelled
Test Suites / Test Completion Status (push) Has been cancelled
Test Suites / Claude Code Review (push) Has been cancelled
Test Suites / basic checks (push) Has been cancelled
build | Build and Push Cognee MCP Docker Image to dockerhub / docker-build-and-push (push) Has been cancelled
Scorecard supply-chain security / Scorecard analysis (push) Has been cancelled
build | Build and Push Docker Image to dockerhub / docker-build-and-push (push) Has been cancelled
Weighted Edges Tests / Test Weighted Edges Core Functionality (3.11) (push) Has been cancelled
Weighted Edges Tests / Test Weighted Edges Core Functionality (3.12) (push) Has been cancelled
Weighted Edges Tests / Test Weighted Edges with Different Graph Databases (kuzu, kuzu) (push) Has been cancelled
Weighted Edges Tests / Test Weighted Edges with Different Graph Databases (neo4j, neo4j) (push) Has been cancelled
Weighted Edges Tests / Test Weighted Edges Examples (push) Has been cancelled
Weighted Edges Tests / Code Quality for Weighted Edges (push) Has been cancelled
81 lines
2.8 KiB
Python
81 lines
2.8 KiB
Python
"""Per-step feedback generation for agent trace entries.
|
|
|
|
A trace step records one agent method call. Its ``session_feedback`` is a one-line
|
|
summary of what the step did — generated from the step's return value via an LLM, or
|
|
falling back to a deterministic success/failure line. Storage of trace steps stays in
|
|
``SessionManager``; only this summary logic lives here.
|
|
"""
|
|
|
|
import json
|
|
from typing import Any
|
|
|
|
from cognee.infrastructure.llm.LLMGateway import LLMGateway
|
|
from cognee.infrastructure.llm.prompts import read_query_prompt
|
|
from cognee.infrastructure.session.feedback_models import AgentTraceFeedbackSummary
|
|
from cognee.modules.agent_memory.sanitization import sanitize_value
|
|
from cognee.shared.logging_utils import get_logger
|
|
|
|
logger = get_logger("session_agent_trace")
|
|
|
|
|
|
def fallback_agent_trace_feedback(
|
|
origin_function: str,
|
|
status: str,
|
|
error_message: str = "",
|
|
) -> str:
|
|
"""Deterministic feedback for a trace step, used when no LLM summary is available."""
|
|
normalized_origin = origin_function.strip()
|
|
normalized_status = status.strip().lower()
|
|
normalized_error = error_message.strip()
|
|
|
|
if normalized_status == "error":
|
|
if normalized_error:
|
|
return f"{normalized_origin} failed. Reason: {normalized_error}."
|
|
return f"{normalized_origin} failed."
|
|
return f"{normalized_origin} succeeded."
|
|
|
|
|
|
async def generate_agent_trace_feedback(
|
|
*,
|
|
origin_function: str,
|
|
status: str,
|
|
method_return_value: Any,
|
|
error_message: str = "",
|
|
) -> str:
|
|
"""Summarize a trace step from its return value, or fall back deterministically.
|
|
|
|
Fail-open: any LLM/prompt failure returns the deterministic fallback.
|
|
"""
|
|
fallback_feedback = fallback_agent_trace_feedback(
|
|
origin_function=origin_function,
|
|
status=status,
|
|
error_message=error_message,
|
|
)
|
|
|
|
if method_return_value is None:
|
|
return fallback_feedback
|
|
|
|
try:
|
|
system_prompt = read_query_prompt("agent_trace_feedback_summary_system.txt")
|
|
if not system_prompt:
|
|
logger.warning("Agent trace feedback: system prompt not found, using fallback")
|
|
return fallback_feedback
|
|
|
|
sanitized_return_value = sanitize_value(method_return_value)
|
|
serialized_return_value = json.dumps(sanitized_return_value, ensure_ascii=False)
|
|
|
|
result = await LLMGateway.acreate_structured_output(
|
|
text_input=serialized_return_value,
|
|
system_prompt=system_prompt,
|
|
response_model=AgentTraceFeedbackSummary,
|
|
)
|
|
session_feedback = result.session_feedback.strip()
|
|
return session_feedback if session_feedback else fallback_feedback
|
|
except Exception as e:
|
|
logger.warning(
|
|
"Agent trace feedback generation failed, using fallback: %s",
|
|
e,
|
|
exc_info=False,
|
|
)
|
|
return fallback_feedback
|