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

background-service.md

160 lines8.0 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## Troubleshooting
94​
95Start with `t3 service status` on the host. It prints the log path and, on Linux,
96checks whether the installed service is running, enabled, and allowed to survive
97logout.
98​
99If it stops when your SSH session closes, check for `linger-disabled`. An
100administrator can enable lingering with:
101​
102```sh
103sudo loginctl enable-linger "$(id -un)"
104```
105​
106Over SSH, allow sudo to prompt:
107​
108```sh
109ssh -t your-server 'sudo loginctl enable-linger "$(id -un)"'
110```
111​
112Then retry service setup as your normal user. Run only the `loginctl` command
113with sudo; running T3 Code as root creates a separate installation and Connect
114identity. Without administrator access, run `t3 serve` in a terminal and keep
115that session open.
116​
117| Status problem | Next step |
118| --------------------------------------- | ------------------------------------------------------------------------------------------------------------------------------ |
119| `linger-unavailable` | Run `loginctl show-user "$(id -un)" --property=Linger` and check that systemd-logind is available. |
120| `user-manager-unavailable` | Run `systemctl --user status` in a login session for the service user; check your distribution's systemd user-session support. |
121| `service-disabled` or `service-stopped` | Read the log and `systemctl --user status t3code.service`, then use the repair command printed by T3 Code. |
122| `restart-pending` | A newer version is installed but the service still runs the previous one. Run `t3 service restart`. |
123​
124On macOS, check **System Settings → General → Login Items** if the service no
125longer starts at login. If agent work cannot access Desktop, Documents, or
126Downloads, it may need Full Disk Access for the `t3` executable listed in
127`ProgramArguments` in
128`~/Library/LaunchAgents/com.t3tools.t3code.service.plist`.
129​
130For failures after signing in to T3 Connect, see
131[connection troubleshooting](./t3-connect-troubleshooting.md).
132​
133## Reclaim database space
134​
135T3 Code removes superseded internal history on its own while it runs; your
136conversations are kept. Databases created before this shrank automatically
137still hold the freed space on disk until a one-time offline repair returns it
138and turns on automatic shrinking. Settings > Maintenance (web and desktop) or
139Settings > Maintenance (mobile) suggests the repair when at least 1 GiB can be
140reclaimed.
141​
142Run `t3 doctor` on the machine that hosts the environment. It reports the
143database size and the space it can reclaim, then asks which repairs to run;
144pass `--fix` to run the recommended ones without a prompt (for scripts), or
145`--dry-run` to only report. An installed background service for the same data
146directory is stopped for the repair and started again afterwards, also when
147the repair fails. Quit a server you started by hand, or the desktop app, first:
148the repair refuses to touch a database that is in use. Use `--base-dir` for a
149different data directory.
150​
151The repair can take several minutes on a large database. It rebuilds the
152database through a temporary copy, so it needs free space of about the
153database's size twice over: once in the system temporary directory (`TMPDIR`
154or `/tmp` on Linux and macOS) and once next to the database for its
155write-ahead log.
156​
157If you upgraded from a version before 0.1.0, its database (`state.sqlite`) is
158still kept next to the current one so you can roll back. `t3 doctor` offers to
159delete it, but never selects that repair for you and `--fix` skips it: once it
160is gone, a version before 0.1.0 starts with no threads or settings.