Loading...
Loading...
Runtime context passed to compiled closures — the world as seen from inside
npx skill4agent add simhacker/moollm context"The context IS the world as seen from inside the closure." — Dave Ungar, on lexical scope
world.skills.skill_nameworld.playerworld.roomworld.turn // Current simulation turn
world.timestamp // ISO timestamp
world.adventure // Root adventure state
.name
.flags // Global boolean flags
.world_state // Global key/value state
world.player // Current player
.id
.name
.location // Path to current room
.inventory // Array of item ids
.buffs // Active buffs
world.room // Current room
.id
.name
.path
.exits
.objects
.is_dark
.is_dangerous
world.party // Party state
.members
.leader// When running object simulate/methods:
world.object // The object being simulated
.id
.state // Object's mutable state
// Methods are bound: world.consume_fuel(1)
// When action targets something:
world.target // The target
.id
.type // "object", "character", "room"
// When NPC is simulating:
world.npc // The NPC
.id
.goals
.stateworld.skills.<skill_name>// Skill "economy" → world.skills.economy
world.skills.economy.gold // 100
// Skill "pie-menu" → world.skills.pie_menu (underscore!)
world.skills.pie_menu.last_selection // "north"
// Skill "time" → world.skills.time
world.skills.time.hour // 14
world.skills.time.phase // "afternoon"foo-barfoo_barworld.emit("The lamp dies!") // Show message
world.narrate("Darkness falls.", "dramatic")world.trigger_event("GRUE_APPROACHES", { room: world.room.path })world.has("brass-key") // true/false
world.give("gold-coins") // Add to inventory
world.take("used-potion") // Remove from inventoryworld.flag("dragon_slain") // Get flag
world.set_flag("treasure_found", true) // Set flagworld.get("object.state.fuel") // Get by path
world.set("object.state.lit", true) // Set by pathworld.go("../maze/room-a/") // Move player
world.can_go("north") // Check exitworld.add_buff({ name: "Caffeinated", effect: { energy: +2 }, duration: 5 })
world.remove_buff("caffeinated")
world.has_buff("grue_immunity")world.log("Debug: fuel = " + world.object.state.fuel)simulate_js: (world) => {
if (world.object.state.lit) {
world.consume_fuel(1); // Call object method
if (world.object.state.fuel <= 0) {
world.extinguish(); // Call object method
world.emit("The lamp sputters and dies!");
if (world.room.is_dark && world.room.is_dangerous) {
world.trigger_event("GRUE_APPROACHES");
}
}
}
}guard: "player has the key AND room is not dark"
guard_js: (world) => world.has("brass-key") && !world.room.is_darkscore_if: "player is tired OR room is dark"
score_if_js: (world) => world.has_buff("tired") || world.room.is_dark# Skill "economy" needs to check gold
guard: "player has at least 10 gold"
guard_js: (world) => world.skills.economy.gold >= 10
# Skill "pie-menu" checks last selection
score_if: "last pie menu selection was north"
score_if_js: (world) => world.skills.pie_menu.last_selection === "north"world.skills.skill_name// Skill "economy" → world.skills.economy
world.skills.economy.gold
world.skills.economy.currency
// Skill "pie-menu" → world.skills.pie_menu (underscore!)
world.skills.pie_menu.last_selection
world.skills.pie_menu.hover_direction
// Skill "foo-bar" → world.skills.foo_bar
world.skills.foo_bar.some_stateskill-nameskill_name// Object defines:
methods:
consume_fuel: "reduce fuel by amount"
// At runtime, method is bound:
world.consume_fuel(1) // Works!_js_py# Natural language
guard: "player has the key AND room is not dark"
# BOTH generated:
guard_js: (world) => world.has("brass-key") && !world.room.is_dark
guard_py: lambda world: world.has("brass-key") and not world.room.is_dark| Runtime | Purpose |
|---|---|
| Python | Server-side simulation, testing, LLM tethering |
| JavaScript | Browser runtime, standalone play |
world.playerworld.roomworld.has()world.emit()- event: COMPILE_EXPRESSION
field: guard
source: "player has the key"
targets:
- field: guard_js
language: javascript
- field: guard_py
language: python
expected_type: booleanclass World:
"""Python runtime context — mirrors JavaScript World class."""
def __init__(self, adventure_data):
self.turn = 0
self.adventure = adventure_data
self.player = adventure_data['player']
self.room = None # Set on navigation
self.party = adventure_data['party']
self.object = None # Set during object simulation
self.skills = {} # Skill state namespaces
def has(self, item_id: str) -> bool:
return item_id in self.player.get('inventory', [])
def flag(self, name: str) -> bool:
return self.adventure.get('flags', {}).get(name, False)
def emit(self, message: str):
print(message) # Or queue for output
def trigger_event(self, name: str, data=None):
# Event system handles this
passclass World {
/** JavaScript runtime context — mirrors Python World class. */
constructor(adventureData) {
this.turn = 0;
this.adventure = adventureData;
this.player = adventureData.player;
this.room = null; // Set on navigation
this.party = adventureData.party;
this.object = null; // Set during object simulation
this.skills = {}; // Skill state namespaces
}
has(itemId) {
return (this.player.inventory || []).includes(itemId);
}
flag(name) {
return (this.adventure.flags || {})[name] || false;
}
emit(message) {
console.log(message); // Or queue for UI
}
triggerEvent(name, data) {
// Event system handles this
}
}RUNTIME-CONTEXT — The world passed to closures (Python + JavaScript)