Skip to main content

Crate trusty_common

Crate trusty_common 

Source
Expand description

Shared utility surface for trusty-* projects.

Why: Port auto-detect, data-directory resolution, tracing init, NO_COLOR handling, and the OpenRouter chat-completions client appeared in both trusty-memory and trusty-search with subtle divergence. Centralising keeps them aligned and gives future trusty-* binaries a one-import surface.

What: pure utility functions — no global state. Each subsystem is a free function or a small helper struct.

Test: cargo test -p trusty-common covers port walking, data-dir creation, and the OpenRouter request shape (without hitting the network).

§Test isolation: TRUSTY_DATA_DIR_OVERRIDE

macOS’s dirs::data_dir() resolves the application-support directory via NSFileManager, a native Cocoa API that completely ignores the HOME and XDG_DATA_HOME environment variables. This makes it impossible to redirect data-directory access in tests using ordinary env-var tricks, because the kernel query bypasses the environment entirely.

To work around this, resolve_data_dir checks the DATA_DIR_OVERRIDE_ENV (TRUSTY_DATA_DIR_OVERRIDE) environment variable before consulting dirs::data_dir(). When set, the variable’s value is used as the base directory verbatim, and dirs::data_dir() is never called.

This escape hatch is intended for testing only. Do not set it in production deployments; rely on the OS-standard data directory instead.

Re-exports§

pub use shutdown::shutdown_signal;
pub use chat::BedrockProvider;
pub use chat::ChatEvent;
pub use chat::ChatProvider;
pub use chat::DEFAULT_BEDROCK_MODEL;
pub use chat::LocalModelConfig;
pub use chat::OllamaProvider;
pub use chat::OpenRouterProvider;
pub use chat::ToolCall;
pub use chat::ToolDef;
pub use chat::auto_detect_local_provider;
pub use port::bind_with_auto_port;
pub use data_dir::DATA_DIR_OVERRIDE_ENV;
pub use data_dir::is_dir;
pub use data_dir::resolve_data_dir;
pub use data_dir::sanitize_data_root;
pub use daemon_addr::check_already_running;
pub use daemon_addr::read_daemon_addr;
pub use daemon_addr::write_daemon_addr;
pub use health_probe::probe_health;
pub use tracing_init::init_tracing;
pub use tracing_init::init_tracing_with_buffer;
pub use tracing_init::maybe_disable_color;
pub use openrouter_legacy::ChatMessage;
pub use openrouter_legacy::openrouter_chat;Deprecated
pub use openrouter_legacy::openrouter_chat_stream;Deprecated

Modules§

chat
Provider-agnostic streaming chat abstraction with tool-use support.
claude_config
Claude Code configuration discovery and patching.
daemon_addr
Daemon HTTP-address file helpers.
daemon_guard
Shared CLI daemon-guard helper (probe + spinner + spawn).
data_dir
Data-directory resolution and filesystem utilities.
github_path
Shared GitHub owner/repo path derivation (issue #1220).
health_probe
HTTP health-probe helper.
log_buffer
Bounded in-memory ring buffer of recent tracing log lines.
openrouter_legacy
Deprecated single-shot OpenRouter helpers.
port
TCP port auto-walking helper.
project_discovery
Claude Code project directory discovery.
shutdown
Shared graceful-shutdown signal helper for trusty-* daemons (issue #534).
sys_metrics
Process RSS / CPU sampling and data-directory sizing for daemon health.
tracing_init
Global tracing subscriber initialisation helpers.