Review of PR #533 — Cap the per-request context (correctness lens)
Reviewed at head de1b5b5 (9 files, +98/−18). Base: main at c36a6a02. PR: https://github.com/NVIDIA/hyperflare-inference/pull/533
Key findings
The production change is now sound: four blockers I reported against the earlier head have all been fixed, and I re-verified each of them in the code. What remains blocking is the test the PR itself adds — it cannot pass — plus two nits.
- Blocking —
tests/cli/test_planning.py:230: the "clamp" case plans withcontext=500000, which the PR's own page-multiple validation rejects (500000 % 64 == 32), so the test dies withUsageErrorbefore the assertion it is testing. - Blocking —
tests/cli/test_planning.py:257: asserts a protobuf fieldint64_valuethat does not exist, and even corrected toint_valuewould read 0, because the manifest writes non-negative integers intouint_value. - Nit —
ignition/planning.py:375: a target above the recipe maximum is silently clamped, whiledocs/cli-design.md:446describes the flag as a cap below that maximum. - Nit —
ignition/cli/serve.py:68:max_body_bytesstill sizes the image budget fromrecipe.context, notprepared_context, so a capped core accepts HTTP bodies for prompts the engine will reject at admission.
Previously-reported blockers now fixed (verified): multi-host _export_hosts forwards the flag; Plan.__post_init__ enforces the page multiple; to_dict() and reproduction_arguments()/from_plan() carry the cap; bench validates against prepared_context.
Because the head commit has no CI checks, these two test defects were never exercised. pytest tests/cli/test_planning.py settles both — they are plain arithmetic and protobuf-schema facts, not judgement calls.
Blocking
1. The added test cannot pass: the clamp case violates the new page-multiple validation
File: tests/cli/test_planning.py:229-232 (against ignition/planning.py:289-290)
# Clamp: target > recipe max -> prepared_context == min(recipe.context, tokens)
oversized = planning.plan(recipe, recipe.checkpoints[0], gpus, context=500000, **base)
assert oversized.context_target == 500000
assert oversized.prepared_context == min(recipe.context, oversized.tokens)
- The fixture
_recipe()(tests/cli/test_planning.py:22-54) declarescontext=262144, page_tokens=64; the fixture'sRankCosts(...)(:43) leavespage_tokensunset (ignition/declaration.py,RankCosts.page_tokensdefaultNone), soPlan.page_tokensfalls back torecipe.page_tokens= 64 (ignition/planning.py:329-332). 500000 % 64 == 32, and this PR adds atignition/planning.py:289-290:if self.context_target is not None and self.context_target % self.page_tokens: raise UsageError(f"--request-context {self.context_target:,} is not a multiple of the {self.page_tokens:,}-token page size")planning.plan(...)constructs aPlanthrough thefitted()closure (ignition/planning.py:673-675, 662-674), so__post_init__runs and raisesUsageError("--request-context 500,000 is not a multiple of the 64-token page size"). The "clamp" assertion never executes and the whole test function fails.- The test's own rejection case two blocks later (
:241-242,context=100) is precisely the unaligned-input case;500000belongs in the same bucket, so the clamp case currently contradicts the behaviour the test is validating.
Fix: use a page-aligned over-size target for the clamp case — 524288 = 64 × 8192, which is above recipe.context (262144) and therefore clamps to min(recipe.context, tokens). Keep 100 for the rejection case.
Related design point to settle while touching this: the validation checks the raw context_target, not the clamped prepared_context (ignition/planning.py:375). A target above recipe.context/tokens that is unaligned is rejected even though it changes nothing and would clamp to an aligned value. Decide explicitly whether that should be accepted, floored to a page, or rejected, and state it in the docs row at docs/cli-design.md:446.
2. The manifest assertion names a protobuf field that does not exist
File: tests/cli/test_planning.py:257 (against helios/proto/runtime_core.proto:124-133, ignition/preparation/rebuild_manifest.py:329-333)
assert by_key["context_target"].value.int64_value == 131072
PreparationValue(helios/proto/runtime_core.proto:124-133) is a oneof ofbool_value=1,int_value=2,uint_value=3,float_value=4,string_value=5,list_value=6. There is noint64_value.- Python protobuf exposes only the declared field names, so this raises
AttributeError: Protocol message PreparationValue has no "int64_value" field, failing the test before any value comparison. - Even with the name "corrected" to
int_value, the assertion would read0:_preparation_value(ignition/preparation/rebuild_manifest.py:329-333) returns{'uint_value': value} if value >= 0 else {'int_value': value}, and131072 >= 0, so the populated oneof member isuint_valueandint_valuestays at its default 0.
Fix: assert by_key["context_target"].value.uint_value == 131072.
Nits
3. A cap above the recipe maximum is clamped silently
File: ignition/planning.py:375 (return min(self.recipe.context, self.context_target, self.tokens)) and ignition/planning.py:459-460 (the only validation, context <= 0)
--request-context 300000 on a 256K recipe prepares 262144 with no notice. That is defensible as a clamp — and the pool clamp is documented ("a cap above the pool resolves to the pool capacity, as stock") — but docs/cli-design.md:446, added by this PR, describes the flag as a cap below the recipe's maximum, so a user who believes they have raised the ceiling gets no signal that they have not. The plan report now carries context_target (ignition/planning.py:432) but not the effective value; adding "prepared_context": self.prepared_context to Plan.to_dict() (or a stderr line when the clamp bites) makes it visible and costs one line.
4. max_body_bytes still sizes the image budget from the recipe maximum
File: ignition/cli/serve.py:68
The served body cap is still computed from recipe.context rather than prepared_context. With a cap below the recipe maximum, the frontend accepts request bodies (image payload budgets included) sized for prompts the engine will reject at admission. Harmless functionally — the request fails with the documented context-ceiling error — but it is the one remaining place in the changed area that still treats the recipe maximum as the served context.
Verified and correct (no finding)
Recorded so the coverage is explicit:
- Multi-host export honours the flag.
ignition/cli/serve.py:206now passescontext=args.request_contextin_export_hosts, matching_prepare(:289) anddebug_serve(:472). The regression I reported earlier is closed. - The exported core is page-aligned.
ignition/planning.py:289-290rejects a target that is not a multiple ofPlan.page_tokens(recipe page tokens × DCP), which is what Helios requires at load (helios/src/model/catalog.rs,prepared_sequence_tokens/limits.max_sequence_tokensmultiples ofpage_tokens). The "exports then fails to load" hole is closed. - The cap reaches reproduction and inspection.
ignition/preparation/rebuild_manifest.py:264-265emits--request-contextinreproduction_arguments(),:390recordscontext_targetasPREPARATION_PARAMETER_SCOPE_DEPLOYMENT, andignition/planning.py:432reportscontext_targetinPlan.to_dict().AGENTS.md:452-456is satisfied. - Bench validates against the cap.
ignition/planning.py:248now comparesselection.required_contextagainstself.prepared_context, so an over-large--contextsselection fails at planning rather than after GPU preparation inignition/bench/runner.py. - No-flag path is stock.
prepared_contextreturnsrecipe.contextwhencontext_target is None(ignition/planning.py:370-374), and the previous value at the consumer wasplan.recipe.context(ignition/preparation/bootstrap.py:58), so omitting the flag yields an identical preparation config, core metadata and KV pool.Plan.contextis untouched (:365-368), so pool sizing,guaranteed_tokensandauto/multi-host selection behave as before. The no-op claim holds. - Pool clamp is correct.
min(recipe.context, context_target, self.tokens)(:375) never exceeds physical KV capacity. - Validation placement is right.
fit()rejectscontext <= 0(:459-460) and is the only CLI-reachablePlanconstructor;plan()reaches it throughfitted(); argparsetype=int(ignition/cli/__init__.py:167-170) rejects non-integers. - Prefill-chunk error is improved and correct.
ignition/planning.py:696-700compares the chunk againsteffective_context = min(result.context, result.prepared_context)and distinguishes the recipe default chunk from an explicit one — the right bound and an actionable message. - Compatibility of the new field.
Planis a frozen dataclass andcontext_targetis added with a default ofNone(:176), so every existing positional/keyword construction still works, anddataclasses.replace()copies it, so thereplace(plan, ...)calls in the CLI paths preserve the cap. - No forbidden per-model branching. The flag is generic; the external
hyperflare_recipesrepository contains no reference torequest_context/prepared_context(charter, "Keep the runtime small and model-agnostic").
Files read in full or in the relevant spans: ignition/planning.py (Plan 160-442, fit 445-542, plan 583-732), ignition/preparation/bootstrap.py, ignition/cli/{__init__,bench,serve,preparation}.py, ignition/preparation/{rebuild_manifest,selection,execution,model}.py, ignition/declaration.py, helios/src/{server/config.rs, model.rs, model/catalog.rs, scheduler.rs (not opened), core/memory.rs (not opened)}, helios/proto/runtime_core.proto, tests/cli/{test_planning,test_commands}.py, docs/cli-design.md, AGENTS.md, the charter.
Open questions / what a test must settle
- Do the two test defects reproduce? They follow from arithmetic and the declared protobuf schema, but I could not run the suite.
pytest tests/cli/test_planning.pyconfirms both; the head commit has no CI checks, which is presumably why they were not seen. - Should an unaligned target above the recipe maximum be rejected? Today it is (see finding 1's design note), even though it clamps to an aligned value and changes nothing. This needs a decision, not a measurement.
- Does any recipe re-round
config.contextbefore declaringmax_sequence_tokens? Recipes live in the externalhyperflare_recipesrepository (AGENTS.md:381), outside this diff. Nothing in this repository rounds, and every alignment check lives in Helios (after export), so the ignition-side validation atplanning.py:289-290is the right place regardless.