palyrad daemon. It manages the high-level lifecycle of agent interactions, bridging external communication channels (Discord, CLI, Web Console) with the internal execution logic (LLM providers, tool execution, and the Run State Machine).
GatewayRuntimeState
TheGatewayRuntimeState serves as the primary container for the daemon’s active operational state. It holds references to the JournalStore, ModelProvider, ChannelRouter, and the Vault, coordinating their interactions during a run.
Key Responsibilities
- Session Resolution: Mapping incoming messages to specific
OrchestratorSessionRecordentries based on principal, device, and channel crates/palyra-daemon/src/gateway.rs#68-70. - Run Management: Initializing and tracking the state of active
OrchestratorRunRecordinstances crates/palyra-daemon/src/gateway.rs#67-68. - Tool Mediation: Validating tool calls against security policies and managing the approval workflow for sensitive operations crates/palyra-daemon/src/gateway/runtime.rs#129-136.
Configuration Snapshot
The runtime behavior is governed byGatewayRuntimeConfigSnapshot, which defines limits for tape entries, gRPC/QUIC binding addresses, and specific sub-engine configs like BrowserServiceRuntimeConfig and CanvasHostRuntimeConfig crates/palyra-daemon/src/gateway/runtime.rs#29-49.
Run State Machine (RSM)
TheRunStateMachine manages the transition of a “Run” through its lifecycle. A Run is a single unit of execution triggered by a user prompt or a scheduled event.
Lifecycle States
Runs transition through the followingRunLifecycleState values crates/palyra-daemon/src/orchestrator.rs#77-77:
- Accepted: The run has been created and queued.
- Preparing: Context is being gathered (memory recall, attachment processing).
- Running: The LLM is being queried or tools are being executed.
- AwaitingApproval: Execution is paused pending a user decision on a sensitive tool.
- Succeeded/Failed/Cancelled: Terminal states.
Data Flow: Natural Language to Code Entities
The following diagram illustrates how a natural language message from a user is transformed into internal code entities within the Gateway. Sources: crates/palyra-daemon/src/gateway.rs#42-54, crates/palyra-daemon/src/gateway/runtime.rs#1-25, crates/palyra-daemon/src/transport/grpc/services/gateway/service.rsSession, Run, and Tape Lifecycle
Palyra uses a hierarchical data model to maintain context and history:| Entity | Code Symbol | Persistence | Scope |
|---|---|---|---|
| Session | OrchestratorSessionRecord | JournalStore | Long-lived conversation thread between a Principal and an Agent. |
| Run | OrchestratorRunRecord | JournalStore | A single execution “turn” (User -> Assistant -> Tools). |
| Tape | OrchestratorTapeRecord | JournalStore | An append-only log of events (messages, tool calls, logs) within a Run. |
Tape Event Management
The “Tape” is an immutable ledger of what happened during a run.GatewayRuntimeState enforces limits such as MAX_MODEL_TOKEN_TAPE_EVENTS_PER_RUN (1024) to prevent context window overflow crates/palyra-daemon/src/gateway.rs#103-103.
Sources: crates/palyra-daemon/src/journal.rs#67-69, crates/palyra-daemon/src/gateway.rs#89-105
gRPC GatewayService
TheGatewayService (implemented via Tonic) is the primary API for all clients.
Key RPC Methods
RouteMessage: The main entry point for sending text/attachments to an agent crates/palyra-daemon/src/gateway.rs#96-104.RunStream: Provides a server-streaming response for real-time updates of a run’s progress crates/palyra-daemon/src/gateway.rs#184-186.ResolveOrchestratorSession: Identifies or creates a session for a givensession_keycrates/palyra-daemon/src/transport/http/handlers/console/chat.rs#36-40.
Channel Routing and Pairing
TheChannelRouter manages how different platforms (CLI, Discord, Web) interact with the gateway.
Pairing Workflow
For non-trusted channels (like Discord DMs), Palyra uses a pairing flow to bind a platform identity to a Palyra principal:- Generate Code:
palyra channels pairing-code generate. - Consume Code: User sends
/pair <code>in the external channel. - Approval: The operator approves the
PairingPendingRecordvia the console crates/palyra-daemon/src/channel_router.rs#94-101.
Security Labels
Inbound messages can carrysecurity_labels which influence policy evaluation during the run crates/palyra-daemon/tests/gateway_grpc.rs#146-155.
Sources: crates/palyra-daemon/src/channel_router.rs#112-118, crates/palyra-daemon/src/channels.rs#160-179
Tool Approval Workflow
When a run attempts to execute a tool marked as sensitive (e.g.,shell_execute), the engine triggers the approval workflow.
Process Flow
- Policy Hit:
evaluate_with_configreturnsPolicyDecision::Allowbut withapproval_required: truecrates/palyra-daemon/tests/admin_surface.rs#110-120. - Suspend: The
RunStateMachinetransitions toAwaitingApproval. - Notification: An
ApprovalRecordis created in theJournalStorecrates/palyra-daemon/src/gateway.rs#58-59. - Resolution: The operator provides an
ApprovalDecision(Approve/Deny) via theGatewayService::ResolveApprovalRPC.
Risk Levels
Approvals are categorized byApprovalRiskLevel (Low, Medium, High, Critical) based on the tool’s metadata and the resource being accessed crates/palyra-daemon/src/gateway.rs#58-59.
Sources: crates/palyra-daemon/src/gateway.rs#95-102, crates/palyra-daemon/src/gateway/runtime.rs#129-136, crates/palyra-daemon/src/journal.rs#57-59