|
| 1 | +"""Tests for scripts/evolution_analysis_gate.sh (#910). |
| 2 | +
|
| 3 | +evolution-analysis has two independent halves: |
| 4 | +
|
| 5 | +* Phase 0 — a LOCAL, file-based triage pass (``evolution_local_triage.py``) |
| 6 | + that reads issues/introspection/research sidecars and writes |
| 7 | + ``analysis/YYYY-MM-DD.json``. It needs no GitHub token or ``gh`` at all. |
| 8 | +* Phase 1 — a GitHub-dependent full pass (``gh issue list``, triage/reject/ |
| 9 | + close) that needs WRITE access to the repo. |
| 10 | +
|
| 11 | +Before this fix, evolution-analysis had no gate script of its own, so it |
| 12 | +inherited the generic ``evolution_access_gate.sh`` default: a job-wide |
| 13 | +wake-gate that skips the *entire* agent run — Phase 0 included — whenever |
| 14 | +GitHub write access is unavailable. That meant the token-free local triage |
| 15 | +pass never ran on days the private token was missing/scoped-down, even |
| 16 | +though it needs no GitHub access whatsoever. |
| 17 | +
|
| 18 | +This suite asserts the fix's two guarantees independently: |
| 19 | +
|
| 20 | +1. ``analysis/YYYY-MM-DD.json`` (local triage output) is written regardless |
| 21 | + of GitHub write-access outcome — with push access, without push access, |
| 22 | + and with no ``gh``/token at all. |
| 23 | +2. The wake-gate decision (last stdout line) still correctly reflects |
| 24 | + write access, exactly like ``evolution_access_gate.sh`` alone. |
| 25 | +""" |
| 26 | + |
| 27 | +from __future__ import annotations |
| 28 | + |
| 29 | +import json |
| 30 | +import os |
| 31 | +import shutil |
| 32 | +import stat |
| 33 | +import subprocess |
| 34 | +from pathlib import Path |
| 35 | + |
| 36 | +REPO_ROOT = Path(__file__).resolve().parents[2] |
| 37 | +GATE = REPO_ROOT / "scripts" / "evolution_analysis_gate.sh" |
| 38 | +BASH = shutil.which("bash") or "/bin/bash" |
| 39 | + |
| 40 | + |
| 41 | +def _write_fake_gh(bin_dir: Path, *, perms: str | None, authed: bool = True) -> None: |
| 42 | + """Install a fake `gh` that answers `api user` and `api repos/...`.""" |
| 43 | + if perms is None: |
| 44 | + perms = "{}" |
| 45 | + user_branch = 'echo "tester"\n exit 0' if authed else "exit 1" |
| 46 | + script = f"""#!/bin/bash |
| 47 | +if [ "$1" = "api" ] && [ "$2" = "user" ]; then |
| 48 | + {user_branch} |
| 49 | +fi |
| 50 | +if [ "$1" = "api" ]; then |
| 51 | + case "$2" in |
| 52 | + repos/*) printf '%s' '{perms}'; exit 0 ;; |
| 53 | + esac |
| 54 | +fi |
| 55 | +exit 1 |
| 56 | +""" |
| 57 | + gh = bin_dir / "gh" |
| 58 | + gh.write_text(script) |
| 59 | + gh.chmod(gh.stat().st_mode | stat.S_IXUSR | stat.S_IXGRP | stat.S_IXOTH) |
| 60 | + |
| 61 | + |
| 62 | +def _run_gate(tmp_path: Path, bin_dir: Path, gate: Path = GATE) -> tuple[str, Path]: |
| 63 | + """Run the gate; return (last stdout line, evolution dir). |
| 64 | +
|
| 65 | + Unlike ``test_evolution_access_gate.py`` (which tests a script with no |
| 66 | + dependency beyond ``gh``/``curl``), this gate also runs |
| 67 | + ``evolution_local_triage.py`` via ``python3`` — so PATH here PREPENDS |
| 68 | + ``bin_dir`` (our fake ``gh``, or nothing) onto the real, inherited PATH |
| 69 | + rather than replacing it. That keeps the real ``python3`` (and whatever |
| 70 | + interpreter/shim mechanism it needs on the host) resolvable exactly as |
| 71 | + it would be in production, while still fully controlling which ``gh`` |
| 72 | + the gate sees — the fake one in ``bin_dir`` always shadows any real one. |
| 73 | +
|
| 74 | + ``gate`` defaults to the real ``scripts/evolution_analysis_gate.sh`` but |
| 75 | + can point at a copy in an isolated directory (e.g. to test behavior when |
| 76 | + a sibling script like ``evolution_access_gate.sh`` is deliberately absent). |
| 77 | + """ |
| 78 | + home = tmp_path / "hermes_home" |
| 79 | + evolution_dir = home / "evolution" |
| 80 | + evolution_dir.mkdir(parents=True, exist_ok=True) |
| 81 | + env = dict(os.environ) |
| 82 | + env["PATH"] = f"{bin_dir}{os.pathsep}{os.environ.get('PATH', '')}" |
| 83 | + env["HERMES_HOME"] = str(home) |
| 84 | + env["GITHUB_EVOLUTION_REPO"] = "Owner/repo" |
| 85 | + for k in ("GITHUB_TOKEN", "GITHUB_PRIVATE_TOKEN"): |
| 86 | + env.pop(k, None) |
| 87 | + proc = subprocess.run( |
| 88 | + [BASH, str(gate)], |
| 89 | + capture_output=True, |
| 90 | + text=True, |
| 91 | + env=env, |
| 92 | + ) |
| 93 | + assert proc.returncode == 0, proc.stderr |
| 94 | + return proc.stdout.strip().splitlines()[-1], evolution_dir |
| 95 | + |
| 96 | + |
| 97 | +def _wakes(line: str) -> bool: |
| 98 | + return json.loads(line) == {"wakeAgent": True} |
| 99 | + |
| 100 | + |
| 101 | +def _analysis_json(evolution_dir: Path) -> dict: |
| 102 | + analysis_dir = evolution_dir / "analysis" |
| 103 | + files = list(analysis_dir.glob("*.json")) |
| 104 | + assert len(files) == 1, f"expected exactly one analysis file, found {files}" |
| 105 | + return json.loads(files[0].read_text(encoding="utf-8")) |
| 106 | + |
| 107 | + |
| 108 | +def test_push_access_wakes_and_writes_local_triage(tmp_path: Path) -> None: |
| 109 | + bin_dir = tmp_path / "bin" |
| 110 | + bin_dir.mkdir() |
| 111 | + _write_fake_gh( |
| 112 | + bin_dir, perms='{"admin":false,"maintain":false,"push":true,"pull":true}' |
| 113 | + ) |
| 114 | + line, evo_dir = _run_gate(tmp_path, bin_dir) |
| 115 | + assert _wakes(line) |
| 116 | + result = _analysis_json(evo_dir) |
| 117 | + assert result["local_triage"] is True |
| 118 | + |
| 119 | + |
| 120 | +def test_read_only_account_does_not_wake_but_still_writes_local_triage( |
| 121 | + tmp_path: Path, |
| 122 | +) -> None: |
| 123 | + """The exact bug this issue reports: reachable but push:false must still |
| 124 | + let Phase 0 (local triage) produce output — only the wake decision (Phase |
| 125 | + 1, the LLM agent) should be gated on write access.""" |
| 126 | + bin_dir = tmp_path / "bin" |
| 127 | + bin_dir.mkdir() |
| 128 | + _write_fake_gh( |
| 129 | + bin_dir, perms='{"admin":false,"maintain":false,"push":false,"pull":true}' |
| 130 | + ) |
| 131 | + line, evo_dir = _run_gate(tmp_path, bin_dir) |
| 132 | + assert not _wakes(line) |
| 133 | + result = _analysis_json(evo_dir) |
| 134 | + assert result["local_triage"] is True |
| 135 | + |
| 136 | + |
| 137 | +def test_no_gh_no_token_does_not_wake_but_still_writes_local_triage( |
| 138 | + tmp_path: Path, |
| 139 | +) -> None: |
| 140 | + """No gh on PATH and no env token: cannot confirm write access, so the |
| 141 | + agent must not wake — but local triage needs neither and must still run. |
| 142 | + This is the core regression this issue is about (#910).""" |
| 143 | + bin_dir = tmp_path / "bin" |
| 144 | + bin_dir.mkdir() # deliberately empty: no gh, no curl |
| 145 | + line, evo_dir = _run_gate(tmp_path, bin_dir) |
| 146 | + assert not _wakes(line) |
| 147 | + result = _analysis_json(evo_dir) |
| 148 | + assert result["local_triage"] is True |
| 149 | + |
| 150 | + |
| 151 | +def test_unauthenticated_does_not_wake_but_still_writes_local_triage( |
| 152 | + tmp_path: Path, |
| 153 | +) -> None: |
| 154 | + bin_dir = tmp_path / "bin" |
| 155 | + bin_dir.mkdir() |
| 156 | + _write_fake_gh(bin_dir, perms="{}", authed=False) |
| 157 | + line, evo_dir = _run_gate(tmp_path, bin_dir) |
| 158 | + assert not _wakes(line) |
| 159 | + result = _analysis_json(evo_dir) |
| 160 | + assert result["local_triage"] is True |
| 161 | + |
| 162 | + |
| 163 | +def test_missing_access_gate_fails_closed(tmp_path: Path) -> None: |
| 164 | + """If evolution_access_gate.sh is somehow absent (a degraded install — |
| 165 | + register_evolution_cron.py's _install_access_gate runs unconditionally, |
| 166 | + so this should never normally happen), we cannot confirm write access. |
| 167 | + The gate must fail CLOSED (not wake) rather than default to waking the |
| 168 | + agent — waking unconditionally would defeat the entire point of the |
| 169 | + write-access gate.""" |
| 170 | + bin_dir = tmp_path / "bin" |
| 171 | + bin_dir.mkdir() |
| 172 | + fake_scripts = tmp_path / "fake_scripts" |
| 173 | + fake_scripts.mkdir() |
| 174 | + shutil.copy(GATE, fake_scripts / GATE.name) |
| 175 | + shutil.copy( |
| 176 | + REPO_ROOT / "scripts" / "evolution_local_triage.py", |
| 177 | + fake_scripts / "evolution_local_triage.py", |
| 178 | + ) |
| 179 | + # Deliberately do NOT copy evolution_access_gate.sh alongside it. |
| 180 | + line, evo_dir = _run_gate(tmp_path, bin_dir, gate=fake_scripts / GATE.name) |
| 181 | + assert not _wakes(line) |
| 182 | + result = _analysis_json(evo_dir) |
| 183 | + assert result["local_triage"] is True |
| 184 | + |
| 185 | + |
| 186 | +def test_local_triage_reflects_real_sidecars(tmp_path: Path) -> None: |
| 187 | + """End-to-end: a filed issue in the issues/ sidecar surfaces in the |
| 188 | + gate-produced analysis JSON even when GitHub write access is denied.""" |
| 189 | + bin_dir = tmp_path / "bin" |
| 190 | + bin_dir.mkdir() |
| 191 | + _write_fake_gh(bin_dir, perms='{"push":false}') |
| 192 | + home = tmp_path / "hermes_home" |
| 193 | + issues_dir = home / "evolution" / "issues" |
| 194 | + issues_dir.mkdir(parents=True) |
| 195 | + (issues_dir / "2026-07-08.json").write_text( |
| 196 | + json.dumps({ |
| 197 | + "date": "2026-07-08", |
| 198 | + "proposals": [ |
| 199 | + { |
| 200 | + "title": "Alpha", |
| 201 | + "decision": "filed", |
| 202 | + "issue": 100, |
| 203 | + "priority_score": 1.5, |
| 204 | + "impact": 0.8, |
| 205 | + "effort": 0.3, |
| 206 | + } |
| 207 | + ], |
| 208 | + }) |
| 209 | + ) |
| 210 | + line, evo_dir = _run_gate(tmp_path, bin_dir) |
| 211 | + assert not _wakes(line) |
| 212 | + result = _analysis_json(evo_dir) |
| 213 | + nums = [s["issue_number"] for s in result["selected_for_implementation"]] |
| 214 | + assert 100 in nums |
0 commit comments