Skip to content

fix(ui): fix kb id extraction logic for resource, sync tags#3763

Merged
TheodoreSpeaks merged 2 commits intostagingfrom
fix/kb-tool-ui
Mar 25, 2026
Merged

fix(ui): fix kb id extraction logic for resource, sync tags#3763
TheodoreSpeaks merged 2 commits intostagingfrom
fix/kb-tool-ui

Conversation

@TheodoreSpeaks
Copy link
Collaborator

Summary

Mothership changes to tags aren't refreshed in the ui and open up a "Resource not found" kb page. This is because tag operations return tag_id, which we attempt to open up. Adjusted to attempt to access id first from knowledgeBaseId instead of directly from id. Also added query invalidation for tags on kb operations.

Type of Change

  • Bug fix
  • New feature
  • Breaking change
  • Documentation
  • Other: ___________

Testing

  • Validated creating, editing, and deleting tags works as expected.

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)

Screenshots/Videos

@TheodoreSpeaks
Copy link
Collaborator Author

@cursor @greptile review

@cursor
Copy link

cursor bot commented Mar 25, 2026

Findings

No actionable defects found in this PR after reviewing the branch diff against staging and the Greptile context.

I specifically verified:

  • apps/sim/lib/copilot/resource-extraction.ts (around knowledge_base extraction): ID priority now prefers knowledgeBaseId sources before generic data.id, which correctly avoids treating tag IDs as KB IDs.
  • apps/sim/lib/copilot/tools/server/knowledge/knowledge-base.ts (create_tag, update_tag, delete_tag): tag mutation responses now include knowledgeBaseId; update_tag resolves KB ID via getTagDefinitionById when needed.
  • apps/sim/app/workspace/[workspaceId]/home/components/mothership-view/components/resource-registry/resource-registry.tsx: adds invalidation for knowledgeKeys.tagDefinitions(id), which directly addresses stale tags in UI.
  • apps/sim/lib/copilot/resource-extraction.test.ts: added tests cover key fixed paths (args-based KB ID, response-based KB ID, read-only exclusion).

Open questions / assumptions

  • Assumption: invalidating knowledgeKeys.tagDefinitions(id) is sufficient for the affected UI surfaces (and no additional per-document tag-definition queries need refresh for this bug).
  • Assumption: update_tag intentionally allows lookup by tagDefinitionId without requiring a caller-provided knowledgeBaseId.

Residual risk / testing gaps

  • I could not run tests in this cloud environment because bun is unavailable (bun: command not found), so runtime validation here is limited to static/code-path review.
  • Current new tests target update_tag-style flows; there is no explicit unit test for create_tag/delete_tag extraction paths, though code shape indicates they should behave correctly with the new knowledgeBaseId payload.

Secondary summary

Greptile’s “safe to merge” conclusion matches my review: this is a tightly scoped bug fix with coherent server payload updates + client extraction/invalidation changes, and no obvious regression introduced.

Open in Web Open in Cursor 

@vercel
Copy link

vercel bot commented Mar 25, 2026

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

1 Skipped Deployment
Project Deployment Actions Updated (UTC)
docs Skipped Skipped Mar 25, 2026 5:22pm

Request Review

@greptile-apps
Copy link
Contributor

greptile-apps bot commented Mar 25, 2026

Greptile Summary

This PR fixes two related bugs that together caused Mothership tag operations to open a "Resource not found" KB page and fail to refresh UI state. The root cause was that tag mutation responses return a tag_id in data.id, which the resource extraction logic was mistakenly interpreting as a knowledge base ID. The fix corrects the ID resolution priority and ensures knowledgeBaseId is always included in server responses.

Changes:

  • resource-extraction.ts: Reorders the KB ID fallback chain — args.knowledgeBaseId (from nested params) is now checked first, with data.id demoted to last resort. This prevents tag IDs from being used as KB IDs when routing users post-operation.
  • knowledge-base.ts: Enriches the create_tag, update_tag, and delete_tag response payloads with knowledgeBaseId. For update_tag, a getTagDefinitionById pre-flight call is added to retrieve the KB association (since knowledgeBaseId is not a required input for updates) and also provides early validation.
  • resource-registry.tsx: Adds knowledgeKeys.tagDefinitions(id) to the knowledgebase resource invalidator so that tag definition queries are refreshed when Mothership emits a resource_added event for a knowledge base.
  • resource-extraction.test.ts: Adds three new unit tests covering the corrected ID extraction behavior for tag mutations.

Confidence Score: 5/5

  • Safe to merge — all changes are targeted, well-tested bug fixes with no behavioral regressions for existing operations.
  • The fix correctly addresses both halves of the bug: the server now returns knowledgeBaseId in tag operation responses, and the client extraction logic now checks it before falling back to data.id. The fallback chain still works for all other KB operations (e.g., create where no args.knowledgeBaseId exists yet). The update_tag pre-fetch adds a small DB round-trip but is well-justified. New unit tests cover the corrected behavior and edge cases.
  • No files require special attention.

Important Files Changed

Filename Overview
apps/sim/lib/copilot/resource-extraction.ts Fixes the KB ID extraction priority order — args.knowledgeBaseId is now checked before data.id, preventing tag IDs from being mistakenly used as KB IDs for tag mutation operations.
apps/sim/lib/copilot/tools/server/knowledge/knowledge-base.ts Adds knowledgeBaseId to the response payload for create_tag, update_tag, and delete_tag operations. For update_tag, also introduces a pre-flight getTagDefinitionById check to retrieve the KB association and validate existence before updating.
apps/sim/lib/copilot/resource-extraction.test.ts New test file covering three scenarios: correct KB ID used when present in args, fallback to data.knowledgeBaseId when args omit it, and no resource created for read-only tag operations.
apps/sim/app/workspace/[workspaceId]/home/components/mothership-view/components/resource-registry/resource-registry.tsx Adds knowledgeKeys.tagDefinitions(id) invalidation to the knowledgebase resource invalidator, ensuring tag definitions are refreshed in the UI after Mothership events.

Sequence Diagram

sequenceDiagram
    participant C as Copilot
    participant KBTool as knowledge-base.ts
    participant DB as Database
    participant RE as resource-extraction.ts
    participant RR as resource-registry.tsx
    participant QC as QueryClient

    C->>KBTool: update_tag(tagDefinitionId)
    KBTool->>DB: getTagDefinitionById(tagDefinitionId)
    DB-->>KBTool: { knowledgeBaseId, ... }
    KBTool->>DB: updateTagDefinition(...)
    DB-->>KBTool: updatedTag
    KBTool-->>C: { data: { id: tagId, knowledgeBaseId, ... } }

    C->>RE: extractResourcesFromToolResult('knowledge_base', params, result)
    Note over RE: Priority: args.knowledgeBaseId → params.knowledgeBaseId<br/>→ result.knowledgeBaseId → data.knowledgeBaseId → data.id
    RE-->>C: [{ type: 'knowledgebase', id: kbId }]

    C->>RR: invalidateResourceQueries(kbId)
    RR->>QC: invalidate knowledgeKeys.lists()
    RR->>QC: invalidate knowledgeKeys.detail(kbId)
    RR->>QC: invalidate knowledgeKeys.tagDefinitions(kbId)
    QC-->>C: UI refreshed with correct KB page
Loading

Reviews (2): Last reviewed commit: "Pass knowledge base id back on edit tag" | Re-trigger Greptile

@TheodoreSpeaks TheodoreSpeaks marked this pull request as ready for review March 25, 2026 17:42
@cursor
Copy link

cursor bot commented Mar 25, 2026

PR Summary

Medium Risk
Changes adjust how knowledge base IDs are derived and returned for tag mutations, which affects chat-driven navigation and cache invalidation; mistakes could still cause stale UI or incorrect resource links.

Overview
Fixes Mothership/Chat resource tracking for knowledge base tag mutations by preferring knowledgeBaseId (from tool args or result) over tag id when extracting knowledge_base resources, preventing “resource not found” KB opens.

Ensures tag edits propagate by including knowledgeBaseId in create_tag/update_tag/delete_tag responses, validating tag existence on update_tag, and invalidating knowledgeKeys.tagDefinitions(kbId) whenever a knowledgebase resource is refreshed. Adds Vitest coverage for the updated extraction behavior and read-only exclusions.

Written by Cursor Bugbot for commit 6968e42. This will update automatically on new commits. Configure here.

@TheodoreSpeaks TheodoreSpeaks merged commit 8caaf01 into staging Mar 25, 2026
12 checks passed
Sg312 pushed a commit that referenced this pull request Mar 25, 2026
* fix(ui): fix kb id extraction logic for resource, sync tags

* Pass knowledge base id back on edit tag

---------

Co-authored-by: Theodore Li <theo@sim.ai>
@waleedlatif1 waleedlatif1 deleted the fix/kb-tool-ui branch March 25, 2026 23:12
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