neosian 2.17.1

The state layer for LLM agents, as one static binary: the record, memory and the state process
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
//! Turn persistence over the root (`conversation/file_turns.py`, DESIGN
//! ยง9.8): `root/conversations/<id>/turns.jsonl` and `projections.jsonl`,
//! each read whole under the root-wide lock the memory side holds too, a
//! malformed or newer row refused and never skipped; the appends the
//! record verb and the wire land, unsynced as the Python side writes them.

use std::fs;
use std::path::{Path, PathBuf};

use super::FileStore;
use super::fileio::{append_line, private_mkdir};
use super::time::Stamp;
use crate::conversation::ids::parse_conversation_id;
use crate::conversation::message::Message;
use crate::conversation::page::{Mode, Resume, Visit, owed};
use crate::conversation::rank::{Key, Order, Ranked, Ranking, Request};
use crate::conversation::rows::{
    CONVERSATIONS, PROJECTIONS, TURNS, parse_projection, parse_turn, render_projection, render_turn,
};
use crate::conversation::search::{held_ids, line_text, read_at, scan_log, turn_text};
use crate::conversation::{Error, Projection, Refused, Turn};

/// `_check_cursor`: the ABC's own refusals of a negative start or limit.
fn check_cursor(after: i64, limit: Option<i64>) -> Result<(), Error> {
    if after < 0 {
        return Err(Error::Value(format!("after must be >= 0, got {after}")));
    }
    if let Some(limit) = limit.filter(|limit| *limit < 0) {
        return Err(Error::Value(format!("limit must be >= 0, got {limit}")));
    }
    Ok(())
}

/// The rows past `after`, at most `limit`.
fn window<T>(rows: Vec<T>, after: i64, limit: Option<i64>, turn_of: impl Fn(&T) -> u64) -> Vec<T> {
    let mut kept: Vec<T> = rows
        .into_iter()
        .filter(|row| turn_of(row) > after as u64)
        .collect();
    if let Some(limit) = limit {
        kept.truncate(limit as usize);
    }
    kept
}

impl FileStore {
    /// The turns past `after`, oldest first, at most `limit`; none for a
    /// conversation never written.
    pub fn read_turns(
        &self,
        conversation_id: &str,
        after: i64,
        limit: Option<i64>,
    ) -> Result<Vec<Turn>, Error> {
        let mut turns = Vec::new();
        self.read_turns_each(conversation_id, after, limit, &mut |turn| {
            turns.push(turn);
            Ok(true)
        })?;
        Ok(turns)
    }

    /// `read_turns` one turn at a time (DESIGN ยง40.46): the log streams a
    /// line at a time under the lock and every row is still decoded, so a
    /// malformed row anywhere refuses as a whole read would, even after
    /// `visit` stopped taking turns.
    pub fn read_turns_each(
        &self,
        conversation_id: &str,
        after: i64,
        limit: Option<i64>,
        visit: &mut dyn FnMut(Turn) -> Result<bool, Error>,
    ) -> Result<(), Error> {
        let _held = self.lock()?;
        let file = self.conversation_file(conversation_id, TURNS)?;
        check_cursor(after, limit)?;
        if !file.is_file() {
            return Ok(());
        }
        let mut reader = std::io::BufReader::new(fs::File::open(&file)?);
        let (mut buffer, mut number, mut taken) = (String::new(), 0_usize, 0_i64);
        let mut open = limit != Some(0);
        loop {
            buffer.clear();
            if std::io::BufRead::read_line(&mut reader, &mut buffer)? == 0 {
                return Ok(());
            }
            number += 1;
            let turn = parse_turn(line_text(&buffer), number, conversation_id)?;
            if open && turn.turn > after as u64 {
                taken += 1;
                open = visit(turn)? && limit.is_none_or(|limit| taken < limit);
            }
        }
    }

    /// One size page of `read_turns` (DESIGN ยง40.46): from `hint`, the byte
    /// offset and line number of the row after `after` when an earlier page
    /// found it, else from the log's start, decoding each row it passes; it
    /// stops when `visit` declines a turn and answers where that turn's row
    /// starts. A hint is trusted only at a line start whose row holds turn
    /// `after + 1`; otherwise the page reads from the start. Across the pages
    /// of one read every row is decoded once, so a malformed row still
    /// refuses the read, on the page that reaches it.
    pub fn read_turns_page(
        &self,
        conversation_id: &str,
        after: i64,
        limit: Option<i64>,
        hint: Option<(u64, u64)>,
        visit: &mut dyn FnMut(Turn) -> Result<bool, Error>,
    ) -> Result<Option<(u64, u64)>, Error> {
        use std::io::{BufRead, Read, Seek, SeekFrom};
        let _held = self.lock()?;
        let file = self.conversation_file(conversation_id, TURNS)?;
        check_cursor(after, limit)?;
        if !file.is_file() || limit == Some(0) {
            return Ok(None);
        }
        let mut reader = std::io::BufReader::new(fs::File::open(&file)?);
        let (mut offset, mut number) = (0_u64, 0_u64);
        let mut buffer = String::new();
        if let Some((at, line)) = hint.filter(|(at, line)| *at > 0 && *line > 1) {
            reader.seek(SeekFrom::Start(at - 1))?;
            let mut before = [0_u8];
            let boundary = reader.read(&mut before)? == 1 && before[0] == b'\n';
            let next = boundary && reader.read_line(&mut buffer)? > 0 && {
                let row = parse_turn(line_text(&buffer), line as usize, conversation_id);
                row.is_ok_and(|turn| turn.turn == after as u64 + 1)
            };
            (offset, number) = if next { (at, line - 1) } else { (0, 0) };
            reader.seek(SeekFrom::Start(offset))?;
        }
        let mut taken = 0_i64;
        loop {
            buffer.clear();
            let read = reader.read_line(&mut buffer)? as u64;
            if read == 0 {
                return Ok(None);
            }
            let start = offset;
            (offset, number) = (offset + read, number + 1);
            let turn = parse_turn(line_text(&buffer), number as usize, conversation_id)?;
            if turn.turn > after as u64 {
                if !visit(turn)? {
                    return Ok(Some((start, number)));
                }
                taken += 1;
                if limit.is_some_and(|limit| taken >= limit) {
                    return Ok(None);
                }
            }
        }
    }

    /// The projection entries past `after` in store order (by turn, then
    /// span, insertion order breaking ties), at most `limit`.
    pub fn read_projections(
        &self,
        conversation_id: &str,
        after: i64,
        limit: Option<i64>,
    ) -> Result<Vec<Projection>, Error> {
        let _held = self.lock()?;
        let file = self.conversation_file(conversation_id, PROJECTIONS)?;
        check_cursor(after, limit)?;
        let mut entries = rows(&file, |line, number| {
            parse_projection(line, number, conversation_id)
        })?;
        entries.sort_by_key(|entry| (entry.turn, entry.span));
        Ok(window(entries, after, limit, |entry| entry.turn))
    }

    /// The last turn's number, 0 for a conversation never written.
    pub fn last_turn_number(&self, conversation_id: &str) -> Result<u64, Error> {
        let _held = self.lock()?;
        let file = self.conversation_file(conversation_id, TURNS)?;
        last_number(&file, conversation_id)
    }

    /// One turn appended under the root lock: the number after the log's
    /// last row, the row written whole in one unsynced append.
    pub fn append_turn(
        &self,
        conversation_id: &str,
        messages: Vec<Message>,
        actor: Option<String>,
    ) -> Result<Turn, Error> {
        let _held = self.lock()?;
        let file = self.conversation_file(conversation_id, TURNS)?;
        if messages.is_empty() {
            return Err(Error::Value(
                "a turn must carry at least one message".to_string(),
            ));
        }
        let turn = Turn {
            conversation_id: conversation_id.to_string(),
            turn: last_number(&file, conversation_id)? + 1,
            messages,
            created_at: Stamp::now(),
            actor,
            redacted: false,
        };
        append_line(&file, &render_turn(&turn), false)?;
        Ok(turn)
    }

    /// Projection entries appended in one unsynced write; none is nothing.
    pub fn append_projections(
        &self,
        conversation_id: &str,
        entries: &[Projection],
    ) -> Result<(), Error> {
        let _held = self.lock()?;
        let file = self.conversation_file(conversation_id, PROJECTIONS)?;
        if entries.is_empty() {
            return Ok(());
        }
        private_mkdir(file.parent().unwrap_or(&self.root))?;
        let text: String = entries.iter().map(render_projection).collect();
        append_line(&file, &text, false)?;
        Ok(())
    }

    /// When a conversation began: its first row's stamp, that row alone
    /// parsed; None for one never written.
    pub fn first_turn_stamp(&self, conversation_id: &str) -> Result<Option<Stamp>, Error> {
        let _held = self.lock()?;
        let file = self.conversation_file(conversation_id, TURNS)?;
        if !file.is_file() {
            return Ok(None);
        }
        let text = fs::read_to_string(&file)?;
        match text.lines().next() {
            Some(line) => Ok(Some(parse_turn(line, 1, conversation_id)?.created_at)),
            None => Ok(None),
        }
    }

    /// The turns holding every term, newest first, at most `limit`;
    /// `ids` narrows to those conversations, and an empty list answers
    /// nothing.
    pub fn search_turns(
        &self,
        terms: &[String],
        ids: Option<&[String]>,
        limit: usize,
    ) -> Result<Vec<Turn>, Error> {
        let mut found = Vec::new();
        self.search_each(terms, ids, limit, &mut |turn| {
            found.push(turn);
            Ok(true)
        })?;
        Ok(found)
    }

    /// `search_turns` one turn at a time, newest first (DESIGN ยง40.46).
    pub fn search_each(
        &self,
        terms: &[String],
        ids: Option<&[String]>,
        limit: usize,
        visit: &mut dyn FnMut(Turn) -> Result<bool, Error>,
    ) -> Result<(), Error> {
        let _held = self.lock()?;
        if let Some(ids) = ids {
            for conversation_id in ids {
                parse_conversation_id(conversation_id)?;
            }
            if ids.is_empty() {
                return Ok(());
            }
        }
        let listing = match ids {
            Some(ids) => Listing::Given(ids),
            None => Listing::All(held_ids(&self.root.join(CONVERSATIONS))?),
        };
        // Version 1 is newest without bounds: the same pipeline, at most
        // `limit` keys held while the logs stream (DESIGN ยง40.45).
        let request = Request {
            terms: terms.to_vec(),
            ids: None,
            limit,
            order: Order::Newest,
            since: None,
            before: None,
        };
        self.rank_logs(&request, listing, &Resume::Fresh, &mut |turn, _| {
            visit(turn)
        })?;
        Ok(())
    }

    /// History ranking version 1 under the store lock (DESIGN ยง40.43).
    pub fn rank_turns(&self, request: &Request) -> Result<Vec<Ranked>, Error> {
        let mut ranked = Vec::new();
        self.rank_each(request, &Resume::Fresh, Mode::Each, &mut |turn, score| {
            ranked.push((turn, score));
            Ok(true)
        })?;
        Ok(ranked)
    }

    /// Ranked or newest search handing each turn to `visit` in answer order,
    /// from `resume` (DESIGN ยง40.46); under relevance it answers the
    /// candidate statistics' digest. Files keep no response budget, so the
    /// read mode changes nothing here.
    pub fn rank_each(
        &self,
        request: &Request,
        resume: &Resume,
        _mode: Mode,
        visit: &mut Visit,
    ) -> Result<Option<String>, Error> {
        request.bounds().map_err(Error::Value)?;
        let _held = self.lock()?;
        let listing = match &request.ids {
            Some(ids) => {
                for conversation_id in ids {
                    parse_conversation_id(conversation_id)?;
                }
                Listing::Given(ids)
            }
            None => Listing::All(held_ids(&self.root.join(CONVERSATIONS))?),
        };
        self.rank_logs(request, listing, resume, visit)
    }

    /// Pass one streams every selected log and keeps facts; pass two reads
    /// the winners' lines again by offset and hands each to `visit`. The
    /// caller holds the lock, so the logs cannot change between the passes.
    fn rank_logs(
        &self,
        request: &Request,
        listing: Listing,
        resume: &Resume,
        visit: &mut Visit,
    ) -> Result<Option<String>, Error> {
        let mut ranking = Ranking::new(request, false)?;
        if let Resume::After(last) = resume {
            ranking.resume_after(last.clone());
        }
        let all = request.order == Order::Newest;
        let scan = |ranking: &mut Ranking, id: &str, conversation: u32| {
            let file = self.conversation_file(id, TURNS)?;
            if !file.is_file() {
                return Ok::<(), Error>(());
            }
            scan_log(&file, id, &request.terms, all, |turn, place, line| {
                let key = Key {
                    created: turn.created_at.instant(),
                    conversation,
                    turn: turn.turn,
                    place,
                    line: u32::try_from(line).unwrap_or(u32::MAX),
                };
                ranking.offer(key, &turn_text(&turn))
            })
        };
        match listing {
            Listing::Given(ids) => {
                for id in ids {
                    let conversation = ranking.conversation(id);
                    scan(&mut ranking, id, conversation)?;
                }
            }
            Listing::All(mut ids) => {
                // Sorted, the listing is the id table: no second copy (#417).
                ids.sort_unstable();
                ids.dedup();
                let count = u32::try_from(ids.len()).expect("conversation count fits u32");
                ranking.adopt_sorted(ids);
                for conversation in 0..count {
                    let id = ranking.name(conversation).to_string();
                    scan(&mut ranking, &id, conversation)?;
                }
            }
        }
        let digest = (request.order == Order::Relevance).then(|| ranking.digest());
        let winners = owed(ranking.winners(), resume, digest.as_deref().unwrap_or(""))?;
        // One log open at a time, whatever the number of conversations: the
        // previous one closes before the next opens.
        let mut open: Option<(u32, std::io::BufReader<fs::File>)> = None;
        for (key, id, score) in winners {
            if open
                .as_ref()
                .is_none_or(|(held, _)| *held != key.conversation)
            {
                drop(open.take());
                let file = fs::File::open(self.conversation_file(&id, TURNS)?)?;
                open = Some((key.conversation, std::io::BufReader::new(file)));
            }
            let (_, reader) = open.as_mut().expect("a log is open");
            let turn = read_at(reader, key.place, key.line as usize, &id)?;
            if !visit(turn, score)? {
                break;
            }
        }
        Ok(digest)
    }

    /// The conversation's directory, refused when a symlink would carry
    /// it outside the root.
    pub(super) fn conversation_dir(&self, conversation_id: &str) -> Result<PathBuf, Error> {
        parse_conversation_id(conversation_id)?;
        let candidate = self.root.join(CONVERSATIONS).join(conversation_id);
        if super::fileio::within(&self.root, &candidate)? {
            Ok(candidate)
        } else {
            Err(Refused::id_invalid(conversation_id, "escapes the store root").into())
        }
    }

    pub(super) fn conversation_file(&self, id: &str, name: &str) -> Result<PathBuf, Error> {
        let file = self.conversation_dir(id)?.join(name);
        if !super::fileio::within(&self.root, &file)? {
            return Err(Refused::id_invalid(id, "escapes the store root").into());
        }
        Ok(file)
    }
}

/// The conversations a file search reads: those a request names, in its
/// order, or every one the store holds.
enum Listing<'a> {
    Given(&'a [String]),
    All(Vec<String>),
}

/// The number of a log's last row, 0 for no log; the last line alone is
/// parsed, and refused as any row would be.
fn last_number(file: &Path, conversation_id: &str) -> Result<u64, Error> {
    if !file.is_file() {
        return Ok(0);
    }
    let text = fs::read_to_string(file)?;
    let count = text.lines().count();
    match text.lines().last() {
        None => Ok(0),
        Some(line) => Ok(parse_turn(line, count, conversation_id)?.turn),
    }
}

/// Every row of a JSONL file through `parse`, numbered from 1; a missing
/// file is no rows.
fn rows<T>(
    file: &Path,
    parse: impl Fn(&str, usize) -> Result<T, Refused>,
) -> Result<Vec<T>, Error> {
    if !file.is_file() {
        return Ok(Vec::new());
    }
    let text = fs::read_to_string(file)?;
    text.lines()
        .enumerate()
        .map(|(index, line)| parse(line, index + 1).map_err(Error::from))
        .collect()
}