Sounds like it needs to be diagnosed before replacing parts. Not that replacing a part is always a bad thing in comparison to labor rates vs chance of fixing it. It takes 3 basic things for an engine to run. Compression, fuel, ignition. About the only way it would lose compression is for a valve to be hanging up when it heats up. This can happen with a build up of carbon on the valve stem. It's probably one of the most unlikely things to happen but....
You may be able to narrow it down to fuel or ignition at the moment it cuts out. If you were to hold a spark tester near the spark plug and watch for the tester to stop flashing that may tell you if you are losing spark. To further isolate the problem on the ignition side you would need to move to testing why the spark is dropping out. Systems that could cut the spark out are low oil pressure or over heating. Those 2 protective circuits are utilized on many small engines. Be sure the oil is up and the cooling air system is not blocked. Mice nests, plastic bags etc.
Sometimes oil pressure shut down switches will leak internally and shut down. Need to trouble shoot the shut down system.
Another thing that could be done at the moment it dies out is to supply a secondary method of delivering fuel. One way might be to start manually closing the choke if so equipped. The other way would be to fake the fuel supply and give it fuel by either very slightly supplying some starting fluid or gasoline to the carb intake or air filter intake. I DO NOT recommend using starting fluid as it's hard on the piston rings as it's very flammable and violent. I prefer to use a small spray bottle with gasoline. ANY method of enriching such as this can be dangerous. I dont know your abilities so I'm suggesting that if you are not confident in doing this type of troubleshooting take in to be repaired.
Any time you work on something like this or any small engine move it outdoors and have a fire extinguisher on hand. Again, you are working with fuel and ignition sources so it is dangerous. Keep the amount of fuel used in testing to the bare minimum. Watch for any spills and dont check spark or ignition/electrical with fuel spills present.
If you supplement the fuel system at the moment it starts to die and it then recovers and continues to run it is telling you that you have lost your fuel supply. You then need to troubleshoot the fuel system. If it still dies out when supplying fuel it may mean that you have lost ignition.
One example of spark testerThis may be close to your model
Need exact model number