Skip to content

Commit 1f53ab2

Browse files
fix(server): tell the thread when its worktree branch keeps the temporary name
When branch-name generation or the rename failed, the launch only logged a server warning, so a Semantic or Custom naming setup silently kept t3/<hex>. Record the failure and its reason as a system notice on the thread's run. AI-assisted; verified with the tests and tools listed above.
1 parent 62dc589 commit 1f53ab2

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";
@@ -240,6 +241,7 @@ function makeHarness(options: HarnessOptions = {}) {
240241
layerExternalServices,
241242
layerThreadManagement,
242243
layerReceipts,
244+
layerOrchestrator,
243245
IdAllocator.layer,
244246
),
245247
),
@@ -2404,6 +2406,44 @@ it.effect("starts the agent of a new worktree thread only after its branch is re
24042406
}),
24052407
);
24062408

2409+
it.effect("tells the thread when its worktree branch could not be renamed", () =>
2410+
Effect.gen(function* () {
2411+
const harness = makeHarness({
2412+
generateBranchName: () =>
2413+
Effect.fail(
2414+
new TextGenerationError({
2415+
operation: "generateBranchName",
2416+
detail: "Failed to spawn Codex CLI process",
2417+
}),
2418+
),
2419+
});
2420+
yield* Effect.gen(function* () {
2421+
const launches = yield* ThreadLaunch.ThreadLaunchService;
2422+
const tracker = yield* WorktreeSetupTracker.WorktreeSetupTracker;
2423+
const threads = yield* ThreadManagement.ThreadManagementService;
2424+
const launched = yield* launches.launch(
2425+
launchInput({
2426+
command: "launch:rename-fails",
2427+
thread: "thread:rename-fails",
2428+
message: "Start",
2429+
workspace: { type: "worktree", baseRef: "main" },
2430+
}),
2431+
);
2432+
yield* tracker.stream(launched.threadId).pipe(
2433+
Stream.filter((snapshot) => snapshot?.phase === "done"),
2434+
Stream.runHead,
2435+
);
2436+
const projection = yield* threads.getThreadProjection(launched.threadId);
2437+
const notice = projection.turnItems.find((item) => item.type === "system_notice");
2438+
assert.equal(notice?.type === "system_notice" ? notice.runId : null, projection.runs[0]?.id);
2439+
assert.include(
2440+
notice?.type === "system_notice" ? notice.message : "",
2441+
`stays ${projection.thread.branch}. Text generation failed in generateBranchName: Failed to spawn Codex CLI process`,
2442+
);
2443+
}).pipe(Effect.provide(harness.layer));
2444+
}),
2445+
);
2446+
24072447
it.effect.each([0, 1])("releases an async setup before its completion with exit %s", (exitCode) =>
24082448
Effect.gen(function* () {
24092449
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
const preparationScope = yield* Scope.make("sequential");
252255
const scheduledLaunches = yield* Ref.make<ReadonlySet<CommandId>>(new Set());
@@ -263,6 +266,46 @@ const make = Effect.gen(function* () {
263266
cause,
264267
});
265268

269+
const recordBranchRenameFailure = Effect.fn("ThreadLaunchService.recordBranchRenameFailure")(
270+
function* (threadId: ThreadId, runId: RunId, branch: string, cause: Cause.Cause<unknown>) {
271+
const error = Cause.squash(cause);
272+
const now = yield* DateTime.now;
273+
yield* eventSink.write({
274+
events: [
275+
{
276+
id: yield* ids.allocate.event({ threadId }),
277+
type: "turn-item.updated",
278+
threadId,
279+
runId,
280+
occurredAt: now,
281+
payload: {
282+
id: ids.derive.runSignalTurnItem({ runId, signal: "branch-rename" }),
283+
threadId,
284+
runId,
285+
nodeId: null,
286+
providerThreadId: null,
287+
providerTurnId: null,
288+
nativeItemRef: null,
289+
parentItemId: null,
290+
// EventSink allocates the real position, after the run's existing items.
291+
ordinal: 0,
292+
status: "completed",
293+
title: "Branch not renamed",
294+
startedAt: now,
295+
completedAt: now,
296+
updatedAt: now,
297+
type: "system_notice",
298+
message: `Could not rename the worktree branch, so it stays ${branch}. ${error instanceof Error ? error.message : String(error)}`,
299+
},
300+
},
301+
],
302+
});
303+
},
304+
Effect.catchCause((cause) =>
305+
Effect.logWarning("Could not record the branch rename failure", { cause }),
306+
),
307+
);
308+
266309
const readReceipt = (input: ThreadLaunchInput, commandId: CommandId) =>
267310
receipts
268311
.getByCommandId(commandId)
@@ -633,7 +676,13 @@ const make = Effect.gen(function* () {
633676
threadId,
634677
oldBranch,
635678
cause,
636-
}),
679+
}).pipe(
680+
Effect.andThen(
681+
runId === null || Cause.hasInterruptsOnly(cause)
682+
? Effect.void
683+
: recordBranchRenameFailure(threadId, runId, oldBranch, cause),
684+
),
685+
),
637686
),
638687
Effect.forkIn(preparationScope),
639688
);

‎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)