diff --git a/.gitea/workflows/pre-commit.yml b/.gitea/workflows/pre-commit.yml index bef3351..a87007a 100644 --- a/.gitea/workflows/pre-commit.yml +++ b/.gitea/workflows/pre-commit.yml @@ -43,9 +43,11 @@ jobs: # settings, which read ../.env from backend/. run: cp .env.example .env # The hooks call `uv run ruff` from the repo root, so the workspace .venv - # (pyproject.toml → [tool.uv.workspace]) is what has to exist. + # (pyproject.toml → [tool.uv.workspace]) is what has to exist. The extras + # too, as `make lint-backend` does: strict mypy below checks call sites + # into what only the `server` extra installs. - name: Install backend dependencies - run: uv sync + run: uv sync --all-extras - name: Install frontend dependencies run: bun install --frozen-lockfile # The formatting hooks rewrite files; pre-commit exits non-zero when they diff --git a/.gitea/workflows/test-backend.yml b/.gitea/workflows/test-backend.yml index 739251c..55854a9 100644 --- a/.gitea/workflows/test-backend.yml +++ b/.gitea/workflows/test-backend.yml @@ -16,29 +16,26 @@ concurrency: group: ${{ github.workflow }}-${{ github.ref }} cancel-in-progress: true -env: - # The compose files live in docker/, so a bare `docker compose` from the repo - # root finds nothing. With COMPOSE_FILE set, compose reads .env from the - # working directory — the same file the backend settings load as ../.env. - # compose.ci.yml drops the fixed container_names and the fixed 5432 — both - # daemon-global, so without it a second run on the same host cannot start. - COMPOSE_FILE: docker/compose.yml:docker/compose.dev.yml:docker/compose.ci.yml - jobs: test-backend: runs-on: ubuntu-latest + # The floor `requires-python` declares and the newest release, so neither + # end of the supported range goes untested. + strategy: + fail-fast: false + matrix: + python-version: ["3.12", "3.14"] env: - # Per run, so the `down -v` steps reach neither a dev stack nor a run - # still going. Unique names cannot be recycled, hence the always-on - # teardown below. - COMPOSE_PROJECT_NAME: fluksio-app-ci-backend-${{ github.run_id }} + # uv would otherwise take a managed interpreter it already has over the + # one set up below. + UV_PYTHON: ${{ matrix.python-version }} steps: - name: Checkout uses: actions/checkout@v4 - name: Set up Python uses: actions/setup-python@v5 with: - python-version: "3.13" + python-version: ${{ matrix.python-version }} - name: Set up uv uses: astral-sh/setup-uv@v7 with: @@ -46,7 +43,9 @@ jobs: pyproject.toml backend/pyproject.toml uv.lock - - run: uv sync + # The extras too, as `make test-backend` does: the mail, MQTT and Influx + # tests import what only the `server` extra installs. + - run: uv sync --all-extras # Nothing to start: the suite names its own SQLite file in a temp # directory (backend/tests/__init__.py), the mail paths are patched, and # the flow engine falls back to in-memory state without a Redis host. diff --git a/DESIGN.md b/DESIGN.md index 19f4551..706c2c5 100644 --- a/DESIGN.md +++ b/DESIGN.md @@ -8,7 +8,7 @@ any UI work. ## Sync rule (read before touching tokens) -The `@theme inline`, `:root`, and `.dark` blocks in `app/frontend/src/index.css` +The `@theme inline`, `:root`, and `.theme-dark` blocks in `app/frontend/src/index.css` and `index/src/index.css` **must be kept byte-identical**, and `frontend/src/lib/motion.ts` is duplicated verbatim. Any palette / radius / elevation / motion-token change is made in *both* repos in the same change. diff --git a/Makefile b/Makefile index e8b9a77..4477741 100644 --- a/Makefile +++ b/Makefile @@ -2,9 +2,9 @@ # Convenience targets for development, testing, linting, and deployment. # The workspace root delegates to these (see ../Makefile). -.PHONY: init dev-utils dev dev-local dev-lan rebuild-frontend up down update install dev-backend dev-frontend \ - generate-client sync-example test test-backend test-checks test-frontend soak bench-startup lint lint-backend \ - lint-frontend format-frontend umami build docs docs-serve clean help +.PHONY: init env-check dev-utils dev dev-local dev-lan rebuild-frontend up down update install dev-backend dev-frontend \ + generate-client sync-example test test-backend test-checks test-stack test-frontend test-webkit soak \ + bench-startup bench-engine lint lint-backend lint-frontend format-frontend umami build docs docs-serve clean help COMPOSE_ROOT := $(CURDIR) # Explicit project name keeps this stack isolated from the sibling website @@ -25,6 +25,13 @@ COMPOSE_LOCAL := $(COMPOSE_DEV) -f docker/compose.local.yml # Same stack, with the frontend also published on APP_PORT and proxying the API # there, for clients that cannot resolve app.$(DOMAIN). COMPOSE_LAN := $(COMPOSE_LOCAL) -f docker/compose.lan.yml +# The frontend suite's own stack, a project of its own behind the local Traefik +# (compose.test.yml). The hostname is fixed, never read from a file or the +# command line, so the suite can only ever reach this stack. +TEST_DOMAIN := test.localhost +COMPOSE_TEST := DOMAIN=$(TEST_DOMAIN) STACK_NAME=fluksio-test TAG=test \ + docker compose -p fluksio-test --env-file $(COMPOSE_ROOT)/.env -f docker/compose.yml \ + -f docker/compose.dev.yml -f docker/compose.local.yml -f docker/compose.test.yml help: ## Show available targets @awk 'BEGIN{FS=":.*?## "} /^[a-zA-Z_-]+:.*?##/ {printf " \033[36m%-18s\033[0m %s\n", $$1, $$2}' $(MAKEFILE_LIST) @@ -39,6 +46,17 @@ help: ## Show available targets # `make dev DOMAIN=fluksio.com` still wins. dev dev-utils dev-local dev-lan rebuild-frontend: export DOMAIN ?= localhost +# A key the template lists and the compose files default, missing from .env, +# falls back without a word. For TZ that means every cron runs on UTC. Only a +# warning, so a start never fails over it. `update` runs it after its pull. +dev dev-local dev-lan up: env-check +env-check: + @missing=$$(grep -ohE '\$$\{[A-Z_]+:?-' docker/compose*.yml | sed 's/^..//; s/:\{0,1\}-$$//' | sort -u \ + | while read -r k; do \ + grep -q "^$$k=" .env.example && ! grep -q "^$$k=" .env && printf ' %s' "$$k"; \ + done); \ + [ -z "$$missing" ] || echo " ! .env lacks$$missing, so compose uses its defaults. See .env.example." + # First-time bootstrap. Idempotent: only secrets still set to `changethis` are # generated, so an existing .env keeps its values. init: ## First-time setup (.env + secrets + docker network) @@ -123,12 +141,14 @@ umami: ## Provision + start the optional Umami site-analytics service (idempote update: ## Pull, rebuild using the layer cache, and recreate changed containers git pull + @$(MAKE) --no-print-directory env-check $(COMPOSE_PROD_RUN) build $(COMPOSE_PROD_RUN) up -d --remove-orphans docker image prune -f down: ## Stop all running containers -$(COMPOSE_LOCAL) down + -$(COMPOSE_TEST) down -$(COMPOSE_PROD_RUN) down # ── Development (local, no Docker) ─────────────────────────────── @@ -172,34 +192,49 @@ test-backend: ## Run backend tests (pytest + coverage) PW_VERSION = $(shell sed -n 's/.*"@playwright\/test": "[^0-9]*\([0-9.]*\)".*/\1/p' frontend/package.json | head -1) -# This suite creates and deletes flows, dashboards and users, so the hostname it -# is pointed at is not taken from any file: it is read off the running stack -# (the frontend container's own Traefik rule) and mapped onto the local Traefik -# by address, so DNS never decides. tests/guard.ts is the second line. -test-frontend: ## Run frontend tests (Playwright e2e) against the local stack +# The suite creates and deletes flows, dashboards and users, so it runs against +# a stack of its own, never the dev instance. `up` only recreates what changed, +# and the stack is left running for the next run (and for a look at a failure +# on http://app.test.localhost). `make down` stops it. Its volumes are +# expendable: `docker compose -p fluksio-test down -v` starts it over. +test-stack: ## Start or update the frontend suite's own stack at http://app.test.localhost + @docker ps -q -f name='^fluksio-app-proxy-1$$' | grep -q . \ + || { echo " ✗ the local Traefik is not running, 'make dev' first"; exit 1; } + $(COMPOSE_TEST) up --build -d --wait redis backend frontend + +# The Playwright container, minus its network. tests/guard.ts refuses a public +# address on top of the fixed hostname. +PW_RUN = docker run --rm --ipc=host --user $$(id -u):$$(id -g) -e HOME=/tmp \ + -v $(COMPOSE_ROOT):/app -w /app/frontend \ + -e PLAYWRIGHT_BASE_URL=http://app.$(TEST_DOMAIN) \ + -e PLAYWRIGHT_API_URL=http://api.$(TEST_DOMAIN) \ + -e CI=$${CI:-1} \ + -e FIRST_SUPERUSER="$$(sed -n 's/^FIRST_SUPERUSER=//p' $(COMPOSE_ROOT)/.env | head -1)" \ + -e FIRST_SUPERUSER_PASSWORD="$$(sed -n 's/^FIRST_SUPERUSER_PASSWORD=//p' $(COMPOSE_ROOT)/.env | head -1)" +PW_IMAGE = mcr.microsoft.com/playwright:v$(PW_VERSION)-noble + +# The names are mapped onto the local Traefik by address, so DNS never decides. +# Twice, because Chromium pins *.localhost to loopback whatever /etc/hosts says. +test-frontend: test-stack ## Run frontend tests (Playwright e2e) against the test stack @ip=$$(docker network inspect proxy \ --format '{{range .Containers}}{{if eq .Name "fluksio-app-proxy-1"}}{{.IPv4Address}}{{end}}{{end}}' \ 2>/dev/null | cut -d/ -f1); \ - [ -n "$$ip" ] || { echo " ✗ proxy network or Traefik container not found — is the stack up?"; exit 1; }; \ - domain=$$(docker inspect fluksio-app 2>/dev/null \ - | grep -o 'Host(`app\.[^`]*`)' | head -1 | sed 's/Host(`app\.//;s/`)//'); \ - [ -n "$$domain" ] || { echo " ✗ no running app stack to test — 'make dev' first"; exit 1; }; \ - docker run --rm --network proxy --ipc=host \ - --user $$(id -u):$$(id -g) -e HOME=/tmp \ - --add-host app.$$domain:$$ip --add-host api.$$domain:$$ip \ - -v $(COMPOSE_ROOT):/app -w /app/frontend \ - -e PLAYWRIGHT_BASE_URL=http://app.$$domain \ - -e PLAYWRIGHT_API_URL=http://api.$$domain \ - -e HOST_RESOLVER_RULES="MAP app.$$domain $$ip, MAP api.$$domain $$ip" \ - -e CI=$${CI:-1} \ - -e FIRST_SUPERUSER="$$(sed -n 's/^FIRST_SUPERUSER=//p' $(COMPOSE_ROOT)/.env | head -1)" \ - -e FIRST_SUPERUSER_PASSWORD="$$(sed -n 's/^FIRST_SUPERUSER_PASSWORD=//p' $(COMPOSE_ROOT)/.env | head -1)" \ - mcr.microsoft.com/playwright:v$(PW_VERSION)-noble \ - npx playwright test $(PLAYWRIGHT_ARGS) + [ -n "$$ip" ] || { echo " ✗ the local Traefik is not on the proxy network"; exit 1; }; \ + $(PW_RUN) --network proxy \ + --add-host app.$(TEST_DOMAIN):$$ip --add-host api.$(TEST_DOMAIN):$$ip \ + -e HOST_RESOLVER_RULES="MAP app.$(TEST_DOMAIN) $$ip, MAP api.$(TEST_DOMAIN) $$ip" \ + $(PW_IMAGE) npx playwright test $(PLAYWRIGHT_ARGS) + +# WebKit takes *.localhost to loopback through GLib and ignores /etc/hosts, so +# this run shares Traefik's network namespace, where loopback *is* Traefik. +# Only the drag specs, which is where the engines differ. +test-webkit: test-stack ## Run the drag specs in WebKit against the test stack + @$(PW_RUN) --network container:fluksio-app-proxy-1 -e PLAYWRIGHT_WEBKIT=1 \ + $(PW_IMAGE) npx playwright test --project=webkit $(PLAYWRIGHT_ARGS) # Load and chaos against a *running* stack, never part of `make test`: it # restarts this stack's containers. `SOAK_ARGS="--dry-run"` only looks. -soak: ## Run the soak/chaos harness (SOAK_ARGS="--minutes 30 --scenario redis") +soak: ## Run the soak/chaos harness (SOAK_ARGS="--minutes 30 --scenario redis --force") cd backend && uv run python scripts/soak.py $(SOAK_ARGS) bench-startup: ## Time submitting a run (BENCH_ARGS="--kedro ../some/kedro/project") @@ -248,5 +283,6 @@ docs-serve: ## Serve the documentation site locally with live reload clean: ## Remove build artifacts and caches rm -rf frontend/dist frontend/blob-report frontend/test-results - rm -rf backend/.pytest_cache backend/htmlcov site + rm -rf backend/.pytest_cache backend/htmlcov site \ + || echo " ! not all removed (htmlcov/ root-owned? sudo rm -rf backend/htmlcov)" >&2 find backend -type d -name __pycache__ -exec rm -rf {} + 2>/dev/null || true diff --git a/README.md b/README.md index e6ee9b9..1035146 100644 --- a/README.md +++ b/README.md @@ -66,7 +66,7 @@ make dev-frontend # Vite on :5173 ``` ```sh -make test # pytest + Playwright (the e2e half needs the stack up) +make test # pytest + Playwright (the e2e half on its own stack, behind make dev's Traefik) make lint # ruff + mypy + biome make generate-client # regenerate the frontend SDK from the OpenAPI schema ``` diff --git a/backend/Dockerfile b/backend/Dockerfile index 93f978e..290b454 100644 --- a/backend/Dockerfile +++ b/backend/Dockerfile @@ -26,6 +26,16 @@ ENV PATH="/app/.venv/bin:$PATH" # environment somebody already works in is the case that adopts instead. ENV NODE_VENV=managed +# ffmpeg for node code that transcodes, such as the clip recipe in +# docs/code/nodes.md. Off by default: Debian's package and what it pulls in add +# about 400 MB. Set WITH_FFMPEG=true in .env and rebuild to include it. +ARG WITH_FFMPEG=false +RUN if [ "$WITH_FFMPEG" = true ]; then \ + apt-get update \ + && apt-get install -y --no-install-recommends ffmpeg \ + && rm -rf /var/lib/apt/lists/*; \ + fi + # Install dependencies # Ref: https://docs.astral.sh/uv/guides/integration/docker/#intermediate-layers RUN --mount=type=cache,target=/root/.cache/uv \ diff --git a/backend/fluksio/ai/agent.py b/backend/fluksio/ai/agent.py index 123b9b3..14ad091 100644 --- a/backend/fluksio/ai/agent.py +++ b/backend/fluksio/ai/agent.py @@ -39,7 +39,9 @@ Publish only when the user asks for the change to go live; otherwise leave \ the draft for review in the editor and say so. - list_node_types gives the node types and their parameters; get_node_source \ shows how a Python node is written before you write one. -- Keep answers short, in plain text without markdown. Name what you changed. +- Keep answers short and name what you changed. Answers render as markdown: \ +use lists, `inline code` and fenced code blocks where they help, and no \ +tables, images or HTML. The flow document `save_flow` takes, as a JSON schema: """ @@ -117,8 +119,10 @@ async def run_turn( ) -> AsyncIterator[dict[str, Any]]: """One user turn, yielding progress events as it goes. - A round is appended to *messages* only once every tool in it has answered, - so a turn cut off part-way leaves a transcript the provider still accepts. + A round's text arrives as ``delta`` events while the model writes it, and + once more whole as ``message`` when the round is done. A round is appended + to *messages* only once every tool in it has answered, so a turn cut off + part-way leaves a transcript the provider still accepts. """ messages.append({"role": "user", "content": text}) tools = await tool_specs() @@ -126,18 +130,22 @@ async def run_turn( token = caller_authorization.set(authorization) try: for _ in range(config.max_rounds): + choice: dict[str, Any] = {} try: - response = await client.chat_completion( + async for part in client.stream_completion( api_base=config.api_base, api_key=config.api_key, model=config.model, messages=[system, *messages], tools=tools, - ) + ): + if isinstance(part, str): + yield {"type": "delta", "content": part} + else: + choice = part except (client.ProviderError, httpx.HTTPError) as exc: yield {"type": "error", "detail": f"The provider answered {exc}"} return - choice = response["choices"][0]["message"] content = choice.get("content") or "" calls = choice.get("tool_calls") or [] assistant: dict[str, Any] = {"role": "assistant", "content": content} diff --git a/backend/fluksio/ai/client.py b/backend/fluksio/ai/client.py index fd19e75..d9607a6 100644 --- a/backend/fluksio/ai/client.py +++ b/backend/fluksio/ai/client.py @@ -4,10 +4,14 @@ Chat completions with function tools is the dialect every hosted provider and every local server speaks — OpenAI, Anthropic's compatibility endpoint, OpenRouter, Ollama, vLLM — so one client covers them without a provider SDK. The admin types the URL; the model never chooses where a request goes. + +The call is streamed, so the answer reaches the browser as it is written. """ from __future__ import annotations +import json +from collections.abc import AsyncIterator from typing import Any from urllib.parse import urlparse @@ -39,7 +43,69 @@ def _detail(response: httpx.Response) -> str: return f"{response.status_code}: {text[:300]}" -async def chat_completion( +class Assembler: + """The streamed chunks put back together into the message a plain call returns. + + The providers stream the same ``data:`` lines and differ only in the + margins: OpenRouter interleaves ``:`` comment lines, a thinking model's + reasoning arrives in a field of its own and is left out, OpenAI and + Anthropic send a tool call's arguments in pieces where Ollama sends the call + whole, and some leave out the call's ``index``. + """ + + def __init__(self) -> None: + self.content = "" + self.calls: list[dict[str, Any]] = [] + + def feed(self, line: str) -> str: + """One line of the stream; returns the text it adds to the answer.""" + if not line.startswith("data:"): + return "" + data = line[5:].strip() + if not data or data == "[DONE]": + return "" + try: + chunk = json.loads(data) + except ValueError as exc: + raise ProviderError(f"sent {data[:300]}") from exc + error = chunk.get("error") + if error: + if isinstance(error, dict): + error = error.get("message", error) + raise ProviderError(str(error)[:300]) + choices = chunk.get("choices") or [{}] + delta = choices[0].get("delta") or {} + for piece in delta.get("tool_calls") or []: + self._merge(piece) + text: str = delta.get("content") or "" + self.content += text + return text + + def _merge(self, piece: dict[str, Any]) -> None: + index = piece.get("index", len(self.calls)) + while len(self.calls) <= index: + self.calls.append( + { + "id": "", + "type": "function", + "function": {"name": "", "arguments": ""}, + } + ) + call = self.calls[index] + call["id"] = piece.get("id") or call["id"] + function = piece.get("function") or {} + call["function"]["name"] = function.get("name") or call["function"]["name"] + call["function"]["arguments"] += function.get("arguments") or "" + + def message(self) -> dict[str, Any]: + return { + "role": "assistant", + "content": self.content or None, + "tool_calls": self.calls or None, + } + + +async def stream_completion( *, api_base: str, api_key: str, @@ -47,21 +113,33 @@ async def chat_completion( messages: list[dict[str, Any]], tools: list[dict[str, Any]], timeout: float = 120.0, -) -> dict[str, Any]: - """``POST {api_base}/chat/completions``, the whole answer at once.""" - body: dict[str, Any] = {"model": model, "messages": messages} +) -> AsyncIterator[str | dict[str, Any]]: + """``POST {api_base}/chat/completions`` streamed. + + Yields each piece of the answer's text as it arrives, then the whole + message. The timeout is per chunk, so a long answer is not cut off. + """ + body: dict[str, Any] = {"model": model, "messages": messages, "stream": True} if tools: body["tools"] = tools - async with httpx.AsyncClient(timeout=timeout) as http: - response = await http.post( + assembler = Assembler() + async with ( + httpx.AsyncClient(timeout=timeout) as http, + http.stream( + "POST", api_base.rstrip("/") + "/chat/completions", json=body, headers=_headers(api_key), - ) - if response.is_error: - raise ProviderError(_detail(response)) - result: dict[str, Any] = response.json() - return result + ) as response, + ): + if response.is_error: + await response.aread() + raise ProviderError(_detail(response)) + async for line in response.aiter_lines(): + text = assembler.feed(line) + if text: + yield text + yield assembler.message() async def list_models(api_base: str, api_key: str, timeout: float = 15.0) -> list[str]: diff --git a/backend/fluksio/alembic/versions/2e7ca7d10044_run_declared.py b/backend/fluksio/alembic/versions/2e7ca7d10044_run_declared.py new file mode 100644 index 0000000..e024fee --- /dev/null +++ b/backend/fluksio/alembic/versions/2e7ca7d10044_run_declared.py @@ -0,0 +1,30 @@ +"""run.declared + +The inputs the flow declared when a run was submitted, so its record reads +against that version of the flow and not against whatever the flow declares +now. + +Revision ID: 2e7ca7d10044 +Revises: 930ffa094aad +Create Date: 2026-10-15 + +""" + +import sqlalchemy as sa +from alembic import op + +# revision identifiers, used by Alembic. +revision = "2e7ca7d10044" +down_revision = "930ffa094aad" +branch_labels = None +depends_on = None + + +def upgrade(): + # Nullable: a run submitted before this kept no snapshot, and its readers + # fall back to the flow's current declarations. + op.add_column("run", sa.Column("declared", sa.JSON(), nullable=True)) + + +def downgrade(): + op.drop_column("run", "declared") diff --git a/backend/fluksio/alembic/versions/930ffa094aad_flow_run_parent.py b/backend/fluksio/alembic/versions/930ffa094aad_flow_run_parent.py new file mode 100644 index 0000000..0018c81 --- /dev/null +++ b/backend/fluksio/alembic/versions/930ffa094aad_flow_run_parent.py @@ -0,0 +1,37 @@ +"""flow_run.parent + +A rate-limit flush is a run of its own now, and this names the run whose value +it released. Rows written before this carry an empty string, the same as every +run that continues no other. + +Revision ID: 930ffa094aad +Revises: b7d2e9c4a1f3 +Create Date: 2026-10-15 + +""" + +import sqlalchemy as sa +import sqlmodel.sql.sqltypes +from alembic import op + +# revision identifiers, used by Alembic. +revision = "930ffa094aad" +down_revision = "b7d2e9c4a1f3" +branch_labels = None +depends_on = None + + +def upgrade(): + op.add_column( + "flow_run", + sa.Column( + "parent", + sqlmodel.sql.sqltypes.AutoString(length=64), + nullable=False, + server_default="", + ), + ) + + +def downgrade(): + op.drop_column("flow_run", "parent") diff --git a/backend/fluksio/alembic/versions/ccb21eacf5d5_run_child_foreign_keys.py b/backend/fluksio/alembic/versions/ccb21eacf5d5_run_child_foreign_keys.py new file mode 100644 index 0000000..c7e15c6 --- /dev/null +++ b/backend/fluksio/alembic/versions/ccb21eacf5d5_run_child_foreign_keys.py @@ -0,0 +1,38 @@ +"""run_node, run_metric, run_artifact → run.id + +A foreign key from each run child table to its run, cascading on delete, so +deleting a run takes its rows with it and nothing can write one for a run that +is not there. + +Revision ID: ccb21eacf5d5 +Revises: 2e7ca7d10044 +Create Date: 2026-10-15 + +""" + +from alembic import op + +# revision identifiers, used by Alembic. +revision = "ccb21eacf5d5" +down_revision = "2e7ca7d10044" +branch_labels = None +depends_on = None + +TABLES = ("run_node", "run_metric", "run_artifact") + + +def upgrade(): + for table in TABLES: + # Rows a late write left behind after their run was deleted. Nothing + # reads them, and the constraint would refuse them. + op.execute(f"DELETE FROM {table} WHERE run_id NOT IN (SELECT id FROM run)") + with op.batch_alter_table(table) as batch_op: + batch_op.create_foreign_key( + f"fk_{table}_run_id_run", "run", ["run_id"], ["id"], ondelete="CASCADE" + ) + + +def downgrade(): + for table in TABLES: + with op.batch_alter_table(table) as batch_op: + batch_op.drop_constraint(f"fk_{table}_run_id_run", type_="foreignkey") diff --git a/backend/fluksio/api/deps.py b/backend/fluksio/api/deps.py index d92b336..d3dac28 100644 --- a/backend/fluksio/api/deps.py +++ b/backend/fluksio/api/deps.py @@ -99,14 +99,17 @@ def _locked(defn: DashboardDef, live: dict[str, Any]) -> bool: def _panel_writable(panel_id: str, request: Request) -> set[str]: """The messages this panel may publish to. - The same walk as the read allowlist, minus whatever a locked dashboard - contributes: ``locked`` is a dashboard saying it is there to be looked at, - and until this it stopped only the client drawing the control — the server - took the publish from a screen that asked anyway. + What its dashboards' inputs target, which is narrower than what it may + read: a chart, a reading or a bound setting is something a screen shows, + and showing a message does not entitle it to set one. - Not quite all of it: a querying chart publishes the request it reads by, - and a locked dashboard whose charts cannot ask goes blank rather than - read-only. That request is the exception ``panels.requests_of`` names. + A locked dashboard contributes less still: ``locked`` is a dashboard + saying it is there to be looked at, and until this it stopped only the + client drawing the control — the server took the publish from a screen + that asked anyway. Not nothing, though: a querying chart publishes the + request it reads by, and a locked dashboard whose charts cannot ask goes + blank rather than read-only. That request is the exception + ``panels.requests_of`` names. Union, exactly as the allowlist itself is: a message one dashboard on this panel displays and another controls stays writable, because the unlocked @@ -131,9 +134,7 @@ def _panel_writable(panel_id: str, request: Request) -> set[str]: live = controller.state.get_present(bound) if bound and controller else {} for defn in defns: writable |= ( - panels.requests_of(defn) - if _locked(defn, live) - else panels.messages_of(defn) + panels.requests_of(defn) if _locked(defn, live) else panels.targets_of(defn) ) return writable @@ -174,11 +175,12 @@ def _panel_may(payload: dict[str, Any], request: Request) -> None: Publishing is in the list because a panel cannot be strictly read-only — a control on a panel is the point of putting one there, and a querying chart asks for its window by publishing a request, which is why that request - counts as one of its widget's messages. Bounded to those, though: a screen + counts as one of its widget's targets. Bounded to those, though: a screen on a wall has no business reaching a message no tile on it draws, and the - catalogue behind ``GET /messages/`` is the whole namespace at once. A - dashboard that says it is locked is bounded further still — it entitles a - panel to read every message it names and to publish to none of them but + catalogue behind ``GET /messages/`` is the whole namespace at once. It + reads every message its tiles draw and writes only what its inputs target. + A dashboard that says it is locked is bounded further still — it entitles + a panel to read every message it names and to publish to none of them but the requests its own charts ask by. # ponytail: the panel file and the published dashboards are re-read per diff --git a/backend/fluksio/api/routes/ai.py b/backend/fluksio/api/routes/ai.py index 8c93fd7..0cae07b 100644 --- a/backend/fluksio/api/routes/ai.py +++ b/backend/fluksio/api/routes/ai.py @@ -259,8 +259,9 @@ def chat( ) -> StreamingResponse: """One turn, streamed as server-sent events. - ``message``, ``tool_call`` and ``tool_result`` events as the model works, - ``error`` when something stops it, and ``done`` last either way. + ``delta`` as the model writes, ``message`` with a round's whole text, + ``tool_call`` and ``tool_result`` as it works, ``error`` when something + stops it, and ``done`` last either way. """ config = ai_config.load() if config is None or not config.ready: diff --git a/backend/fluksio/api/routes/artifacts.py b/backend/fluksio/api/routes/artifacts.py index 89fb0b6..45573af 100644 --- a/backend/fluksio/api/routes/artifacts.py +++ b/backend/fluksio/api/routes/artifacts.py @@ -10,17 +10,23 @@ import tempfile from pathlib import Path from typing import Any -from fastapi import APIRouter, Depends, HTTPException, Query, Request +from fastapi import APIRouter, Depends, HTTPException, Query, Request, Response from fastapi.responses import FileResponse from jwt.exceptions import InvalidTokenError from pydantic import BaseModel from sqlmodel import Session from starlette.concurrency import run_in_threadpool -from fluksio.api.deps import user_from_token, worker_claims +from fluksio.api.deps import ( + FlowControllerDep, + get_current_active_superuser, + user_from_token, + worker_claims, +) from fluksio.core.config import settings from fluksio.core.db import engine from fluksio.flow.artifacts import ArtifactStore +from fluksio.flow.runs import referenced_artifacts #: What may be echoed back as a response content type. The caller holds the #: reference and passes its media type, so this guards a header rather than @@ -153,3 +159,33 @@ def get_artifact( headers["Content-Security-Policy"] = "sandbox" headers["X-Content-Type-Options"] = "nosniff" return FileResponse(path, media_type=media_type, headers=headers) + + +@router.delete( + "/{digest}", + status_code=204, + dependencies=[Depends(get_current_active_superuser)], +) +def delete_artifact( + digest: str, request: Request, controller: FlowControllerDep +) -> Response: + """Remove one artifact's bytes from the store. + + A superuser's call, because the store records no uploader and identical + content from two callers is one file, so nobody else can be said to own + it. Refused while anything still names the digest, the same references the + hourly sweep keeps, so a reference that exists never answers 404. The + volatile ring is left alone: its frames fall out on their own. + """ + store = _store(request) + if digest in referenced_artifacts(controller.state, controller.store): + raise HTTPException( + status_code=409, + detail=( + "The artifact is still referenced by a run, a current message or " + "a flow input's initial value" + ), + ) + if not store.delete(digest): + raise HTTPException(status_code=404, detail="No such artifact") + return Response(status_code=204) diff --git a/backend/fluksio/api/routes/cloud.py b/backend/fluksio/api/routes/cloud.py index 79a1bd2..e8e064b 100644 --- a/backend/fluksio/api/routes/cloud.py +++ b/backend/fluksio/api/routes/cloud.py @@ -13,12 +13,14 @@ chain stops with whoever a superuser here typed a code for. from __future__ import annotations import logging +import uuid from typing import Any import httpx from fastapi import APIRouter, Depends, HTTPException, Request from fastapi.concurrency import run_in_threadpool from pydantic import BaseModel, Field +from sqlmodel import col, select from fluksio.api.deps import ( CurrentUser, @@ -28,7 +30,7 @@ from fluksio.api.deps import ( ) from fluksio.cloud import config as cloud_config from fluksio.cloud import enroll as enroll_mod -from fluksio.models import Message, UserPublic +from fluksio.models import Message, User, UserPublic logger = logging.getLogger(__name__) @@ -60,6 +62,7 @@ def read_status(request: Request) -> dict[str, Any]: "instance_id": config.instance_id if config else None, "last_error": None, "connected_since": None, + "revoked": False, } status: dict[str, Any] = connector.status() return status @@ -158,6 +161,151 @@ def add_remote_user(session: SessionDep, body: RemoteUserBody) -> Any: raise HTTPException(status_code=exc.status, detail=exc.detail) from exc +class PortalMember(BaseModel): + #: Their portal account, which is what ``portal_sub`` holds here. + user_id: str + email: str + #: Their local user, or None while they have not opened the instance yet. + local_user_id: uuid.UUID | None = None + #: A local user has their email, which keeps them out until a superuser + #: links the two. Never true of a superuser's account. + linkable: bool = False + + +@router.get( + "/members", + dependencies=[Depends(get_current_active_superuser)], + response_model=list[PortalMember], +) +def read_members(session: SessionDep) -> Any: + """Everyone the portal admits to this instance, its owner aside. + + A share link admits somebody at the portal before this instance has heard + of them, so this is the one place a superuser here can see who may still + arrive. It also shows a membership left standing when a local user was + deleted while the portal could not be told. + """ + config = cloud_config.load() + if config is None: + raise HTTPException( + status_code=409, detail="This instance is not connected to a portal" + ) + try: + response = httpx.get( + f"{config.portal_url.rstrip('/')}/api/v1/instance-members/", + headers={"Authorization": f"Bearer {config.token}"}, + timeout=15.0, + ) + except httpx.HTTPError as exc: + raise HTTPException( + status_code=502, detail=f"Could not reach the portal: {exc}" + ) from exc + if response.status_code != 200: + raise HTTPException( + status_code=502, + detail=f"The portal did not list members ({response.status_code})", + ) + members = response.json()["data"] + subs = [str(member["user_id"]) for member in members] + local = { + user.portal_sub: user.id + for user in session.exec(select(User).where(col(User.portal_sub).in_(subs))) + } + emails = [str(member["email"]) for member in members] + linkable = { + user.email + for user in session.exec(select(User).where(col(User.email).in_(emails))) + if not user.is_superuser and not user.portal_sub + } + return [ + PortalMember( + user_id=str(member["user_id"]), + email=str(member["email"]), + local_user_id=local.get(str(member["user_id"])), + linkable=str(member["user_id"]) not in local + and str(member["email"]) in linkable, + ) + for member in members + ] + + +@router.post( + "/members/{portal_sub}/link", + dependencies=[Depends(get_current_active_superuser)], + response_model=UserPublic, +) +def link_member(session: SessionDep, portal_sub: uuid.UUID) -> Any: + """Let a pending member in as the local user that already has their email. + + Arriving on their own they are refused, because handing an existing + account to whoever holds that address at the portal is not a guess to + make. This is a superuser deciding it, and never for a superuser's account. + """ + if cloud_config.load() is None: + raise HTTPException( + status_code=409, detail="This instance is not connected to a portal" + ) + sub = str(portal_sub) + member = enroll_mod.lookup_member(sub) + if member is None: + raise HTTPException( + status_code=404, detail="The portal did not confirm this member" + ) + if session.exec(select(User).where(User.portal_sub == sub)).first(): + raise HTTPException(status_code=409, detail="That account already has access") + user = session.exec(select(User).where(User.email == str(member["email"]))).first() + if user is None: + raise HTTPException(status_code=404, detail="No local user has this email") + if user.is_superuser: + raise HTTPException( + status_code=403, detail="A superuser cannot be linked to a portal account" + ) + if user.portal_sub: + raise HTTPException( + status_code=409, detail="This user is linked to another portal account" + ) + user.portal_sub = sub + session.add(user) + session.commit() + session.refresh(user) + return user + + +@router.delete( + "/members/{portal_sub}", + dependencies=[Depends(get_current_active_superuser)], + response_model=Message, +) +def revoke_member(portal_sub: uuid.UUID) -> Message: + """Take a portal membership back. + + For somebody who has not arrived, this is the whole of their access. + Somebody who has is deleted under Admin → Users instead, which also does + this. + """ + config = cloud_config.load() + if config is None: + raise HTTPException( + status_code=409, detail="This instance is not connected to a portal" + ) + try: + response = httpx.delete( + f"{config.portal_url.rstrip('/')}/api/v1/instance-members/{portal_sub}", + headers={"Authorization": f"Bearer {config.token}"}, + timeout=15.0, + ) + except httpx.HTTPError as exc: + raise HTTPException( + status_code=502, detail=f"Could not reach the portal: {exc}" + ) from exc + if response.status_code != 200: + raise HTTPException( + status_code=502, + detail=f"The portal refused the revocation ({response.status_code})", + ) + return Message(message="Membership revoked") + + def forget_remote_user(portal_sub: str) -> None: """Tell the portal a remote user is gone from here. @@ -186,19 +334,31 @@ def forget_remote_user(portal_sub: str) -> None: @router.delete( "", dependencies=[Depends(get_current_active_superuser)], response_model=Message ) -def disconnect(request: Request) -> Message: +def disconnect(request: Request, session: SessionDep) -> Message: """Sever the connection from this side. Unilateral and immediate: the config is the only reason portal tokens verify here, so deleting it ends remote access whatever the portal still - has on file. + has on file. The users the portal let in go with it. The account it was + enrolled as stays, and so does every superuser. """ task = getattr(request.app.state, "cloud_task", None) if task is not None: task.cancel() request.app.state.cloud_task = None request.app.state.cloud_connector = None + config = cloud_config.load() cloud_config.delete() + owner = config.local_user_id if config is not None else None + for user in session.exec( + select(User).where( + col(User.portal_sub).is_not(None), + User.is_superuser == False, # noqa: E712 + ) + ): + if str(user.id) != owner: + session.delete(user) + session.commit() return Message(message="Disconnected from the portal") diff --git a/backend/fluksio/api/routes/flows.py b/backend/fluksio/api/routes/flows.py index d4f2a6f..af7fda6 100644 --- a/backend/fluksio/api/routes/flows.py +++ b/backend/fluksio/api/routes/flows.py @@ -1,7 +1,9 @@ """The flow API. Everything the editor can do is available here first.""" import asyncio +import os import time +from pathlib import Path from typing import Any import orjson @@ -32,11 +34,11 @@ from fluksio.api.deps import ( from fluksio.api.socket_auth import TICKET_TTL_S, issue_ticket, socket_token from fluksio.core.db import engine from fluksio.flow.artifacts import is_reference -from fluksio.flow.controller import NODE_TYPES, FlowController +from fluksio.flow.controller import NODE_TYPES, FlowController, follow_header from fluksio.flow.dashboards import DashboardStore from fluksio.flow.events import event_bus from fluksio.flow.messages import flow_of, qualify -from fluksio.flow.panels import messages_for +from fluksio.flow.panels import messages_for, read_config from fluksio.flow.pipeline import ValidationIssue from fluksio.flow.runs import RunRejected from fluksio.flow.schemas import ( @@ -52,6 +54,7 @@ from fluksio.flow.schemas import ( MessageValue, NodeDef, NodeSource, + NodeSourceSaved, NodeStatusPublic, NodeTypeInfo, ) @@ -76,18 +79,19 @@ ws_router = APIRouter(prefix="/flows", tags=["flows"]) class Endpoint(BaseModel): """Something wired into this flow that is not a node in it. - A dashboard control setting one of its messages, a tile showing one, or a - node in another flow on the far side of a dotted name. The canvas draws - these so a value never appears to come from nowhere — or worse, appears to - come from whichever node happens to be drawn as a producer. + A dashboard control setting one of its messages, a tile showing one, a + port on a panel's own hardware, or a node in another flow on the far side + of a dotted name. The canvas draws these so a value never appears to come + from nowhere — or worse, appears to come from whichever node happens to be + drawn as a producer. """ - #: dashboard or flow. + #: dashboard, panel or flow. kind: str #: Stable within its kind, and used as the canvas node id. id: str label: str - #: What sort of widget, or the node type in the other flow. + #: What sort of widget, the panel, or the node type in the other flow. detail: str = "" #: Messages of this flow it publishes, and ones it reads. provides: list[str] = [] @@ -153,6 +157,28 @@ def _endpoints(controller: FlowController, definition: FlowDef) -> list[Endpoint requires=binding["requires"], ) + # A panel's own hardware: an ``in`` port publishes like a control, an + # ``out`` port reads like a tile. The id is the source id the device + # publishes under, so its edges pulse for its values. + prefix = f"{flow}." + for panel in read_config().panels: + for port in panel.io: + if not port.message.startswith(prefix): + continue + key = f"panel:{panel.id}:{port.port}" + endpoint = found.setdefault( + key, + Endpoint( + kind="panel", + id=key, + label=port.port, + detail=panel.title or panel.id, + ), + ) + side = endpoint.provides if port.direction == "in" else endpoint.requires + if port.message not in side: + side.append(port.message) + def _flow_endpoint(key: str, detail: str) -> Endpoint: """One label per node on the far side, however many names reach it.""" return found.setdefault( @@ -336,9 +362,27 @@ def read_library(controller: FlowControllerDep) -> Any: @router.delete("/library/{lib_name}", response_model=Message) -async def delete_shared_node(lib_name: str, controller: FlowControllerDep) -> Any: - """Remove a shared source, as long as no flow still runs it.""" - used_by = controller.store.usages(lib_name) +async def delete_shared_node( + lib_name: str, controller: FlowControllerDep, detach: bool = False +) -> Any: + """Remove a shared source, as long as no flow still runs it. + + With ``detach``, every node using it keeps a copy of the code as its own + first, so the source can be retired while flows still use it. + """ + if detach: + running = await run_in_threadpool(controller.store.usages, lib_name, False) + try: + await run_in_threadpool(controller.store.detach_lib, lib_name) + except LibNotFound: + raise HTTPException(status_code=404, detail=f"No shared node '{lib_name}'") + for flow in sorted({usage.split(".", 1)[0] for usage in running}): + await controller.reload_flow(flow) + return Message(message=f"Deleted shared node '{lib_name}'") + # The published flows too: a draft that unshared a node still runs it. + used_by = sorted( + {*controller.store.usages(lib_name), *controller.store.usages(lib_name, False)} + ) if used_by: raise HTTPException( status_code=409, @@ -382,6 +426,34 @@ def _check_flavors(definition: FlowDef) -> None: ) +def _check_origin(definition: FlowDef) -> None: + """Refuse an ``origin.repo`` that is not a project's own directory. + + Every run of a code-defined flow hashes the python files below it, so + ``/`` or a home directory makes each run walk far more than the project, + and a relative path names nothing the engine can find. + """ + repo = definition.origin.repo if definition.origin is not None else "" + if not repo: + return + if not os.path.isabs(repo): + problem = "is a relative path" + elif os.path.realpath(repo) == "/": + problem = "is the filesystem root" + elif os.path.realpath(repo) == os.path.realpath(Path.home()): + problem = "is the home directory" + else: + return + raise HTTPException( + status_code=422, + detail=( + f"origin.repo '{repo}' {problem}. It has to be the absolute path " + "of the repository the flow is declared in, since every run hashes " + "the python files below it. Sync from inside that repository." + ), + ) + + def _worker_env(node: NodeDef | None) -> dict[str, str]: return node.resources.env if node is not None and node.resources else {} @@ -450,6 +522,7 @@ async def save_flow( raise HTTPException(status_code=400, detail="Node names must be unique") _check_flavors(definition) + _check_origin(definition) if not user.is_superuser: _refuse_code_changes(controller, name, definition) @@ -538,7 +611,7 @@ async def delete_flow( # Its files are gone; its values and queued work would otherwise linger. # Its runs stay: a run record is a record of what ran, and surviving the # flow it belonged to is the point of keeping one. `DELETE /runs?flow=` - # is what clears them. + # is what clears them, and with `history=true` its cascades too. await run_in_threadpool(controller.forget_flow, name) await controller.reload_flow(name) return Message(message=f"Deleted flow '{name}'") @@ -600,9 +673,41 @@ def read_node_source( ) +def _follow_header( + controller: FlowController, name: str, node_id: str, code: str +) -> FlowDef | None: + """Bring a function node's ports and settings in line with its header. + + The draft is what the editor is on, so that is what moves, and only for a + node with its own code: a shared one serves other flows with other ports, + and a synced flow's ports are declared in its repository. Returns the + draft when it changed. + """ + definition = controller.store.read_flow(name, draft=True) + node = next((n for n in definition.nodes if n.id == node_id), None) + spec = NODE_TYPES.get(node.type) if node is not None else None + if ( + node is None + or spec is None + or not spec.free_params + or node.source_ref + or definition.origin + ): + return None + followed = follow_header(node, code) + if followed == node: + return None + definition.nodes = [followed if n.id == node_id else n for n in definition.nodes] + try: + return controller.store.write_draft(definition, definition.version) + except StaleVersion: + # Someone saved in between; the next save of this code tries again. + return None + + @router.put( "/{name}/nodes/{node_id}/source", - response_model=NodeStatusPublic, + response_model=NodeSourceSaved, dependencies=[Depends(get_code_author)], ) async def save_node_source( @@ -610,6 +715,7 @@ async def save_node_source( node_id: str, source: NodeSource, controller: FlowControllerDep, + user: CurrentUser, ) -> Any: """Save a node's source as an unpublished edit and report whether it loads. @@ -619,7 +725,12 @@ async def save_node_source( A shared node writes to the library, so the fix reaches every flow using it — and that one is live immediately rather than waiting for a publish, - because the copy is not any single flow's to hold back. + because the copy is not any single flow's to hold back. Being live, it is + audited like a publish. + + A node with its own code takes its input ports and settings from the + header of ``process`` (see `follow_header`), and the answer carries the + node when that changed it. """ definition = _read_flow(controller, name) ref = _source_ref(definition, node_id) @@ -628,11 +739,15 @@ async def save_node_source( controller.store.write_lib_source, ref, source.code ) if changed: + _audit(f"edited shared node '{ref}'", name, user) await controller.reload_lib_users(ref) else: await run_in_threadpool( controller.store.write_node_source, name, node_id, source.code, True ) + followed = await run_in_threadpool( + _follow_header, controller, name, node_id, source.code + ) node_def = next((n for n in definition.nodes if n.id == node_id), None) device = ( node_def.device @@ -642,10 +757,14 @@ async def save_node_source( error = await run_in_threadpool( controller.compile_check, name, node_id, source.code, device ) - return NodeStatusPublic( + return NodeSourceSaved( id=f"{name}.{node_id}", status="error" if error else "active", error=error, + node=next((n for n in followed.nodes if n.id == node_id), None) + if followed + else None, + version=followed.version if followed else None, ) @@ -907,6 +1026,8 @@ def snapshot_payload( two have to agree, so they build the message here rather than each their own. ``emits`` is what the bus counted while nobody was listening — a client that reconnects between two pages would otherwise start from zero. + ``queued`` is the nodes waiting for a machine, for the same reason: the bus + announces a wait once, and a client that missed it would draw the node idle. ``only`` bounds it to a set of message names, which is what a wall panel gets: the values its own dashboards draw, and none of the rest — no node @@ -925,6 +1046,7 @@ def snapshot_payload( "paused": [], "logs": [], "emits": {}, + "queued": [], } return { "type": "snapshot", @@ -934,6 +1056,7 @@ def snapshot_payload( "paused": controller.paused_flows(), "logs": list(event_bus.recent_logs), "emits": dict(event_bus.emits), + "queued": controller.placer.snapshot()["waiting"] if controller.placer else [], } diff --git a/backend/fluksio/api/routes/oauth.py b/backend/fluksio/api/routes/oauth.py index fe4b797..b5df695 100644 --- a/backend/fluksio/api/routes/oauth.py +++ b/backend/fluksio/api/routes/oauth.py @@ -42,6 +42,7 @@ from fluksio.api.deps import ( get_current_active_superuser, get_current_user, ) +from fluksio.cloud import config as cloud_config from fluksio.core import security from fluksio.core.config import settings from fluksio.models import ( @@ -301,13 +302,22 @@ def authorize_validate( @router.post("/authorize", response_model=OAuthAuthorizeResponse) def authorize( + request: Request, body: OAuthAuthorizeRequest, current_user: CurrentUser, session: SessionDep, ) -> Any: - """Approve a client, on behalf of the signed-in user.""" + """Approve a client, on behalf of the signed-in user. + + Through the hub the endpoint is the one there, which is the resource an + agent that came that way asks for. + """ if not settings.MCP_ENABLED: return _error("access_denied", "The MCP endpoint is switched off.", 403) + resources = {settings.mcp_resource} + base = cloud_config.portal_base(request.headers) + if base is not None: + resources.add(f"{base}/mcp") client = _load_client(session, body.client_id) if client is None: @@ -318,7 +328,7 @@ def authorize( return _error("invalid_request", "PKCE must use S256.") if not _PKCE_RE.match(body.code_challenge): return _error("invalid_request", "Malformed code challenge.") - if body.resource and body.resource.rstrip("/") != settings.mcp_resource: + if body.resource and body.resource.rstrip("/") not in resources: return _error("invalid_target", "Unknown resource.") code = secrets.token_urlsafe(32) diff --git a/backend/fluksio/api/routes/observability.py b/backend/fluksio/api/routes/observability.py index 154a989..1374406 100644 --- a/backend/fluksio/api/routes/observability.py +++ b/backend/fluksio/api/routes/observability.py @@ -19,12 +19,17 @@ from sqlalchemy import select as sa_select from sqlmodel import col, select from fluksio import __version__ -from fluksio.api.deps import FlowControllerDep, SessionDep, get_current_user +from fluksio.api.deps import ( + FlowControllerDep, + SessionDep, + get_current_active_superuser, + get_current_user, +) from fluksio.api.routes.runs import elapsed_ms from fluksio.core.config import settings from fluksio.flow.controller import NodeStatus from fluksio.flow.pipeline import ADVISORY_ISSUES -from fluksio.models import EngineEvent, FlowRun, MetricBucket +from fluksio.models import EngineEvent, FlowRun, Message, MetricBucket router = APIRouter( prefix="/observability", @@ -81,10 +86,23 @@ class FlowRollup(BaseModel): last_error_ts: float | None = None +class NodeTypeTiming(BaseModel): + """How long one kind of node takes to run, across every node of it.""" + + type: str + #: How many published nodes of this type ran inside the window. + nodes: int + executions: int + avg_ms: float + max_ms: float + + class RunRow(BaseModel): id: str flow: str source: str + #: The run this one continues: for a flush, the run whose value it released. + parent: str = "" status: str started_at: datetime finished_at: datetime | None = None @@ -421,6 +439,58 @@ def read_flow_rollups( ] +@router.get("/node-types", response_model=list[NodeTypeTiming]) +def read_node_type_timings( + session: SessionDep, + controller: FlowControllerDep, + hours: Annotated[int, Query(ge=0, le=8760)] = 24, +) -> Any: + """Execution time per node type, slowest first, to point at the hotspots. + + The rollups are kept per node, so the type is joined on from the published + flows. A node deleted since it ran has no type left to count under. + """ + hours = _window_hours(hours) + types = { + f"{flow.name}.{node.id}": node.type + for flow in controller.store.read_all() + for node in flow.nodes + } + statement = ( + sa_select( + col(MetricBucket.node), + func.sum(col(MetricBucket.executions)).label("executions"), + func.sum(col(MetricBucket.duration_sum_ms)).label("duration_sum_ms"), + func.max(col(MetricBucket.duration_max_ms)).label("max_ms"), + ) + .where(col(MetricBucket.bucket) >= _since(hours)) + .group_by(col(MetricBucket.node)) + ) + totals: dict[str, dict[str, float]] = {} + for row in session.execute(statement): + kind = types.get(row.node) + if kind is None or not row.executions: + continue + entry = totals.setdefault( + kind, {"nodes": 0, "executions": 0, "duration_sum_ms": 0.0, "max_ms": 0.0} + ) + entry["nodes"] += 1 + entry["executions"] += row.executions + entry["duration_sum_ms"] += row.duration_sum_ms + entry["max_ms"] = max(entry["max_ms"], row.max_ms) + timings = [ + NodeTypeTiming( + type=kind, + nodes=int(entry["nodes"]), + executions=int(entry["executions"]), + avg_ms=round(entry["duration_sum_ms"] / entry["executions"], 3), + max_ms=round(entry["max_ms"], 3), + ) + for kind, entry in totals.items() + ] + return sorted(timings, key=lambda timing: timing.avg_ms, reverse=True) + + @router.get("/runs", response_model=RunPage) def read_runs( session: SessionDep, @@ -506,3 +576,18 @@ async def read_dead_letters( if controller.execution is None: return [] return await run_in_threadpool(controller.execution.queue.dead_letters, limit) + + +@router.post( + "/flush", + response_model=Message, + dependencies=[Depends(get_current_active_superuser)], +) +async def flush_metrics(request: Request) -> Any: + """Write what the metrics collector holds now, not at its next interval. + + For tooling that reads or clears these tables right after causing events, + such as the soak harness clearing its own rows. + """ + await request.app.state.metrics_collector.flush_now() + return Message(message="Metrics flushed") diff --git a/backend/fluksio/api/routes/panels.py b/backend/fluksio/api/routes/panels.py index 56ef9c7..8cb6b78 100644 --- a/backend/fluksio/api/routes/panels.py +++ b/backend/fluksio/api/routes/panels.py @@ -269,6 +269,8 @@ class PanelsPublic(BaseModel): """ panels: list[PanelDef] = Field(default_factory=list) + #: What a save of this list has to send back. + version: int = 0 #: Whatever this instance was told it is reachable at. The same setting #: the password-reset links are built from, so an instance that has it #: wrong has it wrong in both places. @@ -277,7 +279,9 @@ class PanelsPublic(BaseModel): def _public(config: PanelsConfig) -> PanelsPublic: return PanelsPublic( - panels=config.panels, frontend_host=settings.FRONTEND_HOST.rstrip("/") + panels=config.panels, + version=config.version, + frontend_host=settings.FRONTEND_HOST.rstrip("/"), ) @@ -298,6 +302,11 @@ async def save_panels(body: PanelsConfig) -> Any: A panel that disappears here takes its credential with it, so this is also how a device is unpaired along with its assignment. Re-pairing one screen and keeping the assignment is ``/{panel_id}/unpair`` below. + + The body carries the ``version`` it was based on, and a save someone else + has moved past is refused with 409, as a dashboard's is. Two clients each + writing back the whole list would otherwise drop each other's panels, and + a dropped panel revokes its screen at once. """ seen = set() for panel in body.panels: @@ -314,9 +323,19 @@ async def save_panels(body: PanelsConfig) -> Any: # saving an arrangement — and reading it in a separate step from # writing it is exactly how an unpair in between was undone. with edit_lock: - stored = {p.id: p.nonce for p in read_config().panels} + stored = read_config() + if body.version != stored.version: + raise HTTPException( + status_code=409, + detail={ + "message": "Someone else saved the panels first", + "current_version": stored.version, + }, + ) + nonces = {p.id: p.nonce for p in stored.panels} for panel in body.panels: - panel.nonce = stored.get(panel.id, 0) + panel.nonce = nonces.get(panel.id, 0) + body.version = stored.version + 1 write_config(body) await run_in_threadpool(_save) diff --git a/backend/fluksio/api/routes/runs.py b/backend/fluksio/api/routes/runs.py index e1c1993..c895e9b 100644 --- a/backend/fluksio/api/routes/runs.py +++ b/backend/fluksio/api/routes/runs.py @@ -27,9 +27,9 @@ from fluksio.core.config import settings from fluksio.core.db import writing from fluksio.flow.events import event_bus from fluksio.flow.messages import requalify -from fluksio.flow.runs import RunRejected, RunService, new_run_id +from fluksio.flow.runs import OUTPUT_CAP, RunRejected, RunService, new_run_id from fluksio.flow.store import FlowNotFound -from fluksio.models import Message, Run, RunArtifact, RunMetric, RunNode +from fluksio.models import FlowRun, Message, Run, RunArtifact, RunMetric, RunNode router = APIRouter( prefix="/runs", tags=["runs"], dependencies=[Depends(get_current_user)] @@ -146,6 +146,9 @@ class RunNodeRow(BaseModel): #: Which run it was restored from, when it was. That run is also where this #: node's series was recorded. cached_from: str = "" + #: That run has since been deleted, and this node's curve went with it. Its + #: outputs are still on this run's result. + curve_lost: bool = False class ArtifactRow(BaseModel): @@ -210,6 +213,10 @@ class RunRow(BaseModel): class RunDetail(RunRow): result: dict[str, Any] = Field(default_factory=dict) flow_version: int = 1 + #: The inputs the flow declared when this was submitted, with their + #: declared values. Null for a run from before this was kept, which reads + #: against the flow's current declarations instead. + declared: dict[str, Any] | None = None nodes: list[RunNodeRow] = Field(default_factory=list) artifacts: list[ArtifactRow] = Field(default_factory=list) @@ -512,15 +519,22 @@ def _selected( ids: str, since: datetime | None, until: datetime | None, -) -> list[Run]: - """The runs an export covers, newest first — the filters the list takes. +) -> tuple[list[Run], int]: + """The runs an export covers, newest first, and how many the filters match. Capped: the filters bound a *sensible* request and nothing bounded an unfiltered one, so asking for everything read every row of the table into - memory before a byte was sent. A truncated export says so in a header. + memory before a byte was sent. A truncated export says so in a header, and + only then is the selection counted, since otherwise the rows are the count. """ statement = _selection(flow, status, group, ids, since, until) - return list(session.exec(statement.limit(EXPORT_CAP + 1))) + runs = list(session.exec(statement.limit(EXPORT_CAP + 1))) + if len(runs) <= EXPORT_CAP: + return runs, len(runs) + total = session.exec( + select(func.count()).select_from(statement.order_by(None).subquery()) + ).one() + return runs[:EXPORT_CAP], total def _cell(value: Any) -> Any: @@ -613,7 +627,7 @@ def _stream( name: str, columns: list[str], chunks: Iterator[list[dict[str, Any]]], - truncated: bool = False, + total: int = 0, ) -> StreamingResponse: """The rows out, a chunk at a time rather than a list. @@ -629,11 +643,12 @@ def _stream( body = _csv(columns, chunks) media = "text/csv; charset=utf-8" headers = {"Content-Disposition": f'attachment; filename="{name}.{fmt}"'} - if truncated: + if total > EXPORT_CAP: # The body is otherwise indistinguishable from a complete one, and an # export quietly missing its oldest runs is the wrong thing to hand - # somebody who is about to plot it. + # somebody who is about to plot it. The total says how many it missed. headers["X-Truncated"] = str(EXPORT_CAP) + headers["X-Total-Count"] = str(total) return StreamingResponse(body, media_type=media, headers=headers) @@ -656,9 +671,7 @@ def export_metrics( the metrics it lists; ``stride`` thins each curve — per series, so asking for every tenth point of two metrics gives every tenth point of both. """ - runs = _selected(session, flow, status, group, ids, since, until) - truncated = len(runs) > EXPORT_CAP - runs = runs[:EXPORT_CAP] + runs, total = _selected(session, flow, status, group, ids, since, until) wanted = [part for part in name.split(",") if part] step = max(1, stride) @@ -682,7 +695,7 @@ def export_metrics( ) yield rows - return _stream(format, "metrics", list(METRIC_COLUMNS), chunks(), truncated) + return _stream(format, "metrics", list(METRIC_COLUMNS), chunks(), total) @router.get("/export/runs") @@ -710,9 +723,7 @@ def export_runs( three fields rather than two blobs, and the defaults reach the same depth. """ - runs = _selected(session, flow, status, group, ids, since, until) - truncated = len(runs) > EXPORT_CAP - runs = runs[:EXPORT_CAP] + runs, total = _selected(session, flow, status, group, ids, since, until) inputs = [part for part in params.split(",") if part] or sorted( {path for run in runs for path, _ in _leaves(run.params)} ) @@ -730,7 +741,7 @@ def export_runs( row.update((f"metric.{k}", _cell(_dig(run.result, k))) for k in scores) yield [row] - return _stream(format, "runs", columns, chunks(), truncated) + return _stream(format, "runs", columns, chunks(), total) @router.get("/metrics/names", response_model=list[str]) @@ -773,12 +784,44 @@ def read_run(run_id: str, session: SessionDep) -> Any: ).all() detail = RunDetail.model_validate(run, from_attributes=True) detail.nodes = [RunNodeRow.model_validate(n, from_attributes=True) for n in nodes] + # Worked out here, not stored: a source run is deleted long after the + # nodes restored from it were written. + sources = {n.cached_from for n in nodes if n.cached_from} + kept = ( + set(session.exec(select(col(Run.id)).where(col(Run.id).in_(sources)))) + if sources + else set() + ) + for row in detail.nodes: + row.curve_lost = bool(row.cached_from) and row.cached_from not in kept detail.artifacts = [ ArtifactRow.model_validate(a, from_attributes=True) for a in artifacts ] return detail +@router.get("/{run_id}/nodes/{node}/outputs", response_model=dict[str, Any] | None) +def read_node_outputs(run_id: str, node: str, session: SessionDep) -> Any: + """What one node returned, by message, as the stage cache stored it. + + Kept only for a node that succeeded and may be reused, and only up to + 256 kB of JSON. Null when the node published nothing. + """ + row = session.get(RunNode, {"run_id": run_id, "node": node}) + if row is None: + raise HTTPException(status_code=404, detail=f"No node '{node}' in this run") + if row.outputs is None: + raise HTTPException( + status_code=404, + detail=( + f"Node '{node}' kept no outputs. It failed, may not be cached, " + f"or returned more than {OUTPUT_CAP // 1000} kB or a value JSON " + "cannot carry." + ), + ) + return json.loads(row.outputs) + + @router.post("/{run_id}/cancel", response_model=RunRow) async def cancel_run(run_id: str, request: Request, session: SessionDep) -> Any: """Stop a run. One already past its last node is left as it finished.""" @@ -808,22 +851,19 @@ async def retry_run(run_id: str, request: Request, user: CurrentUser) -> Any: def _drop_runs(session: Session, chosen: Any) -> None: """A selection of runs and everything hanging off them. - The run tables carry a plain string ``run_id`` and no foreign key, so - nothing cascades on its own and these four statements are the whole of it. - ``flow_run``, ``metric_minute`` and ``engine_event`` stay — they are the - observability record and are pruned on their own window. + The run tables' foreign keys cascade, so deleting the runs takes their + nodes, metrics and artifact rows with them. ``flow_run``, ``metric_minute`` + and ``engine_event`` stay — they are the observability record and are + pruned on their own window. Two things this costs, both deliberate. ``RunNode.outputs`` *is* the stage cache, so a later run loses hits these would have served. And a node restored from one of them points here through ``cached_from`` — ``_series`` - already reads a missing source as an empty curve, which is what ``NO_CURVE`` - explains on the screen. The artifact bytes need no help: ``sweep_artifacts`` + already reads a missing source as an empty curve, and ``read_run`` marks + such a node ``curve_lost``. The artifact bytes need no help: ``sweep_artifacts`` keeps whatever a ``run_artifact`` row or a live message still names, so dropping the rows is enough and the hourly sweep reclaims the blobs. """ - session.execute(delete(RunNode).where(col(RunNode.run_id).in_(chosen))) - session.execute(delete(RunMetric).where(col(RunMetric.run_id).in_(chosen))) - session.execute(delete(RunArtifact).where(col(RunArtifact.run_id).in_(chosen))) session.execute(delete(Run).where(col(Run.id).in_(chosen))) @@ -841,6 +881,7 @@ def delete_runs( session: SessionDep, user: CurrentUser, flow: Annotated[str, Query(min_length=1)], + history: bool = False, ) -> Any: """Every run of one flow at once. @@ -848,6 +889,10 @@ def delete_runs( deleting a flow leaves its runs standing on purpose, so this is the only way to clear a history without a call per run. ``flow`` is required — there is no "delete every run of everything" here, by design. + + ``history`` also drops the flow's rows in the run history (``flow_run``), + its cascades included. That is for a flow being deleted, which would + otherwise stay in the history until the retention window passes. """ with writing(session): live = _live_run(session, flow) @@ -866,6 +911,8 @@ def delete_runs( select(func.count()).select_from(Run).where(col(Run.flow) == flow) ).one() _drop_runs(session, chosen) + if history: + session.execute(delete(FlowRun).where(col(FlowRun.flow) == flow)) session.commit() event_bus.publish( { diff --git a/backend/fluksio/api/routes/workers.py b/backend/fluksio/api/routes/workers.py index ca6ef74..2388ef9 100644 --- a/backend/fluksio/api/routes/workers.py +++ b/backend/fluksio/api/routes/workers.py @@ -10,6 +10,7 @@ from __future__ import annotations import asyncio import logging +import time import uuid from datetime import UTC, datetime, timedelta from typing import Any @@ -55,6 +56,11 @@ TOKEN_DAYS = 365 #: The sockets attached with each token, by `jti`, so revoking one drops them. _attached: dict[str, set[WebSocket]] = {} +#: Workers refused for an older protocol, by name, until one attaches again. +#: Such an agent exits on the refusal, so this is the one place left that says +#: which host still needs `pip install -U fluksio-worker`. +_behind: dict[str, WorkerInfo] = {} + class WorkerInfo(BaseModel): name: str @@ -68,6 +74,11 @@ class WorkerInfo(BaseModel): cpus: int = 1 gpus: int = 0 ram_mb: int | None = None + protocol: int = 0 + #: The fluksio-worker it runs; empty for an agent too old to say. + version: str = "" + #: Refused for speaking an older protocol than this engine, so not attached. + behind: bool = False class TokenRequest(BaseModel): @@ -109,9 +120,11 @@ def read_workers(request: Request) -> Any: cpus=worker.inventory.cpus, gpus=worker.inventory.gpus, ram_mb=worker.inventory.ram_mb, + protocol=int(worker.info.get("protocol") or 0), + version=str(worker.info.get("version") or ""), ) for worker in _hub(request.app).workers() - ] + ] + list(_behind.values()) class ResourceLevel(BaseModel): @@ -274,7 +287,19 @@ async def _attached_session(websocket: WebSocket, claims: dict[str, Any]) -> Non await websocket.close(code=1002) return - if hello.get("op") != "hello" or int(hello.get("protocol", 0)) != PROTOCOL: + protocol = int(hello.get("protocol", 0)) + if hello.get("op") != "hello" or protocol != PROTOCOL: + if hello.get("op") == "hello" and protocol < PROTOCOL: + name = str(claims.get("sub") or hello.get("name") or "worker") + _behind[name] = WorkerInfo( + name=name, + labels=sorted(str(label) for label in claims.get("labels") or []), + last_seen=time.time(), + python=str(hello.get("python") or ""), + protocol=protocol, + version=str(hello.get("version") or ""), + behind=True, + ) await websocket.send_json( {"op": "refused", "reason": f"this engine speaks protocol {PROTOCOL}"} ) @@ -308,10 +333,13 @@ async def _attached_session(websocket: WebSocket, claims: dict[str, Any]) -> Non info={ "python": hello.get("python"), "venv_digest": hello.get("venv_digest"), + "protocol": protocol, + "version": hello.get("version"), }, inventory=WorkerInventory.from_hello(hello.get("inventory")), ) hub.attach(worker) + _behind.pop(name, None) await websocket.send_json({"op": "welcome", "protocol": PROTOCOL, "name": name}) try: diff --git a/backend/fluksio/cli.py b/backend/fluksio/cli.py index b187f48..6656ae5 100644 --- a/backend/fluksio/cli.py +++ b/backend/fluksio/cli.py @@ -807,9 +807,12 @@ def main(argv: list[str] | None = None) -> int: parser = _parser() if argv and argv[0] == "run": # A flow's inputs are its own, so `--lr 0.05` cannot be declared here: - # whatever this parser does not know is typed against the flow. - args, rest = parser.parse_known_args(argv) - return int(args.func(args, rest)) + # whatever this parser does not know is typed against the flow. What + # follows `--` is the flow's whatever its name, so an input called + # `timeout` is set there rather than taken as `run --timeout`. + cut = argv.index("--") if "--" in argv else len(argv) + args, rest = parser.parse_known_args(argv[:cut]) + return int(args.func(args, rest + argv[cut:])) args = parser.parse_args(argv) result: int = args.func(args) return result diff --git a/backend/fluksio/cloud/config.py b/backend/fluksio/cloud/config.py index e8abf47..d561014 100644 --- a/backend/fluksio/cloud/config.py +++ b/backend/fluksio/cloud/config.py @@ -6,10 +6,12 @@ flows repository. Deleting it is the local, unilateral way to sever the connection — the portal's tokens stop verifying immediately, whatever the portal still believes. -The portal's public keys are stored here rather than fetched: they are pinned -at enrolment, when a person was holding a claim code they had just read off the -portal's own screen. A hub whose DNS or TLS is later hijacked cannot re-key an -instance that already enrolled; it can only fail to verify. +The portal's public keys are stored here rather than fetched per request. They +are pinned at enrolment, when a person was holding a claim code they had just +read off the portal's own screen, and replaced by the set the hub sends each +time the tunnel attaches, so the hub can publish a new key beside the old one. +That makes the tunnel's TLS what a later key rests on: only a hub that answers +at the enrolled address, to this instance's own credential, can hand it one. """ from __future__ import annotations @@ -18,6 +20,7 @@ import json import logging import os import secrets +from collections.abc import Mapping from dataclasses import dataclass from typing import Any @@ -112,6 +115,23 @@ def delete() -> None: _cache = None +def via_portal(headers: Mapping[str, str]) -> bool: + """Whether the connector replayed this request off the tunnel.""" + return secrets.compare_digest(headers.get(VIA_HEADER, ""), VIA_PORTAL) + + +def portal_base(headers: Mapping[str, str]) -> str | None: + """Where this instance answers at the hub, for a request that came through it. + + The hub serves an instance under ``/i/{id}`` on the host it signs as, the + issuer enrolment pinned. ``None`` for a request made here. + """ + config = _read() + if config is None or not via_portal(headers): + return None + return f"{config.issuer.rstrip('/')}/i/{config.instance_id}" + + def decode_portal_token(token: str) -> dict[str, Any]: """Validate a token the portal minted for this instance. @@ -123,10 +143,16 @@ def decode_portal_token(token: str) -> dict[str, Any]: if config is None: raise InvalidTokenError("this instance is not enrolled with a portal") + # By `kid`, so the hub can publish the next key beside the current one and + # tokens signed with either verify while it rotates. + kid = jwt.get_unverified_header(token).get("kid") try: - key = jwt.PyJWKSet.from_dict(config.jwks).keys[0] - except (IndexError, jwt.PyJWKError) as exc: - raise InvalidTokenError(f"the pinned portal key is unusable: {exc}") from exc + keys = jwt.PyJWKSet.from_dict(config.jwks).keys + except (jwt.PyJWKError, jwt.PyJWKSetError) as exc: + raise InvalidTokenError(f"the pinned portal keys are unusable: {exc}") from exc + key = next((candidate for candidate in keys if candidate.key_id == kid), None) + if key is None: + raise InvalidTokenError("signed with a key the portal has not published") claims: dict[str, Any] = jwt.decode( token, diff --git a/backend/fluksio/cloud/connector.py b/backend/fluksio/cloud/connector.py index a6855bd..e981614 100644 --- a/backend/fluksio/cloud/connector.py +++ b/backend/fluksio/cloud/connector.py @@ -22,19 +22,26 @@ import logging import random import time import uuid +from collections.abc import MutableMapping from dataclasses import replace from datetime import timedelta from typing import Any import httpx +import jwt from fastapi import FastAPI import fluksio from fluksio.cloud import config as cloud_config +from fluksio.cloud.enroll import is_loopback logger = logging.getLogger(__name__) PROTOCOL = 1 +#: The hub closes a credential it will never accept again with this code (see +#: the hub's ``tunnel/protocol.py``). The link then stands down until enrolled +#: again or restarted, rather than dialling a deleted instance forever. +REVOKED_CLOSE = 4410 HEARTBEAT_S = 20.0 STATUS_S = 60.0 BASE_BACKOFF_S = 1.0 @@ -47,9 +54,20 @@ PING_INTERVAL_S = 20.0 PING_TIMEOUT_S = 20.0 CHUNK_BYTES = 256 * 1024 MAX_WS_MESSAGE = 1024 * 1024 -#: Only the versioned API is served over the tunnel. The MCP mount and the -#: OAuth endpoints live outside it and stay local-only. -ALLOWED_PREFIX = "/api/v1/" +#: What the tunnel serves: the versioned API, which the hub opens only to a +#: portal session; the MCP endpoint and the OAuth documents leading an agent +#: to it, which take the agent's own token; and the flows' hooks, of which +#: only those with a secret answer a request from the tunnel. Everything else, +#: the API docs included, stays on this machine. +ALLOWED_PREFIXES = ("/api/v1/", "/hooks/") +ALLOWED_PATHS = frozenset( + { + "/mcp", + "/.well-known/oauth-protected-resource/mcp", + "/.well-known/oauth-authorization-server", + "/.well-known/jwks.json", + } +) #: Proxied calls, and bridged streams, this instance will run at once. #: Neither dict was bounded, and an id the hub reused silently dropped the #: reference to a task that was still running. @@ -89,18 +107,27 @@ async def watch_enrolment(app: FastAPI) -> None: while True: await asyncio.sleep(ENROL_POLL_S) task = getattr(app.state, "cloud_task", None) - if task is not None and task.done(): - # It returns of its own accord when the config goes away, which is - # what Disconnect, here or on the portal, does. - app.state.cloud_task = None - app.state.cloud_connector = None - task = None # `load()`, not `exists()`: a file that cannot be read is not an # enrolment, and the connector would give up on it the instant it # started — which, started from here, is a restart every few seconds. # A config that is fine but unreachable keeps its task, and the # retrying belongs to the connector rather than to this. config = cloud_config.load() + if task is not None and task.done(): + connector = getattr(app.state, "cloud_connector", None) + if ( + connector is not None + and connector.revoked + and _identity(config) == getattr(app.state, "cloud_identity", None) + ): + # Stood down on the portal's word, and kept for its status. + # Only a new enrolment brings the link back. + continue + # It returns of its own accord when the config goes away, which is + # what Disconnect, here or on the portal, does. + app.state.cloud_task = None + app.state.cloud_connector = None + task = None if task is not None and _identity(config) != getattr( app.state, "cloud_identity", None ): @@ -125,6 +152,18 @@ class CloudConnector: self._connected = False self._connected_since: float | None = None self._last_error: str | None = None + self._revoked = False + #: The portal account that owns this instance, as the last welcome said. + self._owner: str | None = None + #: Where the portal opens this instance, as the last welcome said. An + #: older hub does not say. + self.app_url: str | None = None + #: What the hub sent each bridged hook socket, waiting to be received. + self._inboxes: dict[str, asyncio.Queue[dict[str, Any]]] = {} + + @property + def revoked(self) -> bool: + return self._revoked # ------------------------------------------------------------------------- # Status, for the local settings screen @@ -143,6 +182,7 @@ class CloudConnector: "instance_id": config.instance_id if config else None, "last_error": self._last_error, "connected_since": self._connected_since, + "revoked": self._revoked, } # ------------------------------------------------------------------------- @@ -174,6 +214,12 @@ class CloudConnector: raise except Exception as exc: self._last_error = str(exc) + if _is_revoked(exc): + # Dialling again would be refused the same way forever. + # The config stays, so enrolling again replaces it. + self._revoked = True + logger.warning("The portal revoked this instance; standing down") + return logger.warning("Portal link down: %s", exc) finally: self._connected = False @@ -210,7 +256,10 @@ class CloudConnector: str(welcome.get("reason") or "refused by the portal") ) - self._adopt_owner(config, welcome.get("owner")) + self._owner = welcome.get("owner") + self.app_url = welcome.get("app_url") + self._adopt_owner(config, self._owner) + self._adopt_jwks(welcome.get("jwks")) attached = True self._connected = True @@ -229,8 +278,23 @@ class CloudConnector: self._cancel(str(frame.get("id") or "")) elif op == "ws_open": self._start_stream(socket, frame) + elif op == "ws_msg": + inbox = self._inboxes.get(str(frame.get("id") or "")) + if inbox is not None: + inbox.put_nowait( + { + "type": "websocket.receive", + "text": str(frame.get("text") or ""), + } + ) elif op == "ws_close": self._cancel_stream(str(frame.get("id") or "")) + elif op == "member_removed": + await asyncio.to_thread( + self._forget_member, + str(frame.get("user") or ""), + self._owner, + ) finally: keepalive.cancel() # Awaited, not just cancelled: a keepalive that died of its own @@ -285,6 +349,56 @@ class CloudConnector: # link: everything else this connection does still works. logger.exception("Could not map the enrolling account to the portal owner") + @staticmethod + def _adopt_jwks(jwks: Any) -> None: + """Replace the pinned signing keys with the set the hub sends on attach. + + Taken only from a link that is encrypted or on this machine, the rule + enrolment applies to the portal address. Over cleartext anything on the + path could hand this instance a key of its own. + """ + config = cloud_config.load() + if config is None or not isinstance(jwks, dict) or jwks == config.jwks: + return + if not config.ws_url.startswith("wss://") and not is_loopback(config.ws_url): + return + try: + jwt.PyJWKSet.from_dict(jwks) + except jwt.PyJWTError as exc: + logger.warning("Ignoring unusable portal keys: %s", exc) + return + cloud_config.save(replace(config, jwks=jwks)) + logger.info("Took the portal's current signing keys") + + @staticmethod + def _forget_member(portal_sub: str, owner: str | None) -> None: + """Delete the local user of somebody the portal no longer admits. + + Never the owner, and never a superuser: the portal only ever creates + ordinary accounts here, so those are not its to take away. + """ + from sqlmodel import Session, select + + from fluksio.core.db import engine + from fluksio.models import User + + if not portal_sub or portal_sub == owner: + return + try: + with Session(engine) as session: + user = session.exec( + select(User).where(User.portal_sub == portal_sub) + ).first() + if user is None or user.is_superuser: + return + session.delete(user) + session.commit() + logger.info( + "Deleted %s, removed from this instance at the portal", user.email + ) + except Exception: + logger.exception("Could not delete the user the portal removed") + @staticmethod def _local_account(session: Any, config: cloud_config.CloudConfig) -> Any: """The account this instance acts as, or the one that now stands for it. @@ -419,10 +533,10 @@ class CloudConnector: call_id = str(frame["id"]) path = str(frame.get("path") or "") try: - if not path.startswith(ALLOWED_PREFIX): - # Defence in depth: the hub only ever forwards API paths, and - # an instance that trusted anything else would be an SSRF - # gadget aimed at its own process. + if not _allowed(path): + # Defence in depth: the hub forwards nothing else, and an + # instance that trusted it to would be an SSRF gadget aimed at + # its own process. await socket.send( _dump({"op": "err", "id": call_id, "message": "path not allowed"}) ) @@ -438,6 +552,9 @@ class CloudConnector: # is the only thing that makes it true, and the value is this # process's own so nothing off the network can imitate it. headers[cloud_config.VIA_HEADER] = cloud_config.VIA_PORTAL + # Addressed as the caller addressed it, which is the hub. The MCP + # endpoint checks the Host against the names it answers to. + headers["host"] = _hub_host() async with self._client() as client: request = client.build_request( @@ -532,13 +649,16 @@ class CloudConnector: stream_id = str(frame["id"]) path = str(frame.get("path") or "") query = str(frame.get("query") or "") + if _allowed(path) and path.startswith("/hooks/"): + await self._hook_stream(socket, stream_id, path, query) + return # The browser's ticket when it brought one; the hub restates the # session it checked as `token` either way, which is the tunnel's own # channel and stays when the public `?token=` goes. ticket = _query_value(query, "ticket") token = redeem_ticket(ticket) if ticket else _query_value(query, "token") - if not path.startswith(ALLOWED_PREFIX) or not path.endswith("/flows/ws"): + if not path.startswith("/api/v1/") or not path.endswith("/flows/ws"): await socket.send(_dump({"op": "ws_close", "id": stream_id, "code": 1008})) return with Session(engine) as session: @@ -600,12 +720,112 @@ class CloudConnector: _dump({"op": "ws_close", "id": stream_id, "code": 1011}) ) + async def _hook_stream( + self, socket: Any, stream_id: str, path: str, query: str + ) -> None: + """Serve a flow's socket over the tunnel, as uvicorn would serve it here. + + The app is called with a websocket scope of its own, so the route a hook + installed answers exactly as it does a local client, secret included. + """ + inbox: asyncio.Queue[dict[str, Any]] = asyncio.Queue() + inbox.put_nowait({"type": "websocket.connect"}) + self._inboxes[stream_id] = inbox + state = {"accepted": False, "closed": False} + + async def send(message: MutableMapping[str, Any]) -> None: + kind = message["type"] + if kind == "websocket.accept": + state["accepted"] = True + await socket.send(_dump({"op": "ws_open_ok", "id": stream_id})) + elif kind == "websocket.send": + text = message.get("text") + if text is None: + text = (message.get("bytes") or b"").decode(errors="replace") + await socket.send( + _dump({"op": "ws_msg", "id": stream_id, "text": text}) + ) + elif kind == "websocket.close": + state["closed"] = True + # Refused before it was accepted is a refusal whatever the code, + # so no route, a wrong secret and no secret all look alike. + code = message.get("code", 1000) if state["accepted"] else 1008 + await socket.send( + _dump({"op": "ws_close", "id": stream_id, "code": code}) + ) + + scope = { + "type": "websocket", + "asgi": {"version": "3.0"}, + "http_version": "1.1", + "scheme": "ws", + "path": path, + "raw_path": path.encode(), + "root_path": "", + "query_string": query.encode(), + "headers": [ + (b"host", _hub_host().encode()), + (cloud_config.VIA_HEADER.encode(), cloud_config.VIA_PORTAL.encode()), + ], + "client": ("127.0.0.1", 0), + "server": None, + "subprotocols": [], + } + code = 1000 + try: + await self._app(scope, inbox.get, send) + except asyncio.CancelledError: + raise + except Exception as exc: + logger.debug("Hook stream %s ended: %s", stream_id, exc) + code = 1011 + finally: + self._inboxes.pop(stream_id, None) + if not state["closed"]: + with contextlib.suppress(Exception): + await socket.send( + _dump({"op": "ws_close", "id": stream_id, "code": code}) + ) + def _cancel_stream(self, stream_id: str) -> None: task = self._streams.pop(stream_id, None) if task is not None: task.cancel() +def _allowed(path: str) -> bool: + """Whether the tunnel may serve this path. + + A dot segment is refused, literal or encoded: httpx resolves a literal one + before dispatching, so ``/hooks/../api/v1/…`` would otherwise reach the API + as a path the hub forwarded without a session. + """ + from urllib.parse import unquote + + if {".", ".."} & set(unquote(path).split("/")): + return False + return path in ALLOWED_PATHS or path.startswith(ALLOWED_PREFIXES) + + +def _hub_host() -> str: + """The host a caller addressed through the tunnel, the hub's own.""" + from urllib.parse import urlsplit + + config = cloud_config.load() + return urlsplit(config.issuer).netloc if config is not None else "instance.local" + + +def _is_revoked(exc: Exception) -> bool: + """Whether the hub closed the link as revoked, at the handshake or later.""" + from websockets.exceptions import ConnectionClosed + + return ( + isinstance(exc, ConnectionClosed) + and exc.rcvd is not None + and exc.rcvd.code == REVOKED_CLOSE + ) + + def _dump(payload: Any) -> str: import json diff --git a/backend/fluksio/cloud/enroll.py b/backend/fluksio/cloud/enroll.py index 468d1b3..5962b7e 100644 --- a/backend/fluksio/cloud/enroll.py +++ b/backend/fluksio/cloud/enroll.py @@ -38,7 +38,7 @@ class EnrollError(Exception): self.detail = detail -def _is_loopback(url: str) -> bool: +def is_loopback(url: str) -> bool: """Whether the address is this machine. Not a `*.local` name: mDNS answers it with whoever claims it on the LAN, @@ -62,7 +62,7 @@ def redeem_claim( # this address. Over plain http both are readable by anything on the path, # so refuse rather than enrol insecurely — bar a loopback portal, which is # how the stack is developed against itself. - if not base.startswith("https://") and not _is_loopback(base): + if not base.startswith("https://") and not is_loopback(base): raise EnrollError(422, "The portal address must be https:// (or this machine)") try: response = httpx.post( @@ -110,7 +110,7 @@ def enroll( token=data["instance_token"], issuer=data["issuer"], # Pinned here, at the one moment the claim code proves who we are - # talking to. Nothing refreshes this. + # talking to. The tunnel refreshes it on every attach after this. jwks=data["jwks"], local_user_id=str(user.id), enrolled_at=datetime.now(UTC).isoformat(), diff --git a/backend/fluksio/core/config.py b/backend/fluksio/core/config.py index eac2267..f88d8af 100644 --- a/backend/fluksio/core/config.py +++ b/backend/fluksio/core/config.py @@ -8,6 +8,7 @@ from pydantic import ( AnyUrl, BeforeValidator, EmailStr, + Field, HttpUrl, PositiveInt, computed_field, @@ -163,6 +164,25 @@ class Settings(BaseSettings): # grew it then gives the memory back, and the next call pays its imports # again. 0, the default, keeps every worker warm. FLOW_WORKER_MAX_RSS_MB: int = 0 + # How long a node's `stop` may take before a rebuild moves on without it. + # A broker that has gone away can leave a node waiting on an + # acknowledgement that never comes. + FLOW_NODE_STOP_TIMEOUT: float = 5.0 + # How long a request waits for a rebuild that is already running before it + # is answered as busy: one for the whole pipeline, one for a single flow's. + # Past either, the engine is wedged rather than busy. + FLOW_PIPELINE_REBUILD_WAIT: float = 15.0 + FLOW_REBUILD_WAIT: float = 5.0 + # Seconds between restarts of a node's crashed loop, one step per attempt + # with the last repeating. A JSON list in the environment. + FLOW_RESTART_BACKOFF: Annotated[tuple[float, ...], Field(min_length=1)] = ( + 1.0, + 5.0, + 30.0, + 60.0, + ) + # How long a cancelled node loop gets to stop before it is abandoned. + FLOW_CANCEL_GRACE: float = 5.0 # How long the engine's own metrics, events and run records are kept. #: The largest body `PUT /artifacts` will take, in bytes. A checkpoint or a #: video segment is a legitimate artifact, so this is generous rather than diff --git a/backend/fluksio/core/db.py b/backend/fluksio/core/db.py index 0b2d91a..833f8ba 100644 --- a/backend/fluksio/core/db.py +++ b/backend/fluksio/core/db.py @@ -66,8 +66,9 @@ def _sqlite_pragmas(dbapi_connection: Any, _record: Any) -> None: # Commit without waiting for the platter. A power cut can lose the last # transactions; it cannot corrupt the file. cursor.execute("PRAGMA synchronous=NORMAL") - # Off by default in SQLite, and `ondelete="CASCADE"` on the OAuth tables is - # the whole of revoking a client. + # Off by default in SQLite, and `ondelete="CASCADE"` is the whole of + # revoking an OAuth client and of deleting a run's nodes, metrics and + # artifacts with it. cursor.execute("PRAGMA foreign_keys=ON") # A second writer waits rather than raising "database is locked". cursor.execute("PRAGMA busy_timeout=30000") @@ -109,17 +110,30 @@ def _alembic_config(connection: Any) -> Config: def migrate(engine: Engine) -> None: """Bring the schema up to date, creating it if there is nothing there.""" - with engine.begin() as connection: - config = _alembic_config(connection) - tables = inspect(connection).get_table_names() - if not tables: - # Nothing to upgrade from. Building the schema from the models is - # both faster and exactly what the revisions would have produced. - SQLModel.metadata.create_all(connection) - command.stamp(config, "head") - logger.info("created the database schema") - return - command.upgrade(config, "head") + with engine.connect() as connection: + # Foreign keys off while the schema changes. A batch migration rebuilds + # a table by dropping it, and dropping `run` with them on cascades into + # every run's nodes, metrics and artifacts. SQLite ignores the pragma + # inside a transaction, so it is set before the migration's opens and + # reset after it closes. + connection.exec_driver_sql("PRAGMA foreign_keys=OFF") + connection.commit() + try: + with connection.begin(): + config = _alembic_config(connection) + tables = inspect(connection).get_table_names() + if not tables: + # Nothing to upgrade from. Building the schema from the + # models is both faster and exactly what the revisions + # would have produced. + SQLModel.metadata.create_all(connection) + command.stamp(config, "head") + logger.info("created the database schema") + return + command.upgrade(config, "head") + finally: + connection.exec_driver_sql("PRAGMA foreign_keys=ON") + connection.commit() def init_db(session: Session) -> None: diff --git a/backend/fluksio/flow/artifacts.py b/backend/fluksio/flow/artifacts.py index a1d5ad5..6515be3 100644 --- a/backend/fluksio/flow/artifacts.py +++ b/backend/fluksio/flow/artifacts.py @@ -160,6 +160,19 @@ class ArtifactStore: self.put_file(source) return True + def delete(self, digest: str) -> bool: + """Remove one artifact's bytes. Returns whether this store had them. + + The ring is not looked in: its frames fall out on their own. + """ + if not valid_digest(digest): + return False + try: + self._path(digest).unlink() + except FileNotFoundError: + return False + return True + def read(self, digest: str) -> Iterator[bytes]: target = self.path(digest) if target is None: diff --git a/backend/fluksio/flow/controller.py b/backend/fluksio/flow/controller.py index 3e8cdfc..ba631d1 100644 --- a/backend/fluksio/flow/controller.py +++ b/backend/fluksio/flow/controller.py @@ -40,6 +40,7 @@ from fluksio.flow.messages import MessageSpec, flow_of, qualify from fluksio.flow.nodes import ( RESERVED_SETTINGS, ChangeNode, + DebounceNode, DelayNode, ExecNode, FileNode, @@ -53,7 +54,6 @@ from fluksio.flow.nodes import ( NtfyNode, RbeNode, SwitchNode, - TriggerNode, WebSocketNode, WrenNode, ) @@ -97,7 +97,7 @@ HOOK_PREFIX = "/hooks" def _is_hook(node: Node | None) -> TypeGuard[HttpNode | WebSocketNode]: """Mounted under HOOK_PREFIX: a webhook, or a socket the engine serves.""" - return (isinstance(node, HttpNode) and node.mode is HttpNode.Mode.TRIGGER) or ( + return (isinstance(node, HttpNode) and node.mode is HttpNode.Mode.LISTENER) or ( isinstance(node, WebSocketNode) and node.serves ) @@ -106,7 +106,7 @@ def _is_hook(node: Node | None) -> TypeGuard[HttpNode | WebSocketNode]: # A node's `stop` talks to whatever it connected to, and a broker that has gone # away can leave it waiting for an acknowledgement that never arrives — which # used to hold the rebuild, and everything queued behind it, forever. -NODE_STOP_TIMEOUT = 5.0 +NODE_STOP_TIMEOUT = settings.FLOW_NODE_STOP_TIMEOUT # How long a rebuild asked for by a request waits for one already running. # Generous on purpose: a rebuild of a populated instance reconnects every @@ -114,14 +114,14 @@ NODE_STOP_TIMEOUT = 5.0 # healthy one of those should not be turned away. Past that the controller is # wedged rather than busy, and an error the caller can act on beats a request # that never ends. -REBUILD_WAIT = 15.0 +REBUILD_WAIT = settings.FLOW_PIPELINE_REBUILD_WAIT # How long a caller waits for a *flow* rebuild that is already running. A # per-flow rebuild reconnects one flow's nodes rather than the instance's, # so this is a queueing budget — several of them back to back, which is what # seeding does — not the room a single one needs. Fifteen seconds was sized for # the whole-pipeline rebuild and would let a wedge sit unreported. -FLOW_REBUILD_WAIT = 5.0 +FLOW_REBUILD_WAIT = settings.FLOW_REBUILD_WAIT # How long a node has to have been down before it is shown as down. A device # that misses one poll and comes back is a flake, not a fault, and without this @@ -174,6 +174,8 @@ class LoadedNode: #: Published, and with no body of its own — so what runs is the new-node #: template, which returns nothing at all. missing_source: bool = False + #: The device this node names although its type never runs on a worker. + ignored_device: str = "" @dataclass @@ -338,13 +340,13 @@ NODE_TYPES: dict[str, NodeType] = { ), "mqtt": NodeType( title="MQTT", - description="Subscribe to topics, or publish what arrives on its inputs.", + description="Listen on topics, or send to them.", cls=MqttNode, params_schema=_schema_of(MqttNode), ), "http": NodeType( title="HTTP", - description="Receive data on a webhook, or send it to a URL.", + description="Listen on a webhook, or send to a URL and pass the reply on.", cls=HttpNode, params_schema=_schema_of(HttpNode), ), @@ -408,11 +410,11 @@ NODE_TYPES: dict[str, NodeType] = { cls=JoinNode, params_schema=_schema_of(JoinNode), ), - "trigger": NodeType( - title="Trigger", + "debounce": NodeType( + title="Debounce", description="Send one value now and another once things go quiet.", - cls=TriggerNode, - params_schema=_schema_of(TriggerNode), + cls=DebounceNode, + params_schema=_schema_of(DebounceNode), ), "exec": NodeType( title="Command", @@ -837,7 +839,8 @@ class FlowController: async def reload_lib_users(self, ref: str) -> list[str]: """Rebuild the flows running a shared node whose source has changed.""" - flows = sorted({usage.split(".", 1)[0] for usage in self.store.usages(ref)}) + usages = self.store.usages(ref, draft=False) + flows = sorted({usage.split(".", 1)[0] for usage in usages}) for flow in flows: await self.reload_flow(flow) return flows @@ -976,7 +979,7 @@ class FlowController: with placer.claim( # type: ignore[union-attr] wanted, device=device, policy=policy, node=node_id, run=run_id ) as (target, allocation): - env = derive_env(wanted, allocation) + env = derive_env(wanted, allocation, local=target.worker is None) if target.worker is None: return pool.for_env(env).run( # type: ignore[union-attr] owner, @@ -1040,6 +1043,8 @@ class FlowController: node_type = NODE_TYPES.get(node_def.type) if node_type is None: raise ValueError(f"Unknown node type '{node_def.type}'") + if node_def.device and not node_type.has_source: + entry.ignored_device = node_def.device params = resolve_params(node_def.params) @@ -1211,7 +1216,8 @@ class FlowController: # reaches hash to, which `sync` worked out by following # its imports. The ports are here because declaring a new # one changes what the node answers with, which a hit - # would otherwise restore without. + # would otherwise restore without. Changing what goes in + # here changes every key: move `CACHE_SHAPE_SINCE` with it. node.fingerprint = hashlib.sha256( json.dumps( { @@ -1831,6 +1837,93 @@ def _signature_gap( ) +def follow_header(node: NodeDef, code: str) -> NodeDef: + """The node with its input ports and settings read off ``def process(...)``. + + The inverse of the canvas's scaffold, which writes the input ports first, + in their order, and the settings after them. A parameter already declared + keeps its kind. A new one is a setting when it has a default, which becomes + its value, or when it follows a setting, and an input port otherwise. One + name swapped for one other is a rename, so the port keeps its message and + the setting its value. A declared name the header no longer has is dropped. + + A header this cannot read for certain leaves the node as it is: a syntax + error, no top-level ``def process``, a decorator, ``*args``, ``**kwargs``, + a bare ``*`` or ``/``. + """ + try: + tree = ast.parse(code) + except SyntaxError: + return node + defs = [ + entry + for entry in tree.body + if isinstance(entry, ast.FunctionDef) and entry.name == "process" + ] + if not defs: + return node + args = defs[-1].args + if ( + defs[-1].decorator_list + or args.vararg + or args.kwarg + or args.posonlyargs + or args.kwonlyargs + ): + return node + # Defaults belong to the last parameters, so they line up from the end. + defaults: list[ast.expr | None] = [None] * ( + len(args.args) - len(args.defaults) + ) + list(args.defaults) + header = [ + (arg.arg, default) + for arg, default in zip(args.args, defaults, strict=True) + if arg.arg not in RESERVED_SETTINGS + ] + + def written(name: str) -> bool: + return name.isascii() and name.isidentifier() + + ports = {spec.port: spec for spec in node.requires if written(spec.port)} + settings = { + key: value + for key, value in node.params.items() + if key not in RESERVED_SETTINGS and written(key) + } + names = [name for name, _ in header] + gone = [name for name in [*ports, *settings] if name not in names] + new = [name for name in names if name not in ports and name not in settings] + renamed = {new[0]: gone[0]} if len(gone) == len(new) == 1 else {} + + requires: list[MessageSpec] = [] + params = {key: value for key, value in node.params.items() if key not in settings} + after_setting = False + for name, default in header: + was = renamed.get(name, name) + if was in ports: + requires.append(ports[was].model_copy(update={"port": name})) + elif was in settings or default is not None or after_setting: + params[name] = settings[was] if was in settings else _literal(default) + after_setting = True + else: + requires.append(MessageSpec(port=name)) + # Ports the header cannot name, such as a row not named yet, stay. + requires += [spec for spec in node.requires if not written(spec.port)] + return node.model_copy(update={"requires": requires, "params": params}) + + +def _literal(default: ast.expr | None) -> Any: + """A parameter's default as a setting's value, or empty when it is not JSON.""" + if default is None: + return "" + try: + value = ast.literal_eval(default) + json.dumps(value) + except (ValueError, TypeError, SyntaxError, RecursionError): + return "" + return value + + def with_settings( function: Callable[..., Any], params: dict[str, Any] ) -> Callable[..., Any]: @@ -1922,6 +2015,22 @@ def _collect_issues( for entry in loaded.values() if entry.missing_source ] + # Only code travels to a worker, so a device on any other node is a field + # nothing reads. Saying so beats a setting that looks like it took. + issues += [ + ValidationIssue( + code="ignored_device", + message=( + f"Node '{entry.id.rpartition('.')[2]}' names device " + f"'{entry.ignored_device}', but only a Function node runs on a " + "worker. The setting is ignored." + ), + flow=entry.flow, + node=entry.id, + ) + for entry in loaded.values() + if entry.ignored_device + ] return issues diff --git a/backend/fluksio/flow/dashboards.py b/backend/fluksio/flow/dashboards.py index 01319a6..a1c34ba 100644 --- a/backend/fluksio/flow/dashboards.py +++ b/backend/fluksio/flow/dashboards.py @@ -598,6 +598,9 @@ class DashboardSummary(BaseModel): version: int = 1 #: The grid `footprint` is placed in — see ``DashboardDef.columns``. columns: int = 12 + #: A round dashboard's widgets are faces rather than tiles, so a list + #: draws them as a circle instead of at the grid places they carry. + shape: Literal["rect", "round"] = "rect" #: Every widget's shape, so a list can draw the dashboard's outline #: without a document read per tile. footprint: list[WidgetFootprint] = Field(default_factory=list) @@ -666,6 +669,7 @@ class DashboardStore: has_draft=defn.has_draft, version=defn.version, columns=defn.columns, + shape=defn.shape, footprint=defn.footprint, updated_at=self.updated_at(name), ) diff --git a/backend/fluksio/flow/executor.py b/backend/fluksio/flow/executor.py index 0778dfb..c183a9c 100644 --- a/backend/fluksio/flow/executor.py +++ b/backend/fluksio/flow/executor.py @@ -440,13 +440,6 @@ class ExecutionService: self.queue.park(node.flow, item) return True - if item.kind == "flush": - # A rate-limit window ended; nothing to replay, only to let out. - # ponytail: no run record for a flush — it is the tail of the run - # that scheduled it, not a run of its own. - pipeline.flush(node) - return True - if item.guard_key and str(node.recall(item.guard_key, "")) != item.guard_value: # The node moved on while this waited — a restarted timer, say. logger.debug("Guard no longer holds for '%s', dropped", item.node) @@ -461,6 +454,7 @@ class ExecutionService: "flow": item.flow, "node": item.node, "cause": item.cause, + "parent": item.parent, "deliveries": item.deliveries, "ts": now, } @@ -483,6 +477,11 @@ class ExecutionService: replay = item.deliveries > 1 emission = item.kind == "emission" try: + if item.kind == "flush": + # A rate-limit window ended; nothing to replay, only to let out. + # A run of its own, so what it sets off is recorded somewhere. + pipeline.flush(node, item.entry_id) + return True # An emission's values went into state when the node produced them; # this item carries them so its readers get the chunk that caused # the wave rather than whichever is newest by the time they run. @@ -491,7 +490,7 @@ class ExecutionService: published = ( set(item.outputs) if emission - else pipeline.apply_outputs(node, item.outputs or None) + else pipeline.apply_outputs(node, item.outputs or None, item.entry_id) ) pipeline.run_downstream( node, diff --git a/backend/fluksio/flow/logs.py b/backend/fluksio/flow/logs.py index 9cb60bf..e8f1957 100644 --- a/backend/fluksio/flow/logs.py +++ b/backend/fluksio/flow/logs.py @@ -36,17 +36,19 @@ class _Tee(io.TextIOBase): sink(text) try: return self._real.write(text) - except OSError: + except (OSError, ValueError): # A dead stdout — `fluksio serve` runs the engine as a child of the # dashboard, which holds the far end of that pipe — must not fail # the node whose output was being teed. The capture above has it, - # and there is nowhere left to report the loss to anyway. + # and there is nowhere left to report the loss to anyway. A closed + # one (ValueError) is the same at interpreter exit, when whatever + # was wrapped — pytest's capture, say — has been closed first. return len(text) def flush(self) -> None: try: self._real.flush() - except OSError: + except (OSError, ValueError): pass def isatty(self) -> bool: diff --git a/backend/fluksio/flow/messages.py b/backend/fluksio/flow/messages.py index 3629e87..fc09483 100644 --- a/backend/fluksio/flow/messages.py +++ b/backend/fluksio/flow/messages.py @@ -12,6 +12,7 @@ from __future__ import annotations import json import math +import re from enum import Enum from typing import Any @@ -320,6 +321,24 @@ class MessageSpec(BaseModel): return hash((self.name, self.port)) +#: A message named with its flow: the flow's own name, then the name inside it. +#: The one rule for anywhere a name has to arrive qualified; the frontend holds +#: a copy (``QUALIFIED_MESSAGE`` in ``deriveEdges.ts``). +QUALIFIED_PATTERN = re.compile(r"[a-z][a-z0-9_]*(\.[A-Za-z0-9_]+)+") + +QUALIFIED_RULE = ( + "Name the message as flow.name: a lowercase flow name, a dot, " + "then letters, digits and underscores" +) + + +def validate_qualified(value: str) -> str: + """A qualified message name, or a ``ValueError`` saying what one looks like.""" + if not QUALIFIED_PATTERN.fullmatch(value): + raise ValueError(QUALIFIED_RULE) + return value + + def qualify(flow: str, name: str) -> str: """Resolve a message name against its flow namespace.""" if not name: diff --git a/backend/fluksio/flow/metrics.py b/backend/fluksio/flow/metrics.py index 7aaa441..b2f7d3f 100644 --- a/backend/fluksio/flow/metrics.py +++ b/backend/fluksio/flow/metrics.py @@ -103,6 +103,7 @@ class MetricsCollector: # held with the time it arrived so an unpaired one does not stay put. self._tracebacks: dict[tuple[str, str], tuple[float, str]] = {} self._last_prune = 0.0 + self._queue: asyncio.Queue[dict[str, Any]] | None = None # ------------------------------------------------------------------------- # The loop @@ -112,6 +113,7 @@ class MetricsCollector: """Consume the bus until cancelled, flushing on a fixed interval.""" last = time.monotonic() async with self._events.subscribe() as queue: + self._queue = queue while True: # A busy bus never idles, so the flush is on a deadline rather # than on the timeout alone. @@ -129,6 +131,21 @@ class MetricsCollector: await self.flush() last = time.monotonic() + async def flush_now(self) -> None: + """Write what has been heard so far, without waiting for the interval. + + Events the bus has handed over and the loop has not reached yet are + taken first, so whoever caused an event and then asks for a flush finds + it written. + """ + while self._queue is not None and not self._queue.empty(): + event = self._queue.get_nowait() + try: + self.handle(event) + except Exception: + logger.exception("Could not record %s", event.get("type")) + await self.flush() + # ------------------------------------------------------------------------- # Accumulating # ------------------------------------------------------------------------- @@ -291,6 +308,7 @@ class MetricsCollector: "id": run_id, "flow": str(event.get("flow") or "")[:NAME_MAX], "source": str(event.get("cause") or ""), + "parent": str(event.get("parent") or "")[:64], "started_ts": ts, "started_at": datetime.fromtimestamp(ts, UTC), "finished_at": None, diff --git a/backend/fluksio/flow/modules.py b/backend/fluksio/flow/modules.py index cbb9598..ebee5a7 100644 --- a/backend/fluksio/flow/modules.py +++ b/backend/fluksio/flow/modules.py @@ -6,9 +6,12 @@ into a venv on the data volume rather than into the engine's environment: a pin here can never shadow — or be shadowed by — what the app itself runs on, and the worker processes that import them need nothing from the app. -``uv pip sync`` rather than install, so a line taken out of the manifest is -uninstalled. The manifest is written only after a sync succeeds, which is all -the rollback a failed resolve needs. +The manifest is resolved with ``uv pip compile`` first, so a line naming a +package brings its dependencies along, and a manifest that already pins +everything resolves to itself. The result goes to ``uv pip sync`` rather than +install, so a line taken out of the manifest is uninstalled. The manifest is +written only after a sync succeeds, which is all the rollback a failed resolve +needs. None of that applies to an *adopted* venv. A data scientist makes a venv, installs what they work with, and then installs Fluksio into it too — at which @@ -179,35 +182,52 @@ def sync(requirements: str) -> tuple[bool, str]: def _sync(requirements: str) -> tuple[bool, str]: - """The install itself, under `_sync_lock`.""" - manifest = "" + """The resolve and install, under `_sync_lock`.""" try: ensure_venv() - with tempfile.NamedTemporaryFile("w", suffix=".txt", delete=False) as handle: - handle.write(requirements) - manifest = handle.name - result = subprocess.run( - # Empty means empty: without the flag uv refuses to clear a venv, - # so deleting the last line would leave the package installed. - [ - uv_bin(), - "pip", - "sync", - "--allow-empty-requirements", - "--python", - venv_python(), - manifest, - ], - capture_output=True, - text=True, - timeout=SYNC_TIMEOUT, - ) + python = venv_python() + with tempfile.TemporaryDirectory() as directory: + manifest = Path(directory, "requirements.in") + lock = Path(directory, "requirements.txt") + manifest.write_text(requirements) + for command in ( + # The emit flags carry the manifest's index, find-links and + # binary options into the lock, which is all sync reads. + [ + uv_bin(), + "pip", + "compile", + "--quiet", + "--emit-index-url", + "--emit-find-links", + "--emit-build-options", + "--python", + python, + str(manifest), + "--output-file", + str(lock), + ], + # Empty means empty: without the flag uv refuses to clear a + # venv, so deleting the last line would leave the package + # installed. + [ + uv_bin(), + "pip", + "sync", + "--allow-empty-requirements", + "--python", + python, + str(lock), + ], + ): + result = subprocess.run( + command, capture_output=True, text=True, timeout=SYNC_TIMEOUT + ) + if result.returncode != 0: + break except (OSError, subprocess.SubprocessError) as exc: detail = str(getattr(exc, "stderr", None) or exc).strip() return False, f"uv could not run: {detail}" - finally: - if manifest: - Path(manifest).unlink(missing_ok=True) output = (result.stdout + result.stderr).strip() if result.returncode == 0: diff --git a/backend/fluksio/flow/nodes/__init__.py b/backend/fluksio/flow/nodes/__init__.py index 4f91e03..1973a54 100644 --- a/backend/fluksio/flow/nodes/__init__.py +++ b/backend/fluksio/flow/nodes/__init__.py @@ -5,6 +5,7 @@ Split by the outside world each one talks to. Importing from """ from fluksio.flow.nodes.base import RESERVED_SETTINGS, Node +from fluksio.flow.nodes.debounce import DebounceNode from fluksio.flow.nodes.delay import DelayNode from fluksio.flow.nodes.exec import ExecNode from fluksio.flow.nodes.file import FileNode @@ -15,12 +16,12 @@ from fluksio.flow.nodes.logic import ChangeNode, JoinNode, RbeNode, SwitchNode from fluksio.flow.nodes.mlp import MLPNode from fluksio.flow.nodes.mqtt import MqttNode from fluksio.flow.nodes.ntfy import NtfyNode -from fluksio.flow.nodes.trigger import TriggerNode from fluksio.flow.nodes.websocket import WebSocketNode from fluksio.flow.nodes.wren import WrenNode __all__ = [ "ChangeNode", + "DebounceNode", "DelayNode", "ExecNode", "FileNode", @@ -35,7 +36,6 @@ __all__ = [ "RESERVED_SETTINGS", "RbeNode", "SwitchNode", - "TriggerNode", "WebSocketNode", "WrenNode", ] diff --git a/backend/fluksio/flow/nodes/base.py b/backend/fluksio/flow/nodes/base.py index a222143..d8fb937 100644 --- a/backend/fluksio/flow/nodes/base.py +++ b/backend/fluksio/flow/nodes/base.py @@ -90,6 +90,7 @@ class Node: "_pipeline", "synchronous", "_on_health", + "_health", "supervisor", "fingerprint", ) @@ -105,6 +106,9 @@ class Node: self.f = f self._pipeline: Pipeline | None = None self._on_health: Callable[[Node, str, str | None], None] | None = None + # What this node reported since its background loop last started, so + # a crash does not overwrite the reason the loop gave for being down. + self._health: str | None = None # Set by the controller before start(); a node built on its own (tests, # previews) runs its loops unsupervised. self.supervisor: Supervisor | None = None @@ -206,11 +210,25 @@ class Node: Returns the task only when unsupervised, since that is the one case where the caller has to cancel it itself. + + A loop that raises reports the node down with the exception, unless it + already said why it is down. A push session that fails before it + listens would otherwise show up as a crash event and nowhere else. """ + + async def reported() -> None: + self._health = None + try: + await factory() + except Exception as exc: + if self._health != "down": + self.report_health("down", f"{type(exc).__name__}: {exc}") + raise + if self.supervisor is not None: - self.supervisor.spawn(f"{self.id}:{name}", self.flow, factory) + self.supervisor.spawn(f"{self.id}:{name}", self.flow, reported) return None - return asyncio.create_task(factory()) + return asyncio.create_task(reported()) @staticmethod async def _cancel_task(task: asyncio.Task[None]) -> None: @@ -230,6 +248,7 @@ class Node: def report_health(self, status: str, detail: str | None = None) -> None: """Say how this node's connection is doing: ok, degraded or down.""" + self._health = status if self._on_health is not None: self._on_health(self, status, detail) @@ -392,7 +411,7 @@ class Node: Trigger this node externally, executing downstream nodes if dependencies are met. This method is for nodes that receive data from upstream dependencies. - For trigger/subscriber nodes that inject data into the pipeline, use :meth:`inject`. + For listener nodes that inject data into the pipeline, use :meth:`inject`. :param inputs: Input values matching this node's ``requires``. :type inputs: dict | None @@ -410,7 +429,7 @@ class Node: """ Inject data into the pipeline as if this node produced it. - This method is for trigger/subscriber nodes that receive external data + This method is for listener nodes that receive external data (e.g., HTTP requests, MQTT messages) and need to inject it into the pipeline. The data is validated against this node's ``provides`` specification. diff --git a/backend/fluksio/flow/nodes/trigger.py b/backend/fluksio/flow/nodes/debounce.py similarity index 90% rename from backend/fluksio/flow/nodes/trigger.py rename to backend/fluksio/flow/nodes/debounce.py index d85d21e..3c61e66 100644 --- a/backend/fluksio/flow/nodes/trigger.py +++ b/backend/fluksio/flow/nodes/debounce.py @@ -1,8 +1,8 @@ -"""Trigger: send one thing now, another if nothing follows. +"""Debounce: send one thing now, another if nothing follows. -Node-RED's *trigger*. The shape it exists for is "the door opened — turn the -light on, and off again in two minutes unless it opens again", which is -awkward to express any other way. +Node-RED calls this node *trigger*. The shape it exists for is "the door +opened — turn the light on, and off again in two minutes unless it opens +again", which is awkward to express any other way. """ from __future__ import annotations @@ -19,7 +19,7 @@ from fluksio.flow.nodes.base import Node logger = logging.getLogger(__name__) -class TriggerNode(Node): +class DebounceNode(Node): """Emit ``first`` on arrival, then ``then`` once the wait runs out. A value arriving during the wait extends it, so the second emission only @@ -67,17 +67,17 @@ class TriggerNode(Node): ): self.cfg = self.Params.model_validate(params or {}) super().__init__( - f=self._trigger, + f=self._debounce, requires=requires, provides=provides, params=params, - name=name or "trigger", + name=name or "debounce", ) def _outputs(self, value: Any) -> dict[str, Any]: return {spec.port: value for spec in self.output_ports} - def _trigger(self, params: dict[str, Any], **kwargs: Any) -> dict[str, Any] | None: + def _debounce(self, params: dict[str, Any], **kwargs: Any) -> dict[str, Any] | None: if not kwargs or not self.output_ports: return None diff --git a/backend/fluksio/flow/nodes/delay.py b/backend/fluksio/flow/nodes/delay.py index a9d448e..d8d23f7 100644 --- a/backend/fluksio/flow/nodes/delay.py +++ b/backend/fluksio/flow/nodes/delay.py @@ -23,7 +23,8 @@ class DelayNode(Node): This node can: - Add a fixed delay before forwarding messages - - Rate-limit messages to a minimum interval between forwards + - Rate-limit messages to a minimum interval between forwards, sending the + latest one held inside the window when the window ends - Schedule emissions using crontab syntax (like a cron job) The node passes through all input values to outputs with matching names. @@ -34,7 +35,7 @@ class DelayNode(Node): - Without ``requires``: Emits current timestamp at each cron tick - With ``requires``: Emits last received input at each cron tick - **Execution order:** rate check → delay → forward + **Execution order:** delay → rate check → forward :param params: Parameters dict containing: - ``delay`` (int): Fixed delay in seconds before forwarding (default: 0) @@ -89,7 +90,6 @@ class DelayNode(Node): __slots__ = ( "delay", "interval", - "ts", "mapping", "cron_expr", "last_input", @@ -113,11 +113,18 @@ class DelayNode(Node): self.interval = cfg.interval self.mapping = cfg.mapping self.cron_expr = cfg.cron - self.ts = 0.0 self.last_input: dict[str, Any] = {} self._cron_task: asyncio.Task[None] | None = None self._stop_cron: asyncio.Event | None = None + if cfg.interval > 0: + # The engine's own rate limit, on every output: it keeps the latest + # value inside the window and publishes it when the window ends. + kwargs["provides"] = [ + spec.model_copy(update={"interval": max(spec.interval, cfg.interval)}) + for spec in self._normalize_ports(kwargs.get("provides", ())) + ] + super().__init__(f=self._f, params=params, **kwargs) def _port_pairs(self) -> list[tuple[str, str]]: @@ -130,7 +137,7 @@ class DelayNode(Node): ] def _f(self, params: dict[str, Any], **kwargs: Any) -> dict[str, Any] | None: - """Forward messages with optional delay, rate-limiting, and alarm.""" + """Forward messages, after a delay if one is set.""" logger.debug("[%s] Received %s", self.name, kwargs) @@ -138,14 +145,6 @@ class DelayNode(Node): if kwargs: self.last_input = dict(kwargs) - # Apply rate limiting first - if self.interval > 0: - ts = time.time() - if ts <= self.ts + self.interval: - logger.debug("[%s] Stashing %s", self.name, kwargs) - return None - self.ts = ts - if not kwargs: return None diff --git a/backend/fluksio/flow/nodes/http.py b/backend/fluksio/flow/nodes/http.py index d9b13c8..0877cf1 100644 --- a/backend/fluksio/flow/nodes/http.py +++ b/backend/fluksio/flow/nodes/http.py @@ -1,4 +1,4 @@ -"""HTTP nodes: an inbound webhook trigger and an outbound request sender.""" +"""HTTP nodes: an inbound webhook listener and an outbound request sender.""" from __future__ import annotations @@ -81,50 +81,50 @@ def _first_catch_all(app: FastAPI) -> int: class HttpNode(Node): """ - HTTP node that can act as a trigger (receiver) or sender based on configuration. + HTTP node that acts as a listener (webhook) or a sender. This node integrates with FastAPI to either: - - **Trigger mode**: Receive incoming HTTP requests (GET/POST) and inject data + - **Listener**: Receive incoming HTTP requests (GET/POST) and inject data into the pipeline. Used when ``provides`` is specified but ``requires`` is empty. - - **Sender mode**: Make outgoing HTTP requests with pipeline data. Used when - ``requires`` is specified. + - **Sender**: Make outgoing HTTP requests with pipeline data, and publish a + JSON object response on the outputs. Used when ``requires`` is specified. - :param url: The URL endpoint. For trigger mode, this is the route path - (e.g., "/sensors/temperature"). For sender mode, this is the full URL + :param url: The URL endpoint. For a listener, this is the route path + (e.g., "/sensors/temperature"). For a sender, this is the full URL to send requests to. :type url: str :param method: HTTP method - "GET" or "POST". :type method: Literal["GET", "POST"] - :param requires: Messages required by this node (makes it a sender node). + :param requires: Messages required by this node (makes it a sender). :type requires: MessageSpec | list[MessageSpec] | None - :param provides: Messages provided by this node (makes it a trigger node). + :param provides: Messages provided by this node (makes it a listener). :type provides: MessageSpec | list[MessageSpec] | None :param params: Additional parameters for the node. :type params: dict :param name: Optional name for the node. :type name: str | None - :param timeout: Request timeout in seconds (for sender mode). + :param timeout: Request timeout in seconds (sender only). :type timeout: float :param headers: Additional HTTP headers. :type headers: dict[str, str] | None - :param secret: Shared secret callers append to the webhook URL (trigger mode). + :param secret: Shared secret callers append to the webhook URL (listener only). :type secret: str :raises ValueError: If both ``requires`` and ``provides`` are empty, or if the configuration is invalid. :example: - Trigger node (receives POST requests): + Listener (receives POST requests): - >>> trigger = HttpNode( + >>> listener = HttpNode( ... url="/api/sensors/temperature", ... method="POST", ... provides=[MessageSpec(name="temperature", dtype=DType.FLOAT)], ... params={}, ... ) - Sender node (makes POST requests): + Sender (makes POST requests): >>> sender = HttpNode( ... url="https://api.example.com/data", @@ -137,7 +137,7 @@ class HttpNode(Node): class Mode(Enum): """Operating mode of the HTTP node.""" - TRIGGER = "trigger" # Receives HTTP requests + LISTENER = "listener" # Receives HTTP requests SENDER = "sender" # Sends HTTP requests # A repeated request is a repeated request, whatever the endpoint does @@ -159,14 +159,29 @@ class HttpNode(Node): class Params(BaseModel): model_config = ConfigDict(extra="allow") - url: str - method: Literal["GET", "POST"] = "POST" - timeout: float = 30.0 - headers: dict[str, str] = {} + url: str = Field( + description=( + "A path such as /tick, served as a webhook at /hooks//, " + "or the full URL a sender requests." + ) + ) + method: Literal["GET", "POST"] = Field( + default="POST", + description="The method a webhook accepts or a sender uses.", + ) + timeout: float = Field( + default=30.0, description="Seconds a sender waits for the response." + ) + headers: dict[str, str] = Field( + default={}, + description=( + "Request headers, sender only. A value may be a secret reference." + ), + ) query: dict[str, Any] = Field( default_factory=dict, description=( - "Fixed query parameters, sender mode. A value may be a secret " + "Fixed query parameters, sender only. A value may be a secret " "reference, which is how an API key stays out of the flow file." ), ) @@ -228,7 +243,7 @@ class HttpNode(Node): ) # Inputs mean this node sends data out; outputs mean it receives. - self.mode = HttpNode.Mode.SENDER if requires else HttpNode.Mode.TRIGGER + self.mode = HttpNode.Mode.SENDER if requires else HttpNode.Mode.LISTENER self.url = cfg.url self.method = cfg.method.upper() @@ -245,12 +260,12 @@ class HttpNode(Node): name = f"http_{self.mode.value}_{safe_url}" # Initialize parent with appropriate function - # For trigger mode, f is a no-op since data is injected via inject() - # For sender mode, f handles the outgoing HTTP request + # For a listener, f is a no-op since data is injected via inject() + # For a sender, f handles the outgoing HTTP request super().__init__( f=( - self._noop_trigger - if self.mode == HttpNode.Mode.TRIGGER + self._noop_listener + if self.mode == HttpNode.Mode.LISTENER else self._sender_handler ), requires=requires, @@ -260,11 +275,11 @@ class HttpNode(Node): ) @staticmethod - def _noop_trigger(params: dict[str, Any], **kwargs: Any) -> dict[str, Any] | None: + def _noop_listener(params: dict[str, Any], **kwargs: Any) -> dict[str, Any] | None: """ - No-op function for trigger mode nodes. + No-op function for listener nodes. - Trigger mode nodes inject data via :meth:`inject`, not :meth:`__call__`. + Listener nodes inject data via :meth:`inject`, not :meth:`__call__`. This function exists only to satisfy the Node interface. """ return None @@ -273,7 +288,7 @@ class HttpNode(Node): self, params: dict[str, Any], **kwargs: Any ) -> dict[str, Any] | None: """ - Send HTTP request with pipeline data (sender mode). + Send HTTP request with pipeline data (sender). This method is called when upstream dependencies are satisfied. It sends the required data via HTTP request. @@ -286,7 +301,7 @@ class HttpNode(Node): :type params: dict :param kwargs: Pipeline data to send (from required messages). :type kwargs: Any - :returns: Response data if the endpoint returns JSON, None otherwise. + :returns: The reply's values for the declared output ports, or None. :rtype: dict | None """ client = shared_client() @@ -315,8 +330,13 @@ class HttpNode(Node): except Exception: return None # Outputs are keyed by port, so only an object can be one. A bare - # scalar or list is a valid reply, just not something to publish. - return body if isinstance(body, dict) else None + # scalar or list is a valid reply, just not something to publish, + # and neither is a key no port declares: the reply is the + # endpoint's, and a `status` beside the values is common. + if not isinstance(body, dict): + return None + ports = {spec.port for spec in self.output_ports} + return {key: value for key, value in body.items() if key in ports} or None except httpx.HTTPStatusError as e: logger.error( @@ -332,37 +352,37 @@ class HttpNode(Node): async def start(self, app: FastAPI | None = None) -> None: """A webhook needs a route; a sender reaches out on its own.""" - if self.mode is HttpNode.Mode.TRIGGER and app is not None: + if self.mode is HttpNode.Mode.LISTENER and app is not None: self.register_route(app) async def stop(self, app: FastAPI | None = None) -> None: - if self.mode is HttpNode.Mode.TRIGGER and app is not None: + if self.mode is HttpNode.Mode.LISTENER and app is not None: self.unregister_route(app) def register_route(self, app: FastAPI) -> None: """ Register this node's HTTP endpoint with a FastAPI application. - This method should only be called for trigger mode nodes. It creates + This method should only be called for listener nodes. It creates a route that, when called, triggers the node in the pipeline. :param app: The FastAPI application instance. :type app: FastAPI - :raises RuntimeError: If called on a sender mode node. + :raises RuntimeError: If called on a sender. :example: >>> from fastapi import FastAPI >>> app = FastAPI() - >>> trigger_node = HttpNode( + >>> listener = HttpNode( ... url="/sensors/data", ... method="POST", ... provides=[MessageSpec(name="value", dtype=DType.FLOAT)], ... params={}, ... ) - >>> trigger_node.register_route(app) + >>> listener.register_route(app) """ - if self.mode != HttpNode.Mode.TRIGGER: - raise RuntimeError("Can only register routes for trigger mode nodes") + if self.mode != HttpNode.Mode.LISTENER: + raise RuntimeError("Can only register routes for listener nodes") if self._route_registered: return @@ -380,11 +400,18 @@ class HttpNode(Node): :returns: JSON response with trigger result. :rtype: JSONResponse """ - if self.secret and not hmac.compare_digest( - str(request.path_params.get("secret", "")).encode(), - self.secret.encode(), - ): - # A wrong secret must look like no such hook at all. + from fluksio.cloud.config import via_portal + + if ( + self.secret + and not hmac.compare_digest( + str(request.path_params.get("secret", "")).encode(), + self.secret.encode(), + ) + ) or (not self.secret and via_portal(request.headers)): + # A wrong secret must look like no such hook at all, and so must + # a hook without one reached through the portal, which would + # otherwise be open to the whole internet. return JSONResponse(content={"detail": "Not Found"}, status_code=404) try: @@ -408,7 +435,7 @@ class HttpNode(Node): if spec.port in data: typed_data[spec.port] = spec.coerce(data[spec.port]) - # Inject data into the pipeline (trigger mode nodes inject via + # Inject data into the pipeline (listener nodes inject via # provides). Off the loop: journalling is a blocking Redis # round trip, and every webhook was making it on the thread # the whole API answers from. diff --git a/backend/fluksio/flow/nodes/mqtt.py b/backend/fluksio/flow/nodes/mqtt.py index 7a3303f..a6dbc6b 100644 --- a/backend/fluksio/flow/nodes/mqtt.py +++ b/backend/fluksio/flow/nodes/mqtt.py @@ -1,4 +1,4 @@ -"""MQTT nodes: a subscriber that wakes the graph, a publisher that speaks for it.""" +"""MQTT nodes: a listener that wakes the graph, a sender that speaks for it.""" from __future__ import annotations @@ -62,15 +62,14 @@ def topic_matches(filter_: str, topic: str) -> bool: class MqttNode(Node): """ - MQTT node that can act as a subscriber (trigger) or publisher (sender). + MQTT node that acts as a listener or a sender. This node integrates with an MQTT broker to either: - - **Trigger mode (Subscriber)**: Subscribe to MQTT topics and inject received - messages into the pipeline. Used when ``provides`` is specified but - ``requires`` is empty. - - **Sender mode (Publisher)**: Publish pipeline data to MQTT topics. Used when - ``requires`` is specified. + - **Listener**: Subscribe to MQTT topics and inject received messages into + the pipeline. Used when ``provides`` is specified but ``requires`` is empty. + - **Sender**: Publish pipeline data to MQTT topics. Used when ``requires`` + is specified. The ``topic`` parameter in ``params`` controls the mapping between pipeline message names and MQTT topics: @@ -78,11 +77,11 @@ class MqttNode(Node): - **dict**: Explicit mapping from message name to MQTT topic, e.g. ``{"temperature": "sensors/room1/temp", "humidity": "sensors/room1/hum"}``. - **str** (legacy): A single topic string. All messages are mapped to this - one topic (subscriber receives from it, publisher sends to it). + one topic (a listener subscribes to it, a sender publishes to it). - :param requires: Messages required by this node (makes it a publisher node). + :param requires: Messages required by this node (makes it a sender). :type requires: MessageSpec | list[MessageSpec] | None - :param provides: Messages provided by this node (makes it a subscriber node). + :param provides: Messages provided by this node (makes it a listener). :type provides: MessageSpec | list[MessageSpec] | None :param params: Parameters dict containing: - ``topic`` (str | dict): MQTT topic(s). A dict maps message names to @@ -97,7 +96,7 @@ class MqttNode(Node): - ``keepalive`` (int): Keepalive interval in seconds (default: 60) - ``timeout`` (float): Deadline for a broker operation in seconds (default: 10.0) - - ``publish_queue_size`` (int): Publisher backlog depth (default: 256) + - ``send_queue_size`` (int): Sender backlog depth (default: 256) :type params: dict :param name: Optional name for the node. :type name: str | None @@ -105,9 +104,9 @@ class MqttNode(Node): :raises ValueError: If both ``requires`` and ``provides`` are empty. :example: - Subscriber with per-message topics: + Listener with per-message topics: - >>> subscriber = MqttNode( + >>> listener = MqttNode( ... provides=[ ... MessageSpec(name="inverter_input", dtype=DType.FLOAT), ... MessageSpec(name="inverter_output", dtype=DType.FLOAT), @@ -121,9 +120,9 @@ class MqttNode(Node): ... }, ... ) - Publisher with per-message topics: + Sender with per-message topics: - >>> publisher = MqttNode( + >>> sender = MqttNode( ... requires=[ ... MessageSpec(name="target_temp", dtype=DType.FLOAT), ... MessageSpec(name="fan_speed", dtype=DType.INT), @@ -138,9 +137,9 @@ class MqttNode(Node): ... }, ... ) - Legacy single-topic subscriber: + Legacy single-topic listener: - >>> subscriber = MqttNode( + >>> listener = MqttNode( ... provides=[MessageSpec(name="temperature", dtype=DType.FLOAT)], ... params={"topic": "sensors/temperature", "broker_host": "localhost"}, ... ) @@ -149,8 +148,8 @@ class MqttNode(Node): class Mode(Enum): """Operating mode of the MQTT node.""" - SUBSCRIBER = "subscriber" # Receives MQTT messages (trigger) - PUBLISHER = "publisher" # Sends MQTT messages (sender) + LISTENER = "listener" # Subscribes and injects what arrives + SENDER = "sender" # Publishes its inputs # Publishing again is a second command to whatever is listening. idempotent = False @@ -167,14 +166,14 @@ class MqttNode(Node): "retain", "keepalive", "timeout", - "publish_queue_size", + "send_queue_size", "json_keys", "_topic_to_ports", "_wildcards", "_subscription_task", "_mqtt_client", "_stop_event", - "_publish_queue", + "_send_queue", "_publisher_task", "_loop", ) @@ -183,15 +182,41 @@ class MqttNode(Node): model_config = ConfigDict(extra="allow") # One topic for every port, or a per-port mapping. - topic: str | dict[str, str] = "*" - broker_host: str = "localhost" - broker_port: int = 1883 - username: str | None = None - password: str | None = Field(default=None, json_schema_extra={"x-secret": True}) - client_id: str | None = None - qos: int = 0 - retain: bool = False - keepalive: int = 60 + topic: str | dict[str, str] = Field( + default="*", + description='One topic for every port, or {"port": "some/topic"} per port.', + ) + broker_host: str = Field( + default="localhost", + description="Hostname or IP of the broker, as the engine reaches it.", + ) + broker_port: int = Field( + default=1883, description="The port the broker listens on." + ) + username: str | None = Field( + default=None, description="Username for the broker, if it asks for one." + ) + password: str | None = Field( + default=None, + description="Password for the broker, kept in the secrets store.", + json_schema_extra={"x-secret": True}, + ) + client_id: str | None = Field( + default=None, + description="Client identifier the broker sees. Empty picks a random one.", + ) + qos: int = Field( + default=0, + description="Quality of service for subscribing and publishing, 0, 1 or 2.", + ) + retain: bool = Field( + default=False, + description="Have the broker keep the last value sent for later subscribers.", + ) + keepalive: int = Field( + default=60, + description="Seconds between pings that keep an idle connection open.", + ) # Bounds every broker operation: subscribe, publish, and the # disconnect acknowledgement on the way out. Without one a client # whose socket died waits for that ack forever, and the task never @@ -202,9 +227,9 @@ class MqttNode(Node): description="Give up on a broker operation after this many seconds.", ) # Deep enough to ride out a broker hiccup, shallow enough that a - # publisher which cannot keep up drops old values instead of growing + # sender which cannot keep up drops old values instead of growing # without bound. A node that bursts wants more than one that trickles. - publish_queue_size: int = Field( + send_queue_size: int = Field( default=256, gt=0, description=( @@ -215,17 +240,23 @@ class MqttNode(Node): # Which key to lift out of a JSON object payload. A device that wraps # its reading — Victron's ``{"value": 5}`` — is otherwise a Python node # per port. One key for every port, or a per-port mapping. - json_key: str | dict[str, str] = "" + json_key: str | dict[str, str] = Field( + default="", + description=( + "Key to lift out of a JSON object payload, for every port or as " + '{"port": "key"}.' + ), + ) @classmethod def target_key(cls, params: dict[str, Any]) -> str | None: """The broker and topic, which is one physical thing. - A publisher and a subscriber on the same topic get the same key on + A sender and a listener on the same topic get the same key on purpose: they are two ends of one wire, and drawing them as one neuron is the only way the path through the broker shows up at all. """ - # ponytail: publisher and subscriber merge into one neuron; key on mode + # ponytail: sender and listener merge into one neuron; key on mode # as well if the two directions ever need telling apart. fields = cls.Params.model_fields topic = params.get("topic", fields["topic"].default) @@ -253,9 +284,9 @@ class MqttNode(Node): ) # Inputs mean this node publishes; outputs mean it subscribes. - self.mode = MqttNode.Mode.PUBLISHER if requires else MqttNode.Mode.SUBSCRIBER + self.mode = MqttNode.Mode.SENDER if requires else MqttNode.Mode.LISTENER - ports = provides if self.mode == MqttNode.Mode.SUBSCRIBER else requires + ports = provides if self.mode == MqttNode.Mode.LISTENER else requires if isinstance(cfg.topic, dict): self.topics: dict[str, str] = dict(cfg.topic) else: @@ -285,13 +316,13 @@ class MqttNode(Node): self.retain = cfg.retain self.keepalive = cfg.keepalive self.timeout = cfg.timeout - self.publish_queue_size = cfg.publish_queue_size + self.send_queue_size = cfg.send_queue_size # Runtime state self._subscription_task: asyncio.Task[None] | None = None self._mqtt_client = None self._stop_event: asyncio.Event | None = None - self._publish_queue: asyncio.Queue[dict[str, Any]] | None = None + self._send_queue: asyncio.Queue[dict[str, Any]] | None = None self._publisher_task: asyncio.Task[None] | None = None self._loop: asyncio.AbstractEventLoop | None = None @@ -311,13 +342,13 @@ class MqttNode(Node): name = f"mqtt_{self.mode.value}_{safe_topic}" # Initialize parent with appropriate function - # For subscriber mode, f is a no-op since data is injected via inject() - # For publisher mode, f handles the outgoing MQTT publish + # For a listener, f is a no-op since data is injected via inject() + # For a sender, f handles the outgoing MQTT publish super().__init__( f=( - self._noop_subscriber - if self.mode == MqttNode.Mode.SUBSCRIBER - else self._publisher_handler + self._noop_listener + if self.mode == MqttNode.Mode.LISTENER + else self._sender_handler ), requires=requires, provides=provides, @@ -326,13 +357,11 @@ class MqttNode(Node): ) @staticmethod - def _noop_subscriber( - params: dict[str, Any], **kwargs: Any - ) -> dict[str, Any] | None: + def _noop_listener(params: dict[str, Any], **kwargs: Any) -> dict[str, Any] | None: """ - No-op function for subscriber mode nodes. + No-op function for listener nodes. - Subscriber mode nodes inject data via :meth:`inject`, not :meth:`__call__`. + Listener nodes inject data via :meth:`inject`, not :meth:`__call__`. This function exists only to satisfy the Node interface and should not be called directly. @@ -345,11 +374,11 @@ class MqttNode(Node): """ return None - def _publisher_handler( + def _sender_handler( self, params: dict[str, Any], **kwargs: Any ) -> dict[str, Any] | None: """ - Publish pipeline data to MQTT topic (publisher mode). + Publish pipeline data to MQTT topic (sender). This method is called when upstream dependencies are satisfied. Handing the payload to the node's publisher task is all that happens @@ -363,7 +392,7 @@ class MqttNode(Node): :returns: None (publishing is fire-and-forget). :rtype: dict | None """ - loop, queue = self._loop, self._publish_queue + loop, queue = self._loop, self._send_queue if loop is not None and queue is not None: loop.call_soon_threadsafe(self._enqueue, queue, dict(kwargs)) return None @@ -386,21 +415,21 @@ class MqttNode(Node): if queue.full(): try: queue.get_nowait() - logger.warning("Publish queue full for node '%s', dropped", self.name) - self.report_health("degraded", "publish queue full") + logger.warning("Send queue full for node '%s', dropped", self.name) + self.report_health("degraded", "send queue full") except asyncio.QueueEmpty: pass queue.put_nowait(data) async def _publisher_loop(self) -> None: - """Hold one connection and drain the publish queue over it. + """Hold one connection and drain the send queue over it. A dropped connection raises, and the supervisor decides when to - reconnect — the same arrangement the subscriber uses. + reconnect — the same arrangement the listener uses. """ aiomqtt = _aiomqtt() - queue = self._publish_queue + queue = self._send_queue if queue is None: return @@ -461,7 +490,7 @@ class MqttNode(Node): continue # A string goes on the wire as it stands. Devices on a shared - # broker expect bare values, and the subscriber below already + # broker expect bare values, and the listener below already # falls back to the raw text when it is not JSON, so a # fluksio-to-fluksio round trip is unaffected. payload = value if isinstance(value, str) else json.dumps(value) @@ -479,8 +508,8 @@ class MqttNode(Node): ) async def start(self, app: FastAPI | None = None) -> None: - """A subscriber listens; a publisher opens the connection it will reuse.""" - if self.mode is MqttNode.Mode.SUBSCRIBER: + """A listener subscribes; a sender opens the connection it will reuse.""" + if self.mode is MqttNode.Mode.LISTENER: await self.start_subscription() else: await self.start_publisher() @@ -491,41 +520,41 @@ class MqttNode(Node): async def start_publisher(self) -> None: """Run the task that owns this node's connection to the broker.""" - if self._publish_queue is not None: + if self._send_queue is not None: return - self._publish_queue = asyncio.Queue(maxsize=self.publish_queue_size) + self._send_queue = asyncio.Queue(maxsize=self.send_queue_size) self._loop = asyncio.get_running_loop() self._publisher_task = self._run_supervised("mqtt-out", self._publisher_loop) async def stop_publisher(self) -> None: """Drop the queue and let the connection go.""" - if self._publish_queue is None: + if self._send_queue is None: return if self._publisher_task is not None: await self._cancel_task(self._publisher_task) self._publisher_task = None - self._publish_queue = None + self._send_queue = None self._loop = None async def start_subscription(self) -> None: """ - Start the MQTT subscription for trigger mode nodes. + Start the MQTT subscription for listener nodes. This method starts a background task that listens for messages on the subscribed topic and triggers the pipeline when messages arrive. - :raises RuntimeError: If called on a publisher mode node. + :raises RuntimeError: If called on a sender. :example: - >>> subscriber = MqttNode( + >>> listener = MqttNode( ... topic="sensors/#", ... provides=[MessageSpec(name="value", dtype=DType.FLOAT)], ... params={"broker_host": "localhost"}, ... ) - >>> await subscriber.start_subscription() + >>> await listener.start_subscription() """ - if self.mode != MqttNode.Mode.SUBSCRIBER: - raise RuntimeError("Can only start subscription for subscriber mode nodes") + if self.mode != MqttNode.Mode.LISTENER: + raise RuntimeError("Can only start subscription for listener nodes") if self._subscription_task is not None: return # Already running diff --git a/backend/fluksio/flow/nodes/websocket.py b/backend/fluksio/flow/nodes/websocket.py index 15732f6..3d3b796 100644 --- a/backend/fluksio/flow/nodes/websocket.py +++ b/backend/fluksio/flow/nodes/websocket.py @@ -121,7 +121,13 @@ class WebSocketNode(Node): ), ) # Same as the mqtt node: a device that wraps its reading in an object. - json_key: str | dict[str, str] = "" + json_key: str | dict[str, str] = Field( + default="", + description=( + "Key to lift out of a JSON object frame, for every port or as " + '{"port": "key"}.' + ), + ) @classmethod def target_key(cls, params: dict[str, Any]) -> str | None: @@ -336,11 +342,17 @@ class WebSocketNode(Node): async def _serve(self, websocket: WebSocket) -> None: """One client of a served socket, for as long as it stays.""" - if self.secret and not hmac.compare_digest( - str(websocket.path_params.get("secret", "")).encode(), - self.secret.encode(), - ): + from fluksio.cloud.config import via_portal + + if ( + self.secret + and not hmac.compare_digest( + str(websocket.path_params.get("secret", "")).encode(), + self.secret.encode(), + ) + ) or (not self.secret and via_portal(websocket.headers)): # Closed before accepting: the same 403 as a path with no route. + # Through the portal a socket needs a secret, as a webhook does. await websocket.close(code=1008) return await websocket.accept() diff --git a/backend/fluksio/flow/nodes/wren.py b/backend/fluksio/flow/nodes/wren.py index 1f9e2c7..2048508 100644 --- a/backend/fluksio/flow/nodes/wren.py +++ b/backend/fluksio/flow/nodes/wren.py @@ -14,6 +14,7 @@ from collections.abc import Iterable from typing import Any from pydantic import BaseModel, ConfigDict, Field +from threadpoolctl import threadpool_limits from fluksio.flow.messages import DType, MessageSpec from fluksio.flow.nodes.base import Node @@ -306,7 +307,11 @@ class WrenNode(Node): wren = _wren() specs = {spec.port: spec for spec in self.output_ports} house = self._house(wren, specs, presence, outdoor, dark) - calls, confidence = wren.act(prompt, house) + # One BLAS thread answers in about half the time for a model this + # small. Scoped to the call, so numpy elsewhere in the process keeps + # its threads. + with threadpool_limits(1): + calls, confidence = wren.act(prompt, house) confidence = round(float(confidence), 3) write = self.cfg.apply and confidence >= self.cfg.min_confidence writes: dict[str, Any] = {} diff --git a/backend/fluksio/flow/panels.py b/backend/fluksio/flow/panels.py index f8f62a9..26d4703 100644 --- a/backend/fluksio/flow/panels.py +++ b/backend/fluksio/flow/panels.py @@ -21,6 +21,7 @@ from pydantic import BaseModel, Field, field_validator from fluksio.core.config import settings from fluksio.flow.dashboards import DashboardDef, DashboardNotFound, DashboardStore +from fluksio.flow.messages import validate_qualified from fluksio.flow.schemas import _validate_name #: Free text the device interprets: ``gpio.4``, ``backlight``, ``mic``. @@ -51,14 +52,7 @@ class PanelPort(BaseModel): raise ValueError("Use lowercase letters, digits, underscores and dots") return value - @field_validator("message") - @classmethod - def _check_message(cls, value: str) -> str: - flow, _, name = value.partition(".") - if not name: - raise ValueError("Name the message with its flow, as flow.name") - _validate_name(flow) - return value + _check_message = field_validator("message")(validate_qualified) class PanelDef(BaseModel): @@ -96,6 +90,9 @@ class PanelsConfig(BaseModel): """Every panel this instance knows about.""" panels: list[PanelDef] = Field(default_factory=list) + #: Bumped on every save; a save based on an older one is refused, as a + #: dashboard's is. A file written before there was one reads as 0. + version: int = 0 #: Held across a read-modify-write of the panels file. @@ -194,6 +191,16 @@ def requests_of(defn: DashboardDef) -> set[str]: return {w.target for w in defn.widgets if w.type == "chart" and w.target} +def targets_of(defn: DashboardDef) -> set[str]: + """The messages this dashboard's inputs publish to. + + What a screen showing it may write, a narrower set than what it may read. + A tile that only draws a message, and a setting bound to one, are there to + be looked at, so a panel showing them is not entitled to set them. + """ + return {w.target for w in defn.widgets if w.target} + + def io_in(panel_id: str) -> set[str]: """The messages this panel's own hardware publishes to.""" panel = find(panel_id) diff --git a/backend/fluksio/flow/pipeline.py b/backend/fluksio/flow/pipeline.py index 9faf9cc..0060d3c 100644 --- a/backend/fluksio/flow/pipeline.py +++ b/backend/fluksio/flow/pipeline.py @@ -23,6 +23,7 @@ from collections import deque from collections.abc import Callable, Iterator from concurrent.futures import Future, ThreadPoolExecutor, wait from contextlib import contextmanager +from datetime import UTC, datetime from functools import lru_cache from typing import Any, Literal, Protocol @@ -46,7 +47,7 @@ MANUAL_RUN_PREFIX = "manual-" # neither the brain graph nor the health summary treats them as a fault. # Lives here rather than beside the controller so `ValidationIssue` can carry # the distinction itself, and every reader gets it for free. -ADVISORY_ISSUES = frozenset({"unauthenticated_hook"}) +ADVISORY_ISSUES = frozenset({"unauthenticated_hook", "ignored_device"}) class ValidationIssue(BaseModel): @@ -61,6 +62,7 @@ class ValidationIssue(BaseModel): "self_loop_needs_initial", "missing_source", "node_unhealthy", + "ignored_device", ] message: str flow: str = "" @@ -85,7 +87,7 @@ class ValueSource(BaseModel): flow, an agent — be shown at all, since none of those is a node here. """ - #: node, dashboard, flow, agent, chat or api. + #: node, dashboard, flow, agent, chat, api or panel. kind: str = "node" #: Node id, dashboard name, or whatever identifies the caller. id: str = "" @@ -165,6 +167,13 @@ class RunCacheLookup(Protocol): """What an equal execution produced, or None when there is no entry.""" +#: When the current shape of a cache key landed: the last change to what a +#: fingerprint hashes (`FlowController`) or to how the key below reduces its +#: inputs that moved every key. A row written before it matches nothing now, +#: so the run service's sweep un-keys it. Move it forward with such a change. +CACHE_SHAPE_SINCE = datetime(2026, 8, 26, 19, 27, 10, tzinfo=UTC) + + def run_cache_key(fingerprint: str, inputs: dict[str, Any], flow: str = "") -> str: """What this node, with these inputs, is known by. @@ -670,7 +679,7 @@ class Pipeline: return passed, held, due_at, stamps.get(flush_key) def _throttled( - self, node: Node, result: dict[str, Any] + self, node: Node, result: dict[str, Any], entry_id: str = "" ) -> tuple[dict[str, Any], list[str]]: """Hold back the outputs whose port is not due to publish yet. @@ -679,6 +688,9 @@ class Pipeline: than leaving the consumer on the one before it. Nothing declaring an interval means nothing to look up. + ``entry_id`` is the run doing the holding, which the flush names as + its parent. + Returns what passed and the held keys the publish makes stale — the caller drops them in the same round trip it writes the values with, rather than one DEL per port here. @@ -699,28 +711,35 @@ class Pipeline: self._state.update( {self._held_key(name): value for name, value in held.items()} ) - self._schedule_flush(node, due_at, now, pending) + self._schedule_flush(node, due_at, now, pending, entry_id) return passed, stale def _schedule_flush( - self, node: Node, at: float, now: float, pending: float | None + self, + node: Node, + at: float, + now: float, + pending: float | None, + entry_id: str = "", ) -> None: """Come back to this node when its rate-limit window ends. One timer in flight per node: a pending one that is early enough is left alone, and one that turns out to be too late simply finds nothing - to do when it fires. + to do when it fires. So the parent a flush names is the run that booked + it, which held the first value of the window, not necessarily the last. """ if self._queue is None or (pending and now < pending <= at): return self._state[self._flush_key(node.id)] = at - self.defer(node, {}, at - now, kind="flush") + self.defer(node, {}, at - now, kind="flush", parent=entry_id) - def flush(self, node: Node) -> None: + def flush(self, node: Node, entry_id: str = "") -> None: """Let through what this node's rate limits held back. Runs when a window ends: the last value a limited output kept back is published, and a node whose inputs were all held back gets its run. + ``entry_id`` is the flush's own run. """ limited_out = [name for name, s in node.provides.items() if s.interval > 0] stored = ( @@ -737,14 +756,17 @@ class Pipeline: # Publishing runs the limit again, which is what clears the hold — # or puts the timer back, if this fired a hair early. In that case # nothing came out and there is nothing downstream to wake. - published = self.apply_outputs(node, held) + published = self.apply_outputs(node, held, entry_id) if published: - self.run_downstream(node, changed=published) + self.run_downstream(node, entry_id=entry_id, changed=published) if any(spec.interval > 0 for spec in node.requires.values()): # A wake-up that was held back, not a value that just changed — # so this one deliberately walks everything reachable. self._execute_parallel( - {node, *self._get_downstream(node)}, self._state, check_ready=True + {node, *self._get_downstream(node)}, + self._state, + check_ready=True, + entry_id=entry_id, ) def _increment_message_versions(self, outputs: dict[str, Any]) -> None: @@ -791,7 +813,7 @@ class Pipeline: return self._state.compare_and_swap_multi(expected, updates) - def _input_is_due(self, node: Node) -> bool: + def _input_is_due(self, node: Node, entry_id: str = "") -> bool: """Has any rate-limited input waited out its interval? A node runs when *any* of its inputs is due, so a slow port next to a @@ -827,6 +849,7 @@ class Pipeline: ), now, stamps.get(flush_key), + entry_id, ) return False @@ -834,7 +857,7 @@ class Pipeline: return True def _is_node_ready( - self, node: Node, state: StateBackend + self, node: Node, state: StateBackend, entry_id: str = "" ) -> tuple[bool, dict[str, Any]]: """Whether the node may run, and the inputs the answer was read from. @@ -851,7 +874,7 @@ class Pipeline: if any(msg not in values for msg in waited_on): return False, values - if not self._input_is_due(node): + if not self._input_is_due(node, entry_id): return False, values if not node.synchronous: @@ -1062,7 +1085,7 @@ class Pipeline: stale: list[str] = [] if result: - result, stale = self._throttled(node, result) + result, stale = self._throttled(node, result, entry_id) if result: self._record_outputs(node, result, state, stale) @@ -1358,7 +1381,7 @@ class Pipeline: # The values are kept whether or not the answer is yes: # a node that may run reads them below instead of # asking state for the same keys again. - ready, ready_inputs = self._is_node_ready(n, state) + ready, ready_inputs = self._is_node_ready(n, state, entry_id) if not ready: # Completed, not merely passed over: a later trigger # may still make it ready, but not running is not @@ -1440,11 +1463,14 @@ class Pipeline: return self._execute_parallel(nodes, self._state, check_ready=False) - def apply_outputs(self, node: Node, outputs: dict[str, Any] | None) -> set[str]: + def apply_outputs( + self, node: Node, outputs: dict[str, Any] | None, entry_id: str = "" + ) -> set[str]: """Record what a node emitted: state, history, versions and events. Shared by the direct path and by the execution service replaying a journaled item, so a value looks the same on the canvas either way. + ``entry_id`` is the run this happens in. Returns the message names that actually reached state — post rate limiting — which is what the cascade behind it has to walk from. @@ -1453,7 +1479,7 @@ class Pipeline: if outputs: # This is where a chatty subscriber gets thinned out, so a port set # to publish every 60s does so whatever the broker sends. - outputs, stale = self._throttled(node, outputs) + outputs, stale = self._throttled(node, outputs, entry_id) if not outputs: return set() @@ -1569,6 +1595,7 @@ class Pipeline: self._state.update( {self._held_key(name): value for name, value in held.items()} ) + # Held before any run began, so its flush names no parent. self._schedule_flush(node, due_at, now, pending) return True except Exception: @@ -1607,7 +1634,9 @@ class Pipeline: ) try: published = ( - set(outputs or {}) if delivered else self.apply_outputs(node, outputs) + set(outputs or {}) + if delivered + else self.apply_outputs(node, outputs, run_id) ) state = self.run_downstream( node, @@ -1684,6 +1713,7 @@ class Pipeline: seconds: float, guard: tuple[str, Any] | None = None, kind: str = "cascade", + parent: str = "", ) -> bool: """Publish a node's outputs later, without holding a worker thread. @@ -1691,6 +1721,8 @@ class Pipeline: over; if it has moved on, the item is dropped. That is how a wait which gets restarted cancels the one it replaced. + ``parent`` is the run the item continues, recorded beside its own. + Returns False when there is no queue to hold the item, in which case the caller has to wait however it waited before. """ @@ -1703,9 +1735,10 @@ class Pipeline: node=node.id, flow=node.flow, outputs=outputs, - cause="delay", + cause="flush" if kind == "flush" else "delay", guard_key=guard[0] if guard else "", guard_value=str(guard[1]) if guard else "", + parent=parent, ) try: self._queue.add_delayed(item, time.time() + seconds) diff --git a/backend/fluksio/flow/queue.py b/backend/fluksio/flow/queue.py index 7c0ef72..3d46bad 100644 --- a/backend/fluksio/flow/queue.py +++ b/backend/fluksio/flow/queue.py @@ -18,6 +18,7 @@ from __future__ import annotations import heapq import logging +import secrets import threading import time from abc import ABC, abstractmethod @@ -93,6 +94,9 @@ class WorkItem: #: The run this item is, on a ``run`` item. Everything else about a run is #: in its database row, so the journal only has to name it. run_id: str = "" + #: The run that scheduled this item, on a ``flush``: the one whose value + #: the rate limit held back. + parent: str = "" def to_fields(self) -> dict[str, str]: return { @@ -106,6 +110,7 @@ class WorkItem: "guard_value": self.guard_value, "enqueued_at": str(self.enqueued_at), "run_id": self.run_id, + "parent": self.parent, } @classmethod @@ -125,6 +130,7 @@ class WorkItem: deliveries=deliveries, enqueued_at=float(fields.get("enqueued_at") or time.time()), run_id=fields.get("run_id", ""), + parent=fields.get("parent", ""), ) @@ -272,12 +278,15 @@ class MemoryWorkQueue(WorkQueue): self._in_flight = 0 self._counter = 0 self._seq = 0 + # The ids become `FlowRun.id`, a primary key. Counting from zero alone, + # a restart would overwrite the previous boot's rows. + self._boot = secrets.token_hex(4) self._lock = threading.Lock() self._wake = threading.Condition(self._lock) def _next_id(self) -> str: self._seq += 1 - return f"mem-{self._seq}" + return f"mem-{self._boot}-{self._seq}" def add(self, item: WorkItem) -> None: with self._wake: diff --git a/backend/fluksio/flow/remote.py b/backend/fluksio/flow/remote.py index 30678ca..fcf638d 100644 --- a/backend/fluksio/flow/remote.py +++ b/backend/fluksio/flow/remote.py @@ -249,6 +249,11 @@ class RemoteWorker: def cancel(self, call_id: str) -> None: """Ask the worker to kill what it is running for this call.""" + # A closed loop refuses the coroutine and leaves it never awaited, so it + # is not made at all. + if self._loop.is_closed(): + logger.warning("Could not cancel '%s' on '%s'", call_id, self.name) + return try: asyncio.run_coroutine_threadsafe( self._send({"op": "cancel", "call_id": call_id}), self._loop diff --git a/backend/fluksio/flow/resources.py b/backend/fluksio/flow/resources.py index be51011..0e05c22 100644 --- a/backend/fluksio/flow/resources.py +++ b/backend/fluksio/flow/resources.py @@ -65,21 +65,63 @@ ENGINE_RAM_RESERVE_MB = 1024 #: Where a container finds its own memory limit (cgroup v2). CGROUP_MEMORY_MAX = Path("/sys/fs/cgroup/memory.max") +#: How many times the core count a concurrency limit may reach before startup +#: warns. Above one on purpose: a node waiting on the network holds no core. +OVERSUBSCRIBE_FACTOR = 4 + def machine_cpus() -> int: """Cores a node may be given here, when nobody said.""" return max(1, (os.cpu_count() or 1) - ENGINE_RESERVE) +def warn_oversubscribed(limits: dict[str, int]) -> None: + """Warn, never clamp, when a concurrency limit is far past the cores. + + A high number can be right for nodes that wait on devices, but one far + past the machine is more often a zero too many. + """ + cores = os.cpu_count() or 1 + high = [ + f"{name}={value}" + for name, value in limits.items() + if value > OVERSUBSCRIBE_FACTOR * cores + ] + if high: + logger.warning( + "Concurrency above %dx this machine's %d cores: %s", + OVERSUBSCRIBE_FACTOR, + cores, + ", ".join(high), + ) + + +def visible_gpus() -> list[str] | None: + """The cards the engine's own ``CUDA_VISIBLE_DEVICES`` lists, if it is set. + + Indices, UUIDs or MIG instances, kept as written: a child is handed the + same names, so whatever the driver took here it takes there. + """ + mask = os.environ.get("CUDA_VISIBLE_DEVICES") + if mask is None: + return None + return [part.strip() for part in mask.split(",") if part.strip()] + + def machine_gpus() -> int: """Cards a node may be given here, when nobody said. - The device nodes the NVIDIA driver creates, counted — no vendor tool, so + The engine's own ``CUDA_VISIBLE_DEVICES`` where it has one, since a card + the process can see but was not given is not its to hand out. Otherwise + the device nodes the NVIDIA driver creates, counted — no vendor tool, so the one dependency does not become two. It answers for the case that actually bit people (a box with cards, an engine told nothing, every GPU node clamped to zero and running at once) and for nothing else: another vendor, or a card this process cannot see, is still `--gpus N`. """ + visible = visible_gpus() + if visible is not None: + return len(visible) return len(glob.glob("/dev/nvidia[0-9]*")) @@ -204,7 +246,9 @@ class ResourceAccountant: # ----------------------------------------------------------------------------- -def derive_env(wanted: Resources, allocation: Allocation) -> dict[str, str]: +def derive_env( + wanted: Resources, allocation: Allocation, local: bool = True +) -> dict[str, str]: """The environment a worker running this node is started with. Precedence is engine environment, then what the allocation implies, then @@ -215,10 +259,19 @@ def derive_env(wanted: Resources, allocation: Allocation) -> dict[str, str]: contents depend on the version installed, so writing it here would silently replace whatever the author had put there. It travels in ``resources.env``, where it is visible. + + ``local`` is whether the allocation is this machine's. A child's + ``CUDA_VISIBLE_DEVICES`` replaces the engine's rather than narrowing it, so + here a card is named the way the engine's own list names it: under ``=2,3`` + card 0 is device 2. A remote worker does the same against its own list. """ env = {var: str(allocation.cpus) for var in THREAD_VARS} if wanted.gpus: - env["CUDA_VISIBLE_DEVICES"] = ",".join(str(index) for index in allocation.gpus) + names = (visible_gpus() if local else None) or [] + env["CUDA_VISIBLE_DEVICES"] = ",".join( + names[index] if index < len(names) else str(index) + for index in allocation.gpus + ) env.update(wanted.env) return env diff --git a/backend/fluksio/flow/runs.py b/backend/fluksio/flow/runs.py index 1082af5..4f5b3de 100644 --- a/backend/fluksio/flow/runs.py +++ b/backend/fluksio/flow/runs.py @@ -52,11 +52,11 @@ from fluksio.core.db import engine as db_engine from fluksio.flow.artifacts import ArtifactStore, is_reference, valid_digest from fluksio.flow.controller import NODE_TYPES, FlowController, RunContext from fluksio.flow.messages import qualify -from fluksio.flow.pipeline import CacheHit, NodeOutcome, Pipeline +from fluksio.flow.pipeline import CACHE_SHAPE_SINCE, CacheHit, NodeOutcome, Pipeline from fluksio.flow.queue import WorkItem, WorkQueue from fluksio.flow.schemas import FlowDef, NodeDef from fluksio.flow.state import MemoryState, StateBackend -from fluksio.flow.store import FlowStore +from fluksio.flow.store import FlowNotFound, FlowStore from fluksio.models import Run, RunArtifact, RunMetric, RunNode from fluksio.sdk.imports import digest_of as files_digest @@ -375,16 +375,19 @@ def _references_in(value: Any, found: set[str], depth: int = 0) -> None: def sweep_artifacts( - store: ArtifactStore, state: StateBackend, grace_s: float = 0.0 + store: ArtifactStore, + state: StateBackend, + grace_s: float = 0.0, + flows: FlowStore | None = None, ) -> int: """Remove artifact bytes nothing refers to any more. Returns how many went. - Two things refer to an artifact: a run that recorded it, and a message - currently holding it. The second is what makes a stream of media - affordable — a camera publishing a frame a second replaces the reference - each time, so yesterday's frames are unreferenced by definition and the - store does not grow without bound. What a node *returns* is recorded - against its run and kept; what it emits along the way is not. + What still refers to one is `referenced_artifacts`. A message holding one is + what makes a stream of media affordable — a camera publishing a frame a + second replaces the reference each time, so yesterday's frames are + unreferenced by definition and the store does not grow without bound. What + a node *returns* is recorded against its run and kept, what it emits along + the way is not. Skipped entirely while anything is running: a node that stores a checkpoint an hour before it returns has neither a row nor a message naming it yet, @@ -397,17 +400,40 @@ def sweep_artifacts( if active is not None: logger.debug("Artifact sweep skipped: run %s is in flight", active) return 0 + + removed = store.collect(referenced_artifacts(state, flows), grace_s) + if removed: + logger.info("Artifact sweep removed %d unreferenced artifacts", removed) + return removed + + +def referenced_artifacts( + state: StateBackend, flows: FlowStore | None = None +) -> set[str]: + """Every artifact digest something still refers to. + + Three things do: a run that recorded it, a message currently holding it, + and a flow input's ``initial``, published or draft, which a flow that has + never started holds nowhere else. + """ + with Session(db_engine) as session: keep = set(session.exec(select(col(RunArtifact.digest)).distinct()).all()) + if flows is not None: + for name in flows.list_flows(): + for draft in (False, True): + try: + flow = flows.read_flow(name, draft=draft) + except FlowNotFound: + continue + for declared in flow.inputs: + _references_in(declared.initial, keep) + keys = state.keys() for start in range(0, len(keys), _SWEEP_BATCH): for value in state.get_multi(keys[start : start + _SWEEP_BATCH]).values(): _references_in(value, keep) - - removed = store.collect(keep, grace_s) - if removed: - logger.info("Artifact sweep removed %d unreferenced artifacts", removed) - return removed + return keep def seed_values( @@ -440,6 +466,14 @@ def seed_values( return values +def declared_inputs(flow: FlowDef) -> dict[str, Any]: + """What a flow declares it can be given, by name, with the declared values. + + Kept on each run, since the flow can change after it. + """ + return {one.spec.name: one.initial for one in flow.inputs} + + def collect_result(flow: FlowDef, state: StateBackend) -> dict[str, Any]: """What the run produced, keyed by message name without the flow prefix.""" prefix = f"{flow.name}." @@ -563,6 +597,9 @@ class MetricSink: ) ) session.commit() + except IntegrityError: + # Deleted while it was still reporting, as `_record_node` explains. + logger.info("Run %s is gone; not writing its metrics", self.run_id) except Exception: logger.exception( "Could not write %d metric(s) of %s", len(rows), self.run_id @@ -722,6 +759,26 @@ def _cacheable(outcome: NodeOutcome) -> str | None: return text if len(text) <= OUTPUT_CAP else None +def prune_stale_cache() -> int: + """Un-key the stage-cache rows written before the current key shape. + + Such a row can match no key a node computes any more, so it is a cache + entry in name only. The row and its outputs stay: they are the record of + what that run's node returned. Returns how many rows were un-keyed. + """ + with Session(db_engine) as session: + older = select(Run.id).where(col(Run.created_at) < CACHE_SHAPE_SINCE) + result = session.exec( + update(RunNode) + .where(col(RunNode.cache_key) != "", col(RunNode.run_id).in_(older)) + .values(cache_key="") + ) + session.commit() + if result.rowcount: + logger.info("Un-keyed %d stage-cache rows of an older shape", result.rowcount) + return int(result.rowcount) + + class RunCache: """What earlier runs already worked out, looked up by cache key. @@ -911,6 +968,7 @@ class RunService: code_digest=run_digest(flow), params=params, passed=named if passed is None else passed, + declared=declared_inputs(flow), params_digest=digest_of(params, seed), seed=seed, group_id=group_id, @@ -1033,6 +1091,7 @@ class RunService: flow_version=flow.version, params=params, passed=passed, + declared=declared_inputs(flow), params_digest=digest_of(params, seed), seed=seed, group_id=entry["group_id"], @@ -1314,6 +1373,7 @@ class RunService: if time.monotonic() - last_sweep >= SWEEP_INTERVAL_S: last_sweep = time.monotonic() self._sweep_abandoned(now) + prune_stale_cache() except Exception: logger.exception("Could not refresh run leases") @@ -1544,6 +1604,10 @@ class RunService: ) ) session.commit() + except IntegrityError: + # The run was deleted while this node was still going, say once it + # had been swept as abandoned. Its rows have no run to belong to. + logger.info("Run %s is gone; not recording node '%s'", run_id, outcome.node) except Exception: logger.exception( "Could not record node '%s' of run %s", outcome.node, run_id diff --git a/backend/fluksio/flow/schemas.py b/backend/fluksio/flow/schemas.py index 73c6ab3..afe4397 100644 --- a/backend/fluksio/flow/schemas.py +++ b/backend/fluksio/flow/schemas.py @@ -267,6 +267,15 @@ class FlowInput(BaseModel): spec: MessageSpec initial: Any | None = None + origin: Literal["run", "panel"] = Field( + default="run", + description=( + "What writes it. A run input is a parameter a run passes in, a " + "panel input is written by a dashboard or panel control. A hint " + "only: run forms leave panel inputs out, the engine treats both " + "the same." + ), + ) class FlowDef(BaseModel): @@ -334,6 +343,17 @@ class NodeStatusPublic(BaseModel): last_error_ts: float | None = None +class NodeSourceSaved(NodeStatusPublic): + """Whether saved code loads, and what its header changed. + + `node` is set when the parameters of ``process`` added, dropped or renamed + an input port or setting. The draft moved on to `version` with it. + """ + + node: NodeDef | None = None + version: int | None = None + + class MessageValue(BaseModel): """The last payload seen on a message.""" diff --git a/backend/fluksio/flow/store.py b/backend/fluksio/flow/store.py index d414c63..9edef65 100644 --- a/backend/fluksio/flow/store.py +++ b/backend/fluksio/flow/store.py @@ -166,6 +166,51 @@ class FlowStore: if not (self.root / ".git").exists(): self._git("init", "-q") self._commit("Initialise flow store", allow_empty=True) + self._migrate() + + def _migrate(self) -> None: + """Rewrite node types and settings stored under a name they no longer have. + + On 2026-10-15 the node type ``trigger`` became ``debounce`` and the mqtt + setting ``publish_queue_size`` became ``send_queue_size``, with no alias + left for the old names. Each published and draft document is read as + plain JSON and only the matching node changes, so a field this version + does not model survives, and the old document stays in git. The version + goes up by one, so an editor still holding the old document gets a + conflict on its next save instead of writing the old name back. A store + with nothing to rename is left alone. + """ + changed = [] + paths = [*self.root.glob("*/flow.json"), *self.root.glob("*/flow.draft.json")] + for path in sorted(paths): + try: + data = json.loads(path.read_text()) + nodes = [n for n in data["nodes"] if n.get("type") == "trigger"] + queues = [ + n["params"] + for n in data["nodes"] + if n.get("type") == "mqtt" + and "publish_queue_size" in (n.get("params") or {}) + ] + except Exception: + # Unreadable to the engine too, which reports it when it loads. + continue + if not nodes and not queues: + continue + for node in nodes: + node["type"] = "debounce" + for params in queues: + params["send_queue_size"] = params.pop("publish_queue_size") + data["version"] = data.get("version", 1) + 1 + path.write_text(json.dumps(data, indent=2, ensure_ascii=False) + "\n") + changed.append(f"{path.parent.name}/{path.name}") + if changed: + logger.warning( + "Renamed node type 'trigger' to 'debounce' and mqtt " + "'publish_queue_size' to 'send_queue_size' in %s", + ", ".join(changed), + ) + self._commit("Rename 'trigger' nodes and mqtt 'publish_queue_size'") # ------------------------------------------------------------------------- # git @@ -355,12 +400,47 @@ class FlowStore: self._set_source_ref(flow, node_id, None) self._commit(f"Unshare node '{flow}.{node_id}'") - def usages(self, lib_name: str) -> list[str]: - """Every ``flow.node`` pointing at this shared source.""" + def detach_lib(self, name: str) -> None: + """Give every node running this shared source a copy, then delete it. + + Published flows and drafts alike, so a running flow keeps the same code + and nothing waits on a publish. Each document changed goes up a version, + so an editor holding the old one gets a conflict on its next save + instead of pointing a node back at a source that is gone. + """ + with self._write_lock: + code = self.read_lib_source(name) + for flow in self.list_flows(): + for path, node_file in ( + (self._flow_file(flow), self._node_file), + (self._draft_file(flow), self._draft_node_file), + ): + if not path.exists(): + continue + definition = FlowDef.model_validate_json(path.read_text()) + users = [n for n in definition.nodes if n.source_ref == name] + if not users: + continue + for node in users: + target = node_file(flow, node.id) + target.parent.mkdir(parents=True, exist_ok=True) + target.write_text(code) + node.source_ref = None + definition.version += 1 + path.write_text(definition.model_dump_json(indent=2) + "\n") + self._lib_file(name).unlink() + self._commit(f"Detach and delete shared node '{name}'") + + def usages(self, lib_name: str, draft: bool = True) -> list[str]: + """Every ``flow.node`` pointing at this shared source. + + Read from the drafts the editor shows, or with ``draft`` off from the + published flows the engine runs. + """ used = [] for name in self.list_flows(): try: - definition = self.read_flow(name, draft=True) + definition = self.read_flow(name, draft=draft) except FlowNotFound: continue used += [ diff --git a/backend/fluksio/flow/supervision.py b/backend/fluksio/flow/supervision.py index ff2d624..b526271 100644 --- a/backend/fluksio/flow/supervision.py +++ b/backend/fluksio/flow/supervision.py @@ -26,11 +26,12 @@ from collections import deque from collections.abc import Callable, Coroutine from typing import Any +from fluksio.core.config import settings from fluksio.flow.events import EventBus logger = logging.getLogger(__name__) -BACKOFF = (1.0, 5.0, 30.0, 60.0) +BACKOFF = settings.FLOW_RESTART_BACKOFF # A task that burns through this many restarts in the window is not going to # recover by being restarted again *now*. # @@ -49,7 +50,7 @@ QUARANTINE_BACKOFF = (300.0, 900.0, 3600.0) # this is not going to stop on its own — a client closing a socket the broker # no longer answers on is the case seen in the wild — and the rebuild asking # for it must not wait on that forever. -CANCEL_GRACE = 5.0 +CANCEL_GRACE = settings.FLOW_CANCEL_GRACE TaskFactory = Callable[[], Coroutine[Any, Any, None]] diff --git a/backend/fluksio/flow/workers.py b/backend/fluksio/flow/workers.py index 7cb4b78..e678ae0 100644 --- a/backend/fluksio/flow/workers.py +++ b/backend/fluksio/flow/workers.py @@ -17,6 +17,7 @@ import itertools import logging import os import queue +import re import select import signal import subprocess @@ -102,6 +103,8 @@ class NodeCancelled(RemoteError): #: node code chose, so an unbounded cache is a node raising a class named after #: its loop counter. MAX_REMOTE_TYPES = 256 +#: How long one of those names may be. A remote worker's comes off the wire. +MAX_REMOTE_NAME = 128 _remote_types: dict[str, type[RemoteError]] = {} @@ -113,6 +116,7 @@ def _remote_class(name: str) -> type[RemoteError]: the author still reads ``ValueError: bad input`` rather than the name of the transport that carried it. """ + name = re.sub(r"\W", "_", name[:MAX_REMOTE_NAME]) or "RemoteError" cls = _remote_types.get(name) if cls is not None: return cls diff --git a/backend/fluksio/main.py b/backend/fluksio/main.py index 638f1d9..dfc87c8 100644 --- a/backend/fluksio/main.py +++ b/backend/fluksio/main.py @@ -49,6 +49,7 @@ from fluksio.flow.resources import ( fair_share_env, machine_gpus, machine_ram_mb, + warn_oversubscribed, ) from fluksio.flow.runs import RUN_STATE_TTL, RunService, sweep_artifacts from fluksio.flow.secrets import init_secrets @@ -145,6 +146,7 @@ async def _sweep_artifacts(store: ArtifactStore, controller: FlowController) -> store, controller.state, settings.ARTIFACT_GC_GRACE_S, + controller.store, ) except Exception: logger.exception("Artifact sweep failed") @@ -371,12 +373,20 @@ async def lifespan(app: FastAPI) -> AsyncIterator[None]: accountant.ram_mb, settings.FLOW_WORKER_MAX_RSS_MB, ) + warn_oversubscribed( + { + "FLOW_MAX_WORKERS": settings.FLOW_MAX_WORKERS, + "FLOW_MAX_CASCADES": settings.FLOW_MAX_CASCADES, + } + ) watchdog = LoopWatchdog(event_bus) app.state.watchdog = watchdog _background(watchdog.run(), "loop-watchdog") _background(alerts.run(), "alert-manager") - _background(MetricsCollector(event_bus).run(), "metrics-collector") + # On the app so a superuser can ask it to flush now. + app.state.metrics_collector = MetricsCollector(event_bus) + _background(app.state.metrics_collector.run(), "metrics-collector") _background(_sweep_artifacts(artifacts, controller), "artifact-gc") _background(_trim_volatile(volatile), "artifact-ring") await controller.start() @@ -530,19 +540,27 @@ async def rebuild_busy(request: Request, exc: RebuildBusy) -> JSONResponse: # n include_in_schema=False, tags=["oauth"], ) -def oauth_authorization_server() -> JSONResponse: +def oauth_authorization_server(request: Request) -> JSONResponse: """RFC 8414 metadata, so an agent can find its way in unaided. The authorization endpoint is the dashboard rather than the API: approving a client needs a signed-in human, and the browser session lives there. + Through the hub that is the dashboard the portal opens, which the hub names + when the link attaches; the hub's own address is the guess an older one + leaves. """ - issuer = settings.oauth_issuer + from fluksio.cloud import config as cloud_config + + base = cloud_config.portal_base(request.headers) + issuer = base or settings.oauth_issuer + dashboard = settings.FRONTEND_HOST.rstrip("/") + if base is not None: + connector = getattr(request.app.state, "cloud_connector", None) + dashboard = getattr(connector, "app_url", None) or base return JSONResponse( content={ "issuer": issuer, - "authorization_endpoint": ( - f"{settings.FRONTEND_HOST.rstrip('/')}/oauth/authorize" - ), + "authorization_endpoint": f"{dashboard}/oauth/authorize", "token_endpoint": f"{issuer}{settings.API_V1_STR}/oauth/token", "registration_endpoint": f"{issuer}{settings.API_V1_STR}/oauth/register", "jwks_uri": f"{issuer}/.well-known/jwks.json", diff --git a/backend/fluksio/mcp/http.py b/backend/fluksio/mcp/http.py index 3e1cc22..265062c 100644 --- a/backend/fluksio/mcp/http.py +++ b/backend/fluksio/mcp/http.py @@ -4,6 +4,9 @@ The transport is streamable HTTP, stateless, one JSON response per call: no sticky sessions to route and nothing for a proxy to buffer. The SDK serves both ``/mcp`` and the protected-resource metadata beside it, which is why the sub-app is mounted at the root rather than under ``/mcp``. + +An enrolled instance also answers at ``/i/{id}/mcp`` on its hub, so an agent +that came through the tunnel is shown the hub's addresses instead. """ from __future__ import annotations @@ -14,12 +17,16 @@ from urllib.parse import urlparse import jwt from fastapi.concurrency import run_in_threadpool from mcp.server.auth.provider import AccessToken +from mcp.server.auth.routes import build_resource_metadata_url from mcp.server.auth.settings import AuthSettings from mcp.server.transport_security import TransportSecuritySettings from pydantic import AnyHttpUrl -from starlette.applications import Starlette +from starlette.datastructures import Headers +from starlette.responses import JSONResponse +from starlette.types import ASGIApp, Message, Receive, Scope, Send from fluksio.api.deps import oauth_client_lives +from fluksio.cloud import config as cloud_config from fluksio.core import security from fluksio.core.config import settings from fluksio.mcp import server @@ -67,6 +74,20 @@ class _JWTVerifier: ) +class _AndTheHub(list[str]): + """The hosts named here, plus the hub's while this instance is enrolled. + + A call through the tunnel is addressed to the hub, as its Host says. The + hub is looked up per request, since enrolment comes and goes while the + endpoint is up. Its origin is not needed: the hub never forwards one. + """ + + def __contains__(self, host: object) -> bool: + config = cloud_config.load() + hub = urlparse(config.issuer).netloc if config is not None else None + return super().__contains__(host) or (hub is not None and host == hub) + + def _transport_security() -> TransportSecuritySettings: """Which Host headers to trust. @@ -87,14 +108,61 @@ def _transport_security() -> TransportSecuritySettings: "testserver", ] allowed_origins += ["http://localhost", "http://127.0.0.1"] - return TransportSecuritySettings( + # Unvalidated, since validating would copy the hosts into a plain list. + return TransportSecuritySettings.model_construct( enable_dns_rebinding_protection=True, - allowed_hosts=allowed_hosts, + allowed_hosts=_AndTheHub(allowed_hosts), allowed_origins=allowed_origins, ) -def build_http_app(app: object) -> Starlette: +class _ThroughThePortal: + """Show an agent that came through the hub the hub's addresses. + + The SDK builds its metadata once, for the API host, and names it in every + 401. An agent at ``/i/{id}/mcp`` on the hub cannot reach that host, so its + discovery has to lead to the hub's documents instead. + """ + + def __init__(self, app: ASGIApp) -> None: + self.app = app + + async def __call__(self, scope: Scope, receive: Receive, send: Send) -> None: + base = None + if scope["type"] == "http": + base = cloud_config.portal_base(Headers(scope=scope)) + if base is None: + await self.app(scope, receive, send) + return + if scope["path"] == "/.well-known/oauth-protected-resource/mcp": + document = { + "resource": f"{base}/mcp", + "authorization_servers": [base], + "bearer_methods_supported": ["header"], + } + headers = {"Cache-Control": "public, max-age=3600"} + await JSONResponse(document, headers=headers)(scope, receive, send) + return + here = str(build_resource_metadata_url(AnyHttpUrl(settings.mcp_resource))) + there = str(build_resource_metadata_url(AnyHttpUrl(f"{base}/mcp"))) + + async def send_theirs(message: Message) -> None: + if message["type"] == "http.response.start": + message["headers"] = [ + ( + key, + value.replace(here.encode(), there.encode()) + if key == b"www-authenticate" + else value, + ) + for key, value in message["headers"] + ] + await send(message) + + await self.app(scope, receive, send_theirs) + + +def build_http_app(app: object) -> ASGIApp: """The MCP sub-app, wired to this app's API and its OAuth server.""" server.mcp.settings.stateless_http = True server.mcp.settings.json_response = True @@ -109,4 +177,4 @@ def build_http_app(app: object) -> Starlette: server.mcp._token_verifier = _JWTVerifier() # noqa: SLF001 server.connect(app) - return server.mcp.streamable_http_app() + return _ThroughThePortal(server.mcp.streamable_http_app()) diff --git a/backend/fluksio/mcp/server.py b/backend/fluksio/mcp/server.py index 5c7b5a8..4cffc76 100644 --- a/backend/fluksio/mcp/server.py +++ b/backend/fluksio/mcp/server.py @@ -208,7 +208,9 @@ async def save_node_source(name: str, node_id: str, code: str) -> Any: """Save a node's Python source and report whether it compiles. A node defines ``process(...)``, taking one argument per input port and - one per setting, and returns a dict keyed by output port. + one per setting, and returns a dict keyed by output port. A parameter added + or dropped there adds or drops the input port or setting in the draft, + which moves its version on: read the flow again before saving it. """ return await _call( "PUT", f"/flows/{_seg(name)}/nodes/{_seg(node_id)}/source", json={"code": code} diff --git a/backend/fluksio/models.py b/backend/fluksio/models.py index 3235b06..ff6902e 100644 --- a/backend/fluksio/models.py +++ b/backend/fluksio/models.py @@ -298,6 +298,9 @@ class FlowRun(SQLModel, table=True): flow: str = Field(index=True, max_length=255) #: What caused it: the work item's cause, "manual", or "run" for a batch. source: str = "" + #: The run this one continues: on a rate-limit flush, the run whose value + #: the window held back. Empty for everything else. + parent: str = Field(default="", max_length=64) started_at: datetime = Field( index=True, sa_type=UTCDateTime, @@ -359,6 +362,9 @@ class Run(SQLModel, table=True): #: The names in `params` the caller chose; the rest are the flow's declared #: values. Null for a run recorded before this was kept. passed: list[str] | None = Field(sa_column=Column(JSON), default=None) + #: The inputs the flow declared when this was submitted, by name, with + #: their declared values. Null for a run recorded before this was kept. + declared: dict[str, Any] | None = Field(sa_column=Column(JSON), default=None) #: sha256 over params and seed. Two runs of the same thing share it, which #: is what makes "have I already run this?" a lookup. params_digest: str = Field(default="", index=True, max_length=64) @@ -426,7 +432,9 @@ class RunNode(SQLModel, table=True): __tablename__ = "run_node" - run_id: str = Field(primary_key=True, max_length=64) + run_id: str = Field( + primary_key=True, max_length=64, foreign_key="run.id", ondelete="CASCADE" + ) node: str = Field(primary_key=True, max_length=255) #: ok, error, skipped, cached or cancelled. status: str = Field(default="ok", max_length=16) @@ -464,7 +472,9 @@ class RunMetric(SQLModel, table=True): __tablename__ = "run_metric" - run_id: str = Field(primary_key=True, max_length=64) + run_id: str = Field( + primary_key=True, max_length=64, foreign_key="run.id", ondelete="CASCADE" + ) name: str = Field(primary_key=True, max_length=128) #: -1 for a value with no step of its own — a final score. step: int = Field(primary_key=True) @@ -478,7 +488,9 @@ class RunArtifact(SQLModel, table=True): __tablename__ = "run_artifact" - run_id: str = Field(primary_key=True, max_length=64) + run_id: str = Field( + primary_key=True, max_length=64, foreign_key="run.id", ondelete="CASCADE" + ) #: The message the bytes left the node on, which is what addresses them. name: str = Field(primary_key=True, max_length=255) #: What the node called the file, when it said. Kept because a reference diff --git a/backend/fluksio/sdk/cli.py b/backend/fluksio/sdk/cli.py index 7393d56..23fc960 100644 --- a/backend/fluksio/sdk/cli.py +++ b/backend/fluksio/sdk/cli.py @@ -17,6 +17,7 @@ import pkgutil import shutil import sys import time +import warnings from collections.abc import Callable, Iterable, Iterator from contextlib import contextmanager, nullcontext from datetime import UTC, datetime @@ -452,15 +453,20 @@ def _ask_params(definition: dict[str, Any]) -> dict[str, Any]: def _params(definition: dict[str, Any], rest: list[str]) -> dict[str, Any]: - """Turn `--lr 0.05` into a typed parameter, using the flow's own inputs.""" + """Turn `--lr 0.05` into a typed parameter, using the flow's own inputs. + + After `--` every token is `name=value`, which is how an input that shares + its name with a flag of `run` is set. + """ types = _input_types(definition) + cut = rest.index("--") if "--" in rest else len(rest) # Collected as written and typed afterwards, so a name this flow does not # have is refused by name rather than by whatever its value failed to parse # as: `--param lr=0.002` is a sweep's spelling, and said so it reads as an # input called `param` holding unparseable json. raw: dict[str, str | bool] = {} pending: str | None = None - for token in rest: + for token in rest[:cut]: if token.startswith("--"): if pending is not None: # A flag with no value is a flag: `--resume` means true. @@ -479,6 +485,11 @@ def _params(definition: dict[str, Any], rest: list[str]) -> dict[str, Any]: pending = None if pending is not None: raw[pending] = True + for token in rest[cut + 1 :]: + name, sep, value = token.partition("=") + if not sep or not name: + raise SyncError(f"after `--` an input is name=value, not '{token}'") + raw[name.replace("-", "_")] = value unknown = sorted(set(raw) - set(types)) if unknown: hint = "" @@ -1326,7 +1337,11 @@ def _export( try: if getattr(args, "list_names", False): return _list_names(client, args) - rows = fetch(client) + with warnings.catch_warnings(record=True) as caught: + warnings.simplefilter("always") + rows = fetch(client) + for warning in caught: + print(f"fluksio: warning: {warning.message}", file=sys.stderr) except ApiError as exc: if exc.status != 404: raise diff --git a/backend/fluksio/sdk/client.py b/backend/fluksio/sdk/client.py index 0ef0ded..fa71835 100644 --- a/backend/fluksio/sdk/client.py +++ b/backend/fluksio/sdk/client.py @@ -12,6 +12,7 @@ import os import subprocess import time import uuid +import warnings from collections.abc import Iterable from pathlib import Path from typing import Any @@ -511,6 +512,8 @@ class Client: jsonl on the wire rather than csv: it keeps the types the engine has, and it is the format the CLI converts to parquet from. Read whole — the streaming is the engine's side, and a notebook wants a list. + A selection past the engine's cap comes back without its oldest runs, + and that is a warning, since the rows alone look complete. """ if flow: query["flow"] = flow @@ -523,6 +526,16 @@ class Client: response = self._request("GET", path, idempotent=True, params=query) if response.status_code >= 400: raise ApiError(response.status_code, _detail(response)) + cap = response.headers.get("X-Truncated") + if cap: + # An engine older than the count sends the cap alone. + total = response.headers.get("X-Total-Count") + left = f"{int(total) - int(cap)} older runs" if total else "older runs" + warnings.warn( + f"the export stops at {cap} runs, so {left} of this selection " + "were left out; narrow it by flow, status, since or until", + stacklevel=3, + ) return [json.loads(line) for line in response.text.splitlines() if line] def cancel(self, run_id: str) -> Any: diff --git a/backend/pyproject.toml b/backend/pyproject.toml index 18d7911..dfbe63f 100644 --- a/backend/pyproject.toml +++ b/backend/pyproject.toml @@ -17,6 +17,7 @@ classifiers = [ "Programming Language :: Python :: 3", "Programming Language :: Python :: 3.12", "Programming Language :: Python :: 3.13", + "Programming Language :: Python :: 3.14", "Topic :: Home Automation", "Topic :: Scientific/Engineering", "Topic :: System :: Distributed Computing", @@ -58,6 +59,10 @@ dependencies = [ # The `mlp` node, which is registered at boot. Kept out of the extras # because a data-science environment has it already. "numpy>=2.2.6", + # The `wren` node runs its model on one BLAS thread, which answers in about + # half the time. Scoped to the call: OPENBLAS_NUM_THREADS would bind every + # other numpy call in the engine too, and only counts before numpy loads. + "threadpoolctl>=3.5", # The `inject` node's cron. Imported where it is used, behind a message # that cannot fire while this line is here. "croniter>=1.3.0", @@ -114,7 +119,7 @@ fluksio-worker = { workspace = true } [dependency-groups] dev = [ - "pytest<8.0.0,>=7.4.3", + "pytest<10.0.0,>=9.0.3", "mypy<2.0.0,>=1.8.0", "ruff<1.0.0,>=0.2.2", "prek>=0.2.24,<1.0.0", @@ -164,6 +169,11 @@ ignore_missing_imports = true module = ["wren"] ignore_missing_imports = true +# What scopes the `wren` node's BLAS threads to its call; it ships no types. +[[tool.mypy.overrides]] +module = ["threadpoolctl"] +ignore_missing_imports = true + [tool.ruff] target-version = "py312" diff --git a/backend/scripts/soak.py b/backend/scripts/soak.py index a22a29d..609fca5 100644 --- a/backend/scripts/soak.py +++ b/backend/scripts/soak.py @@ -4,11 +4,14 @@ Standalone on purpose, and never part of the test run. It drives the running stack through the *durable* path — the webhook, which journals every trigger to the work queue — and then takes pieces of the stack away to see what survives. - make soak - make soak SOAK_ARGS="--minutes 30 --rps 25" - make soak SOAK_ARGS="--scenario redis" + make soak SOAK_ARGS="--force" + make soak SOAK_ARGS="--minutes 30 --rps 25 --scenario load" + make soak SOAK_ARGS="--scenario redis --force" make soak SOAK_ARGS="--dry-run" +The redis scenario, which `all` includes, needs `--force`: the Redis it stops +is the one the whole stack journals to, not a copy of its own. + Every docker verb goes through :func:`docker`, which refuses any container that is not part of this project's compose stack. The machine this runs on hosts unrelated services; nothing here may ever reach them. @@ -36,7 +39,6 @@ from typing import Any import httpx from fluksio.core.config import settings -from fluksio.flow.metrics import FLUSH_INTERVAL_S log = logging.getLogger("soak") @@ -149,8 +151,9 @@ PREFIX = "soak_" FLOW_LOAD = f"{PREFIX}load" FLOW_CHAIN = f"{PREFIX}chain" FLOW_MQTT = f"{PREFIX}mqtt" +FLOW_SLOW = f"{PREFIX}slow" #: Deletion order: whatever reads another flow's messages goes first. -FLOWS = (FLOW_MQTT, FLOW_CHAIN, FLOW_LOAD) +FLOWS = (FLOW_SLOW, FLOW_MQTT, FLOW_CHAIN, FLOW_LOAD) COUNT_SOURCE = '''"""Count what made it through, reading the total it also writes.""" @@ -159,6 +162,16 @@ def process(tick, total=0, params=None): return {"total": int(total) + 1} ''' +SLOW_SOURCE = '''"""Hold the cascade open, so an engine kill finds work in flight.""" + +import time + + +def process(total, pause): + time.sleep(float(pause)) + return {"done": total} +''' + def spec(name: str, dtype: str = "int", trigger: bool = True) -> dict[str, Any]: return {"name": name, "dtype": dtype, "trigger": trigger} @@ -214,6 +227,30 @@ def chain_flow() -> dict[str, Any]: } +def slow_flow(pause: float) -> dict[str, Any]: + """A sleep at the end of the load's cascade, for the engine scenario only. + + It reads the total, so it runs after the count and never holds a node + worker the count is waiting for. + """ + return { + "name": FLOW_SLOW, + "title": "Soak slow", + "version": 0, + "nodes": [ + { + "id": "pause", + "type": "python", + "title": "Pause", + "position": {"x": 40, "y": 40}, + "params": {"pause": pause}, + "requires": [spec(f"{FLOW_LOAD}.total")], + "provides": [spec("done")], + } + ], + } + + def mqtt_flow() -> dict[str, Any]: """A subscriber on the dev stack's own broker, for the broker scenario.""" return { @@ -339,6 +376,14 @@ def create_flow( log.info("created flow '%s'", name) +def flush_metrics(client: httpx.Client) -> None: + """Have the metrics collector write what it holds now.""" + try: + client.post("/api/v1/observability/flush").raise_for_status() + except httpx.HTTPError as exc: + log.error("could not flush the metrics collector: %s", exc) + + def delete_flows(client: httpx.Client) -> None: for name in FLOWS: try: @@ -691,6 +736,12 @@ def scenario_engine(client: httpx.Client, args: argparse.Namespace, url: str) -> report = Report("engine") dead_before = dead_letters(client) + # The count alone finishes in a few milliseconds, so a kill used to catch + # next to nothing in flight. This pause keeps about three quarters of the + # engine's cascade slots busy at this rate, and the engine still keeps up. + pause = 0.75 * settings.FLOW_MAX_CASCADES / args.rps + create_flow(client, slow_flow(pause), {"pause": SLOW_SOURCE}) + driver = Driver(url, args.rps) log.info("engine: %d rps, killing %s halfway through", args.rps, API) driver.start() @@ -700,6 +751,8 @@ def scenario_engine(client: httpx.Client, args: argparse.Namespace, url: str) -> # Read after the total, so work already accepted but not yet counted makes # the claim below easier rather than impossible. accepted_at_kill = driver.accepted + # Claimed and not acknowledged, so what the reaper has to hand back. + unacked = queue_stats(client).get("pending", "?") docker(API, "kill", API) time.sleep(2.0) @@ -707,6 +760,7 @@ def scenario_engine(client: httpx.Client, args: argparse.Namespace, url: str) -> # it back — and it must never be left down. docker(API, "start", API) recovered = wait_for(lambda: health_code(client) == 200, HEALTH_RECOVER_S, 2.0) + report.stat(f"unacknowledged at the kill {unacked}, stack-wide") report.stat(f"engine answered healthy again {seconds(recovered)} after restart") report.check( f"health back to 200 within {HEALTH_RECOVER_S:.0f}s of the restart", @@ -805,13 +859,13 @@ def clear_redis() -> None: def clear_observability() -> None: """The collector wrote rollups, runs and events for the soak flows.""" - names = ", ".join(repr(flow) for flow in FLOWS) + marks = ", ".join("?" for _ in FLOWS) script = "\n".join( [ "import sqlite3", "db = sqlite3.connect('/data/fluksio.db')", *( - f"db.execute('DELETE FROM {table} WHERE flow IN ({names})')" + f"db.execute('DELETE FROM {table} WHERE flow IN ({marks})', {FLOWS!r})" for table in ("metric_minute", "flow_run", "engine_event") ), "db.commit()", @@ -843,11 +897,11 @@ def cleanup(client: httpx.Client | None) -> None: delete_flows(client) clear_redis() - # Deleting a flow publishes an audit event of its own, and the metrics - # collector buffers what it hears for up to FLUSH_INTERVAL_S before writing - # it. Clearing the tables any sooner races that flush and leaves the rows - # this very cleanup caused. - time.sleep(FLUSH_INTERVAL_S + 5.0) + # Deleting a flow publishes an audit event of its own, which the metrics + # collector holds until its next flush. Clearing the tables before it is + # written would leave the rows this very cleanup caused. + if client is not None: + flush_metrics(client) clear_observability() @@ -885,7 +939,18 @@ def parse_args(argv: list[str] | None) -> argparse.Namespace: action="store_true", help="Report what the harness can see and change nothing.", ) + parser.add_argument( + "--force", + action="store_true", + help="Allow the redis scenario, which stops the Redis the whole stack shares.", + ) args = parser.parse_args(argv) + if args.scenario in ("all", "redis") and not (args.force or args.dry_run): + parser.error( + f"the redis scenario stops {REDIS}, which the whole stack shares, so " + "every flow briefly fails to journal, not just the soak fixtures. " + "Pass --force on a stack nobody relies on, or pick another --scenario." + ) args.rps = max(1, min(MAX_RPS, args.rps)) args.seconds = max(10.0, args.minutes * 60) return args diff --git a/backend/tests/api/routes/test_flows.py b/backend/tests/api/routes/test_flows.py index 1e8f708..a83a281 100644 --- a/backend/tests/api/routes/test_flows.py +++ b/backend/tests/api/routes/test_flows.py @@ -62,6 +62,11 @@ def test_save_then_read_flow( assert body["definition"]["title"] == "Demo" assert {node["id"] for node in body["definition"]["nodes"]} == {"sensor", "logger"} + # The time beside a version carries its offset, so no client guesses a zone. + listed = client.get(f"{PREFIX}/", headers=superuser_token_headers).json()["data"] + summary = next(flow for flow in listed if flow["name"] == "demo") + assert datetime.fromisoformat(summary["updated_at"]).tzinfo is not None + def test_name_mismatch_is_rejected( client: TestClient, superuser_token_headers: dict[str, str] @@ -550,3 +555,125 @@ async def test_the_live_socket_ends_when_its_client_goes( await asyncio.sleep(0.05) parked = [t for t in asyncio.all_tasks() if "Queue.get" in repr(t.get_coro())] assert parked == [] + + +def test_saving_a_header_moves_the_node_ports_in_the_draft( + client: TestClient, superuser_token_headers: dict[str, str] +) -> None: + saved = client.put( + f"{PREFIX}/header", headers=superuser_token_headers, json=a_flow("header") + ).json() + + response = client.put( + f"{PREFIX}/header/nodes/logger/source", + headers=superuser_token_headers, + json={"code": "def process(reading, level, gain=2):\n return {}\n"}, + ).json() + + assert [spec["port"] for spec in response["node"]["requires"]] == [ + "reading", + "level", + ] + assert response["node"]["params"] == {"gain": 2} + draft = client.get(f"{PREFIX}/header", headers=superuser_token_headers).json() + assert draft["definition"]["version"] == response["version"] + assert response["version"] == saved["definition"]["version"] + 1 + logger = next(n for n in draft["definition"]["nodes"] if n["id"] == "logger") + assert logger["params"] == {"gain": 2} + + client.delete(f"{PREFIX}/header", headers=superuser_token_headers) + + +def test_a_shared_node_in_use_is_retired_only_with_detach( + client: TestClient, superuser_token_headers: dict[str, str] +) -> None: + client.put( + f"{PREFIX}/shared", headers=superuser_token_headers, json=a_flow("shared") + ) + client.put( + f"{PREFIX}/shared/nodes/sensor/source", + headers=superuser_token_headers, + json={"code": WORKING_NODE}, + ) + client.post( + f"{PREFIX}/shared/nodes/sensor/share", + headers=superuser_token_headers, + json={"lib_name": "reading"}, + ) + + url = f"{PREFIX}/library/reading" + assert client.delete(url, headers=superuser_token_headers).status_code == 409 + response = client.delete(f"{url}?detach=true", headers=superuser_token_headers) + assert response.status_code == 200 + + assert client.get(f"{PREFIX}/library", headers=superuser_token_headers).json() == [] + source = client.get( + f"{PREFIX}/shared/nodes/sensor/source", headers=superuser_token_headers + ) + assert source.json()["code"] == WORKING_NODE + + client.delete(f"{PREFIX}/shared", headers=superuser_token_headers) + + +def test_a_shared_node_only_the_published_flow_uses_is_kept( + client: TestClient, superuser_token_headers: dict[str, str] +) -> None: + """An unshare in the draft leaves the running flow on the source.""" + client.put( + f"{PREFIX}/unshared", headers=superuser_token_headers, json=a_flow("unshared") + ) + shared = client.post( + f"{PREFIX}/unshared/nodes/sensor/share", + headers=superuser_token_headers, + json={"lib_name": "published_reading"}, + ).json() + client.post( + f"{PREFIX}/unshared/publish", + headers=superuser_token_headers, + json={"version": shared["definition"]["version"]}, + ) + client.post( + f"{PREFIX}/unshared/nodes/sensor/unshare", headers=superuser_token_headers + ) + + url = f"{PREFIX}/library/published_reading" + response = client.delete(url, headers=superuser_token_headers) + assert response.status_code == 409 + assert "unshared.sensor" in response.json()["detail"] + + client.delete(f"{url}?detach=true", headers=superuser_token_headers) + client.delete(f"{PREFIX}/unshared", headers=superuser_token_headers) + + +def test_a_shared_source_write_is_audited( + client: TestClient, + superuser_token_headers: dict[str, str], + monkeypatch: pytest.MonkeyPatch, +) -> None: + """It goes live at once, so it is recorded like a publish.""" + client.put( + f"{PREFIX}/audited", headers=superuser_token_headers, json=a_flow("audited") + ) + client.post( + f"{PREFIX}/audited/nodes/sensor/share", + headers=superuser_token_headers, + json={"lib_name": "audited_reading"}, + ) + + published: list[dict] = [] + monkeypatch.setattr(event_bus, "publish", published.append) + client.put( + f"{PREFIX}/audited/nodes/sensor/source", + headers=superuser_token_headers, + json={"code": WORKING_NODE}, + ) + + (audit,) = [e for e in published if e["type"] == "audit"] + assert audit["action"] == "edited shared node 'audited_reading'" + assert audit["flow"] == "audited" + + client.delete( + f"{PREFIX}/library/audited_reading?detach=true", + headers=superuser_token_headers, + ) + client.delete(f"{PREFIX}/audited", headers=superuser_token_headers) diff --git a/backend/tests/api/routes/test_oauth.py b/backend/tests/api/routes/test_oauth.py index 100e05c..de21684 100644 --- a/backend/tests/api/routes/test_oauth.py +++ b/backend/tests/api/routes/test_oauth.py @@ -80,6 +80,44 @@ def test_the_metadata_says_how_to_get_in(client: TestClient) -> None: } +@pytest.mark.usefixtures("enrolled") +def test_through_the_hub_the_metadata_and_resource_are_the_hubs( + client: TestClient, superuser_token_headers: dict[str, str] +) -> None: + """An agent at /i/{id}/mcp on the hub is sent to the hub's addresses.""" + from fluksio.cloud import config as cloud_config + from tests.utils.portal import INSTANCE_ID, ISSUER + + via = {cloud_config.VIA_HEADER: cloud_config.VIA_PORTAL} + base = f"{ISSUER}/i/{INSTANCE_ID}" + document = client.get("/.well-known/oauth-authorization-server", headers=via).json() + assert document["issuer"] == base + assert document["token_endpoint"] == f"{base}{PREFIX}/token" + # No link is attached here to name the portal's page, so the hub's own. + assert document["authorization_endpoint"] == f"{base}/oauth/authorize" + # A request made here hears nothing about the hub. + local = client.get("/.well-known/oauth-authorization-server").json() + assert local["issuer"] == settings.oauth_issuer + + _verifier, challenge = pkce() + body = { + "client_id": register(client)["client_id"], + "redirect_uri": REDIRECT, + "code_challenge": challenge, + "code_challenge_method": "S256", + "resource": f"{base}/mcp", + } + through = client.post( + f"{PREFIX}/authorize", headers={**superuser_token_headers, **via}, json=body + ) + assert through.status_code == 200, through.text + # Only through the hub is that a resource this instance serves. + here = client.post( + f"{PREFIX}/authorize", headers=superuser_token_headers, json=body + ) + assert here.json()["error"] == "invalid_target" + + def test_a_full_authorization_reaches_the_api( client: TestClient, superuser_token_headers: dict[str, str] ) -> None: diff --git a/backend/tests/api/routes/test_observability.py b/backend/tests/api/routes/test_observability.py index 6e8f963..6a2984f 100644 --- a/backend/tests/api/routes/test_observability.py +++ b/backend/tests/api/routes/test_observability.py @@ -1,9 +1,11 @@ +import time from datetime import UTC, datetime, timedelta from fastapi.testclient import TestClient -from sqlmodel import Session +from sqlmodel import Session, select from fluksio.core.config import settings +from fluksio.flow.events import event_bus from fluksio.models import EngineEvent, FlowRun, MetricBucket PREFIX = f"{settings.API_V1_STR}/observability" @@ -482,3 +484,87 @@ def test_a_node_failing_on_every_call_makes_the_summary_degraded( assert body["nodes"]["failing"] == 1 (problem,) = [p for p in body["problems"] if "failing" in p] assert "house.calc" in problem and "house.old" not in problem + + +def test_a_superuser_can_flush_the_collector_now( + client: TestClient, + superuser_token_headers: dict[str, str], + normal_user_token_headers: dict[str, str], + db: Session, +) -> None: + """Whoever reads or clears the tables need not wait out the interval.""" + event_bus.publish( + { + "type": "audit", + "action": "flushed", + "flow": FLOW, + "user": "a@example.com", + "ts": time.time(), + } + ) + url = f"{PREFIX}/flush" + assert client.post(url, headers=normal_user_token_headers).status_code == 403 + assert client.post(url, headers=superuser_token_headers).status_code == 200 + + written = db.exec( + select(EngineEvent).where( + EngineEvent.flow == FLOW, EngineEvent.detail == "flushed" + ) + ).all() + assert len(written) == 1 + + +def test_execution_time_is_summed_per_node_type( + client: TestClient, superuser_token_headers: dict[str, str], db: Session +) -> None: + from fluksio.flow.schemas import FlowDef, NodeDef + + flow = "node_type_test" + controller = client.app.state.flow_controller + before = controller.store.read_all + controller.store.read_all = lambda: [ + FlowDef( + name=flow, + nodes=[ + NodeDef(id="a", type="python"), + NodeDef(id="b", type="python"), + NodeDef(id="tick", type="inject"), + ], + ) + ] + now = datetime.now(UTC).replace(second=0, microsecond=0) + for node, executions, total, peak in ( + ("a", 2, 30.0, 20.0), + ("b", 2, 10.0, 6.0), + ("tick", 4, 0.4, 0.2), + # Deleted since it ran: nothing to count it under. + ("gone", 1, 500.0, 500.0), + ): + db.add( + MetricBucket( + flow=flow, + node=f"{flow}.{node}", + bucket=now - timedelta(minutes=2), + executions=executions, + duration_sum_ms=total, + duration_max_ms=peak, + ) + ) + db.commit() + try: + rows = client.get( + f"{PREFIX}/node-types", headers=superuser_token_headers + ).json() + finally: + controller.store.read_all = before + + # Slowest first. + assert [row["type"] for row in rows] == ["python", "inject"] + assert rows[0] == { + "type": "python", + "nodes": 2, + "executions": 4, + "avg_ms": 10.0, + "max_ms": 20.0, + } + assert rows[1]["avg_ms"] == 0.1 diff --git a/backend/tests/api/routes/test_panels.py b/backend/tests/api/routes/test_panels.py index a2622ed..7259ef8 100644 --- a/backend/tests/api/routes/test_panels.py +++ b/backend/tests/api/routes/test_panels.py @@ -11,7 +11,11 @@ DASHBOARDS = f"{settings.API_V1_STR}/dashboards" def _panels(client: TestClient, headers: dict[str, str], config: dict) -> None: - response = client.put(f"{PREFIX}/", headers=headers, json=config) + """Replace the panels, based on whatever is stored now.""" + version = client.get(f"{PREFIX}/", headers=headers).json()["version"] + response = client.put( + f"{PREFIX}/", headers=headers, json={**config, "version": version} + ) assert response.status_code == 200, response.text @@ -130,6 +134,39 @@ def test_duplicate_panel_is_refused( assert response.status_code == 422 +def test_a_stale_save_is_refused( + client: TestClient, superuser_token_headers: dict[str, str] +) -> None: + """Two clients writing back the whole list must not drop each other's panels.""" + read = client.get(f"{PREFIX}/", headers=superuser_token_headers).json() + base = read["version"] + + saved = client.put( + f"{PREFIX}/", + headers=superuser_token_headers, + json={"panels": [*read["panels"], {"id": "mine"}], "version": base}, + ) + assert saved.status_code == 200, saved.text + assert saved.json()["version"] == base + 1 + + stale = client.put( + f"{PREFIX}/", + headers=superuser_token_headers, + json={"panels": [*read["panels"], {"id": "theirs"}], "version": base}, + ) + assert stale.status_code == 409 + assert stale.json()["detail"]["current_version"] == base + 1 + + ids = [ + p["id"] + for p in client.get(f"{PREFIX}/", headers=superuser_token_headers).json()[ + "panels" + ] + ] + assert "mine" in ids + assert "theirs" not in ids + + def test_polling_needs_the_secret(client: TestClient) -> None: started = client.post(f"{PREFIX}/pair").json() assert len(started["code"]) == 6 @@ -336,7 +373,8 @@ def test_a_panel_speaks_only_its_own_widgets_messages( == 404 ), target - # And reads the history of everything its widgets bind to. + # And reads the history of everything its widgets bind to, but writes none + # of what they only draw. for name in ( "demo.temperature", "demo.switch_state", @@ -347,6 +385,12 @@ def test_a_panel_speaks_only_its_own_widgets_messages( client.get(f"{messages}/{name}/history", headers=panel_headers).status_code == 200 ), name + assert ( + client.post( + f"{messages}/{name}", headers=panel_headers, json={"value": 1} + ).status_code + == 403 + ), name # A message no tile on this panel names is not its business, in either # direction. diff --git a/backend/tests/api/routes/test_runs.py b/backend/tests/api/routes/test_runs.py index 35bd238..0f1d23c 100644 --- a/backend/tests/api/routes/test_runs.py +++ b/backend/tests/api/routes/test_runs.py @@ -7,6 +7,7 @@ The pipeline half — what a hit restores and what a key is made of — is in import base64 import csv import json +import logging import threading from datetime import UTC, datetime, timedelta @@ -18,7 +19,7 @@ from fluksio.core.db import engine as db_engine from fluksio.flow.artifacts import ArtifactStore from fluksio.flow.controller import FlowController from fluksio.flow.messages import DType, MessageSpec -from fluksio.flow.pipeline import NodeOutcome +from fluksio.flow.pipeline import CACHE_SHAPE_SINCE, NodeOutcome from fluksio.flow.runs import ( LEASE_STALE_S, OUTPUT_CAP, @@ -27,12 +28,13 @@ from fluksio.flow.runs import ( RunService, _cacheable, new_run_id, + prune_stale_cache, resolve_references, seed_values, ) from fluksio.flow.schemas import FlowDef, FlowInput, NodeDef from fluksio.flow.store import FlowStore -from fluksio.models import Run, RunArtifact, RunMetric, RunNode +from fluksio.models import FlowRun, Run, RunArtifact, RunMetric, RunNode def test_a_run_cache_finds_what_an_earlier_run_recorded(tmp_path): @@ -41,6 +43,10 @@ def test_a_run_cache_finds_what_an_earlier_run_recorded(tmp_path): reference = store.put([b"payload"], name="data.bin") plain, with_artifact, collected = "k-plain", "k-artifact", "k-collected" with Session(db_engine) as session: + for run_id in ("cache-1", "cache-2", "cache-3"): + session.add( + Run(id=run_id, flow="study", status="ok", created_at=datetime.now(UTC)) + ) session.add( RunNode( run_id="cache-1", @@ -101,6 +107,38 @@ def test_what_may_be_stored_as_a_cache_entry(): assert _cacheable(ok.model_copy(update={"output_values": big})) is None +def test_a_row_keyed_before_the_current_shape_is_unkeyed(): + """It matches nothing any more, but it is still the run's record.""" + outputs = json.dumps({"study.loss": 1.0}) + with Session(db_engine) as session: + for run_id, made in ( + ("shape-old", CACHE_SHAPE_SINCE - timedelta(days=1)), + ("shape-new", CACHE_SHAPE_SINCE + timedelta(seconds=1)), + ): + session.add(Run(id=run_id, flow="study", status="ok", created_at=made)) + session.add( + RunNode( + run_id=run_id, + node="study.a", + cache_key=run_id, + outputs=outputs, + ) + ) + session.commit() + try: + assert prune_stale_cache() == 1 + with Session(db_engine) as session: + old = session.get(RunNode, ("shape-old", "study.a")) + new = session.get(RunNode, ("shape-new", "study.a")) + assert old is not None and old.cache_key == "" and old.outputs == outputs + assert new is not None and new.cache_key == "shape-new" + finally: + with Session(db_engine) as session: + for run_id in ("shape-old", "shape-new"): + session.delete(session.get(Run, run_id)) + session.commit() + + # ----------------------------------------------------------------------------- # Naming an artifact from outside the process that made it # @@ -456,6 +494,9 @@ def test_a_run_records_the_inputs_it_actually_starts_from(): assert spelled.params_digest == defaulted.params_digest # Only what the caller chose says which value was the flow's own. assert (defaulted.passed, spelled.passed) == (["epochs"], ["epochs", "lr"]) + # And what the flow declared then, so an input it adds later is not + # drawn on this run as a default it never received. + assert defaulted.declared == {"lr": 0.01, "epochs": None} finally: with Session(db_engine) as session: for run in session.exec(select(Run).where(col(Run.id).in_(made))).all(): @@ -888,7 +929,7 @@ def test_overview_is_not_read_as_a_run_id(client, superuser_token_headers): # ----------------------------------------------------------------------------- # Deleting a run # -# The route owns the four statements; what these guard is that it takes the +# The foreign keys cascade; what these guard is that deleting a run takes the # children with it and refuses a run the driver is still writing to. # ----------------------------------------------------------------------------- @@ -925,7 +966,7 @@ def deletable_run(): def test_deleting_a_run_takes_its_children_with_it( client, superuser_token_headers, deletable_run ): - """No foreign key cascades here, so the route has to do it itself.""" + """The foreign keys cascade, which needs them on for every connection.""" answer = client.delete( f"{settings.API_V1_STR}/runs/{deletable_run}", headers=superuser_token_headers ) @@ -940,6 +981,18 @@ def test_deleting_a_run_takes_its_children_with_it( assert left == [], f"{table.__name__} rows outlived the run" +def test_a_node_finishing_after_its_run_was_deleted_is_dropped_quietly(caplog): + """The constraint refuses its row, and that is a no-op rather than an error.""" + service = RunService(controller=_OneFlow(FlowDef(name="gone")), queue=_Collect()) + with caplog.at_level(logging.INFO, logger="fluksio.flow.runs"): + service._record_node("del-gone", NodeOutcome(node="gone.a", ok=True)) + + with Session(db_engine) as session: + assert session.get(RunNode, {"run_id": "del-gone", "node": "gone.a"}) is None + assert "is gone" in caplog.text + assert not [record for record in caplog.records if record.levelno >= logging.ERROR] + + def test_deleting_a_run_that_is_not_there_is_a_404(client, superuser_token_headers): answer = client.delete( f"{settings.API_V1_STR}/runs/nope-1", headers=superuser_token_headers @@ -1021,6 +1074,33 @@ def test_deleting_a_flows_runs_clears_that_flow_only(client, superuser_token_hea session.commit() +def test_history_also_clears_a_flows_cascades(client, superuser_token_headers): + """What deleting a flow together with its runs asks for.""" + made = datetime.now(UTC) + url = f"{settings.API_V1_STR}/runs" + with Session(db_engine) as session: + session.add(Run(id="hist-1", flow="gone", status="ok", created_at=made)) + session.add(FlowRun(id="hist-1", flow="gone", source="run", started_at=made)) + session.add(FlowRun(id="hist-2", flow="gone", started_at=made)) + session.add(FlowRun(id="hist-3", flow="other", started_at=made)) + session.commit() + + plain = client.delete(url, params={"flow": "gone"}, headers=superuser_token_headers) + assert plain.status_code == 200 + with Session(db_engine) as session: + assert session.get(FlowRun, "hist-2") is not None + + answer = client.delete( + url, params={"flow": "gone", "history": True}, headers=superuser_token_headers + ) + assert answer.status_code == 200 + with Session(db_engine) as session: + left = session.exec(select(FlowRun).where(col(FlowRun.flow) == "gone")).all() + assert left == [] + session.delete(session.get(FlowRun, "hist-3")) + session.commit() + + # ----------------------------------------------------------------------------- # A cached node's curve # @@ -1105,6 +1185,42 @@ def test_a_curve_whose_run_is_gone_is_empty_rather_than_an_error( ) assert answer.status_code == 200 assert answer.json() == [] + # And the run says why, rather than leaving an empty chart to explain itself. + (node,) = client.get( + f"{settings.API_V1_STR}/runs/reuse-1", headers=superuser_token_headers + ).json()["nodes"] + assert node["curve_lost"] is True + + +def test_a_node_answers_with_what_it_returned(client, superuser_token_headers): + """What the stage cache stored, and a 404 that says why for a node without.""" + run_id = new_run_id() + with Session(db_engine) as session: + session.add( + Run(id=run_id, flow="study", status="ok", created_at=datetime.now(UTC)) + ) + session.add(RunNode(run_id=run_id, node="fit", outputs='{"study.loss":0.5}')) + session.add(RunNode(run_id=run_id, node="plot", status="error")) + session.commit() + url = f"{settings.API_V1_STR}/runs/{run_id}/nodes" + try: + kept = client.get(f"{url}/fit/outputs", headers=superuser_token_headers) + assert kept.json() == {"study.loss": 0.5} + + none = client.get(f"{url}/plot/outputs", headers=superuser_token_headers) + assert none.status_code == 404 + assert "kept no outputs" in none.json()["detail"] + + absent = client.get(f"{url}/nope/outputs", headers=superuser_token_headers) + assert absent.status_code == 404 + finally: + with Session(db_engine) as session: + for row in session.exec( + select(RunNode).where(col(RunNode.run_id) == run_id) + ).all(): + session.delete(row) + session.delete(session.get(Run, run_id)) + session.commit() def _metric(run_id: str, name: str, step: int, value: float, ts: float) -> RunMetric: @@ -1477,3 +1593,26 @@ def test_runs_page_by_when_they_were_created(client, superuser_token_headers, pa "page-2", "page-1", ] + + +@pytest.mark.usefixtures("paged") +def test_a_truncated_export_says_how_many_runs_it_left_out( + client, superuser_token_headers, monkeypatch +): + """The cap is in one header, the selection's size in another, and the SDK + turns the two into a warning naming what is missing.""" + from fluksio.sdk.client import Client + + monkeypatch.setattr("fluksio.api.routes.runs.EXPORT_CAP", 2) + answer = client.get( + f"{settings.API_V1_STR}/runs/export/runs", + params={"flow": "paged-study", "format": "jsonl"}, + headers=superuser_token_headers, + ) + assert answer.headers["X-Truncated"] == "2" + assert answer.headers["X-Total-Count"] == "3" + assert [row["id"] for row in _lines(answer)] == ["page-2", "page-1"] + + sdk = Client(http=client, token=superuser_token_headers["Authorization"][7:]) + with pytest.warns(UserWarning, match="1 older runs"): + assert len(sdk.export_runs(flow="paged-study")) == 2 diff --git a/backend/tests/api/routes/test_sync.py b/backend/tests/api/routes/test_sync.py index 79c7985..0dbb977 100644 --- a/backend/tests/api/routes/test_sync.py +++ b/backend/tests/api/routes/test_sync.py @@ -9,7 +9,7 @@ from fastapi.testclient import TestClient from fluksio.core.config import settings from fluksio.sdk import FLOWS, MARKER, SyncError -from fluksio.sdk.client import Client, sync +from fluksio.sdk.client import ApiError, Client, sync EXAMPLES = Path(__file__).parents[4] / "examples" PREFIX = settings.API_V1_STR @@ -47,6 +47,15 @@ def commits() -> int: return int(result.stdout.strip()) if result.returncode == 0 else 0 +@pytest.mark.parametrize("repo", ["/", str(Path.home()), "examples"]) +def test_sync_refuses_a_repo_every_run_would_walk_the_disk_for(api, flows, repo): + with pytest.raises(ApiError) as refused: + sync([flows["train"]], api, origin={**ORIGIN, "repo": repo}) + + assert refused.value.status == 422 + assert "origin.repo" in str(refused.value.detail) + + def test_sync_stores_a_runnable_flow_with_its_origin(api, flows): reports = sync([flows["train"]], api, origin=ORIGIN) diff --git a/backend/tests/api/routes/test_workers.py b/backend/tests/api/routes/test_workers.py index 2a5b6bb..884e8a8 100644 --- a/backend/tests/api/routes/test_workers.py +++ b/backend/tests/api/routes/test_workers.py @@ -38,6 +38,27 @@ def test_the_token_names_the_worker_and_its_labels( assert [(w["name"], w["labels"]) for w in attached] == [("gpu-box", ["gpu"])] +def test_the_list_names_each_version_and_who_is_behind( + client: TestClient, superuser_token_headers: dict[str, str] +) -> None: + issued = mint(client, superuser_token_headers) + url = f"{PREFIX}/attach?token={issued['token']}" + + def listed() -> list[tuple[object, ...]]: + answer = client.get(PREFIX, headers=superuser_token_headers).json() + return [(w["protocol"], w["version"], w["behind"]) for w in answer] + + with client.websocket_connect(url) as ws: + ws.send_json({**hello(), "protocol": PROTOCOL - 1, "version": "0.1.5"}) + assert ws.receive_json()["op"] == "refused" + assert listed() == [(PROTOCOL - 1, "0.1.5", True)] + + with client.websocket_connect(url) as ws: + ws.send_json({**hello(), "version": "0.2.0"}) + ws.receive_json() + assert listed() == [(PROTOCOL, "0.2.0", False)] + + def test_a_worker_cannot_claim_a_label_its_token_lacks( client: TestClient, superuser_token_headers: dict[str, str] ) -> None: diff --git a/backend/tests/crud/test_user.py b/backend/tests/crud/test_user.py index c292ebe..ba30679 100644 --- a/backend/tests/crud/test_user.py +++ b/backend/tests/crud/test_user.py @@ -56,6 +56,9 @@ def test_check_if_user_is_superuser(db: Session) -> None: user_in = UserCreate(email=email, password=password, is_superuser=True) user = crud.create_user(session=db, user_create=user_in) assert user.is_superuser is True + # A stray superuser makes adopting the owner ambiguous (tests/test_cloud.py). + db.delete(user) + db.commit() def test_check_if_user_is_superuser_normal_user(db: Session) -> None: @@ -75,6 +78,8 @@ def test_get_user(db: Session) -> None: assert user_2 assert user.email == user_2.email assert jsonable_encoder(user) == jsonable_encoder(user_2) + db.delete(user) + db.commit() def test_update_user(db: Session) -> None: @@ -91,6 +96,8 @@ def test_update_user(db: Session) -> None: assert user.email == user_2.email verified, _ = verify_password(new_password, user_2.hashed_password) assert verified + db.delete(user) + db.commit() def test_authenticate_user_with_bcrypt_upgrades_to_argon2(db: Session) -> None: diff --git a/backend/tests/flow/test_brain_graph.py b/backend/tests/flow/test_brain_graph.py index 72876d7..ec20a45 100644 --- a/backend/tests/flow/test_brain_graph.py +++ b/backend/tests/flow/test_brain_graph.py @@ -19,7 +19,7 @@ from fluksio.flow.store import FlowStore BROKER = {"broker_host": "mosquitto", "broker_port": 1883, "topic": "sensors/temp"} -def subscriber(node_id: str) -> NodeDef: +def listener(node_id: str) -> NodeDef: return NodeDef( id=node_id, type="mqtt", @@ -35,7 +35,7 @@ def controller(tmp_path: Path) -> FlowController: FlowDef( name="house", nodes=[ - subscriber("sensor"), + listener("sensor"), NodeDef( id="scale", type="change", @@ -45,7 +45,7 @@ def controller(tmp_path: Path) -> FlowController: ], ) ) - store.write_flow(FlowDef(name="shed", nodes=[subscriber("sensor")])) + store.write_flow(FlowDef(name="shed", nodes=[listener("sensor")])) return FlowController(store) diff --git a/backend/tests/flow/test_connector.py b/backend/tests/flow/test_connector.py index 4abde58..f530369 100644 --- a/backend/tests/flow/test_connector.py +++ b/backend/tests/flow/test_connector.py @@ -1,6 +1,7 @@ """The connector contract: polling, deduplication, health and discovery.""" import asyncio +from collections.abc import Callable from types import SimpleNamespace from typing import Any @@ -13,6 +14,7 @@ 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 +from fluksio.flow.supervision import Supervisor class Sensor(ConnectorNode): @@ -48,12 +50,28 @@ def a_sensor(readings: list[Any], **params: Any) -> 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.""" +def run_briefly( + node: ConnectorNode, + seconds: float = 0.12, + app: Any = None, + until: Callable[[], bool] | None = None, +) -> None: + """Start the poll loop, let it tick a few times, stop it. + + With ``until``, stop once that holds instead, giving up after 5 s: a fixed + pause is too short when the machine is busy. The caller's asserts then say + what never happened. + """ async def cycle() -> None: await node.start(app) - await asyncio.sleep(seconds) + if until is None: + await asyncio.sleep(seconds) + else: + for _ in range(500): + if until(): + break + await asyncio.sleep(0.01) await node.stop() asyncio.run(cycle()) @@ -97,7 +115,8 @@ def test_a_failing_poll_reports_down_and_keeps_going(): node._on_health = lambda _node, status, detail: health.append((status, detail)) Pipeline(nodes=[node]) - run_briefly(node) + # The first poll fails, so an "ok" after it is the recovery. + run_briefly(node, until=lambda: len(health) > 1 and health[-1][0] == "ok") assert ("down", "RuntimeError: device unplugged") in health # It recovered rather than giving up. @@ -138,6 +157,35 @@ def test_an_undeclared_port_keeps_failing_until_the_node_declares_it(): assert len([entry for entry in health if entry[0] == "down"]) > 1 +def test_a_push_session_that_fails_before_it_listens_reports_down(): + """A crashed loop is node health, not only a crash event.""" + + class Push(Sensor): + async def start(self, app: Any = None) -> None: + self._run_supervised("push", self._session) + + async def _session(self) -> None: + raise ConnectionError("bridge unreachable") + + health: list[tuple[str, str | None]] = [] + node = Push(provides=[MessageSpec(name="reading", dtype=DType.FLOAT)]) + node.assign_flow("demo", "push") + node._on_health = lambda _node, status, detail: health.append((status, detail)) + + async def scenario() -> None: + node.supervisor = Supervisor() + await node.start() + for _ in range(500): + if health: + break + await asyncio.sleep(0.01) + await node.supervisor.cancel_all() + + asyncio.run(scenario()) + + assert health == [("down", "ConnectionError: bridge unreachable")] + + class Actuator(ConnectorNode): """A connector that commands something instead of reading it.""" diff --git a/backend/tests/flow/test_dashboards.py b/backend/tests/flow/test_dashboards.py index 858b0da..f528dcc 100644 --- a/backend/tests/flow/test_dashboards.py +++ b/backend/tests/flow/test_dashboards.py @@ -415,6 +415,7 @@ def test_a_summary_carries_the_footprint_of_the_document(store: DashboardStore): (summary,) = store.list() assert summary.columns == saved.columns + assert summary.shape == "rect" assert [(f.type, f.x, f.y, f.w, f.h) for f in summary.footprint] == [ ("chart", 2, 1, 6, 4), ("gauge", 0, 5, 2, 2), @@ -422,6 +423,12 @@ def test_a_summary_carries_the_footprint_of_the_document(store: DashboardStore): ] +def test_a_summary_says_a_dashboard_is_round(store: DashboardStore): + store.write(DashboardDef(name="knob", shape="round")) + + assert store.list()[0].shape == "round" + + def test_a_summary_carries_when_the_working_copy_was_written(store: DashboardStore): published = store.write(default_dashboard("house")) document = store.root / "house" / "dashboard.json" diff --git a/backend/tests/flow/test_discretization.py b/backend/tests/flow/test_discretization.py index cae205c..6fe3f87 100644 --- a/backend/tests/flow/test_discretization.py +++ b/backend/tests/flow/test_discretization.py @@ -4,7 +4,7 @@ import time from fluksio.flow.executor import ExecutionService from fluksio.flow.messages import DType, MessageSpec -from fluksio.flow.nodes import Node +from fluksio.flow.nodes import DelayNode, Node from fluksio.flow.pipeline import Pipeline from fluksio.flow.queue import MemoryWorkQueue @@ -213,6 +213,33 @@ def test_a_node_whose_inputs_did_not_change_is_not_run(): assert seen == [1.0, 2.0] +def test_a_delay_interval_sends_the_latest_value_when_the_window_ends(): + """It used to drop a value inside the window rather than hold it.""" + seen: list[float] = [] + source = make_node("source", lambda params: None, provides=[spec("raw")]) + delay = DelayNode( + params={"interval": WINDOW}, + requires=[spec("raw")], + provides=[spec("out")], + name="delay", + ) + delay.assign_flow("demo", "delay") + watcher = make_node( + "watcher", lambda out, params: seen.append(out), requires=[spec("out")] + ) + _pipeline, queue, service = _running([source, delay, watcher]) + + for value in (1.0, 2.0, 3.0): + source.inject({"raw": value}) + for item in queue.claim(10, 10): + service._run_item(item) + assert seen == [1.0] + + time.sleep(WINDOW * 2) + assert _run_due(queue, service) == 1 + assert seen == [1.0, 3.0] + + def test_a_join_behind_a_skipped_branch_still_runs(): """Skipping a node frees its consumers, which may already have been passed.""" seen: list[tuple[float, float]] = [] diff --git a/backend/tests/flow/test_drafts.py b/backend/tests/flow/test_drafts.py index b3d085f..5f54da5 100644 --- a/backend/tests/flow/test_drafts.py +++ b/backend/tests/flow/test_drafts.py @@ -125,6 +125,29 @@ def test_a_published_node_with_no_body_is_reported(store: FlowStore): assert [i for i in controller.issues if i.code == "missing_source"] == [] +def test_a_device_on_a_node_without_code_is_reported(store: FlowStore): + """Only code travels to a worker, so the device would be ignored silently.""" + store.write_flow( + FlowDef( + name="heating", + nodes=[ + NodeDef( + id="relay", + type="change", + device="gpu", + provides=[MessageSpec(name="temp")], + ) + ], + ) + ) + controller = FlowController(store) + + asyncio.run(controller.reload()) + (issue,) = [i for i in controller.issues if i.code == "ignored_device"] + assert issue.node == "heating.relay" + assert issue.advisory + + def test_a_node_being_written_in_the_editor_is_not_reported(store: FlowStore): """A draft legitimately has no published body yet — that is what a draft is.""" store.write_draft(a_flow(), 0) diff --git a/backend/tests/flow/test_endpoints.py b/backend/tests/flow/test_endpoints.py index f448e81..dcf3bf6 100644 --- a/backend/tests/flow/test_endpoints.py +++ b/backend/tests/flow/test_endpoints.py @@ -10,8 +10,10 @@ from pathlib import Path import pytest from fluksio.api.routes.flows import _endpoints +from fluksio.core.config import settings from fluksio.flow.controller import FlowController from fluksio.flow.messages import DType, MessageSpec +from fluksio.flow.panels import PanelDef, PanelPort, PanelsConfig, write_config from fluksio.flow.schemas import FlowDef, NodeDef from fluksio.flow.store import FlowStore @@ -109,3 +111,35 @@ def test_the_wiring_index_is_read_once_per_store_revision(controller): controller.store.write_flow(FlowDef(name="shed", nodes=[])) _endpoints(controller, controller.store.read_flow("house")) assert reads == 2 + + +def test_a_panel_port_is_drawn_on_the_side_it_wires(controller, tmp_path, monkeypatch): + monkeypatch.setattr(settings, "PANELS_FILE", tmp_path / "panels.json") + write_config( + PanelsConfig( + panels=[ + PanelDef( + id="hall", + title="Hall", + io=[ + PanelPort(port="gpio.4", message="house.temp", direction="in"), + PanelPort( + port="backlight", message="house.temp", direction="out" + ), + PanelPort(port="mic", message="garage.open", direction="in"), + ], + ) + ] + ) + ) + + endpoints = { + one.id: one + for one in _endpoints(controller, controller.store.read_flow("house")) + } + + # The id is what the device publishes under, so its edges pulse for it. + assert endpoints["panel:hall:gpio.4"].provides == ["house.temp"] + assert endpoints["panel:hall:backlight"].requires == ["house.temp"] + assert endpoints["panel:hall:backlight"].detail == "Hall" + assert "panel:hall:mic" not in endpoints diff --git a/backend/tests/flow/test_house_vocabulary.py b/backend/tests/flow/test_house_vocabulary.py index 10db4b7..aff89a6 100644 --- a/backend/tests/flow/test_house_vocabulary.py +++ b/backend/tests/flow/test_house_vocabulary.py @@ -8,8 +8,8 @@ come up than to go down. from fluksio.flow.messages import DType, MessageSpec from fluksio.flow.nodes import MqttNode +from fluksio.flow.nodes.debounce import DebounceNode from fluksio.flow.nodes.mqtt import topic_matches -from fluksio.flow.nodes.trigger import TriggerNode # ── MQTT topic filters ──────────────────────────────────────────────────── @@ -29,7 +29,7 @@ def test_a_filter_does_not_cover_a_neighbouring_topic(): assert not topic_matches("sensors/living/temp", "sensors/living") -def _subscriber(topic, **params) -> MqttNode: +def _listener(topic, **params) -> MqttNode: node = MqttNode( provides=[MessageSpec(name="reading", port="reading", dtype=DType.FLOAT)], params={"topic": {"reading": topic}, **params}, @@ -40,7 +40,7 @@ def _subscriber(topic, **params) -> MqttNode: def test_a_wildcard_subscription_routes_to_its_port(): """It used to subscribe and then drop every message it was sent.""" - node = _subscriber("shellypm/status/#") + node = _listener("shellypm/status/#") assert node._ports_for("shellypm/status/pm1:0") == ["reading"] assert node._ports_for("shellypmc/status/pm1:0") == [] @@ -63,16 +63,16 @@ def test_an_exact_topic_still_wins_over_a_filter(): def test_json_key_lifts_the_value_a_device_wraps(): """Victron publishes {"value": 47} on every one of its topics.""" - node = _subscriber("N/x/battery/Soc", json_key="value") + node = _listener("N/x/battery/Soc", json_key="value") assert node.json_keys == {"reading": "value"} def test_without_json_key_the_port_name_is_the_key_as_before(): - node = _subscriber("sensors/temp") + node = _listener("sensors/temp") assert node.json_keys == {} -# ── trigger: passthrough and a per-message wait ─────────────────────────── +# ── debounce: passthrough and a per-message wait ────────────────────────── class _Pipeline: @@ -87,8 +87,8 @@ class _Pipeline: return True -def _trigger(**params) -> tuple[TriggerNode, _Pipeline]: - node = TriggerNode( +def _debounce(**params) -> tuple[DebounceNode, _Pipeline]: + node = DebounceNode( requires=[ MessageSpec(name="run", port="run", dtype=DType.STR), MessageSpec(name="run_for", port="run_for", dtype=DType.FLOAT), @@ -103,30 +103,30 @@ def _trigger(**params) -> tuple[TriggerNode, _Pipeline]: def test_the_incoming_value_passes_through_and_the_wait_comes_from_a_port(): - node, pipeline = _trigger() + node, pipeline = _debounce() - assert node._trigger({}, run="DOWN", run_for=28.0) == {"cmd": "DOWN"} + assert node._debounce({}, run="DOWN", run_for=28.0) == {"cmd": "DOWN"} (outputs, seconds, _guard) = pipeline.deferred[-1] assert outputs == {"shutters.cmd": "STOP"} and seconds == 28.0 - node._trigger({}, run="UP", run_for=26.0) + node._debounce({}, run="UP", run_for=26.0) assert pipeline.deferred[-1][1] == 26.0 def test_a_second_command_invalidates_the_stop_the_first_one_scheduled(): - node, pipeline = _trigger() - node._trigger({}, run="DOWN", run_for=28.0) + node, pipeline = _debounce() + node._debounce({}, run="DOWN", run_for=28.0) first = pipeline.deferred[-1][2] - node._trigger({}, run="UP", run_for=26.0) + node._debounce({}, run="UP", run_for=26.0) assert pipeline.deferred[-1][2] != first def test_a_wait_of_zero_sends_nothing_afterwards(): """STOP is commanded once. Nothing follows it, forever.""" - node, pipeline = _trigger() - node._trigger({}, run="DOWN", run_for=28.0) + node, pipeline = _debounce() + node._debounce({}, run="DOWN", run_for=28.0) scheduled = len(pipeline.deferred) - assert node._trigger({}, run="STOP", run_for=0.0) == {"cmd": "STOP"} + assert node._debounce({}, run="STOP", run_for=0.0) == {"cmd": "STOP"} assert len(pipeline.deferred) == scheduled assert node.recall("armed", 0) == 0 diff --git a/backend/tests/flow/test_library.py b/backend/tests/flow/test_library.py index 03eb208..501fff1 100644 --- a/backend/tests/flow/test_library.py +++ b/backend/tests/flow/test_library.py @@ -1,9 +1,11 @@ """Nodes shared across flows: one source, many instances.""" +import asyncio from pathlib import Path import pytest +from fluksio.flow.controller import FlowController from fluksio.flow.messages import MessageSpec from fluksio.flow.schemas import FlowDef, NodeDef from fluksio.flow.store import FlowStore, LibExists, LibNotFound @@ -67,6 +69,30 @@ def test_a_second_flow_can_use_the_same_source(store: FlowStore): assert store.usages("read_temp") == ["cooling.sensor", "heating.sensor"] +def test_a_shared_source_change_rebuilds_only_what_is_published(store: FlowStore): + store.write_flow(a_flow()) + store.write_node_source("heating", "sensor", SOURCE) + store.share_node("heating", "sensor", "read_temp") + # Published with the shared node, which the draft has since dropped. + store.publish_flow("heating", store.read_flow("heating", draft=True).version) + store.unshare_node("heating", "sensor") + # Shared in a draft that was never published. + cooling = a_flow("cooling") + cooling.nodes[0].source_ref = "read_temp" + store.write_draft(cooling, 0) + + controller = FlowController(store) + rebuilt: list[str] = [] + + async def reload_flow(name: str) -> None: + rebuilt.append(name) + + controller.reload_flow = reload_flow # type: ignore[method-assign] + asyncio.run(controller.reload_lib_users("read_temp")) + + assert rebuilt == ["heating"] + + def test_a_shared_name_is_not_taken_twice(store: FlowStore): store.write_flow(a_flow()) store.write_node_source("heating", "sensor", SOURCE) @@ -94,3 +120,27 @@ def test_unsharing_takes_a_private_copy_back(store: FlowStore): def test_a_missing_shared_source_is_reported(store: FlowStore): with pytest.raises(LibNotFound): store.read_lib_source("nothing_here") + + +def test_retiring_a_shared_source_leaves_each_user_a_copy(store: FlowStore): + store.write_flow(a_flow()) + store.write_node_source("heating", "sensor", SOURCE) + store.share_node("heating", "sensor", "read_temp") + store.publish_flow("heating", store.read_flow("heating", draft=True).version) + version = store.read_flow("heating").version + # Shared in a draft that was never published. + cooling = a_flow("cooling") + cooling.nodes[0].source_ref = "read_temp" + store.write_draft(cooling, 0) + + store.detach_lib("read_temp") + + assert store.list_lib() == [] + heating = store.read_flow("heating") + assert heating.nodes[0].source_ref is None + assert heating.version == version + 1 + assert store.read_node_source("heating", "sensor") == SOURCE + # What runs is the same code, so there is nothing to publish. + assert not store.has_draft("heating") + assert store.read_flow("cooling", draft=True).nodes[0].source_ref is None + assert store.read_node_source("cooling", "sensor", draft=True) == SOURCE diff --git a/backend/tests/flow/test_messages.py b/backend/tests/flow/test_messages.py index 8081dd7..2e11862 100644 --- a/backend/tests/flow/test_messages.py +++ b/backend/tests/flow/test_messages.py @@ -2,7 +2,13 @@ import json import pytest -from fluksio.flow.messages import DType, MessageSpec, flow_of, qualify +from fluksio.flow.messages import ( + DType, + MessageSpec, + flow_of, + qualify, + validate_qualified, +) def test_port_defaults_to_last_name_segment(): @@ -201,3 +207,14 @@ def test_a_value_that_refers_to_itself_does_not_hang_the_check(): loop: dict = {} loop["self"] = loop spec.check(loop) + + +@pytest.mark.parametrize("name", ["home.doorbell", "house.kitchen.temperature"]) +def test_a_qualified_name_names_its_flow(name): + assert validate_qualified(name) == name + + +@pytest.mark.parametrize("name", ["doorbell", "Home.x", "home.", "home.a-b", "home..x"]) +def test_anything_else_is_refused_with_the_rule(name): + with pytest.raises(ValueError, match="flow.name"): + validate_qualified(name) diff --git a/backend/tests/flow/test_modules.py b/backend/tests/flow/test_modules.py index 1d3d991..c2401cd 100644 --- a/backend/tests/flow/test_modules.py +++ b/backend/tests/flow/test_modules.py @@ -2,6 +2,7 @@ import shutil import subprocess +import zipfile from pathlib import Path import pytest @@ -77,6 +78,40 @@ def test_a_manifest_that_does_not_resolve_leaves_the_venv_alone(venv: Path): assert (venv / ".applied").read_text() == modules._digest("") +def _wheel(directory: Path, name: str, requires: str = "") -> None: + """A wheel with nothing in it but a name and, optionally, one dependency.""" + info = f"{name}-1.0.dist-info" + metadata = f"Metadata-Version: 2.1\nName: {name}\nVersion: 1.0\n" + if requires: + metadata += f"Requires-Dist: {requires}\n" + with zipfile.ZipFile(directory / f"{name}-1.0-py3-none-any.whl", "w") as wheel: + wheel.writestr(f"{name}/__init__.py", "") + wheel.writestr(f"{info}/METADATA", metadata) + wheel.writestr( + f"{info}/WHEEL", + "Wheel-Version: 1.0\nRoot-Is-Purelib: true\nTag: py3-none-any\n", + ) + wheel.writestr(f"{info}/RECORD", "") + + +def test_a_listed_package_brings_its_dependencies( + venv: Path, tmp_path: Path, monkeypatch: pytest.MonkeyPatch +): + wheels = tmp_path / "wheels" + wheels.mkdir() + _wheel(wheels, "fluksio_top", requires="fluksio_dep") + _wheel(wheels, "fluksio_dep") + monkeypatch.setenv("UV_OFFLINE", "1") + + ok, output = modules.sync(f"--find-links {wheels}\nfluksio-top\n") + + assert ok, output + imported = subprocess.run( + [modules.venv_python(), "-c", "import fluksio_dep"], check=False + ) + assert imported.returncode == 0 + + # --------------------------------------------------------------------------- # Adopting the venv Fluksio was installed into # diff --git a/backend/tests/flow/test_node_settings.py b/backend/tests/flow/test_node_settings.py index 6fabb30..96a2650 100644 --- a/backend/tests/flow/test_node_settings.py +++ b/backend/tests/flow/test_node_settings.py @@ -9,7 +9,7 @@ from pathlib import Path import pytest -from fluksio.flow.controller import FlowController +from fluksio.flow.controller import FlowController, follow_header from fluksio.flow.messages import DType, MessageSpec from fluksio.flow.pipeline import Pipeline from fluksio.flow.schemas import FlowDef, NodeDef @@ -148,3 +148,66 @@ def test_a_shared_body_is_checked_against_each_user(store: FlowStore): assert [n.status for n in nodes] == ["error"] assert "'level' is an input of 'scale'" in (nodes[0].error or "") + + +def _scaffold_header(node: NodeDef) -> str: + """The header the canvas writes: input ports in order, then settings.""" + names = [spec.port for spec in node.requires] + [ + key for key in node.params if key != "synchronous" + ] + return f"def process({', '.join(names)}):\n return {{}}\n" + + +def test_a_scaffold_header_reads_back_as_the_node_it_came_from(): + node = _flow( + [*READING, MessageSpec(name="garden.level", port="depth")], + factor=3, + synchronous=True, + ).nodes[0] + + assert follow_header(node, _scaffold_header(node)) == node + + +def test_a_header_declares_ports_and_settings_that_read_back_the_same(): + header = "def process(reading, level, gain=1.5, mode='eco'):\n return {}\n" + + node = follow_header(_flow([]).nodes[0], header) + + assert [spec.port for spec in node.requires] == ["reading", "level"] + assert node.params == {"gain": 1.5, "mode": "eco"} + assert _scaffold_header(node).split(":")[0] == ( + "def process(reading, level, gain, mode)" + ) + + +def test_a_renamed_parameter_keeps_the_message_and_a_dropped_one_goes(): + node = _flow(READING, factor=3, offset=1).nodes[0] + + renamed = follow_header(node, "def process(value, factor, offset): pass\n") + dropped = follow_header(node, "def process(reading, factor): pass\n") + + assert renamed.requires == [READING[0].model_copy(update={"port": "value"})] + assert renamed.params == {"factor": 3, "offset": 1} + assert dropped.requires == READING + assert dropped.params == {"factor": 3} + + +def test_a_new_parameter_after_a_setting_is_a_setting(): + node = _flow(READING, factor=3).nodes[0] + + followed = follow_header(node, "def process(reading, factor, offset): pass\n") + + assert followed.requires == READING + assert followed.params == {"factor": 3, "offset": ""} + + +def test_a_header_it_cannot_read_for_certain_leaves_the_node(): + node = _flow(READING, factor=3).nodes[0] + + for code in ( + "def process(reading, factor", + "def process(**kw): pass\n", + "@cache\ndef process(value): pass\n", + "def run(value): pass\n", + ): + assert follow_header(node, code) == node diff --git a/backend/tests/flow/test_node_types.py b/backend/tests/flow/test_node_types.py index 00b9d7b..face945 100644 --- a/backend/tests/flow/test_node_types.py +++ b/backend/tests/flow/test_node_types.py @@ -75,7 +75,7 @@ FIXTURES: dict[str, dict] = { ], "provides": [MessageSpec(name="reading", dtype=DType.JSON)], }, - "trigger": { + "debounce": { "params": {"first": True, "then": False, "wait": 120}, "requires": [MessageSpec(name="motion", dtype=DType.BOOL)], "provides": [MessageSpec(name="light", dtype=DType.BOOL)], diff --git a/backend/tests/flow/test_placement.py b/backend/tests/flow/test_placement.py index 73dabf5..27705ed 100644 --- a/backend/tests/flow/test_placement.py +++ b/backend/tests/flow/test_placement.py @@ -193,6 +193,26 @@ def test_a_node_waits_for_a_busy_machine_and_says_so(loop): assert placer.snapshot()["waiting"] == [] +def test_a_reconnecting_client_is_told_what_is_waiting(tmp_path): + """The wait is announced once, so the socket's snapshot carries it again. + + A panel is sent none of it, the same as node status. + """ + from fluksio.api.routes.flows import snapshot_payload + from fluksio.flow.controller import FlowController + from fluksio.flow.store import FlowStore + + placer = Placer(local=ResourceAccountant(cpus=2)) + placer._announce("study.b", "r1", time.monotonic(), "waiting for 2 cpu(s)") + controller = FlowController(FlowStore(tmp_path / "flows"), placer=placer) + + queued = snapshot_payload(controller)["queued"] + assert [(e["node"], e["reason"]) for e in queued] == [ + ("study.b", "waiting for 2 cpu(s)") + ] + assert snapshot_payload(controller, set())["queued"] == [] + + def test_a_worker_attaching_wakes_what_was_waiting_for_one(loop): """A node waiting inside one machine's books could not hear this.""" hub = RemoteWorkerHub() diff --git a/backend/tests/flow/test_queue.py b/backend/tests/flow/test_queue.py index 71cb25e..6922a01 100644 --- a/backend/tests/flow/test_queue.py +++ b/backend/tests/flow/test_queue.py @@ -25,6 +25,18 @@ def test_items_come_back_in_the_order_they_went_in(): assert all(item.entry_id for item in claimed) +def test_two_queues_never_hand_out_the_same_id(): + """A restart without Redis must not reuse the last boot's run ids.""" + first, second = MemoryWorkQueue(), MemoryWorkQueue() + for queue in (first, second): + queue.add(WorkItem(kind="cascade", node="f.n", flow="f")) + + ids = [queue.claim(1, 10)[0].entry_id for queue in (first, second)] + + assert ids[0] != ids[1] + assert all(len(entry_id) <= 64 for entry_id in ids) + + def test_claiming_an_empty_queue_waits_and_gives_up(): queue = MemoryWorkQueue() started = time.monotonic() diff --git a/backend/tests/flow/test_remote.py b/backend/tests/flow/test_remote.py index 8edeb4b..e872e5f 100644 --- a/backend/tests/flow/test_remote.py +++ b/backend/tests/flow/test_remote.py @@ -8,8 +8,10 @@ what happened rather than a wait that never ends. """ import asyncio +import gc import threading import time +import warnings from collections.abc import Iterator import pytest @@ -21,7 +23,12 @@ from fluksio.flow.remote import ( RemoteWorkerHub, WorkerInventory, ) -from fluksio.flow.workers import NodeTimeout, RemoteError +from fluksio.flow.workers import ( + MAX_REMOTE_NAME, + NodeTimeout, + RemoteError, + _remote_class, +) @pytest.fixture @@ -149,6 +156,13 @@ def test_a_failure_keeps_its_class_across_the_socket(loop): assert str(caught[0]) == "bad input" +def test_a_class_name_off_the_wire_is_bounded_and_identifier_safe(): + cls = _remote_class("Bad Name\n" + "x" * 10_000) + assert cls.__name__.isidentifier() + assert len(cls.__name__) == MAX_REMOTE_NAME + assert issubclass(cls, RemoteError) + + def test_a_worker_that_goes_away_fails_the_call_rather_than_hanging(loop): hub = RemoteWorkerHub() worker, socket = attach(hub, loop) @@ -229,6 +243,21 @@ def test_cancelling_a_run_reaches_only_that_run(loop): assert [frame["call_id"] for frame in cancels] == ["run-a:flow.node"] +def test_a_cancel_after_the_loop_closed_leaves_no_coroutine_behind(): + closed = asyncio.new_event_loop() + closed.close() + worker = RemoteWorker( + name="gpu1", labels=[], send=FakeSocket().send_json, loop=closed + ) + + with warnings.catch_warnings(record=True) as caught: + warnings.simplefilter("always") + worker.cancel("run-a:flow.node") + gc.collect() + + assert not [w for w in caught if "never awaited" in str(w.message)] + + # ----------------------------------------------------------------------------- # Liveness, and what a heartbeat is evidence of # diff --git a/backend/tests/flow/test_resources.py b/backend/tests/flow/test_resources.py index f4ee54e..ffcc2fa 100644 --- a/backend/tests/flow/test_resources.py +++ b/backend/tests/flow/test_resources.py @@ -20,6 +20,7 @@ from fluksio.flow.resources import ( fair_share_env, machine_gpus, machine_ram_mb, + warn_oversubscribed, ) from fluksio.flow.schemas import NodeDef, Resources @@ -32,6 +33,7 @@ def test_the_cards_are_counted_from_the_devices_the_driver_makes(monkeypatch): """ from fluksio.flow import resources + monkeypatch.delenv("CUDA_VISIBLE_DEVICES", raising=False) monkeypatch.setattr( resources.glob, "glob", @@ -45,6 +47,16 @@ def test_the_cards_are_counted_from_the_devices_the_driver_makes(monkeypatch): assert machine_gpus() == 0 +def test_a_limit_far_past_the_cores_is_warned_about(monkeypatch, caplog): + """Said once, never clamped: nodes waiting on devices can want a high one.""" + monkeypatch.setattr(os, "cpu_count", lambda: 2) + warn_oversubscribed({"FLOW_MAX_WORKERS": 8, "FLOW_MAX_CASCADES": 9}) + + assert [r.getMessage() for r in caplog.records] == [ + "Concurrency above 4x this machine's 2 cores: FLOW_MAX_CASCADES=9" + ] + + def test_memory_is_the_containers_limit_where_that_is_lower(monkeypatch, tmp_path): """The host's total is memory a container would be killed for using.""" from fluksio.flow import resources @@ -139,12 +151,37 @@ def test_the_share_becomes_the_thread_limit(): assert "CUDA_VISIBLE_DEVICES" not in env -def test_a_gpu_node_is_told_which_card_is_its(): +def test_a_gpu_node_is_told_which_card_is_its(monkeypatch): + monkeypatch.delenv("CUDA_VISIBLE_DEVICES", raising=False) env = derive_env(Resources(cpus=1, gpus=2), Allocation(cpus=1, gpus=(1, 3))) assert env["CUDA_VISIBLE_DEVICES"] == "1,3" +@pytest.mark.parametrize( + ("mask", "cards", "named"), + [(None, 2, "0"), ("2,3", 2, "2"), ("GPU-a1b2, MIG-c3d4", 2, "GPU-a1b2")], +) +def test_the_engines_own_mask_is_the_cards_it_has(monkeypatch, mask, cards, named): + """Under ``CUDA_VISIBLE_DEVICES=2,3`` card 0 is device 2, not device 0. + + A child's mask replaces the engine's, so an unmapped 0 would be a card + the engine was never given. A remote worker maps against its own mask. + """ + from fluksio.flow import resources + + monkeypatch.setattr(resources.glob, "glob", lambda pattern: ["/dev/nvidia0"] * 2) + if mask is None: + monkeypatch.delenv("CUDA_VISIBLE_DEVICES", raising=False) + else: + monkeypatch.setenv("CUDA_VISIBLE_DEVICES", mask) + wanted, allocation = Resources(cpus=1, gpus=1), Allocation(cpus=1, gpus=(0,)) + + assert machine_gpus() == cards + assert derive_env(wanted, allocation)["CUDA_VISIBLE_DEVICES"] == named + assert derive_env(wanted, allocation, local=False)["CUDA_VISIBLE_DEVICES"] == "0" + + def test_what_the_node_asked_for_wins(): """A declaration outranks what the allocation would imply — it is deliberate.""" wanted = Resources( diff --git a/backend/tests/flow/test_senders.py b/backend/tests/flow/test_senders.py index 655066a..e59cd6c 100644 --- a/backend/tests/flow/test_senders.py +++ b/backend/tests/flow/test_senders.py @@ -2,8 +2,10 @@ import asyncio +import httpx + from fluksio.flow.messages import DType, MessageSpec -from fluksio.flow.nodes import MqttNode +from fluksio.flow.nodes import HttpNode, MqttNode from fluksio.flow.nodes.http import close_shared_client, shared_client @@ -20,7 +22,29 @@ def test_http_senders_share_one_pooled_client(): close_shared_client() -def _publisher() -> MqttNode: +def test_an_http_reply_keeps_only_the_declared_ports(monkeypatch): + """A `status` beside the value is the endpoint's business, not an error.""" + reply = {"temp": 21.5, "status": "ok"} + client = httpx.Client( + transport=httpx.MockTransport(lambda _: httpx.Response(200, json=reply)) + ) + monkeypatch.setattr("fluksio.flow.nodes.http.shared_client", lambda: client) + + def sender(*provides: MessageSpec) -> HttpNode: + node = HttpNode( + requires=[MessageSpec(name="setpoint", dtype=DType.FLOAT)], + provides=provides, + params={"url": "http://thermostat.test/set"}, + ) + node.assign_flow("heating", "thermostat") + return node + + temp = MessageSpec(name="temp", dtype=DType.FLOAT) + assert sender(temp).execute({"heating.setpoint": 21.0}) == {"heating.temp": 21.5} + assert sender().execute({"heating.setpoint": 21.0}) is None + + +def _sender() -> MqttNode: node = MqttNode( requires=[MessageSpec(name="setpoint", port="setpoint", dtype=DType.FLOAT)], params={"topic": {"setpoint": "heating/setpoint"}}, @@ -29,27 +53,27 @@ def _publisher() -> MqttNode: return node -def test_a_started_publisher_queues_instead_of_connecting(): +def test_a_started_sender_queues_instead_of_connecting(): """The handler runs on a worker thread; it must not block on the broker.""" - node = _publisher() + node = _sender() async def scenario() -> None: - node._publish_queue = asyncio.Queue(maxsize=4) + node._send_queue = asyncio.Queue(maxsize=4) node._loop = asyncio.get_running_loop() - await asyncio.to_thread(node._publisher_handler, {}, setpoint=21.0) + await asyncio.to_thread(node._sender_handler, {}, setpoint=21.0) # call_soon_threadsafe lands on the next loop pass. await asyncio.sleep(0) - assert node._publish_queue.qsize() == 1 - assert node._publish_queue.get_nowait() == {"setpoint": 21.0} + assert node._send_queue.qsize() == 1 + assert node._send_queue.get_nowait() == {"setpoint": 21.0} asyncio.run(scenario()) -def test_a_full_publish_queue_drops_the_oldest(): +def test_a_full_send_queue_drops_the_oldest(): """A broker that cannot keep up must not grow the queue without bound.""" - node = _publisher() + node = _sender() health: list[tuple[str, str | None]] = [] node._on_health = lambda _n, status, detail: health.append((status, detail)) @@ -61,7 +85,7 @@ def test_a_full_publish_queue_drops_the_oldest(): assert queue.qsize() == 2 assert queue.get_nowait() == {"setpoint": 2.0} assert queue.get_nowait() == {"setpoint": 3.0} - assert health == [("degraded", "publish queue full")] + assert health == [("degraded", "send queue full")] asyncio.run(scenario()) @@ -121,7 +145,7 @@ def test_the_configured_timeout_reaches_the_broker_client(monkeypatch): assert seen["timeout"] == 2.5 -def test_the_configured_backlog_reaches_the_publish_queue(monkeypatch): +def test_the_configured_backlog_reaches_the_send_queue(monkeypatch): """The depth is read when the queue is built, so it has to be per node.""" import aiomqtt @@ -139,14 +163,14 @@ def test_the_configured_backlog_reaches_the_publish_queue(monkeypatch): node = MqttNode( requires=[MessageSpec(name="setpoint", port="setpoint", dtype=DType.FLOAT)], - params={"topic": {"setpoint": "heating/setpoint"}, "publish_queue_size": 8}, + params={"topic": {"setpoint": "heating/setpoint"}, "send_queue_size": 8}, ) node.assign_flow("heating", "out") async def scenario() -> int: await node.start_publisher() - assert node._publish_queue is not None - size = node._publish_queue.maxsize + assert node._send_queue is not None + size = node._send_queue.maxsize await node.stop_publisher() return size diff --git a/backend/tests/flow/test_store.py b/backend/tests/flow/test_store.py index 0734b03..a5e37d3 100644 --- a/backend/tests/flow/test_store.py +++ b/backend/tests/flow/test_store.py @@ -131,3 +131,45 @@ def test_renaming_onto_an_existing_name_is_refused(store: FlowStore): store.rename_flow("heating", "warmth") assert store.list_flows() == ["heating", "warmth"] + + +def test_opening_the_store_renames_trigger_nodes_to_debounce(store: FlowStore): + """The old name has no alias, so what is stored under it is rewritten once.""" + store.write_flow( + FlowDef( + name="hall", + nodes=[NodeDef(id="hold", type="trigger"), NodeDef(id="lamp")], + ) + ) + store.write_draft( + FlowDef(name="hall", nodes=[NodeDef(id="hold", type="trigger")]), + base_version=1, + ) + commits = commit_count(store) + + reopened = FlowStore(store.root) + + published = reopened.read_flow("hall") + draft = reopened.read_flow("hall", draft=True) + assert [node.type for node in published.nodes] == ["debounce", "python"] + assert [node.type for node in draft.nodes] == ["debounce"] + # Bumped, so an editor holding the old draft cannot save `trigger` back. + assert (published.version, draft.version) == (2, 3) + assert commit_count(store) == commits + 1 + + FlowStore(store.root) + assert commit_count(store) == commits + 1 + + +def test_opening_the_store_renames_the_mqtt_publish_queue_size(store: FlowStore): + """The mqtt setting is now `send_queue_size`, with no alias for the old name.""" + relay = NodeDef(id="relay", type="mqtt", params={"publish_queue_size": 8}) + store.write_flow(FlowDef(name="hall", nodes=[relay])) + commits = commit_count(store) + + reopened = FlowStore(store.root) + + published = reopened.read_flow("hall") + assert published.nodes[0].params == {"send_queue_size": 8} + assert published.version == 2 + assert commit_count(store) == commits + 1 diff --git a/backend/tests/flow/test_supervision.py b/backend/tests/flow/test_supervision.py index a5a6746..94d214f 100644 --- a/backend/tests/flow/test_supervision.py +++ b/backend/tests/flow/test_supervision.py @@ -234,3 +234,11 @@ def test_cancelling_one_flow_lifts_only_its_quarantine(): await supervisor.cancel_all() asyncio.run(scenario()) + + +def test_the_restart_steps_are_read_from_the_environment(monkeypatch): + """A list in an environment variable, so it arrives as JSON.""" + from fluksio.core.config import Settings + + monkeypatch.setenv("FLOW_RESTART_BACKOFF", "[2, 10]") + assert Settings().FLOW_RESTART_BACKOFF == (2.0, 10.0) diff --git a/backend/tests/flow/test_wren.py b/backend/tests/flow/test_wren.py index 2f3fff3..44c048b 100644 --- a/backend/tests/flow/test_wren.py +++ b/backend/tests/flow/test_wren.py @@ -143,3 +143,25 @@ def test_nothing_is_written_unless_applied_and_sure(fake_wren, params, confidenc } ] } + + +def test_the_model_runs_on_one_blas_thread(fake_wren, monkeypatch): + """About twice as fast for a model this small; numpy is loaded by `mlp`.""" + from threadpoolctl import threadpool_info + + threads: list[int] = [] + act = fake_wren.act + + def counted(command: str, house: Any) -> tuple[list[Any], float]: + threads.extend( + pool["num_threads"] + for pool in threadpool_info() + if pool["user_api"] == "blas" + ) + return act(command, house) + + monkeypatch.setattr(fake_wren, "act", counted) + _node(("kitchen_lamp", DType.BOOL)).execute({"house.command": "lights on"}) + + assert threads + assert set(threads) == {1} diff --git a/backend/tests/mcp/test_mcp_http.py b/backend/tests/mcp/test_mcp_http.py index 6d04e49..7c9d92e 100644 --- a/backend/tests/mcp/test_mcp_http.py +++ b/backend/tests/mcp/test_mcp_http.py @@ -97,6 +97,55 @@ def test_the_resource_metadata_names_the_authorization_server(over_mcp) -> None: assert settings.oauth_issuer in listed +@pytest.mark.usefixtures("enrolled") +def test_through_the_hub_an_agent_is_shown_the_hubs_addresses( + over_mcp, + client: TestClient, + superuser_token_headers: dict[str, str], + agent: uuid.UUID, +) -> None: + """What the connector sends: its marker, and the hub as the Host.""" + from fluksio.cloud import config as cloud_config + from tests.utils.portal import INSTANCE_ID, ISSUER + + via = {cloud_config.VIA_HEADER: cloud_config.VIA_PORTAL, "Host": "hub.example.test"} + base = f"{ISSUER}/i/{INSTANCE_ID}" + me = client.get( + f"{settings.API_V1_STR}/users/me", headers=superuser_token_headers + ).json() + token = mcp_token(uuid.UUID(me["id"]), agent) + + async def block(http: httpx.AsyncClient) -> list[httpx.Response]: + anonymous = rpc(None, "tools/list") + signed = rpc(token, "tools/list") + return [ + await http.post( + "/mcp", json=anonymous["json"], headers={**anonymous["headers"], **via} + ), + await http.get("/.well-known/oauth-protected-resource/mcp", headers=via), + await http.post( + "/mcp", json=signed["json"], headers={**signed["headers"], **via} + ), + # Any other name is still refused. + await http.post( + "/mcp", + json=signed["json"], + headers={**signed["headers"], "Host": "elsewhere.test"}, + ), + ] + + unauthenticated, document, listed, elsewhere = over_mcp(block) + assert unauthenticated.status_code == 401 + assert ( + f'resource_metadata="{ISSUER}/.well-known/oauth-protected-resource/i/{INSTANCE_ID}/mcp"' + in unauthenticated.headers["www-authenticate"] + ) + assert document.json()["resource"] == f"{base}/mcp" + assert document.json()["authorization_servers"] == [base] + assert listed.status_code == 200 + assert elsewhere.status_code == 421 + + def test_a_browser_token_is_not_an_agent_token( over_mcp, superuser_token_headers: dict[str, str] ) -> None: diff --git a/backend/tests/test_ai.py b/backend/tests/test_ai.py index 430df41..a13f63e 100644 --- a/backend/tests/test_ai.py +++ b/backend/tests/test_ai.py @@ -1,14 +1,14 @@ """The assistant: who may set it up, and that it acts as the person chatting.""" import json -from collections.abc import Iterator +from collections.abc import AsyncIterator, Iterator from pathlib import Path from typing import Any import pytest from fastapi.testclient import TestClient -from fluksio.ai import agent +from fluksio.ai import agent, client from fluksio.core.config import settings @@ -94,12 +94,16 @@ def test_a_turn_runs_the_tools_as_the_person_chatting( ] ) - async def fake_completion(**kwargs: Any) -> dict[str, Any]: + async def fake_completion(**kwargs: Any) -> AsyncIterator[str | dict[str, Any]]: seen.append(kwargs["messages"]) assert any(t["function"]["name"] == "save_flow" for t in kwargs["tools"]) - return {"choices": [{"message": next(answers)}]} + answer = next(answers) + if answer["content"]: + yield "Counted " + yield "them." + yield answer - monkeypatch.setattr(agent.client, "chat_completion", fake_completion) + monkeypatch.setattr(agent.client, "stream_completion", fake_completion) with client.stream( "POST", @@ -114,6 +118,8 @@ def test_a_turn_runs_the_tools_as_the_person_chatting( "start", "tool_call", "tool_result", + "delta", + "delta", "message", "done", ] @@ -136,3 +142,111 @@ def test_a_turn_runs_the_tools_as_the_person_chatting( } client.delete("/api/v1/ai/chat", headers=superuser_token_headers) assert client.get("/api/v1/ai/chat", headers=superuser_token_headers).json() == [] + + +# One answer per provider as it streams it. Ollama's text is recorded from a +# local server, the rest follows the shape each endpoint documents. +STREAMS = { + "ollama": """ +data: {"id":"chatcmpl-888","object":"chat.completion.chunk","model":"gemma3:4b-it-qat","system_fingerprint":"fp_ollama","choices":[{"index":0,"delta":{"role":"assistant","content":"Hello"},"finish_reason":null}]} + +data: {"id":"chatcmpl-888","object":"chat.completion.chunk","model":"gemma3:4b-it-qat","system_fingerprint":"fp_ollama","choices":[{"index":0,"delta":{"role":"assistant","content":" there"},"finish_reason":null}]} + +data: {"id":"chatcmpl-888","object":"chat.completion.chunk","model":"gemma3:4b-it-qat","system_fingerprint":"fp_ollama","choices":[{"index":0,"delta":{"role":"assistant","content":""},"finish_reason":"stop"}]} + +data: [DONE] +""", + "ollama-tools": """ +data: {"id":"chatcmpl-12","object":"chat.completion.chunk","model":"qwen3","system_fingerprint":"fp_ollama","choices":[{"index":0,"delta":{"role":"assistant","content":"","reasoning":"The user wants"},"finish_reason":null}]} + +data: {"id":"chatcmpl-12","object":"chat.completion.chunk","model":"qwen3","system_fingerprint":"fp_ollama","choices":[{"index":0,"delta":{"role":"assistant","content":"","tool_calls":[{"id":"call_k2","index":0,"type":"function","function":{"name":"get_flow","arguments":"{\\"name\\":\\"demo\\"}"}}]},"finish_reason":null}]} + +data: {"id":"chatcmpl-12","object":"chat.completion.chunk","model":"qwen3","system_fingerprint":"fp_ollama","choices":[{"index":0,"delta":{"role":"assistant","content":""},"finish_reason":"tool_calls"}]} + +data: [DONE] +""", + "openai": """ +data: {"id":"chatcmpl-9","object":"chat.completion.chunk","model":"gpt-4.1","choices":[{"index":0,"delta":{"role":"assistant","content":null,"tool_calls":[{"index":0,"id":"call_a","type":"function","function":{"name":"get_flow","arguments":""}}],"refusal":null},"finish_reason":null}]} + +data: {"id":"chatcmpl-9","object":"chat.completion.chunk","model":"gpt-4.1","choices":[{"index":0,"delta":{"tool_calls":[{"index":0,"function":{"arguments":"{\\"name\\""}}]},"finish_reason":null}]} + +data: {"id":"chatcmpl-9","object":"chat.completion.chunk","model":"gpt-4.1","choices":[{"index":0,"delta":{"tool_calls":[{"index":1,"id":"call_b","type":"function","function":{"name":"list_flows","arguments":""}}]},"finish_reason":null}]} + +data: {"id":"chatcmpl-9","object":"chat.completion.chunk","model":"gpt-4.1","choices":[{"index":0,"delta":{"tool_calls":[{"index":0,"function":{"arguments":":\\"demo\\"}"}}]},"finish_reason":null}]} + +data: {"id":"chatcmpl-9","object":"chat.completion.chunk","model":"gpt-4.1","choices":[{"index":0,"delta":{"tool_calls":[{"index":1,"function":{"arguments":"{}"}}]},"finish_reason":null}]} + +data: {"id":"chatcmpl-9","object":"chat.completion.chunk","model":"gpt-4.1","choices":[{"index":0,"delta":{},"finish_reason":"tool_calls"}]} + +data: {"id":"chatcmpl-9","object":"chat.completion.chunk","model":"gpt-4.1","choices":[],"usage":{"prompt_tokens":9,"completion_tokens":12,"total_tokens":21}} + +data: [DONE] +""", + "anthropic": """ +data: {"id":"msg_01","object":"chat.completion.chunk","model":"claude-sonnet-4-5","choices":[{"index":0,"delta":{"role":"assistant","content":"Reading "},"finish_reason":null}]} + +data: {"id":"msg_01","object":"chat.completion.chunk","model":"claude-sonnet-4-5","choices":[{"index":0,"delta":{"content":"it."},"finish_reason":null}]} + +data: {"id":"msg_01","object":"chat.completion.chunk","model":"claude-sonnet-4-5","choices":[{"index":0,"delta":{"tool_calls":[{"index":0,"id":"toolu_01","type":"function","function":{"name":"get_flow","arguments":""}}]},"finish_reason":null}]} + +data: {"id":"msg_01","object":"chat.completion.chunk","model":"claude-sonnet-4-5","choices":[{"index":0,"delta":{"tool_calls":[{"index":0,"function":{"arguments":"{\\"name\\": \\"de"}}]},"finish_reason":null}]} + +data: {"id":"msg_01","object":"chat.completion.chunk","model":"claude-sonnet-4-5","choices":[{"index":0,"delta":{"tool_calls":[{"index":0,"function":{"arguments":"mo\\"}"}}]},"finish_reason":null}]} + +data: {"id":"msg_01","object":"chat.completion.chunk","model":"claude-sonnet-4-5","choices":[{"index":0,"delta":{},"finish_reason":"tool_calls"}]} + +data: [DONE] +""", + "openrouter": """ +: OPENROUTER PROCESSING + +data: {"id":"gen-1","provider":"Google","model":"google/gemini-2.5-flash","object":"chat.completion.chunk","choices":[{"index":0,"delta":{"role":"assistant","content":"","reasoning":"Short answer."},"finish_reason":null}]} + +: OPENROUTER PROCESSING + +data: {"id":"gen-1","provider":"Google","model":"google/gemini-2.5-flash","object":"chat.completion.chunk","choices":[{"index":0,"delta":{"role":"assistant","content":"Hello"},"finish_reason":null}]} + +data: {"id":"gen-1","provider":"Google","model":"google/gemini-2.5-flash","object":"chat.completion.chunk","choices":[{"index":0,"delta":{"role":"assistant","content":" there"},"finish_reason":"stop"}],"usage":{"prompt_tokens":4,"completion_tokens":2,"total_tokens":6}} + +data: [DONE] +""", + # Gemini's compatibility endpoint sends each call whole and unnumbered. + "gemini": """ +data: {"choices":[{"delta":{"role":"assistant","tool_calls":[{"id":"c1","type":"function","function":{"name":"get_flow","arguments":"{\\"name\\":\\"demo\\"}"}},{"id":"c2","type":"function","function":{"name":"list_flows","arguments":"{}"}}]},"finish_reason":"tool_calls","index":0}],"object":"chat.completion.chunk"} + +data: [DONE] +""", +} + +GET_DEMO = {"name": "get_flow", "arguments": '{"name":"demo"}'} +LIST = {"name": "list_flows", "arguments": "{}"} + + +@pytest.mark.parametrize( + ("provider", "content", "calls"), + [ + ("ollama", "Hello there", []), + ("ollama-tools", None, [GET_DEMO]), + ("openai", None, [GET_DEMO, LIST]), + ("anthropic", "Reading it.", [{**GET_DEMO, "arguments": '{"name": "demo"}'}]), + ("openrouter", "Hello there", []), + ("gemini", None, [GET_DEMO, LIST]), + ], +) +def test_a_streamed_answer_comes_back_whole( + provider: str, content: str | None, calls: list[dict[str, str]] +) -> None: + assembler = client.Assembler() + pieces = [assembler.feed(line) for line in STREAMS[provider].splitlines()] + message = assembler.message() + assert "".join(pieces) == (content or "") + assert message["content"] == content + assert [c["function"] for c in message["tool_calls"] or []] == calls + assert all(c["id"] and c["type"] == "function" for c in message["tool_calls"] or []) + + +def test_an_error_inside_the_stream_stops_it() -> None: + assembler = client.Assembler() + line = 'data: {"error":{"code":502,"message":"Provider disconnected"},"choices":[{"index":0,"delta":{"content":""},"finish_reason":"error"}]}' + with pytest.raises(client.ProviderError, match="Provider disconnected"): + assembler.feed(line) diff --git a/backend/tests/test_artifacts.py b/backend/tests/test_artifacts.py index ecaa3c4..c23a27d 100644 --- a/backend/tests/test_artifacts.py +++ b/backend/tests/test_artifacts.py @@ -9,8 +9,11 @@ from sqlmodel import Session, col, select from fluksio.core.config import settings from fluksio.core.db import engine as db_engine from fluksio.flow.artifacts import ArtifactStore, VolatileStore +from fluksio.flow.messages import MessageSpec from fluksio.flow.runs import new_run_id, sweep_artifacts +from fluksio.flow.schemas import FlowDef, FlowInput from fluksio.flow.state import MemoryState +from fluksio.flow.store import FlowStore from fluksio.models import Run, RunArtifact @@ -112,6 +115,27 @@ def test_a_streamed_chunk_falls_out_once_the_message_moves_on(store): assert store.path(second["digest"]) is not None +def test_a_sweep_keeps_what_a_flow_input_starts_with(store, tmp_path): + """A flow that has never run holds its `initial` in no message yet.""" + published = store.put([b"published initial"]) + drafted = store.put([b"draft initial"]) + orphan = store.put([b"referred to by nothing"]) + + flows = FlowStore(tmp_path / "flows") + clip = MessageSpec(name="clip", dtype="artifact") + flows.write_flow( + FlowDef(name="f", inputs=[FlowInput(spec=clip, initial=published)]) + ) + flows.write_draft( + FlowDef(name="g", inputs=[FlowInput(spec=clip, initial=drafted)]), 0 + ) + + assert sweep_artifacts(store, MemoryState(), grace_s=0, flows=flows) == 1 + assert store.path(orphan["digest"]) is None + for kept in (published, drafted): + assert store.path(kept["digest"]) is not None + + def test_a_ring_drops_the_oldest_once_it_is_full(tmp_path): """What makes a camera affordable: the room is fixed, not the history.""" ring = VolatileStore(tmp_path / "ring", limit_bytes=3000) @@ -206,3 +230,33 @@ def test_a_type_that_can_run_script_is_served_sandboxed( assert headers["content-security-policy"] == "sandbox" assert headers["x-content-type-options"] == "nosniff" assert "content-security-policy" not in served(image, "image/png") + + +def test_a_superuser_deletes_an_artifact_nothing_references( + client: TestClient, + superuser_token_headers: dict[str, str], + normal_user_token_headers: dict[str, str], +) -> None: + url = f"{settings.API_V1_STR}/artifacts" + digest = client.put( + f"{url}?name=x.bin", headers=superuser_token_headers, content=b"delete me" + ).json()["digest"] + + refused = client.delete(f"{url}/{digest}", headers=normal_user_token_headers) + assert refused.status_code == 403 + + run_id = _run_row("ok", digest) + try: + held = client.delete(f"{url}/{digest}", headers=superuser_token_headers) + assert held.status_code == 409 + finally: + _forget(run_id) + + gone = client.delete(f"{url}/{digest}", headers=superuser_token_headers) + assert gone.status_code == 204 + assert ( + client.get(f"{url}/{digest}", headers=superuser_token_headers).status_code + == 404 + ) + again = client.delete(f"{url}/{digest}", headers=superuser_token_headers) + assert again.status_code == 404 diff --git a/backend/tests/test_cli.py b/backend/tests/test_cli.py index 61cb816..c6826c8 100644 --- a/backend/tests/test_cli.py +++ b/backend/tests/test_cli.py @@ -90,6 +90,31 @@ def test_run_arguments_are_typed_by_the_flow_they_are_for() -> None: _params(definition, ["--nonesuch", "1"]) +def test_inputs_after_a_double_dash_may_share_a_name_with_a_run_flag( + monkeypatch: pytest.MonkeyPatch, +) -> None: + """`run --timeout` is the wait; `-- timeout=5` is the flow's input.""" + from fluksio import cli + from fluksio.sdk import cli as sdk_cli + + seen = [] + monkeypatch.setattr( + sdk_cli, "cmd_run", lambda args, rest: seen.append((args.timeout, rest)) or 0 + ) + cli.main(["run", "train", "--timeout", "3", "--lr", "0.1", "--", "timeout=5"]) + assert seen == [(3.0, ["--lr", "0.1", "--", "timeout=5"])] + + definition = { + "inputs": [ + {"spec": {"name": "lr", "dtype": "float"}}, + {"spec": {"name": "timeout", "dtype": "int"}}, + ] + } + assert sdk_cli._params(definition, seen[0][1]) == {"lr": 0.1, "timeout": 5} + with pytest.raises(sdk_cli.SyncError, match="name=value"): + sdk_cli._params(definition, ["--", "timeout"]) + + def test_a_sweeps_param_spelling_is_refused_by_name() -> None: """`run --param lr=0.002` is a name this flow has not got, and says so.""" import pytest @@ -636,6 +661,8 @@ def test_two_files_of_one_name_are_refused(tmp_path) -> None: where it sits; this is the spelling that still gets there — two files named on the command line, each rooted at its own directory. """ + import sys + import pytest from fluksio.sdk import SyncError @@ -647,9 +674,14 @@ def test_two_files_of_one_name_are_refused(tmp_path) -> None: first = tmp_path / "s1" / "study.py" second = tmp_path / "s2" / "study.py" - _import(*_module_of(first), first) - with pytest.raises(SyncError, match="both import as 'study'"): - _import(*_module_of(second), second) + roots = {str(tmp_path / "s1"), str(tmp_path / "s2")} + try: + _import(*_module_of(first), first) + with pytest.raises(SyncError, match="both import as 'study'"): + _import(*_module_of(second), second) + finally: + sys.modules.pop("study", None) + sys.path[:] = [entry for entry in sys.path if entry not in roots] def test_how_long_ago_reads_like_a_duration() -> None: diff --git a/backend/tests/test_cloud.py b/backend/tests/test_cloud.py index 76e1665..791a9b2 100644 --- a/backend/tests/test_cloud.py +++ b/backend/tests/test_cloud.py @@ -93,6 +93,41 @@ def test_disconnecting_ends_remote_access( decode_token(portal_token(portal_key)) +def test_disconnecting_deletes_the_users_the_portal_let_in( + client: TestClient, + enrolled: User, + superuser_token_headers: dict[str, str], + db: Session, +) -> None: + """Nothing reaches them once the portal is gone; the rest stay.""" + member = User(email="member-dc@example.com", hashed_password="x", portal_sub="p-1") + boss = User( + email="boss-dc@example.com", + hashed_password="x", + portal_sub="p-2", + is_superuser=True, + ) + plain = create_random_user(db) + db.add(member) + db.add(boss) + db.commit() + member_id, boss_id = member.id, boss.id + + response = client.delete( + f"{settings.API_V1_STR}/cloud", headers=superuser_token_headers + ) + assert response.status_code == 200, response.text + db.expire_all() + assert db.get(User, member_id) is None + assert db.get(User, boss_id) is not None + assert db.get(User, plain.id) is not None + assert db.get(User, enrolled.id) is not None + assert cloud_config.load() is None + db.delete(db.get(User, boss_id)) + db.delete(db.get(User, plain.id)) + db.commit() + + def test_status_reports_not_enrolled( client: TestClient, superuser_token_headers: dict[str, str] ) -> None: @@ -641,3 +676,245 @@ async def test_the_link_sends_its_credential_as_a_header() -> None: await CloudConnector(Mock())._session(config) # type: ignore[arg-type] assert seen == {"path": "/api/v1/tunnel/attach", "auth": "Bearer t0k"} + + +@pytest.mark.anyio +async def test_a_revoked_link_stands_down(enrolled: User) -> None: # noqa: ARG001 + """A deleted instance is told so once, rather than refused forever. + + The config stays, so enrolling again is all it takes to come back. + """ + from websockets.asyncio.server import ServerConnection, serve + + from fluksio.cloud import connector + + dials = 0 + + async def hub(socket: ServerConnection) -> None: + nonlocal dials + dials += 1 + await socket.recv() + await socket.close(code=connector.REVOKED_CLOSE, reason="revoked") + + async with serve(hub, "127.0.0.1", 0) as server: + port = next(iter(server.sockets)).getsockname()[1] + config = cloud_config.load() + assert config is not None + cloud_config.save(replace(config, ws_url=f"ws://127.0.0.1:{port}/attach")) + link = connector.CloudConnector(Mock()) + await asyncio.wait_for(link.serve_forever(), timeout=5) + + assert dials == 1 + assert link.status()["revoked"] is True + assert cloud_config.load() is not None + + # And the watcher leaves it down until the enrolment changes. + app = Mock() + done = asyncio.get_running_loop().create_future() + done.set_result(None) + app.state = Mock( + cloud_task=done, + cloud_connector=link, + cloud_identity=connector._identity(cloud_config.load()), + ) + started: list[object] = [] + with ( + patch.object(connector, "ENROL_POLL_S", 0.01), + patch.object(connector, "start", lambda one: started.append(one)), + ): + watcher = asyncio.create_task(connector.watch_enrolment(app)) + await asyncio.sleep(0.05) + watcher.cancel() + assert not started + + +def test_a_member_removed_at_the_portal_loses_their_user(db: Session) -> None: + """The portal takes away only the ordinary accounts it gave.""" + from fluksio.cloud.connector import CloudConnector + + member = User(email="gone@example.com", hashed_password="x", portal_sub="p-9") + boss = User( + email="boss@example.com", + hashed_password="x", + portal_sub="p-8", + is_superuser=True, + ) + db.add(member) + db.add(boss) + db.commit() + member_id, boss_id = member.id, boss.id + + CloudConnector._forget_member("p-9", "p-8") + CloudConnector._forget_member("p-8", "p-8") + CloudConnector._forget_member("p-8", None) + db.expire_all() + assert db.get(User, member_id) is None + assert db.get(User, boss_id) is not None + db.delete(boss) + db.commit() + + +def test_admin_lists_and_revokes_portal_members( + client: TestClient, + enrolled: User, # noqa: ARG001 (fixture installs the enrolment) + superuser_token_headers: dict[str, str], + normal_user_token_headers: dict[str, str], + db: Session, +) -> None: + """Who the portal admits, and which of them have arrived here.""" + arrived = create_random_user(db) + arrived.portal_sub = str(uuid.uuid4()) + db.add(arrived) + db.commit() + waiting = str(uuid.uuid4()) + listed = Mock( + status_code=200, + json=Mock( + return_value={ + "data": [ + {"user_id": arrived.portal_sub, "email": arrived.email}, + {"user_id": waiting, "email": "invited@example.com"}, + ] + } + ), + ) + with patch("fluksio.api.routes.cloud.httpx.get", return_value=listed) as get: + members = client.get( + f"{settings.API_V1_STR}/cloud/members", headers=superuser_token_headers + ) + assert members.status_code == 200, members.text + assert get.call_args.args[0].endswith("/instance-members/") + assert {row["user_id"]: row["local_user_id"] for row in members.json()} == { + arrived.portal_sub: str(arrived.id), + waiting: None, + } + + with patch( + "fluksio.api.routes.cloud.httpx.delete", return_value=Mock(status_code=200) + ) as delete: + revoked = client.delete( + f"{settings.API_V1_STR}/cloud/members/{waiting}", + headers=superuser_token_headers, + ) + assert revoked.status_code == 200, revoked.text + assert delete.call_args.args[0].endswith(f"/instance-members/{waiting}") + + assert ( + client.get( + f"{settings.API_V1_STR}/cloud/members", headers=normal_user_token_headers + ).status_code + == 403 + ) + db.delete(arrived) + db.commit() + + +def test_a_superuser_links_a_pending_member_to_their_local_user( + client: TestClient, + enrolled: User, # noqa: ARG001 (fixture installs the enrolment) + portal_key: rsa.RSAPrivateKey, + superuser_token_headers: dict[str, str], + normal_user_token_headers: dict[str, str], + db: Session, +) -> None: + """Arriving alone they are refused; linked, they arrive as that user.""" + local = create_random_user(db) + boss = create_random_user(db) + boss.is_superuser = True + db.add(boss) + db.commit() + sub, boss_sub = str(uuid.uuid4()), str(uuid.uuid4()) + + def member(user_id: str, email: str) -> Mock: + return Mock( + status_code=200, + json=Mock(return_value={"user_id": user_id, "email": email}), + ) + + def link(user_id: str, reply: Mock, headers: dict[str, str]) -> httpx.Response: + with patch("fluksio.cloud.enroll.httpx.get", return_value=reply): + return client.post( + f"{settings.API_V1_STR}/cloud/members/{user_id}/link", headers=headers + ) + + # Never adopted on their own: the local row might be anyone's. + with patch("fluksio.cloud.enroll.httpx.get", return_value=member(sub, local.email)): + assert user_from_token(db, portal_token(portal_key, subject=sub)) is None + + listed = Mock( + status_code=200, + json=Mock( + return_value={ + "data": [ + {"user_id": sub, "email": local.email}, + {"user_id": boss_sub, "email": boss.email}, + ] + } + ), + ) + with patch("fluksio.api.routes.cloud.httpx.get", return_value=listed): + members = client.get( + f"{settings.API_V1_STR}/cloud/members", headers=superuser_token_headers + ).json() + assert {row["user_id"]: row["linkable"] for row in members} == { + sub: True, + boss_sub: False, + } + + su = superuser_token_headers + assert link(boss_sub, member(boss_sub, boss.email), su).status_code == 403 + assert link(sub, Mock(status_code=404), su).status_code == 404 + assert link(sub, member(sub, "nobody-here@example.com"), su).status_code == 404 + assert ( + link(sub, member(sub, local.email), normal_user_token_headers).status_code + == 403 + ) + + linked = link(sub, member(sub, local.email), su) + assert linked.status_code == 200, linked.text + assert linked.json()["portal_sub"] == sub + assert link(sub, member(sub, local.email), su).status_code == 409 + db.expire_all() + resolved = user_from_token(db, portal_token(portal_key, subject=sub)) + assert resolved is not None and resolved.id == local.id + db.delete(db.get(User, local.id)) + db.delete(db.get(User, boss.id)) + db.commit() + + +def test_the_portal_keys_follow_the_tunnel( + enrolled: User, # noqa: ARG001 (fixture installs the enrolment) + portal_key: rsa.RSAPrivateKey, +) -> None: + """A rotated keypair arrives on attach, and either key verifies by kid.""" + import json + + from cryptography.hazmat.primitives import serialization + + from fluksio.cloud.connector import CloudConnector + + pinned = cloud_config.load() + assert pinned is not None + successor = rsa.generate_private_key(public_exponent=65537, key_size=2048) + jwk = json.loads(jwt.algorithms.RSAAlgorithm.to_jwk(successor.public_key())) + jwk.update({"use": "sig", "alg": "RS256", "kid": "next"}) + rotated = {"keys": [*jwks(portal_key)["keys"], jwk]} + claims = jwt.decode(portal_token(portal_key), options={"verify_signature": False}) + pem = successor.private_bytes( + encoding=serialization.Encoding.PEM, + format=serialization.PrivateFormat.PKCS8, + encryption_algorithm=serialization.NoEncryption(), + ) + signed_by_next = jwt.encode(claims, pem, algorithm="RS256", headers={"kid": "next"}) + + # Over cleartext to another machine, a key is not worth taking. + CloudConnector._adopt_jwks(rotated) + assert cloud_config.load().jwks == pinned.jwks + with pytest.raises(jwt.exceptions.InvalidTokenError): + decode_token(signed_by_next) + + cloud_config.save(replace(pinned, ws_url="wss://hub.example.test/attach")) + CloudConnector._adopt_jwks({"keys": []}) + CloudConnector._adopt_jwks(rotated) + assert decode_token(portal_token(portal_key))["portal_sub"] == "portal-user-1" + assert decode_token(signed_by_next)["portal_sub"] == "portal-user-1" diff --git a/backend/tests/test_metrics.py b/backend/tests/test_metrics.py index 688573e..7d8d03a 100644 --- a/backend/tests/test_metrics.py +++ b/backend/tests/test_metrics.py @@ -11,7 +11,12 @@ from datetime import UTC, datetime from sqlmodel import Session, select from fluksio.flow.events import EventBus +from fluksio.flow.executor import ExecutionService +from fluksio.flow.messages import DType, MessageSpec from fluksio.flow.metrics import DETAIL_CAP, RUN_STALE_S, MetricsCollector +from fluksio.flow.nodes import Node +from fluksio.flow.pipeline import Pipeline +from fluksio.flow.queue import MemoryWorkQueue from fluksio.models import EngineEvent, FlowRun, MetricBucket FLOW = "metrics-test" @@ -292,3 +297,52 @@ def test_a_failure_keeps_the_run_it_happened_in(db: Session) -> None: failure = db.exec(select(EngineEvent).where(EngineEvent.run == "run-abc123")).one() assert failure.detail.startswith("ValueError: in a run") + + +def test_a_rate_limit_flush_is_a_run_that_names_its_parent(db: Session) -> None: + """A flush used to run under no id, so what it set off was in no run.""" + collector = MetricsCollector(EventBus()) + + class Bus: + """Hands every event straight to the collector.""" + + def publish(self, event: dict) -> None: + collector.handle(event) + + window = 0.05 + source = Node( + f=lambda params: None, + provides=[MessageSpec(name="raw", dtype=DType.FLOAT)], + name="in", + ) + relay = Node( + f=lambda raw, params: {"level": raw}, + requires=[MessageSpec(name="raw", dtype=DType.FLOAT)], + provides=[MessageSpec(name="level", dtype=DType.FLOAT, interval=window)], + name="relay", + ) + source.assign_flow("metrics-flush", "in") + relay.assign_flow("metrics-flush", "relay") + queue = MemoryWorkQueue() + pipeline = Pipeline(nodes=[source, relay], work_queue=queue, events=Bus()) + service = ExecutionService(queue, events=Bus()) + service.bind(pipeline) + + # The second value lands inside the relay's window and is held. + held_in = "" + for value in (1.0, 2.0): + source.inject({"raw": value}) + for item in queue.claim(10, 10): + service._run_item(item) + held_in = item.entry_id + time.sleep(window * 2) + queue.move_due(time.time()) + for item in queue.claim(10, 10): + service._run_item(item) + asyncio.run(collector.flush()) + + flush = db.exec(select(FlowRun).where(FlowRun.source == "flush")).one() + assert (flush.status, flush.parent) == ("ok", held_in) + parent = db.get(FlowRun, held_in) + assert parent is not None + assert (parent.flow, parent.source) == ("metrics-flush", "external") diff --git a/backend/tests/test_tunnel_reach.py b/backend/tests/test_tunnel_reach.py new file mode 100644 index 0000000..7214127 --- /dev/null +++ b/backend/tests/test_tunnel_reach.py @@ -0,0 +1,144 @@ +"""What the portal tunnel reaches on this instance, and what it refuses. + +Requests are handed to the connector as the hub frames them. Besides the API, +which the hub opens only to a portal session, the tunnel serves the MCP +endpoint and the flows' hooks, and a hook only with a secret. +""" + +import asyncio +import base64 +import json +from typing import Any + +import pytest +from fastapi import FastAPI +from fastapi.testclient import TestClient + +from fluksio.cloud.connector import CloudConnector +from fluksio.flow.controller import HOOK_PREFIX +from fluksio.flow.messages import DType, MessageSpec +from fluksio.flow.nodes import HttpNode, WebSocketNode +from fluksio.flow.pipeline import Pipeline + +SECRET = "tunnel-key" +PORT = [MessageSpec(name="temp", dtype=DType.FLOAT)] + + +class _Socket: + """The connector's side of the tunnel, recording what it sends the hub.""" + + def __init__(self) -> None: + self.frames: list[dict[str, Any]] = [] + + async def send(self, text: str) -> None: + self.frames.append(json.loads(text)) + + +def _through_the_tunnel(app: FastAPI, path: str, method: str = "POST") -> int | None: + """The status a call through the hub gets, or None when it is refused.""" + socket = _Socket() + body = base64.b64encode(json.dumps({"temp": 21.5}).encode()).decode() + frame = { + "id": "c1", + "method": method, + "path": path, + "query": "", + "headers": {"content-type": "application/json"}, + "body": body, + } + asyncio.run(CloudConnector(app)._serve(socket, frame)) + head = socket.frames[0] + return None if head["op"] == "err" else int(head["status"]) + + +def _hooks(*nodes: HttpNode | WebSocketNode) -> tuple[FastAPI, Pipeline]: + """Mount nodes the way the controller does, behind an app at the root.""" + pipeline = Pipeline(nodes=list(nodes)) + app = FastAPI() + app.mount("/", FastAPI()) # the MCP catch-all + for node in nodes: + node.url = f"{HOOK_PREFIX}/house/{node.url.lstrip('/')}" + node.register_route(app) + return app, pipeline + + +def _webhook(name: str, **params: object) -> HttpNode: + node = HttpNode(provides=PORT, params={"url": f"/{name}", **params}, name=name) + node.assign_flow("house", name) + return node + + +def _socket(name: str, **params: object) -> WebSocketNode: + node = WebSocketNode(provides=PORT, params={"url": f"/{name}", **params}, name=name) + node.assign_flow("house", name) + return node + + +def test_a_webhook_answers_the_tunnel_only_with_its_secret() -> None: + app, pipeline = _hooks(_webhook("open"), _webhook("guarded", secret=SECRET)) + + # On this network a hook without a secret stays open, as it always was. + assert TestClient(app).post("/hooks/house/open", json={}).status_code == 200 + # Through the hub it looks like a wrong secret does. + assert _through_the_tunnel(app, "/hooks/house/open") == 404 + assert _through_the_tunnel(app, "/hooks/house/guarded/wrong") == 404 + assert "house.temp" not in pipeline.values() + + assert _through_the_tunnel(app, f"/hooks/house/guarded/{SECRET}") == 200 + assert pipeline.values()["house.temp"]["value"] == 21.5 + + +@pytest.mark.parametrize( + "path", + [ + # httpx resolves a literal dot segment, which would carry a path the hub + # forwarded without a session onto the API. + "/hooks/../api/v1/login/access-token", + "/api/v1/../../mcp", + "/hooks/%2e%2e/api/v1/utils/health-check/", + "/docs", + "/.well-known/openid-configuration", + "/mcpx", + ], +) +def test_the_tunnel_refuses_what_it_does_not_serve(path: str) -> None: + assert _through_the_tunnel(FastAPI(), path, method="GET") is None + + +def test_a_served_socket_answers_the_tunnel_only_with_its_secret() -> None: + app, pipeline = _hooks(_socket("open"), _socket("guarded", secret=SECRET)) + + async def scenario() -> list[list[dict[str, Any]]]: + connector = CloudConnector(app) + refused = [] + for stream, path in [ + ("w1", "/hooks/house/open"), + ("w2", "/hooks/house/guarded/wrong"), + ("w3", "/hooks/house/nothing"), + ]: + socket = _Socket() + await connector._stream(socket, {"id": stream, "path": path, "query": ""}) + refused.append(socket.frames) + + socket = _Socket() + frame = {"id": "w4", "path": f"/hooks/house/guarded/{SECRET}", "query": ""} + task = asyncio.create_task(connector._stream(socket, frame)) + while not socket.frames: + await asyncio.sleep(0.01) + assert socket.frames == [{"op": "ws_open_ok", "id": "w4"}] + # What the session loop does with the hub's ws_msg. + connector._inboxes["w4"].put_nowait( + {"type": "websocket.receive", "text": '{"temp": 21.5}'} + ) + while "house.temp" not in pipeline.values(): + await asyncio.sleep(0.01) + task.cancel() + return refused + + refused = asyncio.run(scenario()) + # Refused before opening, every way alike. + assert refused == [ + [{"op": "ws_close", "id": stream, "code": 1008}] + for stream in ("w1", "w2", "w3") + ] + assert pipeline.values()["house.temp"]["value"] == 21.5 diff --git a/bun.lock b/bun.lock index b80c7dc..f825393 100644 --- a/bun.lock +++ b/bun.lock @@ -50,6 +50,7 @@ "react-grid-layout": "^2.2.4", "react-hook-form": "^7.68.0", "react-icons": "^5.5.0", + "react-markdown": "^10.1.0", "tailwind-merge": "^3.4.0", "tailwindcss": "^4.3.3", "uplot": "^1.6.32", @@ -493,10 +494,20 @@ "@types/d3-zoom": ["@types/d3-zoom@3.0.8", "", { "dependencies": { "@types/d3-interpolate": "*", "@types/d3-selection": "*" } }, "sha512-iqMC4/YlFCSlO8+2Ii1GGGliCAY4XdeG748w5vQUbevlbDu0zSjH/+jojorQVBK/se0j6DUFNPBGSqD3YWYnDw=="], + "@types/debug": ["@types/debug@4.1.13", "", { "dependencies": { "@types/ms": "*" } }, "sha512-KSVgmQmzMwPlmtljOomayoR89W4FynCAi3E8PPs7vmDVPe84hT+vGPKkJfThkmXs0x0jAaa9U8uW8bbfyS2fWw=="], + "@types/estree": ["@types/estree@1.0.9", "", {}, "sha512-GhdPgy1el4/ImP05X05Uw4cw2/M93BCUmnEvWZNStlCzEKME4Fkk+YpoA5OiHNQmoS7Cafb8Xa3Pya8m1Qrzeg=="], + "@types/estree-jsx": ["@types/estree-jsx@1.0.5", "", { "dependencies": { "@types/estree": "*" } }, "sha512-52CcUVNFyfb1A2ALocQw/Dd1BQFNmSdkuC3BkZ6iqhdMfQz7JWOFRuJFloOzjk+6WijU56m9oKXFAXc7o3Towg=="], + + "@types/hast": ["@types/hast@3.0.5", "", { "dependencies": { "@types/unist": "*" } }, "sha512-rp/ezSWaD1m44dPKICGhiskI13nVr7qTloFwDa/IYkhhf5nzwP+zIQcIJh3WIFSBOy/H1PzB40jPjMDksN4F+g=="], + "@types/json-schema": ["@types/json-schema@7.0.15", "", {}, "sha512-5+fP8P8MFNC+AyZCDxrB2pkZFPGzqQWUzpSeuuVLvm8VMcorNYavBqoFcxK8bQz4Qsbn4oUEEem4wDLfcysGHA=="], + "@types/mdast": ["@types/mdast@4.0.4", "", { "dependencies": { "@types/unist": "*" } }, "sha512-kGaNbPh1k7AFzgpud/gMdvIm5xuECykRR+JnWKQno9TAXVa6WIVCGTPvYGekIDL4uwCZQSYbUxNBSb1aUo79oA=="], + + "@types/ms": ["@types/ms@2.1.0", "", {}, "sha512-GsCCIZDE/p3i96vtEqx+7dBUGXrc7zeSK3wwPHIaRThS+9OhWIXRqzs4d6k1SVU8g91DrNRWxWUGhp5KXQb2VA=="], + "@types/node": ["@types/node@25.0.10", "", { "dependencies": { "undici-types": "~7.16.0" } }, "sha512-zWW5KPngR/yvakJgGOmZ5vTBemDoSqF3AcV/LrO5u5wTWyEAVVh+IT39G4gtyAkh3CtTZs8aX/yRM82OfzHJRg=="], "@types/react": ["@types/react@19.2.9", "", { "dependencies": { "csstype": "^3.2.2" } }, "sha512-Lpo8kgb/igvMIPeNV2rsYKTgaORYdO1XGVZ4Qz3akwOj0ySGYMPlQWa8BaLn0G63D1aSaAQ5ldR06wCpChQCjA=="], @@ -505,6 +516,10 @@ "@types/trusted-types": ["@types/trusted-types@2.0.7", "", {}, "sha512-ScaPdn1dQczgbl0QFTeTOmVHFULt394XJgOQNoyVhZ6r2vLnMLJfBPd53SB52T/3G36VI1/g2MZaX0cwDuXsfw=="], + "@types/unist": ["@types/unist@3.0.3", "", {}, "sha512-ko/gIFJRv177XgZsZcBwnqJN5x/Gien8qNOn0D5bQU/zAzVf9Zt3BlcUiLqhV9y4ARk0GbT3tnUiPNgnTXzc/Q=="], + + "@ungap/structured-clone": ["@ungap/structured-clone@1.4.0", "", {}, "sha512-1mEZtMKPM09vDmQt5y7YvmN2+DFTP7Tg0EWXdic8/C6VRnpb33e4ghisCIE3WZjsE2N8mf+QV1Zqh7ZFYLWInQ=="], + "@vitejs/plugin-react-swc": ["@vitejs/plugin-react-swc@4.2.2", "", { "dependencies": { "@rolldown/pluginutils": "1.0.0-beta.47", "@swc/core": "^1.13.5" }, "peerDependencies": { "vite": "^4 || ^5 || ^6 || ^7" } }, "sha512-x+rE6tsxq/gxrEJN3Nv3dIV60lFflPj94c90b+NNo6n1QV1QQUTLoL0MpaOVasUZ0zqVBn7ead1B5ecx1JAGfA=="], "@xyflow/react": ["@xyflow/react@12.11.3", "", { "dependencies": { "@xyflow/system": "0.0.80", "classcat": "^5.0.3", "zustand": "^4.4.0" }, "peerDependencies": { "@types/react": ">=17", "@types/react-dom": ">=17", "react": ">=17", "react-dom": ">=17" }, "optionalPeers": ["@types/react", "@types/react-dom"] }, "sha512-G3jogHz2GWUtIOkhavUGno2YzY9u6fILIJBttfsBendb0/HWB90JG+sOTAvlIMEwyvq9zgy9V9ZQSwyQjR5QzQ=="], @@ -529,6 +544,8 @@ "babel-dead-code-elimination": ["babel-dead-code-elimination@1.0.12", "", { "dependencies": { "@babel/core": "^7.23.7", "@babel/parser": "^7.23.6", "@babel/traverse": "^7.23.7", "@babel/types": "^7.23.6" } }, "sha512-GERT7L2TiYcYDtYk1IpD+ASAYXjKbLTDPhBtYj7X1NuRMDTMtAx9kyBenub1Ev41lo91OHCKdmP+egTDmfQ7Ig=="], + "bail": ["bail@2.0.2", "", {}, "sha512-0xO6mYd7JB2YesxDKplafRpsiOzPt9V02ddPCLbY1xYGPOX24NTyN50qnUxgCPcSoYMhKpAuBTjQoRZCAkUDRw=="], + "baseline-browser-mapping": ["baseline-browser-mapping@2.11.27", "", { "bin": { "baseline-browser-mapping": "dist/cli.cjs" } }, "sha512-ElY12DaROGuan+lMmZ8Cvo/ZUbXPe7Enc/9VU/b1T3Kp4dwytRcNdR8DoSJN5SNJT/CuvcCA0DHDVmMOCePdRQ=="], "boring-avatars": ["boring-avatars@2.0.4", "", { "peerDependencies": { "react": ">=18.0.0", "react-dom": ">=18.0.0" } }, "sha512-xhZO/w/6aFmRfkaWohcl2NfyIy87gK5SBbys8kctZeTGF1Apjpv/10pfUuv+YEfVPkESU/h2Y6tt/Dwp+bIZPw=="], @@ -543,6 +560,16 @@ "caniuse-lite": ["caniuse-lite@1.0.30001814", "", {}, "sha512-/Uaf1lAzr59XcMpW0o96WoEfr+VXK2OX4U9AgFoiSHsVJ4HppnIFUjtYzsyDH2+tgANaQb2/oxYGwCPapN1FpA=="], + "ccount": ["ccount@2.0.1", "", {}, "sha512-eyrF0jiFpY+3drT6383f1qhkbGsLSifNAjA61IUjZjmLCWjItY6LB9ft9YhoDgwfmclB2zhu51Lc7+95b8NRAg=="], + + "character-entities": ["character-entities@2.0.2", "", {}, "sha512-shx7oQ0Awen/BRIdkjkvz54PnEEI/EjwXDSIZp86/KKdbafHh1Df/RYGBhn4hbe2+uKC9FnT5UCEdyPz3ai9hQ=="], + + "character-entities-html4": ["character-entities-html4@2.1.0", "", {}, "sha512-1v7fgQRj6hnSwFpq1Eu0ynr/CDEw0rXo2B61qXrLNdHZmPKgb7fqS1a2JwF0rISo9q77jDI8VMEHoApn8qDoZA=="], + + "character-entities-legacy": ["character-entities-legacy@3.0.0", "", {}, "sha512-RpPp0asT/6ufRm//AJVwpViZbGM/MkjQFxJccQRHmISF/22NBtsHqAWmL+/pmkPWoIUJdWyeVleTl1wydHATVQ=="], + + "character-reference-invalid": ["character-reference-invalid@2.0.1", "", {}, "sha512-iBZ4F4wRbyORVsu0jPV7gXkOsGYjGHPmAyv+HiHG8gi5PtC9KI2j1+v8/tlibRvjoWX027ypmG/n0HtO5t7unw=="], + "chokidar": ["chokidar@5.0.0", "", { "dependencies": { "readdirp": "^5.0.0" } }, "sha512-TQMmc3w+5AxjpL8iIiwebF73dRDF4fBIieAqGn9RGCWaEVwQ6Fb2cGe31Yns0RRIzii5goJ1Y7xbMwo1TxMplw=="], "chownr": ["chownr@2.0.0", "", {}, "sha512-bIomtDF5KGpdogkLd9VspvFzk9KfpyyGlS8YFVZl7TGPBHL5snIOnxeshwVgPteQ9b4Eydl+pVbIyE1DcvCWgQ=="], @@ -561,6 +588,8 @@ "combined-stream": ["combined-stream@1.0.8", "", { "dependencies": { "delayed-stream": "~1.0.0" } }, "sha512-FQN4MRfuJeHf7cBbBMJFXhKSDq+2kAArBlmRBvcvFE5BB1HZKXtSFASDhdlz9zOYwxh8lDdnvmMOe/+5cdoEdg=="], + "comma-separated-tokens": ["comma-separated-tokens@2.0.3", "", {}, "sha512-Fu4hJdvzeylCfQPp9SGWidpzrMs7tTrlu6Vb8XGaRGck8QSNZJJp538Wrb60Lax4fPwR64ViY468OIUTbRlGZg=="], + "commander": ["commander@13.0.0", "", {}, "sha512-oPYleIY8wmTVzkvQq10AEok6YcTC4sRUBl8F9gVuwchGVUCTbl/vhLTaQqutuuySYOsu8YTgV+OxKc/8Yvx+mQ=="], "confbox": ["confbox@0.1.8", "", {}, "sha512-RMtmw0iFkeR4YV+fUOSucriAQNb9g8zFR52MWCtl+cCZOFRNL6zeB395vPzFhEjjn4fMxXudmELnl/KF/WrK6w=="], @@ -597,6 +626,8 @@ "debug": ["debug@4.4.3", "", { "dependencies": { "ms": "^2.1.3" } }, "sha512-RGwwWnwQvkVfavKVt22FGLw+xYSdzARwm0ru6DhTVA3umU5hZc28V3kO4stgYryrTlLpuvgI9GiijltAjNbcqA=="], + "decode-named-character-reference": ["decode-named-character-reference@1.3.0", "", { "dependencies": { "character-entities": "^2.0.0" } }, "sha512-GtpQYB283KrPp6nRw50q3U9/VfOutZOe103qlN7BPP6Ad27xYnOIWv4lPzo8HCAL+mMZofJ9KEy30fq6MfaK6Q=="], + "default-browser": ["default-browser@5.4.0", "", { "dependencies": { "bundle-name": "^4.1.0", "default-browser-id": "^5.0.0" } }, "sha512-XDuvSq38Hr1MdN47EDvYtx3U0MTqpCEn+F6ft8z2vYDzMrvQhVp0ui9oQdqW3MvK3vqUETglt1tVGgjLuJ5izg=="], "default-browser-id": ["default-browser-id@5.0.1", "", {}, "sha512-x1VCxdX4t+8wVfd1so/9w+vQ4vx7lKd2Qp5tDRutErwmR85OgmfX7RlLRMWafRMY7hbEiXIbudNrjOAPa/hL8Q=="], @@ -607,12 +638,16 @@ "delayed-stream": ["delayed-stream@1.0.0", "", {}, "sha512-ZySD7Nf91aLB0RxL4KGrKHBXl7Eds1DAmEdcoVawXnLD7SDhpNgtuII2aAkg7a7QS41jxPSZ17p4VdGnMHk3MQ=="], + "dequal": ["dequal@2.0.3", "", {}, "sha512-0je+qPKHEMohvfRTCEo3CrPG6cAzAYgmzKyxRiYSSDkS6eGJdyVJm7WaYA5ECaAD9wLB2T4EEeymA5aFVcYXCA=="], + "destr": ["destr@2.0.5", "", {}, "sha512-ugFTXCtDZunbzasqBxrK93Ik/DRYsO6S/fedkWEMKqt04xZ4csmnmwGDBAb07QWNaGMAmnTIemsYZCksjATwsA=="], "detect-libc": ["detect-libc@2.1.2", "", {}, "sha512-Btj2BOOO83o3WyH59e8MgXsxEQVcarkUOpEYrubB0urwnN10yQ364rsiByU11nZlqWYZm05i/of7io4mzihBtQ=="], "detect-node-es": ["detect-node-es@1.1.0", "", {}, "sha512-ypdmJU/TbBby2Dxibuv7ZLW3Bs1QEmM7nHjEANfohJLvE0XVujisn1qPJcZxg+qDucsr+bP6fLD1rPS3AhJ7EQ=="], + "devlop": ["devlop@1.1.0", "", { "dependencies": { "dequal": "^2.0.0" } }, "sha512-RWmIqhcFf1lRYBvNmr7qTNuyCt/7/ns2jbpp1+PalgE/rDQcBT0fioSMUpJ93irlUhC5hrg4cYqe6U+0ImW0rA=="], + "diff": ["diff@8.0.4", "", {}, "sha512-DPi0FmjiSU5EvQV0++GFDOJ9ASQUVFh5kD+OzOnYdi7n3Wpm9hWWGfB/O2blfHcMVTL5WkQXSnRiK9makhrcnw=="], "dompurify": ["dompurify@3.4.16", "", { "optionalDependencies": { "@types/trusted-types": "^2.0.7" } }, "sha512-sqo+pNp3qRhCIpbgRi1y8Tgk27Bo2Ry7w0dC1NBeNTdZChWjz9Xb/KOoZbRP/R6pQZ80Qw8YhXw13hWWBbMRnQ=="], @@ -639,6 +674,10 @@ "escalade": ["escalade@3.2.0", "", {}, "sha512-WUj2qlxaQtO4g6Pq5c29GTcWGDyd8itL8zTlipgECz3JesAiiOKotd8JU6otB3PACgG6xkJUyVhboMS+bje/jA=="], + "estree-util-is-identifier-name": ["estree-util-is-identifier-name@3.0.0", "", {}, "sha512-hFtqIDZTIUZ9BXLb8y4pYGyk6+wekIivNVTcmvk8NoOh+VeRn5y6cEHzbURrWbfp1fIqdVipilzj+lfaadNZmg=="], + + "extend": ["extend@3.0.2", "", {}, "sha512-fjquC59cD7CyW6urNXK0FBufkZcoiGG80wTuPujX590cB5Ttln20E2UB4S/WARVqhXffZl2LNgS+gQdPIIim/g=="], + "fast-equals": ["fast-equals@4.0.3", "", {}, "sha512-G3BSX9cfKttjr+2o1O22tYMLq0DPluZnYtq1rXumE1SpL/F/SLIfHx08WYQoWSIpeMYf8sRbJ8++71+v6Pnxfg=="], "fdir": ["fdir@6.5.0", "", { "peerDependencies": { "picomatch": "^3 || ^4" } }, "sha512-tIbYtZbucOs0BRGqPJkshJUYdL+SDH7dVM8gjy+ERp3WAUjLEFJE+02kanyHtwjWOnwrKYBiwAmM0p4kLJAnXg=="], @@ -681,12 +720,30 @@ "hasown": ["hasown@2.0.4", "", { "dependencies": { "function-bind": "^1.1.2" } }, "sha512-T2UbfbBEF32wiepXIsMlTW9+dDYC6wMh/t/vYA4tuOMKqWz/n3vr1NFSxQiyP+zk2mXsoMA/i/7qV6LKut1t1A=="], + "hast-util-to-jsx-runtime": ["hast-util-to-jsx-runtime@2.3.6", "", { "dependencies": { "@types/estree": "^1.0.0", "@types/hast": "^3.0.0", "@types/unist": "^3.0.0", "comma-separated-tokens": "^2.0.0", "devlop": "^1.0.0", "estree-util-is-identifier-name": "^3.0.0", "hast-util-whitespace": "^3.0.0", "mdast-util-mdx-expression": "^2.0.0", "mdast-util-mdx-jsx": "^3.0.0", "mdast-util-mdxjs-esm": "^2.0.0", "property-information": "^7.0.0", "space-separated-tokens": "^2.0.0", "style-to-js": "^1.0.0", "unist-util-position": "^5.0.0", "vfile-message": "^4.0.0" } }, "sha512-zl6s8LwNyo1P9uw+XJGvZtdFF1GdAkOg8ujOw+4Pyb76874fLps4ueHXDhXWdk6YHQ6OgUtinliG7RsYvCbbBg=="], + + "hast-util-whitespace": ["hast-util-whitespace@3.0.0", "", { "dependencies": { "@types/hast": "^3.0.0" } }, "sha512-88JUN06ipLwsnv+dVn+OIYOvAuvBMy/Qoi6O7mQHxdPXpjy+Cd6xRkWwux7DKO+4sYILtLBRIKgsdpS2gQc7qw=="], + + "html-url-attributes": ["html-url-attributes@3.0.1", "", {}, "sha512-ol6UPyBWqsrO6EJySPz2O7ZSr856WDrEzM5zMqp+FJJLGMW35cLYmmZnl0vztAZxRUoNZJFTCohfjuIJ8I4QBQ=="], + "https-proxy-agent": ["https-proxy-agent@5.0.1", "", { "dependencies": { "agent-base": "6", "debug": "4" } }, "sha512-dFcAjpTQFgoLMzC2VwU+C/CbS7uRL0lWmxDITmqm7C+7F0Odmj6s9l6alZc6AELXhrnggM2CeWSXHGOdX2YtwA=="], + "inline-style-parser": ["inline-style-parser@0.2.7", "", {}, "sha512-Nb2ctOyNR8DqQoR0OwRG95uNWIC0C1lCgf5Naz5H6Ji72KZ8OcFZLz2P5sNgwlyoJ8Yif11oMuYs5pBQa86csA=="], + + "is-alphabetical": ["is-alphabetical@2.0.1", "", {}, "sha512-FWyyY60MeTNyeSRpkM2Iry0G9hpr7/9kD40mD/cGQEuilcZYS4okz8SN2Q6rLCJ8gbCt6fN+rC+6tMGS99LaxQ=="], + + "is-alphanumerical": ["is-alphanumerical@2.0.1", "", { "dependencies": { "is-alphabetical": "^2.0.0", "is-decimal": "^2.0.0" } }, "sha512-hmbYhX/9MUMF5uh7tOXyK/n0ZvWpad5caBA17GsC6vyuCqaWliRG5K1qS9inmUhEMaOBIW7/whAnSwveW/LtZw=="], + + "is-decimal": ["is-decimal@2.0.1", "", {}, "sha512-AAB9hiomQs5DXWcRB1rqsxGUstbRroFOPPVAomNk/3XHR5JyEZChOyTWe2oayKnsSsr/kcGqF+z6yuH6HHpN0A=="], + "is-docker": ["is-docker@3.0.0", "", { "bin": "cli.js" }, "sha512-eljcgEDlEns/7AXFosB5K/2nCM4P7FQPkGc/DWLy5rmFEWvZayGrik1d9/QIY5nJ4f9YsVvBkA6kJpHn9rISdQ=="], + "is-hexadecimal": ["is-hexadecimal@2.0.1", "", {}, "sha512-DgZQp241c8oO6cA1SbTEWiXeoxV42vlcJxgH+B3hi1AiqqKruZR3ZGF8In3fj4+/y/7rHvlOZLZtgJ/4ttYGZg=="], + "is-inside-container": ["is-inside-container@1.0.0", "", { "dependencies": { "is-docker": "^3.0.0" }, "bin": "cli.js" }, "sha512-KIYLCCJghfHZxqjYBE7rEy0OBuTd5xCHS7tHVgvCLkx7StIoaxwNW3hCALgEUjFfeRk+MG/Qxmp/vtETEF3tRA=="], + "is-plain-obj": ["is-plain-obj@4.1.0", "", {}, "sha512-+Pgi+vMuUNkJyExiMBt5IlFoMyKnr5zhJ4Uspz58WOhBF5QoIZkFyNHIbBAtHwzVAgk5RtndVNsDRN61/mmDqg=="], + "is-wsl": ["is-wsl@3.1.0", "", { "dependencies": { "is-inside-container": "^1.0.0" } }, "sha512-UcVfVfaK4Sc4m7X3dUSoHoozQGBEFeDC+zVo06t98xe8CzHSZZBekNXH+tu0NalHolcJ/QAGqS46Hef7QXBIMw=="], "isbot": ["isbot@5.1.33", "", {}, "sha512-P4Hgb5NqswjkI0J1CM6XKXon/sxKY1SuowE7Qx2hrBhIwICFyXy54mfgB5eMHXsbe/eStzzpbIGNOvGmz+dlKg=="], @@ -727,6 +784,8 @@ "lodash": ["lodash@4.18.1", "", {}, "sha512-dMInicTPVE8d1e5otfwmmjlxkZoUpiVLwyeTdUsi/Caj/gfzzblBcCE5sRHV/AsjuCmxWrte2TNGSYuCeCq+0Q=="], + "longest-streak": ["longest-streak@3.1.0", "", {}, "sha512-9Ri+o0JYgehTaVBBDoMqIl8GXtbWg711O3srftcHhZ0dqnETqLaoIK0x17fUw9rFSlK/0NlsKe0Ahhyl5pXE2g=="], + "loose-envify": ["loose-envify@1.4.0", "", { "dependencies": { "js-tokens": "^3.0.0 || ^4.0.0" }, "bin": { "loose-envify": "cli.js" } }, "sha512-lyuxPGr/Wfhrlem2CL/UcnUc1zcqKAImBDzukY7Y5F/yQiNdko6+fRLevlw1HgMySw7f611UIY408EtxRSoK3Q=="], "lru-cache": ["lru-cache@5.1.1", "", { "dependencies": { "yallist": "^3.0.2" } }, "sha512-KpNARQA3Iwv+jTA0utUVVbrh+Jlrr1Fv0e56GGzAFOXN7dk/FviaDW8LHmK52DlcH4WP2n6gI8vN1aesBFgo9w=="], @@ -739,6 +798,66 @@ "math-intrinsics": ["math-intrinsics@1.1.0", "", {}, "sha512-/IXtbwEk5HTPyEwyKX6hGkYXxM9nbj64B+ilVJnC/R6B0pH5G4V3b0pVbL7DBj4tkhBAppbQUlf6F6Xl9LHu1g=="], + "mdast-util-from-markdown": ["mdast-util-from-markdown@2.1.0", "", { "dependencies": { "@types/mdast": "^4.0.0", "@types/unist": "^3.0.0", "decode-named-character-reference": "^1.0.0", "devlop": "^1.0.0", "mdast-util-to-string": "^4.0.0", "micromark": "^4.0.0", "micromark-util-decode-numeric-character-reference": "^2.0.0", "micromark-util-decode-string": "^2.0.0", "micromark-util-normalize-identifier": "^2.0.0", "micromark-util-symbol": "^2.0.0", "micromark-util-types": "^2.0.0", "unist-util-stringify-position": "^4.0.0" } }, "sha512-KRLgmPaP2H1ul2bpoJkOS+9pAcDAAj9Nu1mqLnh8NLeJG5tlvj42TH80PIGBpROWlRXLk0lDrWNuZCFy/tsB4A=="], + + "mdast-util-mdx-expression": ["mdast-util-mdx-expression@2.0.1", "", { "dependencies": { "@types/estree-jsx": "^1.0.0", "@types/hast": "^3.0.0", "@types/mdast": "^4.0.0", "devlop": "^1.0.0", "mdast-util-from-markdown": "^2.0.0", "mdast-util-to-markdown": "^2.0.0" } }, "sha512-J6f+9hUp+ldTZqKRSg7Vw5V6MqjATc+3E4gf3CFNcuZNWD8XdyI6zQ8GqH7f8169MM6P7hMBRDVGnn7oHB9kXQ=="], + + "mdast-util-mdx-jsx": ["mdast-util-mdx-jsx@3.2.0", "", { "dependencies": { "@types/estree-jsx": "^1.0.0", "@types/hast": "^3.0.0", "@types/mdast": "^4.0.0", "@types/unist": "^3.0.0", "ccount": "^2.0.0", "devlop": "^1.1.0", "mdast-util-from-markdown": "^2.0.0", "mdast-util-to-markdown": "^2.0.0", "parse-entities": "^4.0.0", "stringify-entities": "^4.0.0", "unist-util-stringify-position": "^4.0.0", "vfile-message": "^4.0.0" } }, "sha512-lj/z8v0r6ZtsN/cGNNtemmmfoLAFZnjMbNyLzBafjzikOM+glrjNHPlf6lQDOTccj9n5b0PPihEBbhneMyGs1Q=="], + + "mdast-util-mdxjs-esm": ["mdast-util-mdxjs-esm@2.0.1", "", { "dependencies": { "@types/estree-jsx": "^1.0.0", "@types/hast": "^3.0.0", "@types/mdast": "^4.0.0", "devlop": "^1.0.0", "mdast-util-from-markdown": "^2.0.0", "mdast-util-to-markdown": "^2.0.0" } }, "sha512-EcmOpxsZ96CvlP03NghtH1EsLtr0n9Tm4lPUJUBccV9RwUOneqSycg19n5HGzCf+10LozMRSObtVr3ee1WoHtg=="], + + "mdast-util-phrasing": ["mdast-util-phrasing@4.1.0", "", { "dependencies": { "@types/mdast": "^4.0.0", "unist-util-is": "^6.0.0" } }, "sha512-TqICwyvJJpBwvGAMZjj4J2n0X8QWp21b9l0o7eXyVJ25YNWYbJDVIyD1bZXE6WtV6RmKJVYmQAKWa0zWOABz2w=="], + + "mdast-util-to-hast": ["mdast-util-to-hast@13.2.1", "", { "dependencies": { "@types/hast": "^3.0.0", "@types/mdast": "^4.0.0", "@ungap/structured-clone": "^1.0.0", "devlop": "^1.0.0", "micromark-util-sanitize-uri": "^2.0.0", "trim-lines": "^3.0.0", "unist-util-position": "^5.0.0", "unist-util-visit": "^5.0.0", "vfile": "^6.0.0" } }, "sha512-cctsq2wp5vTsLIcaymblUriiTcZd0CwWtCbLvrOzYCDZoWyMNV8sZ7krj09FSnsiJi3WVsHLM4k6Dq/yaPyCXA=="], + + "mdast-util-to-markdown": ["mdast-util-to-markdown@2.2.0", "", { "dependencies": { "@types/mdast": "^4.0.0", "@types/unist": "^3.0.0", "longest-streak": "^3.0.0", "mdast-util-phrasing": "^4.0.0", "mdast-util-to-string": "^4.0.0", "micromark-util-character": "^2.0.0", "micromark-util-classify-character": "^2.0.0", "micromark-util-decode-string": "^2.0.0", "micromark-util-html-tag-name": "^2.0.0", "unist-util-visit": "^5.0.0", "zwitch": "^2.0.0" } }, "sha512-2Jn/ADrHFFyyniBMi0bncTbJqU78K3/TEYvzvwrWWzD9MYPUa/Gm4r3nOeKFQg/PDM5BsbDYdc8ackW4NTY6NA=="], + + "mdast-util-to-string": ["mdast-util-to-string@4.0.0", "", { "dependencies": { "@types/mdast": "^4.0.0" } }, "sha512-0H44vDimn51F0YwvxSJSm0eCDOJTRlmN0R1yBh4HLj9wiV1Dn0QoXGbvFAWj2hSItVTlCmBF1hqKlIyUBVFLPg=="], + + "micromark": ["micromark@4.0.3", "", { "dependencies": { "@types/debug": "^4.0.0", "debug": "^4.0.0", "decode-named-character-reference": "^1.0.0", "devlop": "^1.0.0", "micromark-core-commonmark": "^2.0.0", "micromark-factory-space": "^2.0.0", "micromark-util-character": "^2.0.0", "micromark-util-chunked": "^2.0.0", "micromark-util-combine-extensions": "^2.0.0", "micromark-util-decode-numeric-character-reference": "^2.0.0", "micromark-util-edit-map": "^1.0.0", "micromark-util-encode": "^2.0.0", "micromark-util-normalize-identifier": "^2.0.0", "micromark-util-resolve-all": "^2.0.0", "micromark-util-sanitize-uri": "^2.0.0", "micromark-util-subtokenize": "^2.0.0", "micromark-util-symbol": "^2.0.0", "micromark-util-types": "^2.0.0" } }, "sha512-oGYfQzHSG5dOMovQcJ3fyTmZlWAWpi0XA0sJwJs+i6OT88o1+Jtw/8z0CmdowSLxGhhFK89rQ/oXph/wN02PNw=="], + + "micromark-core-commonmark": ["micromark-core-commonmark@2.0.4", "", { "dependencies": { "decode-named-character-reference": "^1.0.0", "devlop": "^1.0.0", "micromark-factory-destination": "^2.0.0", "micromark-factory-label": "^2.0.0", "micromark-factory-space": "^2.1.0", "micromark-factory-title": "^2.0.0", "micromark-factory-whitespace": "^2.0.0", "micromark-util-character": "^2.0.0", "micromark-util-chunked": "^2.0.0", "micromark-util-classify-character": "^2.0.0", "micromark-util-edit-map": "^1.0.0", "micromark-util-html-tag-name": "^2.0.0", "micromark-util-normalize-identifier": "^2.0.0", "micromark-util-resolve-all": "^2.0.0", "micromark-util-subtokenize": "^2.0.0", "micromark-util-symbol": "^2.0.0", "micromark-util-types": "^2.0.3" } }, "sha512-wxEeE8v8XVvOrxn1TZj74qYhAtTQsSqufHVS3uNVyT1MupXxhyaHk8/9xDNPh9BmL77QNDgWTkZ3RsYHB2ry7w=="], + + "micromark-factory-destination": ["micromark-factory-destination@2.0.1", "", { "dependencies": { "micromark-util-character": "^2.0.0", "micromark-util-symbol": "^2.0.0", "micromark-util-types": "^2.0.0" } }, "sha512-Xe6rDdJlkmbFRExpTOmRj9N3MaWmbAgdpSrBQvCFqhezUn4AHqJHbaEnfbVYYiexVSs//tqOdY/DxhjdCiJnIA=="], + + "micromark-factory-label": ["micromark-factory-label@2.0.1", "", { "dependencies": { "devlop": "^1.0.0", "micromark-util-character": "^2.0.0", "micromark-util-symbol": "^2.0.0", "micromark-util-types": "^2.0.0" } }, "sha512-VFMekyQExqIW7xIChcXn4ok29YE3rnuyveW3wZQWWqF4Nv9Wk5rgJ99KzPvHjkmPXF93FXIbBp6YdW3t71/7Vg=="], + + "micromark-factory-space": ["micromark-factory-space@2.1.0", "", { "dependencies": { "micromark-util-character": "^2.0.0", "micromark-util-types": "^2.0.0" } }, "sha512-fS8hnLIjnjvdQIj39Geug8wWsR0HrYZ43KShKxNfwT7t2LOHo/LbWZEzEaSOjwtjdCsjoE6syHhonEzW0zv0+Q=="], + + "micromark-factory-title": ["micromark-factory-title@2.0.1", "", { "dependencies": { "micromark-factory-space": "^2.0.0", "micromark-util-character": "^2.0.0", "micromark-util-symbol": "^2.0.0", "micromark-util-types": "^2.0.0" } }, "sha512-5bZ+3CjhAd9eChYTHsjy6TGxpOFSKgKKJPJxr293jTbfry2KDoWkhBb6TcPVB4NmzaPhMs1Frm9AZH7OD4Cjzw=="], + + "micromark-factory-whitespace": ["micromark-factory-whitespace@2.0.1", "", { "dependencies": { "micromark-factory-space": "^2.0.0", "micromark-util-character": "^2.0.0", "micromark-util-symbol": "^2.0.0", "micromark-util-types": "^2.0.0" } }, "sha512-Ob0nuZ3PKt/n0hORHyvoD9uZhr+Za8sFoP+OnMcnWK5lngSzALgQYKMr9RJVOWLqQYuyn6ulqGWSXdwf6F80lQ=="], + + "micromark-util-character": ["micromark-util-character@2.1.1", "", { "dependencies": { "micromark-util-symbol": "^2.0.0", "micromark-util-types": "^2.0.0" } }, "sha512-wv8tdUTJ3thSFFFJKtpYKOYiGP2+v96Hvk4Tu8KpCAsTMs6yi+nVmGh1syvSCsaxz45J6Jbw+9DD6g97+NV67Q=="], + + "micromark-util-chunked": ["micromark-util-chunked@2.0.1", "", { "dependencies": { "micromark-util-symbol": "^2.0.0" } }, "sha512-QUNFEOPELfmvv+4xiNg2sRYeS/P84pTW0TCgP5zc9FpXetHY0ab7SxKyAQCNCc1eK0459uoLI1y5oO5Vc1dbhA=="], + + "micromark-util-classify-character": ["micromark-util-classify-character@2.0.1", "", { "dependencies": { "micromark-util-character": "^2.0.0", "micromark-util-symbol": "^2.0.0", "micromark-util-types": "^2.0.0" } }, "sha512-K0kHzM6afW/MbeWYWLjoHQv1sgg2Q9EccHEDzSkxiP/EaagNzCm7T/WMKZ3rjMbvIpvBiZgwR3dKMygtA4mG1Q=="], + + "micromark-util-combine-extensions": ["micromark-util-combine-extensions@2.0.1", "", { "dependencies": { "micromark-util-chunked": "^2.0.0", "micromark-util-types": "^2.0.0" } }, "sha512-OnAnH8Ujmy59JcyZw8JSbK9cGpdVY44NKgSM7E9Eh7DiLS2E9RNQf0dONaGDzEG9yjEl5hcqeIsj4hfRkLH/Bg=="], + + "micromark-util-decode-numeric-character-reference": ["micromark-util-decode-numeric-character-reference@2.0.2", "", { "dependencies": { "micromark-util-symbol": "^2.0.0" } }, "sha512-ccUbYk6CwVdkmCQMyr64dXz42EfHGkPQlBj5p7YVGzq8I7CtjXZJrubAYezf7Rp+bjPseiROqe7G6foFd+lEuw=="], + + "micromark-util-decode-string": ["micromark-util-decode-string@2.0.1", "", { "dependencies": { "decode-named-character-reference": "^1.0.0", "micromark-util-character": "^2.0.0", "micromark-util-decode-numeric-character-reference": "^2.0.0", "micromark-util-symbol": "^2.0.0" } }, "sha512-nDV/77Fj6eH1ynwscYTOsbK7rR//Uj0bZXBwJZRfaLEJ1iGBR6kIfNmlNqaqJf649EP0F3NWNdeJi03elllNUQ=="], + + "micromark-util-edit-map": ["micromark-util-edit-map@1.0.0", "", { "dependencies": { "micromark-util-types": "^2.0.0" } }, "sha512-Pa2ljlsEL6sVwFaYeyrOLSYbQt73JvGbPOYFq+9AElXiSnfI5Q4465RRZ1sHv7lDjDOnGRDDaqPXqClqWK/q1Q=="], + + "micromark-util-encode": ["micromark-util-encode@2.0.1", "", {}, "sha512-c3cVx2y4KqUnwopcO9b/SCdo2O67LwJJ/UyqGfbigahfegL9myoEFoDYZgkT7f36T0bLrM9hZTAaAyH+PCAXjw=="], + + "micromark-util-html-tag-name": ["micromark-util-html-tag-name@2.0.1", "", {}, "sha512-2cNEiYDhCWKI+Gs9T0Tiysk136SnR13hhO8yW6BGNyhOC4qYFnwF1nKfD3HFAIXA5c45RrIG1ub11GiXeYd1xA=="], + + "micromark-util-normalize-identifier": ["micromark-util-normalize-identifier@2.0.1", "", { "dependencies": { "micromark-util-symbol": "^2.0.0" } }, "sha512-sxPqmo70LyARJs0w2UclACPUUEqltCkJ6PhKdMIDuJ3gSf/Q+/GIe3WKl0Ijb/GyH9lOpUkRAO2wp0GVkLvS9Q=="], + + "micromark-util-resolve-all": ["micromark-util-resolve-all@2.0.1", "", { "dependencies": { "micromark-util-types": "^2.0.0" } }, "sha512-VdQyxFWFT2/FGJgwQnJYbe1jjQoNTS4RjglmSjTUlpUMa95Htx9NHeYW4rGDJzbjvCsl9eLjMQwGeElsqmzcHg=="], + + "micromark-util-sanitize-uri": ["micromark-util-sanitize-uri@2.0.1", "", { "dependencies": { "micromark-util-character": "^2.0.0", "micromark-util-encode": "^2.0.0", "micromark-util-symbol": "^2.0.0" } }, "sha512-9N9IomZ/YuGGZZmQec1MbgxtlgougxTodVwDzzEouPKo3qFWvymFHWcnDi2vzV1ff6kas9ucW+o3yzJK9YB1AQ=="], + + "micromark-util-subtokenize": ["micromark-util-subtokenize@2.1.0", "", { "dependencies": { "devlop": "^1.0.0", "micromark-util-chunked": "^2.0.0", "micromark-util-symbol": "^2.0.0", "micromark-util-types": "^2.0.0" } }, "sha512-XQLu552iSctvnEcgXw6+Sx75GflAPNED1qx7eBJ+wydBb2KCbRZe+NwvIEEMM83uml1+2WSXpBAcp9IUCgCYWA=="], + + "micromark-util-symbol": ["micromark-util-symbol@2.0.1", "", {}, "sha512-vs5t8Apaud9N28kgCrRUdEed4UJ+wWNvicHLPxCa9ENlYuAY31M0ETy5y1vA33YoNPDFTghEbnh6efaE8h4x0Q=="], + + "micromark-util-types": ["micromark-util-types@2.0.3", "", {}, "sha512-oxB2Ik03hI0gv+VNn9tnh1t1YEe9MDPptViAEgfdf3YQHsn0pzGTgCdlSCJXcwhqm8phaEuM7zeEu3QQzVBrPg=="], + "mime-db": ["mime-db@1.52.0", "", {}, "sha512-sPU4uV7dYlvtWJxwwxHD0PuihVNiE7TyAbQ5SWxDCB9mUYvOgroQOwYQQOKPJ8CIbE+1ETVlOoK1UC2nU3gYvg=="], "mime-types": ["mime-types@2.1.35", "", { "dependencies": { "mime-db": "1.52.0" } }, "sha512-ZDY+bPm5zTTF+YpCrAU9nK0UgICYPT0QtT1NZWFv4s++TNkcgVaT0g6+4R2uI4MjQjzysHB1zxuWL50hzaeXiw=="], @@ -779,6 +898,8 @@ "open": ["open@10.1.2", "", { "dependencies": { "default-browser": "^5.2.1", "define-lazy-prop": "^3.0.0", "is-inside-container": "^1.0.0", "is-wsl": "^3.1.0" } }, "sha512-cxN6aIDPz6rm8hbebcP7vrQNhvRcveZoJU72Y7vskh4oIm+BZwBECnx5nTmrlres1Qapvx27Qo1Auukpf8PKXw=="], + "parse-entities": ["parse-entities@4.0.2", "", { "dependencies": { "@types/unist": "^2.0.0", "character-entities-legacy": "^3.0.0", "character-reference-invalid": "^2.0.0", "decode-named-character-reference": "^1.0.0", "is-alphanumerical": "^2.0.0", "is-decimal": "^2.0.0", "is-hexadecimal": "^2.0.0" } }, "sha512-GG2AQYWoLgL877gQIKeRPGO1xF9+eG1ujIb5soS5gPvLQ1y2o8FL90w2QWNdf9I361Mpp7726c+lj3U0qK1uGw=="], + "pathe": ["pathe@1.1.2", "", {}, "sha512-whLdWMYL2TwI08hn8/ZqAbrVemu0LNaNNJZX73O6qaIdCTfXutsLhMkjdENX0qhsQ9uIimo4/aQOmXkoon2nDQ=="], "perfect-debounce": ["perfect-debounce@1.0.0", "", {}, "sha512-xCy9V055GLEqoFaHoC1SoLIaLmWctgCUaBaWxDZ7/Zx4CTyX7cJQLJOok/orfjZAh9kEYpjJa4d0KcJmCbctZA=="], @@ -799,6 +920,8 @@ "prop-types": ["prop-types@15.8.1", "", { "dependencies": { "loose-envify": "^1.4.0", "object-assign": "^4.1.1", "react-is": "^16.13.1" } }, "sha512-oj87CgZICdulUohogVAR7AjlC0327U4el4L6eAvOqCeudMDVU0NThNaV+b9Df4dXgSP1gXMTnPdhfe/2qDH5cg=="], + "property-information": ["property-information@7.2.0", "", {}, "sha512-IAtzIB6sUiWaJYrX9smp3V46pBGbBeLFRGdh25kg1334VcBlD8HzhPeNIWQH9zhGmo2itIe25EHt9dQP7G5hmg=="], + "proxy-from-env": ["proxy-from-env@2.1.0", "", {}, "sha512-cJ+oHTW1VAEa8cJslgmUZrc+sjRKgAKl3Zyse6+PV38hZe/V6Z14TbCuXcan9F9ghlz4QrFr2c92TNF82UkYHA=="], "radix-ui": ["radix-ui@1.6.7", "", { "dependencies": { "@radix-ui/primitive": "1.1.7", "@radix-ui/react-accessible-icon": "1.1.15", "@radix-ui/react-accordion": "1.2.20", "@radix-ui/react-alert-dialog": "1.1.23", "@radix-ui/react-arrow": "1.1.15", "@radix-ui/react-aspect-ratio": "1.1.15", "@radix-ui/react-avatar": "1.2.6", "@radix-ui/react-checkbox": "1.3.11", "@radix-ui/react-collapsible": "1.1.20", "@radix-ui/react-collection": "1.1.15", "@radix-ui/react-compose-refs": "1.1.5", "@radix-ui/react-context": "1.2.2", "@radix-ui/react-context-menu": "2.3.7", "@radix-ui/react-dialog": "1.1.23", "@radix-ui/react-direction": "1.1.4", "@radix-ui/react-dismissable-layer": "1.1.19", "@radix-ui/react-dropdown-menu": "2.1.24", "@radix-ui/react-focus-guards": "1.1.6", "@radix-ui/react-focus-scope": "1.1.16", "@radix-ui/react-form": "0.1.16", "@radix-ui/react-hover-card": "1.1.23", "@radix-ui/react-label": "2.1.15", "@radix-ui/react-menu": "2.1.24", "@radix-ui/react-menubar": "1.1.24", "@radix-ui/react-navigation-menu": "1.2.22", "@radix-ui/react-one-time-password-field": "0.1.16", "@radix-ui/react-password-toggle-field": "0.1.11", "@radix-ui/react-popover": "1.1.23", "@radix-ui/react-popper": "1.3.7", "@radix-ui/react-portal": "1.1.17", "@radix-ui/react-presence": "1.1.10", "@radix-ui/react-primitive": "2.1.10", "@radix-ui/react-progress": "1.1.16", "@radix-ui/react-radio-group": "1.4.7", "@radix-ui/react-roving-focus": "1.1.19", "@radix-ui/react-scroll-area": "1.2.18", "@radix-ui/react-select": "2.3.7", "@radix-ui/react-separator": "1.1.15", "@radix-ui/react-slider": "1.4.7", "@radix-ui/react-slot": "1.3.3", "@radix-ui/react-switch": "1.3.7", "@radix-ui/react-tabs": "1.1.21", "@radix-ui/react-toast": "1.2.23", "@radix-ui/react-toggle": "1.1.18", "@radix-ui/react-toggle-group": "1.1.19", "@radix-ui/react-toolbar": "1.1.19", "@radix-ui/react-tooltip": "1.2.16", "@radix-ui/react-use-callback-ref": "1.1.4", "@radix-ui/react-use-controllable-state": "1.2.6", "@radix-ui/react-use-effect-event": "0.0.5", "@radix-ui/react-use-escape-keydown": "1.1.5", "@radix-ui/react-use-is-hydrated": "0.1.3", "@radix-ui/react-use-layout-effect": "1.1.4", "@radix-ui/react-use-size": "1.1.4", "@radix-ui/react-visually-hidden": "1.2.11" }, "peerDependencies": { "@types/react": "*", "@types/react-dom": "*", "react": "^16.8 || ^17.0 || ^18.0 || ^19.0 || ^19.0.0-rc", "react-dom": "^16.8 || ^17.0 || ^18.0 || ^19.0 || ^19.0.0-rc" }, "optionalPeers": ["@types/react", "@types/react-dom"] }, "sha512-QBdhh1arIEUvPC0dQ5+nwWAxt7+N+oP/9jPwjJkGFoSk/sqxg32gJtSXGtFh8frAIcS6oC9cx2Q+7KYCQLOAeA=="], @@ -821,6 +944,8 @@ "react-is": ["react-is@16.13.1", "", {}, "sha512-24e6ynE2H+OKt4kqsOvNd8kBpV65zoxbA4BVsEOB3ARVWQki/DHzaUoC5KuON/BiccDaCCTZBuOcfZs70kR8bQ=="], + "react-markdown": ["react-markdown@10.1.0", "", { "dependencies": { "@types/hast": "^3.0.0", "@types/mdast": "^4.0.0", "devlop": "^1.0.0", "hast-util-to-jsx-runtime": "^2.0.0", "html-url-attributes": "^3.0.0", "mdast-util-to-hast": "^13.0.0", "remark-parse": "^11.0.0", "remark-rehype": "^11.0.0", "unified": "^11.0.0", "unist-util-visit": "^5.0.0", "vfile": "^6.0.0" }, "peerDependencies": { "@types/react": ">=18", "react": ">=18" } }, "sha512-qKxVopLT/TyA6BX3Ue5NwabOsAzm0Q7kAPwq6L+wWDwisYs7R8vZ0nRXqq6rkueboxpkjvLGU9fWifiX/ZZFxQ=="], + "react-remove-scroll": ["react-remove-scroll@2.7.2", "", { "dependencies": { "react-remove-scroll-bar": "^2.3.7", "react-style-singleton": "^2.2.3", "tslib": "^2.1.0", "use-callback-ref": "^1.3.3", "use-sidecar": "^1.1.3" }, "peerDependencies": { "@types/react": "*", "react": "^16.8.0 || ^17.0.0 || ^18.0.0 || ^19.0.0 || ^19.0.0-rc" } }, "sha512-Iqb9NjCCTt6Hf+vOdNIZGdTiH1QSqr27H/Ek9sv/a97gfueI/5h1s3yRi1nngzMUaOOToin5dI1dXKdXiF+u0Q=="], "react-remove-scroll-bar": ["react-remove-scroll-bar@2.3.8", "", { "dependencies": { "react-style-singleton": "^2.2.2", "tslib": "^2.0.0" }, "peerDependencies": { "@types/react": "*", "react": "^16.8.0 || ^17.0.0 || ^18.0.0 || ^19.0.0" } }, "sha512-9r+yi9+mgU33AKcj6IbT9oRCO78WriSj6t/cF8DWBZJ9aOGPOTEDvdUDz1FwKim7QXWwmHqtdHnRJfhAxEG46Q=="], @@ -831,6 +956,10 @@ "readdirp": ["readdirp@5.1.1", "", {}, "sha512-Kko+Y5XQ6fM+Ce3dq3m9YGxnacYZYl9cA1wZjaF3Vbry2L3i1qVg8+CAgNPsXRArPMUMCaOR7oa9Nqntc43JKA=="], + "remark-parse": ["remark-parse@11.0.0", "", { "dependencies": { "@types/mdast": "^4.0.0", "mdast-util-from-markdown": "^2.0.0", "micromark-util-types": "^2.0.0", "unified": "^11.0.0" } }, "sha512-FCxlKLNGknS5ba/1lmpYijMUzX2esxW5xQqjWxw2eHFfS2MSdaHVINFmhjo+qN1WhZhNimq0dZATN9pH0IDrpA=="], + + "remark-rehype": ["remark-rehype@11.1.2", "", { "dependencies": { "@types/hast": "^3.0.0", "@types/mdast": "^4.0.0", "mdast-util-to-hast": "^13.0.0", "unified": "^11.0.0", "vfile": "^6.0.0" } }, "sha512-Dh7l57ianaEoIpzbp0PC9UKAdCSVklD8E5Rpw7ETfbTl3FqcOOgq5q2LVDhgGCkaBv7p24JXikPdvhhmHvKMsw=="], + "resize-observer-polyfill": ["resize-observer-polyfill@1.5.1", "", {}, "sha512-LwZrotdHOo12nQuZlHEmtuXdqGoOD0OhaxopaNFxWzInpEgaLWoVuAMbTzixuosCx2nEG58ngzW3vxdWoxIgdg=="], "rollup": ["rollup@4.64.0", "", { "dependencies": { "@types/estree": "1.0.9" }, "optionalDependencies": { "@napi-rs/lzma-linux-x64-gnu": "1.5.1", "@rollup/rollup-android-arm-eabi": "4.64.0", "@rollup/rollup-android-arm64": "4.64.0", "@rollup/rollup-darwin-arm64": "4.64.0", "@rollup/rollup-darwin-x64": "4.64.0", "@rollup/rollup-freebsd-arm64": "4.64.0", "@rollup/rollup-freebsd-x64": "4.64.0", "@rollup/rollup-linux-arm-gnueabihf": "4.64.0", "@rollup/rollup-linux-arm-musleabihf": "4.64.0", "@rollup/rollup-linux-arm64-gnu": "4.64.0", "@rollup/rollup-linux-arm64-musl": "4.64.0", "@rollup/rollup-linux-loong64-gnu": "4.64.0", "@rollup/rollup-linux-loong64-musl": "4.64.0", "@rollup/rollup-linux-ppc64-gnu": "4.64.0", "@rollup/rollup-linux-ppc64-musl": "4.64.0", "@rollup/rollup-linux-riscv64-gnu": "4.64.0", "@rollup/rollup-linux-riscv64-musl": "4.64.0", "@rollup/rollup-linux-s390x-gnu": "4.64.0", "@rollup/rollup-linux-x64-gnu": "4.64.0", "@rollup/rollup-linux-x64-musl": "4.64.0", "@rollup/rollup-openbsd-x64": "4.64.0", "@rollup/rollup-openharmony-arm64": "4.64.0", "@rollup/rollup-win32-arm64-msvc": "4.64.0", "@rollup/rollup-win32-ia32-msvc": "4.64.0", "@rollup/rollup-win32-x64-gnu": "4.64.0", "@rollup/rollup-win32-x64-msvc": "4.64.0", "fsevents": "~2.3.2" }, "bin": { "rollup": "dist/bin/rollup" } }, "sha512-gjA/kFeDfffII8COoPsvLPvE16hWDTEc1+0QKQNKUkuV8iJjqsC4xXs8/FzuL0v9EV39kuxTbAgcFoTRyy2TGQ=="], @@ -849,8 +978,16 @@ "source-map-js": ["source-map-js@1.2.2", "", {}, "sha512-KGj/8Y43x35aZVDtt+J4mK1hoLGHULMYfSkODJNQjNDC3oW1PqPoxMwo0pLUsWM/UEGzON/NxeHywEfNXNP3Vw=="], + "space-separated-tokens": ["space-separated-tokens@2.0.2", "", {}, "sha512-PEGlAwrG8yXGXRjW32fGbg66JAlOAwbObuqVoJpv/mRgoWDQfgH1wDPvtzWyUSNAXBGSk8h755YDbbcEy3SH2Q=="], + "state-local": ["state-local@1.0.7", "", {}, "sha512-HTEHMNieakEnoe33shBYcZ7NX83ACUjCu8c40iOGEZsngj9zRnkqS9j1pqQPXwobB0ZcVTk27REb7COQ0UR59w=="], + "stringify-entities": ["stringify-entities@4.0.4", "", { "dependencies": { "character-entities-html4": "^2.0.0", "character-entities-legacy": "^3.0.0" } }, "sha512-IwfBptatlO+QCJUo19AqvrPNqlVMpW9YEL2LIVY+Rpv2qsjCGxaDLNRgeGsQWJhfItebuJhsGSLjaBbNSQ+ieg=="], + + "style-to-js": ["style-to-js@1.1.21", "", { "dependencies": { "style-to-object": "1.0.14" } }, "sha512-RjQetxJrrUJLQPHbLku6U/ocGtzyjbJMP9lCNK7Ag0CNh690nSH8woqWH9u16nMjYBAok+i7JO1NP2pOy8IsPQ=="], + + "style-to-object": ["style-to-object@1.0.14", "", { "dependencies": { "inline-style-parser": "0.2.7" } }, "sha512-LIN7rULI0jBscWQYaSswptyderlarFkjQ+t79nzty8tcIAceVomEVlLzH5VP4Cmsv6MtKhs7qaAiwlcp+Mgaxw=="], + "tailwind-merge": ["tailwind-merge@3.4.0", "", {}, "sha512-uSaO4gnW+b3Y2aWoWfFpX62vn2sR3skfhbjsEnaBI81WD1wBLlHZe5sWf0AqjksNdYTbGBEd0UasQMT3SNV15g=="], "tailwindcss": ["tailwindcss@4.3.3", "", {}, "sha512-gOhV3P7ufE62QDGg1zVaTgCR+EtPv92k2nIhVcVKcLmxT1sUBsQGhnZj175j+MqRt4zLF7ic+sCYjfhxMxj7YQ=="], @@ -863,6 +1000,10 @@ "tinyglobby": ["tinyglobby@0.2.15", "", { "dependencies": { "fdir": "^6.5.0", "picomatch": "^4.0.3" } }, "sha512-j2Zq4NyQYG5XMST4cbs02Ak8iJUdxRM0XI5QyxXuZOzKOINmWurp3smXu3y5wDcJrptwpSjgXHzIQxR0omXljQ=="], + "trim-lines": ["trim-lines@3.0.1", "", {}, "sha512-kRj8B+YHZCc9kQYdWfJB2/oUl9rA99qbowYYBtr4ui4mZyAQ2JpvVBd/6U2YloATfqBhBTSMhTpgBHtU0Mf3Rg=="], + + "trough": ["trough@2.2.0", "", {}, "sha512-tmMpK00BjZiUyVyvrBK7knerNgmgvcV/KLVyuma/SC+TQN167GrMRciANTz09+k3zW8L8t60jWO1GpfkZdjTaw=="], + "tslib": ["tslib@2.8.1", "", {}, "sha512-oJFu94HQb+KVduSUQL7wnpmqnfmLsOA/nAh6b6EH0wCEoK0/mPeXU6c3wKDV83MkOuHPRHtSXKKU99IBazS/2w=="], "tw-animate-css": ["tw-animate-css@1.4.0", "", {}, "sha512-7bziOlRqH0hJx80h/3mbicLW7o8qLsH5+RaLR2t+OHM3D0JlWGODQKQ4cxbK7WlvmUxpcj6Kgu6EKqjrGFe3QQ=="], @@ -875,6 +1016,18 @@ "undici-types": ["undici-types@7.16.0", "", {}, "sha512-Zz+aZWSj8LE6zoxD+xrjh4VfkIG8Ya6LvYkZqtUQGJPZjYl53ypCaUwWqo7eI0x66KBGeRo+mlBEkMSeSZ38Nw=="], + "unified": ["unified@11.0.5", "", { "dependencies": { "@types/unist": "^3.0.0", "bail": "^2.0.0", "devlop": "^1.0.0", "extend": "^3.0.0", "is-plain-obj": "^4.0.0", "trough": "^2.0.0", "vfile": "^6.0.0" } }, "sha512-xKvGhPWw3k84Qjh8bI3ZeJjqnyadK+GEFtazSfZv/rKeTkTjOJho6mFqh2SM96iIcZokxiOpg78GazTSg8+KHA=="], + + "unist-util-is": ["unist-util-is@6.0.1", "", { "dependencies": { "@types/unist": "^3.0.0" } }, "sha512-LsiILbtBETkDz8I9p1dQ0uyRUWuaQzd/cuEeS1hoRSyW5E5XGmTzlwY1OrNzzakGowI9Dr/I8HVaw4hTtnxy8g=="], + + "unist-util-position": ["unist-util-position@5.0.0", "", { "dependencies": { "@types/unist": "^3.0.0" } }, "sha512-fucsC7HjXvkB5R3kTCO7kUjRdrS0BJt3M/FPxmHMBOm8JQi2BsHAHFsy27E0EolP8rp0NzXsJ+jNPyDWvOJZPA=="], + + "unist-util-stringify-position": ["unist-util-stringify-position@4.0.0", "", { "dependencies": { "@types/unist": "^3.0.0" } }, "sha512-0ASV06AAoKCDkS2+xw5RXJywruurpbC4JZSm7nr7MOt1ojAzvyyaO+UxZf18j8FCF6kmzCZKcAgN/yu2gm2XgQ=="], + + "unist-util-visit": ["unist-util-visit@5.1.0", "", { "dependencies": { "@types/unist": "^3.0.0", "unist-util-is": "^6.0.0", "unist-util-visit-parents": "^6.0.0" } }, "sha512-m+vIdyeCOpdr/QeQCu2EzxX/ohgS8KbnPDgFni4dQsfSCtpz8UqDyY5GjRru8PDKuYn7Fq19j1CQ+nJSsGKOzg=="], + + "unist-util-visit-parents": ["unist-util-visit-parents@6.0.2", "", { "dependencies": { "@types/unist": "^3.0.0", "unist-util-is": "^6.0.0" } }, "sha512-goh1s1TBrqSqukSc8wrjwWhL0hiJxgA8m4kFxGlQ+8FYQ3C/m11FcTs4YYem7V664AhHVvgoQLk890Ssdsr2IQ=="], + "unplugin": ["unplugin@3.4.0", "", { "dependencies": { "@jridgewell/remapping": "^2.3.5", "picomatch": "^4.0.7", "webpack-virtual-modules": "^0.6.2" }, "peerDependencies": { "@farmfe/core": "*", "@rsbuild/core": "*", "@rspack/core": "*", "bun-types-no-globals": "*", "esbuild": "*", "rolldown": "*", "rollup": "*", "unloader": "*", "vite": "*", "webpack": "*" }, "optionalPeers": ["@farmfe/core", "@rsbuild/core", "@rspack/core", "bun-types-no-globals", "esbuild", "rolldown", "rollup", "unloader", "vite", "webpack"] }, "sha512-9skdIFlCsPdFV7wUfZxNsFInlW+7nJmGu2gkTu0OUhF56aXGsHab9x52/QhdJ4lC7ZDPWTxbiC4ANqVlsuaW3w=="], "update-browserslist-db": ["update-browserslist-db@1.3.3", "", { "dependencies": { "escalade": "^3.2.0", "picocolors": "^1.1.1" }, "peerDependencies": { "browserslist": ">= 4.21.0" }, "bin": { "update-browserslist-db": "cli.js" } }, "sha512-pJ2sYawQS0R/WI928Gj5GlPhTGzbMelq0+4INtSYNDV9ErKJcX6xjGWkoG/VnB3dpUm00zALaqkrUD77pO5TDQ=="], @@ -887,6 +1040,10 @@ "use-sync-external-store": ["use-sync-external-store@1.6.0", "", { "peerDependencies": { "react": "^16.8.0 || ^17.0.0 || ^18.0.0 || ^19.0.0" } }, "sha512-Pp6GSwGP/NrPIrxVFAIkOQeyw8lFenOHijQWkUTrDvrF4ALqylP2C/KCkeS9dpUM3KvYRQhna5vt7IL95+ZQ9w=="], + "vfile": ["vfile@6.0.3", "", { "dependencies": { "@types/unist": "^3.0.0", "vfile-message": "^4.0.0" } }, "sha512-KzIbH/9tXat2u30jf+smMwFCsno4wHVdNmzFyL+T/L3UGqqk6JKfVqOFOZEpZSHADH1k40ab6NUIXZq422ov3Q=="], + + "vfile-message": ["vfile-message@4.0.3", "", { "dependencies": { "@types/unist": "^3.0.0", "unist-util-stringify-position": "^4.0.0" } }, "sha512-QTHzsGd1EhbZs4AsQ20JX1rC3cOlt/IWJruk893DfLRr57lcnOeMaWG4K0JrRta4mIJZKth2Au3mM3u03/JWKw=="], + "vite": ["vite@7.3.7", "", { "dependencies": { "esbuild": "^0.27.0 || ^0.28.0", "fdir": "^6.5.0", "picomatch": "^4.0.3", "postcss": "^8.5.6", "rollup": "^4.43.0", "tinyglobby": "^0.2.15" }, "optionalDependencies": { "fsevents": "~2.3.3" }, "peerDependencies": { "@types/node": "^20.19.0 || >=22.12.0", "jiti": ">=1.21.0", "less": "^4.0.0", "lightningcss": "^1.21.0", "sass": "^1.70.0", "sass-embedded": "^1.70.0", "stylus": ">=0.54.8", "sugarss": "^5.0.0", "terser": "^5.16.0", "tsx": "^4.8.1", "yaml": "^2.4.2" }, "optionalPeers": ["@types/node", "jiti", "less", "lightningcss", "sass", "sass-embedded", "stylus", "sugarss", "terser", "tsx", "yaml"], "bin": { "vite": "bin/vite.js" } }, "sha512-AHEzTORMbi7/7aXp7w7KZ5+DCUdSeLsJz0grY8xHpc9DHdYb0vQu6DqJfm2tOw3gDExNgPgnJTXxM6syYFHaCA=="], "webpack-virtual-modules": ["webpack-virtual-modules@0.6.2", "", {}, "sha512-66/V2i5hQanC51vBQKPH4aI8NMAcBW59FVBs+rC7eGHupMyfn34q7rZIE+ETlJ+XTevqfUhVVBgSUNSW2flEUQ=="], @@ -899,6 +1056,8 @@ "zustand": ["zustand@4.5.7", "", { "dependencies": { "use-sync-external-store": "^1.2.2" }, "peerDependencies": { "@types/react": ">=16.8", "immer": ">=9.0.6", "react": ">=16.8" }, "optionalPeers": ["@types/react", "immer", "react"] }, "sha512-CHOUy7mu3lbD6o6LJLfllpjkzhHXSBlX8B9+qPddUsIfeF5S/UZ5q0kmCsnRqT1UHFQZchNFDDzMbQsuesHWlw=="], + "zwitch": ["zwitch@2.0.4", "", {}, "sha512-bXE4cR/kVZhKZX/RjPEflHaKVhUVl85noU3v6b8apfQEc1x4A+zBxjZ4lN8LqGd6WZ3dl98pY4o717VFmoPp+A=="], + "@tailwindcss/node/jiti": ["jiti@2.7.0", "", { "bin": { "jiti": "lib/jiti-cli.mjs" } }, "sha512-AC/7JofJvZGrrneWNaEnJeOLUx+JlGt7tNa0wZiRPT4MY1wmfKjt2+6O2p2uz2+skll8OZZmJMNqeke7kKbNgQ=="], "@tailwindcss/oxide-wasm32-wasi/@emnapi/core": ["@emnapi/core@1.11.3", "", { "dependencies": { "@emnapi/wasi-threads": "1.2.3", "tslib": "^2.4.0" }, "bundled": true }, "sha512-zLpS5asjEb7lq8jYLq37N6XKaE41DIexlY1rF/z4/tIl3wo13Sqm28fRyfIsKZD+NZ8mM5RoKkpW/rBcuoSZSg=="], @@ -947,6 +1106,8 @@ "nypm/pathe": ["pathe@2.0.3", "", {}, "sha512-WUjGcAqP1gQacoQe+OBJsFA7Ld4DyXuUIjZ5cc75cLHvJ7dtNsTugphxIADwspS+AraAUePCKrSVtPLFj/F88w=="], + "parse-entities/@types/unist": ["@types/unist@2.0.11", "", {}, "sha512-CmBKiL6NNo/OqgmMn95Fk9Whlp2mtvIv+KNpQKN2F4SjvrEesubTRWGYSg+BnWZOnlCaSTU1sMpsBOzgbYhnsA=="], + "pkg-types/pathe": ["pathe@2.0.3", "", {}, "sha512-WUjGcAqP1gQacoQe+OBJsFA7Ld4DyXuUIjZ5cc75cLHvJ7dtNsTugphxIADwspS+AraAUePCKrSVtPLFj/F88w=="], "playwright/fsevents": ["fsevents@2.3.2", "", { "os": "darwin" }, "sha512-xiqMQR4xAeHTuB9uWm+fFRcIOgKBMiOBP+eXiyT7jsgVCq1bkVygt00oASowB7EdtpOHaaPgKt812P9ab+DDKA=="], diff --git a/development.md b/development.md index 01348e7..797e7a5 100644 --- a/development.md +++ b/development.md @@ -66,9 +66,10 @@ Standalone (`cd app && make dev`) publishes the full set: ## Hot reload, and what it misses -`compose.dev.yml` bind-mounts `backend/fluksio` into the api container and runs uvicorn with -`--reload`, so a backend edit is live. The `develop.watch` block next to it syncs the same -directory, but only under `docker compose watch`, which no target runs — the detached +`compose.dev.yml` bind-mounts `backend/fluksio` and `worker/fluksio_worker` into the api +container and runs uvicorn with `--reload` over both, so a backend edit is live and a worker +edit restarts the engine along with its local workers. The `develop.watch` block next to it +syncs the same directories, but only under `docker compose watch`, which no target runs — the detached `up -d` flow never triggers it. The mount is what does the work, so an api container created before the mount existed keeps serving the source baked into its image and has to be recreated once. @@ -128,18 +129,30 @@ SQLite file in a temp directory (deleted per session, so a run never touches dev data), its own superuser, and `ENVIRONMENT=local` with `DOMAIN=localhost`, so a checkout configured for a deployment cannot drag that deployment's configuration into the run. -**Frontend (e2e).** `make test-frontend` runs the suite inside the pinned -`mcr.microsoft.com/playwright:v-noble` image (version read from -`frontend/package.json`) on the `proxy` network, against a stack that must already be up. It -maps `app.$(DOMAIN)` and `api.$(DOMAIN)` onto the Traefik container's address twice, with -`--add-host` and with `HOST_RESOLVER_RULES`, because Chromium pins `*.localhost` to loopback -whatever `/etc/hosts` says. Narrow a run with +**Frontend (e2e).** `make test-frontend` runs the suite against a stack of its own, because +the suite creates and deletes flows, dashboards and users. `make test-stack`, which it runs +first, starts or updates the compose project `fluksio-test` (`docker/compose.test.yml` over the +local stack's files), with its own containers, volumes and image tags. The local Traefik serves +it on `app.test.localhost` and `api.test.localhost`, so `make dev` has to be up, in either +mode: the test stack never layers `compose.lan.yml` and publishes no port. It stays running +after a run for a look at a failure, `make down` stops it, and its volumes are expendable +(`docker compose -p fluksio-test down -v` starts it over). + +The suite runs inside the pinned `mcr.microsoft.com/playwright:v-noble` image +(version read from `frontend/package.json`) on the `proxy` network. It maps both names onto +the Traefik container's address twice, with `--add-host` and with `HOST_RESOLVER_RULES`, +because Chromium pins `*.localhost` to loopback whatever `/etc/hosts` says. Narrow a run with `make test-frontend PLAYWRIGHT_ARGS="--grep flows"`. +`make test-webkit` runs the drag specs in WebKit against the same stack. WebKit takes +`*.localhost` to loopback through GLib and ignores `/etc/hosts`, so that container shares +Traefik's network namespace, where loopback is Traefik. Playwright's Linux WebKit reads a +`writing-mode: vertical-lr` range across, so a vertical slider's geometry is not asserted there. + The specs are `frontend/tests/*.spec.ts`; `playwright.config.ts` defines `setup` → -`chromium` and `mobile`. `auth.setup.ts` logs in once into `playwright/.auth/user.json`, -which both browser projects reuse; `mobile` runs `mobile.spec.ts` only, on a Pixel 5 at -393px. +`chromium` and `mobile`, plus `webkit` when `PLAYWRIGHT_WEBKIT=1` is set. `auth.setup.ts` logs +in once into `playwright/.auth/user.json`, which the browser projects reuse; `mobile` runs +`mobile.spec.ts` only, on a Pixel 5 at 393px. Both origins come from `PLAYWRIGHT_BASE_URL` and `PLAYWRIGHT_API_URL`, deliberately never from `VITE_API_URL` — that one belongs to the app build and in a deployment checkout it @@ -152,8 +165,8 @@ creates and deletes flows, dashboards and users. `PLAYWRIGHT_ALLOW_PUBLIC=1` ove `app/frontend/screenshots/{light,dark}/`. It is an alias for `make verify-docker`: the run always happens in the Playwright container, which pins the browser by reading `frontend/package.json`. Running Playwright on the host is unsupported — the workspace pins a -browser revision whose install is incomplete. The domain comes from `.env`, and -`make verify DOMAIN=example.com` still wins. +browser revision whose install is incomplete. The domain comes off the running stack (the +frontend container's Traefik rule), so the shots are of whatever `make dev` last started. ## Lint, hooks and the generated client @@ -171,8 +184,9 @@ contract between the two frontends, and that target is the only thing checking i ## CI -`.gitea/workflows/` runs four jobs, each writing `.env` from `.env.example` first: -`test-backend.yml` (pytest, plus a check that both wheels build), `playwright.yml` (the e2e suite in +`.gitea/workflows/` runs four jobs: +`test-backend.yml` (pytest on Python 3.12 and 3.14, plus a check that both wheels build), `playwright.yml` (the e2e suite in two shards, one compose project each, the browser loading the SPA from the nginx `frontend` service), `pre-commit.yml` (the hooks above, plus strict mypy, which they do not cover) and -`test-compose.yml` (a smoke test that the production images actually come up). +`test-compose.yml` (a smoke test that the production images actually come up). All but +`test-backend.yml` write `.env` from `.env.example` first. The backend suite needs none. diff --git a/docker/compose.dev.yml b/docker/compose.dev.yml index e9fa1e7..a482a9d 100644 --- a/docker/compose.dev.yml +++ b/docker/compose.dev.yml @@ -8,7 +8,9 @@ services: # on http://api.localhost / http://app.localhost via the routers in # compose.yml. Dashboard at http://localhost:8090. proxy: - image: traefik:3.6 + # A patch tag, so a host holding an older cached 3.6 pulls this one. 3.6.25 + # knows --accesslog.fields.queryparameters, which older 3.6 refuses. + image: traefik:3.6.25 volumes: - /var/run/docker.sock:/var/run/docker.sock ports: @@ -92,10 +94,18 @@ services: build: context: .. dockerfile: backend/Dockerfile + # `fastapi dev`, because `run` takes no `--reload-dir`. Watching the worker + # too makes a worker edit restart the engine, which respawns the local + # worker pool with the new code. command: - fastapi - - run - - --reload + - dev + - --host + - 0.0.0.0 + - --reload-dir + - /app/backend + - --reload-dir + - /app/worker - "fluksio/main.py" develop: watch: @@ -107,6 +117,9 @@ services: - .venv - path: ../backend/pyproject.toml action: rebuild + - path: ../worker/fluksio_worker + action: sync + target: /app/worker/fluksio_worker volumes: # What makes --reload above mean anything in the detached flow (`make dev`, # `make dev-local`): it starts the stack with `up -d`, which never runs the diff --git a/docker/compose.lan.yml b/docker/compose.lan.yml index 6dcea53..5b2eba6 100644 --- a/docker/compose.lan.yml +++ b/docker/compose.lan.yml @@ -3,7 +3,8 @@ # For reaching the app from a machine that cannot be given a DNS record or a # hosts entry. Traefik routes on the Host header and a bare IP matches no # router, so the frontend is published on a host port instead and proxies the -# API on that same port: http://:${APP_PORT} is the whole app. +# API and the hooks on that same port: http://:${APP_PORT} is the +# whole app. services: frontend: diff --git a/docker/compose.test.yml b/docker/compose.test.yml new file mode 100644 index 0000000..9ad86d3 --- /dev/null +++ b/docker/compose.test.yml @@ -0,0 +1,23 @@ +# The frontend suite's own stack. `make test-stack` layers this on +# compose.local.yml under the project `fluksio-test`, with DOMAIN=test.localhost +# and STACK_NAME=fluksio-test. It shares the dev stack's Traefik and nothing +# else: its own containers, volumes, image tags and hostnames, so the flows, +# dashboards and users the suite creates never reach the dev instance's data. +services: + + # `container_name` ignores the project name, so the dev stack's names would + # collide. + redis: + container_name: !reset null + + backend: + container_name: !reset null + # compose.dev.yml also brings up the broker and the time-series database + # with it, which no spec talks to. + depends_on: !override + redis: + condition: service_healthy + restart: true + + frontend: + container_name: !reset null diff --git a/docker/compose.traefik.yml b/docker/compose.traefik.yml index 0353564..541c80b 100644 --- a/docker/compose.traefik.yml +++ b/docker/compose.traefik.yml @@ -1,6 +1,8 @@ services: traefik: - image: traefik:3.6 + # A patch tag, so a host holding an older cached 3.6 pulls this one. 3.6.25 + # knows --accesslog.fields.queryparameters, which older 3.6 refuses. + image: traefik:3.6.25 ports: # Listen on port 80, default for HTTP, necessary to redirect to HTTPS - 80:80 diff --git a/docker/compose.yml b/docker/compose.yml index f78ed61..93eabfc 100644 --- a/docker/compose.yml +++ b/docker/compose.yml @@ -163,6 +163,9 @@ services: build: context: .. dockerfile: backend/Dockerfile + args: + # Opt-in: ffmpeg costs the image about 400 MB (backend/Dockerfile). + - WITH_FFMPEG=${WITH_FFMPEG:-false} labels: - autoheal=true - traefik.enable=true diff --git a/docker/nginx-api-proxy.conf b/docker/nginx-api-proxy.conf index 1c36445..d8ca29f 100644 --- a/docker/nginx-api-proxy.conf +++ b/docker/nginx-api-proxy.conf @@ -4,11 +4,17 @@ # client asks for /api/v1/... on whatever origin served it, and never needs a # resolvable api.${DOMAIN} or a CORS allowance. -location /api { +# /hooks is where the flows' webhooks and served sockets live, next to the API +# on the backend. One regex takes both, and a regex outranks `location /`, +# which would otherwise answer /hooks with the SPA. +location ~ ^/(api|hooks) { # Docker's embedded DNS, re-resolved: nginx otherwise pins the backend's # address at startup and 502s for good once that container is recreated. resolver 127.0.0.11 valid=10s ipv6=off; - set $backend_upstream http://backend:8000; + # The container name, which only the dev API carries. The test stack's + # backend answers to the service name `backend` on the shared proxy + # network too. + set $backend_upstream http://fluksio-api:8000; proxy_pass $backend_upstream$request_uri; proxy_http_version 1.1; @@ -18,8 +24,9 @@ location /api { # header naively would take it for the caller. proxy_set_header X-Forwarded-For $remote_addr; proxy_set_header X-Forwarded-Proto $scheme; - # The flow websocket (/api/v1/flows/ws) upgrades through here; passing the - # client's own Connection header covers both it and ordinary requests. + # The flow websocket (/api/v1/flows/ws) and a served socket upgrade through + # here; passing the client's own Connection header covers both and + # ordinary requests. proxy_set_header Upgrade $http_upgrade; proxy_set_header Connection $http_connection; # A live socket outlives the 60s default by design. diff --git a/docs/code/agents.md b/docs/code/agents.md index 4ce4ad4..363299c 100644 --- a/docs/code/agents.md +++ b/docs/code/agents.md @@ -22,8 +22,12 @@ Manage registered clients under **Admin → Agents**. Revoking a client invalidates its access. MCP uses a separate signing key at `OAUTH_PRIVATE_KEY_FILE`; rotating that key invalidates all MCP tokens. -MCP is not forwarded by the [portal tunnel](../interface/portal.md). -Connect directly to the instance's API host. +An instance connected to a [portal](../interface/portal.md) also answers +through it, at `https://hub.fluksio.com/i//mcp` (the portal's +own `hub.` host for a custom one). The portal forwards the endpoint and its +OAuth, and the instance checks the agent's token as it does locally. The +consent page opens in the instance as the portal serves it, so open the +instance from the portal first if this browser has not recently. ## Available tools @@ -50,8 +54,10 @@ A superuser configures an OpenAI-compatible provider under the dashboard's Assistant entry for signed-in users. The assistant uses the same tools with your own permissions. Draft edits -appear in the editor, and each answer includes a tool trace. Conversations -are stored per user on the instance; the bin button clears yours. +appear in the editor. Each answer streams in as the model writes it and +renders as markdown with a tool trace. +Conversations are stored per user on the instance; the bin button clears +yours. `MCP_ENABLED` is only needed for external clients, so the built-in assistant works without it. diff --git a/docs/code/api.md b/docs/code/api.md index e608e89..2e002ad 100644 --- a/docs/code/api.md +++ b/docs/code/api.md @@ -12,7 +12,8 @@ export FLUKSIO=http://127.0.0.1:8000/api/v1 Use the port printed by `fluksio serve`. For Docker, use `http://api.localhost/api/v1`, `http://:8080/api/v1` on a LAN stack, or `https://api./api/v1` for a deployment. -All paths below are relative to this base. +All paths below are relative to this base. Timestamps in responses are +ISO 8601 and always carry their UTC offset. ## Authenticating @@ -80,9 +81,13 @@ Function and Command nodes, changing Command settings, or changing `resources.env`. Other users can edit wiring, titles, and ordinary settings. Ordinary source edits stay in the draft until publishing. Shared library -source changes affect its users immediately. +source changes affect its users immediately, and the audit trail records them +like a publish. `POST /flows/{name}/nodes/{id}/share` promotes a source to the library; `/unshare` makes it private again. `GET /flows/library` lists shared sources. +`DELETE /flows/library/{name}` retires one, and answers 409 while nodes still +use it. With `?detach=true` each of those nodes keeps a copy of the code, in +the published flow and the draft, and the shared source is deleted. ### Running and controlling @@ -124,9 +129,10 @@ values. You can publish only to declared messages, just as dashboard controls do | `GET` | `/runs` | query history | | `GET` | `/runs/overview` | summarize runs per flow | | `GET` | `/runs/{id}` | read parameters, results, nodes, and artifacts | +| `GET` | `/runs/{id}/nodes/{node}/outputs` | read what one node returned | | `POST` | `/runs/{id}/cancel`, `/runs/{id}/retry` | cancel, or create a retry | | `DELETE` | `/runs/{id}` | delete a finished run and its records | -| `DELETE` | `/runs?flow={name}` | delete a flow's runs; refused while any are active | +| `DELETE` | `/runs?flow={name}` | delete a flow's runs; refused while any are active. `history=true` also clears its cascades from the history | | `GET` | `/runs/{id}/metrics?name=&stride=` | read metric series | | `GET` | `/runs/metrics/names` | list metric names across a selection | | `GET` | `/runs/series/compare?ids=a,b,c&metric=&x=` | compare curves by step, time, or another metric | @@ -140,10 +146,16 @@ History filters include `flow`, `status`, `group`, `digest`, `since`, `before`, `limit`, and `offset`. Rows arrive newest first; use the last row's `created_at` as `before` for stable pagination. +A node's outputs come from the stage cache, so only a node that succeeded +and may be cached has them, up to 256 kB of JSON. Any other node answers +`404`. + Exports also accept `ids` and `until`, with `format=csv` or `jsonl`. The metrics export is a long table; the runs export is one row per run. Use `params` and `metrics` to choose columns, including dotted paths into -record outputs. +record outputs. An export covers at most the newest 10 000 runs. When a +selection matches more, the response carries `X-Truncated` with that cap and +`X-Total-Count` with the number of runs the selection matched. Import takes `{"runs": [...]}`, records completed results without executing code, and deduplicates by `external_id`. See @@ -161,6 +173,11 @@ curl "$FLUKSIO/artifacts/" \ The upload returns a reference with the content digest. Identical bytes are stored once. Node code uses `save_artifact` and `load_artifact` instead. +A superuser can remove stored bytes with `DELETE /artifacts/`. While +a run, a current message or a flow input's initial value still references the +digest, the call returns `409`. The hourly sweep removes unreferenced bytes +on its own, so this is for reclaiming space sooner. + ## Dashboards and panels | Method | Path | Action | @@ -171,7 +188,7 @@ stored once. Node code uses `save_artifact` and `load_artifact` instead. | `POST` | `/dashboards/from-flow/{flow}` | create a batch flow's results dashboard | | `PUT` | `/dashboards/{name}` | save a draft | | `POST` | `/dashboards/{name}/publish`, `/dashboards/{name}/discard`, `/dashboards/{name}/rename` | publish, discard, or rename | -| `GET`, `PUT` | `/panels/` | read or update panel assignments | +| `GET`, `PUT` | `/panels/` | read or replace panel assignments; send the current `version` | | `POST` | `/panels/pair` | start device pairing | | `GET` | `/panels/pair/{code}` | poll pairing with its `secret` | | `POST` | `/panels/{id}/pair` | approve the pairing | @@ -202,6 +219,7 @@ Connector state keys can also be listed and cleared by a superuser; see | `GET` | `/observability/summary` | health and engine version; always returns `200` | | `GET` | `/observability/timeseries`, `/observability/flows` | execution metrics or flow rollups | | `GET` | `/observability/runs`, `/observability/events`, `/observability/dead-letter` | cascades, events, or abandoned work | +| `POST` | `/observability/flush` | write buffered metrics and events now; superuser only | | `GET` | `/utils/health/` | deep health check; fails on loop or state-backend problems | | `GET` | `/workers`, `/workers/resources` | attached workers and resource availability | | `GET`, `POST` | `/workers/tokens` | list or mint worker credentials; superuser only | diff --git a/docs/code/cli.md b/docs/code/cli.md index 301d7cb..5758313 100644 --- a/docs/code/cli.md +++ b/docs/code/cli.md @@ -65,8 +65,9 @@ that instance's running engine; plain mode refuses to start a duplicate. | `--worker-max-rss MB` | `0` (disabled) | Retire a worker above this resident memory after its call | Resource defaults can also come from the corresponding `FLOW_*` settings; -see [Configuration](../reference/configuration.md). GPU detection uses -NVIDIA device nodes. Set `--gpus` when your hardware is not detected. +see [Configuration](../reference/configuration.md). GPU detection counts the +entries of `CUDA_VISIBLE_DEVICES` when it is set, otherwise NVIDIA device +nodes. Set `--gpus` when your hardware is not detected. Use local disk for the data directory on a cluster: SQLite's write-ahead log does not support NFS. @@ -163,6 +164,11 @@ The generated node bodies import your functions. Keep the project available to each machine running them; sync does not copy your package to a remote worker. Uploading source requires a superuser token. +The flow records the git repository it was declared in, or the directory when +there is none, and each run hashes the Python files below it. The engine +refuses `/`, its home directory and relative paths there, so run sync from +inside the project's repository. + | Flag | Purpose | |---|---| | `--dry-run` | Print flow documents and node bodies without uploading | @@ -184,6 +190,13 @@ Syncs local code, then submits a run. Flags such as `--lr` name the flow's declared inputs and are converted to their declared types. Unknown inputs are rejected. Use `--param name=a,b` with `sweep` for multiple values. +Everything after `--` is an input written as `name=value`. Use it for an input +that shares its name with a flag of `run`, such as `timeout`: + +```sh +fluksio run train --wait --timeout 600 -- timeout=30 lr=0.05 +``` + After the first sync, `run` imports the module that declared the flow. If no declaration is recorded, it searches the working directory and skips paths covered by `.gitignore`. Import errors in that fallback search produce @@ -297,6 +310,8 @@ select columns, including dotted paths such as `final_metrics.train_loss`. Both support `--flow`, repeated `--run ID`, `--group`, `--status`, `--since`, `--until` and `--local`. The run ID joins results to metrics. +An export stops at the newest 10 000 runs, and when a selection matches more, +the CLI and the Python client warn how many were left out. Output defaults to CSV on stdout. `-o FILE` writes a file; `--format jsonl` writes JSON Lines. Parquet requires `pip install 'fluksio[parquet]'` and diff --git a/docs/code/nodes.md b/docs/code/nodes.md index 498a70e..4b6dec3 100644 --- a/docs/code/nodes.md +++ b/docs/code/nodes.md @@ -29,6 +29,24 @@ Inside a node, `import fluksio` exposes reporting and artifact helpers. It does not expose the engine's internal modules. Other imports come from the node's Python environment; see [Packages](#packages). +## The header follows the ports + +On the canvas, the parameters of `process` and the node's input ports and +settings are kept in step, in both directions. + +- Changing ports or settings in the node panel rewrites the + `def process(...)` line and leaves the rest of the code as it is. Input + ports come first, in their order, then the settings. A parameter that stays + keeps its annotation and default. +- Saving the code reads that line back. A name the node already has keeps + its kind. A new parameter becomes a setting when it has a default, which + becomes its value, or when it comes after a setting. Any other new + parameter becomes an input port. Removing a parameter removes its port or + setting, and renaming a single one renames the port (keeping its message) + or the setting (keeping its value). +- Shared code, code from `fluksio sync`, and a header with a decorator, + `*args`, `**kwargs`, a bare `*` or `/` are left alone in both directions. + ## Producing values over time Yield dictionaries to publish outputs before the node finishes: @@ -150,9 +168,134 @@ def process(clip): ``` Use it only for clips headed for such a panel. Browsers play the originals, -and the result plays there too, just smaller. The Docker image does not ship -`ffmpeg` yet, so the node runs only where `ffmpeg` is on the `PATH`, such as a -[remote worker](workers.md). +and the result plays there too, just smaller. The node needs `ffmpeg` on the +`PATH`. The Docker image includes it only when built with +[`WITH_FFMPEG=true`](../reference/configuration.md#image-options) in `.env`. +Otherwise run the node on a [remote worker](workers.md) that has it. + +### Speech to and from a panel + +openpanel publishes a 16 kHz mono WAV on its `mic` port after its wake word, +and plays any `audio` reference sent to its `speaker` port (WAV, MP3 or AAC). +Two Function nodes close that loop with local models. +[faster-whisper](https://github.com/SYSTRAN/faster-whisper) turns speech into +text and [piper](https://github.com/OHF-Voice/piper1-gpl) turns text into +speech. + +The listening node has an `audio` input port `audio` and a `str` output port +`text`: + +```python +from functools import cache +from pathlib import Path + +from faster_whisper import WhisperModel + +import fluksio + +MODELS = Path.home() / ".cache" / "voice" + + +@cache +def _model(size): + # Downloaded on the first call, then held by the worker between calls. + return WhisperModel( + size, device="cpu", compute_type="int8", download_root=str(MODELS) + ) + + +def process(audio, model="base", language="en"): + # vad_filter skips silence, which Whisper would otherwise hear as "You". + segments, _ = _model(model).transcribe( + fluksio.load_artifact(audio), language=language or None, vad_filter=True + ) + return {"text": " ".join(segment.text.strip() for segment in segments)} +``` + +The speaking node has a `str` input port `text` and an `audio` output port +`speech`: + +```python +import io +import wave +from functools import cache +from pathlib import Path + +from piper import PiperVoice +from piper.download_voices import download_voice + +import fluksio + +MODELS = Path.home() / ".cache" / "voice" + + +@cache +def _voice(name): + # Downloaded on the first call, then held by the worker between calls. + MODELS.mkdir(parents=True, exist_ok=True) + download_voice(name, MODELS) + return PiperVoice.load(MODELS / f"{name}.onnx") + + +def process(text, voice="en_US-lessac-medium"): + buffer = io.BytesIO() + with wave.open(buffer, "wb") as wav: + _voice(voice).synthesize_wav(text, wav) + speech = fluksio.save_artifact(buffer.getvalue(), "speech.wav", "audio/wav") + return {"speech": speech} +``` + +`model`, `language` and `voice` are settings. `tiny` is faster than `base` and +less accurate. An empty `language` lets Whisper guess it for each clip. `voice` +takes any name from the [piper voices](https://huggingface.co/rhasspy/piper-voices), +such as `de_DE-thorsten-medium`. + +To wire the loop in a flow `home`: + +1. Declare a [flow input](../concepts/flows.md#flow-inputs) `heard` of type + `audio` with origin `panel`. +2. Under the panel's [I/O](../interface/dashboards.md#the-devices-own-io), wire + `mic` as `in` to `home.heard` and `speaker` as `out` to `home.reply`. +3. Bind the listening node's `audio` to `heard` and its `text` to `said`. +4. Have your own nodes read `said` and publish an answer as a `str`, such as + `answer`. +5. Bind the speaking node's `text` to `answer` and its `speech` to `reply`. + +Binding the speaking node's `text` straight to `said` makes the panel repeat +what it heard, which tests the loop before there is any logic in it. + +Both packages go into the node's environment, as described in +[Packages](#packages). In a managed environment, list them in **Modules**: + +```text +faster-whisper +av<19 +piper-tts +``` + +`av<19` holds PyAV back, since faster-whisper 1.2.1 cannot open files with +PyAV 19. In your own venv or on a remote worker, +`pip install faster-whisper "av<19" piper-tts` does the same. + +The models land in `~/.cache/voice` on the machine running the node. The Docker +image keeps its home directory inside the container, so a recreated container +downloads them again. Point `MODELS` at a directory under `/data` to keep them +on the volume. An [attached worker](workers.md) starts a process per call and +loads the model each time. + +What it costs, measured on a 2016 server CPU (Xeon E5-2683 v4): + +| | Size | Memory | 3 s of speech, one core | All cores | +|---|---|---|---|---| +| Packages | 150 MB download, 460 MB installed | | | | +| Whisper `tiny` | 78 MB | 300 MB | 1.6 s | 0.7 s | +| Whisper `base` | 148 MB | 375 MB | 2.9 s | 1.2 s | +| piper `en_US-lessac-medium` | 63 MB | 250 MB | 0.6 s | 0.3 s | + +Each model downloads once, on its node's first call. Loading it takes about a +second for Whisper and four seconds for piper. Memory is the worker's peak while +it holds the model. These figures come from piper's own speech fed back into +Whisper. A room microphone is harder, on `tiny` most of all. ## Printing @@ -218,14 +361,15 @@ already has a managed one. Without an existing venv, it creates a managed environment. `NODE_VENV=managed` always requests one; the Docker image uses this setting. -In managed mode, the Modules manifest is installed with `uv pip sync` and -versioned with the flows. Applying it refreshes local workers. The API accepts -the same manifest: +In managed mode, the Modules manifest is resolved with `uv pip compile`, +installed with `uv pip sync` and versioned with the flows. Listing a package +brings its dependencies along. Applying it refreshes local workers. The API +accepts the same manifest: ```sh curl -X POST "$FLUKSIO/modules/apply" -H "Authorization: Bearer $TOKEN" \ -H 'Content-Type: application/json' \ - -d '{"requirements": "numpy>=2\npandas\n"}' + -d '{"requirements": "numpy>=2\n"}' ``` Here `$FLUKSIO` is the API base URL ending in `/api/v1`, and `$TOKEN` is an diff --git a/docs/code/workers.md b/docs/code/workers.md index 810c501..b996bda 100644 --- a/docs/code/workers.md +++ b/docs/code/workers.md @@ -127,7 +127,7 @@ curl "$API_URL/workers" -H "Authorization: Bearer $TOKEN" curl "$API_URL/workers/resources" -H "Authorization: Bearer $TOKEN" ``` -## Revoke or upgrade +## Revoke a token List tokens and revoke one by its `jti`: @@ -138,12 +138,19 @@ curl -X POST "$API_URL/workers/tokens/$JTI/revoke" \ ``` Revocation also disconnects attached workers and blocks artifact access. -Replacing the OAuth signing key revokes all worker tokens. +Replacing the OAuth signing key revokes all worker tokens. Older tokens without +signed labels and a `jti` need to be replaced. -Upgrade the engine before its workers, then run `pip install -U fluksio-worker` -on each host. Engine and worker must speak the same protocol; a mismatch is -reported and refused. Older tokens without signed labels and a `jti` need to -be replaced. +## Upgrading workers + +Engine and worker must speak the same protocol. A worker on another protocol is +refused and exits, so a release that bumps the protocol needs +`pip install -U fluksio-worker` on every worker host. Upgrade the engine first, +then the workers. + +The **Workers** screen shows each worker's package version and protocol. A +worker refused for an older protocol stays listed as **Needs upgrade** until it +attaches again, or until the engine restarts. ## Slurm provisioning diff --git a/docs/concepts/flows.md b/docs/concepts/flows.md index 70353cd..34b00e9 100644 --- a/docs/concepts/flows.md +++ b/docs/concepts/flows.md @@ -96,13 +96,18 @@ Declare messages supplied by dashboard controls, the API or run parameters as the flow's **Inputs**. Give live inputs a starting value: ```json -{"inputs": [{"spec": {"name": "setpoint", "dtype": "float"}, "initial": 21.0}]} +{"inputs": [{"spec": {"name": "setpoint", "dtype": "float"}, "initial": 21.0, "origin": "panel"}]} ``` This seeds `house.setpoint` in flow `house` if it has no value yet. Later publications replace it. Batch inputs are run parameters, and batch outputs select the run's result. See [Where a node's values come from](values.md). +`origin` says what writes an input: `run` (the default) for a parameter a run +passes in, `panel` for a dashboard or panel control. The canvas labels each +input with it, and the run form leaves panel inputs out, so a run keeps their +starting value. The engine treats both the same. + ## Drafts and publishing - **Saving** stores a draft; the engine keeps running the published version. @@ -132,9 +137,10 @@ The canvas reports issues on the affected nodes: | `missing_source` | a Function node has no saved code | | `node_error` | code or configuration could not load | | `unauthenticated_hook` | a webhook or served websocket has no shared secret; advisory | +| `ignored_device` | a node other than a Function node names a device, which nothing reads; advisory | | `node_unhealthy` | a loaded node reports a runtime problem, such as a lost connection | -Build issues prevent the affected flow from running. The last two are runtime +Build issues prevent the affected flow from running. The last three are runtime or advisory issues: other work continues, and node health clears when the connection recovers. diff --git a/docs/concepts/runs.md b/docs/concepts/runs.md index bb51221..ecee531 100644 --- a/docs/concepts/runs.md +++ b/docs/concepts/runs.md @@ -139,11 +139,14 @@ Use `fluksio run ... --no-cache`, `fluksio sweep ... --no-cache`, or Only Python nodes are cached. Built-in and connector nodes are not. The cache uses run history; outputs too large to store there are not cacheable, and -missing artifact bytes make an entry a miss. +missing artifact bytes make an entry a miss. An entry written before the cache +key last changed shape can never match again, so the engine drops it from the +cache and keeps the node's recorded outputs. A cache hit does not replay emissions. Its metrics are read from the original run, so comparisons still show the recorded curve. Deleting that original run -leaves cached results available but removes their curve. +leaves cached results available but removes their curve. The run's page then +names the deleted run each lost curve went with. ## Objects that cannot be serialized @@ -183,15 +186,18 @@ cached stages. Cancel active runs before retrying them. The **Runs** screen filters by flow, status or sweep. Each run shows its parameters, per-node timings and logs, traceback, metrics, artifacts and -result. +result. Its inputs are listed as the flow declared them when the run was +submitted, and the ones it took from the flow are marked as defaults. Select two or more runs to compare curves and the parameters that differ. Plot against step, elapsed seconds or another metric from the same runs. Comparisons have shareable URLs. -Deleting a flow keeps its run history. You can still inspect those runs, but -cannot retry them until the flow exists again. Delete history separately, -after active runs finish or are cancelled. +Deleting a flow keeps its run history, and the artifacts those runs recorded +stay on disk. You can still inspect those runs, but cannot retry them until +the flow exists again. Tick **Also delete its runs** in the delete dialog to +clear them with the flow, or delete history separately after active runs +finish or are cancelled. ### Taking it into a dataframe diff --git a/docs/concepts/values.md b/docs/concepts/values.md index 1628f71..33ff8a8 100644 --- a/docs/concepts/values.md +++ b/docs/concepts/values.md @@ -47,8 +47,10 @@ The initial value seeds an unset live message. Later publications take over; changing the starting value does not overwrite a value already in state. Use **Publish this value now** in the input row to send it immediately. -The canvas draws flow inputs as labelled endpoints. Without a producer or -declared input, a consuming node reports `unconnected_input`. +The canvas draws flow inputs as labelled endpoints, marked `run` or `panel` +by what writes them. A panel input is one a dashboard or panel control writes, +and the run form does not ask for it. Without a producer or declared input, a +consuming node reports `unconnected_input`. A batch flow's inputs are run parameters. Its **Result** section selects the messages returned when the run finishes. See [Runs](runs.md). diff --git a/docs/getting-started/facility-automation.md b/docs/getting-started/facility-automation.md index e0b850b..ac5a02f 100644 --- a/docs/getting-started/facility-automation.md +++ b/docs/getting-started/facility-automation.md @@ -86,10 +86,10 @@ Publish the change. The Function now responds to sensor readings instead of the test value. If your sensor sends a JSON object such as `{"temperature": 18.0}`, set `json_key` to `temperature`. -An MQTT node with input ports publishes to its configured topic; one with -output ports subscribes. Add a publishing node only after checking the payload -format your device expects. A relay may need `ON` and `OFF` rather than a -boolean. +An MQTT node with inputs is a sender and publishes to its configured topic; +one with outputs only is a listener and subscribes. Add a sender only after +checking the payload format your device expects. A relay may need `ON` and +`OFF` rather than a boolean. !!! tip "Keep the first test on screen" diff --git a/docs/interface/dashboards.md b/docs/interface/dashboards.md index e5d3997..c3a0bb8 100644 --- a/docs/interface/dashboards.md +++ b/docs/interface/dashboards.md @@ -13,7 +13,14 @@ to a paired screen, such as a wall tablet. 4. **Publish** when the dashboard is ready. Edits stay in a draft; paired screens show the published version. On phones, -tiles stack in one column and dragging is disabled, but settings remain editable. +tiles stack in one column over the dashboard's ground (its background image, +or the Glass look's drifting colours) and dragging is disabled, but settings +remain editable. +In edit mode, ⌘Z / Ctrl-Z undoes an edit and ⌘⇧Z / Ctrl-Shift-Z redoes it. + +A widget can end up below the canvas, for example after the canvas was made +smaller. In edit mode the canvas then grows to reach it, under a dashed line +where the panel ends. Scroll down and drag it back, or use **Reflow**. Controls can publish real values while you edit, so choose their targets with care. Your new Switch may already be flipping something! @@ -329,7 +336,7 @@ dashboard's look's. | What | Moves | From → to | Timing | Input during it | |---|---|---|---|---| | Face paging (round) | the faces, side by side on a track | the track slides one face width S, the new face coming in from the side it lies on (the next from the right). A dot that skips faces brings its face in beside the one shown, so a turn is always one width | spring, from where the track is (a swipe's release speed carried on) | a new turn while one runs: the running one lands at once and the next starts | -| Dashboard paging (round panel) | the dashboard, and beside it the next one | slides S up (the next, from below) or down (the previous, from above), the next one drawn on the face it opens on; once landed, it is the dashboard shown, and its name pill shows for a moment. One opened by its address replaces the last at once | spring | further paging waits for the turn to land | +| Dashboard paging (round panel) | the dashboard, and beside it the next one | slides S up (the next, from below) or down (the previous, from above), the next one drawn on the face it opens on; once landed, it is the dashboard shown, and its name pill shows for a moment. Each one on the track wears its own look, theme and palette. The ground is the dashboard shown's and changes over as the turn begins, without a fade. One opened by its address replaces the last at once | spring | further paging waits for the turn to land | | Page dot | the current dot and the last | muted at 0.5 opacity ↔ text colour at 1 | 200 ms, cubic-bezier(0.4, 0, 0.2, 1) | retargets | | Dashboard name | the pill, on landing on another dashboard | opacity 0 → 1, held 1000 ms, 1 → 0 | in 200 ms, out 150 ms, cubic-bezier(0.4, 0, 0.2, 1) | — | | Ring value set by a notch, a key or the engine | the value arc's end, the handle (slider, selector, colour) | from where it is drawn to the new fraction of the sweep | spring | each notch retargets from where it is, at the speed it has | @@ -340,7 +347,7 @@ dashboard's look's. | Value face, Value and Gauge tiles | the number | counts to the new value, written to its precision | spring | retargets | | Slider tile set elsewhere | the fill's end, the handle, the reading's dot | from where it is drawn to the new fraction | spring | a drag or a key moves it at once | | Gauge and Bar tiles | the value arc, each bar's fill | from where it is drawn to the new fraction | spring | retargets | -| Dashboard change (rectangular panel) | every tile | the old dashboard goes at once; each new tile opacity 0 → 1, y +8 → 0 | spring | — | +| Dashboard change (rectangular panel) | every tile | the old dashboard goes at once. The new tiles come in row by row, 30 ms apart, each opacity 0 → 1 and y +8 → 0 (Glass: +12, and scale 0.96 → 1): from below, top row first, for a dashboard further down the rail, and from above (y −8 → 0), bottom row first, for one further up | spring | — | | Rail | the current entry's marker | slides to the new entry | spring | retargets | With reduced motion asked for, nothing travels on its own: a page under the @@ -388,9 +395,11 @@ As a superuser: The screen collects its credential and shows the assigned published dashboards. Several dashboards get a navigation rail. -A panel credential permits its assigned dashboards, their bound messages and -the artifacts they show. It also permits publishing from its controls and query -charts. On a locked dashboard only the query charts may still publish. +A panel credential reads its assigned dashboards, their bound messages and the +artifacts they show. It writes only the targets of their controls and query +charts, and the `in` ports of its own [I/O](#the-devices-own-io). A message its +tiles only draw, or a dashboard setting is bound to, stays read only. On a +locked dashboard only the query charts may still publish. **Unpair screen** revokes a locally paired credential and keeps the panel's dashboard assignments. Deleting the panel removes the assignment and its @@ -411,24 +420,28 @@ to 640×360. Re-encode other clips with the [clip recipe](../code/nodes.md#makin The device decides what the panel's [I/O](#the-devices-own-io) port names mean. openpanel serves `gpio.`, `backlight`, `touch`, `mic` and `speaker`, lists each one's `config` in its README and ignores any other name. A browser -screen serves none of them. +screen serves none of them. The [speech recipe](../code/nodes.md#speech-to-and-from-a-panel) +turns `mic` into text and text into `speaker`. ### The device's own I/O A panel can also act as a node. Under **I/O**, wire a port on the device to a -message: +message. A new port saves once it has a port name and a message, at the latest +when the dialog closes: | Field | Meaning | |---|---| | `port` | The device's name for it, such as `gpio.4`, `backlight`, `touch`, `mic` or `speaker`. Lowercase letters, digits, `_` and `.` | | `direction` | `in`: the device publishes to the message. `out`: the device follows it | -| `message` | A qualified `flow.name`. A flow must declare it, as for a control | +| `message` | A qualified `flow.name`, a lowercase flow name, a dot, then letters, digits and underscores. A flow must declare it, as for a control | | `config` | A JSON object passed to the device unread, such as `{"pull": "up", "debounce_ms": 50}` | The credential may publish to its `in` messages even when a dashboard is locked, and may upload an artifact named after one (`PUT /artifacts?name=`), which is how a microphone sends a recording. -`out` messages are read only. +`out` messages are read only. The flow canvas draws each port as an endpoint, +an `in` port feeding the nodes that consume its message and an `out` port fed +by the nodes that produce it. !!! note "If the local link is wrong" diff --git a/docs/interface/flow-editor.md b/docs/interface/flow-editor.md index 5ab659e..56d2620 100644 --- a/docs/interface/flow-editor.md +++ b/docs/interface/flow-editor.md @@ -5,12 +5,22 @@ the connections and arranges the canvas for you. ## The canvas -The graph runs left to right on desktop and top to bottom on phones. +The graph runs left to right on desktop and top to bottom on phones. On a +phone, at most two nodes sit side by side and the rest wrap onto the rows +below, so a large flow grows downwards in one column. - **Nodes** show their execution status. -- **Edges** pulse when values pass through. Click one to inspect its payload. -- **Endpoints** show flow inputs, dashboard widgets and nodes in other flows - that exchange messages with this flow. +- **Edges** are routed around the nodes, a loop back to an earlier node + included, and pulse when values pass through. Click one to inspect its + payload. +- **Endpoints** show flow inputs, dashboard widgets, panel I/O ports and nodes + in other flows that exchange messages with this flow. A value passing lights + one up in a sweep: left to right when it sends into the flow, right to left + when it reads out of it. +- **Clicking an endpoint** shows a flow input's type, starting value and + current value, with a pencil that opens the flow settings to edit it. A panel + port opens its panel in the Panels dialog. A node in another flow opens that + flow on the label leading back here, with its edges highlighted. Values update over a websocket. A connection banner tells you when the editor cannot reach the engine. @@ -78,7 +88,7 @@ Open **Flow settings** from the pencil button in the dock. |---|---| | **Running** | starts or stops the published live flow's subscriptions, schedules and webhooks | | **Mode** | chooses continuous `live` operation or requested `batch` runs | -| **Inputs** | declares external messages and starting values; batch inputs become run parameters | +| **Inputs** | declares external messages, their starting values and whether a run or a panel writes them; run inputs become run parameters | | **Result** | selects batch output messages | | **Contents** | shows the node count and message namespace | diff --git a/docs/interface/index.md b/docs/interface/index.md index 7005ebd..ba86273 100644 --- a/docs/interface/index.md +++ b/docs/interface/index.md @@ -58,6 +58,12 @@ Health shows flow status, node load errors, queue depth and event-loop lag. After you have used batch runs, it also shows running and queued runs. A degraded instance lists the problems that need attention. +### Execution time per node type + +One bar per node type at its average time per run, slowest first, on a log +scale. Each row also shows how often the type ran and its slowest single run, +so a slow or busy kind of node stands out. + ### Activity Choose 1h, 6h, 24h or 7d to review executions and failures, recent runs, diff --git a/docs/interface/operations.md b/docs/interface/operations.md index d332425..9db94d8 100644 --- a/docs/interface/operations.md +++ b/docs/interface/operations.md @@ -32,8 +32,9 @@ the flow repository. Using references keeps credentials out of saved flows. packages and Python version. There are two setups: - **Managed environment:** Docker uses a separate node environment. A - superuser edits **Requirements** and presses **Apply**. Packages are synced - with `uv pip sync`; deleting a requirement removes it. The manifest is + superuser edits **Requirements** and presses **Apply**. The list is resolved + with `uv pip compile` and synced with `uv pip sync`, so each package brings + its dependencies and deleting a requirement removes it. The manifest is versioned with the flows, and successful installs refresh the workers. - **Adopted environment:** a fresh pip installation inside your own virtual environment can use that environment for nodes too. Modules is read-only. diff --git a/docs/interface/portal.md b/docs/interface/portal.md index e407f2a..ac0cb26 100644 --- a/docs/interface/portal.md +++ b/docs/interface/portal.md @@ -52,23 +52,56 @@ Their account appears under **Admin → Users** with a *Portal* badge. They sign in through the portal rather than with a local password. Invitations are time-limited; generate a fresh one if it expires. +Somebody admitted by a link gets their local account the first time they open +the instance. Until then they are listed under **Admin → Users → Pending portal +members**, where you can revoke the invitation. + +If a local user already has their email, they are refused rather than handed +that account. A superuser can press **Link portal account** beside them to let +them in as that user, who keeps their local password. Superuser accounts +cannot be linked. A linked user counts as a portal user from then on, so +removing them at the portal, or disconnecting, deletes them. + +## Hooks and agents through the portal + +The portal also forwards a flow's hooks and the MCP endpoint, so they work +without an inbound port: + +- A webhook or a served WebSocket: + `https://hub.fluksio.com/i//hooks///`, + with `wss://` for a socket. +- An agent: `https://hub.fluksio.com/i//mcp`, see + [Agents over MCP](../code/agents.md). + +Through the portal a hook needs a [secret](../reference/node-types.md#http). +A hook without one answers only on the instance's own address; through the +portal it gets the same 404, or closed socket, as a wrong secret. The instance +id is the one in the address when you open the instance from the portal. + ## Manage or end remote access **Settings → Remote access** shows the connection status and the latest connection error. The instance needs outbound access to the portal, and any proxy on that route must support WebSockets. -- To remove a person's access, remove them from the portal's sharing dialog - or edit their account under **Admin → Users** and clear **Is active?**. +- To remove a person's access, remove them from the portal's sharing dialog, + which also deletes their local account while the instance is connected, or + delete or deactivate their account under **Admin → Users**. - To end all portal access from the instance, press **Disconnect**. This stops the connection and invalidates portal credentials locally, without requiring - the portal to be online. + the portal to be online. It also deletes the users the portal let in; the + account that enrolled and every superuser stay. - To replace the pairing, use **Connect with a new code**. Remote users and panels may need to pair again. +If the instance is removed at the portal, or its code is renewed there, the +status reads **Removed at the portal** and the instance stops dialling. Connect +with a new code to restore remote access. + Local sign-in and flows continue after disconnecting. Portal requests follow -the instance's normal account permissions, and the instance verifies portal -tokens against the public signing key pinned during enrollment. +the instance's normal account permissions. The instance verifies portal tokens +against the portal's signing keys, which it pins during enrollment and takes +again from the portal each time it connects. ## Custom portals diff --git a/docs/reference/configuration.md b/docs/reference/configuration.md index 3e37778..149e8c9 100644 --- a/docs/reference/configuration.md +++ b/docs/reference/configuration.md @@ -100,6 +100,12 @@ to the backend. An empty value needs that proxy when the frontend and backend are served separately. See [facility automation](../getting-started/facility-automation.md#run-it-long-term) for deployment settings. +## Image options + +`WITH_FFMPEG=true` in `.env` installs `ffmpeg` into the backend image on the +next build, for node code that transcodes video. It is off by default because +it adds about 400 MB. + ## Environment | Variable | Default | Purpose | @@ -115,7 +121,8 @@ stack does. Production disables it entirely. Staging and production also refuse `changethis` values for the session key or administrator password. Set `TZ` in `.env` for schedules that should follow local time, such as -`Europe/Berlin`. +`Europe/Berlin`. `make up`, `make update` and `make dev` warn when `.env` lacks +it, or any other key `.env.example` lists that Compose would otherwise default. ## Engine and resources @@ -126,20 +133,31 @@ Set `TZ` in `.env` for schedules that should follow local time, such as | `FLOW_MAX_RUNS` | `4` | concurrent batch runs | | `FLOW_NODE_TIMEOUT` | `0` | maximum node silence in seconds, unless the node overrides it; `0` disables | | `FLOW_CPUS` | `0` | CPU budget; `0` detects cores and reserves two for the engine | -| `FLOW_GPUS` | `0` | local GPU count; `0` detects NVIDIA device nodes | +| `FLOW_GPUS` | `0` | local GPU count; `0` counts the engine's `CUDA_VISIBLE_DEVICES`, or NVIDIA device nodes without it | | `FLOW_RAM` | `0` | memory budget in MB; `0` detects host/container memory and reserves 1 GiB | | `FLOW_WORKER_MAX_RSS_MB` | `0` | retire a local worker above this memory use after a call; `0` disables | +| `FLOW_NODE_STOP_TIMEOUT` | `5` | seconds a node gets to stop during a rebuild before the rebuild moves on | +| `FLOW_PIPELINE_REBUILD_WAIT` | `15` | seconds a request waits for a running whole-pipeline rebuild before it gets a 503 | +| `FLOW_REBUILD_WAIT` | `5` | the same for a single flow's rebuild, such as a deploy | +| `FLOW_RESTART_BACKOFF` | `[1,5,30,60]` | seconds between restarts of a crashed node loop; the last step repeats | +| `FLOW_CANCEL_GRACE` | `5` | seconds a cancelled node loop gets to stop before it is abandoned | | `OBS_RETENTION_DAYS` | `30` | retention for metrics, events and run records | The three concurrency limits must be at least `1`. They also have CLI flags: -`--max-workers`, `--max-cascades` and `--max-runs`. Resource flags are `--gpus`, -`--ram` and `--worker-max-rss`. +`--max-workers`, `--max-cascades` and `--max-runs`. When `FLOW_MAX_WORKERS` or +`FLOW_MAX_CASCADES` is more than four times the machine's core count, the +engine logs a warning at startup and uses the value anyway. Resource flags are +`--gpus`, `--ram` and `--worker-max-rss`. Nodes with declared resources are scheduled against the available budget. Undeclared nodes use the shared pool and receive thread caps. Attached workers report their own inventory; the engine's resource settings describe only its own machine. See [Remote workers](../code/workers.md) and the **Workers** screen. +When the engine runs with `CUDA_VISIBLE_DEVICES`, a node's card is named the +way that list names it, so under `2,3` the first card is device `2`. UUID and +MIG entries are passed on as written. + ## Artifacts | Variable | Default | Purpose | diff --git a/docs/reference/connector-contract.md b/docs/reference/connector-contract.md index 95aff46..3e75e41 100644 --- a/docs/reference/connector-contract.md +++ b/docs/reference/connector-contract.md @@ -164,8 +164,8 @@ await asyncio.to_thread(self.store.delete, "token:device") 64 KiB and each package allows 256 keys; exceeding a limit raises `ValueError`. - Calls block on the database: use `asyncio.to_thread` on the event loop. -Superusers can list keys and clear them under **Stored state** in the node -panel. Values are never returned. The equivalent API paths are: +Superusers can list keys, delete one, or clear them all under **Stored state** +in the node panel. Values are never returned. The equivalent API paths are: | Method | Path | |---|---| diff --git a/docs/reference/node-types.md b/docs/reference/node-types.md index 7366d04..1551c55 100644 --- a/docs/reference/node-types.md +++ b/docs/reference/node-types.md @@ -25,9 +25,11 @@ arrive as keyword arguments. ### MQTT -**`mqtt`** — subscribe to topics, or publish what arrives on its inputs. +**`mqtt`** — listen on topics, or send to them. -A node with *outputs only* subscribes; a node with *inputs* publishes. +The ports pick the direction: outputs only make a **listener**, inputs a +**sender**. A listener subscribes to its topics, a sender publishes its inputs +to them. | Setting | Default | Notes | |---|---|---| @@ -40,7 +42,7 @@ A node with *outputs only* subscribes; a node with *inputs* publishes. | `retain` | `false` | on published messages | | `keepalive` | `60` | seconds | | `timeout` | `10.0` | seconds before a broker operation is given up on | -| `publish_queue_size` | `256` | payloads that may wait for the broker; past this the oldest is dropped and the node reports degraded | +| `send_queue_size` | `256` | payloads that may wait for the broker; past this the oldest is dropped and the node reports degraded | | `json_key` | — | key to lift out of an object payload; one for every port, or `{"port": "key"}` | A topic may be a filter: `+` matches one level, `#` the rest. Everything a @@ -55,18 +57,22 @@ unwrapped only if it contains the port's name as a key. ### HTTP -**`http`** — receive data on a webhook, or send it to a URL. +**`http`** — listen on a webhook, or send to a URL and pass the reply on. -Outputs only makes it a **webhook**: the engine mounts a route at -`/hooks/{flow}/{url}/{secret}` while the flow runs. Inputs make it a **sender**. +The ports pick the direction: outputs only make a **listener**, inputs a +**sender**. A listener is a **webhook**: the engine mounts a route at +`/hooks/{flow}/{url}/{secret}` while the flow runs. A sender requests `url` +and publishes a JSON object response on its outputs, one key per output port. +Keys no port declares are dropped, and a response that is not an object +publishes nothing. | Setting | Default | Notes | |---|---|---| -| `url` | — | the route path in webhook mode, the full URL in sender mode | +| `url` | — | the route path for a webhook, the full URL for a sender | | `method` | `POST` | `GET` or `POST` | -| `timeout` | `30` | seconds, sender mode | -| `headers` | `{}` | | -| `query` | `{}` | fixed query parameters; a value may be a secret reference | +| `timeout` | `30` | seconds, sender only | +| `headers` | `{}` | request headers, sender only; a value may be a secret reference | +| `query` | `{}` | fixed query parameters, sender only; a value may be a secret reference | | `send_inputs` | `true` | off when the inputs only trigger the request | | `secret` | — | shared secret appended to the webhook URL; takes a secret reference | @@ -74,6 +80,8 @@ Outputs only makes it a **webhook**: the engine mounts a route at The canvas flags this as an advisory issue rather than stopping the flow, because a webhook on a private network is a legitimate thing to want. + Through the [portal](../interface/portal.md#hooks-and-agents-through-the-portal) + a webhook or served socket answers only with a secret. ### WebSocket @@ -171,13 +179,14 @@ Use `cron` for scheduled emissions, or trigger the node by hand for testing. | Setting | Default | Notes | |---|---|---| | `delay` | `0` | seconds to hold each message, fractional | -| `interval` | `0` | minimum seconds between forwards | +| `interval` | `0` | minimum seconds between forwards, per output | | `mapping` | `{}` | input port → output port; paired in order when empty | | `cron` | — | five-field expression | -Order of operations: rate check → delay → forward. With a `cron` and no inputs -it emits the current time on each tick; with inputs it emits the last value it -received. +Order of operations: delay → rate check → forward. A message arriving inside +the `interval` window is held, and when the window ends the latest one held is +sent. With a `cron` and no inputs it emits the current time on each tick; with +inputs it emits the last value it received. Timers wait until their deadline; busy workers can delay execution. @@ -186,11 +195,12 @@ Timers wait until their deadline; busy workers can delay execution. Batch flows reject rate-limited non-streaming ports. Runs have no live timer to release a held value. -### Trigger +### Debounce -**`trigger`** — send one value now and another once things go quiet. Despite -the name, a debounce and hold rather than a scheduler: everything it sends -starts from a value arriving. For a cron tick, see [`inject`](#inject). +**`debounce`** — send one value now and another once things go quiet. A +debounce and hold rather than a scheduler: everything it sends starts from a +value arriving. For a cron tick, see [`inject`](#inject). Node-RED calls this +node *trigger*. | Setting | Default | Notes | |---|---|---| @@ -362,7 +372,7 @@ Each record in `actions` contains `message`, `value`, `applied`, `delay_s`, applied. Keep `apply=false` while checking how the model interprets commands. A call with a delay ("in 10m") is held by the engine's work queue, like the -Trigger node's second value: it survives a restart where the queue is in +Debounce node's second value: it survives a restart where the queue is in Redis, and not otherwise. The node adds default devices to the model's view for missing expected devices. diff --git a/frontend/package.json b/frontend/package.json index c85b5ae..791a0cb 100644 --- a/frontend/package.json +++ b/frontend/package.json @@ -57,6 +57,7 @@ "react-grid-layout": "^2.2.4", "react-hook-form": "^7.68.0", "react-icons": "^5.5.0", + "react-markdown": "^10.1.0", "tailwind-merge": "^3.4.0", "tailwindcss": "^4.3.3", "uplot": "^1.6.32", diff --git a/frontend/playwright.config.ts b/frontend/playwright.config.ts index 7795f7b..0fc88dd 100644 --- a/frontend/playwright.config.ts +++ b/frontend/playwright.config.ts @@ -28,7 +28,7 @@ export default defineConfig({ /* Shared settings for all the projects below. See https://playwright.dev/docs/api/class-testoptions. */ use: { /* Base URL to use in actions like `await page.goto('/')`. Defaults to the - integrated stack (`make dev`), the only origin the API allows CORS from. + suite's own stack (`make test-stack`), never the dev instance's data. Point PLAYWRIGHT_BASE_URL elsewhere to test another running server; the API origin follows it (see tests/config.ts). */ baseURL: appUrl, @@ -81,14 +81,22 @@ export default defineConfig({ // dependencies: ['setup'], // }, - // { - // name: 'webkit', - // use: { - // ...devices['Desktop Safari'], - // storageState: 'playwright/.auth/user.json', - // }, - // dependencies: ['setup'], - // }, + /* The drag specs in WebKit, where the engines differ. Opt-in, because + WebKit only reaches a *.localhost stack from inside Traefik's network + namespace, which is what `make test-webkit` runs in. */ + ...(process.env.PLAYWRIGHT_WEBKIT + ? [ + { + name: 'webkit', + use: { + ...devices['Desktop Safari'], + storageState: 'playwright/.auth/user.json', + }, + testMatch: /-drag\.spec\.ts/, + dependencies: ['setup'], + }, + ] + : []), // { // name: 'Mobile Safari', diff --git a/frontend/scripts/capture-screenshots.mjs b/frontend/scripts/capture-screenshots.mjs index 831baba..615bfa5 100644 --- a/frontend/scripts/capture-screenshots.mjs +++ b/frontend/scripts/capture-screenshots.mjs @@ -223,10 +223,11 @@ async function capturePanels(page, dir) { } /** - * A media tile drawing what a camera published, where there is one. + * A media tile drawing what a camera published: the Demo's Media screen, + * whose house is drawn by a node as heat. * - * Skipped unless the media example is seeded (root `make seed-example-media`), - * since it is the one shot that needs a source of frames. The bytes arrive as + * Skipped unless the Demo is seeded (root `make seed-hosted-demo`), since it + * is the one shot that needs a source of frames. The bytes arrive as * a blob — pushed down the socket, or fetched with the session's credential, * which no `img` could carry on its own — so a `blob:` source is the proof the * whole path ran rather than that a picture is merely present. @@ -237,25 +238,23 @@ async function capturePanels(page, dir) { * where idleness only ever said the page stopped asking. */ async function captureMedia(page, dir) { - const answer = await page.goto(`${APP_URL}/view/camera`, { + const answer = await page.goto(`${APP_URL}/view/media`, { waitUntil: "domcontentloaded", }) if (!answer?.ok()) return - const picture = page.locator("img[alt='Test camera']") + const picture = page.locator("img[alt='The house, as heat']") try { await picture.waitFor({ timeout: 15000 }) await page.waitForFunction( () => document - .querySelector("img[alt='Test camera']") + .querySelector("img[alt='The house, as heat']") ?.src?.startsWith("blob:") ?? false, { timeout: 15000 }, ) } catch { - console.warn( - " media tile drew nothing — is `make seed-example-media` run?", - ) + console.warn(" media tile drew nothing — is the Demo seeded?") return } await page.waitForTimeout(500) @@ -296,7 +295,13 @@ async function captureWorkers(page, dir) { async function captureFlows(page, dir) { await page.goto(`${APP_URL}/flows`, { waitUntil: "networkidle" }) - if (await page.getByTestId("flow-card").count()) { + // The Demo's music flow, where it is seeded: two nodes, live values on the + // edges and the tiles at both ends, all still legible once fitted. The house + // has fifteen nodes, which fitted to the screen read as dots. + const demo = page.locator("[data-testid=flow-card][href$='/flows/music']") + if (await demo.count()) { + await demo.click() + } else if (await page.getByTestId("flow-card").count()) { await page.getByTestId("flow-card").first().click() } else { await page.getByTestId("new-flow").click() diff --git a/frontend/src/client/schemas.gen.ts b/frontend/src/client/schemas.gen.ts index 3349cee..222746c 100644 --- a/frontend/src/client/schemas.gen.ts +++ b/frontend/src/client/schemas.gen.ts @@ -828,6 +828,12 @@ export const DashboardSummarySchema = { title: 'Columns', default: 12 }, + shape: { + type: 'string', + enum: ['rect', 'round'], + title: 'Shape', + default: 'rect' + }, footprint: { items: { '$ref': '#/components/schemas/WidgetFootprint' @@ -946,10 +952,11 @@ export const EndpointSchema = { title: 'Endpoint', description: `Something wired into this flow that is not a node in it. -A dashboard control setting one of its messages, a tile showing one, or a -node in another flow on the far side of a dotted name. The canvas draws -these so a value never appears to come from nowhere — or worse, appears to -come from whichever node happens to be drawn as a producer.` +A dashboard control setting one of its messages, a tile showing one, a +port on a panel's own hardware, or a node in another flow on the far side +of a dotted name. The canvas draws these so a value never appears to come +from nowhere — or worse, appears to come from whichever node happens to be +drawn as a producer.` } as const; export const EnrollBodySchema = { @@ -1355,6 +1362,13 @@ export const FlowInput_InputSchema = { } ], title: 'Initial' + }, + origin: { + type: 'string', + enum: ['run', 'panel'], + title: 'Origin', + description: 'What writes it. A run input is a parameter a run passes in, a panel input is written by a dashboard or panel control. A hint only: run forms leave panel inputs out, the engine treats both the same.', + default: 'run' } }, type: 'object', @@ -1376,6 +1390,13 @@ export const FlowInput_OutputSchema = { } ], title: 'Initial' + }, + origin: { + type: 'string', + enum: ['run', 'panel'], + title: 'Origin', + description: 'What writes it. A run input is a parameter a run passes in, a panel input is written by a dashboard or panel control. A hint only: run forms leave panel inputs out, the engine treats both the same.', + default: 'run' } }, type: 'object', @@ -2522,6 +2543,92 @@ export const NodeSourceSchema = { description: 'The Python source of a node.' } as const; +export const NodeSourceSavedSchema = { + properties: { + id: { + type: 'string', + title: 'Id' + }, + status: { + type: 'string', + title: 'Status', + default: 'active' + }, + error: { + anyOf: [ + { + type: 'string' + }, + { + type: 'null' + } + ], + title: 'Error' + }, + health: { + type: 'string', + enum: ['ok', 'degraded', 'down'], + title: 'Health', + default: 'ok' + }, + health_detail: { + anyOf: [ + { + type: 'string' + }, + { + type: 'null' + } + ], + title: 'Health Detail' + }, + last_error: { + type: 'string', + title: 'Last Error', + default: '' + }, + last_error_ts: { + anyOf: [ + { + type: 'number' + }, + { + type: 'null' + } + ], + title: 'Last Error Ts' + }, + node: { + anyOf: [ + { + '$ref': '#/components/schemas/NodeDef-Output' + }, + { + type: 'null' + } + ] + }, + version: { + anyOf: [ + { + type: 'integer' + }, + { + type: 'null' + } + ], + title: 'Version' + } + }, + type: 'object', + required: ['id'], + title: 'NodeSourceSaved', + description: `Whether saved code loads, and what its header changed. + +\`node\` is set when the parameters of \`\`process\`\` added, dropped or renamed +an input port or setting. The draft moved on to \`version\` with it.` +} as const; + export const NodeStatusPublicSchema = { properties: { id: { @@ -2636,6 +2743,35 @@ export const NodeTypeInfoSchema = { description: 'A node type the editor can offer, with its parameter schema.' } as const; +export const NodeTypeTimingSchema = { + properties: { + type: { + type: 'string', + title: 'Type' + }, + nodes: { + type: 'integer', + title: 'Nodes' + }, + executions: { + type: 'integer', + title: 'Executions' + }, + avg_ms: { + type: 'number', + title: 'Avg Ms' + }, + max_ms: { + type: 'number', + title: 'Max Ms' + } + }, + type: 'object', + required: ['type', 'nodes', 'executions', 'avg_ms', 'max_ms'], + title: 'NodeTypeTiming', + description: 'How long one kind of node takes to run, across every node of it.' +} as const; + export const OAuthAuthorizeInfoSchema = { properties: { client_name: { @@ -2933,6 +3069,11 @@ export const PanelsConfigSchema = { }, type: 'array', title: 'Panels' + }, + version: { + type: 'integer', + title: 'Version', + default: 0 } }, type: 'object', @@ -2949,6 +3090,11 @@ export const PanelsPublicSchema = { type: 'array', title: 'Panels' }, + version: { + type: 'integer', + title: 'Version', + default: 0 + }, frontend_host: { type: 'string', title: 'Frontend Host', @@ -3014,6 +3160,39 @@ export const PlacementSchema = { description: "Where a widget sits in its section's grid, in grid units." } as const; +export const PortalMemberSchema = { + properties: { + user_id: { + type: 'string', + title: 'User Id' + }, + email: { + type: 'string', + title: 'Email' + }, + local_user_id: { + anyOf: [ + { + type: 'string', + format: 'uuid' + }, + { + type: 'null' + } + ], + title: 'Local User Id' + }, + linkable: { + type: 'boolean', + title: 'Linkable', + default: false + } + }, + type: 'object', + required: ['user_id', 'email'], + title: 'PortalMember' +} as const; + export const PrivateUserCreateSchema = { properties: { email: { @@ -3474,6 +3653,18 @@ export const RunDetailSchema = { title: 'Flow Version', default: 1 }, + declared: { + anyOf: [ + { + additionalProperties: true, + type: 'object' + }, + { + type: 'null' + } + ], + title: 'Declared' + }, nodes: { items: { '$ref': '#/components/schemas/RunNodeRow' @@ -3533,6 +3724,11 @@ export const RunNodeRowSchema = { type: 'string', title: 'Cached From', default: '' + }, + curve_lost: { + type: 'boolean', + title: 'Curve Lost', + default: false } }, type: 'object', @@ -4325,7 +4521,7 @@ export const ValidationIssueSchema = { properties: { code: { type: 'string', - enum: ['cycle', 'unconnected_input', 'missing_initial_value', 'node_error', 'unauthenticated_hook', 'self_loop_needs_initial', 'missing_source', 'node_unhealthy'], + enum: ['cycle', 'unconnected_input', 'missing_initial_value', 'node_error', 'unauthenticated_hook', 'self_loop_needs_initial', 'missing_source', 'node_unhealthy', 'ignored_device'], title: 'Code' }, message: { @@ -4603,6 +4799,21 @@ export const WorkerInfoSchema = { } ], title: 'Ram Mb' + }, + protocol: { + type: 'integer', + title: 'Protocol', + default: 0 + }, + version: { + type: 'string', + title: 'Version', + default: '' + }, + behind: { + type: 'boolean', + title: 'Behind', + default: false } }, type: 'object', @@ -4777,6 +4988,11 @@ export const fluksio__api__routes__observability__RunRowSchema = { type: 'string', title: 'Source' }, + parent: { + type: 'string', + title: 'Parent', + default: '' + }, status: { type: 'string', title: 'Status' diff --git a/frontend/src/client/sdk.gen.ts b/frontend/src/client/sdk.gen.ts index 8cab379..221429d 100644 --- a/frontend/src/client/sdk.gen.ts +++ b/frontend/src/client/sdk.gen.ts @@ -3,7 +3,7 @@ import type { CancelablePromise } from './core/CancelablePromise'; import { OpenAPI } from './core/OpenAPI'; import { request as __request } from './core/request'; -import type { AiReadAiConfigResponse, AiUpdateAiConfigData, AiUpdateAiConfigResponse, AiDeleteAiConfigResponse, AiListAiModelsData, AiListAiModelsResponse, AiReadAiStatusResponse, AiReadControlNodesResponse, AiReadChatResponse, AiChatData, AiChatResponse, AiClearChatResponse, AlertsReadAlertsConfigResponse, AlertsSaveAlertsConfigData, AlertsSaveAlertsConfigResponse, AlertsReadWebpushKeyResponse, AlertsAddWebpushSubscriptionData, AlertsAddWebpushSubscriptionResponse, AlertsRemoveWebpushSubscriptionData, AlertsRemoveWebpushSubscriptionResponse, AlertsTestChannelData, AlertsTestChannelResponse, ArtifactsPutArtifactData, ArtifactsPutArtifactResponse, ArtifactsGetArtifactData, ArtifactsGetArtifactResponse, CloudReadStatusResponse, CloudEnrollData, CloudEnrollResponse, CloudAddRemoteUserData, CloudAddRemoteUserResponse, CloudDisconnectResponse, ConnectorsReadConnectorStateData, ConnectorsReadConnectorStateResponse, ConnectorsClearConnectorStateData, ConnectorsClearConnectorStateResponse, ConnectorsDeleteConnectorStateData, ConnectorsDeleteConnectorStateResponse, DashboardsReadDashboardsResponse, DashboardsReadDashboardData, DashboardsReadDashboardResponse, DashboardsCreateDashboardData, DashboardsCreateDashboardResponse, DashboardsSaveDashboardData, DashboardsSaveDashboardResponse, DashboardsDeleteDashboardData, DashboardsDeleteDashboardResponse, DashboardsGenerateResultsDashboardData, DashboardsGenerateResultsDashboardResponse, DashboardsPublishDashboardData, DashboardsPublishDashboardResponse, DashboardsDiscardDashboardDraftData, DashboardsDiscardDashboardDraftResponse, DashboardsRenameDashboardData, DashboardsRenameDashboardResponse, FlavorsReadFlavorsResponse, FlavorsCreateFlavorData, FlavorsCreateFlavorResponse, FlavorsUpdateFlavorData, FlavorsUpdateFlavorResponse, FlavorsDeleteFlavorData, FlavorsDeleteFlavorResponse, FlowsReadFlowsResponse, FlowsReadNodeTypesResponse, FlowsReadGraphResponse, FlowsReadLibraryResponse, FlowsDeleteSharedNodeData, FlowsDeleteSharedNodeResponse, FlowsReadFlowData, FlowsReadFlowResponse, FlowsSaveFlowData, FlowsSaveFlowResponse, FlowsDeleteFlowData, FlowsDeleteFlowResponse, FlowsPublishFlowData, FlowsPublishFlowResponse, FlowsDiscardDraftData, FlowsDiscardDraftResponse, FlowsRenameFlowData, FlowsRenameFlowResponse, FlowsReadNodeSourceData, FlowsReadNodeSourceResponse, FlowsSaveNodeSourceData, FlowsSaveNodeSourceResponse, FlowsShareNodeData, FlowsShareNodeResponse, FlowsUnshareNodeData, FlowsUnshareNodeResponse, FlowsStartFlowData, FlowsStartFlowResponse, FlowsStopFlowData, FlowsStopFlowResponse, FlowsPauseFlowData, FlowsPauseFlowResponse, FlowsResumeFlowData, FlowsResumeFlowResponse, FlowsStepFlowData, FlowsStepFlowResponse, FlowsValidateFlowData, FlowsValidateFlowResponse, FlowsRunFlowData, FlowsRunFlowResponse, FlowsTriggerNodeData, FlowsTriggerNodeResponse, FlowsCancelNodeData, FlowsCancelNodeResponse, FlowsAcknowledgeNodeErrorData, FlowsAcknowledgeNodeErrorResponse, FlowsReadFlowStateData, FlowsReadFlowStateResponse, FlowsReadMessageHistoryData, FlowsReadMessageHistoryResponse, FlowsIssueSocketTicketResponse, LoginLoginAccessTokenData, LoginLoginAccessTokenResponse, LoginTestTokenResponse, LoginRecoverPasswordData, LoginRecoverPasswordResponse, LoginResetPasswordData, LoginResetPasswordResponse, LoginRecoverPasswordHtmlContentData, LoginRecoverPasswordHtmlContentResponse, MessagesReadMessagesResponse, MessagesPublishMessageData, MessagesPublishMessageResponse, MessagesReadMessageHistoryData, MessagesReadMessageHistoryResponse, ModulesReadModulesResponse, ModulesApplyModulesData, ModulesApplyModulesResponse, ModulesRefreshModulesResponse, OauthRegisterClientData, OauthRegisterClientResponse, OauthAuthorizeValidateData, OauthAuthorizeValidateResponse, OauthAuthorizeData, OauthAuthorizeResponse, OauthTokenData, OauthTokenResponse, OauthReadClientsResponse, OauthRevokeClientData, OauthRevokeClientResponse, ObservabilityReadSummaryResponse, ObservabilityReadTimeseriesData, ObservabilityReadTimeseriesResponse, ObservabilityReadFlowRollupsData, ObservabilityReadFlowRollupsResponse, ObservabilityReadRunsData, ObservabilityReadRunsResponse, ObservabilityReadEventsData, ObservabilityReadEventsResponse, ObservabilityReadDeadLettersData, ObservabilityReadDeadLettersResponse, PanelsReadPanelsResponse, PanelsSavePanelsData, PanelsSavePanelsResponse, PanelsStartPairingResponse, PanelsPollPairingData, PanelsPollPairingResponse, PanelsPendingDeviceData, PanelsPendingDeviceResponse, PanelsApprovePairingData, PanelsApprovePairingResponse, PanelsUnpairPanelData, PanelsUnpairPanelResponse, PanelsReadPanelData, PanelsReadPanelResponse, PrivateCreateUserData, PrivateCreateUserResponse, RunsCreateRunData, RunsCreateRunResponse, RunsCreateSweepData, RunsCreateSweepResponse, RunsImportRunsData, RunsImportRunsResponse, RunsReadRunsData, RunsReadRunsResponse, RunsDeleteRunsData, RunsDeleteRunsResponse, RunsReadOverviewResponse, RunsExportMetricsData, RunsExportMetricsResponse, RunsExportRunsData, RunsExportRunsResponse, RunsReadMetricNamesData, RunsReadMetricNamesResponse, RunsReadRunData, RunsReadRunResponse, RunsDeleteRunData, RunsDeleteRunResponse, RunsCancelRunData, RunsCancelRunResponse, RunsRetryRunData, RunsRetryRunResponse, RunsReadMetricsData, RunsReadMetricsResponse, RunsCompareMetricData, RunsCompareMetricResponse, SearchReadSearchIndexResponse, SecretsReadSecretsResponse, SecretsSaveSecretData, SecretsSaveSecretResponse, SecretsDeleteSecretData, SecretsDeleteSecretResponse, UsersReadUsersData, UsersReadUsersResponse, UsersCreateUserData, UsersCreateUserResponse, UsersReadUserMeResponse, UsersDeleteUserMeResponse, UsersUpdateUserMeData, UsersUpdateUserMeResponse, UsersUpdatePasswordMeData, UsersUpdatePasswordMeResponse, UsersRegisterUserData, UsersRegisterUserResponse, UsersReadUserByIdData, UsersReadUserByIdResponse, UsersUpdateUserData, UsersUpdateUserResponse, UsersDeleteUserData, UsersDeleteUserResponse, UtilsTestEmailData, UtilsTestEmailResponse, UtilsReadConfigResponse, UtilsHealthCheckResponse, UtilsHealthResponse, WorkersReadWorkersResponse, WorkersReadResourcesResponse, WorkersReadTokensResponse, WorkersIssueTokenData, WorkersIssueTokenResponse, WorkersRevokeTokenData, WorkersRevokeTokenResponse, WorkersReadRuntimeResponse } from './types.gen'; +import type { AiReadAiConfigResponse, AiUpdateAiConfigData, AiUpdateAiConfigResponse, AiDeleteAiConfigResponse, AiListAiModelsData, AiListAiModelsResponse, AiReadAiStatusResponse, AiReadControlNodesResponse, AiReadChatResponse, AiChatData, AiChatResponse, AiClearChatResponse, AlertsReadAlertsConfigResponse, AlertsSaveAlertsConfigData, AlertsSaveAlertsConfigResponse, AlertsReadWebpushKeyResponse, AlertsAddWebpushSubscriptionData, AlertsAddWebpushSubscriptionResponse, AlertsRemoveWebpushSubscriptionData, AlertsRemoveWebpushSubscriptionResponse, AlertsTestChannelData, AlertsTestChannelResponse, ArtifactsPutArtifactData, ArtifactsPutArtifactResponse, ArtifactsGetArtifactData, ArtifactsGetArtifactResponse, ArtifactsDeleteArtifactData, ArtifactsDeleteArtifactResponse, CloudReadStatusResponse, CloudEnrollData, CloudEnrollResponse, CloudAddRemoteUserData, CloudAddRemoteUserResponse, CloudReadMembersResponse, CloudLinkMemberData, CloudLinkMemberResponse, CloudRevokeMemberData, CloudRevokeMemberResponse, CloudDisconnectResponse, ConnectorsReadConnectorStateData, ConnectorsReadConnectorStateResponse, ConnectorsClearConnectorStateData, ConnectorsClearConnectorStateResponse, ConnectorsDeleteConnectorStateData, ConnectorsDeleteConnectorStateResponse, DashboardsReadDashboardsResponse, DashboardsReadDashboardData, DashboardsReadDashboardResponse, DashboardsCreateDashboardData, DashboardsCreateDashboardResponse, DashboardsSaveDashboardData, DashboardsSaveDashboardResponse, DashboardsDeleteDashboardData, DashboardsDeleteDashboardResponse, DashboardsGenerateResultsDashboardData, DashboardsGenerateResultsDashboardResponse, DashboardsPublishDashboardData, DashboardsPublishDashboardResponse, DashboardsDiscardDashboardDraftData, DashboardsDiscardDashboardDraftResponse, DashboardsRenameDashboardData, DashboardsRenameDashboardResponse, FlavorsReadFlavorsResponse, FlavorsCreateFlavorData, FlavorsCreateFlavorResponse, FlavorsUpdateFlavorData, FlavorsUpdateFlavorResponse, FlavorsDeleteFlavorData, FlavorsDeleteFlavorResponse, FlowsReadFlowsResponse, FlowsReadNodeTypesResponse, FlowsReadGraphResponse, FlowsReadLibraryResponse, FlowsDeleteSharedNodeData, FlowsDeleteSharedNodeResponse, FlowsReadFlowData, FlowsReadFlowResponse, FlowsSaveFlowData, FlowsSaveFlowResponse, FlowsDeleteFlowData, FlowsDeleteFlowResponse, FlowsPublishFlowData, FlowsPublishFlowResponse, FlowsDiscardDraftData, FlowsDiscardDraftResponse, FlowsRenameFlowData, FlowsRenameFlowResponse, FlowsReadNodeSourceData, FlowsReadNodeSourceResponse, FlowsSaveNodeSourceData, FlowsSaveNodeSourceResponse, FlowsShareNodeData, FlowsShareNodeResponse, FlowsUnshareNodeData, FlowsUnshareNodeResponse, FlowsStartFlowData, FlowsStartFlowResponse, FlowsStopFlowData, FlowsStopFlowResponse, FlowsPauseFlowData, FlowsPauseFlowResponse, FlowsResumeFlowData, FlowsResumeFlowResponse, FlowsStepFlowData, FlowsStepFlowResponse, FlowsValidateFlowData, FlowsValidateFlowResponse, FlowsRunFlowData, FlowsRunFlowResponse, FlowsTriggerNodeData, FlowsTriggerNodeResponse, FlowsCancelNodeData, FlowsCancelNodeResponse, FlowsAcknowledgeNodeErrorData, FlowsAcknowledgeNodeErrorResponse, FlowsReadFlowStateData, FlowsReadFlowStateResponse, FlowsReadMessageHistoryData, FlowsReadMessageHistoryResponse, FlowsIssueSocketTicketResponse, LoginLoginAccessTokenData, LoginLoginAccessTokenResponse, LoginTestTokenResponse, LoginRecoverPasswordData, LoginRecoverPasswordResponse, LoginResetPasswordData, LoginResetPasswordResponse, LoginRecoverPasswordHtmlContentData, LoginRecoverPasswordHtmlContentResponse, MessagesReadMessagesResponse, MessagesPublishMessageData, MessagesPublishMessageResponse, MessagesReadMessageHistoryData, MessagesReadMessageHistoryResponse, ModulesReadModulesResponse, ModulesApplyModulesData, ModulesApplyModulesResponse, ModulesRefreshModulesResponse, OauthRegisterClientData, OauthRegisterClientResponse, OauthAuthorizeValidateData, OauthAuthorizeValidateResponse, OauthAuthorizeData, OauthAuthorizeResponse, OauthTokenData, OauthTokenResponse, OauthReadClientsResponse, OauthRevokeClientData, OauthRevokeClientResponse, ObservabilityReadSummaryResponse, ObservabilityReadTimeseriesData, ObservabilityReadTimeseriesResponse, ObservabilityReadFlowRollupsData, ObservabilityReadFlowRollupsResponse, ObservabilityReadNodeTypeTimingsData, ObservabilityReadNodeTypeTimingsResponse, ObservabilityReadRunsData, ObservabilityReadRunsResponse, ObservabilityReadEventsData, ObservabilityReadEventsResponse, ObservabilityReadDeadLettersData, ObservabilityReadDeadLettersResponse, ObservabilityFlushMetricsResponse, PanelsReadPanelsResponse, PanelsSavePanelsData, PanelsSavePanelsResponse, PanelsStartPairingResponse, PanelsPollPairingData, PanelsPollPairingResponse, PanelsPendingDeviceData, PanelsPendingDeviceResponse, PanelsApprovePairingData, PanelsApprovePairingResponse, PanelsUnpairPanelData, PanelsUnpairPanelResponse, PanelsReadPanelData, PanelsReadPanelResponse, PrivateCreateUserData, PrivateCreateUserResponse, RunsCreateRunData, RunsCreateRunResponse, RunsCreateSweepData, RunsCreateSweepResponse, RunsImportRunsData, RunsImportRunsResponse, RunsReadRunsData, RunsReadRunsResponse, RunsDeleteRunsData, RunsDeleteRunsResponse, RunsReadOverviewResponse, RunsExportMetricsData, RunsExportMetricsResponse, RunsExportRunsData, RunsExportRunsResponse, RunsReadMetricNamesData, RunsReadMetricNamesResponse, RunsReadRunData, RunsReadRunResponse, RunsDeleteRunData, RunsDeleteRunResponse, RunsReadNodeOutputsData, RunsReadNodeOutputsResponse, RunsCancelRunData, RunsCancelRunResponse, RunsRetryRunData, RunsRetryRunResponse, RunsReadMetricsData, RunsReadMetricsResponse, RunsCompareMetricData, RunsCompareMetricResponse, SearchReadSearchIndexResponse, SecretsReadSecretsResponse, SecretsSaveSecretData, SecretsSaveSecretResponse, SecretsDeleteSecretData, SecretsDeleteSecretResponse, UsersReadUsersData, UsersReadUsersResponse, UsersCreateUserData, UsersCreateUserResponse, UsersReadUserMeResponse, UsersDeleteUserMeResponse, UsersUpdateUserMeData, UsersUpdateUserMeResponse, UsersUpdatePasswordMeData, UsersUpdatePasswordMeResponse, UsersRegisterUserData, UsersRegisterUserResponse, UsersReadUserByIdData, UsersReadUserByIdResponse, UsersUpdateUserData, UsersUpdateUserResponse, UsersDeleteUserData, UsersDeleteUserResponse, UtilsTestEmailData, UtilsTestEmailResponse, UtilsReadConfigResponse, UtilsHealthCheckResponse, UtilsHealthResponse, WorkersReadWorkersResponse, WorkersReadResourcesResponse, WorkersReadTokensResponse, WorkersIssueTokenData, WorkersIssueTokenResponse, WorkersRevokeTokenData, WorkersRevokeTokenResponse, WorkersReadRuntimeResponse } from './types.gen'; export class AiService { /** @@ -115,8 +115,9 @@ export class AiService { * Chat * One turn, streamed as server-sent events. * - * ``message``, ``tool_call`` and ``tool_result`` events as the model works, - * ``error`` when something stops it, and ``done`` last either way. + * ``delta`` as the model writes, ``message`` with a round's whole text, + * ``tool_call`` and ``tool_result`` as it works, ``error`` when something + * stops it, and ``done`` last either way. * @param data The data for the request. * @param data.requestBody * @returns unknown Successful Response @@ -320,6 +321,33 @@ export class ArtifactsService { } }); } + + /** + * Delete Artifact + * Remove one artifact's bytes from the store. + * + * A superuser's call, because the store records no uploader and identical + * content from two callers is one file, so nobody else can be said to own + * it. Refused while anything still names the digest, the same references the + * hourly sweep keeps, so a reference that exists never answers 404. The + * volatile ring is left alone: its frames fall out on their own. + * @param data The data for the request. + * @param data.digest + * @returns void Successful Response + * @throws ApiError + */ + public static deleteArtifact(data: ArtifactsDeleteArtifactData): CancelablePromise { + return __request(OpenAPI, { + method: 'DELETE', + url: '/api/v1/artifacts/{digest}', + path: { + digest: data.digest + }, + errors: { + 422: 'Validation Error' + } + }); + } } export class CloudService { @@ -393,13 +421,82 @@ export class CloudService { }); } + /** + * Read Members + * Everyone the portal admits to this instance, its owner aside. + * + * A share link admits somebody at the portal before this instance has heard + * of them, so this is the one place a superuser here can see who may still + * arrive. It also shows a membership left standing when a local user was + * deleted while the portal could not be told. + * @returns PortalMember Successful Response + * @throws ApiError + */ + public static readMembers(): CancelablePromise { + return __request(OpenAPI, { + method: 'GET', + url: '/api/v1/cloud/members' + }); + } + + /** + * Link Member + * Let a pending member in as the local user that already has their email. + * + * Arriving on their own they are refused, because handing an existing + * account to whoever holds that address at the portal is not a guess to + * make. This is a superuser deciding it, and never for a superuser's account. + * @param data The data for the request. + * @param data.portalSub + * @returns UserPublic Successful Response + * @throws ApiError + */ + public static linkMember(data: CloudLinkMemberData): CancelablePromise { + return __request(OpenAPI, { + method: 'POST', + url: '/api/v1/cloud/members/{portal_sub}/link', + path: { + portal_sub: data.portalSub + }, + errors: { + 422: 'Validation Error' + } + }); + } + + /** + * Revoke Member + * Take a portal membership back. + * + * For somebody who has not arrived, this is the whole of their access. + * Somebody who has is deleted under Admin → Users instead, which also does + * this. + * @param data The data for the request. + * @param data.portalSub + * @returns Message Successful Response + * @throws ApiError + */ + public static revokeMember(data: CloudRevokeMemberData): CancelablePromise { + return __request(OpenAPI, { + method: 'DELETE', + url: '/api/v1/cloud/members/{portal_sub}', + path: { + portal_sub: data.portalSub + }, + errors: { + 422: 'Validation Error' + } + }); + } + /** * Disconnect * Sever the connection from this side. * * Unilateral and immediate: the config is the only reason portal tokens * verify here, so deleting it ends remote access whatever the portal still - * has on file. + * has on file. The users the portal let in go with it. The account it was + * enrolled as stays, and so does every superuser. * @returns Message Successful Response * @throws ApiError */ @@ -819,8 +916,12 @@ export class FlowsService { /** * Delete Shared Node * Remove a shared source, as long as no flow still runs it. + * + * With ``detach``, every node using it keeps a copy of the code as its own + * first, so the source can be retired while flows still use it. * @param data The data for the request. * @param data.libName + * @param data.detach * @returns Message Successful Response * @throws ApiError */ @@ -831,6 +932,9 @@ export class FlowsService { path: { lib_name: data.libName }, + query: { + detach: data.detach + }, errors: { 422: 'Validation Error' } @@ -1010,12 +1114,17 @@ export class FlowsService { * * A shared node writes to the library, so the fix reaches every flow using * it — and that one is live immediately rather than waiting for a publish, - * because the copy is not any single flow's to hold back. + * because the copy is not any single flow's to hold back. Being live, it is + * audited like a publish. + * + * A node with its own code takes its input ports and settings from the + * header of ``process`` (see `follow_header`), and the answer carries the + * node when that changed it. * @param data The data for the request. * @param data.name * @param data.nodeId * @param data.requestBody - * @returns NodeStatusPublic Successful Response + * @returns NodeSourceSaved Successful Response * @throws ApiError */ public static saveNodeSource(data: FlowsSaveNodeSourceData): CancelablePromise { @@ -1659,6 +1768,9 @@ export class OauthService { /** * Authorize * Approve a client, on behalf of the signed-in user. + * + * Through the hub the endpoint is the one there, which is the resource an + * agent that came that way asks for. * @param data The data for the request. * @param data.requestBody * @returns OAuthAuthorizeResponse Successful Response @@ -1810,6 +1922,30 @@ export class ObservabilityService { }); } + /** + * Read Node Type Timings + * Execution time per node type, slowest first, to point at the hotspots. + * + * The rollups are kept per node, so the type is joined on from the published + * flows. A node deleted since it ran has no type left to count under. + * @param data The data for the request. + * @param data.hours + * @returns NodeTypeTiming Successful Response + * @throws ApiError + */ + public static readNodeTypeTimings(data: ObservabilityReadNodeTypeTimingsData = {}): CancelablePromise { + return __request(OpenAPI, { + method: 'GET', + url: '/api/v1/observability/node-types', + query: { + hours: data.hours + }, + errors: { + 422: 'Validation Error' + } + }); + } + /** * Read Runs * Recent cascades, newest first, and how many there were in total. @@ -1899,6 +2035,22 @@ export class ObservabilityService { } }); } + + /** + * Flush Metrics + * Write what the metrics collector holds now, not at its next interval. + * + * For tooling that reads or clears these tables right after causing events, + * such as the soak harness clearing its own rows. + * @returns Message Successful Response + * @throws ApiError + */ + public static flushMetrics(): CancelablePromise { + return __request(OpenAPI, { + method: 'POST', + url: '/api/v1/observability/flush' + }); + } } export class PanelsService { @@ -1922,6 +2074,11 @@ export class PanelsService { * A panel that disappears here takes its credential with it, so this is also * how a device is unpaired along with its assignment. Re-pairing one screen * and keeping the assignment is ``/{panel_id}/unpair`` below. + * + * The body carries the ``version`` it was based on, and a save someone else + * has moved past is refused with 409, as a dashboard's is. Two clients each + * writing back the whole list would otherwise drop each other's panels, and + * a dropped panel revokes its screen at once. * @param data The data for the request. * @param data.requestBody * @returns PanelsPublic Successful Response @@ -2244,8 +2401,13 @@ export class RunsService { * deleting a flow leaves its runs standing on purpose, so this is the only * way to clear a history without a call per run. ``flow`` is required — * there is no "delete every run of everything" here, by design. + * + * ``history`` also drops the flow's rows in the run history (``flow_run``), + * its cascades included. That is for a flow being deleted, which would + * otherwise stay in the history until the retention window passes. * @param data The data for the request. * @param data.flow + * @param data.history * @returns Message Successful Response * @throws ApiError */ @@ -2254,7 +2416,8 @@ export class RunsService { method: 'DELETE', url: '/api/v1/runs', query: { - flow: data.flow + flow: data.flow, + history: data.history }, errors: { 422: 'Validation Error' @@ -2450,6 +2613,32 @@ export class RunsService { }); } + /** + * Read Node Outputs + * What one node returned, by message, as the stage cache stored it. + * + * Kept only for a node that succeeded and may be reused, and only up to + * 256 kB of JSON. Null when the node published nothing. + * @param data The data for the request. + * @param data.runId + * @param data.node + * @returns unknown Successful Response + * @throws ApiError + */ + public static readNodeOutputs(data: RunsReadNodeOutputsData): CancelablePromise { + return __request(OpenAPI, { + method: 'GET', + url: '/api/v1/runs/{run_id}/nodes/{node}/outputs', + path: { + run_id: data.runId, + node: data.node + }, + errors: { + 422: 'Validation Error' + } + }); + } + /** * Cancel Run * Stop a run. One already past its last node is left as it finished. diff --git a/frontend/src/client/types.gen.ts b/frontend/src/client/types.gen.ts index 0f8c144..f825088 100644 --- a/frontend/src/client/types.gen.ts +++ b/frontend/src/client/types.gen.ts @@ -215,6 +215,7 @@ export type DashboardSummary = { has_draft?: boolean; version?: number; columns?: number; + shape?: 'rect' | 'round'; footprint?: Array; updated_at?: (string | null); }; @@ -256,10 +257,11 @@ export type DType = 'float' | 'int' | 'str' | 'bool' | 'json' | 'series' | 'reco /** * Something wired into this flow that is not a node in it. * - * A dashboard control setting one of its messages, a tile showing one, or a - * node in another flow on the far side of a dotted name. The canvas draws - * these so a value never appears to come from nowhere — or worse, appears to - * come from whichever node happens to be drawn as a producer. + * A dashboard control setting one of its messages, a tile showing one, a + * port on a panel's own hardware, or a node in another flow on the far side + * of a dotted name. The canvas draws these so a value never appears to come + * from nowhere — or worse, appears to come from whichever node happens to be + * drawn as a producer. */ export type Endpoint = { kind: string; @@ -391,14 +393,27 @@ export type FlowDetail = { export type FlowInput_Input = { spec: MessageSpec; initial?: (unknown | null); + /** + * What writes it. A run input is a parameter a run passes in, a panel input is written by a dashboard or panel control. A hint only: run forms leave panel inputs out, the engine treats both the same. + */ + origin?: 'run' | 'panel'; }; +/** + * What writes it. A run input is a parameter a run passes in, a panel input is written by a dashboard or panel control. A hint only: run forms leave panel inputs out, the engine treats both the same. + */ +export type origin = 'run' | 'panel'; + /** * A message the flow starts with rather than computes. */ export type FlowInput_Output = { spec: MessageSpec; initial?: (unknown | null); + /** + * What writes it. A run input is a parameter a run passes in, a panel input is written by a dashboard or panel control. A hint only: run forms leave panel inputs out, the engine treats both the same. + */ + origin?: 'run' | 'panel'; }; /** @@ -506,6 +521,7 @@ export type fluksio__api__routes__observability__RunRow = { id: string; flow: string; source: string; + parent?: string; status: string; started_at: string; finished_at?: (string | null); @@ -872,6 +888,26 @@ export type NodeSource = { missing?: boolean; }; +/** + * Whether saved code loads, and what its header changed. + * + * `node` is set when the parameters of ``process`` added, dropped or renamed + * an input port or setting. The draft moved on to `version` with it. + */ +export type NodeSourceSaved = { + id: string; + status?: string; + error?: (string | null); + health?: 'ok' | 'degraded' | 'down'; + health_detail?: (string | null); + last_error?: string; + last_error_ts?: (number | null); + node?: (NodeDef_Output | null); + version?: (number | null); +}; + +export type health = 'ok' | 'degraded' | 'down'; + /** * Whether a node loaded, and how its connection is doing. */ @@ -885,8 +921,6 @@ export type NodeStatusPublic = { last_error_ts?: (number | null); }; -export type health = 'ok' | 'degraded' | 'down'; - /** * A node type the editor can offer, with its parameter schema. */ @@ -903,6 +937,17 @@ export type NodeTypeInfo = { plugin?: (string | null); }; +/** + * How long one kind of node takes to run, across every node of it. + */ +export type NodeTypeTiming = { + type: string; + nodes: number; + executions: number; + avg_ms: number; + max_ms: number; +}; + /** * What the consent page shows, all of it validated server-side. */ @@ -993,6 +1038,7 @@ export type direction = 'in' | 'out'; */ export type PanelsConfig = { panels?: Array; + version?: number; }; /** @@ -1008,6 +1054,7 @@ export type PanelsConfig = { */ export type PanelsPublic = { panels?: Array; + version?: number; frontend_host?: string; }; @@ -1029,6 +1076,13 @@ export type Placement = { h?: number; }; +export type PortalMember = { + user_id: string; + email: string; + local_user_id?: (string | null); + linkable?: boolean; +}; + export type PrivateUserCreate = { email: string; password: string; @@ -1173,6 +1227,9 @@ export type RunDetail = { [key: string]: unknown; }; flow_version?: number; + declared?: ({ + [key: string]: unknown; +} | null); nodes?: Array; artifacts?: Array; }; @@ -1187,6 +1244,7 @@ export type RunNodeRow = { logs: string; cache_key?: string; cached_from?: string; + curve_lost?: boolean; }; export type RunPage = { @@ -1405,7 +1463,7 @@ export type ValidationError = { * Something wrong with a flow — a fault, or merely advisory. */ export type ValidationIssue = { - code: 'cycle' | 'unconnected_input' | 'missing_initial_value' | 'node_error' | 'unauthenticated_hook' | 'self_loop_needs_initial' | 'missing_source' | 'node_unhealthy'; + code: 'cycle' | 'unconnected_input' | 'missing_initial_value' | 'node_error' | 'unauthenticated_hook' | 'self_loop_needs_initial' | 'missing_source' | 'node_unhealthy' | 'ignored_device'; message: string; flow?: string; nodes?: Array<(string)>; @@ -1418,7 +1476,7 @@ export type ValidationIssue = { readonly advisory: boolean; }; -export type code = 'cycle' | 'unconnected_input' | 'missing_initial_value' | 'node_error' | 'unauthenticated_hook' | 'self_loop_needs_initial' | 'missing_source' | 'node_unhealthy'; +export type code = 'cycle' | 'unconnected_input' | 'missing_initial_value' | 'node_error' | 'unauthenticated_hook' | 'self_loop_needs_initial' | 'missing_source' | 'node_unhealthy' | 'ignored_device'; export type ValidationResult = { issues?: Array; @@ -1507,6 +1565,9 @@ export type WorkerInfo = { cpus?: number; gpus?: number; ram_mb?: (number | null); + protocol?: number; + version?: string; + behind?: boolean; }; /** @@ -1594,6 +1655,12 @@ export type ArtifactsGetArtifactData = { export type ArtifactsGetArtifactResponse = (unknown); +export type ArtifactsDeleteArtifactData = { + digest: string; +}; + +export type ArtifactsDeleteArtifactResponse = (void); + export type CloudReadStatusResponse = ({ [key: string]: unknown; }); @@ -1610,6 +1677,20 @@ export type CloudAddRemoteUserData = { export type CloudAddRemoteUserResponse = (UserPublic); +export type CloudReadMembersResponse = (Array); + +export type CloudLinkMemberData = { + portalSub: string; +}; + +export type CloudLinkMemberResponse = (UserPublic); + +export type CloudRevokeMemberData = { + portalSub: string; +}; + +export type CloudRevokeMemberResponse = (Message); + export type CloudDisconnectResponse = (Message); export type ConnectorsReadConnectorStateData = { @@ -1715,6 +1796,7 @@ export type FlowsReadGraphResponse = (BrainGraph); export type FlowsReadLibraryResponse = (Array); export type FlowsDeleteSharedNodeData = { + detach?: boolean; libName: string; }; @@ -1772,7 +1854,7 @@ export type FlowsSaveNodeSourceData = { requestBody: NodeSource; }; -export type FlowsSaveNodeSourceResponse = (NodeStatusPublic); +export type FlowsSaveNodeSourceResponse = (NodeSourceSaved); export type FlowsShareNodeData = { name: string; @@ -1972,6 +2054,12 @@ export type ObservabilityReadFlowRollupsData = { export type ObservabilityReadFlowRollupsResponse = (Array); +export type ObservabilityReadNodeTypeTimingsData = { + hours?: number; +}; + +export type ObservabilityReadNodeTypeTimingsResponse = (Array); + export type ObservabilityReadRunsData = { flow?: (string | null); limit?: number; @@ -1999,6 +2087,8 @@ export type ObservabilityReadDeadLettersData = { export type ObservabilityReadDeadLettersResponse = (Array); +export type ObservabilityFlushMetricsResponse = (Message); + export type PanelsReadPanelsResponse = (PanelsPublic); export type PanelsSavePanelsData = { @@ -2082,6 +2172,7 @@ export type RunsReadRunsResponse = (Array); export type RunsDeleteRunsData = { flow: string; + history?: boolean; }; export type RunsDeleteRunsResponse = (Message); @@ -2139,6 +2230,15 @@ export type RunsDeleteRunData = { export type RunsDeleteRunResponse = (void); +export type RunsReadNodeOutputsData = { + node: string; + runId: string; +}; + +export type RunsReadNodeOutputsResponse = (({ + [key: string]: unknown; +} | null)); + export type RunsCancelRunData = { runId: string; }; diff --git a/frontend/src/components/Admin/PortalMembers.tsx b/frontend/src/components/Admin/PortalMembers.tsx new file mode 100644 index 0000000..240ec03 --- /dev/null +++ b/frontend/src/components/Admin/PortalMembers.tsx @@ -0,0 +1,181 @@ +import { useMutation, useQuery, useQueryClient } from "@tanstack/react-query" +import { useState } from "react" + +import { CloudService, type PortalMember } from "@/client" +import { InfoTip, PANEL_SECTION } from "@/components/Flow/SidePanel" +import { Button } from "@/components/ui/button" +import { + Dialog, + DialogContent, + DialogDescription, + DialogFooter, + DialogHeader, + DialogTitle, +} from "@/components/ui/dialog" +import useCustomToast from "@/hooks/useCustomToast" +import { handleError } from "@/utils" + +const membersKey = ["cloud", "members"] + +/** Portal members who have no user here yet, and the way to take one back. + * A share link admits somebody at the portal before this instance hears of + * them, so without this list nobody here could see who may still arrive. + * Those who have arrived are users above, deleted there. A member whose email + * a local user already has is refused on arrival until linked to that user + * here. */ +export function PortalMembers() { + const { data: status } = useQuery({ + queryKey: ["cloud", "status"], + queryFn: async () => + (await CloudService.readStatus()) as { + enrolled: boolean + revoked: boolean + }, + }) + // A revoked link's credential no longer lists anything at the portal. + const enrolled = Boolean(status?.enrolled && !status.revoked) + const { data, isError } = useQuery({ + queryKey: membersKey, + queryFn: () => CloudService.readMembers(), + enabled: enrolled, + retry: false, + }) + const queryClient = useQueryClient() + const { showSuccessToast, showErrorToast } = useCustomToast() + const [pending, setPending] = useState(null) + const [linking, setLinking] = useState(null) + + const revoke = useMutation({ + mutationFn: (portalSub: string) => CloudService.revokeMember({ portalSub }), + onSuccess: (message) => { + showSuccessToast(message.message) + setPending(null) + }, + onError: handleError.bind(showErrorToast), + onSettled: () => { + queryClient.invalidateQueries({ queryKey: membersKey }) + }, + }) + + const link = useMutation({ + mutationFn: (portalSub: string) => CloudService.linkMember({ portalSub }), + onSuccess: (user) => { + showSuccessToast(`Linked ${user.email}`) + setLinking(null) + queryClient.invalidateQueries({ queryKey: ["users"] }) + }, + onError: handleError.bind(showErrorToast), + onSettled: () => { + queryClient.invalidateQueries({ queryKey: membersKey }) + }, + }) + + if (!enrolled) return null + const waiting = (data ?? []).filter((member) => !member.local_user_id) + + return ( +
+
+

Pending portal members

+ + Admitted at the portal, with no user here until they first open this + instance. Revoking takes the invitation back. + +
+ {isError ? ( +

+ Could not reach the portal. +

+ ) : waiting.length === 0 ? ( +

Nobody pending.

+ ) : ( +
+ {waiting.map((member) => ( +
+ {member.email} +
+ {member.linkable && ( + + )} + +
+
+ ))} +
+ )} + + !open && setPending(null)} + > + + + Revoke {pending?.email}? + + They lose access through the portal and need a new invitation to + get it back. + + + + + + + + + + !open && setLinking(null)} + > + + + Link {linking?.email}? + + Through the portal they open this instance as the local user with + this email, who keeps their password. Removing them at the portal, + or disconnecting it, then deletes that user. + + + + + + + + +
+ ) +} + +export default PortalMembers diff --git a/frontend/src/components/Chat/ChatPanel.tsx b/frontend/src/components/Chat/ChatPanel.tsx index 1932482..63ef012 100644 --- a/frontend/src/components/Chat/ChatPanel.tsx +++ b/frontend/src/components/Chat/ChatPanel.tsx @@ -1,7 +1,7 @@ import { queryOptions, useQuery, useQueryClient } from "@tanstack/react-query" import { useRouterState } from "@tanstack/react-router" import { Bird, Bot, MessageCircle, Send, Square, Trash2 } from "lucide-react" -import { useEffect, useRef, useState } from "react" +import { lazy, Suspense, useEffect, useRef, useState } from "react" import { AiService, type ChatItem } from "@/client" import { flowKeys } from "@/components/Flow/queries" @@ -23,6 +23,9 @@ import { type ChatTab, chatStore, useChat } from "./chatStore" import { type ChatEvent, streamTurn } from "./stream" import { type Action, type ControlItem, useControl } from "./useControl" +/** The renderer arrives with the first answer, not with the app. */ +const Markdown = lazy(() => import("./Markdown")) + export const aiStatusQueryOptions = () => queryOptions({ queryKey: ["ai", "status"], @@ -172,10 +175,12 @@ export function ChatPanel() { .finally(() => setLoaded(true)) }, [open, loaded, available]) - // biome-ignore lint/correctness/useExhaustiveDependencies: scroll when the list grows, not on every render. + const tail = shown[shown.length - 1] + const tailLength = tail && "text" in tail ? tail.text.length : 0 + // biome-ignore lint/correctness/useExhaustiveDependencies: scroll when the list or a streamed answer grows, not on every render. useEffect(() => { bottom.current?.scrollIntoView({ block: "end" }) - }, [shown.length, waiting]) + }, [shown.length, tailLength, waiting]) const send = async () => { const text = draft.trim() @@ -187,11 +192,18 @@ export function ChatPanel() { controller.current = abort const onEvent = (event: ChatEvent) => { switch (event.type) { + // A round's text grows in the last item and is settled by `message`; + // a tool call or the next turn always comes between two rounds. + case "delta": case "message": - setItems((prev) => [ - ...prev, - { kind: "assistant", text: event.content }, - ]) + setItems((prev) => { + const last = prev[prev.length - 1] + if (last?.kind !== "assistant") + return [...prev, { kind: "assistant", text: event.content }] + const text = + event.type === "delta" ? last.text + event.content : event.content + return [...prev.slice(0, -1), { kind: "assistant", text }] + }) break case "tool_call": setItems((prev) => [ @@ -382,8 +394,14 @@ function Message({ item }: { item: Item }) { ) case "assistant": return ( -
- {item.text} +
+ {item.text}
+ } + > + +
) case "error": diff --git a/frontend/src/components/Chat/Markdown.tsx b/frontend/src/components/Chat/Markdown.tsx new file mode 100644 index 0000000..b12783d --- /dev/null +++ b/frontend/src/components/Chat/Markdown.tsx @@ -0,0 +1,57 @@ +import ReactMarkdown, { type Components } from "react-markdown" + +/** + * An assistant's answer as markdown, compact and on the panel's text size. + * + * Raw HTML is never rendered, and images are dropped so an answer cannot make + * the browser fetch a URL on its own. A link opens in a new tab when clicked. + */ +const components: Components = { + a: ({ node: _, ...props }) => ( + + ), + ul: ({ node: _, ...props }) => ( +
    + ), + ol: ({ node: _, ...props }) => ( +
      + ), + h1: ({ node: _, ...props }) =>

      , + h2: ({ node: _, ...props }) =>

      , + h3: ({ node: _, ...props }) =>

      , + h4: ({ node: _, ...props }) =>

      , + blockquote: ({ node: _, ...props }) => ( +
      + ), + hr: () =>
      , + pre: ({ node: _, ...props }) => ( +
      +  ),
      +  code: ({ node: _, ...props }) => (
      +    
      +  ),
      +}
      +
      +export default function Markdown({ text }: { text: string }) {
      +  return (
      +    
      + + {text} + +
      + ) +} diff --git a/frontend/src/components/Chat/stream.ts b/frontend/src/components/Chat/stream.ts index bc8b276..6a0eea1 100644 --- a/frontend/src/components/Chat/stream.ts +++ b/frontend/src/components/Chat/stream.ts @@ -3,6 +3,7 @@ import { apiToken } from "@/lib/portal" export type ChatEvent = | { type: "start" } + | { type: "delta"; content: string } | { type: "message"; content: string } | { type: "tool_call"; name: string; arguments: Record } | { type: "tool_result"; name: string; ok: boolean; error?: string } diff --git a/frontend/src/components/Common/ConnectionNotice.tsx b/frontend/src/components/Common/ConnectionNotice.tsx index f4675f7..03954d2 100644 --- a/frontend/src/components/Common/ConnectionNotice.tsx +++ b/frontend/src/components/Common/ConnectionNotice.tsx @@ -1,9 +1,9 @@ import { useEffect, useSyncExternalStore } from "react" -import { ago } from "@/components/Health/queries" import { connectionStore } from "@/lib/connectionStore" import { dismiss, notify } from "@/lib/notificationStore" import { isPortal } from "@/lib/portal" +import { ago } from "@/lib/utils" /** One card, replaced in place, rather than one per poll that confirms it. */ const ID = "connection-offline" diff --git a/frontend/src/components/Common/DashboardMosaic.tsx b/frontend/src/components/Common/DashboardMosaic.tsx index a3e5cb7..cd623eb 100644 --- a/frontend/src/components/Common/DashboardMosaic.tsx +++ b/frontend/src/components/Common/DashboardMosaic.tsx @@ -65,6 +65,9 @@ function blocksOf(footprint: WidgetFootprint[]): { * dashboard. The summary carries the shapes, so it costs no request either. */ function Footprint({ dashboard }: { dashboard: DashboardSummary }) { + if (dashboard.shape === "round") { + return + } const { blocks, rows } = blocksOf(dashboard.footprint ?? []) const columns = dashboard.columns || DEFAULT_COLUMNS // Before the editor could place things, every widget was written at 0,0; @@ -112,6 +115,31 @@ function Footprint({ dashboard }: { dashboard: DashboardSummary }) { ) } +/** + * A round dashboard shows one face at a time, so its grid places mean + * nothing: it is drawn as the circle, with a dot per face where a panel puts + * its page dots. + */ +function RoundFootprint({ faces }: { faces: WidgetFootprint[] }) { + return ( +
      +
      +
      + {faces.map((face, index) => ( + + ))} +
      +
      +
      + ) +} + /** One dashboard in the mosaic. */ function Tile({ dashboard, diff --git a/frontend/src/components/Common/GlobalSearch.tsx b/frontend/src/components/Common/GlobalSearch.tsx index 8007bee..0f7bbc7 100644 --- a/frontend/src/components/Common/GlobalSearch.tsx +++ b/frontend/src/components/Common/GlobalSearch.tsx @@ -182,7 +182,10 @@ export function GlobalSearch({ // Panels are managed in a dialog on the dashboards screen, which opens // itself when the address says so. case "panel": - return navigate({ to: "/dashboards", search: { panels: true } }) + return navigate({ + to: "/dashboards", + search: { panels: true, panel: entry.name }, + }) case "secret": return navigate({ to: "/secrets" }) case "module": diff --git a/frontend/src/components/Common/OverviewToolbar.tsx b/frontend/src/components/Common/OverviewToolbar.tsx index b0c8d05..2512290 100644 --- a/frontend/src/components/Common/OverviewToolbar.tsx +++ b/frontend/src/components/Common/OverviewToolbar.tsx @@ -277,6 +277,7 @@ export function ConfirmDelete({ pending, description, onConfirm, + children, }: { open: boolean onOpenChange: (open: boolean) => void @@ -286,6 +287,8 @@ export function ConfirmDelete({ /** What is actually lost, when the git-backed answer below is not it. */ description?: string onConfirm: () => void + /** An option the delete takes, drawn between the question and the buttons. */ + children?: ReactNode }) { return ( @@ -307,6 +310,7 @@ export function ConfirmDelete({ )} + {children} - {live && name ? ( -
      - now - {current === undefined ? ( - — - ) : ( - - - - )} - - - - - Put this value into the flow now - -
      - ) : null} +
      + + onChange({ ...declared, origin: origin as typeof declared.origin }) + } + /> + {live && name ? ( + <> + now + {current === undefined ? ( + — + ) : ( + + + + )} + + + + + Put this value into the flow now + + + ) : null} +
      ) } @@ -338,8 +355,9 @@ export function BoundarySections({
      Inputs - Messages that arrive from outside, such as a dashboard control, a - run or the API, and the value the flow starts from. + Messages that arrive from outside, and the value the flow starts + from. A run passes a run input in, a dashboard or panel control + writes a panel input.