Skip to content

models: list a cmd tool's request fields, from its bindings - #165

Draft
smfworks wants to merge 29 commits into
jayleaton:mainfrom
smfworks:upstream/models-request-fields
Draft

smfworks wants to merge 29 commits into
jayleaton:mainfrom
smfworks:upstream/models-request-fields

Conversation

@smfworks

Copy link
Copy Markdown
Contributor

Stacked on #160 (for the size grid it reuses). Draft: review #160 first.

Problem

The ComfyUI cmd worker refuses a request field that its workflow does not bind, rather than ignoring it. That is right, but a client cannot see the bound fields in advance. An agent sent negative_prompt to an image edit and to a video model that take none. Both were refused only after the model had loaded (model '…' takes no negative prompt).

Change

  • GET /v1/models adds request for a cmd image or video tool with options.bindings:
    • fields: the request fields the workflow reads, named as the API takes them (prompt, size, seed, steps, guidance, negative_prompt, seconds, fps, audio, image, input_reference, mask). Width and height are size; frames are seconds.
    • modes: for each mode whose fields differ (image_edit, image_to_video), its fields. A JSON null in a mode's bindings drops the field, as it does in the worker.
    • max_references: how many input images an edit takes.
  • size (the width/height grid, as on /v1/tools) is also listed for image and video models.
  • request is absent for built-in engines (they validate their own fields) and for tools without bindings.
  • No workflow paths, node ids, or option values are exposed.
  • docs/AGENTS.md has the field list and an example.

Tests

  • Unit test (engine.zig): fields from base bindings in a stable order; per-mode fields with references and mask; a null binding drops size; a mode with no changes is not listed; max_references is counted; chat tools and tools without options give null.
  • Checked against a deployment config with MiniMax H3 and H3-Image. Neither lists negative_prompt. The H3-Image image_edit mode adds image and mask, with max_references: 5. The H3 image_to_video mode adds input_reference.
  • zig build test-unit passes; zig fmt --check is clean.

tools/comfyui/worker.py starts its own ComfyUI (options.comfy_dir/python/args/env,
loopback, a private output directory) or uses options.url, fills an API-format
workflow from the request through explicit bindings (node.input), queues it,
reports progress from /ws, waits on /history and fetches 0.png.. or video.mp4
through /view. Unbound fields (negative prompt, steps, guidance), edits and
image-to-video are refused, never ignored; sizes, frame grids (frame_multiple /
frame_offset) and fps are checked; audio off removes a bound input. Cancel is
SIGUSR1 (/queue delete + /interrupt), and the worker stays loaded. Standard
library only; test_worker.py runs it against a fake ComfyUI (16 checks).

engine.fill: a video tool's options may name default_width/default_height for
requests without a size (a cmd model's own size); else 1344x768 as before.
options.modes.image_edit / image_to_video give requests with input images
their own workflow and bindings, merged over the tool's. references, mask
and first_frame are uploaded to ComfyUI (/upload/image, into the worker's
private input directory, cleaned per job) and wired in through added
LoadImage nodes (IMAGE, or MASK for a mask). Requests a tool cannot honour
are still refused. Tests: 24 checks against a fake ComfyUI.
image_edit may set defaults and an unmasked overlay (workflow, bindings,
defaults) used when a request has input images but no mask; request fields
still override. Masked edits keep the tool's fast defaults. Tests cover the
auto-select and the overrides.
worker.py had grown to 662 lines with edits and image to video, past the
600-line convention; the RFC 6455 client moves to comfy_events.py (76 lines) and
worker.py is 596. No behaviour change: the 29 worker checks pass.
…ts code

A job, video or 3D asset object's error now has type (the value an HTTP error
for the same failure carries, e.g. insufficient_memory), code (the same value,
as before) and message. A client matching the HTTP type in a job's message
missed jobs refused for room (the message reads "insufficient memory: ...")
and gave up instead of retrying. docs/AGENTS.md: match type or code, never
the message. Test: the error object's JSON.
A video job holds its model while it runs; a chat request (or an API load)
that needs the room is refused at once with 503 insufficient_memory, and loads
once the job is done. The scheduler already does this (in_use); the test pins
it, and fails if admit may take a slot in use.
ComfyUI no longer gets a session of its own (start_new_session): it stays in
the worker's process group, so the daemon's SIGKILL of that group on abort or
timeout takes it down too, and PR_SET_PDEATHSIG (SIGTERM) covers the worker
dying alone. Before, a hard kill left ComfyUI holding the GPU. An unload now
signals ComfyUI and every process below it instead of its group; SIGUSR1
(cancel) still goes to the worker alone.

Test: SIGKILL of the worker's group mid-render, the fake ComfyUI is gone
(fails on the old worker). The worker tests start the worker in its own group,
as the daemon does.
Expose each tool's state, resident bytes, queue depth, last load and last
error, plus the memory budget and eviction count. /health stays {"ok":true}.
The output has no paths, secrets or log text.
size_snap rounds width and height to the nearest multiple inside the model's
limits (1280x720 becomes 1280x704 on a 32 grid) and the response names the
size used. The default still refuses an off-grid size. GET /v1/tools lists
each tool's size constraints.
Tool logs under data/logs rotate by size and keep N older files, both
configurable (8 MiB and 3 by default).
A 32-pixel tool no longer answers "multiples of 16". The error names that
tool's limits, and a cmd tool that declares them is refused before it loads.
A chat request could unload a model that was still loading or committing
memory for a job (minimax-h3, priority 10, for qwen3.8-flash-next). A tool
in that state, or running a job, is not a victim. A request that fits by
unloading an idle model still does. One that cannot fit while a
non-evictable job holds the memory is refused at once with 503
insufficient_memory (type and code) and retry_after, also sent as
Retry-After, instead of sitting until the client times out. One that will
fit once a load's temporary overshoot settles waits for that load.

retry_after_s in the config (30 when unset) is the hint; a blocker whose
last load took longer raises it.
last_load_s read like a duration but held a unix timestamp. It is now
last_load_unix_s, and last_load_ms (the figure GET /v1/tools already shows)
says how long that load took. The Prometheus series keeps its name
(..._last_load_timestamp_seconds).
The journal line printed the host's MemAvailable as "MiB available",
which reads as the router budget's and can be far from it on a unified
memory machine. It now prints the budget's free bytes (the ceiling less
what is resident and admitted, as /v1/status free_bytes), then the host's
available memory and the reserve, labelled. evictionLine is a pure function
with a test, including an over-budget tool (0 free, no wrap).
…age recovers

roomIfEvicted took the victims' resident bytes off the request
(fits(extra -| freeable)). That clamps at 0 and still asks the current
usage to fit, so once a worker reported more than its estimate and usage
went over the budget, every later load was refused with 503 though the
worker was idle and could be evicted. The check now takes the victims'
bytes off what is held and adds them back to the host's available memory
(fitsFreeing), the state the eviction loop would reach.

Regression test: a 100 MiB budget, a worker estimated at 60 MiB that loads
at 120 MiB, then a 10 MiB load evicts it and loads.
…t keeps it open

A worker's descendant can start its own session, survive the group kill
and keep stderr open. joinLog waited for EOF forever with the scheduler
gate held, so unload and status stopped responding. The log-copy thread
now polls with a stop flag: after the worker is gone it stops at the first
quiet poll (100 ms) or after 1 s if the descendant keeps writing. wait
closes the read end only after the copy thread returns.

Test: a worker starts a descendant in a new session holding stderr, then
exits. Unload returns within 3 s while the descendant is still alive, and
another load/unload works. The test fails without the stop flag.
…age recovers

roomIfEvicted took the victims' resident bytes off the request
(fits(extra -| freeable)). That clamps at 0 and still asks the current
usage to fit, so once a worker reported more than its estimate and usage
went over the budget, every later load was refused with 503 though the
worker was idle and could be evicted. The check now takes the victims'
bytes off what is held and adds them back to the host's available memory
(fitsFreeing), the state the eviction loop would reach.

Regression test: a 100 MiB budget, a worker estimated at 60 MiB that loads
at 120 MiB, then a 10 MiB load evicts it and loads.
An agent sent negative_prompt to an image edit and a video model that
take none; the worker refused both after loading the model. /v1/models
now lists request.fields for a cmd image or video tool with bindings,
and request.modes for each mode that differs (image_edit,
image_to_video). Keys are named as the API reads them (width/height are
size, frames is seconds); JSON null drops a binding, as in the worker.
max_references is how many input images an edit takes. Common transport
fields are not listed. Unknown for a built-in engine or a tool without
bindings, so absent then. size gives the size grid, as on /v1/tools.

No paths, node ids, or option values are exposed. Tested against an
H3/H3-Image config: neither lists negative_prompt, edit adds image/mask
and max_references 5, i2v adds input_reference.
ci.yml: main's openai_audio step, then this branch's ComfyUI worker step.
…s' bytes

fitsFreeing added the victims' resident bytes to MemAvailable, read as 0
when unreadable. With a 20 MiB reserve and no reading, an idle 60 MiB
worker alone cleared the reserve, so the room check passed, the eviction
loop unloaded the worker, and the 10 MiB load was still refused with 503.
Before the over-budget fix the same load was refused and the worker stayed.

When the reading is unavailable and the check would count freed bytes, it
now refuses first. With nothing freed the check is unchanged.

memory.test_available lets a test stand in for the host's reading.
Regression test: "no MemAvailable reading: a load is refused and no
healthy worker is evicted for it" (fails without the fix).
…ble reading refuses before counting the victims' bytes) into this branch

# Conflicts:
#	src/sched/scheduler.zig
…ol ids are never blank

The line went through a 256-byte buffer, and bufPrint failed (NoSpaceLeft)
between two valid 100-character tool ids, so the journal got a blank entry.
admit now logs std.log.info(eviction_fmt, evictionArgs(...)) directly.

Test: "the eviction line between two 100-character tool ids is whole, not
blank"; "the eviction line reports the budget's free bytes, not the host's"
now formats through the same eviction_fmt.
The worker writes its detached descendant's pid over the last one on each
load, and the test only killed the first. It now reads the pid after each
load and registers that child's cleanup then, and also checks the second
unload is quick and the two descendants differ.
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