Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 19 additions & 11 deletions benchmarks/utils/critics.py
Original file line number Diff line number Diff line change
Expand Up @@ -150,11 +150,12 @@ def get_completed_instances(output_file: str) -> Set[EvalInstanceID]:
Get all instance IDs present in output file
(completed, regardless of success/failure).

Args:
output_file: Path to the JSONL output file

Returns:
Set of instance IDs that were completed (processed)
Reads ``instance_id`` directly from each JSON line WITHOUT validating the
full ``EvalOutput`` model. Resume must recognise prior completion across
schema drift — e.g. archives written with an older SDK whose
tool/observation events no longer match current pydantic models — so a
stale archive still causes completed instances to be skipped instead of
silently being re-run from scratch.
"""
completed_instances: Set[EvalInstanceID] = set()

Expand All @@ -164,19 +165,26 @@ def get_completed_instances(output_file: str) -> Set[EvalInstanceID]:
try:
with open(output_file, "r", encoding="utf-8") as f:
for line_num, line in enumerate(f, 1):
stripped = line.strip()
if not stripped:
continue
try:
data = json.loads(line.strip())
output = EvalOutput.model_validate(data)
completed_instances.add(output.instance_id)

data = json.loads(stripped)
except json.JSONDecodeError as e:
logger.warning(
f"Invalid JSON on line {line_num} in {output_file}: {e}"
)
except Exception as e:
continue

instance_id = (
data.get("instance_id") if isinstance(data, dict) else None
)
if not instance_id:
logger.warning(
f"Error processing line {line_num} in {output_file}: {e}"
f"Missing 'instance_id' on line {line_num} in {output_file}"
)
continue
completed_instances.add(instance_id)

except Exception as e:
logger.warning(f"Error reading output file {output_file}: {e}")
Expand Down
64 changes: 64 additions & 0 deletions tests/test_iterative_resume.py
Original file line number Diff line number Diff line change
Expand Up @@ -443,3 +443,67 @@ def _make_output(instance_id, has_patch):
assert result_ids == {"D"}, (
f"Expected to retry only D (failed in attempt 2), but got: {result_ids}"
)


def test_get_completed_instances_tolerates_stale_archive_schema():
"""
Resume must skip instances that were completed by an older SDK whose
EvalOutput schema has since drifted (e.g. browser tool/observation
events whose pydantic discriminators no longer match current models).

Reading instance_id from raw JSON avoids treating those rows as
"never completed" and re-running them from scratch. Regression test
for evaluation#515.
"""
from benchmarks.utils.critics import get_completed_instances

with tempfile.TemporaryDirectory() as tmpdir:
path = os.path.join(tmpdir, "output.critic_attempt_1.jsonl")
with open(path, "w") as f:
# 1) Stale row: has instance_id but a history event with a
# discriminator value the current SDK no longer knows about.
# EvalOutput.model_validate would raise on this; raw JSON
# parsing must not.
stale = {
"instance_id": "instance_stale",
"instruction": "old instruction",
"instance": {"x": 1},
"test_result": {"git_patch": "old patch"},
"error": None,
"history": [
{
"kind": "BrowserNavigateTool",
"tool_name": "browser",
"args": {"url": "https://example.com"},
}
],
}
f.write(json.dumps(stale) + "\n")

# 2) Fresh row written via current EvalOutput model. Must also
# still be detected.
fresh = EvalOutput(
instance_id="instance_fresh",
test_result={"git_patch": "p"},
instruction="i",
error=None,
history=[],
instance={"x": 2},
)
f.write(fresh.model_dump_json() + "\n")

# 3) Blank line: must be tolerated, not counted.
f.write("\n")

# 4) Malformed JSON: must be skipped with a warning, not raise.
f.write("{not valid json}\n")

# 5) JSON object missing instance_id: must be skipped.
f.write(json.dumps({"foo": "bar"}) + "\n")

completed = get_completed_instances(path)

assert completed == {"instance_stale", "instance_fresh"}, (
f"Expected both stale-schema and fresh rows to be recognised as "
f"completed, got: {completed}"
)
Loading