error_hooks
error_hooks
¶
Error learning hooks for integration with ErrorClassifier.
This module implements error learning as designed in Movement III: - Extend ErrorClassifier with learning hooks (CV 0.82) - Track error patterns globally - Learn adaptive wait times based on actual recovery success - Integrate with existing ErrorClassifier without major refactoring
Error Learning Hook Integration Points: 1. on_error_classified: Called when an error is classified - Records error occurrence with context - Queries similar past errors for suggested_wait adjustment
- on_error_recovered: Called when recovery after waiting succeeds
- Records actual_wait and recovery_success to error_recoveries
-
Updates learned wait times
-
on_auth_failure: Distinguishes transient vs permanent auth failures
Classes¶
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
Functions¶
wrap_classifier_with_learning
¶
Wrap an ErrorClassifier with learning hooks.
This is a convenience function that creates learning hooks and returns them alongside the classifier for easy integration.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
classifier
|
ErrorClassifier
|
The ErrorClassifier to wrap. |
required |
global_store
|
GlobalLearningStore | None
|
Global learning store for persistence. |
None
|
Returns:
| Type | Description |
|---|---|
tuple[ErrorClassifier, ErrorLearningHooks]
|
Tuple of (classifier, hooks) for use in runner. |
Source code in src/marianne/learning/error_hooks.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
|