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
//! Substrate primitive over `anyhow::Result<T>` — the ONE substrate
//! owner of the `.map_err(|e| anyhow::anyhow!("<ctx>: {e}"))` wrap-
//! shape every reconciler consumer restates by hand at the
//! anyhow-returning → anyhow-returning display-prefix boundary. Peer
//! of [`crate::kube_error::KubeResultExt`] and
//! [`crate::hostname::HostnameResultExt`] on the flatten-wrap axis; the
//! three traits partition the display-prefix wrap space by underlying
//! error type — [`kube::Error`] on the K8s peer, [`crate::hostname::
//! HostnameError`] on the hostname peer, [`anyhow::Error`] on this
//! peer (the "already an anyhow error, we just want a static or
//! `format!`-composed slug in front of its `Display` output" case).
//!
//! Pre-lift the shape was hand-authored at FIVE `phase_machine.rs`
//! sites in `tatara-reconciler` past the ★★ PRIME-DIRECTIVE ≥ 2
//! duplication threshold, each restating the SAME closure — capture
//! an [`anyhow::Error`] returned by a downstream primitive
//! ([`crate::boundary::check_depends_on`],
//! [`crate::render::render_routing`],
//! [`crate::boundary::evaluate`],
//! [`crate::render::render_export_jobs`],
//! [`crate::ssapply::apply_owned`]), prepend a context slug identifying
//! which primitive faulted, delegate the tail to [`anyhow::Error`]'s
//! `Display` impl via the `{e}` slot — differing only in the context
//! slug prefix each callsite stamped.
//!
//! Post-lift each callsite reads
//! `<anyhow-returning-call>().await.flatten_ctx("<slug>")?` (or the
//! owned-`String` peer for consumers that compose the slug via
//! `format!`) and the wrap-shape lives at ONE substrate owner here.
//! The composed [`anyhow::Error`]'s `Display` is byte-identical to
//! the pre-lift chain (`format!("{ctx}: {e}")`, threading the source
//! [`anyhow::Error`]'s own `Display` verbatim into the `{e}` slot),
//! so operator-facing log output and any error-chain greps still
//! match bytewise. A regression that drifts the separator, swaps
//! the two slots, or wraps the source [`anyhow::Error`] with a
//! chain-form `source` (which would change `Display` output on the
//! `err` slot when downstream consumers format with `{e}` rather
//! than `{e:#}`) surfaces at the tests below rather than as silent
//! operator-facing drift across the five pre-lift consumers.
//!
//! ### Naming — `flatten_ctx`, not `anyhow::Context::context`
//!
//! Same discipline as [`crate::kube_error::KubeResultExt::kube_ctx`]
//! and [`crate::hostname::HostnameResultExt::hostname_ctx`] — but with
//! a more urgent motivation, because THIS trait operates on the SAME
//! `anyhow::Result<T>` type [`anyhow::Context::context`] takes.
//! `anyhow::Context::context` wraps the source in a chain (so
//! `Display` emits ONLY the context slug and callers reach the source
//! [`anyhow::Error`] via [`std::error::Error::source`] traversal, or
//! by formatting with the alternate `{:#}` specifier that walks the
//! chain), while this trait's `flatten_ctx` FLATTENS to a display-
//! prefix shape (`"<ctx>: <anyhow::Error display>"`) — the pre-lift
//! wire format every consumer's `tracing::error!(error = %e, ...)`
//! log line already encoded. Sharing the name (`context`) would let
//! a caller who has [`anyhow::Context`] in scope resolve to the
//! WRONG method (a chain-wrap instead of the display-prefix flatten)
//! and silently drop the underlying error detail from every
//! reconciler-error tracing span whose formatter interpolates `{e}`.
//!
//! ### Two flavors: `flatten_ctx` + `flatten_ctx_with`
//!
//! * [`FlattenCtxExt::flatten_ctx`] takes a `&'static str` context —
//! the most common shape, matching every static-slug consumer
//! (`"depends_on check"`, `"render routing"`, `"render export
//! jobs"`, `"apply export job"`). Static binding keeps the
//! compile-time contract that the context slug is a bare literal,
//! no allocation, no dynamic content leaking into an error stream
//! downstream operators grep on.
//! * [`FlattenCtxExt::flatten_ctx_with`] takes an owned [`String`]
//! context — the escape hatch for the one dynamic-slug consumer
//! (`format!("evaluate {:?}", c.kind)`, the `ConditionKind`
//! variant name only known at runtime), matching the pre-lift
//! shape where a `format!` composed the slug per-call.
//!
//! ### `#[must_use]`
//!
//! Every consumer threads the `?` short-circuit onto its handler's
//! `Result<_, anyhow::Error>` return — dropping the wrap swallows
//! the underlying failure entirely, which is never the intended
//! semantic at any of the five pre-lift consumers.
//!
//! Theory anchor: THEORY.md §VI.1 (generation over composition — the
//! anyhow-with-display-prefix wrap-shape recurred at five hand-
//! authored sites past the ★★ PRIME-DIRECTIVE ≥ 2 duplication
//! trigger, and is lifted to ONE substrate owner here). THEORY.md
//! §II.1 invariant 5 (composition preserves proofs — a regression
//! that drifts the display-prefix separator or the byte-shape at
//! ONE site surfaces here at the substrate pin rather than as
//! silent operator-facing skew across every reconciler phase tick).
/// Substrate extension trait over `anyhow::Result<T>` — the ONE
/// substrate owner of the `.map_err(|e| anyhow::anyhow!("<ctx>: {e}"))`
/// display-prefix wrap-shape for consumers whose source error is
/// already `anyhow::Error`. See the module docs for the full callsite
/// audit + the naming rationale (why `flatten_ctx` and not
/// `anyhow::Context::context`).