|
1 | 1 | import { describe, expect, test } from "bun:test" |
2 | | -import { AIError, HttpContext, InvalidRequestError, TransportError } from "@opencode/ai" |
| 2 | +import { AIError, HttpContext, InvalidProviderOutputError, InvalidRequestError, TransportError } from "@opencode/ai" |
3 | 3 | import type { |
4 | 4 | ChannelObservation, |
5 | 5 | WebSocketChannelExchange, |
@@ -634,6 +634,50 @@ describe("SessionModelTransport", () => { |
634 | 634 | ) |
635 | 635 | }) |
636 | 636 |
|
| 637 | + test("keeps the Session on HTTP after repeated idle timeouts", async () => { |
| 638 | + const sent = queue<void>() |
| 639 | + let opens = 0 |
| 640 | + const connector: WebSocketConnector = { |
| 641 | + open: () => |
| 642 | + Effect.gen(function* () { |
| 643 | + opens++ |
| 644 | + const messages = yield* Queue.unbounded<string | Uint8Array, AIError>() |
| 645 | + return { |
| 646 | + sendText: () => |
| 647 | + Queue.offer(sent, undefined).pipe( |
| 648 | + Effect.andThen(opens > 5 ? Queue.offer(messages, "completed") : Effect.void), |
| 649 | + Effect.asVoid, |
| 650 | + ), |
| 651 | + messages: Stream.fromQueue(messages), |
| 652 | + close: Queue.shutdown(messages).pipe(Effect.asVoid), |
| 653 | + } |
| 654 | + }), |
| 655 | + } |
| 656 | + |
| 657 | + await runWithTestClock( |
| 658 | + connector, |
| 659 | + Effect.gen(function* () { |
| 660 | + const transport = yield* SessionModelTransport.Service |
| 661 | + const executor = transport.bind(session, undefined, 1_000) |
| 662 | + for (let attempt = 0; attempt < 5; attempt++) { |
| 663 | + const running = yield* collect(executor, exchange(`attempt-${attempt}`)).pipe( |
| 664 | + Effect.result, |
| 665 | + Effect.forkChild({ startImmediately: true }), |
| 666 | + ) |
| 667 | + yield* Queue.take(sent) |
| 668 | + yield* TestClock.adjust("1 second") |
| 669 | + expect(yield* Fiber.join(running)).toMatchObject({ |
| 670 | + _tag: "Failure", |
| 671 | + failure: { reason: { _tag: "Transport", code: "idle-timeout" } }, |
| 672 | + }) |
| 673 | + } |
| 674 | + |
| 675 | + expect(yield* collect(executor, exchange("sixth"))).toEqual(["fallback:sixth"]) |
| 676 | + expect(opens).toBe(5) |
| 677 | + }), |
| 678 | + ) |
| 679 | + }) |
| 680 | + |
637 | 681 | test("closes a newly opened connection when request creation is interrupted", async () => { |
638 | 682 | const opened = Deferred.makeUnsafe<void>() |
639 | 683 | const messages = queue<string | Uint8Array, AIError>() |
@@ -765,6 +809,72 @@ describe("SessionModelTransport", () => { |
765 | 809 | ) |
766 | 810 | }) |
767 | 811 |
|
| 812 | + test("keeps the Session on HTTP after repeated response parser failures", async () => { |
| 813 | + const fixture = automatic() |
| 814 | + const item = (id: string): WebSocketChannelExchange => ({ |
| 815 | + ...exchange(id), |
| 816 | + driver: { |
| 817 | + create: () => Effect.succeed({ message: id, mode: "full" }), |
| 818 | + observe: () => |
| 819 | + Effect.fail(new AIError({ reason: new InvalidProviderOutputError({ message: "Invalid response frame" }) })), |
| 820 | + }, |
| 821 | + }) |
| 822 | + |
| 823 | + await run( |
| 824 | + fixture.connector, |
| 825 | + Effect.gen(function* () { |
| 826 | + const transport = yield* SessionModelTransport.Service |
| 827 | + const executor = transport.bind(session) |
| 828 | + for (let attempt = 0; attempt < 5; attempt++) { |
| 829 | + const result = yield* Effect.result(collect(executor, item(`attempt-${attempt}`))) |
| 830 | + expect(result._tag).toBe("Failure") |
| 831 | + } |
| 832 | + expect(fixture.connections).toHaveLength(5) |
| 833 | + expect(yield* collect(executor, exchange("sixth"))).toEqual(["fallback:sixth"]) |
| 834 | + expect(fixture.connections).toHaveLength(5) |
| 835 | + }), |
| 836 | + ) |
| 837 | + }) |
| 838 | + |
| 839 | + test("does not count interrupted exchanges as stream failures", async () => { |
| 840 | + const sent = queue<string>() |
| 841 | + let opens = 0 |
| 842 | + const connector: WebSocketConnector = { |
| 843 | + open: () => |
| 844 | + Effect.gen(function* () { |
| 845 | + opens++ |
| 846 | + const messages = yield* Queue.unbounded<string | Uint8Array, AIError>() |
| 847 | + return { |
| 848 | + sendText: (message) => |
| 849 | + Queue.offer(sent, message).pipe( |
| 850 | + Effect.andThen(message === "sixth" ? Queue.offer(messages, `completed:${message}`) : Effect.void), |
| 851 | + Effect.asVoid, |
| 852 | + ), |
| 853 | + messages: Stream.fromQueue(messages), |
| 854 | + close: Queue.shutdown(messages).pipe(Effect.asVoid), |
| 855 | + } |
| 856 | + }), |
| 857 | + } |
| 858 | + |
| 859 | + await run( |
| 860 | + connector, |
| 861 | + Effect.gen(function* () { |
| 862 | + const transport = yield* SessionModelTransport.Service |
| 863 | + const executor = transport.bind(session) |
| 864 | + for (let attempt = 0; attempt < 5; attempt++) { |
| 865 | + const running = yield* collect(executor, exchange(`attempt-${attempt}`)).pipe( |
| 866 | + Effect.forkChild({ startImmediately: true }), |
| 867 | + ) |
| 868 | + expect(yield* Queue.take(sent)).toBe(`attempt-${attempt}`) |
| 869 | + yield* Fiber.interrupt(running) |
| 870 | + } |
| 871 | + |
| 872 | + expect(yield* collect(executor, exchange("sixth"))).toEqual(["completed:sixth"]) |
| 873 | + expect(opens).toBe(6) |
| 874 | + }), |
| 875 | + ) |
| 876 | + }) |
| 877 | + |
768 | 878 | test("falls back to HTTP after close code 1009 and keeps the Session on HTTP", async () => { |
769 | 879 | const messages = queue<string | Uint8Array, AIError>() |
770 | 880 | let opened = 0 |
|
0 commit comments