TEXT_FIELDS = ("scenario", "buyer", "baseline", "backend_dependency",
               "validation_owner", "outcome_metric", "source_id")
def assess(record):
    errors = [name for name in TEXT_FIELDS if type(record.get(name)) is not str or not record.get(name)]
    for name, unit in (("frequency", "decisions/week"), ("service_hours", "hours/quarter"),
                       ("backend_cost_increase", "fraction")):
        item = record.get(name, {})
        if type(item.get("value")) not in (int, float) or item.get("unit") != unit:
            errors.append(name + "_schema")
    for name in ("owned_data", "owned_ip", "paying_pilot"):
        if type(record.get(name)) is not bool:
            errors.append(name)
    scalable = (not errors and record["paying_pilot"] and record["owned_data"] and record["owned_ip"] and
                record["service_hours"]["value"] <= 80 and record["backend_cost_increase"]["value"] <= 0.20)
    return {"decision": "invalid" if errors else ("product-evidence" if scalable else "services-only"),
            "errors": sorted(set(errors)), "service_margin_hours": None if errors else 80 - record["service_hours"]["value"],
            "backend_margin_fraction": None if errors else 0.20 - record["backend_cost_increase"]["value"]}
record = {"scenario": "paid routing pilot", "buyer": "daily route planner",
          "frequency": {"value": 5, "unit": "decisions/week"},
          "baseline": "incumbent tuned heuristic", "owned_data": True, "owned_ip": True,
          "backend_dependency": "two declared QPU adapters", "validation_owner": "customer operations",
          "outcome_metric": "cost per feasible route", "source_id": "scenario:pilot-ledger-Q3",
          "service_hours": {"value": 120, "unit": "hours/quarter"},
          "backend_cost_increase": {"value": 0.40, "unit": "fraction"}, "paying_pilot": True}
base_result = assess(record)
bad = {key: value for key, value in record.items() if key != "validation_owner"}
bad_result = assess(bad)
scaled = {**record, "service_hours": {"value": 70, "unit": "hours/quarter"},
          "backend_cost_increase": {"value": 0.15, "unit": "fraction"}}
scaled_result = assess(scaled)
assert base_result["decision"] == "services-only" and base_result["service_margin_hours"] == -40
assert bad_result["decision"] == "invalid" and "validation_owner" in bad_result["errors"]
assert scaled_result["decision"] == "product-evidence" and scaled_result["backend_margin_fraction"] > 0
print(f"PASS: 77 company workbook base={base_result['decision']} service_margin={base_result['service_margin_hours']}h invalid={bad_result['errors']} scaled={scaled_result['decision']}")
