[SC-16888] Reconcile stale agents on sync to prune archived/deleted VM records#124
Open
even-steven wants to merge 1 commit into
Open
Conversation
…eted records After each sync pull from ValidMind, delete synced agent rows whose vm_cuid is no longer present in the response (archived or deleted in VM). - AgentsRepo.DeleteSyncedStaleForOrg: deletes synced agents for the given org whose vm_cuid is not in keepCUIDs. If keepCUIDs is empty, removes all synced agents for the org. Manually-created agents (empty vm_organization_cuid) are never touched. - AgentsRepo.ListVMCUIDsWithBindings: returns vm_cuids of agents with at least one managed_agent_binding row. These are added to keepCUIDs before pruning so their ON DELETE CASCADE binding config is preserved. - syncAgents now collects activeCUIDs from successful upserts, fetches boundCUIDs, and calls DeleteSyncedStaleForOrg with the union. If the bindings query fails, the prune step is skipped (safe degradation). Co-authored-by: Cursor <cursoragent@cursor.com>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
What and why?
Previously, archiving or deleting an inventory record in ValidMind left the corresponding agent row in Atryum's database indefinitely — the sync only upserted, never removed. The agent remained visible in the Atryum UI until a user changed the org or record type in Settings (which triggers
DeleteSynced).This PR adds reconciliation to every sync run: after upserting the active records returned from ValidMind, any synced agent for the configured org whose
vm_cuidis no longer in the pull is deleted. This covers records archived or deleted in ValidMind.Safety: agents with active managed agent bindings (
managed_agent_bindingsFK →agents.id ON DELETE CASCADE) are exempted from pruning — theirvm_cuidis added tokeepCUIDsbefore the delete so binding configuration is not accidentally destroyed.Companion PR in the backend repo adds the CRUD-event triggers that fire the sync automatically: validmind/backend#3330
How to test
What needs special review?
ListVMCUIDsWithBindingsuses a raw SQL query (agents WHERE id IN (SELECT DISTINCT agent_cuid FROM managed_agent_bindings)). This works for both SQLite and PostgreSQL dialects but bypasses the squirrel query builder — intentional for the subquery pattern.ListVMCUIDsWithBindingserrors, the entire prune step is skipped (logged). This is a safe degradation: stale agents linger until the next successful sync rather than risking accidental deletion.Dependencies, breaking changes, and deployment notes
validmind/backend: validmind/backend#3330 — adds the CRUD event triggers that call Atryum's sync endpoint automatically.Release notes
Archived or deleted ValidMind inventory records are now automatically removed from Atryum's agent list on the next sync, keeping the two systems consistent without manual intervention.
Made with Cursor