OpenViking icon indicating copy to clipboard operation
OpenViking copied to clipboard

add-resource into an existing directory deletes all other resources (mirror sync instead of merge)

Open procesd opened this issue 3 months ago • 3 comments

Summary

Adding a resource to a directory that already contains other resources deletes everything except the newly-added resource — a last-write-wins "reorg". N adds to one directory leave 1 survivor. Earlier resources, verified present via viking_browse list, are gone minutes later (NOT_FOUND on stat/list). This is deterministic and affects every shared topic directory.

Environment

  • openviking 0.4.12 (reproduced) — and the same code path is present in 0.4.16 (openviking/storage/viking_fs/_sync.py, sync_tree).
  • Native install (uv tool), local embedding + OpenAI-compatible VLM.

Steps to reproduce

  1. Add a first resource, creating a new directory:

    ov add-resource a.md --parent-auto-create viking://resources/test
    # -> viking://resources/test/a/ (a.md is chunked into a directory)
    
  2. Add a second resource targeting the SAME directory directly (the directory now exists, so this is an "add to existing directory"):

    ov add-resource b.md --to viking://resources/test
    # equivalently via HTTP: POST /api/v1/resources {"to": "viking://resources/test"}
    
  3. Wait for the semantic queue to drain, then list the directory:

    ov ls viking://resources/test
    

Expected

Both a/ and b.md are present.

Actual

Only the most-recently-added resource remains. test/a/ (and any other prior content) is gone: NOT_FOUND on stat/list. N adds to one directory => 1 survivor.

Root cause

The add-resource path performs a mirror sync ("target ends up mirroring the source") instead of an additive merge:

  1. --to <existing-dir> resolves the target URI to the directory itself (openviking/parse/tree_builder.py:108-109 returns to_uri directly when it is not a content root). The target already exists, so target_preexisting=True (openviking/utils/resource_processor.py:352).

  2. Because the target pre-exists, the parsed temp tree is NOT persisted in place; the temp URI is passed as the semantic "source" and the existing directory as "target" (openviking/utils/summarizer.py:117-127 -> SemanticMsg(uri=temp, target_uri=existing_dir)).

  3. SemanticProcessor.on_dequeue sees msg.uri != msg.target_uri and calls _sync_topdown_recursive(source=temp, target=existing_dir) (openviking/storage/queuefs/semantic_processor.py:403).

  4. _sync_topdown_recursive (0.4.12, semantic_processor.py:770-981) mirrors the source over the target. The two destructive branches are:

    if target_file and not root_file:        # line 842
        viking_fs.rm(target_file)
    if target_subdir and not root_subdir:    # line 921
        viking_fs.rm(target_subdir, recursive=True)
    

    The source temp tree contains ONLY the newly-added resource, so every sibling file and directory in the target is deleted.

  5. The target_preexisting flag is threaded through SemanticMsg (semantic_msg.py:56, set at summarizer.py:130) but is never consumed in the sync decision — the merge-vs-replace distinction was never wired up.

In 0.4.16 the logic moved to openviking/storage/viking_fs/_sync.py sync_tree but the same delete branches remain (lines ~166 and ~243), with the docstring "the target ends up mirroring the source".

Suggested fix

Thread target_preexisting into the sync and make it additive (skip target-only deletes) when the target pre-existed. Deleting target-only entries should only happen for an explicit replace / full-directory re-import, via a dedicated overwrite/replace flag rather than being the default.

A minimal stopgap (validated locally) is:

async def _sync_topdown_recursive(self, root_uri, target_uri, ctx=None,
                                  file_change_status=None, lock=None,
                                  merge: bool = True) -> DiffResult:
    ...
    if not merge and target_file and not root_file:
        ...
    if not merge and target_subdir and not root_subdir:
        ...

Impact

Data loss in the shared knowledge base: silently destroys previously-ingested resources whenever a new resource is added to a populated directory. Affects all topics, not just a single case.

procesd avatar Aug 22 '26 21:08 procesd

I opened an opinionated PR in #4218 where the default behaviour is to keep existing files. It makes sense to me but I understand that it changes the sync behaviour. It can easily changed to delete_missing = True to make the fix backwards compatible. Still, for a software in v0.4.12, I think it would be overkill to worry too much about that.

procesd avatar Aug 22 '26 22:08 procesd

Thanks for the report and for opening #4218.

The current behavior of --to is:

  • --to points to the final target to write. If the target already exists, that target is refreshed.
  • If the target is a directory, files or subdirectories that are not produced by the current import may be removed.
  • --parent is the option to use when you want to add a new resource under an existing directory.

So for this case:

ov add-resource b.md --to viking://resources/test

test is treated as the target being refreshed, not as a parent directory to append into. To append b.md under test, use --parent viking://resources/test instead.

I opened #4357 to clarify this in the docs. Given that, I would not merge #4218 as-is because it changes the default sync behavior from refresh/mirror to additive merge.

sponge225 avatar Aug 26 '26 12:08 sponge225

Oh, I see, thanks for taking the time to clarify So if my default use case is adding I can just use -p, --parent-auto-create <uri> Target parent URI (create parent directory if it does not exist) (cannot be used with --to or --parent)

procesd avatar Aug 26 '26 13:08 procesd