Repository navigation
refine(runtime): HandleRef::expect_owned_by + CommandRequest::handle_id_or_legacy (re-opens #1491) - #1498
Merged
Conversation
…id_or_legacy Per Joel: > "You get to refine the pattern with better knowledge, therefore > improving elegance and reliability" Distill two primitives from the kinks the first real HandleRef consumer (PR #1490) had to handle inline, so every future consumer reaches for the substrate rather than reimplementing them. # The primitives ## HandleRef::expect_owned_by(owner, type_tag) -> Result<Uuid, String> The canonical handle-validation entry point. Returns the inner UUID when both the owner and type_tag match expectations; otherwise emits typed errors that name BOTH the offending value AND the expected value. Owner-mismatch is checked first (owner determines routing) and the error explicitly hints at the grid-interceptor responsibility — the diagnostic turns "weird error" into "ah, the interceptor misfired" or "ah, this caller built a bogus handle." Replaces ~12 lines of validation boilerplate per handle-consuming handler. Standardizes the error format across every module that uses handles. ## CommandRequest::handle_id_or_legacy(...) The single primitive shared by every additive migration of a stringly-typed id to a typed HandleRef. Walks two shapes: 1. envelope `handle` (new canonical) — validated via expect_owned_by, error prefixed with the command name 2. legacy string field on the params (back-compat) 3. neither → typed error naming BOTH supported shapes so the caller knows what to add Returns the resolved id as a String — the historical wire format every consumer's state map is already keyed on. New modules that key state by Uuid natively can `Uuid::parse_str` the result; legacy-only strings parse-fail there, which is fine because handle-only consumers post-migration don't have a legacy field to fall back to. Replaces ~25 lines of bespoke resolver per migration. Standardizes the error format across every dual-shape migration. # The consumer-side win (data.rs) Before (35-line `resolve_query_cursor_id` static fn + two callsites that each invoked it): ```rust fn resolve_query_cursor_id(handle, legacy, command) -> Result<...> { if let Some(h) = handle { if h.owner != DATA_MODULE_OWNER { return Err(...); } // 6 lines if h.type_tag != QUERY_CURSOR_TYPE_TAG { return Err(...); } // 6 lines return Ok(h.id.to_string()); } if let Some(id) = legacy { return Ok(id.clone()); } Err(format!("...")) // 4 lines } // Plus the two callsites: Self::resolve_query_cursor_id(...) ``` After (the static fn is gone; callsites invoke the substrate primitive directly): ```rust let cursor_id = req.handle_id_or_legacy( DATA_MODULE_OWNER, QUERY_CURSOR_TYPE_TAG, "queryId", &req.params.query_id, "data/query-next", )?; ``` Net: -84 lines from data.rs. The 411-line substrate addition is all either documentation, tested primitives, or new substrate-level tests — every future handle consumer benefits from this shrink, not just data. # Tests (48 pass, 1 ignored — onnxruntime, unrelated) ## New (runtime::cell_shapes::tests, 5) - `expect_owned_by_returns_uuid_when_owner_and_type_match` — happy path - `expect_owned_by_rejects_wrong_owner_with_both_values_named` - `expect_owned_by_rejects_wrong_type_tag_with_both_values_named` - `expect_owned_by_checks_owner_first_then_type` — pins routing-first precedence (owner before type) - `expect_owned_by_error_includes_routing_hint` — pins the grid-interceptor diagnostic in the owner-mismatch error ## New (runtime::command_envelope::tests, 6) - `handle_id_or_legacy_prefers_envelope_handle_when_both_present` — precedence (envelope wins) so consumers mid-migration don't diverge from new consumers about which id the resolver sees - `handle_id_or_legacy_falls_back_to_legacy_string_when_no_handle` - `handle_id_or_legacy_errors_loud_when_neither_shape_provided` - `handle_id_or_legacy_prepends_command_name_to_handle_validation_errors` - `handle_id_or_legacy_propagates_type_mismatch_with_command_name` - `handle_id_or_legacy_uses_canonical_uuid_string_for_handle_path` — pins the bridge-format invariant: handle-path and legacy-path resolve to the SAME string representation ## Pre-existing (modules::data::tests, all 17 still pass) The 10 HandleRef migration tests + 7 pre-existing cursor tests exercise the SAME behavior they did before through the refactored callsites. No regression — net effect is the substrate now owns what data.rs used to own inline. # What this PR explicitly does NOT do - Does NOT add convenience constructors like `CommandResponse::with_handle_minted` (auto-generate UUID). That case is one line (`Uuid::new_v4()` then `with_handle(...)`); the primitive doesn't justify the API surface. - Does NOT add a `handle_type!(QueryCursor)` macro that derives the type_tag string from the module + struct name at compile time. Worth considering, but the doc-convention `const QUERY_CURSOR_TYPE_TAG = "data::QueryCursor"` pattern is already cheap and explicit. - Does NOT touch other handle-related types (Stream, Lambda placeholders). Those are reserved-but-unused; their kinks will surface when they get real consumers. # References - PR #1485 (cell shapes — HandleRef defined here, extended here) - PR #1486 (envelope pattern — CommandRequest defined here, extended here) - PR #1490 (first real HandleRef consumer — the inline boilerplate this PR distills lived there) Co-Authored-By: Claude Opus 4.7 <[email protected]>
joelteply
force-pushed
the
refine/handle-validation-primitives-v2
branch
from
May 31, 2026 00:32
4dcf491 to
fed8d4b
Compare
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.
Re-opens #1491 which auto-closed when its base (
feat/data-query-handle-migration) was deleted with #1490's auto-close.What lands
Same content as #1491: two substrate primitives distilled from the data-cursor migration's inline boilerplate:
HandleRef::expect_owned_by(owner, type_tag) → Result<Uuid, String>— canonical handle-validation entry pointCommandRequest::handle_id_or_legacy(...)— dual-shape resolver for any migration from string-typed ids to typed handles11 new substrate tests + the data-cursor consumer refactored to use them (-84 lines from data.rs).
Stacks on
#1497 (the new home of #1490's content). Will need to retarget to
canaryonce #1497 merges.Original PR
Full description on #1491. Closed with #1490.