agent-sessions 0.3.0

Read native coding-agent sessions as typed Rust events with source provenance
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
use super::*;
use rusqlite::{Connection, OpenFlags};

#[derive(Debug, Clone, PartialEq, Eq, Serialize)]
pub struct DatabaseSession {
    pub id: String,
    pub title: Option<String>,
    pub cwd: Option<String>,
}
fn open(agent: Agent, path: &Path, opts: &ReadOptions) -> Result<Connection, ImportError> {
    if !matches!(
        agent,
        Agent::OpenCode
            | Agent::Cursor
            | Agent::Goose
            | Agent::Hermes
            | Agent::ZCode
            | Agent::CursorCli
            | Agent::Zed
            | Agent::Warp
    ) {
        return Err(ReadError::InvalidOptions("no database adapter for this agent").into());
    }
    if opts.stop_at_byte.is_some() {
        return Err(ReadError::InvalidOptions(
            "database snapshots do not have byte-prefix boundaries",
        )
        .into());
    }
    let size = std::fs::metadata(path).map_err(ReadError::Io)?.len();
    if let Some(limit) = opts.max_file_bytes
        && size > limit
    {
        return Err(ReadError::TooLarge { limit }.into());
    }
    Connection::open_with_flags(
        path,
        OpenFlags::SQLITE_OPEN_READ_ONLY | OpenFlags::SQLITE_OPEN_NO_MUTEX,
    )
    .map_err(|e| ReadError::Database(e).into())
}
/// Enumerate native IDs; never creates or migrates the database.
pub fn list_database_sessions(
    agent: Agent,
    path: &Path,
    opts: &ReadOptions,
) -> Result<Vec<DatabaseSession>, ImportError> {
    let conn = open(agent, path, opts)?;
    let mut result = Vec::new();
    if matches!(agent, Agent::OpenCode | Agent::ZCode) {
        let mut statement = conn
            .prepare("SELECT id,title,directory FROM session ORDER BY time_created,id")
            .map_err(ReadError::Database)?;
        let rows = statement
            .query_map([], |r| {
                Ok(DatabaseSession {
                    id: r.get(0)?,
                    title: r.get(1)?,
                    cwd: r.get(2)?,
                })
            })
            .map_err(ReadError::Database)?;
        for row in rows {
            result.push(row.map_err(ReadError::Database)?);
        }
    } else if agent == Agent::Goose {
        let mut statement = conn
            .prepare("SELECT id,name,working_dir FROM sessions ORDER BY created_at,id")
            .map_err(ReadError::Database)?;
        let rows = statement
            .query_map([], |r| {
                Ok(DatabaseSession {
                    id: r.get(0)?,
                    title: r.get(1)?,
                    cwd: r.get(2)?,
                })
            })
            .map_err(ReadError::Database)?;
        for row in rows {
            result.push(row.map_err(ReadError::Database)?);
        }
    } else if agent == Agent::Hermes {
        let mut statement = conn
            .prepare("SELECT id,title,cwd FROM sessions ORDER BY started_at,id")
            .map_err(ReadError::Database)?;
        for row in statement
            .query_map([], |r| {
                Ok(DatabaseSession {
                    id: r.get(0)?,
                    title: r.get(1)?,
                    cwd: r.get(2)?,
                })
            })
            .map_err(ReadError::Database)?
        {
            result.push(row.map_err(ReadError::Database)?);
        }
    } else if agent == Agent::Warp {
        let mut statement=conn.prepare("SELECT conversation_id,summary FROM agent_conversations ORDER BY last_modified_at,id").map_err(ReadError::Database)?;
        for row in statement
            .query_map([], |r| {
                Ok(DatabaseSession {
                    id: r.get(0)?,
                    title: r.get(1)?,
                    cwd: None,
                })
            })
            .map_err(ReadError::Database)?
        {
            result.push(row.map_err(ReadError::Database)?);
        }
    } else if agent == Agent::CursorCli {
        let meta = super::cursor_cli::metadata(&conn, opts)?;
        result.push(DatabaseSession {
            id: crate::parser::required(&meta, "agentId")?.into(),
            title: crate::parser::string(&meta, "name"),
            cwd: None,
        });
    } else if agent == Agent::Zed {
        let mut statement = conn
            .prepare("SELECT id,summary FROM threads ORDER BY updated_at,id")
            .map_err(ReadError::Database)?;
        for row in statement
            .query_map([], |r| {
                Ok(DatabaseSession {
                    id: r.get(0)?,
                    title: r.get(1)?,
                    cwd: None,
                })
            })
            .map_err(ReadError::Database)?
        {
            result.push(row.map_err(ReadError::Database)?);
        }
    } else {
        let mut statement = conn
            .prepare("SELECT key FROM cursorDiskKV WHERE key LIKE 'composerData:%' ORDER BY key")
            .map_err(ReadError::Database)?;
        let rows = statement
            .query_map([], |r| r.get::<_, String>(0))
            .map_err(ReadError::Database)?;
        for row in rows {
            let key = row.map_err(ReadError::Database)?;
            result.push(DatabaseSession {
                id: key["composerData:".len()..].into(),
                title: None,
                cwd: None,
            });
        }
    }
    Ok(result)
}
fn json(conn: &Connection, sql: &str, key: &str, opts: &ReadOptions) -> Result<Value, ImportError> {
    // Check serialized row size in SQL before copying the value into Rust.
    let mut stmt = conn.prepare(sql).map_err(ReadError::Database)?;
    let mut rows = stmt.query([key]).map_err(ReadError::Database)?;
    let row = rows
        .next()
        .map_err(ReadError::Database)?
        .ok_or(LineErrorKind::MissingField("database record"))?;
    let size = row.get::<_, u64>(0).map_err(ReadError::Database)?;
    if opts.max_line_bytes.is_some_and(|limit| size > limit as u64) {
        return Err(LineErrorKind::TooLong.into());
    }
    let bytes = row.get::<_, Vec<u8>>(1).map_err(ReadError::Database)?;
    serde_json::from_slice(&bytes).map_err(|_| LineErrorKind::InvalidJson.into())
}
fn source(table: &str, key: &str) -> ImportSource {
    ImportSource::DatabaseRow {
        table: table.into(),
        key: key.into(),
    }
}
/// Read one session within a SQLite read transaction. No byte offsets are invented.
/// opts.max_line_bytes bounds each serialized row; max_file_bytes bounds the DB file.
pub fn import_database(
    agent: Agent,
    path: &Path,
    id: &str,
    opts: &ReadOptions,
) -> Result<SessionImport, ImportError> {
    let mut conn = open(agent, path, opts)?;
    let tx = conn.transaction().map_err(ReadError::Database)?;
    let mut result = SessionImport::default();
    let mut state = State {
        session_id: Some(id.into()),
        ..Default::default()
    };
    if agent == Agent::Warp {
        super::warp::import(&tx, id, opts, &mut result)?;
    } else if agent == Agent::CursorCli {
        super::cursor_cli::import(&tx, id, opts, &mut result)?;
    } else if agent == Agent::Zed {
        let (data_type, parent, length): (String, Option<String>, u64) = tx
            .query_row(
                "SELECT data_type,parent_id,length(data) FROM threads WHERE id=?1",
                [id],
                |r| Ok((r.get(0)?, r.get(1)?, r.get(2)?)),
            )
            .map_err(ReadError::Database)?;
        if opts.max_line_bytes.is_some_and(|n| length > n as u64) {
            return Err(LineErrorKind::TooLong.into());
        }
        let data: Vec<u8> = tx
            .query_row("SELECT data FROM threads WHERE id=?1", [id], |r| r.get(0))
            .map_err(ReadError::Database)?;
        let bytes = match data_type.as_str() {
            "json" => data,
            "zstd" => {
                let decoder =
                    zstd::stream::read::Decoder::new(data.as_slice()).map_err(ReadError::Io)?;
                let limit = opts.max_line_bytes.map(|n| n as u64).unwrap_or(u64::MAX);
                let mut decoded = Vec::new();
                std::io::Read::read_to_end(
                    &mut std::io::Read::take(decoder, limit.saturating_add(1)),
                    &mut decoded,
                )
                .map_err(ReadError::Io)?;
                if decoded.len() as u64 > limit {
                    return Err(LineErrorKind::TooLong.into());
                }
                decoded
            }
            _ => return Err(LineErrorKind::InvalidField("threads.data_type".into()).into()),
        };
        let value: Value =
            serde_json::from_slice(&bytes).map_err(|_| LineErrorKind::InvalidJson)?;
        super::document(agent, &value, opts, &mut result)?;
        for event in &mut result.events {
            event.sources = vec![source("threads", id)];
            event.session_id = Some(id.into());
            if let Event::Message(m) = &mut event.value {
                m.is_sidechain |= parent.is_some();
            }
        }
    } else if matches!(agent, Agent::OpenCode | Agent::ZCode) {
        let (directory, parent): (String, Option<String>) = tx
            .query_row(
                "SELECT directory,parent_id FROM session WHERE id=?1",
                [id],
                |r| Ok((r.get(0)?, r.get(1)?)),
            )
            .map_err(ReadError::Database)?;
        state.sidechain = parent.is_some();
        emit_meta(
            MetaUpdate {
                session_id: Some(id.into()),
                cwd: Some(directory),
                ..Default::default()
            },
            vec![source("session", id)],
            opts,
            &mut result,
        );
        let ids = {
            let mut stmt = tx
                .prepare(if agent == Agent::ZCode {
                    "SELECT id FROM message WHERE session_id=?1 ORDER BY sequence,id"
                } else {
                    "SELECT id FROM message WHERE session_id=?1 ORDER BY time_created,id"
                })
                .map_err(ReadError::Database)?;
            stmt.query_map([id], |r| r.get::<_, String>(0))
                .map_err(ReadError::Database)?
                .collect::<Result<Vec<_>, _>>()
                .map_err(ReadError::Database)?
        };
        for message_id in ids {
            let mut info = json(
                &tx,
                "SELECT length(CAST(data AS BLOB)),CAST(data AS BLOB) FROM message WHERE id=?1",
                &message_id,
                opts,
            )?;
            if !info.is_object() {
                return Err(LineErrorKind::InvalidField("message.data".into()).into());
            }
            info["id"] = Value::String(message_id.clone());
            info["sessionID"] = Value::String(id.into());
            let ids = {
                let mut stmt=tx.prepare(if agent == Agent::ZCode { "SELECT id FROM part WHERE message_id=?1 AND session_id=?2 ORDER BY sequence,id" } else { "SELECT id FROM part WHERE message_id=?1 AND session_id=?2 ORDER BY time_created,id" }).map_err(ReadError::Database)?;
                stmt.query_map([&message_id, id], |r| r.get::<_, String>(0))
                    .map_err(ReadError::Database)?
                    .collect::<Result<Vec<_>, _>>()
                    .map_err(ReadError::Database)?
            };
            let mut parts = Vec::new();
            let mut sources = vec![source("message", &message_id)];
            for part_id in ids {
                parts.push(json(
                    &tx,
                    "SELECT length(CAST(data AS BLOB)),CAST(data AS BLOB) FROM part WHERE id=?1",
                    &part_id,
                    opts,
                )?);
                sources.push(source("part", &part_id));
            }
            let record = serde_json::json!({"info":info,"parts":parts});
            parse_record(agent, &record, &mut state, sources, opts, &mut result)?;
            result.summary.lines += 1;
        }
    } else if agent == Agent::Goose {
        let directory: String = tx
            .query_row("SELECT working_dir FROM sessions WHERE id=?1", [id], |r| {
                r.get(0)
            })
            .map_err(ReadError::Database)?;
        emit_meta(
            MetaUpdate {
                session_id: Some(id.into()),
                cwd: Some(directory),
                ..Default::default()
            },
            vec![source("sessions", id)],
            opts,
            &mut result,
        );
        let mut stmt=tx.prepare("SELECT id,message_id,role,created_timestamp,length(CAST(content_json AS BLOB)),CAST(content_json AS BLOB),length(CAST(metadata_json AS BLOB)),CAST(metadata_json AS BLOB) FROM messages WHERE session_id=?1 ORDER BY created_timestamp,id").map_err(ReadError::Database)?;
        let mut rows = stmt.query([id]).map_err(ReadError::Database)?;
        while let Some(row) = rows.next().map_err(ReadError::Database)? {
            let content_size: u64 = row.get(4).map_err(ReadError::Database)?;
            let metadata_size: Option<u64> = row.get(6).map_err(ReadError::Database)?;
            if opts.max_line_bytes.is_some_and(|limit| {
                content_size.saturating_add(metadata_size.unwrap_or(0)) > limit as u64
            }) {
                return Err(LineErrorKind::TooLong.into());
            }
            let content: Vec<u8> = row.get(5).map_err(ReadError::Database)?;
            let metadata: Option<Vec<u8>> = row.get(7).map_err(ReadError::Database)?;
            let content: Value =
                serde_json::from_slice(&content).map_err(|_| LineErrorKind::InvalidJson)?;
            let metadata: Value = metadata
                .map(|v| serde_json::from_slice(&v))
                .transpose()
                .map_err(|_| LineErrorKind::InvalidJson)?
                .unwrap_or(Value::Null);
            let row_id: i64 = row.get(0).map_err(ReadError::Database)?;
            let message_id: Option<String> = row.get(1).map_err(ReadError::Database)?;
            let role: String = row.get(2).map_err(ReadError::Database)?;
            let created: i64 = row.get(3).map_err(ReadError::Database)?;
            let record = serde_json::json!({"id":message_id,"role":role,"created":created,"content":content,"metadata":metadata});
            parse_record(
                agent,
                &record,
                &mut state,
                vec![source("messages", &row_id.to_string())],
                opts,
                &mut result,
            )?;
            result.summary.lines += 1;
        }
    } else if agent == Agent::Hermes {
        let (model, cwd, parent): (Option<String>, Option<String>, Option<String>) = tx
            .query_row(
                "SELECT model,cwd,parent_session_id FROM sessions WHERE id=?1",
                [id],
                |r| Ok((r.get(0)?, r.get(1)?, r.get(2)?)),
            )
            .map_err(ReadError::Database)?;
        state.sidechain = parent.is_some();
        emit_meta(
            MetaUpdate {
                session_id: Some(id.into()),
                model,
                cwd,
                ..Default::default()
            },
            vec![source("sessions", id)],
            opts,
            &mut result,
        );
        let mut stmt = tx.prepare("SELECT id,role,length(CAST(content AS BLOB)),content,tool_call_id,length(CAST(tool_calls AS BLOB)),tool_calls,timestamp FROM messages WHERE session_id=?1 ORDER BY timestamp,id").map_err(ReadError::Database)?;
        let mut rows = stmt.query([id]).map_err(ReadError::Database)?;
        while let Some(row) = rows.next().map_err(ReadError::Database)? {
            let content_size: Option<u64> = row.get(2).map_err(ReadError::Database)?;
            let calls_size: Option<u64> = row.get(5).map_err(ReadError::Database)?;
            if opts.max_line_bytes.is_some_and(|limit| {
                content_size
                    .unwrap_or(0)
                    .saturating_add(calls_size.unwrap_or(0))
                    > limit as u64
            }) {
                return Err(LineErrorKind::TooLong.into());
            }
            let row_id: i64 = row.get(0).map_err(ReadError::Database)?;
            let role: String = row.get(1).map_err(ReadError::Database)?;
            let content: Option<String> = row.get(3).map_err(ReadError::Database)?;
            // Hermes explicitly tags structured content. Plain JSON-looking text stays text.
            let content = match content {
                Some(s) if s.starts_with("\0json:") => {
                    serde_json::from_str(&s[6..]).map_err(|_| LineErrorKind::InvalidJson)?
                }
                Some(s) => Value::String(s),
                None => Value::Null,
            };
            let call_id: Option<String> = row.get(4).map_err(ReadError::Database)?;
            let calls: Option<String> = row.get(6).map_err(ReadError::Database)?;
            let calls: Value = calls
                .map(|s| serde_json::from_str(&s))
                .transpose()
                .map_err(|_| LineErrorKind::InvalidJson)?
                .unwrap_or(Value::Null);
            let timestamp: Option<f64> = row.get(7).map_err(ReadError::Database)?;
            let mut record = serde_json::json!({"role":role,"content":content,"tool_call_id":call_id,"timestamp":timestamp});
            if !calls.is_null() {
                record["tool_calls"] = calls;
            }
            parse_record(
                agent,
                &record,
                &mut state,
                vec![source("messages", &row_id.to_string())],
                opts,
                &mut result,
            )?;
            result.summary.lines += 1;
        }
        tally(
            &mut result.summary.ignored_types,
            "hermes:session-totals-not-message-usage",
        );
    } else {
        let key = format!("composerData:{id}");
        let composer = json(
            &tx,
            "SELECT length(CAST(value AS BLOB)),CAST(value AS BLOB) FROM cursorDiskKV WHERE key=?1",
            &key,
            opts,
        )?;
        emit_meta(
            MetaUpdate {
                session_id: Some(id.into()),
                model: composer
                    .pointer("/modelConfig/modelName")
                    .and_then(Value::as_str)
                    .map(str::to_owned),
                ..Default::default()
            },
            vec![source("cursorDiskKV", &key)],
            opts,
            &mut result,
        );
        let headers = composer
            .get("fullConversationHeadersOnly")
            .and_then(Value::as_array)
            .ok_or(LineErrorKind::MissingField("fullConversationHeadersOnly"))?;
        for header in headers {
            let bubble = crate::parser::required(header, "bubbleId")?;
            let key = format!("bubbleId:{id}:{bubble}");
            let record = json(
                &tx,
                "SELECT length(CAST(value AS BLOB)),CAST(value AS BLOB) FROM cursorDiskKV WHERE key=?1",
                &key,
                opts,
            )?;
            parse_record(
                agent,
                &record,
                &mut state,
                vec![source("cursorDiskKV", &key)],
                opts,
                &mut result,
            )?;
            result.summary.lines += 1;
        }
    }
    // Dropping a read transaction rolls back only its read snapshot; no DB writes.
    result.summary.status = ReadStatus::Complete;
    Ok(result)
}