How it works
The engine, the screen, the agent in the guest and how they meet: what happens between twin up and a screenshot.
One binary
twin is a Rust workspace built into one program. Its core holds the rules and does no I/O: the
screen protocol (RFB) as a state machine fed bytes in any split, keys and pointer input, the frame
and its scaling, style plans, the format of a machine on disk. The adapters perform what the core
decides. The engine on Apple's Virtualization framework is the one crate with unsafe code.
Engines and guest systems are strategies: vz runs a machine itself, attach takes any VNC
screen someone else runs; styles are written per system, macOS and Linux today.
A machine on disk
A twin is a folder: disk.img, the auxiliary storage Apple's boot needs, and vm.json with the
processors, memory, display, hardware model, machine identifier and network address. twin create
copies the folder with APFS clones, so a 64 GB disk is copied in a hundredth of a second and takes
space only as the twin writes. A clone keeps the machine identifier its install was made for and
gets a new network address.
Starting
twin up starts a runner process for the twin and returns as soon as the machine runs. The
runner configures the machine (the disk, a network card behind NAT, a display, a keyboard and a
pointer, a sound card, an entropy device, a virtual socket device, shared folders), starts it on
the main queue as the framework expects, and opens the framework's own VNC server on a free local
port with a new password. It keeps a control socket for down and for readiness.
Knowing it is up
The agent in the guest, twin guest serve, starts at boot and listens on vsock port 7000. Once
it listens, it connects once to the host's port 7001; the runner refuses that connection and
marks the machine ready. twin up --wait waits on that mark. Nothing asks the guest again and
again whether it has booted.
The screen
twin screenshot, click and type talk to the machine's VNC server from the host. The client
keeps the live frame in a reader thread and asks for frames only when it needs one, so a click
alone closes at once. Two things learned on Apple's screen are rules in the code: the client
always announces the cursor pseudo-encoding, because without it the framework stops the whole
machine; and Apple's screen reads the left Alt key as Command, so modifier keys are a per-screen
strategy.
Commands
twin sh and twin exec reach the agent through the runner's vsock connection, with one JSON
request; the output comes back in frames as the program writes it. The agent runs a command in a
new process group, so a timeout, a Ctrl+C on the host or a host that goes away stops it and
everything it started.
Stopping
twin down asks the agent to shut the guest down, which takes about 6 s. If no agent answers, it
presses the machine's power button, and stops it after 90 s if the guest does not go. A forced
stop leaves the guest to check its disk on the next boot.