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 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` against the active database in read-only mode.
333. The staging directory is renamed to its immutable version path only after preflight succeeds.
344. The active child sends `request-update`. The launcher validates the child and target, writes
35 pending state, generates the update ID, then replies `update-accepted`.
365. After a short response-flush grace period, the launcher stops the active child.
376. The launcher starts the target as a trial and gives it the pending update over IPC.
387. The trial acquires dependencies, binds HTTP, starts every long-running root fiber, and verifies
39 that each root is parked at the activation gate. It does not run migrations.
408. The trial sends `prepared`. The launcher durably commits B before replying `committed`.
419. The child opens the existing activation gate, accepts commands, and publishes lifecycle ready
42 with the terminal update outcome.
43
44Post-commit startup does not call service `start`, `initialize`, `connect`, `load`, or `acquire`
45operations. It only opens prepared gates and publishes prepared lifecycle state.
46
47The launcher serializes child exits, IPC messages, and timers. A trial must report prepared within
48120 seconds. If the trial exits or times out before prepared, the launcher records rollback and
49starts A. After commit, B is active and normal systemd restart policy applies.
50
51## Migration Boundary
52
53Remote update preflight requires exact equality between the database's applied `(id, name)` rows
54and the target release's migration manifest. A target with a missing, additional, renamed, or
55unknown migration is blocked. Remote updates never migrate or downgrade a database.
56
57This deliberately means any release containing a migration requires a local service update:
58
59```sh
60npx t3@<version> service update
61```
62
63The local command stops the unit, selects the new launcher and exact runtime, then restarts the
64service. Its normal server startup may run migrations.
65
66## Client Correlation
67
68The update acknowledgement includes the launcher-generated update ID. After reconnecting, clients
69wait for a lifecycle ready event carrying that same ID. `committed` completes the operation only
70when the ready server is the target version. `rolled-back` and `failed` end it immediately with the
71recorded reason. Older servers without an ID retain version-only reconnect behavior.
72
73## Capability and Compatibility
74
75The existing additive RPC and lifecycle schemas remain compatible with older clients. New servers
76advertise remote self-update only when they have valid launcher context and a live IPC channel.
77Desktop-managed servers direct the user to update the desktop app. Other process shapes provide a
78manual command; the old detached foreground respawn path no longer exists.
79
80## Source Map
81
82- Launcher and state machine: `apps/server/src/serviceLauncher.ts`
83- IPC and durable state types: `apps/server/src/cloud/serviceProtocol.ts`
84- Child IPC adapter: `apps/server/src/cloud/serviceLauncherClient.ts`
85- Staging and preflight: `apps/server/src/cloud/pinnedRuntime.ts` and `servicePreflight.ts`
86- Service installation: `apps/server/src/cloud/bootService.ts`
87- Activation boundary: `apps/server/src/serverRuntimeStartup.ts` and `serverActivation.ts`
88- Client outcome correlation: `packages/client-runtime/src/state/server.ts`