schema_version: 1
case_id: sql-tail-latency-spike-during-incident-window
ground_truth:
  root_cause: A TiDB internal range-splitting path retained abnormal heap under a heavy lookup workload, driving GC pressure and tail-latency spikes; the workload was a trigger rather than the full root cause
judge:
  semantic_strategy: undecided
  contract:
    checks:
    - id: time_window_match
      required: true
      description: Actual diagnosis time window matches one allowed case window.
      match: all_of
      rules:
      - source: trace_fact
        matcher: bool_true
        path: time_window_match
    - id: cluster_scope_match
      required: true
      description: Actual diagnosis cluster scope matches expected cluster id.
      match: all_of
      rules:
      - source: trace_fact
        matcher: bool_true
        path: cluster_scope_match
  workflow:
    checks:
    - id: architecture_context_loaded
      weight: 5
      required: true
      description: Architecture context is loaded.
      match: all_of
      rules:
      - source: workflow_fact
        matcher: bool_true
        path: architecture_context_loaded
    - id: misdiagnosis_guardrails_loaded
      weight: 5
      required: true
      description: Misdiagnosis guardrails are loaded.
      match: all_of
      rules:
      - source: workflow_fact
        matcher: bool_true
        path: misdiagnosis_guardrails_loaded
    - id: diagnosis_run_initialized
      weight: 5
      required: true
      description: Diagnosis run is initialized.
      match: all_of
      rules:
      - source: workflow_fact
        matcher: bool_true
        path: diagnosis_run_initialized
    - id: evidence_collection_completed
      weight: 10
      required: true
      description: Evidence collection completes.
      match: all_of
      rules:
      - source: workflow_fact
        matcher: bool_true
        path: evidence_collection_completed
    - id: summary_read_completed
      weight: 5
      required: false
      description: Summary artifact is read when available.
      match: all_of
      rules:
      - source: workflow_fact
        matcher: bool_true
        path: summary_read_completed
    - id: final_answer_emitted
      weight: 0
      required: false
      description: Final answer is emitted.
      match: all_of
      rules:
      - source: workflow_fact
        matcher: bool_true
        path: final_answer_emitted
  execution_contract:
    checks:
    - id: required_workflow_present
      required: true
      description: Required workflow milestones are present.
      match: all_of
      rules:
      - source: workflow_check
        matcher: subset_observed
        paths:
        - architecture_context_loaded
        - misdiagnosis_guardrails_loaded
        - diagnosis_run_initialized
        - evidence_collection_completed
    - id: required_artifacts_present
      required: true
      description: Required evidence artifacts are present.
      match: all_of
      rules:
      - source: artifact_fact
        matcher: subset_observed
        paths:
        - core_evidence_bundle_present
    - id: preferred_channels_minimum_observed
      required: true
      description: At least one preferred evidence channel is observed.
      match: all_of
      rules:
      - source: channel_fact
        matcher: count_gte
        paths:
        - query_or_metric
        threshold: 1
    - id: preferred_channels_all_observed
      required: false
      description: Preferred evidence channels are all observed.
      match: all_of
      rules:
      - source: channel_fact
        matcher: subset_observed
        paths:
        - query_or_metric
  evidence_collection:
    weight: 5
    checks:
    - id: required_artifacts_present
      weight: 4
      required: true
      description: Required evidence artifacts are present and readable.
      match: all_of
      rules:
      - source: artifact_fact
        matcher: subset_observed
        paths:
        - core_evidence_bundle_present
    - id: preferred_channels_minimum_observed
      weight: 0
      required: true
      description: At least the minimum number of preferred evidence channels are observed.
      match: all_of
      rules:
      - source: channel_fact
        matcher: count_gte
        paths:
        - query_or_metric
        threshold: 1
    - id: preferred_channels_all_observed
      weight: 6
      required: false
      description: Preferred evidence channels are all observed.
      match: all_of
      rules:
      - source: channel_fact
        matcher: subset_observed
        paths:
        - query_or_metric
  critical_evidence:
    weight: 30
    checks:
    - id: gc_signal_present
      weight: 8
      description: Evidence shows abnormal TiDB GC pressure during the incident window.
      match: any_of
      rules:
      - source: artifact_json
        artifacts:
        - metrics
        matcher: json_text_contains_any
        keywords:
        - go_gc
        - gc pause
        - GC pause
        - gc pressure
      - source: artifact_json
        artifacts:
        - summary_json
        matcher: json_text_contains_any
        keywords:
        - GC
        - heap
        - pause
    - id: heap_profile_signal_present
      weight: 9
      description: Evidence includes heap-profile outliers and a dominant memory-retention stack.
      match: any_of
      rules:
      - source: artifact_json
        artifacts:
        - summary_json
        matcher: json_text_contains_any
        keywords:
        - heap
        - inuse_space
        - memory retention
        - dominant allocation
      - source: artifact_json
        artifacts:
        - queries
        matcher: json_text_contains_any
        keywords:
        - IndexLookUp
        - process_keys
        - large scan
  evidence_routing:
    weight: 5
    checks:
    - id: analysis_patch_valid_json
      weight: 1
      required: true
      description: Analysis patch is valid JSON whenever the patch 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
    - id: analysis_items_shape_valid
      weight: 2
      required: true
      description: Analysis patch items are structurally valid whenever the patch exists.
      match: any_of
      rules:
      - source: routing_fact
        matcher: bool_false
        path: analysis_patch_present
      - source: routing_fact
        matcher: bool_true
        path: analysis_items_shape_valid
    - id: analysis_key_evidence_present
      weight: 2
      required: true
      description: Analysis patch includes key evidence whenever the patch exists.
      match: any_of
      rules:
      - source: routing_fact
        matcher: bool_false
        path: analysis_patch_present
      - source: routing_fact
        matcher: bool_true
        path: analysis_has_key_evidence
    - id: metric_refs_resolvable
      weight: 1
      required: true
      description: Metric references resolve whenever the patch requires them.
      match: any_of
      rules:
      - source: routing_fact
        matcher: bool_false
        path: analysis_metric_refs_required
      - source: routing_fact
        matcher: bool_true
        path: analysis_metric_refs_resolvable
    - id: query_refs_resolvable
      weight: 1
      required: true
      description: Query references resolve whenever the patch requires them.
      match: any_of
      rules:
      - source: routing_fact
        matcher: bool_false
        path: analysis_query_refs_required
      - source: routing_fact
        matcher: bool_true
        path: analysis_query_refs_resolvable
    - id: supporting_evidence_present
      weight: 0
      required: false
      description: Analysis patch includes supporting evidence whenever the patch exists.
      match: any_of
      rules:
      - source: routing_fact
        matcher: bool_false
        path: analysis_patch_present
      - source: routing_fact
        matcher: bool_true
        path: analysis_has_supporting_evidence
    - id: analysis_log_or_event_channel_resolvable
      weight: 0
      required: false
      description: Routed log or event items stay resolvable against observed channels.
      match: all_of
      rules:
      - source: routing_fact
        matcher: bool_true
        path: analysis_log_or_event_channel_resolvable
  diagnosis:
    checks:
    - id: identify_gc_pressure_as_primary_driver
      mode: blended
      weight: 3
      required: true
      description: Identifies GC pressure and frequent allocation activity as the primary driver of tail latency.
      deterministic:
        match: all_of
        rules:
        - source: output_text
          matcher: keyword_any
          keywords:
          - GC pressure
          - gc pause
          - frequent gc
          - frequent malloc
          - allocation pressure
          - GC 压力
          - 频繁 GC
          - 频繁 malloc
        - source: output_text
          matcher: keyword_any
          keywords:
          - tail latency
          - P999
          - latency spike
          - 尾延迟
      semantic:
        id: 1
        title: 主路径判断
        description: |
          判断回答是否把主路径识别为 GC pressure、frequent allocation / frequent GC
          对尾延迟的直接放大作用，而不是 generic latency symptom。
    - id: identify_heap_profile_dominant_stack
      mode: blended
      weight: 3
      required: true
      description: Uses heap-profile stack evidence to localize the dominant problematic path.
      deterministic:
        match: any_of
        rules:
        - source: output_text
          matcher: keyword_any
          keywords:
          - heap profile
          - dominant stack
          - stack trace
          - range splitting path
          - range split
          - split range
          - heap stack
          - pprof
      semantic:
        id: 2
        title: heap profile 栈定位
        description: |
          判断回答是否基于 heap profile / dominant stack 证据，将问题定位到
          range split 相关调用栈或同等粒度的具体内部路径，而不是只停留在 workload trigger。
    - id: mention_github_issue_id
      mode: deterministic_only
      weight: 3
      required: false
      description: Mentions the concrete GitHub issue id that matches the diagnosed path.
      deterministic:
        match: any_of
        rules:
        - source: output_text
          matcher: regex_any
          patterns:
          - "(github|issue)\\s*#?\\d+"
          - "issue\\s+id\\s*[:#]?\\s*\\d+"
          - "github issue\\s*[:#]?\\s*\\d+"
    - id: recommend_fixed_version_upgrade
      mode: blended
      weight: 4
      required: false
      description: Recommends upgrading to a fixed version or build.
      deterministic:
        match: any_of
        rules:
        - source: output_text
          matcher: keyword_any
          keywords:
          - fixed release
          - upgrade
          - patched build
          - fixed version
          - 升级
          - 修复版本
      semantic:
        id: 3
        title: 升级修复建议
        description: |
          判断回答是否建议升级到 fixed version、
          patched build 或已修复 release。
