use std::path::Path;
use tracing::debug;
use trusty_common::catchup::{CatchupOptions, run_catchup};
pub async fn pm_catchup_context(project_dir: &Path) -> Option<String> {
let memory_url = trusty_common::mcp::memory_rpc::resolve_memory_base_url_or_unreachable();
let opts = CatchupOptions {
project_dir: project_dir.to_path_buf(),
memory_url,
include_git: true,
include_palace: true,
git_limit: 50,
drawer_limit: 15,
full: false,
};
let digest = run_catchup(&opts, false).await;
if digest.trim().is_empty() {
debug!("pm_catchup_context: empty digest — omitting from PM prompt");
None
} else {
Some(digest)
}
}
#[cfg(test)]
mod tests {
use super::*;
use std::fs;
use std::process::Command;
use tempfile::TempDir;
fn init_git_repo(dir: &TempDir) {
let p = dir.path();
for args in [
vec!["init"],
vec!["config", "user.email", "test@test.com"],
vec!["config", "user.name", "Test"],
] {
let _ = Command::new("git").arg("-C").arg(p).args(&args).output();
}
let _ = fs::write(p.join("README.md"), b"test");
let _ = Command::new("git")
.arg("-C")
.arg(p)
.args(["add", "."])
.output();
let _ = Command::new("git")
.arg("-C")
.arg(p)
.args(["commit", "-m", "init"])
.output();
}
#[tokio::test]
async fn pm_catchup_context_does_not_panic_on_empty_repo() {
let tmp = TempDir::new().unwrap();
init_git_repo(&tmp);
unsafe {
std::env::set_var(
trusty_common::mcp::memory_rpc::TRUSTY_MEMORY_URL_ENV,
"http://127.0.0.1:19999",
)
};
let result = pm_catchup_context(tmp.path()).await;
match &result {
Some(ctx) => {
assert!(
!ctx.trim().is_empty(),
"Some(ctx) must not be empty/whitespace"
);
}
None => {
}
}
let state_file_in_project = tmp.path().join(".trusty-mpm");
assert!(
!state_file_in_project.exists(),
"advance_watermark=false must not write state inside the project dir"
);
}
#[tokio::test]
async fn pm_catchup_context_does_not_panic_on_non_git_dir() {
let tmp = TempDir::new().unwrap();
unsafe {
std::env::set_var(
trusty_common::mcp::memory_rpc::TRUSTY_MEMORY_URL_ENV,
"http://127.0.0.1:19999",
)
};
let _result = pm_catchup_context(tmp.path()).await;
}
}