D: reuse the shared proxy bounds and status table in MCP peers

The peer follow stream had no byte cap and kept a whole unterminated
frame in memory, and the MCP surface carried its own peer status table
that already disagreed with REST for peer 400.

- mcp::peers::fetch now returns proxy::PreparedResponse, so the status
  comes from proxy::classify_status and the body from
  proxy::prepare_response (peer byte cap, deadline, shutdown), the same
  two steps REST uses. status_error, body_bytes and the local 2xx check
  are gone.
- tail_journal consumes that prepared stream; a Frames cursor keeps a
  partial frame from being rescanned from byte 0 on every chunk, the
  entry cap truncates the result, and the end of the stream maps to the
  note (entry cap, shutdown, byte cap, deadline or close).
- Prompts resolve the peer argument against the registry (unknown or
  disabled peer is refused); list_peers returns the error registry()
  produced; one wording for an unknown peer name.
- Tests: byte cap on an unterminated frame, entry cap, buffered body,
  follow 404/500, deadline with an open stream, shutdown, frame cursor,
  REST/MCP parity for peer 400/404/401/5xx, and a full golden snapshot of
  the peers-off tool table. Peer scripts use the stripped path form C2
  forwards.
This commit is contained in:
ari committed 2026-10-19 23:09:45 -04:00
1 parent e2c553dbe4
commit c0e76607f6
5 files changed
+592 -213

No files matched your search

+78 -7
View File
@@ -72,13 +72,28 @@ fn init_body() -> &'static str {
impl Harness {
async fn post(&self, body: &str, sid: Option<&str>) -> (u16, String, String) {
let mut s = TcpStream::connect(("127.0.0.1", self.port)).await.unwrap();
let host = format!("127.0.0.1:{}", self.port);
let mut headers: Vec<(&str, &str)> = MCP_HEADERS.to_vec();
if let Some(sid) = sid {
headers.push(("Mcp-Session-Id", sid));
}
let req = common::request("POST", "/mcp", &host, &headers, Some(body));
self.send("/mcp", &headers, Some(body)).await
}
async fn get(&self, path: &str) -> (u16, String, String) {
self.send(path, &[], None).await
}
/// One request on a fresh connection, read to close.
async fn send(
&self,
path: &str,
headers: &[(&str, &str)],
body: Option<&str>,
) -> (u16, String, String) {
let method = if body.is_some() { "POST" } else { "GET" };
let mut s = TcpStream::connect(("127.0.0.1", self.port)).await.unwrap();
let host = format!("127.0.0.1:{}", self.port);
let req = common::request(method, path, &host, headers, body);
s.write_all(req.as_bytes()).await.unwrap();
let mut buf = Vec::new();
let _ = tokio::time::timeout(WAIT, async {
@@ -191,7 +206,13 @@ async fn tool_list_is_ten_with_peers_and_eight_without() {
let tools = h.tools(&sid).await;
let mut names: Vec<&str> = tools.iter().map(|t| t["name"].as_str().unwrap()).collect();
names.sort();
assert_eq!(names, skyg_linux_tracker::mcp::peers::tool_names());
// A fixture, not read back from the router: renaming or dropping a tool
// has to be an explicit edit here too.
assert_eq!(
names.join(","),
"get_container,get_container_logs,get_journal_logs,get_peer,get_systemd_unit,\
list_containers,list_peers,list_systemd_timers,list_systemd_units,tail_journal"
);
let units = tool(&tools, "list_systemd_units");
assert!(
units["inputSchema"]["properties"]["peer"].is_object(),
@@ -274,7 +295,7 @@ async fn peer_tools_are_absent_without_configuration() {
async fn peer_argument_reads_through_the_consolidator() {
let fake = FakeTransport::new();
fake.peer("beta").respond(
"/api/v1/systemd/units",
"/systemd/units",
200,
"application/json",
r#"[{"name":"peer-only.service","active_state":"failed"}]"#,
@@ -296,7 +317,7 @@ async fn peer_argument_reads_through_the_consolidator() {
let calls = fake.calls();
assert_eq!(calls.len(), 1);
assert_eq!(calls[0].1.path, "/api/v1/systemd/units");
assert_eq!(calls[0].1.path, "/systemd/units");
assert_eq!(
calls[0].1.query,
vec![("state".to_owned(), "failed".to_owned())]
@@ -316,11 +337,61 @@ async fn peer_argument_reads_through_the_consolidator() {
assert_eq!(fake.calls().len(), 1, "local calls never reach a peer");
}
/// Both surfaces classify a peer status with the same table, so a client
/// sees one error per peer answer whichever it uses.
#[tokio::test]
async fn rest_and_mcp_report_the_same_peer_status_errors() {
let rows = [
(400, 503, "unavailable", "peer beta: http_400"),
(404, 404, "not_found", "peer beta: not found on peer"),
(
401,
503,
"unavailable",
"peer beta: peer rejected credentials",
),
(500, 503, "unavailable", "peer beta: http_500"),
];
for (peer_status, http_status, code, message) in rows {
let fake = FakeTransport::new();
fake.peer("beta")
.respond("/systemd/units", peer_status, "application/json", "{}");
let h = start(peers_state(&fake)).await;
let (status, head, body) = h.get("/api/v1/peers/beta/systemd/units").await;
assert_eq!(status, http_status, "peer {peer_status}: {head}\n{body}");
let rest: serde_json::Value = serde_json::from_str(&body).unwrap();
assert_eq!(rest["error"]["code"], code, "peer {peer_status}: {rest}");
assert_eq!(
rest["error"]["message"], message,
"peer {peer_status}: {rest}"
);
assert_eq!(
head.lines()
.find_map(|l| l.strip_prefix("x-slinuxm-peer: "))
.map(str::trim),
Some("beta"),
"{head}"
);
let sid = h.open().await;
let v = h
.call(&sid, 2, "list_systemd_units", r#"{"peer":"beta"}"#)
.await;
assert_eq!(v["result"]["isError"], true, "peer {peer_status}: {v}");
let text = result_text(&v);
assert!(
text.starts_with(&format!("{code}: {message}.")),
"peer {peer_status} over MCP: {text}"
);
}
}
#[tokio::test]
async fn peer_failures_are_visible_tool_errors() {
let fake = FakeTransport::new();
fake.peer("beta").respond_err(
"/api/v1/systemd/units",
"/systemd/units",
skyg_linux_tracker::peers::PeerError::Rejected,
);
let h = start(peers_state(&fake)).await;