diff --git a/docs/subsystems/provider-plugins.md b/docs/subsystems/provider-plugins.md index 3b07791f7..8fa7bac5a 100644 --- a/docs/subsystems/provider-plugins.md +++ b/docs/subsystems/provider-plugins.md @@ -339,6 +339,8 @@ Every other call but `init` is cancellable too (#4678): a models list, a usage r A plugin `fetch` has a bound before its head as well as between its body's bytes (`bodyIdleWait`, 30 minutes, in [`host.go`](../../internal/plugin/host.go); `headRead` for the head). A plugin whose vendor took the request and never answered (the Devin plugin returns its head only with Devin's first words; wu_chong4136 on X waited 10 to 20 minutes) is given up on with an `abort`, and the agent gets the gateway's retryable 502 (an SSE error on a stream) instead of keepalives for as long as the plugin waits. A plugin can bound its own vendor tighter: the Devin plugin gives up after 5 minutes of quiet with a 504 (0.1.45, the Devin mover's `min`). +A completed fetch still drains its queued response head before interpreting the terminal result (`headRead`). The head and completion may arrive between the reader's initial empty-queue check and its result check; that short reply must not become “the plugin answered no response”. The terminal result is carried into the body, preserving its bytes and any final error (`TestHostHeadArrivesWithCompletion`). + The GUI's plugin integration includes `subOf`, `pluginSubs`, and `startPluginSignIn`. A change to sign-in or provider presentation must follow these paths as well as the built-in paths it affects. ## Verification diff --git a/internal/plugin/head_completion_test.go b/internal/plugin/head_completion_test.go new file mode 100644 index 000000000..8ce2f8342 --- /dev/null +++ b/internal/plugin/head_completion_test.go @@ -0,0 +1,57 @@ +package plugin + +import ( + "context" + "encoding/base64" + "io" + "runtime" + "strings" + "testing" + "time" +) + +// A short reply can finish between the head reader's empty-queue check +// and its check for a completed call. The queued head still belongs to +// that reply, and its body and terminal error must reach the consumer. +func TestHostHeadArrivesWithCompletion(t *testing.T) { + for _, why := range []string{"", "the vendor ended early"} { + t.Run(why, func(t *testing.T) { + f := newFakeHost(t) + ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second) + defer cancel() + for range 128 { + id, c := f.begin(t, true) + type result struct { + head message + answer *message + err error + } + read := make(chan result, 1) + go func() { + h, a, err := c.headRead(ctx) + read <- result{h, a, err} + }() + runtime.Gosched() + c.push(message{Event: "head", Status: 200}) + c.push(message{Event: "chunk", Data: base64.StdEncoding.EncodeToString([]byte("reply"))}) + answer := message{} + if why != "" { + answer = errAnswer(why) + } + c.transition(callAnswered, nil, &answer) + r := <-read + if r.err != nil || r.head.Status != 200 { + c.release() + t.Fatalf("completed reply's head = %+v, %v", r.head, r.err) + } + b := readBody(f, id, c, ctx) + b.answer = r.answer + got, err := io.ReadAll(b) + b.Close() + if string(got) != "reply" || why == "" && err != nil || why != "" && (err == nil || !strings.Contains(err.Error(), why)) { + t.Fatalf("body = %q, %v; want reply, error %q", got, err, why) + } + } + }) + } +} diff --git a/internal/plugin/host.go b/internal/plugin/host.go index b6509b341..f18398252 100644 --- a/internal/plugin/host.go +++ b/internal/plugin/host.go @@ -2213,6 +2213,12 @@ func (c *call) headRead(ctx context.Context) (head message, answer *message, err return message{}, nil, err } if m := c.result(); m != nil { + // The head may have arrived with this answer after the empty + // queue check above. Drain it before interpreting the answer, + // just as when done wakes the reader below. + if hm, ok := c.pop(); ok { + return hm, m, nil + } if m.Error != nil { return message{}, m, errors.New(m.Error.Message) }