Building Chaos Lab: When Environment Variables Become Laws of Nature
Source: Dev.to
Overview
Traditional game development often hard‑codes physics engines and biological rules. In Chaos Lab we flipped the script: the configuration file itself becomes the game engine.
Reactive Config Architecture
The project is built on a Reactive Config architecture. At its core lies Varlock, a toolkit for creating AI‑safe, schema‑validated environment variables.
Varlock Schema
Instead of manually checking process.env.GRAVITY, we define its behavior in a .env.schema file:
# @spec WORLD_GRAVITY
# @description The strength of planetary attraction.
# @type number(min=0.1, max=2.0)
WORLD_GRAVITY=1.0
Varlock guarantees that, regardless of how a user—or an AI agent—modifies the environment, the values stay within “physically viable” limits.
Live Validation with Chokidar
On the server we use Chokidar to watch for .env changes. When a change occurs, Varlock’s internal graph loader validates the new state:
const graph = await internal.loadVarlockEnvGraph({
entryFilePaths: [path.join(__dirname, '.env.schema')]
});
await graph.resolveEnvValues();
const serialized = graph.getSerializedGraph();
// Broadcast currentValues via Socket.IO
AI Integration with Google Gemini
The most exciting part is how we feed Varlock metadata to Google Gemini. Rather than asking the AI to “tell a story,” we provide the descriptions and types defined in the schema so the model understands the meaning of the numbers.
const prompt = `
LAWS OF NATURE (Varlock Metadata): ${JSON.stringify(metadata)}
CURRENT VALUES: ${JSON.stringify(values)}
Describe the ecosystem evolution based on these specific constraints...
`;
For example, if ATMOSPHERE_TOXICITY is high, the AI knows why the creatures are gasping for air.
UI Evolution
The UI started as a dark, brutalist terminal. We recently pivoted to a Light Lab aesthetic using Tailwind CSS’s subtle slate and blue palettes, creating a clean, clinical environment that contrasts beautifully with the “chaos” happening in the simulation.
Conclusion
Chaos Lab demonstrates a new pattern: Schema‑Driven AI applications. By using tools like Varlock (https://varlock.dev) to provide structured context to LLMs, we can build reliable, interpretable, and highly dynamic systems that feel alive.
- GitHub: (link not provided in the original content)