The meaning is that Harry is recalling a past action or behavior, and, while remembering it NOW, in the present of the narrative, he is judging it as stupid. The use of the Present Perfect here is necessary with the question word how together with the past modal construction beginning with the modal verb could. There is a definite tone of self-criticism in the question.
Could + a present tense verb can show possibility, and could have + a past participle usually shows a past possibility that did not occur. However, in WH-questions with how, the meaning can be quite different, as it is in your example, where the question often means that the action following could have did occur, and the questioner is asking why. Often, the question is rhetorical and judgmental. (I can think of only one exception where a WH-question with how and could have is not judgmental: How could I have done that?, in answer to, for example, a policeman accusing someone of a crime when that questioner has a solid alibi. In such a context, the questioner is asking (either sincerely or disingenously) for proof or a logical explanation of how they could have possibly committed the crime, but not conceding that they had done so; neither, obviously, are they judging themselves for a crime they have not admitted to having committed. There may be other such exceptions...)
In positive sentences using could have, there is not necessarily as strong a tone of criticism (For example, You could have helped sounds judgmental, but He could have been busy does not sound judgmental; the latter sentence is simply speculating about a past possibility), and in negative yes/no questions (Couldn't you have helped?) or WH-questions with how (either with first, second or third person pronouns), there often is a judgmental nuance (for example, How could I have been so stupid?, How could you have done that?, How could she have been so stupid?). However, the exception I provided above could be rephrased as either a negative yes/no question or a WH-question with how that is not judgmental: Couldn't he have been busy? or How could he have committed the crime, since he has a solid alibi?
(In the present tense in a WH-question with how, could + [be], a similar tone of judgmentalism can be shown, but with a present, ongoing meaning: How could I/he/she/they be so stupid? Often, the difference between How could he be so stupid? and How could he have been so stupid? is negligible.)
I hope this helps. Please let us know if you have any other questions.