# Evidence Context Compaction Plan

## Summary

Reduce evidence JSON pollution in the agent context without taking on the
Studio metrics loading issue.

In workspace `ws-1776220777461-75f939e68` / `O11Y-2396397`,
the agent-facing summary and raw metrics evidence were about
7 MB because metric entries embedded full `chart_data.series` payloads. The
agent can accidentally read those files and spend context on raw time-series
points instead of compact diagnostic evidence.

## Key Changes

- Keep `evidence/metrics.json` backward-compatible for Studio by
  leaving full `chart_data.series` in that file for now.
- Update `agentkit/skills/builtin/tidb-clinic-diagnosis/scripts/collect_evidence.py`.
- Add helpers such as `compact_metric_for_ai` and `compact_metrics_for_ai`.
- Write compact metrics into `evidence_summary_for_agent.json`.
- Compact metric entries should retain core diagnostic fields:
  `name`, `instance`, `peak`, `mean`, `quota`, `utilization`, `unit`,
  `severity`, `promql`, and `link`.
- Compact metric entries must not include raw `chart_data.series`.
- Do not add `chart_data_ref`; raw chart payloads are stored only in
  `evidence/metrics.json` and are not part of the AI reasoning path.
- Treat metric raw values/labels and full log rows as detail data. Store them
  only in raw sidecars.
- Treat log occurrence counts as diagnostic data. Include counts in compact
  summary/report evidence.
- Treat slow query, decoded plan, and TopSQL evidence as diagnostic data.
- Update `backend/investibook/core/acp.py` so diagnosis report hydration merges
  compact metrics into `diagnosis_report.json` instead of copying full raw
  series from sidecars.
- Update the diagnosis skill guidance so agents read
  `evidence_summary_for_agent.json` or the hydrated report for reasoning, and
  only read `evidence/metrics.json` when they explicitly need raw chart
  data.

## Test Plan

- Unit test `compact_metric_for_ai` with an input metric containing
  `chart_data.series`; verify the output has no raw series and retains the
  diagnostic fields.
- Unit test `save_evidence_files`; verify `evidence/metrics.json`
  still contains full chart data while `evidence_summary_for_agent.json` does not.
- Unit test diagnosis report hydration in `backend/investibook/core/acp.py`;
  verify hydrated `evidence.metrics` contains compact metrics only.
- Regression check an O11Y-2396397-like run; verify compact summary and
  hydrated report are small enough for agent reasoning, with a target below
  500 KB for the summary/report path.
- Verify no frontend contract is intentionally changed by this plan.

## Assumptions

- The Studio loading and rendering problem is out of scope.
- Full chart data remains available in `evidence/metrics.json`.
- If the frontend later needs to render from compact summaries, that should be
  handled by a separate Studio plan; the AI-facing compact files should not
  carry raw chart references.
