Skip to main content

Module discovery

Module discovery 

Source
Expand description

Daemon discovery for crate::tui_client::CodeEngine (issue #3415, DOC-50 §3.4).

Why: tcode tui needs to find an already-running tcode serve --http daemon without the operator hand-copying a port. DOC-50 §3.4 specifies the lookup priority — explicit override, then a discovery file, then a liveness check — but its prose sketched a NEW ~/.trusty-code/daemon.json shape; crate::serve::discovery documents why this crate follows the ALREADY-ESTABLISHED sibling convention (trusty-memory/trusty-search’s plain-text http_addr file) instead. This module is the READ side of that convention; crate::serve::discovery’s write_http_addr_file is the write side, called from crate::serve::http::run_http. What: lookup_daemon tries, in order, DAEMON_URL_ENV (an explicit override — trusted without a liveness check of its own source, but still ping-verified before use, same as the file-sourced candidate) then the http_addr discovery file; whichever candidate is found is verified alive via GET {url}/health (the SAME route crate::serve::methods::health_payload answers, already the ecosystem-standard liveness probe per crate::serve::http’s docs). It reports the outcome as a three-way Lookup — live, found-but-dead (naming its Source), or no candidate at all — because a CALLER has to branch on WHICH source failed: tcode tui auto-spawns a daemon when the discovery file is stale or absent, but must NOT when DAEMON_URL_ENV explicitly named an address, since spawning something at a DIFFERENT address would quietly ignore that instruction (#4512). A live daemon additionally carries the ReportedBinding it published, because “a daemon is answering” is NOT the same question as “that daemon serves the project I mean to work in” — see ReportedBinding. discover_daemon_url is the attach-only wrapper over it, collapsing the same three outcomes into a DiscoveryError with an actionable message — never a silent None/default. Test: discovery_tests::* for the pure candidate-selection logic (env var precedence, file fallback, “neither present” case), the Lookup-to-DiscoveryError collapse, and ReportedBinding parsing; the liveness-ping branch is covered end-to-end in tests/tui_client_engine.rs against a mock daemon (a unit test would need a real bound socket to ping, which belongs in that hermetic integration suite, not here).

Enums§

DiscoveryError
See module docs.
Lookup
The three distinguishable outcomes of one daemon lookup.
ReportedBinding
The project binding a live daemon published on GET /health (#4512).
Source
Which discovery source produced a candidate URL — carried into Lookup::Dead (and from there into DiscoveryError::NotAlive) so the error message tells the operator WHERE the stale/wrong pointer came from, and so tcode tui’s auto-spawn can tell an EXPLICIT instruction it must obey (Source::Env) from a stale file it may replace (Source::File).

Constants§

DAEMON_URL_ENV
Environment variable that, when set to a non-empty value, names the daemon URL directly — highest-priority discovery source (DOC-50 §3.4 point 1).

Functions§

discover_daemon_url
Resolve and verify a live daemon URL, failing when none can be attached to — the attach-only view of lookup_daemon.
lookup_daemon
Resolve a candidate daemon URL and report whether it is live, per this module’s docs.