shep 0.1.5

The shep binary: a process manager that keeps a flock of long-running processes alive on macOS and Linux, with logs, watch and cron restarts, and webhook alerts
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
//! `$SHEP_HOME/shep.toml`, the daemon's own config file — read and rewritten
//! by [`ShepToml`], the one writer this binary has for it.
//!
//! Edits go through `toml_edit`'s [`DocumentMut`] rather than round-tripping
//! through a plain `toml::Table`: `shep.toml` is hand-written far more often
//! than it is generated, and a `shep enable` that reformatted it — dropping
//! comments, reordering keys — would be a reason not to run `shep enable`.
//! [`shep_core::config::DaemonConfig::load`] is still the one place the
//! SHAPE of the file is decided (what a key means, what a bad value looks
//! like); this module only ever adds or removes the handful of keys each
//! verb owns, leaving everything else exactly as it was read.
//!
//! Every edit goes through [`ShepToml::edit`] or [`ShepToml::try_edit`] (for
//! a closure that can itself refuse), which together are the whole write
//! path: `$SHEP_HOME` created at `0700`, an exclusive advisory lock on a
//! sibling `shep.toml.lock` held across the read-modify-write, and the new
//! document staged in a `0600` temp file, `fsync`ed and `rename`d over the
//! original -- but only when the closure actually produced a value to
//! save; a `try_edit` closure's own `Err` leaves `path` untouched, not
//! merely unchanged. Each of the write's three steps is the same shape
//! `shep-core`'s `barks::append` already uses, for the same reasons and
//! after the same bug: two writers racing on `barks.jsonl` silently lost
//! half of each other's records until an advisory lock landed there.
//!
//! `#[cfg(unix)]` wholesale, at `commands`' own declaration in `main.rs` —
//! `flock(2)` and unix mode bits are both Unix-only, and Windows is shep's
//! 0% tier where no verb runs at all.

use std::io::Write as _;
use std::os::unix::fs::{DirBuilderExt as _, OpenOptionsExt as _, PermissionsExt as _};
use std::path::{Path, PathBuf};

use toml_edit::{Array, DocumentMut, Item, Table, Value};

use crate::style::StyleLevel;

/// Mode `shep.toml` — and the sibling lock file and staging file it is
/// written through — is created with: owner read/write, nobody else.
///
/// Unlike `barks.jsonl`'s own `0600`, this is not belt-and-braces. This
/// file is where `docs/dogs.md` tells an operator to paste a Discord or
/// Slack webhook URL, and both carry a bearer token in the path, so the
/// mode here is the guard rather than a second one behind `$SHEP_HOME`'s.
/// It is also what a `tar`, a `cp -p` or a backup of `$SHEP_HOME` carries
/// out with the file, somewhere no directory mode follows it.
const CONFIG_FILE_MODE: u32 = 0o600;

/// Extensions [`ShepToml::write_starter_interpreters`] maps, in the order
/// they land in `shep.toml`.
///
/// `js`/`mjs`/`cjs` cover the three ways a Node script is named; `py` maps
/// to `python3` rather than bare `python`, which is absent or still points
/// at Python 2 on plenty of hosts shep runs on; `rb` and `sh` round out the
/// four families Rin named directly. Two more chosen with judgement:
/// `pl` for Perl and `php` for PHP, both single, unambiguous interpreters
/// that ship alongside node/python3/ruby/sh on most of the same hosts.
/// Left out on purpose: `ts` (no single safe default exists; ts-node, tsx
/// and deno all disagree about how to run one, and picking the wrong one
/// silently is worse than making the operator say so), and anything
/// Windows-only such as `ps1`, since shep's Windows tier is 0 percent as
/// of this writing.
const STARTER_INTERPRETERS: &[(&str, &str)] = &[
    ("js", "node"),
    ("mjs", "node"),
    ("cjs", "node"),
    ("py", "python3"),
    ("rb", "ruby"),
    ("sh", "sh"),
    ("pl", "perl"),
    ("php", "php"),
];

/// The comment [`ShepToml::write_starter_interpreters`] writes directly
/// above the `[interpreters]` table it scaffolds, so the mapping reads as
/// something an operator can see and edit rather than as hidden shep
/// behaviour.
///
/// Plain `#` TOML comment lines, not `///` Rust doc syntax: this text
/// lands inside `shep.toml` itself, for an operator to read there, so the
/// project's "no dashes in anything a user reads" rule governs it exactly
/// as it governs `welcome.rs`'s copy.
const INTERPRETERS_STARTER_COMMENT: &str = "\
# Extension -> interpreter mapping. shep applies one of these to a script
# when nothing more specific already named an interpreter: not this app's
# own Flockfile entry, and not --interpreter on the command line, both of
# which win over anything here. shep never guesses beyond what is written
# below, so edit freely: change an interpreter, add an extension, or
# delete an entry (or this whole table) to turn the mapping off for it.
";

/// The one writer of `$SHEP_HOME/shep.toml` in this binary.
///
/// A missing file is created (as an empty document — [`Self::edit`] makes
/// `$SHEP_HOME` too, if needed); a file that will not parse is refused
/// rather than overwritten, because it may hold every knob a daemon boots
/// with, credentials included, and there is no undo for losing it to a
/// typo'd verb.
///
/// [`Self::edit`] is the only way to reach one of these, and holds the
/// document's lock for exactly as long as the closure runs. Reading and
/// writing are deliberately not separate public steps: a caller that
/// could read, think, and then write would be the lost update this type
/// takes a lock to prevent.
pub struct ShepToml {
    path: PathBuf,
    doc: DocumentMut,
}

/// Manual, not derived: `doc` is the parsed document, and a `[dog.<name>]`
/// table routinely holds a webhook URL with a bearer token in its query
/// string (`SECURITY.md`) — the same exposure `DogSectionToml`
/// (shep-core's own `protocol::request`) exists to keep out of a
/// `{:?}`-formatted `Response`. `ShepToml` never crosses that wire, but it
/// is exactly as capable of being `{:?}`-printed into a log by some future
/// caller, so it gets the same treatment: only the path, never the parsed
/// document.
impl std::fmt::Debug for ShepToml {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        f.debug_struct("ShepToml")
            .field("path", &self.path)
            .finish_non_exhaustive()
    }
}

impl ShepToml {
    /// Reads `path`, hands the document to `f`, and writes it back — the
    /// whole read-modify-write under one exclusive advisory lock.
    ///
    /// The one way to write this file. `f` returns whatever the caller
    /// needed to read while the lock was held — `shep enable` reads a
    /// dog's source before it edits the same document — and that value
    /// comes back on success.
    ///
    /// Serialised against any other editor, in this process or another,
    /// by a lock on a sibling `shep.toml.lock` ([`ConfigLock`]). Without
    /// it, `shep adopt otel ... & shep enable metrics &` from one
    /// provisioning script has both processes read the pre-edit document
    /// and write the whole thing back, and the loser's edit is gone with
    /// no error on either side. That is not theory: `barks.jsonl` lost
    /// half its records to the identical shape before it grew the same
    /// lock.
    ///
    /// `f` here is infallible; see [`Self::try_edit`] for a closure that
    /// can itself refuse the edit before anything is written.
    ///
    /// # Errors
    /// - [`ShepTomlError::Io`] — `$SHEP_HOME` could not be created, the
    ///   lock beside the file could not be taken, or the file could not
    ///   be read or replaced.
    /// - [`ShepTomlError::Parse`] — the file exists and is not valid
    ///   TOML. Refused rather than overwritten, and `f` never runs.
    pub fn edit<T>(path: &Path, f: impl FnOnce(&mut Self) -> T) -> Result<T, ShepTomlError> {
        let (mut doc, _lock) = Self::open_locked(path)?;
        let value = f(&mut doc);
        doc.save()?;
        Ok(value)
    }

    /// Like [`Self::edit`], but for a closure that can itself refuse the
    /// edit: `f`'s own `Err` skips [`Self::save`] entirely, the same way a
    /// [`Self::open`] failure already does. A setter whose key can already
    /// be occupied by a shape it cannot write into (an operator's
    /// hand-written `style = "full"` where [`Self::set_style_level`] needs
    /// a table, say) must be able to say so without the read-modify-
    /// write underneath it staging and renaming a byte-identical copy of
    /// the file back over itself anyway — that rename still lands a fresh
    /// inode and forces [`CONFIG_FILE_MODE`] on a file that a refused edit
    /// never actually touched, and for a symlinked `path` it is what
    /// replaces the link with a plain file. [`Self::edit`]'s `f` cannot
    /// refuse at all, so that failure mode did not exist before this
    /// method's first caller needed to fail from inside the closure.
    ///
    /// Generic over the closure's own error `E` rather than fixed to
    /// [`ShepTomlError`], so a caller whose own failure is its own type
    /// does not have to wrap this module's error a second time;
    /// `E: From<ShepTomlError>` is what lets `?` cover this method's own
    /// setup failures (home dir, lock, parse) the same way it already
    /// covers `f`'s.
    ///
    /// # Errors
    /// Everything [`Self::edit`] can fail with, converted through
    /// `E::from`, plus whatever `f` itself returns as `Err` -- in either
    /// case, `path` is left exactly as [`Self::open`] found it.
    pub fn try_edit<T, E: From<ShepTomlError>>(
        path: &Path,
        f: impl FnOnce(&mut Self) -> Result<T, E>,
    ) -> Result<T, E> {
        let (mut doc, _lock) = Self::open_locked(path)?;
        let value = f(&mut doc)?;
        doc.save()?;
        Ok(value)
    }

    /// Creates `$SHEP_HOME` if missing, takes `path`'s exclusive lock, and
    /// opens the document -- the setup [`Self::edit`] and [`Self::try_edit`]
    /// share; only what happens with the open document, and whether a
    /// failure from it still reaches [`Self::save`], differs between the
    /// two.
    ///
    /// The returned [`ConfigLock`] must outlive every use of the returned
    /// `Self` -- it is what makes the read this function just did and the
    /// caller's eventual `save` one transaction as far as any other editor
    /// is concerned, the same guarantee [`Self::edit`]'s own doc describes.
    fn open_locked(path: &Path) -> Result<(Self, ConfigLock), ShepTomlError> {
        let parent = path.parent().unwrap_or_else(|| Path::new("."));
        create_home_dir(parent).map_err(|source| ShepTomlError::Io {
            path: parent.to_path_buf(),
            source,
        })?;

        // Held until the caller's `Self`/`ConfigLock` pair both drop, so
        // the read just below and the caller's eventual rename inside
        // `save` are one transaction as far as any other editor is
        // concerned.
        let lock = ConfigLock::acquire(path).map_err(|source| ShepTomlError::Io {
            path: path.to_path_buf(),
            source,
        })?;

        let doc = Self::open(path)?;
        Ok((doc, lock))
    }

    /// Reads `path`, treating a missing file as an empty document.
    ///
    /// Private. Reached two ways: from [`Self::edit`]/[`Self::try_edit`]
    /// with the document's lock already held for a write, and from
    /// [`Self::adopted_dog_path_readonly`] with no lock at all, for a
    /// caller that only ever reads.
    ///
    /// # Errors
    /// - [`ShepTomlError::Io`] — the file exists and could not be read.
    /// - [`ShepTomlError::Parse`] — the file exists and is not valid TOML.
    fn open(path: &Path) -> Result<Self, ShepTomlError> {
        let doc = match std::fs::read_to_string(path) {
            Ok(text) => text
                .parse::<DocumentMut>()
                .map_err(|source| ShepTomlError::Parse {
                    path: path.to_path_buf(),
                    source,
                })?,
            Err(source) if source.kind() == std::io::ErrorKind::NotFound => DocumentMut::new(),
            Err(source) => {
                return Err(ShepTomlError::Io {
                    path: path.to_path_buf(),
                    source,
                });
            }
        };
        Ok(Self {
            path: path.to_path_buf(),
            doc,
        })
    }

    /// Adds `name` to `[daemon] enabled_dogs` (idempotently) and ensures a
    /// `[dog.<name>]` table exists for the dog to be configured through.
    ///
    /// Never truncates a `[dog.<name>]` table that already exists — a
    /// dog's own configuration is not this writer's to touch, only its
    /// existence.
    pub fn enable_dog(&mut self, name: &str) {
        let daemon = self.daemon_table_mut();
        let enabled_dogs = daemon
            .entry("enabled_dogs")
            .or_insert_with(|| Item::Value(Value::Array(Array::new())))
            .as_array_mut()
            .expect("enabled_dogs is only ever written as an array");
        if !enabled_dogs.iter().any(|v| v.as_str() == Some(name)) {
            enabled_dogs.push(name);
        }
        self.dog_table_mut(name);
    }

    /// Removes `name` from `[daemon] enabled_dogs`, leaving `[dog.<name>]`
    /// in place: an operator who disables a dog to restart it must not lose
    /// the configuration they wrote for it.
    pub fn disable_dog(&mut self, name: &str) {
        if let Some(enabled_dogs) = self
            .doc
            .get_mut("daemon")
            .and_then(Item::as_table_mut)
            .and_then(|daemon| daemon.get_mut("enabled_dogs"))
            .and_then(Item::as_array_mut)
        {
            enabled_dogs.retain(|v| v.as_str() != Some(name));
        }
    }

    /// Records `name`'s binary in `[daemon] adopted_dogs` and enables it.
    ///
    /// Called by `commands::dogs::adopt`, once `vet_binary` has already
    /// vetted `exec` — this method itself does no vetting, and never
    /// truncates anything past the two keys it owns.
    pub fn adopt_dog(&mut self, name: &str, exec: &Path) {
        let daemon = self.daemon_table_mut();
        let adopted_dogs = daemon
            .entry("adopted_dogs")
            .or_insert_with(|| Item::Table(Table::new()))
            .as_table_mut()
            .expect("adopted_dogs is only ever written as a table");
        adopted_dogs.insert(
            name,
            Item::Value(exec.to_string_lossy().into_owned().into()),
        );
        self.enable_dog(name);
    }

    /// The binary path recorded for `name` in `[daemon] adopted_dogs`, if
    /// any — `None` for a built-in dog, or a name this document has never
    /// heard of.
    ///
    /// Read by `commands::dogs::rehome` before [`Self::rehome_dog`] removes
    /// the entry, so the verb can still report what it forgot.
    #[must_use]
    pub fn adopted_dog_path(&self, name: &str) -> Option<PathBuf> {
        self.doc
            .get("daemon")?
            .as_table()?
            .get("adopted_dogs")?
            .as_table()?
            .get(name)?
            .as_str()
            .map(PathBuf::from)
    }

    /// [`Self::adopted_dog_path`] without [`Self::edit`]'s write side --
    /// for a caller that only wants the answer, such as `lib.rs`'s
    /// `dispatch_adopted_dog`, which runs on every unrecognized verb, most
    /// of which are typos rather than dog names.
    ///
    /// Creates nothing: a missing `$SHEP_HOME` or a missing `path` is an
    /// ordinary "no such dog" answer ([`Self::open`] already treats a
    /// missing file as an empty document), never a reason to create
    /// either. Takes no lock, unlike [`Self::edit`] -- `Self::save`'s
    /// rename onto `path` is atomic, so a concurrent writer can only ever
    /// make this read observe the document just before or just after that
    /// write, never a torn one.
    ///
    /// # Errors
    /// [`ShepTomlError::Io`] if `path` exists and could not be read.
    /// [`ShepTomlError::Parse`] if `path` exists and is not valid TOML.
    pub fn adopted_dog_path_readonly(
        path: &Path,
        name: &str,
    ) -> Result<Option<PathBuf>, ShepTomlError> {
        Ok(Self::open(path)?.adopted_dog_path(name))
    }

    /// Forgets `name` entirely: out of `enabled_dogs`, out of
    /// `adopted_dogs`, and `[dog.<name>]` removed. The difference between
    /// `rehome` and `disable`, and the reason they are two verbs.
    ///
    /// Called by `commands::dogs::rehome`.
    pub fn rehome_dog(&mut self, name: &str) {
        self.disable_dog(name);
        if let Some(adopted_dogs) = self
            .doc
            .get_mut("daemon")
            .and_then(Item::as_table_mut)
            .and_then(|daemon| daemon.get_mut("adopted_dogs"))
            .and_then(Item::as_table_mut)
        {
            adopted_dogs.remove(name);
        }
        if let Some(dog) = self.doc.get_mut("dog").and_then(Item::as_table_mut) {
            dog.remove(name);
        }
    }

    /// Writes `[style] level = "<level>"`, creating the `[style]` table
    /// when this document has none yet, and replacing the value when one
    /// is already there.
    ///
    /// The value written is `level`'s own `Display` spelling --
    /// `full`/`plain`/`bare` -- the same string `style_from_config`
    /// (`lib.rs`) parses back through `clap::ValueEnum::from_str`, so a
    /// round trip through this setter and back stays one grammar rather
    /// than a writer and a reader that merely happen to agree today.
    ///
    /// Called by `shep style <level>` (`Commands::Style`'s set form).
    ///
    /// # Errors
    /// [`ShepTomlError::WrongShape`] -- `style` is already there as
    /// something other than a table, e.g. an operator hand-wrote
    /// `style = "full"` at the top level. Reported rather than forced:
    /// `.as_table_mut().expect(..)` on `entry().or_insert_with(..)` is
    /// this module's usual idiom (`enable_dog`/`disable_dog`/`adopt_dog`/
    /// `rehome_dog` all still use it), but it is sound there only because
    /// nothing else in this file ever writes those keys as anything but
    /// a table, so the `expect` never actually fires on real input. This
    /// setter's key can be hand-written by an operator who reasonably
    /// guessed `style = "full"` instead of the `[style]` header, so the
    /// same `expect` here is reachable from data this process does not
    /// control -- exactly the panicking-constructor shape IR-21 rules
    /// out. The four sibling setters above still carry the shape this one
    /// used to; that is a tracked follow-up, not this fix's scope.
    pub fn set_style_level(&mut self, level: StyleLevel) -> Result<(), ShepTomlError> {
        let item = self
            .doc
            .entry("style")
            .or_insert_with(|| Item::Table(Table::new()));
        let Some(style) = item.as_table_mut() else {
            return Err(ShepTomlError::WrongShape {
                path: self.path.clone(),
                key: "style",
                found: item.type_name(),
            });
        };
        style.insert("level", Item::Value(level.to_string().into()));
        Ok(())
    }

    /// Writes the starter `[interpreters]` mapping (task 47) -- a script
    /// extension to the interpreter shep runs it with, active from the
    /// moment it lands rather than commented into inertness, with an
    /// explanatory comment above the table so it reads as something an
    /// operator wrote and can freely edit rather than as hidden behaviour.
    /// Active is the point: shep never infers an interpreter on its own,
    /// but a fresh `$SHEP_HOME` still has to be able to run the
    /// `shep start server.js` `welcome.rs` and `--help` both advertise as
    /// the quick start, and a mapping nobody has uncommented yet cannot do
    /// that.
    ///
    /// A no-op when `[interpreters]` already exists. Called once per home
    /// from `lib.rs`'s first-run scaffold, but idempotent by construction
    /// rather than by that single call site -- the same reasoning
    /// [`Self::enable_dog`] gives for its own idempotence -- so a caller
    /// that runs this twice, or a `shep.toml` an operator has since hand-
    /// edited, is never clobbered or duplicated.
    pub fn write_starter_interpreters(&mut self) {
        if self.doc.contains_key("interpreters") {
            return;
        }
        let mut table = Table::new();
        for (extension, interpreter) in STARTER_INTERPRETERS {
            table.insert(extension, Item::Value((*interpreter).into()));
        }
        table.decor_mut().set_prefix(INTERPRETERS_STARTER_COMMENT);
        self.doc.insert("interpreters", Item::Table(table));
    }

    /// Writes the document back: staged in a sibling temp file at
    /// [`CONFIG_FILE_MODE`], `fsync`ed, then `rename`d over `path`.
    ///
    /// Private, and reached only from [`Self::edit`] with the lock held.
    ///
    /// `std::fs::write` opens `O_TRUNC`, so a crash, a signal or an
    /// `ENOSPC` between the truncate and the write leaves an operator's
    /// whole `shep.toml` truncated or empty — the one loss this type's
    /// own doc says there is no undo for. Staging and renaming is what
    /// every other file this workspace writes already does
    /// (`barks::write_ring`, `snapshot::write_atomic`,
    /// `boot::write_pidfile`), and it is also what re-tightens a
    /// `shep.toml` that an older shep left at `0644`: the rename
    /// installs the staging file's inode, mode included.
    ///
    /// # Errors
    /// - [`ShepTomlError::Io`] — the staging file could not be created or
    ///   written, or the rename over `path` failed.
    fn save(&self) -> Result<(), ShepTomlError> {
        let parent = self.path.parent().unwrap_or_else(|| Path::new("."));
        let mut tmp = create_config_file(parent).map_err(|source| self.io_error(source))?;
        tmp.write_all(self.doc.to_string().as_bytes())
            .map_err(|source| self.io_error(source))?;
        tmp.as_file()
            .sync_all()
            .map_err(|source| self.io_error(source))?;
        // `persist` is `rename(2)`. On failure the `NamedTempFile` comes
        // back inside the error and its `Drop` removes the staging file,
        // so a failed replace leaves nothing behind in `$SHEP_HOME`.
        tmp.persist(&self.path)
            .map_err(|err| self.io_error(err.error))?;
        Ok(())
    }

    /// This file's [`ShepTomlError::Io`], for the several ways one write
    /// of it can fail.
    fn io_error(&self, source: std::io::Error) -> ShepTomlError {
        ShepTomlError::Io {
            path: self.path.clone(),
            source,
        }
    }

    /// `[daemon]`, creating it (empty) if this document has none yet.
    fn daemon_table_mut(&mut self) -> &mut Table {
        self.doc
            .entry("daemon")
            .or_insert_with(|| Item::Table(Table::new()))
            .as_table_mut()
            .expect("daemon is only ever written as a table")
    }

    /// `[dog.<name>]`, creating it (empty) if it does not exist yet — never
    /// touched again once it does, per [`Self::enable_dog`]'s own doc.
    fn dog_table_mut(&mut self, name: &str) -> &mut Table {
        let dog = self
            .doc
            .entry("dog")
            .or_insert_with(|| Item::Table(Table::new()))
            .as_table_mut()
            .expect("dog is only ever written as a table");
        dog.entry(name)
            .or_insert_with(|| Item::Table(Table::new()))
            .as_table_mut()
            .expect("a dog's own section is only ever written as a table")
    }
}

/// Creates `dir` (and any missing parent) at `boot::DIR_MODE` directly,
/// via `DirBuilderExt`, rather than `create_dir_all` and a later `chmod`.
///
/// `$SHEP_HOME` holds the webhook URLs `docs/dogs.md` tells an operator to
/// paste into `[dog.bark.sinks]`, and on a host that has never booted a
/// shepherd this call is the one that creates it — `boot::init_dirs`, which
/// force-chmods it to `DIR_MODE`, does not run until the first `shep
/// muster`. A `create_dir_all` here would leave it at the ambient umask,
/// typically `0755`, for every local user to read until that boot. Asking
/// for the mode at `mkdir` time also leaves no window in which the
/// directory exists wider, the same TOCTOU discipline
/// `launch::launch_command` and `boot::create_dir_at_dir_mode` each spell
/// out at their own call.
///
/// Reuses `shep_daemon::boot::DIR_MODE` rather than restating `0o700`: one
/// spelling of the number, so a change to the daemon's posture cannot pass
/// this by.
fn create_home_dir(dir: &Path) -> std::io::Result<()> {
    std::fs::DirBuilder::new()
        .recursive(true)
        .mode(shep_daemon::boot::DIR_MODE)
        .create(dir)
}

/// Creates the staging file the config is written through, in `parent` so
/// the later `rename` stays within one filesystem.
///
/// Mode-at-creation rather than a separate `chmod` pass (`tempfile` passes
/// these permissions to the `open` call itself): there is no window in
/// which the file holding a webhook token sits at whatever the process
/// umask leaves it. Same shape, and same reasoning, as
/// `barks::create_ring_file`.
fn create_config_file(parent: &Path) -> std::io::Result<tempfile::NamedTempFile> {
    tempfile::Builder::new()
        .prefix("shep")
        .suffix(".toml.tmp")
        .permissions(std::fs::Permissions::from_mode(CONFIG_FILE_MODE))
        .tempfile_in(parent)
}

/// An exclusive advisory lock over one `shep.toml`, held for as long as
/// the value lives and released when it drops (including on an early `?`,
/// and by the kernel if the process dies holding it).
///
/// The lock is on a sibling `shep.toml.lock`, never on the config itself,
/// and that is the whole design decision — the same one `barks::RingLock`
/// records: [`ShepToml::save`] finishes by `rename`ing a new file over the
/// config, which replaces the inode. A lock taken on the config would be a
/// lock on an inode the very next successful save unlinks; the next writer
/// would open the *new* inode, find it unlocked, and the two would be
/// excluding nothing. The lock file is never renamed, never rewritten and
/// never read; it exists only to be an inode with a stable identity, and
/// it is left on disk between edits on purpose so both writers keep
/// agreeing on which one it is.
struct ConfigLock {
    /// `flock(2)` is released by this handle's `Drop`. Named with a
    /// leading underscore because it is held, never read.
    _flock: nix::fcntl::Flock<std::fs::File>,
}

impl ConfigLock {
    /// Blocks until this process holds `path`'s lock exclusively.
    ///
    /// # Errors
    /// The lock file could not be created beside `path`, or `flock` failed
    /// for a reason other than contention (contention blocks rather than
    /// failing).
    fn acquire(path: &Path) -> std::io::Result<Self> {
        use nix::fcntl::{Flock, FlockArg};

        let file = std::fs::OpenOptions::new()
            .write(true)
            .create(true)
            .truncate(false)
            .mode(CONFIG_FILE_MODE)
            .open(lock_path(path))?;

        // `LockExclusive` blocks; the non-blocking variant would need a
        // retry loop and a deadline, and a `shep enable` that waits its
        // turn behind a concurrent `shep adopt` is exactly the behaviour
        // wanted here.
        Flock::lock(file, FlockArg::LockExclusive)
            .map(|flock| Self { _flock: flock })
            .map_err(|(_file, errno)| std::io::Error::from(errno))
    }
}

/// The lock file that guards `path`: its own name with `.lock` appended,
/// so it sits in `$SHEP_HOME` next to the config and inherits that
/// directory's `0700`.
fn lock_path(path: &Path) -> PathBuf {
    let mut name = path
        .file_name()
        .map(std::ffi::OsStr::to_os_string)
        .unwrap_or_default();
    name.push(".lock");
    path.parent().unwrap_or_else(|| Path::new(".")).join(name)
}

/// What [`ShepToml::edit`] can fail with. Module-scoped per IR-18.
///
/// Deliberately NOT `#[non_exhaustive]`, and this is the comment IR-20 asks
/// for in the negative case. shep-cli is `[[bin]]`-only — no `lib.rs`, no
/// published surface — so nothing outside this binary can match on this enum
/// and there is no downstream `match` for the attribute to protect. Adding it
/// would tax only this crate's own exhaustive matches, which are the ones we
/// WANT the compiler to break when `ShepToml::edit` grows a new failure mode.
/// Same reasoning as [`CronScheduleError`](shep_core::config::CronScheduleError)'s
/// own omission, for a different reason: that one is closed, this one is
/// unexported.
pub enum ShepTomlError {
    /// A read or write of `path` itself failed.
    Io {
        /// The path that failed.
        path: PathBuf,
        /// The underlying IO failure.
        source: std::io::Error,
    },
    /// `path` exists but is not valid TOML.
    Parse {
        /// The path that failed to parse.
        path: PathBuf,
        /// The parser's own complaint.
        source: toml_edit::TomlError,
    },
    /// `path` parses, but `key` is already there as something other than
    /// a table -- e.g. an operator writing `style = "full"` at the top
    /// level instead of `[style]` / `level = "full"`. Legal TOML, but not
    /// a shape any setter in this module can write a sub-key into:
    /// forcing it to a table would silently discard whatever the
    /// operator actually wrote there.
    WrongShape {
        /// The file that holds the wrongly-shaped value.
        path: PathBuf,
        /// The table key that was expected -- `"style"` for
        /// [`ShepToml::set_style_level`], the only caller today.
        key: &'static str,
        /// What TOML actually found there ([`Item::type_name`]) --
        /// `"string"`, `"array"`, and so on; never `"table"`, since that
        /// is the one shape this variant is never raised for.
        found: &'static str,
    },
}

/// Manual, not derived: `toml_edit::TomlError` carries the ENTIRE source
/// document internally (its own `raw` field, kept for `Display`'s
/// line-and-column rendering) — a `#[derive(Debug)]` here would forward to
/// that type's own derived `Debug` and print `shep.toml` in full, secrets
/// included, the exact leak `DaemonConfig`'s own `Debug` already declines
/// for its `dog` field. `Display` still shows the parser's full
/// line-of-context message (below): that is the one deliberate surface
/// meant for the operator who broke their own file to read, same as
/// `DaemonConfigError::Toml`'s. `Debug` is not that surface — it is what a
/// log captures — so it is redacted to the path and the parser's short
/// `message()`, never the line it quotes.
impl std::fmt::Debug for ShepTomlError {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        match self {
            Self::Io { path, source } => f
                .debug_struct("Io")
                .field("path", path)
                .field("source", source)
                .finish(),
            Self::Parse { path, source } => f
                .debug_struct("Parse")
                .field("path", path)
                .field("message", &source.message())
                .finish(),
            Self::WrongShape { path, key, found } => f
                .debug_struct("WrongShape")
                .field("path", path)
                .field("key", key)
                .field("found", found)
                .finish(),
        }
    }
}

impl std::fmt::Display for ShepTomlError {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        match self {
            Self::Io { path, source } => write!(f, "{}: {source}", path.display()),
            Self::Parse { path, source } => write!(f, "{}: {source}", path.display()),
            Self::WrongShape { path, key, found } => write!(
                f,
                "{}: [{key}] must be a table, found a {found}",
                path.display()
            ),
        }
    }
}

impl core::error::Error for ShepTomlError {
    fn source(&self) -> Option<&(dyn core::error::Error + 'static)> {
        match self {
            Self::Io { source, .. } => Some(source),
            Self::Parse { source, .. } => Some(source),
            Self::WrongShape { .. } => None,
        }
    }
}

#[cfg(test)]
mod tests {
    use shep_core::config::DaemonConfig;

    use super::*;

    /// `path`'s permission bits, masked to the nine that matter.
    fn mode_of(path: &Path) -> u32 {
        std::fs::metadata(path).unwrap().permissions().mode() & 0o777
    }

    /// fails if the writer round-trips through a plain `toml::Table`. An
    /// operator's `shep.toml` is hand-written, and a `shep enable` that
    /// silently dropped their comments and reordered their keys is a reason
    /// not to run `shep enable`.
    #[test]
    fn enabling_a_dog_leaves_the_rest_of_the_file_exactly_as_it_was() {
        let dir = tempfile::tempdir().unwrap();
        let path = dir.path().join("shep.toml");
        let original = "# the shepherd's own knobs\n[daemon]\nlog_level = \"info\"  # chatty\nlog_json = false\n";
        std::fs::write(&path, original).unwrap();

        ShepToml::edit(&path, |doc| doc.enable_dog("metrics")).unwrap();

        let written = std::fs::read_to_string(&path).unwrap();
        assert!(written.contains("# the shepherd's own knobs"));
        assert!(written.contains("# chatty"));
        assert!(
            written.find("log_level").unwrap() < written.find("log_json").unwrap(),
            "key order survives"
        );

        let cfg = DaemonConfig::load(Some(&written), &|_| None).unwrap();
        assert_eq!(cfg.daemon.enabled_dogs, vec!["metrics"]);
        assert!(
            cfg.dog.contains_key("metrics"),
            "a table to configure it through"
        );
    }

    /// fails if `enable` appends a duplicate on the second call, which
    /// would make the daemon try to start one dog twice at boot, or if
    /// `disable` takes the dog's configuration with it — the operator who
    /// disables a dog to restart it must get their rules back.
    #[test]
    fn enable_is_idempotent_and_disable_keeps_the_config_it_did_not_write() {
        let dir = tempfile::tempdir().unwrap();
        let path = dir.path().join("shep.toml");
        std::fs::write(&path, "[dog.bark]\ndebounce = \"30s\"\n").unwrap();

        ShepToml::edit(&path, |doc| {
            doc.enable_dog("bark");
            doc.enable_dog("bark");
        })
        .unwrap();
        let cfg =
            DaemonConfig::load(Some(&std::fs::read_to_string(&path).unwrap()), &|_| None).unwrap();
        assert_eq!(cfg.daemon.enabled_dogs, vec!["bark"]);

        ShepToml::edit(&path, |doc| doc.disable_dog("bark")).unwrap();
        let written = std::fs::read_to_string(&path).unwrap();
        let cfg = DaemonConfig::load(Some(&written), &|_| None).unwrap();
        assert!(cfg.daemon.enabled_dogs.is_empty());
        assert!(
            written.contains("30s"),
            "disable stops a dog; rehome is what forgets it"
        );
    }

    /// fails if a `shep.toml` that will not parse is overwritten instead of
    /// refused. That file may hold every knob a daemon boots with; losing
    /// it to a typo'd `shep enable` is not recoverable.
    #[test]
    fn a_file_that_will_not_parse_is_refused_rather_than_replaced() {
        let dir = tempfile::tempdir().unwrap();
        let path = dir.path().join("shep.toml");
        std::fs::write(&path, "[daemon\nlog_json = true\n").unwrap();
        assert!(matches!(
            ShepToml::edit(&path, |doc| doc.enable_dog("metrics")),
            Err(ShepTomlError::Parse { .. })
        ));
        assert_eq!(
            std::fs::read_to_string(&path).unwrap(),
            "[daemon\nlog_json = true\n"
        );
    }

    /// fails if `rehome_dog` leaves anything behind: `[daemon] adopted_dogs`,
    /// `enabled_dogs`, or `[dog.<name>]` itself — the whole point of `rehome`
    /// over `disable` is that nothing survives it.
    #[test]
    fn rehoming_a_dog_forgets_it_entirely() {
        let dir = tempfile::tempdir().unwrap();
        let path = dir.path().join("shep.toml");
        ShepToml::edit(&path, |doc| {
            doc.adopt_dog("otel", Path::new("/usr/local/bin/shep-otel"));
        })
        .unwrap();
        let written = std::fs::read_to_string(&path).unwrap();
        let cfg = DaemonConfig::load(Some(&written), &|_| None).unwrap();
        assert_eq!(cfg.daemon.enabled_dogs, vec!["otel"]);
        assert_eq!(
            cfg.daemon
                .adopted_dogs
                .get("otel")
                .map(std::path::PathBuf::as_path),
            Some(Path::new("/usr/local/bin/shep-otel"))
        );
        assert!(cfg.dog.contains_key("otel"));

        ShepToml::edit(&path, |doc| doc.rehome_dog("otel")).unwrap();
        let written = std::fs::read_to_string(&path).unwrap();
        let cfg = DaemonConfig::load(Some(&written), &|_| None).unwrap();
        assert!(cfg.daemon.enabled_dogs.is_empty());
        assert!(!cfg.daemon.adopted_dogs.contains_key("otel"));
        assert!(!cfg.dog.contains_key("otel"));
    }

    /// fails if `adopted_dog_path` cannot see an entry `adopt_dog` wrote
    /// (the read `commands::dogs::rehome` needs before `rehome_dog` erases
    /// it), or if it invents a path for a name it never recorded — a
    /// built-in dog, or one this document has never heard of.
    #[test]
    fn adopted_dog_path_reads_what_adopt_dog_wrote_and_nothing_else() {
        let dir = tempfile::tempdir().unwrap();
        let path = dir.path().join("shep.toml");
        ShepToml::edit(&path, |doc| {
            doc.enable_dog("metrics"); // built-in: no `adopted_dogs` entry at all
            doc.adopt_dog("otel", Path::new("/usr/local/bin/shep-otel"));

            assert_eq!(
                doc.adopted_dog_path("otel"),
                Some(PathBuf::from("/usr/local/bin/shep-otel"))
            );
            assert_eq!(doc.adopted_dog_path("metrics"), None);
            assert_eq!(doc.adopted_dog_path("ghost"), None);
        })
        .unwrap();
    }

    /// A missing `shep.toml` is not an error — `edit` opens it as an empty
    /// document and creates the file (and `$SHEP_HOME` itself).
    #[test]
    fn a_missing_file_opens_empty_and_edit_creates_it() {
        let dir = tempfile::tempdir().unwrap();
        let path = dir.path().join("nested").join("shep.toml");
        ShepToml::edit(&path, |doc| doc.enable_dog("metrics")).unwrap();
        assert!(path.exists());
    }

    /// fails if `set_style_level` writes a spelling `DaemonConfig::load`'s
    /// own `[style]` reader can't parse back for any of the three levels —
    /// the whole point of writing `level`'s own `Display` string is that
    /// the round trip needs no hand-written expectation to agree with.
    #[test]
    fn setting_a_style_level_round_trips_through_daemon_config() {
        for level in [StyleLevel::Full, StyleLevel::Plain, StyleLevel::Bare] {
            let dir = tempfile::tempdir().unwrap();
            let path = dir.path().join("shep.toml");
            ShepToml::try_edit(&path, |doc| doc.set_style_level(level)).unwrap();
            let written = std::fs::read_to_string(&path).unwrap();
            let cfg = DaemonConfig::load(Some(&written), &|_| None).unwrap();
            assert_eq!(cfg.style.level.as_deref(), Some(level.to_string().as_str()));
        }
    }

    /// fails if setting a style level round-trips through a plain
    /// `toml::Table` the way `enabling_a_dog_leaves_the_rest_of_the_file_
    /// exactly_as_it_was` guards against for dogs — an operator's
    /// `shep.toml` is hand-written, and `shep style` touching only
    /// `[style]` is the whole point of `ShepToml` over `toml::to_string`.
    #[test]
    fn setting_a_style_level_leaves_the_rest_of_the_file_exactly_as_it_was() {
        let dir = tempfile::tempdir().unwrap();
        let path = dir.path().join("shep.toml");
        let original = "# the shepherd's own knobs\n[daemon]\nlog_level = \"info\"  # chatty\nlog_json = false\n";
        std::fs::write(&path, original).unwrap();

        ShepToml::try_edit(&path, |doc| doc.set_style_level(StyleLevel::Plain)).unwrap();

        let written = std::fs::read_to_string(&path).unwrap();
        assert!(written.contains("# the shepherd's own knobs"));
        assert!(written.contains("# chatty"));
        assert!(
            written.find("log_level").unwrap() < written.find("log_json").unwrap(),
            "key order survives"
        );

        let cfg = DaemonConfig::load(Some(&written), &|_| None).unwrap();
        assert_eq!(cfg.style.level.as_deref(), Some("plain"));
    }

    /// fails if a second `shep style` appends a second `level` key rather
    /// than replacing the first — `toml_edit::Table::insert` on an
    /// existing key is supposed to do the latter, but nothing pinned that
    /// this setter actually relies on that rather than, say, always going
    /// through `entry().or_insert_with()`, which would leave the first
    /// value in place forever.
    #[test]
    fn setting_a_style_level_twice_replaces_rather_than_appends() {
        let dir = tempfile::tempdir().unwrap();
        let path = dir.path().join("shep.toml");
        ShepToml::try_edit(&path, |doc| doc.set_style_level(StyleLevel::Full)).unwrap();
        ShepToml::try_edit(&path, |doc| doc.set_style_level(StyleLevel::Bare)).unwrap();

        let written = std::fs::read_to_string(&path).unwrap();
        assert_eq!(written.matches("level").count(), 1, "one key, not appended");
        let cfg = DaemonConfig::load(Some(&written), &|_| None).unwrap();
        assert_eq!(cfg.style.level.as_deref(), Some("bare"));
    }

    /// A `$SHEP_HOME` with no `shep.toml` at all is the common case for a
    /// first `shep style <level>`, and it must create one rather than
    /// refusing — the same behaviour `a_missing_file_opens_empty_and_edit_
    /// creates_it` pins for `enable_dog`.
    #[test]
    fn setting_a_style_level_into_a_home_with_no_shep_toml_creates_one() {
        let dir = tempfile::tempdir().unwrap();
        let path = dir.path().join("shep.toml");
        assert!(!path.exists());

        ShepToml::try_edit(&path, |doc| doc.set_style_level(StyleLevel::Bare)).unwrap();

        assert!(path.exists());
        let cfg =
            DaemonConfig::load(Some(&std::fs::read_to_string(&path).unwrap()), &|_| None).unwrap();
        assert_eq!(cfg.style.level.as_deref(), Some("bare"));
    }

    /// The starter mapping is active, not commented into inertness: task
    /// 47's whole point is that a fresh `$SHEP_HOME` can run `shep start
    /// server.js` without any further setup, and a mapping nobody has
    /// uncommented cannot do that.
    #[test]
    fn the_starter_interpreters_are_written_active() {
        let dir = tempfile::tempdir().unwrap();
        let path = dir.path().join("shep.toml");

        ShepToml::edit(&path, |doc| doc.write_starter_interpreters()).unwrap();

        let cfg =
            DaemonConfig::load(Some(&std::fs::read_to_string(&path).unwrap()), &|_| None).unwrap();
        assert_eq!(cfg.interpreters.get("js").map(String::as_str), Some("node"));
        assert_eq!(
            cfg.interpreters.get("mjs").map(String::as_str),
            Some("node")
        );
        assert_eq!(
            cfg.interpreters.get("cjs").map(String::as_str),
            Some("node")
        );
        assert_eq!(
            cfg.interpreters.get("py").map(String::as_str),
            Some("python3")
        );
        assert_eq!(cfg.interpreters.get("rb").map(String::as_str), Some("ruby"));
        assert_eq!(cfg.interpreters.get("sh").map(String::as_str), Some("sh"));
    }

    /// The mapping is visible and editable, not hidden magic: an
    /// explanatory comment sits right above the table it writes, in the
    /// same file an operator already has open.
    #[test]
    fn the_starter_interpreters_carry_an_explanatory_comment() {
        let dir = tempfile::tempdir().unwrap();
        let path = dir.path().join("shep.toml");

        ShepToml::edit(&path, |doc| doc.write_starter_interpreters()).unwrap();

        let written = std::fs::read_to_string(&path).unwrap();
        assert!(
            written.contains("# Extension -> interpreter mapping"),
            "no explanatory comment above [interpreters]:\n{written}"
        );
        assert!(
            written.find("# Extension -> interpreter mapping").unwrap()
                < written.find("[interpreters]").unwrap(),
            "the comment must precede the table it explains:\n{written}"
        );
        assert!(
            !written.contains('\u{2014}') && !written.contains('\u{2013}'),
            "no em or en dashes in copy an operator reads:\n{written}"
        );
    }

    /// fails if a second scaffold (a second `ensure_home_at` for whatever
    /// reason, or a caller re-running the first-run hook) appends a
    /// duplicate `[interpreters]` table, or overwrites one an operator has
    /// since edited to their own taste.
    #[test]
    fn writing_the_starter_interpreters_twice_does_not_duplicate_or_clobber() {
        let dir = tempfile::tempdir().unwrap();
        let path = dir.path().join("shep.toml");

        ShepToml::edit(&path, |doc| doc.write_starter_interpreters()).unwrap();
        // An operator's own edit to the mapping this scaffold wrote.
        let edited = std::fs::read_to_string(&path)
            .unwrap()
            .replace("js = \"node\"", "js = \"bun\"");
        std::fs::write(&path, &edited).unwrap();

        ShepToml::edit(&path, |doc| doc.write_starter_interpreters()).unwrap();

        let written = std::fs::read_to_string(&path).unwrap();
        assert_eq!(
            written.matches("[interpreters]").count(),
            1,
            "one table, not appended:\n{written}"
        );
        let cfg = DaemonConfig::load(Some(&written), &|_| None).unwrap();
        assert_eq!(
            cfg.interpreters.get("js").map(String::as_str),
            Some("bun"),
            "the operator's own edit must survive a second scaffold call"
        );
    }

    /// fails if `set_style_level` panics instead of reporting when
    /// `style` already exists as something other than a table -- the
    /// reviewer's live repro for this task: an operator writing
    /// `style = "full"` at the top level (legal TOML, and a natural
    /// guess) used to abort the whole process with an internal
    /// assertion, exit 101, from inside this setter's old `.expect(..)`.
    /// A clean [`ShepTomlError::WrongShape`] is required instead.
    ///
    /// Also fails if a refused write still replaces the file. The first
    /// version of this fix routed the setter through [`ShepToml::edit`],
    /// which always calls `save()` after the closure runs regardless of
    /// what the closure returned -- so a refused write still staged a
    /// fresh file and renamed it over the original: identical bytes, but
    /// a new inode, and the mode forced to [`CONFIG_FILE_MODE`] even
    /// though the original here is `0644`. Content equality alone hid
    /// that, which is why this checks the file's metadata rather than
    /// only what is in it. [`ShepToml::try_edit`] is what actually
    /// prevents it: it never reaches `save` when the closure returns
    /// `Err`.
    #[test]
    fn a_style_key_that_is_not_a_table_is_reported_and_the_file_is_never_rewritten() {
        use std::os::unix::fs::MetadataExt as _;

        let dir = tempfile::tempdir().unwrap();
        let path = dir.path().join("shep.toml");
        let original = "style = \"full\"\n";
        std::fs::write(&path, original).unwrap();
        std::fs::set_permissions(&path, std::fs::Permissions::from_mode(0o644)).unwrap();
        let before = std::fs::metadata(&path).unwrap();

        let err = ShepToml::try_edit(&path, |doc| doc.set_style_level(StyleLevel::Bare))
            .expect_err("style is a string here, not a table");
        assert!(
            matches!(
                &err,
                ShepTomlError::WrongShape { key, found, .. }
                    if *key == "style" && *found == "string"
            ),
            "{err:?}"
        );
        assert_eq!(
            err.to_string(),
            format!(
                "{}: [style] must be a table, found a string",
                path.display()
            )
        );

        assert_eq!(
            std::fs::read_to_string(&path).unwrap(),
            original,
            "a refused write must leave the operator's file exactly as it was"
        );
        let after = std::fs::metadata(&path).unwrap();
        assert_eq!(
            before.ino(),
            after.ino(),
            "a refused write must not replace the file -- same inode, not just same bytes"
        );
        assert_eq!(
            before.mode() & 0o777,
            after.mode() & 0o777,
            "a refused write must not touch the file's mode"
        );
    }

    /// fails if the first `shep enable` on a host that has never booted a
    /// shepherd leaves either the file or `$SHEP_HOME` readable by another
    /// local user. This is the file `docs/dogs.md` tells an operator to
    /// paste a Discord webhook URL into, and `boot::init_dirs` — the
    /// force-chmod that would otherwise narrow the directory — does not run
    /// until the first `shep muster`, which may be much later or never.
    ///
    /// Both modes are asserted on a path where neither the directory nor
    /// the file existed beforehand, because that is the case the ambient
    /// umask would decide.
    #[test]
    fn a_first_edit_creates_the_home_and_the_file_owner_only() {
        let dir = tempfile::tempdir().unwrap();
        let home = dir.path().join("cold");
        let path = home.join("shep.toml");

        ShepToml::edit(&path, |doc| doc.enable_dog("bark")).unwrap();

        assert_eq!(
            mode_of(&home),
            0o700,
            "$SHEP_HOME is readable by other local users until the first boot"
        );
        assert_eq!(
            mode_of(&path),
            0o600,
            "the file a webhook token goes in, and the mode a `tar` of it keeps"
        );
    }

    /// fails if an existing `shep.toml` left wide by an older shep (or by
    /// an operator's own `touch`) stays wide after this writer replaces it.
    /// The rename installs the staging file's inode, mode included, so the
    /// narrowing is a property of the write path rather than a chmod pass
    /// somebody has to remember to run.
    #[test]
    fn editing_a_world_readable_config_leaves_it_owner_only() {
        let dir = tempfile::tempdir().unwrap();
        let path = dir.path().join("shep.toml");
        std::fs::write(&path, "[daemon]\n").unwrap();
        std::fs::set_permissions(&path, std::fs::Permissions::from_mode(0o644)).unwrap();

        ShepToml::edit(&path, |doc| doc.enable_dog("bark")).unwrap();

        assert_eq!(mode_of(&path), 0o600);
    }

    /// The redaction IR-41 requires: `Debug` on a parse failure carries the
    /// path and the parser's short message, never the document
    /// `toml_edit::TomlError` quotes to render its own `Display` — a
    /// `[dog.bark]` table sitting next to the syntax error would otherwise
    /// put a webhook token in `{:?}` output.
    #[test]
    fn parse_error_debug_never_prints_the_document() {
        let path = PathBuf::from("/home/rin/.shep/shep.toml");
        let secret = "https://hooks.example.com/services/T00/B00/super-secret-token";
        let broken = format!("[dog.bark]\nwebhook = \"{secret}\"\n[daemon\n");
        let source = broken.parse::<DocumentMut>().unwrap_err();
        let err = ShepTomlError::Parse { path, source };

        let debug = format!("{err:?}");
        assert!(
            !debug.contains(secret),
            "the document must never reach Debug: {debug}"
        );
        assert!(!debug.contains("webhook"), "{debug}");
        assert!(!debug.contains("hooks.example.com"), "{debug}");
        assert_eq!(
            debug,
            "Parse { path: \"/home/rin/.shep/shep.toml\", message: \"invalid table header\\n\
             expected `.`, `]`\" }"
        );

        // `Display`, unlike `Debug`, is the deliberate surface an operator
        // reads to find their own typo — it still shows the offending line.
        let display = err.to_string();
        assert!(display.contains("invalid table header"));
    }

    /// Env var naming the `shep.toml` the re-executed child should edit.
    /// Its presence is also what tells the child it is a child.
    const CHILD_PATH_VAR: &str = "SHEP_CONFIG_RACE_PATH";
    /// Env var carrying the child's tag, which decides both which verb's
    /// edit it makes and what it names the dogs it writes.
    const CHILD_TAG_VAR: &str = "SHEP_CONFIG_RACE_TAG";
    /// How many edits each of the two writers makes. One apiece would race
    /// only in the instant the two overlap; this many makes an unlocked
    /// read-modify-write lose an edit on essentially every run.
    const EDITS_PER_WRITER: usize = 100;
    /// The tag whose child adopts (`[daemon] adopted_dogs` plus
    /// `enabled_dogs`); the other enables (`enabled_dogs` alone). Two
    /// different edits, so a survivor of one cannot stand in for the other.
    const ADOPTING_TAG: &str = "alpha";

    /// Not a test — the child half of
    /// [`two_writer_processes_do_not_lose_each_other_s_edits`], which
    /// re-executes this binary with `--ignored --exact` to reach it. It is
    /// `#[ignore]`d so a normal run never picks it up, and it asserts
    /// nothing: its job is to hammer [`ShepToml::edit`] from a second OS
    /// process, and the parent does the judging.
    #[test]
    #[ignore = "child process of two_writer_processes_do_not_lose_each_other_s_edits"]
    fn config_race_child() {
        let Ok(path) = std::env::var(CHILD_PATH_VAR) else {
            panic!("{CHILD_PATH_VAR} unset — this test is only run as a child process");
        };
        let tag = std::env::var(CHILD_TAG_VAR).expect("child needs a tag");
        let path = PathBuf::from(path);

        for i in 0..EDITS_PER_WRITER {
            let name = format!("{tag}-{i}");
            ShepToml::edit(&path, |doc| {
                if tag == ADOPTING_TAG {
                    doc.adopt_dog(&name, Path::new("/usr/local/bin/shep-otel"));
                } else {
                    doc.enable_dog(&name);
                }
            })
            .expect("child edit");
        }
    }

    /// fails if two `shep` processes editing one `shep.toml` lose each
    /// other's edits — `shep adopt otel ... & shep enable metrics &` out of
    /// a provisioning script, where both read the pre-edit document and
    /// both write the whole thing back.
    ///
    /// Two OS processes, not two threads: this document is read and written
    /// by whole `shep` invocations, so any in-process serialisation would
    /// prove nothing about the bug, which is a read-modify-write across a
    /// `rename` with no lock between address spaces. `barks.jsonl` had the
    /// identical shape and lost half its records to it.
    #[test]
    fn two_writer_processes_do_not_lose_each_other_s_edits() {
        let dir = tempfile::tempdir().unwrap();
        let path = dir.path().join("shep.toml");
        let exe = std::env::current_exe().expect("test binary path");

        let children: Vec<_> = [ADOPTING_TAG, "beta"]
            .iter()
            .map(|tag| {
                std::process::Command::new(&exe)
                    .args([
                        "--exact",
                        "--ignored",
                        "commands::shep_toml::tests::config_race_child",
                    ])
                    .env(CHILD_PATH_VAR, &path)
                    .env(CHILD_TAG_VAR, tag)
                    // Piped, not inherited: a passing run should not
                    // interleave two child harnesses' output into this
                    // one's, and a failing child's harness output is
                    // exactly what the assertion below needs to show.
                    .stdout(std::process::Stdio::piped())
                    .spawn()
                    .expect("spawn writer")
            })
            .collect();

        for child in children {
            let out = child.wait_with_output().expect("wait for writer");
            assert!(
                out.status.success(),
                "a writer process failed: {}\n{}",
                out.status,
                String::from_utf8_lossy(&out.stdout)
            );
        }

        let written = std::fs::read_to_string(&path).unwrap();
        let cfg = DaemonConfig::load(Some(&written), &|_| None).unwrap();
        for i in 0..EDITS_PER_WRITER {
            let adopted = format!("{ADOPTING_TAG}-{i}");
            let enabled = format!("beta-{i}");
            assert!(
                cfg.daemon.adopted_dogs.contains_key(&adopted),
                "{adopted}: an adopt was overwritten by the other writer"
            );
            assert!(
                cfg.daemon.enabled_dogs.contains(&adopted),
                "{adopted}: the adopt's own enable was overwritten"
            );
            assert!(
                cfg.daemon.enabled_dogs.contains(&enabled),
                "{enabled}: an enable was overwritten by the other writer"
            );
        }
        assert_eq!(
            cfg.daemon.enabled_dogs.len(),
            2 * EDITS_PER_WRITER,
            "the config enables dogs nobody asked for"
        );
    }
}