Skip to main content

agentstategraph_mcp/
lib.rs

1//! Library surface of `agentstategraph-mcp`.
2//!
3//! The binary (`src/main.rs`) re-uses these modules. Exposing them as a
4//! library lets integration tests under `tests/` exercise the HTTP
5//! router, auth middleware, and rate-limit wiring directly against the
6//! same code paths the server runs in production.
7
8pub mod auth;
9pub mod http;
10pub mod policy_signing;
11pub mod server;
12pub mod store_gauges;
13
14/// The trace the current work belongs to, from oxtel's current span context,
15/// as a commit links to it (DESIGN.md Plan H t-009). The server passes it to
16/// [`agentstategraph::link_commits_to_traces`] unless `ASG_TRACE_LINKS` is
17/// off.
18pub fn current_trace() -> Option<agentstategraph::CommitTrace> {
19    oxtel::sdk::trace::current_span_context()
20        .filter(|context| context.is_valid())
21        .map(|context| agentstategraph::CommitTrace {
22            trace_id: context.trace_id.to_string(),
23            span_id: context.span_id.to_string(),
24        })
25}
26
27/// The server's trace filter (DESIGN.md Plan H t-011). Spans at `info`, but
28/// not one per SQL statement: that cost a small read more than anything else
29/// telemetry does. `ASG_SQL_SPANS=on` adds them back, and `RUST_LOG`, when
30/// set, replaces the whole filter. `db.client.operation.duration` records
31/// either way.
32pub fn traces_filter() -> String {
33    if let Some(directives) = std::env::var("RUST_LOG")
34        .ok()
35        .filter(|d| !d.trim().is_empty())
36    {
37        return directives;
38    }
39    let sql_spans = std::env::var("ASG_SQL_SPANS").unwrap_or_default();
40    match sql_spans.trim().to_ascii_lowercase().as_str() {
41        "1" | "on" | "true" | "yes" => "info".to_string(),
42        _ => "info,oxtel_sql=warn".to_string(),
43    }
44}
45
46/// Whether each commit is logged as an `asg.commit` event (DESIGN.md Plan H
47/// t-010): only when `ASG_COMMIT_EVENTS` is `1`, `on`, `true` or `yes`.
48pub fn commit_events_from_env() -> bool {
49    let value = std::env::var("ASG_COMMIT_EVENTS").unwrap_or_default();
50    matches!(
51        value.trim().to_ascii_lowercase().as_str(),
52        "1" | "on" | "true" | "yes"
53    )
54}
55
56/// The log filter directive that lets `asg.commit` events through whatever
57/// level the rest of the log runs at.
58pub const COMMIT_EVENTS_DIRECTIVE: &str = "agentstategraph::provenance=info";
59
60/// Whether commits are linked to their traces: yes, unless `ASG_TRACE_LINKS`
61/// is `0`, `off`, `false` or `no`.
62pub fn trace_links_from_env() -> bool {
63    let value = std::env::var("ASG_TRACE_LINKS").unwrap_or_default();
64    !matches!(
65        value.trim().to_ascii_lowercase().as_str(),
66        "0" | "off" | "false" | "no"
67    )
68}