use std::collections::{BTreeMap, BTreeSet};
use std::io::{Read, Write};
use std::net::{TcpListener, TcpStream};
use std::sync::atomic::{AtomicBool, AtomicU64, Ordering};
use std::sync::{Arc, Mutex};
use std::time::{Duration, Instant};
use zeroize::Zeroizing;
use mnemo_engine::{
EssenceScope, KeywordIndex, Memory, Mnemo, MnemoError, Scope, MNEMO_META_INVALID_AT,
MNEMO_META_KEY, MNEMO_META_KIND, MNEMO_META_KIND_TURN, MNEMO_META_SUPERSEDED_BY,
MNEMO_META_THREAD,
};
use serde_json::{json, Map, Value};
const PAGE: &str = include_str!("inspect.html");
const ESSENCE_BUDGET: usize = 64 * 1024;
const MAX_PAGE: usize = 1000;
const MAX_HEAD: usize = 16 * 1024;
pub use mnemo_engine::keys::Secret;
pub fn read_key_file(path: &str) -> Result<Zeroizing<[u8; 32]>, String> {
mnemo_engine::keys::read_key_file(path).map_err(|e| e.to_string())
}
struct Version {
id: String,
content: String,
created_at: i64,
invalid_at: Option<i64>,
superseded_by: Option<String>,
}
fn chain_order(versions: Vec<Version>) -> Vec<Version> {
let targets: BTreeSet<String> = versions
.iter()
.filter_map(|v| v.superseded_by.clone())
.collect();
let mut by_id: BTreeMap<String, Version> =
versions.into_iter().map(|v| (v.id.clone(), v)).collect();
let mut starts: Vec<(i64, String)> = by_id
.values()
.filter(|v| !targets.contains(&v.id))
.map(|v| (v.created_at, v.id.clone()))
.collect();
starts.sort();
let mut out = Vec::new();
for (_, id) in starts {
let mut next = Some(id);
while let Some(v) = next.and_then(|id| by_id.remove(&id)) {
next = v.superseded_by.clone();
out.push(v);
}
}
let mut rest: Vec<Version> = by_id.into_values().collect();
rest.sort_by(|a, b| (a.created_at, &a.id).cmp(&(b.created_at, &b.id)));
out.extend(rest);
out
}
struct Thread {
id: String,
essence: String,
essence_items: usize,
turns: Vec<Value>,
}
pub struct Loaded {
about: Value,
memories: Vec<Value>,
contents: KeywordIndex,
agents: Vec<String>,
keys: BTreeMap<(String, String), Vec<Version>>,
threads: Vec<Thread>,
project_essence: Option<String>,
snapshots: Value,
findings: Vec<Value>,
tasks: Vec<Value>,
loaded_at: i64,
}
fn last_change(history: &[Value], created_at: i64) -> i64 {
history
.last()
.and_then(|h| h.get("at"))
.and_then(Value::as_i64)
.unwrap_or(created_at)
}
fn finding_json(m: &Memory, f: &mnemo_engine::Finding) -> Value {
json!({
"id": f.id,
"record_id": f.record_id.to_string(),
"batch": f.batch,
"category": f.category,
"priority": f.priority,
"status": f.status,
"title": f.title,
"body": f.body,
"refs": f.refs,
"author": f.author,
"triage": f.triage,
"triaged_by": f.triaged_by,
"verified_ref": f.verified_ref,
"verified_by": f.verified_by,
"private": mnemo_engine::visibility_of(m) == "private",
"created_at": f.created_at,
"updated_at": last_change(&f.history, f.created_at),
"history": f.history,
})
}
fn task_json(t: &mnemo_engine::Task) -> Value {
json!({
"slug": t.slug,
"record_id": t.id.to_string(),
"title": t.title,
"body": t.body,
"status": t.status,
"owner": t.owner,
"creator": t.creator,
"role": t.role,
"after": t.after,
"refs": t.refs,
"pre_approved": t.pre_approved,
"created_at": t.created_at,
"updated_at": last_change(&t.history, t.created_at),
"history": t.history,
})
}
const FINDING_STATUS_ORDER: [&str; 6] = [
"open",
"needs-user",
"accepted",
"deferred",
"rejected",
"verified",
];
const TASK_STATUS_ORDER: [&str; 5] = ["queued", "active", "blocked", "ready", "done"];
fn status_counts(items: &[Value], order: &[&str]) -> Value {
let mut counts: BTreeMap<String, usize> = BTreeMap::new();
for v in items {
*counts
.entry(v["status"].as_str().unwrap_or("").to_string())
.or_default() += 1;
}
let mut out: Vec<Value> = Vec::new();
for s in order {
if let Some(n) = counts.remove(*s) {
out.push(json!({ "status": s, "count": n }));
}
}
for (s, n) in counts {
out.push(json!({ "status": s, "count": n }));
}
Value::Array(out)
}
fn now() -> i64 {
std::time::SystemTime::now()
.duration_since(std::time::UNIX_EPOCH)
.map(|d| d.as_secs() as i64)
.unwrap_or(0)
}
fn open_error(e: MnemoError) -> String {
match e {
MnemoError::Locked { holder, .. } => match holder {
Some(h) => format!("a writer has this file open ({h}); try again later"),
None => "a writer has this file open; close it or try again".to_string(),
},
MnemoError::NeedsWriteOpen { .. } => "the file has a write to finish (a pending WAL or \
a torn header) and the inspector never writes; any writer's next call recovers it \
(a shared handle's next call, or any mnemo write command), then try again"
.to_string(),
other => other.to_string(),
}
}
fn meta_str<'a>(m: &'a Memory, k: &str) -> Option<&'a str> {
m.metadata.get(k).and_then(Value::as_str)
}
fn memory_json(m: &Memory) -> Value {
let mut r = Map::new();
r.insert("id".into(), json!(m.id.to_string()));
r.insert("type".into(), json!(m.memory_type.as_str()));
r.insert("agent".into(), json!(m.agent_id));
r.insert(
"scope".into(),
json!(match m.scope {
Scope::Private => "private",
Scope::Shared => "shared",
}),
);
for (field, k) in [
("key", MNEMO_META_KEY),
("thread", MNEMO_META_THREAD),
("kind", MNEMO_META_KIND),
("superseded_by", MNEMO_META_SUPERSEDED_BY),
] {
if let Some(v) = meta_str(m, k) {
r.insert(field.into(), json!(v));
}
}
if let Some(t) = m.metadata.get(MNEMO_META_INVALID_AT) {
r.insert("invalid_at".into(), t.clone());
}
if mnemo_engine::visibility_of(m) == "private" {
r.insert("private".into(), json!(true));
}
if let Some(s) = &m.session_id {
r.insert("session".into(), json!(s));
}
r.insert("created_at".into(), json!(m.created_at));
r.insert("accessed_at".into(), json!(m.accessed_at));
r.insert("access_count".into(), json!(m.access_count));
r.insert(
"importance".into(),
format!("{}", m.importance)
.parse::<f64>()
.map(|x| json!(x))
.unwrap_or(Value::Null),
);
if let Some(t) = m.ttl_secs {
r.insert("ttl_secs".into(), json!(t));
}
r.insert("metadata".into(), Value::Object(m.metadata.clone()));
r.insert("content".into(), json!(m.content));
Value::Object(r)
}
pub fn load(path: &str, secret: &Secret) -> Result<Loaded, String> {
let mut db = match secret {
Secret::Passphrase(p) => Mnemo::open_read_only_waiting(path, p, crate::wait_opts()),
Secret::Key(k) => Mnemo::open_read_only_waiting_with_key(path, k, crate::wait_opts()),
}
.map_err(open_error)?;
let e = |e: MnemoError| e.to_string();
let stats = db.stats().map_err(e)?;
let summary = db.stats_summary();
let identity = db.embedder_identity().map_err(e)?;
let briefing = db.about_in(mnemo_engine::VisibilitySet::ALL).map_err(e)?;
let manifest_embedder = briefing
.iter()
.find(|m| meta_str(m, "topic") == Some("manifest"))
.and_then(|m| m.metadata.get("embedder").cloned());
let onboarding: Vec<Value> = briefing
.iter()
.map(|m| {
json!({
"id": m.id.to_string(),
"topic": meta_str(m, "topic"),
"content": m.content,
})
})
.collect();
let cache = db.cache_stats(None);
let all = db.memories().map_err(e)?;
let mut thread_ids: BTreeSet<String> = BTreeSet::new();
let mut essence_counts: BTreeMap<String, usize> = BTreeMap::new();
let mut project_items = 0usize;
let mut keys: BTreeMap<(String, String), Vec<Version>> = BTreeMap::new();
let mut shown: Vec<&Memory> = Vec::new();
for m in &all {
let kind = meta_str(m, MNEMO_META_KIND);
if let Some(t) = meta_str(m, MNEMO_META_THREAD) {
thread_ids.insert(t.to_string());
if kind == Some("essence") {
*essence_counts.entry(t.to_string()).or_default() += 1;
}
}
if kind == Some("essence") && meta_str(m, MNEMO_META_THREAD).is_none() {
project_items += 1;
}
if kind == Some(MNEMO_META_KIND_TURN) {
continue;
}
if let Some(k) = meta_str(m, MNEMO_META_KEY) {
let owner = match m.scope {
Scope::Private => m.agent_id.clone(),
Scope::Shared => String::new(),
};
keys.entry((k.to_string(), owner))
.or_default()
.push(Version {
id: m.id.to_string(),
content: m.content.clone(),
created_at: m.created_at,
invalid_at: m
.metadata
.get(MNEMO_META_INVALID_AT)
.and_then(Value::as_i64),
superseded_by: meta_str(m, MNEMO_META_SUPERSEDED_BY).map(str::to_string),
});
}
shown.push(m);
}
shown.sort_by_key(|m| m.id);
let keys: BTreeMap<(String, String), Vec<Version>> = keys
.into_iter()
.map(|(k, vs)| (k, chain_order(vs)))
.collect();
let mut finding_rows: Vec<(mnemo_engine::Finding, &Memory)> = all
.iter()
.filter_map(|m| mnemo_engine::Finding::from_memory(m).map(|f| (f, m)))
.collect();
finding_rows.sort_by_key(|(f, _)| f.sort_key());
let findings: Vec<Value> = finding_rows
.iter()
.map(|(f, m)| finding_json(m, f))
.collect();
drop(finding_rows);
let tasks: Vec<Value> = all
.iter()
.filter_map(mnemo_engine::Task::from_memory)
.map(|t| task_json(&t))
.collect();
let contents = KeywordIndex::new(shown.iter().map(|m| m.content.as_str()));
let memories: Vec<Value> = shown.iter().map(|m| memory_json(m)).collect();
let agents: Vec<String> = shown
.iter()
.map(|m| m.agent_id.clone())
.collect::<BTreeSet<_>>()
.into_iter()
.collect();
drop(shown);
drop(all);
let mut threads = Vec::new();
for id in &thread_ids {
let essence = db
.essence(EssenceScope::Thread(id.clone()), ESSENCE_BUDGET)
.map_err(e)?;
let turns = db
.thread_export_turns(id)
.map_err(e)?
.iter()
.map(|t| t.to_json())
.collect();
threads.push(Thread {
id: id.clone(),
essence,
essence_items: essence_counts.get(id).copied().unwrap_or(0),
turns,
});
}
let project = db
.essence(EssenceScope::Project, ESSENCE_BUDGET)
.map_err(e)?;
let snaps: Vec<Value> = db
.snapshots()
.iter()
.map(|s| {
json!({
"txn_id": s.txn_id,
"created_at": s.created_at,
"memory_count": s.memory_count,
})
})
.collect();
drop(db);
let about = json!({
"path": path,
"format_version": summary.format_version,
"recall_mode": summary.recall_mode.as_str(),
"dimensions": summary.dimensions,
"memories": summary.memories,
"tombstoned": summary.deleted,
"file_bytes": summary.file_bytes,
"wal_pages": summary.wal_pages,
"has_index": summary.has_index,
"vector_column_bytes": summary.vector_column_bytes,
"created_at": stats.created_at,
"agents": stats.agents,
"embedder": identity.map(|i| json!({
"name": i.name,
"dimensions": i.dimensions,
"normalize": i.normalize,
"query_convention": i.query_convention,
})),
"keyword_tokenizer": stats.keyword_tokenizer.map(|k| k.as_str()),
"manifest_embedder": manifest_embedder,
"cache": { "entries": cache.entries, "bytes": cache.bytes },
"onboarding": onboarding,
"threads": thread_ids.len(),
"keys": keys.len(),
"findings": findings.len(),
"tasks": tasks.len(),
});
let snapshots = json!({
"retained": summary.snapshots_retained,
"oldest_restorable_at": summary.oldest_restorable_at,
"pinned_bytes": summary.snapshot_pinned_bytes,
"list": snaps,
});
Ok(Loaded {
about,
memories,
contents,
agents,
keys,
threads,
project_essence: (project_items > 0).then_some(project),
snapshots,
findings,
tasks,
loaded_at: now(),
})
}
pub struct State {
path: String,
secret: Secret,
loaded: Loaded,
}
impl State {
pub fn new(path: String, secret: Secret, loaded: Loaded) -> Self {
Self {
path,
secret,
loaded,
}
}
}
pub fn bind(port: Option<u16>) -> std::io::Result<TcpListener> {
TcpListener::bind(("127.0.0.1", port.unwrap_or(0)))
}
pub fn new_token() -> String {
Mnemo::generate_key()
.iter()
.map(|b| format!("{b:02x}"))
.collect()
}
pub struct Watch {
started: Instant,
last_secs: AtomicU64,
page_served: AtomicBool,
}
impl Watch {
pub fn new() -> Arc<Self> {
Arc::new(Self {
started: Instant::now(),
last_secs: AtomicU64::new(0),
page_served: AtomicBool::new(false),
})
}
fn touch(&self) {
self.last_secs
.store(self.started.elapsed().as_secs(), Ordering::Relaxed);
}
fn idle_for(&self) -> Duration {
let last = Duration::from_secs(self.last_secs.load(Ordering::Relaxed));
self.started.elapsed().saturating_sub(last)
}
}
pub fn parse_idle(s: &str) -> Result<Option<Duration>, String> {
let s = s.trim();
let (num, unit) = match s.char_indices().find(|(_, c)| !c.is_ascii_digit()) {
Some((i, _)) => s.split_at(i),
None => (s, "m"),
};
let n: u64 = num
.parse()
.map_err(|_| format!("--idle-timeout {s:?}: use a number with s, m or h (30m)"))?;
let per = match unit {
"s" => 1,
"m" => 60,
"h" => 3600,
_ => return Err(format!("--idle-timeout {s:?}: use s, m or h (30m)")),
};
let secs = n
.checked_mul(per)
.ok_or_else(|| format!("--idle-timeout {s:?} is too long (use 0 for never)"))?;
Ok((secs > 0).then(|| Duration::from_secs(secs)))
}
pub fn serve(
listener: TcpListener,
token: String,
code: String,
state: Arc<Mutex<State>>,
watch: Arc<Watch>,
idle: Option<Duration>,
) -> std::io::Result<()> {
let port = listener.local_addr()?.port();
let token = Arc::new(Zeroizing::new(token));
let code = Arc::new(Mutex::new(Some(Zeroizing::new(code))));
if let Some(idle) = idle {
let (state, watch) = (state.clone(), watch.clone());
std::thread::spawn(move || loop {
std::thread::sleep(Duration::from_millis(250));
if watch.idle_for() >= idle {
let mut st = state.lock().unwrap_or_else(|p| p.into_inner());
st.secret = Secret::Key(Zeroizing::new([0u8; 32]));
println!(
"stopping: no request for {}s (--idle-timeout)",
idle.as_secs()
);
let _ = std::io::Write::flush(&mut std::io::stdout());
std::process::exit(0);
}
});
}
for stream in listener.incoming() {
let Ok(stream) = stream else { continue };
let (token, code, state, watch) =
(token.clone(), code.clone(), state.clone(), watch.clone());
std::thread::spawn(move || {
let _ = handle(stream, &token, &code, port, &state, &watch);
});
}
Ok(())
}
struct Request {
method: String,
path: String,
query: BTreeMap<String, String>,
host: Option<String>,
hosts: usize,
}
fn percent_decode(s: &str) -> String {
fn hex(c: u8) -> Option<u8> {
(c as char).to_digit(16).map(|d| d as u8)
}
let b = s.as_bytes();
let mut out = Vec::with_capacity(b.len());
let mut i = 0;
while i < b.len() {
match b[i] {
b'+' => out.push(b' '),
b'%' if i + 2 < b.len() => match (hex(b[i + 1]), hex(b[i + 2])) {
(Some(h), Some(l)) => {
out.push(h * 16 + l);
i += 2;
}
_ => out.push(b'%'),
},
c => out.push(c),
}
i += 1;
}
String::from_utf8_lossy(&out).into_owned()
}
fn parse(head: &str) -> Option<Request> {
let mut lines = head.split("\r\n");
let mut first = lines.next()?.split(' ');
let method = first.next()?.to_string();
let target = first.next()?;
first.next()?.strip_prefix("HTTP/1.")?;
let (path, qs) = target.split_once('?').unwrap_or((target, ""));
let mut query = BTreeMap::new();
for pair in qs.split('&').filter(|p| !p.is_empty()) {
let (k, v) = pair.split_once('=').unwrap_or((pair, ""));
query.insert(percent_decode(k), percent_decode(v));
}
let mut host = None;
let mut hosts = 0;
for line in lines {
if let Some((k, v)) = line.split_once(':') {
if k.trim().eq_ignore_ascii_case("host") {
hosts += 1;
host = Some(v.trim().to_ascii_lowercase());
}
}
}
Some(Request {
method,
path: path.to_string(),
query,
host,
hosts,
})
}
fn same(a: &str, b: &str) -> bool {
a.len() == b.len()
&& a.bytes()
.zip(b.bytes())
.fold(0u8, |acc, (x, y)| acc | (x ^ y))
== 0
}
fn respond(
s: &mut TcpStream,
status: &str,
ctype: &str,
body: &[u8],
extra: &str,
) -> std::io::Result<()> {
let head = format!(
"HTTP/1.1 {status}\r\nContent-Type: {ctype}\r\nContent-Length: {}\r\n\
Cache-Control: no-store\r\nX-Content-Type-Options: nosniff\r\n\
Referrer-Policy: no-referrer\r\nX-Frame-Options: DENY\r\n{extra}Connection: close\r\n\r\n",
body.len()
);
s.write_all(head.as_bytes())?;
s.write_all(body)?;
s.flush()
}
fn respond_json(s: &mut TcpStream, status: &str, v: &Value) -> std::io::Result<()> {
respond(
s,
status,
"application/json; charset=utf-8",
v.to_string().as_bytes(),
"",
)
}
fn handle(
mut s: TcpStream,
token: &str,
code: &Mutex<Option<Zeroizing<String>>>,
port: u16,
state: &Mutex<State>,
watch: &Watch,
) -> std::io::Result<()> {
s.set_read_timeout(Some(Duration::from_secs(10)))?;
s.set_write_timeout(Some(Duration::from_secs(10)))?;
let mut buf = Vec::new();
let mut chunk = [0u8; 2048];
while !buf.windows(4).any(|w| w == b"\r\n\r\n") {
let n = s.read(&mut chunk)?;
if n == 0 {
return Ok(());
}
buf.extend_from_slice(&chunk[..n]);
if buf.len() > MAX_HEAD {
return respond(
&mut s,
"431 Request Header Fields Too Large",
"text/plain",
b"too large",
"",
);
}
}
let end = buf
.windows(4)
.position(|w| w == b"\r\n\r\n")
.unwrap_or(buf.len());
let head = String::from_utf8_lossy(&buf[..end]).into_owned();
let Some(req) = parse(&head) else {
return respond(&mut s, "400 Bad Request", "text/plain", b"bad request", "");
};
if req.hosts != 1 {
return respond(
&mut s,
"400 Bad Request",
"text/plain",
b"exactly one Host header",
"",
);
}
let ok_host = req
.host
.as_deref()
.is_some_and(|h| h == format!("127.0.0.1:{port}") || h == format!("localhost:{port}"));
if !ok_host {
return respond(&mut s, "403 Forbidden", "text/plain", b"wrong Host", "");
}
if req.method != "GET" {
return respond(
&mut s,
"405 Method Not Allowed",
"text/plain",
b"only GET is served",
"Allow: GET\r\n",
);
}
if req.path == "/" {
return respond(
&mut s,
"200 OK",
"text/html; charset=utf-8",
PAGE.as_bytes(),
"Content-Security-Policy: default-src 'none'; script-src 'unsafe-inline'; \
style-src 'unsafe-inline'; connect-src 'self'; base-uri 'none'; \
form-action 'none'; frame-ancestors 'none'\r\n",
);
}
if req.path == "/api/session" {
let mut slot = code.lock().unwrap_or_else(|p| p.into_inner());
let ok = req
.query
.get("c")
.is_some_and(|c| slot.as_deref().is_some_and(|k| same(c, k)));
if !ok {
return respond(
&mut s,
"403 Forbidden",
"text/plain",
b"this link has been used, or is wrong",
"",
);
}
*slot = None;
drop(slot);
watch.page_served.store(true, Ordering::Relaxed);
watch.touch();
return respond_json(&mut s, "200 OK", &json!({ "token": token }));
}
if !req.query.get("t").is_some_and(|t| same(t, token)) {
return respond(
&mut s,
"403 Forbidden",
"text/plain",
b"missing or wrong token",
"",
);
}
watch.touch();
let (status, body) = {
let mut st = state.lock().unwrap_or_else(|p| p.into_inner());
if req.path == "/api/reload" {
match load(&st.path, &st.secret) {
Ok(l) => {
st.loaded = l;
("200 OK", about(&st.loaded))
}
Err(e) => ("409 Conflict", json!({ "error": e })),
}
} else {
match route(&st.loaded, &req) {
Ok(Some(v)) => ("200 OK", v),
Ok(None) => ("404 Not Found", json!({ "error": "not found" })),
Err(e) => ("400 Bad Request", json!({ "error": e })),
}
}
};
respond_json(&mut s, status, &body)
}
fn about(l: &Loaded) -> Value {
let mut a = l.about.clone();
a["loaded_at"] = json!(l.loaded_at);
a
}
fn date_range(req: &Request) -> Result<(Option<i64>, Option<i64>), String> {
let get = |k: &str| -> Result<Option<i64>, String> {
match req.query.get(k).map(String::as_str) {
None | Some("") => Ok(None),
Some(v) => v
.parse::<i64>()
.map(Some)
.map_err(|_| format!("{k}: {v:?} isn't a time in unix seconds")),
}
};
Ok((get("since")?, get("until")?))
}
fn in_range(t: &Value, (since, until): (Option<i64>, Option<i64>)) -> bool {
let Some(t) = t.as_i64() else {
return since.is_none() && until.is_none();
};
since.is_none_or(|s| t >= s) && until.is_none_or(|u| t <= u)
}
fn route(l: &Loaded, req: &Request) -> Result<Option<Value>, String> {
let range = date_range(req)?;
match req.query.get("date").map(String::as_str) {
None | Some("") | Some("created") | Some("updated") => {}
Some(v) => return Err(format!("date: {v:?} isn't created or updated")),
}
Ok(route_in(l, req, range))
}
fn route_in(l: &Loaded, req: &Request, range: (Option<i64>, Option<i64>)) -> Option<Value> {
let q = |k: &str| {
req.query
.get(k)
.map(String::as_str)
.filter(|v| !v.is_empty())
};
match req.path.as_str() {
"/api/about" => Some(about(l)),
"/api/memories" => {
let order: Vec<(usize, Option<f32>)> = match q("q") {
Some(text) => l
.contents
.search(text, l.memories.len())
.into_iter()
.map(|(i, s)| (i, Some(s)))
.collect(),
None => (0..l.memories.len()).map(|i| (i, None)).collect(),
};
let types: Option<Vec<&str>> = q("type").map(|t| t.split(',').collect());
let keep = |m: &Value| {
in_range(&m["created_at"], range)
&& types
.as_ref()
.is_none_or(|ts| ts.contains(&m["type"].as_str().unwrap_or("")))
&& q("agent").is_none_or(|a| m["agent"] == a)
&& q("thread").is_none_or(|t| m["thread"] == t)
&& match q("keyed") {
Some("yes") => m.get("key").is_some(),
Some("no") => m.get("key").is_none(),
_ => true,
}
};
let matches: Vec<(usize, Option<f32>)> = order
.into_iter()
.filter(|(i, _)| keep(&l.memories[*i]))
.collect();
let limit: usize = q("limit")
.and_then(|v| v.parse().ok())
.unwrap_or(200)
.min(MAX_PAGE);
let offset: usize = q("offset").and_then(|v| v.parse().ok()).unwrap_or(0);
let total = matches.len();
let page: Vec<Value> = matches
.into_iter()
.skip(offset)
.take(limit)
.map(|(i, score)| {
let mut m = l.memories[i].clone();
if let Some(s) = score {
m["score"] = json!(s);
}
m
})
.collect();
let agents = &l.agents;
Some(json!({ "total": total, "offset": offset, "memories": page, "agents": agents }))
}
"/api/keys" => Some(Value::Array(
l.keys
.iter()
.map(|((key, owner), vs)| {
json!({
"key": key,
"owner": owner,
"versions": vs.len(),
"current": vs.last().map(|v| v.content.as_str()),
"updated_at": vs.last().map(|v| v.created_at),
})
})
.collect(),
)),
"/api/history" => {
let key = q("key")?;
let owner = req.query.get("owner").cloned().unwrap_or_default();
let vs = l.keys.get(&(key.to_string(), owner.clone()))?;
Some(json!({
"key": key,
"owner": owner,
"versions": vs.iter().map(|v| json!({
"id": v.id,
"content": v.content,
"created_at": v.created_at,
"invalid_at": v.invalid_at,
"current": v.invalid_at.is_none(),
})).collect::<Vec<_>>(),
}))
}
"/api/threads" => Some(json!({
"project_essence": l.project_essence,
"threads": l.threads.iter().map(|t| json!({
"id": t.id,
"turns": t.turns.len(),
"essence_items": t.essence_items,
})).collect::<Vec<_>>(),
})),
"/api/thread" => {
let id = q("id")?;
let t = l.threads.iter().find(|t| t.id == id)?;
let mut v = json!({
"id": t.id,
"essence": t.essence,
"essence_items": t.essence_items,
"turn_count": t.turns.len(),
});
if q("turns") == Some("1") {
v["turns"] = Value::Array(t.turns.clone());
}
Some(v)
}
"/api/snapshots" => Some(l.snapshots.clone()),
"/api/findings" => {
let dated: Vec<Value> = l
.findings
.iter()
.filter(|f| in_range(&f["created_at"], range))
.cloned()
.collect();
let list: Vec<Value> = dated
.iter()
.filter(|f| {
["status", "category", "priority"]
.iter()
.all(|k| q(k).is_none_or(|want| f[*k] == want))
})
.cloned()
.collect();
Some(json!({
"total": dated.len(),
"counts": status_counts(&dated, &FINDING_STATUS_ORDER),
"findings": list,
"statuses": FINDING_STATUS_ORDER,
"categories": mnemo_engine::FINDING_CATEGORIES,
"priorities": mnemo_engine::FINDING_PRIORITIES,
}))
}
"/api/tasks" => {
let field = if q("date") == Some("updated") {
"updated_at"
} else {
"created_at"
};
let dated: Vec<Value> = l
.tasks
.iter()
.filter(|t| in_range(&t[field], range))
.cloned()
.collect();
let list: Vec<Value> = dated
.iter()
.filter(|t| q("status").is_none_or(|want| t["status"] == want))
.cloned()
.collect();
Some(json!({
"total": dated.len(),
"counts": status_counts(&dated, &TASK_STATUS_ORDER),
"tasks": list,
"statuses": TASK_STATUS_ORDER,
}))
}
_ => None,
}
}
pub fn write_redirect(url: &str) -> std::io::Result<(std::path::PathBuf, std::path::PathBuf)> {
let tag: String = Mnemo::generate_key()[..8]
.iter()
.map(|b| format!("{b:02x}"))
.collect();
let dir = std::env::temp_dir().join(format!("mnemo-inspect-{}-{tag}", std::process::id()));
#[cfg(unix)]
let builder = {
use std::os::unix::fs::DirBuilderExt;
let mut b = std::fs::DirBuilder::new();
b.mode(0o700);
b
};
#[cfg(not(unix))]
let builder = std::fs::DirBuilder::new();
builder.create(&dir)?;
let page = dir.join("open.html");
let mut opts = std::fs::OpenOptions::new();
opts.write(true).create_new(true);
#[cfg(unix)]
{
use std::os::unix::fs::OpenOptionsExt;
opts.mode(0o600);
}
let mut f = opts.open(&page)?;
write!(
f,
"<!doctype html><meta charset=\"utf-8\"><meta name=\"referrer\" content=\"no-referrer\">\
<meta http-equiv=\"refresh\" content=\"0;url={url}\"><title>mnemo inspect</title>\
<a href=\"{url}\">Open the inspector</a>"
)?;
f.sync_all()?;
Ok((dir, page))
}
pub fn open_browser(url: &str, watch: Arc<Watch>) -> std::io::Result<()> {
let (dir, page) = write_redirect(url)?;
#[cfg(target_os = "macos")]
let mut cmd = std::process::Command::new("open");
#[cfg(target_os = "windows")]
let mut cmd = {
let mut c = std::process::Command::new("cmd");
c.args(["/C", "start", ""]);
c
};
#[cfg(not(any(target_os = "macos", target_os = "windows")))]
let mut cmd = std::process::Command::new("xdg-open");
let launched = cmd
.arg(&page)
.stdout(std::process::Stdio::null())
.stderr(std::process::Stdio::null())
.spawn()
.map(|_| ());
std::thread::spawn(move || {
let start = Instant::now();
while !watch.page_served.load(Ordering::Relaxed)
&& start.elapsed() < Duration::from_secs(60)
{
std::thread::sleep(Duration::from_millis(200));
}
let _ = std::fs::remove_dir_all(&dir);
});
launched
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn requests_parse_and_decode() {
let r =
parse("GET /api/memories?q=oat+milk&key=a%3Ab&t=x HTTP/1.1\r\nHost: 127.0.0.1:9\r\n")
.unwrap();
assert_eq!(
(r.method.as_str(), r.path.as_str()),
("GET", "/api/memories")
);
assert_eq!(r.query["q"], "oat milk");
assert_eq!(r.query["key"], "a:b");
assert_eq!(r.host.as_deref(), Some("127.0.0.1:9"));
assert_eq!(percent_decode("100%"), "100%");
assert_eq!(percent_decode("%zz"), "%zz");
assert!(parse("garbage").is_none());
}
#[test]
fn tokens_compare_in_full() {
assert!(same("abc", "abc"));
assert!(!same("abc", "abd") && !same("abc", "ab"));
assert_eq!(new_token().len(), 64);
assert_ne!(new_token(), new_token());
}
#[test]
fn chains_follow_the_links_not_the_ids() {
let v = |id: &str, next: Option<&str>, at: i64| Version {
id: id.into(),
content: id.into(),
created_at: at,
invalid_at: next.map(|_| at),
superseded_by: next.map(str::to_string),
};
let order: Vec<String> = chain_order(vec![
v("m", None, 5),
v("a", Some("m"), 5),
v("z", Some("a"), 5),
])
.into_iter()
.map(|x| x.id)
.collect();
assert_eq!(order, ["z", "a", "m"]);
}
#[test]
fn the_redirect_page_is_private_and_points_at_the_url() {
let url = "http://127.0.0.1:9/?c=abc123";
let (dir, page) = write_redirect(url).unwrap();
let body = std::fs::read_to_string(&page).unwrap();
assert!(
body.contains("content=\"0;url=http://127.0.0.1:9/?c=abc123\""),
"{body}"
);
#[cfg(unix)]
{
use std::os::unix::fs::PermissionsExt;
let mode =
|p: &std::path::Path| std::fs::metadata(p).unwrap().permissions().mode() & 0o777;
assert_eq!(mode(&dir), 0o700);
assert_eq!(mode(&page), 0o600);
}
std::fs::remove_dir_all(&dir).unwrap();
}
#[test]
fn idle_timeouts_parse() {
assert_eq!(parse_idle("30m").unwrap(), Some(Duration::from_secs(1800)));
assert_eq!(parse_idle("90s").unwrap(), Some(Duration::from_secs(90)));
assert_eq!(parse_idle("1h").unwrap(), Some(Duration::from_secs(3600)));
assert_eq!(parse_idle("45").unwrap(), Some(Duration::from_secs(2700)));
assert_eq!(parse_idle("0").unwrap(), None);
assert!(parse_idle("soon").is_err() && parse_idle("5d").is_err());
let too_long = parse_idle("1152921504606846976h").unwrap_err();
assert!(too_long.contains("too long"), "{too_long}");
assert!(parse_idle(&format!("{}m", u64::MAX / 60 + 1)).is_err());
assert!(parse_idle(&format!("{}s", u64::MAX)).unwrap().is_some());
}
#[test]
fn it_binds_loopback_only() {
let l = bind(None).unwrap();
assert!(l.local_addr().unwrap().ip().is_loopback());
}
}