AGENTS.md instructions for /home/pjensen/Repos/JSHack
--- project-doc ---
AGENTS.md - JSHack Field Manual
JSHack is a zero-dependency browser roguelike: pure JavaScript, pure ES modules,
ECS architecture, Deno tooling. This file is optimized for coding agents. Prefer
the commands below over manual spelunking.
Deeper docs:
README.md
SEPARATION_MANIFEST.md
RUNTIME_TOPOLOGY_DOCTRINE.md
TEN_COMMANDMENTS.md
ecs-js/AGENTS.md
Hard Laws
No build step. Pure ES modules. No webpack, babel, JSX, TypeScript, or
bundling.
Deno, not Node. Tests and tools run with Deno. Default test command:
deno test --allow-read.
JavaScript only. Keep source as .js / .mjs.
Mobile-first. Touch is primary; desktop keyboard is secondary.
Separation is law.rules/ never imports display/ or bridge/;
display/ never imports rules/; bridge/ projects read-only rule state.
No system-to-system calls. Systems communicate with world.emit /
world.on; scheduler owns ordering.
No swallowed event failures. Do not add emitSafe-style wrappers around
world.emit.
Determinism is sacred. In rules simulation use world.rand(), never
Math.random(). No timers/fetch/promises/async in systems.
Integer grid only. World coords may be negative. Do not clamp to >= 0;
normalize with Number.isFinite(v) ? (v | 0) : fallback.
One canonical path. Spawn, damage, spell cast, status application,
inventory transfer, and materialization paths must delegate to the canonical
implementation.
Runtime multiplicity is child entities. Runtime
effects/procs/sockets/enchantments are child topology, not arrays on parent
components.
ECS-js is vendored. Do not edit src/lib/ecs-js/ unless fixing the ECS
library itself.
Search before writing. Extend existing patterns. Do exactly what was
asked; no opportunistic content wiring or unrelated cleanup.
Agent Commands
Run these before guessing.
Project Health
bash
deno run --allow-read tools/agent-health.mjs
deno run --allow-read tools/import-boundary-report.mjs
deno run --allow-read tools/system-map.mjs
agent-health summarizes boundary violations, remaining emitSafe refs,
nondeterminism hazards, generation-time async allowances, system counts, and
possible system-to-system calls. Classified allowances are context for agents;
headline hazards are the things to investigate first.
Event Bus
bash
deno run --allow-read tools/event-bus-explorer.mjs --format summary --top 40
deno run --allow-read tools/event-bus-explorer.mjs --format csv --event damaged
deno run --allow-read tools/event-bus-explorer.mjs --format mermaid --event damaged
deno run --allow-read tools/event-bus-explorer.mjs --producer-only --format summary --top 40
deno run --allow-read tools/event-bus-explorer.mjs --consumer-only --format summary --top 40
Use producer-only events to find emitted behavior with no consumer. Use
consumer-only events to find stale listeners, dynamic producers, or missing
producers.
System Map
bash
deno run --allow-read tools/system-map.mjs --format summary
deno run --allow-read tools/system-map.mjs --format csv
deno run --allow-read tools/system-map.mjs --phase effects
deno run --allow-read tools/system-map.mjs --unregistered
deno run --allow-read tools/system-map.mjs --missing-tests
Scheduler truth lives in src/main/scheduler.js. Some
imported-not-registered files are listener installers, not phase systems; audit
before deleting.
Target Lookup
bash
deno run --allow-read tools/agent-target.mjs damaged --limit 80
deno run --allow-read tools/content-id-audit.mjs goblin --limit 80
deno run --allow-read tools/content-id-audit.mjs potion_water --limit 80
agent-target is a ranked code search for events/functions/files/tests.
content-id-audit follows item, monster, spell, material, loot, visual, and
test references by ID.
Focused Tests
bash
deno test --allow-read tests/separationBoundaries.test.mjs
deno test --allow-read tests/determinismArchitectureGuards.test.mjs
deno test --allow-read tests/schedulerArchitectureGuards.test.mjs
deno test --allow-read tests/dealDamage.test.mjs
deno test --allow-read tests/castSpell.test.mjs
deno test --allow-read tests/interaction.test.mjs
deno test --allow-read tests/movementRefactored.test.mjs
deno test --allow-read tests/contentCatalogCanonical.test.mjs
Full runtime suite:
bash
deno test --allow-read --no-check
Plain deno test --allow-read may fail during type-check on non-game packaging
scripts before tests run. Use --no-check when you need runtime coverage.
rules/ deterministic simulation; no DOM, display, timers, async systems
bridge/ read-only projection from rules state to WorldView DTOs
display/ presentation only; reads WorldView and main-provided contracts
main/ wiring, lifecycle, input dispatch, scheduler setup
shared/ pure math/grid utilities; no ECS or DOM
lib/ vendored ecs-js and deity-js
content/ DSL-authored content definitions and content helpers
Five scheduler phases are declared in
src/main/scheduler.js:
text
ai intent producers
intents movement/combat/use/cast/interaction consumers
effects per-turn derived effects and world simulation
scripts content DSL tick hooks
cleanup entity removal, lifespan, spatial sync
The bridge contract is
src/bridge/schema/worldView.js. Display must
not push tags onto entity records; tag projection belongs in the bridge.
const INSTALLED = Symbol.for("jshack:domain:feature:installed");
export function installFeatureListeners(world) {
if (world[INSTALLED]) return;
world[INSTALLED] = true;
world.on("event:name", (payload) => {});
}
Install listeners from configureWorld() unless the feature is display/main
wiring.
New Systems
Add src/rules/systems/mySystem.js.
Register it in src/main/scheduler.js in the right
phase.
Add a focused test in tests/.
Run deno test --allow-read tests/mySystem.test.mjs.
Spatial Queries
Use forEachInRadius, forEachInRect, or tile query helpers. Do not full-scan
world.query() for proximity-sensitive behavior.
Spawning
Route through canonical archetypes/materialization helpers. Debug spawns,
dungeon population, and spawner children must have parity tests if they differ.
Events
Use direct world.emit(event, payload). If an event should be visible to
display/audio/messages, verify with:
bash
deno run --allow-read tools/event-bus-explorer.mjs --format csv --event event:name
Red Tests
Tests can be stale. Do not make tests green by restoring old behavior unless the
user explicitly asks for that behavior.
Classify failures:
Real bug: fix implementation.
Stale expectation: update the test to current intended behavior.
I want to make a new, specialized type of trap, this one is called the arrow trap. it is a combination pressure plate, and an aimed arrow shot directly at the pressure plate. what makes this trap unique is that it resets every turn.
also interesting is that since this trap resets, it can be used in an adversarial way by luring monsters toward the trap.
in an ideal situation, the arrows themselves are kind of nasty -- instead of flat damage, we could use all sorts of nasty things.
similar variant might magic missle trap but that can be left for a later exploration. we can start simply, simple arrows even.
the challenge here is that this is a two part trap, the plate, and an arrow that comes from adjacent wall -- I dont know that we need a full on shooter, perhaps that the arrow shoots from either left, right, top, down -- nearest wall.
Assistant
I’ll trace the existing trap, projectile, and dungeon placement patterns first so the arrow trap plugs into the current rules path instead of becoming a separate mechanism.
Assistant
The health check is clean: no boundary violations, no nondeterminism hazards, and no emitSafe references. The existing trap system and trap scripts are the integration point, with ranged projectile display already available through projectile:spawn and damaged payload metadata.
Assistant
There is an old test title saying monsters do not trigger traps, but the implementation now explicitly allows monsters to trigger nearby active traps. I’m treating the implementation and user request as the intended behavior, especially because the arrow trap is meant to be lureable.
Assistant
I also loaded the local RTK instruction; future shell commands will go through rtk as requested by the repo-level include. Next I’m making the minimal rules/display/test edits: reusable arrow behavior in the trap script, repeat handling in trapSystem, an archetype and generation table entry, then focused tests.
Assistant
The core behavior is now in place. I’m running the focused trap and architecture checks next; if any stale expectations show up, I’ll classify them before changing tests.
Assistant
Focused tests are green and agent health stayed clean. I’m doing a final diff pass now to catch naming, import ordering, or accidental scope drift before I wrap this up.
Assistant
One small cleanup from the diff pass: the arrow trap already uses ranged:shot and damaged for visible feedback, so I’m dropping the extra trap:arrow event to avoid adding an unconsumed event surface.
User
I do not like resetsEveryTurn: true why not resetsEvery: 1 to make for something generically applicable.
Assistant
Agreed. I’ll switch this to a numeric resetsEvery contract and make trap rearming generic instead of arrow-specific boolean behavior. That means a value of 1 can reset on the next world step, and larger values have a natural place to work later.
Assistant
Implemented the arrow trap with resetsEvery: 1, not resetEveryTurn.
Key points:
trapSystem now supports generic trap reset scheduling through params.resetsEvery, using params.resetAtStep internally.
Arrow traps fire from the nearest cardinal wall, emit the existing ranged:shot event for projectile/audio/VFX, and deal canonical arrow damage.
Monsters can trigger arrow traps, so lure plays work.
deno test --allow-read tests/trap.test.mjs tests/displayGlyphCoverage.test.mjs tests/separationBoundaries.test.mjs tests/determinismArchitectureGuards.test.mjs tests/schedulerArchitectureGuards.test.mjs
tools/agent-health.mjs
event-bus-explorer confirms ranged:shot is consumed by display/audio projectile wiring.
User
one issue, the trap is firing after I walk off the tile; that might be tricky to solve, and require a re-ordering of trap scheduling
Assistant
I’ll check where trap resolution sits relative to movement and cleanup. The likely fix is to rearm at the end of trap processing without allowing the same trap to fire again until a future tick, rather than rearming at the start and immediately seeing whoever has already moved away or onto it in the wrong phase.
Assistant
The trap currently runs after movement in the intents phase, so the bug is coming from my reset logic: I rearmed at the start of trapSystem, which lets a just-reset trap immediately evaluate the current post-move position in that same call. I’m changing reset processing so due traps rearm and then skip triggering until the next scheduler pass.
Assistant
I made the rearm skip same-pass trigger, but I want a regression test that matches the reported symptom, not just the internal assumption. I’m checking how world.tick advances world.step around the scheduler so the test models an actual turn.
Assistant
There’s another subtlety: in real world.tick, world.step increments before systems run and component writes are deferred until tick end. So I’m adding a world.tick regression that steps onto the plate, then steps off on the next tick, to verify the arrow trap does not fire on departure and only becomes armed afterward.
Assistant
The regression is now in place. I’m running the focused suite; if this still reproduces the departure shot, the next lever is moving reset processing to a later phase instead of guarding within trapSystem.
Assistant
The new regression passes, but the same-pass rearm guard is too conservative: it prevents repeat firing when someone intentionally stays on the plate. Since trapSystem already runs after movement, rearming at the start of that system is enough to avoid departure shots while preserving every-turn pressure. I’m removing the skip and keeping the departure regression.
Assistant
I’ve restored the every-turn firing semantics while leaving reset state numeric and generic. Now I’m rerunning trap tests and the architecture checks to verify both repeat-on-stay and no-fire-on-departure hold together.
Assistant
Fixed the reset contract and added a regression for the walk-off case.
What changed:
Arrow traps now use resetsEvery: 1.
trapSystem re-arms due traps at the start of trap processing, after movement has already resolved.
Added a world.tick regression proving: step onto plate fires, step off next turn does not fire, trap is then armed for a later actor.
Kept the every-turn behavior for actors who stay on the plate or monsters being lured through it.
right -- but your missing my broader point, walking ONTO the trap should trigger it, not walking off of it .
Assistant
That’s the right framing. The trap trigger should be edge-triggered by arrival onto the plate, not level-triggered by “someone is currently standing here” during trapSystem. I’m going to switch trap activation to consume movement arrivals, while keeping the old scan only as a fallback for tests/manual setups if needed.
Assistant
To make this precise, I’m adding a trap step listener installed from configureWorld. It records moved arrivals, and trapSystem will resolve only those arrivals against traps at the destination tile. That keeps disarm ordering intact because the queued arrival is still processed in the trap phase.
Assistant
This is a bigger semantic correction, so I’m updating the tests away from “actor already standing on trap when trapSystem scans.” The tests will now queue explicit arrival events, which matches the actual trigger rule: from != to and to is the trap tile.
Assistant
The trap suite is green after the arrival-event change. One related shock-trap test was still using the old “already standing on it” setup, so I’m updating that to step onto the trap as well.
Assistant
You were right: the trap should be arrival-triggered, not occupancy-triggered.
I changed trapSystem so traps now resolve queued moved arrivals onto trap tiles. configureWorld() installs a trap step listener that records movement events, then trapSystem resolves those arrivals in the normal trap phase. That preserves ordering with disarm, but avoids firing because an actor merely remains on or walks off a tile.
I also updated the tests to match the real semantic:
stepping onto a trap fires
walking off does not fire
reusable arrow traps can fire again only on a later arrival after resetsEvery
shock trap test now steps onto the plate instead of pre-positioning on it