Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor: GeometryIdentifier checks value range #4094

Merged
merged 1 commit into from
Feb 18, 2025

Conversation

paulgessinger
Copy link
Member

@paulgessinger paulgessinger commented Feb 17, 2025

I've run into overflowing ID values one too many times now. This PR proposes making the value setters check the value to be assigned to be within the valid range.

I'm currently throwing an exception, which we can debate if that's the best way to handle failure.

--- END COMMIT MESSAGE ---

Thoughts @benjaminhuth @andiwand?

Summary by CodeRabbit

  • New Features
    • Introduced a method to calculate the maximum value for identifiers, enhancing validation capabilities.
  • Bug Fixes
    • Improved error handling now throws exceptions for out-of-bound values, ensuring robust identifier configuration.

@paulgessinger paulgessinger added this to the next milestone Feb 17, 2025
Copy link

coderabbitai bot commented Feb 17, 2025

Walkthrough

A new static method getMaxValue added has been, calculating maximum valid identifier value from a mask. Modified now the setBits method is, with error handling that throws an std::invalid_argument when input exceeds the maximum allowed. The constant kPassiveMask is removed, leaving the passive identifier under kApproachMask. Updated tests now expect exceptions rather than truncating the value. Changes maintain existing functionality with improved validation.

Changes

File(s) Change Summary
Core/include/Acts/.../GeometryIdentifier.hpp Added constexpr static Value getMaxValue(Value mask); updated setBits to throw std::invalid_argument on overflow; removed kPassiveMask.
Tests/UnitTests/Core/Geometry/GeometryIdentifierTests.cpp Revised tests to assert that an exception is thrown for out-of-range values instead of truncating to zero.

Sequence Diagram(s)

sequenceDiagram
    participant C as Client
    participant GI as GeometryIdentifier
    C->>GI: setBits(mask, id)
    GI->>GI: getMaxValue(mask)
    alt id within valid range
        GI-->>C: Bits set successfully
    else id exceeds max value
        GI-->>C: Throw std::invalid_argument
    end
Loading

Suggested labels

Fails Athena tests

Suggested reviewers

  • benjaminhuth

Poem

In the code galaxy, change now shines bright,
A method new emerges, wisdom in its light.
Errors caught with care, like force, they are strong,
Yoda’s logic guides the bits, never going wrong.
May the code be with you, in harmony all along.

✨ Finishing Touches
  • 📝 Generate Docstrings (Beta)

Thank you for using CodeRabbit. We offer it for free to the OSS community and would appreciate your support in helping us grow. If you find it useful, would you consider giving us a shout-out on your favorite social media?

❤️ Share
🪧 Tips

Chat

There are 3 ways to chat with CodeRabbit:

  • Review comments: Directly reply to a review comment made by CodeRabbit. Example:
    • I pushed a fix in commit <commit_id>, please review it.
    • Generate unit testing code for this file.
    • Open a follow-up GitHub issue for this discussion.
  • Files and specific lines of code (under the "Files changed" tab): Tag @coderabbitai in a new review comment at the desired location with your query. Examples:
    • @coderabbitai generate unit testing code for this file.
    • @coderabbitai modularize this function.
  • PR comments: Tag @coderabbitai in a new PR comment to ask questions about the PR branch. For the best results, please provide a very specific query, as very limited context is provided in this mode. Examples:
    • @coderabbitai gather interesting stats about this repository and render them as a table. Additionally, render a pie chart showing the language distribution in the codebase.
    • @coderabbitai read src/utils.ts and generate unit testing code.
    • @coderabbitai read the files in the src/scheduler package and generate a class diagram using mermaid and a README in the markdown format.
    • @coderabbitai help me debug CodeRabbit configuration file.

Note: Be mindful of the bot's finite context window. It's strongly recommended to break down tasks such as reading entire modules into smaller chunks. For a focused discussion, use review comments to chat about specific files and their changes, instead of using the PR comments.

CodeRabbit Commands (Invoked using PR comments)

  • @coderabbitai pause to pause the reviews on a PR.
  • @coderabbitai resume to resume the paused reviews.
  • @coderabbitai review to trigger an incremental review. This is useful when automatic reviews are disabled for the repository.
  • @coderabbitai full review to do a full review from scratch and review all the files again.
  • @coderabbitai summary to regenerate the summary of the PR.
  • @coderabbitai generate docstrings to generate docstrings for this PR. (Beta)
  • @coderabbitai resolve resolve all the CodeRabbit review comments.
  • @coderabbitai configuration to show the current CodeRabbit configuration for the repository.
  • @coderabbitai help to get help.

Other keywords and placeholders

  • Add @coderabbitai ignore anywhere in the PR description to prevent this PR from being reviewed.
  • Add @coderabbitai summary to generate the high-level summary at a specific location in the PR description.
  • Add @coderabbitai anywhere in the PR title to generate the title automatically.

CodeRabbit Configuration File (.coderabbit.yaml)

  • You can programmatically configure CodeRabbit by adding a .coderabbit.yaml file to the root of your repository.
  • Please see the configuration documentation for more information.
  • If your editor has YAML language server enabled, you can add the path at the top of this file to enable auto-completion and validation: # yaml-language-server: $schema=https://coderabbit.ai/integrations/schema.v2.json

Documentation and Community

  • Visit our Documentation for detailed information on how to use CodeRabbit.
  • Join our Discord Community to get help, request features, and share feedback.
  • Follow us on X/Twitter for updates and announcements.

@github-actions github-actions bot added the Component - Core Affects the Core module label Feb 17, 2025
@paulgessinger paulgessinger force-pushed the refactor/geo-id-checked branch from 3fd9454 to 5673f81 Compare February 17, 2025 12:47
benjaminhuth
benjaminhuth previously approved these changes Feb 17, 2025
Copy link
Member

@benjaminhuth benjaminhuth left a comment

Choose a reason for hiding this comment

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

Sounds like a very good idea! This is never done in a time-critical context, right? I think exception is a good thing to do here

Copy link

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

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

Actionable comments posted: 0

🔭 Outside diff range comments (1)
Core/include/Acts/Geometry/GeometryIdentifier.hpp (1)

130-140: ⚠️ Potential issue

Missing headers, I sense. Include them, you must!

Required headers for exception handling and string operations, missing they are. Add them, you must:

+#include <stdexcept>
+#include <string>

Prevent compilation errors, this will. Strong in error handling, your code is, but headers it needs!

🧰 Tools
🪛 GitHub Actions: Analysis

[error] 132-132: 'invalid_argument' is not a member of 'std'. Did you forget to '#include '?


[error] 133-133: 'to_string' is not a member of 'std'. Did you forget to '#include '?


[error] 134-134: 'to_string' is not a member of 'std'. Did you forget to '#include '?

🧹 Nitpick comments (2)
Core/include/Acts/Geometry/GeometryIdentifier.hpp (1)

95-106: Document the maximum values, we should!

In comments, the maximum values shown are. But in class documentation, mention them we should. Help future Padawans understand the limits, it will.

Add to class documentation, you should:

 /// - Approach surfaces (for a layer)
 /// - Sensitive surfaces (confined to a layer, also called modules)
+///
+/// Maximum values for each component:
+/// - Volume:    255 (8 bits)
+/// - Boundary:  255 (8 bits)
+/// - Layer:     4095 (12 bits)
+/// - Approach:  255 (8 bits)
+/// - Sensitive: 1048575 (20 bits)
+/// - Extra:     255 (8 bits)
Tests/UnitTests/Core/Geometry/GeometryIdentifierTests.cpp (1)

48-59: Well tested, these changes are!

Comprehensive test coverage, you have achieved. All components for overflow, you check. Exception handling, properly verified it is.

Suggest one improvement, I do. Add test case for boundary values, we should:

+  // verify maximum values are accepted
+  BOOST_CHECK_NO_THROW(GeometryIdentifier(ref).setVolume(volumeMax));
+  BOOST_CHECK_NO_THROW(GeometryIdentifier(ref).setBoundary(boundaryMax));
+  BOOST_CHECK_NO_THROW(GeometryIdentifier(ref).setLayer(layerMax));
+  BOOST_CHECK_NO_THROW(GeometryIdentifier(ref).setApproach(approachMax));
+  BOOST_CHECK_NO_THROW(GeometryIdentifier(ref).setSensitive(sensitiveMax));
+  BOOST_CHECK_NO_THROW(GeometryIdentifier(ref).setExtra(extraMax));
📜 Review details

Configuration used: CodeRabbit UI
Review profile: CHILL
Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between e4606c6 and 5673f81.

📒 Files selected for processing (2)
  • Core/include/Acts/Geometry/GeometryIdentifier.hpp (1 hunks)
  • Tests/UnitTests/Core/Geometry/GeometryIdentifierTests.cpp (1 hunks)
🧰 Additional context used
🪛 GitHub Actions: Analysis
Core/include/Acts/Geometry/GeometryIdentifier.hpp

[error] 132-132: 'invalid_argument' is not a member of 'std'. Did you forget to '#include '?


[error] 133-133: 'to_string' is not a member of 'std'. Did you forget to '#include '?


[error] 134-134: 'to_string' is not a member of 'std'. Did you forget to '#include '?

⏰ Context from checks skipped due to timeout of 90000ms (6)
  • GitHub Check: unused_files
  • GitHub Check: missing_includes
  • GitHub Check: macos
  • GitHub Check: linux_ubuntu_extra (ubuntu2204, 20, clang++)
  • GitHub Check: linux_ubuntu_extra (ubuntu2204, 20, g++)
  • GitHub Check: linux_ubuntu
🔇 Additional comments (1)
Core/include/Acts/Geometry/GeometryIdentifier.hpp (1)

121-123: Clean and efficient, this implementation is!

Wisely implemented, the getMaxValue method is. Reuse existing extractShift function it does, and maximum value correctly calculate it does.

@paulgessinger
Copy link
Member Author

@benjaminhuth I don't think so.

I pushed a commit adding a missing include. Can you approve again?

Copy link

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

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

Actionable comments posted: 0

🔭 Outside diff range comments (1)
Core/include/Acts/Geometry/GeometryIdentifier.hpp (1)

97-107: 💡 Verification agent

🧩 Analysis chain

Verify the wisdom of our masks, we must.

Test all bit masks with maximum values, essential it is.


🏁 Script executed:

#!/bin/bash
# Description: Generate test cases for all masks maximum values

echo "Generating test cases for maximum values:"
for mask in "kVolumeMask" "kBoundaryMask" "kLayerMask" "kApproachMask" "kSensitiveMask" "kExtraMask"; do
  rg --type cpp "static constexpr.*$mask.*=.*0x([0-9a-f]+)" -o | \
    awk '{print "Test case for " $1 ": value = 0x" $NF}'
done

Length of output: 2065


Verify the profound wisdom of our masks, we must – with a slight refinement, yes.

The bit masks defined in Core/include/Acts/Geometry/GeometryIdentifier.hpp have been exercised with maximum values, and their outputs confirm their intended sizes. Hmmm, however, a minor quirk observed there is: the printed output from the test script shows a redundant "0x" prefix (for example, printing 0x0x00ff000000000000). Adjust the test script’s output formatting—perhaps by removing the additional "0x" in the AWK print command—so clarity, we ensure.

  • Confirmed, the masks produce their maximum values as intended.
  • Suggested, clean the output from the test script to remove the duplicate "0x" prefix.
  • Test script remains largely the same, serving its purpose well.

May the Force guide you in polishing this detail.

🧹 Nitpick comments (2)
Core/include/Acts/Geometry/GeometryIdentifier.hpp (2)

122-124: Strong with the Force, this method is. Yet documentation, it lacks.

Clear and concise implementation, but document its purpose and return value, we must.

Add documentation like this, you should:

+  /// Calculate the maximum allowed value for a given bit mask
+  /// @param mask The bit mask to calculate maximum value for
+  /// @return The maximum value that can be stored in the masked bits
   constexpr static Value getMaxValue(Value mask) {

132-136: Protect against the dark side of overflow, this validation does.

Clear error message, it provides. Yet discuss alternative approaches to exception throwing, we should.

Consider these alternatives, you might:

  1. Return Result instead of throwing
  2. Clamp value to maximum instead of throwing
  3. Add a validation-only method for upstream checks

Your thoughts on these approaches, share with us you must, @benjaminhuth and @andiwand.

📜 Review details

Configuration used: CodeRabbit UI
Review profile: CHILL
Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between 5673f81 and 2764d27.

📒 Files selected for processing (1)
  • Core/include/Acts/Geometry/GeometryIdentifier.hpp (2 hunks)
⏰ Context from checks skipped due to timeout of 90000ms (3)
  • GitHub Check: merge-sentinel
  • GitHub Check: CI Bridge / build_gnn_tensorrt
  • GitHub Check: macos
🔇 Additional comments (1)
Core/include/Acts/Geometry/GeometryIdentifier.hpp (1)

14-14: Wise addition of error handling capabilities, this is.

The force of std::invalid_argument, properly included it now is.

Copy link

github-actions bot commented Feb 17, 2025

📊: Physics performance monitoring for dec1164

Full contents

physmon summary

andiwand
andiwand previously approved these changes Feb 18, 2025
Copy link
Contributor

@andiwand andiwand left a comment

Choose a reason for hiding this comment

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

IMO exceptions provide better error handling over Result. As long as we cannot proof that it's slower I would always opt for exceptions

Copy link
Contributor

kodiakhq bot commented Feb 18, 2025

This PR currently has a merge conflict. Please resolve this and then re-add the automerge label.

Copy link

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

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

Actionable comments posted: 0

🧹 Nitpick comments (1)
Tests/UnitTests/Core/Geometry/GeometryIdentifierTests.cpp (1)

48-59: Test the withXXX methods also, we must!

From past learnings, wisdom we gain. Both setXXX and withXXX methods, tested they should be. Compatibility with both mutable and immutable APIs, ensure we must.

Add these tests, you should:

+ BOOST_CHECK_THROW(GeometryIdentifier(ref).withVolume(volumeMax + 1),
+                   std::invalid_argument);
+ BOOST_CHECK_THROW(GeometryIdentifier(ref).withBoundary(boundaryMax + 1),
+                   std::invalid_argument);
+ BOOST_CHECK_THROW(GeometryIdentifier(ref).withLayer(layerMax + 1),
+                   std::invalid_argument);
+ BOOST_CHECK_THROW(GeometryIdentifier(ref).withApproach(approachMax + 1),
+                   std::invalid_argument);
+ BOOST_CHECK_THROW(GeometryIdentifier(ref).withSensitive(sensitiveMax + 1),
+                   std::invalid_argument);
+ BOOST_CHECK_THROW(GeometryIdentifier(ref).withExtra(extraMax + 1),
+                   std::invalid_argument);
📜 Review details

Configuration used: CodeRabbit UI
Review profile: CHILL
Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between 2764d27 and dec1164.

📒 Files selected for processing (2)
  • Core/include/Acts/Geometry/GeometryIdentifier.hpp (1 hunks)
  • Tests/UnitTests/Core/Geometry/GeometryIdentifierTests.cpp (1 hunks)
🧰 Additional context used
🧠 Learnings (1)
Tests/UnitTests/Core/Geometry/GeometryIdentifierTests.cpp (1)
Learnt from: paulgessinger
PR: acts-project/acts#4095
File: Tests/UnitTests/Core/Geometry/GeometryIdentifierTests.cpp:63-72
Timestamp: 2025-02-17T14:31:10.699Z
Learning: In GeometryIdentifier tests, both `setXXX` and `withXXX` methods should be tested to ensure compatibility with both mutable and immutable APIs.
⏰ Context from checks skipped due to timeout of 90000ms (8)
  • GitHub Check: CI Bridge / build_gnn_tensorrt
  • GitHub Check: merge-sentinel
  • GitHub Check: macos
  • GitHub Check: linux_ubuntu_extra (ubuntu2204, 20, clang++)
  • GitHub Check: missing_includes
  • GitHub Check: linux_ubuntu_extra (ubuntu2204, 20, g++)
  • GitHub Check: linux_ubuntu
  • GitHub Check: build_debug
🔇 Additional comments (3)
Core/include/Acts/Geometry/GeometryIdentifier.hpp (2)

192-194: Approve this implementation, I do!

Simple yet powerful, this method is. Maximum value from mask, correctly it calculates. Clean and efficient, the implementation remains.


201-212: Wise decision to add value range checks, this is!

Prevent overflow issues, this change will. Clear error messages, it provides. The Force, strong with this implementation, it is.

Tests/UnitTests/Core/Geometry/GeometryIdentifierTests.cpp (1)

48-59: Approve these tests, I do!

Well-crafted tests, these are. Each component's boundaries, they verify. Exception handling, thoroughly tested it is.

@kodiakhq kodiakhq bot merged commit cc3b0a4 into acts-project:main Feb 18, 2025
48 checks passed
@acts-project-service acts-project-service added the Fails Athena tests This PR causes a failure in the Athena tests label Feb 18, 2025
kodiakhq bot pushed a commit that referenced this pull request Feb 19, 2025
Surface the maximum values for the `GeometryIdentifier` components.

Blocked by:
- #4094
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Component - Core Affects the Core module Fails Athena tests This PR causes a failure in the Athena tests
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants