Self-Sanitation

PiSovereign’s Automated Self-Sanitation phase runs during every dream cycle, positioned between NREM (consolidation) and REM (creative synthesis). It autonomously detects and repairs degradation across six subsystems without manual intervention.

Architecture

Dream Cycle Flow
────────────────
  NREM (consolidation)
    ↓
  SANITATION (this feature)
    ↓
  REM (creative synthesis)

The sanitation phase takes a snapshot of system state before any changes. If a critical failure occurs during processing, the service performs an automatic rollback to the snapshot, ensuring that a failed sanitation run never leaves the system in a worse state.

Six Subsystems

1. Memory Hygiene

Scans all active memories in configurable batch sizes, sending each batch to the LLM for verdict classification:

VerdictAction
okNo action
staleQuarantined with reason
poisonedQuarantined immediately
contradictedLosing memory quarantined, winner kept

Quarantined memories receive a MemoryType::Quarantined classification and are automatically purged after the configured TTL (default: 7 days).

2. Knowledge Graph Integrity

  • Detects orphan nodes (no incoming or outgoing edges)
  • Detects dangling edges (source or target node missing)
  • Counts total structural issues

3. Adversarial Defense Evolution

When integrated with the red-team service, analyzes recent red-team results (bypasses) and:

  • Extracts attack patterns from successful bypass payloads
  • Generates DynamicDefensePattern entries with normalized regex patterns
  • Deduplicates against existing active patterns via hash-based lookup

4. Config Drift Correction

Compares current SanitationConfig parameters against observed system behavior and adjusts within safe bounds:

  • quarantine_ttl_days: floor 1, ceiling 30
  • memory_batch_size: floor 5, ceiling 50

Adjustments are logged via the event bus.

5. Quality Regression Detection

Computes a composite quality score from three weighted signals:

score = 1.0 - (re_ask_rate × W_re_ask + correction_rate × W_correction + abandonment_rate × W_abandon)

Compares against the previous sanitation result. Score drops trigger QualityRegression domain events. The quality trend is classified as Improving, Stable, or Degrading.

6. Behavioral Anomaly Detection

Monitors for anomalous patterns:

  • Latency spikes: p99 latency exceeding N× the rolling average
  • Error rate surges: error fraction above threshold

High-severity anomalies can trigger an automatic process restart with exponential backoff.

Configuration

All parameters live under [dream.sanitation] in config.toml:

ParameterTypeDefaultDescription
enabledbooltrueEnable the sanitation phase
quarantine_ttl_daysu327Days before quarantined memories are purged
memory_batch_sizeusize10Memories per LLM evaluation batch
max_restart_countu324Max restarts before giving up
restart_backoff_base_secsu6430Base delay for exponential backoff
anomaly_window_secsu64300Anomaly detection time window
latency_spike_multiplierf322.0p99 latency spike threshold
error_rate_thresholdf320.05Error rate alarm threshold
quality_re_ask_weightf320.4Re-ask rate weight in quality score
quality_correction_weightf320.4Correction rate weight
quality_abandonment_weightf320.2Abandonment rate weight

Quarantine Lifecycle

Memory detected as suspect
  ↓
MemoryType set to Quarantined
  ↓
quarantined_at timestamp recorded
  ↓
(7 days pass — configurable via quarantine_ttl_days)
  ↓
NREM phase purges expired quarantined memories

Prometheus Metrics

All metrics are exposed at /metrics with the pisovereign_sanitation_ prefix:

MetricTypeDescription
pisovereign_sanitation_memories_quarantinedcounterTotal memories quarantined
pisovereign_sanitation_contradictions_resolvedcounterContradictions resolved
pisovereign_sanitation_graph_orphanscounterGraph orphan nodes detected
pisovereign_sanitation_defense_patterns_addedcounterNew defense patterns created
pisovereign_sanitation_defense_score_x100gaugeDefense score × 100
pisovereign_sanitation_quality_score_x100gaugeQuality score × 100
pisovereign_sanitation_anomalies_detectedcounterBehavioral anomalies detected
pisovereign_sanitation_rollbackscounterSnapshot rollbacks performed

API Endpoints

GET /v1/sanitation/latest

Returns the most recent sanitation result for the current user.

GET /v1/sanitation/results/{session_id}

Returns all sanitation results for a specific dream session.

GET /v1/sanitation/defense-patterns

Lists all active dynamic defense patterns.

DELETE /v1/sanitation/defense-patterns/{id}

Deactivates a specific defense pattern by ID.

GET /v1/sanitation/quality

Returns the current quality assessment including composite score and trend direction.

Troubleshooting

Quarantined memories you want to keep: Currently, quarantined memories must be restored via direct database update before the TTL expires. Set memory_type = 'fact' (or the original type) and clear quarantined_at / quarantine_reason.

Defense score declining: Check GET /v1/sanitation/defense-patterns for recently added patterns. Patterns with low effectiveness can be deactivated via the DELETE endpoint.

Quality score regression: Inspect GET /v1/sanitation/quality for the recommendations array. Common causes include increased re-ask rates (unclear responses) or high correction rates (factual errors from stale memories).