# Turn batching: progress

Branch `turn-batching` from main 03f8a91. Design note:
`docs/superpowers/specs/2026-11-11-equipo-turn-batching.md`.

## Done

1. **Finding.** The instance already batched messages sent during a turn
   into one next turn (64cd2ce). The gap was that the messages were not
   distinct: `Text` was the words joined by blank lines, and the files
   were one list for the whole turn.
2. **Change** (instance only, no wire, fixture or migration change):
   `turnText` sets each message of a batch apart, with its files. `sync`
   starts a stranded queued turn.
3. **Failing first.** `TestHeadlessTurnBatching` (real runner, fake CLI,
   thintest instance) failed on main for the input check. The output is
   in commit fd22d42.
4. **Contracts** (`internal/thintest/batching_contract_test.go`, protocol
   1 runner): `TestBatchCrashBetweenTurns`, `TestBatchStrandedRecovers`,
   `TestBatchAfterTurnEnds` (failed, cancelled, interrupted),
   `TestBatchWaitsForRetriedTurn`, `TestBatchCancelledWhileWaiting`,
   `TestBatchMarkerClearsTogether`, `TestBatchSplitsToFitTheFrame`, and
   headless `TestHeadlessBatchAfterRefusedReply` (real runner).
5. **Astra round 1** (gpt-6-astra, high): REQUEST CHANGES, three
   Important findings.
   - Oversized batches could exceed the 256 KiB frame, and the runner
     would refuse them on every resend. Fixed: batches split to fit, the
     tail is trimmed to fit, and a waiting batch is kept out of an
     earlier batch's tail.
   - A store error on a runner frame closed with `CloseRevoked`, which
     stops a real runner. Fixed: the close is now 1011 (retryable);
     `ErrForbidden` still closes `CloseRevoked`.
   - Queued marker on each bubble: this is a product question, left for
     the controller. The branch keeps the single note from the
     thinking-stale fix.
6. **Rulings.** No hold between turns, and the single note stays.
   `chat.failed_many` ("The coordinator could not answer these
   messages.") is added for a failed batch, on the page and from the
   event stream. Delegated Astra copy pass: KEEP, marked COPY ACCEPTED BY
   DELEGATION (Astra, 2026-11-11) in copy.go and the Plan 3 inventory.
   Mutations: drop the page's batch case and `TestBatchFailedSaysThemAll`
   dies ("a failed batch lacks the note for its messages"); drop the
   script's and `TestBrowserInstantThinkingQueued` dies ("waiting for the
   failed batch's note").
7. **Merged origin/main** 42a31cb (ship buttons). The budgets conflict
   was resolved by keeping both reasons (internal/thintest at 13600, then 13630 with the plural note).

## Mutations

Each guard was broken once, with the targeted runs below, then restored.
Command: `CGO_ENABLED=0 nice -n 10 go test ./internal/thintest -run
'^(TestBatch|TestQueuedMessagesJoinOneTurn$)'` and, where shown, `go test
./internal/headless -run '^TestHeadlessTurnBatching$'`.

| Guard | Mutation | Died |
|---|---|---|
| Ordering | dispatch reads the batch `seq DESC` | thintest: CrashBetweenTurns "batch lacks alpha in place", StrandedRecovers and others; headless: "turn input lacks Message 1 of 3" |
| Distinct messages | `turnText` joins the words as before | the same tests in both packages |
| One next turn | every message opens a turn of its own (`fresh := true`) | thintest: "message 2: ... MessageIDs:[one id]" in every TestBatch |
| One at a time (send) | a message dispatches beside the running turn | thintest: "message 2" in every TestBatch |
| One at a time (end and recovery) | `dispatchNext` ignores a turn in flight | thintest: CrashBetweenTurns, the batch frame arrived during the held turn |
| Atomic end and next | `reply` commits when the next dispatch fails | thintest: CrashBetweenTurns "the reply was acked with the next turn refused" |
| Recovery | `sync` no longer calls `dispatchNext` | thintest: StrandedRecovers "no next turn" |
| Marker clearing | the line's holder may be a turn that ended | thintest: MarkerClearsTogether "the note stayed after the turn picked the messages up" (headless survives: its runner acks at once) |
| One note | every waiting bubble gets the note | thintest: MarkerClearsTogether; headless: "queued note not once under the latest message" |
| Frame fit (split) | a batch never fills up | thintest: SplitsToFitTheFrame "12 large messages made batches of [one]" |
| Frame fit (tail) | the tail is never trimmed | thintest: SplitsToFitTheFrame "message too big: read limited at 262145 bytes" |
| Tail excludes waiting batches | the tail takes every other turn's messages | thintest: SplitsToFitTheFrame "batch 1's tail holds a message still waiting" |
| Retryable close | a failed frame closes `CloseRevoked` again | headless: BatchAfterRefusedReply "runner stopped: runner credential refused" |

## Spend (fake CLI, input tokens are prompt length / 4)

| Case | Turns after the slow one | Input tokens |
|---|---|---|
| main, three messages during a slow turn | 1 | 102 |
| this branch, the same | 1 | 154 (+52 for the batch line and headers) |
| the same three words, each after the last reply | 3 | 2, 3, 3 |

The fake does not model the fixed context every real turn pays (about
17k input tokens on staging, 77% cached), so the third row understates
real spend: three turns pay that context three times, and a batch pays
it once. Usage stays per turn: one row for the batch turn and none for
any single message (`TestHeadlessTurnBatching`).

## Verification

- Targeted: the contracts above, `TestQueuedMessagesJoinOneTurn`,
  `TestCancel`, `TestMailboxResendAndGenerations`, `TestDrainAndRestart`,
  `TestSecondaryRunnerSyncIgnored`, `TestContractAttachments`,
  `TestContractStorageFullInWords`, the move-in contracts,
  `TestContractConversation`, `TestSendIdempotency`, `TestChatPage`,
  `TestReplacedConnectionFenced`, headless `6_quick_messages`.
- Browser, one at a time: `TestBrowserInstantThinking*`,
  `TestBrowserThinkingLinesClear`, `TestBrowserDraftStreamsFormatted`.
  All pass.
- `make budget`, vet, staticcheck on the changed packages, and
  `TestReleaseCopyReviewed` with `EQUIPO_RELEASE_COPY_CHECK=1` (no UI
  strings changed).
