Skip to content

fix(agent): always fetch latest custom tool from DB when customToolId is present#3208

Merged
waleedlatif1 merged 4 commits intostagingfrom
fix/custom-tools
Feb 12, 2026
Merged

fix(agent): always fetch latest custom tool from DB when customToolId is present#3208
waleedlatif1 merged 4 commits intostagingfrom
fix/custom-tools

Conversation

@waleedlatif1
Copy link
Collaborator

Summary

  • Always fetch latest custom tool definition from DB when customToolId is present, even if inline schema exists
  • Graceful fallback to inline schema if DB fetch fails
  • Fixes version mismatch where LLM received stale inline schema but execution validated against live DB schema

Type of Change

  • Bug fix

Testing

  • Added 6 tests for customToolId resolution (DB as source of truth, fallback, no customToolId)
  • 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

vercel bot commented Feb 12, 2026

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

1 Skipped Deployment
Project Deployment Actions Updated (UTC)
docs Skipped Skipped Feb 12, 2026 9:05pm

Request Review

@greptile-apps
Copy link
Contributor

greptile-apps bot commented Feb 12, 2026

Greptile Overview

Greptile Summary

This PR fixes a critical version mismatch issue where the LLM received stale inline tool schemas while execution validated against the latest database schema. The fix ensures the database is always the single source of truth when customToolId is present.

Key changes:

  • Modified createCustomTool to always fetch from DB when customToolId exists (previously only fetched if inline schema was missing)
  • Added graceful fallback: if DB fetch fails but inline schema exists, use inline schema instead of returning null
  • Ensures both schema and code are always synchronized from the same source (DB or inline, never mixed)

Testing:

  • 6 new test cases validate DB-first behavior, fallback scenarios, and that DB code is used during execution
  • Tests confirm inline schema is no longer used when customToolId is present and DB fetch succeeds

Confidence Score: 5/5

  • This PR is safe to merge with minimal risk
  • The implementation is straightforward, well-tested, and fixes a clear bug. The logic change is minimal (removing && !schema condition and restructuring if-else), thoroughly tested with 6 comprehensive test cases, and maintains backward compatibility via graceful fallback.
  • No files require special attention

Important Files Changed

Filename Overview
apps/sim/executor/handlers/agent/agent-handler.ts Updated createCustomTool to always fetch latest schema/code from DB when customToolId is present, with graceful fallback to inline schema on fetch failure
apps/sim/executor/handlers/agent/agent-handler.test.ts Added comprehensive test suite covering DB-first resolution, fallback scenarios, and validation that DB schema/code is used over stale inline data

Sequence Diagram

sequenceDiagram
    participant Agent as AgentBlockHandler
    participant CT as createCustomTool
    participant DB as fetchCustomToolById
    participant LLM as LLM Provider

    Agent->>CT: createCustomTool(tool)
    
    alt customToolId present
        CT->>DB: fetchCustomToolById(customToolId)
        alt DB fetch succeeds
            DB-->>CT: {schema, code, title}
            Note over CT: Use DB schema & code<br/>(always latest)
        else DB fetch fails
            DB-->>CT: null
            alt inline schema exists
                Note over CT: Fallback to inline schema
            else no inline schema
                CT-->>Agent: return null
            end
        end
    else no customToolId
        Note over CT: Use inline schema & code
    end
    
    CT->>CT: filterSchemaForLLM(schema)
    CT-->>Agent: tool definition
    Agent->>LLM: executeProviderRequest(tools)
    
    alt LLM calls tool
        LLM-->>Agent: tool call
        Agent->>CT: executeFunction(params)
        Note over CT: Execute with DB code<br/>(same source as schema)
    end
Loading

Copy link
Contributor

@greptile-apps greptile-apps bot left a comment

Choose a reason for hiding this comment

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

2 files reviewed, no comments

Edit Code Review Agent Settings | Greptile

@waleedlatif1 waleedlatif1 merged commit ebc2ffa into staging Feb 12, 2026
11 checks passed
@waleedlatif1 waleedlatif1 deleted the fix/custom-tools branch February 12, 2026 23:31
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