[Feature]: content-aware add_resource — opt-in --if-changed / --if-newer / --dry-run to make writes idempotent
Issue Origin
Observed or reproduced in a real environment
Problem Statement
add_resource to an existing viking:// URI silently overwrites whatever is there. OpenViking has no built-in content awareness on the write path:
- No content hash comparison → identical writes still re-enqueue full embedding work.
- No mtime / version comparison → a stale copy (e.g., a laptop that has been offline for a day) will happily overwrite a fresher server copy.
- No diff preview → the caller has no way to see what changed before the overwrite lands.
In practice this bites hardest for multi-endpoint sync scenarios, where the same authoritative document is edited in several places (Feishu / local Word / an agent-generated version) and periodically flushed back into OV. Every flush is a potential silent regression.
Related but distinct: #2383 (repeated add_resource reports full embedding work on unchanged content) tackles the embedding cost half of the same root cause — this issue asks for a content lifecycle API on top.
Proposed Solution
Add opt-in content-aware flags to add_resource that keep the current "overwrite" default (backward compatible) but let callers opt into safer semantics:
--if-changed(hash) → skip the write entirely ifsha256(new) == sha256(existing); emit ano-opTaskRecord and don't touch the vector store.--if-newer(mtime / explicit--source-mtime) → only overwrite when the source is strictly newer than the target; otherwise refuse withstale_sourceand exit non-zero.--dry-run→ return a summary of{would_write, bytes, sha_before, sha_after, mtime_before, mtime_after}without writing.- Default behavior stays as today (unconditional overwrite) — no surprise for existing pipelines.
This pairs naturally with the v0.4.10 prune_orphans reindex mode (#3096) to give operators a complete "resource lifecycle" surface: import (idempotent), reindex (owner-preserving), prune (garbage-collect).
Alternatives Considered
- Force callers to hash upstream. Works, but every caller has to reimplement the same hash-compare loop against the OV target — this is what an idempotent write API is supposed to solve.
- Add a separate
synccommand. Larger surface than needed;--if-changed/--if-neweras flags onadd_resourcereuse the existing pipeline and stay composable with--to,--wait,--watch-interval. - Move the check to the Connector layer. Would only cover Connector-imported sources (
tos://etc.), not local files or arbitrary sources — the exact scenarios where multi-endpoint sync happens most.
Feature Area
Filesystem Operations
Use Case
Before: a QGYG project master document lives in three places:
- Feishu wiki (edited by humans in a web UI)
- A local Word file on the CEO's laptop
- OV at
viking://resources/qgyg-master/
Every sync loop imports all three back into OV, unconditionally. Result: embedding queue thrashes on unchanged content, and a stale laptop copy can quietly overwrite a fresh Feishu edit.
After:
# Import only if content actually changed
ov add-resource ./master.docx \
--to viking://resources/qgyg-master/ \
--if-changed
# Refuse to overwrite with a stale copy
ov add-resource ./master.docx \
--to viking://resources/qgyg-master/ \
--if-newer --source-mtime "$(date -r ./master.docx +%s)"
Quantified impact for the QGYG workspace alone: master document is re-imported ~5×/week across endpoints. With --if-changed at least 3–4 of those are no-ops → embedding queue load down ~70% for this single asset, and stale-overwrite class of bug becomes structurally impossible.
Example API (Optional)
# Idempotent write — no-op when content is byte-identical
client.add_resource(
source="./master.docx",
to="viking://resources/qgyg-master/",
if_changed=True,
)
# -> TaskRecord(status="no-op", reason="sha256_match", sha="a1b2...")
# Refuse to regress
client.add_resource(
source="./master.docx",
to="viking://resources/qgyg-master/",
if_newer=True,
source_mtime=1721145600,
)
# -> TaskRecord(status="rejected", reason="stale_source",
# source_mtime=1721145600, target_mtime=1721232000)
# Dry run
client.add_resource(
source="./master.docx",
to="viking://resources/qgyg-master/",
dry_run=True,
)
# -> DryRunResult(would_write=True, bytes=42_881, sha_before="...", sha_after="...")
Additional Context
- Complementary to #2383 (repeated
add_resourcere-embeds unchanged content): #2383 asks the pipeline to detect no-ops internally; this issue asks for an explicit, contract-level--if-changedflag so callers can require idempotence and get a deterministic status back. - Pairs with v0.4.10
prune_orphans(#3096) as the second half of a resource-lifecycle surface. - Companion issues in this batch: #3305 (bare
INVALID_ARGUMENTon directoryread) and #3306 (missingtask_idonadd_resourcetimeout). All three make theviking://filesystem behave more like an actual versioned filesystem. - Contribution: willing to implement the client-side
--if-changed/--if-newerflags and hash comparison logic once the server exposes a stable "get target sha / mtime" endpoint.
I traced this proposal against current main. The user problem is real, but I don't think a CLI-only implementation would provide the safety guarantee described here.
Today:
AddResourceRequesthas no source fingerprint/revision contract.- temp uploads do not persist a computed source SHA (
sha256is currently unset in shared-upload metadata). - target resolution and the tree lock lead directly into parse/enqueue; there is no atomic compare-and-swap against a target revision.
- the stored resource tree is parsed/derived output, so hashing it is not equivalent to hashing the original source bytes.
That creates two correctness traps:
--if-changedcannot compare local source bytes with a stable target-source digest because that digest does not exist.- a standalone
--dry-runcheck is subject to TOCTOU: another writer can update the target before the real add-resource call.
A safe, reusable staged contract would be:
- Persist target source metadata (at least
source_sha256, an explicit source revision/mtime, and target revision) as part of the resource write. - Evaluate write preconditions under the same target tree lock used for ingestion. The API should accept a write policy/precondition, not rely on a client-side check.
- Make dry-run return the observed target revision/precondition token; a subsequent write can supply that token and receives a conflict if the target changed.
- Define fingerprints per source kind before exposing flags: single local file, normalized directory/archive, remote URL/connector snapshot, and watch refresh.
- Only then map SDK/CLI flags onto that server contract.
The first implementation slice I would accept is narrower than the full issue: server-side source metadata + atomic if_changed for one well-defined source kind, with compatibility kept unconditional by default. It should prove:
- identical input returns a deterministic no-op and queues no parse/vector work;
- changed input writes normally;
- two concurrent writers cannot both pass a stale precondition;
- upload cleanup still occurs on no-op/conflict;
- watch and connector paths either implement the same semantics or reject the option explicitly;
- no regression to current unconditional overwrite behavior.
I would avoid --if-newer until the source-revision trust model is explicit; raw client filesystem mtime is not a safe cross-endpoint ordering primitive.
This looks architecture-worthy rather than a small edge-case patch. If maintainers agree with the staged server contract, it would be useful to mark the first supported source kind and response schema before implementation starts.