MCPcopy Create free account
hub / github.com/atomicdotdev/atomic / read_turn

Function read_turn

atomic-agent/src/transcript/opencode.rs:113–159  ·  view source on GitHub ↗

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)

Source from the content-addressed store, hash-verified

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

Callers 1

enrich_opencode_turnMethod · 0.85

Calls 6

locate_dbFunction · 0.85
same_dirFunction · 0.85
read_messagesFunction · 0.85
read_partsFunction · 0.85
assembleFunction · 0.70
getMethod · 0.65

Tested by

no test coverage detected