localvectordb.patching module
Pure document patch operations (find/replace + span splice).
This module has no database dependency: it turns a document’s current content
plus a list of ops into the new content string. Every higher-level surface
(LocalVectorDB.patch(), the HTTP PATCH route, the patch_document MCP
tool, the CLI patch command) compiles down to apply_ops().
Op shapes (op selects the kind):
{"op": "splice", "start": int, "end": int, "text": str}– the primitive.start/endare character offsets into the original content (0 <= start <= end <= len(content));content[start:end]is replaced bytext.{"op": "replace", "find": str, "replace": str, "count": int = 1}–findmust occur exactlycounttimes (non-overlapping) or the whole patch fails. Each occurrence becomes a splice.{"op": "append", "text": str}/{"op": "prepend", "text": str}– the common no-anchor cases; map to zero-width splices at the ends.
All ops in one call are resolved against the original content (not applied
sequentially against each other’s output), validated non-overlapping, and applied
atomically. Any unmatched/ambiguous find, out-of-range or overlapping splice,
or malformed op raises PatchError and produces no output.
- class localvectordb.patching.PatchResult(updated: bool, new_hash: str, ops_applied: int)
Bases:
objectOutcome of a patch.
updatedisFalseonly when the ops produced content byte-for-byte identical to what was stored (and no metadata changed).new_hashis the SHA-256 of the resulting content;ops_appliedis the number of ops in the call.
- localvectordb.patching.apply_ops(content: str, ops: List[Dict[str, Any]]) str
Apply
opstocontentand return the new content.Ops resolve against the original
content. The resulting splices are sorted and validated non-overlapping, then applied in a single left-to-right pass. RaisesPatchErroron any invalid, unmatched, ambiguous, or overlapping op; nothing is applied partially.