Files
magpie/docs
yetone 17d2ef10fa claude: a conversation's instructions stay after Claude Code's prompt in every interactive run
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: eb8b840dd gave 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 only 541102eba (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.
2026-11-03 12:40:21 +08:00
..