agent86-core 1.4.0

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
//! Long-term memory, ported from Python's `memory/` package: the episodic and
//! semantic halves of `store.py`, plus `episodic.py` and `semantic.py`.
//!
//! - **Episodes**: one row per completed turn (task and outcome), the flight data
//!   recorder. When a new task begins, the most similar past episodes are recalled
//!   into the system prompt, so the agent benefits from (or is warned by) what
//!   happened last time.
//! - **Memories**: durable facts the agent stores and looks up through the
//!   `remember` and `recall` tools.
//!
//! Both live in the session database ([`SessionStore`]) with their embeddings as
//! `float32` BLOBs. Search is a brute-force cosine scan, as Python's store does
//! without `sqlite-vec`; retention caps bound the log tables, though curated
//! facts are never pruned automatically. Rows embedded with a different
//! dimension (another embedder) are skipped, never compared. Beyond Python, a
//! fact or episode can be pinned ([`entries`]), which exempts it from both.

pub mod embeddings;
pub mod entries;
// The local sentence-transformers model (candle, tokenizers) and the Hugging Face
// cache it loads from, behind the `local-embeddings` feature.
#[cfg(feature = "local-embeddings")]
pub mod hub;
#[cfg(feature = "local-embeddings")]
pub mod sentence_transformers;
pub mod sizing;
pub mod working;

use crate::session::{SessionStore, StoreError};
use embeddings::{cosine, pack, unpack};
pub use entries::{Entry, EntryKind, ListQuery};
use rusqlite::{params, Connection};
use serde_json::{Map, Value};
use std::time::{SystemTime, UNIX_EPOCH};

/// Recalled episodes must score at least this, so unrelated past tasks stay out.
pub const EPISODE_MIN_SCORE: f64 = 0.35;
/// Facts `recall` returns must score at least this.
pub const FACT_MIN_SCORE: f64 = 0.30;
/// `metadata["kind"]` of an archived compaction rather than a turn; never recalled.
pub const COMPACTION_KIND: &str = "compaction";
/// The note's heading, as Python's `recall_note`.
const RECALL_HEADING: &str = "Relevant past experience (from memory; may help or warn):";

/// One search result, as Python's `Hit`.
#[derive(Debug, Clone, PartialEq)]
pub struct Hit {
    pub id: i64,
    /// The episode's task, or the fact's text.
    pub text: String,
    /// Cosine similarity to the query.
    pub score: f64,
    /// The row's metadata; an episode's also carries its `outcome`.
    pub metadata: Map<String, Value>,
}

impl Hit {
    /// An episode's outcome, or `""`.
    pub fn outcome(&self) -> &str {
        self.metadata
            .get("outcome")
            .and_then(Value::as_str)
            .unwrap_or_default()
    }
}

/// Rows a prune removed, per table, in Python's dict order.
pub type Removed = Vec<(&'static str, i64)>;

fn error(error: rusqlite::Error) -> StoreError {
    StoreError(error.to_string())
}

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

impl SessionStore {
    // ---- episodes -----------------------------------------------------------

    /// Record a completed turn, as Python's `add_episode`. Returns the new row id.
    pub fn add_episode(
        &self,
        session_id: &str,
        task: &str,
        outcome: &str,
        metadata: &Map<String, Value>,
    ) -> Result<i64, StoreError> {
        let embedding = pack(&self.embedder.encode_one(task));
        let connection = self.connection();
        connection
            .execute(
                "INSERT INTO episodes (session_id, ts, task, outcome, embedding, metadata_json) \
                 VALUES (?1, ?2, ?3, ?4, ?5, ?6)",
                params![
                    session_id,
                    now(),
                    task,
                    outcome,
                    embedding,
                    metadata_json(metadata)
                ],
            )
            .map_err(error)?;
        Ok(connection.last_insert_rowid())
    }

    /// The `k` episodes whose task is most similar to `query`.
    pub fn search_episodes(&self, query: &str, k: usize) -> Result<Vec<Hit>, StoreError> {
        let query = self.embedder.encode_one(query);
        let connection = self.connection();
        rank(
            &connection,
            "SELECT id, task, embedding, metadata_json, outcome FROM episodes",
            &query,
            k,
            true,
        )
    }

    // ---- semantic memories --------------------------------------------------

    /// Store a fact, as Python's `add_memory`. Returns the new row id.
    pub fn add_memory(
        &self,
        text: &str,
        kind: &str,
        metadata: &Map<String, Value>,
    ) -> Result<i64, StoreError> {
        let embedding = pack(&self.embedder.encode_one(text));
        let connection = self.connection();
        connection
            .execute(
                "INSERT INTO memories (ts, kind, text, embedding, metadata_json) \
                 VALUES (?1, ?2, ?3, ?4, ?5)",
                params![now(), kind, text, embedding, metadata_json(metadata)],
            )
            .map_err(error)?;
        Ok(connection.last_insert_rowid())
    }

    /// The `k` facts most similar to `query`.
    pub fn search_memories(&self, query: &str, k: usize) -> Result<Vec<Hit>, StoreError> {
        let query = self.embedder.encode_one(query);
        let connection = self.connection();
        rank(
            &connection,
            "SELECT id, text, embedding, metadata_json FROM memories",
            &query,
            k,
            false,
        )
    }

    /// Delete one fact by id, pinned or not; `true` if a row was removed.
    pub fn delete_memory(&self, id: i64) -> Result<bool, StoreError> {
        self.forget(EntryKind::Fact, id)
    }

    // ---- episodic and semantic facets ---------------------------------------

    /// Archive the messages a compaction replaced, with the summary that
    /// replaced them, as Python's `record_compaction`: compaction shrinks the
    /// working memory but must not destroy the record. Tagged
    /// [`COMPACTION_KIND`], so recall skips it.
    pub fn record_compaction(
        &self,
        session_id: &str,
        summary: &str,
        dropped: &[crate::types::Message],
    ) -> Result<i64, StoreError> {
        let mut metadata = Map::new();
        metadata.insert("kind".into(), Value::from(COMPACTION_KIND));
        metadata.insert("dropped".into(), Value::from(dropped.len()));
        metadata.insert("summary".into(), Value::from(summary));
        self.add_episode(
            session_id,
            &format!("[compacted {} messages] {summary}", dropped.len()),
            &working::archive_payload(dropped),
            &metadata,
        )
    }

    /// Past episodes relevant to `task`, compactions excluded, as Python's
    /// `EpisodicMemory.recall`.
    pub fn recall_episodes(&self, task: &str, k: usize) -> Result<Vec<Hit>, StoreError> {
        Ok(self
            .search_episodes(task, k)?
            .into_iter()
            .filter(|hit| {
                hit.score >= EPISODE_MIN_SCORE
                    && hit.metadata.get("kind").and_then(Value::as_str) != Some(COMPACTION_KIND)
            })
            .collect())
    }

    /// A system-context note summarizing relevant past turns, or `None`, as
    /// Python's `EpisodicMemory.recall_note`.
    pub fn recall_note(&self, task: &str) -> Option<String> {
        let hits = self.recall_episodes(task, 3).ok()?;
        if hits.is_empty() {
            return None;
        }
        let mut lines = vec![RECALL_HEADING.to_owned()];
        for hit in &hits {
            let mut outcome = hit
                .outcome()
                .split_whitespace()
                .collect::<Vec<_>>()
                .join(" ");
            if outcome.chars().count() > 200 {
                outcome = outcome.chars().take(200).collect::<String>() + " ...";
            }
            lines.push(format!(
                "- task: {} -> {outcome}",
                crate::types::python_repr(&hit.text)
            ));
        }
        Some(lines.join("\n"))
    }

    /// Facts relevant to `query`, as Python's `SemanticMemory.search`.
    pub fn search_facts(&self, query: &str, k: usize) -> Result<Vec<Hit>, StoreError> {
        Ok(self
            .search_memories(query, k)?
            .into_iter()
            .filter(|hit| hit.score >= FACT_MIN_SCORE)
            .collect())
    }

    // ---- pruning ------------------------------------------------------------

    /// Apply retention limits, as Python's `prune`: episodes and sessions (the
    /// append-only log) always, curated facts only when `memories` is set.
    /// Pinned rows are exempt and do not count toward `keep_last` (beyond
    /// Python).
    pub fn prune(
        &self,
        older_than_days: Option<f64>,
        keep_last: Option<i64>,
        memories: bool,
    ) -> Result<Removed, StoreError> {
        let connection = self.connection();
        let mut removed = vec![
            (
                "episodes",
                prune_table(&connection, Table::Episodes, older_than_days, keep_last)?,
            ),
            (
                "sessions",
                prune_table(&connection, Table::Sessions, older_than_days, keep_last)?,
            ),
        ];
        if memories {
            removed.push((
                "memories",
                prune_table(&connection, Table::Memories, older_than_days, keep_last)?,
            ));
        }
        entries::sweep_pins(&connection)?;
        Ok(removed)
    }

    /// Per-table retention caps for the log, as Python's `enforce_retention`. A cap
    /// of 0 disables it; facts are never touched. Only tables that lost rows are
    /// reported. Pinned episodes are exempt and do not count toward the cap.
    pub fn enforce_retention(
        &self,
        max_episodes: i64,
        max_sessions: i64,
        max_age_days: f64,
    ) -> Result<Removed, StoreError> {
        let age = (max_age_days != 0.0).then_some(max_age_days);
        let connection = self.connection();
        let mut removed = Removed::new();
        for (name, table, cap) in [
            ("episodes", Table::Episodes, max_episodes),
            ("sessions", Table::Sessions, max_sessions),
        ] {
            if cap != 0 || age.is_some() {
                let count = prune_table(&connection, table, age, (cap != 0).then_some(cap))?;
                if count != 0 {
                    removed.push((name, count));
                }
            }
        }
        entries::sweep_pins(&connection)?;
        Ok(removed)
    }
}

/// `json.dumps(metadata or {})`.
fn metadata_json(metadata: &Map<String, Value>) -> String {
    crate::pyjson::python_json_dumps(&Value::Object(metadata.clone()), true)
}

/// Score every row against `query` and keep the best `k`, as Python's `_rank`.
///
/// The SELECT yields `id, text, embedding, metadata_json` and, for episodes, the
/// outcome, which joins the metadata. The sort is stable, so ties keep row order.
fn rank(
    connection: &Connection,
    sql: &str,
    query: &[f64],
    k: usize,
    with_outcome: bool,
) -> Result<Vec<Hit>, StoreError> {
    let mut statement = connection.prepare(sql).map_err(error)?;
    let mut rows = statement.query([]).map_err(error)?;
    let mut scored = Vec::new();
    while let Some(row) = rows.next().map_err(error)? {
        let blob: Option<Vec<u8>> = row.get(2).map_err(error)?;
        let Some(blob) = blob.filter(|blob| !blob.is_empty()) else {
            continue;
        };
        let vector = unpack(&blob);
        if vector.len() != query.len() {
            // Embedded by a different embedder: a cross-dimension score means nothing.
            continue;
        }
        let raw: Option<String> = row.get(3).map_err(error)?;
        let mut metadata = raw
            .as_deref()
            .and_then(|raw| serde_json::from_str::<Map<String, Value>>(raw).ok())
            .unwrap_or_default();
        if with_outcome {
            let outcome: String = row.get(4).map_err(error)?;
            metadata.insert("outcome".into(), Value::String(outcome));
        }
        scored.push(Hit {
            id: row.get(0).map_err(error)?,
            text: row.get(1).map_err(error)?,
            score: cosine(query, &vector),
            metadata,
        });
    }
    scored.sort_by(|a, b| b.score.total_cmp(&a.score));
    scored.truncate(k);
    Ok(scored)
}

#[derive(Clone, Copy)]
enum Table {
    Episodes,
    Sessions,
    Memories,
}

impl Table {
    /// The table, its timestamp column, and its id column.
    fn columns(self) -> (&'static str, &'static str, &'static str) {
        match self {
            Self::Episodes => ("episodes", "ts", "id"),
            Self::Sessions => ("sessions", "updated_at", "session_id"),
            Self::Memories => ("memories", "ts", "id"),
        }
    }

    /// SQL true of the rows a prune may remove: every session, and the facts and
    /// episodes that are not pinned.
    fn prunable(self) -> String {
        match self {
            Self::Episodes => EntryKind::Episode.unpinned(),
            Self::Sessions => "1".to_owned(),
            Self::Memories => EntryKind::Fact.unpinned(),
        }
    }
}

/// Delete rows failing any given constraint and count them, as Python's
/// `_prune_table`: a row survives only if it is newer than the age cutoff and
/// among the `keep_last` most recent, or is pinned (beyond Python); pinned rows
/// are left out of the `keep_last` count.
fn prune_table(
    connection: &Connection,
    table: Table,
    older_than_days: Option<f64>,
    keep_last: Option<i64>,
) -> Result<i64, StoreError> {
    let prunable = table.prunable();
    let (table, ts, id) = table.columns();
    let mut deleted = 0;
    if let Some(days) = older_than_days {
        let cutoff = now() - days * 86400.0;
        deleted += connection
            .execute(
                &format!("DELETE FROM {table} WHERE {ts} < ?1 AND {prunable}"),
                params![cutoff],
            )
            .map_err(error)?;
    }
    if let Some(keep) = keep_last {
        deleted += connection
            .execute(
                &format!(
                    "DELETE FROM {table} WHERE {prunable} AND {id} NOT IN \
                     (SELECT {id} FROM {table} WHERE {prunable} ORDER BY {ts} DESC LIMIT ?1)"
                ),
                params![keep.max(0)],
            )
            .map_err(error)?;
    }
    Ok(deleted as i64)
}