Repository navigation
refactor(api): createSession as a controller - #2728
Merged
Merged
Conversation
aliev
requested changes
Nov 30, 2026
The types createSession takes and returns now live in sessions/payload, since a controller in its own package cannot import internal/api without an import cycle. The types other operations use too, such as Greeting, are in schema. internal/api keeps an alias for each one, so no other code changes and openapi.yaml stays the same. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
createSession is the first operation in the new layout: a controller with a Spec and a Handle method, its payload, and a routes.go that mounts it. The controller gets what it needs through a Deps interface and no longer sees the Server. The checks run in the same order and openapi.yaml stays the same. Spec.Events is only a placeholder, nothing fills or reads it yet. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
createSession's request is still refused by its Resolve and its serverside tags, as #2729 made it. humabind runs the same check register does. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
kanat
force-pushed
the
refactor/controller
branch
from
November 30, 2026 14:52
18abba6 to
3830ff9
Compare
aliev
approved these changes
Nov 30, 2026
kanat
marked this pull request as ready for review
November 30, 2026 15:38
👋 Hi Kanat, want a second pair of eyes on this?I'm Jeffery, Stream's review agent. I look for bugs, regressions, missing tests and slow paths, and I only post findings I've checked against the code.
Or comment |
openapi.go now builds the humabind.API once, so every feature registers its controllers on the same one. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
register and humabind both run the list in interceptors.go before the handler. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
They only check the input and may refuse it. The list now lives in validate.go. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
routes.go builds the humabind.API from them. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
ValidateFunc turns a function into a Validator, the way http.HandlerFunc does. The test hook moves to humabind, because validators now only run on requests. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
The operation argument had no reader and the interface had one implementation, so the list now holds functions and needs no conversion. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
The API is wrapped once, in newAPI, and the validators are added next to the middleware, so sessions.Register no longer takes them. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
RegisterOperation keeps the request's type in the operation's Metadata, and the test of the validation rules reads it from the registered operations instead of setting a global hook. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
No production code serves TestRequestsFollowTheValidationRules any more. The registry holds the types of the bodies, so the test no longer checks serverside parameters or a pointer body tagged required. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This covers the two rules Huma's registry cannot: serverside parameters and a pointer body tagged required. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Co-authored-by: Cursor <cursoragent@cursor.com>
This was referenced Dec 1, 2026
kanat
added a commit
that referenced
this pull request
Dec 1, 2026
`createSession` moved to a controller, but nothing told the next author how to write one, and the root `AGENTS.md` still sent every new operation to `register` and `policies.go`. An agent reading both followed whichever it read first. Part of AI-4467. ## How it works ``` root AGENTS.md «HTTP API» ├─ new operation -> acceleration/internal/api/AGENTS.md (controllers) └─ legacy ops -> register in validate.go (do not add more) ``` ## Changes | Change | Where | | --- | --- | | Controller guide: Spec fields, `Spec.Name` rule, `humabind.Option`, wiring in `newAPI` after `UseValidator`, errors, validation checks, testing via `RouterSuite` (#2728) | `acceleration/internal/api/AGENTS.md` (+ `CLAUDE.md` -> `@AGENTS.md`) | | `register`/`policies.go` marked legacy, `endpoint.AccessClientAccessible` named | root `AGENTS.md` | | `Deps` comment says it is a migration adapter, not the pattern | `sessions/controller/deps.go` | Facts the guide states that are easy to get wrong: - `Spec.Events` is declared but no code reads it (`humabind/humabind.go`, `operation()`). - A validator only applies to controllers registered after `UseValidator` (`humabind/humabind.go:54`, `openapi.go:359`). - `APIError` constructors are unexported in `apierror.go`, so a controller gets refusals from its deps until they move to a leaf package. ## Validation Docs and one comment only. `go build ./internal/api/...` and `gofmt` pass. --------- Co-authored-by: Cursor <cursoragent@cursor.com>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Why
Today every API operation is a method on
Server, in one large package, and can reach everything the server holds. This PR shows what a controller looks like in the new layout and how it is registered, using one operation:createSession(POST /v1/agents/sessions). The API itself does not change,openapi.yamlis the same as onmain.How it works
The controller
Every operation becomes a struct that implements one interface:
Specsays what the operation is andHandledoes the work. A controller does not know aboutServer.This is
createSession, without the body ofHandle:The input and output are structs with a
Bodyfield, because Huma reads the request body from a field with that name. Path, query and header parameters go next to it:What the spec is for
The spec keeps everything we declare about an operation in one place, next to its code. It is plain data with no Huma types in it, and it can be read without a running server, which is how
openapi.yamlis generated.Name,Summary,Description,Tags,Deprecatedopenapi.yaml, which the SDKs are generated fromStatus,StatusDescriptionAccessErrorsEventsHow a controller is registered
Each feature has a
routes.gowith its paths:newAPIinopenapi.gowraps the Huma API once and adds the validators next to the middleware:humabindalso hasGet,Put,PatchandDelete.How the request is checked
The checks from #2729 stay as they are, so
createSessionanswers the same errors as onmain:internal/api/validate.go. The controllers get it fromUseValidator, and the other operations fromregister, so neither can skip theserversidetags.UseValidatorhas to come before the operations, asUseMiddlewaredoes. Today the list holds onlyvalidateServerSideFields.Resolveis on the type inpayload. Its rules read the caller, which onlyinternal/apiknows, sointernal/apisets them inpayload.CreateSessionProblems. This is a workaround, likeDeps.TestRequestsFollowTheValidationRulesno longer needs a hook in production code. It reads the types of the request bodies from the schema registry of Huma. The registry does not hold path, query and header parameters, or whetherBodyis a pointer, so the newTestRequestStructsFollowTheValidationRuleschecks those in the source.Dependencies (not final)
A controller gets its dependencies when it is constructed, as
CreateSession{Deps: deps}.The
Depsinterface in this PR is a workaround, please do not review it as the final design.createSessioncalls many helpers onServer, andDepsis the quick way to move those calls out of the controller.session_deps.goimplements it by calling the existing helpers.Dependencies do not have to look like this. Later they may return layers that several controllers can share, instead of one method for each helper
createSessionhappens to need.Folders
Changes
It is easier to review the commits one by one.
38570a9bfonly moves types. A controller in its own package cannot importinternal/apiwithout an import cycle, so the types it uses moved out, andinternal/apikeeps an alias for each one.55e8ce933adds the new layout. This is the commit to read closely.4136c79e9addresses the review:humabind.registeris unexported andendpoint.gois nowcontroller.go.3830ff94ekeeps the request checks of AI-4429: standardize router API input validation #2729 after the rebase.81653ebd8,14c5a659danda408b0aadname those checks validators and list them in one place, invalidate.go.52c472b2dpasses that list tosessions.Register.140ccf9b4makesValidatoran interface and addsValidateFunc, which turns a function into aValidator.bb34d0716makes a validator a plain function,ValidateFunc, and removes theValidatorinterface again.768d7ebfbaddshumabind.NewandUseValidator, sosessions.Registerno longer takes the validators.9118cc0d8,6161455f3and4f677c536remove the global hook that onlyTestRequestsFollowTheValidationRulesused, and addTestRequestStructsFollowTheValidationRules.All the other operations stay where they are.
Validation
go build ./...,go vet ./internal/api/..., also with-tags integration, at4f677c536go test ./internal/api/...at4f677c536go test -tags integration ./internal/apiat3830ff94e, before the validator commitsTestResponsesAskedSuitefail becauseffmpegis not installed on my machine, and the same 2 fail onmain(07fa7a972). No other test failsgo run ./cmd/openapiat4f677c536openapi.yamlis identical tomaincreateSession(25 bodies, as an anonymous caller, a user and a backend), at3830ff94e, before the validator commitsmainTestCreateSessionRefusesItsRequestOverHTTPfails whencreateSessionskips theserversidetags orResolve. The existing HTTP tests ininternal/apicover the rest.What each new test in
humabind_test.gocatchesTestBodyControllerDecodesRequestAndWritesStatusAndResponseTestValidatorsRefuseInOrderBeforeTheControllerTestBodyControllerRefusesMissingRequestBodyTestBodyControllerUsesWireDTOComponentsInOpenAPIx-client-accessibleTestAccessControlsClientAccessibleOpenAPIExtensionAccessis acceptedTestUnknownAccessPanicsAtRegistrationTestControllerErrorSetsHTTPStatusTestNonBodyControllerReceivesPathParametersDirectly🤖 Generated with Claude Code