From a663ad309d4247d95be992e7a46c723f99c7214b Mon Sep 17 00:00:00 2001 From: TATP-233 Date: Thu, 3 Dec 2026 12:46:13 +0800 Subject: [PATCH] fix: resolve joint limits and stabilize PR checkouts Resolve declared entity joint ranges by name on backends with named queries, with the existing layout fallback for other backends. Check out immutable PR head SHAs so merging cannot delete a merge ref before in-flight jobs start. Closes #2679. Refs #2669. --- .github/workflows/ci.yml | 14 +++--- .github/workflows/docs.yml | 2 + .github/workflows/tensor-runtime-ci.yml | 2 +- docs/sphinx/source/changelog.md | 16 +++++++ src/unilab/base/entity.py | 10 +++++ tests/base/test_entity_facade.py | 6 ++- tests/base/test_sim_backend_smoke.py | 46 ++++++++++++++++++++ tests/envs/mdp/test_events.py | 5 ++- tests/envs/mdp/test_joint_position_action.py | 5 ++- tests/envs/mdp/test_observations.py | 5 ++- tests/envs/test_manager_based_rl_env.py | 5 ++- tests/managers/test_device_contracts.py | 5 ++- tests/scripts/test_ci_scope.py | 11 +++++ 13 files changed, 112 insertions(+), 20 deletions(-) diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index 097cdf9cb..ba83e1a05 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -29,7 +29,7 @@ jobs: steps: - uses: actions/checkout@v6.0.2 with: - ref: ${{ github.event_name == 'workflow_dispatch' && github.ref || format('refs/pull/{0}/merge', github.event.pull_request.number) }} + ref: ${{ github.event_name == 'pull_request' && github.event.pull_request.head.sha || github.ref }} persist-credentials: false - name: Install uv uses: astral-sh/setup-uv@v8.0.0 @@ -49,7 +49,7 @@ jobs: steps: - uses: actions/checkout@v6.0.2 with: - ref: ${{ github.event_name == 'workflow_dispatch' && github.ref || format('refs/pull/{0}/merge', github.event.pull_request.number) }} + ref: ${{ github.event_name == 'pull_request' && github.event.pull_request.head.sha || github.ref }} persist-credentials: false - name: Install uv uses: astral-sh/setup-uv@v8.0.0 @@ -67,7 +67,7 @@ jobs: steps: - uses: actions/checkout@v6.0.2 with: - ref: ${{ github.event_name == 'workflow_dispatch' && github.ref || format('refs/pull/{0}/merge', github.event.pull_request.number) }} + ref: ${{ github.event_name == 'pull_request' && github.event.pull_request.head.sha || github.ref }} persist-credentials: false - name: Install uv uses: astral-sh/setup-uv@v8.0.0 @@ -103,7 +103,7 @@ jobs: steps: - uses: actions/checkout@v6.0.2 with: - ref: ${{ github.event_name == 'workflow_dispatch' && github.ref || format('refs/pull/{0}/merge', github.event.pull_request.number) }} + ref: ${{ github.event_name == 'pull_request' && github.event.pull_request.head.sha || github.ref }} persist-credentials: false - name: Install uv uses: astral-sh/setup-uv@v8.0.0 @@ -139,7 +139,7 @@ jobs: steps: - uses: actions/checkout@v6.0.2 with: - ref: ${{ github.event_name == 'workflow_dispatch' && github.ref || format('refs/pull/{0}/merge', github.event.pull_request.number) }} + ref: ${{ github.event_name == 'pull_request' && github.event.pull_request.head.sha || github.ref }} persist-credentials: false - name: Install uv uses: astral-sh/setup-uv@v8.0.0 @@ -218,7 +218,7 @@ jobs: steps: - uses: actions/checkout@v6.0.2 with: - ref: ${{ github.event_name == 'workflow_dispatch' && github.ref || format('refs/pull/{0}/merge', github.event.pull_request.number) }} + ref: ${{ github.event_name == 'pull_request' && github.event.pull_request.head.sha || github.ref }} persist-credentials: false - name: Install uv uses: astral-sh/setup-uv@v8.0.0 @@ -292,7 +292,7 @@ jobs: steps: - uses: actions/checkout@v6.0.2 with: - ref: ${{ github.event_name == 'workflow_dispatch' && github.ref || format('refs/pull/{0}/merge', github.event.pull_request.number) }} + ref: ${{ github.event_name == 'pull_request' && github.event.pull_request.head.sha || github.ref }} persist-credentials: false - name: Install uv uses: astral-sh/setup-uv@v8.0.0 diff --git a/.github/workflows/docs.yml b/.github/workflows/docs.yml index 648f74031..c413286a0 100644 --- a/.github/workflows/docs.yml +++ b/.github/workflows/docs.yml @@ -47,6 +47,8 @@ jobs: steps: - name: Checkout uses: actions/checkout@v4 + with: + ref: ${{ github.event_name == 'pull_request' && github.event.pull_request.head.sha || github.ref }} - name: Install uv uses: astral-sh/setup-uv@v8.0.0 diff --git a/.github/workflows/tensor-runtime-ci.yml b/.github/workflows/tensor-runtime-ci.yml index e479c3adb..a310c3cb2 100644 --- a/.github/workflows/tensor-runtime-ci.yml +++ b/.github/workflows/tensor-runtime-ci.yml @@ -34,7 +34,7 @@ jobs: steps: - uses: actions/checkout@v6.0.2 with: - ref: ${{ github.event_name == 'workflow_dispatch' && github.ref || format('refs/pull/{0}/merge', github.event.pull_request.number) }} + ref: ${{ github.event_name == 'pull_request' && github.event.pull_request.head.sha || github.ref }} persist-credentials: false - name: Install uv uses: astral-sh/setup-uv@v8.0.0 diff --git a/docs/sphinx/source/changelog.md b/docs/sphinx/source/changelog.md index 6184c90a1..948ea7ef7 100644 --- a/docs/sphinx/source/changelog.md +++ b/docs/sphinx/source/changelog.md @@ -34,6 +34,14 @@ PyPI 版本变更与未发布变更;发布日期采用 PyPI 上传日期。完 捕获期间挂起循环垃圾回收器 ([#2668](https://github.com/Motphys/UniLab/issues/2668))。 +- Pull-request workflows now check out the immutable PR head commit instead of + the ephemeral `refs/pull//merge` ref, so merging a PR cannot make its + in-flight jobs fail during checkout + ([#2669](https://github.com/Motphys/UniLab/pull/2669)). + PR 工作流现在 checkout 不可变的 PR head 提交,不再使用临时的 + `refs/pull//merge` ref,避免 PR 合并导致运行中的任务在 checkout 时失败 + ([#2669](https://github.com/Motphys/UniLab/pull/2669))。 + ### Changed / 变更 - Updated the `unisim-core` pin from 2.9.0 to 2.9.1 in the default and ROCm @@ -79,6 +87,14 @@ PyPI 版本变更与未发布变更;发布日期采用 PyPI 上传日期。完 ### Fixed / 修复 +- Non-composed entities now resolve declared joint position limits by joint name + on backends with named range queries, preventing qpos offsets introduced by + non-root free joints from selecting the wrong row or raising an out-of-bounds + error ([#2679](https://github.com/Motphys/UniLab/issues/2679)). + 非 composed 实体现在在支持按名称查询限位的后端上按关节名称解析声明的关节位置限位, + 避免非 root freejoint 引入的 qpos 偏移导致选取错误行或下标越界 + ([#2679](https://github.com/Motphys/UniLab/issues/2679))。 + - Manager-based MuJoCo and MJWarp scenes now resolve declared sites from authored models before backend construction, tracking only their dynamic parent bodies instead of every named body when resolution is available. Scene and physical-entity diff --git a/src/unilab/base/entity.py b/src/unilab/base/entity.py index 7096fbff3..0027cee4f 100644 --- a/src/unilab/base/entity.py +++ b/src/unilab/base/entity.py @@ -3216,6 +3216,16 @@ def _materialize_soft_joint_pos_limits( result = np.asarray(backend.get_joint_range(names=self._joint_names)).copy() result.setflags(write=False) return result + try: + named_ranges = backend.get_joint_range(names=self._joint_names) + except NotImplementedError: + pass + else: + if named_ranges is None: + return None + result = np.asarray(named_ranges).copy() + result.setflags(write=False) + return result try: raw_ranges = backend.get_joint_range() except (AttributeError, NotImplementedError) as exc: diff --git a/tests/base/test_entity_facade.py b/tests/base/test_entity_facade.py index ba03359d6..a6aa5dd6d 100644 --- a/tests/base/test_entity_facade.py +++ b/tests/base/test_entity_facade.py @@ -170,9 +170,11 @@ def get_default_dof_pos(self) -> np.ndarray: self._check("default joint position") return np.arange(5, dtype=np.float32) + 10.0 - def get_joint_range(self) -> np.ndarray: + def get_joint_range(self, *, names=None) -> np.ndarray: self._check("joint position limits") - return self.joint_range + if names is None: + return self.joint_range + return self.joint_range[[self.joint_ids[name] for name in names]] def get_tracked_body_views(self, body_names, device=None) -> TrackedBodyStateViews: capability = { diff --git a/tests/base/test_sim_backend_smoke.py b/tests/base/test_sim_backend_smoke.py index e1a8d71a2..e6c99806b 100644 --- a/tests/base/test_sim_backend_smoke.py +++ b/tests/base/test_sim_backend_smoke.py @@ -24,6 +24,7 @@ ) from unilab.assets import ASSETS_ROOT_PATH +from unilab.base.entity import EntityCfg, EntityScene from unilab.base.scene import SceneCfg pytest.importorskip("mujoco", reason="mujoco not installed") @@ -150,6 +151,51 @@ def test_mujoco_backend_smoke_contract(robot): assert caps.supports_interval_term(INTERVAL_TERM_BODY_TORQUE) +def test_entity_joint_limits_follow_non_root_freejoint_qpos_offset(tmp_path): + from unilab.base.backend_factory import create_backend + + model_file = tmp_path / "free_object_with_hinge.xml" + model_file.write_text( + """ + + + + + + + + + + + + + + + + + + +""".strip() + ) + backend = create_backend( + "mujoco", SceneCfg(model_file=str(model_file)), num_envs=1, sim_dt=SIM_DT + ) + np.testing.assert_array_equal(backend.get_joint_dof_pos_indices(["hinge"]), [8]) + + scene = EntityScene( + { + "robot": EntityCfg(joint_names=("slide",), actuator_names=("slide_ctrl",)), + "object": EntityCfg(root_body_name="lid_base", joint_names=("hinge",)), + }, + backend, + ) + + robot_limits = scene["robot"].data.soft_joint_pos_limits(torch.device("cpu")) + object_limits = scene["object"].data.soft_joint_pos_limits(torch.device("cpu")) + torch.testing.assert_close(robot_limits, torch.tensor([[-1.0, 1.0]])) + torch.testing.assert_close(object_limits, torch.tensor([[-0.5, 0.5]])) + + def test_mujoco_interval_root_velocity_kick_is_row_selective_and_refreshes_sensors(): from unisim.backend.mujoco.backend import MuJoCoBackend diff --git a/tests/envs/mdp/test_events.py b/tests/envs/mdp/test_events.py index eb7a6aedd..89ebedd91 100644 --- a/tests/envs/mdp/test_events.py +++ b/tests/envs/mdp/test_events.py @@ -344,8 +344,9 @@ def get_dof_vel(self) -> np.ndarray: def get_default_dof_pos(self) -> np.ndarray: return np.zeros(3) - def get_joint_range(self) -> np.ndarray: - return np.tile([-1.0, 1.0], (3, 1)) + def get_joint_range(self, *, names=None) -> np.ndarray: + count = len(names) if names is not None else 3 + return np.tile([-1.0, 1.0], (count, 1)) def get_joint_dof_indices(self, names) -> np.ndarray: table = {"j0": 6, "j1": 7, "j2": 8} diff --git a/tests/envs/mdp/test_joint_position_action.py b/tests/envs/mdp/test_joint_position_action.py index 92413f525..32bac611a 100644 --- a/tests/envs/mdp/test_joint_position_action.py +++ b/tests/envs/mdp/test_joint_position_action.py @@ -79,8 +79,9 @@ def get_dof_vel(self) -> np.ndarray: def get_default_dof_pos(self) -> np.ndarray: return np.asarray([0.1, 0.2, 0.3], dtype=np.float32) - def get_joint_range(self) -> np.ndarray: - return np.tile(np.asarray([[-1.0, 1.0]], dtype=np.float32), (3, 1)) + def get_joint_range(self, *, names=None) -> np.ndarray: + count = len(names) if names is not None else 3 + return np.tile(np.asarray([[-1.0, 1.0]], dtype=np.float32), (count, 1)) def get_joint_state_qpos_indices(self, names) -> np.ndarray: return np.asarray([self.joint_index[name] for name in names], dtype=np.int32) diff --git a/tests/envs/mdp/test_observations.py b/tests/envs/mdp/test_observations.py index 460cf76ff..ea40619ca 100644 --- a/tests/envs/mdp/test_observations.py +++ b/tests/envs/mdp/test_observations.py @@ -130,8 +130,9 @@ def get_dof_vel(self) -> np.ndarray: def get_default_dof_pos(self) -> np.ndarray: return np.asarray([0.1, 0.2, 0.3], dtype=np.float32) - def get_joint_range(self) -> np.ndarray: - return np.tile(np.asarray([[-1.0, 1.0]], dtype=np.float32), (3, 1)) + def get_joint_range(self, *, names=None) -> np.ndarray: + count = len(names) if names is not None else 3 + return np.tile(np.asarray([[-1.0, 1.0]], dtype=np.float32), (count, 1)) def get_body_pos_w(self, ids: np.ndarray) -> np.ndarray: return self.body_pos[:, ids] diff --git a/tests/envs/test_manager_based_rl_env.py b/tests/envs/test_manager_based_rl_env.py index 7f567d711..44d7428de 100644 --- a/tests/envs/test_manager_based_rl_env.py +++ b/tests/envs/test_manager_based_rl_env.py @@ -363,8 +363,9 @@ def get_state_views(self, fields, device=None) -> dict[str, torch.Tensor]: views["qvel"] = self.qvel.detach().clone().to(target) return views - def get_joint_range(self) -> np.ndarray: - return np.array([[-1.0, 1.0]], dtype=np.float32) + def get_joint_range(self, *, names=None) -> np.ndarray: + count = len(names) if names is not None else 1 + return np.tile(np.asarray([[-1.0, 1.0]], dtype=np.float32), (count, 1)) def set_state_tensor(self, env_indices, qpos, qvel, randomization=None) -> None: assert self.materialize_calls == 1 diff --git a/tests/managers/test_device_contracts.py b/tests/managers/test_device_contracts.py index cee3e7021..35a531238 100644 --- a/tests/managers/test_device_contracts.py +++ b/tests/managers/test_device_contracts.py @@ -347,8 +347,9 @@ def get_dof_vel(self) -> np.ndarray: def get_default_dof_pos(self) -> np.ndarray: return np.asarray([0.1, 0.2, 0.3], dtype=np.float32) - def get_joint_range(self) -> np.ndarray: - return np.tile(np.asarray([[-1.0, 1.0]], dtype=np.float32), (3, 1)) + def get_joint_range(self, *, names=None) -> np.ndarray: + count = len(names) if names is not None else 3 + return np.tile(np.asarray([[-1.0, 1.0]], dtype=np.float32), (count, 1)) def get_root_state_layout(self, root_body_name: str) -> RootStateLayout: if root_body_name != "base": diff --git a/tests/scripts/test_ci_scope.py b/tests/scripts/test_ci_scope.py index 90c5dce2f..9b230a437 100644 --- a/tests/scripts/test_ci_scope.py +++ b/tests/scripts/test_ci_scope.py @@ -20,3 +20,14 @@ def test_ci_installs_only_scoped_backend_extras() -> None: if extra.lower() in SHELVED_BACKENDS: offenders.append(f"{workflow.name}:--extra={extra}") assert offenders == [], f"CI installs shelved backend extras: {offenders}" + + +def test_pull_request_workflows_check_out_immutable_head_commits() -> None: + offenders: list[str] = [] + for workflow in sorted(WORKFLOWS.glob("*.yml")): + text = workflow.read_text(encoding="utf-8") + if "pull_request:" not in text: + continue + if "refs/pull/" in text or "github.event.pull_request.head.sha" not in text: + offenders.append(workflow.name) + assert offenders == [], f"PR workflows use ephemeral merge refs: {offenders}"