Skip to content

fix rules cache compat and script test deps#2960

Open
akshat4703 wants to merge 2 commits intomandiant:masterfrom
akshat4703:akshat/fix-rules-cache-compat-and-script-test-deps
Open

fix rules cache compat and script test deps#2960
akshat4703 wants to merge 2 commits intomandiant:masterfrom
akshat4703:akshat/fix-rules-cache-compat-and-script-test-deps

Conversation

@akshat4703
Copy link
Copy Markdown

Summary

This PR addresses two unrelated but currently visible failures in one branch:

  1. Rules cache compatibility bug
  • Fixes crashes like:
    AttributeError: '_RuleFeatureIndex' object has no attribute 'bytes_prefix_index'
  • Root cause is stale/older cached ruleset objects being loaded after _RuleFeatureIndex structure changes.
  • Updates cache compatibility handling (versioning/invalidation path) so outdated cache files are ignored/rebuilt safely.
  1. Script test dependency robustness
  • Script tests currently fail when optional script dependencies are missing (sarif_om, jschema_to_python).
  • Makes dependency expectations explicit so script test behavior is stable and easier to reproduce.

Why

These failures are pre-existing and not introduced by feature work, but they block reliable local validation and can confuse contributors.

Validation

  • Reproduced original failures before fix.
  • Verified cache path no longer crashes with stale cache artifacts.
  • Verified script test behavior is deterministic with dependency handling in place.

@gemini-code-assist
Copy link
Copy Markdown
Contributor

Summary of Changes

Hello, I'm Gemini Code Assist1! I'm currently reviewing this pull request and will post my feedback shortly. In the meantime, here's a summary to help you and other reviewers quickly get up to speed!

This pull request resolves two distinct issues that were hindering reliable local validation and potentially confusing contributors. It enhances the robustness of the rules caching mechanism by ensuring compatibility with schema changes and improves the stability of script tests by making their dependency expectations explicit, preventing failures due to missing optional packages.

Highlights

  • Rules Cache Compatibility: Addressed a bug causing crashes (e.g., AttributeError) when loading stale or older cached ruleset objects. The cache version has been incremented, and the system now gracefully handles and invalidates outdated cache files, preventing runtime errors.
  • Script Test Robustness: Improved the stability of script tests by explicitly handling optional dependencies. The capa2sarif.py test will now be skipped if its required optional dependencies (sarif_om, jschema_to_python) are not installed, ensuring deterministic test behavior.

🧠 New Feature in Public Preview: You can now enable Memory to help Gemini Code Assist learn from your team's feedback. This makes future code reviews more consistent and personalized to your project's style. Click here to enable Memory in your admin console.

Using Gemini Code Assist

The full guide for Gemini Code Assist can be found on our documentation page, here are some quick tips.

Invoking Gemini

You can request assistance from Gemini at any point by creating a comment using either /gemini <command> or @gemini-code-assist <command>. Below is a summary of the supported commands on the current page.

Feature Command Description
Code Review /gemini review Performs a code review for the current pull request in its current state.
Pull Request Summary /gemini summary Provides a summary of the current pull request in its current state.
Comment @gemini-code-assist Responds in comments when explicitly tagged, both in pull request comments and review comments.
Help /gemini help Displays a list of available commands.

Customization

To customize Gemini Code Assist for GitHub experience, repository maintainers can create a configuration file and/or provide a custom code review style guide (such as PEP-8 for Python) by creating and adding files to a .gemini/ folder in the base of the repository. Detailed instructions can be found here.

Limitations & Feedback

Gemini Code Assist may make mistakes. Please leave feedback on any instances where its feedback is incorrect or counter productive. You can react with 👍 and 👎 on @gemini-code-assist comments. If you're interested in giving your feedback about your experience with Gemini Code Assist for GitHub and other Google products, sign up here.

Footnotes

  1. Review the Privacy Notices, Generative AI Prohibited Use Policy, Terms of Service, and learn how to configure Gemini Code Assist in GitHub here. Gemini can make mistakes, so double check it and use code with caution.

Copy link
Copy Markdown
Contributor

@gemini-code-assist gemini-code-assist bot left a comment

Choose a reason for hiding this comment

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

Code Review

This pull request correctly addresses two separate issues: a cache compatibility bug and fragility in script tests with optional dependencies. The cache version is bumped to invalidate old caches, and the exception handling for cache loading is made more robust. The script tests now correctly skip tests for capa2sarif.py when its dependencies are not met. My review includes a couple of suggestions for improvement: in capa/rules/cache.py, I've suggested logging the specific exception when cache loading fails to aid in future debugging. In tests/test_scripts.py, I've proposed a change to improve code readability by breaking up a long line, following PEP 8 guidelines.

Comment on lines +162 to 163
except (AssertionError, EOFError, pickle.UnpicklingError, zlib.error, AttributeError, TypeError, ValueError):
logger.debug("rule set cache is invalid: %s", path)
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

medium

While the broad exception handling is good for robustness, it would be beneficial for debugging to log the specific exception that was caught. This will help diagnose future cache corruption issues more easily.

Suggested change
except (AssertionError, EOFError, pickle.UnpicklingError, zlib.error, AttributeError, TypeError, ValueError):
logger.debug("rule set cache is invalid: %s", path)
except (AssertionError, EOFError, pickle.UnpicklingError, zlib.error, AttributeError, TypeError, ValueError) as e:
logger.debug("rule set cache is invalid: %s: %s", path, e)

logger = logging.getLogger(__name__)

CD = Path(__file__).resolve().parent
HAS_CAPA2SARIF_DEPS = importlib.util.find_spec("sarif_om") is not None and importlib.util.find_spec("jschema_to_python") is not None
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

medium

This line is a bit long and combines two separate checks. For better readability and to adhere to standard line length limits (like PEP 8's recommendation), consider breaking it into multiple lines.

Suggested change
HAS_CAPA2SARIF_DEPS = importlib.util.find_spec("sarif_om") is not None and importlib.util.find_spec("jschema_to_python") is not None
HAS_SARIF_OM = importlib.util.find_spec("sarif_om") is not None
HAS_JSCHEMA_TO_PYTHON = importlib.util.find_spec("jschema_to_python") is not None
HAS_CAPA2SARIF_DEPS = HAS_SARIF_OM and HAS_JSCHEMA_TO_PYTHON
References
  1. PEP 8, the style guide for Python code, recommends limiting lines to a maximum of 79 characters to improve readability. The current line exceeds this limit. (link)

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

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant