Open History to see the last commit
1# Running T3 Code in the background
2
3On Linux and macOS, T3 Code can run as a service for your user so you do not need
4to keep a terminal open.
5
6## Manage the service
7
8Install the `t3` CLI first ([Install T3 Code](./install.md#command-line)), then
9run these commands on the machine that will host T3 Code:
10
11| Task | Command |
12| ------------------------------------- | ---------------------- |
13| Set up access from your other devices | `t3 up` |
14| See how your other devices reach it | `t3 status` |
15| Install and start | `t3 service install` |
16| Inspect status and log location | `t3 service status` |
17| Move to a newer release | `t3 update` |
18| Restart | `t3 service restart` |
19| Stop and remove from startup | `t3 service uninstall` |
20
21Uninstalling the service leaves your projects, threads, and settings intact.
22Running `t3 service install` again repairs a service that `t3 service status`
23reports as broken.
24
25`t3 update` downloads the newest release on your channel and switches `t3`
26and the service to it. Restarting interrupts running agent turns, terminals,
27and remote clients, so it asks first; answer no and the service keeps running
28the old version until you run `t3 service restart`. Pass `--yes` from a
29script. A server you started by hand is left running; stop and start it again
30to pick up the new version. Wait for any remote update already in progress
31before updating; to match a remote client's version, follow
32[Updating T3 Code](./updating.md).
33
34Pass an exact version (`t3 update 0.0.42`) to pin one, `--channel nightly` to
35switch trains, or `--allow-downgrade` to move backwards. `preview` is a
36maintainers' test train: its builds can be broken and are never offered as
37updates, so the installer and `t3 update` ask for confirmation before
38installing one.
39
40`t3 uninstall` removes the background service, the `t3` launcher, and the
41downloaded versions after showing you the list and asking once. Your projects,
42threads, and settings under `~/.t3/userdata` are kept, along with `service.env`
43under T3 home. Pass `--yes` from a script.
44
45## Local network access
46
47A new service answers only on this machine. Other devices reach it through
48T3 Connect, or directly once you choose the local network in `t3 up` (or run
49`t3 up --lan`, which sets `T3CODE_LAN=true` in `service.env`). It then listens on
50your local network and Tailscale addresses, so the desktop and mobile apps on
51the same network connect directly instead of through T3 Connect. It still does
52not answer on public addresses. Anyone on the same network can reach the port,
53so a pairing code is all that keeps them out. The hosted web app at app.t3.codes
54stays on T3 Connect, because browsers block plain HTTP from an HTTPS page.
55
56A service installed before `T3CODE_LAN` existed keeps listening on the local
57network. `t3 status` says so, and `t3 up --no-lan` turns it off. Any
58`T3CODE_HOST` value replaces this setting.
59
60## Configure the service environment
61
62Put custom environment variables in `service.env` under the service's T3 home
63directory, normally `~/.t3/service.env`. This file survives service installation
64and updates. For example:
65
66```dotenv
67T3CODE_PORT=3773
68T3CODE_HOST=127.0.0.1
69```
70
71Use `KEY=VALUE` entries, quoting values that contain spaces or `#`. Values are
72literal: shell commands and variable expansion are not evaluated. Entries
73override the inherited service environment, except for `PATH`, `T3CODE_HOME`,
74`T3_BOOT_SERVICE_UNIT`, and `T3_SERVICE_LAUNCHER_CONTEXT` (including case variants).
75
76Run `t3 service restart` after changing the file. Changes apply when the service
77starts again; switching the server version alone keeps the current values.
78For a service installed before env-file support, run `t3 service install` once
79with an up-to-date CLI.
80A missing file is ignored; other read failures stop service startup.
81If the file contains credentials, restrict access to that file with `chmod 600`
82(for the default T3 home, `chmod 600 ~/.t3/service.env`).
83
84## Platform support
85
86Linux needs systemd user services. Setup enables lingering so T3 Code starts at
87boot and keeps running after logout. If this needs administrator permission,
88setup prints a recovery command before changing the service.
89
90macOS starts the service when you log in and stops it when you log out. Keep the
91Mac logged in and awake for unattended remote access. Installing over SSH while
92nobody is logged in at the Mac's screen can fail at the final start step; the
93service is still installed and will start at the next login.
94
95Windows background services are not supported. To keep a Windows host available
96through T3 Connect, leave the desktop app running with its local environment
97enabled, or keep `t3 serve` running in a terminal. Minimize the desktop app rather
98than closing its last window, which stops its local server. Linking T3 Connect
99does not configure automatic startup at Windows sign-in.
100
101`t3 up` installs the service when you set up T3 Connect, but the two are managed
102separately. Signing out of T3 Connect does not stop or uninstall the service.
103
104## Keep the server responsive when agents use all memory
105
106On Linux, agents already run with lower priority than the server. When they
107fill the machine's memory, though, the kernel may still slow down or stop the
108server, because only an administrator can reserve memory for it. Run, as your
109normal user:
110
111```sh
112t3 service protect
113```
114
115It shows the system settings it will change, asks before applying them, and
116then asks for your sudo password. If the machine runs Tailscale or SSH, it
117also offers to protect them, so you can still reach the machine. Memory
118protection takes effect at once. The server's protection from the kernel's
119out-of-memory killer takes effect after `t3 service restart`. `t3 service status` reports whether
120the protection is in place, and `t3 service protect --undo` removes it.
121
122## Troubleshooting
123
124Start with `t3 service status` on the host. It prints the log path and, on Linux,
125checks whether the installed service is running, enabled, and allowed to survive
126logout.
127
128If it stops when your SSH session closes, check for `linger-disabled`. An
129administrator can enable lingering with:
130
131```sh
132sudo loginctl enable-linger "$(id -un)"
133```
134
135Over SSH, allow sudo to prompt:
136
137```sh
138ssh -t your-server 'sudo loginctl enable-linger "$(id -un)"'
139```
140
141Then retry service setup as your normal user. Run only the `loginctl` command
142with sudo; running T3 Code as root creates a separate installation and Connect
143identity. Without administrator access, run `t3 serve` in a terminal and keep
144that session open.
145
146| Status problem | Next step |
147| --------------------------------------- | ------------------------------------------------------------------------------------------------------------------------------ |
148| `linger-unavailable` | Run `loginctl show-user "$(id -un)" --property=Linger` and check that systemd-logind is available. |
149| `user-manager-unavailable` | Run `systemctl --user status` in a login session for the service user; check your distribution's systemd user-session support. |
150| `service-disabled` or `service-stopped` | Read the log and `systemctl --user status t3code.service`, then use the repair command printed by T3 Code. |
151| `restart-pending` | A newer version is installed but the service still runs the previous one. Run `t3 service restart`. |
152
153On macOS, check **System Settings → General → Login Items** if the service no
154longer starts at login. If agent work cannot access Desktop, Documents, or
155Downloads, it may need Full Disk Access for the `t3` executable listed in
156`ProgramArguments` in
157`~/Library/LaunchAgents/com.t3tools.t3code.service.plist`.
158
159For failures after signing in to T3 Connect, see
160[connection troubleshooting](./t3-connect-troubleshooting.md).
161
162## Reclaim database space
163
164T3 Code removes internal history older than a day on its own while it runs;
165your conversations are kept. Databases created before this shrank automatically
166still hold the freed space on disk until a one-time offline repair returns it
167and turns on automatic shrinking. Settings > Maintenance (web and desktop) or
168Settings > Maintenance (mobile) suggests the repair when at least 1 GiB can be
169reclaimed.
170
171When the desktop app or the background service runs T3 Code on that machine,
172select **Reclaim** in Settings on web or desktop. T3 Code checks for free disk
173space, then stops, repairs the database, and starts again. It is offline while
174the repair runs, and running agents stop. If the repair fails, Settings shows
175why.
176
177In other cases, such as a server you started by hand, run `t3 doctor` on the
178machine that hosts the environment. It reports the database size and the
179space it can reclaim, then asks which repairs to run;
180pass `--fix` to run the recommended ones without a prompt (for scripts), or
181`--dry-run` to only report. An installed background service for the same data
182directory is stopped for the repair and started again afterwards, also when
183the repair fails. Quit a server you started by hand, or the desktop app, first:
184the repair refuses to touch a database that is in use. Use `--base-dir` for a
185different data directory.
186
187The repair can take several minutes on a large database. It rebuilds the
188database through a temporary copy, so it needs free space of about twice the
189data still in use (the database size minus the reclaimable space): once in the
190system temporary directory (on Linux and macOS, `TMPDIR`, or `/var/tmp` when it
191is not set) and once next to the database for its write-ahead log.
192
193If you upgraded from a version before 0.1.0, its database (`state.sqlite`) is
194still kept next to the current one so you can roll back. `t3 doctor` offers to
195delete it, but never selects that repair for you and `--fix` skips it: once it
196is gone, a version before 0.1.0 starts with no threads or settings.