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
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
//! `Engine::rename_entity` — change an entity's slug, move its file
//! on the backend, and rewrite the in-memory store.
use std::path::Path;
use crate::engine_fallback_type;
use crate::entity::EntityId;
use crate::entity::id::validate_and_derive_slug;
use crate::entity::parser::parse_markdown;
use crate::entity::store_builder::push_entities_into_store;
use crate::ops::WarningHint;
use crate::provenance::{Provenance, ProvenanceKind};
use crate::vcs::{Actor, ClientId, CommitContext};
use crate::workspace::MountCapability;
use super::super::{Engine, EngineError, RenameEntityArgs, RenameEntityOutcome};
use super::{make_stub, unknown_type_error};
/// One entity whose marked sections a rename's wiki-link rewrite moves: its
/// id, its sections before the rewrite, and after.
type MarkRehash = (
EntityId,
indexmap::IndexMap<String, String>,
indexmap::IndexMap<String, String>,
);
impl Engine {
/// Positional + CommitContext wrapper around
/// [`Self::rename_entity`].
pub fn rename_entity_with_ctx(
&mut self,
old_id: &EntityId,
new_title: &str,
expected_hash: &str,
ctx: &CommitContext<'_>,
) -> Result<RenameEntityOutcome, EngineError> {
let args = RenameEntityArgs {
id: old_id.clone(),
expected_hash: Some(expected_hash.to_string()),
new_title: new_title.to_string(),
};
self.rename_entity(args, ctx.actor, ctx.client.as_ref(), ctx.note.as_deref())
}
/// Rename an entity by changing its title — the slug, id, and
/// on-disk file path follow.
///
/// **Same-mem referrers and self-references are rewritten
/// atomically.** The renaming entity is treated as the first
/// referrer of itself: every entry in its own `relationships`
/// list whose target equals the old id is updated to point at
/// the new id, and every `[[<old-slug>]]` token in its own
/// section bodies is rewritten to the new slug (respecting
/// fenced-code and inline-code masking). Every other entity in
/// the same mem that pointed at the old id (via an explicit
/// relation or an inline body wiki-link) gets the same two-
/// surface rewrite. All rewrites land in one per-mem commit.
/// Cross-mem referrers and ReadOnly-mount referrers are not
/// yet walked — those land with the multi-mem atomicity
/// machinery and the residual-stub demotion path respectively.
pub fn rename_entity(
&mut self,
args: RenameEntityArgs,
actor: Actor,
client: Option<&ClientId>,
note: Option<&str>,
) -> Result<RenameEntityOutcome, EngineError> {
// A short id resolves (or refuses) before anything reads its mem.
let mut args = args;
let (resolved, short_hint) = self.resolve_entity_id(&args.id)?;
args.id = resolved;
let id = &args.id;
let mem = id.mem().to_string();
let mount_idx = self
.mounts
.iter()
.position(|m| m.mount.mem == mem)
.ok_or_else(|| self.unknown_mem_error(&mem))?;
if self.mounts[mount_idx].mount.capability != MountCapability::Write {
return Err(EngineError::ReadOnlyMount(mem));
}
// Reload-before-operation: reload if a sibling advanced the
// mem ref so the `expected_hash` compare below runs against
// current truth. The drift notice rides the outcome's
// `warnings` (real-rename path).
let mut drift_warnings: Vec<WarningHint> = short_hint.into_iter().collect();
drift_warnings.extend(self.reload_if_stale(Some(&mem)));
let entity = self
.store
.get(id)
.ok_or_else(|| EngineError::NotFound { id: id.to_string() })?;
// Stub guard — stubs derive their title from the id and have
// no `entity_type` to validate against. Recovery is
// `memstead_create` (stub adoption). Item 02 realised the
// `STUB_NOT_RENAMABLE` code the description list had
// advertised since the strictness work landed.
if entity.stub {
return Err(EngineError::StubNotRenamable { id: id.to_string() });
}
if let Some(expected) = args.expected_hash.as_deref()
&& entity.content_hash != expected
{
return Err(EngineError::HashMismatch {
id: id.to_string(),
current: entity.content_hash.clone(),
is_stub: entity.stub,
});
}
let derivation = validate_and_derive_slug(&args.new_title)?;
let new_slug = derivation.slug.clone();
let new_id = EntityId::new(&mem, &new_slug);
crate::entity::id::enforce_id_length(new_id.as_ref())?;
if new_id == *id {
// Title-with-same-slug: surface a Tier-2 warning rather
// than touching disk for nothing. Matches full's
// RenameResult slug-noop shape so autonomous skills
// can distinguish the silent no-op from a successful
// cosmetic rewrite via the typed warning code.
return Ok(RenameEntityOutcome {
old_id: id.clone(),
new_id: new_id.clone(),
old_path: entity.file_path.clone(),
new_path: entity.file_path.clone(),
content_hash: entity.content_hash.clone(),
write_id: String::new(),
warnings: vec![WarningHint::TitleNormalizedToSlugNoop {
requested_title: args.new_title.clone(),
current_slug: id.name().to_string(),
}],
});
}
if let Some(existing) = self.store.get(&new_id) {
return Err(EngineError::AlreadyExists {
id: new_id.to_string(),
existing_title: existing.title.clone(),
existing_is_stub: existing.stub,
});
}
let schema = self
.schemas
.get(&mem)
.expect("schema present for every registered mount");
let type_def = schema
.get_type(&entity.entity_type)
.ok_or_else(|| unknown_type_error(schema, &entity.entity_type))?;
let old_file_path = entity.file_path.clone();
let new_file_path = format!("{new_slug}.md");
let mut next = entity.clone();
next.id = new_id.clone();
next.title = args.new_title.clone();
next.file_path = new_file_path.clone();
// Self-reference rewrite, surface 1/2 — `relationships` list.
// Any explicit edge `<this> --<type>--> <old_id>` is repointed
// to `<this> --<type>--> <new_id>` so the regenerated
// `## Relationships` section reflects the new id rather than
// a relation that would re-emit a stub of the old slug at
// next read.
for rel in next.relationships.iter_mut() {
if rel.target == *id {
rel.target = new_id.clone();
}
}
// Self-reference rewrite, surface 2/2 — section bodies.
// Every `[[<old-slug>]]` token in the renaming entity's own
// section bodies becomes `[[<new-slug>]]`. Code-fenced and
// inline-code matches are preserved (the rewriter shares the
// masking discipline of `extract_inline_links`).
//
// The body parser admits both short form `[[slug]]` and
// full-id form `[[mem--slug]]` (same mem) as references to
// the same entity. The bare-slug rewriter only catches the
// first; `rewrite_cross_mem_slug` (which already powers the
// cross-mem Tier-2 rewrite) covers the second by matching
// `<mem>--<slug>` and `<mem>:<slug>` forms. Calling both
// here preserves the form the author wrote — short stays short,
// full-id stays full-id — just retargeted to the new slug.
let old_slug = id.name().to_string();
let new_slug_owned = new_slug.clone();
for body in next.sections.values_mut() {
let (rewritten, count) = crate::entity::wikilink_rewrite::rewrite_bare_slug(
body,
&old_slug,
&new_slug_owned,
);
if count > 0 {
*body = rewritten;
}
let (rewritten, count) = crate::entity::wikilink_rewrite::rewrite_cross_mem_slug(
body,
&mem,
&old_slug,
&new_slug_owned,
);
if count > 0 {
*body = rewritten;
}
}
// Every entity whose on-disk file the rename rewrites
// (the renaming entity itself plus every Write-mem referrer
// touched by the body/relationships rewrite cascade) gets
// `auto_timestamp` metadata fields stamped before
// `generate_markdown` so the new file carries the stamp.
// Pre-compute `today` once so all entities rewritten by this
// logical operation receive the same timestamp.
let today = self.now_iso();
super::auto_stamp_timestamps(&mut next, type_def.as_ref(), &today);
let markdown = super::render_for_write(&next, type_def.as_ref())?;
// Referrer collection. Walk every incoming edge — explicit
// relations and `EdgeSource::BodyLink` synthesised mirrors
// alike — and bucket each unique referrer by its mem's
// capability. Same-mem referrers are guaranteed Write (the
// mount-capability gate at the top of this fn refused the
// rename if the renaming mem itself isn't Write). Cross-
// mem referrers split into Write (rewriteable, subject to
// the `cross_mem_links` policy gate below) and ReadOnly
// (the engine has no write access; their handling lands with
// the rename-path residual-stub demotion in the next cut and
// is filtered out here).
let mut seen: std::collections::HashSet<EntityId> = std::collections::HashSet::new();
let mut same_mem_ids: Vec<EntityId> = Vec::new();
let mut cross_mem_ids: Vec<EntityId> = Vec::new();
for in_edge in self.store.incoming(id) {
if in_edge.from == *id || !seen.insert(in_edge.from.clone()) {
continue;
}
if in_edge.from.mem() == mem {
same_mem_ids.push(in_edge.from.clone());
} else {
cross_mem_ids.push(in_edge.from.clone());
}
}
// Prose-only referrers. Body wiki-links are not edge sources
// (every store edge originates from the auto-managed
// `## Relationships` section), so a hand-authored file carrying
// an inline `[[old-slug]]` with no relationship row has no
// incoming edge and the walk above cannot see it — its link
// would silently go stale. Scan section bodies with the same
// lenient extractor the load-time drift scan uses (code-fence
// and inline-code masking included), gated on a cheap substring
// probe so the store-wide pass stays proportional to actual
// mentions. Engine-written entities are covered either way:
// alias synthesis always emits the row on write.
for entity in self.store.all_entities() {
if entity.id == *id || entity.stub || seen.contains(&entity.id) {
continue;
}
let mentions = entity.sections.values().any(|body| {
body.contains(old_slug.as_str())
&& crate::entity::parser::extract_inline_links_lenient(body, entity.id.mem())
.iter()
.any(|t| t == id)
});
if !mentions {
continue;
}
seen.insert(entity.id.clone());
if entity.id.mem() == mem {
same_mem_ids.push(entity.id.clone());
} else {
cross_mem_ids.push(entity.id.clone());
}
}
// Cross-mem peers are partitioned by mount capability.
// Write peers feed the cross-mem rewrite plan; ReadOnly
// peers feed the residual-stub demotion path (the engine
// can't rewrite their on-disk markdown, so we materialise an
// in-memory stub at the OLD id that holds the surviving
// incoming edges from the ReadOnly mount — mirrors the
// delete-path's same-shaped demotion).
let mut cross_mem_write_ids: Vec<EntityId> = Vec::new();
let mut readonly_referrers: Vec<EntityId> = Vec::new();
for from_id in cross_mem_ids {
match self
.mount(from_id.mem())
.map(|m| m.capability)
.unwrap_or(MountCapability::Write)
{
MountCapability::Write => cross_mem_write_ids.push(from_id),
MountCapability::ReadOnly => readonly_referrers.push(from_id),
}
}
readonly_referrers.sort_by_key(|a| a.to_string());
// Pre-flight policy gate. Each propagated referrer rewrite
// is an edge of the form `referrer ∈ peer_mem → renamed ∈
// mem` — same direction as the original edge. The gate
// consults `cross_mem_link_allowed(peer_mem, mem)`,
// which is the direction the policy gates new edges with
// (forward-looking add-filter). A blocked peer aborts the
// rename up-front — no writes have happened yet, so the
// refusal is clean. This is the edge direction, not its
// inverse.
let mut blocked_counts: std::collections::BTreeMap<String, usize> =
std::collections::BTreeMap::new();
for from_id in &cross_mem_write_ids {
let peer_mem = from_id.mem().to_string();
if !self.cross_mem_link_allowed(&peer_mem, &mem) {
*blocked_counts.entry(peer_mem).or_insert(0) += 1;
}
}
if !blocked_counts.is_empty() {
let blocked_referrers: Vec<crate::engine::error::BlockedReferrer> = blocked_counts
.into_iter()
.map(|(peer_mem, count)| crate::engine::error::BlockedReferrer {
from_mem: peer_mem,
to_mem: mem.clone(),
count,
})
.collect();
return Err(EngineError::RenameBlockedByCrossMemPolicy {
from_mem: mem.clone(),
blocked_referrers,
});
}
// Deterministic iteration order so the resulting per-mem
// pending-op replay is stable across runs (helpful for test
// snapshots and human reviewers).
same_mem_ids.sort_by_key(|a| a.to_string());
cross_mem_write_ids.sort_by_key(|a| a.to_string());
// ----- Same-mem rewrite plan -----
// (rewritten_markdown, file_path, type_def) per same-mem
// referrer — collected before any backend write so a
// per-referrer schema or rewrite failure aborts the rename
// before anything lands.
let mut same_mem_writes: Vec<(
String,
String,
std::sync::Arc<memstead_schema::TypeDefinition>,
)> = Vec::with_capacity(same_mem_ids.len());
// Whose marked sections this rename rewrites, before and after. A
// wiki-link retarget moves marked bytes without being an authorship
// event, so the marks are recomputed over the new bytes in the same
// commit; unfollowed, ordinary housekeeping would serve a
// contributor's untouched sentences as the workspace's own.
let mut same_mem_rehash: Vec<MarkRehash> = Vec::new();
for from_id in &same_mem_ids {
let Some(referrer) = self.store.get(from_id) else {
continue;
};
if referrer.stub {
continue;
}
let referrer_type_def = schema
.get_type(&referrer.entity_type)
.ok_or_else(|| unknown_type_error(schema, &referrer.entity_type))?;
let mut next_ref = referrer.clone();
for rel in next_ref.relationships.iter_mut() {
if rel.target == *id {
rel.target = new_id.clone();
}
}
for body in next_ref.sections.values_mut() {
let (rewritten, count) = crate::entity::wikilink_rewrite::rewrite_bare_slug(
body,
&old_slug,
&new_slug_owned,
);
if count > 0 {
*body = rewritten;
}
// A same-mem referrer may also use
// full-id form `[[<mem>--<slug>]]` to point at the
// renaming entity — covered by the cross-mem helper
// which matches the `--` and `:` separator forms.
let (rewritten, count) = crate::entity::wikilink_rewrite::rewrite_cross_mem_slug(
body,
&mem,
&old_slug,
&new_slug_owned,
);
if count > 0 {
*body = rewritten;
}
}
// Do NOT bump the referrer's `auto_timestamp` fields. A
// rename rewrites the referrer's body wiki-link to the new
// slug — a foreign-key change, not a semantic edit — so its
// `last_modified` staleness clock must NOT reset (a staleness
// audit would otherwise read every referrer of a renamed
// entity as freshly-touched). The referrer's content hash
// still changes (its body now holds the new slug); only the
// staleness clock is preserved by carrying the prior
// timestamp through from the cloned referrer. (Pre-fix this
// stamped the shared `today` for cross-entity consistency.)
let ref_markdown = super::render_for_write(&next_ref, referrer_type_def.as_ref())?;
same_mem_rehash.push((
from_id.clone(),
referrer.sections.clone(),
next_ref.sections.clone(),
));
same_mem_writes.push((ref_markdown, next_ref.file_path.clone(), referrer_type_def));
}
// ----- Cross-mem rewrite plan -----
// Group cross-mem Write referrers by their mem so each
// peer mem's backend gets one commit. Each entry holds the
// peer mount index, the peer mem's schema, and the list of
// (markdown, file_path, type_def) for that mem's referrers.
struct PeerMemPlan {
mount_idx: usize,
mem: String,
writes: Vec<(
String,
String,
std::sync::Arc<memstead_schema::TypeDefinition>,
)>,
/// Whose marked sections this rewrite moves, before and after —
/// the peer mem's own record is the one that holds its marks.
rehash: Vec<MarkRehash>,
}
let mut peer_plans: std::collections::BTreeMap<String, PeerMemPlan> =
std::collections::BTreeMap::new();
for from_id in &cross_mem_write_ids {
let peer_mem = from_id.mem().to_string();
let peer_mount_idx = self
.mounts
.iter()
.position(|m| m.mount.mem == peer_mem)
.expect("peer mount present for collected referrer id");
let peer_schema = self
.schemas
.get(&peer_mem)
.expect("schema present for every registered mount");
let Some(referrer) = self.store.get(from_id) else {
continue;
};
if referrer.stub {
continue;
}
let referrer_type_def = peer_schema
.get_type(&referrer.entity_type)
.ok_or_else(|| unknown_type_error(peer_schema, &referrer.entity_type))?;
let before_sections = referrer.sections.clone();
let mut next_ref = referrer.clone();
for rel in next_ref.relationships.iter_mut() {
if rel.target == *id {
rel.target = new_id.clone();
}
}
// Cross-mem referrers reference the renaming entity
// via the cross-mem wiki-link forms (`[[<mem>:<slug>]]`
// or the legacy `[[<mem>--<slug>]]`). The bare-slug
// form is reserved for same-mem refs and never appears
// here.
for body in next_ref.sections.values_mut() {
let (rewritten, count) = crate::entity::wikilink_rewrite::rewrite_cross_mem_slug(
body,
&mem,
&old_slug,
&new_slug_owned,
);
if count > 0 {
*body = rewritten;
}
}
// A cross-mem Write peer is a referrer too — preserve its
// `last_modified` for the same reason as the same-mem case
// above (slug rewrite is a foreign-key change, not a semantic
// edit). Its content hash still changes; the staleness clock
// does not reset.
let ref_markdown = super::render_for_write(&next_ref, referrer_type_def.as_ref())?;
let plan_entry = peer_plans
.entry(peer_mem.clone())
.or_insert_with(|| PeerMemPlan {
mount_idx: peer_mount_idx,
mem: peer_mem.clone(),
writes: Vec::new(),
rehash: Vec::new(),
});
plan_entry
.writes
.push((ref_markdown, next_ref.file_path.clone(), referrer_type_def));
plan_entry
.rehash
.push((from_id.clone(), before_sections, next_ref.sections.clone()));
}
// ----- Apply: renaming entity's own mem first -----
// Mint a single `logical_operation_id` up front so every
// commit produced by this rename — source mem + every peer
// mem — carries the same correlation id in its provenance
// entry. Single-mem renames also tag with an id (it just
// maps to one commit); consumers branch on whether the id
// recurs to identify a multi-commit logical operation.
let logical_op_id = crate::provenance::mint_logical_operation_id();
let backend = self.mounts[mount_idx].backend.as_ref();
backend.write_entity(Path::new(&new_file_path), markdown.as_bytes())?;
backend.delete_entity(Path::new(&old_file_path))?;
for (ref_markdown, ref_file_path, _) in &same_mem_writes {
backend.write_entity(Path::new(ref_file_path), ref_markdown.as_bytes())?;
}
// Move the renamed entity's anchor row old_id → new_id in the SAME
// commit as the file move so entity + anchors rewind together and
// resolution finds every anchor under the new id (zero under the
// old). A no-op when the entity had no anchors (byte-identical).
super::stage_anchors_rename(backend, id, &new_id)?;
// An adopted body's origin marks follow the slug in the same commit,
// for the same reason the anchors do: the marks are keyed by slug and
// a rename moves the id while leaving the contributor's bytes alone.
// Following the key here is what lets the read path be exact instead
// of inferring a rename from matching content across slugs.
super::stage_proposal_marks_rename(backend, id, &new_id)?;
// The self-link rewrite moves the renamed entity's own marked bytes,
// under its NEW slug now that the marks have followed the key.
super::stage_proposal_marks_rehash(backend, &new_id, &entity.sections, &next.sections)?;
for (ref_id, before, after) in &same_mem_rehash {
super::stage_proposal_marks_rehash(backend, ref_id, before, after)?;
}
// The check ledger follows the id too. It is workspace state, not
// mem content, so it cannot ride the commit; it is carried before
// the commit so a ledger that refuses stops the rename the way a
// refused check stops a record, and a commit that then fails
// leaves at worst a carried line under an id that never came to
// be — never a renamed entity that lost its checks.
self.carry_checks_across_rename(id, &new_id)?;
let commit_subject = format!("memstead: rename {} → {new_id}", id);
let mut ctx = self.commit_context(
Some("rename_entity"),
actor,
client.cloned(),
note.map(String::from),
);
ctx.logical_operation_id = Some(logical_op_id.as_str());
let write_id = backend.commit(&commit_subject, &ctx)?;
backend.append_provenance(
&Provenance::new(
std::time::SystemTime::now(),
ProvenanceKind::Rename,
Some(new_id.to_string()),
actor,
client.cloned(),
note.map(String::from),
)
.with_role(self.current_role)
.with_identity(self.current_identity.clone())
.with_logical_operation_id(logical_op_id.clone()),
)?;
self.record_self_write(mount_idx, &write_id);
let stamp_warnings = self.stamp_mutation_versions(mount_idx);
let mut peer_stamp_warnings: Vec<WarningHint> = Vec::new();
// ----- Apply: cross-mem peer mems (parent-pinned) -----
// Snapshot every peer mem's current head before any peer
// writes begin. The snapshots are pinned through
// `commit_with_expected_parent`, so a sibling writer that
// advances a peer mem's head between snapshot and commit
// aborts the commit with `BackendError::ParentMismatch`. The
// engine layer maps this to `RENAME_PARTIAL_FAILURE` (the
// source mem has already committed by this point — its
// state is durable; only the failed peer's writes are lost).
// Folder and archive backends inherit the trait's default
// `commit_with_expected_parent` (which ignores the parent
// and delegates to `commit`); the git-branch backend
// overrides to check the per-mem branch tip.
let mut peer_snapshots: std::collections::BTreeMap<String, Option<String>> =
std::collections::BTreeMap::new();
for plan in peer_plans.values() {
let peer_backend = self.mounts[plan.mount_idx].backend.as_ref();
let snapshot = peer_backend.current_head()?;
peer_snapshots.insert(plan.mem.clone(), snapshot);
}
// Track which mems have already committed in this logical
// operation. On a peer-commit failure, the engine surfaces
// the partial-state envelope so the agent can decide whether
// to retry, reconcile, or accept.
let mut committed_mems: Vec<String> = vec![mem.clone()];
for plan in peer_plans.values() {
let peer_backend = self.mounts[plan.mount_idx].backend.as_ref();
for (ref_markdown, ref_file_path, _) in &plan.writes {
peer_backend.write_entity(Path::new(ref_file_path), ref_markdown.as_bytes())?;
}
// Each peer mem holds its own record, so its marks are carried
// into its own commit.
for (ref_id, before, after) in &plan.rehash {
super::stage_proposal_marks_rehash(peer_backend, ref_id, before, after)?;
}
let peer_commit_subject = format!(
"memstead: rename {} → {new_id} (cross-mem rewrite in `{}`)",
id, plan.mem
);
let mut peer_ctx = self.commit_context(
Some("rename_entity"),
actor,
client.cloned(),
note.map(String::from),
);
peer_ctx.logical_operation_id = Some(logical_op_id.as_str());
let expected = peer_snapshots.get(&plan.mem).cloned().unwrap_or(None);
let peer_commit_result = peer_backend.commit_with_expected_parent(
&peer_commit_subject,
&peer_ctx,
expected.as_deref(),
);
let peer_write_id = match peer_commit_result {
Ok(sha) => sha,
Err(crate::backend::BackendError::ParentMismatch { .. }) => {
return Err(EngineError::RenamePartialFailure {
committed_mems: std::mem::take(&mut committed_mems),
failed_mem: plan.mem.clone(),
failure_cause: "drift".to_string(),
});
}
Err(e) => return Err(e.into()),
};
peer_backend.append_provenance(
&Provenance::new(
std::time::SystemTime::now(),
ProvenanceKind::Rename,
Some(new_id.to_string()),
actor,
client.cloned(),
note.map(String::from),
)
.with_role(self.current_role)
.with_identity(self.current_identity.clone())
.with_logical_operation_id(logical_op_id.clone()),
)?;
self.record_self_write(plan.mount_idx, &peer_write_id);
// Per peer mount, not only the source mem: a rename touches every
// pinned peer, and each one's config write can meet its own
// intervening writer.
peer_stamp_warnings.extend(self.stamp_mutation_versions(plan.mount_idx));
committed_mems.push(plan.mem.clone());
}
// ----- Re-parse and push -----
let parse_result = parse_markdown(&markdown, &new_file_path, type_def.as_ref(), &mem)
.map_err(|e| EngineError::ParseAfterWrite(e.to_string()))?;
let content_hash = parse_result.entity.content_hash.clone();
let mut parse_results = vec![parse_result];
for (ref_markdown, ref_file_path, ref_type_def) in &same_mem_writes {
let pr = parse_markdown(ref_markdown, ref_file_path, ref_type_def.as_ref(), &mem)
.map_err(|e| EngineError::ParseAfterWrite(e.to_string()))?;
parse_results.push(pr);
}
for plan in peer_plans.values() {
for (ref_markdown, ref_file_path, ref_type_def) in &plan.writes {
let pr = parse_markdown(
ref_markdown,
ref_file_path,
ref_type_def.as_ref(),
&plan.mem,
)
.map_err(|e| EngineError::ParseAfterWrite(e.to_string()))?;
parse_results.push(pr);
}
}
// Residual-stub demotion for ReadOnly cross-mem referrers.
// The engine can't rewrite ReadOnly-mount markdown, so the
// wiki-links there still point at the OLD slug after the
// rename. To keep `incoming(<new_id>)` aligned with what a
// fresh boot would produce (and to surface the dangling
// reference to the agent), we demote the OLD-id store entry
// to a stub instead of removing it outright. Its surviving
// `in_edges` from the ReadOnly mount remain valid — they
// point at the now-stub at the old id.
//
// When no ReadOnly referrers exist, the old entry is
// removed cleanly (the existing Write-path behaviour).
let mut outcome_warnings: Vec<WarningHint> = Vec::new();
outcome_warnings.extend(stamp_warnings);
outcome_warnings.extend(peer_stamp_warnings);
// Reload-before-operation drift notice, surfaced first.
outcome_warnings.append(&mut drift_warnings);
// Title↔slug divergence of the NEW title — same visibility
// contract as create's.
if !derivation.dropped_chars.is_empty() {
outcome_warnings.push(WarningHint::TitleCharsDroppedFromSlug {
title: args.new_title.trim().to_string(),
dropped_chars: derivation.dropped_chars.clone(),
slug: new_slug.clone(),
});
}
if readonly_referrers.is_empty() {
self.store.remove(id);
} else {
// Sever outgoing edges from the old id (the entity is
// gone — its body and relations live at the new id now)
// and replace the node with a stub at the same id. The
// in_edges from the ReadOnly mount survive untouched.
self.store.remove_edges_from(id);
self.store.upsert(
id.clone(),
make_stub(
id,
crate::entity::StubKind::Residual {
since_commit: write_id.clone(),
readonly_referrers: readonly_referrers.clone(),
},
),
);
outcome_warnings.push(WarningHint::ResidualStubForReadOnlyReferrers {
id: id.clone(),
referrers: readonly_referrers,
});
}
let fallback = engine_fallback_type();
// Incremental (flywheel W8/01): the touched set is the OLD id
// (its document must leave the index) plus every re-parsed
// entity — the renamed entity at its new id and each rewritten
// referrer.
let mut touched: Vec<crate::EntityId> = parse_results
.iter()
.map(|pr| pr.entity.id.clone())
.collect();
touched.push(id.clone());
push_entities_into_store(&mut self.store, parse_results, fallback.as_ref(), None);
crate::entity::store_builder::remap_alias_target_edge_sources(
&mut self.store,
&self.schemas,
);
self.invalidate_communities();
self.maintain_search_indexes(&touched);
// `require_notes` provenance nudge — single engine-level
// enforcement point. Only reached on the real-rename path; the
// slug-noop short-circuit returns early above with an empty
// `write_id` and never demands a note.
if let Some(w) = self.note_missing_warning("rename_entity", note) {
outcome_warnings.push(w);
}
Ok(RenameEntityOutcome {
old_id: id.clone(),
new_id,
old_path: old_file_path,
new_path: new_file_path,
content_hash,
write_id,
warnings: outcome_warnings,
})
}
}
#[cfg(test)]
mod tests;