# Rejected Prompt Restore

> When an agent declines the first prompt of a session, the text goes back into the composer with the agent's reason above it, ready to edit and resend.

- Category: errors
- Job: Recover — Explain failures and limits, keep partial work, and offer a way forward.
- Slug: `rejected-prompt-restore`
- HTML: https://ai-patterns.app/patterns/errors/rejected-prompt-restore
- JSON: https://ai-patterns.app/api/patterns/errors/rejected-prompt-restore
- Seen in: [Windsurf](https://docs.devin.ai/desktop/changelog)

## Summary
When an agent declines the first prompt of a session — out of scope, not allowed, missing access — the person gets their words back. No empty or failed session is left behind: the prompt returns to the composer, the agent's reason sits right above it, and editing and resending is the obvious next step.

## When to use
- Agents check a request before starting (permissions, workspace scope, policy, missing connectors) and can refuse it outright.
- Prompts are long and specific, so retyping one is a real cost.
- The refusal has a reason the person can act on: remove a part, narrow the scope, grant access.

## When not to use
- The agent started and failed partway. That is a failed run, not a rejected prompt; use **Generation Error** or **Partial Response**.
- The reply is generated but blocked afterwards by a filter. Show the blocked state on the reply; there's nothing to restore.
- The problem is ambiguity, not refusal. Ask instead with **Clarifying Questions** so the work can start.
- Later turns in a running session. Keep the conversation and answer in it; the prompt doesn't need restoring.

## Anatomy
- Reason card: above the composer, a short title ("The agent didn't start this task"), the reason in one sentence, an optional suggestion, a hint that the prompt is back below, and Dismiss.
- Composer: holds the restored text exactly as sent, cursor at the end, with a subtle highlight until it's edited.
- Send again: the ordinary send button, renamed for assistive tech while a rejection is showing.

## Behavior
- On rejection, put the exact text back in the composer and move focus there, cursor at the end. Attachments and chips sent with it come back too.
- Don't create a session, a title or a history entry for a prompt that never started.
- The reason card stays while the person edits, and its hint changes once the text differs from what was rejected ("Edited — send when ready.").
- Sending again runs the normal check. If it passes, the card goes away and the session starts; if it fails again, the card updates with the new reason.
- Dismiss hides the card but keeps the text; the person decides whether to keep or clear it.
- If several prompts are rejected in a row for the same reason, say what would work instead rather than repeating the same line.

## Content guidelines
- The reason names the rule and the thing: "It can only change files inside this project, and ~/Downloads is outside it." Not "Request not permitted."
- Give a way forward when there is one: "Remove that part, or open ~/Downloads as its own project."
- Don't apologise or moralise, and don't guess at intent. State the limit.
- Keep the title neutral and consistent: "The agent didn't start this task".

## Accessibility
- The reason card is a `role="alert"` region, so the rejection is announced when it appears.
- The composer is linked to the reason with `aria-describedby` while it shows, so the reason is read when focus returns to the input.
- Focus moves into the composer with the cursor at the end of the restored text; nothing is selected, so typing doesn't replace it.
- Dismiss and Send again have text labels for screen readers. The card fades instead of sliding under `prefers-reduced-motion`.

## Related patterns
- **Generation Error** — Use it instead when the agent started and the run failed.
- **Clarifying Questions** — Use it instead when the prompt is unclear rather than refused.
- **Checkpoint Restore** — Use it for a later turn: rewinding the conversation also hands the prompt back for editing.
- **Prompt Enhancer** — Use it as well to suggest a rewrite of the restored prompt that the agent will accept.

## Implementation

Reference implementation (React, TypeScript, Tailwind v4, Motion, Radix). Keep every rule from the spec above; if the host project has its own design system, re-express the markup and motion in it rather than copying these classes.

npm dependencies: `lucide-react`, `motion`

Local imports to provide: `@/lib/utils` (`cn` is the usual `clsx` + `tailwind-merge` helper).

```tsx title="component.tsx"
"use client";

import * as React from "react";
import { AnimatePresence, motion, useReducedMotion } from "motion/react";
import { ArrowUp, CircleSlash, X } from "lucide-react";

import { cn } from "@/lib/utils";

export interface PromptRejection {
  /** Why the agent didn't start, in one sentence ("It can't edit files outside this project."). */
  reason: string;
  /** What would work instead, if the agent said ("Ask about files in /web instead."). */
  suggestion?: string;
  /** The exact text that was rejected, so edits can be compared against it. */
  prompt: string;
}

export interface RejectedPromptComposerProps {
  value: string;
  onChange: (value: string) => void;
  onSend: (value: string) => void;
  /** Set when the agent rejected the last prompt; the composer already holds its text. */
  rejection?: PromptRejection | null;
  onDismissRejection: () => void;
  /** The agent is deciding whether to start. */
  sending?: boolean;
  placeholder?: string;
  className?: string;
}

/**
 * A composer that gets the person's words back when an agent rejects the
 * first prompt of a session. Nothing is left behind as a failed session:
 * the text returns to the input, the agent's reason sits right above it,
 * and editing and resending is the obvious next step.
 */
export function RejectedPromptComposer({
  value,
  onChange,
  onSend,
  rejection,
  onDismissRejection,
  sending = false,
  placeholder = "Describe a task…",
  className,
}: RejectedPromptComposerProps) {
  const reduce = useReducedMotion();
  const inputRef = React.useRef<HTMLTextAreaElement>(null);
  const reasonId = React.useId();
  const edited = rejection ? value.trim() !== rejection.prompt.trim() : false;

  // When a rejection lands, put the cursor back at the end of the restored text.
  React.useEffect(() => {
    if (!rejection) return;
    const el = inputRef.current;
    if (!el) return;
    el.focus();
    el.setSelectionRange(el.value.length, el.value.length);
  }, [rejection]);

  function send() {
    if (!value.trim() || sending) return;
    onSend(value);
  }

  return (
    <div className={cn("flex flex-col gap-2", className)}>
      <AnimatePresence initial={false}>
        {rejection && (
          <motion.div
            key="rejection"
            role="alert"
            id={reasonId}
            initial={reduce ? { opacity: 0 } : { opacity: 0, y: 6 }}
            animate={{ opacity: 1, y: 0 }}
            exit={reduce ? { opacity: 0 } : { opacity: 0, y: 6 }}
            transition={{ duration: 0.18, ease: "easeOut" }}
            className="flex items-start gap-2.5 rounded-xl border bg-muted/40 px-3 py-2.5"
          >
            <CircleSlash className="mt-0.5 size-4 shrink-0 text-muted-foreground" aria-hidden />
            <div className="flex min-w-0 flex-1 flex-col gap-0.5">
              <p className="text-xs font-medium">The agent didn&apos;t start this task</p>
              <p className="text-xs text-muted-foreground">{rejection.reason}</p>
              {rejection.suggestion && <p className="text-xs text-muted-foreground">{rejection.suggestion}</p>}
              <p className="mt-1 text-[11px] text-muted-foreground">
                {edited ? "Edited — send when ready." : "Your prompt is back in the box below. Edit it and send again."}
              </p>
            </div>
            <button
              type="button"
              onClick={onDismissRejection}
              className="rounded-md p-0.5 text-muted-foreground transition-colors hover:bg-accent hover:text-foreground"
            >
              <X className="size-3.5" aria-hidden />
              <span className="sr-only">Dismiss</span>
            </button>
          </motion.div>
        )}
      </AnimatePresence>

      <div
        className={cn(
          "flex items-end gap-2 rounded-2xl bg-card px-3 py-2 transition-colors border",
          rejection && !edited && "border-foreground/30"
        )}
      >
        <textarea
          ref={inputRef}
          value={value}
          rows={2}
          onChange={(e) => onChange(e.target.value)}
          onKeyDown={(e) => {
            if (e.key === "Enter" && !e.shiftKey) {
              e.preventDefault();
              send();
            }
          }}
          placeholder={placeholder}
          aria-label="Task for the agent"
          aria-describedby={rejection ? reasonId : undefined}
          className="min-w-0 flex-1 resize-none bg-transparent py-1 text-sm outline-none placeholder:text-muted-foreground"
        />
        <button
          type="button"
          onClick={send}
          disabled={!value.trim() || sending}
          className="flex size-8 shrink-0 items-center justify-center rounded-full bg-foreground text-background transition-opacity disabled:opacity-40"
        >
          <ArrowUp className="size-4" aria-hidden />
          <span className="sr-only">{rejection ? "Send again" : "Send"}</span>
        </button>
      </div>
    </div>
  );
}
```
