agent86-core 1.2.2

The agent behind agent86: a safe agent runtime you can embed
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
//! Sessions: the agent state threaded through a conversation and its SQLite
//! persistence, ported from Python's `orchestration/state.py` and the session
//! half of `memory/store.py`.
//!
//! The database file, schema, and `state_json` layout are Python's, so a session
//! started by either implementation can be resumed by the other. The same store
//! holds episodes and semantic facts; their half is in [`crate::memory`].

use crate::memory::embeddings::{Embedder, HashingEmbedder};
use crate::turn::{new_session_id, TurnSummary};
use crate::types::{AgentPhase, Message, Role, Step, Usage};
use regex::Regex;
use rusqlite::{params, Connection, OptionalExtension};
use serde::{Deserialize, Serialize};
use std::path::{Path, PathBuf};
use std::sync::{Arc, LazyLock, Mutex, MutexGuard};
use std::time::{SystemTime, UNIX_EPOCH};

/// Longest a derived session title may be, including the ellipsis.
pub const SESSION_TITLE_MAX: usize = 60;

/// Mutable state for one agent session, as Python's `AgentState`.
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
pub struct AgentState {
    #[serde(default = "new_session_id")]
    pub session_id: String,
    #[serde(default = "initial_phase")]
    pub phase: AgentPhase,
    #[serde(default)]
    pub messages: Vec<Message>,
    #[serde(default)]
    pub steps: Vec<Step>,
    #[serde(default)]
    pub usage: Usage,
    /// The turn that just finished; `None` before the first turn.
    #[serde(default)]
    pub last_turn: Option<TurnSummary>,
}

fn initial_phase() -> AgentPhase {
    AgentPhase::Init
}

impl AgentState {
    /// A fresh session, ready to execute, as Python's `Harness.new_session`.
    pub fn new() -> Self {
        Self {
            session_id: new_session_id(),
            phase: AgentPhase::Execute,
            messages: Vec::new(),
            steps: Vec::new(),
            usage: Usage::default(),
            last_turn: None,
        }
    }

    pub fn step_count(&self) -> usize {
        self.steps.len()
    }
}

impl Default for AgentState {
    fn default() -> Self {
        Self::new()
    }
}

/// The header line an `@file` mention block opens with.
static MENTION_BLOCK: LazyLock<Regex> =
    LazyLock::new(|| Regex::new(r"(?m)^--- @.*---[ \t]*$").expect("valid pattern"));

/// What the user typed, with any `@file` attachment blocks stripped off.
pub fn typed_prefix(text: &str) -> &str {
    match MENTION_BLOCK.find(text) {
        None => text,
        Some(found) => {
            let head = &text[..found.start()];
            head.split("\n\n").next().unwrap_or_default()
        }
    }
}

/// A display name for a session: its first user message, collapsed and
/// truncated. `None` until there is one, so no placeholder name sticks.
pub fn session_title(state: &AgentState) -> Option<String> {
    let collapse = |text: &str| text.split_whitespace().collect::<Vec<_>>().join(" ");
    for message in state
        .messages
        .iter()
        .filter(|message| message.role == Role::User)
    {
        let typed = collapse(typed_prefix(&message.content));
        let text = if typed.is_empty() {
            collapse(&message.content)
        } else {
            typed
        };
        if text.is_empty() {
            continue;
        }
        if text.chars().count() <= SESSION_TITLE_MAX {
            return Some(text);
        }
        let head: String = text.chars().take(SESSION_TITLE_MAX - 3).collect();
        return Some(format!("{head}..."));
    }
    None
}

/// Longest title `/rename` sets, in characters.
pub const RENAME_TITLE_MAX: usize = 200;

/// Why a title given to `/rename` was refused.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum TitleError {
    /// Nothing is left once it is trimmed and cleaned.
    Empty,
}

/// A title the user typed, made fit to store and to show in every list: trimmed,
/// its first line only, without control or bidirectional-formatting characters
/// (which could reorder or hide what other text in a list shows), and at most
/// [`RENAME_TITLE_MAX`] characters.
pub fn clean_title(raw: &str) -> Result<String, TitleError> {
    let first = raw.trim().lines().next().unwrap_or_default();
    let kept: String = first
        .chars()
        .filter(|c| !c.is_control() && !is_bidi_format(*c))
        .collect();
    let capped: String = kept.trim().chars().take(RENAME_TITLE_MAX).collect();
    let title = capped.trim_end().to_owned();
    if title.is_empty() {
        Err(TitleError::Empty)
    } else {
        Ok(title)
    }
}

/// Unicode's bidirectional formatting characters (marks, embeddings, overrides,
/// and isolates).
fn is_bidi_format(c: char) -> bool {
    matches!(
        c,
        '\u{061C}' | '\u{200E}' | '\u{200F}' | '\u{202A}'..='\u{202E}' | '\u{2066}'..='\u{2069}'
    )
}

/// The memory database could not be opened. The message says what to do about it.
#[derive(Debug, Clone, PartialEq, Eq, thiserror::Error)]
#[error("{0}")]
pub struct StoreError(pub String);

/// Python's memory database: sessions here, episodes and facts in [`crate::memory`].
///
/// Clones share one connection, so the CLI, the harness, and the memory tools
/// all use the same store. Access is serialized by its lock, as Python's store is
/// only ever used from one thread at a time.
#[derive(Clone)]
pub struct SessionStore {
    connection: Arc<Mutex<Connection>>,
    pub(crate) embedder: Arc<dyn Embedder>,
    path: PathBuf,
}

impl std::fmt::Debug for SessionStore {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        f.debug_struct("SessionStore")
            .field("path", &self.path)
            .field("embedder", &self.embedder.spec())
            .finish()
    }
}

impl SessionStore {
    /// Open (creating if needed) the database at `path`, or `:memory:`, embedding
    /// with the default `hash:256`.
    pub fn open(path: &Path) -> Result<Self, StoreError> {
        Self::open_with(path, Arc::new(HashingEmbedder::default()))
    }

    /// [`SessionStore::open`] with the embedder episodes and facts are indexed by.
    pub fn open_with(path: &Path, embedder: Arc<dyn Embedder>) -> Result<Self, StoreError> {
        let opened = (|| -> Result<Connection, Box<dyn std::error::Error>> {
            if path != Path::new(":memory:") {
                if let Some(parent) = path
                    .parent()
                    .filter(|parent| !parent.as_os_str().is_empty())
                {
                    std::fs::create_dir_all(parent)?;
                }
            }
            let connection = Connection::open(path)?;
            connection.execute_batch(SCHEMA)?;
            connection.execute_batch(crate::memory::entries::PINS_SCHEMA)?;
            Ok(connection)
        })();
        opened
            .map(|connection| Self {
                connection: Arc::new(Mutex::new(connection)),
                embedder,
                path: path.to_owned(),
            })
            .map_err(|error| {
                StoreError(format!(
                    "Could not open the memory database at {} - {error}. Another agent86 may be \
                 using it (close it and retry), or point `[memory] path` at another file - \
                 `agent86 config path` shows which file to edit. `[memory] enabled = false` \
                 turns memory off entirely.",
                    path.display()
                ))
            })
    }

    /// The database file, as Python's `MemoryStore.path`.
    pub fn path(&self) -> &Path {
        &self.path
    }

    /// The embedder's spec, e.g. `hash:256`.
    pub fn embedder_spec(&self) -> String {
        self.embedder.spec()
    }

    /// The connection; a panic while it was held cannot leave SQLite inconsistent.
    pub(crate) fn connection(&self) -> MutexGuard<'_, Connection> {
        self.connection
            .lock()
            .unwrap_or_else(std::sync::PoisonError::into_inner)
    }

    /// Upsert a session's state. `title = None` leaves any stored name alone.
    pub fn save(&self, state: &AgentState, title: Option<&str>) -> Result<(), StoreError> {
        let state_json =
            serde_json::to_string(state).map_err(|error| StoreError(error.to_string()))?;
        let now = now();
        self.connection()
            .execute(
                "INSERT INTO sessions (session_id, created_at, updated_at, title, state_json)
                 VALUES (?1, ?2, ?3, ?4, ?5)
                 ON CONFLICT(session_id) DO UPDATE SET
                     updated_at = excluded.updated_at,
                     title = COALESCE(excluded.title, sessions.title),
                     state_json = excluded.state_json",
                params![state.session_id, now, now, title, state_json],
            )
            .map(drop)
            .map_err(|error| StoreError(error.to_string()))
    }

    /// The stored state for `session_id`, if there is one that parses.
    pub fn load(&self, session_id: &str) -> Result<Option<AgentState>, StoreError> {
        let raw: Option<String> = self
            .connection()
            .query_row(
                "SELECT state_json FROM sessions WHERE session_id = ?1",
                params![session_id],
                |row| row.get(0),
            )
            .optional()
            .map_err(|error| StoreError(error.to_string()))?;
        raw.map(|json| {
            serde_json::from_str(&json).map_err(|error| {
                StoreError(format!("session {session_id} could not be read: {error}"))
            })
        })
        .transpose()
    }

    /// The stored title for one session, or `None` (including for an unknown session).
    pub fn title(&self, session_id: &str) -> Result<Option<String>, StoreError> {
        self.connection()
            .query_row(
                "SELECT title FROM sessions WHERE session_id = ?1",
                params![session_id],
                |row| row.get::<_, Option<String>>(0),
            )
            .optional()
            .map(Option::flatten)
            .map_err(|error| StoreError(error.to_string()))
    }

    /// Set a session's title (beyond Python, which names a session only once, from
    /// its first message): what `/rename` does. Returns whether there was such a
    /// session. The last-activity time is left alone, so a rename does not move the
    /// session up a list. [`SessionStore::persist`] never replaces a title that is
    /// set, so the new one stays. Callers clean the title first ([`clean_title`]).
    pub fn set_title(&self, session_id: &str, title: &str) -> Result<bool, StoreError> {
        self.connection()
            .execute(
                "UPDATE sessions SET title = ?2 WHERE session_id = ?1",
                params![session_id, title],
            )
            .map(|changed| changed > 0)
            .map_err(|error| StoreError(error.to_string()))
    }

    /// Save `state`, naming the session once, on the first save that has a user
    /// message to name it after, as Python's `Harness._persist`.
    pub fn persist(&self, state: &AgentState) -> Result<(), StoreError> {
        let title = if self.title(&state.session_id)?.is_none() {
            session_title(state)
        } else {
            None
        };
        self.save(state, title.as_deref())
    }

    /// Recent sessions, newest first, as Python's `MemoryStore.recent_sessions`.
    pub fn recent_sessions(&self, limit: usize) -> Result<Vec<SessionInfo>, StoreError> {
        let error = |error: rusqlite::Error| StoreError(error.to_string());
        let connection = self.connection();
        let mut statement = connection
            .prepare(
                "SELECT session_id, updated_at, title FROM sessions \
                 ORDER BY updated_at DESC LIMIT ?1",
            )
            .map_err(error)?;
        let rows = statement
            .query_map(params![limit as i64], |row| {
                Ok(SessionInfo {
                    session_id: row.get(0)?,
                    updated_at: row.get::<_, Option<f64>>(1)?.unwrap_or(0.0),
                    title: row.get(2)?,
                })
            })
            .map_err(error)?;
        rows.collect::<Result<_, _>>().map_err(error)
    }

    /// Row counts of the three tables, for `/memory`.
    pub fn counts(&self) -> Result<StoreCounts, StoreError> {
        let count = |table: &str| {
            self.connection()
                .query_row(&format!("SELECT COUNT(*) FROM {table}"), [], |row| {
                    row.get::<_, i64>(0)
                })
                .map_err(|error| StoreError(error.to_string()))
        };
        Ok(StoreCounts {
            sessions: count("sessions")?,
            episodes: count("episodes")?,
            memories: count("memories")?,
        })
    }
}

/// One row of the session list, as Python's `SessionInfo`.
#[derive(Debug, Clone, PartialEq)]
pub struct SessionInfo {
    pub session_id: String,
    /// `None` for a session saved before its first user message.
    pub title: Option<String>,
    /// Seconds since the Unix epoch.
    pub updated_at: f64,
}

impl SessionInfo {
    /// The title, or a stand-in for a session that never got one.
    pub fn label(&self) -> &str {
        self.title.as_deref().unwrap_or("(untitled)")
    }
}

/// How many rows the memory database holds.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub struct StoreCounts {
    pub sessions: i64,
    pub episodes: i64,
    pub memories: i64,
}

/// Python's schema, verbatim.
const SCHEMA: &str = "
CREATE TABLE IF NOT EXISTS sessions (
    session_id TEXT PRIMARY KEY,
    created_at REAL NOT NULL,
    updated_at REAL NOT NULL,
    title TEXT,
    state_json TEXT NOT NULL
);
CREATE TABLE IF NOT EXISTS episodes (
    id INTEGER PRIMARY KEY AUTOINCREMENT,
    session_id TEXT,
    ts REAL NOT NULL,
    task TEXT NOT NULL,
    outcome TEXT NOT NULL,
    embedding BLOB,
    metadata_json TEXT
);
CREATE TABLE IF NOT EXISTS memories (
    id INTEGER PRIMARY KEY AUTOINCREMENT,
    ts REAL NOT NULL,
    kind TEXT NOT NULL DEFAULT 'fact',
    text TEXT NOT NULL,
    embedding BLOB,
    metadata_json TEXT
);
";

fn now() -> f64 {
    SystemTime::now()
        .duration_since(UNIX_EPOCH)
        .map_or(0.0, |elapsed| elapsed.as_secs_f64())
}

/// `[memory] path` with a leading `~` expanded, as Python's `os.path.expanduser`.
pub fn expand_home(path: &str) -> PathBuf {
    let home = if cfg!(windows) { "USERPROFILE" } else { "HOME" };
    match (path.strip_prefix('~'), std::env::var_os(home)) {
        (Some(rest), Some(home)) if rest.is_empty() || rest.starts_with(['/', '\\']) => {
            // Joined part by part, so the separators are the platform's, as with
            // Python's `Path`; `~/.agent86/x` must not become `...\.agent86/x`.
            let mut expanded = PathBuf::from(home);
            for part in rest.split(['/', '\\']).filter(|part| !part.is_empty()) {
                expanded.push(part);
            }
            expanded
        }
        _ => PathBuf::from(path),
    }
}