OpenViking icon indicating copy to clipboard operation
OpenViking copied to clipboard

[Bug]: Semantic reindex rebuilds chunker-split file paths as directories (empty or metadata-only), reads then fail with "Cannot read directory as file"

Open wyl1988 opened this issue 3 months ago • 1 comments

Environment

  • OpenViking version: 0.4.7 (PyPI, local deployment, openviking.service on 127.0.0.1:1933)
  • OS: Linux (x86_64)
  • Affected trees: user/<uid>/memories/, user/<uid>/peers/<agent>/memories/, user/<uid>/resources/

Summary

When the chunker has split a large .md file into _c001..._cNN chunk files and deleted the base file, a subsequent semantic reindex of the containing tree recreates the (now non-existent) base .md path as a directory instead of leaving it absent:

  • Form A — empty directory at foo.md/ (caught by find -type d -empty)
  • Form B — metadata-only directory at foo.md/ containing only .abstract.md / .overview.md sidecars (NOT empty, so it silently escapes empty-dir checks)

Afterwards, any read of viking://.../foo.md fails with:

Cannot read directory as file

The chunk contents remain intact and searchable at foo_c001.md...foo_cNN.md — only the base path's on-disk shape is corrupted (file path → directory).

Reproduction

  1. Have a memory/resource file large enough that the chunker splits it into _cNN chunks and removes the base file (e.g. profile_g20260823.md → profile_g20260823_c001.md ... _c007.md). The chunks stay readable.
  2. Trigger a semantic reindex over a scope that covers those paths (batch reindex on the memories tree; in our case the reindex cascaded into Semantic-Nodes resource-tree rebuild).
  3. After the reindex completes, foo.md/ exists as a directory (Form A or B).
  4. ov read / any memory read of the base URI now returns Cannot read directory as file.

Observed evidence (production instance)

Three waves over three days, all correlated with reindex activity by directory birth time:

Wave When (birth times) Count Location Form
1 2026-08-25 (reindex window) 25 user/default/memories/ A (empty)
2 2026-08-25 20:27–20:28 (same reindex, cascade tail) 23 peers/{class_expert,commander,ops_expert}/memories/ B (metadata-only, escaped -empty detection)
3 2026-08-26 00:10 (follow-up reindex of a resource tree) 6 (+4 from wave 2 window) resources/.../ A
  • Directory birth times cluster tightly (within 1–2 minutes) inside known reindex windows — including a wave created after wave 1 had already been manually cleaned, i.e. re-running reindex reliably recreates them.
  • Form B directories contain only .abstract.md / .overview.md — i.e. they were produced by OV's directory-metadata pipeline, treating the chunked-away file path as a directory node.
  • Real breakage observed: agent memory reads failing with Cannot read directory as file (e.g. peers/class_expert/memories/skills/compile_chapter_note(.wiki).md, 2026-08-28).
  • After rmdir of the phantom directories, reads recover and chunks remain retrievable via ov find — no vector data lost.

Related observation (same root family): for chunked-away base paths, the Semantic-Nodes queue keeps referencing the old base path and vectorization logs File not found, then skips (chunks are already vectorized). So the pipeline already "knows" these base paths are gone in some stages, yet the tree-rebuild stage recreates them as directories.

Suspected root cause

The reindex / Semantic-Nodes tree-rebuild stage appears to treat every node it schedules (including paths whose base file was removed by the chunker) as a directory node and mkdirs it (possibly while writing .abstract.md/.overview.md rollups). Related evidence that the reindex pipeline assumes directory semantics for file paths: #4030 (pathlock_acquire_tree() called on a single-file URI → "not a directory" lock error). In our case the same file-as-directory assumption instead succeeds silently on the filesystem and leaves a phantom directory behind.

Suggested fix direction: before tree rebuild / sidecar generation, check whether the path is (or should be) a file — skip paths that only exist as _cNN chunk sets, and never create a directory where a .md file path is expected.

Impact

  • Silent filesystem-shape corruption under reindex; reads of affected URIs fail.
  • Form B escapes find -type d -empty monitoring, so it lingers until a read fails.
  • Recurs on every reindex that covers affected scopes — this is a stable bug, not a one-off race.

Workaround

  • Delete the phantom directories (rmdir for Form A; for Form B verify the directory only contains .abstract.md/.overview.md, back those up, then rmdir).
  • Avoid batch reindex over trees containing chunked-away base paths until fixed.
  • Monitor: flag any .md-named directory under memories/ trees.

wyl1988 avatar Aug 29 '26 02:08 wyl1988

I'd like to take this. I'll first reproduce the chunked-away base path being recreated during semantic reindex, then keep the fix scoped to preventing file-shaped URIs from being materialized as directories during tree and sidecar rebuild. I'll add regression coverage for both empty and metadata-only phantom directories without changing chunk naming or normal directory summaries.

ranxi2001 avatar Aug 29 '26 05:08 ranxi2001