Skip to content

fix(delta): add content-based validation to block-level operations (prevents silent overwrites) - #3404

Open
handnewb wants to merge 2 commits into
vectorize-io:mainfrom
handnewb:fix/delta-block-expected-content-validation
Open

fix(delta): add content-based validation to block-level operations (prevents silent overwrites)#3404
handnewb wants to merge 2 commits into
vectorize-io:mainfrom
handnewb:fix/delta-block-expected-content-validation

Conversation

@handnewb

Copy link
Copy Markdown
Contributor

Summary

Fixes #3273: In delta-mode mental-model refresh, ReplaceBlockOp and RemoveBlockOp targeted blocks by LLM-computed integer index with range-only validation. A wrong-but-in-range index silently overwrote or removed an unrelated block, and the refresh committed it as success.

Root Cause

The apply_operations function validated only that the index was within [0, len(blocks)) — no content check. If the LLM's computed index drifted (e.g., because the document changed since the LLM last saw it, or the LLM miscounted), the operation corrupted the document silently.

Solution

New expected_block field on ReplaceBlockOp and RemoveBlockOp:

  • Optional — None by default (fully backward compatible)
  • When set, the operation is only applied if section.blocks[index] serialises to the same JSON as expected_block
  • On mismatch, the operation is skipped with a clear reason ("index drift detected") — the document stays intact
  • New _blocks_equal() helper compares via model_dump_json()

Impact

  • LLM index drift no longer causes silent data corruption
  • Backward compatible: existing prompts that don't emit expected_block see no behavior change
  • The audit trail (skipped list) now surfaces drift events for debugging
  • Progressive enhancement: updated LLM prompts can emit expected_block for full protection

@handnewb
handnewb force-pushed the fix/delta-block-expected-content-validation branch from 3b994b4 to 2d63951 Compare August 11, 2026 21:24
ReplaceBlockOp and RemoveBlockOp now accept an optional expected_block
field.  When provided, the operation is only applied if the current block
at the target index serialises to the same JSON as expected_block.

This protects against LLM-computed index drift: a wrong-but-in-range index
would previously silently overwrite or remove an unrelated block.  The
operation is now skipped with a clear reason logged, and the document
stays intact.

The change is backward-compatible: when expected_block is None (the
default, and the only shape old LLM prompts produce), the existing
range-only validation is preserved unchanged.

Closes vectorize-io#3273.
@handnewb
handnewb force-pushed the fix/delta-block-expected-content-validation branch from 2d63951 to 6c21369 Compare August 11, 2026 21:28
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Delta block ops can silently overwrite the wrong block: bare index targeting, range-only validation

1 participant