Commit Graph
2 Commits
Author SHA1 Message Date
yetone 5ec2375688 gateway: a call's HTTP headers both ways, masked, folded under its bodies
usrname1833 on Discord asked to see the raw request and response HTTP
headers (查看原始请求和原始响应的http头), to debug as with a capture.
magpie kept the client's request headers and its own response headers
only in the request archive, and nothing of what went upstream.

Now a call keeps its headers both ways (internal/gateway/wire_headers.go):
- the client's, as they reached magpie: headerLogged in Handler() clones
  them for every request that isn't a GET, HEAD or OPTIONS;
- each request magpie sent upstream for the call: method, address,
  headers, and the answer's status, protocol, headers and time, or the
  error. rpmTransport (under s.client) records it as it is sent;
  provider.SendsThroughPlugin records a plugin account's hand-off, shown
  as handed to the plugin, since the plugin's own request to its vendor
  is made in Bun and magpie doesn't see it (the view says so). The first
  8 tries are kept, the rest counted.
Recording copies each header map; nothing is masked or sorted as a
request goes. Secrets are taken out when the headers are read
(Call.Headers): by what each header is (new redact.SecretHeader, now
behind redact.ScrubHeader: Authorization, cookies, x-api-key, api-key,
any *-Key, *-Token, *-Secret, *-Auth, *-Signature, but Idempotency-Key
and a count of tokens such as anthropic-ratelimit-tokens-remaining),
any Bearer or Basic value, and redact.Scrub on the rest; an address
loses its password, a secret-named query value (?key=) and its fragment.
They live with the call in the 40-call ring; /api/gateway/call gives
them as "headers". The request archive keeps the upstream ones as
"upstream" beside the request's and response's headers, masked the same
way. No body is stored that wasn't before.

GUI: under a recent call's bodies on Gateway and a request's on Routing,
"HTTP headers" folds open the client's headers, then each upstream
request beside its answer (wireHeadersView, plugins/usage/page.js); the
archive's copy shows its upstream requests too. Strings in all four
languages. docs/subsystems/gui-shell.md describes it.

Also changed by SecretHeader: the request archive no longer masks a
rate-limit count header (…-tokens-remaining), which the old name rule
took for a token. (provider.SecretHeader, which backups use, is another
function and unchanged.)

Tests: TestRecentCallHeadersBothWays (a fake vendor; the client's key and
cookie, magpie's upstream key and the vendor's Set-Cookie all masked;
status, protocol and response headers kept), TestRecentCallHeadersEachTry
(a 429 then the next key, both kept in order), TestPluginHandOffHeaders,
TestScrubURL, TestSecretHeaderRealNames (real header names both ways),
TestArchiveOnThisComputer and TestRecentCallBodiesOnDemand extended,
call-headers.test.cjs (Gateway, Routing and the archive's copy; chromium
and webkit; en at 1100px, zh, zh-TW, ja, de at 380px and Routing at 440;
nothing out of the box, no left-border accent).

Run (on mote, macOS 27.2, through mote-run, unless named):
- Full suite at ebd22b1b4 (the same change on 5daba8337): gofmt -l
  empty, GOOS=windows go vet ./... ok, GOOS=linux go vet -tags nogui
  ./... ok, go test -count=1 -timeout 45m ./...: every package ok but
  four tests, none in this change's code, each filed:
  TestEveryRouteHasACommandOrIsNamed (internal/cli,
  /api/usage/checkin-account from e87140242, #4826),
  TestCodexPoolSaysTheClientsVersion (#4807: another test's Codex
  identity, passes alone, 3 of 3 here), TestMainWindowCorners/macOS_26_SDK
  (#3949, macOS 27), TestClosingWindowMarksPageOutOfSight (passes
  alone; filed #4924).
- Rebased on a3768cdd1 (it changed internal/gateway, routing/client.js
  and i18n.js): gofmt, go vet, Linux (nogui) and Windows vet of
  internal/... ok; go test of internal/gateway, gui, redact, provider:
  ok but TestCodexPoolSaysTheClientsVersion (#4807, ok 3 of 3 alone
  with this change's gateway tests) and TestMainWindowCorners (#3949);
  TestCoreScriptBudget ok. GUI: call-headers, routing-bodies,
  recent-call-bodies, request-archive and i18n-keys, 58 of 58.
- Rebased again on 2c90922b0 (app.js and internal/provider, no file of
  this change): TestCoreScriptBudget and TestRecentCall* ok;
  i18n-keys plus call-headers 24 of 24 three times out of four at load
  ~130 on mote. The first try had 1 red of 24 whose name was lost
  (its output was grepped, not kept); it did not come back in 3 runs.
- At the pushed head, rebased on 898c314b7, then 3e61d814e and 8d4d8b2d5 (gateway search, redact; vet, internal/redact and the header tests again ok) (app.css, app.js, i18n.js,
  gui-shell.md and internal/gateway changed): gofmt, vet of gateway and
  gui ok; TestCoreScriptBudget, TestRecentCall*, TestRouteCall,
  TestPluginHandOffHeaders, TestArchiveOnThisComputer, TestScrubURL ok;
  i18n-keys, call-headers, routing-bodies, recent-call-bodies 54 of 54.
- call-headers.test.cjs 20 of 20 (chromium and webkit; en at 1100, zh,
  zh-TW, ja, de at 380; Routing at 440), screenshots looked over.
- GUI: not the whole suite. mote had three other sessions' full GUI
  suites running and the Linux box one, so 131 files the change can
  reach (gateway*, routing bodies/log/steady/story/time, recent*,
  call-*, request-archive, masking*, usage*, i18n*, gui-*, account-eye)
  ran on the Windows box: 1739 of 1862. Every red file was run again at
  5daba8337 on the same box: routing-bodies, gateway-card,
  gateway-systemone, masking-demo, usage-ledger's Requests and the rest
  are red there too (Windows WebKit and Windows-only), and account-eye,
  routing-log-icons, usage-ledger-leave and -probe pass alone at this
  change. call-headers' 4 webkit Routing reds on Windows are
  routing-bodies' own (".rt-day" never visible in Windows WebKit).

Broken by hand, each then put back:
- ScrubHeader back on the name rule: TestSecretHeaderRealNames red
  ("a count of tokens: [REDACTED]").
- rpm.go's upstreamBegins taken out: TestRecentCallHeadersBothWays,
  TestRecentCallHeadersEachTry, TestArchiveOnThisComputer and
  TestRecentCallBodiesOnDemand red.
- scrubHeaders off the sent headers: TestPluginHandOffHeaders red with
  the plugin's key shown.
- Gateway client.js's toggle taken out: the 5 chromium recent-call GUI
  tests red.

Not tried:
- The whole GUI suite (above), and the GUI on Linux.
- A real account or the real app: only fakes.
- What a plugin sends its vendor (made in Bun, not seen by magpie; the
  view says so), and headers Go's transport adds itself
  (Accept-Encoding, Content-Length).
- magpie's own requests (System One's decision, titles, search) keep no
  headers; a gateway served by another magpie shows none.

Asked by usrname1833 on Discord.
2026-11-03 02:25:23 +08:00
yetoneandxiaohei-info 3144868538 gui, gateway: the Providers and Gateway pages no longer download every recent call's bodies; an opened call reads its own (#1521, xiaohei-info)
GET /api/providers carried up to 40 recent gateway calls with their
request and response previews (up to 256 KB each) in gateway.calls,
and every provider write returned the same state. The Providers page
never shows them; only an opened row on Gateway does. With 40 calls of
~300 KB each, /api/providers was 21,019,635 bytes on origin/main.

Taken from xiaohei-info's patch (bb26b802, attached to #1521 as
external PRs are closed): providersState drops the bodies from the
calls it sends; GET /api/gateway/call returns one call's previews and
truncation flags with Cache-Control: no-store, 404 once it has left
the ring; the page fetches on open, dedupes a pending read, keeps
loaded bodies across refreshes, drops them with their call, shows
"Fetching…" and a "Try again" on a failed read; the strings in zh,
zh-TW, ja and de; the Go and Playwright tests; the gui-shell note.

Changed from the patch: a call is asked for by a number the gateway
gives it in the ring (Call.ID, Server.RecentCall), not by time, agent
and model, which two calls of one agent to one model can share. The
page keys a row by that id too (callRow), so two such calls no longer
open, or show the bodies of, each other. The tests ask with ids, and
the Playwright fixture gives two calls the same time, agent and model.

The route is on the same mux as the rest of the API, so magpie web's
key guard covers it, and the gateway's own port doesn't serve it. The
bodies are the ones the ring kept, masked as before (redacted runs
before capture). Nothing else reads gateway.calls' bodies: Routing
reads /api/gateway/trace, and the tray, TUI, CLI and /api/state don't
read the ring.

Run:
- Real use: magpie web built from origin/main and from this commit,
  sandbox HOME, a local upstream answering 300 KB, 40 calls of 300 KB.
  /api/providers 21,019,635 bytes before, 45,525 after (calls: 14,503
  bytes). Opening a row in Chromium and WebKit fetched
  /api/gateway/call once (524,412 bytes) and showed both bodies with
  their "first 256 KB" marks. Without the key: 401, also for a
  rebound Host; on the gateway port: 404; an evicted id: 404.
- TestRecentCallBodiesOnDemand fails with the bodies left in
  providersState ("providers must keep call metadata without sending
  bodies"), and with RecentCall ignoring the id ("the second call's
  detail isn't its own").
- recent-call-bodies.test.cjs fails on main's app.js, and with callRow
  keyed by time|agent|model again.
- GUI: gateway-fold, json-tree, phone-rows, recent-calls-more,
  request-archive, recent-call-bodies, sse-body: 24/24 in Chromium and
  24/24 in WebKit (en, zh, zh-TW, ja, de).
- gofmt -l clean; GOOS=linux and GOOS=windows go vet -tags nogui on
  internal/gui and internal/gateway.
- Full GUI suite: 5225 tests, 71 failures under load ~200, none in a
  gateway or recent-call file. The 20 files with failures, rerun on
  this commit and on origin/main: 54 and 58 fail; what fails on one
  only (Sessions checkbox colour, Sessions paging scroll, account
  dragging, ZCode block) moves between runs on both.
- go test -tags nogui ./...: every package ok on the rebased head.

Co-authored-by: xiaohei-info <14011749+xiaohei-info@users.noreply.github.com>
2026-10-11 03:12:46 +08:00