Skip to main content
The Run Orchestration & Application Layer is the central execution engine of the Palyra daemon (palyrad). It manages the lifecycle of agent runs, transforms raw user input into model-ready prompts through context augmentation, handles the bidirectional streaming of events, and manages post-run maintenance such as session compaction and learning.

Run Stream Pipeline

Agent execution is modeled as a “Run Stream,” a stateful pipeline that coordinates between the user (via gRPC or HTTP), the model provider, and the system’s internal tools.

Execution Flow

  1. Initialization: A RunStreamRequest is received. The system resolves the OrchestratorSessionRecord crates/palyra-daemon/src/transport/http/handlers/console/chat.rs#40-69.
  2. Input Assembly: The prepare_model_provider_input function gathers attachments, context references, and performs RAG (Retrieval-Augmented Generation) to build the final prompt crates/palyra-daemon/src/application/provider_input.rs#53-64.
  3. Provider Execution: The orchestrated loop calls execute_model_provider crates/palyra-daemon/src/application/run_stream/orchestration.rs#147-183.
  4. Event Processing: Model outputs (text chunks, tool calls) are processed by process_run_stream_provider_events crates/palyra-daemon/src/application/run_stream/orchestration.rs#13-15.
  5. Finalization: The run transitions to Done, heartbeats are cleared, and post-run reflection is scheduled crates/palyra-daemon/src/application/run_stream/orchestration.rs#107-144.

Run State Machine

The RunStateMachine tracks the lifecycle of a single execution unit using the RunLifecycleState enum crates/palyra-daemon/src/application/run_stream/orchestration.rs#29-29.
StateDescription
AcceptedRun created and queued.
InProgressModel provider is actively generating or tools are executing.
DoneRun completed successfully.
CancelledExecution stopped by user or system timeout.
FailedTerminal error encountered during execution.
Sources: crates/palyra-daemon/src/application/run_stream/orchestration.rs#107-144, crates/palyra-daemon/src/application/run_stream/orchestration.rs#198-203.

Provider Input Assembly & Context

Before a request is sent to an LLM, the Application Layer assembles a PreparedModelProviderInput crates/palyra-daemon/src/application/provider_input.rs#42-45. This process integrates multiple context sources: Sources: crates/palyra-daemon/src/application/provider_input.rs#42-156, crates/palyra-daemon/src/application/provider_input.rs#159-203.

Session Compaction

To maintain long-running conversations without exceeding LLM context windows, Palyra employs a session_window_v1 strategy crates/palyra-daemon/src/application/session_compaction.rs#31-31.

Compaction Logic

  1. Eligibility: Triggered when token usage exceeds AUTO_SESSION_COMPACTION_MIN_INPUT_TOKENS (default 480) crates/palyra-daemon/src/application/provider_input.rs#37-37.
  2. Condensation: The system selects older OrchestratorTapeRecord entries and generates a summary_text crates/palyra-daemon/src/application/session_compaction.rs#76-94.
  3. Checkpointing: Creates an OrchestratorCheckpointRecord to allow “time-travel” or restoration of the pre-compacted state crates/palyra-daemon/src/application/session_compaction.rs#142-147.
  4. Durable Writes: Significant facts or procedures identified during compaction are written to the Workspace via apply_workspace_managed_block crates/palyra-daemon/src/application/session_compaction.rs#13-19.
Sources: crates/palyra-daemon/src/application/session_compaction.rs#31-147, crates/palyra-daemon/src/application/provider_input.rs#35-40.

Learning Runtime & Background Queue

The daemon runs a background supervisor that handles asynchronous maintenance and “learning” tasks.

Background Queue Loop

The spawn_background_queue_loop initializes a task worker that polls the JournalStore for pending OrchestratorBackgroundTaskRecord entries crates/palyra-daemon/src/background_queue.rs#31-44.

Task Processing

Sources: crates/palyra-daemon/src/background_queue.rs#31-142.

Architectural Diagrams

Run Orchestration Data Flow

This diagram bridges the Natural Language Space (User Intent) to the Code Entity Space (Orchestration logic). Sources: crates/palyra-daemon/src/transport/http/handlers/console/chat.rs#139-183, crates/palyra-daemon/src/application/provider_input.rs#53-64, crates/palyra-daemon/src/application/run_stream/orchestration.rs#107-113.

Session Compaction & Persistence

This diagram maps the logical compaction process to the underlying code entities. Sources: crates/palyra-daemon/src/application/session_compaction.rs#76-94, crates/palyra-daemon/src/application/session_compaction.rs#129-147, crates/palyra-daemon/src/application/session_compaction.rs#13-27.

Key Components Reference

ComponentCode EntityResponsibility
Run Loopexecute_run_stream_provider_requestManages the async future for LLM calls and handles cancellation polling crates/palyra-daemon/src/application/run_stream/orchestration.rs#147-183.
Input BuilderPrepareModelProviderInputRequestStruct containing all data needed to build a context-aware prompt crates/palyra-daemon/src/application/provider_input.rs#53-64.
Background Workerpoll_background_queueFetches and dispatches pending background tasks from the journal crates/palyra-daemon/src/background_queue.rs#46-93.
Compaction StrategySESSION_COMPACTION_STRATEGYDefines how the session window is calculated and condensed crates/palyra-daemon/src/application/session_compaction.rs#31-31.
Sources: crates/palyra-daemon/src/application/run_stream/orchestration.rs, crates/palyra-daemon/src/application/provider_input.rs, crates/palyra-daemon/src/background_queue.rs, crates/palyra-daemon/src/application/session_compaction.rs.