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 chat::ChatEvent;
pub use chat::ChatProvider;
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;

Modules§

chat
Provider-agnostic streaming chat abstraction with tool-use support.
claude_config
Claude Code configuration discovery and patching.
log_buffer
Bounded in-memory ring buffer of recent tracing log lines.
project_discovery
Claude Code project directory discovery.
sys_metrics
Process RSS / CPU sampling and data-directory sizing for daemon health.

Structs§

ChatMessage
OpenAI-compatible chat message.

Constants§

DATA_DIR_OVERRIDE_ENV
Environment variable name for the data-directory test escape hatch.

Functions§

bind_with_auto_port
Bind to addr; if the port is in use, walk forward up to max_attempts ports and return the first listener that binds.
init_tracing
Initialise the global tracing subscriber.
init_tracing_with_buffer
Initialise the global tracing subscriber and capture events into a log_buffer::LogBuffer so the daemon can serve recent logs over HTTP.
is_dir
Check whether a path exists and is a directory.
maybe_disable_color
Disable coloured terminal output when requested or when stdout is not a TTY.
openrouter_chatDeprecated
Send a chat completion request to OpenRouter and return the assistant’s message content.
openrouter_chat_streamDeprecated
Stream chat-completion deltas from OpenRouter through a tokio mpsc channel.
read_daemon_addr
Read the daemon’s HTTP address from the app’s data directory.
resolve_data_dir
Resolve <data_dir>/<app_name>, creating it if it doesn’t exist.
write_daemon_addr
Write the daemon’s bound HTTP address to the app’s data directory.