Bringing Order to Agent-Written Code

Introducing goffer, our in-house linter framework

COEFL Go-JP #4

Dennis(デニス) Metzger(メッツガー)

Backend Engineer @ Finatext

Are AI agents
writing your code?

Agent-written code

  • It compiles ✅
  • Tests pass ✅
  • go vet has no complaints ✅

But then you review it…

  • Initialization that bypasses constructors
  • Imports that ignore the architecture
  • Lots of similar tests
  • Comment language that changes on a whim
  • Rules in markdown files are ignored

The age of LLMs is the age of linters

  • AGENTS.md
  • SKILL.md
  • "Fix the linter warnings" ✅️

goffer

  • Finatext's in-house linter framework
  • Unlike golangci-lint, focused on
    project-specific rules
  • Designed as an oracle for agentic coding

One config file: .goffer.yaml

  1. # .goffer.yaml
  2. similartest:
  3. threshold: 90
  4. min_statements: 2
  5. forbid:
  6. functions:
  7. - ban: ^time\.Now\(\) time\.Time$

Rules and targeting, all declared in one place

Scope: where to look

  1. scope:
  2. include:
  3. packages:
  4. - ^myapp/internal/
  5. exclude:
  6. files:
  7. - \.gen\.go$
  8. functions:
  9. - \.UnmarshalJSON\(

Filter packages, files, and functions with regexes

include / exclude

  • include: matching any pattern is enough
    (empty means everything is in)
  • exclude: anything that matches is out
  • exclude wins over include

Scope has three layers

  1. scope: # ① shared by all linters
  2. exclude:
  3. files:
  4. - \.gen\.go$
  5. englishonly:
  6. scope: # ② per linter
  7. exclude:
  8. packages:
  9. - ^myapp/legacy/
  10. forbid:
  11. functions:
  12. - ban: ^time\.Now\(\) time\.Time$
  13. scope: # ③ per rule
  14. exclude:
  15. packages:
  16. - ^myapp/internal/clock$

All layers are ANDed

Target: what to report

  1. enforceconstructor:
  2. target:
  3. include:
  4. types:
  5. - \.(Amount|Order)$
  • scope = where to analyze
  • target = what to report
  • target/allow in allowimporters and
    ban in forbid follow the same idea

Adopt it incrementally

  • Exclude generated and legacy code
  • Include only new packages
  • Be strict only on the code
    agents actually touch

Let the LLM discover the rules

Existing code
Review comments
Architecture docs
↓
LLM
↓
Proposed .goffer.yaml
↓
Human review
↓
Deterministic enforcement

Five linters

  • enforceconstructor - enforce constructor usage
  • allowimporters - restrict imports
  • forbid - ban APIs
  • similartest - detect similar tests
  • englishonly - keep code in English

enforceconstructor

Flags struct literals that
bypass the constructor

  1. enforceconstructor:
  2. scope:
  3. include:
  4. packages:
  5. - myapp/
  1. package payment
  2. type Amount struct {
  3. Value int
  4. Currency string
  5. }
  6. func NewAmount(v int, c string) (Amount, error) {
  7. if v <= 0 {
  8. return Amount{}, ErrInvalidAmount
  9. }
  10. return Amount{Value: v, Currency: c}, nil
  11. }
  1. func chargeUser(u User, total int) error {
  2. amount := payment.Amount{
  3. Value: total,
  4. Currency: "JPY",
  5. }
  6. return charge(u, amount)
  7. }
  1. payment.Amount should not be initialized directly;
  2. use payment.NewAmount instead

allowimporters

Restricts who may import a package

  1. allowimporters:
  2. rules:
  3. - target: ^myapp/internal/domain$
  4. allow:
  5. - ^myapp/internal/service$
  6. - ^myapp/cmd/
  1. package handler
  2. import (
  3. "net/http"
  4. "myapp/internal/domain"
  5. )
  1. myapp/internal/handler is not allowed to import
  2. myapp/internal/domain

forbid

Bans specific packages, functions, methods, and types

  1. forbid:
  2. functions:
  3. - ban: ^time\.Now\(\) time\.Time$
  4. message: use internal/clock.Now() instead
  1. func (s *Service) Create(o Order) error {
  2. o.CreatedAt = time.Now()
  3. return s.repo.Save(o)
  4. }
  1. use internal/clock.Now() instead

similartest

  • Finds Test functions with near-identical structure
  • Normalizes away names and literals and compares structure only
  • → candidates for a table-driven test
  1. func TestValidate_Empty(t *testing.T) {
  2. err := validate("")
  3. if err == nil {
  4. t.Fatal("expected error")
  5. }
  6. }
  7. func TestValidate_TooLong(t *testing.T) {
  8. err := validate("aaaaaaaaaaaaaaaaaaaaaaaa")
  9. if err == nil {
  10. t.Fatal("expected error")
  11. }
  12. }
  1. test is 100% similar to TestValidate_TooLong
  2. test is 100% similar to TestValidate_Empty

englishonly

  • Flags comments and test names not written in English
  • No statistical language detection,
    based on Unicode scripts
  • Skips generated files, directives, and URLs
  1. // ユーザーの残高をチェックする
  2. func validateBalance(u User) error {
  3. if u.Balance < 0 {
  4. return ErrInsufficientBalance
  5. }
  6. return nil
  7. }
  8. // issues a receipt (領収書)
  9. func issueReceipt(o Order) (Receipt, error) {
  10. return generateReceipt(o)
  11. }
  1. comment is not written in English
  2. (found Katakana characters)

A single word in parentheses like (領収書) is fine as a gloss

  1. func TestValidate(t *testing.T) {
  2. tests := []struct{ name, in string }{
  3. {name: "空文字はエラー", in: ""},
  4. }
  5. for _, tt := range tests {
  6. t.Run(tt.name, func(t *testing.T) {
  7. // ...
  8. })
  9. }
  10. }
  1. test name "空文字はエラー" is not written in English
  2. (found Han characters)

Usage

  • Locally / by the agent: goffer ./...
  • CI: GitHub Action with inline PR annotations
  • The agent instruction is one line:
    "make goffer pass"

Recap

  • Project rules belong in a linter,
    not in a document
  • goffer = go/analysis + YAML,
    rules managed declaratively
  • An oracle for agents:
    code quality enforced mechanically