Skip to main content

Module discovery

Module discovery 

Source
Expand description

tcode serve --http daemon discovery file (issue #3415, DOC-50 §3.4).

Why: a client needs to find the daemon’s TCP listener without the operator copying a port number by hand. Since #6637 that client is trusty-code-gui’s webview alone — tcode tui dials the daemon’s Unix socket, whose path is derived rather than discovered — so this file and the listener it points at retire together in PR 2. DOC-50 §3.4’s prose sketched a NEW, tcode-specific JSON file (~/.trusty-code/daemon.json, {daemon_url, pid, started_at}) for this — but this crate’s siblings already solve the identical problem, and their convention is different: trusty-memory (which wrote one until #6286 retired both the file and its listener) and trusty-search (crates/trusty-search/src/service/daemon.rs) both write the bound host:port as PLAIN TEXT to a file named http_addr under trusty_common::resolve_data_dir(<app_name>), atomically (tmp + rename) so a reader never observes a half-written value. This module follows THAT established convention for tcode instead of inventing a second, JSON-shaped one: pid/started_at have no consumer anywhere in this codebase yet, and a plain-text host:port is exactly what a client needs to build http://{addr}. What: http_addr_path resolves {resolve_data_dir("trusty-code")}/http_addr (server AND client side share this one path-resolution function so they can never drift onto two different locations); [write_http_addr_file] (called from crate::serve::http::run_http after binding) atomically writes the bound address; [remove_http_addr_file] (called on graceful shutdown) clears it so a stopped daemon doesn’t leave a stale pointer for the next reader; read_http_addr_file (test-only since #6637; its one production caller went with tui_client::discovery) reads and trims it back. Every operation is best-effort: a write or remove failure leaves a client with no pointer to the TCP listener, which it reports as no daemon rather than as a crash, and never stops the daemon serving — the socket is bound first and is what every other client uses. A read failure (file absent, stale content, unreadable) means “no candidate from this source,” not a hard error. Test: discovery_tests::*.

Constants§

HTTP_ADDR_FILENAME
Filename of the discovery file, written under resolve_data_dir("trusty-code") — mirrors what trusty-memory’s http_addr convention exactly (same filename, same directory-resolution helper, same plain-text host:port content).

Functions§

http_addr_path
Resolve {resolve_data_dir("trusty-code")}/http_addr, or None if the data directory cannot be resolved (matches retired http_addr_path did: the degrade-gracefully contract — a resolution failure here is never fatal, only a missing discovery source).