import { BadRequestException } from "@nestjs/common"; export interface CompletableTask { code?: string | null; title?: string | null; 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 hasValidQaEvidence(logs: unknown): boolean { if (!Array.isArray(logs)) return false; const validPassStatuses = ["QA_PASSED", "VERIFIED", "PRODUCTION_VERIFIED"]; let foundValidPass = false; for (const log of logs) { if (!log || typeof log !== "object") continue; const entry = log as Record; const status = String(entry.status || "").toUpperCase(); const note = String(entry.note || "").toLowerCase(); // Rejection of invalid QA claims: 404, missing article, not found, post-tsk if ( note.includes("404") || note.includes("مقاله یافت نشد") || note.includes("not found") || note.includes("missing article") || note.includes("post-tsk") ) { return false; } if (validPassStatuses.includes(status)) { foundValidPass = true; } } return foundValidPass; } export function assertTaskCanComplete(task: CompletableTask): void { const rawUrl = task.deliverableUrl?.trim(); if (!rawUrl) { throw new BadRequestException("DONE requires a verifiable deliverable URL"); } const lowerUrl = rawUrl.toLowerCase(); // Reject internal pseudo-protocols if (lowerUrl.startsWith("canina://") || lowerUrl.includes("canina://")) { throw new BadRequestException("Deliverable URL cannot use pseudo-protocol canina://; an independently verifiable HTTP(S) URL is required"); } // Reject git repository pointers or staging branches as production deliverables if (lowerUrl.startsWith("git://") || lowerUrl.includes("git://stage")) { throw new BadRequestException("Deliverable URL cannot be a git staging reference; an independently verifiable HTTP(S) URL is required"); } // Must be an HTTP or HTTPS URL if (!lowerUrl.startsWith("http://") && !lowerUrl.startsWith("https://")) { throw new BadRequestException("DONE requires an independently verifiable HTTP or HTTPS deliverable URL"); } // Reject deleted or placeholder post-tsk articles if (/post[-_]?tsk/i.test(rawUrl)) { throw new BadRequestException("Deliverable URL references an unverified or deleted post-tsk placeholder article"); } if (task.title && /post[-_]?tsk/i.test(task.title)) { throw new BadRequestException("Task references an unverified or deleted post-tsk article"); } if (task.code && /post[-_]?tsk/i.test(task.code)) { throw new BadRequestException("Task references an unverified or deleted post-tsk code"); } if (!hasAcceptanceCriteria(task.acceptanceCriteria)) { throw new BadRequestException("DONE requires explicit acceptance criteria"); } if (!hasValidQaEvidence(task.executionLogs)) { throw new BadRequestException("DONE requires QA/production verification evidence"); } }