Skip to content

armgit / sie

Public Imported from GitHub
0

Open-source inference server and production cluster for all the models your agent needs.

feat(gateway): carry the requested model on queued work (#504)

Krisztian Gajdarc98bfbb038dc
diff --git a/packages/sie_gateway/docs/architecture-guide.md b/packages/sie_gateway/docs/architecture-guide.md
index 1dca76708..dfcec2780 100644
--- a/packages/sie_gateway/docs/architecture-guide.md
+++ b/packages/sie_gateway/docs/architecture-guide.md
@@ -421,7 +421,7 @@ The `ConfigNotification` JSON payload carries the following fields, verified aga
The hot path is always **msgpack** (`rmp_serde` on the gateway, `msgpack-python` / `msgpack-numpy` on workers). Specifically:
-- `WorkItem` (gateway → worker on JetStream): msgpack.
+- `WorkItem` (gateway → worker on JetStream): msgpack. `model_id` is the route the work runs on. When routing dispatched the request to another route of the requested model (a profile variant), `display_model` carries the model id the caller asked for, so logs and accounting downstream can report it; it is omitted otherwise. `WorkDispatcher::publish_work` takes the same `display_model`.
- `WorkResult` (worker → gateway on the inbox subject): msgpack. Numpy arrays use `msgpack-numpy`'s extension-free encoding (maps with a `nd: true` sentinel + `type`, `shape`, `data`). When the gateway advertises `WorkItem.accepts_result_chunks: true`, a result that does not fit one NATS message may instead arrive as named-msgpack `result_chunk_v1` envelopes. This boolean negotiates v1 only; a future envelope version requires a new capability rather than reinterpreting the existing field. The gateway reassembles v1 transfers within the pending request lifetime, with a SHA-256 digest and fixed item/chunk/request/process memory limits, then decodes the reconstructed bytes as the same `WorkResult`. The gateway transcodes to native JSON arrays only when the client's `Accept` header asks for JSON.
JSON (`serde_json`) is used where payloads are low-frequency or human-oriented:
@@ -820,6 +820,7 @@ WorkItem {
operation: "encode",
model_id: "BAAI/bge-m3",
profile_id: "default",
+ display_model: "…", // the model the caller asked for; only when it differs from model_id
pool_name: "default",
admission_pool: "default",
machine_profile: "default",
diff --git a/packages/sie_gateway/src/handlers/proxy.rs b/packages/sie_gateway/src/handlers/proxy.rs
index b4b9c8049..f0e03519c 100644
--- a/packages/sie_gateway/src/handlers/proxy.rs
+++ b/packages/sie_gateway/src/handlers/proxy.rs
@@ -2928,6 +2928,7 @@ async fn queue_mode_proxy(
admission_pool,
endpoint,
model,
+ display_model,
engine,
bundle_config_hash,
items,
@@ -13714,6 +13715,7 @@ mod tests {
_admission_pool: &str,
_endpoint: &str,
_model: &str,
+ _display_model: &str,
_engine: &str,
_bundle_config_hash: &str,
_items: Vec<rmpv::Value>,
@@ -14972,6 +14974,7 @@ mod tests {
_admission_pool: &str,
_endpoint: &str,
_model: &str,
+ _display_model: &str,
_engine: &str,
_bundle_config_hash: &str,
_items: Vec<rmpv::Value>,
diff --git a/packages/sie_gateway/src/handlers/sse.rs b/packages/sie_gateway/src/handlers/sse.rs
index e3b7aa38c..c49f4150d 100644
--- a/packages/sie_gateway/src/handlers/sse.rs
+++ b/packages/sie_gateway/src/handlers/sse.rs
@@ -1698,6 +1698,7 @@ mod tests {
_admission_pool: &str,
_endpoint: &str,
_model: &str,
+ _display_model: &str,
_engine: &str,
_bundle_config_hash: &str,
_items: Vec<rmpv::Value>,
diff --git a/packages/sie_gateway/src/queue/dispatch.rs b/packages/sie_gateway/src/queue/dispatch.rs
index 153f70704..ea914138d 100644
--- a/packages/sie_gateway/src/queue/dispatch.rs
+++ b/packages/sie_gateway/src/queue/dispatch.rs
@@ -175,12 +175,16 @@ impl From<String> for DispatchError {
#[async_trait]
pub trait WorkDispatcher: Send + Sync {
#[allow(clippy::too_many_arguments)]
+ /// `model` is the route the work runs on. `display_model` is the model id
+ /// the caller asked for, which logs and accounting downstream report;
+ /// equal to `model` unless routing chose a profile variant of it.
async fn publish_work(
self: Arc<Self>,
target: PublishTarget,
admission_pool: &str,
endpoint: &str,
model: &str,
+ display_model: &str,
engine: &str,
bundle_config_hash: &str,
items: Vec<rmpv::Value>,
@@ -347,6 +351,7 @@ impl WorkDispatcher for WorkPublisher {
admission_pool: &str,
endpoint: &str,
model: &str,
+ display_model: &str,
engine: &str,
bundle_config_hash: &str,
items: Vec<rmpv::Value>,
@@ -365,6 +370,7 @@ impl WorkDispatcher for WorkPublisher {
admission_pool,
endpoint,
model,
+ display_model,
engine,
bundle_config_hash,
items,
@@ -582,6 +588,7 @@ mod performance_tests {
_admission_pool: &str,
_endpoint: &str,
_model: &str,
+ _display_model: &str,
_engine: &str,
_bundle_config_hash: &str,
_items: Vec<rmpv::Value>,
@@ -718,6 +725,7 @@ mod performance_tests {
black_box("default"),
black_box("encode"),
black_box("catalog/model"),
+ black_box("catalog/model"),
black_box("benchmark"),
black_box(""),
Vec::new(),
@@ -757,6 +765,7 @@ mod performance_tests {
black_box("default"),
black_box("encode"),
black_box("catalog/model"),
+ black_box("catalog/model"),
black_box("benchmark"),
black_box(""),
Vec::new(),
diff --git a/packages/sie_gateway/src/queue/publisher.rs b/packages/sie_gateway/src/queue/publisher.rs
index 99d42a9d0..0f1b0f0c7 100644
--- a/packages/sie_gateway/src/queue/publisher.rs
+++ b/packages/sie_gateway/src/queue/publisher.rs
@@ -524,6 +524,10 @@ struct WorkItemRef<'a> {
pub operation: &'a str,
pub model_id: &'a str,
pub profile_id: &'a str,
+ /// The model id the caller asked for, when routing dispatched the work to
+ /// another route of it (a profile variant). Absent otherwise.
+ #[serde(skip_serializing_if = "Option::is_none")]
+ pub display_model: Option<&'a str>,
pub engine: &'a str,
pub pool_name: &'a str,
#[serde(skip_serializing_if = "str::is_empty")]
@@ -569,6 +573,12 @@ struct WorkItemRef<'a> {
pub tracestate: Option<&'a str>,
}
+/// The requested model id a work item carries: present only when it differs
+/// from the route the work runs on.
+fn requested_model_on_wire<'a>(display_model: &'a str, model: &str) -> Option<&'a str> {
+ (!display_model.is_empty() && display_model != model).then_some(display_model)
+}
+
/// Per-request context that every [`WorkItemRef`] in a batch borrows
/// from. Grouping these lets us build the per-item view with a single
/// struct literal and keeps `publish_single` / `publish_score` from
@@ -577,6 +587,7 @@ struct WorkItemShared<'a> {
request_id: &'a str,
endpoint: &'a str,
model: &'a str,
+ display_model: Option<&'a str>,
pool: &'a str,
admission_pool: &'a str,
gpu: &'a str,
@@ -2915,6 +2926,7 @@ impl WorkPublisher {
admission_pool: &str,
endpoint: &str,
model: &str,
+ display_model: &str,
engine: &str,
bundle_config_hash: &str,
items: Vec<rmpv::Value>,
@@ -2939,6 +2951,7 @@ impl WorkPublisher {
admission_pool,
endpoint,
model,
+ display_model,
engine,
bundle_config_hash,
items,
@@ -2966,6 +2979,7 @@ impl WorkPublisher {
admission_pool: &str,
endpoint: &str,
model: &str,
+ display_model: &str,
engine: &str,
bundle_config_hash: &str,
items: Vec<rmpv::Value>,
@@ -3072,6 +3086,7 @@ impl WorkPublisher {
request_id: &request_id,
endpoint,
model,
+ display_model: requested_model_on_wire(display_model, model),
pool: &pool,
admission_pool,
gpu: &gpu,
@@ -3372,6 +3387,7 @@ impl WorkPublisher {
operation: shared.endpoint,
model_id: shared.model,
profile_id: "default",
+ display_model: shared.display_model,
engine: shared.engine,
pool_name: shared.pool,
admission_pool: shared.admission_pool,
@@ -3584,6 +3600,7 @@ impl WorkPublisher {
request_id: &request_id,
endpoint: "generate",
model: &model,
+ display_model: requested_model_on_wire(display_model, &model),
pool: &pool,
admission_pool,
gpu: &machine_profile,
@@ -3730,6 +3747,7 @@ impl WorkPublisher {
request_id: &request_id,
endpoint: "generate",
model: &model,
+ display_model: requested_model_on_wire(display_model, &model),
pool: &pool,
admission_pool,
gpu: &machine_profile,
@@ -3997,6 +4015,7 @@ impl WorkPublisher {
operation: shared.endpoint,
model_id: shared.model,
profile_id: "default",
+ display_model: shared.display_model,
engine: shared.engine,
pool_name: shared.pool,
admission_pool: shared.admission_pool,
@@ -4292,6 +4311,7 @@ impl WorkPublisher {
operation: shared.endpoint,
model_id: shared.model,
profile_id: "default",
+ display_model: shared.display_model,
engine: shared.engine,
pool_name: shared.pool,
admission_pool: shared.admission_pool,
@@ -6120,6 +6140,8 @@ mod tests {
pub model_id: String,
#[serde(default)]
pub profile_id: String,
+ #[serde(default, skip_serializing_if = "Option::is_none")]
+ pub display_model: Option<String>,
#[serde(default)]
pub engine: String,
pub pool_name: String,
@@ -7478,6 +7500,7 @@ mod tests {
operation: "encode".to_string(),
model_id: "BAAI/bge-m3".to_string(),
profile_id: String::new(),
+ display_model: None,
engine: "pytorch".to_string(),
pool_name: "default".to_string(),
admission_pool: "default".to_string(),
@@ -7580,6 +7603,7 @@ mod tests {
operation: "encode".to_string(),
model_id: "BAAI/bge-m3".to_string(),
profile_id: "default".to_string(),
+ display_model: Some("org/requested".to_string()),
engine: "pytorch".to_string(),
pool_name: "default".to_string(),
admission_pool: "tenant".to_string(),
@@ -7625,6 +7649,7 @@ mod tests {
operation: &owned.operation,
model_id: &owned.model_id,
profile_id: &owned.profile_id,
+ display_model: owned.display_model.as_deref(),
engine: &owned.engine,
pool_name: &owned.pool_name,
admission_pool: &owned.admission_pool,
@@ -7805,6 +7830,7 @@ mod tests {
operation: "encode".to_string(),
model_id: "model".to_string(),
profile_id: String::new(),
+ display_model: None,
engine: String::new(),
pool_name: "default".to_string(),
admission_pool: String::new(),
@@ -8487,6 +8513,7 @@ mod tests {
operation: "generate".to_string(),
model_id: "m".to_string(),
profile_id: "default".to_string(),
+ display_model: None,
engine: "pytorch".to_string(),
pool_name: "p".to_string(),
admission_pool: String::new(),
@@ -8638,6 +8665,7 @@ mod tests {
operation: "generate".to_string(),
model_id: "m".to_string(),
profile_id: "default".to_string(),
+ display_model: None,
engine: "pytorch".to_string(),
pool_name: "p".to_string(),
admission_pool: String::new(),
@@ -8684,6 +8712,7 @@ mod tests {
operation: "generate".to_string(),
model_id: "m".to_string(),
profile_id: "default".to_string(),
+ display_model: None,
engine: "pytorch".to_string(),
pool_name: "p".to_string(),
admission_pool: String::new(),
@@ -8966,6 +8995,150 @@ mod tests {
);
}
+ /// NATS-gated: a work item carries `display_model` only when the caller
+ /// asked for a model other than the route the work runs on, on the batch
+ /// and on the generation publish path alike.
+ #[tokio::test]
+ async fn work_items_carry_the_requested_model_only_when_it_differs() {
+ use futures_util::StreamExt;
+
+ let Ok(url) = std::env::var("NATS_URL") else {
+ eprintln!("skipping: NATS_URL not set");
+ return;
+ };
+ let client =
+ match tokio::time::timeout(Duration::from_secs(2), async_nats::connect(&url)).await {
+ Ok(Ok(c)) => c,
+ _ => {
+ eprintln!("skipping: could not connect to NATS at {url}");
+ return;
+ }
+ };
+ let nanos = std::time::SystemTime::now()
+ .duration_since(std::time::UNIX_EPOCH)
+ .map(|d| d.as_nanos())
+ .unwrap_or(0);
+ let pool = format!("itdisplay{nanos}");
+ let publisher = Arc::new(WorkPublisher::new(
+ async_nats::jetstream::new(client.clone()),
+ "it-router".to_string(),
+ Arc::new(crate::queue::payload_store::DisabledPayloadStore),
+ Duration::from_secs(5),
+ 1024,
+ WorkStreamConfig {
+ max_age: Duration::from_secs(300),
+ storage: jetstream::stream::StorageType::Memory,
+ num_replicas: 1,
+ },
+ ));
+ let subjects = format!("sie.work.{pool}.>");
+ let stream = async_nats::jetstream::new(client.clone())
+ .get_or_create_stream(jetstream::stream::Config {
+ name: stream_name(&pool),
+ subjects: vec![subjects.clone()],
+ retention: jetstream::stream::RetentionPolicy::WorkQueue,
+ storage: jetstream::stream::StorageType::Memory,
+ max_age: Duration::from_secs(300),
+ max_messages: 100_000,
+ discard: jetstream::stream::DiscardPolicy::New,
+ ..Default::default()
+ })
+ .await
+ .expect("create work stream");
+ stream
+ .create_consumer(jetstream::consumer::pull::Config {
+ durable_name: Some("itworker".to_string()),
+ filter_subject: subjects.clone(),
+ ..Default::default()
+ })
+ .await
+ .expect("create work consumer");
+ let mut sub = client.subscribe(subjects).await.expect("subscribe");
+ client.flush().await.expect("flush");
+ let target = |model: &str| PublishTarget::Pool {
+ pool: pool.clone(),
+ machine_profile: "l4".to_string(),
+ bundle: "default".to_string(),
+ model: model.to_string(),
+ };
+ async fn next_work_item(sub: &mut async_nats::Subscriber) -> (String, Option<String>) {
+ let msg = tokio::time::timeout(Duration::from_secs(5), sub.next())
+ .await
+ .expect("timed out waiting for the published work item")
+ .expect("subscription closed before a message arrived");
+ let work: WorkItem = rmp_serde::from_slice(&msg.payload).expect("decode work item");
+ (work.model_id, work.display_model)
+ }
+ let mut published = Vec::new();
+
+ for (model, display_model) in [
+ ("acme/model", "acme/model"),
+ ("acme/model:remote", "acme/model"),
+ ] {
+ let items = vec![rmpv::Value::Map(vec![(
+ rmpv::Value::from("text"),
+ rmpv::Value::from("hello"),
+ )])];
+ let (_request_id, _rx, durability) = publisher
+ .publish_work(
+ target(model),
+ &pool,
+ "encode",
+ model,
+ display_model,
+ "pytorch",
+ "",
+ items,
+ &WorkParams::default(),
+ )
+ .await
+ .expect("publish_work");
+ durability.wait().await.expect("durable publish ACK");
+ published.push(next_work_item(&mut sub).await);
+ }
+ let generate = WorkParams {
+ generate: Some(GenerateParams {
+ input: GenerateInput::Prompt {
+ prompt: "hi".to_string(),
+ },
+ max_new_tokens: 4,
+ ..Default::default()
+ }),
+ ..WorkParams::default()
+ };
+ let (_request_id, _rx, _notify, durability) = publisher
+ .publish_generate_streaming(
+ target("acme/model:remote"),
+ "acme/model",
+ "pytorch",
+ "",
+ &generate,
+ &pool,
+ )
+ .await
+ .expect("publish_generate_streaming");
+ durability.wait().await.expect("durable publish ACK");
+ published.push(next_work_item(&mut sub).await);
+
+ assert_eq!(
+ published,
+ vec![
+ ("acme/model".to_string(), None),
+ (
+ "acme/model:remote".to_string(),
+ Some("acme/model".to_string())
+ ),
+ (
+ "acme/model:remote".to_string(),
+ Some("acme/model".to_string())
+ ),
+ ]
+ );
+ let _ = async_nats::jetstream::new(client.clone())
+ .delete_stream(stream_name(&pool))
+ .await;
+ }
+
/// Integration test (issue #1500): drive a real `WorkPublisher` encode
/// publish over a live NATS/JetStream broker, with the inbound trace
/// context scoped over the publish via `with_context` exactly as
@@ -9094,7 +9267,7 @@ mod tests {
// Mirror the handler: scope the inbound context over the publish.
let (_request_id, _rx, durability) = publisher
.publish_work(
- target, &pool, "encode", model, "pytorch", "", items, &params,
+ target, &pool, "encode", model, model, "pytorch", "", items, &params,
)
.with_context(cx)
.await
@@ -9225,6 +9398,7 @@ mod tests {
&pool,
"encode",
model,
+ model,
"pytorch",
"",
items,
@@ -9371,7 +9545,7 @@ mod tests {
)])];
let (request_id, _rx, durability) = publisher
.publish_work(
- target, &pool, "encode", model, "pytorch", "", items, &params,
+ target, &pool, "encode", model, model, "pytorch", "", items, &params,
)
.await
.expect("publish oversized work item");
diff --git a/packages/sie_sdk/src/sie_sdk/queue_types.py b/packages/sie_sdk/src/sie_sdk/queue_types.py
index b6de5bd8c..17880b32d 100644
--- a/packages/sie_sdk/src/sie_sdk/queue_types.py
+++ b/packages/sie_sdk/src/sie_sdk/queue_types.py
@@ -31,6 +31,8 @@ class WorkItem(_WorkItemRequired, total=False):
operation: Inference operation: ``"encode"`` | ``"score"`` | ``"extract"``.
model_id: Model identifier (e.g., ``"BAAI/bge-m3"``).
profile_id: Profile name (e.g., ``"default"``).
+ display_model: The model id the caller asked for, when the gateway dispatched the
+ work to another route of it, such as a profile variant. Absent otherwise.
pool_name: Target pool (e.g., ``"default"``).
machine_profile: Required GPU type (e.g., ``"l4"``). Workers validate this.
bundle_config_hash: Expected bundle config hash from the gateway's ModelRegistry.
@@ -60,6 +62,7 @@ class WorkItem(_WorkItemRequired, total=False):
"""
bundle_config_hash: str
+ display_model: str
# Item payload (inline or reference)
item: dict[str, Any] | None