consciousness/src/mind/log.rs
Kent Overstreet 2b632d568b learn: nanosecond timestamps, token ranges for /score
Two related changes to the learn subsystem:

1. AST node timestamps are now non-optional — both Leaf and Branch
   variants carry a DateTime<Utc>. UNIX_EPOCH means "unset" (old entries
   deserialized from on-disk conversation logs).

   Training uses timestamps as unique keys for dedup, so we promote to
   nanosecond precision: node_timestamp_ns(), TrainData.timestamp_ns,
   FinetuneCandidate.timestamp_ns, mark_trained(ns).

2. build_token_ids() now also returns token-position ranges of assistant
   messages. These are passed to vLLM's /score endpoint via the new
   score_ranges field so only scored-position logprobs are returned —
   cuts bandwidth/compute when scoring small windows.

Co-Authored-By: Proof of Concept <poc@bcachefs.org>
2026-04-16 11:48:37 -04:00

87 lines
2.7 KiB
Rust

use anyhow::{Context, Result};
use std::fs::{File, OpenOptions};
use std::io::Write;
use std::path::{Path, PathBuf};
use crate::agent::context::AstNode;
use crate::hippocampus::transcript::JsonlBackwardIter;
use memmap2::Mmap;
pub struct ConversationLog {
path: PathBuf,
}
impl ConversationLog {
pub fn new(path: PathBuf) -> Result<Self> {
if let Some(parent) = path.parent() {
std::fs::create_dir_all(parent)
.with_context(|| format!("creating log dir {}", parent.display()))?;
}
Ok(Self { path })
}
pub fn append_node(&self, node: &AstNode) -> Result<()> {
let mut file = OpenOptions::new()
.create(true)
.append(true)
.open(&self.path)
.with_context(|| format!("opening log {}", self.path.display()))?;
let line = serde_json::to_string(node)
.context("serializing node for log")?;
writeln!(file, "{}", line)
.context("writing to conversation log")?;
file.sync_all()
.context("syncing conversation log")?;
Ok(())
}
/// Read nodes from the tail of the log, newest first.
/// Caller decides when to stop (budget, count, etc).
pub fn read_tail(&self) -> Result<TailNodes> {
if !self.path.exists() {
anyhow::bail!("log does not exist");
}
let file = File::open(&self.path)
.with_context(|| format!("opening log {}", self.path.display()))?;
if file.metadata()?.len() == 0 {
anyhow::bail!("log is empty");
}
let mmap = unsafe { Mmap::map(&file)? };
Ok(TailNodes { _file: file, mmap })
}
pub fn path(&self) -> &Path {
&self.path
}
pub fn oldest_timestamp(&self) -> Option<chrono::DateTime<chrono::Utc>> {
// Read forward from the start to find first non-epoch timestamp
let file = File::open(&self.path).ok()?;
let mmap = unsafe { Mmap::map(&file).ok()? };
for line in mmap.split(|&b| b == b'\n') {
if line.is_empty() { continue; }
if let Ok(node) = serde_json::from_slice::<AstNode>(line) {
if let Some(leaf) = node.leaf() {
let ts = leaf.timestamp();
if ts != chrono::DateTime::UNIX_EPOCH {
return Some(ts);
}
}
}
}
None
}
}
/// Iterates over conversation log nodes newest-first, using mmap + backward scan.
pub struct TailNodes {
_file: File,
mmap: Mmap,
}
impl TailNodes {
pub fn iter(&self) -> impl Iterator<Item = AstNode> + '_ {
JsonlBackwardIter::new(&self.mmap)
.filter_map(|bytes| serde_json::from_slice::<AstNode>(bytes).ok())
}
}