Index
learning
¶
Learning module for outcome recording, pattern detection, and judgment.
This module provides: - Outcome recording and pattern detection (local workspace) - Global learning store for cross-workspace pattern persistence - Pattern weighting with recency/effectiveness decay - Pattern aggregation for merging outcomes into global patterns - Error learning hooks for adaptive wait times - Migration from workspace-local to global store - Judgment client for LLM-based decision making
Classes¶
AggregationResult
¶
PatternAggregator
¶
Aggregates patterns from executions into the global store.
Implements the aggregation strategy defined in Movement III: 1. Record all sheet outcomes to executions table 2. Run PatternDetector.detect_all() on new outcomes 3. Merge detected patterns with existing patterns in global store 4. Update priority_score for all affected patterns 5. Record pattern_applications for any patterns that were applied 6. Update error_recoveries for any error learning
Conflict resolution strategy: - Same pattern: merge by increasing counts and updating timestamps - Different suggested_actions: keep action with higher effectiveness
Initialize the pattern aggregator.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
global_store
|
GlobalLearningStore
|
The global learning store for persistence. |
required |
weighter
|
PatternWeighter | None
|
Pattern weighter for priority calculation. |
None
|
Source code in src/marianne/learning/aggregator.py
Functions¶
aggregate_outcomes
¶
Aggregate a batch of outcomes into the global store.
This is the main entry point called after job completion.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
outcomes
|
list[SheetOutcome]
|
List of sheet outcomes from the completed job. |
required |
workspace_path
|
Path
|
Path to the workspace for hashing. |
required |
model
|
str | None
|
Optional model name used for execution. |
None
|
instrument_name
|
str | None
|
Optional instrument/backend type for pattern scoping. |
None
|
Returns:
| Type | Description |
|---|---|
AggregationResult
|
AggregationResult with statistics about the aggregation. |
Source code in src/marianne/learning/aggregator.py
merge_with_conflict_resolution
¶
Merge a new pattern with an existing one using conflict resolution.
Resolution strategy from design document: - occurrence_count: sum - effectiveness_score: weighted average by occurrence_count - last_seen: max - last_confirmed: max - suggested_action: keep action with higher effectiveness
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
existing
|
PatternRecord
|
The existing pattern record. |
required |
new
|
DetectedPattern
|
The newly detected pattern. |
required |
Returns:
| Type | Description |
|---|---|
dict[str, object]
|
Dict of updated field values. |
Source code in src/marianne/learning/aggregator.py
prune_deprecated_patterns
¶
Remove patterns that are below the effectiveness threshold.
Patterns are deprecated (not deleted) if: - They have enough application data (>= 3) - Their effectiveness is below 0.3
Returns:
| Type | Description |
|---|---|
int
|
Number of patterns deprecated. |
Source code in src/marianne/learning/aggregator.py
ErrorLearningConfig
dataclass
¶
ErrorLearningConfig(enabled=True, min_samples=3, learning_rate=0.3, max_wait_time=7200.0, min_wait_time=10.0, decay_factor=0.9)
Configuration for error learning.
Attributes:
| Name | Type | Description |
|---|---|---|
enabled |
bool
|
Master switch for error learning. |
min_samples |
int
|
Minimum recovery samples before using learned delay. |
learning_rate |
float
|
How much to weight new observations vs existing. |
max_wait_time |
float
|
Maximum wait time to suggest (cap on learning). |
min_wait_time |
float
|
Minimum wait time to suggest (floor on learning). |
decay_factor |
float
|
How much to decay old samples over time. |
ErrorLearningContext
dataclass
¶
ErrorLearningHooks
¶
Learning hooks for ErrorClassifier integration.
Provides hooks that can be called at various points in error handling to record error patterns and learn from recovery attempts.
The hooks follow the design pattern of non-invasive integration: - They can be optionally called by the runner - If global_store is None, hooks are no-ops - All operations are logged for debugging
Usage
hooks = ErrorLearningHooks(global_store)
When an error is classified¶
adjusted = hooks.on_error_classified(context) if adjusted.suggested_wait_seconds: await asyncio.sleep(adjusted.suggested_wait_seconds)
After recovery attempt¶
hooks.on_error_recovered(context, success=True)
Initialize error learning hooks.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
global_store
|
GlobalLearningStore | None
|
Global learning store for persistence. If None, hooks are no-ops. |
None
|
config
|
ErrorLearningConfig | None
|
Error learning configuration. |
None
|
Source code in src/marianne/learning/error_hooks.py
Attributes¶
Functions¶
on_error_classified
¶
Hook called when an error is classified.
Records the error occurrence and potentially adjusts the suggested wait time based on learned patterns.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
context
|
ErrorLearningContext
|
Full error context including job/sheet info. |
required |
Returns:
| Type | Description |
|---|---|
ClassifiedError
|
The error with potentially adjusted suggested_wait_seconds. |
Source code in src/marianne/learning/error_hooks.py
on_error_recovered
¶
Hook called after a recovery attempt.
Records the actual wait time and whether recovery succeeded, updating the learned wait times for this error code.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
context
|
ErrorLearningContext
|
Error context with actual_wait filled in. |
required |
success
|
bool
|
Whether the recovery attempt succeeded. |
required |
Source code in src/marianne/learning/error_hooks.py
on_auth_failure
¶
Hook to analyze auth failures.
Uses historical data to determine if this auth failure is likely transient (worth retrying) or permanent (should fail immediately).
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
context
|
ErrorLearningContext
|
Error context for the auth failure. |
required |
Returns:
| Type | Description |
|---|---|
bool
|
Tuple of (is_transient, reason). |
str
|
If is_transient is True, the error might recover after a delay. |
Source code in src/marianne/learning/error_hooks.py
get_error_stats
¶
Get statistics for a specific error code.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
error_code
|
str
|
The error code to query (e.g., 'E103'). |
required |
Returns:
| Type | Description |
|---|---|
dict[str, str | int | float]
|
Dictionary with error statistics. |
Source code in src/marianne/learning/error_hooks.py
ErrorRecoveryRecord
dataclass
¶
ErrorRecoveryRecord(id, error_code, suggested_wait, actual_wait, recovery_success, recorded_at, model, time_of_day)
A record of error recovery timing for learning adaptive waits.
ExecutionRecord
dataclass
¶
ExecutionRecord(id, workspace_hash, job_hash, sheet_num, started_at, completed_at, duration_seconds, status, retry_count, success_without_retry, validation_pass_rate, confidence_score, model, error_codes=list())
A record of a sheet execution stored in the global database.
Functions¶
__post_init__
¶
Clamp fields to valid ranges.
Source code in src/marianne/learning/store/models.py
GlobalLearningStore
¶
Bases: PatternMixin, ExecutionMixin, RateLimitMixin, DriftMixin, EscalationMixin, BudgetMixin, PatternLifecycleMixin, GlobalLearningStoreBase
Global learning store combining all mixins.
This is the primary interface for Marianne's cross-workspace learning system. It provides persistent storage for execution outcomes, detected patterns, error recovery data, and learning metrics across all Marianne workspaces.
The class is composed from multiple mixins, each providing domain-specific functionality. The base class (listed last for proper MRO) provides: - SQLite connection management with WAL mode for concurrent access - Schema creation and version migration - Hashing utilities for workspace and job identification
Mixin Capabilities
PatternMixin: - record_pattern(), get_patterns(), get_pattern_by_id() - record_pattern_application(), update_pattern_effectiveness() - quarantine lifecycle: quarantine_pattern(), validate_pattern() - trust scoring, success factor analysis - pattern discovery broadcasting
ExecutionMixin: - record_outcome() for sheet execution outcomes - get_execution_stats(), get_recent_executions() - get_similar_executions() for learning activation - workspace clustering for cross-workspace correlation
RateLimitMixin: - record_rate_limit_event() for cross-workspace coordination - get_recent_rate_limits() to check before API calls - Enables parallel jobs to avoid hitting same limits
DriftMixin: - calculate_drift_metrics() for effectiveness drift - detect_epistemic_drift() for belief-level monitoring - auto_retire_drifting_patterns() for lifecycle management - get_pattern_evolution_trajectory() for historical analysis
EscalationMixin: - record_escalation_decision() when handlers respond - get_similar_escalation() for pattern-based suggestions - Closes the learning loop for escalation handling
BudgetMixin: - get_exploration_budget(), update_exploration_budget() - record_entropy_response() for diversity injection - Dynamic budget with floor/ceiling to prevent over-convergence
Example
from marianne.learning.store import GlobalLearningStore store = GlobalLearningStore()
Record a pattern¶
store.record_pattern( ... pattern_type="rate_limit_recovery", ... pattern_content={"action": "exponential_backoff"}, ... context={"error_code": "E101"}, ... source_job="job-123", ... )
Query execution statistics¶
stats = store.get_execution_stats() print(f"Total executions: {stats['total_executions']}")
Attributes:
| Name | Type | Description |
|---|---|---|
db_path |
Path to the SQLite database file. |
|
_logger |
MarianneLogger
|
Module logger instance for consistent logging. |
Note
The database uses WAL mode for safe concurrent access from multiple Marianne jobs. Schema migrations are applied automatically when the store is initialized.
Source code in src/marianne/learning/store/base.py
PatternRecord
dataclass
¶
PatternRecord(id, pattern_type, pattern_name, description, occurrence_count, first_seen, last_seen, last_confirmed, led_to_success_count, led_to_failure_count, effectiveness_score, variance, suggested_action, context_tags, priority_score, quarantine_status=PENDING, provenance_job_hash=None, provenance_sheet_num=None, quarantined_at=None, validated_at=None, quarantine_reason=None, trust_score=0.5, trust_calculation_date=None, success_factors=None, success_factors_updated_at=None, active=True, content_hash=None, instrument_name=None)
A pattern record stored in the global database.
v19 Evolution: Extended with quarantine_status, provenance, and trust_score fields to support the Pattern Quarantine & Provenance and Pattern Trust Scoring evolutions.
Attributes¶
quarantine_status
class-attribute
instance-attribute
¶
quarantine_status = PENDING
Current status in the quarantine lifecycle.
provenance_job_hash
class-attribute
instance-attribute
¶
Hash of the job that first created this pattern.
provenance_sheet_num
class-attribute
instance-attribute
¶
Sheet number where this pattern was first observed.
quarantined_at
class-attribute
instance-attribute
¶
When the pattern was moved to QUARANTINED status.
validated_at
class-attribute
instance-attribute
¶
When the pattern was moved to VALIDATED status.
quarantine_reason
class-attribute
instance-attribute
¶
Reason for quarantine (if quarantined).
trust_score
class-attribute
instance-attribute
¶
Trust score (0.0-1.0). 0.5 is neutral, >0.7 is high trust.
trust_calculation_date
class-attribute
instance-attribute
¶
When trust_score was last calculated.
success_factors
class-attribute
instance-attribute
¶
WHY this pattern succeeds - captured context conditions and factors.
success_factors_updated_at
class-attribute
instance-attribute
¶
When success_factors were last updated.
active
class-attribute
instance-attribute
¶
Whether this pattern is active (False = soft-deleted).
content_hash
class-attribute
instance-attribute
¶
SHA-256 hash of pattern content for cross-name deduplication.
instrument_name
class-attribute
instance-attribute
¶
Backend instrument that produced this pattern (e.g., 'claude_cli').
Functions¶
__post_init__
¶
Clamp scored fields to valid ranges.
Source code in src/marianne/learning/store/models.py
JudgmentClient
¶
HTTP client for Recursive Light judgment API.
Connects to Recursive Light's /api/marianne/judgment endpoint to get TDF-aligned execution decisions with accumulated wisdom.
Falls back to default "retry" action on connection errors for graceful degradation.
Attributes:
| Name | Type | Description |
|---|---|---|
endpoint |
Base URL for the Recursive Light API. |
|
timeout |
Request timeout in seconds. |
Initialize the judgment client.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
endpoint
|
str
|
Base URL for the Recursive Light API server. |
required |
timeout
|
float
|
Request timeout in seconds. Defaults to 30.0. |
30.0
|
Source code in src/marianne/learning/judgment.py
Functions¶
get_judgment
async
¶
Get execution judgment from Recursive Light.
Posts the query to RL's /api/marianne/judgment endpoint and parses the response. On errors, returns a default "retry" action for graceful degradation.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
query
|
JudgmentQuery
|
Full context about the sheet execution state. |
required |
Returns:
| Type | Description |
|---|---|
JudgmentResponse
|
JudgmentResponse with recommended action. On connection |
JudgmentResponse
|
errors, returns default retry action with low confidence. |
Source code in src/marianne/learning/judgment.py
close
async
¶
Close the HTTP client connection.
Should be called when done using the client to clean up resources.
Source code in src/marianne/learning/judgment.py
__aenter__
async
¶
__aexit__
async
¶
Async context manager exit - closes client.
JudgmentQuery
dataclass
¶
JudgmentQuery(job_id, sheet_num, validation_results, execution_history, error_patterns, retry_count, confidence)
Query sent to Recursive Light for execution judgment.
Contains all relevant context about the current sheet execution state needed for RL to provide informed judgment.
JudgmentResponse
dataclass
¶
JudgmentResponse(recommended_action, confidence, reasoning, prompt_modifications=None, escalation_urgency=None, human_question=None, patterns_learned=list())
Response from Recursive Light with execution judgment.
Provides recommended action, reasoning, and optional modifications for how to proceed with sheet execution.
Attributes¶
recommended_action
instance-attribute
¶
Recommended action: - proceed: Continue to next sheet (current succeeded) - retry: Retry the current sheet - completion: Run completion prompt for partial success - escalate: Escalate to human for decision - abort: Stop the entire job
prompt_modifications
class-attribute
instance-attribute
¶
Optional modifications to apply to prompt on retry.
escalation_urgency
class-attribute
instance-attribute
¶
Urgency level if action is 'escalate'.
human_question
class-attribute
instance-attribute
¶
Specific question to ask human if escalating.
patterns_learned
class-attribute
instance-attribute
¶
New patterns identified by RL from this execution.
LocalJudgmentClient
¶
Local fallback judgment client using simple heuristics.
Provides judgment without network calls for offline operation or when Recursive Light is unavailable. Uses confidence thresholds and pass rate heuristics to determine actions.
This is a simpler, always-available alternative that makes reasonable decisions based on local metrics alone.
Attributes:
| Name | Type | Description |
|---|---|---|
proceed_threshold |
Confidence above which to proceed (default 0.7). |
|
retry_threshold |
Confidence above which to retry (default 0.4). |
|
max_retries |
Maximum retries before escalating (default 3). |
Initialize the local judgment client.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
proceed_threshold
|
float
|
Confidence >= this proceeds to next sheet. |
0.7
|
retry_threshold
|
float
|
Confidence >= this (but < proceed) retries. Below this, escalates or aborts. |
0.4
|
max_retries
|
int
|
Maximum retry count before escalating. |
3
|
Source code in src/marianne/learning/judgment.py
Functions¶
get_judgment
async
¶
Get execution judgment using local heuristics.
Decision logic: 1. High confidence (>= proceed_threshold) -> proceed 2. Medium confidence (>= retry_threshold) -> retry (unless max retries) 3. Low confidence (< retry_threshold) -> escalate 4. Max retries exceeded -> completion or escalate based on pass rate
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
query
|
JudgmentQuery
|
Full context about the sheet execution state. |
required |
Returns:
| Type | Description |
|---|---|
JudgmentResponse
|
JudgmentResponse with recommended action based on heuristics. |
Source code in src/marianne/learning/judgment.py
345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 | |
MigrationResult
dataclass
¶
MigrationResult(errors=list(), skipped_workspaces=list(), imported_workspaces=list(), workspaces_found=0, outcomes_imported=0, patterns_detected=0)
Result of a migration operation.
Attributes:
| Name | Type | Description |
|---|---|---|
workspaces_found |
int
|
Number of workspaces with outcomes. |
outcomes_imported |
int
|
Total outcomes imported. |
patterns_detected |
int
|
Patterns detected from imported outcomes. |
errors |
list[str]
|
Any errors encountered during migration. |
skipped_workspaces |
list[str]
|
Workspaces skipped (already imported, etc.). |
imported_workspaces |
list[str]
|
Workspaces successfully imported. |
OutcomeMigrator
¶
Migrates workspace-local outcomes to the global store.
This migrator scans for existing .marianne-outcomes.json files and imports their contents into the global SQLite database, enabling cross-workspace learning from historical data.
Migration is: - Non-destructive: Original files are preserved - Idempotent: Already-imported outcomes are skipped - Pattern-aware: Runs pattern detection after import
Usage
migrator = OutcomeMigrator(global_store) result = migrator.migrate_all() print(f"Imported {result.outcomes_imported} outcomes")
Initialize the outcome migrator.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
global_store
|
GlobalLearningStore
|
Global learning store to import into. |
required |
aggregator
|
PatternAggregator | None
|
Optional pattern aggregator for pattern detection. |
None
|
Source code in src/marianne/learning/migration.py
Functions¶
migrate_all
¶
Migrate all discoverable workspace-local outcomes.
Scans standard locations plus any additional paths for .marianne-outcomes.json files and imports them.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
scan_patterns
|
list[str] | None
|
Glob patterns to scan (defaults to standard locations). |
None
|
additional_paths
|
list[Path] | None
|
Additional specific paths to scan. |
None
|
Returns:
| Type | Description |
|---|---|
MigrationResult
|
MigrationResult with import statistics. |
Source code in src/marianne/learning/migration.py
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 | |
migrate_workspace
¶
Migrate a single workspace's outcomes.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
workspace_path
|
Path
|
Path to the workspace directory. |
required |
Returns:
| Type | Description |
|---|---|
MigrationResult
|
MigrationResult for this workspace. |
Source code in src/marianne/learning/migration.py
JsonOutcomeStore
¶
JSON-file based outcome store implementation.
Stores outcomes in a JSON file with atomic saves, following the same pattern as JsonStateBackend.
Initialize the JSON outcome store.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
store_path
|
Path
|
Path to the JSON file for storing outcomes. |
required |
Source code in src/marianne/learning/outcomes.py
Functions¶
record
async
¶
Record a sheet outcome to the store.
After recording, if there are enough outcomes (>= 5), pattern detection is run and patterns_detected is populated on the outcome.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
outcome
|
SheetOutcome
|
The sheet outcome to record. |
required |
Source code in src/marianne/learning/outcomes.py
query_similar
async
¶
Query for similar past outcomes.
Currently returns recent outcomes for the same job_id. Future: implement semantic similarity matching.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
context
|
dict[str, Any]
|
Context dict containing job_id and other metadata. |
required |
limit
|
int
|
Maximum number of outcomes to return. |
10
|
Returns:
| Type | Description |
|---|---|
list[SheetOutcome]
|
List of similar sheet outcomes. |
Source code in src/marianne/learning/outcomes.py
get_patterns
async
¶
Get detected patterns for a job.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
job_name
|
str
|
The job name to get patterns for. |
required |
Returns:
| Type | Description |
|---|---|
list[str]
|
List of pattern strings detected across outcomes. |
Source code in src/marianne/learning/outcomes.py
detect_patterns
async
¶
Detect patterns from all recorded outcomes.
Uses PatternDetector to analyze historical outcomes and identify recurring patterns that can inform future executions. Results are cached and invalidated when outcomes change (record/load).
Returns:
| Type | Description |
|---|---|
list[DetectedPattern]
|
List of DetectedPattern objects sorted by confidence. |
Source code in src/marianne/learning/outcomes.py
get_relevant_patterns
async
¶
Get pattern descriptions relevant to the given context.
This method detects patterns, matches them to the context, and returns human-readable descriptions suitable for prompt injection.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
context
|
dict[str, Any]
|
Context dict containing job_id, sheet_num, validation_types, etc. |
required |
limit
|
int
|
Maximum number of patterns to return. |
5
|
Returns:
| Type | Description |
|---|---|
list[str]
|
List of pattern description strings for prompt injection. |
Source code in src/marianne/learning/outcomes.py
OutcomeStore
¶
SheetOutcome
dataclass
¶
SheetOutcome(sheet_id, job_id, validation_results, execution_duration, retry_count, completion_mode_used, final_status, validation_pass_rate, success_without_retry, patterns_detected=list(), timestamp=(lambda: now(tz=UTC))(), failure_category_counts=dict(), semantic_patterns=list(), fix_suggestions=list(), patterns_applied=list(), stdout_tail='', stderr_tail='', error_history=list(), grounding_passed=None, grounding_confidence=None, grounding_guidance=None)
Structured outcome data from a completed sheet execution.
This dataclass captures all relevant information about a sheet execution for learning and pattern detection purposes.
Attributes¶
failure_category_counts
class-attribute
instance-attribute
¶
Counts of each failure category from validation results.
Example: {'missing': 2, 'stale': 1, 'malformed': 0} Categories: missing, malformed, incomplete, stale, error
semantic_patterns
class-attribute
instance-attribute
¶
Extracted semantic patterns from failure_reason fields.
Example: ['file not created', 'pattern not found', 'content empty'] These are normalized phrases that can be aggregated across outcomes.
fix_suggestions
class-attribute
instance-attribute
¶
Collected suggested_fix values from failed validations.
Example: ['Ensure file is created in workspace/', 'Add missing import']
patterns_applied
class-attribute
instance-attribute
¶
Pattern descriptions that were applied/injected for this sheet execution.
These are the patterns from get_relevant_patterns() that were included in the prompt. Used for effectiveness tracking: correlate patterns_applied with success_without_retry to measure which patterns actually help.
Example: ['⚠️ Common issue: file_exists validation tends to fail (seen 3x)']
stdout_tail
class-attribute
instance-attribute
¶
Captured tail of stdout from execution.
Stores the last N characters of stdout for pattern extraction. Used by OutputPatternExtractor to detect error patterns.
stderr_tail
class-attribute
instance-attribute
¶
Captured tail of stderr from execution.
Stores the last N characters of stderr for pattern extraction. Used by OutputPatternExtractor to detect error patterns.
error_history
class-attribute
instance-attribute
¶
History of errors encountered during execution.
Each entry is a dict with error_code, category, message, etc. Used by _detect_error_code_patterns() for recurring error analysis.
Example: [{'error_code': 'E009', 'category': 'transient', 'message': 'Rate limited'}]
grounding_passed
class-attribute
instance-attribute
¶
Whether all grounding hooks passed (None if grounding not enabled).
Used to correlate grounding outcomes with validation results and pattern effectiveness over time.
grounding_confidence
class-attribute
instance-attribute
¶
Average confidence across grounding hooks (0.0-1.0, None if not enabled).
Higher confidence means external validators had high certainty in their results. Can be correlated with success_without_retry to identify reliable grounding sources.
grounding_guidance
class-attribute
instance-attribute
¶
Recovery guidance from failed grounding hooks (None if passed or not enabled).
Captures actionable suggestions from external validators that failed, useful for pattern detection and learning what recovery strategies work.
DetectedPattern
dataclass
¶
DetectedPattern(pattern_type, description, frequency=1, success_rate=0.0, last_seen=(lambda: now(tz=UTC))(), context_tags=list(), evidence=list(), confidence=0.5, applications=0, successes_after_application=0, quarantine_status=None, trust_score=None)
A pattern detected from historical outcomes.
Patterns are learned behaviors that can inform future executions. They include both positive patterns (what works) and negative patterns (what to avoid).
v19 Evolution: Extended with optional quarantine_status and trust_score fields for integration with Pattern Quarantine & Trust Scoring features.
Attributes¶
description
instance-attribute
¶
Human-readable description of what this pattern represents.
frequency
class-attribute
instance-attribute
¶
How often this pattern has been observed.
success_rate
class-attribute
instance-attribute
¶
Rate at which this pattern leads to success (0.0-1.0).
last_seen
class-attribute
instance-attribute
¶
When this pattern was last observed (UTC).
context_tags
class-attribute
instance-attribute
¶
Tags for matching: job types, validation types, error categories.
evidence
class-attribute
instance-attribute
¶
Sheet IDs that contributed to detecting this pattern.
confidence
class-attribute
instance-attribute
¶
Confidence in this pattern (0.0-1.0). Higher = more reliable.
applications
class-attribute
instance-attribute
¶
Number of times this pattern was applied (included in prompts).
successes_after_application
class-attribute
instance-attribute
¶
Number of success_without_retry outcomes when this pattern was applied.
quarantine_status
class-attribute
instance-attribute
¶
Quarantine status from global store.
trust_score
class-attribute
instance-attribute
¶
Trust score (0.0-1.0) from global store. None if not from global store.
effectiveness_rate
property
¶
Compute effectiveness rate from applications and successes.
Returns:
| Type | Description |
|---|---|
float
|
Effectiveness rate (0.0-1.0). Returns 0.4 (slightly below neutral) |
float
|
when applications < 3 to prefer proven patterns over unproven ones. |
float
|
This prevents unproven patterns from being treated equally with |
float
|
patterns that have demonstrated moderate (50%) success. |
effectiveness_weight
property
¶
Compute weight for blending effectiveness into relevance scoring.
Uses gradual ramp-up: full weight only after 5 applications. This prevents new patterns from being over-weighted.
Returns:
| Type | Description |
|---|---|
float
|
Weight (0.0-1.0) based on sample size. |
is_quarantined
property
¶
Check if pattern is in quarantine status.
v19 Evolution: Used for quarantine-aware scoring.
is_validated
property
¶
Check if pattern is in validated status.
v19 Evolution: Used for trust-aware scoring.
Functions¶
to_prompt_guidance
¶
Format this pattern as guidance for prompts.
v19 Evolution: Now includes quarantine/trust context when available.
Returns:
| Type | Description |
|---|---|
str
|
A concise string suitable for injection into prompts. |
Source code in src/marianne/learning/patterns.py
PatternApplicator
¶
Applies patterns to modify prompts for better execution.
Takes matched patterns and generates prompt modifications that incorporate learned insights.
Initialize the pattern applicator.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
patterns
|
list[DetectedPattern]
|
List of patterns to apply. |
required |
Source code in src/marianne/learning/patterns.py
Functions¶
generate_prompt_section
¶
Generate a prompt section from patterns.
Returns:
| Type | Description |
|---|---|
str
|
Formatted markdown section for prompt injection. |
Source code in src/marianne/learning/patterns.py
get_pattern_descriptions
¶
Get pattern descriptions as a list of strings.
Returns:
| Type | Description |
|---|---|
list[str]
|
List of pattern guidance strings. |
Source code in src/marianne/learning/patterns.py
PatternDetector
¶
Detects patterns from historical sheet outcomes.
Analyzes a collection of SheetOutcome objects to identify recurring patterns that can inform future executions.
Initialize the pattern detector.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
outcomes
|
list[SheetOutcome]
|
List of historical sheet outcomes to analyze. |
required |
Source code in src/marianne/learning/patterns.py
Functions¶
detect_all
¶
Detect all pattern types from outcomes.
Returns:
| Type | Description |
|---|---|
list[DetectedPattern]
|
List of detected patterns sorted by confidence. |
Source code in src/marianne/learning/patterns.py
calculate_success_rate
staticmethod
¶
Calculate overall success rate from outcomes.
Success is defined as validation_pass_rate == 1.0 (all validations passed). Partial passes (e.g., 0.5) are counted as failures.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
outcomes
|
list[SheetOutcome]
|
List of sheet outcomes. |
required |
Returns:
| Type | Description |
|---|---|
float
|
Success rate as a float (0.0-1.0). |
Source code in src/marianne/learning/patterns.py
PatternMatcher
¶
Matches detected patterns to execution context.
Given a set of detected patterns and a current execution context, finds patterns that are relevant to the current situation.
Initialize the pattern matcher.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
patterns
|
list[DetectedPattern]
|
List of detected patterns to match against. |
required |
Source code in src/marianne/learning/patterns.py
Functions¶
match
¶
Find patterns relevant to the given context.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
context
|
dict[str, Any]
|
Context dict with job_id, sheet_num, validation_types, etc. |
required |
limit
|
int
|
Maximum number of patterns to return. |
5
|
Returns:
| Type | Description |
|---|---|
list[DetectedPattern]
|
List of matching patterns sorted by relevance. |
Source code in src/marianne/learning/patterns.py
PatternType
¶
Bases: Enum
Types of patterns that can be detected from outcomes.
Attributes¶
VALIDATION_FAILURE
class-attribute
instance-attribute
¶
Recurring validation failure pattern (e.g., file not created).
RETRY_SUCCESS
class-attribute
instance-attribute
¶
Pattern where retry succeeds after specific failure.
COMPLETION_MODE
class-attribute
instance-attribute
¶
Pattern where completion mode is effective.
SUCCESS_WITHOUT_RETRY
class-attribute
instance-attribute
¶
Pattern of success without retry (positive pattern).
HIGH_CONFIDENCE
class-attribute
instance-attribute
¶
Pattern with high validation confidence.
LOW_CONFIDENCE
class-attribute
instance-attribute
¶
Pattern with low validation confidence (needs attention).
SEMANTIC_FAILURE
class-attribute
instance-attribute
¶
Pattern detected from semantic failure_reason/failure_category analysis.
These patterns are extracted from the failure_reason and failure_category fields in ValidationResult, providing deeper insight into WHY failures occur. Examples: - 'stale' category appearing frequently (files not modified) - 'file not created' reason appearing across multiple sheets
OUTPUT_PATTERN
class-attribute
instance-attribute
¶
Pattern extracted from stdout/stderr output during execution.
These patterns are detected by analyzing the raw output text for common error signatures, stack traces, and failure indicators. Useful for learning from execution-level failures that may not be captured by validation.
SEMANTIC_INSIGHT
class-attribute
instance-attribute
¶
Pattern generated by LLM-based semantic analysis of sheet completions.
These patterns are produced by the conductor's SemanticAnalyzer, which examines sheet output, validation results, and error history to generate deeper insights about why executions succeed or fail. Unlike statistically detected patterns, semantic insights capture nuanced reasoning about prompt effectiveness, agent behavior, and anti-patterns.
RESOURCE_ANOMALY
class-attribute
instance-attribute
¶
Resource anomaly detected during execution (memory spike, zombie, OOM).
These patterns are produced by the profiler's AnomalyDetector, which runs heuristic checks on each system snapshot. No LLM calls — pure threshold-based detection of memory spikes, runaway processes, zombies, FD exhaustion, and memory pressure.
RESOURCE_CORRELATION
class-attribute
instance-attribute
¶
Learned correlation between resource usage and outcomes.
These patterns are produced by the profiler's CorrelationAnalyzer, which periodically cross-references resource profiles of completed jobs with their outcomes (success/failure, validation results) to identify statistical patterns like high-RSS-predicts-failure.
PatternWeighter
¶
PatternWeighter(*, decay_rate_per_month=DEFAULT_DECAY_RATE_PER_MONTH, effectiveness_threshold=DEFAULT_EFFECTIVENESS_THRESHOLD, epistemic_threshold=DEFAULT_EPISTEMIC_THRESHOLD, min_applications_for_effectiveness=DEFAULT_MIN_APPLICATIONS, frequency_normalization_base=DEFAULT_FREQUENCY_BASE)
Calculates priority scores for patterns.
Implements the combined recency + effectiveness weighting algorithm as specified in the Movement III design document.
The priority formula is
priority = ( effectiveness_score × recency_factor × frequency_factor × (1 - variance) )
Where
- effectiveness_score = successes / (successes + failures + 1)
- recency_factor = (1 - decay_rate) ^ months_since_last_confirmed
- frequency_factor = min(1.0, log(occurrence_count + 1) / log(100))
- variance = std_dev(pattern_application_outcomes)
Initialize the pattern weighter.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
decay_rate_per_month
|
float
|
Fraction of priority lost per month. |
DEFAULT_DECAY_RATE_PER_MONTH
|
effectiveness_threshold
|
float
|
Below this, patterns are deprecated. |
DEFAULT_EFFECTIVENESS_THRESHOLD
|
epistemic_threshold
|
float
|
Variance threshold for learnable patterns. |
DEFAULT_EPISTEMIC_THRESHOLD
|
min_applications_for_effectiveness
|
int
|
Min applications before using actual rate. |
DEFAULT_MIN_APPLICATIONS
|
frequency_normalization_base
|
int
|
Log base for frequency factor. |
DEFAULT_FREQUENCY_BASE
|
Source code in src/marianne/learning/weighter.py
Functions¶
calculate_priority
¶
calculate_priority(occurrence_count, led_to_success_count, led_to_failure_count, last_confirmed, variance=0.0, now=None)
Calculate the priority score for a pattern.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
occurrence_count
|
int
|
How many times this pattern was observed. |
required |
led_to_success_count
|
int
|
Times pattern led to success when applied. |
required |
led_to_failure_count
|
int
|
Times pattern led to failure when applied. |
required |
last_confirmed
|
datetime
|
When this pattern was last confirmed effective. |
required |
variance
|
float
|
Standard deviation of pattern application outcomes. |
0.0
|
now
|
datetime | None
|
Current time for recency calculation (defaults to now). |
None
|
Returns:
| Type | Description |
|---|---|
float
|
Priority score from 0.0 to 1.0. |
Source code in src/marianne/learning/weighter.py
calculate_effectiveness
¶
Calculate effectiveness score from success/failure counts.
Uses Laplace smoothing (add-one) to handle cold start: effectiveness = (successes + 0.5) / (successes + failures + 1)
This gives a slightly optimistic prior (0.5) for patterns with no application history.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
success_count
|
int
|
Number of successful outcomes after application. |
required |
failure_count
|
int
|
Number of failed outcomes after application. |
required |
Returns:
| Type | Description |
|---|---|
float
|
Effectiveness score from 0.0 to 1.0. |
Source code in src/marianne/learning/weighter.py
calculate_recency_factor
¶
Calculate recency factor with exponential decay.
Formula: recency = (1 - decay_rate) ^ months_since_last_confirmed
With default 10% decay, a pattern loses: - 10% after 1 month - 19% after 2 months - 27% after 3 months - 35% after 4 months - etc.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
last_confirmed
|
datetime
|
When the pattern was last confirmed effective. |
required |
now
|
datetime | None
|
Current time (defaults to now). |
None
|
Returns:
| Type | Description |
|---|---|
float
|
Recency factor from 0.0 to 1.0. |
Source code in src/marianne/learning/weighter.py
calculate_frequency_factor
¶
Calculate frequency factor from occurrence count.
Formula: frequency = max(floor, min(1.0, log(count + 1) / log(100)))
This gives diminishing returns for high counts: - 1 occurrence: max(0.6, ~0.15) = 0.6 (floored) - 10 occurrences: max(0.6, ~0.52) = 0.6 (floored) - 50 occurrences: max(0.6, ~0.85) = 0.85 - 100+ occurrences: 1.0
The floor prevents single-occurrence patterns from being crushed below the exploitation query threshold. See issue #101 and the matching FREQUENCY_FACTOR_FLOOR in patterns_crud.py.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
occurrence_count
|
int
|
How many times the pattern was observed. |
required |
Returns:
| Type | Description |
|---|---|
float
|
Frequency factor from |
Source code in src/marianne/learning/weighter.py
is_deprecated
¶
Check if a pattern should be deprecated due to low effectiveness.
A pattern is deprecated if: 1. It has enough application data (>= min_applications) 2. Its effectiveness is below the threshold
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
led_to_success_count
|
int
|
Times pattern led to success. |
required |
led_to_failure_count
|
int
|
Times pattern led to failure. |
required |
Returns:
| Type | Description |
|---|---|
bool
|
True if pattern should be deprecated. |
Source code in src/marianne/learning/weighter.py
classify_uncertainty
¶
Classify the uncertainty type of a pattern.
Epistemic uncertainty (variance < threshold): The pattern behavior can be learned with more data. Keep tracking and applying.
Aleatoric uncertainty (variance >= threshold): The pattern has inherently unpredictable outcomes. Deprioritize but don't remove.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
variance
|
float
|
The variance of pattern application outcomes. |
required |
Returns:
| Type | Description |
|---|---|
str
|
'epistemic' or 'aleatoric' |
Source code in src/marianne/learning/weighter.py
calculate_variance
¶
Calculate variance from a list of boolean outcomes.
Used to track consistency of pattern applications.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
outcomes
|
list[bool]
|
List of True (success) / False (failure) outcomes. |
required |
Returns:
| Type | Description |
|---|---|
float
|
Variance from 0.0 (all same) to 0.25 (50/50 split). |
Source code in src/marianne/learning/weighter.py
Functions¶
aggregate_job_outcomes
¶
Convenience function to aggregate outcomes after job completion.
This is the main entry point for the aggregation system.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
outcomes
|
list[SheetOutcome]
|
List of sheet outcomes from the completed job. |
required |
workspace_path
|
Path
|
Path to the workspace for hashing. |
required |
global_store
|
GlobalLearningStore | None
|
Optional global store (uses default if None). |
None
|
model
|
str | None
|
Optional model name used for execution. |
None
|
Returns:
| Type | Description |
|---|---|
AggregationResult
|
AggregationResult with statistics. |
Source code in src/marianne/learning/aggregator.py
record_error_recovery
¶
Record an error recovery to the global store.
Convenience function for use in the runner when a recovery is attempted.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
global_store
|
GlobalLearningStore | None
|
Global learning store (no-op if None). |
required |
error
|
ClassifiedError | ClassificationResult
|
The error that was recovered from. |
required |
actual_wait
|
float
|
Actual time waited in seconds. |
required |
success
|
bool
|
Whether recovery succeeded. |
required |
model
|
str | None
|
Optional model name. |
None
|
Source code in src/marianne/learning/error_hooks.py
get_global_store
¶
Get or create the global learning store singleton.
This function provides a convenient singleton accessor for the GlobalLearningStore. It ensures only one store instance exists per database path, avoiding the overhead of creating multiple connections to the same SQLite database.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
db_path
|
Path | None
|
Optional custom database path. If None, uses the default path at ~/.marianne/global-learning.db. |
None
|
Returns:
| Type | Description |
|---|---|
GlobalLearningStore
|
The GlobalLearningStore singleton instance. |
Example
store = get_global_store() # Uses default path store = get_global_store(Path("/custom/path.db")) # Custom path
Source code in src/marianne/learning/store/__init__.py
check_migration_status
¶
Check the current migration status.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
global_store
|
GlobalLearningStore
|
Global learning store to check. |
required |
Returns:
| Type | Description |
|---|---|
dict[str, Any]
|
Dictionary with migration status information. |
Source code in src/marianne/learning/migration.py
migrate_existing_outcomes
¶
Convenience function to migrate all existing outcomes.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
global_store
|
GlobalLearningStore
|
Global learning store to import into. |
required |
scan_patterns
|
list[str] | None
|
Optional custom scan patterns. |
None
|
additional_paths
|
list[Path] | None
|
Optional additional paths to scan. |
None
|
Returns:
| Type | Description |
|---|---|
MigrationResult
|
MigrationResult with import statistics. |
Source code in src/marianne/learning/migration.py
calculate_priority
¶
calculate_priority(occurrence_count, led_to_success_count, led_to_failure_count, last_confirmed, variance=0.0)
Convenience function to calculate priority without creating a weighter.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
occurrence_count
|
int
|
How many times this pattern was observed. |
required |
led_to_success_count
|
int
|
Times pattern led to success when applied. |
required |
led_to_failure_count
|
int
|
Times pattern led to failure when applied. |
required |
last_confirmed
|
datetime
|
When this pattern was last confirmed effective. |
required |
variance
|
float
|
Standard deviation of pattern application outcomes. |
0.0
|
Returns:
| Type | Description |
|---|---|
float
|
Priority score from 0.0 to 1.0. |