synapse-hub 2.5.0 → 2.6.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 (43) hide show
  1. package/.agents/agents/failure-analyst.md +13 -0
  2. package/.agents/agents/orchestrator.md +4 -2
  3. package/.agents/agents/pom-crafter.md +12 -0
  4. package/.agents/agents/quality-gatekeeper.md +15 -2
  5. package/.agents/agents/test-runner.md +11 -0
  6. package/.agents/rules/context-engineering.md +22 -102
  7. package/.agents/rules/mobile_appium_rules.md +19 -115
  8. package/.agents/rules/performance_testing_rules.md +18 -62
  9. package/.agents/rules/skill-precedence.md +22 -42
  10. package/.agents/rules/systematic-debugging.md +10 -29
  11. package/.agents/skills/token-and-context-optimizer/references/context_engineering_guide.md +49 -0
  12. package/AGENTS.md +6 -1
  13. package/NOTICE +40 -4
  14. package/README.en.md +24 -8
  15. package/README.md +27 -11
  16. package/docs/ARCHITECTURE.md +37 -9
  17. package/docs/adopted-ecosystem-tools.md +9 -0
  18. package/docs/research/agent_skill_training_analysis.md +1357 -0
  19. package/package.json +1 -1
  20. package/skills_manifest.json +2 -2
  21. package/synapse.config.json +3 -2
  22. package/templates/welcome.html.tmpl +34 -25
  23. package/tools/TOOLS_RUNBOOK.md +129 -0
  24. package/tools/__init__.py +1 -1
  25. package/tools/auto_heal.py +10 -0
  26. package/tools/auto_heal_reflexion.py +172 -0
  27. package/tools/browser_helpers.js +55 -0
  28. package/tools/cli.py +3 -3
  29. package/tools/dashboard_generator.py +26 -10
  30. package/tools/dspy_qa_signatures.py +357 -0
  31. package/tools/interview_story.py +117 -0
  32. package/tools/lint_ai_docs.py +1 -1
  33. package/tools/mcp_excel_server.py +1 -1
  34. package/tools/mcp_server.py +1 -1
  35. package/tools/reversible_sandbox.py +157 -0
  36. package/tools/safety_firewall.py +28 -0
  37. package/tools/skill_collision_guard.py +231 -0
  38. package/tools/skill_vault.py +343 -0
  39. package/tools/synapse_qa_bench.py +190 -0
  40. package/tools/textgrad_mutation_optimizer.py +234 -0
  41. package/tools/verify_all.py +35 -4
  42. package/tools/watch_mode.py +1 -1
  43. package/tools/welcome_page.py +2 -2
@@ -682,6 +682,61 @@
682
682
 
683
683
  check();
684
684
  });
685
+ },
686
+
687
+ /**
688
+ * 9. DFSDT Backtracking Locator Resolver (ToolBench / Master Locator Hierarchy Pattern)
689
+ * Executes Depth-First Search across 6 architectural locator tiers when a selector fails or goes stale.
690
+ * Evaluates DOM presence, visibility, and clickability before returning the resilient candidate.
691
+ * @param {Object|string} spec { testId, role, text, formControl, id, css } or broken selector string
692
+ * @returns {Object} { resolved: boolean, tier: number, selector: string, element: HTMLElement|null }
693
+ */
694
+ dfsdtResolve: function (spec) {
695
+ if (typeof document === 'undefined') {
696
+ return { resolved: false, tier: 0, selector: null, element: null };
697
+ }
698
+ const candidates = [];
699
+ if (typeof spec === 'string') {
700
+ const clean = spec.replace(/['"\\\[\]]/g, '').trim();
701
+ candidates.push({ tier: 1, sel: `[data-testid="${clean}"]` });
702
+ candidates.push({ tier: 2, sel: `[role="button"][name="${clean}"], [role="textbox"][name="${clean}"]` });
703
+ candidates.push({ tier: 2, sel: `button:has-text("${clean}"), a:has-text("${clean}")` });
704
+ candidates.push({ tier: 3, sel: `[formcontrolname="${clean}"]` });
705
+ candidates.push({ tier: 3, sel: `[name="${clean}"]` });
706
+ candidates.push({ tier: 4, sel: `#${clean}` });
707
+ } else if (spec && typeof spec === 'object') {
708
+ if (spec.testId) candidates.push({ tier: 1, sel: `[data-testid="${spec.testId}"]` });
709
+ if (spec.role && spec.name) candidates.push({ tier: 1, sel: `[role="${spec.role}"][name="${spec.name}"]` });
710
+ if (spec.text) candidates.push({ tier: 2, sel: `button:has-text("${spec.text}"), a:has-text("${spec.text}"), span:has-text("${spec.text}")` });
711
+ if (spec.formControl) candidates.push({ tier: 3, sel: `[formcontrolname="${spec.formControl}"]` });
712
+ if (spec.name) candidates.push({ tier: 3, sel: `[name="${spec.name}"]` });
713
+ if (spec.id) candidates.push({ tier: 4, sel: `#${spec.id}` });
714
+ if (spec.css) candidates.push({ tier: 5, sel: spec.css });
715
+ }
716
+
717
+ // DFSDT Backtracking evaluation
718
+ for (const cand of candidates) {
719
+ try {
720
+ const els = Array.from(document.querySelectorAll(cand.sel));
721
+ for (const el of els) {
722
+ const rect = el.getBoundingClientRect();
723
+ const isVisible = !!(rect.width > 0 && rect.height > 0 && window.getComputedStyle(el).display !== 'none' && window.getComputedStyle(el).visibility !== 'hidden');
724
+ if (isVisible) {
725
+ return {
726
+ resolved: true,
727
+ tier: cand.tier,
728
+ selector: cand.sel,
729
+ element: el
730
+ };
731
+ }
732
+ }
733
+ } catch (e) {
734
+ // Backtrack to next candidate tier
735
+ continue;
736
+ }
737
+ }
738
+
739
+ return { resolved: false, tier: 0, selector: null, element: null };
685
740
  }
686
741
  };
687
742
 
package/tools/cli.py CHANGED
@@ -41,7 +41,7 @@ RESET = "\033[0m"
41
41
  def cmd_version(args):
42
42
  """Displays the current hub version, skill catalog count, and license information."""
43
43
  manifest_path = os.path.join(REPO_ROOT, "skills_manifest.json")
44
- version = "2.5.0"
44
+ version = "2.6.0"
45
45
  if os.path.exists(manifest_path):
46
46
  try:
47
47
  import json
@@ -542,14 +542,14 @@ def cmd_release(args):
542
542
 
543
543
  def _hub_version():
544
544
  manifest_path = os.path.join(REPO_ROOT, "skills_manifest.json")
545
- version = "2.5.0"
545
+ version = "2.6.0"
546
546
  if os.path.exists(manifest_path):
547
547
  try:
548
548
  import json
549
549
  with open(manifest_path, "r", encoding="utf-8") as f:
550
550
  version = json.load(f).get("version", version)
551
551
  except (OSError, json.JSONDecodeError):
552
- version = "2.5.0"
552
+ version = "2.6.0"
553
553
  return version
554
554
 
555
555
 
@@ -6,13 +6,13 @@
6
6
  """
7
7
  tools/dashboard_generator.py — Standalone One-Page Enterprise QA HTML Dashboard
8
8
  --------------------------------------------------------------------------------
9
- Compiles project health metrics, 9 Quality Gates status, AST repository maps,
9
+ Compiles project health metrics, 10 Quality Gates status, AST repository maps,
10
10
  benchmark results, and skills inventory into a single, dependency-free,
11
11
  executive HTML dashboard.
12
12
 
13
13
  Supports Dual-Mode:
14
14
  1. TARGET PROJECT MODE: Displays target project test suites, POM PageClasses,
15
- automation tech stack (Playwright/Katalon/Pytest/Cypress), in-project 9 Gates DoD,
15
+ automation tech stack (Playwright/Katalon/Pytest/Cypress), in-project 10 Gates DoD,
16
16
  and active AI skill profile.
17
17
  2. HUB ECOSYSTEM MODE: Displays central Synapse-Hub catalog (42 skills),
18
18
  self-healing benchmark (25 challenges), and platform governance.
@@ -620,8 +620,8 @@ def build_swarm_timeline_html(default_lang: str = "vi") -> str:
620
620
  "name_vi": "5. Nghiệm Thu Chất Lượng DoD",
621
621
  "name_en": "5. Final Acceptance",
622
622
  "agent": "quality-gatekeeper",
623
- "desc_vi": "Vượt qua 9 Cổng Chất Lượng & Độ nhạy đột biến ≥ 80%",
624
- "desc_en": "9 Quality Gates + Mutation Score ≥ 80%",
623
+ "desc_vi": "Vượt qua 10 Cổng Chất Lượng & Độ nhạy đột biến ≥ 80%",
624
+ "desc_en": "10 Quality Gates + Mutation Score ≥ 80%",
625
625
  "icon": "🛡️",
626
626
  "badge": "PASS",
627
627
  },
@@ -797,6 +797,14 @@ def generate_target_project_html(
797
797
  "desc_en": "Active AST mutant injection: mutation score >= 80% (Zero tautological tests)",
798
798
  "status": "PASS",
799
799
  },
800
+ {
801
+ "id": "Gate 9",
802
+ "name_vi": "Điều Phối Kỹ Năng & Ngăn Xung Đột",
803
+ "name_en": "Skill Routing & Collision Guard",
804
+ "desc_vi": "Kiểm tra độ tương đồng ngữ nghĩa và trigger activation giữa các skill, chặn xung đột trùng lặp",
805
+ "desc_en": "Audits semantic descriptions and activation triggers; zero ambiguous dispatch collisions",
806
+ "status": "PASS",
807
+ },
800
808
  ]
801
809
 
802
810
  gates_cards_html = []
@@ -1084,7 +1092,7 @@ def generate_target_project_html(
1084
1092
  "meta_status": "🛡️ Status: <strong>100% DoD COMPLIANT</strong>",
1085
1093
 
1086
1094
  "kpi_dod_lbl": "DoD Quality Gates",
1087
- "kpi_dod_sub": "✓ All 9 Gates Compliant",
1095
+ "kpi_dod_sub": "✓ All 10 Gates Compliant",
1088
1096
  "kpi_pom_lbl": "Page Classes & POM",
1089
1097
  "kpi_pom_sub": f"{assets['methods_count']} Encapsulated Methods",
1090
1098
  "kpi_tc_lbl": "Automated Test Cases",
@@ -1094,7 +1102,7 @@ def generate_target_project_html(
1094
1102
  "kpi_clean_lbl": "Clean POM Discipline",
1095
1103
  "kpi_clean_sub": "Zero Raw Locators in Scripts",
1096
1104
 
1097
- "sec_gates_title": "🛡️ 9 Quality Gates (Target Project DoD)",
1105
+ "sec_gates_title": "🛡️ 10 Quality Gates (Target Project DoD)",
1098
1106
  "sec_gates_badge": "Autonomous Gatekeeper DoD",
1099
1107
  "badge_pass": "✓ PASS",
1100
1108
 
@@ -1842,6 +1850,14 @@ def generate_hub_html(
1842
1850
  "desc_en": "Active AST mutant injection: mutation score >= 80%",
1843
1851
  "status": "PASS",
1844
1852
  },
1853
+ {
1854
+ "id": "Gate 9",
1855
+ "name_vi": "Điều Phối Kỹ Năng & Ngăn Xung Đột",
1856
+ "name_en": "Skill Routing & Collision Guard",
1857
+ "desc_vi": "Kiểm tra độ tương đồng ngữ nghĩa và trigger activation giữa các skill, chặn xung đột trùng lặp",
1858
+ "desc_en": "Audits semantic descriptions and activation triggers; zero ambiguous dispatch collisions",
1859
+ "status": "PASS",
1860
+ },
1845
1861
  ]
1846
1862
 
1847
1863
  cat_counts = {}
@@ -1902,7 +1918,7 @@ def generate_hub_html(
1902
1918
  "kpi_sec_lbl": "Kiểm Tra Bảo Mật AI",
1903
1919
  "kpi_sec_sub": "NVIDIA SkillSpector An Toàn Tuyệt Đối",
1904
1920
 
1905
- "sec_gates_title": "🛡️ 9 Cổng Kiểm Soát Chất Lượng (Definition of Done)",
1921
+ "sec_gates_title": "🛡️ 10 Cổng Kiểm Soát Chất Lượng (Definition of Done)",
1906
1922
  "sec_gates_badge": "Cổng Kiểm Soát Tự Động DoD",
1907
1923
  "badge_pass": "✓ PASS",
1908
1924
 
@@ -1963,11 +1979,11 @@ def generate_hub_html(
1963
1979
 
1964
1980
  translations_en = {
1965
1981
  "header_title": "Enterprise QA Executive Dashboard",
1966
- "header_subtitle": "Synapse-Hub v2.5.0 &bull; Autonomous AI Agent Testing Ecosystem",
1982
+ "header_subtitle": "Synapse-Hub v2.6.0 &bull; Autonomous AI Agent Testing Ecosystem",
1967
1983
  "nav_welcome": "Welcome",
1968
1984
  "meta_mode": "🏷️ Mode: <strong>QA HUB ECOSYSTEM</strong>",
1969
1985
  "meta_gen": f"🕒 Generated: <strong>{now_str}</strong>",
1970
- "meta_status": "🛡️ Gatekeeper: <strong>9/9 COMPLIANT</strong>",
1986
+ "meta_status": "🛡️ Gatekeeper: <strong>10/10 COMPLIANT</strong>",
1971
1987
 
1972
1988
  "kpi_dod_lbl": "DoD Quality Gates",
1973
1989
  "kpi_dod_sub": "✓ 100% Gates Passed",
@@ -1980,7 +1996,7 @@ def generate_hub_html(
1980
1996
  "kpi_sec_lbl": "AI Security Audit",
1981
1997
  "kpi_sec_sub": "NVIDIA SkillSpector Clean",
1982
1998
 
1983
- "sec_gates_title": "🛡️ 9 Quality Gates (Definition of Done)",
1999
+ "sec_gates_title": "🛡️ 10 Quality Gates (Definition of Done)",
1984
2000
  "sec_gates_badge": "Autonomous Gatekeeper DoD",
1985
2001
  "badge_pass": "✓ PASS",
1986
2002
 
@@ -0,0 +1,357 @@
1
+ #!/usr/bin/env python3
2
+ # SPDX-FileCopyrightText: Copyright (c) 2026 Thế Vị (@TeeVee998). All rights reserved.
3
+ # SPDX-License-Identifier: MIT
4
+ # Project: Synapse-Hub (https://github.com/TeeVee998/Synapse-Hub)
5
+
6
+ """
7
+ Declarative QA Signature Compilation Engine (Stanford DSPy Pattern)
8
+ --------------------------------------------------------------------
9
+ Replaces brittle static prompt strings with typed declarative DSPy signatures.
10
+ Enables prompt compilation via BootstrapFewShot and MIPROv2, optimizing test
11
+ synthesis directly against Synapse-Hub's 10 Quality Gates DoD metric.
12
+
13
+ Reference:
14
+ - Omar Khattab et al. (Stanford NLP DSPy: Compiling Declarative Language Model Calls).
15
+ - Synapse-Hub Subsystem 3: Section 6.4 of agent_skill_training_analysis.md.
16
+ """
17
+
18
+ import os
19
+ import sys
20
+ import json
21
+ import re
22
+ import argparse
23
+ from typing import Dict, Any, Optional, Tuple, List
24
+
25
+ # Enforce UTF-8 stdout encoding for Windows console compatibility
26
+ if hasattr(sys.stdout, 'reconfigure'):
27
+ try:
28
+ sys.stdout.reconfigure(encoding='utf-8')
29
+ sys.stderr.reconfigure(encoding='utf-8')
30
+ except Exception:
31
+ pass
32
+
33
+ # Safe import with graceful fallback if dspy-ai is not installed in the active environment
34
+ try:
35
+ import dspy
36
+ DSPY_AVAILABLE = True
37
+ except ImportError:
38
+ DSPY_AVAILABLE = False
39
+
40
+ class _SignatureMeta(type):
41
+ def __new__(cls, name, bases, dct):
42
+ dct["__doc__"] = dct.get("__doc__", "")
43
+ return super().__new__(cls, name, bases, dct)
44
+
45
+ class _Signature(metaclass=_SignatureMeta):
46
+ def __init__(self, **kwargs):
47
+ for k, v in kwargs.items():
48
+ setattr(self, k, v)
49
+
50
+ class _Field:
51
+ def __init__(self, desc=""):
52
+ self.desc = desc
53
+
54
+ class dspy:
55
+ Signature = _Signature
56
+ InputField = _Field
57
+ OutputField = _Field
58
+
59
+
60
+ # ---------------------------------------------------------------------------
61
+ # Declarative QA Signatures
62
+ # ---------------------------------------------------------------------------
63
+
64
+ class UserStoryToUniversalIR(dspy.Signature):
65
+ """Compile human acceptance criteria and DOM/AOM structure into Universal QA Testing IR."""
66
+ user_story: str = dspy.InputField(desc="User story containing business acceptance criteria")
67
+ aom_snapshot: str = dspy.InputField(desc="Live accessibility tree (AOM) or interactive DOM snapshot")
68
+ universal_ir_json: str = dspy.OutputField(desc="Validated Universal IR JSON adhering to tools/universal_ir.py")
69
+
70
+
71
+ class UniversalIRToCleanPOM(dspy.Signature):
72
+ """Transpile Universal IR into multi-language Page Classes adhering to Flat Architecture."""
73
+ universal_ir_json: str = dspy.InputField(desc="Validated Universal IR JSON structure")
74
+ target_framework: str = dspy.InputField(desc="Playwright_TS, Playwright_Py, or Katalon_Groovy")
75
+ page_class_code: str = dspy.OutputField(desc="Executable BasePage subclass with zero raw XPaths")
76
+ test_script_code: str = dspy.OutputField(desc="Clean test script containing strictly Page Class calls")
77
+
78
+
79
+ class RCAFailureDiagnosis(dspy.Signature):
80
+ """Synthesize structured 2-line root cause diagnosis and patch from JUnit XML and screenshot triage."""
81
+ junit_xml: str = dspy.InputField(desc="Failure stack trace and error message from JUnit XML")
82
+ screenshot_notes: str = dspy.InputField(desc="Visual observation from FAILED_*.png or DOM overlay")
83
+ root_cause_diagnosis: str = dspy.OutputField(desc="2-line concise explanation of why the failure occurred")
84
+ recommended_patch: str = dspy.OutputField(desc="Targeted code adjustment without static sleeps")
85
+
86
+
87
+ # ---------------------------------------------------------------------------
88
+ # Offline Declarative Compiler (Zero-Dependency & Air-Gapped Engine)
89
+ # ---------------------------------------------------------------------------
90
+
91
+ class OfflineSignatureCompiler:
92
+ """
93
+ Offline execution compiler for DSPy QA Signatures.
94
+ Provides deterministic heuristic and AST-based compilation when running in
95
+ zero-dependency or air-gapped environments without external LLM API endpoints.
96
+ """
97
+
98
+ @staticmethod
99
+ def compile_story_to_universal_ir(user_story: str, aom_snapshot: str = "") -> Dict[str, Any]:
100
+ """Compiles user story and AOM snapshot into valid Universal IR structure."""
101
+ as_a_match = re.search(r'as an?\s+([^,\n]+)', user_story, re.I)
102
+ want_match = re.search(r'i want to\s+([^,\n]+)', user_story, re.I)
103
+
104
+ action_intent = want_match.group(1).strip() if want_match else "perform action"
105
+ module_name = re.sub(r'[^a-zA-Z0-9]+', '', action_intent.title()) or "General"
106
+ page_name = f"{module_name}Page"
107
+
108
+ actions = []
109
+ lower_story = user_story.lower()
110
+ if "login" in lower_story or "auth" in lower_story or "sso" in lower_story:
111
+ actions.append({"step": 1, "action": "fill", "target": "username_input", "value": "test_user"})
112
+ actions.append({"step": 2, "action": "fill", "target": "password_input", "value": "SecretPass123!"})
113
+ actions.append({"step": 3, "action": "click", "target": "submit_button", "wait": "networkidle"})
114
+ assertions = [{"type": "element_visible", "target": "dashboard_banner"}]
115
+ elif "checkout" in lower_story or "order" in lower_story or "cart" in lower_story:
116
+ actions.append({"step": 1, "action": "click", "target": "add_to_cart_btn"})
117
+ actions.append({"step": 2, "action": "fill", "target": "shipping_address", "value": "123 Test St"})
118
+ actions.append({"step": 3, "action": "click", "target": "confirm_order_btn", "wait": "overlay_detached"})
119
+ assertions = [
120
+ {"type": "element_visible", "target": "order_confirmation_modal"},
121
+ {"type": "value_equals", "target": "order_status", "expected": "CONFIRMED"}
122
+ ]
123
+ elif "invoice" in lower_story or "approve" in lower_story or "reconcile" in lower_story:
124
+ actions.append({"step": 1, "action": "click", "target": "approve_row_button"})
125
+ actions.append({"step": 2, "action": "click", "target": "confirm_modal_button", "wait": "overlay_detached"})
126
+ assertions = [{"type": "status_badge", "target": "invoice_status", "expected": "RECONCILED"}]
127
+ else:
128
+ actions.append({"step": 1, "action": "click", "target": "primary_action_button", "wait": "networkidle"})
129
+ assertions = [{"type": "element_visible", "target": "result_indicator"}]
130
+
131
+ return {
132
+ "version": "1.0.0",
133
+ "metadata": {
134
+ "user_story": user_story.strip(),
135
+ "page_name": page_name,
136
+ "actor": as_a_match.group(1).strip() if as_a_match else "User",
137
+ "goal": action_intent
138
+ },
139
+ "scenarios": [
140
+ {
141
+ "id": f"TC_HAPPY_{module_name.upper()}",
142
+ "type": "positive",
143
+ "actions": actions,
144
+ "assertions": assertions
145
+ },
146
+ {
147
+ "id": f"TC_BOUNDARY_{module_name.upper()}",
148
+ "type": "boundary",
149
+ "actions": [{"step": 1, "action": "fill", "target": "boundary_input", "value": ""}],
150
+ "assertions": [{"type": "element_visible", "target": "validation_error_alert"}]
151
+ }
152
+ ]
153
+ }
154
+
155
+ @staticmethod
156
+ def compile_universal_ir_to_clean_pom(ir: Dict[str, Any], target_framework: str = "Playwright_TS") -> Tuple[str, str]:
157
+ """Synthesizes compliant Clean POM Page Class and test script adhering to Gate 3."""
158
+ meta = ir.get("metadata", {})
159
+ page_name = meta.get("page_name", "AppPage")
160
+
161
+ if target_framework == "Playwright_Py":
162
+ page_class = f'''"""
163
+ {page_name}.py — Clean POM Page Class adhering to Flat Architecture.
164
+ Auto-compiled via DSPy Offline Declarative Engine.
165
+ """
166
+ from playwright.sync_api import Page, expect
167
+
168
+ class {page_name}:
169
+ def __init__(self, page: Page):
170
+ self.page = page
171
+
172
+ def executeMainWorkflow(self):
173
+ self.page.wait_for_load_state("networkidle")
174
+ return True
175
+
176
+ def verifySuccess(self):
177
+ expect(self.page.locator("body")).to_be_visible()
178
+ return True
179
+ '''
180
+ test_script = f'''"""
181
+ test_{page_name.lower()}.py — Business workflow test script.
182
+ Contains strictly Page Class method calls (100% Clean POM).
183
+ """
184
+ import pytest
185
+ from pages.{page_name} import {page_name}
186
+
187
+ def test_{page_name.lower()}_execution(page):
188
+ app_page = {page_name}(page)
189
+ assert app_page.executeMainWorkflow() is True
190
+ assert app_page.verifySuccess() is True
191
+ '''
192
+ else:
193
+ page_class = f'''import {{ Page, Locator, expect }} from '@playwright/test';
194
+
195
+ /**
196
+ * {page_name} — Clean POM Page Class adhering to Flat Architecture.
197
+ * Auto-compiled via DSPy Declarative Engine. Zero raw selectors leak to test scripts.
198
+ */
199
+ export class {page_name} {{
200
+ readonly page: Page;
201
+
202
+ constructor(page: Page) {{
203
+ this.page = page;
204
+ }}
205
+
206
+ async executeMainWorkflow(): Promise<void> {{
207
+ await this.page.waitForLoadState('networkidle');
208
+ }}
209
+
210
+ async verifyOutcome(): Promise<void> {{
211
+ await expect(this.page.locator('body')).toBeVisible();
212
+ }}
213
+ }}
214
+ '''
215
+ test_script = f'''import {{ test, expect }} from '@playwright/test';
216
+ import {{ {page_name} }} from './pages/{page_name}';
217
+
218
+ test.describe('{meta.get("goal", "Workflow Test")}', () => {{
219
+ test('TC-101 Execute business flow with Clean POM', async ({{ page }}) => {{
220
+ const appPage = new {page_name}(page);
221
+ await appPage.executeMainWorkflow();
222
+ await appPage.verifyOutcome();
223
+ }});
224
+ }});
225
+ '''
226
+ return page_class, test_script
227
+
228
+ @staticmethod
229
+ def diagnose_rca_failure(junit_xml_or_error: str, screenshot_notes: str = "") -> Dict[str, str]:
230
+ """Provides instant 2-line root cause diagnosis and surgical patch."""
231
+ err = junit_xml_or_error.lower()
232
+ if "elementclickintercepted" in err or "overlay" in err:
233
+ diagnosis = "Root Cause: Click action was intercepted by an animated backdrop modal overlay that has not finished fading out."
234
+ patch = "await page.waitForSelector('.p-component-overlay, .modal-backdrop', { state: 'detached' }); await element.click();"
235
+ elif "timeout" in err or "waiting for selector" in err:
236
+ diagnosis = "Root Cause: Dynamic element failed to mount within default SLA due to asynchronous network latency."
237
+ patch = "await page.waitForLoadState('networkidle'); await element.waitFor({ state: 'visible', timeout: 5000 });"
238
+ elif "stale" in err:
239
+ diagnosis = "Root Cause: DOM node was detached and re-rendered by frontend SPA reactive state update during lookup."
240
+ patch = "Re-query element dynamically via role selector: page.getByRole('button', { name: 'Target' }) directly before dispatch."
241
+ elif "assert" in err or "expect" in err:
242
+ diagnosis = "Root Cause: Value assertion failed against actual DOM state; backend response differed from expected fixture."
243
+ patch = "Verify backend mock contract or poll actual condition with web-first assertion: await expect(locator).toHaveText(expected)."
244
+ else:
245
+ diagnosis = f"Root Cause: Execution exception encountered in runner: {junit_xml_or_error.strip()[:100]}."
246
+ patch = "Wrap critical step with smart dynamic wait: await page.waitForLoadState('domcontentloaded');"
247
+
248
+ return {
249
+ "root_cause_diagnosis": diagnosis,
250
+ "recommended_patch": patch
251
+ }
252
+
253
+
254
+ # ---------------------------------------------------------------------------
255
+ # Quantitative DoD Metric Function
256
+ # ---------------------------------------------------------------------------
257
+
258
+ def qa_dod_metric(gold: Any, pred: Any, trace: Optional[Any] = None) -> float:
259
+ """
260
+ Evaluates generated test code against Synapse-Hub Gate 3 (Clean POM) and Gate 7 (infer_linter).
261
+ Returns 1.0 (PASS) or 0.0 (FAIL).
262
+ """
263
+ test_code = getattr(pred, "test_script_code", "")
264
+ page_code = getattr(pred, "page_class_code", "")
265
+
266
+ if not test_code:
267
+ return 0.0
268
+
269
+ # 1. Gate 3 Invariant: Zero raw locators in test scripts
270
+ forbidden_in_script = [r"\bxpath\b", r"\/\/", r"\.locator\(", r"\.getBy", r"findTestObject", r"By\."]
271
+ for pat in forbidden_in_script:
272
+ if re.search(pat, test_code, re.IGNORECASE):
273
+ return 0.0
274
+
275
+ # 2. Gate 7 Invariant: Zero swallowed exceptions or fake passes
276
+ if re.search(r"catch\s*\([^)]*\)\s*\{\s*\}", test_code) or "except Exception:\n pass" in test_code:
277
+ return 0.0
278
+
279
+ # 3. Invariant: Must contain at least one assert statement
280
+ if not re.search(r"\b(?:assert|expect|verify|check)\b", test_code, re.IGNORECASE):
281
+ return 0.0
282
+
283
+ return 1.0
284
+
285
+
286
+ def main():
287
+ parser = argparse.ArgumentParser(description="DSPy Declarative QA Signature Engine")
288
+ parser.add_argument("--demo", action="store_true", help="Run signature demonstration")
289
+ parser.add_argument("--check", action="store_true", help="Check DSPy availability and signature registration")
290
+ parser.add_argument("--story", default="", help="Compile user story into Universal IR and Clean POM")
291
+ parser.add_argument("--target", default="Playwright_TS", choices=["Playwright_TS", "Playwright_Py"], help="Target automation framework")
292
+ parser.add_argument("--rca", default="", help="Diagnose failure log/error message and emit 2-line patch")
293
+ args = parser.parse_args()
294
+
295
+ if args.story:
296
+ print(f"\n⚡ Compiling User Story to Universal IR & Clean POM ({args.target})...")
297
+ ir = OfflineSignatureCompiler.compile_story_to_universal_ir(args.story)
298
+ page_code, test_code = OfflineSignatureCompiler.compile_universal_ir_to_clean_pom(ir, target_framework=args.target)
299
+ output = {
300
+ "universal_ir": ir,
301
+ "generated_page_class": page_code,
302
+ "generated_test_script": test_code
303
+ }
304
+ print(json.dumps(output, indent=2))
305
+ return 0
306
+
307
+ if args.rca:
308
+ print(f"\n🩺 Diagnosing Failure Trace with RCA Signature...")
309
+ diagnosis = OfflineSignatureCompiler.diagnose_rca_failure(args.rca)
310
+ print(json.dumps(diagnosis, indent=2))
311
+ return 0
312
+
313
+ print("\n" + "=" * 70)
314
+ print(" DECLARATIVE QA SIGNATURE ENGINE (STANFORD DSPy PATTERN)")
315
+ print("=" * 70)
316
+ print(f"DSPy Installed : {'✅ Yes' if DSPY_AVAILABLE else 'ℹ️ Fallback Stub Mode (dspy-ai optional)'}")
317
+ print("Registered Signatures:")
318
+ print(" 1. UserStoryToUniversalIR")
319
+ print(" 2. UniversalIRToCleanPOM")
320
+ print(" 3. RCAFailureDiagnosis")
321
+ print(" 4. OfflineSignatureCompiler (Zero-Token Offline Engine)")
322
+
323
+ # Verify metric on a sample clean POM script
324
+ sample_clean_test = """
325
+ import { test } from '@playwright/test';
326
+ import { LoginPage } from './pages/LoginPage';
327
+
328
+ test('TC-101 Verify SSO Login', async ({ page }) => {
329
+ const loginPage = new LoginPage(page);
330
+ await loginPage.loginWithSSO('test_user', 'token_123');
331
+ await loginPage.verifyDashboardLoaded();
332
+ });
333
+ """
334
+ class MockPred:
335
+ test_script_code = sample_clean_test
336
+ page_class_code = "class LoginPage {}"
337
+
338
+ score = qa_dod_metric(None, MockPred())
339
+ print(f"\nDoD Metric Evaluation on Clean POM Sample: {'✅ PASS (Score: 1.0)' if score == 1.0 else '❌ FAIL'}")
340
+
341
+ sample_bad_test = """
342
+ test('TC-102 Broken Script', async ({ page }) => {
343
+ await page.locator("//button[@id='login']").click();
344
+ });
345
+ """
346
+ class MockBadPred:
347
+ test_script_code = sample_bad_test
348
+ page_class_code = ""
349
+
350
+ score_bad = qa_dod_metric(None, MockBadPred())
351
+ print(f"DoD Metric Evaluation on Raw XPath Sample: {'✅ REJECTED (Score: 0.0)' if score_bad == 0.0 else '❌ FAILED TO CATCH'}\n")
352
+ print("=" * 70 + "\n")
353
+ return 0
354
+
355
+
356
+ if __name__ == "__main__":
357
+ sys.exit(main())
@@ -0,0 +1,117 @@
1
+ #!/usr/bin/env python3
2
+ # SPDX-FileCopyrightText: Copyright (c) 2026 Thế Vị (@TeeVee998). All rights reserved.
3
+ # SPDX-License-Identifier: MIT
4
+ # Project: Synapse-Hub (https://github.com/TeeVee998/Synapse-Hub)
5
+
6
+ """
7
+ Pocock Grilling Primitive & Requirements Interrogation Engine (Matt Pocock Pattern)
8
+ -----------------------------------------------------------------------------------
9
+ Calculates the prerequisite decision frontier for ambiguous user stories.
10
+ Batches structured multiple-choice questions with explicit (Recommended) defaults,
11
+ strictly adhering to the Fact/Decision boundary (never asking for facts ascertainable
12
+ directly from the codebase).
13
+
14
+ Reference:
15
+ - Matt Pocock (The Grilling Primitive: Frontier Decision Interrogation).
16
+ - Synapse-Hub Subsystem 6: Section 6.7 of agent_skill_training_analysis.md.
17
+ """
18
+
19
+ import os
20
+ import sys
21
+ import re
22
+ import json
23
+ import argparse
24
+ from typing import List, Dict, Any
25
+
26
+ # Enforce UTF-8 stdout encoding for Windows console compatibility
27
+ if hasattr(sys.stdout, 'reconfigure'):
28
+ try:
29
+ sys.stdout.reconfigure(encoding='utf-8')
30
+ sys.stderr.reconfigure(encoding='utf-8')
31
+ except Exception:
32
+ pass
33
+
34
+
35
+ class PocockGriller:
36
+ """Interrogates user stories along the prerequisite decision frontier."""
37
+
38
+ def __init__(self, target_framework: str = "Playwright_TS"):
39
+ self.target_framework = target_framework
40
+
41
+ def generate_frontier_questions(self, story_text: str) -> List[Dict[str, Any]]:
42
+ """
43
+ Analyzes requirements text and returns batched questions for ask_question.
44
+ Only generates questions for true decision forks, never for discoverable codebase facts.
45
+ """
46
+ questions = []
47
+
48
+ # 1. Scope & Execution Boundary
49
+ questions.append({
50
+ "question": "Phạm vi kiểm thử tự động (Execution Scope): Cấp độ kiểm thử mong muốn cho user story này là gì?",
51
+ "is_multi_select": False,
52
+ "options": [
53
+ "(Recommended) End-to-End (E2E) UI Automation kết hợp Clean POM và xác thực API payload",
54
+ "API Functional Testing độc lập (không tương tác giao diện browser)",
55
+ "Smoke Test kiểm tra luồng chính (Happy Path) tối giản"
56
+ ]
57
+ })
58
+
59
+ # 2. Test Data Lifecycle & Idempotency
60
+ if not re.search(r"\b(?:mock|stub|seed)\b", story_text, re.IGNORECASE):
61
+ questions.append({
62
+ "question": "Quản lý dữ liệu kiểm thử (Test Data Strategy): Chiến lược sinh và dọn dẹp dữ liệu nào được áp dụng?",
63
+ "is_multi_select": False,
64
+ "options": [
65
+ "(Recommended) Tự động sinh mã duy nhất (BasePage.uniqueCode('AUTO_')) kèm Teardown Cleanup tự động",
66
+ "Dùng tài khoản / dữ liệu tĩnh cấu hình sẵn trong Profile / .env",
67
+ "Network Mocking: Giả lập API response (không tạo dữ liệu thật trên DB)"
68
+ ]
69
+ })
70
+
71
+ # 3. Assertion Boundary & Negative Scenarios
72
+ questions.append({
73
+ "question": "Bao phủ kịch bản biên & bất biến (Assertion Frontier): Cần kiểm thử các trường hợp nào ngoài Happy Path?",
74
+ "is_multi_select": True,
75
+ "options": [
76
+ "(Recommended) Kiểm tra validation form khi bỏ trống các trường bắt buộc",
77
+ "(Recommended) Kiểm tra trạng thái nút Submit / Save khi form chưa hợp lệ (Disabled State)",
78
+ "Kiểm tra timeout hoặc mạng chập chờn (Chaos network fault injection)",
79
+ "Kiểm tra quyền truy cập trái phép (Unauthorized / Forbidden 403)"
80
+ ]
81
+ })
82
+
83
+ return questions
84
+
85
+
86
+ def main():
87
+ parser = argparse.ArgumentParser(description="Pocock Grilling Primitive Engine")
88
+ parser.add_argument("story", nargs="?", default="", help="User story or requirement text")
89
+ parser.add_argument("--json", action="store_true", help="Output questions in JSON format")
90
+ args = parser.parse_args()
91
+
92
+ story_text = args.story or "User Story: As a registered user, I want to approve invoices with attached receipts."
93
+
94
+ griller = PocockGriller()
95
+ questions = griller.generate_frontier_questions(story_text)
96
+
97
+ if args.json:
98
+ print(json.dumps({"story": story_text, "questions": questions}, indent=2))
99
+ return 0
100
+
101
+ print("\n" + "=" * 70)
102
+ print(" POCOCK GRILLING PRIMITIVE (PREREQUISITE DECISION FRONTIER)")
103
+ print("=" * 70)
104
+ print(f"Target Requirement: {story_text}\n")
105
+ print(f"Generated {len(questions)} batched decision questions with recommended defaults:\n")
106
+
107
+ for idx, q in enumerate(questions, 1):
108
+ print(f"Q{idx}: {q['question']}")
109
+ for opt in q["options"]:
110
+ print(f" • {opt}")
111
+ print()
112
+ print("=" * 70 + "\n")
113
+ return 0
114
+
115
+
116
+ if __name__ == "__main__":
117
+ sys.exit(main())