"""extend.py: your turn, add a SECOND incident type and keep the gate green.

The drill handles one incident: a bad release, mitigated by a rollback. Real on-call sees many shapes.
Add a second one and prove your operations loop still closes: a STALE-INDEX outage, where the runbook's
fix is to re-embed the changed docs (M33 data ops), not to roll back a release.

Steps:
  1. In ../solution/drill.py, the M33 block of parts.py already gives you the release tools; for this
     you also want a tiny index. Add a stale-index scenario: a source doc changed, answers went wrong,
     the runbook step is "reindex the changed doc", and the SLI recovers.
  2. Add a check to ../solution/evals.py for your new scenario (assert it detected + reindexed + recovered).
  3. Follow the M26 rule: add the check FIRST (watch it fail), then make it pass.
  4. Keep `python ../solution/evals.py` green (exit 0).

This file is just a sketch to think against; do the real work in drill.py and evals.py.
"""

# A minimal stale-index helper you can drop into the drill (compact M33 data-ops idea):
import hashlib


def _hash(text):
    return hashlib.sha256(text.encode()).hexdigest()[:12]


class TinyIndex:
    def __init__(self):
        self.docs = {}                       # id -> {source_hash}

    def upsert(self, doc_id, text):
        self.docs[doc_id] = {"source_hash": _hash(text)}

    def is_stale(self, doc_id, current_text):
        rec = self.docs.get(doc_id)
        return rec is None or rec["source_hash"] != _hash(current_text)

    def reindex(self, doc_id, current_text):
        self.upsert(doc_id, current_text)    # re-embed the changed doc


if __name__ == "__main__":
    idx = TinyIndex()
    idx.upsert("refund-policy", "Refunds within 30 days.")
    changed = "Refunds within 14 days."      # the source changed upstream
    print("stale before reindex:", idx.is_stale("refund-policy", changed))   # True
    idx.reindex("refund-policy", changed)
    print("stale after reindex: ", idx.is_stale("refund-policy", changed))   # False
    print("\nNow wire this into drill.py as a second incident, and add its eval check. Keep evals.py green.")
