Open History to see the last commit
1# Server Update Architecture
2
3> For maintainers. Using T3 Code? See [docs/user](../user/).
4
5Remote server updates use one stable launcher selected by the platform service manager (systemd on
6Linux, launchd on macOS). Foreground CLI processes do not self-update, and a running server never
7edits its service definition or durable service state.
8
9## Ownership
10
11The service files under `<baseDir>/runtime` are:
12
13- `service-launcher.mjs`, the stable process selected by the service manager;
14- `service-state.json`, the launcher's durable selection state;
15- `versions/<version>`, immutable exact-version npm installs.
16
17The launcher is the only runtime writer of `service-state.json`. `t3 service install` and
18`t3 service update` may replace the launcher and state while the unit is stopped. Server children
19only communicate with the launcher over their inherited IPC channel.
20
21The state contains one active version and, at most, one update record:
22
23- `pending A → B` selects B as a retryable trial;
24- `committed A → B` selects B for ordinary restarts;
25- `rolled-back A → B` or `failed A → B` selects A;
26- invalid state fails closed so the service manager cannot guess at a runtime.
27
28Every write uses same-directory replacement plus file and directory fsync.
29
30## Remote Update
31
321. The active server installs `t3@<target>` into a unique staging directory.
332. The target runs `__service-preflight` and verifies that the stable launcher supports its update
34 protocol.
353. The staging directory is renamed to its immutable version path only after preflight succeeds.
364. The active child sends `request-update`. The launcher validates the child and target, writes
37 pending state, generates the update ID, then replies `update-accepted`.
385. After a short response-flush grace period, the launcher stops the active child.
396. With SQLite quiescent, the launcher snapshots the database, WAL, and shared-memory files.
407. The launcher starts the target as a trial and gives it the pending update over IPC.
418. The trial runs migrations, acquires dependencies, binds HTTP, starts every long-running root
42 fiber, and verifies that each root is parked at the activation gate.
439. The trial sends `prepared`. The launcher durably commits B, deletes the snapshot, then replies
44 `committed`.
4510. The child opens the existing activation gate, accepts commands, and publishes lifecycle ready
46 with the terminal update outcome.
47
48Post-commit startup does not call service `start`, `initialize`, `connect`, `load`, or `acquire`
49operations. It only opens prepared gates and publishes prepared lifecycle state.
50
51The launcher serializes child exits, IPC messages, and timers. A trial must report prepared within
52120 seconds. If the trial exits or times out before prepared, the launcher stops it, restores the
53snapshot, records rollback, and starts A. A durable restore marker makes an interrupted restore
54resume before either version can boot. After commit, B is active and the service manager's normal
55restart policy applies.
56
57## Database Rollback
58
59The launcher snapshots `state.sqlite`, `state.sqlite-wal`, and `state.sqlite-shm` after the old
60server stops and before the trial starts. This makes trial migrations and writes reversible without
61requiring down migrations. The snapshot is retained across launcher restarts and is removed only
62after commit or after both restore and the terminal rollback state are durable.
63
64The protocol version is part of the safety boundary. A target that requires database snapshots is
65blocked when the installed launcher is too old. Upgrade the launcher once with:
66
67```sh
68npx t3@<version> service update
69```
70
71The local command stops the unit, selects the new launcher and exact runtime, then restarts the
72service. Later releases, including releases with migrations, can use the remote trial path.
73
74Snapshots briefly require enough free disk for another copy of the SQLite files. Attachments and
75other files under the state directory are outside this rollback boundary.
76
77## Client Correlation
78
79The update acknowledgement includes the launcher-generated update ID. After reconnecting, clients
80wait for a lifecycle ready event carrying that same ID. `committed` completes the operation only
81when the ready server is the target version. `rolled-back` and `failed` end it immediately with the
82recorded reason. Older servers without an ID retain version-only reconnect behavior.
83
84## Capability and Compatibility
85
86The existing additive RPC and lifecycle schemas remain compatible with older clients. New servers
87advertise remote self-update only when they have valid launcher context and a live IPC channel.
88Desktop-managed servers advertise `desktopAppUpdate` when the desktop telemetry control fd is
89attached. The progress RPC asks the desktop app to check and download, then returns a preparation
90token while the backend is still connected. Only after the client receives that result does it send
91`server.commitDesktopUpdate`. A successful commit closes the connection and must reconnect at the
92prepared version. The desktop app and bundled server versions stay equal because
93`scripts/update-release-package-versions.ts` bumps them together. If install fails, the desktop keeps its windows, restarts stopped backends, and
94replays the failure for the same token. This two-phase handoff prevents backend shutdown from
95dropping the only successful RPC result. Desktop servers without the capability direct the user to
96update the desktop app locally. Other process shapes provide a manual command; the old detached
97foreground respawn path no longer exists.
98
99## Source Map
100
101- Launcher and state machine: `apps/server/src/serviceLauncher.ts`
102- IPC and durable state types: `apps/server/src/cloud/serviceProtocol.ts`
103- Child IPC adapter: `apps/server/src/cloud/serviceLauncherClient.ts`
104- Staging and preflight: `apps/server/src/cloud/pinnedRuntime.ts` and `servicePreflight.ts`
105- Service installation: `apps/server/src/cloud/bootService.ts`
106- Activation boundary: `apps/server/src/serverRuntimeStartup.ts` and `serverActivation.ts`
107- Client outcome correlation: `packages/client-runtime/src/state/server.ts`