Skip to main content
Background services in Palyra provide the asynchronous execution layer for long-running tasks, periodic maintenance, and autonomous system recovery. This subsystem ensures that the core daemon remains responsive while handling complex operations like AI-driven reflection, device pairing, and self-healing diagnostics.

Background Queue & Task Orchestration

The background_queue provides a durable execution mechanism for tasks that should not block the main request-response cycle. It is managed by a dedicated loop that polls the JournalStore for pending OrchestratorBackgroundTaskRecord entries.

Task Lifecycle

  1. Scheduling: Tasks are created via create_orchestrator_background_task crates/palyra-daemon/src/application/learning.rs#90-120.
  2. Polling: The spawn_background_queue_loop crates/palyra-daemon/src/background_queue.rs#31-44 runs every 3 seconds (defined by BACKGROUND_QUEUE_IDLE_SLEEP crates/palyra-daemon/src/background_queue.rs#28) to fetch tasks.
  3. Execution: Tasks are dispatched based on their task_kind. For example, post_run_reflection triggers the AI learning pipeline crates/palyra-daemon/src/background_queue.rs#12-26.
  4. Heartbeating: During execution, the queue records a WorkHeartbeatUpdate crates/palyra-daemon/src/background_queue.rs#108-112 to signal to the Self-Healing system that progress is being made.

Data Flow: Background Task Dispatch

StepEntityActionCode Reference
1GatewayRuntimeStatelist_orchestrator_background_taskscrates/palyra-daemon/src/background_queue.rs#51-60
2BackgroundQueueprocess_background_taskcrates/palyra-daemon/src/background_queue.rs#95-101
3SelfHealingrecord_self_healing_heartbeatcrates/palyra-daemon/src/background_queue.rs#108-112
4JournalStoreupdate_orchestrator_background_taskcrates/palyra-daemon/src/background_queue.rs#71-89
Sources: crates/palyra-daemon/src/background_queue.rs#28-112, crates/palyra-daemon/src/application/learning.rs#90-120

Self-Healing & Diagnostics

The Self-Healing system monitors the health of daemon operations and automatically attempts remediation when incidents are detected. It relies on heartbeats from active work units (Runs, Background Tasks, Channel Workers).

Implementation Entities

The Doctor System

The “Doctor” system provides diagnostic checks and automated recovery. It is exposed via the CLI and the Console Operations section. Sources: crates/palyra-daemon/src/gateway/runtime.rs#28-33, crates/palyra-cli/src/commands/status.rs#39-57, crates/palyra-daemon/src/background_queue.rs#102-112

Node Runtime & Device Pairing

The NodeRuntimeState manages the lifecycle of external nodes (CLI, Desktop, or other Daemons) connecting to the central gateway. This includes the secure mTLS-backed pairing flow.

Pairing Flow (mTLS)

Palyra uses a multi-step pairing process to establish trust:
  1. Initiation: A node requests pairing via PairingMethod (Pin or QR) crates/palyra-daemon/src/node_runtime.rs#24-45.
  2. Approval: The daemon creates an ApprovalPromptRecord for the operator to review in the Console crates/palyra-daemon/src/node_rpc.rs#187-200.
  3. Verification: Upon approval, the VerifiedPairing material is generated, and the node receives a client certificate crates/palyra-daemon/src/node_runtime.rs#106-121.
  4. Enforcement: Subsequent gRPC calls require the certificate fingerprint to match the device_id crates/palyra-daemon/src/node_rpc.rs#99-129.

Node RPC Entity Mapping

The following diagram bridges the logical pairing process to the internal code structures. Diagram: Node Pairing & RPC Verification Sources: crates/palyra-daemon/src/node_runtime.rs#106-156, crates/palyra-daemon/src/node_rpc.rs#99-129

Learning & Reflection Loop

The Learning subsystem is a specialized background service that analyzes completed runs to extract “durable facts,” “preferences,” and “procedures.”

Post-Run Reflection

When a run completes, finalize_run_stream_after_provider_response crates/palyra-daemon/src/application/run_stream/orchestration.rs#107-113 triggers the learning pipeline.
  1. Sampling: The system checks LearningRuntimeConfig to see if the run should be analyzed based on sampling_percent crates/palyra-daemon/src/application/learning.rs#47-64.
  2. Task Creation: A background task of kind post_run_reflection is queued crates/palyra-daemon/src/application/learning.rs#30-46.
  3. Candidate Generation: The task processes the run transcript to generate LearningCandidateRecord entries for facts or procedures crates/palyra-daemon/src/application/learning.rs#158-185.
  4. Auto-Application: If a candidate exceeds the durable_fact_auto_write_threshold_bps, it is automatically committed to the memory store crates/palyra-daemon/src/gateway/runtime.rs#85-96.

Learning Configuration

ParameterDescriptionCode Reference
sampling_percentPercentage of runs analyzedcrates/palyra-daemon/src/gateway/runtime.rs#87
cooldown_msMinimum time between reflectionscrates/palyra-daemon/src/gateway/runtime.rs#88
max_candidatesMax candidates per runcrates/palyra-daemon/src/gateway/runtime.rs#90
Sources: crates/palyra-daemon/src/application/learning.rs#30-185, crates/palyra-daemon/src/gateway/runtime.rs#85-96, crates/palyra-daemon/src/application/run_stream/orchestration.rs#107-144

System Health & Maintenance

The daemon performs periodic maintenance on the MemoryStore and Journal via the memory_maintenance_status crates/palyra-daemon/src/transport/http/handlers/console/memory.rs#11-20.

Maintenance Tasks

Diagram: Background Maintenance Loop Sources: crates/palyra-daemon/src/background_queue.rs#31-112, crates/palyra-daemon/src/transport/http/handlers/console/memory.rs#11-53, crates/palyra-daemon/src/application/run_stream/cancellation.rs#33-47