@@ -23,6 +23,7 @@ import {
2323} from "@t3tools/contracts" ;
2424import * as Cause from "effect/Cause" ;
2525import * as Context from "effect/Context" ;
26+ import * as DateTime from "effect/DateTime" ;
2627import * as Effect from "effect/Effect" ;
2728import * as Exit from "effect/Exit" ;
2829import * as Fiber from "effect/Fiber" ;
@@ -47,6 +48,7 @@ import * as ProviderRegistry from "../provider/ProviderRegistry.ts";
4748import * as ServerSettings from "../serverSettings.ts" ;
4849import * as TextGeneration from "../textGeneration/TextGeneration.ts" ;
4950import * as CommandReceiptStore from "./CommandReceiptStore.ts" ;
51+ import * as EventSink from "./EventSink.ts" ;
5052import * as IdAllocator from "@t3tools/provider-core/server/IdAllocator" ;
5153import type * as Orchestrator from "./Orchestrator.ts" ;
5254import { 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 ) ;
0 commit comments