Read the current turn's data for `session_id` from OpenCode's store. Returns `None` when the database is missing or unreadable, the session is unknown, or the session belongs to a different directory than `expected_dir` (a guard against reading an unrelated project's session with a colliding ID).
(session_id: &str, expected_dir: &Path)
| 110 | /// `expected_dir` (a guard against reading an unrelated project's session |
| 111 | /// with a colliding ID). |
| 112 | pub(crate) fn read_turn(session_id: &str, expected_dir: &Path) -> Option<TurnData> { |
| 113 | let db_path = locate_db()?; |
| 114 | |
| 115 | let conn = match Connection::open_with_flags( |
| 116 | &db_path, |
| 117 | OpenFlags::SQLITE_OPEN_READ_ONLY | OpenFlags::SQLITE_OPEN_NO_MUTEX, |
| 118 | ) { |
| 119 | Ok(c) => c, |
| 120 | Err(e) => { |
| 121 | log::debug!("opencode store: cannot open {}: {}", db_path.display(), e); |
| 122 | return None; |
| 123 | } |
| 124 | }; |
| 125 | let _ = conn.busy_timeout(std::time::Duration::from_millis(DB_BUSY_TIMEOUT_MS)); |
| 126 | |
| 127 | // The session row carries the project directory; refuse to read a |
| 128 | // session that belongs to a different checkout. |
| 129 | let directory: String = match conn.query_row( |
| 130 | "SELECT directory FROM session WHERE id = ?1", |
| 131 | [session_id], |
| 132 | |row| row.get(0), |
| 133 | ) { |
| 134 | Ok(d) => d, |
| 135 | Err(e) => { |
| 136 | log::debug!( |
| 137 | "opencode store: session {} not found in {}: {}", |
| 138 | session_id, |
| 139 | db_path.display(), |
| 140 | e |
| 141 | ); |
| 142 | return None; |
| 143 | } |
| 144 | }; |
| 145 | if !same_dir(Path::new(&directory), expected_dir) { |
| 146 | log::warn!( |
| 147 | "opencode store: session {} belongs to '{}', not '{}' — skipping", |
| 148 | session_id, |
| 149 | directory, |
| 150 | expected_dir.display() |
| 151 | ); |
| 152 | return None; |
| 153 | } |
| 154 | |
| 155 | let messages = read_messages(&conn, session_id)?; |
| 156 | let parts = read_parts(&conn, session_id)?; |
| 157 | Some(assemble(&messages, &parts)) |
| 158 | } |
| 159 | |
| 160 | /// Compare two directories, canonicalizing when possible. |
| 161 | fn same_dir(a: &Path, b: &Path) -> bool { |
no test coverage detected