| 75 | return msgs; |
| 76 | } |
| 77 | |
| 78 | function assertInvariants(out: Message[]): void { |
| 79 | // No two consecutive user messages (strict providers reject this). |
| 80 | for (let i = 1; i < out.length; i++) { |
| 81 | expect(out[i]!.role === 'user' && out[i - 1]!.role === 'user').toBe(false); |
| 82 | } |
| 83 | // No orphaned tool result — each must follow an assistant or another tool. |
| 84 | for (let i = 0; i < out.length; i++) { |
| 85 | if (out[i]!.role === 'tool') { |
| 86 | expect(i > 0 && (out[i - 1]!.role === 'assistant' || out[i - 1]!.role === 'tool')).toBe(true); |
| 87 | } |
| 88 | } |
| 89 | } |
| 90 | |
| 91 | it('returns unchanged when under budget', () => { |