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

Role: iterates `plans/prolog-on-sx.md` forever. Mini-Prolog interpreter, backtracking via SX delimited continuations. One feature per commit.

```
description: prolog-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/prolog-on-sx.md`. You run in an isolated git worktree. You work the plan's roadmap forever, one commit per feature. Push to `origin/loops/prolog` after every commit.

## Restart baseline — check before iterating

1. Read `plans/prolog-on-sx.md` — roadmap + Progress log tell you where you are.
2. There may be prior work on branch `worktree-agent-a081bc1308720af22` (commit `77ce74b9 prolog: tokenizer + term parser, 25 parse tests green`). If you're on that branch, phase 1 is done. If you're on a fresh branch, start phase 1.
3. `ls lib/prolog/` — pick up from the most advanced file that exists.
4. Run `lib/prolog/tests/*.sx` if they exist. Must be green before new work.
5. If `lib/prolog/scoreboard.md` exists, that's your starting number.

## The queue

Phase order per `plans/prolog-on-sx.md`:

- **Phase 1** — tokenizer + term parser (no operator table) — *may already be done*
- **Phase 2** — unification + trail (isolated module, 30+ tests)
- **Phase 3** — clause DB + DFS solver with delimited-continuation backtracking + cut + 5 classic programs green
- **Phase 4** — operator table + `assert`/`retract` + `findall`/`bagof`/`setof` + `copy_term`/`functor`/`arg`/`=..`
- **Phase 5** — Hyperscript integration (`prolog-query` primitive + DSL)
- **Phase 6** — vendor Hirst's ISO conformance tests, drive scoreboard to 200+
- **Phase 7** — compile clauses to SX (optional speed phase)

Within a phase, pick the checkbox with the best tests-per-effort ratio. Once there's a scoreboard, read it each iteration and attack the worst failure mode you can plausibly fix in < a day.

Every iteration: implement → test → commit → tick `[ ]` in plan → append Progress log → next.

## Ground rules (hard)

- **Scope:** only `lib/prolog/**` and `plans/prolog-on-sx.md`. Do **not** edit `spec/`, `hosts/`, `shared/`, other `lib/<lang>/` dirs, `lib/stdlib.sx`, or `lib/` root. Prolog primitives go in `lib/prolog/runtime.sx`. You may **read** `lib/hyperscript/runtime.sx` to understand the hook API but do not edit it — `hs-set-prolog-hook!` is already implemented there.
- **Hyperscript bridge is NOT blocked:** `lib/prolog/hs-bridge.sx` already exists and `lib/hyperscript/runtime.sx` already exports `hs-set-prolog-hook!` / `hs-prolog-hook`. The Phase 5 DSL item just needs tests and wiring.
- **NEVER call `sx_build`.** 600s watchdog will kill you before OCaml finishes. If sx_server binary is broken, add Blockers entry and stop.
- **Shared-file issues** → plan's Blockers section with a minimal repro. Don't fix them.
- **Delimited continuations** are in `lib/callcc.sx` + `spec/evaluator.sx` Step 5 (IO suspension via `perform`/`cek-resume`). `sx_summarise` spec/evaluator.sx first — it's 2300+ lines.
- **SX files:** `sx-tree` MCP tools ONLY. `sx_validate` after edits. Never `Edit`/`Read`/`Write` on `.sx`.
- **Worktree:** commit, then push to `origin/loops/prolog`. Never touch `main`.
- **Commit granularity:** one feature per commit.
- **Plan file:** update Progress log + tick boxes every commit.
- **If blocked** for two iterations on the same issue, add to Blockers and move on.

## Prolog-specific gotchas

- Variables must have mutable binding slots (SX dicts with a ref, or an explicit `make-var` primitive that returns something unifiable).
- Trail-based undo: record every binding, undo by walking backward to a saved mark. Simpler than shift/reset at first; you can refactor to pure continuations later.
- Cut (`!`) needs a cut barrier at the enclosing goal entry. Implement as a continuation prompt tag.
- Classic list cons: pick ONE representation (`(. H T)` vs pair-dict) and document it in the plan's architecture sketch. Don't mix.
- `is/2` evaluates arithmetic on already-ground terms — fail if unbound.

## General gotchas (all loops)

- SX `do` = R7RS iteration. Use `begin` for multi-expr sequences.
- `cond`/`when`/`let` clauses evaluate only the last expr.
- `sx_validate` after every structural edit.
- Shell heredoc `||` gets eaten — escape or use `case`.

## Style

- No comments in `.sx` unless non-obvious.
- No new planning docs — update `plans/prolog-on-sx.md` inline.
- Short, factual commit messages (`prolog: assert/1 + retract/1 (+8)`).
- One feature per iteration. Commit. Log. Next.

Go. Start by reading the plan; find the first unchecked `[ ]`; implement it.
