Docs / docs (push) Successful in 30s
Playwright Tests / test-playwright (1, 2) (push) Successful in 3m7s
Playwright Tests / test-playwright (2, 2) (push) Successful in 1m54s
pre-commit / pre-commit (push) Failing after 4m24s
Test Backend / test-backend (push) Successful in 3m8s
Compose Smoke Test / test-compose (push) Successful in 40s
Playwright Tests / merge-reports (push) Successful in 1m33s
A port may now declare `image`, `audio` or `video`. Each is the artifact
reference the engine already had, narrowed by the `media_type` on it, so a
speech recogniser declares what it eats rather than taking any bytes at all and
finding out. Bytes still never travel as a message and nothing on the wire
stops being JSON: a camera publishes one reference per frame, a microphone one
per chunk, and a reference may carry a `meta` dict nothing here interprets.
Streaming media is therefore an ordinary streaming port — with one change to
what that means. An emission used to journal an item with no payload, so
downstream read whatever was current when the item was claimed; a consumer
slower than its producer saw only the newest chunk and the ones between were
lost. That is right for a training curve and wrong for a second of speech, so
an emission now journals a `kind="emission"` item carrying its values, and the
executor hands them to the nodes reading that message instead of writing them
to state again. The value in state stays the latest, which is what everything
else reads, and the wave is filtered by what actually changed rather than
walking everything reachable. No queue serialization change — the existing
`outputs` field carries it.
Continuous media makes the store's missing GC a real problem, so this closes
it: `sweep_artifacts` runs hourly, keeps every digest a `run_artifact` row
records or a live message holds, spares anything written in the last hour, and
stands aside entirely while a run is in flight, since a node may store a
checkpoint long before it returns the reference to it. That also collects the
orphans a deleted flow has always left behind. `ARTIFACT_GC_INTERVAL_S=0` turns
it off.
Around the edges: `GET /artifacts/{digest}` serves the media type the caller
passes and answers ranged requests, so a browser plays a clip rather than
downloading it; `PUT` spools to disk instead of holding the whole body in
memory, as does `save_artifact` given a path; a Media widget draws whatever its
message points at, and a wall panel may fetch the bytes its own tiles are
showing and nothing else; and a connector gets `save_artifact`, for a device
whose readings are bytes.
What this cannot do is live video: a frame every second or two is a glance, and
the honest answer above that is the camera's own stream, which the widget takes
as a URL and the browser plays from source.
Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
227 lines
6.6 KiB
Python
227 lines
6.6 KiB
Python
"""The connector contract: polling, deduplication, health and discovery."""
|
|
|
|
import asyncio
|
|
from types import SimpleNamespace
|
|
from typing import Any
|
|
|
|
import pytest
|
|
|
|
from fluksio.flow.artifacts import ArtifactStore
|
|
from fluksio.flow.connector import CONTRACT_VERSION, ConnectorNode
|
|
from fluksio.flow.controller import NODE_TYPES
|
|
from fluksio.flow.messages import DType, MessageSpec
|
|
from fluksio.flow.nodes import Node
|
|
from fluksio.flow.pipeline import Pipeline
|
|
from fluksio.flow.plugins import load_plugins
|
|
|
|
|
|
class Sensor(ConnectorNode):
|
|
contract = CONTRACT_VERSION
|
|
title = "Test sensor"
|
|
description = "Reads whatever it is told to."
|
|
|
|
class Params(ConnectorNode.Params):
|
|
secret_token: str | None = None
|
|
|
|
def __init__(self, readings: list[Any] | None = None, **kwargs: Any) -> None:
|
|
super().__init__(**kwargs)
|
|
self._readings = list(readings or [])
|
|
self.polls = 0
|
|
|
|
async def poll(self) -> dict[str, Any] | None:
|
|
self.polls += 1
|
|
if not self._readings:
|
|
return None
|
|
value = self._readings.pop(0)
|
|
if isinstance(value, Exception):
|
|
raise value
|
|
return {"reading": value}
|
|
|
|
|
|
def a_sensor(readings: list[Any], **params: Any) -> Sensor:
|
|
node = Sensor(
|
|
readings=readings,
|
|
provides=[MessageSpec(name="reading", dtype=DType.FLOAT)],
|
|
params={"poll_interval": 0.01, **params},
|
|
)
|
|
node.assign_flow("demo", "sensor")
|
|
return node
|
|
|
|
|
|
def run_briefly(node: ConnectorNode, seconds: float = 0.12, app: Any = None) -> None:
|
|
"""Start the poll loop, let it tick a few times, stop it."""
|
|
|
|
async def cycle() -> None:
|
|
await node.start(app)
|
|
await asyncio.sleep(seconds)
|
|
await node.stop()
|
|
|
|
asyncio.run(cycle())
|
|
|
|
|
|
def test_polling_publishes_what_it_reads():
|
|
node = a_sensor([21.5])
|
|
pipeline = Pipeline(nodes=[node])
|
|
|
|
run_briefly(node)
|
|
|
|
assert pipeline.state["demo.reading"] == 21.5
|
|
|
|
|
|
def test_an_unchanged_reading_is_not_republished():
|
|
node = a_sensor([21.5, 21.5, 21.5])
|
|
consumer_ran: list[float] = []
|
|
|
|
def consume(reading, params):
|
|
consumer_ran.append(reading)
|
|
return None
|
|
|
|
consumer = Node(
|
|
f=consume,
|
|
requires=[MessageSpec(name="reading", dtype=DType.FLOAT)],
|
|
name="consumer",
|
|
)
|
|
consumer.assign_flow("demo", "consumer")
|
|
Pipeline(nodes=[node, consumer])
|
|
|
|
run_briefly(node)
|
|
|
|
# Polled repeatedly, but the value never changed, so downstream ran once.
|
|
assert node.polls > 1
|
|
assert consumer_ran == [21.5]
|
|
|
|
|
|
def test_a_failing_poll_reports_down_and_keeps_going():
|
|
health: list[tuple[str, str | None]] = []
|
|
node = a_sensor([RuntimeError("device unplugged"), 21.5])
|
|
node._on_health = lambda _node, status, detail: health.append((status, detail))
|
|
Pipeline(nodes=[node])
|
|
|
|
run_briefly(node)
|
|
|
|
assert ("down", "RuntimeError: device unplugged") in health
|
|
# It recovered rather than giving up.
|
|
assert health[-1][0] == "ok"
|
|
|
|
|
|
class Actuator(ConnectorNode):
|
|
"""A connector that commands something instead of reading it."""
|
|
|
|
contract = CONTRACT_VERSION
|
|
title = "Test actuator"
|
|
|
|
def __init__(self, **kwargs: Any) -> None:
|
|
super().__init__(**kwargs)
|
|
self.commands: list[dict[str, Any]] = []
|
|
|
|
def write(self, **ports: Any) -> None:
|
|
self.commands.append(ports)
|
|
return None
|
|
|
|
|
|
def test_an_incoming_message_reaches_a_connector_that_writes():
|
|
node = Actuator(requires=[MessageSpec(name="level", dtype=DType.INT)])
|
|
node.assign_flow("demo", "actuator")
|
|
|
|
assert node.execute({"demo.level": 255}) is None
|
|
assert node.commands == [{"level": 255}]
|
|
|
|
|
|
def test_a_read_only_connector_ignores_what_reaches_it():
|
|
node = a_sensor([])
|
|
assert node.execute({}) is None
|
|
|
|
|
|
def test_a_credential_param_is_marked_for_the_editor():
|
|
schema = Sensor.Params.model_json_schema()
|
|
assert schema["properties"]["poll_interval"]["default"] == 0
|
|
assert "secret_token" in schema["properties"]
|
|
|
|
|
|
def test_a_connector_is_discovered_from_its_entry_point(monkeypatch):
|
|
class FakeDist:
|
|
name = "fluksio-connector-test"
|
|
version = "0.1.0"
|
|
|
|
class FakeEntry:
|
|
name = "test_sensor"
|
|
dist = FakeDist()
|
|
|
|
def load(self):
|
|
return Sensor
|
|
|
|
monkeypatch.setattr(
|
|
"fluksio.flow.plugins.entry_points", lambda group: [FakeEntry()]
|
|
)
|
|
try:
|
|
assert load_plugins() == ["test_sensor"]
|
|
assert NODE_TYPES["test_sensor"].plugin == "fluksio-connector-test 0.1.0"
|
|
assert NODE_TYPES["test_sensor"].title == "Test sensor"
|
|
finally:
|
|
NODE_TYPES.pop("test_sensor", None)
|
|
|
|
|
|
def test_a_connector_written_for_another_contract_is_refused(monkeypatch):
|
|
class Outdated(ConnectorNode):
|
|
contract = CONTRACT_VERSION + 1
|
|
|
|
class FakeEntry:
|
|
name = "outdated"
|
|
dist = None
|
|
|
|
def load(self):
|
|
return Outdated
|
|
|
|
monkeypatch.setattr(
|
|
"fluksio.flow.plugins.entry_points", lambda group: [FakeEntry()]
|
|
)
|
|
assert load_plugins() == []
|
|
assert "outdated" not in NODE_TYPES
|
|
|
|
|
|
def test_a_connector_may_not_take_over_a_built_in_type(monkeypatch):
|
|
class FakeEntry:
|
|
name = "mqtt"
|
|
dist = None
|
|
|
|
def load(self): # pragma: no cover - never reached
|
|
raise AssertionError("should not be loaded")
|
|
|
|
monkeypatch.setattr(
|
|
"fluksio.flow.plugins.entry_points", lambda group: [FakeEntry()]
|
|
)
|
|
assert load_plugins() == []
|
|
assert NODE_TYPES["mqtt"].plugin is None
|
|
|
|
|
|
def test_a_connector_publishes_bytes_as_a_media_reference(tmp_path):
|
|
"""A camera's reading is bytes, and bytes never travel as a message."""
|
|
|
|
class Camera(ConnectorNode):
|
|
contract = CONTRACT_VERSION
|
|
|
|
async def poll(self) -> dict[str, Any] | None:
|
|
return {"frame": self.save_artifact(b"\x89PNG...", "f.png", "image/png")}
|
|
|
|
node = Camera(
|
|
provides=[MessageSpec(name="frame", dtype=DType.IMAGE)],
|
|
params={"poll_interval": 0.01},
|
|
)
|
|
node.assign_flow("demo", "camera")
|
|
pipeline = Pipeline(nodes=[node])
|
|
|
|
# Before it starts there is no store to write to, and saying so beats an
|
|
# AttributeError from inside somebody's connector.
|
|
with pytest.raises(RuntimeError, match="artifact store"):
|
|
node.save_artifact(b"x")
|
|
|
|
store = ArtifactStore(tmp_path / "artifacts")
|
|
app = SimpleNamespace(state=SimpleNamespace(artifact_store=store))
|
|
run_briefly(node, app=app)
|
|
|
|
reference = pipeline.state["demo.frame"]
|
|
# It typechecks against the port it was published on, which is the whole
|
|
# point of a media dtype.
|
|
MessageSpec(name="demo.frame", dtype=DType.IMAGE).check(reference)
|
|
assert store.path(reference["digest"]).read_bytes() == b"\x89PNG..."
|