Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
37 changes: 8 additions & 29 deletions openviking/service/reindex_executor.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,9 +37,12 @@
from openviking.service.task_work_index import bind_task_context, get_task_context
from openviking.session.memory.utils.memory_file_utils import MemoryFileUtils
from openviking.storage.abstract_overview import (
ABSTRACT_NOT_READY_SUFFIX,
ABSTRACT_OVERVIEW_FILENAMES,
OVERVIEW_NOT_READY_SUFFIX,
body_for_preview,
embedding_text_for_body,
is_directory_summary_placeholder,
)
from openviking.storage.errors import LockError
from openviking.storage.internal_names import is_storage_internal_name
Expand Down Expand Up @@ -77,30 +80,6 @@
}


# Trailing markers VikingFS appends when a directory has no generated .abstract.md/.overview.md
# (see openviking/storage/viking_fs.py). The rendered value is a placeholder, not semantic
# content, and must never be embedded as an ABSTRACT (L0) / OVERVIEW (L1) vector (issue #2434).
_ABSTRACT_NOT_READY_SUFFIX = "[Directory abstract is not ready]"
_OVERVIEW_NOT_READY_SUFFIX = "[Directory overview is not ready]"


def _is_not_ready_sentinel(text: str, suffix: str) -> bool:
"""Return True if *text* is a VikingFS not-ready directory placeholder.

VikingFS renders these as a single ``# <uri>`` header followed only by the not-ready marker.
Match that exact shape (a ``#`` header with no substantive body before the trailing marker)
so the check is uri-agnostic yet never drops real directory content that merely ends with,
or mentions, the user-facing marker phrase.
"""
if not text:
return False
head = text.rstrip()
if not head.endswith(suffix):
return False
head = head[: -len(suffix)].strip()
return head.startswith("#") and "\n" not in head


@dataclass
class _ReindexCounters:
scanned_records: int = 0
Expand Down Expand Up @@ -2096,9 +2075,9 @@ async def _read_memory_directory_source(self, uri: str, *, ctx: RequestContext)
return _SourceRead(exists=True, error=exc)
try:
text = body_for_preview(raw).strip()
if _is_not_ready_sentinel(text, _ABSTRACT_NOT_READY_SUFFIX) or _is_not_ready_sentinel(
text, _OVERVIEW_NOT_READY_SUFFIX
):
if is_directory_summary_placeholder(
text, ABSTRACT_NOT_READY_SUFFIX
) or is_directory_summary_placeholder(text, OVERVIEW_NOT_READY_SUFFIX):
text = ""
return _SourceRead(exists=True, text=text)
except Exception as exc:
Expand All @@ -2109,14 +2088,14 @@ async def _read_directory_abstract(self, uri: str, *, ctx: RequestContext) -> st
value = await get_viking_fs().abstract(uri, ctx=ctx)
except Exception:
return ""
return "" if _is_not_ready_sentinel(value, _ABSTRACT_NOT_READY_SUFFIX) else value
return "" if is_directory_summary_placeholder(value, ABSTRACT_NOT_READY_SUFFIX) else value

async def _read_directory_overview(self, uri: str, *, ctx: RequestContext) -> str:
try:
value = await get_viking_fs().overview(uri, ctx=ctx)
except Exception:
return ""
return "" if _is_not_ready_sentinel(value, _OVERVIEW_NOT_READY_SUFFIX) else value
return "" if is_directory_summary_placeholder(value, OVERVIEW_NOT_READY_SUFFIX) else value

async def _upsert_context(
self,
Expand Down
13 changes: 13 additions & 0 deletions openviking/storage/abstract_overview.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,8 @@
logger = get_logger(__name__)

ABSTRACT_OVERVIEW_FILENAMES = frozenset({".abstract.md", ".overview.md"})
ABSTRACT_NOT_READY_SUFFIX = "[Directory abstract is not ready]"
OVERVIEW_NOT_READY_SUFFIX = "[Directory overview is not ready]"
EMBEDDING_METADATA_FIELDS = ("directory",)
_METADATA_ORDER = ("directory", "source", "generated_by", "freshness")
_MARKDOWN_URI_SAFE_ASCII = frozenset(
Expand Down Expand Up @@ -74,6 +76,17 @@ def is_abstract_overview_uri(uri: str) -> bool:
return uri.rstrip("/").rsplit("/", 1)[-1] in ABSTRACT_OVERVIEW_FILENAMES


def is_directory_summary_placeholder(text: str, suffix: str) -> bool:
"""Identify VikingFS's heading-only marker without dropping authored content."""
if not text:
return False
heading = text.rstrip()
if not heading.endswith(suffix):
return False
heading = heading[: -len(suffix)].strip()
return heading.startswith("#") and "\n" not in heading


def _normalize_directory_uri(dir_uri: str) -> str:
value = str(dir_uri or "").strip()
if not value:
Expand Down
12 changes: 9 additions & 3 deletions openviking/storage/queuefs/semantic_executor.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,12 @@
get_task_context,
)
from openviking.storage.abstract_overview import (
ABSTRACT_NOT_READY_SUFFIX,
AbstractOverviewFormatError,
AbstractOverviewWriteResult,
body_for_preview,
freshness_metadata,
is_directory_summary_placeholder,
read_abstract_overview_pending_snapshot,
write_abstract_overview,
)
Expand Down Expand Up @@ -1356,6 +1358,7 @@ def stale(self) -> bool:

async def _finalize_children_abstracts(self, node: DirNode) -> List[Dict[str, str]]:
results: List[Dict[str, str]] = []
memory_reindex = self._context_type == "memory" and self._generation_trigger == "reindex"
for idx, child_uri in enumerate(node.children_dirs):
if (
node.sampled_children_dirs is not None
Expand All @@ -1368,9 +1371,12 @@ async def _finalize_children_abstracts(self, node: DirNode) -> List[Dict[str, st
abstract = await self._viking_fs.abstract(child_uri, ctx=self._ctx)
except Exception:
abstract = ""
results.append({"name": child_uri.split("/")[-1], "abstract": abstract})
else:
results.append(item)
item = {"name": child_uri.split("/")[-1], "abstract": abstract}
if memory_reindex and is_directory_summary_placeholder(
item["abstract"], ABSTRACT_NOT_READY_SUFFIX
):
continue
results.append(item)
return results

def _is_stale(self) -> bool:
Expand Down
26 changes: 13 additions & 13 deletions tests/service/test_reindex_placeholder.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,46 +8,46 @@
shape-anchored so legitimate directory content is never dropped.
"""

from openviking.service.reindex_executor import (
_ABSTRACT_NOT_READY_SUFFIX,
_OVERVIEW_NOT_READY_SUFFIX,
_is_not_ready_sentinel,
from openviking.storage.abstract_overview import (
ABSTRACT_NOT_READY_SUFFIX,
OVERVIEW_NOT_READY_SUFFIX,
is_directory_summary_placeholder,
)


def test_real_abstract_sentinel_is_detected():
rendered = "# viking://memory/projects/foo [Directory abstract is not ready]"
assert _is_not_ready_sentinel(rendered, _ABSTRACT_NOT_READY_SUFFIX)
assert is_directory_summary_placeholder(rendered, ABSTRACT_NOT_READY_SUFFIX)


def test_real_overview_sentinel_is_detected():
rendered = "# viking://memory/projects/foo\n\n[Directory overview is not ready]"
assert _is_not_ready_sentinel(rendered, _OVERVIEW_NOT_READY_SUFFIX)
assert is_directory_summary_placeholder(rendered, OVERVIEW_NOT_READY_SUFFIX)


def test_sentinel_detection_is_uri_agnostic():
for uri in ("viking://a/b/", "viking://x%20y/z", "viking://"):
assert _is_not_ready_sentinel(
f"# {uri} [Directory abstract is not ready]", _ABSTRACT_NOT_READY_SUFFIX
assert is_directory_summary_placeholder(
f"# {uri} [Directory abstract is not ready]", ABSTRACT_NOT_READY_SUFFIX
)


def test_content_mentioning_phrase_mid_body_is_preserved():
real = "# Notes\n\nWe used to show '[Directory abstract is not ready]' here, but now it has content."
assert not _is_not_ready_sentinel(real, _ABSTRACT_NOT_READY_SUFFIX)
assert not is_directory_summary_placeholder(real, ABSTRACT_NOT_READY_SUFFIX)


def test_authored_content_ending_with_marker_is_preserved():
# multi-line authored body that ends with the exact user-facing marker must survive
real = "# Project notes\n\nCurrent CLI output: [Directory abstract is not ready]"
assert not _is_not_ready_sentinel(real, _ABSTRACT_NOT_READY_SUFFIX)
assert not is_directory_summary_placeholder(real, ABSTRACT_NOT_READY_SUFFIX)


def test_real_content_preserved():
assert not _is_not_ready_sentinel(
"# Project\n\nReal abstract body.", _ABSTRACT_NOT_READY_SUFFIX
assert not is_directory_summary_placeholder(
"# Project\n\nReal abstract body.", ABSTRACT_NOT_READY_SUFFIX
)


def test_empty_is_not_a_sentinel():
assert not _is_not_ready_sentinel("", _ABSTRACT_NOT_READY_SUFFIX)
assert not is_directory_summary_placeholder("", ABSTRACT_NOT_READY_SUFFIX)
38 changes: 38 additions & 0 deletions tests/storage/test_semantic_executor_stats.py
Original file line number Diff line number Diff line change
Expand Up @@ -341,6 +341,44 @@ async def test_non_recursive_memory_includes_files_and_reads_child_abstracts(mon
assert coverage["total_children"] == 2


@pytest.mark.asyncio
async def test_memory_reindex_rolls_up_only_ready_child_abstracts(monkeypatch):
root_uri = "viking://user/alice/memories/events"
ready_uri = f"{root_uri}/ready"
pending_uri = f"{root_uri}/pending"
ready_abstract = "# Ready\n\nEvent summaries are available."
fake_fs = _FakeVikingFS(
{root_uri: [{"name": name, "isDir": True} for name in ("ready", "pending")]},
abstracts={
ready_uri: ready_abstract,
pending_uri: f"# {pending_uri} [Directory abstract is not ready]",
},
)
monkeypatch.setattr(
"openviking.storage.queuefs.semantic_executor.get_viking_fs", lambda: fake_fs
)
_patch_semantic_config(monkeypatch)
processor = _FakeProcessor()
executor = SemanticTreeExecutor(
processor=processor,
context_type="memory",
max_concurrent_llm=2,
ctx=RequestContext(user=UserIdentifier("acc1", "alice"), role=Role.USER),
recursive=False,
generation_trigger="reindex",
)

await executor.run(root_uri)

assert processor.overview_inputs[-1][:3] == (
root_uri,
[],
[{"name": "ready", "abstract": ready_abstract}],
)
assert processor.overview_inputs[-1][3]["total_children"] == 2
assert processor.vectorized_dirs == [root_uri]


@pytest.mark.asyncio
async def test_busy_parent_snapshot_preserves_changed_file_work(monkeypatch):
root_uri = "viking://resources/wide"
Expand Down