Skip to content

Dodaios is an open-source agent OS for your desktop: it runs your coding agents, browser and apps by voice or chat, on your own machine. Formerly Personal Jarvis.

feat(ultrawiki): export the projection as a real Obsidian vault

Ruben Lütke714d8a63e38c
diff --git a/jarvis/ultrawiki/vault_export.py b/jarvis/ultrawiki/vault_export.py
new file mode 100644
index 0000000000..8c0c26d339
--- /dev/null
+++ b/jarvis/ultrawiki/vault_export.py
@@ -0,0 +1,498 @@
+"""Write the UltraWiki projection out as a real Obsidian vault.
+
+The knowledge lives in a database, which is right for retrieval and useless
+for reading with your own eyes. This module projects it onto disk as plain
+Markdown with ``[[wikilinks]]``, so any Markdown tool — Obsidian above all —
+can browse the same knowledge, follow the links, and draw its own graph.
+
+**One-way by design.** The three generated directories are rewritten on every
+export; ``My notes/`` is created once and never touched again, and is what the
+existing ``obsidian-vault`` connector reads back in. That gives two-way flow
+without a merge problem: generated content is disposable, authored content is
+never written by us.
+
+**Deletion is fail-closed.** A stale note is removed only when its front
+matter says we wrote it. A file that a person put into ``Topics/`` by hand
+survives every export, because "it is in our folder" is not proof that it is
+ours.
+
+Filenames are the whole cross-platform surface. Measured on a real corpus, 14
+of 947 topic labels contain a character that is illegal or path-changing in a
+filename and 12 more differ only in case — which is the same file on Windows
+and macOS. :func:`safe_note_name` and :func:`assign_note_names` are therefore
+pure, deterministic, and tested independently of the OS the tests run on.
+"""
+
+from __future__ import annotations
+
+import hashlib
+import json
+import logging
+import os
+import tempfile
+import unicodedata
+from collections.abc import Iterable
+from dataclasses import dataclass
+from pathlib import Path
+from typing import Any, Protocol
+
+log = logging.getLogger(__name__)
+
+__all__ = [
+ "GENERATED_DIRS",
+ "GENERATED_MARKER",
+ "MANIFEST_NAME",
+ "MOMENTS_DIR",
+ "OVERVIEW_DIR",
+ "TOPICS_DIR",
+ "USER_DIR",
+ "ExportResult",
+ "assign_note_names",
+ "export_vault",
+ "safe_note_name",
+]
+
+TOPICS_DIR = "Topics"
+MOMENTS_DIR = "Moments"
+OVERVIEW_DIR = "Overview"
+
+#: Rewritten on every export. Everything else in the vault is the user's.
+GENERATED_DIRS: tuple[str, ...] = (TOPICS_DIR, MOMENTS_DIR, OVERVIEW_DIR)
+
+#: Created once, never written to again — and read back by the
+#: ``obsidian-vault`` connector, which is how notes flow the other way.
+USER_DIR = "My notes"
+
+#: The front-matter line that authorises deletion. No marker, no delete.
+GENERATED_MARKER = "generated_by: ultrawiki"
+
+#: What the last export wrote: {vault-relative POSIX path: content hash}.
+#:
+#: Without it, deciding "has this note changed" means reading all of them
+#: back, which measured 2.5 ms per file on a machine with a live virus
+#: scanner — 24 s for a re-export of a corpus where nothing changed at all,
+#: four times longer than writing the whole vault from scratch.
+#:
+#: It is an optimisation, never the authority: a missing or unreadable
+#: manifest falls back to the front-matter marker, so the export still prunes
+#: correctly on the first run after an update or after someone deletes it.
+MANIFEST_NAME = ".ultrawiki-manifest.json"
+_MANIFEST_VERSION = 1
+
+#: Illegal on Windows, and `/` would silently create directories on POSIX.
+_UNSAFE_CHARS = '<>:"/\\|?*'
+
+#: Reserved DOS device names: a file called `CON.md` cannot be created on
+#: Windows at all, and the failure is obscure.
+_RESERVED_NAMES = frozenset(
+ {"con", "prn", "aux", "nul"}
+ | {f"com{i}" for i in range(1, 10)}
+ | {f"lpt{i}" for i in range(1, 10)}
+)
+
+#: Leaves room for the collision suffix well inside every filesystem limit.
+_MAX_STEM = 120
+
+#: Bucket for moments whose timestamp could not be read.
+_UNDATED = "undated"
+
+
+class _Nameable(Protocol):
+ key: str
+ label: str
+
+
+@dataclass(frozen=True, slots=True)
+class ExportResult:
+ """What one export wrote, for the honest report the UI shows."""
+
+ root: Path
+ topics: int
+ moments: int
+ written: int
+ unchanged: int
+ removed: int
+
+
+def safe_note_name(label: str) -> str:
+ """A filename stem that is legal and identical on Windows, macOS and Linux.
+
+ Not merely defensive: on a real corpus a slash in a topic name would have
+ created directories on Linux and raised on Windows, and a trailing dot
+ would be dropped by Windows so the file on disk no longer matches the name
+ we recorded — which quietly breaks the orphan comparison and makes the
+ export delete-and-rewrite forever.
+ """
+ text = unicodedata.normalize("NFC", label)
+ cleaned = "".join(
+ "-" if (char in _UNSAFE_CHARS or ord(char) < 32) else char for char in text
+ )
+ cleaned = " ".join(cleaned.split())
+ # A name must not END on a separator, a dot or a space: the first is ugly,
+ # the second and third are silently dropped by Windows.
+ cleaned = cleaned.rstrip(" .-_")
+ if not cleaned:
+ cleaned = "untitled"
+ if len(cleaned) > _MAX_STEM:
+ digest = hashlib.sha256(label.encode("utf-8")).hexdigest()[:8]
+ cleaned = f"{cleaned[:_MAX_STEM].rstrip(' .-_')}-{digest}"
+ if cleaned.split(".")[0].lower() in _RESERVED_NAMES:
+ cleaned = f"{cleaned}-note"
+ return cleaned
+
+
+def assign_note_names(
+ items: Iterable[_Nameable], taken: set[str] | None = None
+) -> dict[str, str]:
+ """Collision-free filename stems, keyed by the item's key.
+
+ Names are unique across the WHOLE vault, not per directory, because an
+ Obsidian ``[[wikilink]]`` resolves by name: two notes sharing one name in
+ different folders make every link to that name ambiguous.
+
+ Collisions resolve in key order so a re-export produces the same names —
+ otherwise every export would churn the vault and Obsidian would lose its
+ per-note state.
+ """
+ used = {name.lower() for name in (taken or set())}
+ names: dict[str, str] = {}
+ for item in sorted(items, key=lambda entry: entry.key):
+ stem = safe_note_name(item.label)
+ candidate = stem
+ suffix = 2
+ while candidate.lower() in used:
+ candidate = f"{stem}-{suffix}"
+ suffix += 1
+ used.add(candidate.lower())
+ names[item.key] = candidate
+ return names
+
+
+@dataclass(frozen=True, slots=True)
+class _MomentName:
+ """Adapter so moments go through the same naming rules as topics."""
+
+ key: str
+ label: str
+
+
+def _front_matter(fields: dict[str, Any]) -> str:
+ lines = ["---", GENERATED_MARKER]
+ for name, value in fields.items():
+ lines.append(f"{name}: {value}")
+ lines.append("---")
+ return "\n".join(lines)
+
+
+def _link(name: str) -> str:
+ return f"[[{name}]]"
+
+
+def _load_manifest(vault: Path) -> dict[str, str]:
+ """What the previous export wrote, or an empty map if that is unknowable."""
+ path = vault / MANIFEST_NAME
+ try:
+ data = json.loads(path.read_text(encoding="utf-8"))
+ except (OSError, json.JSONDecodeError):
+ return {}
+ if not isinstance(data, dict) or data.get("version") != _MANIFEST_VERSION:
+ return {}
+ files = data.get("files")
+ return files if isinstance(files, dict) else {}
+
+
+def _save_manifest(vault: Path, files: dict[str, str]) -> None:
+ payload = json.dumps(
+ {"version": _MANIFEST_VERSION, "files": files},
+ ensure_ascii=False,
+ indent=0,
+ sort_keys=True,
+ )
+ _write_atomic(vault / MANIFEST_NAME, payload.encode("utf-8"))
+
+
+def _write_if_changed(path: Path, text: str, known_hash: str | None) -> tuple[bool, str]:
+ """Write *text* atomically unless it is already on disk unchanged.
+
+ Skipping unchanged files keeps a re-export from touching every note's
+ mtime — a vault where every file changed on every export re-syncs,
+ re-indexes and backs up in full for no reason. The comparison goes through
+ the manifest hash plus one cheap existence check, never a full read.
+ """
+ payload = text.encode("utf-8")
+ digest = hashlib.sha256(payload).hexdigest()
+ if known_hash == digest and path.exists():
+ return False, digest
+ path.parent.mkdir(parents=True, exist_ok=True)
+ _write_atomic(path, payload)
+ return True, digest
+
+
+def _write_atomic(path: Path, payload: bytes) -> None:
+ path.parent.mkdir(parents=True, exist_ok=True)
+ handle, temp_name = tempfile.mkstemp(dir=str(path.parent), suffix=".tmp")
+ try:
+ with os.fdopen(handle, "wb") as stream:
+ stream.write(payload)
+ stream.flush()
+ os.fsync(stream.fileno())
+ os.replace(temp_name, path)
+ except BaseException:
+ Path(temp_name).unlink(missing_ok=True)
+ raise
+
+
+def _is_ours(path: Path) -> bool:
+ """True only for a file we generated. Anything unreadable counts as NOT
+ ours — the export must never delete a file it could not verify."""
+ try:
+ with path.open("r", encoding="utf-8", errors="strict") as stream:
+ head = stream.read(400)
+ except (OSError, UnicodeDecodeError):
+ return False
+ return GENERATED_MARKER in head
+
+
+def _topic_note(entity: Any, names: dict[str, str], moment_names: dict[str, str],
+ moments: tuple[Any, ...]) -> str:
+ lines = [
+ _front_matter(
+ {
+ "topic_key": entity.key,
+ "mentions": entity.mentions,
+ "first_seen": entity.first_seen,
+ "last_seen": entity.last_seen,
+ }
+ ),
+ "",
+ f"# {entity.label}",
+ "",
+ f"Mentioned {entity.mentions} time(s), "
+ f"{entity.first_seen[:10] or 'unknown'} to {entity.last_seen[:10] or 'unknown'}.",
+ "",
+ ]
+
+ if entity.neighbors:
+ lines.append("## Appears with")
+ lines.append("")
+ for key, shared in entity.neighbors[:30]:
+ target = names.get(key)
+ if target:
+ lines.append(f"- {_link(target)} — {shared} shared moment(s)")
+ lines.append("")
+
+ if moments:
+ lines.append("## Moments")
+ lines.append("")
+ for moment in moments[:200]:
+ name = moment_names.get(str(moment.document_id))
+ date = moment.timestamp_utc[:10]
+ if name:
+ lines.append(f"- {date} — {_link(name)}")
+ lines.append("")
+
+ return "\n".join(lines).rstrip() + "\n"
+
+
+def _moment_note(moment: Any, names: dict[str, str]) -> str:
+ lines = [
+ _front_matter(
+ {
+ "document_id": moment.document_id,
+ "date": moment.timestamp_utc,
+ "source": moment.source_label,
+ }
+ ),
+ "",
+ f"# {moment.title}",
+ "",
+ ]
+ if moment.summary:
+ lines += [moment.summary, ""]
+ if moment.resolution:
+ lines += ["## Outcome", "", moment.resolution, ""]
+
+ topics = [names[key] for key in moment.entity_keys if key in names]
+ if topics:
+ lines += ["## Topics", "", " · ".join(_link(name) for name in topics), ""]
+
+ lines += [
+ "---",
+ "",
+ f"{moment.timestamp_utc[:10]} · {moment.source_label}",
+ ]
+ if moment.permalink:
+ lines.append(f"[Open the original]({moment.permalink})")
+ return "\n".join(lines).rstrip() + "\n"
+
+
+def _overview_topics(projection: Any, names: dict[str, str]) -> str:
+ lines = [
+ _front_matter({"overview": "topics", "count": len(projection.entities)}),
+ "",
+ "# All topics",
+ "",
+ "Everything the knowledge base has a name for, most mentioned first.",
+ "",
+ ]
+ for entity in projection.entities:
+ name = names.get(entity.key)
+ if name:
+ lines.append(f"- {_link(name)} — {entity.mentions}")
+ return "\n".join(lines).rstrip() + "\n"
+
+
+def _overview_timeline(projection: Any, moment_names: dict[str, str]) -> str:
+ buckets: dict[str, list[Any]] = {}
+ for moment in projection.moments:
+ buckets.setdefault(moment.month or _UNDATED, []).append(moment)
+
+ lines = [
+ _front_matter({"overview": "timeline", "months": len(buckets)}),
+ "",
+ "# Timeline",
+ "",
+ "Every moment, newest month first.",
+ "",
+ ]
+ for month in sorted(buckets, reverse=True):
+ lines += [f"## {month}", ""]
+ for moment in buckets[month]:
+ name = moment_names.get(str(moment.document_id))
+ if name:
+ lines.append(f"- {moment.timestamp_utc[:10]} — {_link(name)}")
+ lines.append("")
+ return "\n".join(lines).rstrip() + "\n"
+
+
+def _overview_sources(projection: Any) -> str:
+ counts: dict[str, int] = {}
+ for moment in projection.moments:
+ label = moment.source_label or moment.source_id or "unknown"
+ counts[label] = counts.get(label, 0) + 1
+
+ lines = [
+ _front_matter({"overview": "sources", "count": len(counts)}),
+ "",
+ "# Sources",
+ "",
+ "Where these moments came from.",
+ "",
+ ]
+ for label in sorted(counts, key=lambda name: (-counts[name], name)):
+ lines.append(f"- {label} — {counts[label]} moment(s)")
+ return "\n".join(lines).rstrip() + "\n"
+
+
+def _readme(result_dirs: tuple[str, ...]) -> str:
+ generated = ", ".join(f"`{name}/`" for name in result_dirs)
+ return (
+ _front_matter({"overview": "readme"})
+ + "\n\n"
+ + "# This vault is generated\n\n"
+ + f"Jarvis rewrites {generated} on every export. Edits inside those\n"
+ + "folders are replaced the next time it runs.\n\n"
+ + f"`{USER_DIR}/` is yours. Nothing in this vault ever writes there, and\n"
+ + "Jarvis can read those notes back in as a source.\n\n"
+ + "Start at [[All topics]] or [[Timeline]].\n"
+ )
+
+
+def export_vault(projection: Any, root: str | Path) -> ExportResult:
+ """Write the whole projection to *root* and prune our own stale notes."""
+ vault = Path(root)
+ vault.mkdir(parents=True, exist_ok=True)
+ (vault / USER_DIR).mkdir(exist_ok=True)
+
+ topic_names = assign_note_names(projection.entities)
+ moment_items = [
+ _MomentName(key=str(moment.document_id), label=moment.title)
+ for moment in projection.moments
+ ]
+ # Moments share the namespace with topics so no wikilink is ambiguous.
+ moment_names = assign_note_names(
+ moment_items, taken=set(topic_names.values())
+ )
+
+ previous = _load_manifest(vault)
+ current: dict[str, str] = {}
+ written = 0
+ unchanged = 0
+
+ def emit(path: Path, text: str) -> None:
+ nonlocal written, unchanged
+ relative = path.relative_to(vault).as_posix()
+ did_write, digest = _write_if_changed(path, text, previous.get(relative))
+ current[relative] = digest
+ if did_write:
+ written += 1
+ else:
+ unchanged += 1
+
+ for entity in projection.entities:
+ emit(
+ vault / TOPICS_DIR / f"{topic_names[entity.key]}.md",
+ _topic_note(
+ entity,
+ topic_names,
+ moment_names,
+ projection.moments_by_entity.get(entity.key, ()),
+ ),
+ )
+
+ for moment in projection.moments:
+ name = moment_names[str(moment.document_id)]
+ emit(
+ vault / MOMENTS_DIR / (moment.month or _UNDATED) / f"{name}.md",
+ _moment_note(moment, topic_names),
+ )
+
+ emit(vault / OVERVIEW_DIR / "All topics.md", _overview_topics(projection, topic_names))
+ emit(vault / OVERVIEW_DIR / "Timeline.md", _overview_timeline(projection, moment_names))
+ emit(vault / OVERVIEW_DIR / "Sources.md", _overview_sources(projection))
+ emit(vault / "README.md", _readme(GENERATED_DIRS))
+
+ removed = _prune(vault, {vault / rel for rel in current}, previous)
+ _save_manifest(vault, current)
+
+ return ExportResult(
+ root=vault,
+ topics=len(projection.entities),
+ moments=len(projection.moments),
+ written=written,
+ unchanged=unchanged,
+ removed=removed,
+ )
+
+
+def _prune(vault: Path, kept: set[Path], previous: dict[str, str]) -> int:
+ """Delete our own notes that this export did not write, and nothing else.
+
+ Two independent proofs of ownership, and either one suffices: the note is
+ listed in the previous manifest, or its front matter carries the marker.
+ The marker path is what keeps pruning correct when the manifest is missing
+ (first run after an update, or a user who deleted it); the manifest path
+ is what avoids reading every surviving note back on a normal run.
+ """
+ removed = 0
+ for directory in GENERATED_DIRS:
+ base = vault / directory
+ if not base.is_dir():
+ continue
+ for path in sorted(base.rglob("*.md")):
+ if path in kept:
+ continue
+ relative = path.relative_to(vault).as_posix()
+ if relative not in previous and not _is_ours(path):
+ continue
+ try:
+ path.unlink()
+ removed += 1
+ except OSError as exc: # a locked file must not abort the export
+ log.warning("vault export: could not remove %s (%s)", path, exc)
+ # Empty month folders left behind read as "nothing happened then",
+ # which is a claim the data does not make.
+ for folder in sorted(base.rglob("*"), reverse=True):
+ if folder.is_dir() and not any(folder.iterdir()):
+ folder.rmdir()
+ return removed
diff --git a/tests/unit/ultrawiki/test_vault_export.py b/tests/unit/ultrawiki/test_vault_export.py
new file mode 100644
index 0000000000..676bfa75f8
--- /dev/null
+++ b/tests/unit/ultrawiki/test_vault_export.py
@@ -0,0 +1,360 @@
+"""Vault-export unit tests — the projection as a real Obsidian vault.
+
+Filenames carry most of the risk here and all of the cross-platform risk. On
+the reference corpus 14 of 947 topic labels contain a character that is
+illegal or dangerous in a filename (``personaljarvis/personaljarvis``,
+``win32/uia``, ``@eslint/plugin-kit``): written naively a slash silently
+creates a subdirectory on POSIX and raises on Windows. Another 12 labels
+differ only in case, which is the same file on Windows and macOS. Those cases
+are tested through the pure naming function, so they are covered wherever CI
+happens to run.
+
+The second risk is destructive: the export deletes stale files. It may only
+ever delete what it wrote itself.
+"""
+
+from __future__ import annotations
+
+from pathlib import Path
+
+import pytest
+
+from jarvis.ultrawiki.projection import (
+ ProjectedEntity,
+ ProjectedMoment,
+ WikiProjection,
+)
+from jarvis.ultrawiki.vault_export import (
+ GENERATED_DIRS,
+ GENERATED_MARKER,
+ MANIFEST_NAME,
+ USER_DIR,
+ assign_note_names,
+ export_vault,
+ safe_note_name,
+)
+
+
+def entity(
+ key: str,
+ label: str,
+ *,
+ mentions: int = 2,
+ first: str = "2026-03-01T10:00:00Z",
+ last: str = "2026-04-01T10:00:00Z",
+ neighbors: tuple[tuple[str, int], ...] = (),
+) -> ProjectedEntity:
+ return ProjectedEntity(
+ key=key,
+ label=label,
+ mentions=mentions,
+ item_ids=(1,) * mentions,
+ first_seen=first,
+ last_seen=last,
+ neighbors=neighbors,
+ )
+
+
+def moment(
+ document_id: int,
+ title: str,
+ *,
+ entity_keys: tuple[str, ...] = (),
+ timestamp: str = "2026-03-01T10:00:00Z",
+ summary: str = "A summary.",
+ resolution: str = "",
+) -> ProjectedMoment:
+ return ProjectedMoment(
+ document_id=document_id,
+ item_id=document_id * 10,
+ title=title,
+ summary=summary,
+ resolution=resolution,
+ entity_keys=entity_keys,
+ timestamp_utc=timestamp,
+ source_id="src1",
+ source_label="Jarvis Conversations",
+ permalink=f"app://item/{document_id}",
+ )
+
+
+def projection(
+ entities: list[ProjectedEntity], moments: list[ProjectedMoment]
+) -> WikiProjection:
+ by_entity: dict[str, list[ProjectedMoment]] = {}
+ for m in moments:
+ for key in m.entity_keys:
+ by_entity.setdefault(key, []).append(m)
+ return WikiProjection(
+ entities=tuple(entities),
+ moments=tuple(moments),
+ entity_by_key={e.key: e for e in entities},
+ moments_by_entity={k: tuple(v) for k, v in by_entity.items()},
+ )
+
+
+TRIP = projection(
+ [
+ entity("bora bora", "Bora Bora", mentions=2, neighbors=(("tahiti", 2),)),
+ entity("tahiti", "Tahiti", mentions=2, neighbors=(("bora bora", 2),)),
+ ],
+ [
+ moment(1, "How do I get to Bora Bora?", entity_keys=("bora bora", "tahiti")),
+ moment(
+ 2,
+ "Which airline flies to Tahiti?",
+ entity_keys=("bora bora", "tahiti"),
+ timestamp="2026-04-01T10:00:00Z",
+ ),
+ ],
+)
+
+
+# ---------------------------------------------------------------------------
+# Filenames — the cross-platform surface
+# ---------------------------------------------------------------------------
+
+
+ ("label", "forbidden"),
+ [
+ ("personaljarvis/personaljarvis", "/"),
+ ("win32/uia", "/"),
+ ("C:\\Windows", "\\"),
+ ('a "quoted" name', '"'),
+ ("what?", "?"),
+ ("a*b", "*"),
+ ("a<b>c", "<"),
+ ("a|b", "|"),
+ ("ratio 1:2", ":"),
+ ],
+)
+def test_characters_that_break_a_path_are_replaced(label: str, forbidden: str):
+ name = safe_note_name(label)
+ assert forbidden not in name
+ # And what comes out is a single path component, not a nested path.
+ assert Path(name).name == name
+
+
+def test_control_characters_are_replaced():
+ assert "\n" not in safe_note_name("two\nlines")
+ assert "\t" not in safe_note_name("a\tb")
+
+
+def test_trailing_dots_and_spaces_are_stripped():
+ # Windows silently drops them, which would break the orphan comparison:
+ # the file on disk would no longer match the name we think we wrote.
+ assert safe_note_name("version 1.") == "version 1"
+ assert safe_note_name("spaced ") == "spaced"
+
+
[email protected]("reserved", ["CON", "PRN", "AUX", "NUL", "COM1", "LPT9", "con"])
+def test_reserved_device_names_get_a_suffix(reserved: str):
+ name = safe_note_name(reserved)
+ assert name.lower() != reserved.lower()
+ assert reserved.lower() in name.lower()
+
+
+def test_long_labels_are_truncated_but_stay_distinct():
+ base = "x" * 200
+ a = safe_note_name(base + "aaa")
+ b = safe_note_name(base + "bbb")
+
+ assert len(a) <= 140
+ assert a != b # the trailing hash keeps a shared prefix from colliding
+
+
+def test_a_label_that_sanitizes_to_nothing_still_gets_a_name():
+ assert safe_note_name("///").strip() != ""
+
+
+def test_names_are_composed_unicode():
+ decomposed = "Cafe\u0301"
+ assert safe_note_name(decomposed) == "Caf\u00e9"
+
+
+def test_colliding_labels_get_deterministic_distinct_names():
+ # Two labels that sanitize to the same string, plus a case pair that is one
+ # file on Windows and macOS.
+ names = assign_note_names(
+ [
+ entity("a/b", "A/B"),
+ entity("a-b", "A-B"),
+ entity("apple", "Apple"),
+ ]
+ )
+
+ assert len(set(names.values())) == 3
+ assert names == assign_note_names(
+ [entity("a/b", "A/B"), entity("a-b", "A-B"), entity("apple", "Apple")]
+ )
+
+
+def test_names_never_collide_case_insensitively():
+ names = assign_note_names([entity("x", "Report"), entity("y", "REPORT")])
+ lowered = [name.lower() for name in names.values()]
+ assert len(set(lowered)) == 2
+
+
+# ---------------------------------------------------------------------------
+# Vault shape
+# ---------------------------------------------------------------------------
+
+
+def test_export_writes_topics_moments_and_overview(tmp_path: Path):
+ result = export_vault(TRIP, tmp_path / "vault")
+ root = tmp_path / "vault"
+
+ assert (root / "Topics" / "Bora Bora.md").exists()
+ assert (root / "Moments" / "2026-03" / "How do I get to Bora Bora.md").exists()
+ assert (root / "Overview" / "All topics.md").exists()
+ assert (root / "Overview" / "Timeline.md").exists()
+ assert (root / "Overview" / "Sources.md").exists()
+ assert (root / "README.md").exists()
+ assert result.topics == 2
+ assert result.moments == 2
+
+
+def test_the_user_directory_is_created_and_never_written_into(tmp_path: Path):
+ root = tmp_path / "vault"
+ export_vault(TRIP, root)
+ mine = root / USER_DIR / "my thoughts.md"
+ mine.write_text("Mine.", encoding="utf-8")
+
+ export_vault(TRIP, root)
+
+ assert mine.read_text(encoding="utf-8") == "Mine."
+
+
+def test_a_topic_note_links_to_its_neighbours_and_moments(tmp_path: Path):
+ export_vault(TRIP, tmp_path / "vault")
+ text = (tmp_path / "vault" / "Topics" / "Bora Bora.md").read_text(encoding="utf-8")
+
+ assert "[[Tahiti]]" in text
+ assert "[[How do I get to Bora Bora]]" in text
+
+
+def test_a_moment_note_links_its_topics_and_keeps_its_evidence_link(tmp_path: Path):
+ export_vault(TRIP, tmp_path / "vault")
+ text = (
+ tmp_path / "vault" / "Moments" / "2026-03" / "How do I get to Bora Bora.md"
+ ).read_text(encoding="utf-8")
+
+ assert "[[Bora Bora]]" in text
+ assert "app://item/1" in text
+
+
+def test_every_generated_note_declares_that_it_is_generated(tmp_path: Path):
+ root = tmp_path / "vault"
+ export_vault(TRIP, root)
+
+ for directory in GENERATED_DIRS:
+ for note in (root / directory).rglob("*.md"):
+ assert GENERATED_MARKER in note.read_text(encoding="utf-8"), note
+
+
+def test_files_are_utf8_without_bom_and_lf(tmp_path: Path):
+ root = tmp_path / "vault"
+ export_vault(TRIP, root)
+ raw = (root / "Topics" / "Bora Bora.md").read_bytes()
+
+ assert not raw.startswith(b"\xef\xbb\xbf")
+ assert b"\r\n" not in raw
+
+
+# ---------------------------------------------------------------------------
+# Re-export: idempotent, and destructive only towards its own files
+# ---------------------------------------------------------------------------
+
+
+def test_re_export_produces_byte_identical_notes(tmp_path: Path):
+ root = tmp_path / "vault"
+ export_vault(TRIP, root)
+ before = {
+ path: path.read_bytes() for path in (root / "Topics").rglob("*.md")
+ }
+
+ export_vault(TRIP, root)
+
+ after = {path: path.read_bytes() for path in (root / "Topics").rglob("*.md")}
+ assert after == before
+
+
+def test_a_topic_that_disappeared_is_removed(tmp_path: Path):
+ root = tmp_path / "vault"
+ export_vault(TRIP, root)
+ assert (root / "Topics" / "Tahiti.md").exists()
+
+ smaller = projection([TRIP.entities[0]], list(TRIP.moments))
+ export_vault(smaller, root)
+
+ assert not (root / "Topics" / "Tahiti.md").exists()
+ assert (root / "Topics" / "Bora Bora.md").exists()
+
+
+def test_a_hand_written_file_inside_a_generated_folder_survives(tmp_path: Path):
+ root = tmp_path / "vault"
+ export_vault(TRIP, root)
+ stray = root / "Topics" / "my own note.md"
+ stray.write_text("I wrote this.", encoding="utf-8")
+
+ export_vault(TRIP, root)
+
+ # Fail-closed: no generated marker means it is not ours to delete.
+ assert stray.read_text(encoding="utf-8") == "I wrote this."
+
+
+def test_a_re_export_does_not_read_the_notes_back(tmp_path: Path, monkeypatch):
+ """Comparing by re-reading every note costs 2.5 ms per file on a machine
+ with a virus scanner — 24 s for a corpus that changed nothing. The export
+ remembers what it wrote instead."""
+ root = tmp_path / "vault"
+ export_vault(TRIP, root)
+
+ reads: list[Path] = []
+ original = Path.read_bytes
+
+ def counting_read(self: Path) -> bytes:
+ reads.append(self)
+ return original(self)
+
+ monkeypatch.setattr(Path, "read_bytes", counting_read)
+ export_vault(TRIP, root)
+
+ assert [p for p in reads if p.suffix == ".md"] == []
+
+
+def test_a_lost_manifest_still_prunes_correctly(tmp_path: Path):
+ """The manifest is an optimisation, not the authority: without it the
+ export falls back to the front-matter marker rather than pruning nothing
+ (stale notes forever) or everything (someone else's files)."""
+ root = tmp_path / "vault"
+ export_vault(TRIP, root)
+ stray = root / "Topics" / "mine.md"
+ stray.write_text("Mine.", encoding="utf-8")
+ (root / MANIFEST_NAME).unlink()
+
+ export_vault(projection([TRIP.entities[0]], list(TRIP.moments)), root)
+
+ assert not (root / "Topics" / "Tahiti.md").exists()
+ assert stray.read_text(encoding="utf-8") == "Mine."
+
+
+def test_a_note_deleted_by_hand_is_written_again(tmp_path: Path):
+ root = tmp_path / "vault"
+ export_vault(TRIP, root)
+ (root / "Topics" / "Tahiti.md").unlink()
+
+ export_vault(TRIP, root)
+
+ assert (root / "Topics" / "Tahiti.md").exists()
+
+
+def test_an_empty_projection_produces_a_vault_that_explains_itself(tmp_path: Path):
+ root = tmp_path / "vault"
+
+ result = export_vault(projection([], []), root)
+
+ assert result.topics == 0
+ assert (root / "README.md").exists()
+ assert (root / USER_DIR).is_dir()