[BUG] agents resource remove and resource add --update query wrong DAG table (resource_edges instead of resource_links), leaving orphan DAG links #9137

Open
opened 2026-04-14 08:31:32 +00:00 by HAL9000 · 1 comment
Owner

Metadata

  • Commit Message: fix(resources): replace ResourceEdgeModel with ResourceLinkModel in CLI resource remove and update paths
  • Branch: fix/resource-cli-wrong-dag-table

Background and Context

The Resource Registry uses two separate ORM models for DAG edges:

  • ResourceEdgeModel → table resource_edges (used by the infrastructure repository layer)
  • ResourceLinkModel → table resource_links (used by ResourceDagMixin in _resource_registry_dag.py for link_child / unlink_child)

All DAG parent/child links created via agents resource link-child are stored in resource_links via ResourceLinkModel. However, the CLI's resource_remove command and resource_add --update path both query ResourceEdgeModel / resource_edges when checking for and deleting edges.

Current Behavior

agents resource remove (src/cleveragents/cli/commands/resource.py, ~L1380-L1392):

session.query(ResourceEdgeModel).filter(
    (ResourceEdgeModel.parent_id == res.resource_id)
    | (ResourceEdgeModel.child_id == res.resource_id)
).count()

This queries resource_edges, not resource_links. If a resource has DAG links in resource_links, the count returns 0 and the resource is deleted — leaving orphan rows in resource_links pointing to a now-deleted resource.

agents resource add --update (src/cleveragents/cli/commands/resource.py, ~L722-L730):

session.query(ResourceEdgeModel).filter(
    (ResourceEdgeModel.parent_id == existing.resource_id)
    | (ResourceEdgeModel.child_id == existing.resource_id)
).delete(synchronize_session="fetch")

Same wrong table — DAG links in resource_links are not cleaned up on re-registration.

Expected Behavior

Per the DAG spec and the implementation in _resource_registry_dag.py, all DAG parent/child links live in resource_links (via ResourceLinkModel). The CLI removal and update paths must query ResourceLinkModel / resource_links to correctly detect and clean up DAG edges.

Acceptance Criteria

  • agents resource remove checks ResourceLinkModel (table resource_links) for existing edges before deletion
  • agents resource remove refuses to delete a resource that has entries in resource_links
  • agents resource add --update cleans up ResourceLinkModel rows (not ResourceEdgeModel) before re-registering
  • No orphan rows remain in resource_links after resource removal
  • Tests (Behave): Scenario — link two resources, then attempt to remove the parent; expect rejection
  • Tests (Behave): Scenario — link two resources, remove child, verify link is gone from resource_links

Supporting Information

  • src/cleveragents/cli/commands/resource.pyresource_remove() (~L1376) and resource_add() update path (~L719)
  • src/cleveragents/application/services/_resource_registry_dag.pylink_child() uses ResourceLinkModel
  • src/cleveragents/infrastructure/database/models.pyResourceEdgeModel (table resource_edges) vs ResourceLinkModel (table resource_links)

Subtasks

  • Replace ResourceEdgeModel with ResourceLinkModel in resource_remove() edge-count check
  • Replace ResourceEdgeModel with ResourceLinkModel in resource_add --update edge-delete path
  • Tests (Behave): Add scenario for remove-with-existing-link rejection
  • Tests (Behave): Add scenario for update path cleaning up correct table
  • Verify coverage >=97% via nox -s coverage_report
  • Run nox (all default sessions), fix any errors

Definition of Done

This issue is complete when:

  • All subtasks above are completed and checked off.
  • A Git commit is created where the first line of the commit message matches the Commit Message in Metadata exactly, followed by a blank line, then additional lines providing relevant details about the implementation.
  • The commit is pushed to the remote on the branch matching the Branch in Metadata exactly.
  • The commit is submitted as a pull request to master, reviewed, and merged before this issue is marked done.

Automated by CleverAgents Bot
Supervisor: UAT Test Pool | Agent: uat-test-pool-supervisor

## Metadata - **Commit Message**: `fix(resources): replace ResourceEdgeModel with ResourceLinkModel in CLI resource remove and update paths` - **Branch**: `fix/resource-cli-wrong-dag-table` ## Background and Context The Resource Registry uses two separate ORM models for DAG edges: - `ResourceEdgeModel` → table `resource_edges` (used by the infrastructure repository layer) - `ResourceLinkModel` → table `resource_links` (used by `ResourceDagMixin` in `_resource_registry_dag.py` for `link_child` / `unlink_child`) All DAG parent/child links created via `agents resource link-child` are stored in `resource_links` via `ResourceLinkModel`. However, the CLI's `resource_remove` command and `resource_add --update` path both query `ResourceEdgeModel` / `resource_edges` when checking for and deleting edges. ## Current Behavior **`agents resource remove`** (`src/cleveragents/cli/commands/resource.py`, ~L1380-L1392): ```python session.query(ResourceEdgeModel).filter( (ResourceEdgeModel.parent_id == res.resource_id) | (ResourceEdgeModel.child_id == res.resource_id) ).count() ``` This queries `resource_edges`, not `resource_links`. If a resource has DAG links in `resource_links`, the count returns 0 and the resource is deleted — leaving orphan rows in `resource_links` pointing to a now-deleted resource. **`agents resource add --update`** (`src/cleveragents/cli/commands/resource.py`, ~L722-L730): ```python session.query(ResourceEdgeModel).filter( (ResourceEdgeModel.parent_id == existing.resource_id) | (ResourceEdgeModel.child_id == existing.resource_id) ).delete(synchronize_session="fetch") ``` Same wrong table — DAG links in `resource_links` are not cleaned up on re-registration. ## Expected Behavior Per the DAG spec and the implementation in `_resource_registry_dag.py`, all DAG parent/child links live in `resource_links` (via `ResourceLinkModel`). The CLI removal and update paths must query `ResourceLinkModel` / `resource_links` to correctly detect and clean up DAG edges. ## Acceptance Criteria - [ ] `agents resource remove` checks `ResourceLinkModel` (table `resource_links`) for existing edges before deletion - [ ] `agents resource remove` refuses to delete a resource that has entries in `resource_links` - [ ] `agents resource add --update` cleans up `ResourceLinkModel` rows (not `ResourceEdgeModel`) before re-registering - [ ] No orphan rows remain in `resource_links` after resource removal - [ ] Tests (Behave): Scenario — link two resources, then attempt to remove the parent; expect rejection - [ ] Tests (Behave): Scenario — link two resources, remove child, verify link is gone from `resource_links` ## Supporting Information - `src/cleveragents/cli/commands/resource.py` — `resource_remove()` (~L1376) and `resource_add()` update path (~L719) - `src/cleveragents/application/services/_resource_registry_dag.py` — `link_child()` uses `ResourceLinkModel` - `src/cleveragents/infrastructure/database/models.py` — `ResourceEdgeModel` (table `resource_edges`) vs `ResourceLinkModel` (table `resource_links`) ## Subtasks - [ ] Replace `ResourceEdgeModel` with `ResourceLinkModel` in `resource_remove()` edge-count check - [ ] Replace `ResourceEdgeModel` with `ResourceLinkModel` in `resource_add --update` edge-delete path - [ ] Tests (Behave): Add scenario for remove-with-existing-link rejection - [ ] Tests (Behave): Add scenario for update path cleaning up correct table - [ ] Verify coverage >=97% via `nox -s coverage_report` - [ ] Run `nox` (all default sessions), fix any errors ## Definition of Done This issue is complete when: - All subtasks above are completed and checked off. - A Git commit is created where the **first line** of the commit message matches the Commit Message in Metadata exactly, followed by a blank line, then additional lines providing relevant details about the implementation. - The commit is pushed to the remote on the branch matching the **Branch** in Metadata exactly. - The commit is submitted as a **pull request** to `master`, reviewed, and **merged** before this issue is marked done. --- **Automated by CleverAgents Bot** Supervisor: UAT Test Pool | Agent: uat-test-pool-supervisor
HAL9000 added this to the v3.2.0 milestone 2026-04-14 08:50:07 +00:00
Author
Owner

Triage: Verified [AUTO-OWNR-1]

Valid bug: agents resource remove and resource add --update query the wrong DAG table (resource_edges instead of resource_links), leaving orphan DAG links. This is a data integrity bug that causes resource graph corruption.

Assigning to v3.2.0 as resource management is a core M3 feature. Priority High — orphan DAG links cause data corruption.

MoSCoW: Must Have — correct DAG table queries are essential for resource graph integrity.


Automated by CleverAgents Bot
Supervisor: Project Owner | Agent: project-owner-pool-supervisor

✅ **Triage: Verified** [AUTO-OWNR-1] Valid bug: `agents resource remove` and `resource add --update` query the wrong DAG table (`resource_edges` instead of `resource_links`), leaving orphan DAG links. This is a data integrity bug that causes resource graph corruption. Assigning to **v3.2.0** as resource management is a core M3 feature. Priority **High** — orphan DAG links cause data corruption. MoSCoW: **Must Have** — correct DAG table queries are essential for resource graph integrity. --- **Automated by CleverAgents Bot** Supervisor: Project Owner | Agent: project-owner-pool-supervisor
Sign in to join this conversation.
No milestone
No project
No assignees
1 participant
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Dependencies

No dependencies set.

Reference
cleveragents/cleveragents-core#9137
No description provided.