-
Notifications
You must be signed in to change notification settings - Fork 16
auto select evaluators correctly #323
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from 1 commit
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,126 @@ | ||
| import json | ||
| import os | ||
| import time | ||
| from types import SimpleNamespace | ||
| from unittest.mock import patch | ||
|
|
||
| import pytest | ||
|
|
||
| from eval_protocol.cli_commands import create_rft as cr | ||
|
|
||
|
|
||
| def _write_json(path: str, data: dict) -> None: | ||
| os.makedirs(os.path.dirname(path), exist_ok=True) | ||
| with open(path, "w", encoding="utf-8") as f: | ||
| json.dump(data, f) | ||
|
|
||
|
|
||
| def test_load_and_save_last_evaluator(tmp_path, monkeypatch): | ||
| # Force HOME to temp so expanduser paths remain inside tmp | ||
| monkeypatch.setenv("HOME", str(tmp_path / "home")) | ||
| project = tmp_path / "proj" | ||
| project.mkdir() | ||
|
|
||
| # Initially none | ||
| assert cr._load_last_evaluator(str(project)) is None | ||
|
|
||
| # Save and load | ||
| cr._save_last_evaluator(str(project), "evaluator-abc") | ||
| assert cr._load_last_evaluator(str(project)) == "evaluator-abc" | ||
|
|
||
|
|
||
| def test_auto_select_uses_last_pointer(tmp_path, monkeypatch): | ||
| monkeypatch.setenv("HOME", str(tmp_path / "home")) | ||
| project = tmp_path / "proj" | ||
| project.mkdir() | ||
|
|
||
| # Write last pointer under project | ||
| last_path = project / ".eval_protocol" / "last_evaluator.json" | ||
| _write_json(str(last_path), {"evaluator_id": "chosen-id"}) | ||
|
|
||
| eid = cr._auto_select_evaluator_id(str(project)) | ||
| assert eid == "chosen-id" | ||
|
|
||
|
|
||
| def test_auto_select_single_trace(tmp_path, monkeypatch): | ||
| monkeypatch.setenv("HOME", str(tmp_path / "home")) | ||
| project = tmp_path / "proj" | ||
| project.mkdir() | ||
|
|
||
| # Single evaluator trace under project | ||
| trace = project / ".eval_protocol" / "evaluators" / "only-one.json" | ||
| _write_json(str(trace), {"dummy": True}) | ||
|
|
||
| eid = cr._auto_select_evaluator_id(str(project)) | ||
| assert eid == "only-one" | ||
|
|
||
|
|
||
| def test_auto_select_multiple_traces_non_interactive_most_recent(tmp_path, monkeypatch): | ||
| monkeypatch.setenv("HOME", str(tmp_path / "home")) | ||
| project = tmp_path / "proj" | ||
| project.mkdir() | ||
|
|
||
| # Two traces with different mtimes | ||
| older = project / ".eval_protocol" / "evaluators" / "older.json" | ||
| newer = project / ".eval_protocol" / "evaluators" / "newer.json" | ||
| _write_json(str(older), {}) | ||
| _write_json(str(newer), {}) | ||
| # Set older then newer mtime | ||
| t0 = time.time() - 100 | ||
| os.utime(str(older), (t0, t0)) | ||
| t1 = time.time() | ||
| os.utime(str(newer), (t1, t1)) | ||
|
|
||
| eid = cr._auto_select_evaluator_id(str(project), non_interactive=True) | ||
| assert eid == "newer" | ||
|
|
||
|
|
||
| def test_auto_select_multiple_traces_interactive_prompt(tmp_path, monkeypatch): | ||
| monkeypatch.setenv("HOME", str(tmp_path / "home")) | ||
| project = tmp_path / "proj" | ||
| project.mkdir() | ||
|
|
||
| # Two traces with different mtimes to force ordering: newer first, older second | ||
| older = project / ".eval_protocol" / "evaluators" / "older.json" | ||
| newer = project / ".eval_protocol" / "evaluators" / "newer.json" | ||
| _write_json(str(older), {}) | ||
| _write_json(str(newer), {}) | ||
| t0 = time.time() - 100 | ||
| os.utime(str(older), (t0, t0)) | ||
| t1 = time.time() | ||
| os.utime(str(newer), (t1, t1)) | ||
|
|
||
| with patch("builtins.input", return_value="2"): | ||
| eid = cr._auto_select_evaluator_id(str(project), non_interactive=False) | ||
| # Choosing "2" should pick the second item by recency => "older" | ||
| assert eid == "older" | ||
|
|
||
|
|
||
| def test_auto_select_falls_back_to_single_discovered_test(tmp_path, monkeypatch): | ||
| monkeypatch.setenv("HOME", str(tmp_path / "home")) | ||
| project = tmp_path / "proj" | ||
| project.mkdir() | ||
|
|
||
| # No traces; provide exactly one discovered test | ||
| test_file = project / "metric" / "test_calendar.py" | ||
| test_file.parent.mkdir(parents=True, exist_ok=True) | ||
| test_file.write_text("# dummy", encoding="utf-8") | ||
|
|
||
| dummy = SimpleNamespace(qualname="calendar_agent.test_calendar_agent_evaluation", file_path=str(test_file)) | ||
| monkeypatch.setattr(cr, "_discover_tests", lambda cwd: [dummy]) | ||
|
|
||
| eid = cr._auto_select_evaluator_id(str(project)) | ||
| assert eid is not None | ||
| # Should incorporate function name suffix | ||
| assert "test_calendar_agent_evaluation".split("_")[-1] in eid or "test-calendar-agent-evaluation" in eid | ||
|
|
||
|
|
||
| def test_auto_select_returns_none_when_no_candidates(tmp_path, monkeypatch): | ||
| monkeypatch.setenv("HOME", str(tmp_path / "home")) | ||
| project = tmp_path / "proj" | ||
| project.mkdir() | ||
|
|
||
| # No traces, no tests | ||
| monkeypatch.setattr(cr, "_discover_tests", lambda cwd: []) | ||
| eid = cr._auto_select_evaluator_id(str(project)) | ||
| assert eid is None |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.