hegelion 0.4.0__py3-none-any.whl

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. hegelion/__init__.py +45 -0
  2. hegelion/core/__init__.py +29 -0
  3. hegelion/core/agent.py +166 -0
  4. hegelion/core/autocoding_state.py +293 -0
  5. hegelion/core/backends.py +442 -0
  6. hegelion/core/cache.py +92 -0
  7. hegelion/core/config.py +276 -0
  8. hegelion/core/core.py +649 -0
  9. hegelion/core/engine.py +865 -0
  10. hegelion/core/logging_utils.py +67 -0
  11. hegelion/core/models.py +293 -0
  12. hegelion/core/parsing.py +271 -0
  13. hegelion/core/personas.py +81 -0
  14. hegelion/core/prompt_autocoding.py +353 -0
  15. hegelion/core/prompt_dialectic.py +414 -0
  16. hegelion/core/prompts.py +127 -0
  17. hegelion/core/schema.py +67 -0
  18. hegelion/core/validation.py +68 -0
  19. hegelion/council.py +254 -0
  20. hegelion/examples_data/__init__.py +6 -0
  21. hegelion/examples_data/glm4_6_examples.jsonl +2 -0
  22. hegelion/judge.py +230 -0
  23. hegelion/mcp/__init__.py +3 -0
  24. hegelion/mcp/server.py +918 -0
  25. hegelion/scripts/hegelion_agent_cli.py +90 -0
  26. hegelion/scripts/hegelion_bench.py +117 -0
  27. hegelion/scripts/hegelion_cli.py +497 -0
  28. hegelion/scripts/hegelion_dataset.py +99 -0
  29. hegelion/scripts/hegelion_eval.py +137 -0
  30. hegelion/scripts/mcp_setup.py +150 -0
  31. hegelion/search_providers.py +151 -0
  32. hegelion/training/__init__.py +7 -0
  33. hegelion/training/datasets.py +123 -0
  34. hegelion/training/generator.py +232 -0
  35. hegelion/training/mlx_scu_trainer.py +379 -0
  36. hegelion/training/mlx_trainer.py +181 -0
  37. hegelion/training/unsloth_trainer.py +136 -0
  38. hegelion-0.4.0.dist-info/METADATA +295 -0
  39. hegelion-0.4.0.dist-info/RECORD +43 -0
  40. hegelion-0.4.0.dist-info/WHEEL +5 -0
  41. hegelion-0.4.0.dist-info/entry_points.txt +8 -0
  42. hegelion-0.4.0.dist-info/licenses/LICENSE +21 -0
  43. hegelion-0.4.0.dist-info/top_level.txt +1 -0
@@ -0,0 +1,81 @@
1
+ """Standard critic personas for Hegelion dialectics."""
2
+
3
+ from typing import Dict, List, Optional
4
+ from dataclasses import dataclass
5
+
6
+
7
+ @dataclass
8
+ class Persona:
9
+ """A critic persona definition."""
10
+
11
+ name: str
12
+ description: str
13
+ focus: str
14
+ instructions: str
15
+
16
+
17
+ # Standard Council of Critics
18
+ LOGICIAN = Persona(
19
+ name="The Logician",
20
+ description="Expert in logical consistency and formal reasoning",
21
+ focus="logical fallacies, internal contradictions, invalid inferences, missing premises",
22
+ instructions="Identify logical flaws. Focus on structure and validity of arguments, not the premises themselves.",
23
+ )
24
+
25
+ EMPIRICIST = Persona(
26
+ name="The Empiricist",
27
+ description="Expert in evidence, facts, and empirical grounding",
28
+ focus="factual errors, unsupported claims, missing evidence, contradictions with established science",
29
+ instructions="Identify factual inaccuracies or lack of evidence. Require empirical proof for claims.",
30
+ )
31
+
32
+ ETHICIST = Persona(
33
+ name="The Ethicist",
34
+ description="Expert in ethical implications and societal impact",
35
+ focus="potential harm, ethical blind spots, fairness issues, unintended consequences",
36
+ instructions="Identify ethical risks and societal impacts. Focus on human values and potential harm.",
37
+ )
38
+
39
+ # Specialized Personas
40
+ SECURITY_ENGINEER = Persona(
41
+ name="Security Engineer",
42
+ description="Senior security engineer looking for exploits",
43
+ focus="vulnerabilities, attack vectors, data leaks, security best practices",
44
+ instructions="Act as a red teamer. Try to break the system or find ways to exploit the proposed approach.",
45
+ )
46
+
47
+ RUTHLESS_EDITOR = Persona(
48
+ name="Ruthless Editor",
49
+ description="Experienced editor focused on clarity and brevity",
50
+ focus="fluff, redundancy, jargon, unclear phrasing, weak structure",
51
+ instructions="Cut the fluff. Demand clarity and precision. Point out where the argument meanders.",
52
+ )
53
+
54
+ DEVILS_ADVOCATE = Persona(
55
+ name="Devil's Advocate",
56
+ description="Professional contrarian testing robustness",
57
+ focus="alternative interpretations, edge cases, steel-manning opposing views",
58
+ instructions="Take the opposite view regardless of your personal belief. Find the strongest argument against the thesis.",
59
+ )
60
+
61
+ # Persona Registry
62
+ PRESETS: Dict[str, List[Persona]] = {
63
+ "council": [LOGICIAN, EMPIRICIST, ETHICIST],
64
+ "security": [SECURITY_ENGINEER],
65
+ "editorial": [RUTHLESS_EDITOR],
66
+ "debate": [DEVILS_ADVOCATE],
67
+ "comprehensive": [LOGICIAN, EMPIRICIST, ETHICIST, DEVILS_ADVOCATE],
68
+ }
69
+
70
+
71
+ def get_personas(
72
+ preset_name: Optional[str] = None, custom_personas: Optional[List[Persona]] = None
73
+ ) -> List[Persona]:
74
+ """Get a list of personas based on preset name or custom definition."""
75
+ if custom_personas:
76
+ return custom_personas
77
+
78
+ if not preset_name:
79
+ return [] # Default to standard single antithesis if no preset
80
+
81
+ return PRESETS.get(preset_name.lower(), [])
@@ -0,0 +1,353 @@
1
+ """Prompt generation for dialectical autocoding sessions.
2
+
3
+ This module provides prompt generation for the coach-player autocoding loop
4
+ based on the g3 paper's adversarial cooperation paradigm. Prompts guide
5
+ LLMs through implementation (player) and validation (coach) phases.
6
+ """
7
+
8
+ from __future__ import annotations
9
+
10
+ from dataclasses import dataclass
11
+ from typing import Any, Dict, Optional
12
+
13
+
14
+ @dataclass
15
+ class AutocodingPrompt:
16
+ """A structured prompt for autocoding phases.
17
+
18
+ Attributes:
19
+ phase: The autocoding phase - "player" or "coach".
20
+ prompt: The full prompt text for the LLM.
21
+ instructions: Short instructions for the LLM.
22
+ expected_format: Description of expected output format.
23
+ requirements_embedded: Whether requirements are included in prompt.
24
+ """
25
+
26
+ phase: str
27
+ prompt: str
28
+ instructions: str
29
+ expected_format: str
30
+ requirements_embedded: bool = True
31
+
32
+ def to_dict(self) -> Dict[str, Any]:
33
+ """Serialize prompt to dictionary."""
34
+ return {
35
+ "phase": self.phase,
36
+ "prompt": self.prompt,
37
+ "instructions": self.instructions,
38
+ "expected_format": self.expected_format,
39
+ "requirements_embedded": self.requirements_embedded,
40
+ }
41
+
42
+
43
+ class PromptDrivenAutocoding:
44
+ """Generates prompts for dialectical autocoding sessions.
45
+
46
+ Based on the g3 paper's adversarial cooperation paradigm:
47
+ - Player agent focuses on implementation
48
+ - Coach agent focuses on validation
49
+ - Fresh context each turn
50
+ - Requirements are the single source of truth
51
+ """
52
+
53
+ def generate_player_prompt(
54
+ self,
55
+ requirements: str,
56
+ coach_feedback: Optional[str] = None,
57
+ turn_number: int = 1,
58
+ max_turns: int = 10,
59
+ ) -> AutocodingPrompt:
60
+ """Generate a prompt for the player (implementation) agent.
61
+
62
+ Args:
63
+ requirements: The requirements document (source of truth).
64
+ coach_feedback: Feedback from previous coach turn, if any.
65
+ turn_number: Current turn number (1-indexed for display).
66
+ max_turns: Maximum turns in the session.
67
+
68
+ Returns:
69
+ AutocodingPrompt for the player phase.
70
+ """
71
+ turns_remaining = max_turns - turn_number + 1
72
+
73
+ if coach_feedback:
74
+ feedback_section = f"""## PREVIOUS COACH FEEDBACK
75
+ {coach_feedback}
76
+
77
+ Address the issues identified above. Focus on the specific items marked with X."""
78
+ else:
79
+ feedback_section = """## GETTING STARTED
80
+ This is the first turn. Read the requirements carefully and begin implementation."""
81
+
82
+ prompt = f"""You are the PLAYER agent in a dialectical autocoding session.
83
+
84
+ Your role is to IMPLEMENT the requirements. A separate COACH agent will verify your work.
85
+
86
+ ## REQUIREMENTS (Source of Truth)
87
+ {requirements}
88
+
89
+ ## SESSION STATUS
90
+ Turn: {turn_number}/{max_turns} ({turns_remaining} remaining)
91
+ {feedback_section}
92
+
93
+ ## WORKSPACE GUIDANCE
94
+ Before implementing, explore the workspace:
95
+ 1. Check current file structure with ls or tree
96
+ 2. Read existing code to understand patterns and conventions
97
+ 3. Check git status to see what has changed
98
+ 4. Run any existing tests to understand the current state
99
+
100
+ ## YOUR TASK
101
+ 1. Read the requirements carefully - they should be structured as a checklist
102
+ 2. Implement a solution that satisfies ALL requirements
103
+ 3. Write code, create tests, and execute commands as needed
104
+ 4. Run tests and verify your implementation compiles/runs
105
+ 5. Address specific feedback from the coach with targeted improvements
106
+
107
+ ## CRITICAL RULES
108
+ - DO NOT declare success prematurely
109
+ - DO NOT claim "all requirements satisfied" unless you have verified each one
110
+ - DO NOT summarize what you did - just do the implementation
111
+ - Focus on IMPLEMENTATION, not self-assessment
112
+ - The coach will INDEPENDENTLY verify compliance
113
+ - Leave verification to the coach
114
+
115
+ Begin your implementation now. Work methodically through the requirements."""
116
+
117
+ return AutocodingPrompt(
118
+ phase="player",
119
+ prompt=prompt,
120
+ instructions="Implement the requirements. Do not declare success - the coach will verify.",
121
+ expected_format="Implementation actions: code changes, test execution, file operations",
122
+ requirements_embedded=True,
123
+ )
124
+
125
+ def generate_coach_prompt(
126
+ self,
127
+ requirements: str,
128
+ turn_number: int = 1,
129
+ max_turns: int = 10,
130
+ ) -> AutocodingPrompt:
131
+ """Generate a prompt for the coach (validation) agent.
132
+
133
+ Args:
134
+ requirements: The requirements document (source of truth).
135
+ turn_number: Current turn number (1-indexed for display).
136
+ max_turns: Maximum turns in the session.
137
+
138
+ Returns:
139
+ AutocodingPrompt for the coach phase.
140
+ """
141
+ turns_remaining = max_turns - turn_number
142
+
143
+ prompt = f"""You are the COACH agent in a dialectical autocoding session.
144
+
145
+ Your role is to VERIFY the implementation against requirements. Be rigorous and objective.
146
+
147
+ ## REQUIREMENTS (Source of Truth)
148
+ {requirements}
149
+
150
+ ## SESSION STATUS
151
+ Turn: {turn_number}/{max_turns} ({turns_remaining} turns remaining after this)
152
+
153
+ ## WORKSPACE GUIDANCE
154
+ Thoroughly verify the implementation:
155
+ 1. Read the implemented code files
156
+ 2. Run the test suite and check ALL results
157
+ 3. Verify the application compiles/builds successfully
158
+ 4. Check that each requirement is ACTUALLY implemented, not just claimed
159
+ 5. Test edge cases mentioned in the requirements
160
+
161
+ ## YOUR TASK
162
+ 1. Review the current implementation in the workspace
163
+ 2. Verify compliance with EACH requirement INDEPENDENTLY
164
+ 3. IGNORE any success claims from the player - verify yourself
165
+ 4. Provide structured feedback using the exact format below
166
+
167
+ ## CRITICAL RULES
168
+ - DO NOT trust the player's self-assessment
169
+ - VERIFY each requirement by examining code and running tests
170
+ - Be specific about what is missing or incorrect
171
+ - Provide actionable feedback the player can address
172
+
173
+ ## OUTPUT FORMAT
174
+
175
+ **REQUIREMENTS COMPLIANCE:**
176
+ (Check each item from the requirements doc - use the exact requirement text)
177
+ - [checkmark] [Requirement text] - [verification notes: how you verified]
178
+ - [X] [Requirement text] - [specific issue: what's wrong or missing]
179
+ ...
180
+
181
+ **IMMEDIATE ACTIONS NEEDED:**
182
+ (Only if there are issues - list specific fixes required)
183
+ 1. [Specific action with file/function names]
184
+ 2. [Specific action with file/function names]
185
+ ...
186
+
187
+ **ASSESSMENT:**
188
+ [If ALL requirements show checkmarks with solid verification]
189
+ COACH APPROVED
190
+
191
+ [Otherwise, provide a brief summary of remaining work]
192
+
193
+ Use checkmark for satisfied requirements and X for unsatisfied ones.
194
+ Be thorough - the player depends on your feedback to make progress."""
195
+
196
+ return AutocodingPrompt(
197
+ phase="coach",
198
+ prompt=prompt,
199
+ instructions="Verify implementation against requirements. Ignore player's self-assessment.",
200
+ expected_format="Compliance checklist with checkmarks/X, actions needed, and COACH APPROVED or summary",
201
+ requirements_embedded=True,
202
+ )
203
+
204
+ def generate_single_shot_prompt(
205
+ self,
206
+ requirements: str,
207
+ max_turns: int = 10,
208
+ ) -> AutocodingPrompt:
209
+ """Generate a single prompt for self-directed autocoding.
210
+
211
+ This combines player and coach roles into a single prompt that
212
+ guides the LLM through iterative implementation with self-verification.
213
+
214
+ Args:
215
+ requirements: The requirements document (source of truth).
216
+ max_turns: Maximum iterations to attempt.
217
+
218
+ Returns:
219
+ AutocodingPrompt for single-shot autocoding.
220
+ """
221
+ prompt = f"""You will perform DIALECTICAL AUTOCODING: iterative implementation with self-verification.
222
+
223
+ ## REQUIREMENTS (Source of Truth)
224
+ {requirements}
225
+
226
+ ## PROCESS
227
+ Alternate between PLAYER and COACH roles for up to {max_turns} iterations:
228
+
229
+ ### PLAYER PHASE
230
+ 1. Explore the workspace (file structure, existing code, git status)
231
+ 2. Implement requirements methodically
232
+ 3. Write tests and verify they pass
233
+ 4. DO NOT declare success - move to coach phase
234
+
235
+ ### COACH PHASE
236
+ 1. Review your implementation against EACH requirement
237
+ 2. Run tests and verify builds
238
+ 3. Create a compliance checklist:
239
+ - [checkmark] [Requirement] - [how verified]
240
+ - [X] [Requirement] - [what's wrong]
241
+ 4. If all requirements satisfied: output "COACH APPROVED"
242
+ 5. Otherwise: list specific actions needed and return to player phase
243
+
244
+ ## CRITICAL RULES
245
+ - Requirements are the SINGLE SOURCE OF TRUTH
246
+ - In coach phase, IGNORE your own success claims - verify independently
247
+ - Be honest about compliance - partial implementation is not success
248
+ - Each iteration should make measurable progress
249
+ - Stop when COACH APPROVED or {max_turns} iterations reached
250
+
251
+ ## OUTPUT FORMAT
252
+ For each iteration:
253
+ ```
254
+ === ITERATION N ===
255
+
256
+ [PLAYER]
257
+ [Implementation actions taken]
258
+
259
+ [COACH]
260
+ **REQUIREMENTS COMPLIANCE:**
261
+ - [checkmark/X] [Requirement] - [notes]
262
+ ...
263
+
264
+ **ASSESSMENT:**
265
+ [COACH APPROVED or CONTINUE]
266
+ ```
267
+
268
+ Begin dialectical autocoding now. Start with ITERATION 1."""
269
+
270
+ return AutocodingPrompt(
271
+ phase="single_shot",
272
+ prompt=prompt,
273
+ instructions="Iterate between implementation and verification until approved or max turns.",
274
+ expected_format="Iterations with player actions and coach compliance checklists",
275
+ requirements_embedded=True,
276
+ )
277
+
278
+
279
+ def create_autocoding_workflow(
280
+ requirements: str,
281
+ max_turns: int = 10,
282
+ ) -> Dict[str, Any]:
283
+ """Create a complete autocoding workflow as structured data.
284
+
285
+ Args:
286
+ requirements: The requirements document.
287
+ max_turns: Maximum turns in the session.
288
+
289
+ Returns:
290
+ Workflow dictionary with steps and instructions.
291
+ """
292
+ autocoding = PromptDrivenAutocoding()
293
+
294
+ workflow = {
295
+ "workflow_type": "dialectical_autocoding",
296
+ "requirements": requirements,
297
+ "max_turns": max_turns,
298
+ "steps": [
299
+ {
300
+ "step": 1,
301
+ "name": "Initialize Session",
302
+ "action": "Call autocoding_init with requirements",
303
+ "returns": "AutocodingState",
304
+ },
305
+ {
306
+ "step": 2,
307
+ "name": "Player Turn",
308
+ "action": "Call player_prompt with state, execute returned prompt",
309
+ "returns": "Implementation actions",
310
+ "repeat": True,
311
+ },
312
+ {
313
+ "step": 3,
314
+ "name": "Coach Turn",
315
+ "action": "Call coach_prompt with state, execute returned prompt",
316
+ "returns": "Compliance checklist and feedback",
317
+ "repeat": True,
318
+ },
319
+ {
320
+ "step": 4,
321
+ "name": "Advance State",
322
+ "action": "Call autocoding_advance with coach feedback and approval status",
323
+ "returns": "Updated AutocodingState",
324
+ "repeat": True,
325
+ },
326
+ ],
327
+ "termination": {
328
+ "approved": "Coach outputs 'COACH APPROVED'",
329
+ "timeout": f"Reached {max_turns} turns without approval",
330
+ },
331
+ "instructions": {
332
+ "execution_mode": "iterative",
333
+ "description": "Alternate between player and coach phases until approved or timeout",
334
+ "state_passing": "Pass AutocodingState dict between all tool calls",
335
+ },
336
+ }
337
+
338
+ # Include sample prompts for reference
339
+ workflow["sample_prompts"] = {
340
+ "player": autocoding.generate_player_prompt(
341
+ requirements="{{requirements}}",
342
+ coach_feedback=None,
343
+ turn_number=1,
344
+ max_turns=max_turns,
345
+ ).to_dict(),
346
+ "coach": autocoding.generate_coach_prompt(
347
+ requirements="{{requirements}}",
348
+ turn_number=1,
349
+ max_turns=max_turns,
350
+ ).to_dict(),
351
+ }
352
+
353
+ return workflow