@@ -67,6 +67,7 @@ const adapter = {
6767
6868interface HarnessOptions {
6969 readonly createWorktree ?: GitWorkflow . GitWorkflowService [ "Service" ] [ "createWorktree" ] ;
70+ readonly renameBranch ?: GitWorkflow . GitWorkflowService [ "Service" ] [ "renameBranch" ] ;
7071 readonly runSetup ?: ProjectSetupScriptRunner . ProjectSetupScriptRunner [ "Service" ] [ "runForThread" ] ;
7172 readonly generateTitle ?: TextGeneration . TextGeneration [ "Service" ] [ "generateThreadTitle" ] ;
7273 readonly generateBranchName ?: TextGeneration . TextGeneration [ "Service" ] [ "generateBranchName" ] ;
@@ -87,11 +88,14 @@ function makeHarness(options: HarnessOptions = {}) {
8788 const outbox = EffectOutbox . layer . pipe ( Layer . provide ( database ) ) ;
8889 const createWorktree = vi . fn (
8990 options . createWorktree ??
90- ( ( ) =>
91+ ( ( input ) =>
9192 Effect . succeed ( {
92- worktree : { path : "/repo-worktrees/feature" , refName : "feature" , headSha : "abc" } ,
93+ worktree : { path : "/repo-worktrees/feature" , refName : input . newRefName , headSha : "abc" } ,
9394 } as never ) ) ,
9495 ) ;
96+ const renameBranch = vi . fn (
97+ options . renameBranch ?? ( ( input ) => Effect . succeed ( { branch : input . newBranch } ) ) ,
98+ ) ;
9599 const runSetup = vi . fn (
96100 options . runSetup ?? ( ( ) => Effect . succeed ( { status : "no-script" as const } ) ) ,
97101 ) ;
@@ -113,6 +117,7 @@ function makeHarness(options: HarnessOptions = {}) {
113117 } ) ,
114118 Layer . mock ( GitWorkflow . GitWorkflowService ) ( {
115119 createWorktree,
120+ renameBranch,
116121 fetchRemote : ( ) => Effect . void ,
117122 removeWorktree : ( ) => Effect . void ,
118123 resolveRemoteTrackingCommit : ( ) =>
@@ -154,6 +159,7 @@ function makeHarness(options: HarnessOptions = {}) {
154159 return {
155160 layer : Layer . mergeAll ( launch , threadManagement , titleRegeneration , outbox , database ) ,
156161 createWorktree,
162+ renameBranch,
157163 generateBranchName,
158164 generateThreadTitle,
159165 runSetup,
@@ -721,6 +727,174 @@ it.effect("falls back when the source control writer is unavailable", () =>
721727 } ) ,
722728) ;
723729
730+ it . effect ( "names the worktree itself when the client provides no branch" , ( ) =>
731+ Effect . gen ( function * ( ) {
732+ const harness = makeHarness ( ) ;
733+ yield * Effect . gen ( function * ( ) {
734+ const launches = yield * ThreadLaunch . ThreadLaunchService ;
735+ const threads = yield * ThreadManagement . ThreadManagementService ;
736+ const launched = yield * launches . launch (
737+ launchInput ( {
738+ command : "command:launch:server-named-branch" ,
739+ thread : "thread:launch:server-named-branch" ,
740+ message : "Build the feature" ,
741+ workspace : { type : "worktree" , baseRef : "main" } ,
742+ } ) ,
743+ ) ;
744+ yield * waitUntil ( ( ) => Effect . sync ( ( ) => harness . createWorktree . mock . calls . length === 1 ) ) ;
745+ assert . match (
746+ harness . createWorktree . mock . calls [ 0 ] ?. [ 0 ] . newRefName ?? "" ,
747+ / ^ t 3 c o d e \/ [ 0 - 9 a - f ] { 8 } $ / u,
748+ ) ;
749+ yield * waitUntil ( ( ) =>
750+ threads
751+ . getThreadProjection ( launched . threadId )
752+ . pipe ( Effect . map ( ( projection ) => projection . thread . branch === "generated-branch" ) ) ,
753+ ) ;
754+ } ) . pipe ( Effect . provide ( harness . layer ) ) ;
755+ } ) ,
756+ ) ;
757+
758+ it . effect ( "renames a temporary t3code/<hash> branch off the provisioning critical path" , ( ) =>
759+ Effect . gen ( function * ( ) {
760+ const branchNameStarted = yield * Deferred . make < void > ( ) ;
761+ const allowBranchName = yield * Deferred . make < void > ( ) ;
762+ const harness = makeHarness ( {
763+ createWorktree : ( input ) =>
764+ Effect . succeed ( {
765+ worktree : { path : "/repo-worktrees/temp" , refName : input . newRefName , headSha : "abc" } ,
766+ } as never ) ,
767+ generateBranchName : ( ) =>
768+ Deferred . succeed ( branchNameStarted , undefined ) . pipe (
769+ Effect . andThen ( Deferred . await ( allowBranchName ) ) ,
770+ Effect . as ( { branch : "generated-branch" } ) ,
771+ ) ,
772+ } ) ;
773+ yield * Effect . gen ( function * ( ) {
774+ const launches = yield * ThreadLaunch . ThreadLaunchService ;
775+ const threads = yield * ThreadManagement . ThreadManagementService ;
776+ const launched = yield * launches . launch (
777+ launchInput ( {
778+ command : "command:launch:temp-branch" ,
779+ thread : "thread:launch:temp-branch" ,
780+ message : "Build the feature" ,
781+ workspace : { type : "worktree" , baseRef : "main" , branch : "t3code/abcd1234" } ,
782+ } ) ,
783+ ) ;
784+ yield * Deferred . await ( branchNameStarted ) ;
785+ assert . equal ( harness . createWorktree . mock . calls [ 0 ] ?. [ 0 ] . newRefName , "t3code/abcd1234" ) ;
786+ yield * waitUntil ( ( ) =>
787+ threads
788+ . getThreadProjection ( launched . threadId )
789+ . pipe ( Effect . map ( ( projection ) => projection . runs [ 0 ] ?. status === "starting" ) ) ,
790+ ) ;
791+ assert . equal (
792+ ( yield * threads . getThreadProjection ( launched . threadId ) ) . thread . branch ,
793+ "t3code/abcd1234" ,
794+ ) ;
795+ yield * Deferred . succeed ( allowBranchName , undefined ) ;
796+ yield * waitUntil ( ( ) =>
797+ threads
798+ . getThreadProjection ( launched . threadId )
799+ . pipe ( Effect . map ( ( projection ) => projection . thread . branch === "generated-branch" ) ) ,
800+ ) ;
801+ assert . deepEqual ( harness . renameBranch . mock . calls [ 0 ] ?. [ 0 ] , {
802+ cwd : "/repo-worktrees/temp" ,
803+ oldBranch : "t3code/abcd1234" ,
804+ newBranch : "generated-branch" ,
805+ } ) ;
806+ } ) . pipe ( Effect . provide ( harness . layer ) ) ;
807+ } ) ,
808+ ) ;
809+
810+ it . effect ( "keeps an explicit branch name instead of generating one" , ( ) =>
811+ Effect . gen ( function * ( ) {
812+ const harness = makeHarness ( ) ;
813+ yield * Effect . gen ( function * ( ) {
814+ const launches = yield * ThreadLaunch . ThreadLaunchService ;
815+ yield * launches . launch (
816+ launchInput ( {
817+ command : "command:launch:explicit-branch" ,
818+ thread : "thread:launch:explicit-branch" ,
819+ message : "Build the feature" ,
820+ workspace : { type : "worktree" , baseRef : "main" , branch : "my-feature" } ,
821+ } ) ,
822+ ) ;
823+ yield * waitUntil ( ( ) => Effect . sync ( ( ) => harness . createWorktree . mock . calls . length === 1 ) ) ;
824+ assert . equal ( harness . generateBranchName . mock . calls . length , 0 ) ;
825+ assert . equal ( harness . createWorktree . mock . calls [ 0 ] ?. [ 0 ] . newRefName , "my-feature" ) ;
826+ } ) . pipe ( Effect . provide ( harness . layer ) ) ;
827+ } ) ,
828+ ) ;
829+
830+ it . effect ( "keeps the temporary branch when branch generation fails" , ( ) =>
831+ Effect . gen ( function * ( ) {
832+ const harness = makeHarness ( {
833+ createWorktree : ( input ) =>
834+ Effect . succeed ( {
835+ worktree : { path : "/repo-worktrees/temp" , refName : input . newRefName , headSha : "abc" } ,
836+ } as never ) ,
837+ generateBranchName : ( ) => Effect . die ( "branch generation is down" ) ,
838+ } ) ;
839+ yield * Effect . gen ( function * ( ) {
840+ const launches = yield * ThreadLaunch . ThreadLaunchService ;
841+ const threads = yield * ThreadManagement . ThreadManagementService ;
842+ const launched = yield * launches . launch (
843+ launchInput ( {
844+ command : "command:launch:branch-fallback" ,
845+ thread : "thread:launch:branch-fallback" ,
846+ message : "Build the feature" ,
847+ workspace : { type : "worktree" , baseRef : "main" , branch : "t3code/abcd1234" } ,
848+ } ) ,
849+ ) ;
850+ yield * waitUntil ( ( ) => Effect . sync ( ( ) => harness . generateBranchName . mock . calls . length === 1 ) ) ;
851+ assert . equal ( harness . createWorktree . mock . calls [ 0 ] ?. [ 0 ] . newRefName , "t3code/abcd1234" ) ;
852+ yield * waitUntil ( ( ) =>
853+ threads
854+ . getThreadProjection ( launched . threadId )
855+ . pipe ( Effect . map ( ( projection ) => projection . runs [ 0 ] ?. status === "starting" ) ) ,
856+ ) ;
857+ assert . equal ( harness . renameBranch . mock . calls . length , 0 ) ;
858+ assert . equal (
859+ ( yield * threads . getThreadProjection ( launched . threadId ) ) . thread . branch ,
860+ "t3code/abcd1234" ,
861+ ) ;
862+ } ) . pipe ( Effect . provide ( harness . layer ) ) ;
863+ } ) ,
864+ ) ;
865+
866+ it . effect ( "renames a temporary branch on an existing worktree to a generated name" , ( ) =>
867+ Effect . gen ( function * ( ) {
868+ const harness = makeHarness ( ) ;
869+ yield * Effect . gen ( function * ( ) {
870+ const launches = yield * ThreadLaunch . ThreadLaunchService ;
871+ const threads = yield * ThreadManagement . ThreadManagementService ;
872+ const launched = yield * launches . launch (
873+ launchInput ( {
874+ command : "command:launch:existing-worktree-rename" ,
875+ thread : "thread:launch:existing-worktree-rename" ,
876+ message : "Build the feature" ,
877+ workspace : {
878+ type : "existing_worktree" ,
879+ worktreePath : "/repo-worktrees/t3code-abcd1234" ,
880+ branch : "t3code/abcd1234" ,
881+ } ,
882+ } ) ,
883+ ) ;
884+ yield * waitUntil ( ( ) =>
885+ threads
886+ . getThreadProjection ( launched . threadId )
887+ . pipe ( Effect . map ( ( projection ) => projection . thread . branch === "generated-branch" ) ) ,
888+ ) ;
889+ assert . deepEqual ( harness . renameBranch . mock . calls [ 0 ] ?. [ 0 ] , {
890+ cwd : "/repo-worktrees/t3code-abcd1234" ,
891+ oldBranch : "t3code/abcd1234" ,
892+ newBranch : "generated-branch" ,
893+ } ) ;
894+ } ) . pipe ( Effect . provide ( harness . layer ) ) ;
895+ } ) ,
896+ ) ;
897+
724898for ( const failurePoint of [ "worktree" , "setup" ] as const ) {
725899 it . effect (
726900 `${ failurePoint } failure keeps the thread and message visible and emits failure items` ,
0 commit comments