Metadata-Version: 2.5
Name: sturnyx
Version: 0.1.0
Summary: Monitoring and optional federated learning for Sturnyx workloads
Requires-Python: >=3.11.8
Provides-Extra: fl
Requires-Dist: nvflare==2.9.0; extra == 'fl'
Description-Content-Type: text/markdown

# Sturnyx workload library

Projects choose whether to use this library and install their own dependencies.
The platform never adds it to an image automatically. Once released, install a
chosen version with `pip install sturnyx==VERSION` for monitoring, or
`pip install 'sturnyx[fl]==VERSION'` for federated learning, in the project Dockerfile.
For development, run `uv build` here and explicitly COPY/install the resulting
wheel in an example image. No package has been published by this repository.

Clusters inspect the SDK version installed in built workload images before
accepting preparation, including the server image before job export. Pin a
version supported by the selected cluster; updating the desktop does not replace
the SDK inside a saved image. Images without the optional SDK remain valid.
See [SDK compatibility](../../docs/sdk-compatibility.md) for configuration and checks.

## Monitoring

`sturnyx.monitor` uses only the Python standard library. It works in solo and
collaborative and test workload containers without importing NVFlare or a training framework.

```python
from sturnyx import monitor

monitor.metric("dataset.rows", 2500)  # local by default
monitor.metric("loss", 0.18, step=4, visibility="shared")
monitor.progress("preprocessing", 40, 100, visibility="shared")
monitor.log("Preprocessing complete", visibility="shared")
```

`metric(name, value, *, step=None, unit=None, visibility="local")` accepts finite
scalars. `progress(name, current, total, *, visibility="local")` requires
`0 <= current <= total` and a positive total. `log(message, *, level="info",
name="log", visibility="local")` supports debug, info, warning, and error.
Names are limited to 128 UTF-8 bytes, units to 32, and messages to 4096.
Invalid arguments raise `ValueError`; reporting returns `False` if its local
destination is unavailable, so missing monitoring does not terminate a workload.

The agent supplies `STURNYX_MONITOR_DIR`, a narrowly scoped writable mount. Each
process opens a dedicated append-only descriptor there; reports never mix with
stdout/stderr, and no account credentials or additional network connection enter
the workload. The library handles concurrent threads and processes. Do not mount
agent state or create platform credentials in the image.

Local reports never upload. In collaborative deployments, only reports explicitly
marked shared are forwarded by the agent. Shared reports retain worker identity;
they are not automatically aggregated. **Solo and test reports always stay local**, even
when the caller specifies shared visibility. Raw stdout/stderr always stay local.
These visibility rules apply to metrics, progress, and diagnostic messages alike.

Open **Monitor run** under Solo runs, or **Monitor deployment** in a project's
Execution & monitoring view. Select a report and workers to follow, pause live
updates, or load earlier history. Viewing another worker's shared reports requires
Monitor deployments permission; participants otherwise see their own. Developers
can monitor from an unpaired machine through the platform. Local container output
is available through the agent on the computer executing that workload.

Shared history is retained for 30 days by default (`MONITORING_RETENTION_DAYS` on
the API). Local reports remain in run artifacts; the agent journal is capped at
64 MiB and 256 named report series per workload. The view keeps up to 2000 points
in memory. Monitoring reports never change container or deployment status.

## Runtime environment

`sturnyx.runtime` uses only the standard library. The platform identifies the
environment without changing application settings:

```python
from sturnyx import runtime

class Dataset:
    def __len__(self):
        return min(10, len(self.rows)) if runtime.is_test() else len(self.rows)

print(runtime.environment())
```

`environment()` returns `test` in developer test deployments, `production` in
solo/collaborative deployments, or `unmanaged` outside an injected environment.
`is_test()` is a convenience predicate. Invalid injected values raise `ValueError`.
The same query is available in project server code. Non-Python programs can read
`STURNYX_ENVIRONMENT` directly; Python containers can also run:

```bash
python -m sturnyx.runtime
```

Select a saved snapshot and **Test deployment** in the app to run real federation
with simulated participants locally. Tests preserve project workflow settings,
normal container startup and dependency choices. No dataset, batch or round
reductions are supplied by Sturnyx. Results and reports stay local, and passing
tests is not required for publication/start. Use **Run solo** for an ordinary
single-container project.

## Federated learning

Install the `fl` extra. The existing model, task, and parameter exchange API now
lives under **`sturnyx.fl`**, with framework helpers under `sturnyx.fl.helpers`.
Migrate `import sturnyx as fl` to `from sturnyx import fl`.

The image starts its own program using its normal ENTRYPOINT/CMD:

```python
from sturnyx import fl

fl.init()
try:
    while fl.is_running():
        model = fl.receive()
        # Train/evaluate with model.params and send an fl.FLModel result.
        fl.send(model)
finally:
    fl.shutdown()
```

The bundled agent supplies a private deployment context and runs a separate
NVFlare sidecar. `init()` attaches to that runtime; it does not launch training,
create a deployment, or require the user to enter addresses or credentials.
The same interface works for app-managed test deployments. Calling it outside a
managed container fails with an explanation instead of creating a hidden runtime.

Framework dependencies belong in project images. The familiar NVFlare model,
task inspection, parameter exchange, and FedProx/SCAFFOLD helpers are retained.
Supported workflows follow the platform's pinned NVFlare 2.9.0 execution contract.
Host orchestration, pairing, Docker, and private server code are excluded from
this wheel. Solo containers can use monitoring without any federated runtime.
