oficina / base Public

Answer Equipo's refusal of Ask's key with 409, never 401

Astra's review: a relayed write whose key Equipo refused answered
401 {"state":"reconnect"}, and the native kit reads every 401 as Base's
own session ending, so the app would sign in again instead of setting
Ask up again.

askDone now answers 409 {"state":"reconnect"} for every client. A 401
is only ever Base's member gate, {"code":"unauthenticated"}. ask.js
already shows the panel's state from the answer's body, so the web
still shows "Set it up again"; a card's answer reloads the conversation
only for a stale card (409 ready), as before.

Tests: the contract test expects 409 reconnect from send, cancel and
answer; a new test calls every Ask route (state, events, writes, check,
dock, the page, Oficina connect, the native set-up, Disconnect) while
the key is fine, refused as used, lost, and after Equipo's session ended,
and fails on any 401. docs/api/base-native-v1.md says so.

make ci: 16 packages ran, 0 failed; test step 11.0s of 60s.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Paul Campbell pushed by claude agent, operated by paul@keymail.dev cdfeac760232bc581415c37e09523d0952e1722b parent 04c24e2
4 files changed, +78 −9
  • docs/api/base-native-v1.md +5 −1
  • internal/look/static/ask.js +3 −2
  • internal/web/ask_relay.go +3 −2
  • internal/webtest/ask_native_contract_test.go +67 −4
diff --git a/docs/api/base-native-v1.md b/docs/api/base-native-v1.md
index c514a66..9bc4f88 100644
--- a/docs/api/base-native-v1.md
+++ b/docs/api/base-native-v1.md
@@ -83,6 +83,10 @@ this document breaks the web too.
## Ask Base
+No Ask route answers `401` while Base's session is good: Equipo refusing
+Ask's key is `409 {"state":"reconnect"}` (or `state` `reconnect` in a read).
+A `401` is only ever Base's own `{"code":"unauthenticated"}`.
+
The web's own routes. Writes are **form-encoded**
(`application/x-www-form-urlencoded`), not JSON, and must send
`Accept: application/json` to get JSON back (without it they redirect, as
@@ -197,7 +201,7 @@ Form: `choice` (an option's `value`), `revision` (the card's, as seen) and
| status | body | meaning |
|---|---|---|
| `409` | `{"state":"connect"\|"pending"\|"reconnect"\|"unavailable"}` | Not connected: nothing was sent. |
-| `401` | `{"state":"reconnect"}` | **Equipo** refused the key. This is not Base's session ending (that's `{"code":"unauthenticated"}`): don't sign in again; connect Ask again. |
+| `409` | `{"state":"reconnect"}` | Equipo refused the key as it was used. Set Ask up again (with Oficina ID, below). |
| `409` | `{"state":"norunner"\|"oldrunner"}` | No runner can answer; nothing was sent. |
| `502` | `{"state":"unavailable"}` | Equipo didn't answer. Try again with the same key. |
| `400` | plain text | The form couldn't be read. |
diff --git a/internal/look/static/ask.js b/internal/look/static/ask.js
index 862b0cd..8a06515 100644
--- a/internal/look/static/ask.js
+++ b/internal/look/static/ask.js
@@ -1570,7 +1570,8 @@ function start(root) {
if (root.dataset.state !== "ready") show("ready");
return out;
}
- // Lost the connection: the panel says what to do. No runner that
+ // Lost the connection (a 409 saying so; Equipo refusing the key is
+ // 409 reconnect, never 401): the panel says what to do. No runner that
// can answer: the conversation stays, with a note saying so.
// Otherwise the conversation stays, with a line to try again (a 409
// is stale: the next events show how things are).
@@ -1620,7 +1621,7 @@ function start(root) {
for (const b of form.querySelectorAll("button")) b.disabled = false;
return;
}
- if (!out && lastStatus === 409) {
+ if (!out && lastStatus === 409 && lastState === "ready") {
// The card moved on (a new revision, or answered elsewhere): read the
// conversation afresh and let the person choose again. Never resend.
await load();
diff --git a/internal/web/ask_relay.go b/internal/web/ask_relay.go
index b6dab9e..92a5603 100644
--- a/internal/web/ask_relay.go
+++ b/internal/web/ask_relay.go
@@ -61,7 +61,8 @@ type askConn struct {
}
// askDone finishes a relayed write. A 401 from Equipo means the member must
-// reconnect; a 409 no_runner or runner_too_old means the person has no
+// reconnect: Base answers 409 reconnect, never 401, which is only ever
+// Base's own session ending (the native app signs in again on one); a 409 no_runner or runner_too_old means the person has no
// runner that can answer (norunner, oldrunner), and what they wrote stays
// unsent; any other 409 (a card already answered, a turn already ended) is
// stale, and the conversation as it is now says so; anything else is a
@@ -72,7 +73,7 @@ func (a *app) askDone(w http.ResponseWriter, r *http.Request, c askConn, err err
switch {
case errors.Is(err, equipo.ErrUnauthenticated):
a.askLost(r.Context(), c.me, c.device)
- status, state = http.StatusUnauthorized, askReconnect
+ status, state = http.StatusConflict, askReconnect
case errors.Is(err, equipo.ErrNoRunner):
status, state = http.StatusConflict, askNoRunner
case errors.Is(err, equipo.ErrRunnerTooOld):
diff --git a/internal/webtest/ask_native_contract_test.go b/internal/webtest/ask_native_contract_test.go
index 1d6a828..be42266 100644
--- a/internal/webtest/ask_native_contract_test.go
+++ b/internal/webtest/ask_native_contract_test.go
@@ -301,13 +301,25 @@ func TestAskContractCards(t *testing.T) {
wantBody(t, "stale", app.nativeForm("/ask/cards/"+q2+"/answer", url.Values{"choice": {"S"}, "revision": {"1"}, "key": {"a-3"}}), 409, `{"state":"ready"}`)
}
-// Equipo refusing the key is Ask's 401 {"state":"reconnect"}, not Base's
-// own {"code":"unauthenticated"}: the session is fine.
+// Equipo refusing the key is Ask's 409 {"state":"reconnect"}, never a 401:
+// a 401 is always Base's own session ending ({"code":"unauthenticated"}),
+// and the kit signs in again on one.
func TestAskContractLostKey(t *testing.T) {
web, app, f := newNativeAskApp(t)
web.approveEquipo(f)
- f.RevokeAll()
- wantBody(t, "send", app.nativeForm("/ask/messages", url.Values{"text": {"Hi"}, "key": {"l-1"}}), 401, `{"state":"reconnect"}`)
+ for i, call := range []func() Resp{
+ func() Resp { return app.nativeForm("/ask/messages", url.Values{"text": {"Hi"}, "key": {"l-1"}}) },
+ func() Resp { return app.nativeForm("/ask/turns/t_0001/cancel", url.Values{"key": {"l-2"}}) },
+ func() Resp {
+ return app.nativeForm("/ask/cards/c_0001/answer", url.Values{"choice": {"go"}, "revision": {"1"}, "key": {"l-3"}})
+ },
+ } {
+ // Each time the key is refused afresh: reconnect, and Base's
+ // stored link is lost.
+ web.approveEquipo(f)
+ f.RevokeAll()
+ wantBody(t, "write "+strconv.Itoa(i), call(), 409, `{"state":"reconnect"}`)
+ }
if s, _ := app.askState(); s.State != "reconnect" {
t.Errorf("state after a refused key: %s", s.State)
}
@@ -316,6 +328,57 @@ func TestAskContractLostKey(t *testing.T) {
}
}
+// While Base's session is good, no Ask route answers 401, whatever Equipo
+// says of the key: connected, refused as it is used, or already lost.
+func TestAskNeverAnswers401WithASession(t *testing.T) {
+ web, app, f := newNativeAskApp(t)
+ f.LinkOficinaID("subject-owner", ownerAddress)
+ calls := map[string]func() Resp{
+ "state": func() Resp { return app.nativeGet("/ask/state") },
+ "send": func() Resp { return app.nativeForm("/ask/messages", url.Values{"text": {"Hi"}, "key": {equipoKey()}}) },
+ "cancel": func() Resp { return app.nativeForm("/ask/turns/t_9999/cancel", url.Values{"key": {equipoKey()}}) },
+ "answer": func() Resp {
+ return app.nativeForm("/ask/cards/c_9999/answer", url.Values{"choice": {"x"}, "revision": {"1"}, "key": {equipoKey()}})
+ },
+ "check": func() Resp { return app.nativeForm("/ask/connect/check", url.Values{"next": {"ask"}}) },
+ "dock": func() Resp { return app.nativeForm("/ask/dock", url.Values{"open": {"1"}}) },
+ "page": func() Resp { return app.get("/ask") },
+ "oficina": func() Resp { return app.get("/ask/oficina/connect") },
+ "set-up": func() Resp { return app.connectWithOficina(f, "subject-owner") },
+ "replayed": func() Resp { return app.nativePost("/api/v1/ask/oficina", `{"assertion":"x","verifier":"y"}`) },
+ }
+ try := func(when string) {
+ t.Helper()
+ for name, call := range calls {
+ if name == "set-up" {
+ continue
+ }
+ if r := call(); r.Code == http.StatusUnauthorized {
+ t.Errorf("%s, %s: 401 %s", when, name, r.Body)
+ }
+ }
+ app.events(t, "0", func([]sseFrame) bool { return true })
+ }
+ try("not connected")
+ web.approveEquipo(f)
+ try("connected")
+ f.RevokeAll()
+ try("refused as used")
+ try("lost")
+ if r := calls["set-up"](); r.Code != 200 {
+ t.Fatalf("set-up: %d %s", r.Code, r.Body)
+ }
+ f.EndNativeSession(f.DeviceSession(liveDevice(t, f)))
+ try("Equipo's session ended")
+ if r := app.post("/ask/disconnect", nil); r.Code == http.StatusUnauthorized {
+ t.Errorf("disconnect: 401")
+ }
+}
+
+var keySeq int
+
+func equipoKey() string { keySeq++; return "never-401-" + strconv.Itoa(keySeq) }
+
// sseFrame is one frame of an event stream, as the native client parses it.
type sseFrame struct {
ID, Event, Data, Retry string