feat(api): restrict field/property names to a portable, readable rule (#1383)#1396
Open
alighazi288 wants to merge 3 commits into
Open
feat(api): restrict field/property names to a portable, readable rule (#1383)#1396alighazi288 wants to merge 3 commits into
alighazi288 wants to merge 3 commits into
Conversation
…MemMachine#1383) Implement the validation rule requested in MemMachine#1383 for user-supplied field/property names: - starts with a lowercase letter, then [a-z0-9_] - max 32 characters - leading underscore reserved for system use Adds `_is_valid_field_name` and `SafeFieldName` in `common/api/spec.py`, alongside the existing `_is_valid_name` / `SafeId` (which validates identifiers — kept permissive). The new rule is applied to user-supplied field/property names on input/spec models. Response/entity models stay permissive so legacy records continue to deserialize, and lookup-by-name fields (`*.category_name` on `AddFeatureSpec`, `UpdateFeatureSpec`, `DisableSemanticCategorySpec`) are kept permissive — those resolve against existing categories at `semantic_memory.py:299`, so tightening would block operations on legacy categories.
CI on MemMachine#1396 surfaced two issues missed in the initial pass: - ty static check (server, 3.12/3.14): ty doesn't honor Pydantic's metaclass-generated `__init__` defaults, so direct kwargs construction (`AddFeatureSpec(...)`) of specs with optional fields was flagged as missing-argument. The rest of the file uses `Model.model_validate({...})` for exactly this reason; converted the four new tests to match. - test-install integration tests: six existing `create_semantic_set_type` fixtures in `test_integration_complete.py` used `name=` values with spaces and capitals ("User Sessions", "List Test Set Type", etc.). The live server now enforces `SafeFieldName` on `CreateSemanticSetTypeSpec.name`, so these 422'd. Renamed each to snake_case.
Contributor
Author
|
@edwinyyyu Would appreciate a review when you have the time! |
Contributor
There was a problem hiding this comment.
Pull request overview
Adds a stricter validation rule for user-supplied field/property names in the API v2 spec models to improve metadata portability/readability across storage backends (per #1383), and updates tests/fixtures to comply.
Changes:
- Introduces
SafeFieldName(and_is_valid_field_name) with a 1–32 char, lowercase-start,[a-z0-9_]rule and reserved leading_. - Applies
SafeFieldNameto selected request/spec inputs (dict keys, scalars, list items) while keeping certain lookup-by-name fields permissive for legacy compatibility. - Updates server/client tests to use compliant semantic set type names and adds unit tests for validation + Pydantic wiring.
Reviewed changes
Copilot reviewed 5 out of 5 changed files in this pull request and generated 3 comments.
Show a summary per file
| File | Description |
|---|---|
| packages/common/src/memmachine_common/api/spec.py | Adds SafeFieldName validation and applies it to relevant request/spec model fields. |
| packages/server/server_tests/memmachine_server/server/api_v2/test_spec.py | Adds validator/wiring test coverage for SafeFieldName and carveout behavior. |
| packages/server/server_tests/memmachine_server/server/api_v2/test_router.py | Updates REST router test payload/assertions for new name/tag rules. |
| packages/client/client_tests/test_memory.py | Updates client test expectations for semantic set type name. |
| packages/client/client_tests/test_integration_complete.py | Updates integration tests to use compliant semantic set type name values. |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
Comment on lines
+193
to
+196
| raise InvalidNameError( | ||
| "Field/property name must start with a-z and contain only " | ||
| f"[a-z0-9_]: '{v}'", | ||
| ) |
Comment on lines
+182
to
+186
| if not v or len(v) > MAX_FIELD_NAME_LENGTH: | ||
| raise InvalidNameError( | ||
| f"Field/property name must be 1-{MAX_FIELD_NAME_LENGTH} chars, " | ||
| f"got {len(v)}: '{v}'", | ||
| ) |
Comment on lines
+467
to
+468
| reused on `tag`, `feature`, `name`, `category_name` (create paths), | ||
| `tag_name`.""" |
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.
Purpose of the change
Implement the validation rule requested in #1383 for user-supplied field/property names to ensure data portability and readability across storage backends (AWS, Chroma, Neo4j) without requiring aggressive translation layers.
Description
Adds a
SafeFieldNameannotated type inpackages/common/src/memmachine_common/api/spec.pyenforcing the rule from the issue on user-supplied field/property names:[a-z0-9_]Distinct from the existing
_is_valid_name/SafeId(which validates identifiers —org_id,project_id, UIDs — and stays permissive).Where it's applied
Input/spec models only:
Dict-key types:
MemoryMessage.metadata, the four*.set_metadatafilter dicts (Search / List / GetSemanticSetId / ListSemanticSetIds),AddFeatureSpec.feature_metadata,UpdateFeatureSpec.metadata.Scalar fields:
AddFeatureSpec.{tag, feature},UpdateFeatureSpec.{tag, feature},CreateSemanticSetTypeSpec {name, metadata_tags items},GetSemanticSetIdSpec.metadata_tags items,AddSemanticCategorySpec.category_name,AddSemanticCategoryTemplateSpec.category_name,AddSemanticTagSpec.tag_name.Deliberately not touched
Response/entity models (
Episode.metadata,SemanticFeature.*, etc.): permissive so legacy records stilldeserialize. Strict input + permissive output drains the corpus as records are rewritten.
Lookup-by-name fields (
category_nameonAddFeatureSpec,UpdateFeatureSpec,DisableSemanticCategorySpec): resolved against existing categories atsemantic_memory.py:299; tightening would orphan legacy categories. Pinned bytest_lookup_carveout_stays_permissive.set_id: identifier, not a field/property name. Out of issue scope.Fixes/Closes
Fixes #1383
Type of change
How Has This Been Tested?
packages/server/server_tests/memmachine_server/server/api_v2/test_spec.pygains 7 test functions (~19 cases via parametrise):UpdateFeatureSpecpartial-update test (Nonedefaults skip validation).Plus two existing fixtures in
test_router.py/test_memory.pyrenamed from"User Sessions"→"user_sessions".Checklist
Maintainer Checklist
Further comments
Compatibility Note: This is a breaking change for REST clients attempting to submit field/property names outside the new rule. A
422 Unprocessable Entitywill be returned citing the exact rule violated (length, reserved-prefix, or charset).For read-modify-write patterns, updating a legacy record containing non-conforming keys will result in a 422 until the client scrubs or re-keys the data.