{"record":{"id":"ac9c5819f13d4b08","repo":"rtk-ai/rtk","slug":"failed-to-query-first-seen-timestamp-e","errorCode":null,"errorMessage":"Failed to query first seen timestamp: {e}","messagePattern":"Failed to query first seen timestamp: (.+?)","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"error","filePath":"src/core/tracking.rs","lineNumber":1402,"sourceCode":"        let pattern = format!(\"rtk {}\", name);\n        let count: i64 = self.conn.query_row(\n            \"SELECT COUNT(*) FROM commands WHERE rtk_cmd LIKE ?1 || '%'\",\n            params![pattern],\n            |row| row.get(0),\n        )?;\n        Ok(count)\n    }\n\n    /// Days since first recorded command (installation age).\n    pub fn first_seen_days(&self) -> Result<i64> {\n        let oldest: Option<String> =\n            match self\n                .conn\n                .query_row(\"SELECT MIN(timestamp) FROM commands\", [], |row| row.get(0))\n            {\n                Ok(v) => v,\n                Err(rusqlite::Error::QueryReturnedNoRows) => None,\n                Err(e) => return Err(anyhow::anyhow!(\"Failed to query first seen timestamp: {e}\")),\n            };\n        match oldest {\n            Some(ts) => {\n                let first = chrono::NaiveDateTime::parse_from_str(&ts, \"%Y-%m-%dT%H:%M:%S\")\n                    .or_else(|_| chrono::NaiveDateTime::parse_from_str(&ts, \"%Y-%m-%d %H:%M:%S\"))\n                    .map(|dt| dt.and_utc())\n                    .unwrap_or_else(|_| chrono::Utc::now());\n                let days = (chrono::Utc::now() - first).num_days();\n                Ok(days.max(0))\n            }\n            None => Ok(0),\n        }\n    }\n\n    /// Number of distinct active days in the last 30 days.\n    pub fn active_days_30d(&self) -> Result<i64> {\n        let since = (chrono::Utc::now() - chrono::Duration::days(30))\n            .format(\"%Y-%m-%dT%H:%M:%S\")","sourceCodeStart":1384,"sourceCodeEnd":1420,"githubUrl":"https://github.com/rtk-ai/rtk/blob/0924356b4caba4989607227b7c8824d3d8098719/src/core/tracking.rs#L1384-L1420","documentation":"When computing usage statistics, tracking queries `SELECT MIN(timestamp) FROM commands` to find the first recorded command. Any rusqlite error other than an empty result set is wrapped in this anyhow error, meaning the DB could not be queried as expected.","triggerScenarios":"Calling the gain/stats routine when the commands table is corrupted, locked by another process, the timestamp column has unexpected type, or the database file is unreadable — anything making query_row return a non-NoRows error.","commonSituations":"Concurrent rtk processes holding the SQLite lock; a corrupted ~/.local/share/rtk recall.db; a schema migrated by an incompatible rtk version; disk I/O errors.","solutions":["Close other rtk processes and retry (SQLite lock contention is transient).","Back up and delete the recall database so rtk recreates it with a fresh schema.","Check the file permissions of the database file and its containing directory.","Run `sqlite3 <db> 'PRAGMA integrity_check;'` to confirm corruption before deleting."],"exampleFix":"// before\nrm ~/.local/share/rtk/recall.db   # last resort only\n// after\nfuser ~/.local/share/rtk/recall.db  # find competing process, kill it, then retry","handlingStrategy":"retry","validationCode":"import sqlite3\ndb = sqlite3.connect('~/.local/share/rtk/recall.db')\ndb.execute('SELECT MIN(timestamp) FROM commands')  # fails early if corrupted/locked\ndb.execute('PRAGMA integrity_check')","typeGuard":null,"tryCatchPattern":"match tracking.first_seen() {\n    Err(e) if e.to_string().contains(\"Failed to query first seen\") => {\n        // wait and retry once (lock contention), else recreate the db\n    }\n    other => other,\n}","preventionTips":["Avoid running multiple rtk instances concurrently against the same store","Run PRAGMA integrity_check after crashes or disk-full events","Back up recall.db before upgrading rtk versions"],"tags":["sqlite","database","tracking"],"backgroundTag":"database-query-failed","analyzedSha":"0924356b4caba4989607227b7c8824d3d8098719","analyzedAt":"2026-09-19T19:25:29.612Z","contentChangedAt":"2026-09-19T19:25:29.612Z","schemaVersion":2},"datasetVersion":"2026-09-23T08:17:48.524Z"}