Skip to content
Source
Sign inSign up
pingdotgg/t3code
Open History to see the last commit

server-updates.md

98 lines5.1 KB

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