Shared task board where people and AI coding agents work as teammates. Source-available, self-hostable.
Go
fix(apps): keep Remote MCP app calls out of the monthly app call limit (#11529)
pseudoyu35477e6d7ad9
diff --git a/server/internal/connectedapps/gateway.go b/server/internal/connectedapps/gateway.goindex 86c9cc0ff0..1fae7a63fe 100644--- a/server/internal/connectedapps/gateway.go+++ b/server/internal/connectedapps/gateway.go@@ -509,6 +509,10 @@ func (s *Service) toolRun(ctx context.Context, rn *run, toolSlug string, args maif refusal := s.refuseCall(ctx, rn, app, &call, policy); refusal != nil {return refusal}+ // Last: it counts the call, which runs as soon as this returns nil.+ if exceeded := s.overQuota(ctx, &call); exceeded != nil {+ return textResult(exceeded.Message(), true)+ }resp, err := s.composio.ExecuteTool(ctx, acct.connectedAccountID, acct.composioUserID, *tool, args)s.touch(ctx, rn.workspaceID, acct)if err != nil {@@ -529,8 +533,8 @@ func (s *Service) toolRun(ctx context.Context, rn *run, toolSlug string, args ma}// refuseCall applies what stands between an allowed policy and executing a-// call: a blocking rule, the run losing access, the account's rate limit, and-// the workspace's monthly app call allowance.+// call: a blocking rule, the run losing access and the account's rate limit.+// Composio calls then check the workspace's monthly app call allowance.// It records and returns the refusal, or nil when the call may run.func (s *Service) refuseCall(ctx context.Context, rn *run, app db.AgentApp, call *toolCall, policy string) map[string]any {if policy == PolicyBlock {@@ -557,10 +561,6 @@ func (s *Service) refuseCall(ctx context.Context, rn *run, app db.AgentApp, calls.recordCall(ctx, *call)return textResult(fmt.Sprintf("rate limited: this %s account already ran %d calls in the last minute. Wait a minute before the next call, and batch work where the app allows it.", call.toolkit, s.perMinute), true)}- // Last: it counts the call, which runs as soon as this returns nil.- if exceeded := s.overQuota(ctx, call); exceeded != nil {- return textResult(exceeded.Message(), true)- }return nil}diff --git a/server/internal/handler/connected_apps_quota_test.go b/server/internal/handler/connected_apps_quota_test.goindex e29930db75..fbc89dea90 100644--- a/server/internal/handler/connected_apps_quota_test.go+++ b/server/internal/handler/connected_apps_quota_test.go@@ -19,6 +19,16 @@ import (// newAppsQuotaFixture meters the fixture's gateway with the real app call// quota under an enforced limit.func newAppsQuotaFixture(t *testing.T, limit int) (*appsFixture, *entitlementtest.Stub) {+ t.Helper()+ stub, meter := enforceAppCallLimit(t, limit)+ f := newAppsFixture(t, meter)+ f.h.Entitlements = stub+ return f, stub+}++// enforceAppCallLimit returns an entitlement stub that enforces limit app+// calls this period, and the option that meters a gateway with it.+func enforceAppCallLimit(t *testing.T, limit int) (*entitlementtest.Stub, func(*connectedapps.Config)) {t.Helper()if testHandler == nil {t.Skip("database not available")@@ -30,16 +40,14 @@ func newAppsQuotaFixture(t *testing.T, limit int) (*appsFixture, *entitlementtesAction: entitlement.ActionEnforce, Limit: &limit, PeriodStart: &start, PeriodEnd: &end, ResetAt: &end,Notifications: &entitlement.NotificationPolicy{OnRejection: entitlement.NotificationFirstRejectionPerLimit},}})- f := newAppsFixture(t, func(cfg *connectedapps.Config) {- cfg.CallQuota = service.AppToolCallQuota{Queries: testHandler.Queries, Entitlements: stub, TxStarter: testPool}- })- f.h.Entitlements = stubt.Cleanup(func() {ctx := context.Background()testPool.Exec(ctx, `DELETE FROM app_tool_call_quota_period WHERE workspace_id = $1`, testWorkspaceID)testPool.Exec(ctx, `DELETE FROM entitlement_limit_overage WHERE workspace_id = $1 AND gate = 'app_tool_calls'`, testWorkspaceID)})- return f, stub+ return stub, func(cfg *connectedapps.Config) {+ cfg.CallQuota = service.AppToolCallQuota{Queries: testHandler.Queries, Entitlements: stub, TxStarter: testPool}+ }}func (f *appsFixture) callOutcomes(t *testing.T) []string {@@ -97,6 +105,30 @@ func TestConnectedApps_MonthlyQuotaRefusesGatewayCalls(t *testing.T) {}}+// Remote MCP apps do not run through Composio: their calls neither count+// against the monthly limit nor stop at it.+func TestConnectedApps_MonthlyQuotaSkipsRemoteApps(t *testing.T) {+ stub, meter := enforceAppCallLimit(t, 0)+ f := newRemoteAppsFixture(t, meter)+ f.h.Entitlements = stub+ f.connect(t, testUserID)+ f.putAgentApps(t, testUserID, []map[string]string{{"toolkit_slug": "mobbin", "account_scope": "personal", "level": "read"}})++ _, result := f.remote(t, f.token(t), "tools/call", map[string]any{"name": "search_screens", "arguments": map[string]any{}})+ if !strings.Contains(string(result), `"type":"image"`) {+ t.Fatalf("remote call with no app calls left = %s", result)+ }+ if got := f.callOutcomes(t); len(got) != 1 || got[0] != "ok" {+ t.Fatalf("call log = %v, want [ok]", got)+ }+ var metered, periods int+ if err := testPool.QueryRow(context.Background(), `+ SELECT (SELECT count(*) FROM app_tool_call WHERE task_id = $1 AND metered_period_start IS NOT NULL),+ (SELECT count(*) FROM app_tool_call_quota_period WHERE workspace_id = $2)`, f.taskID, testWorkspaceID).Scan(&metered, &periods); err != nil || metered != 0 || periods != 0 {+ t.Fatalf("metered calls %d, quota periods %d (err %v); want none", metered, periods, err)+ }+}+// A confirmed call approved after the workspace ran out fails the request// instead of running it.func TestConnectedApps_MonthlyQuotaRefusesApprovedCalls(t *testing.T) {diff --git a/server/internal/handler/connected_apps_remote_test.go b/server/internal/handler/connected_apps_remote_test.goindex 4498bacffd..de3cf98977 100644--- a/server/internal/handler/connected_apps_remote_test.go+++ b/server/internal/handler/connected_apps_remote_test.go@@ -101,7 +101,7 @@ type remoteAppsFixture struct {up *remoteFakeUpstream}-func newRemoteAppsFixture(t *testing.T) *remoteAppsFixture {+func newRemoteAppsFixture(t *testing.T, opts ...func(*connectedapps.Config)) *remoteAppsFixture {t.Helper()f := newAppsFixture(t)box, err := secretbox.New(bytes.Repeat([]byte{7}, secretbox.KeySize))@@ -109,7 +109,7 @@ func newRemoteAppsFixture(t *testing.T) *remoteAppsFixture {t.Fatal(err)}up := &remoteFakeUpstream{}- apps, err := connectedapps.New(connectedapps.Config{+ cfg := connectedapps.Config{Queries: f.h.Queries,Composio: f.h.Composio,TokenSecret: connectedapps.DeriveTokenSecret([]byte("apps-test-secret")),@@ -117,7 +117,11 @@ func newRemoteAppsFixture(t *testing.T) *remoteAppsFixture {SecretBox: box,DB: testPool,RemoteUpstream: up,- })+ }+ for _, opt := range opts {+ opt(&cfg)+ }+ apps, err := connectedapps.New(cfg)if err != nil {t.Fatal(err)}