Why is my screen locked when executing this script?

When I execute this script on a machine with MacOS 11.7 using screen sharing, it locks the screen, despite that I have set it to not lock for hours. Later on in the script I take a screenshot, which doesn’t work (is not useful) because it just captures the lock screen. The rest of the script works fine even though the screen is locked.

tell application "Cloudflare WARP" to quit
do shell script "open -g -a 'Cloudflare WARP'"
try
	do shell script "/Applications/Cloudflare\\ WARP.app/Contents/Resources/warp-cli registration delete"
end try

do shell script "/Applications/Cloudflare\\ WARP.app/Contents/Resources/warp-cli registration new"
set connectResult to do shell script "/Applications/Cloudflare\\ WARP.app/Contents/Resources/warp-cli connect"


if connectResult contains "Success" then
	repeat
		set status to do shell script "/Applications/Cloudflare\\ WARP.app/Contents/Resources/warp-cli status"
		if status contains "Connected" then
			exit repeat
		end if
		delay 0.5
	end repeat
end if
tell application "Cloudflare WARP" to activate
delay 1
tell application "System Events" to activate