Skip to content

Add on_use_handlers dict field for backward compatibility with cogames - #1

Open
aaln wants to merge 1 commit into
mainfrom
fix/add-on-use-handlers-compat
Open

Add on_use_handlers dict field for backward compatibility with cogames#1
aaln wants to merge 1 commit into
mainfrom
fix/add-on-use-handlers-compat

Conversation

@aaln

@aaln aaln commented Apr 16, 2026

Copy link
Copy Markdown
Contributor

Summary

  • Adds on_use_handlers: dict[str, Handler] field to GridObjectConfig for backward compatibility with cogames
  • The dict is automatically merged into the singular on_use_handler field via firstMatch() in the existing model validator
  • Fixes cogames import crash: pydantic_core._pydantic_core.ValidationError: Extra inputs are not permitted for on_use_handlers

Context

The standalone mettagrid repo replaced on_use_handlers (dict) with on_use_handler (singular AnyHandler | None) using composite FirstMatch/AllOf handler types. However, cogames extensively uses the dict form (on_use_handlers={...}) across extractors, hubs, junctions, cognitive substrate evals, and overcogged game configs.

This adds the dict field back as a convenience that bridges to the new singular API — the model validator merges the dict entries into a FirstMatch composite, preserving the same runtime behavior.

Test plan

  • Verified GridObjectConfig(on_use_handlers={"solve": Handler()}) correctly merges into on_use_handler
  • Verified single-entry dict collapses to plain Handler (via firstMatch flattening)
  • Verified empty dict leaves on_use_handler as None
  • Verified both on_use_handler and on_use_handlers merge correctly
  • Verified AgentConfig (inherits from GridObjectConfig) also works
  • Verified exact cogames exploration.py pattern works without error

Made with Cursor

cogames passes on_use_handlers as a dict[str, Handler] in many places
(extractors, hubs, junctions, cognitive substrate evals, overcogged).
The standalone mettagrid repo only has the singular on_use_handler field,
causing a pydantic ValidationError on import.

Adds on_use_handlers as a convenience field that gets merged into
on_use_handler via firstMatch() in the model validator.

Made-with: Cursor
@aaln

aaln commented Apr 16, 2026

Copy link
Copy Markdown
Contributor Author

This PR adds backward compatibility for the on_use_handlers dict API that cogames relies on extensively.

The standalone mettagrid repo introduced composite handler types (FirstMatch/AllOf) and replaced on_use_handlers: dict[str, Handler] with on_use_handler: AnyHandler | None. However, cogames still uses the dict form in ~20+ places (extractors, hubs, junctions, cognitive substrate evals, overcogged, gear stations). This causes a pydantic ValidationError: Extra inputs are not permitted on any cogames import.

The fix is 8 lines: it adds on_use_handlers back as a convenience field and merges it into on_use_handler via firstMatch() in the existing model validator. Single-entry dicts collapse to a plain Handler, multi-entry dicts become a FirstMatch composite, and if both fields are provided they merge correctly. No behavior change for anyone already using the singular on_use_handler API.

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