schema_version: 1
case_id: top-tidb-p99-latency-nodes-in-saas01-during-latest-window
ground_truth:
  root_cause: >-
    Correctly answering this case requires collecting TiDB P99 latency metrics
    for the exact cluster and resolved window, then ranking TiDB pods by their
    average 1-minute P99 latency over the full 5-minute window. After
    identifying the top 3 TiDB pods, the answer must also provide the
    same-window average P99 breakdown for plan building, distributed execution,
    TiDB-client-side TiKV request latency, TiDB-client-side PD TSO request
    latency, and Go GC pause latency on those same nodes. In addition, it must
    provide two cluster-level averages computed over all relevant server nodes:
    TiKV-side gRPC request handle latency and PD-side TSO request handle
    latency. If the primary latency metric is absent, only yields empty series,
    or any required node-level or cluster-level signal is missing, evidence
    must be marked insufficient instead of fabricating a ranking.
judge:
  semantic_strategy: undecided
  contract:
    checks:
    - id: cluster_scope_match
      required: true
      description: Actual diagnosis cluster scope matches the expected cluster id.
      match: all_of
      rules:
      - source: trace_fact
        matcher: bool_true
        path: cluster_scope_match
  workflow:
    checks:
    - id: final_answer_emitted
      weight: 0
      required: true
      description: Final answer is emitted for the ad-hoc query.
      match: all_of
      rules:
      - source: workflow_fact
        matcher: bool_true
        path: final_answer_emitted
    - id: collect_evidence_not_used
      weight: 0
      required: true
      description: This ad-hoc case must not invoke collect_evidence.py.
      match: all_of
      rules:
      - source: workflow_fact
        matcher: bool_false
        path: collect_evidence_invoked
  execution_contract:
    checks:
    - id: required_workflow_present
      required: true
      description: The minimal ad-hoc workflow is present.
      match: all_of
      rules:
      - source: workflow_check
        matcher: subset_observed
        paths:
        - final_answer_emitted
    - id: collect_evidence_not_used
      required: true
      description: This ad-hoc case must not invoke collect_evidence.py.
      match: all_of
      rules:
      - source: workflow_check
        matcher: subset_observed
        paths:
        - collect_evidence_not_used
    - id: required_report_present
      required: true
      description: Workspace diagnosis report is present.
      match: all_of
      rules:
      - source: artifact_fact
        matcher: subset_observed
        paths:
        - diagnosis_report_present
    - id: query_or_metric_channel_observed
      required: true
      description: At least one query or metric evidence channel is observed.
      match: all_of
      rules:
      - source: channel_fact
        matcher: count_gte
        paths:
        - query_or_metric
        threshold: 1
  evidence_collection:
    weight: 4
    checks:
    - id: required_report_present
      weight: 2
      required: true
      description: Workspace diagnosis report is present and readable.
      match: all_of
      rules:
      - source: artifact_fact
        matcher: subset_observed
        paths:
        - diagnosis_report_present
    - id: query_or_metric_channel_observed
      weight: 2
      required: true
      description: Query or metric evidence is observed for this ad-hoc request.
      match: all_of
      rules:
      - source: channel_fact
        matcher: count_gte
        paths:
        - query_or_metric
        threshold: 1
  critical_evidence:
    weight: 80
    checks:
    - id: latency_metric_present
      weight: 4
      required: true
      description: Evidence includes the TiDB P99 latency metric family for this request.
      match: any_of
      rules:
      - source: artifact_json
        artifacts:
        - metrics
        matcher: json_text_contains_any
        keywords:
        - tidb_server_handle_query_duration_seconds_bucket
        - histogram_quantile(0.99
      - source: artifact_json
        artifacts:
        - workspace_report
        matcher: json_text_contains_any
        keywords:
        - tidb_server_handle_query_duration_seconds_bucket
        - histogram_quantile(0.99
    - id: breakdown_metrics_present
      weight: 6
      required: true
      description: Evidence includes the five node-level breakdown metric families and the two cluster-level service-side metric families needed for this request.
      match: all_of
      rules:
      - source: artifact_json
        artifacts:
        - metrics
        - workspace_report
        matcher: json_text_contains_any
        keywords:
        - tidb_session_compile_duration_seconds_bucket
      - source: artifact_json
        artifacts:
        - metrics
        - workspace_report
        matcher: json_text_contains_any
        keywords:
        - tidb_distsql_handle_query_duration_seconds_bucket
      - source: artifact_json
        artifacts:
        - metrics
        - workspace_report
        matcher: json_text_contains_any
        keywords:
        - tidb_tikvclient_request_seconds_bucket
      - source: artifact_json
        artifacts:
        - metrics
        - workspace_report
        matcher: json_text_contains_any
        keywords:
        - type="Get"
      - source: artifact_json
        artifacts:
        - metrics
        - workspace_report
        matcher: json_text_contains_any
        keywords:
        - pd_client_request_handle_requests_duration_seconds_bucket
      - source: artifact_json
        artifacts:
        - metrics
        - workspace_report
        matcher: json_text_contains_any
        keywords:
        - type="tso"
      - source: artifact_json
        artifacts:
        - metrics
        - workspace_report
        matcher: json_text_contains_any
        keywords:
        - go_gc_pauses_seconds_bucket
      - source: artifact_json
        artifacts:
        - metrics
        - workspace_report
        matcher: json_text_contains_any
        keywords:
        - tikv_grpc_msg_duration_seconds_bucket
      - source: artifact_json
        artifacts:
        - metrics
        - workspace_report
        matcher: json_text_contains_any
        keywords:
        - type="kv_get"
      - source: artifact_json
        artifacts:
        - metrics
        - workspace_report
        matcher: json_text_contains_any
        keywords:
        - pd_server_handle_tso_duration_seconds_bucket
    - id: latency_metric_series_nonempty
      weight: 6
      required: true
      description: Metric evidence includes at least one non-empty time-series payload instead of an all-empty metrics bundle.
      match: all_of
      rules:
      - source: artifact_json
        artifacts:
        - metrics
        matcher: json_values_nonempty
        path: metrics.chart_data.series.values
    - id: output_cluster_id_matches_reference
      weight: 4
      required: true
      description: Output JSON cluster_id matches the runtime reference.
      match: all_of
      rules:
      - source: output_json
        matcher: json_path_equals_reference
        path: cluster_id
        reference_output_id: reference_answer
        reference_path: cluster_id
    - id: output_window_start_matches_reference
      weight: 4
      required: true
      description: Output JSON window.start matches the runtime reference.
      match: all_of
      rules:
      - source: output_json
        matcher: json_path_equals_reference
        path: window.start
        reference_output_id: reference_answer
        reference_path: window.start
    - id: output_window_end_matches_reference
      weight: 4
      required: true
      description: Output JSON window.end matches the runtime reference.
      match: all_of
      rules:
      - source: output_json
        matcher: json_path_equals_reference
        path: window.end
        reference_output_id: reference_answer
        reference_path: window.end
    - id: evidence_sufficient_matches_reference
      weight: 4
      required: true
      description: Output JSON evidence_sufficient matches the runtime reference.
      match: all_of
      rules:
      - source: output_json
        matcher: json_path_equals_reference
        path: evidence_sufficient
        reference_output_id: reference_answer
        reference_path: evidence_sufficient
    - id: cluster_avg_tikv_grpc_matches_reference
      weight: 4
      required: true
      description: Output JSON cluster_avg_tikv_grpc_request_handle_p99_s matches the runtime reference.
      match: all_of
      rules:
      - source: output_json
        matcher: json_path_equals_reference
        path: cluster_avg_tikv_grpc_request_handle_p99_s
        reference_output_id: reference_answer
        reference_path: cluster_avg_tikv_grpc_request_handle_p99_s
    - id: cluster_avg_pd_tso_matches_reference
      weight: 4
      required: true
      description: Output JSON cluster_avg_pd_tso_request_handle_p99_s matches the runtime reference.
      match: all_of
      rules:
      - source: output_json
        matcher: json_path_equals_reference
        path: cluster_avg_pd_tso_request_handle_p99_s
        reference_output_id: reference_answer
        reference_path: cluster_avg_pd_tso_request_handle_p99_s
    - id: top_nodes_match_runtime_reference
      weight: 10
      required: true
      description: Output JSON ranked TiDB nodes and their P99 breakdown match the runtime reference ordering and values.
      match: all_of
      rules:
      - source: output_json
        matcher: json_array_matches_reference
        path: top_nodes_by_avg_p99_latency
        reference_output_id: reference_answer
        reference_path: top_nodes_by_avg_p99_latency
        key_fields:
        - node
        numeric_fields:
        - avg_p99_latency_s
        - avg_plan_build_p99_s
        - avg_distributed_execution_p99_s
        - avg_kv_request_p99_s
        - avg_get_tso_wait_p99_s
        - avg_gc_stw_pause_p99_s
        threshold: 0.00001
  evidence_routing:
    weight: 0
    checks:
    - id: analysis_patch_valid_when_present
      weight: 0
      required: true
      description: Analysis patch is only required to be valid when it exists.
      match: any_of
      rules:
      - source: routing_fact
        matcher: bool_false
        path: analysis_patch_present
      - source: routing_fact
        matcher: bool_true
        path: analysis_patch_valid_json
  critical:
  - id: output_cluster_id_matches_reference
    description: Output JSON cluster_id matches the runtime reference.
  - id: output_window_matches_reference
    description: Output JSON window.start and window.end match the runtime reference.
  - id: output_evidence_sufficient_matches_reference
    description: Output JSON evidence_sufficient matches the runtime reference.
  - id: output_cluster_side_metrics_match_reference
    description: Output JSON cluster-level TiKV and PD server-side P99 averages match the runtime reference.
  - id: primary_latency_metric_series_nonempty
    description: Evidence includes a non-empty TiDB P99 latency metric series rather than an all-empty metrics bundle.
  - id: output_top_nodes_match_reference
    description: Output JSON top_nodes_by_avg_p99_latency matches the runtime reference ordering and values, including the five node-level supplemental P99 signals.
