Skip to content

fix(evolve): verify grid hash against stored hash txt if present (#267) - #269

Open
ashishsoni-ai wants to merge 3 commits into
NNPDF:mainfrom
ashishsoni-ai:fix/grid-hash-check-against-stored-txt
Open

ashishsoni-ai wants to merge 3 commits into
NNPDF:mainfrom
ashishsoni-ai:fix/grid-hash-check-against-stored-txt

Conversation

@ashishsoni-ai

Copy link
Copy Markdown
Contributor

Closes #267

Summary

In set_fktable_metadata(), verifies that the computed MD5 hash of an input grid matches the hash stored in an accompanying .txt file (if present in the grid directory) before writing the provenance metadata into the FK table. If a mismatch is detected, raises an informative ValueError to prevent propagating corrupted or incorrectly matched grids, as requested in #267.

Changes

  • src/pineko/evolve.py:
    • When grid_path is provided, check for candidate hash .txt files in grid_path_obj.parent (<name>.txt, <name>.with_suffix('.txt'), or <stem>.txt).
    • If a .txt hash file is found, verify stored_hash == grid_hash, raising ValueError on mismatch.
    • If no .txt file exists, proceeds normally without error.
  • tests/test_evolve.py:
    • Added unit test cases covering:
      • Missing grid path.
      • Grid path without a .txt hash file.
      • Grid path with a matching .txt file (both raw hash and md5sum <hash> <filename> format).
      • Grid path with a mismatched .txt file.

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

@felixhekhorn felixhekhorn added the enhancement New feature or request label Nov 26, 2026

@scarlehoff scarlehoff left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The test itself is exactly what I had in mind, but it looks unnecessarily complicated?

Comment thread src/pineko/evolve.py Outdated
txt_candidates = [
grid_path_obj.parent / f"{grid_path_obj.name}.txt",
grid_path_obj.with_suffix(".txt"),
]

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What is the difference between these two possible candidates? Are they not the same?

Comment thread src/pineko/evolve.py Outdated
if stem_name.endswith(ext):
stem_name = stem_name[: -len(ext)]
txt_candidates.append(grid_path_obj.parent / f"{stem_name}.txt")
break

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why this loop. All pineappl grids have the same extension, which should be defined in parser.py.

@ashishsoni-ai

Copy link
Copy Markdown
Contributor Author

Thanks @scarlehoff! Simplified both the implementation and the test:

  • Replaced the extension loop and redundant with_suffix(".txt") candidate in src/pineko/evolve.py with parser.EXT (checking <stem>.txt and falling back to <grid_name>.txt).
  • Consolidated the hash check unit tests in tests/test_evolve.py into a single concise test_set_fktable_metadata_hash_check test.
  • Verified with pytest tests/test_evolve.py (3 passed) and black --check.

@scarlehoff scarlehoff left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Comment thread src/pineko/evolve.py Outdated
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)

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
# 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)

Comment thread src/pineko/evolve.py
Comment on lines +89 to +92
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}"
)

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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

@scarlehoff

scarlehoff commented Nov 27, 2026 •

Copy link
Copy Markdown
Member

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).
Also, it is somewhat ok for a focused change like this one, where I already have a clear picture of what I want to see (and I'm fine with letting the details to the magical oracle) but seeing that, I feel a bit uneasy about more complex scenarios where I don't (so I will certainly miss many details that I would prefer not to miss...)

@ashishsoni-ai

Copy link
Copy Markdown
Contributor Author

Thanks @scarlehoff! I've updated the PR with all of your suggestions:

  • Fail-fast check: Extracted evolve.check_grid_hash(grid_path) and called it at the start of TheoryBuilder.opcard() and TheoryBuilder.eko() so mismatches fail before computing operator cards or evolution kernels.
  • comes_from_online_grid metadata: Added comes_from_online_grid to set_fktable_metadata() (defaulting to "False", flipped to "True" only when the .txt hash check passes).
  • Actionable error message & comment: Updated the mismatch ValueError to mention uploading via theories_slim or removing the .txt file, and removed (#267) from the inline comment.
  • Tests: Updated tests/test_evolve.py to cover comes_from_online_grid and the updated error message (pytest tests/test_evolve.py: 3 passed, black --check: clean).

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.

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

enhancement New feature or request

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Check that the grid's hash agree with the stored hash (if present)

3 participants