trusty_common/lib.rs
1//! Shared utility surface for trusty-* projects.
2//!
3//! Why: Port auto-detect, data-directory resolution, tracing init, NO_COLOR
4//! handling, and the OpenRouter chat-completions client appeared in both
5//! trusty-memory and trusty-search with subtle divergence. Centralising keeps
6//! them aligned and gives future trusty-* binaries a one-import surface.
7//!
8//! What: pure utility functions — no global state. Each subsystem is a free
9//! function or a small helper struct.
10//!
11//! Test: `cargo test -p trusty-common` covers port walking, data-dir creation,
12//! and the OpenRouter request shape (without hitting the network).
13//!
14//! # Test isolation: `TRUSTY_DATA_DIR_OVERRIDE`
15//!
16//! macOS's [`dirs::data_dir()`] resolves the application-support directory via
17//! `NSFileManager`, a native Cocoa API that completely ignores the `HOME` and
18//! `XDG_DATA_HOME` environment variables. This makes it impossible to redirect
19//! data-directory access in tests using ordinary env-var tricks, because the
20//! kernel query bypasses the environment entirely.
21//!
22//! To work around this, [`resolve_data_dir`] checks the
23//! [`DATA_DIR_OVERRIDE_ENV`] (`TRUSTY_DATA_DIR_OVERRIDE`) environment variable
24//! before consulting `dirs::data_dir()`. When set, the variable's value is used
25//! as the base directory verbatim, and `dirs::data_dir()` is never called.
26//!
27//! **This escape hatch is intended for testing only.** Do not set it in
28//! production deployments; rely on the OS-standard data directory instead.
29
30use std::net::SocketAddr;
31use std::path::{Path, PathBuf};
32
33pub mod chat;
34pub mod claude_config;
35pub mod project_discovery;
36
37/// Bounded in-memory ring buffer of recent tracing log lines.
38///
39/// Why: trusty-* daemons expose a `/logs/tail` endpoint so operators can read
40/// recent logs over HTTP without file I/O or a daemon restart. The buffer and
41/// its `tracing_subscriber::Layer` live here so every daemon shares one impl.
42/// What: `LogBuffer` (thread-safe capped `VecDeque<String>`) plus
43/// `LogBufferLayer` (the tracing layer that feeds it).
44/// Test: `cargo test -p trusty-common log_buffer` covers capacity eviction,
45/// tail semantics, and layer capture.
46pub mod log_buffer;
47
48/// Process RSS / CPU sampling and data-directory sizing for daemon health.
49///
50/// Why: every trusty-* daemon's `/health` endpoint reports its own resident
51/// memory, CPU usage, and on-disk footprint; the sampling logic is identical
52/// across them so it lives here once.
53/// What: `SysMetrics` (per-process RSS + CPU sampler) and `dir_size_bytes`
54/// (recursive directory byte count).
55/// Test: `cargo test -p trusty-common sys_metrics`.
56pub mod sys_metrics;
57
58/// macOS LaunchAgent generation and lifecycle management. macOS-only —
59/// the module compiles to nothing on every other platform.
60#[cfg(target_os = "macos")]
61pub mod launchd;
62
63#[cfg(feature = "axum-server")]
64pub mod server;
65
66/// Shared JSON-RPC 2.0 / MCP primitives (formerly the `trusty-mcp-core` crate).
67///
68/// Why: Centralises `Request`/`Response`/`JsonRpcError` envelopes, the
69/// `initialize` response builder, an async stdio dispatch loop, and the
70/// OpenRPC `rpc.discover` helpers so every MCP server in the workspace
71/// imports the same types.
72/// What: Gated behind the `mcp` feature; pulls in no extra dependencies
73/// beyond `serde` / `tokio`, both of which are already required.
74/// Test: `cargo test -p trusty-common --features mcp` runs the module's
75/// own unit tests (envelope round-trips, stdio loop dispatch, OpenRPC
76/// builder shape).
77#[cfg(feature = "mcp")]
78pub mod mcp;
79
80/// General-purpose JSON-RPC client + transports (formerly the library half
81/// of the `trusty-rpc` crate).
82///
83/// Why: Both `trpc` (the CLI) and any future library consumer want one
84/// place that owns the JSON-RPC envelope construction, stdio-subprocess
85/// transport, HTTP transport, and pretty-printers.
86/// What: Gated behind the `rpc` feature; requires `uuid` for request id
87/// generation. The HTTP transport reuses the workspace `reqwest`.
88/// Test: `cargo test -p trusty-common --features rpc` runs the module's
89/// own unit tests (envelope extraction, pretty-print smoke tests).
90#[cfg(feature = "rpc")]
91pub mod rpc;
92
93/// Shared text-embedding abstraction (formerly the `trusty-embedder` crate).
94///
95/// Why: trusty-memory and trusty-search both ship near-identical `Embedder`
96/// traits and `FastEmbedder` implementations; centralising the surface here
97/// keeps them aligned and lets future consumers pick up embedding for free
98/// without a separate published crate.
99/// What: Gated behind the `embedder` feature. Exposes the `Embedder` trait,
100/// `FastEmbedder` (fastembed-rs, all-MiniLM-L6-v2, 384-d) with LRU caching
101/// and ORT warmup, and (under `embedder-test-support`) the `MockEmbedder`
102/// test double.
103/// Test: `cargo test -p trusty-common --features embedder,embedder-test-support`
104/// covers the mock embedder and ONNX-backed `#[ignore]`d integration tests.
105#[cfg(feature = "embedder")]
106pub mod embedder;
107
108/// Unified RPC client surface for the `trusty-embedderd` standalone process.
109///
110/// Why: absorbs both the former `trusty-embedder-client` HTTP crate (PR #163)
111/// and the former `embed_client` UDS module (PR #157) into a single unified
112/// module. Reduces workspace crate count and provides one trait (`EmbedderClient`)
113/// with three concrete implementations (InProcess, HTTP remote, UDS remote) so
114/// call sites are identical regardless of transport. The `embed-client` feature
115/// and `embed_client` module are retired by issue #164; use `embedder-client`
116/// and `trusty_common::embedder_client::UdsEmbedderClient` instead.
117/// What: Gated behind the `embedder-client` feature. Exposes the
118/// `EmbedderClient` trait, `InProcessEmbedderClient`, `RemoteEmbedderClient`
119/// (HTTP), `UdsEmbedderClient` (UDS), `EmbedRequest` / `EmbedResponse` wire
120/// types, and `EmbedderError`. The UDS impl uses `tokio::net::UnixStream`
121/// with newline-framed JSON-RPC 2.0 — no additional dependencies.
122/// Test: `cargo test -p trusty-common --features embedder-client` covers
123/// error-display, JSON round-trip, URL assembly, UDS wire types, and empty-
124/// batch short-circuits. ONNX-backed tests are in
125/// `trusty-embedderd/tests/bit_identical.rs` (`#[ignore]`).
126#[cfg(feature = "embedder-client")]
127pub mod embedder_client;
128
129/// Zero-dependency BM25 lexical index + code-aware tokenizer (issue #156).
130///
131/// Why: trusty-memory, trusty-search, and the per-palace
132/// `trusty-bm25-daemon` subprocess all want one shared BM25 implementation
133/// so the tokenizer's camelCase / PascalCase / alpha↔digit splits stay
134/// consistent across the workspace. Originally ported from open-mpm; now
135/// the single source of truth lives here.
136/// What: Gated behind the `bm25` feature. Adds no new dependencies — pure
137/// `std` + `tracing` (already required).
138/// Test: `cargo test -p trusty-common --features bm25`.
139#[cfg(feature = "bm25")]
140pub mod bm25;
141
142/// Reusable schema-migration kernel (issue #179).
143///
144/// Why: trusty-search, trusty-memory, and other long-lived stores have grown
145/// ad-hoc schema-migration loops that drift apart. Centralising the
146/// `SchemaVersion` newtype, the `Migration<S>` trait, and a `MigrationRunner`
147/// that applies pending steps in order (writing a stamp after each) collapses
148/// those into one shared kernel. The `file_stamp` helper covers the common
149/// "JSON sidecar in the store's data dir" stamp format; redb-stamp users get
150/// a documented recipe instead of a heavyweight dep.
151/// What: gated behind the `migrations` feature flag. Adds no new
152/// dependencies — pure `serde` + `serde_json` + `anyhow` + `tracing` which
153/// the crate already requires.
154/// Test: `cargo test -p trusty-common --features migrations` covers the
155/// runner ordering, crash resumption, write-stamp failure propagation, and
156/// the file-stamp round-trip / atomic-write behaviour.
157#[cfg(feature = "migrations")]
158pub mod migrations;
159
160/// UDS JSON-RPC client for the per-palace `trusty-bm25-daemon` subprocess
161/// (issue #156).
162///
163/// Why: trusty-memory needs a lexical-search lane without holding an
164/// in-process BM25 index. `Bm25Client` delegates to the per-palace daemon
165/// over `$TMPDIR/trusty-bm25-<palace>.sock`, matching the design of
166/// `EmbedClient` and `trusty-embed-daemon` (PR #157).
167/// What: Gated behind the `bm25-client` feature. Pure user of existing
168/// `tokio` / `serde_json` / `anyhow` workspace deps — adds no new
169/// dependencies.
170/// Test: `cargo test -p trusty-common --features bm25-client` covers
171/// request shape and path defaults; end-to-end coverage lives in
172/// `trusty-bm25-daemon/tests/`.
173#[cfg(feature = "bm25-client")]
174pub mod bm25_client;
175
176/// Symbol-graph engine (formerly the `trusty-symgraph` crate).
177///
178/// Why: All trusty-* tools that touch source code (open-mpm, trusty-search,
179/// trusty-analyze) want the same `EntityType` / `RawEntity` / `EdgeKind`
180/// data shapes and (for orchestrators) the same tree-sitter pipeline. Living
181/// here lets the workspace ship one tree-sitter `links =` slot instead of
182/// juggling two crates that both claim it.
183/// What: Gated behind two features. `symgraph` exposes only the contracts
184/// surface (`EntityType`, `RawEntity`, `EdgeKind`, `fact_hash_str`, tables)
185/// — no tree-sitter, no `links` conflict. `symgraph-parser` additionally
186/// pulls in tree-sitter and the full parse → registry → emit stack.
187/// `symgraph-server` enables the HTTP server frontend.
188/// Test: `cargo test -p trusty-common --features symgraph` exercises the
189/// contracts surface; `cargo test -p trusty-symgraph` covers the parser
190/// path through the thin re-export shim.
191#[cfg(feature = "symgraph")]
192pub mod symgraph;
193
194/// Memory Palace storage engine (formerly the `trusty-memory-core` crate).
195///
196/// Why: Centralises the Memory Palace data model (`Palace` / `Wing` /
197/// `Room` / `Drawer`), storage backends (usearch vector index + SQLite
198/// knowledge graph + chat-session log + payload store), retrieval handle,
199/// and the dream / decay / analytics / git-history surfaces so every
200/// trusty-* binary that talks to a palace reuses the same types. Absorbed
201/// into `trusty-common` (issue #5 phase 2d) so we ship one fewer published
202/// crate.
203/// What: Gated behind the `memory-core` feature because it pulls in heavy
204/// storage deps (`usearch`, `rusqlite`, `r2d2`, `git2`, `kuzu`). Enables
205/// the embedder surface automatically (memory-core → embedder).
206/// Test: `cargo test -p trusty-common --features memory-core` exercises
207/// the full surface.
208#[cfg(feature = "memory-core")]
209pub mod memory_core;
210
211/// Unified ticketing MCP server (formerly the `trusty-tickets` crate).
212///
213/// Why: Claude Code and the rest of the trusty-* suite need a single MCP
214/// surface that can talk to GitHub Issues, JIRA, and Linear without the
215/// caller needing to know which backend is configured. Absorbing into
216/// `trusty-common` reduces the workspace crate count and co-locates the
217/// HTTP client surface with the other protocol helpers.
218/// What: Gated behind the `tickets` feature. Exposes `tickets::api::*`
219/// (config, models, Backend trait, three concrete backends), `tickets::server`
220/// (MCP dispatch loop + `run_stdio`), and `tickets::tools` (the tool-list
221/// schema). Requires the `mcp` feature for the stdio loop.
222/// Test: `cargo test -p trusty-common --features tickets` runs the module's
223/// own unit tests (dispatch, tool-list counts, config parsing, serde
224/// round-trips). Live backend tests require env-var credentials.
225#[cfg(feature = "tickets")]
226pub mod tickets;
227
228/// Declarative CLI help system with "did you mean?" suggestions (issue #216).
229///
230/// Why: every standalone trusty-* binary used to render its `--help` and
231/// unknown-subcommand error output independently, so the formats drifted
232/// apart over time. Centralising the help model into one YAML schema, one
233/// canonical renderer, and one Jaro-Winkler suggester keeps the six binaries
234/// (search, memory, analyze, mpm-cli, tga, open-mpm) speaking with a single
235/// user-facing voice.
236/// What: gated behind the `cli-help` feature. Pulls in `serde_yaml`, `strsim`,
237/// and `indexmap`. Exposes `HelpConfig` / `CommandDef` / `FlagDef` / `Example`
238/// + `load_help` / `render_help` / `suggest`.
239/// Test: `cargo test -p trusty-common --features cli-help`.
240#[cfg(feature = "cli-help")]
241pub mod help;
242
243/// Unified monitor TUI for the trusty-search and trusty-memory daemons
244/// (formerly the `trusty-monitor-tui` crate).
245///
246/// Why: operators run both daemons and want one terminal surface that shows
247/// the health of both at a glance. Living here behind the `monitor-tui`
248/// feature flag matches the workspace's "one fewer published crate" direction
249/// (issue #31 companion) and keeps the dashboard logic unit-testable.
250/// What: gated behind the `monitor-tui` feature, which pulls in `ratatui` and
251/// `crossterm`. Exposes `monitor::run` (the entry point the `trusty-monitor`
252/// binary calls) plus the pure `dashboard` / `search_client` / `memory_client`
253/// submodules.
254/// Test: `cargo test -p trusty-common --features monitor-tui` covers the
255/// rendering, layout, and HTTP-client pieces.
256#[cfg(feature = "monitor-tui")]
257pub mod monitor;
258
259/// Throttled crates.io update-notification helper.
260///
261/// Why: User-facing CLIs should nudge operators when a newer release is
262/// available without adding perceptible latency. A shared implementation
263/// keeps the throttle, cache, opt-out, and User-Agent logic consistent across
264/// every consumer in the workspace.
265/// What: Gated behind the `update-check` feature. Exposes
266/// [`update::check_throttled`] (the main entry — reads a per-crate JSON cache
267/// under the OS cache dir, queries crates.io at most once per 24 h),
268/// [`update::check_crates_io`] (the raw network call), [`update::notice`]
269/// (formatted upgrade message), and [`update::UpdateInfo`] (the result type).
270/// All failures degrade to `None` — the check is best-effort and will not
271/// panic or stall a CLI.
272/// Opt-out: set `TRUSTY_NO_UPDATE_CHECK` or `CI` to any non-empty value.
273/// Test: `cargo test -p trusty-common --features update-check`.
274#[cfg(feature = "update-check")]
275pub mod update;
276
277/// Error-capture layer for the trusty-* consent-gated bug-reporting system
278/// (bug-reporting Phase 1, issue #479).
279///
280/// Why: Every trusty-* daemon encounters runtime errors that developers need
281/// to see but that must be captured locally and only filed to GitHub after
282/// explicit user consent. A shared capture layer in `trusty-common` means
283/// all daemons gain error capture without per-binary changes.
284/// What: Gated behind the `bug-capture` feature. Exposes:
285/// - [`error_capture::CapturedError`] — structured error record.
286/// - [`error_capture::ErrorStore`] — ring buffer + JSONL store.
287/// - [`error_capture::BugCaptureLayer`] — the tracing Layer.
288/// - [`error_capture::bug_capture_layer`] — convenience constructor.
289/// - [`error_capture::TRUSTY_NO_BUG_CAPTURE_ENV`] — opt-out env name.
290/// Additive: does not alter stderr logging. Opt-out via
291/// `TRUSTY_NO_BUG_CAPTURE=1`. New dep: `sha2` (already workspace-optional).
292/// Test: `cargo test -p trusty-common --features bug-capture`.
293#[cfg(feature = "bug-capture")]
294pub mod error_capture;
295
296pub use chat::{
297 ChatEvent, ChatProvider, LocalModelConfig, OllamaProvider, OpenRouterProvider, ToolCall,
298 ToolDef, auto_detect_local_provider,
299};
300
301use anyhow::{Context, Result, anyhow};
302use serde::{Deserialize, Serialize};
303use tokio::net::TcpListener;
304
305// ─── Port binding ─────────────────────────────────────────────────────────
306
307/// Bind to `addr`; if the port is in use, walk forward up to `max_attempts`
308/// ports and return the first listener that binds.
309///
310/// Why: Running multiple instances of a trusty-* daemon (or restarting before
311/// the kernel releases the prior socket) shouldn't produce a noisy failure —
312/// auto-incrementing gives a friendlier developer experience while still
313/// honouring the user's preferred starting port.
314/// What: returns the first successful `tokio::net::TcpListener`. Callers can
315/// inspect `local_addr()` to discover where it landed and report it however
316/// they prefer — this function does not perform any I/O on stdout/stderr.
317/// `max_attempts == 0` means "try `addr` exactly once".
318/// Test: `auto_port_walks_forward` binds a port, then calls this with the
319/// occupied port and confirms a different free port is returned.
320pub async fn bind_with_auto_port(addr: SocketAddr, max_attempts: u16) -> Result<TcpListener> {
321 use std::io::ErrorKind;
322 let mut current = addr;
323 for attempt in 0..=max_attempts {
324 match TcpListener::bind(current).await {
325 Ok(l) => return Ok(l),
326 Err(e) if e.kind() == ErrorKind::AddrInUse && attempt < max_attempts => {
327 let next_port = current.port().saturating_add(1);
328 if next_port == 0 {
329 anyhow::bail!("ran out of ports while searching for free slot");
330 }
331 tracing::warn!("port {} in use, trying {}", current.port(), next_port);
332 current.set_port(next_port);
333 }
334 Err(e) => return Err(e.into()),
335 }
336 }
337 anyhow::bail!("could not find free port after {max_attempts} attempts")
338}
339
340// ─── Data directory ───────────────────────────────────────────────────────
341
342/// Environment variable name for the data-directory test escape hatch.
343///
344/// Why: macOS's `dirs::data_dir()` delegates to `NSFileManager`, a native Cocoa
345/// API that ignores `HOME` and `XDG_DATA_HOME`. Setting `HOME` in a test process
346/// does **not** redirect `dirs::data_dir()` on macOS, making path isolation
347/// impossible without a separate bypass. This constant names that bypass.
348///
349/// What: When `TRUSTY_DATA_DIR_OVERRIDE` is set in the environment,
350/// [`resolve_data_dir`] uses its value as the base directory and skips the
351/// `dirs::data_dir()` call entirely. The final path is
352/// `${TRUSTY_DATA_DIR_OVERRIDE}/<app_name>`, identical in structure to the
353/// normal OS-standard path.
354///
355/// **Intended for tests only.** Do not set this variable in production; it
356/// bypasses the OS-standard application-data directory.
357///
358/// Test: All `resolve_data_dir` tests in this module set this var to a
359/// temporary directory so they run identically on macOS, Linux, and Windows.
360pub const DATA_DIR_OVERRIDE_ENV: &str = "TRUSTY_DATA_DIR_OVERRIDE";
361
362/// Resolve `<data_dir>/<app_name>`, creating it if it doesn't exist.
363///
364/// Why: All trusty-* tools want a per-machine, per-app directory under the
365/// OS-standard data dir (`~/Library/Application Support/`, `~/.local/share/`,
366/// `%APPDATA%/`). If `dirs::data_dir()` is unavailable (rare — locked-down
367/// containers), falls back to `~/.<app_name>` so the tool still works.
368///
369/// The [`DATA_DIR_OVERRIDE_ENV`] (`TRUSTY_DATA_DIR_OVERRIDE`) environment
370/// variable provides a test escape hatch: when set, `dirs::data_dir()` is
371/// **never called** and the variable's value is used as the base directory
372/// instead. This is necessary because macOS's `dirs::data_dir()` calls
373/// `NSFileManager` — a native Cocoa API that resolves the application-support
374/// directory through the system rather than through the process environment —
375/// so setting `HOME` or `XDG_DATA_HOME` in a test process does not redirect
376/// it. `TRUSTY_DATA_DIR_OVERRIDE` is the only reliable cross-platform way to
377/// isolate test data paths. **It is intended for tests only; do not set it in
378/// production.**
379///
380/// What: returns the absolute path `${base}/<app_name>` (created if absent).
381/// Resolution order:
382/// 1. `$TRUSTY_DATA_DIR_OVERRIDE/<app_name>` — when the env var is set.
383/// 2. `$(dirs::data_dir())/<app_name>` — normal OS-standard path.
384/// 3. `~/.<app_name>` — fallback when `dirs::data_dir()` returns `None`.
385///
386/// Test: `resolve_data_dir_creates_directory` pins a temporary directory via
387/// `TRUSTY_DATA_DIR_OVERRIDE` and asserts that the returned path is created
388/// under it, exercising both the override path and directory-creation logic.
389pub fn resolve_data_dir(app_name: &str) -> Result<PathBuf> {
390 let base = if let Ok(override_dir) = std::env::var(DATA_DIR_OVERRIDE_ENV) {
391 PathBuf::from(override_dir)
392 } else {
393 dirs::data_dir()
394 .or_else(|| dirs::home_dir().map(|h| h.join(format!(".{app_name}"))))
395 .context("could not resolve data directory or home directory")?
396 };
397 let dir = if base.ends_with(format!(".{app_name}")) {
398 base
399 } else {
400 base.join(app_name)
401 };
402 std::fs::create_dir_all(&dir)
403 .with_context(|| format!("create data directory {}", dir.display()))?;
404 Ok(dir)
405}
406
407// ─── Daemon address file ──────────────────────────────────────────────────
408
409/// Filename used inside each app's data directory to record the daemon's
410/// bound HTTP address. Kept as a module-level constant so writers and readers
411/// can't drift.
412const DAEMON_ADDR_FILENAME: &str = "http_addr";
413
414/// Write the daemon's bound HTTP address to the app's data directory.
415///
416/// Why: Both trusty-search and trusty-memory persist their bound `host:port`
417/// to disk so MCP clients (and follow-up CLI invocations) can discover where
418/// the daemon ended up after auto-port-walking. Centralising the path layout
419/// keeps the two projects in sync and prevents a third trusty-* daemon from
420/// inventing yet another location.
421/// What: writes `addr` verbatim (no trailing newline) to
422/// `{resolve_data_dir(app_name)}/http_addr`, creating the directory if it
423/// doesn't yet exist. Atomic-overwrite semantics aren't required — the file
424/// is rewritten on every daemon start.
425/// Test: `daemon_addr_round_trips` writes then reads under a stubbed HOME and
426/// confirms equality.
427pub fn write_daemon_addr(app_name: &str, addr: &str) -> Result<()> {
428 let dir = resolve_data_dir(app_name)?;
429 let path = dir.join(DAEMON_ADDR_FILENAME);
430 std::fs::write(&path, addr).with_context(|| format!("write daemon addr to {}", path.display()))
431}
432
433/// Read the daemon's HTTP address from the app's data directory.
434///
435/// Why: CLI commands and MCP clients need to discover the running daemon's
436/// bound port. Returning `Option` lets callers distinguish "daemon never
437/// started" (file absent) from "filesystem error" (permission denied, etc.)
438/// without resorting to string matching on error messages.
439/// What: reads `{resolve_data_dir(app_name)}/http_addr`, trims surrounding
440/// whitespace, and returns `Some(addr)`. Returns `Ok(None)` iff the file
441/// does not exist; any other I/O error propagates as `Err`.
442/// Test: `daemon_addr_round_trips` and `read_daemon_addr_missing_returns_none`.
443pub fn read_daemon_addr(app_name: &str) -> Result<Option<String>> {
444 let dir = resolve_data_dir(app_name)?;
445 let path = dir.join(DAEMON_ADDR_FILENAME);
446 match std::fs::read_to_string(&path) {
447 Ok(s) => Ok(Some(s.trim().to_string())),
448 Err(e) if e.kind() == std::io::ErrorKind::NotFound => Ok(None),
449 Err(e) => Err(anyhow::Error::new(e))
450 .with_context(|| format!("read daemon addr from {}", path.display())),
451 }
452}
453
454// ─── Already-running guard ────────────────────────────────────────────────
455
456/// Issue a short-timeout `GET {base_url}{health_path}` and report whether it
457/// returns a 2xx response.
458///
459/// Why: every trusty-* daemon's "is one already running?" check follows the
460/// same shape — probe the recorded address for `/health` with a tight timeout
461/// so a dead daemon does not block the start command for the discovery
462/// timeout. Lifting the probe into one helper keeps the request/timeout
463/// configuration identical across `check_already_running` (file-based) and the
464/// trusty-mpm lock-file path (where the URL is derived from a TOML file).
465/// What: builds a `reqwest::Client` with a 1 s request timeout, issues the GET,
466/// returns `true` only when the response is HTTP 2xx. Any client-builder error
467/// or transport failure returns `false`.
468/// Test: covered indirectly via `check_already_running_*` and the three daemon
469/// integration paths.
470pub async fn probe_health(base_url: &str, health_path: &str) -> bool {
471 let probe = format!("{base_url}{health_path}");
472 let client = match reqwest::Client::builder()
473 .timeout(std::time::Duration::from_secs(1))
474 .build()
475 {
476 Ok(c) => c,
477 Err(_) => return false,
478 };
479 matches!(client.get(&probe).send().await, Ok(resp) if resp.status().is_success())
480}
481
482/// Probe whether an existing daemon recorded at `addr_file` is healthy and,
483/// if so, return its base URL so the caller can refuse to start a duplicate.
484///
485/// Why: every trusty-* daemon (search, memory, mpm) historically port-walked on
486/// boot. Invoking the `start` / `serve` command a second time silently spawned
487/// a second instance on the next free port — splitting traffic between two
488/// stores, doubling RSS, and confusing every client that resolves the address
489/// from disk. The CLI must read the recorded address, ask the live process for
490/// `/health`, and if both succeed report "already running" and exit 0 rather
491/// than racing a duplicate process against the port walker. A shared helper
492/// keeps the three daemons honest — drift here is the bug we are fixing.
493/// What: returns `Some("http://<addr>")` only when (a) `addr_file` exists and
494/// is readable, (b) its trimmed contents parse as a non-empty `host:port`, and
495/// (c) an HTTP `GET http://<addr><health_path>` returns a 2xx within ~1.5 s
496/// (1 s request timeout plus tokio scheduling slack). Returns `None` on every
497/// other outcome — missing file, unreadable contents, dead address, non-2xx
498/// response — so the caller treats that as "no live daemon, proceed".
499/// Side-effect (stale-file cleanup): when the file exists but the health probe
500/// fails (or the file is empty / malformed), the function best-effort deletes
501/// it via `std::fs::remove_file` so the next caller does not chase the same
502/// dead address. A delete failure is intentionally ignored.
503/// Test: `check_already_running_returns_none_when_file_missing`,
504/// `check_already_running_returns_none_when_file_empty`,
505/// `check_already_running_returns_none_when_address_dead`,
506/// `check_already_running_returns_url_when_health_ok`.
507pub async fn check_already_running(addr_file: &Path, health_path: &str) -> Option<String> {
508 let raw = match std::fs::read_to_string(addr_file) {
509 Ok(s) => s,
510 Err(_) => return None,
511 };
512 let addr = raw.trim();
513 if addr.is_empty() {
514 // Empty / whitespace-only file is treated as stale — best-effort delete.
515 let _ = std::fs::remove_file(addr_file);
516 return None;
517 }
518 let url = format!("http://{addr}");
519 if probe_health(&url, health_path).await {
520 Some(url)
521 } else {
522 // Stale file pointing at a dead address. Clear it so the next start
523 // attempt is not blocked by a probe against the dead URL.
524 let _ = std::fs::remove_file(addr_file);
525 None
526 }
527}
528
529// ─── CLI initialisation ───────────────────────────────────────────────────
530
531/// Initialise the global tracing subscriber.
532///
533/// Why: Every trusty-* binary wants the same verbosity ladder and the same
534/// `RUST_LOG` override semantics. Defining it once removes the boilerplate
535/// from every `main.rs`.
536/// What: `verbose_count` maps `0 → warn`, `1 → info`, `2 → debug`, `3+ →
537/// trace`. If `RUST_LOG` is set in the environment it wins. Logs go to
538/// stderr so stdout stays clean for MCP JSON-RPC.
539/// Test: side-effecting (global subscriber) — covered by integration with
540/// `cargo run -- -v status` in downstream crates.
541pub fn init_tracing(verbose_count: u8) {
542 let default_filter = match verbose_count {
543 0 => "warn",
544 1 => "info",
545 2 => "debug",
546 _ => "trace",
547 };
548 let filter = tracing_subscriber::EnvFilter::try_from_default_env()
549 .unwrap_or_else(|_| tracing_subscriber::EnvFilter::new(default_filter));
550 // try_init so callers that pre-install a subscriber don't panic.
551 let _ = tracing_subscriber::fmt()
552 .with_env_filter(filter)
553 .with_writer(std::io::stderr)
554 .with_target(false)
555 .try_init();
556}
557
558/// Initialise the global tracing subscriber and capture events into a
559/// [`log_buffer::LogBuffer`] so the daemon can serve recent logs over HTTP.
560///
561/// Why: daemons expose `GET /logs/tail`, which needs an in-memory ring of
562/// recent log lines. Routing capture through the subscriber means every
563/// existing `tracing::info!` / `warn!` call site is mirrored automatically —
564/// no second logging API to keep in sync. The stderr `fmt` layer is retained
565/// so operators still see live logs in the terminal / launchd log file.
566/// What: builds a `tracing_subscriber::registry` with two layers — the
567/// standard stderr `fmt` layer (same verbosity ladder + `RUST_LOG` override
568/// as [`init_tracing`]) and a [`log_buffer::LogBufferLayer`] feeding the
569/// returned [`log_buffer::LogBuffer`]. Uses `try_init`, so a process that has
570/// already installed a subscriber keeps it; the returned buffer is still
571/// valid (just empty) in that case.
572/// Test: `cargo test -p trusty-common log_buffer` covers the layer; the
573/// daemon `/logs/tail` integration tests cover the wired path end-to-end.
574#[must_use]
575pub fn init_tracing_with_buffer(verbose_count: u8, capacity: usize) -> log_buffer::LogBuffer {
576 use tracing_subscriber::Layer as _;
577 use tracing_subscriber::layer::SubscriberExt;
578 use tracing_subscriber::util::SubscriberInitExt;
579
580 let default_filter = match verbose_count {
581 0 => "warn",
582 1 => "info",
583 2 => "debug",
584 _ => "trace",
585 };
586 // Stderr filter follows the same verbosity ladder + `RUST_LOG` override as
587 // `init_tracing` so terminal output stays compact at the operator's chosen
588 // level.
589 let stderr_filter = tracing_subscriber::EnvFilter::try_from_default_env()
590 .unwrap_or_else(|_| tracing_subscriber::EnvFilter::new(default_filter));
591
592 // The log-buffer layer must capture activity even when the stderr filter
593 // is set to `warn` (the default for `trusty-search start` without `-v`).
594 // Operators reading `/logs/tail` expect to see info-level lifecycle events
595 // (file-watcher reindexes, startup scans). Without a separate filter the
596 // global stderr filter would suppress them before they reach the buffer.
597 // `RUST_LOG_BUFFER` lets ops widen or narrow the buffer independently of
598 // stderr; the default of `info` matches the activity feed's intent.
599 let buffer_filter = tracing_subscriber::EnvFilter::try_from_env("RUST_LOG_BUFFER")
600 .unwrap_or_else(|_| tracing_subscriber::EnvFilter::new("info"));
601
602 let buffer = log_buffer::LogBuffer::new(capacity);
603 let fmt_layer = tracing_subscriber::fmt::layer()
604 .with_writer(std::io::stderr)
605 .with_target(false)
606 .with_filter(stderr_filter);
607 let buf_layer = log_buffer::LogBufferLayer::new(buffer.clone()).with_filter(buffer_filter);
608 // try_init so callers that pre-install a subscriber don't panic — the
609 // returned buffer simply stays empty in that (rare) case.
610 let _ = tracing_subscriber::registry()
611 .with(fmt_layer)
612 .with(buf_layer)
613 .try_init();
614 buffer
615}
616
617/// Initialise the global tracing subscriber with a [`log_buffer::LogBuffer`]
618/// **and** a [`error_capture::BugCaptureLayer`] composed in one `try_init` call.
619///
620/// Why: `tracing_subscriber::registry().try_init()` can only succeed once per
621/// process. Callers that need both the HTTP log-tail buffer (issue #35)
622/// and Phase 1 bug capture must compose all three layers in a single call;
623/// two separate `try_init` calls would leave the second one silently ignored.
624/// This helper is the canonical entry-point for daemon binaries that want
625/// both features wired together at startup.
626/// What: builds an `EnvFilter`-gated stderr `fmt` layer, an info-level
627/// `LogBufferLayer`, and a `BugCaptureLayer` for `app_name`/`crate_version`;
628/// installs them together via `try_init`. Returns `(LogBuffer, ErrorStore)`
629/// so the caller can stash both handles in the daemon's `AppState`.
630/// All capture is to a JSONL file under `<dirs::data_dir()>/<app_name>/`
631/// and an in-memory ring — nothing is written to stdout, so this is
632/// MCP-safe. Honours `TRUSTY_NO_BUG_CAPTURE` for opt-out.
633/// Test: `cargo test -p trusty-common --features bug-capture -- init_tracing_with_capture`.
634#[cfg(feature = "bug-capture")]
635#[must_use]
636pub fn init_tracing_with_buffer_and_capture(
637 verbose_count: u8,
638 capacity: usize,
639 app_name: &str,
640 crate_version: impl Into<String>,
641) -> (log_buffer::LogBuffer, error_capture::ErrorStore) {
642 use tracing_subscriber::Layer as _;
643 use tracing_subscriber::layer::SubscriberExt;
644 use tracing_subscriber::util::SubscriberInitExt;
645
646 let default_filter = match verbose_count {
647 0 => "warn",
648 1 => "info",
649 2 => "debug",
650 _ => "trace",
651 };
652 let stderr_filter = tracing_subscriber::EnvFilter::try_from_default_env()
653 .unwrap_or_else(|_| tracing_subscriber::EnvFilter::new(default_filter));
654 let buffer_filter = tracing_subscriber::EnvFilter::try_from_env("RUST_LOG_BUFFER")
655 .unwrap_or_else(|_| tracing_subscriber::EnvFilter::new("info"));
656
657 let buffer = log_buffer::LogBuffer::new(capacity);
658 let (capture_layer, store) = error_capture::bug_capture_layer(
659 app_name,
660 error_capture::DEFAULT_CAPTURE_CAPACITY,
661 crate_version,
662 );
663
664 let fmt_layer = tracing_subscriber::fmt::layer()
665 .with_writer(std::io::stderr)
666 .with_target(false)
667 .with_filter(stderr_filter);
668 let buf_layer = log_buffer::LogBufferLayer::new(buffer.clone()).with_filter(buffer_filter);
669 // All three layers are composed in one try_init so subsequent try_init
670 // calls from other code paths become no-ops and do not race with ours.
671 let _ = tracing_subscriber::registry()
672 .with(fmt_layer)
673 .with(buf_layer)
674 .with(capture_layer)
675 .try_init();
676 (buffer, store)
677}
678
679/// Disable coloured terminal output when requested or when stdout is not a TTY.
680///
681/// Why: Pipe-friendly output is mandatory for scripting (`trusty-search list
682/// | jq …`). `NO_COLOR` / `TERM=dumb` are the canonical signals; passing
683/// `--no-color` should override too.
684/// What: calls `colored::control::set_override(false)` when the caller asks
685/// for it or when the standard heuristics indicate no colour.
686/// Test: side-effecting global; trivially covered by manual `NO_COLOR=1 cargo
687/// run -- list`.
688pub fn maybe_disable_color(no_color: bool) {
689 let env_says_no =
690 std::env::var("NO_COLOR").is_ok() || std::env::var("TERM").as_deref() == Ok("dumb");
691 if no_color || env_says_no {
692 colored::control::set_override(false);
693 }
694}
695
696// ─── OpenRouter ───────────────────────────────────────────────────────────
697
698const OPENROUTER_URL: &str = "https://openrouter.ai/api/v1/chat/completions";
699const HTTP_REFERER: &str = "https://github.com/bobmatnyc/trusty-common";
700const X_TITLE: &str = "trusty-common";
701const OPENROUTER_CONNECT_TIMEOUT_SECS: u64 = 10;
702const OPENROUTER_REQUEST_TIMEOUT_SECS: u64 = 120; // chat completions can take 60–90s
703
704/// OpenAI-compatible chat message.
705///
706/// Why: Both trusty-memory's `chat` subcommand and trusty-search's `/chat`
707/// endpoint speak the OpenRouter format. Sharing the struct keeps them in
708/// step (and lets callers compose chat histories without re-defining types).
709/// Tool-use additions (`tool_call_id`, `tool_calls`) follow the OpenAI
710/// function-calling shape: assistant messages set `tool_calls` when the model
711/// requests tool invocations; subsequent `role: "tool"` messages echo the
712/// matching `tool_call_id` with the tool's result in `content`.
713/// What: `role` is one of `"system" | "user" | "assistant" | "tool"`.
714/// `content` is the message text. `tool_call_id` is the id of the tool call
715/// this message is replying to (only set when `role == "tool"`). `tool_calls`
716/// is the raw OpenAI `tool_calls` array on an assistant message that asked
717/// to invoke tools — kept as `serde_json::Value` so we don't drop any fields
718/// the upstream may add.
719/// Test: serde round-trip in `chat_message_round_trips`.
720#[derive(Debug, Clone, Serialize, Deserialize)]
721pub struct ChatMessage {
722 pub role: String,
723 pub content: String,
724 #[serde(skip_serializing_if = "Option::is_none", default)]
725 pub tool_call_id: Option<String>,
726 #[serde(skip_serializing_if = "Option::is_none", default)]
727 pub tool_calls: Option<Vec<serde_json::Value>>,
728}
729
730#[derive(Debug, Serialize)]
731struct ChatRequest<'a> {
732 model: &'a str,
733 messages: &'a [ChatMessage],
734 stream: bool,
735}
736
737#[derive(Debug, Deserialize)]
738struct ChatResponse {
739 choices: Vec<Choice>,
740}
741
742#[derive(Debug, Deserialize)]
743struct Choice {
744 message: ResponseMessage,
745}
746
747#[derive(Debug, Deserialize)]
748struct ResponseMessage {
749 #[serde(default)]
750 content: String,
751}
752
753/// Send a chat completion request to OpenRouter and return the assistant's
754/// message content.
755///
756/// Why: A one-shot, non-streaming chat call is the common-case helper — used
757/// by trusty-memory's `chat` CLI and trusty-search's `/chat` endpoint.
758/// What: POSTs `{model, messages, stream: false}` to OpenRouter with bearer
759/// auth, decodes the response, and returns `choices[0].message.content`.
760/// Errors propagate as anyhow with HTTP status context.
761/// Test: error paths covered by `openrouter_propagates_http_errors` (uses a
762/// blackhole base URL — no real call).
763#[deprecated(since = "0.3.1", note = "Use OpenRouterProvider::chat_stream instead")]
764pub async fn openrouter_chat(
765 api_key: &str,
766 model: &str,
767 messages: Vec<ChatMessage>,
768) -> Result<String> {
769 if api_key.is_empty() {
770 return Err(anyhow!("openrouter api key is empty"));
771 }
772 let client = reqwest::Client::builder()
773 .connect_timeout(std::time::Duration::from_secs(
774 OPENROUTER_CONNECT_TIMEOUT_SECS,
775 ))
776 .timeout(std::time::Duration::from_secs(
777 OPENROUTER_REQUEST_TIMEOUT_SECS,
778 ))
779 .build()
780 .context("build reqwest client for openrouter_chat")?;
781 let body = ChatRequest {
782 model,
783 messages: &messages,
784 stream: false,
785 };
786 let resp = client
787 .post(OPENROUTER_URL)
788 .bearer_auth(api_key)
789 .header("HTTP-Referer", HTTP_REFERER)
790 .header("X-Title", X_TITLE)
791 .json(&body)
792 .send()
793 .await
794 .context("POST openrouter chat completions")?;
795 let status = resp.status();
796 if !status.is_success() {
797 let text = resp.text().await.unwrap_or_default();
798 return Err(anyhow!("openrouter HTTP {status}: {text}"));
799 }
800 let payload: ChatResponse = resp.json().await.context("decode openrouter response")?;
801 payload
802 .choices
803 .into_iter()
804 .next()
805 .map(|c| c.message.content)
806 .ok_or_else(|| anyhow!("openrouter returned no choices"))
807}
808
809/// Stream chat-completion deltas from OpenRouter through a tokio mpsc channel.
810///
811/// Why: `chat` UIs want incremental tokens for a responsive feel; the
812/// streaming endpoint emits SSE `data:` frames with delta content.
813/// What: POSTs the request with `stream: true`, parses each SSE `data:` line
814/// as a JSON object, extracts `choices[0].delta.content`, and sends each
815/// non-empty chunk to `tx`. The function returns when the stream terminates
816/// (either by `[DONE]` sentinel or by upstream EOF).
817/// Test: integration-only (no offline mock); covered manually via the
818/// trusty-search `/chat` endpoint that re-uses this helper.
819#[deprecated(since = "0.3.1", note = "Use OpenRouterProvider::chat_stream instead")]
820pub async fn openrouter_chat_stream(
821 api_key: &str,
822 model: &str,
823 messages: Vec<ChatMessage>,
824 tx: tokio::sync::mpsc::Sender<String>,
825) -> Result<()> {
826 use futures_util::StreamExt;
827
828 if api_key.is_empty() {
829 return Err(anyhow!("openrouter api key is empty"));
830 }
831 let client = reqwest::Client::builder()
832 .connect_timeout(std::time::Duration::from_secs(
833 OPENROUTER_CONNECT_TIMEOUT_SECS,
834 ))
835 .timeout(std::time::Duration::from_secs(
836 OPENROUTER_REQUEST_TIMEOUT_SECS,
837 ))
838 .build()
839 .context("build reqwest client for openrouter_chat_stream")?;
840 let body = ChatRequest {
841 model,
842 messages: &messages,
843 stream: true,
844 };
845 let resp = client
846 .post(OPENROUTER_URL)
847 .bearer_auth(api_key)
848 .header("HTTP-Referer", HTTP_REFERER)
849 .header("X-Title", X_TITLE)
850 .json(&body)
851 .send()
852 .await
853 .context("POST openrouter chat completions (stream)")?;
854 let status = resp.status();
855 if !status.is_success() {
856 let text = resp.text().await.unwrap_or_default();
857 return Err(anyhow!("openrouter HTTP {status}: {text}"));
858 }
859
860 let mut buf = String::new();
861 let mut stream = resp.bytes_stream();
862 while let Some(chunk) = stream.next().await {
863 let bytes = chunk.context("read openrouter stream chunk")?;
864 let text = match std::str::from_utf8(&bytes) {
865 Ok(s) => s,
866 Err(_) => continue,
867 };
868 buf.push_str(text);
869
870 while let Some(idx) = buf.find('\n') {
871 let line: String = buf.drain(..=idx).collect();
872 let line = line.trim();
873 let Some(payload) = line.strip_prefix("data:").map(str::trim) else {
874 continue;
875 };
876 if payload.is_empty() || payload == "[DONE]" {
877 continue;
878 }
879 let v: serde_json::Value = match serde_json::from_str(payload) {
880 Ok(v) => v,
881 Err(_) => continue,
882 };
883 if let Some(delta) = v
884 .get("choices")
885 .and_then(|c| c.get(0))
886 .and_then(|c| c.get("delta"))
887 .and_then(|d| d.get("content"))
888 .and_then(|c| c.as_str())
889 && !delta.is_empty()
890 && tx.send(delta.to_string()).await.is_err()
891 {
892 // Receiver dropped — caller has lost interest.
893 return Ok(());
894 }
895 }
896 }
897 Ok(())
898}
899
900// ─── Misc helpers ─────────────────────────────────────────────────────────
901
902/// Check whether a path exists and is a directory.
903///
904/// Why: tiny but commonly-needed shim — clearer at call sites than
905/// `path.exists() && path.is_dir()`.
906/// What: returns `true` iff the path exists and metadata reports a directory.
907/// Test: `is_dir_recognises_directories`.
908pub fn is_dir(path: &Path) -> bool {
909 path.metadata().map(|m| m.is_dir()).unwrap_or(false)
910}
911
912#[cfg(test)]
913mod tests {
914 use super::*;
915 use std::sync::Mutex;
916
917 /// Serialises tests that mutate the `TRUSTY_DATA_DIR_OVERRIDE` env var so
918 /// they don't race when `cargo test` runs them in parallel threads.
919 static ENV_LOCK: Mutex<()> = Mutex::new(());
920
921 #[tokio::test]
922 async fn auto_port_walks_forward() {
923 // Bind to an OS-chosen port, then ask auto-port to start there.
924 let occupied = TcpListener::bind("127.0.0.1:0").await.unwrap();
925 let port = occupied.local_addr().unwrap().port();
926 let addr: SocketAddr = format!("127.0.0.1:{port}").parse().unwrap();
927 let next = bind_with_auto_port(addr, 8).await.unwrap();
928 let got = next.local_addr().unwrap().port();
929 assert_ne!(got, port, "expected walk-forward to a different port");
930 }
931
932 #[tokio::test]
933 async fn auto_port_zero_attempts_still_binds_free() {
934 let addr: SocketAddr = "127.0.0.1:0".parse().unwrap();
935 let l = bind_with_auto_port(addr, 0).await.unwrap();
936 assert!(l.local_addr().unwrap().port() > 0);
937 }
938
939 #[test]
940 fn resolve_data_dir_creates_directory() {
941 let _guard = ENV_LOCK.lock().unwrap_or_else(|e| e.into_inner());
942 // Use the override env var so we deterministically control the base
943 // directory cross-platform (macOS's dirs::data_dir ignores HOME).
944 let tmp = tempfile_like_dir();
945 // SAFETY: env mutation; tests in this module run serially via
946 // #[test] threading isolation only when MUTEX-guarded — we accept
947 // the residual risk since the override var is unique to these tests.
948 unsafe {
949 std::env::set_var(DATA_DIR_OVERRIDE_ENV, &tmp);
950 }
951 let dir = resolve_data_dir("trusty-test-xyz").unwrap();
952 assert!(
953 dir.exists(),
954 "data dir should be created at {}",
955 dir.display()
956 );
957 assert!(dir.is_dir());
958 assert!(
959 dir.starts_with(&tmp),
960 "data dir {} should live under override {}",
961 dir.display(),
962 tmp.display()
963 );
964 unsafe {
965 std::env::remove_var(DATA_DIR_OVERRIDE_ENV);
966 }
967 }
968
969 #[test]
970 fn daemon_addr_round_trips() {
971 let _guard = ENV_LOCK.lock().unwrap_or_else(|e| e.into_inner());
972 let tmp = tempfile_like_dir();
973 // SAFETY: env mutation; see note in resolve_data_dir_creates_directory.
974 unsafe {
975 std::env::set_var(DATA_DIR_OVERRIDE_ENV, &tmp);
976 }
977 let app = format!(
978 "trusty-test-daemon-{}-{}",
979 std::process::id(),
980 std::time::SystemTime::now()
981 .duration_since(std::time::UNIX_EPOCH)
982 .map(|d| d.as_nanos())
983 .unwrap_or(0)
984 );
985 write_daemon_addr(&app, "127.0.0.1:12345").unwrap();
986 let got = read_daemon_addr(&app).unwrap();
987 unsafe {
988 std::env::remove_var(DATA_DIR_OVERRIDE_ENV);
989 }
990 assert_eq!(got.as_deref(), Some("127.0.0.1:12345"));
991 }
992
993 #[test]
994 fn read_daemon_addr_missing_returns_none() {
995 let _guard = ENV_LOCK.lock().unwrap_or_else(|e| e.into_inner());
996 let tmp = tempfile_like_dir();
997 // SAFETY: env mutation; see note in resolve_data_dir_creates_directory.
998 unsafe {
999 std::env::set_var(DATA_DIR_OVERRIDE_ENV, &tmp);
1000 }
1001 let app = format!(
1002 "trusty-test-daemon-missing-{}-{}",
1003 std::process::id(),
1004 std::time::SystemTime::now()
1005 .duration_since(std::time::UNIX_EPOCH)
1006 .map(|d| d.as_nanos())
1007 .unwrap_or(0)
1008 );
1009 let got = read_daemon_addr(&app).unwrap();
1010 unsafe {
1011 std::env::remove_var(DATA_DIR_OVERRIDE_ENV);
1012 }
1013 assert!(got.is_none(), "expected None when file absent, got {got:?}");
1014 }
1015
1016 #[test]
1017 fn is_dir_recognises_directories() {
1018 let tmp = tempfile_like_dir();
1019 assert!(is_dir(&tmp));
1020 assert!(!is_dir(&tmp.join("nope")));
1021 }
1022
1023 #[test]
1024 fn chat_message_round_trips() {
1025 let m = ChatMessage {
1026 role: "user".into(),
1027 content: "hello".into(),
1028 tool_call_id: None,
1029 tool_calls: None,
1030 };
1031 let s = serde_json::to_string(&m).unwrap();
1032 let back: ChatMessage = serde_json::from_str(&s).unwrap();
1033 assert_eq!(back.role, "user");
1034 assert_eq!(back.content, "hello");
1035 }
1036
1037 #[tokio::test]
1038 #[allow(deprecated)]
1039 async fn openrouter_chat_rejects_empty_key() {
1040 let err = openrouter_chat("", "x", vec![]).await.unwrap_err();
1041 assert!(err.to_string().contains("api key"));
1042 }
1043
1044 #[tokio::test]
1045 async fn check_already_running_returns_none_when_file_missing() {
1046 // Why: a fresh machine (no prior daemon) must skip the probe entirely
1047 // and let the caller proceed with normal startup.
1048 let tmp = tempfile_like_dir();
1049 let missing = tmp.join("does-not-exist");
1050 let got = check_already_running(&missing, "/health").await;
1051 assert!(got.is_none());
1052 }
1053
1054 #[tokio::test]
1055 async fn check_already_running_returns_none_when_file_empty() {
1056 // Why: a half-written / truncated address file should be treated as
1057 // "no daemon" and the stale file cleared so the next start does not
1058 // see it again.
1059 let tmp = tempfile_like_dir();
1060 let path = tmp.join("http_addr");
1061 std::fs::write(&path, " \n ").unwrap();
1062 let got = check_already_running(&path, "/health").await;
1063 assert!(got.is_none());
1064 assert!(
1065 !path.exists(),
1066 "empty address file should be cleaned up by check_already_running"
1067 );
1068 }
1069
1070 #[tokio::test]
1071 async fn check_already_running_returns_none_when_address_dead() {
1072 // Why: a stale address (daemon previously crashed) must NOT block a
1073 // fresh start; the helper must probe, see no listener, clear the file,
1074 // and report "no daemon".
1075 let tmp = tempfile_like_dir();
1076 let path = tmp.join("http_addr");
1077 // Reserved unbound port — TCP connect will fail fast.
1078 std::fs::write(&path, "127.0.0.1:1\n").unwrap();
1079 let got = check_already_running(&path, "/health").await;
1080 assert!(got.is_none(), "dead address should map to None");
1081 assert!(
1082 !path.exists(),
1083 "stale address file should be cleaned up by check_already_running"
1084 );
1085 }
1086
1087 #[tokio::test]
1088 async fn check_already_running_returns_url_when_health_ok() {
1089 // Why: positive control — when a daemon really is listening and
1090 // returns 2xx on the health path, the helper must report its URL so
1091 // the caller can refuse to spawn a duplicate.
1092 // What: spin up a one-shot mini HTTP server on an ephemeral port that
1093 // answers `GET /health → 200`, write the address to the file, and
1094 // confirm the helper returns the expected URL.
1095 let listener = tokio::net::TcpListener::bind("127.0.0.1:0").await.unwrap();
1096 let local = listener.local_addr().unwrap();
1097 let server = tokio::spawn(async move {
1098 use tokio::io::{AsyncReadExt, AsyncWriteExt};
1099 if let Ok((mut sock, _)) = listener.accept().await {
1100 let mut buf = [0u8; 1024];
1101 let _ = sock.read(&mut buf).await;
1102 let _ = sock
1103 .write_all(b"HTTP/1.1 200 OK\r\nContent-Length: 2\r\n\r\nok")
1104 .await;
1105 let _ = sock.shutdown().await;
1106 }
1107 });
1108
1109 let tmp = tempfile_like_dir();
1110 let path = tmp.join("http_addr");
1111 std::fs::write(&path, format!("{local}\n")).unwrap();
1112
1113 let got = check_already_running(&path, "/health").await;
1114 assert_eq!(got.as_deref(), Some(format!("http://{local}").as_str()));
1115 assert!(
1116 path.exists(),
1117 "address file must be preserved when the daemon is healthy"
1118 );
1119 let _ = server.await;
1120 }
1121
1122 // Test-only helper: makes a unique scratch dir without pulling in tempfile
1123 // as a dev-dep (keeps the dependency surface minimal).
1124 fn tempfile_like_dir() -> PathBuf {
1125 let pid = std::process::id();
1126 let nanos = std::time::SystemTime::now()
1127 .duration_since(std::time::UNIX_EPOCH)
1128 .map(|d| d.as_nanos())
1129 .unwrap_or(0);
1130 let p = std::env::temp_dir().join(format!("trusty-common-test-{pid}-{nanos}"));
1131 std::fs::create_dir_all(&p).unwrap();
1132 p
1133 }
1134}