Skip to content
Source
Sign inSign up
pingdotgg/t3code

fix(server): agent scheduled tasks reuse one thread instead of one per run #16726

Closedsaphid(GitHub profile, opens in a new tab)wants to merge 1 commit intomain fromrefs/pull/16726/headopened View on GitHub

Problem

Fixes #16725. When an agent calls schedule_task with bindToCurrentThread: false, every run launches a new top-level thread and a new git worktree and branch from origin/main. Nothing reuses or removes them. One 45-minute watchdog produced 28 threads and 36 worktrees and branches in about a day.

Change

bindToCurrentThread: false now creates one thread for the task when it is scheduled, and binds the task to it. Every run queues into that thread, like any bound task. update_scheduled_task does the same when it unbinds a task.

  • The thread uses the caller's branch and worktree when the caller is in the task's project. Otherwise it uses the project root. No per-run worktree is created.
  • Task thread ids use their own namespace (thread:mcp-scheduled-task:…), so they cannot collide with create_threads ids.
  • A retried schedule_task replays the original thread creation. Before binding, it checks that the thread still exists, is not archived, and does not run above the caller's limits.
  • The tool, instruction, and contract text now describe false as "one thread for the task".

Tasks created in the web or mobile settings keep their current behavior, a fresh thread per run. Existing unbound MCP tasks also keep it until they are rebound.

Known, bounded trade-offs, accepted during review to avoid extra machinery:

  • If saving the task fails after its thread is created, one empty idle thread remains. It has no worktree and no run.
  • Calling update_scheduled_task with bindToCurrentThread: false on a task that already has its own thread creates another thread.

Scope and approval

This is the focused fix for bug #16725. It changes what bindToCurrentThread: false means for agent-created tasks only, from "a thread per run" to "one thread for the task". If maintainers prefer to keep a thread per run, the per-run worktree leak is still a bug, and I can narrow this to that.

Verification

  • vp test run src/mcp/OrchestratorMcpToolkit.integration.test.ts src/provider/T3OrchestrationInstructions.test.ts src/mcp/OrchestratorMcpService.test.ts (in apps/server): 3 files, 28 tests passed. The new assertions cover:
    • an unbound task gets one thread in the caller's workspace and is stored with the root strategy;
    • the thread id cannot collide with a create_threads id;
    • a retry against a deleted thread is refused.
  • Mutation checks: removing the deleted-thread guard, or reverting to an unbound task, makes the test fail.
  • Server tsc --noEmit, vp lint, and vp fmt --check on the changed files passed.
  • Real install: on Nightly 0.0.46-nightly.20261005.2689, binding the leaking watchdog to a single thread stopped the sprawl. Its later runs queued into that thread, and no new threads or branches appeared.
  • Not checked: the scheduler is stubbed in the integration test. Firing a bound task into a never-run thread is existing behavior, covered by ScheduledTaskService tests.

Implemented with Claude Opus 5.5 in Claude Code inside T3 Code. Reviewed independently by GPT-6.1 Sol (high) through Codex in T3 Code: four rounds, final round with no actionable findings.

🤖 Generated with Claude Code

    1. github-actions[bot](GitHub profile, opens in a new tab) added labelsvouch:trusted

    2. Approvability

      Verdict: Not approved

      Macroscope's review found this PR not approvable — This changes scheduled-task execution semantics by replacing isolated per-run threads and worktrees with a persistent task-owned thread and shared workspace. The implementation is focused and tested, but the altered existing behavior and persistent thread/workspace lifecycle warrant human review.

      You can add or adjust custom eligibility rules. [Learn more](https://docs.macroscope.com/approvability#example-custom-eligibility-rules).
    3. juliusmarminge closed this

    4. Works as expected.

    5. Pre-merge checks failed. Please resolve the failing checks before merging.

Sign in to join this conversation.