lean-ctx 3.9.18

Context Runtime for AI Agents with CCP. 71 MCP tools, 10 read modes, 95+ compression patterns, cross-session memory (CCP), persistent AI knowledge with temporal facts + contradiction detection, multi-agent context sharing, LITM-aware positioning, AAAK compact format, adaptive compression with Thompson Sampling bandits. Supports 24+ AI tools. Reduces LLM token consumption by up to 99%.
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
//! Canonical rules source — single source of truth for all lean-ctx guidance.
//!
//! All content is declared as `pub const` at the top. Three profiles (LONGFORM,
//! FULL, COMPACT) define which sections compose each output format. Four
//! wrappers (Longform, Dedicated, Shared, Bare) select the profile and wrapping
//! style. One `render()` function assembles everything, including the
//! compression-level output-style prompt (Lite / Standard / Max).
//!
//! Profile economics (#578): every *injected* file bills its tokens on every
//! single session, so FULL (dedicated rule files) and COMPACT (shared files +
//! MCP instructions) stay tight. LONGFORM is carried only by the project
//! `LEAN-CTX.md`, which agents open on demand — it keeps the verbose teaching
//! sections (loop taxonomy, navigation paradox, recovery vocabulary, CEP).
//!
//! ***Every*** template, injected rule file, AGENTS.md block, and MCP
//! instructions field MUST derive its content from this module.

use crate::core::config::CompressionLevel;

/// Stable HTML-comment anchor that marks the start of any lean-ctx rule
/// section.  Never changes — used for find/replace in shared files and for
/// ownership detection in dedicated files.  The version number follows on the
/// next line (see `render`).
pub const START_MARK: &str = "<!-- lean-ctx-rules -->";

/// Prefix shared by every lean-ctx rules marker including legacy versioned
/// formats (`<!-- lean-ctx-rules-v9 -->`). Use for substring detection when
/// the exact constant would miss older installs.
pub const RULES_MARKER_PREFIX: &str = "<!-- lean-ctx-rules";

/// Start marker for lightweight AGENTS.md/CODEBUDDY.md/CLAUDE.md pointer
/// blocks. These are deliberately separate from `START_MARK` / `<!-- lean-ctx-rules -->`
/// because the pointer-only vs full-rules distinction drives duplicate detection
/// in `doctor overhead` — a pointer-only file (`is_pointer_only`) must not be
/// counted as a second source for its client.
pub const AGENTS_BLOCK_START: &str = "<!-- lean-ctx -->";

/// End marker for AGENTS.md/CODEBUDDY.md/CLAUDE.md pointer blocks.
pub const AGENTS_BLOCK_END: &str = "<!-- /lean-ctx -->";

/// Owner banner placed as the first line of the project-level `LEAN-CTX.md`
/// artifact (`<repo>/LEAN-CTX.md`, `rust/LEAN-CTX.md`). Marks the whole file as
/// lean-ctx-owned so uninstall can remove it wholesale; the writer
/// (`hooks::ensure_project_agents_integration`), the regenerator
/// (`gen_rules` example) and the drift gate all share this one literal.
pub const PROJECT_LEAN_CTX_OWNED_MARKER: &str = "<!-- lean-ctx-owned: PROJECT-LEAN-CTX.md v1 -->";

/// Closing marker that ends a lean-ctx rule section.
pub const END_MARK: &str = "<!-- /lean-ctx-rules -->";

/// Markers of the heavy compression / output-style block — the per-turn payload
/// that drives cross-channel duplication (#684/#548).
///
/// `render()` wraps the compression prompt in these markers for **persistent
/// carriers** (the `Dedicated` and `Shared` wrappers, i.e. every injected rule
/// file). This is the single carrier/marker model: coverage and dedup
/// (`core::rules_channel`, `cli::rules_dedup`) detect and thin the payload by
/// these markers, so the writer and the readers can never disagree again. The
/// ephemeral `Bare` MCP-instructions channel deliberately omits the markers —
/// its inclusion is *governed* by carrier coverage (`client_autoloads_compression`),
/// so a per-session marker would be pure noise.
pub const COMPRESSION_BLOCK_START: &str = "<!-- lean-ctx-compression -->";

/// Closing marker of the compression / output-style block (see
/// [`COMPRESSION_BLOCK_START`]).
pub const COMPRESSION_BLOCK_END: &str = "<!-- /lean-ctx-compression -->";

/// Current rules version (monotonically increasing integer).  Embedded as
/// `<!-- version: {RULES_VERSION} -->` right after `START_MARK` so the
/// injection layer can parse it and decide whether a file is up-to-date.
///
/// History: v3 (#609) adds the `AGENT_LOOP` taxonomy + `NAV_PARADOX` guidance to
/// the FULL profile and a compact one-liner to COMPACT. v4 adds recovery guidance
/// so agents learn the (MCP-optional) decompression paths proactively instead of
/// re-reading compressed output line-by-line. v5 (#578) is the self-footprint
/// diet: the injected FULL profile drops to ~470 tokens (the intent playbook
/// absorbs the loop taxonomy and navigation paradox; the verbose
/// `AGENT_LOOP`/`NAV_PARADOX`/`RECOVER`/`CEP` sections move to the LONGFORM
/// profile that only the on-demand project `LEAN-CTX.md` carries).
/// v6: HookCovered profile strengthened — ctx_compose-first priority, imperative
/// session memory requirement, ctx_callgraph/ctx_search(semantic) promoted from
/// optional to mandatory. Fixes agents falling back to native Read+Grep for
/// exploration on hook-covered hosts (Cursor).
/// Bumping it forces every committed `LEAN-CTX.md` artifact to be regenerated
/// (see `tests/rules_drift.rs`) and every injected user file to resync.
pub const RULES_VERSION: usize = 8;

/// Banner placed at the top of dedicated rule files (non-shadow only).
pub const CRITICAL: &str = "CRITICAL: ALWAYS use lean-ctx ctx_* tools instead of native equivalents. \
     This is NOT optional.";

/// Weak-model adherence nudge (#1067 / GH #593). Smaller models (e.g. GLM 5.2 in
/// Windsurf) often read the tool-mapping rules yet still answer from memory or
/// reach for a built-in tool instead of emitting an actual MCP call — so
/// `lean-ctx watch` stays empty and the layer looks "off". This one imperative
/// line makes the *act of invoking* a ctx_* tool the explicit first step. It
/// lives in the dedicated rule files of *every* agent (`FULL_NON_SHADOW`), which
/// is what #1067 asks for; the Windsurf project template (`windsurfrules.txt`)
/// carries its own copy. It is omitted where it would be dead weight: shadow
/// mode enforces routing at the call layer, and the `Bare`/instructions channel
/// is capped separately.
pub const MUST_INVOKE: &str = "ACTUALLY EMIT the ctx_* tool call (ctx_compose first) — \
    describing a tool is not calling it.";

/// Compact bullet-list mapping (for both dedicated and shared contexts).
pub const BULLETS: &str = "\
MANDATORY MAPPING:\n\
• Read/cat -> ctx_read(path, mode)\n\
• Grep -> ctx_search(pattern, path)\n\
• Shell/bash -> ctx_shell(command)\n\
• Glob/find -> ctx_glob(pattern)\n\
• ls/find -> ctx_tree(path, depth)";

/// One-line admonition reinforcing the tool preference.  Always right after
/// `BULLETS` in both profiles.
pub const NEVER: &str = "NEVER use native Read/Grep/Shell/Glob when a ctx_* equivalent \
    exists. SELF-CORRECT: the moment you reach for one, stop and call the ctx_* tool instead.";

/// Intent-to-tool playbook — maps common agent questions to the right tool.
/// Since v5 it also absorbs the agent-loop phases (#609) and the navigation
/// paradox one-liner, replacing the separate verbose sections in the injected
/// profile (they stay verbatim in LONGFORM).
pub const INTENT: &str = "\
Tool selection by intent:\n\
• Orient / understand code (call FIRST) -> ctx_compose\n\
• Read a file -> ctx_read(path, mode=signatures|map|full); edit after reading -> ctx_patch\n\
• Exact symbol -> ctx_search(action=symbol); pattern -> ctx_search; by meaning -> ctx_search(action=semantic)\n\
• Files by glob -> ctx_glob; structure -> ctx_tree; callers/impact -> ctx_callgraph\n\
• Verify after edits -> ctx_shell(test/build); memory -> ctx_session / ctx_knowledge\n\
Semantic questions -> search tools, not whole-file reads: reading more ≠ understanding more.";

/// Anti-patterns that waste tokens and round-trips.
pub const ANTI: &str = "\
Anti-patterns — do NOT:\n\
• Chain ctx_search -> ctx_read -> ctx_search(action=symbol) — one ctx_compose replaces all three\n\
• Use ctx_read(mode=full) for orientation — use mode=signatures\n\
• Use ctx_callgraph/ctx_graph for const/static/variable refs — they track call\n\
  edges and file deps only; use ctx_search instead";

/// Encourages parallel tool calls to reduce round-trips.
pub const PARALLEL: &str = "\
PARALLEL: fire independent tool calls in the SAME turn — ctx_compose bundles \
multiple lookups into one call.";

/// Agent-loop tool taxonomy (#609). Names each phase of the gather → act →
/// verify loop an agent actually runs in and the one lean-ctx tool that serves
/// it. Since v5 (#578) LONGFORM-only — the injected profiles carry the phases
/// folded into `INTENT`.
pub const AGENT_LOOP: &str = "\
AGENT LOOP (phase -> tool):\n\
• Orient — understand before acting -> ctx_compose\n\
• Find — exact symbol by name -> ctx_search(action=symbol)\n\
• Read — a file, structurally -> ctx_read(mode=signatures|map)\n\
• Locate — a pattern across files -> ctx_search\n\
• Trace — callers / callees / blast radius -> ctx_callgraph\n\
• Verify — after an edit -> ctx_shell(test/build) + native lints";

/// Navigation-paradox guidance (#609): reading more is not understanding more.
/// Steers semantic questions to BM25 + meaning search and reserves the call/dep
/// graph for genuinely hidden architectural edges. Since v5 LONGFORM-only —
/// `INTENT` carries the one-line thesis in the injected profiles.
pub const NAV_PARADOX: &str = "\
NAVIGATION PARADOX: reading more ≠ understanding more.\n\
• Semantic question (\"where/how is X handled?\") -> ctx_search (BM25) + ctx_search(action=semantic) (meaning), not whole-file reads\n\
• Hidden architectural deps (who calls this, what breaks) -> ctx_callgraph / ctx_graph — for these only\n\
• Navigate structure (signatures, symbols) before reading entire files";

/// One-line automation reminder.
pub const AUTO: &str = "Long shell jobs: ctx_shell(run_in_background=true), then poll job_id. \
    ctx_session=memory; full guide: LEAN-CTX.md";

/// Recovery vocabulary (verbose, LONGFORM profile). lean-ctx compression is fully
/// reversible (CCR), but agents otherwise only discover the escape hatch reactively
/// from output hints — so they re-read compressed files line-by-line instead of
/// expanding (the "too compressed" complaint). The MCP-free path ("read the shown
/// file path with any tool") covers orgs that forbid MCP. Since v5 every injected
/// profile (FULL + COMPACT/Bare) carries the terser [`RECOVER_COMPACT`] instead;
/// the reactive footers in `ctx_read`/`archive`/`ctx_shell` still teach the
/// `ctx_expand` path in context.
pub const RECOVER: &str = "RECOVER: compressed output is reversible — never re-read line-by-line. \
    Need full/exact? Read the shown file path with any tool (no MCP), or \
    ctx_read(mode=full|raw=true); [Archived]/tee/firewall → ctx_expand(id=...).";

/// Terse injected variant of [`RECOVER`] (FULL + COMPACT/Bare). The cold
/// first-contact handshake renders the COMPACT profile, so this one-liner keeps
/// the static char/token budget (`tests/intensive_benchmarks.rs`,
/// `instructions.rs`); since v5 the FULL dedicated files carry it too (#578).
/// Keeps the two primary MCP-optional paths and the "never line-by-line" rule.
/// Must keep the `(no MCP)` clause (asserted in tests).
pub const RECOVER_COMPACT: &str = "RECOVER: compression is reversible — read the shown path \
    (no MCP) or ctx_read(raw=true), never re-read line-by-line.";

/// Context Engineering Protocol version reference.
pub const CEP: &str = "CEP v1: 1.ACT FIRST 2.DELTA ONLY (Fn refs) 3.STRUCTURED (+/-/~) \
     4.ONE LINE PER ACTION 5.QUALITY ANCHOR";

/// LITM end-of-instructions preference line.
pub const LITM_END: &str = "TOOL PREFERENCE (END): ctx_compose>chain ctx_read>Read ctx_shell>Shell \
     ctx_search>Grep ctx_glob>Glob ctx_tree>ls | Edit/Write/Delete=native";

/// Minimal rules body for shadow mode (#963). Under shadow-mode interception
/// native Read/Grep/Shell/Glob calls are transparently routed to ctx_*, so the
/// tool-mapping and "use ctx_* instead of native" guidance is dead weight — the
/// enforcement happens at the call layer, not in the prompt. Only the lean-ctx
/// tools that have *no* native trigger to intercept still need advertising.
pub const SHADOW_MINIMAL: &str = "\
lean-ctx shadow mode: native read/search/shell calls auto-route to ctx_* — no tool-mapping needed.\n\
File editing → native Edit/StrReplace (lean-ctx only handles reads); if denied, use ctx_patch.\n\
Exclusive tools (no native trigger): ctx_compose (understand code, call first), ctx_search(action=symbol) (exact symbol), ctx_search(action=semantic) (by meaning), ctx_callgraph (callers), ctx_knowledge / ctx_session (memory).";

/// Hook-covered header (GL #1153): the honest replacement for the
/// `CRITICAL`/`BULLETS`/`NEVER` mapping on hosts whose *installed hooks*
/// already compress the native tools (Cursor: `preToolUse` rewrite covers
/// Shell, redirect covers Read/Grep). There a "NEVER use native tools" rule
/// fights the host's own tool guidance and is unenforceable — the model calls
/// native tools anyway and the hooks compress them transparently. Saying so
/// removes the instruction dissonance instead of losing the battle silently.
pub const HOOK_COVERED_HEADER: &str = "\
CRITICAL: ALWAYS prefer lean-ctx ctx_* tools over native equivalents — ctx_* tools \
provide superior caching, compression, and session memory. Hooks compress native \
Shell/Read/Grep as fallback, but direct ctx_* calls are the primary path.\n\
ACTUALLY EMIT the ctx_* tool call (ctx_compose first) — describing a tool is not calling it.\n\
WHY ctx_read > native Read: ctx_read picks the optimal mode (map/signatures/terse) \
per file, caches for instant re-reads (~13 tokens), and compresses 26-92%. Native \
Read through hook redirect is limited to verbatim pass-through (~5% savings).";

/// The tools worth an explicit MCP call on a hook-covered host: capabilities
/// with *no* native equivalent the hooks could intercept. Kept in sync with
/// [`SHADOW_MINIMAL`]'s exclusive-tools line (same rationale, different cause).
pub const HOOK_COVERED_TOOLS: &str = "\
MANDATORY MAPPING (always use ctx_* instead of native):\n\
• Read/cat -> ctx_read(path, mode) — cached, 10 modes, re-reads ~13 tokens\n\
• Grep/search -> ctx_search(pattern, path) — also action=symbol|semantic for definitions/meaning\n\
• Shell/bash -> ctx_shell(command) — 95+ compression patterns\n\
• ctx_compose — orient in code FIRST (bundles search + read + symbols) — call before editing/debugging\n\
• ctx_callgraph — callers, callees, blast radius — use instead of manual file reading\n\
• ctx_session / ctx_knowledge — persistent memory — record decisions & progress after milestones\n\
• ctx_expand — recover full text from [Archived]/compressed output";

// ── Output-style compression prompts ───────────────────────────

/// Lite compression — concise, bullet-point output.
pub const LITE_PROMPT: &str = "\
OUTPUT STYLE: concise
- Bullet points over paragraphs
- Skip filler words and hedging (\"I think\", \"probably\", \"it seems\")
- 1-sentence explanations max, then code/action
- No repeating what the user said";

/// Standard compression — dense, atomic fact lines, abbreviations.
pub const STANDARD_PROMPT: &str = "\
OUTPUT STYLE: dense
- Each statement = one atomic fact line
- Use abbreviations: fn, cfg, impl, deps, req, res, ctx, err, ret
- Diff lines only (+/-/~), never repeat unchanged code
- Symbols: → (causes), + (adds), − (removes), ~ (modifies), ∴ (therefore)
- No narration, no filler, no hedging
- BUDGET: ≤200 tokens per response unless code block required";

/// Max compression — expert-terse, telegraph format, symbolic vocabulary.
pub const MAX_PROMPT: &str = "\
OUTPUT STYLE: expert-terse
- Telegraph format: subject-verb-object, drop articles/prepositions
- Symbolic vocabulary: → cause, ∵ because, ∴ therefore, ⊕ add, ⊖ remove, Δ change, ≈ similar, ≠ different, ∈ in/member, ∅ empty/none, ✓ ok, ✗ fail
- Code blocks: untouched (never compress code syntax)
- Each line: max 80 chars
- Zero narration, zero filler
- BUDGET: ≤100 tokens per non-code response";

/// Raw compression — densest possible output. Bullet points only, zero prose,
/// diff-style facts, no intro/outro. Tighter than Max (#795).
pub const RAW_PROMPT: &str = "\
OUTPUT STYLE: raw-dense
- Bullet points ONLY, zero prose, no intro, no outro, no greetings
- Diff-style facts: +added, -removed, ~changed, !breaking
- One fact per line, max 60 chars
- Symbolic: → ∵ ∴ ⊕ ⊖ Δ ≈ ≠ ∈ ∅ ✓ ✗ (same as expert-terse)
- Code blocks: untouched
- BUDGET: ≤50 tokens per non-code response
- NEVER explain what you did — show only the result";

/// Return the compression prompt text for a given level (empty string for Off).
pub fn compression_text(level: CompressionLevel) -> &'static str {
    match level {
        CompressionLevel::Off => "",
        CompressionLevel::Lite => LITE_PROMPT,
        CompressionLevel::Standard => STANDARD_PROMPT,
        CompressionLevel::Max => MAX_PROMPT,
        CompressionLevel::Raw => RAW_PROMPT,
    }
}

// The verbose teaching profile — only the on-demand project `LEAN-CTX.md`
// Static profile arrays removed (#756): replaced by the profile-aware
// section-builder functions below (longform_non_shadow_sections, etc.).

// ── Profile-aware section assembly (#756) ──────────────────────
//
// Each function returns Vec<String> with the same section ordering as the
// static arrays above, but replaces tool-referencing constants with their
// dynamic equivalents from `rules_sections`.

fn s(c: &str) -> String {
    c.to_string()
}

fn longform_non_shadow_sections(p: &super::tool_profiles::ToolProfile) -> Vec<String> {
    use super::rules_sections as rs;
    let mut v = vec![
        s(CRITICAL),
        s(MUST_INVOKE),
        s(BULLETS),
        s(NEVER),
        rs::intent_section(p),
        s(AGENT_LOOP), // verbose teaching — LONGFORM only
        rs::anti_section(p),
        s(NAV_PARADOX), // verbose teaching — LONGFORM only
        s(PARALLEL),
        s(AUTO),
        s(RECOVER),
        s(CEP),
        rs::litm_end_section(p),
    ];
    if let Some(fb) = rs::ctx_call_fallback(p) {
        v.push(fb);
    }
    v
}

fn full_non_shadow_sections(p: &super::tool_profiles::ToolProfile) -> Vec<String> {
    use super::rules_sections as rs;
    let mut v = vec![
        s(CRITICAL),
        s(MUST_INVOKE),
        s(BULLETS),
        s(NEVER),
        rs::intent_section(p),
        rs::anti_section(p),
        s(PARALLEL),
        s(AUTO),
        s(RECOVER_COMPACT),
        rs::litm_end_section(p),
    ];
    if let Some(fb) = rs::ctx_call_fallback(p) {
        v.push(fb);
    }
    v
}

fn full_shadow_sections(p: &super::tool_profiles::ToolProfile) -> Vec<String> {
    use super::rules_sections as rs;
    vec![rs::shadow_minimal_section(p)]
}

fn hook_covered_non_shadow_sections(p: &super::tool_profiles::ToolProfile) -> Vec<String> {
    use super::rules_sections as rs;
    let mut v = vec![
        s(HOOK_COVERED_HEADER),
        rs::hook_covered_tools_section(p),
        s(PARALLEL),
        s(RECOVER_COMPACT),
    ];
    if let Some(fb) = rs::ctx_call_fallback(p) {
        v.push(fb);
    }
    v
}

fn compact_non_shadow_sections(p: &super::tool_profiles::ToolProfile) -> Vec<String> {
    use super::rules_sections as rs;
    let mut v = vec![
        s(CRITICAL),
        s(BULLETS),
        s(NEVER),
        rs::intent_section(p),
        rs::anti_section(p),
        s(PARALLEL),
        s(RECOVER_COMPACT),
    ];
    if let Some(fb) = rs::ctx_call_fallback(p) {
        v.push(fb);
    }
    v
}

fn compact_shadow_sections(p: &super::tool_profiles::ToolProfile) -> Vec<String> {
    use super::rules_sections as rs;
    vec![rs::shadow_minimal_section(p)]
}

/// Selects the profile (LONGFORM / FULL / COMPACT) and the wrapping style
/// (markers, headers, footers) for `render()`.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum Wrapper {
    /// **On-demand long form** (project `LEAN-CTX.md`). LONGFORM profile with
    /// the same marker wrapping as `Dedicated`. Not auto-loaded by any client
    /// — agents open it on demand via the AGENTS.md pointer, so it can afford
    /// the verbose teaching sections.
    Longform,

    /// **Dedicated rule file.**  FULL profile.  Wrapped with `START_MARK`,
    /// `<!-- version: N -->`, and `END_MARK`.  Non-shadow includes the
    /// `CRITICAL` banner before the body.  The whole file is lean-ctx–owned;
    /// the injection layer detects staleness by parsing the version comment.
    Dedicated,

    /// **Shared file section** (appended to AGENTS.md, GEMINI.md, etc.).
    /// COMPACT profile.  Same marker wrapping for find/replace within a
    /// larger shared file.  Non-shadow includes `## Tool Mapping` header.
    Shared,

    /// **MCP session instructions.**  COMPACT profile.  No markers or
    /// headers — bare content used inline in per-session MCP instructions.
    Bare,

    /// **Hook-covered dedicated rule file** (GL #1153). For hosts whose
    /// installed lean-ctx hooks already compress the native tools (Cursor:
    /// PreToolUse rewrite/redirect). Same marker/version wrapping as
    /// `Dedicated`, but the body swaps the unenforceable tool-mapping for the
    /// honest hook-coverage note plus the exclusive-capability advert.
    /// Shadow mode collapses it to the same minimal profile as `Dedicated`.
    HookCovered,
}

/// Render lean-ctx rules for a given wrapper, shadow mode, compression level,
/// and tool profile (#756).
///
/// * `shadow` — when true, tool-mapping sections (BULLETS, NEVER,
///   CRITICAL banner, "## Tool Mapping" header) are omitted.
/// * `wrapper` — selects the profile (FULL / COMPACT) and wrapping style.
/// * `level` — selects the output-style compression prompt (Lite / Standard /
///   Max) which is appended to the body. `Off` omits it.
/// * `tool_profile` — filters tool references in dynamic sections so agents
///   only see tools they can actually call.
pub fn render(
    shadow: bool,
    wrapper: Wrapper,
    level: CompressionLevel,
    tool_profile: &super::tool_profiles::ToolProfile,
) -> String {
    use super::rules_sections as rs;

    let sections: Vec<String> = match (wrapper, shadow) {
        (Wrapper::Longform, false) => longform_non_shadow_sections(tool_profile),
        (Wrapper::Longform | Wrapper::Dedicated | Wrapper::HookCovered, true) => {
            full_shadow_sections(tool_profile)
        }
        (Wrapper::Dedicated, false) => full_non_shadow_sections(tool_profile),
        (Wrapper::HookCovered, false) => hook_covered_non_shadow_sections(tool_profile),
        (_, false) => compact_non_shadow_sections(tool_profile),
        (_, true) => compact_shadow_sections(tool_profile),
    };

    // Suppress empty sections (a section builder may return "" when the
    // profile hides all tools it would mention).
    let mut body: String = sections
        .into_iter()
        .filter(|s| !s.is_empty())
        .collect::<Vec<_>>()
        .join("\n\n");
    let _ = rs::intent_section; // anchor — ensures the module is linked

    // Append the compression / output-style prompt for active levels. Persistent
    // carriers (Dedicated, Shared) wrap it in the canonical COMPRESSION_BLOCK
    // markers so coverage/dedup (rules_channel, rules_dedup) can detect and thin
    // it; the ephemeral Bare MCP channel keeps it unmarked (#684/#548).
    let compression = compression_text(level);
    if !compression.is_empty() {
        body.push('\n');
        if matches!(wrapper, Wrapper::Bare) {
            body.push_str(compression);
        } else {
            body.push_str(COMPRESSION_BLOCK_START);
            body.push('\n');
            body.push_str(compression);
            body.push('\n');
            body.push_str(COMPRESSION_BLOCK_END);
        }
    }

    if matches!(wrapper, Wrapper::Bare) {
        return body;
    }

    let version_line = format!("<!-- version: {RULES_VERSION} -->");

    format!("{START_MARK}\n{version_line}\n\n{body}\n{END_MARK}")
}

/// Unmarked render of the hook-covered profile for ephemeral channels
/// (the mcp.json `instructions` snapshot on hook-covered hosts, GL #1153).
/// The `Bare` counterpart of `Wrapper::HookCovered`: same body, no markers —
/// per-session channels are governed by carrier coverage, so markers would be
/// noise (see [`COMPRESSION_BLOCK_START`]). Shadow collapses to the regular
/// bare shadow profile (interception supersedes hook coverage).
pub fn render_hook_covered_bare(
    shadow: bool,
    level: CompressionLevel,
    tool_profile: &super::tool_profiles::ToolProfile,
) -> String {
    if shadow {
        return render(true, Wrapper::Bare, level, tool_profile);
    }
    let sections = hook_covered_non_shadow_sections(tool_profile);
    let mut body: String = sections
        .into_iter()
        .filter(|s| !s.is_empty())
        .collect::<Vec<_>>()
        .join("\n\n");
    let compression = compression_text(level);
    if !compression.is_empty() {
        body.push('\n');
        body.push_str(compression);
    }
    body
}
// Rules-file parsing and validation live in the sibling module.
pub use super::rules_validation::RulesFile;

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

    fn tp() -> super::super::tool_profiles::ToolProfile {
        super::super::tool_profiles::ToolProfile::Power
    }

    // --- Constants ---

    #[test]
    fn bullets_uses_ctx_shell() {
        assert!(BULLETS.contains("ctx_shell"));
        assert!(!BULLETS.contains("lean-ctx -c"));
        assert!(!BULLETS.contains("ctx_edit"));
    }

    #[test]
    fn sections_not_empty() {
        assert!(!BULLETS.is_empty());
        assert!(!NEVER.is_empty());
        assert!(!INTENT.is_empty());
        assert!(!ANTI.is_empty());
        assert!(!PARALLEL.is_empty());
        assert!(!AUTO.is_empty());
        assert!(!CEP.is_empty());
        assert!(!LITM_END.is_empty());
        assert!(!CRITICAL.is_empty());
    }

    #[test]
    fn intent_contains_ctx_compose() {
        assert!(INTENT.contains("ctx_compose"));
    }

    #[test]
    fn anti_contains_do_not() {
        assert!(ANTI.contains("do NOT"));
    }

    #[test]
    fn parallel_contains_parallel() {
        assert!(PARALLEL.contains("PARALLEL"));
    }

    // --- Agent loop + navigation paradox (#609) ---

    #[test]
    fn agent_loop_names_every_phase() {
        for phase in ["Orient", "Find", "Read", "Locate", "Trace", "Verify"] {
            assert!(AGENT_LOOP.contains(phase), "AGENT_LOOP must name {phase}");
        }
        assert!(AGENT_LOOP.contains("ctx_compose") && AGENT_LOOP.contains("ctx_callgraph"));
    }

    #[test]
    fn nav_paradox_steers_semantic_vs_graph() {
        assert!(
            NAV_PARADOX.contains("ctx_search(action=semantic)"),
            "semantic route must use folded action (#509)"
        );
        assert!(NAV_PARADOX.contains("ctx_callgraph"), "graph route");
        assert!(
            NAV_PARADOX.contains(""),
            "must carry the reading≠understanding thesis"
        );
    }

    #[test]
    fn longform_carries_loop_and_paradox_injected_full_does_not() {
        // v5 (#578): the verbose teaching sections live only in the on-demand
        // LEAN-CTX.md (Longform); the injected dedicated files fold the loop
        // phases + paradox thesis into INTENT.
        let long = render(false, Wrapper::Longform, CompressionLevel::Off, &tp());
        assert!(
            long.contains("AGENT LOOP"),
            "LONGFORM must carry AGENT_LOOP"
        );
        assert!(
            long.contains("NAVIGATION PARADOX"),
            "LONGFORM must carry NAV_PARADOX"
        );
        assert!(long.contains(CEP), "LONGFORM must carry CEP");

        let full = render(false, Wrapper::Dedicated, CompressionLevel::Off, &tp());
        assert!(
            !full.contains("AGENT LOOP (phase -> tool):"),
            "injected FULL must not inline the multi-line AGENT_LOOP block"
        );
        assert!(
            !full.contains("NAVIGATION PARADOX: reading"),
            "injected FULL must not inline the multi-line NAV_PARADOX block"
        );
        assert!(
            full.contains(''),
            "INTENT must keep the reading≠understanding thesis in FULL"
        );
        // #509: ctx_symbol folded into ctx_search(action=symbol)
        for phase_tool in [
            "ctx_compose",
            "ctx_search(action=symbol)",
            "ctx_search",
            "ctx_callgraph",
        ] {
            assert!(
                full.contains(phase_tool),
                "FULL keeps the loop tools via INTENT: {phase_tool}"
            );
        }
    }

    #[test]
    fn injected_profiles_stay_lean() {
        // The whole point of v5 (#578): injected files bill every session.
        // chars/4 ≈ tokens — the dedicated body must stay around ~470 tok and
        // the Longform must be a strict superset.
        let full = render(false, Wrapper::Dedicated, CompressionLevel::Off, &tp());
        eprintln!(
            "rules footprint: dedicated={} chars (~{} tok), longform={} chars, bare={} chars",
            full.len(),
            full.len() / 4,
            render(false, Wrapper::Longform, CompressionLevel::Off, &tp()).len(),
            render(false, Wrapper::Bare, CompressionLevel::Off, &tp()).len(),
        );
        assert!(
            full.len() <= 2400,
            "injected dedicated rules must stay ≤2400 chars (~600 tok), got {} chars (~{} tok)",
            full.len(),
            full.len() / 4
        );
        let long = render(false, Wrapper::Longform, CompressionLevel::Off, &tp());
        assert!(
            long.len() > full.len(),
            "Longform ({}) must carry more than the injected profile ({})",
            long.len(),
            full.len()
        );
        let compact = render(false, Wrapper::Bare, CompressionLevel::Off, &tp());
        assert!(
            compact.len() < full.len(),
            "COMPACT/Bare ({}) must stay below the dedicated profile ({})",
            compact.len(),
            full.len()
        );
    }

    #[test]
    fn compact_profile_has_no_multiline_teaching_sections() {
        // COMPACT (shared + Bare) keeps the per-session channel lean: no
        // multi-line AGENT_LOOP/NAV_PARADOX blocks; INTENT carries the thesis.
        let out = render(false, Wrapper::Shared, CompressionLevel::Off, &tp());
        assert!(
            !out.contains("AGENT LOOP (phase -> tool):"),
            "COMPACT must not inline the multi-line AGENT_LOOP block"
        );
        assert!(
            !out.contains("NAVIGATION PARADOX: reading"),
            "COMPACT must not inline the multi-line NAV_PARADOX block"
        );
        assert!(
            out.contains(''),
            "COMPACT keeps the reading≠understanding thesis via INTENT"
        );
    }

    #[test]
    fn shadow_omits_loop_and_paradox() {
        // #963: shadow collapses to the irreducible minimum — the routing
        // taxonomy is redundant once native calls are intercepted.
        for wrapper in [Wrapper::Longform, Wrapper::Dedicated, Wrapper::Shared] {
            let out = render(true, wrapper, CompressionLevel::Off, &tp());
            assert!(!out.contains("AGENT LOOP"), "{wrapper:?} shadow drops loop");
            assert!(
                !out.contains("NAVIGATION PARADOX"),
                "{wrapper:?} shadow drops paradox"
            );
        }
    }

    #[test]
    fn recover_reaches_every_non_shadow_carrier() {
        // The recovery vocabulary must reach every non-shadow carrier so agents
        // never re-read compressed output line-by-line, and every carrier must
        // keep the MCP-free path ("read the shown path") for orgs that ban MCP.
        // v5 (#578): only Longform carries the verbose RECOVER; every injected
        // profile (Dedicated FULL + Shared/Bare COMPACT) carries the terse
        // RECOVER_COMPACT one-liner.
        let long = render(false, Wrapper::Longform, CompressionLevel::Off, &tp());
        assert!(
            long.contains(RECOVER),
            "Longform must carry the verbose RECOVER verbatim"
        );
        for wrapper in [Wrapper::Dedicated, Wrapper::Shared, Wrapper::Bare] {
            let out = render(false, wrapper, CompressionLevel::Off, &tp());
            assert!(
                out.contains(RECOVER_COMPACT),
                "{wrapper:?} must carry RECOVER_COMPACT verbatim"
            );
            assert!(
                !out.contains(RECOVER),
                "{wrapper:?} must not inline the verbose RECOVER block"
            );
        }
        for wrapper in [
            Wrapper::Longform,
            Wrapper::Dedicated,
            Wrapper::Shared,
            Wrapper::Bare,
        ] {
            assert!(
                render(false, wrapper, CompressionLevel::Off, &tp()).contains("(no MCP)"),
                "{wrapper:?} recovery line must keep the MCP-free path"
            );
        }
        for wrapper in [Wrapper::Dedicated, Wrapper::Shared] {
            let out = render(true, wrapper, CompressionLevel::Off, &tp());
            assert!(
                !out.contains(RECOVER) && !out.contains(RECOVER_COMPACT),
                "{wrapper:?} shadow drops all RECOVER guidance"
            );
        }
    }

    // --- render() — Dedicated ---

    #[test]
    fn dedicated_has_markers_and_version() {
        let out = render(false, Wrapper::Dedicated, CompressionLevel::Off, &tp());
        assert!(out.contains(START_MARK));
        assert!(out.contains(&format!("<!-- version: {RULES_VERSION} -->")));
        assert!(out.contains(END_MARK));
        assert!(out.contains(BULLETS));
        assert!(out.contains(NEVER));
        assert!(out.contains("CRITICAL"));
    }

    #[test]
    fn dedicated_shadow_is_minimal() {
        // #963: shadow drops the whole tool-mapping AND routing playbook —
        // interception makes them redundant. Only the exclusive-tool advert remains.
        let out = render(true, Wrapper::Dedicated, CompressionLevel::Off, &tp());
        assert!(out.contains(START_MARK));
        assert!(!out.contains("MANDATORY MAPPING"), "no BULLETS in shadow");
        assert!(!out.contains(NEVER), "no NEVER in shadow");
        assert!(!out.contains("CRITICAL"), "no CRITICAL banner in shadow");
        assert!(
            !out.contains("Tool selection by intent"),
            "routing INTENT block is redundant under interception"
        );
        assert!(
            !out.contains("Anti-patterns") && !out.contains("PARALLEL tool calls"),
            "ANTI/PARALLEL routing guidance is dropped in shadow"
        );
        assert!(
            out.contains("shadow mode") && out.contains("ctx_compose"),
            "shadow keeps the exclusive-tool advert"
        );
    }

    #[test]
    fn shadow_is_smaller_than_non_shadow() {
        // The whole point of #963: the shadow body must be a strict reduction.
        let shadow = render(true, Wrapper::Dedicated, CompressionLevel::Off, &tp());
        let full = render(false, Wrapper::Dedicated, CompressionLevel::Off, &tp());
        assert!(
            shadow.len() < full.len(),
            "shadow ({}) must be smaller than non-shadow ({})",
            shadow.len(),
            full.len()
        );
    }

    #[test]
    fn dedicated_litm_structure() {
        let out = render(false, Wrapper::Dedicated, CompressionLevel::Off, &tp());
        let lines: Vec<&str> = out.lines().collect();
        let first_5 = lines[..5.min(lines.len())].join("\n");
        assert!(
            first_5.contains("CRITICAL") || first_5.contains("MUST"),
            "LITM: MUST/CRITICAL instruction near start"
        );
        // LITM_END or NEVER should appear in the final content lines (before END_MARK).
        let tail = lines[lines.len().saturating_sub(8)..].join("\n");
        assert!(
            tail.contains("PREFERENCE") || tail.contains("NEVER"),
            "LITM: reinforcement near end, tail={tail:?}"
        );
    }

    #[test]
    fn dedicated_carries_weak_model_invoke_nudge() {
        // #1067/GH #593: the "actually CALL ctx_*" nudge must ride every dedicated
        // rule file (Windsurf, Cursor, Claude, …) in non-shadow mode, and must be
        // absent where it is dead weight: shadow mode (call-layer routing) and the
        // Bare/instructions channel (separately capped).
        let dedicated = render(false, Wrapper::Dedicated, CompressionLevel::Off, &tp());
        assert!(
            dedicated.contains(MUST_INVOKE),
            "dedicated non-shadow rules must carry the MUST_INVOKE nudge"
        );
        assert!(
            !render(true, Wrapper::Dedicated, CompressionLevel::Off, &tp()).contains(MUST_INVOKE),
            "shadow mode must not carry the nudge (routing is enforced at the call layer)"
        );
        assert!(
            !render(false, Wrapper::Bare, CompressionLevel::Off, &tp()).contains(MUST_INVOKE),
            "Bare/instructions channel is capped separately and carries no copy"
        );
    }

    // --- render() — Shared ---

    #[test]
    fn shared_has_markers_and_header() {
        let out = render(false, Wrapper::Shared, CompressionLevel::Off, &tp());
        assert!(out.contains(START_MARK));
        assert!(out.contains(END_MARK));
        assert!(out.contains("MANDATORY MAPPING"));
        assert!(out.contains(BULLETS));
    }

    #[test]
    fn shared_shadow_omits_mapping() {
        let out = render(true, Wrapper::Shared, CompressionLevel::Off, &tp());
        assert!(out.contains(START_MARK));
        assert!(
            !out.contains("MANDATORY MAPPING"),
            "shadow must not have header"
        );
        assert!(
            !out.contains("MANDATORY MAPPING"),
            "shadow must not contain BULLETS"
        );
    }

    // --- render() — Bare ---

    #[test]
    fn bare_has_no_markers() {
        let out = render(false, Wrapper::Bare, CompressionLevel::Off, &tp());
        assert!(!out.contains(START_MARK), "Bare must not have START_MARK");
        assert!(!out.contains(END_MARK), "Bare must not have END_MARK");
        assert!(!out.contains("<!-- version:"), "Bare must not have version");
        assert!(out.contains(BULLETS));
        assert!(out.contains(NEVER));
    }

    #[test]
    fn bare_shadow_only_read_modes() {
        let out = render(true, Wrapper::Bare, CompressionLevel::Off, &tp());
        assert!(!out.contains(NEVER), "shadow Bare must not have NEVER");
        assert!(
            !out.contains("MANDATORY MAPPING"),
            "shadow Bare must not have BULLETS"
        );
    }

    // --- Compression level tests ---

    #[test]
    fn render_includes_lite_prompt() {
        let out = render(false, Wrapper::Bare, CompressionLevel::Lite, &tp());
        assert!(out.contains("OUTPUT STYLE: concise"));
        assert!(out.contains("Bullet points"));
    }

    #[test]
    fn render_includes_standard_prompt() {
        let out = render(false, Wrapper::Bare, CompressionLevel::Standard, &tp());
        assert!(out.contains("OUTPUT STYLE: dense"));
        assert!(out.contains("atomic fact"));
    }

    #[test]
    fn render_includes_max_prompt() {
        let out = render(false, Wrapper::Bare, CompressionLevel::Max, &tp());
        assert!(out.contains("OUTPUT STYLE: expert-terse"));
        assert!(out.contains("Telegraph"));
    }

    #[test]
    fn render_off_excludes_compression() {
        let out = render(false, Wrapper::Bare, CompressionLevel::Off, &tp());
        assert!(!out.contains("OUTPUT STYLE:"));
    }

    #[test]
    fn compression_text_matches_level() {
        assert!(compression_text(CompressionLevel::Off).is_empty());
        assert!(compression_text(CompressionLevel::Lite).contains("Bullet"));
        assert!(compression_text(CompressionLevel::Standard).contains("fn, cfg"));
        assert!(compression_text(CompressionLevel::Max).contains("Telegraph"));
    }

    // --- Compression marker model (#548 B2) ---

    #[test]
    fn carrier_wrappers_wrap_compression_in_markers() {
        // Persistent carriers must delimit the compression payload so coverage
        // and dedup can detect/thin it (#684/#548).
        for wrapper in [Wrapper::Longform, Wrapper::Dedicated, Wrapper::Shared] {
            let out = render(false, wrapper, CompressionLevel::Standard, &tp());
            assert!(
                out.contains(COMPRESSION_BLOCK_START) && out.contains(COMPRESSION_BLOCK_END),
                "{wrapper:?} must wrap compression in COMPRESSION_BLOCK markers"
            );
            // The marked region must actually contain the prompt body.
            let start = out.find(COMPRESSION_BLOCK_START).unwrap();
            let end = out.find(COMPRESSION_BLOCK_END).unwrap();
            assert!(start < end, "{wrapper:?}: start marker precedes end marker");
            assert!(out[start..end].contains("OUTPUT STYLE: dense"));
        }
    }

    #[test]
    fn bare_wrapper_emits_compression_without_markers() {
        // The ephemeral MCP channel keeps the payload unmarked — its inclusion is
        // governed by carrier coverage, so per-session markers would be noise.
        let out = render(false, Wrapper::Bare, CompressionLevel::Standard, &tp());
        assert!(out.contains("OUTPUT STYLE: dense"));
        assert!(!out.contains(COMPRESSION_BLOCK_START));
        assert!(!out.contains(COMPRESSION_BLOCK_END));
    }

    #[test]
    fn compression_off_emits_no_markers_in_any_wrapper() {
        for wrapper in [
            Wrapper::Longform,
            Wrapper::Dedicated,
            Wrapper::Shared,
            Wrapper::Bare,
        ] {
            let out = render(false, wrapper, CompressionLevel::Off, &tp());
            assert!(
                !out.contains(COMPRESSION_BLOCK_START) && !out.contains(COMPRESSION_BLOCK_END),
                "{wrapper:?}: Off must emit no compression markers"
            );
        }
    }

    #[test]
    fn rendered_carrier_block_is_seen_as_carrying_compression() {
        // The detection helper that coverage/dedup rely on must agree with the
        // writer's output (the bug this slice fixes: it previously never did).
        let dedicated = render(false, Wrapper::Dedicated, CompressionLevel::Lite, &tp());
        assert!(crate::core::rules_channel::carries_full_rules(&dedicated));
        assert!(dedicated.contains(COMPRESSION_BLOCK_START));
    }

    // --- Wrapper round-trip ---

    #[test]
    fn all_wrappers_produce_output() {
        for shadow in [false, true] {
            for wrapper in [
                Wrapper::Longform,
                Wrapper::Dedicated,
                Wrapper::Shared,
                Wrapper::Bare,
            ] {
                let out = render(shadow, wrapper, CompressionLevel::Off, &tp());
                assert!(!out.is_empty(), "{wrapper:?} shadow={shadow} is empty");
            }
        }
    }

    // --- RulesFile ---

    #[test]
    fn rules_file_parses_version() {
        let content = format!(
            "stuff before\n{START_MARK}\n<!-- version: {RULES_VERSION} -->\n\nbody\n{END_MARK}\nstuff after"
        );
        let f = RulesFile::parse(&content);
        assert!(f.has_content());
        assert_eq!(f.version(), RULES_VERSION);
        assert!(f.is_current());
        assert!(f.prefix().contains("stuff before"));
        assert!(f.suffix().contains("stuff after"));
    }

    #[test]
    fn rules_file_no_version_defaults_to_zero() {
        let content = format!("{START_MARK}\nbody\n{END_MARK}");
        let f = RulesFile::parse(&content);
        assert!(f.has_content());
        assert_eq!(f.version(), 0);
        assert!(!f.is_current());
    }

    #[test]
    fn rules_file_no_start_marker_no_content() {
        let f = RulesFile::parse("just user stuff");
        assert!(!f.has_content());
        assert_eq!(f.version(), 0);
    }

    #[test]
    fn block_matches_render_true_for_fresh_render() {
        let fresh = render(false, Wrapper::Dedicated, CompressionLevel::Off, &tp());
        let content = format!("user before\n{fresh}\nuser after");
        let f = RulesFile::parse(&content);
        assert!(f.is_current(), "fresh render carries the current version");
        assert!(
            f.block_matches_render(false, Wrapper::Dedicated, CompressionLevel::Off, &tp()),
            "an unchanged block must compare equal to a fresh render"
        );
    }

    #[test]
    fn block_matches_render_false_on_compression_change() {
        // Body rendered at Off, then asked whether it matches a Max render:
        // the version is identical but the compression payload differs (#548).
        let content = render(false, Wrapper::Dedicated, CompressionLevel::Off, &tp());
        let f = RulesFile::parse(&content);
        assert!(f.is_current());
        assert!(
            !f.block_matches_render(false, Wrapper::Dedicated, CompressionLevel::Max, &tp()),
            "a compression-level change must be detected as drift"
        );
    }

    #[test]
    fn block_matches_render_false_on_shadow_change() {
        let content = render(false, Wrapper::Dedicated, CompressionLevel::Lite, &tp());
        let f = RulesFile::parse(&content);
        assert!(
            !f.block_matches_render(true, Wrapper::Dedicated, CompressionLevel::Lite, &tp()),
            "a shadow-mode toggle must be detected as drift"
        );
    }

    #[test]
    fn block_matches_render_false_without_block() {
        let f = RulesFile::parse("plain user content, no markers");
        assert!(!f.block_matches_render(false, Wrapper::Dedicated, CompressionLevel::Off, &tp()));
    }

    #[test]
    fn rules_file_merged_replaces_section() {
        let content =
            format!("before\n{START_MARK}\n<!-- version: 1 -->\n\nold\n{END_MARK}\nafter");
        let f = RulesFile::parse(&content);
        let merged = f.merged(false, Wrapper::Shared, CompressionLevel::Off, &tp());
        assert!(merged.contains("before"), "prefix preserved");
        assert!(merged.contains("after"), "suffix preserved");
        assert!(!merged.contains("old"), "old content replaced");
        assert!(merged.contains(&format!("<!-- version: {RULES_VERSION} -->")));
    }

    #[test]
    fn rules_file_merged_appends_when_no_section() {
        let content = "user content";
        let f = RulesFile::parse(content);
        assert!(!f.has_content());
        let merged = f.merged(false, Wrapper::Bare, CompressionLevel::Off, &tp());
        assert!(merged.contains("user content"));
        assert!(merged.contains(BULLETS));
    }

    #[test]
    fn rules_file_without_section_strips_content() {
        let content =
            format!("header\n{START_MARK}\n<!-- version: 1 -->\n\nbody\n{END_MARK}\nfooter");
        let f = RulesFile::parse(&content);
        let stripped = f.without_section();
        assert!(stripped.contains("header"));
        assert!(stripped.contains("footer"));
        assert!(!stripped.contains("body"));
        assert!(!stripped.contains(START_MARK));
    }

    #[test]
    fn rules_file_without_section_noop_when_no_content() {
        let content = "just user text";
        let f = RulesFile::parse(content);
        assert_eq!(f.without_section(), content);
    }

    #[test]
    fn bullets_lead_with_four_core_redirects() {
        // Most-used routes (Read/Grep/Shell/Glob) lead; ls->ctx_tree trails.
        let read = BULLETS.find("ctx_read").expect("ctx_read mapping present");
        let search = BULLETS
            .find("ctx_search")
            .expect("ctx_search mapping present");
        let shell = BULLETS
            .find("ctx_shell")
            .expect("ctx_shell mapping present");
        let glob = BULLETS.find("ctx_glob").expect("ctx_glob mapping present");
        let tree = BULLETS.find("ctx_tree").expect("ctx_tree mapping present");
        assert!(
            read < search && search < shell && shell < glob && glob < tree,
            "core redirects (read<search<shell<glob) must precede ctx_tree"
        );
    }

    #[test]
    fn never_carries_self_correction() {
        // Self-correction reinforces the redirect harder than a bare prohibition.
        assert!(
            NEVER.contains("SELF-CORRECT"),
            "NEVER must teach self-correction"
        );
        assert!(
            NEVER.contains("call"),
            "NEVER must spell out the corrective action"
        );
    }

    #[test]
    fn critical_names_ctx_family() {
        assert!(
            CRITICAL.contains("ctx_*"),
            "CRITICAL must name the ctx_* family"
        );
    }

    // --- HookCovered profile (GL #1153) ---

    #[test]
    fn hook_covered_carries_strict_mapping() {
        // v8: HookCovered is strict — always prefer ctx_* over native tools.
        // The MANDATORY MAPPING replaces the old "no native equivalent" list.
        let out = render(false, Wrapper::HookCovered, CompressionLevel::Off, &tp());
        assert!(
            !out.contains(NEVER),
            "HookCovered uses its own strict header, not the NEVER constant"
        );
        assert!(
            out.contains(HOOK_COVERED_HEADER),
            "must carry the strict preference header"
        );
        assert!(
            out.contains("MANDATORY MAPPING")
                && out.contains("ctx_read")
                && out.contains("ctx_search"),
            "must carry the full mandatory mapping"
        );
        assert!(
            out.contains("ctx_compose") && out.contains("action=symbol|semantic"),
            "must advertise the search capabilities"
        );
    }

    #[test]
    fn hook_covered_keeps_markers_version_and_recovery() {
        // Coverage detection (rules_channel::carries_full_rules /
        // client_autoloads_rules) and the injector's drift check both key on
        // the canonical markers — HookCovered must stay a first-class carrier.
        let out = render(false, Wrapper::HookCovered, CompressionLevel::Off, &tp());
        assert!(out.contains(START_MARK) && out.contains(END_MARK));
        assert!(out.contains(&format!("<!-- version: {RULES_VERSION} -->")));
        assert!(out.contains(RECOVER_COMPACT), "recovery line must survive");
        assert!(out.contains("(no MCP)"), "MCP-free recovery path stays");
    }

    #[test]
    fn hook_covered_is_leaner_than_full() {
        let covered = render(false, Wrapper::HookCovered, CompressionLevel::Off, &tp());
        let full = render(false, Wrapper::Dedicated, CompressionLevel::Off, &tp());
        assert!(
            covered.len() < full.len(),
            "HookCovered ({}) must be a strict reduction of FULL ({})",
            covered.len(),
            full.len()
        );
    }

    #[test]
    fn hook_covered_shadow_collapses_to_minimal() {
        // Interception supersedes hook coverage — same minimal profile as
        // Dedicated shadow.
        let covered_shadow = render(true, Wrapper::HookCovered, CompressionLevel::Off, &tp());
        let dedicated_shadow = render(true, Wrapper::Dedicated, CompressionLevel::Off, &tp());
        assert_eq!(covered_shadow, dedicated_shadow);
    }

    #[test]
    fn hook_covered_wraps_compression_in_markers() {
        let out = render(
            false,
            Wrapper::HookCovered,
            CompressionLevel::Standard,
            &tp(),
        );
        assert!(out.contains(COMPRESSION_BLOCK_START) && out.contains(COMPRESSION_BLOCK_END));
        assert!(out.contains("OUTPUT STYLE: dense"));
    }

    // --- Profile-aware rules (#756) ---

    #[test]
    fn rules_only_mention_enabled_tools() {
        use super::super::rules_sections;
        use super::super::tool_profiles::ToolProfile;
        let exclusive_tools = ["ctx_compose", "ctx_callgraph", "ctx_patch"];
        for profile in [ToolProfile::Minimal, ToolProfile::Standard] {
            let mut sections = vec![
                rules_sections::intent_section(&profile),
                rules_sections::hook_covered_tools_section(&profile),
                rules_sections::shadow_minimal_section(&profile),
                rules_sections::anti_section(&profile),
                rules_sections::litm_end_section(&profile),
            ];
            if let Some(fb) = rules_sections::ctx_call_fallback(&profile) {
                sections.push(fb);
            }
            for tool in &exclusive_tools {
                if !profile.is_tool_enabled(tool) {
                    for section in &sections {
                        assert!(
                            !section.contains(tool),
                            "profile {:?} dynamic section must not mention disabled tool {tool}",
                            profile.as_str()
                        );
                    }
                }
            }
        }
    }

    #[test]
    fn minimal_rules_shorter_than_standard() {
        use super::super::tool_profiles::ToolProfile;
        let min = render(
            false,
            Wrapper::Dedicated,
            CompressionLevel::Off,
            &ToolProfile::Minimal,
        );
        let std = render(
            false,
            Wrapper::Dedicated,
            CompressionLevel::Off,
            &ToolProfile::Standard,
        );
        assert!(
            min.len() < std.len(),
            "minimal ({}) must be shorter than standard ({})",
            min.len(),
            std.len()
        );
    }

    #[test]
    fn power_profile_output_identical_to_default() {
        use super::super::tool_profiles::ToolProfile;
        let power = render(
            false,
            Wrapper::Dedicated,
            CompressionLevel::Off,
            &ToolProfile::Power,
        );
        assert!(
            power.contains("ctx_compose"),
            "Power must include all tools"
        );
        assert!(
            power.contains("ctx_callgraph"),
            "Power must include all tools"
        );
        assert!(
            power.contains("ctx_session"),
            "Power must include all tools"
        );
    }

    #[test]
    fn render_is_deterministic_across_profiles() {
        use super::super::tool_profiles::ToolProfile;
        for profile in [
            ToolProfile::Minimal,
            ToolProfile::Standard,
            ToolProfile::Power,
        ] {
            let a = render(false, Wrapper::Dedicated, CompressionLevel::Off, &profile);
            let b = render(false, Wrapper::Dedicated, CompressionLevel::Off, &profile);
            assert_eq!(
                a,
                b,
                "render must be deterministic for {:?}",
                profile.as_str()
            );
        }
    }

    #[test]
    fn minimal_has_ctx_call_fallback() {
        use super::super::tool_profiles::ToolProfile;
        let rules = render(
            false,
            Wrapper::Dedicated,
            CompressionLevel::Off,
            &ToolProfile::Minimal,
        );
        assert!(
            rules.contains("ctx_call"),
            "minimal profile must include ctx_call gateway hint"
        );
    }

    #[test]
    fn power_has_no_ctx_call_fallback() {
        use super::super::tool_profiles::ToolProfile;
        let rules = render(
            false,
            Wrapper::Dedicated,
            CompressionLevel::Off,
            &ToolProfile::Power,
        );
        assert!(
            !rules.contains("ctx_call(tool="),
            "power profile must not include ctx_call gateway hint"
        );
    }
}