wren node: Train in the panel, as a run of wren's training flow

POST/GET /flows/{name}/nodes/{id}/train submit wren_train on the node's
targets, examples and tests and read its latest run (run.for_node,
cause "train"); "Use this model" writes `model` into the draft.
This commit is contained in:
2026-10-25 01:18:54 +02:00
parent 07fa5a2dc2
commit e9941b770d
11 changed files with 671 additions and 3 deletions
@@ -0,0 +1,33 @@
"""run.for_node
A wren node's Train button submits wren's training flow as a run. The node
it was pressed on is what ties that run back to the node's panel.
Revision ID: a3c7e5d91f42
Revises: ccb21eacf5d5
Create Date: 2026-10-24
"""
import sqlalchemy as sa
import sqlmodel.sql.sqltypes
from alembic import op
# revision identifiers, used by Alembic.
revision = "a3c7e5d91f42"
down_revision = "ccb21eacf5d5"
branch_labels = None
depends_on = None
def upgrade():
op.add_column(
"run",
sa.Column(
"for_node", sqlmodel.sql.sqltypes.AutoString(length=255), nullable=True
),
)
def downgrade():
op.drop_column("run", "for_node")
+125 -1
View File
@@ -57,6 +57,7 @@ from fluksio.flow.devices import (
)
from fluksio.flow.events import event_bus
from fluksio.flow.messages import MEDIA_FAMILIES, DType, flow_of, qualify
from fluksio.flow.nodes.wren import TRAIN_FLOW, WrenNode, pack_meta
from fluksio.flow.pipeline import ValidationIssue
from fluksio.flow.runs import RunRejected
from fluksio.flow.schemas import (
@@ -84,7 +85,7 @@ from fluksio.flow.store import (
LibNotFound,
StaleVersion,
)
from fluksio.models import Flavor, Message, Run
from fluksio.models import Flavor, Message, Run, RunArtifact, RunNode
logger = logging.getLogger(__name__)
@@ -159,6 +160,24 @@ class TriggerRequest(BaseModel):
values: dict[str, Any] = {}
class Training(BaseModel):
"""A wren node's latest training run, as its panel shows it."""
id: str
#: queued, running, ok, error, cancelled or abandoned.
status: str
#: What went wrong, when it failed.
error: str = ""
#: final_loss, n_params, exact_match, threshold and coverage, once it ended.
result: dict[str, Any] = {}
#: The trained model's digest, for the node's ``model`` setting.
model: str = ""
#: From the model's own record: the confidence it recommends, and its
#: score on the node's ``tests`` when there were any.
threshold: float | None = None
test_exact_match: float | None = None
def _endpoints(controller: FlowController, definition: FlowDef) -> list[Endpoint]:
"""Everything wired into this flow from outside it."""
flow = definition.name
@@ -1010,6 +1029,111 @@ def acknowledge_node_error(
return Message(message=f"Cleared the failure on '{node_id}'")
# -----------------------------------------------------------------------------
# Training a wren node
# -----------------------------------------------------------------------------
def _node_def(controller: FlowController, name: str, node_id: str) -> NodeDef:
"""A node as the editor shows it: from the draft when there is one."""
node = next(
(n for n in _read_flow(controller, name).nodes if n.id == node_id), None
)
if node is None:
raise HTTPException(
status_code=404, detail=f"No node named '{node_id}' in flow '{name}'"
)
return node
@router.post("/{name}/nodes/{node_id}/train", response_model=Training, status_code=202)
async def train_node(
name: str,
node_id: str,
request: Request,
controller: FlowControllerDep,
user: CurrentUser,
) -> Any:
"""Train a new model for a wren node, as a run of wren's training flow.
It learns the node's targets, its ``examples`` and its ``tests``, as the
editor shows them. The node keeps running its model until the new one is
set as its ``model``.
"""
node = _node_def(controller, name, node_id)
if node.type != "wren":
raise HTTPException(status_code=400, detail=f"'{node_id}' is not a wren node")
try:
wren = WrenNode(
requires=[spec for spec in node.requires if spec.name],
provides=[spec for spec in node.provides if spec.name],
params=node.params,
)
except (ValueError, RuntimeError) as exc:
raise HTTPException(status_code=400, detail=str(exc)) from exc
service = getattr(request.app.state, "run_service", None)
if service is None:
raise HTTPException(status_code=503, detail="Runs are not available")
try:
run = await run_in_threadpool(
service.submit,
TRAIN_FLOW,
params={"installation": orjson.dumps(wren.installation()).decode()},
cause="train",
actor=user.email,
no_cache=True,
for_node=f"{name}.{node_id}",
)
except FlowNotFound:
raise HTTPException(
status_code=409,
detail=(
f"wren's training flow '{TRAIN_FLOW}' is not registered. Run "
"`fluksio sync wren.flow` from an environment with wren[train]."
),
) from None
except RunRejected as exc:
raise HTTPException(status_code=422, detail=str(exc)) from exc
return Training(id=run.id, status=run.status)
@router.get("/{name}/nodes/{node_id}/train", response_model=Training | None)
def read_training(
name: str,
node_id: str,
request: Request,
controller: FlowControllerDep,
session: SessionDep,
) -> Any:
"""A wren node's latest training run, or null when it has none."""
_node_def(controller, name, node_id)
run = session.exec(
select(Run)
.where(col(Run.flow) == TRAIN_FLOW, col(Run.for_node) == f"{name}.{node_id}")
.order_by(col(Run.created_at).desc())
).first()
if run is None:
return None
training = Training(id=run.id, status=run.status, result=run.result)
if run.status == "error":
failed = session.exec(
select(RunNode.error).where(
col(RunNode.run_id) == run.id, col(RunNode.error) != ""
)
).first()
training.error = failed or run.status_reason
# The model, once the run made one the store still has.
pack = session.get(RunArtifact, {"run_id": run.id, "name": f"{TRAIN_FLOW}.pack"})
store = getattr(request.app.state, "artifact_store", None)
path = store.path(pack.digest) if pack is not None and store else None
if pack is not None and path is not None:
meta = pack_meta(path)
training.model = pack.digest
training.threshold = meta.get("threshold")
training.test_exact_match = meta.get("test_exact_match")
return training
@router.get("/{name}/state", response_model=FlowStatePublic)
def read_flow_state(name: str, controller: FlowControllerDep, hidden: HiddenDep) -> Any:
"""The last value seen on every message of this flow."""
+29
View File
@@ -46,6 +46,13 @@ def _wren() -> Any:
return wren
def pack_meta(path: Any) -> dict[str, Any]:
"""What a model records of its training: its targets, threshold and,
when it had tests, ``test_exact_match``."""
found: dict[str, Any] = _wren().load_pack(path)["meta"]
return found
#: The input the command arrives on, and the only one that wakes the node.
PROMPT = "prompt"
#: The output the calls are reported on, and the two that say what came of
@@ -55,6 +62,8 @@ REPLY = "reply"
UNHANDLED = "unhandled"
#: The reply when nothing was done and nothing reads ``unhandled``.
CANT = "Sorry, I can't do that yet."
#: wren's training flow, which wren registers: ``fluksio sync wren.flow``.
TRAIN_FLOW = "wren_train"
_TARGET_TYPES = {DType.BOOL, DType.INT, DType.FLOAT, DType.STR}
@@ -260,6 +269,17 @@ class WrenNode(Node):
description="Your own words, and words heard wrong, in the model's: "
'{"traverse": "living room light", "page too": "patio"}.',
)
examples: list[dict[str, Any]] = Field(
default_factory=list,
description="Training only: commands to learn, targets named by port, "
'as {"state": {"user_location": "kitchen"}, "command": "lights on", '
'"calls": [["kitchen_light", true]]}.',
)
tests: list[dict[str, Any]] = Field(
default_factory=list,
description="Training only: commands a new model is scored on, like "
'examples. "calls": [] is one it must pass on.',
)
__slots__ = ("cfg", "targets", "pack", "untrained")
@@ -339,6 +359,15 @@ class WrenNode(Node):
name=name or "wren",
)
def installation(self) -> dict[str, Any]:
"""What wren's training flow learns from: the targets, each with a
value, and the ``examples`` and ``tests``."""
return {
"targets": [t | {"value": _unset(t)} for t in self.targets.values()],
"examples": self.cfg.examples,
"tests": self.cfg.tests,
}
async def start(self, app: FastAPI | None = None) -> None:
store = getattr(app.state, "artifact_store", None) if app else None
self.pack = self._load(store)
+3
View File
@@ -951,6 +951,7 @@ class RunService:
idempotency_key: str | None = None,
parent_id: str | None = None,
passed: list[str] | None = None,
for_node: str | None = None,
) -> Run:
"""Journal a run and wake an engine up for it. Never blocks on it.
@@ -993,6 +994,7 @@ class RunService:
actor=actor,
idempotency_key=idempotency_key,
parent_id=parent_id,
for_node=for_node,
)
with Session(db_engine) as session:
session.add(run)
@@ -1249,6 +1251,7 @@ class RunService:
# What its parent's caller chose. A parent from before that was
# recorded falls back to the names its params carry.
passed=source.passed,
for_node=source.for_node,
)
def _flow_of(self, run_id: str) -> str | None:
+4 -1
View File
@@ -388,8 +388,11 @@ class Run(SQLModel, table=True):
external_id: str | None = Field(
default=None, unique=True, index=True, max_length=255
)
#: Where it was asked for: api, cli, sdk, hook, sweep or import.
#: Where it was asked for: api, cli, sdk, hook, sweep, import or train.
cause: str = Field(default="api", max_length=32)
#: The node it was asked for from, as `flow.node`: the wren node a
#: training run is for. Null for every other run.
for_node: str | None = Field(default=None, max_length=255)
#: Re-execute every node, whatever the stage cache holds for it.
no_cache: bool = False
#: Run the unpublished working copy rather than what is published. Read