Skip to content

Commit 480f20a

Browse files
authored
Merge pull request #70 from Accenture/feat/agent-question-app
feat(agent-question): the store-and-forward inbox of the person an agent asks - RFC-0014 step 3
2 parents ffd3686 + 8a9b4d7 commit 480f20a

12 files changed

Lines changed: 3177 additions & 0 deletions

File tree

‎CHANGELOG.md‎

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -107,6 +107,35 @@
107107
on the bus with channels that take, refuse and miss the event; the page and the endpoint against a fake engine; the
108108
three auth modes with a signed token; the hook). **Upgrade note:** none - `add_routes` is additive.
109109

110+
6. **The agent-question app: a store-and-forward inbox for the person an agent asks (RFC-0014, build plan step 3).**
111+
`extensions/agent-question/agent_question.py` is a standalone application on the package and the first channel of the
112+
ask-a-person design, standing beside a chat, a mail server or a topic as one channel among equals; the main flow
113+
(`person.ask`, step 1) knows nothing of it and sends it the message event like any route. `agent.question.inbox` is
114+
the intake: a wait (an approval or a question) is upserted on `(graph_id, correlation_id, turn)`, so a re-advice for
115+
the same turn updates the card rather than adding one, and an advice (a status update, an acknowledgement, an
116+
outcome) is appended; the row is committed before any push. The person's side runs under the host through
117+
`Platform.add_routes`: `GET /questions/app` is the page, `GET /questions?since=<seq>` the replay a reconnecting
118+
device calls first - every row that changed since its cursor, with the cursor to continue from - and `GET
119+
/questions/events` the stream a connected device holds, each event carrying a changed row and `Last-Event-ID` the
120+
same cursor, so a browser's reconnect misses nothing. The person selects which card to answer, in any order; the
121+
card's button automates the click: `POST /questions/{id}/decide` posts `{ticket, decisions, decided_by}` to the main
122+
flow's reply endpoint - the link's own base, under this app's bearer (`question.reply.token`) - records the outcome
123+
on the row and tells every connected device; a decision the engine accepted leaves the row `answered`, nothing
124+
waiting leaves it `expired`, a refusal or an endpoint that did not answer leaves it `open` with the outcome shown.
125+
`POST /questions/{id}/seen` marks an advice read. Open waits past their expiry are marked `expired` by a sweeper
126+
(`question.sweep`, 30 s) and before every replay. The inbox is memory (development; a restart forgets) or PostgreSQL
127+
(`question.inbox: postgres`, the `postgres` extra; `schema/postgres.sql` with the change sequence
128+
`question_inbox_seq`, the partial unique index that makes waits upsert and advices append, `purge.sql` as the
129+
installation's retention; `dev/schema-apply.sh` applies the schema to the helper's development database). Who may use
130+
the page is `question.auth`: `none` in development (the page offers a name field), `proxy` (the gateway's trusted
131+
header) or `oauth2` (an access token against the issuer's keys). Rows are never deleted by the app. Pinned by
132+
`tests/vectors/agent-question-vectors.json` (30 cases: the row a message becomes, the state an outcome leaves, the
133+
request the click sends, and the reference inbox on a scripted sequence with its ids and change sequence), shared
134+
with the Node twin, and `tests/test_agent_question.py` (48 tests: the intake on the bus, the replay and the stream
135+
with keep-alives, a decision forwarded to a fake reply endpoint and broadcast, an advice seen, expiry, the proxy
136+
mode, the PostgreSQL statements on a recording fake, the hooks and the boot; a live PostgreSQL gated on
137+
`AGENT_QUESTION_TEST_URL`). **Upgrade note:** none - a new application; the main flow lists it in `ask.channels`.
138+
110139
### Fixed
111140

112141
1. **A direct `python extensions/mcp-binding/mcp_binding.py` now connects its servers.** The binding attached its boot
Lines changed: 94 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,94 @@
1+
# The agent-question app
2+
3+
The first channel of RFC-0014 (mercury-composable, `draft-design-specs/ask-a-person-channel.md`, Part II): a
4+
**store-and-forward inbox** for a person whose device is not always connected, standing beside a chat, a mail server
5+
or a topic as one channel among equals. The main flow - `person.ask` in [`extensions/ask-person`](../ask-person/README.md)
6+
- knows nothing of it: it sends one message event with the resume link to the routes it is configured with, and this
7+
application is one of those routes. It is a standalone application on the package, run in its own process.
8+
9+
- **Intake.** `agent.question.inbox` is a composable function reached over Event-over-HTTP like any peer function. A
10+
wait (`kind` `approval` or `question`) is **upserted** on `(graph_id, correlation_id, turn)` - a re-advice for the same
11+
turn updates the card rather than adding one - and an **advice** (a status update, an acknowledgement, an outcome) is
12+
**appended**, since those accumulate. Write first: the row is committed before any push to a browser.
13+
- **The person's side**, under the pack's host: `GET /questions/app` is the page; `GET /questions?since=<seq>` the
14+
**replay** a reconnecting device calls first - every row that changed since its cursor, waits and advices alike, with
15+
the cursor to continue from; `GET /questions/events` the **stream** a connected device holds, each event carrying a
16+
changed row, `Last-Event-ID` the same cursor, so a browser's reconnect misses nothing. The person selects which card
17+
to answer, in any order; the card's button **automates the click**: `POST /questions/{id}/decide` posts
18+
`{ticket, decisions, decided_by}` to the main flow's reply endpoint - the link's own base, under this app's bearer -
19+
records the outcome on the row and tells every connected device. `POST /questions/{id}/seen` marks an advice read.
20+
- **States.** A wait is `open` until the engine accepts the decision (`answered`) or finds nothing waiting
21+
(`expired`); a refused decision or an endpoint that did not answer leaves it `open` with the outcome shown. An
22+
advice is `open` until the person marks it `seen`. Open waits past their expiry are marked `expired` by a sweeper
23+
and before every replay. The engine stays the authority: a second device on the same card gets `nothing waiting`,
24+
and the row records the first. Rows are never deleted here; `schema/purge.sql` is the installation's.
25+
26+
The Node.js twin (mercury-nodejs `extensions/agent-question`) serves the same route, the same page and the same
27+
schema, pinned by the same vector file.
28+
29+
## Run
30+
31+
```bash
32+
mercury-serve extensions/agent-question/agent_question.py # the inbox in memory, no sign-in: development
33+
open http://127.0.0.1:8092/questions/app
34+
```
35+
36+
Point the main flow at it: in the ask-person extension, `ask.channels: [agent.question.inbox]` and
37+
`ask.channel.endpoints: [{route: agent.question.inbox, url: http://127.0.0.1:8092/api/event}]`. For a deployment:
38+
39+
```bash
40+
pip install 'mercury-composable[postgres]'
41+
POSTGRES_PASSWORD=... extensions/llm-helper/dev/postgres-up.sh # the development database the helper also uses
42+
extensions/agent-question/dev/schema-apply.sh # schema/postgres.sql, safe to repeat
43+
mercury-serve extensions/agent-question/agent_question.py -Dquestion.inbox=postgres -Dquestion.inbox.postgres.ssl=false
44+
```
45+
46+
The extension reads `resources/application.yml` next to `agent_question.py`, whichever directory the command runs
47+
from; `-Dkey=value` overrides any key. It listens on port 8092 by default. The boot stops the application by name when
48+
the inbox is `postgres` without the driver, when the database does not answer, or when the auth mode is unknown; with
49+
the inbox in memory it warns that a restart forgets every card.
50+
51+
## The row
52+
53+
What a message event becomes (the replay and the stream carry rows in this shape):
54+
55+
| Field | Meaning |
56+
| --- | --- |
57+
| `id`, `seq` | the row, and the change sequence a replay reads from (bumped on every write) |
58+
| `kind`, `graph_id`, `correlation_id`, `turn` | from the message; a wait upserts on the last three |
59+
| `ticket`, `link`, `expires_at` | a wait's, from the message; null for an advice |
60+
| `description`, `summary`, `details`, `calls` | what the card shows; `calls` is the approval block as the loop returned it |
61+
| `state` | `open`, `answered`, `expired` (a wait) or `seen` (an advice) |
62+
| `requested_at`, `updated_at` | the message's time and the row's last change |
63+
| `answered_at`, `answered_by`, `decisions`, `outcome` | the click: who, when, what, and the reply endpoint's answer |
64+
65+
## Identity
66+
67+
`question.auth`: `none` in development - no sign-in, the page offers a name field the decision carries as
68+
`decided_by`; `proxy` - the installation's SSO, a trusted header set by the gateway (`question.auth.header`,
69+
`x-forwarded-user`) names the person; `oauth2` - a bearer access token validated against the issuer's published keys
70+
(`question.auth.oauth2.issuer`, `.audience`, or `.jwks-url`; PyJWT, the `ask` extra). The app passes the person to the
71+
main flow as `decided_by` under its own bearer (`question.reply.token`, the reply endpoint's `ask.reply.auth.token`).
72+
73+
## Configuration
74+
75+
| Key | Default | Meaning |
76+
| --- | --- | --- |
77+
| `question.inbox` | `memory` | `memory` or `postgres` |
78+
| `question.inbox.postgres.host` / `.port` / `.database` / `.username` / `.password` / `.ssl` / `.timeout.ms` | the helper's audit store's defaults | the connection; the password a `${POSTGRES_PASSWORD}` reference |
79+
| `question.sweep` | `30s` | how often open waits past their expiry are marked expired |
80+
| `question.auth` | `none` | `none`, `proxy` or `oauth2` |
81+
| `question.auth.header` | `x-forwarded-user` | with `proxy` |
82+
| `question.auth.oauth2.issuer` / `.audience` / `.jwks-url` | - | with `oauth2` |
83+
| `question.reply.token` | - | the bearer presented to the reply endpoint |
84+
| `question.reply.timeout` | `10s` | how long the reply endpoint may take |
85+
| `question.page.title` | `Agent questions` | the page's title |
86+
87+
## Tests
88+
89+
`tests/test_agent_question.py` runs the shared vectors (`tests/vectors/agent-question-vectors.json`: the row a message
90+
becomes, the state an outcome leaves, the request the click sends, the inbox's transitions on a scripted sequence) and
91+
drives the app in-process: the intake on the bus, the replay and the stream, a decision forwarded to a fake reply
92+
endpoint with the outcome recorded and broadcast, an advice marked seen, the proxy mode, the PostgreSQL inbox's
93+
statements on a recording fake of the pool, and - gated on `AGENT_QUESTION_TEST_URL` - the schema and the rows on a live
94+
PostgreSQL.

0 commit comments

Comments
 (0)