Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion comfy_cli/command/assets_library.py
Original file line number Diff line number Diff line change
Expand Up @@ -152,7 +152,9 @@ def ls_cmd(
renderer = get_renderer()
target = cloud_target_or_local_error(where, renderer)

params: list[tuple[str, Any]] = [("limit", min(max(limit, 1), 500))]
# `capped_total=false` asks Comfy Cloud for the exact `total` this command
# forwards; without it the server may answer with a capped count.
params: list[tuple[str, Any]] = [("limit", min(max(limit, 1), 500)), ("capped_total", "false")]
if name:
params.append(("name_contains", name))
for t in tags.split(",") if tags else []:
Expand Down
11 changes: 9 additions & 2 deletions comfy_cli/command/models/search.py
Original file line number Diff line number Diff line change
Expand Up @@ -457,7 +457,7 @@ def _cloud_search(
tokens = _search_tokens(text)
if tokens is None:
# No filter: one page, the server's own total.
body = _assets_page(target, {**base, "limit": min(max(limit, 1), 500)})
body = _assets_page(target, {**base, **_EXACT_TOTAL, "limit": min(max(limit, 1), 500)})
rows = [_asset_to_row(a) for a in body.get("assets") or [] if isinstance(a, dict)]
return rows, int(body.get("total") or len(rows)), False
if not tokens:
Expand All @@ -469,7 +469,7 @@ def _cloud_search(
if plain_word:
# The server's substring match is already exact for a plain word:
# one page, and its total, when it finds anything.
body = _assets_page(target, {**base, "name_contains": text, "limit": min(max(limit, 1), 500)})
body = _assets_page(target, {**base, **_EXACT_TOTAL, "name_contains": text, "limit": min(max(limit, 1), 500)})
rows = [_asset_to_row(a) for a in body.get("assets") or [] if isinstance(a, dict)]
if rows:
return rows, int(body.get("total") or len(rows)), False
Expand All @@ -480,6 +480,13 @@ def _cloud_search(
return _scan_assets(target, base, tokens, limit)


# The two one-page answers above report the server's `total` as exact.
# `capped_total=false` asks Comfy Cloud for that exact count; without it the
# server may answer with a capped one. A scan counts its own matches and
# does not send it.
_EXACT_TOTAL = {"capped_total": "false"}


def _scan_assets(
target, params: dict[str, Any], tokens: list[tuple[str, str]], limit: int
) -> tuple[list[dict[str, Any]], int, bool]:
Expand Down
13 changes: 13 additions & 0 deletions tests/comfy_cli/command/models/test_search.py
Original file line number Diff line number Diff line change
Expand Up @@ -964,6 +964,19 @@ def test_single_word_query_is_sent_to_the_server_unchanged(self, cloud_target, m
assert queries[0]["name_contains"] == ["flux"]
assert len(queries) == 1

def test_one_page_answers_ask_for_the_exact_total(self, cloud_target, monkeypatch, capsys):
"""A one-page answer reports the server's `total` as exact, so it asks for the uncapped count."""
queries = _patch_asset_server(monkeypatch, _CLOUD_CATALOG)
_run(["search", "--where", "cloud"], capsys)
_run(["search", "--text", "flux", "--where", "cloud"], capsys)
assert [q["capped_total"] for q in queries] == [["false"], ["false"]]

def test_a_scan_does_not_ask_for_the_exact_total(self, cloud_target, monkeypatch, capsys):
"""A scan counts its own matches, so it leaves the server's total capped."""
queries = _patch_asset_server(monkeypatch, _CLOUD_CATALOG)
_run(["search", "--text", "sdxl base", "--where", "cloud"], capsys)
assert queries and all("capped_total" not in q for q in queries)

def test_a_word_the_filename_splits_still_finds_it(self, cloud_target, monkeypatch, capsys):
"""`sdxl base` must find `sd_xl_base_1.0`, as local search does, though the server only knows `sd_xl`."""
_patch_asset_server(monkeypatch, _CLOUD_CATALOG)
Expand Down
10 changes: 10 additions & 0 deletions tests/comfy_cli/command/test_assets_library.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
import os
import sys
import urllib.error
import urllib.parse
from pathlib import Path
from types import SimpleNamespace
from typing import Any
Expand Down Expand Up @@ -191,6 +192,15 @@ def test_forwards_has_more_and_total(self, cloud_target, monkeypatch, capsys):
assert data["count"] == 1
assert data["assets"][0]["id"] == "a1"

def test_asks_for_the_exact_total(self, cloud_target, monkeypatch, capsys):
# Without `capped_total=false` Comfy Cloud may answer with a capped
# `total`, and `ls` would forward it as if it were the exact count.
calls = _patch_urlopen(monkeypatch, {"assets": [], "has_more": False, "total": 0})
_run(["ls", "--name", "cat", "--where", "cloud"], capsys)
query = urllib.parse.parse_qs(urllib.parse.urlparse(calls[0]["url"]).query)
assert query["capped_total"] == ["false"]
assert query["name_contains"] == ["cat"]

def test_forwards_has_more_false(self, cloud_target, monkeypatch, capsys):
# `false` is a real answer, not a missing one — the falsy value must
# survive, otherwise the consumer cannot distinguish "not truncated"
Expand Down
Loading