diff --git a/backend/src/admin/department-task-completion.policy.spec.ts b/backend/src/admin/department-task-completion.policy.spec.ts new file mode 100644 index 00000000..b6444efd --- /dev/null +++ b/backend/src/admin/department-task-completion.policy.spec.ts @@ -0,0 +1,44 @@ +import { BadRequestException } from "@nestjs/common"; +import { assertTaskCanComplete } from "./department-task-completion.policy"; + +describe("department task completion policy", () => { + it("rejects model-only output without a live deliverable", () => { + expect(() => + assertTaskCanComplete({ + deliverableUrl: null, + acceptanceCriteria: { criteria: ["live URL responds 200"] }, + executionLogs: [{ status: "COMPLETED_BY_AGENT" }], + }), + ).toThrow(BadRequestException); + }); + + it("rejects completion without acceptance criteria", () => { + expect(() => + assertTaskCanComplete({ + deliverableUrl: "https://canina.ir/blog/verified", + acceptanceCriteria: null, + executionLogs: [{ status: "QA_PASSED" }], + }), + ).toThrow(BadRequestException); + }); + + it("rejects completion without QA evidence", () => { + expect(() => + assertTaskCanComplete({ + deliverableUrl: "https://canina.ir/blog/verified", + acceptanceCriteria: { criteria: ["HTTP 200"] }, + executionLogs: [{ status: "COMPLETED_BY_AGENT" }], + }), + ).toThrow(BadRequestException); + }); + + it("allows completion with URL, criteria and QA evidence", () => { + expect(() => + assertTaskCanComplete({ + deliverableUrl: "https://canina.ir/blog/verified", + acceptanceCriteria: { criteria: ["HTTP 200"] }, + executionLogs: [{ status: "QA_PASSED" }], + }), + ).not.toThrow(); + }); +}); diff --git a/backend/src/admin/department-task-completion.policy.ts b/backend/src/admin/department-task-completion.policy.ts new file mode 100644 index 00000000..72fbbf3e --- /dev/null +++ b/backend/src/admin/department-task-completion.policy.ts @@ -0,0 +1,40 @@ +import { BadRequestException } from "@nestjs/common"; + +interface CompletabaleTask { + deliverableUrl?: string | null; + acceptanceCriteria?: unknown; + executionLogs?: unknown; +} + +function hasAcceptanceCriteria(criteria: unknown): boolean { + if (!criteria) return false; + if (typeof criteria === "string") return criteria.trim().length > 0; + if (Array.isArray(criteria)) return criteria.length > 0; + if (typeof criteria === "object") { + const value = criteria as Record; + const items = value.criteria; + return Array.isArray(items) ? items.length > 0 : Object.keys(value).length > 0; + } + return false; +} + +function hasQaEvidence(logs: unknown): boolean { + if (!Array.isArray(logs)) return false; + return logs.some((log) => { + if (!log || typeof log !== "object") return false; + const status = String((log as Record).status || "").toUpperCase(); + return ["QA_PASSED", "VERIFIED", "PRODUCTION_VERIFIED"].includes(status); + }); +} + +export function assertTaskCanComplete(task: CompletabaleTask): void { + if (!task.deliverableUrl?.trim()) { + throw new BadRequestException("DONE requires a verifiable deliverable URL"); + } + if (!hasAcceptanceCriteria(task.acceptanceCriteria)) { + throw new BadRequestException("DONE requires explicit acceptance criteria"); + } + if (!hasQaEvidence(task.executionLogs)) { + throw new BadRequestException("DONE requires QA/production verification evidence"); + } +} diff --git a/backend/src/admin/department-tasks.controller.ts b/backend/src/admin/department-tasks.controller.ts index 22d57571..3dfa6826 100644 --- a/backend/src/admin/department-tasks.controller.ts +++ b/backend/src/admin/department-tasks.controller.ts @@ -10,6 +10,7 @@ import { UseGuards, NotFoundException, } from "@nestjs/common"; +import { assertTaskCanComplete } from "./department-task-completion.policy"; import { ApiTags, ApiBearerAuth, ApiOperation } from "@nestjs/swagger"; import { JwtAuthGuard } from "../auth/jwt-auth.guard"; import { RolesGuard } from "../common/guards/roles.guard"; @@ -165,6 +166,15 @@ export class DepartmentTasksController { }); } + const candidateDeliverableUrl = body.deliverableUrl || existing.deliverableUrl; + if (body.status === "DONE") { + assertTaskCanComplete({ + deliverableUrl: candidateDeliverableUrl, + acceptanceCriteria: existing.acceptanceCriteria, + executionLogs: logs, + }); + } + const updateData: any = { status: body.status, executionLogs: logs, diff --git a/frontend/admin-panel/src/pages/TasksManagementPage.tsx b/frontend/admin-panel/src/pages/TasksManagementPage.tsx index 3b294125..48d44407 100644 --- a/frontend/admin-panel/src/pages/TasksManagementPage.tsx +++ b/frontend/admin-panel/src/pages/TasksManagementPage.tsx @@ -134,10 +134,16 @@ export default function TasksManagementPage() { ۳. تست استیج و QA (TEST) ); + case 'WAITING_DEPENDENCIES': + return ( + + ۳. منتظر تکمیل اقدامات وابسته + + ); case 'DONE': return ( - ۴. مستقر پروداکشن (DONE) + ۴. خروجی راستی‌آزمایی‌شده (DONE) ); default: diff --git a/scripts/orchestration/orchestration_worker.py b/scripts/orchestration/orchestration_worker.py new file mode 100644 index 00000000..b5dc78fa --- /dev/null +++ b/scripts/orchestration/orchestration_worker.py @@ -0,0 +1,388 @@ +#!/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() diff --git a/scripts/orchestration/test_orchestration_worker.py b/scripts/orchestration/test_orchestration_worker.py new file mode 100644 index 00000000..5d8e9ef4 --- /dev/null +++ b/scripts/orchestration/test_orchestration_worker.py @@ -0,0 +1,62 @@ +import json +import unittest + +from orchestration_worker import ( + build_child_tasks, + determine_parent_status, + parse_structured_response, +) + + +class OrchestrationWorkerTests(unittest.TestCase): + def test_unverified_model_response_can_never_be_done(self): + self.assertEqual( + determine_parent_status(has_children=False, deliverable_url=None, verified=False), + "TEST", + ) + self.assertEqual( + determine_parent_status(has_children=False, deliverable_url="https://canina.ir/x", verified=False), + "TEST", + ) + + def test_verified_live_deliverable_may_be_done(self): + self.assertEqual( + determine_parent_status(has_children=False, deliverable_url="https://canina.ir/x", verified=True), + "DONE", + ) + + def test_children_force_waiting_dependencies(self): + self.assertEqual( + determine_parent_status(has_children=True, deliverable_url=None, verified=False), + "WAITING_DEPENDENCIES", + ) + + def test_parse_json_code_fence(self): + payload = parse_structured_response('```json\n{"summary":"ok","child_tasks":[]}\n```') + self.assertEqual(payload["summary"], "ok") + + def test_child_task_codes_are_deterministic_and_include_parent(self): + proposal = { + "child_tasks": [ + { + "department": "CONTENT", + "title": "تدوین مقاله", + "description": "مقاله مستند", + "priority": "HIGH", + "acceptance_criteria": ["پیش‌نویس موجود باشد"], + } + ] + } + first = build_child_tasks("TSK-390939", proposal, depth=0) + second = build_child_tasks("TSK-390939", proposal, depth=0) + self.assertEqual(first[0]["code"], second[0]["code"]) + self.assertEqual(first[0]["acceptanceCriteria"]["parentTaskCode"], "TSK-390939") + self.assertEqual(first[0]["acceptanceCriteria"]["depth"], 1) + + def test_depth_limit_blocks_recursive_fanout(self): + proposal = {"child_tasks": [{"department": "SEO", "title": "x"}]} + self.assertEqual(build_child_tasks("TSK-1", proposal, depth=2), []) + + +if __name__ == "__main__": + unittest.main()