From 171475905fddb4e6ec3e752a4db2e586f9abfb77 Mon Sep 17 00:00:00 2001 From: yetone Date: Wed, 18 Nov 2026 20:04:49 +0800 Subject: [PATCH] plugin: a reply's head that arrives with its completion is read, not dropped (#7017) headRead checked the queue, found it empty, and then found the call's result: a head the plugin queued between those two checks was lost, and the reply was read as headless ("the vendor ended early"). The result branch now takes a head queued meanwhile, as the c.done branch already did. body.Read is not affected: it loops back to the queue after taking the result. From recih's branch (68bdf17e6), ported as its own commit. Run: TestHostHeadArrivesWithCompletion passes on origin/main without help (the window is narrow); with a 2ms sleep injected between the pop and the result check it is red on origin/main ("completed reply's head = {...}, the vendor ended early") and green with this change. The full stack's run, on mote at e4506887c (this stack on origin/main 7300be4d0): go test -timeout 45m ./...: 101 packages ok, 2 FAIL, both open issues that fail the same at 7300be4d0 on mote, where an ssh session forwards 127.0.0.1:3425 to a real magpie: internal/cli's TestHealthcheckFindsTheRunningGatewayWithoutMagpieAddr, TestUpdateRestartsTheGatewayItFindsWithoutMagpieAddr and TestUpdateFindsAMagpieThatWroteNoRecord (#6223), internal/gui's TestLaneLimitRoutes (#5150). On mica: gofmt -l clean; go vet ./..., GOOS=linux go vet -tags nogui ./..., GOOS=windows go vet ./..., go build, GOOS=linux go build -tags nogui, GOOS=windows go build all ok. go test -race -count=2 of the new and touched tests in internal/gateway, gui, plugin, tui ok on mote. Rebased since. At dc507aefd on mote: go vet (darwin, GOOS=linux -tags nogui, windows) ok; go test of internal/cli, tui, plugin, gateway, gui, claudebridge ok but for the same #6223 and #5150 reds. At 939fe31b6 (on origin/main 78b6d47cc) on mote: gofmt -l clean, the three vets ok, the stack's tests with TestClaude* and TestCoreScriptBudget ok in all six packages. Pushed on b400566f9 after a clean rebase; the commits between touch no Go file this stack does. Co-authored-by: recih <240406+recih@users.noreply.github.com> --- docs/subsystems/provider-plugins.md | 2 + internal/plugin/head_completion_test.go | 57 +++++++++++++++++++++++++ internal/plugin/host.go | 6 +++ 3 files changed, 65 insertions(+) create mode 100644 internal/plugin/head_completion_test.go 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) }