Skip to content

Solution for long term memory for agent coding CLIs and to facilitate handoff between different agent vendors

Rust

fix(admin): carry the resolved project name; tidy the id-path follow-ups

AkitaOnRails2e64a21ed53d
diff --git a/CHANGELOG.md b/CHANGELOG.md
index ac3395ab..bb98054d 100644
--- a/CHANGELOG.md
+++ b/CHANGELOG.md
@@ -328,7 +328,9 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
or renamed. The id is looked up inside the named workspace: sending both or
neither is a 400, a malformed UUID is a 400, and an id that names no project
in that workspace is a 404. `list-projects` now prints each project's id so
- an operator can copy it. (#1208, #1226)
+ an operator can copy it, and `GET /api/v1/projects` rows carry
+ `workspace_id` and `project_id` (hidden with the row for a restricted
+ project). (#1208, #1226)
- Added `ai-memory project init`, which creates a `.ai-memory.toml` marker at
the current worktree root (or the current directory outside Git) after
asking for `workspace` and `project`. The defaults are the scope the CLI and
diff --git a/crates/ai-memory-cli/src/commands/project_init.rs b/crates/ai-memory-cli/src/commands/project_init.rs
index 38d656ad..b40d216d 100644
--- a/crates/ai-memory-cli/src/commands/project_init.rs
+++ b/crates/ai-memory-cli/src/commands/project_init.rs
@@ -147,7 +147,24 @@ fn write_new_marker(path: &Path, body: &str) -> Result<()> {
tmp.as_file()
.sync_all()
.with_context(|| format!("syncing {}", tmp.path().display()))?;
- std::fs::hard_link(tmp.path(), path).with_context(|| format!("creating {}", path.display()))?;
+ match std::fs::hard_link(tmp.path(), path) {
+ Ok(()) => {}
+ // FAT/exFAT and some network or bind mounts refuse hard links. Fall
+ // back to an exclusive create, which still never replaces a marker.
+ Err(error) if error.kind() != io::ErrorKind::AlreadyExists => {
+ let mut file = std::fs::OpenOptions::new()
+ .write(true)
+ .create_new(true)
+ .open(path)
+ .with_context(|| format!("creating {}", path.display()))?;
+ file.write_all(body.as_bytes())
+ .and_then(|()| file.sync_all())
+ .with_context(|| format!("writing {} (after: {error})", path.display()))?;
+ }
+ Err(error) => {
+ return Err(error).with_context(|| format!("creating {}", path.display()));
+ }
+ }
if let Ok(dir) = std::fs::File::open(dir) {
let _ = dir.sync_all();
}
diff --git a/crates/ai-memory-cli/src/commands/purge_project.rs b/crates/ai-memory-cli/src/commands/purge_project.rs
index 40d5ebeb..58683e84 100644
--- a/crates/ai-memory-cli/src/commands/purge_project.rs
+++ b/crates/ai-memory-cli/src/commands/purge_project.rs
@@ -142,15 +142,10 @@ pub async fn run(config: &Config, args: PurgeProjectArgs) -> Result<()> {
// The flag that names the target, for the refusal's "re-run with" line and
// the preview's fallback label. A `--project-id` run has no resolved name
// to print, so the id stands in.
- let target_flag = match (&project, &project_id) {
- (Some(project), _) => format!("--project {project}"),
- (None, Some(id)) => format!("--project-id {id}"),
- (None, None) => unreachable!("clap requires one of --project/--project-id"),
- };
- let target_label = match (&project, &project_id) {
- (Some(project), _) => project.clone(),
- (None, Some(id)) => id.clone(),
- (None, None) => unreachable!("clap requires one of --project/--project-id"),
+ let (target_flag, target_label) = match (&project, &project_id) {
+ (Some(project), _) => (format!("--project {project}"), project.clone()),
+ (None, Some(id)) => (format!("--project-id {id}"), id.clone()),
+ (None, None) => bail!("one of --project or --project-id is required"),
};
if !args.confirm {
diff --git a/crates/ai-memory-mcp/src/admin.rs b/crates/ai-memory-mcp/src/admin.rs
index 19017bbc..af2d5376 100644
--- a/crates/ai-memory-mcp/src/admin.rs
+++ b/crates/ai-memory-mcp/src/admin.rs
@@ -2025,11 +2025,36 @@ async fn lookup_ws_no_create(
/// is looked up by id within the workspace (404 on miss, so a foreign-scope
/// id is correctly refused). The bad-UUID case is a 400, mirroring how
/// `lookup_ws_proj_no_create` already maps unparseable names to 400.
+///
+/// Returns the project's stored name with the ids, so labels, admission
+/// contexts and move destinations never fall back to the UUID: a failed name
+/// read is a 500, not a guess.
async fn resolve_target_for_admin(
state: &AdminState,
workspace: &str,
project_name: Option<&str>,
project_id: Option<&str>,
+) -> Result<(WorkspaceId, ProjectId, String), (StatusCode, Json<serde_json::Value>)> {
+ let (ws_id, proj_id) =
+ resolve_target_ids_for_admin(state, workspace, project_name, project_id).await?;
+ match state.reader.project_name_by_id(ws_id, proj_id).await {
+ Ok(Some(name)) => Ok((ws_id, proj_id, name)),
+ Ok(None) => Err((
+ StatusCode::NOT_FOUND,
+ Json(serde_json::json!({ "error": "project not found in workspace" })),
+ )),
+ Err(e) => Err((
+ StatusCode::INTERNAL_SERVER_ERROR,
+ Json(serde_json::json!({ "error": e.to_string() })),
+ )),
+ }
+}
+
+async fn resolve_target_ids_for_admin(
+ state: &AdminState,
+ workspace: &str,
+ project_name: Option<&str>,
+ project_id: Option<&str>,
) -> Result<(WorkspaceId, ProjectId), (StatusCode, Json<serde_json::Value>)> {
match (project_name, project_id) {
(Some(_), Some(_)) => Err((
@@ -5024,7 +5049,7 @@ async fn handle_purge_project(
}
// Look up workspace and project IDs without auto-creating.
- let (ws_id, proj_id) = match resolve_target_for_admin(
+ let (ws_id, proj_id, project_name) = match resolve_target_for_admin(
&state,
&req.workspace,
req.project.as_deref(),
@@ -5032,19 +5057,9 @@ async fn handle_purge_project(
)
.await
{
- Ok(ids) => ids,
+ Ok(target) => target,
Err(e) => return e,
};
- // Look up the project name after resolving by id, so display labels and
- // admission contexts can carry it regardless of whether the operator
- // reached us via `project = "<name>"` or `project_id = "<uuid>"`. (#1208)
- let project_name: String = state
- .reader
- .project_name_by_id(ws_id, proj_id)
- .await
- .ok()
- .flatten()
- .unwrap_or_else(|| proj_id.to_string());
let label = format!("{}/{}", req.workspace, project_name);
@@ -5199,7 +5214,7 @@ async fn purge_project_preview(
req: &PurgeProjectRequest,
author_id: Option<ai_memory_core::UserId>,
) -> (StatusCode, Json<serde_json::Value>) {
- let (ws_id, proj_id) = match resolve_target_for_admin(
+ let (ws_id, proj_id, project_name) = match resolve_target_for_admin(
state,
&req.workspace,
req.project.as_deref(),
@@ -5207,19 +5222,9 @@ async fn purge_project_preview(
)
.await
{
- Ok(ids) => ids,
+ Ok(target) => target,
Err(e) => return e,
};
- // Look up the project name after resolving by id, so display labels and
- // admission contexts can carry it regardless of whether the operator
- // reached us via `project = "<name>"` or `project_id = "<uuid>"`. (#1208)
- let project_name: String = state
- .reader
- .project_name_by_id(ws_id, proj_id)
- .await
- .ok()
- .flatten()
- .unwrap_or_else(|| proj_id.to_string());
let label = format!("{}/{}", req.workspace, project_name);
@@ -5888,7 +5893,7 @@ async fn handle_rename_project(
) -> impl IntoResponse {
let author_id = author_ext.map(|axum::Extension(u)| u);
// Look up workspace + source project; 404 if either is absent.
- let (ws_id, proj_id) = match resolve_target_for_admin(
+ let (ws_id, proj_id, project_name) = match resolve_target_for_admin(
&state,
&req.workspace,
req.from.as_deref(),
@@ -5896,19 +5901,9 @@ async fn handle_rename_project(
)
.await
{
- Ok(ids) => ids,
+ Ok(target) => target,
Err(e) => return e,
};
- // Look up the project name after resolving by id, so display labels and
- // admission contexts can carry it regardless of whether the operator
- // reached us via `from = "<name>"` or `project_id = "<uuid>"`. (#1208)
- let project_name: String = state
- .reader
- .project_name_by_id(ws_id, proj_id)
- .await
- .ok()
- .flatten()
- .unwrap_or_else(|| proj_id.to_string());
// Step 3: execute the rename. The writer validates the name and
// returns ProjectNameTaken / InvalidProjectName on conflicts. A
@@ -6463,7 +6458,7 @@ async fn move_project_core(
}
// Resolve the SOURCE without auto-creating — 404 on a typo.
- let (src_ws, src_proj) = match resolve_target_for_admin(
+ let (src_ws, src_proj, project_name) = match resolve_target_for_admin(
state,
&req.from_workspace,
req.project.as_deref(),
@@ -6471,19 +6466,9 @@ async fn move_project_core(
)
.await
{
- Ok(ids) => ids,
+ Ok(target) => target,
Err(e) => return Err(MoveErr::from(e)),
};
- // Look up the project name after resolving by id, so display labels and
- // admission contexts can carry it regardless of whether the operator
- // reached us via `project = "<name>"` or `project_id = "<uuid>"`. (#1208)
- let project_name: String = state
- .reader
- .project_name_by_id(src_ws, src_proj)
- .await
- .ok()
- .flatten()
- .unwrap_or_else(|| src_proj.to_string());
// Same guard on the RESOLVED name, so the `project_id` path (which has no
// name to inspect up front) is refused identically.
diff --git a/docs/use-cases.md b/docs/use-cases.md
index 7a465d9c..9e75f776 100644
--- a/docs/use-cases.md
+++ b/docs/use-cases.md
@@ -117,10 +117,12 @@
- **"Quit at 4 PM, pick up at 9 AM in a different agent."** The
SessionStart hook in the next supported hook client prepends a
typed handoff with open questions, next steps, and a session summary. Grok
- cannot show that text before the first prompt. The first tool's
- `PostToolUse` hook adds it as `additionalContext`. If the session never
- calls a tool, ask Grok to call `memory_handoff_accept`. Zero still must
- call `memory_handoff_accept`.
+ cannot show that text before the first prompt: the first-turn rule
+ `install-hooks --agent grok` installs has it call `memory_handoff_list`
+ first, and that tool's `PostToolUse` hook adds the handoff as
+ `additionalContext`. Without the rule, ask Grok to list and then accept the
+ handoff. Zero still must call `memory_handoff_list` then
+ `memory_handoff_accept`.
- **"What did we decide about X six weeks ago?"** Use `memory_query X` from
the agent for FTS5 fused with entity matches and linked-page expansion (plus
vector similarity when an embedder is configured). For a quick terminal-only