diff --git a/CHANGELOG.md b/CHANGELOG.md index 9769bea..1e341f2 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -83,6 +83,30 @@ through the binding's own probe and compares the digests (GitHub's when `GITHUB_MCP_TOKEN` is set, in both modes), so a recipe that rots fails by name. +5. **Ask a person, the main flow (RFC-0014 in mercury-composable, build plan step 1).** + `extensions/ask-person/ask_person.py` serves `person.ask`, the composable function a graph's hold path calls before + it suspends for a person: the message names the graph (`graph_id`, `description`, `node`), the transaction + (`correlation_id`, `turn`), the waiting `calls` as the loop returned them and the suspension's `ttl`; the function + issues a signed, stateless ticket (`v1..`, the canonical payload and its HMAC-SHA256 under + `ask.ticket.key`, a previous key verifying after a rotation), builds the resume link and sends the whole message as + one event to every route in `ask.channels` - the agent-question app's intake, a chat poster, a mail sender, a topic + publisher - knowing nothing of what a route does with it; a channel that fails is logged by type and never fails the + hold. A third kind, `advice`, is a notification with no decision (a status update of a long run, an acknowledgement + after a response): no ticket, no link. The resume link opens the wait on the process's reply page, `GET + /ask/{ticket}`, read from the engine by the reference graph's re-advice and never resuming - link unfurlers click + links; `POST /ask/reply` with `{ticket, decisions}` - a person's browser, or a channel app under its bearer with + `decided_by` - verifies the ticket, maps the graph id to the engine's entry through the `ask.graphs` allowlist (never + to an address a message told it), prefixes who decided and when to each tool call's note, sends the resume (`{turn, + decisions}` with the correlation id) and returns the engine's outcome; an expired ticket is a 410 without a call to + the engine. Who may answer is `ask.reply.auth`: `none` in development, `proxy` (the gateway's trusted header, the + installation's SSO) or `oauth2` (an access token validated against the issuer's published keys; the new `ask` extra + adds PyJWT). The host gains `Platform.add_routes`, the hook an extension mounts its own pages and streams with, after + the host's paths and before the catch-all. Pinned by `tests/vectors/ask-person-vectors.json` (72 cases: the ticket + issued, verified, tampered, expired and rotated; the message validated and the event built; the re-advice and the + resume requests; the outcomes), shared byte for byte with the Node twin, and `tests/test_ask_person.py` (the function + on the bus with channels that take, refuse and miss the event; the page and the endpoint against a fake engine; the + three auth modes with a signed token; the hook). **Upgrade note:** none - `add_routes` is additive. + ### Fixed 1. **A direct `python extensions/mcp-binding/mcp_binding.py` now connects its servers.** The binding attached its boot diff --git a/README.md b/README.md index 9784256..486b81a 100644 --- a/README.md +++ b/README.md @@ -231,6 +231,10 @@ transcript lives in the store the helper is configured with. The built-in agent `extensions/agent-tools/` are the essential tools such a loop names in its catalog - files inside one workspace, the web inside an allowlist of sites, a clock - each a composable function whose sandbox is deployment configuration, never an argument of a call. +The ask-a-person main flow in `extensions/ask-person/` carries such a loop's wait for a person to the +installation's channels and brings the decision back through a resume link, knowing no channel and keeping +no store; the agent-question app, a store-and-forward channel for an occasionally connected device, is a +separate application on the same package. ## Development diff --git a/extensions/ask-person/README.md b/extensions/ask-person/README.md new file mode 100644 index 0000000..41b6e44 --- /dev/null +++ b/extensions/ask-person/README.md @@ -0,0 +1,133 @@ +# Ask a person - the main flow + +An agent loop can wait for a person: a tool call marked `write: true` waits under the loop's default mode, and the +**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 +conversation with the answer. The graph suspends durably on its state store and resumes at most once. What was missing +is the person: nothing carried a waiting call to anyone, and nothing brought a decision back except a request a +developer typed. This extension is the **main flow** of RFC-0014 (mercury-composable, +`draft-design-specs/ask-a-person-channel.md`): it carries the wait out and brings the decision in, knows no channel, +and keeps no store - the engine's at-most-once resume stays the authority. + +- **`person.ask`** is a composable function the graph's hold path calls before it suspends. The message names the + graph (`graph_id`, `description`, `node`), the transaction (`correlation_id`, `turn`), the waiting `calls` exactly as + the loop returned them, and the suspension's `ttl`. The function issues a signed, stateless **ticket**, builds the + **resume link** and sends the whole message as one event to every route in `ask.channels` - the agent-question app's + intake, a chat poster, a mail sender, a topic publisher - knowing nothing of what a route does with it. A channel + that fails is logged by type and never fails the hold. A third kind, **`advice`**, is a notification with no decision + (a status update of a long run, an acknowledgement after a response): no ticket, no link. +- **The resume link**, `/ask/`, opens the wait on this process's reply page. A `GET` shows the + current wait, read from the engine by the reference graph's re-advice (a request without decisions), and never + resumes - link unfurlers and mail scanners click links. Only a submit resumes: **`POST /ask/reply`** with + `{ticket, decisions}` - a person's browser, or a channel app on the person's behalf under its own bearer with + `decided_by` - verifies the ticket, maps the graph id to the engine's entry through the `ask.graphs` allowlist (never + to an address a message told it), prefixes who decided and when to each tool call's note, sends the resume (the same + entry, the correlation id in `x-correlation-id`, `{turn, decisions}`) and returns the engine's outcome to the invoker. + +The Node.js twin (mercury-nodejs `extensions/ask-person`) serves the same route and the same page, pinned by the +same vector file. + +## Run + +```bash +pip install mercury-composable # the open and the proxy modes add no dependency +pip install 'mercury-composable[ask]' # + PyJWT for the oauth2 mode of the reply page +export ASK_TICKET_KEY='a long random secret' +mercury-serve extensions/ask-person/ask_person.py -Dask.ticket.key='${ASK_TICKET_KEY}' +python extensions/ask-person/ask_person.py # the same without mercury-serve +python extensions/ask-person/ask_person.py inspect v1.eyJ... # read a ticket (a check, never a resume) +``` + +The extension reads `resources/application.yml` next to `ask_person.py`, whichever directory the command runs from; +`-Dkey=value` overrides any key, and a value may carry `${ENV_VAR:default}` references. It listens on port 8091 by +default (`rest.server.port`). The boot stops the application by name when the ticket key is not set, the reply URL is +not an http or https base, the auth mode is unknown, or `oauth2` is chosen without PyJWT or without an issuer or a JWKS +URL; it warns when no channel or no graph is configured. + +## The message + +```json +{ + "kind": "approval", + "graph_id": "order-assistant", + "description": "Customer-support assistant: a refund waits for a supervisor", + "correlation_id": "order-1002-case", + "turn": 2, + "node": "assist", + "summary": "issue_refund for ORD-1002, 174.00 USD: 'Shipment lost in transit'", + "calls": [{"id": "toolu_01...", "name": "issue_refund", "route": "demo.refund.issue", "wait": true, + "input": {"order_id": "ORD-1002", "amount": 174.0, "reason": "Shipment lost in transit"}}], + "ttl": "2d", + "requested_at": "2026-11-19T09:12:04.118Z" +} +``` + +| Field | A wait (`approval`, `question`) | An `advice` | +| --- | --- | --- | +| `kind` | `approval` (a call needs `approved`) or `question` (an ask row) | `advice` | +| `graph_id`, `description`, `correlation_id`, `summary` | required text | required text | +| `node` | optional: the loop node that waits | optional | +| `turn` | required, a whole number: the turn that waits | optional: the turn it follows | +| `calls` | required: the approval block as the loop returned it | refused | +| `ttl` | required: the suspend node's time to live (`30m`, `12h`, `2d`, or whole seconds) | refused | +| `details` | - | optional text, the body | +| `requested_at` | optional ISO-8601; the default is now | optional | + +The function cross-checks `graph_id` against the engine's `x-graph-id` header when one is present. It answers +`{ticket, link, expires_at, delivered}` for a wait and `{delivered}` for an advice, where `delivered` lists each channel +route with `ok` and the status it answered; `expires_at` is `requested_at` plus `ttl`, the suspension's own expiry. The +event a channel receives is the message plus `ticket`, `link` and `expires_at`. + +## The ticket + +`v1..`: the payload `{"g": graph_id, "c": correlation_id, "t": turn, "k": kind, "e": expiry}` as +canonical JSON (keys sorted, no spaces, Unicode kept) and its HMAC-SHA256 under `ask.ticket.key`, both base64url without +padding. Opaque and clean - no question text, no business data, no person - and stateless: the reply endpoint verifies +it with the key alone, so every channel works without a store. `ask.ticket.previous-key` verifies the tickets issued +before a rotation. A ticket is accepted as many times as it is presented: single use is the engine's at-most-once +resume, and the second presentation gets `nothing waiting`. An expired ticket (410) is refused without a call to the +engine; a malformed one or another version is a 400, one no key signs a 403. + +## The reply page and endpoint + +- `GET /ask/{ticket}` renders the wait: the graph and the turn from the ticket, the calls from the engine's re-advice + - approve or refuse with a note per waiting call, an answer (the options offered, or free text) for a question, a + call marked `wait: false` shown as running on resume. A wait the engine no longer holds (`nothing-waiting`, HTTP 410) + renders as expired; an engine that has moved on shows its reply. +- `POST /ask/reply` takes `{"ticket": "...", "decisions": [...], "decided_by": "..."}`: `{id, approved, note?}` for a + tool call, `{id, answer}` for a question - the shapes the loop accepts. Each tool call's note is prefixed + `[ at