Feature Request: Support Bounded Feedback Loops (per-step parameter control via step_index)
- Dominant language
- Python
- Stars
- 133k
- Forks
- 15.7k
- Avg merge
- 1d 7h
- Merged PRs (30d)
- 158
Description
## Problem
Currently, ComfyUI's DAG execution engine treats any dependency cycle as an error. This prevents workflows where a sampler's internal iteration variable (`step_index`) feeds back upstream through math expressions to dynamically control its own per-step parameters (CFG, s_noise, eta, etc.).
**Desired workflow:**
```
SamplerCustomAdvanced → step_index → ComfyMathExpression("3 + a*2") → cfg → CFGGuider → SamplerCustomAdvanced
```
The sampler loop is bounded (N steps, then terminates), so this isn't an infinite cycle — the DAG should recognize and allow these bounded feedback loops.
## Proposed Solution
A general, extensible mechanism that:
1. **Static validation** — Allow dependency cycles where at least one node declares `BOUNDED_FEEDBACK` (e.g., `{"step_index"}`)
2. **Graph building** — Skip strong/blocking links for declared feedback sockets, record them as feedback edges, supply initial placeholder values
3. **Chain resolution** — Walk multi-hop `ComfyMathExpression` chains to find terminal `CFGGuider` / `SamplerXXX` targets, composing per-step callables
4. **Per-step re-read** — Patch all stochastic sampler functions to re-read mutable parameters at each loop iteration
5. **Safety** — Pop `_dynamic_sampler_options` before model calls; only inject when feedback is active
## Benefits
- **One-line opt-in**: `BOUNDED_FEEDBACK = {"step_index"}` on any node
- **Works with all sampler types** (23 stochastic functions patched)
- **Multi-hop chains**: step_index → MathExpr_A → MathExpr_B → CFGGuider
- **Multi-input expressions**: e.g., `a/b` where `b` comes from a PrimitiveFloat
- **Safe by design**: dynamic options never leak to model calls
## Implementation
PR: #14550
5 files changed:
- `execution.py` — validation bypass, chain builder, bootstrap/injection logic
- `comfy_execution/graph.py` — feedback edge handling in topological sort
- `comfy/k_diffusion/sampling.py` — per-step re-read helpers + 23 sampler patches
- `comfy_extras/nodes_custom_sampler.py` — `BOUNDED_FEEDBACK` + step_index output + callback wrapper
- `comfy/samplers.py` — conditional dynamic options injection in KSAMPLER
Contributor guide
Research direction
Start with execution.py and comfy_execution/graph.py, then read the related changes in PR #14550. Review how feedback edges, chain resolution, and per-step sampler updates are handled across the five listed files. Done means bounded step_index feedback works safely across the described sampler and math-expression paths without leaking dynamic options into model calls.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- python
- Domain
- backend, machine-learning
- Issue type
- Feature
- Difficulty
- 5/5
- Estimated time
- Over a week
- Activity status
- Stale
- Clarity
- Clearly specified
- Newbie friendliness
- 20/100