Sai runtime
Ship the Sai runtime in your desktop app and supervise it, with its command line, events and exit codes.
The Sai runtime (sai-runtime) lets Sai work on your customer's own computer. Your desktop app ships it and starts it in the background. It enrolls the computer into the customer's Tenant once, and from then on Sai, which runs in Simular's cloud, uses the computer through it: the screen, keyboard and mouse, a browser and the shell.
Who does what
Your app talks to the runtime only through the process: its command line, stdin, stdout and exit code. Your app doesn't call the Sai API for the runtime, and never holds your Organisation Key.
What the runtime can reach
- Its credential stays on the computer. The runtime creates its machine credential when it enrolls and keeps it in its state folder. Your backend and your app only ever handle the enrollment code, which is spent once used and expires after 15 minutes.
- A computer can't act for its Tenant. The runtime's sign-in can't start work, answer approvals or read conversations. It reaches only what a run needs: the model, image generation and the Tenant's files. Code running on one computer can't drive another computer of the same Tenant.
- Work comes from your Organisation. Sai sends the runtime only work started for its Tenant with a Tenant token: by your backend, or by an owner or admin in the console's Playground.
- Live view shows and controls the real computer. Anyone holding a
liveUrlfor a Tenant machine can see and control your customer's own computer, with whatever is open on it. See Live view.
Builds
- Google Chrome. Browser tasks need Google Chrome installed.
- Pinned versions.
sai-runtime --versionprints the build's version, such as0.1.3. The runtime never updates itself: your app ships one version. Sai keeps a minimum version, and a runtime below it exits12(see Exit codes), so ship runtime updates in your app's own updates. - Not every version on every platform.
0.1.2is for macOS only: Windows went from0.1.1to0.1.3. - Several displays. From
0.1.3, Sai sees every display of a computer with more than one, and can work on any of them. Before, it saw only the display under the mouse. - Network. The runtime calls the Sai API at
https://api.simular.ai, the same host your backend calls. For live view, the runtime from0.1.2on macOS and from0.1.3on Windows also opens a WebSocket towss://proxy.vm.api.simular.cloud/relay/ws. If the computer is behind a firewall, allow both.
Each build is a zip named for its version and platform, with its SHA-256 beside it. Each build also has the source of its GPL-licensed parts beside it, on macOS from 0.1.2 and on Windows from 0.1.3 (see Open-source licenses):
https://storage.googleapis.com/simular-pro-agent-updates/runner-daemon-enrolled/sai-runtime-<version>-<build>.zip
https://storage.googleapis.com/simular-pro-agent-updates/runner-daemon-enrolled/sai-runtime-<version>-<build>.zip.sha256
https://storage.googleapis.com/simular-pro-agent-updates/runner-daemon-enrolled/sai-runtime-<version>-<build>-source.tar.gzA published version never changes, so pin the version and its SHA-256 in your build. To find the newest version, read latest-runtime-darwin-arm64.yml or latest-runtime-win32-x64.yml in the same folder: it names the version, the zip's path, its sha256 and size. The Shell commands below are for macOS, the PowerShell ones for Windows.
base=https://storage.googleapis.com/simular-pro-agent-updates/runner-daemon-enrolled
curl -fsSLO "$base/sai-runtime-0.1.3-darwin-arm64.zip"
curl -fsSLO "$base/sai-runtime-0.1.3-darwin-arm64.zip.sha256"
shasum -a 256 -c sai-runtime-0.1.3-darwin-arm64.zip.sha256The zip holds one folder, sai-runtime/:
On macOS, unpack it with ditto, which keeps file modes. On Windows, any unzip tool will do:
ditto -x -k sai-runtime-0.1.3-darwin-arm64.zip .
sai-runtime/sai-runtime --versionShip it inside your app
These rules hold whatever your app is built with.
- Keep the folder whole, inside your app. Ship
sai-runtime/with everything in it, unchanged. The executable loadsnative/from beside itself when it starts, and stops if anything there is missing. Don't move, rename, prune, compress or deduplicate anything in it. - Keep it out of archives. Install it as ordinary files and folders on disk. If your app packs its resources into an archive, leave the runtime out: started from inside one, it can't find
native/and never starts. - Don't re-sign it. Ship it with Simular's signature as it is. Many packaging tools sign, strip or compress every executable inside an app by default: exclude this folder from all three. On Windows,
sai-runtime.exeand the.exe,.nodeand.dllfiles innative\carry Simular's Authenticode signature, and a tool that signs every.exeit packs with your certificate replaces it. - On macOS, keep file modes. The executable must stay executable.
- Start it from your app, from your app's own process or a child of it, in the signed-in user's session: never as a launch agent or daemon on macOS, or as a Windows service. macOS holds the app that started a process responsible for that process's permissions; see macOS permissions. On Windows, the runtime's credential belongs to the Windows account that enrolled it; see Windows.
- Update by replacing your whole app. Ship each update as a new app with the runtime version you want inside. Quit your app, which stops the runtime, replace the whole app, and launch it again. Never change files inside an installed app: on macOS that breaks your app's signature, and macOS stops matching your app to its permissions.
Open-source licenses
The runtime contains open-source software, and licenses.txt lists it with its licenses. Its VNC server for live view is under GPL-2.0: on macOS from 0.1.2, sai-vnc is macVNC and LibVNCServer; on Windows from 0.1.3, sai-vnc.exe is LibVNCServer. Your app redistributes them, so:
- Keep
licenses.txtin the folder you ship, as with everything else in it. - Point your users to the source. Simular publishes it beside each zip, in the same folder, as
sai-runtime-<version>-darwin-arm64-source.tar.gzorsai-runtime-<version>-win32-x64-source.tar.gz. Link it for the version and platform you ship, for example from your app's About or legal notices.
macOS permissions
macOS holds your app responsible for the runtime it starts, so the user grants the runtime's permissions to your app, under your app's name. The runtime never asks; it reports what's missing in a permissions_missing event. Your app asks.
Or open the permission's pane in System Settings for the user:
x-apple.systempreferences:com.apple.preference.security?Privacy_Accessibility
x-apple.systempreferences:com.apple.preference.security?Privacy_ScreenCapture- Ask before Sai works. Ask when the user turns Sai on, or as soon as
permissions_missinglists something. - Your backend can check too. From
0.1.3, the runtime reports whether Sai can see the screen, use accessibility and control the computer, as the machine'scapabilities. - Without a permission, the actions that need it fail, and the runtime keeps running. Without Accessibility, the first keyboard or mouse action also makes macOS show its own dialog, naming your app.
- Granting Accessibility restarts the runtime. macOS keeps refusing keyboard and mouse input to a process that started without Accessibility, even after the grant. So the runtime prints
permissions_missingwithoutaccessibility, then exits15, and your app starts it again at once. - Test your app launched the way users launch it: from the Finder, the Dock or
open. If you start your app's executable from a terminal, macOS holds the terminal responsible, so the permissions are the terminal's, not your app's.
Windows
The runtime works the same way on Windows, with these differences.
- No permissions to ask for. Windows makes the user grant the runtime nothing, so it never prints
permissions_missing. - Start it without a console window.
sai-runtime.exeis a console program, so if your app starts it as it is, Windows opens a console window beside your app. Start it hidden, with theCREATE_NO_WINDOWprocess creation flag or your language's way to hide a child process, and with its stdin, stdout and stderr connected to pipes your app holds, as on macOS. Commands the runtime runs for Sai open no window either. - Its credential is protected with DPAPI for the Windows account that enrolled, so no other account or computer can open it, even with a copy of the file. An administrator resetting that account's password, unlike the user changing it, loses it, as it does every DPAPI secret: the next
runexits10, and the computer enrolls again as a new machine. - The Visual C++ runtime is inside.
native\carries the Visual C++ runtime its modules need (vcruntime140.dll, beside the module that loads it, with Microsoft's signature), so the runtime starts on a PC without the Visual C++ Redistributable. Your installer doesn't need to install it. Leave that file where it is. - Programs running as administrator. The runtime can't use the windows of a program running as administrator unless it runs as administrator too.
Live view
Live view lets a person watch the computer's screen and use its mouse and keyboard, for example to sign in to a site for Sai. Your backend opens it with POST /v1/agents/machines/{machineId}/live and a Tenant token, and gets a liveUrl and a websocketUrl, as for any other machine.
It needs the runtime 0.1.2 or later on macOS, and 0.1.3 or later on Windows. Your app sets nothing up for it: the runtime starts its own VNC server from native/ (sai-vnc, on Windows sai-vnc.exe), which listens only on the computer itself with a password the runtime picks at random, and starts it again if it exits. A viewer doesn't need the user's login.
- macOS. Live view uses the Screen Recording and Accessibility the user granted your app (see macOS permissions), so the user doesn't turn on Screen Sharing. From
0.1.3, it keeps working through a screen lock. With0.1.2, it could stay down for up to 5 minutes after the user unlocked the screen. - Windows. Live view needs no Remote Desktop and no Windows login. It can't show UAC prompts or the lock screen, which Windows hides from every program: while one is up, a viewer keeps the last picture, and sees the screen again once it's gone.
0.1.3fixes viewers that couldn't connect. With0.1.2, a new viewer could sometimes fail to connect for a few seconds.
Command line
sai-runtime run [--enroll] [--name <label>]
sai-runtime --versionStart it and keep it running
- Start
sai-runtime runwhen your app starts, or when the user turns Sai on, and keep it running for as long as Sai should be able to use the computer. It never outlives your app: see Stopping. If Sai should work while your window is closed, keep your app running in the background. - Give it three pipes: stdin, which you keep open, stdout for events and stderr for its log. Read stdout and stderr all the time: a pipe nobody reads fills up and can stall the runtime.
- Start with plain
run. Add--enrollonly after it exits10. - One runtime runs per OS user; a second one exits
14. If your app can run twice, only the copy that started the runtime supervises it. - Until the first
ready, it is signing in and connecting. Without a network it keeps retrying, backing off up to 5 minutes between tries, and says nothing. - Its state folder is
~/Library/Application Support/Sai Runtime/on macOS and%LOCALAPPDATA%\Sai Runtime\on Windows. It holds the machine credential (on macOS readable by the user only, on Windows protected with DPAPI), crash logs and files Sai uses during runs. Don't read, change or move anything in it. Deleting it makes the computer forget its machine: the nextrunexits10, and enrolling again creates a new machine.
A supervisor, in outline:
start:
spawn <runtime folder>/sai-runtime run (Windows: sai-runtime.exe, no console window)
stdin: a pipe you keep open stdout: a pipe stderr: a pipe
each stdout line → parse as JSON → handle the event; skip unknown types
each stderr line → your log file
on exit → act on the exit code
enroll (after exit 10):
code ← your backend
spawn <runtime folder>/sai-runtime run --enroll --name <label>
with SAI_ENROLLMENT_TOKEN=<code> in this child's environment only
if it crashes before enrolled (an exit code from "Any other"):
after the backoff, start it again the same way, with the same code
stop:
write {"type":"stop"} and a newline to stdin, or close stdin
wait for the exit; kill the process if it hasn't exited after 10 sEvents
The runtime writes events to stdout, one JSON object per line:
{"type":"enrolled","machineId":"tm_5a1f0c9e2b7d4e6a8c3f1b2d"}
{"type":"permissions_missing","missing":["accessibility","screen_recording"]}
{"type":"ready","machineId":"tm_5a1f0c9e2b7d4e6a8c3f1b2d"}
{"type":"busy"}
{"type":"idle"}busyandidlecome once per run, not once per action.- Drops shorter than 30 seconds say nothing. A connection that dies without closing is noticed within about 40 seconds, so
disconnectedcan come about 70 seconds after the network goes. - Ignore event types you don't know, since a later runtime may add some, and lines that aren't JSON.
Exit codes
At start, exits 10 to 14 come within a second or two. Exits 11, 12 and 13 can also come later: 11 within about a minute of the revocation, 12 and 13 when the runtime next signs in, within the hour.
Enrolling
runexits10.- Your app asks your backend for an enrollment code, as the user signed in to your app. Your backend creates the code for that user's Tenant, never for a Tenant the request names.
- Your app starts
sai-runtime run --enroll --name "<label>"withSAI_ENROLLMENT_TOKEN=<code>in its environment. - The runtime enrolls and prints
enrolledwith the newmachineId. Your app sends it to your backend, which checks it withGET /v1/tenants/{tenantId}/machines/{machineId}and records it against the user. The runtime then connects as on any start.
- The code goes in the environment variable, never on the command line. Other processes, and security tools that record command lines, can see a command line. The runtime removes the variable from its own environment once it has read it, so nothing it starts inherits the code.
- Only for that one start. Set the variable on that child process alone, never in your app's own environment. Don't log the code or store it, and reuse it only to resume an enrollment.
- An enrollment cut short resumes. If the runtime crashes or is killed before it prints
enrolled, startrun --enrollagain with the same code. Within the code's 15 minutes, it finishes the same enrollment, as the same machine. With a new code instead, the first enrollment may already have created a machine, which then stays in the Tenant's machine list, offline, until your backend revokes it. - A refused code exits
10again. Get a new one. If several in a row are refused, back off instead of asking for more. - Name the machine.
--nameis how the machine appears in the Tenant's machine list. Without it, the name is the computer's host name, which often includes the person's name. - Enroll at first launch, not in your installer. The code is for the Tenant of the user signed in to your app, and an installer often runs as another account (root on macOS), so the credential would belong to that account.
Stopping
- Write
{"type":"stop"}and a newline to the runtime's stdin, or close its stdin. It ends the action in progress, which Sai sees as failed, tells Sai it is offline, closes its connection and exits0, within about 5 seconds. - If it hasn't exited after 10 seconds, kill it. A killed runtime can't tell Sai it is offline, so Sai shows it offline within 3 minutes.
- Stop it before your app quits, and wait up to those 10 seconds for it to exit.
- If your app crashes or is killed, the runtime's stdin closes and it stops by itself. Keeping stdin open is what keeps it running.
- Stopping doesn't end a shell command that a run started: that command keeps running.
- On macOS, it also stops on SIGTERM and SIGINT. Windows has no such signals: stop it through stdin.
Lifecycle
First launch on a computer
- Your app starts
run. It exits10. - Your app asks your backend for a code. Your backend finds or creates the user's Tenant and creates a code for it.
- Your app starts
run --enrollwith the code. The runtime printsenrolled, and your app sends themachineIdto your backend, which checks and records it. - The runtime prints
permissions_missing, and your app asks the user for them. Once the user grants Accessibility, the runtime exits15, and your app starts it again. - The runtime prints
ready. Your backend can send it work.
Every later launch
Your app starts run, and the runtime prints ready. No code is needed.
The user signs out of your app
To pause Sai on the computer, stop the runtime. The machine stays enrolled and shows offline. To take the computer off the user's Tenant, your backend also revokes the machine.
A different user signs in on the same OS account
There is one machine per OS account, and it belongs to the previous user's Tenant.
- Before the runtime starts again, your backend revokes the previous user's machine.
- Your app stops the runtime if it runs, and starts
run. It exits11and deletes its credential. - Your app enrolls it for the new user, with a code for the new user's Tenant.
A customer's access ends, then comes back
Your backend disables the Tenant. Sai refuses its work at once. A running runtime exits 13 when it next signs in, within the hour; stop it from your app to end it now. Every later start exits 13, and your app tries again now and then. Once your backend enables the Tenant again, the next try connects, with no enrollment.
If your backend deleted the Tenant instead, which disables it and revokes its machines, the runtime exits 11 within about a minute and deletes its credential. If the user starts it again, it exits 10, and your backend can't get a code for the disabled Tenant: see Create an enrollment code.
Sai retires your runtime version
The runtime exits 12. Your app tells the user it needs updating, and doesn't start the runtime again until it is updated.