-
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 3 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,25 @@ | ||
| """Typed domain models for Cursor schema (closes #24). | ||
|
|
||
| Cursor's on-disk JSON shapes are not versioned, so silent renames of fields | ||
| like ``composerData`` or ``latestRootBlobId`` would otherwise pass through | ||
| ``dict.get(...)`` with a fallback default and produce empty conversations | ||
| with no error raised. The models here add a schema-validation boundary at | ||
| database read sites: ``from_dict`` classmethods raise ``SchemaError`` when | ||
| critical fields are missing, so drift becomes loud instead of silent. | ||
| """ | ||
|
|
||
| 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,49 @@ | ||
| """CliSessionMeta — typed model for the Cursor CLI ``meta`` blob.""" | ||
|
|
||
| from __future__ import annotations | ||
|
|
||
| from dataclasses import dataclass, field | ||
| from typing import Any | ||
|
|
||
| from models.errors import SchemaError | ||
|
|
||
|
|
||
| @dataclass(frozen=True) | ||
| class CliSessionMeta: | ||
| """The ``meta`` blob at the head of a Cursor CLI ``store.db`` blob graph. | ||
|
|
||
| ``latestRootBlobId`` is the entry point for the conversation reconstruction | ||
| BFS in ``utils/cli_chat_reader.traverse_blobs``; without it, the entire | ||
| conversation is unreachable. ``createdAt`` is documented as part of the | ||
| meta-blob schema (see ``utils/cli_chat_reader`` module docstring) and is | ||
| captured here, but it is not gated on — only ``latestRootBlobId`` is the | ||
| hard requirement, since that is the only field whose absence prevents | ||
| conversation reconstruction. | ||
| """ | ||
|
|
||
| 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,136 @@ | ||
| """Composer (conversation) and Bubble (message) typed models.""" | ||
|
|
||
| from __future__ import annotations | ||
|
|
||
| from dataclasses import dataclass, field | ||
| from typing import Any | ||
|
|
||
| from models.errors import SchemaError | ||
|
|
||
|
|
||
| @dataclass(frozen=True) | ||
| class Composer: | ||
| """A Cursor conversation (a.k.a. "composer") row. | ||
|
|
||
| Required fields per the schema-validation contract (issue #24): | ||
| - ``fullConversationHeadersOnly`` — without this, a composer cannot be | ||
| rendered (no message order is recoverable). | ||
| - ``createdAt`` — Cursor writes this on every composer (verified | ||
| 17/17 against a live workspaceStorage). A missing value is the | ||
| kind of drift this layer exists to surface. | ||
|
|
||
| The composer ID is intentionally passed in as a constructor argument | ||
| rather than read from ``raw`` because Cursor stores it in the row key | ||
| (``composerData:<id>``) rather than in the JSON value. | ||
| """ | ||
|
|
||
| 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 composer_id: | ||
| raise SchemaError("Composer", "composerId", hint="empty composer ID") | ||
|
timon0305 marked this conversation as resolved.
Outdated
|
||
| 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.
|
||
| 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=raw.get("createdAt"), | ||
| name=raw.get("name"), | ||
| last_updated_at=raw.get("lastUpdatedAt"), | ||
| model_config=model_config, | ||
| raw=raw, | ||
| ) | ||
|
|
||
|
|
||
| @dataclass(frozen=True) | ||
| class WorkspaceLocalComposer: | ||
| """A composer entry from ``composer.composerData`` ItemTable rows. | ||
|
|
||
| These are summary records that live in each per-workspace ``state.vscdb``. | ||
| They share ``composerId`` and ``lastUpdatedAt`` with the global composer | ||
| schema, but they do **not** carry ``fullConversationHeadersOnly`` or | ||
| ``createdAt`` — those only exist on the global ``cursorDiskKV`` rows that | ||
| ``Composer.from_dict`` validates. Treating both shapes through the same | ||
| model would reject every workspace-local entry, so this slim model | ||
| exists to keep schema-drift detection at the boundary without conflating | ||
| the two storage paths. | ||
| """ | ||
|
|
||
| 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: | ||
| """A single message bubble within a composer. | ||
|
|
||
| The bubble ID lives in the row key (``bubbleId:<composer_id>:<bubble_id>``) | ||
| rather than the JSON value, so it is passed in explicitly. The raw dict | ||
| is preserved to keep downstream rendering code (which still walks the | ||
| untyped shape) working without modification. | ||
| """ | ||
|
|
||
| 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 bubble_id: | ||
| raise SchemaError("Bubble", "bubbleId", hint="empty bubble ID") | ||
| 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,22 @@ | ||
| """Exception types for the typed-model schema-validation layer.""" | ||
|
|
||
| from __future__ import annotations | ||
|
|
||
|
|
||
| class SchemaError(ValueError): | ||
| """Raised when a required Cursor schema field is missing or malformed. | ||
|
|
||
| Inherits from ``ValueError`` so call sites that already catch generic | ||
| deserialisation errors (e.g. ``json.JSONDecodeError`` is a subclass of | ||
| ``ValueError``) also catch schema drift without needing a separate | ||
| ``except`` clause. New code should catch ``SchemaError`` explicitly. | ||
| """ | ||
|
|
||
| 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) | ||
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.