diff --git a/.github/workflows/public-repo-hygiene.yml b/.github/workflows/public-repo-hygiene.yml index 196f61dda..d6b720fbb 100644 --- a/.github/workflows/public-repo-hygiene.yml +++ b/.github/workflows/public-repo-hygiene.yml @@ -40,7 +40,19 @@ jobs: # set_title prototype that ADR-032 originally recorded, since # generalized and merged there as PR #235). It fits the ticket-ID shape # but is a public doc filename, not an internal ticket. - ticket_allowlist: HAILUO-03,HALO-03,ADR-032 + # `IEEE-754` is the IEEE floating-point standard, named in + # comfy_cli/workflow_ops.py, docs/op-vocabulary-v1.md and + # tests/comfy_cli/test_op_id_reuse_contract.py while documenting why an + # integer past 2^53 is spelled the way ECMAScript spells it. The checker + # already carries `UTF-8`, `ISO-8601`, `SHA-256` and three `RFC-####`s as + # exact tokens and `CVE`/`CWE`/`PEP`/`RFC`/`ISO`/`UTF` as prefixes, so a + # public standards identifier is a known class of false positive there; + # `IEEE` is simply not on either list yet. Only the exact-token list is + # reachable from a caller, so this is the token, not the namespace — a + # prefix carve-out for `IEEE` belongs upstream in + # TICKET_ALLOWED_PREFIXES, where the same comment already argues the + # namespace is the right granularity for this class. + ticket_allowlist: HAILUO-03,HALO-03,ADR-032,IEEE-754 # One `exclude_paths:` entry, and the bar it had to clear is recorded here # because the entry is permanent. Every entry this caller used to carry # was a false positive in the checker itself — Hugging Face model URLs and diff --git a/CHANGELOG.md b/CHANGELOG.md index c6debae16..ff19b8260 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -190,6 +190,32 @@ history. ### Changed +- A structured-edit batch is bounded at 1,024 ops, the limit the shared + `comfy-multi-player` applier already enforces, and each op in it is bounded by + the per-op payload limits that applier applies. `comfy workflow apply` refuses + a longer batch and writes nothing — counting the ops the specs actually mint, + since one `set_dynamic_group_rows` spec expands into a write per row field. + `templates fetch --emit-ops` and `generate --emit-workflow --emit-ops` report + `ops_skipped` beside the workflow file they still write, rather than emitting + a batch that would be rejected whole on arrival. + + Where the bound can be reached: the measurement behind it covered the + workflows being written — over the whole template gallery the longest batch + that path produces is 50 ops — and a replacement also emits a `delete_node` + per node already in the file being replaced, which is not bounded by anything + this measurement saw. So a fetch or an emit into a path that already holds a + workflow of about a thousand nodes can exceed the bound while the incoming + template is small. That case now skips the batch and keeps the file, where an + emit previously failed and wrote nothing. + +- An integer past 2^53 in an op payload is digested as the double it becomes, + the way ECMAScript spells it, rather than as its exact Python digits. JSON + carries no integer type, so a relay through the shared applier rounds such a + value — a seed at `0xffffffffffffffff` comes back as `18446744073709552000` — + and digesting the exact digits made an identical op echoed back look like a + changed payload reusing its `op_id`. Integers inside that range, which is + every id, stamp and ordinary widget value, are unaffected. + - Once every model byte is in, the progress line of `comfy deploy up`, `comfy deploy status` (with or without `--watch`) and a watching `comfy deploy promote` or `rollback` adds "finishing: putting the models in diff --git a/comfy_cli/command/generate/app.py b/comfy_cli/command/generate/app.py index 0585df524..a0d5fd412 100644 --- a/comfy_cli/command/generate/app.py +++ b/comfy_cli/command/generate/app.py @@ -593,6 +593,7 @@ def _track_error(error_kind: str, exc: BaseException) -> None: prefix = meta.get("output-prefix") if isinstance(meta.get("output-prefix"), str) else "generate" renderer = get_renderer() ops: list | None = None + ops_skipped: str | None = None from comfy_cli import workflow_ops try: @@ -625,7 +626,7 @@ def _track_error(error_kind: str, exc: BaseException) -> None: # is left without its terminal event. _track_error("emit", e) raise - workflow, ops = emit.write_frontend_workflow( + workflow, ops, ops_skipped = emit.write_frontend_workflow( name, values, Path(emit_path).expanduser(), @@ -683,6 +684,17 @@ def _track_error(error_kind: str, exc: BaseException) -> None: if renderer.is_pretty(): rprint(f"[bold green]Wrote workflow:[/bold green] {emit_path}") rprint(f" run it: comfy run --workflow {emit_path}") + if ops_skipped: + # `--emit-ops` is a request, and this is the one path where + # it silently went unanswered: the batch is reported as + # `ops_skipped` in the envelope below, which `emit()` does + # not print in pretty mode. Without this the user asked for + # ops, saw only "Wrote workflow", and had no way to tell the + # batch apart from one that was minted. + renderer.warn( + f"--emit-ops: no batch was minted — {ops_skipped}", + hint="the workflow file above is complete; apply it as a whole document", + ) payload = { "out": str(Path(emit_path).expanduser()), "model": name, @@ -691,6 +703,12 @@ def _track_error(error_kind: str, exc: BaseException) -> None: } if ops is not None: payload["ops"] = ops + elif ops_skipped: + # Same envelope key `templates fetch --emit-ops` uses: the file + # was written, the batch was not minted, and this says why so a + # consumer can fall back to the whole-document path instead of + # guessing from a missing key. + payload["ops_skipped"] = ops_skipped renderer.emit(payload, command="generate emit-workflow") return diff --git a/comfy_cli/command/generate/emit.py b/comfy_cli/command/generate/emit.py index 2bce3a439..d1bd1435b 100644 --- a/comfy_cli/command/generate/emit.py +++ b/comfy_cli/command/generate/emit.py @@ -618,12 +618,22 @@ def write_frontend_workflow( actor: str = "cli", base_version: int = 0, output_prefix: str = "generate", -) -> tuple[dict[str, Any], list[dict[str, Any]]]: +) -> tuple[dict[str, Any], list[dict[str, Any]] | None, str | None]: """Build the workflow for ``model`` as a FRONTEND-format graph and write it - to ``path``; return ``(workflow, ops)`` where ``ops`` is the stamped - ``replace_ops`` batch that turns whatever ``path`` previously held into the - new graph (empty previous ⇒ no delete half), ready for the envelope exactly - like ``templates fetch --emit-ops``. + to ``path``; return ``(workflow, ops, ops_skipped)`` where ``ops`` is the + stamped ``replace_ops`` batch that turns whatever ``path`` previously held + into the new graph (empty previous ⇒ no delete half), ready for the envelope + exactly like ``templates fetch --emit-ops``. + + ``ops`` is ``None`` with ``ops_skipped`` set when the batch cannot be minted. + That is NOT a failure of the request — the file is written either way, and a + consumer falls back to the whole-document path it had before ops existed, + exactly as ``templates fetch --emit-ops`` does. The reason it can happen at + all is the PREVIOUS document: ``replace_ops`` emits a ``delete_node`` per + node already at ``path``, so a target that holds a large workflow can push + the batch past the 1,024-op bound even though the graph being written is + tiny. That is ordinary user data, not a bug in the built graph, so failing + the whole command over it would discard a workflow that is perfectly good. Raises ``EmitError``/``UnsupportedModelError`` like :func:`write_workflow`; an applier failure surfaces as ``EmitError`` (the request itself was @@ -665,10 +675,20 @@ def write_frontend_workflow( # `replace_ops` so the returned workflow, the file and the batch describe # one document. workflow_ops.strip_internal(workflow) + ops: list[dict[str, Any]] | None = None + ops_skipped: str | None = None try: ops = workflow_ops.replace_ops(previous, workflow, actor=actor, base_version=base_version) - except workflow_ops.NotExpressibleError as e: # can't-happen for our own built graph; fail loudly if it does - raise EmitError(f"the {model!r} workflow cannot be expressed as ops: {e}") from e + except workflow_ops.NotExpressibleError as e: + # Non-fatal, like `templates fetch --emit-ops`: the workflow itself is + # fine and still gets written. This used to be commented as can't-happen + # for our own built graph, and for the STRUCTURAL reason it is — but the + # batch also counts a `delete_node` per node already at `path`, which is + # user data, so a large existing file could fail the whole command and + # write nothing. The umbrella `emit_workflow_failed` hint then told the + # caller to check the model name and its required inputs, for a failure + # neither of them caused. + ops_skipped = str(e) path.parent.mkdir(parents=True, exist_ok=True) path.write_text(json.dumps(workflow, indent=2) + "\n", encoding="utf-8") - return workflow, ops + return workflow, ops, ops_skipped diff --git a/comfy_cli/workflow_ops.py b/comfy_cli/workflow_ops.py index 9a22f5b81..81633b2c6 100644 --- a/comfy_cli/workflow_ops.py +++ b/comfy_cli/workflow_ops.py @@ -97,6 +97,21 @@ _MAX_OP_COLLECTION_ENTRIES = 4096 _MAX_OP_COST = 262_144 +# The fourth bound in that same `src/limits.ts` block (`MAX_OPS_PER_BATCH`, also +# 1024 at 221b2b2e470c79afaface24b1edcb53cdafc8922), and the only one that is +# about the BATCH rather than about one op's payload. CMP checks it in `applyOps` +# before any op is processed (`applier.ts`, `batch of N ops exceeds the 1024-op +# limit; rejected before any op was processed`) and again in `inspectOps`, so a +# longer batch is refused WHOLE — not truncated, and not partially applied. +# +# Without a counterpart here, comfy-cli happily builds and hands out a batch of +# any length, and the refusal lands on the shared package after comfy-cli has +# already written the document (`apply_specs`) or already told a caller the batch +# is the legal translation of its graph (`replace_ops`). That is the same +# mint-then-refuse shape the three payload bounds above exist to close, one level +# up. Mirror it, never widen it here alone. +_MAX_OPS_PER_BATCH = 1024 + #: Any surrogate code point in a Python ``str`` is a LONE surrogate — astral #: characters are single code points there — so matching the whole block is #: enough to find the ones that cannot be UTF-8 encoded. @@ -146,6 +161,39 @@ def _es_number(value: float) -> str: return f"{mantissa}e{'+' if e >= 0 else '-'}{abs(e)}" +#: ECMAScript holds an integer exactly only up to here. JSON has no integer type +#: on the other side of the wire — `JSON.parse` reads every numeric literal as an +#: IEEE-754 double, and comfy-multi-player's canonicalizer refuses a `BigInt` +#: payload outright (`applier.ts`: `rejectBigIntPayload`) — so past this bound the +#: value ARRIVES there rounded and `Number::toString` spells the rounded number. +_ES_EXACT_INT = 2**53 + + +def _es_int(value: int) -> str: + """Render one ``int`` as ECMAScript renders the number ``JSON.parse`` makes + of the same literal. + + Inside ``_ES_EXACT_INT`` the two spellings already agree, so the exact digits + are kept. Outside it they do not, and the disagreement is not cosmetic: a + seed at a FLOAT/INT widget's ``0xffffffffffffffff`` maximum renders + ``18446744073709551615`` here and ``18446744073709552000`` after a relay + through JS, and ``10**21`` renders as ``1e+21`` there — so an IDENTICAL op + echoed back through the shared package digests differently and is refused as + ``op_id_reuse``. That is the failure :func:`_es_number` exists to prevent, + one type over, which is why the rendering goes through the same double path. + """ + if -_ES_EXACT_INT <= value <= _ES_EXACT_INT: + return str(value) + try: + as_double = float(value) + except OverflowError: + # Past the double range `JSON.parse` yields Infinity, which + # `JSON.stringify` renders as `null` — the same spelling the non-finite + # float branch below uses. + return "null" + return _es_number(as_double) + + def _canonical_string(value: str) -> str: """One JSON string literal, escaped the way ``JSON.stringify`` escapes it. @@ -171,7 +219,17 @@ def _canonical_key(key: Any) -> str: if isinstance(key, bool): # before int: bool is an int subclass return "true" if key else "false" if isinstance(key, int): - return str(key) + if -_ES_EXACT_INT <= key <= _ES_EXACT_INT: + return str(key) + # Past the exact range the property name is coerced from the DOUBLE + # `JSON.parse` produced, so fall through to the float path below rather + # than spelling out digits no JS object key would ever carry. Note the + # Infinity spelling there, not `_es_int`'s `null`: a KEY is coerced with + # `String()`, not rendered by `JSON.stringify`. + try: + key = float(key) + except OverflowError: + key = math.inf if key > 0 else -math.inf if isinstance(key, float): if math.isnan(key): return "NaN" @@ -183,21 +241,41 @@ def _canonical_key(key: Any) -> str: raise ValueError(f"malformed_op: object key of type {type(key).__name__} is not canonical JSON") +def _charge(budget: list[int], units: int) -> None: + """Charge ``units`` against the cost budget and refuse AT the charge. + + Checking only at the end of :func:`_canonical_value` left the bound not + actually bounding the work: a string is charged by length and then handed to + ``_canonical_string``, which runs ``json.dumps`` plus a surrogate scan over + the whole thing and allocates up to ~6x its size for control-heavy input, so + an oversized payload was fully rendered before being refused. The shared + package has the same property for the same reason — ``opBoundsRefusal`` walks + and charges the payload BEFORE ``normalize`` renders anything — and the + refusal is unchanged either way, because the budget only ever grows. + """ + budget[0] += units + if budget[0] > _MAX_OP_COST: + raise ValueError(f"malformed_op: payload exceeds the {_MAX_OP_COST}-unit cost budget") + + def _canonical_value(value: Any, depth: int, budget: list[int]) -> str: if depth > _MAX_OP_PAYLOAD_DEPTH: raise ValueError(f"payload_too_deep: op payload nests deeper than {_MAX_OP_PAYLOAD_DEPTH} levels") - budget[0] += 1 + _charge(budget, 1) if value is None: - budget[0] += 8 + _charge(budget, 8) rendered = "null" elif isinstance(value, bool): # before int: bool is an int subclass - budget[0] += 8 + _charge(budget, 8) rendered = "true" if value else "false" elif isinstance(value, int): - budget[0] += 8 - rendered = str(value) + # Still a flat 8, deliberately: the cost model is mirrored from the + # shared package ("8 per numeric/leaf", `src/limits.ts`), so charging a + # huge int by its digit count would refuse a payload CMP accepts. + _charge(budget, 8) + rendered = _es_int(value) elif isinstance(value, float): - budget[0] += 8 + _charge(budget, 8) # ``JSON.stringify`` renders every non-finite number as ``null``, so # doing the same is what makes the two canonical forms agree. It also # un-breaks overwriting a widget whose CURRENT value is NaN/Infinity @@ -205,16 +283,30 @@ def _canonical_value(value: Any, depth: int, budget: list[int]) -> str: # ``old``), which the previous ``allow_nan=False`` rejected outright. rendered = _es_number(value) if math.isfinite(value) else "null" elif isinstance(value, str): - budget[0] += len(value) + _charge(budget, len(value)) rendered = _canonical_string(value) elif isinstance(value, dict): if len(value) > _MAX_OP_COLLECTION_ENTRIES: raise ValueError(f"malformed_op: object exceeds {_MAX_OP_COLLECTION_ENTRIES} entries") - budget[0] += 4 + _charge(budget, 4) members = [] + # Coercion is not injective: `1` and `"1"`, `True` and `"true"`, `None` + # and `"null"` are distinct Python keys that name the same JS property. + # Emitting both produces an object literal no JS object can hold, and + # because `members.sort` is stable the digest of such a dict then depends + # on INSERTION order — so the canonical form would not be canonical. + # Only a programmatically built op can reach it (parsed JSON always has + # `str` keys), which is why it is refused rather than silently merged: + # there is no payload here whose intent a merge would preserve. + coerced: dict[str, Any] = {} for key in value: name = _canonical_key(key) - budget[0] += len(name) + _charge(budget, len(name)) + if name in coerced: + raise ValueError( + f"malformed_op: object keys {coerced[name]!r} and {key!r} both canonicalize to {name!r}" + ) + coerced[name] = key members.append((name, value[key])) # Unicode code point order at every depth: Python sorts `str` that way. members.sort(key=lambda member: member[0]) @@ -224,12 +316,10 @@ def _canonical_value(value: Any, depth: int, budget: list[int]) -> str: elif isinstance(value, list | tuple): if len(value) > _MAX_OP_COLLECTION_ENTRIES: raise ValueError(f"malformed_op: array exceeds {_MAX_OP_COLLECTION_ENTRIES} entries") - budget[0] += 4 + _charge(budget, 4) rendered = "[" + ",".join(_canonical_value(child, depth + 1, budget) for child in value) + "]" else: raise ValueError(f"malformed_op: op payload is not canonical JSON: {type(value).__name__} is not serializable") - if budget[0] > _MAX_OP_COST: - raise ValueError(f"malformed_op: payload exceeds the {_MAX_OP_COST}-unit cost budget") return rendered @@ -2693,13 +2783,20 @@ def reset_doc(workflow: dict, *, actor: str = "cli", base_version: int = 0) -> t class NotExpressibleError(ValueError): - """A graph uses structure the frozen v1 vocabulary cannot express. + """A graph the frozen v1 vocabulary cannot express as one op batch. Raised by :func:`replace_ops` INSTEAD of returning a partial batch. A partial batch is the dangerous answer: it applies cleanly and leaves a document that is not the graph the caller asked for. The caller is expected to fall back to whatever whole-document path it had before (the cloud agent re-mints), and to say why. + + Two reasons reach it. The graph may use STRUCTURE no frozen op kind can + create (a subgraph definition, a canvas group, a reroute point). Or the + translation may simply be too LONG — more than ``_MAX_OPS_PER_BATCH`` ops, + which the shared applier refuses whole. Both are "there is no legal batch + for this graph", and both want the same caller behaviour, so they share the + type rather than making every caller learn a second one. """ @@ -2798,7 +2895,8 @@ def replace_ops(old: dict, new: dict, *, actor: str = "cli", base_version: int = it reproduces the STRUCTURE (classes + wiring) while the op path reproduces the graph exactly, widget values included. - :raises NotExpressibleError: ``new`` uses structure no frozen op can create. + :raises NotExpressibleError: ``new`` uses structure no frozen op can create, + or its translation needs more than ``_MAX_OPS_PER_BATCH`` ops. """ reason = _inexpressible_reason(new) if reason: @@ -2886,6 +2984,32 @@ def replace_ops(old: dict, new: dict, *, actor: str = "cli", base_version: int = }, ) ) + # The batch is counted at the end rather than predicted up front because the + # exact length is only known here (a link to a node `new` does not contain is + # dropped above, so it costs no op), and nothing has left the function yet — + # the ops exist only in this local list. Returning a longer batch would hand + # the caller an artifact the shared applier rejects whole as `malformed_op`, + # which is the same "it applies cleanly nowhere" answer a partial batch is. + if len(ops) > _MAX_OPS_PER_BATCH: + raise NotExpressibleError( + f"the workflow needs {len(ops)} ops, more than the {_MAX_OPS_PER_BATCH}-op batch limit " + "(delete_node per existing node, add_node per node, connect per link)" + ) + # The batch length is only the outer bound. These ops are built here and + # handed straight to a caller — unlike `apply_specs`', every one of which + # goes through `apply_op` and is canonicalized there — so nothing had + # checked the PER-OP payload bounds (§20.3: depth 64, 4,096 entries, 262,144 + # cost). A node with an outsized `widgets_values`, or a `delete_node` whose + # `removed_links` runs past the entry cap, produced a batch this function + # presented as the legal translation of the graph and the shared applier + # then refused as `malformed_op`: the same mint-then-refuse shape one level + # down. Canonicalized rather than digested, since the refusal is the whole + # point and the hash is not needed. + for i, minted in enumerate(ops): + try: + _canonical_op(minted) + except ValueError as e: + raise NotExpressibleError(f"op #{i} ({minted['op']}) is not a legal op payload: {e}") from e return ops @@ -3410,7 +3534,29 @@ def _split_ref_slot(spec_val: str, aliases: dict[str, Any]) -> tuple[Any, Any]: def apply_specs( workflow: dict, graph, specs: list, *, actor: str = "cli", base_version: int = 0 ) -> tuple[dict, list, dict]: - """Apply edit specs to ``workflow`` in order. Returns (workflow, ops, aliases).""" + """Apply edit specs to ``workflow`` in order. Returns (workflow, ops, aliases). + + :raises ValueError: the batch mints more than ``_MAX_OPS_PER_BATCH`` ops. + """ + # The bound is on the OPS, and one spec is not one op — `set_dynamic_group_rows` + # expands into a row-count write plus one `set_widget` per row field. So this + # entry check is the cheap half: a spec list already longer than the bound + # cannot mint a batch within it (every spec mints at least one op), and + # catching it here refuses before the `at` coercion and before + # `layout.assign_positions`, both of which are per-spec work. The half that + # actually binds is the `len(ops)` check after the loop below; a batch of + # 1,000 specs that expands past 1,024 ops passes this one and is caught there. + # + # Either way the refusal is whole-batch, because `applyOps` refuses an + # over-long batch WHOLE: accepting it here applies the edit locally and has + # every op in it rejected downstream. §4's abort-remainder contract has + # nothing to say about that case — it describes which ops in a batch apply, + # and here none do. + if len(specs) > _MAX_OPS_PER_BATCH: + raise ValueError( + f"malformed_op: batch of {len(specs)} specs exceeds the {_MAX_OPS_PER_BATCH}-op limit; " + "rejected before any op was processed" + ) # Parse a string `at` up front: layout treats a pinned `at` as an obstacle # and reads it as coordinates. specs = [ @@ -3561,6 +3707,17 @@ def unknown_size(class_type: Any) -> list[float] | None: ) input_targets.add(encoded_target) ops.append(op) + # The entry check above counted specs. This one counts what they + # actually minted, which is what `applyOps` bounds: a batch of 1,000 + # specs carrying a few `set_dynamic_group_rows` clears the entry check + # and still returns more than 1,024 ops, and the shared applier then + # refuses every one of them as `malformed_op` after comfy-cli has + # already written the document. Inside the `try` so the discard rehint + # below records `spec_index`/`applied_count` like every other refusal + # in this batch, and `applied_count` is 0 — the caller discards the + # whole batch, so nothing this loop wrote survives. + if len(ops) > _MAX_OPS_PER_BATCH: + raise ValueError(f"malformed_op: batch mints {len(ops)} ops, more than the {_MAX_OPS_PER_BATCH}-op limit") except (NotBatchableError, DeprecatedNodeType): # Already carries a registered code and a hint that says what to do # instead — don't wrap it into a generic hint. @@ -3612,13 +3769,27 @@ def apply_op(workflow: dict, op: dict, graph) -> dict: recorded_digests = workflow.get("_applied_op_digests") recorded_ops = workflow.get("_applied_ops") if op_id in (recorded_ops or ()): - recorded = (recorded_digests or {}).get(op_id) - if recorded is None: + ledger = recorded_digests if isinstance(recorded_digests, dict) else {} + # MEMBERSHIP, not ``.get(...) is None``: a document carrying + # ``{: null}`` has a digest entry, it just has a malformed one. + # Reading its absence as "legacy list-only ledger" made a + # changed-payload redelivery a silent no-op on exactly the shape the + # up-front check does not cover — every other non-string value failed + # closed on the ``!=`` below, so null was the one that failed open. + if op_id not in ledger: # A legacy list-only ledger keeps its historical duplicate # behaviour, and deliberately WITHOUT digesting the payload first: # a replay whose envelope is not canonical must still be the # documented no-op rather than a ``malformed_op``. return workflow + recorded = ledger[op_id] + # The up-front check validates the ledger's TYPE, not its values, and + # both arrive from the same untrusted JSON. Refused on read rather than + # by walking every entry on every apply. + if not isinstance(recorded, str): + raise ValueError( + f"malformed_op: _applied_op_digests[{op_id!r}] must be a string digest, got {type(recorded).__name__}" + ) if recorded != _op_digest(op): raise ValueError(f"op_id_reuse: op_id {op_id!r} was already applied with a different payload") return workflow @@ -3636,14 +3807,28 @@ def apply_op(workflow: dict, op: dict, graph) -> dict: # different orders diverge silently. queued = workflow.get("_deferred_widget_ops") waiting = queued.get(op_id) if isinstance(queued, dict) else None - # Cannot raise: a queued op was canonicalized on the way in. - if isinstance(waiting, dict) and _op_digest(waiting) != digest: + # ``_applied_digest``, not ``_op_digest``: ``_deferred_widget_ops`` comes off + # the DOCUMENT and is never validated on load, so an oversized or deeply + # nested queued entry raised its own ``malformed_op:``/``payload_too_deep:`` + # here — against the incoming op, blocking it (even as an identical + # redelivery) with an error naming a payload that is not the one being + # applied. ``None`` is a mismatch, and correctly so: the op above was just + # digested, so it IS canonical, and an uncanonical queued entry therefore + # cannot be the same payload. ``_retry_deferred_widget_ops`` already treats + # an uncanonical queued entry as unusable for the same reason. + if isinstance(waiting, dict) and _applied_digest(waiting) != digest: raise ValueError(f"op_id_reuse: op_id {op_id!r} is already deferred with a different payload") - had_applied_ops = "_applied_ops" in workflow - workflow.setdefault("_applied_ops", []) + # BEFORE the ``setdefault`` below, because both of these raise OUTSIDE the + # rollback: a first-seen op with no ``op`` key, or a non-insert op carrying + # ``definitions``, is rejected and used to leave an empty ``_applied_ops`` + # on a document that arrived without one — contradicting §20.2's "a document + # that arrived without a ledger is never left carrying an empty one by a + # rejected op". kind = op["op"] if kind != "insert_workflow" and "definitions" in op: raise ValueError(f"malformed_op: {kind} does not accept definitions") + had_applied_ops = "_applied_ops" in workflow + workflow.setdefault("_applied_ops", []) # Snapshot the LWW bookkeeping so an exception escaping a handler cannot # leave a stamp committed WITHOUT its op_id recorded below. That pairing is # the poison state: a retry of the identical op loses to the failed diff --git a/docs/op-vocabulary-v1.md b/docs/op-vocabulary-v1.md index 34b047e6d..57c2dcd2a 100644 --- a/docs/op-vocabulary-v1.md +++ b/docs/op-vocabulary-v1.md @@ -1400,3 +1400,208 @@ in a different insertion order is an ordinary no-op. bullet now cites this section. Downstream repos pinning this document by SHA move the SHA together with their comfy-cli pin, and must move their CMP A8 pin with it. + +## 21. Amendment v1.12 — 2026-10-31 (a batch is bounded at 1,024 ops) + +**A batch carries at most 1,024 ops.** `comfy-multi-player`'s `src/limits.ts` at +`221b2b2e470c79afaface24b1edcb53cdafc8922` fixes `MAX_OPS_PER_BATCH` at 1024 and +refuses a longer batch **whole** — `applyOps` checks it before any op is +processed and returns a `malformed_op` rejection for every op in the batch, and +`inspectOps` raises `malformed_op` from the storage preflight. comfy-cli had no +counterpart, so `apply_specs` would accept and apply a batch of any length and +`replace_ops` would hand a caller one, and the refusal arrived from the shared +package only after this side had committed to the write. + +That is the same mint-then-refuse divergence v1.11 §20.3 closed for the three +payload bounds, one level up: §4's abort-remainder contract is about which ops in +a batch apply, and an over-long batch has no answer under it at all because none +of them do. The bound is therefore mirrored, not chosen here, and cited by SHA +rather than by branch for the same reason §20.3 gives. If a real edit needs a +longer batch, widen it in `comfy-multi-player` and move this pin; never widen it +here alone. + +Where it is enforced, and the two different refusals: + +* **`apply_specs`** checks it TWICE, because one spec is not one op. On entry + it raises `malformed_op: batch of specs exceeds the 1024-op limit; + rejected before any op was processed` — before the `at` coercion and before + the position planner, both of which are per-spec work, so the message's + promise is literally true. That check cannot be the whole bound: + `set_dynamic_group_rows` expands into a row-count write plus one `set_widget` + per row field, so a spec list within the bound can still mint a batch + past it. After the loop, and still inside the batch's discard handler, it + raises `malformed_op: batch mints ops, more than the 1024-op limit`; + `applied_count` is 0 there like every other mid-batch refusal. +* **`replace_ops`** raises **`NotExpressibleError`**, the existing "there is no + legal batch for this graph" signal, rather than a bare `ValueError`. Its + reasons are now *structure no frozen kind can create*, *a translation longer + than the bound*, and *one op whose own payload is out of §20.3's bounds* — + that last one because these ops are handed to a caller without ever passing + through `apply_op`, so nothing else canonicalizes them. All three want the + same caller behaviour, which both production callers already implement — + `templates fetch --emit-ops` and `generate`'s emit path each report + `ops_skipped` beside the file they still wrote. The count binds on the SUM of + `old` and `new`, because §8.8's replacement shape emits a `delete_node` per + node already in `old` before the adds. + +Measured headroom, and what the measurement does NOT cover. Across all 735 +workflows in `Comfy-Org/workflow_templates` — the gallery +`templates fetch --emit-ops` reads — the longest batch `replace_ops` actually +mints is **50 ops**, 4.9% of the bound. That measured the `new` side only. The +`old` side is whatever document the caller is replacing, which is user data and +is bounded by nothing here: a fetch or an emit into a path already holding a +workflow of ~1,000 nodes can exceed the bound on the `delete_node` half alone +while the incoming template is tiny. Both callers degrade rather than fail on +that case (`ops_skipped` beside a file they still write). 447 of those templates carry a +subgraph definition, a canvas group or a reroute point and are refused as +inexpressible before size is considered; across all 735, nodes+links peaks at +212, so even if every one of them became expressible the worst case stays a fifth +of the bound. The largest expressible workflow in comfy-cli's own fixtures mints +3 ops. + +The one document-sized payload this bound does **not** cover is a standalone +`clear` or `reset_doc`, which enumerates every node id inside ONE op rather than +as one op each. Those are bounded by §20.3's 4,096-entry collection cap instead, +and §1.5/§1.6 keep them out of `BATCHABLE_OPS`, so no large document turns into a +long batch behind a caller's back. + +**No change to §§1-3, 5-8.** No op kind was added, removed, or re-scoped; +`FROZEN_OPS` / `DEFERRED_OPS` / `BATCHABLE_OPS` are untouched. §4 gains an outer +precondition — a batch must be within the bound before abort-remainder has +anything to say about it — and is otherwise unaltered. Downstream repos pinning +this document by SHA move the SHA together with their comfy-cli pin. + +## 22. Amendment v1.13 — 2026-10-31 (v1.11 corrections: canonical form and ledger edges) + +Six corrections to Amendment v1.11, all found by review of the implementation +rather than by a failure in the field. None of them re-scopes an op kind and none +needs a change in `comfy-multi-player`; each is comfy-cli coming into line with +what v1.11 and CMP A8 already say. + +### 22.1 An integer past 2^53 is spelled as the double it becomes + +**§20.1's "numbers rendered as ECMAScript renders them" covers integers too, and +comfy-cli did not implement it that way.** It rendered every `int` with Python's +`str`, which is exact at any magnitude. The counterpart has no such type: JSON +carries no integer, `JSON.parse` reads every numeric literal as an IEEE-754 +double, and CMP's canonicalizer refuses a `BigInt` payload outright +(`applier.ts`: `rejectBigIntPayload`). So past 2^53 the other side holds the +ROUNDED value and `Number::toString` spells that. + +The consequence was the failure §20.1's float rule exists to prevent, one type +over. A seed at an INT widget's `0xffffffffffffffff` maximum spelled +`18446744073709551615` here and `18446744073709552000` after a relay through JS, +and `10**21` spelled `1e+21` there — so an **identical** op echoed back through +the shared package digested differently and was refused as `op_id_reuse`. + +The rule, which changes nothing inside the exact range: + +* `-2^53 <= n <= 2^53` keeps its exact digits — the two spellings already agree + there, and this is every integer any widget, node id, link id or stamp + carries; +* outside it, the value is rendered through the same double path as a float, so + it reads as `JSON.stringify(JSON.parse(literal))` would; +* past the double range entirely (`10**400`), `JSON.parse` yields `Infinity` and + `JSON.stringify` renders `null`, matching §20.1's non-finite bullet; +* an integer **object key** is the one place the two diverge, because a property + name is coerced with `String()` rather than rendered by `JSON.stringify`: + there the non-finite end is `"Infinity"` / `"-Infinity"`. Only a + programmatically built op can reach a non-`str` key at all — parsed JSON + always has string keys. + +The **cost charge is unchanged** at a flat 8 units per number, deliberately: +`src/limits.ts` documents the model as "8 per numeric/leaf", so charging a huge +integer by its digit count would refuse a payload CMP accepts — the same +validation drift §20.3 withdrew a comfy-cli-only budget to avoid. + +CMP already produces these spellings, because it canonicalizes with +`JSON.stringify`; this records that comfy-cli now agrees with it. The only +behavioural effect is on an op carrying an integer past 2^53, which now digests +as its JS spelling. + +### 22.2 The cost bound refuses at the charge, not after the render + +§20.3's budget exists to cap the WORK a hostile payload costs, and it was +checked only at the END of each `_canonical_value` call. A string is charged by +length and then handed to the renderer, which runs `json.dumps` plus a surrogate +scan over the whole thing and allocates up to ~6x its size for control-heavy +input — so an oversized string, or an oversized object key at the `+= len(name)` +charge, was fully rendered before being refused. The bound therefore did not +bound the work. + +Charging and refusing at the same point fixes that, and it does not change +WHETHER a payload is refused, because the budget only ever grows: every payload +that exceeded at the end exceeds at the charge that crossed the line, and every +charge that crosses the line is followed by an end that is also over. The shared +package already has this property for the same reason — `opBoundsRefusal` walks +and charges the payload before `normalize` renders anything. + +What it can change is WHICH refusal a payload that breaks two bounds at once +reports. An object with an oversized key AND a value nested past the depth limit +used to charge every key without checking, reach the values, and report +`payload_too_deep:`; it now reports the cost budget at the key. Both are +refusals of the same payload, and the cost-first order is the one CMP's +depth-first walk already produces, so this moves comfy-cli toward the +counterpart rather than away from it. No single-violation payload changes its +code. + +### 22.3 Object keys that collide after coercion are `malformed_op` + +§20.1 renders an object key as "its string form, matching ECMAScript property +coercion", and that coercion is not injective: `1` and `"1"`, `True` and +`"true"`, `None` and `"null"`, `1.5` and `"1.5"` are distinct Python keys naming +the same JS property. Emitting both members produced an object literal no JS +object can hold, and because the member sort is stable the digest of such a dict +then depended on INSERTION order — so the same logical payload could digest two +ways and §20's whole "one `op_id`, one payload" guarantee had a hole in it. + +Such a pair is refused as `malformed_op` rather than merged: there is no intent +in it a merge would preserve. Only a programmatically built op can reach the +case at all, since parsed JSON always has `str` keys, so no wire payload +changes behaviour. + +### 22.4 A `null` digest entry is malformed, not a legacy ledger + +§20.2's "legacy documents are untouched" rule is keyed on a document having +`_applied_ops` and **no** `_applied_op_digests` entry for the op. comfy-cli +decided that with `recorded is None`, which cannot tell a MISSING entry apart +from an explicit `null` one, and the up-front validation checks the ledger's +type rather than its values — both arrive from the same untrusted JSON. + +So a document carrying `{: null}` read as a legacy list-only ledger and +returned a changed-payload redelivery as a silent no-op, which is precisely the +divergence v1.11 exists to stop. Every other non-string value failed closed on +the digest comparison, so `null` was the one shape that failed open. + +The legacy case is now decided by MEMBERSHIP, and a present-but-non-string +digest entry is `malformed_op` — not `op_id_reuse`, because the document's +bookkeeping is broken rather than the sender's id being reused. It is refused on +read rather than by walking every ledger entry on every apply. + +### 22.5 The deferred-queue digest is taken defensively + +§20.2's deferred-queue gate digested the QUEUED entry with the strict +canonicalizer, under a comment claiming it could not raise because a queued op +was canonicalized on the way in. `_deferred_widget_ops` comes off the document +and is never validated on load, so that does not hold: an oversized or deeply +nested queued entry raised its own `malformed_op:` / `payload_too_deep:` against +the INCOMING op, blocking a well-formed op with an error naming a payload that +is not the one being applied. + +The queued entry is now digested with the same tolerant helper the retry loop +uses, and a `None` digest is a mismatch — correctly, because the incoming op has +already been digested at that point and is therefore canonical, so an +uncanonical queued entry cannot be the same payload. The refusal is +`op_id_reuse`, which is what it is. + +### 22.6 Kind and `definitions` are validated before the ledger is seeded + +§20.2 promises that "a document that arrived without a ledger is never left +carrying an empty one by a rejected op". Two validations sat on the wrong side of +that line: the read of the op's `op` field, and the refusal of `definitions` on +a non-`insert_workflow` op, both ran after `_applied_ops` had been seeded and +both raise outside the handler rollback. So a first-seen op with no `op` key, or +a non-insert op carrying `definitions`, was correctly rejected and left +`{"_applied_ops": []}` behind on a document that had no ledger at all. + +Both now run before the seed. No message or code changes. diff --git a/tests/comfy_cli/command/generate/test_emit_ops.py b/tests/comfy_cli/command/generate/test_emit_ops.py index 711677c4f..bbdca0df7 100644 --- a/tests/comfy_cli/command/generate/test_emit_ops.py +++ b/tests/comfy_cli/command/generate/test_emit_ops.py @@ -304,7 +304,7 @@ def test_ops_fold_multiple_images_through_image_batch(): def test_write_frontend_workflow_writes_frontend_and_returns_replace_batch(tmp_path): out = tmp_path / "workflow.json" - wf, ops = emit.write_frontend_workflow( + wf, ops, _skipped = emit.write_frontend_workflow( "nano-banana", {"prompt": "add sunglasses", "image": "cat.png"}, out, @@ -322,16 +322,54 @@ def test_write_frontend_workflow_writes_frontend_and_returns_replace_batch(tmp_p def test_write_frontend_workflow_emits_delete_half_over_a_previous_graph(tmp_path): out = tmp_path / "workflow.json" - _wf1, _ops1 = emit.write_frontend_workflow( + _wf1, _ops1, _s1 = emit.write_frontend_workflow( "flux-2", {"prompt": "first"}, out, _graph(), actor="agent", base_version=0 ) - _wf2, ops2 = emit.write_frontend_workflow( + _wf2, ops2, _s2 = emit.write_frontend_workflow( "nano-banana", {"prompt": "second", "image": "cat.png"}, out, _graph(), actor="agent", base_version=1 ) deletes = [o for o in ops2 if o["op"] == "delete_node"] assert deletes, "replacing an existing canvas must delete what it replaces, like templates fetch" +def test_a_large_existing_file_skips_the_batch_instead_of_discarding_the_workflow(tmp_path): + """``replace_ops`` emits a ``delete_node`` per node ALREADY at the target + path, so a target holding a big workflow pushes the batch past the 1,024-op + bound even though the graph being written is tiny. + + That is ordinary user data at a path the user named, not a bug in the built + graph. Raising turned it into an ``EmitError`` before the write, so nothing + was saved at all and the umbrella hint told the caller to check the model + name and its required inputs — for a failure neither of them caused. + ``templates fetch --emit-ops`` already degrades on the same condition, and + this now matches it: the file is written, ``ops`` is ``None``, and + ``ops_skipped`` says why. Red on the parent with ``EmitError``. + """ + out = tmp_path / "workflow.json" + out.write_text( + json.dumps( + { + "nodes": [ + {"id": i + 1, "type": "LoadImage", "widgets_values": ["example.png"], "inputs": [], "outputs": []} + for i in range(workflow_ops._MAX_OPS_PER_BATCH + 1) + ], + "links": [], + } + ), + encoding="utf-8", + ) + + wf, ops, ops_skipped = emit.write_frontend_workflow( + "flux-2", {"prompt": "first"}, out, _graph(), actor="agent", base_version=0 + ) + + assert ops is None + assert "1024-op batch limit" in ops_skipped + # The workflow itself is good and is on disk, which is the deliverable. + assert json.loads(out.read_text()) == wf + assert isinstance(wf.get("nodes"), list) and wf["nodes"] + + def test_write_frontend_workflow_unsupported_model_still_raises(tmp_path): with pytest.raises(emit.UnsupportedModelError): emit.write_frontend_workflow("no-such-model", {}, tmp_path / "w.json", _graph()) @@ -434,6 +472,149 @@ def test_cli_emit_ops_writes_frontend_and_envelope_ops(tmp_path, monkeypatch, ru assert isinstance(on_disk.get("nodes"), list), "the written file is canvas-editable frontend format" +def test_cli_emit_ops_says_so_in_pretty_mode_when_no_batch_was_minted(tmp_path, monkeypatch): + """A skipped batch has to be visible to a human, not only to `--json`. + + `ops_skipped` travels in the envelope, and `Renderer.emit` prints nothing in + pretty mode — so a pretty-mode run that asked for `--emit-ops` and got no + batch printed exactly what a successful one prints ("Wrote workflow"), with + no way to tell the two apart. The condition here is the ordinary one: the + target path already holds a workflow large enough that the `delete_node` per + existing node pushes the batch past the 1,024-op bound. + """ + from typer.testing import CliRunner + + from comfy_cli.cmdline import app + + oi_path = tmp_path / "object_info.json" + oi_path.write_text(json.dumps(_object_info()), encoding="utf-8") + monkeypatch.setenv("COMFY_OBJECT_INFO_FILE", str(oi_path)) + out = tmp_path / "workflow.json" + out.write_text( + json.dumps( + { + "nodes": [ + {"id": i + 1, "type": "LoadImage", "widgets_values": ["example.png"], "inputs": [], "outputs": []} + for i in range(workflow_ops._MAX_OPS_PER_BATCH + 1) + ], + "links": [], + } + ), + encoding="utf-8", + ) + + # `COMFY_OUTPUT` rather than a TTY probe: under CliRunner stdout is not a + # TTY, so mode resolution (`Renderer.resolve` rule 6) picks JSON and the + # pretty branch under test is never reached. `NO_COLOR`/`COLUMNS` keep Rich + # from wrapping or styling the warning mid-assertion. + result = CliRunner().invoke( + app, + [ + "generate", + "nano-banana", + "--prompt", + "add sunglasses", + "--image", + "cat.png", + "--emit-workflow", + str(out), + "--emit-ops", + ], + env={"COMFY_OUTPUT": "pretty", "NO_COLOR": "1", "COLUMNS": "400"}, + ) + + assert result.exit_code == 0, result.output + assert "Wrote workflow" in result.output + assert "--emit-ops" in result.output and "no batch was minted" in result.output + assert "1024-op batch limit" in result.output + # The file is still the deliverable and is still the newly built graph. + on_disk = json.loads(out.read_text()) + assert len(on_disk["nodes"]) <= workflow_ops._MAX_OPS_PER_BATCH + + +@pytest.mark.parametrize("flag", ["--json", "--json-stream"]) +def test_cli_emit_ops_reports_the_skip_in_the_machine_envelope(tmp_path, monkeypatch, flag): + """The machine-readable half of the same condition. + + The pretty warning exists because `ops_skipped` reaches only the envelope, + so that key is the whole premise and is worth pinning directly: on a skip the + envelope carries `ops_skipped` with the refusal reason and carries no `ops` + key at all, which is what lets a consumer fall back to the whole-document + path instead of inferring it from an empty batch. Both machine modes, since + `--json-stream` resolves to NDJSON and emits the same final envelope. + """ + from typer.testing import CliRunner + + from comfy_cli.cmdline import app + + oi_path = tmp_path / "object_info.json" + oi_path.write_text(json.dumps(_object_info()), encoding="utf-8") + monkeypatch.setenv("COMFY_OBJECT_INFO_FILE", str(oi_path)) + out = tmp_path / "workflow.json" + out.write_text( + json.dumps( + { + "nodes": [ + {"id": i + 1, "type": "LoadImage", "widgets_values": ["example.png"], "inputs": [], "outputs": []} + for i in range(workflow_ops._MAX_OPS_PER_BATCH + 1) + ], + "links": [], + } + ), + encoding="utf-8", + ) + + result = CliRunner().invoke( + app, + [ + flag, + "generate", + "nano-banana", + "--prompt", + "add sunglasses", + "--image", + "cat.png", + "--emit-workflow", + str(out), + "--emit-ops", + ], + ) + + assert result.exit_code == 0, result.output + envelope = json.loads(result.output.strip().splitlines()[-1]) + assert envelope["ok"] is True + data = envelope["data"] + assert "1024-op batch limit" in data["ops_skipped"] + # Absent, not null or empty: `templates fetch --emit-ops` uses the same + # envelope shape, and a consumer branches on the key being there. + assert "ops" not in data, sorted(data) + assert data["format"] == "frontend" + # The file is still written, which is the deliverable either way. + assert json.loads(out.read_text())["nodes"] + + +def test_cli_emit_ops_is_quiet_in_pretty_mode_when_the_batch_was_minted(tmp_path, monkeypatch): + """The complement: the warning is conditional, not printed on every run.""" + from typer.testing import CliRunner + + from comfy_cli.cmdline import app + + oi_path = tmp_path / "object_info.json" + oi_path.write_text(json.dumps(_object_info()), encoding="utf-8") + monkeypatch.setenv("COMFY_OBJECT_INFO_FILE", str(oi_path)) + out = tmp_path / "workflow.json" + + result = CliRunner().invoke( + app, + ["generate", "nano-banana", "--prompt", "x", "--image", "cat.png", "--emit-workflow", str(out), "--emit-ops"], + env={"COMFY_OUTPUT": "pretty", "NO_COLOR": "1", "COLUMNS": "400"}, + ) + + assert result.exit_code == 0, result.output + assert "Wrote workflow" in result.output + assert "no batch was minted" not in result.output + + def test_cli_emit_ops_without_emit_workflow_is_an_error(tmp_path, monkeypatch): from typer.testing import CliRunner @@ -454,7 +635,7 @@ def test_write_frontend_workflow_strips_apply_bookkeeping_before_write_and_batch next edit. Strip BEFORE ``replace_ops`` so the returned workflow, the file and the batch all describe the same document.""" out = tmp_path / "workflow.json" - wf, ops = emit.write_frontend_workflow( + wf, ops, _skipped = emit.write_frontend_workflow( "nano-banana", {"prompt": "ORIGINAL", "image": "cat.png"}, out, _graph(), actor="agent", base_version=7 ) on_disk = json.loads(out.read_text()) @@ -502,7 +683,7 @@ def test_write_frontend_workflow_materializes_nano_banana_pro_from_the_nightly_i options for model — closest: gemini-2.5-flash-image" — because emit built GeminiImageNode. #921 routes that model to GeminiImage2Node; this replays that input through the same frontend (canvas-ops) path.""" - wf, ops = emit.write_frontend_workflow( + wf, ops, _skipped = emit.write_frontend_workflow( "nano-banana", { "prompt": "Redraw this character as 16-bit pixel art.", diff --git a/tests/comfy_cli/test_dynamic_combo_other_option_widget.py b/tests/comfy_cli/test_dynamic_combo_other_option_widget.py index fa91a98f8..d2525983f 100644 --- a/tests/comfy_cli/test_dynamic_combo_other_option_widget.py +++ b/tests/comfy_cli/test_dynamic_combo_other_option_widget.py @@ -600,6 +600,44 @@ def test_a_deferred_write_is_bound_to_its_canonical_payload_once_applied(graph): assert wf == before +def test_an_uncanonical_queued_entry_does_not_block_with_the_wrong_error(graph): + """``_deferred_widget_ops`` comes off the DOCUMENT and is never validated on + load, so the queued entry is not guaranteed canonical the way the comment + here used to claim. + + Digesting it with ``_op_digest`` raised its own ``payload_too_deep:`` / + ``malformed_op:`` against the INCOMING op — an error naming a payload that + is not the one being applied, on an op that may be perfectly well formed. + ``_applied_digest`` returns ``None`` instead, which is a mismatch: the + incoming op was already digested above, so it is canonical, and an + uncanonical queued entry cannot be the same payload. The refusal is + therefore ``op_id_reuse``, which is what it actually is. + """ + wf, node_id = _fresh(graph) + hidden_write = workflow_ops._new_op( + "set_widget", + "agent-a", + 1, + node_id=node_id, + widget="model.prompt_expansion_mode", + value="quality", + ) + workflow_ops.apply_op(wf, hidden_write, graph) + assert hidden_write["op_id"] in wf["_deferred_widget_ops"] + + # Only a hand-edited document can get here, which is the point: nothing + # validates this on load. + nested = "leaf" + for _ in range(workflow_ops._MAX_OP_PAYLOAD_DEPTH + 2): + nested = [nested] + wf["_deferred_widget_ops"][hidden_write["op_id"]]["value"] = nested + before = copy.deepcopy(wf) + + with pytest.raises(ValueError, match="^op_id_reuse:"): + workflow_ops.apply_op(wf, copy.deepcopy(hidden_write), graph) + assert wf == before + + def test_a_second_payload_on_a_deferred_op_id_is_refused_rather_than_overwriting_it(graph): """The reuse gate used to read ``_applied_ops`` only. A deferred write sits in ``_deferred_widget_ops`` and nowhere else, so a second op carrying the diff --git a/tests/comfy_cli/test_insert_workflow_op.py b/tests/comfy_cli/test_insert_workflow_op.py index c92148483..69c958468 100644 --- a/tests/comfy_cli/test_insert_workflow_op.py +++ b/tests/comfy_cli/test_insert_workflow_op.py @@ -186,4 +186,6 @@ def test_non_definition_op_rejects_definitions_field(): with pytest.raises(ValueError, match="malformed_op.*definitions"): workflow_ops.apply_op(live, op, None) - assert live == {"nodes": [], "links": [], "_applied_ops": []} + # Amendment v1.11 (op-vocabulary-v1.md §20.2): a document that arrived + # without a ledger is never left carrying an empty one by a rejected op. + assert live == {"nodes": [], "links": []} diff --git a/tests/comfy_cli/test_max_ops_per_batch.py b/tests/comfy_cli/test_max_ops_per_batch.py new file mode 100644 index 000000000..d25ed7616 --- /dev/null +++ b/tests/comfy_cli/test_max_ops_per_batch.py @@ -0,0 +1,284 @@ +"""A batch is bounded at ``_MAX_OPS_PER_BATCH``, mirrored from the shared package. + +``comfy-multi-player``'s ``src/limits.ts`` has FOUR bounds on untrusted ops. The +three payload ones (depth, collection entries, cost) are pinned in +``test_op_id_reuse_contract.py`` beside the canonicalizer they guard. This file +owns the fourth, which is about the BATCH rather than about one op's payload, and +is therefore enforced somewhere else entirely: at the two surfaces that build or +dispatch a whole batch, ``apply_specs`` and ``replace_ops``. + +Why a local bound at all, when the shared package already has one: without it +comfy-cli builds a batch of any length and the refusal lands downstream *after* +comfy-cli has already written the document or already handed a caller the batch +as the legal translation of its graph. That is the mint-then-refuse shape — the +producer commits, the shared applier rejects the whole batch — that the payload +bounds exist to close one level down. +""" + +from __future__ import annotations + +import copy +import json +from pathlib import Path +from typing import Any + +import pytest + +from comfy_cli import workflow_ops as ops +from comfy_cli.cql.engine import Graph + +_OBJECT_INFO: dict[str, Any] = { + "KSampler": { + "input": {"required": {"steps": ["INT", {"default": 20, "min": 1, "max": 10000}]}}, + "input_order": {"required": ["steps"]}, + "output": ["LATENT"], + "output_name": ["LATENT"], + "category": "sampling", + "display_name": "KSampler", + "python_module": "nodes", + }, +} + +_FIXTURES = Path(__file__).parent / "fixtures" + + +@pytest.fixture +def graph() -> Graph: + return Graph.from_object_info(_OBJECT_INFO) + + +def _nodes(count: int) -> dict: + """A frontend-format graph of ``count`` unlinked nodes. + + ``replace_ops`` mints one ``add_node`` per node and one ``connect`` per link, + so an unlinked graph makes the op count equal to the node count and the + boundary exact. + """ + return { + "nodes": [ + {"id": i + 1, "type": "KSampler", "widgets_values": [20], "inputs": [], "outputs": []} for i in range(count) + ], + "links": [], + } + + +def _add_node_specs(count: int) -> list[dict]: + return [{"op": "add_node", "class_type": "KSampler"} for _ in range(count)] + + +_DYNAMIC_GROUP_FIXTURE = json.loads((_FIXTURES / "dynamic_group.json").read_text(encoding="utf-8")) + + +@pytest.fixture +def graph_with_group() -> Graph: + """A catalog carrying a DynamicGroup class, so a batch can be built whose op + count is larger than its spec count.""" + return Graph.from_object_info(copy.deepcopy(_DYNAMIC_GROUP_FIXTURE["object_info"])) + + +def _group_specs(pairs: int) -> list[dict]: + """``pairs`` × (one ``add_node``, one three-row ``set_dynamic_group_rows``). + + Each pair is 2 specs and 11 ops — 1 for the node, then the row count plus + three row fields (``lora_name``, ``strength``, ``enabled``) × 3 rows. A + group may only be written once per batch, hence one fresh node per write. + """ + rows = [{"lora_name": "A.safetensors", "strength": 1.0} for _ in range(3)] + specs: list[dict] = [] + for i in range(pairs): + specs.append({"op": "add_node", "class_type": "DevToolsNodeWithDynamicGroup", "as": f"n{i}"}) + specs.append({"op": "set_dynamic_group_rows", "node": f"$n{i}", "group": "loras", "rows": rows}) + return specs + + +# --- the constant is mirrored, not chosen here ------------------------------ + + +def test_the_batch_bound_mirrors_the_shared_op_package_value(): + """``MAX_OPS_PER_BATCH`` is 1024 in ``comfy-multi-player``'s ``src/limits.ts`` + at ``221b2b2e470c79afaface24b1edcb53cdafc8922`` — cited by SHA rather than by + branch, because the whole point is that the two implementations agree. + + The shared package enforces it in two places, both of which refuse the batch + WHOLE rather than truncating or partially applying it: ``applyOps`` checks it + before any op is processed and returns a ``malformed_op`` rejection for every + op in the batch, and ``inspectOps`` raises ``malformed_op`` from the storage + preflight. So comfy-cli cannot buy anything by allowing a longer batch — it + can only move where the batch dies, to after comfy-cli has committed to it. + Widen it there and move this pin; never widen it here alone. + """ + assert ops._MAX_OPS_PER_BATCH == 1024 + + +# --- the boundary: N applies, N+1 is refused -------------------------------- + + +def test_apply_specs_accepts_a_batch_of_exactly_the_limit(graph): + workflow, minted, _aliases = ops.apply_specs({"nodes": [], "links": []}, graph, _add_node_specs(1024)) + assert len(minted) == ops._MAX_OPS_PER_BATCH + assert len(workflow["nodes"]) == ops._MAX_OPS_PER_BATCH + + +def test_apply_specs_refuses_one_op_past_the_limit(graph): + workflow = {"nodes": [], "links": []} + with pytest.raises(ValueError, match=r"^malformed_op: batch of 1025 specs exceeds the 1024-op limit"): + ops.apply_specs(workflow, graph, _add_node_specs(1025)) + # "Rejected before any op was processed", the same promise the shared + # package's message makes: the document the caller handed in is untouched, + # including by the position planner that runs on every spec. + assert workflow == {"nodes": [], "links": []} + + +def test_apply_specs_counts_the_ops_the_specs_expand_into_not_the_specs(graph_with_group): + """One spec is not one op, so the entry check on ``len(specs)`` is only the + cheap half of the bound. + + ``set_dynamic_group_rows`` expands into a row-count write plus one + ``set_widget`` per row field, so 200 specs here mint 1,100 ops: comfortably + past the entry check and comfortably past the bound. Without the second + check this batch applies locally and is handed to a caller, and ``applyOps`` + then refuses every op in it as ``malformed_op`` — the mint-then-refuse shape + this bound exists to close. Red on the parent, which returns 1,100 ops. + """ + with pytest.raises(ValueError, match=r"^malformed_op: batch mints 1100 ops, more than the 1024-op limit") as exc: + ops.apply_specs({"nodes": [], "links": []}, graph_with_group, _group_specs(100)) + # Refused like every other mid-batch failure, so a summary-receipt caller + # reads the same structured position and the same "nothing was written". + assert exc.value.applied_count == 0 + + +def test_a_batch_whose_expansion_stays_within_the_bound_still_applies(graph_with_group): + """The complement of the test above: the second check bounds the expansion, + it does not refuse an expanding batch as such.""" + _workflow, minted, _aliases = ops.apply_specs({"nodes": [], "links": []}, graph_with_group, _group_specs(90)) + assert len(minted) == 990 + + +def test_replace_ops_mints_a_batch_of_exactly_the_limit(): + assert len(ops.replace_ops({"nodes": [], "links": []}, _nodes(1024))) == ops._MAX_OPS_PER_BATCH + + +def test_replace_ops_refuses_to_mint_one_op_past_the_limit(): + """Refused as ``NotExpressibleError``, not as a bare ``ValueError``. + + That is what the two production callers already catch, and the distinction is + behavioural rather than cosmetic: both ``templates fetch --emit-ops`` and + ``generate``'s emit path treat it as non-fatal and report ``ops_skipped`` + beside a file they have still written. A bare ``ValueError`` would escape + both and crash a fetch that had otherwise succeeded. + """ + with pytest.raises(ops.NotExpressibleError, match=r"needs 1025 ops, more than the 1024-op batch limit"): + ops.replace_ops({"nodes": [], "links": []}, _nodes(1025)) + + +def test_the_old_document_counts_toward_the_batch_too(): + """``replace_ops`` emits a ``delete_node`` for every node already in ``old`` + before the adds, so the bound binds on the SUM. Counting only ``new`` would + let a 600-node replacement of a 600-node canvas mint 1,200 ops. + """ + assert len(ops.replace_ops(_nodes(512), _nodes(512))) == ops._MAX_OPS_PER_BATCH + with pytest.raises(ops.NotExpressibleError, match=r"needs 1200 ops"): + ops.replace_ops(_nodes(600), _nodes(600)) + + +# --- the per-op payload bounds, on a batch nothing applies ------------------ + + +def test_replace_ops_refuses_an_op_whose_own_payload_is_out_of_bounds(): + """The batch length is only the OUTER bound. + + ``apply_specs``' ops each go through ``apply_op``, which canonicalizes them + and so enforces §20.3's per-op bounds (depth 64, 4,096 entries, 262,144 + cost). ``replace_ops`` builds its ops and hands them straight to a caller, + so nothing checked those — and ``templates fetch --emit-ops`` presented the + result as the legal translation of the graph while the shared applier + refused it whole as ``malformed_op``. Same mint-then-refuse shape, one level + down. + """ + # A `widgets_values` past the entry cap: copied verbatim into `add_node`. + oversized = { + "nodes": [ + { + "id": 1, + "type": "KSampler", + "widgets_values": [0] * (ops._MAX_OP_COLLECTION_ENTRIES + 1), + "inputs": [], + "outputs": [], + } + ], + "links": [], + } + with pytest.raises(ops.NotExpressibleError, match=r"op #0 \(add_node\) is not a legal op payload: .*entries"): + ops.replace_ops({"nodes": [], "links": []}, oversized) + + # And by cost, from one very long widget string. + costly = { + "nodes": [ + { + "id": 1, + "type": "KSampler", + "widgets_values": ["x" * (ops._MAX_OP_COST + 1)], + "inputs": [], + "outputs": [], + } + ], + "links": [], + } + with pytest.raises(ops.NotExpressibleError, match=r"not a legal op payload: .*cost budget"): + ops.replace_ops({"nodes": [], "links": []}, costly) + + +def test_the_per_op_check_leaves_an_ordinary_batch_alone(): + """It refuses a payload the shared applier would refuse, and nothing else: + every op a real graph mints is orders of magnitude inside the bounds.""" + minted = ops.replace_ops(_nodes(4), _nodes(8)) + assert len(minted) == 12 + assert all(len(ops._op_digest(op)) == 64 for op in minted) + + +# --- where it actually bites, measured rather than assumed ------------------ + + +def test_the_batch_bound_binds_only_far_past_any_real_workflow(): + """Pinned so the next reader does not have to re-derive the headroom before + deciding whether the bound caps a real edit. + + Measured over all 735 workflows in ``Comfy-Org/workflow_templates`` (the + gallery ``templates fetch --emit-ops`` reads), the longest batch + ``replace_ops`` actually mints is **50 ops** — 4.9% of the bound — from + ``templates-1_input-multiple_styles_prompt``. 447 of those templates carry a + subgraph definition, a canvas group or a reroute point, so ``replace_ops`` + refuses them as inexpressible before size is even considered; across all 735 + the absolute upper bound on nodes+links is 212, still a fifth of the bound. + The largest expressible workflow in this repo's own fixtures mints 3 ops. + + What that measurement does NOT cover is the ``old`` side. ``replace_ops`` + emits a ``delete_node`` per node already in the document being replaced, and + that document is user data bounded by nothing here — see + ``test_the_old_document_counts_toward_the_batch_too``. Both production + callers degrade rather than fail on it. + + This test pins the fixture end of the ``new``-side measurement, which is the + half that can regress in CI. + """ + fixture = json.loads((_FIXTURES / "seedream_5_0_pro_t2i_ui.json").read_text(encoding="utf-8")) + minted = ops.replace_ops({"nodes": [], "links": []}, fixture) + assert len(minted) * 100 < ops._MAX_OPS_PER_BATCH, ( + f"the widest real fixture batch is {len(minted)} ops; if it is approaching " + f"{ops._MAX_OPS_PER_BATCH} the bound needs widening in comfy-multi-player first" + ) + + +def test_no_production_surface_builds_a_batch_near_the_bound(graph): + """The one shape in the frozen vocabulary that scales with document size and + is NOT capped by this bound: a standalone ``clear`` or ``reset_doc``, which + carries every node id inside ONE op's payload rather than as one op each. + Those are bounded by ``_MAX_OP_COLLECTION_ENTRIES`` (4,096 entries) instead, + and they are standalone-only — ``BATCHABLE_OPS`` excludes them — so there is + no shape where a large document turns into a long batch behind the caller's + back. + """ + assert "clear" not in ops.BATCHABLE_OPS + assert "reset_doc" not in ops.BATCHABLE_OPS + _workflow, op = ops.clear(_nodes(2048)) + assert len(op["removed_nodes"]) == 2048 diff --git a/tests/comfy_cli/test_op_id_reuse_contract.py b/tests/comfy_cli/test_op_id_reuse_contract.py index b982740a7..8e8c629a5 100644 --- a/tests/comfy_cli/test_op_id_reuse_contract.py +++ b/tests/comfy_cli/test_op_id_reuse_contract.py @@ -110,6 +110,59 @@ def test_numbers_are_rendered_the_way_ecmascript_renders_them(): assert ops._canonical_op({"v": 5e-324}) == '{"v":5e-324}' +def test_an_integer_past_the_exact_range_is_spelled_the_way_js_spells_it(): + """``JSON.parse`` has no integer type: it reads every numeric literal as an + IEEE-754 double, and CMP's canonicalizer refuses a ``BigInt`` payload + outright (``applier.ts``: ``rejectBigIntPayload``). So past 2**53 the other + side holds the ROUNDED value and ``Number::toString`` spells that, while + ``str(value)`` here would spell the exact digits. + + These are the spellings ``JSON.stringify(JSON.parse(literal))`` produces, + taken from a real engine, not from the spec text. + """ + assert ops._canonical_op({"v": 2**53}) == '{"v":9007199254740992}' + assert ops._canonical_op({"v": 2**53 + 1}) == '{"v":9007199254740992}' + # A legal INT-widget maximum: a seed at 0xffffffffffffffff. + assert ops._canonical_op({"v": 18446744073709551615}) == '{"v":18446744073709552000}' + assert ops._canonical_op({"v": -18446744073709551615}) == '{"v":-18446744073709552000}' + # At 1e21 JS switches to exponent form for an integer too. + assert ops._canonical_op({"v": 10**21}) == '{"v":1e+21}' + assert ops._canonical_op({"v": 10**20}) == '{"v":100000000000000000000}' + # Past the double range `JSON.parse` yields Infinity, which stringifies as + # `null` — the same spelling the non-finite float branch uses. + assert ops._canonical_op({"v": 10**400}) == '{"v":null}' + # Inside the exact range nothing changed: the digits are kept. + assert ops._canonical_op({"v": 20}) == '{"v":20}' + assert ops._canonical_op({"v": -(2**53)}) == '{"v":-9007199254740992}' + + +def test_a_max_seed_relayed_through_js_digests_the_same(graph): + """The failure the test above prevents, end to end. Without the rounding an + op carrying ``0xffffffffffffffff`` digests one way here and another after a + JS relay, so the IDENTICAL op echoed back is refused as ``op_id_reuse`` — + exactly what the float spelling was fixed to stop. + """ + workflow = _workflow() + exact = _op(18446744073709551615) + ops.apply_op(workflow, exact, graph) + relayed = _op(18446744073709552000) # what `JSON.parse(JSON.stringify(op))` yields + assert ops._op_digest(relayed) == ops._op_digest(exact) + assert ops.apply_op(workflow, relayed, graph) is workflow + + +def test_an_integer_object_key_past_the_exact_range_coerces_like_a_js_key(): + """A key is coerced with ``String()``, not rendered by ``JSON.stringify``, so + the two differ on the non-finite end: ``String(Infinity)`` is ``"Infinity"`` + where ``JSON.stringify(Infinity)`` is ``null``. Only a programmatically built + op reaches a non-``str`` key at all — parsed JSON always has string keys. + """ + assert ops._canonical_op({"v": {18446744073709551615: 1}}) == '{"v":{"18446744073709552000":1}}' + assert ops._canonical_op({"v": {10**21: 1}}) == '{"v":{"1e+21":1}}' + assert ops._canonical_op({"v": {10**400: 1}}) == '{"v":{"Infinity":1}}' + assert ops._canonical_op({"v": {-(10**400): 1}}) == '{"v":{"-Infinity":1}}' + assert ops._canonical_op({"v": {7: 1}}) == '{"v":{"7":1}}' + + def test_object_keys_sort_by_code_point_not_by_utf16_code_unit(): """§8.1's caveat applies to the key sort too: for ASCII, JS UTF-16 ``<`` agrees with code-point order, but above the Basic Multilingual Plane it does @@ -121,6 +174,33 @@ def test_object_keys_sort_by_code_point_not_by_utf16_code_unit(): assert ops._canonical_op({"\U0001f600": 1, "\uff00": 2}) == '{"\uff00":2,"\U0001f600":1}' +@pytest.mark.parametrize( + ("payload", "name"), + [ + ({1: "a", "1": "b"}, "1"), + ({True: "a", "true": "b"}, "true"), + ({None: "a", "null": "b"}, "null"), + ({1.5: "a", "1.5": "b"}, "1.5"), + ], +) +def test_keys_that_collide_after_coercion_are_refused(payload, name): + """Coercion is not injective, and a canonical form that is not well defined + is not canonical. + + Emitting both members produces an object literal no JS object can hold, and + because the member sort is stable the digest of such a dict would depend on + INSERTION order — so the same logical payload could digest two ways and an + identical redelivery could be read as reuse. Refused rather than merged: + there is no intent here a merge would preserve. Only a programmatically + built op can reach it, since parsed JSON always has ``str`` keys. + """ + with pytest.raises(ValueError, match=rf"^malformed_op: object keys .* both canonicalize to {name!r}"): + ops._canonical_op({"v": payload}) + + # The ordinary case is untouched: a `str` key that merely LOOKS numeric. + assert ops._canonical_op({"v": {name: "b"}}) == f'{{"v":{{"{name}":"b"}}}}' + + def test_a_float_widget_relayed_through_js_digests_the_same(graph): """A FLOAT op whose value is integral comes back from a JS relay as an int. Both spellings have to digest identically, or the identical redelivery is @@ -168,7 +248,8 @@ def test_the_bounds_mirror_the_shared_op_package_values(): ``comfy-multi-player``'s ``src/limits.ts`` at ``221b2b2e470c79afaface24b1edcb53cdafc8922`` fixes ``MAX_PAYLOAD_DEPTH`` at - 64, ``MAX_COLLECTION_ENTRIES`` at 4096 and ``MAX_OP_COST`` at 262144, and + 64, ``MAX_COLLECTION_ENTRIES`` at 4096, ``MAX_OP_COST`` at 262144 and + ``MAX_OPS_PER_BATCH`` at 1024, and its A8 canonicalizer charges the whole envelope on EVERY apply, before the dedupe gate (``applyOps`` -> ``opDigest`` -> ``canonicalJson`` -> ``opBoundsRefusal``). So a looser bound here cannot rescue an oversized @@ -179,6 +260,12 @@ def test_the_bounds_mirror_the_shared_op_package_values(): assert ops._MAX_OP_PAYLOAD_DEPTH == 64 assert ops._MAX_OP_COLLECTION_ENTRIES == 4096 assert ops._MAX_OP_COST == 262_144 + # The fourth bound in the same `src/limits.ts` block bounds the BATCH rather + # than one op's payload, so it is enforced at `apply_specs`/`replace_ops` + # instead of in the canonicalizer and is pinned, with its boundary cases, in + # `test_max_ops_per_batch.py`. Asserted here too so that reading this list + # cannot suggest only three of the four are mirrored. + assert ops._MAX_OPS_PER_BATCH == 1024 def test_ordinary_large_payloads_are_not_rejected_by_the_canonicalization_bounds(): @@ -219,6 +306,31 @@ def test_the_shared_bounds_bind_only_past_any_observed_workflow(): ops._op_digest({"value": "x" * 131_065, "old": "y" * 131_065}) +def test_an_oversized_string_is_refused_before_it_is_rendered(monkeypatch): + """The cost bound exists to cap the WORK a hostile payload costs, so it has + to refuse at the charge rather than after the render. + + ``_canonical_string`` runs ``json.dumps`` plus a surrogate scan over the + whole string and allocates up to ~6x its size for control-heavy input, so + checking the budget only at the end of the call meant the oversized value + was fully rendered first. The shared package charges before it renders for + the same reason (``opBoundsRefusal`` walks the payload ahead of + ``normalize``). The refusal itself is unchanged — the budget only grows. + """ + rendered: list[str] = [] + real = ops._canonical_string + monkeypatch.setattr(ops, "_canonical_string", lambda value: (rendered.append(value), real(value))[1]) + oversized = "x" * (ops._MAX_OP_COST + 1) + + with pytest.raises(ValueError, match=f"^malformed_op: payload exceeds the {ops._MAX_OP_COST}-unit cost budget"): + ops._canonical_op({"v": oversized}) + + assert oversized not in rendered + # Charged per key too, so a dict of oversized keys cannot slip past either. + with pytest.raises(ValueError, match="cost budget"): + ops._canonical_op({"k" * (ops._MAX_OP_COST + 1): 1}) + + def test_canonicalization_still_refuses_a_runaway_payload(): nested: Any = "leaf" for _ in range(ops._MAX_OP_PAYLOAD_DEPTH + 2): @@ -238,6 +350,30 @@ def test_canonicalization_still_refuses_a_runaway_payload(): # --- Amendment v1.10: rejecting before mutation ----------------------------- +@pytest.mark.parametrize( + ("mutate", "exc"), + [ + (lambda op: op.pop("op"), KeyError), + (lambda op: op.update(definitions={"x": {}}), ValueError), + ], + ids=["no-op-key", "definitions-on-a-non-insert-op"], +) +def test_a_rejected_op_leaves_no_empty_ledger_behind_either(graph, mutate, exc): + """Both of these validations raise OUTSIDE ``apply_op``'s rollback, so when + they ran after ``setdefault("_applied_ops", [])`` a rejected op left an empty + ledger on a document that arrived without one — contradicting the §20.2 + guarantee the amendment states. Red on the parent, which comes back carrying + ``{"_applied_ops": []}``. + """ + workflow = _workflow() + before = copy.deepcopy(workflow) + bad = _op() + mutate(bad) + with pytest.raises(exc): + ops.apply_op(workflow, bad, graph) + assert workflow == before + + def test_a_rejected_first_seen_op_leaves_no_ledger_behind(graph): """ "Rejects before mutation" has to include the ledger itself: a document that arrived without ``_applied_ops`` must not come back carrying an empty @@ -286,6 +422,54 @@ def test_a_truthy_non_dict_ledger_is_refused_on_the_reuse_branch_too(graph): ops.apply_op(workflow, applied, graph) +def test_a_null_digest_entry_is_not_read_as_a_legacy_ledger(graph): + """``recorded is None`` could not tell a MISSING digest apart from an + explicit ``null`` one, and the up-front check validates the ledger's type + rather than its values. + + So a document carrying ``{: null}`` read as "legacy list-only ledger" + and returned a changed-payload redelivery as a silent no-op — the exact + divergence Amendment v1.11 exists to stop. Every other non-string value + failed closed on the digest comparison, so ``null`` was the one shape that + failed OPEN. Red on the parent, which returns the workflow unchanged. + """ + workflow = _workflow() + ops.apply_op(workflow, _op(25), graph) + workflow["_applied_op_digests"][_op()["op_id"]] = None + before = copy.deepcopy(workflow) + + with pytest.raises(ValueError, match=r"^malformed_op: _applied_op_digests\[.*\] must be a string digest"): + ops.apply_op(workflow, _op(30), graph) + assert workflow == before + + +@pytest.mark.parametrize("recorded", [None, 7, ["digest"], {"d": 1}, True]) +def test_a_non_string_digest_entry_is_malformed_not_a_mismatch(graph, recorded): + """``malformed_op``, not ``op_id_reuse``: the document's bookkeeping is + broken, which is not the same finding as a sender reusing an id. An + IDENTICAL redelivery is refused too — there is no recorded digest to + recognise it by. + """ + workflow = _workflow() + applied = _op(25) + ops.apply_op(workflow, applied, graph) + workflow["_applied_op_digests"][applied["op_id"]] = recorded + with pytest.raises(ValueError, match="^malformed_op: _applied_op_digests"): + ops.apply_op(workflow, applied, graph) + + +def test_a_legacy_list_only_ledger_is_still_a_silent_no_op(graph): + """The complement: a document with ``_applied_ops`` and NO digest entry + keeps its historical drop-the-duplicate behaviour, and keeps it without + digesting the replayed payload first.""" + workflow = _workflow() + applied = _op(25) + ops.apply_op(workflow, applied, graph) + del workflow["_applied_op_digests"][applied["op_id"]] + assert ops.apply_op(workflow, _op(30), graph) is workflow + assert workflow["nodes"][0]["widgets_values"] == [25] + + @pytest.mark.parametrize("op_id", [["a"], {"a": 1}, 7, None]) def test_a_non_string_op_id_is_refused_before_the_handler_runs(graph, op_id): workflow = _workflow()