> ## Documentation Index
> Fetch the complete documentation index at: https://docs-code.palyra.com/llms.txt
> Use this file to discover all available pages before exploring further.

# Core Daemon Architecture (palyra-daemon)

<details>
  <summary>Relevant source files</summary>

  The following files were used as context for generating this wiki page:

  * crates/palyra-common/src/tool\_catalog.rs
  * crates/palyra-daemon/src/application/channel\_turn/mod.rs
  * crates/palyra-daemon/src/application/context\_engine.rs
  * crates/palyra-daemon/src/application/context\_references.rs
  * crates/palyra-daemon/src/application/mod.rs
  * crates/palyra-daemon/src/application/route\_message/orchestration.rs
  * crates/palyra-daemon/src/application/run\_stream/agent\_loop.rs
  * crates/palyra-daemon/src/application/run\_stream/orchestration.rs
  * crates/palyra-daemon/src/application/run\_stream/tool\_flow\.rs
  * crates/palyra-daemon/src/application/tool\_runtime/mod.rs
  * crates/palyra-daemon/src/gateway.rs
  * crates/palyra-daemon/src/gateway/runtime.rs
  * crates/palyra-daemon/src/journal.rs
  * crates/palyra-daemon/src/lib.rs
  * crates/palyra-daemon/src/tool\_posture.rs
  * crates/palyra-daemon/src/tool\_protocol.rs
  * crates/palyra-daemon/src/transport/grpc/services/gateway/service.rs
  * crates/palyra-daemon/src/transport/http/handlers/console/diagnostics.rs
  * crates/palyra-daemon/src/transport/http/router.rs
  * crates/palyra-daemon/tests/gateway\_grpc.rs
  * crates/palyra-daemon/tests/golden/current\_state\_inventory.json
</details>

The `palyrad` daemon is the central hub of the Palyra ecosystem. It hosts the gateway runtime, model provider adapters, tool execution backends, and the durable journal in a single process [crates/palyra-daemon/src/lib.rs#1-10](http://crates/palyra-daemon/src/lib.rs#1-10). It manages the entire lifecycle of agent interactions, from inbound message routing to iterative tool execution and post-run learning.

## System Hub: GatewayRuntimeState

The core of the daemon is the `GatewayRuntimeState`. This structure acts as the shared coordinator for all transport surfaces (gRPC, HTTP, and QUIC), providing a unified interface to the underlying subsystems [crates/palyra-daemon/src/gateway/runtime.rs#1-12](http://crates/palyra-daemon/src/gateway/runtime.rs#1-12).

### Subsystem Interconnection

The daemon organizes its logic into several major domains that interact through the gateway runtime:

* **Transport Layer**: Handles protocol-specific communication (Axum for HTTP, Tonic for gRPC) and maps external requests into internal gateway commands [crates/palyra-daemon/src/transport/http/router.rs#1-7](http://crates/palyra-daemon/src/transport/http/router.rs#1-7).
* **Orchestration**: Manages the `RunStateMachine`, transitioning agents through phases like `Queued`, `Running`, and `Waiting` [crates/palyra-daemon/src/orchestrator.rs#107-107](http://crates/palyra-daemon/src/orchestrator.rs#107-107).
* **Journaling**: Provides a tamper-evident, SQLite-backed audit log and durable storage for sessions, tape, and memory [crates/palyra-daemon/src/journal.rs#1-15](http://crates/palyra-daemon/src/journal.rs#1-15).
* **Tool Runtime**: A secure dispatch layer that executes tools (e.g., shell commands, wasm plugins) under strict policy control [crates/palyra-daemon/src/tool\_protocol.rs#1-11](http://crates/palyra-daemon/src/tool_protocol.rs#1-11).

### Daemon Entity Map

The following diagram illustrates how high-level architectural concepts map to specific code entities within the `palyra-daemon` crate.

```mermaid theme={null}
graph TD
    subgraph "Transport Layer (Code Entity Space)"
        HTTP["transport::http::router::build_router"]
        GRPC["transport::grpc::services::gateway::GatewayServiceImpl"]
    end

    subgraph "Core Hub"
        GRS["gateway::runtime::GatewayRuntimeState"]
    end

    subgraph "Orchestration & Logic"
        RSM["orchestrator::RunStateMachine"]
        AL["application::run_stream::agent_loop"]
        TR["application::tool_registry::ToolCatalog"]
    end

    subgraph "Persistence"
        JS["journal::JournalStore (SQLite)"]
    end

    HTTP --> GRS
    GRPC --> GRS
    GRS --> RSM
    RSM --> AL
    AL --> TR
    GRS --> JS
```

**Sources:** [crates/palyra-daemon/src/lib.rs#14-27](http://crates/palyra-daemon/src/lib.rs#14-27), [crates/palyra-daemon/src/gateway/runtime.rs#1-12](http://crates/palyra-daemon/src/gateway/runtime.rs#1-12), [crates/palyra-daemon/src/application/mod.rs#1-5](http://crates/palyra-daemon/src/application/mod.rs#1-5)

## Major Subsystems

The daemon's functionality is partitioned into specialized subsystems. Detailed documentation for each can be found in their respective child pages.

### Gateway Runtime and Session Lifecycle

The gateway manages `OrchestratorSessionRecord` and `RunLifecycleState`. It handles inbound message admission via the `InboundCoalescer` and ensures that sessions are resolved and cleaned up properly [crates/palyra-daemon/src/gateway/runtime.rs#7-12](http://crates/palyra-daemon/src/gateway/runtime.rs#7-12).

* For details, see [Gateway Runtime and Session Lifecycle](/core_daemon_architecture_palyra-daemon/gateway_runtime_and_session_lifecycle).

### Agent Loop and Run Orchestration

The `AgentRunLoopState` drives the iterative process of prompting a model, parsing tool calls, and feeding results back. This loop is recorded on an append-only `Tape` for determinism and auditability [crates/palyra-daemon/src/application/run\_stream/orchestration.rs#1-15](http://crates/palyra-daemon/src/application/run_stream/orchestration.rs#1-15).

* For details, see [Agent Loop and Run Orchestration](/core_daemon_architecture_palyra-daemon/agent_loop_and_run_orchestration).

### Journal Store and Audit System

The `JournalStore` is a SQLite database operating in WAL mode. It implements a SHA-256 hash-chain to ensure that every `JournalEventRecord` is tamper-evident. It also handles sensitive data redaction before persistence [crates/palyra-daemon/src/journal.rs#1-20](http://crates/palyra-daemon/src/journal.rs#1-20).

* For details, see [Journal Store and Audit System](/core_daemon_architecture_palyra-daemon/journal_store_and_audit_system).

### Transport Layer: HTTP, gRPC, and QUIC

The daemon exposes multiple interfaces. The Axum-based HTTP router serves the Web Console and Admin APIs, while gRPC services handle high-performance communication for tools and remote workers [crates/palyra-daemon/src/transport/http/router.rs#25-38](http://crates/palyra-daemon/src/transport/http/router.rs#25-38).

* For details, see [Transport Layer: HTTP, gRPC, and QUIC](/core_daemon_architecture_palyra-daemon/transport_layer_http_grpc_and_quic).

## Request Flow: Natural Language to Code Execution

This diagram traces the path of a user request from the "Natural Language Space" (an inbound message) through the internal "Code Entity Space" until a tool is executed.

```mermaid theme={null}
sequenceDiagram
    participant User as "User (Natural Language)"
    participant Router as "application::route_message"
    participant Coalescer as "application::inbound_coalescer"
    participant Orchestrator as "application::run_stream::process_run_stream_message"
    participant Loop as "application::run_stream::agent_loop"
    participant Tool as "tool_protocol::execute_tool_call"

    User->>Router: "Inbound Message"
    Router->>Coalescer: "Coalesce Window"
    Coalescer->>Orchestrator: "Start Run"
    loop Agent Iteration
        Orchestrator->>Loop: "Next Step"
        Loop->>Tool: "execute_tool_call(proposal)"
        Tool-->>Loop: "ToolExecutionOutcome"
    end
    Loop-->>User: "Final Answer"
```

**Sources:** [crates/palyra-daemon/src/application/mod.rs#47-48](http://crates/palyra-daemon/src/application/mod.rs#47-48), [crates/palyra-daemon/src/application/run\_stream/orchestration.rs#3-9](http://crates/palyra-daemon/src/application/run_stream/orchestration.rs#3-9), [crates/palyra-daemon/src/tool\_protocol.rs#6-11](http://crates/palyra-daemon/src/tool_protocol.rs#6-11)

## Security and Governance

The daemon enforces a "deny-by-default" security posture. Every tool call is gated by `decide_tool_call`, which evaluates the request against Cedar policies [crates/palyra-daemon/src/tool\_protocol.rs#4-5](http://crates/palyra-daemon/src/tool_protocol.rs#4-5). Security-relevant invariants, such as execution limits and resource cleanup (e.g., stale PID files, browser sessions), are managed centrally within the gateway hub [crates/palyra-daemon/src/gateway.rs#9-11](http://crates/palyra-daemon/src/gateway.rs#9-11).

**Sources:** [crates/palyra-daemon/src/gateway.rs#1-11](http://crates/palyra-daemon/src/gateway.rs#1-11), [crates/palyra-daemon/src/tool\_protocol.rs#1-15](http://crates/palyra-daemon/src/tool_protocol.rs#1-15)

## Child Pages

* [Gateway Runtime and Session Lifecycle](/core_daemon_architecture_palyra-daemon/gateway_runtime_and_session_lifecycle)
* [Agent Loop and Run Orchestration](/core_daemon_architecture_palyra-daemon/agent_loop_and_run_orchestration)
* [Journal Store and Audit System](/core_daemon_architecture_palyra-daemon/journal_store_and_audit_system)
* [Transport Layer: HTTP, gRPC, and QUIC](/core_daemon_architecture_palyra-daemon/transport_layer_http_grpc_and_quic)
