Skip to content

Fix sensor ancestor suffix trimming - #6068

Merged
AntoineRichard merged 1 commit into
isaac-sim:developfrom
AntoineRichard:antoiner/fix-sensor-base-ancestor-suffix
Jun 9, 2026
Merged

AntoineRichard merged 1 commit into
isaac-sim:developfrom
AntoineRichard:antoiner/fix-sensor-base-ancestor-suffix

Conversation

@AntoineRichard

Copy link
Copy Markdown
Collaborator

Description

Fixes rigid-body ancestor expression resolution for sensors mounted under a
non-rigid child when the terminal child suffix also appears earlier in the path.
The old implementation used an unbounded str.replace(), which could remove
more than the sensor-relative suffix.

Simple example:

target_expr   = /World/envs/env_.*/Robot/link/link
relative_path = link
old result    = /World/envs/env_.*/Robot
new result    = /World/envs/env_.*/Robot/link

The fix verifies that the relative path is a terminal suffix and slices only that
end segment.

No issue filed.

Validation:

  • ./isaaclab.sh -p -m pytest source/isaaclab/test/sensors/test_sensor_base.py::test_rigid_body_ancestor_expr_trims_only_terminal_suffix -q
  • PATH=/tmp/git-lfs-v3.7.1:$PATH ./isaaclab.sh -f

Type of change

  • Bug fix (non-breaking change which fixes an issue)

Screenshots

Not applicable.

Checklist

  • I have read and understood the contribution guidelines
  • I have run the pre-commit checks with ./isaaclab.sh --format
  • I have made corresponding changes to the documentation, or documentation is not required for this change
  • My changes generate no new warnings
  • I have added tests that prove my fix is effective or that my feature works
  • I have added a changelog fragment under source/<pkg>/changelog.d/ for every touched package (do not edit CHANGELOG.rst or bump extension.toml — CI handles that)
  • I have added my name to the CONTRIBUTORS.md or my name already exists there

Trim only the terminal sensor-relative path when building rigid-body ancestor expressions so repeated child suffixes earlier in the prim path are preserved.

Add a regression covering a repeated link/link sensor path.
@github-actions github-actions Bot added bug Something isn't working isaac-lab Related to Isaac Lab team labels Jun 9, 2026
@AntoineRichard
AntoineRichard requested a review from ooctipus June 9, 2026 09:29

@isaaclab-review-bot isaaclab-review-bot Bot 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.

🤖 Isaac Lab Review Bot — PR #6068

Summary

This PR fixes a bug in SensorBase._resolve_rigid_body_ancestor_expr() where str.replace() was used to strip the sensor-relative suffix from a target expression. Since str.replace() replaces the first occurrence (not the last), paths like /World/envs/env_.*/Robot/link/link would incorrectly strip the earlier /link segment, yielding /World/envs/env_.*/Robot instead of /World/envs/env_.*/Robot/link. The fix uses str.endswith() validation followed by slice-based suffix removal.

Design Assessment

✅ Correct approach. The fix replaces unbounded str.replace() with explicit suffix validation and slice removal — a well-known Python idiom that is both safe and efficient. The endswith() guard converts a silent data corruption into a loud RuntimeError, following Isaac Lab's fail-fast conventions for path resolution.

✅ Error message quality. The RuntimeError includes both the target expression and the relative path, providing sufficient context for debugging.

✅ Minimal blast radius. Only the final 3 lines of the method are changed. The ancestor traversal, early-return path (sensor == body), and pose resolution are untouched.

Findings

🟢 No critical or warning-level issues found.

🔵 Suggestion — consider removesuffix (Python 3.9+):
Since Isaac Lab requires Python ≥3.10, the implementation could use target_expr.removesuffix(suffix) after the endswith() check. This is purely stylistic — the current slice is equally correct and arguably more explicit given the guard clause.

🔵 Suggestion — test the negative path:
The test exercises the happy path (suffix is correctly terminal), but does not assert that the new RuntimeError is raised for the previously-buggy scenario (e.g., a malformed target_expr that doesn't end with the expected suffix). Adding a pytest.raises(RuntimeError) case would lock in the guard behavior.

Test Coverage

✅ Regression test included. test_rigid_body_ancestor_expr_trims_only_terminal_suffix creates the exact pathological topology (/Robot/link/link) and asserts the ancestor expression preserves the intermediate /link segment. This directly exercises the fixed code path.

⚠️ Minor gap: No negative test for the new RuntimeError guard. Low risk since the guard is straightforward, but worth considering for completeness.

CI Status

Check Status
pre-commit ✅ pass
Build Wheel ✅ pass
Broken Links ✅ pass
Installation Tests ⏳ pending
Changelog check ⏳ pending
Docs build ⏳ pending

Core quality gates (pre-commit, wheel build) pass. Infrastructure checks still pending but unlikely to be affected by this change.

Verdict

✅ LGTM — Approve-worthy. Clean, minimal, well-tested bug fix with proper error handling. The suffix-slice approach is correct and robust. No blocking issues found.

@greptile-apps

greptile-apps Bot commented Jun 9, 2026

Copy link
Copy Markdown
Contributor

Greptile Summary

This PR fixes a path-trimming bug in _resolve_rigid_body_ancestor_expr where str.replace() could remove a non-terminal occurrence of the sensor-relative suffix (e.g. /World/…/link/link incorrectly trimmed to /World/…/Robot instead of /World/…/Robot/link). The fix adds an endswith() guard and slices from the end of the string, and converts the previous silent-corruption path into an explicit RuntimeError.

  • sensor_base.py: Replaces the unbounded str.replace() with a terminal-suffix check + slice; adds a RuntimeError when the expression does not end with the expected suffix.
  • test_sensor_base.py: Adds a regression test covering the repeated path-segment case.
  • changelog.d/: New changelog fragment describing the fix.

Confidence Score: 4/5

Safe to merge; the fix is narrowly scoped and the new behavior is strictly better than silent string corruption.

The core fix is correct and well-tested for the reported case. One minor gap: when target_expr equals suffix exactly, the slice returns an empty string that is passed downstream as a physics-view expression with no validation, which could produce a confusing failure. This is an extremely unlikely path in practice but would benefit from an explicit guard.

The slice in sensor_base.py line 481 could produce an empty rigid_parent_expr; the new RuntimeError branch in the same function has no test coverage.

Important Files Changed

Filename Overview
source/isaaclab/isaaclab/sensors/sensor_base.py Replaces unbounded str.replace() with an endswith() guard + terminal slice to correctly trim only the sensor-relative suffix from the ancestor expression.
source/isaaclab/test/sensors/test_sensor_base.py Adds a regression test for the repeated-segment bug; tests the happy path but does not cover the new RuntimeError branch or the edge case where rigid_parent_expr becomes an empty string.
source/isaaclab/changelog.d/fix-sensor-base-ancestor-suffix.rst New changelog fragment accurately describing the sensor ancestor suffix bug fix.

Flowchart

%%{init: {'theme': 'neutral'}}%%
flowchart TD
    A["_resolve_rigid_body_ancestor_expr()"] --> B["resolve first matching prim\ntarget_expr"]
    B --> C["get_first_matching_ancestor_prim\nHasAPI RigidBodyAPI"]
    C --> D{ancestor found?}
    D -- No --> E["raise RuntimeError"]
    D -- Yes --> F{ancestor == prim?}
    F -- Yes --> G["return target_expr, None, None"]
    F -- No --> H["MakeRelativePath\nsuffix = '/' + relative_path"]
    H --> I{target_expr ends with suffix?}
    I -- No --> J["raise RuntimeError\nnew: was silent corruption"]
    I -- Yes --> K["rigid_parent_expr = target_expr at negative len suffix"]
    K --> L["resolve_prim_pose"]
    L --> M["return rigid_parent_expr, pos, quat"]
Loading

Reviews (1): Last reviewed commit: "Fix sensor ancestor suffix trimming" | Re-trigger Greptile

f"Failed to build rigid body ancestor expression: target expression {target_expr!r} does not end "
f"with relative path {relative_path!r}."
)
rigid_parent_expr = target_expr[: -len(suffix)]

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.

P2 Empty rigid_parent_expr when sensor is at path root

If target_expr equals suffix exactly (e.g. target_expr = "/link" with relative_path = "link"), target_expr[:-len(suffix)] evaluates to "". The empty string is then returned as rigid_parent_expr and used downstream as a physics-view expression, which would likely produce a silent failure or an unhelpful error at the call site. An explicit guard here would make the failure mode clearer, even though this degenerate case (sensor mounted at the USD stage root) is extremely unlikely in practice.

@mmichelis mmichelis 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.

Nice fix! I added a quick point that could be nice to discuss.

@AntoineRichard
AntoineRichard merged commit 5124aa1 into isaac-sim:develop Jun 9, 2026
37 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

bug Something isn't working isaac-lab Related to Isaac Lab team

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants