Skip to content

AI-4467: guide for writing API controllers - #2804

Merged
kanat merged 6 commits into
mainfrom
docs/api-controllers-agents-md
Dec 1, 2026
Merged

kanat merged 6 commits into
mainfrom
docs/api-controllers-agents-md

Conversation

@kanat

@kanat kanat commented Dec 1, 2026 •

Copy link
Copy Markdown
Collaborator

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>
@github-actions github-actions Bot added the docs label Dec 1, 2026
@kanat
kanat marked this pull request as ready for review December 1, 2026 01:56
@getstream-metis

Copy link
Copy Markdown

👋 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.

  • Review this PR

Or comment /jeffery review. I'll stay quiet unless you ask.

@kanat kanat left a comment

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.Events is never read: humabind.go:115-160 reads every other field, and only create_session.go and humabind import endpoint.
  • A validator only applies to controllers registered after UseValidator: humabind.go:76 copies api.validators at registration, and openapi.go:360 comes before :392.
  • TestRequestStructsFollowTheValidationRules covers */payload. Probe: serverside:"yes" on sessions/payload/session.go:34 fails it, naming that line.
  • Staff-only and customer-less are set through Operation.Security (openapi.go:432, manage.go:125), so they belong in a humabind.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.yaml is unchanged after go run ./cmd/openapi. go build ./internal/api/..., go vet ./internal/api/sessions/... and gofmt are clean.


## Testing

Never fake a `Deps` to unit-test `Handle`. Test a controller's behavior over HTTP through

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[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.

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Comment thread acceleration/internal/api/AGENTS.md Outdated
## 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`,

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[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.

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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 kanat left a comment

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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:

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.

Comment thread AGENTS.md Outdated
Comment on lines +102 to +104
- 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.

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[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:

Suggested change
- 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.

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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".

kanat and others added 5 commits November 30, 2026 21:04
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>
@kanat

kanat commented Dec 1, 2026

Copy link
Copy Markdown
Collaborator Author

R1.2 Fixed in 410df48: replaced the registerWidgets / register() example in .claude/skills/go/api.md with a humabind.Post controller registration and a pointer to acceleration/internal/api/AGENTS.md; fixed .claude/skills/go/SKILL.md line 48 path from go-testing/api.md to go/api.md.

R1.4 Fixed in 7662019: added to Testing in acceleration/internal/api/AGENTS.md — once Deps is a struct of real layers, a unit test may call Handle with a zero Deps{} to pin a refusal that answers before any layer; fakes/stubs are still never allowed.

T1.1 Fixed (no commit): removed the ## Why heading so the three sentences are plain prose at the top; moved #2728 into the Changes table row for the controller guide.

@kanat kanat left a comment

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

VERDICT: GO

All five round-1 findings are fixed and verified against the code (file:line).

PRIOR:

  • R1.1 — fixed — controllers.md:7 and AGENTS.md:122-126 both now say HTTP test lives in internal/api as <resource>_<operation>_test.go; verified router_suite_test.go:3 is package api, session_create_test.go is package api, sessions/controller/create_session.go is package controller.
  • R1.2 — fixed — .claude/skills/go/SKILL.md:48 now points at go/api.md (file confirmed); api.md example replaced with humabind.Post+controller.Deps matching sessions/routes.go shape; humabind package confirmed at acceleration/internal/api/humabind/.
  • R1.3 — fixed — AGENTS.md:42-43 now says "access log uses the PascalCased form (CreateSession)"; AGENTS.md:79-82 now says "APIError is exported; constructors unexported; barrier is import direction"; all three claims verified against apierror.go:184, apierror.go:268-320, openapi.go:392, server.go:1359-1364.
  • R1.4 — fixed — new paragraph at AGENTS.md:127-129 matches orchestrator decision verbatim.
  • T1.1 — fixed — PR body opens with plain prose, no ## Why heading, (#2728) in Changes table.
  • T1.2 — fixed — root AGENTS.md:102-104 reads the verbatim rewrite; validate.go:50 and policies.go:140,154,165,177 verified.

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).

@kanat
kanat merged commit 3e73c6a into main Dec 1, 2026
13 checks passed
@kanat
kanat deleted the docs/api-controllers-agents-md branch December 1, 2026 02:28
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant