# Asks
A question a conversation waits on a person for, kept in the journal and answered once by any
client. The user's view is [questions](../manual/questions.md); the phone's wire is the
[remote protocol](../design/remote-protocol.md)'s Amendments.
Tier: core (`src/ask.c`). Clock: durability. Record: `pic_ask`. Default: on.
## The table
`pic_ask`, a table of its own made `IF NOT EXISTS` by every full open, so no schema version
([durability](durability.md#the-schema)). A journal no pic of this version has opened has none:
every reader takes a missing table as no asks, and `pic_ask_open` refuses.
| Column | Meaning |
|---|---|
| `ask_key` | the asker's key, unique: the same key is the same question, so a replay finds it |
| `session_id` | the conversation waiting |
| `kind` | `confirm`, `select` or `input` |
| `title`, `body` | what is asked; at most 4000 bytes each |
| `spec` | a select's `{"options":[...]}` (1 to 32 strings), an input's `{"placeholder"}` |
| `created_ms`, `expires_ms` | when it was asked; past `expires_ms` it reads as `expired` |
| `state` | `open`, `answered`, `expired` or `withdrawn` |
| `answer`, `answered_by`, `answered_ms` | the one answer (JSON) and who gave it: `user`, `cli`, `attach`, `remote:DEVICE` |
| `version` | taken from a counter by every open and close, indexed, so a reader follows changes |
Indexes: `pic_ask_open (state, session_id)` for the open ones, `pic_ask_version (version)` for the
changes. `pic remote` and `pic attach` read only these: no whole-history query on their loops.
## One answer
`pic_ask_answer` checks the value against the kind (`true` or `false`, one of the options, text,
`null` to dismiss), then in one `IMMEDIATE` transaction updates the row only while it is still
`open` and not expired. No row changed means another answer came first: the caller gets
`PIC_ASK_ALREADY` with the answer that stands. The same `answered_by` giving the same answer again
is `PIC_ASK_ANSWERED`, so a phone's retry is not told it lost.
A loop or goal the model asked for (`pic_wake_new.ask`) opens its ask, key `wake:ID`, kind
`confirm`, in the transaction that makes the wake, expiring with it. Its answer is the wake's
decision: `pic_ask_answer` hands it to `pic_wake_answer`, whose transaction checks the wake still
waits for an OK, decides it, queues the model's message and closes the ask. `pic loop approve`,
`approve_wake` and the terminal's prompt go through `pic_wake_answer` too, so every path closes the
ask in the decision's transaction. `pic_wake_set` stopping a waiting wake withdraws its ask.
## Waiting
`pic_ask_wait` reads the row every 100 ms until it is not open, or until `pic_interrupted`. The
waiter holds no transaction while it waits. Killed while waiting, it leaves the ask open; the next
owner of the request replays the asking call, which opens the same key and finds the ask, answered
or not. A dev build's `PIC_TEST_ASK` registers `ask_person`, a replay-safe tool that asks and waits,
keyed `fx:EFFECT`.
## Clients
| Client | Shows | Answers |
|---|---|---|
| terminal | polls `get_asks` every 2 s; `OV_ASK` for an ask that is not a wake's (those keep `OV_APPROVE`) | `answer_ask` |
| `pic answer` | the open asks of the journal | `pic answer ID VALUE` |
| `pic attach` | Pi's `extension_ui_request` per ask of its conversation opened since the last pass | `extension_ui_response` on stdin with `-i` |
| RPC | `get_asks` | `answer_ask`, `extension_ui_response` |
| `pic remote` | down `ask`; `asks` in `get_state`; a notification per new ask | up `answer` |
`pic remote` keeps per journal the newest version it told (`ask_version`); at its first look it
only learns it. When the journal's `data_version` changes it reads the asks after it, at most 50,
sends each as an `ask` record to every phone with a confirmed session, and notifies the open ones.
Its own answer does not change its `data_version`, so it looks again on the next tick. A phone's
new session gets every open ask of every journal. An asked wake is told as its ask, not by the
loops' "needs you" check.
## Fault points
`ask_answer`, reached twice per answer: arrival 1 just before the commit, arrival 2 just after.
`test/pic/ask.sh` crashes at both with a run waiting, and kills a waiting run.