Fix telemetry event loss on build failures and server shutdown (#85867)
## Problem
Telemetry events were not being captured in three scenarios:
### 1. MCP Telemetry Lost on Dev Server Shutdown
Two telemetry instances were created during dev server startup. MCP
events were recorded to one instance but shutdown flushed a different
instance.
**Fix:** Reuse the existing telemetry instance from `traceGlobals`.
### 2. Process Exit Timeout Killing Async Telemetry
The CLI force-kills child processes after 100ms
(`NEXT_EXIT_TIMEOUT_MS`). Async telemetry operations were interrupted
before completion.
**Fix:** Use `flushDetached()` to write events to disk and spawn a
detached process for async submission, allowing immediate parent exit.
### 3. Build Errors Calling `process.exit()` Before Telemetry Flush
`process.exit()` calls in error handlers (SWC failures, missing
dependencies, route conflicts) bypassed finally blocks and killed
processes before telemetry completed.
**Fix:** Throw errors instead of calling `process.exit()`, add finally
blocks with `await telemetry.flush()` in critical paths.
## Changes
**Core:**
- `server/dev/next-dev-server.ts`: Reuse telemetry instance
- `server/lib/start-server.ts`: Use `flushDetached()` for shutdown
- `build/swc/index.ts`: Remove `process.exit()` calls
- `build/turbopack-build/impl.ts`: Add telemetry flush in worker finally
block
- `build/index.ts`: Add telemetry flush in finally block, throw instead
of exit
- `lib/verify-partytown-setup.ts`: Throw instead of exit
**Supporting:**
- `telemetry/storage.ts`: Generate unique event files per PID
- `telemetry/detached-flush.ts`: Accept optional eventsFile parameter
- `errors.json`: Update detached-flush error message
## Testing
All telemetry tests now pass:
- MCP telemetry on dev server shutdown
- SWC load failures in worker threads
- Build configuration errors
✅ Non-blocking shutdown
✅ Timeout-independent
✅ No race conditions
✅ Works in worker threads