Files
canina/scripts/orchestration/orchestration_worker.py
T

447 lines
19 KiB
Python

#!/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
import uuid
from dataclasses import dataclass
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, strict=False)
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"
# A model URL and its self-assessed verification cannot satisfy independent QA.
return "TEST"
@dataclass(frozen=True)
class QAResult:
passed: bool
reason: str
def verify_artifact(deliverable: dict[str, Any], verification: dict[str, Any]) -> QAResult:
"""Independent, rigorous verification of live deliverables. Rejects all fictitious URLs."""
if not isinstance(deliverable, dict):
return QAResult(False, "Missing or invalid deliverable payload")
url = deliverable.get("url")
if not url or not isinstance(url, str) or not url.strip():
return QAResult(False, "Missing or invalid URL in deliverable")
url = url.strip()
# 1. Reject fake/internal protocols and stage references
if url.startswith("canina://") or url.startswith("git://") or "post-tsk-" in url or "localhost" in url:
return QAResult(False, "Fictitious, internal or stage protocol rejected")
# 2. Enforce standard https
if not url.startswith("https://") and not url.startswith("http://"):
return QAResult(False, "Missing or invalid URL protocol (http/https required)")
# 3. Independent HTTP check
try:
import urllib.request
req = urllib.request.Request(url, headers={"User-Agent": "Canina-Orchestration-QA-Bot/1.0"})
with urllib.request.urlopen(req, timeout=10) as resp:
code = resp.getcode()
if code != 200:
return QAResult(False, f"HTTP check failed with status code {code}")
body = resp.read().decode("utf-8", errors="ignore")
# Check for soft-404 patterns in Persian
soft_404_patterns = ["مقاله یافت نشد", "صفحه مورد نظر یافت نشد", "404 -", "خطای ۴۰۴", "محصول یافت نشد"]
for pattern in soft_404_patterns:
if pattern in body:
return QAResult(False, f"Soft 404 or missing content detected in live page ({pattern})")
if len(body) < 200:
return QAResult(False, "Live page body suspiciously empty (< 200 bytes)")
return QAResult(True, f"Independently verified live artifact: HTTP 200, length {len(body)}")
except Exception as exc:
return QAResult(False, f"HTTP check failed: {exc}")
def _literal(value: Any) -> str:
return "'" + str(value).replace("'", "''") + "'"
def claim_sql(token: str) -> str:
"""Select and lease one root or child in a single locked statement."""
event = json.dumps({"status": "CLAIMED", "claim_token": token})
return f"""
WITH candidate AS (
SELECT id FROM department_tasks
WHERE status = 'TODO' OR (
status = 'IN_PROGRESS' AND execution_logs->-1->>'status' = 'CLAIMED'
AND updated_at < NOW() - INTERVAL '15 minutes'
)
ORDER BY CASE priority WHEN 'CEO_EXPEDITE' THEN 0 WHEN 'CRITICAL' THEN 1
WHEN 'HIGH' THEN 2 WHEN 'MEDIUM' THEN 3 ELSE 4 END,
updated_at ASC, id ASC
FOR UPDATE SKIP LOCKED LIMIT 1
), claimed AS (
UPDATE department_tasks t SET status = 'IN_PROGRESS', updated_at = NOW(),
execution_logs = COALESCE(t.execution_logs, '[]'::jsonb) || {_literal(event)}::jsonb
FROM candidate WHERE t.id = candidate.id
RETURNING t.id, t.code, t.department, t.title, t.description,
t.acceptance_criteria, t.execution_logs, t.assignee, t.priority
) SELECT COALESCE(json_agg(claimed), '[]'::json)::text FROM claimed;
"""
def complete_sql(task_id: str, token: str, status: str, event: dict[str, Any],
children: list[dict[str, Any]] | None = None) -> str:
"""Fenced transition and idempotent child insert in one atomic SQL statement."""
if status not in ('TODO', 'TEST', 'WAITING_DEPENDENCIES', 'REJECTED'):
raise ValueError('Invalid worker transition')
entries = []
for child in children or []:
entries.append('(' + ', '.join(_literal(x) for x in (
child['code'], child['department'], child['title'], child['description'],
child['priority'], child['assignee'], json.dumps(child['acceptanceCriteria'], ensure_ascii=False)
)) + ')')
insert = ''
if entries:
insert = f""", inserted AS (
INSERT INTO department_tasks (id, code, department, title, description,
status, priority, assignee, acceptance_criteria, execution_logs, created_at, updated_at)
SELECT gen_random_uuid(), v.code, v.department, v.title, v.description,
'TODO', v.priority, v.assignee, v.criteria::jsonb, '[]'::jsonb, NOW(), NOW()
FROM (VALUES {', '.join(entries)}) AS v(code, department, title, description, priority, assignee, criteria)
WHERE EXISTS (SELECT 1 FROM completed)
ON CONFLICT (code) DO NOTHING RETURNING id
)"""
return f"""
WITH completed AS (
UPDATE department_tasks SET status = {_literal(status)}, updated_at = NOW(),
execution_logs = COALESCE(execution_logs, '[]'::jsonb) || {_literal(json.dumps(event, ensure_ascii=False))}::jsonb
WHERE id = {_literal(task_id)}::uuid AND status = 'IN_PROGRESS'
AND updated_at >= NOW() - INTERVAL '15 minutes'
AND execution_logs->-1->>'claim_token' = {_literal(token)}
RETURNING id
){insert}
SELECT id FROM completed;
"""
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:
if os.environ.get('CANINA_ORCHESTRATION_LIVE_WRITE') != 'I_ACCEPT_LIVE_MUTATION':
raise RuntimeError('Live database writes disabled; use injected mock database for dry runs')
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 claim_next(self) -> dict[str, Any] | None:
token = str(uuid.uuid4())
rows = json.loads(self.sql(claim_sql(token)) or '[]')
if not rows:
return None
return {**rows[0], 'claim_token': token}
def finish_claim(self, task: dict[str, Any], event: dict[str, Any], status: str,
children: list[dict[str, Any]] | None = None) -> bool:
return bool(self.sql(complete_sql(task['id'], task['claim_token'], status, event, children)))
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 check_corporate_guardrails(task: dict[str, Any], db: Database) -> tuple[bool, str]:
"""Enforce corporate operational constraints (Rate limits, Business domain bounds)."""
dept = (task.get("department") or "").upper()
title = task.get("title") or ""
# 1. Anti-Hallucination & Core Domain Invariant: Supplement / Veterinary only
forbidden_terms = ["لواشک", "پوشاک", "ارز دیجیتال", "بیت کوین", "فارکس", "آرایشی انسان", "املاک"]
for term in forbidden_terms:
if term in title or term in (task.get("description") or ""):
return False, f"Domain Violation: '{term}' is strictly forbidden. Canina operates exclusively in European clinical pet supplements."
# 2. Content Rate Limiting: Max 1 new blog post per 24h
if dept == "CONTENT" and any(k in title for k in ["نگارش محتوای مقاله", "تولید مقاله", "انتشار پست", "پست وبلاگ"]):
# Check how many blogs were created today
try:
today_count_raw = db.sql("SELECT count(*) FROM blogs WHERE created_at >= CURRENT_DATE AND is_published = true;")
today_count = int(today_count_raw.strip().splitlines()[-1] if today_count_raw.strip() else "0")
if today_count >= 1:
return False, "Rate Limit Reached: Maximum 1 new blog post permitted per day. Task queued for next publishing window."
except Exception:
pass
return True, "Passed"
def execute_next_task(db: Database, model: ModelClient) -> bool:
"""Produce reviewable drafts; never publish or trust model QA assertions."""
task = db.claim_next()
if not task:
return False
allowed, reason = check_corporate_guardrails(task, db)
if not allowed:
# Defer task gracefully
event = {'timestamp': utc_now(), 'status': 'RATE_LIMITED_DEFERRED', 'reason': reason}
db.finish_claim(task, event, 'WAITING_DEPENDENCIES')
return True
prompt = execution_prompt(task)
try:
raw = model.complete(prompt)
result = parse_structured_response(raw)
raw_criteria = task.get('acceptance_criteria')
if isinstance(raw_criteria, dict):
criteria = raw_criteria
elif isinstance(raw_criteria, list):
criteria = {'items': raw_criteria, 'depth': 0}
else:
criteria = {}
children = build_child_tasks(task['code'], result, int(criteria.get('depth', 0)))
deliverable = result.get('deliverable') or {}
qa = verify_artifact(deliverable, result.get('verification') or {})
status = determine_parent_status(has_children=bool(children),
deliverable_url=deliverable.get('url'), verified=qa.passed)
event = {'timestamp': utc_now(), 'status': 'QA_PENDING', 'prompt_to_model': prompt,
'model_response': raw, 'deliverable': deliverable,
'qa': qa.reason, 'childrenProposed': len(children)}
if not db.finish_claim(task, event, status, children):
raise RuntimeError('Claim lost before completion; no result committed')
except Exception as exc:
db.finish_claim(task, {'timestamp': utc_now(), 'status': 'AGENT_EXECUTION_FAILED',
'error': str(exc)}, 'TODO')
raise
return True
execute_next_child = execute_next_task
def reconcile_parent_states(db: Database) -> bool:
parents = db.rows_json(
"""
SELECT id, code, status
FROM department_tasks p
WHERE status = 'WAITING_DEPENDENCIES'
AND EXISTS (SELECT 1 FROM department_tasks c
WHERE c.acceptance_criteria->>'parentTaskCode' = p.code)
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
# Dependency completion alone cannot pass parent QA. Leave it blocked pending
# independently persisted attestations; no synthetic URL or DONE transition.
return False
def reconcile_pseudo_done(db: Database, model: ModelClient) -> bool:
# Legacy DONE records require a separate human audit, not autonomous rewrites.
return False
def main() -> None:
db = Database()
model = ModelClient()
if execute_next_task(db, model):
print("Executed one child task; unverified model output remains in TEST, never DONE.")
return
# Parent reconciliation requires independent persisted QA attestations.
print("No actionable orchestration task found.")
if __name__ == "__main__":
main()