RootCommandContext, which manages profile resolution, state isolation, and connection parameters for the daemon.
RootCommandContext and Boot Sequence
TheRootCommandContext is the central authority for the CLI’s operational state crates/palyra-cli/src/app/mod.rs#34-46. It is initialized during the CLI’s startup phase and stored in a global OnceLock crates/palyra-cli/src/app/mod.rs#146-150.
Multi-Stage Boot Sequence
- Argument Parsing: The CLI uses
clapto parseRootOptions, which include global overrides like--profile,--config, and--state-rootcrates/palyra-cli/src/args/mod.rs. - Context Installation: The
install_root_contextfunction is called crates/palyra-cli/src/app/mod.rs#152-158, which triggersbuild_root_context. - Profile Resolution: The system looks for a
profiles.tomlfile to determine the active profile crates/palyra-cli/src/app/mod.rs#26-31. - State/Config Mapping: Based on the profile or global flags, the
state_rootandconfig_pathare finalized crates/palyra-cli/src/app/mod.rs#35-37. - Connection Setup: The context resolves the
AgentConnection(gRPC/HTTP URLs and credentials) required to talk to thepalyraddaemon crates/palyra-cli/src/app/mod.rs#216-223.
CLI Boot Data Flow
The following diagram illustrates how raw inputs are transformed into a validatedRootCommandContext.
Title: CLI Initialization and Context Resolution
Sources: crates/palyra-cli/src/app/mod.rs#34-158, crates/palyra-cli/src/args/mod.rs
Profile Resolution (profiles.toml)
Profiles allow users to switch between different daemon instances (e.g., local vs. remote) and isolated environments. Profiles are stored in a TOML registry located atcli/profiles.toml within the CLI state root crates/palyra-cli/src/app/mod.rs#24-31.
Profile Configuration Structure
TheCliConnectionProfile struct defines the attributes of a profile crates/palyra-cli/src/app/mod.rs#94-114:
| Field | Description |
|---|---|
daemon_url | The base HTTP URL for the daemon (e.g., http://127.0.0.1:7142). |
grpc_url | The gRPC endpoint for the gateway. |
state_root | Isolated directory for logs, identity, and temporary files. |
strict_mode | Enables guardrails that prevent accidental destructive actions. |
risk_level | Metadata indicating the sensitivity of the environment (Low, High, etc.). |
Connection Overrides
Connection parameters are resolved using a hierarchy of precedence crates/palyra-cli/src/app/mod.rs#216-240:- Explicit CLI arguments (e.g.,
--daemon-url). - Environment variables like
PALYRA_DAEMON_URLcrates/palyra-cli/src/lib.rs#160. - Active profile settings in
profiles.toml. - Hardcoded defaults (e.g.,
127.0.0.1:7142) crates/palyra-cli/src/lib.rs#151-160.
State Root and Isolation
Palyra enforces strict isolation via thestate_root. This directory contains all per-profile data, including:
- Identity: Device keys and mTLS certificates crates/palyra-cli/src/lib.rs#113-115.
- Vault: Encrypted local secrets crates/palyra-cli/src/lib.rs#123-126.
- Skills: Local skill artifacts and trust stores crates/palyra-cli/src/lib.rs#168-173.
state_root, the CLI automatically switches its internal file lookups to that path, ensuring that a “Production” profile cannot accidentally use “Development” credentials.
Title: Profile Isolation and Resource Mapping
Sources: crates/palyra-cli/src/app/mod.rs#28-31, crates/palyra-cli/src/lib.rs#123-126
Strict Mode and Guardrails
To prevent accidental damage in high-risk environments, profiles can enablestrict_mode crates/palyra-cli/src/app/mod.rs#109.
Guardrail Mechanisms
- Destructive Actions: Commands like
profile deleteorsecrets unsetrequire the--allow-strict-profile-actionsflag if the active profile is in strict mode crates/palyra-cli/src/app/mod.rs#45. - Risk Awareness: The
risk_levelfield (Low, Elevated, High, Critical) is used by the TUI and CLI to colorize output and provide additional confirmation prompts crates/palyra-cli/src/app/mod.rs#107. - Profile Mismatch Protection: The CLI can be configured to fail if the
--expect-profileargument does not match the resolved active profile.
Implementation Reference
Key Classes and Functions
| Entity | File | Purpose |
|---|---|---|
RootCommandContext | crates/palyra-cli/src/app/mod.rs#34 | Holds the resolved CLI environment. |
CliProfilesDocument | crates/palyra-cli/src/app/mod.rs#85 | The deserialized structure of profiles.toml. |
AgentConnection | crates/palyra-cli/src/lib.rs#221 | Connection details for gRPC and HTTP APIs. |
resolve_grpc_url | crates/palyra-cli/src/app/mod.rs#216 | Logic for merging profile and override URLs. |
install_root_context | crates/palyra-cli/src/app/mod.rs#152 | Entry point for CLI initialization. |
Configuration Precedence
The resolution logic inbuild_root_context follows this order:
RootOptionspassed via command line.- Environment variables (e.g.,
PALYRA_STATE_ROOT). CliConnectionProfilefields fromprofiles.toml.- Platform-specific default paths (via
palyra_common).