Skip to content

fix: reject stale parent ids on tag import and make parent changes order-independent - #871

Closed
ormsbee wants to merge 3 commits into
openedx:mainfrom
ormsbee:ormsbee/858-stale-parent-id-simpler
Closed

ormsbee wants to merge 3 commits into
openedx:mainfrom
ormsbee:ormsbee/858-stale-parent-id-simpler

Conversation

@ormsbee

@ormsbee ormsbee commented Oct 15, 2026 •

Copy link
Copy Markdown
Contributor

Preamble

@ufedaseyeuconsultant, @mgwozdz-unicon: I did this as an experiment, to see if a more capable model (Fable 5.1) could make something equivalent to #867 without as much code and complexity. I was particularly worried by _ParentForest and whether we really needed that many tests to run through our edge cases. I have not actually looked through this code in detail—what follows is entirely Claude-generated. I mostly wanted to see how well it could do for a first pass. Please take a look at this and see if it seems like a reasonable approach to you. Thank you.

Description

Alternative to #867 with the same fixes in about a third of the code. Closes #858.

Problem

UpdateParentTag.applies_for compared a row's parent_id with the parent's pre-import external_id. A row that kept the old id was "no change", so _validate_parent never ran for it, and a file that renamed tag_1 to tag_50 while a child row still said parent_id: tag_1 was accepted silently (#858).

Fixing that exposed the neighbouring problems #867 found: parent references only worked when the parent's row came first; a kept grandchild of a deleted chain was removed by the delete cascade; parent cycles and imports deeper than the limit passed the Plan step (Tag.save validates only the saved tag's depth, cascades descendants unchecked, and silently corrupts lineage on a cycle); and some valid restructures failed part-way through because an intermediate step was too deep or cyclic.

Approach

#867 searches for an action order that is valid at every intermediate step, which needs a dependency sort with subtree walks, an in-memory forest simulator, and a second plan build when the simulation fails. This PR always detaches first instead, so no simulation is needed:

  1. End state once. TagImportPlan._end_state computes the tree as it will be after the import from one query. _validate_hierarchy rejects a parent cycle (once, on its first row) and a tree deeper than the limit there.
  2. Detach, then attach. A synthesized DetachTag action (like StageTagExternalIdForSwap) moves every existing tag whose parent changes to the root, after staging and before the row actions. After that every tag is at or above its final depth and none is an ancestor of its new parent, so the row actions can run in any order without exceeding the depth limit or building a cycle.
  3. Parents first. Rows are built after the row that creates or renames-in their parent. Each row has at most one such dependency, so the ordering is a short iterative walk, and a file whose parents already precede their children keeps its order.

For #858 itself, UpdateParentTag.applies_for now treats "same id as my current parent" as a parent change when this import gives that id to another tag, and _validate_parent rejects a row that names its own parent's pre-import id while a swap row takes that id, since it could mean "stay with my parent" or "move under the tag now called that". A child naming the end-state id of a swapped parent is accepted. _validate_parent recognises a renamed-in parent from _vacated_pks, so that check no longer depends on row order either.

What changed

All in src/openedx_tagging/import_export/:

  • actions.py: _validate_parent rewritten without exception-as-control-flow, plus the own-parent check; UpdateParentTag.applies_for as above, and its description cached at plan time so the execution log doesn't read "from parent None" after the detach; new DetachTag action; "Unknown parent" message no longer tells the admin to reorder the file.
  • import_plan.py: _existing_tags, _end_state, _validate_hierarchy, _build_detach_actions, _order_rows_parent_first; kept children are promoted before the first delete; a case-only rename no longer counts as vacating its id.
  • exceptions.py: ParentCycleError, TagTooDeepError.

User-visible changes

  • New Plan errors: Row N's parent_id (X) creates a parent cycle. and This import would leave tag X more than 6 levels deep. "Row N" is the N-th data row, as in the existing DuplicateFinalIdError.
  • A stale parent id on an existing child is rejected with the existing message: Parent tag tag_1 is renamed to tag_50 in this file; use tag_50 as the parent_id.
  • Unknown parent tag (X). Add a row for the parent to your file. replaces the old "add parent before the child" wording.
  • A plan that moves an existing tag lists Move tag X to the root first, so that the parent changes in this file can run in any order. before the row actions. With replace=True, promotions of kept children now precede the deletes.

Supporting information

Testing instructions

Automated: pytest tests/openedx_tagging/import_export/ -o addopts="" (214 tests) and pytest tests/openedx_tagging -o addopts="". Quality: tox -e quality.

Manual, through the taxonomy import endpoint or the Studio import wizard, on a taxonomy with tag_1 (root), tag_2 under tag_1, tag_3 (root), tag_4 under tag_3:

File (JSON tags rows) Expected
tag_50 (previous_id tag_1), tag_2 (parent_id tag_1), tag_3, tag_4 (parent_id tag_3) Rejected at the Plan step: Parent tag tag_1 is renamed to tag_50 in this file; use tag_50 as the parent_id.
tag_3 (previous_id tag_1), tag_1 (previous_id tag_3), tag_2 (parent_id tag_3), tag_4 (parent_id tag_1), in any row order Imports. Each child stays on the same physical parent; the export shows tag_2 under tag_3 and tag_4 under tag_1.
tag_1 (parent_id tag_2), plus the other three rows unchanged Rejected: Row 1's parent_id (tag_2) creates a parent cycle.

Other information

  • No migrations, no model changes, nothing in openedx-platform needs to change.
  • Existing tests that changed: two replace=True plan expectations (promotion before deletes, order only); two plans that move an existing tag gain the detach line; the "Unknown parent" wording; one _validate_parent unit test now sets _vacated_pks instead of a built rename action.
  • Design choices open to discussion: a tag that moves to the root gets both the detach line and its row's "update parent to None" line, which is a no-op by then. The too-deep error names the shallowest tag that would cross the limit, which can be an unchanged descendant rather than a row.
  • AI tools: Claude Code with Claude Fable 5.1 (claude-fable-5-1).

🤖 Generated with Claude Code

ormsbee and others added 3 commits October 14, 2026 18:01
…l_id

Closes openedx#858. Alternative to openedx#867 with a much smaller footprint.

UpdateParentTag.applies_for compared the row's parent_id with the parent's
pre-import external_id, so a row that kept the old id was "no change" and
_validate_parent never ran for it. When the same file renamed that parent
away (tag_1 -> tag_50), the stale reference was accepted silently.

Now such a row applies as a parent change whenever its parent is renamed
away (`_vacated_pks`), and _validate_parent rejects it with the existing
"renamed to X; use X" error. _validate_parent also rejects a row whose
parent_id is its own parent's pre-import id when another row takes that id
(a swap): that reference is ambiguous, while a child naming the end-state
id of a swapped parent is still accepted. A case-only rename no longer
counts as vacating the id, since external_id is case-insensitive.

Co-Authored-By: Claude Fable 5.1 <[email protected]>
…trees

Covers the remaining bugs openedx#867 found around parent_id handling, with one
mechanism instead of a dry-run simulator and a conditional rebuild:

- The plan computes the tree as it will be after the import (_end_state),
  rejects a parent cycle or a tree deeper than the limit there (Tag.save
  only validates the saved tag's depth, and silently corrupts lineage on a
  cycle), and records which existing tags change parent.
- Rows are built after the row that creates or renames-in their parent, so
  a child may precede its new parent in the file. _validate_parent
  recognises a renamed-in parent from _vacated_pks, whatever the row order.
- execute() detaches every moved tag before running the actions. After
  that each tag is at or above its final depth and none is an ancestor of
  its new parent, so no action can overflow or form a cycle part-way.
- Kept children of deleted tags are promoted before the first delete, so
  the cascade cannot remove a kept grandchild.
- A child whose deleted parent's id is reclaimed by a rename follows the
  renamed tag instead of being left at the root.

Co-Authored-By: Claude Fable 5.1 <[email protected]>
The detach of every tag whose parent changes used to run as a hidden phase at
the start of execute(). It is now a synthesized DetachTag action per tag,
like StageTagExternalIdForSwap, built after staging and before the row
actions, so the plan lists everything that runs. A root tag, a kept child
of a deleted tag (already promoted) and a row with an unknown parent get no
detach action. The tag is looked up by pk, since staging may have moved it
onto a placeholder id.

Co-Authored-By: Claude Fable 5.1 <[email protected]>
@ormsbee
ormsbee marked this pull request as ready for review October 15, 2026 17:59
@mgwozdz-unicon

Copy link
Copy Markdown
Contributor

I had Opus do an analysis of this PR to compare it with #867 and it claims that it does cover the same ground with a small caveat and less testing. Claude drafted the following comment to share its analysis:

To compare the two PRs, I ran #867's tests against this branch's code, because #867's tests map one-to-one to the acceptance criteria in #858, which Seva updated on 10-14 to cover all the extra fixes. I ran them on SQLite only. test_import_plan.py from #867 couldn't run here, because it imports #867's private _ParentForest class, so I checked plan-level behavior with a couple of direct generate_actions calls instead.

Coverage. Behaviorally, this PR covers nearly all of #858. Out of 199 tests, 156 pass, including the swap, row-order, delete-cascade, depth-limit, intermediate-step and case-variant scenarios. Most of the 43 failures are only wording differences: this PR keeps the existing "Parent tag X is renamed to Y in this file" message, and its depth error names the tag rather than using the ticket's quoted text. Both of those are fine with me. One mixed cycle (a rename row plus a new tag) gets the cycle error here instead of "Unknown parent tag", which #858 doesn't specify either way. Two gaps remain:

  1. The stale-parent error doesn't identify the row. ACs 1 and 3 and Additional fix 6 in Tag import: parent_id isn't re-validated when it matches the pre-import parent #858 require the error to name the child's row. Here the message reads Action error in 'update_parent' (#3): Parent tag COMP-P is renamed to COMP-P2 in this file; use COMP-P2 as the parent_id. In my test file the offending row was row 2, and the #3 is the action's position in the plan, so an admin can't map the error back to a line in their file. Adding the row number (the same "Row N" convention as DuplicateFinalIdError and the new ParentCycleError) or the child's id would close this.
  2. Every re-parent of an existing tag now adds a "Move tag X to the root first" action, including a simple move of c from under a to under b in an otherwise unchanged file. That conflicts with two ACs in Tag import: parent_id isn't re-validated when it matches the pre-import parent #858: Additional fix 1 says a file that already lists parents before children produces the same plan as before, and Additional fix 5 says a plan whose steps are all valid has no extra move-to-root actions.

Whether the gaps matter. I don't think either one is a correctness problem. The row number is a small usability fix that I'd like to see in whichever PR we go with. The extra move-to-root lines are the price of the always-detach approach, and they're what lets this PR drop the in-memory simulation and the second plan build. I think that's a reasonable trade, and that the two ACs it breaks describe #867's mechanism more than an outcome an admin needs. If we go this way, I'd rather amend those two ACs in #858 than add the simulation back.

The bigger difference between the PRs is test depth, not behavior. This PR adds about 360 lines of tests against about 1,900 in #867. Whichever implementation we keep, I'd suggest bringing over #867's TestImportAcceptanceScenarios class (adjusted for message wording), because it is the part that ties each #858 criterion to a test across JSON, CSV and row orders.

Comment on lines +128 to +131
found = (self.tag.parent_id or "").casefold() in {
renamed_to.casefold() for renamed_to in vacated_pks.values()
} or self._search_action(indexed_actions, CreateTag.name, "id", self.tag.parent_id, casefold=True)
if found and not (parent_tag is not None and self._is_current_parent(parent_tag)):

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggestion: these four lines took me a few reads. As far as I can tell, found means "some row of this file gives the id parent_id to a tag, by a rename onto it or by a create action built earlier", and line 131 means "accept that, unless the id currently belongs to this row's own parent (the ambiguous swap)". Naming the pieces would make both lines say that directly, something like:

renamed_onto_id = (self.tag.parent_id or "").casefold() in {
    renamed_to.casefold() for renamed_to in vacated_pks.values()
}
created_with_id = self._search_action(indexed_actions, CreateTag.name, "id", self.tag.parent_id, casefold=True)
names_own_parents_old_id = parent_tag is not None and self._is_current_parent(parent_tag)
if (renamed_onto_id or created_with_id) and not names_own_parents_old_id:
    return None

).format(parent_id=self.tag.parent_id),
)

def _is_current_parent(self, parent_tag: Tag) -> bool:

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nit: I found the name and docstring of _is_current_parent hard to follow, mostly because "this row's tag" could be a tag that doesn't exist yet. If I read it right, it answers "before the import, was parent_tag the parent of the tag this row describes?", looking that tag up by previous_id when the row renames it and by id otherwise. Something like _was_parent_before_import, with that lookup rule in the docstring, would be clearer to me.

"""
This action applies whenever there is a change on the parent.

A row that names its parent's current id is still a parent change when

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nit: I had trouble telling whether "current id" meant the id before or after the import. In this PR "current" always means before the import, but the new docstrings and comments use several terms for the same two ideas: "current id" here, "currently holding" (line 110), "own current parent" (line 115) and "pre-import id" (line 116) for the id before the import, and "final id" and "end state" in import_plan.py for the id after it. Could we settle on "pre-import id" and "final id" throughout?

Comment on lines +356 to +358
Such a row used to count as "no change", so a stale id got through
`_validate_parent` and a reclaimed id lost the child to the delete
phase's promotion.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nit: these lines describe how the code used to behave, which belongs in the commit history rather than the docstring. I'd delete them, or keep only the reason in present tense, for example "Otherwise a stale id would skip parent validation, and a child of a reclaimed id would be lost to the delete phase's promotion."

taxonomy_tag.parent is not None
and taxonomy_tag.parent.external_id != tag.parent_id
) or (taxonomy_tag.parent is None and tag.parent_id is not None)
parent = taxonomy_tag.parent

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nit: old_parent would make it clearer that this is the parent before the import, and it matches the old_parent label __str__ already uses on line 342.

action = self._action()
tag = self.taxonomy.tag_set.get(external_id='tag_2')
self.assertIsNotNone(tag.parent)
self.taxonomy.tag_set.filter(pk=tag.pk).update(external_id="oel-import-staging:x")

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nit: on first read I thought this test claimed that changing the external_id removes the parent. I think the update() simulates a staging action that already moved the tag onto a placeholder id, to show that DetachTag finds the tag by pk. A comment like "Simulate a staging action that already ran" would make that clear.

Comment on lines +815 to +824
"""
tag_1 and tag_3 swap ids, and each of their children names the id its
parent will hold after the import: tag_2 (under tag_1, which becomes
tag_3) says parent_id=tag_3, and tag_4 (under tag_3, which becomes tag_1)
says parent_id=tag_1. The full-replace import succeeds, whether the
children's rows come before or after the renames, and each child stays on
the same physical parent. See test_import_plan.TestTagImportPlan
.test_generate_actions_swap_child_naming_own_parent_stale_id_rejected
for the children naming the pre-import ids instead.
"""

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nit: the test body already lays out the rows and the expected result, so I don't think this docstring adds much, and the dotted reference to the other test would go stale silently if that test were renamed. I'd delete the docstring, or cut it to one line.

Comment on lines +632 to +638
"""
tag_2 is already under tag_1. The file renames tag_1 to tag_50 while the
tag_2 row still says parent_id=tag_1. That row matches the parent's
pre-import id, so it used to count as "no change" and slipped past
_validate_parent, although tag_1 names nothing after the import. It must
be rejected, and the error must name the parent's new id.
"""

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nit: this docstring describes how the code used to behave ("it used to count as 'no change'"), and the test is clear without it. I'd delete it.

Comment on lines +661 to +665
"""
tag_2 is under tag_1, and the file swaps the ids of tag_1 and tag_3. After
the import "tag_1" names the old tag_3, so a tag_2 row that still says
parent_id=tag_1 could mean "stay with my parent" or "move under the tag
now called tag_1". It is rejected, once, and told to use tag_3.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nit: the test is self-explanatory, so I don't think the docstring is needed.

Comment on lines +824 to +827
self.assertLess(
max(index for index, name in enumerate(names) if name == 'detach'),
min(index for index, name in enumerate(names) if name != 'detach'),
)

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nit: it took me a moment to see that this asserts every detach action comes before every other action in the plan. A one-line comment would help, for example "All detaches run before any row action." This only holds here because these cases have no renames or deletes, which are built before the detaches, so the comment could say that too.

@ormsbee

ormsbee commented Oct 17, 2026

Copy link
Copy Markdown
Contributor Author

Closing in favor of #877

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.

Tag import: parent_id isn't re-validated when it matches the pre-import parent

2 participants