|
| 1 | +# Ask a person - the main flow |
| 2 | + |
| 3 | +An agent loop can wait for a person: a tool call marked `write: true` waits under the loop's default mode, and the |
| 4 | +**ask row** of the loop spec's section 7.3 lets the model put a question to a person and carry on in the same |
| 5 | +conversation with the answer. The graph suspends durably on its state store and resumes at most once. What was missing |
| 6 | +is the person: nothing carried a waiting call to anyone, and nothing brought a decision back except a request a |
| 7 | +developer typed. This extension is the **main flow** of RFC-0014 (mercury-composable, |
| 8 | +`draft-design-specs/ask-a-person-channel.md`): it carries the wait out and brings the decision in, knows no channel, |
| 9 | +and keeps no store - the engine's at-most-once resume stays the authority. |
| 10 | + |
| 11 | +- **`person.ask`** is a composable function the graph's hold path calls before it suspends. The message names the |
| 12 | + graph (`graph_id`, `description`, `node`), the transaction (`correlation_id`, `turn`), the waiting `calls` exactly as |
| 13 | + the loop returned them, and the suspension's `ttl`. The function issues a signed, stateless **ticket**, builds the |
| 14 | + **resume link** and sends the whole message as one event to every route in `ask.channels` - the agent-question app's |
| 15 | + intake, a chat poster, a mail sender, a topic publisher - knowing nothing of what a route does with it. A channel |
| 16 | + that fails is logged by type and never fails the hold. A third kind, **`advice`**, is a notification with no decision |
| 17 | + (a status update of a long run, an acknowledgement after a response): no ticket, no link. |
| 18 | +- **The resume link**, `<ask.reply.url>/ask/<ticket>`, opens the wait on this process's reply page. A `GET` shows the |
| 19 | + current wait, read from the engine by the reference graph's re-advice (a request without decisions), and never |
| 20 | + resumes - link unfurlers and mail scanners click links. Only a submit resumes: **`POST /ask/reply`** with |
| 21 | + `{ticket, decisions}` - a person's browser, or a channel app on the person's behalf under its own bearer with |
| 22 | + `decided_by` - verifies the ticket, maps the graph id to the engine's entry through the `ask.graphs` allowlist (never |
| 23 | + to an address a message told it), prefixes who decided and when to each tool call's note, sends the resume (the same |
| 24 | + entry, the correlation id in `x-correlation-id`, `{turn, decisions}`) and returns the engine's outcome to the invoker. |
| 25 | + |
| 26 | +The Node.js twin (mercury-nodejs `extensions/ask-person`) serves the same route and the same page, pinned by the |
| 27 | +same vector file. |
| 28 | + |
| 29 | +## Run |
| 30 | + |
| 31 | +```bash |
| 32 | +pip install mercury-composable # the open and the proxy modes add no dependency |
| 33 | +pip install 'mercury-composable[ask]' # + PyJWT for the oauth2 mode of the reply page |
| 34 | +export ASK_TICKET_KEY='a long random secret' |
| 35 | +mercury-serve extensions/ask-person/ask_person.py -Dask.ticket.key='${ASK_TICKET_KEY}' |
| 36 | +python extensions/ask-person/ask_person.py # the same without mercury-serve |
| 37 | +python extensions/ask-person/ask_person.py inspect v1.eyJ... # read a ticket (a check, never a resume) |
| 38 | +``` |
| 39 | + |
| 40 | +The extension reads `resources/application.yml` next to `ask_person.py`, whichever directory the command runs from; |
| 41 | +`-Dkey=value` overrides any key, and a value may carry `${ENV_VAR:default}` references. It listens on port 8091 by |
| 42 | +default (`rest.server.port`). The boot stops the application by name when the ticket key is not set, the reply URL is |
| 43 | +not an http or https base, the auth mode is unknown, or `oauth2` is chosen without PyJWT or without an issuer or a JWKS |
| 44 | +URL; it warns when no channel or no graph is configured. |
| 45 | + |
| 46 | +## The message |
| 47 | + |
| 48 | +```json |
| 49 | +{ |
| 50 | + "kind": "approval", |
| 51 | + "graph_id": "order-assistant", |
| 52 | + "description": "Customer-support assistant: a refund waits for a supervisor", |
| 53 | + "correlation_id": "order-1002-case", |
| 54 | + "turn": 2, |
| 55 | + "node": "assist", |
| 56 | + "summary": "issue_refund for ORD-1002, 174.00 USD: 'Shipment lost in transit'", |
| 57 | + "calls": [{"id": "toolu_01...", "name": "issue_refund", "route": "demo.refund.issue", "wait": true, |
| 58 | + "input": {"order_id": "ORD-1002", "amount": 174.0, "reason": "Shipment lost in transit"}}], |
| 59 | + "ttl": "2d", |
| 60 | + "requested_at": "2026-11-19T09:12:04.118Z" |
| 61 | +} |
| 62 | +``` |
| 63 | + |
| 64 | +| Field | A wait (`approval`, `question`) | An `advice` | |
| 65 | +| --- | --- | --- | |
| 66 | +| `kind` | `approval` (a call needs `approved`) or `question` (an ask row) | `advice` | |
| 67 | +| `graph_id`, `description`, `correlation_id`, `summary` | required text | required text | |
| 68 | +| `node` | optional: the loop node that waits | optional | |
| 69 | +| `turn` | required, a whole number: the turn that waits | optional: the turn it follows | |
| 70 | +| `calls` | required: the approval block as the loop returned it | refused | |
| 71 | +| `ttl` | required: the suspend node's time to live (`30m`, `12h`, `2d`, or whole seconds) | refused | |
| 72 | +| `details` | - | optional text, the body | |
| 73 | +| `requested_at` | optional ISO-8601; the default is now | optional | |
| 74 | + |
| 75 | +The function cross-checks `graph_id` against the engine's `x-graph-id` header when one is present. It answers |
| 76 | +`{ticket, link, expires_at, delivered}` for a wait and `{delivered}` for an advice, where `delivered` lists each channel |
| 77 | +route with `ok` and the status it answered; `expires_at` is `requested_at` plus `ttl`, the suspension's own expiry. The |
| 78 | +event a channel receives is the message plus `ticket`, `link` and `expires_at`. |
| 79 | + |
| 80 | +## The ticket |
| 81 | + |
| 82 | +`v1.<payload>.<mac>`: the payload `{"g": graph_id, "c": correlation_id, "t": turn, "k": kind, "e": expiry}` as |
| 83 | +canonical JSON (keys sorted, no spaces, Unicode kept) and its HMAC-SHA256 under `ask.ticket.key`, both base64url without |
| 84 | +padding. Opaque and clean - no question text, no business data, no person - and stateless: the reply endpoint verifies |
| 85 | +it with the key alone, so every channel works without a store. `ask.ticket.previous-key` verifies the tickets issued |
| 86 | +before a rotation. A ticket is accepted as many times as it is presented: single use is the engine's at-most-once |
| 87 | +resume, and the second presentation gets `nothing waiting`. An expired ticket (410) is refused without a call to the |
| 88 | +engine; a malformed one or another version is a 400, one no key signs a 403. |
| 89 | + |
| 90 | +## The reply page and endpoint |
| 91 | + |
| 92 | +- `GET /ask/{ticket}` renders the wait: the graph and the turn from the ticket, the calls from the engine's re-advice |
| 93 | + - approve or refuse with a note per waiting call, an answer (the options offered, or free text) for a question, a |
| 94 | + call marked `wait: false` shown as running on resume. A wait the engine no longer holds (`nothing-waiting`, HTTP 410) |
| 95 | + renders as expired; an engine that has moved on shows its reply. |
| 96 | +- `POST /ask/reply` takes `{"ticket": "...", "decisions": [...], "decided_by": "..."}`: `{id, approved, note?}` for a |
| 97 | + tool call, `{id, answer}` for a question - the shapes the loop accepts. Each tool call's note is prefixed |
| 98 | + `[<who> at <time>]`, so the audit store carries who decided without a new field; an answer travels unchanged. The |
| 99 | + engine's status and JSON body come back as they are; a graph that does not answer or answers outside the contract is a |
| 100 | + 502 `{"type": "error", "message": ...}`; an expired ticket is a 410 `{"type": "expired"}`. |
| 101 | +- **Who may answer** (`ask.reply.auth`): `none` in development - no sign-in, `decided_by` is taken as given or the |
| 102 | + person is `anonymous`; `proxy` - the installation's SSO, a trusted header set by the gateway (`ask.reply.auth.header`, |
| 103 | + `x-forwarded-user`) names the person; `oauth2` - a bearer access token validated against the issuer's published keys |
| 104 | + (`ask.reply.auth.oauth2.issuer`, `.audience`, or `.jwks-url`; PyJWT). In every mode a channel app may present the |
| 105 | + bearer `ask.reply.auth.token` and name the person in `decided_by`. |
| 106 | +- **The allowlist** `ask.graphs` maps a graph id to the engine entry the resume is sent to; a ticket for a graph that is |
| 107 | + not listed is a 404, and the endpoint never posts to an address a message told it. |
| 108 | + |
| 109 | +## Configuration |
| 110 | + |
| 111 | +| Key | Default | Meaning | |
| 112 | +| --- | --- | --- | |
| 113 | +| `ask.enabled` | `true` | `false` answers every call with a 503 | |
| 114 | +| `ask.channels` | `[]` | the routes that receive the message event (a YAML list, or `a,b` as a `-D` value) | |
| 115 | +| `ask.channel.endpoints` | `[]` | `{route, url}` entries for routes served by another process (or `route=url,...`) | |
| 116 | +| `ask.channel.timeout` | `5s` | how long one channel may take | |
| 117 | +| `ask.ticket.key` | - | the HMAC key, a `${ASK_TICKET_KEY}` reference; required | |
| 118 | +| `ask.ticket.previous-key` | - | verifies tickets issued before a rotation | |
| 119 | +| `ask.reply.url` | - | the public base of the resume link; required | |
| 120 | +| `ask.reply.auth` | `none` | `none`, `proxy` or `oauth2` | |
| 121 | +| `ask.reply.auth.header` | `x-forwarded-user` | with `proxy`: the header that names the signed-in person | |
| 122 | +| `ask.reply.auth.oauth2.issuer` / `.audience` / `.jwks-url` | - | with `oauth2` | |
| 123 | +| `ask.reply.auth.token` | - | the bearer a channel app presents when it invokes the link | |
| 124 | +| `ask.graphs` | `[]` | `{id, entry}` entries: the graphs a reply may resume (or `id=url,...`) | |
| 125 | +| `ask.engine.timeout` | `60s` | how long the engine may take to answer a re-advice or a resume | |
| 126 | + |
| 127 | +## Tests |
| 128 | + |
| 129 | +`tests/test_ask_person.py` runs the shared vectors (`tests/vectors/ask-person-vectors.json`: the ticket issued, verified, |
| 130 | +tampered, expired and rotated; the message validated and the event built; the re-advice and the resume requests; the |
| 131 | +outcomes) and drives the function and the page in-process: channels that take, refuse and miss the event; the reply |
| 132 | +endpoint against a fake engine; the three auth modes with a signed token; the host hook that mounts the routes before |
| 133 | +the catch-all. |
0 commit comments