masaka on Discord asked to see the whole context behind the context window
card: click the agent's prompt, or any other part, and read it.
What changed:
- gateway: the trace keeps the last 10 request bodies in memory only
(promptBodies; at most 32 MiB each and 64 MiB together, oldest dropped
first), masked as the vendor got them. For a model the user let have
the request unmasked, the masked copy is kept all the same. Never
written to disk, never in telemetry. Route.Prompt and the routing
history keep names and sizes only, as before.
- gateway: Server.PromptText re-reads a part's items and their text from
that body (prompt_text.go). promptBuilder gains a keep mode that records
each item's pieces: system text, tool definitions laid out as JSON, each
turn's user/thinking/assistant/call pieces, tool results, and images
or sealed reasoning named with their size. Sizes are unchanged.
- gui: GET /api/context/text?id=&kind=[&item=] (no-store; 404 "not kept"
when the body is gone or another process serves the gateway).
- gui: in the card, a legend part, a waffle cell or a content row
(click, Enter or Space) opens a wide modal: one item alone with "All of
{part}", or the part's list 100 rows at a time, each item's text
fetched when it is opened and shown 20000 characters at a time, with
copy buttons. Strings in zh, zh-TW, ja and de.
- docs: gateway-routing and gui-shell subsystem pages; the site's start
page (en, zh, ja) says the text is kept for the last 10 requests in
memory and sent nowhere. The README Privacy is unchanged, since nothing
more is sent.
What was run:
- go test -tags nogui ./... (temp HOME). internal/gateway hit the 10m
default under the full parallel run, in TestRequestTailAfterOldLimit,
which takes 57s alone on the base commit too; the package run alone
with -timeout 30m: ok (700s, the machine shared with other runs). Every other package: ok.
- New tests TestPromptTextIsWhatWasSent, ...KeepsOnlyTheLastBodies,
...ChatPieces, TestContextTextAPI. Broken by hand: keeping the unmasked
body fails (the key shows), and dropping keepBody fails ("not kept").
- gofmt -l; go vet, GOOS=linux and GOOS=windows go vet on internal/gateway
and internal/gui.
- GUI: tests/context-text.test.cjs in Chromium and WebKit, en/zh/zh-TW/
ja/de, at 1100 and 420px: legend, row, cell and keyboard open the text,
long text grows on demand, the page never scrolls, and a gone body is
named. It fails with the click wiring removed. Under a load average of
~140 one case failed once: the test read the list while the dialog
still said "Reading…". Reproduced with a 400ms reply, fixed by waiting
for the list; the mock now always answers after 200ms. A second case
("a click doesn't move the page", 662 vs 605 on the contents row) came
from taking the page's top while a wheel scroll was still easing: the
test now wheels to the row like the other cases, and wheelTo waits for
three equal looks. Four full runs at load ~150 then passed (80 cases). context-window,
i18n-keys, held-animations and the modal tests (dialog-backdrop-drag,
editor-short-window, readable-focus, ux-safety, whats-new, add-sheet,
api-key-theme) pass in both engines.
- Real use: a sandbox magpie (own HOME, ports 3486/3487, the Codex login)
served a real `claude -p` (Claude Code) request on codex/gpt-6-luna
that read a file. In its web UI, Usage > Context: each of System
prompt, Tools, Memory, Files and Conversation, a waffle cell and a
contents row opened the request's real text (Claude Code's system
prompt, 21 tool definitions, the CLAUDE.md reminder, main.go's Read
result, the turn with its thinking and call). No page errors.
163 lines
5.9 KiB
Go
163 lines
5.9 KiB
Go
package gateway
|
|
|
|
import (
|
|
"context"
|
|
"encoding/json"
|
|
"net/http/httptest"
|
|
"strings"
|
|
"testing"
|
|
|
|
"github.com/yetone/magpie/internal/provider"
|
|
"github.com/yetone/magpie/internal/settings"
|
|
)
|
|
|
|
// masaka on Discord: clicking a part of the context window card shows the
|
|
// text the request sent there — the system prompt, each tool's definition,
|
|
// each turn — masked as the vendor got it, even where the request went to
|
|
// a model the user let have it unmasked. The route, which goes in the
|
|
// routing history on disk, still holds no text.
|
|
func TestPromptTextIsWhatWasSent(t *testing.T) {
|
|
const key = "sk-proj-abcdEFGH1234ijklMNOP5678qrst"
|
|
for _, unmasked := range []bool{false, true} {
|
|
f := &fake{ctype: "application/json", reply: `{"id":"msg_1","type":"message","role":"assistant","model":"m1","content":[{"type":"text","text":"ok"}],"stop_reason":"end_turn","usage":{"input_tokens":300,"output_tokens":1}}`}
|
|
setup(t, provider.Anthropic, f)
|
|
if unmasked {
|
|
p, err := provider.Find("fake")
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
p.Unredacted = true
|
|
if err := provider.Save(*p); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
}
|
|
if err := settings.Save(settings.Settings{Redact: true}); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
body := `{"model":"fake/m1","max_tokens":10,
|
|
"system":"You are the release agent. Ship carefully; the deploy key is ` + key + `.",
|
|
"tools":[{"name":"Deploy","description":"Deploys the build to production.","input_schema":{"type":"object","properties":{"env":{"type":"string"}}}}],
|
|
"messages":[
|
|
{"role":"user","content":"ship the build"},
|
|
{"role":"assistant","content":[{"type":"text","text":"Deploying."},{"type":"tool_use","id":"c1","name":"Deploy","input":{"env":"prod"}}]},
|
|
{"role":"user","content":[{"type":"tool_result","tool_use_id":"c1","content":"deployed to prod in 42s"}]}]}`
|
|
s := New()
|
|
rec := httptest.NewRecorder()
|
|
s.Handler().ServeHTTP(rec, httptest.NewRequest("POST", "/v1/messages", strings.NewReader(body)))
|
|
if rec.Code != 200 {
|
|
t.Fatalf("%d %s", rec.Code, rec.Body)
|
|
}
|
|
if strings.Contains(string(f.got), key) != unmasked {
|
|
t.Fatalf("unmasked %v sent %s", unmasked, f.got)
|
|
}
|
|
rs := s.Trace(context.Background(), 0, 0).Routes
|
|
if len(rs) != 1 || rs[0].Prompt == nil {
|
|
t.Fatalf("%+v", rs)
|
|
}
|
|
id := rs[0].ID
|
|
text := func(kind string) string {
|
|
pt, ok := s.PromptText(id, kind, true)
|
|
if !ok {
|
|
t.Fatalf("%s: not kept", kind)
|
|
}
|
|
b, _ := json.Marshal(pt)
|
|
return string(b)
|
|
}
|
|
sys := text(PartSystem)
|
|
if !strings.Contains(sys, "Ship carefully; the deploy key is") || strings.Contains(sys, key) {
|
|
t.Fatalf("unmasked %v system %s", unmasked, sys)
|
|
}
|
|
if tools := text(PartTools); !strings.Contains(tools, `Deploys the build to production.`) || !strings.Contains(tools, `\"env\"`) {
|
|
t.Fatalf("tools %s", tools)
|
|
}
|
|
if res := text(PartResults); !strings.Contains(res, "deployed to prod in 42s") {
|
|
t.Fatalf("results %s", res)
|
|
}
|
|
chat, _ := s.PromptText(id, PartChat, true)
|
|
var roles []string
|
|
for _, e := range chat.Items {
|
|
for _, pc := range e.Pieces {
|
|
roles = append(roles, e.Name+":"+pc.Role+":"+pc.Name)
|
|
}
|
|
}
|
|
if got := strings.Join(roles, " "); got != "1:user: 1:assistant: 1:call:Deploy" {
|
|
t.Fatalf("chat %s", got)
|
|
}
|
|
// listed without text when no item is asked for
|
|
if l, _ := s.PromptText(id, PartSystem, false); len(l.Items) != 1 || l.Items[0].Pieces != nil {
|
|
t.Fatalf("list %+v", l)
|
|
}
|
|
// the route, as the history keeps it, holds names only
|
|
b, _ := json.Marshal(rs[0])
|
|
if strings.Contains(string(b), "Ship carefully") || strings.Contains(string(b), "Deploys the build") {
|
|
t.Fatalf("route carries text: %s", b)
|
|
}
|
|
}
|
|
}
|
|
|
|
// Only the last promptBodies requests' bodies are kept, a body over
|
|
// promptBodyMax never, and together no more than promptBodiesMax.
|
|
func TestPromptTextKeepsOnlyTheLastBodies(t *testing.T) {
|
|
var tr trace
|
|
body := []byte(`{"system":"s","messages":[{"role":"user","content":"hi"}]}`)
|
|
for id := int64(1); id <= promptBodies+2; id++ {
|
|
tr.keepBody(id, provider.Anthropic, body)
|
|
}
|
|
for id := int64(1); id <= promptBodies+2; id++ {
|
|
if _, ok := tr.keptBodyOf(id); ok != (id > 2) {
|
|
t.Fatalf("body %d kept %v", id, ok)
|
|
}
|
|
}
|
|
tr.keepBody(100, provider.Anthropic, make([]byte, promptBodyMax+1))
|
|
if _, ok := tr.keptBodyOf(100); ok {
|
|
t.Fatal("kept a body over promptBodyMax")
|
|
}
|
|
big := make([]byte, promptBodiesMax/3)
|
|
for id := int64(200); id < 204; id++ {
|
|
tr.keepBody(id, provider.Anthropic, big)
|
|
}
|
|
total := 0
|
|
for _, b := range tr.bodies {
|
|
total += len(b.body)
|
|
}
|
|
if _, ok := tr.keptBodyOf(203); !ok || total > promptBodiesMax {
|
|
t.Fatalf("kept %d bytes", total)
|
|
}
|
|
if _, ok := tr.keptBodyOf(200); ok {
|
|
t.Fatal("kept the oldest past promptBodiesMax")
|
|
}
|
|
}
|
|
|
|
// A chat completions assistant turn is its reasoning, what it said and
|
|
// each call, a piece each; a call's arguments, a JSON string, read laid out.
|
|
func TestPromptTextChatPieces(t *testing.T) {
|
|
body := []byte(`{"model":"m","messages":[
|
|
{"role":"system","content":"be brief"},
|
|
{"role":"user","content":"read it"},
|
|
{"role":"assistant","reasoning_content":"I should read it.","content":"Reading.","tool_calls":[{"id":"c1","type":"function","function":{"name":"read","arguments":"{\"path\":\"a.go\"}"}}]},
|
|
{"role":"tool","tool_call_id":"c1","content":"package a"}]}`)
|
|
b, ok := promptPieces(provider.Chat, body)
|
|
if !ok {
|
|
t.Fatal("not read")
|
|
}
|
|
var got []string
|
|
for _, pc := range b.pieces[PartChat+"\x00"+itemKey("turn", "1")] {
|
|
pc = pc.read()
|
|
got = append(got, pc.Role+"="+pc.Text)
|
|
}
|
|
want := "user=read it|thinking=I should read it.|assistant=Reading.|call={\n \"path\": \"a.go\"\n}"
|
|
if strings.Join(got, "|") != want {
|
|
t.Fatalf("got %q", strings.Join(got, "|"))
|
|
}
|
|
// the sizes are what they were before the text was kept
|
|
if p := promptOf(provider.Chat, body); partOf(p, PartChat).Tokens != func() int {
|
|
n := 0
|
|
for _, it := range b.parts[PartChat] {
|
|
n += it.Tokens
|
|
}
|
|
return n
|
|
}() {
|
|
t.Fatalf("sizes differ")
|
|
}
|
|
}
|