# User input requests

|  | Live Workflow question | Declared Agent question |
| --- | --- | --- |
| Caller | `ask_user(question)`, `runtime.ask(...)` | Agent tool interaction with a declared pre-wait |
| While waiting | Same process, same call stack | Execution paused, attempt ended |
| Saved | Question, policy, answer. No checkpoint. | Checkpoint |
| After the answer | The function returns from the call. No new attempt, no model or finished tool replayed. | Continues from the checkpoint |
| Browser refresh | Question drawn again while its owner is active | Question drawn again |
| Worker restart | Execution interrupted, stack not restored, nothing replayed | Stays available |
| Tool permissions | Cannot grant them | Permission approvals unchanged |

A preapproved wait takes precedence over the live binding and must match the requested presentation exactly.

## API and outcomes

```
from openprogram.programs.tools.interaction.ask_user import ask_user
answer = ask_user("Who wrote this report?")

runtime.ask(prompt=None, *, options=None, multi=False, allow_custom=True,
            questions=None, timeout=300.0, default=None)
```

| Outcome | `runtime.ask` | `ask_user` |
| --- | --- | --- |
| Answered | The answer | The answer |
| Declined | `UserDeclined` | `None` |
| Timed out | `default` if given, else `AskTimeout` | `None` |
| Execution cancelled | Execution cancellation exception | Propagates |
| Runtime, storage or transport error | Propagates | Propagates |
| No handler, no usable runtime |  | `None` |

`runtime.confirm` and `runtime.form` share the question transport.

**How `ask_user` finds an answer**

1. A handler set by `run_with_follow_up`, scoped to the execution context and inherited by its background thread, or the global CLI handler. The scoped handler neither replaces the frontend handler nor captures another concurrent Workflow's question.
2. `runtime.ask`, when the runtime has a question transport or a connected session.
3. Terminal input, on a TTY.
4. `None`.

Each `FollowUp` accepts one answer; a second fails at once.

## Live Workflow ownership

The subprocess runner binds execution ID, attempt ID, generation and producer identity for the call. A runtime without this binding cannot create a live question.

Creating and answering both verify, in one transaction, that the execution is running, the attempt is the current active owner and its lease is valid.

- **Publish**: The child sends `question.asked` through `QueueTransport`; the parent builds the frontend event from the stored request and relays the canonical answer notification
- **Answer authority**: The canonical wait store. The registry only wakes a local waiter.
- **Rejected**: Duplicate answers and replies from another execution, by the command protocol

## Browser and process lifecycle

| Event | Result |
| --- | --- |
| Session load | Open questions replayed as the same frame the question card already handles |
| Answer | Question endpoints and the execution answer command use one record and one authorization check |
| Execution stopped | Its open questions cancelled |
| Child exits | Only that producer's live questions closed |
| Owner lost | Abandoned live questions cancelled; a stale reply cannot continue an ended call |

**Reference designs**

| System | Taken from it |
| --- | --- |
| opencode | Tool calls `ctx.ask(...)`: server Deferred and pending map, `permission.asked` down, REST reply up, a list endpoint so a reconnecting client recovers pending questions. A rejection may carry text the model sees as the tool error. |
| Claude Code | AskUserQuestion rides the permission pipeline; options plus an always-present “Other”; a pending request snapshot in session metadata so remote UIs can redraw it, execution stack never saved; interactive tools disabled with no human attached. |
| openclaw | 30-minute timeout with an explicit fallback; channel buttons whose value is a plain text command (`/approve <id> …`), so text-only channels work the same; for channel-started runs the tool returns “pending” and the result is injected later. |
| MCP elicitation | Three outcomes (accept, decline, cancel) and flat-object schema limits for form-style asks. |

**Verification**

Tests use public `ask_user` with the real Runtime and canonical wait commands: same-call continuation, cancellation, invalid owner or session, transport failure, producer cleanup. Native acceptance needs a question in the installed App, a browser refresh, and an answer that lets the Workflow finish. A returned `WAITING_USER` object does not show the UI worked.
