The requirement to have an absolute guarantee on being able to restore the session state is too strict for pooled connections.
In practice the pooled connections exhibit a cyclical pattern where each reuse of a connection causes certain statements to be executed. This could be solved by detecting said cycles but even a tiny change in each statement (e.g. a timestamp) would prevent simple comparisons.
A crude but far more predictable method is to retain the most recent history while actively discarding any commands that are older than the configured value. This can be implemented by using the value of max_sescmd_history as the limit and by adding a prune_sescmd_history parameter that enables this pruning.
There is one caveat: the history pruning has a possibility of causing inconsistent session states if the history size is shorter than the cycle length of session commands. Given that this is configurable, it can be avoided but careless use of the parameters can still cause unexpected behavior. As the default history length in 2.3 is 50 commands, it is relatively unlikely that this would have any negative side-effects.