Skip to content

Open-source AI teammates in Slack, Teams, Discord, Telegram and Mattermost. Self-host it, with any agent harness and any model.

fix(agent-providers): a Switch without the grants route is not asked for grants every 30 s

raullopez-sandboxa68acfa5fc2a
diff --git a/console/packages/agent-providers/src/host/service-access.test.ts b/console/packages/agent-providers/src/host/service-access.test.ts
index 51fa1288e..c5c571e7c 100644
--- a/console/packages/agent-providers/src/host/service-access.test.ts
+++ b/console/packages/agent-providers/src/host/service-access.test.ts
@@ -2,6 +2,7 @@ import { describe, expect, it, vi } from 'vitest';
import {
grantedSkills,
issueServiceToken,
+ NO_GRANTS_ROUTE_MS,
readServiceGrants,
type ServiceGrant,
skillContext,
@@ -115,8 +116,22 @@ describe('readServiceGrants', () => {
expect(init?.headers).toEqual({ Authorization: 'Bearer agent-key' });
});
- it('takes a Switch without the route as granting nothing', async () => {
- expect(await readServiceGrants(SWITCH, answering(404, { detail: 'Not Found' }))).toEqual([]);
+ it('takes a Switch without the route as granting nothing, and does not ask it again for a while', async () => {
+ vi.useFakeTimers();
+ try {
+ const without = { ...SWITCH, agentId: 'agent-without-route' };
+ const notFound = answering(404, { detail: 'Not Found' });
+ expect(await readServiceGrants(without, notFound)).toEqual([]);
+ // Every check, each session start: asking each time would trip Switch's refusal breaker.
+ expect(await readServiceGrants(without, notFound)).toEqual([]);
+ expect(notFound).toHaveBeenCalledTimes(1);
+ vi.advanceTimersByTime(NO_GRANTS_ROUTE_MS + 1);
+ const granted = answering(200, { grants: [GITHUB] });
+ expect(await readServiceGrants(without, granted)).toEqual([GITHUB]);
+ expect(granted).toHaveBeenCalledTimes(1);
+ } finally {
+ vi.useRealTimers();
+ }
});
it('says when Switch refuses or cannot be reached', async () => {
diff --git a/console/packages/agent-providers/src/host/service-access.ts b/console/packages/agent-providers/src/host/service-access.ts
index af736d5d3..ce7775753 100644
--- a/console/packages/agent-providers/src/host/service-access.ts
+++ b/console/packages/agent-providers/src/host/service-access.ts
@@ -119,8 +119,19 @@ export type ServiceSkill = { name: string; content: string };
export type ServiceEndpoint = { endpoint: string; token: string; agentId: string };
/**
- * The agent's grants. A Switch from before service connections answers the
- * route with 404, which is the truth: it grants nothing.
+ * How long a Switch that answered the grants route with 404 is taken at its
+ * word before it is asked again. Its refusals are counted: asked on every
+ * check (every 30 s, and each session start) it soon answers 429 instead,
+ * which reads as Switch refusing the grants rather than having none.
+ */
+export const NO_GRANTS_ROUTE_MS = 10 * 60_000;
+
+/** Where the grants route answered 404, by endpoint and agent, until when that is believed. */
+const noGrantsRoute = new Map<string, number>();
+
+/**
+ * The agent's grants. A Switch from before service connections, or one with
+ * them off, answers the route with 404, which is the truth: it grants nothing.
*/
export async function readServiceGrants(
switchEndpoint: ServiceEndpoint,
@@ -129,6 +140,7 @@ export async function readServiceGrants(
const url =
switchEndpoint.endpoint.replace(/\/$/, '') +
`/agents/${encodeURIComponent(switchEndpoint.agentId)}/service-grants`;
+ if ((noGrantsRoute.get(url) ?? 0) > Date.now()) return [];
let response: Response;
try {
response = await fetchImpl(url, {
@@ -143,8 +155,10 @@ export async function readServiceGrants(
}
if (response.status === 404) {
await response.body?.cancel();
+ noGrantsRoute.set(url, Date.now() + NO_GRANTS_ROUTE_MS);
return [];
}
+ noGrantsRoute.delete(url);
if (!response.ok) {
await response.body?.cancel();
throw new Error(`Switch refused this agent's service grants (HTTP ${response.status}).`);