{"slug":"rejected-prompt-restore","category":"errors","job":"recover","added":"2026-11-08T16:25Z","title":"Rejected Prompt Restore","description":"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.","url":"https://ai-patterns.app/patterns/errors/rejected-prompt-restore","markdown":"https://ai-patterns.app/patterns/errors/rejected-prompt-restore.md","json":"https://ai-patterns.app/api/patterns/errors/rejected-prompt-restore","seenIn":[{"team":"Windsurf","source":"https://docs.devin.ai/desktop/changelog"}],"spec":"# Rejected Prompt Restore\n\n## Summary\nWhen 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.\n\n## When to use\n- Agents check a request before starting (permissions, workspace scope, policy, missing connectors) and can refuse it outright.\n- Prompts are long and specific, so retyping one is a real cost.\n- The refusal has a reason the person can act on: remove a part, narrow the scope, grant access.\n\n## When not to use\n- The agent started and failed partway. That is a failed run, not a rejected prompt; use **Generation Error** or **Partial Response**.\n- The reply is generated but blocked afterwards by a filter. Show the blocked state on the reply; there's nothing to restore.\n- The problem is ambiguity, not refusal. Ask instead with **Clarifying Questions** so the work can start.\n- Later turns in a running session. Keep the conversation and answer in it; the prompt doesn't need restoring.\n\n## Anatomy\n- 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.\n- Composer: holds the restored text exactly as sent, cursor at the end, with a subtle highlight until it's edited.\n- Send again: the ordinary send button, renamed for assistive tech while a rejection is showing.\n\n## Behavior\n- 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.\n- Don't create a session, a title or a history entry for a prompt that never started.\n- The reason card stays while the person edits, and its hint changes once the text differs from what was rejected (\"Edited — send when ready.\").\n- 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.\n- Dismiss hides the card but keeps the text; the person decides whether to keep or clear it.\n- If several prompts are rejected in a row for the same reason, say what would work instead rather than repeating the same line.\n\n## Content guidelines\n- 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.\"\n- Give a way forward when there is one: \"Remove that part, or open ~/Downloads as its own project.\"\n- Don't apologise or moralise, and don't guess at intent. State the limit.\n- Keep the title neutral and consistent: \"The agent didn't start this task\".\n\n## Accessibility\n- The reason card is a `role=\"alert\"` region, so the rejection is announced when it appears.\n- The composer is linked to the reason with `aria-describedby` while it shows, so the reason is read when focus returns to the input.\n- Focus moves into the composer with the cursor at the end of the restored text; nothing is selected, so typing doesn't replace it.\n- Dismiss and Send again have text labels for screen readers. The card fades instead of sliding under `prefers-reduced-motion`.\n\n## Related patterns\n- **Generation Error** — Use it instead when the agent started and the run failed.\n- **Clarifying Questions** — Use it instead when the prompt is unclear rather than refused.\n- **Checkpoint Restore** — Use it for a later turn: rewinding the conversation also hands the prompt back for editing.\n- **Prompt Enhancer** — Use it as well to suggest a rewrite of the restored prompt that the agent will accept.\n","code":"\"use client\";\n\nimport * as React from \"react\";\nimport { AnimatePresence, motion, useReducedMotion } from \"motion/react\";\nimport { ArrowUp, CircleSlash, X } from \"lucide-react\";\n\nimport { cn } from \"@/lib/utils\";\n\nexport interface PromptRejection {\n  /** Why the agent didn't start, in one sentence (\"It can't edit files outside this project.\"). */\n  reason: string;\n  /** What would work instead, if the agent said (\"Ask about files in /web instead.\"). */\n  suggestion?: string;\n  /** The exact text that was rejected, so edits can be compared against it. */\n  prompt: string;\n}\n\nexport interface RejectedPromptComposerProps {\n  value: string;\n  onChange: (value: string) => void;\n  onSend: (value: string) => void;\n  /** Set when the agent rejected the last prompt; the composer already holds its text. */\n  rejection?: PromptRejection | null;\n  onDismissRejection: () => void;\n  /** The agent is deciding whether to start. */\n  sending?: boolean;\n  placeholder?: string;\n  className?: string;\n}\n\n/**\n * A composer that gets the person's words back when an agent rejects the\n * first prompt of a session. Nothing is left behind as a failed session:\n * the text returns to the input, the agent's reason sits right above it,\n * and editing and resending is the obvious next step.\n */\nexport function RejectedPromptComposer({\n  value,\n  onChange,\n  onSend,\n  rejection,\n  onDismissRejection,\n  sending = false,\n  placeholder = \"Describe a task…\",\n  className,\n}: RejectedPromptComposerProps) {\n  const reduce = useReducedMotion();\n  const inputRef = React.useRef<HTMLTextAreaElement>(null);\n  const reasonId = React.useId();\n  const edited = rejection ? value.trim() !== rejection.prompt.trim() : false;\n\n  // When a rejection lands, put the cursor back at the end of the restored text.\n  React.useEffect(() => {\n    if (!rejection) return;\n    const el = inputRef.current;\n    if (!el) return;\n    el.focus();\n    el.setSelectionRange(el.value.length, el.value.length);\n  }, [rejection]);\n\n  function send() {\n    if (!value.trim() || sending) return;\n    onSend(value);\n  }\n\n  return (\n    <div className={cn(\"flex flex-col gap-2\", className)}>\n      <AnimatePresence initial={false}>\n        {rejection && (\n          <motion.div\n            key=\"rejection\"\n            role=\"alert\"\n            id={reasonId}\n            initial={reduce ? { opacity: 0 } : { opacity: 0, y: 6 }}\n            animate={{ opacity: 1, y: 0 }}\n            exit={reduce ? { opacity: 0 } : { opacity: 0, y: 6 }}\n            transition={{ duration: 0.18, ease: \"easeOut\" }}\n            className=\"flex items-start gap-2.5 rounded-xl border bg-muted/40 px-3 py-2.5\"\n          >\n            <CircleSlash className=\"mt-0.5 size-4 shrink-0 text-muted-foreground\" aria-hidden />\n            <div className=\"flex min-w-0 flex-1 flex-col gap-0.5\">\n              <p className=\"text-xs font-medium\">The agent didn&apos;t start this task</p>\n              <p className=\"text-xs text-muted-foreground\">{rejection.reason}</p>\n              {rejection.suggestion && <p className=\"text-xs text-muted-foreground\">{rejection.suggestion}</p>}\n              <p className=\"mt-1 text-[11px] text-muted-foreground\">\n                {edited ? \"Edited — send when ready.\" : \"Your prompt is back in the box below. Edit it and send again.\"}\n              </p>\n            </div>\n            <button\n              type=\"button\"\n              onClick={onDismissRejection}\n              className=\"rounded-md p-0.5 text-muted-foreground transition-colors hover:bg-accent hover:text-foreground\"\n            >\n              <X className=\"size-3.5\" aria-hidden />\n              <span className=\"sr-only\">Dismiss</span>\n            </button>\n          </motion.div>\n        )}\n      </AnimatePresence>\n\n      <div\n        className={cn(\n          \"flex items-end gap-2 rounded-2xl bg-card px-3 py-2 transition-colors border\",\n          rejection && !edited && \"border-foreground/30\"\n        )}\n      >\n        <textarea\n          ref={inputRef}\n          value={value}\n          rows={2}\n          onChange={(e) => onChange(e.target.value)}\n          onKeyDown={(e) => {\n            if (e.key === \"Enter\" && !e.shiftKey) {\n              e.preventDefault();\n              send();\n            }\n          }}\n          placeholder={placeholder}\n          aria-label=\"Task for the agent\"\n          aria-describedby={rejection ? reasonId : undefined}\n          className=\"min-w-0 flex-1 resize-none bg-transparent py-1 text-sm outline-none placeholder:text-muted-foreground\"\n        />\n        <button\n          type=\"button\"\n          onClick={send}\n          disabled={!value.trim() || sending}\n          className=\"flex size-8 shrink-0 items-center justify-center rounded-full bg-foreground text-background transition-opacity disabled:opacity-40\"\n        >\n          <ArrowUp className=\"size-4\" aria-hidden />\n          <span className=\"sr-only\">{rejection ? \"Send again\" : \"Send\"}</span>\n        </button>\n      </div>\n    </div>\n  );\n}\n","dependencies":["lucide-react","motion"],"localImports":["@/lib/utils"]}