The best way to control Claude Code, Codex and more from any computer or phone
Mirrored from https://github.com/Camo67/t3code.git
Synced Nov 21, 09:45 UTC
fix(server): a hung branch name no longer holds a new thread in setup for minutes (#37538)
Co-authored-by: Tobias <tobias@MacStudonTobias.localdomain> Co-authored-by: Julius Marminge <julius0216@outlook.com>
Tobias Lüscher committed
Nov 16, 2026 at 09:41 UTC
e00614e126c5455c962395eaa743ff5c56d2b2ba
2 files changed
+123
-9
apps/server/src/orchestration-v2/ThreadLaunchService.test.ts
+93
@@ -2886,6 +2886,43 @@ it.effect("tells the thread when its worktree branch could not be renamed", () =
2886
}),
2887
);
2888
2889
+it.effect("starts the agent on the temporary branch when branch naming hangs", () =>
2890
+ Effect.gen(function* () {
2891
+ const branchNameStarted = yield* Deferred.make<void>();
2892
+ const harness = makeHarness({
2893
+ // A rate-limited provider retries until the CLI's own timeout, minutes later.
2894
+ generateBranchName: () =>
2895
+ Deferred.succeed(branchNameStarted, undefined).pipe(Effect.andThen(Effect.never)),
2896
+ });
2897
+ yield* Effect.gen(function* () {
2898
+ const launches = yield* ThreadLaunch.ThreadLaunchService;
2899
+ const tracker = yield* WorktreeSetupTracker.WorktreeSetupTracker;
2900
+ const threads = yield* ThreadManagement.ThreadManagementService;
2901
+ const launched = yield* launches.launch(
2902
+ launchInput({
2903
+ command: "launch:rename-hangs",
2904
+ thread: "thread:rename-hangs",
2905
+ message: "Start",
2906
+ workspace: { type: "worktree", baseRef: "main" },
2907
+ }),
2908
+ );
2909
+ yield* Deferred.await(branchNameStarted);
2910
+ yield* TestClock.adjust("20 seconds");
2911
+ yield* tracker.stream(launched.threadId).pipe(
2912
+ Stream.filter((snapshot) => snapshot?.phase === "done"),
2913
+ Stream.runHead,
2914
+ );
2915
+ assert.equal(harness.renameBranch.mock.calls.length, 0);
2916
+ const projection = yield* threads.getThreadProjection(launched.threadId);
2917
+ const notice = projection.turnItems.find((item) => item.type === "system_notice");
2918
+ assert.include(
2919
+ notice?.type === "system_notice" ? notice.message : "",
2920
+ `stays ${projection.thread.branch}. Text generation failed in generateBranchName: Timed out after 20 seconds.`,
2921
+ );
2922
+ }).pipe(Effect.provide(harness.layer));
2923
+ }),
2924
+);
2925
+
2926
it.effect("reports submodule downloads as their own setup step", () =>
2927
Effect.gen(function* () {
2928
const progressReady =
@@ -3020,6 +3057,62 @@ it.effect.each([0, 1])("releases an async setup before its completion with exit
3057
}),
3058
);
3059
3060
+it.effect("shows an async setup as done while the agent still waits for its branch name", () =>
3061
+ Effect.gen(function* () {
3062
+ const branchNameStarted = yield* Deferred.make<void>();
3063
+ const allowBranchName = yield* Deferred.make<void>();
3064
+ const harness = makeHarness({
3065
+ runSetup: () =>
3066
+ Effect.succeed({
3067
+ status: "started" as const,
3068
+ async: true,
3069
+ scriptId: "setup",
3070
+ scriptName: "Setup",
3071
+ scriptCommand: "vp install",
3072
+ terminalId: "setup",
3073
+ cwd: "/repo-worktrees/feature",
3074
+ completion: Effect.succeed({ exitCode: 0, durationMs: 1 }),
3075
+ }),
3076
+ generateBranchName: () =>
3077
+ Deferred.succeed(branchNameStarted, undefined).pipe(
3078
+ Effect.andThen(Deferred.await(allowBranchName)),
3079
+ Effect.as({ branch: "generated-branch" }),
3080
+ ),
3081
+ });
3082
+ yield* Effect.gen(function* () {
3083
+ const launches = yield* ThreadLaunch.ThreadLaunchService;
3084
+ const tracker = yield* WorktreeSetupTracker.WorktreeSetupTracker;
3085
+ const launched = yield* launches.launch(
3086
+ launchInput({
3087
+ command: "launch:async-setup-before-name",
3088
+ thread: "thread:async-setup-before-name",
3089
+ message: "Start",
3090
+ workspace: { type: "worktree", baseRef: "main" },
3091
+ }),
3092
+ );
3093
+ yield* Deferred.await(branchNameStarted);
3094
+ const snapshot = yield* tracker.stream(launched.threadId).pipe(
3095
+ Stream.filter(
3096
+ (current) =>
3097
+ current?.stages.some(
3098
+ (stage) =>
3099
+ (stage.id === "setup-script" &&
3100
+ (stage.status === "done" || stage.status === "failed")) ||
3101
+ (stage.id === "agent" && stage.status === "done"),
3102
+ ) === true,
3103
+ ),
3104
+ Stream.runHead,
3105
+ );
3106
+ const stages = Option.getOrUndefined(snapshot)?.stages ?? [];
3107
+ const setupStage = stages.find((stage) => stage.id === "setup-script");
3108
+ assert.equal(setupStage?.status, "done");
3109
+ assert.isNotNull(setupStage?.endedAt ?? null);
3110
+ assert.notEqual(stages.find((stage) => stage.id === "agent")?.status, "done");
3111
+ yield* Deferred.succeed(allowBranchName, undefined);
3112
+ }).pipe(Effect.provide(harness.layer));
3113
+ }),
3114
+);
3115
+
3116
it.effect.each(["origin/master", ":/initial"])(
3117
"refuses a worktree in a non-Git project before checking base %s",
3118
(baseRef) =>
apps/server/src/orchestration-v2/ThreadLaunchService.ts
+30
-9
@@ -19,6 +19,7 @@ import {
19
type RunId,
20
type RuntimeMode,
21
type ScheduledTaskId,
22
+ TextGenerationError,
23
ThreadId,
24
} from "@t3tools/contracts";
25
import * as Cause from "effect/Cause";
@@ -59,6 +60,9 @@ import { randomUuidV4 } from "@t3tools/provider-core/server/randomUuid";
60
import { DispatchModeLimit } from "./DispatchModeLimit.ts";
61
import * as ThreadManagement from "./ThreadManagementService.ts";
62
63
+// The agent waits for its branch name; a rate-limited provider would hold it for minutes.
64
+const BRANCH_NAME_TIMEOUT = "20 seconds";
65
+
66
export type ThreadLaunchWorkspaceStrategy =
67
| { readonly type: "root"; readonly branch?: string | undefined }
68
| {
@@ -569,6 +573,16 @@ const make = Effect.gen(function* () {
573
projectId: input.projectId,
574
})
575
.pipe(
576
+ Effect.timeoutOrElse({
577
+ duration: BRANCH_NAME_TIMEOUT,
578
+ orElse: () =>
579
+ Effect.fail(
580
+ new TextGenerationError({
581
+ operation: "generateBranchName",
582
+ detail: `Timed out after ${BRANCH_NAME_TIMEOUT}.`,
583
+ }),
584
+ ),
585
+ }),
586
Effect.map((result) => ({
587
branch: result.branch,
588
exactName: settings.branchNamingMode === "custom",
@@ -946,10 +960,12 @@ const make = Effect.gen(function* () {
960
if (setup.completion) {
961
const awaitCompletion = Effect.gen(function* () {
962
const completion = yield* setup.completion!;
949
- yield* setupTracker.stage(threadId, "setup-script", {
950
- status: completion.exitCode === 0 ? "done" : "failed",
951
- detail: `exited with ${completion.exitCode ?? "no exit code"}`,
952
- });
963
+ yield* setupTracker.stageStatus(
964
+ threadId,
965
+ "setup-script",
966
+ completion.exitCode === 0 ? "done" : "failed",
967
+ `exited with ${completion.exitCode ?? "no exit code"}`,
968
+ );
969
if (completion.exitCode !== 0 && !setup.async)
970
return yield* mapError(
971
input,
@@ -958,14 +974,19 @@ const make = Effect.gen(function* () {
974
)(`Setup script exited with ${completion.exitCode ?? "no exit code"}.`);
975
});
976
if (setup.async) {
961
- awaitAsyncSetup = awaitCompletion.pipe(
977
+ // Record the result when the script ends, not after the agent starts.
978
+ const asyncSetup = yield* awaitCompletion.pipe(
979
Effect.catchCause((cause) =>
963
- setupTracker.stage(threadId, "setup-script", {
964
- status: "failed",
965
- detail: failureDetail(Cause.squash(cause)),
966
- }),
980
+ setupTracker.stageStatus(
981
+ threadId,
982
+ "setup-script",
983
+ "failed",
984
+ failureDetail(Cause.squash(cause)),
985
+ ),
986
),
987
+ Effect.forkChild,
988
);
989
+ awaitAsyncSetup = Fiber.join(asyncSetup);
990
} else {
991
yield* awaitCompletion;
992
}