File size: 10,254 Bytes
48fe669
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
"""
Miner Policy Server for Robotics Subnet

This FastAPI server exposes your robotics policy as HTTP endpoints
that validators can query during evaluation.

DEPLOYMENT OPTIONS:

1. Basilica Platform (Recommended):
   - Use kinitro CLI: kinitro miner push --repo YOUR_HF_REPO --revision YOUR_REVISION
   - Or use one-command deploy: kinitro miner deploy -r YOUR_HF_REPO -p . --netuid YOUR_NETUID

2. Self-Hosted:
   - Run this server directly with uvicorn
   - Ensure your endpoint is publicly accessible

After deployment, commit your policy on-chain:
    kinitro miner commit --endpoint YOUR_ENDPOINT_URL --netuid YOUR_NETUID

Endpoints:
    POST /reset - Reset policy for new episode
    POST /act   - Get action given observation
    GET  /health - Health check

Usage:
    # Local testing
    uvicorn server:app --host 0.0.0.0 --port 8000
"""

import json
import logging
import time
from contextlib import asynccontextmanager, contextmanager
from typing import Any

import uvicorn
from fastapi import FastAPI, HTTPException

# Import your policy implementation
from policy import RobotPolicy
from pydantic import BaseModel

# Import from local rl_interface (self-contained for Basilica deployment)
from rl_interface import Action, ActionKeys, Observation

# =============================================================================
# Structured Logging
# =============================================================================


class StructuredLogger:
    """JSON structured logger for production observability."""

    def __init__(self, name: str):
        self.logger = logging.getLogger(name)
        self.logger.setLevel(logging.INFO)

        # Remove existing handlers to avoid duplicates
        self.logger.handlers = []

        # Add JSON handler
        handler = logging.StreamHandler()
        handler.setFormatter(logging.Formatter("%(message)s"))
        self.logger.addHandler(handler)

        self.name = name

    def _log(self, level: str, message: str, **kwargs: Any) -> None:
        """Emit a structured log entry."""
        entry = {
            "timestamp": time.strftime("%Y-%m-%dT%H:%M:%S%z"),
            "level": level,
            "logger": self.name,
            "message": message,
            **kwargs,
        }
        log_method = getattr(self.logger, level.lower())
        log_method(json.dumps(entry))

    def info(self, message: str, **kwargs: Any) -> None:
        self._log("INFO", message, **kwargs)

    def warning(self, message: str, **kwargs: Any) -> None:
        self._log("WARNING", message, **kwargs)

    def error(self, message: str, **kwargs: Any) -> None:
        self._log("ERROR", message, **kwargs)

    def debug(self, message: str, **kwargs: Any) -> None:
        self._log("DEBUG", message, **kwargs)

    @contextmanager
    def measure(self, operation: str, **extra: Any):
        """Context manager to measure and log operation duration."""
        start = time.time()
        try:
            yield
        except Exception as e:
            duration = time.time() - start
            self.error(
                f"{operation} failed",
                operation=operation,
                duration_ms=round(duration * 1000, 2),
                error=str(e),
                error_type=type(e).__name__,
                **extra,
            )
            raise
        else:
            duration = time.time() - start
            self.info(
                f"{operation} completed",
                operation=operation,
                duration_ms=round(duration * 1000, 2),
                **extra,
            )


# Initialize logger
logger = StructuredLogger("kinitro.policy")


@asynccontextmanager
async def lifespan(app: FastAPI):
    logger.info("Starting policy server")
    get_policy()
    yield
    await shutdown()


app = FastAPI(
    title="Robotics Policy Server",
    description="Miner policy endpoint for robotics subnet evaluation",
    version="1.0.0",
    lifespan=lifespan,
)

# Global state
_state: dict[str, Any] = {
    "policy": None,
    "request_count": 0,
    "error_count": 0,
}


def get_policy() -> RobotPolicy:
    """Get or initialize the policy."""
    policy = _state["policy"]
    if policy is None:
        with logger.measure("policy_load"):
            policy = RobotPolicy()
            _state["policy"] = policy
        logger.info(
            "Policy initialized",
            model_loaded=policy.is_loaded() if policy else False,
        )
    return policy


# =============================================================================
# Request/Response Models
# =============================================================================


class TaskConfig(BaseModel):
    """Task configuration for episode reset."""

    env_id: str | None = None
    env_name: str | None = None
    task_name: str | None = None
    seed: int | None = None
    task_id: int | None = None


class ResetRequest(BaseModel):
    """Request body for /reset endpoint."""

    task_config: TaskConfig


class ResetResponse(BaseModel):
    """Response body for /reset endpoint."""

    status: str = "ok"
    episode_id: str | None = None


class ActRequest(BaseModel):
    """Request body for /act endpoint."""

    obs: Observation


class ActResponse(BaseModel):
    """Response body for /act endpoint."""

    action: Action


class HealthResponse(BaseModel):
    """Response body for /health endpoint."""

    status: str
    model_loaded: bool
    uptime_seconds: float


# =============================================================================
# Endpoints
# =============================================================================

_start_time = time.time()


@app.get("/health", response_model=HealthResponse)
async def health_check():
    """
    Health check endpoint.

    Validators may call this to verify your endpoint is running.
    """
    policy = get_policy()
    uptime = time.time() - _start_time
    model_loaded = policy.is_loaded()

    logger.info(
        "Health check",
        status="healthy",
        model_loaded=model_loaded,
        uptime_seconds=round(uptime, 2),
        request_count=_state["request_count"],
        error_count=_state["error_count"],
    )

    return HealthResponse(
        status="healthy",
        model_loaded=model_loaded,
        uptime_seconds=uptime,
    )


@app.post("/reset", response_model=ResetResponse)
async def reset(request: ResetRequest):
    """
    Reset policy for a new episode.

    Called by validators at the start of each evaluation episode.
    Use this to reset any internal state and optionally condition
    your policy on the task configuration.

    Args:
        request: Contains task_config with environment/task info

    Returns:
        Status and optional episode ID
    """
    _state["request_count"] += 1
    task_config = request.task_config.model_dump()

    try:
        policy = get_policy()
        with logger.measure(
            "reset",
            env_id=task_config.get("env_id"),
            task_id=task_config.get("task_id"),
        ):
            episode_id = await policy.reset(task_config)
        return ResetResponse(status="ok", episode_id=episode_id)
    except Exception as e:
        _state["error_count"] += 1
        logger.error(
            "Reset failed",
            error=str(e),
            error_type=type(e).__name__,
            env_id=task_config.get("env_id"),
            task_id=task_config.get("task_id"),
        )
        raise HTTPException(status_code=500, detail=str(e))


@app.post("/act", response_model=ActResponse)
async def act(request: ActRequest):
    """
    Get action for current observation.

    Called by validators every timestep during evaluation.
    You have approximately 500ms to respond (configurable by validator).

    Args:
        request: Contains canonical observation (proprioceptive + vision)

    Returns:
        Canonical action payload
    """
    _state["request_count"] += 1

    try:
        policy = get_policy()

        obs = request.obs

        # Get action from policy (measure timing)
        start = time.time()
        action = await policy.act(obs)
        duration_ms = (time.time() - start) * 1000

        # Log periodically (every 100 requests) or if slow
        if _state["request_count"] % 100 == 0 or duration_ms > 100:
            logger.info(
                "Act request",
                duration_ms=round(duration_ms, 2),
                request_count=_state["request_count"],
                obs_dim=len(obs.proprio_array()),
                has_images=bool(obs.rgb),
            )

        if isinstance(action, Action):
            action_obj = action
        elif isinstance(action, dict):
            action_obj = Action.model_validate(action)
        else:
            # Default schema for backward compatibility
            schema = {ActionKeys.EE_TWIST: 6, ActionKeys.GRIPPER: 1}
            action_obj = Action.from_array(action, schema)
        return ActResponse(action=action_obj)
    except Exception as e:
        _state["error_count"] += 1
        logger.error(
            "Act failed",
            error=str(e),
            error_type=type(e).__name__,
            obs_dim=len(request.obs.proprio_array()) if request.obs else 0,
        )
        raise HTTPException(status_code=500, detail=str(e))


# =============================================================================
# Startup/Shutdown
# =============================================================================


async def shutdown():
    """Cleanup on shutdown."""
    logger.info(
        "Shutting down policy server",
        total_requests=_state["request_count"],
        total_errors=_state["error_count"],
    )
    policy = _state["policy"]
    if policy is not None:
        # Guard cleanup() call in case policy doesn't implement it
        cleanup = getattr(policy, "cleanup", None)
        if callable(cleanup):
            await cleanup()
        _state["policy"] = None


# =============================================================================
# Run with uvicorn
# =============================================================================

if __name__ == "__main__":
    uvicorn.run(app, host="0.0.0.0", port=8000)