"""Offline educational reconstruction. No HTTP client or production access.""" from copy import deepcopy import json from pathlib import Path class WorkflowError(Exception): pass class MockBitrix: """Minimal REST-shaped fixture; deliberately cannot connect to a portal.""" def __init__(self, fail=False, corrupt=False): self.tasks = {} self.fail, self.corrupt = fail, corrupt def call(self, method, params): if self.fail: return {"error": "DEMO_FAILURE"} if method == "tasks.task.add": task_id = 900001 + len(self.tasks) task = {"id": str(task_id), **deepcopy(params["fields"])} self.tasks[task_id] = task return {"result": {"task": deepcopy(task)}} if method == "tasks.task.get": task = deepcopy(self.tasks.get(int(params["taskId"]), {})) if self.corrupt: task["UF_CRM_TASK"] = [] return {"result": {"task": task}} raise WorkflowError("Unsupported demo method") def create_verified_task(api, case): if not case.get("human_approved"): raise WorkflowError("Human approval is required in this scenario") links = case.get("crm_links", []) if not links or not case.get("acceptance_criteria"): raise WorkflowError("CRM links and acceptance criteria are required") fields = {"TITLE": case["title"], "DESCRIPTION": case["description"] + "\n\nКритерии приёмки:\n" + "\n".join(case["acceptance_criteria"]), "RESPONSIBLE_ID": case["responsible_id"], "UF_CRM_TASK": links} response = api.call("tasks.task.add", {"fields": fields}) if response.get("error"): raise WorkflowError("Task creation failed; no retry was attempted") try: task_id = int(response["result"]["task"]["id"]) except (KeyError, TypeError, ValueError): raise WorkflowError("Uncertain creation result; reconcile before retry") from None readback = api.call("tasks.task.get", {"taskId": task_id, "select": ["*", "UF_CRM_TASK"]}) task = readback.get("result", {}).get("task", {}) # API field casing varies: normalize selected fields, then compare values. actual = {k.replace("_", "").lower(): v for k, v in task.items()} expected = {k.replace("_", "").lower(): v for k, v in fields.items()} if readback.get("error") or any(actual.get(k) != v for k, v in expected.items()): raise WorkflowError(f"Readback mismatch for demo task {task_id}; stop and reconcile") return {"mode": "offline mock", "synthetic": True, "task_id": task_id, "creation_verified": True, "crm_links": links, "handoff": {"to": "Agent B", "next": "Run synthetic acceptance checks", "acceptance": "pending human review"}, "journal": "Task creation and CRM links verified. Execution is still pending."} def main(): case = json.loads((Path(__file__).resolve().parents[1] / "examples/case.json").read_text()) print(json.dumps(create_verified_task(MockBitrix(), case), ensure_ascii=False, indent=2)) if __name__ == "__main__": main()