Metadata-Version: 2.4
Name: holonode
Version: 1.1.0
Summary: Worker SDK for the Holarchy orchestrator.
Author-email: Dmitry Gagarin aka madgagarin <madgagarin@gmail.com>
License-Expression: MPL-2.0
Project-URL: Homepage, https://github.com/madgagarin/holonode
Project-URL: Bug Tracker, https://github.com/madgagarin/holonode/issues
Keywords: worker,sdk,orchestrator,distributed,task-queue,rxon,hln,holarchy
Classifier: Development Status :: 5 - Production/Stable
Classifier: Intended Audience :: Developers
Classifier: Programming Language :: Python :: 3
Classifier: Programming Language :: Python :: 3.11
Classifier: Programming Language :: Python :: 3.12
Classifier: Programming Language :: Python :: 3.13
Classifier: Programming Language :: Python :: 3.14
Classifier: Operating System :: OS Independent
Classifier: Typing :: Typed
Requires-Python: >=3.11
Description-Content-Type: text/markdown
License-File: LICENSE
Requires-Dist: rxon~=1.2.0
Requires-Dist: aiohttp~=3.14.3
Requires-Dist: python-json-logger~=4.2.0
Requires-Dist: aiofiles~=25.1.0
Requires-Dist: orjson~=3.12.0
Requires-Dist: zstandard>=0.23.0
Provides-Extra: s3
Requires-Dist: obstore<1.0.0,>=0.11.1; extra == "s3"
Provides-Extra: pydantic
Requires-Dist: pydantic<3.0.0,>=2.13.4; extra == "pydantic"
Provides-Extra: metrics
Requires-Dist: opentelemetry-api<2.0.0,>=1.44.0; extra == "metrics"
Requires-Dist: opentelemetry-sdk<2.0.0,>=1.44.0; extra == "metrics"
Requires-Dist: opentelemetry-exporter-otlp<2.0.0,>=1.44.0; extra == "metrics"
Requires-Dist: opentelemetry-semantic-conventions>=0.50b0; extra == "metrics"
Provides-Extra: dev
Requires-Dist: holonode[metrics,pydantic,s3]; extra == "dev"
Requires-Dist: pytest~=9.1.0; extra == "dev"
Requires-Dist: pytest-asyncio~=1.4.0; extra == "dev"
Requires-Dist: aioresponses~=0.7.9; extra == "dev"
Requires-Dist: pytest-mock; extra == "dev"
Requires-Dist: types-aiofiles~=25.1.0.20260518; extra == "dev"
Requires-Dist: watchdog; extra == "dev"
Requires-Dist: mutmut~=3.8.0; extra == "dev"
Dynamic: license-file

**EN** | [ES](https://github.com/madgagarin/holonode/blob/main/docs/es/README.md) | [RU](https://github.com/madgagarin/holonode/blob/main/docs/ru/README.md)

# Holonode SDK

[![License: MPL 2.0](https://img.shields.io/badge/License-MPL%202.0-brightgreen.svg)](https://opensource.org/licenses/MPL-2.0)
[![PyPI Version](https://img.shields.io/pypi/v/holonode.svg)](https://pypi.org/project/holonode/)
[![Python 3.11+](https://img.shields.io/badge/python-3.11+-blue.svg)](https://www.python.org/downloads/release/python-3110/)

**Holonode SDK** is the official high-performance Python SDK for building distributed execution nodes (Workers / Holons) compatible with the **Holarchy** orchestrator. It serves as an execution node (**Shell**) in a **Holarchical Logic Network (HLN)**, coordinating with the orchestrator (**Ghost**) via the **RXON** (Reverse Axon Protocol).

The SDK automates all low-level communication concerns: task polling, dynamic registration, heartbeats, S3 payload management, Zero Trust authentication, and graceful shutdown.

## 🚀 Key Features

- **Language:** Python 3.11+
- **Protocol:** Native support for **RXON** (Reverse Axon Protocol) for **HLN** (Holarchical Logic Network).
- **Communication Model:**
  - **PULL:** Workers poll tasks from orchestrators (operates securely behind NAT/Firewall without public incoming ports).
  - **WebSocket:** Optional real-time bidirectional command channel (task cancellation, custom runtime commands).
- **Zero Trust Security & Policy Enforcement:**
  - Mandatory HMAC-SHA256 signing for all worker messages using `WORKER_TOKEN`.
  - Cryptographic verification of `orchestrator_signature` (`sig`) on incoming tasks via `ORCHESTRATOR_SECRET_KEY`.
  - Strict enforcement of `allowed_skills` task execution policies.
  - Automatic collection and reporting of task execution cost metrics (`costs`).
  - Identity Chain and Origin Worker ID support for provenance tracking across infinite holarchy layers.
  - Replay protection with timestamp validation.
  - **Supply Chain Security & Attestation Verification (`holonode.attestation`)**: Configurable verification of OCI 1.1 Referrers metadata (`REQUIRE_ATTESTATION`, `MIN_SLSA_LEVEL`, `TRUSTED_ATTESTATION_KEYS`), validating SBOM/provenance artifact types, SLSA levels, and Ed25519 signatures before mounting skills.
- **Dynamic Skill Mounting (`SkillMounter`):**
  - Mounts and unmounts versioned `namespace/name` skills at startup (`INITIAL_SKILL_MANIFESTS`), programmatically, or via orchestrator heartbeat sync (`mounted_manifests`, `mount_digests`, `desired_skills`, `unmount_digests`).
  - Fetches deterministic `.tar.zst` CAS bundles and manifests from HoloReg (`HOLOREG_URL`) with local SHA-256 caching (`LocalBlobCache`), asset symlinking, and isolated `venv` environments (`env_digest`).
- **Traffic & Performance Optimization:**
  - **Telemetry Throttling (Heartbeat Deadband):** Hardware telemetry (CPU/RAM/GPU) is only sent when a value changes by >5% or after a 60s forced interval, drastically saving network bandwidth.
  - **ETag-Based Blob Caching:** Heavy assets (e.g. AI model weights) are downloaded only once from S3, cached locally with ETag validation, and symlinked to task workspaces.
  - **Async Results Uploader:** Task results are queued via non-blocking `asyncio.Queue` with automatic retries and exponential backoff, instantly freeing the worker for the next task.
  - **3-Tier Skill Lifecycle:** _Supported_ (catalog), _Available_ (dynamic limits), and _Hot_ (cached in memory/VRAM).
  - **Stable Hashing:** Sends full skill catalog only when changed, using `skills_hash` for ultra-light heartbeats.
- **S3 Streaming:** High-performance payload streaming using `obstore`. Zero OOM on large files.
- **AI-Agent Support:** Supports Chain of Thought and Tool Use via `OrchestratorClient` dependency injection for subtask delegation.
- **Hardware Awareness:** Built-in monitoring for CPU, RAM, and NVIDIA GPUs (via `psutil` and `GPUtil`).
- **Modern Observability:**
  - Native distributed tracing and metrics with **OpenTelemetry**.
  - Automatic **Trace Context Propagation**: Extracts `trace_id` from tasks and propagates it to all events and child spans.
  - OTLP metrics push export via `OTEL_EXPORTER_OTLP_ENDPOINT`.

## Table of Contents

- [Core Architecture: Orchestrators and Workers](#core-architecture-orchestrators-and-workers)
- [Ecosystem](#ecosystem)
- [Installation](#installation)
- [Quick Start](#quick-start)
  - [Usage as a Library](#usage-as-a-library)
  - [CLI Usage](#cli-usage)
- [Dynamic Skill Mounting](#dynamic-skill-mounting)
- [Configuration Reference](#configuration-reference)
- [Resilience & Connectivity](#resilience--connectivity)
- [Observability](#observability)
- [Security](#security)
- [Contributing](#contributing)
- [License](#license)

## Core Architecture: Orchestrators and Workers

Holonode SDK implements the execution tier of a **Holarchical Logic Network (HLN)**:

- **Orchestrator**: Manages workflow state machines (Blueprints), tracks job lifecycles, and coordinates tasks. The orchestrator acts as a passive HTTP server and never initiates outbound connections to workers.
- **Worker**: An autonomous execution node. It establishes reverse HTTP polling connections to the orchestrator, registers supported skills and hardware capabilities, pulls assigned tasks into isolated workspaces, and posts results asynchronously.
- **Hierarchical Composition**: Standardized contracts allow an entire orchestrator instance to be wrapped inside a worker interface, enabling recursive multi-level task delegation across distributed clusters.

## Ecosystem

Holonode SDK is an integral part of the Holarchy distributed ecosystem:

- **[Holarchy Orchestrator](https://github.com/madgagarin/holarchy)**: High-performance state-machine workflow engine coordinating tasks and distributed blueprints.
- **[RXON Protocol](https://github.com/madgagarin/rxon)**: Shared lightweight reverse-connection protocol powering inter-node communication across the HLN ecosystem.
- **[HLN (Holarchical Logic Network)](https://github.com/madgagarin/hln)**: Architectural pattern, manifesto, and design specification for self-similar holarchies.
- **[Holonode SDK](https://github.com/madgagarin/holonode)**: The official Python SDK for building execution workers (this repository).
- **[Full Example](https://github.com/madgagarin/holarchy-full-example)**: Reference project demonstrating the orchestrator and workers in action.

## Installation

- **Install the core SDK only:**

  ```bash
  pip install holonode
  ```

- **Install with S3 payload offloading support:**

  ```bash
  pip install "holonode[s3]"
  ```

- **Install with Pydantic v2 schema inference support:**

  ```bash
  pip install "holonode[pydantic]"
  ```

- **Install with OpenTelemetry metrics and tracing:**

  ```bash
  pip install "holonode[metrics]"
  ```

- **Install all production features (Recommended):**

  ```bash
  pip install "holonode[s3,pydantic,metrics]"
  ```

- **Install for development:**

  ```bash
  pip install -e ".[dev]"
  ```

## Quick Start

### Usage as a Library

```python
from pydantic import BaseModel, Field
from holonode import OrchestratorClient, TaskFiles, Worker


class GreetingParams(BaseModel):
    name: str = Field(default="World", description="Name to greet")


worker = Worker(worker_type="greeter-service")


@worker.skill("hello_world", risk="low", permissions=["greetings:write"])
async def my_skill(params: GreetingParams, files: TaskFiles):
    """The SDK validates incoming task data directly into GreetingParams."""
    return {"message": f"Hello, {params.name}!"}


@worker.skill("ai_agent_reasoning", risk="medium", permissions=["ai:reason"])
async def agent_skill(params: dict, orchestrator_client: OrchestratorClient):
    """AI agent skill delegating a subtask (tool use) via OrchestratorClient."""
    search_result = await orchestrator_client.call_skill("web_search", {"query": params["search_query"]})
    return {"result": f"Based on web search: {search_result['data']}"}


@worker.on_command("reboot")
async def handle_reboot(command: dict):
    """Custom command received over the real-time WebSocket channel."""
    print("Rebooting worker...")


if __name__ == "__main__":
    worker.run()
```

> For step-by-step guidance, see the [Local Worker Development Cookbook](https://github.com/madgagarin/holarchy/blob/main/docs/cookbook/local_worker_development.md).

### CLI Usage

The SDK includes a built-in CLI for running workers, inspecting skills, and local debugging:

```bash
# Run the worker with health-check server (port 8083 by default)
worker run --app my_worker:worker

# Run with terminal statusline HUD
worker run --app my_worker:worker --hud

# Run on a custom port without health check
worker run --app my_worker:worker --port 9000 --no-health-check

# Run with auto-reload on code changes
worker run --app my_worker:worker --reload

# Execute a single skill locally in an isolated sandbox (no orchestrator required)
worker exec --app my_worker:worker --skill hello_world --params '{"name": "Alice"}'
worker exec --app my_worker:worker --skill hello_world --params @params.json

# Inspect registered skills, risk levels, and permissions
worker inspect --app my_worker:worker
worker inspect --app my_worker:worker --json
worker inspect --app my_worker:worker --skill hello_world

# Start local web testbench for manual skill testing
worker dev --app my_worker:worker --port 8088
```

## Dynamic Skill Mounting

Holonode SDK supports hot-mounting and unmounting skills from Content-Addressable Storage (CAS) manifests via `SkillMounter` without restarting the worker process:

- **Startup:** Set `INITIAL_SKILL_MANIFESTS='["sha256:..."]'` to mount bundles automatically during `Worker.load_skills()`.
- **Heartbeat Sync:** Workers report `mounted_manifests` in heartbeats, and orchestrators can push `mount_digests` (or `desired_skills`) and `unmount_digests` in heartbeat responses.
- **Programmatic API:** Call `await worker.mount_skills([...])`, `await worker.unmount_skills([...])`, or `worker.get_mounted_manifests()`.
- **CAS Cache & Isolation:** Bundles (`.tar.zst` in `portable` mode) and assets are cached in `~/.hln/cache/blobs/sha256`, unpacked into `~/.hln/mounted/`, symlinked for large assets, and isolated per `env_digest` in `~/.hln/venvs/`.
- **Attestation Enforcement:** Configure `REQUIRE_ATTESTATION`, `MIN_SLSA_LEVEL`, and `TRUSTED_ATTESTATION_KEYS` to verify OCI 1.1 Referrers before loading skill code.

> See Step 12 of the [Development Guide](https://github.com/madgagarin/holonode/blob/main/docs/DEVELOPMENT.md#step-12-dynamic-skill-mounting--attestations-skillmounter) for full examples and manifest details.

## Configuration Reference

All settings can be configured via environment variables:

| Variable | Type | Default | Description |
| :--- | :---: | :---: | :--- |
| `ORCHESTRATOR_URL` | String | `http://localhost:8080` | Fallback URL when connecting to a single orchestrator |
| `ORCHESTRATORS_CONFIG` | JSON String | `None` | JSON list of orchestrator configs (`url`, `priority`, `weight`) |
| `MULTI_ORCHESTRATOR_MODE` | String | `WATERFALL` | Polling strategy: `WATERFALL` (priority order) or `ROUND_ROBIN` |
| `WORKER_ID` | String | Auto-generated | Unique identifier for the worker instance |
| `WORKER_TYPE` | String | `generic-cpu-worker` | Type tag for skill matching and targeting |
| `WORKER_PORT` | Integer | `8083` | Port for the built-in HTTP health check probe (`/health`) |
| `WORKER_ENABLE_HUD` | Boolean | `False` | Enable statusline TUI HUD in terminal |
| `WORKER_TOKEN` | String | `None` | Secret token used for HMAC-SHA256 message signing (Zero Trust) |
| `ORCHESTRATOR_SECRET_KEY` | String | `None` | Secret key used to verify incoming task signatures (`sig`) |
| `REQUIRE_TASK_SIGNATURE` | Boolean | `True` | Enforce signature verification when `ORCHESTRATOR_SECRET_KEY` is set |
| `COST_PER_SKILL` | JSON String | `None` | JSON mapping of skill execution tariffs (e.g. `'{"render": 0.05}'`) |
| `MAX_CONCURRENT_TASKS` | Integer | `10` | Global concurrency limit for parallel task executions |
| `WORKER_ENABLE_WEBSOCKETS` | Boolean | `False` | Enable real-time WebSocket connection for commands and cancellation |
| `WORKER_SKILLS_DIR` | String | `skills` | Directory for auto-discovered `SkillBlueprint` modules |
| `INITIAL_SKILL_MANIFESTS` | JSON List | `[]` | JSON list of manifest digests to mount via `SkillMounter` at startup |
| `HOLOREG_URL` | String | `http://localhost:8080` | HoloReg registry URL for fetching CAS skill bundles and manifests |
| `HOLOREG_TOKEN` | String | `None` | Optional Bearer token for authenticating with HoloReg |
| `REQUIRE_ATTESTATION` | Boolean | `False` | Enforce attestation verification (`holonode.attestation`) before mounting skills |
| `MIN_SLSA_LEVEL` | Integer | `0` | Minimum required SLSA provenance level when mounting skills |
| `TRUSTED_ATTESTATION_KEYS` | JSON String | `None` | JSON list or mapping of trusted Ed25519 PEM public keys for attestation verification |
| `HEARTBEAT_INTERVAL` | Float | `15.0` | Interval (seconds) between periodic worker heartbeats |
| `WORKER_HEARTBEAT_COOLDOWN` | Float | `2.0` | Minimum cooldown (seconds) between debounced heartbeats |
| `RATE_LIMIT_BACKOFF_FLOOR` | Float | `30.0` | Minimum backoff delay (seconds) on HTTP 429 without `Retry-After` |
| `IDLE_POLL_DELAY` | Float | `0.5` | Delay (seconds) between polling cycles when no tasks are available |
| `WORKER_SHUTDOWN_TIMEOUT` | Float | `30.0` | Maximum time (seconds) to wait for active tasks during graceful shutdown |
| `WORKER_ENABLE_METRICS` | Boolean | `False` | Enable OpenTelemetry tracing and metrics collection |
| `S3_ENDPOINT_URL` | String | `None` | S3-compatible storage endpoint URL for payload offloading |
| `S3_ACCESS_KEY` | String | `None` | S3 access key ID |
| `S3_SECRET_KEY` | String | `None` | S3 secret access key |
| `S3_DEFAULT_BUCKET` | String | `None` | Default S3 bucket for task file transfers |
| `WORKER_BLOB_CACHE_DIR` | String | `/tmp/holarchy_cache` | Local directory for caching S3 blobs with ETag validation |
| `TASK_FILES_DIR` | String | `/tmp/payloads` | Local base directory for isolated task workspace files |
| `WORKER_TELEMETRY_DEADBAND` | Float | `5.0` | Percentage change threshold for emitting hardware telemetry |
| `WORKER_TELEMETRY_FORCE_INTERVAL` | Float | `60.0` | Maximum interval (seconds) before telemetry is forcefully emitted |
| `POLL_BACKOFF_INITIAL` | Float | `1.0` | Initial delay (seconds) after network error or 429 response |
| `POLL_BACKOFF_MAX` | Float | `60.0` | Maximum backoff delay (seconds) |
| `POLL_BACKOFF_FACTOR` | Float | `2.0` | Multiplier for exponential backoff calculations |
| `STRICT_EVENT_VALIDATION` | Boolean | `True` | Validate emitted events against protocol schemas before dispatch |
| `LOG_LEVEL` | String | `INFO` | Logging verbosity (`DEBUG`, `INFO`, `WARNING`, `ERROR`) |

## Resilience & Connectivity

- **Independent Orchestrator Managers:** Each orchestrator connection is handled by an isolated background task. Outages or rate limits on one orchestrator never block others.
- **Smart Backoff & Retry Storm Protection:** Unified exponential backoff honors the HTTP `Retry-After` header (seconds or HTTP date). Errors with status 429 without `Retry-After` enforce a mandatory 30-second safety floor to prevent retry storms.
- **Heartbeat Debouncing:** Throttles heartbeats to a maximum of once every 2 seconds. State updates during the cooldown are coalesced and dispatched cleanly.
- **Graceful Shutdown:** Handles `SIGINT` and `SIGTERM` cleanly, awaiting active task completion and notifying orchestrators prior to process exit.

## Observability

Holonode SDK provides first-class observability using **OpenTelemetry**:

- **Distributed Tracing:** Workers extract `trace_id` from tasks and create child spans (`task.{type}`). All emitted events and S3 operations share the same trace.
- **Push Metrics (OTLP):** When `OTEL_EXPORTER_OTLP_ENDPOINT` is configured, metrics are automatically exported via OTLP.
- **ObservabilityManager Injection:** Handlers can accept `obs: ObservabilityManager` to create custom sub-spans and attach diagnostic metadata.

## Security

Security is foundational to the Holarchy ecosystem:

- See the full [RXON / HLN Security Model](https://github.com/madgagarin/rxon/blob/main/SECURITY.md).
- See the [Holarchy Security Policy](https://github.com/madgagarin/holarchy/blob/main/SECURITY.md).
- Detailed worker security policies are documented in [SECURITY.md](https://github.com/madgagarin/holonode/blob/main/SECURITY.md).

## Contributing

We welcome contributions! Please review our [Contributor Guide](https://github.com/madgagarin/holonode/blob/main/CONTRIBUTING.md) and [Development Guide](https://github.com/madgagarin/holonode/blob/main/docs/DEVELOPMENT.md) for environment setup and testing instructions.

## License

This project is licensed under the **Mozilla Public License 2.0 (MPL 2.0)**.
