Skip to content

Commit 5fef533

Browse files
authored
Merge pull request #68 from Accenture/feat/ask-person-main-flow
feat(ask-person): the main flow of RFC-0014 - person.ask, the ticket, the resume link and the reply page (step 1)
2 parents dcf2a69 + ee0bd46 commit 5fef533

11 files changed

Lines changed: 3775 additions & 4 deletions

File tree

‎CHANGELOG.md‎

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,30 @@
8383
through the binding's own probe and compares the digests (GitHub's when `GITHUB_MCP_TOKEN` is set, in both modes), so
8484
a recipe that rots fails by name.
8585

86+
5. **Ask a person, the main flow (RFC-0014 in mercury-composable, build plan step 1).**
87+
`extensions/ask-person/ask_person.py` serves `person.ask`, the composable function a graph's hold path calls before
88+
it suspends for a person: the message names the graph (`graph_id`, `description`, `node`), the transaction
89+
(`correlation_id`, `turn`), the waiting `calls` as the loop returned them and the suspension's `ttl`; the function
90+
issues a signed, stateless ticket (`v1.<payload>.<mac>`, the canonical payload and its HMAC-SHA256 under
91+
`ask.ticket.key`, a previous key verifying after a rotation), builds the resume link and sends the whole message as
92+
one event to every route in `ask.channels` - the agent-question app's intake, a chat poster, a mail sender, a topic
93+
publisher - knowing nothing of what a route does with it; a channel that fails is logged by type and never fails the
94+
hold. A third kind, `advice`, is a notification with no decision (a status update of a long run, an acknowledgement
95+
after a response): no ticket, no link. The resume link opens the wait on the process's reply page, `GET
96+
/ask/{ticket}`, read from the engine by the reference graph's re-advice and never resuming - link unfurlers click
97+
links; `POST /ask/reply` with `{ticket, decisions}` - a person's browser, or a channel app under its bearer with
98+
`decided_by` - verifies the ticket, maps the graph id to the engine's entry through the `ask.graphs` allowlist (never
99+
to an address a message told it), prefixes who decided and when to each tool call's note, sends the resume (`{turn,
100+
decisions}` with the correlation id) and returns the engine's outcome; an expired ticket is a 410 without a call to
101+
the engine. Who may answer is `ask.reply.auth`: `none` in development, `proxy` (the gateway's trusted header, the
102+
installation's SSO) or `oauth2` (an access token validated against the issuer's published keys; the new `ask` extra
103+
adds PyJWT). The host gains `Platform.add_routes`, the hook an extension mounts its own pages and streams with, after
104+
the host's paths and before the catch-all. Pinned by `tests/vectors/ask-person-vectors.json` (72 cases: the ticket
105+
issued, verified, tampered, expired and rotated; the message validated and the event built; the re-advice and the
106+
resume requests; the outcomes), shared byte for byte with the Node twin, and `tests/test_ask_person.py` (the function
107+
on the bus with channels that take, refuse and miss the event; the page and the endpoint against a fake engine; the
108+
three auth modes with a signed token; the hook). **Upgrade note:** none - `add_routes` is additive.
109+
86110
### Fixed
87111

88112
1. **A direct `python extensions/mcp-binding/mcp_binding.py` now connects its servers.** The binding attached its boot

‎README.md‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -231,6 +231,10 @@ transcript lives in the store the helper is configured with. The built-in agent
231231
`extensions/agent-tools/` are the essential tools such a loop names in its catalog - files inside one
232232
workspace, the web inside an allowlist of sites, a clock - each a composable function whose sandbox is
233233
deployment configuration, never an argument of a call.
234+
The ask-a-person main flow in `extensions/ask-person/` carries such a loop's wait for a person to the
235+
installation's channels and brings the decision back through a resume link, knowing no channel and keeping
236+
no store; the agent-question app, a store-and-forward channel for an occasionally connected device, is a
237+
separate application on the same package.
234238

235239
## Development
236240

‎extensions/ask-person/README.md‎

Lines changed: 133 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,133 @@
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

Comments
 (0)