Bringing Order to Agent-Written Code
Introducing goffer, our in-house linter framework
Backend Engineer @ Finatext
Are AI agents
writing your code?
Agent-written code
go vet has no complaints ✅But then you review it…
The age of LLMs is the age of linters
One config file: .goffer.yaml
- # .goffer.yaml
- similartest:
- threshold: 90
- min_statements: 2
- forbid:
- functions:
- - ban: ^time\.Now\(\) time\.Time$
Rules and targeting, all declared in one place
- scope:
- include:
- packages:
- - ^myapp/internal/
- exclude:
- files:
- - \.gen\.go$
- functions:
- - \.UnmarshalJSON\(
Filter packages, files, and functions with regexes
include: matching any pattern is enoughexclude: anything that matches is out
- scope: # ① shared by all linters
- exclude:
- files:
- - \.gen\.go$
- englishonly:
- scope: # ② per linter
- exclude:
- packages:
- - ^myapp/legacy/
- forbid:
- functions:
- - ban: ^time\.Now\(\) time\.Time$
- scope: # ③ per rule
- exclude:
- packages:
- - ^myapp/internal/clock$
All layers are ANDed
- enforceconstructor:
- target:
- include:
- types:
- - \.(Amount|Order)$
target/allow in allowimporters andban in forbid follow the same idea.goffer.yamlenforceconstructor - enforce constructor usageallowimporters - restrict importsforbid - ban APIssimilartest - detect similar testsenglishonly - keep code in EnglishenforceconstructorFlags struct literals that
bypass the constructor
- enforceconstructor:
- scope:
- include:
- packages:
- - myapp/
- package payment
- type Amount struct {
- Value int
- Currency string
- }
- func NewAmount(v int, c string) (Amount, error) {
- if v <= 0 {
- return Amount{}, ErrInvalidAmount
- }
- return Amount{Value: v, Currency: c}, nil
- }
- func chargeUser(u User, total int) error {
- amount := payment.Amount{
- Value: total,
- Currency: "JPY",
- }
- return charge(u, amount)
- }
- payment.Amount should not be initialized directly;
- use payment.NewAmount instead
allowimportersRestricts who may import a package
- allowimporters:
- rules:
- - target: ^myapp/internal/domain$
- allow:
- - ^myapp/internal/service$
- - ^myapp/cmd/
- package handler
- import (
- "net/http"
- "myapp/internal/domain"
- )
- myapp/internal/handler is not allowed to import
- myapp/internal/domain
forbidBans specific packages, functions, methods, and types
- forbid:
- functions:
- - ban: ^time\.Now\(\) time\.Time$
- message: use internal/clock.Now() instead
- func (s *Service) Create(o Order) error {
- o.CreatedAt = time.Now()
- return s.repo.Save(o)
- }
- use internal/clock.Now() instead
similartestTest functions with near-identical structure
- func TestValidate_Empty(t *testing.T) {
- err := validate("")
- if err == nil {
- t.Fatal("expected error")
- }
- }
- func TestValidate_TooLong(t *testing.T) {
- err := validate("aaaaaaaaaaaaaaaaaaaaaaaa")
- if err == nil {
- t.Fatal("expected error")
- }
- }
- test is 100% similar to TestValidate_TooLong
- test is 100% similar to TestValidate_Empty
englishonly
- // ユーザーの残高をチェックする
- func validateBalance(u User) error {
- if u.Balance < 0 {
- return ErrInsufficientBalance
- }
- return nil
- }
- // issues a receipt (領収書)
- func issueReceipt(o Order) (Receipt, error) {
- return generateReceipt(o)
- }
- comment is not written in English
- (found Katakana characters)
A single word in parentheses like (領収書) is fine as a gloss
- func TestValidate(t *testing.T) {
- tests := []struct{ name, in string }{
- {name: "空文字はエラー", in: ""},
- }
- for _, tt := range tests {
- t.Run(tt.name, func(t *testing.T) {
- // ...
- })
- }
- }
- test name "空文字はエラー" is not written in English
- (found Han characters)
goffer ./...go/analysis + YAML,