Commit ebf5dbef authored by Jan Reimes's avatar Jan Reimes
Browse files

Fix: _get_tdoc now correctly retrieves TDocs from database

Fixed bug in TDocDatabase._get_tdoc that caused TDocs to not be
found in the local database, leading to unnecessary external API calls
to whatthespec.net.

Issue:
- _get_tdoc used self.connection.model_from_table("tdocs", tdoc_id.upper())
- This method was throwing KeyError, causing all lookups to fail
- As a result, checkout_tdoc_to_workspace fell back to whatthespec.net
  even for TDocs that existed in the local database

Fix:
- Changed _get_tdoc to use direct SQL query instead of model_from_table
- Query retrieves all required fields from actual database schema
- Returns properly constructed TDocMetadata object

Impact:
- TDocs with URLs in database are now correctly found
- whatthespec.net is only contacted for TDocs NOT in database
- Commands like `ai workspace add-members` work efficiently

Tested with:
- S4AQ200164: Now correctly retrieved from database with URL
- Non-existent TDocs: Return None as expected
parent 67486cd8
Loading
Loading
Loading
Loading
+25 −3
Original line number Diff line number Diff line
@@ -287,11 +287,33 @@ class TDocDatabase(MeetingDatabase):

    def _get_tdoc(self, tdoc_id: str) -> TDocMetadata | None:
        """Get a TDoc by ID."""
        import sqlite3

        try:
            return self.connection.model_from_table("tdocs", tdoc_id.upper())
        except KeyError:
            # Use direct SQL query to retrieve TDoc by ID
            # Query all required fields from actual database schema
            with sqlite3.connect(self.connection.filename) as conn:
                cursor = conn.cursor()
                cursor.execute(
                    "SELECT tdoc_id, meeting_id, title, url, source, contact, agenda_item_nbr "
                    "FROM tdocs WHERE tdoc_id = ?",
                    (tdoc_id.upper(),)
                )
                row = cursor.fetchone()
                if row:
                    from tdoc_crawler.tdocs.models import TDocMetadata
                    return TDocMetadata(
                        tdoc_id=row[0],
                        meeting_id=row[1],
                        title=row[2],
                        url=row[3],
                        source=row[4],
                        contact=row[5],
                        agenda_item_nbr=row[6],
                    )
                return None
        except sqlite3.Error:
            return None

    # ------------------------------------------------------------------
    # Normalisation helpers
    # ------------------------------------------------------------------