Skip to content

Conversation

@daveey
Copy link
Contributor

@daveey daveey commented Jan 19, 2026

Summary

  • Add TagIndex class (std::unordered_map<int, std::vector<GridObject*>>) for O(1) tag-based object lookups
  • Add mutable tag methods to GridObject (has_tag, add_tag, remove_tag) that automatically sync with TagIndex
  • Integrate TagIndex with MettaGrid - populates on object creation, exposed to Python via pybind11
  • Add AddTagMutation and RemoveTagMutation for event-based tag modification
  • Add tag_id accessors to TagFilter and ObjectTypeFilter for efficient lookup
  • EventScheduler now uses TagIndex for pre-filtering event targets (O(1) vs O(n))
  • Add validation requiring ObjectTypeFilter in EventConfig for TagIndex efficiency
  • Update existing events to include ObjectTypeFilter (HasTag)

Test plan

  • All TagIndex unit tests passing (8 tests)
  • All event validation tests passing (2 tests)
  • All event config tests passing (37 tests)
  • All clip takeover event tests passing (12 tests)
  • All cogames event tests passing (4 tests)
  • Total: 113 tests passing

🤖 Generated with Claude Code

@blacksmith-sh

This comment has been minimized.

Copy link

@chatgpt-codex-connector chatgpt-codex-connector bot left a comment

Choose a reason for hiding this comment

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

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: 708946fc58

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

Comment on lines 118 to 122
cpp_filter = CppObjectTypeFilterConfig()
cpp_filter.entity = convert_entity_ref(filter_config.target)
# Use hash of tag name as ID (this is a placeholder - actual implementation
# should use proper tag name to ID mapping)
cpp_filter.tag_id = hash(tag_name) % (2**31)

Choose a reason for hiding this comment

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

P1 Badge Map ObjectTypeFilter tags to real tag IDs

The ObjectTypeFilter conversion sets tag_id to hash(tag_name) % (2**31), which does not match the tag IDs generated by IdMap/tag_name_to_id and is randomized by PYTHONHASHSEED. That means any ObjectTypeFilter in handlers/events will never match actual objects and TagIndex pre-filtering will always return empty candidates, so those events/handlers won't fire. Use the tag_name_to_id mapping (and raise if the tag is unknown) instead of hashing.

Useful? React with 👍 / 👎.

@daveey daveey changed the base branch from main to graphite-base/4990 January 19, 2026 23:15
@daveey daveey changed the base branch from graphite-base/4990 to daveey-events January 19, 2026 23:16
Copy link
Contributor Author

daveey commented Jan 19, 2026

Warning

This pull request is not mergeable via GitHub because a downstack PR is open. Once all requirements are satisfied, merge this PR as a stack on Graphite.
Learn more


How to use the Graphite Merge Queue

Add either label to this PR to merge it via the merge queue:

  • add-to-merge-queue - adds this PR to the back of the merge queue
  • add-to-merge-queue-as-hotfix - for urgent hot fixes, skip the queue and merge this PR next

You must have a Graphite account in order to use the merge queue. Sign up using this link.

An organization admin has enabled the Graphite Merge Queue in this repository.

Please do not merge from GitHub as this will restart CI on PRs being processed by the merge queue.

This stack of pull requests is managed by Graphite. Learn more about stacking.

@datadog-official

This comment has been minimized.

@openhands-ai
Copy link

openhands-ai bot commented Jan 19, 2026

Looks like there are a few issues preventing this PR from being merged!

  • GitHub Actions are failing:
    • Test and Benchmark

If you'd like me to help, just leave a comment, like

@OpenHands please fix the failing actions on PR #4990 at branch `daveey-tag-index`

Feel free to include any additional details that might help me get this PR into a better state.

You can manage your notification settings

daveey and others added 10 commits January 19, 2026 15:32
Add a TagIndex class that maintains a mapping from tag IDs to lists of
GridObjects with that tag. This provides O(1) lookup for finding objects
by tag, which will be used by EventScheduler for fast event filtering.

The TagIndex provides:
- register_object: Index all tags on an object
- unregister_object: Remove object from all tag indices
- on_tag_added/removed: Dynamic tag updates
- get_objects_with_tag: O(1) lookup by tag ID
- count_objects_with_tag: Get count without allocation

Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
Add has_tag, add_tag, and remove_tag methods to GridObject for runtime
tag manipulation. These methods integrate with TagIndex to keep indices
synchronized when tags change.

Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
- Add TagIndex member to MettaGrid class
- Wire up TagIndex to objects on creation via set_tag_index and register_object
- Add tag_index() Python binding for accessing TagIndex from Python
- Add count_objects_with_tag method to TagIndex Python binding
- Fix tag ID mapping to include default agent tags for consistency between
  Python IdMap.tag_names() and C++ convert_to_cpp_game_config

This enables efficient tag-based object lookup from both C++ and Python.
The TagIndex is populated when objects are created and stays in sync when
tags are added or removed via add_tag/remove_tag methods.

Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
Add mutation classes that allow handlers and events to add or remove
tags from objects. This enables dynamic tag manipulation during
gameplay, allowing objects to change their tags based on game events.

Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
- Add get_filters() accessor to Event class
- Update EventScheduler::process_timestep to accept TagIndex
- Implement pre-filtering logic that finds most selective tag filter
- Use TagIndex to get candidate objects before applying remaining filters

Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
…ciency

- Add model_validator to EventConfig requiring at least one ObjectTypeFilter
- Events must have tag-based filters for efficient O(1) lookups via TagIndex
- Update all existing event configurations in tests and missions to include HasTag
- Add test_event_validation.py with validation tests

Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
Add note to test_tag_index.py explaining that event-based integration
tests are pending EventScheduler integration with MettaGrid. The core
tag mutation + TagIndex sync functionality is already tested in
test_tag_index_syncs_with_add_remove.

Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
…ective resolution

- Add events field to C++ GameConfig and pybind11 bindings
- Add convert_event_configs_to_cpp for Python-to-C++ event conversion
- Pass collectives_by_name to EventScheduler for resolving collective pointers
- Add get_mutations() accessor to Event class for collective resolution
- Call EventScheduler.process_timestep() from MettaGrid._step()
- Add integration tests verifying events fire and apply mutations correctly

The key fix was resolving collective pointers in AlignToCollectiveMutation
during EventScheduler initialization, as set_collective() was never being
called, leaving the resolved pointer as nullptr.

Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
After rebasing daveey-tag-index onto daveey-events, several
inconsistencies arose due to overlapping changes. This commit
fixes:

- Add ObjectTypeFilterConfig, CollectiveFilterConfig, and
  NearCollectiveFilterConfig to C++ handler_config.hpp
- Add pybind11 bindings for new filter config types
- Fix TagFilter/ObjectTypeFilter to use single tag_id
- Add max_targets field to EventConfig and its pybind11 binding
- Update tests to use HasTag (ObjectTypeFilter) instead of
  hasTag (TagFilter) where EventConfig requires it
- Add Python imports for new filter config types

Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
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.

2 participants