grounding
grounding
¶
External grounding hooks for external validation of sheet outputs.
Provides a mechanism for validating sheet outputs against external sources (APIs, databases, file checksums, etc.) to prevent model drift and ensure output quality beyond internal validation.
This addresses the mathematical necessity of external validators documented in arXiv 2601.05280 (entropy decay in self-training) and DGM objective hacking.
Attributes¶
Classes¶
GroundingPhase
¶
Bases: str, Enum
When the grounding hook should execute relative to validation.
GroundingContext
dataclass
¶
GroundingContext(job_id, sheet_num, prompt, output, output_files=list(), validation_passed=None, validation_details=list(), metadata=dict())
Context provided to grounding hooks for validation.
Contains all relevant information about the sheet execution that the hook can use to perform external validation.
Attributes¶
output_files
class-attribute
instance-attribute
¶
List of file paths created/modified by the sheet.
validation_passed
class-attribute
instance-attribute
¶
Result of internal validation (None if pre_validation phase).
validation_details
class-attribute
instance-attribute
¶
Detailed validation results from internal engine.
metadata
class-attribute
instance-attribute
¶
Additional context metadata (e.g., config values, timing).
GroundingResult
dataclass
¶
GroundingResult(passed, hook_name, message='', confidence=1.0, details=dict(), timestamp=(lambda: now(UTC))(), recovery_guidance=None, should_escalate=False)
Result from a grounding hook execution.
Contains the validation outcome and optional guidance for recovery.
Attributes¶
confidence
class-attribute
instance-attribute
¶
Confidence in the grounding result (0.0-1.0).
details
class-attribute
instance-attribute
¶
Additional details about the validation.
timestamp
class-attribute
instance-attribute
¶
When the grounding check was performed.
recovery_guidance
class-attribute
instance-attribute
¶
Optional guidance for what to do on failure.
should_escalate
class-attribute
instance-attribute
¶
Whether this failure should trigger escalation (if available).
GroundingHook
¶
Bases: Protocol
Protocol for external grounding hooks.
Implementations can check outputs against external sources like: - API endpoints for data freshness - File checksums for artifact integrity - External validators for output quality - Database queries for data consistency
Attributes¶
Functions¶
validate
async
¶
Perform external validation on the sheet output.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
context
|
GroundingContext
|
Full context about the sheet execution. |
required |
Returns:
| Type | Description |
|---|---|
GroundingResult
|
GroundingResult with pass/fail status and details. |
Source code in src/marianne/execution/grounding.py
FileChecksumGroundingHook
¶
FileChecksumGroundingHook(expected_checksums=None, checksum_algorithm='sha256', name=None, *, allowed_root=None)
Example grounding hook that validates file checksums.
Checks that output files have expected checksums, preventing model from overwriting important files incorrectly.
Initialize the file checksum hook.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
expected_checksums
|
dict[str, str] | None
|
Map of file path to expected checksum. |
None
|
checksum_algorithm
|
Literal['md5', 'sha256']
|
Algorithm to use for checksums. |
'sha256'
|
name
|
str | None
|
Custom name for this hook (defaults to "file_checksum"). |
None
|
allowed_root
|
str | Path | None
|
If set, all file paths must resolve within this
directory. Rejects absolute paths and |
None
|
Source code in src/marianne/execution/grounding.py
Functions¶
validate
async
¶
Validate file checksums match expected values.
Source code in src/marianne/execution/grounding.py
GroundingEngine
¶
Engine for executing grounding hooks.
Coordinates multiple hooks and aggregates their results.
Initialize the grounding engine.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
hooks
|
list[GroundingHook] | None
|
List of grounding hooks to execute. |
None
|
config
|
GroundingConfig | None
|
Configuration for grounding behavior (from core.config). |
None
|
Source code in src/marianne/execution/grounding.py
Functions¶
add_hook
¶
get_hook_count
¶
run_hooks
async
¶
Run all hooks matching the specified phase.
Includes a circuit breaker: after CIRCUIT_BREAKER_THRESHOLD consecutive failures (timeout or exception), remaining hooks are skipped to avoid wasting O(hooks x timeout) when an external service is down.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
context
|
GroundingContext
|
Context for grounding validation. |
required |
phase
|
GroundingPhase
|
Which phase to run hooks for. |
required |
Returns:
| Type | Description |
|---|---|
list[GroundingResult]
|
List of GroundingResult from all matching hooks. |
Source code in src/marianne/execution/grounding.py
274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 | |
aggregate_results
¶
Aggregate multiple grounding results into overall status.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
results
|
list[GroundingResult]
|
List of grounding results to aggregate. |
required |
Returns:
| Type | Description |
|---|---|
tuple[bool, str]
|
Tuple of (overall_passed, summary_message). |
Source code in src/marianne/execution/grounding.py
Functions¶
create_hook_from_config
¶
Factory function to create a grounding hook from configuration.
This is the integration point for hook registration. The factory reads hook configuration from YAML and instantiates the appropriate hook class.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
hook_config
|
GroundingHookConfig
|
Configuration for the hook (from GroundingConfig.hooks). |
required |
Returns:
| Type | Description |
|---|---|
GroundingHook
|
An instantiated GroundingHook ready for registration. |
Raises:
| Type | Description |
|---|---|
ValueError
|
If hook type is unknown. |
Example
from marianne.core.config import GroundingHookConfig config = GroundingHookConfig( type="file_checksum", expected_checksums={"output.txt": "abc123..."}, ) hook = create_hook_from_config(config) grounding_engine.add_hook(hook)