Skip to content

fix(knowledge): skip sync and document processing when KB is deleted#4327

Merged
waleedlatif1 merged 3 commits intostagingfrom
waleedlatif1/connector-kb-archive
Apr 29, 2026
Merged

fix(knowledge): skip sync and document processing when KB is deleted#4327
waleedlatif1 merged 3 commits intostagingfrom
waleedlatif1/connector-kb-archive

Conversation

@waleedlatif1
Copy link
Copy Markdown
Collaborator

Summary

  • Guard dispatchSync to refuse enqueueing the connector-sync trigger task when the KB is deleted or the connector is archived/deleted
  • executeSync now returns a SyncResult with error instead of throwing on missing connector / deleted KB, so the connector-sync task succeeds without 3x retries
  • processDocumentAsync logs a warn and returns when the KB is deleted instead of throwing — stops the retry flood on knowledge-process-document

Type of Change

  • Bug fix

Testing

Tested manually

Checklist

  • Code follows project style guidelines
  • Self-reviewed my changes
  • Tests added/updated and passing
  • No new warnings introduced
  • I confirm that I have read and agree to the terms outlined in the Contributor License Agreement (CLA)

@vercel
Copy link
Copy Markdown

vercel Bot commented Apr 29, 2026

The latest updates on your projects. Learn more about Vercel for GitHub.

1 Skipped Deployment
Project Deployment Actions Updated (UTC)
docs Skipped Skipped Apr 29, 2026 0:31am

Request Review

@cursor
Copy link
Copy Markdown

cursor Bot commented Apr 29, 2026

PR Summary

Medium Risk
Changes control flow in background sync and document-processing paths to skip work and update connector/document state instead of throwing, which could affect scheduling/status transitions but is localized to knowledge sync/processing.

Overview
Prevents background work from being queued or retried when its underlying resources are no longer valid.

dispatchSync now checks for missing connectors, deleted knowledge bases, and archived/deleted connectors before triggering a sync; when the KB is deleted it also marks the connector as error and clears nextSyncAt.

executeSync and processDocumentAsync stop throwing on missing/invalid KB/connector and instead log a warning and return an error/failed status (including persisting a "Knowledge base deleted" error), avoiding Trigger.dev retry floods.

Reviewed by Cursor Bugbot for commit dfae79a. Configure here.

@greptile-apps
Copy link
Copy Markdown
Contributor

greptile-apps Bot commented Apr 29, 2026

Greptile Summary

This PR adds defensive guards in dispatchSync and executeSync to gracefully handle deleted knowledge bases and archived/deleted connectors, preventing retry floods on knowledge-process-document and connector-sync tasks. The changes are correct and complement the existing scheduler-level filtering (isNull(knowledgeBase.deletedAt)) already in sync/route.ts.

Confidence Score: 5/5

Safe to merge — no P1/P0 issues; logic is correct, type-safe, and complementary to existing scheduler-level filtering.

All three change sites (dispatchSync Trigger path, executeSync, processDocumentAsync) correctly handle the deleted-KB and archived/deleted-connector cases. SyncResult.error is already typed as optional string, the scheduler already pre-filters these cases so no zombie-scheduler loop is possible, and the connector/document DB states are left in unambiguous terminal form. Previously raised review concerns have been addressed in this HEAD commit.

No files require special attention.

Important Files Changed

Filename Overview
apps/sim/lib/knowledge/connectors/sync-engine.ts Adds KB-deleted and connector-archived/deleted guards in dispatchSync (Trigger.dev path) and converts throws to graceful SyncResult error returns in executeSync; connector state correctly cleaned up (status=error, nextSyncAt=null) on KB-deleted detection.
apps/sim/lib/knowledge/documents/service.ts Replaces throw on missing KB with a graceful early return that marks the document failed with processingError = 'Knowledge base deleted', eliminating the retry flood on the knowledge-process-document queue.

Flowchart

%%{init: {'theme': 'neutral'}}%%
flowchart TD
    CRON["Cron scheduler\n/api/knowledge/connectors/sync"] -->|"filters: active/error, nextSyncAt≤now,\narchivedAt=null, deletedAt=null,\nkb.deletedAt=null"| DS["dispatchSync(connectorId)"]

    DS -->|"isTriggerAvailable()"| TRG_PATH["Trigger.dev path"]
    DS -->|"else"| INLINE["executeSync (inline, fire-and-forget)"]

    TRG_PATH --> R1{"row found?"}
    R1 -->|"No"| WARN1["log warn, return"]
    R1 -->|"Yes"| R2{"kb deleted?"}
    R2 -->|"Yes"| UPD1["set connector status=error,\nnextSyncAt=null, log warn, return"]
    R2 -->|"No"| R3{"connector archived\nor deleted?"}
    R3 -->|"Yes"| WARN2["log warn, return"]
    R3 -->|"No"| ENQUEUE["Trigger.dev task enqueued"]
    ENQUEUE --> ES["executeSync(connectorId)"]

    INLINE --> ES

    ES --> C1{"connector found\n(not archived/deleted)?"}
    C1 -->|"No"| RET1["return {error: 'connector_unavailable'}"]
    C1 -->|"Yes"| C2{"KB found\n(not deleted)?"}
    C2 -->|"No"| UPD2["set connector status=error,\nnextSyncAt=null\nreturn {error: 'knowledge_base_deleted'}"]
    C2 -->|"Yes"| SYNC["Run sync logic"]

    PDA["processDocumentAsync"] --> KB_CHK{"KB found\n(not deleted)?"}
    KB_CHK -->|"No"| DOC_FAIL["mark document failed\n'Knowledge base deleted', return"]
    KB_CHK -->|"Yes"| PROC["Process document"]
Loading

Reviews (4): Last reviewed commit: "fix(knowledge): mark connector errored i..." | Re-trigger Greptile

Comment thread apps/sim/lib/knowledge/connectors/sync-engine.ts
Comment thread apps/sim/lib/knowledge/documents/service.ts
@waleedlatif1
Copy link
Copy Markdown
Collaborator Author

@greptile

@waleedlatif1
Copy link
Copy Markdown
Collaborator Author

@cursor review

Comment thread apps/sim/lib/knowledge/connectors/sync-engine.ts
Comment thread apps/sim/lib/knowledge/documents/service.ts
@waleedlatif1
Copy link
Copy Markdown
Collaborator Author

@greptile

@waleedlatif1
Copy link
Copy Markdown
Collaborator Author

@cursor review

Comment thread apps/sim/lib/knowledge/connectors/sync-engine.ts
@waleedlatif1
Copy link
Copy Markdown
Collaborator Author

@greptile

@waleedlatif1
Copy link
Copy Markdown
Collaborator Author

@cursor review

Copy link
Copy Markdown

@cursor cursor Bot left a comment

Choose a reason for hiding this comment

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

Cursor Bugbot has reviewed your changes and found 1 potential issue.

Fix All in Cursor

❌ Bugbot Autofix is OFF. To automatically fix reported issues with cloud agents, enable autofix in the Cursor dashboard.

Reviewed by Cursor Bugbot for commit 19eb8a1. Configure here.

Comment thread apps/sim/lib/knowledge/connectors/sync-engine.ts
Copy link
Copy Markdown

@cursor cursor Bot left a comment

Choose a reason for hiding this comment

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

✅ Bugbot reviewed your changes and found no new issues!

Comment @cursor review or bugbot run to trigger another review on this PR

Reviewed by Cursor Bugbot for commit dfae79a. Configure here.

@waleedlatif1 waleedlatif1 merged commit 7f12a13 into staging Apr 29, 2026
14 checks passed
@waleedlatif1 waleedlatif1 deleted the waleedlatif1/connector-kb-archive branch April 29, 2026 00:41
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.

1 participant