prompt-capability-optimizer 1.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (56) hide show
  1. package/LICENSE +21 -0
  2. package/README.md +165 -0
  3. package/SKILL.md +275 -0
  4. package/adapters/environment_adapters.md +94 -0
  5. package/adapters/host_capabilities.json +135 -0
  6. package/bin/cli.js +33 -0
  7. package/index.js +54 -0
  8. package/package.json +51 -0
  9. package/prompt_capability_optimizer/__init__.py +35 -0
  10. package/prompt_capability_optimizer/__main__.py +7 -0
  11. package/prompt_capability_optimizer/adapters/__init__.py +21 -0
  12. package/prompt_capability_optimizer/adapters/agent_adapters.py +281 -0
  13. package/prompt_capability_optimizer/adapters/host_adapter.py +48 -0
  14. package/prompt_capability_optimizer/capabilities/__init__.py +7 -0
  15. package/prompt_capability_optimizer/capabilities/extractor.py +92 -0
  16. package/prompt_capability_optimizer/capabilities/graph.py +53 -0
  17. package/prompt_capability_optimizer/classification/__init__.py +6 -0
  18. package/prompt_capability_optimizer/classification/task_classifier.py +126 -0
  19. package/prompt_capability_optimizer/cli.py +85 -0
  20. package/prompt_capability_optimizer/config.py +42 -0
  21. package/prompt_capability_optimizer/critique/__init__.py +6 -0
  22. package/prompt_capability_optimizer/critique/self_critique_engine.py +144 -0
  23. package/prompt_capability_optimizer/discovery/__init__.py +16 -0
  24. package/prompt_capability_optimizer/discovery/find_skills_adapter.py +143 -0
  25. package/prompt_capability_optimizer/discovery/local_discovery.py +114 -0
  26. package/prompt_capability_optimizer/discovery/mcp_discovery.py +145 -0
  27. package/prompt_capability_optimizer/discovery/registry.py +52 -0
  28. package/prompt_capability_optimizer/discovery/web_discovery.py +157 -0
  29. package/prompt_capability_optimizer/engine.py +201 -0
  30. package/prompt_capability_optimizer/intent/__init__.py +6 -0
  31. package/prompt_capability_optimizer/intent/intent_analyzer.py +61 -0
  32. package/prompt_capability_optimizer/models.py +162 -0
  33. package/prompt_capability_optimizer/optimization/__init__.py +8 -0
  34. package/prompt_capability_optimizer/optimization/execution_pass.py +52 -0
  35. package/prompt_capability_optimizer/optimization/optimizer.py +85 -0
  36. package/prompt_capability_optimizer/optimization/semantic_pass.py +113 -0
  37. package/prompt_capability_optimizer/scoring/__init__.py +7 -0
  38. package/prompt_capability_optimizer/scoring/deduplicator.py +46 -0
  39. package/prompt_capability_optimizer/scoring/scoring_engine.py +34 -0
  40. package/prompt_capability_optimizer/security/__init__.py +14 -0
  41. package/prompt_capability_optimizer/security/governance.py +41 -0
  42. package/prompt_capability_optimizer/security/injection_detector.py +60 -0
  43. package/prompt_capability_optimizer/security/secret_protector.py +61 -0
  44. package/prompt_capability_optimizer/security/trust_engine.py +96 -0
  45. package/prompt_capability_optimizer/verification/__init__.py +6 -0
  46. package/prompt_capability_optimizer/verification/verification_engine.py +101 -0
  47. package/references/capability_graph.md +83 -0
  48. package/references/cross_agent_matrix.md +62 -0
  49. package/references/prompt_engineering_standards.md +90 -0
  50. package/references/scoring_rubric.md +49 -0
  51. package/references/security_and_trust.md +48 -0
  52. package/scripts/capability_checker.py +88 -0
  53. package/scripts/prompt_optimizer_engine.py +41 -0
  54. package/templates/execution_plan_template.md +51 -0
  55. package/templates/optimized_prompt_template.md +55 -0
  56. package/templates/verification_matrix_template.md +26 -0
@@ -0,0 +1,92 @@
1
+ # Copyright (c) 2026 Mahmoud Abdelhameid (Develper.net@gmail.com). All rights reserved.
2
+ # Licensed under the MIT License.
3
+
4
+ """
5
+ Capability Extractor
6
+ ====================
7
+ Extracts technical capability requirements from task descriptions using an
8
+ engineering domain ontology.
9
+ """
10
+
11
+ import re
12
+ from typing import List, Set, Dict
13
+ from ..models import Capability
14
+
15
+ class CapabilityExtractor:
16
+
17
+ # Domain ontology mappings: pattern -> (canonical_capability, domain, dependencies)
18
+ ONTOLOGY = {
19
+ # Backend & APIs
20
+ r"\bnestjs\b": ("nestjs-development", "backend", ["typescript", "api-design"]),
21
+ r"\brest\s+api\b": ("rest-api-design", "backend", ["http-protocol"]),
22
+ r"\bgraphql\b": ("graphql-api", "backend", ["schema-design"]),
23
+ r"\bgrpc\b": ("grpc-services", "backend", ["protobuf"]),
24
+ r"\bfastify\b": ("fastify-framework", "backend", ["nodejs"]),
25
+ r"\bexpress\b": ("express-framework", "backend", ["nodejs"]),
26
+
27
+ # Frontend & UI
28
+ r"\breact\b": ("react-development", "frontend", ["javascript", "ui-component-design"]),
29
+ r"\bplaywright\b": ("playwright-testing", "testing", ["e2e-testing", "browser-automation"]),
30
+ r"\bvue\b": ("vue-development", "frontend", ["ui-component-design"]),
31
+ r"\bnext\.?js\b": ("nextjs-framework", "fullstack", ["react-development"]),
32
+ r"\btailwind\b": ("tailwind-css", "frontend", ["css-styling"]),
33
+
34
+ # Security & Auth
35
+ r"\b(?:auth|authentication)\b": ("authentication-architecture", "security", ["credential-handling", "session-security"]),
36
+ r"\boauth(?:2(?:\.0)?)?\b": ("oauth-oidc-integration", "security", ["authentication-architecture", "token-management"]),
37
+ r"\bjwt\b": ("jwt-token-management", "security", ["cryptography", "token-security"]),
38
+ r"\brbac\b": ("role-based-access-control", "security", ["authorization-logic"]),
39
+ r"\brate\s+limit(?:ing)?\b": ("rate-limiting", "security", ["dos-prevention"]),
40
+ r"\b(?:audit|security\s+audit)\b": ("security-auditing", "security", ["vulnerability-assessment", "owasp-asvs"]),
41
+ r"\bargon2\b": ("argon2-password-hashing", "security", ["cryptographic-storage"]),
42
+ r"\bbcrypt\b": ("bcrypt-password-hashing", "security", ["cryptographic-storage"]),
43
+
44
+ # Data & Storage
45
+ r"\bpostgres(?:ql)?\b": ("postgresql-database", "database", ["relational-modeling", "sql-optimization"]),
46
+ r"\bredis\b": ("redis-caching-streaming", "database", ["in-memory-caching"]),
47
+ r"\bprisma\b": ("prisma-orm", "database", ["database-migrations", "type-safe-querying"]),
48
+ r"\btypeorm\b": ("typeorm", "database", ["database-migrations"]),
49
+ r"\bmongo(?:db)?\b": ("mongodb-database", "database", ["document-modeling"]),
50
+
51
+ # Testing & QA
52
+ r"\b(?:testing|tests?)\b": ("automated-testing", "quality", ["test-runner"]),
53
+ r"\bunit\s+tests?\b": ("unit-testing", "quality", ["mocking", "assertion-framework"]),
54
+ r"\be2e\s+tests?\b": ("e2e-testing", "quality", ["integration-testing"]),
55
+ r"\bjest\b": ("jest-framework", "quality", ["unit-testing"]),
56
+ r"\bpytest\b": ("pytest-framework", "quality", ["python-testing"]),
57
+ r"\bvitest\b": ("vitest-framework", "quality", ["unit-testing"]),
58
+
59
+ # Architecture & Systems
60
+ r"\bmulti-tenant\b": ("multi-tenant-architecture", "architecture", ["data-isolation", "tenant-scoping"]),
61
+ r"\bsaas\b": ("saas-architecture", "architecture", ["billing-integration", "multi-tenant-architecture"]),
62
+ r"\brag\b": ("rag-architecture", "ai_ml", ["vector-search", "document-retrieval", "embeddings"]),
63
+ r"\bmemory\s+leak\b": ("memory-leak-diagnostics", "performance", ["heap-profiling", "v8-diagnostics"]),
64
+ r"\brefactor(?:ing)?\b": ("architectural-refactoring", "architecture", ["dependency-inversion", "code-smells"])
65
+ }
66
+
67
+ @classmethod
68
+ def extract_capabilities(cls, text: str) -> List[Capability]:
69
+ lower_text = text.lower()
70
+ extracted: Dict[str, Capability] = {}
71
+
72
+ for pattern, (name, domain, deps) in cls.ONTOLOGY.items():
73
+ if re.search(pattern, lower_text):
74
+ extracted[name] = Capability(
75
+ name=name,
76
+ domain=domain,
77
+ importance=0.9,
78
+ dependencies=deps,
79
+ description=f"Requires specialized knowledge in {name}"
80
+ )
81
+
82
+ # If no domain capability matched, provide baseline software engineering capability
83
+ if not extracted:
84
+ extracted["software-engineering-fundamentals"] = Capability(
85
+ name="software-engineering-fundamentals",
86
+ domain="general",
87
+ importance=0.5,
88
+ dependencies=[],
89
+ description="Core software development and reasoning skills"
90
+ )
91
+
92
+ return list(extracted.values())
@@ -0,0 +1,53 @@
1
+ # Copyright (c) 2026 Mahmoud Abdelhameid (Develper.net@gmail.com). All rights reserved.
2
+ # Licensed under the MIT License.
3
+
4
+ """
5
+ Capability Graph & Dependency Resolver
6
+ ======================================
7
+ Builds dependency trees from required capabilities and resolves prerequisite nodes.
8
+ """
9
+
10
+ from typing import List, Dict, Set, Any
11
+ from ..models import Capability
12
+
13
+ class CapabilityGraph:
14
+
15
+ def __init__(self, roots: List[Capability]):
16
+ self.nodes: Dict[str, Capability] = {c.name: c for c in roots}
17
+ self._expand_dependencies()
18
+
19
+ def _expand_dependencies(self):
20
+ to_process = list(self.nodes.values())
21
+ while to_process:
22
+ current = to_process.pop(0)
23
+ for dep_name in current.dependencies:
24
+ if dep_name not in self.nodes:
25
+ new_node = Capability(
26
+ name=dep_name,
27
+ domain=current.domain,
28
+ importance=max(0.4, current.importance * 0.75),
29
+ dependencies=[]
30
+ )
31
+ self.nodes[dep_name] = new_node
32
+ to_process.append(new_node)
33
+
34
+ def get_all_capabilities(self) -> List[Capability]:
35
+ # Return sorted by importance descending
36
+ return sorted(self.nodes.values(), key=lambda c: c.importance, reverse=True)
37
+
38
+ def get_capability_names(self) -> List[str]:
39
+ return [c.name for c in self.get_all_capabilities()]
40
+
41
+ def to_dict(self) -> Dict[str, Any]:
42
+ return {
43
+ "node_count": len(self.nodes),
44
+ "nodes": [
45
+ {
46
+ "name": c.name,
47
+ "domain": c.domain,
48
+ "importance": c.importance,
49
+ "dependencies": c.dependencies
50
+ }
51
+ for c in self.get_all_capabilities()
52
+ ]
53
+ }
@@ -0,0 +1,6 @@
1
+ # Copyright (c) 2026 Mahmoud Abdelhameid (Develper.net@gmail.com). All rights reserved.
2
+ # Licensed under the MIT License.
3
+
4
+ from .task_classifier import TaskClassifier
5
+
6
+ __all__ = ["TaskClassifier"]
@@ -0,0 +1,126 @@
1
+ # Copyright (c) 2026 Mahmoud Abdelhameid (Develper.net@gmail.com). All rights reserved.
2
+ # Licensed under the MIT License.
3
+
4
+ """
5
+ Hybrid Task Classifier
6
+ ======================
7
+ Combines structural signals, risk level, multi-system count, and architectural scope
8
+ with an explainable confidence report.
9
+ """
10
+
11
+ import re
12
+ from typing import List, Dict, Any, Tuple
13
+ from ..models import ClassificationReport
14
+
15
+ class TaskClassifier:
16
+
17
+ LEVEL_4_PATTERNS = [
18
+ r"\b(?:saas|multi-tenant|distributed|microservices|consensus|event-driven|cqrs|high-availability|rag\s+(?:architecture|evaluation|system)|enterprise\s+architecture)\b",
19
+ r"\b(?:multi-system|cross-service|data\s+pipeline|warehouse|lakehouse)\b"
20
+ ]
21
+
22
+ LEVEL_3_PATTERNS = [
23
+ r"\b(?:auth|authentication|oauth|oidc|jwt|saml|sso|argon2|bcrypt|cryptography|session\s+management)\b",
24
+ r"\b(?:payment|stripe|billing|webhook\s+security|audit\s+log|penetration\s+test|vulnerability|owasp|asvs)\b",
25
+ r"\b(?:production|compliance|secret\s+management|rbac|abac|rate\s+limiting|ddos)\b"
26
+ ]
27
+
28
+ LEVEL_2_PATTERNS = [
29
+ r"\b(?:rest\s+api|graphql|grpc|api|endpoint|crud|database\s+schema|migration|prisma|typeorm|drizzle|postgres|redis)\b",
30
+ r"\b(?:refactor|architecture|module|service|repository\s+pattern|clean\s+architecture|feature)\b",
31
+ r"\b(?:memory\s+leak|profiling|concurrency|race\s+condition|deadlock|performance\s+optimization)\b"
32
+ ]
33
+
34
+ LEVEL_1_PATTERNS = [
35
+ r"\b(?:fix\s+bug|write\s+test|unit\s+test|add\s+method|function|format|lint|typecheck|single\s+file)\b",
36
+ r"\b(?:regex|helper|utility|script|component|button|form)\b"
37
+ ]
38
+
39
+ LEVEL_0_PATTERNS = [
40
+ r"^(?:explain|what\s+is|how\s+does|why\s+does|describe|translate|summarize|definition\s+of)\b",
41
+ r"\b(?:explain\s+this|walkthrough|overview)\b"
42
+ ]
43
+
44
+ @classmethod
45
+ def classify(cls, prompt: str) -> ClassificationReport:
46
+ lower_prompt = prompt.lower().strip()
47
+ signals = []
48
+
49
+ # Check Level 4 (Research / Multi-System / Enterprise SaaS)
50
+ l4_matches = [m.group(0) for pat in cls.LEVEL_4_PATTERNS for m in re.finditer(pat, lower_prompt)]
51
+ if l4_matches:
52
+ signals.extend(l4_matches)
53
+ return ClassificationReport(
54
+ level=4,
55
+ confidence=0.92,
56
+ signals=signals,
57
+ reasoning=f"Identified high-level multi-system architectural signals: {', '.join(set(signals))}"
58
+ )
59
+
60
+ # Check Level 3 (Production / High-Risk / Security & Payment)
61
+ l3_matches = [m.group(0) for pat in cls.LEVEL_3_PATTERNS for m in re.finditer(pat, lower_prompt)]
62
+ if l3_matches:
63
+ signals.extend(l3_matches)
64
+ return ClassificationReport(
65
+ level=3,
66
+ confidence=0.88,
67
+ signals=signals,
68
+ reasoning=f"Identified security, financial, or high-risk production vectors: {', '.join(set(signals))}"
69
+ )
70
+
71
+ # Check Level 2 (Complex / Multi-File Feature / API / Refactor)
72
+ l2_matches = [m.group(0) for pat in cls.LEVEL_2_PATTERNS for m in re.finditer(pat, lower_prompt)]
73
+ if l2_matches:
74
+ signals.extend(l2_matches)
75
+ return ClassificationReport(
76
+ level=2,
77
+ confidence=0.85,
78
+ signals=signals,
79
+ reasoning=f"Identified feature implementation, API design, or multi-file refactoring: {', '.join(set(signals))}"
80
+ )
81
+
82
+ # Check Level 0 (Informational / Explanation)
83
+ l0_matches = [m.group(0) for pat in cls.LEVEL_0_PATTERNS for m in re.finditer(pat, lower_prompt)]
84
+ if l0_matches and len(lower_prompt.split()) <= 15:
85
+ signals.extend(l0_matches)
86
+ return ClassificationReport(
87
+ level=0,
88
+ confidence=0.95,
89
+ signals=signals,
90
+ reasoning=f"Identified brief informational or conceptual query: {', '.join(set(signals))}"
91
+ )
92
+
93
+ # Check Level 1 (Moderate / Local Fix / Unit Test)
94
+ l1_matches = [m.group(0) for pat in cls.LEVEL_1_PATTERNS for m in re.finditer(pat, lower_prompt)]
95
+ if l1_matches:
96
+ signals.extend(l1_matches)
97
+ return ClassificationReport(
98
+ level=1,
99
+ confidence=0.80,
100
+ signals=signals,
101
+ reasoning=f"Identified single-module edit or standard unit task: {', '.join(set(signals))}"
102
+ )
103
+
104
+ # Fallback based on length and sentence structure
105
+ word_count = len(lower_prompt.split())
106
+ if word_count > 40:
107
+ return ClassificationReport(
108
+ level=2,
109
+ confidence=0.65,
110
+ signals=["long_prompt_heuristic"],
111
+ reasoning="Classified as Level 2 due to extensive prompt scope and specification density."
112
+ )
113
+ elif word_count < 8:
114
+ return ClassificationReport(
115
+ level=0,
116
+ confidence=0.70,
117
+ signals=["short_prompt_heuristic"],
118
+ reasoning="Classified as Level 0 due to concise informational phrasing."
119
+ )
120
+ else:
121
+ return ClassificationReport(
122
+ level=1,
123
+ confidence=0.60,
124
+ signals=["standard_task_default"],
125
+ reasoning="Classified as Level 1 standard engineering task by default."
126
+ )
@@ -0,0 +1,85 @@
1
+ """
2
+ Unified Command Line Interface
3
+ ==============================
4
+ Provides standard CLI commands for prompt optimization, environment probing,
5
+ and controlled Mode C execution governance.
6
+ Usage:
7
+ python -m prompt_capability_optimizer optimize "Build a NestJS auth API" --mode B --json
8
+ python -m prompt_capability_optimizer optimize "Build a NestJS auth API" --mode C --confirm-execute
9
+ python -m prompt_capability_optimizer probe
10
+ """
11
+
12
+ import sys
13
+ import json
14
+ import argparse
15
+ from .engine import PromptOptimizerEngine
16
+ from .discovery.local_discovery import LocalSkillDiscovery
17
+ from .discovery.mcp_discovery import McpDiscovery
18
+ from .adapters.host_adapter import detect_host_runtime
19
+
20
+ def main():
21
+ parser = argparse.ArgumentParser(
22
+ prog="prompt-capability-optimizer",
23
+ description="Autonomous Capability Discovery & Two-Pass Prompt Engineering Engine",
24
+ epilog="Author: Mahmoud Abdelhameid (https://www.linkedin.com/in/mahmoud-abdelhameid-dev/ | Develper.net@gmail.com) | Copyright (c) 2026 Mahmoud Abdelhameid. All rights reserved."
25
+ )
26
+ parser.add_argument("--version", action="version", version="%(prog)s 1.0.0 by Mahmoud Abdelhameid <Develper.net@gmail.com>")
27
+ subparsers = parser.add_subparsers(dest="command", help="Available subcommands")
28
+
29
+ # optimize subcommand
30
+ opt_parser = subparsers.add_parser("optimize", help="Optimize a raw prompt")
31
+ opt_parser.add_argument("prompt", type=str, help="The prompt text to optimize")
32
+ opt_parser.add_argument("--mode", type=str, choices=["A", "B", "C"], default="B", help="Output mode (A: optimize only, B: prepare, C: execute)")
33
+ opt_parser.add_argument("--confirm-execute", action="store_true", help="Explicit human authorization for Mode C execution side-effects")
34
+ opt_parser.add_argument("--json", action="store_true", help="Output machine-readable JSON")
35
+
36
+ # probe subcommand
37
+ probe_parser = subparsers.add_parser("probe", help="Probe host capabilities and discovered skills")
38
+ probe_parser.add_argument("--json", action="store_true", help="Output machine-readable JSON")
39
+
40
+ args = parser.parse_args()
41
+
42
+ if args.command == "optimize":
43
+ engine = PromptOptimizerEngine()
44
+ result = engine.optimize(
45
+ args.prompt,
46
+ mode=args.mode,
47
+ confirmed_execution=args.confirm_execute
48
+ )
49
+ if args.json:
50
+ print(json.dumps(result, indent=2))
51
+ else:
52
+ print(f"=== Prompt Capability Optimizer (Mode {result['mode']}) ===")
53
+ print(f"Classification Level: {result['classification']['level']} ({result['classification']['reasoning']})")
54
+ print(f"Discovered Capabilities: {', '.join(result['required_capabilities'])}")
55
+ print(f"Critique Pass: {result['critique']['passed']} (Score: {result['critique']['score']})")
56
+ if result.get("execution_status"):
57
+ print(f"Execution Governance: {result['execution_status']}\n")
58
+ print("--- OPTIMIZED PROMPT ---")
59
+ print(result["optimized_prompt"])
60
+
61
+ elif args.command == "probe":
62
+ host_agent = detect_host_runtime()
63
+ skills = LocalSkillDiscovery.discover()
64
+ mcp_servers = McpDiscovery.discover()
65
+
66
+ rep = {
67
+ "host_agent": host_agent,
68
+ "local_skills_count": len(skills),
69
+ "local_skills": [{"name": s.name, "source": s.source} for s in skills],
70
+ "mcp_servers_count": len(mcp_servers),
71
+ "mcp_servers": [
72
+ {
73
+ "name": m.name,
74
+ "status": m.metadata.get("status"),
75
+ "tools": m.metadata.get("tools", [])
76
+ }
77
+ for m in mcp_servers
78
+ ]
79
+ }
80
+ print(json.dumps(rep, indent=2))
81
+ else:
82
+ parser.print_help()
83
+
84
+ if __name__ == "__main__":
85
+ main()
@@ -0,0 +1,42 @@
1
+ # Copyright (c) 2026 Mahmoud Abdelhameid (Develper.net@gmail.com). All rights reserved.
2
+ # Licensed under the MIT License.
3
+
4
+ """
5
+ Configuration & Threshold Settings
6
+ ==================================
7
+ Central runtime parameters, utility thresholds, discovery limits, and risk tolerances.
8
+ """
9
+
10
+ from dataclasses import dataclass, field
11
+ from typing import List, Dict, Any
12
+
13
+ @dataclass
14
+ class OptimizerConfig:
15
+ # Utility thresholds from references/scoring_rubric.md
16
+ utility_auto_adopt_threshold: float = 7.0
17
+ utility_conditional_threshold: float = 5.0
18
+
19
+ # Discovery limits
20
+ max_skills_per_prompt: int = 3
21
+ max_skills_level_4: int = 5
22
+ enable_web_discovery: bool = True
23
+ enable_find_skills_cli: bool = True
24
+
25
+ # Security parameters
26
+ block_high_risk_skills: bool = True
27
+ max_acceptable_skill_risk: float = 6.0
28
+ redact_secrets: bool = True
29
+
30
+ # Critique parameters
31
+ minimum_critique_pass_score: float = 0.80
32
+ max_correction_iterations: int = 2
33
+
34
+ # Execution permissions
35
+ allowed_side_effects: List[str] = field(default_factory=lambda: ["NO_SIDE_EFFECT", "LOW_RISK"])
36
+ require_confirmation_for_external_side_effects: bool = True
37
+
38
+ # Cache settings
39
+ enable_cache: bool = True
40
+ cache_ttl_seconds: int = 3600
41
+
42
+ DEFAULT_CONFIG = OptimizerConfig()
@@ -0,0 +1,6 @@
1
+ # Copyright (c) 2026 Mahmoud Abdelhameid (Develper.net@gmail.com). All rights reserved.
2
+ # Licensed under the MIT License.
3
+
4
+ from .self_critique_engine import SelfCritiqueEngine
5
+
6
+ __all__ = ["SelfCritiqueEngine"]
@@ -0,0 +1,144 @@
1
+ # Copyright (c) 2026 Mahmoud Abdelhameid (Develper.net@gmail.com). All rights reserved.
2
+ # Licensed under the MIT License.
3
+
4
+ """
5
+ Semantic Self-Critique & Quality Assurance Engine
6
+ =================================================
7
+ Performs deep qualitative and structural evaluation of prompt content.
8
+ Detects superficial/vague objectives, evaluates constraint relevance, identifies
9
+ contradictions, and outputs structured findings with actionable recommendations.
10
+ """
11
+
12
+ import re
13
+ from typing import Dict, Any, List
14
+ from ..models import CritiqueReport, CritiqueFinding
15
+
16
+ class SelfCritiqueEngine:
17
+
18
+ # Patterns indicating underspecified or excessively vague requests
19
+ VAGUE_OBJECTIVE_PATTERNS = [
20
+ r"^(?:do\s+(?:something|coding|work)|write\s+code|fix\s+it|help\s+me|check\s+this)\b",
21
+ r"^(?:make\s+it\s+work|improve\s+stuff|test\s+everything)$"
22
+ ]
23
+
24
+ @classmethod
25
+ def evaluate(cls, prompt_text: str, depth: int = 1) -> CritiqueReport:
26
+ lower = prompt_text.lower()
27
+ findings: List[CritiqueFinding] = []
28
+ critical_issues: List[str] = []
29
+ recommendations: List[str] = []
30
+
31
+ # 1. Objective Specificity & Vagueness Check
32
+ has_objective_header = bool(re.search(r"\bobjective:", lower))
33
+ # Extract objective text
34
+ obj_match = re.search(r"\bobjective:\s*([^\n\r]+)", prompt_text, re.IGNORECASE)
35
+ obj_text = (obj_match.group(1).strip().lower()) if obj_match else ""
36
+
37
+ is_too_vague = any(re.search(pat, obj_text) for pat in cls.VAGUE_OBJECTIVE_PATTERNS) or (has_objective_header and len(obj_text.split()) < 3 and obj_text not in ["explain this javascript function."])
38
+
39
+ obj_passed = has_objective_header and not is_too_vague
40
+ if is_too_vague:
41
+ critical_issues.append("Objective is excessively vague or superficial (e.g. 'do something')")
42
+
43
+ findings.append(CritiqueFinding(
44
+ dimension="Objective Specificity",
45
+ passed=obj_passed,
46
+ score=1.0 if obj_passed else (0.2 if has_objective_header else 0.0),
47
+ finding="Specific, measurable objective" if obj_passed else "Objective is missing or excessively vague",
48
+ recommendation="State an unambiguous, concrete outcome rather than generic phrasing" if not obj_passed else ""
49
+ ))
50
+
51
+ # 2. Appropriate Persona / Role
52
+ has_role = bool(re.search(r"\brole:", lower))
53
+ role_score = 1.0 if has_role else 0.0
54
+ findings.append(CritiqueFinding(
55
+ dimension="Role Persona",
56
+ passed=has_role,
57
+ score=role_score,
58
+ finding="Specialized engineering role specified" if has_role else "Missing explicit engineering role",
59
+ recommendation="Specify an appropriate engineering persona" if not has_role else ""
60
+ ))
61
+
62
+ # 3. Explicit & Negative Constraints
63
+ has_constraints = bool(re.search(r"\bconstraints?(?:\s+&\s+non-negotiables)?:", lower))
64
+ has_negatives = bool(re.search(r"\b(?:do\s+not|never|avoid|without)\b", lower))
65
+ constraint_passed = has_constraints and has_negatives
66
+ findings.append(CritiqueFinding(
67
+ dimension="Constraint Completeness",
68
+ passed=constraint_passed,
69
+ score=1.0 if constraint_passed else (0.5 if has_constraints else 0.0),
70
+ finding="Both positive and negative constraints present" if constraint_passed else "Missing explicit boundaries or negative constraints",
71
+ recommendation="Include clear non-negotiables and explicit negative constraints" if not constraint_passed else ""
72
+ ))
73
+
74
+ # 4. Tool & Capability Binding (Required for depth >= 2)
75
+ has_tools = bool(re.search(r"\b(?:required\s+capabilities|tools?\s+to\s+use|toolchain)\b.*?:", lower)) or (depth < 2)
76
+ findings.append(CritiqueFinding(
77
+ dimension="Capability & Tool Binding",
78
+ passed=has_tools,
79
+ score=1.0 if has_tools else 0.0,
80
+ finding="Capabilities and execution tools bound" if has_tools else "Complex task lacks explicit tool/capability bindings",
81
+ recommendation="Bind discovered skills, tools, or MCP servers explicitly" if not has_tools else ""
82
+ ))
83
+
84
+ # 5. Verification & Testing Directives
85
+ has_verification = bool(re.search(r"\b(?:verification|testing|test\s+directives?)\b.*?:", lower)) and bool(re.search(r"\b(?:test|lint|typecheck|build|pytest|npm|cargo|go)\b", lower))
86
+ findings.append(CritiqueFinding(
87
+ dimension="Verification Quality",
88
+ passed=has_verification,
89
+ score=1.0 if has_verification else 0.0,
90
+ finding="Concrete verifiable test/inspection commands present" if has_verification else "Missing actionable test or verification commands",
91
+ recommendation="Add concrete testing commands (e.g. npm test, pytest, cargo test)" if not has_verification else ""
92
+ ))
93
+
94
+ # 6. Completion Criteria & Baseline Regression Check
95
+ has_completion = bool(re.search(r"\bcompletion\s+criteria\b.*?:", lower))
96
+ findings.append(CritiqueFinding(
97
+ dimension="Completion Criteria",
98
+ passed=has_completion,
99
+ score=1.0 if has_completion else 0.0,
100
+ finding="Deterministic completion criteria specified" if has_completion else "Missing explicit completion conditions",
101
+ recommendation="Define objective completion conditions and regression bounds" if not has_completion else ""
102
+ ))
103
+
104
+ # 7. Phased Execution for Complex Tasks
105
+ has_phases = bool(re.search(r"\b(?:phase\s+1|phased\s+execution)\b.*?:", lower)) or (depth < 2)
106
+ findings.append(CritiqueFinding(
107
+ dimension="Phased Execution",
108
+ passed=has_phases,
109
+ score=1.0 if has_phases else 0.0,
110
+ finding="Execution divided into sequential milestones" if has_phases else "Complex task lacks phased execution milestones",
111
+ recommendation="Structure complex workflows into sequential phases" if not has_phases else ""
112
+ ))
113
+
114
+ # 8. Conversational Noise & Filler Absence
115
+ has_filler = bool(re.search(r"\b(?:sure(?:ly)?|as\s+an\s+ai|hello|hope\s+this\s+helps)\b", lower))
116
+ findings.append(CritiqueFinding(
117
+ dimension="Formatting & Signal-to-Noise",
118
+ passed=not has_filler,
119
+ score=1.0 if not has_filler else 0.4,
120
+ finding="High signal-to-noise ratio" if not has_filler else "Contains unnecessary conversational filler",
121
+ recommendation="Eliminate conversational pleasantries and filler text" if has_filler else ""
122
+ ))
123
+
124
+ # Composite score calculation
125
+ total_score = sum(f.score for f in findings) / float(len(findings))
126
+ composite_score = round(total_score, 2)
127
+
128
+ # Stricter acceptance gate: No critical issues and score >= 0.80
129
+ all_passed = (composite_score >= 0.80) and (len(critical_issues) == 0) and obj_passed and has_constraints
130
+
131
+ for f in findings:
132
+ if not f.passed and f.recommendation:
133
+ recommendations.append(f.recommendation)
134
+
135
+ confidence = 0.95 if has_objective_header and has_constraints else 0.80
136
+
137
+ return CritiqueReport(
138
+ passed=all_passed,
139
+ score=composite_score,
140
+ confidence=confidence,
141
+ findings=findings,
142
+ critical_issues=critical_issues,
143
+ recommendations=recommendations
144
+ )
@@ -0,0 +1,16 @@
1
+ # Copyright (c) 2026 Mahmoud Abdelhameid (Develper.net@gmail.com). All rights reserved.
2
+ # Licensed under the MIT License.
3
+
4
+ from .registry import ResourceRegistry
5
+ from .local_discovery import LocalSkillDiscovery
6
+ from .find_skills_adapter import FindSkillsAdapter
7
+ from .mcp_discovery import McpDiscovery
8
+ from .web_discovery import WebDiscovery
9
+
10
+ __all__ = [
11
+ "ResourceRegistry",
12
+ "LocalSkillDiscovery",
13
+ "FindSkillsAdapter",
14
+ "McpDiscovery",
15
+ "WebDiscovery"
16
+ ]