File size: 8,609 Bytes
c0e3412
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
"""Type definitions and core data structures for the CPPTAI framework.

All names and docstrings are in English, per user request.
"""

import os
import uuid
from dataclasses import dataclass, field
from enum import Enum
from typing import Any, Dict, List, Optional


class DifficultyLevel(Enum):
    """Discrete difficulty levels used to rank problem blocks."""

    IMPOSSIBLE = 5
    HARD = 4
    MEDIUM = 3
    NORMAL = 2
    EASY = 1
    TRIVIAL = 0


@dataclass
class ProblemBlock:
    """Atomic unit extracted from a complex problem statement.

    Attributes:
        id: Stable identifier for the block.
        content: Raw text content of the block.
        difficulty: Coarse difficulty level for sorting and reporting.
        complexity_score: Continuous [0, 1] score estimating inherent complexity.
        solution_probability: Continuous [0, 1] score estimating solvability.
        improbability: Continuous [0, 1] score = 1 - solution_probability.
        floor_index: Integer floor assigned in Vertical Topology (Phase II).
        dependencies: IDs of other blocks this block depends on.
    """

    id: str
    content: str
    difficulty: DifficultyLevel
    complexity_score: float
    solution_probability: float
    improbability: float
    floor_index: int = 0
    dependencies: List[str] = field(default_factory=list)
    influence_score: float = 0.0


@dataclass
class BenchmarkItem:
    """Canonical benchmark item used by runners and evaluators."""

    id: str
    prompt: str
    expected: List[str]
    dataset: str
    metadata: Dict[str, Any] = field(default_factory=dict)


@dataclass
class ExplorationTrajectory:
    """Singola traiettoria di esplorazione generata dall'Explorer (Phase 0).

    Ogni traiettoria rappresenta un'interpretazione/framing del problema
    originale, generata con un diverso 'seed di rumore' e progressivamente
    raffinata attraverso denoising steps.
    """

    id: str
    interpretation: str
    confidence: float
    reasoning_path: List[str]
    noise_seed: int
    novelty_score: float = 0.0
    feasibility_score: float = 0.0
    metadata: Dict[str, Any] = field(default_factory=dict)

    def to_summary_dict(self) -> Dict[str, Any]:
        return {
            "id": self.id,
            "interpretation": self.interpretation[:200],
            "confidence": round(self.confidence, 4),
            "reasoning_steps": len(self.reasoning_path),
            "novelty": round(self.novelty_score, 4),
        }


@dataclass
class AnalyzerPrepared:
    """Output della fase Analyzer (Phase 0.5) — problema arricchito per Phase I.

    L'Analyzer prende le traiettorie dell'Explorer, seleziona le migliori,
    e produce un problema arricchito che diventa l'input di Phase I.
    """

    enriched_problem: str
    top_interpretations: List[str]
    aggregate_confidence: float
    reasoning_summary: str
    trajectories_used: int
    patterns_identified: List[str] = field(default_factory=list)
    metadata: Dict[str, Any] = field(default_factory=dict)


@dataclass
class RunConfig:
    """Run configuration for reproducible execution."""

    seed: int = 0
    external_enabled: bool = True
    cache_mode: str = "online"
    output_dir: str = "."
    benchmark_name: str = "mixed"
    model_name: str = "DeepSeek-V3.2-Exp"
    max_iterations: int = 100
    domain: str = ""
    problem_type: str = ""
    # --- Explorer + Analyzer options (default: disabled for backward compat) ---
    explorer_enabled: bool = False
    analyzer_enabled: bool = False
    explorer_num_trajectories: int = 5
    explorer_batch_size: int = 1
    explorer_max_workers: int = 4
    explorer_adaptive: bool = True
    explorer_parallel_llm: bool = False
    explorer_cache_results: bool = True
    explorer_diversity_weight: float = 0.6
    explorer_noise_level: float = 0.6
    explorer_temperature: float = 0.8
    explorer_denoising_steps: int = 3
    analyzer_ensemble_size: int = 3
    analyzer_coherence_threshold: float = 0.5

    @staticmethod
    def from_env() -> "RunConfig":
        external_enabled = (os.getenv("BENCH_DISABLE_EXTERNAL", "0") != "1") and (
            os.getenv("CPPTAI_DISABLE_EXTERNAL", "0") != "1"
        )
        cache_mode = os.getenv("CPPTAI_CACHE_MODE", "online")
        seed = int(os.getenv("CPPTAI_SEED", "0"))
        output_dir = os.getenv("CPPTAI_OUTPUT_DIR", ".")
        benchmark_name = os.getenv("CPPTAI_BENCHMARK", "mixed")
        model_name = os.getenv("CPPTAI_MODEL", "DeepSeek-V3.2-Exp")
        max_iterations = int(os.getenv("CPPTAI_MAX_ITERS", "100"))
        explorer_enabled = os.getenv("CPPTAI_EXPLORER_ENABLED", "0") == "1"
        analyzer_enabled = os.getenv("CPPTAI_ANALYZER_ENABLED", "0") == "1"
        explorer_num_trajectories = int(os.getenv("CPPTAI_EXPLORER_TRAJECTORIES", "5"))
        explorer_batch_size = int(os.getenv("CPPTAI_EXPLORER_BATCH_SIZE", "1"))
        explorer_max_workers = int(os.getenv("CPPTAI_EXPLORER_MAX_WORKERS", "4"))
        explorer_adaptive = os.getenv("CPPTAI_EXPLORER_ADAPTIVE", "1") == "1"
        explorer_parallel_llm = os.getenv("CPPTAI_EXPLORER_PARALLEL_LLM", "0") == "1"
        explorer_cache_results = os.getenv("CPPTAI_EXPLORER_CACHE", "1") == "1"
        explorer_diversity_weight = float(os.getenv("CPPTAI_EXPLORER_DIVERSITY_WEIGHT", "0.6"))
        explorer_noise_level = float(os.getenv("CPPTAI_EXPLORER_NOISE", "0.6"))
        explorer_temperature = float(os.getenv("CPPTAI_EXPLORER_TEMP", "0.8"))
        explorer_denoising_steps = int(os.getenv("CPPTAI_EXPLORER_DENOISE_STEPS", "3"))
        analyzer_ensemble_size = int(os.getenv("CPPTAI_ANALYZER_ENSEMBLE", "3"))
        analyzer_coherence_threshold = float(os.getenv("CPPTAI_ANALYZER_THRESHOLD", "0.5"))
        return RunConfig(
            seed=seed,
            external_enabled=external_enabled,
            cache_mode=cache_mode,
            output_dir=output_dir,
            benchmark_name=benchmark_name,
            model_name=model_name,
            max_iterations=max_iterations,
            explorer_enabled=explorer_enabled,
            analyzer_enabled=analyzer_enabled,
            explorer_num_trajectories=explorer_num_trajectories,
            explorer_batch_size=explorer_batch_size,
            explorer_max_workers=explorer_max_workers,
            explorer_adaptive=explorer_adaptive,
            explorer_parallel_llm=explorer_parallel_llm,
            explorer_cache_results=explorer_cache_results,
            explorer_diversity_weight=explorer_diversity_weight,
            explorer_noise_level=explorer_noise_level,
            explorer_temperature=explorer_temperature,
            explorer_denoising_steps=explorer_denoising_steps,
            analyzer_ensemble_size=analyzer_ensemble_size,
            analyzer_coherence_threshold=analyzer_coherence_threshold,
        )


@dataclass
class PhaseOutput:
    """Structured phase output for artifacts."""

    name: str
    input: Dict[str, Any]
    output: Dict[str, Any]
    decisions: Dict[str, Any] = field(default_factory=dict)
    errors: List[str] = field(default_factory=list)
    duration_sec: float = 0.0


@dataclass
class RunArtifact:
    """Canonical artifact schema for reproducibility and auditability."""

    run_id: str
    git_commit: str
    timestamp: str
    benchmark_name: str
    task_id: str
    model_name: str
    seed: int
    external_enabled: bool
    cache_mode: str
    phase_outputs: List[PhaseOutput]
    entropy_by_phase: Dict[str, float]
    final_answer: str
    verification_result: Dict[str, Any]
    runtime_seconds: float
    token_counts: Dict[str, int]
    provenance: Dict[str, Any] = field(default_factory=dict)
    failure_phase: Optional[str] = None
    failure_reason: Optional[str] = None
    fallback_triggered: bool = False
    # --- Explorer + Analyzer campi ---
    explorer_trajectories_count: Optional[int] = None
    analyzer_confidence: Optional[float] = None
    exploration_data: Optional[Dict[str, Any]] = None


@dataclass
class SolutionState:
    """Canonical solution state tracked across phases."""

    problem: str
    blocks: List[ProblemBlock] = field(default_factory=list)
    building_height: int = 1
    block_solutions: List[Dict[str, Any]] = field(default_factory=list)
    coherence: float = 0.2
    completeness: float = 0.2
    confidence: float = 0.2
    extra: Dict[str, Any] = field(default_factory=dict)
    # --- Explorer + Analyzer campi ---
    explorer_trajectories: List[ExplorationTrajectory] = field(default_factory=list)
    analyzer_prepared: Optional[AnalyzerPrepared] = None
    exploration_enabled: bool = True