Phone farm — technical stack
How the phone farm is built: four farms on one main, exchange through SQLite WAL, a task's five gates, selector reconnaissance, flags, deploys, self-checks.
TL;DR
A Node hub enqueues tasks, a Python worker pool executes them, and they exchange data through a shared SQLite database in WAL mode. Five gates stand between "task created" and "finger touches the screen". Selectors are tied to the app version, and a task would rather fail than tap blindly. Deployment goes only through CI and can roll back code, but not the schema — and that is stated honestly.
Four farms, one main
The repository is shared by four farms in four countries — about 40 phones, from a local USB fleet to a dedicated Windows machine. There are no long-lived per-farm branches — only temporary ones per task. Farm-specific values never appear in the code at all: host, path, timezone, device serial number, email — all of it lives in each farm's own .env.
Two CI checks hold this in place. The first forbids new farm literals in the code. The second requires every new selector alias to carry a note saying on which farm and on which device it was scouted: an alias is a statement that "I verified this build on my phone", and a Pixel on one farm is not a Samsung on another.
Why exchange through the database, not over HTTP
The hub and the worker don't talk to each other over the network. Both work with one SQLite file in WAL mode, and that has three consequences. A single source of truth: a task's state can't diverge between two processes. The worker survives a hub restart and vice versa — the queue lives on disk, not in memory. And readers don't block the writer, so the panel can poll state as often as it likes without getting in the way of the work.
The cost of this decision is that both processes must live on the same machine. For the farm that is not a limitation: the phones hang off the USB of that same host anyway.
The life of a task
- The scheduler spreads tasks across the week based on the persona's schedule and activity weights — each persona has its own load profile.
- The task goes into the queue with a start time and a priority.
- The worker claims it with a per-phone lock: two processes can't touch the same screen at the same time.
- Before execution it passes five gates: the persona's activity window in its timezone; the night pause; profile parking; phone cooldown after an alarm; confirmed network connectivity of the device.
- The task runs under an expiring lease. If the worker dies, the lease expires and the task returns to the queue — except for tasks with consequences, which can't be blindly retried.
- The outcome is recorded explicitly: completed, failed with a reason, skipped, or set aside for manual review.
The last point is fundamental. If a restart cut off a task with an irreversible action, the system never retries it: the interrupted tap may already have reached the platform. A human decides the fate of such a task.
Reconnaissance before code
The main threat to mobile automation is an app update. A button moves, an identifier changes, and a blind script starts tapping the wrong elements: at best it opens "Not interested", at worst it reports someone else's account.
That is why no task is written against guessed selectors. The order is strict:
- Selectors live separately and are tied to the build version; there are no identifiers in the task body at all.
- The first line of every task after connecting to the device checks the map against the screen. On a mismatch — an exception, and the task fails in an understandable way.
- Raw screen dumps are stored per version: 10 apps, 75 builds.
- When the platform ships a new build, the check is supposed to break. That is not a bug but the design: the hub marks the version as outdated, and re-scouting follows.
On top of this runs the self-healing queue: the system captures the screen itself, proposes a map fix, and waits for a human. A human always approves.
Flags
376 flags with a registry that knows the dependencies between them: some flags are meaningless unless a neighboring flag is set first, and the registry checks that. The rule is simple — new behavior ships switched off. It is turned on by changing the configuration on a specific farm, not by editing code, so one farm can run with the new behavior while the others are still on the old one, and all of them are built from the same commit.
Deployment
Deployment goes only through CI: push to main → self-hosted runner → deploy script over a reverse SSH tunnel. Running the script by hand is forbidden by the rules, and a git hook blocks pushes to main.
The chain looks like this:
- Drain — cooperative. A flag file is set, the worker stops picking up new work and finishes what it started. The wait is up to 46 minutes, and on timeout it is the deployment that gets aborted, not a live task. The flag file is removed by a trap on any exit — a safeguard added after a specific incident in July.
- Database snapshot via
VACUUM INTO, followed by a readability check of the new file. A failed check kills the deployment before anything touches the schema. - Schema — additive and idempotent, applied at hub startup. There is no versioned migration runner, and no down migrations either.
- Health gate — a check that the newly started hub responds.
- Code rollback to the previous release if the build, unpacking, or dependency installation fails.
To be honest about the limits: a failed health gate does not trigger a rollback — the deployment fails, and the new hub stays deployed. The schema is never rolled back by design, so what is safe is rolling old code back onto the new schema, and the database snapshot is kept for a disaster and restored by hand.
The system doesn't trust its own reports
This is the most unusual part of the project. The daily digest doesn't list successes — it looks for places where a success was counted for nothing:
- Swallowed desync — tasks closed as a success or a skip while the screen map had drifted: no failure, the counters are silent, and the action never happened.
- "Screen confirmed, but zero rows" — not a breakage by itself, but this is exactly what a drifted map looks like when tasks are reporting success at the same time.
- Builds treated as equivalent that keep failing — cases where the machine made a decision without a human and the decision looks wrong.
- Repeat comments under one post — with the caveat that this is a permanent background: the farm can't erase what has already been published.
Underneath are 8173 tests in 358 files, 31 contract checkers, and mutation specs that verify a test actually kills the mutant rather than just being green.
Honest limits
- Scale is bound by physics: USB ports, power, space. Phones can't be deployed in the cloud.
- The hub and the worker must live on the same machine — the cost of exchanging through a database file.
- The night pause and persona windows reduce daytime throughput on purpose.
- A new app version stops work on it until re-scouting. That is a choice in favor of the account, not the metrics.