There is evidence that Oswald was recruited by US intelligence when he was a teen in the Civil Air Patrol, and that he had gone to the Soviet Union as a false defector (the Soviets did the same to the US). He was part of operation to kill JFK, but then he was set up to be the fall guy behind his back. He claimed he was a patsy before he was he was killed.
If he wasn't working for the government than some of his actions and associations before the assassination are just baffling. It's also a mystery why he was able to return to the US without any issues after defecting.
https://www.kennedysandking.com/john-f-kennedy-articles/1290 https://jfkfacts.org/was-oswald-a-cia-operative-or-not/