Skip to content

[backport cloud/1.54] fix: keep released subgraph definitions consistent across convert-to-subgraph - #18522

Merged
comfy-pr-bot merged 1 commit into
cloud/1.54from
backport-18040-to-cloud-1.54
Sep 23, 2026
Merged

comfy-pr-bot merged 1 commit into
cloud/1.54from
backport-18040-to-cloud-1.54

Conversation

@comfy-pr-bot

Copy link
Copy Markdown
Member

Backport of #18040 to cloud/1.54

Automatically created by backport workflow.

@comfy-pr-bot comfy-pr-bot added the backport Backporting a PR onto a release candidate label Sep 23, 2026
@comfy-pr-bot
comfy-pr-bot enabled auto-merge (squash) September 23, 2026 03:46
@vercel

vercel Bot commented Sep 23, 2026 •

Copy link
Copy Markdown

The latest updates on your projects. Learn more about Vercel for GitHub.

1 Skipped Deployment
Project Deployment Actions Updated
billing-web Ignored Ignored Sep 23, 2026 4:00am UTC

Request Review

@github-actions

github-actions Bot commented Sep 23, 2026 •

Copy link
Copy Markdown
Contributor

🎭 Playwright: ✅ 1938 passed, 0 failed · 1 flaky

📊 Browser Reports
  • chromium: View Report (✅ 1917 / ❌ 0 / ⚠️ 1 / ⏭️ 13)
  • chromium-2x: View Report (✅ 2 / ❌ 0 / ⚠️ 0 / ⏭️ 0)
  • chromium-0.5x: View Report (✅ 1 / ❌ 0 / ⚠️ 0 / ⏭️ 0)
  • mobile-chrome: View Report (✅ 18 / ❌ 0 / ⚠️ 0 / ⏭️ 0)
  • New-test walkthrough (chromium, recorded video): View Report

🎨 Storybook: ✅ Built — View Storybook

Details

⏰ Completed at: 09/23/2026, 04:08:59 AM UTC

Links

…subgraph (#18040)

<!-- ccr-slack-attribution -->
_Requested via [Slack
thread](https://comfy-organization.slack.com/archives/C09KFK2ABB8/p1788424987450979?thread_ts=1788424987.450979&cid=C09KFK2ABB8)_

## ELI5

Converting a selection to a subgraph is like packing a box into a bigger
box. If the small box was the only one of its kind, the code threw away
its packing list the moment it left the shelf, then put the box in the
bigger box anyway. Every later save found a box with no list and raised
an alarm. Now the packing list survives the move, because the box is
being re-shelved, not discarded.

## Motivation

`graph_serialization_state_mismatch` is currently the most frequent
error type from `serialiseStoredNodes` in production. Datadog RUM shows
nearly all of those events as "live node N has no stored state" with a
subgraph `graphId`, in sessions that used Convert Selection to Subgraph,
Unpack, or New Subgraph. The report fires on every save until reload,
and the serialized definition for that subgraph falls back to live-node
serialization, so the node store and the saved workflow disagree for the
rest of the session. Fixing the trigger in the conversion path removes
the dominant source of the alert.

## Provenance

- **Authored by:** interactive session
- **Verified:** `pnpm vitest run
src/lib/litegraph/src/subgraph/SubgraphConversion.nestedDefinitions.test.ts`:
1 failed on the test-only commit ("live node 1 has no stored state"), 1
passed with the fix; `pnpm vitest run src/lib/litegraph/src/subgraph/`:
19 files, 356 passed; `pnpm vitest run src/lib/litegraph/src/LGraph`
plus every test file referencing `releaseSubgraphs`,
`convertToSubgraph`, `unpackSubgraph`, `findReleasableSubgraphs` or
`detachAllNodesFromStores`: 46 files, 648 passed, 7 expected fail, 5
skipped; `pnpm typecheck` (via the pre-commit hook on both commits):
clean; `eslint` and `oxlint --type-aware` on the two changed files: 0
errors; `oxfmt --check`: clean; `pnpm knip`: clean
- **Deviations:** unknown

## Reviewer context

- **Type:** bug fix, litegraph subgraph lifecycle
- **Slots into:** no project or epic; ad-hoc production error fix
- **Not in scope:** the general hazard that a registered subgraph node
type keeps a closure over a released `Subgraph` instance and
`LGraph.add()` re-inserts it into `rootGraph.subgraphs` without node
state. This PR removes the trigger seen in production; hardening
`releaseSubgraphs` / `add()` against resurrection is a follow-up.

## Summary

- Convert-to-subgraph no longer releases the subgraph definitions
referenced by the nodes it is about to re-create inside the new
subgraph.
- Adds a regression test that fails on `main` with the production
mismatch report and passes with the fix.

**Before:** convert nodes A and B into subgraph S1 (host node H1). Then
select H1 plus another node and convert that into S2. Serializing the
root graph reports `Graph serialization state mismatch` with `mismatch:
"live node A has no stored state"` and `graphId: S1`, on every save
until reload.

**After:** the same sequence serializes cleanly. S1 stays a valid
definition, nested inside S2, with all of its nodes stored.

**Mechanism:**

1. `_convertToSubgraphImpl` removes the selected nodes with
`this.remove(node)` before configuring the new subgraph from the
serialized selection.
2. `removeNode` sees that H1 is a `SubgraphNode` and calls
`releaseSubgraphs(findReleasableSubgraphs(...))`. H1 was S1's only
instance, so S1 is released: its node store records are deleted and it
is dropped from `rootGraph.subgraphs`, but the `Subgraph` instance keeps
its `_nodes`.
3. The registered node type for S1 is a class whose constructor closes
over that same `Subgraph` instance, so `LiteGraph.createNode(S1.id)`
inside S2's configure returns a host pointing at the released instance.
4. `LGraph.add()` walks nested subgraph nodes and re-inserts S1 into
`rootGraph.subgraphs`. S1 now has live nodes and zero stored state, and
`serialiseStoredNodes` reports the mismatch on every serialize.

**Fix choice:** option (a), skip the release for nodes that the
conversion is moving. Every node removed in that loop is re-created
inside the new subgraph from the same serialized data, so the set of
definitions in use is identical before and after the operation and
nothing should be released. This is a three-line change behind a new
`GraphRemoveOptions.preserveSubgraphDefinitions` flag, used only by
`_convertToSubgraphImpl`. The alternative (tearing down released
instances fully and teaching `add()` to re-attach or refuse them)
touches the release and add paths for every caller and is a broader
behavioral change; unregistering the node type is not viable because the
nested host inside the new subgraph is created through that type.

## Changes

- **What**: `GraphRemoveOptions.preserveSubgraphDefinitions`;
`removeNode` skips `releaseSubgraphs` when it is set;
`_convertToSubgraphImpl` passes it when removing the moved nodes. New
test
`src/lib/litegraph/src/subgraph/SubgraphConversion.nestedDefinitions.test.ts`.
If `createSubgraph` throws after the originals are removed,
`_convertToSubgraphImpl` releases the removed nodes' definitions that no
live node still references (`findOrphanedSubgraphs` in
`subgraphUtils.ts`) and rethrows, so a failed conversion leaves no
orphaned definitions registered.

## Red / green

The first commit adds only the failing test (CI should be red on it),
the second commit adds the fix (CI should be green). Unpack was checked
and is not affected by this trigger: it materializes the inner nodes
into the parent before removing the host, so the definitions are still
live when the host is released.

## Review Focus

- `findReleasableSubgraphs` is still called for every other removal path
(delete, undo, unpack), so nothing else changes behavior.
- If a moved node's type cannot be re-created inside the new subgraph,
its definition stays registered but unused; `findUsedSubgraphIds`
already excludes unused definitions from serialization.

## Test plan

- [ ] CI red on the test-only commit, green on the fix commit
- [ ] Convert nodes to a subgraph, then convert a selection containing
that subgraph's host into another subgraph, save: no
`graph_serialization_state_mismatch` in the console or RUM
- [ ] Unpack the outer subgraph afterwards and save again: still clean

🤖 Generated with [Claude Code](https://claude.com/claude-code)

https://claude.ai/code/session_01TsajsCwfDusee9F9rPWNb7

---
_Generated by [Claude
Code](https://claude.ai/code/session_01TsajsCwfDusee9F9rPWNb7)_

---------

Co-authored-by: Christian Byrne <cbyrne@comfy.org>
Co-authored-by: glary-bot <bot@comfy.org>
@claude
claude Bot force-pushed the backport-18040-to-cloud-1.54 branch from d48661b to 9375fee Compare September 23, 2026 04:00
@github-actions github-actions Bot added the risk:xhigh PR risk grade (advisory shadow check; grader-owned) label Sep 23, 2026
@codecov

codecov Bot commented Sep 23, 2026

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.

@@              Coverage Diff              @@
##             cloud/1.54   #18522   +/-   ##
=============================================
  Coverage              ?   78.60%           
=============================================
  Files                 ?     2009           
  Lines                 ?   111117           
  Branches              ?    33554           
=============================================
  Hits                  ?    87347           
  Misses                ?    23591           
  Partials              ?      179           
Flag Coverage Δ
unit 78.60% <100.00%> (?)

Flags with carried forward coverage won't be shown. Click here to find out more.

Files with missing lines Coverage Δ
src/lib/litegraph/src/subgraph/subgraphUtils.ts 89.34% <100.00%> (ø)
🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.
  • 📦 JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.

@comfy-pr-bot
comfy-pr-bot merged commit 9e7e8fe into cloud/1.54 Sep 23, 2026
91 checks passed
@comfy-pr-bot
comfy-pr-bot deleted the backport-18040-to-cloud-1.54 branch September 23, 2026 04:33
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

backport Backporting a PR onto a release candidate risk:xhigh PR risk grade (advisory shadow check; grader-owned)

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants