Skip to content

An open-source, faster Devin alternative

TypeScript

fix(slack): handle raced managed authorization

Igor Zalutski3ee6a6574c3c
diff --git a/sdks/typescript/src/agents/agents.test.ts b/sdks/typescript/src/agents/agents.test.ts
index 9263d9af..9331d1b5 100644
--- a/sdks/typescript/src/agents/agents.test.ts
+++ b/sdks/typescript/src/agents/agents.test.ts
@@ -22,7 +22,10 @@ describe("Agents managed Slack", () => {
returnDeploymentId: "dep_1",
});
- expect(result.authorizeUrl).toContain("slack.com/oauth/v2/authorize");
+ expect("authorizeUrl" in result).toBe(true);
+ expect("authorizeUrl" in result ? result.authorizeUrl : undefined).toContain(
+ "slack.com/oauth/v2/authorize",
+ );
expect(fetcher).toHaveBeenCalledWith(
"https://api.example.test/v3/agents/agt_1/slack/managed/authorize",
expect.objectContaining({
@@ -60,6 +63,32 @@ describe("Agents managed Slack", () => {
expect(JSON.stringify(result)).not.toContain("xoxb-");
});
+ it("returns an already-active connection from a raced authorize", async () => {
+ const fetcher = vi.fn(async () =>
+ Response.json({
+ mode: "managed",
+ status: "active",
+ workspace: { id: "T1", name: "Acme" },
+ app: { id: "A1", handle: "OpenComputer" },
+ open_url: "https://slack.com/app_redirect?app=A1&team=T1",
+ connected_at: "2026-07-16T17:00:00Z",
+ }),
+ );
+ const oc = new OpenComputer({
+ apiKey: "test-key",
+ baseUrl: "https://api.example.test/v3",
+ fetch: fetcher as typeof fetch,
+ maxRetries: 0,
+ });
+
+ const result = await oc.agents.authorizeManagedSlack("agt_1");
+
+ expect(result).toMatchObject({ status: "active" });
+ expect("openUrl" in result ? result.openUrl : undefined).toContain(
+ "slack.com/app_redirect",
+ );
+ });
+
it("disconnects the managed connection without uninstalling the app", async () => {
const fetcher = vi.fn(async () =>
Response.json({ ok: true, status: "disconnected" }),
diff --git a/sdks/typescript/src/agents/agents.ts b/sdks/typescript/src/agents/agents.ts
index 700df288..2f7cd7fc 100644
--- a/sdks/typescript/src/agents/agents.ts
+++ b/sdks/typescript/src/agents/agents.ts
@@ -142,7 +142,7 @@ export class Agents {
authorizeManagedSlack(
id: string,
opts: { returnDeploymentId?: string } = {},
- ): Promise<ManagedSlackAuthorization> {
+ ): Promise<ManagedSlackAuthorization | ManagedSlackConnection> {
return this.http.request("POST", `/agents/${id}/slack/managed/authorize`, {
body: opts,
});
diff --git a/web/src/api/schemas.ts b/web/src/api/schemas.ts
index b31180ff..271bae6b 100644
--- a/web/src/api/schemas.ts
+++ b/web/src/api/schemas.ts
@@ -709,13 +709,6 @@ export const SlackManifestResponseSchema = z.object({
status: z.string(),
})
-export const ManagedSlackAuthorizeResponseSchema = z.object({
- mode: z.literal('managed'),
- status: z.literal('pending'),
- authorize_url: z.string(),
- expires_at: z.string(),
-})
-
export const ManagedSlackConnectionSchema = z.object({
mode: z.literal('managed'),
status: z.enum(['active', 'disconnected', 'error', 'revoked']),
@@ -726,6 +719,19 @@ export const ManagedSlackConnectionSchema = z.object({
error_code: z.string().nullish(),
})
+// A race-safe authorize is idempotent: if the connection became active after
+// the dashboard status read, the API returns that active connection instead of
+// minting another OAuth intent.
+export const ManagedSlackAuthorizeResponseSchema = z.union([
+ z.object({
+ mode: z.literal('managed'),
+ status: z.literal('pending'),
+ authorize_url: z.string(),
+ expires_at: z.string(),
+ }),
+ ManagedSlackConnectionSchema.refine((value) => value.status === 'active'),
+])
+
export const ManagedSlackDisconnectResponseSchema = z.object({
ok: z.literal(true),
status: z.literal('disconnected'),
diff --git a/web/src/api/slack-schemas.test.ts b/web/src/api/slack-schemas.test.ts
index 286692cf..8fd4dcc4 100644
--- a/web/src/api/slack-schemas.test.ts
+++ b/web/src/api/slack-schemas.test.ts
@@ -38,4 +38,17 @@ describe('managed Slack response schemas', () => {
}),
).toThrow()
})
+
+ it('accepts an idempotent active response from authorize', () => {
+ expect(
+ ManagedSlackAuthorizeResponseSchema.parse({
+ mode: 'managed',
+ status: 'active',
+ workspace: { id: 'T1', name: 'Acme' },
+ app: { id: 'A1', handle: 'OpenComputer' },
+ open_url: 'https://slack.com/app_redirect?app=A1&team=T1',
+ connected_at: '2026-07-16T18:10:00Z',
+ }),
+ ).toMatchObject({ mode: 'managed', status: 'active' })
+ })
})
diff --git a/web/src/components/slack-connect.tsx b/web/src/components/slack-connect.tsx
index 16580e2f..ec9e070f 100644
--- a/web/src/components/slack-connect.tsx
+++ b/web/src/components/slack-connect.tsx
@@ -185,8 +185,12 @@ export function SlackConnect({
const authorizeManagedMutation = useMutation({
mutationFn: () => authorizeManagedSlack(agentId),
- onSuccess: ({ authorize_url }) => {
- window.location.assign(authorize_url)
+ onSuccess: (result) => {
+ if ('authorize_url' in result) {
+ window.location.assign(result.authorize_url)
+ return
+ }
+ void invalidateManaged()
},
onError: (error) =>
notifyError("Couldn't start the Slack connection.", error),