Skip to content

Make ResourceSessionFactory generic over its session type - #1007

Merged
burtenshaw merged 2 commits into
huggingface:mainfrom
sergiopaniego:generic-resource-session-factory
Jul 28, 2026
Merged

Make ResourceSessionFactory generic over its session type#1007
burtenshaw merged 2 commits 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
Typing-only change with no runtime or behavioral impact; existing factory subclasses without type arguments stay compatible.

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

In openenv.core.harness, the factory adds SessionT (bound to ResourceSession) and create() is annotated as -> SessionT. OpenCodeSessionFactory is updated to ResourceSessionFactory[OpenCodeSession], so callers get OpenCodeSession from create() without casts—relevant for TRL loop-owning harness work that previously needed cast(LoopOwningSession, session).

No runtime behavior change. Subclasses that omit the type parameter (e.g. browsergym, openspiel, reasoning_gym) remain valid and unchanged.

Reviewed by Cursor Bugbot for commit 37100cc. 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.

Copilot AI review requested due to automatic review settings July 27, 2026 13:43

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.

@burtenshaw
burtenshaw merged commit 7a882b8 into huggingface:main Jul 28, 2026
8 checks passed
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.

3 participants