1use std::path::PathBuf;
2
3use clap::{Parser, ValueEnum};
4use serde_json::json;
5
6use crate::CliError;
7use crate::output::{ExitKind, print_json, print_markdown};
8use crate::setup::{CliContext, CliEngine};
9
10#[derive(Parser, Debug)]
27pub struct Args {
28 #[arg(long, value_enum, default_value_t = Format::Markdown)]
38 pub format: Format,
39
40 #[arg(long, short = 'o', value_name = "PATH")]
46 pub output: Option<PathBuf>,
47
48 #[arg(long = "mem", value_name = "NAME")]
57 pub mem_name: Option<String>,
58
59 #[arg(long)]
69 pub self_contained: bool,
70
71 #[arg(long = "base-url", value_name = "URL")]
77 pub base_url: Option<String>,
78}
79
80#[derive(ValueEnum, Clone, Copy, Debug)]
81pub enum Format {
82 Markdown,
84 Mem,
86 Json,
88 Html,
91 LlmsTxt,
95}
96
97pub fn run(ctx: &CliContext, args: Args) -> anyhow::Result<()> {
98 if matches!(args.format, Format::Json) {
99 return run_json(ctx, args);
100 }
101 if matches!(args.format, Format::Html) {
102 return run_html(ctx, args);
103 }
104 if matches!(args.format, Format::LlmsTxt) {
105 return run_llms_txt(ctx, args);
106 }
107 match ctx.cli_engine()? {
108 #[cfg(feature = "mem-repo")]
109 CliEngine::MemRepo(engine) => match args.format {
110 Format::Markdown => run_markdown(ctx, &engine, args.mem_name.as_deref()),
111 Format::Mem => run_mem(ctx, &engine, args),
112 Format::Json => unreachable!("dispatched to run_json above"),
113 Format::Html => unreachable!("dispatched to run_html above"),
114 Format::LlmsTxt => unreachable!("dispatched to run_llms_txt above"),
115 },
116 CliEngine::Filesystem(engine) => match args.format {
117 Format::Markdown => Err(CliError::new(
123 ExitKind::Validation,
124 "INVALID_INPUT",
125 "--format markdown is not yet supported on filesystem-mem `memstead export` — entities are already on disk in their canonical form",
126 )
127 .into()),
128 Format::Mem => run_mem_filesystem(ctx, &engine, args),
129 Format::Json => unreachable!("dispatched to run_json above"),
130 Format::Html => unreachable!("dispatched to run_html above"),
131 Format::LlmsTxt => unreachable!("dispatched to run_llms_txt above"),
132 },
133 }
134}
135
136const JSON_EXPORT_FORMAT: &str = "memstead-export/v1";
140
141fn run_json(ctx: &CliContext, args: Args) -> anyhow::Result<()> {
150 if args.output.is_some() {
154 return Err(CliError::new(
155 ExitKind::Validation,
156 "INVALID_INPUT",
157 "--output applies only to --format mem — the JSON document goes to stdout; redirect it instead",
158 )
159 .into());
160 }
161
162 let cli_engine = ctx.cli_engine()?;
163 let engine = cli_engine.base();
164
165 let all_names: Vec<String> = engine.mem_names().into_iter().map(String::from).collect();
166 let selected: Vec<String> = match &args.mem_name {
170 Some(name) => {
171 if !all_names.iter().any(|n| n == name) {
172 return Err(CliError::new(
173 ExitKind::NotFound,
174 "UNKNOWN_MEM",
175 format!(
176 "unknown mem '{name}' — loaded mems: {}",
177 all_names.join(", ")
178 ),
179 )
180 .with_details(json!({ "mem": name, "loaded": all_names }))
181 .into());
182 }
183 vec![name.clone()]
184 }
185 None => all_names
186 .iter()
187 .filter(|n| engine.mem_router().is_writable(n))
188 .cloned()
189 .collect(),
190 };
191
192 let mut mems = serde_json::Map::new();
193 for mem_name in &selected {
194 let schema_pin = engine
197 .mounts_with_optional_config()
198 .find(|(name, _)| name == mem_name)
199 .and_then(|(_, c)| c)
200 .and_then(|c| c.schema.as_ref())
201 .map(|s| s.to_string());
202
203 let mut entities: Vec<&memstead_base::Entity> = engine
204 .store()
205 .all_entities()
206 .filter(|e| !e.stub && e.mem == *mem_name)
207 .collect();
208 entities.sort_by(|a, b| a.id.as_ref().cmp(b.id.as_ref()));
209
210 let envelopes: Vec<serde_json::Value> = entities
211 .iter()
212 .map(|entity| {
213 let body = memstead_base::render::render_entity_markdown(entity, None);
214 let tokens = memstead_base::chunking::estimate_tokens(&body);
215 let outgoing = engine.store().outgoing(&entity.id);
216 memstead_base::render::build_entity_envelope(
219 entity,
220 tokens,
221 None,
222 None,
223 None,
224 engine.mem_origin_class(entity.id.mem()),
225 outgoing,
226 None,
227 None,
228 None,
229 )
230 })
231 .collect();
232
233 let mut group = serde_json::Map::new();
234 if let Some(s) = schema_pin {
235 group.insert("schema".to_string(), json!(s));
236 }
237 group.insert(
238 "read_only".to_string(),
239 json!(!engine.mem_router().is_writable(mem_name)),
240 );
241 group.insert("entity_count".to_string(), json!(envelopes.len()));
242 group.insert("entities".to_string(), serde_json::Value::Array(envelopes));
243 mems.insert(mem_name.clone(), serde_json::Value::Object(group));
244 }
245
246 print_json(&json!({
247 "format": JSON_EXPORT_FORMAT,
248 "mems": mems,
249 }))
250}
251
252#[cfg(feature = "mem-repo")]
253fn run_markdown(
254 ctx: &CliContext,
255 engine: &memstead_base::Engine,
256 mem_filter: Option<&str>,
257) -> anyhow::Result<()> {
258 let result = engine
264 .export_markdown(mem_filter, None)
265 .map_err(CliError::from_engine_op)?;
266
267 if ctx.json {
268 let mut body = json!({
269 "written": result.written,
270 "unchanged": result.unchanged,
271 });
272 if !result.skipped_mounts.is_empty() {
273 body["skipped_mounts"] = serde_json::to_value(&result.skipped_mounts)
274 .unwrap_or_else(|_| serde_json::Value::Array(Vec::new()));
275 }
276 if !result.refused_entities.is_empty() {
277 body["refused_entities"] = serde_json::to_value(&result.refused_entities)
278 .unwrap_or_else(|_| serde_json::Value::Array(Vec::new()));
279 }
280 print_json(&body)?;
281 } else {
282 let mut block = format!(
283 "# Export — markdown\n\n- Written: {}\n- Unchanged: {}",
284 result.written, result.unchanged,
285 );
286 if !result.skipped_mounts.is_empty() {
287 block.push_str("\n\n## Skipped mounts\n");
288 for m in &result.skipped_mounts {
289 block.push_str(&format!(
290 "\n- `{}` — backend `{}` ({}); use `--format mem` for archive export",
291 m.mem, m.active_backend, m.reason,
292 ));
293 }
294 }
295 if !result.refused_entities.is_empty() {
299 block.push_str("\n\n## Refused entities\n");
300 for r in &result.refused_entities {
301 block.push_str(&format!("\n- `{}` [{}] — {}", r.id, r.reason, r.detail));
302 }
303 }
304 print_markdown(&block);
305 }
306 Ok(())
307}
308
309#[cfg(feature = "mem-repo")]
310fn run_mem(ctx: &CliContext, engine: &memstead_base::Engine, args: Args) -> anyhow::Result<()> {
311 let mem_name = resolve_mem_name(engine, args.mem_name)?;
312 let config = engine
318 .mem_configs_named()
319 .find(|(name, _)| *name == mem_name)
320 .map(|(_, c)| c)
321 .ok_or_else(|| {
322 CliError::new(
323 ExitKind::NotFound,
324 "UNKNOWN_MEM",
325 format!("mem config not found for '{mem_name}'"),
326 )
327 })?;
328
329 let output = match args.output {
330 Some(p) => p,
331 None => default_output_path(&mem_name, config)?,
332 };
333
334 let mut result = engine
335 .export_mem(&mem_name, &output)
336 .map_err(CliError::from_engine_op)?;
337
338 let dropped = if args.self_contained {
344 let self_contained = make_self_contained_on_disk(&output)?;
345 result.size_bytes = self_contained.bytes.len() as u64;
346 result.dangling_cross_mem_edges.clear();
347 Some(self_contained.dropped)
348 } else {
349 None
350 };
351
352 let dangling = &result.dangling_cross_mem_edges;
357
358 if ctx.json {
359 let mut warnings: Vec<_> = dangling
360 .iter()
361 .map(|e| {
362 json!({
363 "code": "DANGLING_CROSS_MEM_EDGE_IN_EXPORT",
364 "entity": e.entity_path,
365 "target_id": e.target_id,
366 "target_mem": e.target_mem,
367 })
368 })
369 .collect();
370 if let Some(dropped) = &dropped {
371 warnings.extend(dropped.iter().map(|e| {
372 json!({
373 "code": "CROSS_MEM_EDGE_DROPPED",
374 "entity": e.entity_path,
375 "target_id": e.target_id,
376 "target_mem": e.target_mem,
377 })
378 }));
379 }
380 warnings.extend(result.unterminated_fence_entities.iter().map(|id| {
381 json!({
382 "code": "UNTERMINATED_FENCE_IN_EXPORT",
383 "entity": id,
384 })
385 }));
386 print_json(&json!({
387 "archive_path": result.archive_path,
388 "name": result.name,
389 "version": result.version,
390 "entity_count": result.entity_count,
391 "size_bytes": result.size_bytes,
392 "self_contained": args.self_contained,
393 "warnings": warnings,
394 }))?;
395 } else {
396 let mut block = format!(
397 "# Exported `{}` v{}\n\n- Archive: `{}`\n- Entities: {}\n- Size: {} bytes",
398 result.name,
399 result.version,
400 result.archive_path,
401 result.entity_count,
402 result.size_bytes,
403 );
404 if args.self_contained {
405 block.push_str("\n- Self-contained: yes");
406 }
407 if !result.unterminated_fence_entities.is_empty() {
410 block.push_str(
411 "\n\n## Entities `install` will refuse\n\nEach ends a section inside an \
412 unterminated code fence, which absorbed the sections after it. Repair through \
413 the engine (replace the absorbing section) and re-export.\n",
414 );
415 for id in &result.unterminated_fence_entities {
416 block.push_str(&format!("\n- `{id}` [UNTERMINATED_FENCE_IN_EXPORT]"));
417 }
418 }
419 if !dangling.is_empty() {
420 block.push_str("\n\n## Warnings\n");
421 for e in dangling {
422 block.push_str(&format!(
423 "\n- **DANGLING_CROSS_MEM_EDGE_IN_EXPORT**: `{}` → `{}` (mem `{}`) — \
424 target lives outside this archive; `memstead install` will reject it unless \
425 mem `{}` is also present.",
426 e.entity_path, e.target_id, e.target_mem, e.target_mem,
427 ));
428 }
429 }
430 if let Some(dropped) = &dropped
431 && !dropped.is_empty()
432 {
433 block.push_str("\n\n## Dropped cross-mem edges\n");
434 for e in dropped {
435 block.push_str(&format!(
436 "\n- **CROSS_MEM_EDGE_DROPPED**: `{}` → `{}` (mem `{}`): the relationship \
437 row does not travel; a body wiki-link to the same target still does.",
438 e.entity_path, e.target_id, e.target_mem,
439 ));
440 }
441 }
442 print_markdown(&block);
443 }
444 Ok(())
445}
446
447fn make_self_contained_on_disk(
450 path: &std::path::Path,
451) -> anyhow::Result<memstead_base::validator::SelfContainedArchive> {
452 let bytes = std::fs::read(path).map_err(|e| {
453 CliError::new(
454 ExitKind::Generic,
455 crate::INTERNAL_CODE,
456 format!("read {}: {e}", path.display()),
457 )
458 })?;
459 let out = memstead_base::validator::make_archive_self_contained(&bytes).map_err(|e| {
460 CliError::new(
461 ExitKind::Generic,
462 "ARCHIVE_VALIDATION_FAILED",
463 format!("self-contained re-pack of {}: {e}", path.display()),
464 )
465 })?;
466 std::fs::write(path, &out.bytes).map_err(|e| {
467 CliError::new(
468 ExitKind::Generic,
469 crate::INTERNAL_CODE,
470 format!("write {}: {e}", path.display()),
471 )
472 })?;
473 Ok(out)
474}
475
476#[cfg(feature = "mem-repo")]
477fn resolve_mem_name(
478 engine: &memstead_base::Engine,
479 explicit: Option<String>,
480) -> anyhow::Result<String> {
481 if let Some(name) = explicit {
482 return Ok(name);
483 }
484 let writable: Vec<String> = engine
488 .mounts_with_optional_config()
489 .filter(|(name, _)| engine.mem_router().is_writable(name))
490 .map(|(name, _)| name.to_string())
491 .collect();
492
493 match writable.len() {
494 0 => Err(CliError::new(
495 ExitKind::Generic,
496 "NO_WRITABLE_MEM",
497 "no writable mem loaded — nothing to export",
498 )
499 .into()),
500 1 => Ok(writable.into_iter().next().unwrap()),
501 _ => Err(CliError::new(
502 ExitKind::Validation,
503 "AMBIGUOUS_MEM",
504 format!(
505 "multiple writable mems loaded ({}); pass --mem <name>",
506 writable.join(", ")
507 ),
508 )
509 .with_details(json!({ "mems": writable }))
510 .into()),
511 }
512}
513
514fn run_mem_filesystem(
521 ctx: &CliContext,
522 engine: &memstead_base::Engine,
523 args: Args,
524) -> anyhow::Result<()> {
525 let workspace_mem = engine
526 .mem_names()
527 .into_iter()
528 .next()
529 .map(String::from)
530 .unwrap_or_default();
531 if let Some(name) = args.mem_name.as_deref()
532 && name != workspace_mem
533 {
534 return Err(CliError::new(
535 ExitKind::NotFound,
536 "UNKNOWN_MEM",
537 format!(
538 "filesystem-mem is single-mem: workspace mem is `{workspace_mem}`, --mem `{name}` does not match"
539 ),
540 )
541 .into());
542 }
543
544 let bytes = engine
556 .export_mem_to_bytes(&workspace_mem)
557 .map_err(CliError::from_engine_op)?;
558
559 let output = match args.output {
560 Some(p) => p,
561 None => {
562 PathBuf::from(format!(
567 "{workspace_mem}.{}",
568 memstead_schema::ARCHIVE_EXTENSION
569 ))
570 }
571 };
572
573 std::fs::write(&output, &bytes).map_err(|e| {
574 CliError::new(
575 ExitKind::Generic,
576 crate::INTERNAL_CODE,
577 format!("write {}: {e}", output.display()),
578 )
579 })?;
580 let dropped = if args.self_contained {
581 Some(make_self_contained_on_disk(&output)?.dropped)
582 } else {
583 None
584 };
585 let size_bytes = std::fs::metadata(&output)
586 .map(|m| m.len() as usize)
587 .unwrap_or(bytes.len());
588 let entity_count = engine
592 .store()
593 .all_entities()
594 .filter(|e| !e.stub && e.id.mem() == workspace_mem)
595 .count();
596
597 if ctx.json {
598 let warnings: Vec<_> = dropped
599 .iter()
600 .flatten()
601 .map(|e| {
602 json!({
603 "code": "CROSS_MEM_EDGE_DROPPED",
604 "entity": e.entity_path,
605 "target_id": e.target_id,
606 "target_mem": e.target_mem,
607 })
608 })
609 .collect();
610 print_json(&json!({
611 "archive_path": output.to_string_lossy(),
612 "name": workspace_mem,
613 "entity_count": entity_count,
614 "size_bytes": size_bytes,
615 "self_contained": args.self_contained,
616 "warnings": warnings,
617 }))?;
618 } else {
619 let mut block = format!(
620 "# Exported `{workspace_mem}`\n\n- Archive: `{}`\n- Entities: {}\n- Size: {} bytes",
621 output.display(),
622 entity_count,
623 size_bytes,
624 );
625 if args.self_contained {
626 block.push_str("\n- Self-contained: yes");
627 let n = dropped.as_ref().map(|d| d.len()).unwrap_or(0);
628 if n > 0 {
629 block.push_str(&format!("\n- Cross-mem edges dropped: {n}"));
630 }
631 }
632 print_markdown(&block);
633 }
634 Ok(())
635}
636
637#[cfg(feature = "mem-repo")]
638fn default_output_path(
639 mem_name: &str,
640 config: &memstead_schema::MemConfig,
641) -> anyhow::Result<PathBuf> {
642 let version = config.version.as_ref().ok_or_else(|| {
643 CliError::from_engine_op(memstead_base::EngineError::MemConfigIncomplete {
650 mem: mem_name.to_string(),
651 missing_fields: vec!["version".to_string()],
652 })
653 })?;
654 let filename = format!(
657 "{mem_name}-{version}.{}",
658 memstead_schema::ARCHIVE_EXTENSION
659 );
660 Ok(PathBuf::from(filename))
661}
662
663fn run_llms_txt(ctx: &CliContext, args: Args) -> anyhow::Result<()> {
675 let engine_holder = ctx.cli_engine()?;
676 let engine = engine_holder.base();
677 let mem = resolve_single_mem(engine, args.mem_name.as_deref())?;
678
679 let ctx_opts = memstead_base::engine::export_llms_txt::LlmsTxtContext {
680 authority: None,
681 href_prefix: args
682 .base_url
683 .clone()
684 .map(|u| u.trim_end_matches('/').to_string())
685 .unwrap_or_default(),
686 wider_project: Vec::new(),
687 };
688 let doc = engine
689 .render_llms_txt(&mem, &ctx_opts)
690 .map_err(CliError::from_engine_op)?;
691
692 match &args.output {
693 Some(path) => {
694 std::fs::write(path, &doc).map_err(|e| {
695 CliError::new(
696 ExitKind::Generic,
697 "IO_ERROR",
698 format!("write {}: {e}", path.display()),
699 )
700 })?;
701 if ctx.json {
702 print_json(&serde_json::json!({
703 "mem": mem,
704 "written": path.display().to_string(),
705 "bytes": doc.len(),
706 }))?;
707 } else {
708 println!("Wrote {} ({} bytes)", path.display(), doc.len());
709 }
710 }
711 None => print!("{doc}"),
715 }
716 Ok(())
717}
718
719fn resolve_single_mem(
723 engine: &memstead_base::Engine,
724 requested: Option<&str>,
725) -> Result<String, CliError> {
726 if let Some(m) = requested {
727 return Ok(m.to_string());
728 }
729 let writables: Vec<String> = engine
730 .writable_mem_names()
731 .iter()
732 .map(|s| s.to_string())
733 .collect();
734 match writables.as_slice() {
735 [one] => Ok(one.clone()),
736 [] => Err(CliError::new(
737 ExitKind::Validation,
738 "INVALID_INPUT",
739 "no writable mem loaded — pass --mem <name>",
740 )),
741 _ => Err(CliError::new(
742 ExitKind::Validation,
743 "INVALID_INPUT",
744 format!(
745 "multiple writable mems loaded ({}) — pass --mem <name>",
746 writables.join(", ")
747 ),
748 )),
749 }
750}
751
752fn run_html(ctx: &CliContext, args: Args) -> anyhow::Result<()> {
758 let engine_holder = ctx.cli_engine()?;
759 let engine = engine_holder.base();
760 let mem = match &args.mem_name {
763 Some(m) => m.clone(),
764 None => {
765 let writables: Vec<String> = engine
766 .writable_mem_names()
767 .iter()
768 .map(|s| s.to_string())
769 .collect();
770 match writables.as_slice() {
771 [one] => one.clone(),
772 [] => {
773 return Err(CliError::new(
774 ExitKind::Validation,
775 "INVALID_INPUT",
776 "no writable mem loaded — pass --mem <name>",
777 )
778 .into());
779 }
780 _ => {
781 return Err(CliError::new(
782 ExitKind::Validation,
783 "INVALID_INPUT",
784 format!(
785 "multiple writable mems loaded ({}) — pass --mem <name>",
786 writables.join(", ")
787 ),
788 )
789 .into());
790 }
791 }
792 }
793 };
794 let now = time::OffsetDateTime::now_utc();
795 let export_date = format!(
796 "{:04}-{:02}-{:02}",
797 now.year(),
798 u8::from(now.month()),
799 now.day()
800 );
801 let html = engine
802 .render_html_export(&mem, &export_date)
803 .map_err(CliError::from_engine_op)?;
804 let out_path = args
805 .output
806 .clone()
807 .unwrap_or_else(|| PathBuf::from(format!("{mem}.html")));
808 std::fs::write(&out_path, &html).map_err(|e| {
809 CliError::new(
810 ExitKind::Generic,
811 "IO_ERROR",
812 format!("write {}: {e}", out_path.display()),
813 )
814 })?;
815 if ctx.json {
816 print_json(&serde_json::json!({
817 "format": "html",
818 "mem": mem,
819 "path": out_path,
820 "bytes": html.len(),
821 "exported": export_date,
822 }))?;
823 } else {
824 print_markdown(&format!(
825 "# HTML export\n\n- Mem: `{mem}`\n- File: `{}`\n- Size: {} bytes\n- Exported: {export_date}\n\nSelf-contained — open it from anywhere, no server needed.\n",
826 out_path.display(),
827 html.len()
828 ));
829 }
830 Ok(())
831}
832
833#[cfg(test)]
834mod tests {
835 use super::*;
836 use clap::Parser;
837
838 #[test]
841 fn export_mem_selection_flag_is_mem_not_mem_name() {
842 let parsed = Args::try_parse_from(["export", "--mem", "specs", "--format", "mem"]).unwrap();
843 assert_eq!(parsed.mem_name.as_deref(), Some("specs"));
844 assert!(
845 Args::try_parse_from(["export", "--mem-name", "specs"]).is_err(),
846 "the retired --mem-name flag must not parse"
847 );
848 }
849}