The highly compensated definition is determined by the IRS.
I got hit by that the first time I sold stock in my old company (stock options). The company, at the time, didn't match. I was then limited to 4% for the next year.
I made sure to schedule my sales after that to all fall in a single year. My salary alone has never been high enough to be an HCE. It was only the stock that put me over.
Eventually that company started matching, which removes the HCE issue.
Matching only helps to a certain extent. Without looking it up again, I don't remember the exactly language, but despite matching, my wife can only contribute up to 10% of gross because she is an HCE. IIRC, it has to do with the amount of employees participating and rate at which they contribute? Maybe that last part is not right though. Hard to remember.
I have done non-discrim testing for medical benefit plans and cafeteria plans, but have not directly done 401k testing. My best understanding of non-discrimination testing for 401k plans is as follows:
I can't remember if there is a test for amount of employees contributing or not (there is for discrimination testing of some benefits but not others).
Regardless, the most common for the 401k plan to fail are tests that compare the rate of contribution before match and including match). In each case you compare the NHCE average deferral percentage to HCE average deferral percentage. The plan fails the test if:
If NHCE% < 2% then fail if HCE% > NHCE% X 2
If NCHE% 2-8% then fail if HCE% > NHCE% + 2%
If NHCE% > 8% then fail if HCE% > NHCE% X 1.25
The HCE definition is selected by the government. An HCE is someone who is a 5% or greater owner in the company or has a salary of $115,000 (number might have changed recently). There is discrimination testing for some health benefits that defines HCEs as anyone in the top 25% of earners in the company, and still others that test for "key employees" who are determined based on ownership and/or whether they are officers at the company.
There are plan designs that help lower the chance of failing the test that don't involve limiting contributions or refunding HCE contributions. For example, matching a decent amount gives NHCEs incentive to contribute. An employer making a separate non-matching contribution might limit non-mustachian HCE deferrals because they may feel they are "saving enough" with the matching. Of course, matching/contributing to 401ks is expensive too.