-
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 1 commit
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,24 @@ | ||
| """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 | ||
| from models.errors import SchemaError | ||
| from models.export import ExportEntry | ||
| from models.workspace import Workspace | ||
|
|
||
| __all__ = [ | ||
| "Bubble", | ||
| "CliSessionMeta", | ||
| "Composer", | ||
| "ExportEntry", | ||
| "SchemaError", | ||
| "Workspace", | ||
| ] |
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 @@ | ||
| """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": | ||
| 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,83 @@ | ||
| """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: | ||
| - ``fullConversationHeadersOnly`` — without this, a composer cannot be | ||
| rendered (no message order is recoverable). This is the only hard | ||
| requirement: real Cursor data legitimately omits ``createdAt`` for | ||
| older composers (the existing call sites already fall back to | ||
| ``lastUpdatedAt`` and then to epoch zero), so it is captured but | ||
| not gated on. | ||
|
|
||
| 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 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") | ||
|
|
||
|
timon0305 marked this conversation as resolved.
|
||
| headers = raw.get("fullConversationHeadersOnly") or [] | ||
| if not isinstance(headers, list): | ||
| raise SchemaError( | ||
| "Composer", | ||
| "fullConversationHeadersOnly", | ||
| hint=f"expected list, got {type(headers).__name__}", | ||
| ) | ||
|
timon0305 marked this conversation as resolved.
Outdated
|
||
|
|
||
| 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 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 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) | ||
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,40 @@ | ||
| """ExportEntry — typed model for an export manifest record (JSONL line).""" | ||
|
|
||
| from __future__ import annotations | ||
|
|
||
| from dataclasses import dataclass, field | ||
| from typing import Any | ||
|
|
||
| from models.errors import SchemaError | ||
|
|
||
|
|
||
| @dataclass(frozen=True) | ||
| class ExportEntry: | ||
| """A single record in the export manifest (one line in ``manifest.jsonl``). | ||
|
|
||
| Required fields are the YAML-frontmatter keys that downstream tooling | ||
| indexes against: a missing ``log_id`` makes the entry unaddressable, and | ||
| a missing ``title`` produces unreadable output. Timestamps are optional — | ||
| not every Cursor conversation has both a creation and update time. | ||
| """ | ||
|
|
||
| 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": | ||
| for required in ("log_id", "title", "workspace"): | ||
| if required not in raw or raw[required] in (None, ""): | ||
| raise SchemaError("ExportEntry", required) | ||
| return cls( | ||
| log_id=str(raw["log_id"]), | ||
| title=str(raw["title"]), | ||
| workspace=str(raw["workspace"]), | ||
|
timon0305 marked this conversation as resolved.
Outdated
|
||
| created_at=raw.get("created_at"), | ||
| updated_at=raw.get("updated_at"), | ||
| 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,37 @@ | ||
| """Workspace — typed model for a single Cursor workspace folder.""" | ||
|
|
||
| from __future__ import annotations | ||
|
|
||
| from dataclasses import dataclass, field | ||
| from typing import Any | ||
|
|
||
| from models.errors import SchemaError | ||
|
|
||
|
|
||
| @dataclass(frozen=True) | ||
| class Workspace: | ||
| """A Cursor workspace entry. | ||
|
|
||
| The workspace ID is the directory name on disk (Cursor uses random | ||
| short hashes as workspace IDs) and is passed in explicitly. ``folder`` | ||
| is the absolute path of the project the workspace targets, read from | ||
| ``workspace.json``; it may legitimately be ``None`` for a CLI-only | ||
| workspace, so missing-folder is not a schema error. | ||
| """ | ||
|
|
||
| workspace_id: str | ||
| folder: str | None = None | ||
| raw: dict[str, Any] = field(default_factory=dict) | ||
|
|
||
| @classmethod | ||
| def from_dict(cls, raw: dict[str, Any], *, workspace_id: str) -> "Workspace": | ||
| if not workspace_id: | ||
| raise SchemaError("Workspace", "workspaceId", hint="empty workspace ID") | ||
|
timon0305 marked this conversation as resolved.
Outdated
|
||
| folder = raw.get("folder") | ||
| if folder is not None and not isinstance(folder, str): | ||
| raise SchemaError( | ||
| "Workspace", | ||
| "folder", | ||
| hint=f"expected str or None, got {type(folder).__name__}", | ||
| ) | ||
| return cls(workspace_id=workspace_id, folder=folder, 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.