Files
stroblmeandClaude Fable 5 a38e2745eb Add a Python SDK: flows declared in your own repository
A data scientist keeps their code where it is and decorates it: `@node`
declares a function's ports beside the function, `Flow(name, nodes=[...])`
says which of them make a flow, and `use(fn, wire=..., **settings)` rebinds
one for a single flow. `fluksio sync` uploads the document plus a generated
import shim per node, so the store still holds a complete, runnable,
git-versioned definition while the code it imports stays theirs.

`fluksio login|run|runs` and `flow.submit().wait()` are the client half, over
the run endpoints that already existed. Runs record the user repository's
commit beside the store's, so "what code produced this number" is answerable
on the side that now holds the code.

- `fluksio/sdk/`: ports, decorators, the flow builder and its checks, the shim
  generator, an HTTP client and sync. Standard library only at import, so
  `from fluksio import node` in a training script pulls in no engine.
- `FlowDef.origin` marks a flow code-defined; `Run.origin_commit` carries the
  repository's commit; `POST /modules/refresh` retires the workers without an
  install, which every sync calls — a worker holds the imported package in
  memory, so an edit to it is invisible until the process goes.
- The canvas shows a generated body read-only and names the repository to edit
  instead; a body edited there stops the next sync rather than being discarded.
- The worker's reporter carries inert `Port`, `node`, `use` and `Flow`, since
  the shim imports a module whose first line declares them.
- `examples/myresearch` is the worked example, `make sync-example` uploads it.

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_012ue1tkFWB1bcGy3aWhCKpU
2026-08-23 20:16:08 +02:00

109 lines
3.7 KiB
Python

"""The python packages node code may import.
A manifest in the flow store, a venv on the data volume, and one button that
brings the second in line with the first. Nothing here restarts the engine:
the worker pool retires its processes, and the next node call picks up the new
packages.
"""
import time
from typing import Any
from fastapi import APIRouter, Depends, HTTPException
from fastapi.concurrency import run_in_threadpool
from fluksio.api.deps import (
CurrentUser,
FlowControllerDep,
WorkerPoolDep,
get_current_user,
)
from fluksio.flow import modules
from fluksio.flow.events import event_bus
from fluksio.flow.schemas import ApplyRequest, ApplyResult, ModulesInfo
from fluksio.models import Message
router = APIRouter(
prefix="/modules", tags=["modules"], dependencies=[Depends(get_current_user)]
)
@router.get("/", response_model=ModulesInfo)
async def read_modules(controller: FlowControllerDep) -> Any:
"""What node code can import, and whether it matches the manifest."""
return await run_in_threadpool(modules.info, controller.store)
@router.post("/apply", response_model=ApplyResult)
async def apply_modules(
body: ApplyRequest,
controller: FlowControllerDep,
pool: WorkerPoolDep,
user: CurrentUser,
) -> Any:
"""Install exactly these requirements, then hand them to the workers.
A manifest that does not resolve changes nothing: the venv is left as it
was and the stored manifest is only written once the install succeeded.
Only the flows already holding a node that would not load are rebuilt,
because those are the ones an install is called to fix. A flow that this
install *breaks* — a package taken back out from under it — is still green
and fails at call time with the node author's own import error, until
something rebuilds it.
"""
ok, output = await run_in_threadpool(modules.sync, body.requirements)
if not ok:
raise HTTPException(
status_code=400,
detail=output or "These requirements could not be installed",
)
await run_in_threadpool(controller.store.write_requirements, body.requirements)
event_bus.publish(
{
"type": "audit",
"action": "installed modules",
"flow": "",
"user": user.email,
"ts": time.time(),
}
)
# Retire the workers first, so the rebuild compiles every node against the
# packages that were just installed — a node that could not import one is
# the reason this was called, and it stays red until it is built again.
await _retire(controller, pool)
return ApplyResult(ok=True, output=output)
@router.post("/refresh", response_model=Message)
async def refresh_modules(
controller: FlowControllerDep,
pool: WorkerPoolDep,
user: CurrentUser,
) -> Any:
"""Retire the workers without installing anything.
A node that imports the caller's own package holds it in `sys.modules` for
as long as the process lives, so editing that package changes nothing a
running worker can see — recompiling the node would not help either, since
the import returns the module already there. Retiring the processes is the
whole of it, and `fluksio sync` asks for it after every upload.
"""
event_bus.publish(
{
"type": "audit",
"action": "refreshed workers",
"flow": "",
"user": user.email,
"ts": time.time(),
}
)
await _retire(controller, pool)
return Message(message="Workers retired; the next call imports afresh")
async def _retire(controller: Any, pool: Any) -> None:
"""Send the worker processes away and rebuild whatever was broken."""
pool.respawn_all()
await controller.reload_failed_flows()