$npx -y skills add Senpi-ai/senpi-skills --skill senpi-trading-runtimeHow a Senpi trading strategy interacts with the runtime engine (@senpi-ai/runtime) on Hyperliquid: a strategy runs from a runtime.yaml pointing at a Python module that exports scan(inputs, ctx), which the runtime supervises and calls each interval, then owns execution, risk guard
| 1 | # Senpi Trading Runtime — the runtime contract |
| 2 | |
| 3 | This skill is **infrastructure**: the canonical knowledge of how the Senpi runtime |
| 4 | (**`@senpi-ai/runtime`**) behaves and how a strategy interacts with it. The lifecycle skills — |
| 5 | author (build), ops (install/monitor), discover (recommend) — reference this one for the contract. |
| 6 | |
| 7 | ## The runtime model |
| 8 | |
| 9 | A strategy runs from a **`runtime.yaml`** that points at an in-repo Python module. The runtime **spawns and |
| 10 | supervises** that module and calls a frozen **`scan(inputs, ctx)`** every `interval_seconds`. The |
| 11 | division of labor is fixed: |
| 12 | |
| 13 | - **Your code produces signals — nothing else.** `scan(inputs, ctx)` *reads* market and account data |
| 14 | and *returns* a `list[dict]` of candidate signals. It does not open, close, size, schedule, or |
| 15 | execute anything. |
| 16 | - **The runtime owns everything downstream:** scheduling (`interval_seconds`), spawning + |
| 17 | supervising + restarting the scanner, validating (`signal_data_schema`) + de-duplicating the |
| 18 | signals you return, **sizing & order execution** (`FEE_OPTIMIZED_LIMIT`), slot accounting, |
| 19 | `risk.guard_rails`, the two-phase **DSL** trailing-stop exits, and **crash-safe position reconcile** |
| 20 | on restart. |
| 21 | |
| 22 | ## How your code talks to the runtime |
| 23 | |
| 24 | The interaction surface is small and one-directional — you read, you return signals, the runtime acts. |
| 25 | |
| 26 | - **`runtime.yaml`** declares the scanner(s), the action gate, the exit engine, and the risk |
| 27 | guard-rails, and passes author tunables down via `inputs:`. → `references/runtime-yaml.md` |
| 28 | - **`scan(inputs, ctx)`** is the single entry point. `inputs` is the runtime's `inputs:` map; `ctx` |
| 29 | gives you: |
| 30 | - **`ctx.senpi_mcp.call_tool(name, args)`** — the Senpi MCP client, **read-only** (market, |
| 31 | account, leaderboard, discovery, `strategy_get*`, …). It is the only way to fetch data. |
| 32 | - **`ctx.state`** — transactional, runtime-persisted history (`last()` / `append()` / `len`) for |
| 33 | dedup, rotation, and first-seen ledgers; advances only on a clean tick. |
| 34 | - **`ctx.wallet`** — the strategy's wallet address. |
| 35 | - → `references/scan-contract.md` |
| 36 | - **The return value** is a `list[dict]`, one per candidate signal (`asset`, `direction`, |
| 37 | `marginUsd`, `leverage`, `data{}`). The runtime validates each `data{}` against the runtime.yaml's |
| 38 | `signal_data_schema`, then sizes, executes, and manages exits. |
| 39 | |
| 40 | Keep the thesis logic in a sibling pure **`scoring.py`** (no I/O, no MCP) so it is unit-testable; |
| 41 | `scan.py` does the reads + state, `scoring.py` does the math. |
| 42 | |
| 43 | ## Runtime commands (essentials) |
| 44 | |
| 45 | The plugin registers a `senpi` command group on the gateway. Deploying a runtime and checking it: |
| 46 | |
| 47 | ```bash |
| 48 | openclaw plugins install @senpi-ai/runtime |
| 49 | openclaw senpi runtime create -p runtime.yaml # hot-loads; the runtime supervises the scanner |
| 50 | openclaw senpi runtime list # id, source, status (running/stopped) |
| 51 | ``` |
| 52 | |
| 53 | Beyond `runtime create/list/delete`, the CLI exposes the runtime's live state — `senpi dsl |
| 54 | positions|inspect|closes` (the exit engine), `senpi action list|inspect|history|decisions` (the |
| 55 | decision layer), `senpi risk` (am I allowed to trade, and why not), `senpi audit` (backend trade |
| 56 | trail with AI reasoning), `senpi scanner` (per-scanner health, liveness, and barren detection), |
| 57 | `senpi events`/`senpi explain <asset>` (the local domain-event log — the trade narrative, and one |
| 58 | asset's stitched lifecycle), `senpi status`/`senpi state` (health), and `senpi guide …` (in-shell |
| 59 | reference). Full surface with every option → `references/runtime-cli.md`. |
| 60 | |
| 61 | **To confirm open positions are actually stop-loss protected** (a position with no DSL shows up as an |
| 62 | *absence* in `dsl positions`, so it's easy to miss) → the verdict procedure in |
| 63 | `references/dsl-protection-check.md`. |
| 64 | |
| 65 | ## The reference set |
| 66 | |
| 67 | | Read this | For | |
| 68 | |---|---| |
| 69 | | `references/runtime-concepts.md` | How the runtime behaves end to end: the runtime pipeline, `position_tracker`, and the two-phase DSL exit engine | |
| 70 | | `references/runtime-yaml.md` | The `runtime.yaml` schema — every section, the `external_scanner` fields, the risk guard-rails | |
| 71 | | `references/scan-contract |