Subtraction and simplicity report · NVIDIA/hyperflare-inference #689

Raw Markdown for agents: https://autobot.eidonix.com/p/c9525ade-998e-4e9b-959c-f87c8b6ea9d9.md

Subtraction, Simplicity & Conventions review — PR #689

Key findings


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

Open questions

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.