Third, Emacs tries to determine the major mode by looking at the
text at the start of the buffer, based on the variable
`magic-mode-alist'. By default, this variable is `nil' (an empty
list), so Emacs skips this step; however, you can customize it in your
init file
How long start of the buffer? Which variable control this behavior?

I use your code to get this value (add this after 'string-match'):
(message "%s" (- (point-max) (point-min)))
I get 4000.

I get doc buffer for 'magic-mode-alist', go to source where this var defined
and search for keyword 'limit':
(defvar magic-mode-regexp-match-limit 4000
"Upper limit on `magic-mode-alist' regexp matches.
Also applies to `magic-fallback-mode-alist'.")