Files
magpie/internal/gateway/prompt_archive_test.go
T
yetone 0872bc2a0e context: a request's text and reply from the request archive (wangbinyq)
wangbinyq on Discord: with the request archive on, clicking a block of
the context window card still said "只保留最近 10 个请求的原文", since the
card read only the last 10 bodies magpie keeps in memory.

What changed:
- /api/context/text tries memory first, then the request archive's copy
  when the route names one (Route.Archive, the route found in the trace
  or in the history file of the card's ?day=, HistoryRoute looks a day
  either side). gateway.ArchivedPromptText parses it as PromptText does
  and masks it through maskedCopy, as the body in memory is; it is
  marked from "archive" and the card says so.
- New /api/context/reply: the reply to the request, from the Recent
  calls (Server.ReplyBody) or the archive's Response. gateway.ReplyPieces
  reads it into thinking/assistant/call pieces (conversationOutput, split
  out of conversationTurn) after redact.ScrubJSON, and masks every piece
  through maskedCopy. The card's text view has a Reply button beside
  Close that shows it and goes back to the part.
- What is shown is what the archive stores, masked as the memory copy
  would be, nothing more. A body or reply that can't be masked shows
  nothing (404 "unread"); one the archive cut short isn't parsed ("cut");
  one it names and doesn't have says so ("missing"), and one with no
  archive copy keeps the old "only the last 10" line ("off"). A failed
  bucket read is an error, not "gone".
- The archive read is capped at archiveReadMost and the last copy read is
  kept 5 minutes, so the items opened one by one read it once.
- ctxtext.js takes the route (id and time) instead of the id; context.js
  passes it (its one-line change paid for by a comment cut, core budget
  not raised). New strings in zh, zh-TW, ja and de.
- docs/subsystems/gateway-routing.md (Prompt) and gui-shell.md.

Run on f3335dd1b (main has since d73f2254d, textcalls_anthropic.go and
its test only, none of these files), on mote (macOS 27):
- go test -run 'TestArchivedPromptText|TestReplyPieces|TestPromptText|
  TestConversation|TestContext|TestCoreScriptBudget' ./internal/gateway
  ./internal/gui: ok.
- Whole ./internal/gui and ./internal/gateway (before the last rebase,
  same change): ok except TestMainWindowCorners/macOS_26_SDK (#3949) and
  TestCodexPoolSaysTheClientsVersion (#4807, fixed on main by 17f2387e1
  since).
- GOOS=linux go vet -tags nogui and GOOS=windows go vet of both: ok;
  gofmt -l clean.
- GUI, both engines, every language, 1100 and 420 wide: every file that
  reaches the context card (context-text, context-map, context-window,
  context-map-live, context-scroll-end, context-scripts, context-tune,
  ctx-route, compact-one-value, routing-ctx-*, routing-context-fold,
  routing-live-patch, routing-return-cost) plus i18n-keys: 514 of 518.
  The 4 reds are context-map's webkit 420px "two tiles overlap", red the
  same on origin/main f3335dd1b run alone (5 of 10 webkit there), #3488.
- The whole GUI suite is running on the Linux box (wsl-run) as this is
  pushed; mote had another session's full suite on it.

Broken by hand (each went red):
- maskReplyPieces skipped: TestReplyPiecesAreMasked (phone unmasked).
- ArchivedPromptText's masking skipped: TestArchivedPromptTextIsMasked
  and TestContextTextFromTheArchive (phone in the 200 body).
- ctxtext.js's "missing" reason dropped: context-text.test.cjs, 10 of 10
  chromium.

Not tried: a real archived request in the running app, the bucket
(WebDAV) archive with a real server, and a reply cut short by the
archive's size in a real call.
2026-11-03 13:34:35 +08:00

89 lines
3.9 KiB
Go

package gateway
import (
"encoding/json"
"strings"
"testing"
"github.com/yetone/magpie/internal/provider"
"github.com/yetone/magpie/internal/settings"
)
// wangbinyq on Discord: with the request archive on, the context window
// card still said a request's text was gone once it left memory. The
// archive's copy is read instead, masked as the copy in memory is: a phone
// number in it shows as its placeholder, as it would from memory.
func TestArchivedPromptTextIsMasked(t *testing.T) {
t.Setenv("XDG_CONFIG_HOME", t.TempDir())
t.Setenv("XDG_CACHE_HOME", t.TempDir())
if err := settings.Save(settings.Settings{Redact: true, RedactPersonal: true}); err != nil {
t.Fatal(err)
}
const phone = "13800138000"
body := []byte(`{"model":"m","system":"You are the release agent.","messages":[
{"role":"user","content":"call me at ` + phone + ` once it ships"},
{"role":"assistant","content":"Shipping."}]}`)
pt, ok := ArchivedPromptText(provider.Anthropic, body, 7, PartChat, true, 0)
if !ok || pt.From != "archive" || len(pt.Items) == 0 {
t.Fatalf("%v %+v", ok, pt)
}
b, _ := json.Marshal(pt)
if strings.Contains(string(b), phone) || !strings.Contains(string(b), "once it ships") {
t.Fatalf("archived text %s", b)
}
sys, ok := ArchivedPromptText(provider.Anthropic, body, 7, PartSystem, true, 0)
if b, _ := json.Marshal(sys); !ok || !strings.Contains(string(b), "You are the release agent.") {
t.Fatalf("system %s", b)
}
// a body cut short reads as nothing
if _, ok := ArchivedPromptText(provider.Anthropic, []byte(`{"model":`), 8, PartChat, true, 0); ok {
t.Fatal("read a body cut short")
}
}
// The reply beside the prompt: its text, its thinking and its calls, from
// a whole reply or a stream, masked as the prompt's text is.
func TestReplyPiecesAreMasked(t *testing.T) {
t.Setenv("XDG_CONFIG_HOME", t.TempDir())
t.Setenv("XDG_CACHE_HOME", t.TempDir())
if err := settings.Save(settings.Settings{Redact: true, RedactPersonal: true}); err != nil {
t.Fatal(err)
}
const phone = "13800138000"
whole := `{"id":"msg_1","type":"message","role":"assistant","model":"m1","content":[
{"type":"thinking","thinking":"The user wants a call."},
{"type":"text","text":"I will call ` + phone + ` when it ships."},
{"type":"tool_use","id":"c1","name":"Deploy","input":{"env":"prod","notify":"` + phone + `"}}],
"stop_reason":"tool_use","usage":{"input_tokens":3,"output_tokens":9}}`
pieces, cut, ok := ReplyPieces(provider.Anthropic, []byte(whole), false)
if !ok || cut {
t.Fatalf("%v %v", ok, cut)
}
var roles []string
for _, p := range pieces {
roles = append(roles, p.Role+":"+p.Name)
if strings.Contains(p.Text, phone) {
t.Fatalf("%s unmasked: %s", p.Role, p.Text)
}
}
if got := strings.Join(roles, " "); got != "thinking: assistant: call:Deploy" {
t.Fatalf("pieces %s", got)
}
if !strings.Contains(pieces[1].Text, "when it ships") || !strings.Contains(pieces[2].Text, `"prod"`) {
t.Fatalf("%+v", pieces)
}
stream := sse(
"event: message_start\ndata: "+`{"type":"message_start","message":{"id":"msg_1","type":"message","role":"assistant","model":"m1","content":[],"usage":{"input_tokens":3,"output_tokens":0}}}`,
"event: content_block_start\ndata: "+`{"type":"content_block_start","index":0,"content_block":{"type":"text","text":""}}`,
"event: content_block_delta\ndata: "+`{"type":"content_block_delta","index":0,"delta":{"type":"text_delta","text":"Reach me at `+phone+`."}}`,
"event: content_block_stop\ndata: "+`{"type":"content_block_stop","index":0}`,
"event: message_stop\ndata: "+`{"type":"message_stop"}`)
pieces, _, ok = ReplyPieces(provider.Anthropic, []byte(stream), false)
if !ok || len(pieces) != 1 || pieces[0].Role != "assistant" || strings.Contains(pieces[0].Text, phone) || !strings.Contains(pieces[0].Text, "Reach me at") {
t.Fatalf("stream %v %+v", ok, pieces)
}
if _, _, ok := ReplyPieces(provider.Anthropic, nil, false); ok {
t.Fatal("read an empty reply")
}
}