Repository navigation
fix(evolve): verify grid hash against stored hash txt if present (#267) - #269
ashishsoni-ai wants to merge 4 commits into
Conversation
scarlehoff
left a comment
There was a problem hiding this comment.
The test itself is exactly what I had in mind, but it looks unnecessarily complicated?
| txt_candidates = [ | ||
| grid_path_obj.parent / f"{grid_path_obj.name}.txt", | ||
| grid_path_obj.with_suffix(".txt"), | ||
| ] |
There was a problem hiding this comment.
What is the difference between these two possible candidates? Are they not the same?
| if stem_name.endswith(ext): | ||
| stem_name = stem_name[: -len(ext)] | ||
| txt_candidates.append(grid_path_obj.parent / f"{stem_name}.txt") | ||
| break |
There was a problem hiding this comment.
Why this loop. All pineappl grids have the same extension, which should be defined in parser.py.
|
Thanks @scarlehoff! Simplified both the implementation and the test:
|
scarlehoff
left a comment
There was a problem hiding this comment.
The check as it is and where it is, is correct.
however thinking about the happiness of the users, the most expensive part is computing the evolution kernel, and the current check happens at the level of the FkTable creation (correct! don't get me wrong)
But we should have a second check that happens at the very beginning, the first time we read the grid (to create the operator card and kernel) pineko should immediately check whether the grid's hash matches the stored hash and fail there otherwise. Fail fast, fail soon and all that.
| grid_path_obj = pathlib.Path(grid_path).resolve() | ||
| grid_hash = hashlib.md5(grid_path_obj.read_bytes()).hexdigest() | ||
|
|
||
| # Check that the grid's hash agrees with the stored hash (if present) (#267) |
There was a problem hiding this comment.
| # Check that the grid's hash agrees with the stored hash (if present) (#267) | |
| # Check that the grid's hash agrees with the stored hash (if present) |
| raise ValueError( | ||
| f"Grid hash mismatch for '{grid_path_obj.name}': " | ||
| f"computed {grid_hash}, but stored hash in '{txt_path.name}' is {stored_hash}" | ||
| ) |
There was a problem hiding this comment.
There are valid reasons to run pineko with a modified grid that has not been upload to the server yet, so the error message should tell the user the solution, which is to upload the grid to the theory server using the available script in the theories_slim repository.
Otherwise, the user must remove the .txt file if they want to run.
Looking at the grid hashes stored in some of the jet fktables I realized something relatively bad (cc @felixhekhorn) which is that some of the fktables came from grids that I was not able to match with any of the grids that we have online.
I think this should be flagged smh, i.e., we should have a metadata key in the fktable comes_from_online_grid which defaults to False and that is flipped to True only once this check passes.
e.g., if the user decides they want to run pineko and not upload the grid to the server, the fktable will forever be shamed with comes_from_online_grid=False
|
Btw, for my own education. You are running this with Antigravity and with a free account, is that correct? Do you mind sharing the kind of prompts you are using? I'm curious about how much direction you need to give antigravity. The results themselves are not bad, although there is a lot of redundancy (like the check for different extensions) which is completely unfounded (I don't think we do that anywhere). |
|
Thanks @scarlehoff! I've updated the PR with all of your suggestions:
Regarding your question about Antigravity: I'm using the paid Pro version of Antigravity IDE. My workflow is to first read the issue and understand the context with AI, then give a clear, structured prompt targeting the exact modules and conventions so it implements the fix cleanly, and finally review and test the diff locally. Your feedback on avoiding unnecessary abstractions (like the extension loop) is super helpful for keeping my prompts tighter. |
scarlehoff
left a comment
There was a problem hiding this comment.
Some final comments:
a)
This is a bit tricky for nfonll, I guess the moment that one of the grids don't come from online all should inherit the False flag, right? (I think right now they would all get the True from the first fktable in the combination)
b)
Thanks for the check at the opcard, and eko commands. We should have one also at the beginning of fks (the metadata is only written at the end)
|
|
||
| comes_from_online_grid = False | ||
| if txt_path.is_file(): | ||
| stored_hash = txt_path.read_text().strip().split()[0] |
There was a problem hiding this comment.
Btw, the hashes inside the txt file have this format:
ATLASDY2D8TEV-aMCfast_obs_0-40de1dbf6adedbc563e777c6665fcf8a.pineappl.lz4
So <name>-<hash>.{EXT}
this would be for ATLASDY2D8TEV-aMCfast_obs_0.txt
| tcard, | ||
| ipd, | ||
| iil, | ||
| ) |
There was a problem hiding this comment.
Ok, something that I noticed only now, the opcard command from pineko pineko opcard instead of pineko theory opcards would skip the check because it doesn't go through the theory (also true for the _check_scale_variation!!)
Now, I never ever use pineko opcard and I think nobody uses it, but perhaps the check for illegal grids should be added to evolve.write_operator_card_from_file so that every command passes through there @felixhekhorn
|
Thanks @scarlehoff! Updated the PR with all of your points:
|
scarlehoff
left a comment
There was a problem hiding this comment.
fks and pineko opcard checks (src/pineko/theory.py, src/pineko/evolve.py): Added evolve.check_grid_hash() at the start of TheoryBuilder.fk() and inside write_operator_card_from_file().
But now instead it is happening twice for pineko theory opcards
| if txt_path.is_file(): | ||
| stored_hash = txt_path.read_text().strip().split()[0] | ||
| content = txt_path.read_text().strip() | ||
| stored_hash = content.removesuffix(f".{parser.EXT}").rsplit("-", 1)[-1] |
There was a problem hiding this comment.
Is this safe? / guaranteed to work always? I guess as long as the hash doesn't contain - which it shouldn't...?
Closes #267
Summary
In
set_fktable_metadata(), verifies that the computed MD5 hash of an input grid matches the hash stored in an accompanying.txtfile (if present in the grid directory) before writing the provenance metadata into the FK table. If a mismatch is detected, raises an informativeValueErrorto prevent propagating corrupted or incorrectly matched grids, as requested in #267.Changes
src/pineko/evolve.py:grid_pathis provided, check for candidate hash.txtfiles ingrid_path_obj.parent(<name>.txt,<name>.with_suffix('.txt'), or<stem>.txt)..txthash file is found, verifystored_hash == grid_hash, raisingValueErroron mismatch..txtfile exists, proceeds normally without error.tests/test_evolve.py:.txthash file..txtfile (both raw hash andmd5sum<hash> <filename>format)..txtfile.Verification
pytest -k "not test_nnpdf_grids" tests/: 26 passed.black --check src/pineko/evolve.py tests/test_evolve.py: clean.git diff --check: 0 issues.AI usage
Harness: Antigravity IDE
Model: Gemini 3.8 Flash