# Jeprof Heap Flamegraph Plan

## Current Design

- `/continuous_profiling/action_token` stores the frontend query string as `downloadTokenParams.Q`.
- `/continuous_profiling/single_profile/view` restores that query string, downloads the profile object, and returns the raw bytes.
- The only special case is `component=tikv` and `profile_type=heap`, where the API runs the embedded `jeprof.in` with `--collapsed` and returns `text/plain`.
- The downloaded rawdata filename may use a `.proto` suffix even when the bytes are ASCII jeprof symbolized heap data beginning with `--- symbol` followed by `--- heap`.

## Risks

- A `.proto` suffix is not a reliable content contract for TiKV or TiFlash heap profiles.
- `go tool pprof` and speedscope cannot directly parse jeprof symbolized heap text.
- Returning raw jeprof text to a flamegraph request makes downstream conversion fail instead of receiving collapsed stacks.
- TiFlash heap is not covered by the current TiKV-only conversion branch.

## Target Contract

- `data_format=text` is the canonical request for collapsed stack text.
- For `component=tikv|tiflash`, `profile_type=heap`, and `data_format=text`, the API converts jeprof symbolized heap text with embedded `jeprof.in --collapsed`.
- The response content type for that contract is `text/plain`.
- The API only runs jeprof when the downloaded bytes look like jeprof heap text; pprof protobuf bytes are returned unchanged.
- Download/raw payload behavior is unchanged; the zip download still returns the stored raw bytes.
- The API does not infer protobuf-vs-jeprof from the filename suffix.

## Implementation Steps

1. Bind `data_format` from the restored profile query.
2. Replace the TiKV-only heap branch with a helper that checks component, profile type, data format, and jeprof heap content markers.
3. Extract jeprof collapsed conversion into a testable function using a temporary profile file.
4. Preserve stderr only for error reporting, not successful response bytes.
5. Add local tests for TiKV/TiFlash text conversion selection and symbolized jeprof heap collapsed output.

## Verification Checklist

- `go test ./pkg/ngm/conprof`
- A TiKV heap flamegraph request with `data_format=text` returns collapsed stack text.
- A TiFlash heap flamegraph request with `data_format=text` returns collapsed stack text.
- Non-heap or non-text requests do not enter jeprof conversion.
