# ISS-0095: Evidence Panel UI Crashes with JSON SyntaxError on Large Files

## Background

Discovered in workspace ws-1779849323231-f013bf8a9 (prod, `investibook.com`, created by
nizheming@pingcap.com). The user reported via `/issue`:

> workspace evidence ui 疯狂抖动 SyntaxError: Unexpected non-whitespace character after
> JSON at position 43431 (line 1307 column 1)

Source: GitHub issue #602.

## Observed Problem

The Evidence Panel (Studio panel file viewer) displays evidence files collected by
`collect_evidence.py`. When an evidence file (e.g., `evidence_summary_for_agent.txt` or
individual JSON files like `metrics.json`, `topsql_tidb.json`) is large (>43KB), the
frontend JSON parser fails:

```
SyntaxError: Unexpected non-whitespace character after JSON at position 43431 (line 1307 column 1)
```

This causes the UI to "疯狂抖动" (rapidly flicker/thrash), likely because:
1. The component attempts to parse the file as JSON.
2. JSON parsing fails, triggering a re-render or error state.
3. The error state and recovery cycle rapidly alternates — causing visible flickering.

The error at position 43431 / line 1307 suggests a large evidence file that contains
either: (a) multiple concatenated JSON objects, (b) a JSON object followed by extra
content (e.g., a log line or separator), or (c) truncated JSON with trailing garbage.

## Root Cause

Two possible root causes:

**R1 — `collect_evidence.py` writes non-pure-JSON to JSON output files:**
The script may append status lines or warnings to stdout/files that are nominally JSON,
producing `{"key": "val"}\nsome status line` — valid JSON followed by extra data. This
is related to ISS-0058 (Loki manual query non-pure JSON). The same pattern may affect
other evidence JSON files when warnings are written to the same file descriptor.

**R2 — Frontend FileViewer always tries JSON.parse() on large files:**
`frontend/components/StudioPanel/FileViewer.tsx` may unconditionally call `JSON.parse()`
on file content without checking the content-type or file extension, causing crashes on
`.txt` files or malformed JSON files.

## Fix Plan

1. **Short-term (frontend hardening):**
   - In `FileViewer.tsx`, wrap JSON.parse() in a `try/catch` block.
   - If parsing fails, fall back to raw text rendering (do not flicker).
   - Only attempt JSON.parse() for files with `.json` extension or
     `Content-Type: application/json`.

2. **Root cause fix (backend/script):**
   - Audit `collect_evidence.py` for any print/warn/log statements that write to the same
     file descriptor as JSON output. Move all non-JSON output to stderr.
   - For files that should be pure JSON, add a validation step:
     `python3 -c "import json; json.load(open('file.json'))"` before writing final output.
   - For `evidence_summary_for_agent.txt` (a text file), ensure it is never parsed as JSON
     by the frontend.

3. **Large file handling:**
   - If evidence files can exceed 50KB, the FileViewer should paginate or lazily load
     content rather than loading the entire file into the DOM at once. This prevents both
     the JSON error and the performance issue from large DOM nodes.

## Verification

Open a workspace with a large evidence file (>40KB). The Evidence panel should:
- Show the file content without flickering.
- Display a readable error message (not a crash loop) if JSON is malformed.
- Not attempt JSON parsing on `.txt` files.
