Blog7 min read

Building an iOS SameGame Locally with Freehand, Ollama, and Qwen4

Final 5-color SameGame board, Cleared screen, and generated app icon built locally on an M5 Max with Freehand and Ollama

Back in April, I wrote up one-shotting an Apple Silicon monitor with Claude Opus 4.7. I am writing this post as another time-stamped marker for AI progress, eight months later, this time using only local resources: no cloud APIs, no API keys, and nothing leaving the laptop.

The stack was Microsoft’s Freehand (freehand 0.1.2) pointed at Ollama running qwen4:latest (qwen4_moe, 56.8B parameters, nvfp4, 35 GB on disk, 1M context, with native vision, tools, and thinking) on a MacBook Pro (Apple M5 Max, 18 CPU cores, 128 GB unified memory).

Over 82 minutes, with eight short prompts and two manual interrupts, it built a working SwiftUI SameGame for iOS where every dealt board is verified clearable: a platform-free Swift package, a CLI, 23 passing unit tests (checked across 200 seeds), iOS Simulator UI tests, a generated app icon, and a matching launch screen.

Why Freehand

Most coding-agent harnesses wrap a model in dozens of specialized tools, fixed planning stages, permission layers, and automatic context managers. Freehand (arXiv:2612.02442) takes the opposite bet: leave the decisions to the model.

Its entire runtime prompt is about 200 words, and it exposes one tool: bash. There are no separate file-edit tools, search tools, or hard-coded workflows. After each turn, the harness appends a single status line with the turn number, context size, tokens, and elapsed time. Even context management is handed back to the model: the conversation lives in an editable transcript.json file that the model can trim or reorganize itself whenever it wants.

Freehand’s own README notes that this minimal design is meant for frontier models, because removing tools and restrictions leaves the model with no scaffolding to lean on. That makes it a clean benchmark for a local model. If qwen4 can hold a 200-turn iOS build together with nothing but a shell and 200 words of instructions, you are measuring the model rather than a heavy harness.

In practice, that minimalism showed up on both sides of the session: Qwen4 drove xcodegen, xcodebuild, xcrun simctl, and swift test directly without fighting tool wrappers, and because nothing restricted how often it checked its work, it also ran tests compulsively until I stepped in.

The local config

Everything runs through ~/.freehand/config.json, pointed at Ollama’s OpenAI-compatible endpoint with auth turned off:

{
  "provider": "openai-chat",
  "api_base": "http://localhost:11434/v1",
  "auth": "none",
  "model": "qwen4:latest",
  "active_connection": "ollama",
  "connections": {
    "ollama": {
      "name": "Ollama (local)",
      "config": {
        "provider": "openai-chat",
        "api_base": "http://localhost:11434/v1",
        "auth": "none",
        "model": "qwen4:latest"
      }
    }
  }
}

I left context_window unset in this session, so Freehand did not auto-compact and let context grow to 165,791 tokens inside Qwen4’s 1M window. If you want Freehand’s context meter and compaction active, set "context_window" explicitly (Ollama’s /v1/models endpoint omits it).

One quirk worth knowing: freehand check prints reasoning effort low only because the smoke test hardcodes --effort low. Actual sessions default to high and send "reasoning_effort": "high" for qwen4 models. I left it on high for the whole run.

Eight prompts, two interrupts, and a 9-minute silence

I started in an empty folder with Xcode 27.2, xcodegen, and a booted iPhone 18 Pro simulator. Here is the opening prompt, verbatim:

lets build something cool here, how about samegame for ios, solvable boards

Freehand checked the empty directory, confirmed Xcode and the booted simulator, and scaffolded Package.swift. Then Turn 3 went quiet for 9.2 minutes, emitting 43,771 output tokens before writing Board.swift. At minute 11, I typed:

what is going on?

Looking at the transcript afterward, it had spent that turn reasoning through the board representation and reverse-construction rules before writing the engine. Once it settled on a plan (a platform-free SameGameKit Swift package tested on macOS first, wrapped by a SwiftUI iOS target), individual turns moved much faster.

Because Freehand drives bash directly, Qwen4 built with xcodebuild, installed via xcrun simctl, and launched the app on the booted simulator. From there I played on the simulator and pasted screenshots back into Freehand. Qwen4 supports native vision, so it read the screenshots directly.

The rest of the session, verbatim, typos included:

  1. 11:09 AM (pasted simulator screenshot): there are gaps though in the initial boards + also the cells should move left if an empty column emerges
  2. 11:22 AM: great works good, lets add a cell count header and bonus when someone clears - multiply scores by 10 and bonus based on number of left
  3. 11:30 AM (pasted simulator screenshot): the row with the number of cells per color is not there or hidden
  4. 11:38 AM (interrupted): this has been runnig for a long time, I can see the bar now + should be 5 colors
  5. 11:44 AM (interrupted): too much testing, looks good though, lets create a luanch screen and icon
  6. 11:49 AM: ok write the agents.md so i can restart the session later

Comparison of the 11:09 AM mid-session build with top-row gaps versus the 11:50 AM final 5-color board with a color count bar

The screenshot loop worked well. After I pasted the 11:09 AM capture showing missing blocks along the top edge, Qwen4 traced the short fill in Generator.swift, added left-shift column collapse, and added a 100-seed regression check ("Dealt boards have no gaps"). The score-bonus prompt wired up the x10 clear multiplier plus an efficiency bonus of up to 5,000 points (applied only when the board actually empties and reversed cleanly by Undo, visible on the Cleared screen in the hero image above). And the final prompt produced an 8 KB AGENTS.md summarizing the architecture, build commands, and invariants so a fresh session can pick up without carrying a 165k-token history.

How the clearable-board generator works

Random SameGame deals frequently dead-end. In Sources/SameGameKit/Generator.swift, Qwen4 combined reverse construction with rejection sampling:

  • Start from an empty grid and lay connected runs of same-colored blocks on top of adjacent columns so columns stay packed with no mid-column holes.
  • The fusion catch: you cannot replay those construction steps in reverse as the solution. Two separate placements of the same color that end up touching fuse into one larger connected component, so neither original placement is a legal move on its own.
  • Because of that, the guarantee comes from simulated verification and rejection: BoardGenerator runs an independent greedy sweep (clearGreedy, removing the largest legal group until empty) against the real Board rules. If a candidate board dead-ends, it is discarded and rebuilt with the next deterministic SplitMix64 seed.

Separately, Sources/SameGameKit/Solver.swift implements a forward depth-first search that shares no code with the generator and verifies small boards exhaustively in the test suite.

Session numbers

Freehand logs turn counts, timings, and token usage to its session transcript:

Metric Value
Wall-clock time 82.3 minutes (10:28 AM – 11:50 AM)
Agent turns 201 turns (194 bash tool calls)
Build and test runs 51 swift test runs, 55 xcodebuild runs, 9 simctl runs
Peak context 165,791 tokens
Total input tokens 18,805,102 (18,621,987 cached, 99.0% KV-cache hit rate)
Total output tokens 181,892
Code generated 1,620 lines of Swift across 15 files
Unit tests 23 tests in 7 suites (including 200-seed clearability checks), passing in 0.46 s
Cloud API cost $0.00

What it left on disk:

  • Sources/SameGameKit/: platform-free Swift package (Board, Game, Generator, Solver)
  • Sources/samegame/: CLI (swift run samegame 42)
  • App/SameGame/: SwiftUI iOS app (BoardView, GameModel, GameView, Palette, LaunchScreen) plus a programmatically rendered 4×4 app icon where no two neighboring blocks match
  • Tests/SameGameKitTests/ and AppUITests/: engine checks plus Simulator UI tests (tap to clear, hint outline, undo round-trip)

What worked and what didn’t

The biggest surprise was how well local KV caching held up across 201 turns. 18.8 million input tokens sounds unusable on a laptop until 99.0% of them hit Ollama’s cache; once warmed up, normal tool turns took seconds even past 150k context. Having native vision in the same local model also meant pasting iPhone Simulator screenshots worked without leaving the terminal.

The rough edges were mostly self-inflicted by reasoning_effort: high and loose prompts:

  • 9.2 minutes on Turn 3. On local qwen4, starting at medium effort and bumping to high only when stuck is a much better trade.
  • Compulsive re-testing. Running swift test 51 times and xcodebuild 55 times is why I interrupted it twice ("this has been runnig for a long time" and "too much testing, looks good though"). Tighter instructions about when to run full simulator builds would have cut half the wall-clock time.

One funny comparison at the end: when I rebuilt iSameGame 6 earlier this year, the trick that avoided color-fusion retries was choosing each cluster’s color at placement time so it never matched any neighbor it touched. Qwen4 hit the same fusion problem independently from an empty folder, then solved it with greedy verification and seed retries instead. Both produce boards that always clear; mine takes one attempt per board, and this one took 82 minutes, 35 GB of unified memory, and zero cloud calls.

Gavi Narra ·

How-to guides for our apps, and developer notes →