keeyangyy (#3938) named their client: Reasonix 1.x, which sends no session header (it sets x-opencode-session on its OpenCode Go paths only) and opens every conversation on a snapshot it writes, the same bytes in every conversation started in one workspace while its memory and skills catalog don't change: {"role":"user","content":"<session-context version=\"1\">\nThis host-generated snapshot supersedes every earlier session-context snapshot.\n\n## Environment\n...\nDigest: sha256:<hex>\n</session-context>"}, {"role":"user","content":"the question the user actually typed"} userWords didn't know the block, so the commit before this one still hashed it alone: every conversation a workspace opened had one id, and under Stays = session each new one was kept on the account that answered the first. "<session-context" joins injected, beside Codex's "<environment_context>", with no closing ">" so that its version attribute matches. What asks userWords now passes over the snapshot as it passes over Codex's context: - the conversation's id runs from the snapshot through what the user typed (conversationOpening); - the turn count: [snapshot][typed] is turn 1, where it was 2 (turnIn); - a group's conversation key is the typed words, where it was the snapshot, the same for every conversation in the workspace (firstWords, in ruleKey and the affinity scope); - the turn before, which the classifier is told (turnBefore): on a conversation's first turn, and on each turn a new snapshot comes with, it was the snapshot, workspace and memory and all, as the user's previous message with no reply; now it is nothing on a first turn, and the user's words before the snapshot and the reply to them on a later one; - the classifier's question, Copilot's Auto and gatewayQuery take the user's last words before a snapshot sent after them, and the Claude subscription takes one after tool results as the agent's notice (agentNotice). A Reasonix conversation under way when this lands gets an id of its own on its next turn and is routed once as a new conversation, as the previous commit says of the others: it may move to another account, and its cache there starts over. The shape is the report's, which keeyangyy read from Reasonix's source (internal/sessioncontext) and didn't capture; its folder and digest are left out as they left them, and the tests use it as written. Not changed: the prompt breakdown (promptBuilder.userText) keeps its own list of Codex's blocks and still files the snapshot as a chat turn, so a Reasonix conversation's first turn shows there as two turns, where Codex's context adds none. That is left for a commit of its own. What was run (tree 71364f08, on origin/maina5315c70, go1.26.8, linux/amd64, Linux 5.15), outside graftcp, umask 077: - With the previous commit's prompt.go and the tests as committed, the four tests this commit touches are red: TestReasonixConversationsInOneWorkspaceAreApart on every protocol ("chat: two conversations opened on one snapshot: magpie-310d1b42730f273079337f01 and magpie-310d1b42730f273079337f01 (the snapshot alone: magpie-310d1b42730f273079337f01)"); TestNewConversationsInOneFolderAreRoutedApart ("Reasonix's second conversation went to k2 as "session", conv magpie-310d1b42730f273079337f01; its first is on k2, conv magpie-310d1b42730f273079337f01"); TestTurnInSkipsWhatTheAgentPutIn ("Reasonix's snapshot, then the user's words: turn 2 within false, want 1 false"); TestFirstWordsAreWhatTheUserSaid ("key changed for conversation 4"). - Here, those four -count=5: 20 pass. - The tests of the opening and of what userWords feeds, in internal/gateway, sessions, gui and provider (the conversation id, dsh's ids, affinity, rotate, strict, OpenCode's sessions, the turn count, the classifier's question, firstWords, Copilot's Auto, a rule's turn held past an agent's message): ok, 121 top-level tests passed, none failed. - The previous commit's 12 hand-breaks of conversationOpening, userOpens and writeOpening, each alone on this tree: each red on the same tests as at a6037f68, and the first (the first user message hashed alone) also on TestReasonixConversationsInOneWorkspaceAreApart. - -race -count=5 of the routing and opening tests with the new ones: 85 pass, no race. -race -count=3 of the two allocation bounds: 6 pass. - gofmt -l clean. go vet -tags nogui for linux, windows and darwin, go build -tags nogui for linux and darwin, and go vet and go build for windows without the tag: exit 0 each. go vet and go build without the tag on linux: exit 1 (no libsoup-3.0 or gio-unix-2.0 here). - go test -tags nogui -count=1 -timeout 45m ./... (Bun 1.4.2, log kept; 8m43s, load 28 at its start): 72 packages ok, 5 red, none of them this commit's tests or what they cover. internal/gateway ran to its end (510s), so the new tests passed in it. The reds: agent TestInstalledVersion, TestQoderSettingsFromAnIDEPluginIsNotTheCLI and TestOmpDetectedWithPiDir (#3141), and TestReasonixCLIRequiresSupportedNativeVersion and TestReasonixCLIRecoversAfterProbeTimeout (#3641, the Agents page's check of the Reasonix CLI, which this doesn't reach); edit TestWriteAtomicReadOnlyHardLink and testenv TestProgram (umask 077, #3640); gateway TestPluginToldWhereTheAgentWorks (#3675) and TestUploadStillUnderwayIsNotCutByHealthPing (red on main, #3909, which has a test-only patch); gui TestGatewayHeldByAContainer and TestGatewayFixQuitsOnlyAMagpie (#3649). Not tried: Reasonix itself, or a body it sent. Its source (internal/agent/session_context.go, 1.39.10) sends a new snapshot only when its digest changes, as a user message of its own; the openai provider magpie writes for it (internal/agent/reasonix.go) sends each message as a Chat message of its own, and its Anthropic provider, which merges adjacent user messages, keeps the snapshot a text block of its own, which userWords reads alone. Ported with the commit before onto origin/main5ee5274bdfrom JoeyWu-tech/magpie fix/3938-opening-run-4 (02c1de292, f50687a46; posted on #3938, sha256 7cdb7e83...). The code applied as is; the docs and extrabody.go's comment were merged by hand beside main's newer text (${threadId}, ${subagentId}). What was run for the port (mote, macOS, go1.26.8): the five tests in conversation_opening_test.go and user_said_test.go's new case, with the conversation id, dsh, affinity, rotate, strict, sticks and extra body tests: ok. The same tests on5ee5274bd(old code, new tests): TestConversationsOnOneEnvironmentMessageAreApart (chat, messages, responses, gemini, gemini without roles), TestReasonixConversationsInOneWorkspaceAreApart, TestCacheMarkMovingOnKeepsTheID, TestOpeningHashReadsInPlace and TestNewConversationsInOneFolderAreRoutedApart red. Batch run, at tree 104d75f0a (the twelve coffemon ports together, on origin/main7d022bdb6), mote, macOS, go1.26.8: - go test -tags nogui -count=1 -timeout 45m on the packages the batch changes (agent, autostart, daemon, gateway, library, proc, provider, sessions, testenv, tui, gui): ok each. - GUI, node --test with Playwright 1.62.0, Chromium and WebKit: library-*.test.cjs and i18n-keys.test.cjs, 1044 of 1044 pass. - gofmt -l: nothing. go vet ./..., GOOS=linux go vet -tags nogui ./..., GOOS=windows go vet ./... and go build ./...: ok at cdd7c8e46, the same batch on origin/main00098f12bone rebase before. - The whole go test ./... at cdd7c8e46: 86 packages ok, one red, internal/cli TestEveryRouteHasACommandOrIsNamed (GET /api/context/reply has no command), red on main too (#5026). - Linux (wsl-run, Ubuntu 24.04, at 339055896, the batch onf4c664a09): proc, autostart, testenv, tui ok; agent: only TestGrokInstalled red (#3548); #4033's proc.Look reds (Zed, Copilot, Qoder, Cursor, Omp) pass. - Windows (win-run, Windows 11, at 339055896): proc, autostart, testenv, tui ok; library and agent red in the same tests as on origin/mainf4c664a09(#4115, #1827, #4786; #5068, #5069, #5070 filed now). - Rebased onto origin/mainfb1f4856a(three commits, no file in common with the batch) and run again at the pushed tree: gofmt -l nothing, go build ./... and go vet ./... ok; go test -tags nogui sessions, agent, autostart, tui, proc, testenv ok; i18n-keys and library-* 1044 of 1044 pass. - Not run before the push: the whole GUI suite (a run at cdd7c8e46 was cut off at 1170 of ~1174 tests when mote rebooted; it runs again after the tag and any red is filed).
436 lines
23 KiB
Go
436 lines
23 KiB
Go
package gateway
|
|
|
|
import (
|
|
"bytes"
|
|
"context"
|
|
"crypto/sha256"
|
|
"encoding/hex"
|
|
"io"
|
|
"net/http"
|
|
"runtime"
|
|
"strconv"
|
|
"strings"
|
|
"testing"
|
|
|
|
"github.com/yetone/magpie/internal/provider"
|
|
"github.com/yetone/magpie/internal/routes"
|
|
)
|
|
|
|
// keeyangyy (#3938): a client that sends no session header and opens every
|
|
// conversation on a user message it writes alone, the same for every
|
|
// conversation started in one folder, with what the user typed after it.
|
|
// The texts are the report's own.
|
|
const (
|
|
openingEnv = "<environment_context>cwd=/w/proj ...</environment_context>"
|
|
openingAsked = "the question the user actually typed"
|
|
openingOther = "write the release notes for 0.2"
|
|
)
|
|
|
|
// openingProtocol builds a request's turns as one protocol has them.
|
|
type openingProtocol struct {
|
|
name string
|
|
body func(turns ...string) string
|
|
user func(text string) string
|
|
reply func(text string) string
|
|
image string // a user message that is a screenshot alone
|
|
}
|
|
|
|
func openingProtocols() []openingProtocol {
|
|
const png = "iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAYAAAAfFcSJAAAADUlEQVR42mNkYPhfDwAChwGA60e6kgAAAABJRU5ErkJggg=="
|
|
q := strconv.Quote
|
|
join := func(turns []string) string { return strings.Join(turns, ",") }
|
|
gemini := func(turns ...string) string {
|
|
return `{"systemInstruction":{"parts":[{"text":"You are a coding agent."}]},"contents":[` + join(turns) + `]}`
|
|
}
|
|
geminiReply := func(s string) string { return `{"role":"model","parts":[{"text":` + q(s) + `}]}` }
|
|
geminiImage := `"parts":[{"inlineData":{"mimeType":"image/png","data":"` + png + `"}}]}`
|
|
return []openingProtocol{
|
|
{"chat",
|
|
func(turns ...string) string {
|
|
return `{"model":"m","messages":[{"role":"system","content":"..."},` + join(turns) + `]}`
|
|
},
|
|
func(s string) string { return `{"role":"user","content":` + q(s) + `}` },
|
|
func(s string) string { return `{"role":"assistant","content":` + q(s) + `}` },
|
|
`{"role":"user","content":[{"type":"image_url","image_url":{"url":"data:image/png;base64,` + png + `"}}]}`},
|
|
{"messages",
|
|
func(turns ...string) string {
|
|
return `{"model":"m","max_tokens":1024,"system":"You are a coding agent.","messages":[` + join(turns) + `]}`
|
|
},
|
|
func(s string) string { return `{"role":"user","content":[{"type":"text","text":` + q(s) + `}]}` },
|
|
func(s string) string { return `{"role":"assistant","content":[{"type":"text","text":` + q(s) + `}]}` },
|
|
`{"role":"user","content":[{"type":"image","source":{"type":"base64","media_type":"image/png","data":"` + png + `"}}]}`},
|
|
{"responses",
|
|
func(turns ...string) string {
|
|
return `{"model":"m","instructions":"You are a coding agent.","input":[` + join(turns) + `]}`
|
|
},
|
|
func(s string) string {
|
|
return `{"type":"message","role":"user","content":[{"type":"input_text","text":` + q(s) + `}]}`
|
|
},
|
|
func(s string) string {
|
|
return `{"type":"message","role":"assistant","content":[{"type":"output_text","text":` + q(s) + `}]}`
|
|
},
|
|
`{"type":"message","role":"user","content":[{"type":"input_image","image_url":"data:image/png;base64,` + png + `"}]}`},
|
|
{"gemini", gemini,
|
|
func(s string) string { return `{"role":"user","parts":[{"text":` + q(s) + `}]}` },
|
|
geminiReply, `{"role":"user",` + geminiImage},
|
|
// Gemini takes a content without a role as the user's
|
|
{"gemini without roles", gemini,
|
|
func(s string) string { return `{"parts":[{"text":` + q(s) + `}]}` },
|
|
geminiReply, `{` + geminiImage},
|
|
}
|
|
}
|
|
|
|
// openingID is the id a request's first user message alone was hashed to,
|
|
// before #3938 and still for a conversation that opens in the user's words.
|
|
func openingID(msg string) string {
|
|
sum := sha256.Sum256([]byte(msg))
|
|
return "magpie-" + hex.EncodeToString(sum[:12])
|
|
}
|
|
|
|
func TestConversationsOnOneEnvironmentMessageAreApart(t *testing.T) {
|
|
id := func(body string) string { return conversationID(http.Header{}, []byte(body)) }
|
|
for _, p := range openingProtocols() {
|
|
t.Run(p.name, func(t *testing.T) {
|
|
env := p.user(openingEnv)
|
|
a := id(p.body(env, p.user(openingAsked)))
|
|
b := id(p.body(env, p.user(openingOther)))
|
|
if a == b || a == openingID(env) {
|
|
t.Errorf("two conversations opened on one environment message: %s and %s (the message alone: %s)", a, b, openingID(env))
|
|
}
|
|
// a later turn repeats the opening, the answer and the user's next message after it
|
|
if got := id(p.body(env, p.user(openingAsked), p.reply("Looked into it."), p.user("thanks, now the tests"))); got != a {
|
|
t.Errorf("the first conversation's next turn is %s, not %s", got, a)
|
|
}
|
|
if got := id(p.body(env, p.user(openingOther), p.reply("Done."), p.user("and the changelog"))); got != b {
|
|
t.Errorf("the second conversation's next turn is %s, not %s", got, b)
|
|
}
|
|
// a screenshot is the user's own as their words are: the
|
|
// opening ends at it, before the message they send after it
|
|
shot := id(p.body(env, p.image))
|
|
if got := id(p.body(env, p.image, p.user("what is this error?"))); shot == openingID(env) || got != shot {
|
|
t.Errorf("opened on the agent's message and a screenshot: %s, and %s with the user's next message (the agent's alone: %s)", shot, got, openingID(env))
|
|
}
|
|
|
|
// one that opens in the user's words is what it always was:
|
|
// its first user message alone, sha256's first 12 bytes in hex
|
|
asked := p.user(openingAsked)
|
|
want := openingID(asked)
|
|
for name, body := range map[string]string{
|
|
"alone": p.body(asked),
|
|
"a later turn": p.body(asked, p.reply("Looked into it."), p.user("thanks")),
|
|
"the agent's message after it": p.body(asked, p.user(openingEnv)),
|
|
"the agent's, then a later turn": p.body(asked, p.user(openingEnv), p.reply("Looked into it."), p.user("thanks")),
|
|
} {
|
|
if got := id(body); got != want {
|
|
t.Errorf("%s: %s, not the first user message's own %s", name, got, want)
|
|
}
|
|
}
|
|
})
|
|
}
|
|
|
|
// An agent's system or developer message between them is passed over.
|
|
for name, pair := range map[string][2]string{
|
|
"chat": {
|
|
`{"messages":[{"role":"system","content":"..."},{"role":"user","content":"` + openingEnv + `"},{"role":"developer","content":"Approval policy: ask."},{"role":"user","content":"` + openingAsked + `"}]}`,
|
|
`{"messages":[{"role":"system","content":"..."},{"role":"user","content":"` + openingEnv + `"},{"role":"user","content":"` + openingAsked + `"}]}`,
|
|
},
|
|
"responses": {
|
|
`{"input":[{"type":"message","role":"developer","content":"..."},{"type":"message","role":"user","content":"` + openingEnv + `"},{"type":"message","role":"system","content":"Approval policy: ask."},{"type":"message","role":"user","content":"` + openingAsked + `"}]}`,
|
|
`{"input":[{"type":"message","role":"developer","content":"..."},{"type":"message","role":"user","content":"` + openingEnv + `"},{"type":"message","role":"user","content":"` + openingAsked + `"}]}`,
|
|
},
|
|
} {
|
|
if got, want := id(pair[0]), id(pair[1]); got != want {
|
|
t.Errorf("%s: the developer message between them moved the id: %s, not %s", name, got, want)
|
|
}
|
|
}
|
|
|
|
// dsh's messages route (internal/sessions/testdata/dsh-gateway) marks
|
|
// its last user message for the cache: its runtime context, after what
|
|
// the user typed. The mark moves on to the next turn's, and the id
|
|
// stays the typed message's own, which the Context page finds dsh's
|
|
// sessions by (dshConvs).
|
|
typed := `{"role":"user","content":"` + openingAsked + `"}`
|
|
runtime := func(mark string) string {
|
|
return `{"role":"user","content":[{"type":"text","text":"Current runtime context. This snapshot supersedes earlier runtime-context snapshots."` + mark + `}]}`
|
|
}
|
|
marked := runtime(`,"cache_control":{"type":"ephemeral"}`)
|
|
for name, body := range map[string]string{
|
|
"its first turn": `{"model":"m","messages":[` + typed + `,` + marked + `]}`,
|
|
"its next": `{"model":"m","messages":[` + typed + `,` + runtime("") + `,{"role":"assistant","content":[{"type":"text","text":"Done."}]},{"role":"user","content":"thanks"},` + marked + `]}`,
|
|
} {
|
|
if got, want := id(body), openingID(typed); got != want {
|
|
t.Errorf("dsh's %s: %s, not the typed message's own %s", name, got, want)
|
|
}
|
|
}
|
|
|
|
// What the opening is not: a history its client cut, which starts with
|
|
// a tool's result, has no opening left, and its first user message is
|
|
// hashed alone as before, whatever the user says after it; nor is a
|
|
// result after the agent's message part of the opening.
|
|
cut := `{"role":"user","content":[{"type":"tool_result","tool_use_id":"toolu_gone","content":"exit 0"}]}`
|
|
if got, want := id(`{"model":"m","messages":[`+cut+`,{"role":"user","content":"go on"},{"role":"assistant","content":"On it."}]}`), openingID(cut); got != want {
|
|
t.Errorf("a history cut at a tool's result: %s, not its first message's own %s", got, want)
|
|
}
|
|
env := `{"role":"user","content":"` + openingEnv + `"}`
|
|
if got, want := id(`{"model":"m","messages":[`+env+`,`+cut+`,{"role":"user","content":"go on"}]}`), openingID(env); got != want {
|
|
t.Errorf("a tool's result after the agent's message: %s, not the first message's own %s", got, want)
|
|
}
|
|
// A message without a role is no one's in Chat and Responses, and
|
|
// Responses' calls, their outputs and reasoning have none: each ends
|
|
// the opening too, as a function's result does in Gemini's.
|
|
respEnv := `{"type":"message","role":"user","content":"` + openingEnv + `"}`
|
|
respAsked := `{"type":"message","role":"user","content":"` + openingAsked + `"}`
|
|
gemEnv := `{"role":"user","parts":[{"text":"` + openingEnv + `"}]}`
|
|
gemAsked := `{"role":"user","parts":[{"text":"` + openingAsked + `"}]}`
|
|
result := `"parts":[{"functionResponse":{"name":"shell","response":{"output":"exit 0"}}}]}`
|
|
for name, c := range map[string][2]string{
|
|
"chat, a message without a role": {`{"messages":[` + env + `,{"content":"preface"},{"role":"user","content":"` + openingAsked + `"}]}`, env},
|
|
"responses, a call": {`{"input":[` + respEnv + `,{"type":"function_call","call_id":"call_1","name":"shell","arguments":"{}"},` + respAsked + `]}`, respEnv},
|
|
"responses, a call's output": {`{"input":[` + respEnv + `,{"type":"function_call_output","call_id":"call_1","output":"exit 0"},` + respAsked + `]}`, respEnv},
|
|
"responses, reasoning": {`{"input":[` + respEnv + `,{"type":"reasoning","summary":[]},` + respAsked + `]}`, respEnv},
|
|
"gemini, a function's result": {`{"contents":[` + gemEnv + `,{"role":"user",` + result + `,` + gemAsked + `]}`, gemEnv},
|
|
"gemini, one in its own role": {`{"contents":[` + gemEnv + `,{"role":"function",` + result + `,` + gemAsked + `]}`, gemEnv},
|
|
} {
|
|
if got, want := id(c[0]), openingID(c[1]); got != want {
|
|
t.Errorf("%s after the agent's message: %s, not the first message's own %s", name, got, want)
|
|
}
|
|
}
|
|
// A Responses input that is one string is hashed as it was.
|
|
if got, want := id(`{"model":"m","input":"`+openingAsked+`"}`), openingID(`"`+openingAsked+`"`); got != want {
|
|
t.Errorf("a string input: %s, not %s", got, want)
|
|
}
|
|
}
|
|
|
|
// keeyangyy's client (#3938), Reasonix 1.x, sends no session header and
|
|
// opens every conversation on its snapshot of the workspace, the same for
|
|
// each one started there, before what the user typed: the snapshot is the
|
|
// agent's (injected), so its conversations are apart as Codex's are, in
|
|
// each protocol, and each keeps its id on later turns.
|
|
func TestReasonixConversationsInOneWorkspaceAreApart(t *testing.T) {
|
|
id := func(body string) string { return conversationID(http.Header{}, []byte(body)) }
|
|
for _, p := range openingProtocols() {
|
|
snap := p.user(reasonixContext)
|
|
a := id(p.body(snap, p.user(openingAsked)))
|
|
b := id(p.body(snap, p.user(openingOther)))
|
|
if a == b || a == openingID(snap) {
|
|
t.Errorf("%s: two conversations opened on one snapshot: %s and %s (the snapshot alone: %s)", p.name, a, b, openingID(snap))
|
|
}
|
|
if got := id(p.body(snap, p.user(openingAsked), p.reply("Looked into it."), p.user("thanks, now the tests"))); got != a {
|
|
t.Errorf("%s: the first conversation's next turn is %s, not %s", p.name, got, a)
|
|
}
|
|
}
|
|
}
|
|
|
|
// An agent marks its newest user message for the cache and moves the mark
|
|
// on to the next turn's: the report's opening goes out as [environment]
|
|
// [typed, marked], then [environment][typed][reply][next, marked], and so
|
|
// on. The typed message goes on unmarked as it was, as a plain string
|
|
// (dsh's way with one part) or on a message marked whole. Every turn has
|
|
// the first turn's id.
|
|
func TestCacheMarkMovingOnKeepsTheID(t *testing.T) {
|
|
id := func(body string) string { return conversationID(http.Header{}, []byte(body)) }
|
|
q := strconv.Quote
|
|
const mark = `,"cache_control":{"type":"ephemeral"}`
|
|
const png = `"iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAYAAAAfFcSJAAAADUlEQVR42mNkYPhfDwAChwGA60e6kgAAAABJRU5ErkJggg=="`
|
|
ps := map[string]openingProtocol{}
|
|
for _, p := range openingProtocols() {
|
|
ps[p.name] = p
|
|
}
|
|
chat := func(content string) string { return `{"role":"user","content":` + content + `}` }
|
|
resp := func(content string) string { return `{"type":"message","role":"user","content":` + content + `}` }
|
|
text := func(kind string) func(s, mark string) string {
|
|
return func(s, mark string) string { return `[{"type":"` + kind + `","text":` + q(s) + mark + `}]` }
|
|
}
|
|
shot := map[string]func(s, mark string) string{
|
|
"chat": func(s, mark string) string {
|
|
return `[{"type":"text","text":` + q(s) + `},{"type":"image_url","image_url":{"url":"data:image/png;base64,` + png[1:] + `}` + mark + `}]`
|
|
},
|
|
"messages": func(s, mark string) string {
|
|
return `[{"type":"text","text":` + q(s) + `},{"type":"image","source":{"type":"base64","media_type":"image/png","data":` + png + `}` + mark + `}]`
|
|
},
|
|
"responses": func(s, mark string) string {
|
|
return `[{"type":"input_text","text":` + q(s) + `},{"type":"input_image","image_url":"data:image/png;base64,` + png[1:] + mark + `}]`
|
|
},
|
|
}
|
|
type spelling struct{ marked, plain func(s string) string }
|
|
for _, c := range []struct {
|
|
name, p string
|
|
spelling
|
|
}{
|
|
{"messages, one part", "messages", spelling{func(s string) string { return chat(text("text")(s, mark)) }, func(s string) string { return chat(text("text")(s, "")) }}},
|
|
{"messages, a string unmarked", "messages", spelling{func(s string) string { return chat(text("text")(s, mark)) }, func(s string) string { return chat(q(s)) }}},
|
|
{"messages, a screenshot", "messages", spelling{func(s string) string { return chat(shot["messages"](s, mark)) }, func(s string) string { return chat(shot["messages"](s, "")) }}},
|
|
{"chat, one part", "chat", spelling{func(s string) string { return chat(text("text")(s, mark)) }, func(s string) string { return chat(text("text")(s, "")) }}},
|
|
{"chat, a string unmarked", "chat", spelling{func(s string) string { return chat(text("text")(s, mark)) }, func(s string) string { return chat(q(s)) }}},
|
|
{"chat, the message marked", "chat", spelling{func(s string) string { return `{"role":"user","content":` + q(s) + mark + `}` }, func(s string) string { return chat(q(s)) }}},
|
|
{"chat, a screenshot", "chat", spelling{func(s string) string { return chat(shot["chat"](s, mark)) }, func(s string) string { return chat(shot["chat"](s, "")) }}},
|
|
{"responses, one part", "responses", spelling{func(s string) string { return resp(text("input_text")(s, mark)) }, func(s string) string { return resp(text("input_text")(s, "")) }}},
|
|
{"responses, a string unmarked", "responses", spelling{func(s string) string { return resp(text("input_text")(s, mark)) }, func(s string) string { return resp(q(s)) }}},
|
|
{"responses, a screenshot", "responses", spelling{func(s string) string { return resp(shot["responses"](s, mark)) }, func(s string) string { return resp(shot["responses"](s, "")) }}},
|
|
} {
|
|
p := ps[c.p]
|
|
env, r := p.user(openingEnv), p.reply("Done.")
|
|
first := id(p.body(env, c.marked(openingAsked)))
|
|
if first == openingID(env) || first == id(p.body(env, c.marked(openingOther))) {
|
|
t.Errorf("%s: the first turn is %s, the environment message's own or the other conversation's", c.name, first)
|
|
}
|
|
for n, body := range []string{
|
|
p.body(env, c.plain(openingAsked), r, c.marked("now the tests")),
|
|
p.body(env, c.plain(openingAsked), r, c.plain("now the tests"), r, c.marked("and the changelog")),
|
|
} {
|
|
if got := id(body); got != first {
|
|
t.Errorf("%s: turn %d is %s, not the first turn's %s", c.name, n+2, got, first)
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
// An opening of several messages is read where it is, as one message
|
|
// always was: every request of a conversation hashes it again (its
|
|
// affinity, its trace, and more), and the user's message in it can hold
|
|
// a screenshot of a megabyte. Hashing it allocates a small part of the
|
|
// body (image_input_test.go's bar).
|
|
func TestOpeningHashReadsInPlace(t *testing.T) {
|
|
shot := strings.Repeat("iVBORw0KGgo", 100_000)
|
|
turns := []string{`{"role":"user","content":"` + openingEnv + `"}`,
|
|
`{"role":"user","content":[{"type":"text","text":"what is this error?"},{"type":"image_url","image_url":{"url":"data:image/png;base64,` + shot + `"},"cache_control":{"type":"ephemeral"}}]}`}
|
|
for i := range 50 {
|
|
turns = append(turns, `{"role":"assistant","content":"Looked into it."}`, `{"role":"user","content":"and step `+strconv.Itoa(i)+`"}`)
|
|
}
|
|
body := []byte(`{"model":"m","messages":[` + strings.Join(turns, ",") + `]}`)
|
|
ctx, done := context.WithCancel(context.Background())
|
|
defer done()
|
|
holdBody(ctx, body) // as readRequestBody holds a request's
|
|
id := conversationID(http.Header{}, body)
|
|
if id == openingID(turns[0]) {
|
|
t.Fatal("the environment message was hashed alone")
|
|
}
|
|
var before, after runtime.MemStats
|
|
runtime.ReadMemStats(&before)
|
|
const n = 5
|
|
for range n {
|
|
if conversationID(http.Header{}, body) != id {
|
|
t.Fatal("the conversation's id changed")
|
|
}
|
|
}
|
|
runtime.ReadMemStats(&after)
|
|
per := (after.TotalAlloc - before.TotalAlloc) / n
|
|
if per > uint64(len(body))/8 {
|
|
t.Fatalf("the id of a %d-byte body opening on a screenshot allocated %d bytes", len(body), per)
|
|
}
|
|
t.Logf("the id of a %d-byte body opening on a screenshot allocated %d bytes", len(body), per)
|
|
}
|
|
|
|
// keyedStreams is keyed answering a request that asks for a stream with
|
|
// one, as the gateway asks a Chat vendor for on /v1/messages.
|
|
type keyedStreams struct{ keyed }
|
|
|
|
func (k *keyedStreams) ServeHTTP(w http.ResponseWriter, r *http.Request) {
|
|
body, _ := io.ReadAll(r.Body)
|
|
if !bytes.Contains(body, []byte(`"stream":true`)) {
|
|
r.Body = io.NopCloser(bytes.NewReader(body))
|
|
k.keyed.ServeHTTP(w, r)
|
|
return
|
|
}
|
|
key := strings.TrimPrefix(r.Header.Get("Authorization"), "Bearer ")
|
|
k.tried = append(k.tried, key)
|
|
w.Header().Set("Content-Type", "text/event-stream")
|
|
io.WriteString(w, `data: {"id":"x","choices":[{"index":0,"delta":{"role":"assistant","content":"from `+key+`"}}]}`+"\n\n"+
|
|
`data: {"id":"x","choices":[{"index":0,"delta":{},"finish_reason":"stop"}],"usage":{"prompt_tokens":3000,"completion_tokens":5}}`+"\n\n"+
|
|
"data: [DONE]\n\n")
|
|
}
|
|
|
|
// The report's case through the gateway: a provider with two keys, each
|
|
// conversation kept on its own for the session and new ones handed round
|
|
// in turn. A second conversation started in the same folder goes to the
|
|
// key the first isn't on, as a new one, and each one's next turn stays
|
|
// where it began. So does a conversation on /v1/messages, its cache mark
|
|
// moving on to each turn's newest message, and the reporter's client's.
|
|
func TestNewConversationsInOneFolderAreRoutedApart(t *testing.T) {
|
|
fresh(t)
|
|
v := &keyedStreams{}
|
|
serveOn(t, "aff", "k1", []string{"m"}, v, "k2")
|
|
if err := provider.SetRouting("aff", provider.Rotate); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if err := provider.SetAffinity("aff", provider.AffinitySession); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
routes.Forget("aff")
|
|
s := New()
|
|
routed := func(path, body string) string {
|
|
if code, out := postTo(t, s, path, body); code != http.StatusOK {
|
|
t.Fatalf("%d %s", code, out)
|
|
}
|
|
r := s.trace.routes[len(s.trace.routes)-1]
|
|
if r.Affinity == nil {
|
|
t.Fatalf("no affinity traced: %+v", r)
|
|
}
|
|
return v.tried[len(v.tried)-1] + " " + r.Affinity.Why + " " + r.Conv
|
|
}
|
|
turn := func(opening string, typed ...string) string {
|
|
msgs := `{"role":"system","content":"..."},{"role":"user","content":` + strconv.Quote(opening) + `}`
|
|
for i, said := range typed {
|
|
if i > 0 {
|
|
msgs += `,{"role":"assistant","content":"Done."}`
|
|
}
|
|
msgs += `,{"role":"user","content":"` + said + `"}`
|
|
}
|
|
return routed("/v1/chat/completions", `{"model":"aff/m","messages":[`+msgs+`]}`)
|
|
}
|
|
first := strings.Fields(turn(openingEnv, openingAsked))
|
|
second := strings.Fields(turn(openingEnv, openingOther))
|
|
if first[1] != "first" || second[1] != "first" || second[0] == first[0] || second[2] == first[2] {
|
|
t.Fatalf("the second conversation went to %s as %q, conv %s; the first is on %s, conv %s", second[0], second[1], second[2], first[0], first[2])
|
|
}
|
|
for _, c := range []struct {
|
|
typed []string
|
|
was []string
|
|
}{
|
|
{[]string{openingAsked, "thanks, now the tests"}, first},
|
|
{[]string{openingOther, "and the changelog"}, second},
|
|
} {
|
|
if got := strings.Fields(turn(openingEnv, c.typed...)); got[0] != c.was[0] || got[1] != "session" || got[2] != c.was[2] {
|
|
t.Errorf("%q's next turn went to %s as %q, conv %s; it began on %s, conv %s", c.typed[0], got[0], got[1], got[2], c.was[0], c.was[2])
|
|
}
|
|
}
|
|
|
|
said := func(text, mark string) string {
|
|
return `{"role":"user","content":[{"type":"text","text":"` + text + `"` + mark + `}]}`
|
|
}
|
|
const marked = `,"cache_control":{"type":"ephemeral"}`
|
|
reply := `{"role":"assistant","content":[{"type":"text","text":"Done."}]}`
|
|
messages := func(turns ...string) string {
|
|
return routed("/v1/messages", `{"model":"aff/m","max_tokens":64,"system":"You are a coding agent.","messages":[`+
|
|
said(openingEnv, "")+`,`+strings.Join(turns, ",")+`]}`)
|
|
}
|
|
third := strings.Fields(messages(said("fix the flaky test", marked)))
|
|
if third[1] != "first" || third[2] == first[2] || third[2] == second[2] {
|
|
t.Fatalf("the conversation on /v1/messages went to %s as %q, conv %s (the others' %s and %s)", third[0], third[1], third[2], first[2], second[2])
|
|
}
|
|
for n, turns := range [][]string{
|
|
{said("fix the flaky test", ""), reply, said("now the lint", marked)},
|
|
{said("fix the flaky test", ""), reply, said("now the lint", ""), reply, said("and push it", marked)},
|
|
} {
|
|
if got := strings.Fields(messages(turns...)); got[0] != third[0] || got[1] != "session" || got[2] != third[2] {
|
|
t.Errorf("its turn %d on /v1/messages went to %s as %q, conv %s; it began on %s, conv %s", n+2, got[0], got[1], got[2], third[0], third[2])
|
|
}
|
|
}
|
|
|
|
// the report's client, Reasonix 1.x, opening each conversation on its
|
|
// snapshot of the workspace, the same in every one started there
|
|
fourth := strings.Fields(turn(reasonixContext, openingAsked))
|
|
fifth := strings.Fields(turn(reasonixContext, openingOther))
|
|
if fourth[1] != "first" || fifth[1] != "first" || fifth[0] == fourth[0] || fifth[2] == fourth[2] {
|
|
t.Fatalf("Reasonix's second conversation went to %s as %q, conv %s; its first is on %s, conv %s", fifth[0], fifth[1], fifth[2], fourth[0], fourth[2])
|
|
}
|
|
for _, c := range []struct{ typed, was []string }{
|
|
{[]string{openingAsked, "thanks, now the tests"}, fourth},
|
|
{[]string{openingOther, "and the changelog"}, fifth},
|
|
} {
|
|
if got := strings.Fields(turn(reasonixContext, c.typed...)); got[0] != c.was[0] || got[1] != "session" || got[2] != c.was[2] {
|
|
t.Errorf("Reasonix's %q next turn went to %s as %q, conv %s; it began on %s, conv %s", c.typed[0], got[0], got[1], got[2], c.was[0], c.was[2])
|
|
}
|
|
}
|
|
}
|