Files
magpie/docs/subsystems
JoeyWu-tech 985b9f1a0a gateway: Reasonix's session-context snapshot is the agent's (#3938)
keeyangyy (#3938) named their client: Reasonix 1.x, which sends no
session header (it sets x-opencode-session on its OpenCode Go paths
only) and opens every conversation on a snapshot it writes, the same
bytes in every conversation started in one workspace while its memory
and skills catalog don't change:

  {"role":"user","content":"<session-context version=\"1\">\nThis host-generated snapshot supersedes every earlier session-context snapshot.\n\n## Environment\n...\nDigest: sha256:<hex>\n</session-context>"},
  {"role":"user","content":"the question the user actually typed"}

userWords didn't know the block, so the commit before this one still
hashed it alone: every conversation a workspace opened had one id, and
under Stays = session each new one was kept on the account that
answered the first.

"<session-context" joins injected, beside Codex's
"<environment_context>", with no closing ">" so that its version
attribute matches. What asks userWords now passes over the snapshot as
it passes over Codex's context:
- the conversation's id runs from the snapshot through what the user
  typed (conversationOpening);
- the turn count: [snapshot][typed] is turn 1, where it was 2
  (turnIn);
- a group's conversation key is the typed words, where it was the
  snapshot, the same for every conversation in the workspace
  (firstWords, in ruleKey and the affinity scope);
- the turn before, which the classifier is told (turnBefore): on a
  conversation's first turn, and on each turn a new snapshot comes
  with, it was the snapshot, workspace and memory and all, as the
  user's previous message with no reply; now it is nothing on a first
  turn, and the user's words before the snapshot and the reply to them
  on a later one;
- the classifier's question, Copilot's Auto and gatewayQuery take the
  user's last words before a snapshot sent after them, and the Claude
  subscription takes one after tool results as the agent's notice
  (agentNotice).

A Reasonix conversation under way when this lands gets an id of its
own on its next turn and is routed once as a new conversation, as the
previous commit says of the others: it may move to another account,
and its cache there starts over.

The shape is the report's, which keeyangyy read from Reasonix's source
(internal/sessioncontext) and didn't capture; its folder and digest
are left out as they left them, and the tests use it as written.

Not changed: the prompt breakdown (promptBuilder.userText) keeps its
own list of Codex's blocks and still files the snapshot as a chat
turn, so a Reasonix conversation's first turn shows there as two
turns, where Codex's context adds none. That is left for a commit of
its own.

What was run (tree 71364f08, on origin/main a5315c70, go1.26.8,
linux/amd64, Linux 5.15), outside graftcp, umask 077:
- With the previous commit's prompt.go and the tests as committed, the
  four tests this commit touches are red:
  TestReasonixConversationsInOneWorkspaceAreApart on every protocol
  ("chat: two conversations opened on one snapshot:
  magpie-310d1b42730f273079337f01 and magpie-310d1b42730f273079337f01
  (the snapshot alone: magpie-310d1b42730f273079337f01)");
  TestNewConversationsInOneFolderAreRoutedApart ("Reasonix's second
  conversation went to k2 as "session", conv
  magpie-310d1b42730f273079337f01; its first is on k2, conv
  magpie-310d1b42730f273079337f01"); TestTurnInSkipsWhatTheAgentPutIn
  ("Reasonix's snapshot, then the user's words: turn 2 within false,
  want 1 false"); TestFirstWordsAreWhatTheUserSaid ("key changed for
  conversation 4").
- Here, those four -count=5: 20 pass.
- The tests of the opening and of what userWords feeds, in
  internal/gateway, sessions, gui and provider (the conversation id,
  dsh's ids, affinity, rotate, strict, OpenCode's sessions, the turn
  count, the classifier's question, firstWords, Copilot's Auto, a
  rule's turn held past an agent's message): ok, 121 top-level tests
  passed, none failed.
- The previous commit's 12 hand-breaks of conversationOpening,
  userOpens and writeOpening, each alone on this tree: each red on the
  same tests as at a6037f68, and the first (the first user message
  hashed alone) also on TestReasonixConversationsInOneWorkspaceAreApart.
- -race -count=5 of the routing and opening tests with the new ones:
  85 pass, no race. -race -count=3 of the two allocation bounds: 6
  pass.
- gofmt -l clean. go vet -tags nogui for linux, windows and darwin, go
  build -tags nogui for linux and darwin, and go vet and go build for
  windows without the tag: exit 0 each. go vet and go build without the
  tag on linux: exit 1 (no libsoup-3.0 or gio-unix-2.0 here).
- go test -tags nogui -count=1 -timeout 45m ./... (Bun 1.4.2, log
  kept; 8m43s, load 28 at its start): 72 packages ok, 5 red, none of
  them this commit's tests or what they cover. internal/gateway ran to
  its end (510s), so the new tests passed in it. The reds: agent
  TestInstalledVersion, TestQoderSettingsFromAnIDEPluginIsNotTheCLI and
  TestOmpDetectedWithPiDir (#3141), and
  TestReasonixCLIRequiresSupportedNativeVersion and
  TestReasonixCLIRecoversAfterProbeTimeout (#3641, the Agents page's
  check of the Reasonix CLI, which this doesn't reach); edit
  TestWriteAtomicReadOnlyHardLink and testenv TestProgram (umask 077,
  #3640); gateway TestPluginToldWhereTheAgentWorks (#3675) and
  TestUploadStillUnderwayIsNotCutByHealthPing (red on main, #3909,
  which has a test-only patch); gui TestGatewayHeldByAContainer and
  TestGatewayFixQuitsOnlyAMagpie (#3649).

Not tried: Reasonix itself, or a body it sent. Its source
(internal/agent/session_context.go, 1.39.10) sends a new snapshot only
when its digest changes, as a user message of its own; the openai
provider magpie writes for it (internal/agent/reasonix.go) sends each
message as a Chat message of its own, and its Anthropic provider,
which merges adjacent user messages, keeps the snapshot a text block of
its own, which userWords reads alone.

Ported with the commit before onto origin/main 5ee5274bd from
JoeyWu-tech/magpie fix/3938-opening-run-4 (02c1de292, f50687a46; posted
on #3938, sha256 7cdb7e83...). The code applied as is; the docs and
extrabody.go's comment were merged by hand beside main's newer text
(${threadId}, ${subagentId}).

What was run for the port (mote, macOS, go1.26.8): the five tests in
conversation_opening_test.go and user_said_test.go's new case, with
the conversation id, dsh, affinity, rotate, strict, sticks and extra
body tests: ok. The same tests on 5ee5274bd (old code, new tests):
TestConversationsOnOneEnvironmentMessageAreApart (chat, messages,
responses, gemini, gemini without roles),
TestReasonixConversationsInOneWorkspaceAreApart,
TestCacheMarkMovingOnKeepsTheID, TestOpeningHashReadsInPlace and
TestNewConversationsInOneFolderAreRoutedApart red.

Batch run, at tree 104d75f0a (the twelve coffemon ports together, on
origin/main 7d022bdb6), mote, macOS, go1.26.8:
- go test -tags nogui -count=1 -timeout 45m on the packages the batch
  changes (agent, autostart, daemon, gateway, library, proc, provider,
  sessions, testenv, tui, gui): ok each.
- GUI, node --test with Playwright 1.62.0, Chromium and WebKit:
  library-*.test.cjs and i18n-keys.test.cjs, 1044 of 1044 pass.
- gofmt -l: nothing. go vet ./..., GOOS=linux go vet -tags nogui ./...,
  GOOS=windows go vet ./... and go build ./...: ok at cdd7c8e46, the same
  batch on origin/main 00098f12b one rebase before.
- The whole go test ./... at cdd7c8e46: 86 packages ok, one red,
  internal/cli TestEveryRouteHasACommandOrIsNamed (GET
  /api/context/reply has no command), red on main too (#5026).
- Linux (wsl-run, Ubuntu 24.04, at 339055896, the batch on f4c664a09):
  proc, autostart, testenv, tui ok; agent: only TestGrokInstalled red
  (#3548); #4033's proc.Look reds (Zed, Copilot, Qoder, Cursor, Omp)
  pass.
- Windows (win-run, Windows 11, at 339055896): proc, autostart, testenv,
  tui ok; library and agent red in the same tests as on origin/main
  f4c664a09 (#4115, #1827, #4786; #5068, #5069, #5070 filed now).
- Rebased onto origin/main fb1f4856a (three commits, no file in
  common with the batch) and run again at the pushed tree: gofmt -l
  nothing, go build ./... and go vet ./... ok; go test -tags nogui
  sessions, agent, autostart, tui, proc, testenv ok; i18n-keys and
  library-* 1044 of 1044 pass.
- Not run before the push: the whole GUI suite (a run at cdd7c8e46 was
  cut off at 1170 of ~1174 tests when mote rebooted; it runs again after
  the tag and any red is filed).
2026-11-03 20:38:11 +08:00
..

Subsystem design references

These pages describe current responsibilities, runtime paths, state, and contracts. Start with the subsystem relevant to your task, then follow its source links to verify the implementation you change.

Subsystem Reference
Built-in subscriptions moving to plugins Provider and plugin ownership
Built-in plugins: registry, lifecycle, plugins.yml, presets, settings schemas, the window's kernel (slots, error boundary, kit) Plugin runtime
Plugins that run on the gateway's requests and replies Gateway middleware
Tools that plugins give, offered to every agent as an MCP server Plugin tools
Plugins that await context recall and observe completed replies Asynchronous gateway request hooks
Claude subscription turns across Claude Code runs Claude subscription bridge
How the gateway picks a provider, key or account, and falls back Gateway routing and fallback
Gateway Session Aggregation Gateway Session Aggregation
Connecting an agent's config to the gateway, drift, disconnect, WSL twins Agent wiring
Providers, keys, subscription accounts, refresh and allowance Providers and accounts
Instructions, MCP servers, skills and RTK written into agents Library
One MCP server, magpie, given to every agent in place of the Library's servers MCP router
Tray, panel, window, magpie web and the page's JSON API GUI app shell
Encrypted backups and setup synchronization over WebDAV, S3 or GitHub Sync and backup

A subsystem can span several packages or repositories. Its reference describes the behavior those parts provide together; it does not need to list every function.

Maintain a reference

Update the owning reference in the same PR when a change alters a documented responsibility, interface, state transition, runtime path, compatibility rule, or failure behavior. An internal refactor that preserves these contracts does not require a prose change.

Keep each fact in one reference and link to it from agent instructions or other pages. Link to source files and named symbols rather than copying implementation details that change frequently. Explain field meanings and constraints that signatures alone do not express.

For a new reference, include the responsibilities and boundaries, source of truth, main runtime paths, state transitions, important constraints, and existing verification commands. Include failure and compatibility behavior where relevant. Document current behavior separately from proposed changes; the proposal belongs in its issue or PR.

Describe a PR's semantic changes

For each affected subsystem, explain the observable change with its trigger and before/after behavior. Include changes to interfaces, state, compatibility, and failure behavior when relevant. Link the affected reference, implementation, and verification evidence. State when a refactor preserves the documented behavior.

For example, a change to plugin adoption should explain which migration state and account condition trigger it, which implementation handles requests before and after adoption, and which test exercises that transition.

This is an author-provided explanation that a reviewer verifies against the diff and surrounding code. It is not an automatically proven compatibility verdict.

Review a change

  1. Identify the affected subsystem and the actual runtime path, including plugin or built-in ownership.
  2. Compare the claimed before/after behavior with the base and head implementation. Trace relevant callers and state transitions beyond the changed lines.
  3. Check that changed contracts appear in the owning reference and that source links still resolve.
  4. Check that the cited tests or runtime evidence exercise the claimed behavior. Report checks that were not run.

A documentation update alone does not prove the implementation. Existing tests and runtime evidence remain necessary for behavior changes.