@@ -1511,6 +1511,7 @@ def tensor_contract(value: Any) -> dict[str, Any]:
15111511 declaration["application_overridable"] = True
15121512 components[name] = declaration
15131513 declare_request_alignment(workflow)
1514+ declare_input_admission(workflow)
15141515 return metadata
15151516
15161517
@@ -1554,6 +1555,157 @@ def stamp(contract: Any) -> None:
15541555 declaration.setdefault("release_boundary", declaration.get("scope", "invocation"))
15551556
15561557
1558+ def published_value_references(workflow: dict[str, Any]) -> set[str]:
1559+ """Every value name the published program reads, on any path.
1560+
1561+ Reachability here is deliberately path-insensitive: it answers "does this
1562+ workflow ever look at this value", which is what an admission decision
1563+ needs. Whether a particular request reaches the branch that reads it is a
1564+ runtime fact, and a package that guessed at it would be describing one
1565+ caller rather than its own contract.
1566+ """
1567+ references: set[str] = set()
1568+
1569+ def note(value: Any) -> None:
1570+ if isinstance(value, str):
1571+ references.add(value)
1572+ elif isinstance(value, dict):
1573+ for item in value.values():
1574+ note(item)
1575+ elif isinstance(value, list):
1576+ for item in value:
1577+ note(item)
1578+
1579+ def visit(step: Any) -> None:
1580+ if isinstance(step, list):
1581+ for item in step:
1582+ visit(item)
1583+ return
1584+ if not isinstance(step, dict):
1585+ return
1586+ kind = step.get("kind")
1587+ if kind == "invoke":
1588+ note(step.get("inputs"))
1589+ elif kind == "emit":
1590+ note(step.get("value"))
1591+ note(step.get("valid_length"))
1592+ note(step.get("when"))
1593+ elif kind == "branch":
1594+ note(step.get("predicate"))
1595+ note(step.get("outputs"))
1596+ for case in (step.get("cases") or {}).values():
1597+ visit(case)
1598+ visit(step.get("default"))
1599+ elif kind == "loop":
1600+ note(step.get("continue_when"))
1601+ note(step.get("max_iterations"))
1602+ for carry in step.get("carried") or []:
1603+ note(carry.get("next"))
1604+ note(carry.get("initial"))
1605+ for key in ("steps", "setup", "nodes"):
1606+ visit(step.get(key))
1607+
1608+ visit(workflow.get("steps") or [])
1609+ for declaration in (workflow.get("state") or {}).values():
1610+ if isinstance(declaration, dict):
1611+ note(declaration.get("initializer"))
1612+ # A bounded or growing cell reads its own extent every step, so the
1613+ # bound and the step size are read positions like any other. The
1614+ # sibling ``kind``/``axis`` keys describe the recurrence rather than
1615+ # naming values, so they are not references.
1616+ recurrence = declaration.get("recurrence") or {}
1617+ note(recurrence.get("max"))
1618+ note(recurrence.get("increment"))
1619+ # The serving block names the workflow values a runtime reads to drive
1620+ # batching -- the active/done row masks and the accepted length.
1621+ serving = workflow.get("serving") or {}
1622+ for key, value in serving.items():
1623+ if key != "state_service":
1624+ note(value)
1625+ # ``state_service`` is mostly port and cell names rather than values, with
1626+ # one exception: a group's fixed update extent is itself a workflow value.
1627+ for group in ((serving.get("state_service") or {}).get("groups") or {}).values():
1628+ if isinstance(group, dict):
1629+ note((group.get("update") or {}).get("capacity"))
1630+ return references
1631+
1632+
1633+ def declare_input_admission(workflow: dict[str, Any]) -> None:
1634+ """Publish every package input's admission requirement instead of implying it.
1635+
1636+ ``required`` is what a runtime admits a request against: an input it holds
1637+ required and the caller did not attach is a rejected request, on every path,
1638+ before a single component runs. A consumer cannot see what an *absent*
1639+ ``required`` key was meant to say, so it has to choose a default, and the
1640+ choice it makes is the opposite of what omission means to a producer -- a
1641+ value the workflow computes for itself, defaults for itself, or explicitly
1642+ branches on the absence of, silently becomes a mandatory caller attachment.
1643+
1644+ So the flag is derived from the published program rather than left to a
1645+ reader, and stamped on every declaration:
1646+
1647+ * a declaration the package can satisfy on its own -- it carries a
1648+ ``default``, which is also the only thing a package-owned ``literal``
1649+ source is ever bound from -- is not something a caller can be required to
1650+ send;
1651+ * a declaration whose absence the program *observes*, through a
1652+ ``present_as`` symbol the steps actually branch on, is one the workflow
1653+ has been written to run without;
1654+ * anything else is genuinely externally required, and says so out loud.
1655+
1656+ A builder that declares both an escape and ``required: True`` has written
1657+ two contradictory contracts, and there is no reading of the package that
1658+ satisfies both, so this fails closed rather than picking one. The mirror
1659+ case fails closed for the same reason: an input marked optional that the
1660+ package has no way to proceed without is not optional, it is a request that
1661+ is admitted and then fails part-way through on an unbound value, which is
1662+ strictly worse than the rejection it replaced.
1663+ """
1664+ inputs = workflow.get("inputs") or {}
1665+ if not inputs:
1666+ return
1667+ references = published_value_references(workflow)
1668+ for name, declaration in inputs.items():
1669+ if not isinstance(declaration, dict):
1670+ continue
1671+ escapes = []
1672+ literal = (declaration.get("source") or {}).get("kind") == "literal"
1673+ if "default" in declaration:
1674+ escapes.append("a package-supplied default" if literal else "a default")
1675+ elif literal:
1676+ # A literal source is resolved from the declaration's own default
1677+ # and from nothing else, so one without a default names a value the
1678+ # package neither holds nor can ask a caller for.
1679+ raise ValueError(
1680+ f"workflow input {name!r} is sourced from a package literal but "
1681+ "carries no default, so nothing ever binds it"
1682+ )
1683+ present_as = declaration.get("present_as")
1684+ if present_as is not None:
1685+ if present_as not in references:
1686+ raise ValueError(
1687+ f"workflow input {name!r} declares the presence symbol "
1688+ f"{present_as!r} that no step reads, so the workflow never "
1689+ "handles the input being absent"
1690+ )
1691+ escapes.append(f"the presence gate {present_as!r}")
1692+ if not escapes:
1693+ if declaration.get("required", True) is False:
1694+ raise ValueError(
1695+ f"workflow input {name!r} is declared optional but the workflow "
1696+ "carries no default and no presence gate for it, so a request "
1697+ "that omits it has no defined behaviour"
1698+ )
1699+ declaration["required"] = True
1700+ continue
1701+ if declaration.get("required", False):
1702+ raise ValueError(
1703+ f"workflow input {name!r} is declared required but the workflow "
1704+ f"already proceeds without it through {', '.join(escapes)}"
1705+ )
1706+ declaration["required"] = False
1707+
1708+
15571709def add_adapter_service_to_metadata(
15581710 metadata: dict[str, Any],
15591711 pkg: Any,
@@ -1587,6 +1739,9 @@ def compatible_input(
15871739 contract.get("dtype") == dtype
15881740 and contract.get("rank") == len(shape)
15891741 and contract.get("shape") == shape
1742+ # A selection tensor the service reads for every batch it plans has
1743+ # no package-side escape, so admission derivation stamps it required
1744+ # and this stays a required-input check either way.
15901745 and declaration.get("required", True)
15911746 and declaration.get("source", {}).get("kind") in {"request", "application"}
15921747 and (
@@ -1612,6 +1767,10 @@ def ensure_input(
16121767 else {"kind": "opaque"}
16131768 ),
16141769 "source": source or {"kind": "request"},
1770+ # A selection tensor has no package-side default and no
1771+ # presence gate: the service reads it for every batch it plans,
1772+ # so the caller supplies it or the request is not admissible.
1773+ "required": True,
16151774 }
16161775 if not compatible_input(name, dtype=dtype, shape=shape, role=role):
16171776 raise ValueError(
@@ -1684,6 +1843,7 @@ def ensure_input(
16841843 # inside `ensure_input` also covers the declarations a producer wrote
16851844 # by hand before attaching the adapter service.
16861845 declare_request_alignment(workflow)
1846+ declare_input_admission(workflow)
16871847 return metadata
16881848
16891849
0 commit comments