Skip to content

armgit / sie

Public Imported from GitHub
0

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

fix(sidecar): keep the caller's key order of output_schema and options through to the engine (#724)

svonava78fa1b485612
diff --git a/packages/sie_gateway/src/handlers/proxy.rs b/packages/sie_gateway/src/handlers/proxy.rs
index d5e9ee502..8d26e18d7 100644
--- a/packages/sie_gateway/src/handlers/proxy.rs
+++ b/packages/sie_gateway/src/handlers/proxy.rs
@@ -18834,6 +18834,69 @@ mod tests {
assert_reason_first_order(&queued_json_schema(&params));
}
+ /// An extract request's `output_schema` and `options`, keys deliberately
+ /// not alphabetical: an adapter that reads a mapping of options in order
+ /// answers differently when they arrive sorted.
+ const CALLER_ORDER_EXTRACT_PARAMS: &str = r#"{
+ "output_schema": {
+ "priority": "string",
+ "intent": {"choice": {"refund": "asks for money back", "cancel": "wants to stop", "billing": "asks about a charge"}},
+ "frustration": "number"
+ },
+ "options": {"threshold": 0.5, "label_criteria": {"refund": "r", "cancel": "c", "billing": "b"}, "multi_label": false}
+ }"#;
+
+ fn assert_caller_order_extract_params(params: &WorkParams) {
+ let queued = |value: &serde_json::Value| -> rmpv::Value {
+ let bytes = rmp_serde::to_vec_named(value).expect("encode queue field");
+ rmpv::decode::read_value(&mut bytes.as_slice()).expect("decode queue field")
+ };
+ let schema = queued(params.output_schema.as_ref().expect("output_schema"));
+ assert_eq!(msgpack_keys(&schema), ["priority", "intent", "frustration"]);
+ assert_eq!(
+ msgpack_keys(&msgpack_field(&msgpack_field(&schema, "intent"), "choice")),
+ ["refund", "cancel", "billing"]
+ );
+ let options = queued(params.options.as_ref().expect("options"));
+ assert_eq!(
+ msgpack_keys(&options),
+ ["threshold", "label_criteria", "multi_label"]
+ );
+ assert_eq!(
+ msgpack_keys(&msgpack_field(&options, "label_criteria")),
+ ["refund", "cancel", "billing"]
+ );
+ }
+
+ #[test]
+ fn extract_output_schema_and_options_reach_queue_in_caller_key_order() {
+ let json_body =
+ format!(r#"{{"items": [{{"text": "hi"}}], "params": {CALLER_ORDER_EXTRACT_PARAMS}}}"#);
+ let (_, params) =
+ parse_queue_request(json_body.as_bytes(), false, "extract").expect("json body");
+ assert_caller_order_extract_params(&params);
+
+ // Decode the params straight into rmpv so the msgpack body keeps the
+ // text's key order without going through serde_json::Value.
+ let params_msgpack: rmpv::Value =
+ serde_json::from_str(CALLER_ORDER_EXTRACT_PARAMS).expect("params as msgpack value");
+ let body = rmpv::Value::Map(vec![
+ (
+ rmpv::Value::from("items"),
+ rmpv::Value::Array(vec![rmpv::Value::Map(vec![(
+ rmpv::Value::from("text"),
+ rmpv::Value::from("hi"),
+ )])]),
+ ),
+ (rmpv::Value::from("params"), params_msgpack),
+ ]);
+ let mut msgpack_body = Vec::new();
+ rmpv::encode::write_value(&mut msgpack_body, &body).expect("encode msgpack body");
+ let (_, params) =
+ parse_queue_request(&msgpack_body, true, "extract").expect("msgpack body");
+ assert_caller_order_extract_params(&params);
+ }
+
#[tokio::test]
async fn installed_empty_generation_policy_fails_native_and_openai_closed() {
let pool_manager = Arc::new(PoolManager::new(vec!["l4".to_string()]));
diff --git a/packages/sie_server_sidecar/Cargo.toml b/packages/sie_server_sidecar/Cargo.toml
index 784da22ec..795f64536 100644
--- a/packages/sie_server_sidecar/Cargo.toml
+++ b/packages/sie_server_sidecar/Cargo.toml
@@ -28,7 +28,17 @@ serde = { version = "1", features = ["derive"] }
# serde_json's default "fast" parser can be 1 ULP off for some decimals
# (e.g. "14.394267984578837" parses 1 bit low), which breaks the
# shadow-trace ULP diff. Do not drop this feature.
-serde_json = { version = "1", features = ["float_roundtrip"] }
+#
+# `preserve_order` keeps JSON object keys in the order the caller sent them.
+# A work item's `options` and `output_schema` are decoded into
+# `serde_json::Value` and re-encoded for the engine, and an adapter that reads
+# a mapping in order (the options of a choice question, the properties of a
+# schema) answers differently when the keys arrive sorted. The gateway sets
+# the same feature; a workspace build already unified it into this crate,
+# while the shipped binary, built with `--package sie-server-sidecar`, sorted.
+# Nothing in this crate relies on sorted `Value` keys. Do not drop this
+# feature either.
+serde_json = { version = "1", features = ["float_roundtrip", "preserve_order"] }
serde_bytes = "0.11"
half = "2"
rmp-serde = "1"
diff --git a/packages/sie_server_sidecar/src/work_types.rs b/packages/sie_server_sidecar/src/work_types.rs
index 05b6a588a..1c96b5613 100644
--- a/packages/sie_server_sidecar/src/work_types.rs
+++ b/packages/sie_server_sidecar/src/work_types.rs
@@ -321,6 +321,110 @@ mod tests {
);
}
+ fn ordered_map(entries: &[(&str, WireValue)]) -> WireValue {
+ WireValue::Map(
+ entries
+ .iter()
+ .map(|(key, value)| (WireValue::from(*key), value.clone()))
+ .collect(),
+ )
+ }
+
+ fn map_keys(value: &WireValue) -> Vec<String> {
+ value
+ .as_map()
+ .expect("msgpack map")
+ .iter()
+ .map(|(key, _)| key.as_str().expect("string key").to_string())
+ .collect()
+ }
+
+ fn map_field<'a>(value: &'a WireValue, key: &str) -> &'a WireValue {
+ value
+ .as_map()
+ .expect("msgpack map")
+ .iter()
+ .find(|(candidate, _)| candidate.as_str() == Some(key))
+ .map(|(_, value)| value)
+ .unwrap_or_else(|| panic!("missing {key:?}"))
+ }
+
+ /// The engine must see `output_schema` and `options` with their keys in
+ /// the order the caller sent them: an adapter that reads a mapping of
+ /// options in order (a choice question's option to criterion mapping)
+ /// answers differently when the keys arrive sorted.
+ #[test]
+ fn work_item_keeps_caller_key_order_through_to_the_engine_frame() {
+ // Deliberately not alphabetical at any level.
+ let choices = ordered_map(&[
+ ("refund", WireValue::from("asks for money back")),
+ ("cancel", WireValue::from("wants to stop the plan")),
+ ("technical", WireValue::from("reports a fault")),
+ ("billing_question", WireValue::from("asks about a charge")),
+ ("other", WireValue::from("anything else")),
+ ]);
+ let output_schema = ordered_map(&[
+ ("priority", WireValue::from("string")),
+ ("intent", ordered_map(&[("choice", choices.clone())])),
+ ("frustration", WireValue::from("number")),
+ ]);
+ let options = ordered_map(&[
+ ("threshold", WireValue::from(0.5)),
+ ("label_criteria", choices),
+ ("multi_label", WireValue::from(false)),
+ ]);
+
+ // A gateway-shaped work item, with the two mappings in caller order.
+ let template = rmp_serde::to_vec_named(&sample_work_item()).unwrap();
+ let mut fields = match rmp_serde::from_slice::<WireValue>(&template).unwrap() {
+ WireValue::Map(fields) => fields,
+ other => panic!("work item must encode as a map, got {other}"),
+ };
+ fields.retain(|(key, _)| !matches!(key.as_str(), Some("output_schema" | "options")));
+ fields.push((WireValue::from("output_schema"), output_schema));
+ fields.push((WireValue::from("options"), options));
+ let mut wire = Vec::new();
+ rmpv::encode::write_value(&mut wire, &WireValue::Map(fields)).unwrap();
+
+ // Decode as the queue and local-ingest paths do, then encode the
+ // engine frame's extract item as the IPC client does.
+ let item: WorkItem = rmp_serde::from_slice(&wire).unwrap();
+ let frame = rmp_serde::to_vec_named(&crate::ipc_types::ExtractBatchItem {
+ work_item_id: item.work_item_id.clone(),
+ request_id: item.request_id.clone(),
+ item_index: item.item_index,
+ total_items: item.total_items,
+ timestamp: item.timestamp,
+ item: item.item.clone().unwrap(),
+ labels: item.labels.clone(),
+ output_schema: item.output_schema.clone(),
+ instruction: item.instruction.clone(),
+ options: item.options.clone(),
+ profile_id: None,
+ bundle_config_hash: None,
+ payload_fetch_ms: 0.0,
+ prepared_audio: None,
+ })
+ .unwrap();
+ let sent: WireValue = rmp_serde::from_slice(&frame).unwrap();
+
+ let schema = map_field(&sent, "output_schema");
+ assert_eq!(map_keys(schema), ["priority", "intent", "frustration"]);
+ assert_eq!(
+ map_keys(map_field(map_field(schema, "intent"), "choice")),
+ ["refund", "cancel", "technical", "billing_question", "other"]
+ );
+ let options = map_field(&sent, "options");
+ assert_eq!(
+ map_keys(options),
+ ["threshold", "label_criteria", "multi_label"]
+ );
+ assert_eq!(
+ map_keys(map_field(options, "label_criteria")),
+ ["refund", "cancel", "technical", "billing_question", "other"]
+ );
+ }
+
#[test]
fn work_item_forward_compatible_extra_field_ignored() {
// Producer adds a new field we don't know about — we must still decode.