Fix historial: marcar como contexto pasado, no como nueva petición
El modelo repetía tareas anteriores porque el historial se reconstruía como mensajes user/assistant que parecían peticiones nuevas. Ahora el historial va como un bloque de contexto marcado explícitamente con [HISTORIAL — NO ejecutar de nuevo]. Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
This commit is contained in:
@@ -557,22 +557,36 @@ class ContextEngine:
|
|||||||
|
|
||||||
messages: list[dict[str, Any]] = []
|
messages: list[dict[str, Any]] = []
|
||||||
|
|
||||||
# Include previous task exchanges as conversation history
|
# Include previous task exchanges as compact conversation history
|
||||||
# (so the model remembers what was said in earlier turns)
|
if session.task_history:
|
||||||
for entry in session.task_history[-10:]:
|
history_lines = ["[HISTORIAL DE CONVERSACIÓN ANTERIOR — NO ejecutar de nuevo, solo contexto]"]
|
||||||
summary = entry.get("summary", "")
|
for entry in session.task_history[-10:]:
|
||||||
objective = entry.get("objective", "")
|
objective = entry.get("objective", "")[:200]
|
||||||
if summary.startswith("User: "):
|
summary = entry.get("summary", "")
|
||||||
# Direct response format: "User: X → Agent: Y"
|
key_data = entry.get("key_data", {})
|
||||||
parts = summary.split(" → Agent: ", 1)
|
tools = entry.get("tools_used", [])
|
||||||
user_msg = objective or parts[0].replace("User: ", "", 1)
|
|
||||||
agent_msg = parts[1] if len(parts) > 1 else summary
|
history_lines.append(f"Usuario pidió: {objective}")
|
||||||
messages.append({"role": "user", "content": user_msg})
|
if tools:
|
||||||
messages.append({"role": "assistant", "content": agent_msg})
|
history_lines.append(f" Tools usadas: {', '.join(tools[:5])}")
|
||||||
elif objective:
|
if key_data:
|
||||||
# Task with tools — include as compact exchange
|
kd_parts = []
|
||||||
messages.append({"role": "user", "content": objective})
|
for table, nums in key_data.get("tables", {}).items():
|
||||||
messages.append({"role": "assistant", "content": summary[:500] if summary else "Tarea completada."})
|
kd_parts.append(f"{table}: records {nums}")
|
||||||
|
if key_data.get("sections"):
|
||||||
|
kd_parts.append(f"sections: {key_data['sections'][:5]}")
|
||||||
|
if key_data.get("modules"):
|
||||||
|
kd_parts.append(f"modules: {key_data['modules'][:5]}")
|
||||||
|
if kd_parts:
|
||||||
|
history_lines.append(f" Datos clave: {'; '.join(kd_parts)}")
|
||||||
|
# Extract agent response from summary
|
||||||
|
if " → Agent: " in summary:
|
||||||
|
agent_part = summary.split(" → Agent: ", 1)[1][:200]
|
||||||
|
history_lines.append(f" Resultado: {agent_part}")
|
||||||
|
history_lines.append("")
|
||||||
|
|
||||||
|
messages.append({"role": "user", "content": "\n".join(history_lines)})
|
||||||
|
messages.append({"role": "assistant", "content": "Entendido, tengo el contexto del historial. ¿En qué puedo ayudarte ahora?"})
|
||||||
|
|
||||||
# Current user message
|
# Current user message
|
||||||
messages.append({"role": "user", "content": user_content})
|
messages.append({"role": "user", "content": user_content})
|
||||||
|
|||||||
Reference in New Issue
Block a user