# Conventions and agents

Source: https://bogie-go.com/docs/conventions.html

Go has no autoload and no `method_missing`. Nothing exists until it is written down and registered, so every step that another framework would do for you has a visible line in a Bogie app, and the six rules below are what keep that visible shape consistent as the app grows.

## The six rules

1. **`ctx context.Context` is the first parameter of anything doing I/O.** No `context.TODO()`. Cancellation, deadlines and tracing travel in it, so a request that is abandoned stops its query instead of finishing work nobody will read.

   ```
   func (s *Store) GetPost(
   	ctx context.Context, id string,
   ) (models.Post, error) {
   	// ...
   	row, err := s.q.GetPost(ctx, pk)
   	// ...
   }

   // Not this: GetPost(id string), or
   // context.TODO() inside the body.
   ```
2. **Interfaces are declared where they are consumed**, next to the code that uses them, holding only the methods that code actually calls, never exported from the package that implements them. A controller's store interface lists five methods, not the whole store, so each stays short and readable, and there is no mocking library anywhere in a generated app.

   ```
   // beside PostsController
   type postsStore interface {
   	GetPost(ctx context.Context, id string) (models.Post, error)
   	// ...four more
   }

   // Not this: exporting a Store interface
   // from db/adapter next to *Store.
   ```
3. **`app/models` imports nothing third-party.** The type and its `Validate` can be read, and called from a test, without pulling in a database driver or an HTTP framework.

   ```
   package models

   import "fmt"

   func (p Post) Validate() error {
   	if p.Title == "" {
   		return fmt.Errorf(
   			"%w: title is required", ErrInvalid)
   	}
   	return nil
   }

   // Not this: import "github.com/gin-gonic/gin"
   ```
4. **Constructors return `(T, error)`.** Only `main` exits. A bad configuration fails at boot with a message, never with a panic in the middle of a handler.

   ```
   // config/config.go
   func Load() (Config, error)

   // db/adapter/store.go
   func Open(ctx context.Context, url string) (*Store, error) {
   	pool, err := pgxpool.New(ctx, url)
   	if err != nil {
   		return nil, fmt.Errorf("store: open database: %w", err)
   	}
   	// ...
   }

   // main.go: the only place that exits
   if err := run(ctx, args); err != nil {
   	fmt.Fprintln(os.Stderr, "blog:", err)
   	os.Exit(1)
   }
   ```
5. **`*gin.Context` never travels below `app/controllers`.** A handler converts HTTP into model values, calls one function, and converts the result back. Everything below that line can be called from a job, a CLI, or a test, not only from a request.

   ```
   func (ctl *PostsController) Show(c *gin.Context) {
   	p, err := ctl.Store.GetPost(
   		c.Request.Context(), c.Param("id"))
   	if err != nil {
   		ctl.fail(c, err)
   		return
   	}
   	c.JSON(http.StatusOK, postView(p))
   }

   // Below the controller, no *gin.Context:
   // GetPost(ctx context.Context, id string)
   ```
6. **No `util/`, `helpers/` or `common/`.** A package is named for what it provides, which is a required prefix at every call site, so the name has to say something.

   ```
   svc := publish_post.New(store, log)
   err := svc.Run(ctx)

   // Not this: util.Publish, helpers.Post,
   // or anything in common/
   ```

## The layout is by role, on purpose

`app/controllers`, `app/services`, `app/models`, `db/migrate`: every file is where its job says it should be, and the generators keep it that way deliberately. Do not "fix" this into a different Go layout; it is not an oversight, it is the point. There is no `spec/` directory: Go tests sit beside the code they test, written with Ginkgo and Gomega, and what packages share (the test database, factories, the whole app) lives in `test/`.

## Underscored namespace packages

A namespace (everything mounted under `/admin`, say) is a Go package, `admin_controllers`, plural like every other directory, one per namespace. Go style would spell that `admincontrollers`; `.golangci.yml` disables the ST1003 naming check for exactly this, and nothing else, so the readable name can stand without the linter fighting the layout it was asked to keep.

## AGENTS.md and CLAUDE.md, and why generators are the deterministic path

Every generated app ships an `AGENTS.md` (with `CLAUDE.md` as the same content) holding the six rules above, every command, and a recipe per generator. It exists because an agent working on a Bogie app has the same problem a new hire does: nothing autoloads, so a controller that is written but not registered compiles, lints clean, and serves nothing. Running `bogie g controller` instead of writing the file by hand gets the registration right every time, in the same three places, whether the hand on the keyboard is a person's or an agent's; `bogie doctor` is the check that confirms it actually happened.
