| 1 | package pi |
| 2 | |
| 3 | import ( |
| 4 | "context" |
| 5 | "encoding/json" |
| 6 | "errors" |
| 7 | "io" |
| 8 | "strings" |
| 9 | "testing" |
| 10 | "time" |
| 11 | |
| 12 | "amadan.net/oficina/equipo/internal/runner/provider" |
| 13 | ) |
| 14 | |
| 15 | // A Pi that stops reading its stdin cannot hold the runner: a write |
| 16 | // gives up when its context ends, a command too, and other writes wait |
| 17 | // their turn without blocking the client. |
| 18 | func TestClientWritesAreCancellable(t *testing.T) { |
| 19 | stdinR, stdinW := io.Pipe() // nobody reads stdinR |
| 20 | defer stdinR.Close() |
| 21 | stdoutR, stdoutW := io.Pipe() |
| 22 | defer stdoutW.Close() |
| 23 | c := NewClient(stdoutR, stdinW) |
| 24 | for _, call := range []func(context.Context) error{ |
| 25 | func(ctx context.Context) error { return c.Send(ctx, map[string]string{"type": "abort"}) }, |
| 26 | func(ctx context.Context) error { _, err := c.Call(ctx, map[string]any{"type": "abort"}); return err }, |
| 27 | } { |
| 28 | ctx, cancel := context.WithTimeout(context.Background(), 100*time.Millisecond) |
| 29 | start := time.Now() |
| 30 | err := call(ctx) |
| 31 | cancel() |
| 32 | if !errors.Is(err, context.DeadlineExceeded) || time.Since(start) > 2*time.Second { |
| 33 | t.Fatalf("a write to a stdin nobody reads: %v after %v", err, time.Since(start)) |
| 34 | } |
| 35 | } |
| 36 | } |
| 37 | |
| 38 | // stuckProcess is a Pi whose stdin nobody reads; its stdout answers |
| 39 | // get_state, then says nothing more. |
| 40 | type stuckProcess struct { |
| 41 | stdinR *io.PipeReader |
| 42 | stdinW *io.PipeWriter |
| 43 | stdoutR *io.PipeReader |
| 44 | stdoutW *io.PipeWriter |
| 45 | } |
| 46 | |
| 47 | func (p *stuckProcess) Stdin() io.WriteCloser { return p.stdinW } |
| 48 | func (p *stuckProcess) Stdout() io.ReadCloser { return p.stdoutR } |
| 49 | func (p *stuckProcess) Stderr() io.ReadCloser { return io.NopCloser(strings.NewReader("")) } |
| 50 | func (p *stuckProcess) ID() string { return "stuck" } |
| 51 | func (p *stuckProcess) Wait(ctx context.Context) (provider.Exit, error) { |
| 52 | <-ctx.Done() |
| 53 | return provider.Exit{}, ctx.Err() |
| 54 | } |
| 55 | |
| 56 | type stuckHost struct{ p *stuckProcess } |
| 57 | |
| 58 | func (h stuckHost) Start(ctx context.Context, l provider.Launch, argv []string) (provider.Process, error) { |
| 59 | go func() { |
| 60 | // Read get_state, answer it, then stop reading for good. |
| 61 | buf := make([]byte, 4096) |
| 62 | n, _ := h.p.stdinR.Read(buf) |
| 63 | var cmd struct{ ID string } |
| 64 | json.Unmarshal(buf[:n], &cmd) |
| 65 | raw, _ := json.Marshal(map[string]any{"type": "response", "id": cmd.ID, "command": "get_state", "success": true, "data": map[string]any{"sessionId": "01234567-89ab", "model": map[string]string{"id": l.Selection.Model, "provider": l.Selection.Provider}}}) |
| 66 | h.p.stdoutW.Write(append(raw, '\n')) |
| 67 | // And a question it waits on, so an interrupt has a dialog to |
| 68 | // cancel first. |
| 69 | h.p.stdoutW.Write([]byte(`{"type":"extension_ui_request","id":"ui-1","method":"confirm","title":"equipo:tool_call","message":"{}"}` + "\n")) |
| 70 | }() |
| 71 | return h.p, nil |
| 72 | } |
| 73 | |
| 74 | func TestLauncherInterruptIsBounded(t *testing.T) { |
| 75 | p := &stuckProcess{} |
| 76 | p.stdinR, p.stdinW = io.Pipe() |
| 77 | p.stdoutR, p.stdoutW = io.Pipe() |
| 78 | defer p.stdoutW.Close() |
| 79 | defer p.stdinR.Close() |
| 80 | l, _ := New(stuckHost{p}) |
| 81 | sess, err := l.openWith(context.Background(), context.Background(), provider.Launch{Selection: provider.Selection{Provider: "tensorx", Model: "m"}}, []string{"/opt/node"}) |
| 82 | if err != nil { |
| 83 | t.Fatal(err) |
| 84 | } |
| 85 | events, _ := l.Events(context.Background(), sess) |
| 86 | if e := <-events; e.Kind != KindAsk { |
| 87 | t.Fatalf("event %s", e.Kind) |
| 88 | } |
| 89 | ctx, cancel := context.WithTimeout(context.Background(), 200*time.Millisecond) |
| 90 | defer cancel() |
| 91 | start := time.Now() |
| 92 | if err := l.Interrupt(ctx, sess); err == nil || time.Since(start) > 2*time.Second { |
| 93 | t.Fatalf("an interrupt Pi never reads: %v after %v", err, time.Since(start)) |
| 94 | } |
| 95 | ctx2, cancel2 := context.WithTimeout(context.Background(), 200*time.Millisecond) |
| 96 | defer cancel2() |
| 97 | if err := l.StartTurn(ctx2, sess, provider.Turn{ID: "t", Prompt: "x"}); err == nil { |
| 98 | t.Fatal("a prompt Pi never reads went through") |
| 99 | } |
| 100 | } |
| 101 | |
| 102 | // What Pi sends is held to a budget: records wait in the client only up |
| 103 | // to its bytes, Pi waits for the rest (it honours stdout backpressure), |
| 104 | // and a record over the size limit ends the stream. |
| 105 | func TestClientQueueIsBounded(t *testing.T) { |
| 106 | stdoutR, stdoutW := io.Pipe() |
| 107 | defer stdoutW.Close() |
| 108 | c := newClientLimits(stdoutR, io.Discard, 1000, 300) |
| 109 | line := strings.Repeat("x", 90) |
| 110 | go func() { |
| 111 | for range 20 { |
| 112 | if _, err := stdoutW.Write([]byte(`{"type":"` + line + `"}` + "\n")); err != nil { |
| 113 | return |
| 114 | } |
| 115 | } |
| 116 | }() |
| 117 | time.Sleep(300 * time.Millisecond) |
| 118 | if q := c.queuedBytes(); q > 300 || len(c.Records) > 3 { |
| 119 | t.Fatalf("queued %d bytes in %d records with a budget of 300", q, len(c.Records)) |
| 120 | } |
| 121 | // Taking one lets one more in. |
| 122 | got := <-c.Records |
| 123 | c.Release(len(got)) |
| 124 | time.Sleep(100 * time.Millisecond) |
| 125 | if q := c.queuedBytes(); q > 300 || q == 0 { |
| 126 | t.Fatalf("queued %d", q) |
| 127 | } |
| 128 | |
| 129 | big := newClientLimits(strings.NewReader(`{"type":"`+strings.Repeat("y", 2000)+`"}`+"\n"+`{"type":"after"}`+"\n"), io.Discard, 1000, 300) |
| 130 | n := 0 |
| 131 | for range big.Records { |
| 132 | n++ |
| 133 | } |
| 134 | if n != 0 { |
| 135 | t.Fatalf("an oversized record did not end the stream: %d records after it", n) |
| 136 | } |
| 137 | } |
| 138 | |
| 139 | // A tool call's arguments in an event are clipped; the call itself is |
| 140 | // Pi's to run. |
| 141 | func TestDecodeClipsToolArguments(t *testing.T) { |
| 142 | d := &Decoder{} |
| 143 | evs, err := d.Decode([]byte(`{"type":"tool_execution_start","toolCallId":"c","toolName":"write","args":{"content":"` + strings.Repeat("z", 200000) + `"}}`)) |
| 144 | if err != nil || len(evs) != 1 { |
| 145 | t.Fatal(err) |
| 146 | } |
| 147 | if len(evs[0].Payload) > maxEventArgs+1024 { |
| 148 | t.Fatalf("payload of %d bytes", len(evs[0].Payload)) |
| 149 | } |
| 150 | } |
| 151 | |