FIELDS = {"owner": str, "cost": dict, "duration": dict, "learning_artifact": str,
          "portable": bool, "expiry": str, "stop_trigger": str, "source_id": str}
def choose(dossier, maximum_spend_usd, deadline_weeks):
    errors = []
    eligible = []
    for name, option in dossier.get("options", {}).items():
        missing = [field for field, kind in FIELDS.items()
                   if field not in option or type(option[field]) is not kind or option[field] in ("", None)]
        cost, duration = option.get("cost", {}), option.get("duration", {})
        if type(cost.get("value")) is not int or cost.get("unit") != "USD":
            missing.append("cost_schema")
        if type(duration.get("value")) is not int or duration.get("unit") != "weeks":
            missing.append("duration_schema")
        if missing:
            errors.append(name + ":" + ",".join(sorted(set(missing))))
        elif (cost["value"] <= maximum_spend_usd and duration["value"] <= deadline_weeks and
              option["portable"] and option["learning_artifact"]):
            eligible.append(name)
    choice = "partner" if "partner" in eligible else (eligible[0] if eligible else "defer")
    return {"decision": "invalid" if errors else choice, "errors": errors, "eligible": sorted(eligible)}
dossier = {"scenario": "four-week chemistry information purchase", "options": {
    "build": {"owner": "R&D", "cost": {"value": 800000, "unit": "USD"},
              "duration": {"value": 24, "unit": "weeks"}, "learning_artifact": "resource ledger",
              "portable": True, "expiry": "2026-11-14", "stop_trigger": "resource ceiling missed",
              "source_id": "scenario:internal-estimate"},
    "partner": {"owner": "science lead", "cost": {"value": 120000, "unit": "USD"},
                "duration": {"value": 4, "unit": "weeks"}, "learning_artifact": "portable benchmark",
                "portable": True, "expiry": "2026-11-14", "stop_trigger": "no baseline gap",
                "source_id": "scenario:partner-SOW"},
    "wait": {"owner": "strategy", "cost": {"value": 0, "unit": "USD"},
             "duration": {"value": 12, "unit": "weeks"}, "learning_artifact": "scheduled evidence review",
             "portable": False, "expiry": "2026-11-14", "stop_trigger": "new evidence release",
             "source_id": "scenario:review-calendar"}}}
base_result = choose(dossier, 200000, 8)
bad_options = {**dossier["options"], "partner": {key: value for key, value in dossier["options"]["partner"].items() if key != "owner"}}
bad_result = choose({**dossier, "options": bad_options}, 200000, 8)
tight_result = choose(dossier, 100000, 8)
assert base_result["decision"] == "partner" and base_result["eligible"] == ["partner"]
assert bad_result["decision"] == "invalid" and "owner" in bad_result["errors"][0]
assert tight_result["decision"] == "defer" and tight_result["eligible"] == []
print(f"PASS: 72 option workbook choice={base_result['decision']} invalid={bad_result['errors']} spend100k={tight_result['decision']}")
