LBJ realized that it would be very risky for Oswald to have a trial by jury. If Oswald was a CIA asset, then termination was better than exposing CIA connection to Oswald and Oswald himself perhaps testifying of his prior activity in the USSR as part of the defector program which few of the regular public knew existed in 1963. There could be a chain reaction of death to CIA agents who might be exposed by Oswald naming some names.
Since the prevailing attitude of the counter intelligence CIA director, James Angelton was that National Security takes precedence over the constraints of the US Constitution, its easy to see why they might have made the decision to have Oswald terminated and to deflect that action being blamed on CIA, they forced Jack Ruby, their Mafia intelligence insider to do the job, and to do it on camera as well, so as to make it appear that the Mafia was behind the JFK assassination. Jack wias probably promised a new identity and a fake death certificate, only Jack found out a little later that he REALLY WAS going to be dead.