I'm having that problem when re-opening KDevelop with some documents opened in editor. When doing so, for some reasons AST gets attached only for some contexts.
So I think it should be fixed there instead. I.e. if document is opened in editor and no ast attached, create one. I think that isUpdateRequired is the culprit here.
Or is it another bug? And sessionData in code completion can be empty in some other situations?

Yeah, I think what you are talking about is something different. Note how the above explictly requests the AST, so it should always add the sessionData.

When you just reopen a KDevelop instance, the project import will not see any changes and nothing happens. Only with this patch, and only after you requested code completion once, will the AST be added. There is certainly room for improvement. Something like ensuring the active document gets an AST associated after restart, but no DUChain is updated.