|
| 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