Skip to content

Convert Claude Code or Codex into the most intelligent Deep Research Agent. Collect, search, and synthesize web research into a persistent, searchable wiki that builds on itself. Hosted API + MCP: hyperresearch.ai

fix: note mv resolves the destination into the synced tree, and refuses outside-tree and overwrite (#128)

Francois du Plessis847de424ede0
diff --git a/src/hyperresearch/cli/note.py b/src/hyperresearch/cli/note.py
index 6973872..3b652a7 100644
--- a/src/hyperresearch/cli/note.py
+++ b/src/hyperresearch/cli/note.py
@@ -545,10 +545,24 @@ def note_update(
@app.command("mv")
def note_mv(
note_id: str = typer.Argument(..., help="Note ID to move"),
- new_path: str = typer.Argument(..., help="New relative path (e.g. notes/python/renamed.md)"),
+ new_path: str = typer.Argument(
+ ...,
+ help=(
+ "Destination relative to the vault root, e.g. research/notes/renamed.md. "
+ "A bare name lands in research/notes/ and a missing .md is added. The note "
+ "keeps its id (it lives in frontmatter, not the filename), so wiki-links to "
+ "it stay valid and nothing else is rewritten."
+ ),
+ ),
json_output: bool = typer.Option(False, "--json", "-j", help="JSON output"),
) -> None:
- """Move/rename a note, updating all references."""
+ """Move a note file within the tree sync scans (research/notes or research/temp).
+
+ The id is unchanged: `mv` moves the file, it does not rename the note.
+ """
+ from pathlib import Path
+
+ from hyperresearch.core.claims import _under
from hyperresearch.core.sync import compute_sync_plan, execute_sync
from hyperresearch.core.vault import Vault
@@ -564,7 +578,42 @@ def note_mv(
raise typer.Exit(1)
old_file = vault.root / row["path"]
- new_file = vault.root / new_path
+
+ # Resolve the destination into something sync will see. Joined verbatim, a
+ # bare name landed at the vault root with no suffix, where sync never
+ # looks, so the note silently left the index; and rename() replaced an
+ # existing note file without a word. research/index/ is synced but not a
+ # destination: IndexGenerator.build_all() deletes every .md there on the
+ # next repair, so a note moved in would be lost.
+ dest = Path(new_path)
+ if dest.name and dest.suffix.lower() != ".md":
+ dest = dest.with_name(dest.name + ".md")
+ if dest.parent == Path("."):
+ dest = vault.notes_dir.relative_to(vault.root) / dest
+ new_file = vault.root / dest
+ synced_roots = (vault.notes_dir, vault.temp_dir)
+ if not any(_under(new_file, r) and new_file.resolve() != r.resolve() for r in synced_roots):
+ allowed = ", ".join(r.relative_to(vault.root).as_posix() + "/" for r in synced_roots)
+ msg = f"Destination {dest.as_posix()} is outside the synced tree ({allowed})"
+ if json_output:
+ output(error(msg, "OUTSIDE_SYNCED_TREE"), json_mode=True)
+ else:
+ console.print(f"[red]{msg}[/]")
+ raise typer.Exit(1)
+ # samefile: on a case-insensitive filesystem a case-only rename
+ # (n-f -> N-F.md) "exists" because it is the note's own file.
+ if new_file.exists() and not new_file.samefile(old_file):
+ msg = f"Destination already exists: {dest.as_posix()}"
+ if json_output:
+ output(error(msg, "DESTINATION_EXISTS"), json_mode=True)
+ else:
+ console.print(f"[red]{msg}[/]")
+ raise typer.Exit(1)
+ # Resolve the parent only: on a case-insensitive filesystem resolve() on the
+ # file itself returns the old spelling during a case-only rename.
+ rel_new = (
+ (new_file.parent.resolve() / new_file.name).relative_to(vault.root.resolve()).as_posix()
+ )
new_file.parent.mkdir(parents=True, exist_ok=True)
old_file.rename(new_file)
@@ -574,9 +623,9 @@ def note_mv(
execute_sync(vault, plan)
if json_output:
- output(success({"old_path": row["path"], "new_path": new_path}, vault=str(vault.root)), json_mode=True)
+ output(success({"old_path": row["path"], "new_path": rel_new}, vault=str(vault.root)), json_mode=True)
else:
- console.print(f"[green]Moved:[/] {row['path']} → {new_path}")
+ console.print(f"[green]Moved:[/] {row['path']} → {rel_new}")
@app.command("rm")
diff --git a/tests/test_cli/test_note_ops.py b/tests/test_cli/test_note_ops.py
index 2b22ca6..6dfce07 100644
--- a/tests/test_cli/test_note_ops.py
+++ b/tests/test_cli/test_note_ops.py
@@ -88,15 +88,97 @@ def test_note_rm_cleans_raw_file_and_assets(vault_with_notes):
def test_note_mv(vault_with_notes):
+ result = runner.invoke(
+ app, ["note", "mv", "beta-note", "research/notes/moved/beta-note.md", "--json"]
+ )
+ assert result.exit_code == 0, result.output
+ data = json.loads(result.output)
+ assert data["data"]["new_path"] == "research/notes/moved/beta-note.md"
+ assert (vault_with_notes / "research" / "notes" / "moved" / "beta-note.md").exists()
+
+
+# ---------------------------------------------------------------------------
+# `note mv` destinations must stay where sync can see them. The destination
+# used to be joined onto the vault root verbatim: a bare name landed at the
+# vault root with no .md suffix, outside everything sync scans, so the note
+# silently left the index; and an existing file was overwritten.
+# ---------------------------------------------------------------------------
+
+
+def _indexed_path(note_id: str) -> str | None:
+ from hyperresearch.core.vault import Vault
+
+ row = Vault.discover().db.execute(
+ "SELECT path FROM notes WHERE id = ?", (note_id,)
+ ).fetchone()
+ return row["path"] if row else None
+
+
+def test_note_mv_bare_name_lands_in_notes_dir_with_md_suffix(vault_with_notes):
+ result = runner.invoke(app, ["note", "mv", "beta-note", "beta-renamed", "--json"])
+ assert result.exit_code == 0, result.output
+ data = json.loads(result.output)
+ assert data["data"]["new_path"] == "research/notes/beta-renamed.md"
+ assert (vault_with_notes / "research" / "notes" / "beta-renamed.md").exists()
+ assert not (vault_with_notes / "beta-renamed").exists()
+ # The id lives in frontmatter, so the note stays indexed under it, at the new path.
+ assert _indexed_path("beta-note") == "research/notes/beta-renamed.md"
+
+
+def test_note_mv_refuses_a_destination_outside_the_synced_tree(vault_with_notes):
result = runner.invoke(
app, ["note", "mv", "beta-note", "notes/moved/beta-note.md", "--json"]
)
- assert result.exit_code == 0
+ assert result.exit_code == 1, result.output
data = json.loads(result.output)
- assert data["data"]["new_path"] == "notes/moved/beta-note.md"
- assert (vault_with_notes / "notes" / "moved" / "beta-note.md").exists()
+ assert data["ok"] is False
+ assert data["error_code"] == "OUTSIDE_SYNCED_TREE"
+ assert (vault_with_notes / "research" / "notes" / "beta-note.md").exists()
+ assert not (vault_with_notes / "notes").exists()
+ assert _indexed_path("beta-note") == "research/notes/beta-note.md"
+
+
[email protected]("dest", ["research/notes/alpha-note.md", "alpha-note"])
+def test_note_mv_refuses_to_overwrite_an_existing_file(vault_with_notes, dest):
+ result = runner.invoke(app, ["note", "mv", "beta-note", dest, "--json"])
+ assert result.exit_code == 1, result.output
+ data = json.loads(result.output)
+ assert data["error_code"] == "DESTINATION_EXISTS"
+ alpha = (vault_with_notes / "research" / "notes" / "alpha-note.md").read_text(encoding="utf-8")
+ assert "id: alpha-note" in alpha
+ assert _indexed_path("alpha-note") == "research/notes/alpha-note.md"
+ assert _indexed_path("beta-note") == "research/notes/beta-note.md"
+
+
+
+def test_note_mv_refuses_the_index_dir(vault_with_notes):
+ # build_all() wipes research/index/*.md on every repair, so a note moved
+ # there would be deleted the next time the index regenerates.
+ result = runner.invoke(
+ app, ["note", "mv", "beta-note", "research/index/beta-note.md", "--json"]
+ )
+ assert result.exit_code == 1, result.output
+ assert json.loads(result.output)["error_code"] == "OUTSIDE_SYNCED_TREE"
+ assert _indexed_path("beta-note") == "research/notes/beta-note.md"
+
+
+def test_note_mv_allows_a_case_only_rename(vault_with_notes):
+ result = runner.invoke(
+ app, ["note", "mv", "beta-note", "research/notes/Beta-Note.md", "--json"]
+ )
+ assert result.exit_code == 0, result.output
+ assert json.loads(result.output)["data"]["new_path"] == "research/notes/Beta-Note.md"
+ names = [f.name for f in (vault_with_notes / "research" / "notes").iterdir()]
+ assert "Beta-Note.md" in names
+ assert "beta-note.md" not in names
+ assert _indexed_path("beta-note") == "research/notes/Beta-Note.md"
+def test_note_mv_does_not_double_an_uppercase_suffix(vault_with_notes):
+ result = runner.invoke(app, ["note", "mv", "beta-note", "Renamed.MD", "--json"])
+ assert result.exit_code == 0, result.output
+ assert json.loads(result.output)["data"]["new_path"] == "research/notes/Renamed.MD"
+
def test_note_show_raw(vault_with_notes):
result = runner.invoke(app, ["note", "show", "alpha-note", "--raw"])
assert result.exit_code == 0