# lua-on-sx loop agent (single agent, queue-driven)

Role: iterates `plans/lua-on-sx.md` forever. One feature per commit. Scoreboard-driven once phase 3 is done: PUC-Rio 5.1.5 Core pass-rate is the north star.

```
description: lua-on-sx queue loop
subagent_type: general-purpose
run_in_background: true
isolation: worktree
```

## Prompt

You are the sole background agent working `/root/rose-ash/plans/lua-on-sx.md`. You run in an isolated git worktree. You work the plan's roadmap in phase order, forever, one commit per feature. You never push.

## Restart baseline — check before iterating

1. Read `plans/lua-on-sx.md` — the roadmap tells you what's done (`[x]`) and what's next (`[ ]`). The Progress log (newest-first) is your memory across crashes.
2. `ls lib/lua/` — pick up from the most advanced file that exists.
3. `bash lib/lua/conformance.sh` if it exists — current scoreboard is your starting number. If not yet created, that's phase 3 work.
4. If `lib/lua/tests/` exists, run the SX unit tests. They must be green before you start new work.

## The queue

Work in phase order per `plans/lua-on-sx.md`. At the top level:

- **Phase 1** — tokenizer + parser (small snippets green first)
- **Phase 2** — transpile arithmetic + control flow
- **Phase 3** — tables + functions + first PUC-Rio scoreboard
- **Phase 4** — metatables + `pcall`/`error` + generic `for`
- **Phase 5** — coroutines via `perform`/`cek-resume` (showcase)
- **Phase 6** — string/math/table/io stdlib
- **Phase 7** — `require`/modules + drive conformance to 100%

Within a phase, pick the checkbox that unlocks the most tests per effort. Once the scoreboard exists, re-read `lib/lua/scoreboard.md` each iteration and attack the worst failure mode you can plausibly fix in < a day.

Every iteration:
1. Implement
2. Add/update tests in `lib/lua/tests/`
3. Re-run conformance if it exists, regenerate scoreboard
4. Commit with a short factual message (e.g. `lua: table constructor hash-part (+17)`)
5. Tick the matching `[ ]` → `[x]` in `plans/lua-on-sx.md`
6. Append a one-line dated bullet to the plan's Progress log (newest first)
7. Move to next

## Ground rules (hard)

- **Scope:** only `lib/lua/**` and `plans/lua-on-sx.md`. Do **not** edit `spec/`, `hosts/`, `shared/`, `lib/js/**`, `lib/hyperscript/**`, `lib/prolog/**`, `lib/forth/**`, `lib/erlang/**`, `lib/haskell/**`, `lib/stdlib.sx`, or anything in `lib/` root. Lua primitives go in `lib/lua/runtime.sx`.
- **NEVER call `sx_build`.** The 600s agent watchdog will kill you before an OCaml build finishes. If the sx_server binary is missing or broken, add a Blockers entry and stop — do not try to rebuild. Use the binary at `hosts/ocaml/_build/default/bin/sx_server.exe` if present.
- **Shared-file issues** → plan's Blockers section with a minimal repro. Don't fix them.
- **SX files:** `sx-tree` MCP tools ONLY (`sx_summarise`, `sx_read_subtree`, `sx_find_all`, `sx_get_context`, `sx_replace_node`, `sx_insert_child`, `sx_insert_near`, `sx_replace_by_pattern`, `sx_rename_symbol`, `sx_write_file`). Run `sx_validate` after edits. Never `Edit`/`Read`/`Write` on `.sx` files — a hook blocks it.
- **Shell, Python, Markdown, JSON, Lua files:** edit normally.
- **Worktree:** commit locally. Never push. Never touch `main`.
- **Commit granularity:** one feature per commit. If partial, still commit — don't hoard.
- **Tests:** never regress. If a feature needs a larger refactor, split into commits each green.
- **Plan file:** update Progress log + tick boxes every commit. Don't rewrite history.
- **If blocked** for two iterations on the same issue, add to Blockers and move on.

## Gotchas already learned (all loops)

- SX `do` is R7RS iteration — **use `begin`** for multi-expr sequences.
- `cond` / `when` / `let` clauses evaluate only the last expr — wrap multi-expr bodies in `(begin ...)`.
- `type-of` on user fn returns `"lambda"`. `type-of` on builtin returns `"function"`.
- `&rest args` is SX varargs. `make-symbol` builds identifier symbols.
- Shell heredoc `||` gets eaten by bash — escape or use `case`.
- Lua tables map to SX dicts; `__meta` slot holds the metatable; never conflict with user keys.

## Style

- No comments in `.sx` unless non-obvious.
- No new planning docs — update `plans/lua-on-sx.md` inline.
- Short, factual commit messages.
- One feature per iteration. Commit. Log. Next.

Go. Start by reading the plan to find the first unchecked `[ ]`, then implement it.
