trusty-code 0.8.0

Per-project Claude-Code-compatible MPM orchestration harness (bin: tcode) — an original coding harness, tracked under epic #2052.
//! Proof-of-life JSON-RPC methods for `tcode serve` (#2053).
//!
//! Why: the transport + router need at least one real method to prove the
//! whole path works end-to-end without pulling in `session.*`/`task.*`/
//! `harness.describe` scope (those land in #2054/#2056/#2066). `ping` and
//! `health` are the minimal, side-effect-free pair for that job, shared
//! verbatim across both transports: the STDIO and socket `health` methods
//! are one registration, so they cannot drift into different shapes. #6637
//! slice 2c retired the third caller, an anonymous HTTP `GET /health` route,
//! and the trimmed `liveness_payload` it answered.
//! What: `register` wires both methods into a [`Router`]; `ping` returns
//! `{"pong": true}`; `health` returns [`health_payload`]'s server name,
//! crate version, static `"ok"` status, pid, and the daemon's project
//! binding.
//! Test: `methods::tests::*`.

use std::sync::Arc;

use serde_json::{Value, json};

use crate::binding::ProjectBinding;
use crate::jsonrpc::{ConnectionContext, Router, RpcError};

/// Register every proof-of-life method onto `router`.
///
/// Why: keeps `crate::serve::build_router` a one-line call as more method
/// groups (session/task/harness) register themselves the same way later.
/// `binding` is taken here — rather than `health` staying a bare `fn` — so
/// the daemon can REPORT which project it serves (#4512): a daemon binds
/// exactly one `ProjectBinding` for its whole life, and a client that
/// auto-attaches to a daemon it did not start has no other way to tell
/// whether it is about to operate against the wrong project.
/// What: registers `"ping"` and `"health"`; `binding` is shared into the
/// `health` closure behind an `Arc` so answering a probe never clones a
/// `PathBuf`.
/// Test: `methods_register_wires_ping_and_health`,
/// `health_reports_the_daemons_project_binding`.
pub fn register(router: &mut Router, binding: ProjectBinding) {
    router.register("ping", ping);
    let binding = Arc::new(binding);
    router.register("health", move |params: Value, ctx: ConnectionContext| {
        let binding = binding.clone();
        async move { health(&binding, params, ctx).await }
    });
}

/// `ping` — returns `{"pong": true}` regardless of `params`.
///
/// Why: the cheapest possible round-trip proof that a transport and the
/// router are wired correctly.
/// What: ignores `params` and the connection context, never fails.
/// Test: `ping_returns_pong_true`.
async fn ping(_params: Value, _ctx: ConnectionContext) -> Result<Value, RpcError> {
    Ok(json!({"pong": true}))
}

/// `health` — returns server identity, crate version, status, and binding.
///
/// Why: a slightly richer proof-of-life than `ping` that a caller can use to
/// confirm which build of `tcode` — and, since #4512, which PROJECT — it is
/// talking to.
/// What: ignores `params` and the connection context, never fails; the
/// payload is [`health_payload`].
/// Test: `health_returns_server_identity`,
/// `health_reports_the_daemons_project_binding`.
async fn health(
    binding: &ProjectBinding,
    _params: Value,
    _ctx: ConnectionContext,
) -> Result<Value, RpcError> {
    Ok(health_payload(binding))
}

/// The shared `health` payload:
/// `{"server","version","status","pid","binding"}`.
///
/// Why: pulled out of the `health` JSON-RPC handler so a second caller can
/// return the identical shape without duplicating the `json!` literal — one
/// source of truth for what "healthy" means.
///
/// `pid` and `binding` are ADDITIVE (#4512) — every field that was here
/// before is unchanged, so an older client that only reads
/// `server`/`version`/`status` keeps working. They exist because a daemon
/// binds exactly one `ProjectBinding` at `build_router` time and holds it for
/// its whole life, while `tcode tui` now AUTO-ATTACHES to whatever daemon
/// discovery finds. Without the binding on the wire, a TUI launched in
/// project B silently drives project A's daemon; `pid` is what lets an
/// operator (or a test) identify and signal the exact process it found.
/// What: `server = "tcode"`, `version = crate::VERSION`
/// (`CARGO_PKG_VERSION`), `status = "ok"`, `pid = std::process::id()`,
/// `binding = ProjectBinding::to_json()` (the SAME `{state, root}` wire shape
/// `Session` already serialises, not a second spelling). Never fails — there
/// is no fallible state to check.
///
/// `incremental_index` is additive too (#2798). The write/edit tool executors
/// hand every successful write to a BOUNDED background pool, which loses work
/// two ways when a degraded trusty-search daemon backs it up. Both used to
/// exist only as a `warn!` line, so a sustained episode — files silently not
/// indexed for as long as it lasts — looked identical to a healthy daemon here.
/// This is the same process that owns the counters, and the TUI already polls
/// this payload, so it is where the loss becomes visible.
///
/// Each loss is a count plus an age, and the age (`null` until the first one)
/// is what separates "happened once an hour ago" from "happening right now".
/// The two losses stay separate numbers because they need different fixes:
/// `dropped_batches` means the pool refused the batch outright and none of it
/// ran, while `truncated_batches` means the pool accepted and started it and
/// the 30s per-batch budget then cut it short partway, abandoning the files it
/// had not reached. Reporting only drops would read `0` throughout an episode
/// in which every batch is accepted and then truncated.
/// `build` is additive too (#8164) — the short git SHA `build.rs` embeds,
/// which is what `tcode tui`'s startup splash compares against its own to
/// warn that the daemon it attached to is a different build.
/// Test: `health_payload_has_expected_shape`,
/// `health_payload_reports_a_bound_project_root`,
/// `health_payload_reports_the_daemon_build_sha`,
/// `health_payload_reports_incremental_index_drops`,
/// `health_payload_reports_incremental_index_truncations`.
pub(crate) fn health_payload(binding: &ProjectBinding) -> Value {
    // #2798: publish the background-index loss counters where a health check
    // already looks, so a saturated pool is not invisible.
    let drops = trusty_common::search_index::index_drop_stats();
    json!({
        "server": "tcode",
        "version": crate::VERSION,
        // #8164: the SHA, not just the semver. A daemon started before a
        // reinstall reports the same `version` as the fresh binary that
        // replaced it, so a client had no way to see it was talking to a
        // stale process — that gap reproduced #4602 after its fix shipped.
        // Same constant `tcode --version` prints.
        "build": crate::build_info::GIT_HASH,
        "status": "ok",
        "pid": std::process::id(),
        "binding": binding.to_json(),
        "incremental_index": {
            "dropped_batches": drops.dropped_batches,
            "seconds_since_last_drop": drops.seconds_since_last_drop,
            "truncated_batches": drops.truncated_batches,
            "seconds_since_last_truncation": drops.seconds_since_last_truncation,
        },
    })
}

#[cfg(test)]
mod tests {
    use super::*;
    use tokio::sync::mpsc;

    fn test_ctx() -> ConnectionContext {
        let (tx, _rx) = mpsc::unbounded_channel();
        ConnectionContext::new(tx)
    }

    /// `ping` must return `{"pong": true}` and never fail.
    #[tokio::test]
    async fn ping_returns_pong_true() {
        let result = ping(Value::Null, test_ctx())
            .await
            .expect("ping must not fail");
        assert_eq!(result, json!({"pong": true}));
    }

    /// `health` must report the server name, version, and "ok" status.
    #[tokio::test]
    async fn health_returns_server_identity() {
        let result = health(&ProjectBinding::None, Value::Null, test_ctx())
            .await
            .expect("health must not fail");
        assert_eq!(result["server"], "tcode");
        assert_eq!(result["status"], "ok");
        assert_eq!(result["version"], crate::VERSION);
    }

    /// `health_payload` (the one `health` body both transports answer) must carry
    /// the original three fields UNCHANGED plus #4512's additive `pid` and
    /// `binding` — pinned so the backward-compatible promise in this
    /// function's docs can't be broken by a later edit.
    #[test]
    fn health_payload_has_expected_shape() {
        let v = health_payload(&ProjectBinding::None);
        assert_eq!(v["server"], "tcode");
        assert_eq!(v["status"], "ok");
        assert_eq!(v["version"], crate::VERSION);
        assert_eq!(v["pid"], std::process::id());
        assert_eq!(v["binding"]["state"], crate::binding::STATE_PROJECTLESS);
        assert!(v["binding"]["root"].is_null());
    }

    /// The daemon's own build SHA must be on the wire (#8164), and it must be
    /// the SAME constant `tcode --version` prints.
    ///
    /// Why: a client can already read `version`, but two builds of the same
    /// semver are indistinguishable by it — which is exactly the stale-daemon
    /// case the splash's mismatch warning exists to catch. Asserting against
    /// `build_info::GIT_HASH` (rather than merely "a non-empty string") is
    /// what pins the two surfaces to one source; a second, independently
    /// derived SHA here would satisfy a looser assertion while reporting a
    /// different build.
    /// What: the field is present and equals `build_info::GIT_HASH`, which is
    /// the literal `"unknown"` outside a git checkout — a real, reportable
    /// value, so no case is excluded.
    /// Test: this test.
    #[test]
    fn health_payload_reports_the_daemon_build_sha() {
        let v = health_payload(&ProjectBinding::None);
        assert_eq!(v["build"], crate::build_info::GIT_HASH);
        assert!(
            v["build"].as_str().is_some_and(|s| !s.is_empty()),
            "the build field must always carry a value, got {v}"
        );
    }

    /// The background-index drop counters must be on the wire, and a process
    /// that has dropped nothing must say so distinguishably (#2798).
    ///
    /// Why: dropping a batch when the pool saturates is only an acceptable
    /// trade because the loss is visible somewhere a health check reads. This
    /// pins that it IS on the wire — the field existing is the whole fix, and a
    /// later edit that quietly removes it would restore the blind spot.
    /// What: nothing in this crate's tests submits to the shared index pool, so
    /// `dropped_batches` is provably `0` here — asserted exactly, not merely as
    /// "numeric". The age is then asserted STRICTLY `null`: a `|| is_u64()`
    /// disjunction would pass just as happily if a later edit reported a
    /// misleading `0`, which is the regression this test exists to catch.
    /// Test: this test.
    #[test]
    fn health_payload_reports_incremental_index_drops() {
        let v = health_payload(&ProjectBinding::None);
        let drops = &v["incremental_index"];
        assert_eq!(
            drops["dropped_batches"], 0,
            "no test in this crate submits to the pool, so nothing can have been \
             dropped, got {drops}"
        );
        assert!(
            drops["seconds_since_last_drop"].is_null(),
            "with no drops the age must be null, never a misleading 0, got {drops}"
        );
    }

    /// The budget-truncation counters must be on the wire too, and must be
    /// their own fields rather than folded into the drop counters (#2798).
    ///
    /// Why: a batch the pool ACCEPTS and then cuts short at the 30s budget is a
    /// different fault from one the pool refuses — different cause, different
    /// fix — and an operator who cannot tell them apart cannot act on either.
    /// Summing them, or publishing only the drop pair, would leave an episode
    /// of repeated truncations reading as a perfectly healthy `0`.
    /// What: asserts both truncation fields are present under
    /// `incremental_index` and are NOT the same JSON values as the drop pair's
    /// keys; as with drops, a fresh test process has truncated nothing, so the
    /// count is exactly `0` and the age is strictly `null`.
    /// Test: this test.
    #[test]
    fn health_payload_reports_incremental_index_truncations() {
        let v = health_payload(&ProjectBinding::None);
        let drops = &v["incremental_index"];
        assert_eq!(
            drops["truncated_batches"], 0,
            "no test in this crate runs a batch, so nothing can have been \
             truncated, got {drops}"
        );
        assert!(
            drops["seconds_since_last_truncation"].is_null(),
            "with no truncations the age must be null, never a misleading 0, got {drops}"
        );
        let keys: Vec<&String> = drops
            .as_object()
            .expect("incremental_index must be an object")
            .keys()
            .collect();
        assert!(
            keys.contains(&&"dropped_batches".to_string())
                && keys.contains(&&"truncated_batches".to_string()),
            "the two losses must be separate fields, never one summed number, got {keys:?}"
        );
    }

    /// A BOUND daemon must publish its project root, since that is the field
    /// `cli::daemon_autospawn` compares against the client's own project
    /// before attaching (#4512).
    #[test]
    fn health_payload_reports_a_bound_project_root() {
        let dir = tempfile::tempdir().expect("tempdir");
        let binding = ProjectBinding::resolve(Some(dir.path().to_path_buf())).expect("must bind");
        let v = health_payload(&binding);
        assert_eq!(
            v["binding"]["root"],
            json!(binding.label().expect("a bound binding has a root"))
        );
    }

    /// The `health` METHOD (not just the payload helper) must carry the
    /// daemon's binding, so the STDIO transport reports it too.
    #[tokio::test]
    async fn health_reports_the_daemons_project_binding() {
        let dir = tempfile::tempdir().expect("tempdir");
        let binding = ProjectBinding::resolve(Some(dir.path().to_path_buf())).expect("must bind");
        let result = health(&binding, Value::Null, test_ctx())
            .await
            .expect("health must not fail");
        assert_eq!(result["binding"], binding.to_json());
    }

    /// `register` must wire both methods so the router recognises them
    /// (rather than returning method-not-found).
    #[tokio::test]
    async fn methods_register_wires_ping_and_health() {
        use trusty_mcp::Request;

        let mut router = Router::new();
        register(&mut router, ProjectBinding::None);

        for method in ["ping", "health"] {
            let req = Request {
                jsonrpc: Some("2.0".to_string()),
                id: Some(json!(1)),
                method: method.to_string(),
                params: None,
            };
            let resp = router.dispatch(req, &test_ctx()).await;
            assert!(
                resp.error.is_none(),
                "{method} should be registered, got {:?}",
                resp.error
            );
        }
    }
}