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

background-service.md

178 lines8.8 KB

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