add-resource into an existing directory deletes all other resources (mirror sync instead of merge)
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
-
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) -
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"} -
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:
-
--to <existing-dir>resolves the target URI to the directory itself (openviking/parse/tree_builder.py:108-109returnsto_uridirectly when it is not a content root). The target already exists, sotarget_preexisting=True(openviking/utils/resource_processor.py:352). -
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)). -
SemanticProcessor.on_dequeueseesmsg.uri != msg.target_uriand calls_sync_topdown_recursive(source=temp, target=existing_dir)(openviking/storage/queuefs/semantic_processor.py:403). -
_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.
-
The
target_preexistingflag is threaded throughSemanticMsg(semantic_msg.py:56, set atsummarizer.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.
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.
Thanks for the report and for opening #4218.
The current behavior of --to is:
--topoints 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.
--parentis 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.
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)