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.
two new arch updates to ecs-js -- events can now be classes for better contracts, install gaurds have been made canonical. naturally this is a huge lift; but see what you can do to explore the terrain, how big of a lift it is.
Assistant
I’ll use the repo’s using-entire guidance for this exploration so the assessment is grounded in local patterns and history, then I’ll map the current event and install-guard surfaces before estimating migration size.
Assistant
I found the local command wrapper requirement too: shell commands in this repo should go through rtk. I’m checking Entire status and the current ECS/event API next, then I’ll quantify call sites rather than hand-wave the scope.
Assistant
The first broad grep confirms this is wide: thousands of direct string event call sites across rules, display, main, and tests. I’m narrowing now into “migration surfaces”: unique event names, producer/consumer counts, current ECS support for class events, and existing guard patterns.
Assistant
ECS-js already supports both legacy string events and new EcsEvent subclasses side by side. That means the migration does not have to be big-bang; the lift is mostly defining contracts and converting selected producer/consumer clusters coherently.
Assistant
The canonical install-guard update is already present as defineExtension / world.install(...), but the game code has not adopted it yet. The current game still uses many world-local Symbol.for(...installed...) guards, especially in scheduler-installed rule listeners and main/display wiring.
Assistant
One measurement command hit quoting/CLI-version friction, so I’m switching to simpler local scans instead of spending time on tooling. The headline numbers from the project’s own event explorer are already reliable: 1,789 source call sites and 589 unique literal events under src.
Assistant
The counts also show tests are a significant part of the work: they subscribe and emit directly to assert behavior. A class-event migration should budget for test helper updates, not just production code.
Assistant
I explored the terrain; no code changes made.
Event classes: big lift if done globally. The project’s event explorer reports src has 1,789 event call sites, 589 unique literal event names, 921 producers, 868 consumers, and 51 dynamic event sites. Including tests, direct grep finds 1,427 world.on(...) and 696 world.emit(...) call sites. This is not a single refactor; it is an incremental contract migration.
The ECS side is ready: core.js supports world.on(EventClass, fn) and world.emit(new EventClass(...)), while legacy string events still work. Tests cover non-cross-delivery between strings and classes.
The main design snag is layer ownership. If display needs to listen to a rules-produced class event, the class cannot live under rules/ without violating display’s no-rules-import law. Current src/shared/events/statusEvent.js is payload-helper only, and shared/ is documented as “no ECS”, so class contracts probably need either a new neutral contract layer or an explicit exception for event contract classes.
Install guards: medium lift and much more mechanical. ECS-js now has canonical extensions: defineExtension/world.install. Game code still has about 73 installed/listener/wiring Symbol.for guard patterns outside src/lib. The densest area is configureWorld, which directly calls many installX(world) listener functions.
Best migration shape there: convert each listener module to export a defineExtension(...) object, then keep the old installX(world) { return world.install(XExtension); } wrapper during migration. That preserves scheduler/tests while replacing bespoke guards one module at a time.
Recommended first slice: do install guards first for scheduler-installed rule listeners. It is lower risk, establishes the canonical pattern, and avoids the cross-layer event-contract question. For event classes, start with one high-value stable cluster like damaged, moved, or died, but only after deciding where cross-layer event contract classes live.
User
why dont you make two concrete moves; one in each direction to prove a couple simple cases.
migrate one event
migrate one installer gaurd
NOTE: I do not agree that events are not shared and can ref ecs-js -- that doesnt make sense, we since our aim is to get them under the yoke. ;-)
Assistant
I’ll make one narrow class-event migration and one canonical world.install migration, keeping both small enough to verify with focused tests. I’ll choose call sites with a contained producer/consumer surface so this proves the pattern without dragging half the bus along.
Assistant
I found a clean event candidate: guidance:pulse has one production path and two consumers, one display and one test. For the installer guard, installScoreListener is a minimal scheduler-installed listener with a single bespoke symbol guard.
Assistant
I’m going to add a shared GuidancePulseEvent that imports EcsEvent from ecs-js, then swap the producer and consumers to class routing. Separately, I’ll convert scoreSystem to a defineExtension export while keeping installScoreListener(world) as the compatibility wrapper.
Assistant
Both concrete moves are now in place. I’m running focused tests around the migrated event and score listener, then the architecture guards that catch boundary and scheduler regressions.
User
why not stay broadly within one system, surely scoring also had an event -- catch my drift?
Assistant
Yes, I catch it. The two proofs should share a domain so we’re proving a coherent migration path, not two unrelated examples. I’ll rework this: keep the score installer extension, and make the event proof about scoring’s own trigger path.
Assistant
Scoring does not produce a score-specific event today; it consumes the canonical died event. I’m going to make the scoring slice use a DiedEvent class while leaving the legacy "died" emission in place for the many other consumers. That is the practical bridge pattern for broad events.
Assistant
I’m removing the guidance example and replacing it with a scoring-centered event slice: DiedEvent as a shared ECS event class, dealDamage emitting it alongside the legacy string event, and scoreSystem consuming the class event through the new extension installer.
User
<turn_aborted>
The user interrupted the previous turn on purpose. Any running unified exec processes may still be running in the background. If any tools/commands were aborted, they may have partially executed.
</turn_aborted>
User
gross.
Assistant
Agreed. The guidance example was the wrong shape, and pivoting to global died would also be too broad for a scoring proof. I’m going to clean up my unrelated guidance changes first, then keep the score installer migration as the one valid piece unless we add a truly score-owned event.
Assistant
I’m reverting the guidance event slice now. After that I’ll only touch score-owned files/tests: installer guard stays in scoreSystem, and any event proof should be a score event emitted by that extension, not tutorial or global display wiring.
Assistant
The cleaner scoring slice is: define a DiedEvent contract, have canonical dealDamage emit it before the legacy "died" event, and move only scoreSystem onto the class subscription. That keeps legacy consumers untouched while proving the event-class path in the same domain as the installer migration.
Assistant
The score slice is now coherent: one extension-backed installer and one class-event migration for the score listener’s death input. I’m running targeted tests for the new score behavior plus death/damage and architecture guards.
Assistant
Targeted tests pass. One caveat I’m checking now: the diff summary didn’t show the new files because the filtered diff command only displayed tracked-file changes, so I’m verifying the untracked contract/test files are present before I wrap up.
Assistant
Cleaned up the unrelated guidance event change.
Made the two concrete moves inside the scoring/death path:
Added DiedEvent in deathEvents.js, importing EcsEvent from ecs-js.
Updated canonical dealDamage to emit new DiedEvent(...) before the legacy "died" string event, so existing consumers keep working.
Migrated scoreSystem.js to:
use defineExtension(...) / world.install(...) instead of its symbol install guard
consume DiedEvent instead of "died"
Added scoreSystem.test.mjs proving both the canonical installer guard and class-event subscription.
Verified:
deno test --allow-read tests/scoreSystem.test.mjs tests/dealDamage.test.mjs tests/deathModes.test.mjs
deno test --allow-read tests/separationBoundaries.test.mjs tests/schedulerArchitectureGuards.test.mjs
User
try that again, please; I think the system split might be fine.
User
<turn_aborted>
The user interrupted the previous turn on purpose. Any running unified exec processes may still be running in the background. If any tools/commands were aborted, they may have partially executed.
</turn_aborted>
User
??
User
<turn_aborted>
The user interrupted the previous turn on purpose. Any running unified exec processes may still be running in the background. If any tools/commands were aborted, they may have partially executed.
</turn_aborted>
User
si?
User
<turn_aborted>
The user interrupted the previous turn on purpose. Any running unified exec processes may still be running in the background. If any tools/commands were aborted, they may have partially executed.
</turn_aborted>
User
?
User
<turn_aborted>
The user interrupted the previous turn on purpose. Any running unified exec processes may still be running in the background. If any tools/commands were aborted, they may have partially executed.
</turn_aborted>
User
?
User
<turn_aborted>
The user interrupted the previous turn on purpose. Any running unified exec processes may still be running in the background. If any tools/commands were aborted, they may have partially executed.
</turn_aborted>
User
?
User
<turn_aborted>
The user interrupted the previous turn on purpose. Any running unified exec processes may still be running in the background. If any tools/commands were aborted, they may have partially executed.
</turn_aborted>
User
?
Assistant
Yes. I’m here. I’ll treat the last coherent request as: keep the scoring/death split if it looks fine, then re-check the current patch and tighten it rather than wandering into another subsystem.
Assistant
Current worktree is clean, so the interrupted attempt did not leave the scoring patch in place. I’m going to reapply the coherent version from scratch: one shared death event contract, dealDamage emits class plus legacy string, score listener consumes the class and uses world.install.
Assistant
The source is back at baseline. I’m editing only four files now: new death event contract, dealDamage, scoreSystem, and a focused score-system test.
Assistant
The patch is reapplied in the intended scoring/death slice. I’m running the focused tests and architecture guards now.
Assistant
Done, this time in one coherent scoring/death slice.
What changed:
Added DiedEvent in deathEvents.js.
Updated dealDamage.js to emit new DiedEvent(...) before the legacy "died" string event.
Updated scoreSystem.js to use defineExtension(...) / world.install(...) and subscribe to DiedEvent.
Added scoreSystem.test.mjs proving both the canonical installer guard and the class event path.
Verified:
deno test --allow-read tests/scoreSystem.test.mjs tests/dealDamage.test.mjs tests/deathModes.test.mjs
deno test --allow-read tests/separationBoundaries.test.mjs tests/schedulerArchitectureGuards.test.mjs
User
tell you what, lets make this happen organically
Assistant
Agreed. Best path is to let new/nearby work pull these patterns in naturally:
For any listener installer we touch, convert that installer to defineExtension(...) and keep the old installX(world) wrapper if callers already use it.
For any event we touch in a feature, introduce a class contract only when we are already editing both the meaningful producer and at least one real consumer.
For broad events like died, keep legacy string emission during migration and move consumers one by one.
Avoid standalone “migration sweeps” unless the touched subsystem has become internally consistent enough to finish cheaply.
That keeps this under the yoke without turning it into archaeology.
User
lets add this as a critical instruction to AGENTS.md
Assistant
I’ll update AGENTS.md with this as a project instruction, scoped to organic adoption of class events and canonical extensions rather than a forced migration.
Assistant
I found the right place: this belongs under Hard Laws because it changes how agents should touch events/listeners during normal work. I’m adding a concise bullet there.
Assistant
The instruction is in AGENTS.md. I’m doing a quick diff check to make sure the wording is scoped and no unrelated files are changed.
Assistant
Added the instruction to AGENTS.md under Hard Laws.
It now tells agents to adopt EcsEvent classes and defineExtension(...) / world.install(...) organically when touching relevant code, keep compatibility shims where needed, and avoid broad migration sweeps.