Skip to content

feat: report archived course runs in the criteria-groups GET (#840) - #944

Merged
mgwozdz-unicon merged 10 commits into
openedx:mainfrom
ufedaseyeuconsultant:ufedaseyeu/840--archived-course-keys-in-get-competency-response
Nov 25, 2026
Merged

mgwozdz-unicon merged 10 commits into
openedx:mainfrom
ufedaseyeuconsultant:ufedaseyeu/840--archived-course-keys-in-get-competency-response

Conversation

@ufedaseyeuconsultant

@ufedaseyeuconsultant ufedaseyeuconsultant commented Nov 18, 2026 •

Copy link
Copy Markdown
Contributor

Description

Closes #840.

GET /api/cbe/v1/competencies/<tag_id>/criteria-groups/ (#681, with #682's course_keys scoping) returns a competency's criteria tree. Its response now also has archived_course_keys: the course run keys whose course-level group for this competency is archived. Since #717, creating a criterion for such a course run is rejected with a 400, and nothing unarchives a course-level group in the MVP, so the Courses & Content panel on the Competency Management page (#841) can use this list to tell an author, when they expand such a course run, that it no longer accepts new associations, instead of letting them run into the rejection.

{
  "groups": [...],
  "criteria": [...],
  "criteria_count": 3,
  "total_criteria_count": 3,
  "archived_course_keys": ["course-v1:OrgX+CS101+2026"]
}

The key is always present. Its value is a list of course run key strings, sorted, each listed once, and [] when nothing is archived. The other keys are unchanged.

How it works

get_competency_criteria_tree(tag, course_keys) in src/openedx_learning/applets/cbe/api.py reads every group of the competency, archived ones included, and computes the two results separately from that one load:

  • _find_archived_course_keys reports a course run when an archived group carries a course and sits directly under a root. That is the same definition of a course-level group that [BE] Enforce archived in Competency Criteria creation #717's create-rejection checks, so "can no longer take new criteria" holds by construction, and the two rules cannot drift apart. An archived root does not hide the archived course-level groups under it, because the list is computed from every loaded group, not from the walk; an archived root is also the normal state of a competency whose course runs are all archived, since archiving the last course-level group archives the root automatically. An archived leaf group is never reported: the API writes leaf groups without a course (ADR 0002 Decision 2 puts the course only on the course-level group), and a leaf group that carries one, which only a row written directly through the ORM can have, still fails the parent-is-a-root test.
  • _find_active_groups walks the groups down from the roots, skips an archived group with everything below it, and returns the active groups with the course run branch each one sits in. The tree (groups, criteria, and both counts) is built from that, unchanged in content.
  • The list follows the same course_keys scope as the tree: no course_keys reports every run, a list reports only the runs it names, and an empty list reports none.
  • The scoping compares course keys directly (the requested keys against each course-level group's course.course_key, which the groups query already loads), so no lookup resolves keys to course run ids. Queries: 2 without course_keys, 3 with it (the third is the unscoped total_criteria_count), 1 when the competency has no groups. CompetencyCriteriaTree in api.py carries the new archived_course_keys: list[CourseKey], and the view returns it as strings.

The existing IsAuthenticated and oel_tagging.view_taxonomy checks run before the tree is read: a caller who cannot view the competency's taxonomy gets a 403 with no list. The list does not depend on the caller's Studio access to the listed courses, because #841 shows its message for an archived course run to every author who can see that course run, including one who cannot edit it.

Who is affected

  • Course Authors: nothing visible yet. [FE] Explain archived competency rules in the Courses & Content panel #841 uses the list in the Courses & Content panel: an archived course run stays in the course list, and expanding it shows a message in place of its outline.
  • Developers consuming the endpoint: one new key in the response. No existing key is removed, renamed, or changed in value.

Supporting information

Testing instructions

Automated:

  1. pytest tests/openedx_learning/applets/cbe -o addopts="" (-o addopts="" skips the coverage flags tox.ini adds).
  2. The quality checks as CI runs them: lint-imports, pylint, pycodestyle, isort --check-only, pydocstyle src and mypy --show-traceback (mypy also checks the tests).

The new tests in test_api.py and test_views.py cover every scenario in #840, plus unit tests for the two helpers the tree read is split into:

  • An archived course-level group is reported by its course run key; an active run of the same course and another active run are not.
  • An archived leaf group, or an archived criterion next to an active one, under an active course-level group does not report its course run, with and without a course set on the leaf group (the second shape only a direct ORM write can produce).
  • An archived root over archived course-level groups X and Y reports exactly X and Y, with no keyless entry.
  • Six archived course runs with differing course codes, created out of alphabetical order, come back as one exact str-sorted list.
  • A sub-competency's archived course-level group is not reported for its parent.
  • The list is empty for a competency with no groups and for one where every group is active.
  • The list follows the scope: no course_keys reports X and Y, [X, Z] reports X, [] reports nothing at the API, and over a real request [X, Z] reports X.
  • A requested key that matches no course run is ignored wherever it appears in the request, and the tree comes back as if it were not named.
  • An archived course run is listed while its archived rows stay out of groups and criteria.
  • After a real DELETE through [BE] Build endpoint for removing a Competency Criteria Group #675's group endpoint, with learner progress on the only leaf group of course run X, the next GET reports X and leaves out X's rows.
  • A caller with no Studio access to X still sees X listed; a caller who cannot view the taxonomy gets a 403 with no list.
  • The query counts stay at 2 and 3 with archived rows present, and at 1 for a competency with no groups.

Each new test was checked by breaking the code it covers and confirming it fails for the stated reason.

Manual, against a devstack, with a competency tag <tag_id> that has criteria for course runs X and Y:

  1. GET /api/cbe/v1/competencies/<tag_id>/criteria-groups/. Expect "archived_course_keys": [].
  2. Record learner progress on the criterion in X's only leaf group (grade the subsection for a learner, or create a StudentCompetencyCriterionStatus row from a Django shell), then DELETE /api/cbe/v1/competencies/<tag_id>/criteria-groups/<leaf_group_id>/. Expect 200 with "archived": true.
  3. GET the tree again. Expect "archived_course_keys": ["<X>"] and none of X's groups or criteria.
  4. GET with ?course_keys=<Y>. Expect "archived_course_keys": []. GET with ?course_keys=<X>,<Y>. Expect ["<X>"].

Deadline

None.

Other information

🤖 Generated with Claude Code

Summary by CodeRabbit

  • New Features

    • The competency criteria response now includes archived course keys, allowing clients to identify archived course runs separately from the active criteria tree.
    • Course-key filtering applies to both the criteria tree and archived-course list. Results follow the requested key order, and unmatched keys are ignored.
  • Bug Fixes

    • Archived groups and their descendants are excluded from the active criteria tree.
    • The total criteria count reflects all active-tree criteria before course-key filtering.

ufedaseyeuconsultant and others added 7 commits November 18, 2026 00:22
…#840)

Adds archived_course_keys to CompetencyCriteriaTree and to the
criteria-groups response: the course run keys whose course-level group
for the selected competency is archived, sorted, each once, following
the course_keys scope.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
)

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Drop the docstring paragraph about the create path, refresh the stale
summary and view docstrings, reword the walk comments, and tighten the
tests: one run archived under two parents is listed once, an active
Rule Group under an archived course-level group stays hidden, and the
duplicate 403 view test is folded into the existing one.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
)

The function mixed run ranking, the group-tree walk, request scoping,
and the criteria fetch in one body. Each is now a module-private helper
with its own unit tests: _rank_requested_course_runs, _walk_group_tree,
_scope_groups_to_requested_runs, and _fetch_criteria_in_group_order.
The walk collects every archived course run and the caller limits that
list to the requested runs, so behavior and query counts are unchanged.

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
)

get_competency_criteria_tree() resolved the requested course keys to
CourseRun ids with get_course_run_ids(), only to compare those ids with
each group's course_id. The groups query already loads each group's
course, so the walk now records every kept group's branch by course key
and the request is matched by key in Python. A scoped read drops the
run-id lookup, from four queries to three. Matching by key is the same
comparison the lookup made: CourseRun.course_key is case sensitive and
so is CourseKey equality.

The walk also collects archived course keys directly, so the caller
filters that set by the requested keys instead of mapping run ids back
to keys at the end. _rank_requested_course_runs and
_archived_course_level_run are gone: the first is a single dict
comprehension at its one call site, and the second is one condition in
the walk, covered by the walk tests.

The names and comments now say what a value means. course_key_of_branch
maps each kept group's id to the course key of the branch it sits in,
or to None when the group is in no branch. The walk's comments state
what each step guarantees, and the rank comment says a rank is only the
position of a key in the request, used to order the returned branches.

Adds a test that a requested key matching no course run changes
nothing, wherever it sits in the request.

Co-Authored-By: Jesper Hodge <19345795+jesperhodge@users.noreply.github.com>
Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
…nedx#840)

The docstring described the result in terms of "reachable" groups and
the groups "above any course", which a reader had to decode from the
code. It now starts with the shape of the tree, with a diagram of the
root, course-level groups, Rule Groups, and criteria, and then covers
what is left out, how course_keys scopes the result, the order of
groups and criteria, the two counts, and when a course run is listed in
archived_course_keys.

Each claim was checked against the code. The group order is stated as
sorted by (parent_id, ordering, id), which keeps the children of one
parent together and is neither depth-first nor strictly level by level.
The groups list is empty when the root is archived, so it does not
always start with the root. The root is not listed as an archived course
run because the API creates it with no course.

Co-Authored-By: Jesper Hodge <19345795+jesperhodge@users.noreply.github.com>
Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
@openedx-webhooks openedx-webhooks added the open-source-contribution PR author is not from Axim or 2U label Nov 18, 2026
@openedx-webhooks

Copy link
Copy Markdown

Thanks for the pull request, @ufedaseyeuconsultant!

This repository is currently maintained by @axim-engineering.

Once you've gone through the following steps feel free to tag them in a comment and let them know that your changes are ready for engineering review.

🔘 Get product approval

If you haven't already, check this list to see if your contribution needs to go through the product review process.

  • If it does, you'll need to submit a product proposal for your contribution, and have it reviewed by the Product Working Group.
    • This process (including the steps you'll need to take) is documented here.
  • If it doesn't, simply proceed with the next step.
🔘 Provide context

To help your reviewers and other members of the community understand the purpose and larger context of your changes, feel free to add as much of the following information to the PR description as you can:

  • Dependencies

    This PR must be merged before / after / at the same time as ...

  • Blockers

    This PR is waiting for OEP-1234 to be accepted.

  • Timeline information

    This PR must be merged by XX date because ...

  • Partner information

    This is for a course on edx.org.

  • Supporting documentation
  • Relevant Open edX discussion forum threads
🔘 Get a green build

If one or more checks are failing, continue working on your changes until this is no longer the case and your build turns green.

Details
Where can I find more information?

If you'd like to get more details on all aspects of the review process for open source pull requests (OSPRs), check out the following resources:

When can I expect my changes to be merged?

Our goal is to get community contributions seen and reviewed as efficiently as possible.

However, the amount of time that it takes to review and merge a PR can vary significantly based on factors such as:

  • The size and impact of the changes that it introduces
  • The need for product review
  • Maintenance status of the parent repository

💡 As a result it may take up to several weeks or months to complete a review and merge your PR.

@coderabbitai

coderabbitai Bot commented Nov 18, 2026 •

Copy link
Copy Markdown

Explain Change →

No actionable comments were generated in the recent review.

Walkthrough

The criteria-tree API now returns archived course-run keys alongside groups and criteria. It filters archived groups from the active tree, applies optional course-key scoping to active branches and archived keys, and returns archived keys as strings in the REST response.

Changes: Criteria tree and REST response

Layer / File(s) Summary
Archived-aware criteria-tree read
src/openedx_learning/applets/cbe/api.py, tests/openedx_learning/applets/cbe/test_api.py, tests/openedx_learning/applets/cbe/utils.py
The tree result adds archived_course_keys. The API finds archived course-level groups, excludes archived groups and descendants from the active tree, scopes branches by requested course keys, and orders criteria by group. Tests cover archive handling, scoping, ordering, counts, and query behavior.
REST response and endpoint coverage
src/openedx_learning/applets/cbe/rest_api/v1/views.py, tests/openedx_learning/applets/cbe/test_views.py
The endpoint documents and returns archived course keys as strings. Tests cover scoping, access control, empty results, and archived course-level groups after deletion.

Estimated code review effort: 3 (Moderate) | ~25 minutes

Change: Feature · Severity of issue fixed: Medium

Sequence Diagram(s)

sequenceDiagram
  participant View as CompetencyCriteriaTreeView
  participant API as get_competency_criteria_tree
  participant Groups as CompetencyCriteriaGroup query
  View->>API: request criteria tree with course_keys
  API->>Groups: fetch active and archived groups
  Groups-->>API: group rows
  API-->>View: groups, criteria, archived_course_keys
  View-->>View: serialize course keys as strings
Loading

Possibly related issues

Possibly related PRs

  • openedx/openedx-core#948 — This PR changes scoped reads in get_competency_criteria_tree(), which this change also modifies to return archived course keys.
  • openedx/openedx-core#959 — This PR also changes the scoped criteria-tree read path extended by this change.

Suggested reviewers: alezconsultant, ormsbee

Merge Risk: Minimal · up to 6c9e1

The criteria-groups GET now reports archived course-run keys as an additive response field. Existing fields and access checks are unchanged. No merge-blocking risk was identified.

Security architecture risk: Low · up to 6c9e1

Readers who can view a competency’s taxonomy can now see which course runs have archived criteria for that competency, even if they cannot access those runs. The change exposes identifiers and archive status, not archived criteria or new write access.

Retained concerns

  • Low · security · inferred: The new field makes historical competency-to-course associations visible at taxonomy scope, with no course-run visibility check. If course visibility is intended to protect those associations, the taxonomy check alone does not enforce that policy.
Security review details

Security Blast Radius

  • observed — Each unfiltered response can disclose all archived course-run keys for one readable competency, but does not return archived criteria or learner records. Across requests, the scope is the competencies the caller can read under the effective taxonomy policy.

Trust Boundaries and Controls

  • observed — The endpoint requires an authenticated session or JWT and checks view_taxonomy on the tag’s taxonomy. The repository’s default rule allows enabled taxonomies and restricts disabled ones to taxonomy administrators. Course-key validation limits and normalizes the request filter; it does not authorize access to a course.

Hardening Proposals

  • proposed — Make the intended audience for archived course associations explicit. If those associations must inherit course-run visibility, apply the same visibility policy to the new field for both filtered and unfiltered reads.
Pre-merge checks: Passed 4

Check name Status Explanation
Description Check Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check Passed The title "feat: report archived course runs in the criteria-groups GET" matches the main change. The PR adds archived_course_keys to the criteria-groups GET response, and the summaries and objectives…
Linked Issues check Passed [#840] Requirements: add an always-present, sorted, unique archived_course_keys list of course run keys to the GET competency criteria-groups response. The list follows the course_keys scope, arch…
Out of Scope Changes check Passed The change set touches api.py (tree walk split into _find_archived_course_keys, _find_active_groups, _scope_groups_to_requested_course_runs, and _fetch_criteria_in_group_order; new dataclass…
Full details: Title check

Explanation

The title "feat: report archived course runs in the criteria-groups GET" matches the main change. The PR adds archived_course_keys to the criteria-groups GET response, and the summaries and objectives describe this field as the primary change. The title references the #840 issue number in the PR number slot, but it does not affect relevance. The title is short, specific, and in one sentence.

Full details: Linked Issues check

Explanation

[#840] Requirements: add an always-present, sorted, unique archived_course_keys list of course run keys to the GET competency criteria-groups response. The list follows the course_keys scope, archived rows stay out of groups and criteria, archived course-level groups are reported beneath an archived root, and callers without taxonomy view permission get no list.

Evidence at head 6c9e1e4: _find_archived_course_keys in src/openedx_learning/applets/cbe/api.py builds root_ids from every loaded group, then reports a group only when it is archived, has a course, and its parent_id is in root_ids. An archived root therefore does not hide its archived course-level children, and an archived leaf with a course is not reported. _find_active_groups walks down from active roots and skips archived groups with their descendants, so archived rows stay out of the tree. CompetencyCriteriaTree.archived_course_keys: list[CourseKey] carries the new field. The tree body of get_competency_criteria_tree was truncated in the tool output, so the scoping call and the view wiring are supported by the PR description, the docstrings, and the previous review, not by a fresh read of that body.

The change since the previous review is a refactor of the tree walk into helpers with the same stated behavior. The helper logic meets the acceptance scenarios. The PR description previously said nested course-level groups are reported; the code and the issue's suggested rule report only direct children of roots. The code matches the issue, so this is a description issue, not a requirement gap.

Full details: Out of Scope Changes check

Explanation

The change set touches api.py (tree walk split into _find_archived_course_keys, _find_active_groups, _scope_groups_to_requested_course_runs, and _fetch_criteria_in_group_order; new dataclass field), views.py (new response key and API documentation), CBE tests, and the test utility create_course_level_group. The helper split and scoping support the #840 field and the course_keys scope from #682. The test changes cover the #840 scenarios and the helpers. No frontend, migration, or unrelated endpoint change appears in the reviewed evidence.

Residual uncertainty: the raw summary says total_criteria_count counts the active tree before course scoping. The PR description states the same. I did not compare these counts with the base revision because the body of the tree function was not readable in this review, so this remains a minor uncertainty.

Finishing Touches

Generate unit tests (beta)

  • Create a new PR
  • Autofix · Keep fixing CodeRabbit findings and required CI, and resolving merge conflicts
Recent review info

Run configuration

  • Source mode: Auto
  • Repository: Unavailable
  • Branch: Unavailable
  • File: No configuration file from this repository applied
  • Commit: Unavailable
  • Layers: defaults
  • Review profile: CHILL
  • Plan: Advanced
  • Run ID: 8660683c-1a04-476c-8928-cf7a96d19d51
Commits

Reviewing files that changed from the base of the PR and between 76790ff and 6c9e1e4.

Files selected for processing: 2

  • src/openedx_learning/applets/cbe/rest_api/v1/views.py
  • tests/openedx_learning/applets/cbe/test_api.py
Files skipped from review as they are similar to previous changes: 1

  • tests/openedx_learning/applets/cbe/test_api.py

Included review availability: This review used your included allowance. Your plan provides up to 10 included reviews per hour; 9 remain after this review.

@mgwozdz-unicon mgwozdz-unicon left a comment

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.

Claude and I reviewed this at 2f71e8d, and Claude drafted this comment. Every scenario of #840 is implemented, each has a test that fails when its behavior is broken, and we found no defects. The branch merges cleanly into the current main, and comparing course keys in Python matches the same rows as the lookup it replaced. Item 1 needs to change before merge. The rest are smaller requests, mostly about names, docstrings, and tests.

  1. Compute archived_course_keys separately from the tree walk (api.py). _walk_group_tree scores 18 on cognitive complexity (uvx complexipy@8.3.0), and we aim for 12 or less. It is hard to follow because it does two jobs: it decides which groups are returned, and it collects the archived course runs. The second job is what needs the parent_was_kept flag and the else branch.

    • Please compute the list on its own, from the groups the query already loaded: every archived group that has a course and whose parent is the root. The query counts stay at 1, 2, and 3.
    • The walk then only skips an archived group with everything below it, and records the course run of each remaining group. Please have it return the list of active groups as well, so that get_competency_criteria_tree no longer tests group.id in course_key_of_branch to decide what is returned, and name the function after what it returns, for example _find_active_groups.
    • This gives the list the same definition of a course-level group that _create_leaf_group uses when it rejects a new criterion (#717), so "can no longer take new criteria" in the docstring holds by construction. Today the list uses a broader definition, the first group with a course on the path from the root, and the two agree only because the API never creates a group between the root and a course-level group. Please update the docstring bullet that starts "Precisely" to match, since it describes the broader rule.
    • In the tests, test_archived_course_keys_reports_a_course_level_group_below_a_course_less_group and test_archived_course_keys_lists_a_run_once_when_archived_under_two_course_less_groups go away, and the test_walk_group_tree_... tests change with the function. Please drop test_walk_group_tree_prunes_the_subtree_of_an_archived_group_above_any_course in any case: its tree cannot come from the API, and what it pins is a course run that is neither in the tree nor on the list. The existing test_get_competency_criteria_tree_returns_nothing_under_an_archived_root covers the same rule.
  2. Names (api.py, both test files).

    • course_key_of_branch reads as one course key for one branch. It is a dictionary from a group's id to the course key of the branch that group sits in. Please rename it to group_id_to_branch_course_key_map.
    • parent_branch_key and branch_key hold course keys: parent_branch_course_key and branch_course_key.
    • "kept" in the new helpers and "reachable" in the CompetencyCriteriaTree docstring, the view docstring, and the new test test_walk_group_tree_maps_each_reachable_group_to_its_branch_course mean the same thing. Please use one clearer wording everywhere, for example "in the active tree" (is_in_active_tree, active_groups).
    • rank_by_course_key is a dictionary such as {course_key: 1}, and the reader has to work out that the number is the key's position in the request. Please pass _scope_groups_to_requested_courses the plain list of requested keys instead, named something like course_keys_in_request_order, and let the helper number them. The line that limits the archived list to the requested keys can then test against the requested keys directly. The helper's own name says "courses" where everything else says "course runs".
    • "Rule Group" is the frontend's term, and this PR is the first to use it in the backend. Please say "leaf group", as _create_leaf_group and ADR 0002 do, in the api.py docstrings and comments and in the 25 places in the two test files (test names, docstrings, rule_group_carries_course, rule_group_b, x_rule_group).
  3. The docstring of get_competency_criteria_tree (api.py). Please correct these passages:

    • "Any other group outside every branch" announces a kind of group the API never creates, and the reader learns that only several lines later. Please describe the tree the API creates and say plainly that the root always comes back while it is active.
    • Delete "even rows that are not archived themselves" (archiving a group archives everything below it) and "It does not select a single course run's tree" (a request with one key does return one run's branch).
    • "An archived root does not hide the archived course-level groups below it" reads as an odd rule without its reason. Please add it: the root is archived automatically once its last course-level group is, so an archived root is the normal state of a competency whose course runs are all archived.
    • The paragraph on archived course runs has a line that ends early after "sorted by str," and needs rewrapping.
  4. Other docstrings and comments (api.py, views.py).

    • The CompetencyCriteriaTree docstring and the view docstring say "archived course runs". The course run is not archived, its course-level group for this competency is, and Studio already uses "archived" for runs past their end date. Please use the wording of the function's summary line. In the view docstring, "the sorted course run key strings that have an archived course-level group" says that a string has a group.
    • _scope_groups_to_requested_courses: please rewrite the docstring to say what the function does for the trees the API creates (keep the root, keep the requested branches in the order requested, drop the others), and remove the comment block directly under it, which repeats it. The two reasons worth keeping can sit next to their lines: the root gets 0 so that it sorts first, and sorted() is stable.
    • The comment above the position dictionary in get_competency_criteria_tree says it "only orders the returned branches". It also filters the archived list.
  5. Add a test that pins the sort of archived_course_keys (test_api.py). The keys come out of a set. With sorted(..., key=str) removed, the tests passed in 4 of 6 runs with different hash seeds, and a sort by run code also passed, because every test uses the run "2026". Please add one test with six or more archived course runs, with different course codes, created in an order that is not alphabetical, and assert the exact list.

  6. Tests that build a tree the API cannot produce (test_api.py). Please either build a tree the API can leave behind, or say in the docstring that it cannot.

    • test_archived_course_keys_lists_a_run_whose_rows_are_left_out_of_the_tree: x_active_rule_group is an active group under an archived course-level group. The test does not need it, and the docstring says "a run with only archived rows". Please archive it or remove it with its assert. The last assert in this test cannot fail, because the line before it already requires the criteria to be exactly y_criterion.
    • test_archived_course_keys_ignores_archived_rule_groups_and_criteria_under_an_active_course_level_group: please say in the docstring what the tree is and why the test runs twice. The second run puts the course on the archived leaf group, which only a row written outside the API can have, so that the test fails if the code reports every archived group with a course. The archived leaf group has no variable name; archived_leaf_group, active_leaf_group, and leaf_group_carries_course would show the tree.
    • test_scope_groups_to_requested_courses_keeps_groups_above_any_course_first uses a group with no course between the root and the course-level group.
    • test_walk_group_tree_places_a_group_re_parented_under_a_later_id_parent_by_its_real_parent: please describe the tree after the move in the docstring, and explain two things that were confusing. The groups have to be moved with update() because a child with a smaller id than its parent can exist only if it was created first and moved afterward. The assert on the query order checks the setup, that A is listed before its parent B, and the three final asserts show that every group still got the right course key.
    • In the rewritten walk tests, please keep an active leaf group next to an archived one under an active course-level group (the cascade archives a course-level group whose last active child is archived), name a group after where it sits (nested_under_leaf_group, not nested), assert both returned values, and leave out asserts that an earlier assert already implies.
  7. Smaller test items (test_api.py, test_views.py).

    • _create_course_level_group is defined word for word in both files. Please move it to tests/openedx_learning/applets/cbe/utils.py.
    • test_archived_course_keys_follows_the_course_keys_scope: please add a comment on the lines that turn the short names in the scope parameter into course keys.
    • The three test_scope_groups_to_requested_courses_... tests then pass that list ([course_key(course_run)], []) in place of a bare {course_key(course_run): 1}. Please say in the third test's docstring what was requested and in what order.
    • test_tree_response_lists_an_archived_course_run_the_caller_cannot_author_in: the docstring says "Reading the tree needs no tagging permission", and the view requires oel_tagging.view_taxonomy. What the test shows is that no authoring access to the course run is needed.
  8. PR description. Three statements need correcting. ADR 0002 does not describe a shape "where every group in a course subtree has the course": Decision 2 says only the course-level group carries it, since the 2026-10-22 changelog entry. _course_key_of has no unit test, so "unit tests for each helper" is not accurate. An archive reaches the root only when the root's last course-level group is archived.

ufedaseyeuconsultant and others added 2 commits November 24, 2026 18:13
openedx#840)

Addresses review items 1 to 4 on the criteria tree read.

archived_course_keys is now computed by _find_archived_course_keys from the
groups the query already loaded: every archived group that has a course and
whose parent is a root. That is the group _create_leaf_group checks when it
rejects a new criterion, so the docstring's claim that such a course run can
no longer take new criteria holds by construction. The query counts stay at
1, 2, and 3.

The tree walk is now _find_active_groups. It skips an archived group with
everything below it and returns both the active groups, in query order, and
the map from each active group's id to its branch course key, so
get_competency_criteria_tree no longer tests membership in that map to decide
what to return. The walk scores 11 on cognitive complexity instead of 18, and
get_competency_criteria_tree scores 8 instead of 12.

_scope_groups_to_requested_courses is now
_scope_groups_to_requested_course_runs and takes the plain list of requested
keys in request order, numbering them itself, and the scope helper tests pass
that list. Variables are renamed to
group_id_to_branch_course_key_map, parent_branch_course_key, and
branch_course_key. The docstrings and comments use "active tree" in place of
"kept" and "reachable", "leaf group" in place of "Rule Group", and "course runs
whose rules are archived" in place of "archived course runs". The
get_competency_criteria_tree docstring describes the tree the API creates,
states that the root always comes back while it is active, gives the reason an
archived root is the normal state, and drops the two misleading sentences.

The tests follow the code: the two archived-key tests and the walk test that
relied on a course-less group above a course-level group are removed, the walk
tests are reworked for the new function and assert both returned values, and
"Rule Group" naming becomes "leaf group" in both test files.

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
…enedx#840)

Addresses review items 5 to 7 on the criteria tree tests.

A new test archives six course runs with different course codes, created out
of alphabetical order, and asserts the exact sorted list. It fails when the
sort is removed and when the list is sorted by run code, and it passes under
every PYTHONHASHSEED value tried.

The test for a run whose rows are left out of the tree no longer builds an
active leaf group under an archived course-level group, and its final assert,
which the criteria assert before it already implied, is gone. The test that
ignores archived leaf groups states its tree in the docstring, explains why
it runs twice, and names the leaf group variables after their role. The
scope test with a course-less group above a course-level group says in its
docstring that the API cannot create that tree. The re-parenting test
describes the tree after the move, why the groups are moved with update(), and
what the query-order assert and the final asserts show.

_create_course_level_group moves to the shared cbe test utils as
create_course_level_group instead of being defined in both test modules. The
scope-keys test comments the lines that turn short names into course keys. The
ordering test among the scope helper tests says what was requested and in what
order. The view test about an
archived course run the caller cannot author in says it shows that no
authoring access is needed.

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
@ufedaseyeuconsultant

Copy link
Copy Markdown
Contributor Author

@mgwozdz-unicon

  1. archived_course_keys is now computed by its own helper, _find_archived_course_keys, from the groups the query already loaded: an archived group with a course whose parent is a root, the same course-level-group definition [BE] Enforce archived in Competency Criteria creation #717 checks. The walk became _find_active_groups: it only skips archived subtrees and records each remaining group's branch course run, returns the active groups too, and get_competency_criteria_tree no longer probes the mapping for membership. Query counts stay at 1, 2 and 3, and the counts tests still pin them. Cognitive complexity: _find_active_groups 11, _find_archived_course_keys 9, get_competency_criteria_tree 8. The "Precisely" bullet now states the strict rule, and the three tests you named are gone.
  2. All the renames are in: group_id_to_branch_course_key_map, parent_branch_course_key / branch_course_key, "in the active tree" (active_groups) everywhere "kept"/"reachable" was, _scope_groups_to_requested_course_runs taking the plain requested-keys list in request order and numbering it itself, and "leaf group" in place of "Rule Group" across api.py and both test files.
  3. The get_competency_criteria_tree docstring describes the tree the API creates, says the root always comes back while it is active, drops the two sentences you flagged, gives the archived-root rule its reason (the root is archived automatically once its last course-level group is), and the broken line is rewrapped.
  4. The "archived course runs" phrasing is gone from the dataclass and view docstrings; _scope_groups_to_requested_course_runs's docstring says what it does for API-created trees with the duplicate comment block removed and the two inline reasons kept at their lines; the position-dictionary comment now says it also filters the archived list.
  5. Added: six archived course runs with differing course codes created out of alphabetical order, asserted as one exact str-sorted list. It fails without sorted(..., key=str) under six different hash seeds.
  6. The impossible-tree tests are reworked as asked: the stray active leaf group is archived and the can't-fail asserts dropped; the twice-running test's docstring explains the tree and why it runs twice, with archived_leaf_group / active_leaf_group / leaf_group_carries_course names; the scope-ordering and re-parenting tests document their trees (the update() explanation included); the rewritten walk tests keep an active leaf next to an archived one under an active course-level group, assert both returned values, and name groups after where they sit.
  7. create_course_level_group lives once in the shared cbe test utils; the scope-keys test comments its key-building lines; the ordering test says what was requested and in what order; the authoring-access view test's docstring now says what it shows (no authoring access to the course run is needed; oel_tagging.view_taxonomy is still required).
  8. The PR description is updated: the mechanism section describes the separate computation and the strict rule, the ADR 0002 sentence matches Decision 2's current text (course only on the course-level group), the helper-tests claim is scoped to the two helpers that have them, and the archive-cascade sentence says the root is archived only with its last course-level group.

@mgwozdz-unicon mgwozdz-unicon left a comment

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.

Claude and I re-reviewed this at 76790ff, and Claude drafted this comment. All 8 items from the last review are done, and every scenario of #840 is still covered. The three functions now score 11, 9, and 8 on cognitive complexity (uvx complexipy@8.3.0), and the new sort test fails under every hash seed we tried once the sort is removed. Approving. Two small docstring nits are left:

  1. CompetencyCriteriaTreeView docstring (views.py). The course_keys paragraph still says "the groups above any course" three times. The get_competency_criteria_tree docstring now says "the root" for the same thing, so please use "the root" here as well.

  2. Two _find_active_groups tests (test_api.py). test_find_active_groups_drops_everything_below_an_archived_course_level_group and test_find_active_groups_drops_an_archived_leaf_group_and_its_subtree_but_keeps_its_active_sibling each build a tree the API cannot leave behind: an active group under an archived one, and a group nested under a leaf group. Please add a docstring sentence to each saying the API cannot create that tree, as the archived-root test next to them already does.

@mphilbrick211 mphilbrick211 moved this from Needs Triage to In Eng Review in Contributions Nov 24, 2026
…#840)

The view docstring's course_keys paragraph names the root the way
get_competency_criteria_tree's docstring does, instead of "the groups
above any course". The two _find_active_groups tests whose trees the
API cannot create now say so in their docstrings, like the
archived-root test next to them.

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
@ufedaseyeuconsultant

Copy link
Copy Markdown
Contributor Author

@mgwozdz-unicon Done

@mgwozdz-unicon
mgwozdz-unicon merged commit 152a5aa into openedx:main Nov 25, 2026
8 checks passed
@github-project-automation github-project-automation Bot moved this from In Eng Review to Done in Contributions Nov 25, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

open-source-contribution PR author is not from Axim or 2U

Projects

Status: Done

Development

Successfully merging this pull request may close these issues.

[BE] Add Archived Course Keys to GET Competency Criteria & Groups Endpoint

3 participants