M5c-2 plan: task 11, the admin socket's requests move out of serve.rs

Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
This commit is contained in:
2026-09-28 21:07:18 -07:00
co-authored by Claude Opus 5.5
parent 531a723e39
commit 5b39b7ffc3
4 changed files with 535 additions and 2 deletions
@@ -0,0 +1,75 @@
//! The owner's requests on `memoryd`'s admin socket: `reindex`, and the memory commands `log`,
//! `undo`, `pending`, `accept`, `reject` and `sync` (M5a spec, section 10; M5c spec, section 5).
//! Moved out of `serve.rs` as they were, to keep it under the line limit.
use bxcore::{MemoryAdminReply, MemoryCommand, Message, Reindexed, Timestamp};
use super::{Memoryd, error};
use crate::honcho::report;
use crate::index::{self, Index};
use crate::{journal, review};
impl Memoryd {
pub(super) fn reindex(&self) -> Message {
let mut state = self.lock();
let max_class = self.config.index.max_class;
match index::rebuild(
&self.config.index_path(),
max_class,
&self.config.sessions_dir(),
&self.store.path("notes.md"),
) {
Ok((counts, problems)) => {
for problem in problems {
(self.log)(format!("memoryd: not indexed: {problem}").as_str());
}
match Index::open(&self.config.index_path(), max_class) {
Ok(index) => {
state.index = index;
Message::Reindexed(Reindexed {
logs: counts.logs,
records: counts.records,
notes: counts.notes,
})
}
Err(e) => error(e),
}
}
Err(e) => error(e),
}
}
pub(super) fn admin(&self, command: MemoryCommand, arg: &str) -> Message {
let state = self.lock();
let now = Timestamp::now();
let result = match command {
MemoryCommand::Log => {
let n = if arg.is_empty() {
10
} else {
match arg.parse::<usize>() {
Ok(n) => n,
Err(_) => return error(format!("log takes a count; {arg:?} is not one")),
}
};
journal::log(&self.store, n)
}
MemoryCommand::Undo => journal::undo(&self.store, arg, now),
MemoryCommand::Pending => review::list(&self.store),
MemoryCommand::Accept => review::accept(&self.store, arg, now),
MemoryCommand::Reject => review::reject(&self.store, arg, now),
MemoryCommand::Sync => Ok(match &self.mirror {
Some(mirror) => mirror.report(),
None => report::NOT_CONFIGURED.to_string(),
}),
};
if result.is_ok() && matches!(command, MemoryCommand::Undo | MemoryCommand::Accept) {
self.mirror(None);
}
self.index_notes(&state);
match result {
Ok(text) => Message::MemoryAdminReply(MemoryAdminReply { text }),
Err(e) => error(e),
}
}
}