clone

a browser grid puzzle game where you push words around to rewrite the rules of each level

main
miau-is-true / openspec / changes / archive / 2026-09-23-build-dynamic-rule-engine / specs / rule-engine / spec.md
spec.md 8.7K source

ADDED Requirements

Requirement: The engine SHALL represent a deterministic stacked grid state

The engine SHALL represent a level as a bounded rectangular grid containing uniquely identified object and word entities. Multiple entities, including words, SHALL be allowed in one cell, and a turn snapshot SHALL include positions, entity kinds, word text, active rules, move count, win state, and the complete history needed for undo.

Scenario: Multiple words share a cell

  • WHEN two word entities occupy the same coordinate
  • THEN both remain addressable and rule scanning considers both word texts at that coordinate

Scenario: Invalid movement is rejected

  • WHEN a caller requests a diagonal move, an out-of-bounds move, or a move after the level is won
  • THEN the engine leaves state and undo history unchanged

Requirement: The engine SHALL derive the MVP rule vocabulary from movable words

The engine SHALL scan horizontal and vertical straight lines for NOUN IS VALUE sentences, with adjacent AND terms expanding the noun side or value side. The initial semantic properties SHALL be exactly YOU, PUSH, STOP, WIN, SINK, and DEFEAT; a noun-to-noun value SHALL be treated as a transformation. Rules SHALL be recomputed from the current word positions after a turn that changes board state.

Scenario: An AND sentence expands to multiple properties

  • WHEN the board contains BABA IS YOU AND PUSH in one straight line
  • THEN the active rule table contains BABA IS YOU and BABA IS PUSH

Scenario: A moved word changes the active rules

  • WHEN a legal move changes the position of a word entity
  • THEN the engine replaces the active rule table with rules parsed from the new word positions before the next player intent

Scenario: A malformed line is ignored

  • WHEN a word line lacks IS or has no noun and value on both sides
  • THEN the line produces no active rule and does not make the turn fail

Requirement: The engine SHALL resolve controlled movement and pushes atomically

Entities whose noun has YOU SHALL receive the requested cardinal intent. A moving entity SHALL push a contiguous chain of PUSH entities when every destination is in bounds and free of STOP entities; a blocked chain SHALL not move any member. Word entities SHALL be pushable by default so that players can edit rules.

  • WHEN a YOU entity moves toward one or more adjacent PUSH entities with an empty in-bounds destination
  • THEN the chain moves one cell in the requested direction and the move count increases by one

Scenario: STOP blocks a push

  • WHEN a requested movement would push an entity into a STOP entity or outside the grid
  • THEN no entity moves and the move count does not increase

Scenario: Several YOU entities use stable ordering

  • WHEN more than one YOU entity receives the same intent
  • THEN the engine resolves them in a documented stable order and produces the same state for the same initial state and intent

Requirement: The engine SHALL apply transformations and contact effects deterministically

After movement, the engine SHALL apply active noun-to-noun transformations, then resolve contact effects using the MVP properties. SINK SHALL remove all occupants of a contact cell, DEFEAT SHALL remove controlled entities sharing its cell, and WIN SHALL mark the level won when a controlled entity shares a cell with a winning entity. The engine SHALL expose the settled state only after these effects complete.

Scenario: A noun transforms

  • WHEN the active rule BABA IS KEKE is present after a successful turn
  • THEN every object with noun BABA becomes noun KEKE before the next rule scan

Scenario: Sink removes contact occupants

  • WHEN an entity with SINK shares a cell with another entity after movement
  • THEN every entity in that cell is removed before the turn is committed

Scenario: Defeat removes YOU

  • WHEN a YOU entity shares a cell with an entity having DEFEAT
  • THEN the controlled entity is removed and the level is not marked won by that contact

Scenario: Win marks the level complete

  • WHEN a YOU entity shares a cell with an entity having WIN
  • THEN the engine marks the level won and rejects later movement until reset or progression

Requirement: The engine SHALL support complete turn undo and reset

The engine SHALL save the complete pre-turn state before each successful movement and SHALL restore it with undo. reset SHALL restore the original level, clear history, clear the win state, and rebuild rules and derived effects.

Scenario: Undo restores a rule edit

  • WHEN a successful push moves a word and the caller invokes undo
  • THEN the word positions, active rules, objects, move count, and win state match the state before that push

Scenario: Undo with no history is harmless

  • WHEN the caller invokes undo before any successful move
  • THEN the engine returns a false or equivalent no-op result and leaves the state unchanged

Scenario: Reset returns to the level start

  • WHEN the caller invokes reset after any sequence of moves and effects
  • THEN the initial entities and rules are restored, move count is zero, win is false, and undo history is empty

Requirement: Active rules SHALL identify their source words

Each active rule SHALL expose the word entity IDs that formed it in reading order and whether the sentence runs horizontally or vertically. Duplicate sentences at different locations SHALL retain deterministic source information.

Scenario: A horizontal sentence reports its source

  • WHEN adjacent word entities form MIAU IS YOU from left to right
  • THEN the resulting rule identifies those word IDs in order and reports horizontal axis

Scenario: A vertical sentence reports its source

  • WHEN adjacent word entities form ROCK IS PUSH from top to bottom
  • THEN the resulting rule identifies those word IDs in order and reports vertical axis

Requirement: Engine actions SHALL return settled turn events

step, undo, and reset SHALL return an event record after resolving state. Every record SHALL have kind: 'step' | 'undo' | 'reset', accepted, ordered moved, full-record rulesAdded and rulesRemoved, ordered transformed, ordered destroyed, controlLost, and final won fields. A blocked or invalid step SHALL retain kind: 'step', report accepted: false, and leave state unchanged.

For movement, each entry SHALL be { id, from, to, pushedBy }, where pushedBy is the immediate pusher's entity id or null for a directly controlled mover. A physical block SHALL be { id, dir, blockerId, reason }, where blockerId is the blocking entity id or null at the board edge and reason is stop or bounds. Rule delta entries SHALL be complete rule records with subject, predicate, sourceIds, and axis. Destroyed entries SHALL be { id, noun|text, at, cause } with cause sink or defeat. Arrays SHALL preserve engine resolution order: movement, transformation, destruction, then win evaluation. controlLost SHALL be true when the settled state has no YOU entity.

Scenario: Breaking a rule produces a rule delta

  • WHEN a successful push moves a word out of a sentence
  • THEN the step result lists the word movement with from, to, and pushedBy, and lists the removed rule as a full record retaining its former sourceIds and axis

Scenario: A blocked push produces no state change

  • WHEN a push chain is blocked by STOP or the board edge
  • THEN the step result has kind: 'step', reports accepted: false, identifies id, dir, blockerId, and reason, and leaves move count and history unchanged

Scenario: Undo reports a restored state

  • WHEN a successful move is undone
  • THEN the undo result has kind: 'undo', reports accepted: true, the reverse movement with pushedBy: null, the rule changes, controlLost, and the restored win state

Scenario: Contact destruction includes presentation details

  • WHEN a SINK or DEFEAT interaction removes an entity
  • THEN the step result lists its id, noun or text, cell coordinate, and cause in the destruction order

Scenario: Control loss is explicit

  • WHEN a settled step removes the last entity with the YOU property
  • THEN the step result reports controlLost: true even though the state remains inspectable for the loss feedback

Scenario: Event arrays preserve resolution order

  • WHEN one accepted step moves entities, transforms an object, destroys contacts, and evaluates a win
  • THEN the event arrays retain movement order first, transformation order second, destruction order third, and the final win result after those phases