qql-protocol 0.5.0

Qdrant REST protocol surface for QQL: closed result IR, strict OpenAPI response parsing, schema and telemetry
Documentation
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
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
//! Strict REST response parsing: one OpenAPI shape per operation.
//!
//! REST is the only transport that speaks JSON envelopes, so
//! `RestQdrant::execute_planned` is the canonical caller. Every function here
//! reads exactly the OpenAPI response shape of its operation; a missing or
//! mistyped field fails with `QQL-BACKEND-ENVELOPE`. There are no fallbacks,
//! no shape detection, and no raw passthrough.
//!
//! Server telemetry is the one lenient extraction
//! ([`ServerTelemetry::from_envelope_opt`]): absent or misshapen telemetry
//! degrades to `None` and never fails a successful response.
//!
//! [`ServerTelemetry::from_envelope_opt`]: crate::telemetry::ServerTelemetry::from_envelope_opt

use std::collections::HashMap;

use serde_json::Value;

use qql_core::error::QqlError;
use qql_plan::{
    PlanFacetValue, PlanGroupId, PlanPointId, PlanShardKey, PlanVectorStruct, PlannedOperation,
};

use crate::response::{
    BackendResponse, ExecData, FacetHit, GroupedSearchResult, SearchHit, score_from_wire,
};
use crate::schema::CollectionInfo;
use crate::telemetry::ServerTelemetry;

fn envelope_err(message: impl Into<String>) -> QqlError {
    QqlError::backend("QQL-BACKEND-ENVELOPE", message.into(), None)
}

/// Parse one REST envelope into typed data for `op`.
pub fn parse_planned(op: &PlannedOperation, envelope: Value) -> Result<BackendResponse, QqlError> {
    // Telemetry only (lenient); the data path below is strict.
    let telemetry = ServerTelemetry::from_envelope_opt(&envelope);
    let data = match op {
        PlannedOperation::Query { .. } | PlannedOperation::Scroll { .. } => {
            ExecData::Hits(parse_points(envelope)?)
        }
        PlannedOperation::GetPoints { .. } => ExecData::Hits(parse_bare_records(envelope)?),
        PlannedOperation::QueryGroups { .. } => ExecData::Groups(parse_groups(envelope)?),
        PlannedOperation::Count { .. } => ExecData::Count(parse_count(envelope)?),
        PlannedOperation::Facet { .. } => ExecData::Facet(parse_facet(envelope)?),
        PlannedOperation::ListCollections => {
            ExecData::Collections(parse_collection_names(envelope)?)
        }
        PlannedOperation::GetCollection { .. } => {
            ExecData::Collection(parse_collection_info(envelope)?)
        }
        PlannedOperation::ListShardKeys { .. } => ExecData::ShardKeys(parse_shard_keys(envelope)?),
        PlannedOperation::GetQuotas => ExecData::Quotas(parse_quotas(envelope)?),
        PlannedOperation::SetQuotas { request } => {
            // `PUT /quotas` answers a boolean status; the typed result is the
            // replacement config the caller sent.
            match envelope.get("result") {
                Some(Value::Bool(_)) => ExecData::Quotas(request.config.clone()),
                _ => {
                    return Err(envelope_err(
                        "set quotas response is missing the boolean result field",
                    ));
                }
            }
        }
        // Writes carry no typed response body: the executor owns upsert counts
        // and error classification already happened at the HTTP status level.
        PlannedOperation::Upsert { .. }
        | PlannedOperation::Delete { .. }
        | PlannedOperation::UpdatePayload { .. }
        | PlannedOperation::OverwritePayload { .. }
        | PlannedOperation::ClearPayload { .. }
        | PlannedOperation::DeletePayload { .. }
        | PlannedOperation::UpdateVectors { .. }
        | PlannedOperation::DeleteVectors { .. }
        | PlannedOperation::CreateCollection { .. }
        | PlannedOperation::UpdateCollection { .. }
        | PlannedOperation::DropCollection { .. }
        | PlannedOperation::CreateIndex { .. }
        | PlannedOperation::DropIndex { .. }
        | PlannedOperation::CreateShardKey { .. }
        | PlannedOperation::DropShardKey { .. } => ExecData::Mutation { affected: None },
        PlannedOperation::CrossRerank { .. } => {
            return Err(QqlError::execution(
                "QQL-REST-CLIENT-SIDE",
                "CROSS RERANK is client-side and has no REST response",
                None,
            ));
        }
        PlannedOperation::Batch { .. } => {
            return Err(QqlError::execution(
                "QQL-REST-BATCH",
                "BATCH is dispatched by the Executor through the batch RPCs, not as a single REST route",
                None,
            ));
        }
    };
    Ok(BackendResponse { data, telemetry })
}

/// Parse one `/points/query/batch` result array: each OpenAPI `QueryResponse`
/// item carries its points at the item's top level (`{"points": […]}`).
///
/// Takes the `result` array value itself (the transport envelope is validated
/// and unwrapped by the caller), consuming items without cloning.
pub fn parse_query_batch(result: Value) -> Result<Vec<BackendResponse>, QqlError> {
    let items = match result {
        Value::Array(items) => items,
        _ => {
            return Err(envelope_err(
                "query batch response is missing result as an array",
            ));
        }
    };
    items
        .into_iter()
        .map(|item| {
            // Telemetry reads borrowed fields; the points below move out.
            let telemetry = ServerTelemetry::from_envelope_opt(&item);
            let mut map = match item {
                Value::Object(map) => map,
                _ => {
                    return Err(envelope_err(
                        "query batch item is missing points as an array",
                    ));
                }
            };
            let points = match map.remove("points") {
                Some(Value::Array(points)) => points,
                _ => {
                    return Err(envelope_err(
                        "query batch item is missing points as an array",
                    ));
                }
            };
            let hits = points
                .into_iter()
                .map(parse_hit)
                .collect::<Result<Vec<_>, _>>()?;
            Ok(BackendResponse {
                data: ExecData::Hits(hits),
                // Telemetry only (lenient); the hits above are strict.
                telemetry,
            })
        })
        .collect()
}

/// Parse one `/points/batch` result array: each item must be an OpenAPI
/// `UpdateResult` (`status` ∈ `acknowledged | completed | wait_timeout`,
/// optional `operation_id`). Per-item results are status-only: the executor
/// derives upsert counts from the request.
///
/// `wait_timeout` means the server did not confirm the write inside the
/// batch's wait window, so it is surfaced as a per-item failure through the
/// existing batch error path (`QQL-BACKEND-BATCH`) — never reported as
/// success. Unknown or malformed items fail closed (`QQL-BACKEND-ENVELOPE`).
pub fn parse_update_batch(result: Value) -> Result<Vec<BackendResponse>, QqlError> {
    let items = match result {
        Value::Array(items) => items,
        _ => {
            return Err(envelope_err(
                "update batch response is missing result as an array",
            ));
        }
    };
    items.into_iter().map(parse_update_batch_item).collect()
}

/// Parse one OpenAPI `UpdateResult` into a status-only mutation response.
fn parse_update_batch_item(item: Value) -> Result<BackendResponse, QqlError> {
    let mut item = match item {
        Value::Object(map) => map,
        _ => return Err(envelope_err("update batch item is not an object")),
    };
    let status = match item.remove("status") {
        Some(Value::String(status)) => status,
        _ => return Err(envelope_err("update batch item is missing a string status")),
    };
    match status.as_str() {
        "acknowledged" | "completed" => Ok(BackendResponse {
            data: ExecData::Mutation { affected: None },
            telemetry: None,
        }),
        "wait_timeout" => Err(QqlError::backend(
            "QQL-BACKEND-BATCH",
            "update batch item was not confirmed before the server wait timeout (status: wait_timeout)",
            None,
        )),
        other => Err(envelope_err(format!(
            "update batch item status '{other}' is not a valid UpdateStatus (acknowledged|completed|wait_timeout)"
        ))),
    }
}

/// Parse `GET /collections` (`result.collections[*].name`).
pub fn parse_collection_names(envelope: Value) -> Result<Vec<String>, QqlError> {
    let missing = || envelope_err("list collections response is missing result.collections");
    let mut envelope = match envelope {
        Value::Object(map) => map,
        _ => return Err(missing()),
    };
    let mut result = match envelope.remove("result") {
        Some(Value::Object(map)) => map,
        _ => return Err(missing()),
    };
    let collections = match result.remove("collections") {
        Some(Value::Array(items)) => items,
        _ => return Err(missing()),
    };
    collections
        .into_iter()
        .map(|entry| match entry {
            Value::Object(mut map) => match map.remove("name") {
                Some(Value::String(name)) => Ok(name),
                _ => Err(envelope_err("collection entry is missing a string name")),
            },
            _ => Err(envelope_err("collection entry is missing a string name")),
        })
        .collect()
}

/// Parse `GET /collections/{name}` as an existence probe: any well-formed
/// collection-info envelope (a `result` object with a string `status`, the
/// same signal [`parse_collection_info`] reads) means the collection exists.
/// Fails closed (`QQL-BACKEND-ENVELOPE`) on a missing or mistyped shape
/// instead of sniffing for `status`/`exists` keys. The 404 → `Ok(false)` arm
/// stays with the caller, keyed on the transport error message.
pub fn parse_collection_exists(envelope: Value) -> Result<bool, QqlError> {
    let mut envelope = match envelope {
        Value::Object(map) => map,
        _ => {
            return Err(envelope_err(
                "get collection response is missing a result object with a string status",
            ));
        }
    };
    let result = match envelope.remove("result") {
        Some(Value::Object(result)) => result,
        _ => {
            return Err(envelope_err(
                "get collection response is missing a result object with a string status",
            ));
        }
    };
    match result.get("status").and_then(Value::as_str) {
        Some(_) => Ok(true),
        None => Err(envelope_err("collection info is missing a string status")),
    }
}

/// Parse `GET /collections/{name}` (`result` → [`CollectionInfo`]).
pub fn parse_collection_info(envelope: Value) -> Result<CollectionInfo, QqlError> {
    let result = match envelope {
        Value::Object(mut map) => match map.remove("result") {
            Some(Value::Object(result)) => result,
            _ => {
                return Err(envelope_err(
                    "get collection response is missing a result object",
                ));
            }
        },
        _ => {
            return Err(envelope_err(
                "get collection response is missing a result object",
            ));
        }
    };
    let status = result
        .get("status")
        .and_then(Value::as_str)
        .ok_or_else(|| envelope_err("collection info is missing a string status"))?
        .to_string();
    let segments_count = result
        .get("segments_count")
        .and_then(Value::as_u64)
        .ok_or_else(|| envelope_err("collection info is missing an unsigned segments_count"))?;
    // Re-wrap for the shared schema reader (borrows; no field clones).
    let result = Value::Object(result);
    Ok(CollectionInfo {
        status,
        // `points_count` is nullable in the OpenAPI schema; an unreported
        // count reads as zero.
        points_count: result
            .get("points_count")
            .and_then(Value::as_u64)
            .unwrap_or(0),
        // `indexed_vectors_count` is nullable in the OpenAPI schema; absent or
        // null both read as "not reported".
        indexed_vectors_count: result.get("indexed_vectors_count").and_then(Value::as_u64),
        segments_count,
        schema: crate::schema::schema_from_rest_result(&result),
    })
}

/// Parse a query / scroll response (`result.points` → scored hits).
fn parse_points(envelope: Value) -> Result<Vec<SearchHit>, QqlError> {
    let mut envelope = match envelope {
        Value::Object(map) => map,
        _ => {
            return Err(envelope_err(
                "query/scroll response is missing result.points",
            ));
        }
    };
    let mut result = match envelope.remove("result") {
        Some(Value::Object(map)) => map,
        _ => {
            return Err(envelope_err(
                "query/scroll response is missing result.points",
            ));
        }
    };
    match result.remove("points") {
        Some(Value::Array(points)) => points.into_iter().map(parse_hit).collect(),
        _ => Err(envelope_err(
            "query/scroll response is missing result.points",
        )),
    }
}

/// Parse a get-points response: `result` is a bare array of point records.
fn parse_bare_records(envelope: Value) -> Result<Vec<SearchHit>, QqlError> {
    let mut envelope = match envelope {
        Value::Object(map) => map,
        _ => {
            return Err(envelope_err(
                "get points response is missing result as an array",
            ));
        }
    };
    match envelope.remove("result") {
        Some(Value::Array(records)) => records.into_iter().map(parse_hit).collect(),
        _ => Err(envelope_err(
            "get points response is missing result as an array",
        )),
    }
}

/// Parse one OpenAPI `ScoredPoint` / `Record` into a [`SearchHit`]. `score`
/// defaults to `0.0` for unscored retrieve/scroll records, matching the typed
/// gRPC/edge paths. Takes the record by value: ids, payload maps, and vectors
/// move out instead of cloning per field.
fn parse_hit(value: Value) -> Result<SearchHit, QqlError> {
    let mut record = match value {
        Value::Object(map) => map,
        _ => return Err(envelope_err("point record is not an object")),
    };
    let id = parse_point_id(
        record
            .remove("id")
            .ok_or_else(|| envelope_err("point record is missing id"))?,
    )?;
    let score = match record.remove("score") {
        None | Some(Value::Null) => 0.0,
        Some(Value::Number(number)) => score_from_wire(
            number
                .as_f64()
                .ok_or_else(|| envelope_err("point score is not a finite number"))?,
        ),
        Some(other) => {
            return Err(envelope_err(format!(
                "point score must be a number, got {other}"
            )));
        }
    };
    let payload = match record.remove("payload") {
        None | Some(Value::Null) => None,
        Some(Value::Object(map)) => Some(map.into_iter().collect::<HashMap<_, _>>()),
        Some(other) => {
            return Err(envelope_err(format!(
                "point payload must be an object or null, got {other}"
            )));
        }
    };
    let vector = match record.remove("vector") {
        None | Some(Value::Null) => None,
        Some(vector) => Some(serde_json::from_value::<PlanVectorStruct>(vector).map_err(
            |error| {
                envelope_err(format!(
                    "point vector does not match VectorStructOutput: {error}"
                ))
            },
        )?),
    };
    Ok(SearchHit {
        id,
        score,
        payload,
        collection: None,
        vector,
    })
}

fn parse_point_id(value: Value) -> Result<PlanPointId, QqlError> {
    match value {
        Value::Number(number) => number
            .as_u64()
            .map(PlanPointId::Number)
            .ok_or_else(|| envelope_err(format!("point id {number} is not an unsigned integer"))),
        Value::String(id) => Ok(PlanPointId::String(id)),
        other => Err(envelope_err(format!(
            "point id must be a string or unsigned integer, got {other}"
        ))),
    }
}

/// Parse a grouped query response (`result.groups`).
fn parse_groups(envelope: Value) -> Result<Vec<GroupedSearchResult>, QqlError> {
    let mut envelope = match envelope {
        Value::Object(map) => map,
        _ => {
            return Err(envelope_err(
                "grouped query response is missing result.groups",
            ));
        }
    };
    let mut result = match envelope.remove("result") {
        Some(Value::Object(map)) => map,
        _ => {
            return Err(envelope_err(
                "grouped query response is missing result.groups",
            ));
        }
    };
    match result.remove("groups") {
        Some(Value::Array(groups)) => groups
            .into_iter()
            .map(|group| {
                let mut group = match group {
                    Value::Object(map) => map,
                    _ => return Err(envelope_err("group is missing id")),
                };
                let group_id = serde_json::from_value::<PlanGroupId>(
                    group
                        .remove("id")
                        .ok_or_else(|| envelope_err("group is missing id"))?,
                )
                .map_err(|error| {
                    envelope_err(format!("group id does not match GroupId: {error}"))
                })?;
                let hits = match group.remove("hits") {
                    Some(Value::Array(hits)) => hits,
                    _ => return Err(envelope_err("group is missing hits as an array")),
                };
                let hits = hits
                    .into_iter()
                    .map(parse_hit)
                    .collect::<Result<Vec<_>, _>>()?;
                Ok(GroupedSearchResult { group_id, hits })
            })
            .collect(),
        _ => Err(envelope_err(
            "grouped query response is missing result.groups",
        )),
    }
}

/// Parse a count response (`result.count`).
fn parse_count(envelope: Value) -> Result<u64, QqlError> {
    envelope
        .get("result")
        .and_then(|result| result.get("count"))
        .and_then(Value::as_u64)
        .ok_or_else(|| envelope_err("count response is missing an unsigned result.count"))
}

/// Parse a facet response (`result.hits[*]` with strict `{value, count}`).
fn parse_facet(envelope: Value) -> Result<Vec<FacetHit>, QqlError> {
    let mut envelope = match envelope {
        Value::Object(map) => map,
        _ => return Err(envelope_err("facet response is missing result.hits")),
    };
    let mut result = match envelope.remove("result") {
        Some(Value::Object(map)) => map,
        _ => return Err(envelope_err("facet response is missing result.hits")),
    };
    match result.remove("hits") {
        Some(Value::Array(hits)) => hits
            .into_iter()
            .map(|hit| {
                let mut hit = match hit {
                    Value::Object(map) => map,
                    _ => return Err(envelope_err("facet hit is missing value")),
                };
                let value = serde_json::from_value::<PlanFacetValue>(
                    hit.remove("value")
                        .ok_or_else(|| envelope_err("facet hit is missing value"))?,
                )
                .map_err(|error| {
                    envelope_err(format!("facet value does not match FacetValue: {error}"))
                })?;
                let count = match hit.remove("count") {
                    Some(Value::Number(count)) => count
                        .as_u64()
                        .ok_or_else(|| envelope_err("facet hit is missing an unsigned count"))?,
                    _ => {
                        return Err(envelope_err("facet hit is missing an unsigned count"));
                    }
                };
                Ok(FacetHit { value, count })
            })
            .collect(),
        _ => Err(envelope_err("facet response is missing result.hits")),
    }
}

/// Parse `GET /collections/{name}/shards` (`result.shard_keys`, nullable when
/// the collection does not use custom sharding).
///
/// Lenient shape (unchanged): a missing/null `result` or `shard_keys` reads as
/// no custom sharding; only a present-but-mistyped value fails.
fn parse_shard_keys(envelope: Value) -> Result<Vec<PlanShardKey>, QqlError> {
    let mut envelope = match envelope {
        Value::Object(map) => map,
        _ => return Ok(Vec::new()),
    };
    let mut result = match envelope.remove("result") {
        Some(Value::Object(map)) => map,
        _ => return Ok(Vec::new()),
    };
    match result.remove("shard_keys") {
        None | Some(Value::Null) => Ok(Vec::new()),
        Some(Value::Array(entries)) => entries
            .into_iter()
            .map(|entry| {
                let mut entry = match entry {
                    Value::Object(map) => map,
                    _ => {
                        return Err(envelope_err("shard key entry is missing key"));
                    }
                };
                let key = entry
                    .remove("key")
                    .ok_or_else(|| envelope_err("shard key entry is missing key"))?;
                match key {
                    Value::String(keyword) => Ok(PlanShardKey::Keyword(keyword)),
                    Value::Number(number) => {
                        number.as_u64().map(PlanShardKey::Number).ok_or_else(|| {
                            envelope_err(format!("shard key {number} is not an unsigned integer"))
                        })
                    }
                    other => Err(envelope_err(format!(
                        "shard key must be a string or unsigned integer, got {other}"
                    ))),
                }
            })
            .collect(),
        Some(other) => Err(envelope_err(format!(
            "shard_keys must be an array or null, got {other}"
        ))),
    }
}

/// Parse `GET /quotas` (`result.config` → [`QuotaConfig`](qql_plan::QuotaConfig)).
fn parse_quotas(envelope: Value) -> Result<qql_plan::QuotaConfig, QqlError> {
    let mut envelope = match envelope {
        Value::Object(map) => map,
        _ => return Err(envelope_err("get quotas response is missing result.config")),
    };
    let mut result = match envelope.remove("result") {
        Some(Value::Object(map)) => map,
        _ => return Err(envelope_err("get quotas response is missing result.config")),
    };
    match result.remove("config") {
        Some(config) => serde_json::from_value(config)
            .map_err(|error| envelope_err(format!("quota config is invalid: {error}"))),
        None => Err(envelope_err("get quotas response is missing result.config")),
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use serde_json::json;

    fn planned(sql: &str) -> PlannedOperation {
        qql_plan::plan::plan(&qql_core::parser::Parser::parse(sql).expect("parse")).expect("plan")
    }

    #[test]
    fn parses_scored_points_and_vectors_strictly() {
        let envelope = json!({
            "result": {"points": [{
                "id": 7,
                "score": 0.75,
                "payload": {"title": "a"},
                "vector": [0.5, 0.25],
            }]},
            "status": "ok",
            "time": 0.125,
        });
        let response =
            parse_planned(&planned("SCROLL FROM docs LIMIT 1"), envelope).expect("strict parse");
        let hits = response.data.hits().expect("hits");
        assert_eq!(hits.len(), 1);
        assert_eq!(hits[0].id, PlanPointId::Number(7));
        assert_eq!(hits[0].score, 0.75);
        assert_eq!(
            hits[0].vector,
            Some(PlanVectorStruct::Single(qql_plan::PlanVectorValue::Dense(
                vec![0.5, 0.25]
            )))
        );
        assert_eq!(response.telemetry.unwrap().time_s, Some(0.125));
    }

    #[test]
    fn parse_hit_rounds_score_once_to_shortest_f64() {
        // `0.95` arrives as a JSON decimal; ingestion stores the shortest
        // round-trip f64 exactly — no epsilon needed, and the serialized
        // report emits the same decimal.
        let response = parse_planned(
            &planned("SCROLL FROM docs LIMIT 1"),
            json!({"result": {"points": [{"id": 1, "score": 0.95}]}, "status": "ok"}),
        )
        .expect("strict parse");
        let hits = response.data.hits().expect("hits");
        assert_eq!(hits[0].score, 0.95f64);
        let value = serde_json::to_value(&hits[0]).expect("hit serializes");
        assert_eq!(value["score"], json!(0.95));
    }

    #[test]
    fn get_points_parses_bare_record_array() {
        let op = planned("QUERY POINTS (1) FROM docs");
        let response = parse_planned(
            &op,
            json!({"result": [{"id": 1, "payload": {"text": "a"}}], "status": "ok"}),
        )
        .expect("strict parse");
        let hits = response.data.hits().expect("hits");
        assert_eq!(hits[0].id, PlanPointId::Number(1));
        assert_eq!(hits[0].score, 0.0);
        assert!(response.telemetry.is_none());
    }

    #[test]
    fn missing_or_mistyped_shapes_fail_closed() {
        let scroll = planned("SCROLL FROM docs LIMIT 1");
        // Missing result.points.
        let err = parse_planned(&scroll, json!({"result": {}, "status": "ok"})).unwrap_err();
        assert_eq!(err.code, "QQL-BACKEND-ENVELOPE");
        // Missing count (no zero fallback).
        let err = parse_planned(
            &planned("COUNT FROM docs"),
            json!({"result": {}, "status": "ok"}),
        )
        .unwrap_err();
        assert_eq!(err.code, "QQL-BACKEND-ENVELOPE");
        // Fabricated point id.
        let err = parse_planned(
            &scroll,
            json!({"result": {"points": [{"score": 1.0}]}, "status": "ok"}),
        )
        .unwrap_err();
        assert_eq!(err.code, "QQL-BACKEND-ENVELOPE");
        // Unmodelled vector shape.
        let err = parse_planned(
            &scroll,
            json!({"result": {"points": [{"id": 1, "vector": {"text": "x"}}]}, "status": "ok"}),
        )
        .unwrap_err();
        assert_eq!(err.code, "QQL-BACKEND-ENVELOPE");
    }

    #[test]
    fn parses_count_facet_and_groups() {
        let count = parse_planned(
            &planned("COUNT FROM docs"),
            json!({"result": {"count": 7}, "status": "ok"}),
        )
        .unwrap();
        assert_eq!(count.data.count(), Some(7));

        let facet = parse_planned(
            &planned("FACET city FROM docs LIMIT 10"),
            json!({"result": {"hits": [
                {"value": "NYC", "count": 2},
                {"value": 7, "count": 1},
                {"value": true, "count": 3},
            ]}, "status": "ok"}),
        )
        .unwrap();
        assert_eq!(
            facet.data.facet().unwrap(),
            [
                FacetHit {
                    value: PlanFacetValue::Keyword("NYC".into()),
                    count: 2
                },
                FacetHit {
                    value: PlanFacetValue::Integer(7),
                    count: 1
                },
                FacetHit {
                    value: PlanFacetValue::Bool(true),
                    count: 3
                },
            ]
        );

        let groups = parse_planned(
            &planned("QUERY TEXT 'x' MODEL 'm' FROM docs USING dense GROUP BY category LIMIT 3"),
            json!({"result": {"groups": [{"id": -3, "hits": [{"id": 1}]}]}, "status": "ok"}),
        )
        .unwrap();
        let groups = groups.data.groups().unwrap();
        assert_eq!(groups[0].group_id, PlanGroupId::Signed(-3));
        assert_eq!(groups[0].hits.len(), 1);
    }

    #[test]
    fn parses_collections_collection_shard_keys_and_quotas() {
        let collections = parse_planned(
            &PlannedOperation::ListCollections,
            json!({"result": {"collections": [{"name": "alpha"}, {"name": "beta"}]}, "status": "ok"}),
        )
        .unwrap();
        assert_eq!(
            collections.data.collections().unwrap(),
            ["alpha".to_string(), "beta".to_string()]
        );

        let info = parse_planned(
            &planned("SHOW COLLECTION docs"),
            json!({
                "result": {
                    "status": "green",
                    "points_count": 12,
                    "segments_count": 2,
                    "config": {"params": {"vectors": {"size": 3, "distance": "Cosine"}}},
                    "payload_schema": {},
                },
                "status": "ok",
            }),
        )
        .unwrap();
        let info = info.data.collection().expect("collection info");
        assert_eq!(info.status, "green");
        assert_eq!(info.points_count, 12);
        assert_eq!(info.segments_count, 2);
        assert_eq!(info.schema.vectors.len(), 1);

        let keys = parse_planned(
            &planned("SHOW SHARD KEYS ON COLLECTION docs"),
            json!({"result": {"shard_keys": [{"key": "acme"}, {"key": 101}]}, "status": "ok"}),
        )
        .unwrap();
        assert_eq!(
            keys.data.shard_keys().unwrap(),
            [
                PlanShardKey::Keyword("acme".into()),
                PlanShardKey::Number(101)
            ]
        );

        // Null shard_keys (non-custom sharding) is an empty list.
        let none = parse_planned(
            &planned("SHOW SHARD KEYS ON COLLECTION docs"),
            json!({"result": {"shard_keys": null}, "status": "ok"}),
        )
        .unwrap();
        assert_eq!(none.data.shard_keys().unwrap(), []);

        let quotas = parse_planned(
            &PlannedOperation::GetQuotas,
            json!({
                "result": {"config": {"enabled": true, "max_resident_memory_percent": 80}},
                "status": "ok",
            }),
        )
        .unwrap();
        let config = quotas.data.quotas().expect("quota config");
        assert_eq!(config.enabled, Some(true));
        assert_eq!(config.max_resident_memory_percent, Some(80));
    }

    #[test]
    fn parses_collection_exists_strictly() {
        let info = json!({
            "result": {"status": "green", "segments_count": 2},
            "status": "ok",
        });
        assert!(parse_collection_exists(info).expect("exists"));

        // Missing result, missing status, and non-object envelopes fail
        // closed instead of reading as absent.
        for bad in [
            json!({"result": {}, "status": "ok"}),
            json!({"result": {"exists": true}, "status": "ok"}),
            json!({"status": "ok"}),
            json!("ok"),
        ] {
            let err = parse_collection_exists(bad).unwrap_err();
            assert_eq!(err.code, "QQL-BACKEND-ENVELOPE");
        }
    }

    #[test]
    fn parses_set_quotas_result_and_query_batch_items() {
        let op = planned("SET QUOTA (enabled = true)");
        let response = parse_planned(&op, json!({"result": true, "status": "ok"})).unwrap();
        assert_eq!(response.data.quotas().unwrap().enabled, Some(true));

        let err = parse_planned(&op, json!({"result": {"ok": true}, "status": "ok"})).unwrap_err();
        assert_eq!(err.code, "QQL-BACKEND-ENVELOPE");

        let items = parse_query_batch(json!([
            {"points": [{"id": 1, "score": 0.9}, {"id": 2, "score": 0.8}]},
            {"points": [{"id": 3, "score": 0.7}]},
        ]))
        .unwrap();
        assert_eq!(items.len(), 2);
        assert_eq!(items[0].data.hits().unwrap().len(), 2);
        assert_eq!(items[1].data.hits().unwrap()[0].id, PlanPointId::Number(3));

        // A query item without its `points` array is an envelope violation,
        // regardless of any extra status-like keys.
        let err = parse_query_batch(json!([{"status": "error", "error": "point 42 not found"}]))
            .unwrap_err();
        assert_eq!(err.code, "QQL-BACKEND-ENVELOPE");
    }

    #[test]
    fn parses_update_batch_statuses_strictly() {
        // The OpenAPI `UpdateResult` statuses: acknowledged/completed are
        // success, wait_timeout is a write that was not confirmed.
        let items = parse_update_batch(json!([
            {"status": "acknowledged", "operation_id": 1},
            {"status": "completed"},
        ]))
        .expect("valid statuses parse");
        assert_eq!(items.len(), 2);
        assert_eq!(items[0].data, ExecData::Mutation { affected: None });

        let err = parse_update_batch(json!([{"status": "wait_timeout"}]))
            .expect_err("wait_timeout is a per-item failure");
        assert_eq!(err.code, "QQL-BACKEND-BATCH");
        assert!(err.message.contains("wait_timeout"));

        // Unknown or malformed statuses fail closed instead of reading as
        // success.
        for bad in [
            json!([{"status": "error", "error": "boom"}]),
            json!([{"operation_id": 3}]),
            json!(["completed"]),
        ] {
            let err = parse_update_batch(bad).expect_err("malformed item must fail");
            assert_eq!(err.code, "QQL-BACKEND-ENVELOPE");
        }
    }
}