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

Validate note status before writing it to frontmatter (#89)

Marcelo Oliveira6e9e849593ae
diff --git a/src/hyperresearch/cli/note.py b/src/hyperresearch/cli/note.py
index b96df8e..6973872 100644
--- a/src/hyperresearch/cli/note.py
+++ b/src/hyperresearch/cli/note.py
@@ -441,9 +441,19 @@ def note_update(
from hyperresearch.core.frontmatter import parse_frontmatter, serialize_frontmatter
from hyperresearch.core.sync import compute_sync_plan, execute_sync
from hyperresearch.core.vault import Vault
- from hyperresearch.models.note import ContentType, Tier
+ from hyperresearch.models.note import ContentType, NoteStatus, Tier
# Validate enums up-front
+ if set_status is not None:
+ try:
+ NoteStatus(set_status)
+ except ValueError:
+ valid = ", ".join(s.value for s in NoteStatus)
+ if json_output:
+ output(error(f"Invalid --status '{set_status}'. Must be one of: {valid}", "INVALID_STATUS"), json_mode=True)
+ else:
+ console.print(f"[red]Invalid --status '{set_status}'.[/] Must be one of: {valid}")
+ raise typer.Exit(1)
if set_tier is not None:
try:
Tier(set_tier)
diff --git a/src/hyperresearch/mcp/server.py b/src/hyperresearch/mcp/server.py
index 7aa39ce..d4d2c06 100644
--- a/src/hyperresearch/mcp/server.py
+++ b/src/hyperresearch/mcp/server.py
@@ -367,6 +367,22 @@ def update_note(note_id: str, status: str = "", add_tags: str = "", remove_tags:
"""
from hyperresearch.core.frontmatter import parse_frontmatter, serialize_frontmatter
from hyperresearch.core.sync import compute_sync_plan, execute_sync
+ from hyperresearch.models.note import NoteStatus
+
+ # Validate up-front. NoteMeta is a pydantic model without
+ # validate_assignment, so `meta.status = status` would accept any string
+ # and write it to frontmatter — after which parse_frontmatter refuses to
+ # read the note back and sync drops it from the index for good.
+ if status:
+ try:
+ NoteStatus(status)
+ except ValueError:
+ valid = ", ".join(s.value for s in NoteStatus)
+ return json.dumps({
+ "ok": False,
+ "error": f"Invalid status '{status}'. Must be one of: {valid}",
+ "error_code": "INVALID_STATUS",
+ })
vault = _get_vault()
vault.auto_sync()
diff --git a/tests/test_cli/test_note_ops.py b/tests/test_cli/test_note_ops.py
index 61d9d19..2b22ca6 100644
--- a/tests/test_cli/test_note_ops.py
+++ b/tests/test_cli/test_note_ops.py
@@ -117,3 +117,48 @@ def test_note_list_with_filters(vault_with_notes):
result = runner.invoke(app, ["note", "list", "--sort", "title", "--limit", "1", "--json"])
data = json.loads(result.output)
assert data["count"] == 1
+
+
+def test_note_update_rejects_invalid_status(vault_with_notes):
+ """`note update --status` must validate before writing frontmatter.
+
+ Regression test: NoteMeta is a pydantic model configured without
+ validate_assignment, so `meta.status = set_status` used to accept any
+ string and serialize it straight into the note. The write reported
+ success, but parse_frontmatter then refused to read the note back, and
+ execute_sync swallowed the ValidationError per-file — leaving the note
+ permanently absent from the index while the DB served stale rows. A
+ one-letter typo (`evergreeen`) was enough to orphan a note for good.
+ """
+ result = runner.invoke(
+ app, ["note", "update", "alpha-note", "--status", "evergreeen", "--json"]
+ )
+ assert result.exit_code == 1
+ data = json.loads(result.output)
+ assert data["ok"] is False
+ assert data["error_code"] == "INVALID_STATUS"
+ assert "evergreen" in data["error"] # lists the valid values
+
+ # The note on disk must be untouched and still parseable.
+ from hyperresearch.core.frontmatter import parse_frontmatter
+
+ content = (vault_with_notes / "research/notes/alpha-note.md").read_text(encoding="utf-8")
+ meta, _ = parse_frontmatter(content)
+ assert meta.status == "draft"
+
+
+def test_note_update_accepts_valid_status_and_stays_indexed(vault_with_notes):
+ """The happy path must still write, and the note must survive a resync."""
+ result = runner.invoke(
+ app, ["note", "update", "alpha-note", "--status", "evergreen", "--json"]
+ )
+ assert result.exit_code == 0
+ assert json.loads(result.output)["ok"] is True
+
+ sync = runner.invoke(app, ["sync", "--json"])
+ assert sync.exit_code == 0
+ assert not json.loads(sync.output)["data"]["errors"]
+
+ listed = runner.invoke(app, ["note", "list", "--json"])
+ rows = json.loads(listed.output)["data"]
+ assert [n["status"] for n in rows if n["id"] == "alpha-note"] == ["evergreen"]
diff --git a/tests/test_mcp/__init__.py b/tests/test_mcp/__init__.py
new file mode 100644
index 0000000..e69de29
diff --git a/tests/test_mcp/test_update_note.py b/tests/test_mcp/test_update_note.py
new file mode 100644
index 0000000..1c2fc27
--- /dev/null
+++ b/tests/test_mcp/test_update_note.py
@@ -0,0 +1,59 @@
+"""Tests for the MCP server's update_note tool."""
+
+from __future__ import annotations
+
+import json
+import os
+from pathlib import Path
+
+import pytest
+from typer.testing import CliRunner
+
+from hyperresearch.cli import app
+
+mcp = pytest.importorskip("hyperresearch.mcp.server")
+
+runner = CliRunner()
+
+
+def mcp_vault(tmp_path: Path) -> Path:
+ vault_dir = tmp_path / "kb"
+ runner.invoke(app, ["init", str(vault_dir)])
+ os.chdir(vault_dir)
+ runner.invoke(app, ["note", "new", "Alpha Note"])
+ runner.invoke(app, ["sync"])
+ mcp._vault = None # module-global cache; force rediscovery under tmp_path
+ yield vault_dir
+ mcp._vault = None
+
+
+def test_update_note_rejects_invalid_status(mcp_vault):
+ """update_note must validate status before assigning it to NoteMeta.
+
+ Regression test: NoteMeta has no validate_assignment, so
+ `meta.status = status` accepted any string and wrote it to frontmatter.
+ parse_frontmatter then rejected the file, and sync dropped the note from
+ the index permanently while reporting the write as successful.
+ """
+ out = json.loads(mcp.update_note("alpha-note", status="evergreeen"))
+ assert out["ok"] is False
+ assert out["error_code"] == "INVALID_STATUS"
+ assert "evergreen" in out["error"]
+
+ from hyperresearch.core.frontmatter import parse_frontmatter
+
+ content = (mcp_vault / "research/notes/alpha-note.md").read_text(encoding="utf-8")
+ meta, _ = parse_frontmatter(content)
+ assert meta.status == "draft"
+
+
+def test_update_note_accepts_valid_status(mcp_vault):
+ out = json.loads(mcp.update_note("alpha-note", status="evergreen"))
+ assert out["ok"] is True
+
+ from hyperresearch.core.frontmatter import parse_frontmatter
+
+ content = (mcp_vault / "research/notes/alpha-note.md").read_text(encoding="utf-8")
+ meta, _ = parse_frontmatter(content)
+ assert meta.status == "evergreen"