Files
leleka/crates/worker-protocol/src/lib.rs
T

81 lines
2.8 KiB
Rust

//! The line protocol the gateway speaks to an inference worker (the format of Strata's `--serve` mode).
//!
//! Input lines: `GEN <max_new> [temperature=<f>] [top_p=<f>] [seed=<n>] <id>,<id>,...`, `STOP` (abandon the running
//! generation), `QUIT`. Output lines: `READY <max_context> stop` once, then per request any number of
//! `PP <read> <total>`, `REUSED <n>`, `T <id>`, and exactly one `DONE ...` or `ERR <message>`.
//!
//! Both ends live here so they cannot drift: [`lines`] writes what a worker prints, [`events`] reads it as the gateway
//! does, and [`command`] reads what the gateway sends a CPU worker. The gateway's richer `GEN` writer (every sampling
//! key of the Original, golden tested against the Python server) stays in `strata-control`'s `wire.rs`; this crate
//! holds the parts both sides share.
pub mod command;
pub mod events;
pub mod lines;
pub use events::{Done, Event, Finish, parse_line};
#[cfg(test)]
mod tests {
use super::*;
/// Every line a worker writes is read back by the gateway parser as the same event.
#[test]
fn worker_lines_round_trip_through_the_gateway_parser() {
assert_eq!(
parse_line(&lines::token_line(42)).unwrap(),
Some(Event::Token(42))
);
assert_eq!(
parse_line(&lines::progress_line(64, 4096)).unwrap(),
Some(Event::PromptProgress {
read: 64,
total: 4096
})
);
assert_eq!(parse_line(&lines::reused_line(200)).unwrap(), None);
assert_eq!(
parse_line(&lines::error_line(
"bad
thing"
))
.unwrap(),
Some(Event::Error("bad thing".into()))
);
for finish in [Finish::Stop, Finish::Length, Finish::Cancel] {
assert_done_line_round_trips(finish);
}
}
fn assert_done_line_round_trips(finish: Finish) {
let phases = lines::PhaseMs {
prompt_ms: 45.12,
decode_ms: 88.26,
};
let line = lines::done_line(12, 300, phases, finish, 200);
let Some(Event::Done(done)) = parse_line(&line).unwrap() else {
panic!("{line}")
};
assert_eq!(
(done.generated, done.prompt_tokens, done.finish, done.reused),
(12, 300, finish, Some(200))
);
assert_eq!((done.prompt_ms, done.decode_ms), (45.1, 88.3));
assert_eq!(
(
done.drafts_accepted,
done.drafts_offered,
done.expert_hits,
done.expert_lookups
),
(Some(0), Some(0), Some(0), Some(0))
);
}
/// The `READY` handshake is not an event: the process supervisor reads it once, `parse_line` never does.
#[test]
fn ready_is_not_a_request_event() {
assert!(parse_line(&lines::ready_line(16384)).is_err());
}
}