Subtraction, Simplicity & Conventions review — PR #689
Key findings
- The production diff is already lean.
failure_response(http.rs:26-31) is an extraction, not a wrapper: it removes body-building duplication sorejection(http.rs:32-41) collapses to three lines.PreparationErrorreplaces the oldrejection_details(…, AdmissionError::Invalid)+http::errorhack with a three-arm classifier. No flags, config knobs, env vars,#[allow]s, new dependencies, or dead public API. Nothing to subtract at the design level. - Finding 1 (highest value, ~28 lines): the new wire test's
make_stateclosure hand-rebuilds a fullHttpStatefield-for-field (phase_tests.rs:8452-8480) instead of mutating the sharedhttp_state_forhelper, which is the pattern the neighbouring test uses (phase_tests.rs:5086-5087). - Finding 2 (~100 lines): three sections of the same wire test are the identical six-line loop body, differing only in the expected code string and one boolean (phase_tests.rs:8632-8638, 8689-8695, 8711-8717). Folding them into one table keeps coverage and makes the matrix readable.
- Finding 3 (medium):
preparation_error'sFrontendError::ImageDecodearm (http.rs:127-129) cannot fire from the only call path, because every reachable image site is already re-typed toAdmissionError::Imageinsidedescribe_images(request.rs:1141, 1194, 1196, 1208, 1230). It is a narrower second copy ofrequest::prepare's mapping (request.rs:30-36) and is the natural deletion point for the asymmetry the dossier flags. - Repo conventions:
AGENTS.md(root) asks to "reject unsupported abstractions … do not add fallback layers," which supports Finding 3. There is nohelios/AGENTS.md. The PR description carries the four parts the charter asks for. No structural convention violations found. - What is not a finding, stated plainly: the
failure_response/rejectionsplit (http.rs:26-41) correctly keepslog_failure+with_request_idpost-submission only whilefailure_responsestays pure;rejection_details(http.rs:60-72) is reused rather than duplicated; and the unit test at http.rs:739-786 is a good test even though it partly mirrors theerror_codetable (server.rs:743-755).
1. Delete the hand-rebuilt HttpState literal; mutate the shared helper — ~28 lines
helios/src/server/runtime/phase_tests.rs:8452-8480:
let base = http_state_for(fixture, sender, None, &[]);
let state = Arc::new(super::super::HttpState {
first_response: base.first_response.clone(),
log_to_stderr: false,
model: base.model.clone(),
frontend: base.frontend.clone(),
images: with_images.then(|| {
Arc::new(
ImageProcessor::load(ImageSettings::Deepseek4 {}, ImageLimits::default())
.unwrap(),
)
}),
grammar: base.grammar.clone(),
frontend_slots: base.frontend_slots.clone(),
ingress_slots: base.ingress_slots.clone(),
max_body_bytes: 4096,
…/* 11 more field copies ending at accepting: Mutex::new(true) */
This copies every field of HttpState (the full literal the helper already builds at phase_tests.rs:4273-4295) just to change three: images, context_hint, and max_body_bytes: 4096 (needed only so the 4,200-byte body case at :8787 trips PAYLOAD_TOO_LARGE).
The established pattern in the same file is one line of mutation — phase_tests.rs:5086-5087:
let mut state = http_state_for(fixture, sender, None, &[]);
Arc::get_mut(&mut state).unwrap().thinking_token_budget = Some(2);
Replace the whole literal with:
let mut state = http_state_for(fixture, sender, None, &[]);
{
let state = Arc::get_mut(&mut state).unwrap();
state.max_body_bytes = 4096;
state.context_hint = context_hint.to_owned();
if with_images {
state.images = Some(Arc::new(
ImageProcessor::load(ImageSettings::Deepseek4 {}, ImageLimits::default()).unwrap(),
));
}
}
(state, receiver)
Beyond the deleted lines, this removes a duplicate of every HttpState field that will silently rot: the same-day sibling PRs #691 and #692 both touch HttpState/http::generate, so any new field would require a second edit here for no reason.
2. Collapse three identical loop bodies into one table — ~100 lines
Sections "2. unsupported_feature", "3. invalid_grammar" and "4. invalid_image" end with the same six lines. Verbatim from phase_tests.rs:8632-8638:
] {
let (state, receiver) = make_state(false, "");
let (status, body) = send_raw(state, path, None, payload.to_string()).await;
assert_eq!(status, StatusCode::BAD_REQUEST, "{path}: {body}");
assert_envelope(path, &body, "invalid_request_error", "unsupported_feature");
assert!(receiver.try_recv().is_err(), "{path}");
}
and again at :8689-8695 with "invalid_grammar", and at :8711-8717 with "invalid_image" and make_state(true, ""). Only the code string and the with_images flag differ.
Replace the three let invalid_schema = …; for (path, payload) in [...] {} blocks with a single table over the existing entries:
let invalid_schema = json!({"type": "not-a-valid-json-schema-type"});
for (expected_code, with_images, path, payload) in [
("unsupported_feature", false, "/v1/chat/completions", json!({…logit_bias…})),
// … remaining 6 unsupported_feature rows, 6 invalid_grammar rows, 3 invalid_image rows
] {
let (state, receiver) = make_state(with_images, "");
let (status, body) = send_raw(state, path, None, payload.to_string()).await;
assert_eq!(status, StatusCode::BAD_REQUEST, "{path}: {body}");
assert_envelope(path, &body, "invalid_request_error", expected_code);
assert!(receiver.try_recv().is_err(), "{path}");
}
Coverage is unchanged and should be kept; only the shape changes. The benefit is that the covered (code, route, payload-class) matrix becomes readable at a glance instead of split across three blocks with duplicated assert scaffolding.
3. Delete the speculative ImageDecode arm in preparation_error
helios/src/server/http.rs:124-132:
fn preparation_error(error: Error) -> PreparationError {
match error {
Error::Admission(_) => PreparationError::Failure(error),
Error::Frontend(crate::FrontendError::ImageDecode(_)) => {
PreparationError::Failure(error.admission_context(AdmissionError::Image))
}
other => PreparationError::Http(StatusCode::BAD_REQUEST, other.to_string()),
}
}
Every image-decode site reachable from generate's block is re-typed before the error can reach here: describe_images maps check_batch, decode_encoded, describe_image, geometry and check_budget with .map_err(|error| error.admission_context(AdmissionError::Image)) at helios/src/server/request.rs:1141, 1194, 1196, 1208, 1230, and the raw image rejects are constructed as AdmissionError::Image directly. prepare_validated_inference is the only preparation step that touches images (http.rs:270-280), so this arm is unreachable in practice.
It is also a second, narrower copy of request::prepare's mapping (request.rs:30-36), which types ImageDecode → Image and everything else → Invalid:
prepare_inner(frontend, processor, compiler, config, body).map_err(|error| match error {
Error::Admission(_) => error,
Error::Frontend(FrontendError::ImageDecode(_)) => {
error.admission_context(AdmissionError::Image)
}
_ => error.admission_context(AdmissionError::Invalid),
})
Because preparation_error special-cases only ImageDecode, other FrontendError variants surfacing during preparation still flatten to invalid_request_error. Deleting the arm removes the divergence at its source and matches AGENTS.md's "do not add fallback layers."
Replace with:
fn preparation_error(error: Error) -> PreparationError {
match error {
Error::Admission(_) => PreparationError::Failure(error),
other => PreparationError::Http(StatusCode::BAD_REQUEST, other.to_string()),
}
}
Then delete the corresponding unit-test row at helios/src/server/http.rs:758-761:
(
Error::frontend(crate::FrontendError::ImageDecode(Failure::new("corrupt png"))),
"invalid_image",
),
(If maintainers instead want the request::prepare contract, the opposite move — type all non-Admission errors as Invalid — would make the arm disappear too, but that is a wire-behaviour decision for the Correctness & Contracts lens, not a simplification.)
4. Optional micro-simplification: decode_body's hand-written conversion
helios/src/server/http.rs:212-213:
let body = decode_body(&bytes, &encoding, prepare_state.max_body_bytes)
.map_err(|(status, message)| PreparationError::Http(status, message))?;
The closure is exactly a From<(StatusCode, String)> impl for PreparationError. Adding impl From<(StatusCode, String)> for PreparationError next to the enum (http.rs:119-122) reduces the call site to decode_body(&bytes, &encoding, prepare_state.max_body_bytes)?. One line; noted for completeness, not worth a round trip on its own.
Not a finding
failure_response/rejectionsplit is correct and minimal.http.rs:26-41keepslog_failure(only whenstate.log_to_stderr) andwith_request_idon the post-submission path, so thex-request-idand logging contract indocs/api.md(not opened) anddocs/core-design.md:1119-1121(not opened) is preserved, while the shared body construction lives in one place.rejection_detailsis reused, not duplicated (http.rs:60-72 is untouched and called by both paths), socontext_hint/capacity_hintrendering has a single implementation.- The unit test at http.rs:739-786 partially mirrors the
error_codetable (server.rs:743-755) already covered end-to-end by the wire test. Trimming the fiveAdmissionErrorrows would be tidier, but this is a good test and only becomes actionable as a consequence of Finding 3. - No structural convention violations. Root
AGENTS.mdrequires no flags/env knobs/fallback abstractions and the four-part PR description; all are satisfied. There is nohelios/AGENTS.md.
Open questions
- Whether maintainers want the
ImageDecodearm gone (Finding 3) or the broader typing of allFrontendErrorvariants during preparation — either removes the asymmetry, but they change wire behaviour differently. - Whether the
AdmissionError::Invalid→invalid_requestchange is in scope beyond #446's four codes; not a simplification concern, but the biggest client-visible delta. - Whether
docs/api.md:248-259(not opened) should list the typed 400 codes, mirroring what sibling PR #638 did for its wire change.
Not found
No CI checks exist on head 2f72e1b, so the reported test totals cannot be reproduced here; all of the above is read from the code, not from test output.