1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
//! `memstead rename` — change an entity's title, ID, file path, and every incoming wiki-link.
//!
//! Hash handling matches `memstead update`: strict by default, `--auto-hash`
//! refetches from the store, `--force` explicitly accepts the overwrite.
use clap::Parser;
use memstead_base::vcs::Actor;
use memstead_base::{EntityId, RenameEntityArgs};
use crate::CliError;
use crate::output::{ExitKind, print_json, print_markdown};
use crate::setup::{CliContext, CliEngine};
#[derive(Parser, Debug)]
#[command(after_long_help = super::slug_derivation_help())]
pub struct Args {
/// Current entity ID. A bare slug without the `mem--` prefix resolves
/// when exactly one mounted mem carries it (announced as
/// `SHORT_ID_RESOLVED`); otherwise refuses `ENTITY_ID_MISSING_MEM` naming
/// the candidates.
pub id: String,
/// New title. The ID is re-derived from the title.
pub new_title: String,
/// Hash from `memstead entity <id>`. Required unless `--auto-hash` or `--force`.
#[arg(long = "expected-hash", value_name = "HASH")]
pub expected_hash: Option<String>,
/// Refetch the current hash immediately before writing.
#[arg(long, conflicts_with_all = ["expected_hash", "force"])]
pub auto_hash: bool,
/// Skip the hash check (explicit overwrite).
#[arg(long, conflicts_with_all = ["expected_hash", "auto_hash"])]
pub force: bool,
/// Agent-authored provenance note (≤280 chars). When
/// `[mutations].require_notes = true` a missing note adds a
/// `NOTE_MISSING` warning.
#[arg(long)]
pub note: Option<String>,
}
pub fn run(ctx: &CliContext, args: Args) -> anyhow::Result<()> {
let id = EntityId::canonical(&args.id);
let new_title = args.new_title.clone();
match ctx.cli_engine()? {
#[cfg(feature = "mem-repo")]
CliEngine::MemRepo(mut engine) => {
let lookup_id = crate::setup::preflight_id(&mut engine, &id)?;
let expected_hash = resolve_expected_hash_mem_repo(&engine, &lookup_id, &args)?;
let result = engine
.rename_entity_with_ctx(
&id,
&new_title,
&expected_hash,
&crate::setup::cli_ctx_with_note(args.note.clone()),
)
.map_err(CliError::from_engine_op)?;
let mem_changed = engine.take_mem_changed_notices();
if ctx.json {
let mut body = serde_json::to_value(&result).unwrap_or(serde_json::Value::Null);
super::merge_mem_changed_json(&mut body, &mem_changed);
print_json(&body)?;
} else {
let mut body = format!(
"# Renamed\n\n- `{}` → `{}`\n- Path: {} → {}\n- Hash: `{}`",
result.old_id,
result.new_id,
result.old_path,
result.new_path,
result.content_hash,
);
// The same warnings line the filesystem branch and the
// sibling verbs render: a `SHORT_ID_RESOLVED` or
// `NOTE_MISSING` hint must not be visible only under
// `--json`.
if !result.warnings.is_empty() {
let parts: Vec<String> =
result.warnings.iter().map(|w| w.to_string()).collect();
body.push_str(&format!("\n- Warnings: {}", parts.join("; ")));
}
body.push_str(&super::render_mem_changed_block(&mem_changed));
print_markdown(&body);
}
}
CliEngine::Filesystem(mut engine) => {
let lookup_id = crate::setup::preflight_id(&mut engine, &id)?;
let expected_hash = resolve_expected_hash_filesystem(&engine, &lookup_id, &args)?;
let outcome = engine
.rename_entity(
RenameEntityArgs {
id: id.clone(),
expected_hash: Some(expected_hash),
new_title: new_title.clone(),
},
Actor::Cli,
None,
args.note.as_deref(),
)
.map_err(CliError::from_engine_op)?;
if ctx.json {
print_json(&serde_json::json!({
"old_id": outcome.old_id.as_ref(),
"new_id": outcome.new_id.as_ref(),
"old_path": outcome.old_path,
"new_path": outcome.new_path,
"_hash": outcome.content_hash,
// Backend write identity — response-shape parity with
// the MCP filesystem flavour.
"write_id": outcome.write_id,
// Engine-emitted warnings (e.g. `NOTE_MISSING` under
// `[mutations].require_notes`).
"warnings": outcome.warnings,
}))?;
} else {
let mut body = format!(
"# Renamed\n\n- `{}` → `{}`\n- Path: {} → {}\n- Hash: `{}`",
outcome.old_id,
outcome.new_id,
outcome.old_path,
outcome.new_path,
outcome.content_hash,
);
if !outcome.warnings.is_empty() {
let parts: Vec<String> =
outcome.warnings.iter().map(|w| w.to_string()).collect();
body.push_str(&format!("\n- Warnings: {}", parts.join("; ")));
}
print_markdown(&body);
}
}
}
Ok(())
}
/// Resolve the `expected_hash` for the mem-repo path: either the
/// flag value, or the live hash from the store under `--auto-hash` /
/// `--force`. Mirrors the original inline logic — extracted only so
/// the filesystem path can run the same flag plumbing without
/// duplicating it.
#[cfg(feature = "mem-repo")]
fn resolve_expected_hash_mem_repo(
engine: &memstead_base::Engine,
id: &EntityId,
args: &Args,
) -> anyhow::Result<String> {
if args.auto_hash || args.force {
Ok(engine
.get_entity(id)
.ok_or_else(|| {
CliError::new(
ExitKind::NotFound,
"ENTITY_NOT_FOUND",
format!("entity not found: {id}"),
)
.with_details(serde_json::json!({ "id": id.to_string() }))
})?
.content_hash
.clone())
} else {
args.expected_hash
.clone()
.filter(|h| !h.is_empty())
.ok_or_else(|| {
CliError::new(
ExitKind::Validation,
crate::HASH_FLAG_REQUIRED_CODE,
"missing --expected-hash. Read the entity first (memstead entity <id>) and pass its `_hash`, \
or use --auto-hash / --force.",
)
.into()
})
}
}
fn resolve_expected_hash_filesystem(
engine: &memstead_base::Engine,
id: &EntityId,
args: &Args,
) -> anyhow::Result<String> {
if args.auto_hash || args.force {
Ok(engine
.get_entity(id)
.ok_or_else(|| {
CliError::new(
ExitKind::NotFound,
"ENTITY_NOT_FOUND",
format!("entity not found: {id}"),
)
.with_details(serde_json::json!({ "id": id.to_string() }))
})?
.content_hash
.clone())
} else {
args.expected_hash
.clone()
.filter(|h| !h.is_empty())
.ok_or_else(|| {
CliError::new(
ExitKind::Validation,
crate::HASH_FLAG_REQUIRED_CODE,
"missing --expected-hash. Read the entity first (memstead entity <id>) and pass its `_hash`, \
or use --auto-hash / --force.",
)
.into()
})
}
}