Skip to content

Make ResourceSessionFactory generic over its session type#1007

Open
sergiopaniego wants to merge 1 commit into
huggingface:mainfrom
sergiopaniego:generic-resource-session-factory
Open

Make ResourceSessionFactory generic over its session type#1007
sergiopaniego wants to merge 1 commit into
huggingface:mainfrom
sergiopaniego:generic-resource-session-factory

Conversation

@sergiopaniego

@sergiopaniego sergiopaniego commented Jul 23, 2026

Copy link
Copy Markdown
Member

Makes ResourceSessionFactory generic over the session type it produces.

Why

ResourceSessionFactory.create() is typed as returning the base ResourceSession, so callers lose the concrete session type and have to cast. This is one of the TODO(@openenv) gaps flagged in TRL's loop-owning harness rollout work (huggingface/trl#6420), where the worker does cast(LoopOwningSession, session) because the factory type cannot express which session subtype it returns.

Change

  • ResourceSessionFactory(ABC, Generic[SessionT]) with SessionT bound to ResourceSession, and create() returns SessionT.
  • OpenCodeSessionFactory is now ResourceSessionFactory[OpenCodeSession], so its create() types as returning OpenCodeSession.

Compatibility

Backward compatible: subclassing without a type argument (class MyFactory(ResourceSessionFactory)) stays valid and behaves exactly as before. The other envs that subclass it (browsergym, openspiel, reasoning_gym) are unaffected and import cleanly. Pure typing change, no runtime behavior difference.

AI-assisted.


Note

Low Risk
Pure static typing change with no runtime or API behavior differences; existing non-parameterized subclasses stay compatible.

Overview
ResourceSessionFactory is now generic so create() can be typed to return the concrete session class instead of always ResourceSession.

The harness adds SessionT (bound to ResourceSession), ResourceSessionFactory(ABC, Generic[SessionT]), and changes create()’s return type from ResourceSession to SessionT. OpenCodeSessionFactory is updated to ResourceSessionFactory[OpenCodeSession], aligning the factory type with its existing create() -> OpenCodeSession implementation.

Subclasses that omit the type parameter (e.g. browsergym, openspiel) remain valid; this is typing-only with no runtime behavior change.

Reviewed by Cursor Bugbot for commit ed861aa. Bugbot is set up for automated code reviews on this repo. Configure here.

ResourceSessionFactory.create() was typed as returning the base ResourceSession,
so callers lost the concrete session type and had to cast. Parameterize the
factory over a SessionT bound to ResourceSession, so a subclass like
OpenCodeSessionFactory(ResourceSessionFactory[OpenCodeSession]) types create()
as returning OpenCodeSession. Subclassing without a type argument stays valid
and behaves as before, so existing envs are unaffected.
Copilot AI review requested due to automatic review settings July 23, 2026 16:19

Copilot AI left a comment

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.

Copilot was unable to review this pull request because the user who requested the review has reached their quota limit.

@bot-ci-comment

Copy link
Copy Markdown

The docs for this PR live here. All of your documentation changes will be reflected on that endpoint. The docs are available until 30 days after the last update.

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