use crate::{clients, daemon, doctor, init, launch, mcp, persist, proxy, serve, tools};
use std::io::{IsTerminal, Read, Write};
use std::process::ExitCode;
use std::sync::Arc;
use clap::Parser;
use memfork_core::Db;
use serde_json::json;
use crate::cli::{split_line, Cli, Command, PersistArgs, ScriptLine};
use crate::exec::{execute, ExecError, Outcome};
pub fn from_env() -> ExitCode {
run(Cli::parse())
}
pub fn from_argv(argv: Vec<String>) -> i32 {
match Cli::try_parse_from(argv) {
Ok(cli) => {
if run(cli) == ExitCode::SUCCESS {
0
} else {
1
}
}
Err(e) => {
let _ = e.print();
i32::from(e.use_stderr())
}
}
}
pub fn run(cli: Cli) -> ExitCode {
let result = match &cli.command {
Command::Mcp { persist } => run_mcp(persist),
Command::CrashWriter {
persist,
progress,
limit,
} => run_crash_writer(persist, progress, *limit),
Command::Serve { persist, port } => run_serve(persist, *port),
Command::Stop { data_dir } => run_stop(data_dir.as_deref()),
command => {
let mut stdout = std::io::stdout().lock();
match command {
Command::Run { script } => {
run_script(&mut stdout, script, &cli.global.branch, cli.global.json)
}
Command::Tools { format } => run_tools(&mut stdout, format),
Command::Call { tool, arguments } => run_call(&mut stdout, tool, arguments),
Command::Init {
dry_run,
client,
scope,
} => run_init(
&mut stdout,
*dry_run,
client.as_deref(),
scope,
cli.global.json,
),
Command::Doctor => run_doctor(&mut stdout, cli.global.json),
command => {
let db = Db::new();
execute(&db, &cli.global.branch, command).and_then(|outcome| {
emit(&mut stdout, &outcome, cli.global.json).map_err(io_err)
})
}
}
}
};
match result {
Ok(()) => ExitCode::SUCCESS,
Err(e) => {
let mut stderr = std::io::stderr().lock();
if cli.global.json {
let _ = writeln!(stderr, "{}", json!({"error": e.to_string()}));
} else {
let _ = writeln!(stderr, "memfork: {e}");
}
ExitCode::FAILURE
}
}
}
fn io_err(e: std::io::Error) -> ExecError {
ExecError::Usage(format!("cannot write output: {e}"))
}
fn emit(out: &mut impl Write, outcome: &Outcome, as_json: bool) -> std::io::Result<()> {
if as_json {
writeln!(out, "{}", outcome.json)
} else {
for line in &outcome.text {
writeln!(out, "{line}")?;
}
Ok(())
}
}
fn run_script(
out: &mut impl Write,
script: &str,
default_branch: &str,
as_json: bool,
) -> Result<(), ExecError> {
let source = read_script(script)?;
let db = Db::new();
let mut results = Vec::new();
for (n, raw) in source.lines().enumerate() {
let line_no = n + 1;
let line = strip_comment(raw);
if line.trim().is_empty() {
continue;
}
let args =
split_line(line).map_err(|e| ExecError::Usage(format!("line {line_no}: {e}")))?;
if args.is_empty() {
continue;
}
let parsed = ScriptLine::try_parse_from(&args).map_err(|e| {
ExecError::Usage(format!("line {line_no}: {}", first_line(&e.to_string())))
})?;
if !parsed.command.allowed_in_script() {
return Err(ExecError::Usage(format!(
"line {line_no}: `{}` cannot be used inside a script",
parsed.command.name()
)));
}
let branch = parsed.global.branch.as_deref().unwrap_or(default_branch);
let outcome = execute(&db, branch, &parsed.command)
.map_err(|e| ExecError::Usage(format!("line {line_no}: {e}")))?;
if as_json {
results.push(outcome.json);
} else {
for l in &outcome.text {
writeln!(out, "{l}").map_err(io_err)?;
}
}
}
if as_json {
writeln!(out, "{}", json!(results)).map_err(io_err)?;
}
Ok(())
}
fn open_database(
args: &PersistArgs,
) -> Result<(Db, Option<Arc<persist::Store>>, String), ExecError> {
if args.ephemeral {
let db = Db::new();
apply_eviction(&db, args);
return Ok((db, None, "in memory only; nothing will be kept".to_owned()));
}
let dir = match &args.data_dir {
Some(path) => persist::DataDir {
path: std::path::PathBuf::from(path),
source: persist::Source::Environment,
},
None => persist::datadir::here().map_err(|e| ExecError::Usage(e.to_string()))?,
};
let fsync = persist::FsyncPolicy::parse(&args.fsync)
.ok_or_else(|| ExecError::Usage(format!("unknown fsync policy `{}`", args.fsync)))?;
let options = persist::Options {
fsync,
retention: args.retention,
};
let (db, store, recovery) =
persist::Store::open(&dir.path, options).map_err(|e| ExecError::Usage(e.to_string()))?;
apply_eviction(&db, args);
let mut note = format!("data directory {}", dir.path.display());
if !recovery.is_empty() {
note.push_str(&format!(
"; recovered {} change(s) from the snapshot and {} from the log",
recovery.from_snapshot, recovery.from_wal
));
}
if let Some(reason) = &recovery.torn_tail {
note.push_str(&format!(
"; discarded an incomplete final record ({reason})"
));
}
Ok((db, Some(store), note))
}
fn apply_eviction(db: &Db, args: &PersistArgs) {
if let Some(budget) = args.memory_budget {
db.set_eviction(Some(
memfork_core::EvictionConfig::with_budget(budget).half_life(args.half_life),
));
}
}
fn run_crash_writer(args: &PersistArgs, progress: &str, limit: u64) -> Result<(), ExecError> {
use std::io::Write as _;
let (db, store, _) = open_database(args)?;
let mut log = std::fs::OpenOptions::new()
.create(true)
.append(true)
.open(progress)
.map_err(|e| ExecError::Usage(format!("cannot open {progress}: {e}")))?;
for i in 0..limit {
let id = db
.put(
"main",
&format!("key:{i:08}"),
memfork_core::Value::new(format!("value {i}")),
)
.map_err(|e| ExecError::Usage(e.to_string()))?;
if let Some(store) = &store {
store.flush().map_err(|e| ExecError::Usage(e.to_string()))?;
}
writeln!(log, "{id}")
.map_err(|e| ExecError::Usage(format!("cannot write progress: {e}")))?;
log.flush()
.map_err(|e| ExecError::Usage(format!("cannot flush progress: {e}")))?;
}
Ok(())
}
fn resolve_dir(args: &PersistArgs) -> Result<std::path::PathBuf, ExecError> {
match &args.data_dir {
Some(path) => Ok(std::path::PathBuf::from(path)),
None => persist::datadir::here()
.map(|d| d.path)
.map_err(|e| ExecError::Usage(e.to_string())),
}
}
fn run_serve(args: &PersistArgs, port: u16) -> Result<(), ExecError> {
if args.ephemeral {
return Err(ExecError::Usage(
"`memfork serve --ephemeral` would be a daemon with nothing to share. Drop --ephemeral, or use `memfork mcp --ephemeral` for a private, in-memory session."
.to_owned(),
));
}
let (db, store, note) = open_database(args)?;
let Some(store) = store else {
return Err(ExecError::Usage(
"the daemon needs a data directory".to_owned(),
));
};
eprintln!("memfork: {note}");
let runtime = tokio::runtime::Builder::new_multi_thread()
.enable_all()
.build()
.map_err(|e| ExecError::Usage(format!("cannot start the async runtime: {e}")))?;
let stopped = runtime
.block_on(serve::run(
db,
store,
serve::ServeOptions {
port,
idle_seconds: args.idle_timeout,
},
))
.map_err(ExecError::Usage)?;
eprintln!("memfork: daemon stopped ({stopped:?})");
Ok(())
}
fn run_stop(data_dir: Option<&str>) -> Result<(), ExecError> {
let dir = match data_dir {
Some(path) => std::path::PathBuf::from(path),
None => persist::datadir::here()
.map(|d| d.path)
.map_err(|e| ExecError::Usage(e.to_string()))?,
};
let said = daemon::stop(&dir).map_err(ExecError::Usage)?;
let mut stdout = std::io::stdout().lock();
writeln!(stdout, "{said}").map_err(io_err)
}
fn run_mcp(args: &PersistArgs) -> Result<(), ExecError> {
let _ = tracing_subscriber::fmt()
.with_writer(std::io::stderr)
.with_env_filter(
tracing_subscriber::EnvFilter::try_from_default_env()
.unwrap_or_else(|_| tracing_subscriber::EnvFilter::new("off")),
)
.try_init();
let runtime = tokio::runtime::Builder::new_multi_thread()
.enable_all()
.build()
.map_err(|e| ExecError::Usage(format!("cannot start the async runtime: {e}")))?;
if args.ephemeral {
let (db, _, note) = open_database(args)?;
eprintln!("memfork: {note}");
let session = Arc::new(tools::dispatch::Session::new(db));
return runtime
.block_on(mcp::serve_stdio(session))
.map_err(ExecError::Usage);
}
let dir = resolve_dir(args)?;
let launch = launch::resolve();
let idle = args.idle_timeout;
let connect = {
let dir = dir.clone();
let launch = launch.clone();
move || {
let dir = dir.clone();
let launch = launch.clone();
Box::pin(async move {
let endpoint = tokio::task::spawn_blocking(move || {
daemon::ensure(&dir, &launch, idle).map_err(|e| e.to_string())
})
.await
.map_err(|e| format!("the daemon lookup task failed: {e}"))??;
proxy::Upstream::connect(&endpoint)
.await
.map_err(|e| e.to_string())
}) as futures::future::BoxFuture<'static, Result<proxy::Upstream, String>>
}
};
daemon::usable(&dir).map_err(|e| ExecError::Usage(e.to_string()))?;
eprintln!(
"memfork: sharing the memory in {}; the daemon starts when a tool is called",
dir.display()
);
runtime
.block_on(proxy::serve_stdio(proxy::Proxy::new(Arc::new(connect))))
.map_err(ExecError::Usage)
}
fn run_tools(out: &mut impl Write, format: &str) -> Result<(), ExecError> {
let format = tools::vendor::Format::parse(format).ok_or_else(|| {
ExecError::Usage(format!(
"unknown format `{format}`; expected openai, anthropic or gemini"
))
})?;
let rendered = tools::vendor::render(&tools::all(), format);
let text = serde_json::to_string_pretty(&rendered)
.map_err(|e| ExecError::Usage(format!("cannot render the tool definitions: {e}")))?;
writeln!(out, "{text}").map_err(io_err)
}
fn run_call(out: &mut impl Write, tool: &str, arguments: &str) -> Result<(), ExecError> {
let parsed: serde_json::Value = serde_json::from_str(arguments)
.map_err(|e| ExecError::Usage(format!("`arguments` is not valid JSON: {e}")))?;
let args = match parsed {
serde_json::Value::Object(map) => map,
other => {
return Err(ExecError::Usage(format!(
"`arguments` must be a JSON object, got {other}"
)))
}
};
let session = tools::dispatch::Session::new(Db::new());
let result = session
.call(tool, &args)
.map_err(|e| ExecError::Usage(e.to_string()))?;
let text = serde_json::to_string_pretty(&result)
.map_err(|e| ExecError::Usage(format!("cannot render the result: {e}")))?;
writeln!(out, "{text}").map_err(io_err)
}
fn run_init(
out: &mut impl Write,
dry_run: bool,
client: Option<&str>,
scope: &str,
as_json: bool,
) -> Result<(), ExecError> {
let scope = clients::Scope::parse(scope)
.ok_or_else(|| ExecError::Usage(format!("unknown scope `{scope}`")))?;
let launch = launch::resolve();
let plans = init::plan(scope, client, None, &launch).map_err(ExecError::Usage)?;
let mut changed = 0usize;
let mut failures = Vec::new();
let mut records = Vec::new();
for plan in &plans {
let (verb, detail) = describe(&plan.action, dry_run);
let mut applied_error = None;
let mut backup = None;
if !dry_run && plan.action.changes_anything() {
match init::apply(plan) {
Ok(b) => {
changed += 1;
backup = b;
}
Err(e) => {
failures.push(format!("{}: {e}", plan.display));
applied_error = Some(e);
}
}
}
if as_json {
records.push(json!({
"id": plan.id,
"client": plan.display,
"scope": plan.scope.as_str(),
"method": method_name(&plan.action),
"action": verb,
"detail": detail,
"note": plan.note,
"backup": backup,
"error": applied_error,
}));
} else {
let done = if dry_run || !plan.action.changes_anything() {
verb
} else if applied_error.is_some() {
"failed"
} else {
past_tense(verb)
};
writeln!(out, " {:<14} {:<18} {detail}", plan.display, done).map_err(io_err)?;
if let Some(note) = &plan.note {
writeln!(out, " note: {note}").map_err(io_err)?;
}
if let Some(e) = &applied_error {
writeln!(out, " error: {e}").map_err(io_err)?;
}
if let Some(b) = &backup {
writeln!(out, " backup: {b}").map_err(io_err)?;
}
if dry_run {
if let init::Action::WriteFile(edit) = &plan.action {
write!(out, "{}", edit.diff()).map_err(io_err)?;
}
}
}
}
if as_json {
let doc = json!({
"scope": scope.as_str(),
"dry_run": dry_run,
"command": launch.program.display().to_string(),
"command_line": launch.display(clients::SERVER_ARGS),
"command_argv": launch.argv(clients::SERVER_ARGS),
"clients": records,
});
writeln!(out, "{doc}").map_err(io_err)?;
} else if dry_run {
writeln!(out, "\nNothing was run or written: this was a dry run.").map_err(io_err)?;
} else if changed == 0 {
writeln!(out, "\nNothing to change.").map_err(io_err)?;
} else {
writeln!(
out,
"\nRegistered with {changed} client(s). Restart them to pick up the tools."
)
.map_err(io_err)?;
}
if failures.is_empty() {
Ok(())
} else {
Err(ExecError::Usage(failures.join("; ")))
}
}
fn method_name(action: &init::Action) -> &'static str {
match action {
init::Action::RunCommand { .. } | init::Action::ReplaceCommand { .. } => "client command",
init::Action::WriteFile(_) | init::Action::AlreadyRegistered { .. } => "config file",
init::Action::StaleElsewhere { .. }
| init::Action::NotInstalled
| init::Action::Refused(_) => "none",
}
}
fn describe(action: &init::Action, dry_run: bool) -> (&'static str, String) {
match action {
init::Action::RunCommand { binary, args, .. } => (
if dry_run { "would run" } else { "run" },
format!("{binary} {}", args.join(" ")),
),
init::Action::ReplaceCommand {
binary, add, was, ..
} => (
if dry_run { "would repoint" } else { "repoint" },
format!(
"was {}; now {binary} {}",
was.as_deref().unwrap_or("another MemFork"),
add.join(" ")
),
),
init::Action::StaleElsewhere { was, fix } => (
"points elsewhere",
format!("at {}; {fix}", was.as_deref().unwrap_or("another MemFork")),
),
init::Action::WriteFile(edit) => (
match (dry_run, edit.change) {
(true, clients::edit::Change::Update) => "would update",
(true, _) => "would add",
(false, clients::edit::Change::Update) => "update",
(false, _) => "add",
},
edit.path.display().to_string(),
),
init::Action::AlreadyRegistered { checked } => (
"already registered",
match checked {
clients::Checked::Command(cmd) => format!("`{cmd}` says so"),
clients::Checked::File(path) => path.display().to_string(),
clients::Checked::Nothing => String::new(),
},
),
init::Action::NotInstalled => ("not installed", String::new()),
init::Action::Refused(reason) => ("skipped", reason.clone()),
}
}
fn past_tense(verb: &str) -> &'static str {
match verb {
"run" => "ran",
"add" => "added",
"update" => "updated",
"repoint" => "repointed",
_ => "done",
}
}
fn run_doctor(out: &mut impl Write, as_json: bool) -> Result<(), ExecError> {
if as_json {
writeln!(out, "{}", doctor::json()).map_err(io_err)
} else {
write!(out, "{}", doctor::text()).map_err(io_err)
}
}
fn read_script(script: &str) -> Result<String, ExecError> {
if script == "-" {
if std::io::stdin().is_terminal() {
return Err(ExecError::Usage(
"reading a script from a terminal: pipe one in, or pass a file path".to_owned(),
));
}
let mut buf = String::new();
std::io::stdin()
.read_to_string(&mut buf)
.map_err(|e| ExecError::Usage(format!("cannot read standard input: {e}")))?;
Ok(buf)
} else {
std::fs::read_to_string(script)
.map_err(|e| ExecError::Usage(format!("cannot read `{script}`: {e}")))
}
}
fn strip_comment(line: &str) -> &str {
let mut quote: Option<char> = None;
let mut escaped = false;
for (i, c) in line.char_indices() {
if escaped {
escaped = false;
continue;
}
match (quote, c) {
(Some('"'), '\\') => escaped = true,
(Some(q), ch) if ch == q => quote = None,
(Some(_), _) => {}
(None, '\'') | (None, '"') => quote = Some(c),
(None, '\\') => escaped = true,
(None, '#') => return &line[..i],
(None, _) => {}
}
}
line
}
fn first_line(s: &str) -> String {
s.lines().next().unwrap_or(s).trim().to_owned()
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn comments_are_stripped_outside_quotes() {
assert_eq!(strip_comment("put a b # trailing"), "put a b ");
assert_eq!(strip_comment("# whole line"), "");
assert_eq!(
strip_comment(r#"put a "not # a comment""#),
r#"put a "not # a comment""#
);
assert_eq!(strip_comment("put a b"), "put a b");
}
#[test]
fn first_line_of_a_clap_error() {
assert_eq!(first_line("error: bad\n\nUsage: x"), "error: bad");
}
}