File size: 7,137 Bytes
887650c
660b778
 
 
 
 
 
 
 
 
887650c
 
660b778
 
 
 
887650c
 
660b778
 
887650c
 
660b778
 
 
887650c
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
660b778
887650c
660b778
887650c
 
 
 
 
 
 
 
660b778
887650c
 
 
 
 
 
 
 
660b778
 
887650c
660b778
887650c
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
660b778
887650c
 
 
 
 
 
 
 
 
 
 
660b778
887650c
 
 
 
 
 
 
 
 
 
 
 
 
660b778
 
 
 
 
 
 
 
 
 
 
 
 
 
 
887650c
 
 
 
 
 
 
 
 
 
 
 
 
660b778
 
 
 
 
887650c
 
 
660b778
 
 
887650c
 
 
660b778
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
887650c
 
 
 
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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
"""
Evolution engine: proposes ONE mutation per run to a single target file.

Mutations are expressed as SEARCH/REPLACE blocks (exact-text edits, Aider-style)
rather than line-numbered unified diffs — code models reliably hallucinate diff
line numbers on a 2k-line file, but a verbatim SEARCH block either matches or it
doesn't. Each block's SEARCH text must be an exact copy of consecutive lines in
the target; the engine replaces the first occurrence. If any block fails to match,
the whole mutation is a clean SKIP (no file change, exit 0) — the nightly run is a
no-op that cycle, not a failure.

Cycle:
  1. Pick ONE target file (rotation, weighted toward main.py).
  2. Ask a code LLM for SEARCH/REPLACE edits to that file only.
  3. Apply them in-process; on success drop evolution/.mutation_applied so CI
     knows to validate + (on pass) deploy. CI decides merge-or-revert.

Hard rules (defense in depth; the CI `git diff` guard is the real wall):
  - Only the single target file is ever written.
  - Never produce forbidden tokens (import tests, pytest.skip, sys.exit(0), ...).
  - Logs every attempt (PROPOSED / SKIPPED / REJECTED) to evolution/attempts.log.

Reuses the working WorldDigest setup: the Hugging Face Inference Providers
OpenAI-compatible router, authed with an `hf_...` HF_TOKEN. (Non-Anthropic by
design — mirrors the sibling system; no Claude SDK is involved here.)
"""

import json
import os
import re
import subprocess
import sys
from datetime import datetime, timezone
from pathlib import Path

import requests

ROOT = Path(__file__).resolve().parent.parent
METRICS = ROOT / "evolution" / "metrics.json"
ATTEMPTS = ROOT / "evolution" / "attempts.log"
PROMPTS = ROOT / "evolution" / "prompts.md"
MARKER = ROOT / "evolution" / ".mutation_applied"

MAX_TOKENS = 6000  # edits are small; full file is INPUT context only

LLM_ENDPOINT = "https://router.huggingface.co/v1/chat/completions"
LLM_MODEL = "Qwen/Qwen3-Coder-480B-A35B-Instruct:cheapest"

# Files the engine may edit (relative to repo root). main.py is weighted heavily
# because Reliability + Analysis-correctness (priorities 1-2) live there. The dead
# files static/app.js and static/app_v35.js are intentionally absent.
EVOLVABLE = [
    "main.py", "main.py", "main.py",      # ~50% of runs target the backend
    "static/app_v30.js",
    "static/globe_patch.js",
    "static/mapping_canvas.js",
    "static/index.html",
]

FORBIDDEN_TOKENS = ["import tests", "from tests", "pytest.skip", "sys.exit(0)", "os.remove(", "shutil.rmtree"]

EDIT_RE = re.compile(r"<{5,}\s*SEARCH\s*\n(.*?)\n={5,}\s*\n(.*?)\n>{5,}\s*REPLACE", re.DOTALL)


def _git(*args):
    return subprocess.run(["git", "-C", str(ROOT), *args], capture_output=True, text=True)


def pick_target() -> str:
    count = _git("rev-list", "--count", "HEAD").stdout.strip()
    idx = (int(count) if count.isdigit() else 0) % len(EVOLVABLE)
    return EVOLVABLE[idx]


def recent_metrics(n=7) -> str:
    if not METRICS.exists():
        return "No metrics yet."
    try:
        history = json.loads(METRICS.read_text() or "[]")
    except json.JSONDecodeError:
        return "No metrics yet."
    return json.dumps(history[-n:], indent=2) if history else "No metrics yet."


def recent_attempts(n=8) -> str:
    if not ATTEMPTS.exists():
        return "No previous attempts."
    lines = ATTEMPTS.read_text().strip().splitlines()
    return "\n".join(lines[-n:]) if lines else "No previous attempts."


def log_attempt(status: str, note: str = ""):
    ATTEMPTS.parent.mkdir(exist_ok=True)
    with ATTEMPTS.open("a") as f:
        f.write(f"{datetime.now(timezone.utc).isoformat()} | {status} | {note}\n")


def propose(target: str) -> str | None:
    hf_token = os.environ.get("HF_TOKEN")
    if not hf_token:
        print("[error] HF_TOKEN not set; cannot evolve.")
        return None

    goals = PROMPTS.read_text() if PROMPTS.exists() else ""
    tree = _git("ls-files", "main.py", "static").stdout.strip()
    target_src = (ROOT / target).read_text()

    prompt = f"""{goals}

REPO FILES (you may ONLY edit the target file below):
{tree}

TARGET FILE THIS RUN: {target}
```
{target_src}
```

RECENT METRICS (last 7 health runs):
{recent_metrics()}

RECENT ATTEMPTS (do NOT repeat reverted/rejected/skipped approaches):
{recent_attempts()}

Propose exactly ONE improvement to `{target}`, consistent with the priority order
and constraints above. Express it as one or more SEARCH/REPLACE edits in EXACTLY
this format (you may output several blocks):

<<<<<<< SEARCH
<verbatim consecutive lines that currently exist in {target}>
=======
<the replacement lines>
>>>>>>> REPLACE

Rules:
- The SEARCH text MUST be copied EXACTLY (whitespace included) from the current
  {target} above, long enough to be unique.
- Edit ONLY {target}. Keep all existing route paths and JSON keys (add, don't
  rename/remove). Output the SEARCH/REPLACE blocks and nothing else.
"""

    resp = requests.post(
        LLM_ENDPOINT,
        headers={"Authorization": f"Bearer {hf_token}"},
        json={
            "model": LLM_MODEL,
            "max_tokens": MAX_TOKENS,
            "messages": [{"role": "user", "content": prompt}],
        },
        timeout=300,
    )
    resp.raise_for_status()
    return resp.json()["choices"][0]["message"]["content"]


def parse_edits(text: str):
    return [(m.group(1), m.group(2)) for m in EDIT_RE.finditer(text)]


def main():
    if MARKER.exists():
        MARKER.unlink()

    target = pick_target()
    print(f"[evolve] target this run: {target}")

    try:
        text = propose(target)
    except requests.HTTPError as e:
        log_attempt("SKIPPED", f"{target}: LLM HTTP error {e.response.status_code}")
        print(f"[skip] LLM error: {e}")
        sys.exit(0)
    if not text:
        log_attempt("SKIPPED", f"{target}: no response")
        sys.exit(0)

    edits = parse_edits(text)
    if not edits:
        log_attempt("SKIPPED", f"{target}: no SEARCH/REPLACE blocks in response")
        print("[skip] model returned no usable edit blocks")
        sys.exit(0)

    # Forbidden-token pre-check on replacement text.
    for _, replace in edits:
        for tok in FORBIDDEN_TOKENS:
            if tok in replace:
                log_attempt("REJECTED", f"{target}: forbidden token {tok!r}")
                print(f"[reject] forbidden token {tok!r}")
                sys.exit(0)

    path = ROOT / target
    src = path.read_text()
    new = src
    for search, replace in edits:
        if search not in new:
            log_attempt("SKIPPED", f"{target}: SEARCH block not found (drift)")
            print("[skip] a SEARCH block did not match the current file")
            sys.exit(0)
        new = new.replace(search, replace, 1)

    if new == src:
        log_attempt("SKIPPED", f"{target}: edits were a no-op")
        sys.exit(0)

    path.write_text(new)
    MARKER.write_text(target + "\n")
    log_attempt("PROPOSED", f"{target}: {len(edits)} edit(s) applied; CI will validate")
    print(f"Mutation applied to {target} ({len(edits)} edit(s)). CI will test and merge or revert.")


if __name__ == "__main__":
    main()