Skip to main content

8. Integrity & Repair System

Batho's integrity subsystem provides a comprehensive, automated check-and-repair pipeline for the Arrow Bundle artifact database. It detects corruption, validates data structures, and repairs issues where possible — all exposed through the batho fix CLI command.

8.1 Fix Engine Architecture​

The fix engine orchestrates a four-phase check pipeline with optional parallel execution and automatic repair:

Figure 30: Integrity Check & Repair Pipeline — Four-phase pipeline from bundle health through graph sync, with automatic repair dispatch and multi-format reporting.

Fix Context​

The fix context is the shared state object passed to all checkers and repairers:

PropertyDescription
Root pathRepository root path
Bundle instanceActive Arrow Bundle
Deep modeDecompress and validate every blob (slow)
Dry runCheck only, do not perform repairs
Audit logAppend-only audit trail
Session IDUnique identifier for this fix session

It lazily loads index runs and the latest run from the bundle, caching results across all checkers.


8.2 Data Models​

Severity Levels​

LevelDescriptionAction
CRITICALData loss riskImmediate fix required
ERRORCorruption detectedAuto-fix attempted
WARNINGAnomaly detectedMay be transient
INFOFYINo action needed

Check Status​

StatusDescription
PASSEDNo issues found
FAILEDIssues detected, not repaired
FIXEDIssues detected and automatically repaired
SKIPPEDCheck skipped (e.g., empty database)

Issue​

Each issue captures a single integrity problem with its type, severity, affected table, identifier, description, repair strategy, and whether it is auto-fixable.


8.3 Checkers​

Phase 1: Bundle Health Checker​

The bundle health checker verifies the structural integrity of the Arrow Bundle artifact directory:

  • meta.json validity: Ensures active_files entries exist and are parseable.
  • Active IPC files: Verifies all referenced .vN.ipc files exist on disk.
  • Schema version: Checks BUNDLE_SCHEMA_VERSION matches expected version.

Phase 2: State Consistency Checker​

The state consistency checker validates relational consistency and state anomalies:

  • Stuck runs: Finds runs marked running for more than 24 hours, or stale due to process termination. Checks for inter-process lock conflicts.
  • File tracking desync: Detects files in file_tracking that no longer exist on disk.
  • Orphaned strings: Identifies string_dict entries not referenced by any table.

Phase 3: Blob Integrity Checker​

The blob integrity checker validates compressed blob data in the database:

  • zstd magic header: Verifies blob starts with 0x28B52FFD (zstd magic number).
  • Decompression test (deep mode): Fully decompresses and validates JSON payload.
  • JSON validity (deep mode): Parses decompressed content and validates JSON structure.

Phase 4: Graph Sync Checker​

The graph sync checker verifies hypergraph entity index synchronization:

  • Entity sync: Compares BSG scratch-store entities against bundle agent_view rows.
  • Dangling references: Detects relationships pointing to non-existent entities.
  • Cross-reference validation: Ensures file_id mappings are consistent across tables.

8.4 Repairers​

Each checker is paired with a repairer that can automatically fix detected issues:

Blob Repairer​

Repair StrategyAction
delete_corrupt_file_artifactRemove corrupted file artifact row from bundle
clear_corrupt_run_artifactNull out corrupted JSON column in run_artifacts
delete_corrupt_changelogRemove corrupted changelog entry

Graph Repairer​

Repair StrategyAction
resolve_danglingAttempt to resolve dangling references via the shared Arrow current/ store
delete_invalid_relationshipRemove relationship with non-existent target entity

State Repairer​

Repair StrategyAction
fail_stuck_runMark stuck run as failed with error message
delete_orphaned_stringNo-op (strings are lazily cleaned during compaction)
reset_file_trackingReset is_indexed flag for desynced file tracking rows

8.5 Checks Framework​

The checks framework defines a protocol-based interface for extensible integrity checks:

class IntegrityCheck(Protocol):
name: str
description: str
def run(self, ctx: FixContext) -> CheckResult: ...
def supports_quick_mode(self) -> bool: ...

Registered checks:

CheckDescription
Database integrityDatabase-level structural validation
Index integrityIndex consistency and coverage validation
BSG integrityBSG view integrity and completeness
View integrityArrow IPC view schema and data validation

Each check returns a result with findings, metrics, and duration, enabling the engine to aggregate results across all phases.


8.6 Report Generation​

The report generator produces integrity reports in three formats:

FormatPurposeOutput
textHuman-readable console outputColored summary with issue details
jsonMachine-readable for CI/CD integrationStructured FixReport JSON
csvSpreadsheet analysisOne row per finding

The fix report captures the complete fix session:

FieldDescription
TimestampsSession start and completion times
PathsRepository and artifact paths
ModeDry-run or fix
SummaryCounts by severity level
Check resultsPer-phase check reports
RepairsAttempted repair results
Findings by severityCount of findings at each severity level

8.7 CLI Interface​

# Dry-run: check only, no repairs
batho fix --dry-run

# Deep mode: decompress and validate every blob
batho fix --deep

# Target specific phase
batho fix --target blobs
batho fix --phase 3

# Run checks in parallel
batho fix --parallel

# Output report as JSON to file
batho fix --format json --output report.json

CLI Flags​

FlagDescription
--dry-runCheck only, do not perform repairs
--deepDecompress and validate every blob (slow)
--targetRun specific checker: db, state, blobs, graph, all
--phaseRun specific phase (1–4)
--parallelRun independent checks in parallel
--formatReport format: text, json, csv
--outputWrite report to file instead of stdout