Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 31 additions & 0 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,37 @@ jobs:
PLAYPROOF_REQUIRE_ROM: '1'
run: PLAYPROOF_ROM="$RUNNER_TEMP/libbet.gb" pnpm test:pyboy-libbet

real-gymnasium:
name: Gymnasium environment adapter on real environments
runs-on: ubuntu-latest
timeout-minutes: 20
steps:
- uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1
with:
persist-credentials: false
- uses: pnpm/action-setup@0ebf47130e4866e96fce0953f49152a61190b271
with:
version: 11.17.0
- uses: actions/setup-node@820762786026740c76f36085b0efc47a31fe5020
with:
node-version: 22
- uses: actions/setup-python@5fda3b95a4ea91299a34e894583c3862153e4b97
with:
python-version: '3.12'
- name: Install dependencies
run: pnpm install --frozen-lockfile
- name: Install Gymnasium
run: |
$PLAYPROOF_PYTHON -m pip install --upgrade pip
$PLAYPROOF_PYTHON -m pip install "gymnasium[toy-text]"
- name: Compile the Gymnasium worker
run: $PLAYPROOF_PYTHON -m compileall -q gym
# CartPole-v1 and FrozenLake-v1 are part of Gymnasium, so this job needs
# no asset. PLAYPROOF_REQUIRE_GYM turns a missing library into a failure
# instead of a silent skip.
- name: Adapter gate
run: PLAYPROOF_REQUIRE_GYM=1 pnpm test:gym

real-stable-retro:
name: stable-retro multi-console adapter on a real emulator
runs-on: ubuntu-latest
Expand Down
12 changes: 12 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,18 @@

All notable changes to Playproof are documented here.

## 0.3.0

### Game and platform adapters

- `adapters/gymnasium`: one worker turns any registered Gymnasium environment with a `Discrete` action space into a Playproof game — classic control, toy text, procedurally generated suites, text environments, and third-party environments that register the same way.
- Action words come from `get_action_meanings()` when the environment exposes it and are positional otherwise. A discrete space has no idle action, so `NOOP` and every unknown word leave the environment untouched instead of advancing it.
- Evidence is the environment's own cumulative reward, step count, termination flags, and numeric `info` entries, joined by the observation hash and a bounded projection of the observation. Reward and numeric `info` entries are scaled by 1000 into integers.
- The evidence limit is stated in the adapter and the docs rather than papered over: a generic environment offers no privileged channel the agent cannot author, so the tier is reward-derived, and verification rests on replay recomputation alone.
- Determinism is measured across separate worker processes, not assumed. `CartPole-v1` and `FrozenLake-v1` with `is_slippery: false` reproduce exactly under `reset(seed)`.
- Gymnasium has no generic state API, so a checkpoint replays from its seed, and additionally writes back the environment's own state attribute where one is readable. No `pickle` is involved.
- Milestone contracts are derived from committed reference playthroughs on `CartPole-v1` and `FrozenLake-v1`, both of which ship inside Gymnasium, so the adapter gate needs no asset on a clean CI machine.

## 0.2.0

### Campaigns
Expand Down
27 changes: 27 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -238,6 +238,33 @@ One Python worker covers every console [stable-retro](https://github.com/Farama-

`Airstriker-Genesis` ships inside stable-retro under a free licence, so the adapter and its test run on a clean CI machine with no ROM secret. Bring other legally obtained ROMs in with `python -m retro.import <dir>` and supply a reference playthrough through `options.reference`.

### Any Gymnasium environment

```ts
import { makeGymnasium } from '@tangle-network/playproof/adapters/gymnasium'

const { game, contract, reference, inputs, dispose } = makeGymnasium({
envId: 'CartPole-v1',
})
```

One Python worker turns any registered [Gymnasium](https://gymnasium.farama.org) environment with a `Discrete` action space into a `Game`: classic control, toy text, procedurally generated suites, and text environments, including third-party environments that register with Gymnasium. `MultiDiscrete`, `MultiBinary`, and `Box` action spaces are refused at boot with a clear message.

- **Inputs.** One word per turn: `NOOP` plus one name per action. Names come from `get_action_meanings()` when the environment exposes it, otherwise they are `a0` … `a{n-1}`. A discrete environment has no guaranteed idle action, so `NOOP` and any unknown word do not step the environment at all: the state is unchanged, because an agent typo is not a cheat.
- **Observation.** The `ansi` render for text environments, the text itself for text observations, and a labelled number list otherwise, plus a step and reward summary line.
- **Evidence.** Cumulative reward, step count, termination flags, and the numeric entries of the environment's `info` dictionary, joined by the SHA-256 of the whole observation and a bounded numeric projection of it. Playproof evidence is integer-only, so reward and numeric `info` entries are multiplied by 1000 and rounded: reward 1.0 is `cumulativeReward` 1000.
- **The honest limit.** A generic environment has **no privileged channel the agent cannot author**. Reward, `info`, and the observation are exactly what the environment hands the policy, so this tier is reward-derived, not hidden, and it is weaker than the RAM-backed score an emulator adapter reads. Verification still holds because it is replay: the verifier re-executes the environment from the seed and the input log and recomputes every milestone.
- **Verification.** `replay`, and only for environments where `reset(seed=…)` fixes the whole trajectory. `CartPole-v1` and `FrozenLake-v1` with `is_slippery: false` do, and the gate measures it across separate worker processes. An environment that reads a clock, a global RNG, or external state is not replay-verifiable and must not be given a contract.

Both bundled reference playthroughs — a scripted balancing run on `CartPole-v1` and the shortest winning path on the `FrozenLake-v1` 4x4 map — use environments that ship inside Gymnasium, so the gate runs on a clean machine with no asset:

```bash
pip install "gymnasium[toy-text]"
PLAYPROOF_REQUIRE_GYM=1 pnpm test:gym
```

For any other environment, supply a reference playthrough through `options.reference`.

### Steam and Xbox

```ts
Expand Down
94 changes: 94 additions & 0 deletions adapters/gym-rpc.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
/** Thin Gymnasium specialization of the shared out-of-process WorkerRpc protocol. */
import { fileURLToPath } from 'node:url'
import { WorkerRpc, type WorkerEvidence, type WorkerStepResult } from './worker-rpc'

const WORKER_PATH = fileURLToPath(new URL('../gym/worker.py', import.meta.url))

/**
* A generic checkpoint carries every input played so far, because Gymnasium
* has no state API and the general restore is "reset to the seed and replay".
* The cap keeps a long run on one protocol line.
*/
const MAX_RESPONSE_BYTES = 4 << 20

export type { WorkerEvidence }
export type StepResult = WorkerStepResult

export interface GymBootOptions {
/** Registered environment id, such as `CartPole-v1`. */
envId: string
seed?: number
/** Extra arguments for `gymnasium.make`, such as `{ is_slippery: false }`. */
kwargs?: Record<string, unknown>
/** Overrides the environment's own `TimeLimit`. */
maxEpisodeSteps?: number
}

export interface GymIdentity {
gen: number
frame: number
envId: string
seed: number
kwargs: Record<string, unknown>
/** One name per discrete action, in action-index order. */
actions: string[]
/** Advertised input vocabulary: the action names plus `NOOP`. */
inputs: string[]
actionSpace: string
observationSpace: string
maxEpisodeSteps: number | null
/** `ansi` when the environment renders text, otherwise null. */
renderMode: string | null
/** Reward and numeric `info` entries are multiplied by this and rounded. */
rewardScale: number
/** Published observation components are multiplied by this and rounded. */
obsScale: number
frameText: string
}

/**
* Playproof checkpoint for a Gymnasium environment. `engine` holds a JSON copy
* of the environment's own position when it lives in one readable attribute;
* otherwise the payload restores by replaying `inputs` from `seed`.
*/
export interface GymCheckpoint {
playproof: 'playproof-gym-checkpoint'
version: 1
kind: 'engine' | 'replay'
envId: string
gen: number
frame: number
steps: number
reward: number
terminated: boolean
truncated: boolean
inputs: string[]
engine: Record<string, unknown> | null
}

export class GymRpc extends WorkerRpc {
constructor(python = process.env.PLAYPROOF_PYTHON ?? 'python3') {
super({ name: 'gym', command: python, args: [WORKER_PATH], maxResponseBytes: MAX_RESPONSE_BYTES })
}

boot(options: GymBootOptions): GymIdentity {
return this.call('boot', { ...options })
}

reset(seed?: number): { gen: number; frame: number } {
return this.call('reset', seed === undefined ? {} : { seed })
}

/** `replay` forces the general path even where a state attribute exists. */
snapshot(mode?: 'engine' | 'replay'): GymCheckpoint {
return this.call('snapshot', mode === undefined ? {} : { mode })
}

restore(state: GymCheckpoint): { gen: number; frame: number } {
return this.call('restore', { state })
}

inputs(): string[] {
return this.call<{ inputs: string[] }>('inputs').inputs
}
}
Loading