Commit Graph
9 Commits
Author SHA1 Message Date
yetoneandwaynesguan 0a5dc40abc gateway: a group's trusted Claude, GPT and Gemini thinking streams as it comes, a refusal or error after it going to the next member (#4166)
A try whose model's vendor may refuse after its reasoning (#248) had its
reasoning held while another candidate could answer, whatever the member
had answered lately. Through a routing group Claude Code saw a Claude
member's thinking only with its text, all at once, and never its
"thinking with <effort> effort". waynesguan measured it on one WorkBuddy
account: `workbuddy claude-opus-5.5` alone, first thinking at 3.66s;
through the group, all 67 thinking deltas with the text at 17.25s.

Ported from waynesguan's 71aacaa (on f8987ce61): the trust #4512 gave
DeepSeek now covers Claude, GPT and Gemini, but only where a refusal
after the reasoning can be handed to the next candidate in the same
stream (handsOver, refusalHandover). Once a member's thinking has gone
on to an answer thoughtCleanMost (3) times running, its reasoning goes
as it comes; a refusal after it, nothing yet said, goes to the next
member, whose reply follows as the same one; the refusal takes the trust
back. Where no handover is possible (no stream, a Gemini-protocol agent,
a compaction wanted whole, refusals asked again enough, the last
candidate) it is held as before. holdsThinking is decided after hw.over.

Added in review: the port alone lost failover for errors. passOver
handed over only a refusal; a retryable error after shown reasoning
(Anthropic's mid-stream overloaded_error) counted as the answer begun and
reached the agent, the next member never asked, where the held stream
had failed over. passOver now hands a retryable error over too
(handOver(status, msg, refused=false)): the try fails with its status
and words, as a held one did, and the next member's stream follows. A
non-retryable error (a request at fault) still reaches the agent. This
also applies to GLM's and trusted DeepSeek's shown reasoning, which had
the same gap.

Tests:
- TestTrustedThinkingFailsOverOnEveryPath: claude (Anthropic upstream),
  gpt and gemini (Chat upstream) members x refused / overloaded /
  invalid x /v1/messages, /v1/chat/completions, /v1/responses, 27
  cases; spliced stream checked per protocol (one message_start, block
  indexes, sequence numbers, one [DONE]).
- TestClaudeThinkingStreamsOnceTrusted, TestClaudeTrustedThinkingStreamsThroughGroup
  (waynesguan's), TestClaudeTrustedThinkingErrorFailsOver.

Broken by hand:
- main's fallback.go + gateway.go: the 18 refused/overloaded cases,
  TestClaudeThinkingStreamsOnceTrusted ("a trusted Claude member's
  reasoning is still held") and ...ThroughGroup ("first thought only
  1.541µs before the text") FAIL.
- port without the error handover: the 9 overloaded cases and
  TestClaudeTrustedThinkingErrorFailsOver FAIL (the agent got
  "Overloaded", b asked 0 times).

Run (mote, via mote-run): go test ./internal/gateway/ at 741f7a8f2
(877s) ok but TestClosedBeforeSentEveryTimeSaysWhy (bare EOF, Codex
closed-unsent path, already #5031, not touched here); after rebase on
e7b0c398e: -run 'Thinking|Refusal|Handover|ObjectRoot|DeepSeek' ok;
gofmt -l clean; go vet ./internal/gateway/, GOOS=linux go vet -tags
nogui ./..., GOOS=windows go vet ./... ok. Full suite runs after push;
its reds are filed separately.

Not tried: a real refusal or overloaded_error after thinking from a real
account (can't be triggered at will); Claude Code against a real group
with this build; Windows/WSL runs (vet only).

Co-authored-by: waynesguan <wenwenguan@tencent.com>
2026-11-04 18:37:43 +08:00
ggbdpqandJackzigen e89a4b3c77 gateway tests: a re-asked slowThinker drops its done send instead of blocking (#4530)
slowThinker.done held one send, and a try that is asked again serves
the same fake vendor a second time: the second handler blocked on the
send forever, the reply it served never ended, and t.Cleanup(vendor.
Close) waited on it until go test's own timeout — no test failure, no
output, the whole package with it (#4530).

The tests read the time once; later sends can drop (Jackzigen's fix
in gh#4530, adopted): the send is non-blocking, and
TestThinkingVendorAskedAgainStillEnds, which asks a Gemini fake that
only reasons and whose reply is asked again in place (#667), reads the
reply to its end instead of hanging.

Squashed from the patch's two commits (test, then fix), so no commit
on main hangs the package.

Broken by hand: with the blocking `s.done <- time.Now()` put back,
TestThinkingVendorAskedAgainStillEnds hangs until -timeout 120s
panics, with a serve stuck at thinking_streams_test.go:40. With the
patch it passes in 1.4s, and -race -count=20 is ok.

Co-authored-by: Jackzigen <9687529+Jackzigen@users.noreply.github.com>
2026-11-01 02:45:01 +08:00
yetone 645eb92f3f gateway: a refusal after shown thinking goes to the next fallback
yhwangl on X: a request whose content trips a vendor's safety filter
after the model streamed its thinking, before any answer, stopped there;
the group's next member was never asked, as it is when credit runs out.

When a model's reasoning is shown, a try passes on its first thinking
event, so a refusal after it was the agent's. Now, for a streamed
Anthropic, Chat or Responses request with another member still to ask
(handsOver), the try's events go out one by one (passOver) and a refusal
or refusal error before any text or tool call hands the request on
(handOver): Anthropic's open blocks are closed and the next member's
stream is spliced into the same reply (spliceAfter): its message_start
or response.created/in_progress/queued are dropped, block indexes or
output_index go after those sent, sequence_number goes on. Chat chunks
go as they come. Once text or a call has gone, a refusal stays the
agent's as before. With no other member nothing changes. A refusal is
about the request, so nobody is rested for it.

Zhipu's finish_reason "sensitive" and error 1301 (敏感), DashScope's
data_inspection_failed and Volcengine Ark's *SensitiveContentDetected
are now read as refusals too.

Not covered (#4604): Gemini clients; a Responses reasoning item handed
over gets no output_item.done and the next completed lists only its own
output; A's thinking signature stays in the reply; non-streamed requests.

Run (mote, worktree at base 3baf3b6df):
- go test -count=1 -timeout 45m ./internal/gateway/...: ok (597s)
- targeted -run 'TestRefusal|TestSensitive|TestThinking|TestDeepSeek|
  TestStreamEvent|TestHighRisk|Refus': ok
- gofmt -l internal/: clean; go vet ./internal/gateway; GOOS=linux go vet
  -tags nogui ./...; GOOS=windows go vet ./...: ok
Rebased on 8a410f35a (5 commits, Claude TUI files in internal/gateway):
go vet ./internal/gateway and the targeted run plus ClaudeTUI|
ClaudeSubscription tests ok at the pushed head.
Broken by hand: handsOver forced to false (the old behaviour):
TestRefusalAfterShownThinkingGoesToTheNext (every upstream x client
subtest: b never asked), TestRefusalHandedOverTwice and
TestDeepSeekRefusalHoldsATrustedMemberAgain red; reverted.
Not tried: a real GLM, DashScope or Ark account tripping its filter, and
a real Claude Code or Codex client reading the spliced stream; the tests
use fake upstreams with GLM's, DeepSeek's and Anthropic's event shapes.
2026-11-01 00:40:07 +08:00
ggbdpq 6585315e32 gateway: a DeepSeek member's reasoning streams through once it has answered cleanly three times running (#4512)
c8f199695 holds a member's thinking back for a refusal after it, so
WorkBuddy's filter (plugins #540) never hands the agent a refused
reply's reasoning as if it were an answer. Held, though, DeepSeek group
members kept every request's whole thinking from the agent: its first
token landed only when the first answer delta did (#4512, with the
reporter's per-line SSE timings; direct connections were never held).

Claude's, GPT's and Gemini's thinking is still held whatever the member
has answered lately (#248). DeepSeek's now streams through as it comes
once the member has gone on from its thinking to an answer three times
running (thoughtCleanMost); one refusal takes the trust back, and a
content_filter stop with nothing said does too. The count is per
"<provider>/<model>", and #248's per-vendor split is why the trust is
DeepSeek-only: the held vendors refuse after thinking by design, the
one that hardly ever does is the one whose hold the agent keeps paying
for.

Tests: TestDeepSeekThinkingStreamsOnceTrusted (three clean thinking
replies, then reasoning deltas arrive before any answer delta) and
TestDeepSeekRefusalHoldsATrustedMemberAgain (a refusal re-holds the
next try) cover the trust and its withdrawal; the refusal-family tests
including TestDeepSeekRefusalAfterThinkingFailsOver keep the original
WorkBuddy hold intact.

Verification on the base 91ffe5429, macOS arm64, Go with pinned
GOFLAGS: 8 targeted hold/thinking tests ok; gateway package full
-tags nogui; gofmt, vet on darwin/linux/windows and builds on all
three platforms clean; pre-existing sandbox failures untouched.

Generated-by: GLM-5.3-Flash (ZCode)

Reviewed and run by yetone at the batch head over cac655c14 (this commit,
the four others from ggbdpq's #4495/#4548/#4512/#3260 patches and the
#4548 follow-up), on mote:
- gofmt -l clean; go vet ./... for darwin, linux (-tags nogui), windows;
  go build ./... for darwin and windows.
- go test -tags nogui -count=1 -timeout 45m ./...: every package ok but
  internal/gui, with two filed reds: TestSettingsSaveKeepsWhatItDoesNotSend
  (#4514), red alone at origin/main cac655c14 too, and TestColdTurnsTaken
  (#4510), red only in the full run. Rebased afterwards onto e94fb18b8,
  a usage-page change that touches none of these files.
- Windows (xiaomi-gpu): go test -run 'TestVSCode|TestClaudePicker'
  ./internal/agent ok. TestCopilotFolderFromAnIDEPluginIsNotTheCLI is red
  there at cac655c14 too (#1827).
- Broken by hand: thinkingShown back to !refusesAfterThinking(c.model) -> TestDeepSeekThinkingStreamsOnceTrusted and TestDeepSeekRefusalHoldsATrustedMemberAgain red in 1.5s (no hang). Not tried: a real DeepSeek member in a group. Trade-off kept: once trusted, a member's rare refusal after thinking reaches the agent once (nothing said) before it is held again; trust is per process and builds only from replies that report output tokens.
2026-10-31 20:54:37 +08:00
yetone c8f199695c gateway: DeepSeek's reasoning is held for a refusal after it; WorkBuddy plugin min 0.2.4 (plugins #540)
itspig (plugins #540): deepseek-v4.1-flash on WorkBuddy, through
group/tiered, reasoned ~5s, said "Sorry, I can't respond to this
question.", and ended finish_reason content_filter with 0 output tokens
under a 200. The try was recorded done/200, none of the 15 members behind
it was asked, and DeepSeek Harness failed "The model refused to complete
the request".

Two halves:
- workbuddy 0.2.4 (plugins 4883f94) takes the filter's canned line out of
  a streamed chat that ends content_filter with nothing else said.
- refusesAfterThinking now holds DeepSeek's reasoning too (as Claude's,
  GPT's and Gemini's), only while another candidate could answer: with it
  let through as it came, a refusal after it could never go to the next
  member. DeepSeek's own API documents finish_reason content_filter. The
  cost: in a group with a member behind it, DeepSeek's thinking reaches
  the agent once its text begins (kept alive meanwhile), as Claude's does;
  on the last candidate it streams as before. GLM, Kimi and others are
  unchanged.
- The workbuddy mover's min is 0.2.4 (npm view --prefer-online: 0.2.4).

Run on mote at this tree: go test ./internal/gateway -run
'TestDeepSeekRefusalAfterThinkingFailsOver|TestThinkingHeldFor|
TestRefusalAfterThinkingFailsOver|TestThinkingStreams' ok; go test
./internal/provider ok; gofmt -l clean. The full ./... suite was started at
this tree minus the one-line min and is still running; its result goes on
plugins #540 (owner rule: tag without waiting).

Broken by hand: without the deepseek prefix, TestDeepSeekRefusalAfterThinkingFailsOver/refused
goes red (DeepSeek's reasoning reached the agent, b never asked) and
TestThinkingHeldFor red for the three DeepSeek ids; /answered stays green.

Not tried: a real WorkBuddy refusal (no prompt reproduces it on demand);
the upstream's literal chunking isn't in the issue, so the fixture is
built from its fields (reasoning, content_filter, 0 tokens).
2026-10-31 06:05:29 +08:00
yetone 592899467b gateway: Gemini's reasoning streams as it comes on the last candidate, so Codex isn't left with nothing for the whole of its thinking (LuckymanJC on Discord)
LuckymanJC and kkgg: through magpie the first turn of a Codex session on
a Gemini model was very slow or seemed stalled. Measured with fake
upstreams through the gateway (Codex Responses -> Chat DeepSeek, Gemini;
OpenCode Chat; Anthropic passthrough), first content came within 1-50ms
on every path but one: a Gemini model's reasoning was held even on the
last candidate (`|| last && modelFamily(c.model) != ""` left Gemini out),
so Codex got nothing at all, not even response.created, until the text
began, while Claude's and GPT's has gone as it comes there since
iTianbao's report.

It was held there for #667, a reply that only reasoned being asked
again. That re-ask happens in place in streamTranslated, keeping the
reasoning the agent has, so it doesn't need the hold. But with the try
passing, emptyRetries counted it as past the request's own re-asks and
asked only streamRetries (2) times, and TestGeminiEmptyRepliesAskedAgain
(three empty replies running) went red. A held try that went through on
its reasoning alone (holdWriter.thoughtThrough) is now asked once more in
place (streamRetries+1); a never-answering model still makes at most 4
calls (the test's bound is 6).

Tests: TestThinkingStreamsWithNoOneElse gets "gemini" (Gemini upstream)
and "chat gemini" cases, to Claude Code and Codex. With the old line
restored they go red ("thinking held: first thought only 1.75us before
the text"). With emptyRetries' new branch broken by hand
(streamRetries+0), TestGeminiEmptyRepliesAskedAgain goes red with
"an empty reply"; green with both.
Run: go test -run 'TestGeminiEmpty|TestThinkingStreams|TestThinkingHeld|TestUncountedEmpty|TestReasoningLoop' ./internal/gateway: ok.
Break check repeated at push: the old `|| last && modelFamily(c.model)
!= ""` turns
TestThinkingStreamsWithNoOneElse/{gemini,chat_gemini}/v1/{messages,responses} red ("thinking held: first thought only 1.458µs before the text").
Full suite (go1.26.8 darwin-arm64, rebased on 0fe039f14): go test
-timeout 45m ./... gave 63 packages ok; internal/agent and
internal/gateway timed out at 45m with no test failed (load ~110 from
other sessions; deadline in TestJcode and TestVertexGeminiClients,
#2544 and #2870 seen again) and passed alone at that head: ok
internal/agent 1644.560s, ok internal/gateway 1397.712s. Rebased on
origin/main 4ae6c06dc, every package its new commits touch run in full
there: ok internal/agent 465.353s, internal/gateway 648.172s,
internal/sessions, internal/gui, internal/cli, internal/codexsummary;
gofmt -l on the changed files empty; go vet ./..., GOOS=linux go vet
-tags nogui ./..., GOOS=windows go vet ./... clean; go build for
darwin, linux (nogui) and windows ok (at 0fe039f14).
Then rebased on origin/main 2201b6628 (one line in
internal/provider/migrate_qoder.go): ok internal/provider 608.249s; go
vet of internal/provider, internal/agent and internal/gateway clean.
2026-10-21 03:40:27 +08:00
yetone 8b89b2e36c tests: two waits that went red at load 80–100 leave room: the reasoning relay is timed by the vendor's clock, otel waits a minute for its export
Both went red in full runs of go test ./... on a machine at load 80–100.
Neither is a fault in what they test.

- TestChatReasoningRelayedAsItComes: "first thought after 924ms" against a
  700ms bound from before the request, while the thought still came ~1s
  ahead of the text. The bound timed the gateway's start, not the relay.
  slowThinker now records when it wrote each event. The test checks that
  the first thought reached the client before the vendor wrote the text.
  That is the guarantee, and a slow start no longer counts against it.
- plugins/otel TestOnExportsOffSendsNothing waited 10s for the first export.
  It now waits a minute, as other waits for a plugin's first work do.

Run: go test -count=5 for the relay test, all ok. Broken by hand:
refusesAfterThinking returns true, so the reasoning is held as Claude's is.
The test went red ("reached the client 489µs after the vendor wrote the
text"). Full go test ./... ran with the middleware point commit before this
one (see its body).
2026-10-15 13:40:27 +08:00
yetone 76321e3b42 gateway: a lone provider's Claude or GPT reasoning streams as it comes, not held for a refusal no one else could answer (iTianbao on X: 请加一个开关,让思考实时透传; through a relay Claude Code showed the thinking only all at once with the text, Codex none until then) — holdWriter held Claude's, GPT's and Gemini's reasoning (refusesAfterThinking, up to holdThinking) on every candidate, so a refusal after it could go to the next one (#248), but a refusal on the last candidate is never asked again (the !last checks in serve), so there it only delayed the thinking. On the last candidate Claude's and GPT's reasoning is now shown at once (thinkingShown), and a refusal after it reaches the agent as the vendor's own stop; Gemini's stays held there, for a reply that only reasoned to be asked again (#667, TestGeminiEmptyRepliesAskedAgain). A group with members after it still holds as before. Before: on a slow fake relay (4 thoughts 300ms apart, then text) every relay shape (Messages, Chat with reasoning_content and reasoning, Responses) for claude-sonnet-4-5 and gpt-5.1 gave Claude Code's thinking_delta and Codex's reasoning_summary_text.delta all at ~1.2–1.5s with the text; after, 0/300/600/900ms. TestThinkingStreamsWithNoOneElse (Messages, Chat for Claude and GPT, Responses; each asked from /v1/messages and /v1/responses) fails without the change in every case and passes; TestHeldThinkingKeepsTheAgentAlive's nobody-left case now expects the thinking and Claude's refusal rather than a held error. -race -count=5 on TestThinking*, TestRefusal*, TestHeld*, TestGeminiEmpty*; go test -tags nogui ./internal/gateway, go vet, gofmt -l, linux and windows (nogui) builds pass. Not tried with the reporter's relay: whether it sends GPT's reasoning at all (OpenAI's Chat API doesn't) is unknown, and no switch was added. docs/subsystems/gateway-routing.md says it. 2026-10-06 19:34:24 +08:00
yetone 2fc277de9f gateway: GLM's thinking streams again through a group (悠悠哥 on Discord: ZCode's thinking shows only at the end, all at once) — since a8f0baaa (#248) a stream that had only reasoned was held, up to 4 minutes, until its text or a call began, for a refusal after the reasoning to go to the next account; so GLM on a ZCode account (or any key with retries left, or any group member before the last) showed Claude Code its thinking only once the text came, in one burst. Only Claude's, OpenAI's and Gemini's safety filters end a reply that has only reasoned with a refusal, so reasoning is now held only for those models (claude-*, gpt-*/o-series/codex, gemini-*); anybody else's (GLM, DeepSeek, Kimi, MiniMax…) goes through as it comes, as before #248. Requests, headers and prompts are unchanged. TestThinkingStreamsThroughGroup (a fake vendor streaming three thoughts 200ms apart, on Anthropic's API and on Chat with reasoning_content, read by a real HTTP client through a group) and TestThinkingHeldFor fail without the change; TestRefusalAfterThinkingFailsOver now names Claude and GPT models and still passes. go vet, go build -tags nogui (darwin, GOOS=linux), GOOS=windows go build, go test -tags nogui ./... pass. 2026-10-01 16:20:10 +08:00