hf2q 0.1.29

Pure Rust CLI for converting HuggingFace models to hardware-optimized formats and serving them over an OpenAI-compatible API on Apple Silicon
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
use super::apply_gcd_policy;
use crate::serve::api::grammar::{self, request::compile_request_output_constraint};
use crate::serve::api::schema::{ChatCompletionRequest, ToolChoiceValue};
use crate::serve::api::state::{AppState, ServerConfig};
use axum::body::{to_bytes, Body};
use axum::http::{header, Request, StatusCode};
use serde_json::{json, Value};
use std::sync::atomic::Ordering;
use tower::ServiceExt;

fn schema_config(locked: bool) -> ServerConfig {
    let schema = json!({
        "type": "object",
        "properties": {"decision": {"type": "string", "enum": ["allow"]}},
        "required": ["decision"],
        "additionalProperties": false
    });
    ServerConfig {
        gcd_schema_grammar: Some(grammar::json_schema::schema_to_gbnf(&schema).unwrap()),
        gcd_schema_locked: locked,
        ..ServerConfig::default()
    }
}

fn tool() -> Value {
    json!({
        "type": "function",
        "function": {
            "name": "lookup",
            "parameters": {"type": "object", "properties": {}}
        }
    })
}

fn request_value(extra: Value, stream: bool) -> Value {
    let mut value = json!({
        "model": "locked-schema-test",
        "messages": [{"role": "user", "content": "Return the decision."}],
        "stream": stream,
        "hf2q_enable_thinking": true
    });
    value
        .as_object_mut()
        .unwrap()
        .extend(extra.as_object().unwrap().clone());
    value
}

fn request(extra: Value) -> ChatCompletionRequest {
    serde_json::from_value(request_value(extra, false)).unwrap()
}

fn explicit_constraints() -> Vec<(&'static str, Value)> {
    vec![
        ("grammar", json!("root ::= \"caller\"")),
        ("response_format", json!({"type": "json_object"})),
        ("json_schema", json!({"type": "object"})),
        ("structured_outputs", json!({"choice": ["caller"]})),
    ]
}

fn allowed_requests() -> Vec<Value> {
    vec![
        json!({}),
        json!({"tool_choice": "none"}),
        json!({"tool_choice": "none", "tools": [tool()]}),
    ]
}

fn rejected_tool_requests() -> Vec<(&'static str, Value)> {
    vec![
        ("tools", json!({"tools": [tool()]})),
        ("tools", json!({"tools": [tool()], "tool_choice": "auto"})),
        (
            "tool_choice",
            json!({"tools": [tool()], "tool_choice": "required"}),
        ),
        (
            "tool_choice",
            json!({
                "tools": [tool()],
                "tool_choice": {"type": "function", "function": {"name": "lookup"}}
            }),
        ),
    ]
}

fn invalid_tool_requests() -> Vec<(&'static str, Value)> {
    vec![
        ("tool_choice", json!({"tool_choice": "auto"})),
        ("tools", json!({"tool_choice": "auto", "tools": []})),
        ("tools", json!({"tool_choice": "none", "tools": []})),
        ("tools", json!({"tools": []})),
        ("tool_choice", json!({"tool_choice": "required"})),
        ("tool_choice", json!({"tool_choice": "sometimes"})),
        ("tool_choice", json!({"tool_choice": {"type": "function"}})),
    ]
}

fn grammar_modifiers() -> Vec<(&'static str, Value)> {
    vec![
        ("grammar_lazy", json!(true)),
        ("grammar_lazy", json!(false)),
        ("preserved_tokens", json!([])),
        ("grammar_triggers", json!([])),
    ]
}

#[test]
fn locked_policy_admits_text_and_keeps_the_schema_through_request_compilation() {
    let config = schema_config(true);
    for extra in allowed_requests() {
        let mut req = request(extra.clone());
        // Exercise the same composition as the HTTP handler, not separately
        // ordered gate/injection helpers (which missed the original defect).
        apply_gcd_policy(&config, &mut req).unwrap();
        assert_eq!(req.grammar, config.gcd_schema_grammar, "{extra}");
        assert_eq!(req.hf2q_enable_thinking, Some(false), "{extra}");
        let choice = grammar::request::validate_tool_request(&req).unwrap();
        let compiled = compile_request_output_constraint(&req, &choice)
            .unwrap()
            .expect("every admitted locked request must retain its response grammar");
        let root = compiled.rule_id("root").unwrap();
        for (bytes, expected) in [
            (br#"{"decision":"allow"}"#.as_slice(), true),
            (br#"{"decision":"deny"}"#.as_slice(), false),
            (br#"{"name":"lookup","arguments":{}}"#.as_slice(), false),
        ] {
            let mut runtime = grammar::GrammarRuntime::new(compiled.clone(), root).unwrap();
            let accepted = runtime.accept_bytes(bytes) && runtime.is_terminally_accepted();
            assert_eq!(accepted, expected, "request {extra}, output {bytes:?}");
        }
    }
}

#[test]
fn locked_policy_rejects_caller_constraints_and_activation_modifiers_before_mutation() {
    let config = schema_config(true);
    for (param, value) in explicit_constraints()
        .into_iter()
        .chain(grammar_modifiers())
    {
        let mut req = request(json!({param: value}));
        let original_grammar = req.grammar.clone();
        let error = apply_gcd_policy(&config, &mut req).unwrap_err();
        assert_eq!(error.status, StatusCode::BAD_REQUEST);
        assert_eq!(error.error.param.as_deref(), Some(param));
        assert!(error.error.message.contains("--gcd-schema-locked"));
        assert_eq!(
            req.grammar, original_grammar,
            "rejection must not inject a grammar"
        );
        assert_eq!(req.hf2q_enable_thinking, Some(true));
    }
}

#[test]
fn locked_policy_rejects_tool_precedence_and_malformed_choices() {
    let config = schema_config(true);
    for (param, extra) in rejected_tool_requests()
        .into_iter()
        .chain(invalid_tool_requests())
    {
        let mut req = request(extra.clone());
        let validation_error = grammar::request::validate_tool_request(&req).err();
        let error = apply_gcd_policy(&config, &mut req).unwrap_err();
        assert_eq!(error.status, StatusCode::BAD_REQUEST, "{extra}");
        assert_eq!(error.error.param.as_deref(), Some(param), "{extra}");
        if let Some(expected) = validation_error {
            assert_eq!(error.error.message, expected.message, "{extra}");
        }
        assert!(req.grammar.is_none(), "{extra}");
        assert_eq!(req.hf2q_enable_thinking, Some(true), "{extra}");
    }
}

#[test]
fn unlocked_defaults_preserve_explicit_constraints_and_native_tool_precedence() {
    for config in [
        schema_config(false),
        ServerConfig {
            gcd: true,
            ..ServerConfig::default()
        },
    ] {
        for (param, value) in explicit_constraints() {
            let mut req = request(json!({param: value}));
            let original = req.grammar.clone();
            apply_gcd_policy(&config, &mut req).unwrap();
            assert_eq!(
                req.grammar, original,
                "explicit {param} must suppress injection"
            );
            assert_eq!(req.hf2q_enable_thinking, Some(true));
        }
    }
    for (_, extra) in rejected_tool_requests() {
        let mut req = request(extra);
        apply_gcd_policy(&schema_config(false), &mut req).unwrap();
        let choice = ToolChoiceValue::try_parse(req.tool_choice.as_ref()).unwrap();
        let grammar = compile_request_output_constraint(&req, &choice).unwrap();
        assert_eq!(
            grammar.is_none(),
            matches!(
                choice,
                ToolChoiceValue::Required | ToolChoiceValue::Function(_)
            )
        );
    }
}

#[test]
fn defaults_remain_orthogonal_to_steering_and_locked_configuration_fails_closed() {
    let mut req = request(json!({}));
    apply_gcd_policy(
        &ServerConfig {
            gcd: true,
            ..ServerConfig::default()
        },
        &mut req,
    )
    .unwrap();
    assert_eq!(
        req.grammar.as_deref(),
        Some(include_str!("grammar/gcd_w1.gbnf"))
    );
    assert_eq!(req.hf2q_enable_thinking, Some(false));

    let mut req = request(json!({}));
    apply_gcd_policy(&ServerConfig::default(), &mut req).unwrap();
    assert!(req.grammar.is_none());
    assert_eq!(req.hf2q_enable_thinking, Some(true));

    for config in [
        ServerConfig {
            gcd_schema_locked: true,
            ..ServerConfig::default()
        },
        ServerConfig {
            gcd: true,
            ..schema_config(true)
        },
    ] {
        assert_eq!(
            apply_gcd_policy(&config, &mut req).unwrap_err().status,
            StatusCode::INTERNAL_SERVER_ERROR
        );
        assert!(req.grammar.is_none());
    }
}

/// Drive the actual router, JSON extraction, and production handler. Readiness
/// stops admitted requests before model resolution, so these tests cannot load
/// weights or open SSE. Separate real-model gates cover successful generation.
async fn routed_request(config: ServerConfig, extra: Value, stream: bool) -> (StatusCode, Value) {
    let state = AppState::new(config);
    state.mark_not_ready();
    let app = crate::serve::api::build_router(state.clone());
    let response = app
        .oneshot(
            Request::builder()
                .method("POST")
                .uri("/v1/chat/completions")
                .header(header::CONTENT_TYPE, "application/json")
                .body(Body::from(request_value(extra, stream).to_string()))
                .unwrap(),
        )
        .await
        .unwrap();
    let status = response.status();
    assert!(response.headers()[header::CONTENT_TYPE]
        .to_str()
        .unwrap()
        .starts_with("application/json"));
    assert_eq!(
        state
            .metrics
            .chat_completions_started
            .load(Ordering::Relaxed),
        0
    );
    assert_eq!(state.metrics.requests_total.load(Ordering::Relaxed), 1);
    assert_eq!(
        state
            .metrics
            .requests_rejected_total
            .load(Ordering::Relaxed),
        1
    );
    let bytes = to_bytes(response.into_body(), 1 << 20).await.unwrap();
    (status, serde_json::from_slice(&bytes).unwrap())
}

#[tokio::test]
async fn router_admits_locked_text_requests_to_readiness_for_unary_and_sse() {
    for stream in [false, true] {
        for extra in allowed_requests() {
            let (status, body) = routed_request(schema_config(true), extra.clone(), stream).await;
            assert_eq!(status, StatusCode::SERVICE_UNAVAILABLE, "{extra}: {body}");
            assert_eq!(body["error"]["code"], "not_ready", "{body}");
        }
    }
}

#[tokio::test]
async fn router_rejects_locked_constraint_and_lazy_overrides_before_unary_or_sse() {
    for stream in [false, true] {
        for (param, value) in explicit_constraints()
            .into_iter()
            .chain(grammar_modifiers())
        {
            // A secondary unknown field must not hide the policy rejection.
            let extra = json!({param: value, "beam_search": true});
            let (status, body) = routed_request(schema_config(true), extra, stream).await;
            assert_eq!(status, StatusCode::BAD_REQUEST, "{body}");
            assert_eq!(body["error"]["param"], param, "{body}");
            assert_eq!(body["error"]["type"], "invalid_request_error");
            assert!(body["error"]["message"]
                .as_str()
                .unwrap()
                .contains("--gcd-schema-locked"));
        }
    }
}

#[tokio::test]
async fn router_rejects_locked_tool_requests_before_unary_or_sse() {
    for stream in [false, true] {
        for (param, extra) in rejected_tool_requests()
            .into_iter()
            .chain(invalid_tool_requests())
        {
            let validation_error =
                grammar::request::validate_tool_request(&request(extra.clone())).err();
            let (status, body) = routed_request(schema_config(true), extra, stream).await;
            assert_eq!(status, StatusCode::BAD_REQUEST, "{body}");
            assert_eq!(body["error"]["param"], param, "{body}");
            if let Some(expected) = validation_error {
                assert_eq!(body["error"]["message"], expected.message, "{body}");
            } else {
                assert!(body["error"]["message"]
                    .as_str()
                    .unwrap()
                    .contains("--gcd-schema-locked"));
            }
        }
    }
}

#[tokio::test]
async fn router_keeps_unlocked_request_overrides_for_unary_and_sse() {
    for stream in [false, true] {
        let explicit = explicit_constraints()
            .into_iter()
            .map(|(param, value)| json!({param: value}));
        let tools = rejected_tool_requests().into_iter().map(|(_, extra)| extra);
        for extra in explicit.chain(tools) {
            let (status, body) = routed_request(schema_config(false), extra, stream).await;
            assert_eq!(status, StatusCode::SERVICE_UNAVAILABLE, "{body}");
            assert_eq!(body["error"]["code"], "not_ready", "{body}");
        }
    }
}

#[tokio::test]
async fn router_tool_strict_nonstrict_hints_reach_readiness_without_enabling_gcd() {
    for stream in [false, true] {
        for strict in [None, Some(json!(false)), Some(Value::Null)] {
            let mut definition = tool();
            if let Some(value) = strict {
                definition["function"]["strict"] = value;
            }
            let extra = json!({"tools": [definition]});
            for config in [
                ServerConfig::default(),
                ServerConfig {
                    gcd: true,
                    ..ServerConfig::default()
                },
                schema_config(false),
            ] {
                let (status, body) = routed_request(config, extra.clone(), stream).await;
                assert_eq!(status, StatusCode::SERVICE_UNAVAILABLE, "{body}");
                assert_eq!(body["error"]["code"], "not_ready", "{body}");
            }
            // Non-strict hints cannot bypass the existing locked tool policy.
            let (status, body) = routed_request(schema_config(true), extra.clone(), stream).await;
            assert_eq!(status, StatusCode::BAD_REQUEST, "{body}");
            assert_eq!(body["error"]["param"], "tools", "{body}");
            let mut allowed = extra;
            allowed["tool_choice"] = json!("none");
            let (status, body) = routed_request(schema_config(true), allowed, stream).await;
            assert_eq!(status, StatusCode::SERVICE_UNAVAILABLE, "{body}");
            assert_eq!(body["error"]["code"], "not_ready", "{body}");
        }
    }
}

#[tokio::test]
async fn router_tool_strict_true_and_wrong_types_reject_before_readiness_or_sse() {
    for stream in [false, true] {
        for strict in [json!(true), json!("false"), json!(0), json!({}), json!([])] {
            let mut definition = tool();
            definition["function"]["strict"] = strict.clone();
            for config in [ServerConfig::default(), schema_config(true)] {
                let (status, body) =
                    routed_request(config, json!({"tools": [definition.clone()]}), stream).await;
                assert_eq!(status, StatusCode::BAD_REQUEST, "{body}");
                assert_eq!(body["error"]["type"], "invalid_request_error", "{body}");
                assert_eq!(body["error"]["param"], "body", "{body}");
                if strict == json!(true) {
                    assert!(body["error"]["message"]
                        .as_str()
                        .unwrap()
                        .contains("unsupported strict tool mode"));
                }
            }
        }
    }
}