par-term-scripting 0.2.0

Scripting and observer system for par-term terminal emulator
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
//! Plugin manifest types, discovery, and validation.
//!
//! A plugin is a directory under the plugins root whose name equals its manifest
//! `id`, containing a `manifest.json` and the executable its entry points name.
//! Discovery is deliberately forgiving: one broken manifest produces a warning
//! and is skipped, never blocking the discovery of its neighbours. A plugin is
//! never spawned as a side effect of discovery — the host spawns only what the
//! enabled set names, which is what makes "lands disabled" structural.

use std::collections::{HashMap, HashSet};
use std::path::{Path, PathBuf};

use par_term_config::{RestartPolicy, StatusBarSection};
use serde::{Deserialize, Serialize};

/// The status-bar-widget plugin kind. Unknown kinds in a manifest skip the
/// whole plugin with a visible warning rather than loading partially.
pub const KIND_STATUS_BAR_WIDGET: &str = "status-bar-widget";

/// The action-contributor plugin kind: the plugin contributes entries to
/// the command palette.
pub const KIND_ACTION_CONTRIBUTOR: &str = "action-contributor";

/// The panel plugin kind: the plugin pushes markdown panel content through
/// the `SetPanel`/`ClearPanel` protocol commands (the same surface a tab
/// script's `SetPanel` drives), rendered in the Settings plugins section.
pub const KIND_PANEL: &str = "panel";

/// The overlay plugin kind: the plugin owns a persistent surface drawn over
/// the terminal through the `SetOverlay`/`ClearOverlay` protocol commands
/// (design: docs/plans/2026-09-24-overlay-plugin-design.md). Phase 1 is
/// display-only; `interactive` requires the `overlay.interactive` capability
/// and is forced off without it.
pub const KIND_OVERLAY: &str = "overlay";

/// Entry-point map key for the [`KIND_STATUS_BAR_WIDGET`] kind.
pub const ENTRY_POINT_STATUS_BAR_WIDGET: &str = "statusBarWidget";

/// Entry-point map key for the [`KIND_ACTION_CONTRIBUTOR`] kind.
pub const ENTRY_POINT_ACTION_CONTRIBUTOR: &str = "actionContributor";

/// Entry-point map key for the [`KIND_PANEL`] kind.
pub const ENTRY_POINT_PANEL: &str = "panel";

/// Entry-point map key for the [`KIND_OVERLAY`] kind.
pub const ENTRY_POINT_OVERLAY: &str = "overlay";

/// Activation policy declared by the manifest.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Default, Serialize, Deserialize)]
#[serde(rename_all = "snake_case")]
pub enum PluginActivation {
    /// Discovered but never auto-started; the user enables it in Settings.
    #[default]
    Manual,
    /// Starts with the host once enabled (still gated by the enabled set).
    OnStartup,
}

/// A setting's value type, as declared in a manifest's settings schema.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
#[serde(rename_all = "snake_case")]
pub enum SettingType {
    /// Free-form text.
    String,
    /// Whole number; JSON floats with a fraction are rejected.
    Integer,
    /// Any JSON number.
    Number,
    /// True/false.
    Boolean,
}

impl SettingType {
    /// Human name used in validation error messages.
    pub fn as_str(&self) -> &'static str {
        match self {
            SettingType::String => "string",
            SettingType::Integer => "integer",
            SettingType::Number => "number",
            SettingType::Boolean => "boolean",
        }
    }
}

/// One typed setting declared by a plugin's settings schema.
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct SettingSchemaEntry {
    /// Setting key; must be non-empty and unique within the schema.
    pub key: String,
    /// Value type (serialized as `type`).
    #[serde(rename = "type")]
    pub setting_type: SettingType,
    /// Settings-editor label.
    pub label: String,
    /// Inclusive lower bound for numeric settings.
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub min: Option<f64>,
    /// Inclusive upper bound for numeric settings.
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub max: Option<f64>,
    /// Drag step for numeric settings.
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub step: Option<f64>,
    /// Value used when the persisted settings omit the key.
    pub default_value: serde_json::Value,
}

/// The `statusBarWidget` manifest block: how a status-bar-widget plugin
/// presents itself and what the user can configure.
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct StatusBarWidgetKind {
    /// Name shown in the status-bar Settings list.
    pub display_name: String,
    /// Section the widget entry lands in when first enabled; the user can
    /// move it afterwards. Defaults to the right section.
    #[serde(default = "default_widget_section")]
    pub section: StatusBarSection,
    /// Settings values used when the user has configured nothing.
    #[serde(default)]
    pub defaults: HashMap<String, serde_json::Value>,
    /// Typed settings the host renders an editor for.
    pub schema: Vec<SettingSchemaEntry>,
}

fn default_widget_section() -> StatusBarSection {
    StatusBarSection::Right
}

/// One action a plugin contributes to the command palette when it declares
/// the [`KIND_ACTION_CONTRIBUTOR`] kind.
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct ActionContribution {
    /// Action id; must match `[a-zA-Z0-9_-]+` and be unique within the
    /// manifest.
    pub id: String,
    /// Text shown in the palette entry.
    pub label: String,
    /// Longer explanation shown alongside the label.
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub description: Option<String>,
}

/// One named executable entry point. `command` is relative to the plugin
/// directory only — never a PATH lookup, never allowed to escape the
/// directory (enforced by [`discover_plugins`]).
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct PluginEntryPoint {
    /// Executable file name relative to the plugin directory.
    pub command: String,
    /// Extra argv passed before the host's own settings argument.
    #[serde(default)]
    pub args: Vec<String>,
}

/// A parsed `manifest.json`.
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct PluginManifest {
    /// Manifest format version; only 1 is understood.
    pub schema_version: u32,
    /// Reverse-DNS-ish plugin id; must equal the directory name.
    pub id: String,
    /// Human-readable name.
    pub name: String,
    /// Plugin version, displayed at the enable toggle.
    pub version: String,
    /// Author, displayed at the enable toggle (trust surface).
    #[serde(default)]
    pub author: Option<String>,
    /// License, displayed at the enable toggle (trust surface).
    #[serde(default)]
    pub license: Option<String>,
    /// What the plugin does, shown in Settings.
    #[serde(default)]
    pub description: Option<String>,
    /// Kinds this plugin provides; each must be known to this host.
    #[serde(default)]
    pub kinds: Vec<String>,
    /// When the plugin starts once enabled.
    #[serde(default)]
    pub activation: PluginActivation,
    /// Named entry points; the key selects the kind's executable.
    #[serde(default)]
    pub entry_points: HashMap<String, PluginEntryPoint>,
    /// Required iff `kinds` contains [`KIND_STATUS_BAR_WIDGET`].
    #[serde(default)]
    pub status_bar_widget: Option<StatusBarWidgetKind>,
    /// Actions contributed to the command palette; required non-empty iff
    /// `kinds` contains [`KIND_ACTION_CONTRIBUTOR`], ignored otherwise.
    #[serde(default)]
    pub actions: Vec<ActionContribution>,
    /// Terminal event kinds the plugin wants delivered to its stdin. Empty or
    /// absent means self-scheduled: the plugin receives no terminal events
    /// (unlike tab scripts, where an empty subscription list means all
    /// events). Each name must be one of
    /// [`crate::observer::EVENT_KINDS`]; an unknown or duplicate name skips
    /// the plugin at discovery so a typo'd subscription can never silently
    /// never-fire.
    #[serde(default)]
    pub subscriptions: Vec<String>,
    /// When the host restarts the plugin's exited process: `on_failure`
    /// (the default and the only behaviour before the field existed),
    /// `never`, or `always` — the same policy enum tab scripts configure.
    /// Mode-only by design: backoff parameters (restart delay, crash-loop
    /// cap) are host-owned safety policy a manifest cannot tune its way
    /// out of (parsight decision 95).
    #[serde(default = "default_plugin_restart")]
    pub restart: RestartPolicy,
    /// Overlay-kind capability block (phase 2). Absent means display-only:
    /// `interactive: true` requests are forced off at ingest. Present with
    /// `interactive: true` is required before an overlay may focus and
    /// render interactive widgets.
    #[serde(default)]
    pub overlay: Option<OverlayCapability>,
}

/// Capabilities an `overlay`-kind manifest may claim.
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct OverlayCapability {
    /// Whether the plugin's overlays may request interactivity (focus,
    /// semantic events). Defaults to false — a manifest that declares the
    /// block without the flag still gets display-only overlays.
    #[serde(default)]
    pub interactive: bool,
}

/// The manifest default for [`PluginManifest::restart`] — today's
/// hardcoded behaviour, NOT the config enum's derived default (`Never` is
/// the scripts-config default; a plugin without the field must keep
/// restarting on failure exactly as it always has).
fn default_plugin_restart() -> RestartPolicy {
    RestartPolicy::OnFailure
}

/// A plugin that passed validation, with confinement-checked paths.
#[derive(Debug, Clone, PartialEq)]
pub struct DiscoveredPlugin {
    /// The parsed manifest.
    pub manifest: PluginManifest,
    /// Canonicalized plugin directory.
    pub dir: PathBuf,
    /// Canonicalized, confinement-checked entry executable for the widget
    /// kind. A plugin declaring only the action-contributor kind has no
    /// widget entry and carries its action entry here.
    pub entry_path: PathBuf,
    /// Canonicalized, confinement-checked entry executable for the
    /// action-contributor kind; `None` when the plugin does not declare
    /// that kind.
    pub action_entry_path: Option<PathBuf>,
    /// Canonicalized, confinement-checked entry executable for the panel
    /// kind; `None` when the plugin does not declare that kind.
    pub panel_entry_path: Option<PathBuf>,
    /// Canonicalized, confinement-checked entry executable for the overlay
    /// kind; `None` when the plugin does not declare that kind.
    pub overlay_entry_path: Option<PathBuf>,
}

/// Why a candidate plugin directory was skipped.
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
pub struct DiscoveryWarning {
    /// Directory name under the plugins root.
    pub dir: String,
    /// Human-readable skip reason.
    pub reason: String,
}

/// Scan a plugins root for valid plugin directories.
///
/// An absent root is an empty result, not a warning — a machine with no
/// plugins installed yet has not done anything wrong. Directories are visited
/// in name order so discovery output is deterministic.
pub fn discover_plugins(root: &Path) -> (Vec<DiscoveredPlugin>, Vec<DiscoveryWarning>) {
    let mut plugins = Vec::new();
    let mut warnings = Vec::new();

    let Ok(entries) = std::fs::read_dir(root) else {
        return (plugins, warnings);
    };

    let mut dirs: Vec<PathBuf> = entries
        .filter_map(|e| e.ok())
        .filter(|e| e.file_type().is_ok_and(|t| t.is_dir()))
        .map(|e| e.path())
        .collect();
    dirs.sort();

    for dir in dirs {
        let name = dir
            .file_name()
            .map(|n| n.to_string_lossy().into_owned())
            .unwrap_or_default();
        match validate_plugin_dir(&dir) {
            Ok(plugin) => plugins.push(plugin),
            Err(reason) => warnings.push(DiscoveryWarning { dir: name, reason }),
        }
    }

    (plugins, warnings)
}

/// Parse and fully validate one candidate plugin directory.
pub(crate) fn validate_plugin_dir(dir: &Path) -> Result<DiscoveredPlugin, String> {
    let manifest_path = dir.join("manifest.json");
    let raw = std::fs::read_to_string(&manifest_path)
        .map_err(|e| format!("missing or unreadable manifest.json: {e}"))?;
    let manifest: PluginManifest =
        serde_json::from_str(&raw).map_err(|e| format!("invalid manifest JSON: {e}"))?;

    if manifest.schema_version != 1 {
        return Err(format!(
            "unsupported schemaVersion {} (expected 1)",
            manifest.schema_version
        ));
    }

    let dir_name = dir
        .file_name()
        .map(|n| n.to_string_lossy().into_owned())
        .unwrap_or_default();
    if manifest.id.is_empty() || manifest.id != dir_name {
        return Err(format!(
            "manifest id `{}` does not match directory name `{dir_name}`",
            manifest.id
        ));
    }

    if manifest.kinds.is_empty() {
        return Err("no kinds declared".to_string());
    }
    let known_kinds = [
        KIND_STATUS_BAR_WIDGET,
        KIND_ACTION_CONTRIBUTOR,
        KIND_PANEL,
        KIND_OVERLAY,
    ];
    let unknown: Vec<&str> = manifest
        .kinds
        .iter()
        .map(String::as_str)
        .filter(|k| !known_kinds.contains(k))
        .collect();
    if !unknown.is_empty() {
        return Err(format!(
            "unknown kinds: {} (known: {})",
            unknown.join(", "),
            known_kinds.join(", ")
        ));
    }
    let has_widget = manifest.kinds.iter().any(|k| k == KIND_STATUS_BAR_WIDGET);
    let has_action = manifest.kinds.iter().any(|k| k == KIND_ACTION_CONTRIBUTOR);
    let has_panel = manifest.kinds.iter().any(|k| k == KIND_PANEL);
    let has_overlay = manifest.kinds.iter().any(|k| k == KIND_OVERLAY);

    // A subscription naming a kind the forwarder can never produce would sit
    // inert for the plugin's whole life, so it is rejected at discovery like
    // every other manifest fault. Duplicates are rejected for the same
    // reason as duplicate action ids: an authoring error, not a filter.
    let mut seen_subscriptions: HashSet<&str> = HashSet::new();
    for kind in &manifest.subscriptions {
        if !super::observer::EVENT_KINDS.contains(&kind.as_str()) {
            return Err(format!(
                "unknown subscription kind `{kind}` (valid kinds are the script event \
                 vocabulary, e.g. bell_rang, cwd_changed, command_complete)"
            ));
        }
        if !seen_subscriptions.insert(kind.as_str()) {
            return Err(format!("duplicate subscription kind `{kind}`"));
        }
    }

    // Each declared kind's requirements run only when that kind is present:
    // a both-kinds manifest must satisfy both pairs, an action-only manifest
    // owes no widget block, and vice versa.
    if has_widget {
        let widget = manifest.status_bar_widget.as_ref().ok_or_else(|| {
            format!("{KIND_STATUS_BAR_WIDGET} kind requires a statusBarWidget block")
        })?;
        if widget.schema.is_empty() {
            return Err("statusBarWidget.schema must declare at least one setting".to_string());
        }
        if widget.schema.iter().any(|e| e.key.is_empty()) {
            return Err("statusBarWidget.schema entries must have non-empty keys".to_string());
        }
    }

    if has_action {
        if manifest.actions.is_empty() {
            return Err(format!(
                "{KIND_ACTION_CONTRIBUTOR} kind requires at least one action in `actions`"
            ));
        }
        let mut seen: HashSet<&str> = HashSet::new();
        for action in &manifest.actions {
            // A colon would collide with the wire format's id separator
            // (design D1); ids stay `[a-zA-Z0-9_-]+`.
            if action.id.is_empty()
                || !action
                    .id
                    .chars()
                    .all(|c| c.is_ascii_alphanumeric() || c == '_' || c == '-')
            {
                return Err(format!(
                    "action id `{}` must match [a-zA-Z0-9_-]+",
                    action.id
                ));
            }
            if !seen.insert(action.id.as_str()) {
                return Err(format!("duplicate action id `{}`", action.id));
            }
            if action.label.is_empty() {
                return Err(format!(
                    "action `{}` must have a non-empty label",
                    action.id
                ));
            }
        }
    }

    // The security line: each declared kind's entry command resolves inside
    // the plugin directory only — canonicalize both sides and require
    // containment, so a `..` or symlink escape cannot point execution
    // outside the plugin.
    let dir_canon =
        std::fs::canonicalize(dir).map_err(|e| format!("cannot read plugin dir: {e}"))?;
    let widget_entry = if has_widget {
        let entry = manifest.entry_points.get(ENTRY_POINT_STATUS_BAR_WIDGET).ok_or_else(|| {
            format!(
                "{KIND_STATUS_BAR_WIDGET} kind requires entryPoints.{ENTRY_POINT_STATUS_BAR_WIDGET}"
            )
        })?;
        Some(confinement_check(&dir_canon, &entry.command)?)
    } else {
        None
    };
    let action_entry = if has_action {
        let entry = manifest.entry_points.get(ENTRY_POINT_ACTION_CONTRIBUTOR).ok_or_else(|| {
            format!(
                "{KIND_ACTION_CONTRIBUTOR} kind requires entryPoints.{ENTRY_POINT_ACTION_CONTRIBUTOR}"
            )
        })?;
        Some(confinement_check(&dir_canon, &entry.command)?)
    } else {
        None
    };
    // The panel kind owes no manifest block beyond its entry point — the
    // pushed content is entirely the process's decision at runtime.
    let panel_entry = if has_panel {
        let entry = manifest
            .entry_points
            .get(ENTRY_POINT_PANEL)
            .ok_or_else(|| format!("{KIND_PANEL} kind requires entryPoints.{ENTRY_POINT_PANEL}"))?;
        Some(confinement_check(&dir_canon, &entry.command)?)
    } else {
        None
    };
    // The overlay kind owes no manifest block beyond its entry point — the
    // pushed scene is entirely the process's decision at runtime (same
    // shape as the panel kind).
    let overlay_entry = if has_overlay {
        let entry = manifest
            .entry_points
            .get(ENTRY_POINT_OVERLAY)
            .ok_or_else(|| {
                format!("{KIND_OVERLAY} kind requires entryPoints.{ENTRY_POINT_OVERLAY}")
            })?;
        Some(confinement_check(&dir_canon, &entry.command)?)
    } else {
        None
    };

    // `kinds` is non-empty and every kind is known, so at least one entry
    // resolved; the widget entry is the primary entry when several kinds
    // are declared.
    let entry_path = widget_entry
        .or(action_entry.clone())
        .or(panel_entry.clone())
        .or(overlay_entry.clone())
        .expect("a validated manifest resolves at least one entry point");

    Ok(DiscoveredPlugin {
        manifest,
        dir: dir_canon,
        entry_path,
        action_entry_path: action_entry,
        panel_entry_path: panel_entry,
        overlay_entry_path: overlay_entry,
    })
}

/// Canonicalize one entry command and enforce the confinement rules every
/// kind's entry shares: the resolved path must stay inside the plugin
/// directory (no `..` or symlink escape) and, on Unix, carry the exec bit
/// unless it is a `.py` entry.
///
/// `.py` entries run through the resolved Python interpreter (the same
/// `spawn_command` routing), so the file itself never needs the exec bit —
/// requiring it would silently reject every non-chmodded script plugin on
/// Unix while Windows (no exec bit) accepts it.
fn confinement_check(dir_canon: &Path, command: &str) -> Result<PathBuf, String> {
    let entry_path = dir_canon.join(command);
    let entry_canon = std::fs::canonicalize(&entry_path)
        .map_err(|_| format!("entry point `{command}` not found"))?;
    if !entry_canon.starts_with(dir_canon) {
        return Err(format!(
            "entry point `{command}` escapes the plugin directory"
        ));
    }
    #[cfg(unix)]
    {
        use std::os::unix::fs::PermissionsExt;
        if !command.ends_with(".py") {
            let mode = std::fs::metadata(&entry_canon)
                .map_err(|e| format!("entry point unreadable: {e}"))?
                .permissions()
                .mode();
            if mode & 0o111 == 0 {
                return Err(format!("entry point `{command}` is not executable"));
            }
        }
    }
    Ok(entry_canon)
}

/// Validate persisted settings against a manifest schema.
///
/// Absent keys are filled from the schema defaults; keys the schema no longer
/// declares are dropped (a plugin update must not strand a user's config);
/// values of the wrong type or outside `min`/`max` are errors the caller
/// surfaces instead of spawning with garbage.
pub fn validate_settings(
    schema: &[SettingSchemaEntry],
    values: &HashMap<String, serde_json::Value>,
) -> Result<HashMap<String, serde_json::Value>, String> {
    let mut out: HashMap<String, serde_json::Value> = schema
        .iter()
        .map(|e| (e.key.clone(), e.default_value.clone()))
        .collect();

    for (key, value) in values {
        let Some(entry) = schema.iter().find(|e| &e.key == key) else {
            continue;
        };
        if !value_matches_type(value, entry.setting_type) {
            return Err(format!(
                "setting `{key}` expects {}, got {value}",
                entry.setting_type.as_str()
            ));
        }
        if matches!(
            entry.setting_type,
            SettingType::Integer | SettingType::Number
        ) {
            if let Some(n) = value.as_f64()
                && let Some(min) = entry.min
                && n < min
            {
                return Err(format!(
                    "setting `{key}` value {n} is below the minimum {min}"
                ));
            }
            if let Some(n) = value.as_f64()
                && let Some(max) = entry.max
                && n > max
            {
                return Err(format!(
                    "setting `{key}` value {n} is above the maximum {max}"
                ));
            }
        }
        out.insert(key.clone(), value.clone());
    }

    Ok(out)
}

/// Whether a JSON value fits a declared setting type.
fn value_matches_type(value: &serde_json::Value, setting_type: SettingType) -> bool {
    match setting_type {
        SettingType::String => value.is_string(),
        SettingType::Integer => value.is_u64() || value.is_i64(),
        SettingType::Number => value.is_number(),
        SettingType::Boolean => value.is_boolean(),
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use std::fs;
    use tempfile::TempDir;

    /// The design doc's D1 example manifest (§3), with real strings for the
    /// ellipsis fields, as written to a fixture plugin directory.
    const DESIGN_D1_MANIFEST: &str = r#"{
        "schemaVersion": 1,
        "id": "com.example.agents-usage",
        "name": "Agent Usage",
        "version": "0.1.0",
        "author": "A Plugin Author",
        "license": "MIT",
        "description": "Shows agent usage in the status bar",
        "kinds": ["status-bar-widget"],
        "activation": "manual",
        "entryPoints": { "statusBarWidget": { "command": "agent-widget", "args": [] } },
        "statusBarWidget": {
            "displayName": "Agents",
            "section": "right",
            "defaults": { "refreshIntervalSec": 900 },
            "schema": [
                { "key": "refreshIntervalSec", "type": "integer", "label": "Refresh interval (seconds)",
                  "min": 30, "max": 3600, "step": 30, "defaultValue": 900 }
            ]
        }
    }"#;

    /// Minimal valid manifest, used as the base most mutations start from.
    /// `{id}` is replaced with the fixture directory name by [`write_plugin`].
    const MINIMAL_MANIFEST: &str = r#"{
        "schemaVersion": 1,
        "id": "{id}",
        "name": "Test",
        "version": "0.1.0",
        "kinds": ["status-bar-widget"],
        "entryPoints": { "statusBarWidget": { "command": "widget.py", "args": [] } },
        "statusBarWidget": {
            "displayName": "Test",
            "section": "right",
            "defaults": {},
            "schema": [
                { "key": "format24h", "type": "boolean", "label": "24-hour clock",
                  "defaultValue": true }
            ]
        }
    }"#;

    /// Minimal valid action-contributor manifest, the action-kind counterpart
    /// of [`MINIMAL_MANIFEST`]. `{id}` is replaced by [`write_plugin`].
    const MINIMAL_ACTION_MANIFEST: &str = r#"{
        "schemaVersion": 1,
        "id": "{id}",
        "name": "Test Actions",
        "version": "0.1.0",
        "kinds": ["action-contributor"],
        "entryPoints": { "actionContributor": { "command": "actions.py", "args": [] } },
        "actions": [ { "id": "say-hello", "label": "Say hello" } ]
    }"#;

    /// Minimal valid panel manifest: the kind owes no block beyond its entry
    /// point. `{id}` is replaced by [`write_plugin`].
    const MINIMAL_PANEL_MANIFEST: &str = r#"{
        "schemaVersion": 1,
        "id": "{id}",
        "name": "Test Panel",
        "version": "0.1.0",
        "kinds": ["panel"],
        "entryPoints": { "panel": { "command": "panel.py", "args": [] } }
    }"#;

    /// Both-kinds manifest: one plugin contributing a widget and an action.
    /// `{id}` is replaced by [`write_plugin`].
    const BOTH_KINDS_MANIFEST: &str = r#"{
        "schemaVersion": 1,
        "id": "{id}",
        "name": "Test Both",
        "version": "0.1.0",
        "kinds": ["status-bar-widget", "action-contributor"],
        "entryPoints": {
            "statusBarWidget": { "command": "widget.py", "args": [] },
            "actionContributor": { "command": "actions.py", "args": [] }
        },
        "actions": [ { "id": "ping", "label": "Ping" } ],
        "statusBarWidget": {
            "displayName": "Test",
            "section": "right",
            "defaults": {},
            "schema": [
                { "key": "format24h", "type": "boolean", "label": "24-hour clock",
                  "defaultValue": true }
            ]
        }
    }"#;

    /// Write a plugin directory: manifest plus an executable entry file.
    /// `manifest` may use `{id}` placeholders for the directory name so tests
    /// can keep manifest id and dir name in sync.
    fn write_plugin(root: &Path, dir_name: &str, manifest: &str, entry: Option<&str>) {
        let dir = root.join(dir_name);
        fs::create_dir_all(&dir).expect("create plugin dir");
        let manifest = manifest.replace("{id}", dir_name);
        fs::write(dir.join("manifest.json"), manifest).expect("write manifest");
        if let Some(entry_name) = entry {
            write_exec_file(&dir.join(entry_name));
        }
    }

    /// Write one executable entry file, with the exec bit on Unix.
    fn write_exec_file(path: &Path) {
        fs::write(path, "#!/bin/sh\nsleep 30\n").expect("write entry");
        #[cfg(unix)]
        {
            use std::os::unix::fs::PermissionsExt;
            let mut perms = fs::metadata(path).expect("entry metadata").permissions();
            perms.set_mode(0o755);
            fs::set_permissions(path, perms).expect("chmod entry");
        }
    }

    #[test]
    fn design_d1_example_parses_with_every_field_populated() {
        let tmp = TempDir::new().unwrap();
        write_plugin(
            tmp.path(),
            "com.example.agents-usage",
            DESIGN_D1_MANIFEST,
            Some("agent-widget"),
        );
        let (plugins, warnings) = discover_plugins(tmp.path());
        assert!(warnings.is_empty(), "unexpected warnings: {warnings:?}");
        let p = &plugins[0];
        assert_eq!(p.manifest.id, "com.example.agents-usage");
        assert_eq!(p.manifest.author.as_deref(), Some("A Plugin Author"));
        assert_eq!(p.manifest.license.as_deref(), Some("MIT"));
        assert_eq!(p.manifest.activation, PluginActivation::Manual);
        assert_eq!(p.manifest.kinds, vec![KIND_STATUS_BAR_WIDGET.to_string()]);
        let widget = p.manifest.status_bar_widget.as_ref().unwrap();
        assert_eq!(widget.display_name, "Agents");
        assert_eq!(widget.section, StatusBarSection::Right);
        assert_eq!(
            widget
                .defaults
                .get("refreshIntervalSec")
                .and_then(serde_json::Value::as_u64),
            Some(900)
        );
        assert_eq!(widget.schema.len(), 1);
        assert_eq!(widget.schema[0].setting_type, SettingType::Integer);
        assert_eq!(widget.schema[0].min, Some(30.0));
        assert_eq!(widget.schema[0].max, Some(3600.0));
        assert_eq!(widget.schema[0].step, Some(30.0));
        assert!(p.entry_path.ends_with("agent-widget"));
    }

    #[cfg(unix)]
    #[test]
    fn py_entry_without_exec_bit_is_discovered() {
        // `.py` entries run via the resolved Python interpreter, so
        // validation must not require the exec bit. Regression: a plugin
        // copied in without the bit (fresh `cp -r` of a script plugin) was
        // silently undiscoverable on Unix while Windows accepted it.
        let tmp = TempDir::new().unwrap();
        write_plugin(tmp.path(), "com.example.py-noexec", MINIMAL_MANIFEST, None);
        let entry = tmp.path().join("com.example.py-noexec/widget.py");
        fs::write(&entry, "#!/usr/bin/env python3\n").expect("write entry");
        // Deliberately no chmod: the interpreter routing makes it runnable.
        let (plugins, warnings) = discover_plugins(tmp.path());
        assert!(warnings.is_empty(), "unexpected warnings: {warnings:?}");
        assert_eq!(plugins.len(), 1);
        assert!(plugins[0].entry_path.ends_with("widget.py"));
    }

    #[test]
    fn unknown_extra_keys_parse_for_forward_compatibility() {
        let tmp = TempDir::new().unwrap();
        let manifest = MINIMAL_MANIFEST.replacen(
            "\"version\": \"0.1.0\",",
            "\"version\": \"0.1.0\", \"futureField\": {\"nested\": true},",
            1,
        );
        write_plugin(tmp.path(), "com.example.test", &manifest, Some("widget.py"));
        let (plugins, warnings) = discover_plugins(tmp.path());
        assert!(warnings.is_empty(), "unexpected warnings: {warnings:?}");
        assert_eq!(plugins.len(), 1);
    }

    #[test]
    fn id_mismatching_directory_name_is_skipped_with_a_warning() {
        let tmp = TempDir::new().unwrap();
        write_plugin(
            tmp.path(),
            "com.example.test",
            DESIGN_D1_MANIFEST,
            Some("agent-widget"),
        );
        let (plugins, warnings) = discover_plugins(tmp.path());
        assert!(plugins.is_empty());
        assert_eq!(warnings.len(), 1);
        assert!(warnings[0].reason.contains("does not match directory name"));
    }

    #[test]
    fn unknown_kind_skips_the_plugin_with_a_warning() {
        let tmp = TempDir::new().unwrap();
        let manifest = MINIMAL_MANIFEST.replace("status-bar-widget", "some-future-kind");
        write_plugin(tmp.path(), "com.example.test", &manifest, Some("widget.py"));
        let (plugins, warnings) = discover_plugins(tmp.path());
        assert!(plugins.is_empty());
        assert_eq!(warnings.len(), 1);
        assert!(
            warnings[0]
                .reason
                .contains("unknown kinds: some-future-kind")
        );
    }

    #[test]
    fn escaping_entry_command_is_rejected() {
        let tmp = TempDir::new().unwrap();
        // A real executable OUTSIDE the plugin dir the escape targets.
        let outside = tmp.path().join("outside.sh");
        fs::write(&outside, "#!/bin/sh\nsleep 30\n").unwrap();
        #[cfg(unix)]
        {
            use std::os::unix::fs::PermissionsExt;
            let mut perms = fs::metadata(&outside).unwrap().permissions();
            perms.set_mode(0o755);
            fs::set_permissions(&outside, perms).unwrap();
        }
        let manifest = MINIMAL_MANIFEST.replace("\"widget.py\"", "\"../outside.sh\"");
        write_plugin(tmp.path(), "com.example.test", &manifest, Some("widget.py"));
        let (plugins, warnings) = discover_plugins(tmp.path());
        assert!(plugins.is_empty(), "`..` escape must not be discovered");
        assert_eq!(warnings.len(), 1);
        assert!(warnings[0].reason.contains("escapes the plugin directory"));
    }

    #[test]
    fn missing_entry_file_is_skipped_with_a_warning() {
        let tmp = TempDir::new().unwrap();
        write_plugin(tmp.path(), "com.example.test", MINIMAL_MANIFEST, None);
        let (plugins, warnings) = discover_plugins(tmp.path());
        assert!(plugins.is_empty());
        assert_eq!(warnings.len(), 1);
        assert!(warnings[0].reason.contains("not found"));
    }

    #[cfg(unix)]
    #[test]
    fn non_executable_entry_is_skipped_with_a_warning() {
        use std::os::unix::fs::PermissionsExt;
        let tmp = TempDir::new().unwrap();
        // Non-`.py` entries exec directly, so they must carry the bit;
        // `.py` entries are interpreter-routed (see
        // `py_entry_without_exec_bit_is_discovered`).
        let manifest = MINIMAL_MANIFEST.replace("widget.py", "widget.sh");
        write_plugin(tmp.path(), "com.example.test", &manifest, Some("widget.sh"));
        let entry = tmp.path().join("com.example.test/widget.sh");
        let mut perms = fs::metadata(&entry).unwrap().permissions();
        perms.set_mode(0o644);
        fs::set_permissions(&entry, perms).unwrap();
        let (plugins, warnings) = discover_plugins(tmp.path());
        assert!(plugins.is_empty());
        assert_eq!(warnings.len(), 1);
        assert!(warnings[0].reason.contains("not executable"));
    }

    #[test]
    fn one_broken_plugin_never_blocks_its_neighbours() {
        let tmp = TempDir::new().unwrap();
        write_plugin(
            tmp.path(),
            "com.example.a",
            MINIMAL_MANIFEST,
            Some("widget.py"),
        );
        let dir = tmp.path().join("com.example.broken");
        fs::create_dir_all(&dir).unwrap();
        fs::write(dir.join("manifest.json"), "{ not json").unwrap();
        write_plugin(
            tmp.path(),
            "com.example.c",
            MINIMAL_MANIFEST,
            Some("widget.py"),
        );
        let (plugins, warnings) = discover_plugins(tmp.path());
        assert_eq!(plugins.len(), 2, "both valid neighbours must be discovered");
        assert_eq!(warnings.len(), 1);
        assert_eq!(warnings[0].dir, "com.example.broken");
        assert!(warnings[0].reason.contains("invalid manifest JSON"));
    }

    #[test]
    fn widget_kind_without_status_bar_widget_block_is_skipped() {
        let tmp = TempDir::new().unwrap();
        // Rename only the widget BLOCK key (the entryPoints key of the same
        // name sits inline on a different line); the block's contents become
        // an unknown field, so the manifest parses without a widget block.
        let manifest = MINIMAL_MANIFEST.replace(
            "\"statusBarWidget\": {\n            \"displayName\"",
            "\"notAWidgetBlock\": {\n            \"displayName\"",
        );
        write_plugin(tmp.path(), "com.example.test", &manifest, Some("widget.py"));
        let (plugins, warnings) = discover_plugins(tmp.path());
        assert!(plugins.is_empty());
        assert!(
            warnings[0]
                .reason
                .contains("requires a statusBarWidget block")
        );
    }

    #[test]
    fn widget_kind_without_entry_point_is_skipped() {
        let tmp = TempDir::new().unwrap();
        // Remove the whole entryPoints line; the widget block survives, so
        // the failure is specifically the missing entry point.
        let manifest = MINIMAL_MANIFEST.replace(
            "\"entryPoints\": { \"statusBarWidget\": { \"command\": \"widget.py\", \"args\": [] } },\n",
            "",
        );
        write_plugin(tmp.path(), "com.example.test", &manifest, Some("widget.py"));
        let (plugins, warnings) = discover_plugins(tmp.path());
        assert!(plugins.is_empty());
        assert!(
            warnings[0]
                .reason
                .contains("requires entryPoints.statusBarWidget")
        );
    }

    #[test]
    fn action_kind_manifest_discovers_with_action_entry_path() {
        let tmp = TempDir::new().unwrap();
        write_plugin(
            tmp.path(),
            "com.example.actions",
            MINIMAL_ACTION_MANIFEST,
            Some("actions.py"),
        );
        let (plugins, warnings) = discover_plugins(tmp.path());
        assert!(warnings.is_empty(), "unexpected warnings: {warnings:?}");
        assert_eq!(plugins.len(), 1);
        let p = &plugins[0];
        let action_entry = p.action_entry_path.as_ref().expect("action entry set");
        assert!(
            action_entry.starts_with(&p.dir),
            "must stay inside the plugin dir"
        );
        assert!(action_entry.ends_with("actions.py"));
        assert_eq!(p.manifest.actions.len(), 1);
        assert_eq!(p.manifest.actions[0].id, "say-hello");
        assert_eq!(p.manifest.actions[0].label, "Say hello");
        assert_eq!(p.manifest.actions[0].description, None);
        assert!(p.panel_entry_path.is_none());
    }

    #[test]
    fn panel_kind_manifest_discovers_with_panel_entry_path() {
        let tmp = TempDir::new().unwrap();
        write_plugin(
            tmp.path(),
            "com.example.panel",
            MINIMAL_PANEL_MANIFEST,
            Some("panel.py"),
        );
        let (plugins, warnings) = discover_plugins(tmp.path());
        assert!(warnings.is_empty(), "unexpected warnings: {warnings:?}");
        assert_eq!(plugins.len(), 1);
        let p = &plugins[0];
        let panel_entry = p.panel_entry_path.as_ref().expect("panel entry set");
        assert!(
            panel_entry.starts_with(&p.dir),
            "must stay inside the plugin dir"
        );
        assert!(panel_entry.ends_with("panel.py"));
        // A panel-only manifest has no widget block and no actions, and the
        // panel entry is the plugin's primary entry.
        assert!(p.manifest.status_bar_widget.is_none());
        assert!(p.manifest.actions.is_empty());
        assert!(p.entry_path.ends_with("panel.py"));
        assert!(p.action_entry_path.is_none());
    }

    #[test]
    fn panel_kind_without_entry_point_is_skipped() {
        let tmp = TempDir::new().unwrap();
        let manifest = MINIMAL_PANEL_MANIFEST.replace(
            "\"entryPoints\": { \"panel\": { \"command\": \"panel.py\", \"args\": [] } }",
            "\"entryPoints\": {}",
        );
        write_plugin(tmp.path(), "com.example.panel", &manifest, Some("panel.py"));
        let (plugins, warnings) = discover_plugins(tmp.path());
        assert!(plugins.is_empty());
        assert!(
            warnings[0].reason.contains("requires entryPoints.panel"),
            "got: {}",
            warnings[0].reason
        );
    }

    #[test]
    fn both_kinds_manifest_discovers_with_both_entry_paths() {
        let tmp = TempDir::new().unwrap();
        write_plugin(
            tmp.path(),
            "com.example.both",
            BOTH_KINDS_MANIFEST,
            Some("widget.py"),
        );
        write_exec_file(&tmp.path().join("com.example.both/actions.py"));
        let (plugins, warnings) = discover_plugins(tmp.path());
        assert!(warnings.is_empty(), "unexpected warnings: {warnings:?}");
        assert_eq!(plugins.len(), 1);
        let p = &plugins[0];
        assert!(
            p.entry_path.ends_with("widget.py"),
            "widget entry stays the primary entry"
        );
        let action_entry = p.action_entry_path.as_ref().expect("action entry set");
        assert!(
            action_entry.starts_with(&p.dir),
            "must stay inside the plugin dir"
        );
        assert!(action_entry.ends_with("actions.py"));
        assert_eq!(
            p.manifest.kinds,
            vec![
                KIND_STATUS_BAR_WIDGET.to_string(),
                KIND_ACTION_CONTRIBUTOR.to_string()
            ]
        );
    }

    #[test]
    fn action_kind_with_empty_actions_is_skipped() {
        let tmp = TempDir::new().unwrap();
        let manifest = MINIMAL_ACTION_MANIFEST
            .replace(r#"[ { "id": "say-hello", "label": "Say hello" } ]"#, "[]");
        write_plugin(
            tmp.path(),
            "com.example.actions",
            &manifest,
            Some("actions.py"),
        );
        let (plugins, warnings) = discover_plugins(tmp.path());
        assert!(plugins.is_empty());
        assert!(
            warnings[0]
                .reason
                .contains("action-contributor kind requires")
        );
    }

    #[test]
    fn action_id_with_a_colon_is_skipped() {
        let tmp = TempDir::new().unwrap();
        // A colon in an action id would collide with the wire format's id
        // separator (design D1), so the manifest is skipped outright.
        let manifest = MINIMAL_ACTION_MANIFEST.replace("\"say-hello\"", "\"say:hello\"");
        write_plugin(
            tmp.path(),
            "com.example.actions",
            &manifest,
            Some("actions.py"),
        );
        let (plugins, warnings) = discover_plugins(tmp.path());
        assert!(plugins.is_empty());
        assert!(warnings[0].reason.contains("action id `say:hello`"));
    }

    #[test]
    fn duplicate_action_ids_are_skipped() {
        let tmp = TempDir::new().unwrap();
        let manifest = MINIMAL_ACTION_MANIFEST.replace(
            r#"{ "id": "say-hello", "label": "Say hello" }"#,
            r#"{ "id": "say-hello", "label": "First" }, { "id": "say-hello", "label": "Second" }"#,
        );
        write_plugin(
            tmp.path(),
            "com.example.actions",
            &manifest,
            Some("actions.py"),
        );
        let (plugins, warnings) = discover_plugins(tmp.path());
        assert!(plugins.is_empty());
        assert!(
            warnings[0]
                .reason
                .contains("duplicate action id `say-hello`")
        );
    }

    #[test]
    fn action_with_empty_label_is_skipped() {
        let tmp = TempDir::new().unwrap();
        let manifest =
            MINIMAL_ACTION_MANIFEST.replace("\"label\": \"Say hello\"", "\"label\": \"\"");
        write_plugin(
            tmp.path(),
            "com.example.actions",
            &manifest,
            Some("actions.py"),
        );
        let (plugins, warnings) = discover_plugins(tmp.path());
        assert!(plugins.is_empty());
        assert!(warnings[0].reason.contains("non-empty label"));
    }

    #[test]
    fn actions_without_the_action_kind_are_valid_and_ignored() {
        let tmp = TempDir::new().unwrap();
        // Forward compat: a newer host's action plugin must still discover
        // here as a plain widget plugin; the inert `actions` block is not a
        // reason to skip.
        let manifest = MINIMAL_MANIFEST.replacen(
            "\"kinds\": [\"status-bar-widget\"],",
            concat!(
                "\"kinds\": [\"status-bar-widget\"], ",
                "\"actions\": [ { \"id\": \"orphan\", \"label\": \"Orphan\" } ],"
            ),
            1,
        );
        write_plugin(tmp.path(), "com.example.test", &manifest, Some("widget.py"));
        let (plugins, warnings) = discover_plugins(tmp.path());
        assert!(warnings.is_empty(), "unexpected warnings: {warnings:?}");
        assert_eq!(plugins.len(), 1);
        assert!(
            plugins[0].action_entry_path.is_none(),
            "no action kind declared, so no action entry is required or resolved"
        );
    }

    #[test]
    fn subscriptions_parse_and_survive_discovery() {
        let tmp = TempDir::new().unwrap();
        let manifest = MINIMAL_MANIFEST.replacen(
            "\"kinds\": [\"status-bar-widget\"],",
            concat!(
                "\"kinds\": [\"status-bar-widget\"], ",
                "\"subscriptions\": [\"bell_rang\", \"command_complete\"],"
            ),
            1,
        );
        write_plugin(tmp.path(), "com.example.test", &manifest, Some("widget.py"));
        let (plugins, warnings) = discover_plugins(tmp.path());
        assert!(warnings.is_empty(), "unexpected warnings: {warnings:?}");
        assert_eq!(plugins.len(), 1);
        assert_eq!(
            plugins[0].manifest.subscriptions,
            vec!["bell_rang".to_string(), "command_complete".to_string()]
        );
    }

    #[test]
    fn theme_changed_is_a_valid_subscription_kind() {
        // theme_changed is app-sourced — delivered host-side by
        // `PluginHost::sync_theme`, never by a terminal observer — but it
        // subscribes through the same manifest list, so discovery must
        // accept it or the kind is unreachable.
        let tmp = TempDir::new().unwrap();
        let manifest = MINIMAL_MANIFEST.replacen(
            "\"kinds\": [\"status-bar-widget\"],",
            concat!(
                "\"kinds\": [\"status-bar-widget\"], ",
                "\"subscriptions\": [\"theme_changed\"],"
            ),
            1,
        );
        write_plugin(tmp.path(), "com.example.test", &manifest, Some("widget.py"));
        let (plugins, warnings) = discover_plugins(tmp.path());
        assert!(warnings.is_empty(), "unexpected warnings: {warnings:?}");
        assert_eq!(plugins.len(), 1);
        assert_eq!(
            plugins[0].manifest.subscriptions,
            vec!["theme_changed".to_string()]
        );
    }

    #[test]
    fn manifest_without_subscriptions_defaults_to_empty() {
        // The self-scheduled contract: an absent subscriptions block must
        // parse to an empty list, never a delivery of every event.
        let tmp = TempDir::new().unwrap();
        write_plugin(
            tmp.path(),
            "com.example.test",
            MINIMAL_MANIFEST,
            Some("widget.py"),
        );
        let (plugins, warnings) = discover_plugins(tmp.path());
        assert!(warnings.is_empty(), "unexpected warnings: {warnings:?}");
        assert_eq!(plugins.len(), 1);
        assert!(plugins[0].manifest.subscriptions.is_empty());
    }

    #[test]
    fn unknown_subscription_kind_skips_the_plugin() {
        let tmp = TempDir::new().unwrap();
        let manifest = MINIMAL_MANIFEST.replacen(
            "\"kinds\": [\"status-bar-widget\"],",
            concat!(
                "\"kinds\": [\"status-bar-widget\"], ",
                "\"subscriptions\": [\"bell_rang\", \"not_an_event\"],"
            ),
            1,
        );
        write_plugin(tmp.path(), "com.example.test", &manifest, Some("widget.py"));
        let (plugins, warnings) = discover_plugins(tmp.path());
        assert!(plugins.is_empty());
        assert_eq!(warnings.len(), 1);
        assert!(
            warnings[0]
                .reason
                .contains("unknown subscription kind `not_an_event`"),
            "got: {}",
            warnings[0].reason
        );
    }

    #[test]
    fn duplicate_subscription_kind_is_skipped() {
        let tmp = TempDir::new().unwrap();
        let manifest = MINIMAL_MANIFEST.replacen(
            "\"kinds\": [\"status-bar-widget\"],",
            concat!(
                "\"kinds\": [\"status-bar-widget\"], ",
                "\"subscriptions\": [\"bell_rang\", \"bell_rang\"],"
            ),
            1,
        );
        write_plugin(tmp.path(), "com.example.test", &manifest, Some("widget.py"));
        let (plugins, warnings) = discover_plugins(tmp.path());
        assert!(plugins.is_empty());
        assert!(
            warnings[0]
                .reason
                .contains("duplicate subscription kind `bell_rang`"),
            "got: {}",
            warnings[0].reason
        );
    }

    #[test]
    fn manifest_without_restart_defaults_to_on_failure() {
        // Today's behaviour is the default: a manifest without the field
        // must keep restarting on failure (the config enum's own default,
        // Never, must NOT leak into plugins).
        let tmp = TempDir::new().unwrap();
        write_plugin(
            tmp.path(),
            "com.example.test",
            MINIMAL_MANIFEST,
            Some("widget.py"),
        );
        let (plugins, warnings) = discover_plugins(tmp.path());
        assert!(warnings.is_empty(), "unexpected warnings: {warnings:?}");
        assert_eq!(
            plugins[0].manifest.restart,
            par_term_config::RestartPolicy::OnFailure
        );
    }

    #[test]
    fn overlay_capability_block_parses_and_defaults_to_display_only() {
        let tmp = TempDir::new().unwrap();
        let display_only = MINIMAL_PANEL_MANIFEST.replace(
            "\"kinds\": [\"panel\"],\n        \"entryPoints\": { \"panel\":",
            "\"kinds\": [\"overlay\"],\n        \"entryPoints\": { \"overlay\":",
        );
        write_plugin(
            tmp.path(),
            "com.example.test",
            &display_only,
            Some("panel.py"),
        );
        let (plugins, warnings) = discover_plugins(tmp.path());
        assert!(warnings.is_empty(), "unexpected warnings: {warnings:?}");
        // No overlay block at all: display-only.
        assert_eq!(plugins[0].manifest.overlay, None);

        let interactive = display_only.replace(
            "\"kinds\": [\"overlay\"],",
            "\"kinds\": [\"overlay\"], \"overlay\": { \"interactive\": true },",
        );
        let tmp2 = TempDir::new().unwrap();
        write_plugin(
            tmp2.path(),
            "com.example.test",
            &interactive,
            Some("panel.py"),
        );
        let (plugins, _warnings) = discover_plugins(tmp2.path());
        assert_eq!(
            plugins[0].manifest.overlay,
            Some(OverlayCapability { interactive: true })
        );

        // The block without the flag defaults the flag off — claiming the
        // block alone grants nothing.
        let block_only = display_only.replace(
            "\"kinds\": [\"overlay\"],",
            "\"kinds\": [\"overlay\"], \"overlay\": {},",
        );
        let tmp3 = TempDir::new().unwrap();
        write_plugin(
            tmp3.path(),
            "com.example.test",
            &block_only,
            Some("panel.py"),
        );
        let (plugins, _warnings) = discover_plugins(tmp3.path());
        assert_eq!(
            plugins[0].manifest.overlay,
            Some(OverlayCapability { interactive: false })
        );
    }

    #[test]
    fn explicit_restart_never_parses() {
        let tmp = TempDir::new().unwrap();
        let manifest = MINIMAL_MANIFEST.replace(
            "\"kinds\": [\"status-bar-widget\"],",
            "\"kinds\": [\"status-bar-widget\"], \"restart\": \"never\",",
        );
        write_plugin(tmp.path(), "com.example.test", &manifest, Some("widget.py"));
        let (plugins, warnings) = discover_plugins(tmp.path());
        assert!(warnings.is_empty(), "unexpected warnings: {warnings:?}");
        assert_eq!(plugins.len(), 1);
        assert_eq!(
            plugins[0].manifest.restart,
            par_term_config::RestartPolicy::Never
        );
    }

    #[test]
    fn unknown_restart_mode_is_skipped() {
        // The enum parse rejects unknown strings at manifest parse time;
        // the skip reason must name the restart value, not just "invalid".
        let tmp = TempDir::new().unwrap();
        let manifest = MINIMAL_MANIFEST.replace(
            "\"kinds\": [\"status-bar-widget\"],",
            "\"kinds\": [\"status-bar-widget\"], \"restart\": \"sometimes\",",
        );
        write_plugin(tmp.path(), "com.example.test", &manifest, Some("widget.py"));
        let (plugins, warnings) = discover_plugins(tmp.path());
        assert!(plugins.is_empty());
        assert!(
            warnings[0].reason.contains("sometimes"),
            "got: {}",
            warnings[0].reason
        );
    }

    #[test]
    fn absent_root_is_empty_with_no_warnings() {
        let (plugins, warnings) = discover_plugins(Path::new("/nonexistent/plugins-root-xyz"));
        assert!(plugins.is_empty());
        assert!(warnings.is_empty());
    }

    #[test]
    fn empty_kinds_is_skipped() {
        let tmp = TempDir::new().unwrap();
        let manifest =
            MINIMAL_MANIFEST.replace(r#""kinds": ["status-bar-widget"]"#, r#""kinds": []"#);
        write_plugin(tmp.path(), "com.example.test", &manifest, Some("widget.py"));
        let (plugins, warnings) = discover_plugins(tmp.path());
        assert!(plugins.is_empty());
        assert!(warnings[0].reason.contains("no kinds declared"));
    }

    #[test]
    fn unsupported_schema_version_is_skipped() {
        let tmp = TempDir::new().unwrap();
        let manifest = MINIMAL_MANIFEST.replace("\"schemaVersion\": 1", "\"schemaVersion\": 2");
        write_plugin(tmp.path(), "com.example.test", &manifest, Some("widget.py"));
        let (plugins, warnings) = discover_plugins(tmp.path());
        assert!(plugins.is_empty());
        assert!(warnings[0].reason.contains("unsupported schemaVersion 2"));
    }

    /// Schema used by the validate_settings tests: one entry per type.
    fn test_schema() -> Vec<SettingSchemaEntry> {
        serde_json::from_str(
            r#"[
                { "key": "format24h", "type": "boolean", "label": "24h", "defaultValue": true },
                { "key": "interval", "type": "integer", "label": "Interval",
                  "min": 30, "max": 3600, "defaultValue": 900 },
                { "key": "label", "type": "string", "label": "Label", "defaultValue": "hi" }
            ]"#,
        )
        .unwrap()
    }

    fn settings(pairs: &[(&str, serde_json::Value)]) -> HashMap<String, serde_json::Value> {
        pairs
            .iter()
            .map(|(k, v)| (k.to_string(), v.clone()))
            .collect()
    }

    #[test]
    fn validate_settings_fills_defaults_for_absent_keys() {
        let schema = test_schema();
        let values = settings(&[("interval", serde_json::json!(120))]);
        let out = validate_settings(&schema, &values).unwrap();
        assert_eq!(out.get("format24h"), Some(&serde_json::json!(true)));
        assert_eq!(out.get("interval"), Some(&serde_json::json!(120)));
        assert_eq!(out.get("label"), Some(&serde_json::json!("hi")));
    }

    #[test]
    fn validate_settings_drops_keys_the_schema_no_longer_declares() {
        let schema = test_schema();
        let values = settings(&[("goneKey", serde_json::json!(1))]);
        let out = validate_settings(&schema, &values).unwrap();
        assert!(!out.contains_key("goneKey"));
    }

    #[test]
    fn validate_settings_rejects_wrong_type() {
        let schema = test_schema();
        let values = settings(&[("interval", serde_json::json!("fast"))]);
        let err = validate_settings(&schema, &values).unwrap_err();
        assert!(err.contains("`interval` expects integer"), "got: {err}");
    }

    #[test]
    fn validate_settings_rejects_fractional_integers() {
        let schema = test_schema();
        let values = settings(&[("interval", serde_json::json!(30.5))]);
        assert!(validate_settings(&schema, &values).is_err());
    }

    #[test]
    fn validate_settings_enforces_min_and_max() {
        let schema = test_schema();
        let too_low = settings(&[("interval", serde_json::json!(10))]);
        assert!(
            validate_settings(&schema, &too_low)
                .unwrap_err()
                .contains("below the minimum 30")
        );
        let too_high = settings(&[("interval", serde_json::json!(9999))]);
        assert!(
            validate_settings(&schema, &too_high)
                .unwrap_err()
                .contains("above the maximum 3600")
        );
    }
}