#!/usr/bin/env python3 """Canina cross-department orchestration worker. A model response is analysis, not delivery. The worker records the exact prompt and response, creates deterministic cross-department child tasks, and keeps the parent open until dependencies and acceptance evidence are satisfied. """ from __future__ import annotations import hashlib import json import os import re import subprocess from datetime import datetime, timezone from pathlib import Path from typing import Any import requests MAX_FANOUT_DEPTH = 2 VALID_DEPARTMENTS = { "PMO", "DEV", "DEVOPS", "SEO", "CONTENT", "CLINICAL_RD", "B2B", "SUPPORT" } def utc_now() -> str: return datetime.now(timezone.utc).isoformat() def parse_structured_response(text: str) -> dict[str, Any]: cleaned = text.strip() fenced = re.search(r"```(?:json)?\s*(\{.*\})\s*```", cleaned, re.DOTALL) if fenced: cleaned = fenced.group(1) else: start, end = cleaned.find("{"), cleaned.rfind("}") if start >= 0 and end > start: cleaned = cleaned[start : end + 1] parsed = json.loads(cleaned) if not isinstance(parsed, dict): raise ValueError("Model response must be a JSON object") parsed.setdefault("summary", "") parsed.setdefault("child_tasks", []) parsed.setdefault("deliverable", {}) return parsed def _child_code(parent_code: str, department: str, title: str) -> str: digest = hashlib.sha256(f"{parent_code}|{department}|{title}".encode()).hexdigest()[:8].upper() return f"{parent_code}-{department[:4]}-{digest}"[:50] def build_child_tasks(parent_code: str, proposal: dict[str, Any], depth: int) -> list[dict[str, Any]]: if depth >= MAX_FANOUT_DEPTH: return [] children: list[dict[str, Any]] = [] for raw in proposal.get("child_tasks") or []: if not isinstance(raw, dict): continue department = str(raw.get("department") or "").upper().strip() title = str(raw.get("title") or "").strip() if department not in VALID_DEPARTMENTS or not title: continue criteria = raw.get("acceptance_criteria") or [] children.append( { "code": _child_code(parent_code, department, title), "department": department, "title": title[:255], "description": str(raw.get("description") or title), "priority": str(raw.get("priority") or "HIGH").upper(), "assignee": f"Canina-{department}-Agent", "acceptanceCriteria": { "parentTaskCode": parent_code, "depth": depth + 1, "criteria": criteria if isinstance(criteria, list) else [str(criteria)], "requiredDeliverableType": raw.get("deliverable_type"), }, } ) return children def determine_parent_status(*, has_children: bool, deliverable_url: str | None, verified: bool) -> str: if has_children: return "WAITING_DEPENDENCIES" if deliverable_url and verified: return "DONE" return "TEST" class Database: def __init__(self) -> None: self.ssh = ["ssh", "-F", "/opt/data/.ssh/config", "parsa-host"] self.psql = [ "docker", "exec", "-i", "canino_db_prod", "psql", "-U", "canino_prod", "-d", "caninodb_prod", "-t", "-A", ] def sql(self, statement: str) -> str: result = subprocess.run( self.ssh + self.psql, input=statement, text=True, capture_output=True, timeout=45, check=True, ) return result.stdout.strip() def rows_json(self, query: str) -> list[dict[str, Any]]: wrapped = f"SELECT COALESCE(json_agg(q), '[]'::json)::text FROM ({query}) q;" raw = self.sql(wrapped) return json.loads(raw or "[]") def append_event(self, task_id: str, event: dict[str, Any], *, status: str | None = None) -> None: event_json = json.dumps(event, ensure_ascii=False).replace("$event$", "") status_sql = f", status = '{status}'" if status else "" self.sql( f""" UPDATE department_tasks SET execution_logs = COALESCE(execution_logs, '[]'::jsonb) || $event${event_json}$event$::jsonb, updated_at = NOW(){status_sql} WHERE id = '{task_id}'; """ ) def insert_children(self, parent: dict[str, Any], children: list[dict[str, Any]]) -> int: inserted = 0 for child in children: criteria = json.dumps(child["acceptanceCriteria"], ensure_ascii=False).replace("$criteria$", "") title = child["title"].replace("'", "''") description = child["description"].replace("'", "''") assignee = child["assignee"].replace("'", "''") out = self.sql( f""" INSERT INTO department_tasks (id, code, department, title, description, status, priority, assignee, acceptance_criteria, execution_logs, created_at, updated_at) VALUES (gen_random_uuid(), '{child['code']}', '{child['department']}', '{title}', '{description}', 'TODO', '{child['priority']}', '{assignee}', $criteria${criteria}$criteria$::jsonb, '[]'::jsonb, NOW(), NOW()) ON CONFLICT (code) DO NOTHING RETURNING code; """ ) if out: inserted += 1 return inserted class ModelClient: def __init__(self) -> None: config_path = Path(os.environ.get("HERMES_CONFIG", "/opt/data/config.yaml")) text = config_path.read_text(encoding="utf-8") match = re.search(r"name: 9router (?:Combo|Antigravity).*?api_key:\s*([^\s]+)", text, re.DOTALL) if not match: raise RuntimeError("9router API key not configured") self.api_key = match.group(1).strip() def complete(self, prompt: str) -> str: response = requests.post( "https://nr.parsaaghayi.ir/v1/chat/completions", headers={"Authorization": f"Bearer {self.api_key}", "Content-Type": "application/json"}, json={ "model": "Canina-content", "messages": [ {"role": "system", "content": "You are Canina's cross-department PMO. Return strict JSON only."}, {"role": "user", "content": prompt}, ], "temperature": 0.1, "stream": False, }, timeout=180, ) response.raise_for_status() try: return response.json()["choices"][0]["message"]["content"] except Exception: chunks: list[str] = [] for line in response.text.splitlines(): if not line.startswith("data: ") or line.endswith("[DONE]"): continue try: chunks.append(json.loads(line[6:])["choices"][0]["delta"].get("content", "")) except Exception: pass return "".join(chunks) def decomposition_prompt(task: dict[str, Any]) -> str: logs = task.get("execution_logs") or [] raw_output = logs[-1].get("model_response", "") if logs else "" depth = int((task.get("acceptance_criteria") or {}).get("depth", 0)) return f""" ماموریت {task['code']} در دپارتمان {task['department']} فقط یک تحلیل متنی تولید کرده و هنوز تحویل اجرایی تاییدشده ندارد. عنوان: {task['title']} خروجی خام واقعی مدل: {raw_output} این خروجی را به یک برنامه اجرایی میان‌دپارتمانی تبدیل کن. پاسخ فقط JSON معتبر با این ساختار باشد: {{ "summary": "نتیجه واقعی مرحله تحلیل، بدون ادعای انتشار یا استقرار", "deliverable": {{"type": "analysis", "url": null, "verified": false}}, "child_tasks": [ {{ "department": "یکی از SEO,CONTENT,CLINICAL_RD,B2B,DEV,DEVOPS,SUPPORT,PMO", "title": "اقدام مستقل و قابل تحویل", "description": "شرح دقیق اجرا", "priority": "HIGH", "deliverable_type": "نوع خروجی قابل راستی‌آزمایی", "acceptance_criteria": ["معیار عینی اول", "معیار عینی دوم"] }} ] }} قواعد: حداکثر ۶ child task؛ تکراری نساز؛ کار صرفاً تحلیلی را DONE ننام؛ انتشار، تماس، پیامک، تغییر کد یا پروداکشن را انجام‌شده فرض نکن؛ عمق فعلی {depth} است. """.strip() def execution_prompt(task: dict[str, Any]) -> str: criteria = task.get("acceptance_criteria") or {} return f""" شما ایجنت اجرایی دپارتمان {task['department']} کنینا هستید. کد: {task['code']} عنوان: {task['title']} شرح: {task.get('description') or ''} معیار پذیرش: {json.dumps(criteria, ensure_ascii=False)} این مرحله را واقعاً تا حدی که بدون دسترسی به ابزار خارجی ممکن است انجام بده. پاسخ فقط JSON معتبر: {{ "summary": "آنچه واقعاً تولید یا بررسی شد", "deliverable": {{ "type": "draft|research|code_plan|contact_plan|clinical_review|seo_plan", "content": "خروجی کامل و قابل استفاده، نه خلاصه نمایشی", "url": null, "verified": false }}, "verification": {{"passed": false, "evidence": []}}, "child_tasks": [] }} هیچ انتشار، تماس، دپلوی، تغییر سایت یا تایید بالینی را انجام‌شده فرض نکن. اگر نیازمند ابزار/دپارتمان دیگری است، child_tasks ساختاریافته بساز. """.strip() def execute_next_child(db: Database, model: ModelClient) -> bool: tasks = db.rows_json( """ SELECT id, code, department, title, description, status, acceptance_criteria, execution_logs, deliverable_url FROM department_tasks WHERE status = 'TODO' AND acceptance_criteria->>'parentTaskCode' IS NOT NULL ORDER BY created_at ASC LIMIT 1 """ ) if not tasks: return False task = tasks[0] prompt = execution_prompt(task) db.append_event(task["id"], { "timestamp": utc_now(), "status": "AGENT_EXECUTION_STARTED", "agent": task.get("assignee") or f"Canina-{task['department']}-Agent", "prompt_to_model": prompt, }, status="IN_PROGRESS") try: raw = model.complete(prompt) result = parse_structured_response(raw) except Exception as exc: db.append_event(task["id"], { "timestamp": utc_now(), "status": "AGENT_EXECUTION_FAILED", "error": str(exc), }, status="TODO") raise criteria = task.get("acceptance_criteria") or {} depth = int(criteria.get("depth", 1)) children = build_child_tasks(task["code"], result, depth) inserted = db.insert_children(task, children) deliverable = result.get("deliverable") or {} verification = result.get("verification") or {} verified = bool(deliverable.get("verified")) and bool(verification.get("passed")) next_status = determine_parent_status( has_children=bool(children), deliverable_url=deliverable.get("url"), verified=verified, ) db.append_event(task["id"], { "timestamp": utc_now(), "status": "AGENT_EXECUTION_COMPLETED", "agent": task.get("assignee") or f"Canina-{task['department']}-Agent", "model_response": raw, "summary": result.get("summary"), "deliverable": deliverable, "verification": verification, "childrenCreated": inserted, }, status=next_status) return True def reconcile_parent_states(db: Database) -> bool: parents = db.rows_json( """ SELECT id, code, status FROM department_tasks p WHERE status = 'WAITING_DEPENDENCIES' AND NOT EXISTS ( SELECT 1 FROM department_tasks c WHERE c.acceptance_criteria->>'parentTaskCode' = p.code AND c.status NOT IN ('DONE') ) ORDER BY created_at ASC LIMIT 1 """ ) if not parents: return False parent = parents[0] db.append_event(parent["id"], { "timestamp": utc_now(), "status": "DEPENDENCIES_COMPLETED", "agent": "Canina-PMO-Agent", "note": "تمام child taskها تکمیل و راستی‌آزمایی شده‌اند؛ والد وارد QA نهایی شد.", }, status="TEST") return True def reconcile_pseudo_done(db: Database, model: ModelClient) -> bool: tasks = db.rows_json( """ SELECT id, code, department, title, description, status, acceptance_criteria, execution_logs, deliverable_url FROM department_tasks WHERE status = 'DONE' AND code LIKE 'TSK-%' AND (deliverable_url IS NULL OR deliverable_url = '') AND NOT EXISTS ( SELECT 1 FROM department_tasks child WHERE child.acceptance_criteria->>'parentTaskCode' = department_tasks.code ) ORDER BY created_at ASC LIMIT 1 """ ) if not tasks: return False task = tasks[0] prompt = decomposition_prompt(task) db.append_event(task["id"], { "timestamp": utc_now(), "status": "PMO_DECOMPOSITION_STARTED", "agent": "Canina-PMO-Agent", "prompt_to_model": prompt, }, status="IN_PROGRESS") raw = model.complete(prompt) proposal = parse_structured_response(raw) depth = int((task.get("acceptance_criteria") or {}).get("depth", 0)) children = build_child_tasks(task["code"], proposal, depth) inserted = db.insert_children(task, children) deliverable = proposal.get("deliverable") or {} next_status = determine_parent_status( has_children=bool(children), deliverable_url=deliverable.get("url"), verified=bool(deliverable.get("verified")), ) db.append_event(task["id"], { "timestamp": utc_now(), "status": "PMO_DECOMPOSITION_COMPLETED", "agent": "Canina-PMO-Agent", "model_response": raw, "summary": proposal.get("summary"), "childrenCreated": inserted, }, status=next_status) return True def main() -> None: db = Database() model = ModelClient() if reconcile_pseudo_done(db, model): print("Reconciled one pseudo-DONE root task into cross-department dependencies.") return if execute_next_child(db, model): print("Executed one child task; unverified model output remains in TEST, never DONE.") return if reconcile_parent_states(db): print("Moved one parent with verified completed children into TEST.") return print("No actionable orchestration task found.") if __name__ == "__main__": main()