m4rs0n on Discord: since #2432 (eb8b840dd) a Claude subscription conversation in the interactive mode broke the prompt cache as it grew. The same body sent again read 100% from the cache, but the same body plus an assistant "ok" and a new user turn read only 1,273 of 8,565 tokens and wrote 7,290. A tool loop read a constant 1,273 every call (opus 1,273, sonnet 1,197, haiku 2,000: Claude Code's own system prompt). Cause:eb8b840ddgave a one-off ask (no tools, no reply) its caller's instructions with --append-system-prompt-file, after Claude Code's own prompt, and lets that run go after its reply. Every other run (with tools, with a reply, a resumed or replayed session) kept them in the first pasted message. So the conversation's next request started a run whose system block ended at Claude Code's prompt and whose first message held the instructions. The cached prefix broke right after Claude Code's prompt, and the rest was written again. A tool loop's runs did the same: a pasted first message loses magpie's mark (the paste is one text). The bisect v0.1.2676..v0.1.2677 has only541102eba(the onboarding mark), which doesn't touch the request. Before it, those runs were -p, which keeps the marks. Change: - appendsPrompt no longer requires no tools and no reply. Every interactive run that isn't lean and has no client cache marks appends the instructions. -p keeps them in the first message, where its mark holds. - A run that resumes a session (shelved, branched, branchAt, or a replaySession transcript) is given the file again when the session's run had it (savedSession.appended). Sessions keep no system prompt, and every run key holds the system prompt, so the file is the one the session's run had. Appended runs can now be shelved and branched, which they couldn't before. - replaySession leaves the instructions out of the transcript's user line when they are appended. - docs/subsystems/claude-subscription-bridge.md says so. Tests: - TestClaudeTUIConversationKeepsItsInstructionsWhereTheFirstAskHadThem is the reporter's sequence: A, then A + "ok" + "Now say two words." Both runs are given the same append file, which starts with the rules. No run is typed them, and no session file holds them. - TestClaudeTUIResumedSessionIsGivenItsInstructions: a shelved session is resumed with its append file. - TestClaudeAppendInTheInteractiveMode replaces TestClaudeAppendOnlyForInteractiveOneOffs. Tools and reply append in the interactive mode; client marks and -p don't. - TestClaudeTUIReplayGoesInATranscript now expects the rules out of the transcript's user line and the append flag on the run. Run at the pushed head, on mote: - go test ./internal/gateway -run 'Claude|TUI|Subscription|Replay|Branch|Lean|Append|Shelve|Resume': ok. - GOOS=linux and GOOS=windows go vet ./internal/gateway: ok. - gofmt -l: clean. - The full internal/gateway package was red once, before the replay test was updated, with the failing test cut off by a tail. A rerun with the whole log is under way. Its result goes in the report and, if red, in an issue. Broken by hand: the new tests on origin/main's code fail. - TestClaudeAppendInTheInteractiveMode/tools and /reply fail. - The conversation test fails ("given ... and <none> after Claude Code's system prompt, want the same instructions"). - The resumed test fails. - With only the savedSession.appended carry removed, the resumed test fails ("<none> ... want A's instructions"). Not tried: - A real Claude account. The sandbox recipe needs a live login's OAuth token copied out, and rotated tokens written back, which this session's rules forbid. The prefix claim rests on Claude Code 2.1.316 marking the end of its system block itself (recorded 2026-11-03 for #2432), not on a new real request. - Windows and WSL runs of the interactive mode. Sibling paths: - Streaming and non-streaming both start runs through startRun. - count_tokens doesn't write the cache. - An API-key Claude request is passed through with the client's own marks, outside this bridge.
234 lines
9.6 KiB
Go
234 lines
9.6 KiB
Go
package gateway
|
|
|
|
import (
|
|
"fmt"
|
|
"os"
|
|
"path/filepath"
|
|
"strings"
|
|
"testing"
|
|
|
|
"github.com/yetone/magpie/internal/claudetui"
|
|
"github.com/yetone/magpie/internal/testenv"
|
|
)
|
|
|
|
// appendHarness is a script harness whose Claude Code keeps the file it is
|
|
// given by --append-system-prompt-file, in Claude Code's interactive mode
|
|
// (through a stand-in helper) when tui says so. appended is each run's
|
|
// file by the run's log.
|
|
func appendHarness(t *testing.T, tui bool) (*resumeHarness, func() map[string]string) {
|
|
t.Helper()
|
|
kept := t.TempDir()
|
|
h := newScriptHarness(t, "ok", `prev=
|
|
for a in "$@"; do
|
|
if [ "$prev" = "--append-system-prompt-file" ]; then cp "$a" `+kept+`/$$; fi
|
|
prev=$a
|
|
done`)
|
|
if tui {
|
|
claudeTUIRuns = true
|
|
t.Cleanup(func() { claudeTUIRuns = false })
|
|
helper := filepath.Join(t.TempDir(), "magpie")
|
|
testenv.Program(t, helper, `#!/bin/sh
|
|
[ "$1" = "`+claudetui.Command+`" ] || exit 9
|
|
shift; claude=$1; shift
|
|
exec "$claude" "$@"
|
|
`)
|
|
claudeTUIHelper = helper
|
|
t.Cleanup(func() { claudeTUIHelper = "" })
|
|
}
|
|
return h, func() map[string]string {
|
|
out := map[string]string{}
|
|
for pid, log := range h.runs() {
|
|
b, err := os.ReadFile(filepath.Join(kept, pid))
|
|
if err != nil {
|
|
out[log] = "<none>"
|
|
continue
|
|
}
|
|
out[log] = string(b)
|
|
}
|
|
return out
|
|
}
|
|
}
|
|
|
|
const classifierRules = "You are a security monitor. Block destructive actions; the rules run long."
|
|
|
|
// The auto mode classifier's asks, one-off asks under the same long
|
|
// instructions, are pasted into the interactive mode as one text, so the
|
|
// instructions lost their cache mark and every ask wrote them to the
|
|
// cache anew: 80k tokens written and 1,197 read an ask, the same account
|
|
// and conversation, no 429 (#2432, laozhang713 and j1374615791-lab). They
|
|
// go after Claude Code's own system prompt now, in the block it marks
|
|
// itself, and the ask's first message has the action only.
|
|
func TestClaudeTUIOneOffAskAppendsInstructions(t *testing.T) {
|
|
h, appended := appendHarness(t, true)
|
|
for _, action := range []string{"rm -rf build", "git push --force"} {
|
|
h.post(`{"model":"claude-sonnet-5","max_tokens":64,"system":"` + classifierRules + `","messages":[{"role":"user","content":"<transcript>` + action + `</transcript>"}]}`)
|
|
}
|
|
runs := appended()
|
|
if len(runs) != 2 {
|
|
t.Fatalf("%d runs, want 2: %v", len(runs), runs)
|
|
}
|
|
for log, file := range runs {
|
|
// a test's request comes from another computer: where its tools
|
|
// run follows the instructions, as in a lean run's
|
|
if !strings.HasPrefix(file, classifierRules+"\n\n") || !strings.HasSuffix(file, strings.TrimSpace(envUnknownNote)) {
|
|
t.Errorf("a one-off ask's run was given %q after Claude Code's system prompt, want the caller's instructions\n%s", file, log)
|
|
}
|
|
if strings.Contains(log, "external_system_instructions") || strings.Contains(log, "security monitor") {
|
|
t.Errorf("a one-off ask's first message still has the caller's instructions:\n%s", log)
|
|
}
|
|
if !strings.Contains(log, "rm -rf build") && !strings.Contains(log, "git push --force") {
|
|
t.Errorf("a one-off ask's first message lost the action:\n%s", log)
|
|
}
|
|
}
|
|
}
|
|
|
|
// Every interactive run has the caller's instructions after Claude Code's
|
|
// system prompt, an agent's turn (tools) and a conversation's (a reply
|
|
// before it) too: pasted, they lost their cache mark, and a run started
|
|
// anew read nothing past Claude Code's own prompt. -p takes the client's
|
|
// marks, and a client that places its own keeps them in the first
|
|
// message, so those are unchanged.
|
|
func TestClaudeAppendInTheInteractiveMode(t *testing.T) {
|
|
for _, c := range []struct {
|
|
name string
|
|
tui bool
|
|
appended bool
|
|
body string
|
|
header []string
|
|
}{
|
|
{"tools", true, true, `{"model":"claude-sonnet-5","max_tokens":64,"system":"` + classifierRules + `","tools":[{"name":"read","input_schema":{"type":"object"}}],"messages":[{"role":"user","content":"hi"}]}`, nil},
|
|
{"reply", true, true, `{"model":"claude-sonnet-5","max_tokens":64,"system":"` + classifierRules + `","messages":[{"role":"user","content":"hi"},{"role":"assistant","content":"ok"},{"role":"user","content":"again"}]}`, nil},
|
|
{"client marks", true, false, `{"model":"claude-sonnet-5","max_tokens":64,"system":[{"type":"text","text":"` + classifierRules + `","cache_control":{"type":"ephemeral"}}],"messages":[{"role":"user","content":"hi"}]}`, []string{"X-Magpie-Cache-Marks", "client"}},
|
|
{"print", false, false, `{"model":"claude-sonnet-5","max_tokens":64,"system":"` + classifierRules + `","messages":[{"role":"user","content":"hi"}]}`, nil},
|
|
} {
|
|
t.Run(c.name, func(t *testing.T) {
|
|
h, appended := appendHarness(t, c.tui)
|
|
h.post(c.body, c.header...)
|
|
runs := appended()
|
|
if len(runs) != 1 {
|
|
t.Fatalf("%d runs, want 1: %v", len(runs), runs)
|
|
}
|
|
for log, file := range runs {
|
|
if !c.appended {
|
|
if file != "<none>" {
|
|
t.Errorf("given %q after Claude Code's system prompt, want nothing\n%s", file, log)
|
|
}
|
|
if !strings.Contains(log, "security monitor") {
|
|
t.Errorf("the first message lost the caller's instructions:\n%s", log)
|
|
}
|
|
continue
|
|
}
|
|
if !strings.HasPrefix(file, classifierRules+"\n\n") {
|
|
t.Errorf("given %q after Claude Code's system prompt, want the caller's instructions\n%s", file, log)
|
|
}
|
|
if strings.Contains(log, "security monitor") || strings.Contains(log, "external_system_instructions") {
|
|
t.Errorf("the run was typed the caller's instructions too:\n%s", log)
|
|
}
|
|
}
|
|
if c.appended {
|
|
for _, f := range h.sessionFiles() {
|
|
if strings.Contains(f, "security monitor") {
|
|
t.Errorf("a session the run resumes holds the caller's instructions, which it has after Claude Code's prompt:\n%s", f)
|
|
}
|
|
}
|
|
}
|
|
})
|
|
}
|
|
}
|
|
|
|
// sessionFiles is the words of each session file there is.
|
|
func (h *resumeHarness) sessionFiles() []string {
|
|
files, _ := filepath.Glob(filepath.Join(h.config, "projects", "*", "*.jsonl"))
|
|
var out []string
|
|
for _, f := range files {
|
|
b, _ := os.ReadFile(f)
|
|
out = append(out, string(b))
|
|
}
|
|
return out
|
|
}
|
|
|
|
// m4rs0n on Discord, straight to the gateway with one long cached system
|
|
// block and no tools: the conversation's first request, sent again as it
|
|
// was, read all of it from the cache, but the request after it, the same
|
|
// one with the reply and one more user message, read 1,273 of 8,565 and
|
|
// wrote the rest, as did every request of a loop that went on from there.
|
|
// The first was a one-off ask, its instructions after Claude Code's system
|
|
// prompt and its run let go after the reply; the second's run was given
|
|
// the conversation as its transcript with the instructions in its first
|
|
// message, so what it sent differed from the first's right after Claude
|
|
// Code's own prompt. Every run of the conversation is given the same
|
|
// system prompt now, the caller's instructions after Claude Code's, and
|
|
// none has them in its messages.
|
|
func TestClaudeTUIConversationKeepsItsInstructionsWhereTheFirstAskHadThem(t *testing.T) {
|
|
h, appended := appendHarness(t, true)
|
|
first := `{"role":"user","content":"Say ok."}`
|
|
h.post(`{"model":"claude-sonnet-5","max_tokens":16,"system":"` + classifierRules + `","messages":[` + first + `]}`)
|
|
h.post(`{"model":"claude-sonnet-5","max_tokens":16,"system":"` + classifierRules + `","messages":[` + first + `,{"role":"assistant","content":"ok"},{"role":"user","content":"Now say two words."}]}`)
|
|
runs := appended()
|
|
if len(runs) != 2 {
|
|
t.Fatalf("%d runs, want 2: %v", len(runs), runs)
|
|
}
|
|
var files []string
|
|
for log, file := range runs {
|
|
files = append(files, file)
|
|
if strings.Contains(log, "security monitor") || strings.Contains(log, "external_system_instructions") {
|
|
t.Errorf("a run was typed the caller's instructions, which the conversation's first run had after Claude Code's prompt:\n%s", log)
|
|
}
|
|
}
|
|
if files[0] != files[1] || !strings.HasPrefix(files[0], classifierRules+"\n\n") {
|
|
t.Errorf("the conversation's runs were given %q and %q after Claude Code's system prompt, want the same instructions", files[0], files[1])
|
|
}
|
|
for _, f := range h.sessionFiles() {
|
|
if strings.Contains(f, "security monitor") {
|
|
t.Errorf("the transcript the second run goes on from holds the caller's instructions:\n%s", f)
|
|
}
|
|
}
|
|
}
|
|
|
|
// A conversation's saved session, resumed past idleMost, is given the
|
|
// caller's instructions after Claude Code's system prompt as its run had
|
|
// them: the session keeps no system prompt, and before, a run that had
|
|
// them so was never shelved, so its next turn was a new run that wrote
|
|
// the whole conversation to the cache again.
|
|
func TestClaudeTUIResumedSessionIsGivenItsInstructions(t *testing.T) {
|
|
h, appended := appendHarness(t, true)
|
|
h.ask("rules A", "a1")
|
|
var sidA string
|
|
for pid := range h.runs() {
|
|
sidA = "s" + pid
|
|
}
|
|
for i := range idleMost {
|
|
h.ask(fmt.Sprintf("rules %d", i), "x")
|
|
}
|
|
eventually(t, "A's run still going", func() bool { return len(h.s.subscription.idle) == idleMost })
|
|
before := h.runs()
|
|
h.ask("rules A", "a1", "a2")
|
|
for log, file := range appended() {
|
|
if _, ok := before[logPID(h, log)]; ok {
|
|
continue
|
|
}
|
|
if !strings.Contains(log, "--resume "+sidA) {
|
|
t.Fatalf("A's next turn did not resume its session:\n%s", log)
|
|
}
|
|
if !strings.HasPrefix(file, "rules A\n\n") {
|
|
t.Errorf("the resumed run was given %q after Claude Code's system prompt, want A's instructions\n%s", file, log)
|
|
}
|
|
told := log[strings.Index(log, "\ntold "):]
|
|
if !strings.Contains(told, "a2") || strings.Contains(told, "a1") || strings.Contains(told, "rules A") {
|
|
t.Fatalf("the resumed run was told more than the turn:\n%s", told)
|
|
}
|
|
return
|
|
}
|
|
t.Fatal("A's next turn started no run")
|
|
}
|
|
|
|
// logPID is the run whose log is log.
|
|
func logPID(h *resumeHarness, log string) string {
|
|
for pid, l := range h.runs() {
|
|
if l == log {
|
|
return pid
|
|
}
|
|
}
|
|
return ""
|
|
}
|