Commit e2894f1
Convert clean_metals_inplace.py to validated yaml roundtrip (#86)
* Convert clean_metals_inplace.py to load/dump roundtrip with write_validated_community
Previously this script mutated 149 community YAMLs in place via regex /
line-edit surgery on the raw text, then wrote the result with
path.write_text(string). The recent schema-gap audit (PR #85) flagged
it as the highest-risk writer in the repo: no validation gate, no
curation_history entry on changed files, and the audit detector itself
was blind to it until the previous PR broadened the heuristic to catch
read_text/write_text round-trips against kb/communities/.
Now: load through yaml.safe_load, run the same cleaning logic on the
parsed dict, append a CLEAN_METAL_FIELDS CurationEvent, and write back
via write_validated_community (with backup-restore on validation
failure, matching the apply_pmc_conversions / link_growth_media pattern
from PR #85). The --dry-run default + --apply opt-in flag matches the
existing repo convention.
Cleaning semantics preserved verbatim: the same AMBIGUOUS_METAL_KEYWORDS
table is consulted (TITANIUM / GOLD / PALLADIUM), and the same
word-bounded evidence search runs over the document minus its
metals_present block — implemented now by yaml.safe_dump'ing a shallow
copy of the doc with metals_present stripped, which keyword_in_text
scans identically (its regex anchors on non-alphanumeric boundaries, so
YAML structural characters do not change match outcomes).
Smoke-tested against a /tmp copy of the corpus:
- Baseline corpus (already cleaned by a prior buggy-extractor run):
reports 0/265 files would change, as expected.
- With an injected false-positive (TITANIUM in a doc with no titanium
evidence): correctly removed in dry-run preview and in --apply, with
a schema-valid curation_history event appended and backup unlinked
on success.
Audit row before: yes no yes no no
Audit row after: yes yes yes yes no
(writes_yaml / appends_curation_history / has_write_safeguard /
validates_before_write / wired_into_just — wired_into_just stays no;
this script is a one-shot cleanup, not a recurring pipeline step.)
Baselines remain green: scripts/validate_strict.py reports 0 errors
across 265 files; pytest tests/ 136 passed / 9 skipped; ruff clean.
Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
* Address Copilot review on PR #86
Two findings:
- The backup/restore flow only caught ValidationFailedError. If any
other exception fired after path.rename(backup) (disk error,
permission denied, unexpected serializer fault), the original would
be left as .yaml.bak_metals with no .yaml. Catch BaseException too:
restore the backup before re-raising so the corpus is never left
with a missing canonical file.
- _evidence_text serializes via yaml.safe_dump, which drops comments
and can reflow scalars — strictly not a perfect raw-text equivalent.
Document the caveat: the keywords we search for live in YAML
content (description, evidence snippet, ecological_state), never in
comments, so the on-disk behavior is identical for the corpus today.
If a future workflow starts encoding evidence in YAML comments, the
function should switch to path.read_text() with a line-strip filter.
Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
---------
Co-authored-by: Claude Opus 4.7 (1M context) <noreply@anthropic.com>1 parent 9d863f8 commit e2894f1
1 file changed
Lines changed: 145 additions & 53 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
18 | 18 | | |
19 | 19 | | |
20 | 20 | | |
21 | | - | |
22 | | - | |
23 | | - | |
24 | | - | |
| 21 | + | |
25 | 22 | | |
26 | 23 | | |
27 | 24 | | |
| |||
31 | 28 | | |
32 | 29 | | |
33 | 30 | | |
34 | | - | |
35 | | - | |
| 31 | + | |
| 32 | + | |
36 | 33 | | |
37 | 34 | | |
38 | 35 | | |
| 36 | + | |
| 37 | + | |
| 38 | + | |
| 39 | + | |
| 40 | + | |
| 41 | + | |
| 42 | + | |
| 43 | + | |
| 44 | + | |
| 45 | + | |
| 46 | + | |
| 47 | + | |
| 48 | + | |
| 49 | + | |
| 50 | + | |
| 51 | + | |
| 52 | + | |
| 53 | + | |
| 54 | + | |
39 | 55 | | |
40 | 56 | | |
| 57 | + | |
| 58 | + | |
41 | 59 | | |
42 | 60 | | |
43 | 61 | | |
44 | 62 | | |
| 63 | + | |
| 64 | + | |
45 | 65 | | |
46 | 66 | | |
| 67 | + | |
47 | 68 | | |
| 69 | + | |
| 70 | + | |
| 71 | + | |
| 72 | + | |
48 | 73 | | |
49 | 74 | | |
50 | 75 | | |
| |||
57 | 82 | | |
58 | 83 | | |
59 | 84 | | |
60 | | - | |
61 | | - | |
62 | | - | |
63 | | - | |
64 | | - | |
65 | | - | |
66 | | - | |
67 | | - | |
68 | | - | |
69 | | - | |
70 | | - | |
71 | | - | |
72 | | - | |
73 | | - | |
74 | | - | |
75 | | - | |
76 | | - | |
77 | | - | |
78 | | - | |
79 | | - | |
80 | | - | |
81 | | - | |
82 | | - | |
83 | | - | |
| 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 | + | |
84 | 118 | | |
85 | | - | |
| 119 | + | |
86 | 120 | | |
87 | | - | |
88 | | - | |
89 | | - | |
90 | | - | |
| 121 | + | |
91 | 122 | | |
92 | 123 | | |
93 | 124 | | |
| |||
96 | 127 | | |
97 | 128 | | |
98 | 129 | | |
99 | | - | |
| 130 | + | |
100 | 131 | | |
101 | 132 | | |
102 | 133 | | |
103 | 134 | | |
104 | | - | |
105 | | - | |
| 135 | + | |
| 136 | + | |
| 137 | + | |
106 | 138 | | |
107 | | - | |
108 | | - | |
109 | | - | |
110 | | - | |
111 | | - | |
112 | | - | |
113 | | - | |
114 | 139 | | |
115 | | - | |
116 | | - | |
| 140 | + | |
| 141 | + | |
| 142 | + | |
| 143 | + | |
| 144 | + | |
| 145 | + | |
| 146 | + | |
| 147 | + | |
| 148 | + | |
| 149 | + | |
| 150 | + | |
| 151 | + | |
| 152 | + | |
| 153 | + | |
| 154 | + | |
117 | 155 | | |
118 | 156 | | |
119 | | - | |
120 | | - | |
| 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 | + | |
121 | 198 | | |
122 | 199 | | |
123 | 200 | | |
124 | 201 | | |
125 | | - | |
126 | | - | |
| 202 | + | |
| 203 | + | |
| 204 | + | |
| 205 | + | |
| 206 | + | |
| 207 | + | |
| 208 | + | |
| 209 | + | |
| 210 | + | |
| 211 | + | |
| 212 | + | |
| 213 | + | |
127 | 214 | | |
128 | 215 | | |
| 216 | + | |
| 217 | + | |
129 | 218 | | |
130 | 219 | | |
131 | 220 | | |
132 | 221 | | |
133 | | - | |
| 222 | + | |
134 | 223 | | |
135 | 224 | | |
136 | 225 | | |
137 | 226 | | |
138 | | - | |
| 227 | + | |
| 228 | + | |
139 | 229 | | |
| 230 | + | |
| 231 | + | |
140 | 232 | | |
141 | 233 | | |
142 | 234 | | |
| |||
0 commit comments