Skip to content
pi-c Artifact [6b52b2eaf6]
Login

Artifact [6b52b2eaf6]

Artifact 6b52b2eaf643e6dc070fc0391cee0a3b4746523fe4e1a74105a35ae6855a92b3:


# 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 |
|---|---|
| `id` | 22 characters of random base64url (16 bytes of the platform's entropy), the id every surface uses |
| `ask_key` | the asker's key, unique: the same key is the same question, so a replay finds it |
| `session_id` | the conversation waiting |
| `submission_id` | the request whose run waits on it, or NULL (a loop's OK) |
| `kind` | `confirm`, `select` or `input` |
| `title`, `body` | what is asked, cut to 120 and 2,000 characters when it is asked |
| `spec` | a select's `{"options":[{"label","destructive","default"}]}` (1 to 10 distinct labels of at most 60 characters, at most one default), 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: `terminal` (the interface, `pic answer`, `pic loop approve`), `rpc`, `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 labels, text of
at most 4,000 characters, `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. An answer to an open ask past its
expiry closes it as `expired` (taking a version, so readers are told) and gets `PIC_ASK_EXPIRED`. 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, answers with `by` `terminal`; `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,
and sends each open one as `ask` and each closed one as `ask_settled` to every phone with a
confirmed session, notifying the open ones with its own text. Every 5 s it also reads the open asks
past their expiry (by the `state` index) and tells each, once, as `ask_settled` with kind
`expired`, remembering the last 64 it told. 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. The list counts each
conversation's open asks in one grouped read by index; `get_state` adds `settledAsks`
(`pic_ask_settled`: settled asks whose `submission_id` is still admitted).

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