|
| 1 | +# AI Development Context — ComfyUI Ultimate Auto Sampler Config Grid Testing Suite |
| 2 | + |
| 3 | +> **Paste this at the start of any new Claude conversation to provide full project context.** |
| 4 | +> **GOLDEN RULE: DO NOT REMOVE ANY CODE. DO NOT REMOVE ANY COMMENTS. ONLY CHANGE WHAT IS NECESSARY.** |
| 5 | +
|
| 6 | +## What This Is |
| 7 | + |
| 8 | +A ComfyUI custom node extension for automated image generation grid testing. Users configure combinations of samplers, schedulers, steps, CFG, models, LoRAs, prompts, and resolutions — the system generates all Cartesian products, saves results, and displays them in a virtualized dashboard for comparison. Supports distributed multi-machine generation, upscaling, and CivitAI metadata export. |
| 9 | + |
| 10 | +## Project Location |
| 11 | + |
| 12 | +``` |
| 13 | +Z:\comfy_v0.12.3\ComfyUI\custom_nodes\ComfyUI-Ultimate-Auto-Sampler-Config-Grid-Testing-Suite\ |
| 14 | +``` |
| 15 | + |
| 16 | +Read `ProjectStructure.md` in this directory for the full reference (file structure, all API routes with line numbers, data shapes, communication patterns, dependency graph, and 28 development gotchas). |
| 17 | + |
| 18 | +## Architecture (3 Layers) |
| 19 | + |
| 20 | +1. **Python Backend** (runs inside ComfyUI server) — nodes, orchestration, generation, API endpoints |
| 21 | +2. **JS Frontend Bridge** (`web/`) — registers nodes in ComfyUI browser tab, served at `/extensions/` |
| 22 | +3. **Dashboard SPA** (`resources/`) — standalone app inlined into iframe by `html_generator.py`, zero external requests |
| 23 | + |
| 24 | +## Critical Files & Their Roles |
| 25 | + |
| 26 | +| File | Lines | Role | |
| 27 | +|------|-------|------| |
| 28 | +| `__init__.py` | 933 | API endpoints (config CRUD, dashboard save/export/delete, scan), node mappings, path security | |
| 29 | +| `sampler_node.py` | 314 | Main node class. Unwraps configs, extracts `_distribution` + `_session_settings`, delegates to orchestrator | |
| 30 | +| `config_builder_node.py` | 1167 | Config builder node. `generate_config()` reads ALL state from `lora_config` widget. API endpoints for model lists, trigger lookups | |
| 31 | +| `generation_orchestrator.py` | 2281 | **Largest file.** Main generation loop, smart skip, upscaling (lines 1096-1204), GPU cooldown (1249-1265), distribution, ETA tracking | |
| 32 | +| `image_generation.py` | 748 | KSampler wrapper, VAE decode, `flush_batch_with_vae()` (saves images + updates manifest), `upscale_image()`, `create_image_metadata()` | |
| 33 | +| `config_utils.py` | 674 | Config expansion (Cartesian products), nested prompt parsing, job preparation | |
| 34 | +| `model_loader.py` | 805 | Checkpoint/LoRA/VAE/GGUF loading | |
| 35 | +| `model_cache.py` | 900 | 3-tier cache: LoRA files → incremental states → patched models. Async preloading | |
| 36 | +| `manifest_utils.py` | 120 | Load/save/merge manifest. `save_manifest()` reloads from disk first to preserve concurrent user edits | |
| 37 | +| `metadata_packer.py` | 561 | PNG metadata embedding for CivitAI. `pack_metadata_into_image()` accepts `workflow_data` kwarg | |
| 38 | +| `web/conf_builder/conf-builder-main.js` | 608 | Node registration. Default state (lines 80-147) defines ALL fields including upscaling/cooldown | |
| 39 | +| `web/conf_builder/conf-builder-utilities.js` | 958 | `convertStateToConfigs()` (line 497) — **MUST stay in sync with Python `generate_config()`** | |
| 40 | +| `web/conf_builder/conf-builder-config-management.js` | 4975 | All config builder UI rendering. Upscaling section ~line 4437 | |
| 41 | +| `resources/logic_utils.js` | 599 | Dashboard helpers: `exportFavorites()`, `deleteNonFavorites()`, `loadSession()`, `scanDirectory()` | |
| 42 | +| `resources/logic_ui.js` | 1780 | Dashboard DOM: cards, modals, `buildComfyNodesWorkflow()`, analytics | |
| 43 | +| `resources/logic_virtual.js` | 925 | Virtual scroller — renders only visible cards from thousands | |
| 44 | + |
| 45 | +## The Two Codepaths That Must Stay In Sync |
| 46 | + |
| 47 | +When adding ANY new config field, you MUST update BOTH: |
| 48 | + |
| 49 | +1. **JS:** `convertStateToConfigs()` in `conf-builder-utilities.js` (line 497) |
| 50 | +2. **Python:** `generate_config()` in `config_builder_node.py` (line 465) |
| 51 | + |
| 52 | +Also add default values to: |
| 53 | +- `conf-builder-main.js` default state (line 80) |
| 54 | +- Migration check in `onConfigure` handler (~line 363) for existing saved workflows |
| 55 | + |
| 56 | +## Config Data Flow |
| 57 | + |
| 58 | +``` |
| 59 | +JS node.state |
| 60 | + → JSON.stringify() → lora_config widget value |
| 61 | + → Python json.loads(lora_config) in generate_config() |
| 62 | + → Output: {"configs": [...], "_distribution": {...}, "_session_settings": {...}} |
| 63 | + → sampler_node.py unwraps configs, extracts _distribution + _session_settings |
| 64 | + → generation_orchestrator.run_generation_loop() receives all as params |
| 65 | + → config_utils.expand_configs() creates Cartesian product job list |
| 66 | + → Loop: load model → encode prompts → generate → save |
| 67 | +``` |
| 68 | + |
| 69 | +## Session Settings (Not Per-Config) |
| 70 | + |
| 71 | +`_session_settings` is session-level, embedded alongside configs: |
| 72 | +```json |
| 73 | +{ |
| 74 | + "upscaling": { |
| 75 | + "enabled": true, |
| 76 | + "configs": [{ "mode": "hires_only", "upscale_models": [], "upscale_ratios": "1.5", "hires_denoise": "0.3", "hires_steps": 0, "tiled_vae": false, "tile_size": 512 }] |
| 77 | + }, |
| 78 | + "cooldown": { "enabled": true, "seconds": 5, "every_n": 1, "clear_vram": false } |
| 79 | +} |
| 80 | +``` |
| 81 | +Extracted by `sampler_node.py` line 277, passed to orchestrator as `session_settings` param. |
| 82 | + |
| 83 | +## Image/Manifest Format |
| 84 | + |
| 85 | +Images saved as WebP at `output/benchmarks/{session}/images/img_{id}.webp` |
| 86 | + |
| 87 | +**File URL format** (MUST match exactly for ComfyUI `/view` endpoint): |
| 88 | +``` |
| 89 | +/view?filename={name}&type=output&subfolder=benchmarks/{session}/images |
| 90 | +``` |
| 91 | + |
| 92 | +**Manifest entry required fields:** |
| 93 | +```json |
| 94 | +{ |
| 95 | + "id": 170941234567890, // int(time.time() * 100000) + random.randint(0, 1000) |
| 96 | + "file": "/view?filename=...", // Full URL as above |
| 97 | + "rejected": false, // Required for dashboard display |
| 98 | + "favorited": false, // User annotation |
| 99 | + "seed": 12345, "cfg": 7.0, "steps": 20, |
| 100 | + "sampler": "euler", "scheduler": "normal", |
| 101 | + "model": "model.safetensors", "lora": "lora:1.0:1.0", |
| 102 | + "positive": "prompt", "negative": "neg prompt", |
| 103 | + "width": 1024, "height": 1024, "duration": 45.2, "denoise": 1.0 |
| 104 | +} |
| 105 | +``` |
| 106 | + |
| 107 | +## Key Patterns |
| 108 | + |
| 109 | +- **Model discovery:** `folder_paths.get_filename_list("key")` → API at `/configbuilder/model_lists` → JS `getModelLists()` |
| 110 | +- **Path security:** All endpoints use `_is_path_within()` + `re.sub(r'[^\w\-]', '', name)` sanitization |
| 111 | +- **Dashboard updates:** `PromptServer.send_sync("ultimate_grid.update", {...})` → `dashboard.js` → `postMessage()` → iframe |
| 112 | +- **No `import requests`** — use `urllib.request` (ComfyUI Registry security requirement) |
| 113 | +- **No `subprocess`, `os.system`, `eval()`, `exec()`** — blocked by security scanner |
| 114 | +- **LoRA string format:** `"name:model_str:clip_str"`, stacked with `" + "` separator |
| 115 | +- **Prompt nesting:** Flat list = OR, nested lists = AND (Cartesian product). Recursive in `parse_prompt_input_nested()` |
| 116 | + |
| 117 | +## Field Name Warning |
| 118 | + |
| 119 | +`manifest_utils.py` uses key `"favorite"`. `__init__.py` routes use `"favorited"`. Always check which key the specific code path expects. |
| 120 | + |
| 121 | +## Testing Changes |
| 122 | + |
| 123 | +- **Python files:** Restart ComfyUI |
| 124 | +- **`web/` JS files:** Refresh browser (Ctrl+F5) |
| 125 | +- **`resources/` files:** Must regenerate dashboard HTML — re-run sampler or call `/config_tester/get_session_html` |
| 126 | +- **New state fields:** Add migration in `conf-builder-main.js` `onConfigure` |
| 127 | + |
| 128 | +## Common Tasks |
| 129 | + |
| 130 | +**Add a new config field:** |
| 131 | +1. Add to default state in `conf-builder-main.js` (line 80) |
| 132 | +2. Add migration in `onConfigure` (~line 363) |
| 133 | +3. Add UI rendering in `conf-builder-config-management.js` |
| 134 | +4. Add to `convertStateToConfigs()` in `conf-builder-utilities.js` |
| 135 | +5. Add to `generate_config()` in `config_builder_node.py` |
| 136 | +6. Consume in `generation_orchestrator.py` or relevant backend file |
| 137 | + |
| 138 | +**Add a new API endpoint:** |
| 139 | +1. Add route in `__init__.py` with `@server.PromptServer.instance.routes` |
| 140 | +2. Add path security: sanitize session name, use `_is_path_within()` |
| 141 | +3. Call from dashboard JS (`resources/logic_utils.js`) or config builder JS (`web/conf_builder/`) |
| 142 | + |
| 143 | +**Add a new session setting:** |
| 144 | +1. Add to default state in `conf-builder-main.js` under appropriate key |
| 145 | +2. Add UI in `conf-builder-config-management.js` |
| 146 | +3. Add to `convertStateToConfigs()` session settings block (line 689) |
| 147 | +4. Add to `generate_config()` session settings embedding (line 706) |
| 148 | +5. Consume in `generation_orchestrator.py` after the `session_settings` param check |
| 149 | + |
| 150 | +**Modify dashboard UI:** |
| 151 | +1. Edit files in `resources/` (HTML in `template.html`, JS in `logic_*.js`, CSS in `report.css`) |
| 152 | +2. Remember: changes require HTML regeneration to take effect |
| 153 | +3. JS load order: state → utils → ui → virtual → pipeline → events → init |
0 commit comments