schema_version: 1
case_id: inconsistent-query-result-with-subquery
ground_truth:
  expected_diagnosis_skill_routes:
  - any_of:
    - tidb-query-tuning-autox
    - tidb-query-tuning
  root_cause: A TiKV StreamAgg correctness defect lost an unfinished final group when a result exceeded the 32 MiB response boundary and continued on another page. In the NOT IN DISTINCT plan this made one value disappear from the subquery result and allowed an extra outer row. Removing DISTINCT or forcing HASH_AGG avoids the faulty path.
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: primary_symptom_evidence_present
      weight: 10
      description: Primary symptom evidence is present in the collected evidence.
      match: any_of
      rules:
      - source: logs
        matcher: keyword_any
        keywords:
        - NOT IN
        - DISTINCT
        - same snapshot
        - one row
        - incorrect result
      - source: artifact_json
        artifacts:
        - metrics
        - queries
        - summary_json
        - workspace_report
        matcher: json_text_contains_any
        keywords:
        - NOT IN
        - DISTINCT
        - same snapshot
        - one row
        - incorrect result
    - id: root_cause_mechanism_evidence_present
      weight: 10
      description: Root-cause mechanism evidence is present in the collected evidence.
      match: any_of
      rules:
      - source: logs
        matcher: keyword_any
        keywords:
        - StreamAgg
        - 32 MiB
        - pagination
        - final group
        - unfinished group
      - source: artifact_json
        artifacts:
        - metrics
        - queries
        - summary_json
        - workspace_report
        matcher: json_text_contains_any
        keywords:
        - StreamAgg
        - 32 MiB
        - pagination
        - final group
        - unfinished group
    - id: scope_or_timeline_evidence_present
      weight: 10
      description: Plan comparison evidence is present in the collected evidence.
      match: any_of
      rules:
      - source: logs
        matcher: keyword_any
        keywords:
        - HASH_AGG
        - remove DISTINCT
        - IndexRangeScan
        - same data
        - admin check
      - source: artifact_json
        artifacts:
        - metrics
        - queries
        - summary_json
        - workspace_report
        matcher: json_text_contains_any
        keywords:
        - HASH_AGG
        - remove DISTINCT
        - IndexRangeScan
        - same data
        - admin check
  evidence_routing:
    weight: 5
    checks:
    - id: specialist_routing_contract
      weight: 3
      required: true
      description: Diagnosis is authored by the analyst with a valid classification and the expected specialist route.
      match: all_of
      rules:
      - source: routing_fact
        matcher: bool_true
        path: diagnosis_analyst_dispatched
      - source: routing_fact
        matcher: bool_true
        path: classification_contract_valid
      - source: routing_fact
        matcher: bool_true
        path: expected_diagnosis_skill_routes_selected
    - 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_patch_present
      - 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_patch_present
      - source: routing_fact
        matcher: bool_true
        path: analysis_query_refs_resolvable
  diagnosis:
    checks:
    - id: identify_primary_failure_mechanism
      mode: blended
      weight: 3
      required: true
      description: Primary failure mechanism is identified and supported.
      deterministic:
        match: all_of
        rules:
        - source: output_text
          matcher: keyword_any
          keywords:
          - StreamAgg
        - source: output_text
          matcher: keyword_any
          keywords:
          - 32 MiB
          - 32MiB
          - pagination
        - source: output_text
          matcher: keyword_any
          keywords:
          - group
          - lost
          - drop
      semantic:
        id: 1
        title: Primary failure mechanism
        description: The answer identifies the StreamAgg response-boundary group-loss defect.
    - id: explain_causal_chain
      mode: blended
      weight: 3
      required: true
      description: Causal chain is identified and supported.
      deterministic:
        match: all_of
        rules:
        - source: output_text
          matcher: keyword_any
          keywords:
          - DISTINCT
        - source: output_text
          matcher: keyword_any
          keywords:
          - NOT IN
        - source: output_text
          matcher: keyword_any
          keywords:
          - extra row
          - incorrect result
          - one row
      semantic:
        id: 2
        title: Causal chain
        description: The answer explains how the missing subquery group changes NOT IN semantics and produces the one-row difference.
    - id: recommend_aligned_remediation
      mode: blended
      weight: 3
      required: true
      description: Aligned remediation is identified and supported.
      deterministic:
        match: all_of
        rules:
        - source: output_text
          matcher: keyword_any
          keywords:
          - remove DISTINCT
          - without DISTINCT
        - source: output_text
          matcher: keyword_any
          keywords:
          - HASH_AGG
          - HASH_AGG()
        - source: output_text
          matcher: keyword_any
          keywords:
          - upgrade
          - fix
      semantic:
        id: 3
        title: Aligned remediation
        description: The answer recommends avoiding StreamAgg through query rewrite or HashAgg and tracks the permanent fix.
