magpie icon indicating copy to clipboard operation
magpie copied to clipboard

[Bug] Session affinity: a fixed first user message (environment context) makes every conversation the same one

Open keeyangyy opened this issue 1 month ago • 6 comments

Version: 0.1.1763 (also present on 508911e4)

What happens

When a client sends no session header, magpie identifies a conversation by the first role: "user" message alone: conversationID (internal/gateway/gateway.go:6867) walks messages / contents / input, takes the first item whose role is user (lines 6895-6904), and hashes that message's raw bytes into magpie-<12 hex>. stickKey (internal/gateway/affinity.go:467) then scopes it per provider and sub-agent.

Some clients put a fixed, system-like block in exactly that position, e.g.

{"messages": [
  {"role": "system", "content": "..."},
  {"role": "user",   "content": "<environment_context>cwd=/w/proj ...</environment_context>"},
  {"role": "user",   "content": "the question the user actually typed"}
]}

The <environment_context> message is byte-identical in every conversation started from the same cwd, so every new conversation hashes to the same magpie-... id.

Consequences:

  • Stays = session: a new conversation inherits the account that answered the first one, so new conversations never move to another account and affinity.json never gains a new key.
  • routing = rotate: its counter never advances either - in affine() the AffinitySession branch (affinity.go:528) is taken before the AffinityTurn, rotate one (:532), so rotate degenerates into "always the same account".
  • Only per-request strategies (usage / pace / fast) still spread load, and without cache locality.

Steps to reproduce

  1. A provider with two or more accounts, Affinity = session, any routing.
  2. Point a client at magpie that sends a fixed first user message (environment context / user instructions) and the real prompt as a second user message.
  3. Open several new conversations one after another, then compare the conv field in the Routing trace (or the keys in affinity.json): all of them are the same magpie-..., and all of them are served by the same account.

Observed here on a provider with 4 accounts: opening several new sessions in a row, every one landed on the same account.

Proposal

Read the first two user messages instead of one, skipping the ones that are not user text (Anthropic clients send tool results as role: "user" with tool_result blocks; hashing those would change the id every turn):

  • two text user messages exist: hash both, in order;
  • only one: hash it, as today.

One transition to handle: the id changes the first time a session gains its second user message. Cheap fix: when the two-message id has no stick record, look the one-message id up and migrate that record to the new key, so stickiness survives and only differs across conversations.

If changing the default is too invasive, a per-provider choice ("identify a conversation by the first / first two user messages"), defaulting to today's behaviour, would be enough.

keeyangyy avatar Oct 27 '26 18:10 keeyangyy

I'll take this one.

yinwu01 avatar Oct 27 '26 18:10 yinwu01

Patch: https://github.com/JoeyWu-tech/magpie/tree/fix/3938-opening-run-2, commit a6037f68 on origin/main e9dd8e83 (applies as is on f25871cd), git format-patch --no-signature --full-index --stdout e9dd8e83..a6037f68 | sha256sum is d3b2c3da66bdd469b11f6baef10ce5a04097e85a931647938eebf6035083c082.

Thanks for tracing it to the line. When the first user message is one the agent wrote (magpie already tells an <environment_context> or <user_instructions> block from what the user typed), the id now covers the user messages from it through the first one that has the user's own words, an image or a file, so two conversations from one folder get different ids and each keeps its id on later turns. A reply, a tool call or a tool result ends that run, so tool results never move the id, and in your shape neither does a cache mark moving on to the newest message; a conversation whose first user message is already the user's own keeps exactly the id it has today. I didn't go with the first two user messages because in an ordinary conversation the second one is the next turn, so the id would move as soon as it arrives and every record would need the migration you describe; this way the id is settled before the first reply and doesn't move after, so there's nothing to migrate. The new test sends your example through the gateway with rotate and Stays = session, and the second conversation now goes to the other key. Not tried with your client itself, though: which one is it? If its fixed block isn't one magpie already knows, the id stays as it was, so a request body from it would help.

yinwu01 avatar Oct 28 '26 06:10 yinwu01

Client: Reasonix 1.x (Go; desktop + CLI). It sends no session header at all - it only sets x-opencode-session on the OpenCode Go paths - so the body is all the gateway has.

Its first user message is a host-generated block:

{"messages": [
  {"role": "system", "content": "<the agent's system prompt>"},
  {"role": "user",   "content": "<session-context version=\"1\">\nThis host-generated snapshot supersedes every earlier session-context snapshot.\n\n## Environment\n- OS: windows/amd64\n- Shell: powershell\n\n## Workspace\nCurrent workspace: \"<dir>\"\n\n## Background memory\n...\n\n## Skills catalog\n...\n\nDigest: sha256:<hex>\n</session-context>"},
  {"role": "user",   "content": "the question the user actually typed"}
]}

That shape is reconstructed from the client's source (internal/sessioncontext), not captured from the wire; the directory and the digest are redacted.

In a fresh session from the same workspace the block is byte-identical - same environment, same workspace, same memory, same skills catalog, same digest - so under the patch it has to be read as the agent's own message. As far as I can tell <session-context ...> is not in userWords' injected list (internal/gateway/prompt.go:725), so as the patch stands this client keeps the old id: every new conversation is still kept on the account that answered the first one under Stays = session.

If a body captured from the client itself would help more than the shape above, say so and I will take one (redacted).

keeyangyy avatar Oct 28 '26 08:10 keeyangyy

Patch: https://github.com/JoeyWu-tech/magpie/tree/fix/3938-opening-run-4, two commits on origin/main a5315c70 ending at f50687a4 (applies as is on efb55b5c), git format-patch --no-signature --full-index --stdout a5315c70..f50687a4 | sha256sum is 7cdb7e8376f244f500207d422222f55f0567980752765b350c261c362d7c779d. The first commit is a6037f68 rebased, the same code; the second is new.

Thanks, that was the missing piece. The second commit adds <session-context to that list (no closing >, so the version attribute still matches), so Reasonix's snapshot is read as the agent's message, the way Codex's <environment_context> is. With your body as you wrote it, through the gateway with rotate and Stays = session, the second conversation from the same workspace now goes to the other key, and each one's next turn stays where it began; without the new line it goes back to the first one's key as "session". The other places that go by userWords skip the snapshot too, so the turn count and a group's conversation key come from what the user typed instead.

I read Reasonix's source too (internal/agent/session_context.go): it sends a new snapshot only when the digest changes, as a message of its own (on Anthropic, a text block of its own), so later ones are read as the agent's as well and don't move the id. If you can try the branch with Reasonix itself, the thing to look at is whether a second conversation from the same workspace now lands on the other key. A redacted body from a real run would help too.

yinwu01 avatar Oct 28 '26 10:10 yinwu01

Thanks - I will skip the field test for now: it means pointing my client at a test instance, and I would rather not churn that setup. I will check it once this lands in a release; if a second conversation from the same workspace still lands on the first one's key by then, I will capture a redacted body and add it here.

keeyangyy avatar Oct 28 '26 11:10 keeyangyy

Ported @yinwu01's two commits: 55a190f19 and 985b9f1a0, in v0.1.2855. A conversation that opens on a fixed message its agent writes itself (an <environment_context> or <user_instructions> block, an instruction file) no longer gets the same id as every other one opened so, and Reasonix's session-context snapshot is read as the agent's, not the user's. Existing conversations keep their ids (no stick record moves); the first new one on v0.1.2855 starts its own. The commits' tests are red on the old gateway.

yetone avatar Nov 03 '26 12:11 yetone