Skip to main content
The Application Layer in Palyra serves as the central nervous system of the daemon, orchestrating the flow between model providers, tool execution, session persistence, and autonomous learning. It is primarily responsible for the RunStream lifecycle, transforming raw gRPC requests into managed agentic operations.

Orchestration & RunStream Lifecycle

The orchestration logic in crates/palyra-daemon/src/application/run_stream/orchestration.rs manages the state machine of a “Run”. It coordinates model provider requests, handles streaming events, and ensures that the system remains responsive to cancellation signals.

Execution Flow

  1. Input Preparation: The orchestrator calls prepare_model_provider_input to aggregate session history, context references, and system prompts crates/palyra-daemon/src/application/run_stream/orchestration.rs#16-18.
  2. Usage Routing: It calculates the UsageRoutingPlan to determine which model provider should handle the request based on governance policies crates/palyra-daemon/src/application/run_stream/orchestration.rs#32.
  3. Provider Execution: The execute_run_stream_provider_request function wraps the model provider call in a loop that polls for orchestrator cancellation every 100ms crates/palyra-daemon/src/application/run_stream/orchestration.rs#147-159.
  4. Event Processing: Provider outputs are processed via process_run_stream_provider_events, which handles tool calls, content generation, and metadata updates crates/palyra-daemon/src/application/run_stream/orchestration.rs#13-15.
  5. Finalization: After the provider completes, the run is transitioned to a Done state, and post-run reflection is scheduled crates/palyra-daemon/src/application/run_stream/orchestration.rs#107-144.

Run Lifecycle State Transitions

The RunStateMachine tracks the progress of a run through several key states defined in crates/palyra-daemon/src/orchestrator.rs.
StateTransition TriggerDescription
PendingInitialThe run is created but not yet active.
InProgressStartStreamingThe orchestrator has begun communicating with a model provider.
DoneCompleteThe run finished successfully.
CancelledCancelThe user or system terminated the run mid-execution.
Sources: crates/palyra-daemon/src/application/run_stream/orchestration.rs#107-144, crates/palyra-daemon/src/application/run_stream/orchestration.rs#147-183.

Tool Flow & Approvals

Tool execution within a RunStream is governed by a security and approval pipeline located in crates/palyra-daemon/src/application/run_stream/tool_flow.rs.

Tool Execution Pipeline

The function process_run_stream_tool_proposal_event manages the lifecycle of a single tool call crates/palyra-daemon/src/application/run_stream/tool_flow.rs#53-69:
  1. Security Evaluation: evaluate_tool_proposal_security checks the tool against Cedar policies and skill-specific constraints crates/palyra-daemon/src/application/run_stream/tool_flow.rs#121-134.
  2. Approval Resolution: If a tool is deemed sensitive or requires explicit permission, the system checks for a cached decision or pauses for a human-in-the-loop (HITL) response via resolve_run_stream_tool_approval_outcome crates/palyra-daemon/src/application/run_stream/tool_flow.rs#212-228.
  3. Execution: Once approved, execute_run_stream_tool_proposal dispatches the call to the appropriate runtime (WASM, Shell, or Internal) crates/palyra-daemon/src/application/run_stream/tool_flow.rs#87-100.
  4. Tape Recording: Every step (Proposal, Decision, Result) is sent to the client via the Tape to ensure the UI can render the tool’s progress in real-time crates/palyra-daemon/src/application/run_stream/tool_flow.rs#38-43.

Data Flow: Tool Proposal to Result

Sources: crates/palyra-daemon/src/application/run_stream/tool_flow.rs#105-208, crates/palyra-daemon/src/application/run_stream/tool_flow.rs#147-162.

Context References

Palyra allows users to inject external context into a prompt using special syntax (e.g., @file, @url). The application/context_references.rs module handles the resolution and rendering of these references. Sources: crates/palyra-daemon/src/application/context_references.rs#27-35, crates/palyra-daemon/src/application/context_references.rs#131-161.

Learning & Reflection Pipeline

The learning pipeline is an autonomous background process that extracts “Durable Facts”, “Preferences”, and “Procedures” from chat history.

Post-Run Reflection

When a run completes, schedule_post_run_reflection creates a background task crates/palyra-daemon/src/application/learning.rs#41-46. This task is picked up by the BackgroundQueue and processed by process_post_run_reflection_task crates/palyra-daemon/src/application/learning.rs#125-128.

Learning Candidates

The reflection process generates three types of candidates:
  1. Compaction Candidates: Summaries of long conversations to save context tokens crates/palyra-daemon/src/application/learning.rs#161-168.
  2. Preference Candidates: User-specific stylistic or operational choices crates/palyra-daemon/src/application/learning.rs#169-176.
  3. Procedure Candidates: Reusable sequences of tool calls that the agent has successfully performed crates/palyra-daemon/src/application/learning.rs#177-185.

Confidence Thresholds

Learning is governed by BPS (basis points) thresholds defined in LearningRuntimeConfig crates/palyra-daemon/src/gateway/runtime.rs#85-96. If a candidate’s confidence exceeds the auto_write_threshold_bps, it is applied immediately; otherwise, it remains in PendingReview for the user crates/palyra-daemon/src/application/learning.rs#189-195. Sources: crates/palyra-daemon/src/application/learning.rs#30-32, crates/palyra-daemon/src/gateway/runtime.rs#151-166.

Background Queue & Self-Healing

The BackgroundQueue serves as the executor for non-interactive tasks like learning, session cleanup, and scheduled maintenance.

Task Processing

spawn_background_queue_loop initializes a long-running task that polls the JournalStore for pending OrchestratorBackgroundTaskRecord entries crates/palyra-daemon/src/background_queue.rs#31-44. Tasks are processed with heartbeats to allow the self-healing system to detect “stuck” work crates/palyra-daemon/src/background_queue.rs#108-112.

Self-Healing Watchdog

The SelfHealingState tracks incidents across domains like Browser, Artifact, and Approval crates/palyra-daemon/src/self_healing.rs#37-42. Sources: crates/palyra-daemon/src/background_queue.rs#51-60, crates/palyra-daemon/src/self_healing.rs#134-146, crates/palyra-daemon/src/self_healing.rs#225-231.