https://github.com/flammafex/hypertoken
Distributed simulation engine for games and multi-agent AI research featuring: CRDT state sync, OpenAI Gym integration, and P2P multiplayer โ no servers required.
https://github.com/flammafex/hypertoken
ai-training automerge card-games crdt game-engine mcp multiplayer openai-gym p2p pettingzoo python reinforcement-learning serverless simulation typescript webtrc
Last synced: 2 days ago
JSON representation
Distributed simulation engine for games and multi-agent AI research featuring: CRDT state sync, OpenAI Gym integration, and P2P multiplayer โ no servers required.
- Host: GitHub
- URL: https://github.com/flammafex/hypertoken
- Owner: flammafex
- License: apache-2.0
- Created: 2026-01-02T22:55:46.000Z (5 months ago)
- Default Branch: main
- Last Pushed: 2026-03-29T18:21:47.000Z (3 months ago)
- Last Synced: 2026-03-29T20:39:02.516Z (3 months ago)
- Topics: ai-training, automerge, card-games, crdt, game-engine, mcp, multiplayer, openai-gym, p2p, pettingzoo, python, reinforcement-learning, serverless, simulation, typescript, webtrc
- Language: JavaScript
- Homepage: https://hypertoken.ai
- Size: 2.51 MB
- Stars: 0
- Watchers: 0
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
- Notice: NOTICE
Awesome Lists containing this project
README
# ๐งฉ HyperToken
**A game engine where the entire state is a CRDT.**
Deterministic replay, serverless multiplayer, forkable worldsโall from one architectural decision. Built on **[Automerge](https://automerge.org/)** for distributed consensus, with **[OpenAI Gym](https://gymnasium.farama.org/)/[PettingZoo](https://pettingzoo.farama.org/)** interfaces so any game doubles as a training environment.
---
## ๐ What This Gets You
| Capability | How |
|------------|-----|
| **Serverless multiplayer** | CRDTs sync state across peers without a server |
| **Perfect replay** | Every action recorded with actor and timestamp |
| **Forkable worlds** | Snapshot state, explore alternatives, compare outcomes |
| **AI training environments** | Gym/PettingZoo interfaces out of the box |
| **Offline-first** | Peers diverge safely, converge mathematically |
```
Traditional Engine: Server decides โ clients accept โ monthly hosting bill
Blockchain Engine: Consensus decides โ everyone pays gas โ wait 15 seconds
HyperToken: CRDTs merge โ everyone agrees โ zero infrastructure
```
---
## ๐ฎ What You Can Build
**Card Games** โ Blackjack, Poker, Cuttle, custom TCGs. Tokens compose with provenance tracking.
**Strategy Games** โ Game theory simulations, tournaments, agent competitions. 14 Prisoner's Dilemma strategies included.
**Multiplayer Worlds** โ P2P sync, no servers, games that outlive their creators.
**Training Environments** โ Any game is automatically a Gym environment. Multi-agent via PettingZoo.
```bash
# Play now
npm run blackjack # Casino with AI & betting
npm run prisoners-dilemma # Game theory tournament
npm run poker # Texas Hold'em
npm run cuttle # Card combat
# Multiplayer
npm run blackjack:server # Host
npm run blackjack:client # Join
```
---
## โก Quick Start
```bash
git clone https://github.com/flammafex/hypertoken.git
cd hypertoken
npm install
npm run build
npm run blackjack
```
Pre-built WASM binaries included. See [Getting Started Guide](./docs/GETTING_STARTED.md) for the full walkthrough.
---
## ๐๏ธ How It Works
### Tokens Compose With Provenance
```javascript
// Merge tokens โ result tracks where it came from
const enchantedSword = engine.dispatch("token:merge", {
tokens: [sword, fireEnchantment],
resultProperties: { label: "Flaming Sword" }
});
// enchantedSword._mergedFrom = [sword.id, fireEnchantment.id]
// enchantedSword._mergedAt = timestamp
// Split tokens โ pieces track their origin
const pieces = engine.dispatch("token:split", {
token: goldPile,
count: 3
});
// pieces[0]._splitFrom = goldPile.id
```
### State Syncs Automatically
```javascript
const host = new Engine();
host.connect("ws://relay.local:8080");
const client = new Engine();
client.connect("ws://relay.local:8080");
// Both make changes โ CRDTs merge โ identical final state
// No conflict resolution code. No server logic. It just works.
```
### Any Game Becomes a Training Environment
```typescript
class BlackjackEnv extends GymEnvironment {
get observationSpace() { return { shape: [6] }; }
get actionSpace() { return { n: 4 }; } // hit, stand, double, split
async step(action: number) {
const result = this.game.act(action);
return {
observation: this.encodeState(),
reward: result.reward,
terminated: result.done,
truncated: false,
info: {}
};
}
}
```
### Fork State for What-If Exploration
```javascript
const snapshot = engine.snapshot();
// Try option A
engine.dispatch('agent:cooperate');
const cooperateOutcome = engine.getState();
// Rewind, try option B
engine.restore(snapshot);
engine.dispatch('agent:defect');
const defectOutcome = engine.getState();
// Compare and decide
```
---
## ๐ค AI & ML Integration
**Gym/PettingZoo** โ Single-agent and multi-agent interfaces. Turn-based (AEC) and simultaneous (Parallel).
**ONNX Export** โ Train anywhere, deploy the policy in browser or Node.js.
**Python Bridge** โ Connect to the TypeScript engine from Python for training.
**MCP Server** โ Let LLMs play games via [Model Context Protocol](https://modelcontextprotocol.io/).
```bash
npm run mcp:server # LLM integration
npm run bridge:blackjack # Python bridge
```
---
## โ๏ธ Architecture
```
hypertoken/
โโโ core/ # CRDT state management
โ โโโ Token.ts # Entities with provenance tracking
โ โโโ Stack.ts # Ordered collections (decks, piles)
โ โโโ Space.ts # Spatial zones (boards, hands)
โ โโโ Chronicle.ts # Automerge CRDT wrapper
โ โโโ ConsensusCore.ts # P2P synchronization
โ
โโโ core-rs/ # Rust โ WASM (67 typed actions)
โ โโโ src/
โ โโโ stack.rs # 10 stack operations
โ โโโ space.rs # 14 spatial operations
โ โโโ agent.rs # 16 agent operations
โ โโโ token_ops.rs # 7 token transformations
โ โโโ batch.rs # 8 batch operations
โ
โโโ engine/ # Game coordination
โ โโโ Engine.ts # Action dispatch, WASM integration
โ โโโ GameLoop.ts # Turn management
โ โโโ RuleEngine.ts # Condition-triggered actions
โ
โโโ network/ # P2P and server modes
โ โโโ PeerConnection.ts
โ โโโ AuthoritativeServer.ts
โ โโโ HybridPeerManager.ts
โ
โโโ interface/ # RL adapters
โ โโโ Gym.ts # Single-agent
โ โโโ PettingZoo.ts # Multi-agent (turn-based)
โ โโโ PettingZooParallel.ts # Multi-agent (simultaneous)
โ
โโโ examples/ # Working games
โโโ blackjack/
โโโ poker/
โโโ prisoners-dilemma/
โโโ hanabi/
```
---
## ๐ฎ The Philosophy
> "A token isn't valuable because of what it ISโit's valuable because of its relationships"
Tokens derive meaning from context:
- **Who owns it** โ agents, players
- **What's attached** โ enchantments, modifiers
- **Where it is** โ zones, positions
- **What it came from** โ merge/split provenance
- **What rules govern it** โ constraints, triggers
This applies to cards in blackjack, strategies in game theory, shares in a market, or NPCs in a world. The same engine handles all of them because the abstraction is right.
---
## ๐ Compared To
| System | HyperToken's Difference |
|--------|------------------------|
| **Unity/Godot** | Logic-first, no graphics dependency |
| **Colyseus** | P2P, no server required |
| **Blockchain games** | Same guarantees, zero gas fees |
| **Automerge/Yjs** | Game-aware abstractions (tokens, agents, rules) |
| **OpenAI Gym** | Built-in multiplayer, compositional tokens |
---
## ๐ Documentation
- [Action Reference](./engine/ACTIONS.md) โ All 67 actions
- [Architecture Guide](./docs/ARCHITECTURE.md) โ How components connect
- [Python Bridge](./docs/PYTHON_BRIDGE.md) โ PettingZoo integration
- [ONNX Export](./docs/ONNX_EXPORT.md) โ Deploy trained policies
- [Docker Guide](./DOCKER.md) โ Container deployment
---
## ๐ณ Docker
```bash
docker build -t hypertoken:latest .
docker compose up relay
docker compose run --rm quickstart
```
---
## ๐ License
Apache 2.0 โ Copyright ยฉ 2025 The Carpocratian Church of Commonality and Equality, Inc.
---
## ๐ฅ Credits
**Created by Marcellina II (she/her)**
Inspired by Martin Kleppmann's work on CRDTs, Rich Hickey's philosophy on state and time, and the legacy of HyperCard.
---
## ๐ Proemium to the Art of Tokens
*The All is number, and from number flow the forms of things.*
*For as the Monad abides in simplicity, so does it unfold the Dyad,*
*and from their tension spring the harmonies that sustain the world.*
*Among the arts that imitate the order of the heavens,*
*there now arises one most subtle and most justโthe Art of Tokens.*
*In this art, every being is rendered as a form in relation,*
*every action as a motion among forms,*
*and the laws that bind them are set forth as measure and correspondence.*
*Let none deem this art a toy of artifice.*
*It is the discipline by which the mind rehearses creation,*
*a mirror held to the pattern of the world-soul.*
*So may this art be given freely,*
*that all who love Wisdom may join the music of the spheres through understanding,*
*and that the harmony of minds may become the harmony of worlds.*
*For when reason is made common, the gods are near.*