Skip to content

Claim a run's simulator before using it and release it when the run ends - #171

Merged
bitomule merged 2 commits into
mainfrom
worktree-t-mav-157-simulator-targeting
Dec 3, 2026
Merged

bitomule merged 2 commits into
mainfrom
worktree-t-mav-157-simulator-targeting

Conversation

@bitomule

@bitomule bitomule commented Dec 2, 2026 •

Copy link
Copy Markdown
Owner

Closes #157.

Problem

A run could take a simulator another project was using, relaunch its app on it, and leave it locked:

  • With no simulator named, the booted fallback picked whatever was booted, even a simulator another project's run was driving.
  • mav open wrote the simulator lock only after the launch recipe had built, installed and relaunched the app. For the whole build, another project's run saw the simulator as free.
  • mav stop released only the simulator pinned in .mav/config.yaml. A run routed by target_command or the booted fallback kept its lock after it ended.
  • Commands of a live run (mav ui tree) re-ran target_command once its 2-minute cache expired. They could then target a different simulator than the run's own, and rewrite the run's target-command.json with it.

Change

Most of the change is in a new file, internal/mav/simclaim.go; the hooks in cli.go, target.go and matrix.go are small.

  • A run claims its simulator before using it. mav run claims it before its first step, mav open before booting the simulator or running the launch recipe. The read-decide-write happens under an flock on a guard file next to the lock, and lock files are written by temp file and rename, so two runs racing for one free simulator cannot both win.
  • Another project's live run is refused with sim_locked (--force still overrides).
  • The booted fallback skips simulators any other live run holds. If every booted simulator is held, it refuses with a new code, booted_simulator_held, instead of dispatching to simctl's booted.
  • Release by run id. Every lock a run took is released when it stops, fails or its lease expires, however its simulator was chosen. A failed standalone mav open gives its claim back.
  • Holders that live as long as the claim.
    • A flow holds its claim with its own process.
    • A standalone session hands its claim to its worker, which lives exactly as long as the session.
    • mav open holds the claim itself while it restarts the probe stream, so the lock never names a dead process mid-session (--no-relaunch, mav run --run <id>).
  • The run's commands follow the run. While a run holds its simulator, its commands target that simulator; the run records it in .mav/runs/<id>/simulator.json. target_command is still re-run once per cache TTL as a ping, so a pool's TTL lease (simpool lease) keeps being renewed. A command asking for another device (--device/--ios) routes through target_command as before.
  • Stale-target retry. The retry keeps the run's claim until it has secured another simulator, and restores the claim if the retry finds nothing better.
  • Matrix reservations go through the same guarded claim.

Runs of the same project (same root) still share a simulator: matrix children, a manual mav open session alongside a mav run, and concurrent mav runs in one checkout. Protection is between projects, which is what the issue reports.

Tests

internal/mav/simclaim_test.go adds 15 tests. With the wiring in cli.go and target.go reverted to main, 10 of them fail:

--- FAIL: TestBootedFallbackSkipsSimulatorHeldByAnotherProject
--- FAIL: TestOpenRefusesWhenEveryBootedSimulatorIsHeld
--- FAIL: TestOpenClaimsSimulatorBeforeBootingIt
--- FAIL: TestFailedOpenReleasesItsClaim
--- FAIL: TestStopReleasesLockOfTargetCommandSimulator
--- FAIL: TestFlowRunHoldsClaimDuringStepsAndReleasesItOnFailure
--- FAIL: TestFlowRunRefusesSimulatorHeldByAnotherProject
--- FAIL: TestCommandsTargetTheActiveRunsSimulator
--- FAIL: TestStaleRetryMovesTheClaimOnlyWhenItSecuresAnotherSimulator
--- FAIL: TestFlowOverExistingRunHoldsTheClaimThroughItsSteps

With the fix, all 15 pass. The other 5 pass on main too:

  • TestDeviceRequestBypassesTheRunsClaim and TestNoRelaunchOpenKeepsTheSessionClaimAlive guard behaviour the change must not break.
  • TestConcurrentClaimsOnOneSimulatorHaveOneWinner, TestClaimLeavesSameProjectHolderInPlace and TestClaimTakesOverStaleLock test the new claim code directly.

make check (gofmt, go test ./..., build) passes.

Docs

The README precedence section gains a "A simulator another run holds is never taken" section and lists simulator.json in the run state. The skill's lock guidance is updated.

@bitomule
bitomule force-pushed the worktree-t-mav-157-simulator-targeting branch from 06dedb9 to 6331ab3 Compare December 2, 2026 23:50
A run could take a simulator another project's run was using: with nothing
named, the booted fallback picked whatever was booted, and `mav open` wrote
its lock only after the launch recipe had rebuilt and relaunched the app.
`mav stop` released only a simulator pinned in config.yaml, so a run routed
by target_command or the booted fallback stayed locked after it ended.

- Claim the simulator atomically (flock on a guard file) as soon as it is
  resolved: `mav run` before its first step, `mav open` before it boots
  the simulator or runs the launch recipe.
- The booted fallback skips simulators another live run holds and refuses
  with booted_simulator_held when every booted simulator is held.
- Release every lock a run took, by run id, when it stops, fails or its
  lease expires; a failed standalone open gives its claim back.
- While a run holds its simulator, its commands (mav ui included) target it
  instead of re-running target_command, so an expired cache cannot move the
  run to another simulator or rewrite its target-command.json.

Closes #157
- Hold a standalone session's claim with its worker, and hold it with
  open's own process while open restarts the probe stream, so the lock never
  names a dead process mid-session (mav open --no-relaunch, mav run --run).
- The stale-target retry keeps the run's claim until it has secured another
  simulator, and restores it when the retry finds nothing better.
- While a run holds a target_command simulator, still re-run target_command
  once per cache TTL as a ping, so a pool's TTL lease keeps being renewed.
- Matrix reservations go through the same guarded claim, and every lock
  write is atomic (temp file + rename).
@bitomule
bitomule force-pushed the worktree-t-mav-157-simulator-targeting branch from 6331ab3 to 3f1e4bf Compare December 3, 2026 00:12
@bitomule
bitomule merged commit df180c0 into main Dec 3, 2026
1 check passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Simulator targeting: a run can take a simulator another project is using, and mav ui can resolve a different simulator than the active run

1 participant