plugin: a fetch whose head never comes is given up on; Devin min 0.1.45 (wu_chong4136 on X)

wu_chong4136 on X: with the Devin plugin a session sometimes hung for
10 to 20 minutes with no reply, until the effort or the context was
changed. The Devin plugin returns its fetch's head only once Devin sends
the reply's first content, and the host's headRead waited for that head
with no bound, while the gateway kept the agent alive on keepalives. A
request Devin took and never answered therefore hung silently. The body
already had bodyIdleWait (30m between bytes, #4934); the head had none.

- host.go headRead: the same bodyIdleWait bounds the wait for the head.
  On it the fetch ends with "the plugin sent no reply in 30m0s; that
  fetch was given up on", Fetch settles the call, and the host writes
  "abort", so the plugin's fetch signal ends its vendor request too. The
  gateway answers the agent its retryable 502 (an SSE error event on a
  stream). This holds for every plugin fetch, not only Devin's.
- ReplyWaitForTest lets the gateway test shorten the wait.
- Devin mover min 0.1.45 (on npm, `npm view --prefer-online`): the
  plugin itself gives up after 5 minutes of quiet from Devin (no head,
  or no frame between reads) with a retryable 504. A real probe saw
  Devin's first frame at 2.5s and at most 13s between frames.
  (magpie-community/plugins e30f8ed0)
- provider-plugins.md says so.

Tests:
- TestHostHeadWaitEndsAFetchNeverAnswered: a head that never comes ends
  in "sent no reply", the host is sent "abort", and the call is
  released; TestHostHeadWaitSparesAHeadInTime: a head in time is kept.
- TestPluginHeadNeverSentEndsInAnError (gateway, through the fake
  plugin in Bun): a vendor that reads the request and never answers
  gives the agent the error on a stream and on a whole reply, and every
  vendor request the plugin made is closed afterwards (asked 30, open 0).

Run:
- mote: the three tests above, ok.
- Broken by hand (headRead without the timer case): the host test fails
  ("held its reader for ever") and the gateway test hangs to its 60s
  timeout in io.ReadAll.
- mica: gofmt -l clean; go vet for darwin, GOOS=linux and GOOS=windows
  on internal/plugin, internal/gateway, internal/provider: ok.
- mica: go test -tags nogui -timeout 45m ./... at this tree (rebased on
  ac4a4cf84): 101 packages ok; internal/gateway FAIL on
  TestImageMCPKeepsDrawingsOffTheHeap alone ("the heap grew by 31 MB"),
  which passes 3 of 3 alone at the same tree and reads the whole
  process heap; filed as #6890.

Not tried: a real Devin request that goes quiet (it can't be made to
happen on demand).
This commit is contained in:
yetone
2026-11-17 00:39:50 +08:00
parent ac4a4cf849
commit 1c59acd36d
5 changed files with 145 additions and 1 deletions
+2
View File
@@ -335,6 +335,8 @@ A plugin sign-in magpie gives up on ends in the host too (#5016). The user's Can
Every other call but `init` is cancellable too (#4678): a models list, a usage read or a check magpie stops waiting for (its caller left, or `listWait` ran out) gets an `abort`, and `host.js` aborts the call's signal, which the usage hook gets as `usage(..., { signal })` and the models hook as `signal`. The call stays pending in the host until its handler ends, and nothing it answers after the abort is sent, so a hook still saving a refreshed sign-in finishes before the host may exit. `TestCancelledCallKeepsTheHostTillItsHookEnds` and the other tests in [`plugin_rpc_cancel_test.go`](../../internal/plugin/plugin_rpc_cancel_test.go).
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`).
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
+66
View File
@@ -0,0 +1,66 @@
package gateway
import (
"context"
"io"
"net/http"
"strings"
"sync/atomic"
"testing"
"time"
"github.com/yetone/magpie/internal/plugin"
)
// A plugin whose vendor took the request and never answered it (the Devin
// plugin answers its head only once Devin sends the reply's first words)
// left the agent on keepalives with no error for as long as it waited
// (wu_chong4136 on X: 10 to 20 minutes). The plugin's fetch now gives the
// head up after the reply wait, and the agent gets an error it can retry,
// on a stream and on a whole reply. Before, both requests held until the
// client's own deadline.
func TestPluginHeadNeverSentEndsInAnError(t *testing.T) {
t.Cleanup(plugin.ReplyWaitForTest(300 * time.Millisecond))
var asked, open atomic.Int64
up := http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
asked.Add(1)
open.Add(1)
defer open.Add(-1)
// read whole, so the server watches the connection and sees the
// host's abort close it
_, _ = io.Copy(io.Discard, r.Body)
<-r.Context().Done() // took the request, never answers
})
url, model, client := pluginReplyGateway(t, up)
for _, stream := range []bool{true, false} {
ctx, cancel := context.WithTimeout(context.Background(), 2*time.Minute)
req, _ := http.NewRequestWithContext(ctx, http.MethodPost, url+"/v1/chat/completions", strings.NewReader(pluginReplyRequest(model, stream)))
req.Header.Set("Content-Type", "application/json")
res, err := client.Do(req)
if err != nil {
cancel()
t.Fatalf("stream=%t: %v", stream, err)
}
b, err := io.ReadAll(res.Body)
res.Body.Close()
cancel()
if err != nil {
t.Fatalf("stream=%t: the reply was held to the client's deadline: %v", stream, err)
}
if !strings.Contains(string(b), "sent no reply") {
t.Fatalf("stream=%t: status %d, body %q, want the give-up named", stream, res.StatusCode, b)
}
if res.StatusCode == http.StatusOK && !strings.Contains(string(b), "error") {
t.Fatalf("stream=%t: a 200 with no error: %q", stream, b)
}
t.Logf("stream=%t: %d %s", stream, res.StatusCode, b)
}
deadline := time.Now().Add(10 * time.Second)
for open.Load() > 0 && time.Now().Before(deadline) {
time.Sleep(10 * time.Millisecond)
}
t.Logf("asked %d, open %d", asked.Load(), open.Load())
if n := open.Load(); n > 0 {
t.Fatalf("%d of %d vendor requests still open after the fetch was given up on", n, asked.Load())
}
}
+21
View File
@@ -2183,9 +2183,28 @@ var errBodyClosed = errors.New("the reply's body was closed")
// is not cut; a dead connection's read ends at the same bound.
var bodyIdleWait = 30 * time.Minute
// ReplyWaitForTest sets bodyIdleWait, the head's and the body's wait, to d
// until restore.
func ReplyWaitForTest(d time.Duration) (restore func()) {
old := bodyIdleWait
bodyIdleWait = d
return func() { bodyIdleWait = old }
}
// headRead waits for the call's reply head, giving back the answer it read on
// the way: the body has to end with that answer too, error and all.
//
// The head is waited on for bodyIdleWait at most, as the body's silence
// is: a plugin may answer only once its vendor has sent the reply's first
// words (the Devin plugin does), so a request its vendor took and never
// answered left the head unsent, and the gateway kept the agent alive
// with keepalives and nothing else, for as long as the agent waited
// (wu_chong4136 on X: 10 to 20 minutes, until the effort or the
// conversation changed). Given up, the fetch fails with an error that
// names the wait, which the next account or the agent's retry takes up.
func (c *call) headRead(ctx context.Context) (head message, answer *message, err error) {
idle := time.NewTimer(bodyIdleWait)
defer idle.Stop()
for {
if m, ok := c.pop(); ok {
return m, nil, nil
@@ -2216,6 +2235,8 @@ func (c *call) headRead(ctx context.Context) (head message, answer *message, err
// the host is gone: no head is coming, so the fetch gives up
// rather than waiting on a context that may never end
continue
case <-idle.C:
return message{}, nil, fmt.Errorf("the plugin sent no reply in %s; that fetch was given up on", bodyIdleWait)
}
}
}
+55
View File
@@ -1098,6 +1098,61 @@ func TestHostBodyIdleEndsASilentStream(t *testing.T) {
waitFor(t, "the silent call was given up on", func() bool { return !f.registered(id) })
}
// TestHostHeadWaitEndsAFetchNeverAnswered: a plugin that answers only once
// its vendor sends the reply's first words (the Devin plugin) leaves the
// head unsent while a vendor that took the request says nothing, and the
// gateway kept the agent waiting on keepalives for as long as it would wait
// (wu_chong4136 on X: 10 to 20 minutes). The head is waited on for
// bodyIdleWait, as the body's silence is; then the fetch fails with why and
// the plugin's fetch is aborted, as Fetch settles a failed head read. Before,
// the head read held until the client cancelled it.
func TestHostHeadWaitEndsAFetchNeverAnswered(t *testing.T) {
f := newFakeHost(t)
oldWait := bodyIdleWait
bodyIdleWait = 150 * time.Millisecond
t.Cleanup(func() { bodyIdleWait = oldWait })
id, c := f.begin(t, true)
errs := make(chan error, 1)
go func() {
_, _, err := c.headRead(context.Background())
if err != nil {
c.settle(err) // as Fetch does
}
errs <- err
}()
select {
case err := <-errs:
if err == nil || !strings.Contains(err.Error(), "sent no reply") {
t.Fatalf("a head never sent = %v, want the give-up", err)
}
case <-time.After(5 * time.Second):
t.Fatal("a fetch whose head never came held its reader for ever")
}
waitFor(t, "the plugin's fetch was aborted", func() bool {
for _, m := range f.methods() {
if m == "abort" {
return true
}
}
return false
})
waitFor(t, "the call left the host", func() bool { return !f.registered(id) })
}
// TestHostHeadWaitSparesAHeadInTime: a head that comes inside the wait is
// read as before, and the body after it is the body's own wait.
func TestHostHeadWaitSparesAHeadInTime(t *testing.T) {
f := newFakeHost(t)
oldWait := bodyIdleWait
bodyIdleWait = 5 * time.Second
t.Cleanup(func() { bodyIdleWait = oldWait })
id, c := f.begin(t, true)
f.head(id)
if _, _, err := c.headRead(context.Background()); err != nil {
t.Fatalf("a head in time = %v", err)
}
}
// TestHostBodyIdleSparesAFedStream: the idle wait is between bytes, not the
// reply's whole length — a stream a chunk comes to every few waits goes on
// however long it talks, and ends at its answer as before.
File diff suppressed because one or more lines are too long