# Slow Query Handoff Field Priority

<!-- auto-oncall-optimization-record
{
  "schemaVersion": "auto-oncall-optimization-record-v1",
  "optimizationId": "slow-query-handoff-field-priority-2026-08-12",
  "problems": [
    {
      "problemId": "slow-query-projection-does-not-follow-current-diagnostic-question",
      "summary": "Slow Query used a broad fixed projection and fixed compact-fact order, so a load-bearing row could omit or deprioritize statistics, TiKV address pairs, memory, scan, lock, resource-control, or optimizer-phase fields needed by the current scenario question.",
      "status": "optimized_unverified",
      "issueKeys": ["APID-29296"],
      "sourceRefs": [
        "https://tidb.atlassian.net/browse/APID-29296#problem:op-handoff-drops-slow-query-memory-attribution"
      ],
      "signalTextIncludesAny": [
        "Slow Query row lacks the field needed by diagnosis",
        "memory_max requested in a different ranking",
        "stats or Cop address missing from handoff",
        "fixed Slow Query field priority"
      ],
      "observedThrough": "2026-08-12",
      "effectiveAt": "2026-08-12T09:37:52+08:00",
      "verifiedAt": null,
      "attemptedAction": "Allow the Evidence Agent to select and order at most twelve safe Slow Query handoffFields for the current diagnostic question; add them to Wetech and Manus provider projections and preserve them first in each compact row while keeping mandatory identity and execution-interval facts automatic.",
      "evidenceUrl": "https://github.com/tidbcloud/aisupport/tree/main/auto-oncall-services/docs/opts/2026-08-12-slow-query-handoff-field-priority.md"
    }
  ]
}
-->

- Optimization ID: `slow-query-handoff-field-priority-2026-08-12`
- Owner: auto-oncall-services Evidence Agent and Provider Executor
- Source reference: `APID-29296` diagnosis follow-up
- Implementation status: deployed to staging in image
  `6ec97a78441c49a841d1fcd0271112e26c423789`
- Effective status: `optimized_unverified`

## Bounded problem

Increasing the general handoff size preserved more data but did not ensure that
one Slow Query request acquired the fields needed for its current question.
Fixed projections and fixed compaction order could still separate statement
identity from `memory_max`, omit statistics freshness, or lose the TiKV address
paired with a Cop maximum.

## Candidate change

- Add query-level ordered `handoffFields`, capped at twelve and validated against
  safe Slow Query fields.
- Keep workload identity, digest prefix, instance, finish time, derived interval
  overlap, and `query_time` automatic.
- Teach Evidence to choose only mechanism-discriminating groups for the current
  question: stats, Cop max/address, memory/spill, resource control, scan/MVCC,
  lock/backoff, or optimizer phases.
- Add selected fields to Wetech and Manus projections and prioritize them in
  the deterministic compact handoff without exposing raw SQL or plans.

## Verification evidence

Local validation completed on 2026-08-12:

- `npm test` passed all 738 tests across 61 suites.
- Targeted OP evidence tests verify cross-type compact-row priority; Wetech and
  Manus provider tests verify selected projection fields and invalid-field
  rejection.
- Wetech and Manus provider contracts passed 65 and 26 tests.
- `quick_validate.py` passed for all three changed skill packages.

The same staging rollout and `APID-29296` `mode=read` replay
`c530fc32-0448-474a-b21b-79f1b019c9ac` exercised the field contract:

- hotspot round 1 selected `memory_max`, `disk_max`, scan counters, and both
  Cop max/address pairs for its Slow Query;
- the materialized 294.25-second ANALYZE row preserved safe statement identity,
  TiDB instance, derived start/finish interval, 46,295,458,134-byte
  `memory_max`, and the selected mechanism fields in one compact fact;
- the Scenario Diagnostic Agent admitted that execution as a material
  candidate while correctly keeping it distinct from TiKV RSS causality;
- round 2 changed the Slow Query order to `memory_max DESC` and repeated the
  same load-bearing memory, scan, and Cop field group.

This directly proves that Evidence-selected fields reach the provider
projection and upstream diagnosis. It remains `optimized_unverified` because
the parallel query-latency scenario failed, the final answer stayed
`human_required`, and no Jira comment refresh occurred.

## Unresolved gaps

- Query-latency selected unsupported `get_token_time`; provider validation
  rejected the whole scenario plan with `op_batch_parameter_invalid` instead
  of ignoring only that field or repairing it in the same round.
- The final Router decision added a duration-metric semantics boundary whose
  concrete source still needs review.
- No Jira internal comment was refreshed because the replay used `mode=read`.

## Next validation action

Make unsupported handoff-field failure proportional to that field, preserve the
remaining valid plan, and review the duration-semantics source. Then rerun
`APID-29296`; refresh and read back the stable Jira internal comment only after
the complete case criterion passes.
