| 1 | # Turn batching: progress |
| 2 | |
| 3 | Branch `turn-batching` from main 03f8a91. Design note: |
| 4 | `docs/superpowers/specs/2026-11-11-equipo-turn-batching.md`. |
| 5 | |
| 6 | ## Done |
| 7 | |
| 8 | 1. **Finding.** The instance already batched messages sent during a turn |
| 9 | into one next turn (64cd2ce). The gap was that the messages were not |
| 10 | distinct: `Text` was the words joined by blank lines, and the files |
| 11 | were one list for the whole turn. |
| 12 | 2. **Change** (instance only, no wire, fixture or migration change): |
| 13 | `turnText` sets each message of a batch apart, with its files. `sync` |
| 14 | starts a stranded queued turn. |
| 15 | 3. **Failing first.** `TestHeadlessTurnBatching` (real runner, fake CLI, |
| 16 | thintest instance) failed on main for the input check. The output is |
| 17 | in commit fd22d42. |
| 18 | 4. **Contracts** (`internal/thintest/batching_contract_test.go`, protocol |
| 19 | 1 runner): `TestBatchCrashBetweenTurns`, `TestBatchStrandedRecovers`, |
| 20 | `TestBatchAfterTurnEnds` (failed, cancelled, interrupted), |
| 21 | `TestBatchWaitsForRetriedTurn`, `TestBatchCancelledWhileWaiting`, |
| 22 | `TestBatchMarkerClearsTogether`, `TestBatchSplitsToFitTheFrame`, and |
| 23 | headless `TestHeadlessBatchAfterRefusedReply` (real runner). |
| 24 | 5. **Astra round 1** (gpt-6-astra, high): REQUEST CHANGES, three |
| 25 | Important findings. |
| 26 | - Oversized batches could exceed the 256 KiB frame, and the runner |
| 27 | would refuse them on every resend. Fixed: batches split to fit, the |
| 28 | tail is trimmed to fit, and a waiting batch is kept out of an |
| 29 | earlier batch's tail. |
| 30 | - A store error on a runner frame closed with `CloseRevoked`, which |
| 31 | stops a real runner. Fixed: the close is now 1011 (retryable); |
| 32 | `ErrForbidden` still closes `CloseRevoked`. |
| 33 | - Queued marker on each bubble: this is a product question, left for |
| 34 | the controller. The branch keeps the single note from the |
| 35 | thinking-stale fix. |
| 36 | 6. **Rulings.** No hold between turns, and the single note stays. |
| 37 | `chat.failed_many` ("The coordinator could not answer these |
| 38 | messages.") is added for a failed batch, on the page and from the |
| 39 | event stream. Delegated Astra copy pass: KEEP, marked COPY ACCEPTED BY |
| 40 | DELEGATION (Astra, 2026-11-11) in copy.go and the Plan 3 inventory. |
| 41 | Mutations: drop the page's batch case and `TestBatchFailedSaysThemAll` |
| 42 | dies ("a failed batch lacks the note for its messages"); drop the |
| 43 | script's and `TestBrowserInstantThinkingQueued` dies ("waiting for the |
| 44 | failed batch's note"). |
| 45 | 7. **Merged origin/main** 42a31cb (ship buttons). The budgets conflict |
| 46 | was resolved by keeping both reasons (internal/thintest at 13600, then 13630 with the plural note). |
| 47 | |
| 48 | ## Mutations |
| 49 | |
| 50 | Each guard was broken once, with the targeted runs below, then restored. |
| 51 | Command: `CGO_ENABLED=0 nice -n 10 go test ./internal/thintest -run |
| 52 | '^(TestBatch|TestQueuedMessagesJoinOneTurn$)'` and, where shown, `go test |
| 53 | ./internal/headless -run '^TestHeadlessTurnBatching$'`. |
| 54 | |
| 55 | | Guard | Mutation | Died | |
| 56 | |---|---|---| |
| 57 | | 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" | |
| 58 | | Distinct messages | `turnText` joins the words as before | the same tests in both packages | |
| 59 | | One next turn | every message opens a turn of its own (`fresh := true`) | thintest: "message 2: ... MessageIDs:[one id]" in every TestBatch | |
| 60 | | One at a time (send) | a message dispatches beside the running turn | thintest: "message 2" in every TestBatch | |
| 61 | | One at a time (end and recovery) | `dispatchNext` ignores a turn in flight | thintest: CrashBetweenTurns, the batch frame arrived during the held turn | |
| 62 | | Atomic end and next | `reply` commits when the next dispatch fails | thintest: CrashBetweenTurns "the reply was acked with the next turn refused" | |
| 63 | | Recovery | `sync` no longer calls `dispatchNext` | thintest: StrandedRecovers "no next turn" | |
| 64 | | 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) | |
| 65 | | One note | every waiting bubble gets the note | thintest: MarkerClearsTogether; headless: "queued note not once under the latest message" | |
| 66 | | Frame fit (split) | a batch never fills up | thintest: SplitsToFitTheFrame "12 large messages made batches of [one]" | |
| 67 | | Frame fit (tail) | the tail is never trimmed | thintest: SplitsToFitTheFrame "message too big: read limited at 262145 bytes" | |
| 68 | | Tail excludes waiting batches | the tail takes every other turn's messages | thintest: SplitsToFitTheFrame "batch 1's tail holds a message still waiting" | |
| 69 | | Retryable close | a failed frame closes `CloseRevoked` again | headless: BatchAfterRefusedReply "runner stopped: runner credential refused" | |
| 70 | |
| 71 | ## Spend (fake CLI, input tokens are prompt length / 4) |
| 72 | |
| 73 | | Case | Turns after the slow one | Input tokens | |
| 74 | |---|---|---| |
| 75 | | main, three messages during a slow turn | 1 | 102 | |
| 76 | | this branch, the same | 1 | 154 (+52 for the batch line and headers) | |
| 77 | | the same three words, each after the last reply | 3 | 2, 3, 3 | |
| 78 | |
| 79 | The fake does not model the fixed context every real turn pays (about |
| 80 | 17k input tokens on staging, 77% cached), so the third row understates |
| 81 | real spend: three turns pay that context three times, and a batch pays |
| 82 | it once. Usage stays per turn: one row for the batch turn and none for |
| 83 | any single message (`TestHeadlessTurnBatching`). |
| 84 | |
| 85 | ## Verification |
| 86 | |
| 87 | - Targeted: the contracts above, `TestQueuedMessagesJoinOneTurn`, |
| 88 | `TestCancel`, `TestMailboxResendAndGenerations`, `TestDrainAndRestart`, |
| 89 | `TestSecondaryRunnerSyncIgnored`, `TestContractAttachments`, |
| 90 | `TestContractStorageFullInWords`, the move-in contracts, |
| 91 | `TestContractConversation`, `TestSendIdempotency`, `TestChatPage`, |
| 92 | `TestReplacedConnectionFenced`, headless `6_quick_messages`. |
| 93 | - Browser, one at a time: `TestBrowserInstantThinking*`, |
| 94 | `TestBrowserThinkingLinesClear`, `TestBrowserDraftStreamsFormatted`. |
| 95 | All pass. |
| 96 | - `make budget`, vet, staticcheck on the changed packages, and |
| 97 | `TestReleaseCopyReviewed` with `EQUIPO_RELEASE_COPY_CHECK=1` (no UI |
| 98 | strings changed). |
| 99 | |