Hacktoberfest 2026: le issue che i maintainer hanno segnato per ottobre, aperte e adatte ai principianti. Sfoglia le issue Hacktoberfest

replace_evaluation_results() deletes registry lines it could not read

Aperta
#2,796 0 commenti 0 reazioni 0 assegnatari Vedi su GitHub

Nessuno ha ancora preso questa issue.

Valutazione

Difficoltà
3/5
Tempo stimato
1-2 giorni
Idoneità per principianti
65/100
Tipo di issue
Bug
Chiarezza
Specificata chiaramente
Stato di attività
Attiva
Stack tecnologico
python

Direzione di ricerca

The bug is in pyrit/score/scorer_evaluation/scorer_metrics_io.py, specifically the replace_evaluation_results() function and its helper _load_jsonl(). Start by reading the file and understanding how it currently loads and writes JSONL. The fix must ensure that unparseable lines are preserved and that a read error prevents a rewrite. Look at the existing tests for scorer_metrics_io to see how to add a test for this behavior. 'Done' means the repro script passes and no data is lost on partial reads or invalid JSON lines.

Scritto dal modello di indicizzazione a partire dal testo della issue.

Descrizione

Describe the bug

replace_evaluation_results() rebuilds a scorer-metrics registry from _load_jsonl() (pyrit/score/scorer_evaluation/scorer_metrics_io.py:366-378), but that helper is a lookup: it skips lines it cannot parse, and if the read itself fails it logs Failed to load registry ... and returns whatever it managed to read (:304-311). Used as the source of truth for a truncating rewrite, both behaviours destroy data:

  • an entry whose line is not valid JSON — a write killed mid-line, a hand edit — is deleted from the rewritten file for good;
  • one undecodable byte anywhere in the file stops the read early, and the rewrite then contains only the new entry: every other pre-computed metric in that file is gone, and the call returns without an error.

These files ship with the package (pyrit/datasets/scorer_evals/, e.g. refusal_metrics.jsonl at 191 KB) and hold scores produced by hours of model calls, so this is not a rebuildable cache. The docstring states that only the entry with the matching eval_hash is removed and that the write is atomic; open(file_path, "w") over a short list does neither.

Steps/Code to Reproduce
import json
from pathlib import Path

from pyrit.models import ComponentIdentifier
from pyrit.score.scorer_evaluation.scorer_metrics import ObjectiveScorerMetrics
from pyrit.score.scorer_evaluation.scorer_metrics_io import (
    _load_jsonl,
    add_evaluation_results,
    replace_evaluation_results,
)

path = Path("/tmp/pyrit-issue-repro/registry.jsonl")
path.parent.mkdir(parents=True, exist_ok=True)
path.unlink(missing_ok=True)
ident = ComponentIdentifier(class_name="A", class_module="pyrit.score.test", params={"model_name": "m"})
metrics = ObjectiveScorerMetrics(
    num_responses=100, num_human_raters=3, accuracy=0.9,
    accuracy_standard_error=0.02, f1_score=0.91, precision=0.93, recall=0.90,
)

add_evaluation_results(file_path=path, scorer_identifier=ident, eval_hash="hash_a", metrics=metrics)
path.write_text(path.read_text() + '{"hash_b": "b", "metrics": {"acc\n', encoding="utf-8")  # torn line

replace_evaluation_results(file_path=path, scorer_identifier=ident, eval_hash="hash_new", metrics=metrics)
print("torn line survived the rewrite:", '{"hash_b"' in path.read_text(encoding="utf-8"))

path.write_bytes(path.read_bytes() + b'{"eval_hash": "bad", "metrics": \xff\xfe}\n')  # undecodable byte
replace_evaluation_results(file_path=path, scorer_identifier=ident, eval_hash="hash_new2", metrics=metrics)
print("entries left:", [e["eval_hash"] for e in _load_jsonl(path)])

No network, no API key, no GPU.

Expected Results

The rewrite removes only the entry with the matching eval_hash. An unparseable line is left as it was, and a file that cannot be read is not rewritten at all.

Actual Results

On fc692226 (current main):

Invalid JSON at line 2 in /tmp/pyrit-issue-repro/registry.jsonl: Unterminated string starting at: line 1 column 29 (char 28)
Failed to load registry from /tmp/pyrit-issue-repro/registry.jsonl: 'utf-8' codec can't decode byte 0xff in position 882: invalid start byte
torn line survived the rewrite: False
entries left: ['hash_new2']

The torn line was deleted, and after the failed read hash_a was deleted too — with no exception reaching the caller.

Screenshots

Not applicable.

Versions
  • OS: macOS 27.2 (arm64)
  • Python version: 3.11.15
  • PyRIT version: 1.2.0.dev0, run from source at fc692226
  • pyrit.show_version() was not run; the repro above only touches pyrit.score and pyrit.models.
Lingua principale
Python
Stelle
4.5k
Fork
896
Merge medio
3g 8h
PR unite (30g)
191

Guida per i contributori

Nessuna guida per i contributori indicizzata per questo repository

Come iniziare

  1. Leggi tutta la issue e poi la guida ai contributi del progetto.
  2. Commenta sulla issue per dire che te ne occupi tu — evita che due persone facciano lo stesso lavoro.
  3. Fai un fork del repository e lavora su un branch.
  4. Apri una pull request che faccia riferimento al numero della issue.

Altre issue di microsoft/PyRIT

Tutte le issue di microsoft/PyRIT

Issue simili

Altre issue su Python

Ricevi le nuove issue nella tua casella

Un breve riepilogo di issue GitHub adatte ai principianti.