Skip to content

fix(core): re-parenting doc upsert carries the whole subtree - #1378

Merged
ajianaz merged 3 commits into
developfrom
fix/doc-upsert-reparent-descendants
Oct 8, 2026
Merged

ajianaz merged 3 commits into
developfrom
fix/doc-upsert-reparent-descendants

Conversation

@ajianaz

@ajianaz ajianaz commented Oct 8, 2026

Copy link
Copy Markdown
Collaborator

What

  • Store::upsert_document (update branch): when the document's path changes (new parent, or no parent), rewrite the path/depth of its whole subtree. The rewrite is the same helper move_document now uses (rewrite_descendants, prefix substr replace + depth delta; empty legacy paths are skipped as in fix(core): documents has_children, legacy-path move, tag rename dupes, empty index rebuild, recompute_importance #1375).
  • Before writing anything, refuse a move into the document's own subtree (cycle) and a result deeper than MAX_DEPTH (10); the transaction rolls back untouched.
  • move_document shares the new helpers (subtree_selectors, rewrite_descendants); behaviour unchanged.

Why

#1377: tree a → k → g, upsert k under b left g at /a/k/g/. Delete, move and tree queries select subtrees by path LIKE, so deleting a would then delete g although it had moved with k.

Behaviour change

Re-parenting through doc upsert (HTTP/MCP/CLI) now also moves descendants and can now fail with the two validation errors above (previously it silently corrupted paths). CORE docs area, derived-data fix, no wire shape change; noted for the cloud notice (#117).

Testing

  • 5 new tests, all failing on the old code: subtree path/depth rewrite (incl. a sibling sharing a textual prefix), move to root, deleting the old parent keeps the moved subtree, cycle rejected atomically, depth overflow rejected atomically.
  • cargo fmt --check, cargo clippy --workspace --all-targets clean; cargo test --workspace green (core 654); vecq-only core build green (651).
  • Not covered: concurrent upserts (single transaction per call).

Fixes #1377

🤖 Generated with Claude Code

upsert_document rewrote only the document's own parent/path/depth when an
existing slug got a new parent, leaving descendants under the old path (a
later delete of the old parent cascaded onto the moved subtree). It now
rewrites descendants with the same helper as move_document, refuses a move
into the document's own subtree and a result deeper than MAX_DEPTH before
writing anything.

Fixes #1377

Co-Authored-By: Claude Sonnet 5.5 <[email protected]>
@ajianaz ajianaz added bug Something isn't working scope:core Core engine work labels Oct 8, 2026
@github-actions

github-actions Bot commented Oct 8, 2026 •

Copy link
Copy Markdown

🔍 Cora AI Code Review

✅ No issues found. Code looks good!


Review powered by cora-code · BYOK · MIT

Every subtree query matched paths with LIKE '<path>/%' unescaped, so '_' and
'%' in a stored path acted as wildcards: moving /my_doc/ also rewrote
/myxdoc/... and deleting it removed the look-alike subtree. All path LIKE
queries (move, delete, count/list descendants, re-parent rewrite, depth guard)
now escape the prefix and use ESCAPE '\'. Found by CodeCora on #1378.

Refs #1377

Co-Authored-By: Claude Sonnet 5.5 <[email protected]>
The re-parenting cycle check used the escaped LIKE prefix as a string prefix,
which never matched paths containing '_' or '%'. Use the raw directory form.
Found by CodeCora on #1378.

Refs #1377

Co-Authored-By: Claude Sonnet 5.5 <[email protected]>
@ajianaz
ajianaz merged commit 75a5324 into develop Oct 8, 2026
15 checks passed
@ajianaz
ajianaz deleted the fix/doc-upsert-reparent-descendants branch October 13, 2026 00:57
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

bug Something isn't working scope:core Core engine work

Projects

None yet

Development

Successfully merging this pull request may close these issues.

fix(core): doc upsert that re-parents a document leaves its descendants' path/depth stale

2 participants