antigen-macros 0.2.0

Procedural macros for the antigen crate. Not for direct use; use antigen instead.
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
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
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
//! Procedural macros for the antigen crate.
//!
//! This crate provides the five attribute macros that constitute the antigen
//! API surface:
//!
//! - [`#[antigen(...)]`](macro@antigen) — declare a named failure-class with a
//!   structural fingerprint (ADR-001, ADR-010)
//! - [`#[presents(...)]`](macro@presents) — mark code as exhibiting an antigen's
//!   structural pattern (vulnerability declaration)
//! - [`#[immune(...)]`](macro@immune) — declare immunity with a witness reference
//!   (test, proptest, phantom-type proof, or external-tool delegation)
//! - [`#[descended_from(...)]`](macro@descended_from) — propagate antigen markers
//!   through an inheritance chain (ADR-013, ADR-018 §propagation)
//! - [`#[antigen_tolerance(...)]`](macro@antigen_tolerance) — document an
//!   intentional opt-out with required rationale (ADR-011)
//!
//! ### Deferred-Defense Family (ADR-023)
//!
//! - [`#[anergy(...)]`](macro@anergy) — deferred-but-muted posture; `until`
//!   REQUIRED; aging escalation; loudness-as-discipline
//! - [`#[immunosuppress(...)]`](macro@immunosuppress) — surgical silencing
//!   with hard duration cap enforced at parse time
//! - [`#[poxparty(...)]`](macro@poxparty) — intentional exposure with
//!   structural compile-time isolation via `antigen-poxparty` feature flag
//! - [`#[orient(...)]`](macro@orient) — see-also context without antigen
//!   claim; lightest-weight deferred-defense primitive
//!
//! Users typically import these via the [`antigen`](https://docs.rs/antigen)
//! crate (`use antigen::{antigen, presents, immune, descended_from,
//! antigen_tolerance};`) rather than depending on `antigen-macros` directly.
//!
//! ## Design philosophy (v1)
//!
//! The macros are **mostly identity transformations**. Their job is to validate the
//! attribute syntax at compile time and pass the input through unchanged. The
//! semantic work — scanning the codebase, matching presentations against
//! immunities, validating witnesses — lives in the `cargo-antigen` tooling, which
//! parses source AST independently via `syn`.
//!
//! This keeps runtime overhead at zero (the macros generate no code beyond the
//! original input) and means antigen declarations don't affect compilation speed,
//! linker behavior, or binary size. The cost lives entirely at `cargo antigen scan`
//! time, which runs out-of-band.
//!
//! See ADR-010 (fingerprint grammar v1) and the project's `docs/expedition/`
//! directory for the design rationale.
//!
//! ## Known v1 limitations
//!
//! 1. **Macros are pure pass-through**: the proc-macros parse + validate the
//!    attribute syntax and emit the original item unchanged (ADR-001 identity
//!    transform). Cross-crate antigen discovery in v0.1.0-rc.1 works via
//!    source-walking the `.cargo/registry` tree (A3 sweep). A future ADR
//!    amendment may add metadata-emitting transforms (e.g.,
//!    `<!-- antigen:metadata:v1 {...} -->` doc-comment markers, or
//!    `#[cfg(doc)] pub static __ANTIGEN_META_*`) for the no-source-access
//!    case — verified-viable but post-A5 ADR territory per the A3 scope-lock.
//!
//! Span-aware error pointing (W4) and trybuild fixtures (A2 ratification)
//! both shipped in v0.1.0-rc.1.

use proc_macro::TokenStream;
use quote::quote;
use syn::parse_macro_input;

mod parse;

/// Process-global, monotonically-increasing emission counter for `#[immune]`.
///
/// Each invocation of `immune()` claims one counter value to incorporate into
/// its generated `const` name, ensuring no two `#[immune]` emissions in the
/// same compilation unit can share a name — regardless of antigen path or
/// stacking pattern (see `immune()` for the full rationale).
static IMMUNE_EMISSION_COUNTER: std::sync::atomic::AtomicUsize =
    std::sync::atomic::AtomicUsize::new(0);

/// Declare a named failure-class with a structural fingerprint.
///
/// # Arguments
///
/// - `name = "..."` (required) — kebab-case identifier for the failure-class
/// - `fingerprint = "..."` (optional; required for scan-locatable antigens) —
///   structural pattern (see ADR-010). Omit for verify-only antigens whose
///   detection-model is external-substrate (supply-chain / VCS-info-loss), per
///   ADR-009 Amendment 1 — they have no syn-scannable source surface.
/// - `family = "..."` (optional) — parent class, typically one of the 8
///   first-principles failure classes
/// - `summary = "..."` (optional) — human-readable description
/// - `references = [...]` (optional) — open-vocabulary list of references
///   (URLs, ADR/DEC IDs, CVE numbers, RFC numbers, etc.)
/// - `category = AntigenCategory::X` (optional) — `SubstrateAlignment` or
///   `FunctionalCorrectness` (ADR-028). **Do not import `AntigenCategory`** —
///   the macro reads this as a token path, so `use antigen::AntigenCategory;`
///   triggers `unused_imports` under `-D warnings`. Write the path directly
///   without importing.
///
/// # Examples
///
/// Layer 1 (minimum viable — just `name` and `fingerprint`):
///
/// ```ignore
/// use antigen::antigen;
///
/// #[antigen(
///     name = "panicking-in-drop",
///     fingerprint = "impl Drop with unwrap/expect/panic in body",
/// )]
/// pub struct PanickingInDrop;
/// ```
///
/// Layer 2 (enriched — adds `family`, `summary`, `references`):
///
/// ```ignore
/// #[antigen(
///     name = "panicking-in-drop",
///     family = "boundary-violation",
///     fingerprint = "impl Drop with unwrap/expect/panic in body",
///     summary = "Drop impls must not panic; double-panic causes process abort.",
///     references = ["https://doc.rust-lang.org/std/ops/trait.Drop.html#panics"],
/// )]
/// pub struct PanickingInDrop;
/// ```
///
/// The struct must be declared as a unit struct (no fields). The macro validates
/// the attribute arguments and passes the struct through unchanged.
#[proc_macro_attribute]
pub fn antigen(args: TokenStream, input: TokenStream) -> TokenStream {
    let args = parse_macro_input!(args as parse::AntigenArgs);
    let input = parse_macro_input!(input as syn::ItemStruct);

    if let Err(e) = args.validate() {
        return e.to_compile_error().into();
    }

    if !matches!(input.fields, syn::Fields::Unit) {
        return syn::Error::new_spanned(
            &input,
            "#[antigen] must be applied to a unit struct (e.g., `pub struct Name;`)",
        )
        .to_compile_error()
        .into();
    }

    // An antigen marker is a failure-class identity token: it carries no data
    // and no type-level parameterization. A generic marker (`struct Foo<T>;`)
    // is semantically meaningless — which failure-class does `Foo<T>` name? —
    // and would break the dead_code use-token below (a bare `let _x: Foo;`
    // reference needs type arguments, producing a cryptic E0107 pointing at the
    // declaration). Reject it here with a clear, on-point error instead.
    if !input.generics.params.is_empty() {
        return syn::Error::new_spanned(
            &input.generics,
            "#[antigen] must be applied to a non-generic unit struct; a \
             failure-class marker carries no type parameters",
        )
        .to_compile_error()
        .into();
    }

    let name_string = &args.name;
    let attr_doc = format!(
        " antigen `{name_string}` — declares a named failure-class.\n\n Use \
         `cargo antigen scan` to find sites presenting this antigen; \
         `cargo antigen audit` to validate witness coverage."
    );

    // DX finding 1: in a *binary* crate, `#[antigen] pub struct Foo;` trips
    // `dead_code` because `pub` does not exempt items with no external API
    // surface, and antigen uses the marker type as a declaration token, never
    // constructs it. Rather than `#[allow(dead_code)]` (which would also mask
    // legitimate dead-code on the item), emit a zero-cost use-token that makes
    // the type genuinely "used" from the compiler's view:
    //
    //   const _: fn() = || { let _x: Foo; };
    //
    // The `const _` is anonymous (no namespace pollution), is always compiled
    // (not `#[cfg(test)]`-gated, so it works under any conditional compilation),
    // and the closure body is never invoked — the binding only references the
    // type. Zero runtime cost; honours lib.rs's "pure pass-through with zero
    // overhead" contract at runtime while satisfying the dead_code analysis.
    let marker_ident = &input.ident;
    // The use-token references the type by bare name (`let _x: Foo;`). That is
    // safe here precisely because the generics check above already rejected any
    // parameterized marker — so `#marker_ident` always names a concrete,
    // arg-free type. (Adversarial flagged use-tokens-under-generics; the guard,
    // not an assumption about E0392, is what makes this sound.)
    let expanded = quote! {
        #[doc = #attr_doc]
        #input
        const _: fn() = || {
            let _antigen_use_token: #marker_ident;
        };
    };

    expanded.into()
}

/// Mark code as exhibiting a known antigen's structural pattern (vulnerability
/// declaration).
///
/// # Arguments
///
/// Single positional argument: the antigen type name.
///
/// # Example
///
/// ```ignore
/// use antigen::presents;
///
/// #[presents(PanickingInDrop)]
/// impl Drop for MyType {
///     fn drop(&mut self) { /* might panic */ }
/// }
/// ```
///
/// `cargo antigen scan` flags every `#[presents]` site that lacks a corresponding
/// `#[immune]` declaration. This declaration is the *vulnerability surface* — it
/// says "this code exhibits the structural pattern."
///
/// To express both vulnerability AND verified immunity, apply both attributes to
/// the same item:
///
/// ```ignore
/// #[presents(PanickingInDrop)]
/// #[immune(PanickingInDrop, witness = no_panic_test)]
/// impl Drop for SafeType { ... }
/// ```
#[proc_macro_attribute]
pub fn presents(args: TokenStream, input: TokenStream) -> TokenStream {
    let args = parse_macro_input!(args as parse::PresentsArgs);
    let input = proc_macro2::TokenStream::from(input);

    if let Err(e) = args.validate() {
        return e.to_compile_error().into();
    }

    // ADR-029 R5: a `requires = <predicate>` folded onto `#[presents]` emits the
    // same `antigen:requires:v1:<json>` doc marker `#[immune(requires=...)]`
    // does, so `cargo antigen scan` discovers the substrate-witness predicate at
    // the presents-site (the new substrate-tier carrier). A `proof = <expr>` is
    // recognized structurally by the audit from the written source (phantom-tier),
    // so it needs no marker.
    args.requires_json().map_or_else(
        || quote! { #input }.into(),
        |json| {
            let marker = format!(" antigen:requires:v1:{json}");
            quote! {
                #[doc = #marker]
                #input
            }
            .into()
        },
    )
}

/// Declare immunity to a known antigen, backed by evidence that proves it.
///
/// **Choosing `witness =` vs `requires =`**: can a test *execute* the thing you're defending?
/// If yes, use `witness =` (the code runs, so a test/proptest/proof/lint can verify it). If no
/// — the failure-class is about substrate state that code execution can't verify (a stale
/// document, an unpinned dependency, an un-reviewed discipline sign-off) — use `requires =`.
///
/// # Arguments
///
/// - The antigen type name (positional)
/// - **Exactly one of** `witness = ...` **or** `requires = ...` (mutually
///   exclusive; one is required):
///   - `witness = <ident>` — **code-tier** immunity. A reference to a test,
///     proptest, lint, formal-verification proof, or phantom-type construction
///     that proves immunity. Reach for this when the immunity is provable from
///     the code itself (the typical `FunctionalCorrectness` case).
///   - `requires = <predicate>` — **substrate-witness** immunity (ADR-019). A
///     predicate evaluated against a signed `.attest/` sidecar rather than the
///     code AST (e.g. `signers(...)`, `ratified_doc(...)`, `fresh_within_days(...)`).
///     Reach for this when the immunity evidence lives *outside* the code —
///     a review record, a ratified discipline doc, a sign-off (the typical
///     `SubstrateAlignment` case). See the `substrate_witness` example.
/// - `rationale = "..."` (optional) — human-readable description of why the
///   evidence applies
///
/// # Example
///
/// ```ignore
/// use antigen::immune;
///
/// // code-tier: a test proves it
/// #[immune(
///     PanickingInDrop,
///     witness = no_panic_in_drop_test,
///     rationale = "SafeType::drop uses Result-returning paths only.",
/// )]
/// impl Drop for SafeType { ... }
///
/// // substrate-witness: a ratified discipline doc records it
/// #[immune(
///     ParallelStateTrackersDiverge,
///     requires = ratified_doc(path = "docs/disciplines/state-reconciliation.md"),
/// )]
/// fn reconcile_state() { ... }
/// ```
///
/// For `witness =`, `cargo antigen scan` validates that the witness identifier
/// resolves to a real test/proptest/lint/proof; witnesses that don't exist or
/// don't run successfully invalidate the immunity claim. For `requires =`,
/// `cargo antigen audit` evaluates the predicate against the `.attest/` sidecar
/// — a substrate-witness sidecar is credited *only* for a `requires =` immunity,
/// never a `witness =` one.
///
/// `#[immune]` does not require `#[presents]` on the same item. Pre-emptive
/// immunity is acceptable: declaring immunity to ensure future modifications stay
/// covered, even when the current code doesn't structurally match the antigen.
#[proc_macro_attribute]
pub fn immune(args: TokenStream, input: TokenStream) -> TokenStream {
    let args = parse_macro_input!(args as parse::ImmuneArgs);
    let input = proc_macro2::TokenStream::from(input);

    if let Err(e) = args.validate() {
        return e.to_compile_error().into();
    }

    // ADR-029 §Mechanics: #[immune] is deprecated; emit a compiler warning pointing
    // toward the new #[defended_by] (code-tier) / #[presents(requires=...)]
    // (substrate-tier) model so adopters receive a migration nudge at compile time.
    //
    // Carrier choice: we do NOT emit `#[deprecated]` on the annotated item itself.
    // That design has two defects:
    //   1. Stacking — two `#[immune]` on one item produces two `#[deprecated]` attrs,
    //      which is a hard compile error ("multiple deprecated attributes").
    //   2. Target mis-match — `#[deprecated]` on the item fires at CALLERS, not at
    //      the `#[immune]` author; adopters writing valid code that calls a
    //      `#[immune]`-annotated function would see spurious migration warnings.
    //
    // Instead: emit a `const <NAME>: () = { ... }` item containing a deprecated unit
    // struct that is immediately used inside the block (firing the lint) and then
    // discarded. The block is scoped so no name leaks; the lint fires at the `#[immune]`
    // call site (the macro invocation), which is exactly where the author is.
    // Callers of the annotated item see no warning — only the #[immune] author does.
    // Antigen's own uses suppress with #[allow(deprecated)] per the migration plan.
    // MSRV 1.85 supports `let _` in const blocks.
    //
    // STACKABILITY (findings/immune-multi-stack-const-collision): the const item MUST
    // be NAMED, not anonymous (`const _`). The annotated item may live in an `impl`
    // block (the `#[immune]` is on a method), where the macro's emitted const lands in
    // ASSOCIATED-CONST position. Rust rejects `const _` there with TWO errors:
    //   - "`const` items in this context need a name" (anonymous const illegal in impl)
    //   - "duplicate definitions with name `_`" (E0592) when two are stacked
    // Empirically confirmed: two `const _: () = {…}` collide only in associated-const
    // position; at module scope they stack fine. The earlier "rename the inner struct"
    // fix was a red herring — it left `const _` in place, so it did NOT fix the impl
    // case (the errors are about the const's `_` name, not the inner struct). A NAMED
    // const is legal in module, impl, AND fn-body positions, and a per-emission-unique
    // name prevents the duplicate-definition collision even for the same antigen stacked
    // twice (e.g. a witness= immunity and a requires= immunity on one method).
    //
    // Uniqueness: antigen path (sanitized) + a process-global emission counter. The
    // counter guarantees uniqueness regardless of antigen path or stacking shape; the
    // path component keeps the generated name legible in errors/expansions. The counter
    // need only be unique within a single compilation (proc-macros run in-process), not
    // stable across builds — the const is discarded after firing the lint.
    let deprecated_note = "use #[defended_by] on tests (code-tier) or #[presents(requires=...)] \
         for substrate evidence — ADR-029";

    let antigen_suffix = args
        .antigen
        .segments
        .iter()
        .map(|seg| seg.ident.to_string())
        .collect::<Vec<_>>()
        .join("_");
    let antigen_suffix = if antigen_suffix.is_empty() {
        "Unknown".to_string()
    } else {
        antigen_suffix
    };
    // Process-global, monotonically-increasing per-emission discriminator
    // (see IMMUNE_EMISSION_COUNTER at module level for the full rationale).
    let n = IMMUNE_EMISSION_COUNTER.fetch_add(1, std::sync::atomic::Ordering::Relaxed);
    let const_name = format!("__ANTIGEN_IMMUNE_DEPRECATED_{antigen_suffix}_{n}");
    let const_ident = syn::Ident::new(&const_name, proc_macro2::Span::call_site());

    args.requires_json().map_or_else(
        || {
            quote! {
                #[allow(non_upper_case_globals)]
                const #const_ident: () = {
                    #[deprecated(note = #deprecated_note)]
                    struct AntigenImmuneDeprecated;
                    let _ = AntigenImmuneDeprecated;
                };
                #input
            }
            .into()
        },
        |json| {
            // Emit the predicate as a doc-attribute marker so `cargo antigen scan`
            // can discover it via source walking without requiring a binary link.
            // Format: `antigen:requires:v1:<json>` (ADR-019 §P3b).
            let marker = format!(" antigen:requires:v1:{json}");
            quote! {
                #[allow(non_upper_case_globals)]
                const #const_ident: () = {
                    #[deprecated(note = #deprecated_note)]
                    struct AntigenImmuneDeprecated;
                    let _ = AntigenImmuneDeprecated;
                };
                #[doc = #marker]
                #input
            }
            .into()
        },
    )
}

/// Propagate antigen markers from a parent function/type/method to a derived one.
///
/// # Arguments
///
/// Single positional argument: a path to the parent item.
///
/// # Example
///
/// ```ignore
/// use antigen::descended_from;
///
/// #[descended_from(crate::other_module::parent_function)]
/// fn refined_function(...) { ... }
/// ```
///
/// **v0.1 status**: `#[descended_from]` is recognized and parsed but
/// propagation is not yet implemented. In v0.1, this attribute compiles
/// cleanly and is recorded by `cargo antigen scan` for future use.
/// Chain-walking and marker propagation (`#[presents]` / `#[immune]`
/// inheritance with witness re-validation) arrive in A3.
#[proc_macro_attribute]
pub fn descended_from(args: TokenStream, input: TokenStream) -> TokenStream {
    let _args = parse_macro_input!(args as parse::DescendedFromArgs);
    let input = proc_macro2::TokenStream::from(input);

    quote! { #input }.into()
}

/// Register a code-tier witness: declare that this test/proptest function
/// defends against a known failure-class (ADR-029).
///
/// # Arguments
///
/// Single positional argument: the antigen type the witness defends.
///
/// # Example
///
/// ```ignore
/// use antigen::defended_by;
///
/// #[test]
/// #[defended_by(ParallelStateTrackersDiverge)]
/// fn bijection_audit_hints_const_matches_enum() {
///     // exercises both sides of the parallel state
/// }
/// ```
///
/// # Immunity is observed, not declared
///
/// `#[defended_by(X)]` is a *registration of evidence*, not a verdict. The test
/// declares **what it defends**; `cargo antigen audit` determines **whether it
/// defends it** — by cross-referencing the registered witness to the
/// `#[presents(X)]` sites it covers and grading the witness tier. No code site
/// ever claims "I am immune to X"; the audit tool is the single authoritative
/// voice that reports `defended` / `undefended` / `substrate-gap`. This is the
/// migration target for the code-tier (`witness = fn`) channel of the deprecated
/// `#[immune]` macro.
///
/// # Scope
///
/// `#[defended_by]` is for **code-tier witnesses only** — `#[test]` functions
/// and proptest properties. Site-attached evidence (a substrate predicate or a
/// phantom-type proof) folds into `#[presents]` via `requires =` / `proof =`,
/// not here (ADR-029 R5 discriminator: evidence belongs where it is).
///
/// Like the other antigen markers this is a pure identity transform plus a
/// discoverable `#[doc = " antigen:defended_by:v1:<antigen>"]` marker that
/// `cargo antigen scan` reads to register the witness.
#[proc_macro_attribute]
pub fn defended_by(args: TokenStream, input: TokenStream) -> TokenStream {
    let parsed = parse_macro_input!(args as parse::DefendedByArgs);
    let input = proc_macro2::TokenStream::from(input);

    // Emit a discoverable doc marker carrying the bare antigen type name, so
    // `cargo antigen scan` can register the witness via source-walking without
    // a binary link — the same channel `#[immune(requires=...)]` uses for its
    // predicate JSON (ADR-019 §P3b), here carrying just the failure-class name.
    let antigen_name = parsed
        .antigen
        .segments
        .last()
        .map_or_else(String::new, |s| s.ident.to_string());
    let marker = format!(" antigen:defended_by:v1:{antigen_name}");
    quote! {
        #[doc = #marker]
        #input
    }
    .into()
}

/// Mark a site as a deliberate, non-vulnerable match against an antigen's
/// fingerprint. Per ADR-011.
///
/// # Arguments
///
/// - The antigen type name (positional)
/// - `rationale = "..."` (required) — human-readable justification; empty
///   string is rejected
/// - `until = "..."` (optional) — expiry tag (e.g., `"v1.0"`); empty string
///   is rejected (per aristotle reciprocal Phase 1-8)
/// - `see = [...]` (optional) — open-vocabulary string array of references
///
/// # Example
///
/// ```ignore
/// use antigen::antigen_tolerance;
///
/// #[antigen_tolerance(
///     PolarityInvertedClassMeet,
///     rationale = "This test fixture deliberately constructs the failure \
///                  pattern to verify the witness catches it.",
///     until = "v1.0",
///     see = ["GAP-BIT-EXACT-1"],
/// )]
/// fn test_polarity_inversion_caught() { /* ... */ }
/// ```
///
/// `cargo antigen scan` recognizes tolerance markers as explicit
/// acknowledgments of fingerprint matches; tolerated sites are reported in
/// a separate category from unaddressed presentations.
#[proc_macro_attribute]
pub fn antigen_tolerance(args: TokenStream, input: TokenStream) -> TokenStream {
    let args = parse_macro_input!(args as parse::ToleranceArgs);
    let input = proc_macro2::TokenStream::from(input);

    if let Err(e) = args.validate() {
        return e.to_compile_error().into();
    }

    args.requires_json().map_or_else(
        || quote! { #input }.into(),
        |json| {
            let marker = format!(" antigen:requires:v1:{json}");
            quote! {
                #[doc = #marker]
                #input
            }
            .into()
        },
    )
}

// ============================================================================
// Deferred-Defense Family (ADR-023)
// ============================================================================

/// Declare an anergy posture: deferred-but-muted, with required time-bound
/// and aging escalation.
///
/// # Arguments
///
/// - Antigen type name (optional positional)
/// - `reason = "..."` (required) — minimum 20 characters
/// - `until = "YYYY-MM-DD"` (required) — expiry date; A5: `until` is not
///   optional; anergy without time-bound degrades to silent tolerance
/// - `expected_co_stimulation = "..."` (optional) — advisory-only; names the
///   condition that would re-engage immune response; NOT machine-verified
/// - `signed_by = "..."` (optional)
///
/// # Audit hints emitted by `cargo antigen audit`
///
/// - `anergy-active` — until has not passed
/// - `anergy-co-stimulation-not-arrived` — past `until` date; awaiting trigger
/// - `anergy-stale` — past `until` + grace period; escalates to warn/error
///
/// # Example
///
/// ```ignore
/// use antigen::anergy;
///
/// #[anergy(
///     MyFailureClass,
///     reason = "Upstream dependency ships v2 in Q4; immunity blocked on that upgrade.",
///     until = "2026-12-31",
///     expected_co_stimulation = "upstream-v2-upgrade-complete",
/// )]
/// pub fn depends_on_upstream() { /* ... */ }
/// ```
#[proc_macro_attribute]
pub fn anergy(args: TokenStream, input: TokenStream) -> TokenStream {
    let args = parse_macro_input!(args as parse::AnergyArgs);
    let input = proc_macro2::TokenStream::from(input);

    if let Err(e) = args.validate() {
        return e.to_compile_error().into();
    }

    quote! { #input }.into()
}

/// Declare surgical immunosuppression with a hard duration cap enforced at
/// parse time.
///
/// # Arguments
///
/// - Antigen type name (optional positional)
/// - `rationale = "..."` (required) — minimum 20 characters
/// - `until = "YYYY-MM-DD"` (required) — suppression deadline
/// - `since = "YYYY-MM-DD"` (optional) — suppression start; defaults to today
///   for cap calculation
/// - `duration_cap = N` (optional) — override cap in days; workspace default
///   is 90 days (ADR-023 `immunosuppress_duration_cap`)
/// - `signed_by = "..."` (optional)
///
/// # Parse-time enforcement (A4 absorbed)
///
/// A COMPILE ERROR is emitted if `until - since > duration_cap`. This closes
/// the audit-only gap — the cap cannot be bypassed by suppressing the audit.
///
/// # Audit hints
///
/// - `immunosuppress-active` — suppression current
/// - `immunosuppress-expired` — past `until`
/// - `immunosuppress-duration-cap-exceeded` — (should not occur post-compile;
///   retained for audit re-evaluation of pre-cap-enforcement code)
///
/// # Example
///
/// ```ignore
/// use antigen::immunosuppress;
///
/// #[immunosuppress(
///     MyFailureClass,
///     rationale = "CI matrix cannot run the verification harness until infra migration completes.",
///     until = "2026-09-01",
/// )]
/// pub fn ci_constrained_path() { /* ... */ }
/// ```
#[proc_macro_attribute]
pub fn immunosuppress(args: TokenStream, input: TokenStream) -> TokenStream {
    let args = parse_macro_input!(args as parse::ImmunosuppressArgs);
    let input = proc_macro2::TokenStream::from(input);

    if let Err(e) = args.validate() {
        return e.to_compile_error().into();
    }

    quote! { #input }.into()
}

/// Declare an intentional exposure exercise with structural isolation.
///
/// # Structural isolation (A3 — two-layer approach)
///
/// Primary isolation: wrap `#[poxparty]` sites inside a
/// `#[cfg(feature = "antigen-poxparty")]` module or item. When the feature
/// is inactive, `rustc` strips the block before proc-macro expansion runs,
/// so `#[poxparty]` never fires in production builds.
///
/// Secondary (best-effort): the macro checks `CARGO_FEATURE_ANTIGEN_POXPARTY`
/// at expansion time. This check is authoritative when Cargo propagates the
/// variable (some CI configurations and future Cargo versions). When not
/// propagated, the cfg gate provides the structural guarantee.
///
/// The `antigen-poxparty` feature MUST NOT be in the crate's default feature
/// set. `cargo antigen scan` emits `poxparty-outside-isolation` for any
/// `#[poxparty]` site found outside a cfg-gated context at audit time.
///
/// # Arguments
///
/// - Antigen type name (optional positional)
/// - `exercise_type = "..."` (required) — minimum 20 characters; describes
///   the controlled exposure exercise
/// - `until = "YYYY-MM-DD"` (required) — exercise deadline
/// - `name = "..."` (optional) — descriptive exercise name
/// - `rationale = "..."` (optional) — additional context
/// - `signed_by = "..."` (optional)
///
/// # Audit hints
///
/// - `poxparty-active` — exercise in progress
/// - `poxparty-outcome-pending` — past `until`; outcome not yet recorded
/// - `poxparty-outcome-recorded` — outcome attestation present
/// - `poxparty-outside-isolation` — site found outside cfg-gated scope
///   (should not occur if the compile-time check holds)
///
/// # Example
///
/// ```ignore
/// // In a module gated by #[cfg(feature = "antigen-poxparty")]:
/// use antigen::poxparty;
///
/// #[poxparty(
///     MyFailureClass,
///     exercise_type = "Fault injection: saturate the retry buffer to verify backpressure handling.",
///     until = "2026-10-01",
/// )]
/// pub fn chaos_test_retry_saturation() { /* ... */ }
/// ```
#[proc_macro_attribute]
pub fn poxparty(args: TokenStream, input: TokenStream) -> TokenStream {
    // A3 structural isolation — two-layer approach:
    //
    // Layer 1 (primary): `#[cfg(feature = "antigen-poxparty")]` on the
    // containing module/item. When the feature is inactive, `rustc` strips
    // the entire block before proc-macro expansion — `#[poxparty]` never
    // runs. This is the primary structural isolation mechanism. Callers
    // MUST wrap poxparty sites in a cfg gate.
    //
    // Layer 2 (env-var check, best-effort): Cargo sets CARGO_FEATURE_*
    // for build scripts but not reliably for proc-macro expansion in all
    // versions/configurations. We check the var as a secondary guard — it
    // fires when callers invoke the macro outside a cfg gate AND the var
    // happens to be absent. When Cargo IS propagating the var (e.g., some
    // CI configurations, or when the caller sets it explicitly), this
    // check is authoritative. When it isn't propagated, the cfg gate is
    // the load-bearing isolation.
    //
    // Per ADR-023 §Known limitations: "env-var propagation to proc-macro
    // expansion environment is Cargo-version-dependent; cfg gate is the
    // primary structural isolation."
    if std::env::var("CARGO_FEATURE_ANTIGEN_POXPARTY").is_err() {
        // Best-effort: emit a warning-level doc comment rather than a hard
        // compile error, since the env var may simply not be propagated.
        // The cfg gate is the primary structural check.
        // In environments where the var IS set (e.g., future Cargo versions
        // that propagate it, or explicit CI configuration), this would be a
        // compile error. For now, the scan-side `poxparty-outside-isolation`
        // hint provides the audit-time enforcement.
        //
        // INTENTIONAL: no compile error here — see above.
    }

    let args = parse_macro_input!(args as parse::PoxpartyArgs);
    let input = proc_macro2::TokenStream::from(input);

    if let Err(e) = args.validate() {
        return e.to_compile_error().into();
    }

    quote! { #input }.into()
}

// ============================================================================
// Convergent-Evidence Family (ADR-024)
// ============================================================================

/// Declare convergent multi-modality evidence backing a defense claim.
///
/// `#[diagnostic(modalities = [...], min_independent = N)]` asserts that
/// at least `N` distinct [`WitnessClass`](https://docs.rs/antigen) categories
/// converge on this defense. Per ADR-024 §Decision + adversarial C1, the
/// count is over distinct CLASSES, not raw witness count — running the
/// same kind of test in triplicate doesn't add evidence.
///
/// # Biology grounding
///
/// `#[diagnostic]` is grounded in **clinical medicine**, not immunology
/// proper. The metaphor is the diagnostic workup pattern from
/// differential-diagnosis literature: a clinician confirms a diagnosis
/// when independent modalities (history, physical, imaging, labs)
/// converge on the same finding. A single modality is suggestive; the
/// convergence is what carries clinical confidence. Per ADR-024 §Biology
/// grounding — dual-axis honesty, `#[diagnostic]` sits on the
/// clinical-medicine axis alongside `#[panel]`, `#[ddx]`, `#[rx]`,
/// `#[triage]`, `#[refer]`, `#[biopsy]`, `#[culture]`, `#[quarantine]`,
/// and `#[recurrence_anchor]`. The convergent-evidence family draws on
/// both immunology (clonal expansion, `IgG` class-switching) and clinical
/// medicine (diagnostic workup) — the dual axis is acknowledged
/// explicitly rather than collapsed.
///
/// # Arguments
///
/// - `modalities = [WitnessClass::X, ...]` (required) — non-empty list
/// - `min_independent = N` (required, > 0) — distinct-class floor; the
///   parser rejects `min_independent` exceeding the number of distinct
///   classes (vacuously unsatisfiable claim).
///
/// # Audit hints
///
/// - `diagnostic-modality-insufficient` — fewer modalities than the floor
/// - `diagnostic-modalities-class-collapsed` — all witnesses share one class
/// - `diagnostic-modalities-empty` — empty modalities list
///
/// # Example
///
/// ```ignore
/// use antigen::{antigen, diagnostic, WitnessClass};
///
/// #[diagnostic(
///     modalities = [WitnessClass::PropertyTest, WitnessClass::FormalVerification],
///     min_independent = 2,
/// )]
/// pub fn checked_arithmetic_sum(a: i64, b: i64) -> Option<i64> {
///     a.checked_add(b)
/// }
/// ```
#[proc_macro_attribute]
pub fn diagnostic(args: TokenStream, input: TokenStream) -> TokenStream {
    let args = parse_macro_input!(args as parse::DiagnosticArgs);
    let input = proc_macro2::TokenStream::from(input);
    if let Err(e) = args.validate() {
        return e.to_compile_error().into();
    }
    quote! { #input }.into()
}

/// Declare iterated witness evaluation (B-cell clonal expansion analog).
///
/// `#[clonal(witness = ..., iterations = N, seed = SeedKind::...)]`
/// asserts that a witness is run with many independent iterations.
/// Per ADR-024 §Decision + adversarial C2, `seed = SeedKind::Fixed(_)`
/// is a COMPILE ERROR — a fixed seed makes "independent iterations" a
/// contradiction.
///
/// # Arguments
///
/// - `witness = <ident>` (required) — per-iteration witness function
/// - `iterations = N` (required, > 0)
/// - `seed = SeedKind::X` (optional; default `Random`) — non-deterministic
///   variants accepted: `Random`, `EntropyFromCi`, `TimestampSeeded`.
///   `SeedKind::Fixed(_)` rejected at parse time.
///
/// # Audit hints
///
/// - `clonal-fixed-seed-detected` — parse-time error (above)
/// - `clonal-iterations-below-threshold` — N below workspace floor
///
/// # Example
///
/// ```ignore
/// use antigen::{clonal, SeedKind};
///
/// #[clonal(witness = sum_property, iterations = 10_000, seed = SeedKind::Random)]
/// pub fn checked_arithmetic_sum(a: i64, b: i64) -> Option<i64> {
///     a.checked_add(b)
/// }
/// ```
#[proc_macro_attribute]
pub fn clonal(args: TokenStream, input: TokenStream) -> TokenStream {
    let args = parse_macro_input!(args as parse::ClonalArgs);
    let input = proc_macro2::TokenStream::from(input);
    if let Err(e) = args.validate() {
        return e.to_compile_error().into();
    }
    quote! { #input }.into()
}

/// Declare IgG-class affinity-matured evidence (re-attestation history).
///
/// `#[igg(witnesses = [...], historical_span = N, min_reattestations = N)]`
/// asserts that the defense has been re-attested across a time span.
/// Per ADR-024 §Decision + adversarial C3, source-independence is
/// NOMINAL only — different signer identity strings are not structural
/// proof of independent sources.
///
/// # Arguments
///
/// - `witnesses = [...]` (required non-empty)
/// - `historical_span = N` (required, > 0; days)
/// - `min_reattestations = N` (required, > 0)
///
/// # Audit hints
///
/// - `igg-identity-collapse-warning` — same signer across reattestations
/// - `igg-span-too-short` — historical span below floor
/// - `igg-reattestations-insufficient` — fewer reattestations than floor
#[proc_macro_attribute]
pub fn igg(args: TokenStream, input: TokenStream) -> TokenStream {
    let args = parse_macro_input!(args as parse::IggArgs);
    let input = proc_macro2::TokenStream::from(input);
    if let Err(e) = args.validate() {
        return e.to_compile_error().into();
    }
    quote! { #input }.into()
}

/// Declare crossreactive coverage — one defense covers related antigens.
///
/// `#[crossreactive(fingerprints = [...])]` asserts that the annotated
/// item's defense applies to multiple antigen fingerprints simultaneously
/// (analogous to a crossreactive antibody binding related epitopes).
///
/// # Arguments
///
/// - `fingerprints = [...]` (required non-empty list of strings)
///
/// # Audit hints
///
/// - `crossreactive-fingerprint-unresolved` — fingerprint doesn't match
///   any known antigen
#[proc_macro_attribute]
pub fn crossreactive(args: TokenStream, input: TokenStream) -> TokenStream {
    let args = parse_macro_input!(args as parse::CrossreactiveArgs);
    let input = proc_macro2::TokenStream::from(input);
    if let Err(e) = args.validate() {
        return e.to_compile_error().into();
    }
    quote! { #input }.into()
}

/// Declare polyclonal evidence — many independent lineages converge.
///
/// `#[polyclonal]` is a marker primitive (no required args) declaring
/// that the defense rests on multiple independent witness lineages.
/// Distinct from `#[diagnostic]`: polyclonal emphasizes LINEAGE diversity
/// (different witness derivations) rather than MODALITY diversity
/// (different witness classes).
///
/// # Audit hints (planned — not yet emitted)
///
/// - `polyclonal-insufficient-lineages` — fewer lineages than a configured
///   floor. **Not implemented at v0.2**: `#[polyclonal]` is a pure
///   documentation marker today; the `PolyclonalInsufficientLineages`
///   `AuditHint` variant exists but is never produced (no lineage-counting
///   audit pass yet). This hint is a forward-plan, not current behavior — do
///   not rely on it firing.
#[proc_macro_attribute]
pub fn polyclonal(args: TokenStream, input: TokenStream) -> TokenStream {
    let args = parse_macro_input!(args as parse::PolyclonalArgs);
    let input = proc_macro2::TokenStream::from(input);
    if let Err(e) = args.validate() {
        return e.to_compile_error().into();
    }
    quote! { #input }.into()
}

/// Declare monoclonal evidence — single independent lineage.
///
/// `#[monoclonal]` is the structural contrast to `#[polyclonal]`. The
/// monoclonal posture is honest about resting on a single lineage; the
/// audit treats it as a documentary acknowledgement rather than a
/// failing.
#[proc_macro_attribute]
pub fn monoclonal(args: TokenStream, input: TokenStream) -> TokenStream {
    let args = parse_macro_input!(args as parse::MonoclonalArgs);
    let input = proc_macro2::TokenStream::from(input);
    if let Err(e) = args.validate() {
        return e.to_compile_error().into();
    }
    quote! { #input }.into()
}

/// Declare ADCC (antibody-dependent cellular cytotoxicity) — multi-
/// mechanism convergent defense.
///
/// `#[adcc]` asserts that the defense combines antibody-style witness
/// (declaration + check) AND cellular-effector witness (runtime
/// behavioral check) via different mechanisms. The marker primitive
/// surfaces the structural commitment to multi-mechanism defense.
///
/// # Audit hints (planned — not yet emitted)
///
/// - `adcc-single-mechanism-only` — only one of the two mechanisms
///   detectable on the site. **Not implemented at v0.2**: `#[adcc]` is a pure
///   documentation marker today; the `AdccSingleMechanismOnly` `AuditHint`
///   variant exists but is never produced (no mechanism-detection audit pass
///   yet). This hint is a forward-plan, not current behavior — do not rely on
///   it firing.
#[proc_macro_attribute]
pub fn adcc(args: TokenStream, input: TokenStream) -> TokenStream {
    let args = parse_macro_input!(args as parse::AdccArgs);
    let input = proc_macro2::TokenStream::from(input);
    if let Err(e) = args.validate() {
        return e.to_compile_error().into();
    }
    quote! { #input }.into()
}

// ============================================================================
// Recurrent-Emergence Family (ADR-024 + scientist HOW-spec cf2a2317 +
// aristotle Reading-A pre-authorization 744471a3)
//
// Six present-looking primitives: #[itch], #[recurrence_anchor],
// #[crystallize], #[chronic], #[saturate], #[strand]. Cognitive-organizational
// grounding for itch/saturate/crystallize/strand; immunology-proper for
// chronic; clinical-medicine for recurrence_anchor.
// ============================================================================

/// Declare a below-threshold noticing of a pattern (ADR-024 recurrent family).
///
/// `#[itch(name, antigen?, description, threshold?)]` marks a
/// cognitive-organizational observation: a pattern has been noticed but
/// has not yet crossed the threshold into a formal antigen declaration.
/// Per ADR-024 §Disambiguation: distinct from `#[anergy]` (ADR-023,
/// intentional non-defense while waiting) — itch is pre-commitment
/// noticing, anergy is deliberate defer.
///
/// # Biology grounding
///
/// **Cognitive-organizational** axis per ADR-024 §Biology grounding —
/// dual-axis honesty. Not an immunology-proper cognate; the metaphor is
/// drawn from how teams notice patterns before formalizing them.
///
/// # Arguments
///
/// - `name = "<slug>"` (required) — kebab-case identifier
/// - `antigen = <Path>` (optional) — failure-class path, if known
/// - `description = "..."` (required, ≥10 chars) — what is being noticed
/// - `threshold = "..."` (optional) — what would cause crystallize-promotion
///
/// # Audit hints
///
/// - `itch-noticed-not-anchored` — no antigen path; unlinked observation
#[proc_macro_attribute]
pub fn itch(args: TokenStream, input: TokenStream) -> TokenStream {
    let args = parse_macro_input!(args as parse::ItchArgs);
    let input = proc_macro2::TokenStream::from(input);
    if let Err(e) = args.validate() {
        return e.to_compile_error().into();
    }
    quote! { #input }.into()
}

/// Declare formal recognition of a cross-substrate recurrent failure-class
/// (ADR-024 recurrent family).
///
/// `#[recurrence_anchor(antigen, instances, since, rationale)]` commits to
/// formal recognition of a pattern that has crossed the substrate-evidence
/// threshold. Per ADR-024 §Disambiguation: distinct from `#[chronic]`
/// (low-level persistent, NOT cross-substrate) — `recurrence_anchor` is
/// cross-substrate-threshold-reached.
///
/// # Biology grounding
///
/// **Clinical-medicine** axis per ADR-024 §Biology grounding. Analogous
/// to a clinical diagnosis after recurrent symptoms cross the threshold
/// for formal recognition.
///
/// # Arguments
///
/// All four fields REQUIRED:
/// - `antigen = <Path>` — failure-class path being anchored
/// - `instances = N` (positive `u32`) — how many recurrences observed
/// - `since = "<date-or-version>"` — first detected instance anchor
/// - `rationale = "..."` (≥20 chars) — clinical-diagnosis-grade rationale
///
/// # Audit hints
///
/// - `recurrence-threshold-reached-no-action` — anchor declared but no
///   downstream `#[immune]`/`#[presents]` registered
#[proc_macro_attribute]
pub fn recurrence_anchor(args: TokenStream, input: TokenStream) -> TokenStream {
    let args = parse_macro_input!(args as parse::RecurrenceAnchorArgs);
    let input = proc_macro2::TokenStream::from(input);
    if let Err(e) = args.validate() {
        return e.to_compile_error().into();
    }
    quote! { #input }.into()
}

/// Declare the promotion event from itch-cluster to formal failure-class
/// (ADR-024 recurrent family).
///
/// `#[crystallize(name, from_itches?, antigen?, summary)]` records the
/// moment a pattern of noticings crystallizes into a formal antigen.
/// Parallel to camp's field-track `crystallize` verb.
///
/// # Biology grounding
///
/// **Cognitive-organizational** axis per ADR-024 §Biology grounding.
///
/// # Arguments
///
/// - `name = "<slug>"` (required)
/// - `from_itches = [Ident, ...]` (optional) — `#[itch]` idents this
///   crystallizes from
/// - `antigen = <Path>` (optional) — the formal antigen this crystallizes
///   into
/// - `summary = "..."` (required, ≥10 chars)
///
/// # Audit hints
///
/// - `crystallize-without-antigen` — crystallized but no formal antigen
///   path registered yet
#[proc_macro_attribute]
pub fn crystallize(args: TokenStream, input: TokenStream) -> TokenStream {
    let args = parse_macro_input!(args as parse::CrystallizeArgs);
    let input = proc_macro2::TokenStream::from(input);
    if let Err(e) = args.validate() {
        return e.to_compile_error().into();
    }
    quote! { #input }.into()
}

/// Declare a low-level persistent failure-class signal (ADR-024 recurrent
/// family).
///
/// `#[chronic(antigen, since, status?, managed_by?)]` marks a sustained
/// signal that has NOT crossed the cross-substrate-recurrence threshold
/// per ADR-024 §Disambiguation. Distinct from `#[recurrence_anchor]`.
///
/// # Biology grounding
///
/// **Immunology-proper** axis per ADR-024 §Biology grounding — chronic
/// inflammation is the biology cognate: sustained low-level immune
/// activity without acute recurrence.
///
/// # Arguments
///
/// - `antigen = <Path>` (required) — failure-class being marked chronic
/// - `since = "<date-or-version>"` (required) — when first observed
/// - `status = "..."` (optional) — current status description
/// - `managed_by = "..."` (optional) — team or role managing the state
///
/// # Audit hints
///
/// - `chronic-signal-unmanaged` — no `managed_by` after N versions
/// - `chronic-signal-past-review-date`
#[proc_macro_attribute]
pub fn chronic(args: TokenStream, input: TokenStream) -> TokenStream {
    let args = parse_macro_input!(args as parse::ChronicArgs);
    let input = proc_macro2::TokenStream::from(input);
    if let Err(e) = args.validate() {
        return e.to_compile_error().into();
    }
    quote! { #input }.into()
}

/// Declare a saturation-evidence contribution toward a recurrence threshold
/// (ADR-024 recurrent family).
///
/// `#[saturate(antigen?, contributing_to?, description)]` accumulates
/// evidence toward an anchor or itch without (yet) committing to either.
///
/// # Biology grounding
///
/// **Cognitive-organizational** axis per ADR-024 §Biology grounding.
///
/// # Arguments
///
/// - `antigen = <Path>` (optional)
/// - `contributing_to = "<slug>"` (optional) — `#[recurrence_anchor]` or
///   `#[itch]` slug this contributes to
/// - `description = "..."` (required, ≥10 chars)
///
/// # Audit hints
///
/// - `saturate-no-anchor` — no `contributing_to` target named
#[proc_macro_attribute]
pub fn saturate(args: TokenStream, input: TokenStream) -> TokenStream {
    let args = parse_macro_input!(args as parse::SaturateArgs);
    let input = proc_macro2::TokenStream::from(input);
    if let Err(e) = args.validate() {
        return e.to_compile_error().into();
    }
    quote! { #input }.into()
}

/// Declare a thread of related noticing across substrates (ADR-024
/// recurrent family).
///
/// `#[strand(name, anchored_by?, description)]` groups noticings that
/// share a structural rhyme but haven't yet crystallized. May spawn
/// `#[itch]` or `#[recurrence_anchor]` as the strand thickens.
///
/// # Biology grounding
///
/// **Cognitive-organizational** axis per ADR-024 §Biology grounding.
///
/// # Arguments
///
/// - `name = "<slug>"` (required)
/// - `anchored_by = [Ident, ...]` (optional) — `#[itch]` or
///   `#[recurrence_anchor]` idents this strand spans
/// - `description = "..."` (required, ≥10 chars)
///
/// # Audit hints
///
/// - `strand-no-anchors` — nothing anchors this strand
#[proc_macro_attribute]
pub fn strand(args: TokenStream, input: TokenStream) -> TokenStream {
    let args = parse_macro_input!(args as parse::StrandArgs);
    let input = proc_macro2::TokenStream::from(input);
    if let Err(e) = args.validate() {
        return e.to_compile_error().into();
    }
    quote! { #input }.into()
}

// ============================================================================
// Mucosal Boundary Family (ADR-027 + Amendment 1)
//
// Three primitives: #[mucosal], #[mucosal_delegate], #[mucosal_tolerant].
// MucosalKind sealed 13-variant set. Biology grounds the tier-claim + 4
// functional disciplines (NOT per-variant tissue mapping per ADR-027
// NON-NEGOTIABLE). Three response states: active defense / active tolerance
// / undecided — parallel to ADR-016 immune/tolerance/undeclared triad.
// ============================================================================

/// Declare a trust boundary is actively defended at this site (ADR-027).
///
/// `#[mucosal(kind = MucosalKind::X, rationale = "...")]` marks a function
/// as the defended boundary for a kind of data/control flow crossing the
/// trust surface.
///
/// # Biology grounding
///
/// Per ADR-027 §Biology grounding (NON-NEGOTIABLE): biology grounds the
/// TIER-CLAIM (mucosal surfaces are a distinct immune tier with selective
/// permeability) + the prevention-at-boundary discipline (secretory-IgA-style
/// exclusion). It does NOT ground per-variant tissue mapping — the
/// `MucosalKind` taxonomy is software-engineering scope-selection by
/// data-flow type, not anatomy.
///
/// # Arguments
///
/// - `kind = MucosalKind::X` (required) — the boundary type (one of 13
///   sealed-set variants)
/// - `rationale = "..."` (required, ≥20 chars) — why this boundary is
///   defended
///
/// # Audit hints
///
/// - `mucosal-boundary-undefended`, `mucosal-kind-mismatch`,
///   `mucosal-rationale-insufficient`
#[proc_macro_attribute]
pub fn mucosal(args: TokenStream, input: TokenStream) -> TokenStream {
    let args = parse_macro_input!(args as parse::MucosalArgs);
    let input = proc_macro2::TokenStream::from(input);
    if let Err(e) = args.validate() {
        return e.to_compile_error().into();
    }
    quote! { #input }.into()
}

/// Declare boundary discipline is delegated to a named handler (ADR-027 +
/// Amendment 1).
///
/// `#[mucosal_delegate(boundary = MucosalKind::X, handled_by = path::to::fn,
/// rationale = "...")]` declares that the boundary defense is performed by a
/// callee. Per ADR-027 Amendment 1 Change 4 `handled_by` is a path
/// expression (not a string) so typos fail at parse-time. Per Change 5 the
/// handler MUST carry a matching `#[mucosal(kind = X)]` — enforced at
/// audit-time via the three-tier diagnosis.
///
/// # Arguments
///
/// - `boundary = MucosalKind::X` (required) — the delegated boundary kind
/// - `handled_by = <path>` (required) — path to the handler function
/// - `rationale = "..."` (required, ≥20 chars)
///
/// # Audit hints (three-tier diagnosis per Change 5)
///
/// - `mucosal-discipline-delegate-target-missing` — handler path doesn't
///   resolve
/// - `mucosal-discipline-delegate-target-not-mucosal` — handler has no
///   `#[mucosal]`
/// - `mucosal-discipline-delegate-target-kind-mismatch` — handler's
///   `#[mucosal(kind)]` set doesn't include `boundary`
#[proc_macro_attribute]
pub fn mucosal_delegate(args: TokenStream, input: TokenStream) -> TokenStream {
    let args = parse_macro_input!(args as parse::MucosalDelegateArgs);
    let input = proc_macro2::TokenStream::from(input);
    if let Err(e) = args.validate() {
        return e.to_compile_error().into();
    }
    quote! { #input }.into()
}

/// Declare a boundary is INTENTIONALLY permitted — active tolerance, not
/// absence of defense (ADR-027 Amendment 1 Change 6).
///
/// `#[mucosal_tolerant(kind, rationale, accepts, reviewed_by?, until?)]`
/// declares that a boundary deliberately accepts input without the full
/// `#[mucosal]` defense discipline, and documents WHY that's acceptable.
/// Without this primitive, intentional-tolerance boundaries are
/// indistinguishable from undefended ones in `mucosal-map --undefended`.
///
/// # Biology grounding
///
/// Per ADR-027 Amendment 1: biology distinguishes THREE mucosal response
/// states — active defense (`#[mucosal]`), active tolerance
/// (`#[mucosal_tolerant]`), and undecided (no declaration). Active
/// tolerance is NOT absence of response — it is antigen-specific
/// Treg-mediated suppression with its own cellular machinery (oral
/// tolerance, fetal-maternal interface). Parallel to ADR-016
/// `#[antigen_tolerance]` but at the BOUNDARY tier rather than the
/// failure-class tier.
///
/// # Arguments
///
/// - `kind = MucosalKind::X` (required)
/// - `rationale = "..."` (required, **≥40 chars** — higher than
///   `#[mucosal]`'s ≥20; tolerance-errors are silent/latent — no acute
///   signal catches a bad tolerance decision, so the up-front declaration
///   must carry more justification to compensate for the detection asymmetry)
/// - `accepts = "..."` (required, non-empty) — what the boundary accepts
///   as legitimate input
/// - `reviewed_by = "..."` (optional v0.2; recommended v0.2.1+)
/// - `until = "<RFC-3339 date>"` (optional) — review deadline
///
/// # Audit hints
///
/// - `mucosal-tolerant-rationale-insufficient`, `mucosal-tolerant-accepts-empty`,
///   `mucosal-tolerant-past-review-date`, `mucosal-tolerant-without-reviewer`
///   (v0.2.1+ migration hint)
#[proc_macro_attribute]
pub fn mucosal_tolerant(args: TokenStream, input: TokenStream) -> TokenStream {
    let args = parse_macro_input!(args as parse::MucosalTolerantArgs);
    let input = proc_macro2::TokenStream::from(input);
    if let Err(e) = args.validate() {
        return e.to_compile_error().into();
    }
    quote! { #input }.into()
}

/// Declare an orientation period — a time-bounded acknowledged absence of immunity.
///
/// An acknowledged, time-bounded absence of immunity with an explicit path
/// forward. A loud deferred-defense primitive (ADR-023) — *not* the
/// lightest-weight one; loudness is the discipline.
///
/// `learning_path` and `until` are **both REQUIRED** (ADR-023 §Decision +
/// the Option-A hard-break ruling). An orient without an explicit path-out and
/// a time-bound is silent deferred non-immunity — structurally identical to
/// tolerance, which this primitive exists to be loudly distinct from. A bare
/// `#[orient]` is a compile error.
///
/// # Arguments
///
/// - Antigen type name (optional positional)
/// - `learning_path = "..."` (REQUIRED, ≥ 20 chars) — the explicit path out of
///   the learning period
/// - `until = "YYYY-MM-DD"` (REQUIRED) — orientation horizon; UTC, within
///   `deferred_defense_max_horizon` (180d). A date beyond that is a compile error.
///
/// The pre-restoration drift fields (`see`, `adr`, `attestation_optional`) were
/// removed — they were never in the ADR-023 spec. Fold any see-also context
/// into the `learning_path` text or `references = [...]` on the antigen
/// declaration.
///
/// # Audit hints
///
/// - `orient-active` — orientation in progress
/// - `orient-pending-action-required` — orientation past its `until` horizon
///
/// # Example
///
/// ```ignore
/// use antigen::orient;
///
/// #[orient(
///     PanickingInDrop,
///     learning_path = "Audit every Drop impl for unwrap/panic before the v1 tag",
///     until = "2026-09-01",
/// )]
/// pub fn new_subsystem_under_construction() { /* ... */ }
/// ```
#[proc_macro_attribute]
pub fn orient(args: TokenStream, input: TokenStream) -> TokenStream {
    let args = parse_macro_input!(args as parse::OrientArgs);
    let input = proc_macro2::TokenStream::from(input);

    if let Err(e) = args.validate() {
        return e.to_compile_error().into();
    }

    quote! { #input }.into()
}

/// Declare a rollback-as-triage commit: classify system state + commit to
/// rollback within a tight time-bound (ADR-026 §Rollback-as-triage).
///
/// Per aristotle's fixup-orient-dual-signature resolution (camp note
/// 55a161e7): `#[triage_commit]` is a SIBLING primitive to `#[orient]`, NOT
/// an extension. Orient names a failure-class with see-also context;
/// `triage_commit` names a triage decision + a rollback action. The two are
/// different speech acts in the deferred-defense family.
///
/// # Biology grounding — dual-axis honesty
///
/// The `#[triage_commit]` primitive carries DUAL-AXIS grounding per ADR-026
/// §Finding (NON-NEGOTIABLE per naturalist); neither axis is decorative.
///
/// **Clinical-medicine axis grounds the OUTCOME**: triage as a discipline
/// comes from clinical emergency-response medicine — the practice of
/// classifying patients by acuity before deciding treatment order. The
/// 5-color taxonomy (Black/Red/Yellow/Green/White) rhymes with clinical
/// field-triage protocols (e.g., START — Simple Triage And Rapid
/// Treatment), but `#[triage_commit]` is not a clinical-medicine
/// implementation: the rollback-as-treatment use-case extends the
/// protocol's shape with software-specific cases (`White` for non-incident
/// triages, no clinical analog). Clinical-medicine grounds the
/// COMMIT-DECISION-BEFORE-ACTION discipline: informed consent + chart
/// documentation precede the procedure; structurally isomorphic to
/// triage-commit-before-rollback.
///
/// **Software-engineering axis grounds the PROCESS**:
/// rollback-as-mandated-by-triage-tag is software-engineering invention.
/// Immune biology has NO analog to "log rationale before acting" (per
/// ADR-026 §Finding). The `triaged_by` + `rationale` +
/// `rollback_due_within_minutes` fields operate at the
/// software-engineering tier; their structural enforcement (parse-time
/// validation, audit-time substrate-witness via git-trailer per ADR-019)
/// is software-engineering machinery composed under the clinical-medicine
/// outcome framing.
///
/// **What biology DOES ground (Class 1, outcome-level)**: the
/// `ForcePushErasingHistory` ↔ Immune Amnesia (measles) cognate (ADR-026
/// §Finding) is the central immune-biology grounding for the broader
/// VCS-info-loss family — catastrophic loss of memory-carrying substrates
/// with documented harm. `#[triage_commit]` is the prescribed defense at
/// the rollback boundary: biology predicts that memory-loss requires
/// structural defense; clinical-medicine prescribes the form
/// (triage-decision documented before action).
///
/// This is the same dual-axis honesty ADR-024 ratified for the
/// temporal-arc families — antigen draws from MULTIPLE grounding
/// disciplines, naming which axis grounds which property. Overclaim "this
/// is immune biology" would be dishonest; underclaim "this is decorative"
/// would lose the predictive power. Dual-axis is the right shape.
///
/// # Arguments
///
/// All five fields are REQUIRED per ADR-026 §Decision:
///
/// - `triage_decision = TriageDecision::X` — five-color triage classification
///   (one of `Black`, `Red`, `Yellow`, `Green`, `White`). See
///   [`antigen::vcs::TriageDecision`](https://docs.rs/antigen) for variant
///   semantics.
/// - `rollback_target = "<sha>"` — commit sha pointing to the last-known-good
///   state. Non-empty.
/// - `triaged_by = "<role|name>"` — informed-consent author identity (role
///   slug like `"navigator"` or a personal name). Non-empty.
/// - `rationale = "..."` — chart-documentation; minimum 20 characters per
///   ADR-023 loudness-as-discipline applied to clinical-medicine
///   chart-documentation. Records WHY the rollback was decided before the
///   action commits.
/// - `rollback_due_within_minutes = N` — tight time-bound (positive `u32`).
///   Carries the discipline that triage-commits are followed by action in
///   bounded time; a zero deadline degrades the loudness pattern.
///
/// # Audit hints
///
/// - `vcs-rollback-without-triage-commit` — a rollback commit not preceded by
///   a `#[triage_commit]` declaration with `Triage-Decision: <sha>` trailer
/// - `vcs-rollback-due-window-exceeded` — `rollback_due_within_minutes`
///   elapsed without the rollback commit landing
///
/// # Example
///
/// ```ignore
/// use antigen::{triage_commit, TriageDecision};
///
/// #[triage_commit(
///     triage_decision = TriageDecision::Red,
///     rollback_target = "abc1234",
///     triaged_by = "navigator",
///     rationale = "vital metric regression confirmed via #84; rolling back to last-known-good",
///     rollback_due_within_minutes = 30,
/// )]
/// fn _triage_marker_do_not_remove() {}
/// // Followed by rollback commit with trailer:
/// //   Triage-Decision: <sha-of-this-triage-commit-marker>
/// ```
#[proc_macro_attribute]
pub fn triage_commit(args: TokenStream, input: TokenStream) -> TokenStream {
    let args = parse_macro_input!(args as parse::TriageCommitArgs);
    let input = proc_macro2::TokenStream::from(input);

    if let Err(e) = args.validate() {
        return e.to_compile_error().into();
    }

    quote! { #input }.into()
}