{
  "schema_version": "1.3.0",
  "slug": "when-does-an-unstable-sort-reorder-equal-elements-and-break-downstream-assumptions",
  "title": "When does an unstable sort reorder equal elements and break downstream assumptions?",
  "date_of_record": "2026 November 2",
  "scope": "Applies to sorts and priority orderings where readers depend on the relative order of equal-key elements, when the sort implementation is not guaranteed stable. Excludes sorts with an explicit tiebreaker, and sorting primitives where equal elements are indistinguishable.",
  "projections": {
    "unified_markdown_uri": "https://corpusfetch.com/c/when-does-an-unstable-sort-reorder-equal-elements-and-break-downstream-assumptions.md",
    "method_python_slice_uri": "https://corpusfetch.com/c/when-does-an-unstable-sort-reorder-equal-elements-and-break-downstream-assumptions.py",
    "remnant_warrant_slice_uri": "https://corpusfetch.com/c/when-does-an-unstable-sort-reorder-equal-elements-and-break-downstream-assumptions.warrant.json"
  },
  "token_profile": {
    "full_markdown_tokens": 715,
    "python_slice_tokens": 95,
    "warrant_slice_tokens": 220
  },
  "remnant_summary": {
    "core_distinction": "**Predicate.** An ordering over equal keys is deterministic if and only if the sort is stable *or* a tiebreaker makes all keys distinct. If neither holds, the relative order of equal elements is a function of the algorithm's internals and the input order. **Failure.** A list is sorted by priority. Items with equal priority come out in different orders on dif...",
    "what_was_observed": "**Signature.** Non-deterministic output from identical input. Tests that pass alone and fail under load. Order-dependent bugs that vanish on re-run and reappear on the next deploy. **Trace.** A priority queue implemented with a binary heap returns equal-priority items in heap-shape order, which depends on insertion order and the sequence of pops. Two replicas with the same events but different arrival orders produce ...",
    "condition_of_validity": "Deterministic ordering holds only while at least one applies: 1. **Stable sort.** The algorithm is documented stable and that stability is preserved across languages and versions. 2. **Total order.** A tiebreaker makes every key distinct, so no equal elements exist to reorder. 3. **Order irrelevance.** Downstream behavior is provably independent of the order...",
    "decision_boundary": "- Stable sort plus no downstream order dependence: safe, but only as long as the algorithm remains stable. - Total order via tiebreaker: safe regardless of algorithm. - Unstable sort with downstream order dependence: non-deterministic by construction. - Unstable sort plus idempotent downstream: safe if idempotence is verified, unsafe if assumed.",
    "operational_effect": "The assumption lives in a comment somewhere far from the sort call, and library upgrades routinely change stability guarantees. Remediation is a tiebreaker in the comparator. The trap is to add `sorted(..., reverse=True)` in the belief that direction implies determinism. It does not; direction is orthogonal to stability."
  },
  "matched_failure_symptoms": [
    "DOMAIN:EPISTEMIC_PROVENANCE_AND_FEDERATION_ROUTING"
  ],
  "composition_edges": {
    "primary_domain": "epistemic_provenance_and_federation_routing",
    "upstream_precondition_residuals": [
      "https://corpusfetch.com/c/according-to-what-precedence-hierarchy-must-an-instance-resolve-contradictory-directives-across-system-developer-and-user-tiers.md",
      "https://corpusfetch.com/c/how-should-an-instance-decide-that-a-stored-conclusion-has-gone-stale-without-a-new-observation.md"
    ],
    "downstream_consequence_residuals": [
      "https://corpusfetch.com/c/by-what-checkpoint-and-compaction-boundaries-must-an-instance-externalize-working-state-to-prevent-context-window-drift-and-truncation-amnesia.md",
      "https://corpusfetch.com/c/across-what-additive-schema-and-unknown-field-preservation-boundaries-must-an-instance-evolve-wire-contracts-to-prevent-rolling-deploy-deserialization-crashes.md"
    ],
    "co_occurring_sibling_residuals": [
      "https://corpusfetch.com/c/how-can-an-instance-capture-implicit-user-preferences-without-storing-private-data.md",
      "https://corpusfetch.com/c/how-can-an-instance-compress-long-reasoning-traces-into-binding-conclusions.md"
    ]
  },
  "empirical_verification_warrant": {
    "status": "AST_AND_INVARIANT_VERIFIED",
    "syntax_valid": true,
    "code_sha256": "544a565681741f6774ce8b073f1b542f52c68c8cbc5fe98aee24209416d9bfd8",
    "verified_classes": [
      "SortStabilityError",
      "SortStabilityGate"
    ],
    "verified_functions": [
      "dispatch_sort_mode",
      "verify"
    ],
    "verified_functions_count": 2,
    "defensive_guards_count": 1,
    "raised_exceptions": [
      "SortStabilityError"
    ],
    "sample_fail_closed_invariants": [
      "unstable sort with order-dependent downstream: equal elements may reorder"
    ],
    "stdlib_dependencies": [
      "dataclasses"
    ],
    "observation_mode": "EMPIRICAL_BOUNDARY_CAPTURE",
    "zero_external_deps": true
  }
}
