t3code icon indicating copy to clipboard operation
t3code copied to clipboard

[Bug]: Background dev servers started during agent turns are aborted/killed when turn settles

Open DanteAndroid opened this issue 1 week ago • 3 comments

Before submitting

  • [x] I searched existing issues and did not find a duplicate.
  • [x] I included enough detail to reproduce or investigate the problem.

Area

apps/server

Steps to reproduce

  1. In a conversation thread (e.g., using Antigravity / Gemini provider), have the agent start a local development server in the background (e.g., npx wrangler pages dev public --port 8788, vite, or npm run dev).
  2. The dev server boots successfully and begins listening on the port (e.g., http://127.0.0.1:8788).
  3. The agent finishes generating its response, ending the turn and settling the thread.

Expected behavior

Background processes intended as development servers (or listening on local ports for preview) should remain running across turns, or automatically be handed over to the project/session servers lifecycle so preview tabs remain functional.

Actual behavior

When the agent turn settles, T3 Code treats the background subprocess as turn-scoped and aborts/kills it (status transitions to aborted / ended). The TCP socket closes immediately (TCP localhost:...->localhost:8788 (CLOSED)), breaking browser preview as soon as the agent replies.

Impact

Major degradation or frequent failure

Version or commit

T3 Code (Beta) 0.1.11

Environment

macOS Darwin arm64 (Apple Silicon), T3 Code 0.1.11, Antigravity provider

Workaround

  • Run the dev server manually in an external terminal.
  • Force IsDaemon: true on background commands via custom instructions.

DanteAndroid avatar Nov 23 '26 02:11 DanteAndroid

We're seeing what looks like the Linux version of this, with Claude threads on T3 v0.1.12-nightly.20261123 (Ubuntu, systemd 259).

On Linux, T3 starts each agent with systemd-run --user in a transient scope (t3code-claude-<id>.scope under app-t3code-agents-<id>.slice, KillMode=control-group). Every process the agent starts during a turn joins that scope's cgroup. When T3 stops the scope, systemd kills every process still in it. &, nohup and setsid don't help, since they detach a process from the shell and session but not from the cgroup.

A local server the agent started during a Claude turn died within minutes of the turn ending. That happened both when the agent ran it directly and when a Bash PreToolUse hook started it.

From inside a turn, grep t3code- /proc/self/cgroup prints a line when the agent is running in one of these scopes.

Our workaround is to start anything that has to outlive the turn in its own unit, for example systemd-run --user --unit=<name> -- <command>. That process survives, but T3 doesn't track it, and the agent only sees its output by reading the unit's log.

The expected behavior in the issue would cover our case too. We'd like a supported way to start a process that belongs to the thread or project instead of the turn, and that stops when the thread is settled or archived. Until then, we start and stop our own systemd units.

jrdncstr avatar Nov 23 '26 22:11 jrdncstr

Same thing on macOS with Claude threads. T3 Code 0.1.14-nightly.20261127.5332, macOS 27.0.1, Claude Code 2.1.332.

For Claude the server does not die when the turn ends. It dies exactly 2 minutes later, when the idle session is released. The provider log shows query.close at that moment and the dev server's own log shows nothing, because it gets SIGKILL. If I reply within 2 minutes the server stays up, which made this look random for a while.

What kills it

Two changes together:

  • #34505 added a descendant tracker for the macOS path (no agent scope unit). While the CLI works, the server reads the process table once a second and records every descendant of the CLI by pid and start time. A recorded process stays recorded after it is reparented to launchd. When the CLI exits, everything recorded gets SIGKILL (killClaudeDescendant), and a group leader takes its whole group with it.
  • #41174 lowered the idle release for providers with native resume from 30 minutes to 2.

So nohup, &, setsid, a separate process group and a double fork all fail the same way. If the process was a descendant of the CLI during a single poll, it is on the list. Before #34505 a nohup … & server survived on macOS.

Steps to reproduce

  1. In a Claude thread on macOS, have the agent run nohup python3 -m http.server 8766 >/dev/null 2>&1 & in an ordinary Bash call.
  2. Let the turn finish and don't send anything.
  3. curl localhost:8766 answers for 2 minutes, then the connection is refused.

Workaround

Start the server as a launchd job, so it is never a descendant of the CLI:

plist="$TMPDIR/dev-server.example.plist"
plutil -create xml1 "$plist"
plutil -insert Label -string dev-server.example "$plist"
plutil -insert ProgramArguments -json '["/bin/sh", "-c", "exec npm run dev"]' "$plist"
plutil -insert WorkingDirectory -string "$PWD" "$plist"
plutil -insert EnvironmentVariables -json '{}' "$plist"
plutil -insert EnvironmentVariables.PATH -string "$PATH" "$plist"
plutil -insert RunAtLoad -bool true "$plist"
launchctl bootstrap "gui/$(id -u)" "$plist"

Three things cost me time here:

  • The file name has to end in .plist, otherwise bootstrap fails with 5: Input/output error.
  • launchd gives the job only the system PATH, so node, bun and friends are missing unless you pass PATH in.
  • After the server is killed the job stays registered. Run launchctl bootout gui/$(id -u)/dev-server.example before the next bootstrap.

A server started from a T3 terminal or a project action also survives, since the T3 server starts it and not the agent. That one needs a click from me, though.

What I'd like

I understand why #15241 and #34459 wanted leftovers cleaned up, and I don't want that undone. But "the agent starts the dev server and I open it later" is how I review a branch, and right now the only way to get it is to step outside T3 with launchd. idleTimeoutMs is set only in tests, and the tracker has no switch.

Either of these would cover it:

  • a tool the agent can call to start a server under the thread's lifecycle, the way a project action runs: listed under the thread, stopped on settle, left alone on idle release;
  • or a way to mark a process as meant to outlive the session.

The first seems better to me, because T3 would still know about the server and could stop it.

az33zy avatar Nov 27 '26 13:11 az33zy

Another Linux case of the same mechanism, but for a process that belongs to no thread: a plugin daemon shared by all Claude sessions. T3 Code v0.1.13 on WSL2 (Ubuntu 24.04, systemd 255), Claude Code 2.1.333.

The claude-mem plugin runs one background worker on 127.0.0.1:37777 that every Claude session's hooks talk to. The first hook that finds it down spawns it (detached + setsid). Under T3 that spawn happens inside the current session's scope, so the worker joins that cgroup:

0::/user.slice/.../app-t3code-agents-<id>.slice/t3code-claude-<id>.scope

When that one thread's session is released, systemd stops the scope and the shared worker gets SIGTERM. Every other open thread loses it until a later hook respawns it, inside yet another thread's scope:

Dec 01 14:13:13 systemd[237]: Stopping t3code-claude-d0695cd0.scope - .../claude --output-format stream-json ...
[2026-12-01 14:13:13.600] [INFO ] [SYSTEM] Received SIGTERM, shutting down...   (claude-mem worker log)

The worker log shows 0 of these SIGTERMs per day before 2026-11-27 and 5–11 per day since then.

Two details that may help the design here:

  • The integrated terminal is scoped too. claude started from a T3 terminal runs in t3code-terminal-<id>.scope, so a worker spawned from there dies when that terminal closes.
  • The "process owned by the thread" option proposed above wouldn't cover this case, because the worker is meant to outlive every thread. For daemons like this, a documented pattern is enough, for example: "anything that must outlive the session should be started in its own unit (systemd-run --user --unit=... or a user service)". Plugin authors could then detect the scope (grep t3code- /proc/self/cgroup) and do that.

Workaround that works here: run the worker as a systemd --user service with Restart=always. Hooks find it running and never spawn it inside a scope. Reported on the plugin side as well: thedotmack/claude-mem#6686.

luidpivetta avatar Dec 01 '26 17:12 luidpivetta