Skip to content

Commit c833a60

Browse files
fix(server): tell the thread when its worktree branch keeps the temporary name (#34813)
1 parent 71e1f2a commit c833a60

3 files changed

Lines changed: 91 additions & 1 deletion

File tree

‎apps/server/src/orchestration-v2/ThreadLaunchService.test.ts‎

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ import {
2525
ScheduledTaskId,
2626
type ServerProvider,
2727
ThreadId,
28+
TextGenerationError,
2829
} from "@t3tools/contracts";
2930
import * as Cause from "effect/Cause";
3031
import * as Deferred from "effect/Deferred";
@@ -241,6 +242,7 @@ function makeHarness(options: HarnessOptions = {}) {
241242
layerExternalServices,
242243
layerThreadManagement,
243244
layerReceipts,
245+
layerOrchestrator,
244246
IdAllocator.layer,
245247
),
246248
),
@@ -2420,6 +2422,44 @@ it.effect("starts the agent of a new worktree thread only after its branch is re
24202422
}),
24212423
);
24222424

2425+
it.effect("tells the thread when its worktree branch could not be renamed", () =>
2426+
Effect.gen(function* () {
2427+
const harness = makeHarness({
2428+
generateBranchName: () =>
2429+
Effect.fail(
2430+
new TextGenerationError({
2431+
operation: "generateBranchName",
2432+
detail: "Failed to spawn Codex CLI process",
2433+
}),
2434+
),
2435+
});
2436+
yield* Effect.gen(function* () {
2437+
const launches = yield* ThreadLaunch.ThreadLaunchService;
2438+
const tracker = yield* WorktreeSetupTracker.WorktreeSetupTracker;
2439+
const threads = yield* ThreadManagement.ThreadManagementService;
2440+
const launched = yield* launches.launch(
2441+
launchInput({
2442+
command: "launch:rename-fails",
2443+
thread: "thread:rename-fails",
2444+
message: "Start",
2445+
workspace: { type: "worktree", baseRef: "main" },
2446+
}),
2447+
);
2448+
yield* tracker.stream(launched.threadId).pipe(
2449+
Stream.filter((snapshot) => snapshot?.phase === "done"),
2450+
Stream.runHead,
2451+
);
2452+
const projection = yield* threads.getThreadProjection(launched.threadId);
2453+
const notice = projection.turnItems.find((item) => item.type === "system_notice");
2454+
assert.equal(notice?.type === "system_notice" ? notice.runId : null, projection.runs[0]?.id);
2455+
assert.include(
2456+
notice?.type === "system_notice" ? notice.message : "",
2457+
`stays ${projection.thread.branch}. Text generation failed in generateBranchName: Failed to spawn Codex CLI process`,
2458+
);
2459+
}).pipe(Effect.provide(harness.layer));
2460+
}),
2461+
);
2462+
24232463
it.effect.each([0, 1])("releases an async setup before its completion with exit %s", (exitCode) =>
24242464
Effect.gen(function* () {
24252465
const completion = yield* Deferred.make<{ exitCode: number | null; durationMs: number }>();

‎apps/server/src/orchestration-v2/ThreadLaunchService.ts‎

Lines changed: 50 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ import {
2323
} from "@t3tools/contracts";
2424
import * as Cause from "effect/Cause";
2525
import * as Context from "effect/Context";
26+
import * as DateTime from "effect/DateTime";
2627
import * as Effect from "effect/Effect";
2728
import * as Exit from "effect/Exit";
2829
import * as Fiber from "effect/Fiber";
@@ -47,6 +48,7 @@ import * as ProviderRegistry from "../provider/ProviderRegistry.ts";
4748
import * as ServerSettings from "../serverSettings.ts";
4849
import * as TextGeneration from "../textGeneration/TextGeneration.ts";
4950
import * as CommandReceiptStore from "./CommandReceiptStore.ts";
51+
import * as EventSink from "./EventSink.ts";
5052
import * as IdAllocator from "@t3tools/provider-core/server/IdAllocator";
5153
import type * as Orchestrator from "./Orchestrator.ts";
5254
import { makeProviderFailure } from "@t3tools/provider-core/server/failure";
@@ -247,6 +249,7 @@ const make = Effect.gen(function* () {
247249
const receipts = yield* CommandReceiptStore.CommandReceiptStoreV2;
248250
const ids = yield* IdAllocator.IdAllocatorV2;
249251
const threads = yield* ThreadManagement.ThreadManagementService;
252+
const eventSink = yield* EventSink.EventSinkV2;
250253
const managedFolders = yield* ManagedProjectFolders.ManagedProjectFolders;
251254
// A project-folder launch that names no branch records the folder's checkout, as the composer does.
252255
const launchBranch = (strategy: ThreadLaunchWorkspaceStrategy, workspaceRoot: string) =>
@@ -268,6 +271,46 @@ const make = Effect.gen(function* () {
268271
cause,
269272
});
270273

274+
const recordBranchRenameFailure = Effect.fn("ThreadLaunchService.recordBranchRenameFailure")(
275+
function* (threadId: ThreadId, runId: RunId, branch: string, cause: Cause.Cause<unknown>) {
276+
const error = Cause.squash(cause);
277+
const now = yield* DateTime.now;
278+
yield* eventSink.write({
279+
events: [
280+
{
281+
id: yield* ids.allocate.event({ threadId }),
282+
type: "turn-item.updated",
283+
threadId,
284+
runId,
285+
occurredAt: now,
286+
payload: {
287+
id: ids.derive.runSignalTurnItem({ runId, signal: "branch-rename" }),
288+
threadId,
289+
runId,
290+
nodeId: null,
291+
providerThreadId: null,
292+
providerTurnId: null,
293+
nativeItemRef: null,
294+
parentItemId: null,
295+
// EventSink allocates the real position, after the run's existing items.
296+
ordinal: 0,
297+
status: "completed",
298+
title: "Branch not renamed",
299+
startedAt: now,
300+
completedAt: now,
301+
updatedAt: now,
302+
type: "system_notice",
303+
message: `Could not rename the worktree branch, so it stays ${branch}. ${error instanceof Error ? error.message : String(error)}`,
304+
},
305+
},
306+
],
307+
});
308+
},
309+
Effect.catchCause((cause) =>
310+
Effect.logWarning("Could not record the branch rename failure", { cause }),
311+
),
312+
);
313+
271314
const readReceipt = (input: ThreadLaunchInput, commandId: CommandId) =>
272315
receipts
273316
.getByCommandId(commandId)
@@ -638,7 +681,13 @@ const make = Effect.gen(function* () {
638681
threadId,
639682
oldBranch,
640683
cause,
641-
}),
684+
}).pipe(
685+
Effect.andThen(
686+
runId === null || Cause.hasInterruptsOnly(cause)
687+
? Effect.void
688+
: recordBranchRenameFailure(threadId, runId, oldBranch, cause),
689+
),
690+
),
642691
),
643692
Effect.forkIn(preparationScope),
644693
);

‎apps/server/src/orchestration-v2/runtimeLayer.ts‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -283,6 +283,7 @@ const layerThreadLaunchProvided = ThreadLaunchService.layer.pipe(
283283
layerManagedProjectFoldersProvided,
284284
layerThreadManagementProvided,
285285
layerCommandReceiptStoreProvided,
286+
layerEventSink,
286287
IdAllocator.layer,
287288
),
288289
),

0 commit comments

Comments
 (0)