Skip to content

An open-source, faster Devin alternative

TypeScript

Slack: a connection that changed under the manual wizard

Igor Zalutskif6a20ebdbe69
diff --git a/cloudflare-workers/api-edge/src/managed_agents.test.ts b/cloudflare-workers/api-edge/src/managed_agents.test.ts
index 2466ab08..78059cf9 100644
--- a/cloudflare-workers/api-edge/src/managed_agents.test.ts
+++ b/cloudflare-workers/api-edge/src/managed_agents.test.ts
@@ -1579,6 +1579,7 @@ describe("managed agents proxy", () => {
"slack_setup_busy",
"slack_setup_cancelled",
"slack_manual_completion_blocked",
+ "slack_connection_changed",
];
const GENERIC_MESSAGES = [
"The agent request could not be completed.",
@@ -1902,6 +1903,46 @@ describe("managed agents proxy", () => {
});
});
+ it("curates a connection that changed under the manual create route", async () => {
+ vi.stubGlobal(
+ "fetch",
+ vi.fn(async () =>
+ Response.json(
+ {
+ error: {
+ code: "slack_connection_changed",
+ message: "backend text with generation 7",
+ },
+ },
+ { status: 409 },
+ ),
+ ),
+ );
+
+ const response = await proxyManagedAgents(
+ new Request(
+ "https://app.opencomputer.dev/api/managed-agents/channels/slack/connections",
+ {
+ method: "POST",
+ headers: { "content-type": "application/json" },
+ body: JSON.stringify({ agentId: "coder@development", name: "Patch" }),
+ },
+ ),
+ env,
+ caller,
+ "/api/managed-agents",
+ );
+
+ expect(response.status).toBe(409);
+ expect(await response.json()).toEqual({
+ error: {
+ code: "slack_connection_changed",
+ message:
+ "This connection changed while the request was in flight. Reload the page to see its current state before trying again.",
+ },
+ });
+ });
+
it("curates a blocked manual completion on the connection route", async () => {
vi.stubGlobal(
"fetch",
diff --git a/cloudflare-workers/api-edge/src/managed_agents.ts b/cloudflare-workers/api-edge/src/managed_agents.ts
index 50331ecc..55e406bc 100644
--- a/cloudflare-workers/api-edge/src/managed_agents.ts
+++ b/cloudflare-workers/api-edge/src/managed_agents.ts
@@ -163,6 +163,8 @@ const SLACK_SETUP_ERROR_MESSAGES: Record<string, string> = {
"This Slack setup was cancelled. The Slack app may still appear in your workspace's app list and can be removed there.",
slack_manual_completion_blocked:
"Manual completion is blocked: cancel the automated setup for this connection, then generate a new manifest (Reconnect) before entering credentials.",
+ slack_connection_changed:
+ "This connection changed while the request was in flight. Reload the page to see its current state before trying again.",
slack_authorization_denied:
"The Slack installation was declined. Authorize the app again when you are ready.",
slack_authorization_expired:
diff --git a/docs/agents/api.mdx b/docs/agents/api.mdx
index 5bfdf2b7..d4e952a0 100644
--- a/docs/agents/api.mdx
+++ b/docs/agents/api.mdx
@@ -549,6 +549,7 @@ Error codes, as HTTP errors or as `error.code` on the setup:
| `slack_setup_busy` | `409` on cancel: the setup is creating or exchanging; wait for it to finish |
| `slack_setup_cancelled` | Recorded on a setup cancelled after the app was created or its creation was uncertain: the Slack app may still appear in the workspace's app list and can be removed there |
| `slack_manual_completion_blocked` | `409` on the manual credential save (`PUT /channels/slack/connections/<id>`): an automated setup owns the connection, or the pasted manifest is no longer current; cancel the setup, generate a new manifest, then enter credentials |
+| `slack_connection_changed` | `409` on the manual create or reconnect (`POST /channels/slack/connections`): the connection changed while the request was in flight, for example the automated setup connected it; reload before trying again |
| `slack_configuration_token_invalid`, `slack_configuration_token_expired` | The token was rejected; generate a new one and submit it with the same key |
| `slack_manifest_rejected` | Slack rejected the generated app configuration; `pointer` names the field. Not recoverable in place: fix the declaration, redeploy, cancel the setup and start again |
| `slack_app_limit_reached` | The workspace cannot hold another app |
diff --git a/web/src/managed-agents/SlackWizard.test.tsx b/web/src/managed-agents/SlackWizard.test.tsx
index a8d45424..71a22ed8 100644
--- a/web/src/managed-agents/SlackWizard.test.tsx
+++ b/web/src/managed-agents/SlackWizard.test.tsx
@@ -22,6 +22,8 @@ const { ManagedSlackWizard } = await import('./SlackWizard')
const BLOCKED =
'Manual completion is blocked: cancel the automated setup for this connection, then generate a new manifest (Reconnect) before entering credentials.'
+const CHANGED =
+ 'This connection changed while the request was in flight. Reload the page to see its current state before trying again.'
// A connection whose events are rejected: the wizard offers Edit credentials,
// which starts at the details step without a manifest round trip.
@@ -99,7 +101,11 @@ describe('ManagedSlackWizard blocked manual completion', () => {
document.body.replaceChildren()
})
- function render(connectionsHref?: string) {
+ // `null` renders the wizard with no connection at all.
+ function render(
+ connectionsHref?: string,
+ current: ManagedAgentChannel | null = connection,
+ ) {
act(() => {
root.render(
<QueryClientProvider client={client}>
@@ -108,7 +114,7 @@ describe('ManagedSlackWizard blocked manual completion', () => {
agentId="coder"
alias="development"
agentName="Coder"
- connection={connection}
+ connection={current ?? undefined}
connectionsHref={connectionsHref}
/>
</MemoryRouter>
@@ -191,4 +197,33 @@ describe('ManagedSlackWizard blocked manual completion', () => {
),
).toBe(false)
})
+
+ it('shows a connection that changed under the manual create and refreshes what the page knows', async () => {
+ api.startManagedAgentSlack.mockReset()
+ api.startManagedAgentSlack.mockRejectedValue(
+ new ApiError(CHANGED, 409, 'slack_connection_changed'),
+ )
+ const invalidate = vi.spyOn(client, 'invalidateQueries')
+ render(undefined, null)
+
+ act(() => button(container, 'Connect Slack').click())
+ await settle(
+ () => document.body.querySelector('#managed-slack-app-name') !== null,
+ 'the create step',
+ )
+ act(() => button(document.body, 'Generate Slack manifest').click())
+ await settle(
+ () => document.body.querySelector('[role="alert"]') !== null,
+ 'the changed state',
+ )
+ expect(api.startManagedAgentSlack).toHaveBeenCalledTimes(1)
+ const alert = document.body.querySelector('[role="alert"]') as HTMLElement
+ expect(alert.textContent).toContain(CHANGED)
+ expect(invalidate).toHaveBeenCalledWith({
+ queryKey: ['managed-agent-channels'],
+ })
+ expect(invalidate).toHaveBeenCalledWith({
+ queryKey: ['managed-slack-setup'],
+ })
+ })
})
diff --git a/web/src/managed-agents/SlackWizard.tsx b/web/src/managed-agents/SlackWizard.tsx
index f9fbbdad..0daf93ea 100644
--- a/web/src/managed-agents/SlackWizard.tsx
+++ b/web/src/managed-agents/SlackWizard.tsx
@@ -100,6 +100,31 @@ function SlackDestinationSetup({
)
}
+/** A platform refusal shown where the person is, with its way forward. */
+function WizardNotice({
+ message,
+ children,
+}: {
+ message: string
+ children?: ReactNode
+}) {
+ return (
+ <div
+ role="alert"
+ className="bg-status-error-bg/30 space-y-2 rounded-md px-3 py-2"
+ >
+ <div className="flex items-start gap-2">
+ <TriangleAlert
+ className="text-status-error mt-0.5 size-4 shrink-0"
+ aria-hidden
+ />
+ <p className="text-sm">{message}</p>
+ </div>
+ {children}
+ </div>
+ )
+}
+
function WizardSteps({ current, steps }: { current: number; steps: string[] }) {
return (
<ol className="flex items-center gap-2 pt-2">
@@ -181,8 +206,23 @@ export function ManagedSlackWizard({
// The platform's reason a manual save is refused while an automated setup
// owns this connection or the pasted manifest is stale.
const [blocked, setBlocked] = useState<string>()
+ // The connection changed under this request (the automated flow may have
+ // connected it): what the page knows is stale and is re-read.
+ const [changed, setChanged] = useState<string>()
const setupAnchor = slackSetupAnchorId({ agentId, alias, channelId })
const setupOnPage = Boolean(blocked && document.getElementById(setupAnchor))
+ const connectionChanged = (error: unknown) => {
+ if (
+ !(error instanceof ApiError) ||
+ error.type !== 'slack_connection_changed'
+ ) {
+ return false
+ }
+ setChanged(error.message)
+ void queryClient.invalidateQueries({ queryKey: ['managed-agent-channels'] })
+ void queryClient.invalidateQueries({ queryKey: ['managed-slack-setup'] })
+ return true
+ }
const invalidate = () =>
queryClient.invalidateQueries({ queryKey: ['managed-agent-channels'] })
@@ -199,7 +239,10 @@ export function ManagedSlackWizard({
setManifest(result)
void invalidate()
},
- onError: (error) => notifyError("Couldn't prepare the Slack app.", error),
+ onError: (error) => {
+ if (connectionChanged(error)) return
+ notifyError("Couldn't prepare the Slack app.", error)
+ },
})
const complete = useMutation({
mutationFn: () => {
@@ -219,6 +262,7 @@ export function ManagedSlackWizard({
void invalidate()
},
onError: (error) => {
+ if (connectionChanged(error)) return
if (
error instanceof ApiError &&
error.type === 'slack_manual_completion_blocked'
@@ -240,6 +284,7 @@ export function ManagedSlackWizard({
const reset = () => {
setBlocked(undefined)
+ setChanged(undefined)
setStep('create')
setEditing(false)
setManifest(undefined)
@@ -465,6 +510,7 @@ export function ManagedSlackWizard({
</pre>
</>
)}
+ {changed ? <WizardNotice message={changed} /> : null}
<DialogFooter>
<Button variant="ghost" onClick={() => setOpen(false)}>
Cancel
@@ -542,18 +588,9 @@ export function ManagedSlackWizard({
placeholder="xoxb-…"
/>
</Field>
+ {changed ? <WizardNotice message={changed} /> : null}
{blocked ? (
- <div
- role="alert"
- className="bg-status-error-bg/30 space-y-2 rounded-md px-3 py-2"
- >
- <div className="flex items-start gap-2">
- <TriangleAlert
- className="text-status-error mt-0.5 size-4 shrink-0"
- aria-hidden
- />
- <p className="text-sm">{blocked}</p>
- </div>
+ <WizardNotice message={blocked}>
{setupOnPage ? (
<Button
type="button"
@@ -568,7 +605,7 @@ export function ManagedSlackWizard({
<Link to={connectionsHref}>Open Connections</Link>
</Button>
) : null}
- </div>
+ </WizardNotice>
) : null}
<DialogFooter>
<Button