"""Deposit-flight playbook, step 7: the observation-time ablation, pinned.

Observation time is enabled per model on evidence you see first: the model with the
silence token, its flag-off twin and the platform's internal quality safeguards,
all graded at the identical points. The evaluation records that evidence in the model's report
of record; it recommends enablement only when the model passes G1-G6 and does
not regress against its twin. Evidence that does not recommend enabling is
information, not a block: the bundle in step 8 reports it as the finding
`observation_time_not_recommended`, which the operator acknowledges or not.
"""

import json
import os
import sys
from pathlib import Path

sys.path.insert(0, str(Path(__file__).resolve().parents[1]))  # playbooks/playbook_api.py
from playbook_api import Api, State

api, state = Api(), State("deposit-flight")

# The M16 switch this playbook proves: "on" (the default) builds, trains and
# serves with observation time; "off" runs the same chain without it, and the
# quiet-book step then fails, because nothing rises under silence.
FLAG_ON = os.environ.get("TESSERA_PLAYBOOK_OBSERVATION_TIME", "on") != "off"

if not FLAG_ON:
    print("observation time is off: no ablation to run")
    print("ablation step: done")
    sys.exit(0)
models = state["models"]
created = api.post(
    f"/models/{models['on']}/observation-ablations",
    {"without_token_model_id": models["off"]},
    key="deposit-flight-ablation",
)
ablation = api.wait(
    f"/models/{models['on']}/observation-ablations/{created['report_id']}",
    lambda item: item.get("status") in ("succeeded", "failed"),
    what="the observation-time ablation",
)
print(f"ablation: {ablation['status']}")
if ablation["status"] != "succeeded":
    sys.exit(json.dumps(ablation)[:3000])
print(f"gates passed: {ablation.get('gates_passed')}")
print(f"recommend enable: {ablation.get('recommend_enable')}")
report = api.get(f"/models/{models['on']}/eval-report")
evidence = report.get("observation_time_ablation") or {}
for row in evidence.get("labels") or []:
    print(
        f"{row['metric']} with the token {row['with_token']:.4f}, "
        f"without {row['without_token']:.4f} (delta {row['delta']:+.4f})"
    )
for name, arm in sorted((evidence.get("arms") or {}).items()):
    print(f"arm {name}: {arm.get('status')}")
if not ablation.get("recommend_enable"):
    # Since WP-M26 G1 also compares the model with the safeguard arms, and this
    # small CPU fixture can trail the tuned safeguard; the bundle reports it.
    gates = {name: gate.get("status") for name, gate in (report.get("gates") or {}).items()}
    print(f"gates: {json.dumps(gates, sort_keys=True)}")
    print("the evidence does not recommend enabling: the bundle reports it as a finding")
print("ablation step: done")
