-
Notifications
You must be signed in to change notification settings - Fork 1
feat: typed models + schema validation at DB read boundaries (closes #24) #30
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 7 commits
Commits
Show all changes
11 commits
Select commit
Hold shift + click to select a range
1f83f12
feat: typed models + schema validation at DB read boundaries (closes …
timon0305 e853dfd
review: address CodeRabbit feedback on PR #30 — tighten schema gates
timon0305 7774f41
review: route per-row workspace-local composer drift through SchemaEr…
timon0305 d983b9b
review: log non-schema read failures in list_composers (#30)
timon0305 545886d
docs: drop module docstrings and trim class docstrings to one-liners …
timon0305 26b565e
docs: drop task-reference comments and section banners (#30)
timon0305 59ae103
review: tighten ID + createdAt type gates across the model layer (#30)
timon0305 16f861d
review: wire typed models load-bearing across read sites (#30)
timon0305 b9f34a6
review: log schema drift + harden get_composer envelope (#30)
timon0305 4b8548a
Merge origin/master into feat/typed-models-schema-validation-24
timon0305 017d408
review: 2 CodeRabbit findings on PR #30 — response-shape parity + ali…
timon0305 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
Some comments aren't visible on the classic Files Changed page.
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
|
timon0305 marked this conversation as resolved.
|
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,15 @@ | ||
| from models.cli_session import CliSessionMeta | ||
| from models.conversation import Bubble, Composer, WorkspaceLocalComposer | ||
| from models.errors import SchemaError | ||
| from models.export import ExportEntry | ||
| from models.workspace import Workspace | ||
|
|
||
| __all__ = [ | ||
| "Bubble", | ||
| "CliSessionMeta", | ||
| "Composer", | ||
| "ExportEntry", | ||
| "SchemaError", | ||
| "Workspace", | ||
| "WorkspaceLocalComposer", | ||
| ] |
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,38 @@ | ||
| from __future__ import annotations | ||
|
|
||
| from dataclasses import dataclass, field | ||
| from typing import Any | ||
|
|
||
| from models.errors import SchemaError | ||
|
|
||
|
|
||
| @dataclass(frozen=True) | ||
| class CliSessionMeta: | ||
| """CLI session meta blob; latestRootBlobId is the conversation entry point and the only required field.""" | ||
|
|
||
| latest_root_blob_id: str | ||
| created_at: Any = None | ||
| raw: dict[str, Any] = field(default_factory=dict) | ||
|
|
||
| @classmethod | ||
| def from_dict(cls, raw: dict[str, Any]) -> "CliSessionMeta": | ||
| if not isinstance(raw, dict): | ||
| raise SchemaError( | ||
| "CliSessionMeta", | ||
| "meta", | ||
| hint=f"expected object, got {type(raw).__name__}", | ||
| ) | ||
| latest = raw.get("latestRootBlobId") | ||
| if not latest: | ||
| raise SchemaError("CliSessionMeta", "latestRootBlobId") | ||
| if not isinstance(latest, str): | ||
|
timon0305 marked this conversation as resolved.
|
||
| raise SchemaError( | ||
| "CliSessionMeta", | ||
| "latestRootBlobId", | ||
| hint=f"expected str, got {type(latest).__name__}", | ||
| ) | ||
| return cls( | ||
| latest_root_blob_id=latest, | ||
| created_at=raw.get("createdAt"), | ||
| raw=raw, | ||
| ) | ||
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,122 @@ | ||
| from __future__ import annotations | ||
|
|
||
| from dataclasses import dataclass, field | ||
| from typing import Any | ||
|
|
||
| from models.errors import SchemaError | ||
|
|
||
|
|
||
| @dataclass(frozen=True) | ||
| class Composer: | ||
| """Cursor conversation row from globalStorage cursorDiskKV; requires fullConversationHeadersOnly + createdAt.""" | ||
|
|
||
| composer_id: str | ||
| full_conversation_headers_only: list[dict[str, Any]] | ||
| created_at: Any | ||
| name: str | None = None | ||
| last_updated_at: Any = None | ||
| model_config: dict[str, Any] = field(default_factory=dict) | ||
| raw: dict[str, Any] = field(default_factory=dict) | ||
|
|
||
| @classmethod | ||
| def from_dict(cls, raw: dict[str, Any], *, composer_id: str) -> "Composer": | ||
| if not isinstance(raw, dict): | ||
| raise SchemaError( | ||
| "Composer", | ||
| "composerData", | ||
| hint=f"expected object, got {type(raw).__name__}", | ||
| ) | ||
| if not isinstance(composer_id, str) or not composer_id: | ||
| raise SchemaError( | ||
| "Composer", | ||
| "composerId", | ||
| hint=f"expected non-empty str, got {type(composer_id).__name__}", | ||
| ) | ||
| if "fullConversationHeadersOnly" not in raw: | ||
| raise SchemaError("Composer", "fullConversationHeadersOnly") | ||
| if "createdAt" not in raw: | ||
| raise SchemaError("Composer", "createdAt") | ||
|
timon0305 marked this conversation as resolved.
|
||
|
|
||
|
timon0305 marked this conversation as resolved.
|
||
| created_at = raw.get("createdAt") | ||
| if not isinstance(created_at, (int, float)) or isinstance(created_at, bool): | ||
| raise SchemaError( | ||
| "Composer", | ||
| "createdAt", | ||
| hint=f"expected timestamp number, got {type(created_at).__name__}", | ||
| ) | ||
|
timon0305 marked this conversation as resolved.
|
||
|
|
||
| headers = raw.get("fullConversationHeadersOnly") | ||
| if not isinstance(headers, list): | ||
| raise SchemaError( | ||
| "Composer", | ||
| "fullConversationHeadersOnly", | ||
| hint=f"expected list, got {type(headers).__name__}", | ||
| ) | ||
|
|
||
| model_config = raw.get("modelConfig") or {} | ||
| if not isinstance(model_config, dict): | ||
| model_config = {} | ||
|
|
||
| return cls( | ||
| composer_id=composer_id, | ||
| full_conversation_headers_only=headers, | ||
| created_at=created_at, | ||
| name=raw.get("name"), | ||
| last_updated_at=raw.get("lastUpdatedAt"), | ||
| model_config=model_config, | ||
| raw=raw, | ||
| ) | ||
|
|
||
|
|
||
| @dataclass(frozen=True) | ||
| class WorkspaceLocalComposer: | ||
| """Summary composer row from per-workspace state.vscdb ItemTable; only composerId is required.""" | ||
|
|
||
| composer_id: str | ||
| last_updated_at: Any = None | ||
| raw: dict[str, Any] = field(default_factory=dict) | ||
|
|
||
| @classmethod | ||
| def from_dict(cls, raw: dict[str, Any]) -> "WorkspaceLocalComposer": | ||
| if not isinstance(raw, dict): | ||
| raise SchemaError( | ||
| "WorkspaceLocalComposer", | ||
| "composer", | ||
| hint=f"expected object, got {type(raw).__name__}", | ||
| ) | ||
| composer_id = raw.get("composerId") | ||
| if not isinstance(composer_id, str) or not composer_id: | ||
| raise SchemaError( | ||
| "WorkspaceLocalComposer", | ||
| "composerId", | ||
| hint=f"expected non-empty str, got {type(composer_id).__name__}", | ||
| ) | ||
| return cls( | ||
| composer_id=composer_id, | ||
| last_updated_at=raw.get("lastUpdatedAt"), | ||
| raw=raw, | ||
| ) | ||
|
|
||
|
|
||
| @dataclass(frozen=True) | ||
| class Bubble: | ||
| """One message in a composer; bubble_id comes from the row key, not the JSON value.""" | ||
|
|
||
| bubble_id: str | ||
| raw: dict[str, Any] = field(default_factory=dict) | ||
|
|
||
| @classmethod | ||
| def from_dict(cls, raw: dict[str, Any], *, bubble_id: str) -> "Bubble": | ||
| if not isinstance(raw, dict): | ||
| raise SchemaError( | ||
| "Bubble", | ||
| "bubble", | ||
| hint=f"expected object, got {type(raw).__name__}", | ||
| ) | ||
| if not isinstance(bubble_id, str) or not bubble_id: | ||
| raise SchemaError( | ||
| "Bubble", | ||
| "bubbleId", | ||
| hint=f"expected non-empty str, got {type(bubble_id).__name__}", | ||
| ) | ||
| return cls(bubble_id=bubble_id, raw=raw) | ||
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,14 @@ | ||
| from __future__ import annotations | ||
|
|
||
|
|
||
| class SchemaError(ValueError): | ||
| """Raised when a required Cursor schema field is missing or malformed.""" | ||
|
|
||
| def __init__(self, model: str, field: str, *, hint: str | None = None) -> None: | ||
| self.model = model | ||
| self.field = field | ||
| self.hint = hint | ||
| message = f"{model}: missing required field '{field}'" | ||
|
timon0305 marked this conversation as resolved.
Outdated
|
||
| if hint: | ||
| message = f"{message} ({hint})" | ||
| super().__init__(message) | ||
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,43 @@ | ||
| from __future__ import annotations | ||
|
|
||
| from dataclasses import dataclass, field | ||
| from typing import Any | ||
|
|
||
| from models.errors import SchemaError | ||
|
|
||
|
|
||
| @dataclass(frozen=True) | ||
| class ExportEntry: | ||
| """One line of manifest.jsonl; log_id / title / workspace required, timestamps optional.""" | ||
|
|
||
| log_id: str | ||
| title: str | ||
| workspace: str | ||
| created_at: Any = None | ||
| updated_at: Any = None | ||
| raw: dict[str, Any] = field(default_factory=dict) | ||
|
|
||
| @classmethod | ||
| def from_dict(cls, raw: dict[str, Any]) -> "ExportEntry": | ||
| if not isinstance(raw, dict): | ||
| raise SchemaError( | ||
| "ExportEntry", | ||
| "entry", | ||
| hint=f"expected object, got {type(raw).__name__}", | ||
| ) | ||
| for required in ("log_id", "title", "workspace"): | ||
| value = raw.get(required) | ||
| if not isinstance(value, str) or value == "": | ||
| raise SchemaError( | ||
| "ExportEntry", | ||
| required, | ||
| hint=f"expected non-empty str, got {type(value).__name__}", | ||
| ) | ||
| return cls( | ||
| log_id=raw["log_id"], | ||
| title=raw["title"], | ||
| workspace=raw["workspace"], | ||
| created_at=raw.get("created_at"), | ||
| updated_at=raw.get("updated_at"), | ||
| raw=raw, | ||
| ) |
Oops, something went wrong.
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.