# Provider Routing, Metrics-First Discovery, And Replay Cutoff Plan

## Current Design

- Dry-run replay selects the first support comment, then may extend the cutoff
  through a five-minute customer supplement window when that support comment is
  an acknowledgement or clarification request.
- Comment filtering correctly removes support, bot, internal, target, and
  after-cutoff bodies, but every verified customer comment inside the supplement
  window is currently treated as visible context.
- Supported OP diagnostics use the repository-owned `op-diagnosis` skill.
  Exact Wetech selects `wetech-op-grafana` and exact Manus selects
  `manus-op-grafana`; the account-specific skills are read-only transports.
- Provider selection can still over-route product semantics and configuration
  constraint questions into Wetech or Investibook diagnostics merely because
  the ticket mentions TiDB components, cluster identifiers, errors, or an
  affected environment.

## Risks

- Customer-entered timestamps, component names, alert labels, and claimed root
  causes may be inaccurate. Treating them as canonical can make the skill query
  the wrong time window or component.
- A fixed diagnostic sequence would overfit current cases and prevent the skill
  from adapting to different metric shapes.
- Removing the supplement window entirely would hide legitimate technical
  context supplied immediately after an L1 clarification request.
- Broad provider suppression could incorrectly remove diagnostics from genuine
  performance, hotspot, latency, or resource incidents.

## Target Contract

### Metrics-first OP discovery

- Customer timestamps, component labels, instance names, and causal claims are
  hypotheses, not canonical facts.
- For ambiguous alert/runtime tickets, `op-diagnosis` first discovers
  the observed anomaly window and affected entities from read-only metrics.
- Discovery is adaptive rather than a fixed global sequence. The skill chooses
  the next query from observed metric labels and peaks.
- Alert wording such as `tikv` does not prove the affected store type; TiKV,
  TiFlash proxy, PD store metadata, and host resource evidence remain candidates
  until metrics identify the affected entity.

### Provider routing

- Wetech, Manus, Investibook, and Clinic are selected only for genuine
  evidence-required runtime/performance diagnostics.
- Product semantics, system-variable meaning/impact, documented capability, and
  unsupported-setting questions use provider `none` unless the current question
  separately asks for a time-bounded runtime/performance RCA.
- This normalization applies to deterministic dry-run classification and the
  live LLM classifier contract.

### Replay cutoff

- The five-minute supplement window remains available for substantive customer
  technical context.
- A verified customer terminal-state comment such as resolved, fixed, no
  further help, or close the ticket terminates the supplement window.
- The terminal-state comment and everything after it are excluded from
  `input/comments.md`; they cannot turn a historical diagnostic replay into a
  closure reply.
- Support/bot/internal filtering and metadata-only replay auditing remain
  unchanged.

## Invariants

- Jira remains the canonical ticket source; no new fallback source is added.
- Dry-run preview never writes Jira.
- CDC policy is unchanged.
- No fixed end-to-end diagnostic order is introduced.
- No concurrency, timeout, query-budget, or `internal_only` policy changes are
  included.
- No production operation or customer-visible reply path is changed.

## Implementation Steps

1. Add a metrics-first untrusted-input discovery contract to `op-diagnosis`.
2. Add provider-free product-semantics normalization in deterministic policy
   and align the LLM classification prompt.
3. Stop the replay supplement window before verified customer terminal-state
   comments.
4. Add focused classifier, skill-contract, and replay workspace tests.

## Verification Checklist

- APID-29182-shaped variable semantics classify as
  `feature_usage_or_capability/direct_answer/none`.
- Genuine Wetech latency/alert cases still select `wetech`.
- TCID-12911-shaped unsupported-setting/DDL advice does not invoke a performance
  provider solely because the ticket contains a cluster identifier.
- A technical supplement inside five minutes remains visible.
- `定位好了` / `可以关闭` / `please close` inside the supplement window is
  excluded together with later comments.
- `npm run typecheck`
- Focused Node tests, then full `npm test`
