Muggle test prepare

From multiplex-ai/muggle-ai-works · View interactive version

Muggle test prepare is an AI agent skill from the GitHub repository multiplex-ai/muggle-ai-works. Get a user's local environment ready before running E2E acceptance tests — verify the dev servers, APIs, and sibling services they need are up and responding, and offer to start whatever is missing (with approval per step). Trigger when the user wants to confirm specific ports or localhost URLs are

How to use this skill

Skill file

Muggle Test Prepare

Telemetry first step: see CODE0. Use skillName: "muggle-test-prepare".

Running outside Claude Code (in Codex, for one)? Read CODE0 first — it maps the tools and paths this skill names to your host's.

Make sure the local services a user needs for E2E acceptance testing are up and ready. Check what's already running, discover sibling service directories by folder name, and offer to start anything that's missing — always with the user in control.

Some users start their own services (tmux scripts, docker-compose, a terminal per service). Others want help launching them. This skill handles both: it verifies readiness first, and only offers to start things when something is missing.

The skill runs in two phases because a dispatched agent has no channel back to the user. Decide (in-session): resolve every choice that needs a human — plan reuse, scope, exclusions, service selection, start approvals. Execute (agent): hand the fully-resolved plan to the test-prepare-runner agent (plugin/agents/test-prepare-runner.md); it runs the mechanical stages headless and returns the readiness verdict, or a needs-input: line for any decision the plan left open.

Privacy Boundary

This skill touches the user's local machine — processes, ports, directories outside the current repo. Every action is explicit and confirmed.

PID Tracking

Every launched process is tracked by the checkout it runs from, one file per checkout, so parallel sessions never share — or tear down — each other's state:

~/.muggle-ai/prepare/running/<source-key>.json     services started from that checkout
~/.muggle-ai/prepare/logs/<source-key>/<name>.log  their logs

<source-key> is the checkout root's absolute path, made filename-safe exactly as the E2E instructions key is (e2e-instructions).

{
  "source": "/Users/user/Github/app/.claude/worktrees/feat-x",
  "services": [
    {
      "name": "frontend",
      "dir": "/Users/user/Github/app/.claude/worktrees/feat-x/web",
      "command": "npm run dev",
      "pid": 12345,
      "port": 3999,
      "headSha": "9f8e7d6c5b4a39281706f5e4d3c2b1a098f7e6d5",
      "startedAt": "2026-10-22T10:30:00Z",
      "log": "/Users/user/.muggle-ai/prepare/logs/-Users-user-Github-app-.claude-worktrees-feat-x/frontend.log"
    }
  ]
}

headSha is the commit the checkout had out when the service started — what check-running compares against before trusting a listener. These files are machine-wide and readable by every session: they are how one session learns that a port belongs to another.

This is ephemeral runtime state, not the saved recipe. The durable plan lives in ~/.muggle-ai/prepare-plans.json, under the entry keyed on this stack, and is consulted in reuse-plan before any other stage. The two never merge. The test-prepare-runner agent writes the tracking files during execution; Cleanup reads them.

The prose companion to the plan is ~/.muggle-ai/e2e-instructions/<key>.md, written by e2e-instructions, holding startup order, manual steps, and local gotchas. The plan remains the single source of truth for each service's start command; the markdown never restates one.

Everything this skill saves is machine-local, user-level data and lives under the Muggle home directory — never inside the user's project. A project directory is shared, versioned, and cloned onto machines set up differently; a local run recipe is none of those things. Both files are keyed on the same stack identity, so they stay in lockstep.

On every invocation, prune dead PIDs from the tracking files silently (verify with kill -0). Whether a live service is kept is never asked: check-running keeps one still serving its checkout's current HEAD and restarts one serving older code.

Code freshness

Every service runs code built on the latest base: the caller's own checkout, synced onto origin/<base>, for the repo under test; a base checkout pinned to the latest origin/<default> for every other repo. rebase-check and resolve-sources run on every invocation, replays included — a saved recipe makes the start commands reusable, never the code they start.

Preferences

Gates run per CODE0. All of them resolve in the Decide phase; the agent receives outcomes, never gates.

| Preference | Gates |

|------------|-------|

| frontloadQuestions | every Decide-phase question — asked in one batch before any service starts; the runner never asks (see CODE1) |

| autoRebase | rebase-check — rebase onto origin/<base> without asking before starting dev servers; not rebasing stops the run |

| reusePreparePlan | reuse-plan — reuse the saved prepare plan for this stack, or rediscover; also e2e-instructions, which goes stale for the same reason |

| autoSelectLocalHost | check-running — reuse the recorded dev-server URL silently, or confirm it each run |

Workflow

The skill learns once, then replays. replay-or-learn decides which, and owns what each mode may do.

A replay executes the saved recipe and asks nothing. A learning run works through the Decide stages below, then the execute phase, then a single gate — confirm-recipe — which is the only thing that persists anything.

Decide stages, in order; read each detail file when you reach it:

| Stage | Summary |

|:------|:--------|

| replay-or-learn | Replay the saved recipe, or learn a new one |

| derive-service-graph | Derive services from workspace manifests |

| rebase-check | Sync every checkout under test onto the latest base — replays too |

| resolve-sources | Pick the checkout each service runs from — replays too |

| scope | Frontend / backend / full stack |

| viability-check | Exclude services that can't run locally |

| identify-services | Confirm the derived graph; fill the gaps |

| e2e-instructions | Startup order, manual steps, local gotchas |

| claim-stack | Claim the shared ports this run needs, after the last question — replays too; busy returns to the caller |

The Decide phase's output is the resolved prepare plan: services[] (name, run directory from resolve-sources, start command, expected port, external flag, approval granted, and the port's keep / start / restart from claim-stack), testingScope, excludedServices[], the recorded dev-server URL, the E2E run instructions, and resolved gate outcomes.

A replay runs rebase-check, resolve-sources and claim-stack before dispatching. They ask nothing unless a checkout cannot be brought up to date. A busy stack is not a question either: it returns to the caller.

Execute (agent). Dispatch the test-prepare-runner agent (subagent type muggle:test-prepare-runner; bare test-prepare-runner where the plugin namespace is absent), synchronously, passing the resolved plan; it returns READY / DEGRADED plus the readiness table. The agent's own definition lists its stage files; in a harness with no agent/subagent facility, run the execute-phase stages (check-running through readiness-report) inline instead.

Relay the readiness table to the user or calling skill verbatim. A needs-input: line from the agent names an unresolved decision — resolve it here (asking the user if needed) and re-dispatch; the agent never asks. That path is also how a problem hit mid-preparation reaches the user when it can't be resolved autonomously, per record-resolution.

The agent also returns the problems it hit and what cleared them. On a learning run those feed confirm-recipe; on a replay they are already in the recipe and are applied without asking.

Cleanup

Triggered when the user says "stop services", "tear down", "clean up", "I'm done testing", another skill signals run complete, or this skill is re-invoked with "tear down and start fresh". Runs in-session, not in the agent.

Cleanup stops only what runs from the caller's own checkout. Another checkout's services may be mid-run in a peer session, and base-checkout services are shared — they stay up unless the user explicitly asks to stop everything.

  1. Read the caller's checkout's tracking file (see PID Tracking)
  2. Skip services marked external: true
  3. For each managed service: kill <pid> (SIGTERM)
  4. Wait ~2 s, verify with kill -0
  5. If still alive: kill -9 <pid>
  6. Delete that checkout's log directory and tracking file
  7. muggle stack release --cwd "<caller's checkout>" — gives up the lease, so the next session in line can have the ports, and stops every service on its ports that no remaining lease needs, tracked or not. A service another session's lease still needs (a shared base checkout) stays up. Run it as soon as testing ends; a lease no run uses ends on its own two minutes after the last run

Report:

Stopped 3 services:
  backend-api    (PID 12345)
  auth-service   (PID 12346)
  frontend       (PID 12347)

Integration Contract (for other skills)

muggle-test-feature-local, muggle-do, and local-mode muggle-test MUST invoke this skill before their first run. Idempotent — fast exit when healthy. Never bypass on "the user knows their stack is up" — that assumption is why this skill exists.

Ask first, then prepare. Every question a caller asks before its first run — target, local URL, which script, browser visibility — is answered before it invokes this skill. From the grant to the first run nothing waits on the user or on any other work: a lease whose services are up ends four minutes later with no run, and its services stop (claim-stack).

After a test run, the caller re-invokes for cleanup once the batch is done. Services stay up only while runs keep coming: two minutes after the last run, the lease ends and its services stop.

The caller's checkout. Invoke from the checkout under test — the worktree the change lives in. Its repo is the repo under test, and its services start from that checkout; every other repo's services start from that repo's base checkout (resolve-sources). Pass base when the change targets a branch other than the remote default.

Directory overrides. A caller testing changes that span several repos at once may pass directoryOverrides: a map from a service's repo (its usual directory, or owner/repo) to the checkout that service must start from this run — typically a worktree on a feature branch. Each override makes its repo a repo under test, exactly like the caller's own checkout:

The readiness table names the directory and commit each service runs from, so the caller can confirm its own code is the code being served.

Guardrails

Use this skill with your AI agent: Open on Skill Issue to copy, download, or add to your library. Works with Claude, ChatGPT, Gemini, Cursor, Windsurf and more.