Contractual capacity is determined at the time of entering into the contract. For an individual with intermittent lucidity, a court would look at their mental state during the precise time when the contract was made. If they were lucid and comprehended the terms and implications at that moment, the agreement could be valid.
For example: Suppose someone with bipolar disorder signs a lease during a period of clarity between episodes. If they fully understand the lease terms at that time, their contract might be considered valid despite later lapses in judgment.