mesh-llm-config 0.75.0

Configuration parsing and validation for mesh-llm
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
use super::*;

#[derive(Clone, Copy)]
struct CategoryPresentation {
    id: &'static str,
    label: &'static str,
    summary: &'static str,
    order: u32,
}

const RUNTIME_CATEGORY: CategoryPresentation = CategoryPresentation {
    id: "runtime",
    label: "Runtime",
    summary: "Load-time runtime behavior and concurrency defaults",
    order: 10,
};
const MESHLLM_CATEGORY: CategoryPresentation = CategoryPresentation {
    id: "meshllm",
    label: "General",
    summary: "Local node startup and observability settings",
    order: 10,
};
const NETWORK_CATEGORY: CategoryPresentation = CategoryPresentation {
    id: "network",
    label: "Network",
    summary: "Owner-control listener and advertised control endpoint settings",
    order: 20,
};
const ATTESTATION_CATEGORY: CategoryPresentation = CategoryPresentation {
    id: "attestation",
    label: "Attestation",
    summary: "Creation-time certified-build admission requirements",
    order: 30,
};
const TELEMETRY_CATEGORY: CategoryPresentation = CategoryPresentation {
    id: "telemetry",
    label: "Telemetry",
    summary: "Opt-in metrics export and local telemetry queue settings",
    order: 40,
};
const RUNTIME_POLICY_CATEGORY: CategoryPresentation = CategoryPresentation {
    id: "runtime-policy",
    label: "Runtime Policy",
    summary: "Runtime reconciliation behavior applied by the local process",
    order: 10,
};
const MEMORY_CATEGORY: CategoryPresentation = CategoryPresentation {
    id: "memory",
    label: "Memory",
    summary: "VRAM accounting and KV cache policy",
    order: 20,
};
const SPECULATIVE_CATEGORY: CategoryPresentation = CategoryPresentation {
    id: "speculative-decoding",
    label: "Speculative Decoding",
    summary: "Speculative draft policy defaults",
    order: 30,
};
const REQUEST_DEFAULTS_CATEGORY: CategoryPresentation = CategoryPresentation {
    id: "request-defaults",
    label: "Request Defaults",
    summary: "Request-time sampling and reasoning defaults",
    order: 40,
};
const SKIPPY_CATEGORY: CategoryPresentation = CategoryPresentation {
    id: "skippy-transport",
    label: "Skippy Transport",
    summary: "Stage transport, chunking, and lifecycle defaults",
    order: 50,
};
const MULTIMODAL_CATEGORY: CategoryPresentation = CategoryPresentation {
    id: "multimodal",
    label: "Multimodal",
    summary: "Vision projector and image token defaults",
    order: 60,
};
const ADVANCED_SERVER_CATEGORY: CategoryPresentation = CategoryPresentation {
    id: "advanced-server",
    label: "Advanced Server",
    summary: "Advanced server defaults and identity overrides",
    order: 70,
};
const PLUGIN_HOST_CATEGORY: CategoryPresentation = CategoryPresentation {
    id: "plugin-host",
    label: "Plugin Host",
    summary: "Host-owned plugin process and startup settings",
    order: 10,
};
const MAX_ENUM_VALUES_FOR_SEGMENTED_CONTROL: usize = 4;

struct SettingPresentation {
    label: &'static str,
    help: &'static str,
    category: CategoryPresentation,
    order: u32,
    unit: Option<&'static str>,
    placeholder: Option<&'static str>,
    control_hint: Option<&'static str>,
    renderer_id: Option<&'static str>,
}

fn setting_presentation_for_path(rendered: &str) -> Option<SettingPresentation> {
    process_setting_presentation(rendered)
        .or_else(|| runtime_defaults_presentation(rendered))
        .or_else(|| generation_defaults_presentation(rendered))
        .or_else(|| skippy_multimodal_presentation(rendered))
        .or_else(|| model_and_plugin_presentation(rendered))
}

fn process_setting_presentation(rendered: &str) -> Option<SettingPresentation> {
    match rendered {
        "gpu.assignment" => Some(sp(
            "GPU assignment",
            "Choose automatic GPU placement, or require configured model entries to name a concrete GPU device.",
            RUNTIME_CATEGORY,
            10,
        )
        .hint("segmented")),
        "gpu.parallel" => Some(sp(
            "GPU parallelism",
            "Limit the local GPU startup parallelism used when configured models are launched.",
            RUNTIME_CATEGORY,
            20,
        )
        .unit("models")
        .hint("number")),
        "telemetry.enabled" => Some(sp(
            "Telemetry export",
            "Enable opt-in metrics export. Ambient OTel environment variables do not enable export by themselves.",
            TELEMETRY_CATEGORY,
            10,
        )
        .hint("toggle")),
        "telemetry.service_name" => Some(sp(
            "Service name",
            "Service name attached to exported metrics when telemetry is enabled.",
            TELEMETRY_CATEGORY,
            20,
        )
        .placeholder("mesh-llm")
        .hint("text")),
        "telemetry.endpoint" => Some(sp(
            "OTLP endpoint",
            "Default OTLP endpoint used by telemetry exporters when telemetry is enabled.",
            TELEMETRY_CATEGORY,
            30,
        )
        .placeholder("http://127.0.0.1:4317")
        .hint("text")),
        "telemetry.metrics.endpoint" => Some(sp(
            "Metrics endpoint",
            "Metrics-specific OTLP endpoint. Leave empty to inherit the default telemetry endpoint.",
            TELEMETRY_CATEGORY,
            40,
        )
        .placeholder("http://127.0.0.1:4317")
        .hint("text")),
        "telemetry.headers" => Some(sp(
            "Telemetry headers",
            "Optional JSON object of headers attached to OTLP export requests.",
            TELEMETRY_CATEGORY,
            50,
        )
        .placeholder("{\"authorization\":\"Bearer ...\"}")
        .hint("textarea")),
        "telemetry.export_interval_secs" => Some(sp(
            "Export interval",
            "Seconds between telemetry export attempts when telemetry is enabled.",
            TELEMETRY_CATEGORY,
            60,
        )
        .unit("sec")
        .hint("number")),
        "telemetry.queue_size" => Some(sp(
            "Telemetry queue size",
            "Maximum queued telemetry events before nonblocking exporters drop new events.",
            TELEMETRY_CATEGORY,
            70,
        )
        .unit("events")
        .hint("number")),
        "runtime.reconcile_model_targets" => Some(sp(
            "Reconcile model targets",
            "Allow the runtime loop to reconcile configured model targets against current mesh demand.",
            RUNTIME_POLICY_CATEGORY,
            10,
        )
        .hint("toggle")),
        "runtime.reconcile_model_target_demand_upgrades" => Some(sp(
            "Demand upgrades",
            "Allow model-target reconciliation to upgrade targets when repeated demand is observed.",
            RUNTIME_POLICY_CATEGORY,
            20,
        )
        .hint("toggle")),
        "runtime.model_target_demand_upgrade_min_requests" => Some(sp(
            "Demand upgrade request floor",
            "Minimum request count before model-target reconciliation considers a demand upgrade.",
            RUNTIME_POLICY_CATEGORY,
            30,
        )
        .unit("requests")
        .hint("number")),
        "runtime.model_target_demand_upgrade_max_age_secs" => Some(sp(
            "Demand upgrade max age",
            "Maximum age in seconds for requests that count toward demand upgrades.",
            RUNTIME_POLICY_CATEGORY,
            40,
        )
        .unit("sec")
        .hint("number")),
        "runtime.debug" => Some(
            sp(
                "Debug output",
                "Enable mesh runtime debug output on startup. Set MESH_LLM_DEBUG_NATIVE_VERBOSE=1 separately for verbose llama.cpp native logs.",
                MESHLLM_CATEGORY,
                30,
            )
            .hint("toggle"),
        ),
        "runtime.listen_all" => Some(
            sp(
                "Listen on all interfaces",
                "Bind the OpenAI-compatible API and web console listeners to 0.0.0.0 instead of 127.0.0.1. This matches --listen-all and is useful for containers or exposed LAN hosts.",
                NETWORK_CATEGORY,
                30,
            )
            .hint("toggle"),
        ),
        "owner_control.bind" => Some(sp(
            "Owner-control bind",
            "Local address used by the owner-control listener. Set this to the same port as the advertised control address when overriding owner-control discovery.",
            NETWORK_CATEGORY,
            10,
        )
        .placeholder("127.0.0.1:0")
        .hint("text")),
        "owner_control.advertise_addr" => Some(sp(
            "Advertised control address",
            "Concrete address encoded into local owner-control bootstrap payloads. Requires owner-control bind to listen on the same port.",
            NETWORK_CATEGORY,
            20,
        )
        .placeholder("127.0.0.1:7447")
        .hint("text")),
        "mesh_requirements.min_node_version" => Some(sp(
            "Minimum node version",
            "Lowest mesh-llm node version allowed when this requirement-aware mesh is created or joined.",
            ATTESTATION_CATEGORY,
            10,
        )
        .placeholder("0.69.0")
        .hint("text")),
        "mesh_requirements.max_node_version" => Some(sp(
            "Maximum node version",
            "Highest mesh-llm node version allowed when this requirement-aware mesh is created or joined.",
            ATTESTATION_CATEGORY,
            20,
        )
        .placeholder("0.75.0")
        .hint("text")),
        "mesh_requirements.min_protocol_version" => Some(sp(
            "Minimum protocol generation",
            "Lowest protocol generation allowed by this mesh admission policy.",
            ATTESTATION_CATEGORY,
            30,
        )
        .hint("number")),
        "mesh_requirements.max_protocol_version" => Some(sp(
            "Maximum protocol generation",
            "Highest protocol generation allowed by this mesh admission policy.",
            ATTESTATION_CATEGORY,
            40,
        )
        .hint("number")),
        "mesh_requirements.require_release_attestation" => Some(sp(
            "Require certified release",
            "Require peers to advertise a trusted release-build attestation at admission time. This is build provenance, not remote runtime integrity proof.",
            ATTESTATION_CATEGORY,
            50,
        )
        .hint("toggle")),
        "mesh_requirements.release_signer_keys" => Some(sp(
            "Trusted release signer keys",
            "Release signer public keys accepted by this mesh, formatted as ed25519:<64 hex characters>.",
            ATTESTATION_CATEGORY,
            60,
        )
        .placeholder("ed25519:<64 hex characters>")
        .hint("text")),
        _ => None,
    }
}

fn runtime_defaults_presentation(rendered: &str) -> Option<SettingPresentation> {
    match rendered {
        "defaults.throughput.threads" => Some(sp(
            "CPU threads",
            "Sets the default CPU thread count. Use 0 for auto; 256 is a safe UI ceiling for general-purpose systems.",
            RUNTIME_CATEGORY,
            10,
        )
        .unit("threads")
        .hint("range")),
        "defaults.throughput.threads_batch" => Some(sp(
            "Batch threads",
            "Sets the thread count used for batching. Use 0 for auto; 256 is a safe UI ceiling for general-purpose systems.",
            RUNTIME_CATEGORY,
            20,
        )
        .unit("threads")
        .hint("range")),
        "defaults.throughput.continuous_batching" => Some(sp(
            "Continuous batching",
            "Choose whether the runtime should keep batching continuously when supported.",
            RUNTIME_CATEGORY,
            30,
        )
        .hint("segmented")),
        "defaults.hardware.gpu_layers" => Some(sp(
            "GPU layers",
            "Set the GPU layer count, or use auto. The backend also accepts -1 to mean all layers.",
            RUNTIME_CATEGORY,
            40,
        )
        .placeholder("auto or integer layer count")
        .hint("text")),
        "defaults.throughput.parallel" => Some(sp(
            "Default slots / parallel requests",
            "Sets the default parallel slots for placements without their own value. More slots increase KV memory use.",
            RUNTIME_CATEGORY,
            50,
        )
        .unit("slots")
        .hint("range")
        .renderer("slot-meter")),
        "defaults.throughput.tuning_profile" => Some(sp(
            "Default tuning profile",
            "Choose the starting balance between throughput, batch size, and memory use.",
            RUNTIME_CATEGORY,
            60,
        )
        .hint("segmented")),
        "defaults.model_fit.flash_attention" => Some(sp(
            "Flash attention policy",
            "Choose the default attention kernel policy for compatible runtimes.",
            RUNTIME_CATEGORY,
            70,
        )
        .hint("segmented")),
        "defaults.hardware.device" => Some(sp(
            "Default GPU device",
            "Optional fallback device for pinned GPU assignment when a model does not set its own device.",
            RUNTIME_CATEGORY,
            90,
        )
        .placeholder("cuda:0 or CUDA0")
        .hint("text")),
        "defaults.model_fit.kv_cache_policy" => Some(sp(
            "KV cache policy",
            "Select how aggressively KV cache precision is reduced to fit larger contexts.",
            MEMORY_CATEGORY,
            10,
        )
        .hint("segmented")
        .renderer("kv-cache-policy")),
        "defaults.hardware.safety_margin_gb" => Some(sp(
            "Memory / safety margin",
            "Keep this much GPU memory free before placement fit checks pass.",
            MEMORY_CATEGORY,
            20,
        )
        .unit("GB")
        .hint("range")),
        "defaults.model_fit.ctx_size" => Some(sp(
            "Context window size",
            "Set the default context window size in tokens.",
            MEMORY_CATEGORY,
            30,
        )
        .unit("tokens")
        .hint("range")),
        "defaults.model_fit.batch" => Some(sp(
            "Batch size",
            "Set the default prefill batch size.",
            MEMORY_CATEGORY,
            40,
        )
        .unit("tokens")
        .hint("range")),
        "defaults.model_fit.ubatch" => Some(sp(
            "Micro-batch size",
            "Set the default decode micro-batch size.",
            MEMORY_CATEGORY,
            50,
        )
        .unit("tokens")
        .hint("range")),
        "defaults.model_fit.cache_type_k" => Some(sp(
            "KV cache type (K)",
            "Choose the KV cache dtype used for keys.",
            MEMORY_CATEGORY,
            60,
        )
        .hint("segmented")),
        "defaults.model_fit.cache_type_v" => Some(sp(
            "KV cache type (V)",
            "Choose the KV cache dtype used for values.",
            MEMORY_CATEGORY,
            70,
        )
        .hint("segmented")),
        _ => None,
    }
}

fn generation_defaults_presentation(rendered: &str) -> Option<SettingPresentation> {
    match rendered {
        "defaults.speculative.mode" => Some(
            sp(
                "Default speculation mode",
                "Choose the default speculation method, or leave the runtime in auto mode.",
                SPECULATIVE_CATEGORY,
                10,
            )
            .hint("segmented"),
        ),
        "defaults.speculative.draft_selection_policy" => Some(
            sp(
                "Default draft selection policy",
                "Choose how draft models are selected when draft-model speculation is active.",
                SPECULATIVE_CATEGORY,
                20,
            )
            .hint("toggle"),
        ),
        "defaults.speculative.pairing_fault" => Some(
            sp(
                "Incompatible pairing behavior",
                "Choose what happens when the draft and target models cannot pair.",
                SPECULATIVE_CATEGORY,
                30,
            )
            .hint("toggle"),
        ),
        "defaults.speculative.draft_max_tokens" => Some(
            sp(
                "Default draft max tokens",
                "Limit how many draft tokens can be proposed before verification.",
                SPECULATIVE_CATEGORY,
                40,
            )
            .unit("tokens")
            .hint("range"),
        ),
        "defaults.speculative.draft_min_tokens" => Some(
            sp(
                "Default draft minimum tokens",
                "Set the smallest draft batch attempted before verification.",
                SPECULATIVE_CATEGORY,
                50,
            )
            .unit("tokens")
            .hint("range"),
        ),
        "defaults.request_defaults.temperature" => Some(
            sp(
                "Temperature",
                "Fallback sampling temperature for requests that do not provide one.",
                REQUEST_DEFAULTS_CATEGORY,
                10,
            )
            .hint("range"),
        ),
        "defaults.request_defaults.top_p" => Some(
            sp(
                "Top-p",
                "Fallback nucleus sampling threshold for requests that omit one.",
                REQUEST_DEFAULTS_CATEGORY,
                20,
            )
            .hint("range"),
        ),
        "defaults.request_defaults.reasoning_format" => Some(
            sp(
                "Reasoning format",
                "Choose how thinking tokens appear in the response stream.",
                REQUEST_DEFAULTS_CATEGORY,
                30,
            )
            .hint("segmented"),
        ),
        "defaults.request_defaults.reasoning_budget" => Some(
            sp(
                "Reasoning budget",
                "Cap the reasoning tokens reserved before the final answer.",
                REQUEST_DEFAULTS_CATEGORY,
                40,
            )
            .unit("tok")
            .hint("range"),
        ),
        "defaults.request_defaults.repeat_penalty" => Some(
            sp(
                "Repeat penalty",
                "Adjust how strongly repeated tokens are discouraged.",
                REQUEST_DEFAULTS_CATEGORY,
                50,
            )
            .hint("range"),
        ),
        "defaults.request_defaults.repeat_last_n" => Some(
            sp(
                "Repeat last-n window",
                "Set how much recent token history the repeat penalty checks.",
                REQUEST_DEFAULTS_CATEGORY,
                60,
            )
            .unit("tok")
            .hint("range"),
        ),
        "defaults.request_defaults.top_k" => Some(
            sp(
                "Top-k",
                "Limit sampling to the top-k tokens.",
                REQUEST_DEFAULTS_CATEGORY,
                70,
            )
            .hint("range"),
        ),
        "defaults.request_defaults.min_p" => Some(
            sp(
                "Min-p",
                "Filter tokens below a dynamic probability floor.",
                REQUEST_DEFAULTS_CATEGORY,
                80,
            )
            .hint("range"),
        ),
        "defaults.request_defaults.presence_penalty" => Some(
            sp(
                "Presence penalty",
                "Increase or reduce the penalty for introducing new tokens.",
                REQUEST_DEFAULTS_CATEGORY,
                90,
            )
            .hint("range"),
        ),
        "defaults.request_defaults.frequency_penalty" => Some(
            sp(
                "Frequency penalty",
                "Increase or reduce the penalty for repeated tokens.",
                REQUEST_DEFAULTS_CATEGORY,
                100,
            )
            .hint("range"),
        ),
        "defaults.request_defaults.max_tokens" => Some(
            sp(
                "Max tokens",
                "Cap the number of generated tokens for a request.",
                REQUEST_DEFAULTS_CATEGORY,
                110,
            )
            .unit("tokens")
            .hint("range"),
        ),
        _ => None,
    }
}

fn skippy_multimodal_presentation(rendered: &str) -> Option<SettingPresentation> {
    match rendered {
        "defaults.skippy.activation_wire_dtype" => Some(sp(
            "Activation wire dtype",
            "Choose the dtype used when activation frames travel between skippy stages.",
            SKIPPY_CATEGORY,
            10,
        )
        .hint("segmented")),
        "defaults.skippy.stage_model_path" => Some(sp(
            "Stage model path",
            "Set the model or package path used for this skippy stage.",
            SKIPPY_CATEGORY,
            20,
        )
        .placeholder("hf://... or /path/to/stage.gguf")
        .hint("text")),
        "defaults.skippy.stage_role" => Some(sp(
            "Stage role",
            "Choose the stage-chain role when topology is not inferred automatically.",
            SKIPPY_CATEGORY,
            30,
        )
        .hint("select")),
        "defaults.skippy.stage_topology" => Some(sp(
            "Stage topology",
            "Describe the stage chain topology when it is supplied as a text override.",
            SKIPPY_CATEGORY,
            40,
        )
        .placeholder("topology name or path")
        .hint("text")),
        "defaults.skippy.prefill_chunking" => Some(sp(
            "Prefill chunking",
            "Choose how prefill chunks are scheduled across a skippy stage chain.",
            SKIPPY_CATEGORY,
            50,
        )
        .hint("select")),
        "defaults.skippy.prefill_chunk_size" => Some(sp(
            "Prefill chunk size",
            "Set the fixed prefill chunk size. Use 0 to keep the backend auto sentinel.",
            SKIPPY_CATEGORY,
            60,
        )
        .unit("tokens")
        .hint("range")),
        "defaults.skippy.prefill_chunk_schedule" => Some(sp(
            "Prefill chunk schedule",
            "Provide a comma-separated schedule for scheduled prefill chunking.",
            SKIPPY_CATEGORY,
            70,
        )
        .placeholder("e.g. 512,1024,2048")
        .hint("text")),
        "defaults.skippy.binary_stage_transport" => Some(sp(
            "Binary stage transport",
            "Choose whether the binary stage transport is enabled or left to auto selection.",
            SKIPPY_CATEGORY,
            80,
        )
        .hint("segmented")),
        "defaults.multimodal.mmproj_offload" => Some(sp(
            "MMProj offload",
            "Choose whether the multimodal projector stays auto-managed or explicitly on or off.",
            MULTIMODAL_CATEGORY,
            10,
        )
        .hint("segmented")),
        "defaults.multimodal.image_min_tokens" => Some(sp(
            "Image minimum tokens",
            "Set the minimum token budget reserved for each image input.",
            MULTIMODAL_CATEGORY,
            20,
        )
        .unit("tokens")
        .hint("range")),
        "defaults.multimodal.image_max_tokens" => Some(sp(
            "Image maximum tokens",
            "Set the maximum token budget allowed for each image input.",
            MULTIMODAL_CATEGORY,
            30,
        )
        .unit("tokens")
        .hint("range")),
        "defaults.multimodal.mmproj" => Some(sp(
            "MMProj path",
            "Set an explicit local path to the multimodal projector file.",
            MULTIMODAL_CATEGORY,
            40,
        )
        .placeholder("e.g. /path/to/mmproj.gguf")
        .hint("text")),
        "defaults.multimodal.mmproj_url" => Some(sp(
            "MMProj URL",
            "Set a URL used to download or reference the multimodal projector file.",
            MULTIMODAL_CATEGORY,
            50,
        )
        .placeholder("e.g. https://example.com/mmproj.gguf")
        .hint("text")),
        "defaults.advanced.server.alias" => Some(sp(
            "Server alias",
            "Set a human-friendly alias for the server in advanced deployments.",
            ADVANCED_SERVER_CATEGORY,
            10,
        )
        .placeholder("model alias")
        .hint("text")),
        _ => None,
    }
}

fn model_and_plugin_presentation(rendered: &str) -> Option<SettingPresentation> {
    match rendered {
        "models.<model-ref>.model" => Some(
            sp(
                "Model",
                "Model reference for this local placement.",
                RUNTIME_CATEGORY,
                10,
            )
            .renderer("model-placement-model"),
        ),
        "models.<model-ref>.model_fit.ctx_size" => Some(
            sp(
                "Context window size",
                "Context window size for this local placement.",
                MEMORY_CATEGORY,
                20,
            )
            .unit("tokens")
            .renderer("model-placement-context"),
        ),
        "models.<model-ref>.hardware.device" => Some(
            sp(
                "GPU device",
                "Device assignment for this local placement.",
                RUNTIME_CATEGORY,
                30,
            )
            .placeholder("cuda:0")
            .renderer("model-placement-device"),
        ),
        "models.<model-ref>.hardware.gpu_layers" => Some(
            sp(
                "GPU layers",
                "GPU layer count for this local placement.",
                RUNTIME_CATEGORY,
                40,
            )
            .placeholder("-1")
            .renderer("model-placement-gpu-layers"),
        ),
        "plugin.<plugin-name>.enabled" => Some(
            sp(
                "Enabled",
                "Enable or disable the plugin entry.",
                PLUGIN_HOST_CATEGORY,
                10,
            )
            .hint("toggle"),
        ),
        "plugin.<plugin-name>.web_ui_enabled" => Some(
            sp(
                "Web UI projection",
                "Show or hide the plugin's declared web UI without disabling the plugin process.",
                PLUGIN_HOST_CATEGORY,
                15,
            )
            .hint("toggle"),
        ),
        "plugin.<plugin-name>.url" => Some(
            sp(
                "Base URL",
                "URL used by endpoint-style plugins.",
                PLUGIN_HOST_CATEGORY,
                20,
            )
            .placeholder("http://localhost:8000/v1")
            .hint("text"),
        ),
        "plugin.<plugin-name>.command" => Some(
            sp(
                "Plugin command",
                "Optional path to the plugin binary when it is not on PATH.",
                PLUGIN_HOST_CATEGORY,
                30,
            )
            .placeholder("use bundled plugin binary")
            .hint("text"),
        ),
        "plugin.<plugin-name>.args" => Some(
            sp(
                "Args",
                "Additional CLI args passed to the plugin process.",
                PLUGIN_HOST_CATEGORY,
                40,
            )
            .placeholder("comma-separated CLI arguments")
            .hint("text"),
        ),
        "plugin.<plugin-name>.startup.connect_timeout_secs" => Some(
            sp(
                "Connect timeout",
                "Seconds to wait for the plugin transport connection.",
                PLUGIN_HOST_CATEGORY,
                50,
            )
            .unit("sec")
            .hint("number"),
        ),
        "plugin.<plugin-name>.startup.init_timeout_secs" => Some(
            sp(
                "Init timeout",
                "Seconds to wait for plugin initialization.",
                PLUGIN_HOST_CATEGORY,
                60,
            )
            .unit("sec")
            .hint("number"),
        ),
        "plugin.<plugin-name>.startup.optional" => Some(
            sp(
                "Optional startup",
                "Allow the host to continue when the plugin cannot start.",
                PLUGIN_HOST_CATEGORY,
                70,
            )
            .hint("toggle"),
        ),
        "plugin.<plugin-name>.startup.lazy_start" => Some(
            sp(
                "Lazy start",
                "Delay plugin startup until the plugin is first needed.",
                PLUGIN_HOST_CATEGORY,
                80,
            )
            .hint("toggle"),
        ),
        _ => None,
    }
}

fn sp(
    label: &'static str,
    help: &'static str,
    category: CategoryPresentation,
    order: u32,
) -> SettingPresentation {
    SettingPresentation {
        label,
        help,
        category,
        order,
        unit: None,
        placeholder: None,
        control_hint: None,
        renderer_id: None,
    }
}

impl SettingPresentation {
    fn unit(mut self, unit: &'static str) -> Self {
        self.unit = Some(unit);
        self
    }

    fn placeholder(mut self, placeholder: &'static str) -> Self {
        self.placeholder = Some(placeholder);
        self
    }

    fn hint(mut self, control_hint: &'static str) -> Self {
        self.control_hint = Some(control_hint);
        self
    }

    fn renderer(mut self, renderer_id: &'static str) -> Self {
        self.renderer_id = Some(renderer_id);
        self
    }
}

pub(super) fn apply_built_in_presentation_metadata(setting: &mut ConfigSettingSchema) {
    let rendered = setting.path.render();
    let Some(presentation) = setting_presentation_for_path(&rendered) else {
        apply_fallback_presentation_metadata(setting, &rendered);
        return;
    };

    setting.description = Some(presentation.help.to_string());
    if presentation.category.id != ADVANCED_SERVER_CATEGORY.id {
        setting.visibility = ConfigVisibility::User;
    }
    setting.presentation = Some(ConfigPresentationMetadata {
        label: Some(presentation.label.to_string()),
        help: Some(presentation.help.to_string()),
        category_id: Some(presentation.category.id.to_string()),
        category_label: Some(presentation.category.label.to_string()),
        category_summary: Some(presentation.category.summary.to_string()),
        category_order: Some(presentation.category.order),
        setting_order: Some(presentation.order),
        unit: presentation.unit.map(str::to_string),
        placeholder: presentation.placeholder.map(str::to_string),
        control_hint: presentation.control_hint.map(str::to_string),
        renderer_id: presentation.renderer_id.map(str::to_string),
    });
}

fn apply_fallback_presentation_metadata(setting: &mut ConfigSettingSchema, rendered: &str) {
    let Some(category) = fallback_category_for_path(rendered) else {
        return;
    };
    let key = rendered.rsplit('.').next().unwrap_or(rendered);
    let order = fallback_setting_order(rendered);
    let label = title_case_config_key(key);

    setting.presentation = Some(ConfigPresentationMetadata {
        label: Some(label),
        help: setting.description.clone(),
        category_id: Some(category.id.to_string()),
        category_label: Some(category.label.to_string()),
        category_summary: Some(category.summary.to_string()),
        category_order: Some(category.order),
        setting_order: Some(order),
        unit: unit_for_path(rendered).map(str::to_string),
        placeholder: placeholder_for_path(rendered).map(str::to_string),
        control_hint: control_hint_for_schema(&setting.value_schema).map(str::to_string),
        renderer_id: None,
    });
}

fn fallback_category_for_path(rendered: &str) -> Option<CategoryPresentation> {
    if rendered.starts_with("gpu.") {
        return Some(MESHLLM_CATEGORY);
    }
    if rendered.starts_with("telemetry.") {
        return Some(TELEMETRY_CATEGORY);
    }
    if rendered.starts_with("runtime.") {
        return Some(RUNTIME_POLICY_CATEGORY);
    }
    if rendered.starts_with("owner_control.") {
        return Some(NETWORK_CATEGORY);
    }
    if rendered.starts_with("mesh_requirements.") {
        return Some(ATTESTATION_CATEGORY);
    }
    if rendered.starts_with("plugin.<plugin-name>.") {
        return Some(PLUGIN_HOST_CATEGORY);
    }
    if rendered.starts_with("models.<model-ref>.model_fit.") {
        return Some(MEMORY_CATEGORY);
    }
    if rendered.starts_with("models.<model-ref>.hardware.") {
        return Some(RUNTIME_CATEGORY);
    }
    if rendered.starts_with("models.<model-ref>.") {
        return Some(RUNTIME_CATEGORY);
    }
    if rendered.starts_with("defaults.speculative.") {
        return Some(SPECULATIVE_CATEGORY);
    }
    if rendered.starts_with("defaults.request_defaults.") {
        return Some(REQUEST_DEFAULTS_CATEGORY);
    }
    if rendered.starts_with("defaults.skippy.") {
        return Some(SKIPPY_CATEGORY);
    }
    if rendered.starts_with("defaults.multimodal.") {
        return Some(MULTIMODAL_CATEGORY);
    }
    if rendered.starts_with("defaults.advanced.server.") {
        return Some(ADVANCED_SERVER_CATEGORY);
    }
    if rendered.starts_with("defaults.model_fit.") {
        return Some(MEMORY_CATEGORY);
    }
    if rendered.starts_with("defaults.hardware.safety_margin_gb") {
        return Some(MEMORY_CATEGORY);
    }
    if rendered.starts_with("defaults.hardware.") || rendered.starts_with("defaults.throughput.") {
        return Some(RUNTIME_CATEGORY);
    }
    None
}

fn title_case_config_key(key: &str) -> String {
    key.replace('_', " ")
        .split_whitespace()
        .map(|word| {
            let mut chars = word.chars();
            match chars.next() {
                Some(first) => format!("{}{}", first.to_uppercase(), chars.as_str()),
                None => String::new(),
            }
        })
        .collect::<Vec<_>>()
        .join(" ")
}

fn fallback_setting_order(rendered: &str) -> u32 {
    rendered.bytes().fold(0u32, |acc, byte| {
        acc.wrapping_mul(31).wrapping_add(byte as u32)
    })
}

fn unit_for_path(rendered: &str) -> Option<&'static str> {
    let leaf = rendered.rsplit('.').next()?;
    match leaf {
        "ctx_size" | "batch" | "ubatch" | "max_tokens" | "draft_max_tokens"
        | "draft_min_tokens" | "image_min_tokens" | "image_max_tokens" | "prefill_chunk_size" => {
            Some("tokens")
        }
        "threads" | "threads_batch" | "draft_threads" => Some("threads"),
        "parallel" | "cache_idle_slots" => Some("slots"),
        "safety_margin_gb" => Some("GB"),
        "cache_ram_mib" | "fit_target_mib" => Some("MiB"),
        "lifecycle_startup_timeout_ms"
        | "lifecycle_readiness_interval_ms"
        | "lifecycle_health_interval_ms" => Some("ms"),
        _ => None,
    }
}

fn placeholder_for_path(rendered: &str) -> Option<&'static str> {
    let leaf = rendered.rsplit('.').next()?;
    match leaf {
        "device" => Some("cuda:0 or CUDA0"),
        "gpu_layers" => Some("auto or integer layer count"),
        "tensor_split" => Some("e.g. 0.5,0.5"),
        "cpu_affinity" => Some("e.g. 0-3,8-11"),
        "priority" => Some("e.g. 0 or normal"),
        "stage_model_path" => Some("hf://... or /path/to/stage.gguf"),
        "stage_topology" => Some("topology name or path"),
        "prefill_chunk_schedule" => Some("e.g. 512,1024,2048"),
        "mmproj" => Some("/path/to/mmproj.gguf"),
        "mmproj_url" => Some("https://example.com/mmproj.gguf"),
        "server.alias" | "alias" => Some("model alias"),
        "command" => Some("use bundled plugin binary"),
        "args" => Some("comma-separated CLI arguments"),
        "url" => Some("http://localhost:8000/v1"),
        _ => None,
    }
}

fn control_hint_for_schema(schema: &ConfigValueSchema) -> Option<&'static str> {
    match schema {
        ConfigValueSchema::Boolean => Some("toggle"),
        ConfigValueSchema::Integer | ConfigValueSchema::Float => Some("number"),
        ConfigValueSchema::Enum { values }
            if values.len() <= MAX_ENUM_VALUES_FOR_SEGMENTED_CONTROL =>
        {
            Some("segmented")
        }
        ConfigValueSchema::Enum { .. } => Some("select"),
        ConfigValueSchema::OneOf { variants } if variants.iter().any(is_boolean_schema) => {
            Some("segmented")
        }
        ConfigValueSchema::Array { .. } => Some("text"),
        ConfigValueSchema::Object => Some("textarea"),
        _ => None,
    }
}

fn is_boolean_schema(schema: &ConfigValueSchema) -> bool {
    matches!(schema, ConfigValueSchema::Boolean)
}