DEV Community

Cover image for Building Chaos Lab: When Environment Variables Become Laws of Nature
Harish Kotra (he/him)
Harish Kotra (he/him)

Posted on

Building Chaos Lab: When Environment Variables Become Laws of Nature

Traditional game development involves hard-coding physics engines and biological rules. In Chaos Lab, we flipped the script. We built an application where the configuration file is the game engine.

The Architecture of Chaos

The project is built on a "Reactive Config" architecture. At its heart lies Varlock, a toolkit for creating AI-safe, schema-validated environment variables.

1. Defining Reality with Varlock

Instead of checking for process.env.GRAVITY manually, we define its behavior in .env.schema:

# @spec WORLD_GRAVITY
# @description The strength of planetary attraction.
# @type number(min=0.1, max=2.0)
WORLD_GRAVITY=1.0
Enter fullscreen mode Exit fullscreen mode

Varlock ensures that no matter how the user (or an AI agent) modifies the environment, it stays within "physically viable" limits.

2. The Real-time Mutation Loop

We use Chokidar on the server to watch for .env changes. When a change happens, we use Varlock's internal graph loader to validate the state:

const graph = await internal.loadVarlockEnvGraph({
  entryFilePaths: [path.join(__dirname, '.env.schema')]
});
await graph.resolveEnvValues();
const serialized = graph.getSerializedGraph();
// Broadcast currentValues via Socket.IO
Enter fullscreen mode Exit fullscreen mode

3. AI as the Biological Interpreter

The most exciting part is how we use Google Gemini. We don't just ask the AI to "tell a story." We feed it the Varlock Metadata.

By sending the descriptions and types defined in the schema, the AI "understands" the meaning of the numbers. If ATMOSPHERE_TOXICITY is high, the AI knows exactly why the creatures are gasping for air.

const prompt = `
  LAWS OF NATURE (Varlock Metadata): ${JSON.stringify(metadata)}
  CURRENT VALUES: ${JSON.stringify(values)}
  Describe the ecosystem evolution based on these specific constraints...
`;
Enter fullscreen mode Exit fullscreen mode

The "Simpler" UI Pivot

Initially, the UI was a dark, brutalist terminal. We recently pivoted to a "Light Lab" aesthetic using Tailwind CSS's subtle slate and blue palettes. This creates a clean, clinical environment that contrasts beautifully with the "Chaos" happening in the simulation.

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.

Demo

Github: https://github.com/harishkotra/chaos-lab

Top comments (0)