Tests become directory modules named after what they test, with a shared fixtures/harness module:
control-plane/src/{responses,ggml_bridge,gguf_f32_stream,prepare_preview}/tests/, desktop-core/src/runtime_rpc/tests/;
integration targets become tests/<name>/main.rs plus topic modules and a harness (backend_worker_http, vision_http,
memory_gate_http, tool_corpus, mcp_hub, api_over_runtime, runtime_gateway_sdet, native_tools_sdet,
native_tools_retry_sdet, managed_worker, gateway); target names are unchanged. Sources: leleka-inference.rs becomes
src/bin/leleka-inference/ (main.rs plus one module per operator option group), the cp_replay example becomes
examples/cp_replay/, and the tonic service impl of runtime_rpc delegates to session_rpc, model_rpc and generate_rpc
(bodies unchanged). impl_* modules are renamed after what they hold (base_provenance, count_tokens, manifest_fetch,
fetcher, record, capture, in_flight, tool_policy, tool_plan, server). The self-spawning tests that name themselves
(credential_child, the managed worker fixture) use the new module path or stay at the target root.
Test ids: 543 before and after in both crates (strata-control, strata-desktop-core, --all-targets), 0 missing, 0 new,
0 outcome changes; 241 ids differ only in the module path. Warnings in the two crates: 0 before and after.
Counts (strata-control / strata-desktop-core): oversize files 19 -> 0 (13 + 6). Functions over 30 lines, complexity 10,
nesting 4, wide lines, prod: 187/135/17/199 -> 187/135/17/196 and 81/59/6/54 -> 81/59/6/54; test: 214/17/30/133 ->
214/17/30/133 and 65/3/3/30 -> 65/3/3/27. The 211 functions that moved verbatim are in limits-allowlist.json (owner
STRA-338) because the diff gate treats a moved function as added; the entries fall as the functions are shortened.
103 lines
3.6 KiB
Rust
103 lines
3.6 KiB
Rust
//! The API request monitor: `GET /api/requests`, the opt-in inspection of the last requests' prompts and answers of the Python
|
|
//! service (`api_monitor` in its config; `engine/strata/serve/server.py` `begin_request` / `request_records`, tests in
|
|
//! `serve/test_monitor.py`).
|
|
//!
|
|
//! It keeps the last [`RETENTION`] requests of the three generation dialects (`/v1/chat/completions`, `/v1/messages`,
|
|
//! `/v1/responses`) in memory: the request body as pretty JSON, the generated text and reasoning, the usage, the error, the
|
|
//! HTTP status and the wall-clock timings. It is off unless the config sets `"api_monitor": true`, because it holds prompts
|
|
//! and answers in memory; nothing is written to disk and request headers (credentials) are never captured. Every captured
|
|
//! text is bounded to [`CAP`] bytes and says so (`*_truncated`).
|
|
//!
|
|
//! The capture is a middleware around the three routes: it reads the request body once (bounded by the 16 MiB route limit),
|
|
//! hands the same bytes on, and either collects a JSON response or taps an event stream chunk by chunk without delaying it.
|
|
//! A stream whose consumer goes away before it ends is recorded as `disconnected`.
|
|
//!
|
|
//! Differences from the Python monitor, all deliberate: `/v1/responses` is monitored too; `queue_s` and `load_s` are `null`
|
|
//! (a request's wait for the engine is not attributed per request here: see `/metrics` `live.queued`); the state is `queued`
|
|
//! until the first generated text, then `generating`, then `completed`, `error` or `disconnected`; there is no HTML page.
|
|
use std::{
|
|
collections::VecDeque,
|
|
pin::Pin,
|
|
sync::{Arc, Mutex},
|
|
task::{Context, Poll},
|
|
time::{Instant, SystemTime, UNIX_EPOCH},
|
|
};
|
|
|
|
use axum::{
|
|
body::{Body, Bytes},
|
|
extract::{Request, State},
|
|
http::{HeaderValue, StatusCode, header},
|
|
middleware::Next,
|
|
response::{IntoResponse, Response},
|
|
};
|
|
use futures_core::Stream;
|
|
use serde_json::{Map, Value, json};
|
|
|
|
/// Requests kept.
|
|
pub const RETENTION: usize = 100;
|
|
/// Bytes kept of each captured text (the request, the output, the reasoning, the response).
|
|
pub const CAP: usize = 262_144;
|
|
/// Body limit of the generation routes.
|
|
const BODY_LIMIT: usize = 16 * 1024 * 1024;
|
|
/// A response collected whole (not an event stream) is read up to this size for capture.
|
|
const RESPONSE_LIMIT: usize = 64 * 1024 * 1024;
|
|
/// A single event line longer than this is dropped from the capture (the stream itself is untouched).
|
|
const LINE_LIMIT: usize = 1 << 20;
|
|
|
|
#[derive(Debug)]
|
|
struct Record {
|
|
id: String,
|
|
path: String,
|
|
model: Value,
|
|
started_at: f64,
|
|
state: &'static str,
|
|
outcome: Option<&'static str>,
|
|
stream: bool,
|
|
response_format: Value,
|
|
input: String,
|
|
input_truncated: bool,
|
|
output: String,
|
|
output_truncated: bool,
|
|
reasoning: String,
|
|
reasoning_truncated: bool,
|
|
first_token_s: Option<f64>,
|
|
usage: Option<Value>,
|
|
error: Option<Value>,
|
|
http_status: Option<u16>,
|
|
response: Option<String>,
|
|
response_truncated: bool,
|
|
wallclock_s: Option<f64>,
|
|
finished_at: Option<f64>,
|
|
clock: Instant,
|
|
}
|
|
|
|
type Shared = Arc<Mutex<Record>>;
|
|
|
|
/// The monitor: disabled (nothing is kept) or a bounded list of request records.
|
|
#[derive(Default)]
|
|
pub struct RequestMonitor {
|
|
enabled: bool,
|
|
records: Mutex<VecDeque<Shared>>,
|
|
}
|
|
|
|
/// An event stream passed through unchanged while its events are read into the record.
|
|
struct Tap {
|
|
inner: axum::body::BodyDataStream,
|
|
record: Shared,
|
|
pending: Vec<u8>,
|
|
done: bool,
|
|
}
|
|
|
|
#[cfg(test)]
|
|
mod tests;
|
|
|
|
mod now_unix;
|
|
#[allow(unused_imports)]
|
|
pub use now_unix::*;
|
|
mod record;
|
|
#[allow(unused_imports)]
|
|
pub use record::*;
|
|
mod capture;
|
|
#[allow(unused_imports)]
|
|
pub use capture::*;
|