Skip to content

fix: make NodeReplaceManager.register() idempotent#13596

Open
Kosinkadink wants to merge 2 commits intoComfy-Org:masterfrom
Kosinkadink:fix-node-replace-manager-idempotent
Open

fix: make NodeReplaceManager.register() idempotent#13596
Kosinkadink wants to merge 2 commits intoComfy-Org:masterfrom
Kosinkadink:fix-node-replace-manager-idempotent

Conversation

@Kosinkadink
Copy link
Copy Markdown
Member

Summary

  • Make NodeReplaceManager.register() idempotent: re-registering the same (old_node_id, new_node_id) pair is now a no-op (with a debug log) instead of appending a duplicate entry.
  • Multi-alternative behavior is preserved: different
    ew_node_ids for the same old_node_id are still all kept (matching the existing list-of-alternatives design used by �pply_replacements()).
  • Adds unit tests in tests-unit/app_test/node_replace_manager_test.py.

Why

Previously every call to
egister() did setdefault(old, []).append(new) with no dedupe. If the same custom node is reloaded in the same process — e.g. via ComfyUI-Manager's hot-reload — every prior �pi.node_replacement.register(...) call runs again and accumulates duplicate entries. Those duplicates leak through GET /node_replacements and surface in any Quick Fix UI built on top of it.

This was also flagged as a latent issue on the upcoming JSON-based registration in #12999. Fixing it at the manager level addresses both the Python and JSON registration paths in one place.

First-write-wins

�pply_replacements() only uses
eplacements[0], so registration order matters. The dedupe preserves the first registration; subsequent duplicates are ignored. This means a built-in comfy_extras/nodes_replacements.py registration is not silently overridden by a later custom-node registration with the same (old, new) pair.

Test plan

  • python -m pytest tests-unit/app_test/node_replace_manager_test.py -v — 5 tests, all pass.
  • Existing replacements (e.g. comfy_extras/nodes_replacements.py) still register and apply correctly.

When the same (old_node_id, new_node_id) pair is registered more than
once in the same process, the duplicate is now silently ignored (with a
debug log). Previously every call appended a new entry, so reloading a
custom node — e.g. via ComfyUI-Manager hot-reload — would accumulate
stale duplicate replacements that surfaced through GET /node_replacements.

Multiple distinct alternatives for the same old_node_id (different
new_node_ids) are still preserved, matching the existing 'list of
alternatives' design used by apply_replacements().

Adds unit tests in tests-unit/app_test/node_replace_manager_test.py
covering normal registration, multi-alternative support, dedupe, and
first-write-wins ordering.

Co-authored-by: Amp <amp@ampcode.com>
Amp-Thread-ID: https://ampcode.com/threads/T-019dd37c-4751-72ef-9927-3182b5825db0
@coderabbitai
Copy link
Copy Markdown

coderabbitai Bot commented Apr 28, 2026

No actionable comments were generated in the recent review. 🎉

ℹ️ Recent review info
⚙️ Run configuration

Configuration used: Path: .coderabbit.yaml

Review profile: CHILL

Plan: Pro

Run ID: 2cbedcdf-99fa-4b87-975d-a35a62df1f43

📥 Commits

Reviewing files that changed from the base of the PR and between 2ff1d3d and 97e84d3.

📒 Files selected for processing (1)
  • tests-unit/app_test/node_replace_manager_test.py

📝 Walkthrough

Walkthrough

The PR changes app/node_replace_manager.py so NodeReplaceManager.register scans existing entries and skips appending when an identical old_node_idnew_node_id mapping already exists, returning early and emitting a logging.debug when duplicates are ignored. It adds tests-unit/app_test/node_replace_manager_test.py, which stubs the nodes module and re-imports app.node_replace_manager per test, and verifies registration semantics: adding replacements, allowing multiple alternatives for the same old_node_id, idempotency for duplicate pairs, preserving the first registered instance on duplicate-new_id collisions, and independent counts across different old_node_id values.

🚥 Pre-merge checks | ✅ 4 | ❌ 1

❌ Failed checks (1 warning)

Check name Status Explanation Resolution
Docstring Coverage ⚠️ Warning Docstring coverage is 60.00% which is insufficient. The required threshold is 80.00%. Write docstrings for the functions missing them to satisfy the coverage threshold.
✅ Passed checks (4 passed)
Check name Status Explanation
Title check ✅ Passed The title clearly and concisely summarizes the main change: making NodeReplaceManager.register() idempotent to prevent duplicate registrations.
Description check ✅ Passed The description provides a thorough explanation of the change, its rationale (hot-reload duplication issue), the dedupe strategy (first-write-wins), and test results, directly relating to the changeset.
Linked Issues check ✅ Passed Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check ✅ Passed Check skipped because no linked issues were found for this pull request.

✏️ Tip: You can configure your own custom pre-merge checks in the settings.


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 and usage tips.

Copy link
Copy Markdown

@coderabbitai coderabbitai Bot left a comment

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 the current code and only fix it if needed.

Inline comments:
In `@tests-unit/app_test/node_replace_manager_test.py`:
- Around line 5-13: The module-level insertion of a fake 'nodes' into
sys.modules leaks across the test session; instead create a fixture that, before
importing app.node_replace_manager/NodeReplaceManager, injects a temporary
ModuleType with NODE_CLASS_MAPPINGS into sys.modules, yields to run the test,
then removes that key from sys.modules in teardown and calls importlib.reload on
the app.node_replace_manager module to clear any cached imports; update tests to
use that fixture so the fake 'nodes' is scoped per test (or per module) and the
real import state is restored afterwards.
🪄 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: Path: .coderabbit.yaml

Review profile: CHILL

Plan: Pro

Run ID: f37f24b7-25cc-4a3c-a667-3075d17f93dc

📥 Commits

Reviewing files that changed from the base of the PR and between 24de8dc and 2ff1d3d.

📒 Files selected for processing (2)
  • app/node_replace_manager.py
  • tests-unit/app_test/node_replace_manager_test.py

Comment thread tests-unit/app_test/node_replace_manager_test.py Outdated
Per coderabbit feedback: replace the module-level sys.modules['nodes'] = stub
with a per-test fixture using monkeypatch.setitem so the fake module is torn
down after each test, and reload app.node_replace_manager so it picks up the
stub instead of any cached real import.

Amp-Thread-ID: https://ampcode.com/threads/T-019dd37c-4751-72ef-9927-3182b5825db0
Co-authored-by: Amp <amp@ampcode.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant