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:
Jordan Diaz
2026-04-04 10:30:13 +00:00
parent a9fbd01b5d
commit a86445f91a

View File

@@ -557,22 +557,36 @@ class ContextEngine:
messages: list[dict[str, Any]] = []
# Include previous task exchanges as conversation history
# (so the model remembers what was said in earlier turns)
# Include previous task exchanges as compact conversation history
if session.task_history:
history_lines = ["[HISTORIAL DE CONVERSACIÓN ANTERIOR — NO ejecutar de nuevo, solo contexto]"]
for entry in session.task_history[-10:]:
objective = entry.get("objective", "")[:200]
summary = entry.get("summary", "")
objective = entry.get("objective", "")
if summary.startswith("User: "):
# Direct response format: "User: X → Agent: Y"
parts = summary.split(" → Agent: ", 1)
user_msg = objective or parts[0].replace("User: ", "", 1)
agent_msg = parts[1] if len(parts) > 1 else summary
messages.append({"role": "user", "content": user_msg})
messages.append({"role": "assistant", "content": agent_msg})
elif objective:
# Task with tools — include as compact exchange
messages.append({"role": "user", "content": objective})
messages.append({"role": "assistant", "content": summary[:500] if summary else "Tarea completada."})
key_data = entry.get("key_data", {})
tools = entry.get("tools_used", [])
history_lines.append(f"Usuario pidió: {objective}")
if tools:
history_lines.append(f" Tools usadas: {', '.join(tools[:5])}")
if key_data:
kd_parts = []
for table, nums in key_data.get("tables", {}).items():
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
messages.append({"role": "user", "content": user_content})