Skip to content

fix(system): isolate sliced system metadata - #1014

Open
njzjz-bot wants to merge 3 commits into
deepmodeling:masterfrom
njzjz-bot:fix/issue-985
Open

fix(system): isolate sliced system metadata#1014
njzjz-bot wants to merge 3 commits into
deepmodeling:masterfrom
njzjz-bot:fix/issue-985

Conversation

@njzjz-bot

@njzjz-bot njzjz-bot commented Jul 16, 2026

Copy link
Copy Markdown
Contributor

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_append

Why 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

    • Improved subsystem creation to prevent unintended data sharing when slicing frame-dependent data.
    • Updated append behavior so adopting a whole incoming system no longer causes nested array/list aliasing between systems.
    • Subsystem and appended changes no longer unexpectedly modify the original source data.
  • Tests

    • Added regression tests to verify data ownership for subsystem slicing and first append behavior.

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
@dosubot dosubot Bot added size:S This PR changes 10-29 lines, ignoring generated files. dpdata labels Jul 16, 2026
@codspeed-hq

codspeed-hq Bot commented Jul 16, 2026

Copy link
Copy Markdown

Merging this PR will not alter performance

⚠️ Unknown Walltime execution environment detected

Using the Walltime instrument on standard Hosted Runners will lead to inconsistent data.

For the most accurate results, we recommend using CodSpeed Macro Runners: bare-metal machines fine-tuned for performance measurement consistency.

✅ 2 untouched benchmarks


Comparing njzjz-bot:fix/issue-985 (8969202) with master (d2105f6)

Open in CodSpeed

@coderabbitai

coderabbitai Bot commented Jul 16, 2026

Copy link
Copy Markdown

Review Change Stack

Warning

Review limit reached

@njzjz, you've reached your PR review limit, so we couldn't start this review.

Next review available in: 54 minutes

Enable usage-based reviews in Billing to review now. Otherwise, wait until the next included review is available.
You're only billed for reviews past your plan's rate limits ($0.25/file).

How can I continue?

After more reviews become available, a review can be triggered using the @coderabbitai review command as a PR comment. Alternatively, push new commits to this PR.

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.

Review details
⚙️ Run configuration

Configuration used: Organization UI

Review profile: CHILL

Plan: Pro

Run ID: b0fb6c6d-a292-426e-85dd-2de5c2e1ca7d

📥 Commits

Reviewing files that changed from the base of the PR and between 527e2d4 and 8969202.

📒 Files selected for processing (1)
  • dpdata/system.py
📝 Walkthrough

Walkthrough

System.sub_system and the initial System.append path now deep-copy nested data, with tests covering mutation isolation for subsystem slices and first-appended systems.

Changes

System ownership isolation

Layer / File(s) Summary
Deep-copy derived and appended data
dpdata/system.py
System.sub_system deep-copies frame-dependent and frame-independent fields, while the initial System.append path deep-copies incoming system data.
Ownership regression coverage
tests/test_system_append.py
Tests verify that mutations to subsystem or source data do not affect the parent or first-appended target.

Estimated code review effort: 3 (Moderate) | ~20 minutes

Possibly related PRs

🚥 Pre-merge checks | ✅ 5
✅ Passed checks (5 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check ✅ Passed The title is concise and accurately reflects the main change: making sliced system metadata independent.
Linked Issues check ✅ Passed The PR addresses #985 by deep-copying sub_system data and the first append path, with regression tests covering both cases.
Out of Scope Changes check ✅ Passed The changes stay focused on System ownership and regression tests, with no unrelated code paths introduced.
Docstring Coverage ✅ Passed No functions found in the changed files to evaluate docstring coverage. Skipping docstring coverage check.

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.

❤️ Share

Comment @coderabbitai help to get the list of available commands.

@coderabbitai coderabbitai Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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

📥 Commits

Reviewing files that changed from the base of the PR and between 0416b54 and 8a6b8b9.

📒 Files selected for processing (2)
  • dpdata/system.py
  • tests/test_system_append.py

Comment thread tests/test_system_append.py

@wanghan-iapcm wanghan-iapcm 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.

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.

Comment thread tests/test_system_append.py
njzjz-bot added 2 commits July 21, 2026 14:07
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

codecov Bot commented Jul 21, 2026

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 86.94%. Comparing base (d2105f6) to head (8969202).

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.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

@njzjz
njzjz requested a review from wanghan-iapcm July 22, 2026 17:01

@wanghan-iapcm wanghan-iapcm 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.

The #985 fix itself verifies clean -- one note inline on a performance tradeoff worth reconsidering before merge.

Comment thread dpdata/system.py
# 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)])

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.

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] = res

Note 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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

dpdata size:S This PR changes 10-29 lines, ignoring generated files.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[Code scan] System slicing and first append share mutable data with the source

3 participants