Make package-manager detection always consult npm_config_user_agent before looking for lockfiles or probing installed executables. A recognized invoking package manager must win even when a project has a conflicting lockfile, so an npm-driven invocation inside a pnpm or yarn project is detected as npm rather than inferred from the project.
Use the consistent user-agent order yarn, pnpm, bun, then npm across the package-manager detection used by Next.js, @next/codemod, and create-next-app. create-next-app already behaves this way and should remain unchanged. The Next.js helper's PackageManager type has no bun, so a bun user agent there must fall through to its existing lockfile and fallback detection.
For codemod lockfile discovery, use the corresponding precedence yarn.lock, pnpm-lock.yaml, bun.lock/bun.lockb, then package-lock.json when multiple candidates are present. Preserve existing fallback behavior when the user agent is absent or unrecognized, and return immediately for a recognized user agent so filesystem and subprocess fallback work is avoided.