# evanbacon/serve-sim architecture

> Architecture diagram of the GitHub repository evanbacon/serve-sim, generated by GitDiagram from its file tree, README and a sample of source files (last updated 2026-07-15). It is an AI-made overview: check details against the source.

- Interactive diagram: https://gitdiagram.com/evanbacon/serve-sim
- Repository: https://github.com/evanbacon/serve-sim

## Overview

#### Purpose and Entry Points
`serve-sim` hosts booted Apple Simulators for browser- and agent-driven interaction. Its primary CLI starts per-device streaming helpers and an optional preview server; command modes also issue direct simulator actions, query event logs, and manage camera injection. Primary entry points: `packages/serve-sim/src/index.ts`, `packages/serve-sim/src/runtime.ts`, `packages/serve-sim/src/middleware.ts`.

#### Process Topology and Device Lifecycle
The TypeScript runtime discovers/targets booted simulators, allocates ports, starts or connects to one helper per device, and persists helper state under `$TMPDIR/serve-sim/`. This state is the contract between detached helpers, CLI invocations, middleware, and UI. Multi-device session/device coordination is centered in `packages/serve-sim/src/device-session.ts`, `packages/serve-sim/src/device.ts`, `packages/serve-sim/src/state.ts`.

#### Native Streaming Pipeline
A bundled arm64 macOS Swift helper captures simulator frames through `xcrun simctl io`, transforms pixel buffers, encodes H.264 or JPEG/MJPEG, and exposes stream/control endpoints. The native module boundary is Swift/macOS frameworks rather than app instrumentation: `packages/serve-sim/Sources/SimNative/sim-module.swift`, `packages/serve-sim/Sources/SimNative/CaptureEngine.swift`, `packages/serve-sim/Sources/SimNative/H264Encoder.swift`. Runtime requirements are macOS, Apple Silicon, Xcode command-line tools/simctl, and Node 20+.

#### Browser Preview and Interaction
The React client renders simulator chrome, device frame, video, controls, tool panels, and multi-device layout. It consumes MJPEG or AVCC/H.264 streams and sends input over WebSockets. Key presentation/stream boundaries are `packages/serve-sim/src/client/client.tsx`, `packages/serve-sim/src/client/simulator/SimulatorView.tsx`, `packages/serve-sim/src/client/simulator/useSimStream.ts`. Browser actions are translated to HID/gesture, keyboard, rotation, and simulator commands through `packages/serve-sim/src/native.ts`, `packages/serve-sim/src/text-to-keys.ts`, `packages/serve-sim/src/exec-ws.ts`.

#### HTTP Middleware and Remote Exposure
`simMiddleware` embeds the same preview into Connect-compatible servers. It reads helper state and either directs clients to helper ports directly or proxies streams, control WebSockets, and DevTools through a single origin. This makes it suitable for Metro, Vite, Next, Express, or tunneled remote hosts. The server boundary is implemented in `packages/serve-sim/src/middleware.ts`, with port/state resolution in `packages/serve-sim/src/ports.ts` and `packages/serve-sim/src/state.ts`.

#### Accessibility, Logs, and DevTools
The system surfaces simulator accessibility snapshots, recent action/event logs, and WebKit inspection targets to browser tools and agents. Native accessibility extraction crosses into the TypeScript/UI layer; DevTools is exposed as a separately routed endpoint. Relevant components: `packages/serve-sim/Sources/SimNative/AccessibilityBridge.swift`, `packages/serve-sim/src/ax.ts`, `packages/serve-sim/src/devtools-targets.ts`.

#### Synthetic Camera Injection
Camera support is a separate host-helper/injected-dylib architecture. A macOS helper captures or generates BGRA frames from placeholder, file, or webcam sources into POSIX shared memory; a dylib injected with `DYLD_INSERT_LIBRARIES` swizzles AVFoundation in a selected simulator app to consume those frames. Its control socket permits source and mirror updates without relaunching the app. Core paths: `packages/serve-sim/Sources/SimCameraHelper/main.m`, `packages/serve-sim/Sources/SimCameraInjector/SimCameraInjector.m`, `packages/serve-sim/Sources/SimCameraInjector/SimCamFrameSource.m`.

#### Packaging and Agent Integration
The npm package builds TypeScript/browser bundles plus native artifacts, with development orchestration in `packages/serve-sim/build.ts` and `packages/serve-sim/dev.ts`. Agent-facing operating guidance ships as an Open Agent Skill rather than a runtime service: `skills/serve-sim/SKILL.md`, `skills/serve-sim/README.md`.

Main entry points: [`packages/serve-sim/Sources/SimNative/sim-module.swift`](https://github.com/evanbacon/serve-sim/blob/main/packages/serve-sim/Sources/SimNative/sim-module.swift) (Streaming helper), [`packages/serve-sim/src/middleware.ts`](https://github.com/evanbacon/serve-sim/blob/main/packages/serve-sim/src/middleware.ts) (simMiddleware), [`packages/serve-sim/src/runtime.ts`](https://github.com/evanbacon/serve-sim/blob/main/packages/serve-sim/src/runtime.ts) (Runtime orchestration), [`packages/serve-sim/src/client/client.tsx`](https://github.com/evanbacon/serve-sim/blob/main/packages/serve-sim/src/client/client.tsx) (React preview client)

## Diagram (Mermaid)

```mermaid
flowchart TD

subgraph group_ts["TypeScript control plane"]
  node_cli["CLI entry point<br/>Node CLI<br/>[index.ts]"]
  node_runtime["Runtime orchestration<br/>Node runtime<br/>[runtime.ts]"]
  node_sessions["Device sessions<br/>coordination<br/>[device-session.ts]"]
  node_device{{"Simulator device<br/>simctl target<br/>[device.ts]"}}
  node_state[("Helper state<br/>temp-file contract<br/>[state.ts]")]
  node_middleware["simMiddleware<br/>Connect middleware<br/>[middleware.ts]"]
  node_ports["Port resolution<br/>routing<br/>[ports.ts]"]
  node_native_api["Native actions<br/>TS native bridge<br/>[native.ts]"]
  node_ax_api["Accessibility API<br/>TypeScript AX layer<br/>[ax.ts]"]
  node_devtools["DevTools targets<br/>routed endpoint"]
end

subgraph group_native["Native streaming"]
  node_capture["Capture engine<br/>Swift capture"]
  node_encoder["Video encoder<br/>Swift encoder<br/>[H264Encoder.swift]"]
  node_sim_helper["Streaming helper<br/>Swift HTTP/WS helper<br/>[sim-module.swift]"]
  node_accessibility["Accessibility bridge<br/>Swift AX bridge"]
end

subgraph group_browser["Browser preview"]
  node_client["React preview client<br/>React app<br/>[client.tsx]"]
  node_sim_view["Simulator view<br/>React component<br/>[SimulatorView.tsx]"]
  node_stream_hook["Stream client<br/>React stream hook<br/>[useSimStream.ts]"]
end

subgraph group_camera["Synthetic camera"]
  node_camera_helper["Camera host helper<br/>Objective-C helper<br/>[main.m]"]
  node_camera_injector["Injected camera dylib<br/>Objective-C dylib"]
  node_frame_source["Shared frame source<br/>Objective-C frame reader"]
end

node_build["Package build<br/>build orchestration<br/>[build.ts]"]
node_skill["Agent skill guidance<br/>Open Agent Skill<br/>[SKILL.md]"]

node_cli -->|"starts"| node_runtime
node_runtime -->|"coordinates"| node_sessions
node_sessions -->|"targets"| node_device
node_runtime -->|"persists helper records"| node_state
node_runtime -->|"starts or connects"| node_sim_helper
node_sim_helper -->|"captures and controls"| node_device
node_capture -->|"pixel buffers"| node_encoder
node_encoder -->|"video stream"| node_sim_helper
node_accessibility -->|"AX data"| node_sim_helper
node_middleware -->|"reads"| node_state
node_middleware -->|"resolves"| node_ports
node_middleware -->|"proxies streams and control"| node_sim_helper
node_middleware -->|"routes"| node_devtools
node_client -->|"renders"| node_sim_view
node_sim_view -->|"uses"| node_stream_hook
node_stream_hook -->|"consumes video"| node_sim_helper
node_client -->|"sends input"| node_native_api
node_native_api -->|"control WebSocket"| node_sim_helper
node_ax_api -->|"reads snapshots"| node_accessibility
node_camera_helper -->|"writes shared frames"| node_frame_source
node_frame_source -->|"supplies BGRA frames"| node_camera_injector
node_camera_injector -.->|"injected into app"| node_device
node_build -.->|"builds native artifact"| node_sim_helper
node_build -.->|"builds bundle"| node_client

click node_cli "https://github.com/evanbacon/serve-sim/blob/main/packages/serve-sim/src/index.ts"
click node_runtime "https://github.com/evanbacon/serve-sim/blob/main/packages/serve-sim/src/runtime.ts"
click node_sessions "https://github.com/evanbacon/serve-sim/blob/main/packages/serve-sim/src/device-session.ts"
click node_device "https://github.com/evanbacon/serve-sim/blob/main/packages/serve-sim/src/device.ts"
click node_state "https://github.com/evanbacon/serve-sim/blob/main/packages/serve-sim/src/state.ts"
click node_middleware "https://github.com/evanbacon/serve-sim/blob/main/packages/serve-sim/src/middleware.ts"
click node_ports "https://github.com/evanbacon/serve-sim/blob/main/packages/serve-sim/src/ports.ts"
click node_native_api "https://github.com/evanbacon/serve-sim/blob/main/packages/serve-sim/src/native.ts"
click node_capture "https://github.com/evanbacon/serve-sim/blob/main/packages/serve-sim/Sources/SimNative/CaptureEngine.swift"
click node_encoder "https://github.com/evanbacon/serve-sim/blob/main/packages/serve-sim/Sources/SimNative/H264Encoder.swift"
click node_sim_helper "https://github.com/evanbacon/serve-sim/blob/main/packages/serve-sim/Sources/SimNative/sim-module.swift"
click node_accessibility "https://github.com/evanbacon/serve-sim/blob/main/packages/serve-sim/Sources/SimNative/AccessibilityBridge.swift"
click node_client "https://github.com/evanbacon/serve-sim/blob/main/packages/serve-sim/src/client/client.tsx"
click node_sim_view "https://github.com/evanbacon/serve-sim/blob/main/packages/serve-sim/src/client/simulator/SimulatorView.tsx"
click node_stream_hook "https://github.com/evanbacon/serve-sim/blob/main/packages/serve-sim/src/client/simulator/useSimStream.ts"
click node_ax_api "https://github.com/evanbacon/serve-sim/blob/main/packages/serve-sim/src/ax.ts"
click node_devtools "https://github.com/evanbacon/serve-sim/blob/main/packages/serve-sim/src/devtools-targets.ts"
click node_camera_helper "https://github.com/evanbacon/serve-sim/blob/main/packages/serve-sim/Sources/SimCameraHelper/main.m"
click node_camera_injector "https://github.com/evanbacon/serve-sim/blob/main/packages/serve-sim/Sources/SimCameraInjector/SimCameraInjector.m"
click node_frame_source "https://github.com/evanbacon/serve-sim/blob/main/packages/serve-sim/Sources/SimCameraInjector/SimCamFrameSource.m"
click node_build "https://github.com/evanbacon/serve-sim/blob/main/packages/serve-sim/build.ts"
click node_skill "https://github.com/evanbacon/serve-sim/blob/main/skills/serve-sim/SKILL.md"

classDef toneNeutral fill:#f8fafc,stroke:#334155,stroke-width:1.5px,color:#0f172a
classDef toneBlue fill:#dbeafe,stroke:#2563eb,stroke-width:1.5px,color:#172554
classDef toneAmber fill:#fef3c7,stroke:#d97706,stroke-width:1.5px,color:#78350f
classDef toneMint fill:#dcfce7,stroke:#16a34a,stroke-width:1.5px,color:#14532d
classDef toneRose fill:#ffe4e6,stroke:#e11d48,stroke-width:1.5px,color:#881337
classDef toneIndigo fill:#e0e7ff,stroke:#4f46e5,stroke-width:1.5px,color:#312e81
classDef toneTeal fill:#ccfbf1,stroke:#0f766e,stroke-width:1.5px,color:#134e4a
class node_cli,node_runtime,node_sessions,node_device,node_state,node_middleware,node_ports,node_native_api,node_ax_api,node_devtools toneBlue
class node_capture,node_encoder,node_sim_helper,node_accessibility toneAmber
class node_client,node_sim_view,node_stream_hook toneMint
class node_camera_helper,node_camera_injector,node_frame_source toneRose
class node_build,node_skill toneNeutral
```

## Components

### TypeScript control plane

- [CLI entry point](https://github.com/evanbacon/serve-sim/blob/main/packages/serve-sim/src/index.ts): Node CLI · Starts helpers and runs direct simulator commands. · `packages/serve-sim/src/index.ts`
- [Runtime orchestration](https://github.com/evanbacon/serve-sim/blob/main/packages/serve-sim/src/runtime.ts): Node runtime · Discovers booted devices, allocates ports, and manages helpers. · `packages/serve-sim/src/runtime.ts`
- [Device sessions](https://github.com/evanbacon/serve-sim/blob/main/packages/serve-sim/src/device-session.ts): coordination · Coordinates multi-device sessions and targets. · `packages/serve-sim/src/device-session.ts`
- [Simulator device](https://github.com/evanbacon/serve-sim/blob/main/packages/serve-sim/src/device.ts): simctl target · `packages/serve-sim/src/device.ts`
- [Helper state](https://github.com/evanbacon/serve-sim/blob/main/packages/serve-sim/src/state.ts): temp-file contract · Persisted under $TMPDIR/serve-sim for detached process coordination. · `packages/serve-sim/src/state.ts`
- [simMiddleware](https://github.com/evanbacon/serve-sim/blob/main/packages/serve-sim/src/middleware.ts): Connect middleware · Embeds preview and proxies helper services behind one origin. · `packages/serve-sim/src/middleware.ts`
- [Port resolution](https://github.com/evanbacon/serve-sim/blob/main/packages/serve-sim/src/ports.ts): routing · `packages/serve-sim/src/ports.ts`
- [Native actions](https://github.com/evanbacon/serve-sim/blob/main/packages/serve-sim/src/native.ts): TS native bridge · Translates browser actions into simulator commands. · `packages/serve-sim/src/native.ts`
- [Accessibility API](https://github.com/evanbacon/serve-sim/blob/main/packages/serve-sim/src/ax.ts): TypeScript AX layer · `packages/serve-sim/src/ax.ts`
- [DevTools targets](https://github.com/evanbacon/serve-sim/blob/main/packages/serve-sim/src/devtools-targets.ts): routed endpoint · Exposes WebKit inspection targets. · `packages/serve-sim/src/devtools-targets.ts`

### Native streaming

- [Capture engine](https://github.com/evanbacon/serve-sim/blob/main/packages/serve-sim/Sources/SimNative/CaptureEngine.swift): Swift capture · Captures simulator frames through xcrun simctl io. · `packages/serve-sim/Sources/SimNative/CaptureEngine.swift`
- [Video encoder](https://github.com/evanbacon/serve-sim/blob/main/packages/serve-sim/Sources/SimNative/H264Encoder.swift): Swift encoder · Produces H.264 or JPEG/MJPEG streams. · `packages/serve-sim/Sources/SimNative/H264Encoder.swift`
- [Streaming helper](https://github.com/evanbacon/serve-sim/blob/main/packages/serve-sim/Sources/SimNative/sim-module.swift): Swift HTTP/WS helper · Exposes stream and control endpoints per simulator. · `packages/serve-sim/Sources/SimNative/sim-module.swift`
- [Accessibility bridge](https://github.com/evanbacon/serve-sim/blob/main/packages/serve-sim/Sources/SimNative/AccessibilityBridge.swift): Swift AX bridge · Extracts simulator accessibility snapshots. · `packages/serve-sim/Sources/SimNative/AccessibilityBridge.swift`

### Browser preview

- [React preview client](https://github.com/evanbacon/serve-sim/blob/main/packages/serve-sim/src/client/client.tsx): React app · Renders chrome, controls, tools, and multi-device layout. · `packages/serve-sim/src/client/client.tsx`
- [Simulator view](https://github.com/evanbacon/serve-sim/blob/main/packages/serve-sim/src/client/simulator/SimulatorView.tsx): React component · `packages/serve-sim/src/client/simulator/SimulatorView.tsx`
- [Stream client](https://github.com/evanbacon/serve-sim/blob/main/packages/serve-sim/src/client/simulator/useSimStream.ts): React stream hook · Consumes MJPEG or AVCC/H.264 video. · `packages/serve-sim/src/client/simulator/useSimStream.ts`

### Synthetic camera

- [Camera host helper](https://github.com/evanbacon/serve-sim/blob/main/packages/serve-sim/Sources/SimCameraHelper/main.m): Objective-C helper · Writes generated, file, or webcam BGRA frames to shared memory. · `packages/serve-sim/Sources/SimCameraHelper/main.m`
- [Injected camera dylib](https://github.com/evanbacon/serve-sim/blob/main/packages/serve-sim/Sources/SimCameraInjector/SimCameraInjector.m): Objective-C dylib · Swizzles AVFoundation in the selected simulator app. · `packages/serve-sim/Sources/SimCameraInjector/SimCameraInjector.m`
- [Shared frame source](https://github.com/evanbacon/serve-sim/blob/main/packages/serve-sim/Sources/SimCameraInjector/SimCamFrameSource.m): Objective-C frame reader · `packages/serve-sim/Sources/SimCameraInjector/SimCamFrameSource.m`

### Other components

- [Package build](https://github.com/evanbacon/serve-sim/blob/main/packages/serve-sim/build.ts): build orchestration · Builds TypeScript, browser bundles, and native artifacts. · `packages/serve-sim/build.ts`
- [Agent skill guidance](https://github.com/evanbacon/serve-sim/blob/main/skills/serve-sim/SKILL.md): Open Agent Skill · Operating guidance for agents. · `skills/serve-sim/SKILL.md`

## Connections

- CLI entry point → Runtime orchestration: starts
- Runtime orchestration → Device sessions: coordinates
- Device sessions → Simulator device: targets
- Runtime orchestration → Helper state: persists helper records
- Runtime orchestration → Streaming helper: starts or connects
- Streaming helper → Simulator device: captures and controls
- Capture engine → Video encoder: pixel buffers
- Video encoder → Streaming helper: video stream
- Accessibility bridge → Streaming helper: AX data
- simMiddleware → Helper state: reads
- simMiddleware → Port resolution: resolves
- simMiddleware → Streaming helper: proxies streams and control
- simMiddleware → DevTools targets: routes
- React preview client → Simulator view: renders
- Simulator view → Stream client: uses
- Stream client → Streaming helper: consumes video
- React preview client → Native actions: sends input
- Native actions → Streaming helper: control WebSocket
- Accessibility API → Accessibility bridge: reads snapshots
- Camera host helper → Shared frame source: writes shared frames
- Shared frame source → Injected camera dylib: supplies BGRA frames
- Injected camera dylib → Simulator device: injected into app
- Package build → Streaming helper: builds native artifact
- Package build → React preview client: builds bundle
