Skip to content

An open-source, faster Devin alternative

TypeScript

react: stop() rejects when the interrupt request fails, so a control can retry

Igor Zalutskidcb6f46ccd04
diff --git a/docs/agents/react.mdx b/docs/agents/react.mdx
index d699f8ac..3e98a583 100644
--- a/docs/agents/react.mdx
+++ b/docs/agents/react.mdx
@@ -237,9 +237,11 @@ for session status.
## Stop and reopen
-`stop()` requests an interrupt without spending a model turn. It returns
-`Promise<void>` and reports request failures through `error`; a resolved
-promise is **not** confirmation that execution has stopped. Watch the running
+`stop()` requests an interrupt without spending a model turn. It resolves
+once the request was accepted and rejects with a `SendError` when it was
+not, after setting `error`, so a control that showed a stopping state can be
+re-enabled and the request retried; a resolved promise is **not**
+confirmation that execution has stopped. Watch the running
turn settle in `turns`. Stop affects that turn, so the next queued turn can
start afterwards. The [interrupt contract](/agents/api#end-and-interrupt)
describes settlement and remote-command guarantees.
@@ -325,7 +327,7 @@ code.
| `messages` | User and assistant messages in log order |
| `turns` | Turns with status, input, messages, tool calls, result and failure |
| `send(text, options?)` | Submit a turn; resolve with a receipt or reject with `SendError` |
-| `stop()` | Request an interrupt; errors appear in `error`, settlement in `turns` |
+| `stop()` | Request an interrupt; rejects with `SendError` on a failed request, settlement arrives in `turns` |
| `sessionId` | Attached session, or the created one after the first send |
| `isRunning` | Running activity or this instance's unsettled admissions; not a session status |
| `isReplaying` | Attach mode: existing history is still loading |
diff --git a/react/src/index.ts b/react/src/index.ts
index e32e0bd3..f1abdb5a 100644
--- a/react/src/index.ts
+++ b/react/src/index.ts
@@ -140,7 +140,12 @@ export interface UseAgentResult {
* settles when the turn ends.
*/
send: (value: string, options?: SendOptions) => Promise<SendReceipt>;
- /** Requests an interrupt. Request failures set `error`; this promise does not confirm settlement. */
+ /**
+ * Requests an interrupt. Rejects with a `SendError` when the request was
+ * not answered with success, after setting `error`, so a control can be
+ * re-enabled and the request retried; a resolved promise does not confirm
+ * settlement, which arrives through `turns`.
+ */
stop: () => Promise<void>;
sessionId: string | undefined;
/** A turn is running in the log, or a locally admitted turn has not settled. Queued replay alone is false. */
@@ -618,7 +623,9 @@ export function useAgent(
{ method: "POST" },
);
} catch (cause) {
- setError(cause instanceof Error ? cause.message : String(cause));
+ const failure = asSendError(cause);
+ setError(failure.message);
+ throw failure;
}
}, [request]);
diff --git a/react/src/use-agent.test.ts b/react/src/use-agent.test.ts
index 3e208b93..64da0f96 100644
--- a/react/src/use-agent.test.ts
+++ b/react/src/use-agent.test.ts
@@ -311,6 +311,49 @@ test("attach sends turns and interrupts through the app's routes without duplica
await view.unmount();
});
+test("stop rejects when the interrupt request fails, sets error, and can be retried", async (t) => {
+ const session = fakeSession("ses-stop");
+ let interruptFails = true;
+ const fetchWithFailingInterrupt = async (input: string | URL | Request, init?: RequestInit) => {
+ const url = typeof input === "string" ? input : input instanceof URL ? input.href : input.url;
+ if (url.endsWith("/interrupt") && interruptFails) {
+ return Response.json(
+ { error: { code: "interrupt_unavailable", message: "The interrupt could not be delivered" } },
+ { status: 503 },
+ );
+ }
+ return session.fetch(input, init);
+ };
+ const view = mount(t, { sessionId: "ses-stop", basePath: "/app/agent", fetch: fetchWithFailingInterrupt, pollIntervalMs: 5 });
+ await view.render();
+ session.append({ turnId: "t1", type: "turn.started", data: {} });
+ await view.until((result) => result.isRunning, "running turn");
+
+ let rejection: unknown;
+ await act(async () => {
+ rejection = await view.result().stop().catch((cause: unknown) => cause);
+ });
+ assert.ok(rejection instanceof SendError, "a failed interrupt rejects");
+ assert.equal(rejection.status, 503);
+ assert.equal(rejection.code, "interrupt_unavailable");
+ assert.equal(view.result().error, "The interrupt could not be delivered");
+ assert.equal(view.result().isRunning, true, "work continues; the caller can retry");
+
+ interruptFails = false;
+ await act(async () => {
+ await view.result().stop();
+ });
+ assert.equal(
+ session.calls.filter((call) => call.method === "POST" && call.path === "/app/agent/sessions/ses-stop/interrupt").length,
+ 1,
+ "the retry reached the route",
+ );
+ session.append({ turnId: "t1", type: "turn.cancelled", data: { reason: "interrupted" } });
+ const stopped = await view.until((result) => !result.isRunning, "cancellation after the retry");
+ assert.equal(stopped.error, "The interrupt could not be delivered", "the earlier failure stays visible until a newer one");
+ await view.unmount();
+});
+
test("attach reports a failed turn and a rejected send", async (t) => {
const session = fakeSession("ses-4");
const view = mount(t, { sessionId: "ses-4", basePath: "/app/agent", fetch: session.fetch, pollIntervalMs: 5 });