gateway: a plugin's Gemini model is never asked ending with the model's turn (plugins#734)
A test, no code change. plugins#734 says the plugin path (gcp-ai, @ai-sdk/google) skips the gateway's Gemini history rules. It doesn't: a plugin's @ai-sdk/google model is spoken to on CodeAssist (pluginProtocol), built by buildCodeAssistSent and unwrapped by pluginFetch, so the trailing-model user turn and the functionResponse/text split reach it. TestPluginGeminiNeverAskedEndingWithModel holds that end to end through the fake plugin with FAKE-style @ai-sdk/google model fake-gemini and a vendor answering as Vertex does (vertexTurnsAway): Codex compaction after the model's reply, a Codex turn ending with the assistant's message, and #1533's request on Codex's path and /v1/responses, each asked once and never refused. Run on mote (mote-run): go vet ./internal/gateway; go test -run 'EndingWithModel|NeverEndWithModel|ToolResponseTurnHoldsNoText|LocalModelIDAtAPlugin' ./internal/gateway ok. Broken by hand: with the trailing-model guard in buildCodeAssistSent turned off the test fails (vendor refused, a retry got past it); with the functionResponse/text split turned off it fails on both #1533 cases. Not run: the full suite (test-only change in one package). The plugin side got the same rules as defense in depth in magpie-community/plugins 5112465c (gcp-ai 0.2.1).
This commit is contained in:
@@ -0,0 +1,111 @@
|
||||
package gateway
|
||||
|
||||
import (
|
||||
"context"
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"io"
|
||||
"net/http"
|
||||
"net/http/httptest"
|
||||
"path/filepath"
|
||||
"strings"
|
||||
"sync"
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
"github.com/yetone/magpie/internal/plugin"
|
||||
"github.com/yetone/magpie/internal/testenv"
|
||||
)
|
||||
|
||||
// A plugin's Gemini model (@ai-sdk/google, the GCP AI plugin's
|
||||
// gemini-4-argon, plugins#734) is asked through the plugin's fetch with the
|
||||
// contents the gateway's Code Assist build makes, so Codex's compaction on
|
||||
// it never reaches the vendor ending with the model's turn, nor with a
|
||||
// tool's response and the compaction prompt in one user turn: Vertex AI
|
||||
// answers either with 400 "Requests ending with a model turn are not
|
||||
// supported" (vertexTurnsAway).
|
||||
func TestPluginGeminiNeverAskedEndingWithModel(t *testing.T) {
|
||||
bun := testenv.Bun(t)
|
||||
fresh(t)
|
||||
t.Setenv("MAGPIE_BUN", bun)
|
||||
t.Cleanup(plugin.Settle)
|
||||
|
||||
var mu sync.Mutex
|
||||
var bodies []map[string]any
|
||||
refused := 0
|
||||
testenv.FakeVendor(t, http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||
b, _ := io.ReadAll(r.Body)
|
||||
var q map[string]any
|
||||
json.Unmarshal(b, &q)
|
||||
mu.Lock()
|
||||
bodies = append(bodies, q)
|
||||
mu.Unlock()
|
||||
if code, out := vertexTurnsAway(0, q); code != http.StatusOK {
|
||||
mu.Lock()
|
||||
refused++
|
||||
mu.Unlock()
|
||||
w.Header().Set("Content-Type", "application/json")
|
||||
w.WriteHeader(code)
|
||||
io.WriteString(w, out)
|
||||
return
|
||||
}
|
||||
w.Header().Set("Content-Type", "text/event-stream")
|
||||
fmt.Fprint(w, `data: {"candidates":[{"content":{"role":"model","parts":[{"text":"summary"}]},"finishReason":"STOP"}],"usageMetadata":{"promptTokenCount":1,"candidatesTokenCount":1}}`+"\n\n")
|
||||
}))
|
||||
ctx, cancel := context.WithTimeout(context.Background(), time.Minute)
|
||||
defer cancel()
|
||||
abs, _ := filepath.Abs("../plugin/testdata/fake/index.js")
|
||||
if _, err := plugin.Add(ctx, abs); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if _, err := plugin.Providers(ctx); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if _, err := plugin.APIKey(ctx, "fakeco", 0, nil, "k1", plugin.NewAccount); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
const model = `"model":"fakeco/fake-gemini"`
|
||||
const history = `{"type":"message","role":"user","content":[{"type":"input_text","text":"fix the bug"}]},` +
|
||||
`{"type":"function_call","name":"shell","arguments":"{\"command\":[\"ls\"]}","call_id":"call_1"},` +
|
||||
`{"type":"function_call_output","call_id":"call_1","output":"main.go"},` +
|
||||
`{"type":"message","role":"assistant","content":[{"type":"output_text","text":"Fixed it in main.go."}]}`
|
||||
s := New()
|
||||
for i, c := range []struct {
|
||||
name, path, body, want string
|
||||
}{
|
||||
{"codex compaction after the model's reply", CodexPath + "/responses",
|
||||
`{` + model + `,"instructions":"You are Codex.","stream":true,"tools":[{"type":"function","name":"shell","parameters":{"type":"object","properties":{}}}],"input":[` + history + `,{"type":"compaction_trigger"}]}`,
|
||||
codexCompactPrompt},
|
||||
{"codex turn ending with the assistant's message", CodexPath + "/responses",
|
||||
`{` + model + `,"instructions":"You are Codex.","stream":true,"input":[` + history + `]}`,
|
||||
continueText},
|
||||
{"codex compaction after a tool's output", CodexPath + "/responses",
|
||||
strings.Replace(issue1533, `"model": "google-vertex/gemini-3.8-flash"`, model, 1), codexCompactPrompt},
|
||||
{"compaction after a tool's output on /v1/responses", "/v1/responses",
|
||||
strings.Replace(issue1533, `"model": "google-vertex/gemini-3.8-flash"`, model, 1), codexCompactPrompt},
|
||||
} {
|
||||
var rec *httptest.ResponseRecorder
|
||||
if c.path == CodexPath+"/responses" {
|
||||
rec = codexPostTo(s, c.body)
|
||||
} else {
|
||||
rec = vertexPost(s, c.path, c.body)
|
||||
}
|
||||
if rec.Code != http.StatusOK || strings.Contains(rec.Body.String(), "model turn") {
|
||||
t.Errorf("%s: %d %s", c.name, rec.Code, rec.Body.String())
|
||||
continue
|
||||
}
|
||||
mu.Lock()
|
||||
last, sent, no := bodies[len(bodies)-1], len(bodies), refused
|
||||
mu.Unlock()
|
||||
// asked once, and never in a shape the vendor turns away: a
|
||||
// refusal a retry gets past still rests the account (plugins#734)
|
||||
if sent != i+1 || no != 0 {
|
||||
t.Errorf("%s: the vendor was asked %d times for %d requests, %d of them refused", c.name, sent, i+1, no)
|
||||
}
|
||||
role, text := lastTurn(t, last["contents"])
|
||||
if role != "user" || !strings.Contains(text, c.want) {
|
||||
t.Errorf("%s: the plugin's vendor was asked ending with %s %q, want user %q", c.name, role, text, c.want)
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user