tinyharness-lib 0.5.6

Core library for TinyHarness: LLM providers, tools, sessions and context management
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
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
use std::{
    fs,
    io::{self, BufRead, Write},
    path::{Path, PathBuf},
    time::{SystemTime, UNIX_EPOCH},
};

use uuid::Uuid;

use serde::{Deserialize, Serialize};

use crate::{mode::AgentMode, provider::Message, provider::TokenUsage};

// ── Data types ──────────────────────────────────────────────────────────────

/// Metadata about a session, stored as the first line of the JSONL file.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct SessionMeta {
    /// Unique session identifier (UUID v4).
    pub id: String,
    /// Working directory when the session was created.
    pub working_dir: String,
    /// Unix timestamp (seconds) when the session was created.
    pub created_at: u64,
    /// Unix timestamp (seconds) when the session was last updated.
    pub updated_at: u64,
    /// The agent mode used in this session.
    pub mode: AgentMode,
    /// The provider kind (e.g. "ollama", "llama.cpp", "vllm").
    pub provider: String,
    /// The model name.
    pub model: Option<String>,
    /// Optional user-defined name for the session.
    pub name: Option<String>,
    /// Number of messages in the session.
    pub message_count: usize,
    /// Last known token usage reported by the LLM provider.
    /// This is the prompt token count at the time of the last
    /// provider response, serialized so it survives restarts.
    #[serde(default)]
    pub token_usage: Option<TokenUsage>,
    /// Cumulative tool calls made during this session (across all turns).
    #[serde(default)]
    pub total_tool_calls: u64,
    /// Cumulative total tokens consumed across all LLM calls in this session.
    #[serde(default)]
    pub total_tokens_used: u64,
}

/// A single line in the session JSONL file.
#[derive(Debug, Clone, Serialize, Deserialize)]
#[serde(tag = "type")]
pub enum SessionEntry {
    /// First entry: session metadata.
    #[serde(rename = "meta")]
    Meta(SessionMeta),
    /// A conversation message.
    #[serde(rename = "message")]
    Message(Message),
}

// ── Error type ──────────────────────────────────────────────────────────────

/// Error type for session I/O operations.
#[derive(Debug)]
pub enum SessionError {
    Io(std::io::Error),
    Parse(serde_json::Error),
    NotFound(String),
}

impl std::fmt::Display for SessionError {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        match self {
            SessionError::Io(e) => write!(f, "I/O error: {}", e),
            SessionError::Parse(e) => write!(f, "parse error: {}", e),
            SessionError::NotFound(s) => write!(f, "{}", s),
        }
    }
}

impl std::error::Error for SessionError {
    fn source(&self) -> Option<&(dyn std::error::Error + 'static)> {
        match self {
            SessionError::Io(e) => Some(e),
            SessionError::Parse(e) => Some(e),
            SessionError::NotFound(_) => None,
        }
    }
}

impl From<std::io::Error> for SessionError {
    fn from(e: std::io::Error) -> Self {
        SessionError::Io(e)
    }
}

impl From<serde_json::Error> for SessionError {
    fn from(e: serde_json::Error) -> Self {
        SessionError::Parse(e)
    }
}

// ── Session store ───────────────────────────────────────────────────────────

/// Handles persistence of sessions as JSONL files in a directory.
///
/// By default, uses `~/.local/share/tinyharness/sessions/`. This can be
/// overridden with [`SessionStore::new`] for testing or alternative paths.
pub struct SessionStore {
    dir: PathBuf,
}

impl SessionStore {
    /// Create a store that uses the default session directory
    /// (`~/.local/share/tinyharness/sessions/`).
    ///
    /// Prefer [`SessionStore::from_paths`] when the caller has a
    /// [`Paths`](crate::paths::Paths) instance — this form resolves from the
    /// ambient process environment.
    pub fn default_path() -> Self {
        Self::from_paths(&crate::paths::Paths::default())
    }

    /// Create a store that uses `paths.sessions_dir()`.
    pub fn from_paths(paths: &crate::paths::Paths) -> Self {
        SessionStore {
            dir: paths.sessions_dir(),
        }
    }

    /// Create a store that uses a custom directory.
    pub fn new(dir: PathBuf) -> Self {
        SessionStore { dir }
    }

    /// Ensure the session directory exists.
    pub fn ensure_dir(&self) -> Result<(), SessionError> {
        fs::create_dir_all(&self.dir)?;
        Ok(())
    }

    /// Create a brand-new session and write its initial metadata to disk.
    /// Returns the new `Session` handle.
    pub fn create(
        &self,
        working_dir: &str,
        mode: AgentMode,
        provider: &str,
        model: Option<String>,
    ) -> Session {
        let id = Uuid::new_v4().to_string();
        let now = now_timestamp();
        let auto_name = std::path::Path::new(working_dir)
            .file_name()
            .and_then(|n| n.to_str())
            .unwrap_or("session")
            .to_string();

        let meta = SessionMeta {
            id,
            working_dir: working_dir.to_string(),
            created_at: now,
            updated_at: now,
            mode,
            provider: provider.to_string(),
            model,
            name: Some(auto_name),
            message_count: 0,
            token_usage: None,
            total_tool_calls: 0,
            total_tokens_used: 0,
        };

        // Eagerly create the session file so callers get immediate feedback on errors
        let mut session = Session {
            meta: meta.clone(),
            path: self.dir.join(format!("{}.jsonl", meta.id)),
            dirty: true,
            messages_since_save: 0,
            created: false,
        };
        session.ensure_created();
        session
    }

    /// Load an existing session by its full ID.
    /// Returns the session handle and the conversation messages.
    pub fn load(&self, session_id: &str) -> Result<(Session, Vec<Message>), SessionError> {
        let path = self.dir.join(format!("{}.jsonl", session_id));

        if !path.exists() {
            return Err(SessionError::NotFound(format!(
                "Session '{}' not found",
                session_id
            )));
        }

        let file = fs::File::open(&path)?;
        let reader = io::BufReader::new(file);

        let mut meta: Option<SessionMeta> = None;
        let mut messages: Vec<Message> = Vec::new();

        for line in reader.lines() {
            let line = line?;
            let trimmed = line.trim();
            if trimmed.is_empty() {
                continue;
            }
            match serde_json::from_str::<SessionEntry>(trimmed) {
                Ok(SessionEntry::Meta(m)) => {
                    meta = Some(m);
                }
                Ok(SessionEntry::Message(msg)) => {
                    messages.push(msg);
                }
                Err(_) => {
                    // Skip malformed lines rather than failing
                }
            }
        }

        let meta = meta.ok_or_else(|| {
            SessionError::Parse(serde_json::from_str::<SessionEntry>("").unwrap_err())
        })?;

        let session = Session {
            meta: meta.clone(),
            path,
            dirty: false,
            messages_since_save: 0,
            created: true,
        };

        Ok((session, messages))
    }

    /// Find the most recent session for a given working directory.
    pub fn find_latest_for_dir(&self, working_dir: &str) -> Option<String> {
        if !self.dir.exists() {
            return None;
        }

        let entries = fs::read_dir(&self.dir).ok()?;
        let mut best: Option<(u64, String)> = None;

        for entry in entries.flatten() {
            let path = entry.path();
            if path.extension().and_then(|e| e.to_str()) != Some("jsonl") {
                continue;
            }

            if let Some(meta) = read_session_meta(&path)
                && meta.working_dir == working_dir
            {
                match &mut best {
                    Some((best_time, best_id)) => {
                        if meta.updated_at > *best_time {
                            *best_time = meta.updated_at;
                            *best_id = meta.id.clone();
                        }
                    }
                    None => {
                        best = Some((meta.updated_at, meta.id.clone()));
                    }
                }
            }
        }

        best.map(|(_, id)| id)
    }

    /// Find a session by an ID prefix.
    pub fn find_by_prefix(&self, prefix: &str) -> Result<String, SessionError> {
        let all = self.list_all();
        let matches: Vec<&SessionMeta> = all.iter().filter(|m| m.id.starts_with(prefix)).collect();

        match matches.len() {
            0 => Err(SessionError::NotFound(format!(
                "No session found matching '{}'",
                prefix
            ))),
            1 => Ok(matches[0].id.clone()),
            _ => {
                let ids: Vec<&str> = matches
                    .iter()
                    .map(|m| &m.id[..12.min(m.id.len())])
                    .collect();
                Err(SessionError::NotFound(format!(
                    "Multiple sessions match '{}': {}",
                    prefix,
                    ids.join(", ")
                )))
            }
        }
    }

    /// List all sessions, most recently updated first.
    pub fn list_all(&self) -> Vec<SessionMeta> {
        if !self.dir.exists() {
            return Vec::new();
        }

        let mut sessions: Vec<SessionMeta> = Vec::new();

        if let Ok(entries) = fs::read_dir(&self.dir) {
            for entry in entries.flatten() {
                let path = entry.path();
                if path.extension().and_then(|e| e.to_str()) != Some("jsonl") {
                    continue;
                }

                if let Some(meta) = read_session_meta(&path) {
                    sessions.push(meta);
                }
            }
        }

        sessions.sort_by_key(|b| std::cmp::Reverse(b.updated_at));
        sessions
    }

    /// Returns the directory path where sessions are stored.
    pub fn dir(&self) -> &PathBuf {
        &self.dir
    }

    /// Delete a session by its ID.
    pub fn delete(&self, session_id: &str) -> Result<(), SessionError> {
        let path = self.dir.join(format!("{}.jsonl", session_id));

        if !path.exists() {
            return Err(SessionError::NotFound(format!(
                "Session '{}' not found",
                session_id
            )));
        }

        fs::remove_file(&path)?;
        Ok(())
    }
}

// ── Helpers ──────────────────────────────────────────────────────────────────

fn now_timestamp() -> u64 {
    SystemTime::now()
        .duration_since(UNIX_EPOCH)
        .unwrap_or_default()
        .as_secs()
}

fn read_session_meta(path: &PathBuf) -> Option<SessionMeta> {
    let file = fs::File::open(path).ok()?;
    let mut reader = io::BufReader::new(file);
    let mut first_line = String::new();
    if reader.read_line(&mut first_line).ok()? == 0 {
        return None;
    }
    if let Ok(SessionEntry::Meta(meta)) = serde_json::from_str::<SessionEntry>(first_line.trim()) {
        Some(meta)
    } else {
        None
    }
}

// ── Session handle ─────────────────────────────────────────────────────────

/// Manages a session's lifecycle: writing entries to the JSONL file.
///
/// Use [`SessionStore`] to create or load sessions. The `Session` type
/// handles the actual message appending, metadata updates, and auto-save.
pub struct Session {
    meta: SessionMeta,
    path: PathBuf,
    dirty: bool,
    messages_since_save: usize,
    created: bool,
}

/// Auto-save threshold: flush metadata every N messages appended.
const AUTO_SAVE_INTERVAL: usize = 5;

impl Session {
    // ── Mutating operations ────────────────────────────────────────────

    /// Append a message to the session file and update metadata.
    pub fn append_message(&mut self, message: &Message) {
        self.ensure_created();
        self.write_entry(&SessionEntry::Message(message.clone()));
        self.meta.message_count += 1;
        self.meta.updated_at = now_timestamp();
        self.dirty = true;
        self.messages_since_save += 1;
        if self.messages_since_save >= AUTO_SAVE_INTERVAL {
            self.flush();
            self.messages_since_save = 0;
        }
    }

    /// Update the session mode.
    pub fn set_mode(&mut self, mode: AgentMode) {
        self.meta.mode = mode;
        self.meta.updated_at = now_timestamp();
        self.dirty = true;
    }

    /// Update the model name.
    pub fn set_model(&mut self, model: Option<String>) {
        self.meta.model = model;
        self.meta.updated_at = now_timestamp();
        self.dirty = true;
    }

    /// Set a human-readable name for the session.
    pub fn set_name(&mut self, name: String) {
        self.meta.name = Some(name);
        self.meta.updated_at = now_timestamp();
        self.dirty = true;
    }

    /// Set the last known token usage reported by the provider.
    /// This is stored in the session metadata so it survives restarts.
    pub fn set_token_usage(&mut self, usage: TokenUsage) {
        self.meta.token_usage = Some(usage);
        self.meta.updated_at = now_timestamp();
        self.dirty = true;
    }

    /// Add to the cumulative tool call count for this session.
    pub fn add_tool_calls(&mut self, count: u64) {
        self.meta.total_tool_calls += count;
        self.meta.updated_at = now_timestamp();
        self.dirty = true;
    }

    /// Add to the cumulative total tokens used in this session.
    pub fn add_tokens_used(&mut self, count: u64) {
        self.meta.total_tokens_used += count;
        self.meta.updated_at = now_timestamp();
        self.dirty = true;
    }

    /// Flush any metadata changes back to the file (rewrite the first line).
    pub fn flush(&mut self) {
        if !self.dirty {
            return;
        }

        self.ensure_created();

        let file = match fs::read_to_string(&self.path) {
            Ok(content) => content,
            Err(_) => return,
        };

        let lines: Vec<&str> = file.lines().collect();
        if lines.is_empty() {
            return;
        }

        let meta_line = match serde_json::to_string(&SessionEntry::Meta(self.meta.clone())) {
            Ok(l) => l,
            Err(_) => return,
        };

        let tmp_path = self.path.with_extension("jsonl.tmp");

        match fs::File::create(&tmp_path) {
            Ok(mut file) => {
                if writeln!(file, "{}", meta_line).is_err() {
                    let _ = fs::remove_file(&tmp_path);
                    return;
                }
                for line in lines.iter().skip(1) {
                    if writeln!(file, "{}", line).is_err() {
                        let _ = fs::remove_file(&tmp_path);
                        return;
                    }
                }
                if file.flush().is_err() {
                    let _ = fs::remove_file(&tmp_path);
                    return;
                }
            }
            Err(_) => return,
        }

        if fs::rename(&tmp_path, &self.path).is_err() {
            let _ = fs::remove_file(&tmp_path);
        }

        self.dirty = false;
    }

    // ── Accessors ──────────────────────────────────────────────────────

    pub fn id(&self) -> &str {
        &self.meta.id
    }

    /// Full path to the session's JSONL file.
    pub fn path(&self) -> &Path {
        &self.path
    }

    pub fn meta(&self) -> &SessionMeta {
        &self.meta
    }

    // ── Internal ────────────────────────────────────────────────────────

    fn ensure_created(&mut self) {
        if self.created {
            return;
        }

        // Ensure directory exists
        let _ = fs::create_dir_all(
            self.path
                .parent()
                .unwrap_or_else(|| std::path::Path::new(".")),
        );

        self.write_entry(&SessionEntry::Meta(self.meta.clone()));
        self.created = true;
    }

    fn write_entry(&self, entry: &SessionEntry) {
        let line = match serde_json::to_string(entry) {
            Ok(l) => l,
            Err(_) => return,
        };

        if let Ok(mut file) = fs::OpenOptions::new()
            .create(true)
            .append(true)
            .open(&self.path)
        {
            let _ = writeln!(file, "{}", line);
        }
    }
}

impl Drop for Session {
    fn drop(&mut self) {
        if self.created {
            self.flush();
        }
    }
}

/// Format a duration in seconds as a human-friendly age string.
pub fn format_age(secs: u64) -> String {
    if secs < 60 {
        format!("{}s ago", secs)
    } else if secs < 3600 {
        format!("{}m ago", secs / 60)
    } else if secs < 86400 {
        format!("{}h ago", secs / 3600)
    } else {
        format!("{}d ago", secs / 86400)
    }
}