File size: 1,745 Bytes
3af908f
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
"""Post-eval memory updates (strategy hints only, never answers)."""

from __future__ import annotations

import json
import os
from pathlib import Path

from agent.memory.store import append_experience, question_tags


def self_evolve_enabled() -> bool:
    return os.getenv("SELF_EVOLVE", "0").strip().lower() in {"1", "true", "yes"}


def record_run(
    task_id: str,
    question: str,
    strategy: str,
    tools_used: list[str],
    correct: bool,
) -> None:
    if not self_evolve_enabled():
        return
    append_experience(
        {
            "task_id": task_id,
            "question_tags": question_tags(question),
            "strategy": strategy,
            "tools_used": tools_used,
            "correct": correct,
        }
    )


def update_strategy_deltas(report_path: Path) -> None:
    if not self_evolve_enabled() or not report_path.exists():
        return
    try:
        report = json.loads(report_path.read_text(encoding="utf-8"))
    except (json.JSONDecodeError, OSError):
        return

    deltas: dict[str, dict] = {}
    for row in report.get("results") or []:
        if not row.get("correct"):
            continue
        for tag in row.get("question_tags") or []:
            entry = deltas.setdefault(tag, {"successes": 0, "strategies": []})
            entry["successes"] += 1
            strategy = row.get("strategy")
            if strategy and strategy not in entry["strategies"]:
                entry["strategies"].append(strategy)

    memory_dir = Path(os.getenv("AGENT_MEMORY_DIR", "memory"))
    memory_dir.mkdir(parents=True, exist_ok=True)
    (memory_dir / "strategy_deltas.json").write_text(
        json.dumps(deltas, indent=2, ensure_ascii=False),
        encoding="utf-8",
    )