# AGENTS.md - Coni WASM Apps Project Guide

## Project Overview

A collection of browser-based applications and games written in Coni, targeting WebAssembly. Apps are authored in `.coni` source files using Coni's Clojure-like syntax and compiled to Wasm for browser execution via two modes:

- **Dev Mode (Interpreter)**: Bundles the full Coni interpreter as a Go WASM binary (`main.wasm`) using `wasm_exec.js`. Slower startup, full runtime, hot-reloadable.
- **Release Mode (AOT)**: Compiles Coni source directly to Wasm-GC text format (`.wat`), assembled to `.wasm` via `wasm-tools`. Uses `coni_runtime.js` as a thin JS bridge. Fast startup, native performance.

## Build Commands

### Prerequisites
- A working `coni` binary in the sibling `coni-lang` directory (configured via `coni.edn`)
- `wasm-tools` for AOT binary assembly (install via `cargo install wasm-tools`)

### Dev Mode (Interpreter)
```bash
# Build interpreter WASM bundle for an app
make build-dev APP=game/striker1945

# Serve locally (uses index.dev.html)
make serve-dev APP=game/striker1945 PORT=8080
```

### Release Mode (AOT Compiled)
```bash
# AOT compile Coni source to native Wasm-GC
make compile-aot APP=apps/sound-nodes

# Serve locally (uses index.html with coni_runtime.js)
make serve-compiled APP=apps/sound-nodes PORT=8083
```

### Deploy
```bash
# Deploy a single app to production
make deploy-app APP=game/striker1945

# Build and deploy all apps
make deploy
```

## Project Structure

```
coni-wasm-apps/
├── Makefile              # Build, serve, deploy commands
├── coni.edn              # Compiler resolution config (points to coni-lang sibling)
├── wasm_exec.js          # Go WASM runtime (Dev Mode)
├── index.html            # Root landing page
├── shared/               # Shared assets
│   ├── edn-songs/        # Music data files
│   └── sound-engine/     # Audio engine modules
├── apps/                 # Application projects
│   ├── sound-nodes/      # Visual audio node graph editor
│   ├── dashboard-app/    # Dashboard
│   ├── drawing-app/      # Drawing tool
│   ├── music-player/     # Music player
│   └── ...
└── game/                 # Game projects
    ├── striker1945/       # Vertical scrolling shoot-em-up
    ├── wolfenstein/       # Raycasting FPS
    ├── tetris/            # Tetris clone
    ├── space-invaders-wasm/ # Space Invaders
    └── ...
```

### App Directory Layout
Each app/game directory follows this convention:
```
app-name/
├── app.coni              # Main Coni source (entry point)
├── index.html            # Release mode HTML (loads coni_runtime.js + app.wasm)
├── index.dev.html        # Dev mode HTML (loads wasm_exec.js + main.wasm)
├── style.css             # Styles
├── run.js                # Boot script (calls window.bootConiAOT or initWasm)
├── app.wat               # Generated: Wasm-GC text module (AOT output)
├── app.wasm              # Generated: Assembled Wasm binary (AOT output)
├── coni_runtime.js       # Generated: JS runtime bridge (AOT output)
├── main.wasm             # Generated: Interpreter bundle (Dev output)
└── assets/               # Images, sounds, sprites
```

## Code Style

All `.coni` files follow the conventions in the parent `coni-lang/AGENTS.md`:
- **Functions**: `kebab-case`
- **Keywords**: `:keyword-name`
- **Indentation**: 2 spaces
- **Length**: Use `count`, never `len`

### JS Interop (Critical for WASM apps)
```clojure
;; Property access
(.-propertyName obj)              ;; getter
(.-propertyName obj value)        ;; setter

;; Method calls
(.methodName obj arg1 arg2)

;; Global access
(js/global "document")
(js/global "window")

;; Object creation
(js-obj "key1" val1 "key2" val2)

;; Event binding
(js/on-event element :click handler-fn)

;; Unified Gestures / Complex Event Binding (using reframe and PointerEvents)
;; Unifies Touch, Mouse, and Pen interactions into a single clean interface
(require "libs/reframe/src/reframe_wasm.coni" :as rf)
(rf/set-attrs element
  {:on-pointerdown (fn [e] ...)
   :on-pointerup (fn [e] ...)})

;; Style mutation (use doto macro, never chain js/set)
(doto (.-style el)
  (.-color "#FFF")
  (.-fontSize "14px"))
```

### Validate Code Logic
- ALWAYS run `../coni-lang/coni lint inspect <file>` when modifying `.coni` code to ensure lint-clean source before testing.

## Key Libraries

Apps import from the `coni-lang/libs/` directory (resolved via `coni.edn` `:dependencies`):

| Library | Import | Purpose |
|---------|--------|---------|
| `js-game` | `(require "libs/js-game/src/game.coni" :as game)` | Canvas game framework (sprites, input, game loop) |
| `js-game/audio` | `(require "libs/js-game/src/audio.coni" :all)` | WebAudio sound effects and music |
| `reframe` | `(require "libs/reframe/src/reframe_wasm.coni" :as rf)` | Reactive UI framework (hiccup→DOM, state management) |
| `math` | `(require "libs/math/src/math.coni" :as math)` | Math utilities |
| `str` | `(require "libs/str/src/str.coni" :as str)` | String utilities |
| `webgl` | `(require "libs/webgl/src/webgl.coni" :as gl)` | WebGL Native Interop Library |
| `namakemono` | `(require "libs/namakemono/src/namakemono.coni" :all)` | Namakemono App Framework |

## Common Patterns

### WebGL Interop (webgl.coni)
- **Functional Wrappers**: Provides pure functional wrappers around imperative JavaScript WebGL APIs.
- **doto Macro**: Heavily utilizes the `doto` macro for elegant AST evaluation when mutating WebGL context state.
- **Buffer Flushing**: `gl-draw` synchronously flushes massive Array Buffers dynamically out of WebAssembly CGO natively executing drawing sequences against the GPU Graphics Driver.

### Namakemono Framework
- **Auto-Loading**: Namakemono's `run!` function automatically attempts to fetch `assets.json` and will automatically load `sprites.png` into the `:sprites` key if defined in the manifest.
- **Configuration**: Always pass an options map to `run!` containing `:init`, `:update`, `:draw`, and `:config`.
- **Blocking Eval**: Always end Namakemono apps with `(let [c (chan)] (<! c))` to block evaluation and prevent the program from exiting prematurely.


### Game Loop (Canvas-based games)
```clojure
(require "libs/js-game/src/game.coni" :as game)

(defn update-fn [state dt]
  ;; Return updated state map
  (assoc state :x (+ (:x state) (* dt 100))))

(defn render-fn [ctx state]
  (.clearRect ctx 0 0 w h)
  (.fillRect ctx (:x state) 100 32 32))

(game/start! {:update update-fn
              :render render-fn
              :state {:x 0}})
```

### Reactive UI (reframe apps)
```clojure
(require "libs/reframe/src/reframe_wasm.coni" :as rf)

(rf/reg-event-db :init (fn [db _] {:count 0}))
(rf/reg-event-db :inc (fn [db _] (update db :count inc)))
(rf/reg-sub :count (fn [db _] (:count db)))

(defn view []
  [:div {:class "app"}
    [:span (str "Count: " (rf/subscribe :count))]
    [:button {:on-click (fn [e] (rf/dispatch [:inc]))} "+"]])

(rf/dispatch [:init])
(mount "app-root" (view))
(mount-root)
```

### Sprite Loading (games)
```clojure
(def sprites (atom {}))

(defn load-sprite [name path]
  (let [img (.createElement (js/global "document") "img")]
    (.-src img path)
    (swap! sprites assoc name img)))

(defn spr [name] (get @sprites name))
```

## AOT Compilation Notes

### Generated Files (Do NOT Edit)
The following files are auto-generated by `coni compile-wasm` and should not be manually edited:
- `app.wat` — Wasm-GC text module
- `app.wasm` — Assembled binary
- `coni_runtime.js` — JS runtime bridge

### Cache Busting
When iterating on AOT builds, bump the version query strings in `index.html`:
```html
<script src="coni_runtime.js?v=12"></script>
<script src="run.js?v=12"></script>
```

### AOT Limitations
Some interpreter features are not yet fully supported in AOT mode:
- `defmacro`, `defprotocol`, `defmulti`, `defmethod` — compiled as no-ops
- `chan`, `<!`, `<!!` — concurrency primitives emit nil
- `dissoc` — not yet implemented (emits nil with warning)

## Debugging Tips

- **Dev Mode**: Use browser DevTools normally; `println` output goes to the browser console
- **AOT Mode**: Check the Firefox/Chrome console for `[Coni]` prefixed messages
- **Stuck/Hanging**: Force-stop in browser, check the stack trace — usually indicates an infinite loop from a compiler bug
- **Cache issues**: Hard refresh (`Cmd+Shift+R`) or bump `?v=N` in `index.html`
- **Lint before compile**: Run `../../coni-lang/coni lint app.coni` to catch syntax issues early

## STRICT AGENT RULES

- **ALWAYS LINT**: Before testing or compiling any changes to `.coni` files, you MUST run `../coni-lang/coni lint inspect <file1> <file2> ...`. Do NOT skip this step. Fix any syntax errors reported by the linter before proceeding.
- **SINGLE GAME STATE**: When creating or refactoring games, encapsulate all game state (player, enemies, score, etc.) into a single `*state*` atom rather than using multiple separate atoms.
- **AVOID EXPLICIT NILS**: When writing `if` statements that only execute for side-effects, do not provide an explicit `nil` for the false branch. Coni's 2-arity `if` implicitly returns `nil` when the condition is false.
