memoryd: the admin socket's requests move to serve/admin.rs
Co-Authored-By: ornith-1.5-35b-a3b <agent@wntrmute.dev>
This commit is contained in:
@@ -8,9 +8,9 @@ use std::sync::{Arc, Mutex, MutexGuard};
|
||||
use std::time::Duration;
|
||||
|
||||
use bxcore::{
|
||||
DataClass, Empty, Envelope, ErrorCode, MemoryAdminReply, MemoryArgs, MemoryBlockReply,
|
||||
MemoryCall, MemoryCommand, Message, PROTOCOL_VERSION, RecallArgs, Reindexed, SessionId,
|
||||
SessionUpdated, Timestamp, ToolResponse, WireError, read_frame, write_frame,
|
||||
DataClass, Empty, Envelope, ErrorCode, MemoryArgs, MemoryBlockReply, MemoryCall, Message,
|
||||
PROTOCOL_VERSION, RecallArgs, SessionId, SessionUpdated, Timestamp, ToolResponse, WireError,
|
||||
read_frame, write_frame,
|
||||
};
|
||||
|
||||
use crate::config::Config;
|
||||
@@ -18,7 +18,6 @@ use crate::edit::{self, Context};
|
||||
use crate::files::Store;
|
||||
use crate::honcho::ledger::{self, LedgerError};
|
||||
use crate::honcho::mirror::Mirror;
|
||||
use crate::honcho::report;
|
||||
use crate::honcho::search;
|
||||
use crate::honcho::state;
|
||||
use crate::honcho::turns::TurnSync;
|
||||
@@ -27,6 +26,8 @@ use crate::index::{self, Index};
|
||||
use crate::recall::Recalled;
|
||||
use crate::{journal, recall, review};
|
||||
|
||||
mod admin;
|
||||
|
||||
/// How long a peer may take to send its request.
|
||||
pub const READ_WAIT: Duration = Duration::from_secs(10);
|
||||
|
||||
@@ -429,67 +430,4 @@ impl Memoryd {
|
||||
(self.log)(format!("memoryd: notes not indexed: {e}").as_str());
|
||||
}
|
||||
}
|
||||
|
||||
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),
|
||||
}
|
||||
}
|
||||
|
||||
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),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -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),
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user