@@ -958,6 +958,75 @@ def test_did_you_mean_spans_user_dir(self, monkeypatch: MonkeyPatch, tmp_path, c
958958 assert "Did you mean `mybench`?" in capsys .readouterr ().err
959959
960960
961+ class TestInstallRootResolution :
962+ """Built-in assets resolve from the Gym install root regardless of cwd (REQ C2/C5).
963+
964+ In a wheel install PARENT_DIR is site-packages (where the asset trees are installed) while the
965+ user runs from an unrelated project dir, so WORKING_DIR/cwd is not the install root. The selector
966+ must still find built-ins under PARENT_DIR. Editable-from-repo keeps working because PARENT_DIR,
967+ WORKING_DIR, and cwd all coincide and dedupe to a single root.
968+ """
969+
970+ def _make_resources_server (self , root , name : str = "foo" ) -> None :
971+ config_dir = root / "resources_servers" / name / "configs"
972+ config_dir .mkdir (parents = True )
973+ (config_dir / f"{ name } .yaml" ).write_text ("{}\n " )
974+
975+ def test_builtin_resolves_from_install_root_when_cwd_differs (self , monkeypatch : MonkeyPatch , tmp_path ) -> None :
976+ install_root = tmp_path / "site-packages"
977+ user_cwd = tmp_path / "my-project"
978+ install_root .mkdir ()
979+ user_cwd .mkdir ()
980+ self ._make_resources_server (install_root ) # built-in only under the install root
981+ monkeypatch .setattr (cli_main , "PARENT_DIR" , install_root )
982+ monkeypatch .setattr (cli_main , "WORKING_DIR" , user_cwd )
983+ monkeypatch .chdir (user_cwd )
984+
985+ resolved = cli_main ._asset_config_path ("resources-server" , "foo" )
986+ assert resolved == str (install_root / "resources_servers" / "foo" / "configs" / "foo.yaml" )
987+
988+ def test_user_cwd_asset_resolves_when_not_builtin (self , monkeypatch : MonkeyPatch , tmp_path ) -> None :
989+ install_root = tmp_path / "site-packages"
990+ user_cwd = tmp_path / "my-project"
991+ install_root .mkdir ()
992+ user_cwd .mkdir ()
993+ self ._make_resources_server (user_cwd , name = "myenv" ) # exists only in the user's project
994+ monkeypatch .setattr (cli_main , "PARENT_DIR" , install_root )
995+ monkeypatch .setattr (cli_main , "WORKING_DIR" , user_cwd )
996+ monkeypatch .chdir (user_cwd )
997+
998+ resolved = cli_main ._asset_config_path ("resources-server" , "myenv" )
999+ assert resolved == str (user_cwd / "resources_servers" / "myenv" / "configs" / "myenv.yaml" )
1000+
1001+ def test_same_name_in_install_root_and_cwd_is_ambiguous (self , monkeypatch : MonkeyPatch , tmp_path ) -> None :
1002+ # A user asset shadowing a built-in of the same name is ambiguous; they must disambiguate with --config.
1003+ install_root = tmp_path / "site-packages"
1004+ user_cwd = tmp_path / "my-project"
1005+ install_root .mkdir ()
1006+ user_cwd .mkdir ()
1007+ self ._make_resources_server (install_root )
1008+ self ._make_resources_server (user_cwd )
1009+ monkeypatch .setattr (cli_main , "PARENT_DIR" , install_root )
1010+ monkeypatch .setattr (cli_main , "WORKING_DIR" , user_cwd )
1011+ monkeypatch .chdir (user_cwd )
1012+
1013+ with pytest .raises (ValueError , match = "ambiguous" ):
1014+ cli_main ._asset_config_path ("resources-server" , "foo" )
1015+
1016+ def test_editable_layout_single_root_not_self_ambiguous (self , monkeypatch : MonkeyPatch , tmp_path ) -> None :
1017+ # Editable install: PARENT_DIR == WORKING_DIR == cwd. The same file found via all three roots
1018+ # must dedupe to one match, not raise a spurious ambiguity error.
1019+ repo_root = tmp_path / "Gym"
1020+ repo_root .mkdir ()
1021+ self ._make_resources_server (repo_root )
1022+ monkeypatch .setattr (cli_main , "PARENT_DIR" , repo_root )
1023+ monkeypatch .setattr (cli_main , "WORKING_DIR" , repo_root )
1024+ monkeypatch .chdir (repo_root )
1025+
1026+ resolved = cli_main ._asset_config_path ("resources-server" , "foo" )
1027+ assert resolved == str (repo_root / "resources_servers" / "foo" / "configs" / "foo.yaml" )
1028+
1029+
9611030class TestListEnvironmentsRouting :
9621031 def test_list_environments_dispatches (self , monkeypatch : MonkeyPatch ) -> None :
9631032 target , overrides = _dispatch_for (monkeypatch , ["list" , "environments" ])
0 commit comments