fix(BA-5967): make ResourceSlot.to_humanized() resilient to unknown slots#11505
Closed
fregataa wants to merge 2 commits into
Closed
fix(BA-5967): make ResourceSlot.to_humanized() resilient to unknown slots#11505fregataa wants to merge 2 commits into
fregataa wants to merge 2 commits into
Conversation
…lots Resolve each slot independently via per-slot try/except so a missing slot_types entry falls back to _guess_slot_type instead of aborting the whole formatter. Eliminates 500 Internal Server Errors when error messages are humanized against partially-populated slot type metadata. Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
Contributor
There was a problem hiding this comment.
Pull request overview
Note
Copilot was unable to run its full agentic suite in this review.
Makes ResourceSlot.to_humanized() tolerant of missing slot_types entries by resolving units per-slot and falling back to _guess_slot_type(), preventing formatting-time 500s from masking user-facing 4xx errors.
Changes:
- Update
ResourceSlot.to_humanized()to fall back per key whenslot_typeslacks an entry - Add unit tests covering unknown-slot fallback and empty
slot_types - Add a changelog entry documenting the behavior change
Reviewed changes
Copilot reviewed 3 out of 3 changed files in this pull request and generated 3 comments.
| File | Description |
|---|---|
src/ai/backend/common/types.py |
Implements per-slot unit resolution with fallback guessing instead of raising on missing keys |
tests/unit/common/test_types.py |
Adds regression tests validating fallback behavior and empty slot_types handling |
changes/11505.fix.md |
Documents the bugfix and its impact on avoiding 500s during error formatting |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
Comment on lines
1225
to
+1236
| def to_humanized(self, slot_types: Mapping[str, Any]) -> Mapping[str, str]: | ||
| try: | ||
| return { | ||
| k: type(self)._humanize_value(Decimal(v), slot_types[k]) | ||
| for k, v in self.data.items() | ||
| if v is not None | ||
| } | ||
| except KeyError as e: | ||
| raise ValueError(f"Unknown slot type: {e.args[0]!r}") from e | ||
| def _resolve_unit(key: str) -> Any: | ||
| try: | ||
| return slot_types[key] | ||
| except KeyError: | ||
| return type(self)._guess_slot_type(key) | ||
|
|
||
| return { | ||
| k: type(self)._humanize_value(Decimal(v), _resolve_unit(k)) | ||
| for k, v in self.data.items() | ||
| if v is not None | ||
| } |
jopemachine
reviewed
May 8, 2026
| @@ -1223,14 +1223,17 @@ def from_user_input( | |||
| return cls(data) | |||
|
|
|||
| def to_humanized(self, slot_types: Mapping[str, Any]) -> Mapping[str, str]: | |||
Member
There was a problem hiding this comment.
Could we change the parameter type to Mapping[SlotName, SlotTypes]?
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
ResourceSlot.to_humanized()via per-slottry/except KeyError, falling back to_guess_slot_type()for keys missing from the suppliedslot_typesmap.ResourceLimitRule._humanize_slots) ran against partially-populated slot type metadata — a missing display-metadata entry no longer clobbers the underlying user-facing 4xx.Test plan
tests/unit/common/test_types.pycover the per-slot fallback and the empty-slot_typescase.pants fmt,pants fix,pants lint --changed-since=origin/mainpass locally.ResourceLimitRule._humanize_slots, scheduler predicates) continue to work — verifiedtests/unit/common/test_types.pyandtests/unit/manager/sokovan/scheduling_controller/validators/test_session_spec_rules.py.Resolves BA-5967
🤖 Generated with Claude Code