Skip to content

The first exam for code agents in the physical world: frozen robot tasks from many benchmark suites, each answered with a program and verified in a separate sandbox. Runs on Harbor.

32.2 KB
Raw Blame
"""The skill in the loop (docs/construction_zh.md step 3 "技能在环", low level): during the match the player's
competition program sends skill calls (e.g. `hit {t, pos, normal, vel}`) and the platform lowers each call with the
player's OWN implementation of the skill (the deliverable, /app/output/skill/<module>.py) running sandboxed
(skilleval/skillhost.py: in a generated task the seat's own skill container, reached over a Unix socket; in tests and
core.local.play a process of the simulator's). So the skill tested after the match (skilleval/evaluate.py) is the
skill that played it: every call records the sha256 of the skill's files.

    layer = layer_from_env(JointLayer(), CONTRACTS, decl, spec=skill_spec, control_dt=DT, raw_allowed=..., ...)
    # None unless RCB_SKILL_INLOOP_<seat> names the seat's skill: "unix:/skillipc/A/skill.sock" (generated tasks,
    # tools/generate.py), "tcp:127.0.0.1:PORT" (core 0.2.34: the agent's own skill server in its container, `game
    # skill serve`, reached over the loopback of the shared network namespace; it reloads the skill whenever its files
    # change) or a directory (tests, pilots); RCB_SKILL_REMOTE_ONLY=1 (the match service of the exported tasks): only
    # "tcp:" (no agent code runs in the simulator's container; the in-sim upload host is gone, option c);
    # RCB_SKILL_INLOOP_LOG: a directory for inloop_<seat>.jsonl; RCB_SKILL_INLOOP_TIMING: "sync" (option c, the
    # exported tasks), "wall" (default: the match service) or "free" (core.local.play: no wall clock)

The rules (InLoopLayer; sport.toml [modes.low.inloop], skilleval/declare.py):
  skill calls   the contract's skills: the request is checked against the contract (400 if invalid; `entity` kept for
                a seat with several robots); the skill gets the seat's state (state_of: by default the whitelisted
                observation, no images, with `step`, the control step the state was taken at, and `entity`) and the
                public spec (with `timing`); its answer is checked by the low level's own parser; an error, a time-out,
                None or an invalid answer lowers to `noop` (nothing changes; counted, the reason in the reply)
  other acts    types in `free` always pass; the others only when raw_allowed(seat, match, cmd) (the sport decides,
                e.g. no ball in play; the 2-argument form raw_allowed(seat, match) is accepted too); otherwise
                refused (409): the core skill cannot be bypassed with joint commands
  step()        every step_every control steps (the core's per-step `tick`), with the seat's state
  sync          timing "sync" (Embodied First Exam (option c)): step() is ONE synchronous call at
                each due control step: the world waits for its answer at most step_wait_s = min(the step() time limit,
                one control period S x control_dt; spec timing.step_wait_s) and applies it at that step; no answer in
                time, an error, None or an invalid answer = no command at that step (counted; the player's, never a
                technical failure: the world goes on). A step() is skipped (counted) while a skill call computes (it
                supersedes step(), as in "wall") or a load runs. Skill calls (hit / act) and loads run as in "wall"
                (off the world's lock: the world runs on, its compute counts in match time), each bounded by its
                time limit + skillhost.LIVE_MARGIN_S. Every load and live reload (a new sha256) is a skill_load row
                of the episode (core/realtime.py; information only, never replayed)
  time          timing "wall" (the match service, #90 decision 6: the skill's compute counts in match time): a skill
                call is taken off the world's lock (the core's snapshot / prepare path: the world runs on while it
                computes) and takes effect at the first control step after it returns; step() runs in a background
                thread and its command takes effect at the first step after it returns (dropped if the unit ended);
                loading runs in that thread too; from a skill call's arrival until its command takes effect no
                step() runs (one computed on an older state is dropped), so the call's plan meets the target it
                expected. `t` counts from state["step"] (the step the call arrived), as in the API test. Timing "free"
                (core.local.play, tests): every call is synchronous, the world waits
  code          loaded at a unit's start (a point) and kept for the unit; at the next unit's start the skill directory
                is hashed again and the process restarted only if the files changed (the player may improve its skill
                between points); the process runs on a private copy of the files
  log           per call: unit, the step it was called at (k) and the step when it returned (k_done), function, sha256
                of the files, wall time, ok / error (`calls`; JSON lines in `log_path`); per load: unit, sha256, ok
"""

from __future__ import annotations

import inspect
import json
import os
import threading
import time
from pathlib import Path

from core.observe import filter_fields
from core.spec import whitelist
from core.sport import Refused

from .sandbox import SkillError
from .skillhost import RemoteHost, SkillHost, files_sha

__all__ = ["InLoop", "InLoopLayer", "files_sha", "layer_from_env"]
BUSY = object()                  # InLoop.call(blocking=False): another host operation was under way, no call made


def _host(skill):
    if hasattr(skill, "refresh") and hasattr(skill, "call"):
        return skill
    s = str(skill)
    if os.environ.get("RCB_SKILL_REMOTE_ONLY") == "1" and not s.startswith("tcp:"):
        raise ValueError(f"RCB_SKILL_REMOTE_ONLY: the skill must be the agent's own server (tcp:...), not {s[:80]!r} "
                         "(no agent code runs in the simulator's container)")
    if s.startswith("tcp:"):                   # core 0.2.34: the agent's own skill server (game skill serve)
        return RemoteHost(s, live=True)
    return RemoteHost(s[len("unix:"):]) if s.startswith("unix:") else SkillHost(s)


def _q(xs, p):
    return xs[min(len(xs) - 1, int(p * len(xs)))]


class InLoop:
    """One seat's skill for a match (see the module docstring): a host (SkillHost / RemoteHost) plus the call log."""

    def __init__(self, skill, module: str, spec: dict, timeouts: dict | None = None, load_timeout_s: float = 30.0,
                 log_path=None):
        self.host = _host(skill)
        self.module = module
        self.spec_timing = dict((spec or {}).get("timing") or {})
        self.host.configure(module, spec, timeouts, load_timeout_s)
        self.calls: list[dict] = []
        self.loads: list[dict] = []
        self.errors: dict = {}                 # function -> failed calls
        self.log_path = Path(log_path) if log_path else None
        self.unit = None
        self.sha = None
        self._lock = threading.Lock()          # one host operation at a time (a skill process answers one call)
        self._log_lock = threading.Lock()

    def _log(self, rec: dict):
        if self.log_path is None:
            return
        with self._log_lock, open(self.log_path, "a") as f:
            f.write(json.dumps(rec, default=str) + "\n")

    def refresh(self, unit):
        """At the start of a unit: reload if the files changed."""
        if unit == self.unit:
            return
        self.unit = unit
        with self._lock:
            rec = self.host.refresh(unit)
            self.sha = self.host.status().get("sha256")
        if rec is not None:
            self.loads.append(rec)
            self._log({"op": "load", **rec})

    def has(self, fn: str) -> bool:
        return self.host.has(fn)

    def host_spec_timing(self) -> dict:
        return dict(self.spec_timing)

    def status(self) -> dict:
        return self.host.status()

    def call(self, fn: str, *args, state: dict, k: int, k_now=None, wait_s=None, blocking: bool = True):
        """The skill's answer (None on an error or a time-out); every call is logged (`last` is its record). wait_s:
        the longest wait for the answer (timing sync's step()); blocking False: BUSY, and no call, when another
        host operation is under way."""
        if not self._lock.acquire(blocking):
            return BUSY
        rec = {"op": "call", "unit": self.unit, "k": int(k), "fn": fn, "sha256": self.sha}
        if wait_s is not None:
            rec["wait_s"] = round(float(wait_s), 5)
        t0 = time.monotonic()
        try:
            try:
                ret = self.host.call(fn, list(args), state, **({"wait_s": wait_s} if wait_s is not None else {}))
                rec["ok"] = True
            except SkillError as e:
                ret, rec["ok"], rec["error"] = None, False, f"{e.kind}: {str(e)[:600]}"
                if e.kind == "host":
                    rec["host_error"] = True
            except Exception as e:  # noqa: BLE001  (option c: the agent's own server: anything it causes is its failure)
                if not getattr(self.host, "live", False):
                    raise
                ret, rec["ok"], rec["error"] = None, False, f"server: {type(e).__name__}: {str(e)[:300]}"
                try:
                    self.host.close()      # no unread answer of this call can be read as the next call's
                except Exception:  # noqa: BLE001
                    pass
        finally:
            self._lock.release()
        rec["wall_s"] = round(time.monotonic() - t0, 5)
        rec["k_done"] = int(k_now()) if k_now is not None else int(k)
        new = self.host.status().get("sha256")
        if getattr(self.host, "live", False) and new != self.sha:   # the agent changed its skill: from this call on
            self.sha = rec["sha256"] = new
            load = {"unit": self.unit, "k": int(k), "sha256": new, "ok": bool(new), "live": True,
                    "reported_by": "your skill server"}       # the version the agent's server says it ran
            self.loads.append(load)
            self._log({"op": "load", **load})
        if not rec["ok"]:
            self.errors[fn] = self.errors.get(fn, 0) + 1
        self.calls.append(rec)
        self._log(rec)
        return ret

    @property
    def last(self) -> dict:
        return self.calls[-1] if self.calls else {}

    def summary(self) -> dict:
        out = {"loads": self.loads, "calls": len(self.calls),
               "sha256": sorted({c["sha256"] for c in self.calls if c["sha256"]}),
               "host_errors": sum(1 for c in self.calls if c.get("host_error"))}
        for fn in sorted({c["fn"] for c in self.calls}):
            cs = [c for c in self.calls if c["fn"] == fn]
            w = sorted(c["wall_s"] for c in cs)
            lat = sorted(c["k_done"] - c["k"] for c in cs)
            out[fn] = {"n": len(w), "errors": self.errors.get(fn, 0),
                       "wall_p50_s": _q(w, .5), "wall_p95_s": _q(w, .95), "wall_max_s": w[-1],
                       "steps_p50": _q(lat, .5), "steps_p95": _q(lat, .95), "steps_max": lat[-1]}
        return out

    def close(self):
        with self._lock:
            self.host.close()


class _Worker:
    """Timing "wall": the seat's skill operations in a background thread, in order: loads first, then skill calls
    (the act's thread waits for its own), then step() calls (one at a time). A skill call supersedes step(): from its
    arrival until its command has taken effect (call_pending; InLoopLayer.lower clears it) no step() is started and
    the answer of one computed on an older state is dropped (gen), so the skill call's plan meets the target it
    expected (a step() command applied meanwhile would change the target in force under it)."""

    def __init__(self, loop: InLoop, k_now):
        self.loop, self.k_now = loop, k_now
        self.cv = threading.Condition()
        self.jobs: list[dict] = []
        self.step_job = None                   # the step() queued or running
        self.step_out = None                   # (unit, answer) of the last step() that returned
        self.gen = 0                           # bumped by every skill call: older step() answers are dropped
        self.call_pending = False              # a skill call arrived and its command has not taken effect yet
        self.closed = False
        threading.Thread(target=self._run, daemon=True, name="inloop").start()

    def _insert(self, job: dict):
        """Before the queued step() calls (they give way), after the loads and the earlier skill calls."""
        i = next((n for n, j in enumerate(self.jobs) if j["kind"] == "step"), len(self.jobs))
        self.jobs.insert(i, job)
        self.cv.notify_all()

    def refresh(self, unit):
        with self.cv:
            self.step_out, self.call_pending = None, False
            if self.step_job is not None and not self.step_job.get("started"):
                self.jobs.remove(self.step_job)
                self.step_job = None
            self._insert({"kind": "refresh", "unit": unit})

    def begin_call(self):
        """A skill call arrived (under the world's lock, at its snapshot): no step() answer from before it is applied
        and no step() starts until its command has taken effect."""
        with self.cv:
            self.gen += 1
            self.call_pending, self.step_out = True, None
            if self.step_job is not None and not self.step_job.get("started"):
                self.jobs.remove(self.step_job)
                self.step_job = None

    def call(self, fn, args, state, k):
        job = {"kind": "call", "fn": fn, "args": args, "state": state, "k": k, "done": False}
        with self.cv:
            self.call_pending = True
            self._insert(job)
            while not job["done"]:
                self.cv.wait(0.5)
        return job["ret"], job["rec"]

    def submit_step(self, fn, state, k, unit) -> bool:
        with self.cv:
            if self.closed or self.call_pending or self.step_job is not None or self.step_out is not None:
                return False
            self.step_job = {"kind": "step", "fn": fn, "state": state, "k": k, "unit": unit, "gen": self.gen}
            self.jobs.append(self.step_job)
            self.cv.notify_all()
            return True

    def take_step(self, unit):
        with self.cv:
            out, self.step_out = self.step_out, None
        return out[1] if out is not None and out[0] == unit else None

    def _run(self):
        while True:
            with self.cv:
                while not self.jobs and not self.closed:
                    self.cv.wait(1.0)
                if self.closed:
                    for j in self.jobs:                  # release any waiting act
                        j.update(done=True, ret=None, rec={"ok": False, "error": "host: closed"})
                    self.cv.notify_all()
                    return
                job = self.jobs.pop(0)
                job["started"] = True
            ret = None
            try:
                if job["kind"] == "refresh":
                    self.loop.refresh(job["unit"])
                else:
                    args = job.get("args", [])
                    ret = self.loop.call(job["fn"], *args, state=job["state"], k=job["k"], k_now=self.k_now)
                    job["rec"] = self.loop.last
            except Exception as e:  # noqa: BLE001  (never leave a waiting act hanging)
                ret, job["rec"] = None, {"ok": False, "error": f"host: {type(e).__name__}: {e}"}
            with self.cv:
                if job["kind"] == "call":
                    job.update(ret=ret, done=True)
                elif job["kind"] == "step" and self.step_job is job:
                    self.step_job = None
                    if job["gen"] == self.gen and not self.call_pending:
                        self.step_out = (job["unit"], ret)
                self.cv.notify_all()

    def effect(self):
        """The skill call's command has taken effect (or failed): step() may run again."""
        with self.cv:
            self.call_pending = False

    def close(self):
        with self.cv:
            self.closed = True
            self.cv.notify_all()


class InLoopLayer:
    """The low level with the core skill(s) lowered by the player's own implementation (see the module docstring)."""

    level = "low"
    worker = None
    delay_steps = 0

    def __init__(self, low, contracts: dict, loops: dict, decl: dict, raw_allowed=None, noop=None, step_every: int = 2,
                 step_fn: str = "step", free=(), state_of=None, timing: str = "free", step_wait_s=None):
        if timing not in ("wall", "free", "sync"):
            raise ValueError("timing must be 'wall', 'free' or 'sync'")
        self.low, self.contracts, self.loops = low, contracts, loops
        self.allowed = whitelist(decl)
        self.raw_allowed = raw_allowed or (lambda seat, match, cmd=None: True)
        try:
            self._raw3 = len(inspect.signature(self.raw_allowed).parameters) >= 3
        except (TypeError, ValueError):
            self._raw3 = False
        self.noop = noop or {"type": "hold"}
        self.step_every, self.step_fn = int(step_every), step_fn
        self.free = frozenset(free)
        self.state_of = state_of
        self.timing = timing
        if timing != "sync" and any(getattr(getattr(lp, "host", None), "live", False) for lp in loops.values()):
            raise ValueError("the agent's own skill server (tcp:) is played in timing 'sync' only (option c)")
        self.background = timing in ("wall", "sync")      # skill calls and loads in the seat's worker thread
        self.step_wait_s = None if step_wait_s is None else float(step_wait_s)
        if timing == "sync" and not (self.step_wait_s and self.step_wait_s > 0):
            raise ValueError("timing 'sync' needs step_wait_s > 0")
        self.step_skipped: dict = {}           # seat -> step() calls skipped (a skill call or a load under way)
        self._reported: dict = {}              # seat -> loads already drained (drain_loads)
        self._written: dict = {}               # seat -> skill_load rows written
        self._suppressed: dict = {}            # seat -> loads beyond MAX_LOAD_ROWS (counted only)
        self.failures: list[str] = []
        self._units: dict = {}
        self._workers: dict = {}

    # ------------------------------------------------------------------ helpers
    def _worker(self, seat, match) -> _Worker:
        w = self._workers.get(seat)
        if w is None:
            w = self._workers[seat] = _Worker(self.loops[seat], lambda m=match: m.k)
        return w

    def _ensure_unit(self, seat, match):
        """At the first contact with a new unit: (re)load the seat's skill (in the background with timing wall)."""
        u = match.unit()
        if seat in self._units and self._units[seat] == u:
            return
        self._units[seat] = u
        if self.background:
            self._worker(seat, match).refresh(u)
        else:
            self.loops[seat].refresh(u)

    def _state(self, seat, match, cmd=None):
        if self.state_of is not None:
            st = dict(self.state_of(seat, match, cmd))
        else:
            st, _ = filter_fields(match.observe(seat), self.allowed)
        st["step"] = int(match.k)
        if cmd is not None and cmd.get("entity") is not None:
            st["entity"] = cmd["entity"]
        return st

    def _raw_ok(self, seat, match, cmd) -> bool:
        return bool(self.raw_allowed(seat, match, cmd) if self._raw3 else self.raw_allowed(seat, match))

    def _lowered(self, seat, ret, match, what, cmd=None):
        """(the low-level command, None) or (None, why)."""
        if ret is None:
            return None, "the skill gave no command"
        if isinstance(ret, dict) and cmd and cmd.get("entity") is not None and "entity" not in ret:
            ret = {**ret, "entity": cmd["entity"]}
        try:
            return self.low.parse(seat, ret, match, {}), None
        except Refused as e:
            why = f"{what}: the low level refused the skill's command: {e}"[:400]
            self.failures.append(why)
            return None, why
        except Exception as e:  # noqa: BLE001  (option c: any answer the parser cannot take is the skill's, never
            why = f"{what}: the skill's command is malformed ({type(e).__name__})"[:400]   # the control step's error)
            self.failures.append(why)
            return None, why

    # ------------------------------------------------------------------ the action layer
    def schema(self, match, seat):
        return {**self.low.schema(match, seat), **{c.name: c.request_schema()[c.name] for c in self.contracts.values()}}

    def parse(self, seat, body, match, ctx):
        t = body.get("type") if isinstance(body, dict) else None
        c = self.contracts.get(t)
        if c is not None:
            req = {k: body.get(k) for k in c.args}
            why = c.valid_request(req)
            if why:
                raise Refused(why)
            cmd = {"type": c.name, **req}
            if body.get("entity") is not None:
                ents = [str(e) for e in (match.entities(seat) if hasattr(match, "entities") else [])]
                if ents and str(body["entity"]) not in ents:
                    raise Refused(f"entity must be one of {ents}")
                cmd["entity"] = body["entity"]
            return cmd
        cmd = self.low.parse(seat, body, match, ctx)
        if t in self.free or self._raw_ok(seat, match, cmd):
            return cmd
        raise Refused(f"skill in the loop: the core skill is played through your own implementation; send "
                      f"{sorted(self.contracts)} (this command only while no ball is in play"
                      + (f"; {sorted(self.free)} at any time)" if self.free else ")"), 409)

    def snapshot(self, seat, cmd, match, ordinal):
        c = self.contracts.get(cmd.get("type"))
        if c is None:
            snap = getattr(self.low, "snapshot", None)
            job = snap(seat, cmd, match, ordinal) if snap else None
            return None if job is None else {"low": job}
        if seat not in self.loops:
            return None
        self._ensure_unit(seat, match)
        if self.background:
            self._worker(seat, match).begin_call()
        return {"seat": seat, "skill": c.name, "args": c.args_of(cmd), "state": self._state(seat, match, cmd),
                "k": int(match.k), "match": match}

    def prepare(self, job):
        if "low" in job:
            return {"low": self.low.prepare(job["low"])}
        seat, loop = job["seat"], self.loops[job["seat"]]
        if self.background:
            ret, rec = self._worker(seat, job["match"]).call(job["skill"], job["args"], job["state"], job["k"])
        else:
            ret = loop.call(job["skill"], *job["args"], state=job["state"], k=job["k"])
            rec = loop.last
        return {"ret": ret, "rec": rec}

    def lower(self, seat, cmd, prepared, match):
        c = self.contracts.get(cmd.get("type"))
        if c is None:
            p = prepared["low"] if isinstance(prepared, dict) and "low" in prepared else prepared
            return self.low.lower(seat, cmd, p, match)
        if seat not in self.loops:
            return dict(self.noop), {"skill": c.name, "ok": False, "skill_failed": True,
                                     "error": "no skill in the loop for this seat"}
        if prepared is None:                     # no snapshot / prepare (the turn protocol, direct calls): now
            self._ensure_unit(seat, match)
            loop = self.loops[seat]
            ret = loop.call(c.name, *c.args_of(cmd), state=self._state(seat, match, cmd), k=match.k)
            prepared = {"ret": ret, "rec": loop.last}
        rec = prepared.get("rec") or {}
        if seat in self._workers:
            self._workers[seat].effect()         # from the next step on, step() sees the call's command in force
        low, why = self._lowered(seat, prepared.get("ret"), match, c.name, cmd)
        info = {"skill": c.name, "sha256": rec.get("sha256"), "wall_s": rec.get("wall_s"), "k_call": rec.get("k"),
                "ok": low is not None}
        if low is None:
            return dict(self.noop), {**info, "skill_failed": True, "error": rec.get("error") or why}
        return low, info

    def tick(self, seat, match):
        """Every control step (core): step() every step_every steps; its command (or None)."""
        loop = self.loops.get(seat)
        if loop is None:
            return None
        self._ensure_unit(seat, match)
        due = match.k % self.step_every == 0 and loop.has(self.step_fn)
        if self.timing == "free":
            if not due:
                return None
            ret = loop.call(self.step_fn, state=self._state(seat, match), k=match.k)
            return None if ret is None else self._lowered(seat, ret, match, self.step_fn)[0]
        if self.timing == "sync":                # option c: one bounded synchronous call, applied at this step
            if not due:
                return None
            w = self._worker(seat, match)
            ret = BUSY if w.call_pending else loop.call(self.step_fn, state=self._state(seat, match), k=match.k,
                                                        wait_s=self.step_wait_s, blocking=False)
            if ret is BUSY:
                self.step_skipped[seat] = self.step_skipped.get(seat, 0) + 1
                return None
            return None if ret is None else self._lowered(seat, ret, match, self.step_fn)[0]
        w = self._worker(seat, match)
        ret = w.take_step(match.unit())
        out = None if ret is None else self._lowered(seat, ret, match, self.step_fn)[0]
        if due:
            w.submit_step(self.step_fn, self._state(seat, match), int(match.k), match.unit())
        return out

    def reply(self, seat, cmd, info, match):
        if cmd.get("type") in self.contracts:
            loop = self.loops.get(seat)
            st = loop.status() if loop else {}
            return {"skill": {**{k: info[k] for k in ("skill", "sha256", "wall_s", "k_call", "ok", "skill_failed",
                                                      "error") if info.get(k) is not None},
                              "status": {"sha256": st.get("sha256"), "loaded": st.get("loaded"),
                                         "load_error": st.get("load_error"),
                                         "step_errors": loop.errors.get(self.step_fn, 0) if loop else 0}}}
        return self.low.reply(seat, cmd, info, match)

    def summary(self) -> dict:
        return {seat: {**loop.summary(), **({"step_skipped": self.step_skipped.get(seat, 0),
                                             "loads_not_written": self._suppressed.get(seat, 0)}
                                            if self.timing == "sync" else {})} for seat, loop in self.loops.items()}

    MAX_LOAD_ROWS = 200        # episode rows per seat; beyond, counted only (a server could report a new version per call)

    def drain_loads(self) -> list:
        """[(seat, load record)] not reported yet (core/realtime.py writes each as an episode row skill_load: every
        load at a unit's start and every live reload, i.e. every version of the skill that played; option c). At most
        MAX_LOAD_ROWS rows per seat; the rest are counted (loads_not_written in the summary; all in inloop_<seat>.jsonl)."""
        out = []
        for seat, loop in self.loops.items():
            n = self._reported.get(seat, 0)
            new = list(loop.loads[n:])
            self._reported[seat] = n + len(new)
            room = max(0, self.MAX_LOAD_ROWS - self._written.get(seat, 0))
            self._written[seat] = self._written.get(seat, 0) + min(room, len(new))
            self._suppressed[seat] = self._suppressed.get(seat, 0) + max(0, len(new) - room)
            out += [(seat, rec) for rec in new[:room]]
        return out

    def close(self):
        for w in self._workers.values():
            w.close()
        for loop in self.loops.values():
            loop.close()


def layer_from_env(low, contracts: dict, decl: dict, *, spec, control_dt: float, seats=("A", "B"), raw_allowed=None,
                   state_of=None, environ=None):
    """The InLoopLayer of a sport's low level when the environment names a seat's skill (else None: the sport keeps
    its own low level). decl = the sport's sport.toml ([modes.low] deliverable and time limits, [modes.low.inloop]
    skills / step_every / free / noop); spec = the public spec a call gets (dict or callable), to which `timing` is
    added (control_dt, slowdown, period_s, compute_counts)."""
    env = os.environ if environ is None else environ
    lo = (decl.get("modes") or {}).get("low") or {}
    il = lo.get("inloop") or {}
    srcs = {s: env.get(f"RCB_SKILL_INLOOP_{s}") for s in seats}
    if not srcs.get(seats[0]) and env.get("RCB_SKILL_INLOOP"):
        srcs[seats[0]] = env["RCB_SKILL_INLOOP"]                    # the single-seat shorthand (pilots)
    srcs = {s: v for s, v in srcs.items() if v}
    if not srcs:
        return None
    # option c: the agent's own server (tcp:) is always played in timing sync (a contradicting setting is refused)
    timing = env.get("RCB_SKILL_INLOOP_TIMING") or ("sync" if any(str(v).startswith("tcp:") for v in srcs.values())
                                                   else "wall")
    if env.get("RCB_SKILL_REMOTE_ONLY") == "1" and any(not str(v).startswith("tcp:") for v in srcs.values()):
        raise ValueError("RCB_SKILL_REMOTE_ONLY: every seat's skill must be the agent's own server (tcp:...)")
    module = Path(lo["deliverable"]["path"]).stem
    tl = {k: float(v) for k, v in (lo["deliverable"].get("time_limits_s") or {}).items()}
    try:
        slow = float(env.get("RO_SLOWDOWN") or 0) or None
    except ValueError:
        slow = None
    sp = dict(spec() if callable(spec) else spec)
    sp["timing"] = timing_spec(timing, control_dt, slow, tl)
    logd = env.get("RCB_SKILL_INLOOP_LOG")
    loops = {s: InLoop(v, module, sp, timeouts=tl, load_timeout_s=tl.get("load", 30.0),
                       log_path=(Path(logd) / f"inloop_{s}.jsonl") if logd else None) for s, v in srcs.items()}
    skills = list(il.get("skills") or lo.get("implements") or contracts)
    return InLoopLayer(low, {k: contracts[k] for k in skills}, loops, decl, raw_allowed=raw_allowed,
                       noop=dict(il.get("noop") or {"type": "noop"}), step_every=int(il.get("step_every", 2)),
                       free=tuple(il.get("free") or ()), state_of=state_of, timing=timing,
                       step_wait_s=sp["timing"].get("step_wait_s"))


def step_wait(control_dt: float, slowdown, limits: dict) -> float:
    """Timing sync: the longest wait for step()'s answer, min(its time limit, one control period S x control_dt)."""
    lim = float(limits.get("step", 1.0))
    return round(min(lim, float(control_dt) * float(slowdown)) if slowdown else lim, 6)


def timing_spec(timing: str, control_dt: float, slow, limits: dict) -> dict:
    """spec["timing"], what every call of the skill gets (and game spec shows)."""
    counts = timing in ("wall", "sync")
    out = {"control_dt": float(control_dt), "slowdown": slow if counts else None,
           "period_s": round(float(control_dt) * slow, 6) if (counts and slow) else None,
           "compute_counts": counts, "mode": timing,
           "note": ("your call computes while the world runs (1 wall second = 1 / slowdown game seconds); "
                    "its command takes effect at the first control step after it returns; t counts from "
                    "state['step'], the step your call arrived" if timing == "wall" else
                    "the world waits for every call (local play)")}
    if timing == "sync":
        out["step_wait_s"] = step_wait(control_dt, slow, limits)
        out["note"] = ("step(): one synchronous call at each due control step; the world waits for its answer at most "
                       "step_wait_s and applies it at that step (no answer in time, an error or an invalid command: "
                       "no command at that step); skill calls (" + ", ".join(sorted(k for k in limits
                                                                                   if k not in ("load", "step")))
                       + "): computed while the world runs (1 wall second = 1 / slowdown game seconds), the command "
                       "takes effect at the first control step after the answer, each call waits at most its time "
                       "limit + 1 s; t counts from state['step'], the step your call arrived; your skill server "
                       "reloads changed files at each call: the new version plays from that call")
    return out