headgate-api 0.1.2

A policy-aware distributed job queue with PostgreSQL, MySQL, and Redis backends.
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
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
//! Control-API integration tests over the live Postgres store. Opt-in via HG_TEST_PG;
//! requests go straight through the tower service — no listener needed.

use std::sync::Arc;

use axum::body::Body;
use axum::http::{Method, Request, StatusCode, header};
use headgate_api::{ApiConfig, router};
use headgate_core::{
    AdmitRequest, Envelope, Inspect, JobResult, MissedPolicy, Outcome, OutputStore, ProgressStore,
    ProgressUpdate, Schedule, Store,
};
use headgate_postgres::PgStore;
use http_body_util::BodyExt;
use serde_json::{Value, json};
use tower::ServiceExt;

async fn call_with_key(
    app: &axum::Router,
    method: Method,
    uri: &str,
    body: Option<Value>,
    key: &str,
) -> (StatusCode, Value) {
    let mut req = Request::builder()
        .method(method.clone())
        .uri(uri)
        .header("idempotency-key", key);
    if body.is_some() {
        req = req.header(header::CONTENT_TYPE, "application/json");
    }
    let req = req
        .body(
            body.map(|b| Body::from(b.to_string()))
                .unwrap_or_else(Body::empty),
        )
        .unwrap();
    let resp = app.clone().oneshot(req).await.unwrap();
    let status = resp.status();
    let bytes = resp.into_body().collect().await.unwrap().to_bytes();
    let v = serde_json::from_slice(&bytes).unwrap_or(Value::Null);
    (status, v)
}

async fn call(
    app: &axum::Router,
    method: Method,
    uri: &str,
    body: Option<Value>,
) -> (StatusCode, Value) {
    // Distinct per call — reusing a key would (correctly!) replay a previous enqueue.
    static SEQ: std::sync::atomic::AtomicU64 = std::sync::atomic::AtomicU64::new(0);
    let seq = SEQ.fetch_add(1, std::sync::atomic::Ordering::Relaxed);
    call_with_key(app, method, uri, body, &format!("test-{seq}")).await
}

fn b64(s: &str) -> String {
    use base64::Engine;
    base64::engine::general_purpose::STANDARD.encode(s)
}

#[tokio::test]
async fn mid_run_output_has_an_explicit_payload_endpoint() {
    let Ok(conninfo) = std::env::var("HG_TEST_PG") else {
        eprintln!("HG_TEST_PG not set; skipping mid-run output API proof");
        return;
    };
    let store = Arc::new(PgStore::connect(&conninfo, 2).expect("connect"));
    {
        let tx = store.begin().await.unwrap();
        tx.client()
            .unwrap()
            .execute("DELETE FROM headgate_job WHERE ulid = 'api-output'", &[])
            .await
            .unwrap();
        tx.commit().await.unwrap();
    }
    store
        .enqueue(&[Envelope {
            id: "api-output".into(),
            kind: "api.output".into(),
            queue: "api-output".into(),
            payload: b"{}".to_vec(),
            fingerprint: headgate_core::fingerprint("api.output", b"api-output"),
            scheduled_at_ms: 1,
            retention_ms: 0,
            ..Default::default()
        }])
        .await
        .unwrap();
    let claim = store
        .admit(AdmitRequest {
            worker: "api-output-worker".into(),
            lease_id: "api-output-lease".into(),
            queues: vec!["api-output".into()],
            capacity: 1,
            lease: std::time::Duration::from_secs(30),
            quantum: 1,
        })
        .await
        .unwrap()
        .into_iter()
        .next()
        .unwrap()
        .claims[0]
        .lease_ref();
    let lease = claim;
    let output = OutputStore::write_job_output(
        store.as_ref(),
        &lease,
        &JobResult {
            schema_version: 7,
            bytes: vec![0, 0xff],
        },
    )
    .await
    .unwrap();
    let inspect: Arc<dyn Inspect> = store.clone();
    let app = router(inspect, ApiConfig::default());

    let (status, body) = call(&app, Method::GET, "/api/v1/jobs/api-output/output", None).await;
    assert_eq!(status, StatusCode::OK, "{body}");
    assert_eq!(body["schema_version"], 7);
    assert_eq!(body["bytes"], "AP8=");
    assert_eq!(body["fence"], output.fence);
    assert_eq!(body["updated_at_ms"], output.updated_at_ms);

    let (status, body) = call(&app, Method::GET, "/api/v1/jobs/api-output", None).await;
    assert_eq!(status, StatusCode::OK, "{body}");
    assert!(
        body.get("output").is_none(),
        "ordinary job detail leaked output"
    );

    store
        .ack(&lease, Outcome::Success, None, None)
        .await
        .unwrap();
}

#[tokio::test]
async fn job_progress_has_an_explicit_operator_endpoint() {
    let Ok(conninfo) = std::env::var("HG_TEST_PG") else {
        eprintln!("HG_TEST_PG not set; skipping job progress API proof");
        return;
    };
    let store = Arc::new(PgStore::connect(&conninfo, 2).expect("connect"));
    {
        let tx = store.begin().await.unwrap();
        tx.client()
            .unwrap()
            .execute("DELETE FROM headgate_job WHERE ulid = 'api-progress'", &[])
            .await
            .unwrap();
        tx.commit().await.unwrap();
    }
    store
        .enqueue(&[Envelope {
            id: "api-progress".into(),
            kind: "api.progress".into(),
            queue: "api-progress".into(),
            payload: b"{}".to_vec(),
            fingerprint: headgate_core::fingerprint("api.progress", b"api-progress"),
            scheduled_at_ms: 1,
            retention_ms: 0,
            ..Default::default()
        }])
        .await
        .unwrap();
    let lease = store
        .admit(AdmitRequest {
            worker: "api-progress-worker".into(),
            lease_id: "api-progress-lease".into(),
            queues: vec!["api-progress".into()],
            capacity: 1,
            lease: std::time::Duration::from_secs(30),
            quantum: 1,
        })
        .await
        .unwrap()
        .into_iter()
        .next()
        .unwrap()
        .claims[0]
        .lease_ref();
    let progress = ProgressStore::write_job_progress(
        store.as_ref(),
        &lease,
        &ProgressUpdate {
            current: 42,
            total: 100,
            message: Some("encoding frame 420".into()),
        },
    )
    .await
    .unwrap();
    let inspect: Arc<dyn Inspect> = store.clone();
    let app = router(inspect, ApiConfig::default());

    let (status, body) = call(
        &app,
        Method::GET,
        "/api/v1/jobs/api-progress/progress",
        None,
    )
    .await;
    assert_eq!(status, StatusCode::OK, "{body}");
    assert_eq!(body["current"], 42);
    assert_eq!(body["total"], 100);
    assert_eq!(body["message"], "encoding frame 420");
    assert_eq!(body["fence"], progress.fence);
    assert_eq!(body["updated_at_ms"], progress.updated_at_ms);

    let (status, body) = call(&app, Method::GET, "/api/v1/jobs/api-progress", None).await;
    assert_eq!(status, StatusCode::OK, "{body}");
    assert!(
        body.get("progress").is_none(),
        "ordinary job detail leaked progress"
    );

    store
        .ack(&lease, Outcome::Success, None, None)
        .await
        .unwrap();
}

#[tokio::test]
async fn enqueue_outage_is_service_unavailable_not_a_bad_request() {
    let store = Arc::new(
        PgStore::connect(
            "host=127.0.0.1 port=1 user=headgate dbname=headgate connect_timeout=1",
            1,
        )
        .expect("construct lazy pool"),
    );
    let inspect: Arc<dyn Inspect> = store;
    let breaker = Arc::new(
        headgate::CircuitBreaker::new(headgate::CircuitBreakerConfig {
            failure_threshold: 1,
            recovery_timeout: std::time::Duration::from_secs(60),
            half_open_max_calls: 1,
        })
        .unwrap(),
    );
    let mut config = ApiConfig::default();
    config.enqueue_circuit_breaker = Some(breaker.clone());
    let app = router(inspect, config);
    let (status, body) = call_with_key(
        &app,
        Method::POST,
        "/api/v1/jobs",
        Some(json!({"id": "api-outage", "kind": "outage", "payload": b64("{}")})),
        "api-outage",
    )
    .await;
    assert_eq!(status, StatusCode::SERVICE_UNAVAILABLE, "{body}");
    assert!(
        body["error"]
            .as_str()
            .is_some_and(|message| message.starts_with("store unavailable:")),
        "typed outage body changed: {body}"
    );

    let (status, body) = call_with_key(
        &app,
        Method::POST,
        "/api/v1/jobs",
        Some(json!({"id": "api-circuit", "kind": "outage", "payload": b64("{}")})),
        "api-circuit",
    )
    .await;
    assert_eq!(status, StatusCode::SERVICE_UNAVAILABLE, "{body}");
    assert_eq!(body["error"], "enqueue circuit open");
    assert_eq!(body["state"], "open");
    assert!(
        body["retry_after_ms"].as_u64().is_some_and(|ms| ms > 0),
        "open circuit must tell the caller when probes resume: {body}"
    );
    assert_eq!(breaker.snapshot().state, headgate::CircuitState::Open);
}

#[tokio::test]
async fn enqueue_authorization_guards_http_and_periodic_paths() {
    let Ok(conninfo) = std::env::var("HG_TEST_PG") else {
        eprintln!("HG_TEST_PG not set; skipping enqueue authorization API proof");
        return;
    };
    let store = Arc::new(PgStore::connect(&conninfo, 2).expect("connect"));
    {
        let tx = store.begin().await.unwrap();
        tx.client()
            .unwrap()
            .batch_execute(
                "DELETE FROM headgate_job WHERE queue IN ('api-auth', 'api-auth-periodic');
                 DELETE FROM headgate_schedule WHERE id IN ('api-auth-denied', 'api-auth-existing');",
            )
            .await
            .unwrap();
        tx.commit().await.unwrap();
    }

    let authorizer: Arc<dyn headgate::EnqueueAuthorizer> = Arc::new(
        |context: &headgate::EnqueueContext, envelope: &headgate::Envelope| {
            context.source == headgate::EnqueueSource::Http
                && context
                    .identity
                    .as_ref()
                    .is_some_and(|identity| identity.subject == "service:mailer")
                && envelope.kind == "mail.send"
        },
    );
    let inspect: Arc<dyn Inspect> = store.clone();
    let mut config = ApiConfig::default();
    config.enqueue_authorizer = authorizer;
    let app = router(inspect, config).layer(axum::Extension(headgate::EnqueueIdentity::new(
        "service:mailer",
    )));

    let (status, body) = call_with_key(
        &app,
        Method::POST,
        "/api/v1/jobs",
        Some(json!({
            "id": "api-auth-http-denied",
            "kind": "billing.charge",
            "payload": b64("{}"),
            "queue": "api-auth"
        })),
        "api-auth-1",
    )
    .await;
    assert_eq!(status, StatusCode::FORBIDDEN, "{body}");
    assert_eq!(
        body,
        json!({"error": "enqueue forbidden", "kind": "billing.charge"})
    );
    assert!(
        store
            .get_job("api-auth-http-denied", false)
            .await
            .unwrap()
            .is_none(),
        "a forbidden request must not touch the store"
    );

    let (status, _) = call_with_key(
        &app,
        Method::POST,
        "/api/v1/jobs",
        Some(json!({
            "id": "api-auth-http-allowed",
            "kind": "mail.send",
            "payload": b64("{}"),
            "queue": "api-auth"
        })),
        "api-auth-2",
    )
    .await;
    assert_eq!(status, StatusCode::CREATED);

    let (status, body) = call_with_key(
        &app,
        Method::PUT,
        "/api/v1/periodic/api-auth-denied",
        Some(json!({"kind": "billing.charge", "spec": "@every:60000"})),
        "api-auth-3",
    )
    .await;
    assert_eq!(status, StatusCode::FORBIDDEN, "{body}");
    assert!(
        store
            .list_schedules()
            .await
            .unwrap()
            .iter()
            .all(|schedule| schedule.id != "api-auth-denied"),
        "a forbidden periodic definition must not create a future bypass"
    );

    store
        .upsert_schedule(&Schedule {
            id: "api-auth-existing".into(),
            kind: "billing.charge".into(),
            payload: b"{}".to_vec(),
            queue: "api-auth-periodic".into(),
            partition_key: String::new(),
            rate_class: String::new(),
            priority: 0,
            max_attempts: 25,
            retention_ms: 86_400_000,
            spec: "@every:60000".into(),
            next_run_ms: i64::MAX / 2,
            last_enqueued_ms: None,
            on_missed: MissedPolicy::Skip,
            backfill_limit: 0,
            paused: false,
        })
        .await
        .unwrap();
    let before: i64 = store
        .counts(Some("api-auth-periodic"))
        .await
        .unwrap()
        .counts
        .iter()
        .map(|(_, count)| count)
        .sum();
    let (status, body) = call_with_key(
        &app,
        Method::POST,
        "/api/v1/periodic/api-auth-existing/run",
        None,
        "api-auth-4",
    )
    .await;
    assert_eq!(status, StatusCode::FORBIDDEN, "{body}");
    let after: i64 = store
        .counts(Some("api-auth-periodic"))
        .await
        .unwrap()
        .counts
        .iter()
        .map(|(_, count)| count)
        .sum();
    assert_eq!(before, after, "manual periodic run bypassed authorization");
}

#[tokio::test]
async fn control_api_end_to_end() {
    let Ok(conninfo) = std::env::var("HG_TEST_PG") else {
        eprintln!("HG_TEST_PG not set; skipping API test");
        return;
    };
    let store = Arc::new(PgStore::connect(&conninfo, 4).expect("connect"));
    {
        let tx = store.begin().await.unwrap();
        tx.client()
            .unwrap()
            .batch_execute(
                "DELETE FROM headgate_job WHERE queue LIKE 'api-%';
                 DELETE FROM headgate_queue_state WHERE queue LIKE 'api-%';
                 DELETE FROM headgate_rate_bucket WHERE name = 'api-stripe';
                 DELETE FROM headgate_concurrency_limit WHERE name = 'api-cl';",
            )
            .await
            .unwrap();
        tx.commit().await.unwrap();
    }
    let inspect: Arc<dyn Inspect> = store.clone();
    let app = router(inspect, ApiConfig::default());

    // meta declares only what the backend honors (runtime capability boundary).
    let (st, v) = call(&app, Method::GET, "/api/v1/meta", None).await;
    assert_eq!(st, StatusCode::OK);
    assert_eq!(v["backend"], "postgres");
    assert!(
        v["capabilities"]
            .as_array()
            .unwrap()
            .iter()
            .any(|c| c == "inspect")
    );

    // A mutating call without Idempotency-Key is a 400.
    let req = Request::builder()
        .method(Method::POST)
        .uri("/api/v1/jobs")
        .header(header::CONTENT_TYPE, "application/json")
        .body(Body::from(
            json!({"kind": "k", "payload": b64("{}")}).to_string(),
        ))
        .unwrap();
    assert_eq!(
        app.clone().oneshot(req).await.unwrap().status(),
        StatusCode::BAD_REQUEST
    );

    // JSON preserves presence, so an explicit zero is invalid even though the binary
    // envelope reserves zero as the backwards-compatible "field omitted" sentinel.
    let (st, v) = call(
        &app,
        Method::POST,
        "/api/v1/jobs",
        Some(json!({"kind": "api:msg", "payload": b64("{}"), "weight": 0})),
    )
    .await;
    assert_eq!(st, StatusCode::BAD_REQUEST);
    assert_eq!(v["error"], "weight must be >= 1");

    let (st, v) = call(
        &app,
        Method::POST,
        "/api/v1/jobs",
        Some(json!({"kind": "api:msg", "payload": b64("{}"), "unique_replace": 4})),
    )
    .await;
    assert_eq!(st, StatusCode::BAD_REQUEST);
    assert_eq!(
        v["error"],
        "unique_replace requires caller-supplied unique_key"
    );

    // Queue weight and saturation strategy are fleet policy the gate reads, so
    // invariant 16 requires an operational writer and a read-back surface.
    let (st, _) = call(
        &app,
        Method::PUT,
        "/api/v1/queues/api-weighted",
        Some(json!({"weight": 3})),
    )
    .await;
    assert_eq!(st, StatusCode::OK);
    let (st, queues) = call(&app, Method::GET, "/api/v1/queues", None).await;
    assert_eq!(st, StatusCode::OK);
    assert_eq!(
        queues
            .as_array()
            .unwrap()
            .iter()
            .find(|q| q["queue"] == "api-weighted")
            .unwrap()["weight"],
        3
    );

    let (st, _) = call(
        &app,
        Method::PUT,
        "/api/v1/concurrency-limits/api-cl",
        Some(json!({
            "queue": "api-weighted",
            "max_concurrent": 2,
            "on_saturated": "cancel_running"
        })),
    )
    .await;
    assert_eq!(st, StatusCode::OK);
    let (st, limits) = call(&app, Method::GET, "/api/v1/concurrency-limits", None).await;
    assert_eq!(st, StatusCode::OK);
    let limit = limits
        .as_array()
        .unwrap()
        .iter()
        .find(|v| v["name"] == "api-cl")
        .unwrap();
    assert_eq!(limit["queue"], "api-weighted");
    assert_eq!(limit["max_concurrent"], 2);
    assert_eq!(limit["on_saturated"], "cancel_running");

    // Enqueue over HTTP; a retried POST with the same key replays the same job.
    let enq = json!({ "kind": "api:msg", "payload": b64("{\"n\":1}"), "queue": "api-q" });
    let (st, v1) = call_with_key(
        &app,
        Method::POST,
        "/api/v1/jobs",
        Some(enq.clone()),
        "retry-me",
    )
    .await;
    assert_eq!(st, StatusCode::CREATED, "{v1}");
    let (st, v2) = call_with_key(&app, Method::POST, "/api/v1/jobs", Some(enq), "retry-me").await;
    assert_eq!(st, StatusCode::CREATED);
    assert_eq!(
        v1["id"], v2["id"],
        "same Idempotency-Key must replay, not duplicate"
    );
    assert_eq!(v2["replayed"], true);
    let id = v1["id"].as_str().unwrap().to_string();

    // Detail: payload only on request (invariant 9).
    let (st, v) = call(&app, Method::GET, &format!("/api/v1/jobs/{id}"), None).await;
    assert_eq!(st, StatusCode::OK);
    assert!(
        v.get("payload").is_none(),
        "payload must be withheld by default"
    );
    let (_, v) = call(
        &app,
        Method::GET,
        &format!("/api/v1/jobs/{id}?include_payload=true"),
        None,
    )
    .await;
    assert_eq!(v["payload"], b64("{\"n\":1}"));

    // Admission explain: available and nothing blocking → admissible.
    let (st, v) = call(
        &app,
        Method::GET,
        &format!("/api/v1/jobs/{id}/admission"),
        None,
    )
    .await;
    assert_eq!(st, StatusCode::OK);
    assert_eq!(v["admissible"], true, "{v}");

    // Pause the queue → the same job is now blocked_by queue_paused.
    let (st, _) = call(&app, Method::POST, "/api/v1/queues/api-q/pause", None).await;
    assert_eq!(st, StatusCode::NO_CONTENT);
    let (_, v) = call(
        &app,
        Method::GET,
        &format!("/api/v1/jobs/{id}/admission"),
        None,
    )
    .await;
    assert_eq!(v["blocked_by"], "queue_paused");
    assert_eq!(
        v["estimated_admission_ms"],
        Value::Null,
        "won't clear on its own"
    );
    call(&app, Method::POST, "/api/v1/queues/api-q/resume", None).await;

    // Rate-class kill switch (invariant 16): pause the class, job is blocked_by
    // rate_class with no ETA; unpause, and it clears.
    let (st, _) = call(
        &app,
        Method::POST,
        "/api/v1/jobs",
        Some(json!({
            "kind": "api:limited", "payload": b64("{}"), "queue": "api-q",
            "rate_class": "api-stripe", "id": "api-rc-1"
        })),
    )
    .await;
    assert_eq!(st, StatusCode::CREATED);
    let (st, _) = call(
        &app,
        Method::PUT,
        "/api/v1/rate-classes/api-stripe",
        Some(json!({ "limit": 5, "window_ms": 1000, "burst": 5, "paused": true })),
    )
    .await;
    assert_eq!(st, StatusCode::OK);
    let (_, v) = call(&app, Method::GET, "/api/v1/jobs/api-rc-1/admission", None).await;
    assert_eq!(v["blocked_by"], "rate_class", "{v}");
    assert_eq!(v["estimated_admission_ms"], Value::Null);
    let (_, v) = call(&app, Method::GET, "/api/v1/rate-classes", None).await;
    let rc = v
        .as_array()
        .unwrap()
        .iter()
        .find(|r| r["name"] == "api-stripe")
        .unwrap();
    assert_eq!(rc["paused"], true);
    let (st, _) = call(
        &app,
        Method::PUT,
        "/api/v1/rate-classes/api-stripe",
        Some(json!({ "limit": 5, "window_ms": 1000, "burst": 5 })),
    )
    .await;
    assert_eq!(st, StatusCode::OK);
    // Unpaused: tokens refill from 0, so the ETA is finite now.
    // Round 32h: this used to be wrapped in `if v["admissible"] == false { .. }`, which
    // skips itself for `true` AND for `Value::Null` — the value `call` returns for a
    // non-JSON or error response. It was the suite's ONLY coverage of the finite-ETA
    // computation and it was opt-out by construction. Both arms are asserted now: the
    // job is either admissible (the class refilled far enough) or blocked BY THE RATE
    // CLASS with a finite ETA. Nothing else is a legal answer, `null` included.
    let (_, v) = call(&app, Method::GET, "/api/v1/jobs/api-rc-1/admission", None).await;
    match v["admissible"].as_bool() {
        Some(true) => assert_eq!(v["blocked_by"], Value::Null, "{v}"),
        Some(false) => {
            assert_eq!(v["blocked_by"], "rate_class", "{v}");
            assert!(
                v["estimated_admission_ms"].as_i64().is_some(),
                "an unpaused class must give a FINITE eta: {v}"
            );
        }
        None => panic!("/admission must answer with a boolean `admissible`: {v}"),
    }

    // A window that would divide by zero is rejected at the boundary (boundary validation).
    let (st, _) = call(
        &app,
        Method::PUT,
        "/api/v1/rate-classes/api-stripe",
        Some(json!({ "limit": 5, "window_ms": 0 })),
    )
    .await;
    assert_eq!(st, StatusCode::BAD_REQUEST);

    // Counts and list see the jobs.
    let (_, v) = call(&app, Method::GET, "/api/v1/jobs/counts?queue=api-q", None).await;
    assert_eq!(v["counts"]["available"], 2, "{v}");
    let (_, v) = call(&app, Method::GET, "/api/v1/jobs?queue=api-q&limit=1", None).await;
    assert_eq!(v["jobs"].as_array().unwrap().len(), 1);
    assert!(v["next_cursor"].is_string(), "a full page carries a cursor");

    // Cancel → terminal; retry from cancelled is refused (not a table row); delete works.
    let (st, _) = call(
        &app,
        Method::POST,
        &format!("/api/v1/jobs/{id}/cancel"),
        None,
    )
    .await;
    assert_eq!(st, StatusCode::NO_CONTENT);
    let (st, v) = call(
        &app,
        Method::POST,
        &format!("/api/v1/jobs/{id}/retry"),
        None,
    )
    .await;
    assert_eq!(
        st,
        StatusCode::BAD_REQUEST,
        "operator_retry is archived-only: {v}"
    );
    let (st, v) = call(
        &app,
        Method::POST,
        "/api/v1/jobs/actions",
        Some(json!({ "action": "delete", "ids": [id, "api-rc-1", "no-such-job"] })),
    )
    .await;
    assert_eq!(st, StatusCode::OK);
    assert_eq!(v["succeeded"].as_array().unwrap().len(), 2, "{v}");
    assert_eq!(v["failed"][0]["id"], "no-such-job");

    // Unknown job → 404; liveness/readiness answer.
    let (st, _) = call(&app, Method::GET, "/api/v1/jobs/nope/admission", None).await;
    assert_eq!(st, StatusCode::NOT_FOUND);
    let (st, _) = call(&app, Method::GET, "/api/v1/healthz", None).await;
    assert_eq!(st, StatusCode::OK);
    let (st, _) = call(&app, Method::GET, "/api/v1/readyz", None).await;
    assert_eq!(st, StatusCode::OK);
}

#[tokio::test]
async fn phase4_periodic_bulk_workers_search() {
    let Ok(conninfo) = std::env::var("HG_TEST_PG") else {
        eprintln!("HG_TEST_PG not set; skipping API phase-4 test");
        return;
    };
    let store = Arc::new(PgStore::connect(&conninfo, 4).expect("connect"));
    {
        let tx = store.begin().await.unwrap();
        tx.client()
            .unwrap()
            .batch_execute(
                "DELETE FROM headgate_job WHERE queue LIKE 'p4-%';
                 DELETE FROM headgate_schedule WHERE id LIKE 'p4-%';
                 DELETE FROM headgate_operation WHERE id LIKE 'hg%';
                 DELETE FROM headgate_worker WHERE worker_id LIKE 'p4-%';",
            )
            .await
            .unwrap();
        tx.commit().await.unwrap();
    }
    let inspect: Arc<dyn Inspect> = store.clone();
    let app = router(inspect, ApiConfig::default());

    // Periodic upsert validates the spec; the entry lists; fire-now enqueues and a
    // retried fire with the same key replays.
    let (st, v) = call(
        &app,
        Method::PUT,
        "/api/v1/periodic/p4-daily",
        Some(json!({
            "kind": "p4:tick", "spec": "not a cron"
        })),
    )
    .await;
    assert_eq!(st, StatusCode::BAD_REQUEST, "bad spec rejected: {v}");
    let (st, _) = call(
        &app,
        Method::PUT,
        "/api/v1/periodic/p4-daily",
        Some(json!({
            "kind": "p4:tick", "spec": "@every:3600000", "queue": "p4-q",
            "payload": b64("{}")
        })),
    )
    .await;
    assert_eq!(st, StatusCode::OK);
    let (_, v) = call(&app, Method::GET, "/api/v1/periodic", None).await;
    assert!(
        v.as_array().unwrap().iter().any(|s| s["id"] == "p4-daily"),
        "{v}"
    );
    let (st, r1) = call_with_key(
        &app,
        Method::POST,
        "/api/v1/periodic/p4-daily/run",
        None,
        "fire-1",
    )
    .await;
    assert_eq!(st, StatusCode::ACCEPTED);
    let (_, r2) = call_with_key(
        &app,
        Method::POST,
        "/api/v1/periodic/p4-daily/run",
        None,
        "fire-1",
    )
    .await;
    assert_eq!(r1["id"], r2["id"], "same key replays the same fired job");

    // Bulk: empty selector is rejected; a real selector becomes an operation the duty
    // executes in batches; dry_run completes immediately with an estimate.
    let (st, v) = call(
        &app,
        Method::POST,
        "/api/v1/jobs/bulk",
        Some(json!({
            "action": "cancel", "selector": {}
        })),
    )
    .await;
    assert_eq!(st, StatusCode::BAD_REQUEST, "empty selector: {v}");
    for i in 0..3 {
        call(&app, Method::POST, "/api/v1/jobs", Some(json!({
            "kind": "p4:bulk", "payload": b64("{}"), "queue": "p4-bulk", "id": format!("p4-b{i}")
        }))).await;
    }
    let (st, v) = call(
        &app,
        Method::POST,
        "/api/v1/jobs/bulk",
        Some(json!({
            "action": "cancel", "selector": {"queue": "p4-bulk"}, "dry_run": true
        })),
    )
    .await;
    assert_eq!(st, StatusCode::ACCEPTED);
    assert_eq!(v["status"], "completed");
    assert_eq!(v["total_estimated"], 3, "{v}");
    let (st, v) = call(
        &app,
        Method::POST,
        "/api/v1/jobs/bulk",
        Some(json!({
            "action": "cancel", "selector": {"queue": "p4-bulk"}
        })),
    )
    .await;
    assert_eq!(st, StatusCode::ACCEPTED);
    let op_id = v["id"].as_str().unwrap().to_string();
    // Drive the executor directly (in production the "operations" duty does this).
    store.run_pending_operations(1000).await.unwrap();
    let (_, v) = call(
        &app,
        Method::GET,
        &format!("/api/v1/operations/{op_id}"),
        None,
    )
    .await;
    assert_eq!(v["status"], "completed", "{v}");
    assert_eq!(v["affected"], 3);
    let (_, v) = call(&app, Method::GET, "/api/v1/jobs/counts?queue=p4-bulk", None).await;
    assert_eq!(v["counts"]["cancelled"], 3, "{v}");

    // The q search grammar: field terms AND a bare kind-prefix term.
    call(
        &app,
        Method::POST,
        "/api/v1/jobs",
        Some(json!({
            "kind": "p4:search", "payload": b64("{}"), "queue": "p4-q", "id": "p4-s1", "priority": 7
        })),
    )
    .await;
    let (_, v) = call(
        &app,
        Method::GET,
        "/api/v1/jobs?q=queue:p4-q%20priority:7",
        None,
    )
    .await;
    assert_eq!(v["jobs"].as_array().unwrap().len(), 1, "{v}");
    assert_eq!(v["jobs"][0]["id"], "p4-s1");
    // A bare term (no colon) is a kind prefix. Kinds that CONTAIN colons need the
    // explicit kind: field — a colon term always parses as field:value.
    let (_, v) = call(&app, Method::GET, "/api/v1/jobs?q=p4&queue=p4-q", None).await;
    assert!(
        v["jobs"]
            .as_array()
            .unwrap()
            .iter()
            .any(|j| j["id"] == "p4-s1"),
        "bare term is a kind prefix: {v}"
    );
    let (_, v) = call(&app, Method::GET, "/api/v1/jobs?q=kind:p4:search", None).await;
    assert_eq!(
        v["jobs"].as_array().unwrap().len(),
        1,
        "kind: takes colon values: {v}"
    );
    let (st, _) = call(&app, Method::GET, "/api/v1/jobs?q=bogus:x", None).await;
    assert_eq!(st, StatusCode::BAD_REQUEST);

    // Reschedule moves a scheduled job's run time; payload edit changes the
    // fingerprint with the payload.
    call(
        &app,
        Method::POST,
        "/api/v1/jobs",
        Some(json!({
            "kind": "p4:later", "payload": b64("{\"v\":1}"), "queue": "p4-q", "id": "p4-r1",
            "scheduled_at_ms": 99999999999999i64
        })),
    )
    .await;
    let (st, _) = call(
        &app,
        Method::POST,
        "/api/v1/jobs/p4-r1/reschedule",
        Some(json!({ "scheduled_at_ms": 12345 })),
    )
    .await;
    assert_eq!(st, StatusCode::NO_CONTENT);
    let (_, v) = call(&app, Method::GET, "/api/v1/jobs/p4-r1", None).await;
    assert_eq!(v["scheduled_at_ms"], 12345);
    let old_fp = v["fingerprint"].as_str().unwrap().to_string();
    let (st, _) = call(
        &app,
        Method::PUT,
        "/api/v1/jobs/p4-r1/payload",
        Some(json!({ "payload": b64("{\"v\":2}") })),
    )
    .await;
    assert_eq!(st, StatusCode::NO_CONTENT);
    let (_, v) = call(
        &app,
        Method::GET,
        "/api/v1/jobs/p4-r1?include_payload=true",
        None,
    )
    .await;
    assert_eq!(v["payload"], b64("{\"v\":2}"));
    assert_ne!(
        v["fingerprint"].as_str().unwrap(),
        old_fp,
        "fingerprint follows the payload"
    );

    // Workers: a heartbeat registers; the listing shows it.
    store
        .heartbeat_worker(&headgate_core::WorkerMeta {
            worker_id: "p4-w1".into(),
            host: "testhost".into(),
            pid: 42,
            queues: vec!["p4-q".into()],
            concurrency: 8,
            started_at_ms: 1,
            heartbeat_at_ms: 0,
            // round 32: the cluster view's / backlog metrics's additive beat payload.
            inflight: 6,
            polls: 10,
            empty_polls: 2,
        })
        .await
        .unwrap();
    let (_, v) = call(&app, Method::GET, "/api/v1/workers", None).await;
    assert!(
        v.as_array()
            .unwrap()
            .iter()
            .any(|w| w["worker_id"] == "p4-w1"),
        "{v}"
    );

    // surveyed policy behavior the CLUSTER VIEW (round 32). The operational answer this endpoint exists
    // for is "which queues have ZERO live workers" — asserted here in both directions:
    // p4-q is covered by the worker just registered, and p4-bulk (which has jobs and
    // no worker) is PRESENT with live_workers = 0 rather than simply missing.
    //
    // STATE assertions, not global counts: this database is shared with the other test
    // binaries, which run real workers that register real rows. So the fleet totals are
    // asserted as ">= this worker's contribution" and the exact aggregation arithmetic
    // is pinned where it can be deterministic — scripts/test-admission.sh seeds a fixed
    // two-worker registry and byte-diffs /cluster across both language servers.
    // Round 32h: the fleet totals used to be `>= 8` / `>= 6`, which SIBLING binaries
    // satisfy on their own — runtime.rs registers a capacity-4 worker with inflight > 0,
    // bounded_pool.rs a capacity-6 one, signals.rs another — so deleting the
    // heartbeat_worker call above would not have failed them. The check that cannot be
    // satisfied by someone else's row is per-worker: find p4-w1 in the listing and pin
    // ITS numbers, then assert the fleet totals are at least that AND at least as large
    // as the listing they aggregate. `is_number()` is dropped: it passes for 0, so the
    // aggregation arithmetic was not checked at all.
    let (_, ws) = call(&app, Method::GET, "/api/v1/workers", None).await;
    let mine = ws
        .as_array()
        .unwrap()
        .iter()
        .find(|w| w["worker_id"] == "p4-w1")
        .unwrap_or_else(|| panic!("p4-w1 must be listed: {ws}"));
    assert_eq!(mine["concurrency"], 8, "{mine}");
    assert_eq!(mine["inflight"], 6, "{mine}");
    let live: i64 = ws.as_array().unwrap().len() as i64;
    let (_, v) = call(&app, Method::GET, "/api/v1/cluster", None).await;
    assert_eq!(
        v["workers"]["live"].as_i64().unwrap(),
        live,
        "/cluster's live count must equal the /workers listing it aggregates: {v} {ws}"
    );
    assert!(v["capacity_total"].as_i64().unwrap() >= 8, "{v}");
    assert!(v["inflight_total"].as_i64().unwrap() >= 6, "{v}");
    // utilization is inflight/capacity over LIVE workers — a ratio of sums, so it is
    // strictly positive here (p4-w1 alone contributes 6 of its 8) and never > 1.
    let util = v["utilization"].as_f64().unwrap();
    assert!(
        util > 0.0 && util <= 1.0,
        "utilization must be a real ratio of sums: {v}"
    );
    assert!(v["empty_poll_ratio"].as_f64().unwrap() >= 0.0, "{v}");
    let qs = v["queues"].as_array().unwrap();
    let covered = qs
        .iter()
        .find(|q| q["queue"] == "p4-q")
        .expect("p4-q must be listed");
    assert!(covered["live_workers"].as_i64().unwrap() >= 1, "{v}");
    let uncovered = qs
        .iter()
        .find(|q| q["queue"] == "p4-bulk")
        .expect("a queue with no live worker must be LISTED, not omitted");
    assert_eq!(uncovered["live_workers"], 0, "{v}");

    // Schedule delete.
    let (st, _) = call(&app, Method::DELETE, "/api/v1/periodic/p4-daily", None).await;
    assert_eq!(st, StatusCode::NO_CONTENT);
    let (st, _) = call(&app, Method::DELETE, "/api/v1/periodic/p4-daily", None).await;
    assert_eq!(st, StatusCode::NOT_FOUND);
}

#[tokio::test]
async fn sse_events_stream_queue_activity() {
    let Ok(conninfo) = std::env::var("HG_TEST_PG") else {
        eprintln!("HG_TEST_PG not set; skipping SSE test");
        return;
    };
    // connect() carries LISTEN config, so this store is Notifying — the SSE feed.
    let store = Arc::new(PgStore::connect(&conninfo, 4).expect("connect"));
    let inspect: Arc<dyn Inspect> = store.clone();
    let app = router(inspect, ApiConfig::default());

    let resp = app
        .clone()
        .oneshot(
            Request::builder()
                .method(Method::GET)
                .uri("/api/v1/events")
                .body(Body::empty())
                .unwrap(),
        )
        .await
        .unwrap();
    assert_eq!(resp.status(), StatusCode::OK);
    assert!(
        resp.headers()
            .get(header::CONTENT_TYPE)
            .unwrap()
            .to_str()
            .unwrap()
            .starts_with("text/event-stream")
    );

    // Feed enqueues until the subscription (established when the body is first polled)
    // observes one — the poll-fallback contract again: missed pushes cost latency only.
    let feeder = {
        let store = store.clone();
        tokio::spawn(async move {
            for i in 0..40u32 {
                let _ = store
                    .enqueue(&[headgate_core::Envelope {
                        id: format!("sse-{}-{i}", std::process::id()),
                        kind: "sse".into(),
                        payload: vec![0],
                        queue: "sse-q".into(),
                        fingerprint: "fp-sse".into(),
                        scheduled_at_ms: 1,
                        ..Default::default()
                    }])
                    .await;
                tokio::time::sleep(std::time::Duration::from_millis(150)).await;
            }
        })
    };

    use http_body_util::BodyExt;
    let mut body = resp.into_body();
    let deadline = tokio::time::Instant::now() + std::time::Duration::from_secs(8);
    let mut seen = String::new();
    let got = loop {
        let frame = tokio::time::timeout_at(deadline, body.frame())
            .await
            .expect("an SSE event within 8s")
            .expect("stream open")
            .expect("frame ok");
        if let Some(data) = frame.data_ref() {
            seen.push_str(&String::from_utf8_lossy(data));
            if seen.contains("queue_activity") && seen.contains("sse-q") {
                break true;
            }
        }
    };
    feeder.abort();
    assert!(
        got,
        "expected a queue_activity event naming sse-q, saw: {seen}"
    );
}

/// authorization boundary read-only mode: every mutating route 403s with one message; GETs still serve.
#[tokio::test]
async fn read_only_mode_rejects_mutations() {
    use http_body_util::BodyExt;
    use tower::util::ServiceExt;
    let Ok(conninfo) = std::env::var("HG_TEST_PG") else {
        eprintln!("HG_TEST_PG not set; skipping read-only test");
        return;
    };
    let store = std::sync::Arc::new(headgate_postgres::PgStore::connect(&conninfo, 2).unwrap());
    let inspect: std::sync::Arc<dyn headgate_core::Inspect> = store;
    let app = headgate_api::router(
        inspect,
        headgate_api::ApiConfig {
            read_only: true,
            ..Default::default()
        },
    );
    let res = app
        .clone()
        .oneshot(
            axum::http::Request::builder()
                .method("POST")
                .uri("/api/v1/queues/ro-q/pause")
                .header("Idempotency-Key", "ro-1")
                .body(axum::body::Body::empty())
                .unwrap(),
        )
        .await
        .unwrap();
    assert_eq!(res.status(), 403);
    let body =
        String::from_utf8(res.into_body().collect().await.unwrap().to_bytes().to_vec()).unwrap();
    assert!(body.contains("read-only mode"), "{body}");
    let res = app
        .oneshot(
            axum::http::Request::builder()
                .uri("/api/v1/meta")
                .body(axum::body::Body::empty())
                .unwrap(),
        )
        .await
        .unwrap();
    assert_eq!(res.status(), 200, "GETs still serve in read-only mode");
}

// ---------------------------------------------------------------------------
// ROUND 32L, TASK 3.3 — GET /cluster, END TO END from a REAL worker's polling.
//
// The autoscaling row's NOTE recorded the gap exactly: "The /cluster fixtures still write
// polls/empty_polls DIRECTLY, so the API's AGGREGATION over several workers and the ring
// are proven separately, never end to end." Two halves that both pass while the WIRE
// between them is cut — nothing asserted that the numbers /cluster reports ever came from
// a worker that actually polled anything.
//
// This runs the real `Worker` loop against the live store, with nothing written by hand:
// the ring fills from real admissions, the real heartbeat copies it into
// `headgate_worker`, and the assertions read it back out of the HTTP response.
//
// THE DISCRIMINATING FACT is the ring BOUND. The worker below polls many hundreds of
// times; `POLL_WINDOW` is 128, so a rolling window reports 128 and a LIFETIME counter —
// which is what this was before round 32k, and what a regression would restore — reports
// the lifetime total. That difference is what an operator sees as "shrink the fleet"
// while the fleet is saturated.
//
// FLEET TOTALS AND CONCURRENT TESTS. `/cluster` sums every worker the store calls live
// (15 minutes), and sibling test binaries legitimately register their own. So the fleet
// assertions are an IDENTITY against the live set read at the same moment rather than an
// absolute number — and the per-worker assertions, which are the end-to-end claim, are
// scoped to this test's worker id. Nothing here deletes another test's row.
#[tokio::test]
async fn a_real_workers_polling_is_the_number_that_reaches_cluster() {
    use headgate::{Registry, Worker, WorkerConfig};
    use headgate_core::{Envelope, Task};

    let Ok(conninfo) = std::env::var("HG_TEST_PG") else {
        eprintln!("HG_TEST_PG not set; skipping cluster end-to-end test");
        return;
    };
    const WID: &str = "t33l-w1";
    const Q: &str = "t33l-q";

    struct Noop;
    impl Task for Noop {
        const TYPE: &'static str = "t33l:noop";
        fn encode(&self) -> Result<Vec<u8>, headgate_core::CodecError> {
            Ok(vec![])
        }
        fn decode(_: &[u8]) -> Result<Self, headgate_core::CodecError> {
            Ok(Noop)
        }
    }

    let store = Arc::new(PgStore::connect(&conninfo, 6).expect("connect"));
    {
        let tx = store.begin().await.unwrap();
        tx.client()
            .unwrap()
            .execute("DELETE FROM headgate_job WHERE queue = $1", &[&Q])
            .await
            .unwrap();
        tx.client()
            .unwrap()
            .execute("DELETE FROM headgate_worker WHERE worker_id = $1", &[&WID])
            .await
            .unwrap();
        tx.commit().await.unwrap();
    }
    let app = router(store.clone() as Arc<dyn Inspect>, ApiConfig::default());

    let mut reg = Registry::new();
    reg.register::<Noop, _, _>(|_ctx, _t| async move { Ok(()) })
        .unwrap();
    let cfg = WorkerConfig {
        queues: vec![Q.into()],
        capacity: 1,
        // heartbeat = lease / 3 = 100ms, so the ring reaches the registry promptly.
        lease: std::time::Duration::from_millis(300),
        poll: headgate::BackoffConfig {
            floor: std::time::Duration::from_millis(1),
            ceiling: std::time::Duration::from_millis(5),
            multiplier: 2.0,
            jitter: 0.0,
        },
        // This worker must not sweep other tests' jobs; it is here to POLL.
        run_duties: false,
        worker_id: Some(WID.into()),
        ..Default::default()
    };
    let (worker, handle) = Worker::new(store.clone(), reg, cfg);
    let running = tokio::spawn(worker.run());

    // A bounded wait on an OBSERVABLE store value, never a fixed sleep.
    async fn wait_worker(
        store: &Arc<PgStore>,
        wid: &str,
        secs: u64,
        cond: impl Fn(&headgate_core::WorkerMeta) -> bool,
    ) -> headgate_core::WorkerMeta {
        let deadline = tokio::time::Instant::now() + std::time::Duration::from_secs(secs);
        loop {
            let ws = store.list_workers(900_000).await.unwrap();
            if let Some(w) = ws.into_iter().find(|w| w.worker_id == wid) {
                if cond(&w) {
                    return w;
                }
            }
            assert!(
                tokio::time::Instant::now() < deadline,
                "worker {wid} never reached the condition"
            );
            tokio::time::sleep(std::time::Duration::from_millis(25)).await;
        }
    }

    // ---- PHASE 1: idle. The queue is empty, so every admission comes back with zero.
    let idle = wait_worker(&store, WID, 30, |w| w.polls >= 128).await;
    assert_eq!(
        idle.polls, 128,
        "the ring is BOUNDED at POLL_WINDOW and that bound must survive the trip into the \
         registry: this worker has polled far more than 128 times, so a lifetime counter \
         would report thousands here and tell an operator to shrink a fleet on ancient news"
    );
    assert_eq!(
        idle.empty_polls, 128,
        "an idle worker's whole window is empty polls"
    );

    // ...and the HTTP surface reports the fleet sum over the live set, mine included.
    let (st, body) = call(&app, Method::GET, "/api/v1/cluster", None).await;
    assert_eq!(st, StatusCode::OK);
    let live = store.list_workers(900_000).await.unwrap();
    let sum_polls: i64 = live.iter().map(|w| w.polls as i64).sum();
    let sum_empty: i64 = live.iter().map(|w| w.empty_polls as i64).sum();
    assert!(
        sum_polls >= 128,
        "the live set must include this worker's 128"
    );
    assert_eq!(
        body["polls_total"].as_i64().unwrap(),
        sum_polls,
        "polls_total is the SUM over live workers — the numbers real workers reported"
    );
    assert_eq!(body["empty_polls_total"].as_i64().unwrap(), sum_empty);
    // backlog metrics: a ratio of SUMS, never a mean of per-worker ratios.
    let want_ratio = sum_empty as f64 / sum_polls as f64;
    assert!(
        (body["empty_poll_ratio"].as_f64().unwrap() - want_ratio).abs() < 1e-9,
        "empty_poll_ratio must be empty_polls_total / polls_total; got {} want {want_ratio}",
        body["empty_poll_ratio"]
    );
    assert!(
        body["queues"]
            .as_array()
            .unwrap()
            .iter()
            .any(|q| q["queue"] == Q && q["live_workers"].as_i64().unwrap() >= 1),
        "the queue this real worker serves must show live coverage: {}",
        body["queues"]
    );

    // ---- PHASE 2: load it. Now the SAME ring must roll the empty polls out, and the
    // change must reach /cluster. A lifetime counter would still be reporting ~1.0 here,
    // which is the exact operational lie backlog metrics exists to prevent.
    let jobs: Vec<Envelope> = (0..300)
        .map(|i| {
            headgate::prepare_envelope(Envelope {
                id: format!("t33l-{i}"),
                kind: Noop::TYPE.into(),
                payload: vec![],
                queue: Q.into(),
                fingerprint: headgate_core::fingerprint(Noop::TYPE, format!("{i}").as_bytes()),
                scheduled_at_ms: 1,
                retention_ms: 0, // ephemeral: keep the table clean behind us
                ..Default::default()
            })
            .unwrap()
        })
        .collect();
    store.enqueue(&jobs).await.unwrap();

    let busy = wait_worker(&store, WID, 60, |w| w.empty_polls < 32).await;
    assert_eq!(
        busy.polls, 128,
        "the window stays exactly POLL_WINDOW wide under load — it rolls, it does not grow"
    );
    assert!(
        busy.empty_polls < 32,
        "a saturated worker's window must have rolled its idle bits out; got {}/{}",
        busy.empty_polls,
        busy.polls
    );
    let (st, busy_body) = call(&app, Method::GET, "/api/v1/cluster", None).await;
    assert_eq!(st, StatusCode::OK);
    assert!(
        busy_body["empty_polls_total"].as_i64().unwrap() <= sum_empty - 96,
        "the fleet's empty-poll total must fall by this worker's ~96 rolled-out bits: \
         idle={} busy={}",
        sum_empty,
        busy_body["empty_polls_total"]
    );

    handle.shutdown();
    let _ = running.await.expect("worker task");
}