symfonic.agent.middleware¶
middleware ¶
symfonic.agent.middleware -- post-draft reflection and metacognition.
Public API
MetacognitiveMiddleware -- the middleware class ReflectionEvent -- frozen event emitted after reflection ReflectionVerdict -- Literal type alias compute_confidence -- deterministic heuristic detect_sensitive_hit -- sensitive-tag detection
MetacognitiveMiddleware ¶
MetacognitiveMiddleware(
*,
model_provider: Any,
config: FrameworkConfig,
domain: DomainTemplate,
procedural_layer: Any | None = None,
reflection_prompt_template: str | None = None,
callback_manager: CallbackManager | None = None,
observability_hook: ObservabilityHook | None = None,
)
Post-draft reflection pass. Opt-in via FrameworkConfig.metacognition_enabled.
Gate (both disjuncts OR-ed): (a) compute_confidence(activation_log) < config.metacognition_confidence_threshold (b) detect_sensitive_hit(activation_log, sensitive_tags) is not None
On fire: ONE hidden LLM call comparing draft vs. core_preferences and procedural rules retrieved via the activation_log's inference_paths. Returns verdict in {approve, revise, refuse}.
Source code in src/symfonic/agent/middleware/metacognitive.py
evaluate
async
¶
evaluate(
*,
run_id: str,
draft: str,
activation_log: dict[str, Any],
scope: Any,
callback_manager: CallbackManager | None = None,
callback_node_name: str = "metacognition_critic",
tool_calls_this_turn: list[dict[str, Any]]
| None = None,
fabrication_findings: list[FabricationFinding]
| None = None,
intent_verdict: IntentVerdict | None = None,
) -> ReflectionEvent
Run gate check and (if triggered) the hidden reflection call.
Returns a ReflectionEvent. Caller decides what to do with the verdict. If metacognition is disabled, returns an 'approve' event with confidence_before=1.0, reflection_latency_ms=0.0, and NO LLM call made.
callback_manager (v7.4.3) overrides the constructor-provided
manager for this call only; when both are None no callback is
emitted (back-compat). When the gate fires AND a manager is in
scope, an on_llm_end event with
node_name="metacognition_critic" is dispatched after the LLM
call (mirrors react.py:303-311).
Source code in src/symfonic/agent/middleware/metacognitive.py
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 | |
ReflectionEvent
dataclass
¶
ReflectionEvent(
run_id: str,
verdict: ReflectionVerdict,
confidence_before: float,
sensitive_tag_hit: str | None,
original_draft: str,
revised_draft: str | None,
refusal_message: str | None,
matched_preferences: list[str] = list(),
reflection_latency_ms: float = 0.0,
reflection_tokens_in: int = 0,
reflection_tokens_out: int = 0,
)
Emitted by MetacognitiveMiddleware after a reflection pass.
compute_confidence ¶
Deterministic heuristic in [0.0, 1.0]. Returns 0.5 on empty input.
Formula
confidence = clamp01( 0.4 * seed_strength + 0.3 * path_coverage + 0.3 * edge_weight_factor )
Where
seed_strength = avg score of activated_nodes (>= 1.0 if seeded) path_coverage = min(1.0, len(inference_paths) / 3) edge_weight_factor = avg edge weight of traversed_edges / 5.0, capped at 1.0
Public so tests can exercise edge cases without spinning up a full agent.
Source code in src/symfonic/agent/middleware/confidence.py
detect_sensitive_hit ¶
detect_sensitive_hit(
activation_log: dict[str, Any],
sensitive_tags: frozenset[str],
) -> str | None
Return the first matching tag when any activated_node carries one of sensitive_tags in properties['tags'], else None.