fix(system): isolate sliced system metadata - #1014
Conversation
Deep-copy both frame-selected arrays and frame-independent metadata when creating a subsystem so slice views and mutable values cannot alias the source. Add regressions for coordinates, cells, names, and atom types, which existing comparison tests did not mutate. Coding-Agent: Codex Codex-Version: codex-cli 0.144.4 Model: gpt-5.6-sol Reasoning-Effort: xhigh
Merging this PR will not alter performance
|
|
Warning Review limit reached
Next review available in: 54 minutes Enable usage-based reviews in Billing to review now. Otherwise, wait until the next included review is available. How can I continue?After more reviews become available, a review can be triggered using the To avoid repeated limits, reduce automatic review volume by pausing incremental auto-reviews earlier, using label-based review opt-in, excluding WIP or generated PR titles, or requesting reviews manually when the PR is ready. If your team needs uninterrupted high-volume reviews, an organization admin can enable usage-based reviews. How do review limits work?CodeRabbit enforces per-developer PR review limits for each organization. Most developers receive the normal plan review availability. For paid Pro and Pro+ PR reviews, CodeRabbit uses adaptive limits for sustained high-volume activity. When a developer's recent PR review activity reaches the 95th percentile or higher among CodeRabbit users, additional reviews become available more gradually as earlier reviews age out of the rolling window. Please refer docs for additional details. 📝 WalkthroughWalkthrough
ChangesSystem ownership isolation
Estimated code review effort: 3 (Moderate) | ~20 minutes Possibly related PRs
🚥 Pre-merge checks | ✅ 5✅ Passed checks (5 passed)
Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out. Comment |
There was a problem hiding this comment.
Actionable comments posted: 1
🤖 Prompt for all review comments with AI agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.
Inline comments:
In `@tests/test_system_append.py`:
- Around line 45-54: Update System.append() so its first-append path deep-copies
system.data instead of using a shallow dictionary copy, ensuring nested lists
and NumPy arrays do not alias the source. Preserve the existing append behavior
for non-empty systems.
🪄 Autofix (Beta)
Fix all unresolved CodeRabbit comments on this PR:
- Push a commit to this branch (recommended)
- Create a new PR with the fixes
ℹ️ Review info
⚙️ Run configuration
Configuration used: Organization UI
Review profile: CHILL
Plan: Pro
Run ID: c713429b-f5d4-4b18-b072-81d23c410825
📒 Files selected for processing (2)
dpdata/system.pytests/test_system_append.py
wanghan-iapcm
left a comment
There was a problem hiding this comment.
Requesting changes: this fixes the sub_system half of #985 but leaves the first-append()-into-empty path aliased, so the PR's own test_first_append_does_not_alias_source fails and CI (build 3.10 / 3.13) is red. See the inline note for the one-line fix.
Prevent the first append into an empty System from retaining aliases to the source system's nested metadata and arrays. Coding-Agent: Codex Codex-Version: codex-cli 0.144.6 Model: gpt-5.6-sol Reasoning-Effort: xhigh
Resolve the ownership-fix overlap with the implementation already present on master while retaining the subsystem metadata regression coverage. Coding-Agent: Codex Codex-Version: codex-cli 0.144.6 Model: gpt-5.6-sol Reasoning-Effort: xhigh
Codecov Report✅ All modified and coverable lines are covered by tests. Additional details and impacted files@@ Coverage Diff @@
## master #1014 +/- ##
=======================================
Coverage 86.94% 86.94%
=======================================
Files 90 90
Lines 9178 9178
=======================================
Hits 7980 7980
Misses 1198 1198 ☔ View full report in Codecov by Harness. 🚀 New features to boost your workflow:
|
wanghan-iapcm
left a comment
There was a problem hiding this comment.
The #985 fix itself verifies clean -- one note inline on a performance tradeoff worth reconsidering before merge.
| # A slice produces a NumPy view, while advanced indexing | ||
| # produces a copy. Deep-copy the result so ownership is | ||
| # consistent for every supported frame selector. | ||
| tmp.data[tt.name] = deepcopy(self.data[tt.name][tuple(new_shape)]) |
There was a problem hiding this comment.
Fix verified -- test_sub_system_does_not_alias_metadata fails at the merge base (AssertionError: ['X'] != ['H']) and passes here, and I confirmed atom_names, atom_types, coords and cells all leaked pre-fix. Thanks for closing this properly.
One tradeoff worth reconsidering. The comment above is right that advanced indexing already produces a copy -- which means for those selectors this deepcopy is a second full memcpy of the frame data. Measured on a 20k-frame x 100-atom system, merge base vs this head:
| operation | base | head | ratio |
|---|---|---|---|
sub_system(permutation) |
0.021 s | 0.040 s | 1.9x |
sub_system(list) |
0.0021 s | 0.0099 s | 4.7x |
sub_system(slice) |
~0.00003 s | 0.018 s | ~600x |
shuffle() |
0.021 s | 0.039 s | 1.87x |
System.shuffle(), to("list") (which slices per frame), and mixed-format train/test splitting all sit on this path. The CodSpeed suite only covers test_import / test_cli, so it will not flag this.
Copying only when the result actually aliases the source keeps the fix and drops the redundancy:
res = self.data[tt.name][tuple(new_shape)]
if np.shares_memory(res, self.data[tt.name]):
res = res.copy()
tmp.data[tt.name] = resNote that swapping deepcopy for .copy() on its own buys nothing -- I measured 632 us vs 638 us on a (1000,50,3) array. The cost is the redundant copy, not deepcopy. The frame-independent branch below should keep its unconditional deepcopy; that one is the actual #985 fix.
Minor, for the record: test_first_append_does_not_alias_source passes at the merge base, because the first-append deepcopy landed on master via #1015 (2cd82ee) before this branch's merge base -- so it arrived here through the master merge rather than from 527e2d4. Harmless redundant coverage, but test_sub_system_does_not_alias_metadata is the test actually guarding this change.
Fixes #985.
Deep-copy frame-selected arrays and frame-independent metadata so subsystem slices cannot mutate their source through NumPy views or shared lists.
Tests:
cd tests && python -m unittest test_system_appendWhy existing tests missed it: The existing tests only compared sliced values; they never mutated a slice, so aliasing remained invisible.
Coding agent: Codex
Codex version: codex-cli 0.144.4
Model: gpt-5.6-sol
Reasoning effort: xhigh
Summary by CodeRabbit
Bug Fixes
Tests