Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 5 additions & 5 deletions cmd/server/internal/api/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,7 @@ func Unavailable() http.Handler {
err := httpx.Fail(503, "console_store_unavailable", "The console database is not available")
if strings.HasPrefix(r.URL.Path, "/api/console/v1/build-sessions") {
w.Header().Set("Cache-Control", "no-store")
writeBuildError(w, err)
writeBuildError(w, r, err)
return
}
httpx.WriteError(w, err, "console")
Expand All @@ -118,7 +118,7 @@ func (a *API) ServeHTTP(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Cache-Control", "no-store")
if a.links == nil && strings.HasPrefix(r.URL.Path, "/api/console/") && r.URL.Path != "/api/console/openapi.json" {
if strings.HasPrefix(r.URL.Path, "/api/console/v1/build-sessions") {
writeBuildError(w, directoryError(link.ErrUnavailable))
writeBuildError(w, r, directoryError(link.ErrUnavailable))
return
}
httpx.WriteError(w, directoryError(link.ErrUnavailable), "console")
Expand Down Expand Up @@ -155,7 +155,7 @@ func (a *API) routeHandlerMode(mutation, needsRoles, transactional, fresh bool,
if errors.Is(err, identity.ErrUnavailable) {
status = http.StatusServiceUnavailable
}
writeBuildError(w, httpx.Fail(status, "unauthorized", "Authentication is required"))
writeBuildError(w, r, httpx.Fail(status, "unauthorized", "Authentication is required"))
return
}
if errors.Is(err, identity.ErrUnavailable) {
Expand Down Expand Up @@ -192,11 +192,11 @@ func (a *API) routeHandlerMode(mutation, needsRoles, transactional, fresh bool,
if err != nil {
var rejection *domain.BuildRejection
if errors.As(err, &rejection) {
writeBuildError(w, err)
writeBuildError(w, r, err)
return
}
if !transactional {
writeBuildError(w, err)
writeBuildError(w, r, err)
return
}
httpx.WriteError(w, directoryError(err), "console")
Expand Down
2 changes: 1 addition & 1 deletion cmd/server/internal/api/build_request_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ func TestBuildRequestScalarAndCanonicalNumberContract(t *testing.T) {

func TestBuildPackedPromptLimitReturns413(t *testing.T) {
w := httptest.NewRecorder()
writeBuildError(w, prompts.ErrInputTooLarge)
writeBuildError(w, httptest.NewRequest("POST", "/", nil), prompts.ErrInputTooLarge)
require.Equal(t, 413, w.Code)
require.Equal(t, "no-store", w.Header().Get("Cache-Control"))
var body BuildError
Expand Down
9 changes: 8 additions & 1 deletion cmd/server/internal/api/build_sessions.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import (
"unicode/utf8"

"github.com/appthrust/appserver-go/appserver/httpx"
"github.com/appthrust/appserver-go/appserver/requestlog"
"github.com/appthrust/builder-console/cmd/server/internal/buildsession"
"github.com/appthrust/builder-console/cmd/server/internal/conversation/prompts"
"github.com/appthrust/builder-console/cmd/server/internal/domain"
Expand Down Expand Up @@ -107,7 +108,7 @@ func (a *API) registerBuildSessions() {
a.mux.HandleFunc(route.pattern, a.routeHandlerMode(false, false, false, true, route.handler))
}
}
func writeBuildError(w http.ResponseWriter, err error) {
func writeBuildError(w http.ResponseWriter, r *http.Request, err error) {
status := 503
body := BuildError{Error: "service_unavailable", Message: "The conversation service is temporarily unavailable"}
var upstream *buildsession.Failure
Expand All @@ -119,6 +120,8 @@ func writeBuildError(w http.ResponseWriter, err error) {
body.Error = "input_too_large"
body.Message = "The complete conversation inputs exceed the request limit"
case errors.As(err, &upstream):
// Failure.Code is normalized by buildsession; never record upstream details.
requestlog.SetError(r, &httpx.Error{Status: upstream.Status, Code: upstream.Code})
status = upstream.Status
body.Error = upstream.Code
body.Message = upstream.Message
Expand Down Expand Up @@ -153,6 +156,7 @@ func writeBuildError(w http.ResponseWriter, err error) {
}
}
case errors.As(err, &shared):
requestlog.SetError(r, shared)
status = shared.Status
body.Message = shared.Message
switch {
Expand All @@ -169,6 +173,9 @@ func writeBuildError(w http.ResponseWriter, err error) {
default:
body.Error = "service_unavailable"
}
default:
// Unknown errors can contain request data or upstream diagnostics.
requestlog.SetOutcome(r, "failed", "build_session_unavailable")
}
if status == http.StatusTooManyRequests || status == http.StatusServiceUnavailable {
retryAfter := ""
Expand Down
2 changes: 1 addition & 1 deletion cmd/server/internal/api/build_sessions_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -409,7 +409,7 @@ func TestBuildErrorsHaveBoundedRetryAfter(t *testing.T) {
} {
t.Run(tc.name, func(t *testing.T) {
w := httptest.NewRecorder()
writeBuildError(w, tc.err)
writeBuildError(w, httptest.NewRequest("GET", "/", nil), tc.err)
require.Equal(t, tc.want, w.Header().Get("Retry-After"))
if tc.name == "permanent failure" {
var body BuildError
Expand Down
43 changes: 40 additions & 3 deletions cmd/server/internal/api/directory_proxy.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package api

import (
"context"
"errors"
"net/http"
"net/http/httputil"
"net/url"
Expand All @@ -10,10 +11,11 @@ import (

"github.com/appthrust/appserver-go/appserver/httpx"
"github.com/appthrust/appserver-go/appserver/link"
"github.com/appthrust/appserver-go/appserver/requestlog"
)

func (a *API) directoryProxy() http.Handler {
proxy := &httputil.ReverseProxy{
proxy := WithProxyOutcomes(&httputil.ReverseProxy{
Rewrite: func(r *httputil.ProxyRequest) {
r.Out.Host = r.Out.URL.Host
r.Out.URL.Path = "/api/directory/v1/" + strings.TrimPrefix(r.In.URL.Path, "/api/console/directory/")
Expand All @@ -32,10 +34,13 @@ func (a *API) directoryProxy() http.Handler {
}
return nil
},
ErrorHandler: func(w http.ResponseWriter, _ *http.Request, _ error) {
ErrorHandler: func(w http.ResponseWriter, r *http.Request, err error) {
if !errors.Is(err, link.ErrUnavailable) {
requestlog.SetOutcome(r, "upstream_error", "directory_proxy_failed")
}
httpx.WriteError(w, directoryError(link.ErrUnavailable), "console")
},
}
})
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
if r.Header.Get("Authorization") == "" {
httpx.WriteError(w, httpx.Fail(401, "unauthenticated", "A valid bearer token is required"), "console")
Expand Down Expand Up @@ -65,3 +70,35 @@ func (a *API) directoryProxy() http.Handler {
proxy.ServeHTTP(w, r)
})
}

type upstreamStatusTransport struct{ next http.RoundTripper }

func (t upstreamStatusTransport) RoundTrip(r *http.Request) (*http.Response, error) {
next := t.next
if next == nil {
next = http.DefaultTransport
}
resp, err := next.RoundTrip(r)
if err == nil && resp.StatusCode >= 400 {
requestlog.SetOutcome(r, "upstream_error", "upstream_http_status")
}
return resp, err
}

// WithProxyOutcomes records upstream rejections without inspecting their bodies.
// Configure the proxy before serving requests.
func WithProxyOutcomes(proxy *httputil.ReverseProxy) http.Handler {
proxy.Transport = upstreamStatusTransport{next: proxy.Transport}
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
// ReverseProxy aborts a partial stream by panicking with http.ErrAbortHandler
// instead of calling ErrorHandler. Do not intercept or change the panic.
complete := false
defer func() {
if !complete {
requestlog.SetOutcome(r, "aborted", "proxy_response_aborted")
}
}()
proxy.ServeHTTP(w, r)
complete = true
})
}
22 changes: 13 additions & 9 deletions cmd/server/internal/api/draft_reviewers.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import (

"github.com/appthrust/appserver-go/appserver/httpx"
"github.com/appthrust/appserver-go/appserver/identity"
"github.com/appthrust/appserver-go/appserver/requestlog"
"github.com/appthrust/builder-console/cmd/server/internal/domain"
)

Expand All @@ -25,7 +26,7 @@ func WithDraftReviewers(next http.Handler, projectID string, current func() *API
}
payload, err := io.ReadAll(http.MaxBytesReader(w, r.Body, 1<<20))
if err != nil {
writeDraftError(w, httpx.Invalid("Request body exceeds the size limit"))
writeDraftError(w, r, httpx.Invalid("Request body exceeds the size limit"))
return
}
var body map[string]json.RawMessage
Expand All @@ -37,22 +38,22 @@ func WithDraftReviewers(next http.Handler, projectID string, current func() *API
if kind == "improvement" {
a := current()
if a == nil {
writeDraftError(w, httpx.Fail(503, "console_store_unavailable", "The console database is not available"))
writeDraftError(w, r, httpx.Fail(503, "console_store_unavailable", "The console database is not available"))
return
}
ctx, cancel := context.WithTimeout(r.Context(), 30*time.Second)
defer cancel()
claims, bearer, err := a.identity.Authenticate(ctx, r.Header.Get("Authorization"))
if err != nil {
if errors.Is(err, identity.ErrUnavailable) {
writeDraftError(w, httpx.Fail(503, "identity_unavailable", "The identity provider is temporarily unavailable"))
writeDraftError(w, r, httpx.Fail(503, "identity_unavailable", "The identity provider is temporarily unavailable"))
return
}
if errors.Is(err, identity.ErrRawKeyNotAccepted) {
writeDraftError(w, httpx.Fail(401, "raw_key_not_accepted", "Exchange the access key for an assertion before calling this API"))
writeDraftError(w, r, httpx.Fail(401, "raw_key_not_accepted", "Exchange the access key for an assertion before calling this API"))
return
}
writeDraftError(w, httpx.Fail(401, "unauthenticated", "A valid bearer token is required"))
writeDraftError(w, r, httpx.Fail(401, "unauthenticated", "A valid bearer token is required"))
return
}
err = a.withRequest(ctx, claims, bearer, false, true, false, false, func(s *request) error {
Expand All @@ -74,12 +75,12 @@ func WithDraftReviewers(next http.Handler, projectID string, current func() *API
return httpx.NotFound()
})
if err != nil {
writeDraftError(w, directoryError(err))
writeDraftError(w, r, directoryError(err))
return
}
payload, err = json.Marshal(body)
if err != nil {
writeDraftError(w, httpx.Invalid("Invalid draft request"))
writeDraftError(w, r, httpx.Invalid("Invalid draft request"))
return
}
}
Expand All @@ -104,12 +105,15 @@ func ownerSubjects(owners []domain.Person) []string {

// This middleware is on /api/v1, so errors use the Platform API envelope, not
// the flat console envelope. The SPA must be able to distinguish prerequisites.
func writeDraftError(w http.ResponseWriter, err error) {
func writeDraftError(w http.ResponseWriter, r *http.Request, err error) {
var detail *httpx.Error
if !errors.As(err, &detail) {
log.Printf("console draft request failed: %v", err)
if requestlog.ID(r.Context()) == "" {
log.Printf("console draft request failed: %v", err)
}
detail = &httpx.Error{Status: 500, Code: "internal_error", Message: "The console could not complete this request"}
}
requestlog.SetError(r, detail)
type errorDetail struct {
Code string `json:"code"`
Message string `json:"message"`
Expand Down
11 changes: 10 additions & 1 deletion cmd/server/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import (
"time"

"github.com/appthrust/appserver-go/appserver/platformenv"
"github.com/appthrust/appserver-go/appserver/requestlog"
)

func main() {
Expand Down Expand Up @@ -43,7 +44,7 @@ func main() {
if err != nil {
log.Fatal(err)
}
server := &http.Server{Addr: *listen, Handler: handler, ReadHeaderTimeout: 10 * time.Second}
server := newServer(*listen, handler)
ctx, stop := signal.NotifyContext(context.Background(), syscall.SIGINT, syscall.SIGTERM)
defer stop()
go func() {
Expand All @@ -59,3 +60,11 @@ func main() {
log.Fatal(err)
}
}

func newServer(addr string, handler http.Handler) *http.Server {
return &http.Server{
Addr: addr,
Handler: requestlog.Wrap(handler, requestlog.Options{Service: "builder-console"}),
ReadHeaderTimeout: 10 * time.Second,
}
}
11 changes: 7 additions & 4 deletions cmd/server/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (
"github.com/appthrust/appserver-go/appserver/platformclient"
"github.com/appthrust/appserver-go/appserver/platformenv"
"github.com/appthrust/appserver-go/appserver/releaseidentity"
"github.com/appthrust/appserver-go/appserver/requestlog"
"github.com/appthrust/builder-console/cmd/server/internal/api"
"net/http"
"net/http/httputil"
Expand Down Expand Up @@ -40,7 +41,7 @@ func newHandler(staticDir, upstreamURL, projectID string, console http.Handler,
return nil, err
}
catalog := launcher.NewCatalog(platformclient.New(upstreamURL, projectID, platformclient.WithHTTPClient(authenticator.HTTPClient())), "builder-console")
proxy := &httputil.ReverseProxy{
proxy := api.WithProxyOutcomes(&httputil.ReverseProxy{
Rewrite: func(r *httputil.ProxyRequest) {
// Only replace the origin: /api paths and query strings stay intact.
r.Out.URL.Scheme = upstream.Scheme
Expand All @@ -55,10 +56,11 @@ func newHandler(staticDir, upstreamURL, projectID string, console http.Handler,
},
// Flush every AI runtime NDJSON event, not only event-stream responses.
FlushInterval: -1,
ErrorHandler: func(w http.ResponseWriter, _ *http.Request, _ error) {
ErrorHandler: func(w http.ResponseWriter, r *http.Request, _ error) {
requestlog.SetOutcome(r, "upstream_error", "platform_proxy_failed")
writeJSON(w, http.StatusBadGateway, map[string]string{"error": "upstream_unavailable"})
},
}
})
// The Dashboard shares the Platform API origin; its
// Team settings page is where the データ tab sends membership gaps.
config := struct {
Expand All @@ -82,7 +84,8 @@ func newHandler(staticDir, upstreamURL, projectID string, console http.Handler,
writeJSON(w, http.StatusOK, response)
})
mux.Handle("GET /launcher", launcher.NewHandler(catalog, authenticator.Authenticate))
mux.Handle("GET /releasez", releaseidentity.NewHandler("BUILDER_CONSOLE", func(w http.ResponseWriter, _ *http.Request) {
mux.Handle("GET /releasez", releaseidentity.NewHandler("BUILDER_CONSOLE", func(w http.ResponseWriter, r *http.Request) {
requestlog.SetOutcome(r, "unavailable", "release_identity_unavailable")
w.Header().Set("Content-Type", "text/plain; charset=utf-8")
w.WriteHeader(http.StatusServiceUnavailable)
_, _ = w.Write([]byte("UI release status unavailable\n"))
Expand Down
29 changes: 27 additions & 2 deletions cmd/server/server_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -181,13 +181,38 @@ func TestUnauthenticated(t *testing.T) {
func TestUpstreamUnavailable(t *testing.T) {
upstream := httptest.NewServer(http.HandlerFunc(func(http.ResponseWriter, *http.Request) {}))
upstream.Close()
h := testHandler(t, upstream.URL)
output, err := os.CreateTemp(t.TempDir(), "request-log")
if err != nil {
t.Fatal(err)
}
defer output.Close()
// Wrap binds stderr when the production server is constructed.
stderr := os.Stderr
os.Stderr = output
server := newServer(":0", h)
os.Stderr = stderr
w := httptest.NewRecorder()
r := httptest.NewRequest(http.MethodGet, "/api/v1/projects", nil)
r := httptest.NewRequest(http.MethodGet, "/api/v1/projects?private=query-canary", nil)
r.Header.Set("Authorization", "Bearer x")
testHandler(t, upstream.URL).ServeHTTP(w, r)
server.Handler.ServeHTTP(w, r)
if w.Code != 502 || strings.TrimSpace(w.Body.String()) != `{"error":"upstream_unavailable"}` {
t.Fatalf("status=%d body=%s", w.Code, w.Body)
}
line, err := os.ReadFile(output.Name())
if err != nil {
t.Fatal(err)
}
var entry struct {
Service, Outcome, Reason string
Status int
}
if err := json.Unmarshal(line, &entry); err != nil || bytes.Count(line, []byte{'\n'}) != 1 {
t.Fatalf("expected one JSON log line: %q", line)
}
if entry.Service != "builder-console" || entry.Status != 502 || entry.Outcome != "upstream_error" || entry.Reason != "platform_proxy_failed" || bytes.Contains(line, []byte("query-canary")) {
t.Fatalf("unexpected request log: %s", line)
}
}

func TestSSEIncremental(t *testing.T) {
Expand Down
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ module github.com/appthrust/builder-console
go 1.26.0

require (
github.com/appthrust/appserver-go v0.5.0
github.com/appthrust/appserver-go v0.7.0
github.com/google/uuid v1.6.0
github.com/lib/pq v1.12.3
github.com/santhosh-tekuri/jsonschema/v6 v6.0.2
Expand Down
4 changes: 2 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
github.com/appthrust/appserver-go v0.5.0 h1:MrQBL5rMvBEOsGc9vGiGV3cVOEZuSjIf2CkismPO2mo=
github.com/appthrust/appserver-go v0.5.0/go.mod h1:0KSf9RQZ6OxBcE0lxYzzjKF37nfwbKps3wXUGlE0oFo=
github.com/appthrust/appserver-go v0.7.0 h1:Fd1G+l/Ys0aAAiI2ZmMKqAz4qY6Dv7WwsF/rZtUgPUA=
github.com/appthrust/appserver-go v0.7.0/go.mod h1:0KSf9RQZ6OxBcE0lxYzzjKF37nfwbKps3wXUGlE0oFo=
github.com/coreos/go-oidc/v3 v3.20.0 h1:EtE0WIBHk03N+DqGkY4+UONzzZHk7amKt6IyNd7OsZE=
github.com/coreos/go-oidc/v3 v3.20.0/go.mod h1:DYCf24+ncYi+XkIH97GY1+dqoRlbaSI26KVTCI9SrY4=
github.com/davecgh/go-spew v1.1.2-0.20180830191138-d8f796af33cc h1:U9qPSI2PIWSS1VwoXQT9A3Wy9MM3WgvqSxFWenqJduM=
Expand Down