repositories / smith

smith

There are many coding harnesses - but this one is fast

owned by bugabinga

Pin the two wire shapes consumers will key off (#138)

86589ba37e2a15faa2c3337cfe275a637648d8f6

Oliver Jan Krylow <oliver@bugabinga.net> ·

SSH signature · not checked

Browse filesCompare with HEAD

Both shapes were covered only by round-trips, and a round-trip passes just as
happily after a rename because encoder and decoder move together. §7.5's
MuxProvider reads `ProviderError`'s `kind` string to decide failover, so a
silent rename turns classification into a default while every existing test
stays green.

The first attempt reproduced the same fault inside the fix: its CBOR test
round-tripped a definition against itself, which is invariant to
`#[serde(flatten)]`. It now decodes to a CBOR value and asserts the spec's
fields sit at the top level with no `spec` key — removing the attribute fails
it, verified both ways. The kind list became a wildcard-free match so an eighth
variant stops the test compiling rather than shipping unpinned.

`smith/src/provider.rs` and `smith/src/tool.rs`, two tests, 22 total.

Closes #133

Changed files (2)

+87 / −0 text lines

Diff layout

smith/src/provider.rs

Modified

+52 / −0 text lines

229229 assert_eq!(restored, events);
230230 }
232 /// Maps each variant to the `kind` string it must serialize as.
233 ///
234 /// Deliberately wildcard-free: an eighth `ProviderError` variant stops this
235 /// compiling, which is what forces a new kind through the wire-shape test
236 /// rather than letting it ship unpinned. A hand-written list would catch
237 /// renames but not additions.
238 fn expected_kind(error: &ProviderError) -> &'static str {
239 match error {
240 ProviderError::RateLimit(_) => "rate_limit",
241 ProviderError::AuthFailed(_) => "auth_failed",
242 ProviderError::Network(_) => "network",
243 ProviderError::ServerError(_) => "server_error",
244 ProviderError::InvalidRequest(_) => "invalid_request",
245 ProviderError::ModelNotFound(_) => "model_not_found",
246 ProviderError::Timeout(_) => "timeout",
247 }
248 }
250 #[test]
251 fn the_provider_error_wire_shape_is_pinned() {
252 // §7.5's MuxProvider reads the `kind` string to decide failover, so a
253 // rename that both sides follow keeps every round-trip green while
254 // silently turning classification into a default. Pinning the literal is
255 // what makes such a rename visible: this asserts the wire form, not that
256 // serialization is self-consistent.
257 let encoded =
258 serde_json::to_value(ProviderError::RateLimit("slow down".to_owned())).unwrap();
259 assert_eq!(
260 encoded,
261 serde_json::json!({ "kind": "rate_limit", "message": "slow down" })
262 );
264 let all = [
265 ProviderError::RateLimit(String::new()),
266 ProviderError::AuthFailed(String::new()),
267 ProviderError::Network(String::new()),
268 ProviderError::ServerError(String::new()),
269 ProviderError::InvalidRequest(String::new()),
270 ProviderError::ModelNotFound(String::new()),
271 ProviderError::Timeout(String::new()),
272 ];
273 for error in &all {
274 let encoded = serde_json::to_value(error).unwrap();
275 assert_eq!(
276 encoded.get("kind").and_then(serde_json::Value::as_str),
277 Some(expected_kind(error)),
278 "{error:?} must stay on the wire as {}",
279 expected_kind(error)
280 );
281 }
282 }
232284 #[test]
233285 fn provider_errors_carry_their_message() {
234286 // The classification drives failover; the message is what reaches the

smith/src/tool.rs

Modified

+35 / −0 text lines

209209 assert_eq!(restored, definition);
210210 }
212 #[test]
213 fn a_definition_flattens_through_cbor_too() {
214 // Inspect the encoded bytes, not a round-trip. A round-trip is invariant
215 // to `#[serde(flatten)]`: drop the attribute and encoder and decoder
216 // still agree with each other — nested — so the test stays green while
217 // the wire shape has changed underneath it. Only the encoding says
218 // whether the spec's fields sit at the top level.
219 //
220 // CBOR gets its own assertion rather than inheriting the JSON one at
221 // :197 because CBOR is what §6.6 persists and what the §6.11 trace codec
222 // will replay, and `flatten` forces a map encoding that self-describing
223 // formats do not all treat alike.
224 let definition = Echo.definition();
225 let mut bytes = Vec::new();
226 ciborium::into_writer(&definition, &mut bytes).unwrap();
228 let encoded: ciborium::value::Value = ciborium::from_reader(bytes.as_slice()).unwrap();
229 let map = encoded.as_map().unwrap();
230 let keys: Vec<&str> = map.iter().filter_map(|(key, _)| key.as_text()).collect();
232 assert!(
233 !keys.contains(&"spec"),
234 "the spec must flatten, not nest: {keys:?}"
235 );
236 for expected in ["name", "description", "parameters", "execution_mode"] {
237 assert!(
238 keys.contains(&expected),
239 "{expected} must sit at the top level: {keys:?}"
240 );
241 }
243 let restored: ToolDefinition = ciborium::from_reader(bytes.as_slice()).unwrap();
244 assert_eq!(restored, definition);
245 }
212247 #[test]
213248 fn tools_are_sequential_unless_they_say_otherwise() {
214249 assert_eq!(ToolExecutionMode::default(), ToolExecutionMode::Sequential);