mirror of
https://github.com/hanzoai/ai.git
synced 2026-11-20 14:13:23 +00:00
Measured: the callback handed to SendStreamWriter has not run when the call comes
back. fasthttp produces a streamed body by draining that writer while it
serialises the response, so everything the stream reads must outlive the handler
and everything the stream learns must be used inside it.
Five relays were written the other way round, and each lost both halves:
- the family relay and the OpenAI chat path closed the upstream body in a defer
that fired before the relay read a byte, so the client was sent nothing
- the same two, plus both Anthropic paths, read their token counts after
scheduling the callback — where they are still zero, so a streamed answer
billed a prompt estimate and no completion at all
- the router's judge read the answer text the same way and scored every
streamed turn on an empty string
Each now hands the body to the callback that reads it and settles inside the
callback that knows what happened. The context is still the request's: fasthttp
finishes writing the response before fiber releases it.
The chat path had its usage record written out twice, once per shape, differing
only in where the token counts came from. One settle, taking them as arguments.
The two dialect tests that cover this were the ones reporting it, and could not
run until the package compiled.
102 lines
3.8 KiB
Go
102 lines
3.8 KiB
Go
// Copyright 2023-2026 Hanzo AI Inc. All Rights Reserved.
|
|
//
|
|
// Licensed under the Apache License, Version 2.0 (the "License");
|
|
// you may not use this file except in compliance with the License.
|
|
// You may obtain a copy of the License at
|
|
//
|
|
// http://www.apache.org/licenses/LICENSE-2.0
|
|
//
|
|
// Unless required by applicable law or agreed to in writing, software
|
|
// distributed under the License is distributed on an "AS IS" BASIS,
|
|
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
// See the License for the specific language governing permissions and
|
|
// limitations under the License.
|
|
|
|
package controllers
|
|
|
|
import (
|
|
"bufio"
|
|
"bytes"
|
|
"net"
|
|
|
|
"github.com/zap-proto/zip"
|
|
)
|
|
|
|
// How a test in this package gets a controller. One way.
|
|
//
|
|
// A controller IS its request — the resolved principal, the org, the writer it
|
|
// answers through — so a test needs a real context to hold one, and zip builds that
|
|
// over a synthetic fasthttp request. The thing under test therefore sees exactly
|
|
// what production hands it.
|
|
//
|
|
// It replaced an httptest request, an httptest recorder, a router context, a Reset
|
|
// to marry them and an Init to register the controller against a name — five steps
|
|
// whose only purpose was to hand a handler somewhere to read from and write to.
|
|
// Both live on the context now.
|
|
|
|
// visit builds a controller for one request at target — a path, and the query string
|
|
// if it has one.
|
|
//
|
|
// THE TARGET IS SET WHOLE, because zip's TestCtx sets the PATH: hand it
|
|
// "/x?org=maxpower" and the question mark lands in the path while Queries() stays
|
|
// empty, so a handler reads no parameters at all. Tests written that way do not fail
|
|
// — they quietly exercise the no-parameter case and agree with themselves. Setting
|
|
// the request URI parses it where a request is parsed.
|
|
func visit(method, target string) *ApiController {
|
|
c := &ApiController{
|
|
Ctx: zip.New(zip.Config{DisableStartupMessage: true, ReadBufferSize: 32 << 10}).TestCtx(method, target),
|
|
}
|
|
c.Fiber().Request().SetRequestURI(target)
|
|
return c
|
|
}
|
|
|
|
// from sets the socket peer, the one address on a request nobody but the network
|
|
// can set. The public lane counts by it, so a test that does not set it is testing
|
|
// a visitor with no address.
|
|
func from(c *ApiController, addr string) *ApiController {
|
|
if a, err := net.ResolveTCPAddr("tcp", addr); err == nil {
|
|
c.Fiber().RequestCtx().SetRemoteAddr(a)
|
|
}
|
|
return c
|
|
}
|
|
|
|
// sent is the body the controller wrote.
|
|
func sent(c *ApiController) string { return string(c.Fiber().Response().Body()) }
|
|
|
|
// answered is the status the controller set, or 200 when it set none.
|
|
func answered(c *ApiController) int { return c.Fiber().Response().StatusCode() }
|
|
|
|
// stream is a destination for the SSE relays, which take the writer they write to
|
|
// rather than reaching for one. Read it with String once the relay has returned.
|
|
type stream struct {
|
|
buf bytes.Buffer
|
|
w *bufio.Writer
|
|
}
|
|
|
|
func toStream() *stream {
|
|
s := &stream{}
|
|
s.w = bufio.NewWriter(&s.buf)
|
|
return s
|
|
}
|
|
|
|
// String flushes and returns everything the relay wrote.
|
|
func (s *stream) String() string {
|
|
_ = s.w.Flush()
|
|
return s.buf.String()
|
|
}
|
|
|
|
// presenting gives c the credential a caller arrives with.
|
|
//
|
|
// ONE VERB, because there is one way to be somebody here: a signed token, verified
|
|
// the way production verifies it. Pass authtest.Bearer for a caller who should be
|
|
// accepted, or the literal thing a test wants REFUSED — a publishable key, a forgery,
|
|
// an empty string for nobody at all. What it replaced was four different harnesses
|
|
// seeding a fake session store with whatever identity each test wanted to be true,
|
|
// which is not an authorisation test but a test of the fake.
|
|
func presenting(c *ApiController, authorization string) *ApiController {
|
|
if authorization != "" {
|
|
c.Fiber().Request().Header.Set("Authorization", authorization)
|
|
}
|
|
return c
|
|
}
|