-
Notifications
You must be signed in to change notification settings - Fork 1
feat: runtime SessionDict validation at JSONL parse boundary #49
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
37cf318
feat: runtime SessionDict validation at JSONL parse boundary
clean6378-max-it 49393b2
fix: expose SessionValidationError.detail and fix test mypy types
clean6378-max-it 131194f
fix: address PR #49 review (preserve dict keys, role test, parser type)
clean6378-max-it c7122dc
refactor(validation): DRY field checks, clearer root path, null test
clean6378-max-it File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
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
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,7 +1,16 @@ | ||
| """HTTP error response shapes.""" | ||
| """HTTP error response shapes and domain validation errors.""" | ||
|
|
||
| from typing import TypedDict | ||
|
|
||
|
|
||
| class ErrorResponse(TypedDict): | ||
| error: str | ||
|
|
||
|
|
||
| class SessionValidationError(ValueError): | ||
| """Raised when parsed JSONL output does not match SessionDict contract.""" | ||
|
|
||
| def __init__(self, path: str, detail: str) -> None: | ||
| self.path = path | ||
| self.detail = detail | ||
| super().__init__(f"Session validation failed at {path}: {detail}") | ||
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,86 @@ | ||
| """Runtime validation at the JSONL → SessionDict boundary.""" | ||
|
|
||
| import os | ||
| import sys | ||
| from typing import Any | ||
|
|
||
| import pytest | ||
|
|
||
| sys.path.insert(0, os.path.join(os.path.dirname(__file__), "..")) | ||
|
|
||
| from models.errors import SessionValidationError # noqa: E402 | ||
| from utils.jsonl_parser import parse_session # noqa: E402 | ||
| from utils.validation import validate_session_dict # noqa: E402 | ||
|
|
||
| FIXTURES = os.path.join(os.path.dirname(__file__), "fixtures") | ||
|
|
||
|
|
||
| def _valid_payload(**overrides: Any) -> dict[str, Any]: | ||
| base: dict[str, Any] = { | ||
| "session_id": "abc123", | ||
| "title": "Test Session", | ||
| "messages": [{"role": "user", "text": "hello"}], | ||
| "metadata": {"session_id": "abc123"}, | ||
| } | ||
| base.update(overrides) | ||
| return base | ||
|
|
||
|
|
||
| class TestValidateSessionDict: | ||
| def test_missing_session_id(self): | ||
| payload = _valid_payload() | ||
| del payload["session_id"] | ||
| with pytest.raises(SessionValidationError) as exc_info: | ||
| validate_session_dict(payload) | ||
| assert exc_info.value.path == "session_id" | ||
|
|
||
| def test_wrong_type_session_id(self): | ||
|
clean6378-max-it marked this conversation as resolved.
|
||
| with pytest.raises(SessionValidationError) as exc_info: | ||
| validate_session_dict(_valid_payload(session_id=123)) | ||
| assert exc_info.value.path == "session_id" | ||
|
|
||
| def test_null_session_id(self): | ||
| with pytest.raises(SessionValidationError) as exc_info: | ||
| validate_session_dict(_valid_payload(session_id=None)) | ||
| assert exc_info.value.path == "session_id" | ||
| assert exc_info.value.detail == "must not be null" | ||
|
|
||
| def test_null_role_in_message(self): | ||
| with pytest.raises(SessionValidationError) as exc_info: | ||
| validate_session_dict( | ||
| _valid_payload(messages=[{"role": None, "text": "x"}]) | ||
| ) | ||
| assert exc_info.value.path == "messages[0].role" | ||
|
|
||
| def test_missing_role_in_message(self): | ||
| with pytest.raises(SessionValidationError) as exc_info: | ||
| validate_session_dict( | ||
| _valid_payload(messages=[{"text": "no role key"}]) | ||
| ) | ||
| assert exc_info.value.path == "messages[0].role" | ||
|
|
||
| def test_metadata_not_dict(self): | ||
| with pytest.raises(SessionValidationError) as exc_info: | ||
| validate_session_dict(_valid_payload(metadata="not-a-dict")) | ||
| assert exc_info.value.path == "metadata" | ||
|
|
||
| def test_message_not_dict(self): | ||
| with pytest.raises(SessionValidationError) as exc_info: | ||
| validate_session_dict(_valid_payload(messages=["not-a-dict"])) | ||
| assert exc_info.value.path == "messages[0]" | ||
|
|
||
| def test_valid_payload_returns_session_dict(self): | ||
| result = validate_session_dict(_valid_payload()) | ||
| assert result["session_id"] == "abc123" | ||
| assert result["messages"][0]["role"] == "user" | ||
|
|
||
|
|
||
| class TestParseSessionValidationRegression: | ||
| def test_session_minimal_fixture_unchanged(self): | ||
| path = os.path.join(FIXTURES, "session_minimal.jsonl") | ||
| session = parse_session(path) | ||
| assert session["session_id"] == "session_minimal" | ||
| assert session["title"] == "Hello from integration fixture" | ||
| assert len(session["messages"]) == 2 | ||
| assert session["messages"][0]["role"] == "user" | ||
| assert session["messages"][1]["role"] == "assistant" | ||
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
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,56 @@ | ||
| """Runtime validation for TypedDict shapes at untrusted-data boundaries.""" | ||
|
|
||
| from typing import Any, cast | ||
|
|
||
| from models.errors import SessionValidationError | ||
| from models.session import SessionDict | ||
|
|
||
| _ROOT_PATH = "(root)" | ||
|
|
||
|
|
||
| def _require_field( | ||
| container: dict[str, Any], | ||
| key: str, | ||
| expected_type: type[Any], | ||
| type_label: str, | ||
| *, | ||
| path: str | None = None, | ||
| ) -> Any: | ||
| field_path = path or key | ||
| if key not in container: | ||
| raise SessionValidationError(field_path, "missing required field") | ||
| return _require_value(field_path, container[key], expected_type, type_label) | ||
|
|
||
|
|
||
| def _require_value( | ||
| path: str, | ||
| val: Any, | ||
| expected_type: type[Any], | ||
| type_label: str, | ||
| ) -> Any: | ||
| if val is None: | ||
| raise SessionValidationError(path, "must not be null") | ||
| if not isinstance(val, expected_type): | ||
| raise SessionValidationError( | ||
| path, f"expected {type_label}, got {type(val).__name__}" | ||
| ) | ||
| return val | ||
|
|
||
|
|
||
| def validate_session_dict(data: dict[str, Any]) -> SessionDict: | ||
|
clean6378-max-it marked this conversation as resolved.
|
||
| """Validate a plain dict matches SessionDict before returning it.""" | ||
| # Runtime guard for dynamic callers; mypy already types the parameter as dict. | ||
| if not isinstance(data, dict): | ||
| raise SessionValidationError(_ROOT_PATH, "expected dict") | ||
|
|
||
| _require_field(data, "session_id", str, "str") | ||
| _require_field(data, "title", str, "str") | ||
| messages = _require_field(data, "messages", list, "list") | ||
| _require_field(data, "metadata", dict, "dict") | ||
|
|
||
| for index, message in enumerate(messages): | ||
| path = f"messages[{index}]" | ||
| msg_dict = _require_value(path, message, dict, "dict") | ||
| _require_field(msg_dict, "role", str, "str", path=f"{path}.role") | ||
|
|
||
| return cast(SessionDict, data) | ||
Oops, something went wrong.
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.
Uh oh!
There was an error while loading. Please reload this page.