schema_version: 1
case_id: write-latency-during-concurrent-upsert
ground_truth:
  expected_diagnosis_skill_routes:
  - any_of:
    - transaction-oncall-handbook
  root_cause: Concurrent upserts repeatedly updated the same aggregate row, creating unavoidable pessimistic-lock contention on one row/region. A second table amplified each write across four secondary indexes, increasing locked KV entries and commit work. Key scattering or more nodes cannot split one existing hot row; the workload must shard or aggregate differently and reduce index/write amplification.
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
        - logs
        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
        - logs
  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
        - logs
        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
        - logs
  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:
        - upsert
        - lock wait
        - write latency
        - same row
        - concurrency
      - source: artifact_json
        artifacts:
        - metrics
        - queries
        - summary_json
        - workspace_report
        matcher: json_text_contains_any
        keywords:
        - upsert
        - lock wait
        - write latency
        - same row
        - concurrency
    - 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:
        - hot row
        - pessimistic lock
        - vote_count
        - same region
        - contention
      - source: artifact_json
        artifacts:
        - metrics
        - queries
        - summary_json
        - workspace_report
        matcher: json_text_contains_any
        keywords:
        - hot row
        - pessimistic lock
        - vote_count
        - same region
        - contention
    - id: scope_or_timeline_evidence_present
      weight: 10
      description: Write-amplification evidence is present in the collected evidence.
      match: any_of
      rules:
      - source: logs
        matcher: keyword_any
        keywords:
        - secondary index
        - KV writes
        - write amplification
        - commit
        - soft delete
      - source: artifact_json
        artifacts:
        - metrics
        - queries
        - summary_json
        - workspace_report
        matcher: json_text_contains_any
        keywords:
        - secondary index
        - KV writes
        - write amplification
        - commit
        - soft delete
  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:
          - same row
          - hot row
        - source: output_text
          matcher: keyword_any
          keywords:
          - pessimistic lock
          - lock contention
        - source: output_text
          matcher: keyword_any
          keywords:
          - upsert
          - concurrent
      semantic:
        id: 1
        title: Primary failure mechanism
        description: The answer identifies application-level hot-row contention as the primary path.
    - 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:
          - secondary index
          - index
        - source: output_text
          matcher: keyword_any
          keywords:
          - write amplification
          - KV writes
        - source: output_text
          matcher: keyword_any
          keywords:
          - commit
          - locks
      semantic:
        id: 2
        title: Causal chain
        description: The answer explains the additional write and lock footprint from secondary indexes.
    - 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:
          - shard
          - partition
          - aggregate
          - queue
        - source: output_text
          matcher: keyword_any
          keywords:
          - reduce
          - index
          - concurrency
        - source: output_text
          matcher: keyword_any
          keywords:
          - not
          - scatter
          - more nodes
      semantic:
        id: 3
        title: Aligned remediation
        description: The answer recommends changing the write model and explicitly rejects key scattering or simple scale-out as a hot-row fix.
