Commit Graph
5 Commits
Author SHA1 Message Date
ggbdpq 0d76d16ec6 test(gateway): let only the gateway's deadline end a stall's fake answer
What changed: the fake relays in TestStalledAnswerSaysUrlOrB64 and
TestVendorThatStopsMidAnswerIsSaid waited on r.Context().Done() with a
time.After(5 * time.Second) fallback of their own. Under load the fake's
5s could end its answer before the gateway's 500ms stall deadline
(drawUnit = 100ms) was acted on, so the gateway read a cut body and said
502 "isn't an images API's: unexpected end of JSON input" instead of the
stall's 504 (#4245). The fakes now wait on the context alone: the
deadline's cancel is what ends them, so the order can't flip. No product
behavior changes; both tests still assert the same 504 wording.

Run: go test ./internal/gateway -run
'TestStalledAnswerSaysUrlOrB64|TestVendorThatStopsMidAnswerIsSaid'
-count=5 - ok.
go test -tags nogui -timeout 45m ./internal/gateway/... - ok (500s).
go vet ./... and GOOS=windows go vet ./... - ok; GOOS=linux go vet ./...
fails in wails (undefined: pointer) at the base commit too.
CGO_ENABLED=0 GOOS=linux|windows|darwin go build -tags nogui . - ok.

Generated-by: GLM-5.3-Flash (ZCode)
2026-11-01 10:22:57 +08:00
yetone 197849ad4d gateway: image generation's timeout is a setting, 1 to 60 minutes
neighbads on X: a slow image endpoint needed more than the fixed 5
minutes. Settings > Images > Image generation timeout (imageTimeout in
settings.json, 5 when unset, 1 to 60 accepted, anything else a 400) now
gives every drawing that long, on each route it takes:

- drawTimeout() reads the setting; drawWithin marks the context as a
  drawing and sets its deadline. Error texts say the time it was given
  (givenTo), ModelScope's task polling too.
- A marked drawing goes through a vendor transport with no
  ResponseHeaderTimeout (drawsApart), since an images API sends its
  headers only once it has drawn; everything else keeps the 10-minute
  upstreamHeaderWait, so a 30-minute setting isn't cut at 10.
- An image answered as a link is downloaded within the same time
  (imageLongest, never under ImageLongest).
- Videos keep 5 minutes per call (videoTimeout): a video is made between
  its calls, so the setting isn't theirs.
- magpie mcp image waits on a call up to callLongest (2h1m, past the
  60-minute bound twice over), since it may not read the settings (WSL);
  ToolTimeout, which agent configs carry, is unchanged.
- The plugin path: Bun 1.4.2's fetch has no 5-minute cap (tried against
  a server answering after 330s), so nothing changes there.
- api.go keeps the saved value when a page doesn't send imageTimeout;
  prefsKeep carries it on every other Settings save.

GUI: a segmented row under Image generation (5, 10, 15, 30, 60 min; a
value set by hand gets its own segment), in en, zh, zh-TW, ja and de.

Three GUI tests clicked controls the new row pushed under the window's
foot or past the viewport; they now wheel to them with reader.cjs's
reach (disabled-models, search-api, refusal-retries-setting).
searcher-pick checks the help has no title before reach's pointer
passes over it (the app's mouseover sets one on cut-short text).

Run, at 224ea7d5b plus this change (origin/main 9fd80b212 fetched):
- gofmt -l: clean. go vet ./..., GOOS=linux go vet -tags nogui ./...,
  GOOS=windows go vet ./...: clean. GOOS=linux go build -tags nogui and
  GOOS=windows go build: ok.
- go test -p 4 -timeout 45m ./...: 64 packages ok, exit 0.
- New tests: TestLongImageTimeoutLetsASlowVendorFinish (drawUnit and
  upstreamHeaderWait shrunk, no sleeps: a vendor slower than the header
  wait succeeds when given 30, is a 504 at the default),
  TestImageTimeoutSettingReachesTheVendor, TestImageDownloadGetsTheSetMinutes,
  TestOnlyDrawingsWaitPastTheHeaderWait, TestImageTimeoutKeptAndBounded,
  TestCallOutwaitsTheLongestDrawing, TestSettingsImageTimeout,
  image-timeout-setting.test.cjs (5 languages x 1100/420 x chromium and
  webkit, plus a hand-set 45 min kept across another save).
- Broken by hand, each red, then restored: drawsApart sending everything
  through the header-timeout transport (2 red); drawTimeout fixed at 5
  minutes (2 red); callLongest back to ToolTimeout (red at 11m); the
  settings bound at 600 ("61 minutes accepted"); imageTimeout left out of
  prefsKeep (hand-set test red, wanted 45); api.go's keep removed
  ("imageTimeout 0").
- GUI, NODE_PATH at playwright 1.62.0, chromium and webkit: the 115 files
  that reach Settings or the changed assets, 1646 tests, 1627 pass. Of
  the 18 reds: codex-predictions (420, every language) and s3-sync are
  red on 224ea7d5b's own copy too, untouched here; refusal-retries,
  disabled-models, search-api and searcher-pick were the clicks fixed
  above; sidebar's "the list opens and folds as it plays" (chromium)
  samples getAnimations() once and missed the unroll under a load of
  ~175. Rerun of those five files plus image-timeout-setting: 104 of
  104 pass.

Not tried: a real slow image vendor; WSL; the Windows box; Bun other
than 1.4.2; the GUI files that can't reach Settings.
2026-10-19 21:47:35 +08:00
yetone ea3dee96a8 gateway: a URL magpie didn't choose is downloaded from public addresses only, wherever the request came from
5241de909 held an image URL a caller names to public addresses. This
carries the same guarantee to every URL magpie downloads that isn't its
own fixed vendor endpoint, and drops where the request came from as a
reason to reach a private address.

What is downloaded through the one restricted client now (every resolved
address checked and pinned, the dialled address checked again, each of up
to 5 redirects checked the same; loopback, RFC 1918, fc00::/7, link-local
with metadata, CGNAT, multicast, unspecified, documentation/reserved,
IPv4-mapped, NAT64/6to4/Teredo forms all refused):
- a caller's reference image (fetchPicture, draw.go), as before;
- web_fetch's page (search_fetch.go), as before;
- an image a vendor answers by URL, downloaded for a b64_json caller
  (draw.go) - was the gateway's unrestricted client;
- /v1/videos/{id}/content's video URL from the vendor's status
  (video.go) - was the gateway's unrestricted client;
- generate_image's own download of the gateway's image URL
  (internal/imagemcp) - was http.DefaultClient.
A vendor's answer can be steered by what the model is asked to do, so
its URLs are treated as a caller's.

Policy: a request from this computer gets nothing more. The exemption for
a loopback request that wasn't from a web page or through a forwarding
header (withPrivateFetch/privateFetchAllowed) is gone: a reverse proxy or
relay on the same host makes every request it carries arrive that way.
Two explicit allowances remain:
- settings.json "fetchPrivate" (settings.FetchPrivate): names, name:port,
  IPs and CIDR ranges the user lets magpie download from. None by
  default, no Settings page, per-machine (KeepOwn, kept across a GUI
  save). A settings.json that can't be read lets nothing extra in.
- for a vendor's URL only, the host:port of that provider's own
  configured endpoints (a LAN or local provider serving its own images).
The refusal says to send the image itself or name the host in
fetchPrivate. A vendor image that can't be downloaded is still answered
by its URL; a video is a 502.

Audited and left alone: kiro, codex_search, codex_backend and the
catalog/provider http.DefaultClient uses (fixed vendor endpoints);
remoteVideoURL (p.Do to the provider); the MCP proxy and mcpauth (URLs
the user configured); community plugins (only fixed vendor endpoints,
nothing a request names).

Docs: docs/subsystems/gateway-routing.md (the guard paragraph, web_fetch,
test command), docs/subsystems/sync-backup.md (FetchPrivate stays on
each computer).

Tests (new or rewritten): TestEditImageURLFromThisComputer (loopback,
[::1], and loopback with X-Forwarded-For as a same-host reverse proxy:
400 naming fetchPrivate, the private server never asked; with
fetchPrivate ["127.0.0.1"] it is fetched, a non-image still refused),
TestFetchPrivateEntries, TestVendorImageURLAtPrivateAddressNotFetched
(127.0.0.1, a name resolving to it, ::ffff:127.0.0.1; the vendor's own
host:port still fetched), TestVideoContentAtPrivateAddressNotFetched,
imagemcp TestVendorImageAtPrivateAddressNotFetched. gui's
TestSettingsSaveKeepsWhatItDoesNotSend sets FetchPrivate. The two imagemcp
download tests and TestImageByURLIsFetchedForB64Caller now name their
local file server in fetchPrivate / resolve files.test to a public
address.

Broken by hand, each red, restored:
- draw.go inline fetch on s.client -> TestVendorImageURLAtPrivateAddressNotFetched red
- video.go on s.client -> TestVideoContentAtPrivateAddressNotFetched red
- imagemcp on http.DefaultClient -> its TestVendorImageAtPrivateAddressNotFetched red
- fetchPicture unrestricted (the old local bypass) -> TestEditImageURLFromThisComputer red
- fetchPrivate ignored -> TestEditImageURLFromThisComputer, TestFetchPrivateEntries red
Not distinguished: settings.Load in place of LoadChecked passes too (an
unparsable file with no last good generation gives no list either way).
Not tried: a real reverse proxy (nginx/caddy) in front of a running
magpie; the X-Forwarded-For loopback request stands in for it. Not tried
against a real vendor's image or video URL.

gofmt -l clean; go vet -tags nogui darwin, GOOS=linux, GOOS=windows clean.
Full suite: go test -tags nogui -timeout 45m ./... at 8b84dc902 plus this
change: every package ok except agent and gateway (timed out at 45m at
load average ~370, no test failed before the timeout) and gui
(TestSettingsSaveKeepsWhatItDoesNotSend, fixed here by adding
FetchPrivate; TestSetAnswersWhileDevinAndProvidersHang 12.4s under load,
(4726s) with -timeout 150m at load ~220.

Rebased onto ba132d68b (17 commits; only the doc test line conflicted).
At the rebased head: gofmt clean, vet darwin/linux/windows clean, and
the Image/Draw/Video/Fetch/Edit/Private/Public tests in gateway, imagemcp,
gui and settings ok.
2026-10-16 20:12:05 +08:00
yetone bac81385ab gateway: an image answer that stalls says whether it was coming as a url or as b64_json, how much had come and how long nothing more had (#1576, david-zdw)
david-zdw found their relay answers the same size as a 416 B url on one
domain and as a 3 MB b64_json body taking 380 s on another. The 504 "began
answering after … but hadn't sent all of its answer in 5m0s" didn't say
which, and the two point at different fixes: a url answer stuck after its
start, or the image's own bytes coming too slowly.

Change: sendWith reads the vendor's body through a reader that notes when
its last bytes came. On the deadline the error goes on: "it was sending
the image itself as b64_json; 3.0 MB had come, and nothing more for the
last 40s" (or "it was answering with the image's url, not the image; …",
or that what came doesn't yet say which).

Run:
- TestStalledAnswerSaysUrlOrB64 (a b64_json body and a url body, each
  stopping mid-answer), TestSizeOf, and TestVendorThatStopsMidAnswerIsSaid
  updated for the 21 B it got; the draw/fetch tests pass.
- go test ./internal/gateway, gofmt, GOOS=linux/windows go vet -tags nogui.

Not done: b64_json is still read whole into memory, not decoded to disk as
it comes. The image is passed on as bytes all the way to the agent's
answer, so streaming it would change every drawer; left for its own change.
Not tried against the reporter's relay.
2026-10-11 19:59:15 +08:00
yetone f0dc9ea695 imagemcp, gateway: a 4K image a relay answers by URL is saved, and one that can't be downloaded is answered with its URL, not lost (#1576, david-zdw)
What the reporter saw: generate_image (magpie mcp image) failed on 4K and
2048x1152 with "image 1: context deadline exceeded" and "no image: context
deadline exceeded", while the same request to /v1/images/generations came
back in ~40 s, and usage.jsonl showed the upstream's 200. Their relay
answers with a URL whatever response_format says.

Two causes, one per error:

- "image 1: context deadline exceeded" was generate_image's own download
  of the URL (imagemcp.download): 2 minutes for the whole of it, on
  http.DefaultTransport, which knows the global proxy but not a provider's
  own (#237). A 10 MB PNG that was still coming was cut, and the error
  dropped the URL of an image already drawn and paid for. Any earlier
  image of n was saved but not said either.
- "no image: context deadline exceeded" (the usage log's 502s at
  300002-300032 ms) was the gateway's drawTimeout (5 min) expiring while
  it read a vendor's answer that had already begun: sendWith passed the
  bare context error up. The vendor really took over 5 minutes there;
  this commit doesn't change that limit, it says what happened.

What changed:

- gateway: an images caller that asks response_format "b64_json" gets the
  image itself. One the vendor gave only a URL for is fetched by the
  gateway's own client, through the provider's proxy (p.Via), after the
  draw and outside drawTimeout. One that can't be fetched is answered by
  its URL, as before, never dropped. A caller that asks no format still
  gets the URL. Nothing is sent to the vendor differently.
- gateway.FetchImage: an image download gives up when nothing has come
  for ImageStall (1 min) or it has taken ImageLongest (5 min), and says
  which and how much had come. A slow download goes on while it moves.
  The edits' fetchPicture uses it too.
- gateway: a vendor that began answering but hadn't finished in
  drawTimeout is a 504 "<name> began answering after Ns (200 OK) but
  hadn't sent all of its answer in 5m0s", not a 502 "context deadline
  exceeded".
- imagemcp: asks for b64_json; downloads a URL the gateway still hands it
  with FetchImage; an image it can't have is answered with the vendor's
  URL to download it from, beside the ones saved (an error only when
  none was). Its client to the gateway waits ToolTimeout (11 min), past
  the draw and the fetch together, not 6 minutes.

What was run:

- Reproduced on the old code: the new TestFourKImageByURLIsSaved (a real
  3840x2160 PNG, 9.8 MB, served by URL in 256 KB pieces over ~4 s) with
  the old download's 2-minute cap scaled to 1 s fails with the
  reporter's exact "image 1: context deadline exceeded"; the stalled
  case fails the same way, with no URL in the error.
- Broke each part by hand and watched its test go red: no inline fetch,
  fetch not through the provider's proxy, the bare deadline message, the
  stall timer not reset (a total cap again), a lost image ending the
  call, the MCP not asking b64_json.
- go test ./internal/imagemcp/ ./internal/gateway/ ; the full
  go test -tags nogui ./... ; gofmt -l ; GOOS=linux and GOOS=windows
  go vet on both packages.
- On the Windows box (Windows 11 x64): imagemcp.test.exe all passed, and
  gateway.test.exe for the new tests and the images/ModelScope tests.

Not tried: the reporter's relay itself (never contacted), and a real
4K draw end to end. Why their URL took over 2 minutes from the MCP when
their own download took 7.5 s isn't known from here: a provider proxy
the MCP didn't use is one way it happens; a slow file host is another.
Both are covered now.
2026-10-11 14:59:23 +08:00