| 1 | # Equipo turn batching |
| 2 | |
| 3 | 2026-11-11, branch `turn-batching` from main 03f8a91. |
| 4 | |
| 5 | ## What happens today |
| 6 | |
| 7 | The instance already batches. This has been the case since the first thin |
| 8 | v1 commit (64cd2ce, the thin v1 spec's §2 "Second message rule", tested by |
| 9 | `TestQueuedMessagesJoinOneTurn` and headless scenario 6). |
| 10 | |
| 11 | 1. `chatSend` (the page) and `POST /api/v1/messages` (devices) both call |
| 12 | `Store.Send`, then `txn.send` in one write transaction. |
| 13 | 2. `send` looks for the person's queued, undispatched person turn. If it |
| 14 | finds one, the message joins it. If not, it creates a new turn |
| 15 | (`chat_turns`, state `queued`). The message row (`chat_messages`) points |
| 16 | at that turn. |
| 17 | 3. If no dispatched turn is in flight (`queued`, `running` or `waiting`), |
| 18 | `dispatch` runs in the same transaction. It writes one `message` command |
| 19 | to the person's primary runner's mailbox (`runner_commands`): the |
| 20 | attachment frames first, then `wire.MessageCommand` with `Text`, |
| 21 | `PersonText` (one entry per message), the tail and the attachment refs. |
| 22 | The turn becomes `dispatched`. If a turn is already in flight, the |
| 23 | message waits in the queued turn. |
| 24 | 4. The runner's ack moves the turn to `running`. `reply`, `report` |
| 25 | (failed or cancelled), `sync` (a turn the runner no longer holds) and |
| 26 | `Cancel` of an undispatched turn each end the turn and call |
| 27 | `dispatchNext` in the same transaction. `dispatchNext` dispatches the |
| 28 | oldest queued turn (the person's own first) once nothing is in flight. |
| 29 | 5. The runner (`thin.coordBase.prompt`) puts `Text` under "Newest message" |
| 30 | in a fresh session, or sends it alone in a warm one. Claude and Codex |
| 31 | read the same text. |
| 32 | |
| 33 | So three messages sent during a slow turn already become one next turn and |
| 34 | one reply. A message sent while nothing runs starts its own turn at once. |
| 35 | The 13 staging turns fit messages that mostly arrived between turns. I |
| 36 | could not check this, because I did not read staging. |
| 37 | |
| 38 | ## What was missing |
| 39 | |
| 40 | - **Distinct messages.** `Text` was the messages joined with a blank line. |
| 41 | "two\n\nthree" reads the same as one message with two paragraphs. The |
| 42 | fake coordinator, which splits on blank lines, counted the attachment |
| 43 | note as a fourth message. |
| 44 | - **Attachments per message.** The runner lists every file of the turn as |
| 45 | "the person attached these files", with nothing tying a file to the |
| 46 | message that carried it. |
| 47 | - **Recovery of a stranded queued turn.** Each end path dispatches the next |
| 48 | turn in the same transaction, so the current code cannot strand one. A |
| 49 | queued turn left behind with nothing in flight (an older build, a manual |
| 50 | repair) waited until the person sent another message. |
| 51 | |
| 52 | ## The change |
| 53 | |
| 54 | All of it is on the instance, mostly in its turn building. There is no |
| 55 | wire, protocol, fixture or migration change. |
| 56 | |
| 57 | - `dispatch` builds `Text` with `turnText`. One message is sent exactly as |
| 58 | before. Several messages are sent as: |
| 59 | |
| 60 | ``` |
| 61 | [Equipo: the person sent 3 messages while you were busy, oldest first. Each is a separate message; answer them together in one reply.] |
| 62 | |
| 63 | Message 1 of 3: |
| 64 | alpha first |
| 65 | |
| 66 | Message 2 of 3 (attached: notes.md): |
| 67 | bravo second |
| 68 | |
| 69 | Message 3 of 3: |
| 70 | charlie third |
| 71 | ``` |
| 72 | |
| 73 | `relayFiles` also returns each message's file names. `PersonText` and |
| 74 | `Attachments` keep their meaning. Every protocol reads `Text` as the |
| 75 | person's words, so old and new runners get the same frame shape. |
| 76 | - `sync`, after it reconciles, calls `dispatchNext` for the person. |
| 77 | `dispatchNext` does nothing while a turn is in flight. A stranded queued |
| 78 | turn therefore starts on the runner's next connection, exactly once. |
| 79 | - **One frame each** (Astra round 1). A runner reads at most |
| 80 | `wire.MaxFrameBytes` (256 KiB) and refuses a larger frame every time it |
| 81 | is resent. Each message is up to 16 KiB and goes in twice (`Text` and |
| 82 | `PersonText`), JSON-escaped. A message joins the newest waiting batch |
| 83 | only while the batch's own part fits `batchRoom` (the frame less 64 |
| 84 | KiB). Otherwise it starts the next batch, which goes after the first, |
| 85 | one turn at a time and in order. `dispatch` drops the oldest tail |
| 86 | messages until the command fits (`fitTail`). The tail never holds the |
| 87 | messages of a batch still waiting behind the one being sent. |
| 88 | - **A failed runner frame is retryable** (Astra round 1). A store error |
| 89 | while applying a runner frame used to close the socket with |
| 90 | `CloseRevoked`, so a real runner stopped as if its credential had been |
| 91 | revoked. Now it closes with 1011 (internal error): the runner |
| 92 | reconnects with backoff and resends the unacked frame. `ErrForbidden` |
| 93 | keeps `CloseRevoked`. A credential revoked meanwhile is refused at the |
| 94 | handshake (403), and every runner version maps that to revoked. Old |
| 95 | runners treat any other close code as a reason to reconnect. |
| 96 | |
| 97 | ## Crash, retry and end cases |
| 98 | |
| 99 | | Case | What happens | Contract | |
| 100 | |---|---|---| |
| 101 | | Crash between "turn ended" and "next turn created" | The end and the next dispatch are one transaction. Neither commits. The runner's reply is not acked, so it comes again after the restart. The inbox row rolled back with the transaction, so the reply applies once and starts one next turn. | `TestBatchCrashBetweenTurns` | |
| 102 | | Same reply again, runner reconnects | The inbox drops the duplicate reply. The reconnect resends the same unacked command (same frame id), not a new turn. | `TestBatchCrashBetweenTurns` | |
| 103 | | End committed without its successor | The next `sync` dispatches it once. A second sync does nothing. | `TestBatchStrandedRecovers` | |
| 104 | | Running turn fails, is cancelled, or is lost across a runner reconnect | The batch goes next, once. The ended turn keeps only its own message. | `TestBatchAfterTurnEnds` | |
| 105 | | Running turn retried (attempt 2) | The batch keeps waiting. A message sent during the retry joins it. A late attempt-1 reply is ignored. The batch goes once, after attempt 2 replies. | `TestBatchWaitsForRetriedTurn` | |
| 106 | | The instance refuses the next turn (a store error) with a real runner | The reply rolls back and the socket closes 1011. The runner reconnects and resends the reply, and the batch goes once. | `TestHeadlessBatchAfterRefusedReply` | |
| 107 | | Batch too big for one frame | It is split into several batches, sent one after another, in order. Each frame fits. | `TestBatchSplitsToFitTheFrame` | |
| 108 | | Batch cancelled while it waits | All of its messages end `cancelled` together, unsent. The next message gets a turn of its own. | `TestBatchCancelledWhileWaiting` | |
| 109 | | Bubbles and the queued note | Each message keeps its own bubble in the queued turn. One note sits under the latest one, as the thinking-stale fix did. The note clears for all of them when the batch's frame goes out, before the ack. | `TestBatchMarkerClearsTogether`, `TestHeadlessTurnBatching` | |
| 110 | |
| 111 | ## Usage attribution |
| 112 | |
| 113 | Usage is recorded per turn (`chat_usage.turn_id`). A batch is one |
| 114 | coordinator turn, so it is one user message to the CLI and one result. |
| 115 | Claude's rows come from that result's `model_usage_since_last_result`. |
| 116 | Codex's `CodexTally` counts one provider turn per attempt. Both attribute |
| 117 | the whole batch to the batch turn. Nothing reads usage per message. |
| 118 | `TestHeadlessTurnBatching` checks for one row for the batch turn and none |
| 119 | for any single message. |
| 120 | |
| 121 | ## Rulings (controller, 2026-11-11) |
| 122 | |
| 123 | 1. **No hold.** A message sent while nothing runs starts its turn at |
| 124 | once. A fast first reply beats batching messages that arrive between |
| 125 | turns. |
| 126 | 2. **One note.** The single "Waiting its turn" note under the latest |
| 127 | waiting message stays, as the thinking-stale fix built it. |
| 128 | 3. **Plural failure.** A failed turn that covered more than one message |
| 129 | says `chat.failed_many`, "The coordinator could not answer these |
| 130 | messages." (Astra copy pass: KEEP). The page and the event stream |
| 131 | (`chat-stream.js`, from the turn's `message_ids`) both choose it. |
| 132 | `TestBatchFailedSaysThemAll` and `TestBrowserInstantThinkingQueued` |
| 133 | cover it. |
| 134 | |