Repository navigation
AI-4467: guide for writing API controllers - #2804
Conversation
Co-authored-by: Cursor <cursoragent@cursor.com>
👋 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 |
kanat
left a comment
There was a problem hiding this comment.
VERDICT: NO-GO
Round-1 review at 55ec640 (base a84ee77 is an ancestor, so the merge is a no-op). Blocking: R1.1 and R1.2.
[Should fix] R1.2 .claude/skills/go/api.md:95-104 (not in the diff): its example still registers a new operation with register() (registerWidgets). That is the opposite of this guide and of the new root AGENTS.md line, and go/SKILL.md sends Go authors there. Also, go/SKILL.md:48 points at .claude/skills/go-testing/api.md, which does not exist (the file is go/api.md). Fix: point the example at a controller registered with humabind (or add a line sending new operations to acceleration/internal/api/AGENTS.md), and fix the path on line 48.
[Question] R1.4 .thoughts/refactor-sessions-controller-deps.md PR 5 plans sessions/controller/create_session_test.go (CreateSessionControllerSuite), which unit-tests Handle with a zero Deps{} (no fake) for the missing-customer and nil-sessions guards. Guide line 120 says unit tests are for pure helpers only. Kanat to decide: allow that shape in the guide, or drop those tests from the plan. Goes to a ticket.
Checked and true at the head:
Spec.Eventsis never read:humabind.go:115-160reads every other field, and onlycreate_session.goandhumabindimportendpoint.- A validator only applies to controllers registered after
UseValidator:humabind.go:76copiesapi.validatorsat registration, andopenapi.go:360comes before:392. TestRequestStructsFollowTheValidationRulescovers*/payload. Probe:serverside:"yes"onsessions/payload/session.go:34fails it, naming that line.- Staff-only and customer-less are set through
Operation.Security(openapi.go:432,manage.go:125), so they belong in ahumabind.Option. - Status 0 means 200, and access defaults to server-only.
- The Deps guidance matches the plan's final struct and its leaf packages (
apierr,agentconfig,callapp). openapi.yamlis unchanged aftergo run ./cmd/openapi.go build ./internal/api/...,go vet ./internal/api/sessions/...andgofmtare clean.
|
|
||
| ## Testing | ||
|
|
||
| Never fake a `Deps` to unit-test `Handle`. Test a controller's behavior over HTTP through |
There was a problem hiding this comment.
[Should fix] R1.1 This section never says where a controller's test goes. RouterSuite lives in package api, in router_suite_test.go:228, a _test file, so <resource>/controller cannot reach it. Meanwhile .claude/skills/go-testing/controllers.md:7 says "Put a test next to the controller it tests". With the new layout, that sends the test into the subpackage, where the only way to test Handle is a fake Deps, which this line forbids. Fix: say that the HTTP test lives in internal/api as <resource>_<operation>_test.go (session_create_test.go tests sessions/controller/create_session.go), and update controllers.md:7 to match.
There was a problem hiding this comment.
Fixed in b845a76: added a paragraph to Testing saying the HTTP test lives in internal/api as <resource>_<operation>_test.go and that RouterSuite is in package api / cannot be embedded from a subpackage; updated go-testing/controllers.md:7 to match.
| ## Errors | ||
|
|
||
| Return an `APIError` so the failure is the `{"error": ...}` envelope with its own status and | ||
| code; any other error is a 500. `APIError` and its constructors (`invalidRequest`, `notFound`, |
There was a problem hiding this comment.
[Nit] R1.3 APIError itself is exported (apierror.go:184). Only the constructors and errX values are unexported, and the real barrier is the import direction: api imports sessions (openapi.go:392). Suggest: "live in package api (apierror.go), which a controller cannot import; the constructors are unexported there." A related point on line 43: the access line logs the PascalCased id (CreateSession, from pascalCase at openapi.go:506), not Spec.Name as written.
There was a problem hiding this comment.
Fixed in 2689e4a: APIError is now said to be exported; wording changed to explain the import direction (internal/api imports sessions/controller, not the reverse) is the real barrier; access log entry changed to PascalCased form (CreateSession).
kanat
left a comment
There was a problem hiding this comment.
Text review: 1 unclear of 25
[Nit] T1.1 pr — PR opening — the opening is under ## Why (a heading, not plain prose) and contains PR number #2728. Per the repo's PR description rule: at most three plain sentences before the chart, no headings, no PR numbers.
Remove the ## Why heading and move #2728 to the tables section (or a <details> block). The three sentences themselves are good; they just need to stand as plain prose before the chart:
createSessionmoved to a controller, but nothing told the next author how to write one, and the rootAGENTS.mdstill sent every new operation toregisterandpolicies.go. An agent reading both followed whichever it read first.
| - A new operation is a controller: read `acceleration/internal/api/AGENTS.md`. The operations | ||
| still in `package api` are declared with `register` (`internal/api/validate.go`, not | ||
| `huma.Register`), as `internal/api/policies.go` is; do not add new ones that way. |
There was a problem hiding this comment.
[Nit] T1.2 — On cold read, (internal/api/validate.go, not huma.Register) is ambiguous: it looks like a file path is being contrasted with a function name. The intent is to say register is the function in validate.go, not Huma's Register. Adding two words fixes it:
| - A new operation is a controller: read `acceleration/internal/api/AGENTS.md`. The operations | |
| still in `package api` are declared with `register` (`internal/api/validate.go`, not | |
| `huma.Register`), as `internal/api/policies.go` is; do not add new ones that way. | |
| - A new operation is a controller: read `acceleration/internal/api/AGENTS.md`. The operations | |
| still in `package api` use `register` (the function in `internal/api/validate.go`, not | |
| `huma.Register`), as `internal/api/policies.go` does; do not add new ones that way. |
There was a problem hiding this comment.
Fixed in 1ad001a: rewritten verbatim as requested — "use register (the function in internal/api/validate.go, not huma.Register), as internal/api/policies.go does".
Co-authored-by: Cursor <cursoragent@cursor.com>
…R1.1) Co-authored-by: Cursor <cursoragent@cursor.com>
…1.4) Co-authored-by: Cursor <cursoragent@cursor.com>
…R1.2) Co-authored-by: Cursor <cursoragent@cursor.com>
Co-authored-by: Cursor <cursoragent@cursor.com>
|
R1.2 Fixed in 410df48: replaced the R1.4 Fixed in 7662019: added to Testing in T1.1 Fixed (no commit): removed the |
kanat
left a comment
There was a problem hiding this comment.
VERDICT: GO
All five round-1 findings are fixed and verified against the code (file:line).
PRIOR:
- R1.1 — fixed —
controllers.md:7andAGENTS.md:122-126both now say HTTP test lives ininternal/apias<resource>_<operation>_test.go; verifiedrouter_suite_test.go:3ispackage api,session_create_test.goispackage api,sessions/controller/create_session.goispackage controller. - R1.2 — fixed —
.claude/skills/go/SKILL.md:48now points atgo/api.md(file confirmed);api.mdexample replaced withhumabind.Post+controller.Depsmatchingsessions/routes.goshape;humabindpackage confirmed atacceleration/internal/api/humabind/. - R1.3 — fixed —
AGENTS.md:42-43now says "access log uses the PascalCased form (CreateSession)";AGENTS.md:79-82now says "APIErroris exported; constructors unexported; barrier is import direction"; all three claims verified againstapierror.go:184,apierror.go:268-320,openapi.go:392,server.go:1359-1364. - R1.4 — fixed — new paragraph at
AGENTS.md:127-129matches orchestrator decision verbatim. - T1.1 — fixed — PR body opens with plain prose, no
## Whyheading,(#2728)in Changes table. - T1.2 — fixed — root
AGENTS.md:102-104reads the verbatim rewrite;validate.go:50andpolicies.go:140,154,165,177verified.
NEW FINDINGS: none in changed hunks.
BUILD: go build ./internal/api/... → pass; go vet ./internal/api/sessions/... → pass; gofmt -l internal/api/sessions → clean.
MUTATIONS: 0 checked (docs-only PR; all fixes are prose verified against code).
createSessionmoved to a controller, but nothing told the next author how to write one, and the rootAGENTS.mdstill sent every new operation toregisterandpolicies.go. An agent reading both followed whichever it read first. Part of AI-4467.How it works
Changes
Spec.Namerule,humabind.Option, wiring innewAPIafterUseValidator, errors, validation checks, testing viaRouterSuite(#2728)acceleration/internal/api/AGENTS.md(+CLAUDE.md->@AGENTS.md)register/policies.gomarked legacy,endpoint.AccessClientAccessiblenamedAGENTS.mdDepscomment says it is a migration adapter, not the patternsessions/controller/deps.goFacts the guide states that are easy to get wrong:
Spec.Eventsis declared but no code reads it (humabind/humabind.go,operation()).UseValidator(humabind/humabind.go:54,openapi.go:359).APIErrorconstructors are unexported inapierror.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/...andgofmtpass.