From 12ed6f9c42acfb10e13b030bca88601053d55c74 Mon Sep 17 00:00:00 2001 From: Tydin Jarman Date: Mon, 21 Sep 2026 14:50:11 -0700 Subject: [PATCH 1/4] Observation: untracked-file evidence + pytest summary parsing hermes workers write test files as UNTRACKED files (invisible to git diff) and buffer their NDJSON output, so Jev never sees test evidence: on mraize tests_sufficient flatlined at 0.05 for 16 assessments while tests passed 2/2. Two observation fixes: - untracked_evidence: bounded head-of-content for up to 3 untracked text files from git_status (skips binary/dirs, truncates large) - test_results parsed from worker stdout pytest summary lines ('N passed', 'N failed, M passed') so executed-test evidence reaches the assessment +6 tests. Full suite: 115 passed (2 pre-existing Windows failures). --- add_obs_tests.py | 59 ++++++++++++++++++ calib_step1.py | 26 ++++++++ calib_step2.py | 35 +++++++++++ calib_step3.py | 67 ++++++++++++++++++++ calib_step4.py | 58 ++++++++++++++++++ debug_parser.py | 7 +++ fix_parser.py | 49 +++++++++++++++ fix_parser2.py | 17 ++++++ fix_parser3.py | 59 ++++++++++++++++++ fix_regex.py | 22 +++++++ src/foreman/observation.py | 98 +++++++++++++++++++++++++++--- tests/test_observation_evidence.py | 44 ++++++++++++++ 12 files changed, 534 insertions(+), 7 deletions(-) create mode 100644 add_obs_tests.py create mode 100644 calib_step1.py create mode 100644 calib_step2.py create mode 100644 calib_step3.py create mode 100644 calib_step4.py create mode 100644 debug_parser.py create mode 100644 fix_parser.py create mode 100644 fix_parser2.py create mode 100644 fix_parser3.py create mode 100644 fix_regex.py create mode 100644 tests/test_observation_evidence.py diff --git a/add_obs_tests.py b/add_obs_tests.py new file mode 100644 index 0000000..37e95af --- /dev/null +++ b/add_obs_tests.py @@ -0,0 +1,59 @@ +"""Unit tests for untracked_evidence + pytest parser.""" +test = ''' +from pathlib import Path + +from foreman.observation import ObservationBuilder, _pytest_summary_from_output, _untracked_evidence + + +def test_untracked_evidence_captures_new_test_file(tmp_path: Path) -> None: + (tmp_path / "test_new.py").write_text("def test_x():\\n assert True\\n", encoding="utf-8") + evidence = _untracked_evidence(tmp_path, "?? test_new.py", 12000) + assert "untracked: test_new.py" in evidence + assert "test_x" in evidence + + +def test_untracked_evidence_skips_binary_and_dirs(tmp_path: Path) -> None: + (tmp_path / "blob.bin").write_bytes(b"\\x00\\x01\\x02\\x00binary") + (tmp_path / "subdir").mkdir() + evidence = _untracked_evidence(tmp_path, "?? blob.bin\\n?? subdir/", 12000) + assert evidence == "" + + +def test_untracked_evidence_truncates_large_files(tmp_path: Path) -> None: + big = tmp_path / "big.txt" + big.write_text("x" * 9000, encoding="utf-8") + evidence = _untracked_evidence(tmp_path, "?? big.txt", 6000) + assert "truncated" in evidence + assert len(evidence) < 9000 + + +def test_pytest_parser_extracts_pass_and_fail() -> None: + output = "running...\\n================= 2 passed in 0.01s =================\\ndone" + results = _pytest_summary_from_output(output) + assert len(results) == 1 + assert results[0]["passed"] == 2 + assert results[0]["failed"] == 0 + + +def test_pytest_parser_extracts_failures() -> None: + output = "=========== 1 failed, 3 passed in 1.20s ===========" + results = _pytest_summary_from_output(output) + assert results[0]["passed"] == 3 + assert results[0]["failed"] == 1 + + +def test_pytest_parser_ignores_non_summary_lines() -> None: + assert _pytest_summary_from_output("just some text\\n2 passed something else entirely") == [] +''' +open("tests/test_observation_evidence.py", "w").write(test) +import subprocess +r = subprocess.run( + [".venv/Scripts/python", "-m", "pytest", "tests/test_observation_grace_probe.py", + "tests/test_observation.py", "-q", "-p", "no:cacheprovider"], + capture_output=True, text=True, timeout=120, encoding="utf-8", errors="replace", +) +r2 = subprocess.run( + [".venv/Scripts/python", "-m", "pytest", "tests/test_observation.py", "-q", "-p", "no:cacheprovider"], + capture_output=True, text=True, timeout=120, encoding="utf-8", errors="replace", +) +print(r2.stdout[-400:]) \ No newline at end of file diff --git a/calib_step1.py b/calib_step1.py new file mode 100644 index 0000000..cd0e2a8 --- /dev/null +++ b/calib_step1.py @@ -0,0 +1,26 @@ +"""Calibration fix candidates for the hermes evidence gap. + +Fix 1: `git diff` misses UNTRACKED files (test_calc.py is `??`). Jev never +sees the test file content -> tests_sufficient stays ~0.05. Add +`git diff` + intent-to-add trick: `git add -N .` makes untracked files +appear in diff. Do it in the observation builder? No — mutating the repo +from an observer is wrong. Better: capture untracked file contents +separately into the observation via git status --porcelain parsing + +reading the files (bounded). + +Simpler: add a `git ls-files --others --exclude-standard` list to +git_status evidence so Jev at least sees untracked file NAMES, and raise +the per-line cap. Let me check how git_status flows into the observation. +""" +import subprocess + +r = subprocess.run( + ["grep", "-n", "git_status\|test_results", "C:/repos/foreman/src/foreman/foreman/jev.py"], + capture_output=True, text=True, timeout=30, +) +print(r.stdout) +r2 = subprocess.run( + ["sed", "-n", "121,160p", "C:/repos/foreman/src/foreman/foreman/jev.py"], + capture_output=True, text=True, timeout=30, +) +print(r2.stdout) \ No newline at end of file diff --git a/calib_step2.py b/calib_step2.py new file mode 100644 index 0000000..2a334c4 --- /dev/null +++ b/calib_step2.py @@ -0,0 +1,35 @@ +"""Calibration fix 1: untracked-file evidence. + +`git diff` never shows untracked files. hermes workers write test_calc.py as +a NEW file -> it's untracked -> the diff (Jev's main implementation evidence) +only shows calc.py, and the test file is invisible except as a name in +git_status. Fix: in ObservationBuilder, run `git add --intent-to-add`-free +approach — capture untracked file NAMES via status porcelain (already there) +and extend the diff evidence with `git diff --no-index /dev/null `-style +content? Simpler + non-mutating: `git ls-files --others --exclude-standard` +list is already in git_status (?? lines). The real gap is CONTENT of untracked +test files. + +Plan: add an `untracked_evidence` field to FactoryObservation: for each +untracked *.py/*.md file (bounded count+size), include the head of its +content. Then Jev can see test_calc.py's tests. + +Steps: +1. observation.py: parse git_status ?? entries; read bounded content of each + (up to 3 files, 4k chars each) into new field `untracked_evidence: str`. +2. Add to FactoryObservation model. +3. Unit test. +""" +import re + +# 1. Model field +obs = open("src/foreman/observation.py").read() +if "untracked_evidence" not in obs: + obs = obs.replace( + " git_status: str\n git_diff: str", + " git_status: str\n git_diff: str\n untracked_evidence: str = \"\"", + ) + open("src/foreman/observation.py", "w").write(obs) + print("model field added") +else: + print("model field already present") \ No newline at end of file diff --git a/calib_step3.py b/calib_step3.py new file mode 100644 index 0000000..27cc888 --- /dev/null +++ b/calib_step3.py @@ -0,0 +1,67 @@ +"""Calibration fix 1b: populate untracked_evidence in ObservationBuilder.build.""" + +src = open("src/foreman/observation.py").read() + +helper = ''' + +def _untracked_evidence(repository: Path, git_status: str, limit: int) -> str: + """Bounded head-of-content for untracked files named in git_status. + + hermes-style workers write test files as new (untracked) files; `git diff` + never shows them, leaving the supervisor without test evidence. Include a + bounded excerpt of each untracked text file (up to 3 files, ~4k chars + total) so assessments can see new tests/source. Binary-looking files are + skipped. Read-only: the repository is never mutated. + """ + paths: list[str] = [] + for line in git_status.splitlines(): + line = line.strip() + if line.startswith("?? "): + candidate = line[3:].strip().strip('"') + if candidate and not candidate.endswith("/"): + paths.append(candidate) + if not paths: + return "" + + per_file = max(500, limit // 3) + chunks: list[str] = [] + collected = 0 + for name in paths[:3]: + path = repository / name + try: + if not path.is_file() or path.stat().st_size > 1_000_000: + continue + raw = path.read_bytes() + except OSError: + continue + if b"\\x00" in raw[:4096]: + continue # binary + text = raw.decode("utf-8", errors="replace") + excerpt = text[:per_file] + if len(text) > per_file: + excerpt += f"\\n... (truncated, {len(text)} chars total)" + chunks.append(f"--- untracked: {name} ---\\n{excerpt}") + collected += len(excerpt) + if collected >= limit: + break + return "\\n".join(chunks) + +''' + +if "_untracked_evidence" not in src: + # insert helper before ObservationBuilder + src = src.replace( + "class ObservationBuilder:", + helper + "\n\nclass ObservationBuilder:", + ) + # populate the field in build() + src = src.replace( + ' git_status=git_status,\n git_diff=git_diff,', + ' git_status=git_status,\n' + ' git_diff=git_diff,\n' + ' untracked_evidence=_untracked_evidence(repository, git_status, min(self.config.diff_limit, 12_000)),', + ) + open("src/foreman/observation.py", "w").write(src) + print("builder patched") +else: + print("builder already patched") \ No newline at end of file diff --git a/calib_step4.py b/calib_step4.py new file mode 100644 index 0000000..6adc43c --- /dev/null +++ b/calib_step4.py @@ -0,0 +1,58 @@ +"""Add a unit test for untracked_evidence, then calibration fix 2: +TEST_RESULT evidence from worker output. When a worker's stdout contains +pytest-style results ('N passed'), foreman's test_results list is empty — +Jev's tests_sufficient has nothing to key on. Parse pytest lines from worker +stdout into test_results entries in the observation.""" +src = open("src/foreman/observation.py").read() + +if "PYTEST_LINE" not in src: + src = src.replace( + "import asyncio\nfrom datetime import UTC, datetime", + "import asyncio\nimport re\nfrom datetime import UTC, datetime", + ) + helper = ''' + +_PYTEST_LINE = re.compile( + r"^(?:=+\\s*)?(?P\\d+)\\s+passed(?P.*?)(?:\\s+in\\s+(?P